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);
185 /* Newly created object.. Need to mark it private */
186 REISERFS_I(xadir->d_inode)->i_flags |= i_priv_object;
193 /* Returns a dentry corresponding to a specific extended attribute file
194 * for the inode. If flags allow, the file is created. Otherwise, a
195 * valid or negative dentry, or an error is returned. */
196 static struct dentry *
197 get_xa_file_dentry (const struct inode *inode, const char *name, int flags)
199 struct dentry *xadir, *xafile;
202 xadir = open_xa_dir (inode, flags);
203 if (IS_ERR (xadir)) {
204 return ERR_PTR (PTR_ERR (xadir));
205 } else if (xadir && !xadir->d_inode) {
207 return ERR_PTR (-ENODATA);
210 xafile = lookup_one_len (name, xadir, strlen (name));
211 if (IS_ERR (xafile)) {
213 return ERR_PTR (PTR_ERR (xafile));
216 if (xafile->d_inode) { /* file exists */
217 if (flags & XATTR_CREATE) {
222 } else if (flags & XATTR_REPLACE || flags & FL_READONLY) {
225 /* inode->i_sem is down, so nothing else can try to create
227 err = xadir->d_inode->i_op->create (xadir->d_inode, xafile,
234 /* Newly created object.. Need to mark it private */
235 REISERFS_I(xafile->d_inode)->i_flags |= i_priv_object;
241 xafile = ERR_PTR (err);
246 /* Opens a file pointer to the attribute associated with inode */
248 open_xa_file (const struct inode *inode, const char *name, int flags)
250 struct dentry *xafile;
253 xafile = get_xa_file_dentry (inode, name, flags);
255 return ERR_PTR (PTR_ERR (xafile));
256 else if (!xafile->d_inode) {
258 return ERR_PTR (-ENODATA);
261 fp = dentry_open (xafile, NULL, O_RDWR);
262 /* dentry_open dputs the dentry if it fails */
269 * this is very similar to fs/reiserfs/dir.c:reiserfs_readdir, but
270 * we need to drop the path before calling the filldir struct. That
271 * would be a big performance hit to the non-xattr case, so I've copied
272 * the whole thing for now. --clm
274 * the big difference is that I go backwards through the directory,
275 * and don't mess with f->f_pos, but the idea is the same. Do some
276 * action on each and every entry in the directory.
278 * we're called with i_sem held, so there are no worries about the directory
279 * changing underneath us.
281 static int __xattr_readdir(struct file * filp, void * dirent, filldir_t filldir)
283 struct inode *inode = filp->f_dentry->d_inode;
284 struct cpu_key pos_key; /* key of current position in the directory (key of directory entry) */
285 INITIALIZE_PATH (path_to_entry);
286 struct buffer_head * bh;
288 struct item_head * ih, tmp_ih;
292 char small_buf[32] ; /* avoid kmalloc if we can */
293 struct reiserfs_de_head *deh;
298 struct reiserfs_dir_entry de;
301 /* form key for search the next directory entry using f_pos field of
303 next_pos = max_reiserfs_offset(inode);
307 if (next_pos <= DOT_DOT_OFFSET)
309 make_cpu_key (&pos_key, inode, next_pos, TYPE_DIRENTRY, 3);
311 search_res = search_by_entry_key(inode->i_sb, &pos_key, &path_to_entry, &de);
312 if (search_res == IO_ERROR) {
313 // FIXME: we could just skip part of directory which could
315 pathrelse(&path_to_entry);
319 if (search_res == NAME_NOT_FOUND)
322 set_de_name_and_namelen(&de);
323 entry_num = de.de_entry_num;
324 deh = &(de.de_deh[entry_num]);
329 if (!is_direntry_le_ih(ih)) {
330 reiserfs_warning(inode->i_sb, "not direntry %h", ih);
333 copy_item_head(&tmp_ih, ih);
335 /* we must have found item, that is item of this directory, */
336 RFALSE( COMP_SHORT_KEYS (&(ih->ih_key), &pos_key),
337 "vs-9000: found item %h does not match to dir we readdir %K",
340 if (deh_offset(deh) <= DOT_DOT_OFFSET) {
344 /* look for the previous entry in the directory */
345 next_pos = deh_offset (deh) - 1;
347 if (!de_visible (deh))
348 /* it is hidden entry */
351 d_reclen = entry_length(bh, ih, entry_num);
352 d_name = B_I_DEH_ENTRY_FILE_NAME (bh, ih, deh);
353 d_off = deh_offset (deh);
354 d_ino = deh_objectid (deh);
356 if (!d_name[d_reclen - 1])
357 d_reclen = strlen (d_name);
359 if (d_reclen > REISERFS_MAX_NAME(inode->i_sb->s_blocksize)){
360 /* too big to send back to VFS */
364 /* Ignore the .reiserfs_priv entry */
365 if (reiserfs_xattrs (inode->i_sb) &&
366 !old_format_only(inode->i_sb) &&
367 deh_objectid (deh) == le32_to_cpu (INODE_PKEY(REISERFS_SB(inode->i_sb)->priv_root->d_inode)->k_objectid))
370 if (d_reclen <= 32) {
371 local_buf = small_buf ;
373 local_buf = reiserfs_kmalloc(d_reclen, GFP_NOFS, inode->i_sb) ;
375 pathrelse (&path_to_entry);
378 if (item_moved (&tmp_ih, &path_to_entry)) {
379 reiserfs_kfree(local_buf, d_reclen, inode->i_sb) ;
381 /* sigh, must retry. Do this same offset again */
387 // Note, that we copy name to user space via temporary
388 // buffer (local_buf) because filldir will block if
389 // user space buffer is swapped out. At that time
390 // entry can move to somewhere else
391 memcpy (local_buf, d_name, d_reclen);
393 /* the filldir function might need to start transactions,
394 * or do who knows what. Release the path now that we've
395 * copied all the important stuff out of the deh
397 pathrelse (&path_to_entry);
399 if (filldir (dirent, local_buf, d_reclen, d_off, d_ino,
401 if (local_buf != small_buf) {
402 reiserfs_kfree(local_buf, d_reclen, inode->i_sb) ;
406 if (local_buf != small_buf) {
407 reiserfs_kfree(local_buf, d_reclen, inode->i_sb) ;
412 pathrelse (&path_to_entry);
417 * this could be done with dedicated readdir ops for the xattr files,
418 * but I want to get something working asap
419 * this is stolen from vfs_readdir
423 int xattr_readdir(struct file *file, filldir_t filler, void *buf)
425 struct inode *inode = file->f_dentry->d_inode;
427 if (!file->f_op || !file->f_op->readdir)
430 // down(&inode->i_zombie);
432 if (!IS_DEADDIR(inode)) {
434 res = __xattr_readdir(file, buf, filler);
437 // up(&inode->i_zombie);
444 /* Internal operations on file data */
446 reiserfs_put_page(struct page *page)
449 page_cache_release(page);
453 reiserfs_get_page(struct inode *dir, unsigned long n)
455 struct address_space *mapping = dir->i_mapping;
457 /* We can deadlock if we try to free dentries,
458 and an unlink/rmdir has just occured - GFP_NOFS avoids this */
459 mapping->flags = (mapping->flags & ~__GFP_BITS_MASK) | GFP_NOFS;
460 page = read_cache_page (mapping, n,
461 (filler_t*)mapping->a_ops->readpage, NULL);
463 wait_on_page_locked(page);
465 if (!PageUptodate(page))
474 reiserfs_put_page(page);
475 return ERR_PTR(-EIO);
479 xattr_hash (const char *msg, int len)
481 return csum_partial (msg, len, 0);
484 /* Generic extended attribute operations that can be used by xa plugins */
490 reiserfs_xattr_set (struct inode *inode, const char *name, const void *buffer,
491 size_t buffer_size, int flags)
497 struct address_space *mapping;
499 size_t buffer_pos = 0;
500 struct inode *xinode;
501 struct iattr newattrs;
504 if (IS_RDONLY (inode))
507 if (IS_IMMUTABLE (inode) || IS_APPEND (inode))
510 if (get_inode_sd_version (inode) == STAT_DATA_V1)
513 /* Empty xattrs are ok, they're just empty files, no hash */
514 if (buffer && buffer_size)
515 xahash = xattr_hash (buffer, buffer_size);
518 fp = open_xa_file (inode, name, flags);
524 xinode = fp->f_dentry->d_inode;
525 REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
527 /* we need to copy it off.. */
528 if (xinode->i_nlink > 1) {
530 err = reiserfs_xattr_del (inode, name);
533 /* We just killed the old one, we're not replacing anymore */
534 if (flags & XATTR_REPLACE)
535 flags &= ~XATTR_REPLACE;
539 /* Resize it so we're ok to write there */
540 newattrs.ia_size = buffer_size;
541 newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
542 down (&xinode->i_sem);
543 err = notify_change(fp->f_dentry, &newattrs);
547 mapping = xinode->i_mapping;
548 while (buffer_pos < buffer_size || buffer_pos == 0) {
551 size_t page_offset = (file_pos & (PAGE_CACHE_SIZE - 1));
552 if (buffer_size - buffer_pos > PAGE_CACHE_SIZE)
553 chunk = PAGE_CACHE_SIZE;
555 chunk = buffer_size - buffer_pos;
557 page = reiserfs_get_page (xinode, file_pos >> PAGE_CACHE_SHIFT);
559 err = PTR_ERR (page);
564 data = page_address (page);
567 struct reiserfs_xattr_header *rxh;
568 skip = file_pos = sizeof (struct reiserfs_xattr_header);
569 if (chunk + skip > PAGE_CACHE_SIZE)
570 chunk = PAGE_CACHE_SIZE - skip;
571 rxh = (struct reiserfs_xattr_header *)data;
572 rxh->h_magic = cpu_to_le32 (REISERFS_XATTR_MAGIC);
573 rxh->h_hash = cpu_to_le32 (xahash);
576 err = mapping->a_ops->prepare_write (fp, page, page_offset,
577 page_offset + chunk + skip);
580 memcpy (data + skip, buffer + buffer_pos, chunk);
581 err = mapping->a_ops->commit_write (fp, page, page_offset,
582 page_offset + chunk + skip);
585 reiserfs_put_page (page);
589 if (err || buffer_size == 0 || !buffer)
593 /* We can't mark the inode dirty if it's not hashed. This is the case
594 * when we're inheriting the default ACL. If we dirty it, the inode
595 * gets marked dirty, but won't (ever) make it onto the dirty list until
596 * it's synced explicitly to clear I_DIRTY. This is bad. */
597 if (!hlist_unhashed(&inode->i_hash)) {
598 inode->i_ctime = CURRENT_TIME;
599 mark_inode_dirty (inode);
614 reiserfs_xattr_get (const struct inode *inode, const char *name, void *buffer,
621 size_t buffer_pos = 0;
623 struct inode *xinode;
629 /* We can't have xattrs attached to v1 items since they don't have
630 * generation numbers */
631 if (get_inode_sd_version (inode) == STAT_DATA_V1)
634 fp = open_xa_file (inode, name, FL_READONLY);
640 xinode = fp->f_dentry->d_inode;
641 isize = xinode->i_size;
642 REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
644 /* Just return the size needed */
645 if (buffer == NULL) {
646 err = isize - sizeof (struct reiserfs_xattr_header);
650 if (buffer_size < isize - sizeof (struct reiserfs_xattr_header)) {
655 while (file_pos < isize) {
659 if (isize - file_pos > PAGE_CACHE_SIZE)
660 chunk = PAGE_CACHE_SIZE;
662 chunk = isize - file_pos;
664 page = reiserfs_get_page (xinode, file_pos >> PAGE_CACHE_SHIFT);
666 err = PTR_ERR (page);
671 data = page_address (page);
673 struct reiserfs_xattr_header *rxh =
674 (struct reiserfs_xattr_header *)data;
675 skip = file_pos = sizeof (struct reiserfs_xattr_header);
677 /* Magic doesn't match up.. */
678 if (rxh->h_magic != cpu_to_le32 (REISERFS_XATTR_MAGIC)) {
680 reiserfs_put_page (page);
681 reiserfs_warning (inode->i_sb, "Invalid magic for xattr (%s) "
682 "associated with %k", name,
687 hash = le32_to_cpu (rxh->h_hash);
689 memcpy (buffer + buffer_pos, data + skip, chunk);
691 reiserfs_put_page (page);
696 err = isize - sizeof (struct reiserfs_xattr_header);
698 if (xattr_hash (buffer, isize - sizeof (struct reiserfs_xattr_header)) != hash) {
699 reiserfs_warning (inode->i_sb, "Invalid hash for xattr (%s) associated "
700 "with %k", name, INODE_PKEY (inode));
712 __reiserfs_xattr_del (struct dentry *xadir, const char *name, int namelen)
714 struct dentry *dentry;
715 struct inode *dir = xadir->d_inode;
718 dentry = lookup_one_len (name, xadir, namelen);
719 if (IS_ERR (dentry)) {
720 err = PTR_ERR (dentry);
722 } else if (!dentry->d_inode) {
727 /* Skip directories.. */
728 if (S_ISDIR (dentry->d_inode->i_mode))
731 if (!is_reiserfs_priv_object (dentry->d_inode)) {
732 reiserfs_warning (dir->i_sb, "OID %08x [%.*s/%.*s] doesn't have "
733 "priv flag set [parent is %sset].",
734 le32_to_cpu (INODE_PKEY (dentry->d_inode)->k_objectid),
735 xadir->d_name.len, xadir->d_name.name, namelen, name,
736 is_reiserfs_priv_object (xadir->d_inode) ? "" : "not ");
741 err = dir->i_op->unlink (dir, dentry);
754 reiserfs_xattr_del (struct inode *inode, const char *name)
759 if (IS_RDONLY (inode))
762 dir = open_xa_dir (inode, FL_READONLY);
768 err = __reiserfs_xattr_del (dir, name, strlen (name));
772 inode->i_ctime = CURRENT_TIME;
773 mark_inode_dirty (inode);
780 /* The following are side effects of other operations that aren't explicitly
781 * modifying extended attributes. This includes operations such as permissions
782 * or ownership changes, object deletions, etc. */
785 reiserfs_delete_xattrs_filler (void *buf, const char *name, int namelen,
786 loff_t offset, ino_t ino, unsigned int d_type)
788 struct dentry *xadir = (struct dentry *)buf;
790 return __reiserfs_xattr_del (xadir, name, namelen);
794 /* This is called w/ inode->i_sem downed */
796 reiserfs_delete_xattrs (struct inode *inode)
799 struct dentry *dir, *root;
802 /* Skip out, an xattr has no xattrs associated with it */
803 if (is_reiserfs_priv_object (inode) ||
804 get_inode_sd_version (inode) == STAT_DATA_V1 ||
805 !reiserfs_xattrs(inode->i_sb))
809 reiserfs_read_lock_xattrs (inode->i_sb);
810 dir = open_xa_dir (inode, FL_READONLY);
811 reiserfs_read_unlock_xattrs (inode->i_sb);
815 } else if (!dir->d_inode) {
820 fp = dentry_open (dir, NULL, O_RDWR);
823 /* dentry_open dputs the dentry if it fails */
828 err = xattr_readdir (fp, reiserfs_delete_xattrs_filler, dir);
834 /* Leftovers besides . and .. -- that's not good. */
835 if (dir->d_inode->i_nlink <= 2) {
836 root = get_xa_root (inode->i_sb);
837 reiserfs_write_lock_xattrs (inode->i_sb);
838 err = vfs_rmdir (root->d_inode, dir);
839 reiserfs_write_unlock_xattrs (inode->i_sb);
842 reiserfs_warning (inode->i_sb,
843 "Couldn't remove all entries in directory");
852 REISERFS_I(inode)->i_flags = REISERFS_I(inode)->i_flags & ~i_has_xattr_dir;
856 struct reiserfs_chown_buf {
858 struct dentry *xadir;
862 /* XXX: If there is a better way to do this, I'd love to hear about it */
864 reiserfs_chown_xattrs_filler (void *buf, const char *name, int namelen,
865 loff_t offset, ino_t ino, unsigned int d_type)
867 struct reiserfs_chown_buf *chown_buf = (struct reiserfs_chown_buf *)buf;
868 struct dentry *xafile, *xadir = chown_buf->xadir;
869 struct iattr *attrs = chown_buf->attrs;
872 xafile = lookup_one_len (name, xadir, namelen);
874 return PTR_ERR (xafile);
875 else if (!xafile->d_inode) {
880 if (!S_ISDIR (xafile->d_inode->i_mode))
881 err = notify_change (xafile, attrs);
888 reiserfs_chown_xattrs (struct inode *inode, struct iattr *attrs)
893 struct reiserfs_chown_buf buf;
894 unsigned int ia_valid = attrs->ia_valid;
896 /* Skip out, an xattr has no xattrs associated with it */
897 if (is_reiserfs_priv_object (inode) ||
898 get_inode_sd_version (inode) == STAT_DATA_V1 ||
899 !reiserfs_xattrs(inode->i_sb))
903 reiserfs_read_lock_xattrs (inode->i_sb);
904 dir = open_xa_dir (inode, FL_READONLY);
905 reiserfs_read_unlock_xattrs (inode->i_sb);
907 if (PTR_ERR (dir) != -ENODATA)
910 } else if (!dir->d_inode) {
915 fp = dentry_open (dir, NULL, O_RDWR);
918 /* dentry_open dputs the dentry if it fails */
924 attrs->ia_valid &= (ATTR_UID | ATTR_GID | ATTR_CTIME);
929 err = xattr_readdir (fp, reiserfs_chown_xattrs_filler, &buf);
935 err = notify_change (dir, attrs);
942 attrs->ia_valid = ia_valid;
947 /* Actual operations that are exported to VFS-land */
950 * Inode operation getxattr()
951 * Preliminary locking: we down dentry->d_inode->i_sem
954 reiserfs_getxattr (struct dentry *dentry, const char *name, void *buffer,
957 struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix (name);
960 if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
961 get_inode_sd_version (dentry->d_inode) == STAT_DATA_V1)
964 reiserfs_read_lock_xattr_i (dentry->d_inode);
965 reiserfs_read_lock_xattrs (dentry->d_sb);
966 err = xah->get (dentry->d_inode, name, buffer, size);
967 reiserfs_read_unlock_xattrs (dentry->d_sb);
968 reiserfs_read_unlock_xattr_i (dentry->d_inode);
974 * Inode operation setxattr()
976 * dentry->d_inode->i_sem down
979 reiserfs_setxattr (struct dentry *dentry, const char *name, const void *value,
980 size_t size, int flags)
982 struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix (name);
986 if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
987 get_inode_sd_version (dentry->d_inode) == STAT_DATA_V1)
990 if (IS_RDONLY (dentry->d_inode))
993 if (IS_IMMUTABLE (dentry->d_inode) || IS_APPEND (dentry->d_inode))
996 reiserfs_write_lock_xattr_i (dentry->d_inode);
997 lock = !has_xattr_dir (dentry->d_inode);
999 reiserfs_write_lock_xattrs (dentry->d_sb);
1001 reiserfs_read_lock_xattrs (dentry->d_sb);
1002 err = xah->set (dentry->d_inode, name, value, size, flags);
1004 reiserfs_write_unlock_xattrs (dentry->d_sb);
1006 reiserfs_read_unlock_xattrs (dentry->d_sb);
1007 reiserfs_write_unlock_xattr_i (dentry->d_inode);
1012 * Inode operation removexattr()
1014 * dentry->d_inode->i_sem down
1017 reiserfs_removexattr (struct dentry *dentry, const char *name)
1020 struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix (name);
1022 if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
1023 get_inode_sd_version (dentry->d_inode) == STAT_DATA_V1)
1026 if (IS_RDONLY (dentry->d_inode))
1029 if (IS_IMMUTABLE (dentry->d_inode) || IS_APPEND (dentry->d_inode))
1032 reiserfs_write_lock_xattr_i (dentry->d_inode);
1033 reiserfs_read_lock_xattrs (dentry->d_sb);
1035 /* Deletion pre-operation */
1037 err = xah->del (dentry->d_inode, name);
1042 err = reiserfs_xattr_del (dentry->d_inode, name);
1044 dentry->d_inode->i_ctime = CURRENT_TIME;
1045 mark_inode_dirty (dentry->d_inode);
1048 reiserfs_read_unlock_xattrs (dentry->d_sb);
1049 reiserfs_write_unlock_xattr_i (dentry->d_inode);
1054 /* This is what filldir will use:
1055 * r_pos will always contain the amount of space required for the entire
1056 * list. If r_pos becomes larger than r_size, we need more space and we
1057 * return an error indicating this. If r_pos is less than r_size, then we've
1058 * filled the buffer successfully and we return success */
1059 struct reiserfs_listxattr_buf {
1063 struct inode *r_inode;
1067 reiserfs_listxattr_filler (void *buf, const char *name, int namelen,
1068 loff_t offset, ino_t ino, unsigned int d_type)
1070 struct reiserfs_listxattr_buf *b = (struct reiserfs_listxattr_buf *)buf;
1072 if (name[0] != '.' || (namelen != 1 && (name[1] != '.' || namelen != 2))) {
1073 struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix (name);
1074 if (!xah) return 0; /* Unsupported xattr name, skip it */
1076 /* We call ->list() twice because the operation isn't required to just
1077 * return the name back - we want to make sure we have enough space */
1078 len += xah->list (b->r_inode, name, namelen, NULL);
1081 if (b->r_pos + len + 1 <= b->r_size) {
1082 char *p = b->r_buf + b->r_pos;
1083 p += xah->list (b->r_inode, name, namelen, p);
1086 b->r_pos += len + 1;
1093 * Inode operation listxattr()
1095 * Preliminary locking: we down dentry->d_inode->i_sem
1098 reiserfs_listxattr (struct dentry *dentry, char *buffer, size_t size)
1103 struct reiserfs_listxattr_buf buf;
1105 if (!dentry->d_inode)
1108 if (!reiserfs_xattrs(dentry->d_sb) ||
1109 get_inode_sd_version (dentry->d_inode) == STAT_DATA_V1)
1112 reiserfs_read_lock_xattr_i (dentry->d_inode);
1113 reiserfs_read_lock_xattrs (dentry->d_sb);
1114 dir = open_xa_dir (dentry->d_inode, FL_READONLY);
1115 reiserfs_read_unlock_xattrs (dentry->d_sb);
1117 err = PTR_ERR (dir);
1118 if (err == -ENODATA)
1119 err = 0; /* Not an error if there aren't any xattrs */
1123 fp = dentry_open (dir, NULL, O_RDWR);
1126 /* dentry_open dputs the dentry if it fails */
1131 buf.r_size = buffer ? size : 0;
1133 buf.r_inode = dentry->d_inode;
1135 REISERFS_I(dentry->d_inode)->i_flags |= i_has_xattr_dir;
1137 err = xattr_readdir (fp, reiserfs_listxattr_filler, &buf);
1141 if (buf.r_pos > buf.r_size && buffer != NULL)
1150 reiserfs_read_unlock_xattr_i (dentry->d_inode);
1154 /* This is the implementation for the xattr plugin infrastructure */
1155 static struct list_head xattr_handlers = LIST_HEAD_INIT (xattr_handlers);
1156 static rwlock_t handler_lock = RW_LOCK_UNLOCKED;
1158 static struct reiserfs_xattr_handler *
1159 find_xattr_handler_prefix (const char *prefix)
1161 struct reiserfs_xattr_handler *xah = NULL;
1162 struct list_head *p;
1164 read_lock (&handler_lock);
1165 list_for_each (p, &xattr_handlers) {
1166 xah = list_entry (p, struct reiserfs_xattr_handler, handlers);
1167 if (strncmp (xah->prefix, prefix, strlen (xah->prefix)) == 0)
1172 read_unlock (&handler_lock);
1177 __unregister_handlers (void)
1179 struct reiserfs_xattr_handler *xah;
1180 struct list_head *p, *tmp;
1182 list_for_each_safe (p, tmp, &xattr_handlers) {
1183 xah = list_entry (p, struct reiserfs_xattr_handler, handlers);
1189 INIT_LIST_HEAD (&xattr_handlers);
1193 reiserfs_xattr_register_handlers (void)
1196 struct reiserfs_xattr_handler *xah;
1197 struct list_head *p;
1199 write_lock (&handler_lock);
1201 /* If we're already initialized, nothing to do */
1202 if (!list_empty (&xattr_handlers)) {
1203 write_unlock (&handler_lock);
1207 /* Add the handlers */
1208 list_add_tail (&user_handler.handlers, &xattr_handlers);
1209 list_add_tail (&trusted_handler.handlers, &xattr_handlers);
1210 #ifdef CONFIG_REISERFS_FS_SECURITY
1211 list_add_tail (&security_handler.handlers, &xattr_handlers);
1213 #ifdef CONFIG_REISERFS_FS_POSIX_ACL
1214 list_add_tail (&posix_acl_access_handler.handlers, &xattr_handlers);
1215 list_add_tail (&posix_acl_default_handler.handlers, &xattr_handlers);
1218 /* Run initializers, if available */
1219 list_for_each (p, &xattr_handlers) {
1220 xah = list_entry (p, struct reiserfs_xattr_handler, handlers);
1230 /* Clean up other handlers, if any failed */
1232 __unregister_handlers ();
1234 write_unlock (&handler_lock);
1239 reiserfs_xattr_unregister_handlers (void)
1241 write_lock (&handler_lock);
1242 __unregister_handlers ();
1243 write_unlock (&handler_lock);
1246 /* This will catch lookups from the fs root to .reiserfs_priv */
1248 xattr_lookup_poison (struct dentry *dentry, struct qstr *q1, struct qstr *name)
1250 struct dentry *priv_root = REISERFS_SB(dentry->d_sb)->priv_root;
1251 if (name->len == priv_root->d_name.len &&
1252 name->hash == priv_root->d_name.hash &&
1253 !memcmp (name->name, priv_root->d_name.name, name->len)) {
1255 } else if (q1->len == name->len &&
1256 !memcmp(q1->name, name->name, name->len))
1261 static struct dentry_operations xattr_lookup_poison_ops = {
1262 .d_compare = xattr_lookup_poison,
1266 /* We need to take a copy of the mount flags since things like
1267 * MS_RDONLY don't get set until *after* we're called.
1268 * mount_flags != mount_options */
1270 reiserfs_xattr_init (struct super_block *s, int mount_flags)
1274 /* We need generation numbers to ensure that the oid mapping is correct
1275 * v3.5 filesystems don't have them. */
1276 if (!old_format_only (s)) {
1277 set_bit (REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
1278 } else if (reiserfs_xattrs_optional (s)) {
1279 /* Old format filesystem, but optional xattrs have been enabled
1280 * at mount time. Error out. */
1281 reiserfs_warning (s, "xattrs/ACLs not supported on pre v3.6 "
1282 "format filesystem. Failing mount.");
1286 /* Old format filesystem, but no optional xattrs have been enabled. This
1287 * means we silently disable xattrs on the filesystem. */
1288 clear_bit (REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
1291 /* If we don't have the privroot located yet - go find it */
1292 if (reiserfs_xattrs (s) && !REISERFS_SB(s)->priv_root) {
1293 struct dentry *dentry;
1294 dentry = lookup_one_len (PRIVROOT_NAME, s->s_root,
1295 strlen (PRIVROOT_NAME));
1296 if (!IS_ERR (dentry)) {
1297 if (!(mount_flags & MS_RDONLY) && !dentry->d_inode) {
1298 struct inode *inode = dentry->d_parent->d_inode;
1299 down (&inode->i_sem);
1300 err = inode->i_op->mkdir (inode, dentry, 0700);
1307 if (dentry && dentry->d_inode)
1308 reiserfs_warning (s, "Created %s on %s - reserved for "
1309 "xattr storage.", PRIVROOT_NAME,
1310 reiserfs_bdevname (inode->i_sb));
1311 } else if (!dentry->d_inode) {
1316 err = PTR_ERR (dentry);
1318 if (!err && dentry) {
1319 s->s_root->d_op = &xattr_lookup_poison_ops;
1320 REISERFS_I(dentry->d_inode)->i_flags |= i_priv_object;
1321 REISERFS_SB(s)->priv_root = dentry;
1322 } else if (!(mount_flags & MS_RDONLY)) { /* xattrs are unavailable */
1323 /* If we're read-only it just means that the dir hasn't been
1324 * created. Not an error -- just no xattrs on the fs. We'll
1325 * check again if we go read-write */
1326 reiserfs_warning (s, "xattrs/ACLs enabled and couldn't "
1327 "find/create .reiserfs_priv. Failing mount.");
1333 /* This is only nonzero if there was an error initializing the xattr
1334 * directory or if there is a condition where we don't support them. */
1336 clear_bit (REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
1337 clear_bit (REISERFS_XATTRS_USER, &(REISERFS_SB(s)->s_mount_opt));
1338 clear_bit (REISERFS_POSIXACL, &(REISERFS_SB(s)->s_mount_opt));
1341 /* The super_block MS_POSIXACL must mirror the (no)acl mount option. */
1342 s->s_flags = s->s_flags & ~MS_POSIXACL;
1343 if (reiserfs_posixacl (s))
1344 s->s_flags |= MS_POSIXACL;
1350 __reiserfs_permission (struct inode *inode, int mask, struct nameidata *nd,
1353 umode_t mode = inode->i_mode;
1355 /* Prevent vservers from escaping chroot() barriers */
1356 if (IS_BARRIER(inode) && !vx_check(0, VX_ADMIN))
1359 if (mask & MAY_WRITE) {
1361 * Nobody gets write access to a read-only fs.
1363 if ((IS_RDONLY(inode) || (nd && MNT_IS_RDONLY(nd->mnt))) &&
1364 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
1368 * Nobody gets write access to an immutable file.
1370 if (IS_IMMUTABLE(inode))
1374 /* We don't do permission checks on the internal objects.
1375 * Permissions are determined by the "owning" object. */
1376 if (is_reiserfs_priv_object (inode))
1379 if (current->fsuid == inode->i_uid) {
1381 #ifdef CONFIG_REISERFS_FS_POSIX_ACL
1382 } else if (reiserfs_posixacl(inode->i_sb) &&
1383 get_inode_sd_version (inode) != STAT_DATA_V1) {
1384 struct posix_acl *acl;
1386 /* ACL can't contain additional permissions if
1387 the ACL_MASK entry is 0 */
1388 if (!(mode & S_IRWXG))
1392 reiserfs_read_lock_xattr_i (inode);
1393 reiserfs_read_lock_xattrs (inode->i_sb);
1395 acl = reiserfs_get_acl (inode, ACL_TYPE_ACCESS);
1397 reiserfs_read_unlock_xattrs (inode->i_sb);
1398 reiserfs_read_unlock_xattr_i (inode);
1401 if (PTR_ERR (acl) == -ENODATA)
1403 return PTR_ERR (acl);
1407 int err = posix_acl_permission (inode, acl, mask);
1408 posix_acl_release (acl);
1409 if (err == -EACCES) {
1410 goto check_capabilities;
1418 #ifdef CONFIG_REISERFS_FS_POSIX_ACL
1421 if (in_group_p(inode->i_gid))
1426 * If the DACs are ok we don't need any capability check.
1428 if (((mode & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask))
1431 #ifdef CONFIG_REISERFS_FS_POSIX_ACL
1435 * Read/write DACs are always overridable.
1436 * Executable DACs are overridable if at least one exec bit is set.
1438 if (!(mask & MAY_EXEC) ||
1439 (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode))
1440 if (capable(CAP_DAC_OVERRIDE))
1444 * Searching includes executable on directories, else just read.
1446 if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
1447 if (capable(CAP_DAC_READ_SEARCH))
1454 reiserfs_permission (struct inode *inode, int mask, struct nameidata *nd)
1456 return __reiserfs_permission (inode, mask, nd, 1);
1460 reiserfs_permission_locked (struct inode *inode, int mask, struct nameidata *nd)
1462 return __reiserfs_permission (inode, mask, nd, 0);