block_dev.c 42 KB

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