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