block_dev.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629
  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. #ifdef CONFIG_SYSFS
  378. INIT_LIST_HEAD(&bdev->bd_holder_disks);
  379. #endif
  380. inode_init_once(&ei->vfs_inode);
  381. /* Initialize mutex for freeze. */
  382. mutex_init(&bdev->bd_fsfreeze_mutex);
  383. }
  384. static inline void __bd_forget(struct inode *inode)
  385. {
  386. list_del_init(&inode->i_devices);
  387. inode->i_bdev = NULL;
  388. inode->i_mapping = &inode->i_data;
  389. }
  390. static void bdev_evict_inode(struct inode *inode)
  391. {
  392. struct block_device *bdev = &BDEV_I(inode)->bdev;
  393. struct list_head *p;
  394. truncate_inode_pages(&inode->i_data, 0);
  395. invalidate_inode_buffers(inode); /* is it needed here? */
  396. end_writeback(inode);
  397. spin_lock(&bdev_lock);
  398. while ( (p = bdev->bd_inodes.next) != &bdev->bd_inodes ) {
  399. __bd_forget(list_entry(p, struct inode, i_devices));
  400. }
  401. list_del_init(&bdev->bd_list);
  402. spin_unlock(&bdev_lock);
  403. }
  404. static const struct super_operations bdev_sops = {
  405. .statfs = simple_statfs,
  406. .alloc_inode = bdev_alloc_inode,
  407. .destroy_inode = bdev_destroy_inode,
  408. .drop_inode = generic_delete_inode,
  409. .evict_inode = bdev_evict_inode,
  410. };
  411. static struct dentry *bd_mount(struct file_system_type *fs_type,
  412. int flags, const char *dev_name, void *data)
  413. {
  414. return mount_pseudo(fs_type, "bdev:", &bdev_sops, NULL, 0x62646576);
  415. }
  416. static struct file_system_type bd_type = {
  417. .name = "bdev",
  418. .mount = bd_mount,
  419. .kill_sb = kill_anon_super,
  420. };
  421. struct super_block *blockdev_superblock __read_mostly;
  422. void __init bdev_cache_init(void)
  423. {
  424. int err;
  425. struct vfsmount *bd_mnt;
  426. bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
  427. 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
  428. SLAB_MEM_SPREAD|SLAB_PANIC),
  429. init_once);
  430. err = register_filesystem(&bd_type);
  431. if (err)
  432. panic("Cannot register bdev pseudo-fs");
  433. bd_mnt = kern_mount(&bd_type);
  434. if (IS_ERR(bd_mnt))
  435. panic("Cannot create bdev pseudo-fs");
  436. /*
  437. * This vfsmount structure is only used to obtain the
  438. * blockdev_superblock, so tell kmemleak not to report it.
  439. */
  440. kmemleak_not_leak(bd_mnt);
  441. blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */
  442. }
  443. /*
  444. * Most likely _very_ bad one - but then it's hardly critical for small
  445. * /dev and can be fixed when somebody will need really large one.
  446. * Keep in mind that it will be fed through icache hash function too.
  447. */
  448. static inline unsigned long hash(dev_t dev)
  449. {
  450. return MAJOR(dev)+MINOR(dev);
  451. }
  452. static int bdev_test(struct inode *inode, void *data)
  453. {
  454. return BDEV_I(inode)->bdev.bd_dev == *(dev_t *)data;
  455. }
  456. static int bdev_set(struct inode *inode, void *data)
  457. {
  458. BDEV_I(inode)->bdev.bd_dev = *(dev_t *)data;
  459. return 0;
  460. }
  461. static LIST_HEAD(all_bdevs);
  462. struct block_device *bdget(dev_t dev)
  463. {
  464. struct block_device *bdev;
  465. struct inode *inode;
  466. inode = iget5_locked(blockdev_superblock, hash(dev),
  467. bdev_test, bdev_set, &dev);
  468. if (!inode)
  469. return NULL;
  470. bdev = &BDEV_I(inode)->bdev;
  471. if (inode->i_state & I_NEW) {
  472. bdev->bd_contains = NULL;
  473. bdev->bd_inode = inode;
  474. bdev->bd_block_size = (1 << inode->i_blkbits);
  475. bdev->bd_part_count = 0;
  476. bdev->bd_invalidated = 0;
  477. inode->i_mode = S_IFBLK;
  478. inode->i_rdev = dev;
  479. inode->i_bdev = bdev;
  480. inode->i_data.a_ops = &def_blk_aops;
  481. mapping_set_gfp_mask(&inode->i_data, GFP_USER);
  482. inode->i_data.backing_dev_info = &default_backing_dev_info;
  483. spin_lock(&bdev_lock);
  484. list_add(&bdev->bd_list, &all_bdevs);
  485. spin_unlock(&bdev_lock);
  486. unlock_new_inode(inode);
  487. }
  488. return bdev;
  489. }
  490. EXPORT_SYMBOL(bdget);
  491. /**
  492. * bdgrab -- Grab a reference to an already referenced block device
  493. * @bdev: Block device to grab a reference to.
  494. */
  495. struct block_device *bdgrab(struct block_device *bdev)
  496. {
  497. ihold(bdev->bd_inode);
  498. return bdev;
  499. }
  500. long nr_blockdev_pages(void)
  501. {
  502. struct block_device *bdev;
  503. long ret = 0;
  504. spin_lock(&bdev_lock);
  505. list_for_each_entry(bdev, &all_bdevs, bd_list) {
  506. ret += bdev->bd_inode->i_mapping->nrpages;
  507. }
  508. spin_unlock(&bdev_lock);
  509. return ret;
  510. }
  511. void bdput(struct block_device *bdev)
  512. {
  513. iput(bdev->bd_inode);
  514. }
  515. EXPORT_SYMBOL(bdput);
  516. static struct block_device *bd_acquire(struct inode *inode)
  517. {
  518. struct block_device *bdev;
  519. spin_lock(&bdev_lock);
  520. bdev = inode->i_bdev;
  521. if (bdev) {
  522. ihold(bdev->bd_inode);
  523. spin_unlock(&bdev_lock);
  524. return bdev;
  525. }
  526. spin_unlock(&bdev_lock);
  527. bdev = bdget(inode->i_rdev);
  528. if (bdev) {
  529. spin_lock(&bdev_lock);
  530. if (!inode->i_bdev) {
  531. /*
  532. * We take an additional reference to bd_inode,
  533. * and it's released in clear_inode() of inode.
  534. * So, we can access it via ->i_mapping always
  535. * without igrab().
  536. */
  537. ihold(bdev->bd_inode);
  538. inode->i_bdev = bdev;
  539. inode->i_mapping = bdev->bd_inode->i_mapping;
  540. list_add(&inode->i_devices, &bdev->bd_inodes);
  541. }
  542. spin_unlock(&bdev_lock);
  543. }
  544. return bdev;
  545. }
  546. /* Call when you free inode */
  547. void bd_forget(struct inode *inode)
  548. {
  549. struct block_device *bdev = NULL;
  550. spin_lock(&bdev_lock);
  551. if (inode->i_bdev) {
  552. if (!sb_is_blkdev_sb(inode->i_sb))
  553. bdev = inode->i_bdev;
  554. __bd_forget(inode);
  555. }
  556. spin_unlock(&bdev_lock);
  557. if (bdev)
  558. iput(bdev->bd_inode);
  559. }
  560. /**
  561. * bd_may_claim - test whether a block device can be claimed
  562. * @bdev: block device of interest
  563. * @whole: whole block device containing @bdev, may equal @bdev
  564. * @holder: holder trying to claim @bdev
  565. *
  566. * Test whther @bdev can be claimed by @holder.
  567. *
  568. * CONTEXT:
  569. * spin_lock(&bdev_lock).
  570. *
  571. * RETURNS:
  572. * %true if @bdev can be claimed, %false otherwise.
  573. */
  574. static bool bd_may_claim(struct block_device *bdev, struct block_device *whole,
  575. void *holder)
  576. {
  577. if (bdev->bd_holder == holder)
  578. return true; /* already a holder */
  579. else if (bdev->bd_holder != NULL)
  580. return false; /* held by someone else */
  581. else if (bdev->bd_contains == bdev)
  582. return true; /* is a whole device which isn't held */
  583. else if (whole->bd_holder == bd_may_claim)
  584. return true; /* is a partition of a device that is being partitioned */
  585. else if (whole->bd_holder != NULL)
  586. return false; /* is a partition of a held device */
  587. else
  588. return true; /* is a partition of an un-held device */
  589. }
  590. /**
  591. * bd_prepare_to_claim - prepare to claim a block device
  592. * @bdev: block device of interest
  593. * @whole: the whole device containing @bdev, may equal @bdev
  594. * @holder: holder trying to claim @bdev
  595. *
  596. * Prepare to claim @bdev. This function fails if @bdev is already
  597. * claimed by another holder and waits if another claiming is in
  598. * progress. This function doesn't actually claim. On successful
  599. * return, the caller has ownership of bd_claiming and bd_holder[s].
  600. *
  601. * CONTEXT:
  602. * spin_lock(&bdev_lock). Might release bdev_lock, sleep and regrab
  603. * it multiple times.
  604. *
  605. * RETURNS:
  606. * 0 if @bdev can be claimed, -EBUSY otherwise.
  607. */
  608. static int bd_prepare_to_claim(struct block_device *bdev,
  609. struct block_device *whole, void *holder)
  610. {
  611. retry:
  612. /* if someone else claimed, fail */
  613. if (!bd_may_claim(bdev, whole, holder))
  614. return -EBUSY;
  615. /* if claiming is already in progress, wait for it to finish */
  616. if (whole->bd_claiming) {
  617. wait_queue_head_t *wq = bit_waitqueue(&whole->bd_claiming, 0);
  618. DEFINE_WAIT(wait);
  619. prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
  620. spin_unlock(&bdev_lock);
  621. schedule();
  622. finish_wait(wq, &wait);
  623. spin_lock(&bdev_lock);
  624. goto retry;
  625. }
  626. /* yay, all mine */
  627. return 0;
  628. }
  629. /**
  630. * bd_start_claiming - start claiming a block device
  631. * @bdev: block device of interest
  632. * @holder: holder trying to claim @bdev
  633. *
  634. * @bdev is about to be opened exclusively. Check @bdev can be opened
  635. * exclusively and mark that an exclusive open is in progress. Each
  636. * successful call to this function must be matched with a call to
  637. * either bd_finish_claiming() or bd_abort_claiming() (which do not
  638. * fail).
  639. *
  640. * This function is used to gain exclusive access to the block device
  641. * without actually causing other exclusive open attempts to fail. It
  642. * should be used when the open sequence itself requires exclusive
  643. * access but may subsequently fail.
  644. *
  645. * CONTEXT:
  646. * Might sleep.
  647. *
  648. * RETURNS:
  649. * Pointer to the block device containing @bdev on success, ERR_PTR()
  650. * value on failure.
  651. */
  652. static struct block_device *bd_start_claiming(struct block_device *bdev,
  653. void *holder)
  654. {
  655. struct gendisk *disk;
  656. struct block_device *whole;
  657. int partno, err;
  658. might_sleep();
  659. /*
  660. * @bdev might not have been initialized properly yet, look up
  661. * and grab the outer block device the hard way.
  662. */
  663. disk = get_gendisk(bdev->bd_dev, &partno);
  664. if (!disk)
  665. return ERR_PTR(-ENXIO);
  666. whole = bdget_disk(disk, 0);
  667. module_put(disk->fops->owner);
  668. put_disk(disk);
  669. if (!whole)
  670. return ERR_PTR(-ENOMEM);
  671. /* prepare to claim, if successful, mark claiming in progress */
  672. spin_lock(&bdev_lock);
  673. err = bd_prepare_to_claim(bdev, whole, holder);
  674. if (err == 0) {
  675. whole->bd_claiming = holder;
  676. spin_unlock(&bdev_lock);
  677. return whole;
  678. } else {
  679. spin_unlock(&bdev_lock);
  680. bdput(whole);
  681. return ERR_PTR(err);
  682. }
  683. }
  684. #ifdef CONFIG_SYSFS
  685. struct bd_holder_disk {
  686. struct list_head list;
  687. struct gendisk *disk;
  688. int refcnt;
  689. };
  690. static struct bd_holder_disk *bd_find_holder_disk(struct block_device *bdev,
  691. struct gendisk *disk)
  692. {
  693. struct bd_holder_disk *holder;
  694. list_for_each_entry(holder, &bdev->bd_holder_disks, list)
  695. if (holder->disk == disk)
  696. return holder;
  697. return NULL;
  698. }
  699. static int add_symlink(struct kobject *from, struct kobject *to)
  700. {
  701. return sysfs_create_link(from, to, kobject_name(to));
  702. }
  703. static void del_symlink(struct kobject *from, struct kobject *to)
  704. {
  705. sysfs_remove_link(from, kobject_name(to));
  706. }
  707. /**
  708. * bd_link_disk_holder - create symlinks between holding disk and slave bdev
  709. * @bdev: the claimed slave bdev
  710. * @disk: the holding disk
  711. *
  712. * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
  713. *
  714. * This functions creates the following sysfs symlinks.
  715. *
  716. * - from "slaves" directory of the holder @disk to the claimed @bdev
  717. * - from "holders" directory of the @bdev to the holder @disk
  718. *
  719. * For example, if /dev/dm-0 maps to /dev/sda and disk for dm-0 is
  720. * passed to bd_link_disk_holder(), then:
  721. *
  722. * /sys/block/dm-0/slaves/sda --> /sys/block/sda
  723. * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
  724. *
  725. * The caller must have claimed @bdev before calling this function and
  726. * ensure that both @bdev and @disk are valid during the creation and
  727. * lifetime of these symlinks.
  728. *
  729. * CONTEXT:
  730. * Might sleep.
  731. *
  732. * RETURNS:
  733. * 0 on success, -errno on failure.
  734. */
  735. int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk)
  736. {
  737. struct bd_holder_disk *holder;
  738. int ret = 0;
  739. mutex_lock(&bdev->bd_mutex);
  740. WARN_ON_ONCE(!bdev->bd_holder);
  741. /* FIXME: remove the following once add_disk() handles errors */
  742. if (WARN_ON(!disk->slave_dir || !bdev->bd_part->holder_dir))
  743. goto out_unlock;
  744. holder = bd_find_holder_disk(bdev, disk);
  745. if (holder) {
  746. holder->refcnt++;
  747. goto out_unlock;
  748. }
  749. holder = kzalloc(sizeof(*holder), GFP_KERNEL);
  750. if (!holder) {
  751. ret = -ENOMEM;
  752. goto out_unlock;
  753. }
  754. INIT_LIST_HEAD(&holder->list);
  755. holder->disk = disk;
  756. holder->refcnt = 1;
  757. ret = add_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
  758. if (ret)
  759. goto out_free;
  760. ret = add_symlink(bdev->bd_part->holder_dir, &disk_to_dev(disk)->kobj);
  761. if (ret)
  762. goto out_del;
  763. /*
  764. * bdev could be deleted beneath us which would implicitly destroy
  765. * the holder directory. Hold on to it.
  766. */
  767. kobject_get(bdev->bd_part->holder_dir);
  768. list_add(&holder->list, &bdev->bd_holder_disks);
  769. goto out_unlock;
  770. out_del:
  771. del_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
  772. out_free:
  773. kfree(holder);
  774. out_unlock:
  775. mutex_unlock(&bdev->bd_mutex);
  776. return ret;
  777. }
  778. EXPORT_SYMBOL_GPL(bd_link_disk_holder);
  779. /**
  780. * bd_unlink_disk_holder - destroy symlinks created by bd_link_disk_holder()
  781. * @bdev: the calimed slave bdev
  782. * @disk: the holding disk
  783. *
  784. * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
  785. *
  786. * CONTEXT:
  787. * Might sleep.
  788. */
  789. void bd_unlink_disk_holder(struct block_device *bdev, struct gendisk *disk)
  790. {
  791. struct bd_holder_disk *holder;
  792. mutex_lock(&bdev->bd_mutex);
  793. holder = bd_find_holder_disk(bdev, disk);
  794. if (!WARN_ON_ONCE(holder == NULL) && !--holder->refcnt) {
  795. del_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
  796. del_symlink(bdev->bd_part->holder_dir,
  797. &disk_to_dev(disk)->kobj);
  798. kobject_put(bdev->bd_part->holder_dir);
  799. list_del_init(&holder->list);
  800. kfree(holder);
  801. }
  802. mutex_unlock(&bdev->bd_mutex);
  803. }
  804. EXPORT_SYMBOL_GPL(bd_unlink_disk_holder);
  805. #endif
  806. /**
  807. * flush_disk - invalidates all buffer-cache entries on a disk
  808. *
  809. * @bdev: struct block device to be flushed
  810. * @kill_dirty: flag to guide handling of dirty inodes
  811. *
  812. * Invalidates all buffer-cache entries on a disk. It should be called
  813. * when a disk has been changed -- either by a media change or online
  814. * resize.
  815. */
  816. static void flush_disk(struct block_device *bdev, bool kill_dirty)
  817. {
  818. if (__invalidate_device(bdev, kill_dirty)) {
  819. char name[BDEVNAME_SIZE] = "";
  820. if (bdev->bd_disk)
  821. disk_name(bdev->bd_disk, 0, name);
  822. printk(KERN_WARNING "VFS: busy inodes on changed media or "
  823. "resized disk %s\n", name);
  824. }
  825. if (!bdev->bd_disk)
  826. return;
  827. if (disk_partitionable(bdev->bd_disk))
  828. bdev->bd_invalidated = 1;
  829. }
  830. /**
  831. * check_disk_size_change - checks for disk size change and adjusts bdev size.
  832. * @disk: struct gendisk to check
  833. * @bdev: struct bdev to adjust.
  834. *
  835. * This routine checks to see if the bdev size does not match the disk size
  836. * and adjusts it if it differs.
  837. */
  838. void check_disk_size_change(struct gendisk *disk, struct block_device *bdev)
  839. {
  840. loff_t disk_size, bdev_size;
  841. disk_size = (loff_t)get_capacity(disk) << 9;
  842. bdev_size = i_size_read(bdev->bd_inode);
  843. if (disk_size != bdev_size) {
  844. char name[BDEVNAME_SIZE];
  845. disk_name(disk, 0, name);
  846. printk(KERN_INFO
  847. "%s: detected capacity change from %lld to %lld\n",
  848. name, bdev_size, disk_size);
  849. i_size_write(bdev->bd_inode, disk_size);
  850. flush_disk(bdev, false);
  851. }
  852. }
  853. EXPORT_SYMBOL(check_disk_size_change);
  854. /**
  855. * revalidate_disk - wrapper for lower-level driver's revalidate_disk call-back
  856. * @disk: struct gendisk to be revalidated
  857. *
  858. * This routine is a wrapper for lower-level driver's revalidate_disk
  859. * call-backs. It is used to do common pre and post operations needed
  860. * for all revalidate_disk operations.
  861. */
  862. int revalidate_disk(struct gendisk *disk)
  863. {
  864. struct block_device *bdev;
  865. int ret = 0;
  866. if (disk->fops->revalidate_disk)
  867. ret = disk->fops->revalidate_disk(disk);
  868. bdev = bdget_disk(disk, 0);
  869. if (!bdev)
  870. return ret;
  871. mutex_lock(&bdev->bd_mutex);
  872. check_disk_size_change(disk, bdev);
  873. mutex_unlock(&bdev->bd_mutex);
  874. bdput(bdev);
  875. return ret;
  876. }
  877. EXPORT_SYMBOL(revalidate_disk);
  878. /*
  879. * This routine checks whether a removable media has been changed,
  880. * and invalidates all buffer-cache-entries in that case. This
  881. * is a relatively slow routine, so we have to try to minimize using
  882. * it. Thus it is called only upon a 'mount' or 'open'. This
  883. * is the best way of combining speed and utility, I think.
  884. * People changing diskettes in the middle of an operation deserve
  885. * to lose :-)
  886. */
  887. int check_disk_change(struct block_device *bdev)
  888. {
  889. struct gendisk *disk = bdev->bd_disk;
  890. const struct block_device_operations *bdops = disk->fops;
  891. unsigned int events;
  892. events = disk_clear_events(disk, DISK_EVENT_MEDIA_CHANGE |
  893. DISK_EVENT_EJECT_REQUEST);
  894. if (!(events & DISK_EVENT_MEDIA_CHANGE))
  895. return 0;
  896. flush_disk(bdev, true);
  897. if (bdops->revalidate_disk)
  898. bdops->revalidate_disk(bdev->bd_disk);
  899. return 1;
  900. }
  901. EXPORT_SYMBOL(check_disk_change);
  902. void bd_set_size(struct block_device *bdev, loff_t size)
  903. {
  904. unsigned bsize = bdev_logical_block_size(bdev);
  905. bdev->bd_inode->i_size = size;
  906. while (bsize < PAGE_CACHE_SIZE) {
  907. if (size & bsize)
  908. break;
  909. bsize <<= 1;
  910. }
  911. bdev->bd_block_size = bsize;
  912. bdev->bd_inode->i_blkbits = blksize_bits(bsize);
  913. }
  914. EXPORT_SYMBOL(bd_set_size);
  915. static int __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part);
  916. /*
  917. * bd_mutex locking:
  918. *
  919. * mutex_lock(part->bd_mutex)
  920. * mutex_lock_nested(whole->bd_mutex, 1)
  921. */
  922. static int __blkdev_get(struct block_device *bdev, fmode_t mode, int for_part)
  923. {
  924. struct gendisk *disk;
  925. int ret;
  926. int partno;
  927. int perm = 0;
  928. if (mode & FMODE_READ)
  929. perm |= MAY_READ;
  930. if (mode & FMODE_WRITE)
  931. perm |= MAY_WRITE;
  932. /*
  933. * hooks: /n/, see "layering violations".
  934. */
  935. if (!for_part) {
  936. ret = devcgroup_inode_permission(bdev->bd_inode, perm);
  937. if (ret != 0) {
  938. bdput(bdev);
  939. return ret;
  940. }
  941. }
  942. restart:
  943. ret = -ENXIO;
  944. disk = get_gendisk(bdev->bd_dev, &partno);
  945. if (!disk)
  946. goto out;
  947. mutex_lock_nested(&bdev->bd_mutex, for_part);
  948. if (!bdev->bd_openers) {
  949. bdev->bd_disk = disk;
  950. bdev->bd_contains = bdev;
  951. if (!partno) {
  952. struct backing_dev_info *bdi;
  953. ret = -ENXIO;
  954. bdev->bd_part = disk_get_part(disk, partno);
  955. if (!bdev->bd_part)
  956. goto out_clear;
  957. if (disk->fops->open) {
  958. ret = disk->fops->open(bdev, mode);
  959. if (ret == -ERESTARTSYS) {
  960. /* Lost a race with 'disk' being
  961. * deleted, try again.
  962. * See md.c
  963. */
  964. disk_put_part(bdev->bd_part);
  965. bdev->bd_part = NULL;
  966. module_put(disk->fops->owner);
  967. put_disk(disk);
  968. bdev->bd_disk = NULL;
  969. mutex_unlock(&bdev->bd_mutex);
  970. goto restart;
  971. }
  972. if (ret)
  973. goto out_clear;
  974. }
  975. if (!bdev->bd_openers) {
  976. bd_set_size(bdev,(loff_t)get_capacity(disk)<<9);
  977. bdi = blk_get_backing_dev_info(bdev);
  978. if (bdi == NULL)
  979. bdi = &default_backing_dev_info;
  980. bdev_inode_switch_bdi(bdev->bd_inode, bdi);
  981. }
  982. if (bdev->bd_invalidated)
  983. rescan_partitions(disk, bdev);
  984. } else {
  985. struct block_device *whole;
  986. whole = bdget_disk(disk, 0);
  987. ret = -ENOMEM;
  988. if (!whole)
  989. goto out_clear;
  990. BUG_ON(for_part);
  991. ret = __blkdev_get(whole, mode, 1);
  992. if (ret)
  993. goto out_clear;
  994. bdev->bd_contains = whole;
  995. bdev_inode_switch_bdi(bdev->bd_inode,
  996. whole->bd_inode->i_data.backing_dev_info);
  997. bdev->bd_part = disk_get_part(disk, partno);
  998. if (!(disk->flags & GENHD_FL_UP) ||
  999. !bdev->bd_part || !bdev->bd_part->nr_sects) {
  1000. ret = -ENXIO;
  1001. goto out_clear;
  1002. }
  1003. bd_set_size(bdev, (loff_t)bdev->bd_part->nr_sects << 9);
  1004. }
  1005. } else {
  1006. module_put(disk->fops->owner);
  1007. put_disk(disk);
  1008. disk = NULL;
  1009. if (bdev->bd_contains == bdev) {
  1010. if (bdev->bd_disk->fops->open) {
  1011. ret = bdev->bd_disk->fops->open(bdev, mode);
  1012. if (ret)
  1013. goto out_unlock_bdev;
  1014. }
  1015. if (bdev->bd_invalidated)
  1016. rescan_partitions(bdev->bd_disk, bdev);
  1017. }
  1018. }
  1019. bdev->bd_openers++;
  1020. if (for_part)
  1021. bdev->bd_part_count++;
  1022. mutex_unlock(&bdev->bd_mutex);
  1023. return 0;
  1024. out_clear:
  1025. disk_put_part(bdev->bd_part);
  1026. bdev->bd_disk = NULL;
  1027. bdev->bd_part = NULL;
  1028. bdev_inode_switch_bdi(bdev->bd_inode, &default_backing_dev_info);
  1029. if (bdev != bdev->bd_contains)
  1030. __blkdev_put(bdev->bd_contains, mode, 1);
  1031. bdev->bd_contains = NULL;
  1032. out_unlock_bdev:
  1033. mutex_unlock(&bdev->bd_mutex);
  1034. out:
  1035. if (disk)
  1036. module_put(disk->fops->owner);
  1037. put_disk(disk);
  1038. bdput(bdev);
  1039. return ret;
  1040. }
  1041. /**
  1042. * blkdev_get - open a block device
  1043. * @bdev: block_device to open
  1044. * @mode: FMODE_* mask
  1045. * @holder: exclusive holder identifier
  1046. *
  1047. * Open @bdev with @mode. If @mode includes %FMODE_EXCL, @bdev is
  1048. * open with exclusive access. Specifying %FMODE_EXCL with %NULL
  1049. * @holder is invalid. Exclusive opens may nest for the same @holder.
  1050. *
  1051. * On success, the reference count of @bdev is unchanged. On failure,
  1052. * @bdev is put.
  1053. *
  1054. * CONTEXT:
  1055. * Might sleep.
  1056. *
  1057. * RETURNS:
  1058. * 0 on success, -errno on failure.
  1059. */
  1060. int blkdev_get(struct block_device *bdev, fmode_t mode, void *holder)
  1061. {
  1062. struct block_device *whole = NULL;
  1063. int res;
  1064. WARN_ON_ONCE((mode & FMODE_EXCL) && !holder);
  1065. if ((mode & FMODE_EXCL) && holder) {
  1066. whole = bd_start_claiming(bdev, holder);
  1067. if (IS_ERR(whole)) {
  1068. bdput(bdev);
  1069. return PTR_ERR(whole);
  1070. }
  1071. }
  1072. res = __blkdev_get(bdev, mode, 0);
  1073. if (whole) {
  1074. /* finish claiming */
  1075. mutex_lock(&bdev->bd_mutex);
  1076. spin_lock(&bdev_lock);
  1077. if (!res) {
  1078. BUG_ON(!bd_may_claim(bdev, whole, holder));
  1079. /*
  1080. * Note that for a whole device bd_holders
  1081. * will be incremented twice, and bd_holder
  1082. * will be set to bd_may_claim before being
  1083. * set to holder
  1084. */
  1085. whole->bd_holders++;
  1086. whole->bd_holder = bd_may_claim;
  1087. bdev->bd_holders++;
  1088. bdev->bd_holder = holder;
  1089. }
  1090. /* tell others that we're done */
  1091. BUG_ON(whole->bd_claiming != holder);
  1092. whole->bd_claiming = NULL;
  1093. wake_up_bit(&whole->bd_claiming, 0);
  1094. spin_unlock(&bdev_lock);
  1095. /*
  1096. * Block event polling for write claims. Any write
  1097. * holder makes the write_holder state stick until all
  1098. * are released. This is good enough and tracking
  1099. * individual writeable reference is too fragile given
  1100. * the way @mode is used in blkdev_get/put().
  1101. */
  1102. if (!res && (mode & FMODE_WRITE) && !bdev->bd_write_holder) {
  1103. bdev->bd_write_holder = true;
  1104. disk_block_events(bdev->bd_disk);
  1105. }
  1106. mutex_unlock(&bdev->bd_mutex);
  1107. bdput(whole);
  1108. }
  1109. return res;
  1110. }
  1111. EXPORT_SYMBOL(blkdev_get);
  1112. /**
  1113. * blkdev_get_by_path - open a block device by name
  1114. * @path: path to the block device to open
  1115. * @mode: FMODE_* mask
  1116. * @holder: exclusive holder identifier
  1117. *
  1118. * Open the blockdevice described by the device file at @path. @mode
  1119. * and @holder are identical to blkdev_get().
  1120. *
  1121. * On success, the returned block_device has reference count of one.
  1122. *
  1123. * CONTEXT:
  1124. * Might sleep.
  1125. *
  1126. * RETURNS:
  1127. * Pointer to block_device on success, ERR_PTR(-errno) on failure.
  1128. */
  1129. struct block_device *blkdev_get_by_path(const char *path, fmode_t mode,
  1130. void *holder)
  1131. {
  1132. struct block_device *bdev;
  1133. int err;
  1134. bdev = lookup_bdev(path);
  1135. if (IS_ERR(bdev))
  1136. return bdev;
  1137. err = blkdev_get(bdev, mode, holder);
  1138. if (err)
  1139. return ERR_PTR(err);
  1140. if ((mode & FMODE_WRITE) && bdev_read_only(bdev)) {
  1141. blkdev_put(bdev, mode);
  1142. return ERR_PTR(-EACCES);
  1143. }
  1144. return bdev;
  1145. }
  1146. EXPORT_SYMBOL(blkdev_get_by_path);
  1147. /**
  1148. * blkdev_get_by_dev - open a block device by device number
  1149. * @dev: device number of block device to open
  1150. * @mode: FMODE_* mask
  1151. * @holder: exclusive holder identifier
  1152. *
  1153. * Open the blockdevice described by device number @dev. @mode and
  1154. * @holder are identical to blkdev_get().
  1155. *
  1156. * Use it ONLY if you really do not have anything better - i.e. when
  1157. * you are behind a truly sucky interface and all you are given is a
  1158. * device number. _Never_ to be used for internal purposes. If you
  1159. * ever need it - reconsider your API.
  1160. *
  1161. * On success, the returned block_device has reference count of one.
  1162. *
  1163. * CONTEXT:
  1164. * Might sleep.
  1165. *
  1166. * RETURNS:
  1167. * Pointer to block_device on success, ERR_PTR(-errno) on failure.
  1168. */
  1169. struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder)
  1170. {
  1171. struct block_device *bdev;
  1172. int err;
  1173. bdev = bdget(dev);
  1174. if (!bdev)
  1175. return ERR_PTR(-ENOMEM);
  1176. err = blkdev_get(bdev, mode, holder);
  1177. if (err)
  1178. return ERR_PTR(err);
  1179. return bdev;
  1180. }
  1181. EXPORT_SYMBOL(blkdev_get_by_dev);
  1182. static int blkdev_open(struct inode * inode, struct file * filp)
  1183. {
  1184. struct block_device *bdev;
  1185. /*
  1186. * Preserve backwards compatibility and allow large file access
  1187. * even if userspace doesn't ask for it explicitly. Some mkfs
  1188. * binary needs it. We might want to drop this workaround
  1189. * during an unstable branch.
  1190. */
  1191. filp->f_flags |= O_LARGEFILE;
  1192. if (filp->f_flags & O_NDELAY)
  1193. filp->f_mode |= FMODE_NDELAY;
  1194. if (filp->f_flags & O_EXCL)
  1195. filp->f_mode |= FMODE_EXCL;
  1196. if ((filp->f_flags & O_ACCMODE) == 3)
  1197. filp->f_mode |= FMODE_WRITE_IOCTL;
  1198. bdev = bd_acquire(inode);
  1199. if (bdev == NULL)
  1200. return -ENOMEM;
  1201. filp->f_mapping = bdev->bd_inode->i_mapping;
  1202. return blkdev_get(bdev, filp->f_mode, filp);
  1203. }
  1204. static int __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part)
  1205. {
  1206. int ret = 0;
  1207. struct gendisk *disk = bdev->bd_disk;
  1208. struct block_device *victim = NULL;
  1209. mutex_lock_nested(&bdev->bd_mutex, for_part);
  1210. if (for_part)
  1211. bdev->bd_part_count--;
  1212. if (!--bdev->bd_openers) {
  1213. WARN_ON_ONCE(bdev->bd_holders);
  1214. sync_blockdev(bdev);
  1215. kill_bdev(bdev);
  1216. }
  1217. if (bdev->bd_contains == bdev) {
  1218. if (disk->fops->release)
  1219. ret = disk->fops->release(disk, mode);
  1220. }
  1221. if (!bdev->bd_openers) {
  1222. struct module *owner = disk->fops->owner;
  1223. put_disk(disk);
  1224. module_put(owner);
  1225. disk_put_part(bdev->bd_part);
  1226. bdev->bd_part = NULL;
  1227. bdev->bd_disk = NULL;
  1228. bdev_inode_switch_bdi(bdev->bd_inode,
  1229. &default_backing_dev_info);
  1230. if (bdev != bdev->bd_contains)
  1231. victim = bdev->bd_contains;
  1232. bdev->bd_contains = NULL;
  1233. }
  1234. mutex_unlock(&bdev->bd_mutex);
  1235. bdput(bdev);
  1236. if (victim)
  1237. __blkdev_put(victim, mode, 1);
  1238. return ret;
  1239. }
  1240. int blkdev_put(struct block_device *bdev, fmode_t mode)
  1241. {
  1242. if (mode & FMODE_EXCL) {
  1243. bool bdev_free;
  1244. /*
  1245. * Release a claim on the device. The holder fields
  1246. * are protected with bdev_lock. bd_mutex is to
  1247. * synchronize disk_holder unlinking.
  1248. */
  1249. mutex_lock(&bdev->bd_mutex);
  1250. spin_lock(&bdev_lock);
  1251. WARN_ON_ONCE(--bdev->bd_holders < 0);
  1252. WARN_ON_ONCE(--bdev->bd_contains->bd_holders < 0);
  1253. /* bd_contains might point to self, check in a separate step */
  1254. if ((bdev_free = !bdev->bd_holders))
  1255. bdev->bd_holder = NULL;
  1256. if (!bdev->bd_contains->bd_holders)
  1257. bdev->bd_contains->bd_holder = NULL;
  1258. spin_unlock(&bdev_lock);
  1259. /*
  1260. * If this was the last claim, remove holder link and
  1261. * unblock evpoll if it was a write holder.
  1262. */
  1263. if (bdev_free) {
  1264. if (bdev->bd_write_holder) {
  1265. disk_unblock_events(bdev->bd_disk);
  1266. bdev->bd_write_holder = false;
  1267. } else
  1268. disk_check_events(bdev->bd_disk);
  1269. }
  1270. mutex_unlock(&bdev->bd_mutex);
  1271. } else
  1272. disk_check_events(bdev->bd_disk);
  1273. return __blkdev_put(bdev, mode, 0);
  1274. }
  1275. EXPORT_SYMBOL(blkdev_put);
  1276. static int blkdev_close(struct inode * inode, struct file * filp)
  1277. {
  1278. struct block_device *bdev = I_BDEV(filp->f_mapping->host);
  1279. return blkdev_put(bdev, filp->f_mode);
  1280. }
  1281. static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg)
  1282. {
  1283. struct block_device *bdev = I_BDEV(file->f_mapping->host);
  1284. fmode_t mode = file->f_mode;
  1285. /*
  1286. * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
  1287. * to updated it before every ioctl.
  1288. */
  1289. if (file->f_flags & O_NDELAY)
  1290. mode |= FMODE_NDELAY;
  1291. else
  1292. mode &= ~FMODE_NDELAY;
  1293. return blkdev_ioctl(bdev, mode, cmd, arg);
  1294. }
  1295. /*
  1296. * Write data to the block device. Only intended for the block device itself
  1297. * and the raw driver which basically is a fake block device.
  1298. *
  1299. * Does not take i_mutex for the write and thus is not for general purpose
  1300. * use.
  1301. */
  1302. ssize_t blkdev_aio_write(struct kiocb *iocb, const struct iovec *iov,
  1303. unsigned long nr_segs, loff_t pos)
  1304. {
  1305. struct file *file = iocb->ki_filp;
  1306. ssize_t ret;
  1307. BUG_ON(iocb->ki_pos != pos);
  1308. ret = __generic_file_aio_write(iocb, iov, nr_segs, &iocb->ki_pos);
  1309. if (ret > 0 || ret == -EIOCBQUEUED) {
  1310. ssize_t err;
  1311. err = generic_write_sync(file, pos, ret);
  1312. if (err < 0 && ret > 0)
  1313. ret = err;
  1314. }
  1315. return ret;
  1316. }
  1317. EXPORT_SYMBOL_GPL(blkdev_aio_write);
  1318. /*
  1319. * Try to release a page associated with block device when the system
  1320. * is under memory pressure.
  1321. */
  1322. static int blkdev_releasepage(struct page *page, gfp_t wait)
  1323. {
  1324. struct super_block *super = BDEV_I(page->mapping->host)->bdev.bd_super;
  1325. if (super && super->s_op->bdev_try_to_free_page)
  1326. return super->s_op->bdev_try_to_free_page(super, page, wait);
  1327. return try_to_free_buffers(page);
  1328. }
  1329. static const struct address_space_operations def_blk_aops = {
  1330. .readpage = blkdev_readpage,
  1331. .writepage = blkdev_writepage,
  1332. .sync_page = block_sync_page,
  1333. .write_begin = blkdev_write_begin,
  1334. .write_end = blkdev_write_end,
  1335. .writepages = generic_writepages,
  1336. .releasepage = blkdev_releasepage,
  1337. .direct_IO = blkdev_direct_IO,
  1338. };
  1339. const struct file_operations def_blk_fops = {
  1340. .open = blkdev_open,
  1341. .release = blkdev_close,
  1342. .llseek = block_llseek,
  1343. .read = do_sync_read,
  1344. .write = do_sync_write,
  1345. .aio_read = generic_file_aio_read,
  1346. .aio_write = blkdev_aio_write,
  1347. .mmap = generic_file_mmap,
  1348. .fsync = blkdev_fsync,
  1349. .unlocked_ioctl = block_ioctl,
  1350. #ifdef CONFIG_COMPAT
  1351. .compat_ioctl = compat_blkdev_ioctl,
  1352. #endif
  1353. .splice_read = generic_file_splice_read,
  1354. .splice_write = generic_file_splice_write,
  1355. };
  1356. int ioctl_by_bdev(struct block_device *bdev, unsigned cmd, unsigned long arg)
  1357. {
  1358. int res;
  1359. mm_segment_t old_fs = get_fs();
  1360. set_fs(KERNEL_DS);
  1361. res = blkdev_ioctl(bdev, 0, cmd, arg);
  1362. set_fs(old_fs);
  1363. return res;
  1364. }
  1365. EXPORT_SYMBOL(ioctl_by_bdev);
  1366. /**
  1367. * lookup_bdev - lookup a struct block_device by name
  1368. * @pathname: special file representing the block device
  1369. *
  1370. * Get a reference to the blockdevice at @pathname in the current
  1371. * namespace if possible and return it. Return ERR_PTR(error)
  1372. * otherwise.
  1373. */
  1374. struct block_device *lookup_bdev(const char *pathname)
  1375. {
  1376. struct block_device *bdev;
  1377. struct inode *inode;
  1378. struct path path;
  1379. int error;
  1380. if (!pathname || !*pathname)
  1381. return ERR_PTR(-EINVAL);
  1382. error = kern_path(pathname, LOOKUP_FOLLOW, &path);
  1383. if (error)
  1384. return ERR_PTR(error);
  1385. inode = path.dentry->d_inode;
  1386. error = -ENOTBLK;
  1387. if (!S_ISBLK(inode->i_mode))
  1388. goto fail;
  1389. error = -EACCES;
  1390. if (path.mnt->mnt_flags & MNT_NODEV)
  1391. goto fail;
  1392. error = -ENOMEM;
  1393. bdev = bd_acquire(inode);
  1394. if (!bdev)
  1395. goto fail;
  1396. out:
  1397. path_put(&path);
  1398. return bdev;
  1399. fail:
  1400. bdev = ERR_PTR(error);
  1401. goto out;
  1402. }
  1403. EXPORT_SYMBOL(lookup_bdev);
  1404. int __invalidate_device(struct block_device *bdev, bool kill_dirty)
  1405. {
  1406. struct super_block *sb = get_super(bdev);
  1407. int res = 0;
  1408. if (sb) {
  1409. /*
  1410. * no need to lock the super, get_super holds the
  1411. * read mutex so the filesystem cannot go away
  1412. * under us (->put_super runs with the write lock
  1413. * hold).
  1414. */
  1415. shrink_dcache_sb(sb);
  1416. res = invalidate_inodes(sb, kill_dirty);
  1417. drop_super(sb);
  1418. }
  1419. invalidate_bdev(bdev);
  1420. return res;
  1421. }
  1422. EXPORT_SYMBOL(__invalidate_device);