block_dev.c 37 KB

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