loop.c 33 KB

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