block_dev.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166
  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/backing-dev.h>
  17. #include <linux/module.h>
  18. #include <linux/blkpg.h>
  19. #include <linux/magic.h>
  20. #include <linux/dax.h>
  21. #include <linux/buffer_head.h>
  22. #include <linux/swap.h>
  23. #include <linux/pagevec.h>
  24. #include <linux/writeback.h>
  25. #include <linux/mpage.h>
  26. #include <linux/mount.h>
  27. #include <linux/uio.h>
  28. #include <linux/namei.h>
  29. #include <linux/log2.h>
  30. #include <linux/cleancache.h>
  31. #include <linux/dax.h>
  32. #include <linux/badblocks.h>
  33. #include <linux/task_io_accounting_ops.h>
  34. #include <linux/falloc.h>
  35. #include <linux/uaccess.h>
  36. #include "internal.h"
  37. struct bdev_inode {
  38. struct block_device bdev;
  39. struct inode vfs_inode;
  40. };
  41. static const struct address_space_operations def_blk_aops;
  42. static inline struct bdev_inode *BDEV_I(struct inode *inode)
  43. {
  44. return container_of(inode, struct bdev_inode, vfs_inode);
  45. }
  46. struct block_device *I_BDEV(struct inode *inode)
  47. {
  48. return &BDEV_I(inode)->bdev;
  49. }
  50. EXPORT_SYMBOL(I_BDEV);
  51. void __vfs_msg(struct super_block *sb, const char *prefix, const char *fmt, ...)
  52. {
  53. struct va_format vaf;
  54. va_list args;
  55. va_start(args, fmt);
  56. vaf.fmt = fmt;
  57. vaf.va = &args;
  58. printk_ratelimited("%sVFS (%s): %pV\n", prefix, sb->s_id, &vaf);
  59. va_end(args);
  60. }
  61. static void bdev_write_inode(struct block_device *bdev)
  62. {
  63. struct inode *inode = bdev->bd_inode;
  64. int ret;
  65. spin_lock(&inode->i_lock);
  66. while (inode->i_state & I_DIRTY) {
  67. spin_unlock(&inode->i_lock);
  68. ret = write_inode_now(inode, true);
  69. if (ret) {
  70. char name[BDEVNAME_SIZE];
  71. pr_warn_ratelimited("VFS: Dirty inode writeback failed "
  72. "for block device %s (err=%d).\n",
  73. bdevname(bdev, name), ret);
  74. }
  75. spin_lock(&inode->i_lock);
  76. }
  77. spin_unlock(&inode->i_lock);
  78. }
  79. /* Kill _all_ buffers and pagecache , dirty or not.. */
  80. void kill_bdev(struct block_device *bdev)
  81. {
  82. struct address_space *mapping = bdev->bd_inode->i_mapping;
  83. if (mapping->nrpages == 0 && mapping->nrexceptional == 0)
  84. return;
  85. invalidate_bh_lrus();
  86. truncate_inode_pages(mapping, 0);
  87. }
  88. EXPORT_SYMBOL(kill_bdev);
  89. /* Invalidate clean unused buffers and pagecache. */
  90. void invalidate_bdev(struct block_device *bdev)
  91. {
  92. struct address_space *mapping = bdev->bd_inode->i_mapping;
  93. if (mapping->nrpages) {
  94. invalidate_bh_lrus();
  95. lru_add_drain_all(); /* make sure all lru add caches are flushed */
  96. invalidate_mapping_pages(mapping, 0, -1);
  97. }
  98. /* 99% of the time, we don't need to flush the cleancache on the bdev.
  99. * But, for the strange corners, lets be cautious
  100. */
  101. cleancache_invalidate_inode(mapping);
  102. }
  103. EXPORT_SYMBOL(invalidate_bdev);
  104. int set_blocksize(struct block_device *bdev, int size)
  105. {
  106. /* Size must be a power of two, and between 512 and PAGE_SIZE */
  107. if (size > PAGE_SIZE || size < 512 || !is_power_of_2(size))
  108. return -EINVAL;
  109. /* Size cannot be smaller than the size supported by the device */
  110. if (size < bdev_logical_block_size(bdev))
  111. return -EINVAL;
  112. /* Don't change the size if it is same as current */
  113. if (bdev->bd_block_size != size) {
  114. sync_blockdev(bdev);
  115. bdev->bd_block_size = size;
  116. bdev->bd_inode->i_blkbits = blksize_bits(size);
  117. kill_bdev(bdev);
  118. }
  119. return 0;
  120. }
  121. EXPORT_SYMBOL(set_blocksize);
  122. int sb_set_blocksize(struct super_block *sb, int size)
  123. {
  124. if (set_blocksize(sb->s_bdev, size))
  125. return 0;
  126. /* If we get here, we know size is power of two
  127. * and it's value is between 512 and PAGE_SIZE */
  128. sb->s_blocksize = size;
  129. sb->s_blocksize_bits = blksize_bits(size);
  130. return sb->s_blocksize;
  131. }
  132. EXPORT_SYMBOL(sb_set_blocksize);
  133. int sb_min_blocksize(struct super_block *sb, int size)
  134. {
  135. int minsize = bdev_logical_block_size(sb->s_bdev);
  136. if (size < minsize)
  137. size = minsize;
  138. return sb_set_blocksize(sb, size);
  139. }
  140. EXPORT_SYMBOL(sb_min_blocksize);
  141. static int
  142. blkdev_get_block(struct inode *inode, sector_t iblock,
  143. struct buffer_head *bh, int create)
  144. {
  145. bh->b_bdev = I_BDEV(inode);
  146. bh->b_blocknr = iblock;
  147. set_buffer_mapped(bh);
  148. return 0;
  149. }
  150. static struct inode *bdev_file_inode(struct file *file)
  151. {
  152. return file->f_mapping->host;
  153. }
  154. static unsigned int dio_bio_write_op(struct kiocb *iocb)
  155. {
  156. unsigned int op = REQ_OP_WRITE | REQ_SYNC | REQ_IDLE;
  157. /* avoid the need for a I/O completion work item */
  158. if (iocb->ki_flags & IOCB_DSYNC)
  159. op |= REQ_FUA;
  160. return op;
  161. }
  162. #define DIO_INLINE_BIO_VECS 4
  163. static void blkdev_bio_end_io_simple(struct bio *bio)
  164. {
  165. struct task_struct *waiter = bio->bi_private;
  166. WRITE_ONCE(bio->bi_private, NULL);
  167. wake_up_process(waiter);
  168. }
  169. static ssize_t
  170. __blkdev_direct_IO_simple(struct kiocb *iocb, struct iov_iter *iter,
  171. int nr_pages)
  172. {
  173. struct file *file = iocb->ki_filp;
  174. struct block_device *bdev = I_BDEV(bdev_file_inode(file));
  175. struct bio_vec inline_vecs[DIO_INLINE_BIO_VECS], *vecs, *bvec;
  176. loff_t pos = iocb->ki_pos;
  177. bool should_dirty = false;
  178. struct bio bio;
  179. ssize_t ret;
  180. blk_qc_t qc;
  181. int i;
  182. if ((pos | iov_iter_alignment(iter)) &
  183. (bdev_logical_block_size(bdev) - 1))
  184. return -EINVAL;
  185. if (nr_pages <= DIO_INLINE_BIO_VECS)
  186. vecs = inline_vecs;
  187. else {
  188. vecs = kmalloc(nr_pages * sizeof(struct bio_vec), GFP_KERNEL);
  189. if (!vecs)
  190. return -ENOMEM;
  191. }
  192. bio_init(&bio, vecs, nr_pages);
  193. bio_set_dev(&bio, bdev);
  194. bio.bi_iter.bi_sector = pos >> 9;
  195. bio.bi_write_hint = iocb->ki_hint;
  196. bio.bi_private = current;
  197. bio.bi_end_io = blkdev_bio_end_io_simple;
  198. ret = bio_iov_iter_get_pages(&bio, iter);
  199. if (unlikely(ret))
  200. return ret;
  201. ret = bio.bi_iter.bi_size;
  202. if (iov_iter_rw(iter) == READ) {
  203. bio.bi_opf = REQ_OP_READ;
  204. if (iter_is_iovec(iter))
  205. should_dirty = true;
  206. } else {
  207. bio.bi_opf = dio_bio_write_op(iocb);
  208. task_io_account_write(ret);
  209. }
  210. qc = submit_bio(&bio);
  211. for (;;) {
  212. set_current_state(TASK_UNINTERRUPTIBLE);
  213. if (!READ_ONCE(bio.bi_private))
  214. break;
  215. if (!(iocb->ki_flags & IOCB_HIPRI) ||
  216. !blk_mq_poll(bdev_get_queue(bdev), qc))
  217. io_schedule();
  218. }
  219. __set_current_state(TASK_RUNNING);
  220. bio_for_each_segment_all(bvec, &bio, i) {
  221. if (should_dirty && !PageCompound(bvec->bv_page))
  222. set_page_dirty_lock(bvec->bv_page);
  223. put_page(bvec->bv_page);
  224. }
  225. if (vecs != inline_vecs)
  226. kfree(vecs);
  227. if (unlikely(bio.bi_status))
  228. ret = blk_status_to_errno(bio.bi_status);
  229. bio_uninit(&bio);
  230. return ret;
  231. }
  232. struct blkdev_dio {
  233. union {
  234. struct kiocb *iocb;
  235. struct task_struct *waiter;
  236. };
  237. size_t size;
  238. atomic_t ref;
  239. bool multi_bio : 1;
  240. bool should_dirty : 1;
  241. bool is_sync : 1;
  242. struct bio bio;
  243. };
  244. static struct bio_set *blkdev_dio_pool __read_mostly;
  245. static void blkdev_bio_end_io(struct bio *bio)
  246. {
  247. struct blkdev_dio *dio = bio->bi_private;
  248. bool should_dirty = dio->should_dirty;
  249. if (dio->multi_bio && !atomic_dec_and_test(&dio->ref)) {
  250. if (bio->bi_status && !dio->bio.bi_status)
  251. dio->bio.bi_status = bio->bi_status;
  252. } else {
  253. if (!dio->is_sync) {
  254. struct kiocb *iocb = dio->iocb;
  255. ssize_t ret;
  256. if (likely(!dio->bio.bi_status)) {
  257. ret = dio->size;
  258. iocb->ki_pos += ret;
  259. } else {
  260. ret = blk_status_to_errno(dio->bio.bi_status);
  261. }
  262. dio->iocb->ki_complete(iocb, ret, 0);
  263. bio_put(&dio->bio);
  264. } else {
  265. struct task_struct *waiter = dio->waiter;
  266. WRITE_ONCE(dio->waiter, NULL);
  267. wake_up_process(waiter);
  268. }
  269. }
  270. if (should_dirty) {
  271. bio_check_pages_dirty(bio);
  272. } else {
  273. struct bio_vec *bvec;
  274. int i;
  275. bio_for_each_segment_all(bvec, bio, i)
  276. put_page(bvec->bv_page);
  277. bio_put(bio);
  278. }
  279. }
  280. static ssize_t
  281. __blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter, int nr_pages)
  282. {
  283. struct file *file = iocb->ki_filp;
  284. struct inode *inode = bdev_file_inode(file);
  285. struct block_device *bdev = I_BDEV(inode);
  286. struct blk_plug plug;
  287. struct blkdev_dio *dio;
  288. struct bio *bio;
  289. bool is_read = (iov_iter_rw(iter) == READ), is_sync;
  290. loff_t pos = iocb->ki_pos;
  291. blk_qc_t qc = BLK_QC_T_NONE;
  292. int ret = 0;
  293. if ((pos | iov_iter_alignment(iter)) &
  294. (bdev_logical_block_size(bdev) - 1))
  295. return -EINVAL;
  296. bio = bio_alloc_bioset(GFP_KERNEL, nr_pages, blkdev_dio_pool);
  297. bio_get(bio); /* extra ref for the completion handler */
  298. dio = container_of(bio, struct blkdev_dio, bio);
  299. dio->is_sync = is_sync = is_sync_kiocb(iocb);
  300. if (dio->is_sync)
  301. dio->waiter = current;
  302. else
  303. dio->iocb = iocb;
  304. dio->size = 0;
  305. dio->multi_bio = false;
  306. dio->should_dirty = is_read && (iter->type == ITER_IOVEC);
  307. blk_start_plug(&plug);
  308. for (;;) {
  309. bio_set_dev(bio, bdev);
  310. bio->bi_iter.bi_sector = pos >> 9;
  311. bio->bi_write_hint = iocb->ki_hint;
  312. bio->bi_private = dio;
  313. bio->bi_end_io = blkdev_bio_end_io;
  314. ret = bio_iov_iter_get_pages(bio, iter);
  315. if (unlikely(ret)) {
  316. bio->bi_status = BLK_STS_IOERR;
  317. bio_endio(bio);
  318. break;
  319. }
  320. if (is_read) {
  321. bio->bi_opf = REQ_OP_READ;
  322. if (dio->should_dirty)
  323. bio_set_pages_dirty(bio);
  324. } else {
  325. bio->bi_opf = dio_bio_write_op(iocb);
  326. task_io_account_write(bio->bi_iter.bi_size);
  327. }
  328. dio->size += bio->bi_iter.bi_size;
  329. pos += bio->bi_iter.bi_size;
  330. nr_pages = iov_iter_npages(iter, BIO_MAX_PAGES);
  331. if (!nr_pages) {
  332. qc = submit_bio(bio);
  333. break;
  334. }
  335. if (!dio->multi_bio) {
  336. dio->multi_bio = true;
  337. atomic_set(&dio->ref, 2);
  338. } else {
  339. atomic_inc(&dio->ref);
  340. }
  341. submit_bio(bio);
  342. bio = bio_alloc(GFP_KERNEL, nr_pages);
  343. }
  344. blk_finish_plug(&plug);
  345. if (!is_sync)
  346. return -EIOCBQUEUED;
  347. for (;;) {
  348. set_current_state(TASK_UNINTERRUPTIBLE);
  349. if (!READ_ONCE(dio->waiter))
  350. break;
  351. if (!(iocb->ki_flags & IOCB_HIPRI) ||
  352. !blk_mq_poll(bdev_get_queue(bdev), qc))
  353. io_schedule();
  354. }
  355. __set_current_state(TASK_RUNNING);
  356. if (!ret)
  357. ret = blk_status_to_errno(dio->bio.bi_status);
  358. if (likely(!ret))
  359. ret = dio->size;
  360. bio_put(&dio->bio);
  361. return ret;
  362. }
  363. static ssize_t
  364. blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
  365. {
  366. int nr_pages;
  367. nr_pages = iov_iter_npages(iter, BIO_MAX_PAGES + 1);
  368. if (!nr_pages)
  369. return 0;
  370. if (is_sync_kiocb(iocb) && nr_pages <= BIO_MAX_PAGES)
  371. return __blkdev_direct_IO_simple(iocb, iter, nr_pages);
  372. return __blkdev_direct_IO(iocb, iter, min(nr_pages, BIO_MAX_PAGES));
  373. }
  374. static __init int blkdev_init(void)
  375. {
  376. blkdev_dio_pool = bioset_create(4, offsetof(struct blkdev_dio, bio), BIOSET_NEED_BVECS);
  377. if (!blkdev_dio_pool)
  378. return -ENOMEM;
  379. return 0;
  380. }
  381. module_init(blkdev_init);
  382. int __sync_blockdev(struct block_device *bdev, int wait)
  383. {
  384. if (!bdev)
  385. return 0;
  386. if (!wait)
  387. return filemap_flush(bdev->bd_inode->i_mapping);
  388. return filemap_write_and_wait(bdev->bd_inode->i_mapping);
  389. }
  390. /*
  391. * Write out and wait upon all the dirty data associated with a block
  392. * device via its mapping. Does not take the superblock lock.
  393. */
  394. int sync_blockdev(struct block_device *bdev)
  395. {
  396. return __sync_blockdev(bdev, 1);
  397. }
  398. EXPORT_SYMBOL(sync_blockdev);
  399. /*
  400. * Write out and wait upon all dirty data associated with this
  401. * device. Filesystem data as well as the underlying block
  402. * device. Takes the superblock lock.
  403. */
  404. int fsync_bdev(struct block_device *bdev)
  405. {
  406. struct super_block *sb = get_super(bdev);
  407. if (sb) {
  408. int res = sync_filesystem(sb);
  409. drop_super(sb);
  410. return res;
  411. }
  412. return sync_blockdev(bdev);
  413. }
  414. EXPORT_SYMBOL(fsync_bdev);
  415. /**
  416. * freeze_bdev -- lock a filesystem and force it into a consistent state
  417. * @bdev: blockdevice to lock
  418. *
  419. * If a superblock is found on this device, we take the s_umount semaphore
  420. * on it to make sure nobody unmounts until the snapshot creation is done.
  421. * The reference counter (bd_fsfreeze_count) guarantees that only the last
  422. * unfreeze process can unfreeze the frozen filesystem actually when multiple
  423. * freeze requests arrive simultaneously. It counts up in freeze_bdev() and
  424. * count down in thaw_bdev(). When it becomes 0, thaw_bdev() will unfreeze
  425. * actually.
  426. */
  427. struct super_block *freeze_bdev(struct block_device *bdev)
  428. {
  429. struct super_block *sb;
  430. int error = 0;
  431. mutex_lock(&bdev->bd_fsfreeze_mutex);
  432. if (++bdev->bd_fsfreeze_count > 1) {
  433. /*
  434. * We don't even need to grab a reference - the first call
  435. * to freeze_bdev grab an active reference and only the last
  436. * thaw_bdev drops it.
  437. */
  438. sb = get_super(bdev);
  439. if (sb)
  440. drop_super(sb);
  441. mutex_unlock(&bdev->bd_fsfreeze_mutex);
  442. return sb;
  443. }
  444. sb = get_active_super(bdev);
  445. if (!sb)
  446. goto out;
  447. if (sb->s_op->freeze_super)
  448. error = sb->s_op->freeze_super(sb);
  449. else
  450. error = freeze_super(sb);
  451. if (error) {
  452. deactivate_super(sb);
  453. bdev->bd_fsfreeze_count--;
  454. mutex_unlock(&bdev->bd_fsfreeze_mutex);
  455. return ERR_PTR(error);
  456. }
  457. deactivate_super(sb);
  458. out:
  459. sync_blockdev(bdev);
  460. mutex_unlock(&bdev->bd_fsfreeze_mutex);
  461. return sb; /* thaw_bdev releases s->s_umount */
  462. }
  463. EXPORT_SYMBOL(freeze_bdev);
  464. /**
  465. * thaw_bdev -- unlock filesystem
  466. * @bdev: blockdevice to unlock
  467. * @sb: associated superblock
  468. *
  469. * Unlocks the filesystem and marks it writeable again after freeze_bdev().
  470. */
  471. int thaw_bdev(struct block_device *bdev, struct super_block *sb)
  472. {
  473. int error = -EINVAL;
  474. mutex_lock(&bdev->bd_fsfreeze_mutex);
  475. if (!bdev->bd_fsfreeze_count)
  476. goto out;
  477. error = 0;
  478. if (--bdev->bd_fsfreeze_count > 0)
  479. goto out;
  480. if (!sb)
  481. goto out;
  482. if (sb->s_op->thaw_super)
  483. error = sb->s_op->thaw_super(sb);
  484. else
  485. error = thaw_super(sb);
  486. if (error)
  487. bdev->bd_fsfreeze_count++;
  488. out:
  489. mutex_unlock(&bdev->bd_fsfreeze_mutex);
  490. return error;
  491. }
  492. EXPORT_SYMBOL(thaw_bdev);
  493. static int blkdev_writepage(struct page *page, struct writeback_control *wbc)
  494. {
  495. return block_write_full_page(page, blkdev_get_block, wbc);
  496. }
  497. static int blkdev_readpage(struct file * file, struct page * page)
  498. {
  499. return block_read_full_page(page, blkdev_get_block);
  500. }
  501. static int blkdev_readpages(struct file *file, struct address_space *mapping,
  502. struct list_head *pages, unsigned nr_pages)
  503. {
  504. return mpage_readpages(mapping, pages, nr_pages, blkdev_get_block);
  505. }
  506. static int blkdev_write_begin(struct file *file, struct address_space *mapping,
  507. loff_t pos, unsigned len, unsigned flags,
  508. struct page **pagep, void **fsdata)
  509. {
  510. return block_write_begin(mapping, pos, len, flags, pagep,
  511. blkdev_get_block);
  512. }
  513. static int blkdev_write_end(struct file *file, struct address_space *mapping,
  514. loff_t pos, unsigned len, unsigned copied,
  515. struct page *page, void *fsdata)
  516. {
  517. int ret;
  518. ret = block_write_end(file, mapping, pos, len, copied, page, fsdata);
  519. unlock_page(page);
  520. put_page(page);
  521. return ret;
  522. }
  523. /*
  524. * private llseek:
  525. * for a block special file file_inode(file)->i_size is zero
  526. * so we compute the size by hand (just as in block_read/write above)
  527. */
  528. static loff_t block_llseek(struct file *file, loff_t offset, int whence)
  529. {
  530. struct inode *bd_inode = bdev_file_inode(file);
  531. loff_t retval;
  532. inode_lock(bd_inode);
  533. retval = fixed_size_llseek(file, offset, whence, i_size_read(bd_inode));
  534. inode_unlock(bd_inode);
  535. return retval;
  536. }
  537. int blkdev_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
  538. {
  539. struct inode *bd_inode = bdev_file_inode(filp);
  540. struct block_device *bdev = I_BDEV(bd_inode);
  541. int error;
  542. error = file_write_and_wait_range(filp, start, end);
  543. if (error)
  544. return error;
  545. /*
  546. * There is no need to serialise calls to blkdev_issue_flush with
  547. * i_mutex and doing so causes performance issues with concurrent
  548. * O_SYNC writers to a block device.
  549. */
  550. error = blkdev_issue_flush(bdev, GFP_KERNEL, NULL);
  551. if (error == -EOPNOTSUPP)
  552. error = 0;
  553. return error;
  554. }
  555. EXPORT_SYMBOL(blkdev_fsync);
  556. /**
  557. * bdev_read_page() - Start reading a page from a block device
  558. * @bdev: The device to read the page from
  559. * @sector: The offset on the device to read the page to (need not be aligned)
  560. * @page: The page to read
  561. *
  562. * On entry, the page should be locked. It will be unlocked when the page
  563. * has been read. If the block driver implements rw_page synchronously,
  564. * that will be true on exit from this function, but it need not be.
  565. *
  566. * Errors returned by this function are usually "soft", eg out of memory, or
  567. * queue full; callers should try a different route to read this page rather
  568. * than propagate an error back up the stack.
  569. *
  570. * Return: negative errno if an error occurs, 0 if submission was successful.
  571. */
  572. int bdev_read_page(struct block_device *bdev, sector_t sector,
  573. struct page *page)
  574. {
  575. const struct block_device_operations *ops = bdev->bd_disk->fops;
  576. int result = -EOPNOTSUPP;
  577. if (!ops->rw_page || bdev_get_integrity(bdev))
  578. return result;
  579. result = blk_queue_enter(bdev->bd_queue, false);
  580. if (result)
  581. return result;
  582. result = ops->rw_page(bdev, sector + get_start_sect(bdev), page, false);
  583. blk_queue_exit(bdev->bd_queue);
  584. return result;
  585. }
  586. EXPORT_SYMBOL_GPL(bdev_read_page);
  587. /**
  588. * bdev_write_page() - Start writing a page to a block device
  589. * @bdev: The device to write the page to
  590. * @sector: The offset on the device to write the page to (need not be aligned)
  591. * @page: The page to write
  592. * @wbc: The writeback_control for the write
  593. *
  594. * On entry, the page should be locked and not currently under writeback.
  595. * On exit, if the write started successfully, the page will be unlocked and
  596. * under writeback. If the write failed already (eg the driver failed to
  597. * queue the page to the device), the page will still be locked. If the
  598. * caller is a ->writepage implementation, it will need to unlock the page.
  599. *
  600. * Errors returned by this function are usually "soft", eg out of memory, or
  601. * queue full; callers should try a different route to write this page rather
  602. * than propagate an error back up the stack.
  603. *
  604. * Return: negative errno if an error occurs, 0 if submission was successful.
  605. */
  606. int bdev_write_page(struct block_device *bdev, sector_t sector,
  607. struct page *page, struct writeback_control *wbc)
  608. {
  609. int result;
  610. const struct block_device_operations *ops = bdev->bd_disk->fops;
  611. if (!ops->rw_page || bdev_get_integrity(bdev))
  612. return -EOPNOTSUPP;
  613. result = blk_queue_enter(bdev->bd_queue, false);
  614. if (result)
  615. return result;
  616. set_page_writeback(page);
  617. result = ops->rw_page(bdev, sector + get_start_sect(bdev), page, true);
  618. if (result) {
  619. end_page_writeback(page);
  620. } else {
  621. clean_page_buffers(page);
  622. unlock_page(page);
  623. }
  624. blk_queue_exit(bdev->bd_queue);
  625. return result;
  626. }
  627. EXPORT_SYMBOL_GPL(bdev_write_page);
  628. /*
  629. * pseudo-fs
  630. */
  631. static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);
  632. static struct kmem_cache * bdev_cachep __read_mostly;
  633. static struct inode *bdev_alloc_inode(struct super_block *sb)
  634. {
  635. struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, GFP_KERNEL);
  636. if (!ei)
  637. return NULL;
  638. return &ei->vfs_inode;
  639. }
  640. static void bdev_i_callback(struct rcu_head *head)
  641. {
  642. struct inode *inode = container_of(head, struct inode, i_rcu);
  643. struct bdev_inode *bdi = BDEV_I(inode);
  644. kmem_cache_free(bdev_cachep, bdi);
  645. }
  646. static void bdev_destroy_inode(struct inode *inode)
  647. {
  648. call_rcu(&inode->i_rcu, bdev_i_callback);
  649. }
  650. static void init_once(void *foo)
  651. {
  652. struct bdev_inode *ei = (struct bdev_inode *) foo;
  653. struct block_device *bdev = &ei->bdev;
  654. memset(bdev, 0, sizeof(*bdev));
  655. mutex_init(&bdev->bd_mutex);
  656. INIT_LIST_HEAD(&bdev->bd_list);
  657. #ifdef CONFIG_SYSFS
  658. INIT_LIST_HEAD(&bdev->bd_holder_disks);
  659. #endif
  660. bdev->bd_bdi = &noop_backing_dev_info;
  661. inode_init_once(&ei->vfs_inode);
  662. /* Initialize mutex for freeze. */
  663. mutex_init(&bdev->bd_fsfreeze_mutex);
  664. }
  665. static void bdev_evict_inode(struct inode *inode)
  666. {
  667. struct block_device *bdev = &BDEV_I(inode)->bdev;
  668. truncate_inode_pages_final(&inode->i_data);
  669. invalidate_inode_buffers(inode); /* is it needed here? */
  670. clear_inode(inode);
  671. spin_lock(&bdev_lock);
  672. list_del_init(&bdev->bd_list);
  673. spin_unlock(&bdev_lock);
  674. /* Detach inode from wb early as bdi_put() may free bdi->wb */
  675. inode_detach_wb(inode);
  676. if (bdev->bd_bdi != &noop_backing_dev_info) {
  677. bdi_put(bdev->bd_bdi);
  678. bdev->bd_bdi = &noop_backing_dev_info;
  679. }
  680. }
  681. static const struct super_operations bdev_sops = {
  682. .statfs = simple_statfs,
  683. .alloc_inode = bdev_alloc_inode,
  684. .destroy_inode = bdev_destroy_inode,
  685. .drop_inode = generic_delete_inode,
  686. .evict_inode = bdev_evict_inode,
  687. };
  688. static struct dentry *bd_mount(struct file_system_type *fs_type,
  689. int flags, const char *dev_name, void *data)
  690. {
  691. struct dentry *dent;
  692. dent = mount_pseudo(fs_type, "bdev:", &bdev_sops, NULL, BDEVFS_MAGIC);
  693. if (!IS_ERR(dent))
  694. dent->d_sb->s_iflags |= SB_I_CGROUPWB;
  695. return dent;
  696. }
  697. static struct file_system_type bd_type = {
  698. .name = "bdev",
  699. .mount = bd_mount,
  700. .kill_sb = kill_anon_super,
  701. };
  702. struct super_block *blockdev_superblock __read_mostly;
  703. EXPORT_SYMBOL_GPL(blockdev_superblock);
  704. void __init bdev_cache_init(void)
  705. {
  706. int err;
  707. static struct vfsmount *bd_mnt;
  708. bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
  709. 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
  710. SLAB_MEM_SPREAD|SLAB_ACCOUNT|SLAB_PANIC),
  711. init_once);
  712. err = register_filesystem(&bd_type);
  713. if (err)
  714. panic("Cannot register bdev pseudo-fs");
  715. bd_mnt = kern_mount(&bd_type);
  716. if (IS_ERR(bd_mnt))
  717. panic("Cannot create bdev pseudo-fs");
  718. blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */
  719. }
  720. /*
  721. * Most likely _very_ bad one - but then it's hardly critical for small
  722. * /dev and can be fixed when somebody will need really large one.
  723. * Keep in mind that it will be fed through icache hash function too.
  724. */
  725. static inline unsigned long hash(dev_t dev)
  726. {
  727. return MAJOR(dev)+MINOR(dev);
  728. }
  729. static int bdev_test(struct inode *inode, void *data)
  730. {
  731. return BDEV_I(inode)->bdev.bd_dev == *(dev_t *)data;
  732. }
  733. static int bdev_set(struct inode *inode, void *data)
  734. {
  735. BDEV_I(inode)->bdev.bd_dev = *(dev_t *)data;
  736. return 0;
  737. }
  738. static LIST_HEAD(all_bdevs);
  739. /*
  740. * If there is a bdev inode for this device, unhash it so that it gets evicted
  741. * as soon as last inode reference is dropped.
  742. */
  743. void bdev_unhash_inode(dev_t dev)
  744. {
  745. struct inode *inode;
  746. inode = ilookup5(blockdev_superblock, hash(dev), bdev_test, &dev);
  747. if (inode) {
  748. remove_inode_hash(inode);
  749. iput(inode);
  750. }
  751. }
  752. struct block_device *bdget(dev_t dev)
  753. {
  754. struct block_device *bdev;
  755. struct inode *inode;
  756. inode = iget5_locked(blockdev_superblock, hash(dev),
  757. bdev_test, bdev_set, &dev);
  758. if (!inode)
  759. return NULL;
  760. bdev = &BDEV_I(inode)->bdev;
  761. if (inode->i_state & I_NEW) {
  762. bdev->bd_contains = NULL;
  763. bdev->bd_super = NULL;
  764. bdev->bd_inode = inode;
  765. bdev->bd_block_size = i_blocksize(inode);
  766. bdev->bd_part_count = 0;
  767. bdev->bd_invalidated = 0;
  768. inode->i_mode = S_IFBLK;
  769. inode->i_rdev = dev;
  770. inode->i_bdev = bdev;
  771. inode->i_data.a_ops = &def_blk_aops;
  772. mapping_set_gfp_mask(&inode->i_data, GFP_USER);
  773. spin_lock(&bdev_lock);
  774. list_add(&bdev->bd_list, &all_bdevs);
  775. spin_unlock(&bdev_lock);
  776. unlock_new_inode(inode);
  777. }
  778. return bdev;
  779. }
  780. EXPORT_SYMBOL(bdget);
  781. /**
  782. * bdgrab -- Grab a reference to an already referenced block device
  783. * @bdev: Block device to grab a reference to.
  784. */
  785. struct block_device *bdgrab(struct block_device *bdev)
  786. {
  787. ihold(bdev->bd_inode);
  788. return bdev;
  789. }
  790. EXPORT_SYMBOL(bdgrab);
  791. long nr_blockdev_pages(void)
  792. {
  793. struct block_device *bdev;
  794. long ret = 0;
  795. spin_lock(&bdev_lock);
  796. list_for_each_entry(bdev, &all_bdevs, bd_list) {
  797. ret += bdev->bd_inode->i_mapping->nrpages;
  798. }
  799. spin_unlock(&bdev_lock);
  800. return ret;
  801. }
  802. void bdput(struct block_device *bdev)
  803. {
  804. iput(bdev->bd_inode);
  805. }
  806. EXPORT_SYMBOL(bdput);
  807. static struct block_device *bd_acquire(struct inode *inode)
  808. {
  809. struct block_device *bdev;
  810. spin_lock(&bdev_lock);
  811. bdev = inode->i_bdev;
  812. if (bdev && !inode_unhashed(bdev->bd_inode)) {
  813. bdgrab(bdev);
  814. spin_unlock(&bdev_lock);
  815. return bdev;
  816. }
  817. spin_unlock(&bdev_lock);
  818. /*
  819. * i_bdev references block device inode that was already shut down
  820. * (corresponding device got removed). Remove the reference and look
  821. * up block device inode again just in case new device got
  822. * reestablished under the same device number.
  823. */
  824. if (bdev)
  825. bd_forget(inode);
  826. bdev = bdget(inode->i_rdev);
  827. if (bdev) {
  828. spin_lock(&bdev_lock);
  829. if (!inode->i_bdev) {
  830. /*
  831. * We take an additional reference to bd_inode,
  832. * and it's released in clear_inode() of inode.
  833. * So, we can access it via ->i_mapping always
  834. * without igrab().
  835. */
  836. bdgrab(bdev);
  837. inode->i_bdev = bdev;
  838. inode->i_mapping = bdev->bd_inode->i_mapping;
  839. }
  840. spin_unlock(&bdev_lock);
  841. }
  842. return bdev;
  843. }
  844. /* Call when you free inode */
  845. void bd_forget(struct inode *inode)
  846. {
  847. struct block_device *bdev = NULL;
  848. spin_lock(&bdev_lock);
  849. if (!sb_is_blkdev_sb(inode->i_sb))
  850. bdev = inode->i_bdev;
  851. inode->i_bdev = NULL;
  852. inode->i_mapping = &inode->i_data;
  853. spin_unlock(&bdev_lock);
  854. if (bdev)
  855. bdput(bdev);
  856. }
  857. /**
  858. * bd_may_claim - test whether a block device can be claimed
  859. * @bdev: block device of interest
  860. * @whole: whole block device containing @bdev, may equal @bdev
  861. * @holder: holder trying to claim @bdev
  862. *
  863. * Test whether @bdev can be claimed by @holder.
  864. *
  865. * CONTEXT:
  866. * spin_lock(&bdev_lock).
  867. *
  868. * RETURNS:
  869. * %true if @bdev can be claimed, %false otherwise.
  870. */
  871. static bool bd_may_claim(struct block_device *bdev, struct block_device *whole,
  872. void *holder)
  873. {
  874. if (bdev->bd_holder == holder)
  875. return true; /* already a holder */
  876. else if (bdev->bd_holder != NULL)
  877. return false; /* held by someone else */
  878. else if (whole == bdev)
  879. return true; /* is a whole device which isn't held */
  880. else if (whole->bd_holder == bd_may_claim)
  881. return true; /* is a partition of a device that is being partitioned */
  882. else if (whole->bd_holder != NULL)
  883. return false; /* is a partition of a held device */
  884. else
  885. return true; /* is a partition of an un-held device */
  886. }
  887. /**
  888. * bd_prepare_to_claim - prepare to claim a block device
  889. * @bdev: block device of interest
  890. * @whole: the whole device containing @bdev, may equal @bdev
  891. * @holder: holder trying to claim @bdev
  892. *
  893. * Prepare to claim @bdev. This function fails if @bdev is already
  894. * claimed by another holder and waits if another claiming is in
  895. * progress. This function doesn't actually claim. On successful
  896. * return, the caller has ownership of bd_claiming and bd_holder[s].
  897. *
  898. * CONTEXT:
  899. * spin_lock(&bdev_lock). Might release bdev_lock, sleep and regrab
  900. * it multiple times.
  901. *
  902. * RETURNS:
  903. * 0 if @bdev can be claimed, -EBUSY otherwise.
  904. */
  905. static int bd_prepare_to_claim(struct block_device *bdev,
  906. struct block_device *whole, void *holder)
  907. {
  908. retry:
  909. /* if someone else claimed, fail */
  910. if (!bd_may_claim(bdev, whole, holder))
  911. return -EBUSY;
  912. /* if claiming is already in progress, wait for it to finish */
  913. if (whole->bd_claiming) {
  914. wait_queue_head_t *wq = bit_waitqueue(&whole->bd_claiming, 0);
  915. DEFINE_WAIT(wait);
  916. prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
  917. spin_unlock(&bdev_lock);
  918. schedule();
  919. finish_wait(wq, &wait);
  920. spin_lock(&bdev_lock);
  921. goto retry;
  922. }
  923. /* yay, all mine */
  924. return 0;
  925. }
  926. /**
  927. * bd_start_claiming - start claiming a block device
  928. * @bdev: block device of interest
  929. * @holder: holder trying to claim @bdev
  930. *
  931. * @bdev is about to be opened exclusively. Check @bdev can be opened
  932. * exclusively and mark that an exclusive open is in progress. Each
  933. * successful call to this function must be matched with a call to
  934. * either bd_finish_claiming() or bd_abort_claiming() (which do not
  935. * fail).
  936. *
  937. * This function is used to gain exclusive access to the block device
  938. * without actually causing other exclusive open attempts to fail. It
  939. * should be used when the open sequence itself requires exclusive
  940. * access but may subsequently fail.
  941. *
  942. * CONTEXT:
  943. * Might sleep.
  944. *
  945. * RETURNS:
  946. * Pointer to the block device containing @bdev on success, ERR_PTR()
  947. * value on failure.
  948. */
  949. static struct block_device *bd_start_claiming(struct block_device *bdev,
  950. void *holder)
  951. {
  952. struct gendisk *disk;
  953. struct block_device *whole;
  954. int partno, err;
  955. might_sleep();
  956. /*
  957. * @bdev might not have been initialized properly yet, look up
  958. * and grab the outer block device the hard way.
  959. */
  960. disk = get_gendisk(bdev->bd_dev, &partno);
  961. if (!disk)
  962. return ERR_PTR(-ENXIO);
  963. /*
  964. * Normally, @bdev should equal what's returned from bdget_disk()
  965. * if partno is 0; however, some drivers (floppy) use multiple
  966. * bdev's for the same physical device and @bdev may be one of the
  967. * aliases. Keep @bdev if partno is 0. This means claimer
  968. * tracking is broken for those devices but it has always been that
  969. * way.
  970. */
  971. if (partno)
  972. whole = bdget_disk(disk, 0);
  973. else
  974. whole = bdgrab(bdev);
  975. module_put(disk->fops->owner);
  976. put_disk(disk);
  977. if (!whole)
  978. return ERR_PTR(-ENOMEM);
  979. /* prepare to claim, if successful, mark claiming in progress */
  980. spin_lock(&bdev_lock);
  981. err = bd_prepare_to_claim(bdev, whole, holder);
  982. if (err == 0) {
  983. whole->bd_claiming = holder;
  984. spin_unlock(&bdev_lock);
  985. return whole;
  986. } else {
  987. spin_unlock(&bdev_lock);
  988. bdput(whole);
  989. return ERR_PTR(err);
  990. }
  991. }
  992. #ifdef CONFIG_SYSFS
  993. struct bd_holder_disk {
  994. struct list_head list;
  995. struct gendisk *disk;
  996. int refcnt;
  997. };
  998. static struct bd_holder_disk *bd_find_holder_disk(struct block_device *bdev,
  999. struct gendisk *disk)
  1000. {
  1001. struct bd_holder_disk *holder;
  1002. list_for_each_entry(holder, &bdev->bd_holder_disks, list)
  1003. if (holder->disk == disk)
  1004. return holder;
  1005. return NULL;
  1006. }
  1007. static int add_symlink(struct kobject *from, struct kobject *to)
  1008. {
  1009. return sysfs_create_link(from, to, kobject_name(to));
  1010. }
  1011. static void del_symlink(struct kobject *from, struct kobject *to)
  1012. {
  1013. sysfs_remove_link(from, kobject_name(to));
  1014. }
  1015. /**
  1016. * bd_link_disk_holder - create symlinks between holding disk and slave bdev
  1017. * @bdev: the claimed slave bdev
  1018. * @disk: the holding disk
  1019. *
  1020. * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
  1021. *
  1022. * This functions creates the following sysfs symlinks.
  1023. *
  1024. * - from "slaves" directory of the holder @disk to the claimed @bdev
  1025. * - from "holders" directory of the @bdev to the holder @disk
  1026. *
  1027. * For example, if /dev/dm-0 maps to /dev/sda and disk for dm-0 is
  1028. * passed to bd_link_disk_holder(), then:
  1029. *
  1030. * /sys/block/dm-0/slaves/sda --> /sys/block/sda
  1031. * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
  1032. *
  1033. * The caller must have claimed @bdev before calling this function and
  1034. * ensure that both @bdev and @disk are valid during the creation and
  1035. * lifetime of these symlinks.
  1036. *
  1037. * CONTEXT:
  1038. * Might sleep.
  1039. *
  1040. * RETURNS:
  1041. * 0 on success, -errno on failure.
  1042. */
  1043. int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk)
  1044. {
  1045. struct bd_holder_disk *holder;
  1046. int ret = 0;
  1047. mutex_lock(&bdev->bd_mutex);
  1048. WARN_ON_ONCE(!bdev->bd_holder);
  1049. /* FIXME: remove the following once add_disk() handles errors */
  1050. if (WARN_ON(!disk->slave_dir || !bdev->bd_part->holder_dir))
  1051. goto out_unlock;
  1052. holder = bd_find_holder_disk(bdev, disk);
  1053. if (holder) {
  1054. holder->refcnt++;
  1055. goto out_unlock;
  1056. }
  1057. holder = kzalloc(sizeof(*holder), GFP_KERNEL);
  1058. if (!holder) {
  1059. ret = -ENOMEM;
  1060. goto out_unlock;
  1061. }
  1062. INIT_LIST_HEAD(&holder->list);
  1063. holder->disk = disk;
  1064. holder->refcnt = 1;
  1065. ret = add_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
  1066. if (ret)
  1067. goto out_free;
  1068. ret = add_symlink(bdev->bd_part->holder_dir, &disk_to_dev(disk)->kobj);
  1069. if (ret)
  1070. goto out_del;
  1071. /*
  1072. * bdev could be deleted beneath us which would implicitly destroy
  1073. * the holder directory. Hold on to it.
  1074. */
  1075. kobject_get(bdev->bd_part->holder_dir);
  1076. list_add(&holder->list, &bdev->bd_holder_disks);
  1077. goto out_unlock;
  1078. out_del:
  1079. del_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
  1080. out_free:
  1081. kfree(holder);
  1082. out_unlock:
  1083. mutex_unlock(&bdev->bd_mutex);
  1084. return ret;
  1085. }
  1086. EXPORT_SYMBOL_GPL(bd_link_disk_holder);
  1087. /**
  1088. * bd_unlink_disk_holder - destroy symlinks created by bd_link_disk_holder()
  1089. * @bdev: the calimed slave bdev
  1090. * @disk: the holding disk
  1091. *
  1092. * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
  1093. *
  1094. * CONTEXT:
  1095. * Might sleep.
  1096. */
  1097. void bd_unlink_disk_holder(struct block_device *bdev, struct gendisk *disk)
  1098. {
  1099. struct bd_holder_disk *holder;
  1100. mutex_lock(&bdev->bd_mutex);
  1101. holder = bd_find_holder_disk(bdev, disk);
  1102. if (!WARN_ON_ONCE(holder == NULL) && !--holder->refcnt) {
  1103. del_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
  1104. del_symlink(bdev->bd_part->holder_dir,
  1105. &disk_to_dev(disk)->kobj);
  1106. kobject_put(bdev->bd_part->holder_dir);
  1107. list_del_init(&holder->list);
  1108. kfree(holder);
  1109. }
  1110. mutex_unlock(&bdev->bd_mutex);
  1111. }
  1112. EXPORT_SYMBOL_GPL(bd_unlink_disk_holder);
  1113. #endif
  1114. /**
  1115. * flush_disk - invalidates all buffer-cache entries on a disk
  1116. *
  1117. * @bdev: struct block device to be flushed
  1118. * @kill_dirty: flag to guide handling of dirty inodes
  1119. *
  1120. * Invalidates all buffer-cache entries on a disk. It should be called
  1121. * when a disk has been changed -- either by a media change or online
  1122. * resize.
  1123. */
  1124. static void flush_disk(struct block_device *bdev, bool kill_dirty)
  1125. {
  1126. if (__invalidate_device(bdev, kill_dirty)) {
  1127. printk(KERN_WARNING "VFS: busy inodes on changed media or "
  1128. "resized disk %s\n",
  1129. bdev->bd_disk ? bdev->bd_disk->disk_name : "");
  1130. }
  1131. if (!bdev->bd_disk)
  1132. return;
  1133. if (disk_part_scan_enabled(bdev->bd_disk))
  1134. bdev->bd_invalidated = 1;
  1135. }
  1136. /**
  1137. * check_disk_size_change - checks for disk size change and adjusts bdev size.
  1138. * @disk: struct gendisk to check
  1139. * @bdev: struct bdev to adjust.
  1140. *
  1141. * This routine checks to see if the bdev size does not match the disk size
  1142. * and adjusts it if it differs.
  1143. */
  1144. void check_disk_size_change(struct gendisk *disk, struct block_device *bdev)
  1145. {
  1146. loff_t disk_size, bdev_size;
  1147. disk_size = (loff_t)get_capacity(disk) << 9;
  1148. bdev_size = i_size_read(bdev->bd_inode);
  1149. if (disk_size != bdev_size) {
  1150. printk(KERN_INFO
  1151. "%s: detected capacity change from %lld to %lld\n",
  1152. disk->disk_name, bdev_size, disk_size);
  1153. i_size_write(bdev->bd_inode, disk_size);
  1154. flush_disk(bdev, false);
  1155. }
  1156. }
  1157. EXPORT_SYMBOL(check_disk_size_change);
  1158. /**
  1159. * revalidate_disk - wrapper for lower-level driver's revalidate_disk call-back
  1160. * @disk: struct gendisk to be revalidated
  1161. *
  1162. * This routine is a wrapper for lower-level driver's revalidate_disk
  1163. * call-backs. It is used to do common pre and post operations needed
  1164. * for all revalidate_disk operations.
  1165. */
  1166. int revalidate_disk(struct gendisk *disk)
  1167. {
  1168. struct block_device *bdev;
  1169. int ret = 0;
  1170. if (disk->fops->revalidate_disk)
  1171. ret = disk->fops->revalidate_disk(disk);
  1172. bdev = bdget_disk(disk, 0);
  1173. if (!bdev)
  1174. return ret;
  1175. mutex_lock(&bdev->bd_mutex);
  1176. check_disk_size_change(disk, bdev);
  1177. bdev->bd_invalidated = 0;
  1178. mutex_unlock(&bdev->bd_mutex);
  1179. bdput(bdev);
  1180. return ret;
  1181. }
  1182. EXPORT_SYMBOL(revalidate_disk);
  1183. /*
  1184. * This routine checks whether a removable media has been changed,
  1185. * and invalidates all buffer-cache-entries in that case. This
  1186. * is a relatively slow routine, so we have to try to minimize using
  1187. * it. Thus it is called only upon a 'mount' or 'open'. This
  1188. * is the best way of combining speed and utility, I think.
  1189. * People changing diskettes in the middle of an operation deserve
  1190. * to lose :-)
  1191. */
  1192. int check_disk_change(struct block_device *bdev)
  1193. {
  1194. struct gendisk *disk = bdev->bd_disk;
  1195. const struct block_device_operations *bdops = disk->fops;
  1196. unsigned int events;
  1197. events = disk_clear_events(disk, DISK_EVENT_MEDIA_CHANGE |
  1198. DISK_EVENT_EJECT_REQUEST);
  1199. if (!(events & DISK_EVENT_MEDIA_CHANGE))
  1200. return 0;
  1201. flush_disk(bdev, true);
  1202. if (bdops->revalidate_disk)
  1203. bdops->revalidate_disk(bdev->bd_disk);
  1204. return 1;
  1205. }
  1206. EXPORT_SYMBOL(check_disk_change);
  1207. void bd_set_size(struct block_device *bdev, loff_t size)
  1208. {
  1209. unsigned bsize = bdev_logical_block_size(bdev);
  1210. inode_lock(bdev->bd_inode);
  1211. i_size_write(bdev->bd_inode, size);
  1212. inode_unlock(bdev->bd_inode);
  1213. while (bsize < PAGE_SIZE) {
  1214. if (size & bsize)
  1215. break;
  1216. bsize <<= 1;
  1217. }
  1218. bdev->bd_block_size = bsize;
  1219. bdev->bd_inode->i_blkbits = blksize_bits(bsize);
  1220. }
  1221. EXPORT_SYMBOL(bd_set_size);
  1222. static void __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part);
  1223. /*
  1224. * bd_mutex locking:
  1225. *
  1226. * mutex_lock(part->bd_mutex)
  1227. * mutex_lock_nested(whole->bd_mutex, 1)
  1228. */
  1229. static int __blkdev_get(struct block_device *bdev, fmode_t mode, int for_part)
  1230. {
  1231. struct gendisk *disk;
  1232. struct module *owner;
  1233. int ret;
  1234. int partno;
  1235. int perm = 0;
  1236. if (mode & FMODE_READ)
  1237. perm |= MAY_READ;
  1238. if (mode & FMODE_WRITE)
  1239. perm |= MAY_WRITE;
  1240. /*
  1241. * hooks: /n/, see "layering violations".
  1242. */
  1243. if (!for_part) {
  1244. ret = devcgroup_inode_permission(bdev->bd_inode, perm);
  1245. if (ret != 0) {
  1246. bdput(bdev);
  1247. return ret;
  1248. }
  1249. }
  1250. restart:
  1251. ret = -ENXIO;
  1252. disk = get_gendisk(bdev->bd_dev, &partno);
  1253. if (!disk)
  1254. goto out;
  1255. owner = disk->fops->owner;
  1256. disk_block_events(disk);
  1257. mutex_lock_nested(&bdev->bd_mutex, for_part);
  1258. if (!bdev->bd_openers) {
  1259. bdev->bd_disk = disk;
  1260. bdev->bd_queue = disk->queue;
  1261. bdev->bd_contains = bdev;
  1262. bdev->bd_partno = partno;
  1263. if (!partno) {
  1264. ret = -ENXIO;
  1265. bdev->bd_part = disk_get_part(disk, partno);
  1266. if (!bdev->bd_part)
  1267. goto out_clear;
  1268. ret = 0;
  1269. if (disk->fops->open) {
  1270. ret = disk->fops->open(bdev, mode);
  1271. if (ret == -ERESTARTSYS) {
  1272. /* Lost a race with 'disk' being
  1273. * deleted, try again.
  1274. * See md.c
  1275. */
  1276. disk_put_part(bdev->bd_part);
  1277. bdev->bd_part = NULL;
  1278. bdev->bd_disk = NULL;
  1279. bdev->bd_queue = NULL;
  1280. mutex_unlock(&bdev->bd_mutex);
  1281. disk_unblock_events(disk);
  1282. put_disk(disk);
  1283. module_put(owner);
  1284. goto restart;
  1285. }
  1286. }
  1287. if (!ret)
  1288. bd_set_size(bdev,(loff_t)get_capacity(disk)<<9);
  1289. /*
  1290. * If the device is invalidated, rescan partition
  1291. * if open succeeded or failed with -ENOMEDIUM.
  1292. * The latter is necessary to prevent ghost
  1293. * partitions on a removed medium.
  1294. */
  1295. if (bdev->bd_invalidated) {
  1296. if (!ret)
  1297. rescan_partitions(disk, bdev);
  1298. else if (ret == -ENOMEDIUM)
  1299. invalidate_partitions(disk, bdev);
  1300. }
  1301. if (ret)
  1302. goto out_clear;
  1303. } else {
  1304. struct block_device *whole;
  1305. whole = bdget_disk(disk, 0);
  1306. ret = -ENOMEM;
  1307. if (!whole)
  1308. goto out_clear;
  1309. BUG_ON(for_part);
  1310. ret = __blkdev_get(whole, mode, 1);
  1311. if (ret)
  1312. goto out_clear;
  1313. bdev->bd_contains = whole;
  1314. bdev->bd_part = disk_get_part(disk, partno);
  1315. if (!(disk->flags & GENHD_FL_UP) ||
  1316. !bdev->bd_part || !bdev->bd_part->nr_sects) {
  1317. ret = -ENXIO;
  1318. goto out_clear;
  1319. }
  1320. bd_set_size(bdev, (loff_t)bdev->bd_part->nr_sects << 9);
  1321. }
  1322. if (bdev->bd_bdi == &noop_backing_dev_info)
  1323. bdev->bd_bdi = bdi_get(disk->queue->backing_dev_info);
  1324. } else {
  1325. if (bdev->bd_contains == bdev) {
  1326. ret = 0;
  1327. if (bdev->bd_disk->fops->open)
  1328. ret = bdev->bd_disk->fops->open(bdev, mode);
  1329. /* the same as first opener case, read comment there */
  1330. if (bdev->bd_invalidated) {
  1331. if (!ret)
  1332. rescan_partitions(bdev->bd_disk, bdev);
  1333. else if (ret == -ENOMEDIUM)
  1334. invalidate_partitions(bdev->bd_disk, bdev);
  1335. }
  1336. if (ret)
  1337. goto out_unlock_bdev;
  1338. }
  1339. /* only one opener holds refs to the module and disk */
  1340. put_disk(disk);
  1341. module_put(owner);
  1342. }
  1343. bdev->bd_openers++;
  1344. if (for_part)
  1345. bdev->bd_part_count++;
  1346. mutex_unlock(&bdev->bd_mutex);
  1347. disk_unblock_events(disk);
  1348. return 0;
  1349. out_clear:
  1350. disk_put_part(bdev->bd_part);
  1351. bdev->bd_disk = NULL;
  1352. bdev->bd_part = NULL;
  1353. bdev->bd_queue = NULL;
  1354. if (bdev != bdev->bd_contains)
  1355. __blkdev_put(bdev->bd_contains, mode, 1);
  1356. bdev->bd_contains = NULL;
  1357. out_unlock_bdev:
  1358. mutex_unlock(&bdev->bd_mutex);
  1359. disk_unblock_events(disk);
  1360. put_disk(disk);
  1361. module_put(owner);
  1362. out:
  1363. bdput(bdev);
  1364. return ret;
  1365. }
  1366. /**
  1367. * blkdev_get - open a block device
  1368. * @bdev: block_device to open
  1369. * @mode: FMODE_* mask
  1370. * @holder: exclusive holder identifier
  1371. *
  1372. * Open @bdev with @mode. If @mode includes %FMODE_EXCL, @bdev is
  1373. * open with exclusive access. Specifying %FMODE_EXCL with %NULL
  1374. * @holder is invalid. Exclusive opens may nest for the same @holder.
  1375. *
  1376. * On success, the reference count of @bdev is unchanged. On failure,
  1377. * @bdev is put.
  1378. *
  1379. * CONTEXT:
  1380. * Might sleep.
  1381. *
  1382. * RETURNS:
  1383. * 0 on success, -errno on failure.
  1384. */
  1385. int blkdev_get(struct block_device *bdev, fmode_t mode, void *holder)
  1386. {
  1387. struct block_device *whole = NULL;
  1388. int res;
  1389. WARN_ON_ONCE((mode & FMODE_EXCL) && !holder);
  1390. if ((mode & FMODE_EXCL) && holder) {
  1391. whole = bd_start_claiming(bdev, holder);
  1392. if (IS_ERR(whole)) {
  1393. bdput(bdev);
  1394. return PTR_ERR(whole);
  1395. }
  1396. }
  1397. res = __blkdev_get(bdev, mode, 0);
  1398. if (whole) {
  1399. struct gendisk *disk = whole->bd_disk;
  1400. /* finish claiming */
  1401. mutex_lock(&bdev->bd_mutex);
  1402. spin_lock(&bdev_lock);
  1403. if (!res) {
  1404. BUG_ON(!bd_may_claim(bdev, whole, holder));
  1405. /*
  1406. * Note that for a whole device bd_holders
  1407. * will be incremented twice, and bd_holder
  1408. * will be set to bd_may_claim before being
  1409. * set to holder
  1410. */
  1411. whole->bd_holders++;
  1412. whole->bd_holder = bd_may_claim;
  1413. bdev->bd_holders++;
  1414. bdev->bd_holder = holder;
  1415. }
  1416. /* tell others that we're done */
  1417. BUG_ON(whole->bd_claiming != holder);
  1418. whole->bd_claiming = NULL;
  1419. wake_up_bit(&whole->bd_claiming, 0);
  1420. spin_unlock(&bdev_lock);
  1421. /*
  1422. * Block event polling for write claims if requested. Any
  1423. * write holder makes the write_holder state stick until
  1424. * all are released. This is good enough and tracking
  1425. * individual writeable reference is too fragile given the
  1426. * way @mode is used in blkdev_get/put().
  1427. */
  1428. if (!res && (mode & FMODE_WRITE) && !bdev->bd_write_holder &&
  1429. (disk->flags & GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE)) {
  1430. bdev->bd_write_holder = true;
  1431. disk_block_events(disk);
  1432. }
  1433. mutex_unlock(&bdev->bd_mutex);
  1434. bdput(whole);
  1435. }
  1436. return res;
  1437. }
  1438. EXPORT_SYMBOL(blkdev_get);
  1439. /**
  1440. * blkdev_get_by_path - open a block device by name
  1441. * @path: path to the block device to open
  1442. * @mode: FMODE_* mask
  1443. * @holder: exclusive holder identifier
  1444. *
  1445. * Open the blockdevice described by the device file at @path. @mode
  1446. * and @holder are identical to blkdev_get().
  1447. *
  1448. * On success, the returned block_device has reference count of one.
  1449. *
  1450. * CONTEXT:
  1451. * Might sleep.
  1452. *
  1453. * RETURNS:
  1454. * Pointer to block_device on success, ERR_PTR(-errno) on failure.
  1455. */
  1456. struct block_device *blkdev_get_by_path(const char *path, fmode_t mode,
  1457. void *holder)
  1458. {
  1459. struct block_device *bdev;
  1460. int err;
  1461. bdev = lookup_bdev(path);
  1462. if (IS_ERR(bdev))
  1463. return bdev;
  1464. err = blkdev_get(bdev, mode, holder);
  1465. if (err)
  1466. return ERR_PTR(err);
  1467. if ((mode & FMODE_WRITE) && bdev_read_only(bdev)) {
  1468. blkdev_put(bdev, mode);
  1469. return ERR_PTR(-EACCES);
  1470. }
  1471. return bdev;
  1472. }
  1473. EXPORT_SYMBOL(blkdev_get_by_path);
  1474. /**
  1475. * blkdev_get_by_dev - open a block device by device number
  1476. * @dev: device number of block device to open
  1477. * @mode: FMODE_* mask
  1478. * @holder: exclusive holder identifier
  1479. *
  1480. * Open the blockdevice described by device number @dev. @mode and
  1481. * @holder are identical to blkdev_get().
  1482. *
  1483. * Use it ONLY if you really do not have anything better - i.e. when
  1484. * you are behind a truly sucky interface and all you are given is a
  1485. * device number. _Never_ to be used for internal purposes. If you
  1486. * ever need it - reconsider your API.
  1487. *
  1488. * On success, the returned block_device has reference count of one.
  1489. *
  1490. * CONTEXT:
  1491. * Might sleep.
  1492. *
  1493. * RETURNS:
  1494. * Pointer to block_device on success, ERR_PTR(-errno) on failure.
  1495. */
  1496. struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder)
  1497. {
  1498. struct block_device *bdev;
  1499. int err;
  1500. bdev = bdget(dev);
  1501. if (!bdev)
  1502. return ERR_PTR(-ENOMEM);
  1503. err = blkdev_get(bdev, mode, holder);
  1504. if (err)
  1505. return ERR_PTR(err);
  1506. return bdev;
  1507. }
  1508. EXPORT_SYMBOL(blkdev_get_by_dev);
  1509. static int blkdev_open(struct inode * inode, struct file * filp)
  1510. {
  1511. struct block_device *bdev;
  1512. /*
  1513. * Preserve backwards compatibility and allow large file access
  1514. * even if userspace doesn't ask for it explicitly. Some mkfs
  1515. * binary needs it. We might want to drop this workaround
  1516. * during an unstable branch.
  1517. */
  1518. filp->f_flags |= O_LARGEFILE;
  1519. filp->f_mode |= FMODE_NOWAIT;
  1520. if (filp->f_flags & O_NDELAY)
  1521. filp->f_mode |= FMODE_NDELAY;
  1522. if (filp->f_flags & O_EXCL)
  1523. filp->f_mode |= FMODE_EXCL;
  1524. if ((filp->f_flags & O_ACCMODE) == 3)
  1525. filp->f_mode |= FMODE_WRITE_IOCTL;
  1526. bdev = bd_acquire(inode);
  1527. if (bdev == NULL)
  1528. return -ENOMEM;
  1529. filp->f_mapping = bdev->bd_inode->i_mapping;
  1530. filp->f_wb_err = filemap_sample_wb_err(filp->f_mapping);
  1531. return blkdev_get(bdev, filp->f_mode, filp);
  1532. }
  1533. static void __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part)
  1534. {
  1535. struct gendisk *disk = bdev->bd_disk;
  1536. struct block_device *victim = NULL;
  1537. mutex_lock_nested(&bdev->bd_mutex, for_part);
  1538. if (for_part)
  1539. bdev->bd_part_count--;
  1540. if (!--bdev->bd_openers) {
  1541. WARN_ON_ONCE(bdev->bd_holders);
  1542. sync_blockdev(bdev);
  1543. kill_bdev(bdev);
  1544. bdev_write_inode(bdev);
  1545. }
  1546. if (bdev->bd_contains == bdev) {
  1547. if (disk->fops->release)
  1548. disk->fops->release(disk, mode);
  1549. }
  1550. if (!bdev->bd_openers) {
  1551. struct module *owner = disk->fops->owner;
  1552. disk_put_part(bdev->bd_part);
  1553. bdev->bd_part = NULL;
  1554. bdev->bd_disk = NULL;
  1555. if (bdev != bdev->bd_contains)
  1556. victim = bdev->bd_contains;
  1557. bdev->bd_contains = NULL;
  1558. put_disk(disk);
  1559. module_put(owner);
  1560. }
  1561. mutex_unlock(&bdev->bd_mutex);
  1562. bdput(bdev);
  1563. if (victim)
  1564. __blkdev_put(victim, mode, 1);
  1565. }
  1566. void blkdev_put(struct block_device *bdev, fmode_t mode)
  1567. {
  1568. mutex_lock(&bdev->bd_mutex);
  1569. if (mode & FMODE_EXCL) {
  1570. bool bdev_free;
  1571. /*
  1572. * Release a claim on the device. The holder fields
  1573. * are protected with bdev_lock. bd_mutex is to
  1574. * synchronize disk_holder unlinking.
  1575. */
  1576. spin_lock(&bdev_lock);
  1577. WARN_ON_ONCE(--bdev->bd_holders < 0);
  1578. WARN_ON_ONCE(--bdev->bd_contains->bd_holders < 0);
  1579. /* bd_contains might point to self, check in a separate step */
  1580. if ((bdev_free = !bdev->bd_holders))
  1581. bdev->bd_holder = NULL;
  1582. if (!bdev->bd_contains->bd_holders)
  1583. bdev->bd_contains->bd_holder = NULL;
  1584. spin_unlock(&bdev_lock);
  1585. /*
  1586. * If this was the last claim, remove holder link and
  1587. * unblock evpoll if it was a write holder.
  1588. */
  1589. if (bdev_free && bdev->bd_write_holder) {
  1590. disk_unblock_events(bdev->bd_disk);
  1591. bdev->bd_write_holder = false;
  1592. }
  1593. }
  1594. /*
  1595. * Trigger event checking and tell drivers to flush MEDIA_CHANGE
  1596. * event. This is to ensure detection of media removal commanded
  1597. * from userland - e.g. eject(1).
  1598. */
  1599. disk_flush_events(bdev->bd_disk, DISK_EVENT_MEDIA_CHANGE);
  1600. mutex_unlock(&bdev->bd_mutex);
  1601. __blkdev_put(bdev, mode, 0);
  1602. }
  1603. EXPORT_SYMBOL(blkdev_put);
  1604. static int blkdev_close(struct inode * inode, struct file * filp)
  1605. {
  1606. struct block_device *bdev = I_BDEV(bdev_file_inode(filp));
  1607. blkdev_put(bdev, filp->f_mode);
  1608. return 0;
  1609. }
  1610. static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg)
  1611. {
  1612. struct block_device *bdev = I_BDEV(bdev_file_inode(file));
  1613. fmode_t mode = file->f_mode;
  1614. /*
  1615. * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
  1616. * to updated it before every ioctl.
  1617. */
  1618. if (file->f_flags & O_NDELAY)
  1619. mode |= FMODE_NDELAY;
  1620. else
  1621. mode &= ~FMODE_NDELAY;
  1622. return blkdev_ioctl(bdev, mode, cmd, arg);
  1623. }
  1624. /*
  1625. * Write data to the block device. Only intended for the block device itself
  1626. * and the raw driver which basically is a fake block device.
  1627. *
  1628. * Does not take i_mutex for the write and thus is not for general purpose
  1629. * use.
  1630. */
  1631. ssize_t blkdev_write_iter(struct kiocb *iocb, struct iov_iter *from)
  1632. {
  1633. struct file *file = iocb->ki_filp;
  1634. struct inode *bd_inode = bdev_file_inode(file);
  1635. loff_t size = i_size_read(bd_inode);
  1636. struct blk_plug plug;
  1637. ssize_t ret;
  1638. if (bdev_read_only(I_BDEV(bd_inode)))
  1639. return -EPERM;
  1640. if (!iov_iter_count(from))
  1641. return 0;
  1642. if (iocb->ki_pos >= size)
  1643. return -ENOSPC;
  1644. if ((iocb->ki_flags & (IOCB_NOWAIT | IOCB_DIRECT)) == IOCB_NOWAIT)
  1645. return -EOPNOTSUPP;
  1646. iov_iter_truncate(from, size - iocb->ki_pos);
  1647. blk_start_plug(&plug);
  1648. ret = __generic_file_write_iter(iocb, from);
  1649. if (ret > 0)
  1650. ret = generic_write_sync(iocb, ret);
  1651. blk_finish_plug(&plug);
  1652. return ret;
  1653. }
  1654. EXPORT_SYMBOL_GPL(blkdev_write_iter);
  1655. ssize_t blkdev_read_iter(struct kiocb *iocb, struct iov_iter *to)
  1656. {
  1657. struct file *file = iocb->ki_filp;
  1658. struct inode *bd_inode = bdev_file_inode(file);
  1659. loff_t size = i_size_read(bd_inode);
  1660. loff_t pos = iocb->ki_pos;
  1661. if (pos >= size)
  1662. return 0;
  1663. size -= pos;
  1664. iov_iter_truncate(to, size);
  1665. return generic_file_read_iter(iocb, to);
  1666. }
  1667. EXPORT_SYMBOL_GPL(blkdev_read_iter);
  1668. /*
  1669. * Try to release a page associated with block device when the system
  1670. * is under memory pressure.
  1671. */
  1672. static int blkdev_releasepage(struct page *page, gfp_t wait)
  1673. {
  1674. struct super_block *super = BDEV_I(page->mapping->host)->bdev.bd_super;
  1675. if (super && super->s_op->bdev_try_to_free_page)
  1676. return super->s_op->bdev_try_to_free_page(super, page, wait);
  1677. return try_to_free_buffers(page);
  1678. }
  1679. static int blkdev_writepages(struct address_space *mapping,
  1680. struct writeback_control *wbc)
  1681. {
  1682. if (dax_mapping(mapping)) {
  1683. struct block_device *bdev = I_BDEV(mapping->host);
  1684. return dax_writeback_mapping_range(mapping, bdev, wbc);
  1685. }
  1686. return generic_writepages(mapping, wbc);
  1687. }
  1688. static const struct address_space_operations def_blk_aops = {
  1689. .readpage = blkdev_readpage,
  1690. .readpages = blkdev_readpages,
  1691. .writepage = blkdev_writepage,
  1692. .write_begin = blkdev_write_begin,
  1693. .write_end = blkdev_write_end,
  1694. .writepages = blkdev_writepages,
  1695. .releasepage = blkdev_releasepage,
  1696. .direct_IO = blkdev_direct_IO,
  1697. .is_dirty_writeback = buffer_check_dirty_writeback,
  1698. };
  1699. #define BLKDEV_FALLOC_FL_SUPPORTED \
  1700. (FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | \
  1701. FALLOC_FL_ZERO_RANGE | FALLOC_FL_NO_HIDE_STALE)
  1702. static long blkdev_fallocate(struct file *file, int mode, loff_t start,
  1703. loff_t len)
  1704. {
  1705. struct block_device *bdev = I_BDEV(bdev_file_inode(file));
  1706. struct address_space *mapping;
  1707. loff_t end = start + len - 1;
  1708. loff_t isize;
  1709. int error;
  1710. /* Fail if we don't recognize the flags. */
  1711. if (mode & ~BLKDEV_FALLOC_FL_SUPPORTED)
  1712. return -EOPNOTSUPP;
  1713. /* Don't go off the end of the device. */
  1714. isize = i_size_read(bdev->bd_inode);
  1715. if (start >= isize)
  1716. return -EINVAL;
  1717. if (end >= isize) {
  1718. if (mode & FALLOC_FL_KEEP_SIZE) {
  1719. len = isize - start;
  1720. end = start + len - 1;
  1721. } else
  1722. return -EINVAL;
  1723. }
  1724. /*
  1725. * Don't allow IO that isn't aligned to logical block size.
  1726. */
  1727. if ((start | len) & (bdev_logical_block_size(bdev) - 1))
  1728. return -EINVAL;
  1729. /* Invalidate the page cache, including dirty pages. */
  1730. mapping = bdev->bd_inode->i_mapping;
  1731. truncate_inode_pages_range(mapping, start, end);
  1732. switch (mode) {
  1733. case FALLOC_FL_ZERO_RANGE:
  1734. case FALLOC_FL_ZERO_RANGE | FALLOC_FL_KEEP_SIZE:
  1735. error = blkdev_issue_zeroout(bdev, start >> 9, len >> 9,
  1736. GFP_KERNEL, BLKDEV_ZERO_NOUNMAP);
  1737. break;
  1738. case FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE:
  1739. error = blkdev_issue_zeroout(bdev, start >> 9, len >> 9,
  1740. GFP_KERNEL, BLKDEV_ZERO_NOFALLBACK);
  1741. break;
  1742. case FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE | FALLOC_FL_NO_HIDE_STALE:
  1743. error = blkdev_issue_discard(bdev, start >> 9, len >> 9,
  1744. GFP_KERNEL, 0);
  1745. break;
  1746. default:
  1747. return -EOPNOTSUPP;
  1748. }
  1749. if (error)
  1750. return error;
  1751. /*
  1752. * Invalidate again; if someone wandered in and dirtied a page,
  1753. * the caller will be given -EBUSY. The third argument is
  1754. * inclusive, so the rounding here is safe.
  1755. */
  1756. return invalidate_inode_pages2_range(mapping,
  1757. start >> PAGE_SHIFT,
  1758. end >> PAGE_SHIFT);
  1759. }
  1760. const struct file_operations def_blk_fops = {
  1761. .open = blkdev_open,
  1762. .release = blkdev_close,
  1763. .llseek = block_llseek,
  1764. .read_iter = blkdev_read_iter,
  1765. .write_iter = blkdev_write_iter,
  1766. .mmap = generic_file_mmap,
  1767. .fsync = blkdev_fsync,
  1768. .unlocked_ioctl = block_ioctl,
  1769. #ifdef CONFIG_COMPAT
  1770. .compat_ioctl = compat_blkdev_ioctl,
  1771. #endif
  1772. .splice_read = generic_file_splice_read,
  1773. .splice_write = iter_file_splice_write,
  1774. .fallocate = blkdev_fallocate,
  1775. };
  1776. int ioctl_by_bdev(struct block_device *bdev, unsigned cmd, unsigned long arg)
  1777. {
  1778. int res;
  1779. mm_segment_t old_fs = get_fs();
  1780. set_fs(KERNEL_DS);
  1781. res = blkdev_ioctl(bdev, 0, cmd, arg);
  1782. set_fs(old_fs);
  1783. return res;
  1784. }
  1785. EXPORT_SYMBOL(ioctl_by_bdev);
  1786. /**
  1787. * lookup_bdev - lookup a struct block_device by name
  1788. * @pathname: special file representing the block device
  1789. *
  1790. * Get a reference to the blockdevice at @pathname in the current
  1791. * namespace if possible and return it. Return ERR_PTR(error)
  1792. * otherwise.
  1793. */
  1794. struct block_device *lookup_bdev(const char *pathname)
  1795. {
  1796. struct block_device *bdev;
  1797. struct inode *inode;
  1798. struct path path;
  1799. int error;
  1800. if (!pathname || !*pathname)
  1801. return ERR_PTR(-EINVAL);
  1802. error = kern_path(pathname, LOOKUP_FOLLOW, &path);
  1803. if (error)
  1804. return ERR_PTR(error);
  1805. inode = d_backing_inode(path.dentry);
  1806. error = -ENOTBLK;
  1807. if (!S_ISBLK(inode->i_mode))
  1808. goto fail;
  1809. error = -EACCES;
  1810. if (!may_open_dev(&path))
  1811. goto fail;
  1812. error = -ENOMEM;
  1813. bdev = bd_acquire(inode);
  1814. if (!bdev)
  1815. goto fail;
  1816. out:
  1817. path_put(&path);
  1818. return bdev;
  1819. fail:
  1820. bdev = ERR_PTR(error);
  1821. goto out;
  1822. }
  1823. EXPORT_SYMBOL(lookup_bdev);
  1824. int __invalidate_device(struct block_device *bdev, bool kill_dirty)
  1825. {
  1826. struct super_block *sb = get_super(bdev);
  1827. int res = 0;
  1828. if (sb) {
  1829. /*
  1830. * no need to lock the super, get_super holds the
  1831. * read mutex so the filesystem cannot go away
  1832. * under us (->put_super runs with the write lock
  1833. * hold).
  1834. */
  1835. shrink_dcache_sb(sb);
  1836. res = invalidate_inodes(sb, kill_dirty);
  1837. drop_super(sb);
  1838. }
  1839. invalidate_bdev(bdev);
  1840. return res;
  1841. }
  1842. EXPORT_SYMBOL(__invalidate_device);
  1843. void iterate_bdevs(void (*func)(struct block_device *, void *), void *arg)
  1844. {
  1845. struct inode *inode, *old_inode = NULL;
  1846. spin_lock(&blockdev_superblock->s_inode_list_lock);
  1847. list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list) {
  1848. struct address_space *mapping = inode->i_mapping;
  1849. struct block_device *bdev;
  1850. spin_lock(&inode->i_lock);
  1851. if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW) ||
  1852. mapping->nrpages == 0) {
  1853. spin_unlock(&inode->i_lock);
  1854. continue;
  1855. }
  1856. __iget(inode);
  1857. spin_unlock(&inode->i_lock);
  1858. spin_unlock(&blockdev_superblock->s_inode_list_lock);
  1859. /*
  1860. * We hold a reference to 'inode' so it couldn't have been
  1861. * removed from s_inodes list while we dropped the
  1862. * s_inode_list_lock We cannot iput the inode now as we can
  1863. * be holding the last reference and we cannot iput it under
  1864. * s_inode_list_lock. So we keep the reference and iput it
  1865. * later.
  1866. */
  1867. iput(old_inode);
  1868. old_inode = inode;
  1869. bdev = I_BDEV(inode);
  1870. mutex_lock(&bdev->bd_mutex);
  1871. if (bdev->bd_openers)
  1872. func(bdev, arg);
  1873. mutex_unlock(&bdev->bd_mutex);
  1874. spin_lock(&blockdev_superblock->s_inode_list_lock);
  1875. }
  1876. spin_unlock(&blockdev_superblock->s_inode_list_lock);
  1877. iput(old_inode);
  1878. }