diff options
Diffstat (limited to 'fs/hostfs')
| -rw-r--r-- | fs/hostfs/hostfs.h | 63 | ||||
| -rw-r--r-- | fs/hostfs/hostfs_kern.c | 1014 | ||||
| -rw-r--r-- | fs/hostfs/hostfs_user.c | 349 |
3 files changed, 702 insertions, 724 deletions
diff --git a/fs/hostfs/hostfs.h b/fs/hostfs/hostfs.h index cca3fb693f9..9c88da0e855 100644 --- a/fs/hostfs/hostfs.h +++ b/fs/hostfs/hostfs.h @@ -1,9 +1,10 @@ #ifndef __UM_FS_HOSTFS #define __UM_FS_HOSTFS -#include "os.h" +#include <os.h> -/* These are exactly the same definitions as in fs.h, but the names are +/* + * These are exactly the same definitions as in fs.h, but the names are * changed so that this file can be included in both kernel and user files. */ @@ -21,17 +22,13 @@ #define HOSTFS_ATTR_FORCE 512 /* Not a change, but a change it */ #define HOSTFS_ATTR_ATTR_FLAG 1024 -/* If you are very careful, you'll notice that these two are missing: +/* + * If you are very careful, you'll notice that these two are missing: * * #define ATTR_KILL_SUID 2048 * #define ATTR_KILL_SGID 4096 * - * and this is because they were added in 2.5 development in this patch: - * - * http://linux.bkbits.net:8080/linux-2.5/ - * cset@3caf4a12k4XgDzK7wyK-TGpSZ9u2Ww?nav=index.html - * |src/.|src/include|src/include/linux|related/include/linux/fs.h - * + * and this is because they were added in 2.5 development. * Actually, they are not needed by most ->setattr() methods - they are set by * callers of notify_change() to notify that the setuid/setgid bits must be * dropped. @@ -42,7 +39,7 @@ struct hostfs_iattr { unsigned int ia_valid; - mode_t ia_mode; + unsigned short ia_mode; uid_t ia_uid; gid_t ia_gid; loff_t ia_size; @@ -51,18 +48,29 @@ struct hostfs_iattr { struct timespec ia_ctime; }; -extern int stat_file(const char *path, unsigned long long *inode_out, - int *mode_out, int *nlink_out, int *uid_out, int *gid_out, - unsigned long long *size_out, struct timespec *atime_out, - struct timespec *mtime_out, struct timespec *ctime_out, - int *blksize_out, unsigned long long *blocks_out); +struct hostfs_stat { + unsigned long long ino; + unsigned int mode; + unsigned int nlink; + unsigned int uid; + unsigned int gid; + unsigned long long size; + struct timespec atime, mtime, ctime; + unsigned int blksize; + unsigned long long blocks; + unsigned int maj; + unsigned int min; +}; + +extern int stat_file(const char *path, struct hostfs_stat *p, int fd); extern int access_file(char *path, int r, int w, int x); extern int open_file(char *path, int r, int w, int append); -extern int file_type(const char *path, int *maj, int *min); extern void *open_dir(char *path, int *err_out); extern char *read_dir(void *stream, unsigned long long *pos, - unsigned long long *ino_out, int *len_out); + unsigned long long *ino_out, int *len_out, + unsigned int *type_out); extern void close_file(void *stream); +extern int replace_file(int oldfd, int fd); extern void close_dir(void *stream); extern int read_file(int fd, unsigned long long *offset, char *buf, int len); extern int write_file(int fd, unsigned long long *offset, const char *buf, @@ -71,30 +79,19 @@ extern int lseek_file(int fd, long long offset, int whence); extern int fsync_file(int fd, int datasync); extern int file_create(char *name, int ur, int uw, int ux, int gr, int gw, int gx, int or, int ow, int ox); -extern int set_attr(const char *file, struct hostfs_iattr *attrs); +extern int set_attr(const char *file, struct hostfs_iattr *attrs, int fd); extern int make_symlink(const char *from, const char *to); extern int unlink_file(const char *file); extern int do_mkdir(const char *file, int mode); extern int do_rmdir(const char *file); -extern int do_mknod(const char *file, int mode, int dev); +extern int do_mknod(const char *file, int mode, unsigned int major, + unsigned int minor); extern int link_file(const char *from, const char *to); -extern int do_readlink(char *file, char *buf, int size); +extern int hostfs_do_readlink(char *file, char *buf, int size); extern int rename_file(char *from, char *to); extern int do_statfs(char *root, long *bsize_out, long long *blocks_out, long long *bfree_out, long long *bavail_out, long long *files_out, long long *ffree_out, - void *fsid_out, int fsid_size, long *namelen_out, - long *spare_out); + void *fsid_out, int fsid_size, long *namelen_out); #endif - -/* - * Overrides for Emacs so that we follow Linus's tabbing style. - * Emacs will notice this stuff at the end of the file and automatically - * adjust the settings for this buffer only. This must remain at the end - * of the file. - * --------------------------------------------------------------------------- - * Local variables: - * c-file-style: "linux" - * End: - */ diff --git a/fs/hostfs/hostfs_kern.c b/fs/hostfs/hostfs_kern.c index dd711310626..bb529f3b7f2 100644 --- a/fs/hostfs/hostfs_kern.c +++ b/fs/hostfs/hostfs_kern.c @@ -1,61 +1,45 @@ /* - * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com) + * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) * Licensed under the GPL * * Ported the filesystem routines to 2.5. * 2003-02-10 Petr Baudis <pasky@ucw.cz> */ -#include <linux/stddef.h> #include <linux/fs.h> -#include <linux/version.h> +#include <linux/magic.h> #include <linux/module.h> -#include <linux/init.h> -#include <linux/slab.h> +#include <linux/mm.h> #include <linux/pagemap.h> -#include <linux/blkdev.h> -#include <linux/list.h> #include <linux/statfs.h> -#include <linux/kdev_t.h> -#include <asm/uaccess.h> +#include <linux/slab.h> +#include <linux/seq_file.h> +#include <linux/mount.h> +#include <linux/namei.h> #include "hostfs.h" -#include "kern_util.h" -#include "kern.h" -#include "user_util.h" -#include "init.h" +#include <init.h> +#include <kern.h> struct hostfs_inode_info { - char *host_filename; int fd; - int mode; + fmode_t mode; struct inode vfs_inode; }; static inline struct hostfs_inode_info *HOSTFS_I(struct inode *inode) { - return(list_entry(inode, struct hostfs_inode_info, vfs_inode)); -} - -#define FILE_HOSTFS_I(file) HOSTFS_I((file)->f_dentry->d_inode) - -int hostfs_d_delete(struct dentry *dentry) -{ - return(1); + return list_entry(inode, struct hostfs_inode_info, vfs_inode); } -struct dentry_operations hostfs_dentry_ops = { - .d_delete = hostfs_d_delete, -}; +#define FILE_HOSTFS_I(file) HOSTFS_I(file_inode(file)) /* Changed in hostfs_args before the kernel starts running */ -static char *root_ino = "/"; +static char *root_ino = ""; static int append = 0; -#define HOSTFS_SUPER_MAGIC 0x00c0ffee - -static struct inode_operations hostfs_iops; -static struct inode_operations hostfs_dir_iops; -static struct address_space_operations hostfs_link_aops; +static const struct inode_operations hostfs_iops; +static const struct inode_operations hostfs_dir_iops; +static const struct inode_operations hostfs_link_iops; #ifndef MODULE static int __init hostfs_args(char *options, int *add) @@ -63,25 +47,25 @@ static int __init hostfs_args(char *options, int *add) char *ptr; ptr = strchr(options, ','); - if(ptr != NULL) + if (ptr != NULL) *ptr++ = '\0'; - if(*options != '\0') + if (*options != '\0') root_ino = options; options = ptr; - while(options){ + while (options) { ptr = strchr(options, ','); - if(ptr != NULL) + if (ptr != NULL) *ptr++ = '\0'; - if(*options != '\0'){ - if(!strcmp(options, "append")) + if (*options != '\0') { + if (!strcmp(options, "append")) append = 1; else printf("hostfs_args - unsupported option - %s\n", options); } options = ptr; } - return(0); + return 0; } __uml_setup("hostfs=", hostfs_args, @@ -96,70 +80,54 @@ __uml_setup("hostfs=", hostfs_args, ); #endif -static char *dentry_name(struct dentry *dentry, int extra) +static char *__dentry_name(struct dentry *dentry, char *name) { - struct dentry *parent; - char *root, *name; - int len; - - len = 0; - parent = dentry; - while(parent->d_parent != parent){ - len += parent->d_name.len + 1; - parent = parent->d_parent; - } + char *p = dentry_path_raw(dentry, name, PATH_MAX); + char *root; + size_t len; - root = HOSTFS_I(parent->d_inode)->host_filename; - len += strlen(root); - name = kmalloc(len + extra + 1, GFP_KERNEL); - if(name == NULL) return(NULL); - - name[len] = '\0'; - parent = dentry; - while(parent->d_parent != parent){ - len -= parent->d_name.len + 1; - name[len] = '/'; - strncpy(&name[len + 1], parent->d_name.name, - parent->d_name.len); - parent = parent->d_parent; + root = dentry->d_sb->s_fs_info; + len = strlen(root); + if (IS_ERR(p)) { + __putname(name); + return NULL; } - strncpy(name, root, strlen(root)); - return(name); + strlcpy(name, root, PATH_MAX); + if (len > p - name) { + __putname(name); + return NULL; + } + if (p > name + len) { + char *s = name + len; + while ((*s++ = *p++) != '\0') + ; + } + return name; } -static char *inode_name(struct inode *ino, int extra) +static char *dentry_name(struct dentry *dentry) { - struct dentry *dentry; + char *name = __getname(); + if (!name) + return NULL; - dentry = list_entry(ino->i_dentry.next, struct dentry, d_alias); - return(dentry_name(dentry, extra)); + return __dentry_name(dentry, name); } -static int read_name(struct inode *ino, char *name) +static char *inode_name(struct inode *ino) { - /* The non-int inode fields are copied into ints by stat_file and - * then copied into the inode because passing the actual pointers - * in and having them treated as int * breaks on big-endian machines - */ - int err; - int i_mode, i_nlink, i_blksize; - unsigned long long i_size; - unsigned long long i_ino; - unsigned long long i_blocks; - - err = stat_file(name, &i_ino, &i_mode, &i_nlink, &ino->i_uid, - &ino->i_gid, &i_size, &ino->i_atime, &ino->i_mtime, - &ino->i_ctime, &i_blksize, &i_blocks); - if(err) - return(err); - - ino->i_ino = i_ino; - ino->i_mode = i_mode; - ino->i_nlink = i_nlink; - ino->i_size = i_size; - ino->i_blksize = i_blksize; - ino->i_blocks = i_blocks; - return(0); + struct dentry *dentry; + char *name; + + dentry = d_find_alias(ino); + if (!dentry) + return NULL; + + name = dentry_name(dentry); + + dput(dentry); + + return name; } static char *follow_link(char *link) @@ -168,33 +136,33 @@ static char *follow_link(char *link) char *name, *resolved, *end; len = 64; - while(1){ + while (1) { n = -ENOMEM; name = kmalloc(len, GFP_KERNEL); - if(name == NULL) + if (name == NULL) goto out; - n = do_readlink(link, name, len); - if(n < len) + n = hostfs_do_readlink(link, name, len); + if (n < len) break; len *= 2; kfree(name); } - if(n < 0) + if (n < 0) goto out_free; - if(*name == '/') - return(name); + if (*name == '/') + return name; end = strrchr(link, '/'); - if(end == NULL) - return(name); + if (end == NULL) + return name; *(end + 1) = '\0'; len = strlen(link) + strlen(name) + 1; resolved = kmalloc(len, GFP_KERNEL); - if(resolved == NULL){ + if (resolved == NULL) { n = -ENOMEM; goto out_free; } @@ -202,47 +170,26 @@ static char *follow_link(char *link) sprintf(resolved, "%s%s", link, name); kfree(name); kfree(link); - return(resolved); + return resolved; out_free: kfree(name); out: - return(ERR_PTR(n)); + return ERR_PTR(n); } -static int read_inode(struct inode *ino) +static struct inode *hostfs_iget(struct super_block *sb) { - char *name; - int err = 0; - - /* Unfortunately, we are called from iget() when we don't have a dentry - * allocated yet. - */ - if(list_empty(&ino->i_dentry)) - goto out; - - err = -ENOMEM; - name = inode_name(ino, 0); - if(name == NULL) - goto out; - - if(file_type(name, NULL, NULL) == OS_TYPE_SYMLINK){ - name = follow_link(name); - if(IS_ERR(name)){ - err = PTR_ERR(name); - goto out; - } - } - - err = read_name(ino, name); - kfree(name); - out: - return(err); + struct inode *inode = new_inode(sb); + if (!inode) + return ERR_PTR(-ENOMEM); + return inode; } -int hostfs_statfs(struct super_block *sb, struct kstatfs *sf) +static int hostfs_statfs(struct dentry *dentry, struct kstatfs *sf) { - /* do_statfs uses struct statfs64 internally, but the linux kernel + /* + * do_statfs uses struct statfs64 internally, but the linux kernel * struct statfs still has 32-bit versions for most of these fields, * so we convert them here */ @@ -253,18 +200,19 @@ int hostfs_statfs(struct super_block *sb, struct kstatfs *sf) long long f_files; long long f_ffree; - err = do_statfs(HOSTFS_I(sb->s_root->d_inode)->host_filename, + err = do_statfs(dentry->d_sb->s_fs_info, &sf->f_bsize, &f_blocks, &f_bfree, &f_bavail, &f_files, &f_ffree, &sf->f_fsid, sizeof(sf->f_fsid), - &sf->f_namelen, sf->f_spare); - if(err) return(err); + &sf->f_namelen); + if (err) + return err; sf->f_blocks = f_blocks; sf->f_bfree = f_bfree; sf->f_bavail = f_bavail; sf->f_files = f_files; sf->f_ffree = f_ffree; sf->f_type = HOSTFS_SUPER_MAGIC; - return(0); + return 0; } static struct inode *hostfs_alloc_inode(struct super_block *sb) @@ -272,142 +220,182 @@ static struct inode *hostfs_alloc_inode(struct super_block *sb) struct hostfs_inode_info *hi; hi = kmalloc(sizeof(*hi), GFP_KERNEL); - if(hi == NULL) - return(NULL); - - *hi = ((struct hostfs_inode_info) { .host_filename = NULL, - .fd = -1, - .mode = 0 }); + if (hi == NULL) + return NULL; + hi->fd = -1; + hi->mode = 0; inode_init_once(&hi->vfs_inode); - return(&hi->vfs_inode); + return &hi->vfs_inode; } -static void hostfs_delete_inode(struct inode *inode) +static void hostfs_evict_inode(struct inode *inode) { - truncate_inode_pages(&inode->i_data, 0); - if(HOSTFS_I(inode)->fd != -1) { + truncate_inode_pages_final(&inode->i_data); + clear_inode(inode); + if (HOSTFS_I(inode)->fd != -1) { close_file(&HOSTFS_I(inode)->fd); HOSTFS_I(inode)->fd = -1; } - clear_inode(inode); } -static void hostfs_destroy_inode(struct inode *inode) +static void hostfs_i_callback(struct rcu_head *head) { - if(HOSTFS_I(inode)->host_filename) - kfree(HOSTFS_I(inode)->host_filename); - - /*XXX: This should not happen, probably. The check is here for - * additional safety.*/ - if(HOSTFS_I(inode)->fd != -1) { - close_file(&HOSTFS_I(inode)->fd); - printk(KERN_DEBUG "Closing host fd in .destroy_inode\n"); - } - + struct inode *inode = container_of(head, struct inode, i_rcu); kfree(HOSTFS_I(inode)); } -static void hostfs_read_inode(struct inode *inode) +static void hostfs_destroy_inode(struct inode *inode) +{ + call_rcu(&inode->i_rcu, hostfs_i_callback); +} + +static int hostfs_show_options(struct seq_file *seq, struct dentry *root) { - read_inode(inode); + const char *root_path = root->d_sb->s_fs_info; + size_t offset = strlen(root_ino) + 1; + + if (strlen(root_path) > offset) + seq_printf(seq, ",%s", root_path + offset); + + return 0; } -static struct super_operations hostfs_sbops = { +static const struct super_operations hostfs_sbops = { .alloc_inode = hostfs_alloc_inode, - .drop_inode = generic_delete_inode, - .delete_inode = hostfs_delete_inode, .destroy_inode = hostfs_destroy_inode, - .read_inode = hostfs_read_inode, + .evict_inode = hostfs_evict_inode, .statfs = hostfs_statfs, + .show_options = hostfs_show_options, }; -int hostfs_readdir(struct file *file, void *ent, filldir_t filldir) +static int hostfs_readdir(struct file *file, struct dir_context *ctx) { void *dir; char *name; unsigned long long next, ino; int error, len; + unsigned int type; - name = dentry_name(file->f_dentry, 0); - if(name == NULL) return(-ENOMEM); + name = dentry_name(file->f_path.dentry); + if (name == NULL) + return -ENOMEM; dir = open_dir(name, &error); - kfree(name); - if(dir == NULL) return(-error); - next = file->f_pos; - while((name = read_dir(dir, &next, &ino, &len)) != NULL){ - error = (*filldir)(ent, name, len, file->f_pos, - ino, DT_UNKNOWN); - if(error) break; - file->f_pos = next; + __putname(name); + if (dir == NULL) + return -error; + next = ctx->pos; + while ((name = read_dir(dir, &next, &ino, &len, &type)) != NULL) { + if (!dir_emit(ctx, name, len, ino, type)) + break; + ctx->pos = next; } close_dir(dir); - return(0); + return 0; } -int hostfs_file_open(struct inode *ino, struct file *file) +static int hostfs_file_open(struct inode *ino, struct file *file) { + static DEFINE_MUTEX(open_mutex); char *name; - int mode = 0, r = 0, w = 0, fd; + fmode_t mode = 0; + int err; + int r = 0, w = 0, fd; mode = file->f_mode & (FMODE_READ | FMODE_WRITE); - if((mode & HOSTFS_I(ino)->mode) == mode) - return(0); + if ((mode & HOSTFS_I(ino)->mode) == mode) + return 0; - /* The file may already have been opened, but with the wrong access, - * so this resets things and reopens the file with the new access. - */ - if(HOSTFS_I(ino)->fd != -1){ - close_file(&HOSTFS_I(ino)->fd); - HOSTFS_I(ino)->fd = -1; - } + mode |= HOSTFS_I(ino)->mode; - HOSTFS_I(ino)->mode |= mode; - if(HOSTFS_I(ino)->mode & FMODE_READ) +retry: + if (mode & FMODE_READ) r = 1; - if(HOSTFS_I(ino)->mode & FMODE_WRITE) + if (mode & FMODE_WRITE) w = 1; - if(w) + if (w) r = 1; - name = dentry_name(file->f_dentry, 0); - if(name == NULL) - return(-ENOMEM); + name = dentry_name(file->f_path.dentry); + if (name == NULL) + return -ENOMEM; fd = open_file(name, r, w, append); - kfree(name); - if(fd < 0) return(fd); - FILE_HOSTFS_I(file)->fd = fd; + __putname(name); + if (fd < 0) + return fd; + + mutex_lock(&open_mutex); + /* somebody else had handled it first? */ + if ((mode & HOSTFS_I(ino)->mode) == mode) { + mutex_unlock(&open_mutex); + return 0; + } + if ((mode | HOSTFS_I(ino)->mode) != mode) { + mode |= HOSTFS_I(ino)->mode; + mutex_unlock(&open_mutex); + close_file(&fd); + goto retry; + } + if (HOSTFS_I(ino)->fd == -1) { + HOSTFS_I(ino)->fd = fd; + } else { + err = replace_file(fd, HOSTFS_I(ino)->fd); + close_file(&fd); + if (err < 0) { + mutex_unlock(&open_mutex); + return err; + } + } + HOSTFS_I(ino)->mode = mode; + mutex_unlock(&open_mutex); + + return 0; +} + +static int hostfs_file_release(struct inode *inode, struct file *file) +{ + filemap_write_and_wait(inode->i_mapping); - return(0); + return 0; } -int hostfs_fsync(struct file *file, struct dentry *dentry, int datasync) +static int hostfs_fsync(struct file *file, loff_t start, loff_t end, + int datasync) { - return fsync_file(HOSTFS_I(dentry->d_inode)->fd, datasync); + struct inode *inode = file->f_mapping->host; + int ret; + + ret = filemap_write_and_wait_range(inode->i_mapping, start, end); + if (ret) + return ret; + + mutex_lock(&inode->i_mutex); + ret = fsync_file(HOSTFS_I(inode)->fd, datasync); + mutex_unlock(&inode->i_mutex); + + return ret; } -static struct file_operations hostfs_file_fops = { +static const struct file_operations hostfs_file_fops = { .llseek = generic_file_llseek, - .read = generic_file_read, - .sendfile = generic_file_sendfile, - .aio_read = generic_file_aio_read, - .aio_write = generic_file_aio_write, - .readv = generic_file_readv, - .writev = generic_file_writev, - .write = generic_file_write, + .read = new_sync_read, + .splice_read = generic_file_splice_read, + .read_iter = generic_file_read_iter, + .write_iter = generic_file_write_iter, + .write = new_sync_write, .mmap = generic_file_mmap, .open = hostfs_file_open, - .release = NULL, + .release = hostfs_file_release, .fsync = hostfs_fsync, }; -static struct file_operations hostfs_dir_fops = { +static const struct file_operations hostfs_dir_fops = { .llseek = generic_file_llseek, - .readdir = hostfs_readdir, + .iterate = hostfs_readdir, .read = generic_read_dir, }; -int hostfs_writepage(struct page *page, struct writeback_control *wbc) +static int hostfs_writepage(struct page *page, struct writeback_control *wbc) { struct address_space *mapping = page->mapping; struct inode *inode = mapping->host; @@ -424,7 +412,7 @@ int hostfs_writepage(struct page *page, struct writeback_control *wbc) base = ((unsigned long long) page->index) << PAGE_CACHE_SHIFT; err = write_file(HOSTFS_I(inode)->fd, &base, buffer, count); - if(err != count){ + if (err != count) { ClearPageUptodate(page); goto out; } @@ -443,7 +431,7 @@ int hostfs_writepage(struct page *page, struct writeback_control *wbc) return err; } -int hostfs_readpage(struct file *file, struct page *page) +static int hostfs_readpage(struct file *file, struct page *page) { char *buffer; long long start; @@ -453,7 +441,8 @@ int hostfs_readpage(struct file *file, struct page *page) buffer = kmap(page); err = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer, PAGE_CACHE_SIZE); - if(err < 0) goto out; + if (err < 0) + goto out; memset(&buffer[err], 0, PAGE_CACHE_SIZE - err); @@ -464,437 +453,412 @@ int hostfs_readpage(struct file *file, struct page *page) out: kunmap(page); unlock_page(page); - return(err); + return err; } -int hostfs_prepare_write(struct file *file, struct page *page, - unsigned int from, unsigned int to) +static int hostfs_write_begin(struct file *file, struct address_space *mapping, + loff_t pos, unsigned len, unsigned flags, + struct page **pagep, void **fsdata) { - char *buffer; - long long start, tmp; - int err; + pgoff_t index = pos >> PAGE_CACHE_SHIFT; - start = (long long) page->index << PAGE_CACHE_SHIFT; - buffer = kmap(page); - if(from != 0){ - tmp = start; - err = read_file(FILE_HOSTFS_I(file)->fd, &tmp, buffer, - from); - if(err < 0) goto out; - } - if(to != PAGE_CACHE_SIZE){ - start += to; - err = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer + to, - PAGE_CACHE_SIZE - to); - if(err < 0) goto out; - } - err = 0; - out: - kunmap(page); - return(err); + *pagep = grab_cache_page_write_begin(mapping, index, flags); + if (!*pagep) + return -ENOMEM; + return 0; } -int hostfs_commit_write(struct file *file, struct page *page, unsigned from, - unsigned to) +static int hostfs_write_end(struct file *file, struct address_space *mapping, + loff_t pos, unsigned len, unsigned copied, + struct page *page, void *fsdata) { - struct address_space *mapping = page->mapping; struct inode *inode = mapping->host; - char *buffer; - long long start; - int err = 0; + void *buffer; + unsigned from = pos & (PAGE_CACHE_SIZE - 1); + int err; - start = (long long) (page->index << PAGE_CACHE_SHIFT) + from; buffer = kmap(page); - err = write_file(FILE_HOSTFS_I(file)->fd, &start, buffer + from, - to - from); - if(err > 0) err = 0; - if(!err && (start > inode->i_size)) - inode->i_size = start; - + err = write_file(FILE_HOSTFS_I(file)->fd, &pos, buffer + from, copied); kunmap(page); - return(err); + + if (!PageUptodate(page) && err == PAGE_CACHE_SIZE) + SetPageUptodate(page); + + /* + * If err > 0, write_file has added err to pos, so we are comparing + * i_size against the last byte written. + */ + if (err > 0 && (pos > inode->i_size)) + inode->i_size = pos; + unlock_page(page); + page_cache_release(page); + + return err; } -static struct address_space_operations hostfs_aops = { +static const struct address_space_operations hostfs_aops = { .writepage = hostfs_writepage, .readpage = hostfs_readpage, .set_page_dirty = __set_page_dirty_nobuffers, - .prepare_write = hostfs_prepare_write, - .commit_write = hostfs_commit_write + .write_begin = hostfs_write_begin, + .write_end = hostfs_write_end, }; -static int init_inode(struct inode *inode, struct dentry *dentry) +static int read_name(struct inode *ino, char *name) { - char *name; - int type, err = -ENOMEM; - int maj, min; - dev_t rdev = 0; + dev_t rdev; + struct hostfs_stat st; + int err = stat_file(name, &st, -1); + if (err) + return err; - if(dentry){ - name = dentry_name(dentry, 0); - if(name == NULL) - goto out; - type = file_type(name, &maj, &min); - /*Reencode maj and min with the kernel encoding.*/ - rdev = MKDEV(maj, min); - kfree(name); - } - else type = OS_TYPE_DIR; + /* Reencode maj and min with the kernel encoding.*/ + rdev = MKDEV(st.maj, st.min); - err = 0; - if(type == OS_TYPE_SYMLINK) - inode->i_op = &page_symlink_inode_operations; - else if(type == OS_TYPE_DIR) - inode->i_op = &hostfs_dir_iops; - else inode->i_op = &hostfs_iops; - - if(type == OS_TYPE_DIR) inode->i_fop = &hostfs_dir_fops; - else inode->i_fop = &hostfs_file_fops; - - if(type == OS_TYPE_SYMLINK) - inode->i_mapping->a_ops = &hostfs_link_aops; - else inode->i_mapping->a_ops = &hostfs_aops; - - switch (type) { - case OS_TYPE_CHARDEV: - init_special_inode(inode, S_IFCHR, rdev); + switch (st.mode & S_IFMT) { + case S_IFLNK: + ino->i_op = &hostfs_link_iops; break; - case OS_TYPE_BLOCKDEV: - init_special_inode(inode, S_IFBLK, rdev); + case S_IFDIR: + ino->i_op = &hostfs_dir_iops; + ino->i_fop = &hostfs_dir_fops; break; - case OS_TYPE_FIFO: - init_special_inode(inode, S_IFIFO, 0); - break; - case OS_TYPE_SOCK: - init_special_inode(inode, S_IFSOCK, 0); + case S_IFCHR: + case S_IFBLK: + case S_IFIFO: + case S_IFSOCK: + init_special_inode(ino, st.mode & S_IFMT, rdev); + ino->i_op = &hostfs_iops; break; + + default: + ino->i_op = &hostfs_iops; + ino->i_fop = &hostfs_file_fops; + ino->i_mapping->a_ops = &hostfs_aops; } - out: - return(err); + + ino->i_ino = st.ino; + ino->i_mode = st.mode; + set_nlink(ino, st.nlink); + i_uid_write(ino, st.uid); + i_gid_write(ino, st.gid); + ino->i_atime = st.atime; + ino->i_mtime = st.mtime; + ino->i_ctime = st.ctime; + ino->i_size = st.size; + ino->i_blocks = st.blocks; + return 0; } -int hostfs_create(struct inode *dir, struct dentry *dentry, int mode, - struct nameidata *nd) +static int hostfs_create(struct inode *dir, struct dentry *dentry, umode_t mode, + bool excl) { struct inode *inode; char *name; int error, fd; - error = -ENOMEM; - inode = iget(dir->i_sb, 0); - if(inode == NULL) goto out; - - error = init_inode(inode, dentry); - if(error) - goto out_put; + inode = hostfs_iget(dir->i_sb); + if (IS_ERR(inode)) { + error = PTR_ERR(inode); + goto out; + } error = -ENOMEM; - name = dentry_name(dentry, 0); - if(name == NULL) + name = dentry_name(dentry); + if (name == NULL) goto out_put; fd = file_create(name, mode & S_IRUSR, mode & S_IWUSR, mode & S_IXUSR, mode & S_IRGRP, mode & S_IWGRP, mode & S_IXGRP, mode & S_IROTH, mode & S_IWOTH, mode & S_IXOTH); - if(fd < 0) + if (fd < 0) error = fd; - else error = read_name(inode, name); + else + error = read_name(inode, name); - kfree(name); - if(error) + __putname(name); + if (error) goto out_put; HOSTFS_I(inode)->fd = fd; HOSTFS_I(inode)->mode = FMODE_READ | FMODE_WRITE; d_instantiate(dentry, inode); - return(0); + return 0; out_put: iput(inode); out: - return(error); + return error; } -struct dentry *hostfs_lookup(struct inode *ino, struct dentry *dentry, - struct nameidata *nd) +static struct dentry *hostfs_lookup(struct inode *ino, struct dentry *dentry, + unsigned int flags) { struct inode *inode; char *name; int err; - err = -ENOMEM; - inode = iget(ino->i_sb, 0); - if(inode == NULL) + inode = hostfs_iget(ino->i_sb); + if (IS_ERR(inode)) { + err = PTR_ERR(inode); goto out; - - err = init_inode(inode, dentry); - if(err) - goto out_put; + } err = -ENOMEM; - name = dentry_name(dentry, 0); - if(name == NULL) + name = dentry_name(dentry); + if (name == NULL) goto out_put; err = read_name(inode, name); - kfree(name); - if(err == -ENOENT){ + + __putname(name); + if (err == -ENOENT) { iput(inode); inode = NULL; } - else if(err) + else if (err) goto out_put; d_add(dentry, inode); - dentry->d_op = &hostfs_dentry_ops; - return(NULL); + return NULL; out_put: iput(inode); out: - return(ERR_PTR(err)); + return ERR_PTR(err); } -static char *inode_dentry_name(struct inode *ino, struct dentry *dentry) +static int hostfs_link(struct dentry *to, struct inode *ino, + struct dentry *from) { - char *file; - int len; - - file = inode_name(ino, dentry->d_name.len + 1); - if(file == NULL) return(NULL); - strcat(file, "/"); - len = strlen(file); - strncat(file, dentry->d_name.name, dentry->d_name.len); - file[len + dentry->d_name.len] = '\0'; - return(file); -} + char *from_name, *to_name; + int err; -int hostfs_link(struct dentry *to, struct inode *ino, struct dentry *from) -{ - char *from_name, *to_name; - int err; - - if((from_name = inode_dentry_name(ino, from)) == NULL) - return(-ENOMEM); - to_name = dentry_name(to, 0); - if(to_name == NULL){ - kfree(from_name); - return(-ENOMEM); + if ((from_name = dentry_name(from)) == NULL) + return -ENOMEM; + to_name = dentry_name(to); + if (to_name == NULL) { + __putname(from_name); + return -ENOMEM; } - err = link_file(to_name, from_name); - kfree(from_name); - kfree(to_name); - return(err); + err = link_file(to_name, from_name); + __putname(from_name); + __putname(to_name); + return err; } -int hostfs_unlink(struct inode *ino, struct dentry *dentry) +static int hostfs_unlink(struct inode *ino, struct dentry *dentry) { char *file; int err; - if((file = inode_dentry_name(ino, dentry)) == NULL) return(-ENOMEM); - if(append) - return(-EPERM); + if (append) + return -EPERM; + + if ((file = dentry_name(dentry)) == NULL) + return -ENOMEM; err = unlink_file(file); - kfree(file); - return(err); + __putname(file); + return err; } -int hostfs_symlink(struct inode *ino, struct dentry *dentry, const char *to) +static int hostfs_symlink(struct inode *ino, struct dentry *dentry, + const char *to) { char *file; int err; - if((file = inode_dentry_name(ino, dentry)) == NULL) return(-ENOMEM); + if ((file = dentry_name(dentry)) == NULL) + return -ENOMEM; err = make_symlink(file, to); - kfree(file); - return(err); + __putname(file); + return err; } -int hostfs_mkdir(struct inode *ino, struct dentry *dentry, int mode) +static int hostfs_mkdir(struct inode *ino, struct dentry *dentry, umode_t mode) { char *file; int err; - if((file = inode_dentry_name(ino, dentry)) == NULL) return(-ENOMEM); + if ((file = dentry_name(dentry)) == NULL) + return -ENOMEM; err = do_mkdir(file, mode); - kfree(file); - return(err); + __putname(file); + return err; } -int hostfs_rmdir(struct inode *ino, struct dentry *dentry) +static int hostfs_rmdir(struct inode *ino, struct dentry *dentry) { char *file; int err; - if((file = inode_dentry_name(ino, dentry)) == NULL) return(-ENOMEM); + if ((file = dentry_name(dentry)) == NULL) + return -ENOMEM; err = do_rmdir(file); - kfree(file); - return(err); + __putname(file); + return err; } -int hostfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev) +static int hostfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) { struct inode *inode; char *name; - int err = -ENOMEM; + int err; - inode = iget(dir->i_sb, 0); - if(inode == NULL) + inode = hostfs_iget(dir->i_sb); + if (IS_ERR(inode)) { + err = PTR_ERR(inode); goto out; - - err = init_inode(inode, dentry); - if(err) - goto out_put; + } err = -ENOMEM; - name = dentry_name(dentry, 0); - if(name == NULL) + name = dentry_name(dentry); + if (name == NULL) goto out_put; init_special_inode(inode, mode, dev); - err = do_mknod(name, mode, dev); - if(err) + err = do_mknod(name, mode, MAJOR(dev), MINOR(dev)); + if (!err) goto out_free; err = read_name(inode, name); - kfree(name); - if(err) + __putname(name); + if (err) + goto out_put; + if (err) goto out_put; d_instantiate(dentry, inode); - return(0); + return 0; out_free: - kfree(name); + __putname(name); out_put: iput(inode); out: - return(err); + return err; } -int hostfs_rename(struct inode *from_ino, struct dentry *from, - struct inode *to_ino, struct dentry *to) +static int hostfs_rename(struct inode *from_ino, struct dentry *from, + struct inode *to_ino, struct dentry *to) { char *from_name, *to_name; int err; - if((from_name = inode_dentry_name(from_ino, from)) == NULL) - return(-ENOMEM); - if((to_name = inode_dentry_name(to_ino, to)) == NULL){ - kfree(from_name); - return(-ENOMEM); + if ((from_name = dentry_name(from)) == NULL) + return -ENOMEM; + if ((to_name = dentry_name(to)) == NULL) { + __putname(from_name); + return -ENOMEM; } err = rename_file(from_name, to_name); - kfree(from_name); - kfree(to_name); - return(err); + __putname(from_name); + __putname(to_name); + return err; } -int hostfs_permission(struct inode *ino, int desired, struct nameidata *nd) +static int hostfs_permission(struct inode *ino, int desired) { char *name; int r = 0, w = 0, x = 0, err; + if (desired & MAY_NOT_BLOCK) + return -ECHILD; + if (desired & MAY_READ) r = 1; if (desired & MAY_WRITE) w = 1; if (desired & MAY_EXEC) x = 1; - name = inode_name(ino, 0); - if (name == NULL) return(-ENOMEM); + name = inode_name(ino); + if (name == NULL) + return -ENOMEM; if (S_ISCHR(ino->i_mode) || S_ISBLK(ino->i_mode) || - S_ISFIFO(ino->i_mode) || S_ISSOCK(ino->i_mode)) + S_ISFIFO(ino->i_mode) || S_ISSOCK(ino->i_mode)) err = 0; else err = access_file(name, r, w, x); - kfree(name); - if(!err) - err = generic_permission(ino, desired, NULL); + __putname(name); + if (!err) + err = generic_permission(ino, desired); return err; } -int hostfs_setattr(struct dentry *dentry, struct iattr *attr) +static int hostfs_setattr(struct dentry *dentry, struct iattr *attr) { + struct inode *inode = dentry->d_inode; struct hostfs_iattr attrs; char *name; int err; - err = inode_change_ok(dentry->d_inode, attr); + int fd = HOSTFS_I(inode)->fd; + + err = inode_change_ok(inode, attr); if (err) return err; - if(append) + if (append) attr->ia_valid &= ~ATTR_SIZE; attrs.ia_valid = 0; - if(attr->ia_valid & ATTR_MODE){ + if (attr->ia_valid & ATTR_MODE) { attrs.ia_valid |= HOSTFS_ATTR_MODE; attrs.ia_mode = attr->ia_mode; } - if(attr->ia_valid & ATTR_UID){ + if (attr->ia_valid & ATTR_UID) { attrs.ia_valid |= HOSTFS_ATTR_UID; - attrs.ia_uid = attr->ia_uid; + attrs.ia_uid = from_kuid(&init_user_ns, attr->ia_uid); } - if(attr->ia_valid & ATTR_GID){ + if (attr->ia_valid & ATTR_GID) { attrs.ia_valid |= HOSTFS_ATTR_GID; - attrs.ia_gid = attr->ia_gid; + attrs.ia_gid = from_kgid(&init_user_ns, attr->ia_gid); } - if(attr->ia_valid & ATTR_SIZE){ + if (attr->ia_valid & ATTR_SIZE) { attrs.ia_valid |= HOSTFS_ATTR_SIZE; attrs.ia_size = attr->ia_size; } - if(attr->ia_valid & ATTR_ATIME){ + if (attr->ia_valid & ATTR_ATIME) { attrs.ia_valid |= HOSTFS_ATTR_ATIME; attrs.ia_atime = attr->ia_atime; } - if(attr->ia_valid & ATTR_MTIME){ + if (attr->ia_valid & ATTR_MTIME) { attrs.ia_valid |= HOSTFS_ATTR_MTIME; attrs.ia_mtime = attr->ia_mtime; } - if(attr->ia_valid & ATTR_CTIME){ + if (attr->ia_valid & ATTR_CTIME) { attrs.ia_valid |= HOSTFS_ATTR_CTIME; attrs.ia_ctime = attr->ia_ctime; } - if(attr->ia_valid & ATTR_ATIME_SET){ + if (attr->ia_valid & ATTR_ATIME_SET) { attrs.ia_valid |= HOSTFS_ATTR_ATIME_SET; } - if(attr->ia_valid & ATTR_MTIME_SET){ + if (attr->ia_valid & ATTR_MTIME_SET) { attrs.ia_valid |= HOSTFS_ATTR_MTIME_SET; } - name = dentry_name(dentry, 0); - if(name == NULL) return(-ENOMEM); - err = set_attr(name, &attrs); - kfree(name); - if(err) - return(err); + name = dentry_name(dentry); + if (name == NULL) + return -ENOMEM; + err = set_attr(name, &attrs, fd); + __putname(name); + if (err) + return err; - return(inode_setattr(dentry->d_inode, attr)); -} + if ((attr->ia_valid & ATTR_SIZE) && + attr->ia_size != i_size_read(inode)) + truncate_setsize(inode, attr->ia_size); -int hostfs_getattr(struct vfsmount *mnt, struct dentry *dentry, - struct kstat *stat) -{ - generic_fillattr(dentry->d_inode, stat); - return(0); + setattr_copy(inode, attr); + mark_inode_dirty(inode); + return 0; } -static struct inode_operations hostfs_iops = { - .create = hostfs_create, - .link = hostfs_link, - .unlink = hostfs_unlink, - .symlink = hostfs_symlink, - .mkdir = hostfs_mkdir, - .rmdir = hostfs_rmdir, - .mknod = hostfs_mknod, - .rename = hostfs_rename, +static const struct inode_operations hostfs_iops = { .permission = hostfs_permission, .setattr = hostfs_setattr, - .getattr = hostfs_getattr, }; -static struct inode_operations hostfs_dir_iops = { +static const struct inode_operations hostfs_dir_iops = { .create = hostfs_create, .lookup = hostfs_lookup, .link = hostfs_link, @@ -906,110 +870,127 @@ static struct inode_operations hostfs_dir_iops = { .rename = hostfs_rename, .permission = hostfs_permission, .setattr = hostfs_setattr, - .getattr = hostfs_getattr, }; -int hostfs_link_readpage(struct file *file, struct page *page) -{ - char *buffer, *name; - long long start; - int err; - - start = page->index << PAGE_CACHE_SHIFT; - buffer = kmap(page); - name = inode_name(page->mapping->host, 0); - if(name == NULL) return(-ENOMEM); - err = do_readlink(name, buffer, PAGE_CACHE_SIZE); - kfree(name); - if(err == PAGE_CACHE_SIZE) - err = -E2BIG; - else if(err > 0){ - flush_dcache_page(page); - SetPageUptodate(page); - if (PageError(page)) ClearPageError(page); - err = 0; +static void *hostfs_follow_link(struct dentry *dentry, struct nameidata *nd) +{ + char *link = __getname(); + if (link) { + char *path = dentry_name(dentry); + int err = -ENOMEM; + if (path) { + err = hostfs_do_readlink(path, link, PATH_MAX); + if (err == PATH_MAX) + err = -E2BIG; + __putname(path); + } + if (err < 0) { + __putname(link); + link = ERR_PTR(err); + } + } else { + link = ERR_PTR(-ENOMEM); } - kunmap(page); - unlock_page(page); - return(err); + + nd_set_link(nd, link); + return NULL; +} + +static void hostfs_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) +{ + char *s = nd_get_link(nd); + if (!IS_ERR(s)) + __putname(s); } -static struct address_space_operations hostfs_link_aops = { - .readpage = hostfs_link_readpage, +static const struct inode_operations hostfs_link_iops = { + .readlink = generic_readlink, + .follow_link = hostfs_follow_link, + .put_link = hostfs_put_link, }; static int hostfs_fill_sb_common(struct super_block *sb, void *d, int silent) { struct inode *root_inode; - char *name, *data = d; + char *host_root_path, *req_root = d; int err; sb->s_blocksize = 1024; sb->s_blocksize_bits = 10; sb->s_magic = HOSTFS_SUPER_MAGIC; sb->s_op = &hostfs_sbops; + sb->s_d_op = &simple_dentry_operations; + sb->s_maxbytes = MAX_LFS_FILESIZE; - if((data == NULL) || (*data == '\0')) - data = root_ino; + /* NULL is printed as <NULL> by sprintf: avoid that. */ + if (req_root == NULL) + req_root = ""; err = -ENOMEM; - name = kmalloc(strlen(data) + 1, GFP_KERNEL); - if(name == NULL) + sb->s_fs_info = host_root_path = + kmalloc(strlen(root_ino) + strlen(req_root) + 2, GFP_KERNEL); + if (host_root_path == NULL) goto out; - strcpy(name, data); + sprintf(host_root_path, "%s/%s", root_ino, req_root); - root_inode = iget(sb, 0); - if(root_inode == NULL) - goto out_free; + root_inode = new_inode(sb); + if (!root_inode) + goto out; - err = init_inode(root_inode, NULL); - if(err) + err = read_name(root_inode, host_root_path); + if (err) goto out_put; - HOSTFS_I(root_inode)->host_filename = name; + if (S_ISLNK(root_inode->i_mode)) { + char *name = follow_link(host_root_path); + if (IS_ERR(name)) + err = PTR_ERR(name); + else + err = read_name(root_inode, name); + kfree(name); + if (err) + goto out_put; + } err = -ENOMEM; - sb->s_root = d_alloc_root(root_inode); - if(sb->s_root == NULL) - goto out_put; + sb->s_root = d_make_root(root_inode); + if (sb->s_root == NULL) + goto out; - err = read_inode(root_inode); - if(err){ - /* No iput in this case because the dput does that for us */ - dput(sb->s_root); - sb->s_root = NULL; - goto out_free; - } + return 0; - return(0); +out_put: + iput(root_inode); +out: + return err; +} - out_put: - iput(root_inode); - out_free: - kfree(name); - out: - return(err); +static struct dentry *hostfs_read_sb(struct file_system_type *type, + int flags, const char *dev_name, + void *data) +{ + return mount_nodev(type, flags, data, hostfs_fill_sb_common); } -static struct super_block *hostfs_read_sb(struct file_system_type *type, - int flags, const char *dev_name, - void *data) +static void hostfs_kill_sb(struct super_block *s) { - return(get_sb_nodev(type, flags, data, hostfs_fill_sb_common)); + kill_anon_super(s); + kfree(s->s_fs_info); } static struct file_system_type hostfs_type = { .owner = THIS_MODULE, .name = "hostfs", - .get_sb = hostfs_read_sb, - .kill_sb = kill_anon_super, + .mount = hostfs_read_sb, + .kill_sb = hostfs_kill_sb, .fs_flags = 0, }; +MODULE_ALIAS_FS("hostfs"); static int __init init_hostfs(void) { - return(register_filesystem(&hostfs_type)); + return register_filesystem(&hostfs_type); } static void __exit exit_hostfs(void) @@ -1020,14 +1001,3 @@ static void __exit exit_hostfs(void) module_init(init_hostfs) module_exit(exit_hostfs) MODULE_LICENSE("GPL"); - -/* - * Overrides for Emacs so that we follow Linus's tabbing style. - * Emacs will notice this stuff at the end of the file and automatically - * adjust the settings for this buffer only. This must remain at the end - * of the file. - * --------------------------------------------------------------------------- - * Local variables: - * c-file-style: "linux" - * End: - */ diff --git a/fs/hostfs/hostfs_user.c b/fs/hostfs/hostfs_user.c index b97809deba6..67838f3aa20 100644 --- a/fs/hostfs/hostfs_user.c +++ b/fs/hostfs/hostfs_user.c @@ -1,106 +1,89 @@ /* - * Copyright (C) 2000 Jeff Dike (jdike@karaya.com) + * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) * Licensed under the GPL */ -#include <unistd.h> #include <stdio.h> -#include <fcntl.h> +#include <stddef.h> +#include <unistd.h> #include <dirent.h> #include <errno.h> -#include <utime.h> +#include <fcntl.h> #include <string.h> #include <sys/stat.h> #include <sys/time.h> +#include <sys/types.h> #include <sys/vfs.h> #include "hostfs.h" -#include "kern_util.h" -#include "user.h" - -int stat_file(const char *path, unsigned long long *inode_out, int *mode_out, - int *nlink_out, int *uid_out, int *gid_out, - unsigned long long *size_out, struct timespec *atime_out, - struct timespec *mtime_out, struct timespec *ctime_out, - int *blksize_out, unsigned long long *blocks_out) -{ - struct stat64 buf; +#include <utime.h> - if(lstat64(path, &buf) < 0) - return(-errno); - - if(inode_out != NULL) *inode_out = buf.st_ino; - if(mode_out != NULL) *mode_out = buf.st_mode; - if(nlink_out != NULL) *nlink_out = buf.st_nlink; - if(uid_out != NULL) *uid_out = buf.st_uid; - if(gid_out != NULL) *gid_out = buf.st_gid; - if(size_out != NULL) *size_out = buf.st_size; - if(atime_out != NULL) { - atime_out->tv_sec = buf.st_atime; - atime_out->tv_nsec = 0; - } - if(mtime_out != NULL) { - mtime_out->tv_sec = buf.st_mtime; - mtime_out->tv_nsec = 0; - } - if(ctime_out != NULL) { - ctime_out->tv_sec = buf.st_ctime; - ctime_out->tv_nsec = 0; - } - if(blksize_out != NULL) *blksize_out = buf.st_blksize; - if(blocks_out != NULL) *blocks_out = buf.st_blocks; - return(0); +static void stat64_to_hostfs(const struct stat64 *buf, struct hostfs_stat *p) +{ + p->ino = buf->st_ino; + p->mode = buf->st_mode; + p->nlink = buf->st_nlink; + p->uid = buf->st_uid; + p->gid = buf->st_gid; + p->size = buf->st_size; + p->atime.tv_sec = buf->st_atime; + p->atime.tv_nsec = 0; + p->ctime.tv_sec = buf->st_ctime; + p->ctime.tv_nsec = 0; + p->mtime.tv_sec = buf->st_mtime; + p->mtime.tv_nsec = 0; + p->blksize = buf->st_blksize; + p->blocks = buf->st_blocks; + p->maj = os_major(buf->st_rdev); + p->min = os_minor(buf->st_rdev); } -int file_type(const char *path, int *maj, int *min) +int stat_file(const char *path, struct hostfs_stat *p, int fd) { - struct stat64 buf; - - if(lstat64(path, &buf) < 0) - return(-errno); - /*We cannot pass rdev as is because glibc and the kernel disagree - *about its definition.*/ - if(maj != NULL) - *maj = major(buf.st_rdev); - if(min != NULL) - *min = minor(buf.st_rdev); - - if(S_ISDIR(buf.st_mode)) return(OS_TYPE_DIR); - else if(S_ISLNK(buf.st_mode)) return(OS_TYPE_SYMLINK); - else if(S_ISCHR(buf.st_mode)) return(OS_TYPE_CHARDEV); - else if(S_ISBLK(buf.st_mode)) return(OS_TYPE_BLOCKDEV); - else if(S_ISFIFO(buf.st_mode))return(OS_TYPE_FIFO); - else if(S_ISSOCK(buf.st_mode))return(OS_TYPE_SOCK); - else return(OS_TYPE_FILE); + struct stat64 buf; + + if (fd >= 0) { + if (fstat64(fd, &buf) < 0) + return -errno; + } else if (lstat64(path, &buf) < 0) { + return -errno; + } + stat64_to_hostfs(&buf, p); + return 0; } int access_file(char *path, int r, int w, int x) { int mode = 0; - if(r) mode = R_OK; - if(w) mode |= W_OK; - if(x) mode |= X_OK; - if(access(path, mode) != 0) return(-errno); - else return(0); + if (r) + mode = R_OK; + if (w) + mode |= W_OK; + if (x) + mode |= X_OK; + if (access(path, mode) != 0) + return -errno; + else return 0; } int open_file(char *path, int r, int w, int append) { int mode = 0, fd; - if(r && !w) + if (r && !w) mode = O_RDONLY; - else if(!r && w) + else if (!r && w) mode = O_WRONLY; - else if(r && w) + else if (r && w) mode = O_RDWR; else panic("Impossible mode in open_file"); - if(append) + if (append) mode |= O_APPEND; fd = open64(path, mode); - if(fd < 0) return(-errno); - else return(fd); + if (fd < 0) + return -errno; + else return fd; } void *open_dir(char *path, int *err_out) @@ -109,23 +92,26 @@ void *open_dir(char *path, int *err_out) dir = opendir(path); *err_out = errno; - if(dir == NULL) return(NULL); - return(dir); + + return dir; } char *read_dir(void *stream, unsigned long long *pos, - unsigned long long *ino_out, int *len_out) + unsigned long long *ino_out, int *len_out, + unsigned int *type_out) { DIR *dir = stream; struct dirent *ent; seekdir(dir, *pos); ent = readdir(dir); - if(ent == NULL) return(NULL); + if (ent == NULL) + return NULL; *len_out = strlen(ent->d_name); *ino_out = ent->d_ino; + *type_out = ent->d_type; *pos = telldir(dir); - return(ent->d_name); + return ent->d_name; } int read_file(int fd, unsigned long long *offset, char *buf, int len) @@ -133,9 +119,10 @@ int read_file(int fd, unsigned long long *offset, char *buf, int len) int n; n = pread64(fd, buf, len, *offset); - if(n < 0) return(-errno); + if (n < 0) + return -errno; *offset += n; - return(n); + return n; } int write_file(int fd, unsigned long long *offset, const char *buf, int len) @@ -143,9 +130,10 @@ int write_file(int fd, unsigned long long *offset, const char *buf, int len) int n; n = pwrite64(fd, buf, len, *offset); - if(n < 0) return(-errno); + if (n < 0) + return -errno; *offset += n; - return(n); + return n; } int lseek_file(int fd, long long offset, int whence) @@ -153,9 +141,9 @@ int lseek_file(int fd, long long offset, int whence) int ret; ret = lseek64(fd, offset, whence); - if(ret < 0) - return(-errno); - return(0); + if (ret < 0) + return -errno; + return 0; } int fsync_file(int fd, int datasync) @@ -171,6 +159,11 @@ int fsync_file(int fd, int datasync) return 0; } +int replace_file(int oldfd, int fd) +{ + return dup2(oldfd, fd); +} + void close_file(void *stream) { close(*((int *) stream)); @@ -197,66 +190,92 @@ int file_create(char *name, int ur, int uw, int ux, int gr, mode |= ow ? S_IWOTH : 0; mode |= ox ? S_IXOTH : 0; fd = open64(name, O_CREAT | O_RDWR, mode); - if(fd < 0) - return(-errno); - return(fd); + if (fd < 0) + return -errno; + return fd; } -int set_attr(const char *file, struct hostfs_iattr *attrs) +int set_attr(const char *file, struct hostfs_iattr *attrs, int fd) { - struct utimbuf buf; + struct hostfs_stat st; + struct timeval times[2]; int err, ma; - if(attrs->ia_valid & HOSTFS_ATTR_MODE){ - if(chmod(file, attrs->ia_mode) != 0) return(-errno); - } - if(attrs->ia_valid & HOSTFS_ATTR_UID){ - if(chown(file, attrs->ia_uid, -1)) return(-errno); + if (attrs->ia_valid & HOSTFS_ATTR_MODE) { + if (fd >= 0) { + if (fchmod(fd, attrs->ia_mode) != 0) + return -errno; + } else if (chmod(file, attrs->ia_mode) != 0) { + return -errno; + } } - if(attrs->ia_valid & HOSTFS_ATTR_GID){ - if(chown(file, -1, attrs->ia_gid)) return(-errno); + if (attrs->ia_valid & HOSTFS_ATTR_UID) { + if (fd >= 0) { + if (fchown(fd, attrs->ia_uid, -1)) + return -errno; + } else if (chown(file, attrs->ia_uid, -1)) { + return -errno; + } } - if(attrs->ia_valid & HOSTFS_ATTR_SIZE){ - if(truncate(file, attrs->ia_size)) return(-errno); + if (attrs->ia_valid & HOSTFS_ATTR_GID) { + if (fd >= 0) { + if (fchown(fd, -1, attrs->ia_gid)) + return -errno; + } else if (chown(file, -1, attrs->ia_gid)) { + return -errno; + } } - ma = HOSTFS_ATTR_ATIME_SET | HOSTFS_ATTR_MTIME_SET; - if((attrs->ia_valid & ma) == ma){ - buf.actime = attrs->ia_atime.tv_sec; - buf.modtime = attrs->ia_mtime.tv_sec; - if(utime(file, &buf) != 0) return(-errno); + if (attrs->ia_valid & HOSTFS_ATTR_SIZE) { + if (fd >= 0) { + if (ftruncate(fd, attrs->ia_size)) + return -errno; + } else if (truncate(file, attrs->ia_size)) { + return -errno; + } } - else { - struct timespec ts; - - if(attrs->ia_valid & HOSTFS_ATTR_ATIME_SET){ - err = stat_file(file, NULL, NULL, NULL, NULL, NULL, - NULL, NULL, &ts, NULL, NULL, NULL); - if(err != 0) - return(err); - buf.actime = attrs->ia_atime.tv_sec; - buf.modtime = ts.tv_sec; - if(utime(file, &buf) != 0) - return(-errno); + + /* + * Update accessed and/or modified time, in two parts: first set + * times according to the changes to perform, and then call futimes() + * or utimes() to apply them. + */ + ma = (HOSTFS_ATTR_ATIME_SET | HOSTFS_ATTR_MTIME_SET); + if (attrs->ia_valid & ma) { + err = stat_file(file, &st, fd); + if (err != 0) + return err; + + times[0].tv_sec = st.atime.tv_sec; + times[0].tv_usec = st.atime.tv_nsec / 1000; + times[1].tv_sec = st.mtime.tv_sec; + times[1].tv_usec = st.mtime.tv_nsec / 1000; + + if (attrs->ia_valid & HOSTFS_ATTR_ATIME_SET) { + times[0].tv_sec = attrs->ia_atime.tv_sec; + times[0].tv_usec = attrs->ia_atime.tv_nsec / 1000; } - if(attrs->ia_valid & HOSTFS_ATTR_MTIME_SET){ - err = stat_file(file, NULL, NULL, NULL, NULL, NULL, - NULL, &ts, NULL, NULL, NULL, NULL); - if(err != 0) - return(err); - buf.actime = ts.tv_sec; - buf.modtime = attrs->ia_mtime.tv_sec; - if(utime(file, &buf) != 0) - return(-errno); + if (attrs->ia_valid & HOSTFS_ATTR_MTIME_SET) { + times[1].tv_sec = attrs->ia_mtime.tv_sec; + times[1].tv_usec = attrs->ia_mtime.tv_nsec / 1000; + } + + if (fd >= 0) { + if (futimes(fd, times) != 0) + return -errno; + } else if (utimes(file, times) != 0) { + return -errno; } } - if(attrs->ia_valid & HOSTFS_ATTR_CTIME) ; - if(attrs->ia_valid & (HOSTFS_ATTR_ATIME | HOSTFS_ATTR_MTIME)){ - err = stat_file(file, NULL, NULL, NULL, NULL, NULL, NULL, - &attrs->ia_atime, &attrs->ia_mtime, NULL, - NULL, NULL); - if(err != 0) return(err); + + /* Note: ctime is not handled */ + if (attrs->ia_valid & (HOSTFS_ATTR_ATIME | HOSTFS_ATTR_MTIME)) { + err = stat_file(file, &st, fd); + attrs->ia_atime = st.atime; + attrs->ia_mtime = st.mtime; + if (err != 0) + return err; } - return(0); + return 0; } int make_symlink(const char *from, const char *to) @@ -264,8 +283,9 @@ int make_symlink(const char *from, const char *to) int err; err = symlink(to, from); - if(err) return(-errno); - return(0); + if (err) + return -errno; + return 0; } int unlink_file(const char *file) @@ -273,8 +293,9 @@ int unlink_file(const char *file) int err; err = unlink(file); - if(err) return(-errno); - return(0); + if (err) + return -errno; + return 0; } int do_mkdir(const char *file, int mode) @@ -282,8 +303,9 @@ int do_mkdir(const char *file, int mode) int err; err = mkdir(file, mode); - if(err) return(-errno); - return(0); + if (err) + return -errno; + return 0; } int do_rmdir(const char *file) @@ -291,17 +313,19 @@ int do_rmdir(const char *file) int err; err = rmdir(file); - if(err) return(-errno); - return(0); + if (err) + return -errno; + return 0; } -int do_mknod(const char *file, int mode, int dev) +int do_mknod(const char *file, int mode, unsigned int major, unsigned int minor) { int err; - err = mknod(file, mode, dev); - if(err) return(-errno); - return(0); + err = mknod(file, mode, os_makedev(major, minor)); + if (err) + return -errno; + return 0; } int link_file(const char *to, const char *from) @@ -309,20 +333,21 @@ int link_file(const char *to, const char *from) int err; err = link(to, from); - if(err) return(-errno); - return(0); + if (err) + return -errno; + return 0; } -int do_readlink(char *file, char *buf, int size) +int hostfs_do_readlink(char *file, char *buf, int size) { int n; n = readlink(file, buf, size); - if(n < 0) - return(-errno); - if(n < size) + if (n < 0) + return -errno; + if (n < size) buf[n] = '\0'; - return(n); + return n; } int rename_file(char *from, char *to) @@ -330,21 +355,23 @@ int rename_file(char *from, char *to) int err; err = rename(from, to); - if(err < 0) return(-errno); - return(0); + if (err < 0) + return -errno; + return 0; } int do_statfs(char *root, long *bsize_out, long long *blocks_out, long long *bfree_out, long long *bavail_out, long long *files_out, long long *ffree_out, - void *fsid_out, int fsid_size, long *namelen_out, - long *spare_out) + void *fsid_out, int fsid_size, long *namelen_out) { struct statfs64 buf; int err; err = statfs64(root, &buf); - if(err < 0) return(-errno); + if (err < 0) + return -errno; + *bsize_out = buf.f_bsize; *blocks_out = buf.f_blocks; *bfree_out = buf.f_bfree; @@ -355,22 +382,6 @@ int do_statfs(char *root, long *bsize_out, long long *blocks_out, sizeof(buf.f_fsid) > fsid_size ? fsid_size : sizeof(buf.f_fsid)); *namelen_out = buf.f_namelen; - spare_out[0] = buf.f_spare[0]; - spare_out[1] = buf.f_spare[1]; - spare_out[2] = buf.f_spare[2]; - spare_out[3] = buf.f_spare[3]; - spare_out[4] = buf.f_spare[4]; - spare_out[5] = buf.f_spare[5]; - return(0); -} -/* - * Overrides for Emacs so that we follow Linus's tabbing style. - * Emacs will notice this stuff at the end of the file and automatically - * adjust the settings for this buffer only. This must remain at the end - * of the file. - * --------------------------------------------------------------------------- - * Local variables: - * c-file-style: "linux" - * End: - */ + return 0; +} |
