block_dev.c 43 KB

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