block_dev.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342
  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/highmem.h>
  15. #include <linux/blkdev.h>
  16. #include <linux/module.h>
  17. #include <linux/blkpg.h>
  18. #include <linux/buffer_head.h>
  19. #include <linux/writeback.h>
  20. #include <linux/mpage.h>
  21. #include <linux/mount.h>
  22. #include <linux/uio.h>
  23. #include <linux/namei.h>
  24. #include <asm/uaccess.h>
  25. #include "internal.h"
  26. struct bdev_inode {
  27. struct block_device bdev;
  28. struct inode vfs_inode;
  29. };
  30. static inline struct bdev_inode *BDEV_I(struct inode *inode)
  31. {
  32. return container_of(inode, struct bdev_inode, vfs_inode);
  33. }
  34. inline struct block_device *I_BDEV(struct inode *inode)
  35. {
  36. return &BDEV_I(inode)->bdev;
  37. }
  38. EXPORT_SYMBOL(I_BDEV);
  39. static sector_t max_block(struct block_device *bdev)
  40. {
  41. sector_t retval = ~((sector_t)0);
  42. loff_t sz = i_size_read(bdev->bd_inode);
  43. if (sz) {
  44. unsigned int size = block_size(bdev);
  45. unsigned int sizebits = blksize_bits(size);
  46. retval = (sz >> sizebits);
  47. }
  48. return retval;
  49. }
  50. /* Kill _all_ buffers, dirty or not.. */
  51. static void kill_bdev(struct block_device *bdev)
  52. {
  53. invalidate_bdev(bdev, 1);
  54. truncate_inode_pages(bdev->bd_inode->i_mapping, 0);
  55. }
  56. int set_blocksize(struct block_device *bdev, int size)
  57. {
  58. /* Size must be a power of two, and between 512 and PAGE_SIZE */
  59. if (size > PAGE_SIZE || size < 512 || (size & (size-1)))
  60. return -EINVAL;
  61. /* Size cannot be smaller than the size supported by the device */
  62. if (size < bdev_hardsect_size(bdev))
  63. return -EINVAL;
  64. /* Don't change the size if it is same as current */
  65. if (bdev->bd_block_size != size) {
  66. sync_blockdev(bdev);
  67. bdev->bd_block_size = size;
  68. bdev->bd_inode->i_blkbits = blksize_bits(size);
  69. kill_bdev(bdev);
  70. }
  71. return 0;
  72. }
  73. EXPORT_SYMBOL(set_blocksize);
  74. int sb_set_blocksize(struct super_block *sb, int size)
  75. {
  76. if (set_blocksize(sb->s_bdev, size))
  77. return 0;
  78. /* If we get here, we know size is power of two
  79. * and it's value is between 512 and PAGE_SIZE */
  80. sb->s_blocksize = size;
  81. sb->s_blocksize_bits = blksize_bits(size);
  82. return sb->s_blocksize;
  83. }
  84. EXPORT_SYMBOL(sb_set_blocksize);
  85. int sb_min_blocksize(struct super_block *sb, int size)
  86. {
  87. int minsize = bdev_hardsect_size(sb->s_bdev);
  88. if (size < minsize)
  89. size = minsize;
  90. return sb_set_blocksize(sb, size);
  91. }
  92. EXPORT_SYMBOL(sb_min_blocksize);
  93. static int
  94. blkdev_get_block(struct inode *inode, sector_t iblock,
  95. struct buffer_head *bh, int create)
  96. {
  97. if (iblock >= max_block(I_BDEV(inode))) {
  98. if (create)
  99. return -EIO;
  100. /*
  101. * for reads, we're just trying to fill a partial page.
  102. * return a hole, they will have to call get_block again
  103. * before they can fill it, and they will get -EIO at that
  104. * time
  105. */
  106. return 0;
  107. }
  108. bh->b_bdev = I_BDEV(inode);
  109. bh->b_blocknr = iblock;
  110. set_buffer_mapped(bh);
  111. return 0;
  112. }
  113. static int
  114. blkdev_get_blocks(struct inode *inode, sector_t iblock,
  115. struct buffer_head *bh, int create)
  116. {
  117. sector_t end_block = max_block(I_BDEV(inode));
  118. unsigned long max_blocks = bh->b_size >> inode->i_blkbits;
  119. if ((iblock + max_blocks) > end_block) {
  120. max_blocks = end_block - iblock;
  121. if ((long)max_blocks <= 0) {
  122. if (create)
  123. return -EIO; /* write fully beyond EOF */
  124. /*
  125. * It is a read which is fully beyond EOF. We return
  126. * a !buffer_mapped buffer
  127. */
  128. max_blocks = 0;
  129. }
  130. }
  131. bh->b_bdev = I_BDEV(inode);
  132. bh->b_blocknr = iblock;
  133. bh->b_size = max_blocks << inode->i_blkbits;
  134. if (max_blocks)
  135. set_buffer_mapped(bh);
  136. return 0;
  137. }
  138. static ssize_t
  139. blkdev_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
  140. loff_t offset, unsigned long nr_segs)
  141. {
  142. struct file *file = iocb->ki_filp;
  143. struct inode *inode = file->f_mapping->host;
  144. return blockdev_direct_IO_no_locking(rw, iocb, inode, I_BDEV(inode),
  145. iov, offset, nr_segs, blkdev_get_blocks, NULL);
  146. }
  147. static int blkdev_writepage(struct page *page, struct writeback_control *wbc)
  148. {
  149. return block_write_full_page(page, blkdev_get_block, wbc);
  150. }
  151. static int blkdev_readpage(struct file * file, struct page * page)
  152. {
  153. return block_read_full_page(page, blkdev_get_block);
  154. }
  155. static int blkdev_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to)
  156. {
  157. return block_prepare_write(page, from, to, blkdev_get_block);
  158. }
  159. static int blkdev_commit_write(struct file *file, struct page *page, unsigned from, unsigned to)
  160. {
  161. return block_commit_write(page, from, to);
  162. }
  163. /*
  164. * private llseek:
  165. * for a block special file file->f_dentry->d_inode->i_size is zero
  166. * so we compute the size by hand (just as in block_read/write above)
  167. */
  168. static loff_t block_llseek(struct file *file, loff_t offset, int origin)
  169. {
  170. struct inode *bd_inode = file->f_mapping->host;
  171. loff_t size;
  172. loff_t retval;
  173. mutex_lock(&bd_inode->i_mutex);
  174. size = i_size_read(bd_inode);
  175. switch (origin) {
  176. case 2:
  177. offset += size;
  178. break;
  179. case 1:
  180. offset += file->f_pos;
  181. }
  182. retval = -EINVAL;
  183. if (offset >= 0 && offset <= size) {
  184. if (offset != file->f_pos) {
  185. file->f_pos = offset;
  186. }
  187. retval = offset;
  188. }
  189. mutex_unlock(&bd_inode->i_mutex);
  190. return retval;
  191. }
  192. /*
  193. * Filp is never NULL; the only case when ->fsync() is called with
  194. * NULL first argument is nfsd_sync_dir() and that's not a directory.
  195. */
  196. static int block_fsync(struct file *filp, struct dentry *dentry, int datasync)
  197. {
  198. return sync_blockdev(I_BDEV(filp->f_mapping->host));
  199. }
  200. /*
  201. * pseudo-fs
  202. */
  203. static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);
  204. static kmem_cache_t * bdev_cachep __read_mostly;
  205. static struct inode *bdev_alloc_inode(struct super_block *sb)
  206. {
  207. struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, SLAB_KERNEL);
  208. if (!ei)
  209. return NULL;
  210. return &ei->vfs_inode;
  211. }
  212. static void bdev_destroy_inode(struct inode *inode)
  213. {
  214. struct bdev_inode *bdi = BDEV_I(inode);
  215. bdi->bdev.bd_inode_backing_dev_info = NULL;
  216. kmem_cache_free(bdev_cachep, bdi);
  217. }
  218. static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
  219. {
  220. struct bdev_inode *ei = (struct bdev_inode *) foo;
  221. struct block_device *bdev = &ei->bdev;
  222. if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
  223. SLAB_CTOR_CONSTRUCTOR)
  224. {
  225. memset(bdev, 0, sizeof(*bdev));
  226. mutex_init(&bdev->bd_mutex);
  227. mutex_init(&bdev->bd_mount_mutex);
  228. INIT_LIST_HEAD(&bdev->bd_inodes);
  229. INIT_LIST_HEAD(&bdev->bd_list);
  230. #ifdef CONFIG_SYSFS
  231. INIT_LIST_HEAD(&bdev->bd_holder_list);
  232. #endif
  233. inode_init_once(&ei->vfs_inode);
  234. }
  235. }
  236. static inline void __bd_forget(struct inode *inode)
  237. {
  238. list_del_init(&inode->i_devices);
  239. inode->i_bdev = NULL;
  240. inode->i_mapping = &inode->i_data;
  241. }
  242. static void bdev_clear_inode(struct inode *inode)
  243. {
  244. struct block_device *bdev = &BDEV_I(inode)->bdev;
  245. struct list_head *p;
  246. spin_lock(&bdev_lock);
  247. while ( (p = bdev->bd_inodes.next) != &bdev->bd_inodes ) {
  248. __bd_forget(list_entry(p, struct inode, i_devices));
  249. }
  250. list_del_init(&bdev->bd_list);
  251. spin_unlock(&bdev_lock);
  252. }
  253. static struct super_operations bdev_sops = {
  254. .statfs = simple_statfs,
  255. .alloc_inode = bdev_alloc_inode,
  256. .destroy_inode = bdev_destroy_inode,
  257. .drop_inode = generic_delete_inode,
  258. .clear_inode = bdev_clear_inode,
  259. };
  260. static int bd_get_sb(struct file_system_type *fs_type,
  261. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  262. {
  263. return get_sb_pseudo(fs_type, "bdev:", &bdev_sops, 0x62646576, mnt);
  264. }
  265. static struct file_system_type bd_type = {
  266. .name = "bdev",
  267. .get_sb = bd_get_sb,
  268. .kill_sb = kill_anon_super,
  269. };
  270. static struct vfsmount *bd_mnt __read_mostly;
  271. struct super_block *blockdev_superblock;
  272. void __init bdev_cache_init(void)
  273. {
  274. int err;
  275. bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
  276. 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
  277. SLAB_MEM_SPREAD|SLAB_PANIC),
  278. init_once, NULL);
  279. err = register_filesystem(&bd_type);
  280. if (err)
  281. panic("Cannot register bdev pseudo-fs");
  282. bd_mnt = kern_mount(&bd_type);
  283. err = PTR_ERR(bd_mnt);
  284. if (IS_ERR(bd_mnt))
  285. panic("Cannot create bdev pseudo-fs");
  286. blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */
  287. }
  288. /*
  289. * Most likely _very_ bad one - but then it's hardly critical for small
  290. * /dev and can be fixed when somebody will need really large one.
  291. * Keep in mind that it will be fed through icache hash function too.
  292. */
  293. static inline unsigned long hash(dev_t dev)
  294. {
  295. return MAJOR(dev)+MINOR(dev);
  296. }
  297. static int bdev_test(struct inode *inode, void *data)
  298. {
  299. return BDEV_I(inode)->bdev.bd_dev == *(dev_t *)data;
  300. }
  301. static int bdev_set(struct inode *inode, void *data)
  302. {
  303. BDEV_I(inode)->bdev.bd_dev = *(dev_t *)data;
  304. return 0;
  305. }
  306. static LIST_HEAD(all_bdevs);
  307. struct block_device *bdget(dev_t dev)
  308. {
  309. struct block_device *bdev;
  310. struct inode *inode;
  311. inode = iget5_locked(bd_mnt->mnt_sb, hash(dev),
  312. bdev_test, bdev_set, &dev);
  313. if (!inode)
  314. return NULL;
  315. bdev = &BDEV_I(inode)->bdev;
  316. if (inode->i_state & I_NEW) {
  317. bdev->bd_contains = NULL;
  318. bdev->bd_inode = inode;
  319. bdev->bd_block_size = (1 << inode->i_blkbits);
  320. bdev->bd_part_count = 0;
  321. bdev->bd_invalidated = 0;
  322. inode->i_mode = S_IFBLK;
  323. inode->i_rdev = dev;
  324. inode->i_bdev = bdev;
  325. inode->i_data.a_ops = &def_blk_aops;
  326. mapping_set_gfp_mask(&inode->i_data, GFP_USER);
  327. inode->i_data.backing_dev_info = &default_backing_dev_info;
  328. spin_lock(&bdev_lock);
  329. list_add(&bdev->bd_list, &all_bdevs);
  330. spin_unlock(&bdev_lock);
  331. unlock_new_inode(inode);
  332. }
  333. return bdev;
  334. }
  335. EXPORT_SYMBOL(bdget);
  336. long nr_blockdev_pages(void)
  337. {
  338. struct list_head *p;
  339. long ret = 0;
  340. spin_lock(&bdev_lock);
  341. list_for_each(p, &all_bdevs) {
  342. struct block_device *bdev;
  343. bdev = list_entry(p, struct block_device, bd_list);
  344. ret += bdev->bd_inode->i_mapping->nrpages;
  345. }
  346. spin_unlock(&bdev_lock);
  347. return ret;
  348. }
  349. void bdput(struct block_device *bdev)
  350. {
  351. iput(bdev->bd_inode);
  352. }
  353. EXPORT_SYMBOL(bdput);
  354. static struct block_device *bd_acquire(struct inode *inode)
  355. {
  356. struct block_device *bdev;
  357. spin_lock(&bdev_lock);
  358. bdev = inode->i_bdev;
  359. if (bdev) {
  360. atomic_inc(&bdev->bd_inode->i_count);
  361. spin_unlock(&bdev_lock);
  362. return bdev;
  363. }
  364. spin_unlock(&bdev_lock);
  365. bdev = bdget(inode->i_rdev);
  366. if (bdev) {
  367. spin_lock(&bdev_lock);
  368. if (!inode->i_bdev) {
  369. /*
  370. * We take an additional bd_inode->i_count for inode,
  371. * and it's released in clear_inode() of inode.
  372. * So, we can access it via ->i_mapping always
  373. * without igrab().
  374. */
  375. atomic_inc(&bdev->bd_inode->i_count);
  376. inode->i_bdev = bdev;
  377. inode->i_mapping = bdev->bd_inode->i_mapping;
  378. list_add(&inode->i_devices, &bdev->bd_inodes);
  379. }
  380. spin_unlock(&bdev_lock);
  381. }
  382. return bdev;
  383. }
  384. /* Call when you free inode */
  385. void bd_forget(struct inode *inode)
  386. {
  387. struct block_device *bdev = NULL;
  388. spin_lock(&bdev_lock);
  389. if (inode->i_bdev) {
  390. if (inode->i_sb != blockdev_superblock)
  391. bdev = inode->i_bdev;
  392. __bd_forget(inode);
  393. }
  394. spin_unlock(&bdev_lock);
  395. if (bdev)
  396. iput(bdev->bd_inode);
  397. }
  398. int bd_claim(struct block_device *bdev, void *holder)
  399. {
  400. int res;
  401. spin_lock(&bdev_lock);
  402. /* first decide result */
  403. if (bdev->bd_holder == holder)
  404. res = 0; /* already a holder */
  405. else if (bdev->bd_holder != NULL)
  406. res = -EBUSY; /* held by someone else */
  407. else if (bdev->bd_contains == bdev)
  408. res = 0; /* is a whole device which isn't held */
  409. else if (bdev->bd_contains->bd_holder == bd_claim)
  410. res = 0; /* is a partition of a device that is being partitioned */
  411. else if (bdev->bd_contains->bd_holder != NULL)
  412. res = -EBUSY; /* is a partition of a held device */
  413. else
  414. res = 0; /* is a partition of an un-held device */
  415. /* now impose change */
  416. if (res==0) {
  417. /* note that for a whole device bd_holders
  418. * will be incremented twice, and bd_holder will
  419. * be set to bd_claim before being set to holder
  420. */
  421. bdev->bd_contains->bd_holders ++;
  422. bdev->bd_contains->bd_holder = bd_claim;
  423. bdev->bd_holders++;
  424. bdev->bd_holder = holder;
  425. }
  426. spin_unlock(&bdev_lock);
  427. return res;
  428. }
  429. EXPORT_SYMBOL(bd_claim);
  430. void bd_release(struct block_device *bdev)
  431. {
  432. spin_lock(&bdev_lock);
  433. if (!--bdev->bd_contains->bd_holders)
  434. bdev->bd_contains->bd_holder = NULL;
  435. if (!--bdev->bd_holders)
  436. bdev->bd_holder = NULL;
  437. spin_unlock(&bdev_lock);
  438. }
  439. EXPORT_SYMBOL(bd_release);
  440. #ifdef CONFIG_SYSFS
  441. /*
  442. * Functions for bd_claim_by_kobject / bd_release_from_kobject
  443. *
  444. * If a kobject is passed to bd_claim_by_kobject()
  445. * and the kobject has a parent directory,
  446. * following symlinks are created:
  447. * o from the kobject to the claimed bdev
  448. * o from "holders" directory of the bdev to the parent of the kobject
  449. * bd_release_from_kobject() removes these symlinks.
  450. *
  451. * Example:
  452. * If /dev/dm-0 maps to /dev/sda, kobject corresponding to
  453. * /sys/block/dm-0/slaves is passed to bd_claim_by_kobject(), then:
  454. * /sys/block/dm-0/slaves/sda --> /sys/block/sda
  455. * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
  456. */
  457. static struct kobject *bdev_get_kobj(struct block_device *bdev)
  458. {
  459. if (bdev->bd_contains != bdev)
  460. return kobject_get(&bdev->bd_part->kobj);
  461. else
  462. return kobject_get(&bdev->bd_disk->kobj);
  463. }
  464. static struct kobject *bdev_get_holder(struct block_device *bdev)
  465. {
  466. if (bdev->bd_contains != bdev)
  467. return kobject_get(bdev->bd_part->holder_dir);
  468. else
  469. return kobject_get(bdev->bd_disk->holder_dir);
  470. }
  471. static int add_symlink(struct kobject *from, struct kobject *to)
  472. {
  473. if (!from || !to)
  474. return 0;
  475. return sysfs_create_link(from, to, kobject_name(to));
  476. }
  477. static void del_symlink(struct kobject *from, struct kobject *to)
  478. {
  479. if (!from || !to)
  480. return;
  481. sysfs_remove_link(from, kobject_name(to));
  482. }
  483. /*
  484. * 'struct bd_holder' contains pointers to kobjects symlinked by
  485. * bd_claim_by_kobject.
  486. * It's connected to bd_holder_list which is protected by bdev->bd_sem.
  487. */
  488. struct bd_holder {
  489. struct list_head list; /* chain of holders of the bdev */
  490. int count; /* references from the holder */
  491. struct kobject *sdir; /* holder object, e.g. "/block/dm-0/slaves" */
  492. struct kobject *hdev; /* e.g. "/block/dm-0" */
  493. struct kobject *hdir; /* e.g. "/block/sda/holders" */
  494. struct kobject *sdev; /* e.g. "/block/sda" */
  495. };
  496. /*
  497. * Get references of related kobjects at once.
  498. * Returns 1 on success. 0 on failure.
  499. *
  500. * Should call bd_holder_release_dirs() after successful use.
  501. */
  502. static int bd_holder_grab_dirs(struct block_device *bdev,
  503. struct bd_holder *bo)
  504. {
  505. if (!bdev || !bo)
  506. return 0;
  507. bo->sdir = kobject_get(bo->sdir);
  508. if (!bo->sdir)
  509. return 0;
  510. bo->hdev = kobject_get(bo->sdir->parent);
  511. if (!bo->hdev)
  512. goto fail_put_sdir;
  513. bo->sdev = bdev_get_kobj(bdev);
  514. if (!bo->sdev)
  515. goto fail_put_hdev;
  516. bo->hdir = bdev_get_holder(bdev);
  517. if (!bo->hdir)
  518. goto fail_put_sdev;
  519. return 1;
  520. fail_put_sdev:
  521. kobject_put(bo->sdev);
  522. fail_put_hdev:
  523. kobject_put(bo->hdev);
  524. fail_put_sdir:
  525. kobject_put(bo->sdir);
  526. return 0;
  527. }
  528. /* Put references of related kobjects at once. */
  529. static void bd_holder_release_dirs(struct bd_holder *bo)
  530. {
  531. kobject_put(bo->hdir);
  532. kobject_put(bo->sdev);
  533. kobject_put(bo->hdev);
  534. kobject_put(bo->sdir);
  535. }
  536. static struct bd_holder *alloc_bd_holder(struct kobject *kobj)
  537. {
  538. struct bd_holder *bo;
  539. bo = kzalloc(sizeof(*bo), GFP_KERNEL);
  540. if (!bo)
  541. return NULL;
  542. bo->count = 1;
  543. bo->sdir = kobj;
  544. return bo;
  545. }
  546. static void free_bd_holder(struct bd_holder *bo)
  547. {
  548. kfree(bo);
  549. }
  550. /**
  551. * find_bd_holder - find matching struct bd_holder from the block device
  552. *
  553. * @bdev: struct block device to be searched
  554. * @bo: target struct bd_holder
  555. *
  556. * Returns matching entry with @bo in @bdev->bd_holder_list.
  557. * If found, increment the reference count and return the pointer.
  558. * If not found, returns NULL.
  559. */
  560. static struct bd_holder *find_bd_holder(struct block_device *bdev,
  561. struct bd_holder *bo)
  562. {
  563. struct bd_holder *tmp;
  564. list_for_each_entry(tmp, &bdev->bd_holder_list, list)
  565. if (tmp->sdir == bo->sdir) {
  566. tmp->count++;
  567. return tmp;
  568. }
  569. return NULL;
  570. }
  571. /**
  572. * add_bd_holder - create sysfs symlinks for bd_claim() relationship
  573. *
  574. * @bdev: block device to be bd_claimed
  575. * @bo: preallocated and initialized by alloc_bd_holder()
  576. *
  577. * Add @bo to @bdev->bd_holder_list, create symlinks.
  578. *
  579. * Returns 0 if symlinks are created.
  580. * Returns -ve if something fails.
  581. */
  582. static int add_bd_holder(struct block_device *bdev, struct bd_holder *bo)
  583. {
  584. int ret;
  585. if (!bo)
  586. return -EINVAL;
  587. if (!bd_holder_grab_dirs(bdev, bo))
  588. return -EBUSY;
  589. ret = add_symlink(bo->sdir, bo->sdev);
  590. if (ret == 0) {
  591. ret = add_symlink(bo->hdir, bo->hdev);
  592. if (ret)
  593. del_symlink(bo->sdir, bo->sdev);
  594. }
  595. if (ret == 0)
  596. list_add_tail(&bo->list, &bdev->bd_holder_list);
  597. return ret;
  598. }
  599. /**
  600. * del_bd_holder - delete sysfs symlinks for bd_claim() relationship
  601. *
  602. * @bdev: block device to be bd_claimed
  603. * @kobj: holder's kobject
  604. *
  605. * If there is matching entry with @kobj in @bdev->bd_holder_list
  606. * and no other bd_claim() from the same kobject,
  607. * remove the struct bd_holder from the list, delete symlinks for it.
  608. *
  609. * Returns a pointer to the struct bd_holder when it's removed from the list
  610. * and ready to be freed.
  611. * Returns NULL if matching claim isn't found or there is other bd_claim()
  612. * by the same kobject.
  613. */
  614. static struct bd_holder *del_bd_holder(struct block_device *bdev,
  615. struct kobject *kobj)
  616. {
  617. struct bd_holder *bo;
  618. list_for_each_entry(bo, &bdev->bd_holder_list, list) {
  619. if (bo->sdir == kobj) {
  620. bo->count--;
  621. BUG_ON(bo->count < 0);
  622. if (!bo->count) {
  623. list_del(&bo->list);
  624. del_symlink(bo->sdir, bo->sdev);
  625. del_symlink(bo->hdir, bo->hdev);
  626. bd_holder_release_dirs(bo);
  627. return bo;
  628. }
  629. break;
  630. }
  631. }
  632. return NULL;
  633. }
  634. /**
  635. * bd_claim_by_kobject - bd_claim() with additional kobject signature
  636. *
  637. * @bdev: block device to be claimed
  638. * @holder: holder's signature
  639. * @kobj: holder's kobject
  640. *
  641. * Do bd_claim() and if it succeeds, create sysfs symlinks between
  642. * the bdev and the holder's kobject.
  643. * Use bd_release_from_kobject() when relesing the claimed bdev.
  644. *
  645. * Returns 0 on success. (same as bd_claim())
  646. * Returns errno on failure.
  647. */
  648. static int bd_claim_by_kobject(struct block_device *bdev, void *holder,
  649. struct kobject *kobj)
  650. {
  651. int res;
  652. struct bd_holder *bo, *found;
  653. if (!kobj)
  654. return -EINVAL;
  655. bo = alloc_bd_holder(kobj);
  656. if (!bo)
  657. return -ENOMEM;
  658. mutex_lock_nested(&bdev->bd_mutex, BD_MUTEX_PARTITION);
  659. res = bd_claim(bdev, holder);
  660. if (res == 0) {
  661. found = find_bd_holder(bdev, bo);
  662. if (found == NULL) {
  663. res = add_bd_holder(bdev, bo);
  664. if (res)
  665. bd_release(bdev);
  666. }
  667. }
  668. if (res || found)
  669. free_bd_holder(bo);
  670. mutex_unlock(&bdev->bd_mutex);
  671. return res;
  672. }
  673. /**
  674. * bd_release_from_kobject - bd_release() with additional kobject signature
  675. *
  676. * @bdev: block device to be released
  677. * @kobj: holder's kobject
  678. *
  679. * Do bd_release() and remove sysfs symlinks created by bd_claim_by_kobject().
  680. */
  681. static void bd_release_from_kobject(struct block_device *bdev,
  682. struct kobject *kobj)
  683. {
  684. struct bd_holder *bo;
  685. if (!kobj)
  686. return;
  687. mutex_lock_nested(&bdev->bd_mutex, BD_MUTEX_PARTITION);
  688. bd_release(bdev);
  689. if ((bo = del_bd_holder(bdev, kobj)))
  690. free_bd_holder(bo);
  691. mutex_unlock(&bdev->bd_mutex);
  692. }
  693. /**
  694. * bd_claim_by_disk - wrapper function for bd_claim_by_kobject()
  695. *
  696. * @bdev: block device to be claimed
  697. * @holder: holder's signature
  698. * @disk: holder's gendisk
  699. *
  700. * Call bd_claim_by_kobject() with getting @disk->slave_dir.
  701. */
  702. int bd_claim_by_disk(struct block_device *bdev, void *holder,
  703. struct gendisk *disk)
  704. {
  705. return bd_claim_by_kobject(bdev, holder, kobject_get(disk->slave_dir));
  706. }
  707. EXPORT_SYMBOL_GPL(bd_claim_by_disk);
  708. /**
  709. * bd_release_from_disk - wrapper function for bd_release_from_kobject()
  710. *
  711. * @bdev: block device to be claimed
  712. * @disk: holder's gendisk
  713. *
  714. * Call bd_release_from_kobject() and put @disk->slave_dir.
  715. */
  716. void bd_release_from_disk(struct block_device *bdev, struct gendisk *disk)
  717. {
  718. bd_release_from_kobject(bdev, disk->slave_dir);
  719. kobject_put(disk->slave_dir);
  720. }
  721. EXPORT_SYMBOL_GPL(bd_release_from_disk);
  722. #endif
  723. /*
  724. * Tries to open block device by device number. Use it ONLY if you
  725. * really do not have anything better - i.e. when you are behind a
  726. * truly sucky interface and all you are given is a device number. _Never_
  727. * to be used for internal purposes. If you ever need it - reconsider
  728. * your API.
  729. */
  730. struct block_device *open_by_devnum(dev_t dev, unsigned mode)
  731. {
  732. struct block_device *bdev = bdget(dev);
  733. int err = -ENOMEM;
  734. int flags = mode & FMODE_WRITE ? O_RDWR : O_RDONLY;
  735. if (bdev)
  736. err = blkdev_get(bdev, mode, flags);
  737. return err ? ERR_PTR(err) : bdev;
  738. }
  739. EXPORT_SYMBOL(open_by_devnum);
  740. static int
  741. blkdev_get_partition(struct block_device *bdev, mode_t mode, unsigned flags);
  742. struct block_device *open_partition_by_devnum(dev_t dev, unsigned mode)
  743. {
  744. struct block_device *bdev = bdget(dev);
  745. int err = -ENOMEM;
  746. int flags = mode & FMODE_WRITE ? O_RDWR : O_RDONLY;
  747. if (bdev)
  748. err = blkdev_get_partition(bdev, mode, flags);
  749. return err ? ERR_PTR(err) : bdev;
  750. }
  751. EXPORT_SYMBOL(open_partition_by_devnum);
  752. /*
  753. * This routine checks whether a removable media has been changed,
  754. * and invalidates all buffer-cache-entries in that case. This
  755. * is a relatively slow routine, so we have to try to minimize using
  756. * it. Thus it is called only upon a 'mount' or 'open'. This
  757. * is the best way of combining speed and utility, I think.
  758. * People changing diskettes in the middle of an operation deserve
  759. * to lose :-)
  760. */
  761. int check_disk_change(struct block_device *bdev)
  762. {
  763. struct gendisk *disk = bdev->bd_disk;
  764. struct block_device_operations * bdops = disk->fops;
  765. if (!bdops->media_changed)
  766. return 0;
  767. if (!bdops->media_changed(bdev->bd_disk))
  768. return 0;
  769. if (__invalidate_device(bdev))
  770. printk("VFS: busy inodes on changed media.\n");
  771. if (bdops->revalidate_disk)
  772. bdops->revalidate_disk(bdev->bd_disk);
  773. if (bdev->bd_disk->minors > 1)
  774. bdev->bd_invalidated = 1;
  775. return 1;
  776. }
  777. EXPORT_SYMBOL(check_disk_change);
  778. void bd_set_size(struct block_device *bdev, loff_t size)
  779. {
  780. unsigned bsize = bdev_hardsect_size(bdev);
  781. bdev->bd_inode->i_size = size;
  782. while (bsize < PAGE_CACHE_SIZE) {
  783. if (size & bsize)
  784. break;
  785. bsize <<= 1;
  786. }
  787. bdev->bd_block_size = bsize;
  788. bdev->bd_inode->i_blkbits = blksize_bits(bsize);
  789. }
  790. EXPORT_SYMBOL(bd_set_size);
  791. static int __blkdev_put(struct block_device *bdev, unsigned int subclass)
  792. {
  793. int ret = 0;
  794. struct inode *bd_inode = bdev->bd_inode;
  795. struct gendisk *disk = bdev->bd_disk;
  796. mutex_lock_nested(&bdev->bd_mutex, subclass);
  797. lock_kernel();
  798. if (!--bdev->bd_openers) {
  799. sync_blockdev(bdev);
  800. kill_bdev(bdev);
  801. }
  802. if (bdev->bd_contains == bdev) {
  803. if (disk->fops->release)
  804. ret = disk->fops->release(bd_inode, NULL);
  805. } else {
  806. mutex_lock_nested(&bdev->bd_contains->bd_mutex,
  807. subclass + 1);
  808. bdev->bd_contains->bd_part_count--;
  809. mutex_unlock(&bdev->bd_contains->bd_mutex);
  810. }
  811. if (!bdev->bd_openers) {
  812. struct module *owner = disk->fops->owner;
  813. put_disk(disk);
  814. module_put(owner);
  815. if (bdev->bd_contains != bdev) {
  816. kobject_put(&bdev->bd_part->kobj);
  817. bdev->bd_part = NULL;
  818. }
  819. bdev->bd_disk = NULL;
  820. bdev->bd_inode->i_data.backing_dev_info = &default_backing_dev_info;
  821. if (bdev != bdev->bd_contains)
  822. __blkdev_put(bdev->bd_contains, subclass + 1);
  823. bdev->bd_contains = NULL;
  824. }
  825. unlock_kernel();
  826. mutex_unlock(&bdev->bd_mutex);
  827. bdput(bdev);
  828. return ret;
  829. }
  830. int blkdev_put(struct block_device *bdev)
  831. {
  832. return __blkdev_put(bdev, BD_MUTEX_NORMAL);
  833. }
  834. EXPORT_SYMBOL(blkdev_put);
  835. int blkdev_put_partition(struct block_device *bdev)
  836. {
  837. return __blkdev_put(bdev, BD_MUTEX_PARTITION);
  838. }
  839. EXPORT_SYMBOL(blkdev_put_partition);
  840. static int
  841. blkdev_get_whole(struct block_device *bdev, mode_t mode, unsigned flags);
  842. static int
  843. do_open(struct block_device *bdev, struct file *file, unsigned int subclass)
  844. {
  845. struct module *owner = NULL;
  846. struct gendisk *disk;
  847. int ret = -ENXIO;
  848. int part;
  849. file->f_mapping = bdev->bd_inode->i_mapping;
  850. lock_kernel();
  851. disk = get_gendisk(bdev->bd_dev, &part);
  852. if (!disk) {
  853. unlock_kernel();
  854. bdput(bdev);
  855. return ret;
  856. }
  857. owner = disk->fops->owner;
  858. mutex_lock_nested(&bdev->bd_mutex, subclass);
  859. if (!bdev->bd_openers) {
  860. bdev->bd_disk = disk;
  861. bdev->bd_contains = bdev;
  862. if (!part) {
  863. struct backing_dev_info *bdi;
  864. if (disk->fops->open) {
  865. ret = disk->fops->open(bdev->bd_inode, file);
  866. if (ret)
  867. goto out_first;
  868. }
  869. if (!bdev->bd_openers) {
  870. bd_set_size(bdev,(loff_t)get_capacity(disk)<<9);
  871. bdi = blk_get_backing_dev_info(bdev);
  872. if (bdi == NULL)
  873. bdi = &default_backing_dev_info;
  874. bdev->bd_inode->i_data.backing_dev_info = bdi;
  875. }
  876. if (bdev->bd_invalidated)
  877. rescan_partitions(disk, bdev);
  878. } else {
  879. struct hd_struct *p;
  880. struct block_device *whole;
  881. whole = bdget_disk(disk, 0);
  882. ret = -ENOMEM;
  883. if (!whole)
  884. goto out_first;
  885. ret = blkdev_get_whole(whole, file->f_mode, file->f_flags);
  886. if (ret)
  887. goto out_first;
  888. bdev->bd_contains = whole;
  889. mutex_lock_nested(&whole->bd_mutex, BD_MUTEX_WHOLE);
  890. whole->bd_part_count++;
  891. p = disk->part[part - 1];
  892. bdev->bd_inode->i_data.backing_dev_info =
  893. whole->bd_inode->i_data.backing_dev_info;
  894. if (!(disk->flags & GENHD_FL_UP) || !p || !p->nr_sects) {
  895. whole->bd_part_count--;
  896. mutex_unlock(&whole->bd_mutex);
  897. ret = -ENXIO;
  898. goto out_first;
  899. }
  900. kobject_get(&p->kobj);
  901. bdev->bd_part = p;
  902. bd_set_size(bdev, (loff_t) p->nr_sects << 9);
  903. mutex_unlock(&whole->bd_mutex);
  904. }
  905. } else {
  906. put_disk(disk);
  907. module_put(owner);
  908. if (bdev->bd_contains == bdev) {
  909. if (bdev->bd_disk->fops->open) {
  910. ret = bdev->bd_disk->fops->open(bdev->bd_inode, file);
  911. if (ret)
  912. goto out;
  913. }
  914. if (bdev->bd_invalidated)
  915. rescan_partitions(bdev->bd_disk, bdev);
  916. } else {
  917. mutex_lock_nested(&bdev->bd_contains->bd_mutex,
  918. BD_MUTEX_WHOLE);
  919. bdev->bd_contains->bd_part_count++;
  920. mutex_unlock(&bdev->bd_contains->bd_mutex);
  921. }
  922. }
  923. bdev->bd_openers++;
  924. mutex_unlock(&bdev->bd_mutex);
  925. unlock_kernel();
  926. return 0;
  927. out_first:
  928. bdev->bd_disk = NULL;
  929. bdev->bd_inode->i_data.backing_dev_info = &default_backing_dev_info;
  930. if (bdev != bdev->bd_contains)
  931. __blkdev_put(bdev->bd_contains, BD_MUTEX_WHOLE);
  932. bdev->bd_contains = NULL;
  933. put_disk(disk);
  934. module_put(owner);
  935. out:
  936. mutex_unlock(&bdev->bd_mutex);
  937. unlock_kernel();
  938. if (ret)
  939. bdput(bdev);
  940. return ret;
  941. }
  942. int blkdev_get(struct block_device *bdev, mode_t mode, unsigned flags)
  943. {
  944. /*
  945. * This crockload is due to bad choice of ->open() type.
  946. * It will go away.
  947. * For now, block device ->open() routine must _not_
  948. * examine anything in 'inode' argument except ->i_rdev.
  949. */
  950. struct file fake_file = {};
  951. struct dentry fake_dentry = {};
  952. fake_file.f_mode = mode;
  953. fake_file.f_flags = flags;
  954. fake_file.f_dentry = &fake_dentry;
  955. fake_dentry.d_inode = bdev->bd_inode;
  956. return do_open(bdev, &fake_file, BD_MUTEX_NORMAL);
  957. }
  958. EXPORT_SYMBOL(blkdev_get);
  959. static int
  960. blkdev_get_whole(struct block_device *bdev, mode_t mode, unsigned flags)
  961. {
  962. /*
  963. * This crockload is due to bad choice of ->open() type.
  964. * It will go away.
  965. * For now, block device ->open() routine must _not_
  966. * examine anything in 'inode' argument except ->i_rdev.
  967. */
  968. struct file fake_file = {};
  969. struct dentry fake_dentry = {};
  970. fake_file.f_mode = mode;
  971. fake_file.f_flags = flags;
  972. fake_file.f_dentry = &fake_dentry;
  973. fake_dentry.d_inode = bdev->bd_inode;
  974. return do_open(bdev, &fake_file, BD_MUTEX_WHOLE);
  975. }
  976. static int
  977. blkdev_get_partition(struct block_device *bdev, mode_t mode, unsigned flags)
  978. {
  979. /*
  980. * This crockload is due to bad choice of ->open() type.
  981. * It will go away.
  982. * For now, block device ->open() routine must _not_
  983. * examine anything in 'inode' argument except ->i_rdev.
  984. */
  985. struct file fake_file = {};
  986. struct dentry fake_dentry = {};
  987. fake_file.f_mode = mode;
  988. fake_file.f_flags = flags;
  989. fake_file.f_dentry = &fake_dentry;
  990. fake_dentry.d_inode = bdev->bd_inode;
  991. return do_open(bdev, &fake_file, BD_MUTEX_PARTITION);
  992. }
  993. static int blkdev_open(struct inode * inode, struct file * filp)
  994. {
  995. struct block_device *bdev;
  996. int res;
  997. /*
  998. * Preserve backwards compatibility and allow large file access
  999. * even if userspace doesn't ask for it explicitly. Some mkfs
  1000. * binary needs it. We might want to drop this workaround
  1001. * during an unstable branch.
  1002. */
  1003. filp->f_flags |= O_LARGEFILE;
  1004. bdev = bd_acquire(inode);
  1005. if (bdev == NULL)
  1006. return -ENOMEM;
  1007. res = do_open(bdev, filp, BD_MUTEX_NORMAL);
  1008. if (res)
  1009. return res;
  1010. if (!(filp->f_flags & O_EXCL) )
  1011. return 0;
  1012. if (!(res = bd_claim(bdev, filp)))
  1013. return 0;
  1014. blkdev_put(bdev);
  1015. return res;
  1016. }
  1017. static int blkdev_close(struct inode * inode, struct file * filp)
  1018. {
  1019. struct block_device *bdev = I_BDEV(filp->f_mapping->host);
  1020. if (bdev->bd_holder == filp)
  1021. bd_release(bdev);
  1022. return blkdev_put(bdev);
  1023. }
  1024. static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg)
  1025. {
  1026. return blkdev_ioctl(file->f_mapping->host, file, cmd, arg);
  1027. }
  1028. const struct address_space_operations def_blk_aops = {
  1029. .readpage = blkdev_readpage,
  1030. .writepage = blkdev_writepage,
  1031. .sync_page = block_sync_page,
  1032. .prepare_write = blkdev_prepare_write,
  1033. .commit_write = blkdev_commit_write,
  1034. .writepages = generic_writepages,
  1035. .direct_IO = blkdev_direct_IO,
  1036. };
  1037. const struct file_operations def_blk_fops = {
  1038. .open = blkdev_open,
  1039. .release = blkdev_close,
  1040. .llseek = block_llseek,
  1041. .read = do_sync_read,
  1042. .write = do_sync_write,
  1043. .aio_read = generic_file_aio_read,
  1044. .aio_write = generic_file_aio_write_nolock,
  1045. .mmap = generic_file_mmap,
  1046. .fsync = block_fsync,
  1047. .unlocked_ioctl = block_ioctl,
  1048. #ifdef CONFIG_COMPAT
  1049. .compat_ioctl = compat_blkdev_ioctl,
  1050. #endif
  1051. .sendfile = generic_file_sendfile,
  1052. .splice_read = generic_file_splice_read,
  1053. .splice_write = generic_file_splice_write,
  1054. };
  1055. int ioctl_by_bdev(struct block_device *bdev, unsigned cmd, unsigned long arg)
  1056. {
  1057. int res;
  1058. mm_segment_t old_fs = get_fs();
  1059. set_fs(KERNEL_DS);
  1060. res = blkdev_ioctl(bdev->bd_inode, NULL, cmd, arg);
  1061. set_fs(old_fs);
  1062. return res;
  1063. }
  1064. EXPORT_SYMBOL(ioctl_by_bdev);
  1065. /**
  1066. * lookup_bdev - lookup a struct block_device by name
  1067. *
  1068. * @path: special file representing the block device
  1069. *
  1070. * Get a reference to the blockdevice at @path in the current
  1071. * namespace if possible and return it. Return ERR_PTR(error)
  1072. * otherwise.
  1073. */
  1074. struct block_device *lookup_bdev(const char *path)
  1075. {
  1076. struct block_device *bdev;
  1077. struct inode *inode;
  1078. struct nameidata nd;
  1079. int error;
  1080. if (!path || !*path)
  1081. return ERR_PTR(-EINVAL);
  1082. error = path_lookup(path, LOOKUP_FOLLOW, &nd);
  1083. if (error)
  1084. return ERR_PTR(error);
  1085. inode = nd.dentry->d_inode;
  1086. error = -ENOTBLK;
  1087. if (!S_ISBLK(inode->i_mode))
  1088. goto fail;
  1089. error = -EACCES;
  1090. if (nd.mnt->mnt_flags & MNT_NODEV)
  1091. goto fail;
  1092. error = -ENOMEM;
  1093. bdev = bd_acquire(inode);
  1094. if (!bdev)
  1095. goto fail;
  1096. out:
  1097. path_release(&nd);
  1098. return bdev;
  1099. fail:
  1100. bdev = ERR_PTR(error);
  1101. goto out;
  1102. }
  1103. /**
  1104. * open_bdev_excl - open a block device by name and set it up for use
  1105. *
  1106. * @path: special file representing the block device
  1107. * @flags: %MS_RDONLY for opening read-only
  1108. * @holder: owner for exclusion
  1109. *
  1110. * Open the blockdevice described by the special file at @path, claim it
  1111. * for the @holder.
  1112. */
  1113. struct block_device *open_bdev_excl(const char *path, int flags, void *holder)
  1114. {
  1115. struct block_device *bdev;
  1116. mode_t mode = FMODE_READ;
  1117. int error = 0;
  1118. bdev = lookup_bdev(path);
  1119. if (IS_ERR(bdev))
  1120. return bdev;
  1121. if (!(flags & MS_RDONLY))
  1122. mode |= FMODE_WRITE;
  1123. error = blkdev_get(bdev, mode, 0);
  1124. if (error)
  1125. return ERR_PTR(error);
  1126. error = -EACCES;
  1127. if (!(flags & MS_RDONLY) && bdev_read_only(bdev))
  1128. goto blkdev_put;
  1129. error = bd_claim(bdev, holder);
  1130. if (error)
  1131. goto blkdev_put;
  1132. return bdev;
  1133. blkdev_put:
  1134. blkdev_put(bdev);
  1135. return ERR_PTR(error);
  1136. }
  1137. EXPORT_SYMBOL(open_bdev_excl);
  1138. /**
  1139. * close_bdev_excl - release a blockdevice openen by open_bdev_excl()
  1140. *
  1141. * @bdev: blockdevice to close
  1142. *
  1143. * This is the counterpart to open_bdev_excl().
  1144. */
  1145. void close_bdev_excl(struct block_device *bdev)
  1146. {
  1147. bd_release(bdev);
  1148. blkdev_put(bdev);
  1149. }
  1150. EXPORT_SYMBOL(close_bdev_excl);
  1151. int __invalidate_device(struct block_device *bdev)
  1152. {
  1153. struct super_block *sb = get_super(bdev);
  1154. int res = 0;
  1155. if (sb) {
  1156. /*
  1157. * no need to lock the super, get_super holds the
  1158. * read mutex so the filesystem cannot go away
  1159. * under us (->put_super runs with the write lock
  1160. * hold).
  1161. */
  1162. shrink_dcache_sb(sb);
  1163. res = invalidate_inodes(sb);
  1164. drop_super(sb);
  1165. }
  1166. invalidate_bdev(bdev, 0);
  1167. return res;
  1168. }
  1169. EXPORT_SYMBOL(__invalidate_device);