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