block_dev.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715
  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->f_path.dentry->d_inode->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. long nr_blockdev_pages(void)
  480. {
  481. struct block_device *bdev;
  482. long ret = 0;
  483. spin_lock(&bdev_lock);
  484. list_for_each_entry(bdev, &all_bdevs, bd_list) {
  485. ret += bdev->bd_inode->i_mapping->nrpages;
  486. }
  487. spin_unlock(&bdev_lock);
  488. return ret;
  489. }
  490. void bdput(struct block_device *bdev)
  491. {
  492. iput(bdev->bd_inode);
  493. }
  494. EXPORT_SYMBOL(bdput);
  495. static struct block_device *bd_acquire(struct inode *inode)
  496. {
  497. struct block_device *bdev;
  498. spin_lock(&bdev_lock);
  499. bdev = inode->i_bdev;
  500. if (bdev) {
  501. ihold(bdev->bd_inode);
  502. spin_unlock(&bdev_lock);
  503. return bdev;
  504. }
  505. spin_unlock(&bdev_lock);
  506. bdev = bdget(inode->i_rdev);
  507. if (bdev) {
  508. spin_lock(&bdev_lock);
  509. if (!inode->i_bdev) {
  510. /*
  511. * We take an additional reference to bd_inode,
  512. * and it's released in clear_inode() of inode.
  513. * So, we can access it via ->i_mapping always
  514. * without igrab().
  515. */
  516. ihold(bdev->bd_inode);
  517. inode->i_bdev = bdev;
  518. inode->i_mapping = bdev->bd_inode->i_mapping;
  519. list_add(&inode->i_devices, &bdev->bd_inodes);
  520. }
  521. spin_unlock(&bdev_lock);
  522. }
  523. return bdev;
  524. }
  525. static inline int sb_is_blkdev_sb(struct super_block *sb)
  526. {
  527. return sb == blockdev_superblock;
  528. }
  529. /* Call when you free inode */
  530. void bd_forget(struct inode *inode)
  531. {
  532. struct block_device *bdev = NULL;
  533. spin_lock(&bdev_lock);
  534. if (inode->i_bdev) {
  535. if (!sb_is_blkdev_sb(inode->i_sb))
  536. bdev = inode->i_bdev;
  537. __bd_forget(inode);
  538. }
  539. spin_unlock(&bdev_lock);
  540. if (bdev)
  541. iput(bdev->bd_inode);
  542. }
  543. /**
  544. * bd_may_claim - test whether a block device can be claimed
  545. * @bdev: block device of interest
  546. * @whole: whole block device containing @bdev, may equal @bdev
  547. * @holder: holder trying to claim @bdev
  548. *
  549. * Test whether @bdev can be claimed by @holder.
  550. *
  551. * CONTEXT:
  552. * spin_lock(&bdev_lock).
  553. *
  554. * RETURNS:
  555. * %true if @bdev can be claimed, %false otherwise.
  556. */
  557. static bool bd_may_claim(struct block_device *bdev, struct block_device *whole,
  558. void *holder)
  559. {
  560. if (bdev->bd_holder == holder)
  561. return true; /* already a holder */
  562. else if (bdev->bd_holder != NULL)
  563. return false; /* held by someone else */
  564. else if (bdev->bd_contains == bdev)
  565. return true; /* is a whole device which isn't held */
  566. else if (whole->bd_holder == bd_may_claim)
  567. return true; /* is a partition of a device that is being partitioned */
  568. else if (whole->bd_holder != NULL)
  569. return false; /* is a partition of a held device */
  570. else
  571. return true; /* is a partition of an un-held device */
  572. }
  573. /**
  574. * bd_prepare_to_claim - prepare to claim a block device
  575. * @bdev: block device of interest
  576. * @whole: the whole device containing @bdev, may equal @bdev
  577. * @holder: holder trying to claim @bdev
  578. *
  579. * Prepare to claim @bdev. This function fails if @bdev is already
  580. * claimed by another holder and waits if another claiming is in
  581. * progress. This function doesn't actually claim. On successful
  582. * return, the caller has ownership of bd_claiming and bd_holder[s].
  583. *
  584. * CONTEXT:
  585. * spin_lock(&bdev_lock). Might release bdev_lock, sleep and regrab
  586. * it multiple times.
  587. *
  588. * RETURNS:
  589. * 0 if @bdev can be claimed, -EBUSY otherwise.
  590. */
  591. static int bd_prepare_to_claim(struct block_device *bdev,
  592. struct block_device *whole, void *holder)
  593. {
  594. retry:
  595. /* if someone else claimed, fail */
  596. if (!bd_may_claim(bdev, whole, holder))
  597. return -EBUSY;
  598. /* if claiming is already in progress, wait for it to finish */
  599. if (whole->bd_claiming) {
  600. wait_queue_head_t *wq = bit_waitqueue(&whole->bd_claiming, 0);
  601. DEFINE_WAIT(wait);
  602. prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
  603. spin_unlock(&bdev_lock);
  604. schedule();
  605. finish_wait(wq, &wait);
  606. spin_lock(&bdev_lock);
  607. goto retry;
  608. }
  609. /* yay, all mine */
  610. return 0;
  611. }
  612. /**
  613. * bd_start_claiming - start claiming a block device
  614. * @bdev: block device of interest
  615. * @holder: holder trying to claim @bdev
  616. *
  617. * @bdev is about to be opened exclusively. Check @bdev can be opened
  618. * exclusively and mark that an exclusive open is in progress. Each
  619. * successful call to this function must be matched with a call to
  620. * either bd_finish_claiming() or bd_abort_claiming() (which do not
  621. * fail).
  622. *
  623. * This function is used to gain exclusive access to the block device
  624. * without actually causing other exclusive open attempts to fail. It
  625. * should be used when the open sequence itself requires exclusive
  626. * access but may subsequently fail.
  627. *
  628. * CONTEXT:
  629. * Might sleep.
  630. *
  631. * RETURNS:
  632. * Pointer to the block device containing @bdev on success, ERR_PTR()
  633. * value on failure.
  634. */
  635. static struct block_device *bd_start_claiming(struct block_device *bdev,
  636. void *holder)
  637. {
  638. struct gendisk *disk;
  639. struct block_device *whole;
  640. int partno, err;
  641. might_sleep();
  642. /*
  643. * @bdev might not have been initialized properly yet, look up
  644. * and grab the outer block device the hard way.
  645. */
  646. disk = get_gendisk(bdev->bd_dev, &partno);
  647. if (!disk)
  648. return ERR_PTR(-ENXIO);
  649. /*
  650. * Normally, @bdev should equal what's returned from bdget_disk()
  651. * if partno is 0; however, some drivers (floppy) use multiple
  652. * bdev's for the same physical device and @bdev may be one of the
  653. * aliases. Keep @bdev if partno is 0. This means claimer
  654. * tracking is broken for those devices but it has always been that
  655. * way.
  656. */
  657. if (partno)
  658. whole = bdget_disk(disk, 0);
  659. else
  660. whole = bdgrab(bdev);
  661. module_put(disk->fops->owner);
  662. put_disk(disk);
  663. if (!whole)
  664. return ERR_PTR(-ENOMEM);
  665. /* prepare to claim, if successful, mark claiming in progress */
  666. spin_lock(&bdev_lock);
  667. err = bd_prepare_to_claim(bdev, whole, holder);
  668. if (err == 0) {
  669. whole->bd_claiming = holder;
  670. spin_unlock(&bdev_lock);
  671. return whole;
  672. } else {
  673. spin_unlock(&bdev_lock);
  674. bdput(whole);
  675. return ERR_PTR(err);
  676. }
  677. }
  678. #ifdef CONFIG_SYSFS
  679. struct bd_holder_disk {
  680. struct list_head list;
  681. struct gendisk *disk;
  682. int refcnt;
  683. };
  684. static struct bd_holder_disk *bd_find_holder_disk(struct block_device *bdev,
  685. struct gendisk *disk)
  686. {
  687. struct bd_holder_disk *holder;
  688. list_for_each_entry(holder, &bdev->bd_holder_disks, list)
  689. if (holder->disk == disk)
  690. return holder;
  691. return NULL;
  692. }
  693. static int add_symlink(struct kobject *from, struct kobject *to)
  694. {
  695. return sysfs_create_link(from, to, kobject_name(to));
  696. }
  697. static void del_symlink(struct kobject *from, struct kobject *to)
  698. {
  699. sysfs_remove_link(from, kobject_name(to));
  700. }
  701. /**
  702. * bd_link_disk_holder - create symlinks between holding disk and slave bdev
  703. * @bdev: the claimed slave bdev
  704. * @disk: the holding disk
  705. *
  706. * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
  707. *
  708. * This functions creates the following sysfs symlinks.
  709. *
  710. * - from "slaves" directory of the holder @disk to the claimed @bdev
  711. * - from "holders" directory of the @bdev to the holder @disk
  712. *
  713. * For example, if /dev/dm-0 maps to /dev/sda and disk for dm-0 is
  714. * passed to bd_link_disk_holder(), then:
  715. *
  716. * /sys/block/dm-0/slaves/sda --> /sys/block/sda
  717. * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
  718. *
  719. * The caller must have claimed @bdev before calling this function and
  720. * ensure that both @bdev and @disk are valid during the creation and
  721. * lifetime of these symlinks.
  722. *
  723. * CONTEXT:
  724. * Might sleep.
  725. *
  726. * RETURNS:
  727. * 0 on success, -errno on failure.
  728. */
  729. int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk)
  730. {
  731. struct bd_holder_disk *holder;
  732. int ret = 0;
  733. mutex_lock(&bdev->bd_mutex);
  734. WARN_ON_ONCE(!bdev->bd_holder);
  735. /* FIXME: remove the following once add_disk() handles errors */
  736. if (WARN_ON(!disk->slave_dir || !bdev->bd_part->holder_dir))
  737. goto out_unlock;
  738. holder = bd_find_holder_disk(bdev, disk);
  739. if (holder) {
  740. holder->refcnt++;
  741. goto out_unlock;
  742. }
  743. holder = kzalloc(sizeof(*holder), GFP_KERNEL);
  744. if (!holder) {
  745. ret = -ENOMEM;
  746. goto out_unlock;
  747. }
  748. INIT_LIST_HEAD(&holder->list);
  749. holder->disk = disk;
  750. holder->refcnt = 1;
  751. ret = add_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
  752. if (ret)
  753. goto out_free;
  754. ret = add_symlink(bdev->bd_part->holder_dir, &disk_to_dev(disk)->kobj);
  755. if (ret)
  756. goto out_del;
  757. /*
  758. * bdev could be deleted beneath us which would implicitly destroy
  759. * the holder directory. Hold on to it.
  760. */
  761. kobject_get(bdev->bd_part->holder_dir);
  762. list_add(&holder->list, &bdev->bd_holder_disks);
  763. goto out_unlock;
  764. out_del:
  765. del_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
  766. out_free:
  767. kfree(holder);
  768. out_unlock:
  769. mutex_unlock(&bdev->bd_mutex);
  770. return ret;
  771. }
  772. EXPORT_SYMBOL_GPL(bd_link_disk_holder);
  773. /**
  774. * bd_unlink_disk_holder - destroy symlinks created by bd_link_disk_holder()
  775. * @bdev: the calimed slave bdev
  776. * @disk: the holding disk
  777. *
  778. * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
  779. *
  780. * CONTEXT:
  781. * Might sleep.
  782. */
  783. void bd_unlink_disk_holder(struct block_device *bdev, struct gendisk *disk)
  784. {
  785. struct bd_holder_disk *holder;
  786. mutex_lock(&bdev->bd_mutex);
  787. holder = bd_find_holder_disk(bdev, disk);
  788. if (!WARN_ON_ONCE(holder == NULL) && !--holder->refcnt) {
  789. del_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
  790. del_symlink(bdev->bd_part->holder_dir,
  791. &disk_to_dev(disk)->kobj);
  792. kobject_put(bdev->bd_part->holder_dir);
  793. list_del_init(&holder->list);
  794. kfree(holder);
  795. }
  796. mutex_unlock(&bdev->bd_mutex);
  797. }
  798. EXPORT_SYMBOL_GPL(bd_unlink_disk_holder);
  799. #endif
  800. /**
  801. * flush_disk - invalidates all buffer-cache entries on a disk
  802. *
  803. * @bdev: struct block device to be flushed
  804. * @kill_dirty: flag to guide handling of dirty inodes
  805. *
  806. * Invalidates all buffer-cache entries on a disk. It should be called
  807. * when a disk has been changed -- either by a media change or online
  808. * resize.
  809. */
  810. static void flush_disk(struct block_device *bdev, bool kill_dirty)
  811. {
  812. if (__invalidate_device(bdev, kill_dirty)) {
  813. char name[BDEVNAME_SIZE] = "";
  814. if (bdev->bd_disk)
  815. disk_name(bdev->bd_disk, 0, name);
  816. printk(KERN_WARNING "VFS: busy inodes on changed media or "
  817. "resized disk %s\n", name);
  818. }
  819. if (!bdev->bd_disk)
  820. return;
  821. if (disk_part_scan_enabled(bdev->bd_disk))
  822. bdev->bd_invalidated = 1;
  823. }
  824. /**
  825. * check_disk_size_change - checks for disk size change and adjusts bdev size.
  826. * @disk: struct gendisk to check
  827. * @bdev: struct bdev to adjust.
  828. *
  829. * This routine checks to see if the bdev size does not match the disk size
  830. * and adjusts it if it differs.
  831. */
  832. void check_disk_size_change(struct gendisk *disk, struct block_device *bdev)
  833. {
  834. loff_t disk_size, bdev_size;
  835. disk_size = (loff_t)get_capacity(disk) << 9;
  836. bdev_size = i_size_read(bdev->bd_inode);
  837. if (disk_size != bdev_size) {
  838. char name[BDEVNAME_SIZE];
  839. disk_name(disk, 0, name);
  840. printk(KERN_INFO
  841. "%s: detected capacity change from %lld to %lld\n",
  842. name, bdev_size, disk_size);
  843. i_size_write(bdev->bd_inode, disk_size);
  844. flush_disk(bdev, false);
  845. }
  846. }
  847. EXPORT_SYMBOL(check_disk_size_change);
  848. /**
  849. * revalidate_disk - wrapper for lower-level driver's revalidate_disk call-back
  850. * @disk: struct gendisk to be revalidated
  851. *
  852. * This routine is a wrapper for lower-level driver's revalidate_disk
  853. * call-backs. It is used to do common pre and post operations needed
  854. * for all revalidate_disk operations.
  855. */
  856. int revalidate_disk(struct gendisk *disk)
  857. {
  858. struct block_device *bdev;
  859. int ret = 0;
  860. if (disk->fops->revalidate_disk)
  861. ret = disk->fops->revalidate_disk(disk);
  862. bdev = bdget_disk(disk, 0);
  863. if (!bdev)
  864. return ret;
  865. mutex_lock(&bdev->bd_mutex);
  866. check_disk_size_change(disk, bdev);
  867. bdev->bd_invalidated = 0;
  868. mutex_unlock(&bdev->bd_mutex);
  869. bdput(bdev);
  870. return ret;
  871. }
  872. EXPORT_SYMBOL(revalidate_disk);
  873. /*
  874. * This routine checks whether a removable media has been changed,
  875. * and invalidates all buffer-cache-entries in that case. This
  876. * is a relatively slow routine, so we have to try to minimize using
  877. * it. Thus it is called only upon a 'mount' or 'open'. This
  878. * is the best way of combining speed and utility, I think.
  879. * People changing diskettes in the middle of an operation deserve
  880. * to lose :-)
  881. */
  882. int check_disk_change(struct block_device *bdev)
  883. {
  884. struct gendisk *disk = bdev->bd_disk;
  885. const struct block_device_operations *bdops = disk->fops;
  886. unsigned int events;
  887. events = disk_clear_events(disk, DISK_EVENT_MEDIA_CHANGE |
  888. DISK_EVENT_EJECT_REQUEST);
  889. if (!(events & DISK_EVENT_MEDIA_CHANGE))
  890. return 0;
  891. flush_disk(bdev, true);
  892. if (bdops->revalidate_disk)
  893. bdops->revalidate_disk(bdev->bd_disk);
  894. return 1;
  895. }
  896. EXPORT_SYMBOL(check_disk_change);
  897. void bd_set_size(struct block_device *bdev, loff_t size)
  898. {
  899. unsigned bsize = bdev_logical_block_size(bdev);
  900. bdev->bd_inode->i_size = size;
  901. while (bsize < PAGE_CACHE_SIZE) {
  902. if (size & bsize)
  903. break;
  904. bsize <<= 1;
  905. }
  906. bdev->bd_block_size = bsize;
  907. bdev->bd_inode->i_blkbits = blksize_bits(bsize);
  908. }
  909. EXPORT_SYMBOL(bd_set_size);
  910. static int __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part);
  911. /*
  912. * bd_mutex locking:
  913. *
  914. * mutex_lock(part->bd_mutex)
  915. * mutex_lock_nested(whole->bd_mutex, 1)
  916. */
  917. static int __blkdev_get(struct block_device *bdev, fmode_t mode, int for_part)
  918. {
  919. struct gendisk *disk;
  920. struct module *owner;
  921. int ret;
  922. int partno;
  923. int perm = 0;
  924. if (mode & FMODE_READ)
  925. perm |= MAY_READ;
  926. if (mode & FMODE_WRITE)
  927. perm |= MAY_WRITE;
  928. /*
  929. * hooks: /n/, see "layering violations".
  930. */
  931. if (!for_part) {
  932. ret = devcgroup_inode_permission(bdev->bd_inode, perm);
  933. if (ret != 0) {
  934. bdput(bdev);
  935. return ret;
  936. }
  937. }
  938. restart:
  939. ret = -ENXIO;
  940. disk = get_gendisk(bdev->bd_dev, &partno);
  941. if (!disk)
  942. goto out;
  943. owner = disk->fops->owner;
  944. disk_block_events(disk);
  945. mutex_lock_nested(&bdev->bd_mutex, for_part);
  946. if (!bdev->bd_openers) {
  947. bdev->bd_disk = disk;
  948. bdev->bd_queue = disk->queue;
  949. bdev->bd_contains = bdev;
  950. if (!partno) {
  951. struct backing_dev_info *bdi;
  952. ret = -ENXIO;
  953. bdev->bd_part = disk_get_part(disk, partno);
  954. if (!bdev->bd_part)
  955. goto out_clear;
  956. ret = 0;
  957. if (disk->fops->open) {
  958. ret = disk->fops->open(bdev, mode);
  959. if (ret == -ERESTARTSYS) {
  960. /* Lost a race with 'disk' being
  961. * deleted, try again.
  962. * See md.c
  963. */
  964. disk_put_part(bdev->bd_part);
  965. bdev->bd_part = NULL;
  966. bdev->bd_disk = NULL;
  967. bdev->bd_queue = NULL;
  968. mutex_unlock(&bdev->bd_mutex);
  969. disk_unblock_events(disk);
  970. put_disk(disk);
  971. module_put(owner);
  972. goto restart;
  973. }
  974. }
  975. if (!ret && !bdev->bd_openers) {
  976. bd_set_size(bdev,(loff_t)get_capacity(disk)<<9);
  977. bdi = blk_get_backing_dev_info(bdev);
  978. if (bdi == NULL)
  979. bdi = &default_backing_dev_info;
  980. bdev_inode_switch_bdi(bdev->bd_inode, bdi);
  981. }
  982. /*
  983. * If the device is invalidated, rescan partition
  984. * if open succeeded or failed with -ENOMEDIUM.
  985. * The latter is necessary to prevent ghost
  986. * partitions on a removed medium.
  987. */
  988. if (bdev->bd_invalidated) {
  989. if (!ret)
  990. rescan_partitions(disk, bdev);
  991. else if (ret == -ENOMEDIUM)
  992. invalidate_partitions(disk, bdev);
  993. }
  994. if (ret)
  995. goto out_clear;
  996. } else {
  997. struct block_device *whole;
  998. whole = bdget_disk(disk, 0);
  999. ret = -ENOMEM;
  1000. if (!whole)
  1001. goto out_clear;
  1002. BUG_ON(for_part);
  1003. ret = __blkdev_get(whole, mode, 1);
  1004. if (ret)
  1005. goto out_clear;
  1006. bdev->bd_contains = whole;
  1007. bdev_inode_switch_bdi(bdev->bd_inode,
  1008. whole->bd_inode->i_data.backing_dev_info);
  1009. bdev->bd_part = disk_get_part(disk, partno);
  1010. if (!(disk->flags & GENHD_FL_UP) ||
  1011. !bdev->bd_part || !bdev->bd_part->nr_sects) {
  1012. ret = -ENXIO;
  1013. goto out_clear;
  1014. }
  1015. bd_set_size(bdev, (loff_t)bdev->bd_part->nr_sects << 9);
  1016. }
  1017. } else {
  1018. if (bdev->bd_contains == bdev) {
  1019. ret = 0;
  1020. if (bdev->bd_disk->fops->open)
  1021. ret = bdev->bd_disk->fops->open(bdev, mode);
  1022. /* the same as first opener case, read comment there */
  1023. if (bdev->bd_invalidated) {
  1024. if (!ret)
  1025. rescan_partitions(bdev->bd_disk, bdev);
  1026. else if (ret == -ENOMEDIUM)
  1027. invalidate_partitions(bdev->bd_disk, bdev);
  1028. }
  1029. if (ret)
  1030. goto out_unlock_bdev;
  1031. }
  1032. /* only one opener holds refs to the module and disk */
  1033. put_disk(disk);
  1034. module_put(owner);
  1035. }
  1036. bdev->bd_openers++;
  1037. if (for_part)
  1038. bdev->bd_part_count++;
  1039. mutex_unlock(&bdev->bd_mutex);
  1040. disk_unblock_events(disk);
  1041. return 0;
  1042. out_clear:
  1043. disk_put_part(bdev->bd_part);
  1044. bdev->bd_disk = NULL;
  1045. bdev->bd_part = NULL;
  1046. bdev->bd_queue = NULL;
  1047. bdev_inode_switch_bdi(bdev->bd_inode, &default_backing_dev_info);
  1048. if (bdev != bdev->bd_contains)
  1049. __blkdev_put(bdev->bd_contains, mode, 1);
  1050. bdev->bd_contains = NULL;
  1051. out_unlock_bdev:
  1052. mutex_unlock(&bdev->bd_mutex);
  1053. disk_unblock_events(disk);
  1054. put_disk(disk);
  1055. module_put(owner);
  1056. out:
  1057. bdput(bdev);
  1058. return ret;
  1059. }
  1060. /**
  1061. * blkdev_get - open a block device
  1062. * @bdev: block_device to open
  1063. * @mode: FMODE_* mask
  1064. * @holder: exclusive holder identifier
  1065. *
  1066. * Open @bdev with @mode. If @mode includes %FMODE_EXCL, @bdev is
  1067. * open with exclusive access. Specifying %FMODE_EXCL with %NULL
  1068. * @holder is invalid. Exclusive opens may nest for the same @holder.
  1069. *
  1070. * On success, the reference count of @bdev is unchanged. On failure,
  1071. * @bdev is put.
  1072. *
  1073. * CONTEXT:
  1074. * Might sleep.
  1075. *
  1076. * RETURNS:
  1077. * 0 on success, -errno on failure.
  1078. */
  1079. int blkdev_get(struct block_device *bdev, fmode_t mode, void *holder)
  1080. {
  1081. struct block_device *whole = NULL;
  1082. int res;
  1083. WARN_ON_ONCE((mode & FMODE_EXCL) && !holder);
  1084. if ((mode & FMODE_EXCL) && holder) {
  1085. whole = bd_start_claiming(bdev, holder);
  1086. if (IS_ERR(whole)) {
  1087. bdput(bdev);
  1088. return PTR_ERR(whole);
  1089. }
  1090. }
  1091. res = __blkdev_get(bdev, mode, 0);
  1092. if (whole) {
  1093. struct gendisk *disk = whole->bd_disk;
  1094. /* finish claiming */
  1095. mutex_lock(&bdev->bd_mutex);
  1096. spin_lock(&bdev_lock);
  1097. if (!res) {
  1098. BUG_ON(!bd_may_claim(bdev, whole, holder));
  1099. /*
  1100. * Note that for a whole device bd_holders
  1101. * will be incremented twice, and bd_holder
  1102. * will be set to bd_may_claim before being
  1103. * set to holder
  1104. */
  1105. whole->bd_holders++;
  1106. whole->bd_holder = bd_may_claim;
  1107. bdev->bd_holders++;
  1108. bdev->bd_holder = holder;
  1109. }
  1110. /* tell others that we're done */
  1111. BUG_ON(whole->bd_claiming != holder);
  1112. whole->bd_claiming = NULL;
  1113. wake_up_bit(&whole->bd_claiming, 0);
  1114. spin_unlock(&bdev_lock);
  1115. /*
  1116. * Block event polling for write claims if requested. Any
  1117. * write holder makes the write_holder state stick until
  1118. * all are released. This is good enough and tracking
  1119. * individual writeable reference is too fragile given the
  1120. * way @mode is used in blkdev_get/put().
  1121. */
  1122. if (!res && (mode & FMODE_WRITE) && !bdev->bd_write_holder &&
  1123. (disk->flags & GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE)) {
  1124. bdev->bd_write_holder = true;
  1125. disk_block_events(disk);
  1126. }
  1127. mutex_unlock(&bdev->bd_mutex);
  1128. bdput(whole);
  1129. }
  1130. return res;
  1131. }
  1132. EXPORT_SYMBOL(blkdev_get);
  1133. /**
  1134. * blkdev_get_by_path - open a block device by name
  1135. * @path: path to the block device to open
  1136. * @mode: FMODE_* mask
  1137. * @holder: exclusive holder identifier
  1138. *
  1139. * Open the blockdevice described by the device file at @path. @mode
  1140. * and @holder are identical to blkdev_get().
  1141. *
  1142. * On success, the returned block_device has reference count of one.
  1143. *
  1144. * CONTEXT:
  1145. * Might sleep.
  1146. *
  1147. * RETURNS:
  1148. * Pointer to block_device on success, ERR_PTR(-errno) on failure.
  1149. */
  1150. struct block_device *blkdev_get_by_path(const char *path, fmode_t mode,
  1151. void *holder)
  1152. {
  1153. struct block_device *bdev;
  1154. int err;
  1155. bdev = lookup_bdev(path);
  1156. if (IS_ERR(bdev))
  1157. return bdev;
  1158. err = blkdev_get(bdev, mode, holder);
  1159. if (err)
  1160. return ERR_PTR(err);
  1161. if ((mode & FMODE_WRITE) && bdev_read_only(bdev)) {
  1162. blkdev_put(bdev, mode);
  1163. return ERR_PTR(-EACCES);
  1164. }
  1165. return bdev;
  1166. }
  1167. EXPORT_SYMBOL(blkdev_get_by_path);
  1168. /**
  1169. * blkdev_get_by_dev - open a block device by device number
  1170. * @dev: device number of block device to open
  1171. * @mode: FMODE_* mask
  1172. * @holder: exclusive holder identifier
  1173. *
  1174. * Open the blockdevice described by device number @dev. @mode and
  1175. * @holder are identical to blkdev_get().
  1176. *
  1177. * Use it ONLY if you really do not have anything better - i.e. when
  1178. * you are behind a truly sucky interface and all you are given is a
  1179. * device number. _Never_ to be used for internal purposes. If you
  1180. * ever need it - reconsider your API.
  1181. *
  1182. * On success, the returned block_device has reference count of one.
  1183. *
  1184. * CONTEXT:
  1185. * Might sleep.
  1186. *
  1187. * RETURNS:
  1188. * Pointer to block_device on success, ERR_PTR(-errno) on failure.
  1189. */
  1190. struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder)
  1191. {
  1192. struct block_device *bdev;
  1193. int err;
  1194. bdev = bdget(dev);
  1195. if (!bdev)
  1196. return ERR_PTR(-ENOMEM);
  1197. err = blkdev_get(bdev, mode, holder);
  1198. if (err)
  1199. return ERR_PTR(err);
  1200. return bdev;
  1201. }
  1202. EXPORT_SYMBOL(blkdev_get_by_dev);
  1203. static int blkdev_open(struct inode * inode, struct file * filp)
  1204. {
  1205. struct block_device *bdev;
  1206. /*
  1207. * Preserve backwards compatibility and allow large file access
  1208. * even if userspace doesn't ask for it explicitly. Some mkfs
  1209. * binary needs it. We might want to drop this workaround
  1210. * during an unstable branch.
  1211. */
  1212. filp->f_flags |= O_LARGEFILE;
  1213. if (filp->f_flags & O_NDELAY)
  1214. filp->f_mode |= FMODE_NDELAY;
  1215. if (filp->f_flags & O_EXCL)
  1216. filp->f_mode |= FMODE_EXCL;
  1217. if ((filp->f_flags & O_ACCMODE) == 3)
  1218. filp->f_mode |= FMODE_WRITE_IOCTL;
  1219. bdev = bd_acquire(inode);
  1220. if (bdev == NULL)
  1221. return -ENOMEM;
  1222. filp->f_mapping = bdev->bd_inode->i_mapping;
  1223. return blkdev_get(bdev, filp->f_mode, filp);
  1224. }
  1225. static int __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part)
  1226. {
  1227. int ret = 0;
  1228. struct gendisk *disk = bdev->bd_disk;
  1229. struct block_device *victim = NULL;
  1230. mutex_lock_nested(&bdev->bd_mutex, for_part);
  1231. if (for_part)
  1232. bdev->bd_part_count--;
  1233. if (!--bdev->bd_openers) {
  1234. WARN_ON_ONCE(bdev->bd_holders);
  1235. sync_blockdev(bdev);
  1236. kill_bdev(bdev);
  1237. /* ->release can cause the old bdi to disappear,
  1238. * so must switch it out first
  1239. */
  1240. bdev_inode_switch_bdi(bdev->bd_inode,
  1241. &default_backing_dev_info);
  1242. }
  1243. if (bdev->bd_contains == bdev) {
  1244. if (disk->fops->release)
  1245. ret = disk->fops->release(disk, mode);
  1246. }
  1247. if (!bdev->bd_openers) {
  1248. struct module *owner = disk->fops->owner;
  1249. disk_put_part(bdev->bd_part);
  1250. bdev->bd_part = NULL;
  1251. bdev->bd_disk = NULL;
  1252. if (bdev != bdev->bd_contains)
  1253. victim = bdev->bd_contains;
  1254. bdev->bd_contains = NULL;
  1255. put_disk(disk);
  1256. module_put(owner);
  1257. }
  1258. mutex_unlock(&bdev->bd_mutex);
  1259. bdput(bdev);
  1260. if (victim)
  1261. __blkdev_put(victim, mode, 1);
  1262. return ret;
  1263. }
  1264. int blkdev_put(struct block_device *bdev, fmode_t mode)
  1265. {
  1266. mutex_lock(&bdev->bd_mutex);
  1267. if (mode & FMODE_EXCL) {
  1268. bool bdev_free;
  1269. /*
  1270. * Release a claim on the device. The holder fields
  1271. * are protected with bdev_lock. bd_mutex is to
  1272. * synchronize disk_holder unlinking.
  1273. */
  1274. spin_lock(&bdev_lock);
  1275. WARN_ON_ONCE(--bdev->bd_holders < 0);
  1276. WARN_ON_ONCE(--bdev->bd_contains->bd_holders < 0);
  1277. /* bd_contains might point to self, check in a separate step */
  1278. if ((bdev_free = !bdev->bd_holders))
  1279. bdev->bd_holder = NULL;
  1280. if (!bdev->bd_contains->bd_holders)
  1281. bdev->bd_contains->bd_holder = NULL;
  1282. spin_unlock(&bdev_lock);
  1283. /*
  1284. * If this was the last claim, remove holder link and
  1285. * unblock evpoll if it was a write holder.
  1286. */
  1287. if (bdev_free && bdev->bd_write_holder) {
  1288. disk_unblock_events(bdev->bd_disk);
  1289. bdev->bd_write_holder = false;
  1290. }
  1291. }
  1292. /*
  1293. * Trigger event checking and tell drivers to flush MEDIA_CHANGE
  1294. * event. This is to ensure detection of media removal commanded
  1295. * from userland - e.g. eject(1).
  1296. */
  1297. disk_flush_events(bdev->bd_disk, DISK_EVENT_MEDIA_CHANGE);
  1298. mutex_unlock(&bdev->bd_mutex);
  1299. return __blkdev_put(bdev, mode, 0);
  1300. }
  1301. EXPORT_SYMBOL(blkdev_put);
  1302. static int blkdev_close(struct inode * inode, struct file * filp)
  1303. {
  1304. struct block_device *bdev = I_BDEV(filp->f_mapping->host);
  1305. return blkdev_put(bdev, filp->f_mode);
  1306. }
  1307. static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg)
  1308. {
  1309. struct block_device *bdev = I_BDEV(file->f_mapping->host);
  1310. fmode_t mode = file->f_mode;
  1311. /*
  1312. * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
  1313. * to updated it before every ioctl.
  1314. */
  1315. if (file->f_flags & O_NDELAY)
  1316. mode |= FMODE_NDELAY;
  1317. else
  1318. mode &= ~FMODE_NDELAY;
  1319. return blkdev_ioctl(bdev, mode, cmd, arg);
  1320. }
  1321. /*
  1322. * Write data to the block device. Only intended for the block device itself
  1323. * and the raw driver which basically is a fake block device.
  1324. *
  1325. * Does not take i_mutex for the write and thus is not for general purpose
  1326. * use.
  1327. */
  1328. ssize_t blkdev_aio_write(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 blk_plug plug;
  1333. ssize_t ret;
  1334. BUG_ON(iocb->ki_pos != pos);
  1335. blk_start_plug(&plug);
  1336. ret = __generic_file_aio_write(iocb, iov, nr_segs, &iocb->ki_pos);
  1337. if (ret > 0 || ret == -EIOCBQUEUED) {
  1338. ssize_t err;
  1339. err = generic_write_sync(file, pos, ret);
  1340. if (err < 0 && ret > 0)
  1341. ret = err;
  1342. }
  1343. blk_finish_plug(&plug);
  1344. return ret;
  1345. }
  1346. EXPORT_SYMBOL_GPL(blkdev_aio_write);
  1347. static ssize_t blkdev_aio_read(struct kiocb *iocb, const struct iovec *iov,
  1348. unsigned long nr_segs, loff_t pos)
  1349. {
  1350. struct file *file = iocb->ki_filp;
  1351. struct inode *bd_inode = file->f_mapping->host;
  1352. loff_t size = i_size_read(bd_inode);
  1353. if (pos >= size)
  1354. return 0;
  1355. size -= pos;
  1356. if (size < INT_MAX)
  1357. nr_segs = iov_shorten((struct iovec *)iov, nr_segs, size);
  1358. return generic_file_aio_read(iocb, iov, nr_segs, pos);
  1359. }
  1360. /*
  1361. * Try to release a page associated with block device when the system
  1362. * is under memory pressure.
  1363. */
  1364. static int blkdev_releasepage(struct page *page, gfp_t wait)
  1365. {
  1366. struct super_block *super = BDEV_I(page->mapping->host)->bdev.bd_super;
  1367. if (super && super->s_op->bdev_try_to_free_page)
  1368. return super->s_op->bdev_try_to_free_page(super, page, wait);
  1369. return try_to_free_buffers(page);
  1370. }
  1371. static const struct address_space_operations def_blk_aops = {
  1372. .readpage = blkdev_readpage,
  1373. .writepage = blkdev_writepage,
  1374. .write_begin = blkdev_write_begin,
  1375. .write_end = blkdev_write_end,
  1376. .writepages = generic_writepages,
  1377. .releasepage = blkdev_releasepage,
  1378. .direct_IO = blkdev_direct_IO,
  1379. };
  1380. const struct file_operations def_blk_fops = {
  1381. .open = blkdev_open,
  1382. .release = blkdev_close,
  1383. .llseek = block_llseek,
  1384. .read = do_sync_read,
  1385. .write = do_sync_write,
  1386. .aio_read = blkdev_aio_read,
  1387. .aio_write = blkdev_aio_write,
  1388. .mmap = generic_file_mmap,
  1389. .fsync = blkdev_fsync,
  1390. .unlocked_ioctl = block_ioctl,
  1391. #ifdef CONFIG_COMPAT
  1392. .compat_ioctl = compat_blkdev_ioctl,
  1393. #endif
  1394. .splice_read = generic_file_splice_read,
  1395. .splice_write = generic_file_splice_write,
  1396. };
  1397. int ioctl_by_bdev(struct block_device *bdev, unsigned cmd, unsigned long arg)
  1398. {
  1399. int res;
  1400. mm_segment_t old_fs = get_fs();
  1401. set_fs(KERNEL_DS);
  1402. res = blkdev_ioctl(bdev, 0, cmd, arg);
  1403. set_fs(old_fs);
  1404. return res;
  1405. }
  1406. EXPORT_SYMBOL(ioctl_by_bdev);
  1407. /**
  1408. * lookup_bdev - lookup a struct block_device by name
  1409. * @pathname: special file representing the block device
  1410. *
  1411. * Get a reference to the blockdevice at @pathname in the current
  1412. * namespace if possible and return it. Return ERR_PTR(error)
  1413. * otherwise.
  1414. */
  1415. struct block_device *lookup_bdev(const char *pathname)
  1416. {
  1417. struct block_device *bdev;
  1418. struct inode *inode;
  1419. struct path path;
  1420. int error;
  1421. if (!pathname || !*pathname)
  1422. return ERR_PTR(-EINVAL);
  1423. error = kern_path(pathname, LOOKUP_FOLLOW, &path);
  1424. if (error)
  1425. return ERR_PTR(error);
  1426. inode = path.dentry->d_inode;
  1427. error = -ENOTBLK;
  1428. if (!S_ISBLK(inode->i_mode))
  1429. goto fail;
  1430. error = -EACCES;
  1431. if (path.mnt->mnt_flags & MNT_NODEV)
  1432. goto fail;
  1433. error = -ENOMEM;
  1434. bdev = bd_acquire(inode);
  1435. if (!bdev)
  1436. goto fail;
  1437. out:
  1438. path_put(&path);
  1439. return bdev;
  1440. fail:
  1441. bdev = ERR_PTR(error);
  1442. goto out;
  1443. }
  1444. EXPORT_SYMBOL(lookup_bdev);
  1445. int __invalidate_device(struct block_device *bdev, bool kill_dirty)
  1446. {
  1447. struct super_block *sb = get_super(bdev);
  1448. int res = 0;
  1449. if (sb) {
  1450. /*
  1451. * no need to lock the super, get_super holds the
  1452. * read mutex so the filesystem cannot go away
  1453. * under us (->put_super runs with the write lock
  1454. * hold).
  1455. */
  1456. shrink_dcache_sb(sb);
  1457. res = invalidate_inodes(sb, kill_dirty);
  1458. drop_super(sb);
  1459. }
  1460. invalidate_bdev(bdev);
  1461. return res;
  1462. }
  1463. EXPORT_SYMBOL(__invalidate_device);
  1464. void iterate_bdevs(void (*func)(struct block_device *, void *), void *arg)
  1465. {
  1466. struct inode *inode, *old_inode = NULL;
  1467. spin_lock(&inode_sb_list_lock);
  1468. list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list) {
  1469. struct address_space *mapping = inode->i_mapping;
  1470. spin_lock(&inode->i_lock);
  1471. if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW) ||
  1472. mapping->nrpages == 0) {
  1473. spin_unlock(&inode->i_lock);
  1474. continue;
  1475. }
  1476. __iget(inode);
  1477. spin_unlock(&inode->i_lock);
  1478. spin_unlock(&inode_sb_list_lock);
  1479. /*
  1480. * We hold a reference to 'inode' so it couldn't have been
  1481. * removed from s_inodes list while we dropped the
  1482. * inode_sb_list_lock. We cannot iput the inode now as we can
  1483. * be holding the last reference and we cannot iput it under
  1484. * inode_sb_list_lock. So we keep the reference and iput it
  1485. * later.
  1486. */
  1487. iput(old_inode);
  1488. old_inode = inode;
  1489. func(I_BDEV(inode), arg);
  1490. spin_lock(&inode_sb_list_lock);
  1491. }
  1492. spin_unlock(&inode_sb_list_lock);
  1493. iput(old_inode);
  1494. }