block_dev.c 45 KB

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