block_dev.c 37 KB

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