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/mount.h>
39 #include <linux/file.h>
40 #include <linux/pagemap.h>
41 #include <linux/xattr.h>
42 #include <linux/reiserfs_xattr.h>
43 #include <linux/reiserfs_acl.h>
44 #include <asm/uaccess.h>
45 #include <net/checksum.h>
46 #include <linux/smp_lock.h>
47 #include <linux/stat.h>
48 #include <asm/semaphore.h>
50 #define FL_READONLY 128
51 #define FL_DIR_SEM_HELD 256
52 #define PRIVROOT_NAME ".reiserfs_priv"
53 #define XAROOT_NAME "xattrs"
55 static struct reiserfs_xattr_handler *find_xattr_handler_prefix(const char
58 /* Returns the dentry referring to the root of the extended attribute
59 * directory tree. If it has already been retrieved, it is used. If it
60 * hasn't been created and the flags indicate creation is allowed, we
61 * attempt to create it. On error, we return a pointer-encoded error.
63 static struct dentry *get_xa_root(struct super_block *sb, int flags)
65 struct dentry *privroot = dget(REISERFS_SB(sb)->priv_root);
66 struct dentry *xaroot;
68 /* This needs to be created at mount-time */
70 return ERR_PTR(-ENODATA);
72 mutex_lock(&privroot->d_inode->i_mutex);
73 if (REISERFS_SB(sb)->xattr_root) {
74 xaroot = dget(REISERFS_SB(sb)->xattr_root);
78 xaroot = lookup_one_len(XAROOT_NAME, privroot, strlen(XAROOT_NAME));
81 } else if (!xaroot->d_inode) {
83 if (flags == 0 || flags & XATTR_CREATE)
84 err = privroot->d_inode->i_op->mkdir(privroot->d_inode,
88 xaroot = ERR_PTR(err);
92 REISERFS_SB(sb)->xattr_root = dget(xaroot);
95 mutex_unlock(&privroot->d_inode->i_mutex);
100 /* Opens the directory corresponding to the inode's extended attribute store.
101 * If flags allow, the tree to the directory may be created. If creation is
102 * prohibited, -ENODATA is returned. */
103 static struct dentry *open_xa_dir(const struct inode *inode, int flags)
105 struct dentry *xaroot, *xadir;
108 xaroot = get_xa_root(inode->i_sb, flags);
112 /* ok, we have xaroot open */
113 snprintf(namebuf, sizeof(namebuf), "%X.%X",
114 le32_to_cpu(INODE_PKEY(inode)->k_objectid),
115 inode->i_generation);
116 xadir = lookup_one_len(namebuf, xaroot, strlen(namebuf));
122 if (!xadir->d_inode) {
124 if (flags == 0 || flags & XATTR_CREATE) {
125 /* Although there is nothing else trying to create this directory,
126 * another directory with the same hash may be created, so we need
127 * to protect against that */
129 xaroot->d_inode->i_op->mkdir(xaroot->d_inode, xadir,
137 if (!xadir->d_inode) {
140 return ERR_PTR(-ENODATA);
148 /* Returns a dentry corresponding to a specific extended attribute file
149 * for the inode. If flags allow, the file is created. Otherwise, a
150 * valid or negative dentry, or an error is returned. */
151 static struct dentry *get_xa_file_dentry(const struct inode *inode,
152 const char *name, int flags)
154 struct dentry *xadir, *xafile;
157 xadir = open_xa_dir(inode, flags);
159 return ERR_PTR(PTR_ERR(xadir));
160 } else if (xadir && !xadir->d_inode) {
162 return ERR_PTR(-ENODATA);
165 xafile = lookup_one_len(name, xadir, strlen(name));
166 if (IS_ERR(xafile)) {
168 return ERR_PTR(PTR_ERR(xafile));
171 if (xafile->d_inode) { /* file exists */
172 if (flags & XATTR_CREATE) {
177 } else if (flags & XATTR_REPLACE || flags & FL_READONLY) {
180 /* inode->i_mutex is down, so nothing else can try to create
182 err = xadir->d_inode->i_op->create(xadir->d_inode, xafile,
183 0700 | S_IFREG, NULL);
194 xafile = ERR_PTR(err);
198 /* Opens a file pointer to the attribute associated with inode */
199 static struct file *open_xa_file(const struct inode *inode, const char *name,
202 struct dentry *xafile;
205 xafile = get_xa_file_dentry(inode, name, flags);
207 return ERR_PTR(PTR_ERR(xafile));
208 else if (!xafile->d_inode) {
210 return ERR_PTR(-ENODATA);
213 fp = dentry_open(xafile, NULL, O_RDWR);
214 /* dentry_open dputs the dentry if it fails */
220 * this is very similar to fs/reiserfs/dir.c:reiserfs_readdir, but
221 * we need to drop the path before calling the filldir struct. That
222 * would be a big performance hit to the non-xattr case, so I've copied
223 * the whole thing for now. --clm
225 * the big difference is that I go backwards through the directory,
226 * and don't mess with f->f_pos, but the idea is the same. Do some
227 * action on each and every entry in the directory.
229 * we're called with i_mutex held, so there are no worries about the directory
230 * changing underneath us.
232 static int __xattr_readdir(struct file *filp, void *dirent, filldir_t filldir)
234 struct inode *inode = filp->f_path.dentry->d_inode;
235 struct cpu_key pos_key; /* key of current position in the directory (key of directory entry) */
236 INITIALIZE_PATH(path_to_entry);
237 struct buffer_head *bh;
239 struct item_head *ih, tmp_ih;
243 char small_buf[32]; /* avoid kmalloc if we can */
244 struct reiserfs_de_head *deh;
249 struct reiserfs_dir_entry de;
251 /* form key for search the next directory entry using f_pos field of
253 next_pos = max_reiserfs_offset(inode);
257 if (next_pos <= DOT_DOT_OFFSET)
259 make_cpu_key(&pos_key, inode, next_pos, TYPE_DIRENTRY, 3);
262 search_by_entry_key(inode->i_sb, &pos_key, &path_to_entry,
264 if (search_res == IO_ERROR) {
265 // FIXME: we could just skip part of directory which could
267 pathrelse(&path_to_entry);
271 if (search_res == NAME_NOT_FOUND)
274 set_de_name_and_namelen(&de);
275 entry_num = de.de_entry_num;
276 deh = &(de.de_deh[entry_num]);
281 if (!is_direntry_le_ih(ih)) {
282 reiserfs_warning(inode->i_sb, "not direntry %h", ih);
285 copy_item_head(&tmp_ih, ih);
287 /* we must have found item, that is item of this directory, */
288 RFALSE(COMP_SHORT_KEYS(&(ih->ih_key), &pos_key),
289 "vs-9000: found item %h does not match to dir we readdir %K",
292 if (deh_offset(deh) <= DOT_DOT_OFFSET) {
296 /* look for the previous entry in the directory */
297 next_pos = deh_offset(deh) - 1;
299 if (!de_visible(deh))
300 /* it is hidden entry */
303 d_reclen = entry_length(bh, ih, entry_num);
304 d_name = B_I_DEH_ENTRY_FILE_NAME(bh, ih, deh);
305 d_off = deh_offset(deh);
306 d_ino = deh_objectid(deh);
308 if (!d_name[d_reclen - 1])
309 d_reclen = strlen(d_name);
311 if (d_reclen > REISERFS_MAX_NAME(inode->i_sb->s_blocksize)) {
312 /* too big to send back to VFS */
316 /* Ignore the .reiserfs_priv entry */
317 if (reiserfs_xattrs(inode->i_sb) &&
318 !old_format_only(inode->i_sb) &&
320 le32_to_cpu(INODE_PKEY
321 (REISERFS_SB(inode->i_sb)->priv_root->d_inode)->
325 if (d_reclen <= 32) {
326 local_buf = small_buf;
328 local_buf = kmalloc(d_reclen, GFP_NOFS);
330 pathrelse(&path_to_entry);
333 if (item_moved(&tmp_ih, &path_to_entry)) {
336 /* sigh, must retry. Do this same offset again */
342 // Note, that we copy name to user space via temporary
343 // buffer (local_buf) because filldir will block if
344 // user space buffer is swapped out. At that time
345 // entry can move to somewhere else
346 memcpy(local_buf, d_name, d_reclen);
348 /* the filldir function might need to start transactions,
349 * or do who knows what. Release the path now that we've
350 * copied all the important stuff out of the deh
352 pathrelse(&path_to_entry);
354 if (filldir(dirent, local_buf, d_reclen, d_off, d_ino,
356 if (local_buf != small_buf) {
361 if (local_buf != small_buf) {
367 pathrelse(&path_to_entry);
372 * this could be done with dedicated readdir ops for the xattr files,
373 * but I want to get something working asap
374 * this is stolen from vfs_readdir
378 int xattr_readdir(struct file *file, filldir_t filler, void *buf)
380 struct inode *inode = file->f_path.dentry->d_inode;
382 if (!file->f_op || !file->f_op->readdir)
384 mutex_lock_nested(&inode->i_mutex, I_MUTEX_XATTR);
385 // down(&inode->i_zombie);
387 if (!IS_DEADDIR(inode)) {
389 res = __xattr_readdir(file, buf, filler);
392 // up(&inode->i_zombie);
393 mutex_unlock(&inode->i_mutex);
398 /* Internal operations on file data */
399 static inline void reiserfs_put_page(struct page *page)
402 page_cache_release(page);
405 static struct page *reiserfs_get_page(struct inode *dir, unsigned long n)
407 struct address_space *mapping = dir->i_mapping;
409 /* We can deadlock if we try to free dentries,
410 and an unlink/rmdir has just occured - GFP_NOFS avoids this */
411 mapping_set_gfp_mask(mapping, GFP_NOFS);
412 page = read_mapping_page(mapping, n, NULL);
414 wait_on_page_locked(page);
416 if (!PageUptodate(page))
425 reiserfs_put_page(page);
426 return ERR_PTR(-EIO);
429 static inline __u32 xattr_hash(const char *msg, int len)
431 return csum_partial(msg, len, 0);
434 /* Generic extended attribute operations that can be used by xa plugins */
437 * inode->i_mutex: down
440 reiserfs_xattr_set(struct inode *inode, const char *name, const void *buffer,
441 size_t buffer_size, int flags)
447 struct address_space *mapping;
449 size_t buffer_pos = 0;
450 struct inode *xinode;
451 struct iattr newattrs;
454 if (get_inode_sd_version(inode) == STAT_DATA_V1)
457 /* Empty xattrs are ok, they're just empty files, no hash */
458 if (buffer && buffer_size)
459 xahash = xattr_hash(buffer, buffer_size);
462 fp = open_xa_file(inode, name, flags);
468 xinode = fp->f_path.dentry->d_inode;
469 REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
471 /* we need to copy it off.. */
472 if (xinode->i_nlink > 1) {
474 err = reiserfs_xattr_del(inode, name);
477 /* We just killed the old one, we're not replacing anymore */
478 if (flags & XATTR_REPLACE)
479 flags &= ~XATTR_REPLACE;
483 /* Resize it so we're ok to write there */
484 newattrs.ia_size = buffer_size;
485 newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
486 mutex_lock(&xinode->i_mutex);
487 err = notify_change(fp->f_path.dentry, &newattrs);
491 mapping = xinode->i_mapping;
492 while (buffer_pos < buffer_size || buffer_pos == 0) {
495 size_t page_offset = (file_pos & (PAGE_CACHE_SIZE - 1));
496 if (buffer_size - buffer_pos > PAGE_CACHE_SIZE)
497 chunk = PAGE_CACHE_SIZE;
499 chunk = buffer_size - buffer_pos;
501 page = reiserfs_get_page(xinode, file_pos >> PAGE_CACHE_SHIFT);
508 data = page_address(page);
511 struct reiserfs_xattr_header *rxh;
512 skip = file_pos = sizeof(struct reiserfs_xattr_header);
513 if (chunk + skip > PAGE_CACHE_SIZE)
514 chunk = PAGE_CACHE_SIZE - skip;
515 rxh = (struct reiserfs_xattr_header *)data;
516 rxh->h_magic = cpu_to_le32(REISERFS_XATTR_MAGIC);
517 rxh->h_hash = cpu_to_le32(xahash);
520 err = mapping->a_ops->prepare_write(fp, page, page_offset,
521 page_offset + chunk + skip);
524 memcpy(data + skip, buffer + buffer_pos, chunk);
526 mapping->a_ops->commit_write(fp, page, page_offset,
527 page_offset + chunk +
531 reiserfs_put_page(page);
535 if (err || buffer_size == 0 || !buffer)
539 /* We can't mark the inode dirty if it's not hashed. This is the case
540 * when we're inheriting the default ACL. If we dirty it, the inode
541 * gets marked dirty, but won't (ever) make it onto the dirty list until
542 * it's synced explicitly to clear I_DIRTY. This is bad. */
543 if (!hlist_unhashed(&inode->i_hash)) {
544 inode->i_ctime = CURRENT_TIME_SEC;
545 mark_inode_dirty(inode);
549 mutex_unlock(&xinode->i_mutex);
557 * inode->i_mutex: down
560 reiserfs_xattr_get(const struct inode *inode, const char *name, void *buffer,
567 size_t buffer_pos = 0;
569 struct inode *xinode;
575 /* We can't have xattrs attached to v1 items since they don't have
576 * generation numbers */
577 if (get_inode_sd_version(inode) == STAT_DATA_V1)
580 fp = open_xa_file(inode, name, FL_READONLY);
586 xinode = fp->f_path.dentry->d_inode;
587 isize = xinode->i_size;
588 REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
590 /* Just return the size needed */
591 if (buffer == NULL) {
592 err = isize - sizeof(struct reiserfs_xattr_header);
596 if (buffer_size < isize - sizeof(struct reiserfs_xattr_header)) {
601 while (file_pos < isize) {
605 if (isize - file_pos > PAGE_CACHE_SIZE)
606 chunk = PAGE_CACHE_SIZE;
608 chunk = isize - file_pos;
610 page = reiserfs_get_page(xinode, file_pos >> PAGE_CACHE_SHIFT);
617 data = page_address(page);
619 struct reiserfs_xattr_header *rxh =
620 (struct reiserfs_xattr_header *)data;
621 skip = file_pos = sizeof(struct reiserfs_xattr_header);
623 /* Magic doesn't match up.. */
624 if (rxh->h_magic != cpu_to_le32(REISERFS_XATTR_MAGIC)) {
626 reiserfs_put_page(page);
627 reiserfs_warning(inode->i_sb,
628 "Invalid magic for xattr (%s) "
629 "associated with %k", name,
634 hash = le32_to_cpu(rxh->h_hash);
636 memcpy(buffer + buffer_pos, data + skip, chunk);
638 reiserfs_put_page(page);
643 err = isize - sizeof(struct reiserfs_xattr_header);
645 if (xattr_hash(buffer, isize - sizeof(struct reiserfs_xattr_header)) !=
647 reiserfs_warning(inode->i_sb,
648 "Invalid hash for xattr (%s) associated "
649 "with %k", name, INODE_PKEY(inode));
661 __reiserfs_xattr_del(struct dentry *xadir, const char *name, int namelen)
663 struct dentry *dentry;
664 struct inode *dir = xadir->d_inode;
667 dentry = lookup_one_len(name, xadir, namelen);
668 if (IS_ERR(dentry)) {
669 err = PTR_ERR(dentry);
671 } else if (!dentry->d_inode) {
676 /* Skip directories.. */
677 if (S_ISDIR(dentry->d_inode->i_mode))
680 if (!is_reiserfs_priv_object(dentry->d_inode)) {
681 reiserfs_warning(dir->i_sb, "OID %08x [%.*s/%.*s] doesn't have "
682 "priv flag set [parent is %sset].",
683 le32_to_cpu(INODE_PKEY(dentry->d_inode)->
684 k_objectid), xadir->d_name.len,
685 xadir->d_name.name, namelen, name,
686 is_reiserfs_priv_object(xadir->
693 err = dir->i_op->unlink(dir, dentry);
704 int reiserfs_xattr_del(struct inode *inode, const char *name)
709 dir = open_xa_dir(inode, FL_READONLY);
715 err = __reiserfs_xattr_del(dir, name, strlen(name));
719 inode->i_ctime = CURRENT_TIME_SEC;
720 mark_inode_dirty(inode);
727 /* The following are side effects of other operations that aren't explicitly
728 * modifying extended attributes. This includes operations such as permissions
729 * or ownership changes, object deletions, etc. */
732 reiserfs_delete_xattrs_filler(void *buf, const char *name, int namelen,
733 loff_t offset, u64 ino, unsigned int d_type)
735 struct dentry *xadir = (struct dentry *)buf;
737 return __reiserfs_xattr_del(xadir, name, namelen);
741 /* This is called w/ inode->i_mutex downed */
742 int reiserfs_delete_xattrs(struct inode *inode)
745 struct dentry *dir, *root;
748 /* Skip out, an xattr has no xattrs associated with it */
749 if (is_reiserfs_priv_object(inode) ||
750 get_inode_sd_version(inode) == STAT_DATA_V1 ||
751 !reiserfs_xattrs(inode->i_sb)) {
754 reiserfs_read_lock_xattrs(inode->i_sb);
755 dir = open_xa_dir(inode, FL_READONLY);
756 reiserfs_read_unlock_xattrs(inode->i_sb);
760 } else if (!dir->d_inode) {
765 fp = dentry_open(dir, NULL, O_RDWR);
768 /* dentry_open dputs the dentry if it fails */
773 err = xattr_readdir(fp, reiserfs_delete_xattrs_filler, dir);
779 /* Leftovers besides . and .. -- that's not good. */
780 if (dir->d_inode->i_nlink <= 2) {
781 root = get_xa_root(inode->i_sb, XATTR_REPLACE);
782 reiserfs_write_lock_xattrs(inode->i_sb);
783 err = vfs_rmdir(root->d_inode, dir, NULL);
784 reiserfs_write_unlock_xattrs(inode->i_sb);
787 reiserfs_warning(inode->i_sb,
788 "Couldn't remove all entries in directory");
797 REISERFS_I(inode)->i_flags =
798 REISERFS_I(inode)->i_flags & ~i_has_xattr_dir;
802 struct reiserfs_chown_buf {
804 struct dentry *xadir;
808 /* XXX: If there is a better way to do this, I'd love to hear about it */
810 reiserfs_chown_xattrs_filler(void *buf, const char *name, int namelen,
811 loff_t offset, u64 ino, unsigned int d_type)
813 struct reiserfs_chown_buf *chown_buf = (struct reiserfs_chown_buf *)buf;
814 struct dentry *xafile, *xadir = chown_buf->xadir;
815 struct iattr *attrs = chown_buf->attrs;
818 xafile = lookup_one_len(name, xadir, namelen);
820 return PTR_ERR(xafile);
821 else if (!xafile->d_inode) {
826 if (!S_ISDIR(xafile->d_inode->i_mode))
827 err = notify_change(xafile, attrs);
833 int reiserfs_chown_xattrs(struct inode *inode, struct iattr *attrs)
838 struct reiserfs_chown_buf buf;
839 unsigned int ia_valid = attrs->ia_valid;
841 /* Skip out, an xattr has no xattrs associated with it */
842 if (is_reiserfs_priv_object(inode) ||
843 get_inode_sd_version(inode) == STAT_DATA_V1 ||
844 !reiserfs_xattrs(inode->i_sb)) {
847 reiserfs_read_lock_xattrs(inode->i_sb);
848 dir = open_xa_dir(inode, FL_READONLY);
849 reiserfs_read_unlock_xattrs(inode->i_sb);
851 if (PTR_ERR(dir) != -ENODATA)
854 } else if (!dir->d_inode) {
859 fp = dentry_open(dir, NULL, O_RDWR);
862 /* dentry_open dputs the dentry if it fails */
868 attrs->ia_valid &= (ATTR_UID | ATTR_GID | ATTR_CTIME);
873 err = xattr_readdir(fp, reiserfs_chown_xattrs_filler, &buf);
879 err = notify_change(dir, attrs);
886 attrs->ia_valid = ia_valid;
890 /* Actual operations that are exported to VFS-land */
893 * Inode operation getxattr()
894 * Preliminary locking: we down dentry->d_inode->i_mutex
897 reiserfs_getxattr(struct dentry * dentry, const char *name, void *buffer,
900 struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
903 if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
904 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
907 reiserfs_read_lock_xattr_i(dentry->d_inode);
908 reiserfs_read_lock_xattrs(dentry->d_sb);
909 err = xah->get(dentry->d_inode, name, buffer, size);
910 reiserfs_read_unlock_xattrs(dentry->d_sb);
911 reiserfs_read_unlock_xattr_i(dentry->d_inode);
916 * Inode operation setxattr()
918 * dentry->d_inode->i_mutex down
921 reiserfs_setxattr(struct dentry *dentry, const char *name, const void *value,
922 size_t size, int flags)
924 struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
928 if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
929 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
932 reiserfs_write_lock_xattr_i(dentry->d_inode);
933 lock = !has_xattr_dir(dentry->d_inode);
935 reiserfs_write_lock_xattrs(dentry->d_sb);
937 reiserfs_read_lock_xattrs(dentry->d_sb);
938 err = xah->set(dentry->d_inode, name, value, size, flags);
940 reiserfs_write_unlock_xattrs(dentry->d_sb);
942 reiserfs_read_unlock_xattrs(dentry->d_sb);
943 reiserfs_write_unlock_xattr_i(dentry->d_inode);
948 * Inode operation removexattr()
950 * dentry->d_inode->i_mutex down
952 int reiserfs_removexattr(struct dentry *dentry, const char *name)
955 struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
957 if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
958 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
961 reiserfs_write_lock_xattr_i(dentry->d_inode);
962 reiserfs_read_lock_xattrs(dentry->d_sb);
964 /* Deletion pre-operation */
966 err = xah->del(dentry->d_inode, name);
971 err = reiserfs_xattr_del(dentry->d_inode, name);
973 dentry->d_inode->i_ctime = CURRENT_TIME_SEC;
974 mark_inode_dirty(dentry->d_inode);
977 reiserfs_read_unlock_xattrs(dentry->d_sb);
978 reiserfs_write_unlock_xattr_i(dentry->d_inode);
982 /* This is what filldir will use:
983 * r_pos will always contain the amount of space required for the entire
984 * list. If r_pos becomes larger than r_size, we need more space and we
985 * return an error indicating this. If r_pos is less than r_size, then we've
986 * filled the buffer successfully and we return success */
987 struct reiserfs_listxattr_buf {
991 struct inode *r_inode;
995 reiserfs_listxattr_filler(void *buf, const char *name, int namelen,
996 loff_t offset, u64 ino, unsigned int d_type)
998 struct reiserfs_listxattr_buf *b = (struct reiserfs_listxattr_buf *)buf;
1001 || (namelen != 1 && (name[1] != '.' || namelen != 2))) {
1002 struct reiserfs_xattr_handler *xah =
1003 find_xattr_handler_prefix(name);
1005 return 0; /* Unsupported xattr name, skip it */
1007 /* We call ->list() twice because the operation isn't required to just
1008 * return the name back - we want to make sure we have enough space */
1009 len += xah->list(b->r_inode, name, namelen, NULL);
1012 if (b->r_pos + len + 1 <= b->r_size) {
1013 char *p = b->r_buf + b->r_pos;
1014 p += xah->list(b->r_inode, name, namelen, p);
1017 b->r_pos += len + 1;
1025 * Inode operation listxattr()
1027 * Preliminary locking: we down dentry->d_inode->i_mutex
1029 ssize_t reiserfs_listxattr(struct dentry * dentry, char *buffer, size_t size)
1034 struct reiserfs_listxattr_buf buf;
1036 if (!dentry->d_inode)
1039 if (!reiserfs_xattrs(dentry->d_sb) ||
1040 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
1043 reiserfs_read_lock_xattr_i(dentry->d_inode);
1044 reiserfs_read_lock_xattrs(dentry->d_sb);
1045 dir = open_xa_dir(dentry->d_inode, FL_READONLY);
1046 reiserfs_read_unlock_xattrs(dentry->d_sb);
1049 if (err == -ENODATA)
1050 err = 0; /* Not an error if there aren't any xattrs */
1054 fp = dentry_open(dir, NULL, O_RDWR);
1057 /* dentry_open dputs the dentry if it fails */
1062 buf.r_size = buffer ? size : 0;
1064 buf.r_inode = dentry->d_inode;
1066 REISERFS_I(dentry->d_inode)->i_flags |= i_has_xattr_dir;
1068 err = xattr_readdir(fp, reiserfs_listxattr_filler, &buf);
1072 if (buf.r_pos > buf.r_size && buffer != NULL)
1081 reiserfs_read_unlock_xattr_i(dentry->d_inode);
1085 /* This is the implementation for the xattr plugin infrastructure */
1086 static struct list_head xattr_handlers = LIST_HEAD_INIT(xattr_handlers);
1087 static DEFINE_RWLOCK(handler_lock);
1089 static struct reiserfs_xattr_handler *find_xattr_handler_prefix(const char
1092 struct reiserfs_xattr_handler *xah = NULL;
1093 struct list_head *p;
1095 read_lock(&handler_lock);
1096 list_for_each(p, &xattr_handlers) {
1097 xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
1098 if (strncmp(xah->prefix, prefix, strlen(xah->prefix)) == 0)
1103 read_unlock(&handler_lock);
1107 static void __unregister_handlers(void)
1109 struct reiserfs_xattr_handler *xah;
1110 struct list_head *p, *tmp;
1112 list_for_each_safe(p, tmp, &xattr_handlers) {
1113 xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
1119 INIT_LIST_HEAD(&xattr_handlers);
1122 int __init reiserfs_xattr_register_handlers(void)
1125 struct reiserfs_xattr_handler *xah;
1126 struct list_head *p;
1128 write_lock(&handler_lock);
1130 /* If we're already initialized, nothing to do */
1131 if (!list_empty(&xattr_handlers)) {
1132 write_unlock(&handler_lock);
1136 /* Add the handlers */
1137 list_add_tail(&user_handler.handlers, &xattr_handlers);
1138 list_add_tail(&trusted_handler.handlers, &xattr_handlers);
1139 #ifdef CONFIG_REISERFS_FS_SECURITY
1140 list_add_tail(&security_handler.handlers, &xattr_handlers);
1142 #ifdef CONFIG_REISERFS_FS_POSIX_ACL
1143 list_add_tail(&posix_acl_access_handler.handlers, &xattr_handlers);
1144 list_add_tail(&posix_acl_default_handler.handlers, &xattr_handlers);
1147 /* Run initializers, if available */
1148 list_for_each(p, &xattr_handlers) {
1149 xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
1159 /* Clean up other handlers, if any failed */
1161 __unregister_handlers();
1163 write_unlock(&handler_lock);
1167 void reiserfs_xattr_unregister_handlers(void)
1169 write_lock(&handler_lock);
1170 __unregister_handlers();
1171 write_unlock(&handler_lock);
1174 /* This will catch lookups from the fs root to .reiserfs_priv */
1176 xattr_lookup_poison(struct dentry *dentry, struct qstr *q1, struct qstr *name)
1178 struct dentry *priv_root = REISERFS_SB(dentry->d_sb)->priv_root;
1179 if (name->len == priv_root->d_name.len &&
1180 name->hash == priv_root->d_name.hash &&
1181 !memcmp(name->name, priv_root->d_name.name, name->len)) {
1183 } else if (q1->len == name->len &&
1184 !memcmp(q1->name, name->name, name->len))
1189 static struct dentry_operations xattr_lookup_poison_ops = {
1190 .d_compare = xattr_lookup_poison,
1193 /* We need to take a copy of the mount flags since things like
1194 * MS_RDONLY don't get set until *after* we're called.
1195 * mount_flags != mount_options */
1196 int reiserfs_xattr_init(struct super_block *s, int mount_flags)
1200 /* We need generation numbers to ensure that the oid mapping is correct
1201 * v3.5 filesystems don't have them. */
1202 if (!old_format_only(s)) {
1203 set_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
1204 } else if (reiserfs_xattrs_optional(s)) {
1205 /* Old format filesystem, but optional xattrs have been enabled
1206 * at mount time. Error out. */
1207 reiserfs_warning(s, "xattrs/ACLs not supported on pre v3.6 "
1208 "format filesystem. Failing mount.");
1212 /* Old format filesystem, but no optional xattrs have been enabled. This
1213 * means we silently disable xattrs on the filesystem. */
1214 clear_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
1217 /* If we don't have the privroot located yet - go find it */
1218 if (reiserfs_xattrs(s) && !REISERFS_SB(s)->priv_root) {
1219 struct dentry *dentry;
1220 dentry = lookup_one_len(PRIVROOT_NAME, s->s_root,
1221 strlen(PRIVROOT_NAME));
1222 if (!IS_ERR(dentry)) {
1223 if (!(mount_flags & MS_RDONLY) && !dentry->d_inode) {
1224 struct inode *inode = dentry->d_parent->d_inode;
1225 mutex_lock(&inode->i_mutex);
1226 err = inode->i_op->mkdir(inode, dentry, 0700);
1227 mutex_unlock(&inode->i_mutex);
1233 if (dentry && dentry->d_inode)
1235 "Created %s on %s - reserved for "
1240 } else if (!dentry->d_inode) {
1245 err = PTR_ERR(dentry);
1247 if (!err && dentry) {
1248 s->s_root->d_op = &xattr_lookup_poison_ops;
1249 reiserfs_mark_inode_private(dentry->d_inode);
1250 REISERFS_SB(s)->priv_root = dentry;
1251 } else if (!(mount_flags & MS_RDONLY)) { /* xattrs are unavailable */
1252 /* If we're read-only it just means that the dir hasn't been
1253 * created. Not an error -- just no xattrs on the fs. We'll
1254 * check again if we go read-write */
1255 reiserfs_warning(s, "xattrs/ACLs enabled and couldn't "
1256 "find/create .reiserfs_priv. Failing mount.");
1262 /* This is only nonzero if there was an error initializing the xattr
1263 * directory or if there is a condition where we don't support them. */
1265 clear_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
1266 clear_bit(REISERFS_XATTRS_USER, &(REISERFS_SB(s)->s_mount_opt));
1267 clear_bit(REISERFS_POSIXACL, &(REISERFS_SB(s)->s_mount_opt));
1270 /* The super_block MS_POSIXACL must mirror the (no)acl mount option. */
1271 s->s_flags = s->s_flags & ~MS_POSIXACL;
1272 if (reiserfs_posixacl(s))
1273 s->s_flags |= MS_POSIXACL;
1278 static int reiserfs_check_acl(struct inode *inode, int mask)
1280 struct posix_acl *acl;
1281 int error = -EAGAIN; /* do regular unix permission checks by default */
1283 reiserfs_read_lock_xattr_i(inode);
1284 reiserfs_read_lock_xattrs(inode->i_sb);
1286 acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS);
1288 reiserfs_read_unlock_xattrs(inode->i_sb);
1289 reiserfs_read_unlock_xattr_i(inode);
1293 error = posix_acl_permission(inode, acl, mask);
1294 posix_acl_release(acl);
1295 } else if (PTR_ERR(acl) != -ENODATA)
1296 error = PTR_ERR(acl);
1302 int reiserfs_permission(struct inode *inode, int mask, struct nameidata *nd)
1305 * We don't do permission checks on the internal objects.
1306 * Permissions are determined by the "owning" object.
1308 if (is_reiserfs_priv_object(inode))
1312 * Stat data v1 doesn't support ACLs.
1314 if (get_inode_sd_version(inode) == STAT_DATA_V1)
1315 return generic_permission(inode, mask, NULL);
1317 return generic_permission(inode, mask, reiserfs_check_acl);