block_dev.c 44 KB

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