block_dev.c 48 KB

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