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.
31 * Locking works like so:
32 * Directory components (xattr root, xattr dir) are protectd by their i_mutex.
33 * The xattrs themselves are protected by the xattr_sem.
36 #include <linux/reiserfs_fs.h>
37 #include <linux/capability.h>
38 #include <linux/dcache.h>
39 #include <linux/namei.h>
40 #include <linux/errno.h>
42 #include <linux/file.h>
43 #include <linux/pagemap.h>
44 #include <linux/xattr.h>
45 #include <linux/reiserfs_xattr.h>
46 #include <linux/reiserfs_acl.h>
47 #include <asm/uaccess.h>
48 #include <net/checksum.h>
49 #include <linux/stat.h>
50 #include <linux/quotaops.h>
51 #include <linux/security.h>
53 #define PRIVROOT_NAME ".reiserfs_priv"
54 #define XAROOT_NAME "xattrs"
57 /* Helpers for inode ops. We do this so that we don't have all the VFS
58 * overhead and also for proper i_mutex annotation.
59 * dir->i_mutex must be held for all of them. */
60 #ifdef CONFIG_REISERFS_FS_XATTR
61 static int xattr_create(struct inode
*dir
, struct dentry
*dentry
, int mode
)
63 BUG_ON(!mutex_is_locked(&dir
->i_mutex
));
65 return dir
->i_op
->create(dir
, dentry
, mode
, NULL
);
69 static int xattr_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
71 BUG_ON(!mutex_is_locked(&dir
->i_mutex
));
73 return dir
->i_op
->mkdir(dir
, dentry
, mode
);
76 /* We use I_MUTEX_CHILD here to silence lockdep. It's safe because xattr
77 * mutation ops aren't called during rename or splace, which are the
78 * only other users of I_MUTEX_CHILD. It violates the ordering, but that's
79 * better than allocating another subclass just for this code. */
80 static int xattr_unlink(struct inode
*dir
, struct dentry
*dentry
)
83 BUG_ON(!mutex_is_locked(&dir
->i_mutex
));
86 reiserfs_mutex_lock_nested_safe(&dentry
->d_inode
->i_mutex
,
87 I_MUTEX_CHILD
, dir
->i_sb
);
88 error
= dir
->i_op
->unlink(dir
, dentry
);
89 mutex_unlock(&dentry
->d_inode
->i_mutex
);
96 static int xattr_rmdir(struct inode
*dir
, struct dentry
*dentry
)
99 BUG_ON(!mutex_is_locked(&dir
->i_mutex
));
102 reiserfs_mutex_lock_nested_safe(&dentry
->d_inode
->i_mutex
,
103 I_MUTEX_CHILD
, dir
->i_sb
);
104 dentry_unhash(dentry
);
105 error
= dir
->i_op
->rmdir(dir
, dentry
);
107 dentry
->d_inode
->i_flags
|= S_DEAD
;
108 mutex_unlock(&dentry
->d_inode
->i_mutex
);
116 #define xattr_may_create(flags) (!flags || flags & XATTR_CREATE)
118 static struct dentry
*open_xa_root(struct super_block
*sb
, int flags
)
120 struct dentry
*privroot
= REISERFS_SB(sb
)->priv_root
;
121 struct dentry
*xaroot
;
122 if (!privroot
->d_inode
)
123 return ERR_PTR(-ENODATA
);
125 mutex_lock_nested(&privroot
->d_inode
->i_mutex
, I_MUTEX_XATTR
);
127 xaroot
= dget(REISERFS_SB(sb
)->xattr_root
);
129 xaroot
= ERR_PTR(-ENODATA
);
130 else if (!xaroot
->d_inode
) {
132 if (xattr_may_create(flags
))
133 err
= xattr_mkdir(privroot
->d_inode
, xaroot
, 0700);
136 xaroot
= ERR_PTR(err
);
140 mutex_unlock(&privroot
->d_inode
->i_mutex
);
144 static struct dentry
*open_xa_dir(const struct inode
*inode
, int flags
)
146 struct dentry
*xaroot
, *xadir
;
149 xaroot
= open_xa_root(inode
->i_sb
, flags
);
153 snprintf(namebuf
, sizeof(namebuf
), "%X.%X",
154 le32_to_cpu(INODE_PKEY(inode
)->k_objectid
),
155 inode
->i_generation
);
157 mutex_lock_nested(&xaroot
->d_inode
->i_mutex
, I_MUTEX_XATTR
);
159 xadir
= lookup_one_len(namebuf
, xaroot
, strlen(namebuf
));
160 if (!IS_ERR(xadir
) && !xadir
->d_inode
) {
162 if (xattr_may_create(flags
))
163 err
= xattr_mkdir(xaroot
->d_inode
, xadir
, 0700);
166 xadir
= ERR_PTR(err
);
170 mutex_unlock(&xaroot
->d_inode
->i_mutex
);
175 /* The following are side effects of other operations that aren't explicitly
176 * modifying extended attributes. This includes operations such as permissions
177 * or ownership changes, object deletions, etc. */
178 struct reiserfs_dentry_buf
{
179 struct dentry
*xadir
;
181 struct dentry
*dentries
[8];
185 fill_with_dentries(void *buf
, const char *name
, int namelen
, loff_t offset
,
186 u64 ino
, unsigned int d_type
)
188 struct reiserfs_dentry_buf
*dbuf
= buf
;
189 struct dentry
*dentry
;
190 WARN_ON_ONCE(!mutex_is_locked(&dbuf
->xadir
->d_inode
->i_mutex
));
192 if (dbuf
->count
== ARRAY_SIZE(dbuf
->dentries
))
195 if (name
[0] == '.' && (name
[1] == '\0' ||
196 (name
[1] == '.' && name
[2] == '\0')))
199 dentry
= lookup_one_len(name
, dbuf
->xadir
, namelen
);
200 if (IS_ERR(dentry
)) {
201 return PTR_ERR(dentry
);
202 } else if (!dentry
->d_inode
) {
203 /* A directory entry exists, but no file? */
204 reiserfs_error(dentry
->d_sb
, "xattr-20003",
205 "Corrupted directory: xattr %s listed but "
206 "not found for file %s.\n",
207 dentry
->d_name
.name
, dbuf
->xadir
->d_name
.name
);
212 dbuf
->dentries
[dbuf
->count
++] = dentry
;
217 cleanup_dentry_buf(struct reiserfs_dentry_buf
*buf
)
220 for (i
= 0; i
< buf
->count
; i
++)
221 if (buf
->dentries
[i
])
222 dput(buf
->dentries
[i
]);
225 static int reiserfs_for_each_xattr(struct inode
*inode
,
226 int (*action
)(struct dentry
*, void *),
232 struct reiserfs_dentry_buf buf
= {
236 /* Skip out, an xattr has no xattrs associated with it */
237 if (IS_PRIVATE(inode
) || get_inode_sd_version(inode
) == STAT_DATA_V1
)
240 reiserfs_write_unlock(inode
->i_sb
);
241 dir
= open_xa_dir(inode
, XATTR_REPLACE
);
244 reiserfs_write_lock(inode
->i_sb
);
246 } else if (!dir
->d_inode
) {
248 reiserfs_write_lock(inode
->i_sb
);
252 mutex_lock_nested(&dir
->d_inode
->i_mutex
, I_MUTEX_XATTR
);
254 reiserfs_write_lock(inode
->i_sb
);
257 err
= reiserfs_readdir_dentry(dir
, &buf
, fill_with_dentries
, &pos
);
258 while ((err
== 0 || err
== -ENOSPC
) && buf
.count
) {
261 for (i
= 0; i
< buf
.count
&& buf
.dentries
[i
]; i
++) {
263 struct dentry
*dentry
= buf
.dentries
[i
];
265 if (err
== 0 && !S_ISDIR(dentry
->d_inode
->i_mode
))
266 lerr
= action(dentry
, data
);
269 buf
.dentries
[i
] = NULL
;
274 err
= reiserfs_readdir_dentry(dir
, &buf
,
275 fill_with_dentries
, &pos
);
277 mutex_unlock(&dir
->d_inode
->i_mutex
);
279 /* Clean up after a failed readdir */
280 cleanup_dentry_buf(&buf
);
283 /* We start a transaction here to avoid a ABBA situation
284 * between the xattr root's i_mutex and the journal lock.
285 * This doesn't incur much additional overhead since the
286 * new transaction will just nest inside the
287 * outer transaction. */
288 int blocks
= JOURNAL_PER_BALANCE_CNT
* 2 + 2 +
289 4 * REISERFS_QUOTA_TRANS_BLOCKS(inode
->i_sb
);
290 struct reiserfs_transaction_handle th
;
291 err
= journal_begin(&th
, inode
->i_sb
, blocks
);
294 reiserfs_mutex_lock_nested_safe(
295 &dir
->d_parent
->d_inode
->i_mutex
,
296 I_MUTEX_XATTR
, inode
->i_sb
);
297 err
= action(dir
, data
);
298 jerror
= journal_end(&th
, inode
->i_sb
, blocks
);
299 mutex_unlock(&dir
->d_parent
->d_inode
->i_mutex
);
306 /* -ENODATA isn't an error */
312 static int delete_one_xattr(struct dentry
*dentry
, void *data
)
314 struct inode
*dir
= dentry
->d_parent
->d_inode
;
316 /* This is the xattr dir, handle specially. */
317 if (S_ISDIR(dentry
->d_inode
->i_mode
))
318 return xattr_rmdir(dir
, dentry
);
320 return xattr_unlink(dir
, dentry
);
323 static int chown_one_xattr(struct dentry
*dentry
, void *data
)
325 struct iattr
*attrs
= data
;
326 return reiserfs_setattr(dentry
, attrs
);
329 /* No i_mutex, but the inode is unconnected. */
330 int reiserfs_delete_xattrs(struct inode
*inode
)
332 int err
= reiserfs_for_each_xattr(inode
, delete_one_xattr
, NULL
);
334 reiserfs_warning(inode
->i_sb
, "jdm-20004",
335 "Couldn't delete all xattrs (%d)\n", err
);
339 /* inode->i_mutex: down */
340 int reiserfs_chown_xattrs(struct inode
*inode
, struct iattr
*attrs
)
342 int err
= reiserfs_for_each_xattr(inode
, chown_one_xattr
, attrs
);
344 reiserfs_warning(inode
->i_sb
, "jdm-20007",
345 "Couldn't chown all xattrs (%d)\n", err
);
349 #ifdef CONFIG_REISERFS_FS_XATTR
350 /* Returns a dentry corresponding to a specific extended attribute file
351 * for the inode. If flags allow, the file is created. Otherwise, a
352 * valid or negative dentry, or an error is returned. */
353 static struct dentry
*xattr_lookup(struct inode
*inode
, const char *name
,
356 struct dentry
*xadir
, *xafile
;
359 xadir
= open_xa_dir(inode
, flags
);
361 return ERR_CAST(xadir
);
363 mutex_lock_nested(&xadir
->d_inode
->i_mutex
, I_MUTEX_XATTR
);
364 xafile
= lookup_one_len(name
, xadir
, strlen(name
));
365 if (IS_ERR(xafile
)) {
366 err
= PTR_ERR(xafile
);
370 if (xafile
->d_inode
&& (flags
& XATTR_CREATE
))
373 if (!xafile
->d_inode
) {
375 if (xattr_may_create(flags
))
376 err
= xattr_create(xadir
->d_inode
, xafile
,
383 mutex_unlock(&xadir
->d_inode
->i_mutex
);
390 /* Internal operations on file data */
391 static inline void reiserfs_put_page(struct page
*page
)
394 page_cache_release(page
);
397 static struct page
*reiserfs_get_page(struct inode
*dir
, size_t n
)
399 struct address_space
*mapping
= dir
->i_mapping
;
401 /* We can deadlock if we try to free dentries,
402 and an unlink/rmdir has just occured - GFP_NOFS avoids this */
403 mapping_set_gfp_mask(mapping
, GFP_NOFS
);
404 page
= read_mapping_page(mapping
, n
>> PAGE_CACHE_SHIFT
, NULL
);
413 reiserfs_put_page(page
);
414 return ERR_PTR(-EIO
);
417 static inline __u32
xattr_hash(const char *msg
, int len
)
419 return csum_partial(msg
, len
, 0);
422 int reiserfs_commit_write(struct file
*f
, struct page
*page
,
423 unsigned from
, unsigned to
);
424 int reiserfs_prepare_write(struct file
*f
, struct page
*page
,
425 unsigned from
, unsigned to
);
427 static void update_ctime(struct inode
*inode
)
429 struct timespec now
= current_fs_time(inode
->i_sb
);
430 if (hlist_unhashed(&inode
->i_hash
) || !inode
->i_nlink
||
431 timespec_equal(&inode
->i_ctime
, &now
))
434 inode
->i_ctime
= CURRENT_TIME_SEC
;
435 mark_inode_dirty(inode
);
438 static int lookup_and_delete_xattr(struct inode
*inode
, const char *name
)
441 struct dentry
*dentry
, *xadir
;
443 xadir
= open_xa_dir(inode
, XATTR_REPLACE
);
445 return PTR_ERR(xadir
);
447 mutex_lock_nested(&xadir
->d_inode
->i_mutex
, I_MUTEX_XATTR
);
448 dentry
= lookup_one_len(name
, xadir
, strlen(name
));
449 if (IS_ERR(dentry
)) {
450 err
= PTR_ERR(dentry
);
454 if (dentry
->d_inode
) {
455 reiserfs_write_lock(inode
->i_sb
);
456 err
= xattr_unlink(xadir
->d_inode
, dentry
);
457 reiserfs_write_unlock(inode
->i_sb
);
463 mutex_unlock(&xadir
->d_inode
->i_mutex
);
469 /* Generic extended attribute operations that can be used by xa plugins */
472 * inode->i_mutex: down
475 reiserfs_xattr_set_handle(struct reiserfs_transaction_handle
*th
,
476 struct inode
*inode
, const char *name
,
477 const void *buffer
, size_t buffer_size
, int flags
)
480 struct dentry
*dentry
;
484 size_t buffer_pos
= 0;
488 if (get_inode_sd_version(inode
) == STAT_DATA_V1
)
491 reiserfs_write_unlock(inode
->i_sb
);
494 err
= lookup_and_delete_xattr(inode
, name
);
495 reiserfs_write_lock(inode
->i_sb
);
499 dentry
= xattr_lookup(inode
, name
, flags
);
500 if (IS_ERR(dentry
)) {
501 reiserfs_write_lock(inode
->i_sb
);
502 return PTR_ERR(dentry
);
505 down_write(&REISERFS_I(inode
)->i_xattr_sem
);
507 reiserfs_write_lock(inode
->i_sb
);
509 xahash
= xattr_hash(buffer
, buffer_size
);
510 while (buffer_pos
< buffer_size
|| buffer_pos
== 0) {
513 size_t page_offset
= (file_pos
& (PAGE_CACHE_SIZE
- 1));
514 if (buffer_size
- buffer_pos
> PAGE_CACHE_SIZE
)
515 chunk
= PAGE_CACHE_SIZE
;
517 chunk
= buffer_size
- buffer_pos
;
519 page
= reiserfs_get_page(dentry
->d_inode
, file_pos
);
526 data
= page_address(page
);
529 struct reiserfs_xattr_header
*rxh
;
530 skip
= file_pos
= sizeof(struct reiserfs_xattr_header
);
531 if (chunk
+ skip
> PAGE_CACHE_SIZE
)
532 chunk
= PAGE_CACHE_SIZE
- skip
;
533 rxh
= (struct reiserfs_xattr_header
*)data
;
534 rxh
->h_magic
= cpu_to_le32(REISERFS_XATTR_MAGIC
);
535 rxh
->h_hash
= cpu_to_le32(xahash
);
538 err
= reiserfs_prepare_write(NULL
, page
, page_offset
,
539 page_offset
+ chunk
+ skip
);
542 memcpy(data
+ skip
, buffer
+ buffer_pos
, chunk
);
543 err
= reiserfs_commit_write(NULL
, page
, page_offset
,
544 page_offset
+ chunk
+
548 reiserfs_put_page(page
);
552 if (err
|| buffer_size
== 0 || !buffer
)
556 new_size
= buffer_size
+ sizeof(struct reiserfs_xattr_header
);
557 if (!err
&& new_size
< i_size_read(dentry
->d_inode
)) {
558 struct iattr newattrs
= {
559 .ia_ctime
= current_fs_time(inode
->i_sb
),
560 .ia_size
= buffer_size
,
561 .ia_valid
= ATTR_SIZE
| ATTR_CTIME
,
564 reiserfs_write_unlock(inode
->i_sb
);
565 mutex_lock_nested(&dentry
->d_inode
->i_mutex
, I_MUTEX_XATTR
);
566 down_write(&dentry
->d_inode
->i_alloc_sem
);
567 reiserfs_write_lock(inode
->i_sb
);
569 err
= reiserfs_setattr(dentry
, &newattrs
);
570 up_write(&dentry
->d_inode
->i_alloc_sem
);
571 mutex_unlock(&dentry
->d_inode
->i_mutex
);
575 up_write(&REISERFS_I(inode
)->i_xattr_sem
);
580 /* We need to start a transaction to maintain lock ordering */
581 int reiserfs_xattr_set(struct inode
*inode
, const char *name
,
582 const void *buffer
, size_t buffer_size
, int flags
)
585 struct reiserfs_transaction_handle th
;
587 size_t jbegin_count
= reiserfs_xattr_nblocks(inode
, buffer_size
);
589 if (!(flags
& XATTR_REPLACE
))
590 jbegin_count
+= reiserfs_xattr_jcreate_nblocks(inode
);
592 reiserfs_write_lock(inode
->i_sb
);
593 error
= journal_begin(&th
, inode
->i_sb
, jbegin_count
);
595 reiserfs_write_unlock(inode
->i_sb
);
599 error
= reiserfs_xattr_set_handle(&th
, inode
, name
,
600 buffer
, buffer_size
, flags
);
602 error2
= journal_end(&th
, inode
->i_sb
, jbegin_count
);
605 reiserfs_write_unlock(inode
->i_sb
);
611 * inode->i_mutex: down
614 reiserfs_xattr_get(struct inode
*inode
, const char *name
, void *buffer
,
618 struct dentry
*dentry
;
621 size_t buffer_pos
= 0;
628 /* We can't have xattrs attached to v1 items since they don't have
629 * generation numbers */
630 if (get_inode_sd_version(inode
) == STAT_DATA_V1
)
633 dentry
= xattr_lookup(inode
, name
, XATTR_REPLACE
);
634 if (IS_ERR(dentry
)) {
635 err
= PTR_ERR(dentry
);
639 down_read(&REISERFS_I(inode
)->i_xattr_sem
);
641 isize
= i_size_read(dentry
->d_inode
);
643 /* Just return the size needed */
644 if (buffer
== NULL
) {
645 err
= isize
- sizeof(struct reiserfs_xattr_header
);
649 if (buffer_size
< isize
- sizeof(struct reiserfs_xattr_header
)) {
654 while (file_pos
< isize
) {
658 if (isize
- file_pos
> PAGE_CACHE_SIZE
)
659 chunk
= PAGE_CACHE_SIZE
;
661 chunk
= isize
- file_pos
;
663 page
= reiserfs_get_page(dentry
->d_inode
, file_pos
);
670 data
= page_address(page
);
672 struct reiserfs_xattr_header
*rxh
=
673 (struct reiserfs_xattr_header
*)data
;
674 skip
= file_pos
= sizeof(struct reiserfs_xattr_header
);
676 /* Magic doesn't match up.. */
677 if (rxh
->h_magic
!= cpu_to_le32(REISERFS_XATTR_MAGIC
)) {
679 reiserfs_put_page(page
);
680 reiserfs_warning(inode
->i_sb
, "jdm-20001",
681 "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
)) !=
700 reiserfs_warning(inode
->i_sb
, "jdm-20002",
701 "Invalid hash for xattr (%s) associated "
702 "with %k", name
, INODE_PKEY(inode
));
707 up_read(&REISERFS_I(inode
)->i_xattr_sem
);
715 * In order to implement different sets of xattr operations for each xattr
716 * prefix with the generic xattr API, a filesystem should create a
717 * null-terminated array of struct xattr_handler (one for each prefix) and
718 * hang a pointer to it off of the s_xattr field of the superblock.
720 * The generic_fooxattr() functions will use this list to dispatch xattr
721 * operations to the correct xattr_handler.
723 #define for_each_xattr_handler(handlers, handler) \
724 for ((handler) = *(handlers)++; \
726 (handler) = *(handlers)++)
728 /* This is the implementation for the xattr plugin infrastructure */
729 static inline struct xattr_handler
*
730 find_xattr_handler_prefix(struct xattr_handler
**handlers
,
733 struct xattr_handler
*xah
;
738 for_each_xattr_handler(handlers
, xah
) {
739 if (strncmp(xah
->prefix
, name
, strlen(xah
->prefix
)) == 0)
748 * Inode operation getxattr()
751 reiserfs_getxattr(struct dentry
* dentry
, const char *name
, void *buffer
,
754 struct xattr_handler
*handler
;
756 handler
= find_xattr_handler_prefix(dentry
->d_sb
->s_xattr
, name
);
758 if (!handler
|| get_inode_sd_version(dentry
->d_inode
) == STAT_DATA_V1
)
761 return handler
->get(dentry
, name
, buffer
, size
, handler
->flags
);
765 * Inode operation setxattr()
767 * dentry->d_inode->i_mutex down
770 reiserfs_setxattr(struct dentry
*dentry
, const char *name
, const void *value
,
771 size_t size
, int flags
)
773 struct xattr_handler
*handler
;
775 handler
= find_xattr_handler_prefix(dentry
->d_sb
->s_xattr
, name
);
777 if (!handler
|| get_inode_sd_version(dentry
->d_inode
) == STAT_DATA_V1
)
780 return handler
->set(dentry
, name
, value
, size
, flags
, handler
->flags
);
784 * Inode operation removexattr()
786 * dentry->d_inode->i_mutex down
788 int reiserfs_removexattr(struct dentry
*dentry
, const char *name
)
790 struct xattr_handler
*handler
;
791 handler
= find_xattr_handler_prefix(dentry
->d_sb
->s_xattr
, name
);
793 if (!handler
|| get_inode_sd_version(dentry
->d_inode
) == STAT_DATA_V1
)
796 return handler
->set(dentry
, name
, NULL
, 0, XATTR_REPLACE
, handler
->flags
);
799 struct listxattr_buf
{
803 struct dentry
*dentry
;
806 static int listxattr_filler(void *buf
, const char *name
, int namelen
,
807 loff_t offset
, u64 ino
, unsigned int d_type
)
809 struct listxattr_buf
*b
= (struct listxattr_buf
*)buf
;
811 if (name
[0] != '.' ||
812 (namelen
!= 1 && (name
[1] != '.' || namelen
!= 2))) {
813 struct xattr_handler
*handler
;
814 handler
= find_xattr_handler_prefix(b
->dentry
->d_sb
->s_xattr
,
816 if (!handler
) /* Unsupported xattr name */
819 size
= handler
->list(b
->dentry
, b
->buf
+ b
->pos
,
820 b
->size
, name
, namelen
,
825 size
= handler
->list(b
->dentry
, NULL
, 0, name
,
826 namelen
, handler
->flags
);
835 * Inode operation listxattr()
837 * We totally ignore the generic listxattr here because it would be stupid
838 * not to. Since the xattrs are organized in a directory, we can just
839 * readdir to find them.
841 ssize_t
reiserfs_listxattr(struct dentry
* dentry
, char *buffer
, size_t size
)
846 struct listxattr_buf buf
= {
849 .size
= buffer
? size
: 0,
852 if (!dentry
->d_inode
)
855 if (!dentry
->d_sb
->s_xattr
||
856 get_inode_sd_version(dentry
->d_inode
) == STAT_DATA_V1
)
859 dir
= open_xa_dir(dentry
->d_inode
, XATTR_REPLACE
);
863 err
= 0; /* Not an error if there aren't any xattrs */
867 mutex_lock_nested(&dir
->d_inode
->i_mutex
, I_MUTEX_XATTR
);
868 err
= reiserfs_readdir_dentry(dir
, &buf
, listxattr_filler
, &pos
);
869 mutex_unlock(&dir
->d_inode
->i_mutex
);
879 static int reiserfs_check_acl(struct inode
*inode
, int mask
)
881 struct posix_acl
*acl
;
882 int error
= -EAGAIN
; /* do regular unix permission checks by default */
884 acl
= reiserfs_get_acl(inode
, ACL_TYPE_ACCESS
);
888 error
= posix_acl_permission(inode
, acl
, mask
);
889 posix_acl_release(acl
);
890 } else if (PTR_ERR(acl
) != -ENODATA
)
891 error
= PTR_ERR(acl
);
897 static int create_privroot(struct dentry
*dentry
)
900 struct inode
*inode
= dentry
->d_parent
->d_inode
;
901 WARN_ON_ONCE(!mutex_is_locked(&inode
->i_mutex
));
903 err
= xattr_mkdir(inode
, dentry
, 0700);
904 if (err
|| !dentry
->d_inode
) {
905 reiserfs_warning(dentry
->d_sb
, "jdm-20006",
906 "xattrs/ACLs enabled and couldn't "
907 "find/create .reiserfs_priv. "
912 dentry
->d_inode
->i_flags
|= S_PRIVATE
;
913 reiserfs_info(dentry
->d_sb
, "Created %s - reserved for xattr "
914 "storage.\n", PRIVROOT_NAME
);
920 int __init
reiserfs_xattr_register_handlers(void) { return 0; }
921 void reiserfs_xattr_unregister_handlers(void) {}
922 static int create_privroot(struct dentry
*dentry
) { return 0; }
925 /* Actual operations that are exported to VFS-land */
926 struct xattr_handler
*reiserfs_xattr_handlers
[] = {
927 #ifdef CONFIG_REISERFS_FS_XATTR
928 &reiserfs_xattr_user_handler
,
929 &reiserfs_xattr_trusted_handler
,
931 #ifdef CONFIG_REISERFS_FS_SECURITY
932 &reiserfs_xattr_security_handler
,
934 #ifdef CONFIG_REISERFS_FS_POSIX_ACL
935 &reiserfs_posix_acl_access_handler
,
936 &reiserfs_posix_acl_default_handler
,
941 static int xattr_mount_check(struct super_block
*s
)
943 /* We need generation numbers to ensure that the oid mapping is correct
944 * v3.5 filesystems don't have them. */
945 if (old_format_only(s
)) {
946 if (reiserfs_xattrs_optional(s
)) {
947 /* Old format filesystem, but optional xattrs have
948 * been enabled. Error out. */
949 reiserfs_warning(s
, "jdm-2005",
950 "xattrs/ACLs not supported "
951 "on pre-v3.6 format filesystems. "
960 int reiserfs_permission(struct inode
*inode
, int mask
)
963 * We don't do permission checks on the internal objects.
964 * Permissions are determined by the "owning" object.
966 if (IS_PRIVATE(inode
))
969 #ifdef CONFIG_REISERFS_FS_XATTR
971 * Stat data v1 doesn't support ACLs.
973 if (get_inode_sd_version(inode
) != STAT_DATA_V1
)
974 return generic_permission(inode
, mask
, reiserfs_check_acl
);
976 return generic_permission(inode
, mask
, NULL
);
979 /* This will catch lookups from the fs root to .reiserfs_priv */
981 xattr_lookup_poison(struct dentry
*dentry
, struct qstr
*q1
, struct qstr
*name
)
983 struct dentry
*priv_root
= REISERFS_SB(dentry
->d_sb
)->priv_root
;
984 if (container_of(q1
, struct dentry
, d_name
) == priv_root
)
986 if (q1
->len
== name
->len
&&
987 !memcmp(q1
->name
, name
->name
, name
->len
))
992 static const struct dentry_operations xattr_lookup_poison_ops
= {
993 .d_compare
= xattr_lookup_poison
,
996 int reiserfs_lookup_privroot(struct super_block
*s
)
998 struct dentry
*dentry
;
1001 /* If we don't have the privroot located yet - go find it */
1002 reiserfs_mutex_lock_safe(&s
->s_root
->d_inode
->i_mutex
, s
);
1003 dentry
= lookup_one_len(PRIVROOT_NAME
, s
->s_root
,
1004 strlen(PRIVROOT_NAME
));
1005 if (!IS_ERR(dentry
)) {
1006 REISERFS_SB(s
)->priv_root
= dentry
;
1007 if (!reiserfs_expose_privroot(s
))
1008 s
->s_root
->d_op
= &xattr_lookup_poison_ops
;
1009 if (dentry
->d_inode
)
1010 dentry
->d_inode
->i_flags
|= S_PRIVATE
;
1012 err
= PTR_ERR(dentry
);
1013 mutex_unlock(&s
->s_root
->d_inode
->i_mutex
);
1018 /* We need to take a copy of the mount flags since things like
1019 * MS_RDONLY don't get set until *after* we're called.
1020 * mount_flags != mount_options */
1021 int reiserfs_xattr_init(struct super_block
*s
, int mount_flags
)
1024 struct dentry
*privroot
= REISERFS_SB(s
)->priv_root
;
1026 err
= xattr_mount_check(s
);
1030 if (!privroot
->d_inode
&& !(mount_flags
& MS_RDONLY
)) {
1031 reiserfs_mutex_lock_safe(&s
->s_root
->d_inode
->i_mutex
, s
);
1032 err
= create_privroot(REISERFS_SB(s
)->priv_root
);
1033 mutex_unlock(&s
->s_root
->d_inode
->i_mutex
);
1036 if (privroot
->d_inode
) {
1037 s
->s_xattr
= reiserfs_xattr_handlers
;
1038 reiserfs_mutex_lock_safe(&privroot
->d_inode
->i_mutex
, s
);
1039 if (!REISERFS_SB(s
)->xattr_root
) {
1040 struct dentry
*dentry
;
1041 dentry
= lookup_one_len(XAROOT_NAME
, privroot
,
1042 strlen(XAROOT_NAME
));
1043 if (!IS_ERR(dentry
))
1044 REISERFS_SB(s
)->xattr_root
= dentry
;
1046 err
= PTR_ERR(dentry
);
1048 mutex_unlock(&privroot
->d_inode
->i_mutex
);
1053 clear_bit(REISERFS_XATTRS_USER
, &(REISERFS_SB(s
)->s_mount_opt
));
1054 clear_bit(REISERFS_POSIXACL
, &(REISERFS_SB(s
)->s_mount_opt
));
1057 /* The super_block MS_POSIXACL must mirror the (no)acl mount option. */
1058 if (reiserfs_posixacl(s
))
1059 s
->s_flags
|= MS_POSIXACL
;
1061 s
->s_flags
&= ~MS_POSIXACL
;