block_dev.c 46 KB

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