block_dev.c 40 KB

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