blob: 6dff1ba0e10e6f20665810179f0c578a5b18e5d8 [file] [log] [blame]
Cyril Hrubis85e787e2014-06-18 17:09:21 +02001/*
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 <errno.h>
28#include <unistd.h>
29#include <stdlib.h>
30#include <linux/loop.h>
31#include "test.h"
32#include "safe_macros.h"
33
34#ifndef LOOP_CTL_GET_FREE
35# define LOOP_CTL_GET_FREE 0x4C82
36#endif
37
38#define LOOP_CONTROL_FILE "/dev/loop-control"
39
40#define DEV_FILE "test_dev.img"
41
42static char dev_path[1024];
43static int device_acquired;
44
45static const char *dev_variants[] = {
46 "/dev/loop%i",
47 "/dev/loop/%i"
48};
49
50static int set_dev_path(int dev)
51{
52 unsigned int i;
53 struct stat st;
54
55 for (i = 0; i < ARRAY_SIZE(dev_variants); i++) {
56 snprintf(dev_path, sizeof(dev_path), dev_variants[i], dev);
57
58 if (stat(dev_path, &st) == 0 && S_ISBLK(st.st_mode))
59 return 1;
60 }
61
62 return 0;
63}
64
65static int find_free_loopdev(void)
66{
67 int ctl_fd, dev_fd, rc, i;
68 struct loop_info loopinfo;
69
70 /* since Linux 3.1 */
71 ctl_fd = open(LOOP_CONTROL_FILE, O_RDWR);
72
73 if (ctl_fd > 0) {
74 rc = ioctl(ctl_fd, LOOP_CTL_GET_FREE);
75 close(ctl_fd);
76 if (rc >= 0) {
77 set_dev_path(rc);
78 tst_resm(TINFO, "Found free device '%s'", dev_path);
79 return 0;
80 }
81 tst_resm(TINFO, "Couldn't find free loop device");
82 return 1;
83 }
84
85 switch (errno) {
86 case ENOENT:
87 break;
88 default:
89 tst_resm(TBROK | TERRNO, "Failed to open " LOOP_CONTROL_FILE);
90 }
91
92 /*
93 * Older way is to iterate over /dev/loop%i and /dev/loop/%i and try
94 * LOOP_GET_STATUS ioctl() which fails for free loop devices.
95 */
96 for (i = 0; i < 256; i++) {
97
98 if (!set_dev_path(i))
99 continue;
100
101 dev_fd = open(dev_path, O_RDONLY);
102
103 if (dev_fd < 0)
104 continue;
105
106 if (ioctl(dev_fd, LOOP_GET_STATUS, &loopinfo) == 0) {
107 tst_resm(TINFO, "Device '%s' in use", dev_path);
108 } else {
109 if (errno != ENXIO)
110 continue;
111 tst_resm(TINFO, "Found free device '%s'", dev_path);
112 close(dev_fd);
113 return 0;
114 }
115
116 close(dev_fd);
117 }
118
119 tst_resm(TINFO, "No free devices found");
120
121 return 1;
122}
123
124static void attach_device(void (*cleanup_fn)(void),
125 const char *dev, const char *file)
126{
127 int dev_fd, file_fd, err;
128
129 dev_fd = SAFE_OPEN(cleanup_fn, dev, O_RDWR);
130 file_fd = SAFE_OPEN(cleanup_fn, file, O_RDWR);
131
132 if (ioctl(dev_fd, LOOP_SET_FD, file_fd) < 0) {
133 err = errno;
134 close(dev_fd);
135 close(file_fd);
136 tst_brkm(TBROK, cleanup_fn,
137 "ioctl(%s, LOOP_SET_FD, %s) failed: %s",
138 dev, file, tst_strerrno(err));
139 }
140
141 close(dev_fd);
142 close(file_fd);
143}
144
145static void detach_device(void (*cleanup_fn)(void), const char *dev)
146{
147 int dev_fd, err;
148
149 dev_fd = SAFE_OPEN(cleanup_fn, dev, O_RDONLY);
150
151 if (ioctl(dev_fd, LOOP_CLR_FD, 0) < 0) {
152 err = errno;
153 close(dev_fd);
154 tst_brkm(TBROK, cleanup_fn,
155 "ioctl(%s, LOOP_CLR_FD, 0) failed: %s",
156 dev, tst_strerrno(err));
157 }
158
159 close(dev_fd);
160}
161
162const char *tst_acquire_device(void (cleanup_fn)(void))
163{
164 char *dev;
165 struct stat st;
166
167 if (device_acquired)
168 tst_brkm(TBROK, cleanup_fn, "Device allready acquired");
169
170 if (!tst_tmpdir_created()) {
171 tst_brkm(TBROK, cleanup_fn,
172 "Cannot acquire device without tmpdir() created");
173 }
174
175 dev = getenv("LTP_DEV");
176
177 if (dev) {
178 tst_resm(TINFO, "Using test device LTP_DEV='%s'", dev);
179
180 SAFE_STAT(cleanup_fn, dev, &st);
181
182 if (!S_ISBLK(st.st_mode)) {
183 tst_brkm(TBROK, cleanup_fn,
184 "%s is not a block device", dev);
185 }
186
187 return dev;
188 }
189
190 if (tst_fill_file(DEV_FILE, 0, 1024, 20480)) {
191 tst_brkm(TBROK | TERRNO, cleanup_fn,
192 "Failed to create " DEV_FILE);
193
194 }
195
196 if (find_free_loopdev())
197 return NULL;
198
199 attach_device(cleanup_fn, dev_path, DEV_FILE);
200
201 device_acquired = 1;
202
203 return dev_path;
204}
205
206void tst_release_device(void (cleanup_fn)(void), const char *dev)
207{
208 if (getenv("LTP_DEV"))
209 return;
210
211 /*
212 * Loop device was created -> we need to deatch it.
213 *
214 * The file image is deleted in tst_rmdir();
215 */
216 detach_device(cleanup_fn, dev);
217
218 device_acquired = 0;
219}