block_dev.c 54 KB

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