inode.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /*
  2. * arch/s390/hypfs/inode.c
  3. * Hypervisor filesystem for Linux on s390.
  4. *
  5. * Copyright IBM Corp. 2006, 2008
  6. * Author(s): Michael Holzheu <holzheu@de.ibm.com>
  7. */
  8. #define KMSG_COMPONENT "hypfs"
  9. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  10. #include <linux/types.h>
  11. #include <linux/errno.h>
  12. #include <linux/fs.h>
  13. #include <linux/namei.h>
  14. #include <linux/vfs.h>
  15. #include <linux/slab.h>
  16. #include <linux/pagemap.h>
  17. #include <linux/time.h>
  18. #include <linux/parser.h>
  19. #include <linux/sysfs.h>
  20. #include <linux/module.h>
  21. #include <linux/seq_file.h>
  22. #include <linux/mount.h>
  23. #include <asm/ebcdic.h>
  24. #include "hypfs.h"
  25. #define HYPFS_MAGIC 0x687970 /* ASCII 'hyp' */
  26. #define TMP_SIZE 64 /* size of temporary buffers */
  27. static struct dentry *hypfs_create_update_file(struct super_block *sb,
  28. struct dentry *dir);
  29. struct hypfs_sb_info {
  30. uid_t uid; /* uid used for files and dirs */
  31. gid_t gid; /* gid used for files and dirs */
  32. struct dentry *update_file; /* file to trigger update */
  33. time_t last_update; /* last update time in secs since 1970 */
  34. struct mutex lock; /* lock to protect update process */
  35. };
  36. static const struct file_operations hypfs_file_ops;
  37. static struct file_system_type hypfs_type;
  38. static const struct super_operations hypfs_s_ops;
  39. /* start of list of all dentries, which have to be deleted on update */
  40. static struct dentry *hypfs_last_dentry;
  41. static void hypfs_update_update(struct super_block *sb)
  42. {
  43. struct hypfs_sb_info *sb_info = sb->s_fs_info;
  44. struct inode *inode = sb_info->update_file->d_inode;
  45. sb_info->last_update = get_seconds();
  46. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  47. }
  48. /* directory tree removal functions */
  49. static void hypfs_add_dentry(struct dentry *dentry)
  50. {
  51. dentry->d_fsdata = hypfs_last_dentry;
  52. hypfs_last_dentry = dentry;
  53. }
  54. static inline int hypfs_positive(struct dentry *dentry)
  55. {
  56. return dentry->d_inode && !d_unhashed(dentry);
  57. }
  58. static void hypfs_remove(struct dentry *dentry)
  59. {
  60. struct dentry *parent;
  61. parent = dentry->d_parent;
  62. if (!parent || !parent->d_inode)
  63. return;
  64. mutex_lock(&parent->d_inode->i_mutex);
  65. if (hypfs_positive(dentry)) {
  66. if (S_ISDIR(dentry->d_inode->i_mode))
  67. simple_rmdir(parent->d_inode, dentry);
  68. else
  69. simple_unlink(parent->d_inode, dentry);
  70. }
  71. d_delete(dentry);
  72. dput(dentry);
  73. mutex_unlock(&parent->d_inode->i_mutex);
  74. }
  75. static void hypfs_delete_tree(struct dentry *root)
  76. {
  77. while (hypfs_last_dentry) {
  78. struct dentry *next_dentry;
  79. next_dentry = hypfs_last_dentry->d_fsdata;
  80. hypfs_remove(hypfs_last_dentry);
  81. hypfs_last_dentry = next_dentry;
  82. }
  83. }
  84. static struct inode *hypfs_make_inode(struct super_block *sb, int mode)
  85. {
  86. struct inode *ret = new_inode(sb);
  87. if (ret) {
  88. struct hypfs_sb_info *hypfs_info = sb->s_fs_info;
  89. ret->i_mode = mode;
  90. ret->i_uid = hypfs_info->uid;
  91. ret->i_gid = hypfs_info->gid;
  92. ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME;
  93. if (mode & S_IFDIR)
  94. set_nlink(ret, 2);
  95. }
  96. return ret;
  97. }
  98. static void hypfs_evict_inode(struct inode *inode)
  99. {
  100. end_writeback(inode);
  101. kfree(inode->i_private);
  102. }
  103. static int hypfs_open(struct inode *inode, struct file *filp)
  104. {
  105. char *data = filp->f_path.dentry->d_inode->i_private;
  106. struct hypfs_sb_info *fs_info;
  107. if (filp->f_mode & FMODE_WRITE) {
  108. if (!(inode->i_mode & S_IWUGO))
  109. return -EACCES;
  110. }
  111. if (filp->f_mode & FMODE_READ) {
  112. if (!(inode->i_mode & S_IRUGO))
  113. return -EACCES;
  114. }
  115. fs_info = inode->i_sb->s_fs_info;
  116. if(data) {
  117. mutex_lock(&fs_info->lock);
  118. filp->private_data = kstrdup(data, GFP_KERNEL);
  119. if (!filp->private_data) {
  120. mutex_unlock(&fs_info->lock);
  121. return -ENOMEM;
  122. }
  123. mutex_unlock(&fs_info->lock);
  124. }
  125. return nonseekable_open(inode, filp);
  126. }
  127. static ssize_t hypfs_aio_read(struct kiocb *iocb, const struct iovec *iov,
  128. unsigned long nr_segs, loff_t offset)
  129. {
  130. char *data;
  131. ssize_t ret;
  132. struct file *filp = iocb->ki_filp;
  133. /* XXX: temporary */
  134. char __user *buf = iov[0].iov_base;
  135. size_t count = iov[0].iov_len;
  136. if (nr_segs != 1)
  137. return -EINVAL;
  138. data = filp->private_data;
  139. ret = simple_read_from_buffer(buf, count, &offset, data, strlen(data));
  140. if (ret <= 0)
  141. return ret;
  142. iocb->ki_pos += ret;
  143. file_accessed(filp);
  144. return ret;
  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. pr_err("Updating the hypfs tree 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 const 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. pr_err("%s is not a valid mount option\n", str);
  223. return -EINVAL;
  224. }
  225. }
  226. return 0;
  227. }
  228. static int hypfs_show_options(struct seq_file *s, struct vfsmount *mnt)
  229. {
  230. struct hypfs_sb_info *hypfs_info = mnt->mnt_sb->s_fs_info;
  231. seq_printf(s, ",uid=%u", hypfs_info->uid);
  232. seq_printf(s, ",gid=%u", hypfs_info->gid);
  233. return 0;
  234. }
  235. static int hypfs_fill_super(struct super_block *sb, void *data, int silent)
  236. {
  237. struct inode *root_inode;
  238. struct dentry *root_dentry;
  239. int rc = 0;
  240. struct hypfs_sb_info *sbi;
  241. sbi = kzalloc(sizeof(struct hypfs_sb_info), GFP_KERNEL);
  242. if (!sbi)
  243. return -ENOMEM;
  244. mutex_init(&sbi->lock);
  245. sbi->uid = current_uid();
  246. sbi->gid = current_gid();
  247. sb->s_fs_info = sbi;
  248. sb->s_blocksize = PAGE_CACHE_SIZE;
  249. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  250. sb->s_magic = HYPFS_MAGIC;
  251. sb->s_op = &hypfs_s_ops;
  252. if (hypfs_parse_options(data, sb))
  253. return -EINVAL;
  254. root_inode = hypfs_make_inode(sb, S_IFDIR | 0755);
  255. if (!root_inode)
  256. return -ENOMEM;
  257. root_inode->i_op = &simple_dir_inode_operations;
  258. root_inode->i_fop = &simple_dir_operations;
  259. sb->s_root = root_dentry = d_alloc_root(root_inode);
  260. if (!root_dentry) {
  261. iput(root_inode);
  262. return -ENOMEM;
  263. }
  264. if (MACHINE_IS_VM)
  265. rc = hypfs_vm_create_files(sb, root_dentry);
  266. else
  267. rc = hypfs_diag_create_files(sb, root_dentry);
  268. if (rc)
  269. return rc;
  270. sbi->update_file = hypfs_create_update_file(sb, root_dentry);
  271. if (IS_ERR(sbi->update_file))
  272. return PTR_ERR(sbi->update_file);
  273. hypfs_update_update(sb);
  274. pr_info("Hypervisor filesystem mounted\n");
  275. return 0;
  276. }
  277. static struct dentry *hypfs_mount(struct file_system_type *fst, int flags,
  278. const char *devname, void *data)
  279. {
  280. return mount_single(fst, flags, data, hypfs_fill_super);
  281. }
  282. static void hypfs_kill_super(struct super_block *sb)
  283. {
  284. struct hypfs_sb_info *sb_info = sb->s_fs_info;
  285. if (sb->s_root)
  286. hypfs_delete_tree(sb->s_root);
  287. if (sb_info->update_file)
  288. hypfs_remove(sb_info->update_file);
  289. kfree(sb->s_fs_info);
  290. sb->s_fs_info = NULL;
  291. kill_litter_super(sb);
  292. }
  293. static struct dentry *hypfs_create_file(struct super_block *sb,
  294. struct dentry *parent, const char *name,
  295. char *data, mode_t mode)
  296. {
  297. struct dentry *dentry;
  298. struct inode *inode;
  299. mutex_lock(&parent->d_inode->i_mutex);
  300. dentry = lookup_one_len(name, parent, strlen(name));
  301. if (IS_ERR(dentry)) {
  302. dentry = ERR_PTR(-ENOMEM);
  303. goto fail;
  304. }
  305. inode = hypfs_make_inode(sb, mode);
  306. if (!inode) {
  307. dput(dentry);
  308. dentry = ERR_PTR(-ENOMEM);
  309. goto fail;
  310. }
  311. if (mode & S_IFREG) {
  312. inode->i_fop = &hypfs_file_ops;
  313. if (data)
  314. inode->i_size = strlen(data);
  315. else
  316. inode->i_size = 0;
  317. } else if (mode & S_IFDIR) {
  318. inode->i_op = &simple_dir_inode_operations;
  319. inode->i_fop = &simple_dir_operations;
  320. inc_nlink(parent->d_inode);
  321. } else
  322. BUG();
  323. inode->i_private = data;
  324. d_instantiate(dentry, inode);
  325. dget(dentry);
  326. fail:
  327. mutex_unlock(&parent->d_inode->i_mutex);
  328. return dentry;
  329. }
  330. struct dentry *hypfs_mkdir(struct super_block *sb, struct dentry *parent,
  331. const char *name)
  332. {
  333. struct dentry *dentry;
  334. dentry = hypfs_create_file(sb, parent, name, NULL, S_IFDIR | DIR_MODE);
  335. if (IS_ERR(dentry))
  336. return dentry;
  337. hypfs_add_dentry(dentry);
  338. return dentry;
  339. }
  340. static struct dentry *hypfs_create_update_file(struct super_block *sb,
  341. struct dentry *dir)
  342. {
  343. struct dentry *dentry;
  344. dentry = hypfs_create_file(sb, dir, "update", NULL,
  345. S_IFREG | UPDATE_FILE_MODE);
  346. /*
  347. * We do not put the update file on the 'delete' list with
  348. * hypfs_add_dentry(), since it should not be removed when the tree
  349. * is updated.
  350. */
  351. return dentry;
  352. }
  353. struct dentry *hypfs_create_u64(struct super_block *sb, struct dentry *dir,
  354. const char *name, __u64 value)
  355. {
  356. char *buffer;
  357. char tmp[TMP_SIZE];
  358. struct dentry *dentry;
  359. snprintf(tmp, TMP_SIZE, "%llu\n", (unsigned long long int)value);
  360. buffer = kstrdup(tmp, GFP_KERNEL);
  361. if (!buffer)
  362. return ERR_PTR(-ENOMEM);
  363. dentry =
  364. hypfs_create_file(sb, dir, name, buffer, S_IFREG | REG_FILE_MODE);
  365. if (IS_ERR(dentry)) {
  366. kfree(buffer);
  367. return ERR_PTR(-ENOMEM);
  368. }
  369. hypfs_add_dentry(dentry);
  370. return dentry;
  371. }
  372. struct dentry *hypfs_create_str(struct super_block *sb, struct dentry *dir,
  373. const char *name, char *string)
  374. {
  375. char *buffer;
  376. struct dentry *dentry;
  377. buffer = kmalloc(strlen(string) + 2, GFP_KERNEL);
  378. if (!buffer)
  379. return ERR_PTR(-ENOMEM);
  380. sprintf(buffer, "%s\n", string);
  381. dentry =
  382. hypfs_create_file(sb, dir, name, buffer, S_IFREG | REG_FILE_MODE);
  383. if (IS_ERR(dentry)) {
  384. kfree(buffer);
  385. return ERR_PTR(-ENOMEM);
  386. }
  387. hypfs_add_dentry(dentry);
  388. return dentry;
  389. }
  390. static const struct file_operations hypfs_file_ops = {
  391. .open = hypfs_open,
  392. .release = hypfs_release,
  393. .read = do_sync_read,
  394. .write = do_sync_write,
  395. .aio_read = hypfs_aio_read,
  396. .aio_write = hypfs_aio_write,
  397. .llseek = no_llseek,
  398. };
  399. static struct file_system_type hypfs_type = {
  400. .owner = THIS_MODULE,
  401. .name = "s390_hypfs",
  402. .mount = hypfs_mount,
  403. .kill_sb = hypfs_kill_super
  404. };
  405. static const struct super_operations hypfs_s_ops = {
  406. .statfs = simple_statfs,
  407. .evict_inode = hypfs_evict_inode,
  408. .show_options = hypfs_show_options,
  409. };
  410. static struct kobject *s390_kobj;
  411. static int __init hypfs_init(void)
  412. {
  413. int rc;
  414. rc = hypfs_dbfs_init();
  415. if (rc)
  416. return rc;
  417. if (hypfs_diag_init()) {
  418. rc = -ENODATA;
  419. goto fail_dbfs_exit;
  420. }
  421. if (hypfs_vm_init()) {
  422. rc = -ENODATA;
  423. goto fail_hypfs_diag_exit;
  424. }
  425. s390_kobj = kobject_create_and_add("s390", hypervisor_kobj);
  426. if (!s390_kobj) {
  427. rc = -ENOMEM;
  428. goto fail_hypfs_vm_exit;
  429. }
  430. rc = register_filesystem(&hypfs_type);
  431. if (rc)
  432. goto fail_filesystem;
  433. return 0;
  434. fail_filesystem:
  435. kobject_put(s390_kobj);
  436. fail_hypfs_vm_exit:
  437. hypfs_vm_exit();
  438. fail_hypfs_diag_exit:
  439. hypfs_diag_exit();
  440. fail_dbfs_exit:
  441. hypfs_dbfs_exit();
  442. pr_err("Initialization of hypfs failed with rc=%i\n", rc);
  443. return rc;
  444. }
  445. static void __exit hypfs_exit(void)
  446. {
  447. hypfs_diag_exit();
  448. hypfs_vm_exit();
  449. hypfs_dbfs_exit();
  450. unregister_filesystem(&hypfs_type);
  451. kobject_put(s390_kobj);
  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");