block_dev.c 40 KB

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