block_dev.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236
  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/config.h>
  8. #include <linux/init.h>
  9. #include <linux/mm.h>
  10. #include <linux/fcntl.h>
  11. #include <linux/slab.h>
  12. #include <linux/kmod.h>
  13. #include <linux/major.h>
  14. #include <linux/smp_lock.h>
  15. #include <linux/highmem.h>
  16. #include <linux/blkdev.h>
  17. #include <linux/module.h>
  18. #include <linux/blkpg.h>
  19. #include <linux/buffer_head.h>
  20. #include <linux/mpage.h>
  21. #include <linux/mount.h>
  22. #include <linux/uio.h>
  23. #include <linux/namei.h>
  24. #include <asm/uaccess.h>
  25. struct bdev_inode {
  26. struct block_device bdev;
  27. struct inode vfs_inode;
  28. };
  29. static inline struct bdev_inode *BDEV_I(struct inode *inode)
  30. {
  31. return container_of(inode, struct bdev_inode, vfs_inode);
  32. }
  33. inline struct block_device *I_BDEV(struct inode *inode)
  34. {
  35. return &BDEV_I(inode)->bdev;
  36. }
  37. EXPORT_SYMBOL(I_BDEV);
  38. static sector_t max_block(struct block_device *bdev)
  39. {
  40. sector_t retval = ~((sector_t)0);
  41. loff_t sz = i_size_read(bdev->bd_inode);
  42. if (sz) {
  43. unsigned int size = block_size(bdev);
  44. unsigned int sizebits = blksize_bits(size);
  45. retval = (sz >> sizebits);
  46. }
  47. return retval;
  48. }
  49. /* Kill _all_ buffers, dirty or not.. */
  50. static void kill_bdev(struct block_device *bdev)
  51. {
  52. invalidate_bdev(bdev, 1);
  53. truncate_inode_pages(bdev->bd_inode->i_mapping, 0);
  54. }
  55. int set_blocksize(struct block_device *bdev, int size)
  56. {
  57. /* Size must be a power of two, and between 512 and PAGE_SIZE */
  58. if (size > PAGE_SIZE || size < 512 || (size & (size-1)))
  59. return -EINVAL;
  60. /* Size cannot be smaller than the size supported by the device */
  61. if (size < bdev_hardsect_size(bdev))
  62. return -EINVAL;
  63. /* Don't change the size if it is same as current */
  64. if (bdev->bd_block_size != size) {
  65. sync_blockdev(bdev);
  66. bdev->bd_block_size = size;
  67. bdev->bd_inode->i_blkbits = blksize_bits(size);
  68. kill_bdev(bdev);
  69. }
  70. return 0;
  71. }
  72. EXPORT_SYMBOL(set_blocksize);
  73. int sb_set_blocksize(struct super_block *sb, int size)
  74. {
  75. if (set_blocksize(sb->s_bdev, size))
  76. return 0;
  77. /* If we get here, we know size is power of two
  78. * and it's value is between 512 and PAGE_SIZE */
  79. sb->s_blocksize = size;
  80. sb->s_blocksize_bits = blksize_bits(size);
  81. return sb->s_blocksize;
  82. }
  83. EXPORT_SYMBOL(sb_set_blocksize);
  84. int sb_min_blocksize(struct super_block *sb, int size)
  85. {
  86. int minsize = bdev_hardsect_size(sb->s_bdev);
  87. if (size < minsize)
  88. size = minsize;
  89. return sb_set_blocksize(sb, size);
  90. }
  91. EXPORT_SYMBOL(sb_min_blocksize);
  92. static int
  93. blkdev_get_block(struct inode *inode, sector_t iblock,
  94. struct buffer_head *bh, int create)
  95. {
  96. if (iblock >= max_block(I_BDEV(inode))) {
  97. if (create)
  98. return -EIO;
  99. /*
  100. * for reads, we're just trying to fill a partial page.
  101. * return a hole, they will have to call get_block again
  102. * before they can fill it, and they will get -EIO at that
  103. * time
  104. */
  105. return 0;
  106. }
  107. bh->b_bdev = I_BDEV(inode);
  108. bh->b_blocknr = iblock;
  109. set_buffer_mapped(bh);
  110. return 0;
  111. }
  112. static int
  113. blkdev_get_blocks(struct inode *inode, sector_t iblock,
  114. struct buffer_head *bh, int create)
  115. {
  116. sector_t end_block = max_block(I_BDEV(inode));
  117. unsigned long max_blocks = bh->b_size >> inode->i_blkbits;
  118. if ((iblock + max_blocks) > end_block) {
  119. max_blocks = end_block - iblock;
  120. if ((long)max_blocks <= 0) {
  121. if (create)
  122. return -EIO; /* write fully beyond EOF */
  123. /*
  124. * It is a read which is fully beyond EOF. We return
  125. * a !buffer_mapped buffer
  126. */
  127. max_blocks = 0;
  128. }
  129. }
  130. bh->b_bdev = I_BDEV(inode);
  131. bh->b_blocknr = iblock;
  132. bh->b_size = max_blocks << inode->i_blkbits;
  133. if (max_blocks)
  134. set_buffer_mapped(bh);
  135. return 0;
  136. }
  137. static ssize_t
  138. blkdev_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
  139. loff_t offset, unsigned long nr_segs)
  140. {
  141. struct file *file = iocb->ki_filp;
  142. struct inode *inode = file->f_mapping->host;
  143. return blockdev_direct_IO_no_locking(rw, iocb, inode, I_BDEV(inode),
  144. iov, offset, nr_segs, blkdev_get_blocks, NULL);
  145. }
  146. static int blkdev_writepage(struct page *page, struct writeback_control *wbc)
  147. {
  148. return block_write_full_page(page, blkdev_get_block, wbc);
  149. }
  150. static int blkdev_readpage(struct file * file, struct page * page)
  151. {
  152. return block_read_full_page(page, blkdev_get_block);
  153. }
  154. static int blkdev_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to)
  155. {
  156. return block_prepare_write(page, from, to, blkdev_get_block);
  157. }
  158. static int blkdev_commit_write(struct file *file, struct page *page, unsigned from, unsigned to)
  159. {
  160. return block_commit_write(page, from, to);
  161. }
  162. /*
  163. * private llseek:
  164. * for a block special file file->f_dentry->d_inode->i_size is zero
  165. * so we compute the size by hand (just as in block_read/write above)
  166. */
  167. static loff_t block_llseek(struct file *file, loff_t offset, int origin)
  168. {
  169. struct inode *bd_inode = file->f_mapping->host;
  170. loff_t size;
  171. loff_t retval;
  172. mutex_lock(&bd_inode->i_mutex);
  173. size = i_size_read(bd_inode);
  174. switch (origin) {
  175. case 2:
  176. offset += size;
  177. break;
  178. case 1:
  179. offset += file->f_pos;
  180. }
  181. retval = -EINVAL;
  182. if (offset >= 0 && offset <= size) {
  183. if (offset != file->f_pos) {
  184. file->f_pos = offset;
  185. }
  186. retval = offset;
  187. }
  188. mutex_unlock(&bd_inode->i_mutex);
  189. return retval;
  190. }
  191. /*
  192. * Filp is never NULL; the only case when ->fsync() is called with
  193. * NULL first argument is nfsd_sync_dir() and that's not a directory.
  194. */
  195. static int block_fsync(struct file *filp, struct dentry *dentry, int datasync)
  196. {
  197. return sync_blockdev(I_BDEV(filp->f_mapping->host));
  198. }
  199. /*
  200. * pseudo-fs
  201. */
  202. static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);
  203. static kmem_cache_t * bdev_cachep __read_mostly;
  204. static struct inode *bdev_alloc_inode(struct super_block *sb)
  205. {
  206. struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, SLAB_KERNEL);
  207. if (!ei)
  208. return NULL;
  209. return &ei->vfs_inode;
  210. }
  211. static void bdev_destroy_inode(struct inode *inode)
  212. {
  213. struct bdev_inode *bdi = BDEV_I(inode);
  214. bdi->bdev.bd_inode_backing_dev_info = NULL;
  215. kmem_cache_free(bdev_cachep, bdi);
  216. }
  217. static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
  218. {
  219. struct bdev_inode *ei = (struct bdev_inode *) foo;
  220. struct block_device *bdev = &ei->bdev;
  221. if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
  222. SLAB_CTOR_CONSTRUCTOR)
  223. {
  224. memset(bdev, 0, sizeof(*bdev));
  225. mutex_init(&bdev->bd_mutex);
  226. mutex_init(&bdev->bd_mount_mutex);
  227. INIT_LIST_HEAD(&bdev->bd_inodes);
  228. INIT_LIST_HEAD(&bdev->bd_list);
  229. #ifdef CONFIG_SYSFS
  230. INIT_LIST_HEAD(&bdev->bd_holder_list);
  231. #endif
  232. inode_init_once(&ei->vfs_inode);
  233. }
  234. }
  235. static inline void __bd_forget(struct inode *inode)
  236. {
  237. list_del_init(&inode->i_devices);
  238. inode->i_bdev = NULL;
  239. inode->i_mapping = &inode->i_data;
  240. }
  241. static void bdev_clear_inode(struct inode *inode)
  242. {
  243. struct block_device *bdev = &BDEV_I(inode)->bdev;
  244. struct list_head *p;
  245. spin_lock(&bdev_lock);
  246. while ( (p = bdev->bd_inodes.next) != &bdev->bd_inodes ) {
  247. __bd_forget(list_entry(p, struct inode, i_devices));
  248. }
  249. list_del_init(&bdev->bd_list);
  250. spin_unlock(&bdev_lock);
  251. }
  252. static struct super_operations bdev_sops = {
  253. .statfs = simple_statfs,
  254. .alloc_inode = bdev_alloc_inode,
  255. .destroy_inode = bdev_destroy_inode,
  256. .drop_inode = generic_delete_inode,
  257. .clear_inode = bdev_clear_inode,
  258. };
  259. static int bd_get_sb(struct file_system_type *fs_type,
  260. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  261. {
  262. return get_sb_pseudo(fs_type, "bdev:", &bdev_sops, 0x62646576, mnt);
  263. }
  264. static struct file_system_type bd_type = {
  265. .name = "bdev",
  266. .get_sb = bd_get_sb,
  267. .kill_sb = kill_anon_super,
  268. };
  269. static struct vfsmount *bd_mnt __read_mostly;
  270. struct super_block *blockdev_superblock;
  271. void __init bdev_cache_init(void)
  272. {
  273. int err;
  274. bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
  275. 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
  276. SLAB_MEM_SPREAD|SLAB_PANIC),
  277. init_once, NULL);
  278. err = register_filesystem(&bd_type);
  279. if (err)
  280. panic("Cannot register bdev pseudo-fs");
  281. bd_mnt = kern_mount(&bd_type);
  282. err = PTR_ERR(bd_mnt);
  283. if (IS_ERR(bd_mnt))
  284. panic("Cannot create bdev pseudo-fs");
  285. blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */
  286. }
  287. /*
  288. * Most likely _very_ bad one - but then it's hardly critical for small
  289. * /dev and can be fixed when somebody will need really large one.
  290. * Keep in mind that it will be fed through icache hash function too.
  291. */
  292. static inline unsigned long hash(dev_t dev)
  293. {
  294. return MAJOR(dev)+MINOR(dev);
  295. }
  296. static int bdev_test(struct inode *inode, void *data)
  297. {
  298. return BDEV_I(inode)->bdev.bd_dev == *(dev_t *)data;
  299. }
  300. static int bdev_set(struct inode *inode, void *data)
  301. {
  302. BDEV_I(inode)->bdev.bd_dev = *(dev_t *)data;
  303. return 0;
  304. }
  305. static LIST_HEAD(all_bdevs);
  306. struct block_device *bdget(dev_t dev)
  307. {
  308. struct block_device *bdev;
  309. struct inode *inode;
  310. inode = iget5_locked(bd_mnt->mnt_sb, hash(dev),
  311. bdev_test, bdev_set, &dev);
  312. if (!inode)
  313. return NULL;
  314. bdev = &BDEV_I(inode)->bdev;
  315. if (inode->i_state & I_NEW) {
  316. bdev->bd_contains = NULL;
  317. bdev->bd_inode = inode;
  318. bdev->bd_block_size = (1 << inode->i_blkbits);
  319. bdev->bd_part_count = 0;
  320. bdev->bd_invalidated = 0;
  321. inode->i_mode = S_IFBLK;
  322. inode->i_rdev = dev;
  323. inode->i_bdev = bdev;
  324. inode->i_data.a_ops = &def_blk_aops;
  325. mapping_set_gfp_mask(&inode->i_data, GFP_USER);
  326. inode->i_data.backing_dev_info = &default_backing_dev_info;
  327. spin_lock(&bdev_lock);
  328. list_add(&bdev->bd_list, &all_bdevs);
  329. spin_unlock(&bdev_lock);
  330. unlock_new_inode(inode);
  331. }
  332. return bdev;
  333. }
  334. EXPORT_SYMBOL(bdget);
  335. long nr_blockdev_pages(void)
  336. {
  337. struct list_head *p;
  338. long ret = 0;
  339. spin_lock(&bdev_lock);
  340. list_for_each(p, &all_bdevs) {
  341. struct block_device *bdev;
  342. bdev = list_entry(p, struct block_device, bd_list);
  343. ret += bdev->bd_inode->i_mapping->nrpages;
  344. }
  345. spin_unlock(&bdev_lock);
  346. return ret;
  347. }
  348. void bdput(struct block_device *bdev)
  349. {
  350. iput(bdev->bd_inode);
  351. }
  352. EXPORT_SYMBOL(bdput);
  353. static struct block_device *bd_acquire(struct inode *inode)
  354. {
  355. struct block_device *bdev;
  356. spin_lock(&bdev_lock);
  357. bdev = inode->i_bdev;
  358. if (bdev) {
  359. atomic_inc(&bdev->bd_inode->i_count);
  360. spin_unlock(&bdev_lock);
  361. return bdev;
  362. }
  363. spin_unlock(&bdev_lock);
  364. bdev = bdget(inode->i_rdev);
  365. if (bdev) {
  366. spin_lock(&bdev_lock);
  367. if (!inode->i_bdev) {
  368. /*
  369. * We take an additional bd_inode->i_count for inode,
  370. * and it's released in clear_inode() of inode.
  371. * So, we can access it via ->i_mapping always
  372. * without igrab().
  373. */
  374. atomic_inc(&bdev->bd_inode->i_count);
  375. inode->i_bdev = bdev;
  376. inode->i_mapping = bdev->bd_inode->i_mapping;
  377. list_add(&inode->i_devices, &bdev->bd_inodes);
  378. }
  379. spin_unlock(&bdev_lock);
  380. }
  381. return bdev;
  382. }
  383. /* Call when you free inode */
  384. void bd_forget(struct inode *inode)
  385. {
  386. struct block_device *bdev = NULL;
  387. spin_lock(&bdev_lock);
  388. if (inode->i_bdev) {
  389. if (inode->i_sb != blockdev_superblock)
  390. bdev = inode->i_bdev;
  391. __bd_forget(inode);
  392. }
  393. spin_unlock(&bdev_lock);
  394. if (bdev)
  395. iput(bdev->bd_inode);
  396. }
  397. int bd_claim(struct block_device *bdev, void *holder)
  398. {
  399. int res;
  400. spin_lock(&bdev_lock);
  401. /* first decide result */
  402. if (bdev->bd_holder == holder)
  403. res = 0; /* already a holder */
  404. else if (bdev->bd_holder != NULL)
  405. res = -EBUSY; /* held by someone else */
  406. else if (bdev->bd_contains == bdev)
  407. res = 0; /* is a whole device which isn't held */
  408. else if (bdev->bd_contains->bd_holder == bd_claim)
  409. res = 0; /* is a partition of a device that is being partitioned */
  410. else if (bdev->bd_contains->bd_holder != NULL)
  411. res = -EBUSY; /* is a partition of a held device */
  412. else
  413. res = 0; /* is a partition of an un-held device */
  414. /* now impose change */
  415. if (res==0) {
  416. /* note that for a whole device bd_holders
  417. * will be incremented twice, and bd_holder will
  418. * be set to bd_claim before being set to holder
  419. */
  420. bdev->bd_contains->bd_holders ++;
  421. bdev->bd_contains->bd_holder = bd_claim;
  422. bdev->bd_holders++;
  423. bdev->bd_holder = holder;
  424. }
  425. spin_unlock(&bdev_lock);
  426. return res;
  427. }
  428. EXPORT_SYMBOL(bd_claim);
  429. void bd_release(struct block_device *bdev)
  430. {
  431. spin_lock(&bdev_lock);
  432. if (!--bdev->bd_contains->bd_holders)
  433. bdev->bd_contains->bd_holder = NULL;
  434. if (!--bdev->bd_holders)
  435. bdev->bd_holder = NULL;
  436. spin_unlock(&bdev_lock);
  437. }
  438. EXPORT_SYMBOL(bd_release);
  439. #ifdef CONFIG_SYSFS
  440. /*
  441. * Functions for bd_claim_by_kobject / bd_release_from_kobject
  442. *
  443. * If a kobject is passed to bd_claim_by_kobject()
  444. * and the kobject has a parent directory,
  445. * following symlinks are created:
  446. * o from the kobject to the claimed bdev
  447. * o from "holders" directory of the bdev to the parent of the kobject
  448. * bd_release_from_kobject() removes these symlinks.
  449. *
  450. * Example:
  451. * If /dev/dm-0 maps to /dev/sda, kobject corresponding to
  452. * /sys/block/dm-0/slaves is passed to bd_claim_by_kobject(), then:
  453. * /sys/block/dm-0/slaves/sda --> /sys/block/sda
  454. * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
  455. */
  456. static struct kobject *bdev_get_kobj(struct block_device *bdev)
  457. {
  458. if (bdev->bd_contains != bdev)
  459. return kobject_get(&bdev->bd_part->kobj);
  460. else
  461. return kobject_get(&bdev->bd_disk->kobj);
  462. }
  463. static struct kobject *bdev_get_holder(struct block_device *bdev)
  464. {
  465. if (bdev->bd_contains != bdev)
  466. return kobject_get(bdev->bd_part->holder_dir);
  467. else
  468. return kobject_get(bdev->bd_disk->holder_dir);
  469. }
  470. static void add_symlink(struct kobject *from, struct kobject *to)
  471. {
  472. if (!from || !to)
  473. return;
  474. sysfs_create_link(from, to, kobject_name(to));
  475. }
  476. static void del_symlink(struct kobject *from, struct kobject *to)
  477. {
  478. if (!from || !to)
  479. return;
  480. sysfs_remove_link(from, kobject_name(to));
  481. }
  482. /*
  483. * 'struct bd_holder' contains pointers to kobjects symlinked by
  484. * bd_claim_by_kobject.
  485. * It's connected to bd_holder_list which is protected by bdev->bd_sem.
  486. */
  487. struct bd_holder {
  488. struct list_head list; /* chain of holders of the bdev */
  489. int count; /* references from the holder */
  490. struct kobject *sdir; /* holder object, e.g. "/block/dm-0/slaves" */
  491. struct kobject *hdev; /* e.g. "/block/dm-0" */
  492. struct kobject *hdir; /* e.g. "/block/sda/holders" */
  493. struct kobject *sdev; /* e.g. "/block/sda" */
  494. };
  495. /*
  496. * Get references of related kobjects at once.
  497. * Returns 1 on success. 0 on failure.
  498. *
  499. * Should call bd_holder_release_dirs() after successful use.
  500. */
  501. static int bd_holder_grab_dirs(struct block_device *bdev,
  502. struct bd_holder *bo)
  503. {
  504. if (!bdev || !bo)
  505. return 0;
  506. bo->sdir = kobject_get(bo->sdir);
  507. if (!bo->sdir)
  508. return 0;
  509. bo->hdev = kobject_get(bo->sdir->parent);
  510. if (!bo->hdev)
  511. goto fail_put_sdir;
  512. bo->sdev = bdev_get_kobj(bdev);
  513. if (!bo->sdev)
  514. goto fail_put_hdev;
  515. bo->hdir = bdev_get_holder(bdev);
  516. if (!bo->hdir)
  517. goto fail_put_sdev;
  518. return 1;
  519. fail_put_sdev:
  520. kobject_put(bo->sdev);
  521. fail_put_hdev:
  522. kobject_put(bo->hdev);
  523. fail_put_sdir:
  524. kobject_put(bo->sdir);
  525. return 0;
  526. }
  527. /* Put references of related kobjects at once. */
  528. static void bd_holder_release_dirs(struct bd_holder *bo)
  529. {
  530. kobject_put(bo->hdir);
  531. kobject_put(bo->sdev);
  532. kobject_put(bo->hdev);
  533. kobject_put(bo->sdir);
  534. }
  535. static struct bd_holder *alloc_bd_holder(struct kobject *kobj)
  536. {
  537. struct bd_holder *bo;
  538. bo = kzalloc(sizeof(*bo), GFP_KERNEL);
  539. if (!bo)
  540. return NULL;
  541. bo->count = 1;
  542. bo->sdir = kobj;
  543. return bo;
  544. }
  545. static void free_bd_holder(struct bd_holder *bo)
  546. {
  547. kfree(bo);
  548. }
  549. /**
  550. * add_bd_holder - create sysfs symlinks for bd_claim() relationship
  551. *
  552. * @bdev: block device to be bd_claimed
  553. * @bo: preallocated and initialized by alloc_bd_holder()
  554. *
  555. * If there is no matching entry with @bo in @bdev->bd_holder_list,
  556. * add @bo to the list, create symlinks.
  557. *
  558. * Returns 1 if @bo was added to the list.
  559. * Returns 0 if @bo wasn't used by any reason and should be freed.
  560. */
  561. static int add_bd_holder(struct block_device *bdev, struct bd_holder *bo)
  562. {
  563. struct bd_holder *tmp;
  564. if (!bo)
  565. return 0;
  566. list_for_each_entry(tmp, &bdev->bd_holder_list, list) {
  567. if (tmp->sdir == bo->sdir) {
  568. tmp->count++;
  569. return 0;
  570. }
  571. }
  572. if (!bd_holder_grab_dirs(bdev, bo))
  573. return 0;
  574. add_symlink(bo->sdir, bo->sdev);
  575. add_symlink(bo->hdir, bo->hdev);
  576. list_add_tail(&bo->list, &bdev->bd_holder_list);
  577. return 1;
  578. }
  579. /**
  580. * del_bd_holder - delete sysfs symlinks for bd_claim() relationship
  581. *
  582. * @bdev: block device to be bd_claimed
  583. * @kobj: holder's kobject
  584. *
  585. * If there is matching entry with @kobj in @bdev->bd_holder_list
  586. * and no other bd_claim() from the same kobject,
  587. * remove the struct bd_holder from the list, delete symlinks for it.
  588. *
  589. * Returns a pointer to the struct bd_holder when it's removed from the list
  590. * and ready to be freed.
  591. * Returns NULL if matching claim isn't found or there is other bd_claim()
  592. * by the same kobject.
  593. */
  594. static struct bd_holder *del_bd_holder(struct block_device *bdev,
  595. struct kobject *kobj)
  596. {
  597. struct bd_holder *bo;
  598. list_for_each_entry(bo, &bdev->bd_holder_list, list) {
  599. if (bo->sdir == kobj) {
  600. bo->count--;
  601. BUG_ON(bo->count < 0);
  602. if (!bo->count) {
  603. list_del(&bo->list);
  604. del_symlink(bo->sdir, bo->sdev);
  605. del_symlink(bo->hdir, bo->hdev);
  606. bd_holder_release_dirs(bo);
  607. return bo;
  608. }
  609. break;
  610. }
  611. }
  612. return NULL;
  613. }
  614. /**
  615. * bd_claim_by_kobject - bd_claim() with additional kobject signature
  616. *
  617. * @bdev: block device to be claimed
  618. * @holder: holder's signature
  619. * @kobj: holder's kobject
  620. *
  621. * Do bd_claim() and if it succeeds, create sysfs symlinks between
  622. * the bdev and the holder's kobject.
  623. * Use bd_release_from_kobject() when relesing the claimed bdev.
  624. *
  625. * Returns 0 on success. (same as bd_claim())
  626. * Returns errno on failure.
  627. */
  628. static int bd_claim_by_kobject(struct block_device *bdev, void *holder,
  629. struct kobject *kobj)
  630. {
  631. int res;
  632. struct bd_holder *bo;
  633. if (!kobj)
  634. return -EINVAL;
  635. bo = alloc_bd_holder(kobj);
  636. if (!bo)
  637. return -ENOMEM;
  638. mutex_lock(&bdev->bd_mutex);
  639. res = bd_claim(bdev, holder);
  640. if (res || !add_bd_holder(bdev, bo))
  641. free_bd_holder(bo);
  642. mutex_unlock(&bdev->bd_mutex);
  643. return res;
  644. }
  645. /**
  646. * bd_release_from_kobject - bd_release() with additional kobject signature
  647. *
  648. * @bdev: block device to be released
  649. * @kobj: holder's kobject
  650. *
  651. * Do bd_release() and remove sysfs symlinks created by bd_claim_by_kobject().
  652. */
  653. static void bd_release_from_kobject(struct block_device *bdev,
  654. struct kobject *kobj)
  655. {
  656. struct bd_holder *bo;
  657. if (!kobj)
  658. return;
  659. mutex_lock(&bdev->bd_mutex);
  660. bd_release(bdev);
  661. if ((bo = del_bd_holder(bdev, kobj)))
  662. free_bd_holder(bo);
  663. mutex_unlock(&bdev->bd_mutex);
  664. }
  665. /**
  666. * bd_claim_by_disk - wrapper function for bd_claim_by_kobject()
  667. *
  668. * @bdev: block device to be claimed
  669. * @holder: holder's signature
  670. * @disk: holder's gendisk
  671. *
  672. * Call bd_claim_by_kobject() with getting @disk->slave_dir.
  673. */
  674. int bd_claim_by_disk(struct block_device *bdev, void *holder,
  675. struct gendisk *disk)
  676. {
  677. return bd_claim_by_kobject(bdev, holder, kobject_get(disk->slave_dir));
  678. }
  679. EXPORT_SYMBOL_GPL(bd_claim_by_disk);
  680. /**
  681. * bd_release_from_disk - wrapper function for bd_release_from_kobject()
  682. *
  683. * @bdev: block device to be claimed
  684. * @disk: holder's gendisk
  685. *
  686. * Call bd_release_from_kobject() and put @disk->slave_dir.
  687. */
  688. void bd_release_from_disk(struct block_device *bdev, struct gendisk *disk)
  689. {
  690. bd_release_from_kobject(bdev, disk->slave_dir);
  691. kobject_put(disk->slave_dir);
  692. }
  693. EXPORT_SYMBOL_GPL(bd_release_from_disk);
  694. #endif
  695. /*
  696. * Tries to open block device by device number. Use it ONLY if you
  697. * really do not have anything better - i.e. when you are behind a
  698. * truly sucky interface and all you are given is a device number. _Never_
  699. * to be used for internal purposes. If you ever need it - reconsider
  700. * your API.
  701. */
  702. struct block_device *open_by_devnum(dev_t dev, unsigned mode)
  703. {
  704. struct block_device *bdev = bdget(dev);
  705. int err = -ENOMEM;
  706. int flags = mode & FMODE_WRITE ? O_RDWR : O_RDONLY;
  707. if (bdev)
  708. err = blkdev_get(bdev, mode, flags);
  709. return err ? ERR_PTR(err) : bdev;
  710. }
  711. EXPORT_SYMBOL(open_by_devnum);
  712. /*
  713. * This routine checks whether a removable media has been changed,
  714. * and invalidates all buffer-cache-entries in that case. This
  715. * is a relatively slow routine, so we have to try to minimize using
  716. * it. Thus it is called only upon a 'mount' or 'open'. This
  717. * is the best way of combining speed and utility, I think.
  718. * People changing diskettes in the middle of an operation deserve
  719. * to lose :-)
  720. */
  721. int check_disk_change(struct block_device *bdev)
  722. {
  723. struct gendisk *disk = bdev->bd_disk;
  724. struct block_device_operations * bdops = disk->fops;
  725. if (!bdops->media_changed)
  726. return 0;
  727. if (!bdops->media_changed(bdev->bd_disk))
  728. return 0;
  729. if (__invalidate_device(bdev))
  730. printk("VFS: busy inodes on changed media.\n");
  731. if (bdops->revalidate_disk)
  732. bdops->revalidate_disk(bdev->bd_disk);
  733. if (bdev->bd_disk->minors > 1)
  734. bdev->bd_invalidated = 1;
  735. return 1;
  736. }
  737. EXPORT_SYMBOL(check_disk_change);
  738. void bd_set_size(struct block_device *bdev, loff_t size)
  739. {
  740. unsigned bsize = bdev_hardsect_size(bdev);
  741. bdev->bd_inode->i_size = size;
  742. while (bsize < PAGE_CACHE_SIZE) {
  743. if (size & bsize)
  744. break;
  745. bsize <<= 1;
  746. }
  747. bdev->bd_block_size = bsize;
  748. bdev->bd_inode->i_blkbits = blksize_bits(bsize);
  749. }
  750. EXPORT_SYMBOL(bd_set_size);
  751. static int do_open(struct block_device *bdev, struct file *file)
  752. {
  753. struct module *owner = NULL;
  754. struct gendisk *disk;
  755. int ret = -ENXIO;
  756. int part;
  757. file->f_mapping = bdev->bd_inode->i_mapping;
  758. lock_kernel();
  759. disk = get_gendisk(bdev->bd_dev, &part);
  760. if (!disk) {
  761. unlock_kernel();
  762. bdput(bdev);
  763. return ret;
  764. }
  765. owner = disk->fops->owner;
  766. mutex_lock(&bdev->bd_mutex);
  767. if (!bdev->bd_openers) {
  768. bdev->bd_disk = disk;
  769. bdev->bd_contains = bdev;
  770. if (!part) {
  771. struct backing_dev_info *bdi;
  772. if (disk->fops->open) {
  773. ret = disk->fops->open(bdev->bd_inode, file);
  774. if (ret)
  775. goto out_first;
  776. }
  777. if (!bdev->bd_openers) {
  778. bd_set_size(bdev,(loff_t)get_capacity(disk)<<9);
  779. bdi = blk_get_backing_dev_info(bdev);
  780. if (bdi == NULL)
  781. bdi = &default_backing_dev_info;
  782. bdev->bd_inode->i_data.backing_dev_info = bdi;
  783. }
  784. if (bdev->bd_invalidated)
  785. rescan_partitions(disk, bdev);
  786. } else {
  787. struct hd_struct *p;
  788. struct block_device *whole;
  789. whole = bdget_disk(disk, 0);
  790. ret = -ENOMEM;
  791. if (!whole)
  792. goto out_first;
  793. ret = blkdev_get(whole, file->f_mode, file->f_flags);
  794. if (ret)
  795. goto out_first;
  796. bdev->bd_contains = whole;
  797. mutex_lock(&whole->bd_mutex);
  798. whole->bd_part_count++;
  799. p = disk->part[part - 1];
  800. bdev->bd_inode->i_data.backing_dev_info =
  801. whole->bd_inode->i_data.backing_dev_info;
  802. if (!(disk->flags & GENHD_FL_UP) || !p || !p->nr_sects) {
  803. whole->bd_part_count--;
  804. mutex_unlock(&whole->bd_mutex);
  805. ret = -ENXIO;
  806. goto out_first;
  807. }
  808. kobject_get(&p->kobj);
  809. bdev->bd_part = p;
  810. bd_set_size(bdev, (loff_t) p->nr_sects << 9);
  811. mutex_unlock(&whole->bd_mutex);
  812. }
  813. } else {
  814. put_disk(disk);
  815. module_put(owner);
  816. if (bdev->bd_contains == bdev) {
  817. if (bdev->bd_disk->fops->open) {
  818. ret = bdev->bd_disk->fops->open(bdev->bd_inode, file);
  819. if (ret)
  820. goto out;
  821. }
  822. if (bdev->bd_invalidated)
  823. rescan_partitions(bdev->bd_disk, bdev);
  824. } else {
  825. mutex_lock(&bdev->bd_contains->bd_mutex);
  826. bdev->bd_contains->bd_part_count++;
  827. mutex_unlock(&bdev->bd_contains->bd_mutex);
  828. }
  829. }
  830. bdev->bd_openers++;
  831. mutex_unlock(&bdev->bd_mutex);
  832. unlock_kernel();
  833. return 0;
  834. out_first:
  835. bdev->bd_disk = NULL;
  836. bdev->bd_inode->i_data.backing_dev_info = &default_backing_dev_info;
  837. if (bdev != bdev->bd_contains)
  838. blkdev_put(bdev->bd_contains);
  839. bdev->bd_contains = NULL;
  840. put_disk(disk);
  841. module_put(owner);
  842. out:
  843. mutex_unlock(&bdev->bd_mutex);
  844. unlock_kernel();
  845. if (ret)
  846. bdput(bdev);
  847. return ret;
  848. }
  849. int blkdev_get(struct block_device *bdev, mode_t mode, unsigned flags)
  850. {
  851. /*
  852. * This crockload is due to bad choice of ->open() type.
  853. * It will go away.
  854. * For now, block device ->open() routine must _not_
  855. * examine anything in 'inode' argument except ->i_rdev.
  856. */
  857. struct file fake_file = {};
  858. struct dentry fake_dentry = {};
  859. fake_file.f_mode = mode;
  860. fake_file.f_flags = flags;
  861. fake_file.f_dentry = &fake_dentry;
  862. fake_dentry.d_inode = bdev->bd_inode;
  863. return do_open(bdev, &fake_file);
  864. }
  865. EXPORT_SYMBOL(blkdev_get);
  866. static int blkdev_open(struct inode * inode, struct file * filp)
  867. {
  868. struct block_device *bdev;
  869. int res;
  870. /*
  871. * Preserve backwards compatibility and allow large file access
  872. * even if userspace doesn't ask for it explicitly. Some mkfs
  873. * binary needs it. We might want to drop this workaround
  874. * during an unstable branch.
  875. */
  876. filp->f_flags |= O_LARGEFILE;
  877. bdev = bd_acquire(inode);
  878. res = do_open(bdev, filp);
  879. if (res)
  880. return res;
  881. if (!(filp->f_flags & O_EXCL) )
  882. return 0;
  883. if (!(res = bd_claim(bdev, filp)))
  884. return 0;
  885. blkdev_put(bdev);
  886. return res;
  887. }
  888. int blkdev_put(struct block_device *bdev)
  889. {
  890. int ret = 0;
  891. struct inode *bd_inode = bdev->bd_inode;
  892. struct gendisk *disk = bdev->bd_disk;
  893. mutex_lock(&bdev->bd_mutex);
  894. lock_kernel();
  895. if (!--bdev->bd_openers) {
  896. sync_blockdev(bdev);
  897. kill_bdev(bdev);
  898. }
  899. if (bdev->bd_contains == bdev) {
  900. if (disk->fops->release)
  901. ret = disk->fops->release(bd_inode, NULL);
  902. } else {
  903. mutex_lock(&bdev->bd_contains->bd_mutex);
  904. bdev->bd_contains->bd_part_count--;
  905. mutex_unlock(&bdev->bd_contains->bd_mutex);
  906. }
  907. if (!bdev->bd_openers) {
  908. struct module *owner = disk->fops->owner;
  909. put_disk(disk);
  910. module_put(owner);
  911. if (bdev->bd_contains != bdev) {
  912. kobject_put(&bdev->bd_part->kobj);
  913. bdev->bd_part = NULL;
  914. }
  915. bdev->bd_disk = NULL;
  916. bdev->bd_inode->i_data.backing_dev_info = &default_backing_dev_info;
  917. if (bdev != bdev->bd_contains) {
  918. blkdev_put(bdev->bd_contains);
  919. }
  920. bdev->bd_contains = NULL;
  921. }
  922. unlock_kernel();
  923. mutex_unlock(&bdev->bd_mutex);
  924. bdput(bdev);
  925. return ret;
  926. }
  927. EXPORT_SYMBOL(blkdev_put);
  928. static int blkdev_close(struct inode * inode, struct file * filp)
  929. {
  930. struct block_device *bdev = I_BDEV(filp->f_mapping->host);
  931. if (bdev->bd_holder == filp)
  932. bd_release(bdev);
  933. return blkdev_put(bdev);
  934. }
  935. static ssize_t blkdev_file_write(struct file *file, const char __user *buf,
  936. size_t count, loff_t *ppos)
  937. {
  938. struct iovec local_iov = { .iov_base = (void __user *)buf, .iov_len = count };
  939. return generic_file_write_nolock(file, &local_iov, 1, ppos);
  940. }
  941. static ssize_t blkdev_file_aio_write(struct kiocb *iocb, const char __user *buf,
  942. size_t count, loff_t pos)
  943. {
  944. struct iovec local_iov = { .iov_base = (void __user *)buf, .iov_len = count };
  945. return generic_file_aio_write_nolock(iocb, &local_iov, 1, &iocb->ki_pos);
  946. }
  947. static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg)
  948. {
  949. return blkdev_ioctl(file->f_mapping->host, file, cmd, arg);
  950. }
  951. const struct address_space_operations def_blk_aops = {
  952. .readpage = blkdev_readpage,
  953. .writepage = blkdev_writepage,
  954. .sync_page = block_sync_page,
  955. .prepare_write = blkdev_prepare_write,
  956. .commit_write = blkdev_commit_write,
  957. .writepages = generic_writepages,
  958. .direct_IO = blkdev_direct_IO,
  959. };
  960. const struct file_operations def_blk_fops = {
  961. .open = blkdev_open,
  962. .release = blkdev_close,
  963. .llseek = block_llseek,
  964. .read = generic_file_read,
  965. .write = blkdev_file_write,
  966. .aio_read = generic_file_aio_read,
  967. .aio_write = blkdev_file_aio_write,
  968. .mmap = generic_file_mmap,
  969. .fsync = block_fsync,
  970. .unlocked_ioctl = block_ioctl,
  971. #ifdef CONFIG_COMPAT
  972. .compat_ioctl = compat_blkdev_ioctl,
  973. #endif
  974. .readv = generic_file_readv,
  975. .writev = generic_file_write_nolock,
  976. .sendfile = generic_file_sendfile,
  977. .splice_read = generic_file_splice_read,
  978. .splice_write = generic_file_splice_write,
  979. };
  980. int ioctl_by_bdev(struct block_device *bdev, unsigned cmd, unsigned long arg)
  981. {
  982. int res;
  983. mm_segment_t old_fs = get_fs();
  984. set_fs(KERNEL_DS);
  985. res = blkdev_ioctl(bdev->bd_inode, NULL, cmd, arg);
  986. set_fs(old_fs);
  987. return res;
  988. }
  989. EXPORT_SYMBOL(ioctl_by_bdev);
  990. /**
  991. * lookup_bdev - lookup a struct block_device by name
  992. *
  993. * @path: special file representing the block device
  994. *
  995. * Get a reference to the blockdevice at @path in the current
  996. * namespace if possible and return it. Return ERR_PTR(error)
  997. * otherwise.
  998. */
  999. struct block_device *lookup_bdev(const char *path)
  1000. {
  1001. struct block_device *bdev;
  1002. struct inode *inode;
  1003. struct nameidata nd;
  1004. int error;
  1005. if (!path || !*path)
  1006. return ERR_PTR(-EINVAL);
  1007. error = path_lookup(path, LOOKUP_FOLLOW, &nd);
  1008. if (error)
  1009. return ERR_PTR(error);
  1010. inode = nd.dentry->d_inode;
  1011. error = -ENOTBLK;
  1012. if (!S_ISBLK(inode->i_mode))
  1013. goto fail;
  1014. error = -EACCES;
  1015. if (nd.mnt->mnt_flags & MNT_NODEV)
  1016. goto fail;
  1017. error = -ENOMEM;
  1018. bdev = bd_acquire(inode);
  1019. if (!bdev)
  1020. goto fail;
  1021. out:
  1022. path_release(&nd);
  1023. return bdev;
  1024. fail:
  1025. bdev = ERR_PTR(error);
  1026. goto out;
  1027. }
  1028. /**
  1029. * open_bdev_excl - open a block device by name and set it up for use
  1030. *
  1031. * @path: special file representing the block device
  1032. * @flags: %MS_RDONLY for opening read-only
  1033. * @holder: owner for exclusion
  1034. *
  1035. * Open the blockdevice described by the special file at @path, claim it
  1036. * for the @holder.
  1037. */
  1038. struct block_device *open_bdev_excl(const char *path, int flags, void *holder)
  1039. {
  1040. struct block_device *bdev;
  1041. mode_t mode = FMODE_READ;
  1042. int error = 0;
  1043. bdev = lookup_bdev(path);
  1044. if (IS_ERR(bdev))
  1045. return bdev;
  1046. if (!(flags & MS_RDONLY))
  1047. mode |= FMODE_WRITE;
  1048. error = blkdev_get(bdev, mode, 0);
  1049. if (error)
  1050. return ERR_PTR(error);
  1051. error = -EACCES;
  1052. if (!(flags & MS_RDONLY) && bdev_read_only(bdev))
  1053. goto blkdev_put;
  1054. error = bd_claim(bdev, holder);
  1055. if (error)
  1056. goto blkdev_put;
  1057. return bdev;
  1058. blkdev_put:
  1059. blkdev_put(bdev);
  1060. return ERR_PTR(error);
  1061. }
  1062. EXPORT_SYMBOL(open_bdev_excl);
  1063. /**
  1064. * close_bdev_excl - release a blockdevice openen by open_bdev_excl()
  1065. *
  1066. * @bdev: blockdevice to close
  1067. *
  1068. * This is the counterpart to open_bdev_excl().
  1069. */
  1070. void close_bdev_excl(struct block_device *bdev)
  1071. {
  1072. bd_release(bdev);
  1073. blkdev_put(bdev);
  1074. }
  1075. EXPORT_SYMBOL(close_bdev_excl);