loop.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348
  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 prepare_write and/or commit_write are not available on the
  44. * backing filesystem.
  45. * Anton Altaparmakov, 16 Feb 2005
  46. *
  47. * Still To Fix:
  48. * - Advisory locking is ignored here.
  49. * - Should use an own CAP_* category instead of CAP_SYS_ADMIN
  50. *
  51. */
  52. #include <linux/config.h>
  53. #include <linux/module.h>
  54. #include <linux/moduleparam.h>
  55. #include <linux/sched.h>
  56. #include <linux/fs.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/devfs_fs_kernel.h>
  66. #include <linux/smp_lock.h>
  67. #include <linux/swap.h>
  68. #include <linux/slab.h>
  69. #include <linux/loop.h>
  70. #include <linux/suspend.h>
  71. #include <linux/writeback.h>
  72. #include <linux/buffer_head.h> /* for invalidate_bdev() */
  73. #include <linux/completion.h>
  74. #include <linux/highmem.h>
  75. #include <linux/gfp.h>
  76. #include <asm/uaccess.h>
  77. static int max_loop = 8;
  78. static struct loop_device *loop_dev;
  79. static struct gendisk **disks;
  80. /*
  81. * Transfer functions
  82. */
  83. static int transfer_none(struct loop_device *lo, int cmd,
  84. struct page *raw_page, unsigned raw_off,
  85. struct page *loop_page, unsigned loop_off,
  86. int size, sector_t real_block)
  87. {
  88. char *raw_buf = kmap_atomic(raw_page, KM_USER0) + raw_off;
  89. char *loop_buf = kmap_atomic(loop_page, KM_USER1) + loop_off;
  90. if (cmd == READ)
  91. memcpy(loop_buf, raw_buf, size);
  92. else
  93. memcpy(raw_buf, loop_buf, size);
  94. kunmap_atomic(raw_buf, KM_USER0);
  95. kunmap_atomic(loop_buf, KM_USER1);
  96. cond_resched();
  97. return 0;
  98. }
  99. static int transfer_xor(struct loop_device *lo, int cmd,
  100. struct page *raw_page, unsigned raw_off,
  101. struct page *loop_page, unsigned loop_off,
  102. int size, sector_t real_block)
  103. {
  104. char *raw_buf = kmap_atomic(raw_page, KM_USER0) + raw_off;
  105. char *loop_buf = kmap_atomic(loop_page, KM_USER1) + loop_off;
  106. char *in, *out, *key;
  107. int i, keysize;
  108. if (cmd == READ) {
  109. in = raw_buf;
  110. out = loop_buf;
  111. } else {
  112. in = loop_buf;
  113. out = raw_buf;
  114. }
  115. key = lo->lo_encrypt_key;
  116. keysize = lo->lo_encrypt_key_size;
  117. for (i = 0; i < size; i++)
  118. *out++ = *in++ ^ key[(i & 511) % keysize];
  119. kunmap_atomic(raw_buf, KM_USER0);
  120. kunmap_atomic(loop_buf, KM_USER1);
  121. cond_resched();
  122. return 0;
  123. }
  124. static int xor_init(struct loop_device *lo, const struct loop_info64 *info)
  125. {
  126. if (unlikely(info->lo_encrypt_key_size <= 0))
  127. return -EINVAL;
  128. return 0;
  129. }
  130. static struct loop_func_table none_funcs = {
  131. .number = LO_CRYPT_NONE,
  132. .transfer = transfer_none,
  133. };
  134. static struct loop_func_table xor_funcs = {
  135. .number = LO_CRYPT_XOR,
  136. .transfer = transfer_xor,
  137. .init = xor_init
  138. };
  139. /* xfer_funcs[0] is special - its release function is never called */
  140. static struct loop_func_table *xfer_funcs[MAX_LO_CRYPT] = {
  141. &none_funcs,
  142. &xor_funcs
  143. };
  144. static loff_t get_loop_size(struct loop_device *lo, struct file *file)
  145. {
  146. loff_t size, offset, loopsize;
  147. /* Compute loopsize in bytes */
  148. size = i_size_read(file->f_mapping->host);
  149. offset = lo->lo_offset;
  150. loopsize = size - offset;
  151. if (lo->lo_sizelimit > 0 && lo->lo_sizelimit < loopsize)
  152. loopsize = lo->lo_sizelimit;
  153. /*
  154. * Unfortunately, if we want to do I/O on the device,
  155. * the number of 512-byte sectors has to fit into a sector_t.
  156. */
  157. return loopsize >> 9;
  158. }
  159. static int
  160. figure_loop_size(struct loop_device *lo)
  161. {
  162. loff_t size = get_loop_size(lo, lo->lo_backing_file);
  163. sector_t x = (sector_t)size;
  164. if (unlikely((loff_t)x != size))
  165. return -EFBIG;
  166. set_capacity(disks[lo->lo_number], x);
  167. return 0;
  168. }
  169. static inline int
  170. lo_do_transfer(struct loop_device *lo, int cmd,
  171. struct page *rpage, unsigned roffs,
  172. struct page *lpage, unsigned loffs,
  173. int size, sector_t rblock)
  174. {
  175. if (unlikely(!lo->transfer))
  176. return 0;
  177. return lo->transfer(lo, cmd, rpage, roffs, lpage, loffs, size, rblock);
  178. }
  179. /**
  180. * do_lo_send_aops - helper for writing data to a loop device
  181. *
  182. * This is the fast version for backing filesystems which implement the address
  183. * space operations prepare_write and commit_write.
  184. */
  185. static int do_lo_send_aops(struct loop_device *lo, struct bio_vec *bvec,
  186. int bsize, loff_t pos, struct page *page)
  187. {
  188. struct file *file = lo->lo_backing_file; /* kudos to NFsckingS */
  189. struct address_space *mapping = file->f_mapping;
  190. struct address_space_operations *aops = mapping->a_ops;
  191. pgoff_t index;
  192. unsigned offset, bv_offs;
  193. int len, ret = 0;
  194. down(&mapping->host->i_sem);
  195. index = pos >> PAGE_CACHE_SHIFT;
  196. offset = pos & ((pgoff_t)PAGE_CACHE_SIZE - 1);
  197. bv_offs = bvec->bv_offset;
  198. len = bvec->bv_len;
  199. while (len > 0) {
  200. sector_t IV;
  201. unsigned size;
  202. int transfer_result;
  203. IV = ((sector_t)index << (PAGE_CACHE_SHIFT - 9))+(offset >> 9);
  204. size = PAGE_CACHE_SIZE - offset;
  205. if (size > len)
  206. size = len;
  207. page = grab_cache_page(mapping, index);
  208. if (unlikely(!page))
  209. goto fail;
  210. if (unlikely(aops->prepare_write(file, page, offset,
  211. offset + size)))
  212. goto unlock;
  213. transfer_result = lo_do_transfer(lo, WRITE, page, offset,
  214. bvec->bv_page, bv_offs, size, IV);
  215. if (unlikely(transfer_result)) {
  216. char *kaddr;
  217. /*
  218. * The transfer failed, but we still write the data to
  219. * keep prepare/commit calls balanced.
  220. */
  221. printk(KERN_ERR "loop: transfer error block %llu\n",
  222. (unsigned long long)index);
  223. kaddr = kmap_atomic(page, KM_USER0);
  224. memset(kaddr + offset, 0, size);
  225. kunmap_atomic(kaddr, KM_USER0);
  226. }
  227. flush_dcache_page(page);
  228. if (unlikely(aops->commit_write(file, page, offset,
  229. offset + size)))
  230. goto unlock;
  231. if (unlikely(transfer_result))
  232. goto unlock;
  233. bv_offs += size;
  234. len -= size;
  235. offset = 0;
  236. index++;
  237. pos += size;
  238. unlock_page(page);
  239. page_cache_release(page);
  240. }
  241. out:
  242. up(&mapping->host->i_sem);
  243. return ret;
  244. unlock:
  245. unlock_page(page);
  246. page_cache_release(page);
  247. fail:
  248. ret = -1;
  249. goto out;
  250. }
  251. /**
  252. * __do_lo_send_write - helper for writing data to a loop device
  253. *
  254. * This helper just factors out common code between do_lo_send_direct_write()
  255. * and do_lo_send_write().
  256. */
  257. static inline int __do_lo_send_write(struct file *file,
  258. u8 __user *buf, const int len, loff_t pos)
  259. {
  260. ssize_t bw;
  261. mm_segment_t old_fs = get_fs();
  262. set_fs(get_ds());
  263. bw = file->f_op->write(file, buf, len, &pos);
  264. set_fs(old_fs);
  265. if (likely(bw == len))
  266. return 0;
  267. printk(KERN_ERR "loop: Write error at byte offset %llu, length %i.\n",
  268. (unsigned long long)pos, len);
  269. if (bw >= 0)
  270. bw = -EIO;
  271. return bw;
  272. }
  273. /**
  274. * do_lo_send_direct_write - helper for writing data to a loop device
  275. *
  276. * This is the fast, non-transforming version for backing filesystems which do
  277. * not implement the address space operations prepare_write and commit_write.
  278. * It uses the write file operation which should be present on all writeable
  279. * filesystems.
  280. */
  281. static int do_lo_send_direct_write(struct loop_device *lo,
  282. struct bio_vec *bvec, int bsize, loff_t pos, struct page *page)
  283. {
  284. ssize_t bw = __do_lo_send_write(lo->lo_backing_file,
  285. (u8 __user *)kmap(bvec->bv_page) + bvec->bv_offset,
  286. bvec->bv_len, pos);
  287. kunmap(bvec->bv_page);
  288. cond_resched();
  289. return bw;
  290. }
  291. /**
  292. * do_lo_send_write - helper for writing data to a loop device
  293. *
  294. * This is the slow, transforming version for filesystems which do not
  295. * implement the address space operations prepare_write and commit_write. It
  296. * uses the write file operation which should be present on all writeable
  297. * filesystems.
  298. *
  299. * Using fops->write is slower than using aops->{prepare,commit}_write in the
  300. * transforming case because we need to double buffer the data as we cannot do
  301. * the transformations in place as we do not have direct access to the
  302. * destination pages of the backing file.
  303. */
  304. static int do_lo_send_write(struct loop_device *lo, struct bio_vec *bvec,
  305. int bsize, loff_t pos, struct page *page)
  306. {
  307. int ret = lo_do_transfer(lo, WRITE, page, 0, bvec->bv_page,
  308. bvec->bv_offset, bvec->bv_len, pos >> 9);
  309. if (likely(!ret))
  310. return __do_lo_send_write(lo->lo_backing_file,
  311. (u8 __user *)page_address(page), bvec->bv_len,
  312. pos);
  313. printk(KERN_ERR "loop: Transfer error at byte offset %llu, "
  314. "length %i.\n", (unsigned long long)pos, bvec->bv_len);
  315. if (ret > 0)
  316. ret = -EIO;
  317. return ret;
  318. }
  319. static int lo_send(struct loop_device *lo, struct bio *bio, int bsize,
  320. loff_t pos)
  321. {
  322. int (*do_lo_send)(struct loop_device *, struct bio_vec *, int, loff_t,
  323. struct page *page);
  324. struct bio_vec *bvec;
  325. struct page *page = NULL;
  326. int i, ret = 0;
  327. do_lo_send = do_lo_send_aops;
  328. if (!(lo->lo_flags & LO_FLAGS_USE_AOPS)) {
  329. do_lo_send = do_lo_send_direct_write;
  330. if (lo->transfer != transfer_none) {
  331. page = alloc_page(GFP_NOIO | __GFP_HIGHMEM);
  332. if (unlikely(!page))
  333. goto fail;
  334. kmap(page);
  335. do_lo_send = do_lo_send_write;
  336. }
  337. }
  338. bio_for_each_segment(bvec, bio, i) {
  339. ret = do_lo_send(lo, bvec, bsize, pos, page);
  340. if (ret < 0)
  341. break;
  342. pos += bvec->bv_len;
  343. }
  344. if (page) {
  345. kunmap(page);
  346. __free_page(page);
  347. }
  348. out:
  349. return ret;
  350. fail:
  351. printk(KERN_ERR "loop: Failed to allocate temporary page for write.\n");
  352. ret = -ENOMEM;
  353. goto out;
  354. }
  355. struct lo_read_data {
  356. struct loop_device *lo;
  357. struct page *page;
  358. unsigned offset;
  359. int bsize;
  360. };
  361. static int
  362. lo_read_actor(read_descriptor_t *desc, struct page *page,
  363. unsigned long offset, unsigned long size)
  364. {
  365. unsigned long count = desc->count;
  366. struct lo_read_data *p = desc->arg.data;
  367. struct loop_device *lo = p->lo;
  368. sector_t IV;
  369. IV = ((sector_t) page->index << (PAGE_CACHE_SHIFT - 9))+(offset >> 9);
  370. if (size > count)
  371. size = count;
  372. if (lo_do_transfer(lo, READ, page, offset, p->page, p->offset, size, IV)) {
  373. size = 0;
  374. printk(KERN_ERR "loop: transfer error block %ld\n",
  375. page->index);
  376. desc->error = -EINVAL;
  377. }
  378. flush_dcache_page(p->page);
  379. desc->count = count - size;
  380. desc->written += size;
  381. p->offset += size;
  382. return size;
  383. }
  384. static int
  385. do_lo_receive(struct loop_device *lo,
  386. struct bio_vec *bvec, int bsize, loff_t pos)
  387. {
  388. struct lo_read_data cookie;
  389. struct file *file;
  390. int retval;
  391. cookie.lo = lo;
  392. cookie.page = bvec->bv_page;
  393. cookie.offset = bvec->bv_offset;
  394. cookie.bsize = bsize;
  395. file = lo->lo_backing_file;
  396. retval = file->f_op->sendfile(file, &pos, bvec->bv_len,
  397. lo_read_actor, &cookie);
  398. return (retval < 0)? retval: 0;
  399. }
  400. static int
  401. lo_receive(struct loop_device *lo, struct bio *bio, int bsize, loff_t pos)
  402. {
  403. struct bio_vec *bvec;
  404. int i, ret = 0;
  405. bio_for_each_segment(bvec, bio, i) {
  406. ret = do_lo_receive(lo, bvec, bsize, pos);
  407. if (ret < 0)
  408. break;
  409. pos += bvec->bv_len;
  410. }
  411. return ret;
  412. }
  413. static int do_bio_filebacked(struct loop_device *lo, struct bio *bio)
  414. {
  415. loff_t pos;
  416. int ret;
  417. pos = ((loff_t) bio->bi_sector << 9) + lo->lo_offset;
  418. if (bio_rw(bio) == WRITE)
  419. ret = lo_send(lo, bio, lo->lo_blocksize, pos);
  420. else
  421. ret = lo_receive(lo, bio, lo->lo_blocksize, pos);
  422. return ret;
  423. }
  424. /*
  425. * Add bio to back of pending list
  426. */
  427. static void loop_add_bio(struct loop_device *lo, struct bio *bio)
  428. {
  429. unsigned long flags;
  430. spin_lock_irqsave(&lo->lo_lock, flags);
  431. if (lo->lo_biotail) {
  432. lo->lo_biotail->bi_next = bio;
  433. lo->lo_biotail = bio;
  434. } else
  435. lo->lo_bio = lo->lo_biotail = bio;
  436. spin_unlock_irqrestore(&lo->lo_lock, flags);
  437. up(&lo->lo_bh_mutex);
  438. }
  439. /*
  440. * Grab first pending buffer
  441. */
  442. static struct bio *loop_get_bio(struct loop_device *lo)
  443. {
  444. struct bio *bio;
  445. spin_lock_irq(&lo->lo_lock);
  446. if ((bio = lo->lo_bio)) {
  447. if (bio == lo->lo_biotail)
  448. lo->lo_biotail = NULL;
  449. lo->lo_bio = bio->bi_next;
  450. bio->bi_next = NULL;
  451. }
  452. spin_unlock_irq(&lo->lo_lock);
  453. return bio;
  454. }
  455. static int loop_make_request(request_queue_t *q, struct bio *old_bio)
  456. {
  457. struct loop_device *lo = q->queuedata;
  458. int rw = bio_rw(old_bio);
  459. if (!lo)
  460. goto out;
  461. spin_lock_irq(&lo->lo_lock);
  462. if (lo->lo_state != Lo_bound)
  463. goto inactive;
  464. atomic_inc(&lo->lo_pending);
  465. spin_unlock_irq(&lo->lo_lock);
  466. if (rw == WRITE) {
  467. if (lo->lo_flags & LO_FLAGS_READ_ONLY)
  468. goto err;
  469. } else if (rw == READA) {
  470. rw = READ;
  471. } else if (rw != READ) {
  472. printk(KERN_ERR "loop: unknown command (%x)\n", rw);
  473. goto err;
  474. }
  475. loop_add_bio(lo, old_bio);
  476. return 0;
  477. err:
  478. if (atomic_dec_and_test(&lo->lo_pending))
  479. up(&lo->lo_bh_mutex);
  480. out:
  481. bio_io_error(old_bio, old_bio->bi_size);
  482. return 0;
  483. inactive:
  484. spin_unlock_irq(&lo->lo_lock);
  485. goto out;
  486. }
  487. /*
  488. * kick off io on the underlying address space
  489. */
  490. static void loop_unplug(request_queue_t *q)
  491. {
  492. struct loop_device *lo = q->queuedata;
  493. clear_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags);
  494. blk_run_address_space(lo->lo_backing_file->f_mapping);
  495. }
  496. struct switch_request {
  497. struct file *file;
  498. struct completion wait;
  499. };
  500. static void do_loop_switch(struct loop_device *, struct switch_request *);
  501. static inline void loop_handle_bio(struct loop_device *lo, struct bio *bio)
  502. {
  503. int ret;
  504. if (unlikely(!bio->bi_bdev)) {
  505. do_loop_switch(lo, bio->bi_private);
  506. bio_put(bio);
  507. } else {
  508. ret = do_bio_filebacked(lo, bio);
  509. bio_endio(bio, bio->bi_size, ret);
  510. }
  511. }
  512. /*
  513. * worker thread that handles reads/writes to file backed loop devices,
  514. * to avoid blocking in our make_request_fn. it also does loop decrypting
  515. * on reads for block backed loop, as that is too heavy to do from
  516. * b_end_io context where irqs may be disabled.
  517. */
  518. static int loop_thread(void *data)
  519. {
  520. struct loop_device *lo = data;
  521. struct bio *bio;
  522. daemonize("loop%d", lo->lo_number);
  523. /*
  524. * loop can be used in an encrypted device,
  525. * hence, it mustn't be stopped at all
  526. * because it could be indirectly used during suspension
  527. */
  528. current->flags |= PF_NOFREEZE;
  529. set_user_nice(current, -20);
  530. lo->lo_state = Lo_bound;
  531. atomic_inc(&lo->lo_pending);
  532. /*
  533. * up sem, we are running
  534. */
  535. up(&lo->lo_sem);
  536. for (;;) {
  537. down_interruptible(&lo->lo_bh_mutex);
  538. /*
  539. * could be upped because of tear-down, not because of
  540. * pending work
  541. */
  542. if (!atomic_read(&lo->lo_pending))
  543. break;
  544. bio = loop_get_bio(lo);
  545. if (!bio) {
  546. printk("loop: missing bio\n");
  547. continue;
  548. }
  549. loop_handle_bio(lo, bio);
  550. /*
  551. * upped both for pending work and tear-down, lo_pending
  552. * will hit zero then
  553. */
  554. if (atomic_dec_and_test(&lo->lo_pending))
  555. break;
  556. }
  557. up(&lo->lo_sem);
  558. return 0;
  559. }
  560. /*
  561. * loop_switch performs the hard work of switching a backing store.
  562. * First it needs to flush existing IO, it does this by sending a magic
  563. * BIO down the pipe. The completion of this BIO does the actual switch.
  564. */
  565. static int loop_switch(struct loop_device *lo, struct file *file)
  566. {
  567. struct switch_request w;
  568. struct bio *bio = bio_alloc(GFP_KERNEL, 1);
  569. if (!bio)
  570. return -ENOMEM;
  571. init_completion(&w.wait);
  572. w.file = file;
  573. bio->bi_private = &w;
  574. bio->bi_bdev = NULL;
  575. loop_make_request(lo->lo_queue, bio);
  576. wait_for_completion(&w.wait);
  577. return 0;
  578. }
  579. /*
  580. * Do the actual switch; called from the BIO completion routine
  581. */
  582. static void do_loop_switch(struct loop_device *lo, struct switch_request *p)
  583. {
  584. struct file *file = p->file;
  585. struct file *old_file = lo->lo_backing_file;
  586. struct address_space *mapping = file->f_mapping;
  587. mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
  588. lo->lo_backing_file = file;
  589. lo->lo_blocksize = mapping->host->i_blksize;
  590. lo->old_gfp_mask = mapping_gfp_mask(mapping);
  591. mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
  592. complete(&p->wait);
  593. }
  594. /*
  595. * loop_change_fd switched the backing store of a loopback device to
  596. * a new file. This is useful for operating system installers to free up
  597. * the original file and in High Availability environments to switch to
  598. * an alternative location for the content in case of server meltdown.
  599. * This can only work if the loop device is used read-only, and if the
  600. * new backing store is the same size and type as the old backing store.
  601. */
  602. static int loop_change_fd(struct loop_device *lo, struct file *lo_file,
  603. struct block_device *bdev, unsigned int arg)
  604. {
  605. struct file *file, *old_file;
  606. struct inode *inode;
  607. int error;
  608. error = -ENXIO;
  609. if (lo->lo_state != Lo_bound)
  610. goto out;
  611. /* the loop device has to be read-only */
  612. error = -EINVAL;
  613. if (!(lo->lo_flags & LO_FLAGS_READ_ONLY))
  614. goto out;
  615. error = -EBADF;
  616. file = fget(arg);
  617. if (!file)
  618. goto out;
  619. inode = file->f_mapping->host;
  620. old_file = lo->lo_backing_file;
  621. error = -EINVAL;
  622. if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))
  623. goto out_putf;
  624. /* new backing store needs to support loop (eg sendfile) */
  625. if (!inode->i_fop->sendfile)
  626. goto out_putf;
  627. /* size of the new backing store needs to be the same */
  628. if (get_loop_size(lo, file) != get_loop_size(lo, old_file))
  629. goto out_putf;
  630. /* and ... switch */
  631. error = loop_switch(lo, file);
  632. if (error)
  633. goto out_putf;
  634. fput(old_file);
  635. return 0;
  636. out_putf:
  637. fput(file);
  638. out:
  639. return error;
  640. }
  641. static inline int is_loop_device(struct file *file)
  642. {
  643. struct inode *i = file->f_mapping->host;
  644. return i && S_ISBLK(i->i_mode) && MAJOR(i->i_rdev) == LOOP_MAJOR;
  645. }
  646. static int loop_set_fd(struct loop_device *lo, struct file *lo_file,
  647. struct block_device *bdev, unsigned int arg)
  648. {
  649. struct file *file, *f;
  650. struct inode *inode;
  651. struct address_space *mapping;
  652. unsigned lo_blocksize;
  653. int lo_flags = 0;
  654. int error;
  655. loff_t size;
  656. /* This is safe, since we have a reference from open(). */
  657. __module_get(THIS_MODULE);
  658. error = -EBADF;
  659. file = fget(arg);
  660. if (!file)
  661. goto out;
  662. error = -EBUSY;
  663. if (lo->lo_state != Lo_unbound)
  664. goto out_putf;
  665. /* Avoid recursion */
  666. f = file;
  667. while (is_loop_device(f)) {
  668. struct loop_device *l;
  669. if (f->f_mapping->host->i_rdev == lo_file->f_mapping->host->i_rdev)
  670. goto out_putf;
  671. l = f->f_mapping->host->i_bdev->bd_disk->private_data;
  672. if (l->lo_state == Lo_unbound) {
  673. error = -EINVAL;
  674. goto out_putf;
  675. }
  676. f = l->lo_backing_file;
  677. }
  678. mapping = file->f_mapping;
  679. inode = mapping->host;
  680. if (!(file->f_mode & FMODE_WRITE))
  681. lo_flags |= LO_FLAGS_READ_ONLY;
  682. error = -EINVAL;
  683. if (S_ISREG(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  684. struct address_space_operations *aops = mapping->a_ops;
  685. /*
  686. * If we can't read - sorry. If we only can't write - well,
  687. * it's going to be read-only.
  688. */
  689. if (!file->f_op->sendfile)
  690. goto out_putf;
  691. if (aops->prepare_write && aops->commit_write)
  692. lo_flags |= LO_FLAGS_USE_AOPS;
  693. if (!(lo_flags & LO_FLAGS_USE_AOPS) && !file->f_op->write)
  694. lo_flags |= LO_FLAGS_READ_ONLY;
  695. lo_blocksize = inode->i_blksize;
  696. error = 0;
  697. } else {
  698. goto out_putf;
  699. }
  700. size = get_loop_size(lo, file);
  701. if ((loff_t)(sector_t)size != size) {
  702. error = -EFBIG;
  703. goto out_putf;
  704. }
  705. if (!(lo_file->f_mode & FMODE_WRITE))
  706. lo_flags |= LO_FLAGS_READ_ONLY;
  707. set_device_ro(bdev, (lo_flags & LO_FLAGS_READ_ONLY) != 0);
  708. lo->lo_blocksize = lo_blocksize;
  709. lo->lo_device = bdev;
  710. lo->lo_flags = lo_flags;
  711. lo->lo_backing_file = file;
  712. lo->transfer = NULL;
  713. lo->ioctl = NULL;
  714. lo->lo_sizelimit = 0;
  715. lo->old_gfp_mask = mapping_gfp_mask(mapping);
  716. mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
  717. lo->lo_bio = lo->lo_biotail = NULL;
  718. /*
  719. * set queue make_request_fn, and add limits based on lower level
  720. * device
  721. */
  722. blk_queue_make_request(lo->lo_queue, loop_make_request);
  723. lo->lo_queue->queuedata = lo;
  724. lo->lo_queue->unplug_fn = loop_unplug;
  725. set_capacity(disks[lo->lo_number], size);
  726. bd_set_size(bdev, size << 9);
  727. set_blocksize(bdev, lo_blocksize);
  728. kernel_thread(loop_thread, lo, CLONE_KERNEL);
  729. down(&lo->lo_sem);
  730. return 0;
  731. out_putf:
  732. fput(file);
  733. out:
  734. /* This is safe: open() is still holding a reference. */
  735. module_put(THIS_MODULE);
  736. return error;
  737. }
  738. static int
  739. loop_release_xfer(struct loop_device *lo)
  740. {
  741. int err = 0;
  742. struct loop_func_table *xfer = lo->lo_encryption;
  743. if (xfer) {
  744. if (xfer->release)
  745. err = xfer->release(lo);
  746. lo->transfer = NULL;
  747. lo->lo_encryption = NULL;
  748. module_put(xfer->owner);
  749. }
  750. return err;
  751. }
  752. static int
  753. loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
  754. const struct loop_info64 *i)
  755. {
  756. int err = 0;
  757. if (xfer) {
  758. struct module *owner = xfer->owner;
  759. if (!try_module_get(owner))
  760. return -EINVAL;
  761. if (xfer->init)
  762. err = xfer->init(lo, i);
  763. if (err)
  764. module_put(owner);
  765. else
  766. lo->lo_encryption = xfer;
  767. }
  768. return err;
  769. }
  770. static int loop_clr_fd(struct loop_device *lo, struct block_device *bdev)
  771. {
  772. struct file *filp = lo->lo_backing_file;
  773. int gfp = lo->old_gfp_mask;
  774. if (lo->lo_state != Lo_bound)
  775. return -ENXIO;
  776. if (lo->lo_refcnt > 1) /* we needed one fd for the ioctl */
  777. return -EBUSY;
  778. if (filp == NULL)
  779. return -EINVAL;
  780. spin_lock_irq(&lo->lo_lock);
  781. lo->lo_state = Lo_rundown;
  782. if (atomic_dec_and_test(&lo->lo_pending))
  783. up(&lo->lo_bh_mutex);
  784. spin_unlock_irq(&lo->lo_lock);
  785. down(&lo->lo_sem);
  786. lo->lo_backing_file = NULL;
  787. loop_release_xfer(lo);
  788. lo->transfer = NULL;
  789. lo->ioctl = NULL;
  790. lo->lo_device = NULL;
  791. lo->lo_encryption = NULL;
  792. lo->lo_offset = 0;
  793. lo->lo_sizelimit = 0;
  794. lo->lo_encrypt_key_size = 0;
  795. lo->lo_flags = 0;
  796. memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE);
  797. memset(lo->lo_crypt_name, 0, LO_NAME_SIZE);
  798. memset(lo->lo_file_name, 0, LO_NAME_SIZE);
  799. invalidate_bdev(bdev, 0);
  800. set_capacity(disks[lo->lo_number], 0);
  801. bd_set_size(bdev, 0);
  802. mapping_set_gfp_mask(filp->f_mapping, gfp);
  803. lo->lo_state = Lo_unbound;
  804. fput(filp);
  805. /* This is safe: open() is still holding a reference. */
  806. module_put(THIS_MODULE);
  807. return 0;
  808. }
  809. static int
  810. loop_set_status(struct loop_device *lo, const struct loop_info64 *info)
  811. {
  812. int err;
  813. struct loop_func_table *xfer;
  814. if (lo->lo_encrypt_key_size && lo->lo_key_owner != current->uid &&
  815. !capable(CAP_SYS_ADMIN))
  816. return -EPERM;
  817. if (lo->lo_state != Lo_bound)
  818. return -ENXIO;
  819. if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE)
  820. return -EINVAL;
  821. err = loop_release_xfer(lo);
  822. if (err)
  823. return err;
  824. if (info->lo_encrypt_type) {
  825. unsigned int type = info->lo_encrypt_type;
  826. if (type >= MAX_LO_CRYPT)
  827. return -EINVAL;
  828. xfer = xfer_funcs[type];
  829. if (xfer == NULL)
  830. return -EINVAL;
  831. } else
  832. xfer = NULL;
  833. err = loop_init_xfer(lo, xfer, info);
  834. if (err)
  835. return err;
  836. if (lo->lo_offset != info->lo_offset ||
  837. lo->lo_sizelimit != info->lo_sizelimit) {
  838. lo->lo_offset = info->lo_offset;
  839. lo->lo_sizelimit = info->lo_sizelimit;
  840. if (figure_loop_size(lo))
  841. return -EFBIG;
  842. }
  843. memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE);
  844. memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE);
  845. lo->lo_file_name[LO_NAME_SIZE-1] = 0;
  846. lo->lo_crypt_name[LO_NAME_SIZE-1] = 0;
  847. if (!xfer)
  848. xfer = &none_funcs;
  849. lo->transfer = xfer->transfer;
  850. lo->ioctl = xfer->ioctl;
  851. lo->lo_encrypt_key_size = info->lo_encrypt_key_size;
  852. lo->lo_init[0] = info->lo_init[0];
  853. lo->lo_init[1] = info->lo_init[1];
  854. if (info->lo_encrypt_key_size) {
  855. memcpy(lo->lo_encrypt_key, info->lo_encrypt_key,
  856. info->lo_encrypt_key_size);
  857. lo->lo_key_owner = current->uid;
  858. }
  859. return 0;
  860. }
  861. static int
  862. loop_get_status(struct loop_device *lo, struct loop_info64 *info)
  863. {
  864. struct file *file = lo->lo_backing_file;
  865. struct kstat stat;
  866. int error;
  867. if (lo->lo_state != Lo_bound)
  868. return -ENXIO;
  869. error = vfs_getattr(file->f_vfsmnt, file->f_dentry, &stat);
  870. if (error)
  871. return error;
  872. memset(info, 0, sizeof(*info));
  873. info->lo_number = lo->lo_number;
  874. info->lo_device = huge_encode_dev(stat.dev);
  875. info->lo_inode = stat.ino;
  876. info->lo_rdevice = huge_encode_dev(lo->lo_device ? stat.rdev : stat.dev);
  877. info->lo_offset = lo->lo_offset;
  878. info->lo_sizelimit = lo->lo_sizelimit;
  879. info->lo_flags = lo->lo_flags;
  880. memcpy(info->lo_file_name, lo->lo_file_name, LO_NAME_SIZE);
  881. memcpy(info->lo_crypt_name, lo->lo_crypt_name, LO_NAME_SIZE);
  882. info->lo_encrypt_type =
  883. lo->lo_encryption ? lo->lo_encryption->number : 0;
  884. if (lo->lo_encrypt_key_size && capable(CAP_SYS_ADMIN)) {
  885. info->lo_encrypt_key_size = lo->lo_encrypt_key_size;
  886. memcpy(info->lo_encrypt_key, lo->lo_encrypt_key,
  887. lo->lo_encrypt_key_size);
  888. }
  889. return 0;
  890. }
  891. static void
  892. loop_info64_from_old(const struct loop_info *info, struct loop_info64 *info64)
  893. {
  894. memset(info64, 0, sizeof(*info64));
  895. info64->lo_number = info->lo_number;
  896. info64->lo_device = info->lo_device;
  897. info64->lo_inode = info->lo_inode;
  898. info64->lo_rdevice = info->lo_rdevice;
  899. info64->lo_offset = info->lo_offset;
  900. info64->lo_sizelimit = 0;
  901. info64->lo_encrypt_type = info->lo_encrypt_type;
  902. info64->lo_encrypt_key_size = info->lo_encrypt_key_size;
  903. info64->lo_flags = info->lo_flags;
  904. info64->lo_init[0] = info->lo_init[0];
  905. info64->lo_init[1] = info->lo_init[1];
  906. if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
  907. memcpy(info64->lo_crypt_name, info->lo_name, LO_NAME_SIZE);
  908. else
  909. memcpy(info64->lo_file_name, info->lo_name, LO_NAME_SIZE);
  910. memcpy(info64->lo_encrypt_key, info->lo_encrypt_key, LO_KEY_SIZE);
  911. }
  912. static int
  913. loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info)
  914. {
  915. memset(info, 0, sizeof(*info));
  916. info->lo_number = info64->lo_number;
  917. info->lo_device = info64->lo_device;
  918. info->lo_inode = info64->lo_inode;
  919. info->lo_rdevice = info64->lo_rdevice;
  920. info->lo_offset = info64->lo_offset;
  921. info->lo_encrypt_type = info64->lo_encrypt_type;
  922. info->lo_encrypt_key_size = info64->lo_encrypt_key_size;
  923. info->lo_flags = info64->lo_flags;
  924. info->lo_init[0] = info64->lo_init[0];
  925. info->lo_init[1] = info64->lo_init[1];
  926. if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
  927. memcpy(info->lo_name, info64->lo_crypt_name, LO_NAME_SIZE);
  928. else
  929. memcpy(info->lo_name, info64->lo_file_name, LO_NAME_SIZE);
  930. memcpy(info->lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE);
  931. /* error in case values were truncated */
  932. if (info->lo_device != info64->lo_device ||
  933. info->lo_rdevice != info64->lo_rdevice ||
  934. info->lo_inode != info64->lo_inode ||
  935. info->lo_offset != info64->lo_offset)
  936. return -EOVERFLOW;
  937. return 0;
  938. }
  939. static int
  940. loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg)
  941. {
  942. struct loop_info info;
  943. struct loop_info64 info64;
  944. if (copy_from_user(&info, arg, sizeof (struct loop_info)))
  945. return -EFAULT;
  946. loop_info64_from_old(&info, &info64);
  947. return loop_set_status(lo, &info64);
  948. }
  949. static int
  950. loop_set_status64(struct loop_device *lo, const struct loop_info64 __user *arg)
  951. {
  952. struct loop_info64 info64;
  953. if (copy_from_user(&info64, arg, sizeof (struct loop_info64)))
  954. return -EFAULT;
  955. return loop_set_status(lo, &info64);
  956. }
  957. static int
  958. loop_get_status_old(struct loop_device *lo, struct loop_info __user *arg) {
  959. struct loop_info info;
  960. struct loop_info64 info64;
  961. int err = 0;
  962. if (!arg)
  963. err = -EINVAL;
  964. if (!err)
  965. err = loop_get_status(lo, &info64);
  966. if (!err)
  967. err = loop_info64_to_old(&info64, &info);
  968. if (!err && copy_to_user(arg, &info, sizeof(info)))
  969. err = -EFAULT;
  970. return err;
  971. }
  972. static int
  973. loop_get_status64(struct loop_device *lo, struct loop_info64 __user *arg) {
  974. struct loop_info64 info64;
  975. int err = 0;
  976. if (!arg)
  977. err = -EINVAL;
  978. if (!err)
  979. err = loop_get_status(lo, &info64);
  980. if (!err && copy_to_user(arg, &info64, sizeof(info64)))
  981. err = -EFAULT;
  982. return err;
  983. }
  984. static int lo_ioctl(struct inode * inode, struct file * file,
  985. unsigned int cmd, unsigned long arg)
  986. {
  987. struct loop_device *lo = inode->i_bdev->bd_disk->private_data;
  988. int err;
  989. down(&lo->lo_ctl_mutex);
  990. switch (cmd) {
  991. case LOOP_SET_FD:
  992. err = loop_set_fd(lo, file, inode->i_bdev, arg);
  993. break;
  994. case LOOP_CHANGE_FD:
  995. err = loop_change_fd(lo, file, inode->i_bdev, arg);
  996. break;
  997. case LOOP_CLR_FD:
  998. err = loop_clr_fd(lo, inode->i_bdev);
  999. break;
  1000. case LOOP_SET_STATUS:
  1001. err = loop_set_status_old(lo, (struct loop_info __user *) arg);
  1002. break;
  1003. case LOOP_GET_STATUS:
  1004. err = loop_get_status_old(lo, (struct loop_info __user *) arg);
  1005. break;
  1006. case LOOP_SET_STATUS64:
  1007. err = loop_set_status64(lo, (struct loop_info64 __user *) arg);
  1008. break;
  1009. case LOOP_GET_STATUS64:
  1010. err = loop_get_status64(lo, (struct loop_info64 __user *) arg);
  1011. break;
  1012. default:
  1013. err = lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL;
  1014. }
  1015. up(&lo->lo_ctl_mutex);
  1016. return err;
  1017. }
  1018. static int lo_open(struct inode *inode, struct file *file)
  1019. {
  1020. struct loop_device *lo = inode->i_bdev->bd_disk->private_data;
  1021. down(&lo->lo_ctl_mutex);
  1022. lo->lo_refcnt++;
  1023. up(&lo->lo_ctl_mutex);
  1024. return 0;
  1025. }
  1026. static int lo_release(struct inode *inode, struct file *file)
  1027. {
  1028. struct loop_device *lo = inode->i_bdev->bd_disk->private_data;
  1029. down(&lo->lo_ctl_mutex);
  1030. --lo->lo_refcnt;
  1031. up(&lo->lo_ctl_mutex);
  1032. return 0;
  1033. }
  1034. static struct block_device_operations lo_fops = {
  1035. .owner = THIS_MODULE,
  1036. .open = lo_open,
  1037. .release = lo_release,
  1038. .ioctl = lo_ioctl,
  1039. };
  1040. /*
  1041. * And now the modules code and kernel interface.
  1042. */
  1043. module_param(max_loop, int, 0);
  1044. MODULE_PARM_DESC(max_loop, "Maximum number of loop devices (1-256)");
  1045. MODULE_LICENSE("GPL");
  1046. MODULE_ALIAS_BLOCKDEV_MAJOR(LOOP_MAJOR);
  1047. int loop_register_transfer(struct loop_func_table *funcs)
  1048. {
  1049. unsigned int n = funcs->number;
  1050. if (n >= MAX_LO_CRYPT || xfer_funcs[n])
  1051. return -EINVAL;
  1052. xfer_funcs[n] = funcs;
  1053. return 0;
  1054. }
  1055. int loop_unregister_transfer(int number)
  1056. {
  1057. unsigned int n = number;
  1058. struct loop_device *lo;
  1059. struct loop_func_table *xfer;
  1060. if (n == 0 || n >= MAX_LO_CRYPT || (xfer = xfer_funcs[n]) == NULL)
  1061. return -EINVAL;
  1062. xfer_funcs[n] = NULL;
  1063. for (lo = &loop_dev[0]; lo < &loop_dev[max_loop]; lo++) {
  1064. down(&lo->lo_ctl_mutex);
  1065. if (lo->lo_encryption == xfer)
  1066. loop_release_xfer(lo);
  1067. up(&lo->lo_ctl_mutex);
  1068. }
  1069. return 0;
  1070. }
  1071. EXPORT_SYMBOL(loop_register_transfer);
  1072. EXPORT_SYMBOL(loop_unregister_transfer);
  1073. static int __init loop_init(void)
  1074. {
  1075. int i;
  1076. if (max_loop < 1 || max_loop > 256) {
  1077. printk(KERN_WARNING "loop: invalid max_loop (must be between"
  1078. " 1 and 256), using default (8)\n");
  1079. max_loop = 8;
  1080. }
  1081. if (register_blkdev(LOOP_MAJOR, "loop"))
  1082. return -EIO;
  1083. loop_dev = kmalloc(max_loop * sizeof(struct loop_device), GFP_KERNEL);
  1084. if (!loop_dev)
  1085. goto out_mem1;
  1086. memset(loop_dev, 0, max_loop * sizeof(struct loop_device));
  1087. disks = kmalloc(max_loop * sizeof(struct gendisk *), GFP_KERNEL);
  1088. if (!disks)
  1089. goto out_mem2;
  1090. for (i = 0; i < max_loop; i++) {
  1091. disks[i] = alloc_disk(1);
  1092. if (!disks[i])
  1093. goto out_mem3;
  1094. }
  1095. devfs_mk_dir("loop");
  1096. for (i = 0; i < max_loop; i++) {
  1097. struct loop_device *lo = &loop_dev[i];
  1098. struct gendisk *disk = disks[i];
  1099. memset(lo, 0, sizeof(*lo));
  1100. lo->lo_queue = blk_alloc_queue(GFP_KERNEL);
  1101. if (!lo->lo_queue)
  1102. goto out_mem4;
  1103. init_MUTEX(&lo->lo_ctl_mutex);
  1104. init_MUTEX_LOCKED(&lo->lo_sem);
  1105. init_MUTEX_LOCKED(&lo->lo_bh_mutex);
  1106. lo->lo_number = i;
  1107. spin_lock_init(&lo->lo_lock);
  1108. disk->major = LOOP_MAJOR;
  1109. disk->first_minor = i;
  1110. disk->fops = &lo_fops;
  1111. sprintf(disk->disk_name, "loop%d", i);
  1112. sprintf(disk->devfs_name, "loop/%d", i);
  1113. disk->private_data = lo;
  1114. disk->queue = lo->lo_queue;
  1115. }
  1116. /* We cannot fail after we call this, so another loop!*/
  1117. for (i = 0; i < max_loop; i++)
  1118. add_disk(disks[i]);
  1119. printk(KERN_INFO "loop: loaded (max %d devices)\n", max_loop);
  1120. return 0;
  1121. out_mem4:
  1122. while (i--)
  1123. blk_put_queue(loop_dev[i].lo_queue);
  1124. devfs_remove("loop");
  1125. i = max_loop;
  1126. out_mem3:
  1127. while (i--)
  1128. put_disk(disks[i]);
  1129. kfree(disks);
  1130. out_mem2:
  1131. kfree(loop_dev);
  1132. out_mem1:
  1133. unregister_blkdev(LOOP_MAJOR, "loop");
  1134. printk(KERN_ERR "loop: ran out of memory\n");
  1135. return -ENOMEM;
  1136. }
  1137. static void loop_exit(void)
  1138. {
  1139. int i;
  1140. for (i = 0; i < max_loop; i++) {
  1141. del_gendisk(disks[i]);
  1142. blk_put_queue(loop_dev[i].lo_queue);
  1143. put_disk(disks[i]);
  1144. }
  1145. devfs_remove("loop");
  1146. if (unregister_blkdev(LOOP_MAJOR, "loop"))
  1147. printk(KERN_WARNING "loop: cannot unregister blkdev\n");
  1148. kfree(disks);
  1149. kfree(loop_dev);
  1150. }
  1151. module_init(loop_init);
  1152. module_exit(loop_exit);
  1153. #ifndef MODULE
  1154. static int __init max_loop_setup(char *str)
  1155. {
  1156. max_loop = simple_strtol(str, NULL, 0);
  1157. return 1;
  1158. }
  1159. __setup("max_loop=", max_loop_setup);
  1160. #endif