block_dev.c 42 KB

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