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