block_dev.c 51 KB

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