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 <linux/vs_base.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 *prefix);
57 static struct dentry *
58 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));
68 if (IS_ERR (xaroot)) {
70 } else if (!xaroot->d_inode) {
72 down (&privroot->d_inode->i_sem);
73 err = privroot->d_inode->i_op->mkdir (privroot->d_inode, xaroot, 0700);
74 up (&privroot->d_inode->i_sem);
81 REISERFS_SB(sb)->xattr_root = dget (xaroot);
89 /* This will return a dentry, or error, refering to the xa root directory.
90 * If the xa root doesn't exist yet, the dentry will be returned without
91 * an associated inode. This dentry can be used with ->mkdir to create
92 * the xa directory. */
93 static struct dentry *
94 __get_xa_root (struct super_block *s)
96 struct dentry *privroot = dget (REISERFS_SB(s)->priv_root);
97 struct dentry *xaroot = NULL;
99 if (IS_ERR (privroot) || !privroot)
102 xaroot = lookup_one_len (XAROOT_NAME, privroot, strlen (XAROOT_NAME));
103 if (IS_ERR (xaroot)) {
105 } else if (!xaroot->d_inode) {
111 REISERFS_SB(s)->xattr_root = dget (xaroot);
118 /* Returns the dentry (or NULL) referring to the root of the extended
119 * attribute directory tree. If it has already been retreived, it is used.
120 * Otherwise, we attempt to retreive it from disk. It may also return
121 * a pointer-encoded error.
123 static inline struct dentry *
124 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 *
138 open_xa_dir (const struct inode *inode, int flags)
140 struct dentry *xaroot, *xadir;
143 xaroot = get_xa_root (inode->i_sb);
144 if (IS_ERR (xaroot)) {
146 } else if (!xaroot) {
147 if (flags == 0 || flags & XATTR_CREATE) {
148 xaroot = create_xa_root (inode->i_sb);
153 return ERR_PTR (-ENODATA);
156 /* ok, we have xaroot open */
158 snprintf (namebuf, sizeof (namebuf), "%X.%X",
159 le32_to_cpu (INODE_PKEY (inode)->k_objectid),
160 inode->i_generation);
161 xadir = lookup_one_len (namebuf, xaroot, strlen (namebuf));
162 if (IS_ERR (xadir)) {
167 if (!xadir->d_inode) {
169 if (flags == 0 || flags & XATTR_CREATE) {
170 /* Although there is nothing else trying to create this directory,
171 * another directory with the same hash may be created, so we need
172 * to protect against that */
173 err = xaroot->d_inode->i_op->mkdir (xaroot->d_inode, xadir, 0700);
177 return ERR_PTR (err);
180 if (!xadir->d_inode) {
183 return ERR_PTR (-ENODATA);
191 /* Returns a dentry corresponding to a specific extended attribute file
192 * for the inode. If flags allow, the file is created. Otherwise, a
193 * valid or negative dentry, or an error is returned. */
194 static struct dentry *
195 get_xa_file_dentry (const struct inode *inode, const char *name, int flags)
197 struct dentry *xadir, *xafile;
200 xadir = open_xa_dir (inode, flags);
201 if (IS_ERR (xadir)) {
202 return ERR_PTR (PTR_ERR (xadir));
203 } else if (xadir && !xadir->d_inode) {
205 return ERR_PTR (-ENODATA);
208 xafile = lookup_one_len (name, xadir, strlen (name));
209 if (IS_ERR (xafile)) {
211 return ERR_PTR (PTR_ERR (xafile));
214 if (xafile->d_inode) { /* file exists */
215 if (flags & XATTR_CREATE) {
220 } else if (flags & XATTR_REPLACE || flags & FL_READONLY) {
223 /* inode->i_sem is down, so nothing else can try to create
225 err = xadir->d_inode->i_op->create (xadir->d_inode, xafile,
237 xafile = ERR_PTR (err);
242 /* Opens a file pointer to the attribute associated with inode */
244 open_xa_file (const struct inode *inode, const char *name, int flags)
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 */
265 * this is very similar to fs/reiserfs/dir.c:reiserfs_readdir, but
266 * we need to drop the path before calling the filldir struct. That
267 * would be a big performance hit to the non-xattr case, so I've copied
268 * the whole thing for now. --clm
270 * the big difference is that I go backwards through the directory,
271 * and don't mess with f->f_pos, but the idea is the same. Do some
272 * action on each and every entry in the directory.
274 * we're called with i_sem held, so there are no worries about the directory
275 * changing underneath us.
277 static int __xattr_readdir(struct file * filp, void * dirent, filldir_t filldir)
279 struct inode *inode = filp->f_dentry->d_inode;
280 struct cpu_key pos_key; /* key of current position in the directory (key of directory entry) */
281 INITIALIZE_PATH (path_to_entry);
282 struct buffer_head * bh;
284 struct item_head * ih, tmp_ih;
288 char small_buf[32] ; /* avoid kmalloc if we can */
289 struct reiserfs_de_head *deh;
294 struct reiserfs_dir_entry de;
297 /* form key for search the next directory entry using f_pos field of
299 next_pos = max_reiserfs_offset(inode);
303 if (next_pos <= DOT_DOT_OFFSET)
305 make_cpu_key (&pos_key, inode, next_pos, TYPE_DIRENTRY, 3);
307 search_res = search_by_entry_key(inode->i_sb, &pos_key, &path_to_entry, &de);
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) &&
363 deh_objectid (deh) == le32_to_cpu (INODE_PKEY(REISERFS_SB(inode->i_sb)->priv_root->d_inode)->k_objectid))
366 if (d_reclen <= 32) {
367 local_buf = small_buf ;
369 local_buf = reiserfs_kmalloc(d_reclen, GFP_NOFS, inode->i_sb) ;
371 pathrelse (&path_to_entry);
374 if (item_moved (&tmp_ih, &path_to_entry)) {
375 reiserfs_kfree(local_buf, d_reclen, inode->i_sb) ;
377 /* sigh, must retry. Do this same offset again */
383 // Note, that we copy name to user space via temporary
384 // buffer (local_buf) because filldir will block if
385 // user space buffer is swapped out. At that time
386 // entry can move to somewhere else
387 memcpy (local_buf, d_name, d_reclen);
389 /* the filldir function might need to start transactions,
390 * or do who knows what. Release the path now that we've
391 * copied all the important stuff out of the deh
393 pathrelse (&path_to_entry);
395 if (filldir (dirent, local_buf, d_reclen, d_off, d_ino,
397 if (local_buf != small_buf) {
398 reiserfs_kfree(local_buf, d_reclen, inode->i_sb) ;
402 if (local_buf != small_buf) {
403 reiserfs_kfree(local_buf, d_reclen, inode->i_sb) ;
408 pathrelse (&path_to_entry);
413 * this could be done with dedicated readdir ops for the xattr files,
414 * but I want to get something working asap
415 * this is stolen from vfs_readdir
419 int xattr_readdir(struct file *file, filldir_t filler, void *buf)
421 struct inode *inode = file->f_dentry->d_inode;
423 if (!file->f_op || !file->f_op->readdir)
426 // down(&inode->i_zombie);
428 if (!IS_DEADDIR(inode)) {
430 res = __xattr_readdir(file, buf, filler);
433 // up(&inode->i_zombie);
440 /* Internal operations on file data */
442 reiserfs_put_page(struct page *page)
445 page_cache_release(page);
449 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->flags = (mapping->flags & ~__GFP_BITS_MASK) | GFP_NOFS;
456 page = read_cache_page (mapping, n,
457 (filler_t*)mapping->a_ops->readpage, NULL);
459 wait_on_page_locked(page);
461 if (!PageUptodate(page))
470 reiserfs_put_page(page);
471 return ERR_PTR(-EIO);
475 xattr_hash (const char *msg, int len)
477 return csum_partial (msg, len, 0);
480 /* Generic extended attribute operations that can be used by xa plugins */
486 reiserfs_xattr_set (struct inode *inode, const char *name, const void *buffer,
487 size_t buffer_size, int flags)
493 struct address_space *mapping;
495 size_t buffer_pos = 0;
496 struct inode *xinode;
497 struct iattr newattrs;
500 if (IS_RDONLY (inode))
503 if (IS_IMMUTABLE (inode) || IS_APPEND (inode))
506 if (get_inode_sd_version (inode) == STAT_DATA_V1)
509 /* Empty xattrs are ok, they're just empty files, no hash */
510 if (buffer && buffer_size)
511 xahash = xattr_hash (buffer, buffer_size);
514 fp = open_xa_file (inode, name, flags);
520 xinode = fp->f_dentry->d_inode;
521 REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
523 /* we need to copy it off.. */
524 if (xinode->i_nlink > 1) {
526 err = reiserfs_xattr_del (inode, name);
529 /* We just killed the old one, we're not replacing anymore */
530 if (flags & XATTR_REPLACE)
531 flags &= ~XATTR_REPLACE;
535 /* Resize it so we're ok to write there */
536 newattrs.ia_size = buffer_size;
537 newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
538 down (&xinode->i_sem);
539 err = notify_change(fp->f_dentry, &newattrs);
543 mapping = xinode->i_mapping;
544 while (buffer_pos < buffer_size || buffer_pos == 0) {
547 size_t page_offset = (file_pos & (PAGE_CACHE_SIZE - 1));
548 if (buffer_size - buffer_pos > PAGE_CACHE_SIZE)
549 chunk = PAGE_CACHE_SIZE;
551 chunk = buffer_size - buffer_pos;
553 page = reiserfs_get_page (xinode, file_pos >> PAGE_CACHE_SHIFT);
555 err = PTR_ERR (page);
560 data = page_address (page);
563 struct reiserfs_xattr_header *rxh;
564 skip = file_pos = sizeof (struct reiserfs_xattr_header);
565 if (chunk + skip > PAGE_CACHE_SIZE)
566 chunk = PAGE_CACHE_SIZE - skip;
567 rxh = (struct reiserfs_xattr_header *)data;
568 rxh->h_magic = cpu_to_le32 (REISERFS_XATTR_MAGIC);
569 rxh->h_hash = cpu_to_le32 (xahash);
572 err = mapping->a_ops->prepare_write (fp, page, page_offset,
573 page_offset + chunk + skip);
576 memcpy (data + skip, buffer + buffer_pos, chunk);
577 err = mapping->a_ops->commit_write (fp, page, page_offset,
578 page_offset + chunk + skip);
581 reiserfs_put_page (page);
585 if (err || buffer_size == 0 || !buffer)
589 /* We can't mark the inode dirty if it's not hashed. This is the case
590 * when we're inheriting the default ACL. If we dirty it, the inode
591 * gets marked dirty, but won't (ever) make it onto the dirty list until
592 * it's synced explicitly to clear I_DIRTY. This is bad. */
593 if (!hlist_unhashed(&inode->i_hash)) {
594 inode->i_ctime = CURRENT_TIME_SEC;
595 mark_inode_dirty (inode);
610 reiserfs_xattr_get (const struct inode *inode, const char *name, void *buffer,
617 size_t buffer_pos = 0;
619 struct inode *xinode;
625 /* We can't have xattrs attached to v1 items since they don't have
626 * generation numbers */
627 if (get_inode_sd_version (inode) == STAT_DATA_V1)
630 fp = open_xa_file (inode, name, FL_READONLY);
636 xinode = fp->f_dentry->d_inode;
637 isize = xinode->i_size;
638 REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
640 /* Just return the size needed */
641 if (buffer == NULL) {
642 err = isize - sizeof (struct reiserfs_xattr_header);
646 if (buffer_size < isize - sizeof (struct reiserfs_xattr_header)) {
651 while (file_pos < isize) {
655 if (isize - file_pos > PAGE_CACHE_SIZE)
656 chunk = PAGE_CACHE_SIZE;
658 chunk = isize - file_pos;
660 page = reiserfs_get_page (xinode, file_pos >> PAGE_CACHE_SHIFT);
662 err = PTR_ERR (page);
667 data = page_address (page);
669 struct reiserfs_xattr_header *rxh =
670 (struct reiserfs_xattr_header *)data;
671 skip = file_pos = sizeof (struct reiserfs_xattr_header);
673 /* Magic doesn't match up.. */
674 if (rxh->h_magic != cpu_to_le32 (REISERFS_XATTR_MAGIC)) {
676 reiserfs_put_page (page);
677 reiserfs_warning (inode->i_sb, "Invalid magic for xattr (%s) "
678 "associated with %k", name,
683 hash = le32_to_cpu (rxh->h_hash);
685 memcpy (buffer + buffer_pos, data + skip, chunk);
687 reiserfs_put_page (page);
692 err = isize - sizeof (struct reiserfs_xattr_header);
694 if (xattr_hash (buffer, isize - sizeof (struct reiserfs_xattr_header)) != hash) {
695 reiserfs_warning (inode->i_sb, "Invalid hash for xattr (%s) associated "
696 "with %k", name, INODE_PKEY (inode));
708 __reiserfs_xattr_del (struct dentry *xadir, const char *name, int namelen)
710 struct dentry *dentry;
711 struct inode *dir = xadir->d_inode;
714 dentry = lookup_one_len (name, xadir, namelen);
715 if (IS_ERR (dentry)) {
716 err = PTR_ERR (dentry);
718 } else if (!dentry->d_inode) {
723 /* Skip directories.. */
724 if (S_ISDIR (dentry->d_inode->i_mode))
727 if (!is_reiserfs_priv_object (dentry->d_inode)) {
728 reiserfs_warning (dir->i_sb, "OID %08x [%.*s/%.*s] doesn't have "
729 "priv flag set [parent is %sset].",
730 le32_to_cpu (INODE_PKEY (dentry->d_inode)->k_objectid),
731 xadir->d_name.len, xadir->d_name.name, namelen, name,
732 is_reiserfs_priv_object (xadir->d_inode) ? "" : "not ");
737 err = dir->i_op->unlink (dir, dentry);
750 reiserfs_xattr_del (struct inode *inode, const char *name)
755 if (IS_RDONLY (inode))
758 dir = open_xa_dir (inode, FL_READONLY);
764 err = __reiserfs_xattr_del (dir, name, strlen (name));
768 inode->i_ctime = CURRENT_TIME_SEC;
769 mark_inode_dirty (inode);
776 /* The following are side effects of other operations that aren't explicitly
777 * modifying extended attributes. This includes operations such as permissions
778 * or ownership changes, object deletions, etc. */
781 reiserfs_delete_xattrs_filler (void *buf, const char *name, int namelen,
782 loff_t offset, ino_t ino, unsigned int d_type)
784 struct dentry *xadir = (struct dentry *)buf;
786 return __reiserfs_xattr_del (xadir, name, namelen);
790 /* This is called w/ inode->i_sem downed */
792 reiserfs_delete_xattrs (struct inode *inode)
795 struct dentry *dir, *root;
798 /* Skip out, an xattr has no xattrs associated with it */
799 if (is_reiserfs_priv_object (inode) ||
800 get_inode_sd_version (inode) == STAT_DATA_V1 ||
801 !reiserfs_xattrs(inode->i_sb))
805 reiserfs_read_lock_xattrs (inode->i_sb);
806 dir = open_xa_dir (inode, FL_READONLY);
807 reiserfs_read_unlock_xattrs (inode->i_sb);
811 } else if (!dir->d_inode) {
816 fp = dentry_open (dir, NULL, O_RDWR);
819 /* dentry_open dputs the dentry if it fails */
824 err = xattr_readdir (fp, reiserfs_delete_xattrs_filler, dir);
830 /* Leftovers besides . and .. -- that's not good. */
831 if (dir->d_inode->i_nlink <= 2) {
832 root = get_xa_root (inode->i_sb);
833 reiserfs_write_lock_xattrs (inode->i_sb);
834 err = vfs_rmdir (root->d_inode, dir, NULL);
835 reiserfs_write_unlock_xattrs (inode->i_sb);
838 reiserfs_warning (inode->i_sb,
839 "Couldn't remove all entries in directory");
848 REISERFS_I(inode)->i_flags = REISERFS_I(inode)->i_flags & ~i_has_xattr_dir;
852 struct reiserfs_chown_buf {
854 struct dentry *xadir;
858 /* XXX: If there is a better way to do this, I'd love to hear about it */
860 reiserfs_chown_xattrs_filler (void *buf, const char *name, int namelen,
861 loff_t offset, ino_t ino, unsigned int d_type)
863 struct reiserfs_chown_buf *chown_buf = (struct reiserfs_chown_buf *)buf;
864 struct dentry *xafile, *xadir = chown_buf->xadir;
865 struct iattr *attrs = chown_buf->attrs;
868 xafile = lookup_one_len (name, xadir, namelen);
870 return PTR_ERR (xafile);
871 else if (!xafile->d_inode) {
876 if (!S_ISDIR (xafile->d_inode->i_mode))
877 err = notify_change (xafile, attrs);
884 reiserfs_chown_xattrs (struct inode *inode, struct iattr *attrs)
889 struct reiserfs_chown_buf buf;
890 unsigned int ia_valid = attrs->ia_valid;
892 /* Skip out, an xattr has no xattrs associated with it */
893 if (is_reiserfs_priv_object (inode) ||
894 get_inode_sd_version (inode) == STAT_DATA_V1 ||
895 !reiserfs_xattrs(inode->i_sb))
899 reiserfs_read_lock_xattrs (inode->i_sb);
900 dir = open_xa_dir (inode, FL_READONLY);
901 reiserfs_read_unlock_xattrs (inode->i_sb);
903 if (PTR_ERR (dir) != -ENODATA)
906 } else if (!dir->d_inode) {
911 fp = dentry_open (dir, NULL, O_RDWR);
914 /* dentry_open dputs the dentry if it fails */
920 attrs->ia_valid &= (ATTR_UID | ATTR_GID | ATTR_CTIME);
925 err = xattr_readdir (fp, reiserfs_chown_xattrs_filler, &buf);
931 err = notify_change (dir, attrs);
938 attrs->ia_valid = ia_valid;
943 /* Actual operations that are exported to VFS-land */
946 * Inode operation getxattr()
947 * Preliminary locking: we down dentry->d_inode->i_sem
950 reiserfs_getxattr (struct dentry *dentry, const char *name, void *buffer,
953 struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix (name);
956 if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
957 get_inode_sd_version (dentry->d_inode) == STAT_DATA_V1)
960 reiserfs_read_lock_xattr_i (dentry->d_inode);
961 reiserfs_read_lock_xattrs (dentry->d_sb);
962 err = xah->get (dentry->d_inode, name, buffer, size);
963 reiserfs_read_unlock_xattrs (dentry->d_sb);
964 reiserfs_read_unlock_xattr_i (dentry->d_inode);
970 * Inode operation setxattr()
972 * dentry->d_inode->i_sem down
975 reiserfs_setxattr (struct dentry *dentry, const char *name, const void *value,
976 size_t size, int flags)
978 struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix (name);
982 if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
983 get_inode_sd_version (dentry->d_inode) == STAT_DATA_V1)
986 if (IS_RDONLY (dentry->d_inode))
989 if (IS_IMMUTABLE (dentry->d_inode) || IS_APPEND (dentry->d_inode))
992 reiserfs_write_lock_xattr_i (dentry->d_inode);
993 lock = !has_xattr_dir (dentry->d_inode);
995 reiserfs_write_lock_xattrs (dentry->d_sb);
997 reiserfs_read_lock_xattrs (dentry->d_sb);
998 err = xah->set (dentry->d_inode, name, value, size, flags);
1000 reiserfs_write_unlock_xattrs (dentry->d_sb);
1002 reiserfs_read_unlock_xattrs (dentry->d_sb);
1003 reiserfs_write_unlock_xattr_i (dentry->d_inode);
1008 * Inode operation removexattr()
1010 * dentry->d_inode->i_sem down
1013 reiserfs_removexattr (struct dentry *dentry, const char *name)
1016 struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix (name);
1018 if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
1019 get_inode_sd_version (dentry->d_inode) == STAT_DATA_V1)
1022 if (IS_RDONLY (dentry->d_inode))
1025 if (IS_IMMUTABLE (dentry->d_inode) || IS_APPEND (dentry->d_inode))
1028 reiserfs_write_lock_xattr_i (dentry->d_inode);
1029 reiserfs_read_lock_xattrs (dentry->d_sb);
1031 /* Deletion pre-operation */
1033 err = xah->del (dentry->d_inode, name);
1038 err = reiserfs_xattr_del (dentry->d_inode, name);
1040 dentry->d_inode->i_ctime = CURRENT_TIME_SEC;
1041 mark_inode_dirty (dentry->d_inode);
1044 reiserfs_read_unlock_xattrs (dentry->d_sb);
1045 reiserfs_write_unlock_xattr_i (dentry->d_inode);
1050 /* This is what filldir will use:
1051 * r_pos will always contain the amount of space required for the entire
1052 * list. If r_pos becomes larger than r_size, we need more space and we
1053 * return an error indicating this. If r_pos is less than r_size, then we've
1054 * filled the buffer successfully and we return success */
1055 struct reiserfs_listxattr_buf {
1059 struct inode *r_inode;
1063 reiserfs_listxattr_filler (void *buf, const char *name, int namelen,
1064 loff_t offset, ino_t ino, unsigned int d_type)
1066 struct reiserfs_listxattr_buf *b = (struct reiserfs_listxattr_buf *)buf;
1068 if (name[0] != '.' || (namelen != 1 && (name[1] != '.' || namelen != 2))) {
1069 struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix (name);
1070 if (!xah) return 0; /* Unsupported xattr name, skip it */
1072 /* We call ->list() twice because the operation isn't required to just
1073 * return the name back - we want to make sure we have enough space */
1074 len += xah->list (b->r_inode, name, namelen, NULL);
1077 if (b->r_pos + len + 1 <= b->r_size) {
1078 char *p = b->r_buf + b->r_pos;
1079 p += xah->list (b->r_inode, name, namelen, p);
1082 b->r_pos += len + 1;
1089 * Inode operation listxattr()
1091 * Preliminary locking: we down dentry->d_inode->i_sem
1094 reiserfs_listxattr (struct dentry *dentry, char *buffer, size_t size)
1099 struct reiserfs_listxattr_buf buf;
1101 if (!dentry->d_inode)
1104 if (!reiserfs_xattrs(dentry->d_sb) ||
1105 get_inode_sd_version (dentry->d_inode) == STAT_DATA_V1)
1108 reiserfs_read_lock_xattr_i (dentry->d_inode);
1109 reiserfs_read_lock_xattrs (dentry->d_sb);
1110 dir = open_xa_dir (dentry->d_inode, FL_READONLY);
1111 reiserfs_read_unlock_xattrs (dentry->d_sb);
1113 err = PTR_ERR (dir);
1114 if (err == -ENODATA)
1115 err = 0; /* Not an error if there aren't any xattrs */
1119 fp = dentry_open (dir, NULL, O_RDWR);
1122 /* dentry_open dputs the dentry if it fails */
1127 buf.r_size = buffer ? size : 0;
1129 buf.r_inode = dentry->d_inode;
1131 REISERFS_I(dentry->d_inode)->i_flags |= i_has_xattr_dir;
1133 err = xattr_readdir (fp, reiserfs_listxattr_filler, &buf);
1137 if (buf.r_pos > buf.r_size && buffer != NULL)
1146 reiserfs_read_unlock_xattr_i (dentry->d_inode);
1150 /* This is the implementation for the xattr plugin infrastructure */
1151 static struct list_head xattr_handlers = LIST_HEAD_INIT (xattr_handlers);
1152 static DEFINE_RWLOCK(handler_lock);
1154 static struct reiserfs_xattr_handler *
1155 find_xattr_handler_prefix (const char *prefix)
1157 struct reiserfs_xattr_handler *xah = NULL;
1158 struct list_head *p;
1160 read_lock (&handler_lock);
1161 list_for_each (p, &xattr_handlers) {
1162 xah = list_entry (p, struct reiserfs_xattr_handler, handlers);
1163 if (strncmp (xah->prefix, prefix, strlen (xah->prefix)) == 0)
1168 read_unlock (&handler_lock);
1173 __unregister_handlers (void)
1175 struct reiserfs_xattr_handler *xah;
1176 struct list_head *p, *tmp;
1178 list_for_each_safe (p, tmp, &xattr_handlers) {
1179 xah = list_entry (p, struct reiserfs_xattr_handler, handlers);
1185 INIT_LIST_HEAD (&xattr_handlers);
1189 reiserfs_xattr_register_handlers (void)
1192 struct reiserfs_xattr_handler *xah;
1193 struct list_head *p;
1195 write_lock (&handler_lock);
1197 /* If we're already initialized, nothing to do */
1198 if (!list_empty (&xattr_handlers)) {
1199 write_unlock (&handler_lock);
1203 /* Add the handlers */
1204 list_add_tail (&user_handler.handlers, &xattr_handlers);
1205 list_add_tail (&trusted_handler.handlers, &xattr_handlers);
1206 #ifdef CONFIG_REISERFS_FS_SECURITY
1207 list_add_tail (&security_handler.handlers, &xattr_handlers);
1209 #ifdef CONFIG_REISERFS_FS_POSIX_ACL
1210 list_add_tail (&posix_acl_access_handler.handlers, &xattr_handlers);
1211 list_add_tail (&posix_acl_default_handler.handlers, &xattr_handlers);
1214 /* Run initializers, if available */
1215 list_for_each (p, &xattr_handlers) {
1216 xah = list_entry (p, struct reiserfs_xattr_handler, handlers);
1226 /* Clean up other handlers, if any failed */
1228 __unregister_handlers ();
1230 write_unlock (&handler_lock);
1235 reiserfs_xattr_unregister_handlers (void)
1237 write_lock (&handler_lock);
1238 __unregister_handlers ();
1239 write_unlock (&handler_lock);
1242 /* This will catch lookups from the fs root to .reiserfs_priv */
1244 xattr_lookup_poison (struct dentry *dentry, struct qstr *q1, struct qstr *name)
1246 struct dentry *priv_root = REISERFS_SB(dentry->d_sb)->priv_root;
1247 if (name->len == priv_root->d_name.len &&
1248 name->hash == priv_root->d_name.hash &&
1249 !memcmp (name->name, priv_root->d_name.name, name->len)) {
1251 } else if (q1->len == name->len &&
1252 !memcmp(q1->name, name->name, name->len))
1257 static struct dentry_operations xattr_lookup_poison_ops = {
1258 .d_compare = xattr_lookup_poison,
1262 /* We need to take a copy of the mount flags since things like
1263 * MS_RDONLY don't get set until *after* we're called.
1264 * mount_flags != mount_options */
1266 reiserfs_xattr_init (struct super_block *s, int mount_flags)
1270 /* We need generation numbers to ensure that the oid mapping is correct
1271 * v3.5 filesystems don't have them. */
1272 if (!old_format_only (s)) {
1273 set_bit (REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
1274 } else if (reiserfs_xattrs_optional (s)) {
1275 /* Old format filesystem, but optional xattrs have been enabled
1276 * at mount time. Error out. */
1277 reiserfs_warning (s, "xattrs/ACLs not supported on pre v3.6 "
1278 "format filesystem. Failing mount.");
1282 /* Old format filesystem, but no optional xattrs have been enabled. This
1283 * means we silently disable xattrs on the filesystem. */
1284 clear_bit (REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
1287 /* If we don't have the privroot located yet - go find it */
1288 if (reiserfs_xattrs (s) && !REISERFS_SB(s)->priv_root) {
1289 struct dentry *dentry;
1290 dentry = lookup_one_len (PRIVROOT_NAME, s->s_root,
1291 strlen (PRIVROOT_NAME));
1292 if (!IS_ERR (dentry)) {
1293 if (!(mount_flags & MS_RDONLY) && !dentry->d_inode) {
1294 struct inode *inode = dentry->d_parent->d_inode;
1295 down (&inode->i_sem);
1296 err = inode->i_op->mkdir (inode, dentry, 0700);
1303 if (dentry && dentry->d_inode)
1304 reiserfs_warning (s, "Created %s on %s - reserved for "
1305 "xattr storage.", PRIVROOT_NAME,
1306 reiserfs_bdevname (inode->i_sb));
1307 } else if (!dentry->d_inode) {
1312 err = PTR_ERR (dentry);
1314 if (!err && dentry) {
1315 s->s_root->d_op = &xattr_lookup_poison_ops;
1316 reiserfs_mark_inode_private (dentry->d_inode);
1317 REISERFS_SB(s)->priv_root = dentry;
1318 } else if (!(mount_flags & MS_RDONLY)) { /* xattrs are unavailable */
1319 /* If we're read-only it just means that the dir hasn't been
1320 * created. Not an error -- just no xattrs on the fs. We'll
1321 * check again if we go read-write */
1322 reiserfs_warning (s, "xattrs/ACLs enabled and couldn't "
1323 "find/create .reiserfs_priv. Failing mount.");
1329 /* This is only nonzero if there was an error initializing the xattr
1330 * directory or if there is a condition where we don't support them. */
1332 clear_bit (REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
1333 clear_bit (REISERFS_XATTRS_USER, &(REISERFS_SB(s)->s_mount_opt));
1334 clear_bit (REISERFS_POSIXACL, &(REISERFS_SB(s)->s_mount_opt));
1337 /* The super_block MS_POSIXACL must mirror the (no)acl mount option. */
1338 s->s_flags = s->s_flags & ~MS_POSIXACL;
1339 if (reiserfs_posixacl (s))
1340 s->s_flags |= MS_POSIXACL;
1346 __reiserfs_permission (struct inode *inode, int mask, struct nameidata *nd,
1349 umode_t mode = inode->i_mode;
1351 if (mask & MAY_WRITE) {
1353 * Nobody gets write access to a read-only fs.
1355 if ((IS_RDONLY(inode) || (nd && MNT_IS_RDONLY(nd->mnt))) &&
1356 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
1360 * Nobody gets write access to an immutable file.
1362 if (IS_IMMUTABLE(inode))
1366 /* We don't do permission checks on the internal objects.
1367 * Permissions are determined by the "owning" object. */
1368 if (is_reiserfs_priv_object (inode))
1371 if (current->fsuid == inode->i_uid) {
1373 #ifdef CONFIG_REISERFS_FS_POSIX_ACL
1374 } else if (reiserfs_posixacl(inode->i_sb) &&
1375 get_inode_sd_version (inode) != STAT_DATA_V1) {
1376 struct posix_acl *acl;
1378 /* ACL can't contain additional permissions if
1379 the ACL_MASK entry is 0 */
1380 if (!(mode & S_IRWXG))
1384 reiserfs_read_lock_xattr_i (inode);
1385 reiserfs_read_lock_xattrs (inode->i_sb);
1387 acl = reiserfs_get_acl (inode, ACL_TYPE_ACCESS);
1389 reiserfs_read_unlock_xattrs (inode->i_sb);
1390 reiserfs_read_unlock_xattr_i (inode);
1393 if (PTR_ERR (acl) == -ENODATA)
1395 return PTR_ERR (acl);
1399 int err = posix_acl_permission (inode, acl, mask);
1400 posix_acl_release (acl);
1401 if (err == -EACCES) {
1402 goto check_capabilities;
1410 #ifdef CONFIG_REISERFS_FS_POSIX_ACL
1413 if (in_group_p(inode->i_gid))
1418 * If the DACs are ok we don't need any capability check.
1420 if (((mode & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask))
1423 #ifdef CONFIG_REISERFS_FS_POSIX_ACL
1427 * Read/write DACs are always overridable.
1428 * Executable DACs are overridable if at least one exec bit is set.
1430 if (!(mask & MAY_EXEC) ||
1431 (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode))
1432 if (capable(CAP_DAC_OVERRIDE))
1436 * Searching includes executable on directories, else just read.
1438 if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
1439 if (capable(CAP_DAC_READ_SEARCH))
1446 reiserfs_permission (struct inode *inode, int mask, struct nameidata *nd)
1448 return __reiserfs_permission (inode, mask, nd, 1);
1452 reiserfs_permission_locked (struct inode *inode, int mask, struct nameidata *nd)
1454 return __reiserfs_permission (inode, mask, nd, 0);