block_dev.c 42 KB

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