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