block_dev.c 42 KB

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