block_dev.c 54 KB

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