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 static struct reiserfs_xattr_handler *find_xattr_handler_prefix(const char
56 /* Returns the dentry referring to the root of the extended attribute
57 * directory tree. If it has already been retrieved, it is used. If it
58 * hasn't been created and the flags indicate creation is allowed, we
59 * attempt to create it. On error, we return a pointer-encoded error.
61 static struct dentry *get_xa_root(struct super_block *sb, int flags)
63 struct dentry *privroot = dget(REISERFS_SB(sb)->priv_root);
64 struct dentry *xaroot;
66 /* This needs to be created at mount-time */
68 return ERR_PTR(-ENODATA);
70 mutex_lock_nested(&privroot->d_inode->i_mutex, I_MUTEX_XATTR);
71 if (REISERFS_SB(sb)->xattr_root) {
72 xaroot = dget(REISERFS_SB(sb)->xattr_root);
76 xaroot = lookup_one_len(XAROOT_NAME, privroot, strlen(XAROOT_NAME));
79 } else if (!xaroot->d_inode) {
81 if (flags == 0 || flags & XATTR_CREATE)
82 err = privroot->d_inode->i_op->mkdir(privroot->d_inode,
86 xaroot = ERR_PTR(err);
90 REISERFS_SB(sb)->xattr_root = dget(xaroot);
93 mutex_unlock(&privroot->d_inode->i_mutex);
98 /* Opens the directory corresponding to the inode's extended attribute store.
99 * If flags allow, the tree to the directory may be created. If creation is
100 * prohibited, -ENODATA is returned. */
101 static struct dentry *open_xa_dir(const struct inode *inode, int flags)
103 struct dentry *xaroot, *xadir;
106 xaroot = get_xa_root(inode->i_sb, flags);
110 /* ok, we have xaroot open */
111 snprintf(namebuf, sizeof(namebuf), "%X.%X",
112 le32_to_cpu(INODE_PKEY(inode)->k_objectid),
113 inode->i_generation);
114 xadir = lookup_one_len(namebuf, xaroot, strlen(namebuf));
120 if (!xadir->d_inode) {
122 if (flags == 0 || flags & XATTR_CREATE) {
123 /* Although there is nothing else trying to create this directory,
124 * another directory with the same hash may be created, so we need
125 * to protect against that */
127 xaroot->d_inode->i_op->mkdir(xaroot->d_inode, xadir,
135 if (!xadir->d_inode) {
138 return ERR_PTR(-ENODATA);
146 /* Returns a dentry corresponding to a specific extended attribute file
147 * for the inode. If flags allow, the file is created. Otherwise, a
148 * valid or negative dentry, or an error is returned. */
149 static struct dentry *get_xa_file_dentry(const struct inode *inode,
150 const char *name, int flags)
152 struct dentry *xadir, *xafile;
155 xadir = open_xa_dir(inode, flags);
157 return ERR_CAST(xadir);
158 } else if (!xadir->d_inode) {
160 return ERR_PTR(-ENODATA);
163 xafile = lookup_one_len(name, xadir, strlen(name));
164 if (IS_ERR(xafile)) {
166 return ERR_CAST(xafile);
169 if (xafile->d_inode) { /* file exists */
170 if (flags & XATTR_CREATE) {
175 } else if (flags & XATTR_REPLACE || flags & FL_READONLY) {
178 /* inode->i_mutex is down, so nothing else can try to create
180 err = xadir->d_inode->i_op->create(xadir->d_inode, xafile,
181 0700 | S_IFREG, NULL);
192 xafile = ERR_PTR(err);
193 else if (!xafile->d_inode) {
195 xafile = ERR_PTR(-ENODATA);
201 * this is very similar to fs/reiserfs/dir.c:reiserfs_readdir, but
202 * we need to drop the path before calling the filldir struct. That
203 * would be a big performance hit to the non-xattr case, so I've copied
204 * the whole thing for now. --clm
206 * the big difference is that I go backwards through the directory,
207 * and don't mess with f->f_pos, but the idea is the same. Do some
208 * action on each and every entry in the directory.
210 * we're called with i_mutex held, so there are no worries about the directory
211 * changing underneath us.
213 static int __xattr_readdir(struct inode *inode, void *dirent, filldir_t filldir)
215 struct cpu_key pos_key; /* key of current position in the directory (key of directory entry) */
216 INITIALIZE_PATH(path_to_entry);
217 struct buffer_head *bh;
219 struct item_head *ih, tmp_ih;
223 char small_buf[32]; /* avoid kmalloc if we can */
224 struct reiserfs_de_head *deh;
229 struct reiserfs_dir_entry de;
231 /* form key for search the next directory entry using f_pos field of
233 next_pos = max_reiserfs_offset(inode);
237 if (next_pos <= DOT_DOT_OFFSET)
239 make_cpu_key(&pos_key, inode, next_pos, TYPE_DIRENTRY, 3);
242 search_by_entry_key(inode->i_sb, &pos_key, &path_to_entry,
244 if (search_res == IO_ERROR) {
245 // FIXME: we could just skip part of directory which could
247 pathrelse(&path_to_entry);
251 if (search_res == NAME_NOT_FOUND)
254 set_de_name_and_namelen(&de);
255 entry_num = de.de_entry_num;
256 deh = &(de.de_deh[entry_num]);
261 if (!is_direntry_le_ih(ih)) {
262 reiserfs_error(inode->i_sb, "jdm-20000",
263 "not direntry %h", ih);
266 copy_item_head(&tmp_ih, ih);
268 /* we must have found item, that is item of this directory, */
269 RFALSE(COMP_SHORT_KEYS(&(ih->ih_key), &pos_key),
270 "vs-9000: found item %h does not match to dir we readdir %K",
273 if (deh_offset(deh) <= DOT_DOT_OFFSET) {
277 /* look for the previous entry in the directory */
278 next_pos = deh_offset(deh) - 1;
280 if (!de_visible(deh))
281 /* it is hidden entry */
284 d_reclen = entry_length(bh, ih, entry_num);
285 d_name = B_I_DEH_ENTRY_FILE_NAME(bh, ih, deh);
286 d_off = deh_offset(deh);
287 d_ino = deh_objectid(deh);
289 if (!d_name[d_reclen - 1])
290 d_reclen = strlen(d_name);
292 if (d_reclen > REISERFS_MAX_NAME(inode->i_sb->s_blocksize)) {
293 /* too big to send back to VFS */
297 /* Ignore the .reiserfs_priv entry */
298 if (reiserfs_xattrs(inode->i_sb) &&
299 !old_format_only(inode->i_sb) &&
301 le32_to_cpu(INODE_PKEY
302 (REISERFS_SB(inode->i_sb)->priv_root->d_inode)->
306 if (d_reclen <= 32) {
307 local_buf = small_buf;
309 local_buf = kmalloc(d_reclen, GFP_NOFS);
311 pathrelse(&path_to_entry);
314 if (item_moved(&tmp_ih, &path_to_entry)) {
317 /* sigh, must retry. Do this same offset again */
323 // Note, that we copy name to user space via temporary
324 // buffer (local_buf) because filldir will block if
325 // user space buffer is swapped out. At that time
326 // entry can move to somewhere else
327 memcpy(local_buf, d_name, d_reclen);
329 /* the filldir function might need to start transactions,
330 * or do who knows what. Release the path now that we've
331 * copied all the important stuff out of the deh
333 pathrelse(&path_to_entry);
335 if (filldir(dirent, local_buf, d_reclen, d_off, d_ino,
337 if (local_buf != small_buf) {
342 if (local_buf != small_buf) {
348 pathrelse(&path_to_entry);
353 * this could be done with dedicated readdir ops for the xattr files,
354 * but I want to get something working asap
355 * this is stolen from vfs_readdir
359 int xattr_readdir(struct inode *inode, filldir_t filler, void *buf)
362 mutex_lock_nested(&inode->i_mutex, I_MUTEX_XATTR);
363 if (!IS_DEADDIR(inode)) {
365 res = __xattr_readdir(inode, buf, filler);
368 mutex_unlock(&inode->i_mutex);
372 /* Internal operations on file data */
373 static inline void reiserfs_put_page(struct page *page)
376 page_cache_release(page);
379 static struct page *reiserfs_get_page(struct inode *dir, size_t n)
381 struct address_space *mapping = dir->i_mapping;
383 /* We can deadlock if we try to free dentries,
384 and an unlink/rmdir has just occured - GFP_NOFS avoids this */
385 mapping_set_gfp_mask(mapping, GFP_NOFS);
386 page = read_mapping_page(mapping, n >> PAGE_CACHE_SHIFT, NULL);
395 reiserfs_put_page(page);
396 return ERR_PTR(-EIO);
399 static inline __u32 xattr_hash(const char *msg, int len)
401 return csum_partial(msg, len, 0);
404 int reiserfs_commit_write(struct file *f, struct page *page,
405 unsigned from, unsigned to);
406 int reiserfs_prepare_write(struct file *f, struct page *page,
407 unsigned from, unsigned to);
410 /* Generic extended attribute operations that can be used by xa plugins */
413 * inode->i_mutex: down
416 reiserfs_xattr_set(struct inode *inode, const char *name, const void *buffer,
417 size_t buffer_size, int flags)
420 struct dentry *dentry;
424 size_t buffer_pos = 0;
425 struct iattr newattrs;
428 if (get_inode_sd_version(inode) == STAT_DATA_V1)
431 /* Empty xattrs are ok, they're just empty files, no hash */
432 if (buffer && buffer_size)
433 xahash = xattr_hash(buffer, buffer_size);
435 dentry = get_xa_file_dentry(inode, name, flags);
436 if (IS_ERR(dentry)) {
437 err = PTR_ERR(dentry);
441 REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
443 /* Resize it so we're ok to write there */
444 newattrs.ia_size = buffer_size;
445 newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
446 mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_XATTR);
447 err = notify_change(dentry, &newattrs);
451 while (buffer_pos < buffer_size || buffer_pos == 0) {
454 size_t page_offset = (file_pos & (PAGE_CACHE_SIZE - 1));
455 if (buffer_size - buffer_pos > PAGE_CACHE_SIZE)
456 chunk = PAGE_CACHE_SIZE;
458 chunk = buffer_size - buffer_pos;
460 page = reiserfs_get_page(dentry->d_inode, file_pos);
467 data = page_address(page);
470 struct reiserfs_xattr_header *rxh;
471 skip = file_pos = sizeof(struct reiserfs_xattr_header);
472 if (chunk + skip > PAGE_CACHE_SIZE)
473 chunk = PAGE_CACHE_SIZE - skip;
474 rxh = (struct reiserfs_xattr_header *)data;
475 rxh->h_magic = cpu_to_le32(REISERFS_XATTR_MAGIC);
476 rxh->h_hash = cpu_to_le32(xahash);
479 err = reiserfs_prepare_write(NULL, page, page_offset,
480 page_offset + chunk + skip);
483 memcpy(data + skip, buffer + buffer_pos, chunk);
484 err = reiserfs_commit_write(NULL, page, page_offset,
485 page_offset + chunk +
489 reiserfs_put_page(page);
493 if (err || buffer_size == 0 || !buffer)
497 /* We can't mark the inode dirty if it's not hashed. This is the case
498 * when we're inheriting the default ACL. If we dirty it, the inode
499 * gets marked dirty, but won't (ever) make it onto the dirty list until
500 * it's synced explicitly to clear I_DIRTY. This is bad. */
501 if (!hlist_unhashed(&inode->i_hash)) {
502 inode->i_ctime = CURRENT_TIME_SEC;
503 mark_inode_dirty(inode);
507 mutex_unlock(&dentry->d_inode->i_mutex);
515 * inode->i_mutex: down
518 reiserfs_xattr_get(const struct inode *inode, const char *name, void *buffer,
522 struct dentry *dentry;
525 size_t buffer_pos = 0;
532 /* We can't have xattrs attached to v1 items since they don't have
533 * generation numbers */
534 if (get_inode_sd_version(inode) == STAT_DATA_V1)
537 dentry = get_xa_file_dentry(inode, name, FL_READONLY);
538 if (IS_ERR(dentry)) {
539 err = PTR_ERR(dentry);
543 isize = i_size_read(dentry->d_inode);
544 REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
546 /* Just return the size needed */
547 if (buffer == NULL) {
548 err = isize - sizeof(struct reiserfs_xattr_header);
552 if (buffer_size < isize - sizeof(struct reiserfs_xattr_header)) {
557 while (file_pos < isize) {
561 if (isize - file_pos > PAGE_CACHE_SIZE)
562 chunk = PAGE_CACHE_SIZE;
564 chunk = isize - file_pos;
566 page = reiserfs_get_page(dentry->d_inode, file_pos);
573 data = page_address(page);
575 struct reiserfs_xattr_header *rxh =
576 (struct reiserfs_xattr_header *)data;
577 skip = file_pos = sizeof(struct reiserfs_xattr_header);
579 /* Magic doesn't match up.. */
580 if (rxh->h_magic != cpu_to_le32(REISERFS_XATTR_MAGIC)) {
582 reiserfs_put_page(page);
583 reiserfs_warning(inode->i_sb, "jdm-20001",
584 "Invalid magic for xattr (%s) "
585 "associated with %k", name,
590 hash = le32_to_cpu(rxh->h_hash);
592 memcpy(buffer + buffer_pos, data + skip, chunk);
594 reiserfs_put_page(page);
599 err = isize - sizeof(struct reiserfs_xattr_header);
601 if (xattr_hash(buffer, isize - sizeof(struct reiserfs_xattr_header)) !=
603 reiserfs_warning(inode->i_sb, "jdm-20002",
604 "Invalid hash for xattr (%s) associated "
605 "with %k", name, INODE_PKEY(inode));
617 __reiserfs_xattr_del(struct dentry *xadir, const char *name, int namelen)
619 struct dentry *dentry;
620 struct inode *dir = xadir->d_inode;
623 dentry = lookup_one_len(name, xadir, namelen);
624 if (IS_ERR(dentry)) {
625 err = PTR_ERR(dentry);
627 } else if (!dentry->d_inode) {
632 /* Skip directories.. */
633 if (S_ISDIR(dentry->d_inode->i_mode))
636 if (!is_reiserfs_priv_object(dentry->d_inode)) {
637 reiserfs_error(dir->i_sb, "jdm-20003",
638 "OID %08x [%.*s/%.*s] doesn't have "
639 "priv flag set [parent is %sset].",
640 le32_to_cpu(INODE_PKEY(dentry->d_inode)->
641 k_objectid), xadir->d_name.len,
642 xadir->d_name.name, namelen, name,
643 is_reiserfs_priv_object(xadir->d_inode) ? "" :
649 err = dir->i_op->unlink(dir, dentry);
660 int reiserfs_xattr_del(struct inode *inode, const char *name)
665 dir = open_xa_dir(inode, FL_READONLY);
671 err = __reiserfs_xattr_del(dir, name, strlen(name));
675 inode->i_ctime = CURRENT_TIME_SEC;
676 mark_inode_dirty(inode);
683 /* The following are side effects of other operations that aren't explicitly
684 * modifying extended attributes. This includes operations such as permissions
685 * or ownership changes, object deletions, etc. */
688 reiserfs_delete_xattrs_filler(void *buf, const char *name, int namelen,
689 loff_t offset, u64 ino, unsigned int d_type)
691 struct dentry *xadir = (struct dentry *)buf;
693 return __reiserfs_xattr_del(xadir, name, namelen);
697 /* This is called w/ inode->i_mutex downed */
698 int reiserfs_delete_xattrs(struct inode *inode)
700 struct dentry *dir, *root;
703 /* Skip out, an xattr has no xattrs associated with it */
704 if (is_reiserfs_priv_object(inode) ||
705 get_inode_sd_version(inode) == STAT_DATA_V1 ||
706 !reiserfs_xattrs(inode->i_sb)) {
709 reiserfs_read_lock_xattrs(inode->i_sb);
710 dir = open_xa_dir(inode, FL_READONLY);
711 reiserfs_read_unlock_xattrs(inode->i_sb);
715 } else if (!dir->d_inode) {
721 err = xattr_readdir(dir->d_inode, reiserfs_delete_xattrs_filler, dir);
727 /* Leftovers besides . and .. -- that's not good. */
728 if (dir->d_inode->i_nlink <= 2) {
729 root = get_xa_root(inode->i_sb, XATTR_REPLACE);
730 reiserfs_write_lock_xattrs(inode->i_sb);
731 err = vfs_rmdir(root->d_inode, dir);
732 reiserfs_write_unlock_xattrs(inode->i_sb);
735 reiserfs_warning(inode->i_sb, "jdm-20006",
736 "Couldn't remove all entries in directory");
745 REISERFS_I(inode)->i_flags =
746 REISERFS_I(inode)->i_flags & ~i_has_xattr_dir;
750 struct reiserfs_chown_buf {
752 struct dentry *xadir;
756 /* XXX: If there is a better way to do this, I'd love to hear about it */
758 reiserfs_chown_xattrs_filler(void *buf, const char *name, int namelen,
759 loff_t offset, u64 ino, unsigned int d_type)
761 struct reiserfs_chown_buf *chown_buf = (struct reiserfs_chown_buf *)buf;
762 struct dentry *xafile, *xadir = chown_buf->xadir;
763 struct iattr *attrs = chown_buf->attrs;
766 xafile = lookup_one_len(name, xadir, namelen);
768 return PTR_ERR(xafile);
769 else if (!xafile->d_inode) {
774 if (!S_ISDIR(xafile->d_inode->i_mode))
775 err = notify_change(xafile, attrs);
781 int reiserfs_chown_xattrs(struct inode *inode, struct iattr *attrs)
785 struct reiserfs_chown_buf buf;
786 unsigned int ia_valid = attrs->ia_valid;
788 /* Skip out, an xattr has no xattrs associated with it */
789 if (is_reiserfs_priv_object(inode) ||
790 get_inode_sd_version(inode) == STAT_DATA_V1 ||
791 !reiserfs_xattrs(inode->i_sb)) {
794 reiserfs_read_lock_xattrs(inode->i_sb);
795 dir = open_xa_dir(inode, FL_READONLY);
796 reiserfs_read_unlock_xattrs(inode->i_sb);
798 if (PTR_ERR(dir) != -ENODATA)
801 } else if (!dir->d_inode) {
808 attrs->ia_valid &= (ATTR_UID | ATTR_GID | ATTR_CTIME);
813 err = xattr_readdir(dir->d_inode, reiserfs_chown_xattrs_filler, &buf);
819 err = notify_change(dir, attrs);
826 attrs->ia_valid = ia_valid;
830 /* Actual operations that are exported to VFS-land */
833 * Inode operation getxattr()
834 * Preliminary locking: we down dentry->d_inode->i_mutex
837 reiserfs_getxattr(struct dentry * dentry, const char *name, void *buffer,
840 struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
843 if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
844 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
847 reiserfs_read_lock_xattr_i(dentry->d_inode);
848 reiserfs_read_lock_xattrs(dentry->d_sb);
849 err = xah->get(dentry->d_inode, name, buffer, size);
850 reiserfs_read_unlock_xattrs(dentry->d_sb);
851 reiserfs_read_unlock_xattr_i(dentry->d_inode);
856 * Inode operation setxattr()
858 * dentry->d_inode->i_mutex down
861 reiserfs_setxattr(struct dentry *dentry, const char *name, const void *value,
862 size_t size, int flags)
864 struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
868 if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
869 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
872 reiserfs_write_lock_xattr_i(dentry->d_inode);
873 lock = !has_xattr_dir(dentry->d_inode);
875 reiserfs_write_lock_xattrs(dentry->d_sb);
877 reiserfs_read_lock_xattrs(dentry->d_sb);
878 err = xah->set(dentry->d_inode, name, value, size, flags);
880 reiserfs_write_unlock_xattrs(dentry->d_sb);
882 reiserfs_read_unlock_xattrs(dentry->d_sb);
883 reiserfs_write_unlock_xattr_i(dentry->d_inode);
888 * Inode operation removexattr()
890 * dentry->d_inode->i_mutex down
892 int reiserfs_removexattr(struct dentry *dentry, const char *name)
895 struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
897 if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
898 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
901 reiserfs_write_lock_xattr_i(dentry->d_inode);
902 reiserfs_read_lock_xattrs(dentry->d_sb);
904 /* Deletion pre-operation */
906 err = xah->del(dentry->d_inode, name);
911 err = reiserfs_xattr_del(dentry->d_inode, name);
913 dentry->d_inode->i_ctime = CURRENT_TIME_SEC;
914 mark_inode_dirty(dentry->d_inode);
917 reiserfs_read_unlock_xattrs(dentry->d_sb);
918 reiserfs_write_unlock_xattr_i(dentry->d_inode);
922 /* This is what filldir will use:
923 * r_pos will always contain the amount of space required for the entire
924 * list. If r_pos becomes larger than r_size, we need more space and we
925 * return an error indicating this. If r_pos is less than r_size, then we've
926 * filled the buffer successfully and we return success */
927 struct reiserfs_listxattr_buf {
931 struct inode *r_inode;
935 reiserfs_listxattr_filler(void *buf, const char *name, int namelen,
936 loff_t offset, u64 ino, unsigned int d_type)
938 struct reiserfs_listxattr_buf *b = (struct reiserfs_listxattr_buf *)buf;
941 || (namelen != 1 && (name[1] != '.' || namelen != 2))) {
942 struct reiserfs_xattr_handler *xah =
943 find_xattr_handler_prefix(name);
945 return 0; /* Unsupported xattr name, skip it */
947 /* We call ->list() twice because the operation isn't required to just
948 * return the name back - we want to make sure we have enough space */
949 len += xah->list(b->r_inode, name, namelen, NULL);
952 if (b->r_pos + len + 1 <= b->r_size) {
953 char *p = b->r_buf + b->r_pos;
954 p += xah->list(b->r_inode, name, namelen, p);
965 * Inode operation listxattr()
967 * Preliminary locking: we down dentry->d_inode->i_mutex
969 ssize_t reiserfs_listxattr(struct dentry * dentry, char *buffer, size_t size)
973 struct reiserfs_listxattr_buf buf;
975 if (!dentry->d_inode)
978 if (!reiserfs_xattrs(dentry->d_sb) ||
979 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
982 reiserfs_read_lock_xattr_i(dentry->d_inode);
983 reiserfs_read_lock_xattrs(dentry->d_sb);
984 dir = open_xa_dir(dentry->d_inode, FL_READONLY);
985 reiserfs_read_unlock_xattrs(dentry->d_sb);
989 err = 0; /* Not an error if there aren't any xattrs */
994 buf.r_size = buffer ? size : 0;
996 buf.r_inode = dentry->d_inode;
998 REISERFS_I(dentry->d_inode)->i_flags |= i_has_xattr_dir;
1000 err = xattr_readdir(dir->d_inode, reiserfs_listxattr_filler, &buf);
1004 if (buf.r_pos > buf.r_size && buffer != NULL)
1013 reiserfs_read_unlock_xattr_i(dentry->d_inode);
1017 /* This is the implementation for the xattr plugin infrastructure */
1018 static LIST_HEAD(xattr_handlers);
1019 static DEFINE_RWLOCK(handler_lock);
1021 static struct reiserfs_xattr_handler *find_xattr_handler_prefix(const char
1024 struct reiserfs_xattr_handler *xah = NULL;
1025 struct list_head *p;
1027 read_lock(&handler_lock);
1028 list_for_each(p, &xattr_handlers) {
1029 xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
1030 if (strncmp(xah->prefix, prefix, strlen(xah->prefix)) == 0)
1035 read_unlock(&handler_lock);
1039 static void __unregister_handlers(void)
1041 struct reiserfs_xattr_handler *xah;
1042 struct list_head *p, *tmp;
1044 list_for_each_safe(p, tmp, &xattr_handlers) {
1045 xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
1051 INIT_LIST_HEAD(&xattr_handlers);
1054 int __init reiserfs_xattr_register_handlers(void)
1057 struct reiserfs_xattr_handler *xah;
1058 struct list_head *p;
1060 write_lock(&handler_lock);
1062 /* If we're already initialized, nothing to do */
1063 if (!list_empty(&xattr_handlers)) {
1064 write_unlock(&handler_lock);
1068 /* Add the handlers */
1069 list_add_tail(&user_handler.handlers, &xattr_handlers);
1070 list_add_tail(&trusted_handler.handlers, &xattr_handlers);
1071 #ifdef CONFIG_REISERFS_FS_SECURITY
1072 list_add_tail(&security_handler.handlers, &xattr_handlers);
1074 #ifdef CONFIG_REISERFS_FS_POSIX_ACL
1075 list_add_tail(&posix_acl_access_handler.handlers, &xattr_handlers);
1076 list_add_tail(&posix_acl_default_handler.handlers, &xattr_handlers);
1079 /* Run initializers, if available */
1080 list_for_each(p, &xattr_handlers) {
1081 xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
1091 /* Clean up other handlers, if any failed */
1093 __unregister_handlers();
1095 write_unlock(&handler_lock);
1099 void reiserfs_xattr_unregister_handlers(void)
1101 write_lock(&handler_lock);
1102 __unregister_handlers();
1103 write_unlock(&handler_lock);
1106 /* This will catch lookups from the fs root to .reiserfs_priv */
1108 xattr_lookup_poison(struct dentry *dentry, struct qstr *q1, struct qstr *name)
1110 struct dentry *priv_root = REISERFS_SB(dentry->d_sb)->priv_root;
1111 if (name->len == priv_root->d_name.len &&
1112 name->hash == priv_root->d_name.hash &&
1113 !memcmp(name->name, priv_root->d_name.name, name->len)) {
1115 } else if (q1->len == name->len &&
1116 !memcmp(q1->name, name->name, name->len))
1121 static struct dentry_operations xattr_lookup_poison_ops = {
1122 .d_compare = xattr_lookup_poison,
1125 /* We need to take a copy of the mount flags since things like
1126 * MS_RDONLY don't get set until *after* we're called.
1127 * mount_flags != mount_options */
1128 int reiserfs_xattr_init(struct super_block *s, int mount_flags)
1132 /* We need generation numbers to ensure that the oid mapping is correct
1133 * v3.5 filesystems don't have them. */
1134 if (!old_format_only(s)) {
1135 set_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
1136 } else if (reiserfs_xattrs_optional(s)) {
1137 /* Old format filesystem, but optional xattrs have been enabled
1138 * at mount time. Error out. */
1139 reiserfs_warning(s, "jdm-20005",
1140 "xattrs/ACLs not supported on pre v3.6 "
1141 "format filesystem. Failing mount.");
1145 /* Old format filesystem, but no optional xattrs have been enabled. This
1146 * means we silently disable xattrs on the filesystem. */
1147 clear_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
1150 /* If we don't have the privroot located yet - go find it */
1151 if (reiserfs_xattrs(s) && !REISERFS_SB(s)->priv_root) {
1152 struct dentry *dentry;
1153 dentry = lookup_one_len(PRIVROOT_NAME, s->s_root,
1154 strlen(PRIVROOT_NAME));
1155 if (!IS_ERR(dentry)) {
1156 if (!(mount_flags & MS_RDONLY) && !dentry->d_inode) {
1157 struct inode *inode = dentry->d_parent->d_inode;
1158 mutex_lock_nested(&inode->i_mutex,
1160 err = inode->i_op->mkdir(inode, dentry, 0700);
1161 mutex_unlock(&inode->i_mutex);
1167 if (dentry && dentry->d_inode)
1168 reiserfs_info(s, "Created %s - "
1169 "reserved for xattr "
1172 } else if (!dentry->d_inode) {
1177 err = PTR_ERR(dentry);
1179 if (!err && dentry) {
1180 s->s_root->d_op = &xattr_lookup_poison_ops;
1181 reiserfs_mark_inode_private(dentry->d_inode);
1182 REISERFS_SB(s)->priv_root = dentry;
1183 } else if (!(mount_flags & MS_RDONLY)) { /* xattrs are unavailable */
1184 /* If we're read-only it just means that the dir hasn't been
1185 * created. Not an error -- just no xattrs on the fs. We'll
1186 * check again if we go read-write */
1187 reiserfs_warning(s, "jdm-20006",
1188 "xattrs/ACLs enabled and couldn't "
1189 "find/create .reiserfs_priv. "
1196 /* This is only nonzero if there was an error initializing the xattr
1197 * directory or if there is a condition where we don't support them. */
1199 clear_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
1200 clear_bit(REISERFS_XATTRS_USER, &(REISERFS_SB(s)->s_mount_opt));
1201 clear_bit(REISERFS_POSIXACL, &(REISERFS_SB(s)->s_mount_opt));
1204 /* The super_block MS_POSIXACL must mirror the (no)acl mount option. */
1205 s->s_flags = s->s_flags & ~MS_POSIXACL;
1206 if (reiserfs_posixacl(s))
1207 s->s_flags |= MS_POSIXACL;
1212 static int reiserfs_check_acl(struct inode *inode, int mask)
1214 struct posix_acl *acl;
1215 int error = -EAGAIN; /* do regular unix permission checks by default */
1217 reiserfs_read_lock_xattr_i(inode);
1218 reiserfs_read_lock_xattrs(inode->i_sb);
1220 acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS);
1222 reiserfs_read_unlock_xattrs(inode->i_sb);
1223 reiserfs_read_unlock_xattr_i(inode);
1227 error = posix_acl_permission(inode, acl, mask);
1228 posix_acl_release(acl);
1229 } else if (PTR_ERR(acl) != -ENODATA)
1230 error = PTR_ERR(acl);
1236 int reiserfs_permission(struct inode *inode, int mask)
1239 * We don't do permission checks on the internal objects.
1240 * Permissions are determined by the "owning" object.
1242 if (is_reiserfs_priv_object(inode))
1246 * Stat data v1 doesn't support ACLs.
1248 if (get_inode_sd_version(inode) == STAT_DATA_V1)
1249 return generic_permission(inode, mask, NULL);
1251 return generic_permission(inode, mask, reiserfs_check_acl);