block_dev.c 50 KB

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