block_dev.c 35 KB

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