inode.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. // SPDX-License-Identifier: GPL-1.0+
  2. /*
  3. * Hypervisor filesystem for Linux on s390.
  4. *
  5. * Copyright IBM Corp. 2006, 2008
  6. * Author(s): Michael Holzheu <holzheu@de.ibm.com>
  7. */
  8. #define pr_fmt(fmt) "hypfs: " fmt
  9. #include <linux/types.h>
  10. #include <linux/errno.h>
  11. #include <linux/fs.h>
  12. #include <linux/fs_context.h>
  13. #include <linux/fs_parser.h>
  14. #include <linux/namei.h>
  15. #include <linux/vfs.h>
  16. #include <linux/slab.h>
  17. #include <linux/pagemap.h>
  18. #include <linux/time.h>
  19. #include <linux/sysfs.h>
  20. #include <linux/init.h>
  21. #include <linux/kobject.h>
  22. #include <linux/seq_file.h>
  23. #include <linux/uio.h>
  24. #include <asm/machine.h>
  25. #include <asm/ebcdic.h>
  26. #include "hypfs.h"
  27. #define HYPFS_MAGIC 0x687970 /* ASCII 'hyp' */
  28. #define TMP_SIZE 64 /* size of temporary buffers */
  29. static struct dentry *hypfs_create_update_file(struct dentry *dir);
  30. struct hypfs_sb_info {
  31. kuid_t uid; /* uid used for files and dirs */
  32. kgid_t gid; /* gid used for files and dirs */
  33. struct dentry *update_file; /* file to trigger update */
  34. time64_t last_update; /* last update, CLOCK_MONOTONIC time */
  35. struct mutex lock; /* lock to protect update process */
  36. };
  37. static const struct file_operations hypfs_file_ops;
  38. static struct file_system_type hypfs_type;
  39. static const struct super_operations hypfs_s_ops;
  40. /* start of list of all dentries, which have to be deleted on update */
  41. static struct dentry *hypfs_last_dentry;
  42. static void hypfs_update_update(struct super_block *sb)
  43. {
  44. struct hypfs_sb_info *sb_info = sb->s_fs_info;
  45. struct inode *inode = d_inode(sb_info->update_file);
  46. sb_info->last_update = ktime_get_seconds();
  47. simple_inode_init_ts(inode);
  48. }
  49. /* directory tree removal functions */
  50. static void hypfs_add_dentry(struct dentry *dentry)
  51. {
  52. if (IS_ROOT(dentry->d_parent)) {
  53. dentry->d_fsdata = hypfs_last_dentry;
  54. hypfs_last_dentry = dentry;
  55. }
  56. }
  57. static void hypfs_delete_tree(void)
  58. {
  59. while (hypfs_last_dentry) {
  60. struct dentry *next_dentry = hypfs_last_dentry->d_fsdata;
  61. simple_recursive_removal(hypfs_last_dentry, NULL);
  62. hypfs_last_dentry = next_dentry;
  63. }
  64. }
  65. static struct inode *hypfs_make_inode(struct super_block *sb, umode_t mode)
  66. {
  67. struct inode *ret = new_inode(sb);
  68. if (ret) {
  69. struct hypfs_sb_info *hypfs_info = sb->s_fs_info;
  70. ret->i_ino = get_next_ino();
  71. ret->i_mode = mode;
  72. ret->i_uid = hypfs_info->uid;
  73. ret->i_gid = hypfs_info->gid;
  74. simple_inode_init_ts(ret);
  75. if (S_ISDIR(mode))
  76. set_nlink(ret, 2);
  77. }
  78. return ret;
  79. }
  80. static void hypfs_evict_inode(struct inode *inode)
  81. {
  82. clear_inode(inode);
  83. kfree(inode->i_private);
  84. }
  85. static int hypfs_open(struct inode *inode, struct file *filp)
  86. {
  87. char *data = file_inode(filp)->i_private;
  88. struct hypfs_sb_info *fs_info;
  89. if (filp->f_mode & FMODE_WRITE) {
  90. if (!(inode->i_mode & S_IWUGO))
  91. return -EACCES;
  92. }
  93. if (filp->f_mode & FMODE_READ) {
  94. if (!(inode->i_mode & S_IRUGO))
  95. return -EACCES;
  96. }
  97. fs_info = inode->i_sb->s_fs_info;
  98. if(data) {
  99. mutex_lock(&fs_info->lock);
  100. filp->private_data = kstrdup(data, GFP_KERNEL);
  101. if (!filp->private_data) {
  102. mutex_unlock(&fs_info->lock);
  103. return -ENOMEM;
  104. }
  105. mutex_unlock(&fs_info->lock);
  106. }
  107. return nonseekable_open(inode, filp);
  108. }
  109. static ssize_t hypfs_read_iter(struct kiocb *iocb, struct iov_iter *to)
  110. {
  111. struct file *file = iocb->ki_filp;
  112. char *data = file->private_data;
  113. size_t available = strlen(data);
  114. loff_t pos = iocb->ki_pos;
  115. size_t count;
  116. if (pos < 0)
  117. return -EINVAL;
  118. if (pos >= available || !iov_iter_count(to))
  119. return 0;
  120. count = copy_to_iter(data + pos, available - pos, to);
  121. if (!count)
  122. return -EFAULT;
  123. iocb->ki_pos = pos + count;
  124. file_accessed(file);
  125. return count;
  126. }
  127. static ssize_t hypfs_write_iter(struct kiocb *iocb, struct iov_iter *from)
  128. {
  129. int rc;
  130. struct super_block *sb = file_inode(iocb->ki_filp)->i_sb;
  131. struct hypfs_sb_info *fs_info = sb->s_fs_info;
  132. size_t count = iov_iter_count(from);
  133. /*
  134. * Currently we only allow one update per second for two reasons:
  135. * 1. diag 204 is VERY expensive
  136. * 2. If several processes do updates in parallel and then read the
  137. * hypfs data, the likelihood of collisions is reduced, if we restrict
  138. * the minimum update interval. A collision occurs, if during the
  139. * data gathering of one process another process triggers an update
  140. * If the first process wants to ensure consistent data, it has
  141. * to restart data collection in this case.
  142. */
  143. mutex_lock(&fs_info->lock);
  144. if (fs_info->last_update == ktime_get_seconds()) {
  145. rc = -EBUSY;
  146. goto out;
  147. }
  148. hypfs_delete_tree();
  149. if (machine_is_vm())
  150. rc = hypfs_vm_create_files(sb->s_root);
  151. else
  152. rc = hypfs_diag_create_files(sb->s_root);
  153. if (rc) {
  154. pr_err("Updating the hypfs tree failed\n");
  155. hypfs_delete_tree();
  156. goto out;
  157. }
  158. hypfs_update_update(sb);
  159. rc = count;
  160. iov_iter_advance(from, count);
  161. out:
  162. mutex_unlock(&fs_info->lock);
  163. return rc;
  164. }
  165. static int hypfs_release(struct inode *inode, struct file *filp)
  166. {
  167. kfree(filp->private_data);
  168. return 0;
  169. }
  170. enum { Opt_uid, Opt_gid, };
  171. static const struct fs_parameter_spec hypfs_fs_parameters[] = {
  172. fsparam_u32("gid", Opt_gid),
  173. fsparam_u32("uid", Opt_uid),
  174. {}
  175. };
  176. static int hypfs_parse_param(struct fs_context *fc, struct fs_parameter *param)
  177. {
  178. struct hypfs_sb_info *hypfs_info = fc->s_fs_info;
  179. struct fs_parse_result result;
  180. kuid_t uid;
  181. kgid_t gid;
  182. int opt;
  183. opt = fs_parse(fc, hypfs_fs_parameters, param, &result);
  184. if (opt < 0)
  185. return opt;
  186. switch (opt) {
  187. case Opt_uid:
  188. uid = make_kuid(current_user_ns(), result.uint_32);
  189. if (!uid_valid(uid))
  190. return invalf(fc, "Unknown uid");
  191. hypfs_info->uid = uid;
  192. break;
  193. case Opt_gid:
  194. gid = make_kgid(current_user_ns(), result.uint_32);
  195. if (!gid_valid(gid))
  196. return invalf(fc, "Unknown gid");
  197. hypfs_info->gid = gid;
  198. break;
  199. }
  200. return 0;
  201. }
  202. static int hypfs_show_options(struct seq_file *s, struct dentry *root)
  203. {
  204. struct hypfs_sb_info *hypfs_info = root->d_sb->s_fs_info;
  205. seq_printf(s, ",uid=%u", from_kuid_munged(&init_user_ns, hypfs_info->uid));
  206. seq_printf(s, ",gid=%u", from_kgid_munged(&init_user_ns, hypfs_info->gid));
  207. return 0;
  208. }
  209. static int hypfs_fill_super(struct super_block *sb, struct fs_context *fc)
  210. {
  211. struct hypfs_sb_info *sbi = sb->s_fs_info;
  212. struct inode *root_inode;
  213. struct dentry *root_dentry, *update_file;
  214. int rc;
  215. sb->s_blocksize = PAGE_SIZE;
  216. sb->s_blocksize_bits = PAGE_SHIFT;
  217. sb->s_magic = HYPFS_MAGIC;
  218. sb->s_op = &hypfs_s_ops;
  219. root_inode = hypfs_make_inode(sb, S_IFDIR | 0755);
  220. if (!root_inode)
  221. return -ENOMEM;
  222. root_inode->i_op = &simple_dir_inode_operations;
  223. root_inode->i_fop = &simple_dir_operations;
  224. sb->s_root = root_dentry = d_make_root(root_inode);
  225. if (!root_dentry)
  226. return -ENOMEM;
  227. if (machine_is_vm())
  228. rc = hypfs_vm_create_files(root_dentry);
  229. else
  230. rc = hypfs_diag_create_files(root_dentry);
  231. if (rc)
  232. return rc;
  233. update_file = hypfs_create_update_file(root_dentry);
  234. if (IS_ERR(update_file))
  235. return PTR_ERR(update_file);
  236. sbi->update_file = update_file;
  237. hypfs_update_update(sb);
  238. pr_info("Hypervisor filesystem mounted\n");
  239. return 0;
  240. }
  241. static int hypfs_get_tree(struct fs_context *fc)
  242. {
  243. return get_tree_single(fc, hypfs_fill_super);
  244. }
  245. static void hypfs_free_fc(struct fs_context *fc)
  246. {
  247. kfree(fc->s_fs_info);
  248. }
  249. static const struct fs_context_operations hypfs_context_ops = {
  250. .free = hypfs_free_fc,
  251. .parse_param = hypfs_parse_param,
  252. .get_tree = hypfs_get_tree,
  253. };
  254. static int hypfs_init_fs_context(struct fs_context *fc)
  255. {
  256. struct hypfs_sb_info *sbi;
  257. sbi = kzalloc_obj(struct hypfs_sb_info);
  258. if (!sbi)
  259. return -ENOMEM;
  260. mutex_init(&sbi->lock);
  261. sbi->uid = current_uid();
  262. sbi->gid = current_gid();
  263. fc->s_fs_info = sbi;
  264. fc->ops = &hypfs_context_ops;
  265. return 0;
  266. }
  267. static void hypfs_kill_super(struct super_block *sb)
  268. {
  269. struct hypfs_sb_info *sb_info = sb->s_fs_info;
  270. hypfs_last_dentry = NULL;
  271. kill_anon_super(sb);
  272. kfree(sb_info);
  273. }
  274. static struct dentry *hypfs_create_file(struct dentry *parent, const char *name,
  275. char *data, umode_t mode)
  276. {
  277. struct dentry *dentry;
  278. struct inode *inode;
  279. dentry = simple_start_creating(parent, name);
  280. if (IS_ERR(dentry))
  281. return ERR_PTR(-ENOMEM);
  282. inode = hypfs_make_inode(parent->d_sb, mode);
  283. if (!inode) {
  284. simple_done_creating(dentry);
  285. return ERR_PTR(-ENOMEM);
  286. }
  287. if (S_ISREG(mode)) {
  288. inode->i_fop = &hypfs_file_ops;
  289. if (data)
  290. inode->i_size = strlen(data);
  291. else
  292. inode->i_size = 0;
  293. } else if (S_ISDIR(mode)) {
  294. inode->i_op = &simple_dir_inode_operations;
  295. inode->i_fop = &simple_dir_operations;
  296. inc_nlink(d_inode(parent));
  297. } else
  298. BUG();
  299. inode->i_private = data;
  300. d_make_persistent(dentry, inode);
  301. simple_done_creating(dentry);
  302. return dentry; // borrowed
  303. }
  304. struct dentry *hypfs_mkdir(struct dentry *parent, const char *name)
  305. {
  306. struct dentry *dentry;
  307. dentry = hypfs_create_file(parent, name, NULL, S_IFDIR | DIR_MODE);
  308. if (IS_ERR(dentry))
  309. return dentry;
  310. hypfs_add_dentry(dentry);
  311. return dentry;
  312. }
  313. static struct dentry *hypfs_create_update_file(struct dentry *dir)
  314. {
  315. struct dentry *dentry;
  316. dentry = hypfs_create_file(dir, "update", NULL,
  317. S_IFREG | UPDATE_FILE_MODE);
  318. /*
  319. * We do not put the update file on the 'delete' list with
  320. * hypfs_add_dentry(), since it should not be removed when the tree
  321. * is updated.
  322. */
  323. return dentry;
  324. }
  325. int hypfs_create_u64(struct dentry *dir, const char *name, __u64 value)
  326. {
  327. char *buffer;
  328. char tmp[TMP_SIZE];
  329. struct dentry *dentry;
  330. snprintf(tmp, TMP_SIZE, "%llu\n", (unsigned long long int)value);
  331. buffer = kstrdup(tmp, GFP_KERNEL);
  332. if (!buffer)
  333. return -ENOMEM;
  334. dentry =
  335. hypfs_create_file(dir, name, buffer, S_IFREG | REG_FILE_MODE);
  336. if (IS_ERR(dentry)) {
  337. kfree(buffer);
  338. return -ENOMEM;
  339. }
  340. hypfs_add_dentry(dentry);
  341. return 0;
  342. }
  343. int hypfs_create_str(struct dentry *dir, const char *name, char *string)
  344. {
  345. char *buffer;
  346. struct dentry *dentry;
  347. buffer = kmalloc(strlen(string) + 2, GFP_KERNEL);
  348. if (!buffer)
  349. return -ENOMEM;
  350. sprintf(buffer, "%s\n", string);
  351. dentry =
  352. hypfs_create_file(dir, name, buffer, S_IFREG | REG_FILE_MODE);
  353. if (IS_ERR(dentry)) {
  354. kfree(buffer);
  355. return -ENOMEM;
  356. }
  357. hypfs_add_dentry(dentry);
  358. return 0;
  359. }
  360. static const struct file_operations hypfs_file_ops = {
  361. .open = hypfs_open,
  362. .release = hypfs_release,
  363. .read_iter = hypfs_read_iter,
  364. .write_iter = hypfs_write_iter,
  365. };
  366. static struct file_system_type hypfs_type = {
  367. .owner = THIS_MODULE,
  368. .name = "s390_hypfs",
  369. .init_fs_context = hypfs_init_fs_context,
  370. .parameters = hypfs_fs_parameters,
  371. .kill_sb = hypfs_kill_super
  372. };
  373. static const struct super_operations hypfs_s_ops = {
  374. .statfs = simple_statfs,
  375. .evict_inode = hypfs_evict_inode,
  376. .show_options = hypfs_show_options,
  377. };
  378. int __init __hypfs_fs_init(void)
  379. {
  380. int rc;
  381. rc = sysfs_create_mount_point(hypervisor_kobj, "s390");
  382. if (rc)
  383. return rc;
  384. rc = register_filesystem(&hypfs_type);
  385. if (rc)
  386. goto fail;
  387. return 0;
  388. fail:
  389. sysfs_remove_mount_point(hypervisor_kobj, "s390");
  390. return rc;
  391. }