inode.c 12 KB

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