block_dev.c 46 KB

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