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