Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 2 | * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Licensed under the GPL |
| 4 | */ |
| 5 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | #include <linux/ctype.h> |
| 7 | #include <linux/dcache.h> |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 8 | #include <linux/file.h> |
| 9 | #include <linux/fs.h> |
| 10 | #include <linux/init.h> |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/list.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/mount.h> |
| 15 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/statfs.h> |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 17 | #include <linux/types.h> |
Al Viro | 918377b | 2010-06-06 23:56:02 -0400 | [diff] [blame] | 18 | #include <linux/pid_namespace.h> |
Al Viro | d6b722a | 2011-07-27 22:21:58 -0400 | [diff] [blame] | 19 | #include <linux/namei.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <asm/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include "os.h" |
| 22 | |
Al Viro | f382d6e | 2008-02-23 04:53:53 -0500 | [diff] [blame] | 23 | static struct inode *get_inode(struct super_block *, struct dentry *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
| 25 | struct hppfs_data { |
| 26 | struct list_head list; |
| 27 | char contents[PAGE_SIZE - sizeof(struct list_head)]; |
| 28 | }; |
| 29 | |
| 30 | struct hppfs_private { |
| 31 | struct file *proc_file; |
| 32 | int host_fd; |
| 33 | loff_t len; |
| 34 | struct hppfs_data *contents; |
| 35 | }; |
| 36 | |
| 37 | struct hppfs_inode_info { |
Jeff Dike | a0612b1 | 2008-05-12 14:01:49 -0700 | [diff] [blame] | 38 | struct dentry *proc_dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | struct inode vfs_inode; |
| 40 | }; |
| 41 | |
| 42 | static inline struct hppfs_inode_info *HPPFS_I(struct inode *inode) |
| 43 | { |
Paolo 'Blaisorblade' Giarrusso | fd589e0 | 2005-08-26 16:57:53 +0200 | [diff] [blame] | 44 | return container_of(inode, struct hppfs_inode_info, vfs_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | #define HPPFS_SUPER_MAGIC 0xb00000ee |
| 48 | |
Josef 'Jeff' Sipek | ee9b6d6 | 2007-02-12 00:55:41 -0800 | [diff] [blame] | 49 | static const struct super_operations hppfs_sbops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
| 51 | static int is_pid(struct dentry *dentry) |
| 52 | { |
| 53 | struct super_block *sb; |
| 54 | int i; |
| 55 | |
| 56 | sb = dentry->d_sb; |
Jeff Dike | a0612b1 | 2008-05-12 14:01:49 -0700 | [diff] [blame] | 57 | if (dentry->d_parent != sb->s_root) |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 58 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 60 | for (i = 0; i < dentry->d_name.len; i++) { |
| 61 | if (!isdigit(dentry->d_name.name[i])) |
| 62 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | } |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 64 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | static char *dentry_name(struct dentry *dentry, int extra) |
| 68 | { |
| 69 | struct dentry *parent; |
| 70 | char *root, *name; |
| 71 | const char *seg_name; |
| 72 | int len, seg_len; |
| 73 | |
| 74 | len = 0; |
| 75 | parent = dentry; |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 76 | while (parent->d_parent != parent) { |
| 77 | if (is_pid(parent)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | len += strlen("pid") + 1; |
| 79 | else len += parent->d_name.len + 1; |
| 80 | parent = parent->d_parent; |
| 81 | } |
| 82 | |
| 83 | root = "proc"; |
| 84 | len += strlen(root); |
| 85 | name = kmalloc(len + extra + 1, GFP_KERNEL); |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 86 | if (name == NULL) |
| 87 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
| 89 | name[len] = '\0'; |
| 90 | parent = dentry; |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 91 | while (parent->d_parent != parent) { |
| 92 | if (is_pid(parent)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | seg_name = "pid"; |
| 94 | seg_len = strlen("pid"); |
| 95 | } |
| 96 | else { |
| 97 | seg_name = parent->d_name.name; |
| 98 | seg_len = parent->d_name.len; |
| 99 | } |
| 100 | |
| 101 | len -= seg_len + 1; |
| 102 | name[len] = '/'; |
| 103 | strncpy(&name[len + 1], seg_name, seg_len); |
| 104 | parent = parent->d_parent; |
| 105 | } |
| 106 | strncpy(name, root, strlen(root)); |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 107 | return name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | static int file_removed(struct dentry *dentry, const char *file) |
| 111 | { |
| 112 | char *host_file; |
| 113 | int extra, fd; |
| 114 | |
| 115 | extra = 0; |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 116 | if (file != NULL) |
| 117 | extra += strlen(file) + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | |
| 119 | host_file = dentry_name(dentry, extra + strlen("/remove")); |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 120 | if (host_file == NULL) { |
| 121 | printk(KERN_ERR "file_removed : allocation failed\n"); |
| 122 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 125 | if (file != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | strcat(host_file, "/"); |
| 127 | strcat(host_file, file); |
| 128 | } |
| 129 | strcat(host_file, "/remove"); |
| 130 | |
| 131 | fd = os_open_file(host_file, of_read(OPENFLAGS()), 0); |
| 132 | kfree(host_file); |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 133 | if (fd > 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | os_close_file(fd); |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 135 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | } |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 137 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | static struct dentry *hppfs_lookup(struct inode *ino, struct dentry *dentry, |
Jeff Dike | a0612b1 | 2008-05-12 14:01:49 -0700 | [diff] [blame] | 141 | struct nameidata *nd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | { |
Al Viro | 0916a5e | 2011-07-17 22:27:22 -0400 | [diff] [blame] | 143 | struct dentry *proc_dentry, *parent; |
| 144 | struct qstr *name = &dentry->d_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | struct inode *inode; |
| 146 | int err, deleted; |
| 147 | |
| 148 | deleted = file_removed(dentry, NULL); |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 149 | if (deleted < 0) |
| 150 | return ERR_PTR(deleted); |
| 151 | else if (deleted) |
| 152 | return ERR_PTR(-ENOENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | parent = HPPFS_I(ino)->proc_dentry; |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 155 | mutex_lock(&parent->d_inode->i_mutex); |
Al Viro | 0916a5e | 2011-07-17 22:27:22 -0400 | [diff] [blame] | 156 | proc_dentry = lookup_one_len(name->name, parent, name->len); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 157 | mutex_unlock(&parent->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 159 | if (IS_ERR(proc_dentry)) |
| 160 | return proc_dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | |
Al Viro | f382d6e | 2008-02-23 04:53:53 -0500 | [diff] [blame] | 162 | err = -ENOMEM; |
| 163 | inode = get_inode(ino->i_sb, proc_dentry); |
| 164 | if (!inode) |
Al Viro | 3cc0658 | 2011-07-17 22:24:15 -0400 | [diff] [blame] | 165 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | |
| 167 | d_add(dentry, inode); |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 168 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | out: |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 171 | return ERR_PTR(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 174 | static const struct inode_operations hppfs_file_iops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | }; |
| 176 | |
Al Viro | 347f217 | 2006-03-31 02:30:16 -0800 | [diff] [blame] | 177 | static ssize_t read_proc(struct file *file, char __user *buf, ssize_t count, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | loff_t *ppos, int is_user) |
| 179 | { |
Al Viro | 347f217 | 2006-03-31 02:30:16 -0800 | [diff] [blame] | 180 | ssize_t (*read)(struct file *, char __user *, size_t, loff_t *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | ssize_t n; |
| 182 | |
Josef Sipek | 471b17e | 2006-12-08 02:37:07 -0800 | [diff] [blame] | 183 | read = file->f_path.dentry->d_inode->i_fop->read; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 185 | if (!is_user) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | set_fs(KERNEL_DS); |
| 187 | |
| 188 | n = (*read)(file, buf, count, &file->f_pos); |
| 189 | |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 190 | if (!is_user) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | set_fs(USER_DS); |
| 192 | |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 193 | if (ppos) |
| 194 | *ppos = file->f_pos; |
Paolo 'Blaisorblade' Giarrusso | 8e0a218 | 2005-07-14 00:33:41 -0700 | [diff] [blame] | 195 | return n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Al Viro | 347f217 | 2006-03-31 02:30:16 -0800 | [diff] [blame] | 198 | static ssize_t hppfs_read_file(int fd, char __user *buf, ssize_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | { |
| 200 | ssize_t n; |
| 201 | int cur, err; |
| 202 | char *new_buf; |
| 203 | |
| 204 | n = -ENOMEM; |
| 205 | new_buf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 206 | if (new_buf == NULL) { |
| 207 | printk(KERN_ERR "hppfs_read_file : kmalloc failed\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | goto out; |
| 209 | } |
| 210 | n = 0; |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 211 | while (count > 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | cur = min_t(ssize_t, count, PAGE_SIZE); |
| 213 | err = os_read_file(fd, new_buf, cur); |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 214 | if (err < 0) { |
| 215 | printk(KERN_ERR "hppfs_read : read failed, " |
| 216 | "errno = %d\n", err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | n = err; |
| 218 | goto out_free; |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 219 | } else if (err == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | break; |
| 221 | |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 222 | if (copy_to_user(buf, new_buf, err)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | n = -EFAULT; |
| 224 | goto out_free; |
| 225 | } |
| 226 | n += err; |
| 227 | count -= err; |
| 228 | } |
| 229 | out_free: |
| 230 | kfree(new_buf); |
| 231 | out: |
Paolo 'Blaisorblade' Giarrusso | 8e0a218 | 2005-07-14 00:33:41 -0700 | [diff] [blame] | 232 | return n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Al Viro | 347f217 | 2006-03-31 02:30:16 -0800 | [diff] [blame] | 235 | static ssize_t hppfs_read(struct file *file, char __user *buf, size_t count, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | loff_t *ppos) |
| 237 | { |
| 238 | struct hppfs_private *hppfs = file->private_data; |
| 239 | struct hppfs_data *data; |
| 240 | loff_t off; |
| 241 | int err; |
| 242 | |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 243 | if (hppfs->contents != NULL) { |
Jeff Dike | a0612b1 | 2008-05-12 14:01:49 -0700 | [diff] [blame] | 244 | int rem; |
| 245 | |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 246 | if (*ppos >= hppfs->len) |
| 247 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | |
| 249 | data = hppfs->contents; |
| 250 | off = *ppos; |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 251 | while (off >= sizeof(data->contents)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | data = list_entry(data->list.next, struct hppfs_data, |
| 253 | list); |
| 254 | off -= sizeof(data->contents); |
| 255 | } |
| 256 | |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 257 | if (off + count > hppfs->len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | count = hppfs->len - off; |
Jeff Dike | a0612b1 | 2008-05-12 14:01:49 -0700 | [diff] [blame] | 259 | rem = copy_to_user(buf, &data->contents[off], count); |
| 260 | *ppos += count - rem; |
| 261 | if (rem > 0) |
| 262 | return -EFAULT; |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 263 | } else if (hppfs->host_fd != -1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | err = os_seek_file(hppfs->host_fd, *ppos); |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 265 | if (err) { |
| 266 | printk(KERN_ERR "hppfs_read : seek failed, " |
| 267 | "errno = %d\n", err); |
| 268 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | } |
Roel Kluin | 880fe76 | 2009-04-02 16:57:18 -0700 | [diff] [blame] | 270 | err = hppfs_read_file(hppfs->host_fd, buf, count); |
| 271 | if (err < 0) { |
| 272 | printk(KERN_ERR "hppfs_read: read failed: %d\n", err); |
| 273 | return err; |
| 274 | } |
| 275 | count = err; |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 276 | if (count > 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | *ppos += count; |
| 278 | } |
| 279 | else count = read_proc(hppfs->proc_file, buf, count, ppos, 1); |
| 280 | |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 281 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Jeff Dike | a0612b1 | 2008-05-12 14:01:49 -0700 | [diff] [blame] | 284 | static ssize_t hppfs_write(struct file *file, const char __user *buf, |
| 285 | size_t len, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | { |
| 287 | struct hppfs_private *data = file->private_data; |
| 288 | struct file *proc_file = data->proc_file; |
Al Viro | 347f217 | 2006-03-31 02:30:16 -0800 | [diff] [blame] | 289 | ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | |
Josef Sipek | 471b17e | 2006-12-08 02:37:07 -0800 | [diff] [blame] | 291 | write = proc_file->f_path.dentry->d_inode->i_fop->write; |
Jeff Dike | a0612b1 | 2008-05-12 14:01:49 -0700 | [diff] [blame] | 292 | return (*write)(proc_file, buf, len, ppos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | static int open_host_sock(char *host_file, int *filter_out) |
| 296 | { |
| 297 | char *end; |
| 298 | int fd; |
| 299 | |
| 300 | end = &host_file[strlen(host_file)]; |
| 301 | strcpy(end, "/rw"); |
| 302 | *filter_out = 1; |
| 303 | fd = os_connect_socket(host_file); |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 304 | if (fd > 0) |
| 305 | return fd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | |
| 307 | strcpy(end, "/r"); |
| 308 | *filter_out = 0; |
| 309 | fd = os_connect_socket(host_file); |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 310 | return fd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | static void free_contents(struct hppfs_data *head) |
| 314 | { |
| 315 | struct hppfs_data *data; |
| 316 | struct list_head *ele, *next; |
| 317 | |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 318 | if (head == NULL) |
| 319 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 321 | list_for_each_safe(ele, next, &head->list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | data = list_entry(ele, struct hppfs_data, list); |
| 323 | kfree(data); |
| 324 | } |
| 325 | kfree(head); |
| 326 | } |
| 327 | |
| 328 | static struct hppfs_data *hppfs_get_data(int fd, int filter, |
| 329 | struct file *proc_file, |
| 330 | struct file *hppfs_file, |
| 331 | loff_t *size_out) |
| 332 | { |
| 333 | struct hppfs_data *data, *new, *head; |
| 334 | int n, err; |
| 335 | |
| 336 | err = -ENOMEM; |
| 337 | data = kmalloc(sizeof(*data), GFP_KERNEL); |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 338 | if (data == NULL) { |
| 339 | printk(KERN_ERR "hppfs_get_data : head allocation failed\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | goto failed; |
| 341 | } |
| 342 | |
| 343 | INIT_LIST_HEAD(&data->list); |
| 344 | |
| 345 | head = data; |
| 346 | *size_out = 0; |
| 347 | |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 348 | if (filter) { |
| 349 | while ((n = read_proc(proc_file, data->contents, |
Jeff Dike | a0612b1 | 2008-05-12 14:01:49 -0700 | [diff] [blame] | 350 | sizeof(data->contents), NULL, 0)) > 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | os_write_file(fd, data->contents, n); |
| 352 | err = os_shutdown_socket(fd, 0, 1); |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 353 | if (err) { |
| 354 | printk(KERN_ERR "hppfs_get_data : failed to shut down " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | "socket\n"); |
| 356 | goto failed_free; |
| 357 | } |
| 358 | } |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 359 | while (1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | n = os_read_file(fd, data->contents, sizeof(data->contents)); |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 361 | if (n < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | err = n; |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 363 | printk(KERN_ERR "hppfs_get_data : read failed, " |
| 364 | "errno = %d\n", err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | goto failed_free; |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 366 | } else if (n == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | break; |
| 368 | |
| 369 | *size_out += n; |
| 370 | |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 371 | if (n < sizeof(data->contents)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | break; |
| 373 | |
| 374 | new = kmalloc(sizeof(*data), GFP_KERNEL); |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 375 | if (new == 0) { |
| 376 | printk(KERN_ERR "hppfs_get_data : data allocation " |
| 377 | "failed\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | err = -ENOMEM; |
| 379 | goto failed_free; |
| 380 | } |
| 381 | |
| 382 | INIT_LIST_HEAD(&new->list); |
| 383 | list_add(&new->list, &data->list); |
| 384 | data = new; |
| 385 | } |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 386 | return head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | |
| 388 | failed_free: |
| 389 | free_contents(head); |
| 390 | failed: |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 391 | return ERR_PTR(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | static struct hppfs_private *hppfs_data(void) |
| 395 | { |
| 396 | struct hppfs_private *data; |
| 397 | |
| 398 | data = kmalloc(sizeof(*data), GFP_KERNEL); |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 399 | if (data == NULL) |
| 400 | return data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | |
| 402 | *data = ((struct hppfs_private ) { .host_fd = -1, |
| 403 | .len = -1, |
| 404 | .contents = NULL } ); |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 405 | return data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | static int file_mode(int fmode) |
| 409 | { |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 410 | if (fmode == (FMODE_READ | FMODE_WRITE)) |
| 411 | return O_RDWR; |
| 412 | if (fmode == FMODE_READ) |
| 413 | return O_RDONLY; |
| 414 | if (fmode == FMODE_WRITE) |
| 415 | return O_WRONLY; |
| 416 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | static int hppfs_open(struct inode *inode, struct file *file) |
| 420 | { |
David Howells | d76b0d9 | 2008-11-14 10:39:25 +1100 | [diff] [blame] | 421 | const struct cred *cred = file->f_cred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | struct hppfs_private *data; |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 423 | struct vfsmount *proc_mnt; |
Jeff Dike | a0612b1 | 2008-05-12 14:01:49 -0700 | [diff] [blame] | 424 | struct dentry *proc_dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | char *host_file; |
| 426 | int err, fd, type, filter; |
| 427 | |
| 428 | err = -ENOMEM; |
| 429 | data = hppfs_data(); |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 430 | if (data == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | goto out; |
| 432 | |
Josef Sipek | 471b17e | 2006-12-08 02:37:07 -0800 | [diff] [blame] | 433 | host_file = dentry_name(file->f_path.dentry, strlen("/rw")); |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 434 | if (host_file == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | goto out_free2; |
| 436 | |
| 437 | proc_dentry = HPPFS_I(inode)->proc_dentry; |
Al Viro | f382d6e | 2008-02-23 04:53:53 -0500 | [diff] [blame] | 438 | proc_mnt = inode->i_sb->s_fs_info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | |
| 440 | /* XXX This isn't closed anywhere */ |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 441 | data->proc_file = dentry_open(dget(proc_dentry), mntget(proc_mnt), |
David Howells | 745ca24 | 2008-11-14 10:39:22 +1100 | [diff] [blame] | 442 | file_mode(file->f_mode), cred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | err = PTR_ERR(data->proc_file); |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 444 | if (IS_ERR(data->proc_file)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | goto out_free1; |
| 446 | |
| 447 | type = os_file_type(host_file); |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 448 | if (type == OS_TYPE_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | fd = os_open_file(host_file, of_read(OPENFLAGS()), 0); |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 450 | if (fd >= 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | data->host_fd = fd; |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 452 | else |
| 453 | printk(KERN_ERR "hppfs_open : failed to open '%s', " |
| 454 | "errno = %d\n", host_file, -fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | |
| 456 | data->contents = NULL; |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 457 | } else if (type == OS_TYPE_DIR) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | fd = open_host_sock(host_file, &filter); |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 459 | if (fd > 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | data->contents = hppfs_get_data(fd, filter, |
Paolo 'Blaisorblade' Giarrusso | 3f58047 | 2005-07-07 17:56:51 -0700 | [diff] [blame] | 461 | data->proc_file, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | file, &data->len); |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 463 | if (!IS_ERR(data->contents)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | data->host_fd = fd; |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 465 | } else |
| 466 | printk(KERN_ERR "hppfs_open : failed to open a socket " |
| 467 | "in '%s', errno = %d\n", host_file, -fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | } |
| 469 | kfree(host_file); |
| 470 | |
| 471 | file->private_data = data; |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 472 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | |
| 474 | out_free1: |
| 475 | kfree(host_file); |
| 476 | out_free2: |
| 477 | free_contents(data->contents); |
| 478 | kfree(data); |
| 479 | out: |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 480 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | static int hppfs_dir_open(struct inode *inode, struct file *file) |
| 484 | { |
David Howells | d76b0d9 | 2008-11-14 10:39:25 +1100 | [diff] [blame] | 485 | const struct cred *cred = file->f_cred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | struct hppfs_private *data; |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 487 | struct vfsmount *proc_mnt; |
Jeff Dike | a0612b1 | 2008-05-12 14:01:49 -0700 | [diff] [blame] | 488 | struct dentry *proc_dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | int err; |
| 490 | |
| 491 | err = -ENOMEM; |
| 492 | data = hppfs_data(); |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 493 | if (data == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | goto out; |
| 495 | |
| 496 | proc_dentry = HPPFS_I(inode)->proc_dentry; |
Al Viro | f382d6e | 2008-02-23 04:53:53 -0500 | [diff] [blame] | 497 | proc_mnt = inode->i_sb->s_fs_info; |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 498 | data->proc_file = dentry_open(dget(proc_dentry), mntget(proc_mnt), |
David Howells | 745ca24 | 2008-11-14 10:39:22 +1100 | [diff] [blame] | 499 | file_mode(file->f_mode), cred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | err = PTR_ERR(data->proc_file); |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 501 | if (IS_ERR(data->proc_file)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | goto out_free; |
| 503 | |
| 504 | file->private_data = data; |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 505 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | |
| 507 | out_free: |
| 508 | kfree(data); |
| 509 | out: |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 510 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | static loff_t hppfs_llseek(struct file *file, loff_t off, int where) |
| 514 | { |
| 515 | struct hppfs_private *data = file->private_data; |
Paolo 'Blaisorblade' Giarrusso | 3f58047 | 2005-07-07 17:56:51 -0700 | [diff] [blame] | 516 | struct file *proc_file = data->proc_file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | loff_t (*llseek)(struct file *, loff_t, int); |
| 518 | loff_t ret; |
| 519 | |
Josef Sipek | 471b17e | 2006-12-08 02:37:07 -0800 | [diff] [blame] | 520 | llseek = proc_file->f_path.dentry->d_inode->i_fop->llseek; |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 521 | if (llseek != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | ret = (*llseek)(proc_file, off, where); |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 523 | if (ret < 0) |
| 524 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | } |
| 526 | |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 527 | return default_llseek(file, off, where); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | } |
| 529 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 530 | static const struct file_operations hppfs_file_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | .owner = NULL, |
| 532 | .llseek = hppfs_llseek, |
| 533 | .read = hppfs_read, |
| 534 | .write = hppfs_write, |
| 535 | .open = hppfs_open, |
| 536 | }; |
| 537 | |
| 538 | struct hppfs_dirent { |
| 539 | void *vfs_dirent; |
| 540 | filldir_t filldir; |
| 541 | struct dentry *dentry; |
| 542 | }; |
| 543 | |
| 544 | static int hppfs_filldir(void *d, const char *name, int size, |
Al Viro | c8adf94 | 2006-10-09 20:26:58 +0100 | [diff] [blame] | 545 | loff_t offset, u64 inode, unsigned int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | { |
| 547 | struct hppfs_dirent *dirent = d; |
| 548 | |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 549 | if (file_removed(dirent->dentry, name)) |
| 550 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 552 | return (*dirent->filldir)(dirent->vfs_dirent, name, size, offset, |
| 553 | inode, type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | static int hppfs_readdir(struct file *file, void *ent, filldir_t filldir) |
| 557 | { |
| 558 | struct hppfs_private *data = file->private_data; |
Paolo 'Blaisorblade' Giarrusso | 3f58047 | 2005-07-07 17:56:51 -0700 | [diff] [blame] | 559 | struct file *proc_file = data->proc_file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | int (*readdir)(struct file *, void *, filldir_t); |
| 561 | struct hppfs_dirent dirent = ((struct hppfs_dirent) |
| 562 | { .vfs_dirent = ent, |
| 563 | .filldir = filldir, |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 564 | .dentry = file->f_path.dentry |
| 565 | }); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | int err; |
| 567 | |
Josef Sipek | 471b17e | 2006-12-08 02:37:07 -0800 | [diff] [blame] | 568 | readdir = proc_file->f_path.dentry->d_inode->i_fop->readdir; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | |
| 570 | proc_file->f_pos = file->f_pos; |
| 571 | err = (*readdir)(proc_file, &dirent, hppfs_filldir); |
| 572 | file->f_pos = proc_file->f_pos; |
| 573 | |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 574 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | } |
| 576 | |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 577 | static int hppfs_fsync(struct file *file, loff_t start, loff_t end, |
| 578 | int datasync) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | { |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 580 | return filemap_write_and_wait_range(file->f_mapping, start, end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | } |
| 582 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 583 | static const struct file_operations hppfs_dir_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | .owner = NULL, |
| 585 | .readdir = hppfs_readdir, |
| 586 | .open = hppfs_dir_open, |
| 587 | .fsync = hppfs_fsync, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 588 | .llseek = default_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | }; |
| 590 | |
David Howells | 726c334 | 2006-06-23 02:02:58 -0700 | [diff] [blame] | 591 | static int hppfs_statfs(struct dentry *dentry, struct kstatfs *sf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | { |
| 593 | sf->f_blocks = 0; |
| 594 | sf->f_bfree = 0; |
| 595 | sf->f_bavail = 0; |
| 596 | sf->f_files = 0; |
| 597 | sf->f_ffree = 0; |
| 598 | sf->f_type = HPPFS_SUPER_MAGIC; |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 599 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | static struct inode *hppfs_alloc_inode(struct super_block *sb) |
| 603 | { |
| 604 | struct hppfs_inode_info *hi; |
| 605 | |
| 606 | hi = kmalloc(sizeof(*hi), GFP_KERNEL); |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 607 | if (!hi) |
| 608 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 610 | hi->proc_dentry = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | inode_init_once(&hi->vfs_inode); |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 612 | return &hi->vfs_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | } |
| 614 | |
Al Viro | 33b0daa | 2010-06-07 00:12:50 -0400 | [diff] [blame] | 615 | void hppfs_evict_inode(struct inode *ino) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | { |
Al Viro | 33b0daa | 2010-06-07 00:12:50 -0400 | [diff] [blame] | 617 | end_writeback(ino); |
Jeff Dike | a0612b1 | 2008-05-12 14:01:49 -0700 | [diff] [blame] | 618 | dput(HPPFS_I(ino)->proc_dentry); |
| 619 | mntput(ino->i_sb->s_fs_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | } |
| 621 | |
Nick Piggin | fa0d7e3 | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 622 | static void hppfs_i_callback(struct rcu_head *head) |
| 623 | { |
| 624 | struct inode *inode = container_of(head, struct inode, i_rcu); |
Nick Piggin | fa0d7e3 | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 625 | kfree(HPPFS_I(inode)); |
| 626 | } |
| 627 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | static void hppfs_destroy_inode(struct inode *inode) |
| 629 | { |
Nick Piggin | fa0d7e3 | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 630 | call_rcu(&inode->i_rcu, hppfs_i_callback); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | } |
| 632 | |
Josef 'Jeff' Sipek | ee9b6d6 | 2007-02-12 00:55:41 -0800 | [diff] [blame] | 633 | static const struct super_operations hppfs_sbops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | .alloc_inode = hppfs_alloc_inode, |
| 635 | .destroy_inode = hppfs_destroy_inode, |
Al Viro | 33b0daa | 2010-06-07 00:12:50 -0400 | [diff] [blame] | 636 | .evict_inode = hppfs_evict_inode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | .statfs = hppfs_statfs, |
| 638 | }; |
| 639 | |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 640 | static int hppfs_readlink(struct dentry *dentry, char __user *buffer, |
| 641 | int buflen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | { |
Al Viro | 7b264fc | 2009-12-23 00:43:50 -0500 | [diff] [blame] | 643 | struct dentry *proc_dentry = HPPFS_I(dentry->d_inode)->proc_dentry; |
Jeff Dike | a0612b1 | 2008-05-12 14:01:49 -0700 | [diff] [blame] | 644 | return proc_dentry->d_inode->i_op->readlink(proc_dentry, buffer, |
| 645 | buflen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | } |
| 647 | |
Jeff Dike | a0612b1 | 2008-05-12 14:01:49 -0700 | [diff] [blame] | 648 | static void *hppfs_follow_link(struct dentry *dentry, struct nameidata *nd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | { |
Al Viro | 7b264fc | 2009-12-23 00:43:50 -0500 | [diff] [blame] | 650 | struct dentry *proc_dentry = HPPFS_I(dentry->d_inode)->proc_dentry; |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 651 | |
Jeff Dike | a0612b1 | 2008-05-12 14:01:49 -0700 | [diff] [blame] | 652 | return proc_dentry->d_inode->i_op->follow_link(proc_dentry, nd); |
| 653 | } |
Paolo 'Blaisorblade' Giarrusso | d7a60d5 | 2005-08-26 16:57:44 +0200 | [diff] [blame] | 654 | |
Al Viro | 7b264fc | 2009-12-23 00:43:50 -0500 | [diff] [blame] | 655 | static void hppfs_put_link(struct dentry *dentry, struct nameidata *nd, |
| 656 | void *cookie) |
| 657 | { |
| 658 | struct dentry *proc_dentry = HPPFS_I(dentry->d_inode)->proc_dentry; |
| 659 | |
| 660 | if (proc_dentry->d_inode->i_op->put_link) |
| 661 | proc_dentry->d_inode->i_op->put_link(proc_dentry, nd, cookie); |
| 662 | } |
| 663 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 664 | static const struct inode_operations hppfs_dir_iops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | .lookup = hppfs_lookup, |
| 666 | }; |
| 667 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 668 | static const struct inode_operations hppfs_link_iops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | .readlink = hppfs_readlink, |
| 670 | .follow_link = hppfs_follow_link, |
Al Viro | 7b264fc | 2009-12-23 00:43:50 -0500 | [diff] [blame] | 671 | .put_link = hppfs_put_link, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | }; |
| 673 | |
Al Viro | f382d6e | 2008-02-23 04:53:53 -0500 | [diff] [blame] | 674 | static struct inode *get_inode(struct super_block *sb, struct dentry *dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | { |
Al Viro | f382d6e | 2008-02-23 04:53:53 -0500 | [diff] [blame] | 676 | struct inode *proc_ino = dentry->d_inode; |
| 677 | struct inode *inode = new_inode(sb); |
| 678 | |
Al Viro | 3cc0658 | 2011-07-17 22:24:15 -0400 | [diff] [blame] | 679 | if (!inode) { |
| 680 | dput(dentry); |
Al Viro | f382d6e | 2008-02-23 04:53:53 -0500 | [diff] [blame] | 681 | return ERR_PTR(-ENOMEM); |
Al Viro | 3cc0658 | 2011-07-17 22:24:15 -0400 | [diff] [blame] | 682 | } |
Al Viro | f382d6e | 2008-02-23 04:53:53 -0500 | [diff] [blame] | 683 | |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 684 | if (S_ISDIR(dentry->d_inode->i_mode)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | inode->i_op = &hppfs_dir_iops; |
| 686 | inode->i_fop = &hppfs_dir_fops; |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 687 | } else if (S_ISLNK(dentry->d_inode->i_mode)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | inode->i_op = &hppfs_link_iops; |
| 689 | inode->i_fop = &hppfs_file_fops; |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 690 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | inode->i_op = &hppfs_file_iops; |
| 692 | inode->i_fop = &hppfs_file_fops; |
| 693 | } |
| 694 | |
Al Viro | 3cc0658 | 2011-07-17 22:24:15 -0400 | [diff] [blame] | 695 | HPPFS_I(inode)->proc_dentry = dentry; |
Al Viro | f382d6e | 2008-02-23 04:53:53 -0500 | [diff] [blame] | 696 | |
| 697 | inode->i_uid = proc_ino->i_uid; |
| 698 | inode->i_gid = proc_ino->i_gid; |
| 699 | inode->i_atime = proc_ino->i_atime; |
| 700 | inode->i_mtime = proc_ino->i_mtime; |
| 701 | inode->i_ctime = proc_ino->i_ctime; |
| 702 | inode->i_ino = proc_ino->i_ino; |
| 703 | inode->i_mode = proc_ino->i_mode; |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 704 | set_nlink(inode, proc_ino->i_nlink); |
Al Viro | f382d6e | 2008-02-23 04:53:53 -0500 | [diff] [blame] | 705 | inode->i_size = proc_ino->i_size; |
| 706 | inode->i_blocks = proc_ino->i_blocks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | |
Jeff Dike | a0612b1 | 2008-05-12 14:01:49 -0700 | [diff] [blame] | 708 | return inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | static int hppfs_fill_super(struct super_block *sb, void *d, int silent) |
| 712 | { |
| 713 | struct inode *root_inode; |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 714 | struct vfsmount *proc_mnt; |
Al Viro | f382d6e | 2008-02-23 04:53:53 -0500 | [diff] [blame] | 715 | int err = -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | |
Al Viro | 0ceeca5 | 2010-02-05 09:34:36 -0500 | [diff] [blame] | 717 | proc_mnt = mntget(current->nsproxy->pid_ns->proc_mnt); |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 718 | if (IS_ERR(proc_mnt)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | goto out; |
| 720 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | sb->s_blocksize = 1024; |
| 722 | sb->s_blocksize_bits = 10; |
| 723 | sb->s_magic = HPPFS_SUPER_MAGIC; |
| 724 | sb->s_op = &hppfs_sbops; |
Al Viro | f382d6e | 2008-02-23 04:53:53 -0500 | [diff] [blame] | 725 | sb->s_fs_info = proc_mnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | |
| 727 | err = -ENOMEM; |
Al Viro | 4c1d5a6 | 2011-12-07 18:21:57 -0500 | [diff] [blame] | 728 | root_inode = get_inode(sb, dget(proc_mnt->mnt_root)); |
Al Viro | f382d6e | 2008-02-23 04:53:53 -0500 | [diff] [blame] | 729 | if (!root_inode) |
| 730 | goto out_mntput; |
| 731 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | sb->s_root = d_alloc_root(root_inode); |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 733 | if (!sb->s_root) |
| 734 | goto out_iput; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 736 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 738 | out_iput: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | iput(root_inode); |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 740 | out_mntput: |
| 741 | mntput(proc_mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | out: |
| 743 | return(err); |
| 744 | } |
| 745 | |
Al Viro | 3c26ff6 | 2010-07-25 11:46:36 +0400 | [diff] [blame] | 746 | static struct dentry *hppfs_read_super(struct file_system_type *type, |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 747 | int flags, const char *dev_name, |
Al Viro | 3c26ff6 | 2010-07-25 11:46:36 +0400 | [diff] [blame] | 748 | void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | { |
Al Viro | 3c26ff6 | 2010-07-25 11:46:36 +0400 | [diff] [blame] | 750 | return mount_nodev(type, flags, data, hppfs_fill_super); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | static struct file_system_type hppfs_type = { |
| 754 | .owner = THIS_MODULE, |
| 755 | .name = "hppfs", |
Al Viro | 3c26ff6 | 2010-07-25 11:46:36 +0400 | [diff] [blame] | 756 | .mount = hppfs_read_super, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | .kill_sb = kill_anon_super, |
| 758 | .fs_flags = 0, |
| 759 | }; |
| 760 | |
| 761 | static int __init init_hppfs(void) |
| 762 | { |
Dave Hansen | 1dd0dd1 | 2008-02-15 18:37:00 -0500 | [diff] [blame] | 763 | return register_filesystem(&hppfs_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | static void __exit exit_hppfs(void) |
| 767 | { |
| 768 | unregister_filesystem(&hppfs_type); |
| 769 | } |
| 770 | |
| 771 | module_init(init_hppfs) |
| 772 | module_exit(exit_hppfs) |
| 773 | MODULE_LICENSE("GPL"); |