block_dev.c 35 KB

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