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