GNU Linux-libre 5.15.54-gnu
[releases.git] / drivers / block / loop.c
1 /*
2  *  linux/drivers/block/loop.c
3  *
4  *  Written by Theodore Ts'o, 3/29/93
5  *
6  * Copyright 1993 by Theodore Ts'o.  Redistribution of this file is
7  * permitted under the GNU General Public License.
8  *
9  * DES encryption plus some minor changes by Werner Almesberger, 30-MAY-1993
10  * more DES encryption plus IDEA encryption by Nicholas J. Leon, June 20, 1996
11  *
12  * Modularized and updated for 1.1.16 kernel - Mitch Dsouza 28th May 1994
13  * Adapted for 1.3.59 kernel - Andries Brouwer, 1 Feb 1996
14  *
15  * Fixed do_loop_request() re-entrancy - Vincent.Renardias@waw.com Mar 20, 1997
16  *
17  * Added devfs support - Richard Gooch <rgooch@atnf.csiro.au> 16-Jan-1998
18  *
19  * Handle sparse backing files correctly - Kenn Humborg, Jun 28, 1998
20  *
21  * Loadable modules and other fixes by AK, 1998
22  *
23  * Make real block number available to downstream transfer functions, enables
24  * CBC (and relatives) mode encryption requiring unique IVs per data block.
25  * Reed H. Petty, rhp@draper.net
26  *
27  * Maximum number of loop devices now dynamic via max_loop module parameter.
28  * Russell Kroll <rkroll@exploits.org> 19990701
29  *
30  * Maximum number of loop devices when compiled-in now selectable by passing
31  * max_loop=<1-255> to the kernel on boot.
32  * Erik I. Bolsø, <eriki@himolde.no>, Oct 31, 1999
33  *
34  * Completely rewrite request handling to be make_request_fn style and
35  * non blocking, pushing work to a helper thread. Lots of fixes from
36  * Al Viro too.
37  * Jens Axboe <axboe@suse.de>, Nov 2000
38  *
39  * Support up to 256 loop devices
40  * Heinz Mauelshagen <mge@sistina.com>, Feb 2002
41  *
42  * Support for falling back on the write file operation when the address space
43  * operations write_begin is not available on the backing filesystem.
44  * Anton Altaparmakov, 16 Feb 2005
45  *
46  * Still To Fix:
47  * - Advisory locking is ignored here.
48  * - Should use an own CAP_* category instead of CAP_SYS_ADMIN
49  *
50  */
51
52 #include <linux/module.h>
53 #include <linux/moduleparam.h>
54 #include <linux/sched.h>
55 #include <linux/fs.h>
56 #include <linux/pagemap.h>
57 #include <linux/file.h>
58 #include <linux/stat.h>
59 #include <linux/errno.h>
60 #include <linux/major.h>
61 #include <linux/wait.h>
62 #include <linux/blkdev.h>
63 #include <linux/blkpg.h>
64 #include <linux/init.h>
65 #include <linux/swap.h>
66 #include <linux/slab.h>
67 #include <linux/compat.h>
68 #include <linux/suspend.h>
69 #include <linux/freezer.h>
70 #include <linux/mutex.h>
71 #include <linux/writeback.h>
72 #include <linux/completion.h>
73 #include <linux/highmem.h>
74 #include <linux/splice.h>
75 #include <linux/sysfs.h>
76 #include <linux/miscdevice.h>
77 #include <linux/falloc.h>
78 #include <linux/uio.h>
79 #include <linux/ioprio.h>
80 #include <linux/blk-cgroup.h>
81 #include <linux/sched/mm.h>
82 #include <linux/statfs.h>
83
84 #include "loop.h"
85
86 #include <linux/uaccess.h>
87
88 #define LOOP_IDLE_WORKER_TIMEOUT (60 * HZ)
89
90 static DEFINE_IDR(loop_index_idr);
91 static DEFINE_MUTEX(loop_ctl_mutex);
92 static DEFINE_MUTEX(loop_validate_mutex);
93
94 /**
95  * loop_global_lock_killable() - take locks for safe loop_validate_file() test
96  *
97  * @lo: struct loop_device
98  * @global: true if @lo is about to bind another "struct loop_device", false otherwise
99  *
100  * Returns 0 on success, -EINTR otherwise.
101  *
102  * Since loop_validate_file() traverses on other "struct loop_device" if
103  * is_loop_device() is true, we need a global lock for serializing concurrent
104  * loop_configure()/loop_change_fd()/__loop_clr_fd() calls.
105  */
106 static int loop_global_lock_killable(struct loop_device *lo, bool global)
107 {
108         int err;
109
110         if (global) {
111                 err = mutex_lock_killable(&loop_validate_mutex);
112                 if (err)
113                         return err;
114         }
115         err = mutex_lock_killable(&lo->lo_mutex);
116         if (err && global)
117                 mutex_unlock(&loop_validate_mutex);
118         return err;
119 }
120
121 /**
122  * loop_global_unlock() - release locks taken by loop_global_lock_killable()
123  *
124  * @lo: struct loop_device
125  * @global: true if @lo was about to bind another "struct loop_device", false otherwise
126  */
127 static void loop_global_unlock(struct loop_device *lo, bool global)
128 {
129         mutex_unlock(&lo->lo_mutex);
130         if (global)
131                 mutex_unlock(&loop_validate_mutex);
132 }
133
134 static int max_part;
135 static int part_shift;
136
137 static int transfer_xor(struct loop_device *lo, int cmd,
138                         struct page *raw_page, unsigned raw_off,
139                         struct page *loop_page, unsigned loop_off,
140                         int size, sector_t real_block)
141 {
142         char *raw_buf = kmap_atomic(raw_page) + raw_off;
143         char *loop_buf = kmap_atomic(loop_page) + loop_off;
144         char *in, *out, *key;
145         int i, keysize;
146
147         if (cmd == READ) {
148                 in = raw_buf;
149                 out = loop_buf;
150         } else {
151                 in = loop_buf;
152                 out = raw_buf;
153         }
154
155         key = lo->lo_encrypt_key;
156         keysize = lo->lo_encrypt_key_size;
157         for (i = 0; i < size; i++)
158                 *out++ = *in++ ^ key[(i & 511) % keysize];
159
160         kunmap_atomic(loop_buf);
161         kunmap_atomic(raw_buf);
162         cond_resched();
163         return 0;
164 }
165
166 static int xor_init(struct loop_device *lo, const struct loop_info64 *info)
167 {
168         if (unlikely(info->lo_encrypt_key_size <= 0))
169                 return -EINVAL;
170         return 0;
171 }
172
173 static struct loop_func_table none_funcs = {
174         .number = LO_CRYPT_NONE,
175 }; 
176
177 static struct loop_func_table xor_funcs = {
178         .number = LO_CRYPT_XOR,
179         .transfer = transfer_xor,
180         .init = xor_init
181 }; 
182
183 /* xfer_funcs[0] is special - its release function is never called */
184 static struct loop_func_table *xfer_funcs[MAX_LO_CRYPT] = {
185         &none_funcs,
186         &xor_funcs
187 };
188
189 static loff_t get_size(loff_t offset, loff_t sizelimit, struct file *file)
190 {
191         loff_t loopsize;
192
193         /* Compute loopsize in bytes */
194         loopsize = i_size_read(file->f_mapping->host);
195         if (offset > 0)
196                 loopsize -= offset;
197         /* offset is beyond i_size, weird but possible */
198         if (loopsize < 0)
199                 return 0;
200
201         if (sizelimit > 0 && sizelimit < loopsize)
202                 loopsize = sizelimit;
203         /*
204          * Unfortunately, if we want to do I/O on the device,
205          * the number of 512-byte sectors has to fit into a sector_t.
206          */
207         return loopsize >> 9;
208 }
209
210 static loff_t get_loop_size(struct loop_device *lo, struct file *file)
211 {
212         return get_size(lo->lo_offset, lo->lo_sizelimit, file);
213 }
214
215 static void __loop_update_dio(struct loop_device *lo, bool dio)
216 {
217         struct file *file = lo->lo_backing_file;
218         struct address_space *mapping = file->f_mapping;
219         struct inode *inode = mapping->host;
220         unsigned short sb_bsize = 0;
221         unsigned dio_align = 0;
222         bool use_dio;
223
224         if (inode->i_sb->s_bdev) {
225                 sb_bsize = bdev_logical_block_size(inode->i_sb->s_bdev);
226                 dio_align = sb_bsize - 1;
227         }
228
229         /*
230          * We support direct I/O only if lo_offset is aligned with the
231          * logical I/O size of backing device, and the logical block
232          * size of loop is bigger than the backing device's and the loop
233          * needn't transform transfer.
234          *
235          * TODO: the above condition may be loosed in the future, and
236          * direct I/O may be switched runtime at that time because most
237          * of requests in sane applications should be PAGE_SIZE aligned
238          */
239         if (dio) {
240                 if (queue_logical_block_size(lo->lo_queue) >= sb_bsize &&
241                                 !(lo->lo_offset & dio_align) &&
242                                 mapping->a_ops->direct_IO &&
243                                 !lo->transfer)
244                         use_dio = true;
245                 else
246                         use_dio = false;
247         } else {
248                 use_dio = false;
249         }
250
251         if (lo->use_dio == use_dio)
252                 return;
253
254         /* flush dirty pages before changing direct IO */
255         vfs_fsync(file, 0);
256
257         /*
258          * The flag of LO_FLAGS_DIRECT_IO is handled similarly with
259          * LO_FLAGS_READ_ONLY, both are set from kernel, and losetup
260          * will get updated by ioctl(LOOP_GET_STATUS)
261          */
262         if (lo->lo_state == Lo_bound)
263                 blk_mq_freeze_queue(lo->lo_queue);
264         lo->use_dio = use_dio;
265         if (use_dio) {
266                 blk_queue_flag_clear(QUEUE_FLAG_NOMERGES, lo->lo_queue);
267                 lo->lo_flags |= LO_FLAGS_DIRECT_IO;
268         } else {
269                 blk_queue_flag_set(QUEUE_FLAG_NOMERGES, lo->lo_queue);
270                 lo->lo_flags &= ~LO_FLAGS_DIRECT_IO;
271         }
272         if (lo->lo_state == Lo_bound)
273                 blk_mq_unfreeze_queue(lo->lo_queue);
274 }
275
276 /**
277  * loop_set_size() - sets device size and notifies userspace
278  * @lo: struct loop_device to set the size for
279  * @size: new size of the loop device
280  *
281  * Callers must validate that the size passed into this function fits into
282  * a sector_t, eg using loop_validate_size()
283  */
284 static void loop_set_size(struct loop_device *lo, loff_t size)
285 {
286         if (!set_capacity_and_notify(lo->lo_disk, size))
287                 kobject_uevent(&disk_to_dev(lo->lo_disk)->kobj, KOBJ_CHANGE);
288 }
289
290 static inline int
291 lo_do_transfer(struct loop_device *lo, int cmd,
292                struct page *rpage, unsigned roffs,
293                struct page *lpage, unsigned loffs,
294                int size, sector_t rblock)
295 {
296         int ret;
297
298         ret = lo->transfer(lo, cmd, rpage, roffs, lpage, loffs, size, rblock);
299         if (likely(!ret))
300                 return 0;
301
302         printk_ratelimited(KERN_ERR
303                 "loop: Transfer error at byte offset %llu, length %i.\n",
304                 (unsigned long long)rblock << 9, size);
305         return ret;
306 }
307
308 static int lo_write_bvec(struct file *file, struct bio_vec *bvec, loff_t *ppos)
309 {
310         struct iov_iter i;
311         ssize_t bw;
312
313         iov_iter_bvec(&i, WRITE, bvec, 1, bvec->bv_len);
314
315         file_start_write(file);
316         bw = vfs_iter_write(file, &i, ppos, 0);
317         file_end_write(file);
318
319         if (likely(bw ==  bvec->bv_len))
320                 return 0;
321
322         printk_ratelimited(KERN_ERR
323                 "loop: Write error at byte offset %llu, length %i.\n",
324                 (unsigned long long)*ppos, bvec->bv_len);
325         if (bw >= 0)
326                 bw = -EIO;
327         return bw;
328 }
329
330 static int lo_write_simple(struct loop_device *lo, struct request *rq,
331                 loff_t pos)
332 {
333         struct bio_vec bvec;
334         struct req_iterator iter;
335         int ret = 0;
336
337         rq_for_each_segment(bvec, rq, iter) {
338                 ret = lo_write_bvec(lo->lo_backing_file, &bvec, &pos);
339                 if (ret < 0)
340                         break;
341                 cond_resched();
342         }
343
344         return ret;
345 }
346
347 /*
348  * This is the slow, transforming version that needs to double buffer the
349  * data as it cannot do the transformations in place without having direct
350  * access to the destination pages of the backing file.
351  */
352 static int lo_write_transfer(struct loop_device *lo, struct request *rq,
353                 loff_t pos)
354 {
355         struct bio_vec bvec, b;
356         struct req_iterator iter;
357         struct page *page;
358         int ret = 0;
359
360         page = alloc_page(GFP_NOIO);
361         if (unlikely(!page))
362                 return -ENOMEM;
363
364         rq_for_each_segment(bvec, rq, iter) {
365                 ret = lo_do_transfer(lo, WRITE, page, 0, bvec.bv_page,
366                         bvec.bv_offset, bvec.bv_len, pos >> 9);
367                 if (unlikely(ret))
368                         break;
369
370                 b.bv_page = page;
371                 b.bv_offset = 0;
372                 b.bv_len = bvec.bv_len;
373                 ret = lo_write_bvec(lo->lo_backing_file, &b, &pos);
374                 if (ret < 0)
375                         break;
376         }
377
378         __free_page(page);
379         return ret;
380 }
381
382 static int lo_read_simple(struct loop_device *lo, struct request *rq,
383                 loff_t pos)
384 {
385         struct bio_vec bvec;
386         struct req_iterator iter;
387         struct iov_iter i;
388         ssize_t len;
389
390         rq_for_each_segment(bvec, rq, iter) {
391                 iov_iter_bvec(&i, READ, &bvec, 1, bvec.bv_len);
392                 len = vfs_iter_read(lo->lo_backing_file, &i, &pos, 0);
393                 if (len < 0)
394                         return len;
395
396                 flush_dcache_page(bvec.bv_page);
397
398                 if (len != bvec.bv_len) {
399                         struct bio *bio;
400
401                         __rq_for_each_bio(bio, rq)
402                                 zero_fill_bio(bio);
403                         break;
404                 }
405                 cond_resched();
406         }
407
408         return 0;
409 }
410
411 static int lo_read_transfer(struct loop_device *lo, struct request *rq,
412                 loff_t pos)
413 {
414         struct bio_vec bvec, b;
415         struct req_iterator iter;
416         struct iov_iter i;
417         struct page *page;
418         ssize_t len;
419         int ret = 0;
420
421         page = alloc_page(GFP_NOIO);
422         if (unlikely(!page))
423                 return -ENOMEM;
424
425         rq_for_each_segment(bvec, rq, iter) {
426                 loff_t offset = pos;
427
428                 b.bv_page = page;
429                 b.bv_offset = 0;
430                 b.bv_len = bvec.bv_len;
431
432                 iov_iter_bvec(&i, READ, &b, 1, b.bv_len);
433                 len = vfs_iter_read(lo->lo_backing_file, &i, &pos, 0);
434                 if (len < 0) {
435                         ret = len;
436                         goto out_free_page;
437                 }
438
439                 ret = lo_do_transfer(lo, READ, page, 0, bvec.bv_page,
440                         bvec.bv_offset, len, offset >> 9);
441                 if (ret)
442                         goto out_free_page;
443
444                 flush_dcache_page(bvec.bv_page);
445
446                 if (len != bvec.bv_len) {
447                         struct bio *bio;
448
449                         __rq_for_each_bio(bio, rq)
450                                 zero_fill_bio(bio);
451                         break;
452                 }
453         }
454
455         ret = 0;
456 out_free_page:
457         __free_page(page);
458         return ret;
459 }
460
461 static int lo_fallocate(struct loop_device *lo, struct request *rq, loff_t pos,
462                         int mode)
463 {
464         /*
465          * We use fallocate to manipulate the space mappings used by the image
466          * a.k.a. discard/zerorange. However we do not support this if
467          * encryption is enabled, because it may give an attacker useful
468          * information.
469          */
470         struct file *file = lo->lo_backing_file;
471         struct request_queue *q = lo->lo_queue;
472         int ret;
473
474         mode |= FALLOC_FL_KEEP_SIZE;
475
476         if (!blk_queue_discard(q)) {
477                 ret = -EOPNOTSUPP;
478                 goto out;
479         }
480
481         ret = file->f_op->fallocate(file, mode, pos, blk_rq_bytes(rq));
482         if (unlikely(ret && ret != -EINVAL && ret != -EOPNOTSUPP))
483                 ret = -EIO;
484  out:
485         return ret;
486 }
487
488 static int lo_req_flush(struct loop_device *lo, struct request *rq)
489 {
490         struct file *file = lo->lo_backing_file;
491         int ret = vfs_fsync(file, 0);
492         if (unlikely(ret && ret != -EINVAL))
493                 ret = -EIO;
494
495         return ret;
496 }
497
498 static void lo_complete_rq(struct request *rq)
499 {
500         struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq);
501         blk_status_t ret = BLK_STS_OK;
502
503         if (!cmd->use_aio || cmd->ret < 0 || cmd->ret == blk_rq_bytes(rq) ||
504             req_op(rq) != REQ_OP_READ) {
505                 if (cmd->ret < 0)
506                         ret = errno_to_blk_status(cmd->ret);
507                 goto end_io;
508         }
509
510         /*
511          * Short READ - if we got some data, advance our request and
512          * retry it. If we got no data, end the rest with EIO.
513          */
514         if (cmd->ret) {
515                 blk_update_request(rq, BLK_STS_OK, cmd->ret);
516                 cmd->ret = 0;
517                 blk_mq_requeue_request(rq, true);
518         } else {
519                 if (cmd->use_aio) {
520                         struct bio *bio = rq->bio;
521
522                         while (bio) {
523                                 zero_fill_bio(bio);
524                                 bio = bio->bi_next;
525                         }
526                 }
527                 ret = BLK_STS_IOERR;
528 end_io:
529                 blk_mq_end_request(rq, ret);
530         }
531 }
532
533 static void lo_rw_aio_do_completion(struct loop_cmd *cmd)
534 {
535         struct request *rq = blk_mq_rq_from_pdu(cmd);
536
537         if (!atomic_dec_and_test(&cmd->ref))
538                 return;
539         kfree(cmd->bvec);
540         cmd->bvec = NULL;
541         if (likely(!blk_should_fake_timeout(rq->q)))
542                 blk_mq_complete_request(rq);
543 }
544
545 static void lo_rw_aio_complete(struct kiocb *iocb, long ret, long ret2)
546 {
547         struct loop_cmd *cmd = container_of(iocb, struct loop_cmd, iocb);
548
549         cmd->ret = ret;
550         lo_rw_aio_do_completion(cmd);
551 }
552
553 static int lo_rw_aio(struct loop_device *lo, struct loop_cmd *cmd,
554                      loff_t pos, bool rw)
555 {
556         struct iov_iter iter;
557         struct req_iterator rq_iter;
558         struct bio_vec *bvec;
559         struct request *rq = blk_mq_rq_from_pdu(cmd);
560         struct bio *bio = rq->bio;
561         struct file *file = lo->lo_backing_file;
562         struct bio_vec tmp;
563         unsigned int offset;
564         int nr_bvec = 0;
565         int ret;
566
567         rq_for_each_bvec(tmp, rq, rq_iter)
568                 nr_bvec++;
569
570         if (rq->bio != rq->biotail) {
571
572                 bvec = kmalloc_array(nr_bvec, sizeof(struct bio_vec),
573                                      GFP_NOIO);
574                 if (!bvec)
575                         return -EIO;
576                 cmd->bvec = bvec;
577
578                 /*
579                  * The bios of the request may be started from the middle of
580                  * the 'bvec' because of bio splitting, so we can't directly
581                  * copy bio->bi_iov_vec to new bvec. The rq_for_each_bvec
582                  * API will take care of all details for us.
583                  */
584                 rq_for_each_bvec(tmp, rq, rq_iter) {
585                         *bvec = tmp;
586                         bvec++;
587                 }
588                 bvec = cmd->bvec;
589                 offset = 0;
590         } else {
591                 /*
592                  * Same here, this bio may be started from the middle of the
593                  * 'bvec' because of bio splitting, so offset from the bvec
594                  * must be passed to iov iterator
595                  */
596                 offset = bio->bi_iter.bi_bvec_done;
597                 bvec = __bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter);
598         }
599         atomic_set(&cmd->ref, 2);
600
601         iov_iter_bvec(&iter, rw, bvec, nr_bvec, blk_rq_bytes(rq));
602         iter.iov_offset = offset;
603
604         cmd->iocb.ki_pos = pos;
605         cmd->iocb.ki_filp = file;
606         cmd->iocb.ki_complete = lo_rw_aio_complete;
607         cmd->iocb.ki_flags = IOCB_DIRECT;
608         cmd->iocb.ki_ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, 0);
609
610         if (rw == WRITE)
611                 ret = call_write_iter(file, &cmd->iocb, &iter);
612         else
613                 ret = call_read_iter(file, &cmd->iocb, &iter);
614
615         lo_rw_aio_do_completion(cmd);
616
617         if (ret != -EIOCBQUEUED)
618                 cmd->iocb.ki_complete(&cmd->iocb, ret, 0);
619         return 0;
620 }
621
622 static int do_req_filebacked(struct loop_device *lo, struct request *rq)
623 {
624         struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq);
625         loff_t pos = ((loff_t) blk_rq_pos(rq) << 9) + lo->lo_offset;
626
627         /*
628          * lo_write_simple and lo_read_simple should have been covered
629          * by io submit style function like lo_rw_aio(), one blocker
630          * is that lo_read_simple() need to call flush_dcache_page after
631          * the page is written from kernel, and it isn't easy to handle
632          * this in io submit style function which submits all segments
633          * of the req at one time. And direct read IO doesn't need to
634          * run flush_dcache_page().
635          */
636         switch (req_op(rq)) {
637         case REQ_OP_FLUSH:
638                 return lo_req_flush(lo, rq);
639         case REQ_OP_WRITE_ZEROES:
640                 /*
641                  * If the caller doesn't want deallocation, call zeroout to
642                  * write zeroes the range.  Otherwise, punch them out.
643                  */
644                 return lo_fallocate(lo, rq, pos,
645                         (rq->cmd_flags & REQ_NOUNMAP) ?
646                                 FALLOC_FL_ZERO_RANGE :
647                                 FALLOC_FL_PUNCH_HOLE);
648         case REQ_OP_DISCARD:
649                 return lo_fallocate(lo, rq, pos, FALLOC_FL_PUNCH_HOLE);
650         case REQ_OP_WRITE:
651                 if (lo->transfer)
652                         return lo_write_transfer(lo, rq, pos);
653                 else if (cmd->use_aio)
654                         return lo_rw_aio(lo, cmd, pos, WRITE);
655                 else
656                         return lo_write_simple(lo, rq, pos);
657         case REQ_OP_READ:
658                 if (lo->transfer)
659                         return lo_read_transfer(lo, rq, pos);
660                 else if (cmd->use_aio)
661                         return lo_rw_aio(lo, cmd, pos, READ);
662                 else
663                         return lo_read_simple(lo, rq, pos);
664         default:
665                 WARN_ON_ONCE(1);
666                 return -EIO;
667         }
668 }
669
670 static inline void loop_update_dio(struct loop_device *lo)
671 {
672         __loop_update_dio(lo, (lo->lo_backing_file->f_flags & O_DIRECT) |
673                                 lo->use_dio);
674 }
675
676 static void loop_reread_partitions(struct loop_device *lo)
677 {
678         int rc;
679
680         mutex_lock(&lo->lo_disk->open_mutex);
681         rc = bdev_disk_changed(lo->lo_disk, false);
682         mutex_unlock(&lo->lo_disk->open_mutex);
683         if (rc)
684                 pr_warn("%s: partition scan of loop%d (%s) failed (rc=%d)\n",
685                         __func__, lo->lo_number, lo->lo_file_name, rc);
686 }
687
688 static inline int is_loop_device(struct file *file)
689 {
690         struct inode *i = file->f_mapping->host;
691
692         return i && S_ISBLK(i->i_mode) && imajor(i) == LOOP_MAJOR;
693 }
694
695 static int loop_validate_file(struct file *file, struct block_device *bdev)
696 {
697         struct inode    *inode = file->f_mapping->host;
698         struct file     *f = file;
699
700         /* Avoid recursion */
701         while (is_loop_device(f)) {
702                 struct loop_device *l;
703
704                 lockdep_assert_held(&loop_validate_mutex);
705                 if (f->f_mapping->host->i_rdev == bdev->bd_dev)
706                         return -EBADF;
707
708                 l = I_BDEV(f->f_mapping->host)->bd_disk->private_data;
709                 if (l->lo_state != Lo_bound)
710                         return -EINVAL;
711                 /* Order wrt setting lo->lo_backing_file in loop_configure(). */
712                 rmb();
713                 f = l->lo_backing_file;
714         }
715         if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))
716                 return -EINVAL;
717         return 0;
718 }
719
720 /*
721  * loop_change_fd switched the backing store of a loopback device to
722  * a new file. This is useful for operating system installers to free up
723  * the original file and in High Availability environments to switch to
724  * an alternative location for the content in case of server meltdown.
725  * This can only work if the loop device is used read-only, and if the
726  * new backing store is the same size and type as the old backing store.
727  */
728 static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
729                           unsigned int arg)
730 {
731         struct file *file = fget(arg);
732         struct file *old_file;
733         int error;
734         bool partscan;
735         bool is_loop;
736
737         if (!file)
738                 return -EBADF;
739         is_loop = is_loop_device(file);
740         error = loop_global_lock_killable(lo, is_loop);
741         if (error)
742                 goto out_putf;
743         error = -ENXIO;
744         if (lo->lo_state != Lo_bound)
745                 goto out_err;
746
747         /* the loop device has to be read-only */
748         error = -EINVAL;
749         if (!(lo->lo_flags & LO_FLAGS_READ_ONLY))
750                 goto out_err;
751
752         error = loop_validate_file(file, bdev);
753         if (error)
754                 goto out_err;
755
756         old_file = lo->lo_backing_file;
757
758         error = -EINVAL;
759
760         /* size of the new backing store needs to be the same */
761         if (get_loop_size(lo, file) != get_loop_size(lo, old_file))
762                 goto out_err;
763
764         /* and ... switch */
765         disk_force_media_change(lo->lo_disk, DISK_EVENT_MEDIA_CHANGE);
766         blk_mq_freeze_queue(lo->lo_queue);
767         mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
768         lo->lo_backing_file = file;
769         lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping);
770         mapping_set_gfp_mask(file->f_mapping,
771                              lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
772         loop_update_dio(lo);
773         blk_mq_unfreeze_queue(lo->lo_queue);
774         partscan = lo->lo_flags & LO_FLAGS_PARTSCAN;
775         loop_global_unlock(lo, is_loop);
776
777         /*
778          * Flush loop_validate_file() before fput(), for l->lo_backing_file
779          * might be pointing at old_file which might be the last reference.
780          */
781         if (!is_loop) {
782                 mutex_lock(&loop_validate_mutex);
783                 mutex_unlock(&loop_validate_mutex);
784         }
785         /*
786          * We must drop file reference outside of lo_mutex as dropping
787          * the file ref can take open_mutex which creates circular locking
788          * dependency.
789          */
790         fput(old_file);
791         if (partscan)
792                 loop_reread_partitions(lo);
793         return 0;
794
795 out_err:
796         loop_global_unlock(lo, is_loop);
797 out_putf:
798         fput(file);
799         return error;
800 }
801
802 /* loop sysfs attributes */
803
804 static ssize_t loop_attr_show(struct device *dev, char *page,
805                               ssize_t (*callback)(struct loop_device *, char *))
806 {
807         struct gendisk *disk = dev_to_disk(dev);
808         struct loop_device *lo = disk->private_data;
809
810         return callback(lo, page);
811 }
812
813 #define LOOP_ATTR_RO(_name)                                             \
814 static ssize_t loop_attr_##_name##_show(struct loop_device *, char *);  \
815 static ssize_t loop_attr_do_show_##_name(struct device *d,              \
816                                 struct device_attribute *attr, char *b) \
817 {                                                                       \
818         return loop_attr_show(d, b, loop_attr_##_name##_show);          \
819 }                                                                       \
820 static struct device_attribute loop_attr_##_name =                      \
821         __ATTR(_name, 0444, loop_attr_do_show_##_name, NULL);
822
823 static ssize_t loop_attr_backing_file_show(struct loop_device *lo, char *buf)
824 {
825         ssize_t ret;
826         char *p = NULL;
827
828         spin_lock_irq(&lo->lo_lock);
829         if (lo->lo_backing_file)
830                 p = file_path(lo->lo_backing_file, buf, PAGE_SIZE - 1);
831         spin_unlock_irq(&lo->lo_lock);
832
833         if (IS_ERR_OR_NULL(p))
834                 ret = PTR_ERR(p);
835         else {
836                 ret = strlen(p);
837                 memmove(buf, p, ret);
838                 buf[ret++] = '\n';
839                 buf[ret] = 0;
840         }
841
842         return ret;
843 }
844
845 static ssize_t loop_attr_offset_show(struct loop_device *lo, char *buf)
846 {
847         return sysfs_emit(buf, "%llu\n", (unsigned long long)lo->lo_offset);
848 }
849
850 static ssize_t loop_attr_sizelimit_show(struct loop_device *lo, char *buf)
851 {
852         return sysfs_emit(buf, "%llu\n", (unsigned long long)lo->lo_sizelimit);
853 }
854
855 static ssize_t loop_attr_autoclear_show(struct loop_device *lo, char *buf)
856 {
857         int autoclear = (lo->lo_flags & LO_FLAGS_AUTOCLEAR);
858
859         return sysfs_emit(buf, "%s\n", autoclear ? "1" : "0");
860 }
861
862 static ssize_t loop_attr_partscan_show(struct loop_device *lo, char *buf)
863 {
864         int partscan = (lo->lo_flags & LO_FLAGS_PARTSCAN);
865
866         return sysfs_emit(buf, "%s\n", partscan ? "1" : "0");
867 }
868
869 static ssize_t loop_attr_dio_show(struct loop_device *lo, char *buf)
870 {
871         int dio = (lo->lo_flags & LO_FLAGS_DIRECT_IO);
872
873         return sysfs_emit(buf, "%s\n", dio ? "1" : "0");
874 }
875
876 LOOP_ATTR_RO(backing_file);
877 LOOP_ATTR_RO(offset);
878 LOOP_ATTR_RO(sizelimit);
879 LOOP_ATTR_RO(autoclear);
880 LOOP_ATTR_RO(partscan);
881 LOOP_ATTR_RO(dio);
882
883 static struct attribute *loop_attrs[] = {
884         &loop_attr_backing_file.attr,
885         &loop_attr_offset.attr,
886         &loop_attr_sizelimit.attr,
887         &loop_attr_autoclear.attr,
888         &loop_attr_partscan.attr,
889         &loop_attr_dio.attr,
890         NULL,
891 };
892
893 static struct attribute_group loop_attribute_group = {
894         .name = "loop",
895         .attrs= loop_attrs,
896 };
897
898 static void loop_sysfs_init(struct loop_device *lo)
899 {
900         lo->sysfs_inited = !sysfs_create_group(&disk_to_dev(lo->lo_disk)->kobj,
901                                                 &loop_attribute_group);
902 }
903
904 static void loop_sysfs_exit(struct loop_device *lo)
905 {
906         if (lo->sysfs_inited)
907                 sysfs_remove_group(&disk_to_dev(lo->lo_disk)->kobj,
908                                    &loop_attribute_group);
909 }
910
911 static void loop_config_discard(struct loop_device *lo)
912 {
913         struct file *file = lo->lo_backing_file;
914         struct inode *inode = file->f_mapping->host;
915         struct request_queue *q = lo->lo_queue;
916         u32 granularity, max_discard_sectors;
917
918         /*
919          * If the backing device is a block device, mirror its zeroing
920          * capability. Set the discard sectors to the block device's zeroing
921          * capabilities because loop discards result in blkdev_issue_zeroout(),
922          * not blkdev_issue_discard(). This maintains consistent behavior with
923          * file-backed loop devices: discarded regions read back as zero.
924          */
925         if (S_ISBLK(inode->i_mode) && !lo->lo_encrypt_key_size) {
926                 struct request_queue *backingq = bdev_get_queue(I_BDEV(inode));
927
928                 max_discard_sectors = backingq->limits.max_write_zeroes_sectors;
929                 granularity = backingq->limits.discard_granularity ?:
930                         queue_physical_block_size(backingq);
931
932         /*
933          * We use punch hole to reclaim the free space used by the
934          * image a.k.a. discard. However we do not support discard if
935          * encryption is enabled, because it may give an attacker
936          * useful information.
937          */
938         } else if (!file->f_op->fallocate || lo->lo_encrypt_key_size) {
939                 max_discard_sectors = 0;
940                 granularity = 0;
941
942         } else {
943                 struct kstatfs sbuf;
944
945                 max_discard_sectors = UINT_MAX >> 9;
946                 if (!vfs_statfs(&file->f_path, &sbuf))
947                         granularity = sbuf.f_bsize;
948                 else
949                         max_discard_sectors = 0;
950         }
951
952         if (max_discard_sectors) {
953                 q->limits.discard_granularity = granularity;
954                 blk_queue_max_discard_sectors(q, max_discard_sectors);
955                 blk_queue_max_write_zeroes_sectors(q, max_discard_sectors);
956                 blk_queue_flag_set(QUEUE_FLAG_DISCARD, q);
957         } else {
958                 q->limits.discard_granularity = 0;
959                 blk_queue_max_discard_sectors(q, 0);
960                 blk_queue_max_write_zeroes_sectors(q, 0);
961                 blk_queue_flag_clear(QUEUE_FLAG_DISCARD, q);
962         }
963         q->limits.discard_alignment = 0;
964 }
965
966 struct loop_worker {
967         struct rb_node rb_node;
968         struct work_struct work;
969         struct list_head cmd_list;
970         struct list_head idle_list;
971         struct loop_device *lo;
972         struct cgroup_subsys_state *blkcg_css;
973         unsigned long last_ran_at;
974 };
975
976 static void loop_workfn(struct work_struct *work);
977 static void loop_rootcg_workfn(struct work_struct *work);
978 static void loop_free_idle_workers(struct timer_list *timer);
979
980 #ifdef CONFIG_BLK_CGROUP
981 static inline int queue_on_root_worker(struct cgroup_subsys_state *css)
982 {
983         return !css || css == blkcg_root_css;
984 }
985 #else
986 static inline int queue_on_root_worker(struct cgroup_subsys_state *css)
987 {
988         return !css;
989 }
990 #endif
991
992 static void loop_queue_work(struct loop_device *lo, struct loop_cmd *cmd)
993 {
994         struct rb_node **node = &(lo->worker_tree.rb_node), *parent = NULL;
995         struct loop_worker *cur_worker, *worker = NULL;
996         struct work_struct *work;
997         struct list_head *cmd_list;
998
999         spin_lock_irq(&lo->lo_work_lock);
1000
1001         if (queue_on_root_worker(cmd->blkcg_css))
1002                 goto queue_work;
1003
1004         node = &lo->worker_tree.rb_node;
1005
1006         while (*node) {
1007                 parent = *node;
1008                 cur_worker = container_of(*node, struct loop_worker, rb_node);
1009                 if (cur_worker->blkcg_css == cmd->blkcg_css) {
1010                         worker = cur_worker;
1011                         break;
1012                 } else if ((long)cur_worker->blkcg_css < (long)cmd->blkcg_css) {
1013                         node = &(*node)->rb_left;
1014                 } else {
1015                         node = &(*node)->rb_right;
1016                 }
1017         }
1018         if (worker)
1019                 goto queue_work;
1020
1021         worker = kzalloc(sizeof(struct loop_worker), GFP_NOWAIT | __GFP_NOWARN);
1022         /*
1023          * In the event we cannot allocate a worker, just queue on the
1024          * rootcg worker and issue the I/O as the rootcg
1025          */
1026         if (!worker) {
1027                 cmd->blkcg_css = NULL;
1028                 if (cmd->memcg_css)
1029                         css_put(cmd->memcg_css);
1030                 cmd->memcg_css = NULL;
1031                 goto queue_work;
1032         }
1033
1034         worker->blkcg_css = cmd->blkcg_css;
1035         css_get(worker->blkcg_css);
1036         INIT_WORK(&worker->work, loop_workfn);
1037         INIT_LIST_HEAD(&worker->cmd_list);
1038         INIT_LIST_HEAD(&worker->idle_list);
1039         worker->lo = lo;
1040         rb_link_node(&worker->rb_node, parent, node);
1041         rb_insert_color(&worker->rb_node, &lo->worker_tree);
1042 queue_work:
1043         if (worker) {
1044                 /*
1045                  * We need to remove from the idle list here while
1046                  * holding the lock so that the idle timer doesn't
1047                  * free the worker
1048                  */
1049                 if (!list_empty(&worker->idle_list))
1050                         list_del_init(&worker->idle_list);
1051                 work = &worker->work;
1052                 cmd_list = &worker->cmd_list;
1053         } else {
1054                 work = &lo->rootcg_work;
1055                 cmd_list = &lo->rootcg_cmd_list;
1056         }
1057         list_add_tail(&cmd->list_entry, cmd_list);
1058         queue_work(lo->workqueue, work);
1059         spin_unlock_irq(&lo->lo_work_lock);
1060 }
1061
1062 static void loop_update_rotational(struct loop_device *lo)
1063 {
1064         struct file *file = lo->lo_backing_file;
1065         struct inode *file_inode = file->f_mapping->host;
1066         struct block_device *file_bdev = file_inode->i_sb->s_bdev;
1067         struct request_queue *q = lo->lo_queue;
1068         bool nonrot = true;
1069
1070         /* not all filesystems (e.g. tmpfs) have a sb->s_bdev */
1071         if (file_bdev)
1072                 nonrot = blk_queue_nonrot(bdev_get_queue(file_bdev));
1073
1074         if (nonrot)
1075                 blk_queue_flag_set(QUEUE_FLAG_NONROT, q);
1076         else
1077                 blk_queue_flag_clear(QUEUE_FLAG_NONROT, q);
1078 }
1079
1080 static int
1081 loop_release_xfer(struct loop_device *lo)
1082 {
1083         int err = 0;
1084         struct loop_func_table *xfer = lo->lo_encryption;
1085
1086         if (xfer) {
1087                 if (xfer->release)
1088                         err = xfer->release(lo);
1089                 lo->transfer = NULL;
1090                 lo->lo_encryption = NULL;
1091                 module_put(xfer->owner);
1092         }
1093         return err;
1094 }
1095
1096 static int
1097 loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
1098                const struct loop_info64 *i)
1099 {
1100         int err = 0;
1101
1102         if (xfer) {
1103                 struct module *owner = xfer->owner;
1104
1105                 if (!try_module_get(owner))
1106                         return -EINVAL;
1107                 if (xfer->init)
1108                         err = xfer->init(lo, i);
1109                 if (err)
1110                         module_put(owner);
1111                 else
1112                         lo->lo_encryption = xfer;
1113         }
1114         return err;
1115 }
1116
1117 /**
1118  * loop_set_status_from_info - configure device from loop_info
1119  * @lo: struct loop_device to configure
1120  * @info: struct loop_info64 to configure the device with
1121  *
1122  * Configures the loop device parameters according to the passed
1123  * in loop_info64 configuration.
1124  */
1125 static int
1126 loop_set_status_from_info(struct loop_device *lo,
1127                           const struct loop_info64 *info)
1128 {
1129         int err;
1130         struct loop_func_table *xfer;
1131         kuid_t uid = current_uid();
1132
1133         if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE)
1134                 return -EINVAL;
1135
1136         err = loop_release_xfer(lo);
1137         if (err)
1138                 return err;
1139
1140         if (info->lo_encrypt_type) {
1141                 unsigned int type = info->lo_encrypt_type;
1142
1143                 if (type >= MAX_LO_CRYPT)
1144                         return -EINVAL;
1145                 xfer = xfer_funcs[type];
1146                 if (xfer == NULL)
1147                         return -EINVAL;
1148         } else
1149                 xfer = NULL;
1150
1151         err = loop_init_xfer(lo, xfer, info);
1152         if (err)
1153                 return err;
1154
1155         lo->lo_offset = info->lo_offset;
1156         lo->lo_sizelimit = info->lo_sizelimit;
1157         memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE);
1158         memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE);
1159         lo->lo_file_name[LO_NAME_SIZE-1] = 0;
1160         lo->lo_crypt_name[LO_NAME_SIZE-1] = 0;
1161
1162         if (!xfer)
1163                 xfer = &none_funcs;
1164         lo->transfer = xfer->transfer;
1165         lo->ioctl = xfer->ioctl;
1166
1167         lo->lo_flags = info->lo_flags;
1168
1169         lo->lo_encrypt_key_size = info->lo_encrypt_key_size;
1170         lo->lo_init[0] = info->lo_init[0];
1171         lo->lo_init[1] = info->lo_init[1];
1172         if (info->lo_encrypt_key_size) {
1173                 memcpy(lo->lo_encrypt_key, info->lo_encrypt_key,
1174                        info->lo_encrypt_key_size);
1175                 lo->lo_key_owner = uid;
1176         }
1177
1178         return 0;
1179 }
1180
1181 static int loop_configure(struct loop_device *lo, fmode_t mode,
1182                           struct block_device *bdev,
1183                           const struct loop_config *config)
1184 {
1185         struct file *file = fget(config->fd);
1186         struct inode *inode;
1187         struct address_space *mapping;
1188         int error;
1189         loff_t size;
1190         bool partscan;
1191         unsigned short bsize;
1192         bool is_loop;
1193
1194         if (!file)
1195                 return -EBADF;
1196         is_loop = is_loop_device(file);
1197
1198         /* This is safe, since we have a reference from open(). */
1199         __module_get(THIS_MODULE);
1200
1201         /*
1202          * If we don't hold exclusive handle for the device, upgrade to it
1203          * here to avoid changing device under exclusive owner.
1204          */
1205         if (!(mode & FMODE_EXCL)) {
1206                 error = bd_prepare_to_claim(bdev, loop_configure);
1207                 if (error)
1208                         goto out_putf;
1209         }
1210
1211         error = loop_global_lock_killable(lo, is_loop);
1212         if (error)
1213                 goto out_bdev;
1214
1215         error = -EBUSY;
1216         if (lo->lo_state != Lo_unbound)
1217                 goto out_unlock;
1218
1219         error = loop_validate_file(file, bdev);
1220         if (error)
1221                 goto out_unlock;
1222
1223         mapping = file->f_mapping;
1224         inode = mapping->host;
1225
1226         if ((config->info.lo_flags & ~LOOP_CONFIGURE_SETTABLE_FLAGS) != 0) {
1227                 error = -EINVAL;
1228                 goto out_unlock;
1229         }
1230
1231         if (config->block_size) {
1232                 error = blk_validate_block_size(config->block_size);
1233                 if (error)
1234                         goto out_unlock;
1235         }
1236
1237         error = loop_set_status_from_info(lo, &config->info);
1238         if (error)
1239                 goto out_unlock;
1240
1241         if (!(file->f_mode & FMODE_WRITE) || !(mode & FMODE_WRITE) ||
1242             !file->f_op->write_iter)
1243                 lo->lo_flags |= LO_FLAGS_READ_ONLY;
1244
1245         lo->workqueue = alloc_workqueue("loop%d",
1246                                         WQ_UNBOUND | WQ_FREEZABLE,
1247                                         0,
1248                                         lo->lo_number);
1249         if (!lo->workqueue) {
1250                 error = -ENOMEM;
1251                 goto out_unlock;
1252         }
1253
1254         disk_force_media_change(lo->lo_disk, DISK_EVENT_MEDIA_CHANGE);
1255         set_disk_ro(lo->lo_disk, (lo->lo_flags & LO_FLAGS_READ_ONLY) != 0);
1256
1257         INIT_WORK(&lo->rootcg_work, loop_rootcg_workfn);
1258         INIT_LIST_HEAD(&lo->rootcg_cmd_list);
1259         INIT_LIST_HEAD(&lo->idle_worker_list);
1260         lo->worker_tree = RB_ROOT;
1261         timer_setup(&lo->timer, loop_free_idle_workers,
1262                 TIMER_DEFERRABLE);
1263         lo->use_dio = lo->lo_flags & LO_FLAGS_DIRECT_IO;
1264         lo->lo_device = bdev;
1265         lo->lo_backing_file = file;
1266         lo->old_gfp_mask = mapping_gfp_mask(mapping);
1267         mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
1268
1269         if (!(lo->lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync)
1270                 blk_queue_write_cache(lo->lo_queue, true, false);
1271
1272         if (config->block_size)
1273                 bsize = config->block_size;
1274         else if ((lo->lo_backing_file->f_flags & O_DIRECT) && inode->i_sb->s_bdev)
1275                 /* In case of direct I/O, match underlying block size */
1276                 bsize = bdev_logical_block_size(inode->i_sb->s_bdev);
1277         else
1278                 bsize = 512;
1279
1280         blk_queue_logical_block_size(lo->lo_queue, bsize);
1281         blk_queue_physical_block_size(lo->lo_queue, bsize);
1282         blk_queue_io_min(lo->lo_queue, bsize);
1283
1284         loop_config_discard(lo);
1285         loop_update_rotational(lo);
1286         loop_update_dio(lo);
1287         loop_sysfs_init(lo);
1288
1289         size = get_loop_size(lo, file);
1290         loop_set_size(lo, size);
1291
1292         /* Order wrt reading lo_state in loop_validate_file(). */
1293         wmb();
1294
1295         lo->lo_state = Lo_bound;
1296         if (part_shift)
1297                 lo->lo_flags |= LO_FLAGS_PARTSCAN;
1298         partscan = lo->lo_flags & LO_FLAGS_PARTSCAN;
1299         if (partscan)
1300                 lo->lo_disk->flags &= ~GENHD_FL_NO_PART_SCAN;
1301
1302         loop_global_unlock(lo, is_loop);
1303         if (partscan)
1304                 loop_reread_partitions(lo);
1305         if (!(mode & FMODE_EXCL))
1306                 bd_abort_claiming(bdev, loop_configure);
1307         return 0;
1308
1309 out_unlock:
1310         loop_global_unlock(lo, is_loop);
1311 out_bdev:
1312         if (!(mode & FMODE_EXCL))
1313                 bd_abort_claiming(bdev, loop_configure);
1314 out_putf:
1315         fput(file);
1316         /* This is safe: open() is still holding a reference. */
1317         module_put(THIS_MODULE);
1318         return error;
1319 }
1320
1321 static int __loop_clr_fd(struct loop_device *lo, bool release)
1322 {
1323         struct file *filp = NULL;
1324         gfp_t gfp = lo->old_gfp_mask;
1325         struct block_device *bdev = lo->lo_device;
1326         int err = 0;
1327         bool partscan = false;
1328         int lo_number;
1329         struct loop_worker *pos, *worker;
1330
1331         /*
1332          * Flush loop_configure() and loop_change_fd(). It is acceptable for
1333          * loop_validate_file() to succeed, for actual clear operation has not
1334          * started yet.
1335          */
1336         mutex_lock(&loop_validate_mutex);
1337         mutex_unlock(&loop_validate_mutex);
1338         /*
1339          * loop_validate_file() now fails because l->lo_state != Lo_bound
1340          * became visible.
1341          */
1342
1343         mutex_lock(&lo->lo_mutex);
1344         if (WARN_ON_ONCE(lo->lo_state != Lo_rundown)) {
1345                 err = -ENXIO;
1346                 goto out_unlock;
1347         }
1348
1349         filp = lo->lo_backing_file;
1350         if (filp == NULL) {
1351                 err = -EINVAL;
1352                 goto out_unlock;
1353         }
1354
1355         if (test_bit(QUEUE_FLAG_WC, &lo->lo_queue->queue_flags))
1356                 blk_queue_write_cache(lo->lo_queue, false, false);
1357
1358         /* freeze request queue during the transition */
1359         blk_mq_freeze_queue(lo->lo_queue);
1360
1361         destroy_workqueue(lo->workqueue);
1362         spin_lock_irq(&lo->lo_work_lock);
1363         list_for_each_entry_safe(worker, pos, &lo->idle_worker_list,
1364                                 idle_list) {
1365                 list_del(&worker->idle_list);
1366                 rb_erase(&worker->rb_node, &lo->worker_tree);
1367                 css_put(worker->blkcg_css);
1368                 kfree(worker);
1369         }
1370         spin_unlock_irq(&lo->lo_work_lock);
1371         del_timer_sync(&lo->timer);
1372
1373         spin_lock_irq(&lo->lo_lock);
1374         lo->lo_backing_file = NULL;
1375         spin_unlock_irq(&lo->lo_lock);
1376
1377         loop_release_xfer(lo);
1378         lo->transfer = NULL;
1379         lo->ioctl = NULL;
1380         lo->lo_device = NULL;
1381         lo->lo_encryption = NULL;
1382         lo->lo_offset = 0;
1383         lo->lo_sizelimit = 0;
1384         lo->lo_encrypt_key_size = 0;
1385         memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE);
1386         memset(lo->lo_crypt_name, 0, LO_NAME_SIZE);
1387         memset(lo->lo_file_name, 0, LO_NAME_SIZE);
1388         blk_queue_logical_block_size(lo->lo_queue, 512);
1389         blk_queue_physical_block_size(lo->lo_queue, 512);
1390         blk_queue_io_min(lo->lo_queue, 512);
1391         if (bdev) {
1392                 invalidate_bdev(bdev);
1393                 bdev->bd_inode->i_mapping->wb_err = 0;
1394         }
1395         set_capacity(lo->lo_disk, 0);
1396         loop_sysfs_exit(lo);
1397         if (bdev) {
1398                 /* let user-space know about this change */
1399                 kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
1400         }
1401         mapping_set_gfp_mask(filp->f_mapping, gfp);
1402         /* This is safe: open() is still holding a reference. */
1403         module_put(THIS_MODULE);
1404         blk_mq_unfreeze_queue(lo->lo_queue);
1405
1406         partscan = lo->lo_flags & LO_FLAGS_PARTSCAN && bdev;
1407         lo_number = lo->lo_number;
1408         disk_force_media_change(lo->lo_disk, DISK_EVENT_MEDIA_CHANGE);
1409 out_unlock:
1410         mutex_unlock(&lo->lo_mutex);
1411         if (partscan) {
1412                 /*
1413                  * open_mutex has been held already in release path, so don't
1414                  * acquire it if this function is called in such case.
1415                  *
1416                  * If the reread partition isn't from release path, lo_refcnt
1417                  * must be at least one and it can only become zero when the
1418                  * current holder is released.
1419                  */
1420                 if (!release)
1421                         mutex_lock(&lo->lo_disk->open_mutex);
1422                 err = bdev_disk_changed(lo->lo_disk, false);
1423                 if (!release)
1424                         mutex_unlock(&lo->lo_disk->open_mutex);
1425                 if (err)
1426                         pr_warn("%s: partition scan of loop%d failed (rc=%d)\n",
1427                                 __func__, lo_number, err);
1428                 /* Device is gone, no point in returning error */
1429                 err = 0;
1430         }
1431
1432         /*
1433          * lo->lo_state is set to Lo_unbound here after above partscan has
1434          * finished.
1435          *
1436          * There cannot be anybody else entering __loop_clr_fd() as
1437          * lo->lo_backing_file is already cleared and Lo_rundown state
1438          * protects us from all the other places trying to change the 'lo'
1439          * device.
1440          */
1441         mutex_lock(&lo->lo_mutex);
1442         lo->lo_flags = 0;
1443         if (!part_shift)
1444                 lo->lo_disk->flags |= GENHD_FL_NO_PART_SCAN;
1445         lo->lo_state = Lo_unbound;
1446         mutex_unlock(&lo->lo_mutex);
1447
1448         /*
1449          * Need not hold lo_mutex to fput backing file. Calling fput holding
1450          * lo_mutex triggers a circular lock dependency possibility warning as
1451          * fput can take open_mutex which is usually taken before lo_mutex.
1452          */
1453         if (filp)
1454                 fput(filp);
1455         return err;
1456 }
1457
1458 static int loop_clr_fd(struct loop_device *lo)
1459 {
1460         int err;
1461
1462         err = mutex_lock_killable(&lo->lo_mutex);
1463         if (err)
1464                 return err;
1465         if (lo->lo_state != Lo_bound) {
1466                 mutex_unlock(&lo->lo_mutex);
1467                 return -ENXIO;
1468         }
1469         /*
1470          * If we've explicitly asked to tear down the loop device,
1471          * and it has an elevated reference count, set it for auto-teardown when
1472          * the last reference goes away. This stops $!~#$@ udev from
1473          * preventing teardown because it decided that it needs to run blkid on
1474          * the loopback device whenever they appear. xfstests is notorious for
1475          * failing tests because blkid via udev races with a losetup
1476          * <dev>/do something like mkfs/losetup -d <dev> causing the losetup -d
1477          * command to fail with EBUSY.
1478          */
1479         if (atomic_read(&lo->lo_refcnt) > 1) {
1480                 lo->lo_flags |= LO_FLAGS_AUTOCLEAR;
1481                 mutex_unlock(&lo->lo_mutex);
1482                 return 0;
1483         }
1484         lo->lo_state = Lo_rundown;
1485         mutex_unlock(&lo->lo_mutex);
1486
1487         return __loop_clr_fd(lo, false);
1488 }
1489
1490 static int
1491 loop_set_status(struct loop_device *lo, const struct loop_info64 *info)
1492 {
1493         int err;
1494         kuid_t uid = current_uid();
1495         int prev_lo_flags;
1496         bool partscan = false;
1497         bool size_changed = false;
1498
1499         err = mutex_lock_killable(&lo->lo_mutex);
1500         if (err)
1501                 return err;
1502         if (lo->lo_encrypt_key_size &&
1503             !uid_eq(lo->lo_key_owner, uid) &&
1504             !capable(CAP_SYS_ADMIN)) {
1505                 err = -EPERM;
1506                 goto out_unlock;
1507         }
1508         if (lo->lo_state != Lo_bound) {
1509                 err = -ENXIO;
1510                 goto out_unlock;
1511         }
1512
1513         if (lo->lo_offset != info->lo_offset ||
1514             lo->lo_sizelimit != info->lo_sizelimit) {
1515                 size_changed = true;
1516                 sync_blockdev(lo->lo_device);
1517                 invalidate_bdev(lo->lo_device);
1518         }
1519
1520         /* I/O need to be drained during transfer transition */
1521         blk_mq_freeze_queue(lo->lo_queue);
1522
1523         if (size_changed && lo->lo_device->bd_inode->i_mapping->nrpages) {
1524                 /* If any pages were dirtied after invalidate_bdev(), try again */
1525                 err = -EAGAIN;
1526                 pr_warn("%s: loop%d (%s) has still dirty pages (nrpages=%lu)\n",
1527                         __func__, lo->lo_number, lo->lo_file_name,
1528                         lo->lo_device->bd_inode->i_mapping->nrpages);
1529                 goto out_unfreeze;
1530         }
1531
1532         prev_lo_flags = lo->lo_flags;
1533
1534         err = loop_set_status_from_info(lo, info);
1535         if (err)
1536                 goto out_unfreeze;
1537
1538         /* Mask out flags that can't be set using LOOP_SET_STATUS. */
1539         lo->lo_flags &= LOOP_SET_STATUS_SETTABLE_FLAGS;
1540         /* For those flags, use the previous values instead */
1541         lo->lo_flags |= prev_lo_flags & ~LOOP_SET_STATUS_SETTABLE_FLAGS;
1542         /* For flags that can't be cleared, use previous values too */
1543         lo->lo_flags |= prev_lo_flags & ~LOOP_SET_STATUS_CLEARABLE_FLAGS;
1544
1545         if (size_changed) {
1546                 loff_t new_size = get_size(lo->lo_offset, lo->lo_sizelimit,
1547                                            lo->lo_backing_file);
1548                 loop_set_size(lo, new_size);
1549         }
1550
1551         loop_config_discard(lo);
1552
1553         /* update dio if lo_offset or transfer is changed */
1554         __loop_update_dio(lo, lo->use_dio);
1555
1556 out_unfreeze:
1557         blk_mq_unfreeze_queue(lo->lo_queue);
1558
1559         if (!err && (lo->lo_flags & LO_FLAGS_PARTSCAN) &&
1560              !(prev_lo_flags & LO_FLAGS_PARTSCAN)) {
1561                 lo->lo_disk->flags &= ~GENHD_FL_NO_PART_SCAN;
1562                 partscan = true;
1563         }
1564 out_unlock:
1565         mutex_unlock(&lo->lo_mutex);
1566         if (partscan)
1567                 loop_reread_partitions(lo);
1568
1569         return err;
1570 }
1571
1572 static int
1573 loop_get_status(struct loop_device *lo, struct loop_info64 *info)
1574 {
1575         struct path path;
1576         struct kstat stat;
1577         int ret;
1578
1579         ret = mutex_lock_killable(&lo->lo_mutex);
1580         if (ret)
1581                 return ret;
1582         if (lo->lo_state != Lo_bound) {
1583                 mutex_unlock(&lo->lo_mutex);
1584                 return -ENXIO;
1585         }
1586
1587         memset(info, 0, sizeof(*info));
1588         info->lo_number = lo->lo_number;
1589         info->lo_offset = lo->lo_offset;
1590         info->lo_sizelimit = lo->lo_sizelimit;
1591         info->lo_flags = lo->lo_flags;
1592         memcpy(info->lo_file_name, lo->lo_file_name, LO_NAME_SIZE);
1593         memcpy(info->lo_crypt_name, lo->lo_crypt_name, LO_NAME_SIZE);
1594         info->lo_encrypt_type =
1595                 lo->lo_encryption ? lo->lo_encryption->number : 0;
1596         if (lo->lo_encrypt_key_size && capable(CAP_SYS_ADMIN)) {
1597                 info->lo_encrypt_key_size = lo->lo_encrypt_key_size;
1598                 memcpy(info->lo_encrypt_key, lo->lo_encrypt_key,
1599                        lo->lo_encrypt_key_size);
1600         }
1601
1602         /* Drop lo_mutex while we call into the filesystem. */
1603         path = lo->lo_backing_file->f_path;
1604         path_get(&path);
1605         mutex_unlock(&lo->lo_mutex);
1606         ret = vfs_getattr(&path, &stat, STATX_INO, AT_STATX_SYNC_AS_STAT);
1607         if (!ret) {
1608                 info->lo_device = huge_encode_dev(stat.dev);
1609                 info->lo_inode = stat.ino;
1610                 info->lo_rdevice = huge_encode_dev(stat.rdev);
1611         }
1612         path_put(&path);
1613         return ret;
1614 }
1615
1616 static void
1617 loop_info64_from_old(const struct loop_info *info, struct loop_info64 *info64)
1618 {
1619         memset(info64, 0, sizeof(*info64));
1620         info64->lo_number = info->lo_number;
1621         info64->lo_device = info->lo_device;
1622         info64->lo_inode = info->lo_inode;
1623         info64->lo_rdevice = info->lo_rdevice;
1624         info64->lo_offset = info->lo_offset;
1625         info64->lo_sizelimit = 0;
1626         info64->lo_encrypt_type = info->lo_encrypt_type;
1627         info64->lo_encrypt_key_size = info->lo_encrypt_key_size;
1628         info64->lo_flags = info->lo_flags;
1629         info64->lo_init[0] = info->lo_init[0];
1630         info64->lo_init[1] = info->lo_init[1];
1631         if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
1632                 memcpy(info64->lo_crypt_name, info->lo_name, LO_NAME_SIZE);
1633         else
1634                 memcpy(info64->lo_file_name, info->lo_name, LO_NAME_SIZE);
1635         memcpy(info64->lo_encrypt_key, info->lo_encrypt_key, LO_KEY_SIZE);
1636 }
1637
1638 static int
1639 loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info)
1640 {
1641         memset(info, 0, sizeof(*info));
1642         info->lo_number = info64->lo_number;
1643         info->lo_device = info64->lo_device;
1644         info->lo_inode = info64->lo_inode;
1645         info->lo_rdevice = info64->lo_rdevice;
1646         info->lo_offset = info64->lo_offset;
1647         info->lo_encrypt_type = info64->lo_encrypt_type;
1648         info->lo_encrypt_key_size = info64->lo_encrypt_key_size;
1649         info->lo_flags = info64->lo_flags;
1650         info->lo_init[0] = info64->lo_init[0];
1651         info->lo_init[1] = info64->lo_init[1];
1652         if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
1653                 memcpy(info->lo_name, info64->lo_crypt_name, LO_NAME_SIZE);
1654         else
1655                 memcpy(info->lo_name, info64->lo_file_name, LO_NAME_SIZE);
1656         memcpy(info->lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE);
1657
1658         /* error in case values were truncated */
1659         if (info->lo_device != info64->lo_device ||
1660             info->lo_rdevice != info64->lo_rdevice ||
1661             info->lo_inode != info64->lo_inode ||
1662             info->lo_offset != info64->lo_offset)
1663                 return -EOVERFLOW;
1664
1665         return 0;
1666 }
1667
1668 static int
1669 loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg)
1670 {
1671         struct loop_info info;
1672         struct loop_info64 info64;
1673
1674         if (copy_from_user(&info, arg, sizeof (struct loop_info)))
1675                 return -EFAULT;
1676         loop_info64_from_old(&info, &info64);
1677         return loop_set_status(lo, &info64);
1678 }
1679
1680 static int
1681 loop_set_status64(struct loop_device *lo, const struct loop_info64 __user *arg)
1682 {
1683         struct loop_info64 info64;
1684
1685         if (copy_from_user(&info64, arg, sizeof (struct loop_info64)))
1686                 return -EFAULT;
1687         return loop_set_status(lo, &info64);
1688 }
1689
1690 static int
1691 loop_get_status_old(struct loop_device *lo, struct loop_info __user *arg) {
1692         struct loop_info info;
1693         struct loop_info64 info64;
1694         int err;
1695
1696         if (!arg)
1697                 return -EINVAL;
1698         err = loop_get_status(lo, &info64);
1699         if (!err)
1700                 err = loop_info64_to_old(&info64, &info);
1701         if (!err && copy_to_user(arg, &info, sizeof(info)))
1702                 err = -EFAULT;
1703
1704         return err;
1705 }
1706
1707 static int
1708 loop_get_status64(struct loop_device *lo, struct loop_info64 __user *arg) {
1709         struct loop_info64 info64;
1710         int err;
1711
1712         if (!arg)
1713                 return -EINVAL;
1714         err = loop_get_status(lo, &info64);
1715         if (!err && copy_to_user(arg, &info64, sizeof(info64)))
1716                 err = -EFAULT;
1717
1718         return err;
1719 }
1720
1721 static int loop_set_capacity(struct loop_device *lo)
1722 {
1723         loff_t size;
1724
1725         if (unlikely(lo->lo_state != Lo_bound))
1726                 return -ENXIO;
1727
1728         size = get_loop_size(lo, lo->lo_backing_file);
1729         loop_set_size(lo, size);
1730
1731         return 0;
1732 }
1733
1734 static int loop_set_dio(struct loop_device *lo, unsigned long arg)
1735 {
1736         int error = -ENXIO;
1737         if (lo->lo_state != Lo_bound)
1738                 goto out;
1739
1740         __loop_update_dio(lo, !!arg);
1741         if (lo->use_dio == !!arg)
1742                 return 0;
1743         error = -EINVAL;
1744  out:
1745         return error;
1746 }
1747
1748 static int loop_set_block_size(struct loop_device *lo, unsigned long arg)
1749 {
1750         int err = 0;
1751
1752         if (lo->lo_state != Lo_bound)
1753                 return -ENXIO;
1754
1755         err = blk_validate_block_size(arg);
1756         if (err)
1757                 return err;
1758
1759         if (lo->lo_queue->limits.logical_block_size == arg)
1760                 return 0;
1761
1762         sync_blockdev(lo->lo_device);
1763         invalidate_bdev(lo->lo_device);
1764
1765         blk_mq_freeze_queue(lo->lo_queue);
1766
1767         /* invalidate_bdev should have truncated all the pages */
1768         if (lo->lo_device->bd_inode->i_mapping->nrpages) {
1769                 err = -EAGAIN;
1770                 pr_warn("%s: loop%d (%s) has still dirty pages (nrpages=%lu)\n",
1771                         __func__, lo->lo_number, lo->lo_file_name,
1772                         lo->lo_device->bd_inode->i_mapping->nrpages);
1773                 goto out_unfreeze;
1774         }
1775
1776         blk_queue_logical_block_size(lo->lo_queue, arg);
1777         blk_queue_physical_block_size(lo->lo_queue, arg);
1778         blk_queue_io_min(lo->lo_queue, arg);
1779         loop_update_dio(lo);
1780 out_unfreeze:
1781         blk_mq_unfreeze_queue(lo->lo_queue);
1782
1783         return err;
1784 }
1785
1786 static int lo_simple_ioctl(struct loop_device *lo, unsigned int cmd,
1787                            unsigned long arg)
1788 {
1789         int err;
1790
1791         err = mutex_lock_killable(&lo->lo_mutex);
1792         if (err)
1793                 return err;
1794         switch (cmd) {
1795         case LOOP_SET_CAPACITY:
1796                 err = loop_set_capacity(lo);
1797                 break;
1798         case LOOP_SET_DIRECT_IO:
1799                 err = loop_set_dio(lo, arg);
1800                 break;
1801         case LOOP_SET_BLOCK_SIZE:
1802                 err = loop_set_block_size(lo, arg);
1803                 break;
1804         default:
1805                 err = lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL;
1806         }
1807         mutex_unlock(&lo->lo_mutex);
1808         return err;
1809 }
1810
1811 static int lo_ioctl(struct block_device *bdev, fmode_t mode,
1812         unsigned int cmd, unsigned long arg)
1813 {
1814         struct loop_device *lo = bdev->bd_disk->private_data;
1815         void __user *argp = (void __user *) arg;
1816         int err;
1817
1818         switch (cmd) {
1819         case LOOP_SET_FD: {
1820                 /*
1821                  * Legacy case - pass in a zeroed out struct loop_config with
1822                  * only the file descriptor set , which corresponds with the
1823                  * default parameters we'd have used otherwise.
1824                  */
1825                 struct loop_config config;
1826
1827                 memset(&config, 0, sizeof(config));
1828                 config.fd = arg;
1829
1830                 return loop_configure(lo, mode, bdev, &config);
1831         }
1832         case LOOP_CONFIGURE: {
1833                 struct loop_config config;
1834
1835                 if (copy_from_user(&config, argp, sizeof(config)))
1836                         return -EFAULT;
1837
1838                 return loop_configure(lo, mode, bdev, &config);
1839         }
1840         case LOOP_CHANGE_FD:
1841                 return loop_change_fd(lo, bdev, arg);
1842         case LOOP_CLR_FD:
1843                 return loop_clr_fd(lo);
1844         case LOOP_SET_STATUS:
1845                 err = -EPERM;
1846                 if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) {
1847                         err = loop_set_status_old(lo, argp);
1848                 }
1849                 break;
1850         case LOOP_GET_STATUS:
1851                 return loop_get_status_old(lo, argp);
1852         case LOOP_SET_STATUS64:
1853                 err = -EPERM;
1854                 if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) {
1855                         err = loop_set_status64(lo, argp);
1856                 }
1857                 break;
1858         case LOOP_GET_STATUS64:
1859                 return loop_get_status64(lo, argp);
1860         case LOOP_SET_CAPACITY:
1861         case LOOP_SET_DIRECT_IO:
1862         case LOOP_SET_BLOCK_SIZE:
1863                 if (!(mode & FMODE_WRITE) && !capable(CAP_SYS_ADMIN))
1864                         return -EPERM;
1865                 fallthrough;
1866         default:
1867                 err = lo_simple_ioctl(lo, cmd, arg);
1868                 break;
1869         }
1870
1871         return err;
1872 }
1873
1874 #ifdef CONFIG_COMPAT
1875 struct compat_loop_info {
1876         compat_int_t    lo_number;      /* ioctl r/o */
1877         compat_dev_t    lo_device;      /* ioctl r/o */
1878         compat_ulong_t  lo_inode;       /* ioctl r/o */
1879         compat_dev_t    lo_rdevice;     /* ioctl r/o */
1880         compat_int_t    lo_offset;
1881         compat_int_t    lo_encrypt_type;
1882         compat_int_t    lo_encrypt_key_size;    /* ioctl w/o */
1883         compat_int_t    lo_flags;       /* ioctl r/o */
1884         char            lo_name[LO_NAME_SIZE];
1885         unsigned char   lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */
1886         compat_ulong_t  lo_init[2];
1887         char            reserved[4];
1888 };
1889
1890 /*
1891  * Transfer 32-bit compatibility structure in userspace to 64-bit loop info
1892  * - noinlined to reduce stack space usage in main part of driver
1893  */
1894 static noinline int
1895 loop_info64_from_compat(const struct compat_loop_info __user *arg,
1896                         struct loop_info64 *info64)
1897 {
1898         struct compat_loop_info info;
1899
1900         if (copy_from_user(&info, arg, sizeof(info)))
1901                 return -EFAULT;
1902
1903         memset(info64, 0, sizeof(*info64));
1904         info64->lo_number = info.lo_number;
1905         info64->lo_device = info.lo_device;
1906         info64->lo_inode = info.lo_inode;
1907         info64->lo_rdevice = info.lo_rdevice;
1908         info64->lo_offset = info.lo_offset;
1909         info64->lo_sizelimit = 0;
1910         info64->lo_encrypt_type = info.lo_encrypt_type;
1911         info64->lo_encrypt_key_size = info.lo_encrypt_key_size;
1912         info64->lo_flags = info.lo_flags;
1913         info64->lo_init[0] = info.lo_init[0];
1914         info64->lo_init[1] = info.lo_init[1];
1915         if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
1916                 memcpy(info64->lo_crypt_name, info.lo_name, LO_NAME_SIZE);
1917         else
1918                 memcpy(info64->lo_file_name, info.lo_name, LO_NAME_SIZE);
1919         memcpy(info64->lo_encrypt_key, info.lo_encrypt_key, LO_KEY_SIZE);
1920         return 0;
1921 }
1922
1923 /*
1924  * Transfer 64-bit loop info to 32-bit compatibility structure in userspace
1925  * - noinlined to reduce stack space usage in main part of driver
1926  */
1927 static noinline int
1928 loop_info64_to_compat(const struct loop_info64 *info64,
1929                       struct compat_loop_info __user *arg)
1930 {
1931         struct compat_loop_info info;
1932
1933         memset(&info, 0, sizeof(info));
1934         info.lo_number = info64->lo_number;
1935         info.lo_device = info64->lo_device;
1936         info.lo_inode = info64->lo_inode;
1937         info.lo_rdevice = info64->lo_rdevice;
1938         info.lo_offset = info64->lo_offset;
1939         info.lo_encrypt_type = info64->lo_encrypt_type;
1940         info.lo_encrypt_key_size = info64->lo_encrypt_key_size;
1941         info.lo_flags = info64->lo_flags;
1942         info.lo_init[0] = info64->lo_init[0];
1943         info.lo_init[1] = info64->lo_init[1];
1944         if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
1945                 memcpy(info.lo_name, info64->lo_crypt_name, LO_NAME_SIZE);
1946         else
1947                 memcpy(info.lo_name, info64->lo_file_name, LO_NAME_SIZE);
1948         memcpy(info.lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE);
1949
1950         /* error in case values were truncated */
1951         if (info.lo_device != info64->lo_device ||
1952             info.lo_rdevice != info64->lo_rdevice ||
1953             info.lo_inode != info64->lo_inode ||
1954             info.lo_offset != info64->lo_offset ||
1955             info.lo_init[0] != info64->lo_init[0] ||
1956             info.lo_init[1] != info64->lo_init[1])
1957                 return -EOVERFLOW;
1958
1959         if (copy_to_user(arg, &info, sizeof(info)))
1960                 return -EFAULT;
1961         return 0;
1962 }
1963
1964 static int
1965 loop_set_status_compat(struct loop_device *lo,
1966                        const struct compat_loop_info __user *arg)
1967 {
1968         struct loop_info64 info64;
1969         int ret;
1970
1971         ret = loop_info64_from_compat(arg, &info64);
1972         if (ret < 0)
1973                 return ret;
1974         return loop_set_status(lo, &info64);
1975 }
1976
1977 static int
1978 loop_get_status_compat(struct loop_device *lo,
1979                        struct compat_loop_info __user *arg)
1980 {
1981         struct loop_info64 info64;
1982         int err;
1983
1984         if (!arg)
1985                 return -EINVAL;
1986         err = loop_get_status(lo, &info64);
1987         if (!err)
1988                 err = loop_info64_to_compat(&info64, arg);
1989         return err;
1990 }
1991
1992 static int lo_compat_ioctl(struct block_device *bdev, fmode_t mode,
1993                            unsigned int cmd, unsigned long arg)
1994 {
1995         struct loop_device *lo = bdev->bd_disk->private_data;
1996         int err;
1997
1998         switch(cmd) {
1999         case LOOP_SET_STATUS:
2000                 err = loop_set_status_compat(lo,
2001                              (const struct compat_loop_info __user *)arg);
2002                 break;
2003         case LOOP_GET_STATUS:
2004                 err = loop_get_status_compat(lo,
2005                                      (struct compat_loop_info __user *)arg);
2006                 break;
2007         case LOOP_SET_CAPACITY:
2008         case LOOP_CLR_FD:
2009         case LOOP_GET_STATUS64:
2010         case LOOP_SET_STATUS64:
2011         case LOOP_CONFIGURE:
2012                 arg = (unsigned long) compat_ptr(arg);
2013                 fallthrough;
2014         case LOOP_SET_FD:
2015         case LOOP_CHANGE_FD:
2016         case LOOP_SET_BLOCK_SIZE:
2017         case LOOP_SET_DIRECT_IO:
2018                 err = lo_ioctl(bdev, mode, cmd, arg);
2019                 break;
2020         default:
2021                 err = -ENOIOCTLCMD;
2022                 break;
2023         }
2024         return err;
2025 }
2026 #endif
2027
2028 static int lo_open(struct block_device *bdev, fmode_t mode)
2029 {
2030         struct loop_device *lo = bdev->bd_disk->private_data;
2031         int err;
2032
2033         err = mutex_lock_killable(&lo->lo_mutex);
2034         if (err)
2035                 return err;
2036         if (lo->lo_state == Lo_deleting)
2037                 err = -ENXIO;
2038         else
2039                 atomic_inc(&lo->lo_refcnt);
2040         mutex_unlock(&lo->lo_mutex);
2041         return err;
2042 }
2043
2044 static void lo_release(struct gendisk *disk, fmode_t mode)
2045 {
2046         struct loop_device *lo = disk->private_data;
2047
2048         mutex_lock(&lo->lo_mutex);
2049         if (atomic_dec_return(&lo->lo_refcnt))
2050                 goto out_unlock;
2051
2052         if (lo->lo_flags & LO_FLAGS_AUTOCLEAR) {
2053                 if (lo->lo_state != Lo_bound)
2054                         goto out_unlock;
2055                 lo->lo_state = Lo_rundown;
2056                 mutex_unlock(&lo->lo_mutex);
2057                 /*
2058                  * In autoclear mode, stop the loop thread
2059                  * and remove configuration after last close.
2060                  */
2061                 __loop_clr_fd(lo, true);
2062                 return;
2063         } else if (lo->lo_state == Lo_bound) {
2064                 /*
2065                  * Otherwise keep thread (if running) and config,
2066                  * but flush possible ongoing bios in thread.
2067                  */
2068                 blk_mq_freeze_queue(lo->lo_queue);
2069                 blk_mq_unfreeze_queue(lo->lo_queue);
2070         }
2071
2072 out_unlock:
2073         mutex_unlock(&lo->lo_mutex);
2074 }
2075
2076 static const struct block_device_operations lo_fops = {
2077         .owner =        THIS_MODULE,
2078         .open =         lo_open,
2079         .release =      lo_release,
2080         .ioctl =        lo_ioctl,
2081 #ifdef CONFIG_COMPAT
2082         .compat_ioctl = lo_compat_ioctl,
2083 #endif
2084 };
2085
2086 /*
2087  * And now the modules code and kernel interface.
2088  */
2089 static int max_loop;
2090 module_param(max_loop, int, 0444);
2091 MODULE_PARM_DESC(max_loop, "Maximum number of loop devices");
2092 module_param(max_part, int, 0444);
2093 MODULE_PARM_DESC(max_part, "Maximum number of partitions per loop device");
2094 MODULE_LICENSE("GPL");
2095 MODULE_ALIAS_BLOCKDEV_MAJOR(LOOP_MAJOR);
2096
2097 int loop_register_transfer(struct loop_func_table *funcs)
2098 {
2099         unsigned int n = funcs->number;
2100
2101         if (n >= MAX_LO_CRYPT || xfer_funcs[n])
2102                 return -EINVAL;
2103         xfer_funcs[n] = funcs;
2104         return 0;
2105 }
2106
2107 int loop_unregister_transfer(int number)
2108 {
2109         unsigned int n = number;
2110         struct loop_func_table *xfer;
2111
2112         if (n == 0 || n >= MAX_LO_CRYPT || (xfer = xfer_funcs[n]) == NULL)
2113                 return -EINVAL;
2114         /*
2115          * This function is called from only cleanup_cryptoloop().
2116          * Given that each loop device that has a transfer enabled holds a
2117          * reference to the module implementing it we should never get here
2118          * with a transfer that is set (unless forced module unloading is
2119          * requested). Thus, check module's refcount and warn if this is
2120          * not a clean unloading.
2121          */
2122 #ifdef CONFIG_MODULE_UNLOAD
2123         if (xfer->owner && module_refcount(xfer->owner) != -1)
2124                 pr_err("Danger! Unregistering an in use transfer function.\n");
2125 #endif
2126
2127         xfer_funcs[n] = NULL;
2128         return 0;
2129 }
2130
2131 EXPORT_SYMBOL(loop_register_transfer);
2132 EXPORT_SYMBOL(loop_unregister_transfer);
2133
2134 static blk_status_t loop_queue_rq(struct blk_mq_hw_ctx *hctx,
2135                 const struct blk_mq_queue_data *bd)
2136 {
2137         struct request *rq = bd->rq;
2138         struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq);
2139         struct loop_device *lo = rq->q->queuedata;
2140
2141         blk_mq_start_request(rq);
2142
2143         if (lo->lo_state != Lo_bound)
2144                 return BLK_STS_IOERR;
2145
2146         switch (req_op(rq)) {
2147         case REQ_OP_FLUSH:
2148         case REQ_OP_DISCARD:
2149         case REQ_OP_WRITE_ZEROES:
2150                 cmd->use_aio = false;
2151                 break;
2152         default:
2153                 cmd->use_aio = lo->use_dio;
2154                 break;
2155         }
2156
2157         /* always use the first bio's css */
2158         cmd->blkcg_css = NULL;
2159         cmd->memcg_css = NULL;
2160 #ifdef CONFIG_BLK_CGROUP
2161         if (rq->bio && rq->bio->bi_blkg) {
2162                 cmd->blkcg_css = &bio_blkcg(rq->bio)->css;
2163 #ifdef CONFIG_MEMCG
2164                 cmd->memcg_css =
2165                         cgroup_get_e_css(cmd->blkcg_css->cgroup,
2166                                         &memory_cgrp_subsys);
2167 #endif
2168         }
2169 #endif
2170         loop_queue_work(lo, cmd);
2171
2172         return BLK_STS_OK;
2173 }
2174
2175 static void loop_handle_cmd(struct loop_cmd *cmd)
2176 {
2177         struct request *rq = blk_mq_rq_from_pdu(cmd);
2178         const bool write = op_is_write(req_op(rq));
2179         struct loop_device *lo = rq->q->queuedata;
2180         int ret = 0;
2181         struct mem_cgroup *old_memcg = NULL;
2182
2183         if (write && (lo->lo_flags & LO_FLAGS_READ_ONLY)) {
2184                 ret = -EIO;
2185                 goto failed;
2186         }
2187
2188         if (cmd->blkcg_css)
2189                 kthread_associate_blkcg(cmd->blkcg_css);
2190         if (cmd->memcg_css)
2191                 old_memcg = set_active_memcg(
2192                         mem_cgroup_from_css(cmd->memcg_css));
2193
2194         ret = do_req_filebacked(lo, rq);
2195
2196         if (cmd->blkcg_css)
2197                 kthread_associate_blkcg(NULL);
2198
2199         if (cmd->memcg_css) {
2200                 set_active_memcg(old_memcg);
2201                 css_put(cmd->memcg_css);
2202         }
2203  failed:
2204         /* complete non-aio request */
2205         if (!cmd->use_aio || ret) {
2206                 if (ret == -EOPNOTSUPP)
2207                         cmd->ret = ret;
2208                 else
2209                         cmd->ret = ret ? -EIO : 0;
2210                 if (likely(!blk_should_fake_timeout(rq->q)))
2211                         blk_mq_complete_request(rq);
2212         }
2213 }
2214
2215 static void loop_set_timer(struct loop_device *lo)
2216 {
2217         timer_reduce(&lo->timer, jiffies + LOOP_IDLE_WORKER_TIMEOUT);
2218 }
2219
2220 static void loop_process_work(struct loop_worker *worker,
2221                         struct list_head *cmd_list, struct loop_device *lo)
2222 {
2223         int orig_flags = current->flags;
2224         struct loop_cmd *cmd;
2225
2226         current->flags |= PF_LOCAL_THROTTLE | PF_MEMALLOC_NOIO;
2227         spin_lock_irq(&lo->lo_work_lock);
2228         while (!list_empty(cmd_list)) {
2229                 cmd = container_of(
2230                         cmd_list->next, struct loop_cmd, list_entry);
2231                 list_del(cmd_list->next);
2232                 spin_unlock_irq(&lo->lo_work_lock);
2233
2234                 loop_handle_cmd(cmd);
2235                 cond_resched();
2236
2237                 spin_lock_irq(&lo->lo_work_lock);
2238         }
2239
2240         /*
2241          * We only add to the idle list if there are no pending cmds
2242          * *and* the worker will not run again which ensures that it
2243          * is safe to free any worker on the idle list
2244          */
2245         if (worker && !work_pending(&worker->work)) {
2246                 worker->last_ran_at = jiffies;
2247                 list_add_tail(&worker->idle_list, &lo->idle_worker_list);
2248                 loop_set_timer(lo);
2249         }
2250         spin_unlock_irq(&lo->lo_work_lock);
2251         current->flags = orig_flags;
2252 }
2253
2254 static void loop_workfn(struct work_struct *work)
2255 {
2256         struct loop_worker *worker =
2257                 container_of(work, struct loop_worker, work);
2258         loop_process_work(worker, &worker->cmd_list, worker->lo);
2259 }
2260
2261 static void loop_rootcg_workfn(struct work_struct *work)
2262 {
2263         struct loop_device *lo =
2264                 container_of(work, struct loop_device, rootcg_work);
2265         loop_process_work(NULL, &lo->rootcg_cmd_list, lo);
2266 }
2267
2268 static void loop_free_idle_workers(struct timer_list *timer)
2269 {
2270         struct loop_device *lo = container_of(timer, struct loop_device, timer);
2271         struct loop_worker *pos, *worker;
2272
2273         spin_lock_irq(&lo->lo_work_lock);
2274         list_for_each_entry_safe(worker, pos, &lo->idle_worker_list,
2275                                 idle_list) {
2276                 if (time_is_after_jiffies(worker->last_ran_at +
2277                                                 LOOP_IDLE_WORKER_TIMEOUT))
2278                         break;
2279                 list_del(&worker->idle_list);
2280                 rb_erase(&worker->rb_node, &lo->worker_tree);
2281                 css_put(worker->blkcg_css);
2282                 kfree(worker);
2283         }
2284         if (!list_empty(&lo->idle_worker_list))
2285                 loop_set_timer(lo);
2286         spin_unlock_irq(&lo->lo_work_lock);
2287 }
2288
2289 static const struct blk_mq_ops loop_mq_ops = {
2290         .queue_rq       = loop_queue_rq,
2291         .complete       = lo_complete_rq,
2292 };
2293
2294 static int loop_add(int i)
2295 {
2296         struct loop_device *lo;
2297         struct gendisk *disk;
2298         int err;
2299
2300         err = -ENOMEM;
2301         lo = kzalloc(sizeof(*lo), GFP_KERNEL);
2302         if (!lo)
2303                 goto out;
2304         lo->lo_state = Lo_unbound;
2305
2306         err = mutex_lock_killable(&loop_ctl_mutex);
2307         if (err)
2308                 goto out_free_dev;
2309
2310         /* allocate id, if @id >= 0, we're requesting that specific id */
2311         if (i >= 0) {
2312                 err = idr_alloc(&loop_index_idr, lo, i, i + 1, GFP_KERNEL);
2313                 if (err == -ENOSPC)
2314                         err = -EEXIST;
2315         } else {
2316                 err = idr_alloc(&loop_index_idr, lo, 0, 0, GFP_KERNEL);
2317         }
2318         mutex_unlock(&loop_ctl_mutex);
2319         if (err < 0)
2320                 goto out_free_dev;
2321         i = err;
2322
2323         err = -ENOMEM;
2324         lo->tag_set.ops = &loop_mq_ops;
2325         lo->tag_set.nr_hw_queues = 1;
2326         lo->tag_set.queue_depth = 128;
2327         lo->tag_set.numa_node = NUMA_NO_NODE;
2328         lo->tag_set.cmd_size = sizeof(struct loop_cmd);
2329         lo->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_STACKING |
2330                 BLK_MQ_F_NO_SCHED_BY_DEFAULT;
2331         lo->tag_set.driver_data = lo;
2332
2333         err = blk_mq_alloc_tag_set(&lo->tag_set);
2334         if (err)
2335                 goto out_free_idr;
2336
2337         disk = lo->lo_disk = blk_mq_alloc_disk(&lo->tag_set, lo);
2338         if (IS_ERR(disk)) {
2339                 err = PTR_ERR(disk);
2340                 goto out_cleanup_tags;
2341         }
2342         lo->lo_queue = lo->lo_disk->queue;
2343
2344         blk_queue_max_hw_sectors(lo->lo_queue, BLK_DEF_MAX_SECTORS);
2345
2346         /*
2347          * By default, we do buffer IO, so it doesn't make sense to enable
2348          * merge because the I/O submitted to backing file is handled page by
2349          * page. For directio mode, merge does help to dispatch bigger request
2350          * to underlayer disk. We will enable merge once directio is enabled.
2351          */
2352         blk_queue_flag_set(QUEUE_FLAG_NOMERGES, lo->lo_queue);
2353
2354         /*
2355          * Disable partition scanning by default. The in-kernel partition
2356          * scanning can be requested individually per-device during its
2357          * setup. Userspace can always add and remove partitions from all
2358          * devices. The needed partition minors are allocated from the
2359          * extended minor space, the main loop device numbers will continue
2360          * to match the loop minors, regardless of the number of partitions
2361          * used.
2362          *
2363          * If max_part is given, partition scanning is globally enabled for
2364          * all loop devices. The minors for the main loop devices will be
2365          * multiples of max_part.
2366          *
2367          * Note: Global-for-all-devices, set-only-at-init, read-only module
2368          * parameteters like 'max_loop' and 'max_part' make things needlessly
2369          * complicated, are too static, inflexible and may surprise
2370          * userspace tools. Parameters like this in general should be avoided.
2371          */
2372         if (!part_shift)
2373                 disk->flags |= GENHD_FL_NO_PART_SCAN;
2374         disk->flags |= GENHD_FL_EXT_DEVT;
2375         atomic_set(&lo->lo_refcnt, 0);
2376         mutex_init(&lo->lo_mutex);
2377         lo->lo_number           = i;
2378         spin_lock_init(&lo->lo_lock);
2379         spin_lock_init(&lo->lo_work_lock);
2380         disk->major             = LOOP_MAJOR;
2381         disk->first_minor       = i << part_shift;
2382         disk->minors            = 1 << part_shift;
2383         disk->fops              = &lo_fops;
2384         disk->private_data      = lo;
2385         disk->queue             = lo->lo_queue;
2386         disk->events            = DISK_EVENT_MEDIA_CHANGE;
2387         disk->event_flags       = DISK_EVENT_FLAG_UEVENT;
2388         sprintf(disk->disk_name, "loop%d", i);
2389         /* Make this loop device reachable from pathname. */
2390         add_disk(disk);
2391         /* Show this loop device. */
2392         mutex_lock(&loop_ctl_mutex);
2393         lo->idr_visible = true;
2394         mutex_unlock(&loop_ctl_mutex);
2395         return i;
2396
2397 out_cleanup_tags:
2398         blk_mq_free_tag_set(&lo->tag_set);
2399 out_free_idr:
2400         mutex_lock(&loop_ctl_mutex);
2401         idr_remove(&loop_index_idr, i);
2402         mutex_unlock(&loop_ctl_mutex);
2403 out_free_dev:
2404         kfree(lo);
2405 out:
2406         return err;
2407 }
2408
2409 static void loop_remove(struct loop_device *lo)
2410 {
2411         /* Make this loop device unreachable from pathname. */
2412         del_gendisk(lo->lo_disk);
2413         blk_cleanup_disk(lo->lo_disk);
2414         blk_mq_free_tag_set(&lo->tag_set);
2415         mutex_lock(&loop_ctl_mutex);
2416         idr_remove(&loop_index_idr, lo->lo_number);
2417         mutex_unlock(&loop_ctl_mutex);
2418         /* There is no route which can find this loop device. */
2419         mutex_destroy(&lo->lo_mutex);
2420         kfree(lo);
2421 }
2422
2423 static void loop_probe(dev_t dev)
2424 {
2425         int idx = MINOR(dev) >> part_shift;
2426
2427         if (max_loop && idx >= max_loop)
2428                 return;
2429         loop_add(idx);
2430 }
2431
2432 static int loop_control_remove(int idx)
2433 {
2434         struct loop_device *lo;
2435         int ret;
2436
2437         if (idx < 0) {
2438                 pr_warn_once("deleting an unspecified loop device is not supported.\n");
2439                 return -EINVAL;
2440         }
2441                 
2442         /* Hide this loop device for serialization. */
2443         ret = mutex_lock_killable(&loop_ctl_mutex);
2444         if (ret)
2445                 return ret;
2446         lo = idr_find(&loop_index_idr, idx);
2447         if (!lo || !lo->idr_visible)
2448                 ret = -ENODEV;
2449         else
2450                 lo->idr_visible = false;
2451         mutex_unlock(&loop_ctl_mutex);
2452         if (ret)
2453                 return ret;
2454
2455         /* Check whether this loop device can be removed. */
2456         ret = mutex_lock_killable(&lo->lo_mutex);
2457         if (ret)
2458                 goto mark_visible;
2459         if (lo->lo_state != Lo_unbound ||
2460             atomic_read(&lo->lo_refcnt) > 0) {
2461                 mutex_unlock(&lo->lo_mutex);
2462                 ret = -EBUSY;
2463                 goto mark_visible;
2464         }
2465         /* Mark this loop device no longer open()-able. */
2466         lo->lo_state = Lo_deleting;
2467         mutex_unlock(&lo->lo_mutex);
2468
2469         loop_remove(lo);
2470         return 0;
2471
2472 mark_visible:
2473         /* Show this loop device again. */
2474         mutex_lock(&loop_ctl_mutex);
2475         lo->idr_visible = true;
2476         mutex_unlock(&loop_ctl_mutex);
2477         return ret;
2478 }
2479
2480 static int loop_control_get_free(int idx)
2481 {
2482         struct loop_device *lo;
2483         int id, ret;
2484
2485         ret = mutex_lock_killable(&loop_ctl_mutex);
2486         if (ret)
2487                 return ret;
2488         idr_for_each_entry(&loop_index_idr, lo, id) {
2489                 /* Hitting a race results in creating a new loop device which is harmless. */
2490                 if (lo->idr_visible && data_race(lo->lo_state) == Lo_unbound)
2491                         goto found;
2492         }
2493         mutex_unlock(&loop_ctl_mutex);
2494         return loop_add(-1);
2495 found:
2496         mutex_unlock(&loop_ctl_mutex);
2497         return id;
2498 }
2499
2500 static long loop_control_ioctl(struct file *file, unsigned int cmd,
2501                                unsigned long parm)
2502 {
2503         switch (cmd) {
2504         case LOOP_CTL_ADD:
2505                 return loop_add(parm);
2506         case LOOP_CTL_REMOVE:
2507                 return loop_control_remove(parm);
2508         case LOOP_CTL_GET_FREE:
2509                 return loop_control_get_free(parm);
2510         default:
2511                 return -ENOSYS;
2512         }
2513 }
2514
2515 static const struct file_operations loop_ctl_fops = {
2516         .open           = nonseekable_open,
2517         .unlocked_ioctl = loop_control_ioctl,
2518         .compat_ioctl   = loop_control_ioctl,
2519         .owner          = THIS_MODULE,
2520         .llseek         = noop_llseek,
2521 };
2522
2523 static struct miscdevice loop_misc = {
2524         .minor          = LOOP_CTRL_MINOR,
2525         .name           = "loop-control",
2526         .fops           = &loop_ctl_fops,
2527 };
2528
2529 MODULE_ALIAS_MISCDEV(LOOP_CTRL_MINOR);
2530 MODULE_ALIAS("devname:loop-control");
2531
2532 static int __init loop_init(void)
2533 {
2534         int i, nr;
2535         int err;
2536
2537         part_shift = 0;
2538         if (max_part > 0) {
2539                 part_shift = fls(max_part);
2540
2541                 /*
2542                  * Adjust max_part according to part_shift as it is exported
2543                  * to user space so that user can decide correct minor number
2544                  * if [s]he want to create more devices.
2545                  *
2546                  * Note that -1 is required because partition 0 is reserved
2547                  * for the whole disk.
2548                  */
2549                 max_part = (1UL << part_shift) - 1;
2550         }
2551
2552         if ((1UL << part_shift) > DISK_MAX_PARTS) {
2553                 err = -EINVAL;
2554                 goto err_out;
2555         }
2556
2557         if (max_loop > 1UL << (MINORBITS - part_shift)) {
2558                 err = -EINVAL;
2559                 goto err_out;
2560         }
2561
2562         /*
2563          * If max_loop is specified, create that many devices upfront.
2564          * This also becomes a hard limit. If max_loop is not specified,
2565          * create CONFIG_BLK_DEV_LOOP_MIN_COUNT loop devices at module
2566          * init time. Loop devices can be requested on-demand with the
2567          * /dev/loop-control interface, or be instantiated by accessing
2568          * a 'dead' device node.
2569          */
2570         if (max_loop)
2571                 nr = max_loop;
2572         else
2573                 nr = CONFIG_BLK_DEV_LOOP_MIN_COUNT;
2574
2575         err = misc_register(&loop_misc);
2576         if (err < 0)
2577                 goto err_out;
2578
2579
2580         if (__register_blkdev(LOOP_MAJOR, "loop", loop_probe)) {
2581                 err = -EIO;
2582                 goto misc_out;
2583         }
2584
2585         /* pre-create number of devices given by config or max_loop */
2586         for (i = 0; i < nr; i++)
2587                 loop_add(i);
2588
2589         printk(KERN_INFO "loop: module loaded\n");
2590         return 0;
2591
2592 misc_out:
2593         misc_deregister(&loop_misc);
2594 err_out:
2595         return err;
2596 }
2597
2598 static void __exit loop_exit(void)
2599 {
2600         struct loop_device *lo;
2601         int id;
2602
2603         unregister_blkdev(LOOP_MAJOR, "loop");
2604         misc_deregister(&loop_misc);
2605
2606         /*
2607          * There is no need to use loop_ctl_mutex here, for nobody else can
2608          * access loop_index_idr when this module is unloading (unless forced
2609          * module unloading is requested). If this is not a clean unloading,
2610          * we have no means to avoid kernel crash.
2611          */
2612         idr_for_each_entry(&loop_index_idr, lo, id)
2613                 loop_remove(lo);
2614
2615         idr_destroy(&loop_index_idr);
2616 }
2617
2618 module_init(loop_init);
2619 module_exit(loop_exit);
2620
2621 #ifndef MODULE
2622 static int __init max_loop_setup(char *str)
2623 {
2624         max_loop = simple_strtol(str, NULL, 0);
2625         return 1;
2626 }
2627
2628 __setup("max_loop=", max_loop_setup);
2629 #endif