block_dev.c 42 KB

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