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/posix_acl_xattr.h>
23 #include <linux/quotaops.h>
24 #include "jfs_incore.h"
25 #include "jfs_superblock.h"
27 #include "jfs_debug.h"
28 #include "jfs_dinode.h"
29 #include "jfs_extent.h"
30 #include "jfs_metapage.h"
31 #include "jfs_xattr.h"
35 * jfs_xattr.c: extended attribute service
41 * Extended attribute lists (jfs_ea_list) consist of an overall size (32 bit
42 * value) and a variable (0 or more) number of extended attribute
43 * entries. Each extended attribute entry (jfs_ea) is a <name,value> double
44 * where <name> is constructed from a null-terminated ascii string
45 * (1 ... 255 bytes in the name) and <value> is arbitrary 8 bit data
46 * (1 ... 65535 bytes). The in-memory format is
48 * 0 1 2 4 4 + namelen + 1
49 * +-------+--------+--------+----------------+-------------------+
50 * | Flags | Name | Value | Name String \0 | Data . . . . |
51 * | | Length | Length | | |
52 * +-------+--------+--------+----------------+-------------------+
54 * A jfs_ea_list then is structured as
56 * 0 4 4 + EA_SIZE(ea1)
57 * +------------+-------------------+--------------------+-----
58 * | Overall EA | First FEA Element | Second FEA Element | .....
60 * +------------+-------------------+--------------------+-----
64 * FEALISTs are stored on disk using blocks allocated by dbAlloc() and
65 * written directly. An EA list may be in-lined in the inode if there is
66 * sufficient room available.
70 int flag
; /* Indicates what storage xattr points to */
71 int max_size
; /* largest xattr that fits in current buffer */
72 dxd_t new_ea
; /* dxd to replace ea when modifying xattr */
73 struct metapage
*mp
; /* metapage containing ea list */
74 struct jfs_ea_list
*xattr
; /* buffer containing ea list */
78 * ea_buffer.flag values
80 #define EA_INLINE 0x0001
81 #define EA_EXTENT 0x0002
83 #define EA_MALLOC 0x0008
86 #define XATTR_SYSTEM_PREFIX "system."
87 #define XATTR_SYSTEM_PREFIX_LEN (sizeof (XATTR_SYSTEM_PREFIX) - 1)
89 #define XATTR_USER_PREFIX "user."
90 #define XATTR_USER_PREFIX_LEN (sizeof (XATTR_USER_PREFIX) - 1)
92 #define XATTR_OS2_PREFIX "os2."
93 #define XATTR_OS2_PREFIX_LEN (sizeof (XATTR_OS2_PREFIX) - 1)
95 /* XATTR_SECURITY_PREFIX is defined in include/linux/xattr.h */
96 #define XATTR_SECURITY_PREFIX_LEN (sizeof (XATTR_SECURITY_PREFIX) - 1)
98 #define XATTR_TRUSTED_PREFIX "trusted."
99 #define XATTR_TRUSTED_PREFIX_LEN (sizeof (XATTR_TRUSTED_PREFIX) - 1)
102 * These three routines are used to recognize on-disk extended attributes
103 * that are in a recognized namespace. If the attribute is not recognized,
104 * "os2." is prepended to the name
106 static inline int is_os2_xattr(struct jfs_ea
*ea
)
109 * Check for "system."
111 if ((ea
->namelen
>= XATTR_SYSTEM_PREFIX_LEN
) &&
112 !strncmp(ea
->name
, XATTR_SYSTEM_PREFIX
, XATTR_SYSTEM_PREFIX_LEN
))
117 if ((ea
->namelen
>= XATTR_USER_PREFIX_LEN
) &&
118 !strncmp(ea
->name
, XATTR_USER_PREFIX
, XATTR_USER_PREFIX_LEN
))
121 * Check for "security."
123 if ((ea
->namelen
>= XATTR_SECURITY_PREFIX_LEN
) &&
124 !strncmp(ea
->name
, XATTR_SECURITY_PREFIX
,
125 XATTR_SECURITY_PREFIX_LEN
))
128 * Check for "trusted."
130 if ((ea
->namelen
>= XATTR_TRUSTED_PREFIX_LEN
) &&
131 !strncmp(ea
->name
, XATTR_TRUSTED_PREFIX
, XATTR_TRUSTED_PREFIX_LEN
))
134 * Add any other valid namespace prefixes here
138 * We assume it's OS/2's flat namespace
143 static inline int name_size(struct jfs_ea
*ea
)
145 if (is_os2_xattr(ea
))
146 return ea
->namelen
+ XATTR_OS2_PREFIX_LEN
;
151 static inline int copy_name(char *buffer
, struct jfs_ea
*ea
)
153 int len
= ea
->namelen
;
155 if (is_os2_xattr(ea
)) {
156 memcpy(buffer
, XATTR_OS2_PREFIX
, XATTR_OS2_PREFIX_LEN
);
157 buffer
+= XATTR_OS2_PREFIX_LEN
;
158 len
+= XATTR_OS2_PREFIX_LEN
;
160 memcpy(buffer
, ea
->name
, ea
->namelen
);
161 buffer
[ea
->namelen
] = 0;
166 /* Forward references */
167 static void ea_release(struct inode
*inode
, struct ea_buffer
*ea_buf
);
170 * NAME: ea_write_inline
172 * FUNCTION: Attempt to write an EA inline if area is available
175 * Already verified that the specified EA is small enough to fit inline
179 * ealist - EA list pointer
180 * size - size of ealist in bytes
181 * ea - dxd_t structure to be filled in with necessary EA information
182 * if we successfully copy the EA inline
185 * Checks if the inode's inline area is available. If so, copies EA inline
186 * and sets <ea> fields appropriately. Otherwise, returns failure, EA will
187 * have to be put into an extent.
189 * RETURNS: 0 for successful copy to inline area; -1 if area not available
191 static int ea_write_inline(struct inode
*ip
, struct jfs_ea_list
*ealist
,
192 int size
, dxd_t
* ea
)
194 struct jfs_inode_info
*ji
= JFS_IP(ip
);
197 * Make sure we have an EA -- the NULL EA list is valid, but you
200 if (ealist
&& size
> sizeof (struct jfs_ea_list
)) {
201 assert(size
<= sizeof (ji
->i_inline_ea
));
204 * See if the space is available or if it is already being
205 * used for an inline EA.
207 if (!(ji
->mode2
& INLINEEA
) && !(ji
->ea
.flag
& DXD_INLINE
))
213 memcpy(ji
->i_inline_ea
, ealist
, size
);
214 ea
->flag
= DXD_INLINE
;
215 ji
->mode2
&= ~INLINEEA
;
222 /* Free up INLINE area */
223 if (ji
->ea
.flag
& DXD_INLINE
)
224 ji
->mode2
|= INLINEEA
;
233 * FUNCTION: Write an EA for an inode
235 * PRE CONDITIONS: EA has been verified
239 * ealist - EA list pointer
240 * size - size of ealist in bytes
241 * ea - dxd_t structure to be filled in appropriately with where the
244 * NOTES: Will write EA inline if able to, otherwise allocates blocks for an
245 * extent and synchronously writes it to those blocks.
247 * RETURNS: 0 for success; Anything else indicates failure
249 static int ea_write(struct inode
*ip
, struct jfs_ea_list
*ealist
, int size
,
252 struct super_block
*sb
= ip
->i_sb
;
253 struct jfs_inode_info
*ji
= JFS_IP(ip
);
254 struct jfs_sb_info
*sbi
= JFS_SBI(sb
);
264 * Quick check to see if this is an in-linable EA. Short EAs
265 * and empty EAs are all in-linable, provided the space exists.
267 if (!ealist
|| size
<= sizeof (ji
->i_inline_ea
)) {
268 if (!ea_write_inline(ip
, ealist
, size
, ea
))
272 /* figure out how many blocks we need */
273 nblocks
= (size
+ (sb
->s_blocksize
- 1)) >> sb
->s_blocksize_bits
;
275 /* Allocate new blocks to quota. */
276 if (DQUOT_ALLOC_BLOCK(ip
, nblocks
)) {
280 rc
= dbAlloc(ip
, INOHINT(ip
), nblocks
, &blkno
);
282 /*Rollback quota allocation. */
283 DQUOT_FREE_BLOCK(ip
, nblocks
);
288 * Now have nblocks worth of storage to stuff into the FEALIST.
289 * loop over the FEALIST copying data into the buffer one page at
292 cp
= (char *) ealist
;
294 for (i
= 0; i
< nblocks
; i
+= sbi
->nbperpage
) {
296 * Determine how many bytes for this request, and round up to
297 * the nearest aggregate block size
299 nb
= min(PSIZE
, nbytes
);
301 ((((nb
+ sb
->s_blocksize
- 1)) >> sb
->s_blocksize_bits
))
302 << sb
->s_blocksize_bits
;
304 if (!(mp
= get_metapage(ip
, blkno
+ i
, bytes_to_write
, 1))) {
309 memcpy(mp
->data
, cp
, nb
);
312 * We really need a way to propagate errors for
313 * forced writes like this one. --hch
315 * (__write_metapage => release_metapage => flush_metapage)
318 if ((rc
= flush_metapage(mp
))) {
320 * the write failed -- this means that the buffer
321 * is still assigned and the blocks are not being
322 * used. this seems like the best error recovery
335 ea
->flag
= DXD_EXTENT
;
336 DXDsize(ea
, le32_to_cpu(ealist
->size
));
337 DXDlength(ea
, nblocks
);
338 DXDaddress(ea
, blkno
);
340 /* Free up INLINE area */
341 if (ji
->ea
.flag
& DXD_INLINE
)
342 ji
->mode2
|= INLINEEA
;
347 /* Rollback quota allocation. */
348 DQUOT_FREE_BLOCK(ip
, nblocks
);
350 dbFree(ip
, blkno
, nblocks
);
355 * NAME: ea_read_inline
357 * FUNCTION: Read an inlined EA into user's buffer
361 * ealist - Pointer to buffer to fill in with EA
365 static int ea_read_inline(struct inode
*ip
, struct jfs_ea_list
*ealist
)
367 struct jfs_inode_info
*ji
= JFS_IP(ip
);
368 int ea_size
= sizeDXD(&ji
->ea
);
376 if ((sizeDXD(&ji
->ea
) > sizeof (ji
->i_inline_ea
)))
378 if (le32_to_cpu(((struct jfs_ea_list
*) &ji
->i_inline_ea
)->size
)
382 memcpy(ealist
, ji
->i_inline_ea
, ea_size
);
389 * FUNCTION: copy EA data into user's buffer
393 * ealist - Pointer to buffer to fill in with EA
395 * NOTES: If EA is inline calls ea_read_inline() to copy EA.
397 * RETURNS: 0 for success; other indicates failure
399 static int ea_read(struct inode
*ip
, struct jfs_ea_list
*ealist
)
401 struct super_block
*sb
= ip
->i_sb
;
402 struct jfs_inode_info
*ji
= JFS_IP(ip
);
403 struct jfs_sb_info
*sbi
= JFS_SBI(sb
);
406 char *cp
= (char *) ealist
;
412 /* quick check for in-line EA */
413 if (ji
->ea
.flag
& DXD_INLINE
)
414 return ea_read_inline(ip
, ealist
);
416 nbytes
= sizeDXD(&ji
->ea
);
418 jfs_error(sb
, "ea_read: nbytes is 0");
423 * Figure out how many blocks were allocated when this EA list was
424 * originally written to disk.
426 nblocks
= lengthDXD(&ji
->ea
) << sbi
->l2nbperpage
;
427 blkno
= addressDXD(&ji
->ea
) << sbi
->l2nbperpage
;
430 * I have found the disk blocks which were originally used to store
431 * the FEALIST. now i loop over each contiguous block copying the
432 * data into the buffer.
434 for (i
= 0; i
< nblocks
; i
+= sbi
->nbperpage
) {
436 * Determine how many bytes for this request, and round up to
437 * the nearest aggregate block size
439 nb
= min(PSIZE
, nbytes
);
441 ((((nb
+ sb
->s_blocksize
- 1)) >> sb
->s_blocksize_bits
))
442 << sb
->s_blocksize_bits
;
444 if (!(mp
= read_metapage(ip
, blkno
+ i
, bytes_to_read
, 1)))
447 memcpy(cp
, mp
->data
, nb
);
448 release_metapage(mp
);
460 * FUNCTION: Returns buffer containing existing extended attributes.
461 * The size of the buffer will be the larger of the existing
462 * attributes size, or min_size.
464 * The buffer, which may be inlined in the inode or in the
465 * page cache must be release by calling ea_release or ea_put
468 * inode - Inode pointer
469 * ea_buf - Structure to be populated with ealist and its metadata
470 * min_size- minimum size of buffer to be returned
472 * RETURNS: 0 for success; Other indicates failure
474 static int ea_get(struct inode
*inode
, struct ea_buffer
*ea_buf
, int min_size
)
476 struct jfs_inode_info
*ji
= JFS_IP(inode
);
477 struct super_block
*sb
= inode
->i_sb
;
479 int ea_size
= sizeDXD(&ji
->ea
);
480 int blocks_needed
, current_blocks
;
483 int quota_allocation
= 0;
485 /* When fsck.jfs clears a bad ea, it doesn't clear the size */
486 if (ji
->ea
.flag
== 0)
492 ea_buf
->max_size
= 0;
493 ea_buf
->xattr
= NULL
;
496 if ((min_size
<= sizeof (ji
->i_inline_ea
)) &&
497 (ji
->mode2
& INLINEEA
)) {
498 ea_buf
->flag
= EA_INLINE
| EA_NEW
;
499 ea_buf
->max_size
= sizeof (ji
->i_inline_ea
);
500 ea_buf
->xattr
= (struct jfs_ea_list
*) ji
->i_inline_ea
;
501 DXDlength(&ea_buf
->new_ea
, 0);
502 DXDaddress(&ea_buf
->new_ea
, 0);
503 ea_buf
->new_ea
.flag
= DXD_INLINE
;
504 DXDsize(&ea_buf
->new_ea
, min_size
);
508 } else if (ji
->ea
.flag
& DXD_INLINE
) {
509 if (min_size
<= sizeof (ji
->i_inline_ea
)) {
510 ea_buf
->flag
= EA_INLINE
;
511 ea_buf
->max_size
= sizeof (ji
->i_inline_ea
);
512 ea_buf
->xattr
= (struct jfs_ea_list
*) ji
->i_inline_ea
;
517 if (!(ji
->ea
.flag
& DXD_EXTENT
)) {
518 jfs_error(sb
, "ea_get: invalid ea.flag)");
521 current_blocks
= (ea_size
+ sb
->s_blocksize
- 1) >>
522 sb
->s_blocksize_bits
;
524 size
= max(min_size
, ea_size
);
528 * To keep the rest of the code simple. Allocate a
529 * contiguous buffer to work with
531 ea_buf
->xattr
= kmalloc(size
, GFP_KERNEL
);
532 if (ea_buf
->xattr
== NULL
)
535 ea_buf
->flag
= EA_MALLOC
;
536 ea_buf
->max_size
= (size
+ sb
->s_blocksize
- 1) &
537 ~(sb
->s_blocksize
- 1);
542 if ((rc
= ea_read(inode
, ea_buf
->xattr
))) {
543 kfree(ea_buf
->xattr
);
544 ea_buf
->xattr
= NULL
;
549 blocks_needed
= (min_size
+ sb
->s_blocksize
- 1) >>
550 sb
->s_blocksize_bits
;
552 if (blocks_needed
> current_blocks
) {
553 /* Allocate new blocks to quota. */
554 if (DQUOT_ALLOC_BLOCK(inode
, blocks_needed
))
557 quota_allocation
= blocks_needed
;
559 rc
= dbAlloc(inode
, INOHINT(inode
), (s64
) blocks_needed
,
564 DXDlength(&ea_buf
->new_ea
, blocks_needed
);
565 DXDaddress(&ea_buf
->new_ea
, blkno
);
566 ea_buf
->new_ea
.flag
= DXD_EXTENT
;
567 DXDsize(&ea_buf
->new_ea
, min_size
);
569 ea_buf
->flag
= EA_EXTENT
| EA_NEW
;
571 ea_buf
->mp
= get_metapage(inode
, blkno
,
572 blocks_needed
<< sb
->s_blocksize_bits
,
574 if (ea_buf
->mp
== NULL
) {
575 dbFree(inode
, blkno
, (s64
) blocks_needed
);
579 ea_buf
->xattr
= ea_buf
->mp
->data
;
580 ea_buf
->max_size
= (min_size
+ sb
->s_blocksize
- 1) &
581 ~(sb
->s_blocksize
- 1);
584 if ((rc
= ea_read(inode
, ea_buf
->xattr
))) {
585 discard_metapage(ea_buf
->mp
);
586 dbFree(inode
, blkno
, (s64
) blocks_needed
);
591 ea_buf
->flag
= EA_EXTENT
;
592 ea_buf
->mp
= read_metapage(inode
, addressDXD(&ji
->ea
),
593 lengthDXD(&ji
->ea
) << sb
->s_blocksize_bits
,
595 if (ea_buf
->mp
== NULL
) {
599 ea_buf
->xattr
= ea_buf
->mp
->data
;
600 ea_buf
->max_size
= (ea_size
+ sb
->s_blocksize
- 1) &
601 ~(sb
->s_blocksize
- 1);
604 if (EALIST_SIZE(ea_buf
->xattr
) != ea_size
) {
605 printk(KERN_ERR
"ea_get: invalid extended attribute\n");
606 dump_mem("xattr", ea_buf
->xattr
, ea_size
);
607 ea_release(inode
, ea_buf
);
615 /* Rollback quota allocation */
616 if (quota_allocation
)
617 DQUOT_FREE_BLOCK(inode
, quota_allocation
);
622 static void ea_release(struct inode
*inode
, struct ea_buffer
*ea_buf
)
624 if (ea_buf
->flag
& EA_MALLOC
)
625 kfree(ea_buf
->xattr
);
626 else if (ea_buf
->flag
& EA_EXTENT
) {
628 release_metapage(ea_buf
->mp
);
630 if (ea_buf
->flag
& EA_NEW
)
631 dbFree(inode
, addressDXD(&ea_buf
->new_ea
),
632 lengthDXD(&ea_buf
->new_ea
));
636 static int ea_put(struct inode
*inode
, struct ea_buffer
*ea_buf
, int new_size
)
638 struct jfs_inode_info
*ji
= JFS_IP(inode
);
639 unsigned long old_blocks
, new_blocks
;
644 ea_release(inode
, ea_buf
);
646 } else if (ea_buf
->flag
& EA_INLINE
) {
647 assert(new_size
<= sizeof (ji
->i_inline_ea
));
648 ji
->mode2
&= ~INLINEEA
;
649 ea_buf
->new_ea
.flag
= DXD_INLINE
;
650 DXDsize(&ea_buf
->new_ea
, new_size
);
651 DXDaddress(&ea_buf
->new_ea
, 0);
652 DXDlength(&ea_buf
->new_ea
, 0);
653 } else if (ea_buf
->flag
& EA_MALLOC
) {
654 rc
= ea_write(inode
, ea_buf
->xattr
, new_size
, &ea_buf
->new_ea
);
655 kfree(ea_buf
->xattr
);
656 } else if (ea_buf
->flag
& EA_NEW
) {
657 /* We have already allocated a new dxd */
658 flush_metapage(ea_buf
->mp
);
660 /* ->xattr must point to original ea's metapage */
661 rc
= ea_write(inode
, ea_buf
->xattr
, new_size
, &ea_buf
->new_ea
);
662 discard_metapage(ea_buf
->mp
);
667 tid
= txBegin(inode
->i_sb
, 0);
668 down(&ji
->commit_sem
);
670 old_blocks
= new_blocks
= 0;
672 if (ji
->ea
.flag
& DXD_EXTENT
) {
673 invalidate_dxd_metapages(inode
, ji
->ea
);
674 old_blocks
= lengthDXD(&ji
->ea
);
678 txEA(tid
, inode
, &ji
->ea
, &ea_buf
->new_ea
);
679 if (ea_buf
->new_ea
.flag
& DXD_EXTENT
) {
680 new_blocks
= lengthDXD(&ea_buf
->new_ea
);
681 if (ji
->ea
.flag
& DXD_INLINE
)
682 ji
->mode2
|= INLINEEA
;
684 ji
->ea
= ea_buf
->new_ea
;
686 txEA(tid
, inode
, &ji
->ea
, NULL
);
687 if (ji
->ea
.flag
& DXD_INLINE
)
688 ji
->mode2
|= INLINEEA
;
693 /* If old blocks exist, they must be removed from quota allocation. */
695 DQUOT_FREE_BLOCK(inode
, old_blocks
);
697 inode
->i_ctime
= CURRENT_TIME
;
698 rc
= txCommit(tid
, 1, &inode
, 0);
706 * can_set_system_xattr
708 * This code is specific to the system.* namespace. It contains policy
709 * which doesn't belong in the main xattr codepath.
711 static int can_set_system_xattr(struct inode
*inode
, const char *name
,
712 const void *value
, size_t value_len
)
714 #ifdef CONFIG_JFS_POSIX_ACL
715 struct posix_acl
*acl
;
718 if ((current
->fsuid
!= inode
->i_uid
) && !capable(CAP_FOWNER
))
722 * POSIX_ACL_XATTR_ACCESS is tied to i_mode
724 if (strcmp(name
, POSIX_ACL_XATTR_ACCESS
) == 0) {
725 acl
= posix_acl_from_xattr(value
, value_len
);
728 printk(KERN_ERR
"posix_acl_from_xattr returned %d\n",
733 mode_t mode
= inode
->i_mode
;
734 rc
= posix_acl_equiv_mode(acl
, &mode
);
735 posix_acl_release(acl
);
738 "posix_acl_equiv_mode returned %d\n",
742 inode
->i_mode
= mode
;
743 mark_inode_dirty(inode
);
746 * We're changing the ACL. Get rid of the cached one
748 acl
=JFS_IP(inode
)->i_acl
;
749 if (acl
!= JFS_ACL_NOT_CACHED
)
750 posix_acl_release(acl
);
751 JFS_IP(inode
)->i_acl
= JFS_ACL_NOT_CACHED
;
754 } else if (strcmp(name
, POSIX_ACL_XATTR_DEFAULT
) == 0) {
755 acl
= posix_acl_from_xattr(value
, value_len
);
758 printk(KERN_ERR
"posix_acl_from_xattr returned %d\n",
762 posix_acl_release(acl
);
765 * We're changing the default ACL. Get rid of the cached one
767 acl
=JFS_IP(inode
)->i_default_acl
;
768 if (acl
&& (acl
!= JFS_ACL_NOT_CACHED
))
769 posix_acl_release(acl
);
770 JFS_IP(inode
)->i_default_acl
= JFS_ACL_NOT_CACHED
;
774 #endif /* CONFIG_JFS_POSIX_ACL */
778 static int can_set_xattr(struct inode
*inode
, const char *name
,
779 const void *value
, size_t value_len
)
781 if (IS_RDONLY(inode
))
784 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
))
787 if(strncmp(name
, XATTR_SYSTEM_PREFIX
, XATTR_SYSTEM_PREFIX_LEN
) == 0)
791 return can_set_system_xattr(inode
, name
, value
, value_len
);
793 if(strncmp(name
, XATTR_TRUSTED_PREFIX
, XATTR_TRUSTED_PREFIX_LEN
) == 0)
794 return (capable(CAP_SYS_ADMIN
) ? 0 : -EPERM
);
796 #ifdef CONFIG_JFS_SECURITY
797 if (strncmp(name
, XATTR_SECURITY_PREFIX
, XATTR_SECURITY_PREFIX_LEN
)
799 return 0; /* Leave it to the security module */
802 if((strncmp(name
, XATTR_USER_PREFIX
, XATTR_USER_PREFIX_LEN
) != 0) &&
803 (strncmp(name
, XATTR_OS2_PREFIX
, XATTR_OS2_PREFIX_LEN
) != 0))
806 if (!S_ISREG(inode
->i_mode
) &&
807 (!S_ISDIR(inode
->i_mode
) || inode
->i_mode
&S_ISVTX
))
810 return permission(inode
, MAY_WRITE
, NULL
);
813 int __jfs_setxattr(struct inode
*inode
, const char *name
, const void *value
,
814 size_t value_len
, int flags
)
816 struct jfs_ea_list
*ealist
;
817 struct jfs_ea
*ea
, *old_ea
= NULL
, *next_ea
= NULL
;
818 struct ea_buffer ea_buf
;
822 int namelen
= strlen(name
);
823 char *os2name
= NULL
;
828 if ((rc
= can_set_xattr(inode
, name
, value
, value_len
)))
831 if (strncmp(name
, XATTR_OS2_PREFIX
, XATTR_OS2_PREFIX_LEN
) == 0) {
832 os2name
= kmalloc(namelen
- XATTR_OS2_PREFIX_LEN
+ 1,
836 strcpy(os2name
, name
+ XATTR_OS2_PREFIX_LEN
);
838 namelen
-= XATTR_OS2_PREFIX_LEN
;
841 down_write(&JFS_IP(inode
)->xattr_sem
);
843 xattr_size
= ea_get(inode
, &ea_buf
, 0);
844 if (xattr_size
< 0) {
850 ealist
= (struct jfs_ea_list
*) ea_buf
.xattr
;
851 new_size
= sizeof (struct jfs_ea_list
);
854 for (ea
= FIRST_EA(ealist
); ea
< END_EALIST(ealist
);
856 if ((namelen
== ea
->namelen
) &&
857 (memcmp(name
, ea
->name
, namelen
) == 0)) {
859 if (flags
& XATTR_CREATE
) {
864 old_ea_size
= EA_SIZE(ea
);
865 next_ea
= NEXT_EA(ea
);
867 new_size
+= EA_SIZE(ea
);
872 if (flags
& XATTR_REPLACE
) {
882 new_size
+= sizeof (struct jfs_ea
) + namelen
+ 1 + value_len
;
884 if (new_size
> ea_buf
.max_size
) {
886 * We need to allocate more space for merged ea list.
887 * We should only have loop to again: once.
889 ea_release(inode
, &ea_buf
);
890 xattr_size
= ea_get(inode
, &ea_buf
, new_size
);
891 if (xattr_size
< 0) {
898 /* Remove old ea of the same name */
900 /* number of bytes following target EA */
901 length
= (char *) END_EALIST(ealist
) - (char *) next_ea
;
903 memmove(old_ea
, next_ea
, length
);
904 xattr_size
-= old_ea_size
;
907 /* Add new entry to the end */
910 /* Completely new ea list */
911 xattr_size
= sizeof (struct jfs_ea_list
);
913 ea
= (struct jfs_ea
*) ((char *) ealist
+ xattr_size
);
915 ea
->namelen
= namelen
;
916 ea
->valuelen
= (cpu_to_le16(value_len
));
917 memcpy(ea
->name
, name
, namelen
);
918 ea
->name
[namelen
] = 0;
920 memcpy(&ea
->name
[namelen
+ 1], value
, value_len
);
921 xattr_size
+= EA_SIZE(ea
);
924 /* DEBUG - If we did this right, these number match */
925 if (xattr_size
!= new_size
) {
927 "jfs_xsetattr: xattr_size = %d, new_size = %d\n",
928 xattr_size
, new_size
);
935 * If we're left with an empty list, there's no ea
937 if (new_size
== sizeof (struct jfs_ea_list
))
940 ealist
->size
= cpu_to_le32(new_size
);
942 rc
= ea_put(inode
, &ea_buf
, new_size
);
946 ea_release(inode
, &ea_buf
);
948 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
);
1050 ssize_t
jfs_getxattr(struct dentry
*dentry
, const char *name
, void *data
,
1055 err
= __jfs_getxattr(dentry
->d_inode
, name
, data
, buf_size
);
1061 * No special permissions are needed to list attributes except for trusted.*
1063 static inline int can_list(struct jfs_ea
*ea
)
1065 return (strncmp(ea
->name
, XATTR_TRUSTED_PREFIX
,
1066 XATTR_TRUSTED_PREFIX_LEN
) ||
1067 capable(CAP_SYS_ADMIN
));
1070 ssize_t
jfs_listxattr(struct dentry
* dentry
, char *data
, size_t buf_size
)
1072 struct inode
*inode
= dentry
->d_inode
;
1076 struct jfs_ea_list
*ealist
;
1078 struct ea_buffer ea_buf
;
1080 down_read(&JFS_IP(inode
)->xattr_sem
);
1082 xattr_size
= ea_get(inode
, &ea_buf
, 0);
1083 if (xattr_size
< 0) {
1088 if (xattr_size
== 0)
1091 ealist
= (struct jfs_ea_list
*) ea_buf
.xattr
;
1093 /* compute required size of list */
1094 for (ea
= FIRST_EA(ealist
); ea
< END_EALIST(ealist
); ea
= NEXT_EA(ea
)) {
1096 size
+= name_size(ea
) + 1;
1102 if (size
> buf_size
) {
1107 /* Copy attribute names to buffer */
1109 for (ea
= FIRST_EA(ealist
); ea
< END_EALIST(ealist
); ea
= NEXT_EA(ea
)) {
1111 int namelen
= copy_name(buffer
, ea
);
1112 buffer
+= namelen
+ 1;
1117 ea_release(inode
, &ea_buf
);
1119 up_read(&JFS_IP(inode
)->xattr_sem
);
1123 int jfs_removexattr(struct dentry
*dentry
, const char *name
)
1125 return __jfs_setxattr(dentry
->d_inode
, name
, NULL
, 0, XATTR_REPLACE
);