block_dev.c 44 KB

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