diff options
Diffstat (limited to 'fs/reiserfs/xattr.c')
-rw-r--r-- | fs/reiserfs/xattr.c | 2173 |
1 files changed, 1087 insertions, 1086 deletions
diff --git a/fs/reiserfs/xattr.c b/fs/reiserfs/xattr.c index 45582fe8b46..e386d3db305 100644 --- a/fs/reiserfs/xattr.c +++ b/fs/reiserfs/xattr.c @@ -51,67 +51,68 @@ #define PRIVROOT_NAME ".reiserfs_priv" #define XAROOT_NAME "xattrs" -static struct reiserfs_xattr_handler *find_xattr_handler_prefix (const char *prefix); +static struct reiserfs_xattr_handler *find_xattr_handler_prefix(const char + *prefix); -static struct dentry * -create_xa_root (struct super_block *sb) +static struct dentry *create_xa_root(struct super_block *sb) { - struct dentry *privroot = dget (REISERFS_SB(sb)->priv_root); - struct dentry *xaroot; - - /* This needs to be created at mount-time */ - if (!privroot) - return ERR_PTR(-EOPNOTSUPP); - - xaroot = lookup_one_len (XAROOT_NAME, privroot, strlen (XAROOT_NAME)); - if (IS_ERR (xaroot)) { - goto out; - } else if (!xaroot->d_inode) { - int err; - down (&privroot->d_inode->i_sem); - err = privroot->d_inode->i_op->mkdir (privroot->d_inode, xaroot, 0700); - up (&privroot->d_inode->i_sem); - - if (err) { - dput (xaroot); - dput (privroot); - return ERR_PTR (err); - } - REISERFS_SB(sb)->xattr_root = dget (xaroot); - } - -out: - dput (privroot); - return xaroot; + struct dentry *privroot = dget(REISERFS_SB(sb)->priv_root); + struct dentry *xaroot; + + /* This needs to be created at mount-time */ + if (!privroot) + return ERR_PTR(-EOPNOTSUPP); + + xaroot = lookup_one_len(XAROOT_NAME, privroot, strlen(XAROOT_NAME)); + if (IS_ERR(xaroot)) { + goto out; + } else if (!xaroot->d_inode) { + int err; + down(&privroot->d_inode->i_sem); + err = + privroot->d_inode->i_op->mkdir(privroot->d_inode, xaroot, + 0700); + up(&privroot->d_inode->i_sem); + + if (err) { + dput(xaroot); + dput(privroot); + return ERR_PTR(err); + } + REISERFS_SB(sb)->xattr_root = dget(xaroot); + } + + out: + dput(privroot); + return xaroot; } /* This will return a dentry, or error, refering to the xa root directory. * If the xa root doesn't exist yet, the dentry will be returned without * an associated inode. This dentry can be used with ->mkdir to create * the xa directory. */ -static struct dentry * -__get_xa_root (struct super_block *s) +static struct dentry *__get_xa_root(struct super_block *s) { - struct dentry *privroot = dget (REISERFS_SB(s)->priv_root); - struct dentry *xaroot = NULL; - - if (IS_ERR (privroot) || !privroot) - return privroot; - - xaroot = lookup_one_len (XAROOT_NAME, privroot, strlen (XAROOT_NAME)); - if (IS_ERR (xaroot)) { - goto out; - } else if (!xaroot->d_inode) { - dput (xaroot); - xaroot = NULL; - goto out; - } - - REISERFS_SB(s)->xattr_root = dget (xaroot); - -out: - dput (privroot); - return xaroot; + struct dentry *privroot = dget(REISERFS_SB(s)->priv_root); + struct dentry *xaroot = NULL; + + if (IS_ERR(privroot) || !privroot) + return privroot; + + xaroot = lookup_one_len(XAROOT_NAME, privroot, strlen(XAROOT_NAME)); + if (IS_ERR(xaroot)) { + goto out; + } else if (!xaroot->d_inode) { + dput(xaroot); + xaroot = NULL; + goto out; + } + + REISERFS_SB(s)->xattr_root = dget(xaroot); + + out: + dput(privroot); + return xaroot; } /* Returns the dentry (or NULL) referring to the root of the extended @@ -119,147 +120,145 @@ out: * Otherwise, we attempt to retreive it from disk. It may also return * a pointer-encoded error. */ -static inline struct dentry * -get_xa_root (struct super_block *s) +static inline struct dentry *get_xa_root(struct super_block *s) { - struct dentry *dentry = dget (REISERFS_SB(s)->xattr_root); + struct dentry *dentry = dget(REISERFS_SB(s)->xattr_root); - if (!dentry) - dentry = __get_xa_root (s); + if (!dentry) + dentry = __get_xa_root(s); - return dentry; + return dentry; } /* Opens the directory corresponding to the inode's extended attribute store. * If flags allow, the tree to the directory may be created. If creation is * prohibited, -ENODATA is returned. */ -static struct dentry * -open_xa_dir (const struct inode *inode, int flags) +static struct dentry *open_xa_dir(const struct inode *inode, int flags) { - struct dentry *xaroot, *xadir; - char namebuf[17]; - - xaroot = get_xa_root (inode->i_sb); - if (IS_ERR (xaroot)) { - return xaroot; - } else if (!xaroot) { - if (flags == 0 || flags & XATTR_CREATE) { - xaroot = create_xa_root (inode->i_sb); - if (IS_ERR (xaroot)) - return xaroot; - } - if (!xaroot) - return ERR_PTR (-ENODATA); - } - - /* ok, we have xaroot open */ - - snprintf (namebuf, sizeof (namebuf), "%X.%X", - le32_to_cpu (INODE_PKEY (inode)->k_objectid), - inode->i_generation); - xadir = lookup_one_len (namebuf, xaroot, strlen (namebuf)); - if (IS_ERR (xadir)) { - dput (xaroot); - return xadir; - } - - if (!xadir->d_inode) { - int err; - if (flags == 0 || flags & XATTR_CREATE) { - /* Although there is nothing else trying to create this directory, - * another directory with the same hash may be created, so we need - * to protect against that */ - err = xaroot->d_inode->i_op->mkdir (xaroot->d_inode, xadir, 0700); - if (err) { - dput (xaroot); - dput (xadir); - return ERR_PTR (err); - } - } - if (!xadir->d_inode) { - dput (xaroot); - dput (xadir); - return ERR_PTR (-ENODATA); - } - } - - dput (xaroot); - return xadir; + struct dentry *xaroot, *xadir; + char namebuf[17]; + + xaroot = get_xa_root(inode->i_sb); + if (IS_ERR(xaroot)) { + return xaroot; + } else if (!xaroot) { + if (flags == 0 || flags & XATTR_CREATE) { + xaroot = create_xa_root(inode->i_sb); + if (IS_ERR(xaroot)) + return xaroot; + } + if (!xaroot) + return ERR_PTR(-ENODATA); + } + + /* ok, we have xaroot open */ + + snprintf(namebuf, sizeof(namebuf), "%X.%X", + le32_to_cpu(INODE_PKEY(inode)->k_objectid), + inode->i_generation); + xadir = lookup_one_len(namebuf, xaroot, strlen(namebuf)); + if (IS_ERR(xadir)) { + dput(xaroot); + return xadir; + } + + if (!xadir->d_inode) { + int err; + if (flags == 0 || flags & XATTR_CREATE) { + /* Although there is nothing else trying to create this directory, + * another directory with the same hash may be created, so we need + * to protect against that */ + err = + xaroot->d_inode->i_op->mkdir(xaroot->d_inode, xadir, + 0700); + if (err) { + dput(xaroot); + dput(xadir); + return ERR_PTR(err); + } + } + if (!xadir->d_inode) { + dput(xaroot); + dput(xadir); + return ERR_PTR(-ENODATA); + } + } + + dput(xaroot); + return xadir; } /* Returns a dentry corresponding to a specific extended attribute file * for the inode. If flags allow, the file is created. Otherwise, a * valid or negative dentry, or an error is returned. */ -static struct dentry * -get_xa_file_dentry (const struct inode *inode, const char *name, int flags) +static struct dentry *get_xa_file_dentry(const struct inode *inode, + const char *name, int flags) { - struct dentry *xadir, *xafile; - int err = 0; - - xadir = open_xa_dir (inode, flags); - if (IS_ERR (xadir)) { - return ERR_PTR (PTR_ERR (xadir)); - } else if (xadir && !xadir->d_inode) { - dput (xadir); - return ERR_PTR (-ENODATA); - } - - xafile = lookup_one_len (name, xadir, strlen (name)); - if (IS_ERR (xafile)) { - dput (xadir); - return ERR_PTR (PTR_ERR (xafile)); - } - - if (xafile->d_inode) { /* file exists */ - if (flags & XATTR_CREATE) { - err = -EEXIST; - dput (xafile); - goto out; - } - } else if (flags & XATTR_REPLACE || flags & FL_READONLY) { - goto out; - } else { - /* inode->i_sem is down, so nothing else can try to create - * the same xattr */ - err = xadir->d_inode->i_op->create (xadir->d_inode, xafile, - 0700|S_IFREG, NULL); - - if (err) { - dput (xafile); - goto out; - } - } - -out: - dput (xadir); - if (err) - xafile = ERR_PTR (err); - return xafile; -} + struct dentry *xadir, *xafile; + int err = 0; + + xadir = open_xa_dir(inode, flags); + if (IS_ERR(xadir)) { + return ERR_PTR(PTR_ERR(xadir)); + } else if (xadir && !xadir->d_inode) { + dput(xadir); + return ERR_PTR(-ENODATA); + } + + xafile = lookup_one_len(name, xadir, strlen(name)); + if (IS_ERR(xafile)) { + dput(xadir); + return ERR_PTR(PTR_ERR(xafile)); + } + + if (xafile->d_inode) { /* file exists */ + if (flags & XATTR_CREATE) { + err = -EEXIST; + dput(xafile); + goto out; + } + } else if (flags & XATTR_REPLACE || flags & FL_READONLY) { + goto out; + } else { + /* inode->i_sem is down, so nothing else can try to create + * the same xattr */ + err = xadir->d_inode->i_op->create(xadir->d_inode, xafile, + 0700 | S_IFREG, NULL); + + if (err) { + dput(xafile); + goto out; + } + } + out: + dput(xadir); + if (err) + xafile = ERR_PTR(err); + return xafile; +} /* Opens a file pointer to the attribute associated with inode */ -static struct file * -open_xa_file (const struct inode *inode, const char *name, int flags) +static struct file *open_xa_file(const struct inode *inode, const char *name, + int flags) { - struct dentry *xafile; - struct file *fp; - - xafile = get_xa_file_dentry (inode, name, flags); - if (IS_ERR (xafile)) - return ERR_PTR (PTR_ERR (xafile)); - else if (!xafile->d_inode) { - dput (xafile); - return ERR_PTR (-ENODATA); - } + struct dentry *xafile; + struct file *fp; + + xafile = get_xa_file_dentry(inode, name, flags); + if (IS_ERR(xafile)) + return ERR_PTR(PTR_ERR(xafile)); + else if (!xafile->d_inode) { + dput(xafile); + return ERR_PTR(-ENODATA); + } - fp = dentry_open (xafile, NULL, O_RDWR); - /* dentry_open dputs the dentry if it fails */ + fp = dentry_open(xafile, NULL, O_RDWR); + /* dentry_open dputs the dentry if it fails */ - return fp; + return fp; } - /* * this is very similar to fs/reiserfs/dir.c:reiserfs_readdir, but * we need to drop the path before calling the filldir struct. That @@ -273,139 +272,146 @@ open_xa_file (const struct inode *inode, const char *name, int flags) * we're called with i_sem held, so there are no worries about the directory * changing underneath us. */ -static int __xattr_readdir(struct file * filp, void * dirent, filldir_t filldir) +static int __xattr_readdir(struct file *filp, void *dirent, filldir_t filldir) { - struct inode *inode = filp->f_dentry->d_inode; - struct cpu_key pos_key; /* key of current position in the directory (key of directory entry) */ - INITIALIZE_PATH (path_to_entry); - struct buffer_head * bh; - int entry_num; - struct item_head * ih, tmp_ih; - int search_res; - char * local_buf; - loff_t next_pos; - char small_buf[32] ; /* avoid kmalloc if we can */ - struct reiserfs_de_head *deh; - int d_reclen; - char * d_name; - off_t d_off; - ino_t d_ino; - struct reiserfs_dir_entry de; - - - /* form key for search the next directory entry using f_pos field of - file structure */ - next_pos = max_reiserfs_offset(inode); - - while (1) { -research: - if (next_pos <= DOT_DOT_OFFSET) - break; - make_cpu_key (&pos_key, inode, next_pos, TYPE_DIRENTRY, 3); - - search_res = search_by_entry_key(inode->i_sb, &pos_key, &path_to_entry, &de); - if (search_res == IO_ERROR) { - // FIXME: we could just skip part of directory which could - // not be read - pathrelse(&path_to_entry); - return -EIO; - } - - if (search_res == NAME_NOT_FOUND) - de.de_entry_num--; + struct inode *inode = filp->f_dentry->d_inode; + struct cpu_key pos_key; /* key of current position in the directory (key of directory entry) */ + INITIALIZE_PATH(path_to_entry); + struct buffer_head *bh; + int entry_num; + struct item_head *ih, tmp_ih; + int search_res; + char *local_buf; + loff_t next_pos; + char small_buf[32]; /* avoid kmalloc if we can */ + struct reiserfs_de_head *deh; + int d_reclen; + char *d_name; + off_t d_off; + ino_t d_ino; + struct reiserfs_dir_entry de; + + /* form key for search the next directory entry using f_pos field of + file structure */ + next_pos = max_reiserfs_offset(inode); + + while (1) { + research: + if (next_pos <= DOT_DOT_OFFSET) + break; + make_cpu_key(&pos_key, inode, next_pos, TYPE_DIRENTRY, 3); + + search_res = + search_by_entry_key(inode->i_sb, &pos_key, &path_to_entry, + &de); + if (search_res == IO_ERROR) { + // FIXME: we could just skip part of directory which could + // not be read + pathrelse(&path_to_entry); + return -EIO; + } - set_de_name_and_namelen(&de); - entry_num = de.de_entry_num; - deh = &(de.de_deh[entry_num]); + if (search_res == NAME_NOT_FOUND) + de.de_entry_num--; - bh = de.de_bh; - ih = de.de_ih; + set_de_name_and_namelen(&de); + entry_num = de.de_entry_num; + deh = &(de.de_deh[entry_num]); - if (!is_direntry_le_ih(ih)) { - reiserfs_warning(inode->i_sb, "not direntry %h", ih); - break; - } - copy_item_head(&tmp_ih, ih); + bh = de.de_bh; + ih = de.de_ih; - /* we must have found item, that is item of this directory, */ - RFALSE( COMP_SHORT_KEYS (&(ih->ih_key), &pos_key), - "vs-9000: found item %h does not match to dir we readdir %K", - ih, &pos_key); + if (!is_direntry_le_ih(ih)) { + reiserfs_warning(inode->i_sb, "not direntry %h", ih); + break; + } + copy_item_head(&tmp_ih, ih); - if (deh_offset(deh) <= DOT_DOT_OFFSET) { - break; - } + /* we must have found item, that is item of this directory, */ + RFALSE(COMP_SHORT_KEYS(&(ih->ih_key), &pos_key), + "vs-9000: found item %h does not match to dir we readdir %K", + ih, &pos_key); - /* look for the previous entry in the directory */ - next_pos = deh_offset (deh) - 1; + if (deh_offset(deh) <= DOT_DOT_OFFSET) { + break; + } - if (!de_visible (deh)) - /* it is hidden entry */ - continue; + /* look for the previous entry in the directory */ + next_pos = deh_offset(deh) - 1; - d_reclen = entry_length(bh, ih, entry_num); - d_name = B_I_DEH_ENTRY_FILE_NAME (bh, ih, deh); - d_off = deh_offset (deh); - d_ino = deh_objectid (deh); + if (!de_visible(deh)) + /* it is hidden entry */ + continue; - if (!d_name[d_reclen - 1]) - d_reclen = strlen (d_name); + d_reclen = entry_length(bh, ih, entry_num); + d_name = B_I_DEH_ENTRY_FILE_NAME(bh, ih, deh); + d_off = deh_offset(deh); + d_ino = deh_objectid(deh); - if (d_reclen > REISERFS_MAX_NAME(inode->i_sb->s_blocksize)){ - /* too big to send back to VFS */ - continue ; - } + if (!d_name[d_reclen - 1]) + d_reclen = strlen(d_name); - /* Ignore the .reiserfs_priv entry */ - if (reiserfs_xattrs (inode->i_sb) && - !old_format_only(inode->i_sb) && - deh_objectid (deh) == le32_to_cpu (INODE_PKEY(REISERFS_SB(inode->i_sb)->priv_root->d_inode)->k_objectid)) - continue; + if (d_reclen > REISERFS_MAX_NAME(inode->i_sb->s_blocksize)) { + /* too big to send back to VFS */ + continue; + } - if (d_reclen <= 32) { - local_buf = small_buf ; - } else { - local_buf = reiserfs_kmalloc(d_reclen, GFP_NOFS, inode->i_sb) ; - if (!local_buf) { - pathrelse (&path_to_entry); - return -ENOMEM ; - } - if (item_moved (&tmp_ih, &path_to_entry)) { - reiserfs_kfree(local_buf, d_reclen, inode->i_sb) ; - - /* sigh, must retry. Do this same offset again */ - next_pos = d_off; - goto research; - } - } + /* Ignore the .reiserfs_priv entry */ + if (reiserfs_xattrs(inode->i_sb) && + !old_format_only(inode->i_sb) && + deh_objectid(deh) == + le32_to_cpu(INODE_PKEY + (REISERFS_SB(inode->i_sb)->priv_root->d_inode)-> + k_objectid)) + continue; + + if (d_reclen <= 32) { + local_buf = small_buf; + } else { + local_buf = + reiserfs_kmalloc(d_reclen, GFP_NOFS, inode->i_sb); + if (!local_buf) { + pathrelse(&path_to_entry); + return -ENOMEM; + } + if (item_moved(&tmp_ih, &path_to_entry)) { + reiserfs_kfree(local_buf, d_reclen, + inode->i_sb); + + /* sigh, must retry. Do this same offset again */ + next_pos = d_off; + goto research; + } + } - // Note, that we copy name to user space via temporary - // buffer (local_buf) because filldir will block if - // user space buffer is swapped out. At that time - // entry can move to somewhere else - memcpy (local_buf, d_name, d_reclen); + // Note, that we copy name to user space via temporary + // buffer (local_buf) because filldir will block if + // user space buffer is swapped out. At that time + // entry can move to somewhere else + memcpy(local_buf, d_name, d_reclen); - /* the filldir function might need to start transactions, - * or do who knows what. Release the path now that we've - * copied all the important stuff out of the deh - */ - pathrelse (&path_to_entry); - - if (filldir (dirent, local_buf, d_reclen, d_off, d_ino, - DT_UNKNOWN) < 0) { - if (local_buf != small_buf) { - reiserfs_kfree(local_buf, d_reclen, inode->i_sb) ; - } - goto end; - } - if (local_buf != small_buf) { - reiserfs_kfree(local_buf, d_reclen, inode->i_sb) ; - } - } /* while */ + /* the filldir function might need to start transactions, + * or do who knows what. Release the path now that we've + * copied all the important stuff out of the deh + */ + pathrelse(&path_to_entry); + + if (filldir(dirent, local_buf, d_reclen, d_off, d_ino, + DT_UNKNOWN) < 0) { + if (local_buf != small_buf) { + reiserfs_kfree(local_buf, d_reclen, + inode->i_sb); + } + goto end; + } + if (local_buf != small_buf) { + reiserfs_kfree(local_buf, d_reclen, inode->i_sb); + } + } /* while */ -end: - pathrelse (&path_to_entry); - return 0; + end: + pathrelse(&path_to_entry); + return 0; } /* @@ -417,63 +423,59 @@ end: static int xattr_readdir(struct file *file, filldir_t filler, void *buf) { - struct inode *inode = file->f_dentry->d_inode; - int res = -ENOTDIR; - if (!file->f_op || !file->f_op->readdir) - goto out; - down(&inode->i_sem); + struct inode *inode = file->f_dentry->d_inode; + int res = -ENOTDIR; + if (!file->f_op || !file->f_op->readdir) + goto out; + down(&inode->i_sem); // down(&inode->i_zombie); - res = -ENOENT; - if (!IS_DEADDIR(inode)) { - lock_kernel(); - res = __xattr_readdir(file, buf, filler); - unlock_kernel(); - } + res = -ENOENT; + if (!IS_DEADDIR(inode)) { + lock_kernel(); + res = __xattr_readdir(file, buf, filler); + unlock_kernel(); + } // up(&inode->i_zombie); - up(&inode->i_sem); -out: - return res; + up(&inode->i_sem); + out: + return res; } - /* Internal operations on file data */ -static inline void -reiserfs_put_page(struct page *page) +static inline void reiserfs_put_page(struct page *page) { - kunmap(page); - page_cache_release(page); + kunmap(page); + page_cache_release(page); } -static struct page * -reiserfs_get_page(struct inode *dir, unsigned long n) +static struct page *reiserfs_get_page(struct inode *dir, unsigned long n) { - struct address_space *mapping = dir->i_mapping; - struct page *page; - /* We can deadlock if we try to free dentries, - and an unlink/rmdir has just occured - GFP_NOFS avoids this */ - mapping->flags = (mapping->flags & ~__GFP_BITS_MASK) | GFP_NOFS; - page = read_cache_page (mapping, n, - (filler_t*)mapping->a_ops->readpage, NULL); - if (!IS_ERR(page)) { - wait_on_page_locked(page); - kmap(page); - if (!PageUptodate(page)) - goto fail; - - if (PageError(page)) - goto fail; - } - return page; - -fail: - reiserfs_put_page(page); - return ERR_PTR(-EIO); + struct address_space *mapping = dir->i_mapping; + struct page *page; + /* We can deadlock if we try to free dentries, + and an unlink/rmdir has just occured - GFP_NOFS avoids this */ + mapping->flags = (mapping->flags & ~__GFP_BITS_MASK) | GFP_NOFS; + page = read_cache_page(mapping, n, + (filler_t *) mapping->a_ops->readpage, NULL); + if (!IS_ERR(page)) { + wait_on_page_locked(page); + kmap(page); + if (!PageUptodate(page)) + goto fail; + + if (PageError(page)) + goto fail; + } + return page; + + fail: + reiserfs_put_page(page); + return ERR_PTR(-EIO); } -static inline __u32 -xattr_hash (const char *msg, int len) +static inline __u32 xattr_hash(const char *msg, int len) { - return csum_partial (msg, len, 0); + return csum_partial(msg, len, 0); } /* Generic extended attribute operations that can be used by xa plugins */ @@ -482,294 +484,300 @@ xattr_hash (const char *msg, int len) * inode->i_sem: down */ int -reiserfs_xattr_set (struct inode *inode, const char *name, const void *buffer, - size_t buffer_size, int flags) +reiserfs_xattr_set(struct inode *inode, const char *name, const void *buffer, + size_t buffer_size, int flags) { - int err = 0; - struct file *fp; - struct page *page; - char *data; - struct address_space *mapping; - size_t file_pos = 0; - size_t buffer_pos = 0; - struct inode *xinode; - struct iattr newattrs; - __u32 xahash = 0; - - if (IS_RDONLY (inode)) - return -EROFS; - - if (IS_IMMUTABLE (inode) || IS_APPEND (inode)) - return -EPERM; - - if (get_inode_sd_version (inode) == STAT_DATA_V1) - return -EOPNOTSUPP; - - /* Empty xattrs are ok, they're just empty files, no hash */ - if (buffer && buffer_size) - xahash = xattr_hash (buffer, buffer_size); - -open_file: - fp = open_xa_file (inode, name, flags); - if (IS_ERR (fp)) { - err = PTR_ERR (fp); - goto out; - } - - xinode = fp->f_dentry->d_inode; - REISERFS_I(inode)->i_flags |= i_has_xattr_dir; - - /* we need to copy it off.. */ - if (xinode->i_nlink > 1) { - fput(fp); - err = reiserfs_xattr_del (inode, name); - if (err < 0) - goto out; - /* We just killed the old one, we're not replacing anymore */ - if (flags & XATTR_REPLACE) - flags &= ~XATTR_REPLACE; - goto open_file; - } - - /* Resize it so we're ok to write there */ - newattrs.ia_size = buffer_size; - newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME; - down (&xinode->i_sem); - err = notify_change(fp->f_dentry, &newattrs); - if (err) - goto out_filp; - - mapping = xinode->i_mapping; - while (buffer_pos < buffer_size || buffer_pos == 0) { - size_t chunk; - size_t skip = 0; - size_t page_offset = (file_pos & (PAGE_CACHE_SIZE - 1)); - if (buffer_size - buffer_pos > PAGE_CACHE_SIZE) - chunk = PAGE_CACHE_SIZE; - else - chunk = buffer_size - buffer_pos; - - page = reiserfs_get_page (xinode, file_pos >> PAGE_CACHE_SHIFT); - if (IS_ERR (page)) { - err = PTR_ERR (page); - goto out_filp; - } - - lock_page (page); - data = page_address (page); - - if (file_pos == 0) { - struct reiserfs_xattr_header *rxh; - skip = file_pos = sizeof (struct reiserfs_xattr_header); - if (chunk + skip > PAGE_CACHE_SIZE) - chunk = PAGE_CACHE_SIZE - skip; - rxh = (struct reiserfs_xattr_header *)data; - rxh->h_magic = cpu_to_le32 (REISERFS_XATTR_MAGIC); - rxh->h_hash = cpu_to_le32 (xahash); - } - - err = mapping->a_ops->prepare_write (fp, page, page_offset, - page_offset + chunk + skip); - if (!err) { - if (buffer) - memcpy (data + skip, buffer + buffer_pos, chunk); - err = mapping->a_ops->commit_write (fp, page, page_offset, - page_offset + chunk + skip); + int err = 0; + struct file *fp; + struct page *page; + char *data; + struct address_space *mapping; + size_t file_pos = 0; + size_t buffer_pos = 0; + struct inode *xinode; + struct iattr newattrs; + __u32 xahash = 0; + + if (IS_RDONLY(inode)) + return -EROFS; + + if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) + return -EPERM; + + if (get_inode_sd_version(inode) == STAT_DATA_V1) + return -EOPNOTSUPP; + + /* Empty xattrs are ok, they're just empty files, no hash */ + if (buffer && buffer_size) + xahash = xattr_hash(buffer, buffer_size); + + open_file: + fp = open_xa_file(inode, name, flags); + if (IS_ERR(fp)) { + err = PTR_ERR(fp); + goto out; + } + + xinode = fp->f_dentry->d_inode; + REISERFS_I(inode)->i_flags |= i_has_xattr_dir; + + /* we need to copy it off.. */ + if (xinode->i_nlink > 1) { + fput(fp); + err = reiserfs_xattr_del(inode, name); + if (err < 0) + goto out; + /* We just killed the old one, we're not replacing anymore */ + if (flags & XATTR_REPLACE) + flags &= ~XATTR_REPLACE; + goto open_file; + } + + /* Resize it so we're ok to write there */ + newattrs.ia_size = buffer_size; + newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME; + down(&xinode->i_sem); + err = notify_change(fp->f_dentry, &newattrs); + if (err) + goto out_filp; + + mapping = xinode->i_mapping; + while (buffer_pos < buffer_size || buffer_pos == 0) { + size_t chunk; + size_t skip = 0; + size_t page_offset = (file_pos & (PAGE_CACHE_SIZE - 1)); + if (buffer_size - buffer_pos > PAGE_CACHE_SIZE) + chunk = PAGE_CACHE_SIZE; + else + chunk = buffer_size - buffer_pos; + + page = reiserfs_get_page(xinode, file_pos >> PAGE_CACHE_SHIFT); + if (IS_ERR(page)) { + err = PTR_ERR(page); + goto out_filp; + } + + lock_page(page); + data = page_address(page); + + if (file_pos == 0) { + struct reiserfs_xattr_header *rxh; + skip = file_pos = sizeof(struct reiserfs_xattr_header); + if (chunk + skip > PAGE_CACHE_SIZE) + chunk = PAGE_CACHE_SIZE - skip; + rxh = (struct reiserfs_xattr_header *)data; + rxh->h_magic = cpu_to_le32(REISERFS_XATTR_MAGIC); + rxh->h_hash = cpu_to_le32(xahash); + } + + err = mapping->a_ops->prepare_write(fp, page, page_offset, + page_offset + chunk + skip); + if (!err) { + if (buffer) + memcpy(data + skip, buffer + buffer_pos, chunk); + err = + mapping->a_ops->commit_write(fp, page, page_offset, + page_offset + chunk + + skip); + } + unlock_page(page); + reiserfs_put_page(page); + buffer_pos += chunk; + file_pos += chunk; + skip = 0; + if (err || buffer_size == 0 || !buffer) + break; + } + + /* We can't mark the inode dirty if it's not hashed. This is the case + * when we're inheriting the default ACL. If we dirty it, the inode + * gets marked dirty, but won't (ever) make it onto the dirty list until + * it's synced explicitly to clear I_DIRTY. This is bad. */ + if (!hlist_unhashed(&inode->i_hash)) { + inode->i_ctime = CURRENT_TIME_SEC; + mark_inode_dirty(inode); } - unlock_page (page); - reiserfs_put_page (page); - buffer_pos += chunk; - file_pos += chunk; - skip = 0; - if (err || buffer_size == 0 || !buffer) - break; - } - - /* We can't mark the inode dirty if it's not hashed. This is the case - * when we're inheriting the default ACL. If we dirty it, the inode - * gets marked dirty, but won't (ever) make it onto the dirty list until - * it's synced explicitly to clear I_DIRTY. This is bad. */ - if (!hlist_unhashed(&inode->i_hash)) { - inode->i_ctime = CURRENT_TIME_SEC; - mark_inode_dirty (inode); - } - -out_filp: - up (&xinode->i_sem); - fput(fp); - -out: - return err; + + out_filp: + up(&xinode->i_sem); + fput(fp); + + out: + return err; } /* * inode->i_sem: down */ int -reiserfs_xattr_get (const struct inode *inode, const char *name, void *buffer, - size_t buffer_size) +reiserfs_xattr_get(const struct inode *inode, const char *name, void *buffer, + size_t buffer_size) { - ssize_t err = 0; - struct file *fp; - size_t isize; - size_t file_pos = 0; - size_t buffer_pos = 0; - struct page *page; - struct inode *xinode; - __u32 hash = 0; - - if (name == NULL) - return -EINVAL; - - /* We can't have xattrs attached to v1 items since they don't have - * generation numbers */ - if (get_inode_sd_version (inode) == STAT_DATA_V1) - return -EOPNOTSUPP; - - fp = open_xa_file (inode, name, FL_READONLY); - if (IS_ERR (fp)) { - err = PTR_ERR (fp); - goto out; - } - - xinode = fp->f_dentry->d_inode; - isize = xinode->i_size; - REISERFS_I(inode)->i_flags |= i_has_xattr_dir; - - /* Just return the size needed */ - if (buffer == NULL) { - err = isize - sizeof (struct reiserfs_xattr_header); - goto out_dput; - } - - if (buffer_size < isize - sizeof (struct reiserfs_xattr_header)) { - err = -ERANGE; - goto out_dput; - } - - while (file_pos < isize) { - size_t chunk; - char *data; - size_t skip = 0; - if (isize - file_pos > PAGE_CACHE_SIZE) - chunk = PAGE_CACHE_SIZE; - else - chunk = isize - file_pos; - - page = reiserfs_get_page (xinode, file_pos >> PAGE_CACHE_SHIFT); - if (IS_ERR (page)) { - err = PTR_ERR (page); - goto out_dput; - } - - lock_page (page); - data = page_address (page); - if (file_pos == 0) { - struct reiserfs_xattr_header *rxh = - (struct reiserfs_xattr_header *)data; - skip = file_pos = sizeof (struct reiserfs_xattr_header); - chunk -= skip; - /* Magic doesn't match up.. */ - if (rxh->h_magic != cpu_to_le32 (REISERFS_XATTR_MAGIC)) { - unlock_page (page); - reiserfs_put_page (page); - reiserfs_warning (inode->i_sb, "Invalid magic for xattr (%s) " - "associated with %k", name, - INODE_PKEY (inode)); - err = -EIO; - goto out_dput; - } - hash = le32_to_cpu (rxh->h_hash); - } - memcpy (buffer + buffer_pos, data + skip, chunk); - unlock_page (page); - reiserfs_put_page (page); - file_pos += chunk; - buffer_pos += chunk; - skip = 0; - } - err = isize - sizeof (struct reiserfs_xattr_header); - - if (xattr_hash (buffer, isize - sizeof (struct reiserfs_xattr_header)) != hash) { - reiserfs_warning (inode->i_sb, "Invalid hash for xattr (%s) associated " - "with %k", name, INODE_PKEY (inode)); - err = -EIO; - } - -out_dput: - fput(fp); - -out: - return err; + ssize_t err = 0; + struct file *fp; + size_t isize; + size_t file_pos = 0; + size_t buffer_pos = 0; + struct page *page; + struct inode *xinode; + __u32 hash = 0; + + if (name == NULL) + return -EINVAL; + + /* We can't have xattrs attached to v1 items since they don't have + * generation numbers */ + if (get_inode_sd_version(inode) == STAT_DATA_V1) + return -EOPNOTSUPP; + + fp = open_xa_file(inode, name, FL_READONLY); + if (IS_ERR(fp)) { + err = PTR_ERR(fp); + goto out; + } + + xinode = fp->f_dentry->d_inode; + isize = xinode->i_size; + REISERFS_I(inode)->i_flags |= i_has_xattr_dir; + + /* Just return the size needed */ + if (buffer == NULL) { + err = isize - sizeof(struct reiserfs_xattr_header); + goto out_dput; + } + + if (buffer_size < isize - sizeof(struct reiserfs_xattr_header)) { + err = -ERANGE; + goto out_dput; + } + + while (file_pos < isize) { + size_t chunk; + char *data; + size_t skip = 0; + if (isize - file_pos > PAGE_CACHE_SIZE) + chunk = PAGE_CACHE_SIZE; + else + chunk = isize - file_pos; + + page = reiserfs_get_page(xinode, file_pos >> PAGE_CACHE_SHIFT); + if (IS_ERR(page)) { + err = PTR_ERR(page); + goto out_dput; + } + + lock_page(page); + data = page_address(page); + if (file_pos == 0) { + struct reiserfs_xattr_header *rxh = + (struct reiserfs_xattr_header *)data; + skip = file_pos = sizeof(struct reiserfs_xattr_header); + chunk -= skip; + /* Magic doesn't match up.. */ + if (rxh->h_magic != cpu_to_le32(REISERFS_XATTR_MAGIC)) { + unlock_page(page); + reiserfs_put_page(page); + reiserfs_warning(inode->i_sb, + "Invalid magic for xattr (%s) " + "associated with %k", name, + INODE_PKEY(inode)); + err = -EIO; + goto out_dput; + } + hash = le32_to_cpu(rxh->h_hash); + } + memcpy(buffer + buffer_pos, data + skip, chunk); |