inode.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /*
  2. * arch/s390/hypfs/inode.c
  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/module.h>
  21. #include <linux/seq_file.h>
  22. #include <linux/mount.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 super_block *sb,
  28. struct dentry *dir);
  29. struct hypfs_sb_info {
  30. uid_t uid; /* uid used for files and dirs */
  31. gid_t gid; /* gid used for files and dirs */
  32. struct dentry *update_file; /* file to trigger update */
  33. time_t last_update; /* last update time in secs since 1970 */
  34. struct mutex lock; /* lock to protect update process */
  35. };
  36. static const struct file_operations hypfs_file_ops;
  37. static struct file_system_type hypfs_type;
  38. static const struct super_operations hypfs_s_ops;
  39. /* start of list of all dentries, which have to be deleted on update */
  40. static struct dentry *hypfs_last_dentry;
  41. struct dentry *hypfs_dbfs_dir;
  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 = sb_info->update_file->d_inode;
  46. sb_info->last_update = get_seconds();
  47. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  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 inline int hypfs_positive(struct dentry *dentry)
  56. {
  57. return dentry->d_inode && !d_unhashed(dentry);
  58. }
  59. static void hypfs_remove(struct dentry *dentry)
  60. {
  61. struct dentry *parent;
  62. parent = dentry->d_parent;
  63. if (!parent || !parent->d_inode)
  64. return;
  65. mutex_lock(&parent->d_inode->i_mutex);
  66. if (hypfs_positive(dentry)) {
  67. if (S_ISDIR(dentry->d_inode->i_mode))
  68. simple_rmdir(parent->d_inode, dentry);
  69. else
  70. simple_unlink(parent->d_inode, dentry);
  71. }
  72. d_delete(dentry);
  73. dput(dentry);
  74. mutex_unlock(&parent->d_inode->i_mutex);
  75. }
  76. static void hypfs_delete_tree(struct dentry *root)
  77. {
  78. while (hypfs_last_dentry) {
  79. struct dentry *next_dentry;
  80. next_dentry = hypfs_last_dentry->d_fsdata;
  81. hypfs_remove(hypfs_last_dentry);
  82. hypfs_last_dentry = next_dentry;
  83. }
  84. }
  85. static struct inode *hypfs_make_inode(struct super_block *sb, int mode)
  86. {
  87. struct inode *ret = new_inode(sb);
  88. if (ret) {
  89. struct hypfs_sb_info *hypfs_info = sb->s_fs_info;
  90. ret->i_mode = mode;
  91. ret->i_uid = hypfs_info->uid;
  92. ret->i_gid = hypfs_info->gid;
  93. ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME;
  94. if (mode & S_IFDIR)
  95. ret->i_nlink = 2;
  96. else
  97. ret->i_nlink = 1;
  98. }
  99. return ret;
  100. }
  101. static void hypfs_evict_inode(struct inode *inode)
  102. {
  103. end_writeback(inode);
  104. kfree(inode->i_private);
  105. }
  106. static int hypfs_open(struct inode *inode, struct file *filp)
  107. {
  108. char *data = filp->f_path.dentry->d_inode->i_private;
  109. struct hypfs_sb_info *fs_info;
  110. if (filp->f_mode & FMODE_WRITE) {
  111. if (!(inode->i_mode & S_IWUGO))
  112. return -EACCES;
  113. }
  114. if (filp->f_mode & FMODE_READ) {
  115. if (!(inode->i_mode & S_IRUGO))
  116. return -EACCES;
  117. }
  118. fs_info = inode->i_sb->s_fs_info;
  119. if(data) {
  120. mutex_lock(&fs_info->lock);
  121. filp->private_data = kstrdup(data, GFP_KERNEL);
  122. if (!filp->private_data) {
  123. mutex_unlock(&fs_info->lock);
  124. return -ENOMEM;
  125. }
  126. mutex_unlock(&fs_info->lock);
  127. }
  128. return nonseekable_open(inode, filp);
  129. }
  130. static ssize_t hypfs_aio_read(struct kiocb *iocb, const struct iovec *iov,
  131. unsigned long nr_segs, loff_t offset)
  132. {
  133. char *data;
  134. ssize_t ret;
  135. struct file *filp = iocb->ki_filp;
  136. /* XXX: temporary */
  137. char __user *buf = iov[0].iov_base;
  138. size_t count = iov[0].iov_len;
  139. if (nr_segs != 1)
  140. return -EINVAL;
  141. data = filp->private_data;
  142. ret = simple_read_from_buffer(buf, count, &offset, data, strlen(data));
  143. if (ret <= 0)
  144. return ret;
  145. iocb->ki_pos += ret;
  146. file_accessed(filp);
  147. return ret;
  148. }
  149. static ssize_t hypfs_aio_write(struct kiocb *iocb, const struct iovec *iov,
  150. unsigned long nr_segs, loff_t offset)
  151. {
  152. int rc;
  153. struct super_block *sb;
  154. struct hypfs_sb_info *fs_info;
  155. size_t count = iov_length(iov, nr_segs);
  156. sb = iocb->ki_filp->f_path.dentry->d_inode->i_sb;
  157. fs_info = sb->s_fs_info;
  158. /*
  159. * Currently we only allow one update per second for two reasons:
  160. * 1. diag 204 is VERY expensive
  161. * 2. If several processes do updates in parallel and then read the
  162. * hypfs data, the likelihood of collisions is reduced, if we restrict
  163. * the minimum update interval. A collision occurs, if during the
  164. * data gathering of one process another process triggers an update
  165. * If the first process wants to ensure consistent data, it has
  166. * to restart data collection in this case.
  167. */
  168. mutex_lock(&fs_info->lock);
  169. if (fs_info->last_update == get_seconds()) {
  170. rc = -EBUSY;
  171. goto out;
  172. }
  173. hypfs_delete_tree(sb->s_root);
  174. if (MACHINE_IS_VM)
  175. rc = hypfs_vm_create_files(sb, sb->s_root);
  176. else
  177. rc = hypfs_diag_create_files(sb, sb->s_root);
  178. if (rc) {
  179. pr_err("Updating the hypfs tree failed\n");
  180. hypfs_delete_tree(sb->s_root);
  181. goto out;
  182. }
  183. hypfs_update_update(sb);
  184. rc = count;
  185. out:
  186. mutex_unlock(&fs_info->lock);
  187. return rc;
  188. }
  189. static int hypfs_release(struct inode *inode, struct file *filp)
  190. {
  191. kfree(filp->private_data);
  192. return 0;
  193. }
  194. enum { opt_uid, opt_gid, opt_err };
  195. static const match_table_t hypfs_tokens = {
  196. {opt_uid, "uid=%u"},
  197. {opt_gid, "gid=%u"},
  198. {opt_err, NULL}
  199. };
  200. static int hypfs_parse_options(char *options, struct super_block *sb)
  201. {
  202. char *str;
  203. substring_t args[MAX_OPT_ARGS];
  204. if (!options)
  205. return 0;
  206. while ((str = strsep(&options, ",")) != NULL) {
  207. int token, option;
  208. struct hypfs_sb_info *hypfs_info = sb->s_fs_info;
  209. if (!*str)
  210. continue;
  211. token = match_token(str, hypfs_tokens, args);
  212. switch (token) {
  213. case opt_uid:
  214. if (match_int(&args[0], &option))
  215. return -EINVAL;
  216. hypfs_info->uid = option;
  217. break;
  218. case opt_gid:
  219. if (match_int(&args[0], &option))
  220. return -EINVAL;
  221. hypfs_info->gid = option;
  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 vfsmount *mnt)
  232. {
  233. struct hypfs_sb_info *hypfs_info = mnt->mnt_sb->s_fs_info;
  234. seq_printf(s, ",uid=%u", hypfs_info->uid);
  235. seq_printf(s, ",gid=%u", 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_alloc_root(root_inode);
  263. if (!root_dentry) {
  264. iput(root_inode);
  265. return -ENOMEM;
  266. }
  267. if (MACHINE_IS_VM)
  268. rc = hypfs_vm_create_files(sb, root_dentry);
  269. else
  270. rc = hypfs_diag_create_files(sb, root_dentry);
  271. if (rc)
  272. return rc;
  273. sbi->update_file = hypfs_create_update_file(sb, root_dentry);
  274. if (IS_ERR(sbi->update_file))
  275. return PTR_ERR(sbi->update_file);
  276. hypfs_update_update(sb);
  277. pr_info("Hypervisor filesystem mounted\n");
  278. return 0;
  279. }
  280. static struct dentry *hypfs_mount(struct file_system_type *fst, int flags,
  281. const char *devname, void *data)
  282. {
  283. return mount_single(fst, flags, data, hypfs_fill_super);
  284. }
  285. static void hypfs_kill_super(struct super_block *sb)
  286. {
  287. struct hypfs_sb_info *sb_info = sb->s_fs_info;
  288. if (sb->s_root)
  289. hypfs_delete_tree(sb->s_root);
  290. if (sb_info->update_file)
  291. hypfs_remove(sb_info->update_file);
  292. kfree(sb->s_fs_info);
  293. sb->s_fs_info = NULL;
  294. kill_litter_super(sb);
  295. }
  296. static struct dentry *hypfs_create_file(struct super_block *sb,
  297. struct dentry *parent, const char *name,
  298. char *data, mode_t mode)
  299. {
  300. struct dentry *dentry;
  301. struct inode *inode;
  302. mutex_lock(&parent->d_inode->i_mutex);
  303. dentry = lookup_one_len(name, parent, strlen(name));
  304. if (IS_ERR(dentry)) {
  305. dentry = ERR_PTR(-ENOMEM);
  306. goto fail;
  307. }
  308. inode = hypfs_make_inode(sb, mode);
  309. if (!inode) {
  310. dput(dentry);
  311. dentry = ERR_PTR(-ENOMEM);
  312. goto fail;
  313. }
  314. if (mode & S_IFREG) {
  315. inode->i_fop = &hypfs_file_ops;
  316. if (data)
  317. inode->i_size = strlen(data);
  318. else
  319. inode->i_size = 0;
  320. } else if (mode & S_IFDIR) {
  321. inode->i_op = &simple_dir_inode_operations;
  322. inode->i_fop = &simple_dir_operations;
  323. parent->d_inode->i_nlink++;
  324. } else
  325. BUG();
  326. inode->i_private = data;
  327. d_instantiate(dentry, inode);
  328. dget(dentry);
  329. fail:
  330. mutex_unlock(&parent->d_inode->i_mutex);
  331. return dentry;
  332. }
  333. struct dentry *hypfs_mkdir(struct super_block *sb, struct dentry *parent,
  334. const char *name)
  335. {
  336. struct dentry *dentry;
  337. dentry = hypfs_create_file(sb, parent, name, NULL, S_IFDIR | DIR_MODE);
  338. if (IS_ERR(dentry))
  339. return dentry;
  340. hypfs_add_dentry(dentry);
  341. return dentry;
  342. }
  343. static struct dentry *hypfs_create_update_file(struct super_block *sb,
  344. struct dentry *dir)
  345. {
  346. struct dentry *dentry;
  347. dentry = hypfs_create_file(sb, dir, "update", NULL,
  348. S_IFREG | UPDATE_FILE_MODE);
  349. /*
  350. * We do not put the update file on the 'delete' list with
  351. * hypfs_add_dentry(), since it should not be removed when the tree
  352. * is updated.
  353. */
  354. return dentry;
  355. }
  356. struct dentry *hypfs_create_u64(struct super_block *sb, struct dentry *dir,
  357. const char *name, __u64 value)
  358. {
  359. char *buffer;
  360. char tmp[TMP_SIZE];
  361. struct dentry *dentry;
  362. snprintf(tmp, TMP_SIZE, "%llu\n", (unsigned long long int)value);
  363. buffer = kstrdup(tmp, GFP_KERNEL);
  364. if (!buffer)
  365. return ERR_PTR(-ENOMEM);
  366. dentry =
  367. hypfs_create_file(sb, dir, name, buffer, S_IFREG | REG_FILE_MODE);
  368. if (IS_ERR(dentry)) {
  369. kfree(buffer);
  370. return ERR_PTR(-ENOMEM);
  371. }
  372. hypfs_add_dentry(dentry);
  373. return dentry;
  374. }
  375. struct dentry *hypfs_create_str(struct super_block *sb, struct dentry *dir,
  376. const char *name, char *string)
  377. {
  378. char *buffer;
  379. struct dentry *dentry;
  380. buffer = kmalloc(strlen(string) + 2, GFP_KERNEL);
  381. if (!buffer)
  382. return ERR_PTR(-ENOMEM);
  383. sprintf(buffer, "%s\n", string);
  384. dentry =
  385. hypfs_create_file(sb, dir, name, buffer, S_IFREG | REG_FILE_MODE);
  386. if (IS_ERR(dentry)) {
  387. kfree(buffer);
  388. return ERR_PTR(-ENOMEM);
  389. }
  390. hypfs_add_dentry(dentry);
  391. return dentry;
  392. }
  393. static const struct file_operations hypfs_file_ops = {
  394. .open = hypfs_open,
  395. .release = hypfs_release,
  396. .read = do_sync_read,
  397. .write = do_sync_write,
  398. .aio_read = hypfs_aio_read,
  399. .aio_write = hypfs_aio_write,
  400. .llseek = no_llseek,
  401. };
  402. static struct file_system_type hypfs_type = {
  403. .owner = THIS_MODULE,
  404. .name = "s390_hypfs",
  405. .mount = hypfs_mount,
  406. .kill_sb = hypfs_kill_super
  407. };
  408. static const struct super_operations hypfs_s_ops = {
  409. .statfs = simple_statfs,
  410. .evict_inode = hypfs_evict_inode,
  411. .show_options = hypfs_show_options,
  412. };
  413. static struct kobject *s390_kobj;
  414. static int __init hypfs_init(void)
  415. {
  416. int rc;
  417. hypfs_dbfs_dir = debugfs_create_dir("s390_hypfs", NULL);
  418. if (IS_ERR(hypfs_dbfs_dir))
  419. return PTR_ERR(hypfs_dbfs_dir);
  420. if (hypfs_diag_init()) {
  421. rc = -ENODATA;
  422. goto fail_debugfs_remove;
  423. }
  424. if (hypfs_vm_init()) {
  425. rc = -ENODATA;
  426. goto fail_hypfs_diag_exit;
  427. }
  428. s390_kobj = kobject_create_and_add("s390", hypervisor_kobj);
  429. if (!s390_kobj) {
  430. rc = -ENOMEM;
  431. goto fail_hypfs_vm_exit;
  432. }
  433. rc = register_filesystem(&hypfs_type);
  434. if (rc)
  435. goto fail_filesystem;
  436. return 0;
  437. fail_filesystem:
  438. kobject_put(s390_kobj);
  439. fail_hypfs_vm_exit:
  440. hypfs_vm_exit();
  441. fail_hypfs_diag_exit:
  442. hypfs_diag_exit();
  443. fail_debugfs_remove:
  444. debugfs_remove(hypfs_dbfs_dir);
  445. pr_err("Initialization of hypfs failed with rc=%i\n", rc);
  446. return rc;
  447. }
  448. static void __exit hypfs_exit(void)
  449. {
  450. hypfs_diag_exit();
  451. hypfs_vm_exit();
  452. debugfs_remove(hypfs_dbfs_dir);
  453. unregister_filesystem(&hypfs_type);
  454. kobject_put(s390_kobj);
  455. }
  456. module_init(hypfs_init)
  457. module_exit(hypfs_exit)
  458. MODULE_LICENSE("GPL");
  459. MODULE_AUTHOR("Michael Holzheu <holzheu@de.ibm.com>");
  460. MODULE_DESCRIPTION("s390 Hypervisor Filesystem");