block_dev.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237
  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/config.h>
  8. #include <linux/init.h>
  9. #include <linux/mm.h>
  10. #include <linux/fcntl.h>
  11. #include <linux/slab.h>
  12. #include <linux/kmod.h>
  13. #include <linux/major.h>
  14. #include <linux/devfs_fs_kernel.h>
  15. #include <linux/smp_lock.h>
  16. #include <linux/highmem.h>
  17. #include <linux/blkdev.h>
  18. #include <linux/module.h>
  19. #include <linux/blkpg.h>
  20. #include <linux/buffer_head.h>
  21. #include <linux/mpage.h>
  22. #include <linux/mount.h>
  23. #include <linux/uio.h>
  24. #include <linux/namei.h>
  25. #include <asm/uaccess.h>
  26. struct bdev_inode {
  27. struct block_device bdev;
  28. struct inode vfs_inode;
  29. };
  30. static inline struct bdev_inode *BDEV_I(struct inode *inode)
  31. {
  32. return container_of(inode, struct bdev_inode, vfs_inode);
  33. }
  34. inline struct block_device *I_BDEV(struct inode *inode)
  35. {
  36. return &BDEV_I(inode)->bdev;
  37. }
  38. EXPORT_SYMBOL(I_BDEV);
  39. static sector_t max_block(struct block_device *bdev)
  40. {
  41. sector_t retval = ~((sector_t)0);
  42. loff_t sz = i_size_read(bdev->bd_inode);
  43. if (sz) {
  44. unsigned int size = block_size(bdev);
  45. unsigned int sizebits = blksize_bits(size);
  46. retval = (sz >> sizebits);
  47. }
  48. return retval;
  49. }
  50. /* Kill _all_ buffers, dirty or not.. */
  51. static void kill_bdev(struct block_device *bdev)
  52. {
  53. invalidate_bdev(bdev, 1);
  54. truncate_inode_pages(bdev->bd_inode->i_mapping, 0);
  55. }
  56. int set_blocksize(struct block_device *bdev, int size)
  57. {
  58. /* Size must be a power of two, and between 512 and PAGE_SIZE */
  59. if (size > PAGE_SIZE || size < 512 || (size & (size-1)))
  60. return -EINVAL;
  61. /* Size cannot be smaller than the size supported by the device */
  62. if (size < bdev_hardsect_size(bdev))
  63. return -EINVAL;
  64. /* Don't change the size if it is same as current */
  65. if (bdev->bd_block_size != size) {
  66. sync_blockdev(bdev);
  67. bdev->bd_block_size = size;
  68. bdev->bd_inode->i_blkbits = blksize_bits(size);
  69. kill_bdev(bdev);
  70. }
  71. return 0;
  72. }
  73. EXPORT_SYMBOL(set_blocksize);
  74. int sb_set_blocksize(struct super_block *sb, int size)
  75. {
  76. if (set_blocksize(sb->s_bdev, size))
  77. return 0;
  78. /* If we get here, we know size is power of two
  79. * and it's value is between 512 and PAGE_SIZE */
  80. sb->s_blocksize = size;
  81. sb->s_blocksize_bits = blksize_bits(size);
  82. return sb->s_blocksize;
  83. }
  84. EXPORT_SYMBOL(sb_set_blocksize);
  85. int sb_min_blocksize(struct super_block *sb, int size)
  86. {
  87. int minsize = bdev_hardsect_size(sb->s_bdev);
  88. if (size < minsize)
  89. size = minsize;
  90. return sb_set_blocksize(sb, size);
  91. }
  92. EXPORT_SYMBOL(sb_min_blocksize);
  93. static int
  94. blkdev_get_block(struct inode *inode, sector_t iblock,
  95. struct buffer_head *bh, int create)
  96. {
  97. if (iblock >= max_block(I_BDEV(inode))) {
  98. if (create)
  99. return -EIO;
  100. /*
  101. * for reads, we're just trying to fill a partial page.
  102. * return a hole, they will have to call get_block again
  103. * before they can fill it, and they will get -EIO at that
  104. * time
  105. */
  106. return 0;
  107. }
  108. bh->b_bdev = I_BDEV(inode);
  109. bh->b_blocknr = iblock;
  110. set_buffer_mapped(bh);
  111. return 0;
  112. }
  113. static int
  114. blkdev_get_blocks(struct inode *inode, sector_t iblock,
  115. struct buffer_head *bh, int create)
  116. {
  117. sector_t end_block = max_block(I_BDEV(inode));
  118. unsigned long max_blocks = bh->b_size >> inode->i_blkbits;
  119. if ((iblock + max_blocks) > end_block) {
  120. max_blocks = end_block - iblock;
  121. if ((long)max_blocks <= 0) {
  122. if (create)
  123. return -EIO; /* write fully beyond EOF */
  124. /*
  125. * It is a read which is fully beyond EOF. We return
  126. * a !buffer_mapped buffer
  127. */
  128. max_blocks = 0;
  129. }
  130. }
  131. bh->b_bdev = I_BDEV(inode);
  132. bh->b_blocknr = iblock;
  133. bh->b_size = max_blocks << inode->i_blkbits;
  134. if (max_blocks)
  135. set_buffer_mapped(bh);
  136. return 0;
  137. }
  138. static ssize_t
  139. blkdev_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
  140. loff_t offset, unsigned long nr_segs)
  141. {
  142. struct file *file = iocb->ki_filp;
  143. struct inode *inode = file->f_mapping->host;
  144. return blockdev_direct_IO_no_locking(rw, iocb, inode, I_BDEV(inode),
  145. iov, offset, nr_segs, blkdev_get_blocks, NULL);
  146. }
  147. static int blkdev_writepage(struct page *page, struct writeback_control *wbc)
  148. {
  149. return block_write_full_page(page, blkdev_get_block, wbc);
  150. }
  151. static int blkdev_readpage(struct file * file, struct page * page)
  152. {
  153. return block_read_full_page(page, blkdev_get_block);
  154. }
  155. static int blkdev_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to)
  156. {
  157. return block_prepare_write(page, from, to, blkdev_get_block);
  158. }
  159. static int blkdev_commit_write(struct file *file, struct page *page, unsigned from, unsigned to)
  160. {
  161. return block_commit_write(page, from, to);
  162. }
  163. /*
  164. * private llseek:
  165. * for a block special file file->f_dentry->d_inode->i_size is zero
  166. * so we compute the size by hand (just as in block_read/write above)
  167. */
  168. static loff_t block_llseek(struct file *file, loff_t offset, int origin)
  169. {
  170. struct inode *bd_inode = file->f_mapping->host;
  171. loff_t size;
  172. loff_t retval;
  173. mutex_lock(&bd_inode->i_mutex);
  174. size = i_size_read(bd_inode);
  175. switch (origin) {
  176. case 2:
  177. offset += size;
  178. break;
  179. case 1:
  180. offset += file->f_pos;
  181. }
  182. retval = -EINVAL;
  183. if (offset >= 0 && offset <= size) {
  184. if (offset != file->f_pos) {
  185. file->f_pos = offset;
  186. }
  187. retval = offset;
  188. }
  189. mutex_unlock(&bd_inode->i_mutex);
  190. return retval;
  191. }
  192. /*
  193. * Filp is never NULL; the only case when ->fsync() is called with
  194. * NULL first argument is nfsd_sync_dir() and that's not a directory.
  195. */
  196. static int block_fsync(struct file *filp, struct dentry *dentry, int datasync)
  197. {
  198. return sync_blockdev(I_BDEV(filp->f_mapping->host));
  199. }
  200. /*
  201. * pseudo-fs
  202. */
  203. static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);
  204. static kmem_cache_t * bdev_cachep __read_mostly;
  205. static struct inode *bdev_alloc_inode(struct super_block *sb)
  206. {
  207. struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, SLAB_KERNEL);
  208. if (!ei)
  209. return NULL;
  210. return &ei->vfs_inode;
  211. }
  212. static void bdev_destroy_inode(struct inode *inode)
  213. {
  214. struct bdev_inode *bdi = BDEV_I(inode);
  215. bdi->bdev.bd_inode_backing_dev_info = NULL;
  216. kmem_cache_free(bdev_cachep, bdi);
  217. }
  218. static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
  219. {
  220. struct bdev_inode *ei = (struct bdev_inode *) foo;
  221. struct block_device *bdev = &ei->bdev;
  222. if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
  223. SLAB_CTOR_CONSTRUCTOR)
  224. {
  225. memset(bdev, 0, sizeof(*bdev));
  226. mutex_init(&bdev->bd_mutex);
  227. mutex_init(&bdev->bd_mount_mutex);
  228. INIT_LIST_HEAD(&bdev->bd_inodes);
  229. INIT_LIST_HEAD(&bdev->bd_list);
  230. #ifdef CONFIG_SYSFS
  231. INIT_LIST_HEAD(&bdev->bd_holder_list);
  232. #endif
  233. inode_init_once(&ei->vfs_inode);
  234. }
  235. }
  236. static inline void __bd_forget(struct inode *inode)
  237. {
  238. list_del_init(&inode->i_devices);
  239. inode->i_bdev = NULL;
  240. inode->i_mapping = &inode->i_data;
  241. }
  242. static void bdev_clear_inode(struct inode *inode)
  243. {
  244. struct block_device *bdev = &BDEV_I(inode)->bdev;
  245. struct list_head *p;
  246. spin_lock(&bdev_lock);
  247. while ( (p = bdev->bd_inodes.next) != &bdev->bd_inodes ) {
  248. __bd_forget(list_entry(p, struct inode, i_devices));
  249. }
  250. list_del_init(&bdev->bd_list);
  251. spin_unlock(&bdev_lock);
  252. }
  253. static struct super_operations bdev_sops = {
  254. .statfs = simple_statfs,
  255. .alloc_inode = bdev_alloc_inode,
  256. .destroy_inode = bdev_destroy_inode,
  257. .drop_inode = generic_delete_inode,
  258. .clear_inode = bdev_clear_inode,
  259. };
  260. static int bd_get_sb(struct file_system_type *fs_type,
  261. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  262. {
  263. return get_sb_pseudo(fs_type, "bdev:", &bdev_sops, 0x62646576, mnt);
  264. }
  265. static struct file_system_type bd_type = {
  266. .name = "bdev",
  267. .get_sb = bd_get_sb,
  268. .kill_sb = kill_anon_super,
  269. };
  270. static struct vfsmount *bd_mnt __read_mostly;
  271. struct super_block *blockdev_superblock;
  272. void __init bdev_cache_init(void)
  273. {
  274. int err;
  275. bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
  276. 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
  277. SLAB_MEM_SPREAD|SLAB_PANIC),
  278. init_once, NULL);
  279. err = register_filesystem(&bd_type);
  280. if (err)
  281. panic("Cannot register bdev pseudo-fs");
  282. bd_mnt = kern_mount(&bd_type);
  283. err = PTR_ERR(bd_mnt);
  284. if (IS_ERR(bd_mnt))
  285. panic("Cannot create bdev pseudo-fs");
  286. blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */
  287. }
  288. /*
  289. * Most likely _very_ bad one - but then it's hardly critical for small
  290. * /dev and can be fixed when somebody will need really large one.
  291. * Keep in mind that it will be fed through icache hash function too.
  292. */
  293. static inline unsigned long hash(dev_t dev)
  294. {
  295. return MAJOR(dev)+MINOR(dev);
  296. }
  297. static int bdev_test(struct inode *inode, void *data)
  298. {
  299. return BDEV_I(inode)->bdev.bd_dev == *(dev_t *)data;
  300. }
  301. static int bdev_set(struct inode *inode, void *data)
  302. {
  303. BDEV_I(inode)->bdev.bd_dev = *(dev_t *)data;
  304. return 0;
  305. }
  306. static LIST_HEAD(all_bdevs);
  307. struct block_device *bdget(dev_t dev)
  308. {
  309. struct block_device *bdev;
  310. struct inode *inode;
  311. inode = iget5_locked(bd_mnt->mnt_sb, hash(dev),
  312. bdev_test, bdev_set, &dev);
  313. if (!inode)
  314. return NULL;
  315. bdev = &BDEV_I(inode)->bdev;
  316. if (inode->i_state & I_NEW) {
  317. bdev->bd_contains = NULL;
  318. bdev->bd_inode = inode;
  319. bdev->bd_block_size = (1 << inode->i_blkbits);
  320. bdev->bd_part_count = 0;
  321. bdev->bd_invalidated = 0;
  322. inode->i_mode = S_IFBLK;
  323. inode->i_rdev = dev;
  324. inode->i_bdev = bdev;
  325. inode->i_data.a_ops = &def_blk_aops;
  326. mapping_set_gfp_mask(&inode->i_data, GFP_USER);
  327. inode->i_data.backing_dev_info = &default_backing_dev_info;
  328. spin_lock(&bdev_lock);
  329. list_add(&bdev->bd_list, &all_bdevs);
  330. spin_unlock(&bdev_lock);
  331. unlock_new_inode(inode);
  332. }
  333. return bdev;
  334. }
  335. EXPORT_SYMBOL(bdget);
  336. long nr_blockdev_pages(void)
  337. {
  338. struct list_head *p;
  339. long ret = 0;
  340. spin_lock(&bdev_lock);
  341. list_for_each(p, &all_bdevs) {
  342. struct block_device *bdev;
  343. bdev = list_entry(p, struct block_device, bd_list);
  344. ret += bdev->bd_inode->i_mapping->nrpages;
  345. }
  346. spin_unlock(&bdev_lock);
  347. return ret;
  348. }
  349. void bdput(struct block_device *bdev)
  350. {
  351. iput(bdev->bd_inode);
  352. }
  353. EXPORT_SYMBOL(bdput);
  354. static struct block_device *bd_acquire(struct inode *inode)
  355. {
  356. struct block_device *bdev;
  357. spin_lock(&bdev_lock);
  358. bdev = inode->i_bdev;
  359. if (bdev) {
  360. atomic_inc(&bdev->bd_inode->i_count);
  361. spin_unlock(&bdev_lock);
  362. return bdev;
  363. }
  364. spin_unlock(&bdev_lock);
  365. bdev = bdget(inode->i_rdev);
  366. if (bdev) {
  367. spin_lock(&bdev_lock);
  368. if (!inode->i_bdev) {
  369. /*
  370. * We take an additional bd_inode->i_count for inode,
  371. * and it's released in clear_inode() of inode.
  372. * So, we can access it via ->i_mapping always
  373. * without igrab().
  374. */
  375. atomic_inc(&bdev->bd_inode->i_count);
  376. inode->i_bdev = bdev;
  377. inode->i_mapping = bdev->bd_inode->i_mapping;
  378. list_add(&inode->i_devices, &bdev->bd_inodes);
  379. }
  380. spin_unlock(&bdev_lock);
  381. }
  382. return bdev;
  383. }
  384. /* Call when you free inode */
  385. void bd_forget(struct inode *inode)
  386. {
  387. struct block_device *bdev = NULL;
  388. spin_lock(&bdev_lock);
  389. if (inode->i_bdev) {
  390. if (inode->i_sb != blockdev_superblock)
  391. bdev = inode->i_bdev;
  392. __bd_forget(inode);
  393. }
  394. spin_unlock(&bdev_lock);
  395. if (bdev)
  396. iput(bdev->bd_inode);
  397. }
  398. int bd_claim(struct block_device *bdev, void *holder)
  399. {
  400. int res;
  401. spin_lock(&bdev_lock);
  402. /* first decide result */
  403. if (bdev->bd_holder == holder)
  404. res = 0; /* already a holder */
  405. else if (bdev->bd_holder != NULL)
  406. res = -EBUSY; /* held by someone else */
  407. else if (bdev->bd_contains == bdev)
  408. res = 0; /* is a whole device which isn't held */
  409. else if (bdev->bd_contains->bd_holder == bd_claim)
  410. res = 0; /* is a partition of a device that is being partitioned */
  411. else if (bdev->bd_contains->bd_holder != NULL)
  412. res = -EBUSY; /* is a partition of a held device */
  413. else
  414. res = 0; /* is a partition of an un-held device */
  415. /* now impose change */
  416. if (res==0) {
  417. /* note that for a whole device bd_holders
  418. * will be incremented twice, and bd_holder will
  419. * be set to bd_claim before being set to holder
  420. */
  421. bdev->bd_contains->bd_holders ++;
  422. bdev->bd_contains->bd_holder = bd_claim;
  423. bdev->bd_holders++;
  424. bdev->bd_holder = holder;
  425. }
  426. spin_unlock(&bdev_lock);
  427. return res;
  428. }
  429. EXPORT_SYMBOL(bd_claim);
  430. void bd_release(struct block_device *bdev)
  431. {
  432. spin_lock(&bdev_lock);
  433. if (!--bdev->bd_contains->bd_holders)
  434. bdev->bd_contains->bd_holder = NULL;
  435. if (!--bdev->bd_holders)
  436. bdev->bd_holder = NULL;
  437. spin_unlock(&bdev_lock);
  438. }
  439. EXPORT_SYMBOL(bd_release);
  440. #ifdef CONFIG_SYSFS
  441. /*
  442. * Functions for bd_claim_by_kobject / bd_release_from_kobject
  443. *
  444. * If a kobject is passed to bd_claim_by_kobject()
  445. * and the kobject has a parent directory,
  446. * following symlinks are created:
  447. * o from the kobject to the claimed bdev
  448. * o from "holders" directory of the bdev to the parent of the kobject
  449. * bd_release_from_kobject() removes these symlinks.
  450. *
  451. * Example:
  452. * If /dev/dm-0 maps to /dev/sda, kobject corresponding to
  453. * /sys/block/dm-0/slaves is passed to bd_claim_by_kobject(), then:
  454. * /sys/block/dm-0/slaves/sda --> /sys/block/sda
  455. * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
  456. */
  457. static struct kobject *bdev_get_kobj(struct block_device *bdev)
  458. {
  459. if (bdev->bd_contains != bdev)
  460. return kobject_get(&bdev->bd_part->kobj);
  461. else
  462. return kobject_get(&bdev->bd_disk->kobj);
  463. }
  464. static struct kobject *bdev_get_holder(struct block_device *bdev)
  465. {
  466. if (bdev->bd_contains != bdev)
  467. return kobject_get(bdev->bd_part->holder_dir);
  468. else
  469. return kobject_get(bdev->bd_disk->holder_dir);
  470. }
  471. static void add_symlink(struct kobject *from, struct kobject *to)
  472. {
  473. if (!from || !to)
  474. return;
  475. sysfs_create_link(from, to, kobject_name(to));
  476. }
  477. static void del_symlink(struct kobject *from, struct kobject *to)
  478. {
  479. if (!from || !to)
  480. return;
  481. sysfs_remove_link(from, kobject_name(to));
  482. }
  483. /*
  484. * 'struct bd_holder' contains pointers to kobjects symlinked by
  485. * bd_claim_by_kobject.
  486. * It's connected to bd_holder_list which is protected by bdev->bd_sem.
  487. */
  488. struct bd_holder {
  489. struct list_head list; /* chain of holders of the bdev */
  490. int count; /* references from the holder */
  491. struct kobject *sdir; /* holder object, e.g. "/block/dm-0/slaves" */
  492. struct kobject *hdev; /* e.g. "/block/dm-0" */
  493. struct kobject *hdir; /* e.g. "/block/sda/holders" */
  494. struct kobject *sdev; /* e.g. "/block/sda" */
  495. };
  496. /*
  497. * Get references of related kobjects at once.
  498. * Returns 1 on success. 0 on failure.
  499. *
  500. * Should call bd_holder_release_dirs() after successful use.
  501. */
  502. static int bd_holder_grab_dirs(struct block_device *bdev,
  503. struct bd_holder *bo)
  504. {
  505. if (!bdev || !bo)
  506. return 0;
  507. bo->sdir = kobject_get(bo->sdir);
  508. if (!bo->sdir)
  509. return 0;
  510. bo->hdev = kobject_get(bo->sdir->parent);
  511. if (!bo->hdev)
  512. goto fail_put_sdir;
  513. bo->sdev = bdev_get_kobj(bdev);
  514. if (!bo->sdev)
  515. goto fail_put_hdev;
  516. bo->hdir = bdev_get_holder(bdev);
  517. if (!bo->hdir)
  518. goto fail_put_sdev;
  519. return 1;
  520. fail_put_sdev:
  521. kobject_put(bo->sdev);
  522. fail_put_hdev:
  523. kobject_put(bo->hdev);
  524. fail_put_sdir:
  525. kobject_put(bo->sdir);
  526. return 0;
  527. }
  528. /* Put references of related kobjects at once. */
  529. static void bd_holder_release_dirs(struct bd_holder *bo)
  530. {
  531. kobject_put(bo->hdir);
  532. kobject_put(bo->sdev);
  533. kobject_put(bo->hdev);
  534. kobject_put(bo->sdir);
  535. }
  536. static struct bd_holder *alloc_bd_holder(struct kobject *kobj)
  537. {
  538. struct bd_holder *bo;
  539. bo = kzalloc(sizeof(*bo), GFP_KERNEL);
  540. if (!bo)
  541. return NULL;
  542. bo->count = 1;
  543. bo->sdir = kobj;
  544. return bo;
  545. }
  546. static void free_bd_holder(struct bd_holder *bo)
  547. {
  548. kfree(bo);
  549. }
  550. /**
  551. * add_bd_holder - create sysfs symlinks for bd_claim() relationship
  552. *
  553. * @bdev: block device to be bd_claimed
  554. * @bo: preallocated and initialized by alloc_bd_holder()
  555. *
  556. * If there is no matching entry with @bo in @bdev->bd_holder_list,
  557. * add @bo to the list, create symlinks.
  558. *
  559. * Returns 1 if @bo was added to the list.
  560. * Returns 0 if @bo wasn't used by any reason and should be freed.
  561. */
  562. static int add_bd_holder(struct block_device *bdev, struct bd_holder *bo)
  563. {
  564. struct bd_holder *tmp;
  565. if (!bo)
  566. return 0;
  567. list_for_each_entry(tmp, &bdev->bd_holder_list, list) {
  568. if (tmp->sdir == bo->sdir) {
  569. tmp->count++;
  570. return 0;
  571. }
  572. }
  573. if (!bd_holder_grab_dirs(bdev, bo))
  574. return 0;
  575. add_symlink(bo->sdir, bo->sdev);
  576. add_symlink(bo->hdir, bo->hdev);
  577. list_add_tail(&bo->list, &bdev->bd_holder_list);
  578. return 1;
  579. }
  580. /**
  581. * del_bd_holder - delete sysfs symlinks for bd_claim() relationship
  582. *
  583. * @bdev: block device to be bd_claimed
  584. * @kobj: holder's kobject
  585. *
  586. * If there is matching entry with @kobj in @bdev->bd_holder_list
  587. * and no other bd_claim() from the same kobject,
  588. * remove the struct bd_holder from the list, delete symlinks for it.
  589. *
  590. * Returns a pointer to the struct bd_holder when it's removed from the list
  591. * and ready to be freed.
  592. * Returns NULL if matching claim isn't found or there is other bd_claim()
  593. * by the same kobject.
  594. */
  595. static struct bd_holder *del_bd_holder(struct block_device *bdev,
  596. struct kobject *kobj)
  597. {
  598. struct bd_holder *bo;
  599. list_for_each_entry(bo, &bdev->bd_holder_list, list) {
  600. if (bo->sdir == kobj) {
  601. bo->count--;
  602. BUG_ON(bo->count < 0);
  603. if (!bo->count) {
  604. list_del(&bo->list);
  605. del_symlink(bo->sdir, bo->sdev);
  606. del_symlink(bo->hdir, bo->hdev);
  607. bd_holder_release_dirs(bo);
  608. return bo;
  609. }
  610. break;
  611. }
  612. }
  613. return NULL;
  614. }
  615. /**
  616. * bd_claim_by_kobject - bd_claim() with additional kobject signature
  617. *
  618. * @bdev: block device to be claimed
  619. * @holder: holder's signature
  620. * @kobj: holder's kobject
  621. *
  622. * Do bd_claim() and if it succeeds, create sysfs symlinks between
  623. * the bdev and the holder's kobject.
  624. * Use bd_release_from_kobject() when relesing the claimed bdev.
  625. *
  626. * Returns 0 on success. (same as bd_claim())
  627. * Returns errno on failure.
  628. */
  629. static int bd_claim_by_kobject(struct block_device *bdev, void *holder,
  630. struct kobject *kobj)
  631. {
  632. int res;
  633. struct bd_holder *bo;
  634. if (!kobj)
  635. return -EINVAL;
  636. bo = alloc_bd_holder(kobj);
  637. if (!bo)
  638. return -ENOMEM;
  639. mutex_lock(&bdev->bd_mutex);
  640. res = bd_claim(bdev, holder);
  641. if (res || !add_bd_holder(bdev, bo))
  642. free_bd_holder(bo);
  643. mutex_unlock(&bdev->bd_mutex);
  644. return res;
  645. }
  646. /**
  647. * bd_release_from_kobject - bd_release() with additional kobject signature
  648. *
  649. * @bdev: block device to be released
  650. * @kobj: holder's kobject
  651. *
  652. * Do bd_release() and remove sysfs symlinks created by bd_claim_by_kobject().
  653. */
  654. static void bd_release_from_kobject(struct block_device *bdev,
  655. struct kobject *kobj)
  656. {
  657. struct bd_holder *bo;
  658. if (!kobj)
  659. return;
  660. mutex_lock(&bdev->bd_mutex);
  661. bd_release(bdev);
  662. if ((bo = del_bd_holder(bdev, kobj)))
  663. free_bd_holder(bo);
  664. mutex_unlock(&bdev->bd_mutex);
  665. }
  666. /**
  667. * bd_claim_by_disk - wrapper function for bd_claim_by_kobject()
  668. *
  669. * @bdev: block device to be claimed
  670. * @holder: holder's signature
  671. * @disk: holder's gendisk
  672. *
  673. * Call bd_claim_by_kobject() with getting @disk->slave_dir.
  674. */
  675. int bd_claim_by_disk(struct block_device *bdev, void *holder,
  676. struct gendisk *disk)
  677. {
  678. return bd_claim_by_kobject(bdev, holder, kobject_get(disk->slave_dir));
  679. }
  680. EXPORT_SYMBOL_GPL(bd_claim_by_disk);
  681. /**
  682. * bd_release_from_disk - wrapper function for bd_release_from_kobject()
  683. *
  684. * @bdev: block device to be claimed
  685. * @disk: holder's gendisk
  686. *
  687. * Call bd_release_from_kobject() and put @disk->slave_dir.
  688. */
  689. void bd_release_from_disk(struct block_device *bdev, struct gendisk *disk)
  690. {
  691. bd_release_from_kobject(bdev, disk->slave_dir);
  692. kobject_put(disk->slave_dir);
  693. }
  694. EXPORT_SYMBOL_GPL(bd_release_from_disk);
  695. #endif
  696. /*
  697. * Tries to open block device by device number. Use it ONLY if you
  698. * really do not have anything better - i.e. when you are behind a
  699. * truly sucky interface and all you are given is a device number. _Never_
  700. * to be used for internal purposes. If you ever need it - reconsider
  701. * your API.
  702. */
  703. struct block_device *open_by_devnum(dev_t dev, unsigned mode)
  704. {
  705. struct block_device *bdev = bdget(dev);
  706. int err = -ENOMEM;
  707. int flags = mode & FMODE_WRITE ? O_RDWR : O_RDONLY;
  708. if (bdev)
  709. err = blkdev_get(bdev, mode, flags);
  710. return err ? ERR_PTR(err) : bdev;
  711. }
  712. EXPORT_SYMBOL(open_by_devnum);
  713. /*
  714. * This routine checks whether a removable media has been changed,
  715. * and invalidates all buffer-cache-entries in that case. This
  716. * is a relatively slow routine, so we have to try to minimize using
  717. * it. Thus it is called only upon a 'mount' or 'open'. This
  718. * is the best way of combining speed and utility, I think.
  719. * People changing diskettes in the middle of an operation deserve
  720. * to lose :-)
  721. */
  722. int check_disk_change(struct block_device *bdev)
  723. {
  724. struct gendisk *disk = bdev->bd_disk;
  725. struct block_device_operations * bdops = disk->fops;
  726. if (!bdops->media_changed)
  727. return 0;
  728. if (!bdops->media_changed(bdev->bd_disk))
  729. return 0;
  730. if (__invalidate_device(bdev))
  731. printk("VFS: busy inodes on changed media.\n");
  732. if (bdops->revalidate_disk)
  733. bdops->revalidate_disk(bdev->bd_disk);
  734. if (bdev->bd_disk->minors > 1)
  735. bdev->bd_invalidated = 1;
  736. return 1;
  737. }
  738. EXPORT_SYMBOL(check_disk_change);
  739. void bd_set_size(struct block_device *bdev, loff_t size)
  740. {
  741. unsigned bsize = bdev_hardsect_size(bdev);
  742. bdev->bd_inode->i_size = size;
  743. while (bsize < PAGE_CACHE_SIZE) {
  744. if (size & bsize)
  745. break;
  746. bsize <<= 1;
  747. }
  748. bdev->bd_block_size = bsize;
  749. bdev->bd_inode->i_blkbits = blksize_bits(bsize);
  750. }
  751. EXPORT_SYMBOL(bd_set_size);
  752. static int do_open(struct block_device *bdev, struct file *file)
  753. {
  754. struct module *owner = NULL;
  755. struct gendisk *disk;
  756. int ret = -ENXIO;
  757. int part;
  758. file->f_mapping = bdev->bd_inode->i_mapping;
  759. lock_kernel();
  760. disk = get_gendisk(bdev->bd_dev, &part);
  761. if (!disk) {
  762. unlock_kernel();
  763. bdput(bdev);
  764. return ret;
  765. }
  766. owner = disk->fops->owner;
  767. mutex_lock(&bdev->bd_mutex);
  768. if (!bdev->bd_openers) {
  769. bdev->bd_disk = disk;
  770. bdev->bd_contains = bdev;
  771. if (!part) {
  772. struct backing_dev_info *bdi;
  773. if (disk->fops->open) {
  774. ret = disk->fops->open(bdev->bd_inode, file);
  775. if (ret)
  776. goto out_first;
  777. }
  778. if (!bdev->bd_openers) {
  779. bd_set_size(bdev,(loff_t)get_capacity(disk)<<9);
  780. bdi = blk_get_backing_dev_info(bdev);
  781. if (bdi == NULL)
  782. bdi = &default_backing_dev_info;
  783. bdev->bd_inode->i_data.backing_dev_info = bdi;
  784. }
  785. if (bdev->bd_invalidated)
  786. rescan_partitions(disk, bdev);
  787. } else {
  788. struct hd_struct *p;
  789. struct block_device *whole;
  790. whole = bdget_disk(disk, 0);
  791. ret = -ENOMEM;
  792. if (!whole)
  793. goto out_first;
  794. ret = blkdev_get(whole, file->f_mode, file->f_flags);
  795. if (ret)
  796. goto out_first;
  797. bdev->bd_contains = whole;
  798. mutex_lock(&whole->bd_mutex);
  799. whole->bd_part_count++;
  800. p = disk->part[part - 1];
  801. bdev->bd_inode->i_data.backing_dev_info =
  802. whole->bd_inode->i_data.backing_dev_info;
  803. if (!(disk->flags & GENHD_FL_UP) || !p || !p->nr_sects) {
  804. whole->bd_part_count--;
  805. mutex_unlock(&whole->bd_mutex);
  806. ret = -ENXIO;
  807. goto out_first;
  808. }
  809. kobject_get(&p->kobj);
  810. bdev->bd_part = p;
  811. bd_set_size(bdev, (loff_t) p->nr_sects << 9);
  812. mutex_unlock(&whole->bd_mutex);
  813. }
  814. } else {
  815. put_disk(disk);
  816. module_put(owner);
  817. if (bdev->bd_contains == bdev) {
  818. if (bdev->bd_disk->fops->open) {
  819. ret = bdev->bd_disk->fops->open(bdev->bd_inode, file);
  820. if (ret)
  821. goto out;
  822. }
  823. if (bdev->bd_invalidated)
  824. rescan_partitions(bdev->bd_disk, bdev);
  825. } else {
  826. mutex_lock(&bdev->bd_contains->bd_mutex);
  827. bdev->bd_contains->bd_part_count++;
  828. mutex_unlock(&bdev->bd_contains->bd_mutex);
  829. }
  830. }
  831. bdev->bd_openers++;
  832. mutex_unlock(&bdev->bd_mutex);
  833. unlock_kernel();
  834. return 0;
  835. out_first:
  836. bdev->bd_disk = NULL;
  837. bdev->bd_inode->i_data.backing_dev_info = &default_backing_dev_info;
  838. if (bdev != bdev->bd_contains)
  839. blkdev_put(bdev->bd_contains);
  840. bdev->bd_contains = NULL;
  841. put_disk(disk);
  842. module_put(owner);
  843. out:
  844. mutex_unlock(&bdev->bd_mutex);
  845. unlock_kernel();
  846. if (ret)
  847. bdput(bdev);
  848. return ret;
  849. }
  850. int blkdev_get(struct block_device *bdev, mode_t mode, unsigned flags)
  851. {
  852. /*
  853. * This crockload is due to bad choice of ->open() type.
  854. * It will go away.
  855. * For now, block device ->open() routine must _not_
  856. * examine anything in 'inode' argument except ->i_rdev.
  857. */
  858. struct file fake_file = {};
  859. struct dentry fake_dentry = {};
  860. fake_file.f_mode = mode;
  861. fake_file.f_flags = flags;
  862. fake_file.f_dentry = &fake_dentry;
  863. fake_dentry.d_inode = bdev->bd_inode;
  864. return do_open(bdev, &fake_file);
  865. }
  866. EXPORT_SYMBOL(blkdev_get);
  867. static int blkdev_open(struct inode * inode, struct file * filp)
  868. {
  869. struct block_device *bdev;
  870. int res;
  871. /*
  872. * Preserve backwards compatibility and allow large file access
  873. * even if userspace doesn't ask for it explicitly. Some mkfs
  874. * binary needs it. We might want to drop this workaround
  875. * during an unstable branch.
  876. */
  877. filp->f_flags |= O_LARGEFILE;
  878. bdev = bd_acquire(inode);
  879. res = do_open(bdev, filp);
  880. if (res)
  881. return res;
  882. if (!(filp->f_flags & O_EXCL) )
  883. return 0;
  884. if (!(res = bd_claim(bdev, filp)))
  885. return 0;
  886. blkdev_put(bdev);
  887. return res;
  888. }
  889. int blkdev_put(struct block_device *bdev)
  890. {
  891. int ret = 0;
  892. struct inode *bd_inode = bdev->bd_inode;
  893. struct gendisk *disk = bdev->bd_disk;
  894. mutex_lock(&bdev->bd_mutex);
  895. lock_kernel();
  896. if (!--bdev->bd_openers) {
  897. sync_blockdev(bdev);
  898. kill_bdev(bdev);
  899. }
  900. if (bdev->bd_contains == bdev) {
  901. if (disk->fops->release)
  902. ret = disk->fops->release(bd_inode, NULL);
  903. } else {
  904. mutex_lock(&bdev->bd_contains->bd_mutex);
  905. bdev->bd_contains->bd_part_count--;
  906. mutex_unlock(&bdev->bd_contains->bd_mutex);
  907. }
  908. if (!bdev->bd_openers) {
  909. struct module *owner = disk->fops->owner;
  910. put_disk(disk);
  911. module_put(owner);
  912. if (bdev->bd_contains != bdev) {
  913. kobject_put(&bdev->bd_part->kobj);
  914. bdev->bd_part = NULL;
  915. }
  916. bdev->bd_disk = NULL;
  917. bdev->bd_inode->i_data.backing_dev_info = &default_backing_dev_info;
  918. if (bdev != bdev->bd_contains) {
  919. blkdev_put(bdev->bd_contains);
  920. }
  921. bdev->bd_contains = NULL;
  922. }
  923. unlock_kernel();
  924. mutex_unlock(&bdev->bd_mutex);
  925. bdput(bdev);
  926. return ret;
  927. }
  928. EXPORT_SYMBOL(blkdev_put);
  929. static int blkdev_close(struct inode * inode, struct file * filp)
  930. {
  931. struct block_device *bdev = I_BDEV(filp->f_mapping->host);
  932. if (bdev->bd_holder == filp)
  933. bd_release(bdev);
  934. return blkdev_put(bdev);
  935. }
  936. static ssize_t blkdev_file_write(struct file *file, const char __user *buf,
  937. size_t count, loff_t *ppos)
  938. {
  939. struct iovec local_iov = { .iov_base = (void __user *)buf, .iov_len = count };
  940. return generic_file_write_nolock(file, &local_iov, 1, ppos);
  941. }
  942. static ssize_t blkdev_file_aio_write(struct kiocb *iocb, const char __user *buf,
  943. size_t count, loff_t pos)
  944. {
  945. struct iovec local_iov = { .iov_base = (void __user *)buf, .iov_len = count };
  946. return generic_file_aio_write_nolock(iocb, &local_iov, 1, &iocb->ki_pos);
  947. }
  948. static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg)
  949. {
  950. return blkdev_ioctl(file->f_mapping->host, file, cmd, arg);
  951. }
  952. struct address_space_operations def_blk_aops = {
  953. .readpage = blkdev_readpage,
  954. .writepage = blkdev_writepage,
  955. .sync_page = block_sync_page,
  956. .prepare_write = blkdev_prepare_write,
  957. .commit_write = blkdev_commit_write,
  958. .writepages = generic_writepages,
  959. .direct_IO = blkdev_direct_IO,
  960. };
  961. const struct file_operations def_blk_fops = {
  962. .open = blkdev_open,
  963. .release = blkdev_close,
  964. .llseek = block_llseek,
  965. .read = generic_file_read,
  966. .write = blkdev_file_write,
  967. .aio_read = generic_file_aio_read,
  968. .aio_write = blkdev_file_aio_write,
  969. .mmap = generic_file_mmap,
  970. .fsync = block_fsync,
  971. .unlocked_ioctl = block_ioctl,
  972. #ifdef CONFIG_COMPAT
  973. .compat_ioctl = compat_blkdev_ioctl,
  974. #endif
  975. .readv = generic_file_readv,
  976. .writev = generic_file_write_nolock,
  977. .sendfile = generic_file_sendfile,
  978. .splice_read = generic_file_splice_read,
  979. .splice_write = generic_file_splice_write,
  980. };
  981. int ioctl_by_bdev(struct block_device *bdev, unsigned cmd, unsigned long arg)
  982. {
  983. int res;
  984. mm_segment_t old_fs = get_fs();
  985. set_fs(KERNEL_DS);
  986. res = blkdev_ioctl(bdev->bd_inode, NULL, cmd, arg);
  987. set_fs(old_fs);
  988. return res;
  989. }
  990. EXPORT_SYMBOL(ioctl_by_bdev);
  991. /**
  992. * lookup_bdev - lookup a struct block_device by name
  993. *
  994. * @path: special file representing the block device
  995. *
  996. * Get a reference to the blockdevice at @path in the current
  997. * namespace if possible and return it. Return ERR_PTR(error)
  998. * otherwise.
  999. */
  1000. struct block_device *lookup_bdev(const char *path)
  1001. {
  1002. struct block_device *bdev;
  1003. struct inode *inode;
  1004. struct nameidata nd;
  1005. int error;
  1006. if (!path || !*path)
  1007. return ERR_PTR(-EINVAL);
  1008. error = path_lookup(path, LOOKUP_FOLLOW, &nd);
  1009. if (error)
  1010. return ERR_PTR(error);
  1011. inode = nd.dentry->d_inode;
  1012. error = -ENOTBLK;
  1013. if (!S_ISBLK(inode->i_mode))
  1014. goto fail;
  1015. error = -EACCES;
  1016. if (nd.mnt->mnt_flags & MNT_NODEV)
  1017. goto fail;
  1018. error = -ENOMEM;
  1019. bdev = bd_acquire(inode);
  1020. if (!bdev)
  1021. goto fail;
  1022. out:
  1023. path_release(&nd);
  1024. return bdev;
  1025. fail:
  1026. bdev = ERR_PTR(error);
  1027. goto out;
  1028. }
  1029. /**
  1030. * open_bdev_excl - open a block device by name and set it up for use
  1031. *
  1032. * @path: special file representing the block device
  1033. * @flags: %MS_RDONLY for opening read-only
  1034. * @holder: owner for exclusion
  1035. *
  1036. * Open the blockdevice described by the special file at @path, claim it
  1037. * for the @holder.
  1038. */
  1039. struct block_device *open_bdev_excl(const char *path, int flags, void *holder)
  1040. {
  1041. struct block_device *bdev;
  1042. mode_t mode = FMODE_READ;
  1043. int error = 0;
  1044. bdev = lookup_bdev(path);
  1045. if (IS_ERR(bdev))
  1046. return bdev;
  1047. if (!(flags & MS_RDONLY))
  1048. mode |= FMODE_WRITE;
  1049. error = blkdev_get(bdev, mode, 0);
  1050. if (error)
  1051. return ERR_PTR(error);
  1052. error = -EACCES;
  1053. if (!(flags & MS_RDONLY) && bdev_read_only(bdev))
  1054. goto blkdev_put;
  1055. error = bd_claim(bdev, holder);
  1056. if (error)
  1057. goto blkdev_put;
  1058. return bdev;
  1059. blkdev_put:
  1060. blkdev_put(bdev);
  1061. return ERR_PTR(error);
  1062. }
  1063. EXPORT_SYMBOL(open_bdev_excl);
  1064. /**
  1065. * close_bdev_excl - release a blockdevice openen by open_bdev_excl()
  1066. *
  1067. * @bdev: blockdevice to close
  1068. *
  1069. * This is the counterpart to open_bdev_excl().
  1070. */
  1071. void close_bdev_excl(struct block_device *bdev)
  1072. {
  1073. bd_release(bdev);
  1074. blkdev_put(bdev);
  1075. }
  1076. EXPORT_SYMBOL(close_bdev_excl);