block_dev.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921
  1. /*
  2. * linux/fs/block_dev.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. * Copyright (C) 2001 Andrea Arcangeli <andrea@suse.de> SuSE
  6. */
  7. #include <linux/init.h>
  8. #include <linux/mm.h>
  9. #include <linux/fcntl.h>
  10. #include <linux/slab.h>
  11. #include <linux/kmod.h>
  12. #include <linux/major.h>
  13. #include <linux/device_cgroup.h>
  14. #include <linux/highmem.h>
  15. #include <linux/blkdev.h>
  16. #include <linux/backing-dev.h>
  17. #include <linux/module.h>
  18. #include <linux/blkpg.h>
  19. #include <linux/magic.h>
  20. #include <linux/buffer_head.h>
  21. #include <linux/swap.h>
  22. #include <linux/pagevec.h>
  23. #include <linux/writeback.h>
  24. #include <linux/mpage.h>
  25. #include <linux/mount.h>
  26. #include <linux/uio.h>
  27. #include <linux/namei.h>
  28. #include <linux/log2.h>
  29. #include <linux/cleancache.h>
  30. #include <linux/dax.h>
  31. #include <asm/uaccess.h>
  32. #include "internal.h"
  33. struct bdev_inode {
  34. struct block_device bdev;
  35. struct inode vfs_inode;
  36. };
  37. static const struct address_space_operations def_blk_aops;
  38. static inline struct bdev_inode *BDEV_I(struct inode *inode)
  39. {
  40. return container_of(inode, struct bdev_inode, vfs_inode);
  41. }
  42. struct block_device *I_BDEV(struct inode *inode)
  43. {
  44. return &BDEV_I(inode)->bdev;
  45. }
  46. EXPORT_SYMBOL(I_BDEV);
  47. static void bdev_write_inode(struct block_device *bdev)
  48. {
  49. struct inode *inode = bdev->bd_inode;
  50. int ret;
  51. spin_lock(&inode->i_lock);
  52. while (inode->i_state & I_DIRTY) {
  53. spin_unlock(&inode->i_lock);
  54. ret = write_inode_now(inode, true);
  55. if (ret) {
  56. char name[BDEVNAME_SIZE];
  57. pr_warn_ratelimited("VFS: Dirty inode writeback failed "
  58. "for block device %s (err=%d).\n",
  59. bdevname(bdev, name), ret);
  60. }
  61. spin_lock(&inode->i_lock);
  62. }
  63. spin_unlock(&inode->i_lock);
  64. }
  65. /* Kill _all_ buffers and pagecache , dirty or not.. */
  66. void kill_bdev(struct block_device *bdev)
  67. {
  68. struct address_space *mapping = bdev->bd_inode->i_mapping;
  69. if (mapping->nrpages == 0 && mapping->nrshadows == 0)
  70. return;
  71. invalidate_bh_lrus();
  72. truncate_inode_pages(mapping, 0);
  73. }
  74. EXPORT_SYMBOL(kill_bdev);
  75. /* Invalidate clean unused buffers and pagecache. */
  76. void invalidate_bdev(struct block_device *bdev)
  77. {
  78. struct address_space *mapping = bdev->bd_inode->i_mapping;
  79. if (mapping->nrpages == 0)
  80. return;
  81. invalidate_bh_lrus();
  82. lru_add_drain_all(); /* make sure all lru add caches are flushed */
  83. invalidate_mapping_pages(mapping, 0, -1);
  84. /* 99% of the time, we don't need to flush the cleancache on the bdev.
  85. * But, for the strange corners, lets be cautious
  86. */
  87. cleancache_invalidate_inode(mapping);
  88. }
  89. EXPORT_SYMBOL(invalidate_bdev);
  90. int set_blocksize(struct block_device *bdev, int size)
  91. {
  92. /* Size must be a power of two, and between 512 and PAGE_SIZE */
  93. if (size > PAGE_SIZE || size < 512 || !is_power_of_2(size))
  94. return -EINVAL;
  95. /* Size cannot be smaller than the size supported by the device */
  96. if (size < bdev_logical_block_size(bdev))
  97. return -EINVAL;
  98. /* Don't change the size if it is same as current */
  99. if (bdev->bd_block_size != size) {
  100. sync_blockdev(bdev);
  101. bdev->bd_block_size = size;
  102. bdev->bd_inode->i_blkbits = blksize_bits(size);
  103. kill_bdev(bdev);
  104. }
  105. return 0;
  106. }
  107. EXPORT_SYMBOL(set_blocksize);
  108. int sb_set_blocksize(struct super_block *sb, int size)
  109. {
  110. if (set_blocksize(sb->s_bdev, size))
  111. return 0;
  112. /* If we get here, we know size is power of two
  113. * and it's value is between 512 and PAGE_SIZE */
  114. sb->s_blocksize = size;
  115. sb->s_blocksize_bits = blksize_bits(size);
  116. return sb->s_blocksize;
  117. }
  118. EXPORT_SYMBOL(sb_set_blocksize);
  119. int sb_min_blocksize(struct super_block *sb, int size)
  120. {
  121. int minsize = bdev_logical_block_size(sb->s_bdev);
  122. if (size < minsize)
  123. size = minsize;
  124. return sb_set_blocksize(sb, size);
  125. }
  126. EXPORT_SYMBOL(sb_min_blocksize);
  127. static int
  128. blkdev_get_block(struct inode *inode, sector_t iblock,
  129. struct buffer_head *bh, int create)
  130. {
  131. bh->b_bdev = I_BDEV(inode);
  132. bh->b_blocknr = iblock;
  133. set_buffer_mapped(bh);
  134. return 0;
  135. }
  136. static struct inode *bdev_file_inode(struct file *file)
  137. {
  138. return file->f_mapping->host;
  139. }
  140. static ssize_t
  141. blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter, loff_t offset)
  142. {
  143. struct file *file = iocb->ki_filp;
  144. struct inode *inode = bdev_file_inode(file);
  145. if (IS_DAX(inode))
  146. return dax_do_io(iocb, inode, iter, offset, blkdev_get_block,
  147. NULL, DIO_SKIP_DIO_COUNT);
  148. return __blockdev_direct_IO(iocb, inode, I_BDEV(inode), iter, offset,
  149. blkdev_get_block, NULL, NULL,
  150. DIO_SKIP_DIO_COUNT);
  151. }
  152. int __sync_blockdev(struct block_device *bdev, int wait)
  153. {
  154. if (!bdev)
  155. return 0;
  156. if (!wait)
  157. return filemap_flush(bdev->bd_inode->i_mapping);
  158. return filemap_write_and_wait(bdev->bd_inode->i_mapping);
  159. }
  160. /*
  161. * Write out and wait upon all the dirty data associated with a block
  162. * device via its mapping. Does not take the superblock lock.
  163. */
  164. int sync_blockdev(struct block_device *bdev)
  165. {
  166. return __sync_blockdev(bdev, 1);
  167. }
  168. EXPORT_SYMBOL(sync_blockdev);
  169. /*
  170. * Write out and wait upon all dirty data associated with this
  171. * device. Filesystem data as well as the underlying block
  172. * device. Takes the superblock lock.
  173. */
  174. int fsync_bdev(struct block_device *bdev)
  175. {
  176. struct super_block *sb = get_super(bdev);
  177. if (sb) {
  178. int res = sync_filesystem(sb);
  179. drop_super(sb);
  180. return res;
  181. }
  182. return sync_blockdev(bdev);
  183. }
  184. EXPORT_SYMBOL(fsync_bdev);
  185. /**
  186. * freeze_bdev -- lock a filesystem and force it into a consistent state
  187. * @bdev: blockdevice to lock
  188. *
  189. * If a superblock is found on this device, we take the s_umount semaphore
  190. * on it to make sure nobody unmounts until the snapshot creation is done.
  191. * The reference counter (bd_fsfreeze_count) guarantees that only the last
  192. * unfreeze process can unfreeze the frozen filesystem actually when multiple
  193. * freeze requests arrive simultaneously. It counts up in freeze_bdev() and
  194. * count down in thaw_bdev(). When it becomes 0, thaw_bdev() will unfreeze
  195. * actually.
  196. */
  197. struct super_block *freeze_bdev(struct block_device *bdev)
  198. {
  199. struct super_block *sb;
  200. int error = 0;
  201. mutex_lock(&bdev->bd_fsfreeze_mutex);
  202. if (++bdev->bd_fsfreeze_count > 1) {
  203. /*
  204. * We don't even need to grab a reference - the first call
  205. * to freeze_bdev grab an active reference and only the last
  206. * thaw_bdev drops it.
  207. */
  208. sb = get_super(bdev);
  209. drop_super(sb);
  210. mutex_unlock(&bdev->bd_fsfreeze_mutex);
  211. return sb;
  212. }
  213. sb = get_active_super(bdev);
  214. if (!sb)
  215. goto out;
  216. if (sb->s_op->freeze_super)
  217. error = sb->s_op->freeze_super(sb);
  218. else
  219. error = freeze_super(sb);
  220. if (error) {
  221. deactivate_super(sb);
  222. bdev->bd_fsfreeze_count--;
  223. mutex_unlock(&bdev->bd_fsfreeze_mutex);
  224. return ERR_PTR(error);
  225. }
  226. deactivate_super(sb);
  227. out:
  228. sync_blockdev(bdev);
  229. mutex_unlock(&bdev->bd_fsfreeze_mutex);
  230. return sb; /* thaw_bdev releases s->s_umount */
  231. }
  232. EXPORT_SYMBOL(freeze_bdev);
  233. /**
  234. * thaw_bdev -- unlock filesystem
  235. * @bdev: blockdevice to unlock
  236. * @sb: associated superblock
  237. *
  238. * Unlocks the filesystem and marks it writeable again after freeze_bdev().
  239. */
  240. int thaw_bdev(struct block_device *bdev, struct super_block *sb)
  241. {
  242. int error = -EINVAL;
  243. mutex_lock(&bdev->bd_fsfreeze_mutex);
  244. if (!bdev->bd_fsfreeze_count)
  245. goto out;
  246. error = 0;
  247. if (--bdev->bd_fsfreeze_count > 0)
  248. goto out;
  249. if (!sb)
  250. goto out;
  251. if (sb->s_op->thaw_super)
  252. error = sb->s_op->thaw_super(sb);
  253. else
  254. error = thaw_super(sb);
  255. if (error) {
  256. bdev->bd_fsfreeze_count++;
  257. mutex_unlock(&bdev->bd_fsfreeze_mutex);
  258. return error;
  259. }
  260. out:
  261. mutex_unlock(&bdev->bd_fsfreeze_mutex);
  262. return 0;
  263. }
  264. EXPORT_SYMBOL(thaw_bdev);
  265. static int blkdev_writepage(struct page *page, struct writeback_control *wbc)
  266. {
  267. return block_write_full_page(page, blkdev_get_block, wbc);
  268. }
  269. static int blkdev_readpage(struct file * file, struct page * page)
  270. {
  271. return block_read_full_page(page, blkdev_get_block);
  272. }
  273. static int blkdev_readpages(struct file *file, struct address_space *mapping,
  274. struct list_head *pages, unsigned nr_pages)
  275. {
  276. return mpage_readpages(mapping, pages, nr_pages, blkdev_get_block);
  277. }
  278. static int blkdev_write_begin(struct file *file, struct address_space *mapping,
  279. loff_t pos, unsigned len, unsigned flags,
  280. struct page **pagep, void **fsdata)
  281. {
  282. return block_write_begin(mapping, pos, len, flags, pagep,
  283. blkdev_get_block);
  284. }
  285. static int blkdev_write_end(struct file *file, struct address_space *mapping,
  286. loff_t pos, unsigned len, unsigned copied,
  287. struct page *page, void *fsdata)
  288. {
  289. int ret;
  290. ret = block_write_end(file, mapping, pos, len, copied, page, fsdata);
  291. unlock_page(page);
  292. page_cache_release(page);
  293. return ret;
  294. }
  295. /*
  296. * private llseek:
  297. * for a block special file file_inode(file)->i_size is zero
  298. * so we compute the size by hand (just as in block_read/write above)
  299. */
  300. static loff_t block_llseek(struct file *file, loff_t offset, int whence)
  301. {
  302. struct inode *bd_inode = bdev_file_inode(file);
  303. loff_t retval;
  304. inode_lock(bd_inode);
  305. retval = fixed_size_llseek(file, offset, whence, i_size_read(bd_inode));
  306. inode_unlock(bd_inode);
  307. return retval;
  308. }
  309. int blkdev_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
  310. {
  311. struct inode *bd_inode = bdev_file_inode(filp);
  312. struct block_device *bdev = I_BDEV(bd_inode);
  313. int error;
  314. error = filemap_write_and_wait_range(filp->f_mapping, start, end);
  315. if (error)
  316. return error;
  317. /*
  318. * There is no need to serialise calls to blkdev_issue_flush with
  319. * i_mutex and doing so causes performance issues with concurrent
  320. * O_SYNC writers to a block device.
  321. */
  322. error = blkdev_issue_flush(bdev, GFP_KERNEL, NULL);
  323. if (error == -EOPNOTSUPP)
  324. error = 0;
  325. return error;
  326. }
  327. EXPORT_SYMBOL(blkdev_fsync);
  328. /**
  329. * bdev_read_page() - Start reading a page from a block device
  330. * @bdev: The device to read the page from
  331. * @sector: The offset on the device to read the page to (need not be aligned)
  332. * @page: The page to read
  333. *
  334. * On entry, the page should be locked. It will be unlocked when the page
  335. * has been read. If the block driver implements rw_page synchronously,
  336. * that will be true on exit from this function, but it need not be.
  337. *
  338. * Errors returned by this function are usually "soft", eg out of memory, or
  339. * queue full; callers should try a different route to read this page rather
  340. * than propagate an error back up the stack.
  341. *
  342. * Return: negative errno if an error occurs, 0 if submission was successful.
  343. */
  344. int bdev_read_page(struct block_device *bdev, sector_t sector,
  345. struct page *page)
  346. {
  347. const struct block_device_operations *ops = bdev->bd_disk->fops;
  348. int result = -EOPNOTSUPP;
  349. if (!ops->rw_page || bdev_get_integrity(bdev))
  350. return result;
  351. result = blk_queue_enter(bdev->bd_queue, false);
  352. if (result)
  353. return result;
  354. result = ops->rw_page(bdev, sector + get_start_sect(bdev), page, READ);
  355. blk_queue_exit(bdev->bd_queue);
  356. return result;
  357. }
  358. EXPORT_SYMBOL_GPL(bdev_read_page);
  359. /**
  360. * bdev_write_page() - Start writing a page to a block device
  361. * @bdev: The device to write the page to
  362. * @sector: The offset on the device to write the page to (need not be aligned)
  363. * @page: The page to write
  364. * @wbc: The writeback_control for the write
  365. *
  366. * On entry, the page should be locked and not currently under writeback.
  367. * On exit, if the write started successfully, the page will be unlocked and
  368. * under writeback. If the write failed already (eg the driver failed to
  369. * queue the page to the device), the page will still be locked. If the
  370. * caller is a ->writepage implementation, it will need to unlock the page.
  371. *
  372. * Errors returned by this function are usually "soft", eg out of memory, or
  373. * queue full; callers should try a different route to write this page rather
  374. * than propagate an error back up the stack.
  375. *
  376. * Return: negative errno if an error occurs, 0 if submission was successful.
  377. */
  378. int bdev_write_page(struct block_device *bdev, sector_t sector,
  379. struct page *page, struct writeback_control *wbc)
  380. {
  381. int result;
  382. int rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE;
  383. const struct block_device_operations *ops = bdev->bd_disk->fops;
  384. if (!ops->rw_page || bdev_get_integrity(bdev))
  385. return -EOPNOTSUPP;
  386. result = blk_queue_enter(bdev->bd_queue, false);
  387. if (result)
  388. return result;
  389. set_page_writeback(page);
  390. result = ops->rw_page(bdev, sector + get_start_sect(bdev), page, rw);
  391. if (result)
  392. end_page_writeback(page);
  393. else
  394. unlock_page(page);
  395. blk_queue_exit(bdev->bd_queue);
  396. return result;
  397. }
  398. EXPORT_SYMBOL_GPL(bdev_write_page);
  399. /**
  400. * bdev_direct_access() - Get the address for directly-accessibly memory
  401. * @bdev: The device containing the memory
  402. * @dax: control and output parameters for ->direct_access
  403. *
  404. * If a block device is made up of directly addressable memory, this function
  405. * will tell the caller the PFN and the address of the memory. The address
  406. * may be directly dereferenced within the kernel without the need to call
  407. * ioremap(), kmap() or similar. The PFN is suitable for inserting into
  408. * page tables.
  409. *
  410. * Return: negative errno if an error occurs, otherwise the number of bytes
  411. * accessible at this address.
  412. */
  413. long bdev_direct_access(struct block_device *bdev, struct blk_dax_ctl *dax)
  414. {
  415. sector_t sector = dax->sector;
  416. long avail, size = dax->size;
  417. const struct block_device_operations *ops = bdev->bd_disk->fops;
  418. /*
  419. * The device driver is allowed to sleep, in order to make the
  420. * memory directly accessible.
  421. */
  422. might_sleep();
  423. if (size < 0)
  424. return size;
  425. if (!ops->direct_access)
  426. return -EOPNOTSUPP;
  427. if ((sector + DIV_ROUND_UP(size, 512)) >
  428. part_nr_sects_read(bdev->bd_part))
  429. return -ERANGE;
  430. sector += get_start_sect(bdev);
  431. if (sector % (PAGE_SIZE / 512))
  432. return -EINVAL;
  433. avail = ops->direct_access(bdev, sector, &dax->addr, &dax->pfn);
  434. if (!avail)
  435. return -ERANGE;
  436. if (avail > 0 && avail & ~PAGE_MASK)
  437. return -ENXIO;
  438. return min(avail, size);
  439. }
  440. EXPORT_SYMBOL_GPL(bdev_direct_access);
  441. /*
  442. * pseudo-fs
  443. */
  444. static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);
  445. static struct kmem_cache * bdev_cachep __read_mostly;
  446. static struct inode *bdev_alloc_inode(struct super_block *sb)
  447. {
  448. struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, GFP_KERNEL);
  449. if (!ei)
  450. return NULL;
  451. return &ei->vfs_inode;
  452. }
  453. static void bdev_i_callback(struct rcu_head *head)
  454. {
  455. struct inode *inode = container_of(head, struct inode, i_rcu);
  456. struct bdev_inode *bdi = BDEV_I(inode);
  457. kmem_cache_free(bdev_cachep, bdi);
  458. }
  459. static void bdev_destroy_inode(struct inode *inode)
  460. {
  461. call_rcu(&inode->i_rcu, bdev_i_callback);
  462. }
  463. static void init_once(void *foo)
  464. {
  465. struct bdev_inode *ei = (struct bdev_inode *) foo;
  466. struct block_device *bdev = &ei->bdev;
  467. memset(bdev, 0, sizeof(*bdev));
  468. mutex_init(&bdev->bd_mutex);
  469. INIT_LIST_HEAD(&bdev->bd_inodes);
  470. INIT_LIST_HEAD(&bdev->bd_list);
  471. #ifdef CONFIG_SYSFS
  472. INIT_LIST_HEAD(&bdev->bd_holder_disks);
  473. #endif
  474. inode_init_once(&ei->vfs_inode);
  475. /* Initialize mutex for freeze. */
  476. mutex_init(&bdev->bd_fsfreeze_mutex);
  477. }
  478. static inline void __bd_forget(struct inode *inode)
  479. {
  480. list_del_init(&inode->i_devices);
  481. inode->i_bdev = NULL;
  482. inode->i_mapping = &inode->i_data;
  483. }
  484. static void bdev_evict_inode(struct inode *inode)
  485. {
  486. struct block_device *bdev = &BDEV_I(inode)->bdev;
  487. struct list_head *p;
  488. truncate_inode_pages_final(&inode->i_data);
  489. invalidate_inode_buffers(inode); /* is it needed here? */
  490. clear_inode(inode);
  491. spin_lock(&bdev_lock);
  492. while ( (p = bdev->bd_inodes.next) != &bdev->bd_inodes ) {
  493. __bd_forget(list_entry(p, struct inode, i_devices));
  494. }
  495. list_del_init(&bdev->bd_list);
  496. spin_unlock(&bdev_lock);
  497. }
  498. static const struct super_operations bdev_sops = {
  499. .statfs = simple_statfs,
  500. .alloc_inode = bdev_alloc_inode,
  501. .destroy_inode = bdev_destroy_inode,
  502. .drop_inode = generic_delete_inode,
  503. .evict_inode = bdev_evict_inode,
  504. };
  505. static struct dentry *bd_mount(struct file_system_type *fs_type,
  506. int flags, const char *dev_name, void *data)
  507. {
  508. return mount_pseudo(fs_type, "bdev:", &bdev_sops, NULL, BDEVFS_MAGIC);
  509. }
  510. static struct file_system_type bd_type = {
  511. .name = "bdev",
  512. .mount = bd_mount,
  513. .kill_sb = kill_anon_super,
  514. };
  515. struct super_block *blockdev_superblock __read_mostly;
  516. EXPORT_SYMBOL_GPL(blockdev_superblock);
  517. void __init bdev_cache_init(void)
  518. {
  519. int err;
  520. static struct vfsmount *bd_mnt;
  521. bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
  522. 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
  523. SLAB_MEM_SPREAD|SLAB_ACCOUNT|SLAB_PANIC),
  524. init_once);
  525. err = register_filesystem(&bd_type);
  526. if (err)
  527. panic("Cannot register bdev pseudo-fs");
  528. bd_mnt = kern_mount(&bd_type);
  529. if (IS_ERR(bd_mnt))
  530. panic("Cannot create bdev pseudo-fs");
  531. blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */
  532. }
  533. /*
  534. * Most likely _very_ bad one - but then it's hardly critical for small
  535. * /dev and can be fixed when somebody will need really large one.
  536. * Keep in mind that it will be fed through icache hash function too.
  537. */
  538. static inline unsigned long hash(dev_t dev)
  539. {
  540. return MAJOR(dev)+MINOR(dev);
  541. }
  542. static int bdev_test(struct inode *inode, void *data)
  543. {
  544. return BDEV_I(inode)->bdev.bd_dev == *(dev_t *)data;
  545. }
  546. static int bdev_set(struct inode *inode, void *data)
  547. {
  548. BDEV_I(inode)->bdev.bd_dev = *(dev_t *)data;
  549. return 0;
  550. }
  551. static LIST_HEAD(all_bdevs);
  552. struct block_device *bdget(dev_t dev)
  553. {
  554. struct block_device *bdev;
  555. struct inode *inode;
  556. inode = iget5_locked(blockdev_superblock, hash(dev),
  557. bdev_test, bdev_set, &dev);
  558. if (!inode)
  559. return NULL;
  560. bdev = &BDEV_I(inode)->bdev;
  561. if (inode->i_state & I_NEW) {
  562. bdev->bd_contains = NULL;
  563. bdev->bd_super = NULL;
  564. bdev->bd_inode = inode;
  565. bdev->bd_block_size = (1 << inode->i_blkbits);
  566. bdev->bd_part_count = 0;
  567. bdev->bd_invalidated = 0;
  568. inode->i_mode = S_IFBLK;
  569. inode->i_rdev = dev;
  570. inode->i_bdev = bdev;
  571. inode->i_data.a_ops = &def_blk_aops;
  572. mapping_set_gfp_mask(&inode->i_data, GFP_USER);
  573. spin_lock(&bdev_lock);
  574. list_add(&bdev->bd_list, &all_bdevs);
  575. spin_unlock(&bdev_lock);
  576. unlock_new_inode(inode);
  577. }
  578. return bdev;
  579. }
  580. EXPORT_SYMBOL(bdget);
  581. /**
  582. * bdgrab -- Grab a reference to an already referenced block device
  583. * @bdev: Block device to grab a reference to.
  584. */
  585. struct block_device *bdgrab(struct block_device *bdev)
  586. {
  587. ihold(bdev->bd_inode);
  588. return bdev;
  589. }
  590. EXPORT_SYMBOL(bdgrab);
  591. long nr_blockdev_pages(void)
  592. {
  593. struct block_device *bdev;
  594. long ret = 0;
  595. spin_lock(&bdev_lock);
  596. list_for_each_entry(bdev, &all_bdevs, bd_list) {
  597. ret += bdev->bd_inode->i_mapping->nrpages;
  598. }
  599. spin_unlock(&bdev_lock);
  600. return ret;
  601. }
  602. void bdput(struct block_device *bdev)
  603. {
  604. iput(bdev->bd_inode);
  605. }
  606. EXPORT_SYMBOL(bdput);
  607. static struct block_device *bd_acquire(struct inode *inode)
  608. {
  609. struct block_device *bdev;
  610. spin_lock(&bdev_lock);
  611. bdev = inode->i_bdev;
  612. if (bdev) {
  613. bdgrab(bdev);
  614. spin_unlock(&bdev_lock);
  615. return bdev;
  616. }
  617. spin_unlock(&bdev_lock);
  618. bdev = bdget(inode->i_rdev);
  619. if (bdev) {
  620. spin_lock(&bdev_lock);
  621. if (!inode->i_bdev) {
  622. /*
  623. * We take an additional reference to bd_inode,
  624. * and it's released in clear_inode() of inode.
  625. * So, we can access it via ->i_mapping always
  626. * without igrab().
  627. */
  628. bdgrab(bdev);
  629. inode->i_bdev = bdev;
  630. inode->i_mapping = bdev->bd_inode->i_mapping;
  631. list_add(&inode->i_devices, &bdev->bd_inodes);
  632. }
  633. spin_unlock(&bdev_lock);
  634. }
  635. return bdev;
  636. }
  637. /* Call when you free inode */
  638. void bd_forget(struct inode *inode)
  639. {
  640. struct block_device *bdev = NULL;
  641. spin_lock(&bdev_lock);
  642. if (!sb_is_blkdev_sb(inode->i_sb))
  643. bdev = inode->i_bdev;
  644. __bd_forget(inode);
  645. spin_unlock(&bdev_lock);
  646. if (bdev)
  647. bdput(bdev);
  648. }
  649. /**
  650. * bd_may_claim - test whether a block device can be claimed
  651. * @bdev: block device of interest
  652. * @whole: whole block device containing @bdev, may equal @bdev
  653. * @holder: holder trying to claim @bdev
  654. *
  655. * Test whether @bdev can be claimed by @holder.
  656. *
  657. * CONTEXT:
  658. * spin_lock(&bdev_lock).
  659. *
  660. * RETURNS:
  661. * %true if @bdev can be claimed, %false otherwise.
  662. */
  663. static bool bd_may_claim(struct block_device *bdev, struct block_device *whole,
  664. void *holder)
  665. {
  666. if (bdev->bd_holder == holder)
  667. return true; /* already a holder */
  668. else if (bdev->bd_holder != NULL)
  669. return false; /* held by someone else */
  670. else if (bdev->bd_contains == bdev)
  671. return true; /* is a whole device which isn't held */
  672. else if (whole->bd_holder == bd_may_claim)
  673. return true; /* is a partition of a device that is being partitioned */
  674. else if (whole->bd_holder != NULL)
  675. return false; /* is a partition of a held device */
  676. else
  677. return true; /* is a partition of an un-held device */
  678. }
  679. /**
  680. * bd_prepare_to_claim - prepare to claim a block device
  681. * @bdev: block device of interest
  682. * @whole: the whole device containing @bdev, may equal @bdev
  683. * @holder: holder trying to claim @bdev
  684. *
  685. * Prepare to claim @bdev. This function fails if @bdev is already
  686. * claimed by another holder and waits if another claiming is in
  687. * progress. This function doesn't actually claim. On successful
  688. * return, the caller has ownership of bd_claiming and bd_holder[s].
  689. *
  690. * CONTEXT:
  691. * spin_lock(&bdev_lock). Might release bdev_lock, sleep and regrab
  692. * it multiple times.
  693. *
  694. * RETURNS:
  695. * 0 if @bdev can be claimed, -EBUSY otherwise.
  696. */
  697. static int bd_prepare_to_claim(struct block_device *bdev,
  698. struct block_device *whole, void *holder)
  699. {
  700. retry:
  701. /* if someone else claimed, fail */
  702. if (!bd_may_claim(bdev, whole, holder))
  703. return -EBUSY;
  704. /* if claiming is already in progress, wait for it to finish */
  705. if (whole->bd_claiming) {
  706. wait_queue_head_t *wq = bit_waitqueue(&whole->bd_claiming, 0);
  707. DEFINE_WAIT(wait);
  708. prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
  709. spin_unlock(&bdev_lock);
  710. schedule();
  711. finish_wait(wq, &wait);
  712. spin_lock(&bdev_lock);
  713. goto retry;
  714. }
  715. /* yay, all mine */
  716. return 0;
  717. }
  718. /**
  719. * bd_start_claiming - start claiming a block device
  720. * @bdev: block device of interest
  721. * @holder: holder trying to claim @bdev
  722. *
  723. * @bdev is about to be opened exclusively. Check @bdev can be opened
  724. * exclusively and mark that an exclusive open is in progress. Each
  725. * successful call to this function must be matched with a call to
  726. * either bd_finish_claiming() or bd_abort_claiming() (which do not
  727. * fail).
  728. *
  729. * This function is used to gain exclusive access to the block device
  730. * without actually causing other exclusive open attempts to fail. It
  731. * should be used when the open sequence itself requires exclusive
  732. * access but may subsequently fail.
  733. *
  734. * CONTEXT:
  735. * Might sleep.
  736. *
  737. * RETURNS:
  738. * Pointer to the block device containing @bdev on success, ERR_PTR()
  739. * value on failure.
  740. */
  741. static struct block_device *bd_start_claiming(struct block_device *bdev,
  742. void *holder)
  743. {
  744. struct gendisk *disk;
  745. struct block_device *whole;
  746. int partno, err;
  747. might_sleep();
  748. /*
  749. * @bdev might not have been initialized properly yet, look up
  750. * and grab the outer block device the hard way.
  751. */
  752. disk = get_gendisk(bdev->bd_dev, &partno);
  753. if (!disk)
  754. return ERR_PTR(-ENXIO);
  755. /*
  756. * Normally, @bdev should equal what's returned from bdget_disk()
  757. * if partno is 0; however, some drivers (floppy) use multiple
  758. * bdev's for the same physical device and @bdev may be one of the
  759. * aliases. Keep @bdev if partno is 0. This means claimer
  760. * tracking is broken for those devices but it has always been that
  761. * way.
  762. */
  763. if (partno)
  764. whole = bdget_disk(disk, 0);
  765. else
  766. whole = bdgrab(bdev);
  767. module_put(disk->fops->owner);
  768. put_disk(disk);
  769. if (!whole)
  770. return ERR_PTR(-ENOMEM);
  771. /* prepare to claim, if successful, mark claiming in progress */
  772. spin_lock(&bdev_lock);
  773. err = bd_prepare_to_claim(bdev, whole, holder);
  774. if (err == 0) {
  775. whole->bd_claiming = holder;
  776. spin_unlock(&bdev_lock);
  777. return whole;
  778. } else {
  779. spin_unlock(&bdev_lock);
  780. bdput(whole);
  781. return ERR_PTR(err);
  782. }
  783. }
  784. #ifdef CONFIG_SYSFS
  785. struct bd_holder_disk {
  786. struct list_head list;
  787. struct gendisk *disk;
  788. int refcnt;
  789. };
  790. static struct bd_holder_disk *bd_find_holder_disk(struct block_device *bdev,
  791. struct gendisk *disk)
  792. {
  793. struct bd_holder_disk *holder;
  794. list_for_each_entry(holder, &bdev->bd_holder_disks, list)
  795. if (holder->disk == disk)
  796. return holder;
  797. return NULL;
  798. }
  799. static int add_symlink(struct kobject *from, struct kobject *to)
  800. {
  801. return sysfs_create_link(from, to, kobject_name(to));
  802. }
  803. static void del_symlink(struct kobject *from, struct kobject *to)
  804. {
  805. sysfs_remove_link(from, kobject_name(to));
  806. }
  807. /**
  808. * bd_link_disk_holder - create symlinks between holding disk and slave bdev
  809. * @bdev: the claimed slave bdev
  810. * @disk: the holding disk
  811. *
  812. * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
  813. *
  814. * This functions creates the following sysfs symlinks.
  815. *
  816. * - from "slaves" directory of the holder @disk to the claimed @bdev
  817. * - from "holders" directory of the @bdev to the holder @disk
  818. *
  819. * For example, if /dev/dm-0 maps to /dev/sda and disk for dm-0 is
  820. * passed to bd_link_disk_holder(), then:
  821. *
  822. * /sys/block/dm-0/slaves/sda --> /sys/block/sda
  823. * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
  824. *
  825. * The caller must have claimed @bdev before calling this function and
  826. * ensure that both @bdev and @disk are valid during the creation and
  827. * lifetime of these symlinks.
  828. *
  829. * CONTEXT:
  830. * Might sleep.
  831. *
  832. * RETURNS:
  833. * 0 on success, -errno on failure.
  834. */
  835. int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk)
  836. {
  837. struct bd_holder_disk *holder;
  838. int ret = 0;
  839. mutex_lock(&bdev->bd_mutex);
  840. WARN_ON_ONCE(!bdev->bd_holder);
  841. /* FIXME: remove the following once add_disk() handles errors */
  842. if (WARN_ON(!disk->slave_dir || !bdev->bd_part->holder_dir))
  843. goto out_unlock;
  844. holder = bd_find_holder_disk(bdev, disk);
  845. if (holder) {
  846. holder->refcnt++;
  847. goto out_unlock;
  848. }
  849. holder = kzalloc(sizeof(*holder), GFP_KERNEL);
  850. if (!holder) {
  851. ret = -ENOMEM;
  852. goto out_unlock;
  853. }
  854. INIT_LIST_HEAD(&holder->list);
  855. holder->disk = disk;
  856. holder->refcnt = 1;
  857. ret = add_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
  858. if (ret)
  859. goto out_free;
  860. ret = add_symlink(bdev->bd_part->holder_dir, &disk_to_dev(disk)->kobj);
  861. if (ret)
  862. goto out_del;
  863. /*
  864. * bdev could be deleted beneath us which would implicitly destroy
  865. * the holder directory. Hold on to it.
  866. */
  867. kobject_get(bdev->bd_part->holder_dir);
  868. list_add(&holder->list, &bdev->bd_holder_disks);
  869. goto out_unlock;
  870. out_del:
  871. del_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
  872. out_free:
  873. kfree(holder);
  874. out_unlock:
  875. mutex_unlock(&bdev->bd_mutex);
  876. return ret;
  877. }
  878. EXPORT_SYMBOL_GPL(bd_link_disk_holder);
  879. /**
  880. * bd_unlink_disk_holder - destroy symlinks created by bd_link_disk_holder()
  881. * @bdev: the calimed slave bdev
  882. * @disk: the holding disk
  883. *
  884. * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
  885. *
  886. * CONTEXT:
  887. * Might sleep.
  888. */
  889. void bd_unlink_disk_holder(struct block_device *bdev, struct gendisk *disk)
  890. {
  891. struct bd_holder_disk *holder;
  892. mutex_lock(&bdev->bd_mutex);
  893. holder = bd_find_holder_disk(bdev, disk);
  894. if (!WARN_ON_ONCE(holder == NULL) && !--holder->refcnt) {
  895. del_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
  896. del_symlink(bdev->bd_part->holder_dir,
  897. &disk_to_dev(disk)->kobj);
  898. kobject_put(bdev->bd_part->holder_dir);
  899. list_del_init(&holder->list);
  900. kfree(holder);
  901. }
  902. mutex_unlock(&bdev->bd_mutex);
  903. }
  904. EXPORT_SYMBOL_GPL(bd_unlink_disk_holder);
  905. #endif
  906. /**
  907. * flush_disk - invalidates all buffer-cache entries on a disk
  908. *
  909. * @bdev: struct block device to be flushed
  910. * @kill_dirty: flag to guide handling of dirty inodes
  911. *
  912. * Invalidates all buffer-cache entries on a disk. It should be called
  913. * when a disk has been changed -- either by a media change or online
  914. * resize.
  915. */
  916. static void flush_disk(struct block_device *bdev, bool kill_dirty)
  917. {
  918. if (__invalidate_device(bdev, kill_dirty)) {
  919. printk(KERN_WARNING "VFS: busy inodes on changed media or "
  920. "resized disk %s\n",
  921. bdev->bd_disk ? bdev->bd_disk->disk_name : "");
  922. }
  923. if (!bdev->bd_disk)
  924. return;
  925. if (disk_part_scan_enabled(bdev->bd_disk))
  926. bdev->bd_invalidated = 1;
  927. }
  928. /**
  929. * check_disk_size_change - checks for disk size change and adjusts bdev size.
  930. * @disk: struct gendisk to check
  931. * @bdev: struct bdev to adjust.
  932. *
  933. * This routine checks to see if the bdev size does not match the disk size
  934. * and adjusts it if it differs.
  935. */
  936. void check_disk_size_change(struct gendisk *disk, struct block_device *bdev)
  937. {
  938. loff_t disk_size, bdev_size;
  939. disk_size = (loff_t)get_capacity(disk) << 9;
  940. bdev_size = i_size_read(bdev->bd_inode);
  941. if (disk_size != bdev_size) {
  942. printk(KERN_INFO
  943. "%s: detected capacity change from %lld to %lld\n",
  944. disk->disk_name, bdev_size, disk_size);
  945. i_size_write(bdev->bd_inode, disk_size);
  946. flush_disk(bdev, false);
  947. }
  948. }
  949. EXPORT_SYMBOL(check_disk_size_change);
  950. /**
  951. * revalidate_disk - wrapper for lower-level driver's revalidate_disk call-back
  952. * @disk: struct gendisk to be revalidated
  953. *
  954. * This routine is a wrapper for lower-level driver's revalidate_disk
  955. * call-backs. It is used to do common pre and post operations needed
  956. * for all revalidate_disk operations.
  957. */
  958. int revalidate_disk(struct gendisk *disk)
  959. {
  960. struct block_device *bdev;
  961. int ret = 0;
  962. if (disk->fops->revalidate_disk)
  963. ret = disk->fops->revalidate_disk(disk);
  964. blk_integrity_revalidate(disk);
  965. bdev = bdget_disk(disk, 0);
  966. if (!bdev)
  967. return ret;
  968. mutex_lock(&bdev->bd_mutex);
  969. check_disk_size_change(disk, bdev);
  970. bdev->bd_invalidated = 0;
  971. mutex_unlock(&bdev->bd_mutex);
  972. bdput(bdev);
  973. return ret;
  974. }
  975. EXPORT_SYMBOL(revalidate_disk);
  976. /*
  977. * This routine checks whether a removable media has been changed,
  978. * and invalidates all buffer-cache-entries in that case. This
  979. * is a relatively slow routine, so we have to try to minimize using
  980. * it. Thus it is called only upon a 'mount' or 'open'. This
  981. * is the best way of combining speed and utility, I think.
  982. * People changing diskettes in the middle of an operation deserve
  983. * to lose :-)
  984. */
  985. int check_disk_change(struct block_device *bdev)
  986. {
  987. struct gendisk *disk = bdev->bd_disk;
  988. const struct block_device_operations *bdops = disk->fops;
  989. unsigned int events;
  990. events = disk_clear_events(disk, DISK_EVENT_MEDIA_CHANGE |
  991. DISK_EVENT_EJECT_REQUEST);
  992. if (!(events & DISK_EVENT_MEDIA_CHANGE))
  993. return 0;
  994. flush_disk(bdev, true);
  995. if (bdops->revalidate_disk)
  996. bdops->revalidate_disk(bdev->bd_disk);
  997. return 1;
  998. }
  999. EXPORT_SYMBOL(check_disk_change);
  1000. void bd_set_size(struct block_device *bdev, loff_t size)
  1001. {
  1002. unsigned bsize = bdev_logical_block_size(bdev);
  1003. inode_lock(bdev->bd_inode);
  1004. i_size_write(bdev->bd_inode, size);
  1005. inode_unlock(bdev->bd_inode);
  1006. while (bsize < PAGE_CACHE_SIZE) {
  1007. if (size & bsize)
  1008. break;
  1009. bsize <<= 1;
  1010. }
  1011. bdev->bd_block_size = bsize;
  1012. bdev->bd_inode->i_blkbits = blksize_bits(bsize);
  1013. }
  1014. EXPORT_SYMBOL(bd_set_size);
  1015. static void __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part);
  1016. /*
  1017. * bd_mutex locking:
  1018. *
  1019. * mutex_lock(part->bd_mutex)
  1020. * mutex_lock_nested(whole->bd_mutex, 1)
  1021. */
  1022. static int __blkdev_get(struct block_device *bdev, fmode_t mode, int for_part)
  1023. {
  1024. struct gendisk *disk;
  1025. struct module *owner;
  1026. int ret;
  1027. int partno;
  1028. int perm = 0;
  1029. if (mode & FMODE_READ)
  1030. perm |= MAY_READ;
  1031. if (mode & FMODE_WRITE)
  1032. perm |= MAY_WRITE;
  1033. /*
  1034. * hooks: /n/, see "layering violations".
  1035. */
  1036. if (!for_part) {
  1037. ret = devcgroup_inode_permission(bdev->bd_inode, perm);
  1038. if (ret != 0) {
  1039. bdput(bdev);
  1040. return ret;
  1041. }
  1042. }
  1043. restart:
  1044. ret = -ENXIO;
  1045. disk = get_gendisk(bdev->bd_dev, &partno);
  1046. if (!disk)
  1047. goto out;
  1048. owner = disk->fops->owner;
  1049. disk_block_events(disk);
  1050. mutex_lock_nested(&bdev->bd_mutex, for_part);
  1051. if (!bdev->bd_openers) {
  1052. bdev->bd_disk = disk;
  1053. bdev->bd_queue = disk->queue;
  1054. bdev->bd_contains = bdev;
  1055. bdev->bd_inode->i_flags = disk->fops->direct_access ? S_DAX : 0;
  1056. if (!partno) {
  1057. ret = -ENXIO;
  1058. bdev->bd_part = disk_get_part(disk, partno);
  1059. if (!bdev->bd_part)
  1060. goto out_clear;
  1061. ret = 0;
  1062. if (disk->fops->open) {
  1063. ret = disk->fops->open(bdev, mode);
  1064. if (ret == -ERESTARTSYS) {
  1065. /* Lost a race with 'disk' being
  1066. * deleted, try again.
  1067. * See md.c
  1068. */
  1069. disk_put_part(bdev->bd_part);
  1070. bdev->bd_part = NULL;
  1071. bdev->bd_disk = NULL;
  1072. bdev->bd_queue = NULL;
  1073. mutex_unlock(&bdev->bd_mutex);
  1074. disk_unblock_events(disk);
  1075. put_disk(disk);
  1076. module_put(owner);
  1077. goto restart;
  1078. }
  1079. }
  1080. if (!ret) {
  1081. bd_set_size(bdev,(loff_t)get_capacity(disk)<<9);
  1082. if (!blkdev_dax_capable(bdev))
  1083. bdev->bd_inode->i_flags &= ~S_DAX;
  1084. }
  1085. /*
  1086. * If the device is invalidated, rescan partition
  1087. * if open succeeded or failed with -ENOMEDIUM.
  1088. * The latter is necessary to prevent ghost
  1089. * partitions on a removed medium.
  1090. */
  1091. if (bdev->bd_invalidated) {
  1092. if (!ret)
  1093. rescan_partitions(disk, bdev);
  1094. else if (ret == -ENOMEDIUM)
  1095. invalidate_partitions(disk, bdev);
  1096. }
  1097. if (ret)
  1098. goto out_clear;
  1099. } else {
  1100. struct block_device *whole;
  1101. whole = bdget_disk(disk, 0);
  1102. ret = -ENOMEM;
  1103. if (!whole)
  1104. goto out_clear;
  1105. BUG_ON(for_part);
  1106. ret = __blkdev_get(whole, mode, 1);
  1107. if (ret)
  1108. goto out_clear;
  1109. bdev->bd_contains = whole;
  1110. bdev->bd_part = disk_get_part(disk, partno);
  1111. if (!(disk->flags & GENHD_FL_UP) ||
  1112. !bdev->bd_part || !bdev->bd_part->nr_sects) {
  1113. ret = -ENXIO;
  1114. goto out_clear;
  1115. }
  1116. bd_set_size(bdev, (loff_t)bdev->bd_part->nr_sects << 9);
  1117. if (!blkdev_dax_capable(bdev))
  1118. bdev->bd_inode->i_flags &= ~S_DAX;
  1119. }
  1120. } else {
  1121. if (bdev->bd_contains == bdev) {
  1122. ret = 0;
  1123. if (bdev->bd_disk->fops->open)
  1124. ret = bdev->bd_disk->fops->open(bdev, mode);
  1125. /* the same as first opener case, read comment there */
  1126. if (bdev->bd_invalidated) {
  1127. if (!ret)
  1128. rescan_partitions(bdev->bd_disk, bdev);
  1129. else if (ret == -ENOMEDIUM)
  1130. invalidate_partitions(bdev->bd_disk, bdev);
  1131. }
  1132. if (ret)
  1133. goto out_unlock_bdev;
  1134. }
  1135. /* only one opener holds refs to the module and disk */
  1136. put_disk(disk);
  1137. module_put(owner);
  1138. }
  1139. bdev->bd_openers++;
  1140. if (for_part)
  1141. bdev->bd_part_count++;
  1142. mutex_unlock(&bdev->bd_mutex);
  1143. disk_unblock_events(disk);
  1144. return 0;
  1145. out_clear:
  1146. disk_put_part(bdev->bd_part);
  1147. bdev->bd_disk = NULL;
  1148. bdev->bd_part = NULL;
  1149. bdev->bd_queue = NULL;
  1150. if (bdev != bdev->bd_contains)
  1151. __blkdev_put(bdev->bd_contains, mode, 1);
  1152. bdev->bd_contains = NULL;
  1153. out_unlock_bdev:
  1154. mutex_unlock(&bdev->bd_mutex);
  1155. disk_unblock_events(disk);
  1156. put_disk(disk);
  1157. module_put(owner);
  1158. out:
  1159. bdput(bdev);
  1160. return ret;
  1161. }
  1162. /**
  1163. * blkdev_get - open a block device
  1164. * @bdev: block_device to open
  1165. * @mode: FMODE_* mask
  1166. * @holder: exclusive holder identifier
  1167. *
  1168. * Open @bdev with @mode. If @mode includes %FMODE_EXCL, @bdev is
  1169. * open with exclusive access. Specifying %FMODE_EXCL with %NULL
  1170. * @holder is invalid. Exclusive opens may nest for the same @holder.
  1171. *
  1172. * On success, the reference count of @bdev is unchanged. On failure,
  1173. * @bdev is put.
  1174. *
  1175. * CONTEXT:
  1176. * Might sleep.
  1177. *
  1178. * RETURNS:
  1179. * 0 on success, -errno on failure.
  1180. */
  1181. int blkdev_get(struct block_device *bdev, fmode_t mode, void *holder)
  1182. {
  1183. struct block_device *whole = NULL;
  1184. int res;
  1185. WARN_ON_ONCE((mode & FMODE_EXCL) && !holder);
  1186. if ((mode & FMODE_EXCL) && holder) {
  1187. whole = bd_start_claiming(bdev, holder);
  1188. if (IS_ERR(whole)) {
  1189. bdput(bdev);
  1190. return PTR_ERR(whole);
  1191. }
  1192. }
  1193. res = __blkdev_get(bdev, mode, 0);
  1194. if (whole) {
  1195. struct gendisk *disk = whole->bd_disk;
  1196. /* finish claiming */
  1197. mutex_lock(&bdev->bd_mutex);
  1198. spin_lock(&bdev_lock);
  1199. if (!res) {
  1200. BUG_ON(!bd_may_claim(bdev, whole, holder));
  1201. /*
  1202. * Note that for a whole device bd_holders
  1203. * will be incremented twice, and bd_holder
  1204. * will be set to bd_may_claim before being
  1205. * set to holder
  1206. */
  1207. whole->bd_holders++;
  1208. whole->bd_holder = bd_may_claim;
  1209. bdev->bd_holders++;
  1210. bdev->bd_holder = holder;
  1211. }
  1212. /* tell others that we're done */
  1213. BUG_ON(whole->bd_claiming != holder);
  1214. whole->bd_claiming = NULL;
  1215. wake_up_bit(&whole->bd_claiming, 0);
  1216. spin_unlock(&bdev_lock);
  1217. /*
  1218. * Block event polling for write claims if requested. Any
  1219. * write holder makes the write_holder state stick until
  1220. * all are released. This is good enough and tracking
  1221. * individual writeable reference is too fragile given the
  1222. * way @mode is used in blkdev_get/put().
  1223. */
  1224. if (!res && (mode & FMODE_WRITE) && !bdev->bd_write_holder &&
  1225. (disk->flags & GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE)) {
  1226. bdev->bd_write_holder = true;
  1227. disk_block_events(disk);
  1228. }
  1229. mutex_unlock(&bdev->bd_mutex);
  1230. bdput(whole);
  1231. }
  1232. return res;
  1233. }
  1234. EXPORT_SYMBOL(blkdev_get);
  1235. /**
  1236. * blkdev_get_by_path - open a block device by name
  1237. * @path: path to the block device to open
  1238. * @mode: FMODE_* mask
  1239. * @holder: exclusive holder identifier
  1240. *
  1241. * Open the blockdevice described by the device file at @path. @mode
  1242. * and @holder are identical to blkdev_get().
  1243. *
  1244. * On success, the returned block_device has reference count of one.
  1245. *
  1246. * CONTEXT:
  1247. * Might sleep.
  1248. *
  1249. * RETURNS:
  1250. * Pointer to block_device on success, ERR_PTR(-errno) on failure.
  1251. */
  1252. struct block_device *blkdev_get_by_path(const char *path, fmode_t mode,
  1253. void *holder)
  1254. {
  1255. struct block_device *bdev;
  1256. int err;
  1257. bdev = lookup_bdev(path);
  1258. if (IS_ERR(bdev))
  1259. return bdev;
  1260. err = blkdev_get(bdev, mode, holder);
  1261. if (err)
  1262. return ERR_PTR(err);
  1263. if ((mode & FMODE_WRITE) && bdev_read_only(bdev)) {
  1264. blkdev_put(bdev, mode);
  1265. return ERR_PTR(-EACCES);
  1266. }
  1267. return bdev;
  1268. }
  1269. EXPORT_SYMBOL(blkdev_get_by_path);
  1270. /**
  1271. * blkdev_get_by_dev - open a block device by device number
  1272. * @dev: device number of block device to open
  1273. * @mode: FMODE_* mask
  1274. * @holder: exclusive holder identifier
  1275. *
  1276. * Open the blockdevice described by device number @dev. @mode and
  1277. * @holder are identical to blkdev_get().
  1278. *
  1279. * Use it ONLY if you really do not have anything better - i.e. when
  1280. * you are behind a truly sucky interface and all you are given is a
  1281. * device number. _Never_ to be used for internal purposes. If you
  1282. * ever need it - reconsider your API.
  1283. *
  1284. * On success, the returned block_device has reference count of one.
  1285. *
  1286. * CONTEXT:
  1287. * Might sleep.
  1288. *
  1289. * RETURNS:
  1290. * Pointer to block_device on success, ERR_PTR(-errno) on failure.
  1291. */
  1292. struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder)
  1293. {
  1294. struct block_device *bdev;
  1295. int err;
  1296. bdev = bdget(dev);
  1297. if (!bdev)
  1298. return ERR_PTR(-ENOMEM);
  1299. err = blkdev_get(bdev, mode, holder);
  1300. if (err)
  1301. return ERR_PTR(err);
  1302. return bdev;
  1303. }
  1304. EXPORT_SYMBOL(blkdev_get_by_dev);
  1305. static int blkdev_open(struct inode * inode, struct file * filp)
  1306. {
  1307. struct block_device *bdev;
  1308. /*
  1309. * Preserve backwards compatibility and allow large file access
  1310. * even if userspace doesn't ask for it explicitly. Some mkfs
  1311. * binary needs it. We might want to drop this workaround
  1312. * during an unstable branch.
  1313. */
  1314. filp->f_flags |= O_LARGEFILE;
  1315. if (filp->f_flags & O_NDELAY)
  1316. filp->f_mode |= FMODE_NDELAY;
  1317. if (filp->f_flags & O_EXCL)
  1318. filp->f_mode |= FMODE_EXCL;
  1319. if ((filp->f_flags & O_ACCMODE) == 3)
  1320. filp->f_mode |= FMODE_WRITE_IOCTL;
  1321. bdev = bd_acquire(inode);
  1322. if (bdev == NULL)
  1323. return -ENOMEM;
  1324. filp->f_mapping = bdev->bd_inode->i_mapping;
  1325. return blkdev_get(bdev, filp->f_mode, filp);
  1326. }
  1327. static void __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part)
  1328. {
  1329. struct gendisk *disk = bdev->bd_disk;
  1330. struct block_device *victim = NULL;
  1331. mutex_lock_nested(&bdev->bd_mutex, for_part);
  1332. if (for_part)
  1333. bdev->bd_part_count--;
  1334. if (!--bdev->bd_openers) {
  1335. WARN_ON_ONCE(bdev->bd_holders);
  1336. sync_blockdev(bdev);
  1337. kill_bdev(bdev);
  1338. bdev_write_inode(bdev);
  1339. /*
  1340. * Detaching bdev inode from its wb in __destroy_inode()
  1341. * is too late: the queue which embeds its bdi (along with
  1342. * root wb) can be gone as soon as we put_disk() below.
  1343. */
  1344. inode_detach_wb(bdev->bd_inode);
  1345. }
  1346. if (bdev->bd_contains == bdev) {
  1347. if (disk->fops->release)
  1348. disk->fops->release(disk, mode);
  1349. }
  1350. if (!bdev->bd_openers) {
  1351. struct module *owner = disk->fops->owner;
  1352. disk_put_part(bdev->bd_part);
  1353. bdev->bd_part = NULL;
  1354. bdev->bd_disk = NULL;
  1355. if (bdev != bdev->bd_contains)
  1356. victim = bdev->bd_contains;
  1357. bdev->bd_contains = NULL;
  1358. put_disk(disk);
  1359. module_put(owner);
  1360. }
  1361. mutex_unlock(&bdev->bd_mutex);
  1362. bdput(bdev);
  1363. if (victim)
  1364. __blkdev_put(victim, mode, 1);
  1365. }
  1366. void blkdev_put(struct block_device *bdev, fmode_t mode)
  1367. {
  1368. mutex_lock(&bdev->bd_mutex);
  1369. if (mode & FMODE_EXCL) {
  1370. bool bdev_free;
  1371. /*
  1372. * Release a claim on the device. The holder fields
  1373. * are protected with bdev_lock. bd_mutex is to
  1374. * synchronize disk_holder unlinking.
  1375. */
  1376. spin_lock(&bdev_lock);
  1377. WARN_ON_ONCE(--bdev->bd_holders < 0);
  1378. WARN_ON_ONCE(--bdev->bd_contains->bd_holders < 0);
  1379. /* bd_contains might point to self, check in a separate step */
  1380. if ((bdev_free = !bdev->bd_holders))
  1381. bdev->bd_holder = NULL;
  1382. if (!bdev->bd_contains->bd_holders)
  1383. bdev->bd_contains->bd_holder = NULL;
  1384. spin_unlock(&bdev_lock);
  1385. /*
  1386. * If this was the last claim, remove holder link and
  1387. * unblock evpoll if it was a write holder.
  1388. */
  1389. if (bdev_free && bdev->bd_write_holder) {
  1390. disk_unblock_events(bdev->bd_disk);
  1391. bdev->bd_write_holder = false;
  1392. }
  1393. }
  1394. /*
  1395. * Trigger event checking and tell drivers to flush MEDIA_CHANGE
  1396. * event. This is to ensure detection of media removal commanded
  1397. * from userland - e.g. eject(1).
  1398. */
  1399. disk_flush_events(bdev->bd_disk, DISK_EVENT_MEDIA_CHANGE);
  1400. mutex_unlock(&bdev->bd_mutex);
  1401. __blkdev_put(bdev, mode, 0);
  1402. }
  1403. EXPORT_SYMBOL(blkdev_put);
  1404. static int blkdev_close(struct inode * inode, struct file * filp)
  1405. {
  1406. struct block_device *bdev = I_BDEV(bdev_file_inode(filp));
  1407. blkdev_put(bdev, filp->f_mode);
  1408. return 0;
  1409. }
  1410. static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg)
  1411. {
  1412. struct block_device *bdev = I_BDEV(bdev_file_inode(file));
  1413. fmode_t mode = file->f_mode;
  1414. /*
  1415. * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
  1416. * to updated it before every ioctl.
  1417. */
  1418. if (file->f_flags & O_NDELAY)
  1419. mode |= FMODE_NDELAY;
  1420. else
  1421. mode &= ~FMODE_NDELAY;
  1422. return blkdev_ioctl(bdev, mode, cmd, arg);
  1423. }
  1424. /*
  1425. * Write data to the block device. Only intended for the block device itself
  1426. * and the raw driver which basically is a fake block device.
  1427. *
  1428. * Does not take i_mutex for the write and thus is not for general purpose
  1429. * use.
  1430. */
  1431. ssize_t blkdev_write_iter(struct kiocb *iocb, struct iov_iter *from)
  1432. {
  1433. struct file *file = iocb->ki_filp;
  1434. struct inode *bd_inode = bdev_file_inode(file);
  1435. loff_t size = i_size_read(bd_inode);
  1436. struct blk_plug plug;
  1437. ssize_t ret;
  1438. if (bdev_read_only(I_BDEV(bd_inode)))
  1439. return -EPERM;
  1440. if (!iov_iter_count(from))
  1441. return 0;
  1442. if (iocb->ki_pos >= size)
  1443. return -ENOSPC;
  1444. iov_iter_truncate(from, size - iocb->ki_pos);
  1445. blk_start_plug(&plug);
  1446. ret = __generic_file_write_iter(iocb, from);
  1447. if (ret > 0) {
  1448. ssize_t err;
  1449. err = generic_write_sync(file, iocb->ki_pos - ret, ret);
  1450. if (err < 0)
  1451. ret = err;
  1452. }
  1453. blk_finish_plug(&plug);
  1454. return ret;
  1455. }
  1456. EXPORT_SYMBOL_GPL(blkdev_write_iter);
  1457. ssize_t blkdev_read_iter(struct kiocb *iocb, struct iov_iter *to)
  1458. {
  1459. struct file *file = iocb->ki_filp;
  1460. struct inode *bd_inode = bdev_file_inode(file);
  1461. loff_t size = i_size_read(bd_inode);
  1462. loff_t pos = iocb->ki_pos;
  1463. if (pos >= size)
  1464. return 0;
  1465. size -= pos;
  1466. iov_iter_truncate(to, size);
  1467. return generic_file_read_iter(iocb, to);
  1468. }
  1469. EXPORT_SYMBOL_GPL(blkdev_read_iter);
  1470. /*
  1471. * Try to release a page associated with block device when the system
  1472. * is under memory pressure.
  1473. */
  1474. static int blkdev_releasepage(struct page *page, gfp_t wait)
  1475. {
  1476. struct super_block *super = BDEV_I(page->mapping->host)->bdev.bd_super;
  1477. if (super && super->s_op->bdev_try_to_free_page)
  1478. return super->s_op->bdev_try_to_free_page(super, page, wait);
  1479. return try_to_free_buffers(page);
  1480. }
  1481. static const struct address_space_operations def_blk_aops = {
  1482. .readpage = blkdev_readpage,
  1483. .readpages = blkdev_readpages,
  1484. .writepage = blkdev_writepage,
  1485. .write_begin = blkdev_write_begin,
  1486. .write_end = blkdev_write_end,
  1487. .writepages = generic_writepages,
  1488. .releasepage = blkdev_releasepage,
  1489. .direct_IO = blkdev_direct_IO,
  1490. .is_dirty_writeback = buffer_check_dirty_writeback,
  1491. };
  1492. #ifdef CONFIG_FS_DAX
  1493. /*
  1494. * In the raw block case we do not need to contend with truncation nor
  1495. * unwritten file extents. Without those concerns there is no need for
  1496. * additional locking beyond the mmap_sem context that these routines
  1497. * are already executing under.
  1498. *
  1499. * Note, there is no protection if the block device is dynamically
  1500. * resized (partition grow/shrink) during a fault. A stable block device
  1501. * size is already not enforced in the blkdev_direct_IO path.
  1502. *
  1503. * For DAX, it is the responsibility of the block device driver to
  1504. * ensure the whole-disk device size is stable while requests are in
  1505. * flight.
  1506. *
  1507. * Finally, unlike the filemap_page_mkwrite() case there is no
  1508. * filesystem superblock to sync against freezing. We still include a
  1509. * pfn_mkwrite callback for dax drivers to receive write fault
  1510. * notifications.
  1511. */
  1512. static int blkdev_dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1513. {
  1514. return __dax_fault(vma, vmf, blkdev_get_block, NULL);
  1515. }
  1516. static int blkdev_dax_pmd_fault(struct vm_area_struct *vma, unsigned long addr,
  1517. pmd_t *pmd, unsigned int flags)
  1518. {
  1519. return __dax_pmd_fault(vma, addr, pmd, flags, blkdev_get_block, NULL);
  1520. }
  1521. static void blkdev_vm_open(struct vm_area_struct *vma)
  1522. {
  1523. struct inode *bd_inode = bdev_file_inode(vma->vm_file);
  1524. struct block_device *bdev = I_BDEV(bd_inode);
  1525. inode_lock(bd_inode);
  1526. bdev->bd_map_count++;
  1527. inode_unlock(bd_inode);
  1528. }
  1529. static void blkdev_vm_close(struct vm_area_struct *vma)
  1530. {
  1531. struct inode *bd_inode = bdev_file_inode(vma->vm_file);
  1532. struct block_device *bdev = I_BDEV(bd_inode);
  1533. inode_lock(bd_inode);
  1534. bdev->bd_map_count--;
  1535. inode_unlock(bd_inode);
  1536. }
  1537. static const struct vm_operations_struct blkdev_dax_vm_ops = {
  1538. .open = blkdev_vm_open,
  1539. .close = blkdev_vm_close,
  1540. .fault = blkdev_dax_fault,
  1541. .pmd_fault = blkdev_dax_pmd_fault,
  1542. .pfn_mkwrite = blkdev_dax_fault,
  1543. };
  1544. static const struct vm_operations_struct blkdev_default_vm_ops = {
  1545. .open = blkdev_vm_open,
  1546. .close = blkdev_vm_close,
  1547. .fault = filemap_fault,
  1548. .map_pages = filemap_map_pages,
  1549. };
  1550. static int blkdev_mmap(struct file *file, struct vm_area_struct *vma)
  1551. {
  1552. struct inode *bd_inode = bdev_file_inode(file);
  1553. struct block_device *bdev = I_BDEV(bd_inode);
  1554. file_accessed(file);
  1555. inode_lock(bd_inode);
  1556. bdev->bd_map_count++;
  1557. if (IS_DAX(bd_inode)) {
  1558. vma->vm_ops = &blkdev_dax_vm_ops;
  1559. vma->vm_flags |= VM_MIXEDMAP | VM_HUGEPAGE;
  1560. } else {
  1561. vma->vm_ops = &blkdev_default_vm_ops;
  1562. }
  1563. inode_unlock(bd_inode);
  1564. return 0;
  1565. }
  1566. #else
  1567. #define blkdev_mmap generic_file_mmap
  1568. #endif
  1569. const struct file_operations def_blk_fops = {
  1570. .open = blkdev_open,
  1571. .release = blkdev_close,
  1572. .llseek = block_llseek,
  1573. .read_iter = blkdev_read_iter,
  1574. .write_iter = blkdev_write_iter,
  1575. .mmap = blkdev_mmap,
  1576. .fsync = blkdev_fsync,
  1577. .unlocked_ioctl = block_ioctl,
  1578. #ifdef CONFIG_COMPAT
  1579. .compat_ioctl = compat_blkdev_ioctl,
  1580. #endif
  1581. .splice_read = generic_file_splice_read,
  1582. .splice_write = iter_file_splice_write,
  1583. };
  1584. int ioctl_by_bdev(struct block_device *bdev, unsigned cmd, unsigned long arg)
  1585. {
  1586. int res;
  1587. mm_segment_t old_fs = get_fs();
  1588. set_fs(KERNEL_DS);
  1589. res = blkdev_ioctl(bdev, 0, cmd, arg);
  1590. set_fs(old_fs);
  1591. return res;
  1592. }
  1593. EXPORT_SYMBOL(ioctl_by_bdev);
  1594. /**
  1595. * lookup_bdev - lookup a struct block_device by name
  1596. * @pathname: special file representing the block device
  1597. *
  1598. * Get a reference to the blockdevice at @pathname in the current
  1599. * namespace if possible and return it. Return ERR_PTR(error)
  1600. * otherwise.
  1601. */
  1602. struct block_device *lookup_bdev(const char *pathname)
  1603. {
  1604. struct block_device *bdev;
  1605. struct inode *inode;
  1606. struct path path;
  1607. int error;
  1608. if (!pathname || !*pathname)
  1609. return ERR_PTR(-EINVAL);
  1610. error = kern_path(pathname, LOOKUP_FOLLOW, &path);
  1611. if (error)
  1612. return ERR_PTR(error);
  1613. inode = d_backing_inode(path.dentry);
  1614. error = -ENOTBLK;
  1615. if (!S_ISBLK(inode->i_mode))
  1616. goto fail;
  1617. error = -EACCES;
  1618. if (path.mnt->mnt_flags & MNT_NODEV)
  1619. goto fail;
  1620. error = -ENOMEM;
  1621. bdev = bd_acquire(inode);
  1622. if (!bdev)
  1623. goto fail;
  1624. out:
  1625. path_put(&path);
  1626. return bdev;
  1627. fail:
  1628. bdev = ERR_PTR(error);
  1629. goto out;
  1630. }
  1631. EXPORT_SYMBOL(lookup_bdev);
  1632. int __invalidate_device(struct block_device *bdev, bool kill_dirty)
  1633. {
  1634. struct super_block *sb = get_super(bdev);
  1635. int res = 0;
  1636. if (sb) {
  1637. /*
  1638. * no need to lock the super, get_super holds the
  1639. * read mutex so the filesystem cannot go away
  1640. * under us (->put_super runs with the write lock
  1641. * hold).
  1642. */
  1643. shrink_dcache_sb(sb);
  1644. res = invalidate_inodes(sb, kill_dirty);
  1645. drop_super(sb);
  1646. }
  1647. invalidate_bdev(bdev);
  1648. return res;
  1649. }
  1650. EXPORT_SYMBOL(__invalidate_device);
  1651. void iterate_bdevs(void (*func)(struct block_device *, void *), void *arg)
  1652. {
  1653. struct inode *inode, *old_inode = NULL;
  1654. spin_lock(&blockdev_superblock->s_inode_list_lock);
  1655. list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list) {
  1656. struct address_space *mapping = inode->i_mapping;
  1657. spin_lock(&inode->i_lock);
  1658. if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW) ||
  1659. mapping->nrpages == 0) {
  1660. spin_unlock(&inode->i_lock);
  1661. continue;
  1662. }
  1663. __iget(inode);
  1664. spin_unlock(&inode->i_lock);
  1665. spin_unlock(&blockdev_superblock->s_inode_list_lock);
  1666. /*
  1667. * We hold a reference to 'inode' so it couldn't have been
  1668. * removed from s_inodes list while we dropped the
  1669. * s_inode_list_lock We cannot iput the inode now as we can
  1670. * be holding the last reference and we cannot iput it under
  1671. * s_inode_list_lock. So we keep the reference and iput it
  1672. * later.
  1673. */
  1674. iput(old_inode);
  1675. old_inode = inode;
  1676. func(I_BDEV(inode), arg);
  1677. spin_lock(&blockdev_superblock->s_inode_list_lock);
  1678. }
  1679. spin_unlock(&blockdev_superblock->s_inode_list_lock);
  1680. iput(old_inode);
  1681. }