block_dev.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807
  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. sb->s_frozen = SB_FREEZE_TRANS;
  220. up_write(&sb->s_umount);
  221. mutex_unlock(&bdev->bd_fsfreeze_mutex);
  222. return sb;
  223. }
  224. sb->s_frozen = SB_FREEZE_WRITE;
  225. smp_wmb();
  226. sync_filesystem(sb);
  227. sb->s_frozen = SB_FREEZE_TRANS;
  228. smp_wmb();
  229. sync_blockdev(sb->s_bdev);
  230. if (sb->s_op->freeze_fs) {
  231. error = sb->s_op->freeze_fs(sb);
  232. if (error) {
  233. printk(KERN_ERR
  234. "VFS:Filesystem freeze failed\n");
  235. sb->s_frozen = SB_UNFROZEN;
  236. deactivate_locked_super(sb);
  237. bdev->bd_fsfreeze_count--;
  238. mutex_unlock(&bdev->bd_fsfreeze_mutex);
  239. return ERR_PTR(error);
  240. }
  241. }
  242. up_write(&sb->s_umount);
  243. out:
  244. sync_blockdev(bdev);
  245. mutex_unlock(&bdev->bd_fsfreeze_mutex);
  246. return sb; /* thaw_bdev releases s->s_umount */
  247. }
  248. EXPORT_SYMBOL(freeze_bdev);
  249. /**
  250. * thaw_bdev -- unlock filesystem
  251. * @bdev: blockdevice to unlock
  252. * @sb: associated superblock
  253. *
  254. * Unlocks the filesystem and marks it writeable again after freeze_bdev().
  255. */
  256. int thaw_bdev(struct block_device *bdev, struct super_block *sb)
  257. {
  258. int error = -EINVAL;
  259. mutex_lock(&bdev->bd_fsfreeze_mutex);
  260. if (!bdev->bd_fsfreeze_count)
  261. goto out_unlock;
  262. error = 0;
  263. if (--bdev->bd_fsfreeze_count > 0)
  264. goto out_unlock;
  265. if (!sb)
  266. goto out_unlock;
  267. BUG_ON(sb->s_bdev != bdev);
  268. down_write(&sb->s_umount);
  269. if (sb->s_flags & MS_RDONLY)
  270. goto out_unfrozen;
  271. if (sb->s_op->unfreeze_fs) {
  272. error = sb->s_op->unfreeze_fs(sb);
  273. if (error) {
  274. printk(KERN_ERR
  275. "VFS:Filesystem thaw failed\n");
  276. sb->s_frozen = SB_FREEZE_TRANS;
  277. bdev->bd_fsfreeze_count++;
  278. mutex_unlock(&bdev->bd_fsfreeze_mutex);
  279. return error;
  280. }
  281. }
  282. out_unfrozen:
  283. sb->s_frozen = SB_UNFROZEN;
  284. smp_wmb();
  285. wake_up(&sb->s_wait_unfrozen);
  286. if (sb)
  287. deactivate_locked_super(sb);
  288. out_unlock:
  289. mutex_unlock(&bdev->bd_fsfreeze_mutex);
  290. return 0;
  291. }
  292. EXPORT_SYMBOL(thaw_bdev);
  293. static int blkdev_writepage(struct page *page, struct writeback_control *wbc)
  294. {
  295. return block_write_full_page(page, blkdev_get_block, wbc);
  296. }
  297. static int blkdev_readpage(struct file * file, struct page * page)
  298. {
  299. return block_read_full_page(page, blkdev_get_block);
  300. }
  301. static int blkdev_write_begin(struct file *file, struct address_space *mapping,
  302. loff_t pos, unsigned len, unsigned flags,
  303. struct page **pagep, void **fsdata)
  304. {
  305. *pagep = NULL;
  306. return block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
  307. blkdev_get_block);
  308. }
  309. static int blkdev_write_end(struct file *file, struct address_space *mapping,
  310. loff_t pos, unsigned len, unsigned copied,
  311. struct page *page, void *fsdata)
  312. {
  313. int ret;
  314. ret = block_write_end(file, mapping, pos, len, copied, page, fsdata);
  315. unlock_page(page);
  316. page_cache_release(page);
  317. return ret;
  318. }
  319. /*
  320. * private llseek:
  321. * for a block special file file->f_path.dentry->d_inode->i_size is zero
  322. * so we compute the size by hand (just as in block_read/write above)
  323. */
  324. static loff_t block_llseek(struct file *file, loff_t offset, int origin)
  325. {
  326. struct inode *bd_inode = file->f_mapping->host;
  327. loff_t size;
  328. loff_t retval;
  329. mutex_lock(&bd_inode->i_mutex);
  330. size = i_size_read(bd_inode);
  331. switch (origin) {
  332. case 2:
  333. offset += size;
  334. break;
  335. case 1:
  336. offset += file->f_pos;
  337. }
  338. retval = -EINVAL;
  339. if (offset >= 0 && offset <= size) {
  340. if (offset != file->f_pos) {
  341. file->f_pos = offset;
  342. }
  343. retval = offset;
  344. }
  345. mutex_unlock(&bd_inode->i_mutex);
  346. return retval;
  347. }
  348. /*
  349. * Filp is never NULL; the only case when ->fsync() is called with
  350. * NULL first argument is nfsd_sync_dir() and that's not a directory.
  351. */
  352. int blkdev_fsync(struct file *filp, struct dentry *dentry, int datasync)
  353. {
  354. struct inode *bd_inode = filp->f_mapping->host;
  355. struct block_device *bdev = I_BDEV(bd_inode);
  356. int error;
  357. /*
  358. * There is no need to serialise calls to blkdev_issue_flush with
  359. * i_mutex and doing so causes performance issues with concurrent
  360. * O_SYNC writers to a block device.
  361. */
  362. mutex_unlock(&bd_inode->i_mutex);
  363. error = blkdev_issue_flush(bdev, GFP_KERNEL, NULL, BLKDEV_IFL_WAIT);
  364. if (error == -EOPNOTSUPP)
  365. error = 0;
  366. mutex_lock(&bd_inode->i_mutex);
  367. return error;
  368. }
  369. EXPORT_SYMBOL(blkdev_fsync);
  370. /*
  371. * pseudo-fs
  372. */
  373. static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);
  374. static struct kmem_cache * bdev_cachep __read_mostly;
  375. static struct inode *bdev_alloc_inode(struct super_block *sb)
  376. {
  377. struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, GFP_KERNEL);
  378. if (!ei)
  379. return NULL;
  380. return &ei->vfs_inode;
  381. }
  382. static void bdev_destroy_inode(struct inode *inode)
  383. {
  384. struct bdev_inode *bdi = BDEV_I(inode);
  385. kmem_cache_free(bdev_cachep, bdi);
  386. }
  387. static void init_once(void *foo)
  388. {
  389. struct bdev_inode *ei = (struct bdev_inode *) foo;
  390. struct block_device *bdev = &ei->bdev;
  391. memset(bdev, 0, sizeof(*bdev));
  392. mutex_init(&bdev->bd_mutex);
  393. INIT_LIST_HEAD(&bdev->bd_inodes);
  394. INIT_LIST_HEAD(&bdev->bd_list);
  395. #ifdef CONFIG_SYSFS
  396. INIT_LIST_HEAD(&bdev->bd_holder_list);
  397. #endif
  398. inode_init_once(&ei->vfs_inode);
  399. /* Initialize mutex for freeze. */
  400. mutex_init(&bdev->bd_fsfreeze_mutex);
  401. }
  402. static inline void __bd_forget(struct inode *inode)
  403. {
  404. list_del_init(&inode->i_devices);
  405. inode->i_bdev = NULL;
  406. inode->i_mapping = &inode->i_data;
  407. }
  408. static void bdev_clear_inode(struct inode *inode)
  409. {
  410. struct block_device *bdev = &BDEV_I(inode)->bdev;
  411. struct list_head *p;
  412. spin_lock(&bdev_lock);
  413. while ( (p = bdev->bd_inodes.next) != &bdev->bd_inodes ) {
  414. __bd_forget(list_entry(p, struct inode, i_devices));
  415. }
  416. list_del_init(&bdev->bd_list);
  417. spin_unlock(&bdev_lock);
  418. }
  419. static const struct super_operations bdev_sops = {
  420. .statfs = simple_statfs,
  421. .alloc_inode = bdev_alloc_inode,
  422. .destroy_inode = bdev_destroy_inode,
  423. .drop_inode = generic_delete_inode,
  424. .clear_inode = bdev_clear_inode,
  425. };
  426. static int bd_get_sb(struct file_system_type *fs_type,
  427. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  428. {
  429. return get_sb_pseudo(fs_type, "bdev:", &bdev_sops, 0x62646576, mnt);
  430. }
  431. static struct file_system_type bd_type = {
  432. .name = "bdev",
  433. .get_sb = bd_get_sb,
  434. .kill_sb = kill_anon_super,
  435. };
  436. struct super_block *blockdev_superblock __read_mostly;
  437. void __init bdev_cache_init(void)
  438. {
  439. int err;
  440. struct vfsmount *bd_mnt;
  441. bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
  442. 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
  443. SLAB_MEM_SPREAD|SLAB_PANIC),
  444. init_once);
  445. err = register_filesystem(&bd_type);
  446. if (err)
  447. panic("Cannot register bdev pseudo-fs");
  448. bd_mnt = kern_mount(&bd_type);
  449. if (IS_ERR(bd_mnt))
  450. panic("Cannot create bdev pseudo-fs");
  451. /*
  452. * This vfsmount structure is only used to obtain the
  453. * blockdev_superblock, so tell kmemleak not to report it.
  454. */
  455. kmemleak_not_leak(bd_mnt);
  456. blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */
  457. }
  458. /*
  459. * Most likely _very_ bad one - but then it's hardly critical for small
  460. * /dev and can be fixed when somebody will need really large one.
  461. * Keep in mind that it will be fed through icache hash function too.
  462. */
  463. static inline unsigned long hash(dev_t dev)
  464. {
  465. return MAJOR(dev)+MINOR(dev);
  466. }
  467. static int bdev_test(struct inode *inode, void *data)
  468. {
  469. return BDEV_I(inode)->bdev.bd_dev == *(dev_t *)data;
  470. }
  471. static int bdev_set(struct inode *inode, void *data)
  472. {
  473. BDEV_I(inode)->bdev.bd_dev = *(dev_t *)data;
  474. return 0;
  475. }
  476. static LIST_HEAD(all_bdevs);
  477. struct block_device *bdget(dev_t dev)
  478. {
  479. struct block_device *bdev;
  480. struct inode *inode;
  481. inode = iget5_locked(blockdev_superblock, hash(dev),
  482. bdev_test, bdev_set, &dev);
  483. if (!inode)
  484. return NULL;
  485. bdev = &BDEV_I(inode)->bdev;
  486. if (inode->i_state & I_NEW) {
  487. bdev->bd_contains = NULL;
  488. bdev->bd_inode = inode;
  489. bdev->bd_block_size = (1 << inode->i_blkbits);
  490. bdev->bd_part_count = 0;
  491. bdev->bd_invalidated = 0;
  492. inode->i_mode = S_IFBLK;
  493. inode->i_rdev = dev;
  494. inode->i_bdev = bdev;
  495. inode->i_data.a_ops = &def_blk_aops;
  496. mapping_set_gfp_mask(&inode->i_data, GFP_USER);
  497. inode->i_data.backing_dev_info = &default_backing_dev_info;
  498. spin_lock(&bdev_lock);
  499. list_add(&bdev->bd_list, &all_bdevs);
  500. spin_unlock(&bdev_lock);
  501. unlock_new_inode(inode);
  502. }
  503. return bdev;
  504. }
  505. EXPORT_SYMBOL(bdget);
  506. /**
  507. * bdgrab -- Grab a reference to an already referenced block device
  508. * @bdev: Block device to grab a reference to.
  509. */
  510. struct block_device *bdgrab(struct block_device *bdev)
  511. {
  512. atomic_inc(&bdev->bd_inode->i_count);
  513. return bdev;
  514. }
  515. long nr_blockdev_pages(void)
  516. {
  517. struct block_device *bdev;
  518. long ret = 0;
  519. spin_lock(&bdev_lock);
  520. list_for_each_entry(bdev, &all_bdevs, bd_list) {
  521. ret += bdev->bd_inode->i_mapping->nrpages;
  522. }
  523. spin_unlock(&bdev_lock);
  524. return ret;
  525. }
  526. void bdput(struct block_device *bdev)
  527. {
  528. iput(bdev->bd_inode);
  529. }
  530. EXPORT_SYMBOL(bdput);
  531. static struct block_device *bd_acquire(struct inode *inode)
  532. {
  533. struct block_device *bdev;
  534. spin_lock(&bdev_lock);
  535. bdev = inode->i_bdev;
  536. if (bdev) {
  537. atomic_inc(&bdev->bd_inode->i_count);
  538. spin_unlock(&bdev_lock);
  539. return bdev;
  540. }
  541. spin_unlock(&bdev_lock);
  542. bdev = bdget(inode->i_rdev);
  543. if (bdev) {
  544. spin_lock(&bdev_lock);
  545. if (!inode->i_bdev) {
  546. /*
  547. * We take an additional bd_inode->i_count for inode,
  548. * and it's released in clear_inode() of inode.
  549. * So, we can access it via ->i_mapping always
  550. * without igrab().
  551. */
  552. atomic_inc(&bdev->bd_inode->i_count);
  553. inode->i_bdev = bdev;
  554. inode->i_mapping = bdev->bd_inode->i_mapping;
  555. list_add(&inode->i_devices, &bdev->bd_inodes);
  556. }
  557. spin_unlock(&bdev_lock);
  558. }
  559. return bdev;
  560. }
  561. /* Call when you free inode */
  562. void bd_forget(struct inode *inode)
  563. {
  564. struct block_device *bdev = NULL;
  565. spin_lock(&bdev_lock);
  566. if (inode->i_bdev) {
  567. if (!sb_is_blkdev_sb(inode->i_sb))
  568. bdev = inode->i_bdev;
  569. __bd_forget(inode);
  570. }
  571. spin_unlock(&bdev_lock);
  572. if (bdev)
  573. iput(bdev->bd_inode);
  574. }
  575. /**
  576. * bd_may_claim - test whether a block device can be claimed
  577. * @bdev: block device of interest
  578. * @whole: whole block device containing @bdev, may equal @bdev
  579. * @holder: holder trying to claim @bdev
  580. *
  581. * Test whther @bdev can be claimed by @holder.
  582. *
  583. * CONTEXT:
  584. * spin_lock(&bdev_lock).
  585. *
  586. * RETURNS:
  587. * %true if @bdev can be claimed, %false otherwise.
  588. */
  589. static bool bd_may_claim(struct block_device *bdev, struct block_device *whole,
  590. void *holder)
  591. {
  592. if (bdev->bd_holder == holder)
  593. return true; /* already a holder */
  594. else if (bdev->bd_holder != NULL)
  595. return false; /* held by someone else */
  596. else if (bdev->bd_contains == bdev)
  597. return true; /* is a whole device which isn't held */
  598. else if (whole->bd_holder == bd_claim)
  599. return true; /* is a partition of a device that is being partitioned */
  600. else if (whole->bd_holder != NULL)
  601. return false; /* is a partition of a held device */
  602. else
  603. return true; /* is a partition of an un-held device */
  604. }
  605. /**
  606. * bd_prepare_to_claim - prepare to claim a block device
  607. * @bdev: block device of interest
  608. * @whole: the whole device containing @bdev, may equal @bdev
  609. * @holder: holder trying to claim @bdev
  610. *
  611. * Prepare to claim @bdev. This function fails if @bdev is already
  612. * claimed by another holder and waits if another claiming is in
  613. * progress. This function doesn't actually claim. On successful
  614. * return, the caller has ownership of bd_claiming and bd_holder[s].
  615. *
  616. * CONTEXT:
  617. * spin_lock(&bdev_lock). Might release bdev_lock, sleep and regrab
  618. * it multiple times.
  619. *
  620. * RETURNS:
  621. * 0 if @bdev can be claimed, -EBUSY otherwise.
  622. */
  623. static int bd_prepare_to_claim(struct block_device *bdev,
  624. struct block_device *whole, void *holder)
  625. {
  626. retry:
  627. /* if someone else claimed, fail */
  628. if (!bd_may_claim(bdev, whole, holder))
  629. return -EBUSY;
  630. /* if someone else is claiming, wait for it to finish */
  631. if (whole->bd_claiming && whole->bd_claiming != holder) {
  632. wait_queue_head_t *wq = bit_waitqueue(&whole->bd_claiming, 0);
  633. DEFINE_WAIT(wait);
  634. prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
  635. spin_unlock(&bdev_lock);
  636. schedule();
  637. finish_wait(wq, &wait);
  638. spin_lock(&bdev_lock);
  639. goto retry;
  640. }
  641. /* yay, all mine */
  642. return 0;
  643. }
  644. /**
  645. * bd_start_claiming - start claiming a block device
  646. * @bdev: block device of interest
  647. * @holder: holder trying to claim @bdev
  648. *
  649. * @bdev is about to be opened exclusively. Check @bdev can be opened
  650. * exclusively and mark that an exclusive open is in progress. Each
  651. * successful call to this function must be matched with a call to
  652. * either bd_claim() or bd_abort_claiming(). If this function
  653. * succeeds, the matching bd_claim() is guaranteed to succeed.
  654. *
  655. * CONTEXT:
  656. * Might sleep.
  657. *
  658. * RETURNS:
  659. * Pointer to the block device containing @bdev on success, ERR_PTR()
  660. * value on failure.
  661. */
  662. static struct block_device *bd_start_claiming(struct block_device *bdev,
  663. void *holder)
  664. {
  665. struct gendisk *disk;
  666. struct block_device *whole;
  667. int partno, err;
  668. might_sleep();
  669. /*
  670. * @bdev might not have been initialized properly yet, look up
  671. * and grab the outer block device the hard way.
  672. */
  673. disk = get_gendisk(bdev->bd_dev, &partno);
  674. if (!disk)
  675. return ERR_PTR(-ENXIO);
  676. whole = bdget_disk(disk, 0);
  677. put_disk(disk);
  678. if (!whole)
  679. return ERR_PTR(-ENOMEM);
  680. /* prepare to claim, if successful, mark claiming in progress */
  681. spin_lock(&bdev_lock);
  682. err = bd_prepare_to_claim(bdev, whole, holder);
  683. if (err == 0) {
  684. whole->bd_claiming = holder;
  685. spin_unlock(&bdev_lock);
  686. return whole;
  687. } else {
  688. spin_unlock(&bdev_lock);
  689. bdput(whole);
  690. return ERR_PTR(err);
  691. }
  692. }
  693. /* releases bdev_lock */
  694. static void __bd_abort_claiming(struct block_device *whole, void *holder)
  695. {
  696. BUG_ON(whole->bd_claiming != holder);
  697. whole->bd_claiming = NULL;
  698. wake_up_bit(&whole->bd_claiming, 0);
  699. spin_unlock(&bdev_lock);
  700. bdput(whole);
  701. }
  702. /**
  703. * bd_abort_claiming - abort claiming a block device
  704. * @whole: whole block device returned by bd_start_claiming()
  705. * @holder: holder trying to claim @bdev
  706. *
  707. * Abort a claiming block started by bd_start_claiming(). Note that
  708. * @whole is not the block device to be claimed but the whole device
  709. * returned by bd_start_claiming().
  710. *
  711. * CONTEXT:
  712. * Grabs and releases bdev_lock.
  713. */
  714. static void bd_abort_claiming(struct block_device *whole, void *holder)
  715. {
  716. spin_lock(&bdev_lock);
  717. __bd_abort_claiming(whole, holder); /* releases bdev_lock */
  718. }
  719. /**
  720. * bd_claim - claim a block device
  721. * @bdev: block device to claim
  722. * @holder: holder trying to claim @bdev
  723. *
  724. * Try to claim @bdev which must have been opened successfully. This
  725. * function may be called with or without preceding
  726. * blk_start_claiming(). In the former case, this function is always
  727. * successful and terminates the claiming block.
  728. *
  729. * CONTEXT:
  730. * Might sleep.
  731. *
  732. * RETURNS:
  733. * 0 if successful, -EBUSY if @bdev is already claimed.
  734. */
  735. int bd_claim(struct block_device *bdev, void *holder)
  736. {
  737. struct block_device *whole = bdev->bd_contains;
  738. int res;
  739. might_sleep();
  740. spin_lock(&bdev_lock);
  741. res = bd_prepare_to_claim(bdev, whole, holder);
  742. if (res == 0) {
  743. /* note that for a whole device bd_holders
  744. * will be incremented twice, and bd_holder will
  745. * be set to bd_claim before being set to holder
  746. */
  747. whole->bd_holders++;
  748. whole->bd_holder = bd_claim;
  749. bdev->bd_holders++;
  750. bdev->bd_holder = holder;
  751. }
  752. if (whole->bd_claiming)
  753. __bd_abort_claiming(whole, holder); /* releases bdev_lock */
  754. else
  755. spin_unlock(&bdev_lock);
  756. return res;
  757. }
  758. EXPORT_SYMBOL(bd_claim);
  759. void bd_release(struct block_device *bdev)
  760. {
  761. spin_lock(&bdev_lock);
  762. if (!--bdev->bd_contains->bd_holders)
  763. bdev->bd_contains->bd_holder = NULL;
  764. if (!--bdev->bd_holders)
  765. bdev->bd_holder = NULL;
  766. spin_unlock(&bdev_lock);
  767. }
  768. EXPORT_SYMBOL(bd_release);
  769. #ifdef CONFIG_SYSFS
  770. /*
  771. * Functions for bd_claim_by_kobject / bd_release_from_kobject
  772. *
  773. * If a kobject is passed to bd_claim_by_kobject()
  774. * and the kobject has a parent directory,
  775. * following symlinks are created:
  776. * o from the kobject to the claimed bdev
  777. * o from "holders" directory of the bdev to the parent of the kobject
  778. * bd_release_from_kobject() removes these symlinks.
  779. *
  780. * Example:
  781. * If /dev/dm-0 maps to /dev/sda, kobject corresponding to
  782. * /sys/block/dm-0/slaves is passed to bd_claim_by_kobject(), then:
  783. * /sys/block/dm-0/slaves/sda --> /sys/block/sda
  784. * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
  785. */
  786. static int add_symlink(struct kobject *from, struct kobject *to)
  787. {
  788. if (!from || !to)
  789. return 0;
  790. return sysfs_create_link(from, to, kobject_name(to));
  791. }
  792. static void del_symlink(struct kobject *from, struct kobject *to)
  793. {
  794. if (!from || !to)
  795. return;
  796. sysfs_remove_link(from, kobject_name(to));
  797. }
  798. /*
  799. * 'struct bd_holder' contains pointers to kobjects symlinked by
  800. * bd_claim_by_kobject.
  801. * It's connected to bd_holder_list which is protected by bdev->bd_sem.
  802. */
  803. struct bd_holder {
  804. struct list_head list; /* chain of holders of the bdev */
  805. int count; /* references from the holder */
  806. struct kobject *sdir; /* holder object, e.g. "/block/dm-0/slaves" */
  807. struct kobject *hdev; /* e.g. "/block/dm-0" */
  808. struct kobject *hdir; /* e.g. "/block/sda/holders" */
  809. struct kobject *sdev; /* e.g. "/block/sda" */
  810. };
  811. /*
  812. * Get references of related kobjects at once.
  813. * Returns 1 on success. 0 on failure.
  814. *
  815. * Should call bd_holder_release_dirs() after successful use.
  816. */
  817. static int bd_holder_grab_dirs(struct block_device *bdev,
  818. struct bd_holder *bo)
  819. {
  820. if (!bdev || !bo)
  821. return 0;
  822. bo->sdir = kobject_get(bo->sdir);
  823. if (!bo->sdir)
  824. return 0;
  825. bo->hdev = kobject_get(bo->sdir->parent);
  826. if (!bo->hdev)
  827. goto fail_put_sdir;
  828. bo->sdev = kobject_get(&part_to_dev(bdev->bd_part)->kobj);
  829. if (!bo->sdev)
  830. goto fail_put_hdev;
  831. bo->hdir = kobject_get(bdev->bd_part->holder_dir);
  832. if (!bo->hdir)
  833. goto fail_put_sdev;
  834. return 1;
  835. fail_put_sdev:
  836. kobject_put(bo->sdev);
  837. fail_put_hdev:
  838. kobject_put(bo->hdev);
  839. fail_put_sdir:
  840. kobject_put(bo->sdir);
  841. return 0;
  842. }
  843. /* Put references of related kobjects at once. */
  844. static void bd_holder_release_dirs(struct bd_holder *bo)
  845. {
  846. kobject_put(bo->hdir);
  847. kobject_put(bo->sdev);
  848. kobject_put(bo->hdev);
  849. kobject_put(bo->sdir);
  850. }
  851. static struct bd_holder *alloc_bd_holder(struct kobject *kobj)
  852. {
  853. struct bd_holder *bo;
  854. bo = kzalloc(sizeof(*bo), GFP_KERNEL);
  855. if (!bo)
  856. return NULL;
  857. bo->count = 1;
  858. bo->sdir = kobj;
  859. return bo;
  860. }
  861. static void free_bd_holder(struct bd_holder *bo)
  862. {
  863. kfree(bo);
  864. }
  865. /**
  866. * find_bd_holder - find matching struct bd_holder from the block device
  867. *
  868. * @bdev: struct block device to be searched
  869. * @bo: target struct bd_holder
  870. *
  871. * Returns matching entry with @bo in @bdev->bd_holder_list.
  872. * If found, increment the reference count and return the pointer.
  873. * If not found, returns NULL.
  874. */
  875. static struct bd_holder *find_bd_holder(struct block_device *bdev,
  876. struct bd_holder *bo)
  877. {
  878. struct bd_holder *tmp;
  879. list_for_each_entry(tmp, &bdev->bd_holder_list, list)
  880. if (tmp->sdir == bo->sdir) {
  881. tmp->count++;
  882. return tmp;
  883. }
  884. return NULL;
  885. }
  886. /**
  887. * add_bd_holder - create sysfs symlinks for bd_claim() relationship
  888. *
  889. * @bdev: block device to be bd_claimed
  890. * @bo: preallocated and initialized by alloc_bd_holder()
  891. *
  892. * Add @bo to @bdev->bd_holder_list, create symlinks.
  893. *
  894. * Returns 0 if symlinks are created.
  895. * Returns -ve if something fails.
  896. */
  897. static int add_bd_holder(struct block_device *bdev, struct bd_holder *bo)
  898. {
  899. int err;
  900. if (!bo)
  901. return -EINVAL;
  902. if (!bd_holder_grab_dirs(bdev, bo))
  903. return -EBUSY;
  904. err = add_symlink(bo->sdir, bo->sdev);
  905. if (err)
  906. return err;
  907. err = add_symlink(bo->hdir, bo->hdev);
  908. if (err) {
  909. del_symlink(bo->sdir, bo->sdev);
  910. return err;
  911. }
  912. list_add_tail(&bo->list, &bdev->bd_holder_list);
  913. return 0;
  914. }
  915. /**
  916. * del_bd_holder - delete sysfs symlinks for bd_claim() relationship
  917. *
  918. * @bdev: block device to be bd_claimed
  919. * @kobj: holder's kobject
  920. *
  921. * If there is matching entry with @kobj in @bdev->bd_holder_list
  922. * and no other bd_claim() from the same kobject,
  923. * remove the struct bd_holder from the list, delete symlinks for it.
  924. *
  925. * Returns a pointer to the struct bd_holder when it's removed from the list
  926. * and ready to be freed.
  927. * Returns NULL if matching claim isn't found or there is other bd_claim()
  928. * by the same kobject.
  929. */
  930. static struct bd_holder *del_bd_holder(struct block_device *bdev,
  931. struct kobject *kobj)
  932. {
  933. struct bd_holder *bo;
  934. list_for_each_entry(bo, &bdev->bd_holder_list, list) {
  935. if (bo->sdir == kobj) {
  936. bo->count--;
  937. BUG_ON(bo->count < 0);
  938. if (!bo->count) {
  939. list_del(&bo->list);
  940. del_symlink(bo->sdir, bo->sdev);
  941. del_symlink(bo->hdir, bo->hdev);
  942. bd_holder_release_dirs(bo);
  943. return bo;
  944. }
  945. break;
  946. }
  947. }
  948. return NULL;
  949. }
  950. /**
  951. * bd_claim_by_kobject - bd_claim() with additional kobject signature
  952. *
  953. * @bdev: block device to be claimed
  954. * @holder: holder's signature
  955. * @kobj: holder's kobject
  956. *
  957. * Do bd_claim() and if it succeeds, create sysfs symlinks between
  958. * the bdev and the holder's kobject.
  959. * Use bd_release_from_kobject() when relesing the claimed bdev.
  960. *
  961. * Returns 0 on success. (same as bd_claim())
  962. * Returns errno on failure.
  963. */
  964. static int bd_claim_by_kobject(struct block_device *bdev, void *holder,
  965. struct kobject *kobj)
  966. {
  967. int err;
  968. struct bd_holder *bo, *found;
  969. if (!kobj)
  970. return -EINVAL;
  971. bo = alloc_bd_holder(kobj);
  972. if (!bo)
  973. return -ENOMEM;
  974. mutex_lock(&bdev->bd_mutex);
  975. err = bd_claim(bdev, holder);
  976. if (err)
  977. goto fail;
  978. found = find_bd_holder(bdev, bo);
  979. if (found)
  980. goto fail;
  981. err = add_bd_holder(bdev, bo);
  982. if (err)
  983. bd_release(bdev);
  984. else
  985. bo = NULL;
  986. fail:
  987. mutex_unlock(&bdev->bd_mutex);
  988. free_bd_holder(bo);
  989. return err;
  990. }
  991. /**
  992. * bd_release_from_kobject - bd_release() with additional kobject signature
  993. *
  994. * @bdev: block device to be released
  995. * @kobj: holder's kobject
  996. *
  997. * Do bd_release() and remove sysfs symlinks created by bd_claim_by_kobject().
  998. */
  999. static void bd_release_from_kobject(struct block_device *bdev,
  1000. struct kobject *kobj)
  1001. {
  1002. if (!kobj)
  1003. return;
  1004. mutex_lock(&bdev->bd_mutex);
  1005. bd_release(bdev);
  1006. free_bd_holder(del_bd_holder(bdev, kobj));
  1007. mutex_unlock(&bdev->bd_mutex);
  1008. }
  1009. /**
  1010. * bd_claim_by_disk - wrapper function for bd_claim_by_kobject()
  1011. *
  1012. * @bdev: block device to be claimed
  1013. * @holder: holder's signature
  1014. * @disk: holder's gendisk
  1015. *
  1016. * Call bd_claim_by_kobject() with getting @disk->slave_dir.
  1017. */
  1018. int bd_claim_by_disk(struct block_device *bdev, void *holder,
  1019. struct gendisk *disk)
  1020. {
  1021. return bd_claim_by_kobject(bdev, holder, kobject_get(disk->slave_dir));
  1022. }
  1023. EXPORT_SYMBOL_GPL(bd_claim_by_disk);
  1024. /**
  1025. * bd_release_from_disk - wrapper function for bd_release_from_kobject()
  1026. *
  1027. * @bdev: block device to be claimed
  1028. * @disk: holder's gendisk
  1029. *
  1030. * Call bd_release_from_kobject() and put @disk->slave_dir.
  1031. */
  1032. void bd_release_from_disk(struct block_device *bdev, struct gendisk *disk)
  1033. {
  1034. bd_release_from_kobject(bdev, disk->slave_dir);
  1035. kobject_put(disk->slave_dir);
  1036. }
  1037. EXPORT_SYMBOL_GPL(bd_release_from_disk);
  1038. #endif
  1039. /*
  1040. * Tries to open block device by device number. Use it ONLY if you
  1041. * really do not have anything better - i.e. when you are behind a
  1042. * truly sucky interface and all you are given is a device number. _Never_
  1043. * to be used for internal purposes. If you ever need it - reconsider
  1044. * your API.
  1045. */
  1046. struct block_device *open_by_devnum(dev_t dev, fmode_t mode)
  1047. {
  1048. struct block_device *bdev = bdget(dev);
  1049. int err = -ENOMEM;
  1050. if (bdev)
  1051. err = blkdev_get(bdev, mode);
  1052. return err ? ERR_PTR(err) : bdev;
  1053. }
  1054. EXPORT_SYMBOL(open_by_devnum);
  1055. /**
  1056. * flush_disk - invalidates all buffer-cache entries on a disk
  1057. *
  1058. * @bdev: struct block device to be flushed
  1059. *
  1060. * Invalidates all buffer-cache entries on a disk. It should be called
  1061. * when a disk has been changed -- either by a media change or online
  1062. * resize.
  1063. */
  1064. static void flush_disk(struct block_device *bdev)
  1065. {
  1066. if (__invalidate_device(bdev)) {
  1067. char name[BDEVNAME_SIZE] = "";
  1068. if (bdev->bd_disk)
  1069. disk_name(bdev->bd_disk, 0, name);
  1070. printk(KERN_WARNING "VFS: busy inodes on changed media or "
  1071. "resized disk %s\n", name);
  1072. }
  1073. if (!bdev->bd_disk)
  1074. return;
  1075. if (disk_partitionable(bdev->bd_disk))
  1076. bdev->bd_invalidated = 1;
  1077. }
  1078. /**
  1079. * check_disk_size_change - checks for disk size change and adjusts bdev size.
  1080. * @disk: struct gendisk to check
  1081. * @bdev: struct bdev to adjust.
  1082. *
  1083. * This routine checks to see if the bdev size does not match the disk size
  1084. * and adjusts it if it differs.
  1085. */
  1086. void check_disk_size_change(struct gendisk *disk, struct block_device *bdev)
  1087. {
  1088. loff_t disk_size, bdev_size;
  1089. disk_size = (loff_t)get_capacity(disk) << 9;
  1090. bdev_size = i_size_read(bdev->bd_inode);
  1091. if (disk_size != bdev_size) {
  1092. char name[BDEVNAME_SIZE];
  1093. disk_name(disk, 0, name);
  1094. printk(KERN_INFO
  1095. "%s: detected capacity change from %lld to %lld\n",
  1096. name, bdev_size, disk_size);
  1097. i_size_write(bdev->bd_inode, disk_size);
  1098. flush_disk(bdev);
  1099. }
  1100. }
  1101. EXPORT_SYMBOL(check_disk_size_change);
  1102. /**
  1103. * revalidate_disk - wrapper for lower-level driver's revalidate_disk call-back
  1104. * @disk: struct gendisk to be revalidated
  1105. *
  1106. * This routine is a wrapper for lower-level driver's revalidate_disk
  1107. * call-backs. It is used to do common pre and post operations needed
  1108. * for all revalidate_disk operations.
  1109. */
  1110. int revalidate_disk(struct gendisk *disk)
  1111. {
  1112. struct block_device *bdev;
  1113. int ret = 0;
  1114. if (disk->fops->revalidate_disk)
  1115. ret = disk->fops->revalidate_disk(disk);
  1116. bdev = bdget_disk(disk, 0);
  1117. if (!bdev)
  1118. return ret;
  1119. mutex_lock(&bdev->bd_mutex);
  1120. check_disk_size_change(disk, bdev);
  1121. mutex_unlock(&bdev->bd_mutex);
  1122. bdput(bdev);
  1123. return ret;
  1124. }
  1125. EXPORT_SYMBOL(revalidate_disk);
  1126. /*
  1127. * This routine checks whether a removable media has been changed,
  1128. * and invalidates all buffer-cache-entries in that case. This
  1129. * is a relatively slow routine, so we have to try to minimize using
  1130. * it. Thus it is called only upon a 'mount' or 'open'. This
  1131. * is the best way of combining speed and utility, I think.
  1132. * People changing diskettes in the middle of an operation deserve
  1133. * to lose :-)
  1134. */
  1135. int check_disk_change(struct block_device *bdev)
  1136. {
  1137. struct gendisk *disk = bdev->bd_disk;
  1138. const struct block_device_operations *bdops = disk->fops;
  1139. if (!bdops->media_changed)
  1140. return 0;
  1141. if (!bdops->media_changed(bdev->bd_disk))
  1142. return 0;
  1143. flush_disk(bdev);
  1144. if (bdops->revalidate_disk)
  1145. bdops->revalidate_disk(bdev->bd_disk);
  1146. return 1;
  1147. }
  1148. EXPORT_SYMBOL(check_disk_change);
  1149. void bd_set_size(struct block_device *bdev, loff_t size)
  1150. {
  1151. unsigned bsize = bdev_logical_block_size(bdev);
  1152. bdev->bd_inode->i_size = size;
  1153. while (bsize < PAGE_CACHE_SIZE) {
  1154. if (size & bsize)
  1155. break;
  1156. bsize <<= 1;
  1157. }
  1158. bdev->bd_block_size = bsize;
  1159. bdev->bd_inode->i_blkbits = blksize_bits(bsize);
  1160. }
  1161. EXPORT_SYMBOL(bd_set_size);
  1162. static int __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part);
  1163. /*
  1164. * bd_mutex locking:
  1165. *
  1166. * mutex_lock(part->bd_mutex)
  1167. * mutex_lock_nested(whole->bd_mutex, 1)
  1168. */
  1169. static int __blkdev_get(struct block_device *bdev, fmode_t mode, int for_part)
  1170. {
  1171. struct gendisk *disk;
  1172. int ret;
  1173. int partno;
  1174. int perm = 0;
  1175. if (mode & FMODE_READ)
  1176. perm |= MAY_READ;
  1177. if (mode & FMODE_WRITE)
  1178. perm |= MAY_WRITE;
  1179. /*
  1180. * hooks: /n/, see "layering violations".
  1181. */
  1182. ret = devcgroup_inode_permission(bdev->bd_inode, perm);
  1183. if (ret != 0) {
  1184. bdput(bdev);
  1185. return ret;
  1186. }
  1187. lock_kernel();
  1188. restart:
  1189. ret = -ENXIO;
  1190. disk = get_gendisk(bdev->bd_dev, &partno);
  1191. if (!disk)
  1192. goto out_unlock_kernel;
  1193. mutex_lock_nested(&bdev->bd_mutex, for_part);
  1194. if (!bdev->bd_openers) {
  1195. bdev->bd_disk = disk;
  1196. bdev->bd_contains = bdev;
  1197. if (!partno) {
  1198. struct backing_dev_info *bdi;
  1199. ret = -ENXIO;
  1200. bdev->bd_part = disk_get_part(disk, partno);
  1201. if (!bdev->bd_part)
  1202. goto out_clear;
  1203. if (disk->fops->open) {
  1204. ret = disk->fops->open(bdev, mode);
  1205. if (ret == -ERESTARTSYS) {
  1206. /* Lost a race with 'disk' being
  1207. * deleted, try again.
  1208. * See md.c
  1209. */
  1210. disk_put_part(bdev->bd_part);
  1211. bdev->bd_part = NULL;
  1212. module_put(disk->fops->owner);
  1213. put_disk(disk);
  1214. bdev->bd_disk = NULL;
  1215. mutex_unlock(&bdev->bd_mutex);
  1216. goto restart;
  1217. }
  1218. if (ret)
  1219. goto out_clear;
  1220. }
  1221. if (!bdev->bd_openers) {
  1222. bd_set_size(bdev,(loff_t)get_capacity(disk)<<9);
  1223. bdi = blk_get_backing_dev_info(bdev);
  1224. if (bdi == NULL)
  1225. bdi = &default_backing_dev_info;
  1226. bdev->bd_inode->i_data.backing_dev_info = bdi;
  1227. }
  1228. if (bdev->bd_invalidated)
  1229. rescan_partitions(disk, bdev);
  1230. } else {
  1231. struct block_device *whole;
  1232. whole = bdget_disk(disk, 0);
  1233. ret = -ENOMEM;
  1234. if (!whole)
  1235. goto out_clear;
  1236. BUG_ON(for_part);
  1237. ret = __blkdev_get(whole, mode, 1);
  1238. if (ret)
  1239. goto out_clear;
  1240. bdev->bd_contains = whole;
  1241. bdev->bd_inode->i_data.backing_dev_info =
  1242. whole->bd_inode->i_data.backing_dev_info;
  1243. bdev->bd_part = disk_get_part(disk, partno);
  1244. if (!(disk->flags & GENHD_FL_UP) ||
  1245. !bdev->bd_part || !bdev->bd_part->nr_sects) {
  1246. ret = -ENXIO;
  1247. goto out_clear;
  1248. }
  1249. bd_set_size(bdev, (loff_t)bdev->bd_part->nr_sects << 9);
  1250. }
  1251. } else {
  1252. module_put(disk->fops->owner);
  1253. put_disk(disk);
  1254. disk = NULL;
  1255. if (bdev->bd_contains == bdev) {
  1256. if (bdev->bd_disk->fops->open) {
  1257. ret = bdev->bd_disk->fops->open(bdev, mode);
  1258. if (ret)
  1259. goto out_unlock_bdev;
  1260. }
  1261. if (bdev->bd_invalidated)
  1262. rescan_partitions(bdev->bd_disk, bdev);
  1263. }
  1264. }
  1265. bdev->bd_openers++;
  1266. if (for_part)
  1267. bdev->bd_part_count++;
  1268. mutex_unlock(&bdev->bd_mutex);
  1269. unlock_kernel();
  1270. return 0;
  1271. out_clear:
  1272. disk_put_part(bdev->bd_part);
  1273. bdev->bd_disk = NULL;
  1274. bdev->bd_part = NULL;
  1275. bdev->bd_inode->i_data.backing_dev_info = &default_backing_dev_info;
  1276. if (bdev != bdev->bd_contains)
  1277. __blkdev_put(bdev->bd_contains, mode, 1);
  1278. bdev->bd_contains = NULL;
  1279. out_unlock_bdev:
  1280. mutex_unlock(&bdev->bd_mutex);
  1281. out_unlock_kernel:
  1282. unlock_kernel();
  1283. if (disk)
  1284. module_put(disk->fops->owner);
  1285. put_disk(disk);
  1286. bdput(bdev);
  1287. return ret;
  1288. }
  1289. int blkdev_get(struct block_device *bdev, fmode_t mode)
  1290. {
  1291. return __blkdev_get(bdev, mode, 0);
  1292. }
  1293. EXPORT_SYMBOL(blkdev_get);
  1294. static int blkdev_open(struct inode * inode, struct file * filp)
  1295. {
  1296. struct block_device *whole = NULL;
  1297. struct block_device *bdev;
  1298. int res;
  1299. /*
  1300. * Preserve backwards compatibility and allow large file access
  1301. * even if userspace doesn't ask for it explicitly. Some mkfs
  1302. * binary needs it. We might want to drop this workaround
  1303. * during an unstable branch.
  1304. */
  1305. filp->f_flags |= O_LARGEFILE;
  1306. if (filp->f_flags & O_NDELAY)
  1307. filp->f_mode |= FMODE_NDELAY;
  1308. if (filp->f_flags & O_EXCL)
  1309. filp->f_mode |= FMODE_EXCL;
  1310. if ((filp->f_flags & O_ACCMODE) == 3)
  1311. filp->f_mode |= FMODE_WRITE_IOCTL;
  1312. bdev = bd_acquire(inode);
  1313. if (bdev == NULL)
  1314. return -ENOMEM;
  1315. if (filp->f_mode & FMODE_EXCL) {
  1316. whole = bd_start_claiming(bdev, filp);
  1317. if (IS_ERR(whole)) {
  1318. bdput(bdev);
  1319. return PTR_ERR(whole);
  1320. }
  1321. }
  1322. filp->f_mapping = bdev->bd_inode->i_mapping;
  1323. res = blkdev_get(bdev, filp->f_mode);
  1324. if (whole) {
  1325. if (res == 0)
  1326. BUG_ON(bd_claim(bdev, filp) != 0);
  1327. else
  1328. bd_abort_claiming(whole, filp);
  1329. }
  1330. return res;
  1331. }
  1332. static int __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part)
  1333. {
  1334. int ret = 0;
  1335. struct gendisk *disk = bdev->bd_disk;
  1336. struct block_device *victim = NULL;
  1337. mutex_lock_nested(&bdev->bd_mutex, for_part);
  1338. lock_kernel();
  1339. if (for_part)
  1340. bdev->bd_part_count--;
  1341. if (!--bdev->bd_openers) {
  1342. sync_blockdev(bdev);
  1343. kill_bdev(bdev);
  1344. }
  1345. if (bdev->bd_contains == bdev) {
  1346. if (disk->fops->release)
  1347. ret = disk->fops->release(disk, mode);
  1348. }
  1349. if (!bdev->bd_openers) {
  1350. struct module *owner = disk->fops->owner;
  1351. put_disk(disk);
  1352. module_put(owner);
  1353. disk_put_part(bdev->bd_part);
  1354. bdev->bd_part = NULL;
  1355. bdev->bd_disk = NULL;
  1356. bdev->bd_inode->i_data.backing_dev_info = &default_backing_dev_info;
  1357. if (bdev != bdev->bd_contains)
  1358. victim = bdev->bd_contains;
  1359. bdev->bd_contains = NULL;
  1360. }
  1361. unlock_kernel();
  1362. mutex_unlock(&bdev->bd_mutex);
  1363. bdput(bdev);
  1364. if (victim)
  1365. __blkdev_put(victim, mode, 1);
  1366. return ret;
  1367. }
  1368. int blkdev_put(struct block_device *bdev, fmode_t mode)
  1369. {
  1370. return __blkdev_put(bdev, mode, 0);
  1371. }
  1372. EXPORT_SYMBOL(blkdev_put);
  1373. static int blkdev_close(struct inode * inode, struct file * filp)
  1374. {
  1375. struct block_device *bdev = I_BDEV(filp->f_mapping->host);
  1376. if (bdev->bd_holder == filp)
  1377. bd_release(bdev);
  1378. return blkdev_put(bdev, filp->f_mode);
  1379. }
  1380. static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg)
  1381. {
  1382. struct block_device *bdev = I_BDEV(file->f_mapping->host);
  1383. fmode_t mode = file->f_mode;
  1384. /*
  1385. * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
  1386. * to updated it before every ioctl.
  1387. */
  1388. if (file->f_flags & O_NDELAY)
  1389. mode |= FMODE_NDELAY;
  1390. else
  1391. mode &= ~FMODE_NDELAY;
  1392. return blkdev_ioctl(bdev, mode, cmd, arg);
  1393. }
  1394. /*
  1395. * Write data to the block device. Only intended for the block device itself
  1396. * and the raw driver which basically is a fake block device.
  1397. *
  1398. * Does not take i_mutex for the write and thus is not for general purpose
  1399. * use.
  1400. */
  1401. ssize_t blkdev_aio_write(struct kiocb *iocb, const struct iovec *iov,
  1402. unsigned long nr_segs, loff_t pos)
  1403. {
  1404. struct file *file = iocb->ki_filp;
  1405. ssize_t ret;
  1406. BUG_ON(iocb->ki_pos != pos);
  1407. ret = __generic_file_aio_write(iocb, iov, nr_segs, &iocb->ki_pos);
  1408. if (ret > 0 || ret == -EIOCBQUEUED) {
  1409. ssize_t err;
  1410. err = generic_write_sync(file, pos, ret);
  1411. if (err < 0 && ret > 0)
  1412. ret = err;
  1413. }
  1414. return ret;
  1415. }
  1416. EXPORT_SYMBOL_GPL(blkdev_aio_write);
  1417. /*
  1418. * Try to release a page associated with block device when the system
  1419. * is under memory pressure.
  1420. */
  1421. static int blkdev_releasepage(struct page *page, gfp_t wait)
  1422. {
  1423. struct super_block *super = BDEV_I(page->mapping->host)->bdev.bd_super;
  1424. if (super && super->s_op->bdev_try_to_free_page)
  1425. return super->s_op->bdev_try_to_free_page(super, page, wait);
  1426. return try_to_free_buffers(page);
  1427. }
  1428. static const struct address_space_operations def_blk_aops = {
  1429. .readpage = blkdev_readpage,
  1430. .writepage = blkdev_writepage,
  1431. .sync_page = block_sync_page,
  1432. .write_begin = blkdev_write_begin,
  1433. .write_end = blkdev_write_end,
  1434. .writepages = generic_writepages,
  1435. .releasepage = blkdev_releasepage,
  1436. .direct_IO = blkdev_direct_IO,
  1437. };
  1438. const struct file_operations def_blk_fops = {
  1439. .open = blkdev_open,
  1440. .release = blkdev_close,
  1441. .llseek = block_llseek,
  1442. .read = do_sync_read,
  1443. .write = do_sync_write,
  1444. .aio_read = generic_file_aio_read,
  1445. .aio_write = blkdev_aio_write,
  1446. .mmap = generic_file_mmap,
  1447. .fsync = blkdev_fsync,
  1448. .unlocked_ioctl = block_ioctl,
  1449. #ifdef CONFIG_COMPAT
  1450. .compat_ioctl = compat_blkdev_ioctl,
  1451. #endif
  1452. .splice_read = generic_file_splice_read,
  1453. .splice_write = generic_file_splice_write,
  1454. };
  1455. int ioctl_by_bdev(struct block_device *bdev, unsigned cmd, unsigned long arg)
  1456. {
  1457. int res;
  1458. mm_segment_t old_fs = get_fs();
  1459. set_fs(KERNEL_DS);
  1460. res = blkdev_ioctl(bdev, 0, cmd, arg);
  1461. set_fs(old_fs);
  1462. return res;
  1463. }
  1464. EXPORT_SYMBOL(ioctl_by_bdev);
  1465. /**
  1466. * lookup_bdev - lookup a struct block_device by name
  1467. * @pathname: special file representing the block device
  1468. *
  1469. * Get a reference to the blockdevice at @pathname in the current
  1470. * namespace if possible and return it. Return ERR_PTR(error)
  1471. * otherwise.
  1472. */
  1473. struct block_device *lookup_bdev(const char *pathname)
  1474. {
  1475. struct block_device *bdev;
  1476. struct inode *inode;
  1477. struct path path;
  1478. int error;
  1479. if (!pathname || !*pathname)
  1480. return ERR_PTR(-EINVAL);
  1481. error = kern_path(pathname, LOOKUP_FOLLOW, &path);
  1482. if (error)
  1483. return ERR_PTR(error);
  1484. inode = path.dentry->d_inode;
  1485. error = -ENOTBLK;
  1486. if (!S_ISBLK(inode->i_mode))
  1487. goto fail;
  1488. error = -EACCES;
  1489. if (path.mnt->mnt_flags & MNT_NODEV)
  1490. goto fail;
  1491. error = -ENOMEM;
  1492. bdev = bd_acquire(inode);
  1493. if (!bdev)
  1494. goto fail;
  1495. out:
  1496. path_put(&path);
  1497. return bdev;
  1498. fail:
  1499. bdev = ERR_PTR(error);
  1500. goto out;
  1501. }
  1502. EXPORT_SYMBOL(lookup_bdev);
  1503. /**
  1504. * open_bdev_exclusive - open a block device by name and set it up for use
  1505. *
  1506. * @path: special file representing the block device
  1507. * @mode: FMODE_... combination to pass be used
  1508. * @holder: owner for exclusion
  1509. *
  1510. * Open the blockdevice described by the special file at @path, claim it
  1511. * for the @holder.
  1512. */
  1513. struct block_device *open_bdev_exclusive(const char *path, fmode_t mode, void *holder)
  1514. {
  1515. struct block_device *bdev, *whole;
  1516. int error;
  1517. bdev = lookup_bdev(path);
  1518. if (IS_ERR(bdev))
  1519. return bdev;
  1520. whole = bd_start_claiming(bdev, holder);
  1521. if (IS_ERR(whole)) {
  1522. bdput(bdev);
  1523. return whole;
  1524. }
  1525. error = blkdev_get(bdev, mode);
  1526. if (error)
  1527. goto out_abort_claiming;
  1528. error = -EACCES;
  1529. if ((mode & FMODE_WRITE) && bdev_read_only(bdev))
  1530. goto out_blkdev_put;
  1531. BUG_ON(bd_claim(bdev, holder) != 0);
  1532. return bdev;
  1533. out_blkdev_put:
  1534. blkdev_put(bdev, mode);
  1535. out_abort_claiming:
  1536. bd_abort_claiming(whole, holder);
  1537. return ERR_PTR(error);
  1538. }
  1539. EXPORT_SYMBOL(open_bdev_exclusive);
  1540. /**
  1541. * close_bdev_exclusive - close a blockdevice opened by open_bdev_exclusive()
  1542. *
  1543. * @bdev: blockdevice to close
  1544. * @mode: mode, must match that used to open.
  1545. *
  1546. * This is the counterpart to open_bdev_exclusive().
  1547. */
  1548. void close_bdev_exclusive(struct block_device *bdev, fmode_t mode)
  1549. {
  1550. bd_release(bdev);
  1551. blkdev_put(bdev, mode);
  1552. }
  1553. EXPORT_SYMBOL(close_bdev_exclusive);
  1554. int __invalidate_device(struct block_device *bdev)
  1555. {
  1556. struct super_block *sb = get_super(bdev);
  1557. int res = 0;
  1558. if (sb) {
  1559. /*
  1560. * no need to lock the super, get_super holds the
  1561. * read mutex so the filesystem cannot go away
  1562. * under us (->put_super runs with the write lock
  1563. * hold).
  1564. */
  1565. shrink_dcache_sb(sb);
  1566. res = invalidate_inodes(sb);
  1567. drop_super(sb);
  1568. }
  1569. invalidate_bdev(bdev);
  1570. return res;
  1571. }
  1572. EXPORT_SYMBOL(__invalidate_device);