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/dcache.h>
34 #include <linux/namei.h>
35 #include <linux/errno.h>
37 #include <linux/file.h>
38 #include <linux/pagemap.h>
39 #include <linux/xattr.h>
40 #include <linux/reiserfs_xattr.h>
41 #include <linux/reiserfs_acl.h>
42 #include <linux/mbcache.h>
43 #include <asm/uaccess.h>
44 #include <asm/checksum.h>
45 #include <linux/smp_lock.h>
46 #include <linux/stat.h>
47 #include <asm/semaphore.h>
49 #define FL_READONLY 128
50 #define FL_DIR_SEM_HELD 256
51 #define PRIVROOT_NAME ".reiserfs_priv"
52 #define XAROOT_NAME "xattrs"
54 static struct reiserfs_xattr_handler *find_xattr_handler_prefix (const char *prefix);
56 static struct dentry *
57 create_xa_root (struct super_block *sb)
59 struct dentry *privroot = dget (REISERFS_SB(sb)->priv_root);
60 struct dentry *xaroot;
62 /* This needs to be created at mount-time */
64 return ERR_PTR(-EOPNOTSUPP);
66 xaroot = lookup_one_len (XAROOT_NAME, privroot, strlen (XAROOT_NAME));
67 if (IS_ERR (xaroot)) {
69 } else if (!xaroot->d_inode) {
71 down (&privroot->d_inode->i_sem);
72 err = privroot->d_inode->i_op->mkdir (privroot->d_inode, xaroot, 0700);
73 up (&privroot->d_inode->i_sem);
80 REISERFS_SB(sb)->xattr_root = dget (xaroot);
88 /* This will return a dentry, or error, refering to the xa root directory.
89 * If the xa root doesn't exist yet, the dentry will be returned without
90 * an associated inode. This dentry can be used with ->mkdir to create
91 * the xa directory. */
92 static struct dentry *
93 __get_xa_root (struct super_block *s)
95 struct dentry *privroot = dget (REISERFS_SB(s)->priv_root);
96 struct dentry *xaroot = NULL;
98 if (IS_ERR (privroot) || !privroot)
101 xaroot = lookup_one_len (XAROOT_NAME, privroot, strlen (XAROOT_NAME));
102 if (IS_ERR (xaroot)) {
104 } else if (!xaroot->d_inode) {
110 REISERFS_SB(s)->xattr_root = dget (xaroot);
117 /* Returns the dentry (or NULL) referring to the root of the extended
118 * attribute directory tree. If it has already been retreived, it is used.
119 * Otherwise, we attempt to retreive it from disk. It may also return
120 * a pointer-encoded error.
122 static inline struct dentry *
123 get_xa_root (struct super_block *s)
125 struct dentry *dentry = dget (REISERFS_SB(s)->xattr_root);
128 dentry = __get_xa_root (s);
133 /* Opens the directory corresponding to the inode's extended attribute store.
134 * If flags allow, the tree to the directory may be created. If creation is
135 * prohibited, -ENODATA is returned. */
136 static struct dentry *
137 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));
161 if (IS_ERR (xadir)) {
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 */
172 err = xaroot->d_inode->i_op->mkdir (xaroot->d_inode, xadir, 0700);
176 return ERR_PTR (err);
179 if (!xadir->d_inode) {
182 return ERR_PTR (-ENODATA);
184 /* Newly created object.. Need to mark it private */
185 REISERFS_I(xadir->d_inode)->i_flags |= i_priv_object;
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 *
196 get_xa_file_dentry (const struct inode *inode, const char *name, int flags)
198 struct dentry *xadir, *xafile;
201 xadir = open_xa_dir (inode, flags);
202 if (IS_ERR (xadir)) {
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_sem is down, so nothing else can try to create
226 err = xadir->d_inode->i_op->create (xadir->d_inode, xafile,
233 /* Newly created object.. Need to mark it private */
234 REISERFS_I(xafile->d_inode)->i_flags |= i_priv_object;
240 xafile = ERR_PTR (err);
245 /* Opens a file pointer to the attribute associated with inode */
247 open_xa_file (const struct inode *inode, const char *name, int flags)
249 struct dentry *xafile;
252 xafile = get_xa_file_dentry (inode, name, flags);
254 return ERR_PTR (PTR_ERR (xafile));
255 else if (!xafile->d_inode) {
257 return ERR_PTR (-ENODATA);
260 fp = dentry_open (xafile, NULL, O_RDWR);
261 /* dentry_open dputs the dentry if it fails */
268 * this is very similar to fs/reiserfs/dir.c:reiserfs_readdir, but
269 * we need to drop the path before calling the filldir struct. That
270 * would be a big performance hit to the non-xattr case, so I've copied
271 * the whole thing for now. --clm
273 * the big difference is that I go backwards through the directory,
274 * and don't mess with f->f_pos, but the idea is the same. Do some
275 * action on each and every entry in the directory.
277 * we're called with i_sem held, so there are no worries about the directory
278 * changing underneath us.
280 static int __xattr_readdir(struct file * filp, void * dirent, filldir_t filldir)
282 struct inode *inode = filp->f_dentry->d_inode;
283 struct cpu_key pos_key; /* key of current position in the directory (key of directory entry) */
284 INITIALIZE_PATH (path_to_entry);
285 struct buffer_head * bh;
287 struct item_head * ih, tmp_ih;
291 char small_buf[32] ; /* avoid kmalloc if we can */
292 struct reiserfs_de_head *deh;
297 struct reiserfs_dir_entry de;
300 /* form key for search the next directory entry using f_pos field of
302 next_pos = max_reiserfs_offset(inode);
306 if (next_pos <= DOT_DOT_OFFSET)
308 make_cpu_key (&pos_key, inode, next_pos, TYPE_DIRENTRY, 3);
310 search_res = search_by_entry_key(inode->i_sb, &pos_key, &path_to_entry, &de);
311 if (search_res == IO_ERROR) {
312 // FIXME: we could just skip part of directory which could
314 pathrelse(&path_to_entry);
318 if (search_res == NAME_NOT_FOUND)
321 set_de_name_and_namelen(&de);
322 entry_num = de.de_entry_num;
323 deh = &(de.de_deh[entry_num]);
328 if (!is_direntry_le_ih(ih)) {
329 reiserfs_warning(inode->i_sb, "not direntry %h", ih);
332 copy_item_head(&tmp_ih, ih);
334 /* we must have found item, that is item of this directory, */
335 RFALSE( COMP_SHORT_KEYS (&(ih->ih_key), &pos_key),
336 "vs-9000: found item %h does not match to dir we readdir %K",
339 if (deh_offset(deh) <= DOT_DOT_OFFSET) {
343 /* look for the previous entry in the directory */
344 next_pos = deh_offset (deh) - 1;
346 if (!de_visible (deh))
347 /* it is hidden entry */
350 d_reclen = entry_length(bh, ih, entry_num);
351 d_name = B_I_DEH_ENTRY_FILE_NAME (bh, ih, deh);
352 d_off = deh_offset (deh);
353 d_ino = deh_objectid (deh);
355 if (!d_name[d_reclen - 1])
356 d_reclen = strlen (d_name);
358 if (d_reclen > REISERFS_MAX_NAME(inode->i_sb->s_blocksize)){
359 /* too big to send back to VFS */
363 /* Ignore the .reiserfs_priv entry */
364 if (reiserfs_xattrs (inode->i_sb) &&
365 !old_format_only(inode->i_sb) &&
366 deh_objectid (deh) == le32_to_cpu (INODE_PKEY(REISERFS_SB(inode->i_sb)->priv_root->d_inode)->k_objectid))
369 if (d_reclen <= 32) {
370 local_buf = small_buf ;
372 local_buf = reiserfs_kmalloc(d_reclen, GFP_NOFS, inode->i_sb) ;
374 pathrelse (&path_to_entry);
377 if (item_moved (&tmp_ih, &path_to_entry)) {
378 reiserfs_kfree(local_buf, d_reclen, inode->i_sb) ;
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) {
401 reiserfs_kfree(local_buf, d_reclen, inode->i_sb) ;
405 if (local_buf != small_buf) {
406 reiserfs_kfree(local_buf, d_reclen, inode->i_sb) ;
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)
429 // down(&inode->i_zombie);
431 if (!IS_DEADDIR(inode)) {
433 res = __xattr_readdir(file, buf, filler);
436 // up(&inode->i_zombie);
443 /* Internal operations on file data */
445 reiserfs_put_page(struct page *page)
448 page_cache_release(page);
452 reiserfs_get_page(struct inode *dir, unsigned long n)
454 struct address_space *mapping = dir->i_mapping;
456 /* We can deadlock if we try to free dentries,
457 and an unlink/rmdir has just occured - GFP_NOFS avoids this */
458 mapping->flags = (mapping->flags & ~__GFP_BITS_MASK) | GFP_NOFS;
459 page = read_cache_page (mapping, n,
460 (filler_t*)mapping->a_ops->readpage, NULL);
462 wait_on_page_locked(page);
464 if (!PageUptodate(page))
473 reiserfs_put_page(page);
474 return ERR_PTR(-EIO);
478 xattr_hash (const char *msg, int len)
480 return csum_partial (msg, len, 0);
483 /* Generic extended attribute operations that can be used by xa plugins */
489 reiserfs_xattr_set (struct inode *inode, const char *name, const void *buffer,
490 size_t buffer_size, int flags)
496 struct address_space *mapping;
498 size_t buffer_pos = 0;
499 struct inode *xinode;
500 struct iattr newattrs;
503 if (IS_RDONLY (inode))
506 if (IS_IMMUTABLE (inode) || IS_APPEND (inode))
509 if (get_inode_sd_version (inode) == STAT_DATA_V1)
512 /* Empty xattrs are ok, they're just empty files, no hash */
513 if (buffer && buffer_size)
514 xahash = xattr_hash (buffer, buffer_size);
517 fp = open_xa_file (inode, name, flags);
523 xinode = fp->f_dentry->d_inode;
524 REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
526 /* we need to copy it off.. */
527 if (xinode->i_nlink > 1) {
529 err = reiserfs_xattr_del (inode, name);
532 /* We just killed the old one, we're not replacing anymore */
533 if (flags & XATTR_REPLACE)
534 flags &= ~XATTR_REPLACE;
538 /* Resize it so we're ok to write there */
539 newattrs.ia_size = buffer_size;
540 newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
541 down (&xinode->i_sem);
542 err = notify_change(fp->f_dentry, &newattrs);
546 mapping = xinode->i_mapping;
547 while (buffer_pos < buffer_size || buffer_pos == 0) {
550 size_t page_offset = (file_pos & (PAGE_CACHE_SIZE - 1));
551 if (buffer_size - buffer_pos > PAGE_CACHE_SIZE)
552 chunk = PAGE_CACHE_SIZE;
554 chunk = buffer_size - buffer_pos;
556 page = reiserfs_get_page (xinode, file_pos >> PAGE_CACHE_SHIFT);
558 err = PTR_ERR (page);
563 data = page_address (page);
566 struct reiserfs_xattr_header *rxh;
567 skip = file_pos = sizeof (struct reiserfs_xattr_header);
568 if (chunk + skip > PAGE_CACHE_SIZE)
569 chunk = PAGE_CACHE_SIZE - skip;
570 rxh = (struct reiserfs_xattr_header *)data;
571 rxh->h_magic = cpu_to_le32 (REISERFS_XATTR_MAGIC);
572 rxh->h_hash = cpu_to_le32 (xahash);
575 err = mapping->a_ops->prepare_write (fp, page, page_offset,
576 page_offset + chunk + skip);
579 memcpy (data + skip, buffer + buffer_pos, chunk);
580 err = mapping->a_ops->commit_write (fp, page, page_offset,
581 page_offset + chunk + skip);
584 reiserfs_put_page (page);
588 if (err || buffer_size == 0 || !buffer)
592 inode->i_ctime = CURRENT_TIME;
593 mark_inode_dirty (inode);
607 reiserfs_xattr_get (const struct inode *inode, const char *name, void *buffer,
614 size_t buffer_pos = 0;
616 struct inode *xinode;
622 /* We can't have xattrs attached to v1 items since they don't have
623 * generation numbers */
624 if (get_inode_sd_version (inode) == STAT_DATA_V1)
627 fp = open_xa_file (inode, name, FL_READONLY);
633 xinode = fp->f_dentry->d_inode;
634 isize = xinode->i_size;
635 REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
637 /* Just return the size needed */
638 if (buffer == NULL) {
639 err = isize - sizeof (struct reiserfs_xattr_header);
643 if (buffer_size < isize - sizeof (struct reiserfs_xattr_header)) {
648 while (file_pos < isize) {
652 if (isize - file_pos > PAGE_CACHE_SIZE)
653 chunk = PAGE_CACHE_SIZE;
655 chunk = isize - file_pos;
657 page = reiserfs_get_page (xinode, file_pos >> PAGE_CACHE_SHIFT);
659 err = PTR_ERR (page);
664 data = page_address (page);
666 struct reiserfs_xattr_header *rxh =
667 (struct reiserfs_xattr_header *)data;
668 skip = file_pos = sizeof (struct reiserfs_xattr_header);
670 /* Magic doesn't match up.. */
671 if (rxh->h_magic != cpu_to_le32 (REISERFS_XATTR_MAGIC)) {
673 reiserfs_put_page (page);
674 reiserfs_warning (inode->i_sb, "Invalid magic for xattr (%s) "
675 "associated with %k", name,
680 hash = le32_to_cpu (rxh->h_hash);
682 memcpy (buffer + buffer_pos, data + skip, chunk);
684 reiserfs_put_page (page);
689 err = isize - sizeof (struct reiserfs_xattr_header);
691 if (xattr_hash (buffer, isize - sizeof (struct reiserfs_xattr_header)) != hash) {
692 reiserfs_warning (inode->i_sb, "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)->k_objectid),
728 xadir->d_name.len, xadir->d_name.name, namelen, name,
729 is_reiserfs_priv_object (xadir->d_inode) ? "" : "not ");
734 err = dir->i_op->unlink (dir, dentry);
747 reiserfs_xattr_del (struct inode *inode, const char *name)
752 if (IS_RDONLY (inode))
755 dir = open_xa_dir (inode, FL_READONLY);
761 err = __reiserfs_xattr_del (dir, name, strlen (name));
768 /* The following are side effects of other operations that aren't explicitly
769 * modifying extended attributes. This includes operations such as permissions
770 * or ownership changes, object deletions, etc. */
773 reiserfs_delete_xattrs_filler (void *buf, const char *name, int namelen,
774 loff_t offset, ino_t ino, unsigned int d_type)
776 struct dentry *xadir = (struct dentry *)buf;
778 return __reiserfs_xattr_del (xadir, name, namelen);
782 /* This is called w/ inode->i_sem downed */
784 reiserfs_delete_xattrs (struct inode *inode)
787 struct dentry *dir, *root;
790 /* Skip out, an xattr has no xattrs associated with it */
791 if (is_reiserfs_priv_object (inode) ||
792 get_inode_sd_version (inode) == STAT_DATA_V1 ||
793 !reiserfs_xattrs(inode->i_sb))
797 reiserfs_read_lock_xattrs (inode->i_sb);
798 dir = open_xa_dir (inode, FL_READONLY);
799 reiserfs_read_unlock_xattrs (inode->i_sb);
803 } else if (!dir->d_inode) {
808 fp = dentry_open (dir, NULL, O_RDWR);
811 /* dentry_open dputs the dentry if it fails */
816 err = xattr_readdir (fp, reiserfs_delete_xattrs_filler, dir);
822 /* Leftovers besides . and .. -- that's not good. */
823 if (dir->d_inode->i_nlink <= 2) {
824 root = get_xa_root (inode->i_sb);
825 reiserfs_write_lock_xattrs (inode->i_sb);
826 err = vfs_rmdir (root->d_inode, dir);
827 reiserfs_write_unlock_xattrs (inode->i_sb);
830 reiserfs_warning (inode->i_sb,
831 "Couldn't remove all entries in directory");
840 REISERFS_I(inode)->i_flags = REISERFS_I(inode)->i_flags & ~i_has_xattr_dir;
844 struct reiserfs_chown_buf {
846 struct dentry *xadir;
850 /* XXX: If there is a better way to do this, I'd love to hear about it */
852 reiserfs_chown_xattrs_filler (void *buf, const char *name, int namelen,
853 loff_t offset, ino_t ino, unsigned int d_type)
855 struct reiserfs_chown_buf *chown_buf = (struct reiserfs_chown_buf *)buf;
856 struct dentry *xafile, *xadir = chown_buf->xadir;
857 struct iattr *attrs = chown_buf->attrs;
860 xafile = lookup_one_len (name, xadir, namelen);
862 return PTR_ERR (xafile);
863 else if (!xafile->d_inode) {
868 if (!S_ISDIR (xafile->d_inode->i_mode))
869 err = notify_change (xafile, attrs);
876 reiserfs_chown_xattrs (struct inode *inode, struct iattr *attrs)
881 struct reiserfs_chown_buf buf;
882 unsigned int ia_valid = attrs->ia_valid;
884 /* Skip out, an xattr has no xattrs associated with it */
885 if (is_reiserfs_priv_object (inode) ||
886 get_inode_sd_version (inode) == STAT_DATA_V1 ||
887 !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;
935 /* Actual operations that are exported to VFS-land */
938 * Inode operation getxattr()
939 * Preliminary locking: we down dentry->d_inode->i_sem
942 reiserfs_getxattr (struct dentry *dentry, const char *name, void *buffer,
945 struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix (name);
948 if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
949 get_inode_sd_version (dentry->d_inode) == STAT_DATA_V1)
952 reiserfs_read_lock_xattr_i (dentry->d_inode);
953 reiserfs_read_lock_xattrs (dentry->d_sb);
954 err = xah->get (dentry->d_inode, name, buffer, size);
955 reiserfs_read_unlock_xattrs (dentry->d_sb);
956 reiserfs_read_unlock_xattr_i (dentry->d_inode);
962 * Inode operation setxattr()
964 * dentry->d_inode->i_sem down
967 reiserfs_setxattr (struct dentry *dentry, const char *name, const void *value,
968 size_t size, int flags)
970 struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix (name);
974 if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
975 get_inode_sd_version (dentry->d_inode) == STAT_DATA_V1)
978 if (IS_RDONLY (dentry->d_inode))
981 if (IS_IMMUTABLE (dentry->d_inode) || IS_APPEND (dentry->d_inode))
984 reiserfs_write_lock_xattr_i (dentry->d_inode);
985 lock = !has_xattr_dir (dentry->d_inode);
987 reiserfs_write_lock_xattrs (dentry->d_sb);
989 reiserfs_read_lock_xattrs (dentry->d_sb);
990 err = xah->set (dentry->d_inode, name, value, size, flags);
992 reiserfs_write_unlock_xattrs (dentry->d_sb);
994 reiserfs_read_unlock_xattrs (dentry->d_sb);
995 reiserfs_write_unlock_xattr_i (dentry->d_inode);
1000 * Inode operation removexattr()
1002 * dentry->d_inode->i_sem down
1005 reiserfs_removexattr (struct dentry *dentry, const char *name)
1008 struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix (name);
1010 if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
1011 get_inode_sd_version (dentry->d_inode) == STAT_DATA_V1)
1014 if (IS_RDONLY (dentry->d_inode))
1017 if (IS_IMMUTABLE (dentry->d_inode) || IS_APPEND (dentry->d_inode))
1020 reiserfs_write_lock_xattr_i (dentry->d_inode);
1021 reiserfs_read_lock_xattrs (dentry->d_sb);
1023 /* Deletion pre-operation */
1025 err = xah->del (dentry->d_inode, name);
1030 err = reiserfs_xattr_del (dentry->d_inode, name);
1032 dentry->d_inode->i_ctime = CURRENT_TIME;
1033 mark_inode_dirty (dentry->d_inode);
1036 reiserfs_read_unlock_xattrs (dentry->d_sb);
1037 reiserfs_write_unlock_xattr_i (dentry->d_inode);
1042 /* This is what filldir will use:
1043 * r_pos will always contain the amount of space required for the entire
1044 * list. If r_pos becomes larger than r_size, we need more space and we
1045 * return an error indicating this. If r_pos is less than r_size, then we've
1046 * filled the buffer successfully and we return success */
1047 struct reiserfs_listxattr_buf {
1051 struct inode *r_inode;
1055 reiserfs_listxattr_filler (void *buf, const char *name, int namelen,
1056 loff_t offset, ino_t ino, unsigned int d_type)
1058 struct reiserfs_listxattr_buf *b = (struct reiserfs_listxattr_buf *)buf;
1060 if (name[0] != '.' || (namelen != 1 && (name[1] != '.' || namelen != 2))) {
1061 struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix (name);
1062 if (!xah) return 0; /* Unsupported xattr name, skip it */
1064 /* We call ->list() twice because the operation isn't required to just
1065 * return the name back - we want to make sure we have enough space */
1066 len += xah->list (b->r_inode, name, namelen, NULL);
1069 if (b->r_pos + len + 1 <= b->r_size) {
1070 char *p = b->r_buf + b->r_pos;
1071 p += xah->list (b->r_inode, name, namelen, p);
1074 b->r_pos += len + 1;
1081 * Inode operation listxattr()
1083 * Preliminary locking: we down dentry->d_inode->i_sem
1086 reiserfs_listxattr (struct dentry *dentry, char *buffer, size_t size)
1091 struct reiserfs_listxattr_buf buf;
1093 if (!dentry->d_inode)
1096 if (!reiserfs_xattrs(dentry->d_sb) ||
1097 get_inode_sd_version (dentry->d_inode) == STAT_DATA_V1)
1100 reiserfs_read_lock_xattr_i (dentry->d_inode);
1101 reiserfs_read_lock_xattrs (dentry->d_sb);
1102 dir = open_xa_dir (dentry->d_inode, FL_READONLY);
1103 reiserfs_read_unlock_xattrs (dentry->d_sb);
1105 err = PTR_ERR (dir);
1106 if (err == -ENODATA)
1107 err = 0; /* Not an error if there aren't any xattrs */
1111 fp = dentry_open (dir, NULL, O_RDWR);
1114 /* dentry_open dputs the dentry if it fails */
1119 buf.r_size = buffer ? size : 0;
1121 buf.r_inode = dentry->d_inode;
1123 REISERFS_I(dentry->d_inode)->i_flags |= i_has_xattr_dir;
1125 err = xattr_readdir (fp, reiserfs_listxattr_filler, &buf);
1129 if (buf.r_pos > buf.r_size && buffer != NULL)
1138 reiserfs_read_unlock_xattr_i (dentry->d_inode);
1142 /* This is the implementation for the xattr plugin infrastructure */
1143 static struct list_head xattr_handlers = LIST_HEAD_INIT (xattr_handlers);
1144 static rwlock_t handler_lock = RW_LOCK_UNLOCKED;
1146 static struct reiserfs_xattr_handler *
1147 find_xattr_handler_prefix (const char *prefix)
1149 struct reiserfs_xattr_handler *xah = NULL;
1150 struct list_head *p;
1152 read_lock (&handler_lock);
1153 list_for_each (p, &xattr_handlers) {
1154 xah = list_entry (p, struct reiserfs_xattr_handler, handlers);
1155 if (strncmp (xah->prefix, prefix, strlen (xah->prefix)) == 0)
1160 read_unlock (&handler_lock);
1165 __unregister_handlers (void)
1167 struct reiserfs_xattr_handler *xah;
1168 struct list_head *p, *tmp;
1170 list_for_each_safe (p, tmp, &xattr_handlers) {
1171 xah = list_entry (p, struct reiserfs_xattr_handler, handlers);
1177 INIT_LIST_HEAD (&xattr_handlers);
1181 reiserfs_xattr_register_handlers (void)
1184 struct reiserfs_xattr_handler *xah;
1185 struct list_head *p;
1187 write_lock (&handler_lock);
1189 /* If we're already initialized, nothing to do */
1190 if (!list_empty (&xattr_handlers)) {
1191 write_unlock (&handler_lock);
1195 /* Add the handlers */
1196 list_add_tail (&user_handler.handlers, &xattr_handlers);
1197 list_add_tail (&trusted_handler.handlers, &xattr_handlers);
1198 #ifdef CONFIG_REISERFS_FS_SECURITY
1199 list_add_tail (&security_handler.handlers, &xattr_handlers);
1201 #ifdef CONFIG_REISERFS_FS_POSIX_ACL
1202 list_add_tail (&posix_acl_access_handler.handlers, &xattr_handlers);
1203 list_add_tail (&posix_acl_default_handler.handlers, &xattr_handlers);
1206 /* Run initializers, if available */
1207 list_for_each (p, &xattr_handlers) {
1208 xah = list_entry (p, struct reiserfs_xattr_handler, handlers);
1218 /* Clean up other handlers, if any failed */
1220 __unregister_handlers ();
1222 write_unlock (&handler_lock);
1227 reiserfs_xattr_unregister_handlers (void)
1229 write_lock (&handler_lock);
1230 __unregister_handlers ();
1231 write_unlock (&handler_lock);
1234 /* This will catch lookups from the fs root to .reiserfs_priv */
1236 xattr_lookup_poison (struct dentry *dentry, struct qstr *q1, struct qstr *name)
1238 struct dentry *priv_root = REISERFS_SB(dentry->d_sb)->priv_root;
1239 if (name->len == priv_root->d_name.len &&
1240 name->hash == priv_root->d_name.hash &&
1241 !memcmp (name->name, priv_root->d_name.name, name->len)) {
1247 static struct dentry_operations xattr_lookup_poison_ops = {
1248 .d_compare = xattr_lookup_poison,
1252 /* We need to take a copy of the mount flags since things like
1253 * MS_RDONLY don't get set until *after* we're called.
1254 * mount_flags != mount_options */
1256 reiserfs_xattr_init (struct super_block *s, int mount_flags)
1260 /* We need generation numbers to ensure that the oid mapping is correct
1261 * v3.5 filesystems don't have them. */
1262 if (!old_format_only (s)) {
1263 set_bit (REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
1264 } else if (reiserfs_xattrs_optional (s)) {
1265 /* Old format filesystem, but optional xattrs have been enabled
1266 * at mount time. Error out. */
1267 reiserfs_warning (s, "xattrs/ACLs not supported on pre v3.6 "
1268 "format filesystem. Failing mount.");
1272 /* Old format filesystem, but no optional xattrs have been enabled. This
1273 * means we silently disable xattrs on the filesystem. */
1274 clear_bit (REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
1277 /* If we don't have the privroot located yet - go find it */
1278 if (reiserfs_xattrs (s) && !REISERFS_SB(s)->priv_root) {
1279 struct dentry *dentry;
1280 dentry = lookup_one_len (PRIVROOT_NAME, s->s_root,
1281 strlen (PRIVROOT_NAME));
1282 if (!IS_ERR (dentry)) {
1283 if (!(mount_flags & MS_RDONLY) && !dentry->d_inode) {
1284 struct inode *inode = dentry->d_parent->d_inode;
1285 down (&inode->i_sem);
1286 err = inode->i_op->mkdir (inode, dentry, 0700);
1293 if (dentry && dentry->d_inode)
1294 reiserfs_warning (s, "Created %s on %s - reserved for "
1295 "xattr storage.", PRIVROOT_NAME,
1296 reiserfs_bdevname (inode->i_sb));
1297 } else if (!dentry->d_inode) {
1302 err = PTR_ERR (dentry);
1304 if (!err && dentry) {
1305 s->s_root->d_op = &xattr_lookup_poison_ops;
1306 REISERFS_I(dentry->d_inode)->i_flags |= i_priv_object;
1307 REISERFS_SB(s)->priv_root = dentry;
1308 } else if (!(mount_flags & MS_RDONLY)) { /* xattrs are unavailable */
1309 /* If we're read-only it just means that the dir hasn't been
1310 * created. Not an error -- just no xattrs on the fs. We'll
1311 * check again if we go read-write */
1312 reiserfs_warning (s, "xattrs/ACLs enabled and couldn't "
1313 "find/create .reiserfs_priv. Failing mount.");
1319 /* This is only nonzero if there was an error initializing the xattr
1320 * directory or if there is a condition where we don't support them. */
1322 clear_bit (REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
1323 clear_bit (REISERFS_XATTRS_USER, &(REISERFS_SB(s)->s_mount_opt));
1324 clear_bit (REISERFS_POSIXACL, &(REISERFS_SB(s)->s_mount_opt));
1327 /* The super_block MS_POSIXACL must mirror the (no)acl mount option. */
1328 s->s_flags = s->s_flags & ~MS_POSIXACL;
1329 if (reiserfs_posixacl (s))
1330 s->s_flags |= MS_POSIXACL;
1336 __reiserfs_permission (struct inode *inode, int mask, struct nameidata *nd,
1339 umode_t mode = inode->i_mode;
1341 if (mask & MAY_WRITE) {
1343 * Nobody gets write access to a read-only fs.
1345 if (IS_RDONLY(inode) &&
1346 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
1350 * Nobody gets write access to an immutable file.
1352 if (IS_IMMUTABLE(inode))
1356 /* We don't do permission checks on the internal objects.
1357 * Permissions are determined by the "owning" object. */
1358 if (is_reiserfs_priv_object (inode))
1361 if (current->fsuid == inode->i_uid) {
1363 #ifdef CONFIG_REISERFS_FS_POSIX_ACL
1364 } else if (reiserfs_posixacl(inode->i_sb) &&
1365 get_inode_sd_version (inode) != STAT_DATA_V1) {
1366 struct posix_acl *acl;
1368 /* ACL can't contain additional permissions if
1369 the ACL_MASK entry is 0 */
1370 if (!(mode & S_IRWXG))
1374 reiserfs_read_lock_xattr_i (inode);
1375 reiserfs_read_lock_xattrs (inode->i_sb);
1377 acl = reiserfs_get_acl (inode, ACL_TYPE_ACCESS);
1379 reiserfs_read_unlock_xattrs (inode->i_sb);
1380 reiserfs_read_unlock_xattr_i (inode);
1383 if (PTR_ERR (acl) == -ENODATA)
1385 return PTR_ERR (acl);
1389 int err = posix_acl_permission (inode, acl, mask);
1390 posix_acl_release (acl);
1391 if (err == -EACCES) {
1392 goto check_capabilities;
1401 if (in_group_p(inode->i_gid))
1406 * If the DACs are ok we don't need any capability check.
1408 if (((mode & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask))
1413 * Read/write DACs are always overridable.
1414 * Executable DACs are overridable if at least one exec bit is set.
1416 if (!(mask & MAY_EXEC) ||
1417 (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode))
1418 if (capable(CAP_DAC_OVERRIDE))
1422 * Searching includes executable on directories, else just read.
1424 if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
1425 if (capable(CAP_DAC_READ_SEARCH))
1432 reiserfs_permission (struct inode *inode, int mask, struct nameidata *nd)
1434 return __reiserfs_permission (inode, mask, nd, 1);
1438 reiserfs_permission_locked (struct inode *inode, int mask, struct nameidata *nd)
1440 return __reiserfs_permission (inode, mask, nd, 0);