block_dev.c 42 KB

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