block_dev.c 22 KB

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