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/quotaops.h>
25 #include <linux/security.h>
26 #include <linux/vs_dlimit.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 if (DQUOT_ALLOC_BLOCK(ip, nblocks)) {
267 /* Allocate new blocks to dlimit. */
268 if (DLIMIT_ALLOC_BLOCK(ip, nblocks)) {
269 DQUOT_FREE_BLOCK(ip, nblocks);
273 rc = dbAlloc(ip, INOHINT(ip), nblocks, &blkno);
275 /*Rollback dlimit allocation. */
276 DLIMIT_FREE_BLOCK(ip, nblocks);
277 /*Rollback quota allocation. */
278 DQUOT_FREE_BLOCK(ip, nblocks);
283 * Now have nblocks worth of storage to stuff into the FEALIST.
284 * loop over the FEALIST copying data into the buffer one page at
287 cp = (char *) ealist;
289 for (i = 0; i < nblocks; i += sbi->nbperpage) {
291 * Determine how many bytes for this request, and round up to
292 * the nearest aggregate block size
294 nb = min(PSIZE, nbytes);
296 ((((nb + sb->s_blocksize - 1)) >> sb->s_blocksize_bits))
297 << sb->s_blocksize_bits;
299 if (!(mp = get_metapage(ip, blkno + i, bytes_to_write, 1))) {
304 memcpy(mp->data, cp, nb);
307 * We really need a way to propagate errors for
308 * forced writes like this one. --hch
310 * (__write_metapage => release_metapage => flush_metapage)
313 if ((rc = flush_metapage(mp))) {
315 * the write failed -- this means that the buffer
316 * is still assigned and the blocks are not being
317 * used. this seems like the best error recovery
330 ea->flag = DXD_EXTENT;
331 DXDsize(ea, le32_to_cpu(ealist->size));
332 DXDlength(ea, nblocks);
333 DXDaddress(ea, blkno);
335 /* Free up INLINE area */
336 if (ji->ea.flag & DXD_INLINE)
337 ji->mode2 |= INLINEEA;
342 /* Rollback quota allocation. */
343 DLIMIT_FREE_BLOCK(ip, nblocks);
344 /* Rollback quota allocation. */
345 DQUOT_FREE_BLOCK(ip, nblocks);
347 dbFree(ip, blkno, nblocks);
352 * NAME: ea_read_inline
354 * FUNCTION: Read an inlined EA into user's buffer
358 * ealist - Pointer to buffer to fill in with EA
362 static int ea_read_inline(struct inode *ip, struct jfs_ea_list *ealist)
364 struct jfs_inode_info *ji = JFS_IP(ip);
365 int ea_size = sizeDXD(&ji->ea);
373 if ((sizeDXD(&ji->ea) > sizeof (ji->i_inline_ea)))
375 if (le32_to_cpu(((struct jfs_ea_list *) &ji->i_inline_ea)->size)
379 memcpy(ealist, ji->i_inline_ea, ea_size);
386 * FUNCTION: copy EA data into user's buffer
390 * ealist - Pointer to buffer to fill in with EA
392 * NOTES: If EA is inline calls ea_read_inline() to copy EA.
394 * RETURNS: 0 for success; other indicates failure
396 static int ea_read(struct inode *ip, struct jfs_ea_list *ealist)
398 struct super_block *sb = ip->i_sb;
399 struct jfs_inode_info *ji = JFS_IP(ip);
400 struct jfs_sb_info *sbi = JFS_SBI(sb);
403 char *cp = (char *) ealist;
409 /* quick check for in-line EA */
410 if (ji->ea.flag & DXD_INLINE)
411 return ea_read_inline(ip, ealist);
413 nbytes = sizeDXD(&ji->ea);
415 jfs_error(sb, "ea_read: nbytes is 0");
420 * Figure out how many blocks were allocated when this EA list was
421 * originally written to disk.
423 nblocks = lengthDXD(&ji->ea) << sbi->l2nbperpage;
424 blkno = addressDXD(&ji->ea) << sbi->l2nbperpage;
427 * I have found the disk blocks which were originally used to store
428 * the FEALIST. now i loop over each contiguous block copying the
429 * data into the buffer.
431 for (i = 0; i < nblocks; i += sbi->nbperpage) {
433 * Determine how many bytes for this request, and round up to
434 * the nearest aggregate block size
436 nb = min(PSIZE, nbytes);
438 ((((nb + sb->s_blocksize - 1)) >> sb->s_blocksize_bits))
439 << sb->s_blocksize_bits;
441 if (!(mp = read_metapage(ip, blkno + i, bytes_to_read, 1)))
444 memcpy(cp, mp->data, nb);
445 release_metapage(mp);
457 * FUNCTION: Returns buffer containing existing extended attributes.
458 * The size of the buffer will be the larger of the existing
459 * attributes size, or min_size.
461 * The buffer, which may be inlined in the inode or in the
462 * page cache must be release by calling ea_release or ea_put
465 * inode - Inode pointer
466 * ea_buf - Structure to be populated with ealist and its metadata
467 * min_size- minimum size of buffer to be returned
469 * RETURNS: 0 for success; Other indicates failure
471 static int ea_get(struct inode *inode, struct ea_buffer *ea_buf, int min_size)
473 struct jfs_inode_info *ji = JFS_IP(inode);
474 struct super_block *sb = inode->i_sb;
476 int ea_size = sizeDXD(&ji->ea);
477 int blocks_needed, current_blocks;
480 int quota_allocation = 0;
481 int dlimit_allocation = 0;
483 /* When fsck.jfs clears a bad ea, it doesn't clear the size */
484 if (ji->ea.flag == 0)
490 ea_buf->max_size = 0;
491 ea_buf->xattr = NULL;
494 if ((min_size <= sizeof (ji->i_inline_ea)) &&
495 (ji->mode2 & INLINEEA)) {
496 ea_buf->flag = EA_INLINE | EA_NEW;
497 ea_buf->max_size = sizeof (ji->i_inline_ea);
498 ea_buf->xattr = (struct jfs_ea_list *) ji->i_inline_ea;
499 DXDlength(&ea_buf->new_ea, 0);
500 DXDaddress(&ea_buf->new_ea, 0);
501 ea_buf->new_ea.flag = DXD_INLINE;
502 DXDsize(&ea_buf->new_ea, min_size);
506 } else if (ji->ea.flag & DXD_INLINE) {
507 if (min_size <= sizeof (ji->i_inline_ea)) {
508 ea_buf->flag = EA_INLINE;
509 ea_buf->max_size = sizeof (ji->i_inline_ea);
510 ea_buf->xattr = (struct jfs_ea_list *) ji->i_inline_ea;
515 if (!(ji->ea.flag & DXD_EXTENT)) {
516 jfs_error(sb, "ea_get: invalid ea.flag)");
519 current_blocks = (ea_size + sb->s_blocksize - 1) >>
520 sb->s_blocksize_bits;
522 size = max(min_size, ea_size);
526 * To keep the rest of the code simple. Allocate a
527 * contiguous buffer to work with
529 ea_buf->xattr = kmalloc(size, GFP_KERNEL);
530 if (ea_buf->xattr == NULL)
533 ea_buf->flag = EA_MALLOC;
534 ea_buf->max_size = (size + sb->s_blocksize - 1) &
535 ~(sb->s_blocksize - 1);
540 if ((rc = ea_read(inode, ea_buf->xattr))) {
541 kfree(ea_buf->xattr);
542 ea_buf->xattr = NULL;
547 blocks_needed = (min_size + sb->s_blocksize - 1) >>
548 sb->s_blocksize_bits;
550 if (blocks_needed > current_blocks) {
551 /* Allocate new blocks to quota. */
552 if (DQUOT_ALLOC_BLOCK(inode, blocks_needed))
555 quota_allocation = blocks_needed;
557 /* Allocate new blocks to dlimit. */
559 if (DLIMIT_ALLOC_BLOCK(inode, blocks_needed))
561 dlimit_allocation = blocks_needed;
563 rc = dbAlloc(inode, INOHINT(inode), (s64) blocks_needed,
568 DXDlength(&ea_buf->new_ea, blocks_needed);
569 DXDaddress(&ea_buf->new_ea, blkno);
570 ea_buf->new_ea.flag = DXD_EXTENT;
571 DXDsize(&ea_buf->new_ea, min_size);
573 ea_buf->flag = EA_EXTENT | EA_NEW;
575 ea_buf->mp = get_metapage(inode, blkno,
576 blocks_needed << sb->s_blocksize_bits,
578 if (ea_buf->mp == NULL) {
579 dbFree(inode, blkno, (s64) blocks_needed);
583 ea_buf->xattr = ea_buf->mp->data;
584 ea_buf->max_size = (min_size + sb->s_blocksize - 1) &
585 ~(sb->s_blocksize - 1);
588 if ((rc = ea_read(inode, ea_buf->xattr))) {
589 discard_metapage(ea_buf->mp);
590 dbFree(inode, blkno, (s64) blocks_needed);
595 ea_buf->flag = EA_EXTENT;
596 ea_buf->mp = read_metapage(inode, addressDXD(&ji->ea),
597 lengthDXD(&ji->ea) << sb->s_blocksize_bits,
599 if (ea_buf->mp == NULL) {
603 ea_buf->xattr = ea_buf->mp->data;
604 ea_buf->max_size = (ea_size + sb->s_blocksize - 1) &
605 ~(sb->s_blocksize - 1);
608 if (EALIST_SIZE(ea_buf->xattr) != ea_size) {
609 printk(KERN_ERR "ea_get: invalid extended attribute\n");
610 dump_mem("xattr", ea_buf->xattr, ea_size);
611 ea_release(inode, ea_buf);
619 /* Rollback dlimit allocation */
620 if (dlimit_allocation)
621 DLIMIT_FREE_BLOCK(inode, dlimit_allocation);
622 /* Rollback quota allocation */
623 if (quota_allocation)
624 DQUOT_FREE_BLOCK(inode, quota_allocation);
629 static void ea_release(struct inode *inode, struct ea_buffer *ea_buf)
631 if (ea_buf->flag & EA_MALLOC)
632 kfree(ea_buf->xattr);
633 else if (ea_buf->flag & EA_EXTENT) {
635 release_metapage(ea_buf->mp);
637 if (ea_buf->flag & EA_NEW)
638 dbFree(inode, addressDXD(&ea_buf->new_ea),
639 lengthDXD(&ea_buf->new_ea));
643 static int ea_put(tid_t tid, struct inode *inode, struct ea_buffer *ea_buf,
646 struct jfs_inode_info *ji = JFS_IP(inode);
647 unsigned long old_blocks, new_blocks;
651 ea_release(inode, ea_buf);
653 } else if (ea_buf->flag & EA_INLINE) {
654 assert(new_size <= sizeof (ji->i_inline_ea));
655 ji->mode2 &= ~INLINEEA;
656 ea_buf->new_ea.flag = DXD_INLINE;
657 DXDsize(&ea_buf->new_ea, new_size);
658 DXDaddress(&ea_buf->new_ea, 0);
659 DXDlength(&ea_buf->new_ea, 0);
660 } else if (ea_buf->flag & EA_MALLOC) {
661 rc = ea_write(inode, ea_buf->xattr, new_size, &ea_buf->new_ea);
662 kfree(ea_buf->xattr);
663 } else if (ea_buf->flag & EA_NEW) {
664 /* We have already allocated a new dxd */
665 flush_metapage(ea_buf->mp);
667 /* ->xattr must point to original ea's metapage */
668 rc = ea_write(inode, ea_buf->xattr, new_size, &ea_buf->new_ea);
669 discard_metapage(ea_buf->mp);
674 old_blocks = new_blocks = 0;
676 if (ji->ea.flag & DXD_EXTENT) {
677 invalidate_dxd_metapages(inode, ji->ea);
678 old_blocks = lengthDXD(&ji->ea);
682 txEA(tid, inode, &ji->ea, &ea_buf->new_ea);
683 if (ea_buf->new_ea.flag & DXD_EXTENT) {
684 new_blocks = lengthDXD(&ea_buf->new_ea);
685 if (ji->ea.flag & DXD_INLINE)
686 ji->mode2 |= INLINEEA;
688 ji->ea = ea_buf->new_ea;
690 txEA(tid, inode, &ji->ea, NULL);
691 if (ji->ea.flag & DXD_INLINE)
692 ji->mode2 |= INLINEEA;
697 /* If old blocks exist, they must be removed from quota allocation. */
699 DLIMIT_FREE_BLOCK(inode, old_blocks);
700 DQUOT_FREE_BLOCK(inode, old_blocks);
703 inode->i_ctime = CURRENT_TIME;
709 * can_set_system_xattr
711 * This code is specific to the system.* namespace. It contains policy
712 * which doesn't belong in the main xattr codepath.
714 static int can_set_system_xattr(struct inode *inode, const char *name,
715 const void *value, size_t value_len)
717 #ifdef CONFIG_JFS_POSIX_ACL
718 struct posix_acl *acl;
721 if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
725 * POSIX_ACL_XATTR_ACCESS is tied to i_mode
727 if (strcmp(name, POSIX_ACL_XATTR_ACCESS) == 0) {
728 acl = posix_acl_from_xattr(value, value_len);
731 printk(KERN_ERR "posix_acl_from_xattr returned %d\n",
736 mode_t mode = inode->i_mode;
737 rc = posix_acl_equiv_mode(acl, &mode);
738 posix_acl_release(acl);
741 "posix_acl_equiv_mode returned %d\n",
745 inode->i_mode = mode;
746 mark_inode_dirty(inode);
749 * We're changing the ACL. Get rid of the cached one
751 acl =JFS_IP(inode)->i_acl;
752 if (acl != JFS_ACL_NOT_CACHED)
753 posix_acl_release(acl);
754 JFS_IP(inode)->i_acl = JFS_ACL_NOT_CACHED;
757 } else if (strcmp(name, POSIX_ACL_XATTR_DEFAULT) == 0) {
758 acl = posix_acl_from_xattr(value, value_len);
761 printk(KERN_ERR "posix_acl_from_xattr returned %d\n",
765 posix_acl_release(acl);
768 * We're changing the default ACL. Get rid of the cached one
770 acl =JFS_IP(inode)->i_default_acl;
771 if (acl && (acl != JFS_ACL_NOT_CACHED))
772 posix_acl_release(acl);
773 JFS_IP(inode)->i_default_acl = JFS_ACL_NOT_CACHED;
777 #endif /* CONFIG_JFS_POSIX_ACL */
781 static int can_set_xattr(struct inode *inode, const char *name,
782 const void *value, size_t value_len)
784 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
785 return can_set_system_xattr(inode, name, value, value_len);
788 * Don't allow setting an attribute in an unknown namespace.
790 if (strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) &&
791 strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) &&
792 strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN) &&
793 strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN))
796 if (!S_ISREG(inode->i_mode) &&
797 (!S_ISDIR(inode->i_mode) || inode->i_mode &S_ISVTX))
803 int __jfs_setxattr(tid_t tid, struct inode *inode, const char *name,
804 const void *value, size_t value_len, int flags)
806 struct jfs_ea_list *ealist;
807 struct jfs_ea *ea, *old_ea = NULL, *next_ea = NULL;
808 struct ea_buffer ea_buf;
812 int namelen = strlen(name);
813 char *os2name = NULL;
818 if (strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN) == 0) {
819 os2name = kmalloc(namelen - XATTR_OS2_PREFIX_LEN + 1,
823 strcpy(os2name, name + XATTR_OS2_PREFIX_LEN);
825 namelen -= XATTR_OS2_PREFIX_LEN;
828 down_write(&JFS_IP(inode)->xattr_sem);
830 xattr_size = ea_get(inode, &ea_buf, 0);
831 if (xattr_size < 0) {
837 ealist = (struct jfs_ea_list *) ea_buf.xattr;
838 new_size = sizeof (struct jfs_ea_list);
841 for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist);
843 if ((namelen == ea->namelen) &&
844 (memcmp(name, ea->name, namelen) == 0)) {
846 if (flags & XATTR_CREATE) {
851 old_ea_size = EA_SIZE(ea);
852 next_ea = NEXT_EA(ea);
854 new_size += EA_SIZE(ea);
859 if (flags & XATTR_REPLACE) {
869 new_size += sizeof (struct jfs_ea) + namelen + 1 + value_len;
871 if (new_size > ea_buf.max_size) {
873 * We need to allocate more space for merged ea list.
874 * We should only have loop to again: once.
876 ea_release(inode, &ea_buf);
877 xattr_size = ea_get(inode, &ea_buf, new_size);
878 if (xattr_size < 0) {
885 /* Remove old ea of the same name */
887 /* number of bytes following target EA */
888 length = (char *) END_EALIST(ealist) - (char *) next_ea;
890 memmove(old_ea, next_ea, length);
891 xattr_size -= old_ea_size;
894 /* Add new entry to the end */
897 /* Completely new ea list */
898 xattr_size = sizeof (struct jfs_ea_list);
900 ea = (struct jfs_ea *) ((char *) ealist + xattr_size);
902 ea->namelen = namelen;
903 ea->valuelen = (cpu_to_le16(value_len));
904 memcpy(ea->name, name, namelen);
905 ea->name[namelen] = 0;
907 memcpy(&ea->name[namelen + 1], value, value_len);
908 xattr_size += EA_SIZE(ea);
911 /* DEBUG - If we did this right, these number match */
912 if (xattr_size != new_size) {
914 "jfs_xsetattr: xattr_size = %d, new_size = %d\n",
915 xattr_size, new_size);
922 * If we're left with an empty list, there's no ea
924 if (new_size == sizeof (struct jfs_ea_list))
927 ealist->size = cpu_to_le32(new_size);
929 rc = ea_put(tid, inode, &ea_buf, new_size);
933 ea_release(inode, &ea_buf);
935 up_write(&JFS_IP(inode)->xattr_sem);
942 int jfs_setxattr(struct dentry *dentry, const char *name, const void *value,
943 size_t value_len, int flags)
945 struct inode *inode = dentry->d_inode;
946 struct jfs_inode_info *ji = JFS_IP(inode);
950 if ((rc = can_set_xattr(inode, name, value, value_len)))
953 if (value == NULL) { /* empty EA, do not remove */
958 tid = txBegin(inode->i_sb, 0);
959 mutex_lock(&ji->commit_mutex);
960 rc = __jfs_setxattr(tid, dentry->d_inode, name, value, value_len,
963 rc = txCommit(tid, 1, &inode, 0);
965 mutex_unlock(&ji->commit_mutex);
970 ssize_t __jfs_getxattr(struct inode *inode, const char *name, void *data,
973 struct jfs_ea_list *ealist;
975 struct ea_buffer ea_buf;
978 int namelen = strlen(name);
979 char *os2name = NULL;
982 if (strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN) == 0) {
983 os2name = kmalloc(namelen - XATTR_OS2_PREFIX_LEN + 1,
987 strcpy(os2name, name + XATTR_OS2_PREFIX_LEN);
989 namelen -= XATTR_OS2_PREFIX_LEN;
992 down_read(&JFS_IP(inode)->xattr_sem);
994 xattr_size = ea_get(inode, &ea_buf, 0);
996 if (xattr_size < 0) {
1001 if (xattr_size == 0)
1004 ealist = (struct jfs_ea_list *) ea_buf.xattr;
1006 /* Find the named attribute */
1007 for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea))
1008 if ((namelen == ea->namelen) &&
1009 memcmp(name, ea->name, namelen) == 0) {
1011 size = le16_to_cpu(ea->valuelen);
1014 else if (size > buf_size) {
1018 value = ((char *) &ea->name) + ea->namelen + 1;
1019 memcpy(data, value, size);
1025 ea_release(inode, &ea_buf);
1027 up_read(&JFS_IP(inode)->xattr_sem);
1034 ssize_t jfs_getxattr(struct dentry *dentry, const char *name, void *data,
1039 err = __jfs_getxattr(dentry->d_inode, name, data, buf_size);
1045 * No special permissions are needed to list attributes except for trusted.*
1047 static inline int can_list(struct jfs_ea *ea)
1049 return (strncmp(ea->name, XATTR_TRUSTED_PREFIX,
1050 XATTR_TRUSTED_PREFIX_LEN) ||
1051 capable(CAP_SYS_ADMIN));
1054 ssize_t jfs_listxattr(struct dentry * dentry, char *data, size_t buf_size)
1056 struct inode *inode = dentry->d_inode;
1060 struct jfs_ea_list *ealist;
1062 struct ea_buffer ea_buf;
1064 down_read(&JFS_IP(inode)->xattr_sem);
1066 xattr_size = ea_get(inode, &ea_buf, 0);
1067 if (xattr_size < 0) {
1072 if (xattr_size == 0)
1075 ealist = (struct jfs_ea_list *) ea_buf.xattr;
1077 /* compute required size of list */
1078 for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea)) {
1080 size += name_size(ea) + 1;
1086 if (size > buf_size) {
1091 /* Copy attribute names to buffer */
1093 for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea)) {
1095 int namelen = copy_name(buffer, ea);
1096 buffer += namelen + 1;
1101 ea_release(inode, &ea_buf);
1103 up_read(&JFS_IP(inode)->xattr_sem);
1107 int jfs_removexattr(struct dentry *dentry, const char *name)
1109 struct inode *inode = dentry->d_inode;
1110 struct jfs_inode_info *ji = JFS_IP(inode);
1114 if ((rc = can_set_xattr(inode, name, NULL, 0)))
1117 tid = txBegin(inode->i_sb, 0);
1118 mutex_lock(&ji->commit_mutex);
1119 rc = __jfs_setxattr(tid, dentry->d_inode, name, NULL, 0, XATTR_REPLACE);
1121 rc = txCommit(tid, 1, &inode, 0);
1123 mutex_unlock(&ji->commit_mutex);
1128 #ifdef CONFIG_JFS_SECURITY
1129 int jfs_init_security(tid_t tid, struct inode *inode, struct inode *dir)
1137 rc = security_inode_init_security(inode, dir, &suffix, &value, &len);
1139 if (rc == -EOPNOTSUPP)
1143 name = kmalloc(XATTR_SECURITY_PREFIX_LEN + 1 + strlen(suffix),
1147 goto kmalloc_failed;
1149 strcpy(name, XATTR_SECURITY_PREFIX);
1150 strcpy(name + XATTR_SECURITY_PREFIX_LEN, suffix);
1152 rc = __jfs_setxattr(tid, inode, name, value, len, 0);