block_dev.c 42 KB

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