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 PT |
102 | #include <linux/pagevec.h> |
103 | ||
104 | #include <asm/uaccess.h> | |
105 | ||
106 | #include <lustre_lib.h> | |
107 | #include <lustre_lite.h> | |
108 | #include "llite_internal.h" | |
109 | ||
110 | #define LLOOP_MAX_SEGMENTS LNET_MAX_IOV | |
111 | ||
112 | /* Possible states of device */ | |
113 | enum { | |
114 | LLOOP_UNBOUND, | |
115 | LLOOP_BOUND, | |
116 | LLOOP_RUNDOWN, | |
117 | }; | |
118 | ||
119 | struct lloop_device { | |
120 | int lo_number; | |
121 | int lo_refcnt; | |
122 | loff_t lo_offset; | |
123 | loff_t lo_sizelimit; | |
124 | int lo_flags; | |
125 | int (*ioctl)(struct lloop_device *, int cmd, | |
126 | unsigned long arg); | |
127 | ||
128 | struct file *lo_backing_file; | |
129 | struct block_device *lo_device; | |
130 | unsigned lo_blocksize; | |
131 | ||
132 | int old_gfp_mask; | |
133 | ||
134 | spinlock_t lo_lock; | |
135 | struct bio *lo_bio; | |
136 | struct bio *lo_biotail; | |
137 | int lo_state; | |
138 | struct semaphore lo_sem; | |
139 | struct mutex lo_ctl_mutex; | |
140 | atomic_t lo_pending; | |
141 | wait_queue_head_t lo_bh_wait; | |
142 | ||
143 | struct request_queue *lo_queue; | |
144 | ||
145 | const struct lu_env *lo_env; | |
146 | struct cl_io lo_io; | |
147 | struct ll_dio_pages lo_pvec; | |
148 | ||
149 | /* data to handle bio for lustre. */ | |
150 | struct lo_request_data { | |
151 | struct page *lrd_pages[LLOOP_MAX_SEGMENTS]; | |
152 | loff_t lrd_offsets[LLOOP_MAX_SEGMENTS]; | |
153 | } lo_requests[1]; | |
154 | }; | |
155 | ||
156 | /* | |
157 | * Loop flags | |
158 | */ | |
159 | enum { | |
160 | LO_FLAGS_READ_ONLY = 1, | |
161 | }; | |
162 | ||
163 | static int lloop_major; | |
164 | #define MAX_LOOP_DEFAULT 16 | |
165 | static int max_loop = MAX_LOOP_DEFAULT; | |
166 | static struct lloop_device *loop_dev; | |
167 | static struct gendisk **disks; | |
168 | static struct mutex lloop_mutex; | |
169 | static void *ll_iocontrol_magic = NULL; | |
170 | ||
171 | static loff_t get_loop_size(struct lloop_device *lo, struct file *file) | |
172 | { | |
173 | loff_t size, offset, loopsize; | |
174 | ||
175 | /* Compute loopsize in bytes */ | |
176 | size = i_size_read(file->f_mapping->host); | |
177 | offset = lo->lo_offset; | |
178 | loopsize = size - offset; | |
179 | if (lo->lo_sizelimit > 0 && lo->lo_sizelimit < loopsize) | |
180 | loopsize = lo->lo_sizelimit; | |
181 | ||
182 | /* | |
183 | * Unfortunately, if we want to do I/O on the device, | |
184 | * the number of 512-byte sectors has to fit into a sector_t. | |
185 | */ | |
186 | return loopsize >> 9; | |
187 | } | |
188 | ||
189 | static int do_bio_lustrebacked(struct lloop_device *lo, struct bio *head) | |
190 | { | |
191 | const struct lu_env *env = lo->lo_env; | |
192 | struct cl_io *io = &lo->lo_io; | |
193 | struct inode *inode = lo->lo_backing_file->f_dentry->d_inode; | |
194 | struct cl_object *obj = ll_i2info(inode)->lli_clob; | |
195 | pgoff_t offset; | |
196 | int ret; | |
197 | int i; | |
198 | int rw; | |
199 | obd_count page_count = 0; | |
200 | struct bio_vec *bvec; | |
201 | struct bio *bio; | |
202 | ssize_t bytes; | |
203 | ||
204 | struct ll_dio_pages *pvec = &lo->lo_pvec; | |
205 | struct page **pages = pvec->ldp_pages; | |
206 | loff_t *offsets = pvec->ldp_offsets; | |
207 | ||
208 | truncate_inode_pages(inode->i_mapping, 0); | |
209 | ||
210 | /* initialize the IO */ | |
211 | memset(io, 0, sizeof(*io)); | |
212 | io->ci_obj = obj; | |
213 | ret = cl_io_init(env, io, CIT_MISC, obj); | |
214 | if (ret) | |
215 | return io->ci_result; | |
216 | io->ci_lockreq = CILR_NEVER; | |
217 | ||
218 | LASSERT(head != NULL); | |
219 | rw = head->bi_rw; | |
220 | for (bio = head; bio != NULL; bio = bio->bi_next) { | |
221 | LASSERT(rw == bio->bi_rw); | |
222 | ||
223 | offset = (pgoff_t)(bio->bi_sector << 9) + lo->lo_offset; | |
224 | bio_for_each_segment(bvec, bio, i) { | |
225 | BUG_ON(bvec->bv_offset != 0); | |
226 | BUG_ON(bvec->bv_len != PAGE_CACHE_SIZE); | |
227 | ||
228 | pages[page_count] = bvec->bv_page; | |
229 | offsets[page_count] = offset; | |
230 | page_count++; | |
231 | offset += bvec->bv_len; | |
232 | } | |
233 | LASSERT(page_count <= LLOOP_MAX_SEGMENTS); | |
234 | } | |
235 | ||
236 | ll_stats_ops_tally(ll_i2sbi(inode), | |
237 | (rw == WRITE) ? LPROC_LL_BRW_WRITE : LPROC_LL_BRW_READ, | |
238 | page_count); | |
239 | ||
240 | pvec->ldp_size = page_count << PAGE_CACHE_SHIFT; | |
241 | pvec->ldp_nr = page_count; | |
242 | ||
243 | /* FIXME: in ll_direct_rw_pages, it has to allocate many cl_page{}s to | |
244 | * write those pages into OST. Even worse case is that more pages | |
245 | * would be asked to write out to swap space, and then finally get here | |
246 | * again. | |
247 | * Unfortunately this is NOT easy to fix. | |
248 | * Thoughts on solution: | |
249 | * 0. Define a reserved pool for cl_pages, which could be a list of | |
250 | * pre-allocated cl_pages; | |
251 | * 1. Define a new operation in cl_object_operations{}, says clo_depth, | |
252 | * which measures how many layers for this lustre object. Generally | |
253 | * speaking, the depth would be 2, one for llite, and one for lovsub. | |
254 | * However, for SNS, there will be more since we need additional page | |
255 | * to store parity; | |
256 | * 2. Reserve the # of (page_count * depth) cl_pages from the reserved | |
257 | * pool. Afterwards, the clio would allocate the pages from reserved | |
258 | * pool, this guarantees we neeedn't allocate the cl_pages from | |
259 | * generic cl_page slab cache. | |
260 | * Of course, if there is NOT enough pages in the pool, we might | |
261 | * be asked to write less pages once, this purely depends on | |
262 | * implementation. Anyway, we should be careful to avoid deadlocking. | |
263 | */ | |
264 | mutex_lock(&inode->i_mutex); | |
265 | bytes = ll_direct_rw_pages(env, io, rw, inode, pvec); | |
266 | mutex_unlock(&inode->i_mutex); | |
267 | cl_io_fini(env, io); | |
268 | return (bytes == pvec->ldp_size) ? 0 : (int)bytes; | |
269 | } | |
270 | ||
271 | /* | |
272 | * Add bio to back of pending list | |
273 | */ | |
274 | static void loop_add_bio(struct lloop_device *lo, struct bio *bio) | |
275 | { | |
276 | unsigned long flags; | |
277 | ||
278 | spin_lock_irqsave(&lo->lo_lock, flags); | |
279 | if (lo->lo_biotail) { | |
280 | lo->lo_biotail->bi_next = bio; | |
281 | lo->lo_biotail = bio; | |
282 | } else | |
283 | lo->lo_bio = lo->lo_biotail = bio; | |
284 | spin_unlock_irqrestore(&lo->lo_lock, flags); | |
285 | ||
286 | atomic_inc(&lo->lo_pending); | |
287 | if (waitqueue_active(&lo->lo_bh_wait)) | |
288 | wake_up(&lo->lo_bh_wait); | |
289 | } | |
290 | ||
291 | /* | |
292 | * Grab first pending buffer | |
293 | */ | |
294 | static unsigned int loop_get_bio(struct lloop_device *lo, struct bio **req) | |
295 | { | |
296 | struct bio *first; | |
297 | struct bio **bio; | |
298 | unsigned int count = 0; | |
299 | unsigned int page_count = 0; | |
300 | int rw; | |
301 | ||
302 | spin_lock_irq(&lo->lo_lock); | |
303 | first = lo->lo_bio; | |
304 | if (unlikely(first == NULL)) { | |
305 | spin_unlock_irq(&lo->lo_lock); | |
306 | return 0; | |
307 | } | |
308 | ||
309 | /* TODO: need to split the bio, too bad. */ | |
310 | LASSERT(first->bi_vcnt <= LLOOP_MAX_SEGMENTS); | |
311 | ||
312 | rw = first->bi_rw; | |
313 | bio = &lo->lo_bio; | |
314 | while (*bio && (*bio)->bi_rw == rw) { | |
315 | CDEBUG(D_INFO, "bio sector %llu size %u count %u vcnt%u \n", | |
316 | (unsigned long long)(*bio)->bi_sector, (*bio)->bi_size, | |
317 | page_count, (*bio)->bi_vcnt); | |
318 | if (page_count + (*bio)->bi_vcnt > LLOOP_MAX_SEGMENTS) | |
319 | break; | |
320 | ||
321 | ||
322 | page_count += (*bio)->bi_vcnt; | |
323 | count++; | |
324 | bio = &(*bio)->bi_next; | |
325 | } | |
326 | if (*bio) { | |
327 | /* Some of bios can't be mergable. */ | |
328 | lo->lo_bio = *bio; | |
329 | *bio = NULL; | |
330 | } else { | |
331 | /* Hit the end of queue */ | |
332 | lo->lo_biotail = NULL; | |
333 | lo->lo_bio = NULL; | |
334 | } | |
335 | *req = first; | |
336 | spin_unlock_irq(&lo->lo_lock); | |
337 | return count; | |
338 | } | |
339 | ||
3ec65cb3 | 340 | static void loop_make_request(struct request_queue *q, struct bio *old_bio) |
d7e09d03 PT |
341 | { |
342 | struct lloop_device *lo = q->queuedata; | |
343 | int rw = bio_rw(old_bio); | |
344 | int inactive; | |
345 | ||
346 | if (!lo) | |
347 | goto err; | |
348 | ||
349 | CDEBUG(D_INFO, "submit bio sector %llu size %u\n", | |
350 | (unsigned long long)old_bio->bi_sector, old_bio->bi_size); | |
351 | ||
352 | spin_lock_irq(&lo->lo_lock); | |
353 | inactive = (lo->lo_state != LLOOP_BOUND); | |
354 | spin_unlock_irq(&lo->lo_lock); | |
355 | if (inactive) | |
356 | goto err; | |
357 | ||
358 | if (rw == WRITE) { | |
359 | if (lo->lo_flags & LO_FLAGS_READ_ONLY) | |
360 | goto err; | |
361 | } else if (rw == READA) { | |
362 | rw = READ; | |
363 | } else if (rw != READ) { | |
364 | CERROR("lloop: unknown command (%x)\n", rw); | |
365 | goto err; | |
366 | } | |
367 | loop_add_bio(lo, old_bio); | |
368 | LL_MRF_RETURN(0); | |
369 | err: | |
370 | cfs_bio_io_error(old_bio, old_bio->bi_size); | |
371 | LL_MRF_RETURN(0); | |
372 | } | |
373 | ||
374 | ||
375 | static inline void loop_handle_bio(struct lloop_device *lo, struct bio *bio) | |
376 | { | |
377 | int ret; | |
378 | ret = do_bio_lustrebacked(lo, bio); | |
379 | while (bio) { | |
380 | struct bio *tmp = bio->bi_next; | |
381 | bio->bi_next = NULL; | |
382 | cfs_bio_endio(bio, bio->bi_size, ret); | |
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 | ||
409 | set_user_nice(current, -20); | |
410 | ||
411 | lo->lo_state = LLOOP_BOUND; | |
412 | ||
413 | env = cl_env_get(&refcheck); | |
414 | if (IS_ERR(env)) | |
415 | GOTO(out, ret = PTR_ERR(env)); | |
416 | ||
417 | lo->lo_env = env; | |
418 | memset(&lo->lo_pvec, 0, sizeof(lo->lo_pvec)); | |
419 | lo->lo_pvec.ldp_pages = lo->lo_requests[0].lrd_pages; | |
420 | lo->lo_pvec.ldp_offsets = lo->lo_requests[0].lrd_offsets; | |
421 | ||
422 | /* | |
423 | * up sem, we are running | |
424 | */ | |
425 | up(&lo->lo_sem); | |
426 | ||
427 | for (;;) { | |
428 | wait_event(lo->lo_bh_wait, loop_active(lo)); | |
429 | if (!atomic_read(&lo->lo_pending)) { | |
430 | int exiting = 0; | |
431 | spin_lock_irq(&lo->lo_lock); | |
432 | exiting = (lo->lo_state == LLOOP_RUNDOWN); | |
433 | spin_unlock_irq(&lo->lo_lock); | |
434 | if (exiting) | |
435 | break; | |
436 | } | |
437 | ||
438 | bio = NULL; | |
439 | count = loop_get_bio(lo, &bio); | |
440 | if (!count) { | |
441 | CWARN("lloop(minor: %d): missing bio\n", lo->lo_number); | |
442 | continue; | |
443 | } | |
444 | ||
445 | total_count += count; | |
446 | if (total_count < count) { /* overflow */ | |
447 | total_count = count; | |
448 | times = 1; | |
449 | } else { | |
450 | times++; | |
451 | } | |
452 | if ((times & 127) == 0) { | |
453 | CDEBUG(D_INFO, "total: %lu, count: %lu, avg: %lu\n", | |
454 | total_count, times, total_count / times); | |
455 | } | |
456 | ||
457 | LASSERT(bio != NULL); | |
458 | LASSERT(count <= atomic_read(&lo->lo_pending)); | |
459 | loop_handle_bio(lo, bio); | |
460 | atomic_sub(count, &lo->lo_pending); | |
461 | } | |
462 | cl_env_put(env, &refcheck); | |
463 | ||
464 | out: | |
465 | up(&lo->lo_sem); | |
466 | return ret; | |
467 | } | |
468 | ||
469 | static int loop_set_fd(struct lloop_device *lo, struct file *unused, | |
470 | struct block_device *bdev, struct file *file) | |
471 | { | |
472 | struct inode *inode; | |
473 | struct address_space *mapping; | |
474 | int lo_flags = 0; | |
475 | int error; | |
476 | loff_t size; | |
477 | ||
478 | if (!try_module_get(THIS_MODULE)) | |
479 | return -ENODEV; | |
480 | ||
481 | error = -EBUSY; | |
482 | if (lo->lo_state != LLOOP_UNBOUND) | |
483 | goto out; | |
484 | ||
485 | mapping = file->f_mapping; | |
486 | inode = mapping->host; | |
487 | ||
488 | error = -EINVAL; | |
489 | if (!S_ISREG(inode->i_mode) || inode->i_sb->s_magic != LL_SUPER_MAGIC) | |
490 | goto out; | |
491 | ||
492 | if (!(file->f_mode & FMODE_WRITE)) | |
493 | lo_flags |= LO_FLAGS_READ_ONLY; | |
494 | ||
495 | size = get_loop_size(lo, file); | |
496 | ||
497 | if ((loff_t)(sector_t)size != size) { | |
498 | error = -EFBIG; | |
499 | goto out; | |
500 | } | |
501 | ||
502 | /* remove all pages in cache so as dirty pages not to be existent. */ | |
503 | truncate_inode_pages(mapping, 0); | |
504 | ||
505 | set_device_ro(bdev, (lo_flags & LO_FLAGS_READ_ONLY) != 0); | |
506 | ||
507 | lo->lo_blocksize = PAGE_CACHE_SIZE; | |
508 | lo->lo_device = bdev; | |
509 | lo->lo_flags = lo_flags; | |
510 | lo->lo_backing_file = file; | |
511 | lo->ioctl = NULL; | |
512 | lo->lo_sizelimit = 0; | |
513 | lo->old_gfp_mask = mapping_gfp_mask(mapping); | |
514 | mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); | |
515 | ||
516 | lo->lo_bio = lo->lo_biotail = NULL; | |
517 | ||
518 | /* | |
519 | * set queue make_request_fn, and add limits based on lower level | |
520 | * device | |
521 | */ | |
522 | blk_queue_make_request(lo->lo_queue, loop_make_request); | |
523 | lo->lo_queue->queuedata = lo; | |
524 | ||
525 | /* queue parameters */ | |
526 | CLASSERT(PAGE_CACHE_SIZE < (1 << (sizeof(unsigned short) * 8))); | |
527 | blk_queue_logical_block_size(lo->lo_queue, | |
528 | (unsigned short)PAGE_CACHE_SIZE); | |
529 | blk_queue_max_hw_sectors(lo->lo_queue, | |
530 | LLOOP_MAX_SEGMENTS << (PAGE_CACHE_SHIFT - 9)); | |
531 | blk_queue_max_segments(lo->lo_queue, LLOOP_MAX_SEGMENTS); | |
532 | ||
533 | set_capacity(disks[lo->lo_number], size); | |
534 | bd_set_size(bdev, size << 9); | |
535 | ||
536 | set_blocksize(bdev, lo->lo_blocksize); | |
537 | ||
538 | kthread_run(loop_thread, lo, "lloop%d", lo->lo_number); | |
539 | down(&lo->lo_sem); | |
540 | return 0; | |
541 | ||
542 | out: | |
543 | /* This is safe: open() is still holding a reference. */ | |
544 | module_put(THIS_MODULE); | |
545 | return error; | |
546 | } | |
547 | ||
548 | static int loop_clr_fd(struct lloop_device *lo, struct block_device *bdev, | |
549 | int count) | |
550 | { | |
551 | struct file *filp = lo->lo_backing_file; | |
552 | int gfp = lo->old_gfp_mask; | |
553 | ||
554 | if (lo->lo_state != LLOOP_BOUND) | |
555 | return -ENXIO; | |
556 | ||
557 | if (lo->lo_refcnt > count) /* we needed one fd for the ioctl */ | |
558 | return -EBUSY; | |
559 | ||
560 | if (filp == NULL) | |
561 | return -EINVAL; | |
562 | ||
563 | spin_lock_irq(&lo->lo_lock); | |
564 | lo->lo_state = LLOOP_RUNDOWN; | |
565 | spin_unlock_irq(&lo->lo_lock); | |
566 | wake_up(&lo->lo_bh_wait); | |
567 | ||
568 | down(&lo->lo_sem); | |
569 | lo->lo_backing_file = NULL; | |
570 | lo->ioctl = NULL; | |
571 | lo->lo_device = NULL; | |
572 | lo->lo_offset = 0; | |
573 | lo->lo_sizelimit = 0; | |
574 | lo->lo_flags = 0; | |
db7392c2 | 575 | invalidate_bdev(bdev); |
d7e09d03 PT |
576 | set_capacity(disks[lo->lo_number], 0); |
577 | bd_set_size(bdev, 0); | |
578 | mapping_set_gfp_mask(filp->f_mapping, gfp); | |
579 | lo->lo_state = LLOOP_UNBOUND; | |
580 | fput(filp); | |
581 | /* This is safe: open() is still holding a reference. */ | |
582 | module_put(THIS_MODULE); | |
583 | return 0; | |
584 | } | |
585 | ||
586 | static int lo_open(struct block_device *bdev, fmode_t mode) | |
587 | { | |
588 | struct lloop_device *lo = bdev->bd_disk->private_data; | |
589 | ||
590 | mutex_lock(&lo->lo_ctl_mutex); | |
591 | lo->lo_refcnt++; | |
592 | mutex_unlock(&lo->lo_ctl_mutex); | |
593 | ||
594 | return 0; | |
595 | } | |
596 | ||
f1e2f53e | 597 | static void lo_release(struct gendisk *disk, fmode_t mode) |
d7e09d03 PT |
598 | { |
599 | struct lloop_device *lo = disk->private_data; | |
600 | ||
601 | mutex_lock(&lo->lo_ctl_mutex); | |
602 | --lo->lo_refcnt; | |
603 | mutex_unlock(&lo->lo_ctl_mutex); | |
d7e09d03 PT |
604 | } |
605 | ||
606 | /* lloop device node's ioctl function. */ | |
607 | static int lo_ioctl(struct block_device *bdev, fmode_t mode, | |
608 | unsigned int cmd, unsigned long arg) | |
609 | { | |
610 | struct lloop_device *lo = bdev->bd_disk->private_data; | |
611 | struct inode *inode = NULL; | |
612 | int err = 0; | |
613 | ||
614 | mutex_lock(&lloop_mutex); | |
615 | switch (cmd) { | |
616 | case LL_IOC_LLOOP_DETACH: { | |
617 | err = loop_clr_fd(lo, bdev, 2); | |
618 | if (err == 0) | |
418690b6 | 619 | blkdev_put(bdev, 0); /* grabbed in LLOOP_ATTACH */ |
d7e09d03 PT |
620 | break; |
621 | } | |
622 | ||
623 | case LL_IOC_LLOOP_INFO: { | |
624 | struct lu_fid fid; | |
625 | ||
626 | LASSERT(lo->lo_backing_file != NULL); | |
627 | if (inode == NULL) | |
628 | inode = lo->lo_backing_file->f_dentry->d_inode; | |
629 | if (lo->lo_state == LLOOP_BOUND) | |
630 | fid = ll_i2info(inode)->lli_fid; | |
631 | else | |
632 | fid_zero(&fid); | |
633 | ||
634 | if (copy_to_user((struct lu_fid *)arg, &fid, sizeof(fid))) | |
635 | err = -EFAULT; | |
636 | break; | |
637 | } | |
638 | ||
639 | default: | |
640 | err = -EINVAL; | |
641 | break; | |
642 | } | |
643 | mutex_unlock(&lloop_mutex); | |
644 | ||
645 | return err; | |
646 | } | |
647 | ||
648 | static struct block_device_operations lo_fops = { | |
649 | .owner = THIS_MODULE, | |
650 | .open = lo_open, | |
651 | .release = lo_release, | |
652 | .ioctl = lo_ioctl, | |
653 | }; | |
654 | ||
655 | /* dynamic iocontrol callback. | |
656 | * This callback is registered in lloop_init and will be called by | |
657 | * ll_iocontrol_call. | |
658 | * | |
659 | * This is a llite regular file ioctl function. It takes the responsibility | |
660 | * of attaching or detaching a file by a lloop's device numner. | |
661 | */ | |
662 | static enum llioc_iter lloop_ioctl(struct inode *unused, struct file *file, | |
663 | unsigned int cmd, unsigned long arg, | |
664 | void *magic, int *rcp) | |
665 | { | |
666 | struct lloop_device *lo = NULL; | |
667 | struct block_device *bdev = NULL; | |
668 | int err = 0; | |
669 | dev_t dev; | |
670 | ||
671 | if (magic != ll_iocontrol_magic) | |
672 | return LLIOC_CONT; | |
673 | ||
674 | if (disks == NULL) | |
675 | GOTO(out1, err = -ENODEV); | |
676 | ||
677 | CWARN("Enter llop_ioctl\n"); | |
678 | ||
679 | mutex_lock(&lloop_mutex); | |
680 | switch (cmd) { | |
681 | case LL_IOC_LLOOP_ATTACH: { | |
682 | struct lloop_device *lo_free = NULL; | |
683 | int i; | |
684 | ||
685 | for (i = 0; i < max_loop; i++, lo = NULL) { | |
686 | lo = &loop_dev[i]; | |
687 | if (lo->lo_state == LLOOP_UNBOUND) { | |
688 | if (!lo_free) | |
689 | lo_free = lo; | |
690 | continue; | |
691 | } | |
692 | if (lo->lo_backing_file->f_dentry->d_inode == | |
693 | file->f_dentry->d_inode) | |
694 | break; | |
695 | } | |
696 | if (lo || !lo_free) | |
697 | GOTO(out, err = -EBUSY); | |
698 | ||
699 | lo = lo_free; | |
700 | dev = MKDEV(lloop_major, lo->lo_number); | |
701 | ||
702 | /* quit if the used pointer is writable */ | |
703 | if (put_user((long)old_encode_dev(dev), (long*)arg)) | |
704 | GOTO(out, err = -EFAULT); | |
705 | ||
706 | bdev = blkdev_get_by_dev(dev, file->f_mode, NULL); | |
707 | if (IS_ERR(bdev)) | |
708 | GOTO(out, err = PTR_ERR(bdev)); | |
709 | ||
710 | get_file(file); | |
711 | err = loop_set_fd(lo, NULL, bdev, file); | |
712 | if (err) { | |
713 | fput(file); | |
418690b6 | 714 | blkdev_put(bdev, 0); |
d7e09d03 PT |
715 | } |
716 | ||
717 | break; | |
718 | } | |
719 | ||
720 | case LL_IOC_LLOOP_DETACH_BYDEV: { | |
721 | int minor; | |
722 | ||
723 | dev = old_decode_dev(arg); | |
724 | if (MAJOR(dev) != lloop_major) | |
725 | GOTO(out, err = -EINVAL); | |
726 | ||
727 | minor = MINOR(dev); | |
728 | if (minor > max_loop - 1) | |
729 | GOTO(out, err = -EINVAL); | |
730 | ||
731 | lo = &loop_dev[minor]; | |
732 | if (lo->lo_state != LLOOP_BOUND) | |
733 | GOTO(out, err = -EINVAL); | |
734 | ||
735 | bdev = lo->lo_device; | |
736 | err = loop_clr_fd(lo, bdev, 1); | |
737 | if (err == 0) | |
418690b6 | 738 | blkdev_put(bdev, 0); /* grabbed in LLOOP_ATTACH */ |
d7e09d03 PT |
739 | |
740 | break; | |
741 | } | |
742 | ||
743 | default: | |
744 | err = -EINVAL; | |
745 | break; | |
746 | } | |
747 | ||
748 | out: | |
749 | mutex_unlock(&lloop_mutex); | |
750 | out1: | |
751 | if (rcp) | |
752 | *rcp = err; | |
753 | return LLIOC_STOP; | |
754 | } | |
755 | ||
756 | static int __init lloop_init(void) | |
757 | { | |
758 | int i; | |
759 | unsigned int cmdlist[] = { | |
760 | LL_IOC_LLOOP_ATTACH, | |
761 | LL_IOC_LLOOP_DETACH_BYDEV, | |
762 | }; | |
763 | ||
764 | if (max_loop < 1 || max_loop > 256) { | |
765 | max_loop = MAX_LOOP_DEFAULT; | |
766 | CWARN("lloop: invalid max_loop (must be between" | |
767 | " 1 and 256), using default (%u)\n", max_loop); | |
768 | } | |
769 | ||
770 | lloop_major = register_blkdev(0, "lloop"); | |
771 | if (lloop_major < 0) | |
772 | return -EIO; | |
773 | ||
774 | CDEBUG(D_CONFIG, "registered lloop major %d with %u minors\n", | |
775 | lloop_major, max_loop); | |
776 | ||
777 | ll_iocontrol_magic = ll_iocontrol_register(lloop_ioctl, 2, cmdlist); | |
778 | if (ll_iocontrol_magic == NULL) | |
779 | goto out_mem1; | |
780 | ||
781 | OBD_ALLOC_WAIT(loop_dev, max_loop * sizeof(*loop_dev)); | |
782 | if (!loop_dev) | |
783 | goto out_mem1; | |
784 | ||
785 | OBD_ALLOC_WAIT(disks, max_loop * sizeof(*disks)); | |
786 | if (!disks) | |
787 | goto out_mem2; | |
788 | ||
789 | for (i = 0; i < max_loop; i++) { | |
790 | disks[i] = alloc_disk(1); | |
791 | if (!disks[i]) | |
792 | goto out_mem3; | |
793 | } | |
794 | ||
795 | mutex_init(&lloop_mutex); | |
796 | ||
797 | for (i = 0; i < max_loop; i++) { | |
798 | struct lloop_device *lo = &loop_dev[i]; | |
799 | struct gendisk *disk = disks[i]; | |
800 | ||
801 | lo->lo_queue = blk_alloc_queue(GFP_KERNEL); | |
802 | if (!lo->lo_queue) | |
803 | goto out_mem4; | |
804 | ||
805 | mutex_init(&lo->lo_ctl_mutex); | |
806 | sema_init(&lo->lo_sem, 0); | |
807 | init_waitqueue_head(&lo->lo_bh_wait); | |
808 | lo->lo_number = i; | |
809 | spin_lock_init(&lo->lo_lock); | |
810 | disk->major = lloop_major; | |
811 | disk->first_minor = i; | |
812 | disk->fops = &lo_fops; | |
813 | sprintf(disk->disk_name, "lloop%d", i); | |
814 | disk->private_data = lo; | |
815 | disk->queue = lo->lo_queue; | |
816 | } | |
817 | ||
818 | /* We cannot fail after we call this, so another loop!*/ | |
819 | for (i = 0; i < max_loop; i++) | |
820 | add_disk(disks[i]); | |
821 | return 0; | |
822 | ||
823 | out_mem4: | |
824 | while (i--) | |
825 | blk_cleanup_queue(loop_dev[i].lo_queue); | |
826 | i = max_loop; | |
827 | out_mem3: | |
828 | while (i--) | |
829 | put_disk(disks[i]); | |
830 | OBD_FREE(disks, max_loop * sizeof(*disks)); | |
831 | out_mem2: | |
832 | OBD_FREE(loop_dev, max_loop * sizeof(*loop_dev)); | |
833 | out_mem1: | |
834 | unregister_blkdev(lloop_major, "lloop"); | |
835 | ll_iocontrol_unregister(ll_iocontrol_magic); | |
836 | CERROR("lloop: ran out of memory\n"); | |
837 | return -ENOMEM; | |
838 | } | |
839 | ||
840 | static void lloop_exit(void) | |
841 | { | |
842 | int i; | |
843 | ||
844 | ll_iocontrol_unregister(ll_iocontrol_magic); | |
845 | for (i = 0; i < max_loop; i++) { | |
846 | del_gendisk(disks[i]); | |
847 | blk_cleanup_queue(loop_dev[i].lo_queue); | |
848 | put_disk(disks[i]); | |
849 | } | |
49126e47 XZ |
850 | |
851 | unregister_blkdev(lloop_major, "lloop"); | |
d7e09d03 PT |
852 | |
853 | OBD_FREE(disks, max_loop * sizeof(*disks)); | |
854 | OBD_FREE(loop_dev, max_loop * sizeof(*loop_dev)); | |
855 | } | |
856 | ||
857 | module_init(lloop_init); | |
858 | module_exit(lloop_exit); | |
859 | ||
860 | CFS_MODULE_PARM(max_loop, "i", int, 0444, "maximum of lloop_device"); | |
861 | MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>"); | |
862 | MODULE_DESCRIPTION("Lustre virtual block device"); | |
863 | MODULE_LICENSE("GPL"); |