37 #include <linux/capability.h>
40 #include <linux/errno.h>
45 #include <linux/xattr.h>
48 #include <asm/uaccess.h>
50 #include <linux/stat.h>
54 #define PRIVROOT_NAME ".reiserfs_priv"
55 #define XAROOT_NAME "xattrs"
61 #ifdef CONFIG_REISERFS_FS_XATTR
65 return dir->
i_op->create(dir, dentry, mode,
true);
72 return dir->
i_op->mkdir(dir, dentry, mode);
79 static int xattr_unlink(
struct inode *dir,
struct dentry *dentry)
84 reiserfs_mutex_lock_nested_safe(&dentry->
d_inode->i_mutex,
86 error = dir->
i_op->unlink(dir, dentry);
94 static int xattr_rmdir(
struct inode *dir,
struct dentry *dentry)
99 reiserfs_mutex_lock_nested_safe(&dentry->
d_inode->i_mutex,
101 error = dir->
i_op->rmdir(dir, dentry);
111 #define xattr_may_create(flags) (!flags || flags & XATTR_CREATE)
115 struct dentry *privroot = REISERFS_SB(sb)->priv_root;
116 struct dentry *xaroot;
122 xaroot = dget(REISERFS_SB(sb)->xattr_root);
128 err = xattr_mkdir(privroot->
d_inode, xaroot, 0700);
131 xaroot = ERR_PTR(err);
139 static struct dentry *open_xa_dir(
const struct inode *
inode,
int flags)
141 struct dentry *xaroot, *xadir;
144 xaroot = open_xa_root(inode->
i_sb, flags);
148 snprintf(namebuf,
sizeof(namebuf),
"%X.%X",
155 if (!IS_ERR(xadir) && !xadir->
d_inode) {
158 err = xattr_mkdir(xaroot->
d_inode, xadir, 0700);
161 xadir = ERR_PTR(err);
181 u64 ino,
unsigned int d_type)
184 struct dentry *dentry;
190 if (name[0] ==
'.' && (name[1] ==
'\0' ||
191 (name[1] ==
'.' && name[2] ==
'\0')))
195 if (IS_ERR(dentry)) {
196 return PTR_ERR(dentry);
200 "Corrupted directory: xattr %s listed but "
201 "not found for file %s.\n",
215 for (i = 0; i < buf->
count; i++)
220 static int reiserfs_for_each_xattr(
struct inode *inode,
221 int (*
action)(
struct dentry *,
void *),
253 while ((err == 0 || err == -
ENOSPC) && buf.
count) {
261 lerr =
action(dentry, data);
270 fill_with_dentries, &pos);
275 cleanup_dentry_buf(&buf);
289 reiserfs_mutex_lock_nested_safe(
307 static int delete_one_xattr(
struct dentry *dentry,
void *data)
309 struct inode *dir = dentry->
d_parent->d_inode;
313 return xattr_rmdir(dir, dentry);
315 return xattr_unlink(dir, dentry);
318 static int chown_one_xattr(
struct dentry *dentry,
void *data)
327 int err = reiserfs_for_each_xattr(inode, delete_one_xattr,
NULL);
330 "Couldn't delete all xattrs (%d)\n", err);
337 int err = reiserfs_for_each_xattr(inode, chown_one_xattr, attrs);
340 "Couldn't chown all xattrs (%d)\n", err);
344 #ifdef CONFIG_REISERFS_FS_XATTR
348 static struct dentry *xattr_lookup(
struct inode *inode,
const char *name,
351 struct dentry *xadir, *xafile;
354 xadir = open_xa_dir(inode, flags);
356 return ERR_CAST(xadir);
360 if (IS_ERR(xafile)) {
361 err = PTR_ERR(xafile);
371 err = xattr_create(xadir->
d_inode, xafile,
386 static inline void reiserfs_put_page(
struct page *
page)
392 static struct page *reiserfs_get_page(
struct inode *dir,
size_t n)
398 mapping_set_gfp_mask(mapping,
GFP_NOFS);
408 reiserfs_put_page(page);
409 return ERR_PTR(-
EIO);
412 static inline __u32 xattr_hash(
const char *
msg,
int len)
418 unsigned from,
unsigned to);
420 static void update_ctime(
struct inode *inode)
423 if (inode_unhashed(inode) || !inode->
i_nlink ||
424 timespec_equal(&inode->
i_ctime, &now))
428 mark_inode_dirty(inode);
431 static int lookup_and_delete_xattr(
struct inode *inode,
const char *name)
434 struct dentry *dentry, *xadir;
438 return PTR_ERR(xadir);
442 if (IS_ERR(dentry)) {
443 err = PTR_ERR(dentry);
449 err = xattr_unlink(xadir->
d_inode, dentry);
469 struct inode *inode,
const char *name,
470 const void *
buffer,
size_t buffer_size,
int flags)
473 struct dentry *dentry;
477 size_t buffer_pos = 0;
487 err = lookup_and_delete_xattr(inode, name);
492 dentry = xattr_lookup(inode, name, flags);
493 if (IS_ERR(dentry)) {
495 return PTR_ERR(dentry);
502 xahash = xattr_hash(buffer, buffer_size);
503 while (buffer_pos < buffer_size || buffer_pos == 0) {
510 chunk = buffer_size - buffer_pos;
512 page = reiserfs_get_page(dentry->
d_inode, file_pos);
534 memcpy(data + skip, buffer + buffer_pos, chunk);
536 page_offset + chunk +
540 reiserfs_put_page(page);
544 if (err || buffer_size == 0 || !buffer)
549 if (!err && new_size < i_size_read(dentry->
d_inode)) {
550 struct iattr newattrs = {
566 up_write(&REISERFS_I(inode)->i_xattr_sem);
572 int reiserfs_xattr_set(
struct inode *inode,
const char *name,
573 const void *buffer,
size_t buffer_size,
int flags)
578 size_t jbegin_count = reiserfs_xattr_nblocks(inode, buffer_size);
581 jbegin_count += reiserfs_xattr_jcreate_nblocks(inode);
590 error = reiserfs_xattr_set_handle(&th, inode, name,
591 buffer, buffer_size, flags);
605 reiserfs_xattr_get(
struct inode *inode,
const char *name,
void *buffer,
609 struct dentry *dentry;
612 size_t buffer_pos = 0;
624 dentry = xattr_lookup(inode, name, XATTR_REPLACE);
625 if (IS_ERR(dentry)) {
626 err = PTR_ERR(dentry);
630 down_read(&REISERFS_I(inode)->i_xattr_sem);
632 isize = i_size_read(dentry->
d_inode);
635 if (buffer ==
NULL) {
645 while (file_pos < isize) {
652 chunk = isize - file_pos;
654 page = reiserfs_get_page(dentry->
d_inode, file_pos);
670 reiserfs_put_page(page);
672 "Invalid magic for xattr (%s) "
673 "associated with %k", name,
680 memcpy(buffer + buffer_pos, data + skip, chunk);
682 reiserfs_put_page(page);
692 "Invalid hash for xattr (%s) associated "
698 up_read(&REISERFS_I(inode)->i_xattr_sem);
714 #define for_each_xattr_handler(handlers, handler) \
715 for ((handler) = *(handlers)++; \
717 (handler) = *(handlers)++)
721 find_xattr_handler_prefix(
const struct xattr_handler **handlers,
747 handler = find_xattr_handler_prefix(dentry->
d_sb->s_xattr, name);
752 return handler->
get(dentry, name, buffer, size, handler->
flags);
762 size_t size,
int flags)
766 handler = find_xattr_handler_prefix(dentry->
d_sb->s_xattr, name);
771 return handler->
set(dentry, name, value, size, flags, handler->
flags);
782 handler = find_xattr_handler_prefix(dentry->
d_sb->s_xattr, name);
787 return handler->
set(dentry, name,
NULL, 0, XATTR_REPLACE, handler->
flags);
790 struct listxattr_buf {
794 struct dentry *dentry;
797 static int listxattr_filler(
void *buf,
const char *name,
int namelen,
798 loff_t offset,
u64 ino,
unsigned int d_type)
800 struct listxattr_buf *
b = (
struct listxattr_buf *)buf;
802 if (name[0] !=
'.' ||
803 (namelen != 1 && (name[1] !=
'.' || namelen != 2))) {
805 handler = find_xattr_handler_prefix(b->dentry->d_sb->s_xattr,
810 size = handler->
list(b->dentry, b->buf + b->pos,
811 b->size, name, namelen,
816 size = handler->
list(b->dentry,
NULL, 0, name,
817 namelen, handler->
flags);
837 struct listxattr_buf buf = {
840 .size = buffer ? size : 0,
846 if (!dentry->
d_sb->s_xattr ||
850 dir = open_xa_dir(dentry->
d_inode, XATTR_REPLACE);
870 static int create_privroot(
struct dentry *dentry)
873 struct inode *inode = dentry->
d_parent->d_inode;
876 err = xattr_mkdir(inode, dentry, 0700);
879 "xattrs/ACLs enabled and couldn't "
880 "find/create .reiserfs_priv. "
895 static int create_privroot(
struct dentry *dentry) {
return 0; }
899 static const struct xattr_handler *reiserfs_xattr_handlers[] = {
900 #ifdef CONFIG_REISERFS_FS_XATTR
904 #ifdef CONFIG_REISERFS_FS_SECURITY
907 #ifdef CONFIG_REISERFS_FS_POSIX_ACL
923 "xattrs/ACLs not supported "
924 "on pre-v3.6 format filesystems. "
945 static int xattr_hide_revalidate(
struct dentry *dentry,
unsigned int flags)
951 .d_revalidate = xattr_hide_revalidate,
956 struct dentry *dentry;
960 reiserfs_mutex_lock_safe(&s->
s_root->d_inode->i_mutex, s);
963 if (!IS_ERR(dentry)) {
964 REISERFS_SB(s)->priv_root = dentry;
969 err = PTR_ERR(dentry);
981 struct dentry *privroot = REISERFS_SB(s)->priv_root;
983 err = xattr_mount_check(s);
988 reiserfs_mutex_lock_safe(&s->
s_root->d_inode->i_mutex, s);
989 err = create_privroot(REISERFS_SB(s)->priv_root);
994 s->
s_xattr = reiserfs_xattr_handlers;
995 reiserfs_mutex_lock_safe(&privroot->
d_inode->i_mutex, s);
996 if (!REISERFS_SB(s)->xattr_root) {
997 struct dentry *dentry;
1000 if (!IS_ERR(dentry))
1001 REISERFS_SB(s)->xattr_root = dentry;
1003 err = PTR_ERR(dentry);