block_dev.c 39 KB

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