block_dev.c 48 KB

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