block_dev.c 42 KB

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