block_dev.c 44 KB

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