block_dev.c 53 KB

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