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