block_dev.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485
  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. if (whole) {
  1009. /* finish claiming */
  1010. spin_lock(&bdev_lock);
  1011. if (res == 0) {
  1012. BUG_ON(!bd_may_claim(bdev, whole, holder));
  1013. /*
  1014. * Note that for a whole device bd_holders
  1015. * will be incremented twice, and bd_holder
  1016. * will be set to bd_may_claim before being
  1017. * set to holder
  1018. */
  1019. whole->bd_holders++;
  1020. whole->bd_holder = bd_may_claim;
  1021. bdev->bd_holders++;
  1022. bdev->bd_holder = holder;
  1023. }
  1024. /* tell others that we're done */
  1025. BUG_ON(whole->bd_claiming != holder);
  1026. whole->bd_claiming = NULL;
  1027. wake_up_bit(&whole->bd_claiming, 0);
  1028. spin_unlock(&bdev_lock);
  1029. bdput(whole);
  1030. }
  1031. return res;
  1032. }
  1033. EXPORT_SYMBOL(blkdev_get);
  1034. static int blkdev_open(struct inode * inode, struct file * filp)
  1035. {
  1036. struct block_device *bdev;
  1037. /*
  1038. * Preserve backwards compatibility and allow large file access
  1039. * even if userspace doesn't ask for it explicitly. Some mkfs
  1040. * binary needs it. We might want to drop this workaround
  1041. * during an unstable branch.
  1042. */
  1043. filp->f_flags |= O_LARGEFILE;
  1044. if (filp->f_flags & O_NDELAY)
  1045. filp->f_mode |= FMODE_NDELAY;
  1046. if (filp->f_flags & O_EXCL)
  1047. filp->f_mode |= FMODE_EXCL;
  1048. if ((filp->f_flags & O_ACCMODE) == 3)
  1049. filp->f_mode |= FMODE_WRITE_IOCTL;
  1050. bdev = bd_acquire(inode);
  1051. if (bdev == NULL)
  1052. return -ENOMEM;
  1053. filp->f_mapping = bdev->bd_inode->i_mapping;
  1054. return blkdev_get(bdev, filp->f_mode, filp);
  1055. }
  1056. static int __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part)
  1057. {
  1058. int ret = 0;
  1059. struct gendisk *disk = bdev->bd_disk;
  1060. struct block_device *victim = NULL;
  1061. mutex_lock_nested(&bdev->bd_mutex, for_part);
  1062. if (for_part)
  1063. bdev->bd_part_count--;
  1064. if (!--bdev->bd_openers) {
  1065. WARN_ON_ONCE(bdev->bd_holders);
  1066. sync_blockdev(bdev);
  1067. kill_bdev(bdev);
  1068. }
  1069. if (bdev->bd_contains == bdev) {
  1070. if (disk->fops->release)
  1071. ret = disk->fops->release(disk, mode);
  1072. }
  1073. if (!bdev->bd_openers) {
  1074. struct module *owner = disk->fops->owner;
  1075. put_disk(disk);
  1076. module_put(owner);
  1077. disk_put_part(bdev->bd_part);
  1078. bdev->bd_part = NULL;
  1079. bdev->bd_disk = NULL;
  1080. bdev_inode_switch_bdi(bdev->bd_inode,
  1081. &default_backing_dev_info);
  1082. if (bdev != bdev->bd_contains)
  1083. victim = bdev->bd_contains;
  1084. bdev->bd_contains = NULL;
  1085. }
  1086. mutex_unlock(&bdev->bd_mutex);
  1087. bdput(bdev);
  1088. if (victim)
  1089. __blkdev_put(victim, mode, 1);
  1090. return ret;
  1091. }
  1092. int blkdev_put(struct block_device *bdev, fmode_t mode)
  1093. {
  1094. if (mode & FMODE_EXCL) {
  1095. bool bdev_free;
  1096. /*
  1097. * Release a claim on the device. The holder fields
  1098. * are protected with bdev_lock. bd_mutex is to
  1099. * synchronize disk_holder unlinking.
  1100. */
  1101. mutex_lock(&bdev->bd_mutex);
  1102. spin_lock(&bdev_lock);
  1103. WARN_ON_ONCE(--bdev->bd_holders < 0);
  1104. WARN_ON_ONCE(--bdev->bd_contains->bd_holders < 0);
  1105. /* bd_contains might point to self, check in a separate step */
  1106. if ((bdev_free = !bdev->bd_holders))
  1107. bdev->bd_holder = NULL;
  1108. if (!bdev->bd_contains->bd_holders)
  1109. bdev->bd_contains->bd_holder = NULL;
  1110. spin_unlock(&bdev_lock);
  1111. /* if this was the last claim, holder link should go too */
  1112. if (bdev_free)
  1113. bd_unlink_disk_holder(bdev);
  1114. mutex_unlock(&bdev->bd_mutex);
  1115. }
  1116. return __blkdev_put(bdev, mode, 0);
  1117. }
  1118. EXPORT_SYMBOL(blkdev_put);
  1119. static int blkdev_close(struct inode * inode, struct file * filp)
  1120. {
  1121. struct block_device *bdev = I_BDEV(filp->f_mapping->host);
  1122. return blkdev_put(bdev, filp->f_mode);
  1123. }
  1124. static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg)
  1125. {
  1126. struct block_device *bdev = I_BDEV(file->f_mapping->host);
  1127. fmode_t mode = file->f_mode;
  1128. /*
  1129. * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
  1130. * to updated it before every ioctl.
  1131. */
  1132. if (file->f_flags & O_NDELAY)
  1133. mode |= FMODE_NDELAY;
  1134. else
  1135. mode &= ~FMODE_NDELAY;
  1136. return blkdev_ioctl(bdev, mode, cmd, arg);
  1137. }
  1138. /*
  1139. * Write data to the block device. Only intended for the block device itself
  1140. * and the raw driver which basically is a fake block device.
  1141. *
  1142. * Does not take i_mutex for the write and thus is not for general purpose
  1143. * use.
  1144. */
  1145. ssize_t blkdev_aio_write(struct kiocb *iocb, const struct iovec *iov,
  1146. unsigned long nr_segs, loff_t pos)
  1147. {
  1148. struct file *file = iocb->ki_filp;
  1149. ssize_t ret;
  1150. BUG_ON(iocb->ki_pos != pos);
  1151. ret = __generic_file_aio_write(iocb, iov, nr_segs, &iocb->ki_pos);
  1152. if (ret > 0 || ret == -EIOCBQUEUED) {
  1153. ssize_t err;
  1154. err = generic_write_sync(file, pos, ret);
  1155. if (err < 0 && ret > 0)
  1156. ret = err;
  1157. }
  1158. return ret;
  1159. }
  1160. EXPORT_SYMBOL_GPL(blkdev_aio_write);
  1161. /*
  1162. * Try to release a page associated with block device when the system
  1163. * is under memory pressure.
  1164. */
  1165. static int blkdev_releasepage(struct page *page, gfp_t wait)
  1166. {
  1167. struct super_block *super = BDEV_I(page->mapping->host)->bdev.bd_super;
  1168. if (super && super->s_op->bdev_try_to_free_page)
  1169. return super->s_op->bdev_try_to_free_page(super, page, wait);
  1170. return try_to_free_buffers(page);
  1171. }
  1172. static const struct address_space_operations def_blk_aops = {
  1173. .readpage = blkdev_readpage,
  1174. .writepage = blkdev_writepage,
  1175. .sync_page = block_sync_page,
  1176. .write_begin = blkdev_write_begin,
  1177. .write_end = blkdev_write_end,
  1178. .writepages = generic_writepages,
  1179. .releasepage = blkdev_releasepage,
  1180. .direct_IO = blkdev_direct_IO,
  1181. };
  1182. const struct file_operations def_blk_fops = {
  1183. .open = blkdev_open,
  1184. .release = blkdev_close,
  1185. .llseek = block_llseek,
  1186. .read = do_sync_read,
  1187. .write = do_sync_write,
  1188. .aio_read = generic_file_aio_read,
  1189. .aio_write = blkdev_aio_write,
  1190. .mmap = generic_file_mmap,
  1191. .fsync = blkdev_fsync,
  1192. .unlocked_ioctl = block_ioctl,
  1193. #ifdef CONFIG_COMPAT
  1194. .compat_ioctl = compat_blkdev_ioctl,
  1195. #endif
  1196. .splice_read = generic_file_splice_read,
  1197. .splice_write = generic_file_splice_write,
  1198. };
  1199. int ioctl_by_bdev(struct block_device *bdev, unsigned cmd, unsigned long arg)
  1200. {
  1201. int res;
  1202. mm_segment_t old_fs = get_fs();
  1203. set_fs(KERNEL_DS);
  1204. res = blkdev_ioctl(bdev, 0, cmd, arg);
  1205. set_fs(old_fs);
  1206. return res;
  1207. }
  1208. EXPORT_SYMBOL(ioctl_by_bdev);
  1209. /**
  1210. * lookup_bdev - lookup a struct block_device by name
  1211. * @pathname: special file representing the block device
  1212. *
  1213. * Get a reference to the blockdevice at @pathname in the current
  1214. * namespace if possible and return it. Return ERR_PTR(error)
  1215. * otherwise.
  1216. */
  1217. struct block_device *lookup_bdev(const char *pathname)
  1218. {
  1219. struct block_device *bdev;
  1220. struct inode *inode;
  1221. struct path path;
  1222. int error;
  1223. if (!pathname || !*pathname)
  1224. return ERR_PTR(-EINVAL);
  1225. error = kern_path(pathname, LOOKUP_FOLLOW, &path);
  1226. if (error)
  1227. return ERR_PTR(error);
  1228. inode = path.dentry->d_inode;
  1229. error = -ENOTBLK;
  1230. if (!S_ISBLK(inode->i_mode))
  1231. goto fail;
  1232. error = -EACCES;
  1233. if (path.mnt->mnt_flags & MNT_NODEV)
  1234. goto fail;
  1235. error = -ENOMEM;
  1236. bdev = bd_acquire(inode);
  1237. if (!bdev)
  1238. goto fail;
  1239. out:
  1240. path_put(&path);
  1241. return bdev;
  1242. fail:
  1243. bdev = ERR_PTR(error);
  1244. goto out;
  1245. }
  1246. EXPORT_SYMBOL(lookup_bdev);
  1247. /**
  1248. * open_bdev_exclusive - open a block device by name and set it up for use
  1249. *
  1250. * @path: special file representing the block device
  1251. * @mode: FMODE_... combination to pass be used
  1252. * @holder: owner for exclusion
  1253. *
  1254. * Open the blockdevice described by the special file at @path, claim it
  1255. * for the @holder.
  1256. */
  1257. struct block_device *open_bdev_exclusive(const char *path, fmode_t mode, void *holder)
  1258. {
  1259. struct block_device *bdev;
  1260. int error;
  1261. bdev = lookup_bdev(path);
  1262. if (IS_ERR(bdev))
  1263. return bdev;
  1264. error = blkdev_get(bdev, mode | FMODE_EXCL, holder);
  1265. if (error)
  1266. return ERR_PTR(error);
  1267. if ((mode & FMODE_WRITE) && bdev_read_only(bdev)) {
  1268. blkdev_put(bdev, mode);
  1269. return ERR_PTR(-EACCES);
  1270. }
  1271. return bdev;
  1272. }
  1273. EXPORT_SYMBOL(open_bdev_exclusive);
  1274. int __invalidate_device(struct block_device *bdev)
  1275. {
  1276. struct super_block *sb = get_super(bdev);
  1277. int res = 0;
  1278. if (sb) {
  1279. /*
  1280. * no need to lock the super, get_super holds the
  1281. * read mutex so the filesystem cannot go away
  1282. * under us (->put_super runs with the write lock
  1283. * hold).
  1284. */
  1285. shrink_dcache_sb(sb);
  1286. res = invalidate_inodes(sb);
  1287. drop_super(sb);
  1288. }
  1289. invalidate_bdev(bdev);
  1290. return res;
  1291. }
  1292. EXPORT_SYMBOL(__invalidate_device);