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