2 * linux/fs/reiserfs/xattr.c
4 * Copyright (c) 2002 by Jeff Mahoney, <jeffm@suse.com>
9 * In order to implement EA/ACLs in a clean, backwards compatible manner,
10 * they are implemented as files in a "private" directory.
11 * Each EA is in it's own file, with the directory layout like so (/ is assumed
12 * to be relative to fs root). Inside the /.reiserfs_priv/xattrs directory,
13 * directories named using the capital-hex form of the objectid and
14 * generation number are used. Inside each directory are individual files
15 * named with the name of the extended attribute.
17 * So, for objectid 12648430, we could have:
18 * /.reiserfs_priv/xattrs/C0FFEE.0/system.posix_acl_access
19 * /.reiserfs_priv/xattrs/C0FFEE.0/system.posix_acl_default
20 * /.reiserfs_priv/xattrs/C0FFEE.0/user.Content-Type
23 * The file contents are the text of the EA. The size is known based on the
24 * stat data describing the file.
26 * In the case of system.posix_acl_access and system.posix_acl_default, since
27 * these are special cases for filesystem ACLs, they are interpreted by the
28 * kernel, in addition, they are negatively and positively cached and attached
29 * to the inode so that unnecessary lookups are avoided.
32 #include <linux/reiserfs_fs.h>
33 #include <linux/capability.h>
34 #include <linux/dcache.h>
35 #include <linux/namei.h>
36 #include <linux/errno.h>
38 #include <linux/file.h>
39 #include <linux/pagemap.h>
40 #include <linux/xattr.h>
41 #include <linux/reiserfs_xattr.h>
42 #include <linux/reiserfs_acl.h>
43 #include <asm/uaccess.h>
44 #include <net/checksum.h>
45 #include <linux/smp_lock.h>
46 #include <linux/stat.h>
48 #define FL_READONLY 128
49 #define FL_DIR_SEM_HELD 256
50 #define PRIVROOT_NAME ".reiserfs_priv"
51 #define XAROOT_NAME "xattrs"
53 /* Returns the dentry referring to the root of the extended attribute
54 * directory tree. If it has already been retrieved, it is used. If it
55 * hasn't been created and the flags indicate creation is allowed, we
56 * attempt to create it. On error, we return a pointer-encoded error.
58 static struct dentry *get_xa_root(struct super_block *sb, int flags)
60 struct dentry *privroot = dget(REISERFS_SB(sb)->priv_root);
61 struct dentry *xaroot;
63 /* This needs to be created at mount-time */
65 return ERR_PTR(-ENODATA);
67 mutex_lock_nested(&privroot->d_inode->i_mutex, I_MUTEX_XATTR);
68 if (REISERFS_SB(sb)->xattr_root) {
69 xaroot = dget(REISERFS_SB(sb)->xattr_root);
73 xaroot = lookup_one_len(XAROOT_NAME, privroot, strlen(XAROOT_NAME));
76 } else if (!xaroot->d_inode) {
78 if (flags == 0 || flags & XATTR_CREATE)
79 err = privroot->d_inode->i_op->mkdir(privroot->d_inode,
83 xaroot = ERR_PTR(err);
87 REISERFS_SB(sb)->xattr_root = dget(xaroot);
90 mutex_unlock(&privroot->d_inode->i_mutex);
95 /* Opens the directory corresponding to the inode's extended attribute store.
96 * If flags allow, the tree to the directory may be created. If creation is
97 * prohibited, -ENODATA is returned. */
98 static struct dentry *open_xa_dir(const struct inode *inode, int flags)
100 struct dentry *xaroot, *xadir;
103 xaroot = get_xa_root(inode->i_sb, flags);
107 /* ok, we have xaroot open */
108 snprintf(namebuf, sizeof(namebuf), "%X.%X",
109 le32_to_cpu(INODE_PKEY(inode)->k_objectid),
110 inode->i_generation);
111 xadir = lookup_one_len(namebuf, xaroot, strlen(namebuf));
117 if (!xadir->d_inode) {
119 if (flags == 0 || flags & XATTR_CREATE) {
120 /* Although there is nothing else trying to create this directory,
121 * another directory with the same hash may be created, so we need
122 * to protect against that */
124 xaroot->d_inode->i_op->mkdir(xaroot->d_inode, xadir,
132 if (!xadir->d_inode) {
135 return ERR_PTR(-ENODATA);
144 * this is very similar to fs/reiserfs/dir.c:reiserfs_readdir, but
145 * we need to drop the path before calling the filldir struct. That
146 * would be a big performance hit to the non-xattr case, so I've copied
147 * the whole thing for now. --clm
149 * the big difference is that I go backwards through the directory,
150 * and don't mess with f->f_pos, but the idea is the same. Do some
151 * action on each and every entry in the directory.
153 * we're called with i_mutex held, so there are no worries about the directory
154 * changing underneath us.
156 static int __xattr_readdir(struct inode *inode, void *dirent, filldir_t filldir)
158 struct cpu_key pos_key; /* key of current position in the directory (key of directory entry) */
159 INITIALIZE_PATH(path_to_entry);
160 struct buffer_head *bh;
162 struct item_head *ih, tmp_ih;
166 char small_buf[32]; /* avoid kmalloc if we can */
167 struct reiserfs_de_head *deh;
172 struct reiserfs_dir_entry de;
174 /* form key for search the next directory entry using f_pos field of
176 next_pos = max_reiserfs_offset(inode);
180 if (next_pos <= DOT_DOT_OFFSET)
182 make_cpu_key(&pos_key, inode, next_pos, TYPE_DIRENTRY, 3);
185 search_by_entry_key(inode->i_sb, &pos_key, &path_to_entry,
187 if (search_res == IO_ERROR) {
188 // FIXME: we could just skip part of directory which could
190 pathrelse(&path_to_entry);
194 if (search_res == NAME_NOT_FOUND)
197 set_de_name_and_namelen(&de);
198 entry_num = de.de_entry_num;
199 deh = &(de.de_deh[entry_num]);
204 if (!is_direntry_le_ih(ih)) {
205 reiserfs_error(inode->i_sb, "jdm-20000",
206 "not direntry %h", ih);
209 copy_item_head(&tmp_ih, ih);
211 /* we must have found item, that is item of this directory, */
212 RFALSE(COMP_SHORT_KEYS(&(ih->ih_key), &pos_key),
213 "vs-9000: found item %h does not match to dir we readdir %K",
216 if (deh_offset(deh) <= DOT_DOT_OFFSET) {
220 /* look for the previous entry in the directory */
221 next_pos = deh_offset(deh) - 1;
223 if (!de_visible(deh))
224 /* it is hidden entry */
227 d_reclen = entry_length(bh, ih, entry_num);
228 d_name = B_I_DEH_ENTRY_FILE_NAME(bh, ih, deh);
229 d_off = deh_offset(deh);
230 d_ino = deh_objectid(deh);
232 if (!d_name[d_reclen - 1])
233 d_reclen = strlen(d_name);
235 if (d_reclen > REISERFS_MAX_NAME(inode->i_sb->s_blocksize)) {
236 /* too big to send back to VFS */
240 /* Ignore the .reiserfs_priv entry */
241 if (reiserfs_xattrs(inode->i_sb) &&
242 !old_format_only(inode->i_sb) &&
244 le32_to_cpu(INODE_PKEY
245 (REISERFS_SB(inode->i_sb)->priv_root->d_inode)->
249 if (d_reclen <= 32) {
250 local_buf = small_buf;
252 local_buf = kmalloc(d_reclen, GFP_NOFS);
254 pathrelse(&path_to_entry);
257 if (item_moved(&tmp_ih, &path_to_entry)) {
260 /* sigh, must retry. Do this same offset again */
266 // Note, that we copy name to user space via temporary
267 // buffer (local_buf) because filldir will block if
268 // user space buffer is swapped out. At that time
269 // entry can move to somewhere else
270 memcpy(local_buf, d_name, d_reclen);
272 /* the filldir function might need to start transactions,
273 * or do who knows what. Release the path now that we've
274 * copied all the important stuff out of the deh
276 pathrelse(&path_to_entry);
278 if (filldir(dirent, local_buf, d_reclen, d_off, d_ino,
280 if (local_buf != small_buf) {
285 if (local_buf != small_buf) {
291 pathrelse(&path_to_entry);
296 * this could be done with dedicated readdir ops for the xattr files,
297 * but I want to get something working asap
298 * this is stolen from vfs_readdir
302 int xattr_readdir(struct inode *inode, filldir_t filler, void *buf)
305 mutex_lock_nested(&inode->i_mutex, I_MUTEX_XATTR);
306 if (!IS_DEADDIR(inode)) {
308 res = __xattr_readdir(inode, buf, filler);
311 mutex_unlock(&inode->i_mutex);
316 __reiserfs_xattr_del(struct dentry *xadir, const char *name, int namelen)
318 struct dentry *dentry;
319 struct inode *dir = xadir->d_inode;
322 dentry = lookup_one_len(name, xadir, namelen);
323 if (IS_ERR(dentry)) {
324 err = PTR_ERR(dentry);
326 } else if (!dentry->d_inode) {
331 /* Skip directories.. */
332 if (S_ISDIR(dentry->d_inode->i_mode))
335 if (!IS_PRIVATE(dentry->d_inode)) {
336 reiserfs_error(dir->i_sb, "jdm-20003",
337 "OID %08x [%.*s/%.*s] doesn't have "
338 "priv flag set [parent is %sset].",
339 le32_to_cpu(INODE_PKEY(dentry->d_inode)->
340 k_objectid), xadir->d_name.len,
341 xadir->d_name.name, namelen, name,
342 IS_PRIVATE(xadir->d_inode) ? "" :
348 err = dir->i_op->unlink(dir, dentry);
359 /* The following are side effects of other operations that aren't explicitly
360 * modifying extended attributes. This includes operations such as permissions
361 * or ownership changes, object deletions, etc. */
364 reiserfs_delete_xattrs_filler(void *buf, const char *name, int namelen,
365 loff_t offset, u64 ino, unsigned int d_type)
367 struct dentry *xadir = (struct dentry *)buf;
369 return __reiserfs_xattr_del(xadir, name, namelen);
373 /* This is called w/ inode->i_mutex downed */
374 int reiserfs_delete_xattrs(struct inode *inode)
376 struct dentry *dir, *root;
379 /* Skip out, an xattr has no xattrs associated with it */
380 if (IS_PRIVATE(inode) || get_inode_sd_version(inode) == STAT_DATA_V1)
383 reiserfs_read_lock_xattrs(inode->i_sb);
384 dir = open_xa_dir(inode, FL_READONLY);
385 reiserfs_read_unlock_xattrs(inode->i_sb);
389 } else if (!dir->d_inode) {
395 err = xattr_readdir(dir->d_inode, reiserfs_delete_xattrs_filler, dir);
401 /* Leftovers besides . and .. -- that's not good. */
402 if (dir->d_inode->i_nlink <= 2) {
403 root = get_xa_root(inode->i_sb, XATTR_REPLACE);
404 reiserfs_write_lock_xattrs(inode->i_sb);
405 err = vfs_rmdir(root->d_inode, dir);
406 reiserfs_write_unlock_xattrs(inode->i_sb);
409 reiserfs_warning(inode->i_sb, "jdm-20006",
410 "Couldn't remove all entries in directory");
419 REISERFS_I(inode)->i_flags =
420 REISERFS_I(inode)->i_flags & ~i_has_xattr_dir;
424 struct reiserfs_chown_buf {
426 struct dentry *xadir;
430 /* XXX: If there is a better way to do this, I'd love to hear about it */
432 reiserfs_chown_xattrs_filler(void *buf, const char *name, int namelen,
433 loff_t offset, u64 ino, unsigned int d_type)
435 struct reiserfs_chown_buf *chown_buf = (struct reiserfs_chown_buf *)buf;
436 struct dentry *xafile, *xadir = chown_buf->xadir;
437 struct iattr *attrs = chown_buf->attrs;
440 xafile = lookup_one_len(name, xadir, namelen);
442 return PTR_ERR(xafile);
443 else if (!xafile->d_inode) {
448 if (!S_ISDIR(xafile->d_inode->i_mode))
449 err = notify_change(xafile, attrs);
455 int reiserfs_chown_xattrs(struct inode *inode, struct iattr *attrs)
459 struct reiserfs_chown_buf buf;
460 unsigned int ia_valid = attrs->ia_valid;
462 /* Skip out, an xattr has no xattrs associated with it */
463 if (IS_PRIVATE(inode) || get_inode_sd_version(inode) == STAT_DATA_V1)
466 reiserfs_read_lock_xattrs(inode->i_sb);
467 dir = open_xa_dir(inode, FL_READONLY);
468 reiserfs_read_unlock_xattrs(inode->i_sb);
470 if (PTR_ERR(dir) != -ENODATA)
473 } else if (!dir->d_inode) {
480 attrs->ia_valid &= (ATTR_UID | ATTR_GID | ATTR_CTIME);
485 err = xattr_readdir(dir->d_inode, reiserfs_chown_xattrs_filler, &buf);
491 err = notify_change(dir, attrs);
498 attrs->ia_valid = ia_valid;
502 #ifdef CONFIG_REISERFS_FS_XATTR
503 static struct reiserfs_xattr_handler *find_xattr_handler_prefix(const char
506 /* Returns a dentry corresponding to a specific extended attribute file
507 * for the inode. If flags allow, the file is created. Otherwise, a
508 * valid or negative dentry, or an error is returned. */
509 static struct dentry *get_xa_file_dentry(const struct inode *inode,
510 const char *name, int flags)
512 struct dentry *xadir, *xafile;
515 xadir = open_xa_dir(inode, flags);
517 return ERR_CAST(xadir);
518 } else if (xadir && !xadir->d_inode) {
520 return ERR_PTR(-ENODATA);
523 xafile = lookup_one_len(name, xadir, strlen(name));
524 if (IS_ERR(xafile)) {
526 return ERR_CAST(xafile);
529 if (xafile->d_inode) { /* file exists */
530 if (flags & XATTR_CREATE) {
535 } else if (flags & XATTR_REPLACE || flags & FL_READONLY) {
538 /* inode->i_mutex is down, so nothing else can try to create
540 err = xadir->d_inode->i_op->create(xadir->d_inode, xafile,
541 0700 | S_IFREG, NULL);
552 xafile = ERR_PTR(err);
553 else if (!xafile->d_inode) {
555 xafile = ERR_PTR(-ENODATA);
560 /* Internal operations on file data */
561 static inline void reiserfs_put_page(struct page *page)
564 page_cache_release(page);
567 static struct page *reiserfs_get_page(struct inode *dir, size_t n)
569 struct address_space *mapping = dir->i_mapping;
571 /* We can deadlock if we try to free dentries,
572 and an unlink/rmdir has just occured - GFP_NOFS avoids this */
573 mapping_set_gfp_mask(mapping, GFP_NOFS);
574 page = read_mapping_page(mapping, n >> PAGE_CACHE_SHIFT, NULL);
583 reiserfs_put_page(page);
584 return ERR_PTR(-EIO);
587 static inline __u32 xattr_hash(const char *msg, int len)
589 return csum_partial(msg, len, 0);
592 int reiserfs_commit_write(struct file *f, struct page *page,
593 unsigned from, unsigned to);
594 int reiserfs_prepare_write(struct file *f, struct page *page,
595 unsigned from, unsigned to);
598 /* Generic extended attribute operations that can be used by xa plugins */
601 * inode->i_mutex: down
604 reiserfs_xattr_set(struct inode *inode, const char *name, const void *buffer,
605 size_t buffer_size, int flags)
608 struct dentry *dentry;
612 size_t buffer_pos = 0;
613 struct iattr newattrs;
616 if (get_inode_sd_version(inode) == STAT_DATA_V1)
619 /* Empty xattrs are ok, they're just empty files, no hash */
620 if (buffer && buffer_size)
621 xahash = xattr_hash(buffer, buffer_size);
623 dentry = get_xa_file_dentry(inode, name, flags);
624 if (IS_ERR(dentry)) {
625 err = PTR_ERR(dentry);
629 REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
631 /* Resize it so we're ok to write there */
632 newattrs.ia_size = buffer_size;
633 newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
634 mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_XATTR);
635 err = notify_change(dentry, &newattrs);
639 while (buffer_pos < buffer_size || buffer_pos == 0) {
642 size_t page_offset = (file_pos & (PAGE_CACHE_SIZE - 1));
643 if (buffer_size - buffer_pos > PAGE_CACHE_SIZE)
644 chunk = PAGE_CACHE_SIZE;
646 chunk = buffer_size - buffer_pos;
648 page = reiserfs_get_page(dentry->d_inode, file_pos);
655 data = page_address(page);
658 struct reiserfs_xattr_header *rxh;
659 skip = file_pos = sizeof(struct reiserfs_xattr_header);
660 if (chunk + skip > PAGE_CACHE_SIZE)
661 chunk = PAGE_CACHE_SIZE - skip;
662 rxh = (struct reiserfs_xattr_header *)data;
663 rxh->h_magic = cpu_to_le32(REISERFS_XATTR_MAGIC);
664 rxh->h_hash = cpu_to_le32(xahash);
667 err = reiserfs_prepare_write(NULL, page, page_offset,
668 page_offset + chunk + skip);
671 memcpy(data + skip, buffer + buffer_pos, chunk);
672 err = reiserfs_commit_write(NULL, page, page_offset,
673 page_offset + chunk +
677 reiserfs_put_page(page);
681 if (err || buffer_size == 0 || !buffer)
685 /* We can't mark the inode dirty if it's not hashed. This is the case
686 * when we're inheriting the default ACL. If we dirty it, the inode
687 * gets marked dirty, but won't (ever) make it onto the dirty list until
688 * it's synced explicitly to clear I_DIRTY. This is bad. */
689 if (!hlist_unhashed(&inode->i_hash)) {
690 inode->i_ctime = CURRENT_TIME_SEC;
691 mark_inode_dirty(inode);
695 mutex_unlock(&dentry->d_inode->i_mutex);
703 * inode->i_mutex: down
706 reiserfs_xattr_get(const struct inode *inode, const char *name, void *buffer,
710 struct dentry *dentry;
713 size_t buffer_pos = 0;
720 /* We can't have xattrs attached to v1 items since they don't have
721 * generation numbers */
722 if (get_inode_sd_version(inode) == STAT_DATA_V1)
725 dentry = get_xa_file_dentry(inode, name, FL_READONLY);
726 if (IS_ERR(dentry)) {
727 err = PTR_ERR(dentry);
731 isize = i_size_read(dentry->d_inode);
732 REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
734 /* Just return the size needed */
735 if (buffer == NULL) {
736 err = isize - sizeof(struct reiserfs_xattr_header);
740 if (buffer_size < isize - sizeof(struct reiserfs_xattr_header)) {
745 while (file_pos < isize) {
749 if (isize - file_pos > PAGE_CACHE_SIZE)
750 chunk = PAGE_CACHE_SIZE;
752 chunk = isize - file_pos;
754 page = reiserfs_get_page(dentry->d_inode, file_pos);
761 data = page_address(page);
763 struct reiserfs_xattr_header *rxh =
764 (struct reiserfs_xattr_header *)data;
765 skip = file_pos = sizeof(struct reiserfs_xattr_header);
767 /* Magic doesn't match up.. */
768 if (rxh->h_magic != cpu_to_le32(REISERFS_XATTR_MAGIC)) {
770 reiserfs_put_page(page);
771 reiserfs_warning(inode->i_sb, "jdm-20001",
772 "Invalid magic for xattr (%s) "
773 "associated with %k", name,
778 hash = le32_to_cpu(rxh->h_hash);
780 memcpy(buffer + buffer_pos, data + skip, chunk);
782 reiserfs_put_page(page);
787 err = isize - sizeof(struct reiserfs_xattr_header);
789 if (xattr_hash(buffer, isize - sizeof(struct reiserfs_xattr_header)) !=
791 reiserfs_warning(inode->i_sb, "jdm-20002",
792 "Invalid hash for xattr (%s) associated "
793 "with %k", name, INODE_PKEY(inode));
804 int reiserfs_xattr_del(struct inode *inode, const char *name)
809 dir = open_xa_dir(inode, FL_READONLY);
815 err = __reiserfs_xattr_del(dir, name, strlen(name));
819 inode->i_ctime = CURRENT_TIME_SEC;
820 mark_inode_dirty(inode);
827 /* Actual operations that are exported to VFS-land */
830 * Inode operation getxattr()
831 * Preliminary locking: we down dentry->d_inode->i_mutex
834 reiserfs_getxattr(struct dentry * dentry, const char *name, void *buffer,
837 struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
840 if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
841 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
844 reiserfs_read_lock_xattr_i(dentry->d_inode);
845 reiserfs_read_lock_xattrs(dentry->d_sb);
846 err = xah->get(dentry->d_inode, name, buffer, size);
847 reiserfs_read_unlock_xattrs(dentry->d_sb);
848 reiserfs_read_unlock_xattr_i(dentry->d_inode);
853 * Inode operation setxattr()
855 * dentry->d_inode->i_mutex down
858 reiserfs_setxattr(struct dentry *dentry, const char *name, const void *value,
859 size_t size, int flags)
861 struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
865 if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
866 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
869 reiserfs_write_lock_xattr_i(dentry->d_inode);
870 lock = !has_xattr_dir(dentry->d_inode);
872 reiserfs_write_lock_xattrs(dentry->d_sb);
874 reiserfs_read_lock_xattrs(dentry->d_sb);
875 err = xah->set(dentry->d_inode, name, value, size, flags);
877 reiserfs_write_unlock_xattrs(dentry->d_sb);
879 reiserfs_read_unlock_xattrs(dentry->d_sb);
880 reiserfs_write_unlock_xattr_i(dentry->d_inode);
885 * Inode operation removexattr()
887 * dentry->d_inode->i_mutex down
889 int reiserfs_removexattr(struct dentry *dentry, const char *name)
892 struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
894 if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
895 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
898 reiserfs_write_lock_xattr_i(dentry->d_inode);
899 reiserfs_read_lock_xattrs(dentry->d_sb);
901 /* Deletion pre-operation */
903 err = xah->del(dentry->d_inode, name);
908 err = reiserfs_xattr_del(dentry->d_inode, name);
910 dentry->d_inode->i_ctime = CURRENT_TIME_SEC;
911 mark_inode_dirty(dentry->d_inode);
914 reiserfs_read_unlock_xattrs(dentry->d_sb);
915 reiserfs_write_unlock_xattr_i(dentry->d_inode);
919 /* This is what filldir will use:
920 * r_pos will always contain the amount of space required for the entire
921 * list. If r_pos becomes larger than r_size, we need more space and we
922 * return an error indicating this. If r_pos is less than r_size, then we've
923 * filled the buffer successfully and we return success */
924 struct reiserfs_listxattr_buf {
928 struct inode *r_inode;
932 reiserfs_listxattr_filler(void *buf, const char *name, int namelen,
933 loff_t offset, u64 ino, unsigned int d_type)
935 struct reiserfs_listxattr_buf *b = (struct reiserfs_listxattr_buf *)buf;
938 || (namelen != 1 && (name[1] != '.' || namelen != 2))) {
939 struct reiserfs_xattr_handler *xah =
940 find_xattr_handler_prefix(name);
942 return 0; /* Unsupported xattr name, skip it */
944 /* We call ->list() twice because the operation isn't required to just
945 * return the name back - we want to make sure we have enough space */
946 len += xah->list(b->r_inode, name, namelen, NULL);
949 if (b->r_pos + len + 1 <= b->r_size) {
950 char *p = b->r_buf + b->r_pos;
951 p += xah->list(b->r_inode, name, namelen, p);
962 * Inode operation listxattr()
964 * Preliminary locking: we down dentry->d_inode->i_mutex
966 ssize_t reiserfs_listxattr(struct dentry * dentry, char *buffer, size_t size)
970 struct reiserfs_listxattr_buf buf;
972 if (!dentry->d_inode)
975 if (!reiserfs_xattrs(dentry->d_sb) ||
976 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
979 reiserfs_read_lock_xattr_i(dentry->d_inode);
980 reiserfs_read_lock_xattrs(dentry->d_sb);
981 dir = open_xa_dir(dentry->d_inode, FL_READONLY);
982 reiserfs_read_unlock_xattrs(dentry->d_sb);
986 err = 0; /* Not an error if there aren't any xattrs */
991 buf.r_size = buffer ? size : 0;
993 buf.r_inode = dentry->d_inode;
995 REISERFS_I(dentry->d_inode)->i_flags |= i_has_xattr_dir;
997 err = xattr_readdir(dir->d_inode, reiserfs_listxattr_filler, &buf);
1001 if (buf.r_pos > buf.r_size && buffer != NULL)
1010 reiserfs_read_unlock_xattr_i(dentry->d_inode);
1014 /* This is the implementation for the xattr plugin infrastructure */
1015 static LIST_HEAD(xattr_handlers);
1016 static DEFINE_RWLOCK(handler_lock);
1018 static struct reiserfs_xattr_handler *find_xattr_handler_prefix(const char
1021 struct reiserfs_xattr_handler *xah = NULL;
1022 struct list_head *p;
1024 read_lock(&handler_lock);
1025 list_for_each(p, &xattr_handlers) {
1026 xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
1027 if (strncmp(xah->prefix, prefix, strlen(xah->prefix)) == 0)
1032 read_unlock(&handler_lock);
1036 static void __unregister_handlers(void)
1038 struct reiserfs_xattr_handler *xah;
1039 struct list_head *p, *tmp;
1041 list_for_each_safe(p, tmp, &xattr_handlers) {
1042 xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
1048 INIT_LIST_HEAD(&xattr_handlers);
1051 int __init reiserfs_xattr_register_handlers(void)
1054 struct reiserfs_xattr_handler *xah;
1055 struct list_head *p;
1057 write_lock(&handler_lock);
1059 /* If we're already initialized, nothing to do */
1060 if (!list_empty(&xattr_handlers)) {
1061 write_unlock(&handler_lock);
1065 /* Add the handlers */
1066 list_add_tail(&user_handler.handlers, &xattr_handlers);
1067 list_add_tail(&trusted_handler.handlers, &xattr_handlers);
1068 #ifdef CONFIG_REISERFS_FS_SECURITY
1069 list_add_tail(&security_handler.handlers, &xattr_handlers);
1071 #ifdef CONFIG_REISERFS_FS_POSIX_ACL
1072 list_add_tail(&posix_acl_access_handler.handlers, &xattr_handlers);
1073 list_add_tail(&posix_acl_default_handler.handlers, &xattr_handlers);
1076 /* Run initializers, if available */
1077 list_for_each(p, &xattr_handlers) {
1078 xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
1088 /* Clean up other handlers, if any failed */
1090 __unregister_handlers();
1092 write_unlock(&handler_lock);
1096 void reiserfs_xattr_unregister_handlers(void)
1098 write_lock(&handler_lock);
1099 __unregister_handlers();
1100 write_unlock(&handler_lock);
1103 static int reiserfs_check_acl(struct inode *inode, int mask)
1105 struct posix_acl *acl;
1106 int error = -EAGAIN; /* do regular unix permission checks by default */
1108 reiserfs_read_lock_xattr_i(inode);
1109 reiserfs_read_lock_xattrs(inode->i_sb);
1111 acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS);
1113 reiserfs_read_unlock_xattrs(inode->i_sb);
1114 reiserfs_read_unlock_xattr_i(inode);
1118 error = posix_acl_permission(inode, acl, mask);
1119 posix_acl_release(acl);
1120 } else if (PTR_ERR(acl) != -ENODATA)
1121 error = PTR_ERR(acl);
1127 int reiserfs_permission(struct inode *inode, int mask)
1130 * We don't do permission checks on the internal objects.
1131 * Permissions are determined by the "owning" object.
1133 if (IS_PRIVATE(inode))
1136 * Stat data v1 doesn't support ACLs.
1138 if (get_inode_sd_version(inode) == STAT_DATA_V1)
1139 return generic_permission(inode, mask, NULL);
1141 return generic_permission(inode, mask, reiserfs_check_acl);
1144 static int create_privroot(struct dentry *dentry)
1147 struct inode *inode = dentry->d_parent->d_inode;
1148 mutex_lock_nested(&inode->i_mutex, I_MUTEX_XATTR);
1149 err = inode->i_op->mkdir(inode, dentry, 0700);
1150 mutex_unlock(&inode->i_mutex);
1156 if (dentry && dentry->d_inode)
1157 reiserfs_info(dentry->d_sb, "Created %s - reserved for xattr "
1158 "storage.\n", PRIVROOT_NAME);
1163 static int xattr_mount_check(struct super_block *s)
1165 /* We need generation numbers to ensure that the oid mapping is correct
1166 * v3.5 filesystems don't have them. */
1167 if (!old_format_only(s)) {
1168 set_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
1169 } else if (reiserfs_xattrs_optional(s)) {
1170 /* Old format filesystem, but optional xattrs have been enabled
1171 * at mount time. Error out. */
1172 reiserfs_warning(s, "jdm-20005",
1173 "xattrs/ACLs not supported on pre v3.6 "
1174 "format filesystem. Failing mount.");
1177 /* Old format filesystem, but no optional xattrs have
1178 * been enabled. This means we silently disable xattrs
1179 * on the filesystem. */
1180 clear_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
1187 int __init reiserfs_xattr_register_handlers(void) { return 0; }
1188 void reiserfs_xattr_unregister_handlers(void) {}
1191 /* This will catch lookups from the fs root to .reiserfs_priv */
1193 xattr_lookup_poison(struct dentry *dentry, struct qstr *q1, struct qstr *name)
1195 struct dentry *priv_root = REISERFS_SB(dentry->d_sb)->priv_root;
1196 if (name->len == priv_root->d_name.len &&
1197 name->hash == priv_root->d_name.hash &&
1198 !memcmp(name->name, priv_root->d_name.name, name->len)) {
1200 } else if (q1->len == name->len &&
1201 !memcmp(q1->name, name->name, name->len))
1206 static struct dentry_operations xattr_lookup_poison_ops = {
1207 .d_compare = xattr_lookup_poison,
1210 /* We need to take a copy of the mount flags since things like
1211 * MS_RDONLY don't get set until *after* we're called.
1212 * mount_flags != mount_options */
1213 int reiserfs_xattr_init(struct super_block *s, int mount_flags)
1217 #ifdef CONFIG_REISERFS_FS_XATTR
1218 err = xattr_mount_check(s);
1223 /* If we don't have the privroot located yet - go find it */
1224 if (!REISERFS_SB(s)->priv_root) {
1225 struct dentry *dentry;
1226 dentry = lookup_one_len(PRIVROOT_NAME, s->s_root,
1227 strlen(PRIVROOT_NAME));
1228 if (!IS_ERR(dentry)) {
1229 #ifdef CONFIG_REISERFS_FS_XATTR
1230 if (!(mount_flags & MS_RDONLY) && !dentry->d_inode)
1231 err = create_privroot(dentry);
1233 if (!dentry->d_inode) {
1238 err = PTR_ERR(dentry);
1240 if (!err && dentry) {
1241 s->s_root->d_op = &xattr_lookup_poison_ops;
1242 dentry->d_inode->i_flags |= S_PRIVATE;
1243 REISERFS_SB(s)->priv_root = dentry;
1244 #ifdef CONFIG_REISERFS_FS_XATTR
1245 /* xattrs are unavailable */
1246 } else if (!(mount_flags & MS_RDONLY)) {
1247 /* If we're read-only it just means that the dir
1248 * hasn't been created. Not an error -- just no
1249 * xattrs on the fs. We'll check again if we
1251 reiserfs_warning(s, "jdm-20006",
1252 "xattrs/ACLs enabled and couldn't "
1253 "find/create .reiserfs_priv. "
1260 #ifdef CONFIG_REISERFS_FS_XATTR
1263 clear_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
1264 clear_bit(REISERFS_XATTRS_USER, &(REISERFS_SB(s)->s_mount_opt));
1265 clear_bit(REISERFS_POSIXACL, &(REISERFS_SB(s)->s_mount_opt));
1269 /* The super_block MS_POSIXACL must mirror the (no)acl mount option. */
1270 s->s_flags = s->s_flags & ~MS_POSIXACL;
1271 #ifdef CONFIG_REISERFS_FS_POSIX_ACL
1272 if (reiserfs_posixacl(s))
1273 s->s_flags |= MS_POSIXACL;