block_dev.c 36 KB

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