inode.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /*
  2. * arch/s390/hypfs/inode.c
  3. * Hypervisor filesystem for Linux on s390.
  4. *
  5. * Copyright (C) IBM Corp. 2006
  6. * Author(s): Michael Holzheu <holzheu@de.ibm.com>
  7. */
  8. #include <linux/types.h>
  9. #include <linux/errno.h>
  10. #include <linux/fs.h>
  11. #include <linux/namei.h>
  12. #include <linux/vfs.h>
  13. #include <linux/pagemap.h>
  14. #include <linux/gfp.h>
  15. #include <linux/time.h>
  16. #include <linux/parser.h>
  17. #include <linux/sysfs.h>
  18. #include <linux/module.h>
  19. #include <asm/ebcdic.h>
  20. #include "hypfs.h"
  21. #include "hypfs_diag.h"
  22. #define HYPFS_MAGIC 0x687970 /* ASCII 'hyp' */
  23. #define TMP_SIZE 64 /* size of temporary buffers */
  24. static struct dentry *hypfs_create_update_file(struct super_block *sb,
  25. struct dentry *dir);
  26. struct hypfs_sb_info {
  27. uid_t uid; /* uid used for files and dirs */
  28. gid_t gid; /* gid used for files and dirs */
  29. struct dentry *update_file; /* file to trigger update */
  30. time_t last_update; /* last update time in secs since 1970 */
  31. struct mutex lock; /* lock to protect update process */
  32. };
  33. static struct file_operations hypfs_file_ops;
  34. static struct file_system_type hypfs_type;
  35. static struct super_operations hypfs_s_ops;
  36. /* start of list of all dentries, which have to be deleted on update */
  37. static struct dentry *hypfs_last_dentry;
  38. static void hypfs_update_update(struct super_block *sb)
  39. {
  40. struct hypfs_sb_info *sb_info = sb->s_fs_info;
  41. struct inode *inode = sb_info->update_file->d_inode;
  42. sb_info->last_update = get_seconds();
  43. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  44. }
  45. /* directory tree removal functions */
  46. static void hypfs_add_dentry(struct dentry *dentry)
  47. {
  48. dentry->d_fsdata = hypfs_last_dentry;
  49. hypfs_last_dentry = dentry;
  50. }
  51. static void hypfs_remove(struct dentry *dentry)
  52. {
  53. struct dentry *parent;
  54. parent = dentry->d_parent;
  55. if (S_ISDIR(dentry->d_inode->i_mode))
  56. simple_rmdir(parent->d_inode, dentry);
  57. else
  58. simple_unlink(parent->d_inode, dentry);
  59. d_delete(dentry);
  60. dput(dentry);
  61. }
  62. static void hypfs_delete_tree(struct dentry *root)
  63. {
  64. while (hypfs_last_dentry) {
  65. struct dentry *next_dentry;
  66. next_dentry = hypfs_last_dentry->d_fsdata;
  67. hypfs_remove(hypfs_last_dentry);
  68. hypfs_last_dentry = next_dentry;
  69. }
  70. }
  71. static struct inode *hypfs_make_inode(struct super_block *sb, int mode)
  72. {
  73. struct inode *ret = new_inode(sb);
  74. if (ret) {
  75. struct hypfs_sb_info *hypfs_info = sb->s_fs_info;
  76. ret->i_mode = mode;
  77. ret->i_uid = hypfs_info->uid;
  78. ret->i_gid = hypfs_info->gid;
  79. ret->i_blocks = 0;
  80. ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME;
  81. if (mode & S_IFDIR)
  82. ret->i_nlink = 2;
  83. else
  84. ret->i_nlink = 1;
  85. }
  86. return ret;
  87. }
  88. static void hypfs_drop_inode(struct inode *inode)
  89. {
  90. kfree(inode->i_private);
  91. generic_delete_inode(inode);
  92. }
  93. static int hypfs_open(struct inode *inode, struct file *filp)
  94. {
  95. char *data = filp->f_dentry->d_inode->i_private;
  96. struct hypfs_sb_info *fs_info;
  97. if (filp->f_mode & FMODE_WRITE) {
  98. if (!(inode->i_mode & S_IWUGO))
  99. return -EACCES;
  100. }
  101. if (filp->f_mode & FMODE_READ) {
  102. if (!(inode->i_mode & S_IRUGO))
  103. return -EACCES;
  104. }
  105. fs_info = inode->i_sb->s_fs_info;
  106. if(data) {
  107. mutex_lock(&fs_info->lock);
  108. filp->private_data = kstrdup(data, GFP_KERNEL);
  109. if (!filp->private_data) {
  110. mutex_unlock(&fs_info->lock);
  111. return -ENOMEM;
  112. }
  113. mutex_unlock(&fs_info->lock);
  114. }
  115. return 0;
  116. }
  117. static ssize_t hypfs_aio_read(struct kiocb *iocb, const struct iovec *iov,
  118. unsigned long nr_segs, loff_t offset)
  119. {
  120. char *data;
  121. size_t len;
  122. struct file *filp = iocb->ki_filp;
  123. /* XXX: temporary */
  124. char __user *buf = iov[0].iov_base;
  125. size_t count = iov[0].iov_len;
  126. if (nr_segs != 1) {
  127. count = -EINVAL;
  128. goto out;
  129. }
  130. data = filp->private_data;
  131. len = strlen(data);
  132. if (offset > len) {
  133. count = 0;
  134. goto out;
  135. }
  136. if (count > len - offset)
  137. count = len - offset;
  138. if (copy_to_user(buf, data + offset, count)) {
  139. count = -EFAULT;
  140. goto out;
  141. }
  142. iocb->ki_pos += count;
  143. file_accessed(filp);
  144. out:
  145. return count;
  146. }
  147. static ssize_t hypfs_aio_write(struct kiocb *iocb, const struct iovec *iov,
  148. unsigned long nr_segs, loff_t offset)
  149. {
  150. int rc;
  151. struct super_block *sb;
  152. struct hypfs_sb_info *fs_info;
  153. size_t count = iov_length(iov, nr_segs);
  154. sb = iocb->ki_filp->f_dentry->d_inode->i_sb;
  155. fs_info = sb->s_fs_info;
  156. /*
  157. * Currently we only allow one update per second for two reasons:
  158. * 1. diag 204 is VERY expensive
  159. * 2. If several processes do updates in parallel and then read the
  160. * hypfs data, the likelihood of collisions is reduced, if we restrict
  161. * the minimum update interval. A collision occurs, if during the
  162. * data gathering of one process another process triggers an update
  163. * If the first process wants to ensure consistent data, it has
  164. * to restart data collection in this case.
  165. */
  166. mutex_lock(&fs_info->lock);
  167. if (fs_info->last_update == get_seconds()) {
  168. rc = -EBUSY;
  169. goto out;
  170. }
  171. hypfs_delete_tree(sb->s_root);
  172. rc = hypfs_diag_create_files(sb, sb->s_root);
  173. if (rc) {
  174. printk(KERN_ERR "hypfs: Update failed\n");
  175. hypfs_delete_tree(sb->s_root);
  176. goto out;
  177. }
  178. hypfs_update_update(sb);
  179. rc = count;
  180. out:
  181. mutex_unlock(&fs_info->lock);
  182. return rc;
  183. }
  184. static int hypfs_release(struct inode *inode, struct file *filp)
  185. {
  186. kfree(filp->private_data);
  187. return 0;
  188. }
  189. enum { opt_uid, opt_gid, opt_err };
  190. static match_table_t hypfs_tokens = {
  191. {opt_uid, "uid=%u"},
  192. {opt_gid, "gid=%u"},
  193. {opt_err, NULL}
  194. };
  195. static int hypfs_parse_options(char *options, struct super_block *sb)
  196. {
  197. char *str;
  198. substring_t args[MAX_OPT_ARGS];
  199. if (!options)
  200. return 0;
  201. while ((str = strsep(&options, ",")) != NULL) {
  202. int token, option;
  203. struct hypfs_sb_info *hypfs_info = sb->s_fs_info;
  204. if (!*str)
  205. continue;
  206. token = match_token(str, hypfs_tokens, args);
  207. switch (token) {
  208. case opt_uid:
  209. if (match_int(&args[0], &option))
  210. return -EINVAL;
  211. hypfs_info->uid = option;
  212. break;
  213. case opt_gid:
  214. if (match_int(&args[0], &option))
  215. return -EINVAL;
  216. hypfs_info->gid = option;
  217. break;
  218. case opt_err:
  219. default:
  220. printk(KERN_ERR "hypfs: Unrecognized mount option "
  221. "\"%s\" or missing value\n", str);
  222. return -EINVAL;
  223. }
  224. }
  225. return 0;
  226. }
  227. static int hypfs_fill_super(struct super_block *sb, void *data, int silent)
  228. {
  229. struct inode *root_inode;
  230. struct dentry *root_dentry;
  231. int rc = 0;
  232. struct hypfs_sb_info *sbi;
  233. sbi = kzalloc(sizeof(struct hypfs_sb_info), GFP_KERNEL);
  234. if (!sbi)
  235. return -ENOMEM;
  236. mutex_init(&sbi->lock);
  237. sbi->uid = current->uid;
  238. sbi->gid = current->gid;
  239. sb->s_fs_info = sbi;
  240. sb->s_blocksize = PAGE_CACHE_SIZE;
  241. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  242. sb->s_magic = HYPFS_MAGIC;
  243. sb->s_op = &hypfs_s_ops;
  244. if (hypfs_parse_options(data, sb)) {
  245. rc = -EINVAL;
  246. goto err_alloc;
  247. }
  248. root_inode = hypfs_make_inode(sb, S_IFDIR | 0755);
  249. if (!root_inode) {
  250. rc = -ENOMEM;
  251. goto err_alloc;
  252. }
  253. root_inode->i_op = &simple_dir_inode_operations;
  254. root_inode->i_fop = &simple_dir_operations;
  255. root_dentry = d_alloc_root(root_inode);
  256. if (!root_dentry) {
  257. iput(root_inode);
  258. rc = -ENOMEM;
  259. goto err_alloc;
  260. }
  261. rc = hypfs_diag_create_files(sb, root_dentry);
  262. if (rc)
  263. goto err_tree;
  264. sbi->update_file = hypfs_create_update_file(sb, root_dentry);
  265. if (IS_ERR(sbi->update_file)) {
  266. rc = PTR_ERR(sbi->update_file);
  267. goto err_tree;
  268. }
  269. hypfs_update_update(sb);
  270. sb->s_root = root_dentry;
  271. return 0;
  272. err_tree:
  273. hypfs_delete_tree(root_dentry);
  274. d_genocide(root_dentry);
  275. dput(root_dentry);
  276. err_alloc:
  277. kfree(sbi);
  278. return rc;
  279. }
  280. static int hypfs_get_super(struct file_system_type *fst, int flags,
  281. const char *devname, void *data, struct vfsmount *mnt)
  282. {
  283. return get_sb_single(fst, flags, data, hypfs_fill_super, mnt);
  284. }
  285. static void hypfs_kill_super(struct super_block *sb)
  286. {
  287. struct hypfs_sb_info *sb_info = sb->s_fs_info;
  288. if (sb->s_root) {
  289. hypfs_delete_tree(sb->s_root);
  290. hypfs_remove(sb_info->update_file);
  291. kfree(sb->s_fs_info);
  292. sb->s_fs_info = NULL;
  293. }
  294. kill_litter_super(sb);
  295. }
  296. static struct dentry *hypfs_create_file(struct super_block *sb,
  297. struct dentry *parent, const char *name,
  298. char *data, mode_t mode)
  299. {
  300. struct dentry *dentry;
  301. struct inode *inode;
  302. struct qstr qname;
  303. qname.name = name;
  304. qname.len = strlen(name);
  305. qname.hash = full_name_hash(name, qname.len);
  306. dentry = lookup_one_len(name, parent, strlen(name));
  307. if (IS_ERR(dentry))
  308. return ERR_PTR(-ENOMEM);
  309. inode = hypfs_make_inode(sb, mode);
  310. if (!inode) {
  311. dput(dentry);
  312. return ERR_PTR(-ENOMEM);
  313. }
  314. if (mode & S_IFREG) {
  315. inode->i_fop = &hypfs_file_ops;
  316. if (data)
  317. inode->i_size = strlen(data);
  318. else
  319. inode->i_size = 0;
  320. } else if (mode & S_IFDIR) {
  321. inode->i_op = &simple_dir_inode_operations;
  322. inode->i_fop = &simple_dir_operations;
  323. parent->d_inode->i_nlink++;
  324. } else
  325. BUG();
  326. inode->i_private = data;
  327. d_instantiate(dentry, inode);
  328. dget(dentry);
  329. return dentry;
  330. }
  331. struct dentry *hypfs_mkdir(struct super_block *sb, struct dentry *parent,
  332. const char *name)
  333. {
  334. struct dentry *dentry;
  335. dentry = hypfs_create_file(sb, parent, name, NULL, S_IFDIR | DIR_MODE);
  336. if (IS_ERR(dentry))
  337. return dentry;
  338. hypfs_add_dentry(dentry);
  339. parent->d_inode->i_nlink++;
  340. return dentry;
  341. }
  342. static struct dentry *hypfs_create_update_file(struct super_block *sb,
  343. struct dentry *dir)
  344. {
  345. struct dentry *dentry;
  346. dentry = hypfs_create_file(sb, dir, "update", NULL,
  347. S_IFREG | UPDATE_FILE_MODE);
  348. /*
  349. * We do not put the update file on the 'delete' list with
  350. * hypfs_add_dentry(), since it should not be removed when the tree
  351. * is updated.
  352. */
  353. return dentry;
  354. }
  355. struct dentry *hypfs_create_u64(struct super_block *sb, struct dentry *dir,
  356. const char *name, __u64 value)
  357. {
  358. char *buffer;
  359. char tmp[TMP_SIZE];
  360. struct dentry *dentry;
  361. snprintf(tmp, TMP_SIZE, "%lld\n", (unsigned long long int)value);
  362. buffer = kstrdup(tmp, GFP_KERNEL);
  363. if (!buffer)
  364. return ERR_PTR(-ENOMEM);
  365. dentry =
  366. hypfs_create_file(sb, dir, name, buffer, S_IFREG | REG_FILE_MODE);
  367. if (IS_ERR(dentry)) {
  368. kfree(buffer);
  369. return ERR_PTR(-ENOMEM);
  370. }
  371. hypfs_add_dentry(dentry);
  372. return dentry;
  373. }
  374. struct dentry *hypfs_create_str(struct super_block *sb, struct dentry *dir,
  375. const char *name, char *string)
  376. {
  377. char *buffer;
  378. struct dentry *dentry;
  379. buffer = kmalloc(strlen(string) + 2, GFP_KERNEL);
  380. if (!buffer)
  381. return ERR_PTR(-ENOMEM);
  382. sprintf(buffer, "%s\n", string);
  383. dentry =
  384. hypfs_create_file(sb, dir, name, buffer, S_IFREG | REG_FILE_MODE);
  385. if (IS_ERR(dentry)) {
  386. kfree(buffer);
  387. return ERR_PTR(-ENOMEM);
  388. }
  389. hypfs_add_dentry(dentry);
  390. return dentry;
  391. }
  392. static struct file_operations hypfs_file_ops = {
  393. .open = hypfs_open,
  394. .release = hypfs_release,
  395. .read = do_sync_read,
  396. .write = do_sync_write,
  397. .aio_read = hypfs_aio_read,
  398. .aio_write = hypfs_aio_write,
  399. };
  400. static struct file_system_type hypfs_type = {
  401. .owner = THIS_MODULE,
  402. .name = "s390_hypfs",
  403. .get_sb = hypfs_get_super,
  404. .kill_sb = hypfs_kill_super
  405. };
  406. static struct super_operations hypfs_s_ops = {
  407. .statfs = simple_statfs,
  408. .drop_inode = hypfs_drop_inode,
  409. };
  410. static decl_subsys(s390, NULL, NULL);
  411. static int __init hypfs_init(void)
  412. {
  413. int rc;
  414. if (MACHINE_IS_VM)
  415. return -ENODATA;
  416. if (hypfs_diag_init()) {
  417. rc = -ENODATA;
  418. goto fail_diag;
  419. }
  420. kset_set_kset_s(&s390_subsys, hypervisor_subsys);
  421. rc = subsystem_register(&s390_subsys);
  422. if (rc)
  423. goto fail_sysfs;
  424. rc = register_filesystem(&hypfs_type);
  425. if (rc)
  426. goto fail_filesystem;
  427. return 0;
  428. fail_filesystem:
  429. subsystem_unregister(&s390_subsys);
  430. fail_sysfs:
  431. hypfs_diag_exit();
  432. fail_diag:
  433. printk(KERN_ERR "hypfs: Initialization failed with rc = %i.\n", rc);
  434. return rc;
  435. }
  436. static void __exit hypfs_exit(void)
  437. {
  438. hypfs_diag_exit();
  439. unregister_filesystem(&hypfs_type);
  440. subsystem_unregister(&s390_subsys);
  441. }
  442. module_init(hypfs_init)
  443. module_exit(hypfs_exit)
  444. MODULE_LICENSE("GPL");
  445. MODULE_AUTHOR("Michael Holzheu <holzheu@de.ibm.com>");
  446. MODULE_DESCRIPTION("s390 Hypervisor Filesystem");