[PATCH] remove jfs xattr permission checks
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / jfs / xattr.c
blob952da5f917cdc7ad4133ccc1a8cf46e5bd94ec14
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/posix_acl_xattr.h>
23 #include <linux/quotaops.h>
24 #include <linux/security.h>
25 #include "jfs_incore.h"
26 #include "jfs_superblock.h"
27 #include "jfs_dmap.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"
33 #include "jfs_acl.h"
36 * jfs_xattr.c: extended attribute service
38 * Overall design --
40 * Format:
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 | .....
60 * | List Size | | |
61 * +------------+-------------------+--------------------+-----
63 * On-disk:
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.
70 struct ea_buffer {
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
83 #define EA_NEW 0x0004
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)
95 * Check for "system."
97 if ((ea->namelen >= XATTR_SYSTEM_PREFIX_LEN) &&
98 !strncmp(ea->name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
99 return FALSE;
101 * Check for "user."
103 if ((ea->namelen >= XATTR_USER_PREFIX_LEN) &&
104 !strncmp(ea->name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
105 return FALSE;
107 * Check for "security."
109 if ((ea->namelen >= XATTR_SECURITY_PREFIX_LEN) &&
110 !strncmp(ea->name, XATTR_SECURITY_PREFIX,
111 XATTR_SECURITY_PREFIX_LEN))
112 return FALSE;
114 * Check for "trusted."
116 if ((ea->namelen >= XATTR_TRUSTED_PREFIX_LEN) &&
117 !strncmp(ea->name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN))
118 return FALSE;
120 * Add any other valid namespace prefixes here
124 * We assume it's OS/2's flat namespace
126 return TRUE;
129 static inline int name_size(struct jfs_ea *ea)
131 if (is_os2_xattr(ea))
132 return ea->namelen + XATTR_OS2_PREFIX_LEN;
133 else
134 return ea->namelen;
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;
149 return len;
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
160 * PRE CONDITIONS:
161 * Already verified that the specified EA is small enough to fit inline
163 * PARAMETERS:
164 * ip - Inode pointer
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
170 * NOTES:
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
184 * can't copy it!
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))
194 return -EPERM;
196 DXDsize(ea, size);
197 DXDlength(ea, 0);
198 DXDaddress(ea, 0);
199 memcpy(ji->i_inline_ea, ealist, size);
200 ea->flag = DXD_INLINE;
201 ji->mode2 &= ~INLINEEA;
202 } else {
203 ea->flag = 0;
204 DXDsize(ea, 0);
205 DXDlength(ea, 0);
206 DXDaddress(ea, 0);
208 /* Free up INLINE area */
209 if (ji->ea.flag & DXD_INLINE)
210 ji->mode2 |= INLINEEA;
213 return 0;
217 * NAME: ea_write
219 * FUNCTION: Write an EA for an inode
221 * PRE CONDITIONS: EA has been verified
223 * PARAMETERS:
224 * ip - Inode pointer
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
228 * EA was copied
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,
236 dxd_t * ea)
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);
241 int nblocks;
242 s64 blkno;
243 int rc = 0, i;
244 char *cp;
245 s32 nbytes, nb;
246 s32 bytes_to_write;
247 struct metapage *mp;
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))
255 return 0;
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)) {
263 return -EDQUOT;
266 rc = dbAlloc(ip, INOHINT(ip), nblocks, &blkno);
267 if (rc) {
268 /*Rollback quota allocation. */
269 DQUOT_FREE_BLOCK(ip, nblocks);
270 return rc;
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
276 * a time.
278 cp = (char *) ealist;
279 nbytes = size;
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);
286 bytes_to_write =
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))) {
291 rc = -EIO;
292 goto failed;
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)
303 #ifdef _JFS_FIXME
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
309 * we can get ...
311 goto failed;
313 #else
314 flush_metapage(mp);
315 #endif
317 cp += PSIZE;
318 nbytes -= nb;
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;
330 return 0;
332 failed:
333 /* Rollback quota allocation. */
334 DQUOT_FREE_BLOCK(ip, nblocks);
336 dbFree(ip, blkno, nblocks);
337 return rc;
341 * NAME: ea_read_inline
343 * FUNCTION: Read an inlined EA into user's buffer
345 * PARAMETERS:
346 * ip - Inode pointer
347 * ealist - Pointer to buffer to fill in with EA
349 * RETURNS: 0
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);
356 if (ea_size == 0) {
357 ealist->size = 0;
358 return 0;
361 /* Sanity Check */
362 if ((sizeDXD(&ji->ea) > sizeof (ji->i_inline_ea)))
363 return -EIO;
364 if (le32_to_cpu(((struct jfs_ea_list *) &ji->i_inline_ea)->size)
365 != ea_size)
366 return -EIO;
368 memcpy(ealist, ji->i_inline_ea, ea_size);
369 return 0;
373 * NAME: ea_read
375 * FUNCTION: copy EA data into user's buffer
377 * PARAMETERS:
378 * ip - Inode pointer
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);
390 int nblocks;
391 s64 blkno;
392 char *cp = (char *) ealist;
393 int i;
394 int nbytes, nb;
395 s32 bytes_to_read;
396 struct metapage *mp;
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);
403 if (!nbytes) {
404 jfs_error(sb, "ea_read: nbytes is 0");
405 return -EIO;
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);
426 bytes_to_read =
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)))
431 return -EIO;
433 memcpy(cp, mp->data, nb);
434 release_metapage(mp);
436 cp += PSIZE;
437 nbytes -= nb;
440 return 0;
444 * NAME: ea_get
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
453 * PARAMETERS:
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;
464 int size;
465 int ea_size = sizeDXD(&ji->ea);
466 int blocks_needed, current_blocks;
467 s64 blkno;
468 int rc;
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)
473 ea_size = 0;
475 if (ea_size == 0) {
476 if (min_size == 0) {
477 ea_buf->flag = 0;
478 ea_buf->max_size = 0;
479 ea_buf->xattr = NULL;
480 return 0;
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);
491 return 0;
493 current_blocks = 0;
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;
499 goto size_check;
501 current_blocks = 0;
502 } else {
503 if (!(ji->ea.flag & DXD_EXTENT)) {
504 jfs_error(sb, "ea_get: invalid ea.flag)");
505 return -EIO;
507 current_blocks = (ea_size + sb->s_blocksize - 1) >>
508 sb->s_blocksize_bits;
510 size = max(min_size, ea_size);
512 if (size > PSIZE) {
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)
519 return -ENOMEM;
521 ea_buf->flag = EA_MALLOC;
522 ea_buf->max_size = (size + sb->s_blocksize - 1) &
523 ~(sb->s_blocksize - 1);
525 if (ea_size == 0)
526 return 0;
528 if ((rc = ea_read(inode, ea_buf->xattr))) {
529 kfree(ea_buf->xattr);
530 ea_buf->xattr = NULL;
531 return rc;
533 goto size_check;
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))
541 return -EDQUOT;
543 quota_allocation = blocks_needed;
545 rc = dbAlloc(inode, INOHINT(inode), (s64) blocks_needed,
546 &blkno);
547 if (rc)
548 goto clean_up;
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);
562 rc = -EIO;
563 goto clean_up;
565 ea_buf->xattr = ea_buf->mp->data;
566 ea_buf->max_size = (min_size + sb->s_blocksize - 1) &
567 ~(sb->s_blocksize - 1);
568 if (ea_size == 0)
569 return 0;
570 if ((rc = ea_read(inode, ea_buf->xattr))) {
571 discard_metapage(ea_buf->mp);
572 dbFree(inode, blkno, (s64) blocks_needed);
573 goto clean_up;
575 goto size_check;
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) {
582 rc = -EIO;
583 goto clean_up;
585 ea_buf->xattr = ea_buf->mp->data;
586 ea_buf->max_size = (ea_size + sb->s_blocksize - 1) &
587 ~(sb->s_blocksize - 1);
589 size_check:
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);
594 rc = -EIO;
595 goto clean_up;
598 return ea_size;
600 clean_up:
601 /* Rollback quota allocation */
602 if (quota_allocation)
603 DQUOT_FREE_BLOCK(inode, quota_allocation);
605 return (rc);
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) {
613 assert(ea_buf->mp);
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,
623 int new_size)
625 struct jfs_inode_info *ji = JFS_IP(inode);
626 unsigned long old_blocks, new_blocks;
627 int rc = 0;
629 if (new_size == 0) {
630 ea_release(inode, ea_buf);
631 ea_buf = NULL;
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);
645 } else {
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);
650 if (rc)
651 return rc;
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);
660 if (ea_buf) {
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;
668 } else {
669 txEA(tid, inode, &ji->ea, NULL);
670 if (ji->ea.flag & DXD_INLINE)
671 ji->mode2 |= INLINEEA;
672 ji->ea.flag = 0;
673 ji->ea.size = 0;
676 /* If old blocks exist, they must be removed from quota allocation. */
677 if (old_blocks)
678 DQUOT_FREE_BLOCK(inode, old_blocks);
680 inode->i_ctime = CURRENT_TIME;
682 return 0;
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 * 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);
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, POSIX_ACL_XATTR_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 (!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))
771 return -EOPNOTSUPP;
773 if (!S_ISREG(inode->i_mode) &&
774 (!S_ISDIR(inode->i_mode) || inode->i_mode &S_ISVTX))
775 return -EPERM;
777 return 0;
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;
786 int old_ea_size = 0;
787 int xattr_size;
788 int new_size;
789 int namelen = strlen(name);
790 char *os2name = NULL;
791 int found = 0;
792 int rc;
793 int length;
795 if (strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN) == 0) {
796 os2name = kmalloc(namelen - XATTR_OS2_PREFIX_LEN + 1,
797 GFP_KERNEL);
798 if (!os2name)
799 return -ENOMEM;
800 strcpy(os2name, name + XATTR_OS2_PREFIX_LEN);
801 name = os2name;
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) {
809 rc = xattr_size;
810 goto out;
813 again:
814 ealist = (struct jfs_ea_list *) ea_buf.xattr;
815 new_size = sizeof (struct jfs_ea_list);
817 if (xattr_size) {
818 for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist);
819 ea = NEXT_EA(ea)) {
820 if ((namelen == ea->namelen) &&
821 (memcmp(name, ea->name, namelen) == 0)) {
822 found = 1;
823 if (flags & XATTR_CREATE) {
824 rc = -EEXIST;
825 goto release;
827 old_ea = ea;
828 old_ea_size = EA_SIZE(ea);
829 next_ea = NEXT_EA(ea);
830 } else
831 new_size += EA_SIZE(ea);
835 if (!found) {
836 if (flags & XATTR_REPLACE) {
837 rc = -ENODATA;
838 goto release;
840 if (value == NULL) {
841 rc = 0;
842 goto release;
845 if (value)
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) {
856 rc = xattr_size;
857 goto out;
859 goto again;
862 /* Remove old ea of the same name */
863 if (found) {
864 /* number of bytes following target EA */
865 length = (char *) END_EALIST(ealist) - (char *) next_ea;
866 if (length > 0)
867 memmove(old_ea, next_ea, length);
868 xattr_size -= old_ea_size;
871 /* Add new entry to the end */
872 if (value) {
873 if (xattr_size == 0)
874 /* Completely new ea list */
875 xattr_size = sizeof (struct jfs_ea_list);
877 ea = (struct jfs_ea *) ((char *) ealist + xattr_size);
878 ea->flag = 0;
879 ea->namelen = namelen;
880 ea->valuelen = (cpu_to_le16(value_len));
881 memcpy(ea->name, name, namelen);
882 ea->name[namelen] = 0;
883 if (value_len)
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) {
890 printk(KERN_ERR
891 "jfs_xsetattr: xattr_size = %d, new_size = %d\n",
892 xattr_size, new_size);
894 rc = -EINVAL;
895 goto release;
899 * If we're left with an empty list, there's no ea
901 if (new_size == sizeof (struct jfs_ea_list))
902 new_size = 0;
904 ealist->size = cpu_to_le32(new_size);
906 rc = ea_put(tid, inode, &ea_buf, new_size);
908 goto out;
909 release:
910 ea_release(inode, &ea_buf);
911 out:
912 up_write(&JFS_IP(inode)->xattr_sem);
914 kfree(os2name);
916 return rc;
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);
924 int rc;
925 tid_t tid;
927 if ((rc = can_set_xattr(inode, name, value, value_len)))
928 return rc;
930 if (value == NULL) { /* empty EA, do not remove */
931 value = "";
932 value_len = 0;
935 tid = txBegin(inode->i_sb, 0);
936 down(&ji->commit_sem);
937 rc = __jfs_setxattr(tid, dentry->d_inode, name, value, value_len,
938 flags);
939 if (!rc)
940 rc = txCommit(tid, 1, &inode, 0);
941 txEnd(tid);
942 up(&ji->commit_sem);
944 return rc;
947 ssize_t __jfs_getxattr(struct inode *inode, const char *name, void *data,
948 size_t buf_size)
950 struct jfs_ea_list *ealist;
951 struct jfs_ea *ea;
952 struct ea_buffer ea_buf;
953 int xattr_size;
954 ssize_t size;
955 int namelen = strlen(name);
956 char *os2name = NULL;
957 char *value;
959 if (strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN) == 0) {
960 os2name = kmalloc(namelen - XATTR_OS2_PREFIX_LEN + 1,
961 GFP_KERNEL);
962 if (!os2name)
963 return -ENOMEM;
964 strcpy(os2name, name + XATTR_OS2_PREFIX_LEN);
965 name = os2name;
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) {
974 size = xattr_size;
975 goto out;
978 if (xattr_size == 0)
979 goto not_found;
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) {
987 /* Found it */
988 size = le16_to_cpu(ea->valuelen);
989 if (!data)
990 goto release;
991 else if (size > buf_size) {
992 size = -ERANGE;
993 goto release;
995 value = ((char *) &ea->name) + ea->namelen + 1;
996 memcpy(data, value, size);
997 goto release;
999 not_found:
1000 size = -ENODATA;
1001 release:
1002 ea_release(inode, &ea_buf);
1003 out:
1004 up_read(&JFS_IP(inode)->xattr_sem);
1006 kfree(os2name);
1008 return size;
1011 ssize_t jfs_getxattr(struct dentry *dentry, const char *name, void *data,
1012 size_t buf_size)
1014 int err;
1016 err = __jfs_getxattr(dentry->d_inode, name, data, buf_size);
1018 return err;
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;
1034 char *buffer;
1035 ssize_t size = 0;
1036 int xattr_size;
1037 struct jfs_ea_list *ealist;
1038 struct jfs_ea *ea;
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) {
1045 size = xattr_size;
1046 goto out;
1049 if (xattr_size == 0)
1050 goto release;
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)) {
1056 if (can_list(ea))
1057 size += name_size(ea) + 1;
1060 if (!data)
1061 goto release;
1063 if (size > buf_size) {
1064 size = -ERANGE;
1065 goto release;
1068 /* Copy attribute names to buffer */
1069 buffer = data;
1070 for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea)) {
1071 if (can_list(ea)) {
1072 int namelen = copy_name(buffer, ea);
1073 buffer += namelen + 1;
1077 release:
1078 ea_release(inode, &ea_buf);
1079 out:
1080 up_read(&JFS_IP(inode)->xattr_sem);
1081 return size;
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);
1088 int rc;
1089 tid_t tid;
1091 if ((rc = can_set_xattr(inode, name, NULL, 0)))
1092 return rc;
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);
1097 if (!rc)
1098 rc = txCommit(tid, 1, &inode, 0);
1099 txEnd(tid);
1100 up(&ji->commit_sem);
1102 return rc;
1105 #ifdef CONFIG_JFS_SECURITY
1106 int jfs_init_security(tid_t tid, struct inode *inode, struct inode *dir)
1108 int rc;
1109 size_t len;
1110 void *value;
1111 char *suffix;
1112 char *name;
1114 rc = security_inode_init_security(inode, dir, &suffix, &value, &len);
1115 if (rc) {
1116 if (rc == -EOPNOTSUPP)
1117 return 0;
1118 return rc;
1120 name = kmalloc(XATTR_SECURITY_PREFIX_LEN + 1 + strlen(suffix),
1121 GFP_NOFS);
1122 if (!name) {
1123 rc = -ENOMEM;
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);
1131 kfree(name);
1132 kmalloc_failed:
1133 kfree(suffix);
1134 kfree(value);
1136 return rc;
1138 #endif