Commit | Line | Data |
---|---|---|
d7e09d03 PT |
1 | /* |
2 | * GPL HEADER START | |
3 | * | |
4 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License version 2 only, | |
8 | * as published by the Free Software Foundation. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, but | |
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * General Public License version 2 for more details (a copy is included | |
14 | * in the LICENSE file that accompanied this code). | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * version 2 along with this program; If not, see | |
18 | * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf | |
19 | * | |
20 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, | |
21 | * CA 95054 USA or visit www.sun.com if you need additional information or | |
22 | * have any questions. | |
23 | * | |
24 | * GPL HEADER END | |
25 | */ | |
26 | /* | |
27 | * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved. | |
28 | * Use is subject to license terms. | |
29 | * | |
30 | * Copyright (c) 2011, 2012, Intel Corporation. | |
31 | */ | |
32 | /* | |
33 | * This file is part of Lustre, http://www.lustre.org/ | |
34 | * Lustre is a trademark of Sun Microsystems, Inc. | |
35 | */ | |
36 | ||
37 | /* | |
38 | * linux/drivers/block/loop.c | |
39 | * | |
40 | * Written by Theodore Ts'o, 3/29/93 | |
41 | * | |
42 | * Copyright 1993 by Theodore Ts'o. Redistribution of this file is | |
43 | * permitted under the GNU General Public License. | |
44 | * | |
45 | * Modularized and updated for 1.1.16 kernel - Mitch Dsouza 28th May 1994 | |
46 | * Adapted for 1.3.59 kernel - Andries Brouwer, 1 Feb 1996 | |
47 | * | |
48 | * Fixed do_loop_request() re-entrancy - Vincent.Renardias@waw.com Mar 20, 1997 | |
49 | * | |
50 | * Added devfs support - Richard Gooch <rgooch@atnf.csiro.au> 16-Jan-1998 | |
51 | * | |
52 | * Handle sparse backing files correctly - Kenn Humborg, Jun 28, 1998 | |
53 | * | |
54 | * Loadable modules and other fixes by AK, 1998 | |
55 | * | |
56 | * Maximum number of loop devices now dynamic via max_loop module parameter. | |
57 | * Russell Kroll <rkroll@exploits.org> 19990701 | |
58 | * | |
59 | * Maximum number of loop devices when compiled-in now selectable by passing | |
60 | * max_loop=<1-255> to the kernel on boot. | |
61 | * Erik I. Bols?, <eriki@himolde.no>, Oct 31, 1999 | |
62 | * | |
63 | * Completely rewrite request handling to be make_request_fn style and | |
64 | * non blocking, pushing work to a helper thread. Lots of fixes from | |
65 | * Al Viro too. | |
66 | * Jens Axboe <axboe@suse.de>, Nov 2000 | |
67 | * | |
68 | * Support up to 256 loop devices | |
69 | * Heinz Mauelshagen <mge@sistina.com>, Feb 2002 | |
70 | * | |
71 | * Support for falling back on the write file operation when the address space | |
72 | * operations prepare_write and/or commit_write are not available on the | |
73 | * backing filesystem. | |
74 | * Anton Altaparmakov, 16 Feb 2005 | |
75 | * | |
76 | * Still To Fix: | |
77 | * - Advisory locking is ignored here. | |
78 | * - Should use an own CAP_* category instead of CAP_SYS_ADMIN | |
79 | * | |
80 | */ | |
81 | ||
82 | #include <linux/module.h> | |
83 | ||
84 | #include <linux/sched.h> | |
85 | #include <linux/fs.h> | |
86 | #include <linux/file.h> | |
87 | #include <linux/stat.h> | |
88 | #include <linux/errno.h> | |
89 | #include <linux/major.h> | |
90 | #include <linux/wait.h> | |
91 | #include <linux/blkdev.h> | |
92 | #include <linux/blkpg.h> | |
93 | #include <linux/init.h> | |
94 | #include <linux/swap.h> | |
95 | #include <linux/slab.h> | |
96 | #include <linux/suspend.h> | |
97 | #include <linux/writeback.h> | |
98 | #include <linux/buffer_head.h> /* for invalidate_bdev() */ | |
99 | #include <linux/completion.h> | |
100 | #include <linux/highmem.h> | |
101 | #include <linux/gfp.h> | |
d7e09d03 | 102 | #include <linux/pagevec.h> |
e8fd99fd | 103 | #include <linux/uaccess.h> |
d7e09d03 | 104 | |
67a235f5 GKH |
105 | #include "../include/lustre_lib.h" |
106 | #include "../include/lustre_lite.h" | |
d7e09d03 PT |
107 | #include "llite_internal.h" |
108 | ||
109 | #define LLOOP_MAX_SEGMENTS LNET_MAX_IOV | |
110 | ||
111 | /* Possible states of device */ | |
112 | enum { | |
113 | LLOOP_UNBOUND, | |
114 | LLOOP_BOUND, | |
115 | LLOOP_RUNDOWN, | |
116 | }; | |
117 | ||
118 | struct lloop_device { | |
119 | int lo_number; | |
120 | int lo_refcnt; | |
121 | loff_t lo_offset; | |
122 | loff_t lo_sizelimit; | |
123 | int lo_flags; | |
d7e09d03 PT |
124 | struct file *lo_backing_file; |
125 | struct block_device *lo_device; | |
126 | unsigned lo_blocksize; | |
127 | ||
f1c571dc | 128 | gfp_t old_gfp_mask; |
d7e09d03 PT |
129 | |
130 | spinlock_t lo_lock; | |
131 | struct bio *lo_bio; | |
132 | struct bio *lo_biotail; | |
133 | int lo_state; | |
134 | struct semaphore lo_sem; | |
135 | struct mutex lo_ctl_mutex; | |
136 | atomic_t lo_pending; | |
137 | wait_queue_head_t lo_bh_wait; | |
138 | ||
139 | struct request_queue *lo_queue; | |
140 | ||
141 | const struct lu_env *lo_env; | |
142 | struct cl_io lo_io; | |
143 | struct ll_dio_pages lo_pvec; | |
144 | ||
145 | /* data to handle bio for lustre. */ | |
146 | struct lo_request_data { | |
147 | struct page *lrd_pages[LLOOP_MAX_SEGMENTS]; | |
148 | loff_t lrd_offsets[LLOOP_MAX_SEGMENTS]; | |
149 | } lo_requests[1]; | |
150 | }; | |
151 | ||
152 | /* | |
153 | * Loop flags | |
154 | */ | |
155 | enum { | |
156 | LO_FLAGS_READ_ONLY = 1, | |
157 | }; | |
158 | ||
159 | static int lloop_major; | |
160 | #define MAX_LOOP_DEFAULT 16 | |
161 | static int max_loop = MAX_LOOP_DEFAULT; | |
162 | static struct lloop_device *loop_dev; | |
163 | static struct gendisk **disks; | |
164 | static struct mutex lloop_mutex; | |
94a99b46 | 165 | static void *ll_iocontrol_magic; |
d7e09d03 PT |
166 | |
167 | static loff_t get_loop_size(struct lloop_device *lo, struct file *file) | |
168 | { | |
169 | loff_t size, offset, loopsize; | |
170 | ||
171 | /* Compute loopsize in bytes */ | |
172 | size = i_size_read(file->f_mapping->host); | |
173 | offset = lo->lo_offset; | |
174 | loopsize = size - offset; | |
175 | if (lo->lo_sizelimit > 0 && lo->lo_sizelimit < loopsize) | |
176 | loopsize = lo->lo_sizelimit; | |
177 | ||
178 | /* | |
179 | * Unfortunately, if we want to do I/O on the device, | |
180 | * the number of 512-byte sectors has to fit into a sector_t. | |
181 | */ | |
182 | return loopsize >> 9; | |
183 | } | |
184 | ||
185 | static int do_bio_lustrebacked(struct lloop_device *lo, struct bio *head) | |
186 | { | |
187 | const struct lu_env *env = lo->lo_env; | |
188 | struct cl_io *io = &lo->lo_io; | |
2a8a3597 | 189 | struct inode *inode = file_inode(lo->lo_backing_file); |
d7e09d03 PT |
190 | struct cl_object *obj = ll_i2info(inode)->lli_clob; |
191 | pgoff_t offset; | |
192 | int ret; | |
d7e09d03 | 193 | int rw; |
21aef7d9 | 194 | u32 page_count = 0; |
7988613b KO |
195 | struct bio_vec bvec; |
196 | struct bvec_iter iter; | |
d7e09d03 PT |
197 | struct bio *bio; |
198 | ssize_t bytes; | |
199 | ||
200 | struct ll_dio_pages *pvec = &lo->lo_pvec; | |
201 | struct page **pages = pvec->ldp_pages; | |
202 | loff_t *offsets = pvec->ldp_offsets; | |
203 | ||
204 | truncate_inode_pages(inode->i_mapping, 0); | |
205 | ||
206 | /* initialize the IO */ | |
207 | memset(io, 0, sizeof(*io)); | |
208 | io->ci_obj = obj; | |
209 | ret = cl_io_init(env, io, CIT_MISC, obj); | |
210 | if (ret) | |
211 | return io->ci_result; | |
212 | io->ci_lockreq = CILR_NEVER; | |
213 | ||
d7e09d03 | 214 | rw = head->bi_rw; |
6e16818b | 215 | for (bio = head; bio ; bio = bio->bi_next) { |
d7e09d03 PT |
216 | LASSERT(rw == bio->bi_rw); |
217 | ||
4f024f37 | 218 | offset = (pgoff_t)(bio->bi_iter.bi_sector << 9) + lo->lo_offset; |
7988613b KO |
219 | bio_for_each_segment(bvec, bio, iter) { |
220 | BUG_ON(bvec.bv_offset != 0); | |
09cbfeaf | 221 | BUG_ON(bvec.bv_len != PAGE_SIZE); |
d7e09d03 | 222 | |
7988613b | 223 | pages[page_count] = bvec.bv_page; |
d7e09d03 PT |
224 | offsets[page_count] = offset; |
225 | page_count++; | |
7988613b | 226 | offset += bvec.bv_len; |
d7e09d03 PT |
227 | } |
228 | LASSERT(page_count <= LLOOP_MAX_SEGMENTS); | |
229 | } | |
230 | ||
231 | ll_stats_ops_tally(ll_i2sbi(inode), | |
232 | (rw == WRITE) ? LPROC_LL_BRW_WRITE : LPROC_LL_BRW_READ, | |
233 | page_count); | |
234 | ||
09cbfeaf | 235 | pvec->ldp_size = page_count << PAGE_SHIFT; |
d7e09d03 PT |
236 | pvec->ldp_nr = page_count; |
237 | ||
238 | /* FIXME: in ll_direct_rw_pages, it has to allocate many cl_page{}s to | |
239 | * write those pages into OST. Even worse case is that more pages | |
240 | * would be asked to write out to swap space, and then finally get here | |
241 | * again. | |
242 | * Unfortunately this is NOT easy to fix. | |
243 | * Thoughts on solution: | |
244 | * 0. Define a reserved pool for cl_pages, which could be a list of | |
245 | * pre-allocated cl_pages; | |
246 | * 1. Define a new operation in cl_object_operations{}, says clo_depth, | |
247 | * which measures how many layers for this lustre object. Generally | |
248 | * speaking, the depth would be 2, one for llite, and one for lovsub. | |
249 | * However, for SNS, there will be more since we need additional page | |
250 | * to store parity; | |
251 | * 2. Reserve the # of (page_count * depth) cl_pages from the reserved | |
252 | * pool. Afterwards, the clio would allocate the pages from reserved | |
d0a0acc3 | 253 | * pool, this guarantees we needn't allocate the cl_pages from |
d7e09d03 PT |
254 | * generic cl_page slab cache. |
255 | * Of course, if there is NOT enough pages in the pool, we might | |
256 | * be asked to write less pages once, this purely depends on | |
257 | * implementation. Anyway, we should be careful to avoid deadlocking. | |
258 | */ | |
5955102c | 259 | inode_lock(inode); |
d7e09d03 | 260 | bytes = ll_direct_rw_pages(env, io, rw, inode, pvec); |
5955102c | 261 | inode_unlock(inode); |
d7e09d03 PT |
262 | cl_io_fini(env, io); |
263 | return (bytes == pvec->ldp_size) ? 0 : (int)bytes; | |
264 | } | |
265 | ||
266 | /* | |
267 | * Add bio to back of pending list | |
268 | */ | |
269 | static void loop_add_bio(struct lloop_device *lo, struct bio *bio) | |
270 | { | |
271 | unsigned long flags; | |
272 | ||
273 | spin_lock_irqsave(&lo->lo_lock, flags); | |
274 | if (lo->lo_biotail) { | |
275 | lo->lo_biotail->bi_next = bio; | |
276 | lo->lo_biotail = bio; | |
277 | } else | |
278 | lo->lo_bio = lo->lo_biotail = bio; | |
279 | spin_unlock_irqrestore(&lo->lo_lock, flags); | |
280 | ||
281 | atomic_inc(&lo->lo_pending); | |
282 | if (waitqueue_active(&lo->lo_bh_wait)) | |
283 | wake_up(&lo->lo_bh_wait); | |
284 | } | |
285 | ||
286 | /* | |
287 | * Grab first pending buffer | |
288 | */ | |
289 | static unsigned int loop_get_bio(struct lloop_device *lo, struct bio **req) | |
290 | { | |
291 | struct bio *first; | |
292 | struct bio **bio; | |
293 | unsigned int count = 0; | |
294 | unsigned int page_count = 0; | |
295 | int rw; | |
296 | ||
297 | spin_lock_irq(&lo->lo_lock); | |
298 | first = lo->lo_bio; | |
6e16818b | 299 | if (unlikely(!first)) { |
d7e09d03 PT |
300 | spin_unlock_irq(&lo->lo_lock); |
301 | return 0; | |
302 | } | |
303 | ||
304 | /* TODO: need to split the bio, too bad. */ | |
305 | LASSERT(first->bi_vcnt <= LLOOP_MAX_SEGMENTS); | |
306 | ||
307 | rw = first->bi_rw; | |
308 | bio = &lo->lo_bio; | |
309 | while (*bio && (*bio)->bi_rw == rw) { | |
72a87fca | 310 | CDEBUG(D_INFO, "bio sector %llu size %u count %u vcnt%u\n", |
4f024f37 KO |
311 | (unsigned long long)(*bio)->bi_iter.bi_sector, |
312 | (*bio)->bi_iter.bi_size, | |
d7e09d03 PT |
313 | page_count, (*bio)->bi_vcnt); |
314 | if (page_count + (*bio)->bi_vcnt > LLOOP_MAX_SEGMENTS) | |
315 | break; | |
316 | ||
d7e09d03 PT |
317 | page_count += (*bio)->bi_vcnt; |
318 | count++; | |
319 | bio = &(*bio)->bi_next; | |
320 | } | |
321 | if (*bio) { | |
d0a0acc3 | 322 | /* Some of bios can't be mergeable. */ |
d7e09d03 PT |
323 | lo->lo_bio = *bio; |
324 | *bio = NULL; | |
325 | } else { | |
326 | /* Hit the end of queue */ | |
327 | lo->lo_biotail = NULL; | |
328 | lo->lo_bio = NULL; | |
329 | } | |
330 | *req = first; | |
331 | spin_unlock_irq(&lo->lo_lock); | |
332 | return count; | |
333 | } | |
334 | ||
dece1635 | 335 | static blk_qc_t loop_make_request(struct request_queue *q, struct bio *old_bio) |
d7e09d03 PT |
336 | { |
337 | struct lloop_device *lo = q->queuedata; | |
338 | int rw = bio_rw(old_bio); | |
339 | int inactive; | |
340 | ||
54efd50b KO |
341 | blk_queue_split(q, &old_bio, q->bio_split); |
342 | ||
d7e09d03 PT |
343 | if (!lo) |
344 | goto err; | |
345 | ||
346 | CDEBUG(D_INFO, "submit bio sector %llu size %u\n", | |
4f024f37 KO |
347 | (unsigned long long)old_bio->bi_iter.bi_sector, |
348 | old_bio->bi_iter.bi_size); | |
d7e09d03 PT |
349 | |
350 | spin_lock_irq(&lo->lo_lock); | |
b6ee3824 | 351 | inactive = lo->lo_state != LLOOP_BOUND; |
d7e09d03 PT |
352 | spin_unlock_irq(&lo->lo_lock); |
353 | if (inactive) | |
354 | goto err; | |
355 | ||
356 | if (rw == WRITE) { | |
357 | if (lo->lo_flags & LO_FLAGS_READ_ONLY) | |
358 | goto err; | |
359 | } else if (rw == READA) { | |
360 | rw = READ; | |
361 | } else if (rw != READ) { | |
362 | CERROR("lloop: unknown command (%x)\n", rw); | |
363 | goto err; | |
364 | } | |
365 | loop_add_bio(lo, old_bio); | |
dece1635 | 366 | return BLK_QC_T_NONE; |
d7e09d03 | 367 | err: |
5109ea60 | 368 | bio_io_error(old_bio); |
dece1635 | 369 | return BLK_QC_T_NONE; |
d7e09d03 PT |
370 | } |
371 | ||
d7e09d03 PT |
372 | static inline void loop_handle_bio(struct lloop_device *lo, struct bio *bio) |
373 | { | |
374 | int ret; | |
50ffcb7e | 375 | |
d7e09d03 PT |
376 | ret = do_bio_lustrebacked(lo, bio); |
377 | while (bio) { | |
378 | struct bio *tmp = bio->bi_next; | |
50ffcb7e | 379 | |
d7e09d03 | 380 | bio->bi_next = NULL; |
1081230b LT |
381 | bio->bi_error = ret; |
382 | bio_endio(bio); | |
d7e09d03 PT |
383 | bio = tmp; |
384 | } | |
385 | } | |
386 | ||
387 | static inline int loop_active(struct lloop_device *lo) | |
388 | { | |
389 | return atomic_read(&lo->lo_pending) || | |
390 | (lo->lo_state == LLOOP_RUNDOWN); | |
391 | } | |
392 | ||
393 | /* | |
394 | * worker thread that handles reads/writes to file backed loop devices, | |
395 | * to avoid blocking in our make_request_fn. | |
396 | */ | |
397 | static int loop_thread(void *data) | |
398 | { | |
399 | struct lloop_device *lo = data; | |
400 | struct bio *bio; | |
401 | unsigned int count; | |
402 | unsigned long times = 0; | |
403 | unsigned long total_count = 0; | |
404 | ||
405 | struct lu_env *env; | |
406 | int refcheck; | |
407 | int ret = 0; | |
408 | ||
8698a745 | 409 | set_user_nice(current, MIN_NICE); |
d7e09d03 PT |
410 | |
411 | lo->lo_state = LLOOP_BOUND; | |
412 | ||
413 | env = cl_env_get(&refcheck); | |
34e1f2bb JL |
414 | if (IS_ERR(env)) { |
415 | ret = PTR_ERR(env); | |
416 | goto out; | |
417 | } | |
d7e09d03 PT |
418 | |
419 | lo->lo_env = env; | |
420 | memset(&lo->lo_pvec, 0, sizeof(lo->lo_pvec)); | |
421 | lo->lo_pvec.ldp_pages = lo->lo_requests[0].lrd_pages; | |
422 | lo->lo_pvec.ldp_offsets = lo->lo_requests[0].lrd_offsets; | |
423 | ||
424 | /* | |
425 | * up sem, we are running | |
426 | */ | |
427 | up(&lo->lo_sem); | |
428 | ||
429 | for (;;) { | |
430 | wait_event(lo->lo_bh_wait, loop_active(lo)); | |
431 | if (!atomic_read(&lo->lo_pending)) { | |
432 | int exiting = 0; | |
50ffcb7e | 433 | |
d7e09d03 PT |
434 | spin_lock_irq(&lo->lo_lock); |
435 | exiting = (lo->lo_state == LLOOP_RUNDOWN); | |
436 | spin_unlock_irq(&lo->lo_lock); | |
437 | if (exiting) | |
438 | break; | |
439 | } | |
440 | ||
441 | bio = NULL; | |
442 | count = loop_get_bio(lo, &bio); | |
443 | if (!count) { | |
444 | CWARN("lloop(minor: %d): missing bio\n", lo->lo_number); | |
445 | continue; | |
446 | } | |
447 | ||
448 | total_count += count; | |
449 | if (total_count < count) { /* overflow */ | |
450 | total_count = count; | |
451 | times = 1; | |
452 | } else { | |
453 | times++; | |
454 | } | |
455 | if ((times & 127) == 0) { | |
456 | CDEBUG(D_INFO, "total: %lu, count: %lu, avg: %lu\n", | |
457 | total_count, times, total_count / times); | |
458 | } | |
459 | ||
6e16818b | 460 | LASSERT(bio); |
d7e09d03 PT |
461 | LASSERT(count <= atomic_read(&lo->lo_pending)); |
462 | loop_handle_bio(lo, bio); | |
463 | atomic_sub(count, &lo->lo_pending); | |
464 | } | |
465 | cl_env_put(env, &refcheck); | |
466 | ||
467 | out: | |
468 | up(&lo->lo_sem); | |
469 | return ret; | |
470 | } | |
471 | ||
472 | static int loop_set_fd(struct lloop_device *lo, struct file *unused, | |
473 | struct block_device *bdev, struct file *file) | |
474 | { | |
475 | struct inode *inode; | |
476 | struct address_space *mapping; | |
477 | int lo_flags = 0; | |
478 | int error; | |
479 | loff_t size; | |
480 | ||
481 | if (!try_module_get(THIS_MODULE)) | |
482 | return -ENODEV; | |
483 | ||
484 | error = -EBUSY; | |
485 | if (lo->lo_state != LLOOP_UNBOUND) | |
486 | goto out; | |
487 | ||
488 | mapping = file->f_mapping; | |
489 | inode = mapping->host; | |
490 | ||
491 | error = -EINVAL; | |
492 | if (!S_ISREG(inode->i_mode) || inode->i_sb->s_magic != LL_SUPER_MAGIC) | |
493 | goto out; | |
494 | ||
495 | if (!(file->f_mode & FMODE_WRITE)) | |
496 | lo_flags |= LO_FLAGS_READ_ONLY; | |
497 | ||
498 | size = get_loop_size(lo, file); | |
499 | ||
500 | if ((loff_t)(sector_t)size != size) { | |
501 | error = -EFBIG; | |
502 | goto out; | |
503 | } | |
504 | ||
505 | /* remove all pages in cache so as dirty pages not to be existent. */ | |
506 | truncate_inode_pages(mapping, 0); | |
507 | ||
508 | set_device_ro(bdev, (lo_flags & LO_FLAGS_READ_ONLY) != 0); | |
509 | ||
09cbfeaf | 510 | lo->lo_blocksize = PAGE_SIZE; |
d7e09d03 PT |
511 | lo->lo_device = bdev; |
512 | lo->lo_flags = lo_flags; | |
513 | lo->lo_backing_file = file; | |
d7e09d03 PT |
514 | lo->lo_sizelimit = 0; |
515 | lo->old_gfp_mask = mapping_gfp_mask(mapping); | |
516 | mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); | |
517 | ||
518 | lo->lo_bio = lo->lo_biotail = NULL; | |
519 | ||
520 | /* | |
521 | * set queue make_request_fn, and add limits based on lower level | |
522 | * device | |
523 | */ | |
524 | blk_queue_make_request(lo->lo_queue, loop_make_request); | |
525 | lo->lo_queue->queuedata = lo; | |
526 | ||
527 | /* queue parameters */ | |
09cbfeaf | 528 | CLASSERT(PAGE_SIZE < (1 << (sizeof(unsigned short) * 8))); |
d7e09d03 | 529 | blk_queue_logical_block_size(lo->lo_queue, |
09cbfeaf | 530 | (unsigned short)PAGE_SIZE); |
d7e09d03 | 531 | blk_queue_max_hw_sectors(lo->lo_queue, |
09cbfeaf | 532 | LLOOP_MAX_SEGMENTS << (PAGE_SHIFT - 9)); |
d7e09d03 PT |
533 | blk_queue_max_segments(lo->lo_queue, LLOOP_MAX_SEGMENTS); |
534 | ||
535 | set_capacity(disks[lo->lo_number], size); | |
536 | bd_set_size(bdev, size << 9); | |
537 | ||
538 | set_blocksize(bdev, lo->lo_blocksize); | |
539 | ||
540 | kthread_run(loop_thread, lo, "lloop%d", lo->lo_number); | |
541 | down(&lo->lo_sem); | |
542 | return 0; | |
543 | ||
544 | out: | |
545 | /* This is safe: open() is still holding a reference. */ | |
546 | module_put(THIS_MODULE); | |
547 | return error; | |
548 | } | |
549 | ||
550 | static int loop_clr_fd(struct lloop_device *lo, struct block_device *bdev, | |
551 | int count) | |
552 | { | |
553 | struct file *filp = lo->lo_backing_file; | |
f1c571dc | 554 | gfp_t gfp = lo->old_gfp_mask; |
d7e09d03 PT |
555 | |
556 | if (lo->lo_state != LLOOP_BOUND) | |
557 | return -ENXIO; | |
558 | ||
559 | if (lo->lo_refcnt > count) /* we needed one fd for the ioctl */ | |
560 | return -EBUSY; | |
561 | ||
6e16818b | 562 | if (!filp) |
d7e09d03 PT |
563 | return -EINVAL; |
564 | ||
565 | spin_lock_irq(&lo->lo_lock); | |
566 | lo->lo_state = LLOOP_RUNDOWN; | |
567 | spin_unlock_irq(&lo->lo_lock); | |
568 | wake_up(&lo->lo_bh_wait); | |
569 | ||
570 | down(&lo->lo_sem); | |
571 | lo->lo_backing_file = NULL; | |
d7e09d03 PT |
572 | lo->lo_device = NULL; |
573 | lo->lo_offset = 0; | |
574 | lo->lo_sizelimit = 0; | |
575 | lo->lo_flags = 0; | |
db7392c2 | 576 | invalidate_bdev(bdev); |
d7e09d03 PT |
577 | set_capacity(disks[lo->lo_number], 0); |
578 | bd_set_size(bdev, 0); | |
579 | mapping_set_gfp_mask(filp->f_mapping, gfp); | |
580 | lo->lo_state = LLOOP_UNBOUND; | |
581 | fput(filp); | |
582 | /* This is safe: open() is still holding a reference. */ | |
583 | module_put(THIS_MODULE); | |
584 | return 0; | |
585 | } | |
586 | ||
587 | static int lo_open(struct block_device *bdev, fmode_t mode) | |
588 | { | |
589 | struct lloop_device *lo = bdev->bd_disk->private_data; | |
590 | ||
591 | mutex_lock(&lo->lo_ctl_mutex); | |
592 | lo->lo_refcnt++; | |
593 | mutex_unlock(&lo->lo_ctl_mutex); | |
594 | ||
595 | return 0; | |
596 | } | |
597 | ||
f1e2f53e | 598 | static void lo_release(struct gendisk *disk, fmode_t mode) |
d7e09d03 PT |
599 | { |
600 | struct lloop_device *lo = disk->private_data; | |
601 | ||
602 | mutex_lock(&lo->lo_ctl_mutex); | |
603 | --lo->lo_refcnt; | |
604 | mutex_unlock(&lo->lo_ctl_mutex); | |
d7e09d03 PT |
605 | } |
606 | ||
607 | /* lloop device node's ioctl function. */ | |
608 | static int lo_ioctl(struct block_device *bdev, fmode_t mode, | |
609 | unsigned int cmd, unsigned long arg) | |
610 | { | |
611 | struct lloop_device *lo = bdev->bd_disk->private_data; | |
612 | struct inode *inode = NULL; | |
613 | int err = 0; | |
614 | ||
615 | mutex_lock(&lloop_mutex); | |
616 | switch (cmd) { | |
617 | case LL_IOC_LLOOP_DETACH: { | |
618 | err = loop_clr_fd(lo, bdev, 2); | |
619 | if (err == 0) | |
418690b6 | 620 | blkdev_put(bdev, 0); /* grabbed in LLOOP_ATTACH */ |
d7e09d03 PT |
621 | break; |
622 | } | |
623 | ||
624 | case LL_IOC_LLOOP_INFO: { | |
625 | struct lu_fid fid; | |
626 | ||
6e16818b | 627 | if (!lo->lo_backing_file) { |
4de665c1 BG |
628 | err = -ENOENT; |
629 | break; | |
630 | } | |
6e16818b | 631 | if (!inode) |
2a8a3597 | 632 | inode = file_inode(lo->lo_backing_file); |
d7e09d03 PT |
633 | if (lo->lo_state == LLOOP_BOUND) |
634 | fid = ll_i2info(inode)->lli_fid; | |
635 | else | |
636 | fid_zero(&fid); | |
637 | ||
6f9a3bd9 | 638 | if (copy_to_user((void __user *)arg, &fid, sizeof(fid))) |
d7e09d03 PT |
639 | err = -EFAULT; |
640 | break; | |
641 | } | |
642 | ||
643 | default: | |
644 | err = -EINVAL; | |
645 | break; | |
646 | } | |
647 | mutex_unlock(&lloop_mutex); | |
648 | ||
649 | return err; | |
650 | } | |
651 | ||
652 | static struct block_device_operations lo_fops = { | |
653 | .owner = THIS_MODULE, | |
654 | .open = lo_open, | |
655 | .release = lo_release, | |
656 | .ioctl = lo_ioctl, | |
657 | }; | |
658 | ||
659 | /* dynamic iocontrol callback. | |
660 | * This callback is registered in lloop_init and will be called by | |
661 | * ll_iocontrol_call. | |
662 | * | |
663 | * This is a llite regular file ioctl function. It takes the responsibility | |
d0a0acc3 | 664 | * of attaching or detaching a file by a lloop's device number. |
d7e09d03 PT |
665 | */ |
666 | static enum llioc_iter lloop_ioctl(struct inode *unused, struct file *file, | |
667 | unsigned int cmd, unsigned long arg, | |
668 | void *magic, int *rcp) | |
669 | { | |
670 | struct lloop_device *lo = NULL; | |
671 | struct block_device *bdev = NULL; | |
672 | int err = 0; | |
673 | dev_t dev; | |
674 | ||
675 | if (magic != ll_iocontrol_magic) | |
676 | return LLIOC_CONT; | |
677 | ||
6e16818b | 678 | if (!disks) { |
34e1f2bb JL |
679 | err = -ENODEV; |
680 | goto out1; | |
681 | } | |
d7e09d03 PT |
682 | |
683 | CWARN("Enter llop_ioctl\n"); | |
684 | ||
685 | mutex_lock(&lloop_mutex); | |
686 | switch (cmd) { | |
687 | case LL_IOC_LLOOP_ATTACH: { | |
688 | struct lloop_device *lo_free = NULL; | |
689 | int i; | |
690 | ||
691 | for (i = 0; i < max_loop; i++, lo = NULL) { | |
692 | lo = &loop_dev[i]; | |
693 | if (lo->lo_state == LLOOP_UNBOUND) { | |
694 | if (!lo_free) | |
695 | lo_free = lo; | |
696 | continue; | |
697 | } | |
2a8a3597 | 698 | if (file_inode(lo->lo_backing_file) == file_inode(file)) |
d7e09d03 PT |
699 | break; |
700 | } | |
34e1f2bb JL |
701 | if (lo || !lo_free) { |
702 | err = -EBUSY; | |
703 | goto out; | |
704 | } | |
d7e09d03 PT |
705 | |
706 | lo = lo_free; | |
707 | dev = MKDEV(lloop_major, lo->lo_number); | |
708 | ||
709 | /* quit if the used pointer is writable */ | |
6f9a3bd9 | 710 | if (put_user((long)old_encode_dev(dev), (long __user *)arg)) { |
34e1f2bb JL |
711 | err = -EFAULT; |
712 | goto out; | |
713 | } | |
d7e09d03 PT |
714 | |
715 | bdev = blkdev_get_by_dev(dev, file->f_mode, NULL); | |
34e1f2bb JL |
716 | if (IS_ERR(bdev)) { |
717 | err = PTR_ERR(bdev); | |
718 | goto out; | |
719 | } | |
d7e09d03 PT |
720 | |
721 | get_file(file); | |
722 | err = loop_set_fd(lo, NULL, bdev, file); | |
723 | if (err) { | |
724 | fput(file); | |
418690b6 | 725 | blkdev_put(bdev, 0); |
d7e09d03 PT |
726 | } |
727 | ||
728 | break; | |
729 | } | |
730 | ||
731 | case LL_IOC_LLOOP_DETACH_BYDEV: { | |
732 | int minor; | |
733 | ||
734 | dev = old_decode_dev(arg); | |
34e1f2bb JL |
735 | if (MAJOR(dev) != lloop_major) { |
736 | err = -EINVAL; | |
737 | goto out; | |
738 | } | |
d7e09d03 PT |
739 | |
740 | minor = MINOR(dev); | |
34e1f2bb JL |
741 | if (minor > max_loop - 1) { |
742 | err = -EINVAL; | |
743 | goto out; | |
744 | } | |
d7e09d03 PT |
745 | |
746 | lo = &loop_dev[minor]; | |
34e1f2bb JL |
747 | if (lo->lo_state != LLOOP_BOUND) { |
748 | err = -EINVAL; | |
749 | goto out; | |
750 | } | |
d7e09d03 PT |
751 | |
752 | bdev = lo->lo_device; | |
753 | err = loop_clr_fd(lo, bdev, 1); | |
754 | if (err == 0) | |
418690b6 | 755 | blkdev_put(bdev, 0); /* grabbed in LLOOP_ATTACH */ |
d7e09d03 PT |
756 | |
757 | break; | |
758 | } | |
759 | ||
760 | default: | |
761 | err = -EINVAL; | |
762 | break; | |
763 | } | |
764 | ||
765 | out: | |
766 | mutex_unlock(&lloop_mutex); | |
767 | out1: | |
768 | if (rcp) | |
769 | *rcp = err; | |
770 | return LLIOC_STOP; | |
771 | } | |
772 | ||
773 | static int __init lloop_init(void) | |
774 | { | |
775 | int i; | |
776 | unsigned int cmdlist[] = { | |
777 | LL_IOC_LLOOP_ATTACH, | |
778 | LL_IOC_LLOOP_DETACH_BYDEV, | |
779 | }; | |
780 | ||
781 | if (max_loop < 1 || max_loop > 256) { | |
782 | max_loop = MAX_LOOP_DEFAULT; | |
2d00bd17 JP |
783 | CWARN("lloop: invalid max_loop (must be between 1 and 256), using default (%u)\n", |
784 | max_loop); | |
d7e09d03 PT |
785 | } |
786 | ||
787 | lloop_major = register_blkdev(0, "lloop"); | |
788 | if (lloop_major < 0) | |
789 | return -EIO; | |
790 | ||
791 | CDEBUG(D_CONFIG, "registered lloop major %d with %u minors\n", | |
792 | lloop_major, max_loop); | |
793 | ||
794 | ll_iocontrol_magic = ll_iocontrol_register(lloop_ioctl, 2, cmdlist); | |
6e16818b | 795 | if (!ll_iocontrol_magic) |
d7e09d03 PT |
796 | goto out_mem1; |
797 | ||
0fa3b9d3 | 798 | loop_dev = kcalloc(max_loop, sizeof(*loop_dev), GFP_KERNEL); |
d7e09d03 PT |
799 | if (!loop_dev) |
800 | goto out_mem1; | |
801 | ||
0fa3b9d3 | 802 | disks = kcalloc(max_loop, sizeof(*disks), GFP_KERNEL); |
d7e09d03 PT |
803 | if (!disks) |
804 | goto out_mem2; | |
805 | ||
806 | for (i = 0; i < max_loop; i++) { | |
807 | disks[i] = alloc_disk(1); | |
808 | if (!disks[i]) | |
809 | goto out_mem3; | |
810 | } | |
811 | ||
812 | mutex_init(&lloop_mutex); | |
813 | ||
814 | for (i = 0; i < max_loop; i++) { | |
815 | struct lloop_device *lo = &loop_dev[i]; | |
816 | struct gendisk *disk = disks[i]; | |
817 | ||
818 | lo->lo_queue = blk_alloc_queue(GFP_KERNEL); | |
819 | if (!lo->lo_queue) | |
820 | goto out_mem4; | |
821 | ||
822 | mutex_init(&lo->lo_ctl_mutex); | |
823 | sema_init(&lo->lo_sem, 0); | |
824 | init_waitqueue_head(&lo->lo_bh_wait); | |
825 | lo->lo_number = i; | |
826 | spin_lock_init(&lo->lo_lock); | |
827 | disk->major = lloop_major; | |
828 | disk->first_minor = i; | |
829 | disk->fops = &lo_fops; | |
830 | sprintf(disk->disk_name, "lloop%d", i); | |
831 | disk->private_data = lo; | |
832 | disk->queue = lo->lo_queue; | |
833 | } | |
834 | ||
835 | /* We cannot fail after we call this, so another loop!*/ | |
836 | for (i = 0; i < max_loop; i++) | |
837 | add_disk(disks[i]); | |
838 | return 0; | |
839 | ||
840 | out_mem4: | |
841 | while (i--) | |
842 | blk_cleanup_queue(loop_dev[i].lo_queue); | |
843 | i = max_loop; | |
844 | out_mem3: | |
845 | while (i--) | |
846 | put_disk(disks[i]); | |
97903a26 | 847 | kfree(disks); |
d7e09d03 | 848 | out_mem2: |
97903a26 | 849 | kfree(loop_dev); |
d7e09d03 PT |
850 | out_mem1: |
851 | unregister_blkdev(lloop_major, "lloop"); | |
852 | ll_iocontrol_unregister(ll_iocontrol_magic); | |
853 | CERROR("lloop: ran out of memory\n"); | |
854 | return -ENOMEM; | |
855 | } | |
856 | ||
857 | static void lloop_exit(void) | |
858 | { | |
859 | int i; | |
860 | ||
861 | ll_iocontrol_unregister(ll_iocontrol_magic); | |
862 | for (i = 0; i < max_loop; i++) { | |
863 | del_gendisk(disks[i]); | |
864 | blk_cleanup_queue(loop_dev[i].lo_queue); | |
865 | put_disk(disks[i]); | |
866 | } | |
49126e47 XZ |
867 | |
868 | unregister_blkdev(lloop_major, "lloop"); | |
d7e09d03 | 869 | |
97903a26 JL |
870 | kfree(disks); |
871 | kfree(loop_dev); | |
d7e09d03 PT |
872 | } |
873 | ||
8cc7b4b9 PT |
874 | module_param(max_loop, int, 0444); |
875 | MODULE_PARM_DESC(max_loop, "maximum of lloop_device"); | |
a0455471 | 876 | MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>"); |
d7e09d03 | 877 | MODULE_DESCRIPTION("Lustre virtual block device"); |
5b0e50b9 | 878 | MODULE_VERSION(LUSTRE_VERSION_STRING); |
d7e09d03 | 879 | MODULE_LICENSE("GPL"); |
e0f94113 AD |
880 | |
881 | module_init(lloop_init); | |
882 | module_exit(lloop_exit); |