block_dev.c 54 KB

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