block_dev.c 45 KB

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