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
20 #include <linux/capability.h>
22 #include <linux/xattr.h>
23 #include <linux/posix_acl_xattr.h>
24 #include <linux/slab.h>
25 #include <linux/quotaops.h>
26 #include <linux/security.h>
27 #include "jfs_incore.h"
28 #include "jfs_superblock.h"
30 #include "jfs_debug.h"
31 #include "jfs_dinode.h"
32 #include "jfs_extent.h"
33 #include "jfs_metapage.h"
34 #include "jfs_xattr.h"
38 * jfs_xattr.c: extended attribute service
44 * Extended attribute lists (jfs_ea_list) consist of an overall size (32 bit
45 * value) and a variable (0 or more) number of extended attribute
46 * entries. Each extended attribute entry (jfs_ea) is a <name,value> double
47 * where <name> is constructed from a null-terminated ascii string
48 * (1 ... 255 bytes in the name) and <value> is arbitrary 8 bit data
49 * (1 ... 65535 bytes). The in-memory format is
51 * 0 1 2 4 4 + namelen + 1
52 * +-------+--------+--------+----------------+-------------------+
53 * | Flags | Name | Value | Name String \0 | Data . . . . |
54 * | | Length | Length | | |
55 * +-------+--------+--------+----------------+-------------------+
57 * A jfs_ea_list then is structured as
59 * 0 4 4 + EA_SIZE(ea1)
60 * +------------+-------------------+--------------------+-----
61 * | Overall EA | First FEA Element | Second FEA Element | .....
63 * +------------+-------------------+--------------------+-----
67 * FEALISTs are stored on disk using blocks allocated by dbAlloc() and
68 * written directly. An EA list may be in-lined in the inode if there is
69 * sufficient room available.
73 int flag
; /* Indicates what storage xattr points to */
74 int max_size
; /* largest xattr that fits in current buffer */
75 dxd_t new_ea
; /* dxd to replace ea when modifying xattr */
76 struct metapage
*mp
; /* metapage containing ea list */
77 struct jfs_ea_list
*xattr
; /* buffer containing ea list */
81 * ea_buffer.flag values
83 #define EA_INLINE 0x0001
84 #define EA_EXTENT 0x0002
86 #define EA_MALLOC 0x0008
90 * These three routines are used to recognize on-disk extended attributes
91 * that are in a recognized namespace. If the attribute is not recognized,
92 * "os2." is prepended to the name
94 static inline int is_os2_xattr(struct jfs_ea
*ea
)
99 if ((ea
->namelen
>= XATTR_SYSTEM_PREFIX_LEN
) &&
100 !strncmp(ea
->name
, XATTR_SYSTEM_PREFIX
, XATTR_SYSTEM_PREFIX_LEN
))
105 if ((ea
->namelen
>= XATTR_USER_PREFIX_LEN
) &&
106 !strncmp(ea
->name
, XATTR_USER_PREFIX
, XATTR_USER_PREFIX_LEN
))
109 * Check for "security."
111 if ((ea
->namelen
>= XATTR_SECURITY_PREFIX_LEN
) &&
112 !strncmp(ea
->name
, XATTR_SECURITY_PREFIX
,
113 XATTR_SECURITY_PREFIX_LEN
))
116 * Check for "trusted."
118 if ((ea
->namelen
>= XATTR_TRUSTED_PREFIX_LEN
) &&
119 !strncmp(ea
->name
, XATTR_TRUSTED_PREFIX
, XATTR_TRUSTED_PREFIX_LEN
))
122 * Add any other valid namespace prefixes here
126 * We assume it's OS/2's flat namespace
131 static inline int name_size(struct jfs_ea
*ea
)
133 if (is_os2_xattr(ea
))
134 return ea
->namelen
+ XATTR_OS2_PREFIX_LEN
;
139 static inline int copy_name(char *buffer
, struct jfs_ea
*ea
)
141 int len
= ea
->namelen
;
143 if (is_os2_xattr(ea
)) {
144 memcpy(buffer
, XATTR_OS2_PREFIX
, XATTR_OS2_PREFIX_LEN
);
145 buffer
+= XATTR_OS2_PREFIX_LEN
;
146 len
+= XATTR_OS2_PREFIX_LEN
;
148 memcpy(buffer
, ea
->name
, ea
->namelen
);
149 buffer
[ea
->namelen
] = 0;
154 /* Forward references */
155 static void ea_release(struct inode
*inode
, struct ea_buffer
*ea_buf
);
158 * NAME: ea_write_inline
160 * FUNCTION: Attempt to write an EA inline if area is available
163 * Already verified that the specified EA is small enough to fit inline
167 * ealist - EA list pointer
168 * size - size of ealist in bytes
169 * ea - dxd_t structure to be filled in with necessary EA information
170 * if we successfully copy the EA inline
173 * Checks if the inode's inline area is available. If so, copies EA inline
174 * and sets <ea> fields appropriately. Otherwise, returns failure, EA will
175 * have to be put into an extent.
177 * RETURNS: 0 for successful copy to inline area; -1 if area not available
179 static int ea_write_inline(struct inode
*ip
, struct jfs_ea_list
*ealist
,
180 int size
, dxd_t
* ea
)
182 struct jfs_inode_info
*ji
= JFS_IP(ip
);
185 * Make sure we have an EA -- the NULL EA list is valid, but you
188 if (ealist
&& size
> sizeof (struct jfs_ea_list
)) {
189 assert(size
<= sizeof (ji
->i_inline_ea
));
192 * See if the space is available or if it is already being
193 * used for an inline EA.
195 if (!(ji
->mode2
& INLINEEA
) && !(ji
->ea
.flag
& DXD_INLINE
))
201 memcpy(ji
->i_inline_ea
, ealist
, size
);
202 ea
->flag
= DXD_INLINE
;
203 ji
->mode2
&= ~INLINEEA
;
210 /* Free up INLINE area */
211 if (ji
->ea
.flag
& DXD_INLINE
)
212 ji
->mode2
|= INLINEEA
;
221 * FUNCTION: Write an EA for an inode
223 * PRE CONDITIONS: EA has been verified
227 * ealist - EA list pointer
228 * size - size of ealist in bytes
229 * ea - dxd_t structure to be filled in appropriately with where the
232 * NOTES: Will write EA inline if able to, otherwise allocates blocks for an
233 * extent and synchronously writes it to those blocks.
235 * RETURNS: 0 for success; Anything else indicates failure
237 static int ea_write(struct inode
*ip
, struct jfs_ea_list
*ealist
, int size
,
240 struct super_block
*sb
= ip
->i_sb
;
241 struct jfs_inode_info
*ji
= JFS_IP(ip
);
242 struct jfs_sb_info
*sbi
= JFS_SBI(sb
);
252 * Quick check to see if this is an in-linable EA. Short EAs
253 * and empty EAs are all in-linable, provided the space exists.
255 if (!ealist
|| size
<= sizeof (ji
->i_inline_ea
)) {
256 if (!ea_write_inline(ip
, ealist
, size
, ea
))
260 /* figure out how many blocks we need */
261 nblocks
= (size
+ (sb
->s_blocksize
- 1)) >> sb
->s_blocksize_bits
;
263 /* Allocate new blocks to quota. */
264 rc
= dquot_alloc_block(ip
, nblocks
);
268 rc
= dbAlloc(ip
, INOHINT(ip
), nblocks
, &blkno
);
270 /*Rollback quota allocation. */
271 dquot_free_block(ip
, nblocks
);
276 * Now have nblocks worth of storage to stuff into the FEALIST.
277 * loop over the FEALIST copying data into the buffer one page at
280 cp
= (char *) ealist
;
282 for (i
= 0; i
< nblocks
; i
+= sbi
->nbperpage
) {
284 * Determine how many bytes for this request, and round up to
285 * the nearest aggregate block size
287 nb
= min(PSIZE
, nbytes
);
289 ((((nb
+ sb
->s_blocksize
- 1)) >> sb
->s_blocksize_bits
))
290 << sb
->s_blocksize_bits
;
292 if (!(mp
= get_metapage(ip
, blkno
+ i
, bytes_to_write
, 1))) {
297 memcpy(mp
->data
, cp
, nb
);
300 * We really need a way to propagate errors for
301 * forced writes like this one. --hch
303 * (__write_metapage => release_metapage => flush_metapage)
306 if ((rc
= flush_metapage(mp
))) {
308 * the write failed -- this means that the buffer
309 * is still assigned and the blocks are not being
310 * used. this seems like the best error recovery
323 ea
->flag
= DXD_EXTENT
;
324 DXDsize(ea
, le32_to_cpu(ealist
->size
));
325 DXDlength(ea
, nblocks
);
326 DXDaddress(ea
, blkno
);
328 /* Free up INLINE area */
329 if (ji
->ea
.flag
& DXD_INLINE
)
330 ji
->mode2
|= INLINEEA
;
335 /* Rollback quota allocation. */
336 dquot_free_block(ip
, nblocks
);
338 dbFree(ip
, blkno
, nblocks
);
343 * NAME: ea_read_inline
345 * FUNCTION: Read an inlined EA into user's buffer
349 * ealist - Pointer to buffer to fill in with EA
353 static int ea_read_inline(struct inode
*ip
, struct jfs_ea_list
*ealist
)
355 struct jfs_inode_info
*ji
= JFS_IP(ip
);
356 int ea_size
= sizeDXD(&ji
->ea
);
364 if ((sizeDXD(&ji
->ea
) > sizeof (ji
->i_inline_ea
)))
366 if (le32_to_cpu(((struct jfs_ea_list
*) &ji
->i_inline_ea
)->size
)
370 memcpy(ealist
, ji
->i_inline_ea
, ea_size
);
377 * FUNCTION: copy EA data into user's buffer
381 * ealist - Pointer to buffer to fill in with EA
383 * NOTES: If EA is inline calls ea_read_inline() to copy EA.
385 * RETURNS: 0 for success; other indicates failure
387 static int ea_read(struct inode
*ip
, struct jfs_ea_list
*ealist
)
389 struct super_block
*sb
= ip
->i_sb
;
390 struct jfs_inode_info
*ji
= JFS_IP(ip
);
391 struct jfs_sb_info
*sbi
= JFS_SBI(sb
);
394 char *cp
= (char *) ealist
;
400 /* quick check for in-line EA */
401 if (ji
->ea
.flag
& DXD_INLINE
)
402 return ea_read_inline(ip
, ealist
);
404 nbytes
= sizeDXD(&ji
->ea
);
406 jfs_error(sb
, "ea_read: nbytes is 0");
411 * Figure out how many blocks were allocated when this EA list was
412 * originally written to disk.
414 nblocks
= lengthDXD(&ji
->ea
) << sbi
->l2nbperpage
;
415 blkno
= addressDXD(&ji
->ea
) << sbi
->l2nbperpage
;
418 * I have found the disk blocks which were originally used to store
419 * the FEALIST. now i loop over each contiguous block copying the
420 * data into the buffer.
422 for (i
= 0; i
< nblocks
; i
+= sbi
->nbperpage
) {
424 * Determine how many bytes for this request, and round up to
425 * the nearest aggregate block size
427 nb
= min(PSIZE
, nbytes
);
429 ((((nb
+ sb
->s_blocksize
- 1)) >> sb
->s_blocksize_bits
))
430 << sb
->s_blocksize_bits
;
432 if (!(mp
= read_metapage(ip
, blkno
+ i
, bytes_to_read
, 1)))
435 memcpy(cp
, mp
->data
, nb
);
436 release_metapage(mp
);
448 * FUNCTION: Returns buffer containing existing extended attributes.
449 * The size of the buffer will be the larger of the existing
450 * attributes size, or min_size.
452 * The buffer, which may be inlined in the inode or in the
453 * page cache must be release by calling ea_release or ea_put
456 * inode - Inode pointer
457 * ea_buf - Structure to be populated with ealist and its metadata
458 * min_size- minimum size of buffer to be returned
460 * RETURNS: 0 for success; Other indicates failure
462 static int ea_get(struct inode
*inode
, struct ea_buffer
*ea_buf
, int min_size
)
464 struct jfs_inode_info
*ji
= JFS_IP(inode
);
465 struct super_block
*sb
= inode
->i_sb
;
467 int ea_size
= sizeDXD(&ji
->ea
);
468 int blocks_needed
, current_blocks
;
471 int quota_allocation
= 0;
473 /* When fsck.jfs clears a bad ea, it doesn't clear the size */
474 if (ji
->ea
.flag
== 0)
480 ea_buf
->max_size
= 0;
481 ea_buf
->xattr
= NULL
;
484 if ((min_size
<= sizeof (ji
->i_inline_ea
)) &&
485 (ji
->mode2
& INLINEEA
)) {
486 ea_buf
->flag
= EA_INLINE
| EA_NEW
;
487 ea_buf
->max_size
= sizeof (ji
->i_inline_ea
);
488 ea_buf
->xattr
= (struct jfs_ea_list
*) ji
->i_inline_ea
;
489 DXDlength(&ea_buf
->new_ea
, 0);
490 DXDaddress(&ea_buf
->new_ea
, 0);
491 ea_buf
->new_ea
.flag
= DXD_INLINE
;
492 DXDsize(&ea_buf
->new_ea
, min_size
);
496 } else if (ji
->ea
.flag
& DXD_INLINE
) {
497 if (min_size
<= sizeof (ji
->i_inline_ea
)) {
498 ea_buf
->flag
= EA_INLINE
;
499 ea_buf
->max_size
= sizeof (ji
->i_inline_ea
);
500 ea_buf
->xattr
= (struct jfs_ea_list
*) ji
->i_inline_ea
;
505 if (!(ji
->ea
.flag
& DXD_EXTENT
)) {
506 jfs_error(sb
, "ea_get: invalid ea.flag)");
509 current_blocks
= (ea_size
+ sb
->s_blocksize
- 1) >>
510 sb
->s_blocksize_bits
;
512 size
= max(min_size
, ea_size
);
516 * To keep the rest of the code simple. Allocate a
517 * contiguous buffer to work with
519 ea_buf
->xattr
= kmalloc(size
, GFP_KERNEL
);
520 if (ea_buf
->xattr
== NULL
)
523 ea_buf
->flag
= EA_MALLOC
;
524 ea_buf
->max_size
= (size
+ sb
->s_blocksize
- 1) &
525 ~(sb
->s_blocksize
- 1);
530 if ((rc
= ea_read(inode
, ea_buf
->xattr
))) {
531 kfree(ea_buf
->xattr
);
532 ea_buf
->xattr
= NULL
;
537 blocks_needed
= (min_size
+ sb
->s_blocksize
- 1) >>
538 sb
->s_blocksize_bits
;
540 if (blocks_needed
> current_blocks
) {
541 /* Allocate new blocks to quota. */
542 rc
= dquot_alloc_block(inode
, blocks_needed
);
546 quota_allocation
= blocks_needed
;
548 rc
= dbAlloc(inode
, INOHINT(inode
), (s64
) blocks_needed
,
553 DXDlength(&ea_buf
->new_ea
, blocks_needed
);
554 DXDaddress(&ea_buf
->new_ea
, blkno
);
555 ea_buf
->new_ea
.flag
= DXD_EXTENT
;
556 DXDsize(&ea_buf
->new_ea
, min_size
);
558 ea_buf
->flag
= EA_EXTENT
| EA_NEW
;
560 ea_buf
->mp
= get_metapage(inode
, blkno
,
561 blocks_needed
<< sb
->s_blocksize_bits
,
563 if (ea_buf
->mp
== NULL
) {
564 dbFree(inode
, blkno
, (s64
) blocks_needed
);
568 ea_buf
->xattr
= ea_buf
->mp
->data
;
569 ea_buf
->max_size
= (min_size
+ sb
->s_blocksize
- 1) &
570 ~(sb
->s_blocksize
- 1);
573 if ((rc
= ea_read(inode
, ea_buf
->xattr
))) {
574 discard_metapage(ea_buf
->mp
);
575 dbFree(inode
, blkno
, (s64
) blocks_needed
);
580 ea_buf
->flag
= EA_EXTENT
;
581 ea_buf
->mp
= read_metapage(inode
, addressDXD(&ji
->ea
),
582 lengthDXD(&ji
->ea
) << sb
->s_blocksize_bits
,
584 if (ea_buf
->mp
== NULL
) {
588 ea_buf
->xattr
= ea_buf
->mp
->data
;
589 ea_buf
->max_size
= (ea_size
+ sb
->s_blocksize
- 1) &
590 ~(sb
->s_blocksize
- 1);
593 if (EALIST_SIZE(ea_buf
->xattr
) != ea_size
) {
594 printk(KERN_ERR
"ea_get: invalid extended attribute\n");
595 print_hex_dump(KERN_ERR
, "", DUMP_PREFIX_ADDRESS
, 16, 1,
596 ea_buf
->xattr
, ea_size
, 1);
597 ea_release(inode
, ea_buf
);
605 /* Rollback quota allocation */
606 if (quota_allocation
)
607 dquot_free_block(inode
, quota_allocation
);
612 static void ea_release(struct inode
*inode
, struct ea_buffer
*ea_buf
)
614 if (ea_buf
->flag
& EA_MALLOC
)
615 kfree(ea_buf
->xattr
);
616 else if (ea_buf
->flag
& EA_EXTENT
) {
618 release_metapage(ea_buf
->mp
);
620 if (ea_buf
->flag
& EA_NEW
)
621 dbFree(inode
, addressDXD(&ea_buf
->new_ea
),
622 lengthDXD(&ea_buf
->new_ea
));
626 static int ea_put(tid_t tid
, struct inode
*inode
, struct ea_buffer
*ea_buf
,
629 struct jfs_inode_info
*ji
= JFS_IP(inode
);
630 unsigned long old_blocks
, new_blocks
;
634 ea_release(inode
, ea_buf
);
636 } else if (ea_buf
->flag
& EA_INLINE
) {
637 assert(new_size
<= sizeof (ji
->i_inline_ea
));
638 ji
->mode2
&= ~INLINEEA
;
639 ea_buf
->new_ea
.flag
= DXD_INLINE
;
640 DXDsize(&ea_buf
->new_ea
, new_size
);
641 DXDaddress(&ea_buf
->new_ea
, 0);
642 DXDlength(&ea_buf
->new_ea
, 0);
643 } else if (ea_buf
->flag
& EA_MALLOC
) {
644 rc
= ea_write(inode
, ea_buf
->xattr
, new_size
, &ea_buf
->new_ea
);
645 kfree(ea_buf
->xattr
);
646 } else if (ea_buf
->flag
& EA_NEW
) {
647 /* We have already allocated a new dxd */
648 flush_metapage(ea_buf
->mp
);
650 /* ->xattr must point to original ea's metapage */
651 rc
= ea_write(inode
, ea_buf
->xattr
, new_size
, &ea_buf
->new_ea
);
652 discard_metapage(ea_buf
->mp
);
657 old_blocks
= new_blocks
= 0;
659 if (ji
->ea
.flag
& DXD_EXTENT
) {
660 invalidate_dxd_metapages(inode
, ji
->ea
);
661 old_blocks
= lengthDXD(&ji
->ea
);
665 txEA(tid
, inode
, &ji
->ea
, &ea_buf
->new_ea
);
666 if (ea_buf
->new_ea
.flag
& DXD_EXTENT
) {
667 new_blocks
= lengthDXD(&ea_buf
->new_ea
);
668 if (ji
->ea
.flag
& DXD_INLINE
)
669 ji
->mode2
|= INLINEEA
;
671 ji
->ea
= ea_buf
->new_ea
;
673 txEA(tid
, inode
, &ji
->ea
, NULL
);
674 if (ji
->ea
.flag
& DXD_INLINE
)
675 ji
->mode2
|= INLINEEA
;
680 /* If old blocks exist, they must be removed from quota allocation. */
682 dquot_free_block(inode
, old_blocks
);
684 inode
->i_ctime
= CURRENT_TIME
;
690 * can_set_system_xattr
692 * This code is specific to the system.* namespace. It contains policy
693 * which doesn't belong in the main xattr codepath.
695 static int can_set_system_xattr(struct inode
*inode
, const char *name
,
696 const void *value
, size_t value_len
)
698 #ifdef CONFIG_JFS_POSIX_ACL
699 struct posix_acl
*acl
;
702 if (!is_owner_or_cap(inode
))
706 * POSIX_ACL_XATTR_ACCESS is tied to i_mode
708 if (strcmp(name
, POSIX_ACL_XATTR_ACCESS
) == 0) {
709 acl
= posix_acl_from_xattr(value
, value_len
);
712 printk(KERN_ERR
"posix_acl_from_xattr returned %d\n",
717 mode_t mode
= inode
->i_mode
;
718 rc
= posix_acl_equiv_mode(acl
, &mode
);
719 posix_acl_release(acl
);
722 "posix_acl_equiv_mode returned %d\n",
726 inode
->i_mode
= mode
;
727 mark_inode_dirty(inode
);
730 * We're changing the ACL. Get rid of the cached one
732 forget_cached_acl(inode
, ACL_TYPE_ACCESS
);
735 } else if (strcmp(name
, POSIX_ACL_XATTR_DEFAULT
) == 0) {
736 acl
= posix_acl_from_xattr(value
, value_len
);
739 printk(KERN_ERR
"posix_acl_from_xattr returned %d\n",
743 posix_acl_release(acl
);
746 * We're changing the default ACL. Get rid of the cached one
748 forget_cached_acl(inode
, ACL_TYPE_DEFAULT
);
752 #endif /* CONFIG_JFS_POSIX_ACL */
757 * Most of the permission checking is done by xattr_permission in the vfs.
758 * The local file system is responsible for handling the system.* namespace.
759 * We also need to verify that this is a namespace that we recognize.
761 static int can_set_xattr(struct inode
*inode
, const char *name
,
762 const void *value
, size_t value_len
)
764 if (!strncmp(name
, XATTR_SYSTEM_PREFIX
, XATTR_SYSTEM_PREFIX_LEN
))
765 return can_set_system_xattr(inode
, name
, value
, value_len
);
768 * Don't allow setting an attribute in an unknown namespace.
770 if (strncmp(name
, XATTR_TRUSTED_PREFIX
, XATTR_TRUSTED_PREFIX_LEN
) &&
771 strncmp(name
, XATTR_SECURITY_PREFIX
, XATTR_SECURITY_PREFIX_LEN
) &&
772 strncmp(name
, XATTR_USER_PREFIX
, XATTR_USER_PREFIX_LEN
) &&
773 strncmp(name
, XATTR_OS2_PREFIX
, XATTR_OS2_PREFIX_LEN
))
779 int __jfs_setxattr(tid_t tid
, struct inode
*inode
, const char *name
,
780 const void *value
, size_t value_len
, int flags
)
782 struct jfs_ea_list
*ealist
;
783 struct jfs_ea
*ea
, *old_ea
= NULL
, *next_ea
= NULL
;
784 struct ea_buffer ea_buf
;
788 int namelen
= strlen(name
);
789 char *os2name
= NULL
;
794 if (strncmp(name
, XATTR_OS2_PREFIX
, XATTR_OS2_PREFIX_LEN
) == 0) {
795 os2name
= kmalloc(namelen
- XATTR_OS2_PREFIX_LEN
+ 1,
799 strcpy(os2name
, name
+ XATTR_OS2_PREFIX_LEN
);
801 namelen
-= XATTR_OS2_PREFIX_LEN
;
804 down_write(&JFS_IP(inode
)->xattr_sem
);
806 xattr_size
= ea_get(inode
, &ea_buf
, 0);
807 if (xattr_size
< 0) {
813 ealist
= (struct jfs_ea_list
*) ea_buf
.xattr
;
814 new_size
= sizeof (struct jfs_ea_list
);
817 for (ea
= FIRST_EA(ealist
); ea
< END_EALIST(ealist
);
819 if ((namelen
== ea
->namelen
) &&
820 (memcmp(name
, ea
->name
, namelen
) == 0)) {
822 if (flags
& XATTR_CREATE
) {
827 old_ea_size
= EA_SIZE(ea
);
828 next_ea
= NEXT_EA(ea
);
830 new_size
+= EA_SIZE(ea
);
835 if (flags
& XATTR_REPLACE
) {
845 new_size
+= sizeof (struct jfs_ea
) + namelen
+ 1 + value_len
;
847 if (new_size
> ea_buf
.max_size
) {
849 * We need to allocate more space for merged ea list.
850 * We should only have loop to again: once.
852 ea_release(inode
, &ea_buf
);
853 xattr_size
= ea_get(inode
, &ea_buf
, new_size
);
854 if (xattr_size
< 0) {
861 /* Remove old ea of the same name */
863 /* number of bytes following target EA */
864 length
= (char *) END_EALIST(ealist
) - (char *) next_ea
;
866 memmove(old_ea
, next_ea
, length
);
867 xattr_size
-= old_ea_size
;
870 /* Add new entry to the end */
873 /* Completely new ea list */
874 xattr_size
= sizeof (struct jfs_ea_list
);
876 ea
= (struct jfs_ea
*) ((char *) ealist
+ xattr_size
);
878 ea
->namelen
= namelen
;
879 ea
->valuelen
= (cpu_to_le16(value_len
));
880 memcpy(ea
->name
, name
, namelen
);
881 ea
->name
[namelen
] = 0;
883 memcpy(&ea
->name
[namelen
+ 1], value
, value_len
);
884 xattr_size
+= EA_SIZE(ea
);
887 /* DEBUG - If we did this right, these number match */
888 if (xattr_size
!= new_size
) {
890 "jfs_xsetattr: xattr_size = %d, new_size = %d\n",
891 xattr_size
, new_size
);
898 * If we're left with an empty list, there's no ea
900 if (new_size
== sizeof (struct jfs_ea_list
))
903 ealist
->size
= cpu_to_le32(new_size
);
905 rc
= ea_put(tid
, inode
, &ea_buf
, new_size
);
909 ea_release(inode
, &ea_buf
);
911 up_write(&JFS_IP(inode
)->xattr_sem
);
918 int jfs_setxattr(struct dentry
*dentry
, const char *name
, const void *value
,
919 size_t value_len
, int flags
)
921 struct inode
*inode
= dentry
->d_inode
;
922 struct jfs_inode_info
*ji
= JFS_IP(inode
);
926 if ((rc
= can_set_xattr(inode
, name
, value
, value_len
)))
929 if (value
== NULL
) { /* empty EA, do not remove */
934 tid
= txBegin(inode
->i_sb
, 0);
935 mutex_lock(&ji
->commit_mutex
);
936 rc
= __jfs_setxattr(tid
, dentry
->d_inode
, name
, value
, value_len
,
939 rc
= txCommit(tid
, 1, &inode
, 0);
941 mutex_unlock(&ji
->commit_mutex
);
946 ssize_t
__jfs_getxattr(struct inode
*inode
, const char *name
, void *data
,
949 struct jfs_ea_list
*ealist
;
951 struct ea_buffer ea_buf
;
954 int namelen
= strlen(name
);
955 char *os2name
= NULL
;
958 if (strncmp(name
, XATTR_OS2_PREFIX
, XATTR_OS2_PREFIX_LEN
) == 0) {
959 os2name
= kmalloc(namelen
- XATTR_OS2_PREFIX_LEN
+ 1,
963 strcpy(os2name
, name
+ XATTR_OS2_PREFIX_LEN
);
965 namelen
-= XATTR_OS2_PREFIX_LEN
;
968 down_read(&JFS_IP(inode
)->xattr_sem
);
970 xattr_size
= ea_get(inode
, &ea_buf
, 0);
972 if (xattr_size
< 0) {
980 ealist
= (struct jfs_ea_list
*) ea_buf
.xattr
;
982 /* Find the named attribute */
983 for (ea
= FIRST_EA(ealist
); ea
< END_EALIST(ealist
); ea
= NEXT_EA(ea
))
984 if ((namelen
== ea
->namelen
) &&
985 memcmp(name
, ea
->name
, namelen
) == 0) {
987 size
= le16_to_cpu(ea
->valuelen
);
990 else if (size
> buf_size
) {
994 value
= ((char *) &ea
->name
) + ea
->namelen
+ 1;
995 memcpy(data
, value
, size
);
1001 ea_release(inode
, &ea_buf
);
1003 up_read(&JFS_IP(inode
)->xattr_sem
);
1010 ssize_t
jfs_getxattr(struct dentry
*dentry
, const char *name
, void *data
,
1015 err
= __jfs_getxattr(dentry
->d_inode
, name
, data
, buf_size
);
1021 * No special permissions are needed to list attributes except for trusted.*
1023 static inline int can_list(struct jfs_ea
*ea
)
1025 return (strncmp(ea
->name
, XATTR_TRUSTED_PREFIX
,
1026 XATTR_TRUSTED_PREFIX_LEN
) ||
1027 capable(CAP_SYS_ADMIN
));
1030 ssize_t
jfs_listxattr(struct dentry
* dentry
, char *data
, size_t buf_size
)
1032 struct inode
*inode
= dentry
->d_inode
;
1036 struct jfs_ea_list
*ealist
;
1038 struct ea_buffer ea_buf
;
1040 down_read(&JFS_IP(inode
)->xattr_sem
);
1042 xattr_size
= ea_get(inode
, &ea_buf
, 0);
1043 if (xattr_size
< 0) {
1048 if (xattr_size
== 0)
1051 ealist
= (struct jfs_ea_list
*) ea_buf
.xattr
;
1053 /* compute required size of list */
1054 for (ea
= FIRST_EA(ealist
); ea
< END_EALIST(ealist
); ea
= NEXT_EA(ea
)) {
1056 size
+= name_size(ea
) + 1;
1062 if (size
> buf_size
) {
1067 /* Copy attribute names to buffer */
1069 for (ea
= FIRST_EA(ealist
); ea
< END_EALIST(ealist
); ea
= NEXT_EA(ea
)) {
1071 int namelen
= copy_name(buffer
, ea
);
1072 buffer
+= namelen
+ 1;
1077 ea_release(inode
, &ea_buf
);
1079 up_read(&JFS_IP(inode
)->xattr_sem
);
1083 int jfs_removexattr(struct dentry
*dentry
, const char *name
)
1085 struct inode
*inode
= dentry
->d_inode
;
1086 struct jfs_inode_info
*ji
= JFS_IP(inode
);
1090 if ((rc
= can_set_xattr(inode
, name
, NULL
, 0)))
1093 tid
= txBegin(inode
->i_sb
, 0);
1094 mutex_lock(&ji
->commit_mutex
);
1095 rc
= __jfs_setxattr(tid
, dentry
->d_inode
, name
, NULL
, 0, XATTR_REPLACE
);
1097 rc
= txCommit(tid
, 1, &inode
, 0);
1099 mutex_unlock(&ji
->commit_mutex
);
1104 #ifdef CONFIG_JFS_SECURITY
1105 int jfs_init_security(tid_t tid
, struct inode
*inode
, struct inode
*dir
)
1113 rc
= security_inode_init_security(inode
, dir
, &suffix
, &value
, &len
);
1115 if (rc
== -EOPNOTSUPP
)
1119 name
= kmalloc(XATTR_SECURITY_PREFIX_LEN
+ 1 + strlen(suffix
),
1123 goto kmalloc_failed
;
1125 strcpy(name
, XATTR_SECURITY_PREFIX
);
1126 strcpy(name
+ XATTR_SECURITY_PREFIX_LEN
, suffix
);
1128 rc
= __jfs_setxattr(tid
, inode
, name
, value
, len
, 0);