block_dev.c 35 KB

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