block_dev.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622
  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_disks);
  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, NULL, 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_may_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. #ifdef CONFIG_SYSFS
  685. struct bd_holder_disk {
  686. struct list_head list;
  687. struct gendisk *disk;
  688. int refcnt;
  689. };
  690. static struct bd_holder_disk *bd_find_holder_disk(struct block_device *bdev,
  691. struct gendisk *disk)
  692. {
  693. struct bd_holder_disk *holder;
  694. list_for_each_entry(holder, &bdev->bd_holder_disks, list)
  695. if (holder->disk == disk)
  696. return holder;
  697. return NULL;
  698. }
  699. static int add_symlink(struct kobject *from, struct kobject *to)
  700. {
  701. return sysfs_create_link(from, to, kobject_name(to));
  702. }
  703. static void del_symlink(struct kobject *from, struct kobject *to)
  704. {
  705. sysfs_remove_link(from, kobject_name(to));
  706. }
  707. /**
  708. * bd_link_disk_holder - create symlinks between holding disk and slave bdev
  709. * @bdev: the claimed slave bdev
  710. * @disk: the holding disk
  711. *
  712. * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
  713. *
  714. * This functions creates the following sysfs symlinks.
  715. *
  716. * - from "slaves" directory of the holder @disk to the claimed @bdev
  717. * - from "holders" directory of the @bdev to the holder @disk
  718. *
  719. * For example, if /dev/dm-0 maps to /dev/sda and disk for dm-0 is
  720. * passed to bd_link_disk_holder(), then:
  721. *
  722. * /sys/block/dm-0/slaves/sda --> /sys/block/sda
  723. * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
  724. *
  725. * The caller must have claimed @bdev before calling this function and
  726. * ensure that both @bdev and @disk are valid during the creation and
  727. * lifetime of these symlinks.
  728. *
  729. * CONTEXT:
  730. * Might sleep.
  731. *
  732. * RETURNS:
  733. * 0 on success, -errno on failure.
  734. */
  735. int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk)
  736. {
  737. struct bd_holder_disk *holder;
  738. int ret = 0;
  739. mutex_lock(&bdev->bd_mutex);
  740. WARN_ON_ONCE(!bdev->bd_holder);
  741. /* FIXME: remove the following once add_disk() handles errors */
  742. if (WARN_ON(!disk->slave_dir || !bdev->bd_part->holder_dir))
  743. goto out_unlock;
  744. holder = bd_find_holder_disk(bdev, disk);
  745. if (holder) {
  746. holder->refcnt++;
  747. goto out_unlock;
  748. }
  749. holder = kzalloc(sizeof(*holder), GFP_KERNEL);
  750. if (!holder) {
  751. ret = -ENOMEM;
  752. goto out_unlock;
  753. }
  754. INIT_LIST_HEAD(&holder->list);
  755. holder->disk = disk;
  756. holder->refcnt = 1;
  757. ret = add_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
  758. if (ret)
  759. goto out_free;
  760. ret = add_symlink(bdev->bd_part->holder_dir, &disk_to_dev(disk)->kobj);
  761. if (ret)
  762. goto out_del;
  763. list_add(&holder->list, &bdev->bd_holder_disks);
  764. goto out_unlock;
  765. out_del:
  766. del_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
  767. out_free:
  768. kfree(holder);
  769. out_unlock:
  770. mutex_unlock(&bdev->bd_mutex);
  771. return ret;
  772. }
  773. EXPORT_SYMBOL_GPL(bd_link_disk_holder);
  774. /**
  775. * bd_unlink_disk_holder - destroy symlinks created by bd_link_disk_holder()
  776. * @bdev: the calimed slave bdev
  777. * @disk: the holding disk
  778. *
  779. * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
  780. *
  781. * CONTEXT:
  782. * Might sleep.
  783. */
  784. void bd_unlink_disk_holder(struct block_device *bdev, struct gendisk *disk)
  785. {
  786. struct bd_holder_disk *holder;
  787. mutex_lock(&bdev->bd_mutex);
  788. holder = bd_find_holder_disk(bdev, disk);
  789. if (!WARN_ON_ONCE(holder == NULL) && !--holder->refcnt) {
  790. del_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
  791. del_symlink(bdev->bd_part->holder_dir,
  792. &disk_to_dev(disk)->kobj);
  793. list_del_init(&holder->list);
  794. kfree(holder);
  795. }
  796. mutex_unlock(&bdev->bd_mutex);
  797. }
  798. EXPORT_SYMBOL_GPL(bd_unlink_disk_holder);
  799. #endif
  800. /**
  801. * flush_disk - invalidates all buffer-cache entries on a disk
  802. *
  803. * @bdev: struct block device to be flushed
  804. *
  805. * Invalidates all buffer-cache entries on a disk. It should be called
  806. * when a disk has been changed -- either by a media change or online
  807. * resize.
  808. */
  809. static void flush_disk(struct block_device *bdev)
  810. {
  811. if (__invalidate_device(bdev)) {
  812. char name[BDEVNAME_SIZE] = "";
  813. if (bdev->bd_disk)
  814. disk_name(bdev->bd_disk, 0, name);
  815. printk(KERN_WARNING "VFS: busy inodes on changed media or "
  816. "resized disk %s\n", name);
  817. }
  818. if (!bdev->bd_disk)
  819. return;
  820. if (disk_partitionable(bdev->bd_disk))
  821. bdev->bd_invalidated = 1;
  822. }
  823. /**
  824. * check_disk_size_change - checks for disk size change and adjusts bdev size.
  825. * @disk: struct gendisk to check
  826. * @bdev: struct bdev to adjust.
  827. *
  828. * This routine checks to see if the bdev size does not match the disk size
  829. * and adjusts it if it differs.
  830. */
  831. void check_disk_size_change(struct gendisk *disk, struct block_device *bdev)
  832. {
  833. loff_t disk_size, bdev_size;
  834. disk_size = (loff_t)get_capacity(disk) << 9;
  835. bdev_size = i_size_read(bdev->bd_inode);
  836. if (disk_size != bdev_size) {
  837. char name[BDEVNAME_SIZE];
  838. disk_name(disk, 0, name);
  839. printk(KERN_INFO
  840. "%s: detected capacity change from %lld to %lld\n",
  841. name, bdev_size, disk_size);
  842. i_size_write(bdev->bd_inode, disk_size);
  843. flush_disk(bdev);
  844. }
  845. }
  846. EXPORT_SYMBOL(check_disk_size_change);
  847. /**
  848. * revalidate_disk - wrapper for lower-level driver's revalidate_disk call-back
  849. * @disk: struct gendisk to be revalidated
  850. *
  851. * This routine is a wrapper for lower-level driver's revalidate_disk
  852. * call-backs. It is used to do common pre and post operations needed
  853. * for all revalidate_disk operations.
  854. */
  855. int revalidate_disk(struct gendisk *disk)
  856. {
  857. struct block_device *bdev;
  858. int ret = 0;
  859. if (disk->fops->revalidate_disk)
  860. ret = disk->fops->revalidate_disk(disk);
  861. bdev = bdget_disk(disk, 0);
  862. if (!bdev)
  863. return ret;
  864. mutex_lock(&bdev->bd_mutex);
  865. check_disk_size_change(disk, bdev);
  866. mutex_unlock(&bdev->bd_mutex);
  867. bdput(bdev);
  868. return ret;
  869. }
  870. EXPORT_SYMBOL(revalidate_disk);
  871. /*
  872. * This routine checks whether a removable media has been changed,
  873. * and invalidates all buffer-cache-entries in that case. This
  874. * is a relatively slow routine, so we have to try to minimize using
  875. * it. Thus it is called only upon a 'mount' or 'open'. This
  876. * is the best way of combining speed and utility, I think.
  877. * People changing diskettes in the middle of an operation deserve
  878. * to lose :-)
  879. */
  880. int check_disk_change(struct block_device *bdev)
  881. {
  882. struct gendisk *disk = bdev->bd_disk;
  883. const struct block_device_operations *bdops = disk->fops;
  884. unsigned int events;
  885. events = disk_clear_events(disk, DISK_EVENT_MEDIA_CHANGE |
  886. DISK_EVENT_EJECT_REQUEST);
  887. if (!(events & DISK_EVENT_MEDIA_CHANGE))
  888. return 0;
  889. flush_disk(bdev);
  890. if (bdops->revalidate_disk)
  891. bdops->revalidate_disk(bdev->bd_disk);
  892. return 1;
  893. }
  894. EXPORT_SYMBOL(check_disk_change);
  895. void bd_set_size(struct block_device *bdev, loff_t size)
  896. {
  897. unsigned bsize = bdev_logical_block_size(bdev);
  898. bdev->bd_inode->i_size = size;
  899. while (bsize < PAGE_CACHE_SIZE) {
  900. if (size & bsize)
  901. break;
  902. bsize <<= 1;
  903. }
  904. bdev->bd_block_size = bsize;
  905. bdev->bd_inode->i_blkbits = blksize_bits(bsize);
  906. }
  907. EXPORT_SYMBOL(bd_set_size);
  908. static int __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part);
  909. /*
  910. * bd_mutex locking:
  911. *
  912. * mutex_lock(part->bd_mutex)
  913. * mutex_lock_nested(whole->bd_mutex, 1)
  914. */
  915. static int __blkdev_get(struct block_device *bdev, fmode_t mode, int for_part)
  916. {
  917. struct gendisk *disk;
  918. int ret;
  919. int partno;
  920. int perm = 0;
  921. if (mode & FMODE_READ)
  922. perm |= MAY_READ;
  923. if (mode & FMODE_WRITE)
  924. perm |= MAY_WRITE;
  925. /*
  926. * hooks: /n/, see "layering violations".
  927. */
  928. if (!for_part) {
  929. ret = devcgroup_inode_permission(bdev->bd_inode, perm);
  930. if (ret != 0) {
  931. bdput(bdev);
  932. return ret;
  933. }
  934. }
  935. restart:
  936. ret = -ENXIO;
  937. disk = get_gendisk(bdev->bd_dev, &partno);
  938. if (!disk)
  939. goto out;
  940. mutex_lock_nested(&bdev->bd_mutex, for_part);
  941. if (!bdev->bd_openers) {
  942. bdev->bd_disk = disk;
  943. bdev->bd_contains = bdev;
  944. if (!partno) {
  945. struct backing_dev_info *bdi;
  946. ret = -ENXIO;
  947. bdev->bd_part = disk_get_part(disk, partno);
  948. if (!bdev->bd_part)
  949. goto out_clear;
  950. if (disk->fops->open) {
  951. ret = disk->fops->open(bdev, mode);
  952. if (ret == -ERESTARTSYS) {
  953. /* Lost a race with 'disk' being
  954. * deleted, try again.
  955. * See md.c
  956. */
  957. disk_put_part(bdev->bd_part);
  958. bdev->bd_part = NULL;
  959. module_put(disk->fops->owner);
  960. put_disk(disk);
  961. bdev->bd_disk = NULL;
  962. mutex_unlock(&bdev->bd_mutex);
  963. goto restart;
  964. }
  965. if (ret)
  966. goto out_clear;
  967. }
  968. if (!bdev->bd_openers) {
  969. bd_set_size(bdev,(loff_t)get_capacity(disk)<<9);
  970. bdi = blk_get_backing_dev_info(bdev);
  971. if (bdi == NULL)
  972. bdi = &default_backing_dev_info;
  973. bdev_inode_switch_bdi(bdev->bd_inode, bdi);
  974. }
  975. if (bdev->bd_invalidated)
  976. rescan_partitions(disk, bdev);
  977. } else {
  978. struct block_device *whole;
  979. whole = bdget_disk(disk, 0);
  980. ret = -ENOMEM;
  981. if (!whole)
  982. goto out_clear;
  983. BUG_ON(for_part);
  984. ret = __blkdev_get(whole, mode, 1);
  985. if (ret)
  986. goto out_clear;
  987. bdev->bd_contains = whole;
  988. bdev_inode_switch_bdi(bdev->bd_inode,
  989. whole->bd_inode->i_data.backing_dev_info);
  990. bdev->bd_part = disk_get_part(disk, partno);
  991. if (!(disk->flags & GENHD_FL_UP) ||
  992. !bdev->bd_part || !bdev->bd_part->nr_sects) {
  993. ret = -ENXIO;
  994. goto out_clear;
  995. }
  996. bd_set_size(bdev, (loff_t)bdev->bd_part->nr_sects << 9);
  997. }
  998. } else {
  999. module_put(disk->fops->owner);
  1000. put_disk(disk);
  1001. disk = NULL;
  1002. if (bdev->bd_contains == bdev) {
  1003. if (bdev->bd_disk->fops->open) {
  1004. ret = bdev->bd_disk->fops->open(bdev, mode);
  1005. if (ret)
  1006. goto out_unlock_bdev;
  1007. }
  1008. if (bdev->bd_invalidated)
  1009. rescan_partitions(bdev->bd_disk, bdev);
  1010. }
  1011. }
  1012. bdev->bd_openers++;
  1013. if (for_part)
  1014. bdev->bd_part_count++;
  1015. mutex_unlock(&bdev->bd_mutex);
  1016. return 0;
  1017. out_clear:
  1018. disk_put_part(bdev->bd_part);
  1019. bdev->bd_disk = NULL;
  1020. bdev->bd_part = NULL;
  1021. bdev_inode_switch_bdi(bdev->bd_inode, &default_backing_dev_info);
  1022. if (bdev != bdev->bd_contains)
  1023. __blkdev_put(bdev->bd_contains, mode, 1);
  1024. bdev->bd_contains = NULL;
  1025. out_unlock_bdev:
  1026. mutex_unlock(&bdev->bd_mutex);
  1027. out:
  1028. if (disk)
  1029. module_put(disk->fops->owner);
  1030. put_disk(disk);
  1031. bdput(bdev);
  1032. return ret;
  1033. }
  1034. /**
  1035. * blkdev_get - open a block device
  1036. * @bdev: block_device to open
  1037. * @mode: FMODE_* mask
  1038. * @holder: exclusive holder identifier
  1039. *
  1040. * Open @bdev with @mode. If @mode includes %FMODE_EXCL, @bdev is
  1041. * open with exclusive access. Specifying %FMODE_EXCL with %NULL
  1042. * @holder is invalid. Exclusive opens may nest for the same @holder.
  1043. *
  1044. * On success, the reference count of @bdev is unchanged. On failure,
  1045. * @bdev is put.
  1046. *
  1047. * CONTEXT:
  1048. * Might sleep.
  1049. *
  1050. * RETURNS:
  1051. * 0 on success, -errno on failure.
  1052. */
  1053. int blkdev_get(struct block_device *bdev, fmode_t mode, void *holder)
  1054. {
  1055. struct block_device *whole = NULL;
  1056. int res;
  1057. WARN_ON_ONCE((mode & FMODE_EXCL) && !holder);
  1058. if ((mode & FMODE_EXCL) && holder) {
  1059. whole = bd_start_claiming(bdev, holder);
  1060. if (IS_ERR(whole)) {
  1061. bdput(bdev);
  1062. return PTR_ERR(whole);
  1063. }
  1064. }
  1065. res = __blkdev_get(bdev, mode, 0);
  1066. if (whole) {
  1067. /* finish claiming */
  1068. mutex_lock(&bdev->bd_mutex);
  1069. spin_lock(&bdev_lock);
  1070. if (!res) {
  1071. BUG_ON(!bd_may_claim(bdev, whole, holder));
  1072. /*
  1073. * Note that for a whole device bd_holders
  1074. * will be incremented twice, and bd_holder
  1075. * will be set to bd_may_claim before being
  1076. * set to holder
  1077. */
  1078. whole->bd_holders++;
  1079. whole->bd_holder = bd_may_claim;
  1080. bdev->bd_holders++;
  1081. bdev->bd_holder = holder;
  1082. }
  1083. /* tell others that we're done */
  1084. BUG_ON(whole->bd_claiming != holder);
  1085. whole->bd_claiming = NULL;
  1086. wake_up_bit(&whole->bd_claiming, 0);
  1087. spin_unlock(&bdev_lock);
  1088. /*
  1089. * Block event polling for write claims. Any write
  1090. * holder makes the write_holder state stick until all
  1091. * are released. This is good enough and tracking
  1092. * individual writeable reference is too fragile given
  1093. * the way @mode is used in blkdev_get/put().
  1094. */
  1095. if (!res && (mode & FMODE_WRITE) && !bdev->bd_write_holder) {
  1096. bdev->bd_write_holder = true;
  1097. disk_block_events(bdev->bd_disk);
  1098. }
  1099. mutex_unlock(&bdev->bd_mutex);
  1100. bdput(whole);
  1101. }
  1102. return res;
  1103. }
  1104. EXPORT_SYMBOL(blkdev_get);
  1105. /**
  1106. * blkdev_get_by_path - open a block device by name
  1107. * @path: path to the block device to open
  1108. * @mode: FMODE_* mask
  1109. * @holder: exclusive holder identifier
  1110. *
  1111. * Open the blockdevice described by the device file at @path. @mode
  1112. * and @holder are identical to blkdev_get().
  1113. *
  1114. * On success, the returned block_device has reference count of one.
  1115. *
  1116. * CONTEXT:
  1117. * Might sleep.
  1118. *
  1119. * RETURNS:
  1120. * Pointer to block_device on success, ERR_PTR(-errno) on failure.
  1121. */
  1122. struct block_device *blkdev_get_by_path(const char *path, fmode_t mode,
  1123. void *holder)
  1124. {
  1125. struct block_device *bdev;
  1126. int err;
  1127. bdev = lookup_bdev(path);
  1128. if (IS_ERR(bdev))
  1129. return bdev;
  1130. err = blkdev_get(bdev, mode, holder);
  1131. if (err)
  1132. return ERR_PTR(err);
  1133. if ((mode & FMODE_WRITE) && bdev_read_only(bdev)) {
  1134. blkdev_put(bdev, mode);
  1135. return ERR_PTR(-EACCES);
  1136. }
  1137. return bdev;
  1138. }
  1139. EXPORT_SYMBOL(blkdev_get_by_path);
  1140. /**
  1141. * blkdev_get_by_dev - open a block device by device number
  1142. * @dev: device number of block device to open
  1143. * @mode: FMODE_* mask
  1144. * @holder: exclusive holder identifier
  1145. *
  1146. * Open the blockdevice described by device number @dev. @mode and
  1147. * @holder are identical to blkdev_get().
  1148. *
  1149. * Use it ONLY if you really do not have anything better - i.e. when
  1150. * you are behind a truly sucky interface and all you are given is a
  1151. * device number. _Never_ to be used for internal purposes. If you
  1152. * ever need it - reconsider your API.
  1153. *
  1154. * On success, the returned block_device has reference count of one.
  1155. *
  1156. * CONTEXT:
  1157. * Might sleep.
  1158. *
  1159. * RETURNS:
  1160. * Pointer to block_device on success, ERR_PTR(-errno) on failure.
  1161. */
  1162. struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder)
  1163. {
  1164. struct block_device *bdev;
  1165. int err;
  1166. bdev = bdget(dev);
  1167. if (!bdev)
  1168. return ERR_PTR(-ENOMEM);
  1169. err = blkdev_get(bdev, mode, holder);
  1170. if (err)
  1171. return ERR_PTR(err);
  1172. return bdev;
  1173. }
  1174. EXPORT_SYMBOL(blkdev_get_by_dev);
  1175. static int blkdev_open(struct inode * inode, struct file * filp)
  1176. {
  1177. struct block_device *bdev;
  1178. /*
  1179. * Preserve backwards compatibility and allow large file access
  1180. * even if userspace doesn't ask for it explicitly. Some mkfs
  1181. * binary needs it. We might want to drop this workaround
  1182. * during an unstable branch.
  1183. */
  1184. filp->f_flags |= O_LARGEFILE;
  1185. if (filp->f_flags & O_NDELAY)
  1186. filp->f_mode |= FMODE_NDELAY;
  1187. if (filp->f_flags & O_EXCL)
  1188. filp->f_mode |= FMODE_EXCL;
  1189. if ((filp->f_flags & O_ACCMODE) == 3)
  1190. filp->f_mode |= FMODE_WRITE_IOCTL;
  1191. bdev = bd_acquire(inode);
  1192. if (bdev == NULL)
  1193. return -ENOMEM;
  1194. filp->f_mapping = bdev->bd_inode->i_mapping;
  1195. return blkdev_get(bdev, filp->f_mode, filp);
  1196. }
  1197. static int __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part)
  1198. {
  1199. int ret = 0;
  1200. struct gendisk *disk = bdev->bd_disk;
  1201. struct block_device *victim = NULL;
  1202. mutex_lock_nested(&bdev->bd_mutex, for_part);
  1203. if (for_part)
  1204. bdev->bd_part_count--;
  1205. if (!--bdev->bd_openers) {
  1206. WARN_ON_ONCE(bdev->bd_holders);
  1207. sync_blockdev(bdev);
  1208. kill_bdev(bdev);
  1209. }
  1210. if (bdev->bd_contains == bdev) {
  1211. if (disk->fops->release)
  1212. ret = disk->fops->release(disk, mode);
  1213. }
  1214. if (!bdev->bd_openers) {
  1215. struct module *owner = disk->fops->owner;
  1216. put_disk(disk);
  1217. module_put(owner);
  1218. disk_put_part(bdev->bd_part);
  1219. bdev->bd_part = NULL;
  1220. bdev->bd_disk = NULL;
  1221. bdev_inode_switch_bdi(bdev->bd_inode,
  1222. &default_backing_dev_info);
  1223. if (bdev != bdev->bd_contains)
  1224. victim = bdev->bd_contains;
  1225. bdev->bd_contains = NULL;
  1226. }
  1227. mutex_unlock(&bdev->bd_mutex);
  1228. bdput(bdev);
  1229. if (victim)
  1230. __blkdev_put(victim, mode, 1);
  1231. return ret;
  1232. }
  1233. int blkdev_put(struct block_device *bdev, fmode_t mode)
  1234. {
  1235. if (mode & FMODE_EXCL) {
  1236. bool bdev_free;
  1237. /*
  1238. * Release a claim on the device. The holder fields
  1239. * are protected with bdev_lock. bd_mutex is to
  1240. * synchronize disk_holder unlinking.
  1241. */
  1242. mutex_lock(&bdev->bd_mutex);
  1243. spin_lock(&bdev_lock);
  1244. WARN_ON_ONCE(--bdev->bd_holders < 0);
  1245. WARN_ON_ONCE(--bdev->bd_contains->bd_holders < 0);
  1246. /* bd_contains might point to self, check in a separate step */
  1247. if ((bdev_free = !bdev->bd_holders))
  1248. bdev->bd_holder = NULL;
  1249. if (!bdev->bd_contains->bd_holders)
  1250. bdev->bd_contains->bd_holder = NULL;
  1251. spin_unlock(&bdev_lock);
  1252. /*
  1253. * If this was the last claim, remove holder link and
  1254. * unblock evpoll if it was a write holder.
  1255. */
  1256. if (bdev_free) {
  1257. if (bdev->bd_write_holder) {
  1258. disk_unblock_events(bdev->bd_disk);
  1259. bdev->bd_write_holder = false;
  1260. } else
  1261. disk_check_events(bdev->bd_disk);
  1262. }
  1263. mutex_unlock(&bdev->bd_mutex);
  1264. } else
  1265. disk_check_events(bdev->bd_disk);
  1266. return __blkdev_put(bdev, mode, 0);
  1267. }
  1268. EXPORT_SYMBOL(blkdev_put);
  1269. static int blkdev_close(struct inode * inode, struct file * filp)
  1270. {
  1271. struct block_device *bdev = I_BDEV(filp->f_mapping->host);
  1272. return blkdev_put(bdev, filp->f_mode);
  1273. }
  1274. static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg)
  1275. {
  1276. struct block_device *bdev = I_BDEV(file->f_mapping->host);
  1277. fmode_t mode = file->f_mode;
  1278. /*
  1279. * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
  1280. * to updated it before every ioctl.
  1281. */
  1282. if (file->f_flags & O_NDELAY)
  1283. mode |= FMODE_NDELAY;
  1284. else
  1285. mode &= ~FMODE_NDELAY;
  1286. return blkdev_ioctl(bdev, mode, cmd, arg);
  1287. }
  1288. /*
  1289. * Write data to the block device. Only intended for the block device itself
  1290. * and the raw driver which basically is a fake block device.
  1291. *
  1292. * Does not take i_mutex for the write and thus is not for general purpose
  1293. * use.
  1294. */
  1295. ssize_t blkdev_aio_write(struct kiocb *iocb, const struct iovec *iov,
  1296. unsigned long nr_segs, loff_t pos)
  1297. {
  1298. struct file *file = iocb->ki_filp;
  1299. ssize_t ret;
  1300. BUG_ON(iocb->ki_pos != pos);
  1301. ret = __generic_file_aio_write(iocb, iov, nr_segs, &iocb->ki_pos);
  1302. if (ret > 0 || ret == -EIOCBQUEUED) {
  1303. ssize_t err;
  1304. err = generic_write_sync(file, pos, ret);
  1305. if (err < 0 && ret > 0)
  1306. ret = err;
  1307. }
  1308. return ret;
  1309. }
  1310. EXPORT_SYMBOL_GPL(blkdev_aio_write);
  1311. /*
  1312. * Try to release a page associated with block device when the system
  1313. * is under memory pressure.
  1314. */
  1315. static int blkdev_releasepage(struct page *page, gfp_t wait)
  1316. {
  1317. struct super_block *super = BDEV_I(page->mapping->host)->bdev.bd_super;
  1318. if (super && super->s_op->bdev_try_to_free_page)
  1319. return super->s_op->bdev_try_to_free_page(super, page, wait);
  1320. return try_to_free_buffers(page);
  1321. }
  1322. static const struct address_space_operations def_blk_aops = {
  1323. .readpage = blkdev_readpage,
  1324. .writepage = blkdev_writepage,
  1325. .sync_page = block_sync_page,
  1326. .write_begin = blkdev_write_begin,
  1327. .write_end = blkdev_write_end,
  1328. .writepages = generic_writepages,
  1329. .releasepage = blkdev_releasepage,
  1330. .direct_IO = blkdev_direct_IO,
  1331. };
  1332. const struct file_operations def_blk_fops = {
  1333. .open = blkdev_open,
  1334. .release = blkdev_close,
  1335. .llseek = block_llseek,
  1336. .read = do_sync_read,
  1337. .write = do_sync_write,
  1338. .aio_read = generic_file_aio_read,
  1339. .aio_write = blkdev_aio_write,
  1340. .mmap = generic_file_mmap,
  1341. .fsync = blkdev_fsync,
  1342. .unlocked_ioctl = block_ioctl,
  1343. #ifdef CONFIG_COMPAT
  1344. .compat_ioctl = compat_blkdev_ioctl,
  1345. #endif
  1346. .splice_read = generic_file_splice_read,
  1347. .splice_write = generic_file_splice_write,
  1348. };
  1349. int ioctl_by_bdev(struct block_device *bdev, unsigned cmd, unsigned long arg)
  1350. {
  1351. int res;
  1352. mm_segment_t old_fs = get_fs();
  1353. set_fs(KERNEL_DS);
  1354. res = blkdev_ioctl(bdev, 0, cmd, arg);
  1355. set_fs(old_fs);
  1356. return res;
  1357. }
  1358. EXPORT_SYMBOL(ioctl_by_bdev);
  1359. /**
  1360. * lookup_bdev - lookup a struct block_device by name
  1361. * @pathname: special file representing the block device
  1362. *
  1363. * Get a reference to the blockdevice at @pathname in the current
  1364. * namespace if possible and return it. Return ERR_PTR(error)
  1365. * otherwise.
  1366. */
  1367. struct block_device *lookup_bdev(const char *pathname)
  1368. {
  1369. struct block_device *bdev;
  1370. struct inode *inode;
  1371. struct path path;
  1372. int error;
  1373. if (!pathname || !*pathname)
  1374. return ERR_PTR(-EINVAL);
  1375. error = kern_path(pathname, LOOKUP_FOLLOW, &path);
  1376. if (error)
  1377. return ERR_PTR(error);
  1378. inode = path.dentry->d_inode;
  1379. error = -ENOTBLK;
  1380. if (!S_ISBLK(inode->i_mode))
  1381. goto fail;
  1382. error = -EACCES;
  1383. if (path.mnt->mnt_flags & MNT_NODEV)
  1384. goto fail;
  1385. error = -ENOMEM;
  1386. bdev = bd_acquire(inode);
  1387. if (!bdev)
  1388. goto fail;
  1389. out:
  1390. path_put(&path);
  1391. return bdev;
  1392. fail:
  1393. bdev = ERR_PTR(error);
  1394. goto out;
  1395. }
  1396. EXPORT_SYMBOL(lookup_bdev);
  1397. int __invalidate_device(struct block_device *bdev)
  1398. {
  1399. struct super_block *sb = get_super(bdev);
  1400. int res = 0;
  1401. if (sb) {
  1402. /*
  1403. * no need to lock the super, get_super holds the
  1404. * read mutex so the filesystem cannot go away
  1405. * under us (->put_super runs with the write lock
  1406. * hold).
  1407. */
  1408. shrink_dcache_sb(sb);
  1409. res = invalidate_inodes(sb);
  1410. drop_super(sb);
  1411. }
  1412. invalidate_bdev(bdev);
  1413. return res;
  1414. }
  1415. EXPORT_SYMBOL(__invalidate_device);