block_dev.c 45 KB

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