inode.c 12 KB

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