1/*
2 * Copyright (C) 2014 Cyril Hrubis chrubis@suse.cz
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like.  Any license provided herein, whether implied or
15 * otherwise, applies only to this software file.  Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
24#include <sys/types.h>
25#include <sys/stat.h>
26#include <sys/ioctl.h>
27#include <sys/mount.h>
28#include <errno.h>
29#include <unistd.h>
30#include <stdlib.h>
31#include <linux/loop.h>
32#include <stdint.h>
33#include <inttypes.h>
34#include "test.h"
35#include "safe_macros.h"
36
37#ifndef LOOP_CTL_GET_FREE
38# define LOOP_CTL_GET_FREE 0x4C82
39#endif
40
41#define LOOP_CONTROL_FILE "/dev/loop-control"
42
43#define DEV_FILE "test_dev.img"
44#define DEV_SIZE_MB 256
45
46static char dev_path[1024];
47static int device_acquired;
48
49static const char *dev_variants[] = {
50	"/dev/loop%i",
51	"/dev/loop/%i"
52};
53
54static int set_dev_path(int dev)
55{
56	unsigned int i;
57	struct stat st;
58
59	for (i = 0; i < ARRAY_SIZE(dev_variants); i++) {
60		snprintf(dev_path, sizeof(dev_path), dev_variants[i], dev);
61
62		if (stat(dev_path, &st) == 0 && S_ISBLK(st.st_mode))
63			return 1;
64	}
65
66	return 0;
67}
68
69static int find_free_loopdev(void)
70{
71	int ctl_fd, dev_fd, rc, i;
72	struct loop_info loopinfo;
73
74	/* since Linux 3.1 */
75	ctl_fd = open(LOOP_CONTROL_FILE, O_RDWR);
76
77	if (ctl_fd > 0) {
78		rc = ioctl(ctl_fd, LOOP_CTL_GET_FREE);
79		close(ctl_fd);
80		if (rc >= 0) {
81			set_dev_path(rc);
82			tst_resm(TINFO, "Found free device '%s'", dev_path);
83			return 0;
84		}
85		tst_resm(TINFO, "Couldn't find free loop device");
86		return 1;
87	}
88
89	switch (errno) {
90	case ENOENT:
91	break;
92	case EACCES:
93		tst_resm(TINFO | TERRNO,
94		         "Not allowed to open " LOOP_CONTROL_FILE ". "
95			 "Are you root?");
96	break;
97	default:
98		tst_resm(TBROK | TERRNO, "Failed to open " LOOP_CONTROL_FILE);
99	}
100
101	/*
102	 * Older way is to iterate over /dev/loop%i and /dev/loop/%i and try
103	 * LOOP_GET_STATUS ioctl() which fails for free loop devices.
104	 */
105	for (i = 0; i < 256; i++) {
106
107		if (!set_dev_path(i))
108			continue;
109
110		dev_fd = open(dev_path, O_RDONLY);
111
112		if (dev_fd < 0)
113			continue;
114
115		if (ioctl(dev_fd, LOOP_GET_STATUS, &loopinfo) == 0) {
116			tst_resm(TINFO, "Device '%s' in use", dev_path);
117		} else {
118			if (errno != ENXIO)
119				continue;
120			tst_resm(TINFO, "Found free device '%s'", dev_path);
121			close(dev_fd);
122			return 0;
123		}
124
125		close(dev_fd);
126	}
127
128	tst_resm(TINFO, "No free devices found");
129
130	return 1;
131}
132
133static int attach_device(const char *dev, const char *file)
134{
135	int dev_fd, file_fd;
136
137	dev_fd = open(dev, O_RDWR);
138	if (dev_fd < 0) {
139		tst_resm(TWARN | TERRNO, "open('%s', O_RDWR) failed", dev);
140		return 1;
141	}
142
143	file_fd = open(file, O_RDWR);
144	if (file_fd < 0) {
145		tst_resm(TWARN | TERRNO, "open('%s', O_RDWR) failed", file);
146		close(dev_fd);
147		return 1;
148	}
149
150	if (ioctl(dev_fd, LOOP_SET_FD, file_fd) < 0) {
151		close(dev_fd);
152		close(file_fd);
153		tst_resm(TWARN | TERRNO, "ioctl(%s, LOOP_SET_FD, %s) failed",
154			 dev, file);
155		return 1;
156	}
157
158	close(dev_fd);
159	close(file_fd);
160	return 0;
161}
162
163static int detach_device(const char *dev)
164{
165	int dev_fd, ret, i;
166
167	dev_fd = open(dev, O_RDONLY);
168	if (dev_fd < 0) {
169		tst_resm(TWARN | TERRNO, "open(%s) failed", dev);
170		return 1;
171	}
172
173	/* keep trying to clear LOOPDEV until we get ENXIO, a quick succession
174	 * of attach/detach might not give udev enough time to complete */
175	for (i = 0; i < 40; i++) {
176		ret = ioctl(dev_fd, LOOP_CLR_FD, 0);
177
178		if (ret && (errno == ENXIO)) {
179			close(dev_fd);
180			return 0;
181		}
182
183		if (ret && (errno != EBUSY)) {
184			tst_resm(TWARN,
185				 "ioctl(%s, LOOP_CLR_FD, 0) unexpectedly failed with: %s",
186				 dev, tst_strerrno(errno));
187			close(dev_fd);
188			return 1;
189		}
190
191		usleep(50000);
192	}
193
194	close(dev_fd);
195	tst_resm(TWARN,
196		"ioctl(%s, LOOP_CLR_FD, 0) no ENXIO for too long", dev);
197	return 1;
198}
199
200const char *tst_acquire_device__(unsigned int size)
201{
202	int fd;
203	char *dev;
204	struct stat st;
205	unsigned int acq_dev_size;
206	uint64_t ltp_dev_size;
207
208	acq_dev_size = size > DEV_SIZE_MB ? size : DEV_SIZE_MB;
209
210	dev = getenv("LTP_DEV");
211
212	if (dev) {
213		tst_resm(TINFO, "Using test device LTP_DEV='%s'", dev);
214
215		if (stat(dev, &st)) {
216			tst_resm(TWARN | TERRNO, "stat() failed");
217			return NULL;
218		}
219
220		if (!S_ISBLK(st.st_mode)) {
221			tst_resm(TWARN, "%s is not a block device", dev);
222			return NULL;
223		}
224
225		fd = open(dev, O_RDONLY);
226		if (fd < 0) {
227			tst_resm(TWARN | TERRNO,
228				 "open(%s, O_RDONLY) failed", dev);
229			return NULL;
230		}
231
232		if (ioctl(fd, BLKGETSIZE64, &ltp_dev_size)) {
233			tst_resm(TWARN | TERRNO,
234				 "ioctl(fd, BLKGETSIZE64, ...) failed");
235			close(fd);
236			return NULL;
237		}
238
239		if (close(fd)) {
240			tst_resm(TWARN | TERRNO,
241				 "close(fd) failed");
242			return NULL;
243		}
244
245		ltp_dev_size = ltp_dev_size/1024/1024;
246
247		if (acq_dev_size <= ltp_dev_size) {
248			if (tst_fill_file(dev, 0, 1024, 512)) {
249				tst_resm(TWARN | TERRNO,
250					 "Failed to clear the first 512k of %s",
251					 dev);
252			}
253
254			return dev;
255		}
256
257		tst_resm(TINFO, "Skipping $LTP_DEV size %"PRIu64"MB, requested size %uMB",
258				ltp_dev_size, acq_dev_size);
259	}
260
261	if (tst_fill_file(DEV_FILE, 0, 1024, 1024 * acq_dev_size)) {
262		tst_resm(TWARN | TERRNO, "Failed to create " DEV_FILE);
263		return NULL;
264	}
265
266	if (find_free_loopdev())
267		return NULL;
268
269	if (attach_device(dev_path, DEV_FILE))
270		return NULL;
271
272	device_acquired = 1;
273
274	return dev_path;
275}
276
277const char *tst_acquire_device_(void (cleanup_fn)(void), unsigned int size)
278{
279	const char *device;
280
281	if (device_acquired) {
282		tst_brkm(TBROK, cleanup_fn, "Device allready acquired");
283		return NULL;
284	}
285
286	if (!tst_tmpdir_created()) {
287		tst_brkm(TBROK, cleanup_fn,
288		         "Cannot acquire device without tmpdir() created");
289		return NULL;
290	}
291
292	device = tst_acquire_device__(size);
293
294	if (!device) {
295		tst_brkm(TBROK, cleanup_fn, "Failed to acquire device");
296		return NULL;
297	}
298
299	return device;
300}
301
302int tst_release_device(const char *dev)
303{
304	int ret;
305
306	if (getenv("LTP_DEV"))
307		return 0;
308
309	/*
310	 * Loop device was created -> we need to deatch it.
311	 *
312	 * The file image is deleted in tst_rmdir();
313	 */
314	ret = detach_device(dev);
315
316	device_acquired = 0;
317
318	return ret;
319}
320
321int tst_umount(const char *path)
322{
323	int err, ret, i;
324
325	for (i = 0; i < 50; i++) {
326		ret = umount(path);
327		err = errno;
328
329		if (!ret)
330			return 0;
331
332		tst_resm(TINFO, "umount('%s') failed with %s, try %2i...",
333		         path, tst_strerrno(err), i+1);
334
335		if (i == 0 && err == EBUSY) {
336			tst_resm(TINFO, "Likely gvfsd-trash is probing newly "
337			         "mounted fs, kill it to speed up tests.");
338		}
339
340		usleep(100000);
341	}
342
343	tst_resm(TWARN, "Failed to umount('%s') after 50 retries", path);
344	errno = err;
345	return -1;
346}
347