block_dev.c 35 KB

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