block_dev.c 42 KB

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