block_dev.c 55 KB

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