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