inode.c 12 KB

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