Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 2 | * Copyright (C) 2000 - 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 | * Ported the filesystem routines to 2.5. |
| 6 | * 2003-02-10 Petr Baudis <pasky@ucw.cz> |
| 7 | */ |
| 8 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/fs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/module.h> |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 11 | #include <linux/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/pagemap.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/statfs.h> |
Miklos Szeredi | dd2cc4d | 2008-02-08 04:21:43 -0800 | [diff] [blame^] | 14 | #include <linux/seq_file.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include "hostfs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include "init.h" |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 17 | #include "kern.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
| 19 | struct hostfs_inode_info { |
| 20 | char *host_filename; |
| 21 | int fd; |
| 22 | int mode; |
| 23 | struct inode vfs_inode; |
| 24 | }; |
| 25 | |
| 26 | static inline struct hostfs_inode_info *HOSTFS_I(struct inode *inode) |
| 27 | { |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 28 | return list_entry(inode, struct hostfs_inode_info, vfs_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | } |
| 30 | |
Josef Sipek | 680b0da | 2006-12-08 02:37:05 -0800 | [diff] [blame] | 31 | #define FILE_HOSTFS_I(file) HOSTFS_I((file)->f_path.dentry->d_inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
| 33 | int hostfs_d_delete(struct dentry *dentry) |
| 34 | { |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 35 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | struct dentry_operations hostfs_dentry_ops = { |
| 39 | .d_delete = hostfs_d_delete, |
| 40 | }; |
| 41 | |
| 42 | /* Changed in hostfs_args before the kernel starts running */ |
Paolo 'Blaisorblade' Giarrusso | a6eb0be | 2007-03-07 20:41:08 -0800 | [diff] [blame] | 43 | static char *root_ino = ""; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | static int append = 0; |
| 45 | |
| 46 | #define HOSTFS_SUPER_MAGIC 0x00c0ffee |
| 47 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 48 | static const struct inode_operations hostfs_iops; |
| 49 | static const struct inode_operations hostfs_dir_iops; |
Christoph Hellwig | f5e54d6 | 2006-06-28 04:26:44 -0700 | [diff] [blame] | 50 | static const struct address_space_operations hostfs_link_aops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
| 52 | #ifndef MODULE |
| 53 | static int __init hostfs_args(char *options, int *add) |
| 54 | { |
| 55 | char *ptr; |
| 56 | |
| 57 | ptr = strchr(options, ','); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 58 | if (ptr != NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | *ptr++ = '\0'; |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 60 | if (*options != '\0') |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | root_ino = options; |
| 62 | |
| 63 | options = ptr; |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 64 | while (options) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | ptr = strchr(options, ','); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 66 | if (ptr != NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | *ptr++ = '\0'; |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 68 | if (*options != '\0') { |
| 69 | if (!strcmp(options, "append")) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | append = 1; |
| 71 | else printf("hostfs_args - unsupported option - %s\n", |
| 72 | options); |
| 73 | } |
| 74 | options = ptr; |
| 75 | } |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 76 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | __uml_setup("hostfs=", hostfs_args, |
| 80 | "hostfs=<root dir>,<flags>,...\n" |
| 81 | " This is used to set hostfs parameters. The root directory argument\n" |
| 82 | " is used to confine all hostfs mounts to within the specified directory\n" |
| 83 | " tree on the host. If this isn't specified, then a user inside UML can\n" |
| 84 | " mount anything on the host that's accessible to the user that's running\n" |
| 85 | " it.\n" |
| 86 | " The only flag currently supported is 'append', which specifies that all\n" |
| 87 | " files opened by hostfs will be opened in append mode.\n\n" |
| 88 | ); |
| 89 | #endif |
| 90 | |
| 91 | static char *dentry_name(struct dentry *dentry, int extra) |
| 92 | { |
| 93 | struct dentry *parent; |
| 94 | char *root, *name; |
| 95 | int len; |
| 96 | |
| 97 | len = 0; |
| 98 | parent = dentry; |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 99 | while (parent->d_parent != parent) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | len += parent->d_name.len + 1; |
| 101 | parent = parent->d_parent; |
| 102 | } |
| 103 | |
| 104 | root = HOSTFS_I(parent->d_inode)->host_filename; |
| 105 | len += strlen(root); |
| 106 | name = kmalloc(len + extra + 1, GFP_KERNEL); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 107 | if (name == NULL) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 108 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
| 110 | name[len] = '\0'; |
| 111 | parent = dentry; |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 112 | while (parent->d_parent != parent) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | len -= parent->d_name.len + 1; |
| 114 | name[len] = '/'; |
| 115 | strncpy(&name[len + 1], parent->d_name.name, |
| 116 | parent->d_name.len); |
| 117 | parent = parent->d_parent; |
| 118 | } |
| 119 | strncpy(name, root, strlen(root)); |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 120 | return name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | static char *inode_name(struct inode *ino, int extra) |
| 124 | { |
| 125 | struct dentry *dentry; |
| 126 | |
| 127 | dentry = list_entry(ino->i_dentry.next, struct dentry, d_alias); |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 128 | return dentry_name(dentry, extra); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | static int read_name(struct inode *ino, char *name) |
| 132 | { |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 133 | /* |
| 134 | * The non-int inode fields are copied into ints by stat_file and |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | * then copied into the inode because passing the actual pointers |
| 136 | * in and having them treated as int * breaks on big-endian machines |
| 137 | */ |
| 138 | int err; |
| 139 | int i_mode, i_nlink, i_blksize; |
| 140 | unsigned long long i_size; |
| 141 | unsigned long long i_ino; |
| 142 | unsigned long long i_blocks; |
| 143 | |
| 144 | err = stat_file(name, &i_ino, &i_mode, &i_nlink, &ino->i_uid, |
| 145 | &ino->i_gid, &i_size, &ino->i_atime, &ino->i_mtime, |
Alberto Bertogli | 5822b7f | 2007-05-08 00:23:16 -0700 | [diff] [blame] | 146 | &ino->i_ctime, &i_blksize, &i_blocks, -1); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 147 | if (err) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 148 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | |
| 150 | ino->i_ino = i_ino; |
| 151 | ino->i_mode = i_mode; |
| 152 | ino->i_nlink = i_nlink; |
| 153 | ino->i_size = i_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | ino->i_blocks = i_blocks; |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 155 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | static char *follow_link(char *link) |
| 159 | { |
| 160 | int len, n; |
| 161 | char *name, *resolved, *end; |
| 162 | |
| 163 | len = 64; |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 164 | while (1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | n = -ENOMEM; |
| 166 | name = kmalloc(len, GFP_KERNEL); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 167 | if (name == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | goto out; |
| 169 | |
| 170 | n = do_readlink(link, name, len); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 171 | if (n < len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | break; |
| 173 | len *= 2; |
| 174 | kfree(name); |
| 175 | } |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 176 | if (n < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | goto out_free; |
| 178 | |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 179 | if (*name == '/') |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 180 | return name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | |
| 182 | end = strrchr(link, '/'); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 183 | if (end == NULL) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 184 | return name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | |
| 186 | *(end + 1) = '\0'; |
| 187 | len = strlen(link) + strlen(name) + 1; |
| 188 | |
| 189 | resolved = kmalloc(len, GFP_KERNEL); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 190 | if (resolved == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | n = -ENOMEM; |
| 192 | goto out_free; |
| 193 | } |
| 194 | |
| 195 | sprintf(resolved, "%s%s", link, name); |
| 196 | kfree(name); |
| 197 | kfree(link); |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 198 | return resolved; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | |
| 200 | out_free: |
| 201 | kfree(name); |
| 202 | out: |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 203 | return ERR_PTR(n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | } |
| 205 | |
David Howells | 0a370e5 | 2008-02-07 00:15:50 -0800 | [diff] [blame] | 206 | static int hostfs_read_inode(struct inode *ino) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | { |
| 208 | char *name; |
| 209 | int err = 0; |
| 210 | |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 211 | /* |
| 212 | * Unfortunately, we are called from iget() when we don't have a dentry |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | * allocated yet. |
| 214 | */ |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 215 | if (list_empty(&ino->i_dentry)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | goto out; |
| 217 | |
| 218 | err = -ENOMEM; |
| 219 | name = inode_name(ino, 0); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 220 | if (name == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | goto out; |
| 222 | |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 223 | if (file_type(name, NULL, NULL) == OS_TYPE_SYMLINK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | name = follow_link(name); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 225 | if (IS_ERR(name)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | err = PTR_ERR(name); |
| 227 | goto out; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | err = read_name(ino, name); |
| 232 | kfree(name); |
| 233 | out: |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 234 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | } |
| 236 | |
David Howells | 0a370e5 | 2008-02-07 00:15:50 -0800 | [diff] [blame] | 237 | static struct inode *hostfs_iget(struct super_block *sb) |
| 238 | { |
| 239 | struct inode *inode; |
| 240 | long ret; |
| 241 | |
| 242 | inode = iget_locked(sb, 0); |
| 243 | if (!inode) |
| 244 | return ERR_PTR(-ENOMEM); |
| 245 | if (inode->i_state & I_NEW) { |
| 246 | ret = hostfs_read_inode(inode); |
| 247 | if (ret < 0) { |
| 248 | iget_failed(inode); |
| 249 | return ERR_PTR(ret); |
| 250 | } |
| 251 | unlock_new_inode(inode); |
| 252 | } |
| 253 | return inode; |
| 254 | } |
| 255 | |
David Howells | 726c334 | 2006-06-23 02:02:58 -0700 | [diff] [blame] | 256 | int hostfs_statfs(struct dentry *dentry, struct kstatfs *sf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | { |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 258 | /* |
| 259 | * do_statfs uses struct statfs64 internally, but the linux kernel |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | * struct statfs still has 32-bit versions for most of these fields, |
| 261 | * so we convert them here |
| 262 | */ |
| 263 | int err; |
| 264 | long long f_blocks; |
| 265 | long long f_bfree; |
| 266 | long long f_bavail; |
| 267 | long long f_files; |
| 268 | long long f_ffree; |
| 269 | |
David Howells | 726c334 | 2006-06-23 02:02:58 -0700 | [diff] [blame] | 270 | err = do_statfs(HOSTFS_I(dentry->d_sb->s_root->d_inode)->host_filename, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | &sf->f_bsize, &f_blocks, &f_bfree, &f_bavail, &f_files, |
| 272 | &f_ffree, &sf->f_fsid, sizeof(sf->f_fsid), |
| 273 | &sf->f_namelen, sf->f_spare); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 274 | if (err) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 275 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | sf->f_blocks = f_blocks; |
| 277 | sf->f_bfree = f_bfree; |
| 278 | sf->f_bavail = f_bavail; |
| 279 | sf->f_files = f_files; |
| 280 | sf->f_ffree = f_ffree; |
| 281 | sf->f_type = HOSTFS_SUPER_MAGIC; |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 282 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | static struct inode *hostfs_alloc_inode(struct super_block *sb) |
| 286 | { |
| 287 | struct hostfs_inode_info *hi; |
| 288 | |
| 289 | hi = kmalloc(sizeof(*hi), GFP_KERNEL); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 290 | if (hi == NULL) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 291 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | |
| 293 | *hi = ((struct hostfs_inode_info) { .host_filename = NULL, |
| 294 | .fd = -1, |
| 295 | .mode = 0 }); |
| 296 | inode_init_once(&hi->vfs_inode); |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 297 | return &hi->vfs_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | static void hostfs_delete_inode(struct inode *inode) |
| 301 | { |
Mark Fasheh | fef2665 | 2005-09-09 13:01:31 -0700 | [diff] [blame] | 302 | truncate_inode_pages(&inode->i_data, 0); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 303 | if (HOSTFS_I(inode)->fd != -1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | close_file(&HOSTFS_I(inode)->fd); |
| 305 | HOSTFS_I(inode)->fd = -1; |
| 306 | } |
| 307 | clear_inode(inode); |
| 308 | } |
| 309 | |
| 310 | static void hostfs_destroy_inode(struct inode *inode) |
| 311 | { |
Jesper Juhl | f99d49a | 2005-11-07 01:01:34 -0800 | [diff] [blame] | 312 | kfree(HOSTFS_I(inode)->host_filename); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 314 | /* |
| 315 | * XXX: This should not happen, probably. The check is here for |
| 316 | * additional safety. |
| 317 | */ |
| 318 | if (HOSTFS_I(inode)->fd != -1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | close_file(&HOSTFS_I(inode)->fd); |
| 320 | printk(KERN_DEBUG "Closing host fd in .destroy_inode\n"); |
| 321 | } |
| 322 | |
| 323 | kfree(HOSTFS_I(inode)); |
| 324 | } |
| 325 | |
Miklos Szeredi | dd2cc4d | 2008-02-08 04:21:43 -0800 | [diff] [blame^] | 326 | static int hostfs_show_options(struct seq_file *seq, struct vfsmount *vfs) |
| 327 | { |
| 328 | struct inode *root = vfs->mnt_sb->s_root->d_inode; |
| 329 | const char *root_path = HOSTFS_I(root)->host_filename; |
| 330 | size_t offset = strlen(root_ino) + 1; |
| 331 | |
| 332 | if (strlen(root_path) > offset) |
| 333 | seq_printf(seq, ",%s", root_path + offset); |
| 334 | |
| 335 | return 0; |
| 336 | } |
| 337 | |
Josef 'Jeff' Sipek | ee9b6d6 | 2007-02-12 00:55:41 -0800 | [diff] [blame] | 338 | static const struct super_operations hostfs_sbops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | .alloc_inode = hostfs_alloc_inode, |
| 340 | .drop_inode = generic_delete_inode, |
| 341 | .delete_inode = hostfs_delete_inode, |
| 342 | .destroy_inode = hostfs_destroy_inode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | .statfs = hostfs_statfs, |
Miklos Szeredi | dd2cc4d | 2008-02-08 04:21:43 -0800 | [diff] [blame^] | 344 | .show_options = hostfs_show_options, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | }; |
| 346 | |
| 347 | int hostfs_readdir(struct file *file, void *ent, filldir_t filldir) |
| 348 | { |
| 349 | void *dir; |
| 350 | char *name; |
| 351 | unsigned long long next, ino; |
| 352 | int error, len; |
| 353 | |
Josef Sipek | 680b0da | 2006-12-08 02:37:05 -0800 | [diff] [blame] | 354 | name = dentry_name(file->f_path.dentry, 0); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 355 | if (name == NULL) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 356 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | dir = open_dir(name, &error); |
| 358 | kfree(name); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 359 | if (dir == NULL) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 360 | return -error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | next = file->f_pos; |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 362 | while ((name = read_dir(dir, &next, &ino, &len)) != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | error = (*filldir)(ent, name, len, file->f_pos, |
| 364 | ino, DT_UNKNOWN); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 365 | if (error) break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | file->f_pos = next; |
| 367 | } |
| 368 | close_dir(dir); |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 369 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | int hostfs_file_open(struct inode *ino, struct file *file) |
| 373 | { |
| 374 | char *name; |
| 375 | int mode = 0, r = 0, w = 0, fd; |
| 376 | |
| 377 | mode = file->f_mode & (FMODE_READ | FMODE_WRITE); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 378 | if ((mode & HOSTFS_I(ino)->mode) == mode) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 379 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 381 | /* |
| 382 | * The file may already have been opened, but with the wrong access, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | * so this resets things and reopens the file with the new access. |
| 384 | */ |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 385 | if (HOSTFS_I(ino)->fd != -1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | close_file(&HOSTFS_I(ino)->fd); |
| 387 | HOSTFS_I(ino)->fd = -1; |
| 388 | } |
| 389 | |
| 390 | HOSTFS_I(ino)->mode |= mode; |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 391 | if (HOSTFS_I(ino)->mode & FMODE_READ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | r = 1; |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 393 | if (HOSTFS_I(ino)->mode & FMODE_WRITE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | w = 1; |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 395 | if (w) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | r = 1; |
| 397 | |
Josef Sipek | 680b0da | 2006-12-08 02:37:05 -0800 | [diff] [blame] | 398 | name = dentry_name(file->f_path.dentry, 0); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 399 | if (name == NULL) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 400 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | |
| 402 | fd = open_file(name, r, w, append); |
| 403 | kfree(name); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 404 | if (fd < 0) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 405 | return fd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | FILE_HOSTFS_I(file)->fd = fd; |
| 407 | |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 408 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | int hostfs_fsync(struct file *file, struct dentry *dentry, int datasync) |
| 412 | { |
Paolo 'Blaisorblade' Giarrusso | a2d76bd | 2005-07-28 21:16:15 -0700 | [diff] [blame] | 413 | return fsync_file(HOSTFS_I(dentry->d_inode)->fd, datasync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | } |
| 415 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 416 | static const struct file_operations hostfs_file_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | .llseek = generic_file_llseek, |
Badari Pulavarty | 543ade1 | 2006-09-30 23:28:48 -0700 | [diff] [blame] | 418 | .read = do_sync_read, |
Jens Axboe | 5ffc4ef | 2007-06-01 11:49:19 +0200 | [diff] [blame] | 419 | .splice_read = generic_file_splice_read, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | .aio_read = generic_file_aio_read, |
| 421 | .aio_write = generic_file_aio_write, |
Badari Pulavarty | 543ade1 | 2006-09-30 23:28:48 -0700 | [diff] [blame] | 422 | .write = do_sync_write, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | .mmap = generic_file_mmap, |
| 424 | .open = hostfs_file_open, |
| 425 | .release = NULL, |
| 426 | .fsync = hostfs_fsync, |
| 427 | }; |
| 428 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 429 | static const struct file_operations hostfs_dir_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | .llseek = generic_file_llseek, |
| 431 | .readdir = hostfs_readdir, |
| 432 | .read = generic_read_dir, |
| 433 | }; |
| 434 | |
| 435 | int hostfs_writepage(struct page *page, struct writeback_control *wbc) |
| 436 | { |
| 437 | struct address_space *mapping = page->mapping; |
| 438 | struct inode *inode = mapping->host; |
| 439 | char *buffer; |
| 440 | unsigned long long base; |
| 441 | int count = PAGE_CACHE_SIZE; |
| 442 | int end_index = inode->i_size >> PAGE_CACHE_SHIFT; |
| 443 | int err; |
| 444 | |
| 445 | if (page->index >= end_index) |
| 446 | count = inode->i_size & (PAGE_CACHE_SIZE-1); |
| 447 | |
| 448 | buffer = kmap(page); |
| 449 | base = ((unsigned long long) page->index) << PAGE_CACHE_SHIFT; |
| 450 | |
| 451 | err = write_file(HOSTFS_I(inode)->fd, &base, buffer, count); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 452 | if (err != count) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | ClearPageUptodate(page); |
| 454 | goto out; |
| 455 | } |
| 456 | |
| 457 | if (base > inode->i_size) |
| 458 | inode->i_size = base; |
| 459 | |
| 460 | if (PageError(page)) |
| 461 | ClearPageError(page); |
| 462 | err = 0; |
| 463 | |
| 464 | out: |
| 465 | kunmap(page); |
| 466 | |
| 467 | unlock_page(page); |
| 468 | return err; |
| 469 | } |
| 470 | |
| 471 | int hostfs_readpage(struct file *file, struct page *page) |
| 472 | { |
| 473 | char *buffer; |
| 474 | long long start; |
| 475 | int err = 0; |
| 476 | |
| 477 | start = (long long) page->index << PAGE_CACHE_SHIFT; |
| 478 | buffer = kmap(page); |
| 479 | err = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer, |
| 480 | PAGE_CACHE_SIZE); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 481 | if (err < 0) |
| 482 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | |
| 484 | memset(&buffer[err], 0, PAGE_CACHE_SIZE - err); |
| 485 | |
| 486 | flush_dcache_page(page); |
| 487 | SetPageUptodate(page); |
| 488 | if (PageError(page)) ClearPageError(page); |
| 489 | err = 0; |
| 490 | out: |
| 491 | kunmap(page); |
| 492 | unlock_page(page); |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 493 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | } |
| 495 | |
Nick Piggin | ae361ff | 2007-10-16 01:25:17 -0700 | [diff] [blame] | 496 | int hostfs_write_begin(struct file *file, struct address_space *mapping, |
| 497 | loff_t pos, unsigned len, unsigned flags, |
| 498 | struct page **pagep, void **fsdata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | { |
Nick Piggin | ae361ff | 2007-10-16 01:25:17 -0700 | [diff] [blame] | 500 | pgoff_t index = pos >> PAGE_CACHE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | |
Nick Piggin | ae361ff | 2007-10-16 01:25:17 -0700 | [diff] [blame] | 502 | *pagep = __grab_cache_page(mapping, index); |
| 503 | if (!*pagep) |
| 504 | return -ENOMEM; |
| 505 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | } |
| 507 | |
Nick Piggin | ae361ff | 2007-10-16 01:25:17 -0700 | [diff] [blame] | 508 | int hostfs_write_end(struct file *file, struct address_space *mapping, |
| 509 | loff_t pos, unsigned len, unsigned copied, |
| 510 | struct page *page, void *fsdata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | struct inode *inode = mapping->host; |
Nick Piggin | ae361ff | 2007-10-16 01:25:17 -0700 | [diff] [blame] | 513 | void *buffer; |
| 514 | unsigned from = pos & (PAGE_CACHE_SIZE - 1); |
| 515 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | buffer = kmap(page); |
Nick Piggin | ae361ff | 2007-10-16 01:25:17 -0700 | [diff] [blame] | 518 | err = write_file(FILE_HOSTFS_I(file)->fd, &pos, buffer + from, copied); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | kunmap(page); |
Nick Piggin | ae361ff | 2007-10-16 01:25:17 -0700 | [diff] [blame] | 520 | |
| 521 | if (!PageUptodate(page) && err == PAGE_CACHE_SIZE) |
| 522 | SetPageUptodate(page); |
| 523 | |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 524 | /* |
| 525 | * If err > 0, write_file has added err to pos, so we are comparing |
Nick Piggin | ae361ff | 2007-10-16 01:25:17 -0700 | [diff] [blame] | 526 | * i_size against the last byte written. |
| 527 | */ |
| 528 | if (err > 0 && (pos > inode->i_size)) |
| 529 | inode->i_size = pos; |
| 530 | unlock_page(page); |
| 531 | page_cache_release(page); |
| 532 | |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 533 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | } |
| 535 | |
Christoph Hellwig | f5e54d6 | 2006-06-28 04:26:44 -0700 | [diff] [blame] | 536 | static const struct address_space_operations hostfs_aops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | .writepage = hostfs_writepage, |
| 538 | .readpage = hostfs_readpage, |
Paolo 'Blaisorblade' Giarrusso | ffa0aea | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 539 | .set_page_dirty = __set_page_dirty_nobuffers, |
Nick Piggin | ae361ff | 2007-10-16 01:25:17 -0700 | [diff] [blame] | 540 | .write_begin = hostfs_write_begin, |
| 541 | .write_end = hostfs_write_end, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | }; |
| 543 | |
| 544 | static int init_inode(struct inode *inode, struct dentry *dentry) |
| 545 | { |
| 546 | char *name; |
| 547 | int type, err = -ENOMEM; |
| 548 | int maj, min; |
| 549 | dev_t rdev = 0; |
| 550 | |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 551 | if (dentry) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | name = dentry_name(dentry, 0); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 553 | if (name == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | goto out; |
| 555 | type = file_type(name, &maj, &min); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 556 | /* Reencode maj and min with the kernel encoding.*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | rdev = MKDEV(maj, min); |
| 558 | kfree(name); |
| 559 | } |
| 560 | else type = OS_TYPE_DIR; |
| 561 | |
| 562 | err = 0; |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 563 | if (type == OS_TYPE_SYMLINK) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | inode->i_op = &page_symlink_inode_operations; |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 565 | else if (type == OS_TYPE_DIR) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | inode->i_op = &hostfs_dir_iops; |
| 567 | else inode->i_op = &hostfs_iops; |
| 568 | |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 569 | if (type == OS_TYPE_DIR) inode->i_fop = &hostfs_dir_fops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | else inode->i_fop = &hostfs_file_fops; |
| 571 | |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 572 | if (type == OS_TYPE_SYMLINK) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | inode->i_mapping->a_ops = &hostfs_link_aops; |
| 574 | else inode->i_mapping->a_ops = &hostfs_aops; |
| 575 | |
| 576 | switch (type) { |
| 577 | case OS_TYPE_CHARDEV: |
| 578 | init_special_inode(inode, S_IFCHR, rdev); |
| 579 | break; |
| 580 | case OS_TYPE_BLOCKDEV: |
| 581 | init_special_inode(inode, S_IFBLK, rdev); |
| 582 | break; |
| 583 | case OS_TYPE_FIFO: |
| 584 | init_special_inode(inode, S_IFIFO, 0); |
| 585 | break; |
| 586 | case OS_TYPE_SOCK: |
| 587 | init_special_inode(inode, S_IFSOCK, 0); |
| 588 | break; |
| 589 | } |
| 590 | out: |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 591 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | int hostfs_create(struct inode *dir, struct dentry *dentry, int mode, |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 595 | struct nameidata *nd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | { |
| 597 | struct inode *inode; |
| 598 | char *name; |
| 599 | int error, fd; |
| 600 | |
David Howells | 0a370e5 | 2008-02-07 00:15:50 -0800 | [diff] [blame] | 601 | inode = hostfs_iget(dir->i_sb); |
| 602 | if (IS_ERR(inode)) { |
| 603 | error = PTR_ERR(inode); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 604 | goto out; |
David Howells | 0a370e5 | 2008-02-07 00:15:50 -0800 | [diff] [blame] | 605 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | |
| 607 | error = init_inode(inode, dentry); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 608 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | goto out_put; |
| 610 | |
| 611 | error = -ENOMEM; |
| 612 | name = dentry_name(dentry, 0); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 613 | if (name == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | goto out_put; |
| 615 | |
| 616 | fd = file_create(name, |
| 617 | mode & S_IRUSR, mode & S_IWUSR, mode & S_IXUSR, |
| 618 | mode & S_IRGRP, mode & S_IWGRP, mode & S_IXGRP, |
| 619 | mode & S_IROTH, mode & S_IWOTH, mode & S_IXOTH); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 620 | if (fd < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | error = fd; |
| 622 | else error = read_name(inode, name); |
| 623 | |
| 624 | kfree(name); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 625 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | goto out_put; |
| 627 | |
| 628 | HOSTFS_I(inode)->fd = fd; |
| 629 | HOSTFS_I(inode)->mode = FMODE_READ | FMODE_WRITE; |
| 630 | d_instantiate(dentry, inode); |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 631 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | |
| 633 | out_put: |
| 634 | iput(inode); |
| 635 | out: |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 636 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | struct dentry *hostfs_lookup(struct inode *ino, struct dentry *dentry, |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 640 | struct nameidata *nd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | { |
| 642 | struct inode *inode; |
| 643 | char *name; |
| 644 | int err; |
| 645 | |
David Howells | 0a370e5 | 2008-02-07 00:15:50 -0800 | [diff] [blame] | 646 | inode = hostfs_iget(ino->i_sb); |
| 647 | if (IS_ERR(inode)) { |
| 648 | err = PTR_ERR(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | goto out; |
David Howells | 0a370e5 | 2008-02-07 00:15:50 -0800 | [diff] [blame] | 650 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | |
| 652 | err = init_inode(inode, dentry); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 653 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | goto out_put; |
| 655 | |
| 656 | err = -ENOMEM; |
| 657 | name = dentry_name(dentry, 0); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 658 | if (name == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | goto out_put; |
| 660 | |
| 661 | err = read_name(inode, name); |
| 662 | kfree(name); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 663 | if (err == -ENOENT) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | iput(inode); |
| 665 | inode = NULL; |
| 666 | } |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 667 | else if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | goto out_put; |
| 669 | |
| 670 | d_add(dentry, inode); |
| 671 | dentry->d_op = &hostfs_dentry_ops; |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 672 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | |
| 674 | out_put: |
| 675 | iput(inode); |
| 676 | out: |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 677 | return ERR_PTR(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | static char *inode_dentry_name(struct inode *ino, struct dentry *dentry) |
| 681 | { |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 682 | char *file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | int len; |
| 684 | |
| 685 | file = inode_name(ino, dentry->d_name.len + 1); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 686 | if (file == NULL) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 687 | return NULL; |
| 688 | strcat(file, "/"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | len = strlen(file); |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 690 | strncat(file, dentry->d_name.name, dentry->d_name.len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | file[len + dentry->d_name.len] = '\0'; |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 692 | return file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | int hostfs_link(struct dentry *to, struct inode *ino, struct dentry *from) |
| 696 | { |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 697 | char *from_name, *to_name; |
| 698 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 700 | if ((from_name = inode_dentry_name(ino, from)) == NULL) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 701 | return -ENOMEM; |
| 702 | to_name = dentry_name(to, 0); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 703 | if (to_name == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | kfree(from_name); |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 705 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | } |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 707 | err = link_file(to_name, from_name); |
| 708 | kfree(from_name); |
| 709 | kfree(to_name); |
| 710 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | int hostfs_unlink(struct inode *ino, struct dentry *dentry) |
| 714 | { |
| 715 | char *file; |
| 716 | int err; |
| 717 | |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 718 | if ((file = inode_dentry_name(ino, dentry)) == NULL) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 719 | return -ENOMEM; |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 720 | if (append) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 721 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | |
| 723 | err = unlink_file(file); |
| 724 | kfree(file); |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 725 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | int hostfs_symlink(struct inode *ino, struct dentry *dentry, const char *to) |
| 729 | { |
| 730 | char *file; |
| 731 | int err; |
| 732 | |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 733 | if ((file = inode_dentry_name(ino, dentry)) == NULL) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 734 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | err = make_symlink(file, to); |
| 736 | kfree(file); |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 737 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | int hostfs_mkdir(struct inode *ino, struct dentry *dentry, int mode) |
| 741 | { |
| 742 | char *file; |
| 743 | int err; |
| 744 | |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 745 | if ((file = inode_dentry_name(ino, dentry)) == NULL) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 746 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | err = do_mkdir(file, mode); |
| 748 | kfree(file); |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 749 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | int hostfs_rmdir(struct inode *ino, struct dentry *dentry) |
| 753 | { |
| 754 | char *file; |
| 755 | int err; |
| 756 | |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 757 | if ((file = inode_dentry_name(ino, dentry)) == NULL) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 758 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | err = do_rmdir(file); |
| 760 | kfree(file); |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 761 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | int hostfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev) |
| 765 | { |
| 766 | struct inode *inode; |
| 767 | char *name; |
David Howells | 0a370e5 | 2008-02-07 00:15:50 -0800 | [diff] [blame] | 768 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | |
David Howells | 0a370e5 | 2008-02-07 00:15:50 -0800 | [diff] [blame] | 770 | inode = hostfs_iget(dir->i_sb); |
| 771 | if (IS_ERR(inode)) { |
| 772 | err = PTR_ERR(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | goto out; |
David Howells | 0a370e5 | 2008-02-07 00:15:50 -0800 | [diff] [blame] | 774 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | |
| 776 | err = init_inode(inode, dentry); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 777 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | goto out_put; |
| 779 | |
| 780 | err = -ENOMEM; |
| 781 | name = dentry_name(dentry, 0); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 782 | if (name == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | goto out_put; |
| 784 | |
| 785 | init_special_inode(inode, mode, dev); |
Johannes Stezenbach | 88f6cd0 | 2007-01-29 13:19:44 -0800 | [diff] [blame] | 786 | err = do_mknod(name, mode, MAJOR(dev), MINOR(dev)); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 787 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | goto out_free; |
| 789 | |
| 790 | err = read_name(inode, name); |
| 791 | kfree(name); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 792 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | goto out_put; |
| 794 | |
| 795 | d_instantiate(dentry, inode); |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 796 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | |
| 798 | out_free: |
| 799 | kfree(name); |
| 800 | out_put: |
| 801 | iput(inode); |
| 802 | out: |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 803 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | int hostfs_rename(struct inode *from_ino, struct dentry *from, |
| 807 | struct inode *to_ino, struct dentry *to) |
| 808 | { |
| 809 | char *from_name, *to_name; |
| 810 | int err; |
| 811 | |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 812 | if ((from_name = inode_dentry_name(from_ino, from)) == NULL) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 813 | return -ENOMEM; |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 814 | if ((to_name = inode_dentry_name(to_ino, to)) == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | kfree(from_name); |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 816 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | } |
| 818 | err = rename_file(from_name, to_name); |
| 819 | kfree(from_name); |
| 820 | kfree(to_name); |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 821 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | } |
| 823 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | int hostfs_permission(struct inode *ino, int desired, struct nameidata *nd) |
| 825 | { |
| 826 | char *name; |
| 827 | int r = 0, w = 0, x = 0, err; |
| 828 | |
| 829 | if (desired & MAY_READ) r = 1; |
| 830 | if (desired & MAY_WRITE) w = 1; |
| 831 | if (desired & MAY_EXEC) x = 1; |
| 832 | name = inode_name(ino, 0); |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 833 | if (name == NULL) |
| 834 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | |
| 836 | if (S_ISCHR(ino->i_mode) || S_ISBLK(ino->i_mode) || |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 837 | S_ISFIFO(ino->i_mode) || S_ISSOCK(ino->i_mode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | err = 0; |
| 839 | else |
| 840 | err = access_file(name, r, w, x); |
| 841 | kfree(name); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 842 | if (!err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | err = generic_permission(ino, desired, NULL); |
| 844 | return err; |
| 845 | } |
| 846 | |
| 847 | int hostfs_setattr(struct dentry *dentry, struct iattr *attr) |
| 848 | { |
| 849 | struct hostfs_iattr attrs; |
| 850 | char *name; |
| 851 | int err; |
| 852 | |
Alberto Bertogli | 5822b7f | 2007-05-08 00:23:16 -0700 | [diff] [blame] | 853 | int fd = HOSTFS_I(dentry->d_inode)->fd; |
| 854 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | err = inode_change_ok(dentry->d_inode, attr); |
| 856 | if (err) |
| 857 | return err; |
| 858 | |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 859 | if (append) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | attr->ia_valid &= ~ATTR_SIZE; |
| 861 | |
| 862 | attrs.ia_valid = 0; |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 863 | if (attr->ia_valid & ATTR_MODE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | attrs.ia_valid |= HOSTFS_ATTR_MODE; |
| 865 | attrs.ia_mode = attr->ia_mode; |
| 866 | } |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 867 | if (attr->ia_valid & ATTR_UID) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | attrs.ia_valid |= HOSTFS_ATTR_UID; |
| 869 | attrs.ia_uid = attr->ia_uid; |
| 870 | } |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 871 | if (attr->ia_valid & ATTR_GID) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | attrs.ia_valid |= HOSTFS_ATTR_GID; |
| 873 | attrs.ia_gid = attr->ia_gid; |
| 874 | } |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 875 | if (attr->ia_valid & ATTR_SIZE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | attrs.ia_valid |= HOSTFS_ATTR_SIZE; |
| 877 | attrs.ia_size = attr->ia_size; |
| 878 | } |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 879 | if (attr->ia_valid & ATTR_ATIME) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | attrs.ia_valid |= HOSTFS_ATTR_ATIME; |
| 881 | attrs.ia_atime = attr->ia_atime; |
| 882 | } |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 883 | if (attr->ia_valid & ATTR_MTIME) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | attrs.ia_valid |= HOSTFS_ATTR_MTIME; |
| 885 | attrs.ia_mtime = attr->ia_mtime; |
| 886 | } |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 887 | if (attr->ia_valid & ATTR_CTIME) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | attrs.ia_valid |= HOSTFS_ATTR_CTIME; |
| 889 | attrs.ia_ctime = attr->ia_ctime; |
| 890 | } |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 891 | if (attr->ia_valid & ATTR_ATIME_SET) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | attrs.ia_valid |= HOSTFS_ATTR_ATIME_SET; |
| 893 | } |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 894 | if (attr->ia_valid & ATTR_MTIME_SET) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | attrs.ia_valid |= HOSTFS_ATTR_MTIME_SET; |
| 896 | } |
| 897 | name = dentry_name(dentry, 0); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 898 | if (name == NULL) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 899 | return -ENOMEM; |
Alberto Bertogli | 5822b7f | 2007-05-08 00:23:16 -0700 | [diff] [blame] | 900 | err = set_attr(name, &attrs, fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | kfree(name); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 902 | if (err) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 903 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 904 | |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 905 | return inode_setattr(dentry->d_inode, attr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | } |
| 907 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 908 | static const struct inode_operations hostfs_iops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | .create = hostfs_create, |
| 910 | .link = hostfs_link, |
| 911 | .unlink = hostfs_unlink, |
| 912 | .symlink = hostfs_symlink, |
| 913 | .mkdir = hostfs_mkdir, |
| 914 | .rmdir = hostfs_rmdir, |
| 915 | .mknod = hostfs_mknod, |
| 916 | .rename = hostfs_rename, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | .permission = hostfs_permission, |
| 918 | .setattr = hostfs_setattr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | }; |
| 920 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 921 | static const struct inode_operations hostfs_dir_iops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | .create = hostfs_create, |
| 923 | .lookup = hostfs_lookup, |
| 924 | .link = hostfs_link, |
| 925 | .unlink = hostfs_unlink, |
| 926 | .symlink = hostfs_symlink, |
| 927 | .mkdir = hostfs_mkdir, |
| 928 | .rmdir = hostfs_rmdir, |
| 929 | .mknod = hostfs_mknod, |
| 930 | .rename = hostfs_rename, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | .permission = hostfs_permission, |
| 932 | .setattr = hostfs_setattr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | }; |
| 934 | |
| 935 | int hostfs_link_readpage(struct file *file, struct page *page) |
| 936 | { |
| 937 | char *buffer, *name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | int err; |
| 939 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | buffer = kmap(page); |
| 941 | name = inode_name(page->mapping->host, 0); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 942 | if (name == NULL) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 943 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | err = do_readlink(name, buffer, PAGE_CACHE_SIZE); |
| 945 | kfree(name); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 946 | if (err == PAGE_CACHE_SIZE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | err = -E2BIG; |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 948 | else if (err > 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | flush_dcache_page(page); |
| 950 | SetPageUptodate(page); |
| 951 | if (PageError(page)) ClearPageError(page); |
| 952 | err = 0; |
| 953 | } |
| 954 | kunmap(page); |
| 955 | unlock_page(page); |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 956 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | } |
| 958 | |
Christoph Hellwig | f5e54d6 | 2006-06-28 04:26:44 -0700 | [diff] [blame] | 959 | static const struct address_space_operations hostfs_link_aops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | .readpage = hostfs_link_readpage, |
| 961 | }; |
| 962 | |
| 963 | static int hostfs_fill_sb_common(struct super_block *sb, void *d, int silent) |
| 964 | { |
| 965 | struct inode *root_inode; |
Paolo 'Blaisorblade' Giarrusso | 75e8def | 2007-03-29 01:20:33 -0700 | [diff] [blame] | 966 | char *host_root_path, *req_root = d; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | int err; |
| 968 | |
| 969 | sb->s_blocksize = 1024; |
| 970 | sb->s_blocksize_bits = 10; |
| 971 | sb->s_magic = HOSTFS_SUPER_MAGIC; |
| 972 | sb->s_op = &hostfs_sbops; |
| 973 | |
Paolo 'Blaisorblade' Giarrusso | a6eb0be | 2007-03-07 20:41:08 -0800 | [diff] [blame] | 974 | /* NULL is printed as <NULL> by sprintf: avoid that. */ |
Paolo 'Blaisorblade' Giarrusso | 75e8def | 2007-03-29 01:20:33 -0700 | [diff] [blame] | 975 | if (req_root == NULL) |
| 976 | req_root = ""; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | |
| 978 | err = -ENOMEM; |
Paolo 'Blaisorblade' Giarrusso | 75e8def | 2007-03-29 01:20:33 -0700 | [diff] [blame] | 979 | host_root_path = kmalloc(strlen(root_ino) + 1 |
| 980 | + strlen(req_root) + 1, GFP_KERNEL); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 981 | if (host_root_path == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | goto out; |
| 983 | |
Paolo 'Blaisorblade' Giarrusso | 75e8def | 2007-03-29 01:20:33 -0700 | [diff] [blame] | 984 | sprintf(host_root_path, "%s/%s", root_ino, req_root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | |
David Howells | 0a370e5 | 2008-02-07 00:15:50 -0800 | [diff] [blame] | 986 | root_inode = hostfs_iget(sb); |
| 987 | if (IS_ERR(root_inode)) { |
| 988 | err = PTR_ERR(root_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | goto out_free; |
David Howells | 0a370e5 | 2008-02-07 00:15:50 -0800 | [diff] [blame] | 990 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | |
| 992 | err = init_inode(root_inode, NULL); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 993 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | goto out_put; |
| 995 | |
Paolo 'Blaisorblade' Giarrusso | 75e8def | 2007-03-29 01:20:33 -0700 | [diff] [blame] | 996 | HOSTFS_I(root_inode)->host_filename = host_root_path; |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 997 | /* |
| 998 | * Avoid that in the error path, iput(root_inode) frees again |
| 999 | * host_root_path through hostfs_destroy_inode! |
| 1000 | */ |
Paolo 'Blaisorblade' Giarrusso | 75e8def | 2007-03-29 01:20:33 -0700 | [diff] [blame] | 1001 | host_root_path = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | |
| 1003 | err = -ENOMEM; |
| 1004 | sb->s_root = d_alloc_root(root_inode); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 1005 | if (sb->s_root == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | goto out_put; |
| 1007 | |
David Howells | 0a370e5 | 2008-02-07 00:15:50 -0800 | [diff] [blame] | 1008 | err = hostfs_read_inode(root_inode); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 1009 | if (err) { |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 1010 | /* No iput in this case because the dput does that for us */ |
| 1011 | dput(sb->s_root); |
| 1012 | sb->s_root = NULL; |
Paolo 'Blaisorblade' Giarrusso | bca2711 | 2007-03-07 20:41:07 -0800 | [diff] [blame] | 1013 | goto out; |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 1014 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 1016 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 1018 | out_put: |
| 1019 | iput(root_inode); |
| 1020 | out_free: |
Paolo 'Blaisorblade' Giarrusso | 75e8def | 2007-03-29 01:20:33 -0700 | [diff] [blame] | 1021 | kfree(host_root_path); |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 1022 | out: |
| 1023 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | } |
| 1025 | |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 1026 | static int hostfs_read_sb(struct file_system_type *type, |
| 1027 | int flags, const char *dev_name, |
| 1028 | void *data, struct vfsmount *mnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | { |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 1030 | return get_sb_nodev(type, flags, data, hostfs_fill_sb_common, mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | static struct file_system_type hostfs_type = { |
| 1034 | .owner = THIS_MODULE, |
| 1035 | .name = "hostfs", |
| 1036 | .get_sb = hostfs_read_sb, |
| 1037 | .kill_sb = kill_anon_super, |
| 1038 | .fs_flags = 0, |
| 1039 | }; |
| 1040 | |
| 1041 | static int __init init_hostfs(void) |
| 1042 | { |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 1043 | return register_filesystem(&hostfs_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | } |
| 1045 | |
| 1046 | static void __exit exit_hostfs(void) |
| 1047 | { |
| 1048 | unregister_filesystem(&hostfs_type); |
| 1049 | } |
| 1050 | |
| 1051 | module_init(init_hostfs) |
| 1052 | module_exit(exit_hostfs) |
| 1053 | MODULE_LICENSE("GPL"); |