block_dev.c 44 KB

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