diff options
Diffstat (limited to 'fs/hostfs/hostfs_kern.c')
| -rw-r--r-- | fs/hostfs/hostfs_kern.c | 517 |
1 files changed, 229 insertions, 288 deletions
diff --git a/fs/hostfs/hostfs_kern.c b/fs/hostfs/hostfs_kern.c index 87ac1891a185..dd1e55535a4e 100644 --- a/fs/hostfs/hostfs_kern.c +++ b/fs/hostfs/hostfs_kern.c | |||
| @@ -14,12 +14,12 @@ | |||
| 14 | #include <linux/slab.h> | 14 | #include <linux/slab.h> |
| 15 | #include <linux/seq_file.h> | 15 | #include <linux/seq_file.h> |
| 16 | #include <linux/mount.h> | 16 | #include <linux/mount.h> |
| 17 | #include <linux/namei.h> | ||
| 17 | #include "hostfs.h" | 18 | #include "hostfs.h" |
| 18 | #include "init.h" | 19 | #include "init.h" |
| 19 | #include "kern.h" | 20 | #include "kern.h" |
| 20 | 21 | ||
| 21 | struct hostfs_inode_info { | 22 | struct hostfs_inode_info { |
| 22 | char *host_filename; | ||
| 23 | int fd; | 23 | int fd; |
| 24 | fmode_t mode; | 24 | fmode_t mode; |
| 25 | struct inode vfs_inode; | 25 | struct inode vfs_inode; |
| @@ -49,7 +49,7 @@ static int append = 0; | |||
| 49 | 49 | ||
| 50 | static const struct inode_operations hostfs_iops; | 50 | static const struct inode_operations hostfs_iops; |
| 51 | static const struct inode_operations hostfs_dir_iops; | 51 | static const struct inode_operations hostfs_dir_iops; |
| 52 | static const struct address_space_operations hostfs_link_aops; | 52 | static const struct inode_operations hostfs_link_iops; |
| 53 | 53 | ||
| 54 | #ifndef MODULE | 54 | #ifndef MODULE |
| 55 | static int __init hostfs_args(char *options, int *add) | 55 | static int __init hostfs_args(char *options, int *add) |
| @@ -90,71 +90,58 @@ __uml_setup("hostfs=", hostfs_args, | |||
| 90 | ); | 90 | ); |
| 91 | #endif | 91 | #endif |
| 92 | 92 | ||
| 93 | static char *dentry_name(struct dentry *dentry, int extra) | 93 | static char *__dentry_name(struct dentry *dentry, char *name) |
| 94 | { | 94 | { |
| 95 | struct dentry *parent; | 95 | char *p = __dentry_path(dentry, name, PATH_MAX); |
| 96 | char *root, *name; | 96 | char *root; |
| 97 | int len; | 97 | size_t len; |
| 98 | |||
| 99 | len = 0; | ||
| 100 | parent = dentry; | ||
| 101 | while (parent->d_parent != parent) { | ||
| 102 | len += parent->d_name.len + 1; | ||
| 103 | parent = parent->d_parent; | ||
| 104 | } | ||
| 105 | 98 | ||
| 106 | root = HOSTFS_I(parent->d_inode)->host_filename; | 99 | spin_unlock(&dcache_lock); |
| 107 | len += strlen(root); | ||
| 108 | name = kmalloc(len + extra + 1, GFP_KERNEL); | ||
| 109 | if (name == NULL) | ||
| 110 | return NULL; | ||
| 111 | 100 | ||
| 112 | name[len] = '\0'; | 101 | root = dentry->d_sb->s_fs_info; |
| 113 | parent = dentry; | 102 | len = strlen(root); |
| 114 | while (parent->d_parent != parent) { | 103 | if (IS_ERR(p)) { |
| 115 | len -= parent->d_name.len + 1; | 104 | __putname(name); |
| 116 | name[len] = '/'; | 105 | return NULL; |
| 117 | strncpy(&name[len + 1], parent->d_name.name, | 106 | } |
| 118 | parent->d_name.len); | 107 | strncpy(name, root, PATH_MAX); |
| 119 | parent = parent->d_parent; | 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 | ; | ||
| 120 | } | 116 | } |
| 121 | strncpy(name, root, strlen(root)); | ||
| 122 | return name; | 117 | return name; |
| 123 | } | 118 | } |
| 124 | 119 | ||
| 125 | static char *inode_name(struct inode *ino, int extra) | 120 | static char *dentry_name(struct dentry *dentry) |
| 126 | { | 121 | { |
| 127 | struct dentry *dentry; | 122 | char *name = __getname(); |
| 123 | if (!name) | ||
| 124 | return NULL; | ||
| 128 | 125 | ||
| 129 | dentry = list_entry(ino->i_dentry.next, struct dentry, d_alias); | 126 | spin_lock(&dcache_lock); |
| 130 | return dentry_name(dentry, extra); | 127 | return __dentry_name(dentry, name); /* will unlock */ |
| 131 | } | 128 | } |
| 132 | 129 | ||
| 133 | static int read_name(struct inode *ino, char *name) | 130 | static char *inode_name(struct inode *ino) |
| 134 | { | 131 | { |
| 135 | /* | 132 | struct dentry *dentry; |
| 136 | * The non-int inode fields are copied into ints by stat_file and | 133 | char *name = __getname(); |
| 137 | * then copied into the inode because passing the actual pointers | 134 | if (!name) |
| 138 | * in and having them treated as int * breaks on big-endian machines | 135 | return NULL; |
| 139 | */ | ||
| 140 | int err; | ||
| 141 | int i_mode, i_nlink, i_blksize; | ||
| 142 | unsigned long long i_size; | ||
| 143 | unsigned long long i_ino; | ||
| 144 | unsigned long long i_blocks; | ||
| 145 | |||
| 146 | err = stat_file(name, &i_ino, &i_mode, &i_nlink, &ino->i_uid, | ||
| 147 | &ino->i_gid, &i_size, &ino->i_atime, &ino->i_mtime, | ||
| 148 | &ino->i_ctime, &i_blksize, &i_blocks, -1); | ||
| 149 | if (err) | ||
| 150 | return err; | ||
| 151 | 136 | ||
| 152 | ino->i_ino = i_ino; | 137 | spin_lock(&dcache_lock); |
| 153 | ino->i_mode = i_mode; | 138 | if (list_empty(&ino->i_dentry)) { |
| 154 | ino->i_nlink = i_nlink; | 139 | spin_unlock(&dcache_lock); |
| 155 | ino->i_size = i_size; | 140 | __putname(name); |
| 156 | ino->i_blocks = i_blocks; | 141 | return NULL; |
| 157 | return 0; | 142 | } |
| 143 | dentry = list_first_entry(&ino->i_dentry, struct dentry, d_alias); | ||
| 144 | return __dentry_name(dentry, name); /* will unlock */ | ||
| 158 | } | 145 | } |
| 159 | 146 | ||
| 160 | static char *follow_link(char *link) | 147 | static char *follow_link(char *link) |
| @@ -205,53 +192,11 @@ static char *follow_link(char *link) | |||
| 205 | return ERR_PTR(n); | 192 | return ERR_PTR(n); |
| 206 | } | 193 | } |
| 207 | 194 | ||
| 208 | static int hostfs_read_inode(struct inode *ino) | ||
| 209 | { | ||
| 210 | char *name; | ||
| 211 | int err = 0; | ||
| 212 | |||
| 213 | /* | ||
| 214 | * Unfortunately, we are called from iget() when we don't have a dentry | ||
| 215 | * allocated yet. | ||
| 216 | */ | ||
| 217 | if (list_empty(&ino->i_dentry)) | ||
| 218 | goto out; | ||
| 219 | |||
| 220 | err = -ENOMEM; | ||
| 221 | name = inode_name(ino, 0); | ||
| 222 | if (name == NULL) | ||
| 223 | goto out; | ||
| 224 | |||
| 225 | if (file_type(name, NULL, NULL) == OS_TYPE_SYMLINK) { | ||
| 226 | name = follow_link(name); | ||
| 227 | if (IS_ERR(name)) { | ||
| 228 | err = PTR_ERR(name); | ||
| 229 | goto out; | ||
| 230 | } | ||
| 231 | } | ||
| 232 | |||
| 233 | err = read_name(ino, name); | ||
| 234 | kfree(name); | ||
| 235 | out: | ||
| 236 | return err; | ||
| 237 | } | ||
| 238 | |||
| 239 | static struct inode *hostfs_iget(struct super_block *sb) | 195 | static struct inode *hostfs_iget(struct super_block *sb) |
| 240 | { | 196 | { |
| 241 | struct inode *inode; | 197 | struct inode *inode = new_inode(sb); |
| 242 | long ret; | ||
| 243 | |||
| 244 | inode = iget_locked(sb, 0); | ||
| 245 | if (!inode) | 198 | if (!inode) |
| 246 | return ERR_PTR(-ENOMEM); | 199 | return ERR_PTR(-ENOMEM); |
| 247 | if (inode->i_state & I_NEW) { | ||
| 248 | ret = hostfs_read_inode(inode); | ||
| 249 | if (ret < 0) { | ||
| 250 | iget_failed(inode); | ||
| 251 | return ERR_PTR(ret); | ||
| 252 | } | ||
| 253 | unlock_new_inode(inode); | ||
| 254 | } | ||
| 255 | return inode; | 200 | return inode; |
| 256 | } | 201 | } |
| 257 | 202 | ||
| @@ -269,7 +214,7 @@ int hostfs_statfs(struct dentry *dentry, struct kstatfs *sf) | |||
| 269 | long long f_files; | 214 | long long f_files; |
| 270 | long long f_ffree; | 215 | long long f_ffree; |
| 271 | 216 | ||
| 272 | err = do_statfs(HOSTFS_I(dentry->d_sb->s_root->d_inode)->host_filename, | 217 | err = do_statfs(dentry->d_sb->s_fs_info, |
| 273 | &sf->f_bsize, &f_blocks, &f_bfree, &f_bavail, &f_files, | 218 | &sf->f_bsize, &f_blocks, &f_bfree, &f_bavail, &f_files, |
| 274 | &f_ffree, &sf->f_fsid, sizeof(sf->f_fsid), | 219 | &f_ffree, &sf->f_fsid, sizeof(sf->f_fsid), |
| 275 | &sf->f_namelen, sf->f_spare); | 220 | &sf->f_namelen, sf->f_spare); |
| @@ -288,47 +233,32 @@ static struct inode *hostfs_alloc_inode(struct super_block *sb) | |||
| 288 | { | 233 | { |
| 289 | struct hostfs_inode_info *hi; | 234 | struct hostfs_inode_info *hi; |
| 290 | 235 | ||
| 291 | hi = kmalloc(sizeof(*hi), GFP_KERNEL); | 236 | hi = kzalloc(sizeof(*hi), GFP_KERNEL); |
| 292 | if (hi == NULL) | 237 | if (hi == NULL) |
| 293 | return NULL; | 238 | return NULL; |
| 294 | 239 | hi->fd = -1; | |
| 295 | *hi = ((struct hostfs_inode_info) { .host_filename = NULL, | ||
| 296 | .fd = -1, | ||
| 297 | .mode = 0 }); | ||
| 298 | inode_init_once(&hi->vfs_inode); | 240 | inode_init_once(&hi->vfs_inode); |
| 299 | return &hi->vfs_inode; | 241 | return &hi->vfs_inode; |
| 300 | } | 242 | } |
| 301 | 243 | ||
| 302 | static void hostfs_delete_inode(struct inode *inode) | 244 | static void hostfs_evict_inode(struct inode *inode) |
| 303 | { | 245 | { |
| 304 | truncate_inode_pages(&inode->i_data, 0); | 246 | truncate_inode_pages(&inode->i_data, 0); |
| 247 | end_writeback(inode); | ||
| 305 | if (HOSTFS_I(inode)->fd != -1) { | 248 | if (HOSTFS_I(inode)->fd != -1) { |
| 306 | close_file(&HOSTFS_I(inode)->fd); | 249 | close_file(&HOSTFS_I(inode)->fd); |
| 307 | HOSTFS_I(inode)->fd = -1; | 250 | HOSTFS_I(inode)->fd = -1; |
| 308 | } | 251 | } |
| 309 | clear_inode(inode); | ||
| 310 | } | 252 | } |
| 311 | 253 | ||
| 312 | static void hostfs_destroy_inode(struct inode *inode) | 254 | static void hostfs_destroy_inode(struct inode *inode) |
| 313 | { | 255 | { |
| 314 | kfree(HOSTFS_I(inode)->host_filename); | ||
| 315 | |||
| 316 | /* | ||
| 317 | * XXX: This should not happen, probably. The check is here for | ||
| 318 | * additional safety. | ||
| 319 | */ | ||
| 320 | if (HOSTFS_I(inode)->fd != -1) { | ||
| 321 | close_file(&HOSTFS_I(inode)->fd); | ||
| 322 | printk(KERN_DEBUG "Closing host fd in .destroy_inode\n"); | ||
| 323 | } | ||
| 324 | |||
| 325 | kfree(HOSTFS_I(inode)); | 256 | kfree(HOSTFS_I(inode)); |
| 326 | } | 257 | } |
| 327 | 258 | ||
| 328 | static int hostfs_show_options(struct seq_file *seq, struct vfsmount *vfs) | 259 | static int hostfs_show_options(struct seq_file *seq, struct vfsmount *vfs) |
| 329 | { | 260 | { |
| 330 | struct inode *root = vfs->mnt_sb->s_root->d_inode; | 261 | const char *root_path = vfs->mnt_sb->s_fs_info; |
| 331 | const char *root_path = HOSTFS_I(root)->host_filename; | ||
| 332 | size_t offset = strlen(root_ino) + 1; | 262 | size_t offset = strlen(root_ino) + 1; |
| 333 | 263 | ||
| 334 | if (strlen(root_path) > offset) | 264 | if (strlen(root_path) > offset) |
| @@ -339,9 +269,8 @@ static int hostfs_show_options(struct seq_file *seq, struct vfsmount *vfs) | |||
| 339 | 269 | ||
| 340 | static const struct super_operations hostfs_sbops = { | 270 | static const struct super_operations hostfs_sbops = { |
| 341 | .alloc_inode = hostfs_alloc_inode, | 271 | .alloc_inode = hostfs_alloc_inode, |
| 342 | .drop_inode = generic_delete_inode, | ||
| 343 | .delete_inode = hostfs_delete_inode, | ||
| 344 | .destroy_inode = hostfs_destroy_inode, | 272 | .destroy_inode = hostfs_destroy_inode, |
| 273 | .evict_inode = hostfs_evict_inode, | ||
| 345 | .statfs = hostfs_statfs, | 274 | .statfs = hostfs_statfs, |
| 346 | .show_options = hostfs_show_options, | 275 | .show_options = hostfs_show_options, |
| 347 | }; | 276 | }; |
| @@ -353,11 +282,11 @@ int hostfs_readdir(struct file *file, void *ent, filldir_t filldir) | |||
| 353 | unsigned long long next, ino; | 282 | unsigned long long next, ino; |
| 354 | int error, len; | 283 | int error, len; |
| 355 | 284 | ||
| 356 | name = dentry_name(file->f_path.dentry, 0); | 285 | name = dentry_name(file->f_path.dentry); |
| 357 | if (name == NULL) | 286 | if (name == NULL) |
| 358 | return -ENOMEM; | 287 | return -ENOMEM; |
| 359 | dir = open_dir(name, &error); | 288 | dir = open_dir(name, &error); |
| 360 | kfree(name); | 289 | __putname(name); |
| 361 | if (dir == NULL) | 290 | if (dir == NULL) |
| 362 | return -error; | 291 | return -error; |
| 363 | next = file->f_pos; | 292 | next = file->f_pos; |
| @@ -373,40 +302,59 @@ int hostfs_readdir(struct file *file, void *ent, filldir_t filldir) | |||
| 373 | 302 | ||
| 374 | int hostfs_file_open(struct inode *ino, struct file *file) | 303 | int hostfs_file_open(struct inode *ino, struct file *file) |
| 375 | { | 304 | { |
| 305 | static DEFINE_MUTEX(open_mutex); | ||
| 376 | char *name; | 306 | char *name; |
| 377 | fmode_t mode = 0; | 307 | fmode_t mode = 0; |
| 308 | int err; | ||
| 378 | int r = 0, w = 0, fd; | 309 | int r = 0, w = 0, fd; |
| 379 | 310 | ||
| 380 | mode = file->f_mode & (FMODE_READ | FMODE_WRITE); | 311 | mode = file->f_mode & (FMODE_READ | FMODE_WRITE); |
| 381 | if ((mode & HOSTFS_I(ino)->mode) == mode) | 312 | if ((mode & HOSTFS_I(ino)->mode) == mode) |
| 382 | return 0; | 313 | return 0; |
| 383 | 314 | ||
| 384 | /* | 315 | mode |= HOSTFS_I(ino)->mode; |
| 385 | * The file may already have been opened, but with the wrong access, | ||
| 386 | * so this resets things and reopens the file with the new access. | ||
| 387 | */ | ||
| 388 | if (HOSTFS_I(ino)->fd != -1) { | ||
| 389 | close_file(&HOSTFS_I(ino)->fd); | ||
| 390 | HOSTFS_I(ino)->fd = -1; | ||
| 391 | } | ||
| 392 | 316 | ||
| 393 | HOSTFS_I(ino)->mode |= mode; | 317 | retry: |
| 394 | if (HOSTFS_I(ino)->mode & FMODE_READ) | 318 | if (mode & FMODE_READ) |
| 395 | r = 1; | 319 | r = 1; |
| 396 | if (HOSTFS_I(ino)->mode & FMODE_WRITE) | 320 | if (mode & FMODE_WRITE) |
| 397 | w = 1; | 321 | w = 1; |
| 398 | if (w) | 322 | if (w) |
| 399 | r = 1; | 323 | r = 1; |
| 400 | 324 | ||
| 401 | name = dentry_name(file->f_path.dentry, 0); | 325 | name = dentry_name(file->f_path.dentry); |
| 402 | if (name == NULL) | 326 | if (name == NULL) |
| 403 | return -ENOMEM; | 327 | return -ENOMEM; |
| 404 | 328 | ||
| 405 | fd = open_file(name, r, w, append); | 329 | fd = open_file(name, r, w, append); |
| 406 | kfree(name); | 330 | __putname(name); |
| 407 | if (fd < 0) | 331 | if (fd < 0) |
| 408 | return fd; | 332 | return fd; |
| 409 | FILE_HOSTFS_I(file)->fd = fd; | 333 | |
| 334 | mutex_lock(&open_mutex); | ||
| 335 | /* somebody else had handled it first? */ | ||
| 336 | if ((mode & HOSTFS_I(ino)->mode) == mode) { | ||
| 337 | mutex_unlock(&open_mutex); | ||
| 338 | return 0; | ||
| 339 | } | ||
| 340 | if ((mode | HOSTFS_I(ino)->mode) != mode) { | ||
| 341 | mode |= HOSTFS_I(ino)->mode; | ||
| 342 | mutex_unlock(&open_mutex); | ||
| 343 | close_file(&fd); | ||
| 344 | goto retry; | ||
| 345 | } | ||
| 346 | if (HOSTFS_I(ino)->fd == -1) { | ||
| 347 | HOSTFS_I(ino)->fd = fd; | ||
| 348 | } else { | ||
| 349 | err = replace_file(fd, HOSTFS_I(ino)->fd); | ||
| 350 | close_file(&fd); | ||
| 351 | if (err < 0) { | ||
| 352 | mutex_unlock(&open_mutex); | ||
| 353 | return err; | ||
| 354 | } | ||
| 355 | } | ||
| 356 | HOSTFS_I(ino)->mode = mode; | ||
| 357 | mutex_unlock(&open_mutex); | ||
| 410 | 358 | ||
| 411 | return 0; | 359 | return 0; |
| 412 | } | 360 | } |
| @@ -544,54 +492,50 @@ static const struct address_space_operations hostfs_aops = { | |||
| 544 | .write_end = hostfs_write_end, | 492 | .write_end = hostfs_write_end, |
| 545 | }; | 493 | }; |
| 546 | 494 | ||
| 547 | static int init_inode(struct inode *inode, struct dentry *dentry) | 495 | static int read_name(struct inode *ino, char *name) |
| 548 | { | 496 | { |
| 549 | char *name; | 497 | dev_t rdev; |
| 550 | int type, err = -ENOMEM; | 498 | struct hostfs_stat st; |
| 551 | int maj, min; | 499 | int err = stat_file(name, &st, -1); |
| 552 | dev_t rdev = 0; | 500 | if (err) |
| 501 | return err; | ||
| 553 | 502 | ||
| 554 | if (dentry) { | 503 | /* Reencode maj and min with the kernel encoding.*/ |
| 555 | name = dentry_name(dentry, 0); | 504 | rdev = MKDEV(st.maj, st.min); |
| 556 | if (name == NULL) | ||
| 557 | goto out; | ||
| 558 | type = file_type(name, &maj, &min); | ||
| 559 | /* Reencode maj and min with the kernel encoding.*/ | ||
| 560 | rdev = MKDEV(maj, min); | ||
| 561 | kfree(name); | ||
| 562 | } | ||
| 563 | else type = OS_TYPE_DIR; | ||
| 564 | 505 | ||
| 565 | err = 0; | 506 | switch (st.mode & S_IFMT) { |
| 566 | if (type == OS_TYPE_SYMLINK) | 507 | case S_IFLNK: |
| 567 | inode->i_op = &page_symlink_inode_operations; | 508 | ino->i_op = &hostfs_link_iops; |
| 568 | else if (type == OS_TYPE_DIR) | ||
| 569 | inode->i_op = &hostfs_dir_iops; | ||
| 570 | else inode->i_op = &hostfs_iops; | ||
| 571 | |||
| 572 | if (type == OS_TYPE_DIR) inode->i_fop = &hostfs_dir_fops; | ||
| 573 | else inode->i_fop = &hostfs_file_fops; | ||
| 574 | |||
| 575 | if (type == OS_TYPE_SYMLINK) | ||
| 576 | inode->i_mapping->a_ops = &hostfs_link_aops; | ||
| 577 | else inode->i_mapping->a_ops = &hostfs_aops; | ||
| 578 | |||
| 579 | switch (type) { | ||
| 580 | case OS_TYPE_CHARDEV: | ||
| 581 | init_special_inode(inode, S_IFCHR, rdev); | ||
| 582 | break; | 509 | break; |
| 583 | case OS_TYPE_BLOCKDEV: | 510 | case S_IFDIR: |
| 584 | init_special_inode(inode, S_IFBLK, rdev); | 511 | ino->i_op = &hostfs_dir_iops; |
| 512 | ino->i_fop = &hostfs_dir_fops; | ||
| 585 | break; | 513 | break; |
| 586 | case OS_TYPE_FIFO: | 514 | case S_IFCHR: |
| 587 | init_special_inode(inode, S_IFIFO, 0); | 515 | case S_IFBLK: |
| 516 | case S_IFIFO: | ||
| 517 | case S_IFSOCK: | ||
| 518 | init_special_inode(ino, st.mode & S_IFMT, rdev); | ||
| 519 | ino->i_op = &hostfs_iops; | ||
| 588 | break; | 520 | break; |
| 589 | case OS_TYPE_SOCK: | 521 | |
| 590 | init_special_inode(inode, S_IFSOCK, 0); | 522 | default: |
| 591 | break; | 523 | ino->i_op = &hostfs_iops; |
| 592 | } | 524 | ino->i_fop = &hostfs_file_fops; |
| 593 | out: | 525 | ino->i_mapping->a_ops = &hostfs_aops; |
| 594 | return err; | 526 | } |
| 527 | |||
| 528 | ino->i_ino = st.ino; | ||
| 529 | ino->i_mode = st.mode; | ||
| 530 | ino->i_nlink = st.nlink; | ||
| 531 | ino->i_uid = st.uid; | ||
| 532 | ino->i_gid = st.gid; | ||
| 533 | ino->i_atime = st.atime; | ||
| 534 | ino->i_mtime = st.mtime; | ||
| 535 | ino->i_ctime = st.ctime; | ||
| 536 | ino->i_size = st.size; | ||
| 537 | ino->i_blocks = st.blocks; | ||
| 538 | return 0; | ||
| 595 | } | 539 | } |
| 596 | 540 | ||
| 597 | int hostfs_create(struct inode *dir, struct dentry *dentry, int mode, | 541 | int hostfs_create(struct inode *dir, struct dentry *dentry, int mode, |
| @@ -607,12 +551,8 @@ int hostfs_create(struct inode *dir, struct dentry *dentry, int mode, | |||
| 607 | goto out; | 551 | goto out; |
| 608 | } | 552 | } |
| 609 | 553 | ||
| 610 | error = init_inode(inode, dentry); | ||
| 611 | if (error) | ||
| 612 | goto out_put; | ||
| 613 | |||
| 614 | error = -ENOMEM; | 554 | error = -ENOMEM; |
| 615 | name = dentry_name(dentry, 0); | 555 | name = dentry_name(dentry); |
| 616 | if (name == NULL) | 556 | if (name == NULL) |
| 617 | goto out_put; | 557 | goto out_put; |
| 618 | 558 | ||
| @@ -622,9 +562,10 @@ int hostfs_create(struct inode *dir, struct dentry *dentry, int mode, | |||
| 622 | mode & S_IROTH, mode & S_IWOTH, mode & S_IXOTH); | 562 | mode & S_IROTH, mode & S_IWOTH, mode & S_IXOTH); |
| 623 | if (fd < 0) | 563 | if (fd < 0) |
| 624 | error = fd; | 564 | error = fd; |
| 625 | else error = read_name(inode, name); | 565 | else |
| 566 | error = read_name(inode, name); | ||
| 626 | 567 | ||
| 627 | kfree(name); | 568 | __putname(name); |
| 628 | if (error) | 569 | if (error) |
| 629 | goto out_put; | 570 | goto out_put; |
| 630 | 571 | ||
| @@ -652,17 +593,14 @@ struct dentry *hostfs_lookup(struct inode *ino, struct dentry *dentry, | |||
| 652 | goto out; | 593 | goto out; |
| 653 | } | 594 | } |
| 654 | 595 | ||
| 655 | err = init_inode(inode, dentry); | ||
| 656 | if (err) | ||
| 657 | goto out_put; | ||
| 658 | |||
| 659 | err = -ENOMEM; | 596 | err = -ENOMEM; |
| 660 | name = dentry_name(dentry, 0); | 597 | name = dentry_name(dentry); |
| 661 | if (name == NULL) | 598 | if (name == NULL) |
| 662 | goto out_put; | 599 | goto out_put; |
| 663 | 600 | ||
| 664 | err = read_name(inode, name); | 601 | err = read_name(inode, name); |
| 665 | kfree(name); | 602 | |
| 603 | __putname(name); | ||
| 666 | if (err == -ENOENT) { | 604 | if (err == -ENOENT) { |
| 667 | iput(inode); | 605 | iput(inode); |
| 668 | inode = NULL; | 606 | inode = NULL; |
| @@ -680,36 +618,21 @@ struct dentry *hostfs_lookup(struct inode *ino, struct dentry *dentry, | |||
| 680 | return ERR_PTR(err); | 618 | return ERR_PTR(err); |
| 681 | } | 619 | } |
| 682 | 620 | ||
| 683 | static char *inode_dentry_name(struct inode *ino, struct dentry *dentry) | ||
| 684 | { | ||
| 685 | char *file; | ||
| 686 | int len; | ||
| 687 | |||
| 688 | file = inode_name(ino, dentry->d_name.len + 1); | ||
| 689 | if (file == NULL) | ||
| 690 | return NULL; | ||
| 691 | strcat(file, "/"); | ||
| 692 | len = strlen(file); | ||
| 693 | strncat(file, dentry->d_name.name, dentry->d_name.len); | ||
| 694 | file[len + dentry->d_name.len] = '\0'; | ||
| 695 | return file; | ||
| 696 | } | ||
| 697 | |||
| 698 | int hostfs_link(struct dentry *to, struct inode *ino, struct dentry *from) | 621 | int hostfs_link(struct dentry *to, struct inode *ino, struct dentry *from) |
| 699 | { | 622 | { |
| 700 | char *from_name, *to_name; | 623 | char *from_name, *to_name; |
| 701 | int err; | 624 | int err; |
| 702 | 625 | ||
| 703 | if ((from_name = inode_dentry_name(ino, from)) == NULL) | 626 | if ((from_name = dentry_name(from)) == NULL) |
| 704 | return -ENOMEM; | 627 | return -ENOMEM; |
| 705 | to_name = dentry_name(to, 0); | 628 | to_name = dentry_name(to); |
| 706 | if (to_name == NULL) { | 629 | if (to_name == NULL) { |
| 707 | kfree(from_name); | 630 | __putname(from_name); |
| 708 | return -ENOMEM; | 631 | return -ENOMEM; |
| 709 | } | 632 | } |
| 710 | err = link_file(to_name, from_name); | 633 | err = link_file(to_name, from_name); |
| 711 | kfree(from_name); | 634 | __putname(from_name); |
| 712 | kfree(to_name); | 635 | __putname(to_name); |
| 713 | return err; | 636 | return err; |
| 714 | } | 637 | } |
| 715 | 638 | ||
| @@ -718,13 +641,14 @@ int hostfs_unlink(struct inode *ino, struct dentry *dentry) | |||
| 718 | char *file; | 641 | char *file; |
| 719 | int err; | 642 | int err; |
| 720 | 643 | ||
| 721 | if ((file = inode_dentry_name(ino, dentry)) == NULL) | ||
| 722 | return -ENOMEM; | ||
| 723 | if (append) | 644 | if (append) |
| 724 | return -EPERM; | 645 | return -EPERM; |
| 725 | 646 | ||
| 647 | if ((file = dentry_name(dentry)) == NULL) | ||
| 648 | return -ENOMEM; | ||
| 649 | |||
| 726 | err = unlink_file(file); | 650 | err = unlink_file(file); |
| 727 | kfree(file); | 651 | __putname(file); |
| 728 | return err; | 652 | return err; |
| 729 | } | 653 | } |
| 730 | 654 | ||
| @@ -733,10 +657,10 @@ int hostfs_symlink(struct inode *ino, struct dentry *dentry, const char *to) | |||
| 733 | char *file; | 657 | char *file; |
| 734 | int err; | 658 | int err; |
| 735 | 659 | ||
| 736 | if ((file = inode_dentry_name(ino, dentry)) == NULL) | 660 | if ((file = dentry_name(dentry)) == NULL) |
| 737 | return -ENOMEM; | 661 | return -ENOMEM; |
| 738 | err = make_symlink(file, to); | 662 | err = make_symlink(file, to); |
| 739 | kfree(file); | 663 | __putname(file); |
| 740 | return err; | 664 | return err; |
| 741 | } | 665 | } |
| 742 | 666 | ||
| @@ -745,10 +669,10 @@ int hostfs_mkdir(struct inode *ino, struct dentry *dentry, int mode) | |||
| 745 | char *file; | 669 | char *file; |
| 746 | int err; | 670 | int err; |
| 747 | 671 | ||
| 748 | if ((file = inode_dentry_name(ino, dentry)) == NULL) | 672 | if ((file = dentry_name(dentry)) == NULL) |
| 749 | return -ENOMEM; | 673 | return -ENOMEM; |
| 750 | err = do_mkdir(file, mode); | 674 | err = do_mkdir(file, mode); |
| 751 | kfree(file); | 675 | __putname(file); |
| 752 | return err; | 676 | return err; |
| 753 | } | 677 | } |
| 754 | 678 | ||
| @@ -757,10 +681,10 @@ int hostfs_rmdir(struct inode *ino, struct dentry *dentry) | |||
| 757 | char *file; | 681 | char *file; |
| 758 | int err; | 682 | int err; |
| 759 | 683 | ||
| 760 | if ((file = inode_dentry_name(ino, dentry)) == NULL) | 684 | if ((file = dentry_name(dentry)) == NULL) |
| 761 | return -ENOMEM; | 685 | return -ENOMEM; |
| 762 | err = do_rmdir(file); | 686 | err = do_rmdir(file); |
| 763 | kfree(file); | 687 | __putname(file); |
| 764 | return err; | 688 | return err; |
| 765 | } | 689 | } |
| 766 | 690 | ||
| @@ -776,22 +700,20 @@ int hostfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev) | |||
| 776 | goto out; | 700 | goto out; |
| 777 | } | 701 | } |
| 778 | 702 | ||
| 779 | err = init_inode(inode, dentry); | ||
| 780 | if (err) | ||
| 781 | goto out_put; | ||
| 782 | |||
| 783 | err = -ENOMEM; | 703 | err = -ENOMEM; |
| 784 | name = dentry_name(dentry, 0); | 704 | name = dentry_name(dentry); |
| 785 | if (name == NULL) | 705 | if (name == NULL) |
| 786 | goto out_put; | 706 | goto out_put; |
| 787 | 707 | ||
| 788 | init_special_inode(inode, mode, dev); | 708 | init_special_inode(inode, mode, dev); |
| 789 | err = do_mknod(name, mode, MAJOR(dev), MINOR(dev)); | 709 | err = do_mknod(name, mode, MAJOR(dev), MINOR(dev)); |
| 790 | if (err) | 710 | if (!err) |
| 791 | goto out_free; | 711 | goto out_free; |
| 792 | 712 | ||
| 793 | err = read_name(inode, name); | 713 | err = read_name(inode, name); |
| 794 | kfree(name); | 714 | __putname(name); |
| 715 | if (err) | ||
| 716 | goto out_put; | ||
| 795 | if (err) | 717 | if (err) |
| 796 | goto out_put; | 718 | goto out_put; |
| 797 | 719 | ||
| @@ -799,7 +721,7 @@ int hostfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev) | |||
| 799 | return 0; | 721 | return 0; |
| 800 | 722 | ||
| 801 | out_free: | 723 | out_free: |
| 802 | kfree(name); | 724 | __putname(name); |
| 803 | out_put: | 725 | out_put: |
| 804 | iput(inode); | 726 | iput(inode); |
| 805 | out: | 727 | out: |
| @@ -812,15 +734,15 @@ int hostfs_rename(struct inode *from_ino, struct dentry *from, | |||
| 812 | char *from_name, *to_name; | 734 | char *from_name, *to_name; |
| 813 | int err; | 735 | int err; |
| 814 | 736 | ||
| 815 | if ((from_name = inode_dentry_name(from_ino, from)) == NULL) | 737 | if ((from_name = dentry_name(from)) == NULL) |
| 816 | return -ENOMEM; | 738 | return -ENOMEM; |
| 817 | if ((to_name = inode_dentry_name(to_ino, to)) == NULL) { | 739 | if ((to_name = dentry_name(to)) == NULL) { |
| 818 | kfree(from_name); | 740 | __putname(from_name); |
| 819 | return -ENOMEM; | 741 | return -ENOMEM; |
| 820 | } | 742 | } |
| 821 | err = rename_file(from_name, to_name); | 743 | err = rename_file(from_name, to_name); |
| 822 | kfree(from_name); | 744 | __putname(from_name); |
| 823 | kfree(to_name); | 745 | __putname(to_name); |
| 824 | return err; | 746 | return err; |
| 825 | } | 747 | } |
| 826 | 748 | ||
| @@ -832,7 +754,7 @@ int hostfs_permission(struct inode *ino, int desired) | |||
| 832 | if (desired & MAY_READ) r = 1; | 754 | if (desired & MAY_READ) r = 1; |
| 833 | if (desired & MAY_WRITE) w = 1; | 755 | if (desired & MAY_WRITE) w = 1; |
| 834 | if (desired & MAY_EXEC) x = 1; | 756 | if (desired & MAY_EXEC) x = 1; |
| 835 | name = inode_name(ino, 0); | 757 | name = inode_name(ino); |
| 836 | if (name == NULL) | 758 | if (name == NULL) |
| 837 | return -ENOMEM; | 759 | return -ENOMEM; |
| 838 | 760 | ||
| @@ -841,7 +763,7 @@ int hostfs_permission(struct inode *ino, int desired) | |||
| 841 | err = 0; | 763 | err = 0; |
| 842 | else | 764 | else |
| 843 | err = access_file(name, r, w, x); | 765 | err = access_file(name, r, w, x); |
| 844 | kfree(name); | 766 | __putname(name); |
| 845 | if (!err) | 767 | if (!err) |
| 846 | err = generic_permission(ino, desired, NULL); | 768 | err = generic_permission(ino, desired, NULL); |
| 847 | return err; | 769 | return err; |
| @@ -849,13 +771,14 @@ int hostfs_permission(struct inode *ino, int desired) | |||
| 849 | 771 | ||
| 850 | int hostfs_setattr(struct dentry *dentry, struct iattr *attr) | 772 | int hostfs_setattr(struct dentry *dentry, struct iattr *attr) |
| 851 | { | 773 | { |
| 774 | struct inode *inode = dentry->d_inode; | ||
| 852 | struct hostfs_iattr attrs; | 775 | struct hostfs_iattr attrs; |
| 853 | char *name; | 776 | char *name; |
| 854 | int err; | 777 | int err; |
| 855 | 778 | ||
| 856 | int fd = HOSTFS_I(dentry->d_inode)->fd; | 779 | int fd = HOSTFS_I(inode)->fd; |
| 857 | 780 | ||
| 858 | err = inode_change_ok(dentry->d_inode, attr); | 781 | err = inode_change_ok(inode, attr); |
| 859 | if (err) | 782 | if (err) |
| 860 | return err; | 783 | return err; |
| 861 | 784 | ||
| @@ -897,15 +820,26 @@ int hostfs_setattr(struct dentry *dentry, struct iattr *attr) | |||
| 897 | if (attr->ia_valid & ATTR_MTIME_SET) { | 820 | if (attr->ia_valid & ATTR_MTIME_SET) { |
| 898 | attrs.ia_valid |= HOSTFS_ATTR_MTIME_SET; | 821 | attrs.ia_valid |= HOSTFS_ATTR_MTIME_SET; |
| 899 | } | 822 | } |
| 900 | name = dentry_name(dentry, 0); | 823 | name = dentry_name(dentry); |
| 901 | if (name == NULL) | 824 | if (name == NULL) |
| 902 | return -ENOMEM; | 825 | return -ENOMEM; |
| 903 | err = set_attr(name, &attrs, fd); | 826 | err = set_attr(name, &attrs, fd); |
| 904 | kfree(name); | 827 | __putname(name); |
| 905 | if (err) | 828 | if (err) |
| 906 | return err; | 829 | return err; |
| 907 | 830 | ||
| 908 | return inode_setattr(dentry->d_inode, attr); | 831 | if ((attr->ia_valid & ATTR_SIZE) && |
| 832 | attr->ia_size != i_size_read(inode)) { | ||
| 833 | int error; | ||
| 834 | |||
| 835 | error = vmtruncate(inode, attr->ia_size); | ||
| 836 | if (err) | ||
| 837 | return err; | ||
| 838 | } | ||
| 839 | |||
| 840 | setattr_copy(inode, attr); | ||
| 841 | mark_inode_dirty(inode); | ||
| 842 | return 0; | ||
| 909 | } | 843 | } |
| 910 | 844 | ||
| 911 | static const struct inode_operations hostfs_iops = { | 845 | static const struct inode_operations hostfs_iops = { |
| @@ -935,32 +869,41 @@ static const struct inode_operations hostfs_dir_iops = { | |||
| 935 | .setattr = hostfs_setattr, | 869 | .setattr = hostfs_setattr, |
| 936 | }; | 870 | }; |
| 937 | 871 | ||
| 938 | int hostfs_link_readpage(struct file *file, struct page *page) | 872 | static void *hostfs_follow_link(struct dentry *dentry, struct nameidata *nd) |
| 939 | { | 873 | { |
| 940 | char *buffer, *name; | 874 | char *link = __getname(); |
| 941 | int err; | 875 | if (link) { |
| 942 | 876 | char *path = dentry_name(dentry); | |
| 943 | buffer = kmap(page); | 877 | int err = -ENOMEM; |
| 944 | name = inode_name(page->mapping->host, 0); | 878 | if (path) { |
| 945 | if (name == NULL) | 879 | int err = hostfs_do_readlink(path, link, PATH_MAX); |
| 946 | return -ENOMEM; | 880 | if (err == PATH_MAX) |
| 947 | err = hostfs_do_readlink(name, buffer, PAGE_CACHE_SIZE); | 881 | err = -E2BIG; |
| 948 | kfree(name); | 882 | __putname(path); |
| 949 | if (err == PAGE_CACHE_SIZE) | 883 | } |
| 950 | err = -E2BIG; | 884 | if (err < 0) { |
| 951 | else if (err > 0) { | 885 | __putname(link); |
| 952 | flush_dcache_page(page); | 886 | link = ERR_PTR(err); |
| 953 | SetPageUptodate(page); | 887 | } |
| 954 | if (PageError(page)) ClearPageError(page); | 888 | } else { |
| 955 | err = 0; | 889 | link = ERR_PTR(-ENOMEM); |
| 956 | } | 890 | } |
| 957 | kunmap(page); | 891 | |
| 958 | unlock_page(page); | 892 | nd_set_link(nd, link); |
| 959 | return err; | 893 | return NULL; |
| 960 | } | 894 | } |
| 961 | 895 | ||
| 962 | static const struct address_space_operations hostfs_link_aops = { | 896 | static void hostfs_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) |
| 963 | .readpage = hostfs_link_readpage, | 897 | { |
| 898 | char *s = nd_get_link(nd); | ||
| 899 | if (!IS_ERR(s)) | ||
| 900 | __putname(s); | ||
| 901 | } | ||
| 902 | |||
| 903 | static const struct inode_operations hostfs_link_iops = { | ||
| 904 | .readlink = generic_readlink, | ||
| 905 | .follow_link = hostfs_follow_link, | ||
| 906 | .put_link = hostfs_put_link, | ||
| 964 | }; | 907 | }; |
| 965 | 908 | ||
| 966 | static int hostfs_fill_sb_common(struct super_block *sb, void *d, int silent) | 909 | static int hostfs_fill_sb_common(struct super_block *sb, void *d, int silent) |
| @@ -980,49 +923,41 @@ static int hostfs_fill_sb_common(struct super_block *sb, void *d, int silent) | |||
| 980 | req_root = ""; | 923 | req_root = ""; |
| 981 | 924 | ||
| 982 | err = -ENOMEM; | 925 | err = -ENOMEM; |
| 983 | host_root_path = kmalloc(strlen(root_ino) + 1 | 926 | sb->s_fs_info = host_root_path = |
| 984 | + strlen(req_root) + 1, GFP_KERNEL); | 927 | kmalloc(strlen(root_ino) + strlen(req_root) + 2, GFP_KERNEL); |
| 985 | if (host_root_path == NULL) | 928 | if (host_root_path == NULL) |
| 986 | goto out; | 929 | goto out; |
| 987 | 930 | ||
| 988 | sprintf(host_root_path, "%s/%s", root_ino, req_root); | 931 | sprintf(host_root_path, "%s/%s", root_ino, req_root); |
| 989 | 932 | ||
| 990 | root_inode = hostfs_iget(sb); | 933 | root_inode = new_inode(sb); |
| 991 | if (IS_ERR(root_inode)) { | 934 | if (!root_inode) |
| 992 | err = PTR_ERR(root_inode); | 935 | goto out; |
| 993 | goto out_free; | ||
| 994 | } | ||
| 995 | 936 | ||
| 996 | err = init_inode(root_inode, NULL); | 937 | err = read_name(root_inode, host_root_path); |
| 997 | if (err) | 938 | if (err) |
| 998 | goto out_put; | 939 | goto out_put; |
| 999 | 940 | ||
| 1000 | HOSTFS_I(root_inode)->host_filename = host_root_path; | 941 | if (S_ISLNK(root_inode->i_mode)) { |
| 1001 | /* | 942 | char *name = follow_link(host_root_path); |
| 1002 | * Avoid that in the error path, iput(root_inode) frees again | 943 | if (IS_ERR(name)) |
| 1003 | * host_root_path through hostfs_destroy_inode! | 944 | err = PTR_ERR(name); |
| 1004 | */ | 945 | else |
| 1005 | host_root_path = NULL; | 946 | err = read_name(root_inode, name); |
| 947 | kfree(name); | ||
| 948 | if (err) | ||
| 949 | goto out_put; | ||
| 950 | } | ||
| 1006 | 951 | ||
| 1007 | err = -ENOMEM; | 952 | err = -ENOMEM; |
| 1008 | sb->s_root = d_alloc_root(root_inode); | 953 | sb->s_root = d_alloc_root(root_inode); |
| 1009 | if (sb->s_root == NULL) | 954 | if (sb->s_root == NULL) |
| 1010 | goto out_put; | 955 | goto out_put; |
| 1011 | 956 | ||
| 1012 | err = hostfs_read_inode(root_inode); | ||
| 1013 | if (err) { | ||
| 1014 | /* No iput in this case because the dput does that for us */ | ||
| 1015 | dput(sb->s_root); | ||
| 1016 | sb->s_root = NULL; | ||
| 1017 | goto out; | ||
| 1018 | } | ||
| 1019 | |||
| 1020 | return 0; | 957 | return 0; |
| 1021 | 958 | ||
| 1022 | out_put: | 959 | out_put: |
| 1023 | iput(root_inode); | 960 | iput(root_inode); |
| 1024 | out_free: | ||
| 1025 | kfree(host_root_path); | ||
| 1026 | out: | 961 | out: |
| 1027 | return err; | 962 | return err; |
| 1028 | } | 963 | } |
| @@ -1034,11 +969,17 @@ static int hostfs_read_sb(struct file_system_type *type, | |||
| 1034 | return get_sb_nodev(type, flags, data, hostfs_fill_sb_common, mnt); | 969 | return get_sb_nodev(type, flags, data, hostfs_fill_sb_common, mnt); |
| 1035 | } | 970 | } |
| 1036 | 971 | ||
| 972 | static void hostfs_kill_sb(struct super_block *s) | ||
| 973 | { | ||
| 974 | kill_anon_super(s); | ||
| 975 | kfree(s->s_fs_info); | ||
| 976 | } | ||
| 977 | |||
| 1037 | static struct file_system_type hostfs_type = { | 978 | static struct file_system_type hostfs_type = { |
| 1038 | .owner = THIS_MODULE, | 979 | .owner = THIS_MODULE, |
| 1039 | .name = "hostfs", | 980 | .name = "hostfs", |
| 1040 | .get_sb = hostfs_read_sb, | 981 | .get_sb = hostfs_read_sb, |
| 1041 | .kill_sb = kill_anon_super, | 982 | .kill_sb = hostfs_kill_sb, |
| 1042 | .fs_flags = 0, | 983 | .fs_flags = 0, |
| 1043 | }; | 984 | }; |
| 1044 | 985 | ||
