block_dev.c 38 KB

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