inode.c 11 KB

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