block_dev.c 37 KB

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