block_dev.c 55 KB

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