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