block_dev.c 53 KB

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