[PATCH] x86-64 update
[linux-2.6/history.git] / fs / jfs / xattr.c
blobddcb7f7215cd854f797921eb17733463dff799f6
1 /*
2 * Copyright (c) International Business Machines Corp., 2000-2002
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.
9 *
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/fs.h>
21 #include <linux/xattr.h>
22 #include "jfs_incore.h"
23 #include "jfs_dmap.h"
24 #include "jfs_debug.h"
25 #include "jfs_dinode.h"
26 #include "jfs_extent.h"
27 #include "jfs_metapage.h"
28 #include "jfs_xattr.h"
29 #include "jfs_acl.h"
32 * jfs_xattr.c: extended attribute service
34 * Overall design --
36 * Format:
38 * Extended attribute lists (jfs_ea_list) consist of an overall size (32 bit
39 * value) and a variable (0 or more) number of extended attribute
40 * entries. Each extended attribute entry (jfs_ea) is a <name,value> double
41 * where <name> is constructed from a null-terminated ascii string
42 * (1 ... 255 bytes in the name) and <value> is arbitrary 8 bit data
43 * (1 ... 65535 bytes). The in-memory format is
45 * 0 1 2 4 4 + namelen + 1
46 * +-------+--------+--------+----------------+-------------------+
47 * | Flags | Name | Value | Name String \0 | Data . . . . |
48 * | | Length | Length | | |
49 * +-------+--------+--------+----------------+-------------------+
51 * A jfs_ea_list then is structured as
53 * 0 4 4 + EA_SIZE(ea1)
54 * +------------+-------------------+--------------------+-----
55 * | Overall EA | First FEA Element | Second FEA Element | .....
56 * | List Size | | |
57 * +------------+-------------------+--------------------+-----
59 * On-disk:
61 * FEALISTs are stored on disk using blocks allocated by dbAlloc() and
62 * written directly. An EA list may be in-lined in the inode if there is
63 * sufficient room available.
66 struct ea_buffer {
67 int flag; /* Indicates what storage xattr points to */
68 int max_size; /* largest xattr that fits in current buffer */
69 dxd_t new_ea; /* dxd to replace ea when modifying xattr */
70 struct metapage *mp; /* metapage containing ea list */
71 struct jfs_ea_list *xattr; /* buffer containing ea list */
75 * ea_buffer.flag values
77 #define EA_INLINE 0x0001
78 #define EA_EXTENT 0x0002
79 #define EA_NEW 0x0004
80 #define EA_MALLOC 0x0008
82 /* Namespaces */
83 #define XATTR_SYSTEM_PREFIX "system."
84 #define XATTR_SYSTEM_PREFIX_LEN (sizeof (XATTR_SYSTEM_PREFIX) - 1)
86 #define XATTR_USER_PREFIX "user."
87 #define XATTR_USER_PREFIX_LEN (sizeof (XATTR_USER_PREFIX) - 1)
89 #define XATTR_OS2_PREFIX "os2."
90 #define XATTR_OS2_PREFIX_LEN (sizeof (XATTR_OS2_PREFIX) - 1)
93 * These three routines are used to recognize on-disk extended attributes
94 * that are in a recognized namespace. If the attribute is not recognized,
95 * "os2." is prepended to the name
97 static inline int is_os2_xattr(struct jfs_ea *ea)
100 * Check for "system."
102 if ((ea->namelen >= XATTR_SYSTEM_PREFIX_LEN) &&
103 !strncmp(ea->name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
104 return FALSE;
106 * Check for "user."
108 if ((ea->namelen >= XATTR_USER_PREFIX_LEN) &&
109 !strncmp(ea->name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
110 return FALSE;
112 * Add any other valid namespace prefixes here
116 * We assume it's OS/2's flat namespace
118 return TRUE;
121 static inline int name_size(struct jfs_ea *ea)
123 if (is_os2_xattr(ea))
124 return ea->namelen + XATTR_OS2_PREFIX_LEN;
125 else
126 return ea->namelen;
129 static inline int copy_name(char *buffer, struct jfs_ea *ea)
131 int len = ea->namelen;
133 if (is_os2_xattr(ea)) {
134 memcpy(buffer, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN);
135 buffer += XATTR_OS2_PREFIX_LEN;
136 len += XATTR_OS2_PREFIX_LEN;
138 memcpy(buffer, ea->name, ea->namelen);
139 buffer[ea->namelen] = 0;
141 return len;
144 /* Forward references */
145 static void ea_release(struct inode *inode, struct ea_buffer *ea_buf);
148 * NAME: ea_write_inline
150 * FUNCTION: Attempt to write an EA inline if area is available
152 * PRE CONDITIONS:
153 * Already verified that the specified EA is small enough to fit inline
155 * PARAMETERS:
156 * ip - Inode pointer
157 * ealist - EA list pointer
158 * size - size of ealist in bytes
159 * ea - dxd_t structure to be filled in with necessary EA information
160 * if we successfully copy the EA inline
162 * NOTES:
163 * Checks if the inode's inline area is available. If so, copies EA inline
164 * and sets <ea> fields appropriately. Otherwise, returns failure, EA will
165 * have to be put into an extent.
167 * RETURNS: 0 for successful copy to inline area; -1 if area not available
169 static int ea_write_inline(struct inode *ip, struct jfs_ea_list *ealist,
170 int size, dxd_t * ea)
172 struct jfs_inode_info *ji = JFS_IP(ip);
175 * Make sure we have an EA -- the NULL EA list is valid, but you
176 * can't copy it!
178 if (ealist && size > sizeof (struct jfs_ea_list)) {
179 assert(size <= sizeof (ji->i_inline_ea));
182 * See if the space is available or if it is already being
183 * used for an inline EA.
185 if (!(ji->mode2 & INLINEEA) && !(ji->ea.flag & DXD_INLINE))
186 return -EPERM;
188 DXDsize(ea, size);
189 DXDlength(ea, 0);
190 DXDaddress(ea, 0);
191 memcpy(ji->i_inline_ea, ealist, size);
192 ea->flag = DXD_INLINE;
193 ji->mode2 &= ~INLINEEA;
194 } else {
195 ea->flag = 0;
196 DXDsize(ea, 0);
197 DXDlength(ea, 0);
198 DXDaddress(ea, 0);
200 /* Free up INLINE area */
201 if (ji->ea.flag & DXD_INLINE)
202 ji->mode2 |= INLINEEA;
205 return 0;
209 * NAME: ea_write
211 * FUNCTION: Write an EA for an inode
213 * PRE CONDITIONS: EA has been verified
215 * PARAMETERS:
216 * ip - Inode pointer
217 * ealist - EA list pointer
218 * size - size of ealist in bytes
219 * ea - dxd_t structure to be filled in appropriately with where the
220 * EA was copied
222 * NOTES: Will write EA inline if able to, otherwise allocates blocks for an
223 * extent and synchronously writes it to those blocks.
225 * RETURNS: 0 for success; Anything else indicates failure
227 static int ea_write(struct inode *ip, struct jfs_ea_list *ealist, int size,
228 dxd_t * ea)
230 struct super_block *sb = ip->i_sb;
231 struct jfs_inode_info *ji = JFS_IP(ip);
232 struct jfs_sb_info *sbi = JFS_SBI(sb);
233 int nblocks;
234 s64 blkno;
235 int rc = 0, i;
236 char *cp;
237 s32 nbytes, nb;
238 s32 bytes_to_write;
239 struct metapage *mp;
242 * Quick check to see if this is an in-linable EA. Short EAs
243 * and empty EAs are all in-linable, provided the space exists.
245 if (!ealist || size <= sizeof (ji->i_inline_ea)) {
246 if (!ea_write_inline(ip, ealist, size, ea))
247 return 0;
250 /* figure out how many blocks we need */
251 nblocks = (size + (sb->s_blocksize - 1)) >> sb->s_blocksize_bits;
253 rc = dbAlloc(ip, INOHINT(ip), nblocks, &blkno);
254 if (rc)
255 return rc;
258 * Now have nblocks worth of storage to stuff into the FEALIST.
259 * loop over the FEALIST copying data into the buffer one page at
260 * a time.
262 cp = (char *) ealist;
263 nbytes = size;
264 for (i = 0; i < nblocks; i += sbi->nbperpage) {
266 * Determine how many bytes for this request, and round up to
267 * the nearest aggregate block size
269 nb = min(PSIZE, nbytes);
270 bytes_to_write =
271 ((((nb + sb->s_blocksize - 1)) >> sb->s_blocksize_bits))
272 << sb->s_blocksize_bits;
274 if (!(mp = get_metapage(ip, blkno + i, bytes_to_write, 1))) {
275 rc = -EIO;
276 goto failed;
279 memcpy(mp->data, cp, nb);
282 * We really need a way to propagate errors for
283 * forced writes like this one. --hch
285 * (__write_metapage => release_metapage => flush_metapage)
287 #ifdef _JFS_FIXME
288 if ((rc = flush_metapage(mp))) {
290 * the write failed -- this means that the buffer
291 * is still assigned and the blocks are not being
292 * used. this seems like the best error recovery
293 * we can get ...
295 goto failed;
297 #else
298 flush_metapage(mp);
299 #endif
301 cp += PSIZE;
302 nbytes -= nb;
305 ea->flag = DXD_EXTENT;
306 DXDsize(ea, le32_to_cpu(ealist->size));
307 DXDlength(ea, nblocks);
308 DXDaddress(ea, blkno);
310 /* Free up INLINE area */
311 if (ji->ea.flag & DXD_INLINE)
312 ji->mode2 |= INLINEEA;
314 return 0;
316 failed:
317 dbFree(ip, blkno, nblocks);
318 return rc;
322 * NAME: ea_read_inline
324 * FUNCTION: Read an inlined EA into user's buffer
326 * PARAMETERS:
327 * ip - Inode pointer
328 * ealist - Pointer to buffer to fill in with EA
330 * RETURNS: 0
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);
337 if (ea_size == 0) {
338 ealist->size = 0;
339 return 0;
342 /* Sanity Check */
343 if ((sizeDXD(&ji->ea) > sizeof (ji->i_inline_ea)))
344 return -EIO;
345 if (le32_to_cpu(((struct jfs_ea_list *) &ji->i_inline_ea)->size)
346 != ea_size)
347 return -EIO;
349 memcpy(ealist, ji->i_inline_ea, ea_size);
350 return 0;
354 * NAME: ea_read
356 * FUNCTION: copy EA data into user's buffer
358 * PARAMETERS:
359 * ip - Inode pointer
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);
371 int nblocks;
372 s64 blkno;
373 char *cp = (char *) ealist;
374 int i;
375 int nbytes, nb;
376 s32 bytes_to_read;
377 struct metapage *mp;
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);
384 assert(nbytes);
387 * Figure out how many blocks were allocated when this EA list was
388 * originally written to disk.
390 nblocks = lengthDXD(&ji->ea) << sbi->l2nbperpage;
391 blkno = addressDXD(&ji->ea) << sbi->l2nbperpage;
394 * I have found the disk blocks which were originally used to store
395 * the FEALIST. now i loop over each contiguous block copying the
396 * data into the buffer.
398 for (i = 0; i < nblocks; i += sbi->nbperpage) {
400 * Determine how many bytes for this request, and round up to
401 * the nearest aggregate block size
403 nb = min(PSIZE, nbytes);
404 bytes_to_read =
405 ((((nb + sb->s_blocksize - 1)) >> sb->s_blocksize_bits))
406 << sb->s_blocksize_bits;
408 if (!(mp = read_metapage(ip, blkno + i, bytes_to_read, 1)))
409 return -EIO;
411 memcpy(cp, mp->data, nb);
412 release_metapage(mp);
414 cp += PSIZE;
415 nbytes -= nb;
418 return 0;
422 * NAME: ea_get
424 * FUNCTION: Returns buffer containing existing extended attributes.
425 * The size of the buffer will be the larger of the existing
426 * attributes size, or min_size.
428 * The buffer, which may be inlined in the inode or in the
429 * page cache must be release by calling ea_release or ea_put
431 * PARAMETERS:
432 * inode - Inode pointer
433 * ea_buf - Structure to be populated with ealist and its metadata
434 * min_size- minimum size of buffer to be returned
436 * RETURNS: 0 for success; Other indicates failure
438 static int ea_get(struct inode *inode, struct ea_buffer *ea_buf, int min_size)
440 struct jfs_inode_info *ji = JFS_IP(inode);
441 struct super_block *sb = inode->i_sb;
442 int size;
443 int ea_size = sizeDXD(&ji->ea);
444 int blocks_needed, current_blocks;
445 s64 blkno;
446 int rc;
448 /* When fsck.jfs clears a bad ea, it doesn't clear the size */
449 if (ji->ea.flag == 0)
450 ea_size = 0;
452 if (ea_size == 0) {
453 if (min_size == 0) {
454 ea_buf->flag = 0;
455 ea_buf->max_size = 0;
456 ea_buf->xattr = NULL;
457 return 0;
459 if ((min_size <= sizeof (ji->i_inline_ea)) &&
460 (ji->mode2 & INLINEEA)) {
461 ea_buf->flag = EA_INLINE | EA_NEW;
462 ea_buf->max_size = sizeof (ji->i_inline_ea);
463 ea_buf->xattr = (struct jfs_ea_list *) ji->i_inline_ea;
464 DXDlength(&ea_buf->new_ea, 0);
465 DXDaddress(&ea_buf->new_ea, 0);
466 ea_buf->new_ea.flag = DXD_INLINE;
467 DXDsize(&ea_buf->new_ea, min_size);
468 return 0;
470 current_blocks = 0;
471 } else if (ji->ea.flag & DXD_INLINE) {
472 if (min_size <= sizeof (ji->i_inline_ea)) {
473 ea_buf->flag = EA_INLINE;
474 ea_buf->max_size = sizeof (ji->i_inline_ea);
475 ea_buf->xattr = (struct jfs_ea_list *) ji->i_inline_ea;
476 goto size_check;
478 current_blocks = 0;
479 } else {
480 assert(ji->ea.flag & DXD_EXTENT);
481 current_blocks = (ea_size + sb->s_blocksize - 1) >>
482 sb->s_blocksize_bits;
484 size = max(min_size, ea_size);
486 if (size > PSIZE) {
488 * To keep the rest of the code simple. Allocate a
489 * contiguous buffer to work with
491 ea_buf->xattr = kmalloc(size, GFP_KERNEL);
492 if (ea_buf->xattr == NULL)
493 return -ENOMEM;
495 ea_buf->flag |= EA_MALLOC;
496 ea_buf->max_size = (size + sb->s_blocksize - 1) &
497 ~(sb->s_blocksize - 1);
499 if (ea_size == 0)
500 return 0;
502 if ((rc = ea_read(inode, ea_buf->xattr))) {
503 kfree(ea_buf->xattr);
504 ea_buf->xattr = NULL;
505 return rc;
507 goto size_check;
509 blocks_needed = (min_size + sb->s_blocksize - 1) >>
510 sb->s_blocksize_bits;
512 if (blocks_needed > current_blocks) {
513 rc = dbAlloc(inode, INOHINT(inode), (s64) blocks_needed,
514 &blkno);
515 if (rc)
516 return rc;
518 DXDlength(&ea_buf->new_ea, blocks_needed);
519 DXDaddress(&ea_buf->new_ea, blkno);
520 ea_buf->new_ea.flag = DXD_EXTENT;
521 DXDsize(&ea_buf->new_ea, min_size);
523 ea_buf->flag = EA_EXTENT | EA_NEW;
525 ea_buf->mp = get_metapage(inode, blkno,
526 blocks_needed << sb->s_blocksize_bits,
528 if (ea_buf->mp == NULL) {
529 dbFree(inode, blkno, (s64) blocks_needed);
530 return -EIO;
532 ea_buf->xattr = ea_buf->mp->data;
533 ea_buf->max_size = (min_size + sb->s_blocksize - 1) &
534 ~(sb->s_blocksize - 1);
535 if (ea_size == 0)
536 return 0;
537 if ((rc = ea_read(inode, ea_buf->xattr))) {
538 discard_metapage(ea_buf->mp);
539 dbFree(inode, blkno, (s64) blocks_needed);
540 return rc;
542 goto size_check;
544 ea_buf->flag = EA_EXTENT;
545 ea_buf->mp = read_metapage(inode, addressDXD(&ji->ea),
546 lengthDXD(&ji->ea), 1);
547 if (ea_buf->mp == NULL)
548 return -EIO;
549 ea_buf->xattr = ea_buf->mp->data;
550 ea_buf->max_size = (ea_size + sb->s_blocksize - 1) &
551 ~(sb->s_blocksize - 1);
553 size_check:
554 if (EALIST_SIZE(ea_buf->xattr) != ea_size) {
555 printk(KERN_ERR "ea_get: invalid extended attribute\n");
556 dump_mem("xattr", ea_buf->xattr, ea_size);
557 ea_release(inode, ea_buf);
558 return -EIO;
561 return ea_size;
564 static void ea_release(struct inode *inode, struct ea_buffer *ea_buf)
566 if (ea_buf->flag & EA_MALLOC)
567 kfree(ea_buf->xattr);
568 else if (ea_buf->flag & EA_EXTENT) {
569 assert(ea_buf->mp);
570 release_metapage(ea_buf->mp);
572 if (ea_buf->flag & EA_NEW)
573 dbFree(inode, addressDXD(&ea_buf->new_ea),
574 lengthDXD(&ea_buf->new_ea));
578 static int ea_put(struct inode *inode, struct ea_buffer *ea_buf, int new_size)
580 struct jfs_inode_info *ji = JFS_IP(inode);
581 unsigned long old_blocks, new_blocks;
582 int rc = 0;
583 tid_t tid;
585 if (new_size == 0) {
586 ea_release(inode, ea_buf);
587 ea_buf = 0;
588 } else if (ea_buf->flag & EA_INLINE) {
589 assert(new_size <= sizeof (ji->i_inline_ea));
590 ji->mode2 &= ~INLINEEA;
591 ea_buf->new_ea.flag = DXD_INLINE;
592 DXDsize(&ea_buf->new_ea, new_size);
593 DXDaddress(&ea_buf->new_ea, 0);
594 DXDlength(&ea_buf->new_ea, 0);
595 } else if (ea_buf->flag & EA_MALLOC) {
596 rc = ea_write(inode, ea_buf->xattr, new_size, &ea_buf->new_ea);
597 kfree(ea_buf->xattr);
598 } else if (ea_buf->flag & EA_NEW) {
599 /* We have already allocated a new dxd */
600 flush_metapage(ea_buf->mp);
601 } else {
602 /* ->xattr must point to original ea's metapage */
603 rc = ea_write(inode, ea_buf->xattr, new_size, &ea_buf->new_ea);
604 discard_metapage(ea_buf->mp);
606 if (rc)
607 return rc;
609 tid = txBegin(inode->i_sb, 0);
610 down(&ji->commit_sem);
612 old_blocks = new_blocks = 0;
614 if (ji->ea.flag & DXD_EXTENT) {
615 invalidate_dxd_metapages(inode, ji->ea);
616 old_blocks = lengthDXD(&ji->ea);
619 if (ea_buf) {
620 txEA(tid, inode, &ji->ea, &ea_buf->new_ea);
621 if (ea_buf->new_ea.flag & DXD_EXTENT) {
622 new_blocks = lengthDXD(&ea_buf->new_ea);
623 if (ji->ea.flag & DXD_INLINE)
624 ji->mode2 |= INLINEEA;
626 ji->ea = ea_buf->new_ea;
627 } else {
628 txEA(tid, inode, &ji->ea, 0);
629 if (ji->ea.flag & DXD_INLINE)
630 ji->mode2 |= INLINEEA;
631 ji->ea.flag = 0;
632 ji->ea.size = 0;
635 inode->i_blocks += LBLK2PBLK(inode->i_sb, new_blocks - old_blocks);
636 rc = txCommit(tid, 1, &inode, 0);
637 txEnd(tid);
638 up(&ji->commit_sem);
640 return rc;
644 * can_set_system_xattr
646 * This code is specific to the system.* namespace. It contains policy
647 * which doesn't belong in the main xattr codepath.
649 static int can_set_system_xattr(struct inode *inode, const char *name,
650 const void *value, size_t value_len)
652 #ifdef CONFIG_JFS_POSIX_ACL
653 struct posix_acl *acl;
654 int rc;
656 if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
657 return -EPERM;
660 * XATTR_NAME_ACL_ACCESS is tied to i_mode
662 if (strcmp(name, XATTR_NAME_ACL_ACCESS) == 0) {
663 acl = posix_acl_from_xattr(value, value_len);
664 if (acl < 0) {
665 printk(KERN_ERR "posix_acl_from_xattr returned %d\n",
666 rc);
667 return rc;
669 if (acl > 0) {
670 mode_t mode = inode->i_mode;
671 rc = posix_acl_equiv_mode(acl, &mode);
672 posix_acl_release(acl);
673 if (rc < 0) {
674 printk(KERN_ERR
675 "posix_acl_equiv_mode returned %d\n",
676 rc);
677 return rc;
679 inode->i_mode = mode;
680 mark_inode_dirty(inode);
681 if (rc == 0)
682 value = NULL;
685 * We're changing the ACL. Get rid of the cached one
687 acl =JFS_IP(inode)->i_acl;
688 if (acl && (acl != JFS_ACL_NOT_CACHED))
689 posix_acl_release(acl);
690 JFS_IP(inode)->i_acl = JFS_ACL_NOT_CACHED;
691 } else if (strcmp(name, XATTR_NAME_ACL_DEFAULT) == 0) {
693 * We're changing the default ACL. Get rid of the cached one
695 acl =JFS_IP(inode)->i_default_acl;
696 if (acl && (acl != JFS_ACL_NOT_CACHED))
697 posix_acl_release(acl);
698 JFS_IP(inode)->i_default_acl = JFS_ACL_NOT_CACHED;
699 } else
700 /* Invalid xattr name */
701 return -EINVAL;
702 return 0;
703 #else /* CONFIG_JFS_POSIX_ACL */
704 return -EOPNOTSUPP;
705 #endif /* CONFIG_JFS_POSIX_ACL */
708 static int can_set_xattr(struct inode *inode, const char *name,
709 const void *value, size_t value_len)
711 if (IS_RDONLY(inode))
712 return -EROFS;
714 if (IS_IMMUTABLE(inode) || IS_APPEND(inode) || S_ISLNK(inode->i_mode))
715 return -EPERM;
717 if(strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN) == 0)
719 * "system.*"
721 return can_set_system_xattr(inode, name, value, value_len);
723 if((strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN) != 0) &&
724 (strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN) != 0))
725 return -EOPNOTSUPP;
727 if (!S_ISREG(inode->i_mode) &&
728 (!S_ISDIR(inode->i_mode) || inode->i_mode &S_ISVTX))
729 return -EPERM;
731 #ifdef CONFIG_JFS_POSIX_ACL
732 return jfs_permission(inode, MAY_WRITE, NULL);
733 #else
734 return permission(inode, MAY_WRITE, NULL);
735 #endif
738 int __jfs_setxattr(struct inode *inode, const char *name, const void *value,
739 size_t value_len, int flags)
741 struct jfs_ea_list *ealist;
742 struct jfs_ea *ea, *old_ea = NULL, *next_ea = NULL;
743 struct ea_buffer ea_buf;
744 int old_ea_size = 0;
745 int xattr_size;
746 int new_size;
747 int namelen = strlen(name);
748 char *os2name = NULL;
749 int found = 0;
750 int rc;
751 int length;
753 if ((rc = can_set_xattr(inode, name, value, value_len)))
754 return rc;
756 if (strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN) == 0) {
757 os2name = kmalloc(namelen - XATTR_OS2_PREFIX_LEN + 1,
758 GFP_KERNEL);
759 if (!os2name)
760 return -ENOMEM;
761 strcpy(os2name, name + XATTR_OS2_PREFIX_LEN);
762 name = os2name;
763 namelen -= XATTR_OS2_PREFIX_LEN;
766 down_write(&JFS_IP(inode)->xattr_sem);
768 xattr_size = ea_get(inode, &ea_buf, 0);
769 if (xattr_size < 0) {
770 rc = xattr_size;
771 goto out;
774 again:
775 ealist = (struct jfs_ea_list *) ea_buf.xattr;
776 new_size = sizeof (struct jfs_ea_list);
778 if (xattr_size) {
779 for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist);
780 ea = NEXT_EA(ea)) {
781 if ((namelen == ea->namelen) &&
782 (memcmp(name, ea->name, namelen) == 0)) {
783 found = 1;
784 if (flags & XATTR_CREATE) {
785 rc = -EEXIST;
786 goto release;
788 old_ea = ea;
789 old_ea_size = EA_SIZE(ea);
790 next_ea = NEXT_EA(ea);
791 } else
792 new_size += EA_SIZE(ea);
796 if (!found) {
797 if (flags & XATTR_REPLACE) {
798 rc = -ENODATA;
799 goto release;
801 if (value == NULL) {
802 rc = 0;
803 goto release;
806 if (value)
807 new_size += sizeof (struct jfs_ea) + namelen + 1 + value_len;
809 if (new_size > ea_buf.max_size) {
811 * We need to allocate more space for merged ea list.
812 * We should only have loop to again: once.
814 ea_release(inode, &ea_buf);
815 xattr_size = ea_get(inode, &ea_buf, new_size);
816 if (xattr_size < 0) {
817 rc = xattr_size;
818 goto out;
820 goto again;
823 /* Remove old ea of the same name */
824 if (found) {
825 /* number of bytes following target EA */
826 length = (char *) END_EALIST(ealist) - (char *) next_ea;
827 if (length > 0)
828 memmove(old_ea, next_ea, length);
829 xattr_size -= old_ea_size;
832 /* Add new entry to the end */
833 if (value) {
834 if (xattr_size == 0)
835 /* Completely new ea list */
836 xattr_size = sizeof (struct jfs_ea_list);
838 ea = (struct jfs_ea *) ((char *) ealist + xattr_size);
839 ea->flag = 0;
840 ea->namelen = namelen;
841 ea->valuelen = (cpu_to_le16(value_len));
842 memcpy(ea->name, name, namelen);
843 ea->name[namelen] = 0;
844 if (value_len)
845 memcpy(&ea->name[namelen + 1], value, value_len);
846 xattr_size += EA_SIZE(ea);
849 /* DEBUG - If we did this right, these number match */
850 if (xattr_size != new_size) {
851 printk(KERN_ERR
852 "jfs_xsetattr: xattr_size = %d, new_size = %d\n",
853 xattr_size, new_size);
855 rc = -EINVAL;
856 goto release;
860 * If we're left with an empty list, there's no ea
862 if (new_size == sizeof (struct jfs_ea_list))
863 new_size = 0;
865 ealist->size = cpu_to_le32(new_size);
867 rc = ea_put(inode, &ea_buf, new_size);
869 goto out;
870 release:
871 ea_release(inode, &ea_buf);
872 out:
873 up_write(&JFS_IP(inode)->xattr_sem);
875 if (os2name)
876 kfree(os2name);
878 return rc;
881 int jfs_setxattr(struct dentry *dentry, const char *name, const void *value,
882 size_t value_len, int flags)
884 if (value == NULL) { /* empty EA, do not remove */
885 value = "";
886 value_len = 0;
889 return __jfs_setxattr(dentry->d_inode, name, value, value_len, flags);
892 static int can_get_xattr(struct inode *inode, const char *name)
894 #ifdef CONFIG_JFS_POSIX_ACL
895 if(strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN) == 0)
896 return 0;
897 return jfs_permission(inode, MAY_READ, NULL);
898 #else
899 return permission(inode, MAY_READ, NULL);
900 #endif
903 ssize_t __jfs_getxattr(struct inode *inode, const char *name, void *data,
904 size_t buf_size)
906 struct jfs_ea_list *ealist;
907 struct jfs_ea *ea;
908 struct ea_buffer ea_buf;
909 int xattr_size;
910 ssize_t size;
911 int namelen = strlen(name);
912 char *os2name = NULL;
913 int rc;
914 char *value;
916 if ((rc = can_get_xattr(inode, name)))
917 return rc;
919 if (strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN) == 0) {
920 os2name = kmalloc(namelen - XATTR_OS2_PREFIX_LEN + 1,
921 GFP_KERNEL);
922 if (!os2name)
923 return -ENOMEM;
924 strcpy(os2name, name + XATTR_OS2_PREFIX_LEN);
925 name = os2name;
926 namelen -= XATTR_OS2_PREFIX_LEN;
929 down_read(&JFS_IP(inode)->xattr_sem);
931 xattr_size = ea_get(inode, &ea_buf, 0);
933 if (xattr_size < 0) {
934 size = xattr_size;
935 goto out;
938 if (xattr_size == 0)
939 goto not_found;
941 ealist = (struct jfs_ea_list *) ea_buf.xattr;
943 /* Find the named attribute */
944 for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea))
945 if ((namelen == ea->namelen) &&
946 memcmp(name, ea->name, namelen) == 0) {
947 /* Found it */
948 size = le16_to_cpu(ea->valuelen);
949 if (!data)
950 goto release;
951 else if (size > buf_size) {
952 size = -ERANGE;
953 goto release;
955 value = ((char *) &ea->name) + ea->namelen + 1;
956 memcpy(data, value, size);
957 goto release;
959 not_found:
960 size = -ENODATA;
961 release:
962 ea_release(inode, &ea_buf);
963 out:
964 up_read(&JFS_IP(inode)->xattr_sem);
966 if (os2name)
967 kfree(os2name);
969 return size;
972 ssize_t jfs_getxattr(struct dentry *dentry, const char *name, void *data,
973 size_t buf_size)
975 int err;
977 err = __jfs_getxattr(dentry->d_inode, name, data, buf_size);
979 return err;
982 ssize_t jfs_listxattr(struct dentry * dentry, char *data, size_t buf_size)
984 struct inode *inode = dentry->d_inode;
985 char *buffer;
986 ssize_t size = 0;
987 int xattr_size;
988 struct jfs_ea_list *ealist;
989 struct jfs_ea *ea;
990 struct ea_buffer ea_buf;
992 down_read(&JFS_IP(inode)->xattr_sem);
994 xattr_size = ea_get(inode, &ea_buf, 0);
995 if (xattr_size < 0) {
996 size = xattr_size;
997 goto out;
1000 if (xattr_size == 0)
1001 goto release;
1003 ealist = (struct jfs_ea_list *) ea_buf.xattr;
1005 /* compute required size of list */
1006 for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea))
1007 size += name_size(ea) + 1;
1009 if (!data)
1010 goto release;
1012 if (size > buf_size) {
1013 size = -ERANGE;
1014 goto release;
1017 /* Copy attribute names to buffer */
1018 buffer = data;
1019 for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea)) {
1020 int namelen = copy_name(buffer, ea);
1021 buffer += namelen + 1;
1024 release:
1025 ea_release(inode, &ea_buf);
1026 out:
1027 up_read(&JFS_IP(inode)->xattr_sem);
1028 return size;
1031 int jfs_removexattr(struct dentry *dentry, const char *name)
1033 return __jfs_setxattr(dentry->d_inode, name, 0, 0, XATTR_REPLACE);