inode.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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. size_t len;
  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. count = -EINVAL;
  139. goto out;
  140. }
  141. data = filp->private_data;
  142. len = strlen(data);
  143. if (offset > len) {
  144. count = 0;
  145. goto out;
  146. }
  147. if (count > len - offset)
  148. count = len - offset;
  149. if (copy_to_user(buf, data + offset, count)) {
  150. count = -EFAULT;
  151. goto out;
  152. }
  153. iocb->ki_pos += count;
  154. file_accessed(filp);
  155. out:
  156. return count;
  157. }
  158. static ssize_t hypfs_aio_write(struct kiocb *iocb, const struct iovec *iov,
  159. unsigned long nr_segs, loff_t offset)
  160. {
  161. int rc;
  162. struct super_block *sb;
  163. struct hypfs_sb_info *fs_info;
  164. size_t count = iov_length(iov, nr_segs);
  165. sb = iocb->ki_filp->f_path.dentry->d_inode->i_sb;
  166. fs_info = sb->s_fs_info;
  167. /*
  168. * Currently we only allow one update per second for two reasons:
  169. * 1. diag 204 is VERY expensive
  170. * 2. If several processes do updates in parallel and then read the
  171. * hypfs data, the likelihood of collisions is reduced, if we restrict
  172. * the minimum update interval. A collision occurs, if during the
  173. * data gathering of one process another process triggers an update
  174. * If the first process wants to ensure consistent data, it has
  175. * to restart data collection in this case.
  176. */
  177. mutex_lock(&fs_info->lock);
  178. if (fs_info->last_update == get_seconds()) {
  179. rc = -EBUSY;
  180. goto out;
  181. }
  182. hypfs_delete_tree(sb->s_root);
  183. if (MACHINE_IS_VM)
  184. rc = hypfs_vm_create_files(sb, sb->s_root);
  185. else
  186. rc = hypfs_diag_create_files(sb, sb->s_root);
  187. if (rc) {
  188. printk(KERN_ERR "hypfs: Update failed\n");
  189. hypfs_delete_tree(sb->s_root);
  190. goto out;
  191. }
  192. hypfs_update_update(sb);
  193. rc = count;
  194. out:
  195. mutex_unlock(&fs_info->lock);
  196. return rc;
  197. }
  198. static int hypfs_release(struct inode *inode, struct file *filp)
  199. {
  200. kfree(filp->private_data);
  201. return 0;
  202. }
  203. enum { opt_uid, opt_gid, opt_err };
  204. static match_table_t hypfs_tokens = {
  205. {opt_uid, "uid=%u"},
  206. {opt_gid, "gid=%u"},
  207. {opt_err, NULL}
  208. };
  209. static int hypfs_parse_options(char *options, struct super_block *sb)
  210. {
  211. char *str;
  212. substring_t args[MAX_OPT_ARGS];
  213. if (!options)
  214. return 0;
  215. while ((str = strsep(&options, ",")) != NULL) {
  216. int token, option;
  217. struct hypfs_sb_info *hypfs_info = sb->s_fs_info;
  218. if (!*str)
  219. continue;
  220. token = match_token(str, hypfs_tokens, args);
  221. switch (token) {
  222. case opt_uid:
  223. if (match_int(&args[0], &option))
  224. return -EINVAL;
  225. hypfs_info->uid = option;
  226. break;
  227. case opt_gid:
  228. if (match_int(&args[0], &option))
  229. return -EINVAL;
  230. hypfs_info->gid = option;
  231. break;
  232. case opt_err:
  233. default:
  234. printk(KERN_ERR "hypfs: Unrecognized mount option "
  235. "\"%s\" or missing value\n", str);
  236. return -EINVAL;
  237. }
  238. }
  239. return 0;
  240. }
  241. static int hypfs_show_options(struct seq_file *s, struct vfsmount *mnt)
  242. {
  243. struct hypfs_sb_info *hypfs_info = mnt->mnt_sb->s_fs_info;
  244. seq_printf(s, ",uid=%u", hypfs_info->uid);
  245. seq_printf(s, ",gid=%u", hypfs_info->gid);
  246. return 0;
  247. }
  248. static int hypfs_fill_super(struct super_block *sb, void *data, int silent)
  249. {
  250. struct inode *root_inode;
  251. struct dentry *root_dentry;
  252. int rc = 0;
  253. struct hypfs_sb_info *sbi;
  254. sbi = kzalloc(sizeof(struct hypfs_sb_info), GFP_KERNEL);
  255. if (!sbi)
  256. return -ENOMEM;
  257. mutex_init(&sbi->lock);
  258. sbi->uid = current->uid;
  259. sbi->gid = current->gid;
  260. sb->s_fs_info = sbi;
  261. sb->s_blocksize = PAGE_CACHE_SIZE;
  262. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  263. sb->s_magic = HYPFS_MAGIC;
  264. sb->s_op = &hypfs_s_ops;
  265. if (hypfs_parse_options(data, sb)) {
  266. rc = -EINVAL;
  267. goto err_alloc;
  268. }
  269. root_inode = hypfs_make_inode(sb, S_IFDIR | 0755);
  270. if (!root_inode) {
  271. rc = -ENOMEM;
  272. goto err_alloc;
  273. }
  274. root_inode->i_op = &simple_dir_inode_operations;
  275. root_inode->i_fop = &simple_dir_operations;
  276. root_dentry = d_alloc_root(root_inode);
  277. if (!root_dentry) {
  278. iput(root_inode);
  279. rc = -ENOMEM;
  280. goto err_alloc;
  281. }
  282. if (MACHINE_IS_VM)
  283. rc = hypfs_vm_create_files(sb, root_dentry);
  284. else
  285. rc = hypfs_diag_create_files(sb, root_dentry);
  286. if (rc)
  287. goto err_tree;
  288. sbi->update_file = hypfs_create_update_file(sb, root_dentry);
  289. if (IS_ERR(sbi->update_file)) {
  290. rc = PTR_ERR(sbi->update_file);
  291. goto err_tree;
  292. }
  293. hypfs_update_update(sb);
  294. sb->s_root = root_dentry;
  295. printk(KERN_INFO "hypfs: Hypervisor filesystem mounted\n");
  296. return 0;
  297. err_tree:
  298. hypfs_delete_tree(root_dentry);
  299. d_genocide(root_dentry);
  300. dput(root_dentry);
  301. err_alloc:
  302. kfree(sbi);
  303. return rc;
  304. }
  305. static int hypfs_get_super(struct file_system_type *fst, int flags,
  306. const char *devname, void *data, struct vfsmount *mnt)
  307. {
  308. return get_sb_single(fst, flags, data, hypfs_fill_super, mnt);
  309. }
  310. static void hypfs_kill_super(struct super_block *sb)
  311. {
  312. struct hypfs_sb_info *sb_info = sb->s_fs_info;
  313. if (sb->s_root) {
  314. hypfs_delete_tree(sb->s_root);
  315. hypfs_remove(sb_info->update_file);
  316. kfree(sb->s_fs_info);
  317. sb->s_fs_info = NULL;
  318. }
  319. kill_litter_super(sb);
  320. }
  321. static struct dentry *hypfs_create_file(struct super_block *sb,
  322. struct dentry *parent, const char *name,
  323. char *data, mode_t mode)
  324. {
  325. struct dentry *dentry;
  326. struct inode *inode;
  327. struct qstr qname;
  328. qname.name = name;
  329. qname.len = strlen(name);
  330. qname.hash = full_name_hash(name, qname.len);
  331. mutex_lock(&parent->d_inode->i_mutex);
  332. dentry = lookup_one_len(name, parent, strlen(name));
  333. if (IS_ERR(dentry)) {
  334. dentry = ERR_PTR(-ENOMEM);
  335. goto fail;
  336. }
  337. inode = hypfs_make_inode(sb, mode);
  338. if (!inode) {
  339. dput(dentry);
  340. dentry = ERR_PTR(-ENOMEM);
  341. goto fail;
  342. }
  343. if (mode & S_IFREG) {
  344. inode->i_fop = &hypfs_file_ops;
  345. if (data)
  346. inode->i_size = strlen(data);
  347. else
  348. inode->i_size = 0;
  349. } else if (mode & S_IFDIR) {
  350. inode->i_op = &simple_dir_inode_operations;
  351. inode->i_fop = &simple_dir_operations;
  352. parent->d_inode->i_nlink++;
  353. } else
  354. BUG();
  355. inode->i_private = data;
  356. d_instantiate(dentry, inode);
  357. dget(dentry);
  358. fail:
  359. mutex_unlock(&parent->d_inode->i_mutex);
  360. return dentry;
  361. }
  362. struct dentry *hypfs_mkdir(struct super_block *sb, struct dentry *parent,
  363. const char *name)
  364. {
  365. struct dentry *dentry;
  366. dentry = hypfs_create_file(sb, parent, name, NULL, S_IFDIR | DIR_MODE);
  367. if (IS_ERR(dentry))
  368. return dentry;
  369. hypfs_add_dentry(dentry);
  370. return dentry;
  371. }
  372. static struct dentry *hypfs_create_update_file(struct super_block *sb,
  373. struct dentry *dir)
  374. {
  375. struct dentry *dentry;
  376. dentry = hypfs_create_file(sb, dir, "update", NULL,
  377. S_IFREG | UPDATE_FILE_MODE);
  378. /*
  379. * We do not put the update file on the 'delete' list with
  380. * hypfs_add_dentry(), since it should not be removed when the tree
  381. * is updated.
  382. */
  383. return dentry;
  384. }
  385. struct dentry *hypfs_create_u64(struct super_block *sb, struct dentry *dir,
  386. const char *name, __u64 value)
  387. {
  388. char *buffer;
  389. char tmp[TMP_SIZE];
  390. struct dentry *dentry;
  391. snprintf(tmp, TMP_SIZE, "%lld\n", (unsigned long long int)value);
  392. buffer = kstrdup(tmp, GFP_KERNEL);
  393. if (!buffer)
  394. return ERR_PTR(-ENOMEM);
  395. dentry =
  396. hypfs_create_file(sb, dir, name, buffer, S_IFREG | REG_FILE_MODE);
  397. if (IS_ERR(dentry)) {
  398. kfree(buffer);
  399. return ERR_PTR(-ENOMEM);
  400. }
  401. hypfs_add_dentry(dentry);
  402. return dentry;
  403. }
  404. struct dentry *hypfs_create_str(struct super_block *sb, struct dentry *dir,
  405. const char *name, char *string)
  406. {
  407. char *buffer;
  408. struct dentry *dentry;
  409. buffer = kmalloc(strlen(string) + 2, GFP_KERNEL);
  410. if (!buffer)
  411. return ERR_PTR(-ENOMEM);
  412. sprintf(buffer, "%s\n", string);
  413. dentry =
  414. hypfs_create_file(sb, dir, name, buffer, S_IFREG | REG_FILE_MODE);
  415. if (IS_ERR(dentry)) {
  416. kfree(buffer);
  417. return ERR_PTR(-ENOMEM);
  418. }
  419. hypfs_add_dentry(dentry);
  420. return dentry;
  421. }
  422. static const struct file_operations hypfs_file_ops = {
  423. .open = hypfs_open,
  424. .release = hypfs_release,
  425. .read = do_sync_read,
  426. .write = do_sync_write,
  427. .aio_read = hypfs_aio_read,
  428. .aio_write = hypfs_aio_write,
  429. };
  430. static struct file_system_type hypfs_type = {
  431. .owner = THIS_MODULE,
  432. .name = "s390_hypfs",
  433. .get_sb = hypfs_get_super,
  434. .kill_sb = hypfs_kill_super
  435. };
  436. static struct super_operations hypfs_s_ops = {
  437. .statfs = simple_statfs,
  438. .drop_inode = hypfs_drop_inode,
  439. .show_options = hypfs_show_options,
  440. };
  441. static struct kobject *s390_kobj;
  442. static int __init hypfs_init(void)
  443. {
  444. int rc;
  445. if (MACHINE_IS_VM) {
  446. if (hypfs_vm_init())
  447. /* no diag 2fc, just exit */
  448. return -ENODATA;
  449. } else {
  450. if (hypfs_diag_init()) {
  451. rc = -ENODATA;
  452. goto fail_diag;
  453. }
  454. }
  455. s390_kobj = kobject_create_and_add("s390", hypervisor_kobj);
  456. if (!s390_kobj) {
  457. rc = -ENOMEM;;
  458. goto fail_sysfs;
  459. }
  460. rc = register_filesystem(&hypfs_type);
  461. if (rc)
  462. goto fail_filesystem;
  463. return 0;
  464. fail_filesystem:
  465. kobject_put(s390_kobj);
  466. fail_sysfs:
  467. if (!MACHINE_IS_VM)
  468. hypfs_diag_exit();
  469. fail_diag:
  470. printk(KERN_ERR "hypfs: Initialization failed with rc = %i.\n", rc);
  471. return rc;
  472. }
  473. static void __exit hypfs_exit(void)
  474. {
  475. if (!MACHINE_IS_VM)
  476. hypfs_diag_exit();
  477. unregister_filesystem(&hypfs_type);
  478. kobject_put(s390_kobj);
  479. }
  480. module_init(hypfs_init)
  481. module_exit(hypfs_exit)
  482. MODULE_LICENSE("GPL");
  483. MODULE_AUTHOR("Michael Holzheu <holzheu@de.ibm.com>");
  484. MODULE_DESCRIPTION("s390 Hypervisor Filesystem");