block_dev.c 34 KB

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