block_dev.c 35 KB

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