block_dev.c 56 KB

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