block_dev.c 38 KB

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