inode.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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 <[email protected]>
  7. */
  8. #define KMSG_COMPONENT "hypfs"
  9. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  10. #include <linux/types.h>
  11. #include <linux/errno.h>
  12. #include <linux/fs.h>
  13. #include <linux/fs_context.h>
  14. #include <linux/fs_parser.h>
  15. #include <linux/namei.h>
  16. #include <linux/vfs.h>
  17. #include <linux/slab.h>
  18. #include <linux/pagemap.h>
  19. #include <linux/time.h>
  20. #include <linux/sysfs.h>
  21. #include <linux/init.h>
  22. #include <linux/kobject.h>
  23. #include <linux/seq_file.h>
  24. #include <linux/uio.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. inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
  48. }
  49. /* directory tree removal functions */
  50. static void hypfs_add_dentry(struct dentry *dentry)
  51. {
  52. dentry->d_fsdata = hypfs_last_dentry;
  53. hypfs_last_dentry = dentry;
  54. }
  55. static void hypfs_remove(struct dentry *dentry)
  56. {
  57. struct dentry *parent;
  58. parent = dentry->d_parent;
  59. inode_lock(d_inode(parent));
  60. if (simple_positive(dentry)) {
  61. if (d_is_dir(dentry))
  62. simple_rmdir(d_inode(parent), dentry);
  63. else
  64. simple_unlink(d_inode(parent), dentry);
  65. }
  66. d_drop(dentry);
  67. dput(dentry);
  68. inode_unlock(d_inode(parent));
  69. }
  70. static void hypfs_delete_tree(struct dentry *root)
  71. {
  72. while (hypfs_last_dentry) {
  73. struct dentry *next_dentry;
  74. next_dentry = hypfs_last_dentry->d_fsdata;
  75. hypfs_remove(hypfs_last_dentry);
  76. hypfs_last_dentry = next_dentry;
  77. }
  78. }
  79. static struct inode *hypfs_make_inode(struct super_block *sb, umode_t mode)
  80. {
  81. struct inode *ret = new_inode(sb);
  82. if (ret) {
  83. struct hypfs_sb_info *hypfs_info = sb->s_fs_info;
  84. ret->i_ino = get_next_ino();
  85. ret->i_mode = mode;
  86. ret->i_uid = hypfs_info->uid;
  87. ret->i_gid = hypfs_info->gid;
  88. ret->i_atime = ret->i_mtime = ret->i_ctime = current_time(ret);
  89. if (S_ISDIR(mode))
  90. set_nlink(ret, 2);
  91. }
  92. return ret;
  93. }
  94. static void hypfs_evict_inode(struct inode *inode)
  95. {
  96. clear_inode(inode);
  97. kfree(inode->i_private);
  98. }
  99. static int hypfs_open(struct inode *inode, struct file *filp)
  100. {
  101. char *data = file_inode(filp)->i_private;
  102. struct hypfs_sb_info *fs_info;
  103. if (filp->f_mode & FMODE_WRITE) {
  104. if (!(inode->i_mode & S_IWUGO))
  105. return -EACCES;
  106. }
  107. if (filp->f_mode & FMODE_READ) {
  108. if (!(inode->i_mode & S_IRUGO))
  109. return -EACCES;
  110. }
  111. fs_info = inode->i_sb->s_fs_info;
  112. if(data) {
  113. mutex_lock(&fs_info->lock);
  114. filp->private_data = kstrdup(data, GFP_KERNEL);
  115. if (!filp->private_data) {
  116. mutex_unlock(&fs_info->lock);
  117. return -ENOMEM;
  118. }
  119. mutex_unlock(&fs_info->lock);
  120. }
  121. return nonseekable_open(inode, filp);
  122. }
  123. static ssize_t hypfs_read_iter(struct kiocb *iocb, struct iov_iter *to)
  124. {
  125. struct file *file = iocb->ki_filp;
  126. char *data = file->private_data;
  127. size_t available = strlen(data);
  128. loff_t pos = iocb->ki_pos;
  129. size_t count;
  130. if (pos < 0)
  131. return -EINVAL;
  132. if (pos >= available || !iov_iter_count(to))
  133. return 0;
  134. count = copy_to_iter(data + pos, available - pos, to);
  135. if (!count)
  136. return -EFAULT;
  137. iocb->ki_pos = pos + count;
  138. file_accessed(file);
  139. return count;
  140. }
  141. static ssize_t hypfs_write_iter(struct kiocb *iocb, struct iov_iter *from)
  142. {
  143. int rc;
  144. struct super_block *sb = file_inode(iocb->ki_filp)->i_sb;
  145. struct hypfs_sb_info *fs_info = sb->s_fs_info;
  146. size_t count = iov_iter_count(from);
  147. /*
  148. * Currently we only allow one update per second for two reasons:
  149. * 1. diag 204 is VERY expensive
  150. * 2. If several processes do updates in parallel and then read the
  151. * hypfs data, the likelihood of collisions is reduced, if we restrict
  152. * the minimum update interval. A collision occurs, if during the
  153. * data gathering of one process another process triggers an update
  154. * If the first process wants to ensure consistent data, it has
  155. * to restart data collection in this case.
  156. */
  157. mutex_lock(&fs_info->lock);
  158. if (fs_info->last_update == ktime_get_seconds()) {
  159. rc = -EBUSY;
  160. goto out;
  161. }
  162. hypfs_delete_tree(sb->s_root);
  163. if (MACHINE_IS_VM)
  164. rc = hypfs_vm_create_files(sb->s_root);
  165. else
  166. rc = hypfs_diag_create_files(sb->s_root);
  167. if (rc) {
  168. pr_err("Updating the hypfs tree failed\n");
  169. hypfs_delete_tree(sb->s_root);
  170. goto out;
  171. }
  172. hypfs_update_update(sb);
  173. rc = count;
  174. iov_iter_advance(from, count);
  175. out:
  176. mutex_unlock(&fs_info->lock);
  177. return rc;
  178. }
  179. static int hypfs_release(struct inode *inode, struct file *filp)
  180. {
  181. kfree(filp->private_data);
  182. return 0;
  183. }
  184. enum { Opt_uid, Opt_gid, };
  185. static const struct fs_parameter_spec hypfs_fs_parameters[] = {
  186. fsparam_u32("gid", Opt_gid),
  187. fsparam_u32("uid", Opt_uid),
  188. {}
  189. };
  190. static int hypfs_parse_param(struct fs_context *fc, struct fs_parameter *param)
  191. {
  192. struct hypfs_sb_info *hypfs_info = fc->s_fs_info;
  193. struct fs_parse_result result;
  194. kuid_t uid;
  195. kgid_t gid;
  196. int opt;
  197. opt = fs_parse(fc, hypfs_fs_parameters, param, &result);
  198. if (opt < 0)
  199. return opt;
  200. switch (opt) {
  201. case Opt_uid:
  202. uid = make_kuid(current_user_ns(), result.uint_32);
  203. if (!uid_valid(uid))
  204. return invalf(fc, "Unknown uid");
  205. hypfs_info->uid = uid;
  206. break;
  207. case Opt_gid:
  208. gid = make_kgid(current_user_ns(), result.uint_32);
  209. if (!gid_valid(gid))
  210. return invalf(fc, "Unknown gid");
  211. hypfs_info->gid = gid;
  212. break;
  213. }
  214. return 0;
  215. }
  216. static int hypfs_show_options(struct seq_file *s, struct dentry *root)
  217. {
  218. struct hypfs_sb_info *hypfs_info = root->d_sb->s_fs_info;
  219. seq_printf(s, ",uid=%u", from_kuid_munged(&init_user_ns, hypfs_info->uid));
  220. seq_printf(s, ",gid=%u", from_kgid_munged(&init_user_ns, hypfs_info->gid));
  221. return 0;
  222. }
  223. static int hypfs_fill_super(struct super_block *sb, struct fs_context *fc)
  224. {
  225. struct hypfs_sb_info *sbi = sb->s_fs_info;
  226. struct inode *root_inode;
  227. struct dentry *root_dentry, *update_file;
  228. int rc;
  229. sb->s_blocksize = PAGE_SIZE;
  230. sb->s_blocksize_bits = PAGE_SHIFT;
  231. sb->s_magic = HYPFS_MAGIC;
  232. sb->s_op = &hypfs_s_ops;
  233. root_inode = hypfs_make_inode(sb, S_IFDIR | 0755);
  234. if (!root_inode)
  235. return -ENOMEM;
  236. root_inode->i_op = &simple_dir_inode_operations;
  237. root_inode->i_fop = &simple_dir_operations;
  238. sb->s_root = root_dentry = d_make_root(root_inode);
  239. if (!root_dentry)
  240. return -ENOMEM;
  241. if (MACHINE_IS_VM)
  242. rc = hypfs_vm_create_files(root_dentry);
  243. else
  244. rc = hypfs_diag_create_files(root_dentry);
  245. if (rc)
  246. return rc;
  247. update_file = hypfs_create_update_file(root_dentry);
  248. if (IS_ERR(update_file))
  249. return PTR_ERR(update_file);
  250. sbi->update_file = update_file;
  251. hypfs_update_update(sb);
  252. pr_info("Hypervisor filesystem mounted\n");
  253. return 0;
  254. }
  255. static int hypfs_get_tree(struct fs_context *fc)
  256. {
  257. return get_tree_single(fc, hypfs_fill_super);
  258. }
  259. static void hypfs_free_fc(struct fs_context *fc)
  260. {
  261. kfree(fc->s_fs_info);
  262. }
  263. static const struct fs_context_operations hypfs_context_ops = {
  264. .free = hypfs_free_fc,
  265. .parse_param = hypfs_parse_param,
  266. .get_tree = hypfs_get_tree,
  267. };
  268. static int hypfs_init_fs_context(struct fs_context *fc)
  269. {
  270. struct hypfs_sb_info *sbi;
  271. sbi = kzalloc(sizeof(struct hypfs_sb_info), GFP_KERNEL);
  272. if (!sbi)
  273. return -ENOMEM;
  274. mutex_init(&sbi->lock);
  275. sbi->uid = current_uid();
  276. sbi->gid = current_gid();
  277. fc->s_fs_info = sbi;
  278. fc->ops = &hypfs_context_ops;
  279. return 0;
  280. }
  281. static void hypfs_kill_super(struct super_block *sb)
  282. {
  283. struct hypfs_sb_info *sb_info = sb->s_fs_info;
  284. if (sb->s_root)
  285. hypfs_delete_tree(sb->s_root);
  286. if (sb_info && sb_info->update_file)
  287. hypfs_remove(sb_info->update_file);
  288. kfree(sb->s_fs_info);
  289. sb->s_fs_info = NULL;
  290. kill_litter_super(sb);
  291. }
  292. static struct dentry *hypfs_create_file(struct dentry *parent, const char *name,
  293. char *data, umode_t mode)
  294. {
  295. struct dentry *dentry;
  296. struct inode *inode;
  297. inode_lock(d_inode(parent));
  298. dentry = lookup_one_len(name, parent, strlen(name));
  299. if (IS_ERR(dentry)) {
  300. dentry = ERR_PTR(-ENOMEM);
  301. goto fail;
  302. }
  303. inode = hypfs_make_inode(parent->d_sb, mode);
  304. if (!inode) {
  305. dput(dentry);
  306. dentry = ERR_PTR(-ENOMEM);
  307. goto fail;
  308. }
  309. if (S_ISREG(mode)) {
  310. inode->i_fop = &hypfs_file_ops;
  311. if (data)
  312. inode->i_size = strlen(data);
  313. else
  314. inode->i_size = 0;
  315. } else if (S_ISDIR(mode)) {
  316. inode->i_op = &simple_dir_inode_operations;
  317. inode->i_fop = &simple_dir_operations;
  318. inc_nlink(d_inode(parent));
  319. } else
  320. BUG();
  321. inode->i_private = data;
  322. d_instantiate(dentry, inode);
  323. dget(dentry);
  324. fail:
  325. inode_unlock(d_inode(parent));
  326. return dentry;
  327. }
  328. struct dentry *hypfs_mkdir(struct dentry *parent, const char *name)
  329. {
  330. struct dentry *dentry;
  331. dentry = hypfs_create_file(parent, name, NULL, S_IFDIR | DIR_MODE);
  332. if (IS_ERR(dentry))
  333. return dentry;
  334. hypfs_add_dentry(dentry);
  335. return dentry;
  336. }
  337. static struct dentry *hypfs_create_update_file(struct dentry *dir)
  338. {
  339. struct dentry *dentry;
  340. dentry = hypfs_create_file(dir, "update", NULL,
  341. S_IFREG | UPDATE_FILE_MODE);
  342. /*
  343. * We do not put the update file on the 'delete' list with
  344. * hypfs_add_dentry(), since it should not be removed when the tree
  345. * is updated.
  346. */
  347. return dentry;
  348. }
  349. struct dentry *hypfs_create_u64(struct dentry *dir,
  350. const char *name, __u64 value)
  351. {
  352. char *buffer;
  353. char tmp[TMP_SIZE];
  354. struct dentry *dentry;
  355. snprintf(tmp, TMP_SIZE, "%llu\n", (unsigned long long int)value);
  356. buffer = kstrdup(tmp, GFP_KERNEL);
  357. if (!buffer)
  358. return ERR_PTR(-ENOMEM);
  359. dentry =
  360. hypfs_create_file(dir, name, buffer, S_IFREG | REG_FILE_MODE);
  361. if (IS_ERR(dentry)) {
  362. kfree(buffer);
  363. return ERR_PTR(-ENOMEM);
  364. }
  365. hypfs_add_dentry(dentry);
  366. return dentry;
  367. }
  368. struct dentry *hypfs_create_str(struct dentry *dir,
  369. const char *name, char *string)
  370. {
  371. char *buffer;
  372. struct dentry *dentry;
  373. buffer = kmalloc(strlen(string) + 2, GFP_KERNEL);
  374. if (!buffer)
  375. return ERR_PTR(-ENOMEM);
  376. sprintf(buffer, "%s\n", string);
  377. dentry =
  378. hypfs_create_file(dir, name, buffer, S_IFREG | REG_FILE_MODE);
  379. if (IS_ERR(dentry)) {
  380. kfree(buffer);
  381. return ERR_PTR(-ENOMEM);
  382. }
  383. hypfs_add_dentry(dentry);
  384. return dentry;
  385. }
  386. static const struct file_operations hypfs_file_ops = {
  387. .open = hypfs_open,
  388. .release = hypfs_release,
  389. .read_iter = hypfs_read_iter,
  390. .write_iter = hypfs_write_iter,
  391. .llseek = no_llseek,
  392. };
  393. static struct file_system_type hypfs_type = {
  394. .owner = THIS_MODULE,
  395. .name = "s390_hypfs",
  396. .init_fs_context = hypfs_init_fs_context,
  397. .parameters = hypfs_fs_parameters,
  398. .kill_sb = hypfs_kill_super
  399. };
  400. static const struct super_operations hypfs_s_ops = {
  401. .statfs = simple_statfs,
  402. .evict_inode = hypfs_evict_inode,
  403. .show_options = hypfs_show_options,
  404. };
  405. static int __init hypfs_init(void)
  406. {
  407. int rc;
  408. hypfs_dbfs_init();
  409. if (hypfs_diag_init()) {
  410. rc = -ENODATA;
  411. goto fail_dbfs_exit;
  412. }
  413. if (hypfs_vm_init()) {
  414. rc = -ENODATA;
  415. goto fail_hypfs_diag_exit;
  416. }
  417. hypfs_sprp_init();
  418. if (hypfs_diag0c_init()) {
  419. rc = -ENODATA;
  420. goto fail_hypfs_sprp_exit;
  421. }
  422. rc = sysfs_create_mount_point(hypervisor_kobj, "s390");
  423. if (rc)
  424. goto fail_hypfs_diag0c_exit;
  425. rc = register_filesystem(&hypfs_type);
  426. if (rc)
  427. goto fail_filesystem;
  428. return 0;
  429. fail_filesystem:
  430. sysfs_remove_mount_point(hypervisor_kobj, "s390");
  431. fail_hypfs_diag0c_exit:
  432. hypfs_diag0c_exit();
  433. fail_hypfs_sprp_exit:
  434. hypfs_sprp_exit();
  435. hypfs_vm_exit();
  436. fail_hypfs_diag_exit:
  437. hypfs_diag_exit();
  438. pr_err("Initialization of hypfs failed with rc=%i\n", rc);
  439. fail_dbfs_exit:
  440. hypfs_dbfs_exit();
  441. return rc;
  442. }
  443. device_initcall(hypfs_init)