block_dev.c 35 KB

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