inode.c 12 KB

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