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 <linux/aio.h>
  30. #include <asm/uaccess.h>
  31. #include "internal.h"
  32. struct bdev_inode {
  33. struct block_device bdev;
  34. struct inode vfs_inode;
  35. };
  36. static const struct address_space_operations def_blk_aops;
  37. static inline struct bdev_inode *BDEV_I(struct inode *inode)
  38. {
  39. return container_of(inode, struct bdev_inode, vfs_inode);
  40. }
  41. inline struct block_device *I_BDEV(struct inode *inode)
  42. {
  43. return &BDEV_I(inode)->bdev;
  44. }
  45. EXPORT_SYMBOL(I_BDEV);
  46. /*
  47. * Move the inode from its current bdi to a new bdi. If the inode is dirty we
  48. * need to move it onto the dirty list of @dst so that the inode is always on
  49. * the right list.
  50. */
  51. static void bdev_inode_switch_bdi(struct inode *inode,
  52. struct backing_dev_info *dst)
  53. {
  54. struct backing_dev_info *old = inode->i_data.backing_dev_info;
  55. if (unlikely(dst == old)) /* deadlock avoidance */
  56. return;
  57. bdi_lock_two(&old->wb, &dst->wb);
  58. spin_lock(&inode->i_lock);
  59. inode->i_data.backing_dev_info = dst;
  60. if (inode->i_state & I_DIRTY)
  61. list_move(&inode->i_wb_list, &dst->wb.b_dirty);
  62. spin_unlock(&inode->i_lock);
  63. spin_unlock(&old->wb.list_lock);
  64. spin_unlock(&dst->wb.list_lock);
  65. }
  66. /* Kill _all_ buffers and pagecache , dirty or not.. */
  67. void kill_bdev(struct block_device *bdev)
  68. {
  69. struct address_space *mapping = bdev->bd_inode->i_mapping;
  70. if (mapping->nrpages == 0)
  71. return;
  72. invalidate_bh_lrus();
  73. truncate_inode_pages(mapping, 0);
  74. }
  75. EXPORT_SYMBOL(kill_bdev);
  76. /* Invalidate clean unused buffers and pagecache. */
  77. void invalidate_bdev(struct block_device *bdev)
  78. {
  79. struct address_space *mapping = bdev->bd_inode->i_mapping;
  80. if (mapping->nrpages == 0)
  81. return;
  82. invalidate_bh_lrus();
  83. lru_add_drain_all(); /* make sure all lru add caches are flushed */
  84. invalidate_mapping_pages(mapping, 0, -1);
  85. /* 99% of the time, we don't need to flush the cleancache on the bdev.
  86. * But, for the strange corners, lets be cautious
  87. */
  88. cleancache_invalidate_inode(mapping);
  89. }
  90. EXPORT_SYMBOL(invalidate_bdev);
  91. int set_blocksize(struct block_device *bdev, int size)
  92. {
  93. /* Size must be a power of two, and between 512 and PAGE_SIZE */
  94. if (size > PAGE_SIZE || size < 512 || !is_power_of_2(size))
  95. return -EINVAL;
  96. /* Size cannot be smaller than the size supported by the device */
  97. if (size < bdev_logical_block_size(bdev))
  98. return -EINVAL;
  99. /* Don't change the size if it is same as current */
  100. if (bdev->bd_block_size != size) {
  101. sync_blockdev(bdev);
  102. bdev->bd_block_size = size;
  103. bdev->bd_inode->i_blkbits = blksize_bits(size);
  104. kill_bdev(bdev);
  105. }
  106. return 0;
  107. }
  108. EXPORT_SYMBOL(set_blocksize);
  109. int sb_set_blocksize(struct super_block *sb, int size)
  110. {
  111. if (set_blocksize(sb->s_bdev, size))
  112. return 0;
  113. /* If we get here, we know size is power of two
  114. * and it's value is between 512 and PAGE_SIZE */
  115. sb->s_blocksize = size;
  116. sb->s_blocksize_bits = blksize_bits(size);
  117. return sb->s_blocksize;
  118. }
  119. EXPORT_SYMBOL(sb_set_blocksize);
  120. int sb_min_blocksize(struct super_block *sb, int size)
  121. {
  122. int minsize = bdev_logical_block_size(sb->s_bdev);
  123. if (size < minsize)
  124. size = minsize;
  125. return sb_set_blocksize(sb, size);
  126. }
  127. EXPORT_SYMBOL(sb_min_blocksize);
  128. static int
  129. blkdev_get_block(struct inode *inode, sector_t iblock,
  130. struct buffer_head *bh, int create)
  131. {
  132. bh->b_bdev = I_BDEV(inode);
  133. bh->b_blocknr = iblock;
  134. set_buffer_mapped(bh);
  135. return 0;
  136. }
  137. static ssize_t
  138. blkdev_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
  139. loff_t offset, unsigned long nr_segs)
  140. {
  141. struct file *file = iocb->ki_filp;
  142. struct inode *inode = file->f_mapping->host;
  143. return __blockdev_direct_IO(rw, iocb, inode, I_BDEV(inode), iov, offset,
  144. nr_segs, blkdev_get_block, NULL, NULL, 0);
  145. }
  146. int __sync_blockdev(struct block_device *bdev, int wait)
  147. {
  148. if (!bdev)
  149. return 0;
  150. if (!wait)
  151. return filemap_flush(bdev->bd_inode->i_mapping);
  152. return filemap_write_and_wait(bdev->bd_inode->i_mapping);
  153. }
  154. /*
  155. * Write out and wait upon all the dirty data associated with a block
  156. * device via its mapping. Does not take the superblock lock.
  157. */
  158. int sync_blockdev(struct block_device *bdev)
  159. {
  160. return __sync_blockdev(bdev, 1);
  161. }
  162. EXPORT_SYMBOL(sync_blockdev);
  163. /*
  164. * Write out and wait upon all dirty data associated with this
  165. * device. Filesystem data as well as the underlying block
  166. * device. Takes the superblock lock.
  167. */
  168. int fsync_bdev(struct block_device *bdev)
  169. {
  170. struct super_block *sb = get_super(bdev);
  171. if (sb) {
  172. int res = sync_filesystem(sb);
  173. drop_super(sb);
  174. return res;
  175. }
  176. return sync_blockdev(bdev);
  177. }
  178. EXPORT_SYMBOL(fsync_bdev);
  179. /**
  180. * freeze_bdev -- lock a filesystem and force it into a consistent state
  181. * @bdev: blockdevice to lock
  182. *
  183. * If a superblock is found on this device, we take the s_umount semaphore
  184. * on it to make sure nobody unmounts until the snapshot creation is done.
  185. * The reference counter (bd_fsfreeze_count) guarantees that only the last
  186. * unfreeze process can unfreeze the frozen filesystem actually when multiple
  187. * freeze requests arrive simultaneously. It counts up in freeze_bdev() and
  188. * count down in thaw_bdev(). When it becomes 0, thaw_bdev() will unfreeze
  189. * actually.
  190. */
  191. struct super_block *freeze_bdev(struct block_device *bdev)
  192. {
  193. struct super_block *sb;
  194. int error = 0;
  195. mutex_lock(&bdev->bd_fsfreeze_mutex);
  196. if (++bdev->bd_fsfreeze_count > 1) {
  197. /*
  198. * We don't even need to grab a reference - the first call
  199. * to freeze_bdev grab an active reference and only the last
  200. * thaw_bdev drops it.
  201. */
  202. sb = get_super(bdev);
  203. drop_super(sb);
  204. mutex_unlock(&bdev->bd_fsfreeze_mutex);
  205. return sb;
  206. }
  207. sb = get_active_super(bdev);
  208. if (!sb)
  209. goto out;
  210. error = freeze_super(sb);
  211. if (error) {
  212. deactivate_super(sb);
  213. bdev->bd_fsfreeze_count--;
  214. mutex_unlock(&bdev->bd_fsfreeze_mutex);
  215. return ERR_PTR(error);
  216. }
  217. deactivate_super(sb);
  218. out:
  219. sync_blockdev(bdev);
  220. mutex_unlock(&bdev->bd_fsfreeze_mutex);
  221. return sb; /* thaw_bdev releases s->s_umount */
  222. }
  223. EXPORT_SYMBOL(freeze_bdev);
  224. /**
  225. * thaw_bdev -- unlock filesystem
  226. * @bdev: blockdevice to unlock
  227. * @sb: associated superblock
  228. *
  229. * Unlocks the filesystem and marks it writeable again after freeze_bdev().
  230. */
  231. int thaw_bdev(struct block_device *bdev, struct super_block *sb)
  232. {
  233. int error = -EINVAL;
  234. mutex_lock(&bdev->bd_fsfreeze_mutex);
  235. if (!bdev->bd_fsfreeze_count)
  236. goto out;
  237. error = 0;
  238. if (--bdev->bd_fsfreeze_count > 0)
  239. goto out;
  240. if (!sb)
  241. goto out;
  242. error = thaw_super(sb);
  243. if (error) {
  244. bdev->bd_fsfreeze_count++;
  245. mutex_unlock(&bdev->bd_fsfreeze_mutex);
  246. return error;
  247. }
  248. out:
  249. mutex_unlock(&bdev->bd_fsfreeze_mutex);
  250. return 0;
  251. }
  252. EXPORT_SYMBOL(thaw_bdev);
  253. static int blkdev_writepage(struct page *page, struct writeback_control *wbc)
  254. {
  255. return block_write_full_page(page, blkdev_get_block, wbc);
  256. }
  257. static int blkdev_readpage(struct file * file, struct page * page)
  258. {
  259. return block_read_full_page(page, blkdev_get_block);
  260. }
  261. static int blkdev_write_begin(struct file *file, struct address_space *mapping,
  262. loff_t pos, unsigned len, unsigned flags,
  263. struct page **pagep, void **fsdata)
  264. {
  265. return block_write_begin(mapping, pos, len, flags, pagep,
  266. blkdev_get_block);
  267. }
  268. static int blkdev_write_end(struct file *file, struct address_space *mapping,
  269. loff_t pos, unsigned len, unsigned copied,
  270. struct page *page, void *fsdata)
  271. {
  272. int ret;
  273. ret = block_write_end(file, mapping, pos, len, copied, page, fsdata);
  274. unlock_page(page);
  275. page_cache_release(page);
  276. return ret;
  277. }
  278. /*
  279. * private llseek:
  280. * for a block special file file_inode(file)->i_size is zero
  281. * so we compute the size by hand (just as in block_read/write above)
  282. */
  283. static loff_t block_llseek(struct file *file, loff_t offset, int whence)
  284. {
  285. struct inode *bd_inode = file->f_mapping->host;
  286. loff_t size;
  287. loff_t retval;
  288. mutex_lock(&bd_inode->i_mutex);
  289. size = i_size_read(bd_inode);
  290. retval = -EINVAL;
  291. switch (whence) {
  292. case SEEK_END:
  293. offset += size;
  294. break;
  295. case SEEK_CUR:
  296. offset += file->f_pos;
  297. case SEEK_SET:
  298. break;
  299. default:
  300. goto out;
  301. }
  302. if (offset >= 0 && offset <= size) {
  303. if (offset != file->f_pos) {
  304. file->f_pos = offset;
  305. }
  306. retval = offset;
  307. }
  308. out:
  309. mutex_unlock(&bd_inode->i_mutex);
  310. return retval;
  311. }
  312. int blkdev_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
  313. {
  314. struct inode *bd_inode = filp->f_mapping->host;
  315. struct block_device *bdev = I_BDEV(bd_inode);
  316. int error;
  317. error = filemap_write_and_wait_range(filp->f_mapping, start, end);
  318. if (error)
  319. return error;
  320. /*
  321. * There is no need to serialise calls to blkdev_issue_flush with
  322. * i_mutex and doing so causes performance issues with concurrent
  323. * O_SYNC writers to a block device.
  324. */
  325. error = blkdev_issue_flush(bdev, GFP_KERNEL, NULL);
  326. if (error == -EOPNOTSUPP)
  327. error = 0;
  328. return error;
  329. }
  330. EXPORT_SYMBOL(blkdev_fsync);
  331. /*
  332. * pseudo-fs
  333. */
  334. static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);
  335. static struct kmem_cache * bdev_cachep __read_mostly;
  336. static struct inode *bdev_alloc_inode(struct super_block *sb)
  337. {
  338. struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, GFP_KERNEL);
  339. if (!ei)
  340. return NULL;
  341. return &ei->vfs_inode;
  342. }
  343. static void bdev_i_callback(struct rcu_head *head)
  344. {
  345. struct inode *inode = container_of(head, struct inode, i_rcu);
  346. struct bdev_inode *bdi = BDEV_I(inode);
  347. kmem_cache_free(bdev_cachep, bdi);
  348. }
  349. static void bdev_destroy_inode(struct inode *inode)
  350. {
  351. call_rcu(&inode->i_rcu, bdev_i_callback);
  352. }
  353. static void init_once(void *foo)
  354. {
  355. struct bdev_inode *ei = (struct bdev_inode *) foo;
  356. struct block_device *bdev = &ei->bdev;
  357. memset(bdev, 0, sizeof(*bdev));
  358. mutex_init(&bdev->bd_mutex);
  359. INIT_LIST_HEAD(&bdev->bd_inodes);
  360. INIT_LIST_HEAD(&bdev->bd_list);
  361. #ifdef CONFIG_SYSFS
  362. INIT_LIST_HEAD(&bdev->bd_holder_disks);
  363. #endif
  364. inode_init_once(&ei->vfs_inode);
  365. /* Initialize mutex for freeze. */
  366. mutex_init(&bdev->bd_fsfreeze_mutex);
  367. }
  368. static inline void __bd_forget(struct inode *inode)
  369. {
  370. list_del_init(&inode->i_devices);
  371. inode->i_bdev = NULL;
  372. inode->i_mapping = &inode->i_data;
  373. }
  374. static void bdev_evict_inode(struct inode *inode)
  375. {
  376. struct block_device *bdev = &BDEV_I(inode)->bdev;
  377. struct list_head *p;
  378. truncate_inode_pages(&inode->i_data, 0);
  379. invalidate_inode_buffers(inode); /* is it needed here? */
  380. clear_inode(inode);
  381. spin_lock(&bdev_lock);
  382. while ( (p = bdev->bd_inodes.next) != &bdev->bd_inodes ) {
  383. __bd_forget(list_entry(p, struct inode, i_devices));
  384. }
  385. list_del_init(&bdev->bd_list);
  386. spin_unlock(&bdev_lock);
  387. }
  388. static const struct super_operations bdev_sops = {
  389. .statfs = simple_statfs,
  390. .alloc_inode = bdev_alloc_inode,
  391. .destroy_inode = bdev_destroy_inode,
  392. .drop_inode = generic_delete_inode,
  393. .evict_inode = bdev_evict_inode,
  394. };
  395. static struct dentry *bd_mount(struct file_system_type *fs_type,
  396. int flags, const char *dev_name, void *data)
  397. {
  398. return mount_pseudo(fs_type, "bdev:", &bdev_sops, NULL, BDEVFS_MAGIC);
  399. }
  400. static struct file_system_type bd_type = {
  401. .name = "bdev",
  402. .mount = bd_mount,
  403. .kill_sb = kill_anon_super,
  404. };
  405. static struct super_block *blockdev_superblock __read_mostly;
  406. void __init bdev_cache_init(void)
  407. {
  408. int err;
  409. static struct vfsmount *bd_mnt;
  410. bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
  411. 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
  412. SLAB_MEM_SPREAD|SLAB_PANIC),
  413. init_once);
  414. err = register_filesystem(&bd_type);
  415. if (err)
  416. panic("Cannot register bdev pseudo-fs");
  417. bd_mnt = kern_mount(&bd_type);
  418. if (IS_ERR(bd_mnt))
  419. panic("Cannot create bdev pseudo-fs");
  420. blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */
  421. }
  422. /*
  423. * Most likely _very_ bad one - but then it's hardly critical for small
  424. * /dev and can be fixed when somebody will need really large one.
  425. * Keep in mind that it will be fed through icache hash function too.
  426. */
  427. static inline unsigned long hash(dev_t dev)
  428. {
  429. return MAJOR(dev)+MINOR(dev);
  430. }
  431. static int bdev_test(struct inode *inode, void *data)
  432. {
  433. return BDEV_I(inode)->bdev.bd_dev == *(dev_t *)data;
  434. }
  435. static int bdev_set(struct inode *inode, void *data)
  436. {
  437. BDEV_I(inode)->bdev.bd_dev = *(dev_t *)data;
  438. return 0;
  439. }
  440. static LIST_HEAD(all_bdevs);
  441. struct block_device *bdget(dev_t dev)
  442. {
  443. struct block_device *bdev;
  444. struct inode *inode;
  445. inode = iget5_locked(blockdev_superblock, hash(dev),
  446. bdev_test, bdev_set, &dev);
  447. if (!inode)
  448. return NULL;
  449. bdev = &BDEV_I(inode)->bdev;
  450. if (inode->i_state & I_NEW) {
  451. bdev->bd_contains = NULL;
  452. bdev->bd_super = NULL;
  453. bdev->bd_inode = inode;
  454. bdev->bd_block_size = (1 << inode->i_blkbits);
  455. bdev->bd_part_count = 0;
  456. bdev->bd_invalidated = 0;
  457. inode->i_mode = S_IFBLK;
  458. inode->i_rdev = dev;
  459. inode->i_bdev = bdev;
  460. inode->i_data.a_ops = &def_blk_aops;
  461. mapping_set_gfp_mask(&inode->i_data, GFP_USER);
  462. inode->i_data.backing_dev_info = &default_backing_dev_info;
  463. spin_lock(&bdev_lock);
  464. list_add(&bdev->bd_list, &all_bdevs);
  465. spin_unlock(&bdev_lock);
  466. unlock_new_inode(inode);
  467. }
  468. return bdev;
  469. }
  470. EXPORT_SYMBOL(bdget);
  471. /**
  472. * bdgrab -- Grab a reference to an already referenced block device
  473. * @bdev: Block device to grab a reference to.
  474. */
  475. struct block_device *bdgrab(struct block_device *bdev)
  476. {
  477. ihold(bdev->bd_inode);
  478. return bdev;
  479. }
  480. EXPORT_SYMBOL(bdgrab);
  481. long nr_blockdev_pages(void)
  482. {
  483. struct block_device *bdev;
  484. long ret = 0;
  485. spin_lock(&bdev_lock);
  486. list_for_each_entry(bdev, &all_bdevs, bd_list) {
  487. ret += bdev->bd_inode->i_mapping->nrpages;
  488. }
  489. spin_unlock(&bdev_lock);
  490. return ret;
  491. }
  492. void bdput(struct block_device *bdev)
  493. {
  494. iput(bdev->bd_inode);
  495. }
  496. EXPORT_SYMBOL(bdput);
  497. static struct block_device *bd_acquire(struct inode *inode)
  498. {
  499. struct block_device *bdev;
  500. spin_lock(&bdev_lock);
  501. bdev = inode->i_bdev;
  502. if (bdev) {
  503. ihold(bdev->bd_inode);
  504. spin_unlock(&bdev_lock);
  505. return bdev;
  506. }
  507. spin_unlock(&bdev_lock);
  508. bdev = bdget(inode->i_rdev);
  509. if (bdev) {
  510. spin_lock(&bdev_lock);
  511. if (!inode->i_bdev) {
  512. /*
  513. * We take an additional reference to bd_inode,
  514. * and it's released in clear_inode() of inode.
  515. * So, we can access it via ->i_mapping always
  516. * without igrab().
  517. */
  518. ihold(bdev->bd_inode);
  519. inode->i_bdev = bdev;
  520. inode->i_mapping = bdev->bd_inode->i_mapping;
  521. list_add(&inode->i_devices, &bdev->bd_inodes);
  522. }
  523. spin_unlock(&bdev_lock);
  524. }
  525. return bdev;
  526. }
  527. static inline int sb_is_blkdev_sb(struct super_block *sb)
  528. {
  529. return sb == blockdev_superblock;
  530. }
  531. /* Call when you free inode */
  532. void bd_forget(struct inode *inode)
  533. {
  534. struct block_device *bdev = NULL;
  535. spin_lock(&bdev_lock);
  536. if (!sb_is_blkdev_sb(inode->i_sb))
  537. bdev = inode->i_bdev;
  538. __bd_forget(inode);
  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. mutex_lock(&bdev->bd_inode->i_mutex);
  901. i_size_write(bdev->bd_inode, size);
  902. mutex_unlock(&bdev->bd_inode->i_mutex);
  903. while (bsize < PAGE_CACHE_SIZE) {
  904. if (size & bsize)
  905. break;
  906. bsize <<= 1;
  907. }
  908. bdev->bd_block_size = bsize;
  909. bdev->bd_inode->i_blkbits = blksize_bits(bsize);
  910. }
  911. EXPORT_SYMBOL(bd_set_size);
  912. static void __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part);
  913. /*
  914. * bd_mutex locking:
  915. *
  916. * mutex_lock(part->bd_mutex)
  917. * mutex_lock_nested(whole->bd_mutex, 1)
  918. */
  919. static int __blkdev_get(struct block_device *bdev, fmode_t mode, int for_part)
  920. {
  921. struct gendisk *disk;
  922. struct module *owner;
  923. int ret;
  924. int partno;
  925. int perm = 0;
  926. if (mode & FMODE_READ)
  927. perm |= MAY_READ;
  928. if (mode & FMODE_WRITE)
  929. perm |= MAY_WRITE;
  930. /*
  931. * hooks: /n/, see "layering violations".
  932. */
  933. if (!for_part) {
  934. ret = devcgroup_inode_permission(bdev->bd_inode, perm);
  935. if (ret != 0) {
  936. bdput(bdev);
  937. return ret;
  938. }
  939. }
  940. restart:
  941. ret = -ENXIO;
  942. disk = get_gendisk(bdev->bd_dev, &partno);
  943. if (!disk)
  944. goto out;
  945. owner = disk->fops->owner;
  946. disk_block_events(disk);
  947. mutex_lock_nested(&bdev->bd_mutex, for_part);
  948. if (!bdev->bd_openers) {
  949. bdev->bd_disk = disk;
  950. bdev->bd_queue = disk->queue;
  951. bdev->bd_contains = bdev;
  952. if (!partno) {
  953. struct backing_dev_info *bdi;
  954. ret = -ENXIO;
  955. bdev->bd_part = disk_get_part(disk, partno);
  956. if (!bdev->bd_part)
  957. goto out_clear;
  958. ret = 0;
  959. if (disk->fops->open) {
  960. ret = disk->fops->open(bdev, mode);
  961. if (ret == -ERESTARTSYS) {
  962. /* Lost a race with 'disk' being
  963. * deleted, try again.
  964. * See md.c
  965. */
  966. disk_put_part(bdev->bd_part);
  967. bdev->bd_part = NULL;
  968. bdev->bd_disk = NULL;
  969. bdev->bd_queue = NULL;
  970. mutex_unlock(&bdev->bd_mutex);
  971. disk_unblock_events(disk);
  972. put_disk(disk);
  973. module_put(owner);
  974. goto restart;
  975. }
  976. }
  977. if (!ret) {
  978. bd_set_size(bdev,(loff_t)get_capacity(disk)<<9);
  979. bdi = blk_get_backing_dev_info(bdev);
  980. if (bdi == NULL)
  981. bdi = &default_backing_dev_info;
  982. bdev_inode_switch_bdi(bdev->bd_inode, bdi);
  983. }
  984. /*
  985. * If the device is invalidated, rescan partition
  986. * if open succeeded or failed with -ENOMEDIUM.
  987. * The latter is necessary to prevent ghost
  988. * partitions on a removed medium.
  989. */
  990. if (bdev->bd_invalidated) {
  991. if (!ret)
  992. rescan_partitions(disk, bdev);
  993. else if (ret == -ENOMEDIUM)
  994. invalidate_partitions(disk, bdev);
  995. }
  996. if (ret)
  997. goto out_clear;
  998. } else {
  999. struct block_device *whole;
  1000. whole = bdget_disk(disk, 0);
  1001. ret = -ENOMEM;
  1002. if (!whole)
  1003. goto out_clear;
  1004. BUG_ON(for_part);
  1005. ret = __blkdev_get(whole, mode, 1);
  1006. if (ret)
  1007. goto out_clear;
  1008. bdev->bd_contains = whole;
  1009. bdev_inode_switch_bdi(bdev->bd_inode,
  1010. whole->bd_inode->i_data.backing_dev_info);
  1011. bdev->bd_part = disk_get_part(disk, partno);
  1012. if (!(disk->flags & GENHD_FL_UP) ||
  1013. !bdev->bd_part || !bdev->bd_part->nr_sects) {
  1014. ret = -ENXIO;
  1015. goto out_clear;
  1016. }
  1017. bd_set_size(bdev, (loff_t)bdev->bd_part->nr_sects << 9);
  1018. }
  1019. } else {
  1020. if (bdev->bd_contains == bdev) {
  1021. ret = 0;
  1022. if (bdev->bd_disk->fops->open)
  1023. ret = bdev->bd_disk->fops->open(bdev, mode);
  1024. /* the same as first opener case, read comment there */
  1025. if (bdev->bd_invalidated) {
  1026. if (!ret)
  1027. rescan_partitions(bdev->bd_disk, bdev);
  1028. else if (ret == -ENOMEDIUM)
  1029. invalidate_partitions(bdev->bd_disk, bdev);
  1030. }
  1031. if (ret)
  1032. goto out_unlock_bdev;
  1033. }
  1034. /* only one opener holds refs to the module and disk */
  1035. put_disk(disk);
  1036. module_put(owner);
  1037. }
  1038. bdev->bd_openers++;
  1039. if (for_part)
  1040. bdev->bd_part_count++;
  1041. mutex_unlock(&bdev->bd_mutex);
  1042. disk_unblock_events(disk);
  1043. return 0;
  1044. out_clear:
  1045. disk_put_part(bdev->bd_part);
  1046. bdev->bd_disk = NULL;
  1047. bdev->bd_part = NULL;
  1048. bdev->bd_queue = NULL;
  1049. bdev_inode_switch_bdi(bdev->bd_inode, &default_backing_dev_info);
  1050. if (bdev != bdev->bd_contains)
  1051. __blkdev_put(bdev->bd_contains, mode, 1);
  1052. bdev->bd_contains = NULL;
  1053. out_unlock_bdev:
  1054. mutex_unlock(&bdev->bd_mutex);
  1055. disk_unblock_events(disk);
  1056. put_disk(disk);
  1057. module_put(owner);
  1058. out:
  1059. bdput(bdev);
  1060. return ret;
  1061. }
  1062. /**
  1063. * blkdev_get - open a block device
  1064. * @bdev: block_device to open
  1065. * @mode: FMODE_* mask
  1066. * @holder: exclusive holder identifier
  1067. *
  1068. * Open @bdev with @mode. If @mode includes %FMODE_EXCL, @bdev is
  1069. * open with exclusive access. Specifying %FMODE_EXCL with %NULL
  1070. * @holder is invalid. Exclusive opens may nest for the same @holder.
  1071. *
  1072. * On success, the reference count of @bdev is unchanged. On failure,
  1073. * @bdev is put.
  1074. *
  1075. * CONTEXT:
  1076. * Might sleep.
  1077. *
  1078. * RETURNS:
  1079. * 0 on success, -errno on failure.
  1080. */
  1081. int blkdev_get(struct block_device *bdev, fmode_t mode, void *holder)
  1082. {
  1083. struct block_device *whole = NULL;
  1084. int res;
  1085. WARN_ON_ONCE((mode & FMODE_EXCL) && !holder);
  1086. if ((mode & FMODE_EXCL) && holder) {
  1087. whole = bd_start_claiming(bdev, holder);
  1088. if (IS_ERR(whole)) {
  1089. bdput(bdev);
  1090. return PTR_ERR(whole);
  1091. }
  1092. }
  1093. res = __blkdev_get(bdev, mode, 0);
  1094. if (whole) {
  1095. struct gendisk *disk = whole->bd_disk;
  1096. /* finish claiming */
  1097. mutex_lock(&bdev->bd_mutex);
  1098. spin_lock(&bdev_lock);
  1099. if (!res) {
  1100. BUG_ON(!bd_may_claim(bdev, whole, holder));
  1101. /*
  1102. * Note that for a whole device bd_holders
  1103. * will be incremented twice, and bd_holder
  1104. * will be set to bd_may_claim before being
  1105. * set to holder
  1106. */
  1107. whole->bd_holders++;
  1108. whole->bd_holder = bd_may_claim;
  1109. bdev->bd_holders++;
  1110. bdev->bd_holder = holder;
  1111. }
  1112. /* tell others that we're done */
  1113. BUG_ON(whole->bd_claiming != holder);
  1114. whole->bd_claiming = NULL;
  1115. wake_up_bit(&whole->bd_claiming, 0);
  1116. spin_unlock(&bdev_lock);
  1117. /*
  1118. * Block event polling for write claims if requested. Any
  1119. * write holder makes the write_holder state stick until
  1120. * all are released. This is good enough and tracking
  1121. * individual writeable reference is too fragile given the
  1122. * way @mode is used in blkdev_get/put().
  1123. */
  1124. if (!res && (mode & FMODE_WRITE) && !bdev->bd_write_holder &&
  1125. (disk->flags & GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE)) {
  1126. bdev->bd_write_holder = true;
  1127. disk_block_events(disk);
  1128. }
  1129. mutex_unlock(&bdev->bd_mutex);
  1130. bdput(whole);
  1131. }
  1132. return res;
  1133. }
  1134. EXPORT_SYMBOL(blkdev_get);
  1135. /**
  1136. * blkdev_get_by_path - open a block device by name
  1137. * @path: path to the block device to open
  1138. * @mode: FMODE_* mask
  1139. * @holder: exclusive holder identifier
  1140. *
  1141. * Open the blockdevice described by the device file at @path. @mode
  1142. * and @holder are identical to blkdev_get().
  1143. *
  1144. * On success, the returned block_device has reference count of one.
  1145. *
  1146. * CONTEXT:
  1147. * Might sleep.
  1148. *
  1149. * RETURNS:
  1150. * Pointer to block_device on success, ERR_PTR(-errno) on failure.
  1151. */
  1152. struct block_device *blkdev_get_by_path(const char *path, fmode_t mode,
  1153. void *holder)
  1154. {
  1155. struct block_device *bdev;
  1156. int err;
  1157. bdev = lookup_bdev(path);
  1158. if (IS_ERR(bdev))
  1159. return bdev;
  1160. err = blkdev_get(bdev, mode, holder);
  1161. if (err)
  1162. return ERR_PTR(err);
  1163. if ((mode & FMODE_WRITE) && bdev_read_only(bdev)) {
  1164. blkdev_put(bdev, mode);
  1165. return ERR_PTR(-EACCES);
  1166. }
  1167. return bdev;
  1168. }
  1169. EXPORT_SYMBOL(blkdev_get_by_path);
  1170. /**
  1171. * blkdev_get_by_dev - open a block device by device number
  1172. * @dev: device number of block device to open
  1173. * @mode: FMODE_* mask
  1174. * @holder: exclusive holder identifier
  1175. *
  1176. * Open the blockdevice described by device number @dev. @mode and
  1177. * @holder are identical to blkdev_get().
  1178. *
  1179. * Use it ONLY if you really do not have anything better - i.e. when
  1180. * you are behind a truly sucky interface and all you are given is a
  1181. * device number. _Never_ to be used for internal purposes. If you
  1182. * ever need it - reconsider your API.
  1183. *
  1184. * On success, the returned block_device has reference count of one.
  1185. *
  1186. * CONTEXT:
  1187. * Might sleep.
  1188. *
  1189. * RETURNS:
  1190. * Pointer to block_device on success, ERR_PTR(-errno) on failure.
  1191. */
  1192. struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder)
  1193. {
  1194. struct block_device *bdev;
  1195. int err;
  1196. bdev = bdget(dev);
  1197. if (!bdev)
  1198. return ERR_PTR(-ENOMEM);
  1199. err = blkdev_get(bdev, mode, holder);
  1200. if (err)
  1201. return ERR_PTR(err);
  1202. return bdev;
  1203. }
  1204. EXPORT_SYMBOL(blkdev_get_by_dev);
  1205. static int blkdev_open(struct inode * inode, struct file * filp)
  1206. {
  1207. struct block_device *bdev;
  1208. /*
  1209. * Preserve backwards compatibility and allow large file access
  1210. * even if userspace doesn't ask for it explicitly. Some mkfs
  1211. * binary needs it. We might want to drop this workaround
  1212. * during an unstable branch.
  1213. */
  1214. filp->f_flags |= O_LARGEFILE;
  1215. if (filp->f_flags & O_NDELAY)
  1216. filp->f_mode |= FMODE_NDELAY;
  1217. if (filp->f_flags & O_EXCL)
  1218. filp->f_mode |= FMODE_EXCL;
  1219. if ((filp->f_flags & O_ACCMODE) == 3)
  1220. filp->f_mode |= FMODE_WRITE_IOCTL;
  1221. bdev = bd_acquire(inode);
  1222. if (bdev == NULL)
  1223. return -ENOMEM;
  1224. filp->f_mapping = bdev->bd_inode->i_mapping;
  1225. return blkdev_get(bdev, filp->f_mode, filp);
  1226. }
  1227. static void __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part)
  1228. {
  1229. struct gendisk *disk = bdev->bd_disk;
  1230. struct block_device *victim = NULL;
  1231. mutex_lock_nested(&bdev->bd_mutex, for_part);
  1232. if (for_part)
  1233. bdev->bd_part_count--;
  1234. if (!--bdev->bd_openers) {
  1235. WARN_ON_ONCE(bdev->bd_holders);
  1236. sync_blockdev(bdev);
  1237. kill_bdev(bdev);
  1238. /* ->release can cause the old bdi to disappear,
  1239. * so must switch it out first
  1240. */
  1241. bdev_inode_switch_bdi(bdev->bd_inode,
  1242. &default_backing_dev_info);
  1243. }
  1244. if (bdev->bd_contains == bdev) {
  1245. if (disk->fops->release)
  1246. disk->fops->release(disk, mode);
  1247. }
  1248. if (!bdev->bd_openers) {
  1249. struct module *owner = disk->fops->owner;
  1250. disk_put_part(bdev->bd_part);
  1251. bdev->bd_part = NULL;
  1252. bdev->bd_disk = NULL;
  1253. if (bdev != bdev->bd_contains)
  1254. victim = bdev->bd_contains;
  1255. bdev->bd_contains = NULL;
  1256. put_disk(disk);
  1257. module_put(owner);
  1258. }
  1259. mutex_unlock(&bdev->bd_mutex);
  1260. bdput(bdev);
  1261. if (victim)
  1262. __blkdev_put(victim, mode, 1);
  1263. }
  1264. void 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. __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. blkdev_put(bdev, filp->f_mode);
  1306. return 0;
  1307. }
  1308. static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg)
  1309. {
  1310. struct block_device *bdev = I_BDEV(file->f_mapping->host);
  1311. fmode_t mode = file->f_mode;
  1312. /*
  1313. * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
  1314. * to updated it before every ioctl.
  1315. */
  1316. if (file->f_flags & O_NDELAY)
  1317. mode |= FMODE_NDELAY;
  1318. else
  1319. mode &= ~FMODE_NDELAY;
  1320. return blkdev_ioctl(bdev, mode, cmd, arg);
  1321. }
  1322. /*
  1323. * Write data to the block device. Only intended for the block device itself
  1324. * and the raw driver which basically is a fake block device.
  1325. *
  1326. * Does not take i_mutex for the write and thus is not for general purpose
  1327. * use.
  1328. */
  1329. ssize_t blkdev_aio_write(struct kiocb *iocb, const struct iovec *iov,
  1330. unsigned long nr_segs, loff_t pos)
  1331. {
  1332. struct file *file = iocb->ki_filp;
  1333. struct blk_plug plug;
  1334. ssize_t ret;
  1335. BUG_ON(iocb->ki_pos != pos);
  1336. blk_start_plug(&plug);
  1337. ret = __generic_file_aio_write(iocb, iov, nr_segs, &iocb->ki_pos);
  1338. if (ret > 0 || ret == -EIOCBQUEUED) {
  1339. ssize_t err;
  1340. err = generic_write_sync(file, pos, ret);
  1341. if (err < 0 && ret > 0)
  1342. ret = err;
  1343. }
  1344. blk_finish_plug(&plug);
  1345. return ret;
  1346. }
  1347. EXPORT_SYMBOL_GPL(blkdev_aio_write);
  1348. static ssize_t blkdev_aio_read(struct kiocb *iocb, const struct iovec *iov,
  1349. unsigned long nr_segs, loff_t pos)
  1350. {
  1351. struct file *file = iocb->ki_filp;
  1352. struct inode *bd_inode = file->f_mapping->host;
  1353. loff_t size = i_size_read(bd_inode);
  1354. if (pos >= size)
  1355. return 0;
  1356. size -= pos;
  1357. if (size < iocb->ki_left)
  1358. nr_segs = iov_shorten((struct iovec *)iov, nr_segs, size);
  1359. return generic_file_aio_read(iocb, iov, nr_segs, pos);
  1360. }
  1361. /*
  1362. * Try to release a page associated with block device when the system
  1363. * is under memory pressure.
  1364. */
  1365. static int blkdev_releasepage(struct page *page, gfp_t wait)
  1366. {
  1367. struct super_block *super = BDEV_I(page->mapping->host)->bdev.bd_super;
  1368. if (super && super->s_op->bdev_try_to_free_page)
  1369. return super->s_op->bdev_try_to_free_page(super, page, wait);
  1370. return try_to_free_buffers(page);
  1371. }
  1372. static const struct address_space_operations def_blk_aops = {
  1373. .readpage = blkdev_readpage,
  1374. .writepage = blkdev_writepage,
  1375. .write_begin = blkdev_write_begin,
  1376. .write_end = blkdev_write_end,
  1377. .writepages = generic_writepages,
  1378. .releasepage = blkdev_releasepage,
  1379. .direct_IO = blkdev_direct_IO,
  1380. };
  1381. const struct file_operations def_blk_fops = {
  1382. .open = blkdev_open,
  1383. .release = blkdev_close,
  1384. .llseek = block_llseek,
  1385. .read = do_sync_read,
  1386. .write = do_sync_write,
  1387. .aio_read = blkdev_aio_read,
  1388. .aio_write = blkdev_aio_write,
  1389. .mmap = generic_file_mmap,
  1390. .fsync = blkdev_fsync,
  1391. .unlocked_ioctl = block_ioctl,
  1392. #ifdef CONFIG_COMPAT
  1393. .compat_ioctl = compat_blkdev_ioctl,
  1394. #endif
  1395. .splice_read = generic_file_splice_read,
  1396. .splice_write = generic_file_splice_write,
  1397. };
  1398. int ioctl_by_bdev(struct block_device *bdev, unsigned cmd, unsigned long arg)
  1399. {
  1400. int res;
  1401. mm_segment_t old_fs = get_fs();
  1402. set_fs(KERNEL_DS);
  1403. res = blkdev_ioctl(bdev, 0, cmd, arg);
  1404. set_fs(old_fs);
  1405. return res;
  1406. }
  1407. EXPORT_SYMBOL(ioctl_by_bdev);
  1408. /**
  1409. * lookup_bdev - lookup a struct block_device by name
  1410. * @pathname: special file representing the block device
  1411. *
  1412. * Get a reference to the blockdevice at @pathname in the current
  1413. * namespace if possible and return it. Return ERR_PTR(error)
  1414. * otherwise.
  1415. */
  1416. struct block_device *lookup_bdev(const char *pathname)
  1417. {
  1418. struct block_device *bdev;
  1419. struct inode *inode;
  1420. struct path path;
  1421. int error;
  1422. if (!pathname || !*pathname)
  1423. return ERR_PTR(-EINVAL);
  1424. error = kern_path(pathname, LOOKUP_FOLLOW, &path);
  1425. if (error)
  1426. return ERR_PTR(error);
  1427. inode = path.dentry->d_inode;
  1428. error = -ENOTBLK;
  1429. if (!S_ISBLK(inode->i_mode))
  1430. goto fail;
  1431. error = -EACCES;
  1432. if (path.mnt->mnt_flags & MNT_NODEV)
  1433. goto fail;
  1434. error = -ENOMEM;
  1435. bdev = bd_acquire(inode);
  1436. if (!bdev)
  1437. goto fail;
  1438. out:
  1439. path_put(&path);
  1440. return bdev;
  1441. fail:
  1442. bdev = ERR_PTR(error);
  1443. goto out;
  1444. }
  1445. EXPORT_SYMBOL(lookup_bdev);
  1446. int __invalidate_device(struct block_device *bdev, bool kill_dirty)
  1447. {
  1448. struct super_block *sb = get_super(bdev);
  1449. int res = 0;
  1450. if (sb) {
  1451. /*
  1452. * no need to lock the super, get_super holds the
  1453. * read mutex so the filesystem cannot go away
  1454. * under us (->put_super runs with the write lock
  1455. * hold).
  1456. */
  1457. shrink_dcache_sb(sb);
  1458. res = invalidate_inodes(sb, kill_dirty);
  1459. drop_super(sb);
  1460. }
  1461. invalidate_bdev(bdev);
  1462. return res;
  1463. }
  1464. EXPORT_SYMBOL(__invalidate_device);
  1465. void iterate_bdevs(void (*func)(struct block_device *, void *), void *arg)
  1466. {
  1467. struct inode *inode, *old_inode = NULL;
  1468. spin_lock(&inode_sb_list_lock);
  1469. list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list) {
  1470. struct address_space *mapping = inode->i_mapping;
  1471. spin_lock(&inode->i_lock);
  1472. if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW) ||
  1473. mapping->nrpages == 0) {
  1474. spin_unlock(&inode->i_lock);
  1475. continue;
  1476. }
  1477. __iget(inode);
  1478. spin_unlock(&inode->i_lock);
  1479. spin_unlock(&inode_sb_list_lock);
  1480. /*
  1481. * We hold a reference to 'inode' so it couldn't have been
  1482. * removed from s_inodes list while we dropped the
  1483. * inode_sb_list_lock. We cannot iput the inode now as we can
  1484. * be holding the last reference and we cannot iput it under
  1485. * inode_sb_list_lock. So we keep the reference and iput it
  1486. * later.
  1487. */
  1488. iput(old_inode);
  1489. old_inode = inode;
  1490. func(I_BDEV(inode), arg);
  1491. spin_lock(&inode_sb_list_lock);
  1492. }
  1493. spin_unlock(&inode_sb_list_lock);
  1494. iput(old_inode);
  1495. }