block_dev.c 48 KB

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