block_dev.c 47 KB

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