block_dev.c 39 KB

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