block_dev.c 42 KB

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