block_dev.c 42 KB

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