block_dev.c 41 KB

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