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)
95 * These three routines are used to recognize on-disk extended attributes
96 * that are in a recognized namespace. If the attribute is not recognized,
97 * "os2." is prepended to the name
99 static inline int is_os2_xattr(struct jfs_ea
*ea
)
102 * Check for "system."
104 if ((ea
->namelen
>= XATTR_SYSTEM_PREFIX_LEN
) &&
105 !strncmp(ea
->name
, XATTR_SYSTEM_PREFIX
, XATTR_SYSTEM_PREFIX_LEN
))
110 if ((ea
->namelen
>= XATTR_USER_PREFIX_LEN
) &&
111 !strncmp(ea
->name
, XATTR_USER_PREFIX
, XATTR_USER_PREFIX_LEN
))
114 * Add any other valid namespace prefixes here
118 * We assume it's OS/2's flat namespace
123 static inline int name_size(struct jfs_ea
*ea
)
125 if (is_os2_xattr(ea
))
126 return ea
->namelen
+ XATTR_OS2_PREFIX_LEN
;
131 static inline int copy_name(char *buffer
, struct jfs_ea
*ea
)
133 int len
= ea
->namelen
;
135 if (is_os2_xattr(ea
)) {
136 memcpy(buffer
, XATTR_OS2_PREFIX
, XATTR_OS2_PREFIX_LEN
);
137 buffer
+= XATTR_OS2_PREFIX_LEN
;
138 len
+= XATTR_OS2_PREFIX_LEN
;
140 memcpy(buffer
, ea
->name
, ea
->namelen
);
141 buffer
[ea
->namelen
] = 0;
146 /* Forward references */
147 static void ea_release(struct inode
*inode
, struct ea_buffer
*ea_buf
);
150 * NAME: ea_write_inline
152 * FUNCTION: Attempt to write an EA inline if area is available
155 * Already verified that the specified EA is small enough to fit inline
159 * ealist - EA list pointer
160 * size - size of ealist in bytes
161 * ea - dxd_t structure to be filled in with necessary EA information
162 * if we successfully copy the EA inline
165 * Checks if the inode's inline area is available. If so, copies EA inline
166 * and sets <ea> fields appropriately. Otherwise, returns failure, EA will
167 * have to be put into an extent.
169 * RETURNS: 0 for successful copy to inline area; -1 if area not available
171 static int ea_write_inline(struct inode
*ip
, struct jfs_ea_list
*ealist
,
172 int size
, dxd_t
* ea
)
174 struct jfs_inode_info
*ji
= JFS_IP(ip
);
177 * Make sure we have an EA -- the NULL EA list is valid, but you
180 if (ealist
&& size
> sizeof (struct jfs_ea_list
)) {
181 assert(size
<= sizeof (ji
->i_inline_ea
));
184 * See if the space is available or if it is already being
185 * used for an inline EA.
187 if (!(ji
->mode2
& INLINEEA
) && !(ji
->ea
.flag
& DXD_INLINE
))
193 memcpy(ji
->i_inline_ea
, ealist
, size
);
194 ea
->flag
= DXD_INLINE
;
195 ji
->mode2
&= ~INLINEEA
;
202 /* Free up INLINE area */
203 if (ji
->ea
.flag
& DXD_INLINE
)
204 ji
->mode2
|= INLINEEA
;
213 * FUNCTION: Write an EA for an inode
215 * PRE CONDITIONS: EA has been verified
219 * ealist - EA list pointer
220 * size - size of ealist in bytes
221 * ea - dxd_t structure to be filled in appropriately with where the
224 * NOTES: Will write EA inline if able to, otherwise allocates blocks for an
225 * extent and synchronously writes it to those blocks.
227 * RETURNS: 0 for success; Anything else indicates failure
229 static int ea_write(struct inode
*ip
, struct jfs_ea_list
*ealist
, int size
,
232 struct super_block
*sb
= ip
->i_sb
;
233 struct jfs_inode_info
*ji
= JFS_IP(ip
);
234 struct jfs_sb_info
*sbi
= JFS_SBI(sb
);
244 * Quick check to see if this is an in-linable EA. Short EAs
245 * and empty EAs are all in-linable, provided the space exists.
247 if (!ealist
|| size
<= sizeof (ji
->i_inline_ea
)) {
248 if (!ea_write_inline(ip
, ealist
, size
, ea
))
252 /* figure out how many blocks we need */
253 nblocks
= (size
+ (sb
->s_blocksize
- 1)) >> sb
->s_blocksize_bits
;
255 /* Allocate new blocks to quota. */
256 if (DQUOT_ALLOC_BLOCK(ip
, nblocks
)) {
260 rc
= dbAlloc(ip
, INOHINT(ip
), nblocks
, &blkno
);
262 /*Rollback quota allocation. */
263 DQUOT_FREE_BLOCK(ip
, nblocks
);
268 * Now have nblocks worth of storage to stuff into the FEALIST.
269 * loop over the FEALIST copying data into the buffer one page at
272 cp
= (char *) ealist
;
274 for (i
= 0; i
< nblocks
; i
+= sbi
->nbperpage
) {
276 * Determine how many bytes for this request, and round up to
277 * the nearest aggregate block size
279 nb
= min(PSIZE
, nbytes
);
281 ((((nb
+ sb
->s_blocksize
- 1)) >> sb
->s_blocksize_bits
))
282 << sb
->s_blocksize_bits
;
284 if (!(mp
= get_metapage(ip
, blkno
+ i
, bytes_to_write
, 1))) {
289 memcpy(mp
->data
, cp
, nb
);
292 * We really need a way to propagate errors for
293 * forced writes like this one. --hch
295 * (__write_metapage => release_metapage => flush_metapage)
298 if ((rc
= flush_metapage(mp
))) {
300 * the write failed -- this means that the buffer
301 * is still assigned and the blocks are not being
302 * used. this seems like the best error recovery
315 ea
->flag
= DXD_EXTENT
;
316 DXDsize(ea
, le32_to_cpu(ealist
->size
));
317 DXDlength(ea
, nblocks
);
318 DXDaddress(ea
, blkno
);
320 /* Free up INLINE area */
321 if (ji
->ea
.flag
& DXD_INLINE
)
322 ji
->mode2
|= INLINEEA
;
327 /* Rollback quota allocation. */
328 DQUOT_FREE_BLOCK(ip
, nblocks
);
330 dbFree(ip
, blkno
, nblocks
);
335 * NAME: ea_read_inline
337 * FUNCTION: Read an inlined EA into user's buffer
341 * ealist - Pointer to buffer to fill in with EA
345 static int ea_read_inline(struct inode
*ip
, struct jfs_ea_list
*ealist
)
347 struct jfs_inode_info
*ji
= JFS_IP(ip
);
348 int ea_size
= sizeDXD(&ji
->ea
);
356 if ((sizeDXD(&ji
->ea
) > sizeof (ji
->i_inline_ea
)))
358 if (le32_to_cpu(((struct jfs_ea_list
*) &ji
->i_inline_ea
)->size
)
362 memcpy(ealist
, ji
->i_inline_ea
, ea_size
);
369 * FUNCTION: copy EA data into user's buffer
373 * ealist - Pointer to buffer to fill in with EA
375 * NOTES: If EA is inline calls ea_read_inline() to copy EA.
377 * RETURNS: 0 for success; other indicates failure
379 static int ea_read(struct inode
*ip
, struct jfs_ea_list
*ealist
)
381 struct super_block
*sb
= ip
->i_sb
;
382 struct jfs_inode_info
*ji
= JFS_IP(ip
);
383 struct jfs_sb_info
*sbi
= JFS_SBI(sb
);
386 char *cp
= (char *) ealist
;
392 /* quick check for in-line EA */
393 if (ji
->ea
.flag
& DXD_INLINE
)
394 return ea_read_inline(ip
, ealist
);
396 nbytes
= sizeDXD(&ji
->ea
);
398 jfs_error(sb
, "ea_read: nbytes is 0");
403 * Figure out how many blocks were allocated when this EA list was
404 * originally written to disk.
406 nblocks
= lengthDXD(&ji
->ea
) << sbi
->l2nbperpage
;
407 blkno
= addressDXD(&ji
->ea
) << sbi
->l2nbperpage
;
410 * I have found the disk blocks which were originally used to store
411 * the FEALIST. now i loop over each contiguous block copying the
412 * data into the buffer.
414 for (i
= 0; i
< nblocks
; i
+= sbi
->nbperpage
) {
416 * Determine how many bytes for this request, and round up to
417 * the nearest aggregate block size
419 nb
= min(PSIZE
, nbytes
);
421 ((((nb
+ sb
->s_blocksize
- 1)) >> sb
->s_blocksize_bits
))
422 << sb
->s_blocksize_bits
;
424 if (!(mp
= read_metapage(ip
, blkno
+ i
, bytes_to_read
, 1)))
427 memcpy(cp
, mp
->data
, nb
);
428 release_metapage(mp
);
440 * FUNCTION: Returns buffer containing existing extended attributes.
441 * The size of the buffer will be the larger of the existing
442 * attributes size, or min_size.
444 * The buffer, which may be inlined in the inode or in the
445 * page cache must be release by calling ea_release or ea_put
448 * inode - Inode pointer
449 * ea_buf - Structure to be populated with ealist and its metadata
450 * min_size- minimum size of buffer to be returned
452 * RETURNS: 0 for success; Other indicates failure
454 static int ea_get(struct inode
*inode
, struct ea_buffer
*ea_buf
, int min_size
)
456 struct jfs_inode_info
*ji
= JFS_IP(inode
);
457 struct super_block
*sb
= inode
->i_sb
;
459 int ea_size
= sizeDXD(&ji
->ea
);
460 int blocks_needed
, current_blocks
;
463 int quota_allocation
= 0;
465 /* When fsck.jfs clears a bad ea, it doesn't clear the size */
466 if (ji
->ea
.flag
== 0)
472 ea_buf
->max_size
= 0;
473 ea_buf
->xattr
= NULL
;
476 if ((min_size
<= sizeof (ji
->i_inline_ea
)) &&
477 (ji
->mode2
& INLINEEA
)) {
478 ea_buf
->flag
= EA_INLINE
| EA_NEW
;
479 ea_buf
->max_size
= sizeof (ji
->i_inline_ea
);
480 ea_buf
->xattr
= (struct jfs_ea_list
*) ji
->i_inline_ea
;
481 DXDlength(&ea_buf
->new_ea
, 0);
482 DXDaddress(&ea_buf
->new_ea
, 0);
483 ea_buf
->new_ea
.flag
= DXD_INLINE
;
484 DXDsize(&ea_buf
->new_ea
, min_size
);
488 } else if (ji
->ea
.flag
& DXD_INLINE
) {
489 if (min_size
<= sizeof (ji
->i_inline_ea
)) {
490 ea_buf
->flag
= EA_INLINE
;
491 ea_buf
->max_size
= sizeof (ji
->i_inline_ea
);
492 ea_buf
->xattr
= (struct jfs_ea_list
*) ji
->i_inline_ea
;
497 if (!(ji
->ea
.flag
& DXD_EXTENT
)) {
498 jfs_error(sb
, "ea_get: invalid ea.flag)");
501 current_blocks
= (ea_size
+ sb
->s_blocksize
- 1) >>
502 sb
->s_blocksize_bits
;
504 size
= max(min_size
, ea_size
);
508 * To keep the rest of the code simple. Allocate a
509 * contiguous buffer to work with
511 ea_buf
->xattr
= kmalloc(size
, GFP_KERNEL
);
512 if (ea_buf
->xattr
== NULL
)
515 ea_buf
->flag
= EA_MALLOC
;
516 ea_buf
->max_size
= (size
+ sb
->s_blocksize
- 1) &
517 ~(sb
->s_blocksize
- 1);
522 if ((rc
= ea_read(inode
, ea_buf
->xattr
))) {
523 kfree(ea_buf
->xattr
);
524 ea_buf
->xattr
= NULL
;
529 blocks_needed
= (min_size
+ sb
->s_blocksize
- 1) >>
530 sb
->s_blocksize_bits
;
532 if (blocks_needed
> current_blocks
) {
533 /* Allocate new blocks to quota. */
534 if (DQUOT_ALLOC_BLOCK(inode
, blocks_needed
))
537 quota_allocation
= blocks_needed
;
539 rc
= dbAlloc(inode
, INOHINT(inode
), (s64
) blocks_needed
,
544 DXDlength(&ea_buf
->new_ea
, blocks_needed
);
545 DXDaddress(&ea_buf
->new_ea
, blkno
);
546 ea_buf
->new_ea
.flag
= DXD_EXTENT
;
547 DXDsize(&ea_buf
->new_ea
, min_size
);
549 ea_buf
->flag
= EA_EXTENT
| EA_NEW
;
551 ea_buf
->mp
= get_metapage(inode
, blkno
,
552 blocks_needed
<< sb
->s_blocksize_bits
,
554 if (ea_buf
->mp
== NULL
) {
555 dbFree(inode
, blkno
, (s64
) blocks_needed
);
559 ea_buf
->xattr
= ea_buf
->mp
->data
;
560 ea_buf
->max_size
= (min_size
+ sb
->s_blocksize
- 1) &
561 ~(sb
->s_blocksize
- 1);
564 if ((rc
= ea_read(inode
, ea_buf
->xattr
))) {
565 discard_metapage(ea_buf
->mp
);
566 dbFree(inode
, blkno
, (s64
) blocks_needed
);
571 ea_buf
->flag
= EA_EXTENT
;
572 ea_buf
->mp
= read_metapage(inode
, addressDXD(&ji
->ea
),
573 lengthDXD(&ji
->ea
) << sb
->s_blocksize_bits
,
575 if (ea_buf
->mp
== NULL
) {
579 ea_buf
->xattr
= ea_buf
->mp
->data
;
580 ea_buf
->max_size
= (ea_size
+ sb
->s_blocksize
- 1) &
581 ~(sb
->s_blocksize
- 1);
584 if (EALIST_SIZE(ea_buf
->xattr
) != ea_size
) {
585 printk(KERN_ERR
"ea_get: invalid extended attribute\n");
586 dump_mem("xattr", ea_buf
->xattr
, ea_size
);
587 ea_release(inode
, ea_buf
);
595 /* Rollback quota allocation */
596 if (quota_allocation
)
597 DQUOT_FREE_BLOCK(inode
, quota_allocation
);
602 static void ea_release(struct inode
*inode
, struct ea_buffer
*ea_buf
)
604 if (ea_buf
->flag
& EA_MALLOC
)
605 kfree(ea_buf
->xattr
);
606 else if (ea_buf
->flag
& EA_EXTENT
) {
608 release_metapage(ea_buf
->mp
);
610 if (ea_buf
->flag
& EA_NEW
)
611 dbFree(inode
, addressDXD(&ea_buf
->new_ea
),
612 lengthDXD(&ea_buf
->new_ea
));
616 static int ea_put(struct inode
*inode
, struct ea_buffer
*ea_buf
, int new_size
)
618 struct jfs_inode_info
*ji
= JFS_IP(inode
);
619 unsigned long old_blocks
, new_blocks
;
624 ea_release(inode
, ea_buf
);
626 } else if (ea_buf
->flag
& EA_INLINE
) {
627 assert(new_size
<= sizeof (ji
->i_inline_ea
));
628 ji
->mode2
&= ~INLINEEA
;
629 ea_buf
->new_ea
.flag
= DXD_INLINE
;
630 DXDsize(&ea_buf
->new_ea
, new_size
);
631 DXDaddress(&ea_buf
->new_ea
, 0);
632 DXDlength(&ea_buf
->new_ea
, 0);
633 } else if (ea_buf
->flag
& EA_MALLOC
) {
634 rc
= ea_write(inode
, ea_buf
->xattr
, new_size
, &ea_buf
->new_ea
);
635 kfree(ea_buf
->xattr
);
636 } else if (ea_buf
->flag
& EA_NEW
) {
637 /* We have already allocated a new dxd */
638 flush_metapage(ea_buf
->mp
);
640 /* ->xattr must point to original ea's metapage */
641 rc
= ea_write(inode
, ea_buf
->xattr
, new_size
, &ea_buf
->new_ea
);
642 discard_metapage(ea_buf
->mp
);
647 tid
= txBegin(inode
->i_sb
, 0);
648 down(&ji
->commit_sem
);
650 old_blocks
= new_blocks
= 0;
652 if (ji
->ea
.flag
& DXD_EXTENT
) {
653 invalidate_dxd_metapages(inode
, ji
->ea
);
654 old_blocks
= lengthDXD(&ji
->ea
);
658 txEA(tid
, inode
, &ji
->ea
, &ea_buf
->new_ea
);
659 if (ea_buf
->new_ea
.flag
& DXD_EXTENT
) {
660 new_blocks
= lengthDXD(&ea_buf
->new_ea
);
661 if (ji
->ea
.flag
& DXD_INLINE
)
662 ji
->mode2
|= INLINEEA
;
664 ji
->ea
= ea_buf
->new_ea
;
666 txEA(tid
, inode
, &ji
->ea
, NULL
);
667 if (ji
->ea
.flag
& DXD_INLINE
)
668 ji
->mode2
|= INLINEEA
;
673 /* If old blocks exist, they must be removed from quota allocation. */
675 DQUOT_FREE_BLOCK(inode
, old_blocks
);
677 inode
->i_ctime
= CURRENT_TIME
;
678 rc
= txCommit(tid
, 1, &inode
, 0);
686 * can_set_system_xattr
688 * This code is specific to the system.* namespace. It contains policy
689 * which doesn't belong in the main xattr codepath.
691 static int can_set_system_xattr(struct inode
*inode
, const char *name
,
692 const void *value
, size_t value_len
)
694 #ifdef CONFIG_JFS_POSIX_ACL
695 struct posix_acl
*acl
;
698 if ((current
->fsuid
!= inode
->i_uid
) && !capable(CAP_FOWNER
))
702 * XATTR_NAME_ACL_ACCESS is tied to i_mode
704 if (strcmp(name
, XATTR_NAME_ACL_ACCESS
) == 0) {
705 acl
= posix_acl_from_xattr(value
, value_len
);
708 printk(KERN_ERR
"posix_acl_from_xattr returned %d\n",
713 mode_t mode
= inode
->i_mode
;
714 rc
= posix_acl_equiv_mode(acl
, &mode
);
715 posix_acl_release(acl
);
718 "posix_acl_equiv_mode returned %d\n",
722 inode
->i_mode
= mode
;
723 mark_inode_dirty(inode
);
726 * We're changing the ACL. Get rid of the cached one
728 acl
=JFS_IP(inode
)->i_acl
;
729 if (acl
!= JFS_ACL_NOT_CACHED
)
730 posix_acl_release(acl
);
731 JFS_IP(inode
)->i_acl
= JFS_ACL_NOT_CACHED
;
734 } else if (strcmp(name
, XATTR_NAME_ACL_DEFAULT
) == 0) {
735 acl
= posix_acl_from_xattr(value
, value_len
);
738 printk(KERN_ERR
"posix_acl_from_xattr returned %d\n",
742 posix_acl_release(acl
);
745 * We're changing the default ACL. Get rid of the cached one
747 acl
=JFS_IP(inode
)->i_default_acl
;
748 if (acl
&& (acl
!= JFS_ACL_NOT_CACHED
))
749 posix_acl_release(acl
);
750 JFS_IP(inode
)->i_default_acl
= JFS_ACL_NOT_CACHED
;
754 #endif /* CONFIG_JFS_POSIX_ACL */
758 static int can_set_xattr(struct inode
*inode
, const char *name
,
759 const void *value
, size_t value_len
)
761 if (IS_RDONLY(inode
))
764 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
) || S_ISLNK(inode
->i_mode
))
767 if(strncmp(name
, XATTR_SYSTEM_PREFIX
, XATTR_SYSTEM_PREFIX_LEN
) == 0)
771 return can_set_system_xattr(inode
, name
, value
, value_len
);
773 if((strncmp(name
, XATTR_USER_PREFIX
, XATTR_USER_PREFIX_LEN
) != 0) &&
774 (strncmp(name
, XATTR_OS2_PREFIX
, XATTR_OS2_PREFIX_LEN
) != 0))
777 if (!S_ISREG(inode
->i_mode
) &&
778 (!S_ISDIR(inode
->i_mode
) || inode
->i_mode
&S_ISVTX
))
781 return permission(inode
, MAY_WRITE
, NULL
);
784 int __jfs_setxattr(struct inode
*inode
, const char *name
, const void *value
,
785 size_t value_len
, int flags
)
787 struct jfs_ea_list
*ealist
;
788 struct jfs_ea
*ea
, *old_ea
= NULL
, *next_ea
= NULL
;
789 struct ea_buffer ea_buf
;
793 int namelen
= strlen(name
);
794 char *os2name
= NULL
;
799 if ((rc
= can_set_xattr(inode
, name
, value
, value_len
)))
802 if (strncmp(name
, XATTR_OS2_PREFIX
, XATTR_OS2_PREFIX_LEN
) == 0) {
803 os2name
= kmalloc(namelen
- XATTR_OS2_PREFIX_LEN
+ 1,
807 strcpy(os2name
, name
+ XATTR_OS2_PREFIX_LEN
);
809 namelen
-= XATTR_OS2_PREFIX_LEN
;
812 down_write(&JFS_IP(inode
)->xattr_sem
);
814 xattr_size
= ea_get(inode
, &ea_buf
, 0);
815 if (xattr_size
< 0) {
821 ealist
= (struct jfs_ea_list
*) ea_buf
.xattr
;
822 new_size
= sizeof (struct jfs_ea_list
);
825 for (ea
= FIRST_EA(ealist
); ea
< END_EALIST(ealist
);
827 if ((namelen
== ea
->namelen
) &&
828 (memcmp(name
, ea
->name
, namelen
) == 0)) {
830 if (flags
& XATTR_CREATE
) {
835 old_ea_size
= EA_SIZE(ea
);
836 next_ea
= NEXT_EA(ea
);
838 new_size
+= EA_SIZE(ea
);
843 if (flags
& XATTR_REPLACE
) {
853 new_size
+= sizeof (struct jfs_ea
) + namelen
+ 1 + value_len
;
855 if (new_size
> ea_buf
.max_size
) {
857 * We need to allocate more space for merged ea list.
858 * We should only have loop to again: once.
860 ea_release(inode
, &ea_buf
);
861 xattr_size
= ea_get(inode
, &ea_buf
, new_size
);
862 if (xattr_size
< 0) {
869 /* Remove old ea of the same name */
871 /* number of bytes following target EA */
872 length
= (char *) END_EALIST(ealist
) - (char *) next_ea
;
874 memmove(old_ea
, next_ea
, length
);
875 xattr_size
-= old_ea_size
;
878 /* Add new entry to the end */
881 /* Completely new ea list */
882 xattr_size
= sizeof (struct jfs_ea_list
);
884 ea
= (struct jfs_ea
*) ((char *) ealist
+ xattr_size
);
886 ea
->namelen
= namelen
;
887 ea
->valuelen
= (cpu_to_le16(value_len
));
888 memcpy(ea
->name
, name
, namelen
);
889 ea
->name
[namelen
] = 0;
891 memcpy(&ea
->name
[namelen
+ 1], value
, value_len
);
892 xattr_size
+= EA_SIZE(ea
);
895 /* DEBUG - If we did this right, these number match */
896 if (xattr_size
!= new_size
) {
898 "jfs_xsetattr: xattr_size = %d, new_size = %d\n",
899 xattr_size
, new_size
);
906 * If we're left with an empty list, there's no ea
908 if (new_size
== sizeof (struct jfs_ea_list
))
911 ealist
->size
= cpu_to_le32(new_size
);
913 rc
= ea_put(inode
, &ea_buf
, new_size
);
917 ea_release(inode
, &ea_buf
);
919 up_write(&JFS_IP(inode
)->xattr_sem
);
927 int jfs_setxattr(struct dentry
*dentry
, const char *name
, const void *value
,
928 size_t value_len
, int flags
)
930 if (value
== NULL
) { /* empty EA, do not remove */
935 return __jfs_setxattr(dentry
->d_inode
, name
, value
, value_len
, flags
);
938 static int can_get_xattr(struct inode
*inode
, const char *name
)
940 if(strncmp(name
, XATTR_SYSTEM_PREFIX
, XATTR_SYSTEM_PREFIX_LEN
) == 0)
942 return permission(inode
, MAY_READ
, NULL
);
945 ssize_t
__jfs_getxattr(struct inode
*inode
, const char *name
, void *data
,
948 struct jfs_ea_list
*ealist
;
950 struct ea_buffer ea_buf
;
953 int namelen
= strlen(name
);
954 char *os2name
= NULL
;
958 if ((rc
= can_get_xattr(inode
, name
)))
961 if (strncmp(name
, XATTR_OS2_PREFIX
, XATTR_OS2_PREFIX_LEN
) == 0) {
962 os2name
= kmalloc(namelen
- XATTR_OS2_PREFIX_LEN
+ 1,
966 strcpy(os2name
, name
+ XATTR_OS2_PREFIX_LEN
);
968 namelen
-= XATTR_OS2_PREFIX_LEN
;
971 down_read(&JFS_IP(inode
)->xattr_sem
);
973 xattr_size
= ea_get(inode
, &ea_buf
, 0);
975 if (xattr_size
< 0) {
983 ealist
= (struct jfs_ea_list
*) ea_buf
.xattr
;
985 /* Find the named attribute */
986 for (ea
= FIRST_EA(ealist
); ea
< END_EALIST(ealist
); ea
= NEXT_EA(ea
))
987 if ((namelen
== ea
->namelen
) &&
988 memcmp(name
, ea
->name
, namelen
) == 0) {
990 size
= le16_to_cpu(ea
->valuelen
);
993 else if (size
> buf_size
) {
997 value
= ((char *) &ea
->name
) + ea
->namelen
+ 1;
998 memcpy(data
, value
, size
);
1004 ea_release(inode
, &ea_buf
);
1006 up_read(&JFS_IP(inode
)->xattr_sem
);
1014 ssize_t
jfs_getxattr(struct dentry
*dentry
, const char *name
, void *data
,
1019 err
= __jfs_getxattr(dentry
->d_inode
, name
, data
, buf_size
);
1024 ssize_t
jfs_listxattr(struct dentry
* dentry
, char *data
, size_t buf_size
)
1026 struct inode
*inode
= dentry
->d_inode
;
1030 struct jfs_ea_list
*ealist
;
1032 struct ea_buffer ea_buf
;
1034 down_read(&JFS_IP(inode
)->xattr_sem
);
1036 xattr_size
= ea_get(inode
, &ea_buf
, 0);
1037 if (xattr_size
< 0) {
1042 if (xattr_size
== 0)
1045 ealist
= (struct jfs_ea_list
*) ea_buf
.xattr
;
1047 /* compute required size of list */
1048 for (ea
= FIRST_EA(ealist
); ea
< END_EALIST(ealist
); ea
= NEXT_EA(ea
))
1049 size
+= name_size(ea
) + 1;
1054 if (size
> buf_size
) {
1059 /* Copy attribute names to buffer */
1061 for (ea
= FIRST_EA(ealist
); ea
< END_EALIST(ealist
); ea
= NEXT_EA(ea
)) {
1062 int namelen
= copy_name(buffer
, ea
);
1063 buffer
+= namelen
+ 1;
1067 ea_release(inode
, &ea_buf
);
1069 up_read(&JFS_IP(inode
)->xattr_sem
);
1073 int jfs_removexattr(struct dentry
*dentry
, const char *name
)
1075 return __jfs_setxattr(dentry
->d_inode
, name
, NULL
, 0, XATTR_REPLACE
);