block_dev.c 38 KB

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