block_dev.c 44 KB

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