block_dev.c 45 KB

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