block_dev.c 30 KB

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