inode.c 12 KB

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