staging: hv: Fix error checking in channel.c
[linux/fpc-iii.git] / fs / jfs / xattr.c
blobfa96bbb263434618e1c3bc050a2208ccbe3c8fc0
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.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <linux/capability.h>
21 #include <linux/fs.h>
22 #include <linux/xattr.h>
23 #include <linux/posix_acl_xattr.h>
24 #include <linux/slab.h>
25 #include <linux/quotaops.h>
26 #include <linux/security.h>
27 #include "jfs_incore.h"
28 #include "jfs_superblock.h"
29 #include "jfs_dmap.h"
30 #include "jfs_debug.h"
31 #include "jfs_dinode.h"
32 #include "jfs_extent.h"
33 #include "jfs_metapage.h"
34 #include "jfs_xattr.h"
35 #include "jfs_acl.h"
38 * jfs_xattr.c: extended attribute service
40 * Overall design --
42 * Format:
44 * Extended attribute lists (jfs_ea_list) consist of an overall size (32 bit
45 * value) and a variable (0 or more) number of extended attribute
46 * entries. Each extended attribute entry (jfs_ea) is a <name,value> double
47 * where <name> is constructed from a null-terminated ascii string
48 * (1 ... 255 bytes in the name) and <value> is arbitrary 8 bit data
49 * (1 ... 65535 bytes). The in-memory format is
51 * 0 1 2 4 4 + namelen + 1
52 * +-------+--------+--------+----------------+-------------------+
53 * | Flags | Name | Value | Name String \0 | Data . . . . |
54 * | | Length | Length | | |
55 * +-------+--------+--------+----------------+-------------------+
57 * A jfs_ea_list then is structured as
59 * 0 4 4 + EA_SIZE(ea1)
60 * +------------+-------------------+--------------------+-----
61 * | Overall EA | First FEA Element | Second FEA Element | .....
62 * | List Size | | |
63 * +------------+-------------------+--------------------+-----
65 * On-disk:
67 * FEALISTs are stored on disk using blocks allocated by dbAlloc() and
68 * written directly. An EA list may be in-lined in the inode if there is
69 * sufficient room available.
72 struct ea_buffer {
73 int flag; /* Indicates what storage xattr points to */
74 int max_size; /* largest xattr that fits in current buffer */
75 dxd_t new_ea; /* dxd to replace ea when modifying xattr */
76 struct metapage *mp; /* metapage containing ea list */
77 struct jfs_ea_list *xattr; /* buffer containing ea list */
81 * ea_buffer.flag values
83 #define EA_INLINE 0x0001
84 #define EA_EXTENT 0x0002
85 #define EA_NEW 0x0004
86 #define EA_MALLOC 0x0008
90 * These three routines are used to recognize on-disk extended attributes
91 * that are in a recognized namespace. If the attribute is not recognized,
92 * "os2." is prepended to the name
94 static inline int is_os2_xattr(struct jfs_ea *ea)
97 * Check for "system."
99 if ((ea->namelen >= XATTR_SYSTEM_PREFIX_LEN) &&
100 !strncmp(ea->name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
101 return false;
103 * Check for "user."
105 if ((ea->namelen >= XATTR_USER_PREFIX_LEN) &&
106 !strncmp(ea->name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
107 return false;
109 * Check for "security."
111 if ((ea->namelen >= XATTR_SECURITY_PREFIX_LEN) &&
112 !strncmp(ea->name, XATTR_SECURITY_PREFIX,
113 XATTR_SECURITY_PREFIX_LEN))
114 return false;
116 * Check for "trusted."
118 if ((ea->namelen >= XATTR_TRUSTED_PREFIX_LEN) &&
119 !strncmp(ea->name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN))
120 return false;
122 * Add any other valid namespace prefixes here
126 * We assume it's OS/2's flat namespace
128 return true;
131 static inline int name_size(struct jfs_ea *ea)
133 if (is_os2_xattr(ea))
134 return ea->namelen + XATTR_OS2_PREFIX_LEN;
135 else
136 return ea->namelen;
139 static inline int copy_name(char *buffer, struct jfs_ea *ea)
141 int len = ea->namelen;
143 if (is_os2_xattr(ea)) {
144 memcpy(buffer, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN);
145 buffer += XATTR_OS2_PREFIX_LEN;
146 len += XATTR_OS2_PREFIX_LEN;
148 memcpy(buffer, ea->name, ea->namelen);
149 buffer[ea->namelen] = 0;
151 return len;
154 /* Forward references */
155 static void ea_release(struct inode *inode, struct ea_buffer *ea_buf);
158 * NAME: ea_write_inline
160 * FUNCTION: Attempt to write an EA inline if area is available
162 * PRE CONDITIONS:
163 * Already verified that the specified EA is small enough to fit inline
165 * PARAMETERS:
166 * ip - Inode pointer
167 * ealist - EA list pointer
168 * size - size of ealist in bytes
169 * ea - dxd_t structure to be filled in with necessary EA information
170 * if we successfully copy the EA inline
172 * NOTES:
173 * Checks if the inode's inline area is available. If so, copies EA inline
174 * and sets <ea> fields appropriately. Otherwise, returns failure, EA will
175 * have to be put into an extent.
177 * RETURNS: 0 for successful copy to inline area; -1 if area not available
179 static int ea_write_inline(struct inode *ip, struct jfs_ea_list *ealist,
180 int size, dxd_t * ea)
182 struct jfs_inode_info *ji = JFS_IP(ip);
185 * Make sure we have an EA -- the NULL EA list is valid, but you
186 * can't copy it!
188 if (ealist && size > sizeof (struct jfs_ea_list)) {
189 assert(size <= sizeof (ji->i_inline_ea));
192 * See if the space is available or if it is already being
193 * used for an inline EA.
195 if (!(ji->mode2 & INLINEEA) && !(ji->ea.flag & DXD_INLINE))
196 return -EPERM;
198 DXDsize(ea, size);
199 DXDlength(ea, 0);
200 DXDaddress(ea, 0);
201 memcpy(ji->i_inline_ea, ealist, size);
202 ea->flag = DXD_INLINE;
203 ji->mode2 &= ~INLINEEA;
204 } else {
205 ea->flag = 0;
206 DXDsize(ea, 0);
207 DXDlength(ea, 0);
208 DXDaddress(ea, 0);
210 /* Free up INLINE area */
211 if (ji->ea.flag & DXD_INLINE)
212 ji->mode2 |= INLINEEA;
215 return 0;
219 * NAME: ea_write
221 * FUNCTION: Write an EA for an inode
223 * PRE CONDITIONS: EA has been verified
225 * PARAMETERS:
226 * ip - Inode pointer
227 * ealist - EA list pointer
228 * size - size of ealist in bytes
229 * ea - dxd_t structure to be filled in appropriately with where the
230 * EA was copied
232 * NOTES: Will write EA inline if able to, otherwise allocates blocks for an
233 * extent and synchronously writes it to those blocks.
235 * RETURNS: 0 for success; Anything else indicates failure
237 static int ea_write(struct inode *ip, struct jfs_ea_list *ealist, int size,
238 dxd_t * ea)
240 struct super_block *sb = ip->i_sb;
241 struct jfs_inode_info *ji = JFS_IP(ip);
242 struct jfs_sb_info *sbi = JFS_SBI(sb);
243 int nblocks;
244 s64 blkno;
245 int rc = 0, i;
246 char *cp;
247 s32 nbytes, nb;
248 s32 bytes_to_write;
249 struct metapage *mp;
252 * Quick check to see if this is an in-linable EA. Short EAs
253 * and empty EAs are all in-linable, provided the space exists.
255 if (!ealist || size <= sizeof (ji->i_inline_ea)) {
256 if (!ea_write_inline(ip, ealist, size, ea))
257 return 0;
260 /* figure out how many blocks we need */
261 nblocks = (size + (sb->s_blocksize - 1)) >> sb->s_blocksize_bits;
263 /* Allocate new blocks to quota. */
264 rc = dquot_alloc_block(ip, nblocks);
265 if (rc)
266 return rc;
268 rc = dbAlloc(ip, INOHINT(ip), nblocks, &blkno);
269 if (rc) {
270 /*Rollback quota allocation. */
271 dquot_free_block(ip, nblocks);
272 return rc;
276 * Now have nblocks worth of storage to stuff into the FEALIST.
277 * loop over the FEALIST copying data into the buffer one page at
278 * a time.
280 cp = (char *) ealist;
281 nbytes = size;
282 for (i = 0; i < nblocks; i += sbi->nbperpage) {
284 * Determine how many bytes for this request, and round up to
285 * the nearest aggregate block size
287 nb = min(PSIZE, nbytes);
288 bytes_to_write =
289 ((((nb + sb->s_blocksize - 1)) >> sb->s_blocksize_bits))
290 << sb->s_blocksize_bits;
292 if (!(mp = get_metapage(ip, blkno + i, bytes_to_write, 1))) {
293 rc = -EIO;
294 goto failed;
297 memcpy(mp->data, cp, nb);
300 * We really need a way to propagate errors for
301 * forced writes like this one. --hch
303 * (__write_metapage => release_metapage => flush_metapage)
305 #ifdef _JFS_FIXME
306 if ((rc = flush_metapage(mp))) {
308 * the write failed -- this means that the buffer
309 * is still assigned and the blocks are not being
310 * used. this seems like the best error recovery
311 * we can get ...
313 goto failed;
315 #else
316 flush_metapage(mp);
317 #endif
319 cp += PSIZE;
320 nbytes -= nb;
323 ea->flag = DXD_EXTENT;
324 DXDsize(ea, le32_to_cpu(ealist->size));
325 DXDlength(ea, nblocks);
326 DXDaddress(ea, blkno);
328 /* Free up INLINE area */
329 if (ji->ea.flag & DXD_INLINE)
330 ji->mode2 |= INLINEEA;
332 return 0;
334 failed:
335 /* Rollback quota allocation. */
336 dquot_free_block(ip, nblocks);
338 dbFree(ip, blkno, nblocks);
339 return rc;
343 * NAME: ea_read_inline
345 * FUNCTION: Read an inlined EA into user's buffer
347 * PARAMETERS:
348 * ip - Inode pointer
349 * ealist - Pointer to buffer to fill in with EA
351 * RETURNS: 0
353 static int ea_read_inline(struct inode *ip, struct jfs_ea_list *ealist)
355 struct jfs_inode_info *ji = JFS_IP(ip);
356 int ea_size = sizeDXD(&ji->ea);
358 if (ea_size == 0) {
359 ealist->size = 0;
360 return 0;
363 /* Sanity Check */
364 if ((sizeDXD(&ji->ea) > sizeof (ji->i_inline_ea)))
365 return -EIO;
366 if (le32_to_cpu(((struct jfs_ea_list *) &ji->i_inline_ea)->size)
367 != ea_size)
368 return -EIO;
370 memcpy(ealist, ji->i_inline_ea, ea_size);
371 return 0;
375 * NAME: ea_read
377 * FUNCTION: copy EA data into user's buffer
379 * PARAMETERS:
380 * ip - Inode pointer
381 * ealist - Pointer to buffer to fill in with EA
383 * NOTES: If EA is inline calls ea_read_inline() to copy EA.
385 * RETURNS: 0 for success; other indicates failure
387 static int ea_read(struct inode *ip, struct jfs_ea_list *ealist)
389 struct super_block *sb = ip->i_sb;
390 struct jfs_inode_info *ji = JFS_IP(ip);
391 struct jfs_sb_info *sbi = JFS_SBI(sb);
392 int nblocks;
393 s64 blkno;
394 char *cp = (char *) ealist;
395 int i;
396 int nbytes, nb;
397 s32 bytes_to_read;
398 struct metapage *mp;
400 /* quick check for in-line EA */
401 if (ji->ea.flag & DXD_INLINE)
402 return ea_read_inline(ip, ealist);
404 nbytes = sizeDXD(&ji->ea);
405 if (!nbytes) {
406 jfs_error(sb, "ea_read: nbytes is 0");
407 return -EIO;
411 * Figure out how many blocks were allocated when this EA list was
412 * originally written to disk.
414 nblocks = lengthDXD(&ji->ea) << sbi->l2nbperpage;
415 blkno = addressDXD(&ji->ea) << sbi->l2nbperpage;
418 * I have found the disk blocks which were originally used to store
419 * the FEALIST. now i loop over each contiguous block copying the
420 * data into the buffer.
422 for (i = 0; i < nblocks; i += sbi->nbperpage) {
424 * Determine how many bytes for this request, and round up to
425 * the nearest aggregate block size
427 nb = min(PSIZE, nbytes);
428 bytes_to_read =
429 ((((nb + sb->s_blocksize - 1)) >> sb->s_blocksize_bits))
430 << sb->s_blocksize_bits;
432 if (!(mp = read_metapage(ip, blkno + i, bytes_to_read, 1)))
433 return -EIO;
435 memcpy(cp, mp->data, nb);
436 release_metapage(mp);
438 cp += PSIZE;
439 nbytes -= nb;
442 return 0;
446 * NAME: ea_get
448 * FUNCTION: Returns buffer containing existing extended attributes.
449 * The size of the buffer will be the larger of the existing
450 * attributes size, or min_size.
452 * The buffer, which may be inlined in the inode or in the
453 * page cache must be release by calling ea_release or ea_put
455 * PARAMETERS:
456 * inode - Inode pointer
457 * ea_buf - Structure to be populated with ealist and its metadata
458 * min_size- minimum size of buffer to be returned
460 * RETURNS: 0 for success; Other indicates failure
462 static int ea_get(struct inode *inode, struct ea_buffer *ea_buf, int min_size)
464 struct jfs_inode_info *ji = JFS_IP(inode);
465 struct super_block *sb = inode->i_sb;
466 int size;
467 int ea_size = sizeDXD(&ji->ea);
468 int blocks_needed, current_blocks;
469 s64 blkno;
470 int rc;
471 int quota_allocation = 0;
473 /* When fsck.jfs clears a bad ea, it doesn't clear the size */
474 if (ji->ea.flag == 0)
475 ea_size = 0;
477 if (ea_size == 0) {
478 if (min_size == 0) {
479 ea_buf->flag = 0;
480 ea_buf->max_size = 0;
481 ea_buf->xattr = NULL;
482 return 0;
484 if ((min_size <= sizeof (ji->i_inline_ea)) &&
485 (ji->mode2 & INLINEEA)) {
486 ea_buf->flag = EA_INLINE | EA_NEW;
487 ea_buf->max_size = sizeof (ji->i_inline_ea);
488 ea_buf->xattr = (struct jfs_ea_list *) ji->i_inline_ea;
489 DXDlength(&ea_buf->new_ea, 0);
490 DXDaddress(&ea_buf->new_ea, 0);
491 ea_buf->new_ea.flag = DXD_INLINE;
492 DXDsize(&ea_buf->new_ea, min_size);
493 return 0;
495 current_blocks = 0;
496 } else if (ji->ea.flag & DXD_INLINE) {
497 if (min_size <= sizeof (ji->i_inline_ea)) {
498 ea_buf->flag = EA_INLINE;
499 ea_buf->max_size = sizeof (ji->i_inline_ea);
500 ea_buf->xattr = (struct jfs_ea_list *) ji->i_inline_ea;
501 goto size_check;
503 current_blocks = 0;
504 } else {
505 if (!(ji->ea.flag & DXD_EXTENT)) {
506 jfs_error(sb, "ea_get: invalid ea.flag)");
507 return -EIO;
509 current_blocks = (ea_size + sb->s_blocksize - 1) >>
510 sb->s_blocksize_bits;
512 size = max(min_size, ea_size);
514 if (size > PSIZE) {
516 * To keep the rest of the code simple. Allocate a
517 * contiguous buffer to work with
519 ea_buf->xattr = kmalloc(size, GFP_KERNEL);
520 if (ea_buf->xattr == NULL)
521 return -ENOMEM;
523 ea_buf->flag = EA_MALLOC;
524 ea_buf->max_size = (size + sb->s_blocksize - 1) &
525 ~(sb->s_blocksize - 1);
527 if (ea_size == 0)
528 return 0;
530 if ((rc = ea_read(inode, ea_buf->xattr))) {
531 kfree(ea_buf->xattr);
532 ea_buf->xattr = NULL;
533 return rc;
535 goto size_check;
537 blocks_needed = (min_size + sb->s_blocksize - 1) >>
538 sb->s_blocksize_bits;
540 if (blocks_needed > current_blocks) {
541 /* Allocate new blocks to quota. */
542 rc = dquot_alloc_block(inode, blocks_needed);
543 if (rc)
544 return -EDQUOT;
546 quota_allocation = blocks_needed;
548 rc = dbAlloc(inode, INOHINT(inode), (s64) blocks_needed,
549 &blkno);
550 if (rc)
551 goto clean_up;
553 DXDlength(&ea_buf->new_ea, blocks_needed);
554 DXDaddress(&ea_buf->new_ea, blkno);
555 ea_buf->new_ea.flag = DXD_EXTENT;
556 DXDsize(&ea_buf->new_ea, min_size);
558 ea_buf->flag = EA_EXTENT | EA_NEW;
560 ea_buf->mp = get_metapage(inode, blkno,
561 blocks_needed << sb->s_blocksize_bits,
563 if (ea_buf->mp == NULL) {
564 dbFree(inode, blkno, (s64) blocks_needed);
565 rc = -EIO;
566 goto clean_up;
568 ea_buf->xattr = ea_buf->mp->data;
569 ea_buf->max_size = (min_size + sb->s_blocksize - 1) &
570 ~(sb->s_blocksize - 1);
571 if (ea_size == 0)
572 return 0;
573 if ((rc = ea_read(inode, ea_buf->xattr))) {
574 discard_metapage(ea_buf->mp);
575 dbFree(inode, blkno, (s64) blocks_needed);
576 goto clean_up;
578 goto size_check;
580 ea_buf->flag = EA_EXTENT;
581 ea_buf->mp = read_metapage(inode, addressDXD(&ji->ea),
582 lengthDXD(&ji->ea) << sb->s_blocksize_bits,
584 if (ea_buf->mp == NULL) {
585 rc = -EIO;
586 goto clean_up;
588 ea_buf->xattr = ea_buf->mp->data;
589 ea_buf->max_size = (ea_size + sb->s_blocksize - 1) &
590 ~(sb->s_blocksize - 1);
592 size_check:
593 if (EALIST_SIZE(ea_buf->xattr) != ea_size) {
594 printk(KERN_ERR "ea_get: invalid extended attribute\n");
595 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_ADDRESS, 16, 1,
596 ea_buf->xattr, ea_size, 1);
597 ea_release(inode, ea_buf);
598 rc = -EIO;
599 goto clean_up;
602 return ea_size;
604 clean_up:
605 /* Rollback quota allocation */
606 if (quota_allocation)
607 dquot_free_block(inode, quota_allocation);
609 return (rc);
612 static void ea_release(struct inode *inode, struct ea_buffer *ea_buf)
614 if (ea_buf->flag & EA_MALLOC)
615 kfree(ea_buf->xattr);
616 else if (ea_buf->flag & EA_EXTENT) {
617 assert(ea_buf->mp);
618 release_metapage(ea_buf->mp);
620 if (ea_buf->flag & EA_NEW)
621 dbFree(inode, addressDXD(&ea_buf->new_ea),
622 lengthDXD(&ea_buf->new_ea));
626 static int ea_put(tid_t tid, struct inode *inode, struct ea_buffer *ea_buf,
627 int new_size)
629 struct jfs_inode_info *ji = JFS_IP(inode);
630 unsigned long old_blocks, new_blocks;
631 int rc = 0;
633 if (new_size == 0) {
634 ea_release(inode, ea_buf);
635 ea_buf = NULL;
636 } else if (ea_buf->flag & EA_INLINE) {
637 assert(new_size <= sizeof (ji->i_inline_ea));
638 ji->mode2 &= ~INLINEEA;
639 ea_buf->new_ea.flag = DXD_INLINE;
640 DXDsize(&ea_buf->new_ea, new_size);
641 DXDaddress(&ea_buf->new_ea, 0);
642 DXDlength(&ea_buf->new_ea, 0);
643 } else if (ea_buf->flag & EA_MALLOC) {
644 rc = ea_write(inode, ea_buf->xattr, new_size, &ea_buf->new_ea);
645 kfree(ea_buf->xattr);
646 } else if (ea_buf->flag & EA_NEW) {
647 /* We have already allocated a new dxd */
648 flush_metapage(ea_buf->mp);
649 } else {
650 /* ->xattr must point to original ea's metapage */
651 rc = ea_write(inode, ea_buf->xattr, new_size, &ea_buf->new_ea);
652 discard_metapage(ea_buf->mp);
654 if (rc)
655 return rc;
657 old_blocks = new_blocks = 0;
659 if (ji->ea.flag & DXD_EXTENT) {
660 invalidate_dxd_metapages(inode, ji->ea);
661 old_blocks = lengthDXD(&ji->ea);
664 if (ea_buf) {
665 txEA(tid, inode, &ji->ea, &ea_buf->new_ea);
666 if (ea_buf->new_ea.flag & DXD_EXTENT) {
667 new_blocks = lengthDXD(&ea_buf->new_ea);
668 if (ji->ea.flag & DXD_INLINE)
669 ji->mode2 |= INLINEEA;
671 ji->ea = ea_buf->new_ea;
672 } else {
673 txEA(tid, inode, &ji->ea, NULL);
674 if (ji->ea.flag & DXD_INLINE)
675 ji->mode2 |= INLINEEA;
676 ji->ea.flag = 0;
677 ji->ea.size = 0;
680 /* If old blocks exist, they must be removed from quota allocation. */
681 if (old_blocks)
682 dquot_free_block(inode, old_blocks);
684 inode->i_ctime = CURRENT_TIME;
686 return 0;
690 * can_set_system_xattr
692 * This code is specific to the system.* namespace. It contains policy
693 * which doesn't belong in the main xattr codepath.
695 static int can_set_system_xattr(struct inode *inode, const char *name,
696 const void *value, size_t value_len)
698 #ifdef CONFIG_JFS_POSIX_ACL
699 struct posix_acl *acl;
700 int rc;
702 if (!is_owner_or_cap(inode))
703 return -EPERM;
706 * POSIX_ACL_XATTR_ACCESS is tied to i_mode
708 if (strcmp(name, POSIX_ACL_XATTR_ACCESS) == 0) {
709 acl = posix_acl_from_xattr(value, value_len);
710 if (IS_ERR(acl)) {
711 rc = PTR_ERR(acl);
712 printk(KERN_ERR "posix_acl_from_xattr returned %d\n",
713 rc);
714 return rc;
716 if (acl) {
717 mode_t mode = inode->i_mode;
718 rc = posix_acl_equiv_mode(acl, &mode);
719 posix_acl_release(acl);
720 if (rc < 0) {
721 printk(KERN_ERR
722 "posix_acl_equiv_mode returned %d\n",
723 rc);
724 return rc;
726 inode->i_mode = mode;
727 mark_inode_dirty(inode);
730 * We're changing the ACL. Get rid of the cached one
732 forget_cached_acl(inode, ACL_TYPE_ACCESS);
734 return 0;
735 } else if (strcmp(name, POSIX_ACL_XATTR_DEFAULT) == 0) {
736 acl = posix_acl_from_xattr(value, value_len);
737 if (IS_ERR(acl)) {
738 rc = PTR_ERR(acl);
739 printk(KERN_ERR "posix_acl_from_xattr returned %d\n",
740 rc);
741 return rc;
743 posix_acl_release(acl);
746 * We're changing the default ACL. Get rid of the cached one
748 forget_cached_acl(inode, ACL_TYPE_DEFAULT);
750 return 0;
752 #endif /* CONFIG_JFS_POSIX_ACL */
753 return -EOPNOTSUPP;
757 * Most of the permission checking is done by xattr_permission in the vfs.
758 * The local file system is responsible for handling the system.* namespace.
759 * We also need to verify that this is a namespace that we recognize.
761 static int can_set_xattr(struct inode *inode, const char *name,
762 const void *value, size_t value_len)
764 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
765 return can_set_system_xattr(inode, name, value, value_len);
768 * Don't allow setting an attribute in an unknown namespace.
770 if (strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) &&
771 strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) &&
772 strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN) &&
773 strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN))
774 return -EOPNOTSUPP;
776 return 0;
779 int __jfs_setxattr(tid_t tid, struct inode *inode, const char *name,
780 const void *value, size_t value_len, int flags)
782 struct jfs_ea_list *ealist;
783 struct jfs_ea *ea, *old_ea = NULL, *next_ea = NULL;
784 struct ea_buffer ea_buf;
785 int old_ea_size = 0;
786 int xattr_size;
787 int new_size;
788 int namelen = strlen(name);
789 char *os2name = NULL;
790 int found = 0;
791 int rc;
792 int length;
794 if (strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN) == 0) {
795 os2name = kmalloc(namelen - XATTR_OS2_PREFIX_LEN + 1,
796 GFP_KERNEL);
797 if (!os2name)
798 return -ENOMEM;
799 strcpy(os2name, name + XATTR_OS2_PREFIX_LEN);
800 name = os2name;
801 namelen -= XATTR_OS2_PREFIX_LEN;
804 down_write(&JFS_IP(inode)->xattr_sem);
806 xattr_size = ea_get(inode, &ea_buf, 0);
807 if (xattr_size < 0) {
808 rc = xattr_size;
809 goto out;
812 again:
813 ealist = (struct jfs_ea_list *) ea_buf.xattr;
814 new_size = sizeof (struct jfs_ea_list);
816 if (xattr_size) {
817 for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist);
818 ea = NEXT_EA(ea)) {
819 if ((namelen == ea->namelen) &&
820 (memcmp(name, ea->name, namelen) == 0)) {
821 found = 1;
822 if (flags & XATTR_CREATE) {
823 rc = -EEXIST;
824 goto release;
826 old_ea = ea;
827 old_ea_size = EA_SIZE(ea);
828 next_ea = NEXT_EA(ea);
829 } else
830 new_size += EA_SIZE(ea);
834 if (!found) {
835 if (flags & XATTR_REPLACE) {
836 rc = -ENODATA;
837 goto release;
839 if (value == NULL) {
840 rc = 0;
841 goto release;
844 if (value)
845 new_size += sizeof (struct jfs_ea) + namelen + 1 + value_len;
847 if (new_size > ea_buf.max_size) {
849 * We need to allocate more space for merged ea list.
850 * We should only have loop to again: once.
852 ea_release(inode, &ea_buf);
853 xattr_size = ea_get(inode, &ea_buf, new_size);
854 if (xattr_size < 0) {
855 rc = xattr_size;
856 goto out;
858 goto again;
861 /* Remove old ea of the same name */
862 if (found) {
863 /* number of bytes following target EA */
864 length = (char *) END_EALIST(ealist) - (char *) next_ea;
865 if (length > 0)
866 memmove(old_ea, next_ea, length);
867 xattr_size -= old_ea_size;
870 /* Add new entry to the end */
871 if (value) {
872 if (xattr_size == 0)
873 /* Completely new ea list */
874 xattr_size = sizeof (struct jfs_ea_list);
876 ea = (struct jfs_ea *) ((char *) ealist + xattr_size);
877 ea->flag = 0;
878 ea->namelen = namelen;
879 ea->valuelen = (cpu_to_le16(value_len));
880 memcpy(ea->name, name, namelen);
881 ea->name[namelen] = 0;
882 if (value_len)
883 memcpy(&ea->name[namelen + 1], value, value_len);
884 xattr_size += EA_SIZE(ea);
887 /* DEBUG - If we did this right, these number match */
888 if (xattr_size != new_size) {
889 printk(KERN_ERR
890 "jfs_xsetattr: xattr_size = %d, new_size = %d\n",
891 xattr_size, new_size);
893 rc = -EINVAL;
894 goto release;
898 * If we're left with an empty list, there's no ea
900 if (new_size == sizeof (struct jfs_ea_list))
901 new_size = 0;
903 ealist->size = cpu_to_le32(new_size);
905 rc = ea_put(tid, inode, &ea_buf, new_size);
907 goto out;
908 release:
909 ea_release(inode, &ea_buf);
910 out:
911 up_write(&JFS_IP(inode)->xattr_sem);
913 kfree(os2name);
915 return rc;
918 int jfs_setxattr(struct dentry *dentry, const char *name, const void *value,
919 size_t value_len, int flags)
921 struct inode *inode = dentry->d_inode;
922 struct jfs_inode_info *ji = JFS_IP(inode);
923 int rc;
924 tid_t tid;
926 if ((rc = can_set_xattr(inode, name, value, value_len)))
927 return rc;
929 if (value == NULL) { /* empty EA, do not remove */
930 value = "";
931 value_len = 0;
934 tid = txBegin(inode->i_sb, 0);
935 mutex_lock(&ji->commit_mutex);
936 rc = __jfs_setxattr(tid, dentry->d_inode, name, value, value_len,
937 flags);
938 if (!rc)
939 rc = txCommit(tid, 1, &inode, 0);
940 txEnd(tid);
941 mutex_unlock(&ji->commit_mutex);
943 return rc;
946 ssize_t __jfs_getxattr(struct inode *inode, const char *name, void *data,
947 size_t buf_size)
949 struct jfs_ea_list *ealist;
950 struct jfs_ea *ea;
951 struct ea_buffer ea_buf;
952 int xattr_size;
953 ssize_t size;
954 int namelen = strlen(name);
955 char *os2name = NULL;
956 char *value;
958 if (strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN) == 0) {
959 os2name = kmalloc(namelen - XATTR_OS2_PREFIX_LEN + 1,
960 GFP_KERNEL);
961 if (!os2name)
962 return -ENOMEM;
963 strcpy(os2name, name + XATTR_OS2_PREFIX_LEN);
964 name = os2name;
965 namelen -= XATTR_OS2_PREFIX_LEN;
968 down_read(&JFS_IP(inode)->xattr_sem);
970 xattr_size = ea_get(inode, &ea_buf, 0);
972 if (xattr_size < 0) {
973 size = xattr_size;
974 goto out;
977 if (xattr_size == 0)
978 goto not_found;
980 ealist = (struct jfs_ea_list *) ea_buf.xattr;
982 /* Find the named attribute */
983 for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea))
984 if ((namelen == ea->namelen) &&
985 memcmp(name, ea->name, namelen) == 0) {
986 /* Found it */
987 size = le16_to_cpu(ea->valuelen);
988 if (!data)
989 goto release;
990 else if (size > buf_size) {
991 size = -ERANGE;
992 goto release;
994 value = ((char *) &ea->name) + ea->namelen + 1;
995 memcpy(data, value, size);
996 goto release;
998 not_found:
999 size = -ENODATA;
1000 release:
1001 ea_release(inode, &ea_buf);
1002 out:
1003 up_read(&JFS_IP(inode)->xattr_sem);
1005 kfree(os2name);
1007 return size;
1010 ssize_t jfs_getxattr(struct dentry *dentry, const char *name, void *data,
1011 size_t buf_size)
1013 int err;
1015 err = __jfs_getxattr(dentry->d_inode, name, data, buf_size);
1017 return err;
1021 * No special permissions are needed to list attributes except for trusted.*
1023 static inline int can_list(struct jfs_ea *ea)
1025 return (strncmp(ea->name, XATTR_TRUSTED_PREFIX,
1026 XATTR_TRUSTED_PREFIX_LEN) ||
1027 capable(CAP_SYS_ADMIN));
1030 ssize_t jfs_listxattr(struct dentry * dentry, char *data, size_t buf_size)
1032 struct inode *inode = dentry->d_inode;
1033 char *buffer;
1034 ssize_t size = 0;
1035 int xattr_size;
1036 struct jfs_ea_list *ealist;
1037 struct jfs_ea *ea;
1038 struct ea_buffer ea_buf;
1040 down_read(&JFS_IP(inode)->xattr_sem);
1042 xattr_size = ea_get(inode, &ea_buf, 0);
1043 if (xattr_size < 0) {
1044 size = xattr_size;
1045 goto out;
1048 if (xattr_size == 0)
1049 goto release;
1051 ealist = (struct jfs_ea_list *) ea_buf.xattr;
1053 /* compute required size of list */
1054 for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea)) {
1055 if (can_list(ea))
1056 size += name_size(ea) + 1;
1059 if (!data)
1060 goto release;
1062 if (size > buf_size) {
1063 size = -ERANGE;
1064 goto release;
1067 /* Copy attribute names to buffer */
1068 buffer = data;
1069 for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea)) {
1070 if (can_list(ea)) {
1071 int namelen = copy_name(buffer, ea);
1072 buffer += namelen + 1;
1076 release:
1077 ea_release(inode, &ea_buf);
1078 out:
1079 up_read(&JFS_IP(inode)->xattr_sem);
1080 return size;
1083 int jfs_removexattr(struct dentry *dentry, const char *name)
1085 struct inode *inode = dentry->d_inode;
1086 struct jfs_inode_info *ji = JFS_IP(inode);
1087 int rc;
1088 tid_t tid;
1090 if ((rc = can_set_xattr(inode, name, NULL, 0)))
1091 return rc;
1093 tid = txBegin(inode->i_sb, 0);
1094 mutex_lock(&ji->commit_mutex);
1095 rc = __jfs_setxattr(tid, dentry->d_inode, name, NULL, 0, XATTR_REPLACE);
1096 if (!rc)
1097 rc = txCommit(tid, 1, &inode, 0);
1098 txEnd(tid);
1099 mutex_unlock(&ji->commit_mutex);
1101 return rc;
1104 #ifdef CONFIG_JFS_SECURITY
1105 int jfs_init_security(tid_t tid, struct inode *inode, struct inode *dir)
1107 int rc;
1108 size_t len;
1109 void *value;
1110 char *suffix;
1111 char *name;
1113 rc = security_inode_init_security(inode, dir, &suffix, &value, &len);
1114 if (rc) {
1115 if (rc == -EOPNOTSUPP)
1116 return 0;
1117 return rc;
1119 name = kmalloc(XATTR_SECURITY_PREFIX_LEN + 1 + strlen(suffix),
1120 GFP_NOFS);
1121 if (!name) {
1122 rc = -ENOMEM;
1123 goto kmalloc_failed;
1125 strcpy(name, XATTR_SECURITY_PREFIX);
1126 strcpy(name + XATTR_SECURITY_PREFIX_LEN, suffix);
1128 rc = __jfs_setxattr(tid, inode, name, value, len, 0);
1130 kfree(name);
1131 kmalloc_failed:
1132 kfree(suffix);
1133 kfree(value);
1135 return rc;
1137 #endif