block_dev.c 36 KB

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