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