block_dev.c 44 KB

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