block_dev.c 34 KB

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