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 <asm/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 static struct dentry *create_xa_root(struct super_block *sb)
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(-EOPNOTSUPP);
67 xaroot = lookup_one_len(XAROOT_NAME, privroot, strlen(XAROOT_NAME));
70 } else if (!xaroot->d_inode) {
72 mutex_lock(&privroot->d_inode->i_mutex);
74 privroot->d_inode->i_op->mkdir(privroot->d_inode, xaroot,
76 mutex_unlock(&privroot->d_inode->i_mutex);
83 REISERFS_SB(sb)->xattr_root = dget(xaroot);
91 /* This will return a dentry, or error, refering to the xa root directory.
92 * If the xa root doesn't exist yet, the dentry will be returned without
93 * an associated inode. This dentry can be used with ->mkdir to create
94 * the xa directory. */
95 static struct dentry *__get_xa_root(struct super_block *s)
97 struct dentry *privroot = dget(REISERFS_SB(s)->priv_root);
98 struct dentry *xaroot = NULL;
100 if (IS_ERR(privroot) || !privroot)
103 xaroot = lookup_one_len(XAROOT_NAME, privroot, strlen(XAROOT_NAME));
104 if (IS_ERR(xaroot)) {
106 } else if (!xaroot->d_inode) {
112 REISERFS_SB(s)->xattr_root = dget(xaroot);
119 /* Returns the dentry (or NULL) referring to the root of the extended
120 * attribute directory tree. If it has already been retrieved, it is used.
121 * Otherwise, we attempt to retrieve it from disk. It may also return
122 * a pointer-encoded error.
124 static inline struct dentry *get_xa_root(struct super_block *s)
126 struct dentry *dentry = dget(REISERFS_SB(s)->xattr_root);
129 dentry = __get_xa_root(s);
134 /* Opens the directory corresponding to the inode's extended attribute store.
135 * If flags allow, the tree to the directory may be created. If creation is
136 * prohibited, -ENODATA is returned. */
137 static struct dentry *open_xa_dir(const struct inode *inode, int flags)
139 struct dentry *xaroot, *xadir;
142 xaroot = get_xa_root(inode->i_sb);
143 if (IS_ERR(xaroot)) {
145 } else if (!xaroot) {
146 if (flags == 0 || flags & XATTR_CREATE) {
147 xaroot = create_xa_root(inode->i_sb);
152 return ERR_PTR(-ENODATA);
155 /* ok, we have xaroot open */
157 snprintf(namebuf, sizeof(namebuf), "%X.%X",
158 le32_to_cpu(INODE_PKEY(inode)->k_objectid),
159 inode->i_generation);
160 xadir = lookup_one_len(namebuf, xaroot, strlen(namebuf));
166 if (!xadir->d_inode) {
168 if (flags == 0 || flags & XATTR_CREATE) {
169 /* Although there is nothing else trying to create this directory,
170 * another directory with the same hash may be created, so we need
171 * to protect against that */
173 xaroot->d_inode->i_op->mkdir(xaroot->d_inode, xadir,
181 if (!xadir->d_inode) {
184 return ERR_PTR(-ENODATA);
192 /* Returns a dentry corresponding to a specific extended attribute file
193 * for the inode. If flags allow, the file is created. Otherwise, a
194 * valid or negative dentry, or an error is returned. */
195 static struct dentry *get_xa_file_dentry(const struct inode *inode,
196 const char *name, int flags)
198 struct dentry *xadir, *xafile;
201 xadir = open_xa_dir(inode, flags);
203 return ERR_PTR(PTR_ERR(xadir));
204 } else if (xadir && !xadir->d_inode) {
206 return ERR_PTR(-ENODATA);
209 xafile = lookup_one_len(name, xadir, strlen(name));
210 if (IS_ERR(xafile)) {
212 return ERR_PTR(PTR_ERR(xafile));
215 if (xafile->d_inode) { /* file exists */
216 if (flags & XATTR_CREATE) {
221 } else if (flags & XATTR_REPLACE || flags & FL_READONLY) {
224 /* inode->i_mutex is down, so nothing else can try to create
226 err = xadir->d_inode->i_op->create(xadir->d_inode, xafile,
227 0700 | S_IFREG, NULL);
238 xafile = ERR_PTR(err);
242 /* Opens a file pointer to the attribute associated with inode */
243 static struct file *open_xa_file(const struct inode *inode, const char *name,
246 struct dentry *xafile;
249 xafile = get_xa_file_dentry(inode, name, flags);
251 return ERR_PTR(PTR_ERR(xafile));
252 else if (!xafile->d_inode) {
254 return ERR_PTR(-ENODATA);
257 fp = dentry_open(xafile, NULL, O_RDWR);
258 /* dentry_open dputs the dentry if it fails */
264 * this is very similar to fs/reiserfs/dir.c:reiserfs_readdir, but
265 * we need to drop the path before calling the filldir struct. That
266 * would be a big performance hit to the non-xattr case, so I've copied
267 * the whole thing for now. --clm
269 * the big difference is that I go backwards through the directory,
270 * and don't mess with f->f_pos, but the idea is the same. Do some
271 * action on each and every entry in the directory.
273 * we're called with i_mutex held, so there are no worries about the directory
274 * changing underneath us.
276 static int __xattr_readdir(struct file *filp, void *dirent, filldir_t filldir)
278 struct inode *inode = filp->f_dentry->d_inode;
279 struct cpu_key pos_key; /* key of current position in the directory (key of directory entry) */
280 INITIALIZE_PATH(path_to_entry);
281 struct buffer_head *bh;
283 struct item_head *ih, tmp_ih;
287 char small_buf[32]; /* avoid kmalloc if we can */
288 struct reiserfs_de_head *deh;
293 struct reiserfs_dir_entry de;
295 /* form key for search the next directory entry using f_pos field of
297 next_pos = max_reiserfs_offset(inode);
301 if (next_pos <= DOT_DOT_OFFSET)
303 make_cpu_key(&pos_key, inode, next_pos, TYPE_DIRENTRY, 3);
306 search_by_entry_key(inode->i_sb, &pos_key, &path_to_entry,
308 if (search_res == IO_ERROR) {
309 // FIXME: we could just skip part of directory which could
311 pathrelse(&path_to_entry);
315 if (search_res == NAME_NOT_FOUND)
318 set_de_name_and_namelen(&de);
319 entry_num = de.de_entry_num;
320 deh = &(de.de_deh[entry_num]);
325 if (!is_direntry_le_ih(ih)) {
326 reiserfs_warning(inode->i_sb, "not direntry %h", ih);
329 copy_item_head(&tmp_ih, ih);
331 /* we must have found item, that is item of this directory, */
332 RFALSE(COMP_SHORT_KEYS(&(ih->ih_key), &pos_key),
333 "vs-9000: found item %h does not match to dir we readdir %K",
336 if (deh_offset(deh) <= DOT_DOT_OFFSET) {
340 /* look for the previous entry in the directory */
341 next_pos = deh_offset(deh) - 1;
343 if (!de_visible(deh))
344 /* it is hidden entry */
347 d_reclen = entry_length(bh, ih, entry_num);
348 d_name = B_I_DEH_ENTRY_FILE_NAME(bh, ih, deh);
349 d_off = deh_offset(deh);
350 d_ino = deh_objectid(deh);
352 if (!d_name[d_reclen - 1])
353 d_reclen = strlen(d_name);
355 if (d_reclen > REISERFS_MAX_NAME(inode->i_sb->s_blocksize)) {
356 /* too big to send back to VFS */
360 /* Ignore the .reiserfs_priv entry */
361 if (reiserfs_xattrs(inode->i_sb) &&
362 !old_format_only(inode->i_sb) &&
364 le32_to_cpu(INODE_PKEY
365 (REISERFS_SB(inode->i_sb)->priv_root->d_inode)->
369 if (d_reclen <= 32) {
370 local_buf = small_buf;
372 local_buf = kmalloc(d_reclen, GFP_NOFS);
374 pathrelse(&path_to_entry);
377 if (item_moved(&tmp_ih, &path_to_entry)) {
380 /* sigh, must retry. Do this same offset again */
386 // Note, that we copy name to user space via temporary
387 // buffer (local_buf) because filldir will block if
388 // user space buffer is swapped out. At that time
389 // entry can move to somewhere else
390 memcpy(local_buf, d_name, d_reclen);
392 /* the filldir function might need to start transactions,
393 * or do who knows what. Release the path now that we've
394 * copied all the important stuff out of the deh
396 pathrelse(&path_to_entry);
398 if (filldir(dirent, local_buf, d_reclen, d_off, d_ino,
400 if (local_buf != small_buf) {
405 if (local_buf != small_buf) {
411 pathrelse(&path_to_entry);
416 * this could be done with dedicated readdir ops for the xattr files,
417 * but I want to get something working asap
418 * this is stolen from vfs_readdir
422 int xattr_readdir(struct file *file, filldir_t filler, void *buf)
424 struct inode *inode = file->f_dentry->d_inode;
426 if (!file->f_op || !file->f_op->readdir)
428 mutex_lock_nested(&inode->i_mutex, I_MUTEX_XATTR);
429 // down(&inode->i_zombie);
431 if (!IS_DEADDIR(inode)) {
433 res = __xattr_readdir(file, buf, filler);
436 // up(&inode->i_zombie);
437 mutex_unlock(&inode->i_mutex);
442 /* Internal operations on file data */
443 static inline void reiserfs_put_page(struct page *page)
446 page_cache_release(page);
449 static struct page *reiserfs_get_page(struct inode *dir, unsigned long n)
451 struct address_space *mapping = dir->i_mapping;
453 /* We can deadlock if we try to free dentries,
454 and an unlink/rmdir has just occured - GFP_NOFS avoids this */
455 mapping_set_gfp_mask(mapping, GFP_NOFS);
456 page = read_mapping_page(mapping, n, NULL);
458 wait_on_page_locked(page);
460 if (!PageUptodate(page))
469 reiserfs_put_page(page);
470 return ERR_PTR(-EIO);
473 static inline __u32 xattr_hash(const char *msg, int len)
475 return csum_partial(msg, len, 0);
478 /* Generic extended attribute operations that can be used by xa plugins */
481 * inode->i_mutex: down
484 reiserfs_xattr_set(struct inode *inode, const char *name, const void *buffer,
485 size_t buffer_size, int flags)
491 struct address_space *mapping;
493 size_t buffer_pos = 0;
494 struct inode *xinode;
495 struct iattr newattrs;
498 if (get_inode_sd_version(inode) == STAT_DATA_V1)
501 /* Empty xattrs are ok, they're just empty files, no hash */
502 if (buffer && buffer_size)
503 xahash = xattr_hash(buffer, buffer_size);
506 fp = open_xa_file(inode, name, flags);
512 xinode = fp->f_dentry->d_inode;
513 REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
515 /* we need to copy it off.. */
516 if (xinode->i_nlink > 1) {
518 err = reiserfs_xattr_del(inode, name);
521 /* We just killed the old one, we're not replacing anymore */
522 if (flags & XATTR_REPLACE)
523 flags &= ~XATTR_REPLACE;
527 /* Resize it so we're ok to write there */
528 newattrs.ia_size = buffer_size;
529 newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
530 mutex_lock(&xinode->i_mutex);
531 err = notify_change(fp->f_dentry, &newattrs);
535 mapping = xinode->i_mapping;
536 while (buffer_pos < buffer_size || buffer_pos == 0) {
539 size_t page_offset = (file_pos & (PAGE_CACHE_SIZE - 1));
540 if (buffer_size - buffer_pos > PAGE_CACHE_SIZE)
541 chunk = PAGE_CACHE_SIZE;
543 chunk = buffer_size - buffer_pos;
545 page = reiserfs_get_page(xinode, file_pos >> PAGE_CACHE_SHIFT);
552 data = page_address(page);
555 struct reiserfs_xattr_header *rxh;
556 skip = file_pos = sizeof(struct reiserfs_xattr_header);
557 if (chunk + skip > PAGE_CACHE_SIZE)
558 chunk = PAGE_CACHE_SIZE - skip;
559 rxh = (struct reiserfs_xattr_header *)data;
560 rxh->h_magic = cpu_to_le32(REISERFS_XATTR_MAGIC);
561 rxh->h_hash = cpu_to_le32(xahash);
564 err = mapping->a_ops->prepare_write(fp, page, page_offset,
565 page_offset + chunk + skip);
568 memcpy(data + skip, buffer + buffer_pos, chunk);
570 mapping->a_ops->commit_write(fp, page, page_offset,
571 page_offset + chunk +
575 reiserfs_put_page(page);
579 if (err || buffer_size == 0 || !buffer)
583 /* We can't mark the inode dirty if it's not hashed. This is the case
584 * when we're inheriting the default ACL. If we dirty it, the inode
585 * gets marked dirty, but won't (ever) make it onto the dirty list until
586 * it's synced explicitly to clear I_DIRTY. This is bad. */
587 if (!hlist_unhashed(&inode->i_hash)) {
588 inode->i_ctime = CURRENT_TIME_SEC;
589 mark_inode_dirty(inode);
593 mutex_unlock(&xinode->i_mutex);
601 * inode->i_mutex: down
604 reiserfs_xattr_get(const struct inode *inode, const char *name, void *buffer,
611 size_t buffer_pos = 0;
613 struct inode *xinode;
619 /* We can't have xattrs attached to v1 items since they don't have
620 * generation numbers */
621 if (get_inode_sd_version(inode) == STAT_DATA_V1)
624 fp = open_xa_file(inode, name, FL_READONLY);
630 xinode = fp->f_dentry->d_inode;
631 isize = xinode->i_size;
632 REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
634 /* Just return the size needed */
635 if (buffer == NULL) {
636 err = isize - sizeof(struct reiserfs_xattr_header);
640 if (buffer_size < isize - sizeof(struct reiserfs_xattr_header)) {
645 while (file_pos < isize) {
649 if (isize - file_pos > PAGE_CACHE_SIZE)
650 chunk = PAGE_CACHE_SIZE;
652 chunk = isize - file_pos;
654 page = reiserfs_get_page(xinode, file_pos >> PAGE_CACHE_SHIFT);
661 data = page_address(page);
663 struct reiserfs_xattr_header *rxh =
664 (struct reiserfs_xattr_header *)data;
665 skip = file_pos = sizeof(struct reiserfs_xattr_header);
667 /* Magic doesn't match up.. */
668 if (rxh->h_magic != cpu_to_le32(REISERFS_XATTR_MAGIC)) {
670 reiserfs_put_page(page);
671 reiserfs_warning(inode->i_sb,
672 "Invalid magic for xattr (%s) "
673 "associated with %k", name,
678 hash = le32_to_cpu(rxh->h_hash);
680 memcpy(buffer + buffer_pos, data + skip, chunk);
682 reiserfs_put_page(page);
687 err = isize - sizeof(struct reiserfs_xattr_header);
689 if (xattr_hash(buffer, isize - sizeof(struct reiserfs_xattr_header)) !=
691 reiserfs_warning(inode->i_sb,
692 "Invalid hash for xattr (%s) associated "
693 "with %k", name, INODE_PKEY(inode));
705 __reiserfs_xattr_del(struct dentry *xadir, const char *name, int namelen)
707 struct dentry *dentry;
708 struct inode *dir = xadir->d_inode;
711 dentry = lookup_one_len(name, xadir, namelen);
712 if (IS_ERR(dentry)) {
713 err = PTR_ERR(dentry);
715 } else if (!dentry->d_inode) {
720 /* Skip directories.. */
721 if (S_ISDIR(dentry->d_inode->i_mode))
724 if (!is_reiserfs_priv_object(dentry->d_inode)) {
725 reiserfs_warning(dir->i_sb, "OID %08x [%.*s/%.*s] doesn't have "
726 "priv flag set [parent is %sset].",
727 le32_to_cpu(INODE_PKEY(dentry->d_inode)->
728 k_objectid), xadir->d_name.len,
729 xadir->d_name.name, namelen, name,
730 is_reiserfs_priv_object(xadir->
737 err = dir->i_op->unlink(dir, dentry);
748 int reiserfs_xattr_del(struct inode *inode, const char *name)
753 dir = open_xa_dir(inode, FL_READONLY);
759 err = __reiserfs_xattr_del(dir, name, strlen(name));
763 inode->i_ctime = CURRENT_TIME_SEC;
764 mark_inode_dirty(inode);
771 /* The following are side effects of other operations that aren't explicitly
772 * modifying extended attributes. This includes operations such as permissions
773 * or ownership changes, object deletions, etc. */
776 reiserfs_delete_xattrs_filler(void *buf, const char *name, int namelen,
777 loff_t offset, u64 ino, unsigned int d_type)
779 struct dentry *xadir = (struct dentry *)buf;
781 return __reiserfs_xattr_del(xadir, name, namelen);
785 /* This is called w/ inode->i_mutex downed */
786 int reiserfs_delete_xattrs(struct inode *inode)
789 struct dentry *dir, *root;
792 /* Skip out, an xattr has no xattrs associated with it */
793 if (is_reiserfs_priv_object(inode) ||
794 get_inode_sd_version(inode) == STAT_DATA_V1 ||
795 !reiserfs_xattrs(inode->i_sb)) {
798 reiserfs_read_lock_xattrs(inode->i_sb);
799 dir = open_xa_dir(inode, FL_READONLY);
800 reiserfs_read_unlock_xattrs(inode->i_sb);
804 } else if (!dir->d_inode) {
809 fp = dentry_open(dir, NULL, O_RDWR);
812 /* dentry_open dputs the dentry if it fails */
817 err = xattr_readdir(fp, reiserfs_delete_xattrs_filler, dir);
823 /* Leftovers besides . and .. -- that's not good. */
824 if (dir->d_inode->i_nlink <= 2) {
825 root = get_xa_root(inode->i_sb);
826 reiserfs_write_lock_xattrs(inode->i_sb);
827 err = vfs_rmdir(root->d_inode, dir, NULL);
828 reiserfs_write_unlock_xattrs(inode->i_sb);
831 reiserfs_warning(inode->i_sb,
832 "Couldn't remove all entries in directory");
841 REISERFS_I(inode)->i_flags =
842 REISERFS_I(inode)->i_flags & ~i_has_xattr_dir;
846 struct reiserfs_chown_buf {
848 struct dentry *xadir;
852 /* XXX: If there is a better way to do this, I'd love to hear about it */
854 reiserfs_chown_xattrs_filler(void *buf, const char *name, int namelen,
855 loff_t offset, u64 ino, unsigned int d_type)
857 struct reiserfs_chown_buf *chown_buf = (struct reiserfs_chown_buf *)buf;
858 struct dentry *xafile, *xadir = chown_buf->xadir;
859 struct iattr *attrs = chown_buf->attrs;
862 xafile = lookup_one_len(name, xadir, namelen);
864 return PTR_ERR(xafile);
865 else if (!xafile->d_inode) {
870 if (!S_ISDIR(xafile->d_inode->i_mode))
871 err = notify_change(xafile, attrs);
877 int reiserfs_chown_xattrs(struct inode *inode, struct iattr *attrs)
882 struct reiserfs_chown_buf buf;
883 unsigned int ia_valid = attrs->ia_valid;
885 /* Skip out, an xattr has no xattrs associated with it */
886 if (is_reiserfs_priv_object(inode) ||
887 get_inode_sd_version(inode) == STAT_DATA_V1 ||
888 !reiserfs_xattrs(inode->i_sb)) {
891 reiserfs_read_lock_xattrs(inode->i_sb);
892 dir = open_xa_dir(inode, FL_READONLY);
893 reiserfs_read_unlock_xattrs(inode->i_sb);
895 if (PTR_ERR(dir) != -ENODATA)
898 } else if (!dir->d_inode) {
903 fp = dentry_open(dir, NULL, O_RDWR);
906 /* dentry_open dputs the dentry if it fails */
912 attrs->ia_valid &= (ATTR_UID | ATTR_GID | ATTR_CTIME);
917 err = xattr_readdir(fp, reiserfs_chown_xattrs_filler, &buf);
923 err = notify_change(dir, attrs);
930 attrs->ia_valid = ia_valid;
934 /* Actual operations that are exported to VFS-land */
937 * Inode operation getxattr()
938 * Preliminary locking: we down dentry->d_inode->i_mutex
941 reiserfs_getxattr(struct dentry * dentry, const char *name, void *buffer,
944 struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
947 if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
948 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
951 reiserfs_read_lock_xattr_i(dentry->d_inode);
952 reiserfs_read_lock_xattrs(dentry->d_sb);
953 err = xah->get(dentry->d_inode, name, buffer, size);
954 reiserfs_read_unlock_xattrs(dentry->d_sb);
955 reiserfs_read_unlock_xattr_i(dentry->d_inode);
960 * Inode operation setxattr()
962 * dentry->d_inode->i_mutex down
965 reiserfs_setxattr(struct dentry *dentry, const char *name, const void *value,
966 size_t size, int flags)
968 struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
972 if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
973 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
976 reiserfs_write_lock_xattr_i(dentry->d_inode);
977 lock = !has_xattr_dir(dentry->d_inode);
979 reiserfs_write_lock_xattrs(dentry->d_sb);
981 reiserfs_read_lock_xattrs(dentry->d_sb);
982 err = xah->set(dentry->d_inode, name, value, size, flags);
984 reiserfs_write_unlock_xattrs(dentry->d_sb);
986 reiserfs_read_unlock_xattrs(dentry->d_sb);
987 reiserfs_write_unlock_xattr_i(dentry->d_inode);
992 * Inode operation removexattr()
994 * dentry->d_inode->i_mutex down
996 int reiserfs_removexattr(struct dentry *dentry, const char *name)
999 struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
1001 if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
1002 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
1005 reiserfs_write_lock_xattr_i(dentry->d_inode);
1006 reiserfs_read_lock_xattrs(dentry->d_sb);
1008 /* Deletion pre-operation */
1010 err = xah->del(dentry->d_inode, name);
1015 err = reiserfs_xattr_del(dentry->d_inode, name);
1017 dentry->d_inode->i_ctime = CURRENT_TIME_SEC;
1018 mark_inode_dirty(dentry->d_inode);
1021 reiserfs_read_unlock_xattrs(dentry->d_sb);
1022 reiserfs_write_unlock_xattr_i(dentry->d_inode);
1026 /* This is what filldir will use:
1027 * r_pos will always contain the amount of space required for the entire
1028 * list. If r_pos becomes larger than r_size, we need more space and we
1029 * return an error indicating this. If r_pos is less than r_size, then we've
1030 * filled the buffer successfully and we return success */
1031 struct reiserfs_listxattr_buf {
1035 struct inode *r_inode;
1039 reiserfs_listxattr_filler(void *buf, const char *name, int namelen,
1040 loff_t offset, u64 ino, unsigned int d_type)
1042 struct reiserfs_listxattr_buf *b = (struct reiserfs_listxattr_buf *)buf;
1045 || (namelen != 1 && (name[1] != '.' || namelen != 2))) {
1046 struct reiserfs_xattr_handler *xah =
1047 find_xattr_handler_prefix(name);
1049 return 0; /* Unsupported xattr name, skip it */
1051 /* We call ->list() twice because the operation isn't required to just
1052 * return the name back - we want to make sure we have enough space */
1053 len += xah->list(b->r_inode, name, namelen, NULL);
1056 if (b->r_pos + len + 1 <= b->r_size) {
1057 char *p = b->r_buf + b->r_pos;
1058 p += xah->list(b->r_inode, name, namelen, p);
1061 b->r_pos += len + 1;
1069 * Inode operation listxattr()
1071 * Preliminary locking: we down dentry->d_inode->i_mutex
1073 ssize_t reiserfs_listxattr(struct dentry * dentry, char *buffer, size_t size)
1078 struct reiserfs_listxattr_buf buf;
1080 if (!dentry->d_inode)
1083 if (!reiserfs_xattrs(dentry->d_sb) ||
1084 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
1087 reiserfs_read_lock_xattr_i(dentry->d_inode);
1088 reiserfs_read_lock_xattrs(dentry->d_sb);
1089 dir = open_xa_dir(dentry->d_inode, FL_READONLY);
1090 reiserfs_read_unlock_xattrs(dentry->d_sb);
1093 if (err == -ENODATA)
1094 err = 0; /* Not an error if there aren't any xattrs */
1098 fp = dentry_open(dir, NULL, O_RDWR);
1101 /* dentry_open dputs the dentry if it fails */
1106 buf.r_size = buffer ? size : 0;
1108 buf.r_inode = dentry->d_inode;
1110 REISERFS_I(dentry->d_inode)->i_flags |= i_has_xattr_dir;
1112 err = xattr_readdir(fp, reiserfs_listxattr_filler, &buf);
1116 if (buf.r_pos > buf.r_size && buffer != NULL)
1125 reiserfs_read_unlock_xattr_i(dentry->d_inode);
1129 /* This is the implementation for the xattr plugin infrastructure */
1130 static struct list_head xattr_handlers = LIST_HEAD_INIT(xattr_handlers);
1131 static DEFINE_RWLOCK(handler_lock);
1133 static struct reiserfs_xattr_handler *find_xattr_handler_prefix(const char
1136 struct reiserfs_xattr_handler *xah = NULL;
1137 struct list_head *p;
1139 read_lock(&handler_lock);
1140 list_for_each(p, &xattr_handlers) {
1141 xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
1142 if (strncmp(xah->prefix, prefix, strlen(xah->prefix)) == 0)
1147 read_unlock(&handler_lock);
1151 static void __unregister_handlers(void)
1153 struct reiserfs_xattr_handler *xah;
1154 struct list_head *p, *tmp;
1156 list_for_each_safe(p, tmp, &xattr_handlers) {
1157 xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
1163 INIT_LIST_HEAD(&xattr_handlers);
1166 int __init reiserfs_xattr_register_handlers(void)
1169 struct reiserfs_xattr_handler *xah;
1170 struct list_head *p;
1172 write_lock(&handler_lock);
1174 /* If we're already initialized, nothing to do */
1175 if (!list_empty(&xattr_handlers)) {
1176 write_unlock(&handler_lock);
1180 /* Add the handlers */
1181 list_add_tail(&user_handler.handlers, &xattr_handlers);
1182 list_add_tail(&trusted_handler.handlers, &xattr_handlers);
1183 #ifdef CONFIG_REISERFS_FS_SECURITY
1184 list_add_tail(&security_handler.handlers, &xattr_handlers);
1186 #ifdef CONFIG_REISERFS_FS_POSIX_ACL
1187 list_add_tail(&posix_acl_access_handler.handlers, &xattr_handlers);
1188 list_add_tail(&posix_acl_default_handler.handlers, &xattr_handlers);
1191 /* Run initializers, if available */
1192 list_for_each(p, &xattr_handlers) {
1193 xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
1203 /* Clean up other handlers, if any failed */
1205 __unregister_handlers();
1207 write_unlock(&handler_lock);
1211 void reiserfs_xattr_unregister_handlers(void)
1213 write_lock(&handler_lock);
1214 __unregister_handlers();
1215 write_unlock(&handler_lock);
1218 /* This will catch lookups from the fs root to .reiserfs_priv */
1220 xattr_lookup_poison(struct dentry *dentry, struct qstr *q1, struct qstr *name)
1222 struct dentry *priv_root = REISERFS_SB(dentry->d_sb)->priv_root;
1223 if (name->len == priv_root->d_name.len &&
1224 name->hash == priv_root->d_name.hash &&
1225 !memcmp(name->name, priv_root->d_name.name, name->len)) {
1227 } else if (q1->len == name->len &&
1228 !memcmp(q1->name, name->name, name->len))
1233 static struct dentry_operations xattr_lookup_poison_ops = {
1234 .d_compare = xattr_lookup_poison,
1237 /* We need to take a copy of the mount flags since things like
1238 * MS_RDONLY don't get set until *after* we're called.
1239 * mount_flags != mount_options */
1240 int reiserfs_xattr_init(struct super_block *s, int mount_flags)
1244 /* We need generation numbers to ensure that the oid mapping is correct
1245 * v3.5 filesystems don't have them. */
1246 if (!old_format_only(s)) {
1247 set_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
1248 } else if (reiserfs_xattrs_optional(s)) {
1249 /* Old format filesystem, but optional xattrs have been enabled
1250 * at mount time. Error out. */
1251 reiserfs_warning(s, "xattrs/ACLs not supported on pre v3.6 "
1252 "format filesystem. Failing mount.");
1256 /* Old format filesystem, but no optional xattrs have been enabled. This
1257 * means we silently disable xattrs on the filesystem. */
1258 clear_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
1261 /* If we don't have the privroot located yet - go find it */
1262 if (reiserfs_xattrs(s) && !REISERFS_SB(s)->priv_root) {
1263 struct dentry *dentry;
1264 dentry = lookup_one_len(PRIVROOT_NAME, s->s_root,
1265 strlen(PRIVROOT_NAME));
1266 if (!IS_ERR(dentry)) {
1267 if (!(mount_flags & MS_RDONLY) && !dentry->d_inode) {
1268 struct inode *inode = dentry->d_parent->d_inode;
1269 mutex_lock(&inode->i_mutex);
1270 err = inode->i_op->mkdir(inode, dentry, 0700);
1271 mutex_unlock(&inode->i_mutex);
1277 if (dentry && dentry->d_inode)
1279 "Created %s on %s - reserved for "
1284 } else if (!dentry->d_inode) {
1289 err = PTR_ERR(dentry);
1291 if (!err && dentry) {
1292 s->s_root->d_op = &xattr_lookup_poison_ops;
1293 reiserfs_mark_inode_private(dentry->d_inode);
1294 REISERFS_SB(s)->priv_root = dentry;
1295 } else if (!(mount_flags & MS_RDONLY)) { /* xattrs are unavailable */
1296 /* If we're read-only it just means that the dir hasn't been
1297 * created. Not an error -- just no xattrs on the fs. We'll
1298 * check again if we go read-write */
1299 reiserfs_warning(s, "xattrs/ACLs enabled and couldn't "
1300 "find/create .reiserfs_priv. Failing mount.");
1306 /* This is only nonzero if there was an error initializing the xattr
1307 * directory or if there is a condition where we don't support them. */
1309 clear_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
1310 clear_bit(REISERFS_XATTRS_USER, &(REISERFS_SB(s)->s_mount_opt));
1311 clear_bit(REISERFS_POSIXACL, &(REISERFS_SB(s)->s_mount_opt));
1314 /* The super_block MS_POSIXACL must mirror the (no)acl mount option. */
1315 s->s_flags = s->s_flags & ~MS_POSIXACL;
1316 if (reiserfs_posixacl(s))
1317 s->s_flags |= MS_POSIXACL;
1322 static int reiserfs_check_acl(struct inode *inode, int mask)
1324 struct posix_acl *acl;
1325 int error = -EAGAIN; /* do regular unix permission checks by default */
1327 reiserfs_read_lock_xattr_i(inode);
1328 reiserfs_read_lock_xattrs(inode->i_sb);
1330 acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS);
1332 reiserfs_read_unlock_xattrs(inode->i_sb);
1333 reiserfs_read_unlock_xattr_i(inode);
1337 error = posix_acl_permission(inode, acl, mask);
1338 posix_acl_release(acl);
1339 } else if (PTR_ERR(acl) != -ENODATA)
1340 error = PTR_ERR(acl);
1346 int reiserfs_permission(struct inode *inode, int mask, struct nameidata *nd)
1349 * We don't do permission checks on the internal objects.
1350 * Permissions are determined by the "owning" object.
1352 if (is_reiserfs_priv_object(inode))
1356 * Stat data v1 doesn't support ACLs.
1358 if (get_inode_sd_version(inode) == STAT_DATA_V1)
1359 return generic_permission(inode, mask, NULL);
1361 return generic_permission(inode, mask, reiserfs_check_acl);