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
89 static int is_known_namespace(const char *name
)
91 if (strncmp(name
, XATTR_SYSTEM_PREFIX
, XATTR_SYSTEM_PREFIX_LEN
) &&
92 strncmp(name
, XATTR_USER_PREFIX
, XATTR_USER_PREFIX_LEN
) &&
93 strncmp(name
, XATTR_SECURITY_PREFIX
, XATTR_SECURITY_PREFIX_LEN
) &&
94 strncmp(name
, XATTR_TRUSTED_PREFIX
, XATTR_TRUSTED_PREFIX_LEN
))
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 int is_os2_xattr(struct jfs_ea
*ea
)
107 return !is_known_namespace(ea
->name
);
110 static inline int name_size(struct jfs_ea
*ea
)
112 if (is_os2_xattr(ea
))
113 return ea
->namelen
+ XATTR_OS2_PREFIX_LEN
;
118 static inline int copy_name(char *buffer
, struct jfs_ea
*ea
)
120 int len
= ea
->namelen
;
122 if (is_os2_xattr(ea
)) {
123 memcpy(buffer
, XATTR_OS2_PREFIX
, XATTR_OS2_PREFIX_LEN
);
124 buffer
+= XATTR_OS2_PREFIX_LEN
;
125 len
+= XATTR_OS2_PREFIX_LEN
;
127 memcpy(buffer
, ea
->name
, ea
->namelen
);
128 buffer
[ea
->namelen
] = 0;
133 /* Forward references */
134 static void ea_release(struct inode
*inode
, struct ea_buffer
*ea_buf
);
137 * NAME: ea_write_inline
139 * FUNCTION: Attempt to write an EA inline if area is available
142 * Already verified that the specified EA is small enough to fit inline
146 * ealist - EA list pointer
147 * size - size of ealist in bytes
148 * ea - dxd_t structure to be filled in with necessary EA information
149 * if we successfully copy the EA inline
152 * Checks if the inode's inline area is available. If so, copies EA inline
153 * and sets <ea> fields appropriately. Otherwise, returns failure, EA will
154 * have to be put into an extent.
156 * RETURNS: 0 for successful copy to inline area; -1 if area not available
158 static int ea_write_inline(struct inode
*ip
, struct jfs_ea_list
*ealist
,
159 int size
, dxd_t
* ea
)
161 struct jfs_inode_info
*ji
= JFS_IP(ip
);
164 * Make sure we have an EA -- the NULL EA list is valid, but you
167 if (ealist
&& size
> sizeof (struct jfs_ea_list
)) {
168 assert(size
<= sizeof (ji
->i_inline_ea
));
171 * See if the space is available or if it is already being
172 * used for an inline EA.
174 if (!(ji
->mode2
& INLINEEA
) && !(ji
->ea
.flag
& DXD_INLINE
))
180 memcpy(ji
->i_inline_ea
, ealist
, size
);
181 ea
->flag
= DXD_INLINE
;
182 ji
->mode2
&= ~INLINEEA
;
189 /* Free up INLINE area */
190 if (ji
->ea
.flag
& DXD_INLINE
)
191 ji
->mode2
|= INLINEEA
;
200 * FUNCTION: Write an EA for an inode
202 * PRE CONDITIONS: EA has been verified
206 * ealist - EA list pointer
207 * size - size of ealist in bytes
208 * ea - dxd_t structure to be filled in appropriately with where the
211 * NOTES: Will write EA inline if able to, otherwise allocates blocks for an
212 * extent and synchronously writes it to those blocks.
214 * RETURNS: 0 for success; Anything else indicates failure
216 static int ea_write(struct inode
*ip
, struct jfs_ea_list
*ealist
, int size
,
219 struct super_block
*sb
= ip
->i_sb
;
220 struct jfs_inode_info
*ji
= JFS_IP(ip
);
221 struct jfs_sb_info
*sbi
= JFS_SBI(sb
);
231 * Quick check to see if this is an in-linable EA. Short EAs
232 * and empty EAs are all in-linable, provided the space exists.
234 if (!ealist
|| size
<= sizeof (ji
->i_inline_ea
)) {
235 if (!ea_write_inline(ip
, ealist
, size
, ea
))
239 /* figure out how many blocks we need */
240 nblocks
= (size
+ (sb
->s_blocksize
- 1)) >> sb
->s_blocksize_bits
;
242 /* Allocate new blocks to quota. */
243 rc
= dquot_alloc_block(ip
, nblocks
);
247 rc
= dbAlloc(ip
, INOHINT(ip
), nblocks
, &blkno
);
249 /*Rollback quota allocation. */
250 dquot_free_block(ip
, nblocks
);
255 * Now have nblocks worth of storage to stuff into the FEALIST.
256 * loop over the FEALIST copying data into the buffer one page at
259 cp
= (char *) ealist
;
261 for (i
= 0; i
< nblocks
; i
+= sbi
->nbperpage
) {
263 * Determine how many bytes for this request, and round up to
264 * the nearest aggregate block size
266 nb
= min(PSIZE
, nbytes
);
268 ((((nb
+ sb
->s_blocksize
- 1)) >> sb
->s_blocksize_bits
))
269 << sb
->s_blocksize_bits
;
271 if (!(mp
= get_metapage(ip
, blkno
+ i
, bytes_to_write
, 1))) {
276 memcpy(mp
->data
, cp
, nb
);
279 * We really need a way to propagate errors for
280 * forced writes like this one. --hch
282 * (__write_metapage => release_metapage => flush_metapage)
285 if ((rc
= flush_metapage(mp
))) {
287 * the write failed -- this means that the buffer
288 * is still assigned and the blocks are not being
289 * used. this seems like the best error recovery
302 ea
->flag
= DXD_EXTENT
;
303 DXDsize(ea
, le32_to_cpu(ealist
->size
));
304 DXDlength(ea
, nblocks
);
305 DXDaddress(ea
, blkno
);
307 /* Free up INLINE area */
308 if (ji
->ea
.flag
& DXD_INLINE
)
309 ji
->mode2
|= INLINEEA
;
314 /* Rollback quota allocation. */
315 dquot_free_block(ip
, nblocks
);
317 dbFree(ip
, blkno
, nblocks
);
322 * NAME: ea_read_inline
324 * FUNCTION: Read an inlined EA into user's buffer
328 * ealist - Pointer to buffer to fill in with EA
332 static int ea_read_inline(struct inode
*ip
, struct jfs_ea_list
*ealist
)
334 struct jfs_inode_info
*ji
= JFS_IP(ip
);
335 int ea_size
= sizeDXD(&ji
->ea
);
343 if ((sizeDXD(&ji
->ea
) > sizeof (ji
->i_inline_ea
)))
345 if (le32_to_cpu(((struct jfs_ea_list
*) &ji
->i_inline_ea
)->size
)
349 memcpy(ealist
, ji
->i_inline_ea
, ea_size
);
356 * FUNCTION: copy EA data into user's buffer
360 * ealist - Pointer to buffer to fill in with EA
362 * NOTES: If EA is inline calls ea_read_inline() to copy EA.
364 * RETURNS: 0 for success; other indicates failure
366 static int ea_read(struct inode
*ip
, struct jfs_ea_list
*ealist
)
368 struct super_block
*sb
= ip
->i_sb
;
369 struct jfs_inode_info
*ji
= JFS_IP(ip
);
370 struct jfs_sb_info
*sbi
= JFS_SBI(sb
);
373 char *cp
= (char *) ealist
;
379 /* quick check for in-line EA */
380 if (ji
->ea
.flag
& DXD_INLINE
)
381 return ea_read_inline(ip
, ealist
);
383 nbytes
= sizeDXD(&ji
->ea
);
385 jfs_error(sb
, "ea_read: nbytes is 0");
390 * Figure out how many blocks were allocated when this EA list was
391 * originally written to disk.
393 nblocks
= lengthDXD(&ji
->ea
) << sbi
->l2nbperpage
;
394 blkno
= addressDXD(&ji
->ea
) << sbi
->l2nbperpage
;
397 * I have found the disk blocks which were originally used to store
398 * the FEALIST. now i loop over each contiguous block copying the
399 * data into the buffer.
401 for (i
= 0; i
< nblocks
; i
+= sbi
->nbperpage
) {
403 * Determine how many bytes for this request, and round up to
404 * the nearest aggregate block size
406 nb
= min(PSIZE
, nbytes
);
408 ((((nb
+ sb
->s_blocksize
- 1)) >> sb
->s_blocksize_bits
))
409 << sb
->s_blocksize_bits
;
411 if (!(mp
= read_metapage(ip
, blkno
+ i
, bytes_to_read
, 1)))
414 memcpy(cp
, mp
->data
, nb
);
415 release_metapage(mp
);
427 * FUNCTION: Returns buffer containing existing extended attributes.
428 * The size of the buffer will be the larger of the existing
429 * attributes size, or min_size.
431 * The buffer, which may be inlined in the inode or in the
432 * page cache must be release by calling ea_release or ea_put
435 * inode - Inode pointer
436 * ea_buf - Structure to be populated with ealist and its metadata
437 * min_size- minimum size of buffer to be returned
439 * RETURNS: 0 for success; Other indicates failure
441 static int ea_get(struct inode
*inode
, struct ea_buffer
*ea_buf
, int min_size
)
443 struct jfs_inode_info
*ji
= JFS_IP(inode
);
444 struct super_block
*sb
= inode
->i_sb
;
446 int ea_size
= sizeDXD(&ji
->ea
);
447 int blocks_needed
, current_blocks
;
450 int quota_allocation
= 0;
452 /* When fsck.jfs clears a bad ea, it doesn't clear the size */
453 if (ji
->ea
.flag
== 0)
459 ea_buf
->max_size
= 0;
460 ea_buf
->xattr
= NULL
;
463 if ((min_size
<= sizeof (ji
->i_inline_ea
)) &&
464 (ji
->mode2
& INLINEEA
)) {
465 ea_buf
->flag
= EA_INLINE
| EA_NEW
;
466 ea_buf
->max_size
= sizeof (ji
->i_inline_ea
);
467 ea_buf
->xattr
= (struct jfs_ea_list
*) ji
->i_inline_ea
;
468 DXDlength(&ea_buf
->new_ea
, 0);
469 DXDaddress(&ea_buf
->new_ea
, 0);
470 ea_buf
->new_ea
.flag
= DXD_INLINE
;
471 DXDsize(&ea_buf
->new_ea
, min_size
);
475 } else if (ji
->ea
.flag
& DXD_INLINE
) {
476 if (min_size
<= sizeof (ji
->i_inline_ea
)) {
477 ea_buf
->flag
= EA_INLINE
;
478 ea_buf
->max_size
= sizeof (ji
->i_inline_ea
);
479 ea_buf
->xattr
= (struct jfs_ea_list
*) ji
->i_inline_ea
;
484 if (!(ji
->ea
.flag
& DXD_EXTENT
)) {
485 jfs_error(sb
, "ea_get: invalid ea.flag)");
488 current_blocks
= (ea_size
+ sb
->s_blocksize
- 1) >>
489 sb
->s_blocksize_bits
;
491 size
= max(min_size
, ea_size
);
495 * To keep the rest of the code simple. Allocate a
496 * contiguous buffer to work with
498 ea_buf
->xattr
= kmalloc(size
, GFP_KERNEL
);
499 if (ea_buf
->xattr
== NULL
)
502 ea_buf
->flag
= EA_MALLOC
;
503 ea_buf
->max_size
= (size
+ sb
->s_blocksize
- 1) &
504 ~(sb
->s_blocksize
- 1);
509 if ((rc
= ea_read(inode
, ea_buf
->xattr
))) {
510 kfree(ea_buf
->xattr
);
511 ea_buf
->xattr
= NULL
;
516 blocks_needed
= (min_size
+ sb
->s_blocksize
- 1) >>
517 sb
->s_blocksize_bits
;
519 if (blocks_needed
> current_blocks
) {
520 /* Allocate new blocks to quota. */
521 rc
= dquot_alloc_block(inode
, blocks_needed
);
525 quota_allocation
= blocks_needed
;
527 rc
= dbAlloc(inode
, INOHINT(inode
), (s64
) blocks_needed
,
532 DXDlength(&ea_buf
->new_ea
, blocks_needed
);
533 DXDaddress(&ea_buf
->new_ea
, blkno
);
534 ea_buf
->new_ea
.flag
= DXD_EXTENT
;
535 DXDsize(&ea_buf
->new_ea
, min_size
);
537 ea_buf
->flag
= EA_EXTENT
| EA_NEW
;
539 ea_buf
->mp
= get_metapage(inode
, blkno
,
540 blocks_needed
<< sb
->s_blocksize_bits
,
542 if (ea_buf
->mp
== NULL
) {
543 dbFree(inode
, blkno
, (s64
) blocks_needed
);
547 ea_buf
->xattr
= ea_buf
->mp
->data
;
548 ea_buf
->max_size
= (min_size
+ sb
->s_blocksize
- 1) &
549 ~(sb
->s_blocksize
- 1);
552 if ((rc
= ea_read(inode
, ea_buf
->xattr
))) {
553 discard_metapage(ea_buf
->mp
);
554 dbFree(inode
, blkno
, (s64
) blocks_needed
);
559 ea_buf
->flag
= EA_EXTENT
;
560 ea_buf
->mp
= read_metapage(inode
, addressDXD(&ji
->ea
),
561 lengthDXD(&ji
->ea
) << sb
->s_blocksize_bits
,
563 if (ea_buf
->mp
== NULL
) {
567 ea_buf
->xattr
= ea_buf
->mp
->data
;
568 ea_buf
->max_size
= (ea_size
+ sb
->s_blocksize
- 1) &
569 ~(sb
->s_blocksize
- 1);
572 if (EALIST_SIZE(ea_buf
->xattr
) != ea_size
) {
573 printk(KERN_ERR
"ea_get: invalid extended attribute\n");
574 print_hex_dump(KERN_ERR
, "", DUMP_PREFIX_ADDRESS
, 16, 1,
575 ea_buf
->xattr
, ea_size
, 1);
576 ea_release(inode
, ea_buf
);
584 /* Rollback quota allocation */
585 if (quota_allocation
)
586 dquot_free_block(inode
, quota_allocation
);
591 static void ea_release(struct inode
*inode
, struct ea_buffer
*ea_buf
)
593 if (ea_buf
->flag
& EA_MALLOC
)
594 kfree(ea_buf
->xattr
);
595 else if (ea_buf
->flag
& EA_EXTENT
) {
597 release_metapage(ea_buf
->mp
);
599 if (ea_buf
->flag
& EA_NEW
)
600 dbFree(inode
, addressDXD(&ea_buf
->new_ea
),
601 lengthDXD(&ea_buf
->new_ea
));
605 static int ea_put(tid_t tid
, struct inode
*inode
, struct ea_buffer
*ea_buf
,
608 struct jfs_inode_info
*ji
= JFS_IP(inode
);
609 unsigned long old_blocks
, new_blocks
;
613 ea_release(inode
, ea_buf
);
615 } else if (ea_buf
->flag
& EA_INLINE
) {
616 assert(new_size
<= sizeof (ji
->i_inline_ea
));
617 ji
->mode2
&= ~INLINEEA
;
618 ea_buf
->new_ea
.flag
= DXD_INLINE
;
619 DXDsize(&ea_buf
->new_ea
, new_size
);
620 DXDaddress(&ea_buf
->new_ea
, 0);
621 DXDlength(&ea_buf
->new_ea
, 0);
622 } else if (ea_buf
->flag
& EA_MALLOC
) {
623 rc
= ea_write(inode
, ea_buf
->xattr
, new_size
, &ea_buf
->new_ea
);
624 kfree(ea_buf
->xattr
);
625 } else if (ea_buf
->flag
& EA_NEW
) {
626 /* We have already allocated a new dxd */
627 flush_metapage(ea_buf
->mp
);
629 /* ->xattr must point to original ea's metapage */
630 rc
= ea_write(inode
, ea_buf
->xattr
, new_size
, &ea_buf
->new_ea
);
631 discard_metapage(ea_buf
->mp
);
636 old_blocks
= new_blocks
= 0;
638 if (ji
->ea
.flag
& DXD_EXTENT
) {
639 invalidate_dxd_metapages(inode
, ji
->ea
);
640 old_blocks
= lengthDXD(&ji
->ea
);
644 txEA(tid
, inode
, &ji
->ea
, &ea_buf
->new_ea
);
645 if (ea_buf
->new_ea
.flag
& DXD_EXTENT
) {
646 new_blocks
= lengthDXD(&ea_buf
->new_ea
);
647 if (ji
->ea
.flag
& DXD_INLINE
)
648 ji
->mode2
|= INLINEEA
;
650 ji
->ea
= ea_buf
->new_ea
;
652 txEA(tid
, inode
, &ji
->ea
, NULL
);
653 if (ji
->ea
.flag
& DXD_INLINE
)
654 ji
->mode2
|= INLINEEA
;
659 /* If old blocks exist, they must be removed from quota allocation. */
661 dquot_free_block(inode
, old_blocks
);
663 inode
->i_ctime
= CURRENT_TIME
;
669 * can_set_system_xattr
671 * This code is specific to the system.* namespace. It contains policy
672 * which doesn't belong in the main xattr codepath.
674 static int can_set_system_xattr(struct inode
*inode
, const char *name
,
675 const void *value
, size_t value_len
)
677 #ifdef CONFIG_JFS_POSIX_ACL
678 struct posix_acl
*acl
;
681 if (!inode_owner_or_capable(inode
))
685 * POSIX_ACL_XATTR_ACCESS is tied to i_mode
687 if (strcmp(name
, POSIX_ACL_XATTR_ACCESS
) == 0) {
688 acl
= posix_acl_from_xattr(value
, value_len
);
691 printk(KERN_ERR
"posix_acl_from_xattr returned %d\n",
696 mode_t mode
= inode
->i_mode
;
697 rc
= posix_acl_equiv_mode(acl
, &mode
);
698 posix_acl_release(acl
);
701 "posix_acl_equiv_mode returned %d\n",
705 inode
->i_mode
= mode
;
706 mark_inode_dirty(inode
);
709 * We're changing the ACL. Get rid of the cached one
711 forget_cached_acl(inode
, ACL_TYPE_ACCESS
);
714 } else if (strcmp(name
, POSIX_ACL_XATTR_DEFAULT
) == 0) {
715 acl
= posix_acl_from_xattr(value
, value_len
);
718 printk(KERN_ERR
"posix_acl_from_xattr returned %d\n",
722 posix_acl_release(acl
);
725 * We're changing the default ACL. Get rid of the cached one
727 forget_cached_acl(inode
, ACL_TYPE_DEFAULT
);
731 #endif /* CONFIG_JFS_POSIX_ACL */
736 * Most of the permission checking is done by xattr_permission in the vfs.
737 * The local file system is responsible for handling the system.* namespace.
738 * We also need to verify that this is a namespace that we recognize.
740 static int can_set_xattr(struct inode
*inode
, const char *name
,
741 const void *value
, size_t value_len
)
743 if (!strncmp(name
, XATTR_SYSTEM_PREFIX
, XATTR_SYSTEM_PREFIX_LEN
))
744 return can_set_system_xattr(inode
, name
, value
, value_len
);
746 if (!strncmp(name
, XATTR_OS2_PREFIX
, XATTR_OS2_PREFIX_LEN
)) {
748 * This makes sure that we aren't trying to set an
749 * attribute in a different namespace by prefixing it
752 if (is_known_namespace(name
+ XATTR_OS2_PREFIX_LEN
))
758 * Don't allow setting an attribute in an unknown namespace.
760 if (strncmp(name
, XATTR_TRUSTED_PREFIX
, XATTR_TRUSTED_PREFIX_LEN
) &&
761 strncmp(name
, XATTR_SECURITY_PREFIX
, XATTR_SECURITY_PREFIX_LEN
) &&
762 strncmp(name
, XATTR_USER_PREFIX
, XATTR_USER_PREFIX_LEN
))
768 int __jfs_setxattr(tid_t tid
, struct inode
*inode
, const char *name
,
769 const void *value
, size_t value_len
, int flags
)
771 struct jfs_ea_list
*ealist
;
772 struct jfs_ea
*ea
, *old_ea
= NULL
, *next_ea
= NULL
;
773 struct ea_buffer ea_buf
;
777 int namelen
= strlen(name
);
778 char *os2name
= NULL
;
783 if (strncmp(name
, XATTR_OS2_PREFIX
, XATTR_OS2_PREFIX_LEN
) == 0) {
784 os2name
= kmalloc(namelen
- XATTR_OS2_PREFIX_LEN
+ 1,
788 strcpy(os2name
, name
+ XATTR_OS2_PREFIX_LEN
);
790 namelen
-= XATTR_OS2_PREFIX_LEN
;
793 down_write(&JFS_IP(inode
)->xattr_sem
);
795 xattr_size
= ea_get(inode
, &ea_buf
, 0);
796 if (xattr_size
< 0) {
802 ealist
= (struct jfs_ea_list
*) ea_buf
.xattr
;
803 new_size
= sizeof (struct jfs_ea_list
);
806 for (ea
= FIRST_EA(ealist
); ea
< END_EALIST(ealist
);
808 if ((namelen
== ea
->namelen
) &&
809 (memcmp(name
, ea
->name
, namelen
) == 0)) {
811 if (flags
& XATTR_CREATE
) {
816 old_ea_size
= EA_SIZE(ea
);
817 next_ea
= NEXT_EA(ea
);
819 new_size
+= EA_SIZE(ea
);
824 if (flags
& XATTR_REPLACE
) {
834 new_size
+= sizeof (struct jfs_ea
) + namelen
+ 1 + value_len
;
836 if (new_size
> ea_buf
.max_size
) {
838 * We need to allocate more space for merged ea list.
839 * We should only have loop to again: once.
841 ea_release(inode
, &ea_buf
);
842 xattr_size
= ea_get(inode
, &ea_buf
, new_size
);
843 if (xattr_size
< 0) {
850 /* Remove old ea of the same name */
852 /* number of bytes following target EA */
853 length
= (char *) END_EALIST(ealist
) - (char *) next_ea
;
855 memmove(old_ea
, next_ea
, length
);
856 xattr_size
-= old_ea_size
;
859 /* Add new entry to the end */
862 /* Completely new ea list */
863 xattr_size
= sizeof (struct jfs_ea_list
);
865 ea
= (struct jfs_ea
*) ((char *) ealist
+ xattr_size
);
867 ea
->namelen
= namelen
;
868 ea
->valuelen
= (cpu_to_le16(value_len
));
869 memcpy(ea
->name
, name
, namelen
);
870 ea
->name
[namelen
] = 0;
872 memcpy(&ea
->name
[namelen
+ 1], value
, value_len
);
873 xattr_size
+= EA_SIZE(ea
);
876 /* DEBUG - If we did this right, these number match */
877 if (xattr_size
!= new_size
) {
879 "jfs_xsetattr: xattr_size = %d, new_size = %d\n",
880 xattr_size
, new_size
);
887 * If we're left with an empty list, there's no ea
889 if (new_size
== sizeof (struct jfs_ea_list
))
892 ealist
->size
= cpu_to_le32(new_size
);
894 rc
= ea_put(tid
, inode
, &ea_buf
, new_size
);
898 ea_release(inode
, &ea_buf
);
900 up_write(&JFS_IP(inode
)->xattr_sem
);
907 int jfs_setxattr(struct dentry
*dentry
, const char *name
, const void *value
,
908 size_t value_len
, int flags
)
910 struct inode
*inode
= dentry
->d_inode
;
911 struct jfs_inode_info
*ji
= JFS_IP(inode
);
915 if ((rc
= can_set_xattr(inode
, name
, value
, value_len
)))
918 if (value
== NULL
) { /* empty EA, do not remove */
923 tid
= txBegin(inode
->i_sb
, 0);
924 mutex_lock(&ji
->commit_mutex
);
925 rc
= __jfs_setxattr(tid
, dentry
->d_inode
, name
, value
, value_len
,
928 rc
= txCommit(tid
, 1, &inode
, 0);
930 mutex_unlock(&ji
->commit_mutex
);
935 ssize_t
__jfs_getxattr(struct inode
*inode
, const char *name
, void *data
,
938 struct jfs_ea_list
*ealist
;
940 struct ea_buffer ea_buf
;
943 int namelen
= strlen(name
);
946 down_read(&JFS_IP(inode
)->xattr_sem
);
948 xattr_size
= ea_get(inode
, &ea_buf
, 0);
950 if (xattr_size
< 0) {
958 ealist
= (struct jfs_ea_list
*) ea_buf
.xattr
;
960 /* Find the named attribute */
961 for (ea
= FIRST_EA(ealist
); ea
< END_EALIST(ealist
); ea
= NEXT_EA(ea
))
962 if ((namelen
== ea
->namelen
) &&
963 memcmp(name
, ea
->name
, namelen
) == 0) {
965 size
= le16_to_cpu(ea
->valuelen
);
968 else if (size
> buf_size
) {
972 value
= ((char *) &ea
->name
) + ea
->namelen
+ 1;
973 memcpy(data
, value
, size
);
979 ea_release(inode
, &ea_buf
);
981 up_read(&JFS_IP(inode
)->xattr_sem
);
986 ssize_t
jfs_getxattr(struct dentry
*dentry
, const char *name
, void *data
,
991 if (strncmp(name
, XATTR_OS2_PREFIX
, XATTR_OS2_PREFIX_LEN
) == 0) {
993 * skip past "os2." prefix
995 name
+= XATTR_OS2_PREFIX_LEN
;
997 * Don't allow retrieving properly prefixed attributes
998 * by prepending them with "os2."
1000 if (is_known_namespace(name
))
1004 err
= __jfs_getxattr(dentry
->d_inode
, name
, data
, buf_size
);
1010 * No special permissions are needed to list attributes except for trusted.*
1012 static inline int can_list(struct jfs_ea
*ea
)
1014 return (strncmp(ea
->name
, XATTR_TRUSTED_PREFIX
,
1015 XATTR_TRUSTED_PREFIX_LEN
) ||
1016 capable(CAP_SYS_ADMIN
));
1019 ssize_t
jfs_listxattr(struct dentry
* dentry
, char *data
, size_t buf_size
)
1021 struct inode
*inode
= dentry
->d_inode
;
1025 struct jfs_ea_list
*ealist
;
1027 struct ea_buffer ea_buf
;
1029 down_read(&JFS_IP(inode
)->xattr_sem
);
1031 xattr_size
= ea_get(inode
, &ea_buf
, 0);
1032 if (xattr_size
< 0) {
1037 if (xattr_size
== 0)
1040 ealist
= (struct jfs_ea_list
*) ea_buf
.xattr
;
1042 /* compute required size of list */
1043 for (ea
= FIRST_EA(ealist
); ea
< END_EALIST(ealist
); ea
= NEXT_EA(ea
)) {
1045 size
+= name_size(ea
) + 1;
1051 if (size
> buf_size
) {
1056 /* Copy attribute names to buffer */
1058 for (ea
= FIRST_EA(ealist
); ea
< END_EALIST(ealist
); ea
= NEXT_EA(ea
)) {
1060 int namelen
= copy_name(buffer
, ea
);
1061 buffer
+= namelen
+ 1;
1066 ea_release(inode
, &ea_buf
);
1068 up_read(&JFS_IP(inode
)->xattr_sem
);
1072 int jfs_removexattr(struct dentry
*dentry
, const char *name
)
1074 struct inode
*inode
= dentry
->d_inode
;
1075 struct jfs_inode_info
*ji
= JFS_IP(inode
);
1079 if ((rc
= can_set_xattr(inode
, name
, NULL
, 0)))
1082 tid
= txBegin(inode
->i_sb
, 0);
1083 mutex_lock(&ji
->commit_mutex
);
1084 rc
= __jfs_setxattr(tid
, dentry
->d_inode
, name
, NULL
, 0, XATTR_REPLACE
);
1086 rc
= txCommit(tid
, 1, &inode
, 0);
1088 mutex_unlock(&ji
->commit_mutex
);
1093 #ifdef CONFIG_JFS_SECURITY
1094 int jfs_initxattrs(struct inode
*inode
, const struct xattr
*xattr_array
,
1097 const struct xattr
*xattr
;
1098 tid_t
*tid
= fs_info
;
1102 for (xattr
= xattr_array
; xattr
->name
!= NULL
; xattr
++) {
1103 name
= kmalloc(XATTR_SECURITY_PREFIX_LEN
+
1104 strlen(xattr
->name
) + 1, GFP_NOFS
);
1109 strcpy(name
, XATTR_SECURITY_PREFIX
);
1110 strcpy(name
+ XATTR_SECURITY_PREFIX_LEN
, xattr
->name
);
1112 err
= __jfs_setxattr(*tid
, inode
, name
,
1113 xattr
->value
, xattr
->value_len
, 0);
1121 int jfs_init_security(tid_t tid
, struct inode
*inode
, struct inode
*dir
,
1122 const struct qstr
*qstr
)
1124 return security_inode_init_security(inode
, dir
, qstr
,
1125 &jfs_initxattrs
, &tid
);