inode.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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 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/namei.h>
  14. #include <linux/vfs.h>
  15. #include <linux/slab.h>
  16. #include <linux/pagemap.h>
  17. #include <linux/time.h>
  18. #include <linux/parser.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/mount.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_delete(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, opt_err };
  185. static const match_table_t hypfs_tokens = {
  186. {opt_uid, "uid=%u"},
  187. {opt_gid, "gid=%u"},
  188. {opt_err, NULL}
  189. };
  190. static int hypfs_parse_options(char *options, struct super_block *sb)
  191. {
  192. char *str;
  193. substring_t args[MAX_OPT_ARGS];
  194. kuid_t uid;
  195. kgid_t gid;
  196. if (!options)
  197. return 0;
  198. while ((str = strsep(&options, ",")) != NULL) {
  199. int token, option;
  200. struct hypfs_sb_info *hypfs_info = sb->s_fs_info;
  201. if (!*str)
  202. continue;
  203. token = match_token(str, hypfs_tokens, args);
  204. switch (token) {
  205. case opt_uid:
  206. if (match_int(&args[0], &option))
  207. return -EINVAL;
  208. uid = make_kuid(current_user_ns(), option);
  209. if (!uid_valid(uid))
  210. return -EINVAL;
  211. hypfs_info->uid = uid;
  212. break;
  213. case opt_gid:
  214. if (match_int(&args[0], &option))
  215. return -EINVAL;
  216. gid = make_kgid(current_user_ns(), option);
  217. if (!gid_valid(gid))
  218. return -EINVAL;
  219. hypfs_info->gid = gid;
  220. break;
  221. case opt_err:
  222. default:
  223. pr_err("%s is not a valid mount option\n", str);
  224. return -EINVAL;
  225. }
  226. }
  227. return 0;
  228. }
  229. static int hypfs_show_options(struct seq_file *s, struct dentry *root)
  230. {
  231. struct hypfs_sb_info *hypfs_info = root->d_sb->s_fs_info;
  232. seq_printf(s, ",uid=%u", from_kuid_munged(&init_user_ns, hypfs_info->uid));
  233. seq_printf(s, ",gid=%u", from_kgid_munged(&init_user_ns, hypfs_info->gid));
  234. return 0;
  235. }
  236. static int hypfs_fill_super(struct super_block *sb, void *data, int silent)
  237. {
  238. struct inode *root_inode;
  239. struct dentry *root_dentry, *update_file;
  240. int rc = 0;
  241. struct hypfs_sb_info *sbi;
  242. sbi = kzalloc(sizeof(struct hypfs_sb_info), GFP_KERNEL);
  243. if (!sbi)
  244. return -ENOMEM;
  245. mutex_init(&sbi->lock);
  246. sbi->uid = current_uid();
  247. sbi->gid = current_gid();
  248. sb->s_fs_info = sbi;
  249. sb->s_blocksize = PAGE_SIZE;
  250. sb->s_blocksize_bits = PAGE_SHIFT;
  251. sb->s_magic = HYPFS_MAGIC;
  252. sb->s_op = &hypfs_s_ops;
  253. if (hypfs_parse_options(data, sb))
  254. return -EINVAL;
  255. root_inode = hypfs_make_inode(sb, S_IFDIR | 0755);
  256. if (!root_inode)
  257. return -ENOMEM;
  258. root_inode->i_op = &simple_dir_inode_operations;
  259. root_inode->i_fop = &simple_dir_operations;
  260. sb->s_root = root_dentry = d_make_root(root_inode);
  261. if (!root_dentry)
  262. return -ENOMEM;
  263. if (MACHINE_IS_VM)
  264. rc = hypfs_vm_create_files(root_dentry);
  265. else
  266. rc = hypfs_diag_create_files(root_dentry);
  267. if (rc)
  268. return rc;
  269. update_file = hypfs_create_update_file(root_dentry);
  270. if (IS_ERR(update_file))
  271. return PTR_ERR(update_file);
  272. sbi->update_file = update_file;
  273. hypfs_update_update(sb);
  274. pr_info("Hypervisor filesystem mounted\n");
  275. return 0;
  276. }
  277. static struct dentry *hypfs_mount(struct file_system_type *fst, int flags,
  278. const char *devname, void *data)
  279. {
  280. return mount_single(fst, flags, data, hypfs_fill_super);
  281. }
  282. static void hypfs_kill_super(struct super_block *sb)
  283. {
  284. struct hypfs_sb_info *sb_info = sb->s_fs_info;
  285. if (sb->s_root)
  286. hypfs_delete_tree(sb->s_root);
  287. if (sb_info && sb_info->update_file)
  288. hypfs_remove(sb_info->update_file);
  289. kfree(sb->s_fs_info);
  290. sb->s_fs_info = NULL;
  291. kill_litter_super(sb);
  292. }
  293. static struct dentry *hypfs_create_file(struct dentry *parent, const char *name,
  294. char *data, umode_t mode)
  295. {
  296. struct dentry *dentry;
  297. struct inode *inode;
  298. inode_lock(d_inode(parent));
  299. dentry = lookup_one_len(name, parent, strlen(name));
  300. if (IS_ERR(dentry)) {
  301. dentry = ERR_PTR(-ENOMEM);
  302. goto fail;
  303. }
  304. inode = hypfs_make_inode(parent->d_sb, mode);
  305. if (!inode) {
  306. dput(dentry);
  307. dentry = ERR_PTR(-ENOMEM);
  308. goto fail;
  309. }
  310. if (S_ISREG(mode)) {
  311. inode->i_fop = &hypfs_file_ops;
  312. if (data)
  313. inode->i_size = strlen(data);
  314. else
  315. inode->i_size = 0;
  316. } else if (S_ISDIR(mode)) {
  317. inode->i_op = &simple_dir_inode_operations;
  318. inode->i_fop = &simple_dir_operations;
  319. inc_nlink(d_inode(parent));
  320. } else
  321. BUG();
  322. inode->i_private = data;
  323. d_instantiate(dentry, inode);
  324. dget(dentry);
  325. fail:
  326. inode_unlock(d_inode(parent));
  327. return dentry;
  328. }
  329. struct dentry *hypfs_mkdir(struct dentry *parent, const char *name)
  330. {
  331. struct dentry *dentry;
  332. dentry = hypfs_create_file(parent, name, NULL, S_IFDIR | DIR_MODE);
  333. if (IS_ERR(dentry))
  334. return dentry;
  335. hypfs_add_dentry(dentry);
  336. return dentry;
  337. }
  338. static struct dentry *hypfs_create_update_file(struct dentry *dir)
  339. {
  340. struct dentry *dentry;
  341. dentry = hypfs_create_file(dir, "update", NULL,
  342. S_IFREG | UPDATE_FILE_MODE);
  343. /*
  344. * We do not put the update file on the 'delete' list with
  345. * hypfs_add_dentry(), since it should not be removed when the tree
  346. * is updated.
  347. */
  348. return dentry;
  349. }
  350. struct dentry *hypfs_create_u64(struct dentry *dir,
  351. const char *name, __u64 value)
  352. {
  353. char *buffer;
  354. char tmp[TMP_SIZE];
  355. struct dentry *dentry;
  356. snprintf(tmp, TMP_SIZE, "%llu\n", (unsigned long long int)value);
  357. buffer = kstrdup(tmp, GFP_KERNEL);
  358. if (!buffer)
  359. return ERR_PTR(-ENOMEM);
  360. dentry =
  361. hypfs_create_file(dir, name, buffer, S_IFREG | REG_FILE_MODE);
  362. if (IS_ERR(dentry)) {
  363. kfree(buffer);
  364. return ERR_PTR(-ENOMEM);
  365. }
  366. hypfs_add_dentry(dentry);
  367. return dentry;
  368. }
  369. struct dentry *hypfs_create_str(struct dentry *dir,
  370. const char *name, char *string)
  371. {
  372. char *buffer;
  373. struct dentry *dentry;
  374. buffer = kmalloc(strlen(string) + 2, GFP_KERNEL);
  375. if (!buffer)
  376. return ERR_PTR(-ENOMEM);
  377. sprintf(buffer, "%s\n", string);
  378. dentry =
  379. hypfs_create_file(dir, name, buffer, S_IFREG | REG_FILE_MODE);
  380. if (IS_ERR(dentry)) {
  381. kfree(buffer);
  382. return ERR_PTR(-ENOMEM);
  383. }
  384. hypfs_add_dentry(dentry);
  385. return dentry;
  386. }
  387. static const struct file_operations hypfs_file_ops = {
  388. .open = hypfs_open,
  389. .release = hypfs_release,
  390. .read_iter = hypfs_read_iter,
  391. .write_iter = hypfs_write_iter,
  392. .llseek = no_llseek,
  393. };
  394. static struct file_system_type hypfs_type = {
  395. .owner = THIS_MODULE,
  396. .name = "s390_hypfs",
  397. .mount = hypfs_mount,
  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. rc = hypfs_dbfs_init();
  409. if (rc)
  410. return rc;
  411. if (hypfs_diag_init()) {
  412. rc = -ENODATA;
  413. goto fail_dbfs_exit;
  414. }
  415. if (hypfs_vm_init()) {
  416. rc = -ENODATA;
  417. goto fail_hypfs_diag_exit;
  418. }
  419. if (hypfs_sprp_init()) {
  420. rc = -ENODATA;
  421. goto fail_hypfs_vm_exit;
  422. }
  423. if (hypfs_diag0c_init()) {
  424. rc = -ENODATA;
  425. goto fail_hypfs_sprp_exit;
  426. }
  427. rc = sysfs_create_mount_point(hypervisor_kobj, "s390");
  428. if (rc)
  429. goto fail_hypfs_diag0c_exit;
  430. rc = register_filesystem(&hypfs_type);
  431. if (rc)
  432. goto fail_filesystem;
  433. return 0;
  434. fail_filesystem:
  435. sysfs_remove_mount_point(hypervisor_kobj, "s390");
  436. fail_hypfs_diag0c_exit:
  437. hypfs_diag0c_exit();
  438. fail_hypfs_sprp_exit:
  439. hypfs_sprp_exit();
  440. fail_hypfs_vm_exit:
  441. hypfs_vm_exit();
  442. fail_hypfs_diag_exit:
  443. hypfs_diag_exit();
  444. fail_dbfs_exit:
  445. hypfs_dbfs_exit();
  446. pr_err("Initialization of hypfs failed with rc=%i\n", rc);
  447. return rc;
  448. }
  449. device_initcall(hypfs_init)