block_dev.c 43 KB

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