block_dev.c 44 KB

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