2 * linux/fs/reiserfs/xattr.c
4 * Copyright (c) 2002 by Jeff Mahoney, <jeffm@suse.com>
9 * In order to implement EA/ACLs in a clean, backwards compatible manner,
10 * they are implemented as files in a "private" directory.
11 * Each EA is in it's own file, with the directory layout like so (/ is assumed
12 * to be relative to fs root). Inside the /.reiserfs_priv/xattrs directory,
13 * directories named using the capital-hex form of the objectid and
14 * generation number are used. Inside each directory are individual files
15 * named with the name of the extended attribute.
17 * So, for objectid 12648430, we could have:
18 * /.reiserfs_priv/xattrs/C0FFEE.0/system.posix_acl_access
19 * /.reiserfs_priv/xattrs/C0FFEE.0/system.posix_acl_default
20 * /.reiserfs_priv/xattrs/C0FFEE.0/user.Content-Type
23 * The file contents are the text of the EA. The size is known based on the
24 * stat data describing the file.
26 * In the case of system.posix_acl_access and system.posix_acl_default, since
27 * these are special cases for filesystem ACLs, they are interpreted by the
28 * kernel, in addition, they are negatively and positively cached and attached
29 * to the inode so that unnecessary lookups are avoided.
32 #include <linux/reiserfs_fs.h>
33 #include <linux/capability.h>
34 #include <linux/dcache.h>
35 #include <linux/namei.h>
36 #include <linux/errno.h>
38 #include <linux/file.h>
39 #include <linux/pagemap.h>
40 #include <linux/xattr.h>
41 #include <linux/reiserfs_xattr.h>
42 #include <linux/reiserfs_acl.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
57 static struct dentry
*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
));
69 } else if (!xaroot
->d_inode
) {
71 mutex_lock(&privroot
->d_inode
->i_mutex
);
73 privroot
->d_inode
->i_op
->mkdir(privroot
->d_inode
, xaroot
,
75 mutex_unlock(&privroot
->d_inode
->i_mutex
);
82 REISERFS_SB(sb
)->xattr_root
= dget(xaroot
);
90 /* This will return a dentry, or error, refering to the xa root directory.
91 * If the xa root doesn't exist yet, the dentry will be returned without
92 * an associated inode. This dentry can be used with ->mkdir to create
93 * the xa directory. */
94 static struct dentry
*__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 retrieved, it is used.
120 * Otherwise, we attempt to retrieve it from disk. It may also return
121 * a pointer-encoded error.
123 static inline struct dentry
*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
*open_xa_dir(const struct inode
*inode
, int flags
)
138 struct dentry
*xaroot
, *xadir
;
141 xaroot
= get_xa_root(inode
->i_sb
);
142 if (IS_ERR(xaroot
)) {
144 } else if (!xaroot
) {
145 if (flags
== 0 || flags
& XATTR_CREATE
) {
146 xaroot
= create_xa_root(inode
->i_sb
);
151 return ERR_PTR(-ENODATA
);
154 /* ok, we have xaroot open */
156 snprintf(namebuf
, sizeof(namebuf
), "%X.%X",
157 le32_to_cpu(INODE_PKEY(inode
)->k_objectid
),
158 inode
->i_generation
);
159 xadir
= lookup_one_len(namebuf
, xaroot
, strlen(namebuf
));
165 if (!xadir
->d_inode
) {
167 if (flags
== 0 || flags
& XATTR_CREATE
) {
168 /* Although there is nothing else trying to create this directory,
169 * another directory with the same hash may be created, so we need
170 * to protect against that */
172 xaroot
->d_inode
->i_op
->mkdir(xaroot
->d_inode
, xadir
,
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
*get_xa_file_dentry(const struct inode
*inode
,
195 const char *name
, int flags
)
197 struct dentry
*xadir
, *xafile
;
200 xadir
= open_xa_dir(inode
, flags
);
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_mutex is down, so nothing else can try to create
225 err
= xadir
->d_inode
->i_op
->create(xadir
->d_inode
, xafile
,
226 0700 | S_IFREG
, NULL
);
237 xafile
= ERR_PTR(err
);
241 /* Opens a file pointer to the attribute associated with inode */
242 static struct file
*open_xa_file(const struct inode
*inode
, const char *name
,
245 struct dentry
*xafile
;
248 xafile
= get_xa_file_dentry(inode
, name
, flags
);
250 return ERR_PTR(PTR_ERR(xafile
));
251 else if (!xafile
->d_inode
) {
253 return ERR_PTR(-ENODATA
);
256 fp
= dentry_open(xafile
, NULL
, O_RDWR
);
257 /* dentry_open dputs the dentry if it fails */
263 * this is very similar to fs/reiserfs/dir.c:reiserfs_readdir, but
264 * we need to drop the path before calling the filldir struct. That
265 * would be a big performance hit to the non-xattr case, so I've copied
266 * the whole thing for now. --clm
268 * the big difference is that I go backwards through the directory,
269 * and don't mess with f->f_pos, but the idea is the same. Do some
270 * action on each and every entry in the directory.
272 * we're called with i_mutex held, so there are no worries about the directory
273 * changing underneath us.
275 static int __xattr_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
277 struct inode
*inode
= filp
->f_dentry
->d_inode
;
278 struct cpu_key pos_key
; /* key of current position in the directory (key of directory entry) */
279 INITIALIZE_PATH(path_to_entry
);
280 struct buffer_head
*bh
;
282 struct item_head
*ih
, tmp_ih
;
286 char small_buf
[32]; /* avoid kmalloc if we can */
287 struct reiserfs_de_head
*deh
;
292 struct reiserfs_dir_entry de
;
294 /* form key for search the next directory entry using f_pos field of
296 next_pos
= max_reiserfs_offset(inode
);
300 if (next_pos
<= DOT_DOT_OFFSET
)
302 make_cpu_key(&pos_key
, inode
, next_pos
, TYPE_DIRENTRY
, 3);
305 search_by_entry_key(inode
->i_sb
, &pos_key
, &path_to_entry
,
307 if (search_res
== IO_ERROR
) {
308 // FIXME: we could just skip part of directory which could
310 pathrelse(&path_to_entry
);
314 if (search_res
== NAME_NOT_FOUND
)
317 set_de_name_and_namelen(&de
);
318 entry_num
= de
.de_entry_num
;
319 deh
= &(de
.de_deh
[entry_num
]);
324 if (!is_direntry_le_ih(ih
)) {
325 reiserfs_warning(inode
->i_sb
, "not direntry %h", ih
);
328 copy_item_head(&tmp_ih
, ih
);
330 /* we must have found item, that is item of this directory, */
331 RFALSE(COMP_SHORT_KEYS(&(ih
->ih_key
), &pos_key
),
332 "vs-9000: found item %h does not match to dir we readdir %K",
335 if (deh_offset(deh
) <= DOT_DOT_OFFSET
) {
339 /* look for the previous entry in the directory */
340 next_pos
= deh_offset(deh
) - 1;
342 if (!de_visible(deh
))
343 /* it is hidden entry */
346 d_reclen
= entry_length(bh
, ih
, entry_num
);
347 d_name
= B_I_DEH_ENTRY_FILE_NAME(bh
, ih
, deh
);
348 d_off
= deh_offset(deh
);
349 d_ino
= deh_objectid(deh
);
351 if (!d_name
[d_reclen
- 1])
352 d_reclen
= strlen(d_name
);
354 if (d_reclen
> REISERFS_MAX_NAME(inode
->i_sb
->s_blocksize
)) {
355 /* too big to send back to VFS */
359 /* Ignore the .reiserfs_priv entry */
360 if (reiserfs_xattrs(inode
->i_sb
) &&
361 !old_format_only(inode
->i_sb
) &&
363 le32_to_cpu(INODE_PKEY
364 (REISERFS_SB(inode
->i_sb
)->priv_root
->d_inode
)->
368 if (d_reclen
<= 32) {
369 local_buf
= small_buf
;
372 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
,
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
,
407 if (local_buf
!= small_buf
) {
408 reiserfs_kfree(local_buf
, d_reclen
, inode
->i_sb
);
413 pathrelse(&path_to_entry
);
418 * this could be done with dedicated readdir ops for the xattr files,
419 * but I want to get something working asap
420 * this is stolen from vfs_readdir
424 int xattr_readdir(struct file
*file
, filldir_t filler
, void *buf
)
426 struct inode
*inode
= file
->f_dentry
->d_inode
;
428 if (!file
->f_op
|| !file
->f_op
->readdir
)
430 mutex_lock(&inode
->i_mutex
);
431 // down(&inode->i_zombie);
433 if (!IS_DEADDIR(inode
)) {
435 res
= __xattr_readdir(file
, buf
, filler
);
438 // up(&inode->i_zombie);
439 mutex_unlock(&inode
->i_mutex
);
444 /* Internal operations on file data */
445 static inline void reiserfs_put_page(struct page
*page
)
448 page_cache_release(page
);
451 static struct page
*reiserfs_get_page(struct inode
*dir
, unsigned long n
)
453 struct address_space
*mapping
= dir
->i_mapping
;
455 /* We can deadlock if we try to free dentries,
456 and an unlink/rmdir has just occured - GFP_NOFS avoids this */
457 mapping_set_gfp_mask(mapping
, GFP_NOFS
);
458 page
= read_cache_page(mapping
, n
,
459 (filler_t
*) mapping
->a_ops
->readpage
, NULL
);
461 wait_on_page_locked(page
);
463 if (!PageUptodate(page
))
472 reiserfs_put_page(page
);
473 return ERR_PTR(-EIO
);
476 static inline __u32
xattr_hash(const char *msg
, int len
)
478 return csum_partial(msg
, len
, 0);
481 /* Generic extended attribute operations that can be used by xa plugins */
484 * inode->i_mutex: down
487 reiserfs_xattr_set(struct inode
*inode
, const char *name
, const void *buffer
,
488 size_t buffer_size
, int flags
)
494 struct address_space
*mapping
;
496 size_t buffer_pos
= 0;
497 struct inode
*xinode
;
498 struct iattr newattrs
;
501 if (get_inode_sd_version(inode
) == STAT_DATA_V1
)
504 /* Empty xattrs are ok, they're just empty files, no hash */
505 if (buffer
&& buffer_size
)
506 xahash
= xattr_hash(buffer
, buffer_size
);
509 fp
= open_xa_file(inode
, name
, flags
);
515 xinode
= fp
->f_dentry
->d_inode
;
516 REISERFS_I(inode
)->i_flags
|= i_has_xattr_dir
;
518 /* we need to copy it off.. */
519 if (xinode
->i_nlink
> 1) {
521 err
= reiserfs_xattr_del(inode
, name
);
524 /* We just killed the old one, we're not replacing anymore */
525 if (flags
& XATTR_REPLACE
)
526 flags
&= ~XATTR_REPLACE
;
530 /* Resize it so we're ok to write there */
531 newattrs
.ia_size
= buffer_size
;
532 newattrs
.ia_valid
= ATTR_SIZE
| ATTR_CTIME
;
533 mutex_lock(&xinode
->i_mutex
);
534 err
= notify_change(fp
->f_dentry
, &newattrs
);
538 mapping
= xinode
->i_mapping
;
539 while (buffer_pos
< buffer_size
|| buffer_pos
== 0) {
542 size_t page_offset
= (file_pos
& (PAGE_CACHE_SIZE
- 1));
543 if (buffer_size
- buffer_pos
> PAGE_CACHE_SIZE
)
544 chunk
= PAGE_CACHE_SIZE
;
546 chunk
= buffer_size
- buffer_pos
;
548 page
= reiserfs_get_page(xinode
, file_pos
>> PAGE_CACHE_SHIFT
);
555 data
= page_address(page
);
558 struct reiserfs_xattr_header
*rxh
;
559 skip
= file_pos
= sizeof(struct reiserfs_xattr_header
);
560 if (chunk
+ skip
> PAGE_CACHE_SIZE
)
561 chunk
= PAGE_CACHE_SIZE
- skip
;
562 rxh
= (struct reiserfs_xattr_header
*)data
;
563 rxh
->h_magic
= cpu_to_le32(REISERFS_XATTR_MAGIC
);
564 rxh
->h_hash
= cpu_to_le32(xahash
);
567 err
= mapping
->a_ops
->prepare_write(fp
, page
, page_offset
,
568 page_offset
+ chunk
+ skip
);
571 memcpy(data
+ skip
, buffer
+ buffer_pos
, chunk
);
573 mapping
->a_ops
->commit_write(fp
, page
, page_offset
,
574 page_offset
+ chunk
+
578 reiserfs_put_page(page
);
582 if (err
|| buffer_size
== 0 || !buffer
)
586 /* We can't mark the inode dirty if it's not hashed. This is the case
587 * when we're inheriting the default ACL. If we dirty it, the inode
588 * gets marked dirty, but won't (ever) make it onto the dirty list until
589 * it's synced explicitly to clear I_DIRTY. This is bad. */
590 if (!hlist_unhashed(&inode
->i_hash
)) {
591 inode
->i_ctime
= CURRENT_TIME_SEC
;
592 mark_inode_dirty(inode
);
596 mutex_unlock(&xinode
->i_mutex
);
604 * inode->i_mutex: down
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
);
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
,
675 "Invalid magic for xattr (%s) "
676 "associated with %k", name
,
681 hash
= le32_to_cpu(rxh
->h_hash
);
683 memcpy(buffer
+ buffer_pos
, data
+ skip
, chunk
);
685 reiserfs_put_page(page
);
690 err
= isize
- sizeof(struct reiserfs_xattr_header
);
692 if (xattr_hash(buffer
, isize
- sizeof(struct reiserfs_xattr_header
)) !=
694 reiserfs_warning(inode
->i_sb
,
695 "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
)->
731 k_objectid
), xadir
->d_name
.len
,
732 xadir
->d_name
.name
, namelen
, name
,
733 is_reiserfs_priv_object(xadir
->
740 err
= dir
->i_op
->unlink(dir
, dentry
);
751 int reiserfs_xattr_del(struct inode
*inode
, const char *name
)
756 dir
= open_xa_dir(inode
, FL_READONLY
);
762 err
= __reiserfs_xattr_del(dir
, name
, strlen(name
));
766 inode
->i_ctime
= CURRENT_TIME_SEC
;
767 mark_inode_dirty(inode
);
774 /* The following are side effects of other operations that aren't explicitly
775 * modifying extended attributes. This includes operations such as permissions
776 * or ownership changes, object deletions, etc. */
779 reiserfs_delete_xattrs_filler(void *buf
, const char *name
, int namelen
,
780 loff_t offset
, ino_t ino
, unsigned int d_type
)
782 struct dentry
*xadir
= (struct dentry
*)buf
;
784 return __reiserfs_xattr_del(xadir
, name
, namelen
);
788 /* This is called w/ inode->i_mutex downed */
789 int reiserfs_delete_xattrs(struct inode
*inode
)
792 struct dentry
*dir
, *root
;
795 /* Skip out, an xattr has no xattrs associated with it */
796 if (is_reiserfs_priv_object(inode
) ||
797 get_inode_sd_version(inode
) == STAT_DATA_V1
||
798 !reiserfs_xattrs(inode
->i_sb
)) {
801 reiserfs_read_lock_xattrs(inode
->i_sb
);
802 dir
= open_xa_dir(inode
, FL_READONLY
);
803 reiserfs_read_unlock_xattrs(inode
->i_sb
);
807 } else if (!dir
->d_inode
) {
812 fp
= dentry_open(dir
, NULL
, O_RDWR
);
815 /* dentry_open dputs the dentry if it fails */
820 err
= xattr_readdir(fp
, reiserfs_delete_xattrs_filler
, dir
);
826 /* Leftovers besides . and .. -- that's not good. */
827 if (dir
->d_inode
->i_nlink
<= 2) {
828 root
= get_xa_root(inode
->i_sb
);
829 reiserfs_write_lock_xattrs(inode
->i_sb
);
830 err
= vfs_rmdir(root
->d_inode
, dir
);
831 reiserfs_write_unlock_xattrs(inode
->i_sb
);
834 reiserfs_warning(inode
->i_sb
,
835 "Couldn't remove all entries in directory");
844 REISERFS_I(inode
)->i_flags
=
845 REISERFS_I(inode
)->i_flags
& ~i_has_xattr_dir
;
849 struct reiserfs_chown_buf
{
851 struct dentry
*xadir
;
855 /* XXX: If there is a better way to do this, I'd love to hear about it */
857 reiserfs_chown_xattrs_filler(void *buf
, const char *name
, int namelen
,
858 loff_t offset
, ino_t ino
, unsigned int d_type
)
860 struct reiserfs_chown_buf
*chown_buf
= (struct reiserfs_chown_buf
*)buf
;
861 struct dentry
*xafile
, *xadir
= chown_buf
->xadir
;
862 struct iattr
*attrs
= chown_buf
->attrs
;
865 xafile
= lookup_one_len(name
, xadir
, namelen
);
867 return PTR_ERR(xafile
);
868 else if (!xafile
->d_inode
) {
873 if (!S_ISDIR(xafile
->d_inode
->i_mode
))
874 err
= notify_change(xafile
, attrs
);
880 int reiserfs_chown_xattrs(struct inode
*inode
, struct iattr
*attrs
)
885 struct reiserfs_chown_buf buf
;
886 unsigned int ia_valid
= attrs
->ia_valid
;
888 /* Skip out, an xattr has no xattrs associated with it */
889 if (is_reiserfs_priv_object(inode
) ||
890 get_inode_sd_version(inode
) == STAT_DATA_V1
||
891 !reiserfs_xattrs(inode
->i_sb
)) {
894 reiserfs_read_lock_xattrs(inode
->i_sb
);
895 dir
= open_xa_dir(inode
, FL_READONLY
);
896 reiserfs_read_unlock_xattrs(inode
->i_sb
);
898 if (PTR_ERR(dir
) != -ENODATA
)
901 } else if (!dir
->d_inode
) {
906 fp
= dentry_open(dir
, NULL
, O_RDWR
);
909 /* dentry_open dputs the dentry if it fails */
915 attrs
->ia_valid
&= (ATTR_UID
| ATTR_GID
| ATTR_CTIME
);
920 err
= xattr_readdir(fp
, reiserfs_chown_xattrs_filler
, &buf
);
926 err
= notify_change(dir
, attrs
);
933 attrs
->ia_valid
= ia_valid
;
937 /* Actual operations that are exported to VFS-land */
940 * Inode operation getxattr()
941 * Preliminary locking: we down dentry->d_inode->i_mutex
944 reiserfs_getxattr(struct dentry
* dentry
, const char *name
, void *buffer
,
947 struct reiserfs_xattr_handler
*xah
= find_xattr_handler_prefix(name
);
950 if (!xah
|| !reiserfs_xattrs(dentry
->d_sb
) ||
951 get_inode_sd_version(dentry
->d_inode
) == STAT_DATA_V1
)
954 reiserfs_read_lock_xattr_i(dentry
->d_inode
);
955 reiserfs_read_lock_xattrs(dentry
->d_sb
);
956 err
= xah
->get(dentry
->d_inode
, name
, buffer
, size
);
957 reiserfs_read_unlock_xattrs(dentry
->d_sb
);
958 reiserfs_read_unlock_xattr_i(dentry
->d_inode
);
963 * Inode operation setxattr()
965 * dentry->d_inode->i_mutex down
968 reiserfs_setxattr(struct dentry
*dentry
, const char *name
, const void *value
,
969 size_t size
, int flags
)
971 struct reiserfs_xattr_handler
*xah
= find_xattr_handler_prefix(name
);
975 if (!xah
|| !reiserfs_xattrs(dentry
->d_sb
) ||
976 get_inode_sd_version(dentry
->d_inode
) == STAT_DATA_V1
)
979 reiserfs_write_lock_xattr_i(dentry
->d_inode
);
980 lock
= !has_xattr_dir(dentry
->d_inode
);
982 reiserfs_write_lock_xattrs(dentry
->d_sb
);
984 reiserfs_read_lock_xattrs(dentry
->d_sb
);
985 err
= xah
->set(dentry
->d_inode
, name
, value
, size
, flags
);
987 reiserfs_write_unlock_xattrs(dentry
->d_sb
);
989 reiserfs_read_unlock_xattrs(dentry
->d_sb
);
990 reiserfs_write_unlock_xattr_i(dentry
->d_inode
);
995 * Inode operation removexattr()
997 * dentry->d_inode->i_mutex down
999 int reiserfs_removexattr(struct dentry
*dentry
, const char *name
)
1002 struct reiserfs_xattr_handler
*xah
= find_xattr_handler_prefix(name
);
1004 if (!xah
|| !reiserfs_xattrs(dentry
->d_sb
) ||
1005 get_inode_sd_version(dentry
->d_inode
) == STAT_DATA_V1
)
1008 reiserfs_write_lock_xattr_i(dentry
->d_inode
);
1009 reiserfs_read_lock_xattrs(dentry
->d_sb
);
1011 /* Deletion pre-operation */
1013 err
= xah
->del(dentry
->d_inode
, name
);
1018 err
= reiserfs_xattr_del(dentry
->d_inode
, name
);
1020 dentry
->d_inode
->i_ctime
= CURRENT_TIME_SEC
;
1021 mark_inode_dirty(dentry
->d_inode
);
1024 reiserfs_read_unlock_xattrs(dentry
->d_sb
);
1025 reiserfs_write_unlock_xattr_i(dentry
->d_inode
);
1029 /* This is what filldir will use:
1030 * r_pos will always contain the amount of space required for the entire
1031 * list. If r_pos becomes larger than r_size, we need more space and we
1032 * return an error indicating this. If r_pos is less than r_size, then we've
1033 * filled the buffer successfully and we return success */
1034 struct reiserfs_listxattr_buf
{
1038 struct inode
*r_inode
;
1042 reiserfs_listxattr_filler(void *buf
, const char *name
, int namelen
,
1043 loff_t offset
, ino_t ino
, unsigned int d_type
)
1045 struct reiserfs_listxattr_buf
*b
= (struct reiserfs_listxattr_buf
*)buf
;
1048 || (namelen
!= 1 && (name
[1] != '.' || namelen
!= 2))) {
1049 struct reiserfs_xattr_handler
*xah
=
1050 find_xattr_handler_prefix(name
);
1052 return 0; /* Unsupported xattr name, skip it */
1054 /* We call ->list() twice because the operation isn't required to just
1055 * return the name back - we want to make sure we have enough space */
1056 len
+= xah
->list(b
->r_inode
, name
, namelen
, NULL
);
1059 if (b
->r_pos
+ len
+ 1 <= b
->r_size
) {
1060 char *p
= b
->r_buf
+ b
->r_pos
;
1061 p
+= xah
->list(b
->r_inode
, name
, namelen
, p
);
1064 b
->r_pos
+= len
+ 1;
1072 * Inode operation listxattr()
1074 * Preliminary locking: we down dentry->d_inode->i_mutex
1076 ssize_t
reiserfs_listxattr(struct dentry
* dentry
, char *buffer
, size_t size
)
1081 struct reiserfs_listxattr_buf buf
;
1083 if (!dentry
->d_inode
)
1086 if (!reiserfs_xattrs(dentry
->d_sb
) ||
1087 get_inode_sd_version(dentry
->d_inode
) == STAT_DATA_V1
)
1090 reiserfs_read_lock_xattr_i(dentry
->d_inode
);
1091 reiserfs_read_lock_xattrs(dentry
->d_sb
);
1092 dir
= open_xa_dir(dentry
->d_inode
, FL_READONLY
);
1093 reiserfs_read_unlock_xattrs(dentry
->d_sb
);
1096 if (err
== -ENODATA
)
1097 err
= 0; /* Not an error if there aren't any xattrs */
1101 fp
= dentry_open(dir
, NULL
, O_RDWR
);
1104 /* dentry_open dputs the dentry if it fails */
1109 buf
.r_size
= buffer
? size
: 0;
1111 buf
.r_inode
= dentry
->d_inode
;
1113 REISERFS_I(dentry
->d_inode
)->i_flags
|= i_has_xattr_dir
;
1115 err
= xattr_readdir(fp
, reiserfs_listxattr_filler
, &buf
);
1119 if (buf
.r_pos
> buf
.r_size
&& buffer
!= NULL
)
1128 reiserfs_read_unlock_xattr_i(dentry
->d_inode
);
1132 /* This is the implementation for the xattr plugin infrastructure */
1133 static struct list_head xattr_handlers
= LIST_HEAD_INIT(xattr_handlers
);
1134 static DEFINE_RWLOCK(handler_lock
);
1136 static struct reiserfs_xattr_handler
*find_xattr_handler_prefix(const char
1139 struct reiserfs_xattr_handler
*xah
= NULL
;
1140 struct list_head
*p
;
1142 read_lock(&handler_lock
);
1143 list_for_each(p
, &xattr_handlers
) {
1144 xah
= list_entry(p
, struct reiserfs_xattr_handler
, handlers
);
1145 if (strncmp(xah
->prefix
, prefix
, strlen(xah
->prefix
)) == 0)
1150 read_unlock(&handler_lock
);
1154 static void __unregister_handlers(void)
1156 struct reiserfs_xattr_handler
*xah
;
1157 struct list_head
*p
, *tmp
;
1159 list_for_each_safe(p
, tmp
, &xattr_handlers
) {
1160 xah
= list_entry(p
, struct reiserfs_xattr_handler
, handlers
);
1166 INIT_LIST_HEAD(&xattr_handlers
);
1169 int __init
reiserfs_xattr_register_handlers(void)
1172 struct reiserfs_xattr_handler
*xah
;
1173 struct list_head
*p
;
1175 write_lock(&handler_lock
);
1177 /* If we're already initialized, nothing to do */
1178 if (!list_empty(&xattr_handlers
)) {
1179 write_unlock(&handler_lock
);
1183 /* Add the handlers */
1184 list_add_tail(&user_handler
.handlers
, &xattr_handlers
);
1185 list_add_tail(&trusted_handler
.handlers
, &xattr_handlers
);
1186 #ifdef CONFIG_REISERFS_FS_SECURITY
1187 list_add_tail(&security_handler
.handlers
, &xattr_handlers
);
1189 #ifdef CONFIG_REISERFS_FS_POSIX_ACL
1190 list_add_tail(&posix_acl_access_handler
.handlers
, &xattr_handlers
);
1191 list_add_tail(&posix_acl_default_handler
.handlers
, &xattr_handlers
);
1194 /* Run initializers, if available */
1195 list_for_each(p
, &xattr_handlers
) {
1196 xah
= list_entry(p
, struct reiserfs_xattr_handler
, handlers
);
1206 /* Clean up other handlers, if any failed */
1208 __unregister_handlers();
1210 write_unlock(&handler_lock
);
1214 void reiserfs_xattr_unregister_handlers(void)
1216 write_lock(&handler_lock
);
1217 __unregister_handlers();
1218 write_unlock(&handler_lock
);
1221 /* This will catch lookups from the fs root to .reiserfs_priv */
1223 xattr_lookup_poison(struct dentry
*dentry
, struct qstr
*q1
, struct qstr
*name
)
1225 struct dentry
*priv_root
= REISERFS_SB(dentry
->d_sb
)->priv_root
;
1226 if (name
->len
== priv_root
->d_name
.len
&&
1227 name
->hash
== priv_root
->d_name
.hash
&&
1228 !memcmp(name
->name
, priv_root
->d_name
.name
, name
->len
)) {
1230 } else if (q1
->len
== name
->len
&&
1231 !memcmp(q1
->name
, name
->name
, name
->len
))
1236 static struct dentry_operations xattr_lookup_poison_ops
= {
1237 .d_compare
= xattr_lookup_poison
,
1240 /* We need to take a copy of the mount flags since things like
1241 * MS_RDONLY don't get set until *after* we're called.
1242 * mount_flags != mount_options */
1243 int reiserfs_xattr_init(struct super_block
*s
, int mount_flags
)
1247 /* We need generation numbers to ensure that the oid mapping is correct
1248 * v3.5 filesystems don't have them. */
1249 if (!old_format_only(s
)) {
1250 set_bit(REISERFS_XATTRS
, &(REISERFS_SB(s
)->s_mount_opt
));
1251 } else if (reiserfs_xattrs_optional(s
)) {
1252 /* Old format filesystem, but optional xattrs have been enabled
1253 * at mount time. Error out. */
1254 reiserfs_warning(s
, "xattrs/ACLs not supported on pre v3.6 "
1255 "format filesystem. Failing mount.");
1259 /* Old format filesystem, but no optional xattrs have been enabled. This
1260 * means we silently disable xattrs on the filesystem. */
1261 clear_bit(REISERFS_XATTRS
, &(REISERFS_SB(s
)->s_mount_opt
));
1264 /* If we don't have the privroot located yet - go find it */
1265 if (reiserfs_xattrs(s
) && !REISERFS_SB(s
)->priv_root
) {
1266 struct dentry
*dentry
;
1267 dentry
= lookup_one_len(PRIVROOT_NAME
, s
->s_root
,
1268 strlen(PRIVROOT_NAME
));
1269 if (!IS_ERR(dentry
)) {
1270 if (!(mount_flags
& MS_RDONLY
) && !dentry
->d_inode
) {
1271 struct inode
*inode
= dentry
->d_parent
->d_inode
;
1272 mutex_lock(&inode
->i_mutex
);
1273 err
= inode
->i_op
->mkdir(inode
, dentry
, 0700);
1274 mutex_unlock(&inode
->i_mutex
);
1280 if (dentry
&& dentry
->d_inode
)
1282 "Created %s on %s - reserved for "
1287 } else if (!dentry
->d_inode
) {
1292 err
= PTR_ERR(dentry
);
1294 if (!err
&& dentry
) {
1295 s
->s_root
->d_op
= &xattr_lookup_poison_ops
;
1296 reiserfs_mark_inode_private(dentry
->d_inode
);
1297 REISERFS_SB(s
)->priv_root
= dentry
;
1298 } else if (!(mount_flags
& MS_RDONLY
)) { /* xattrs are unavailable */
1299 /* If we're read-only it just means that the dir hasn't been
1300 * created. Not an error -- just no xattrs on the fs. We'll
1301 * check again if we go read-write */
1302 reiserfs_warning(s
, "xattrs/ACLs enabled and couldn't "
1303 "find/create .reiserfs_priv. Failing mount.");
1309 /* This is only nonzero if there was an error initializing the xattr
1310 * directory or if there is a condition where we don't support them. */
1312 clear_bit(REISERFS_XATTRS
, &(REISERFS_SB(s
)->s_mount_opt
));
1313 clear_bit(REISERFS_XATTRS_USER
, &(REISERFS_SB(s
)->s_mount_opt
));
1314 clear_bit(REISERFS_POSIXACL
, &(REISERFS_SB(s
)->s_mount_opt
));
1317 /* The super_block MS_POSIXACL must mirror the (no)acl mount option. */
1318 s
->s_flags
= s
->s_flags
& ~MS_POSIXACL
;
1319 if (reiserfs_posixacl(s
))
1320 s
->s_flags
|= MS_POSIXACL
;
1326 __reiserfs_permission(struct inode
*inode
, int mask
, struct nameidata
*nd
,
1329 umode_t mode
= inode
->i_mode
;
1331 if (mask
& MAY_WRITE
) {
1333 * Nobody gets write access to a read-only fs.
1335 if (IS_RDONLY(inode
) &&
1336 (S_ISREG(mode
) || S_ISDIR(mode
) || S_ISLNK(mode
)))
1340 * Nobody gets write access to an immutable file.
1342 if (IS_IMMUTABLE(inode
))
1346 /* We don't do permission checks on the internal objects.
1347 * Permissions are determined by the "owning" object. */
1348 if (is_reiserfs_priv_object(inode
))
1351 if (current
->fsuid
== inode
->i_uid
) {
1353 #ifdef CONFIG_REISERFS_FS_POSIX_ACL
1354 } else if (reiserfs_posixacl(inode
->i_sb
) &&
1355 get_inode_sd_version(inode
) != STAT_DATA_V1
) {
1356 struct posix_acl
*acl
;
1358 /* ACL can't contain additional permissions if
1359 the ACL_MASK entry is 0 */
1360 if (!(mode
& S_IRWXG
))
1364 reiserfs_read_lock_xattr_i(inode
);
1365 reiserfs_read_lock_xattrs(inode
->i_sb
);
1367 acl
= reiserfs_get_acl(inode
, ACL_TYPE_ACCESS
);
1369 reiserfs_read_unlock_xattrs(inode
->i_sb
);
1370 reiserfs_read_unlock_xattr_i(inode
);
1373 if (PTR_ERR(acl
) == -ENODATA
)
1375 return PTR_ERR(acl
);
1379 int err
= posix_acl_permission(inode
, acl
, mask
);
1380 posix_acl_release(acl
);
1381 if (err
== -EACCES
) {
1382 goto check_capabilities
;
1391 if (in_group_p(inode
->i_gid
))
1396 * If the DACs are ok we don't need any capability check.
1398 if (((mode
& mask
& (MAY_READ
| MAY_WRITE
| MAY_EXEC
)) == mask
))
1403 * Read/write DACs are always overridable.
1404 * Executable DACs are overridable if at least one exec bit is set.
1406 if (!(mask
& MAY_EXEC
) ||
1407 (inode
->i_mode
& S_IXUGO
) || S_ISDIR(inode
->i_mode
))
1408 if (capable(CAP_DAC_OVERRIDE
))
1412 * Searching includes executable on directories, else just read.
1414 if (mask
== MAY_READ
|| (S_ISDIR(inode
->i_mode
) && !(mask
& MAY_WRITE
)))
1415 if (capable(CAP_DAC_READ_SEARCH
))
1421 int reiserfs_permission(struct inode
*inode
, int mask
, struct nameidata
*nd
)
1423 return __reiserfs_permission(inode
, mask
, nd
, 1);
1427 reiserfs_permission_locked(struct inode
*inode
, int mask
, struct nameidata
*nd
)
1429 return __reiserfs_permission(inode
, mask
, nd
, 0);