block_dev.c 49 KB

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