inode.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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_blksize = PAGE_CACHE_SIZE;
  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->u.generic_ip);
  92. generic_delete_inode(inode);
  93. }
  94. static int hypfs_open(struct inode *inode, struct file *filp)
  95. {
  96. char *data = filp->f_dentry->d_inode->u.generic_ip;
  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, __user char *buf,
  119. size_t count, loff_t offset)
  120. {
  121. char *data;
  122. size_t len;
  123. struct file *filp = iocb->ki_filp;
  124. data = filp->private_data;
  125. len = strlen(data);
  126. if (offset > len) {
  127. count = 0;
  128. goto out;
  129. }
  130. if (count > len - offset)
  131. count = len - offset;
  132. if (copy_to_user(buf, data + offset, count)) {
  133. count = -EFAULT;
  134. goto out;
  135. }
  136. iocb->ki_pos += count;
  137. file_accessed(filp);
  138. out:
  139. return count;
  140. }
  141. static ssize_t hypfs_aio_write(struct kiocb *iocb, const char __user *buf,
  142. size_t count, loff_t pos)
  143. {
  144. int rc;
  145. struct super_block *sb;
  146. struct hypfs_sb_info *fs_info;
  147. sb = iocb->ki_filp->f_dentry->d_inode->i_sb;
  148. fs_info = sb->s_fs_info;
  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. rc = hypfs_diag_create_files(sb, sb->s_root);
  166. if (rc) {
  167. printk(KERN_ERR "hypfs: Update failed\n");
  168. hypfs_delete_tree(sb->s_root);
  169. goto out;
  170. }
  171. hypfs_update_update(sb);
  172. rc = count;
  173. out:
  174. mutex_unlock(&fs_info->lock);
  175. return rc;
  176. }
  177. static int hypfs_release(struct inode *inode, struct file *filp)
  178. {
  179. kfree(filp->private_data);
  180. return 0;
  181. }
  182. enum { opt_uid, opt_gid, opt_err };
  183. static match_table_t hypfs_tokens = {
  184. {opt_uid, "uid=%u"},
  185. {opt_gid, "gid=%u"},
  186. {opt_err, NULL}
  187. };
  188. static int hypfs_parse_options(char *options, struct super_block *sb)
  189. {
  190. char *str;
  191. substring_t args[MAX_OPT_ARGS];
  192. if (!options)
  193. return 0;
  194. while ((str = strsep(&options, ",")) != NULL) {
  195. int token, option;
  196. struct hypfs_sb_info *hypfs_info = sb->s_fs_info;
  197. if (!*str)
  198. continue;
  199. token = match_token(str, hypfs_tokens, args);
  200. switch (token) {
  201. case opt_uid:
  202. if (match_int(&args[0], &option))
  203. return -EINVAL;
  204. hypfs_info->uid = option;
  205. break;
  206. case opt_gid:
  207. if (match_int(&args[0], &option))
  208. return -EINVAL;
  209. hypfs_info->gid = option;
  210. break;
  211. case opt_err:
  212. default:
  213. printk(KERN_ERR "hypfs: Unrecognized mount option "
  214. "\"%s\" or missing value\n", str);
  215. return -EINVAL;
  216. }
  217. }
  218. return 0;
  219. }
  220. static int hypfs_fill_super(struct super_block *sb, void *data, int silent)
  221. {
  222. struct inode *root_inode;
  223. struct dentry *root_dentry;
  224. int rc = 0;
  225. struct hypfs_sb_info *sbi;
  226. sbi = kzalloc(sizeof(struct hypfs_sb_info), GFP_KERNEL);
  227. if (!sbi)
  228. return -ENOMEM;
  229. mutex_init(&sbi->lock);
  230. sbi->uid = current->uid;
  231. sbi->gid = current->gid;
  232. sb->s_fs_info = sbi;
  233. sb->s_blocksize = PAGE_CACHE_SIZE;
  234. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  235. sb->s_magic = HYPFS_MAGIC;
  236. sb->s_op = &hypfs_s_ops;
  237. if (hypfs_parse_options(data, sb)) {
  238. rc = -EINVAL;
  239. goto err_alloc;
  240. }
  241. root_inode = hypfs_make_inode(sb, S_IFDIR | 0755);
  242. if (!root_inode) {
  243. rc = -ENOMEM;
  244. goto err_alloc;
  245. }
  246. root_inode->i_op = &simple_dir_inode_operations;
  247. root_inode->i_fop = &simple_dir_operations;
  248. root_dentry = d_alloc_root(root_inode);
  249. if (!root_dentry) {
  250. iput(root_inode);
  251. rc = -ENOMEM;
  252. goto err_alloc;
  253. }
  254. rc = hypfs_diag_create_files(sb, root_dentry);
  255. if (rc)
  256. goto err_tree;
  257. sbi->update_file = hypfs_create_update_file(sb, root_dentry);
  258. if (IS_ERR(sbi->update_file)) {
  259. rc = PTR_ERR(sbi->update_file);
  260. goto err_tree;
  261. }
  262. hypfs_update_update(sb);
  263. sb->s_root = root_dentry;
  264. return 0;
  265. err_tree:
  266. hypfs_delete_tree(root_dentry);
  267. d_genocide(root_dentry);
  268. dput(root_dentry);
  269. err_alloc:
  270. kfree(sbi);
  271. return rc;
  272. }
  273. static int hypfs_get_super(struct file_system_type *fst, int flags,
  274. const char *devname, void *data, struct vfsmount *mnt)
  275. {
  276. return get_sb_single(fst, flags, data, hypfs_fill_super, mnt);
  277. }
  278. static void hypfs_kill_super(struct super_block *sb)
  279. {
  280. struct hypfs_sb_info *sb_info = sb->s_fs_info;
  281. if (sb->s_root) {
  282. hypfs_delete_tree(sb->s_root);
  283. hypfs_remove(sb_info->update_file);
  284. kfree(sb->s_fs_info);
  285. sb->s_fs_info = NULL;
  286. }
  287. kill_litter_super(sb);
  288. }
  289. static struct dentry *hypfs_create_file(struct super_block *sb,
  290. struct dentry *parent, const char *name,
  291. char *data, mode_t mode)
  292. {
  293. struct dentry *dentry;
  294. struct inode *inode;
  295. struct qstr qname;
  296. qname.name = name;
  297. qname.len = strlen(name);
  298. qname.hash = full_name_hash(name, qname.len);
  299. dentry = lookup_one_len(name, parent, strlen(name));
  300. if (IS_ERR(dentry))
  301. return ERR_PTR(-ENOMEM);
  302. inode = hypfs_make_inode(sb, mode);
  303. if (!inode) {
  304. dput(dentry);
  305. return ERR_PTR(-ENOMEM);
  306. }
  307. if (mode & S_IFREG) {
  308. inode->i_fop = &hypfs_file_ops;
  309. if (data)
  310. inode->i_size = strlen(data);
  311. else
  312. inode->i_size = 0;
  313. } else if (mode & S_IFDIR) {
  314. inode->i_op = &simple_dir_inode_operations;
  315. inode->i_fop = &simple_dir_operations;
  316. parent->d_inode->i_nlink++;
  317. } else
  318. BUG();
  319. inode->u.generic_ip = data;
  320. d_instantiate(dentry, inode);
  321. dget(dentry);
  322. return dentry;
  323. }
  324. struct dentry *hypfs_mkdir(struct super_block *sb, struct dentry *parent,
  325. const char *name)
  326. {
  327. struct dentry *dentry;
  328. dentry = hypfs_create_file(sb, parent, name, NULL, S_IFDIR | DIR_MODE);
  329. if (IS_ERR(dentry))
  330. return dentry;
  331. hypfs_add_dentry(dentry);
  332. parent->d_inode->i_nlink++;
  333. return dentry;
  334. }
  335. static struct dentry *hypfs_create_update_file(struct super_block *sb,
  336. struct dentry *dir)
  337. {
  338. struct dentry *dentry;
  339. dentry = hypfs_create_file(sb, dir, "update", NULL,
  340. S_IFREG | UPDATE_FILE_MODE);
  341. /*
  342. * We do not put the update file on the 'delete' list with
  343. * hypfs_add_dentry(), since it should not be removed when the tree
  344. * is updated.
  345. */
  346. return dentry;
  347. }
  348. struct dentry *hypfs_create_u64(struct super_block *sb, struct dentry *dir,
  349. const char *name, __u64 value)
  350. {
  351. char *buffer;
  352. char tmp[TMP_SIZE];
  353. struct dentry *dentry;
  354. snprintf(tmp, TMP_SIZE, "%lld\n", (unsigned long long int)value);
  355. buffer = kstrdup(tmp, GFP_KERNEL);
  356. if (!buffer)
  357. return ERR_PTR(-ENOMEM);
  358. dentry =
  359. hypfs_create_file(sb, dir, name, buffer, S_IFREG | REG_FILE_MODE);
  360. if (IS_ERR(dentry)) {
  361. kfree(buffer);
  362. return ERR_PTR(-ENOMEM);
  363. }
  364. hypfs_add_dentry(dentry);
  365. return dentry;
  366. }
  367. struct dentry *hypfs_create_str(struct super_block *sb, struct dentry *dir,
  368. const char *name, char *string)
  369. {
  370. char *buffer;
  371. struct dentry *dentry;
  372. buffer = kmalloc(strlen(string) + 2, GFP_KERNEL);
  373. if (!buffer)
  374. return ERR_PTR(-ENOMEM);
  375. sprintf(buffer, "%s\n", string);
  376. dentry =
  377. hypfs_create_file(sb, dir, name, buffer, S_IFREG | REG_FILE_MODE);
  378. if (IS_ERR(dentry)) {
  379. kfree(buffer);
  380. return ERR_PTR(-ENOMEM);
  381. }
  382. hypfs_add_dentry(dentry);
  383. return dentry;
  384. }
  385. static struct file_operations hypfs_file_ops = {
  386. .open = hypfs_open,
  387. .release = hypfs_release,
  388. .read = do_sync_read,
  389. .write = do_sync_write,
  390. .aio_read = hypfs_aio_read,
  391. .aio_write = hypfs_aio_write,
  392. };
  393. static struct file_system_type hypfs_type = {
  394. .owner = THIS_MODULE,
  395. .name = "s390_hypfs",
  396. .get_sb = hypfs_get_super,
  397. .kill_sb = hypfs_kill_super
  398. };
  399. static struct super_operations hypfs_s_ops = {
  400. .statfs = simple_statfs,
  401. .drop_inode = hypfs_drop_inode,
  402. };
  403. static decl_subsys(s390, NULL, NULL);
  404. static int __init hypfs_init(void)
  405. {
  406. int rc;
  407. if (MACHINE_IS_VM)
  408. return -ENODATA;
  409. if (hypfs_diag_init()) {
  410. rc = -ENODATA;
  411. goto fail_diag;
  412. }
  413. kset_set_kset_s(&s390_subsys, hypervisor_subsys);
  414. rc = subsystem_register(&s390_subsys);
  415. if (rc)
  416. goto fail_sysfs;
  417. rc = register_filesystem(&hypfs_type);
  418. if (rc)
  419. goto fail_filesystem;
  420. return 0;
  421. fail_filesystem:
  422. subsystem_unregister(&s390_subsys);
  423. fail_sysfs:
  424. hypfs_diag_exit();
  425. fail_diag:
  426. printk(KERN_ERR "hypfs: Initialization failed with rc = %i.\n", rc);
  427. return rc;
  428. }
  429. static void __exit hypfs_exit(void)
  430. {
  431. hypfs_diag_exit();
  432. unregister_filesystem(&hypfs_type);
  433. subsystem_unregister(&s390_subsys);
  434. }
  435. module_init(hypfs_init)
  436. module_exit(hypfs_exit)
  437. MODULE_LICENSE("GPL");
  438. MODULE_AUTHOR("Michael Holzheu <holzheu@de.ibm.com>");
  439. MODULE_DESCRIPTION("s390 Hypervisor Filesystem");