block_dev.c 41 KB

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