2 * Copyright (C) International Business Machines Corp., 2000-2004
3 * Copyright (C) Christoph Hellwig, 2002
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <linux/xattr.h>
22 #include <linux/quotaops.h>
23 #include "jfs_incore.h"
24 #include "jfs_superblock.h"
26 #include "jfs_debug.h"
27 #include "jfs_dinode.h"
28 #include "jfs_extent.h"
29 #include "jfs_metapage.h"
30 #include "jfs_xattr.h"
34 * jfs_xattr.c: extended attribute service
40 * Extended attribute lists (jfs_ea_list) consist of an overall size (32 bit
41 * value) and a variable (0 or more) number of extended attribute
42 * entries. Each extended attribute entry (jfs_ea) is a <name,value> double
43 * where <name> is constructed from a null-terminated ascii string
44 * (1 ... 255 bytes in the name) and <value> is arbitrary 8 bit data
45 * (1 ... 65535 bytes). The in-memory format is
47 * 0 1 2 4 4 + namelen + 1
48 * +-------+--------+--------+----------------+-------------------+
49 * | Flags | Name | Value | Name String \0 | Data . . . . |
50 * | | Length | Length | | |
51 * +-------+--------+--------+----------------+-------------------+
53 * A jfs_ea_list then is structured as
55 * 0 4 4 + EA_SIZE(ea1)
56 * +------------+-------------------+--------------------+-----
57 * | Overall EA | First FEA Element | Second FEA Element | .....
59 * +------------+-------------------+--------------------+-----
63 * FEALISTs are stored on disk using blocks allocated by dbAlloc() and
64 * written directly. An EA list may be in-lined in the inode if there is
65 * sufficient room available.
69 int flag
; /* Indicates what storage xattr points to */
70 int max_size
; /* largest xattr that fits in current buffer */
71 dxd_t new_ea
; /* dxd to replace ea when modifying xattr */
72 struct metapage
*mp
; /* metapage containing ea list */
73 struct jfs_ea_list
*xattr
; /* buffer containing ea list */
77 * ea_buffer.flag values
79 #define EA_INLINE 0x0001
80 #define EA_EXTENT 0x0002
82 #define EA_MALLOC 0x0008
85 #define XATTR_SYSTEM_PREFIX "system."
86 #define XATTR_SYSTEM_PREFIX_LEN (sizeof (XATTR_SYSTEM_PREFIX) - 1)
88 #define XATTR_USER_PREFIX "user."
89 #define XATTR_USER_PREFIX_LEN (sizeof (XATTR_USER_PREFIX) - 1)
91 #define XATTR_OS2_PREFIX "os2."
92 #define XATTR_OS2_PREFIX_LEN (sizeof (XATTR_OS2_PREFIX) - 1)
94 /* XATTR_SECURITY_PREFIX is defined in include/linux/xattr.h */
95 #define XATTR_SECURITY_PREFIX_LEN (sizeof (XATTR_SECURITY_PREFIX) - 1)
97 #define XATTR_TRUSTED_PREFIX "trusted."
98 #define XATTR_TRUSTED_PREFIX_LEN (sizeof (XATTR_TRUSTED_PREFIX) - 1)
101 * These three routines are used to recognize on-disk extended attributes
102 * that are in a recognized namespace. If the attribute is not recognized,
103 * "os2." is prepended to the name
105 static inline int is_os2_xattr(struct jfs_ea
*ea
)
108 * Check for "system."
110 if ((ea
->namelen
>= XATTR_SYSTEM_PREFIX_LEN
) &&
111 !strncmp(ea
->name
, XATTR_SYSTEM_PREFIX
, XATTR_SYSTEM_PREFIX_LEN
))
116 if ((ea
->namelen
>= XATTR_USER_PREFIX_LEN
) &&
117 !strncmp(ea
->name
, XATTR_USER_PREFIX
, XATTR_USER_PREFIX_LEN
))
120 * Check for "security."
122 if ((ea
->namelen
>= XATTR_SECURITY_PREFIX_LEN
) &&
123 !strncmp(ea
->name
, XATTR_SECURITY_PREFIX
,
124 XATTR_SECURITY_PREFIX_LEN
))
127 * Check for "trusted."
129 if ((ea
->namelen
>= XATTR_TRUSTED_PREFIX_LEN
) &&
130 !strncmp(ea
->name
, XATTR_TRUSTED_PREFIX
, XATTR_TRUSTED_PREFIX_LEN
))
133 * Add any other valid namespace prefixes here
137 * We assume it's OS/2's flat namespace
142 static inline int name_size(struct jfs_ea
*ea
)
144 if (is_os2_xattr(ea
))
145 return ea
->namelen
+ XATTR_OS2_PREFIX_LEN
;
150 static inline int copy_name(char *buffer
, struct jfs_ea
*ea
)
152 int len
= ea
->namelen
;
154 if (is_os2_xattr(ea
)) {
155 memcpy(buffer
, XATTR_OS2_PREFIX
, XATTR_OS2_PREFIX_LEN
);
156 buffer
+= XATTR_OS2_PREFIX_LEN
;
157 len
+= XATTR_OS2_PREFIX_LEN
;
159 memcpy(buffer
, ea
->name
, ea
->namelen
);
160 buffer
[ea
->namelen
] = 0;
165 /* Forward references */
166 static void ea_release(struct inode
*inode
, struct ea_buffer
*ea_buf
);
169 * NAME: ea_write_inline
171 * FUNCTION: Attempt to write an EA inline if area is available
174 * Already verified that the specified EA is small enough to fit inline
178 * ealist - EA list pointer
179 * size - size of ealist in bytes
180 * ea - dxd_t structure to be filled in with necessary EA information
181 * if we successfully copy the EA inline
184 * Checks if the inode's inline area is available. If so, copies EA inline
185 * and sets <ea> fields appropriately. Otherwise, returns failure, EA will
186 * have to be put into an extent.
188 * RETURNS: 0 for successful copy to inline area; -1 if area not available
190 static int ea_write_inline(struct inode
*ip
, struct jfs_ea_list
*ealist
,
191 int size
, dxd_t
* ea
)
193 struct jfs_inode_info
*ji
= JFS_IP(ip
);
196 * Make sure we have an EA -- the NULL EA list is valid, but you
199 if (ealist
&& size
> sizeof (struct jfs_ea_list
)) {
200 assert(size
<= sizeof (ji
->i_inline_ea
));
203 * See if the space is available or if it is already being
204 * used for an inline EA.
206 if (!(ji
->mode2
& INLINEEA
) && !(ji
->ea
.flag
& DXD_INLINE
))
212 memcpy(ji
->i_inline_ea
, ealist
, size
);
213 ea
->flag
= DXD_INLINE
;
214 ji
->mode2
&= ~INLINEEA
;
221 /* Free up INLINE area */
222 if (ji
->ea
.flag
& DXD_INLINE
)
223 ji
->mode2
|= INLINEEA
;
232 * FUNCTION: Write an EA for an inode
234 * PRE CONDITIONS: EA has been verified
238 * ealist - EA list pointer
239 * size - size of ealist in bytes
240 * ea - dxd_t structure to be filled in appropriately with where the
243 * NOTES: Will write EA inline if able to, otherwise allocates blocks for an
244 * extent and synchronously writes it to those blocks.
246 * RETURNS: 0 for success; Anything else indicates failure
248 static int ea_write(struct inode
*ip
, struct jfs_ea_list
*ealist
, int size
,
251 struct super_block
*sb
= ip
->i_sb
;
252 struct jfs_inode_info
*ji
= JFS_IP(ip
);
253 struct jfs_sb_info
*sbi
= JFS_SBI(sb
);
263 * Quick check to see if this is an in-linable EA. Short EAs
264 * and empty EAs are all in-linable, provided the space exists.
266 if (!ealist
|| size
<= sizeof (ji
->i_inline_ea
)) {
267 if (!ea_write_inline(ip
, ealist
, size
, ea
))
271 /* figure out how many blocks we need */
272 nblocks
= (size
+ (sb
->s_blocksize
- 1)) >> sb
->s_blocksize_bits
;
274 /* Allocate new blocks to quota. */
275 if (DQUOT_ALLOC_BLOCK(ip
, nblocks
)) {
279 rc
= dbAlloc(ip
, INOHINT(ip
), nblocks
, &blkno
);
281 /*Rollback quota allocation. */
282 DQUOT_FREE_BLOCK(ip
, nblocks
);
287 * Now have nblocks worth of storage to stuff into the FEALIST.
288 * loop over the FEALIST copying data into the buffer one page at
291 cp
= (char *) ealist
;
293 for (i
= 0; i
< nblocks
; i
+= sbi
->nbperpage
) {
295 * Determine how many bytes for this request, and round up to
296 * the nearest aggregate block size
298 nb
= min(PSIZE
, nbytes
);
300 ((((nb
+ sb
->s_blocksize
- 1)) >> sb
->s_blocksize_bits
))
301 << sb
->s_blocksize_bits
;
303 if (!(mp
= get_metapage(ip
, blkno
+ i
, bytes_to_write
, 1))) {
308 memcpy(mp
->data
, cp
, nb
);
311 * We really need a way to propagate errors for
312 * forced writes like this one. --hch
314 * (__write_metapage => release_metapage => flush_metapage)
317 if ((rc
= flush_metapage(mp
))) {
319 * the write failed -- this means that the buffer
320 * is still assigned and the blocks are not being
321 * used. this seems like the best error recovery
334 ea
->flag
= DXD_EXTENT
;
335 DXDsize(ea
, le32_to_cpu(ealist
->size
));
336 DXDlength(ea
, nblocks
);
337 DXDaddress(ea
, blkno
);
339 /* Free up INLINE area */
340 if (ji
->ea
.flag
& DXD_INLINE
)
341 ji
->mode2
|= INLINEEA
;
346 /* Rollback quota allocation. */
347 DQUOT_FREE_BLOCK(ip
, nblocks
);
349 dbFree(ip
, blkno
, nblocks
);
354 * NAME: ea_read_inline
356 * FUNCTION: Read an inlined EA into user's buffer
360 * ealist - Pointer to buffer to fill in with EA
364 static int ea_read_inline(struct inode
*ip
, struct jfs_ea_list
*ealist
)
366 struct jfs_inode_info
*ji
= JFS_IP(ip
);
367 int ea_size
= sizeDXD(&ji
->ea
);
375 if ((sizeDXD(&ji
->ea
) > sizeof (ji
->i_inline_ea
)))
377 if (le32_to_cpu(((struct jfs_ea_list
*) &ji
->i_inline_ea
)->size
)
381 memcpy(ealist
, ji
->i_inline_ea
, ea_size
);
388 * FUNCTION: copy EA data into user's buffer
392 * ealist - Pointer to buffer to fill in with EA
394 * NOTES: If EA is inline calls ea_read_inline() to copy EA.
396 * RETURNS: 0 for success; other indicates failure
398 static int ea_read(struct inode
*ip
, struct jfs_ea_list
*ealist
)
400 struct super_block
*sb
= ip
->i_sb
;
401 struct jfs_inode_info
*ji
= JFS_IP(ip
);
402 struct jfs_sb_info
*sbi
= JFS_SBI(sb
);
405 char *cp
= (char *) ealist
;
411 /* quick check for in-line EA */
412 if (ji
->ea
.flag
& DXD_INLINE
)
413 return ea_read_inline(ip
, ealist
);
415 nbytes
= sizeDXD(&ji
->ea
);
417 jfs_error(sb
, "ea_read: nbytes is 0");
422 * Figure out how many blocks were allocated when this EA list was
423 * originally written to disk.
425 nblocks
= lengthDXD(&ji
->ea
) << sbi
->l2nbperpage
;
426 blkno
= addressDXD(&ji
->ea
) << sbi
->l2nbperpage
;
429 * I have found the disk blocks which were originally used to store
430 * the FEALIST. now i loop over each contiguous block copying the
431 * data into the buffer.
433 for (i
= 0; i
< nblocks
; i
+= sbi
->nbperpage
) {
435 * Determine how many bytes for this request, and round up to
436 * the nearest aggregate block size
438 nb
= min(PSIZE
, nbytes
);
440 ((((nb
+ sb
->s_blocksize
- 1)) >> sb
->s_blocksize_bits
))
441 << sb
->s_blocksize_bits
;
443 if (!(mp
= read_metapage(ip
, blkno
+ i
, bytes_to_read
, 1)))
446 memcpy(cp
, mp
->data
, nb
);
447 release_metapage(mp
);
459 * FUNCTION: Returns buffer containing existing extended attributes.
460 * The size of the buffer will be the larger of the existing
461 * attributes size, or min_size.
463 * The buffer, which may be inlined in the inode or in the
464 * page cache must be release by calling ea_release or ea_put
467 * inode - Inode pointer
468 * ea_buf - Structure to be populated with ealist and its metadata
469 * min_size- minimum size of buffer to be returned
471 * RETURNS: 0 for success; Other indicates failure
473 static int ea_get(struct inode
*inode
, struct ea_buffer
*ea_buf
, int min_size
)
475 struct jfs_inode_info
*ji
= JFS_IP(inode
);
476 struct super_block
*sb
= inode
->i_sb
;
478 int ea_size
= sizeDXD(&ji
->ea
);
479 int blocks_needed
, current_blocks
;
482 int quota_allocation
= 0;
484 /* When fsck.jfs clears a bad ea, it doesn't clear the size */
485 if (ji
->ea
.flag
== 0)
491 ea_buf
->max_size
= 0;
492 ea_buf
->xattr
= NULL
;
495 if ((min_size
<= sizeof (ji
->i_inline_ea
)) &&
496 (ji
->mode2
& INLINEEA
)) {
497 ea_buf
->flag
= EA_INLINE
| EA_NEW
;
498 ea_buf
->max_size
= sizeof (ji
->i_inline_ea
);
499 ea_buf
->xattr
= (struct jfs_ea_list
*) ji
->i_inline_ea
;
500 DXDlength(&ea_buf
->new_ea
, 0);
501 DXDaddress(&ea_buf
->new_ea
, 0);
502 ea_buf
->new_ea
.flag
= DXD_INLINE
;
503 DXDsize(&ea_buf
->new_ea
, min_size
);
507 } else if (ji
->ea
.flag
& DXD_INLINE
) {
508 if (min_size
<= sizeof (ji
->i_inline_ea
)) {
509 ea_buf
->flag
= EA_INLINE
;
510 ea_buf
->max_size
= sizeof (ji
->i_inline_ea
);
511 ea_buf
->xattr
= (struct jfs_ea_list
*) ji
->i_inline_ea
;
516 if (!(ji
->ea
.flag
& DXD_EXTENT
)) {
517 jfs_error(sb
, "ea_get: invalid ea.flag)");
520 current_blocks
= (ea_size
+ sb
->s_blocksize
- 1) >>
521 sb
->s_blocksize_bits
;
523 size
= max(min_size
, ea_size
);
527 * To keep the rest of the code simple. Allocate a
528 * contiguous buffer to work with
530 ea_buf
->xattr
= kmalloc(size
, GFP_KERNEL
);
531 if (ea_buf
->xattr
== NULL
)
534 ea_buf
->flag
= EA_MALLOC
;
535 ea_buf
->max_size
= (size
+ sb
->s_blocksize
- 1) &
536 ~(sb
->s_blocksize
- 1);
541 if ((rc
= ea_read(inode
, ea_buf
->xattr
))) {
542 kfree(ea_buf
->xattr
);
543 ea_buf
->xattr
= NULL
;
548 blocks_needed
= (min_size
+ sb
->s_blocksize
- 1) >>
549 sb
->s_blocksize_bits
;
551 if (blocks_needed
> current_blocks
) {
552 /* Allocate new blocks to quota. */
553 if (DQUOT_ALLOC_BLOCK(inode
, blocks_needed
))
556 quota_allocation
= blocks_needed
;
558 rc
= dbAlloc(inode
, INOHINT(inode
), (s64
) blocks_needed
,
563 DXDlength(&ea_buf
->new_ea
, blocks_needed
);
564 DXDaddress(&ea_buf
->new_ea
, blkno
);
565 ea_buf
->new_ea
.flag
= DXD_EXTENT
;
566 DXDsize(&ea_buf
->new_ea
, min_size
);
568 ea_buf
->flag
= EA_EXTENT
| EA_NEW
;
570 ea_buf
->mp
= get_metapage(inode
, blkno
,
571 blocks_needed
<< sb
->s_blocksize_bits
,
573 if (ea_buf
->mp
== NULL
) {
574 dbFree(inode
, blkno
, (s64
) blocks_needed
);
578 ea_buf
->xattr
= ea_buf
->mp
->data
;
579 ea_buf
->max_size
= (min_size
+ sb
->s_blocksize
- 1) &
580 ~(sb
->s_blocksize
- 1);
583 if ((rc
= ea_read(inode
, ea_buf
->xattr
))) {
584 discard_metapage(ea_buf
->mp
);
585 dbFree(inode
, blkno
, (s64
) blocks_needed
);
590 ea_buf
->flag
= EA_EXTENT
;
591 ea_buf
->mp
= read_metapage(inode
, addressDXD(&ji
->ea
),
592 lengthDXD(&ji
->ea
) << sb
->s_blocksize_bits
,
594 if (ea_buf
->mp
== NULL
) {
598 ea_buf
->xattr
= ea_buf
->mp
->data
;
599 ea_buf
->max_size
= (ea_size
+ sb
->s_blocksize
- 1) &
600 ~(sb
->s_blocksize
- 1);
603 if (EALIST_SIZE(ea_buf
->xattr
) != ea_size
) {
604 printk(KERN_ERR
"ea_get: invalid extended attribute\n");
605 dump_mem("xattr", ea_buf
->xattr
, ea_size
);
606 ea_release(inode
, ea_buf
);
614 /* Rollback quota allocation */
615 if (quota_allocation
)
616 DQUOT_FREE_BLOCK(inode
, quota_allocation
);
621 static void ea_release(struct inode
*inode
, struct ea_buffer
*ea_buf
)
623 if (ea_buf
->flag
& EA_MALLOC
)
624 kfree(ea_buf
->xattr
);
625 else if (ea_buf
->flag
& EA_EXTENT
) {
627 release_metapage(ea_buf
->mp
);
629 if (ea_buf
->flag
& EA_NEW
)
630 dbFree(inode
, addressDXD(&ea_buf
->new_ea
),
631 lengthDXD(&ea_buf
->new_ea
));
635 static int ea_put(struct inode
*inode
, struct ea_buffer
*ea_buf
, int new_size
)
637 struct jfs_inode_info
*ji
= JFS_IP(inode
);
638 unsigned long old_blocks
, new_blocks
;
643 ea_release(inode
, ea_buf
);
645 } else if (ea_buf
->flag
& EA_INLINE
) {
646 assert(new_size
<= sizeof (ji
->i_inline_ea
));
647 ji
->mode2
&= ~INLINEEA
;
648 ea_buf
->new_ea
.flag
= DXD_INLINE
;
649 DXDsize(&ea_buf
->new_ea
, new_size
);
650 DXDaddress(&ea_buf
->new_ea
, 0);
651 DXDlength(&ea_buf
->new_ea
, 0);
652 } else if (ea_buf
->flag
& EA_MALLOC
) {
653 rc
= ea_write(inode
, ea_buf
->xattr
, new_size
, &ea_buf
->new_ea
);
654 kfree(ea_buf
->xattr
);
655 } else if (ea_buf
->flag
& EA_NEW
) {
656 /* We have already allocated a new dxd */
657 flush_metapage(ea_buf
->mp
);
659 /* ->xattr must point to original ea's metapage */
660 rc
= ea_write(inode
, ea_buf
->xattr
, new_size
, &ea_buf
->new_ea
);
661 discard_metapage(ea_buf
->mp
);
666 tid
= txBegin(inode
->i_sb
, 0);
667 down(&ji
->commit_sem
);
669 old_blocks
= new_blocks
= 0;
671 if (ji
->ea
.flag
& DXD_EXTENT
) {
672 invalidate_dxd_metapages(inode
, ji
->ea
);
673 old_blocks
= lengthDXD(&ji
->ea
);
677 txEA(tid
, inode
, &ji
->ea
, &ea_buf
->new_ea
);
678 if (ea_buf
->new_ea
.flag
& DXD_EXTENT
) {
679 new_blocks
= lengthDXD(&ea_buf
->new_ea
);
680 if (ji
->ea
.flag
& DXD_INLINE
)
681 ji
->mode2
|= INLINEEA
;
683 ji
->ea
= ea_buf
->new_ea
;
685 txEA(tid
, inode
, &ji
->ea
, NULL
);
686 if (ji
->ea
.flag
& DXD_INLINE
)
687 ji
->mode2
|= INLINEEA
;
692 /* If old blocks exist, they must be removed from quota allocation. */
694 DQUOT_FREE_BLOCK(inode
, old_blocks
);
696 inode
->i_ctime
= CURRENT_TIME
;
697 rc
= txCommit(tid
, 1, &inode
, 0);
705 * can_set_system_xattr
707 * This code is specific to the system.* namespace. It contains policy
708 * which doesn't belong in the main xattr codepath.
710 static int can_set_system_xattr(struct inode
*inode
, const char *name
,
711 const void *value
, size_t value_len
)
713 #ifdef CONFIG_JFS_POSIX_ACL
714 struct posix_acl
*acl
;
717 if ((current
->fsuid
!= inode
->i_uid
) && !capable(CAP_FOWNER
))
721 * XATTR_NAME_ACL_ACCESS is tied to i_mode
723 if (strcmp(name
, XATTR_NAME_ACL_ACCESS
) == 0) {
724 acl
= posix_acl_from_xattr(value
, value_len
);
727 printk(KERN_ERR
"posix_acl_from_xattr returned %d\n",
732 mode_t mode
= inode
->i_mode
;
733 rc
= posix_acl_equiv_mode(acl
, &mode
);
734 posix_acl_release(acl
);
737 "posix_acl_equiv_mode returned %d\n",
741 inode
->i_mode
= mode
;
742 mark_inode_dirty(inode
);
745 * We're changing the ACL. Get rid of the cached one
747 acl
=JFS_IP(inode
)->i_acl
;
748 if (acl
!= JFS_ACL_NOT_CACHED
)
749 posix_acl_release(acl
);
750 JFS_IP(inode
)->i_acl
= JFS_ACL_NOT_CACHED
;
753 } else if (strcmp(name
, XATTR_NAME_ACL_DEFAULT
) == 0) {
754 acl
= posix_acl_from_xattr(value
, value_len
);
757 printk(KERN_ERR
"posix_acl_from_xattr returned %d\n",
761 posix_acl_release(acl
);
764 * We're changing the default ACL. Get rid of the cached one
766 acl
=JFS_IP(inode
)->i_default_acl
;
767 if (acl
&& (acl
!= JFS_ACL_NOT_CACHED
))
768 posix_acl_release(acl
);
769 JFS_IP(inode
)->i_default_acl
= JFS_ACL_NOT_CACHED
;
773 #endif /* CONFIG_JFS_POSIX_ACL */
777 static int can_set_xattr(struct inode
*inode
, const char *name
,
778 const void *value
, size_t value_len
)
780 if (IS_RDONLY(inode
))
783 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
) || S_ISLNK(inode
->i_mode
))
786 if(strncmp(name
, XATTR_SYSTEM_PREFIX
, XATTR_SYSTEM_PREFIX_LEN
) == 0)
790 return can_set_system_xattr(inode
, name
, value
, value_len
);
792 if(strncmp(name
, XATTR_TRUSTED_PREFIX
, XATTR_TRUSTED_PREFIX_LEN
) != 0)
793 return (capable(CAP_SYS_ADMIN
) ? 0 : -EPERM
);
795 #ifdef CONFIG_JFS_SECURITY
796 if (strncmp(name
, XATTR_SECURITY_PREFIX
, XATTR_SECURITY_PREFIX_LEN
)
798 return 0; /* Leave it to the security module */
801 if((strncmp(name
, XATTR_USER_PREFIX
, XATTR_USER_PREFIX_LEN
) != 0) &&
802 (strncmp(name
, XATTR_OS2_PREFIX
, XATTR_OS2_PREFIX_LEN
) != 0))
805 if (!S_ISREG(inode
->i_mode
) &&
806 (!S_ISDIR(inode
->i_mode
) || inode
->i_mode
&S_ISVTX
))
809 return permission(inode
, MAY_WRITE
, NULL
);
812 int __jfs_setxattr(struct inode
*inode
, const char *name
, const void *value
,
813 size_t value_len
, int flags
)
815 struct jfs_ea_list
*ealist
;
816 struct jfs_ea
*ea
, *old_ea
= NULL
, *next_ea
= NULL
;
817 struct ea_buffer ea_buf
;
821 int namelen
= strlen(name
);
822 char *os2name
= NULL
;
827 if ((rc
= can_set_xattr(inode
, name
, value
, value_len
)))
830 if (strncmp(name
, XATTR_OS2_PREFIX
, XATTR_OS2_PREFIX_LEN
) == 0) {
831 os2name
= kmalloc(namelen
- XATTR_OS2_PREFIX_LEN
+ 1,
835 strcpy(os2name
, name
+ XATTR_OS2_PREFIX_LEN
);
837 namelen
-= XATTR_OS2_PREFIX_LEN
;
840 down_write(&JFS_IP(inode
)->xattr_sem
);
842 xattr_size
= ea_get(inode
, &ea_buf
, 0);
843 if (xattr_size
< 0) {
849 ealist
= (struct jfs_ea_list
*) ea_buf
.xattr
;
850 new_size
= sizeof (struct jfs_ea_list
);
853 for (ea
= FIRST_EA(ealist
); ea
< END_EALIST(ealist
);
855 if ((namelen
== ea
->namelen
) &&
856 (memcmp(name
, ea
->name
, namelen
) == 0)) {
858 if (flags
& XATTR_CREATE
) {
863 old_ea_size
= EA_SIZE(ea
);
864 next_ea
= NEXT_EA(ea
);
866 new_size
+= EA_SIZE(ea
);
871 if (flags
& XATTR_REPLACE
) {
881 new_size
+= sizeof (struct jfs_ea
) + namelen
+ 1 + value_len
;
883 if (new_size
> ea_buf
.max_size
) {
885 * We need to allocate more space for merged ea list.
886 * We should only have loop to again: once.
888 ea_release(inode
, &ea_buf
);
889 xattr_size
= ea_get(inode
, &ea_buf
, new_size
);
890 if (xattr_size
< 0) {
897 /* Remove old ea of the same name */
899 /* number of bytes following target EA */
900 length
= (char *) END_EALIST(ealist
) - (char *) next_ea
;
902 memmove(old_ea
, next_ea
, length
);
903 xattr_size
-= old_ea_size
;
906 /* Add new entry to the end */
909 /* Completely new ea list */
910 xattr_size
= sizeof (struct jfs_ea_list
);
912 ea
= (struct jfs_ea
*) ((char *) ealist
+ xattr_size
);
914 ea
->namelen
= namelen
;
915 ea
->valuelen
= (cpu_to_le16(value_len
));
916 memcpy(ea
->name
, name
, namelen
);
917 ea
->name
[namelen
] = 0;
919 memcpy(&ea
->name
[namelen
+ 1], value
, value_len
);
920 xattr_size
+= EA_SIZE(ea
);
923 /* DEBUG - If we did this right, these number match */
924 if (xattr_size
!= new_size
) {
926 "jfs_xsetattr: xattr_size = %d, new_size = %d\n",
927 xattr_size
, new_size
);
934 * If we're left with an empty list, there's no ea
936 if (new_size
== sizeof (struct jfs_ea_list
))
939 ealist
->size
= cpu_to_le32(new_size
);
941 rc
= ea_put(inode
, &ea_buf
, new_size
);
945 ea_release(inode
, &ea_buf
);
947 up_write(&JFS_IP(inode
)->xattr_sem
);
955 int jfs_setxattr(struct dentry
*dentry
, const char *name
, const void *value
,
956 size_t value_len
, int flags
)
958 if (value
== NULL
) { /* empty EA, do not remove */
963 return __jfs_setxattr(dentry
->d_inode
, name
, value
, value_len
, flags
);
966 static int can_get_xattr(struct inode
*inode
, const char *name
)
968 #ifdef CONFIG_JFS_SECURITY
969 if(strncmp(name
, XATTR_SECURITY_PREFIX
, XATTR_SECURITY_PREFIX_LEN
) == 0)
973 if(strncmp(name
, XATTR_TRUSTED_PREFIX
, XATTR_TRUSTED_PREFIX_LEN
) == 0)
974 return (capable(CAP_SYS_ADMIN
) ? 0 : -EPERM
);
976 if(strncmp(name
, XATTR_SYSTEM_PREFIX
, XATTR_SYSTEM_PREFIX_LEN
) == 0)
979 return permission(inode
, MAY_READ
, NULL
);
982 ssize_t
__jfs_getxattr(struct inode
*inode
, const char *name
, void *data
,
985 struct jfs_ea_list
*ealist
;
987 struct ea_buffer ea_buf
;
990 int namelen
= strlen(name
);
991 char *os2name
= NULL
;
995 if ((rc
= can_get_xattr(inode
, name
)))
998 if (strncmp(name
, XATTR_OS2_PREFIX
, XATTR_OS2_PREFIX_LEN
) == 0) {
999 os2name
= kmalloc(namelen
- XATTR_OS2_PREFIX_LEN
+ 1,
1003 strcpy(os2name
, name
+ XATTR_OS2_PREFIX_LEN
);
1005 namelen
-= XATTR_OS2_PREFIX_LEN
;
1008 down_read(&JFS_IP(inode
)->xattr_sem
);
1010 xattr_size
= ea_get(inode
, &ea_buf
, 0);
1012 if (xattr_size
< 0) {
1017 if (xattr_size
== 0)
1020 ealist
= (struct jfs_ea_list
*) ea_buf
.xattr
;
1022 /* Find the named attribute */
1023 for (ea
= FIRST_EA(ealist
); ea
< END_EALIST(ealist
); ea
= NEXT_EA(ea
))
1024 if ((namelen
== ea
->namelen
) &&
1025 memcmp(name
, ea
->name
, namelen
) == 0) {
1027 size
= le16_to_cpu(ea
->valuelen
);
1030 else if (size
> buf_size
) {
1034 value
= ((char *) &ea
->name
) + ea
->namelen
+ 1;
1035 memcpy(data
, value
, size
);
1041 ea_release(inode
, &ea_buf
);
1043 up_read(&JFS_IP(inode
)->xattr_sem
);
1051 ssize_t
jfs_getxattr(struct dentry
*dentry
, const char *name
, void *data
,
1056 err
= __jfs_getxattr(dentry
->d_inode
, name
, data
, buf_size
);
1062 * No special permissions are needed to list attributes except for trusted.*
1064 static inline int can_list(struct jfs_ea
*ea
)
1066 return (strncmp(ea
->name
, XATTR_TRUSTED_PREFIX
,
1067 XATTR_TRUSTED_PREFIX_LEN
) ||
1068 capable(CAP_SYS_ADMIN
));
1071 ssize_t
jfs_listxattr(struct dentry
* dentry
, char *data
, size_t buf_size
)
1073 struct inode
*inode
= dentry
->d_inode
;
1077 struct jfs_ea_list
*ealist
;
1079 struct ea_buffer ea_buf
;
1081 down_read(&JFS_IP(inode
)->xattr_sem
);
1083 xattr_size
= ea_get(inode
, &ea_buf
, 0);
1084 if (xattr_size
< 0) {
1089 if (xattr_size
== 0)
1092 ealist
= (struct jfs_ea_list
*) ea_buf
.xattr
;
1094 /* compute required size of list */
1095 for (ea
= FIRST_EA(ealist
); ea
< END_EALIST(ealist
); ea
= NEXT_EA(ea
)) {
1097 size
+= name_size(ea
) + 1;
1103 if (size
> buf_size
) {
1108 /* Copy attribute names to buffer */
1110 for (ea
= FIRST_EA(ealist
); ea
< END_EALIST(ealist
); ea
= NEXT_EA(ea
)) {
1112 int namelen
= copy_name(buffer
, ea
);
1113 buffer
+= namelen
+ 1;
1118 ea_release(inode
, &ea_buf
);
1120 up_read(&JFS_IP(inode
)->xattr_sem
);
1124 int jfs_removexattr(struct dentry
*dentry
, const char *name
)
1126 return __jfs_setxattr(dentry
->d_inode
, name
, NULL
, 0, XATTR_REPLACE
);