block_dev.c 37 KB

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