block_dev.c 38 KB

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