block_dev.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994
  1. /*
  2. * linux/fs/block_dev.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. * Copyright (C) 2001 Andrea Arcangeli <andrea@suse.de> SuSE
  6. */
  7. #include <linux/init.h>
  8. #include <linux/mm.h>
  9. #include <linux/fcntl.h>
  10. #include <linux/slab.h>
  11. #include <linux/kmod.h>
  12. #include <linux/major.h>
  13. #include <linux/device_cgroup.h>
  14. #include <linux/highmem.h>
  15. #include <linux/blkdev.h>
  16. #include <linux/backing-dev.h>
  17. #include <linux/module.h>
  18. #include <linux/blkpg.h>
  19. #include <linux/magic.h>
  20. #include <linux/buffer_head.h>
  21. #include <linux/swap.h>
  22. #include <linux/pagevec.h>
  23. #include <linux/writeback.h>
  24. #include <linux/mpage.h>
  25. #include <linux/mount.h>
  26. #include <linux/uio.h>
  27. #include <linux/namei.h>
  28. #include <linux/log2.h>
  29. #include <linux/cleancache.h>
  30. #include <linux/dax.h>
  31. #include <linux/badblocks.h>
  32. #include <linux/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. inode_init_once(&ei->vfs_inode);
  554. /* Initialize mutex for freeze. */
  555. mutex_init(&bdev->bd_fsfreeze_mutex);
  556. }
  557. static void bdev_evict_inode(struct inode *inode)
  558. {
  559. struct block_device *bdev = &BDEV_I(inode)->bdev;
  560. truncate_inode_pages_final(&inode->i_data);
  561. invalidate_inode_buffers(inode); /* is it needed here? */
  562. clear_inode(inode);
  563. spin_lock(&bdev_lock);
  564. list_del_init(&bdev->bd_list);
  565. spin_unlock(&bdev_lock);
  566. }
  567. static const struct super_operations bdev_sops = {
  568. .statfs = simple_statfs,
  569. .alloc_inode = bdev_alloc_inode,
  570. .destroy_inode = bdev_destroy_inode,
  571. .drop_inode = generic_delete_inode,
  572. .evict_inode = bdev_evict_inode,
  573. };
  574. static struct dentry *bd_mount(struct file_system_type *fs_type,
  575. int flags, const char *dev_name, void *data)
  576. {
  577. struct dentry *dent;
  578. dent = mount_pseudo(fs_type, "bdev:", &bdev_sops, NULL, BDEVFS_MAGIC);
  579. if (!IS_ERR(dent))
  580. dent->d_sb->s_iflags |= SB_I_CGROUPWB;
  581. return dent;
  582. }
  583. static struct file_system_type bd_type = {
  584. .name = "bdev",
  585. .mount = bd_mount,
  586. .kill_sb = kill_anon_super,
  587. };
  588. struct super_block *blockdev_superblock __read_mostly;
  589. EXPORT_SYMBOL_GPL(blockdev_superblock);
  590. void __init bdev_cache_init(void)
  591. {
  592. int err;
  593. static struct vfsmount *bd_mnt;
  594. bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
  595. 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
  596. SLAB_MEM_SPREAD|SLAB_ACCOUNT|SLAB_PANIC),
  597. init_once);
  598. err = register_filesystem(&bd_type);
  599. if (err)
  600. panic("Cannot register bdev pseudo-fs");
  601. bd_mnt = kern_mount(&bd_type);
  602. if (IS_ERR(bd_mnt))
  603. panic("Cannot create bdev pseudo-fs");
  604. blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */
  605. }
  606. /*
  607. * Most likely _very_ bad one - but then it's hardly critical for small
  608. * /dev and can be fixed when somebody will need really large one.
  609. * Keep in mind that it will be fed through icache hash function too.
  610. */
  611. static inline unsigned long hash(dev_t dev)
  612. {
  613. return MAJOR(dev)+MINOR(dev);
  614. }
  615. static int bdev_test(struct inode *inode, void *data)
  616. {
  617. return BDEV_I(inode)->bdev.bd_dev == *(dev_t *)data;
  618. }
  619. static int bdev_set(struct inode *inode, void *data)
  620. {
  621. BDEV_I(inode)->bdev.bd_dev = *(dev_t *)data;
  622. return 0;
  623. }
  624. static LIST_HEAD(all_bdevs);
  625. struct block_device *bdget(dev_t dev)
  626. {
  627. struct block_device *bdev;
  628. struct inode *inode;
  629. inode = iget5_locked(blockdev_superblock, hash(dev),
  630. bdev_test, bdev_set, &dev);
  631. if (!inode)
  632. return NULL;
  633. bdev = &BDEV_I(inode)->bdev;
  634. if (inode->i_state & I_NEW) {
  635. bdev->bd_contains = NULL;
  636. bdev->bd_super = NULL;
  637. bdev->bd_inode = inode;
  638. bdev->bd_block_size = i_blocksize(inode);
  639. bdev->bd_part_count = 0;
  640. bdev->bd_invalidated = 0;
  641. inode->i_mode = S_IFBLK;
  642. inode->i_rdev = dev;
  643. inode->i_bdev = bdev;
  644. inode->i_data.a_ops = &def_blk_aops;
  645. mapping_set_gfp_mask(&inode->i_data, GFP_USER);
  646. spin_lock(&bdev_lock);
  647. list_add(&bdev->bd_list, &all_bdevs);
  648. spin_unlock(&bdev_lock);
  649. unlock_new_inode(inode);
  650. }
  651. return bdev;
  652. }
  653. EXPORT_SYMBOL(bdget);
  654. /**
  655. * bdgrab -- Grab a reference to an already referenced block device
  656. * @bdev: Block device to grab a reference to.
  657. */
  658. struct block_device *bdgrab(struct block_device *bdev)
  659. {
  660. ihold(bdev->bd_inode);
  661. return bdev;
  662. }
  663. EXPORT_SYMBOL(bdgrab);
  664. long nr_blockdev_pages(void)
  665. {
  666. struct block_device *bdev;
  667. long ret = 0;
  668. spin_lock(&bdev_lock);
  669. list_for_each_entry(bdev, &all_bdevs, bd_list) {
  670. ret += bdev->bd_inode->i_mapping->nrpages;
  671. }
  672. spin_unlock(&bdev_lock);
  673. return ret;
  674. }
  675. void bdput(struct block_device *bdev)
  676. {
  677. iput(bdev->bd_inode);
  678. }
  679. EXPORT_SYMBOL(bdput);
  680. static struct block_device *bd_acquire(struct inode *inode)
  681. {
  682. struct block_device *bdev;
  683. spin_lock(&bdev_lock);
  684. bdev = inode->i_bdev;
  685. if (bdev) {
  686. bdgrab(bdev);
  687. spin_unlock(&bdev_lock);
  688. return bdev;
  689. }
  690. spin_unlock(&bdev_lock);
  691. bdev = bdget(inode->i_rdev);
  692. if (bdev) {
  693. spin_lock(&bdev_lock);
  694. if (!inode->i_bdev) {
  695. /*
  696. * We take an additional reference to bd_inode,
  697. * and it's released in clear_inode() of inode.
  698. * So, we can access it via ->i_mapping always
  699. * without igrab().
  700. */
  701. bdgrab(bdev);
  702. inode->i_bdev = bdev;
  703. inode->i_mapping = bdev->bd_inode->i_mapping;
  704. }
  705. spin_unlock(&bdev_lock);
  706. }
  707. return bdev;
  708. }
  709. /* Call when you free inode */
  710. void bd_forget(struct inode *inode)
  711. {
  712. struct block_device *bdev = NULL;
  713. spin_lock(&bdev_lock);
  714. if (!sb_is_blkdev_sb(inode->i_sb))
  715. bdev = inode->i_bdev;
  716. inode->i_bdev = NULL;
  717. inode->i_mapping = &inode->i_data;
  718. spin_unlock(&bdev_lock);
  719. if (bdev)
  720. bdput(bdev);
  721. }
  722. /**
  723. * bd_may_claim - test whether a block device can be claimed
  724. * @bdev: block device of interest
  725. * @whole: whole block device containing @bdev, may equal @bdev
  726. * @holder: holder trying to claim @bdev
  727. *
  728. * Test whether @bdev can be claimed by @holder.
  729. *
  730. * CONTEXT:
  731. * spin_lock(&bdev_lock).
  732. *
  733. * RETURNS:
  734. * %true if @bdev can be claimed, %false otherwise.
  735. */
  736. static bool bd_may_claim(struct block_device *bdev, struct block_device *whole,
  737. void *holder)
  738. {
  739. if (bdev->bd_holder == holder)
  740. return true; /* already a holder */
  741. else if (bdev->bd_holder != NULL)
  742. return false; /* held by someone else */
  743. else if (whole == bdev)
  744. return true; /* is a whole device which isn't held */
  745. else if (whole->bd_holder == bd_may_claim)
  746. return true; /* is a partition of a device that is being partitioned */
  747. else if (whole->bd_holder != NULL)
  748. return false; /* is a partition of a held device */
  749. else
  750. return true; /* is a partition of an un-held device */
  751. }
  752. /**
  753. * bd_prepare_to_claim - prepare to claim a block device
  754. * @bdev: block device of interest
  755. * @whole: the whole device containing @bdev, may equal @bdev
  756. * @holder: holder trying to claim @bdev
  757. *
  758. * Prepare to claim @bdev. This function fails if @bdev is already
  759. * claimed by another holder and waits if another claiming is in
  760. * progress. This function doesn't actually claim. On successful
  761. * return, the caller has ownership of bd_claiming and bd_holder[s].
  762. *
  763. * CONTEXT:
  764. * spin_lock(&bdev_lock). Might release bdev_lock, sleep and regrab
  765. * it multiple times.
  766. *
  767. * RETURNS:
  768. * 0 if @bdev can be claimed, -EBUSY otherwise.
  769. */
  770. static int bd_prepare_to_claim(struct block_device *bdev,
  771. struct block_device *whole, void *holder)
  772. {
  773. retry:
  774. /* if someone else claimed, fail */
  775. if (!bd_may_claim(bdev, whole, holder))
  776. return -EBUSY;
  777. /* if claiming is already in progress, wait for it to finish */
  778. if (whole->bd_claiming) {
  779. wait_queue_head_t *wq = bit_waitqueue(&whole->bd_claiming, 0);
  780. DEFINE_WAIT(wait);
  781. prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
  782. spin_unlock(&bdev_lock);
  783. schedule();
  784. finish_wait(wq, &wait);
  785. spin_lock(&bdev_lock);
  786. goto retry;
  787. }
  788. /* yay, all mine */
  789. return 0;
  790. }
  791. /**
  792. * bd_start_claiming - start claiming a block device
  793. * @bdev: block device of interest
  794. * @holder: holder trying to claim @bdev
  795. *
  796. * @bdev is about to be opened exclusively. Check @bdev can be opened
  797. * exclusively and mark that an exclusive open is in progress. Each
  798. * successful call to this function must be matched with a call to
  799. * either bd_finish_claiming() or bd_abort_claiming() (which do not
  800. * fail).
  801. *
  802. * This function is used to gain exclusive access to the block device
  803. * without actually causing other exclusive open attempts to fail. It
  804. * should be used when the open sequence itself requires exclusive
  805. * access but may subsequently fail.
  806. *
  807. * CONTEXT:
  808. * Might sleep.
  809. *
  810. * RETURNS:
  811. * Pointer to the block device containing @bdev on success, ERR_PTR()
  812. * value on failure.
  813. */
  814. static struct block_device *bd_start_claiming(struct block_device *bdev,
  815. void *holder)
  816. {
  817. struct gendisk *disk;
  818. struct block_device *whole;
  819. int partno, err;
  820. might_sleep();
  821. /*
  822. * @bdev might not have been initialized properly yet, look up
  823. * and grab the outer block device the hard way.
  824. */
  825. disk = get_gendisk(bdev->bd_dev, &partno);
  826. if (!disk)
  827. return ERR_PTR(-ENXIO);
  828. /*
  829. * Normally, @bdev should equal what's returned from bdget_disk()
  830. * if partno is 0; however, some drivers (floppy) use multiple
  831. * bdev's for the same physical device and @bdev may be one of the
  832. * aliases. Keep @bdev if partno is 0. This means claimer
  833. * tracking is broken for those devices but it has always been that
  834. * way.
  835. */
  836. if (partno)
  837. whole = bdget_disk(disk, 0);
  838. else
  839. whole = bdgrab(bdev);
  840. module_put(disk->fops->owner);
  841. put_disk(disk);
  842. if (!whole)
  843. return ERR_PTR(-ENOMEM);
  844. /* prepare to claim, if successful, mark claiming in progress */
  845. spin_lock(&bdev_lock);
  846. err = bd_prepare_to_claim(bdev, whole, holder);
  847. if (err == 0) {
  848. whole->bd_claiming = holder;
  849. spin_unlock(&bdev_lock);
  850. return whole;
  851. } else {
  852. spin_unlock(&bdev_lock);
  853. bdput(whole);
  854. return ERR_PTR(err);
  855. }
  856. }
  857. #ifdef CONFIG_SYSFS
  858. struct bd_holder_disk {
  859. struct list_head list;
  860. struct gendisk *disk;
  861. int refcnt;
  862. };
  863. static struct bd_holder_disk *bd_find_holder_disk(struct block_device *bdev,
  864. struct gendisk *disk)
  865. {
  866. struct bd_holder_disk *holder;
  867. list_for_each_entry(holder, &bdev->bd_holder_disks, list)
  868. if (holder->disk == disk)
  869. return holder;
  870. return NULL;
  871. }
  872. static int add_symlink(struct kobject *from, struct kobject *to)
  873. {
  874. return sysfs_create_link(from, to, kobject_name(to));
  875. }
  876. static void del_symlink(struct kobject *from, struct kobject *to)
  877. {
  878. sysfs_remove_link(from, kobject_name(to));
  879. }
  880. /**
  881. * bd_link_disk_holder - create symlinks between holding disk and slave bdev
  882. * @bdev: the claimed slave bdev
  883. * @disk: the holding disk
  884. *
  885. * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
  886. *
  887. * This functions creates the following sysfs symlinks.
  888. *
  889. * - from "slaves" directory of the holder @disk to the claimed @bdev
  890. * - from "holders" directory of the @bdev to the holder @disk
  891. *
  892. * For example, if /dev/dm-0 maps to /dev/sda and disk for dm-0 is
  893. * passed to bd_link_disk_holder(), then:
  894. *
  895. * /sys/block/dm-0/slaves/sda --> /sys/block/sda
  896. * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
  897. *
  898. * The caller must have claimed @bdev before calling this function and
  899. * ensure that both @bdev and @disk are valid during the creation and
  900. * lifetime of these symlinks.
  901. *
  902. * CONTEXT:
  903. * Might sleep.
  904. *
  905. * RETURNS:
  906. * 0 on success, -errno on failure.
  907. */
  908. int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk)
  909. {
  910. struct bd_holder_disk *holder;
  911. int ret = 0;
  912. mutex_lock(&bdev->bd_mutex);
  913. WARN_ON_ONCE(!bdev->bd_holder);
  914. /* FIXME: remove the following once add_disk() handles errors */
  915. if (WARN_ON(!disk->slave_dir || !bdev->bd_part->holder_dir))
  916. goto out_unlock;
  917. holder = bd_find_holder_disk(bdev, disk);
  918. if (holder) {
  919. holder->refcnt++;
  920. goto out_unlock;
  921. }
  922. holder = kzalloc(sizeof(*holder), GFP_KERNEL);
  923. if (!holder) {
  924. ret = -ENOMEM;
  925. goto out_unlock;
  926. }
  927. INIT_LIST_HEAD(&holder->list);
  928. holder->disk = disk;
  929. holder->refcnt = 1;
  930. ret = add_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
  931. if (ret)
  932. goto out_free;
  933. ret = add_symlink(bdev->bd_part->holder_dir, &disk_to_dev(disk)->kobj);
  934. if (ret)
  935. goto out_del;
  936. /*
  937. * bdev could be deleted beneath us which would implicitly destroy
  938. * the holder directory. Hold on to it.
  939. */
  940. kobject_get(bdev->bd_part->holder_dir);
  941. list_add(&holder->list, &bdev->bd_holder_disks);
  942. goto out_unlock;
  943. out_del:
  944. del_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
  945. out_free:
  946. kfree(holder);
  947. out_unlock:
  948. mutex_unlock(&bdev->bd_mutex);
  949. return ret;
  950. }
  951. EXPORT_SYMBOL_GPL(bd_link_disk_holder);
  952. /**
  953. * bd_unlink_disk_holder - destroy symlinks created by bd_link_disk_holder()
  954. * @bdev: the calimed slave bdev
  955. * @disk: the holding disk
  956. *
  957. * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
  958. *
  959. * CONTEXT:
  960. * Might sleep.
  961. */
  962. void bd_unlink_disk_holder(struct block_device *bdev, struct gendisk *disk)
  963. {
  964. struct bd_holder_disk *holder;
  965. mutex_lock(&bdev->bd_mutex);
  966. holder = bd_find_holder_disk(bdev, disk);
  967. if (!WARN_ON_ONCE(holder == NULL) && !--holder->refcnt) {
  968. del_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
  969. del_symlink(bdev->bd_part->holder_dir,
  970. &disk_to_dev(disk)->kobj);
  971. kobject_put(bdev->bd_part->holder_dir);
  972. list_del_init(&holder->list);
  973. kfree(holder);
  974. }
  975. mutex_unlock(&bdev->bd_mutex);
  976. }
  977. EXPORT_SYMBOL_GPL(bd_unlink_disk_holder);
  978. #endif
  979. /**
  980. * flush_disk - invalidates all buffer-cache entries on a disk
  981. *
  982. * @bdev: struct block device to be flushed
  983. * @kill_dirty: flag to guide handling of dirty inodes
  984. *
  985. * Invalidates all buffer-cache entries on a disk. It should be called
  986. * when a disk has been changed -- either by a media change or online
  987. * resize.
  988. */
  989. static void flush_disk(struct block_device *bdev, bool kill_dirty)
  990. {
  991. if (__invalidate_device(bdev, kill_dirty)) {
  992. printk(KERN_WARNING "VFS: busy inodes on changed media or "
  993. "resized disk %s\n",
  994. bdev->bd_disk ? bdev->bd_disk->disk_name : "");
  995. }
  996. if (!bdev->bd_disk)
  997. return;
  998. if (disk_part_scan_enabled(bdev->bd_disk))
  999. bdev->bd_invalidated = 1;
  1000. }
  1001. /**
  1002. * check_disk_size_change - checks for disk size change and adjusts bdev size.
  1003. * @disk: struct gendisk to check
  1004. * @bdev: struct bdev to adjust.
  1005. *
  1006. * This routine checks to see if the bdev size does not match the disk size
  1007. * and adjusts it if it differs.
  1008. */
  1009. void check_disk_size_change(struct gendisk *disk, struct block_device *bdev)
  1010. {
  1011. loff_t disk_size, bdev_size;
  1012. disk_size = (loff_t)get_capacity(disk) << 9;
  1013. bdev_size = i_size_read(bdev->bd_inode);
  1014. if (disk_size != bdev_size) {
  1015. printk(KERN_INFO
  1016. "%s: detected capacity change from %lld to %lld\n",
  1017. disk->disk_name, bdev_size, disk_size);
  1018. i_size_write(bdev->bd_inode, disk_size);
  1019. flush_disk(bdev, false);
  1020. }
  1021. }
  1022. EXPORT_SYMBOL(check_disk_size_change);
  1023. /**
  1024. * revalidate_disk - wrapper for lower-level driver's revalidate_disk call-back
  1025. * @disk: struct gendisk to be revalidated
  1026. *
  1027. * This routine is a wrapper for lower-level driver's revalidate_disk
  1028. * call-backs. It is used to do common pre and post operations needed
  1029. * for all revalidate_disk operations.
  1030. */
  1031. int revalidate_disk(struct gendisk *disk)
  1032. {
  1033. struct block_device *bdev;
  1034. int ret = 0;
  1035. if (disk->fops->revalidate_disk)
  1036. ret = disk->fops->revalidate_disk(disk);
  1037. bdev = bdget_disk(disk, 0);
  1038. if (!bdev)
  1039. return ret;
  1040. mutex_lock(&bdev->bd_mutex);
  1041. check_disk_size_change(disk, bdev);
  1042. bdev->bd_invalidated = 0;
  1043. mutex_unlock(&bdev->bd_mutex);
  1044. bdput(bdev);
  1045. return ret;
  1046. }
  1047. EXPORT_SYMBOL(revalidate_disk);
  1048. /*
  1049. * This routine checks whether a removable media has been changed,
  1050. * and invalidates all buffer-cache-entries in that case. This
  1051. * is a relatively slow routine, so we have to try to minimize using
  1052. * it. Thus it is called only upon a 'mount' or 'open'. This
  1053. * is the best way of combining speed and utility, I think.
  1054. * People changing diskettes in the middle of an operation deserve
  1055. * to lose :-)
  1056. */
  1057. int check_disk_change(struct block_device *bdev)
  1058. {
  1059. struct gendisk *disk = bdev->bd_disk;
  1060. const struct block_device_operations *bdops = disk->fops;
  1061. unsigned int events;
  1062. events = disk_clear_events(disk, DISK_EVENT_MEDIA_CHANGE |
  1063. DISK_EVENT_EJECT_REQUEST);
  1064. if (!(events & DISK_EVENT_MEDIA_CHANGE))
  1065. return 0;
  1066. flush_disk(bdev, true);
  1067. if (bdops->revalidate_disk)
  1068. bdops->revalidate_disk(bdev->bd_disk);
  1069. return 1;
  1070. }
  1071. EXPORT_SYMBOL(check_disk_change);
  1072. void bd_set_size(struct block_device *bdev, loff_t size)
  1073. {
  1074. inode_lock(bdev->bd_inode);
  1075. i_size_write(bdev->bd_inode, size);
  1076. inode_unlock(bdev->bd_inode);
  1077. }
  1078. EXPORT_SYMBOL(bd_set_size);
  1079. static void __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part);
  1080. /*
  1081. * bd_mutex locking:
  1082. *
  1083. * mutex_lock(part->bd_mutex)
  1084. * mutex_lock_nested(whole->bd_mutex, 1)
  1085. */
  1086. static int __blkdev_get(struct block_device *bdev, fmode_t mode, int for_part)
  1087. {
  1088. struct gendisk *disk;
  1089. struct module *owner;
  1090. int ret;
  1091. int partno;
  1092. int perm = 0;
  1093. if (mode & FMODE_READ)
  1094. perm |= MAY_READ;
  1095. if (mode & FMODE_WRITE)
  1096. perm |= MAY_WRITE;
  1097. /*
  1098. * hooks: /n/, see "layering violations".
  1099. */
  1100. if (!for_part) {
  1101. ret = devcgroup_inode_permission(bdev->bd_inode, perm);
  1102. if (ret != 0) {
  1103. bdput(bdev);
  1104. return ret;
  1105. }
  1106. }
  1107. restart:
  1108. ret = -ENXIO;
  1109. disk = get_gendisk(bdev->bd_dev, &partno);
  1110. if (!disk)
  1111. goto out;
  1112. owner = disk->fops->owner;
  1113. disk_block_events(disk);
  1114. mutex_lock_nested(&bdev->bd_mutex, for_part);
  1115. if (!bdev->bd_openers) {
  1116. bdev->bd_disk = disk;
  1117. bdev->bd_queue = disk->queue;
  1118. bdev->bd_contains = bdev;
  1119. if (!partno) {
  1120. ret = -ENXIO;
  1121. bdev->bd_part = disk_get_part(disk, partno);
  1122. if (!bdev->bd_part)
  1123. goto out_clear;
  1124. ret = 0;
  1125. if (disk->fops->open) {
  1126. ret = disk->fops->open(bdev, mode);
  1127. if (ret == -ERESTARTSYS) {
  1128. /* Lost a race with 'disk' being
  1129. * deleted, try again.
  1130. * See md.c
  1131. */
  1132. disk_put_part(bdev->bd_part);
  1133. bdev->bd_part = NULL;
  1134. bdev->bd_disk = NULL;
  1135. bdev->bd_queue = NULL;
  1136. mutex_unlock(&bdev->bd_mutex);
  1137. disk_unblock_events(disk);
  1138. put_disk(disk);
  1139. module_put(owner);
  1140. goto restart;
  1141. }
  1142. }
  1143. if (!ret) {
  1144. bd_set_size(bdev,(loff_t)get_capacity(disk)<<9);
  1145. set_init_blocksize(bdev);
  1146. }
  1147. /*
  1148. * If the device is invalidated, rescan partition
  1149. * if open succeeded or failed with -ENOMEDIUM.
  1150. * The latter is necessary to prevent ghost
  1151. * partitions on a removed medium.
  1152. */
  1153. if (bdev->bd_invalidated) {
  1154. if (!ret)
  1155. rescan_partitions(disk, bdev);
  1156. else if (ret == -ENOMEDIUM)
  1157. invalidate_partitions(disk, bdev);
  1158. }
  1159. if (ret)
  1160. goto out_clear;
  1161. } else {
  1162. struct block_device *whole;
  1163. whole = bdget_disk(disk, 0);
  1164. ret = -ENOMEM;
  1165. if (!whole)
  1166. goto out_clear;
  1167. BUG_ON(for_part);
  1168. ret = __blkdev_get(whole, mode, 1);
  1169. if (ret)
  1170. goto out_clear;
  1171. bdev->bd_contains = whole;
  1172. bdev->bd_part = disk_get_part(disk, partno);
  1173. if (!(disk->flags & GENHD_FL_UP) ||
  1174. !bdev->bd_part || !bdev->bd_part->nr_sects) {
  1175. ret = -ENXIO;
  1176. goto out_clear;
  1177. }
  1178. bd_set_size(bdev, (loff_t)bdev->bd_part->nr_sects << 9);
  1179. set_init_blocksize(bdev);
  1180. }
  1181. } else {
  1182. if (bdev->bd_contains == bdev) {
  1183. ret = 0;
  1184. if (bdev->bd_disk->fops->open)
  1185. ret = bdev->bd_disk->fops->open(bdev, mode);
  1186. /* the same as first opener case, read comment there */
  1187. if (bdev->bd_invalidated) {
  1188. if (!ret)
  1189. rescan_partitions(bdev->bd_disk, bdev);
  1190. else if (ret == -ENOMEDIUM)
  1191. invalidate_partitions(bdev->bd_disk, bdev);
  1192. }
  1193. if (ret)
  1194. goto out_unlock_bdev;
  1195. }
  1196. /* only one opener holds refs to the module and disk */
  1197. put_disk(disk);
  1198. module_put(owner);
  1199. }
  1200. bdev->bd_openers++;
  1201. if (for_part)
  1202. bdev->bd_part_count++;
  1203. mutex_unlock(&bdev->bd_mutex);
  1204. disk_unblock_events(disk);
  1205. return 0;
  1206. out_clear:
  1207. disk_put_part(bdev->bd_part);
  1208. bdev->bd_disk = NULL;
  1209. bdev->bd_part = NULL;
  1210. bdev->bd_queue = NULL;
  1211. if (bdev != bdev->bd_contains)
  1212. __blkdev_put(bdev->bd_contains, mode, 1);
  1213. bdev->bd_contains = NULL;
  1214. out_unlock_bdev:
  1215. mutex_unlock(&bdev->bd_mutex);
  1216. disk_unblock_events(disk);
  1217. put_disk(disk);
  1218. module_put(owner);
  1219. out:
  1220. bdput(bdev);
  1221. return ret;
  1222. }
  1223. /**
  1224. * blkdev_get - open a block device
  1225. * @bdev: block_device to open
  1226. * @mode: FMODE_* mask
  1227. * @holder: exclusive holder identifier
  1228. *
  1229. * Open @bdev with @mode. If @mode includes %FMODE_EXCL, @bdev is
  1230. * open with exclusive access. Specifying %FMODE_EXCL with %NULL
  1231. * @holder is invalid. Exclusive opens may nest for the same @holder.
  1232. *
  1233. * On success, the reference count of @bdev is unchanged. On failure,
  1234. * @bdev is put.
  1235. *
  1236. * CONTEXT:
  1237. * Might sleep.
  1238. *
  1239. * RETURNS:
  1240. * 0 on success, -errno on failure.
  1241. */
  1242. int blkdev_get(struct block_device *bdev, fmode_t mode, void *holder)
  1243. {
  1244. struct block_device *whole = NULL;
  1245. int res;
  1246. WARN_ON_ONCE((mode & FMODE_EXCL) && !holder);
  1247. if ((mode & FMODE_EXCL) && holder) {
  1248. whole = bd_start_claiming(bdev, holder);
  1249. if (IS_ERR(whole)) {
  1250. bdput(bdev);
  1251. return PTR_ERR(whole);
  1252. }
  1253. }
  1254. res = __blkdev_get(bdev, mode, 0);
  1255. if (whole) {
  1256. struct gendisk *disk = whole->bd_disk;
  1257. /* finish claiming */
  1258. mutex_lock(&bdev->bd_mutex);
  1259. spin_lock(&bdev_lock);
  1260. if (!res) {
  1261. BUG_ON(!bd_may_claim(bdev, whole, holder));
  1262. /*
  1263. * Note that for a whole device bd_holders
  1264. * will be incremented twice, and bd_holder
  1265. * will be set to bd_may_claim before being
  1266. * set to holder
  1267. */
  1268. whole->bd_holders++;
  1269. whole->bd_holder = bd_may_claim;
  1270. bdev->bd_holders++;
  1271. bdev->bd_holder = holder;
  1272. }
  1273. /* tell others that we're done */
  1274. BUG_ON(whole->bd_claiming != holder);
  1275. whole->bd_claiming = NULL;
  1276. wake_up_bit(&whole->bd_claiming, 0);
  1277. spin_unlock(&bdev_lock);
  1278. /*
  1279. * Block event polling for write claims if requested. Any
  1280. * write holder makes the write_holder state stick until
  1281. * all are released. This is good enough and tracking
  1282. * individual writeable reference is too fragile given the
  1283. * way @mode is used in blkdev_get/put().
  1284. */
  1285. if (!res && (mode & FMODE_WRITE) && !bdev->bd_write_holder &&
  1286. (disk->flags & GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE)) {
  1287. bdev->bd_write_holder = true;
  1288. disk_block_events(disk);
  1289. }
  1290. mutex_unlock(&bdev->bd_mutex);
  1291. bdput(whole);
  1292. }
  1293. return res;
  1294. }
  1295. EXPORT_SYMBOL(blkdev_get);
  1296. /**
  1297. * blkdev_get_by_path - open a block device by name
  1298. * @path: path to the block device to open
  1299. * @mode: FMODE_* mask
  1300. * @holder: exclusive holder identifier
  1301. *
  1302. * Open the blockdevice described by the device file at @path. @mode
  1303. * and @holder are identical to blkdev_get().
  1304. *
  1305. * On success, the returned block_device has reference count of one.
  1306. *
  1307. * CONTEXT:
  1308. * Might sleep.
  1309. *
  1310. * RETURNS:
  1311. * Pointer to block_device on success, ERR_PTR(-errno) on failure.
  1312. */
  1313. struct block_device *blkdev_get_by_path(const char *path, fmode_t mode,
  1314. void *holder)
  1315. {
  1316. struct block_device *bdev;
  1317. int err;
  1318. bdev = lookup_bdev(path);
  1319. if (IS_ERR(bdev))
  1320. return bdev;
  1321. err = blkdev_get(bdev, mode, holder);
  1322. if (err)
  1323. return ERR_PTR(err);
  1324. if ((mode & FMODE_WRITE) && bdev_read_only(bdev)) {
  1325. blkdev_put(bdev, mode);
  1326. return ERR_PTR(-EACCES);
  1327. }
  1328. return bdev;
  1329. }
  1330. EXPORT_SYMBOL(blkdev_get_by_path);
  1331. /**
  1332. * blkdev_get_by_dev - open a block device by device number
  1333. * @dev: device number of block device to open
  1334. * @mode: FMODE_* mask
  1335. * @holder: exclusive holder identifier
  1336. *
  1337. * Open the blockdevice described by device number @dev. @mode and
  1338. * @holder are identical to blkdev_get().
  1339. *
  1340. * Use it ONLY if you really do not have anything better - i.e. when
  1341. * you are behind a truly sucky interface and all you are given is a
  1342. * device number. _Never_ to be used for internal purposes. If you
  1343. * ever need it - reconsider your API.
  1344. *
  1345. * On success, the returned block_device has reference count of one.
  1346. *
  1347. * CONTEXT:
  1348. * Might sleep.
  1349. *
  1350. * RETURNS:
  1351. * Pointer to block_device on success, ERR_PTR(-errno) on failure.
  1352. */
  1353. struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder)
  1354. {
  1355. struct block_device *bdev;
  1356. int err;
  1357. bdev = bdget(dev);
  1358. if (!bdev)
  1359. return ERR_PTR(-ENOMEM);
  1360. err = blkdev_get(bdev, mode, holder);
  1361. if (err)
  1362. return ERR_PTR(err);
  1363. return bdev;
  1364. }
  1365. EXPORT_SYMBOL(blkdev_get_by_dev);
  1366. static int blkdev_open(struct inode * inode, struct file * filp)
  1367. {
  1368. struct block_device *bdev;
  1369. /*
  1370. * Preserve backwards compatibility and allow large file access
  1371. * even if userspace doesn't ask for it explicitly. Some mkfs
  1372. * binary needs it. We might want to drop this workaround
  1373. * during an unstable branch.
  1374. */
  1375. filp->f_flags |= O_LARGEFILE;
  1376. if (filp->f_flags & O_NDELAY)
  1377. filp->f_mode |= FMODE_NDELAY;
  1378. if (filp->f_flags & O_EXCL)
  1379. filp->f_mode |= FMODE_EXCL;
  1380. if ((filp->f_flags & O_ACCMODE) == 3)
  1381. filp->f_mode |= FMODE_WRITE_IOCTL;
  1382. bdev = bd_acquire(inode);
  1383. if (bdev == NULL)
  1384. return -ENOMEM;
  1385. filp->f_mapping = bdev->bd_inode->i_mapping;
  1386. return blkdev_get(bdev, filp->f_mode, filp);
  1387. }
  1388. static void __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part)
  1389. {
  1390. struct gendisk *disk = bdev->bd_disk;
  1391. struct block_device *victim = NULL;
  1392. mutex_lock_nested(&bdev->bd_mutex, for_part);
  1393. if (for_part)
  1394. bdev->bd_part_count--;
  1395. if (!--bdev->bd_openers) {
  1396. WARN_ON_ONCE(bdev->bd_holders);
  1397. sync_blockdev(bdev);
  1398. kill_bdev(bdev);
  1399. bdev_write_inode(bdev);
  1400. /*
  1401. * Detaching bdev inode from its wb in __destroy_inode()
  1402. * is too late: the queue which embeds its bdi (along with
  1403. * root wb) can be gone as soon as we put_disk() below.
  1404. */
  1405. inode_detach_wb(bdev->bd_inode);
  1406. }
  1407. if (bdev->bd_contains == bdev) {
  1408. if (disk->fops->release)
  1409. disk->fops->release(disk, mode);
  1410. }
  1411. if (!bdev->bd_openers) {
  1412. struct module *owner = disk->fops->owner;
  1413. disk_put_part(bdev->bd_part);
  1414. bdev->bd_part = NULL;
  1415. bdev->bd_disk = NULL;
  1416. if (bdev != bdev->bd_contains)
  1417. victim = bdev->bd_contains;
  1418. bdev->bd_contains = NULL;
  1419. put_disk(disk);
  1420. module_put(owner);
  1421. }
  1422. mutex_unlock(&bdev->bd_mutex);
  1423. bdput(bdev);
  1424. if (victim)
  1425. __blkdev_put(victim, mode, 1);
  1426. }
  1427. void blkdev_put(struct block_device *bdev, fmode_t mode)
  1428. {
  1429. mutex_lock(&bdev->bd_mutex);
  1430. if (mode & FMODE_EXCL) {
  1431. bool bdev_free;
  1432. /*
  1433. * Release a claim on the device. The holder fields
  1434. * are protected with bdev_lock. bd_mutex is to
  1435. * synchronize disk_holder unlinking.
  1436. */
  1437. spin_lock(&bdev_lock);
  1438. WARN_ON_ONCE(--bdev->bd_holders < 0);
  1439. WARN_ON_ONCE(--bdev->bd_contains->bd_holders < 0);
  1440. /* bd_contains might point to self, check in a separate step */
  1441. if ((bdev_free = !bdev->bd_holders))
  1442. bdev->bd_holder = NULL;
  1443. if (!bdev->bd_contains->bd_holders)
  1444. bdev->bd_contains->bd_holder = NULL;
  1445. spin_unlock(&bdev_lock);
  1446. /*
  1447. * If this was the last claim, remove holder link and
  1448. * unblock evpoll if it was a write holder.
  1449. */
  1450. if (bdev_free && bdev->bd_write_holder) {
  1451. disk_unblock_events(bdev->bd_disk);
  1452. bdev->bd_write_holder = false;
  1453. }
  1454. }
  1455. /*
  1456. * Trigger event checking and tell drivers to flush MEDIA_CHANGE
  1457. * event. This is to ensure detection of media removal commanded
  1458. * from userland - e.g. eject(1).
  1459. */
  1460. disk_flush_events(bdev->bd_disk, DISK_EVENT_MEDIA_CHANGE);
  1461. mutex_unlock(&bdev->bd_mutex);
  1462. __blkdev_put(bdev, mode, 0);
  1463. }
  1464. EXPORT_SYMBOL(blkdev_put);
  1465. static int blkdev_close(struct inode * inode, struct file * filp)
  1466. {
  1467. struct block_device *bdev = I_BDEV(bdev_file_inode(filp));
  1468. blkdev_put(bdev, filp->f_mode);
  1469. return 0;
  1470. }
  1471. static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg)
  1472. {
  1473. struct block_device *bdev = I_BDEV(bdev_file_inode(file));
  1474. fmode_t mode = file->f_mode;
  1475. /*
  1476. * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
  1477. * to updated it before every ioctl.
  1478. */
  1479. if (file->f_flags & O_NDELAY)
  1480. mode |= FMODE_NDELAY;
  1481. else
  1482. mode &= ~FMODE_NDELAY;
  1483. return blkdev_ioctl(bdev, mode, cmd, arg);
  1484. }
  1485. /*
  1486. * Write data to the block device. Only intended for the block device itself
  1487. * and the raw driver which basically is a fake block device.
  1488. *
  1489. * Does not take i_mutex for the write and thus is not for general purpose
  1490. * use.
  1491. */
  1492. ssize_t blkdev_write_iter(struct kiocb *iocb, struct iov_iter *from)
  1493. {
  1494. struct file *file = iocb->ki_filp;
  1495. struct inode *bd_inode = bdev_file_inode(file);
  1496. loff_t size = i_size_read(bd_inode);
  1497. struct blk_plug plug;
  1498. ssize_t ret;
  1499. if (bdev_read_only(I_BDEV(bd_inode)))
  1500. return -EPERM;
  1501. if (!iov_iter_count(from))
  1502. return 0;
  1503. if (iocb->ki_pos >= size)
  1504. return -ENOSPC;
  1505. iov_iter_truncate(from, size - iocb->ki_pos);
  1506. blk_start_plug(&plug);
  1507. ret = __generic_file_write_iter(iocb, from);
  1508. if (ret > 0)
  1509. ret = generic_write_sync(iocb, ret);
  1510. blk_finish_plug(&plug);
  1511. return ret;
  1512. }
  1513. EXPORT_SYMBOL_GPL(blkdev_write_iter);
  1514. ssize_t blkdev_read_iter(struct kiocb *iocb, struct iov_iter *to)
  1515. {
  1516. struct file *file = iocb->ki_filp;
  1517. struct inode *bd_inode = bdev_file_inode(file);
  1518. loff_t size = i_size_read(bd_inode);
  1519. loff_t pos = iocb->ki_pos;
  1520. if (pos >= size)
  1521. return 0;
  1522. size -= pos;
  1523. iov_iter_truncate(to, size);
  1524. return generic_file_read_iter(iocb, to);
  1525. }
  1526. EXPORT_SYMBOL_GPL(blkdev_read_iter);
  1527. /*
  1528. * Try to release a page associated with block device when the system
  1529. * is under memory pressure.
  1530. */
  1531. static int blkdev_releasepage(struct page *page, gfp_t wait)
  1532. {
  1533. struct super_block *super = BDEV_I(page->mapping->host)->bdev.bd_super;
  1534. if (super && super->s_op->bdev_try_to_free_page)
  1535. return super->s_op->bdev_try_to_free_page(super, page, wait);
  1536. return try_to_free_buffers(page);
  1537. }
  1538. static int blkdev_writepages(struct address_space *mapping,
  1539. struct writeback_control *wbc)
  1540. {
  1541. if (dax_mapping(mapping)) {
  1542. struct block_device *bdev = I_BDEV(mapping->host);
  1543. return dax_writeback_mapping_range(mapping, bdev, wbc);
  1544. }
  1545. return generic_writepages(mapping, wbc);
  1546. }
  1547. static const struct address_space_operations def_blk_aops = {
  1548. .readpage = blkdev_readpage,
  1549. .readpages = blkdev_readpages,
  1550. .writepage = blkdev_writepage,
  1551. .write_begin = blkdev_write_begin,
  1552. .write_end = blkdev_write_end,
  1553. .writepages = blkdev_writepages,
  1554. .releasepage = blkdev_releasepage,
  1555. .direct_IO = blkdev_direct_IO,
  1556. .is_dirty_writeback = buffer_check_dirty_writeback,
  1557. };
  1558. #define BLKDEV_FALLOC_FL_SUPPORTED \
  1559. (FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | \
  1560. FALLOC_FL_ZERO_RANGE | FALLOC_FL_NO_HIDE_STALE)
  1561. static long blkdev_fallocate(struct file *file, int mode, loff_t start,
  1562. loff_t len)
  1563. {
  1564. struct block_device *bdev = I_BDEV(bdev_file_inode(file));
  1565. struct request_queue *q = bdev_get_queue(bdev);
  1566. struct address_space *mapping;
  1567. loff_t end = start + len - 1;
  1568. loff_t isize;
  1569. int error;
  1570. /* Fail if we don't recognize the flags. */
  1571. if (mode & ~BLKDEV_FALLOC_FL_SUPPORTED)
  1572. return -EOPNOTSUPP;
  1573. /* Don't go off the end of the device. */
  1574. isize = i_size_read(bdev->bd_inode);
  1575. if (start >= isize)
  1576. return -EINVAL;
  1577. if (end >= isize) {
  1578. if (mode & FALLOC_FL_KEEP_SIZE) {
  1579. len = isize - start;
  1580. end = start + len - 1;
  1581. } else
  1582. return -EINVAL;
  1583. }
  1584. /*
  1585. * Don't allow IO that isn't aligned to logical block size.
  1586. */
  1587. if ((start | len) & (bdev_logical_block_size(bdev) - 1))
  1588. return -EINVAL;
  1589. /* Invalidate the page cache, including dirty pages. */
  1590. mapping = bdev->bd_inode->i_mapping;
  1591. truncate_inode_pages_range(mapping, start, end);
  1592. switch (mode) {
  1593. case FALLOC_FL_ZERO_RANGE:
  1594. case FALLOC_FL_ZERO_RANGE | FALLOC_FL_KEEP_SIZE:
  1595. error = blkdev_issue_zeroout(bdev, start >> 9, len >> 9,
  1596. GFP_KERNEL, false);
  1597. break;
  1598. case FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE:
  1599. /* Only punch if the device can do zeroing discard. */
  1600. if (!blk_queue_discard(q) || !q->limits.discard_zeroes_data)
  1601. return -EOPNOTSUPP;
  1602. error = blkdev_issue_discard(bdev, start >> 9, len >> 9,
  1603. GFP_KERNEL, 0);
  1604. break;
  1605. case FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE | FALLOC_FL_NO_HIDE_STALE:
  1606. if (!blk_queue_discard(q))
  1607. return -EOPNOTSUPP;
  1608. error = blkdev_issue_discard(bdev, start >> 9, len >> 9,
  1609. GFP_KERNEL, 0);
  1610. break;
  1611. default:
  1612. return -EOPNOTSUPP;
  1613. }
  1614. if (error)
  1615. return error;
  1616. /*
  1617. * Invalidate again; if someone wandered in and dirtied a page,
  1618. * the caller will be given -EBUSY. The third argument is
  1619. * inclusive, so the rounding here is safe.
  1620. */
  1621. return invalidate_inode_pages2_range(mapping,
  1622. start >> PAGE_SHIFT,
  1623. end >> PAGE_SHIFT);
  1624. }
  1625. const struct file_operations def_blk_fops = {
  1626. .open = blkdev_open,
  1627. .release = blkdev_close,
  1628. .llseek = block_llseek,
  1629. .read_iter = blkdev_read_iter,
  1630. .write_iter = blkdev_write_iter,
  1631. .mmap = generic_file_mmap,
  1632. .fsync = blkdev_fsync,
  1633. .unlocked_ioctl = block_ioctl,
  1634. #ifdef CONFIG_COMPAT
  1635. .compat_ioctl = compat_blkdev_ioctl,
  1636. #endif
  1637. .splice_read = generic_file_splice_read,
  1638. .splice_write = iter_file_splice_write,
  1639. .fallocate = blkdev_fallocate,
  1640. };
  1641. int ioctl_by_bdev(struct block_device *bdev, unsigned cmd, unsigned long arg)
  1642. {
  1643. int res;
  1644. mm_segment_t old_fs = get_fs();
  1645. set_fs(KERNEL_DS);
  1646. res = blkdev_ioctl(bdev, 0, cmd, arg);
  1647. set_fs(old_fs);
  1648. return res;
  1649. }
  1650. EXPORT_SYMBOL(ioctl_by_bdev);
  1651. /**
  1652. * lookup_bdev - lookup a struct block_device by name
  1653. * @pathname: special file representing the block device
  1654. *
  1655. * Get a reference to the blockdevice at @pathname in the current
  1656. * namespace if possible and return it. Return ERR_PTR(error)
  1657. * otherwise.
  1658. */
  1659. struct block_device *lookup_bdev(const char *pathname)
  1660. {
  1661. struct block_device *bdev;
  1662. struct inode *inode;
  1663. struct path path;
  1664. int error;
  1665. if (!pathname || !*pathname)
  1666. return ERR_PTR(-EINVAL);
  1667. error = kern_path(pathname, LOOKUP_FOLLOW, &path);
  1668. if (error)
  1669. return ERR_PTR(error);
  1670. inode = d_backing_inode(path.dentry);
  1671. error = -ENOTBLK;
  1672. if (!S_ISBLK(inode->i_mode))
  1673. goto fail;
  1674. error = -EACCES;
  1675. if (!may_open_dev(&path))
  1676. goto fail;
  1677. error = -ENOMEM;
  1678. bdev = bd_acquire(inode);
  1679. if (!bdev)
  1680. goto fail;
  1681. out:
  1682. path_put(&path);
  1683. return bdev;
  1684. fail:
  1685. bdev = ERR_PTR(error);
  1686. goto out;
  1687. }
  1688. EXPORT_SYMBOL(lookup_bdev);
  1689. int __invalidate_device(struct block_device *bdev, bool kill_dirty)
  1690. {
  1691. struct super_block *sb = get_super(bdev);
  1692. int res = 0;
  1693. if (sb) {
  1694. /*
  1695. * no need to lock the super, get_super holds the
  1696. * read mutex so the filesystem cannot go away
  1697. * under us (->put_super runs with the write lock
  1698. * hold).
  1699. */
  1700. shrink_dcache_sb(sb);
  1701. res = invalidate_inodes(sb, kill_dirty);
  1702. drop_super(sb);
  1703. }
  1704. invalidate_bdev(bdev);
  1705. return res;
  1706. }
  1707. EXPORT_SYMBOL(__invalidate_device);
  1708. void iterate_bdevs(void (*func)(struct block_device *, void *), void *arg)
  1709. {
  1710. struct inode *inode, *old_inode = NULL;
  1711. spin_lock(&blockdev_superblock->s_inode_list_lock);
  1712. list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list) {
  1713. struct address_space *mapping = inode->i_mapping;
  1714. struct block_device *bdev;
  1715. spin_lock(&inode->i_lock);
  1716. if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW) ||
  1717. mapping->nrpages == 0) {
  1718. spin_unlock(&inode->i_lock);
  1719. continue;
  1720. }
  1721. __iget(inode);
  1722. spin_unlock(&inode->i_lock);
  1723. spin_unlock(&blockdev_superblock->s_inode_list_lock);
  1724. /*
  1725. * We hold a reference to 'inode' so it couldn't have been
  1726. * removed from s_inodes list while we dropped the
  1727. * s_inode_list_lock We cannot iput the inode now as we can
  1728. * be holding the last reference and we cannot iput it under
  1729. * s_inode_list_lock. So we keep the reference and iput it
  1730. * later.
  1731. */
  1732. iput(old_inode);
  1733. old_inode = inode;
  1734. bdev = I_BDEV(inode);
  1735. mutex_lock(&bdev->bd_mutex);
  1736. if (bdev->bd_openers)
  1737. func(bdev, arg);
  1738. mutex_unlock(&bdev->bd_mutex);
  1739. spin_lock(&blockdev_superblock->s_inode_list_lock);
  1740. }
  1741. spin_unlock(&blockdev_superblock->s_inode_list_lock);
  1742. iput(old_inode);
  1743. }