initial commit with v2.6.9
[linux-2.6.9-moxart.git] / fs / jfs / xattr.c
blob9c0e3a524e9be7852cfd7ebb2d45b07a81410d0a
1 /*
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.
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 <linux/quotaops.h>
23 #include "jfs_incore.h"
24 #include "jfs_superblock.h"
25 #include "jfs_dmap.h"
26 #include "jfs_debug.h"
27 #include "jfs_dinode.h"
28 #include "jfs_extent.h"
29 #include "jfs_metapage.h"
30 #include "jfs_xattr.h"
31 #include "jfs_acl.h"
34 * jfs_xattr.c: extended attribute service
36 * Overall design --
38 * Format:
40 * Extended attribute lists (jfs_ea_list) consist of an overall size (32 bit
41 * value) and a variable (0 or more) number of extended attribute
42 * entries. Each extended attribute entry (jfs_ea) is a <name,value> double
43 * where <name> is constructed from a null-terminated ascii string
44 * (1 ... 255 bytes in the name) and <value> is arbitrary 8 bit data
45 * (1 ... 65535 bytes). The in-memory format is
47 * 0 1 2 4 4 + namelen + 1
48 * +-------+--------+--------+----------------+-------------------+
49 * | Flags | Name | Value | Name String \0 | Data . . . . |
50 * | | Length | Length | | |
51 * +-------+--------+--------+----------------+-------------------+
53 * A jfs_ea_list then is structured as
55 * 0 4 4 + EA_SIZE(ea1)
56 * +------------+-------------------+--------------------+-----
57 * | Overall EA | First FEA Element | Second FEA Element | .....
58 * | List Size | | |
59 * +------------+-------------------+--------------------+-----
61 * On-disk:
63 * FEALISTs are stored on disk using blocks allocated by dbAlloc() and
64 * written directly. An EA list may be in-lined in the inode if there is
65 * sufficient room available.
68 struct ea_buffer {
69 int flag; /* Indicates what storage xattr points to */
70 int max_size; /* largest xattr that fits in current buffer */
71 dxd_t new_ea; /* dxd to replace ea when modifying xattr */
72 struct metapage *mp; /* metapage containing ea list */
73 struct jfs_ea_list *xattr; /* buffer containing ea list */
77 * ea_buffer.flag values
79 #define EA_INLINE 0x0001
80 #define EA_EXTENT 0x0002
81 #define EA_NEW 0x0004
82 #define EA_MALLOC 0x0008
84 /* Namespaces */
85 #define XATTR_SYSTEM_PREFIX "system."
86 #define XATTR_SYSTEM_PREFIX_LEN (sizeof (XATTR_SYSTEM_PREFIX) - 1)
88 #define XATTR_USER_PREFIX "user."
89 #define XATTR_USER_PREFIX_LEN (sizeof (XATTR_USER_PREFIX) - 1)
91 #define XATTR_OS2_PREFIX "os2."
92 #define XATTR_OS2_PREFIX_LEN (sizeof (XATTR_OS2_PREFIX) - 1)
95 * These three routines are used to recognize on-disk extended attributes
96 * that are in a recognized namespace. If the attribute is not recognized,
97 * "os2." is prepended to the name
99 static inline int is_os2_xattr(struct jfs_ea *ea)
102 * Check for "system."
104 if ((ea->namelen >= XATTR_SYSTEM_PREFIX_LEN) &&
105 !strncmp(ea->name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
106 return FALSE;
108 * Check for "user."
110 if ((ea->namelen >= XATTR_USER_PREFIX_LEN) &&
111 !strncmp(ea->name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
112 return FALSE;
114 * Add any other valid namespace prefixes here
118 * We assume it's OS/2's flat namespace
120 return TRUE;
123 static inline int name_size(struct jfs_ea *ea)
125 if (is_os2_xattr(ea))
126 return ea->namelen + XATTR_OS2_PREFIX_LEN;
127 else
128 return ea->namelen;
131 static inline int copy_name(char *buffer, struct jfs_ea *ea)
133 int len = ea->namelen;
135 if (is_os2_xattr(ea)) {
136 memcpy(buffer, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN);
137 buffer += XATTR_OS2_PREFIX_LEN;
138 len += XATTR_OS2_PREFIX_LEN;
140 memcpy(buffer, ea->name, ea->namelen);
141 buffer[ea->namelen] = 0;
143 return len;
146 /* Forward references */
147 static void ea_release(struct inode *inode, struct ea_buffer *ea_buf);
150 * NAME: ea_write_inline
152 * FUNCTION: Attempt to write an EA inline if area is available
154 * PRE CONDITIONS:
155 * Already verified that the specified EA is small enough to fit inline
157 * PARAMETERS:
158 * ip - Inode pointer
159 * ealist - EA list pointer
160 * size - size of ealist in bytes
161 * ea - dxd_t structure to be filled in with necessary EA information
162 * if we successfully copy the EA inline
164 * NOTES:
165 * Checks if the inode's inline area is available. If so, copies EA inline
166 * and sets <ea> fields appropriately. Otherwise, returns failure, EA will
167 * have to be put into an extent.
169 * RETURNS: 0 for successful copy to inline area; -1 if area not available
171 static int ea_write_inline(struct inode *ip, struct jfs_ea_list *ealist,
172 int size, dxd_t * ea)
174 struct jfs_inode_info *ji = JFS_IP(ip);
177 * Make sure we have an EA -- the NULL EA list is valid, but you
178 * can't copy it!
180 if (ealist && size > sizeof (struct jfs_ea_list)) {
181 assert(size <= sizeof (ji->i_inline_ea));
184 * See if the space is available or if it is already being
185 * used for an inline EA.
187 if (!(ji->mode2 & INLINEEA) && !(ji->ea.flag & DXD_INLINE))
188 return -EPERM;
190 DXDsize(ea, size);
191 DXDlength(ea, 0);
192 DXDaddress(ea, 0);
193 memcpy(ji->i_inline_ea, ealist, size);
194 ea->flag = DXD_INLINE;
195 ji->mode2 &= ~INLINEEA;
196 } else {
197 ea->flag = 0;
198 DXDsize(ea, 0);
199 DXDlength(ea, 0);
200 DXDaddress(ea, 0);
202 /* Free up INLINE area */
203 if (ji->ea.flag & DXD_INLINE)
204 ji->mode2 |= INLINEEA;
207 return 0;
211 * NAME: ea_write
213 * FUNCTION: Write an EA for an inode
215 * PRE CONDITIONS: EA has been verified
217 * PARAMETERS:
218 * ip - Inode pointer
219 * ealist - EA list pointer
220 * size - size of ealist in bytes
221 * ea - dxd_t structure to be filled in appropriately with where the
222 * EA was copied
224 * NOTES: Will write EA inline if able to, otherwise allocates blocks for an
225 * extent and synchronously writes it to those blocks.
227 * RETURNS: 0 for success; Anything else indicates failure
229 static int ea_write(struct inode *ip, struct jfs_ea_list *ealist, int size,
230 dxd_t * ea)
232 struct super_block *sb = ip->i_sb;
233 struct jfs_inode_info *ji = JFS_IP(ip);
234 struct jfs_sb_info *sbi = JFS_SBI(sb);
235 int nblocks;
236 s64 blkno;
237 int rc = 0, i;
238 char *cp;
239 s32 nbytes, nb;
240 s32 bytes_to_write;
241 struct metapage *mp;
244 * Quick check to see if this is an in-linable EA. Short EAs
245 * and empty EAs are all in-linable, provided the space exists.
247 if (!ealist || size <= sizeof (ji->i_inline_ea)) {
248 if (!ea_write_inline(ip, ealist, size, ea))
249 return 0;
252 /* figure out how many blocks we need */
253 nblocks = (size + (sb->s_blocksize - 1)) >> sb->s_blocksize_bits;
255 /* Allocate new blocks to quota. */
256 if (DQUOT_ALLOC_BLOCK(ip, nblocks)) {
257 return -EDQUOT;
260 rc = dbAlloc(ip, INOHINT(ip), nblocks, &blkno);
261 if (rc) {
262 /*Rollback quota allocation. */
263 DQUOT_FREE_BLOCK(ip, nblocks);
264 return rc;
268 * Now have nblocks worth of storage to stuff into the FEALIST.
269 * loop over the FEALIST copying data into the buffer one page at
270 * a time.
272 cp = (char *) ealist;
273 nbytes = size;
274 for (i = 0; i < nblocks; i += sbi->nbperpage) {
276 * Determine how many bytes for this request, and round up to
277 * the nearest aggregate block size
279 nb = min(PSIZE, nbytes);
280 bytes_to_write =
281 ((((nb + sb->s_blocksize - 1)) >> sb->s_blocksize_bits))
282 << sb->s_blocksize_bits;
284 if (!(mp = get_metapage(ip, blkno + i, bytes_to_write, 1))) {
285 rc = -EIO;
286 goto failed;
289 memcpy(mp->data, cp, nb);
292 * We really need a way to propagate errors for
293 * forced writes like this one. --hch
295 * (__write_metapage => release_metapage => flush_metapage)
297 #ifdef _JFS_FIXME
298 if ((rc = flush_metapage(mp))) {
300 * the write failed -- this means that the buffer
301 * is still assigned and the blocks are not being
302 * used. this seems like the best error recovery
303 * we can get ...
305 goto failed;
307 #else
308 flush_metapage(mp);
309 #endif
311 cp += PSIZE;
312 nbytes -= nb;
315 ea->flag = DXD_EXTENT;
316 DXDsize(ea, le32_to_cpu(ealist->size));
317 DXDlength(ea, nblocks);
318 DXDaddress(ea, blkno);
320 /* Free up INLINE area */
321 if (ji->ea.flag & DXD_INLINE)
322 ji->mode2 |= INLINEEA;
324 return 0;
326 failed:
327 /* Rollback quota allocation. */
328 DQUOT_FREE_BLOCK(ip, nblocks);
330 dbFree(ip, blkno, nblocks);
331 return rc;
335 * NAME: ea_read_inline
337 * FUNCTION: Read an inlined EA into user's buffer
339 * PARAMETERS:
340 * ip - Inode pointer
341 * ealist - Pointer to buffer to fill in with EA
343 * RETURNS: 0
345 static int ea_read_inline(struct inode *ip, struct jfs_ea_list *ealist)
347 struct jfs_inode_info *ji = JFS_IP(ip);
348 int ea_size = sizeDXD(&ji->ea);
350 if (ea_size == 0) {
351 ealist->size = 0;
352 return 0;
355 /* Sanity Check */
356 if ((sizeDXD(&ji->ea) > sizeof (ji->i_inline_ea)))
357 return -EIO;
358 if (le32_to_cpu(((struct jfs_ea_list *) &ji->i_inline_ea)->size)
359 != ea_size)
360 return -EIO;
362 memcpy(ealist, ji->i_inline_ea, ea_size);
363 return 0;
367 * NAME: ea_read
369 * FUNCTION: copy EA data into user's buffer
371 * PARAMETERS:
372 * ip - Inode pointer
373 * ealist - Pointer to buffer to fill in with EA
375 * NOTES: If EA is inline calls ea_read_inline() to copy EA.
377 * RETURNS: 0 for success; other indicates failure
379 static int ea_read(struct inode *ip, struct jfs_ea_list *ealist)
381 struct super_block *sb = ip->i_sb;
382 struct jfs_inode_info *ji = JFS_IP(ip);
383 struct jfs_sb_info *sbi = JFS_SBI(sb);
384 int nblocks;
385 s64 blkno;
386 char *cp = (char *) ealist;
387 int i;
388 int nbytes, nb;
389 s32 bytes_to_read;
390 struct metapage *mp;
392 /* quick check for in-line EA */
393 if (ji->ea.flag & DXD_INLINE)
394 return ea_read_inline(ip, ealist);
396 nbytes = sizeDXD(&ji->ea);
397 if (!nbytes) {
398 jfs_error(sb, "ea_read: nbytes is 0");
399 return -EIO;
403 * Figure out how many blocks were allocated when this EA list was
404 * originally written to disk.
406 nblocks = lengthDXD(&ji->ea) << sbi->l2nbperpage;
407 blkno = addressDXD(&ji->ea) << sbi->l2nbperpage;
410 * I have found the disk blocks which were originally used to store
411 * the FEALIST. now i loop over each contiguous block copying the
412 * data into the buffer.
414 for (i = 0; i < nblocks; i += sbi->nbperpage) {
416 * Determine how many bytes for this request, and round up to
417 * the nearest aggregate block size
419 nb = min(PSIZE, nbytes);
420 bytes_to_read =
421 ((((nb + sb->s_blocksize - 1)) >> sb->s_blocksize_bits))
422 << sb->s_blocksize_bits;
424 if (!(mp = read_metapage(ip, blkno + i, bytes_to_read, 1)))
425 return -EIO;
427 memcpy(cp, mp->data, nb);
428 release_metapage(mp);
430 cp += PSIZE;
431 nbytes -= nb;
434 return 0;
438 * NAME: ea_get
440 * FUNCTION: Returns buffer containing existing extended attributes.
441 * The size of the buffer will be the larger of the existing
442 * attributes size, or min_size.
444 * The buffer, which may be inlined in the inode or in the
445 * page cache must be release by calling ea_release or ea_put
447 * PARAMETERS:
448 * inode - Inode pointer
449 * ea_buf - Structure to be populated with ealist and its metadata
450 * min_size- minimum size of buffer to be returned
452 * RETURNS: 0 for success; Other indicates failure
454 static int ea_get(struct inode *inode, struct ea_buffer *ea_buf, int min_size)
456 struct jfs_inode_info *ji = JFS_IP(inode);
457 struct super_block *sb = inode->i_sb;
458 int size;
459 int ea_size = sizeDXD(&ji->ea);
460 int blocks_needed, current_blocks;
461 s64 blkno;
462 int rc;
463 int quota_allocation = 0;
465 /* When fsck.jfs clears a bad ea, it doesn't clear the size */
466 if (ji->ea.flag == 0)
467 ea_size = 0;
469 if (ea_size == 0) {
470 if (min_size == 0) {
471 ea_buf->flag = 0;
472 ea_buf->max_size = 0;
473 ea_buf->xattr = NULL;
474 return 0;
476 if ((min_size <= sizeof (ji->i_inline_ea)) &&
477 (ji->mode2 & INLINEEA)) {
478 ea_buf->flag = EA_INLINE | EA_NEW;
479 ea_buf->max_size = sizeof (ji->i_inline_ea);
480 ea_buf->xattr = (struct jfs_ea_list *) ji->i_inline_ea;
481 DXDlength(&ea_buf->new_ea, 0);
482 DXDaddress(&ea_buf->new_ea, 0);
483 ea_buf->new_ea.flag = DXD_INLINE;
484 DXDsize(&ea_buf->new_ea, min_size);
485 return 0;
487 current_blocks = 0;
488 } else if (ji->ea.flag & DXD_INLINE) {
489 if (min_size <= sizeof (ji->i_inline_ea)) {
490 ea_buf->flag = EA_INLINE;
491 ea_buf->max_size = sizeof (ji->i_inline_ea);
492 ea_buf->xattr = (struct jfs_ea_list *) ji->i_inline_ea;
493 goto size_check;
495 current_blocks = 0;
496 } else {
497 if (!(ji->ea.flag & DXD_EXTENT)) {
498 jfs_error(sb, "ea_get: invalid ea.flag)");
499 return -EIO;
501 current_blocks = (ea_size + sb->s_blocksize - 1) >>
502 sb->s_blocksize_bits;
504 size = max(min_size, ea_size);
506 if (size > PSIZE) {
508 * To keep the rest of the code simple. Allocate a
509 * contiguous buffer to work with
511 ea_buf->xattr = kmalloc(size, GFP_KERNEL);
512 if (ea_buf->xattr == NULL)
513 return -ENOMEM;
515 ea_buf->flag = EA_MALLOC;
516 ea_buf->max_size = (size + sb->s_blocksize - 1) &
517 ~(sb->s_blocksize - 1);
519 if (ea_size == 0)
520 return 0;
522 if ((rc = ea_read(inode, ea_buf->xattr))) {
523 kfree(ea_buf->xattr);
524 ea_buf->xattr = NULL;
525 return rc;
527 goto size_check;
529 blocks_needed = (min_size + sb->s_blocksize - 1) >>
530 sb->s_blocksize_bits;
532 if (blocks_needed > current_blocks) {
533 /* Allocate new blocks to quota. */
534 if (DQUOT_ALLOC_BLOCK(inode, blocks_needed))
535 return -EDQUOT;
537 quota_allocation = blocks_needed;
539 rc = dbAlloc(inode, INOHINT(inode), (s64) blocks_needed,
540 &blkno);
541 if (rc)
542 goto clean_up;
544 DXDlength(&ea_buf->new_ea, blocks_needed);
545 DXDaddress(&ea_buf->new_ea, blkno);
546 ea_buf->new_ea.flag = DXD_EXTENT;
547 DXDsize(&ea_buf->new_ea, min_size);
549 ea_buf->flag = EA_EXTENT | EA_NEW;
551 ea_buf->mp = get_metapage(inode, blkno,
552 blocks_needed << sb->s_blocksize_bits,
554 if (ea_buf->mp == NULL) {
555 dbFree(inode, blkno, (s64) blocks_needed);
556 rc = -EIO;
557 goto clean_up;
559 ea_buf->xattr = ea_buf->mp->data;
560 ea_buf->max_size = (min_size + sb->s_blocksize - 1) &
561 ~(sb->s_blocksize - 1);
562 if (ea_size == 0)
563 return 0;
564 if ((rc = ea_read(inode, ea_buf->xattr))) {
565 discard_metapage(ea_buf->mp);
566 dbFree(inode, blkno, (s64) blocks_needed);
567 goto clean_up;
569 goto size_check;
571 ea_buf->flag = EA_EXTENT;
572 ea_buf->mp = read_metapage(inode, addressDXD(&ji->ea),
573 lengthDXD(&ji->ea) << sb->s_blocksize_bits,
575 if (ea_buf->mp == NULL) {
576 rc = -EIO;
577 goto clean_up;
579 ea_buf->xattr = ea_buf->mp->data;
580 ea_buf->max_size = (ea_size + sb->s_blocksize - 1) &
581 ~(sb->s_blocksize - 1);
583 size_check:
584 if (EALIST_SIZE(ea_buf->xattr) != ea_size) {
585 printk(KERN_ERR "ea_get: invalid extended attribute\n");
586 dump_mem("xattr", ea_buf->xattr, ea_size);
587 ea_release(inode, ea_buf);
588 rc = -EIO;
589 goto clean_up;
592 return ea_size;
594 clean_up:
595 /* Rollback quota allocation */
596 if (quota_allocation)
597 DQUOT_FREE_BLOCK(inode, quota_allocation);
599 return (rc);
602 static void ea_release(struct inode *inode, struct ea_buffer *ea_buf)
604 if (ea_buf->flag & EA_MALLOC)
605 kfree(ea_buf->xattr);
606 else if (ea_buf->flag & EA_EXTENT) {
607 assert(ea_buf->mp);
608 release_metapage(ea_buf->mp);
610 if (ea_buf->flag & EA_NEW)
611 dbFree(inode, addressDXD(&ea_buf->new_ea),
612 lengthDXD(&ea_buf->new_ea));
616 static int ea_put(struct inode *inode, struct ea_buffer *ea_buf, int new_size)
618 struct jfs_inode_info *ji = JFS_IP(inode);
619 unsigned long old_blocks, new_blocks;
620 int rc = 0;
621 tid_t tid;
623 if (new_size == 0) {
624 ea_release(inode, ea_buf);
625 ea_buf = NULL;
626 } else if (ea_buf->flag & EA_INLINE) {
627 assert(new_size <= sizeof (ji->i_inline_ea));
628 ji->mode2 &= ~INLINEEA;
629 ea_buf->new_ea.flag = DXD_INLINE;
630 DXDsize(&ea_buf->new_ea, new_size);
631 DXDaddress(&ea_buf->new_ea, 0);
632 DXDlength(&ea_buf->new_ea, 0);
633 } else if (ea_buf->flag & EA_MALLOC) {
634 rc = ea_write(inode, ea_buf->xattr, new_size, &ea_buf->new_ea);
635 kfree(ea_buf->xattr);
636 } else if (ea_buf->flag & EA_NEW) {
637 /* We have already allocated a new dxd */
638 flush_metapage(ea_buf->mp);
639 } else {
640 /* ->xattr must point to original ea's metapage */
641 rc = ea_write(inode, ea_buf->xattr, new_size, &ea_buf->new_ea);
642 discard_metapage(ea_buf->mp);
644 if (rc)
645 return rc;
647 tid = txBegin(inode->i_sb, 0);
648 down(&ji->commit_sem);
650 old_blocks = new_blocks = 0;
652 if (ji->ea.flag & DXD_EXTENT) {
653 invalidate_dxd_metapages(inode, ji->ea);
654 old_blocks = lengthDXD(&ji->ea);
657 if (ea_buf) {
658 txEA(tid, inode, &ji->ea, &ea_buf->new_ea);
659 if (ea_buf->new_ea.flag & DXD_EXTENT) {
660 new_blocks = lengthDXD(&ea_buf->new_ea);
661 if (ji->ea.flag & DXD_INLINE)
662 ji->mode2 |= INLINEEA;
664 ji->ea = ea_buf->new_ea;
665 } else {
666 txEA(tid, inode, &ji->ea, NULL);
667 if (ji->ea.flag & DXD_INLINE)
668 ji->mode2 |= INLINEEA;
669 ji->ea.flag = 0;
670 ji->ea.size = 0;
673 /* If old blocks exist, they must be removed from quota allocation. */
674 if (old_blocks)
675 DQUOT_FREE_BLOCK(inode, old_blocks);
677 inode->i_ctime = CURRENT_TIME;
678 rc = txCommit(tid, 1, &inode, 0);
679 txEnd(tid);
680 up(&ji->commit_sem);
682 return rc;
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;
696 int rc;
698 if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
699 return -EPERM;
702 * XATTR_NAME_ACL_ACCESS is tied to i_mode
704 if (strcmp(name, XATTR_NAME_ACL_ACCESS) == 0) {
705 acl = posix_acl_from_xattr(value, value_len);
706 if (IS_ERR(acl)) {
707 rc = PTR_ERR(acl);
708 printk(KERN_ERR "posix_acl_from_xattr returned %d\n",
709 rc);
710 return rc;
712 if (acl) {
713 mode_t mode = inode->i_mode;
714 rc = posix_acl_equiv_mode(acl, &mode);
715 posix_acl_release(acl);
716 if (rc < 0) {
717 printk(KERN_ERR
718 "posix_acl_equiv_mode returned %d\n",
719 rc);
720 return rc;
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;
733 return 0;
734 } else if (strcmp(name, XATTR_NAME_ACL_DEFAULT) == 0) {
735 acl = posix_acl_from_xattr(value, value_len);
736 if (IS_ERR(acl)) {
737 rc = PTR_ERR(acl);
738 printk(KERN_ERR "posix_acl_from_xattr returned %d\n",
739 rc);
740 return rc;
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;
752 return 0;
754 #endif /* CONFIG_JFS_POSIX_ACL */
755 return -EOPNOTSUPP;
758 static int can_set_xattr(struct inode *inode, const char *name,
759 const void *value, size_t value_len)
761 if (IS_RDONLY(inode))
762 return -EROFS;
764 if (IS_IMMUTABLE(inode) || IS_APPEND(inode) || S_ISLNK(inode->i_mode))
765 return -EPERM;
767 if(strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN) == 0)
769 * "system.*"
771 return can_set_system_xattr(inode, name, value, value_len);
773 if((strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN) != 0) &&
774 (strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN) != 0))
775 return -EOPNOTSUPP;
777 if (!S_ISREG(inode->i_mode) &&
778 (!S_ISDIR(inode->i_mode) || inode->i_mode &S_ISVTX))
779 return -EPERM;
781 return permission(inode, MAY_WRITE, NULL);
784 int __jfs_setxattr(struct inode *inode, const char *name, const void *value,
785 size_t value_len, int flags)
787 struct jfs_ea_list *ealist;
788 struct jfs_ea *ea, *old_ea = NULL, *next_ea = NULL;
789 struct ea_buffer ea_buf;
790 int old_ea_size = 0;
791 int xattr_size;
792 int new_size;
793 int namelen = strlen(name);
794 char *os2name = NULL;
795 int found = 0;
796 int rc;
797 int length;
799 if ((rc = can_set_xattr(inode, name, value, value_len)))
800 return rc;
802 if (strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN) == 0) {
803 os2name = kmalloc(namelen - XATTR_OS2_PREFIX_LEN + 1,
804 GFP_KERNEL);
805 if (!os2name)
806 return -ENOMEM;
807 strcpy(os2name, name + XATTR_OS2_PREFIX_LEN);
808 name = os2name;
809 namelen -= XATTR_OS2_PREFIX_LEN;
812 down_write(&JFS_IP(inode)->xattr_sem);
814 xattr_size = ea_get(inode, &ea_buf, 0);
815 if (xattr_size < 0) {
816 rc = xattr_size;
817 goto out;
820 again:
821 ealist = (struct jfs_ea_list *) ea_buf.xattr;
822 new_size = sizeof (struct jfs_ea_list);
824 if (xattr_size) {
825 for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist);
826 ea = NEXT_EA(ea)) {
827 if ((namelen == ea->namelen) &&
828 (memcmp(name, ea->name, namelen) == 0)) {
829 found = 1;
830 if (flags & XATTR_CREATE) {
831 rc = -EEXIST;
832 goto release;
834 old_ea = ea;
835 old_ea_size = EA_SIZE(ea);
836 next_ea = NEXT_EA(ea);
837 } else
838 new_size += EA_SIZE(ea);
842 if (!found) {
843 if (flags & XATTR_REPLACE) {
844 rc = -ENODATA;
845 goto release;
847 if (value == NULL) {
848 rc = 0;
849 goto release;
852 if (value)
853 new_size += sizeof (struct jfs_ea) + namelen + 1 + value_len;
855 if (new_size > ea_buf.max_size) {
857 * We need to allocate more space for merged ea list.
858 * We should only have loop to again: once.
860 ea_release(inode, &ea_buf);
861 xattr_size = ea_get(inode, &ea_buf, new_size);
862 if (xattr_size < 0) {
863 rc = xattr_size;
864 goto out;
866 goto again;
869 /* Remove old ea of the same name */
870 if (found) {
871 /* number of bytes following target EA */
872 length = (char *) END_EALIST(ealist) - (char *) next_ea;
873 if (length > 0)
874 memmove(old_ea, next_ea, length);
875 xattr_size -= old_ea_size;
878 /* Add new entry to the end */
879 if (value) {
880 if (xattr_size == 0)
881 /* Completely new ea list */
882 xattr_size = sizeof (struct jfs_ea_list);
884 ea = (struct jfs_ea *) ((char *) ealist + xattr_size);
885 ea->flag = 0;
886 ea->namelen = namelen;
887 ea->valuelen = (cpu_to_le16(value_len));
888 memcpy(ea->name, name, namelen);
889 ea->name[namelen] = 0;
890 if (value_len)
891 memcpy(&ea->name[namelen + 1], value, value_len);
892 xattr_size += EA_SIZE(ea);
895 /* DEBUG - If we did this right, these number match */
896 if (xattr_size != new_size) {
897 printk(KERN_ERR
898 "jfs_xsetattr: xattr_size = %d, new_size = %d\n",
899 xattr_size, new_size);
901 rc = -EINVAL;
902 goto release;
906 * If we're left with an empty list, there's no ea
908 if (new_size == sizeof (struct jfs_ea_list))
909 new_size = 0;
911 ealist->size = cpu_to_le32(new_size);
913 rc = ea_put(inode, &ea_buf, new_size);
915 goto out;
916 release:
917 ea_release(inode, &ea_buf);
918 out:
919 up_write(&JFS_IP(inode)->xattr_sem);
921 if (os2name)
922 kfree(os2name);
924 return rc;
927 int jfs_setxattr(struct dentry *dentry, const char *name, const void *value,
928 size_t value_len, int flags)
930 if (value == NULL) { /* empty EA, do not remove */
931 value = "";
932 value_len = 0;
935 return __jfs_setxattr(dentry->d_inode, name, value, value_len, flags);
938 static int can_get_xattr(struct inode *inode, const char *name)
940 if(strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN) == 0)
941 return 0;
942 return permission(inode, MAY_READ, NULL);
945 ssize_t __jfs_getxattr(struct inode *inode, const char *name, void *data,
946 size_t buf_size)
948 struct jfs_ea_list *ealist;
949 struct jfs_ea *ea;
950 struct ea_buffer ea_buf;
951 int xattr_size;
952 ssize_t size;
953 int namelen = strlen(name);
954 char *os2name = NULL;
955 int rc;
956 char *value;
958 if ((rc = can_get_xattr(inode, name)))
959 return rc;
961 if (strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN) == 0) {
962 os2name = kmalloc(namelen - XATTR_OS2_PREFIX_LEN + 1,
963 GFP_KERNEL);
964 if (!os2name)
965 return -ENOMEM;
966 strcpy(os2name, name + XATTR_OS2_PREFIX_LEN);
967 name = os2name;
968 namelen -= XATTR_OS2_PREFIX_LEN;
971 down_read(&JFS_IP(inode)->xattr_sem);
973 xattr_size = ea_get(inode, &ea_buf, 0);
975 if (xattr_size < 0) {
976 size = xattr_size;
977 goto out;
980 if (xattr_size == 0)
981 goto not_found;
983 ealist = (struct jfs_ea_list *) ea_buf.xattr;
985 /* Find the named attribute */
986 for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea))
987 if ((namelen == ea->namelen) &&
988 memcmp(name, ea->name, namelen) == 0) {
989 /* Found it */
990 size = le16_to_cpu(ea->valuelen);
991 if (!data)
992 goto release;
993 else if (size > buf_size) {
994 size = -ERANGE;
995 goto release;
997 value = ((char *) &ea->name) + ea->namelen + 1;
998 memcpy(data, value, size);
999 goto release;
1001 not_found:
1002 size = -ENODATA;
1003 release:
1004 ea_release(inode, &ea_buf);
1005 out:
1006 up_read(&JFS_IP(inode)->xattr_sem);
1008 if (os2name)
1009 kfree(os2name);
1011 return size;
1014 ssize_t jfs_getxattr(struct dentry *dentry, const char *name, void *data,
1015 size_t buf_size)
1017 int err;
1019 err = __jfs_getxattr(dentry->d_inode, name, data, buf_size);
1021 return err;
1024 ssize_t jfs_listxattr(struct dentry * dentry, char *data, size_t buf_size)
1026 struct inode *inode = dentry->d_inode;
1027 char *buffer;
1028 ssize_t size = 0;
1029 int xattr_size;
1030 struct jfs_ea_list *ealist;
1031 struct jfs_ea *ea;
1032 struct ea_buffer ea_buf;
1034 down_read(&JFS_IP(inode)->xattr_sem);
1036 xattr_size = ea_get(inode, &ea_buf, 0);
1037 if (xattr_size < 0) {
1038 size = xattr_size;
1039 goto out;
1042 if (xattr_size == 0)
1043 goto release;
1045 ealist = (struct jfs_ea_list *) ea_buf.xattr;
1047 /* compute required size of list */
1048 for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea))
1049 size += name_size(ea) + 1;
1051 if (!data)
1052 goto release;
1054 if (size > buf_size) {
1055 size = -ERANGE;
1056 goto release;
1059 /* Copy attribute names to buffer */
1060 buffer = data;
1061 for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea)) {
1062 int namelen = copy_name(buffer, ea);
1063 buffer += namelen + 1;
1066 release:
1067 ea_release(inode, &ea_buf);
1068 out:
1069 up_read(&JFS_IP(inode)->xattr_sem);
1070 return size;
1073 int jfs_removexattr(struct dentry *dentry, const char *name)
1075 return __jfs_setxattr(dentry->d_inode, name, NULL, 0, XATTR_REPLACE);