2 * Copyright (C) International Business Machines Corp., 2000-2004
3 * Copyright (C) Christoph Hellwig, 2002
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <linux/xattr.h>
22 #include <linux/posix_acl_xattr.h>
23 #include <linux/quotaops.h>
24 #include <linux/security.h>
25 #include "jfs_incore.h"
26 #include "jfs_superblock.h"
28 #include "jfs_debug.h"
29 #include "jfs_dinode.h"
30 #include "jfs_extent.h"
31 #include "jfs_metapage.h"
32 #include "jfs_xattr.h"
36 * jfs_xattr.c: extended attribute service
42 * Extended attribute lists (jfs_ea_list) consist of an overall size (32 bit
43 * value) and a variable (0 or more) number of extended attribute
44 * entries. Each extended attribute entry (jfs_ea) is a <name,value> double
45 * where <name> is constructed from a null-terminated ascii string
46 * (1 ... 255 bytes in the name) and <value> is arbitrary 8 bit data
47 * (1 ... 65535 bytes). The in-memory format is
49 * 0 1 2 4 4 + namelen + 1
50 * +-------+--------+--------+----------------+-------------------+
51 * | Flags | Name | Value | Name String \0 | Data . . . . |
52 * | | Length | Length | | |
53 * +-------+--------+--------+----------------+-------------------+
55 * A jfs_ea_list then is structured as
57 * 0 4 4 + EA_SIZE(ea1)
58 * +------------+-------------------+--------------------+-----
59 * | Overall EA | First FEA Element | Second FEA Element | .....
61 * +------------+-------------------+--------------------+-----
65 * FEALISTs are stored on disk using blocks allocated by dbAlloc() and
66 * written directly. An EA list may be in-lined in the inode if there is
67 * sufficient room available.
71 int flag; /* Indicates what storage xattr points to */
72 int max_size; /* largest xattr that fits in current buffer */
73 dxd_t new_ea; /* dxd to replace ea when modifying xattr */
74 struct metapage *mp; /* metapage containing ea list */
75 struct jfs_ea_list *xattr; /* buffer containing ea list */
79 * ea_buffer.flag values
81 #define EA_INLINE 0x0001
82 #define EA_EXTENT 0x0002
84 #define EA_MALLOC 0x0008
88 * These three routines are used to recognize on-disk extended attributes
89 * that are in a recognized namespace. If the attribute is not recognized,
90 * "os2." is prepended to the name
92 static inline int is_os2_xattr(struct jfs_ea *ea)
97 if ((ea->namelen >= XATTR_SYSTEM_PREFIX_LEN) &&
98 !strncmp(ea->name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
103 if ((ea->namelen >= XATTR_USER_PREFIX_LEN) &&
104 !strncmp(ea->name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
107 * Check for "security."
109 if ((ea->namelen >= XATTR_SECURITY_PREFIX_LEN) &&
110 !strncmp(ea->name, XATTR_SECURITY_PREFIX,
111 XATTR_SECURITY_PREFIX_LEN))
114 * Check for "trusted."
116 if ((ea->namelen >= XATTR_TRUSTED_PREFIX_LEN) &&
117 !strncmp(ea->name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN))
120 * Add any other valid namespace prefixes here
124 * We assume it's OS/2's flat namespace
129 static inline int name_size(struct jfs_ea *ea)
131 if (is_os2_xattr(ea))
132 return ea->namelen + XATTR_OS2_PREFIX_LEN;
137 static inline int copy_name(char *buffer, struct jfs_ea *ea)
139 int len = ea->namelen;
141 if (is_os2_xattr(ea)) {
142 memcpy(buffer, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN);
143 buffer += XATTR_OS2_PREFIX_LEN;
144 len += XATTR_OS2_PREFIX_LEN;
146 memcpy(buffer, ea->name, ea->namelen);
147 buffer[ea->namelen] = 0;
152 /* Forward references */
153 static void ea_release(struct inode *inode, struct ea_buffer *ea_buf);
156 * NAME: ea_write_inline
158 * FUNCTION: Attempt to write an EA inline if area is available
161 * Already verified that the specified EA is small enough to fit inline
165 * ealist - EA list pointer
166 * size - size of ealist in bytes
167 * ea - dxd_t structure to be filled in with necessary EA information
168 * if we successfully copy the EA inline
171 * Checks if the inode's inline area is available. If so, copies EA inline
172 * and sets <ea> fields appropriately. Otherwise, returns failure, EA will
173 * have to be put into an extent.
175 * RETURNS: 0 for successful copy to inline area; -1 if area not available
177 static int ea_write_inline(struct inode *ip, struct jfs_ea_list *ealist,
178 int size, dxd_t * ea)
180 struct jfs_inode_info *ji = JFS_IP(ip);
183 * Make sure we have an EA -- the NULL EA list is valid, but you
186 if (ealist && size > sizeof (struct jfs_ea_list)) {
187 assert(size <= sizeof (ji->i_inline_ea));
190 * See if the space is available or if it is already being
191 * used for an inline EA.
193 if (!(ji->mode2 & INLINEEA) && !(ji->ea.flag & DXD_INLINE))
199 memcpy(ji->i_inline_ea, ealist, size);
200 ea->flag = DXD_INLINE;
201 ji->mode2 &= ~INLINEEA;
208 /* Free up INLINE area */
209 if (ji->ea.flag & DXD_INLINE)
210 ji->mode2 |= INLINEEA;
219 * FUNCTION: Write an EA for an inode
221 * PRE CONDITIONS: EA has been verified
225 * ealist - EA list pointer
226 * size - size of ealist in bytes
227 * ea - dxd_t structure to be filled in appropriately with where the
230 * NOTES: Will write EA inline if able to, otherwise allocates blocks for an
231 * extent and synchronously writes it to those blocks.
233 * RETURNS: 0 for success; Anything else indicates failure
235 static int ea_write(struct inode *ip, struct jfs_ea_list *ealist, int size,
238 struct super_block *sb = ip->i_sb;
239 struct jfs_inode_info *ji = JFS_IP(ip);
240 struct jfs_sb_info *sbi = JFS_SBI(sb);
250 * Quick check to see if this is an in-linable EA. Short EAs
251 * and empty EAs are all in-linable, provided the space exists.
253 if (!ealist || size <= sizeof (ji->i_inline_ea)) {
254 if (!ea_write_inline(ip, ealist, size, ea))
258 /* figure out how many blocks we need */
259 nblocks = (size + (sb->s_blocksize - 1)) >> sb->s_blocksize_bits;
261 /* Allocate new blocks to quota. */
262 if (DQUOT_ALLOC_BLOCK(ip, nblocks)) {
266 rc = dbAlloc(ip, INOHINT(ip), nblocks, &blkno);
268 /*Rollback quota allocation. */
269 DQUOT_FREE_BLOCK(ip, nblocks);
274 * Now have nblocks worth of storage to stuff into the FEALIST.
275 * loop over the FEALIST copying data into the buffer one page at
278 cp = (char *) ealist;
280 for (i = 0; i < nblocks; i += sbi->nbperpage) {
282 * Determine how many bytes for this request, and round up to
283 * the nearest aggregate block size
285 nb = min(PSIZE, nbytes);
287 ((((nb + sb->s_blocksize - 1)) >> sb->s_blocksize_bits))
288 << sb->s_blocksize_bits;
290 if (!(mp = get_metapage(ip, blkno + i, bytes_to_write, 1))) {
295 memcpy(mp->data, cp, nb);
298 * We really need a way to propagate errors for
299 * forced writes like this one. --hch
301 * (__write_metapage => release_metapage => flush_metapage)
304 if ((rc = flush_metapage(mp))) {
306 * the write failed -- this means that the buffer
307 * is still assigned and the blocks are not being
308 * used. this seems like the best error recovery
321 ea->flag = DXD_EXTENT;
322 DXDsize(ea, le32_to_cpu(ealist->size));
323 DXDlength(ea, nblocks);
324 DXDaddress(ea, blkno);
326 /* Free up INLINE area */
327 if (ji->ea.flag & DXD_INLINE)
328 ji->mode2 |= INLINEEA;
333 /* Rollback quota allocation. */
334 DQUOT_FREE_BLOCK(ip, nblocks);
336 dbFree(ip, blkno, nblocks);
341 * NAME: ea_read_inline
343 * FUNCTION: Read an inlined EA into user's buffer
347 * ealist - Pointer to buffer to fill in with EA
351 static int ea_read_inline(struct inode *ip, struct jfs_ea_list *ealist)
353 struct jfs_inode_info *ji = JFS_IP(ip);
354 int ea_size = sizeDXD(&ji->ea);
362 if ((sizeDXD(&ji->ea) > sizeof (ji->i_inline_ea)))
364 if (le32_to_cpu(((struct jfs_ea_list *) &ji->i_inline_ea)->size)
368 memcpy(ealist, ji->i_inline_ea, ea_size);
375 * FUNCTION: copy EA data into user's buffer
379 * ealist - Pointer to buffer to fill in with EA
381 * NOTES: If EA is inline calls ea_read_inline() to copy EA.
383 * RETURNS: 0 for success; other indicates failure
385 static int ea_read(struct inode *ip, struct jfs_ea_list *ealist)
387 struct super_block *sb = ip->i_sb;
388 struct jfs_inode_info *ji = JFS_IP(ip);
389 struct jfs_sb_info *sbi = JFS_SBI(sb);
392 char *cp = (char *) ealist;
398 /* quick check for in-line EA */
399 if (ji->ea.flag & DXD_INLINE)
400 return ea_read_inline(ip, ealist);
402 nbytes = sizeDXD(&ji->ea);
404 jfs_error(sb, "ea_read: nbytes is 0");
409 * Figure out how many blocks were allocated when this EA list was
410 * originally written to disk.
412 nblocks = lengthDXD(&ji->ea) << sbi->l2nbperpage;
413 blkno = addressDXD(&ji->ea) << sbi->l2nbperpage;
416 * I have found the disk blocks which were originally used to store
417 * the FEALIST. now i loop over each contiguous block copying the
418 * data into the buffer.
420 for (i = 0; i < nblocks; i += sbi->nbperpage) {
422 * Determine how many bytes for this request, and round up to
423 * the nearest aggregate block size
425 nb = min(PSIZE, nbytes);
427 ((((nb + sb->s_blocksize - 1)) >> sb->s_blocksize_bits))
428 << sb->s_blocksize_bits;
430 if (!(mp = read_metapage(ip, blkno + i, bytes_to_read, 1)))
433 memcpy(cp, mp->data, nb);
434 release_metapage(mp);
446 * FUNCTION: Returns buffer containing existing extended attributes.
447 * The size of the buffer will be the larger of the existing
448 * attributes size, or min_size.
450 * The buffer, which may be inlined in the inode or in the
451 * page cache must be release by calling ea_release or ea_put
454 * inode - Inode pointer
455 * ea_buf - Structure to be populated with ealist and its metadata
456 * min_size- minimum size of buffer to be returned
458 * RETURNS: 0 for success; Other indicates failure
460 static int ea_get(struct inode *inode, struct ea_buffer *ea_buf, int min_size)
462 struct jfs_inode_info *ji = JFS_IP(inode);
463 struct super_block *sb = inode->i_sb;
465 int ea_size = sizeDXD(&ji->ea);
466 int blocks_needed, current_blocks;
469 int quota_allocation = 0;
471 /* When fsck.jfs clears a bad ea, it doesn't clear the size */
472 if (ji->ea.flag == 0)
478 ea_buf->max_size = 0;
479 ea_buf->xattr = NULL;
482 if ((min_size <= sizeof (ji->i_inline_ea)) &&
483 (ji->mode2 & INLINEEA)) {
484 ea_buf->flag = EA_INLINE | EA_NEW;
485 ea_buf->max_size = sizeof (ji->i_inline_ea);
486 ea_buf->xattr = (struct jfs_ea_list *) ji->i_inline_ea;
487 DXDlength(&ea_buf->new_ea, 0);
488 DXDaddress(&ea_buf->new_ea, 0);
489 ea_buf->new_ea.flag = DXD_INLINE;
490 DXDsize(&ea_buf->new_ea, min_size);
494 } else if (ji->ea.flag & DXD_INLINE) {
495 if (min_size <= sizeof (ji->i_inline_ea)) {
496 ea_buf->flag = EA_INLINE;
497 ea_buf->max_size = sizeof (ji->i_inline_ea);
498 ea_buf->xattr = (struct jfs_ea_list *) ji->i_inline_ea;
503 if (!(ji->ea.flag & DXD_EXTENT)) {
504 jfs_error(sb, "ea_get: invalid ea.flag)");
507 current_blocks = (ea_size + sb->s_blocksize - 1) >>
508 sb->s_blocksize_bits;
510 size = max(min_size, ea_size);
514 * To keep the rest of the code simple. Allocate a
515 * contiguous buffer to work with
517 ea_buf->xattr = kmalloc(size, GFP_KERNEL);
518 if (ea_buf->xattr == NULL)
521 ea_buf->flag = EA_MALLOC;
522 ea_buf->max_size = (size + sb->s_blocksize - 1) &
523 ~(sb->s_blocksize - 1);
528 if ((rc = ea_read(inode, ea_buf->xattr))) {
529 kfree(ea_buf->xattr);
530 ea_buf->xattr = NULL;
535 blocks_needed = (min_size + sb->s_blocksize - 1) >>
536 sb->s_blocksize_bits;
538 if (blocks_needed > current_blocks) {
539 /* Allocate new blocks to quota. */
540 if (DQUOT_ALLOC_BLOCK(inode, blocks_needed))
543 quota_allocation = blocks_needed;
545 rc = dbAlloc(inode, INOHINT(inode), (s64) blocks_needed,
550 DXDlength(&ea_buf->new_ea, blocks_needed);
551 DXDaddress(&ea_buf->new_ea, blkno);
552 ea_buf->new_ea.flag = DXD_EXTENT;
553 DXDsize(&ea_buf->new_ea, min_size);
555 ea_buf->flag = EA_EXTENT | EA_NEW;
557 ea_buf->mp = get_metapage(inode, blkno,
558 blocks_needed << sb->s_blocksize_bits,
560 if (ea_buf->mp == NULL) {
561 dbFree(inode, blkno, (s64) blocks_needed);
565 ea_buf->xattr = ea_buf->mp->data;
566 ea_buf->max_size = (min_size + sb->s_blocksize - 1) &
567 ~(sb->s_blocksize - 1);
570 if ((rc = ea_read(inode, ea_buf->xattr))) {
571 discard_metapage(ea_buf->mp);
572 dbFree(inode, blkno, (s64) blocks_needed);
577 ea_buf->flag = EA_EXTENT;
578 ea_buf->mp = read_metapage(inode, addressDXD(&ji->ea),
579 lengthDXD(&ji->ea) << sb->s_blocksize_bits,
581 if (ea_buf->mp == NULL) {
585 ea_buf->xattr = ea_buf->mp->data;
586 ea_buf->max_size = (ea_size + sb->s_blocksize - 1) &
587 ~(sb->s_blocksize - 1);
590 if (EALIST_SIZE(ea_buf->xattr) != ea_size) {
591 printk(KERN_ERR "ea_get: invalid extended attribute\n");
592 dump_mem("xattr", ea_buf->xattr, ea_size);
593 ea_release(inode, ea_buf);
601 /* Rollback quota allocation */
602 if (quota_allocation)
603 DQUOT_FREE_BLOCK(inode, quota_allocation);
608 static void ea_release(struct inode *inode, struct ea_buffer *ea_buf)
610 if (ea_buf->flag & EA_MALLOC)
611 kfree(ea_buf->xattr);
612 else if (ea_buf->flag & EA_EXTENT) {
614 release_metapage(ea_buf->mp);
616 if (ea_buf->flag & EA_NEW)
617 dbFree(inode, addressDXD(&ea_buf->new_ea),
618 lengthDXD(&ea_buf->new_ea));
622 static int ea_put(tid_t tid, struct inode *inode, struct ea_buffer *ea_buf,
625 struct jfs_inode_info *ji = JFS_IP(inode);
626 unsigned long old_blocks, new_blocks;
630 ea_release(inode, ea_buf);
632 } else if (ea_buf->flag & EA_INLINE) {
633 assert(new_size <= sizeof (ji->i_inline_ea));
634 ji->mode2 &= ~INLINEEA;
635 ea_buf->new_ea.flag = DXD_INLINE;
636 DXDsize(&ea_buf->new_ea, new_size);
637 DXDaddress(&ea_buf->new_ea, 0);
638 DXDlength(&ea_buf->new_ea, 0);
639 } else if (ea_buf->flag & EA_MALLOC) {
640 rc = ea_write(inode, ea_buf->xattr, new_size, &ea_buf->new_ea);
641 kfree(ea_buf->xattr);
642 } else if (ea_buf->flag & EA_NEW) {
643 /* We have already allocated a new dxd */
644 flush_metapage(ea_buf->mp);
646 /* ->xattr must point to original ea's metapage */
647 rc = ea_write(inode, ea_buf->xattr, new_size, &ea_buf->new_ea);
648 discard_metapage(ea_buf->mp);
653 old_blocks = new_blocks = 0;
655 if (ji->ea.flag & DXD_EXTENT) {
656 invalidate_dxd_metapages(inode, ji->ea);
657 old_blocks = lengthDXD(&ji->ea);
661 txEA(tid, inode, &ji->ea, &ea_buf->new_ea);
662 if (ea_buf->new_ea.flag & DXD_EXTENT) {
663 new_blocks = lengthDXD(&ea_buf->new_ea);
664 if (ji->ea.flag & DXD_INLINE)
665 ji->mode2 |= INLINEEA;
667 ji->ea = ea_buf->new_ea;
669 txEA(tid, inode, &ji->ea, NULL);
670 if (ji->ea.flag & DXD_INLINE)
671 ji->mode2 |= INLINEEA;
676 /* If old blocks exist, they must be removed from quota allocation. */
678 DQUOT_FREE_BLOCK(inode, old_blocks);
680 inode->i_ctime = CURRENT_TIME;
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 * POSIX_ACL_XATTR_ACCESS is tied to i_mode
704 if (strcmp(name, POSIX_ACL_XATTR_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, POSIX_ACL_XATTR_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 (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
762 return can_set_system_xattr(inode, name, value, value_len);
765 * Don't allow setting an attribute in an unknown namespace.
767 if (strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) &&
768 strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) &&
769 strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN) &&
770 strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN))
773 if (!S_ISREG(inode->i_mode) &&
774 (!S_ISDIR(inode->i_mode) || inode->i_mode &S_ISVTX))
780 int __jfs_setxattr(tid_t tid, struct inode *inode, const char *name,
781 const void *value, size_t value_len, int flags)
783 struct jfs_ea_list *ealist;
784 struct jfs_ea *ea, *old_ea = NULL, *next_ea = NULL;
785 struct ea_buffer ea_buf;
789 int namelen = strlen(name);
790 char *os2name = NULL;
795 if (strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN) == 0) {
796 os2name = kmalloc(namelen - XATTR_OS2_PREFIX_LEN + 1,
800 strcpy(os2name, name + XATTR_OS2_PREFIX_LEN);
802 namelen -= XATTR_OS2_PREFIX_LEN;
805 down_write(&JFS_IP(inode)->xattr_sem);
807 xattr_size = ea_get(inode, &ea_buf, 0);
808 if (xattr_size < 0) {
814 ealist = (struct jfs_ea_list *) ea_buf.xattr;
815 new_size = sizeof (struct jfs_ea_list);
818 for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist);
820 if ((namelen == ea->namelen) &&
821 (memcmp(name, ea->name, namelen) == 0)) {
823 if (flags & XATTR_CREATE) {
828 old_ea_size = EA_SIZE(ea);
829 next_ea = NEXT_EA(ea);
831 new_size += EA_SIZE(ea);
836 if (flags & XATTR_REPLACE) {
846 new_size += sizeof (struct jfs_ea) + namelen + 1 + value_len;
848 if (new_size > ea_buf.max_size) {
850 * We need to allocate more space for merged ea list.
851 * We should only have loop to again: once.
853 ea_release(inode, &ea_buf);
854 xattr_size = ea_get(inode, &ea_buf, new_size);
855 if (xattr_size < 0) {
862 /* Remove old ea of the same name */
864 /* number of bytes following target EA */
865 length = (char *) END_EALIST(ealist) - (char *) next_ea;
867 memmove(old_ea, next_ea, length);
868 xattr_size -= old_ea_size;
871 /* Add new entry to the end */
874 /* Completely new ea list */
875 xattr_size = sizeof (struct jfs_ea_list);
877 ea = (struct jfs_ea *) ((char *) ealist + xattr_size);
879 ea->namelen = namelen;
880 ea->valuelen = (cpu_to_le16(value_len));
881 memcpy(ea->name, name, namelen);
882 ea->name[namelen] = 0;
884 memcpy(&ea->name[namelen + 1], value, value_len);
885 xattr_size += EA_SIZE(ea);
888 /* DEBUG - If we did this right, these number match */
889 if (xattr_size != new_size) {
891 "jfs_xsetattr: xattr_size = %d, new_size = %d\n",
892 xattr_size, new_size);
899 * If we're left with an empty list, there's no ea
901 if (new_size == sizeof (struct jfs_ea_list))
904 ealist->size = cpu_to_le32(new_size);
906 rc = ea_put(tid, inode, &ea_buf, new_size);
910 ea_release(inode, &ea_buf);
912 up_write(&JFS_IP(inode)->xattr_sem);
919 int jfs_setxattr(struct dentry *dentry, const char *name, const void *value,
920 size_t value_len, int flags)
922 struct inode *inode = dentry->d_inode;
923 struct jfs_inode_info *ji = JFS_IP(inode);
927 if ((rc = can_set_xattr(inode, name, value, value_len)))
930 if (value == NULL) { /* empty EA, do not remove */
935 tid = txBegin(inode->i_sb, 0);
936 down(&ji->commit_sem);
937 rc = __jfs_setxattr(tid, dentry->d_inode, name, value, value_len,
940 rc = txCommit(tid, 1, &inode, 0);
947 ssize_t __jfs_getxattr(struct inode *inode, const char *name, void *data,
950 struct jfs_ea_list *ealist;
952 struct ea_buffer ea_buf;
955 int namelen = strlen(name);
956 char *os2name = NULL;
959 if (strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN) == 0) {
960 os2name = kmalloc(namelen - XATTR_OS2_PREFIX_LEN + 1,
964 strcpy(os2name, name + XATTR_OS2_PREFIX_LEN);
966 namelen -= XATTR_OS2_PREFIX_LEN;
969 down_read(&JFS_IP(inode)->xattr_sem);
971 xattr_size = ea_get(inode, &ea_buf, 0);
973 if (xattr_size < 0) {
981 ealist = (struct jfs_ea_list *) ea_buf.xattr;
983 /* Find the named attribute */
984 for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea))
985 if ((namelen == ea->namelen) &&
986 memcmp(name, ea->name, namelen) == 0) {
988 size = le16_to_cpu(ea->valuelen);
991 else if (size > buf_size) {
995 value = ((char *) &ea->name) + ea->namelen + 1;
996 memcpy(data, value, size);
1002 ea_release(inode, &ea_buf);
1004 up_read(&JFS_IP(inode)->xattr_sem);
1011 ssize_t jfs_getxattr(struct dentry *dentry, const char *name, void *data,
1016 err = __jfs_getxattr(dentry->d_inode, name, data, buf_size);
1022 * No special permissions are needed to list attributes except for trusted.*
1024 static inline int can_list(struct jfs_ea *ea)
1026 return (strncmp(ea->name, XATTR_TRUSTED_PREFIX,
1027 XATTR_TRUSTED_PREFIX_LEN) ||
1028 capable(CAP_SYS_ADMIN));
1031 ssize_t jfs_listxattr(struct dentry * dentry, char *data, size_t buf_size)
1033 struct inode *inode = dentry->d_inode;
1037 struct jfs_ea_list *ealist;
1039 struct ea_buffer ea_buf;
1041 down_read(&JFS_IP(inode)->xattr_sem);
1043 xattr_size = ea_get(inode, &ea_buf, 0);
1044 if (xattr_size < 0) {
1049 if (xattr_size == 0)
1052 ealist = (struct jfs_ea_list *) ea_buf.xattr;
1054 /* compute required size of list */
1055 for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea)) {
1057 size += name_size(ea) + 1;
1063 if (size > buf_size) {
1068 /* Copy attribute names to buffer */
1070 for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea)) {
1072 int namelen = copy_name(buffer, ea);
1073 buffer += namelen + 1;
1078 ea_release(inode, &ea_buf);
1080 up_read(&JFS_IP(inode)->xattr_sem);
1084 int jfs_removexattr(struct dentry *dentry, const char *name)
1086 struct inode *inode = dentry->d_inode;
1087 struct jfs_inode_info *ji = JFS_IP(inode);
1091 if ((rc = can_set_xattr(inode, name, NULL, 0)))
1094 tid = txBegin(inode->i_sb, 0);
1095 down(&ji->commit_sem);
1096 rc = __jfs_setxattr(tid, dentry->d_inode, name, NULL, 0, XATTR_REPLACE);
1098 rc = txCommit(tid, 1, &inode, 0);
1100 up(&ji->commit_sem);
1105 #ifdef CONFIG_JFS_SECURITY
1106 int jfs_init_security(tid_t tid, struct inode *inode, struct inode *dir)
1114 rc = security_inode_init_security(inode, dir, &suffix, &value, &len);
1116 if (rc == -EOPNOTSUPP)
1120 name = kmalloc(XATTR_SECURITY_PREFIX_LEN + 1 + strlen(suffix),
1124 goto kmalloc_failed;
1126 strcpy(name, XATTR_SECURITY_PREFIX);
1127 strcpy(name + XATTR_SECURITY_PREFIX_LEN, suffix);
1129 rc = __jfs_setxattr(tid, inode, name, value, len, 0);