block_dev.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  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. unsigned long max_blocks, struct buffer_head *bh, int create)
  116. {
  117. sector_t end_block = max_block(I_BDEV(inode));
  118. if ((iblock + max_blocks) > end_block) {
  119. max_blocks = end_block - iblock;
  120. if ((long)max_blocks <= 0) {
  121. if (create)
  122. return -EIO; /* write fully beyond EOF */
  123. /*
  124. * It is a read which is fully beyond EOF. We return
  125. * a !buffer_mapped buffer
  126. */
  127. max_blocks = 0;
  128. }
  129. }
  130. bh->b_bdev = I_BDEV(inode);
  131. bh->b_blocknr = iblock;
  132. bh->b_size = max_blocks << inode->i_blkbits;
  133. if (max_blocks)
  134. set_buffer_mapped(bh);
  135. return 0;
  136. }
  137. static ssize_t
  138. blkdev_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
  139. loff_t offset, unsigned long nr_segs)
  140. {
  141. struct file *file = iocb->ki_filp;
  142. struct inode *inode = file->f_mapping->host;
  143. return blockdev_direct_IO_no_locking(rw, iocb, inode, I_BDEV(inode),
  144. iov, offset, nr_segs, blkdev_get_blocks, NULL);
  145. }
  146. static int blkdev_writepage(struct page *page, struct writeback_control *wbc)
  147. {
  148. return block_write_full_page(page, blkdev_get_block, wbc);
  149. }
  150. static int blkdev_readpage(struct file * file, struct page * page)
  151. {
  152. return block_read_full_page(page, blkdev_get_block);
  153. }
  154. static int blkdev_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to)
  155. {
  156. return block_prepare_write(page, from, to, blkdev_get_block);
  157. }
  158. static int blkdev_commit_write(struct file *file, struct page *page, unsigned from, unsigned to)
  159. {
  160. return block_commit_write(page, from, to);
  161. }
  162. /*
  163. * private llseek:
  164. * for a block special file file->f_dentry->d_inode->i_size is zero
  165. * so we compute the size by hand (just as in block_read/write above)
  166. */
  167. static loff_t block_llseek(struct file *file, loff_t offset, int origin)
  168. {
  169. struct inode *bd_inode = file->f_mapping->host;
  170. loff_t size;
  171. loff_t retval;
  172. mutex_lock(&bd_inode->i_mutex);
  173. size = i_size_read(bd_inode);
  174. switch (origin) {
  175. case 2:
  176. offset += size;
  177. break;
  178. case 1:
  179. offset += file->f_pos;
  180. }
  181. retval = -EINVAL;
  182. if (offset >= 0 && offset <= size) {
  183. if (offset != file->f_pos) {
  184. file->f_pos = offset;
  185. }
  186. retval = offset;
  187. }
  188. mutex_unlock(&bd_inode->i_mutex);
  189. return retval;
  190. }
  191. /*
  192. * Filp is never NULL; the only case when ->fsync() is called with
  193. * NULL first argument is nfsd_sync_dir() and that's not a directory.
  194. */
  195. static int block_fsync(struct file *filp, struct dentry *dentry, int datasync)
  196. {
  197. return sync_blockdev(I_BDEV(filp->f_mapping->host));
  198. }
  199. /*
  200. * pseudo-fs
  201. */
  202. static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);
  203. static kmem_cache_t * bdev_cachep;
  204. static struct inode *bdev_alloc_inode(struct super_block *sb)
  205. {
  206. struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, SLAB_KERNEL);
  207. if (!ei)
  208. return NULL;
  209. return &ei->vfs_inode;
  210. }
  211. static void bdev_destroy_inode(struct inode *inode)
  212. {
  213. struct bdev_inode *bdi = BDEV_I(inode);
  214. bdi->bdev.bd_inode_backing_dev_info = NULL;
  215. kmem_cache_free(bdev_cachep, bdi);
  216. }
  217. static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
  218. {
  219. struct bdev_inode *ei = (struct bdev_inode *) foo;
  220. struct block_device *bdev = &ei->bdev;
  221. if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
  222. SLAB_CTOR_CONSTRUCTOR)
  223. {
  224. memset(bdev, 0, sizeof(*bdev));
  225. mutex_init(&bdev->bd_mutex);
  226. mutex_init(&bdev->bd_mount_mutex);
  227. INIT_LIST_HEAD(&bdev->bd_inodes);
  228. INIT_LIST_HEAD(&bdev->bd_list);
  229. inode_init_once(&ei->vfs_inode);
  230. }
  231. }
  232. static inline void __bd_forget(struct inode *inode)
  233. {
  234. list_del_init(&inode->i_devices);
  235. inode->i_bdev = NULL;
  236. inode->i_mapping = &inode->i_data;
  237. }
  238. static void bdev_clear_inode(struct inode *inode)
  239. {
  240. struct block_device *bdev = &BDEV_I(inode)->bdev;
  241. struct list_head *p;
  242. spin_lock(&bdev_lock);
  243. while ( (p = bdev->bd_inodes.next) != &bdev->bd_inodes ) {
  244. __bd_forget(list_entry(p, struct inode, i_devices));
  245. }
  246. list_del_init(&bdev->bd_list);
  247. spin_unlock(&bdev_lock);
  248. }
  249. static struct super_operations bdev_sops = {
  250. .statfs = simple_statfs,
  251. .alloc_inode = bdev_alloc_inode,
  252. .destroy_inode = bdev_destroy_inode,
  253. .drop_inode = generic_delete_inode,
  254. .clear_inode = bdev_clear_inode,
  255. };
  256. static struct super_block *bd_get_sb(struct file_system_type *fs_type,
  257. int flags, const char *dev_name, void *data)
  258. {
  259. return get_sb_pseudo(fs_type, "bdev:", &bdev_sops, 0x62646576);
  260. }
  261. static struct file_system_type bd_type = {
  262. .name = "bdev",
  263. .get_sb = bd_get_sb,
  264. .kill_sb = kill_anon_super,
  265. };
  266. static struct vfsmount *bd_mnt;
  267. struct super_block *blockdev_superblock;
  268. void __init bdev_cache_init(void)
  269. {
  270. int err;
  271. bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
  272. 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
  273. SLAB_MEM_SPREAD|SLAB_PANIC),
  274. init_once, NULL);
  275. err = register_filesystem(&bd_type);
  276. if (err)
  277. panic("Cannot register bdev pseudo-fs");
  278. bd_mnt = kern_mount(&bd_type);
  279. err = PTR_ERR(bd_mnt);
  280. if (IS_ERR(bd_mnt))
  281. panic("Cannot create bdev pseudo-fs");
  282. blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */
  283. }
  284. /*
  285. * Most likely _very_ bad one - but then it's hardly critical for small
  286. * /dev and can be fixed when somebody will need really large one.
  287. * Keep in mind that it will be fed through icache hash function too.
  288. */
  289. static inline unsigned long hash(dev_t dev)
  290. {
  291. return MAJOR(dev)+MINOR(dev);
  292. }
  293. static int bdev_test(struct inode *inode, void *data)
  294. {
  295. return BDEV_I(inode)->bdev.bd_dev == *(dev_t *)data;
  296. }
  297. static int bdev_set(struct inode *inode, void *data)
  298. {
  299. BDEV_I(inode)->bdev.bd_dev = *(dev_t *)data;
  300. return 0;
  301. }
  302. static LIST_HEAD(all_bdevs);
  303. struct block_device *bdget(dev_t dev)
  304. {
  305. struct block_device *bdev;
  306. struct inode *inode;
  307. inode = iget5_locked(bd_mnt->mnt_sb, hash(dev),
  308. bdev_test, bdev_set, &dev);
  309. if (!inode)
  310. return NULL;
  311. bdev = &BDEV_I(inode)->bdev;
  312. if (inode->i_state & I_NEW) {
  313. bdev->bd_contains = NULL;
  314. bdev->bd_inode = inode;
  315. bdev->bd_block_size = (1 << inode->i_blkbits);
  316. bdev->bd_part_count = 0;
  317. bdev->bd_invalidated = 0;
  318. inode->i_mode = S_IFBLK;
  319. inode->i_rdev = dev;
  320. inode->i_bdev = bdev;
  321. inode->i_data.a_ops = &def_blk_aops;
  322. mapping_set_gfp_mask(&inode->i_data, GFP_USER);
  323. inode->i_data.backing_dev_info = &default_backing_dev_info;
  324. spin_lock(&bdev_lock);
  325. list_add(&bdev->bd_list, &all_bdevs);
  326. spin_unlock(&bdev_lock);
  327. unlock_new_inode(inode);
  328. }
  329. return bdev;
  330. }
  331. EXPORT_SYMBOL(bdget);
  332. long nr_blockdev_pages(void)
  333. {
  334. struct list_head *p;
  335. long ret = 0;
  336. spin_lock(&bdev_lock);
  337. list_for_each(p, &all_bdevs) {
  338. struct block_device *bdev;
  339. bdev = list_entry(p, struct block_device, bd_list);
  340. ret += bdev->bd_inode->i_mapping->nrpages;
  341. }
  342. spin_unlock(&bdev_lock);
  343. return ret;
  344. }
  345. void bdput(struct block_device *bdev)
  346. {
  347. iput(bdev->bd_inode);
  348. }
  349. EXPORT_SYMBOL(bdput);
  350. static struct block_device *bd_acquire(struct inode *inode)
  351. {
  352. struct block_device *bdev;
  353. spin_lock(&bdev_lock);
  354. bdev = inode->i_bdev;
  355. if (bdev && igrab(bdev->bd_inode)) {
  356. spin_unlock(&bdev_lock);
  357. return bdev;
  358. }
  359. spin_unlock(&bdev_lock);
  360. bdev = bdget(inode->i_rdev);
  361. if (bdev) {
  362. spin_lock(&bdev_lock);
  363. if (inode->i_bdev)
  364. __bd_forget(inode);
  365. inode->i_bdev = bdev;
  366. inode->i_mapping = bdev->bd_inode->i_mapping;
  367. list_add(&inode->i_devices, &bdev->bd_inodes);
  368. spin_unlock(&bdev_lock);
  369. }
  370. return bdev;
  371. }
  372. /* Call when you free inode */
  373. void bd_forget(struct inode *inode)
  374. {
  375. spin_lock(&bdev_lock);
  376. if (inode->i_bdev)
  377. __bd_forget(inode);
  378. spin_unlock(&bdev_lock);
  379. }
  380. int bd_claim(struct block_device *bdev, void *holder)
  381. {
  382. int res;
  383. spin_lock(&bdev_lock);
  384. /* first decide result */
  385. if (bdev->bd_holder == holder)
  386. res = 0; /* already a holder */
  387. else if (bdev->bd_holder != NULL)
  388. res = -EBUSY; /* held by someone else */
  389. else if (bdev->bd_contains == bdev)
  390. res = 0; /* is a whole device which isn't held */
  391. else if (bdev->bd_contains->bd_holder == bd_claim)
  392. res = 0; /* is a partition of a device that is being partitioned */
  393. else if (bdev->bd_contains->bd_holder != NULL)
  394. res = -EBUSY; /* is a partition of a held device */
  395. else
  396. res = 0; /* is a partition of an un-held device */
  397. /* now impose change */
  398. if (res==0) {
  399. /* note that for a whole device bd_holders
  400. * will be incremented twice, and bd_holder will
  401. * be set to bd_claim before being set to holder
  402. */
  403. bdev->bd_contains->bd_holders ++;
  404. bdev->bd_contains->bd_holder = bd_claim;
  405. bdev->bd_holders++;
  406. bdev->bd_holder = holder;
  407. }
  408. spin_unlock(&bdev_lock);
  409. return res;
  410. }
  411. EXPORT_SYMBOL(bd_claim);
  412. void bd_release(struct block_device *bdev)
  413. {
  414. spin_lock(&bdev_lock);
  415. if (!--bdev->bd_contains->bd_holders)
  416. bdev->bd_contains->bd_holder = NULL;
  417. if (!--bdev->bd_holders)
  418. bdev->bd_holder = NULL;
  419. spin_unlock(&bdev_lock);
  420. }
  421. EXPORT_SYMBOL(bd_release);
  422. /*
  423. * Tries to open block device by device number. Use it ONLY if you
  424. * really do not have anything better - i.e. when you are behind a
  425. * truly sucky interface and all you are given is a device number. _Never_
  426. * to be used for internal purposes. If you ever need it - reconsider
  427. * your API.
  428. */
  429. struct block_device *open_by_devnum(dev_t dev, unsigned mode)
  430. {
  431. struct block_device *bdev = bdget(dev);
  432. int err = -ENOMEM;
  433. int flags = mode & FMODE_WRITE ? O_RDWR : O_RDONLY;
  434. if (bdev)
  435. err = blkdev_get(bdev, mode, flags);
  436. return err ? ERR_PTR(err) : bdev;
  437. }
  438. EXPORT_SYMBOL(open_by_devnum);
  439. /*
  440. * This routine checks whether a removable media has been changed,
  441. * and invalidates all buffer-cache-entries in that case. This
  442. * is a relatively slow routine, so we have to try to minimize using
  443. * it. Thus it is called only upon a 'mount' or 'open'. This
  444. * is the best way of combining speed and utility, I think.
  445. * People changing diskettes in the middle of an operation deserve
  446. * to lose :-)
  447. */
  448. int check_disk_change(struct block_device *bdev)
  449. {
  450. struct gendisk *disk = bdev->bd_disk;
  451. struct block_device_operations * bdops = disk->fops;
  452. if (!bdops->media_changed)
  453. return 0;
  454. if (!bdops->media_changed(bdev->bd_disk))
  455. return 0;
  456. if (__invalidate_device(bdev))
  457. printk("VFS: busy inodes on changed media.\n");
  458. if (bdops->revalidate_disk)
  459. bdops->revalidate_disk(bdev->bd_disk);
  460. if (bdev->bd_disk->minors > 1)
  461. bdev->bd_invalidated = 1;
  462. return 1;
  463. }
  464. EXPORT_SYMBOL(check_disk_change);
  465. void bd_set_size(struct block_device *bdev, loff_t size)
  466. {
  467. unsigned bsize = bdev_hardsect_size(bdev);
  468. bdev->bd_inode->i_size = size;
  469. while (bsize < PAGE_CACHE_SIZE) {
  470. if (size & bsize)
  471. break;
  472. bsize <<= 1;
  473. }
  474. bdev->bd_block_size = bsize;
  475. bdev->bd_inode->i_blkbits = blksize_bits(bsize);
  476. }
  477. EXPORT_SYMBOL(bd_set_size);
  478. static int do_open(struct block_device *bdev, struct file *file)
  479. {
  480. struct module *owner = NULL;
  481. struct gendisk *disk;
  482. int ret = -ENXIO;
  483. int part;
  484. file->f_mapping = bdev->bd_inode->i_mapping;
  485. lock_kernel();
  486. disk = get_gendisk(bdev->bd_dev, &part);
  487. if (!disk) {
  488. unlock_kernel();
  489. bdput(bdev);
  490. return ret;
  491. }
  492. owner = disk->fops->owner;
  493. mutex_lock(&bdev->bd_mutex);
  494. if (!bdev->bd_openers) {
  495. bdev->bd_disk = disk;
  496. bdev->bd_contains = bdev;
  497. if (!part) {
  498. struct backing_dev_info *bdi;
  499. if (disk->fops->open) {
  500. ret = disk->fops->open(bdev->bd_inode, file);
  501. if (ret)
  502. goto out_first;
  503. }
  504. if (!bdev->bd_openers) {
  505. bd_set_size(bdev,(loff_t)get_capacity(disk)<<9);
  506. bdi = blk_get_backing_dev_info(bdev);
  507. if (bdi == NULL)
  508. bdi = &default_backing_dev_info;
  509. bdev->bd_inode->i_data.backing_dev_info = bdi;
  510. }
  511. if (bdev->bd_invalidated)
  512. rescan_partitions(disk, bdev);
  513. } else {
  514. struct hd_struct *p;
  515. struct block_device *whole;
  516. whole = bdget_disk(disk, 0);
  517. ret = -ENOMEM;
  518. if (!whole)
  519. goto out_first;
  520. ret = blkdev_get(whole, file->f_mode, file->f_flags);
  521. if (ret)
  522. goto out_first;
  523. bdev->bd_contains = whole;
  524. mutex_lock(&whole->bd_mutex);
  525. whole->bd_part_count++;
  526. p = disk->part[part - 1];
  527. bdev->bd_inode->i_data.backing_dev_info =
  528. whole->bd_inode->i_data.backing_dev_info;
  529. if (!(disk->flags & GENHD_FL_UP) || !p || !p->nr_sects) {
  530. whole->bd_part_count--;
  531. mutex_unlock(&whole->bd_mutex);
  532. ret = -ENXIO;
  533. goto out_first;
  534. }
  535. kobject_get(&p->kobj);
  536. bdev->bd_part = p;
  537. bd_set_size(bdev, (loff_t) p->nr_sects << 9);
  538. mutex_unlock(&whole->bd_mutex);
  539. }
  540. } else {
  541. put_disk(disk);
  542. module_put(owner);
  543. if (bdev->bd_contains == bdev) {
  544. if (bdev->bd_disk->fops->open) {
  545. ret = bdev->bd_disk->fops->open(bdev->bd_inode, file);
  546. if (ret)
  547. goto out;
  548. }
  549. if (bdev->bd_invalidated)
  550. rescan_partitions(bdev->bd_disk, bdev);
  551. } else {
  552. mutex_lock(&bdev->bd_contains->bd_mutex);
  553. bdev->bd_contains->bd_part_count++;
  554. mutex_unlock(&bdev->bd_contains->bd_mutex);
  555. }
  556. }
  557. bdev->bd_openers++;
  558. mutex_unlock(&bdev->bd_mutex);
  559. unlock_kernel();
  560. return 0;
  561. out_first:
  562. bdev->bd_disk = NULL;
  563. bdev->bd_inode->i_data.backing_dev_info = &default_backing_dev_info;
  564. if (bdev != bdev->bd_contains)
  565. blkdev_put(bdev->bd_contains);
  566. bdev->bd_contains = NULL;
  567. put_disk(disk);
  568. module_put(owner);
  569. out:
  570. mutex_unlock(&bdev->bd_mutex);
  571. unlock_kernel();
  572. if (ret)
  573. bdput(bdev);
  574. return ret;
  575. }
  576. int blkdev_get(struct block_device *bdev, mode_t mode, unsigned flags)
  577. {
  578. /*
  579. * This crockload is due to bad choice of ->open() type.
  580. * It will go away.
  581. * For now, block device ->open() routine must _not_
  582. * examine anything in 'inode' argument except ->i_rdev.
  583. */
  584. struct file fake_file = {};
  585. struct dentry fake_dentry = {};
  586. fake_file.f_mode = mode;
  587. fake_file.f_flags = flags;
  588. fake_file.f_dentry = &fake_dentry;
  589. fake_dentry.d_inode = bdev->bd_inode;
  590. return do_open(bdev, &fake_file);
  591. }
  592. EXPORT_SYMBOL(blkdev_get);
  593. static int blkdev_open(struct inode * inode, struct file * filp)
  594. {
  595. struct block_device *bdev;
  596. int res;
  597. /*
  598. * Preserve backwards compatibility and allow large file access
  599. * even if userspace doesn't ask for it explicitly. Some mkfs
  600. * binary needs it. We might want to drop this workaround
  601. * during an unstable branch.
  602. */
  603. filp->f_flags |= O_LARGEFILE;
  604. bdev = bd_acquire(inode);
  605. res = do_open(bdev, filp);
  606. if (res)
  607. return res;
  608. if (!(filp->f_flags & O_EXCL) )
  609. return 0;
  610. if (!(res = bd_claim(bdev, filp)))
  611. return 0;
  612. blkdev_put(bdev);
  613. return res;
  614. }
  615. int blkdev_put(struct block_device *bdev)
  616. {
  617. int ret = 0;
  618. struct inode *bd_inode = bdev->bd_inode;
  619. struct gendisk *disk = bdev->bd_disk;
  620. mutex_lock(&bdev->bd_mutex);
  621. lock_kernel();
  622. if (!--bdev->bd_openers) {
  623. sync_blockdev(bdev);
  624. kill_bdev(bdev);
  625. }
  626. if (bdev->bd_contains == bdev) {
  627. if (disk->fops->release)
  628. ret = disk->fops->release(bd_inode, NULL);
  629. } else {
  630. mutex_lock(&bdev->bd_contains->bd_mutex);
  631. bdev->bd_contains->bd_part_count--;
  632. mutex_unlock(&bdev->bd_contains->bd_mutex);
  633. }
  634. if (!bdev->bd_openers) {
  635. struct module *owner = disk->fops->owner;
  636. put_disk(disk);
  637. module_put(owner);
  638. if (bdev->bd_contains != bdev) {
  639. kobject_put(&bdev->bd_part->kobj);
  640. bdev->bd_part = NULL;
  641. }
  642. bdev->bd_disk = NULL;
  643. bdev->bd_inode->i_data.backing_dev_info = &default_backing_dev_info;
  644. if (bdev != bdev->bd_contains) {
  645. blkdev_put(bdev->bd_contains);
  646. }
  647. bdev->bd_contains = NULL;
  648. }
  649. unlock_kernel();
  650. mutex_unlock(&bdev->bd_mutex);
  651. bdput(bdev);
  652. return ret;
  653. }
  654. EXPORT_SYMBOL(blkdev_put);
  655. static int blkdev_close(struct inode * inode, struct file * filp)
  656. {
  657. struct block_device *bdev = I_BDEV(filp->f_mapping->host);
  658. if (bdev->bd_holder == filp)
  659. bd_release(bdev);
  660. return blkdev_put(bdev);
  661. }
  662. static ssize_t blkdev_file_write(struct file *file, const char __user *buf,
  663. size_t count, loff_t *ppos)
  664. {
  665. struct iovec local_iov = { .iov_base = (void __user *)buf, .iov_len = count };
  666. return generic_file_write_nolock(file, &local_iov, 1, ppos);
  667. }
  668. static ssize_t blkdev_file_aio_write(struct kiocb *iocb, const char __user *buf,
  669. size_t count, loff_t pos)
  670. {
  671. struct iovec local_iov = { .iov_base = (void __user *)buf, .iov_len = count };
  672. return generic_file_aio_write_nolock(iocb, &local_iov, 1, &iocb->ki_pos);
  673. }
  674. static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg)
  675. {
  676. return blkdev_ioctl(file->f_mapping->host, file, cmd, arg);
  677. }
  678. struct address_space_operations def_blk_aops = {
  679. .readpage = blkdev_readpage,
  680. .writepage = blkdev_writepage,
  681. .sync_page = block_sync_page,
  682. .prepare_write = blkdev_prepare_write,
  683. .commit_write = blkdev_commit_write,
  684. .writepages = generic_writepages,
  685. .direct_IO = blkdev_direct_IO,
  686. };
  687. struct file_operations def_blk_fops = {
  688. .open = blkdev_open,
  689. .release = blkdev_close,
  690. .llseek = block_llseek,
  691. .read = generic_file_read,
  692. .write = blkdev_file_write,
  693. .aio_read = generic_file_aio_read,
  694. .aio_write = blkdev_file_aio_write,
  695. .mmap = generic_file_mmap,
  696. .fsync = block_fsync,
  697. .unlocked_ioctl = block_ioctl,
  698. #ifdef CONFIG_COMPAT
  699. .compat_ioctl = compat_blkdev_ioctl,
  700. #endif
  701. .readv = generic_file_readv,
  702. .writev = generic_file_write_nolock,
  703. .sendfile = generic_file_sendfile,
  704. };
  705. int ioctl_by_bdev(struct block_device *bdev, unsigned cmd, unsigned long arg)
  706. {
  707. int res;
  708. mm_segment_t old_fs = get_fs();
  709. set_fs(KERNEL_DS);
  710. res = blkdev_ioctl(bdev->bd_inode, NULL, cmd, arg);
  711. set_fs(old_fs);
  712. return res;
  713. }
  714. EXPORT_SYMBOL(ioctl_by_bdev);
  715. /**
  716. * lookup_bdev - lookup a struct block_device by name
  717. *
  718. * @path: special file representing the block device
  719. *
  720. * Get a reference to the blockdevice at @path in the current
  721. * namespace if possible and return it. Return ERR_PTR(error)
  722. * otherwise.
  723. */
  724. struct block_device *lookup_bdev(const char *path)
  725. {
  726. struct block_device *bdev;
  727. struct inode *inode;
  728. struct nameidata nd;
  729. int error;
  730. if (!path || !*path)
  731. return ERR_PTR(-EINVAL);
  732. error = path_lookup(path, LOOKUP_FOLLOW, &nd);
  733. if (error)
  734. return ERR_PTR(error);
  735. inode = nd.dentry->d_inode;
  736. error = -ENOTBLK;
  737. if (!S_ISBLK(inode->i_mode))
  738. goto fail;
  739. error = -EACCES;
  740. if (nd.mnt->mnt_flags & MNT_NODEV)
  741. goto fail;
  742. error = -ENOMEM;
  743. bdev = bd_acquire(inode);
  744. if (!bdev)
  745. goto fail;
  746. out:
  747. path_release(&nd);
  748. return bdev;
  749. fail:
  750. bdev = ERR_PTR(error);
  751. goto out;
  752. }
  753. /**
  754. * open_bdev_excl - open a block device by name and set it up for use
  755. *
  756. * @path: special file representing the block device
  757. * @flags: %MS_RDONLY for opening read-only
  758. * @holder: owner for exclusion
  759. *
  760. * Open the blockdevice described by the special file at @path, claim it
  761. * for the @holder.
  762. */
  763. struct block_device *open_bdev_excl(const char *path, int flags, void *holder)
  764. {
  765. struct block_device *bdev;
  766. mode_t mode = FMODE_READ;
  767. int error = 0;
  768. bdev = lookup_bdev(path);
  769. if (IS_ERR(bdev))
  770. return bdev;
  771. if (!(flags & MS_RDONLY))
  772. mode |= FMODE_WRITE;
  773. error = blkdev_get(bdev, mode, 0);
  774. if (error)
  775. return ERR_PTR(error);
  776. error = -EACCES;
  777. if (!(flags & MS_RDONLY) && bdev_read_only(bdev))
  778. goto blkdev_put;
  779. error = bd_claim(bdev, holder);
  780. if (error)
  781. goto blkdev_put;
  782. return bdev;
  783. blkdev_put:
  784. blkdev_put(bdev);
  785. return ERR_PTR(error);
  786. }
  787. EXPORT_SYMBOL(open_bdev_excl);
  788. /**
  789. * close_bdev_excl - release a blockdevice openen by open_bdev_excl()
  790. *
  791. * @bdev: blockdevice to close
  792. *
  793. * This is the counterpart to open_bdev_excl().
  794. */
  795. void close_bdev_excl(struct block_device *bdev)
  796. {
  797. bd_release(bdev);
  798. blkdev_put(bdev);
  799. }
  800. EXPORT_SYMBOL(close_bdev_excl);