block_dev.c 40 KB

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