deal with races in /proc/*/{syscall,stack,personality}
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / jfs / xattr.c
blob8b0da9b108021a280d895ec5b26c860dd9a52351
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/quotaops.h>
25 #include <linux/security.h>
26 #include "jfs_incore.h"
27 #include "jfs_superblock.h"
28 #include "jfs_dmap.h"
29 #include "jfs_debug.h"
30 #include "jfs_dinode.h"
31 #include "jfs_extent.h"
32 #include "jfs_metapage.h"
33 #include "jfs_xattr.h"
34 #include "jfs_acl.h"
37 * jfs_xattr.c: extended attribute service
39 * Overall design --
41 * Format:
43 * Extended attribute lists (jfs_ea_list) consist of an overall size (32 bit
44 * value) and a variable (0 or more) number of extended attribute
45 * entries. Each extended attribute entry (jfs_ea) is a <name,value> double
46 * where <name> is constructed from a null-terminated ascii string
47 * (1 ... 255 bytes in the name) and <value> is arbitrary 8 bit data
48 * (1 ... 65535 bytes). The in-memory format is
50 * 0 1 2 4 4 + namelen + 1
51 * +-------+--------+--------+----------------+-------------------+
52 * | Flags | Name | Value | Name String \0 | Data . . . . |
53 * | | Length | Length | | |
54 * +-------+--------+--------+----------------+-------------------+
56 * A jfs_ea_list then is structured as
58 * 0 4 4 + EA_SIZE(ea1)
59 * +------------+-------------------+--------------------+-----
60 * | Overall EA | First FEA Element | Second FEA Element | .....
61 * | List Size | | |
62 * +------------+-------------------+--------------------+-----
64 * On-disk:
66 * FEALISTs are stored on disk using blocks allocated by dbAlloc() and
67 * written directly. An EA list may be in-lined in the inode if there is
68 * sufficient room available.
71 struct ea_buffer {
72 int flag; /* Indicates what storage xattr points to */
73 int max_size; /* largest xattr that fits in current buffer */
74 dxd_t new_ea; /* dxd to replace ea when modifying xattr */
75 struct metapage *mp; /* metapage containing ea list */
76 struct jfs_ea_list *xattr; /* buffer containing ea list */
80 * ea_buffer.flag values
82 #define EA_INLINE 0x0001
83 #define EA_EXTENT 0x0002
84 #define EA_NEW 0x0004
85 #define EA_MALLOC 0x0008
88 static int is_known_namespace(const char *name)
90 if (strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN) &&
91 strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN) &&
92 strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) &&
93 strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN))
94 return false;
96 return true;
100 * These three routines are used to recognize on-disk extended attributes
101 * that are in a recognized namespace. If the attribute is not recognized,
102 * "os2." is prepended to the name
104 static int is_os2_xattr(struct jfs_ea *ea)
106 return !is_known_namespace(ea->name);
109 static inline int name_size(struct jfs_ea *ea)
111 if (is_os2_xattr(ea))
112 return ea->namelen + XATTR_OS2_PREFIX_LEN;
113 else
114 return ea->namelen;
117 static inline int copy_name(char *buffer, struct jfs_ea *ea)
119 int len = ea->namelen;
121 if (is_os2_xattr(ea)) {
122 memcpy(buffer, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN);
123 buffer += XATTR_OS2_PREFIX_LEN;
124 len += XATTR_OS2_PREFIX_LEN;
126 memcpy(buffer, ea->name, ea->namelen);
127 buffer[ea->namelen] = 0;
129 return len;
132 /* Forward references */
133 static void ea_release(struct inode *inode, struct ea_buffer *ea_buf);
136 * NAME: ea_write_inline
138 * FUNCTION: Attempt to write an EA inline if area is available
140 * PRE CONDITIONS:
141 * Already verified that the specified EA is small enough to fit inline
143 * PARAMETERS:
144 * ip - Inode pointer
145 * ealist - EA list pointer
146 * size - size of ealist in bytes
147 * ea - dxd_t structure to be filled in with necessary EA information
148 * if we successfully copy the EA inline
150 * NOTES:
151 * Checks if the inode's inline area is available. If so, copies EA inline
152 * and sets <ea> fields appropriately. Otherwise, returns failure, EA will
153 * have to be put into an extent.
155 * RETURNS: 0 for successful copy to inline area; -1 if area not available
157 static int ea_write_inline(struct inode *ip, struct jfs_ea_list *ealist,
158 int size, dxd_t * ea)
160 struct jfs_inode_info *ji = JFS_IP(ip);
163 * Make sure we have an EA -- the NULL EA list is valid, but you
164 * can't copy it!
166 if (ealist && size > sizeof (struct jfs_ea_list)) {
167 assert(size <= sizeof (ji->i_inline_ea));
170 * See if the space is available or if it is already being
171 * used for an inline EA.
173 if (!(ji->mode2 & INLINEEA) && !(ji->ea.flag & DXD_INLINE))
174 return -EPERM;
176 DXDsize(ea, size);
177 DXDlength(ea, 0);
178 DXDaddress(ea, 0);
179 memcpy(ji->i_inline_ea, ealist, size);
180 ea->flag = DXD_INLINE;
181 ji->mode2 &= ~INLINEEA;
182 } else {
183 ea->flag = 0;
184 DXDsize(ea, 0);
185 DXDlength(ea, 0);
186 DXDaddress(ea, 0);
188 /* Free up INLINE area */
189 if (ji->ea.flag & DXD_INLINE)
190 ji->mode2 |= INLINEEA;
193 return 0;
197 * NAME: ea_write
199 * FUNCTION: Write an EA for an inode
201 * PRE CONDITIONS: EA has been verified
203 * PARAMETERS:
204 * ip - Inode pointer
205 * ealist - EA list pointer
206 * size - size of ealist in bytes
207 * ea - dxd_t structure to be filled in appropriately with where the
208 * EA was copied
210 * NOTES: Will write EA inline if able to, otherwise allocates blocks for an
211 * extent and synchronously writes it to those blocks.
213 * RETURNS: 0 for success; Anything else indicates failure
215 static int ea_write(struct inode *ip, struct jfs_ea_list *ealist, int size,
216 dxd_t * ea)
218 struct super_block *sb = ip->i_sb;
219 struct jfs_inode_info *ji = JFS_IP(ip);
220 struct jfs_sb_info *sbi = JFS_SBI(sb);
221 int nblocks;
222 s64 blkno;
223 int rc = 0, i;
224 char *cp;
225 s32 nbytes, nb;
226 s32 bytes_to_write;
227 struct metapage *mp;
230 * Quick check to see if this is an in-linable EA. Short EAs
231 * and empty EAs are all in-linable, provided the space exists.
233 if (!ealist || size <= sizeof (ji->i_inline_ea)) {
234 if (!ea_write_inline(ip, ealist, size, ea))
235 return 0;
238 /* figure out how many blocks we need */
239 nblocks = (size + (sb->s_blocksize - 1)) >> sb->s_blocksize_bits;
241 /* Allocate new blocks to quota. */
242 if (vfs_dq_alloc_block(ip, nblocks)) {
243 return -EDQUOT;
246 rc = dbAlloc(ip, INOHINT(ip), nblocks, &blkno);
247 if (rc) {
248 /*Rollback quota allocation. */
249 vfs_dq_free_block(ip, nblocks);
250 return rc;
254 * Now have nblocks worth of storage to stuff into the FEALIST.
255 * loop over the FEALIST copying data into the buffer one page at
256 * a time.
258 cp = (char *) ealist;
259 nbytes = size;
260 for (i = 0; i < nblocks; i += sbi->nbperpage) {
262 * Determine how many bytes for this request, and round up to
263 * the nearest aggregate block size
265 nb = min(PSIZE, nbytes);
266 bytes_to_write =
267 ((((nb + sb->s_blocksize - 1)) >> sb->s_blocksize_bits))
268 << sb->s_blocksize_bits;
270 if (!(mp = get_metapage(ip, blkno + i, bytes_to_write, 1))) {
271 rc = -EIO;
272 goto failed;
275 memcpy(mp->data, cp, nb);
278 * We really need a way to propagate errors for
279 * forced writes like this one. --hch
281 * (__write_metapage => release_metapage => flush_metapage)
283 #ifdef _JFS_FIXME
284 if ((rc = flush_metapage(mp))) {
286 * the write failed -- this means that the buffer
287 * is still assigned and the blocks are not being
288 * used. this seems like the best error recovery
289 * we can get ...
291 goto failed;
293 #else
294 flush_metapage(mp);
295 #endif
297 cp += PSIZE;
298 nbytes -= nb;
301 ea->flag = DXD_EXTENT;
302 DXDsize(ea, le32_to_cpu(ealist->size));
303 DXDlength(ea, nblocks);
304 DXDaddress(ea, blkno);
306 /* Free up INLINE area */
307 if (ji->ea.flag & DXD_INLINE)
308 ji->mode2 |= INLINEEA;
310 return 0;
312 failed:
313 /* Rollback quota allocation. */
314 vfs_dq_free_block(ip, nblocks);
316 dbFree(ip, blkno, nblocks);
317 return rc;
321 * NAME: ea_read_inline
323 * FUNCTION: Read an inlined EA into user's buffer
325 * PARAMETERS:
326 * ip - Inode pointer
327 * ealist - Pointer to buffer to fill in with EA
329 * RETURNS: 0
331 static int ea_read_inline(struct inode *ip, struct jfs_ea_list *ealist)
333 struct jfs_inode_info *ji = JFS_IP(ip);
334 int ea_size = sizeDXD(&ji->ea);
336 if (ea_size == 0) {
337 ealist->size = 0;
338 return 0;
341 /* Sanity Check */
342 if ((sizeDXD(&ji->ea) > sizeof (ji->i_inline_ea)))
343 return -EIO;
344 if (le32_to_cpu(((struct jfs_ea_list *) &ji->i_inline_ea)->size)
345 != ea_size)
346 return -EIO;
348 memcpy(ealist, ji->i_inline_ea, ea_size);
349 return 0;
353 * NAME: ea_read
355 * FUNCTION: copy EA data into user's buffer
357 * PARAMETERS:
358 * ip - Inode pointer
359 * ealist - Pointer to buffer to fill in with EA
361 * NOTES: If EA is inline calls ea_read_inline() to copy EA.
363 * RETURNS: 0 for success; other indicates failure
365 static int ea_read(struct inode *ip, struct jfs_ea_list *ealist)
367 struct super_block *sb = ip->i_sb;
368 struct jfs_inode_info *ji = JFS_IP(ip);
369 struct jfs_sb_info *sbi = JFS_SBI(sb);
370 int nblocks;
371 s64 blkno;
372 char *cp = (char *) ealist;
373 int i;
374 int nbytes, nb;
375 s32 bytes_to_read;
376 struct metapage *mp;
378 /* quick check for in-line EA */
379 if (ji->ea.flag & DXD_INLINE)
380 return ea_read_inline(ip, ealist);
382 nbytes = sizeDXD(&ji->ea);
383 if (!nbytes) {
384 jfs_error(sb, "ea_read: nbytes is 0");
385 return -EIO;
389 * Figure out how many blocks were allocated when this EA list was
390 * originally written to disk.
392 nblocks = lengthDXD(&ji->ea) << sbi->l2nbperpage;
393 blkno = addressDXD(&ji->ea) << sbi->l2nbperpage;
396 * I have found the disk blocks which were originally used to store
397 * the FEALIST. now i loop over each contiguous block copying the
398 * data into the buffer.
400 for (i = 0; i < nblocks; i += sbi->nbperpage) {
402 * Determine how many bytes for this request, and round up to
403 * the nearest aggregate block size
405 nb = min(PSIZE, nbytes);
406 bytes_to_read =
407 ((((nb + sb->s_blocksize - 1)) >> sb->s_blocksize_bits))
408 << sb->s_blocksize_bits;
410 if (!(mp = read_metapage(ip, blkno + i, bytes_to_read, 1)))
411 return -EIO;
413 memcpy(cp, mp->data, nb);
414 release_metapage(mp);
416 cp += PSIZE;
417 nbytes -= nb;
420 return 0;
424 * NAME: ea_get
426 * FUNCTION: Returns buffer containing existing extended attributes.
427 * The size of the buffer will be the larger of the existing
428 * attributes size, or min_size.
430 * The buffer, which may be inlined in the inode or in the
431 * page cache must be release by calling ea_release or ea_put
433 * PARAMETERS:
434 * inode - Inode pointer
435 * ea_buf - Structure to be populated with ealist and its metadata
436 * min_size- minimum size of buffer to be returned
438 * RETURNS: 0 for success; Other indicates failure
440 static int ea_get(struct inode *inode, struct ea_buffer *ea_buf, int min_size)
442 struct jfs_inode_info *ji = JFS_IP(inode);
443 struct super_block *sb = inode->i_sb;
444 int size;
445 int ea_size = sizeDXD(&ji->ea);
446 int blocks_needed, current_blocks;
447 s64 blkno;
448 int rc;
449 int quota_allocation = 0;
451 /* When fsck.jfs clears a bad ea, it doesn't clear the size */
452 if (ji->ea.flag == 0)
453 ea_size = 0;
455 if (ea_size == 0) {
456 if (min_size == 0) {
457 ea_buf->flag = 0;
458 ea_buf->max_size = 0;
459 ea_buf->xattr = NULL;
460 return 0;
462 if ((min_size <= sizeof (ji->i_inline_ea)) &&
463 (ji->mode2 & INLINEEA)) {
464 ea_buf->flag = EA_INLINE | EA_NEW;
465 ea_buf->max_size = sizeof (ji->i_inline_ea);
466 ea_buf->xattr = (struct jfs_ea_list *) ji->i_inline_ea;
467 DXDlength(&ea_buf->new_ea, 0);
468 DXDaddress(&ea_buf->new_ea, 0);
469 ea_buf->new_ea.flag = DXD_INLINE;
470 DXDsize(&ea_buf->new_ea, min_size);
471 return 0;
473 current_blocks = 0;
474 } else if (ji->ea.flag & DXD_INLINE) {
475 if (min_size <= sizeof (ji->i_inline_ea)) {
476 ea_buf->flag = EA_INLINE;
477 ea_buf->max_size = sizeof (ji->i_inline_ea);
478 ea_buf->xattr = (struct jfs_ea_list *) ji->i_inline_ea;
479 goto size_check;
481 current_blocks = 0;
482 } else {
483 if (!(ji->ea.flag & DXD_EXTENT)) {
484 jfs_error(sb, "ea_get: invalid ea.flag)");
485 return -EIO;
487 current_blocks = (ea_size + sb->s_blocksize - 1) >>
488 sb->s_blocksize_bits;
490 size = max(min_size, ea_size);
492 if (size > PSIZE) {
494 * To keep the rest of the code simple. Allocate a
495 * contiguous buffer to work with
497 ea_buf->xattr = kmalloc(size, GFP_KERNEL);
498 if (ea_buf->xattr == NULL)
499 return -ENOMEM;
501 ea_buf->flag = EA_MALLOC;
502 ea_buf->max_size = (size + sb->s_blocksize - 1) &
503 ~(sb->s_blocksize - 1);
505 if (ea_size == 0)
506 return 0;
508 if ((rc = ea_read(inode, ea_buf->xattr))) {
509 kfree(ea_buf->xattr);
510 ea_buf->xattr = NULL;
511 return rc;
513 goto size_check;
515 blocks_needed = (min_size + sb->s_blocksize - 1) >>
516 sb->s_blocksize_bits;
518 if (blocks_needed > current_blocks) {
519 /* Allocate new blocks to quota. */
520 if (vfs_dq_alloc_block(inode, blocks_needed))
521 return -EDQUOT;
523 quota_allocation = blocks_needed;
525 rc = dbAlloc(inode, INOHINT(inode), (s64) blocks_needed,
526 &blkno);
527 if (rc)
528 goto clean_up;
530 DXDlength(&ea_buf->new_ea, blocks_needed);
531 DXDaddress(&ea_buf->new_ea, blkno);
532 ea_buf->new_ea.flag = DXD_EXTENT;
533 DXDsize(&ea_buf->new_ea, min_size);
535 ea_buf->flag = EA_EXTENT | EA_NEW;
537 ea_buf->mp = get_metapage(inode, blkno,
538 blocks_needed << sb->s_blocksize_bits,
540 if (ea_buf->mp == NULL) {
541 dbFree(inode, blkno, (s64) blocks_needed);
542 rc = -EIO;
543 goto clean_up;
545 ea_buf->xattr = ea_buf->mp->data;
546 ea_buf->max_size = (min_size + sb->s_blocksize - 1) &
547 ~(sb->s_blocksize - 1);
548 if (ea_size == 0)
549 return 0;
550 if ((rc = ea_read(inode, ea_buf->xattr))) {
551 discard_metapage(ea_buf->mp);
552 dbFree(inode, blkno, (s64) blocks_needed);
553 goto clean_up;
555 goto size_check;
557 ea_buf->flag = EA_EXTENT;
558 ea_buf->mp = read_metapage(inode, addressDXD(&ji->ea),
559 lengthDXD(&ji->ea) << sb->s_blocksize_bits,
561 if (ea_buf->mp == NULL) {
562 rc = -EIO;
563 goto clean_up;
565 ea_buf->xattr = ea_buf->mp->data;
566 ea_buf->max_size = (ea_size + sb->s_blocksize - 1) &
567 ~(sb->s_blocksize - 1);
569 size_check:
570 if (EALIST_SIZE(ea_buf->xattr) != ea_size) {
571 printk(KERN_ERR "ea_get: invalid extended attribute\n");
572 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_ADDRESS, 16, 1,
573 ea_buf->xattr, ea_size, 1);
574 ea_release(inode, ea_buf);
575 rc = -EIO;
576 goto clean_up;
579 return ea_size;
581 clean_up:
582 /* Rollback quota allocation */
583 if (quota_allocation)
584 vfs_dq_free_block(inode, quota_allocation);
586 return (rc);
589 static void ea_release(struct inode *inode, struct ea_buffer *ea_buf)
591 if (ea_buf->flag & EA_MALLOC)
592 kfree(ea_buf->xattr);
593 else if (ea_buf->flag & EA_EXTENT) {
594 assert(ea_buf->mp);
595 release_metapage(ea_buf->mp);
597 if (ea_buf->flag & EA_NEW)
598 dbFree(inode, addressDXD(&ea_buf->new_ea),
599 lengthDXD(&ea_buf->new_ea));
603 static int ea_put(tid_t tid, struct inode *inode, struct ea_buffer *ea_buf,
604 int new_size)
606 struct jfs_inode_info *ji = JFS_IP(inode);
607 unsigned long old_blocks, new_blocks;
608 int rc = 0;
610 if (new_size == 0) {
611 ea_release(inode, ea_buf);
612 ea_buf = NULL;
613 } else if (ea_buf->flag & EA_INLINE) {
614 assert(new_size <= sizeof (ji->i_inline_ea));
615 ji->mode2 &= ~INLINEEA;
616 ea_buf->new_ea.flag = DXD_INLINE;
617 DXDsize(&ea_buf->new_ea, new_size);
618 DXDaddress(&ea_buf->new_ea, 0);
619 DXDlength(&ea_buf->new_ea, 0);
620 } else if (ea_buf->flag & EA_MALLOC) {
621 rc = ea_write(inode, ea_buf->xattr, new_size, &ea_buf->new_ea);
622 kfree(ea_buf->xattr);
623 } else if (ea_buf->flag & EA_NEW) {
624 /* We have already allocated a new dxd */
625 flush_metapage(ea_buf->mp);
626 } else {
627 /* ->xattr must point to original ea's metapage */
628 rc = ea_write(inode, ea_buf->xattr, new_size, &ea_buf->new_ea);
629 discard_metapage(ea_buf->mp);
631 if (rc)
632 return rc;
634 old_blocks = new_blocks = 0;
636 if (ji->ea.flag & DXD_EXTENT) {
637 invalidate_dxd_metapages(inode, ji->ea);
638 old_blocks = lengthDXD(&ji->ea);
641 if (ea_buf) {
642 txEA(tid, inode, &ji->ea, &ea_buf->new_ea);
643 if (ea_buf->new_ea.flag & DXD_EXTENT) {
644 new_blocks = lengthDXD(&ea_buf->new_ea);
645 if (ji->ea.flag & DXD_INLINE)
646 ji->mode2 |= INLINEEA;
648 ji->ea = ea_buf->new_ea;
649 } else {
650 txEA(tid, inode, &ji->ea, NULL);
651 if (ji->ea.flag & DXD_INLINE)
652 ji->mode2 |= INLINEEA;
653 ji->ea.flag = 0;
654 ji->ea.size = 0;
657 /* If old blocks exist, they must be removed from quota allocation. */
658 if (old_blocks)
659 vfs_dq_free_block(inode, old_blocks);
661 inode->i_ctime = CURRENT_TIME;
663 return 0;
667 * can_set_system_xattr
669 * This code is specific to the system.* namespace. It contains policy
670 * which doesn't belong in the main xattr codepath.
672 static int can_set_system_xattr(struct inode *inode, const char *name,
673 const void *value, size_t value_len)
675 #ifdef CONFIG_JFS_POSIX_ACL
676 struct posix_acl *acl;
677 int rc;
679 if (!is_owner_or_cap(inode))
680 return -EPERM;
683 * POSIX_ACL_XATTR_ACCESS is tied to i_mode
685 if (strcmp(name, POSIX_ACL_XATTR_ACCESS) == 0) {
686 acl = posix_acl_from_xattr(value, value_len);
687 if (IS_ERR(acl)) {
688 rc = PTR_ERR(acl);
689 printk(KERN_ERR "posix_acl_from_xattr returned %d\n",
690 rc);
691 return rc;
693 if (acl) {
694 mode_t mode = inode->i_mode;
695 rc = posix_acl_equiv_mode(acl, &mode);
696 posix_acl_release(acl);
697 if (rc < 0) {
698 printk(KERN_ERR
699 "posix_acl_equiv_mode returned %d\n",
700 rc);
701 return rc;
703 inode->i_mode = mode;
704 mark_inode_dirty(inode);
707 * We're changing the ACL. Get rid of the cached one
709 forget_cached_acl(inode, ACL_TYPE_ACCESS);
711 return 0;
712 } else if (strcmp(name, POSIX_ACL_XATTR_DEFAULT) == 0) {
713 acl = posix_acl_from_xattr(value, value_len);
714 if (IS_ERR(acl)) {
715 rc = PTR_ERR(acl);
716 printk(KERN_ERR "posix_acl_from_xattr returned %d\n",
717 rc);
718 return rc;
720 posix_acl_release(acl);
723 * We're changing the default ACL. Get rid of the cached one
725 forget_cached_acl(inode, ACL_TYPE_DEFAULT);
727 return 0;
729 #endif /* CONFIG_JFS_POSIX_ACL */
730 return -EOPNOTSUPP;
734 * Most of the permission checking is done by xattr_permission in the vfs.
735 * The local file system is responsible for handling the system.* namespace.
736 * We also need to verify that this is a namespace that we recognize.
738 static int can_set_xattr(struct inode *inode, const char *name,
739 const void *value, size_t value_len)
741 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
742 return can_set_system_xattr(inode, name, value, value_len);
744 if (!strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN)) {
746 * This makes sure that we aren't trying to set an
747 * attribute in a different namespace by prefixing it
748 * with "os2."
750 if (is_known_namespace(name + XATTR_OS2_PREFIX_LEN))
751 return -EOPNOTSUPP;
752 return 0;
756 * Don't allow setting an attribute in an unknown namespace.
758 if (strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) &&
759 strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) &&
760 strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
761 return -EOPNOTSUPP;
763 return 0;
766 int __jfs_setxattr(tid_t tid, struct inode *inode, const char *name,
767 const void *value, size_t value_len, int flags)
769 struct jfs_ea_list *ealist;
770 struct jfs_ea *ea, *old_ea = NULL, *next_ea = NULL;
771 struct ea_buffer ea_buf;
772 int old_ea_size = 0;
773 int xattr_size;
774 int new_size;
775 int namelen = strlen(name);
776 char *os2name = NULL;
777 int found = 0;
778 int rc;
779 int length;
781 if (strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN) == 0) {
782 os2name = kmalloc(namelen - XATTR_OS2_PREFIX_LEN + 1,
783 GFP_KERNEL);
784 if (!os2name)
785 return -ENOMEM;
786 strcpy(os2name, name + XATTR_OS2_PREFIX_LEN);
787 name = os2name;
788 namelen -= XATTR_OS2_PREFIX_LEN;
791 down_write(&JFS_IP(inode)->xattr_sem);
793 xattr_size = ea_get(inode, &ea_buf, 0);
794 if (xattr_size < 0) {
795 rc = xattr_size;
796 goto out;
799 again:
800 ealist = (struct jfs_ea_list *) ea_buf.xattr;
801 new_size = sizeof (struct jfs_ea_list);
803 if (xattr_size) {
804 for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist);
805 ea = NEXT_EA(ea)) {
806 if ((namelen == ea->namelen) &&
807 (memcmp(name, ea->name, namelen) == 0)) {
808 found = 1;
809 if (flags & XATTR_CREATE) {
810 rc = -EEXIST;
811 goto release;
813 old_ea = ea;
814 old_ea_size = EA_SIZE(ea);
815 next_ea = NEXT_EA(ea);
816 } else
817 new_size += EA_SIZE(ea);
821 if (!found) {
822 if (flags & XATTR_REPLACE) {
823 rc = -ENODATA;
824 goto release;
826 if (value == NULL) {
827 rc = 0;
828 goto release;
831 if (value)
832 new_size += sizeof (struct jfs_ea) + namelen + 1 + value_len;
834 if (new_size > ea_buf.max_size) {
836 * We need to allocate more space for merged ea list.
837 * We should only have loop to again: once.
839 ea_release(inode, &ea_buf);
840 xattr_size = ea_get(inode, &ea_buf, new_size);
841 if (xattr_size < 0) {
842 rc = xattr_size;
843 goto out;
845 goto again;
848 /* Remove old ea of the same name */
849 if (found) {
850 /* number of bytes following target EA */
851 length = (char *) END_EALIST(ealist) - (char *) next_ea;
852 if (length > 0)
853 memmove(old_ea, next_ea, length);
854 xattr_size -= old_ea_size;
857 /* Add new entry to the end */
858 if (value) {
859 if (xattr_size == 0)
860 /* Completely new ea list */
861 xattr_size = sizeof (struct jfs_ea_list);
863 ea = (struct jfs_ea *) ((char *) ealist + xattr_size);
864 ea->flag = 0;
865 ea->namelen = namelen;
866 ea->valuelen = (cpu_to_le16(value_len));
867 memcpy(ea->name, name, namelen);
868 ea->name[namelen] = 0;
869 if (value_len)
870 memcpy(&ea->name[namelen + 1], value, value_len);
871 xattr_size += EA_SIZE(ea);
874 /* DEBUG - If we did this right, these number match */
875 if (xattr_size != new_size) {
876 printk(KERN_ERR
877 "jfs_xsetattr: xattr_size = %d, new_size = %d\n",
878 xattr_size, new_size);
880 rc = -EINVAL;
881 goto release;
885 * If we're left with an empty list, there's no ea
887 if (new_size == sizeof (struct jfs_ea_list))
888 new_size = 0;
890 ealist->size = cpu_to_le32(new_size);
892 rc = ea_put(tid, inode, &ea_buf, new_size);
894 goto out;
895 release:
896 ea_release(inode, &ea_buf);
897 out:
898 up_write(&JFS_IP(inode)->xattr_sem);
900 kfree(os2name);
902 return rc;
905 int jfs_setxattr(struct dentry *dentry, const char *name, const void *value,
906 size_t value_len, int flags)
908 struct inode *inode = dentry->d_inode;
909 struct jfs_inode_info *ji = JFS_IP(inode);
910 int rc;
911 tid_t tid;
913 if ((rc = can_set_xattr(inode, name, value, value_len)))
914 return rc;
916 if (value == NULL) { /* empty EA, do not remove */
917 value = "";
918 value_len = 0;
921 tid = txBegin(inode->i_sb, 0);
922 mutex_lock(&ji->commit_mutex);
923 rc = __jfs_setxattr(tid, dentry->d_inode, name, value, value_len,
924 flags);
925 if (!rc)
926 rc = txCommit(tid, 1, &inode, 0);
927 txEnd(tid);
928 mutex_unlock(&ji->commit_mutex);
930 return rc;
933 ssize_t __jfs_getxattr(struct inode *inode, const char *name, void *data,
934 size_t buf_size)
936 struct jfs_ea_list *ealist;
937 struct jfs_ea *ea;
938 struct ea_buffer ea_buf;
939 int xattr_size;
940 ssize_t size;
941 int namelen = strlen(name);
942 char *value;
944 down_read(&JFS_IP(inode)->xattr_sem);
946 xattr_size = ea_get(inode, &ea_buf, 0);
948 if (xattr_size < 0) {
949 size = xattr_size;
950 goto out;
953 if (xattr_size == 0)
954 goto not_found;
956 ealist = (struct jfs_ea_list *) ea_buf.xattr;
958 /* Find the named attribute */
959 for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea))
960 if ((namelen == ea->namelen) &&
961 memcmp(name, ea->name, namelen) == 0) {
962 /* Found it */
963 size = le16_to_cpu(ea->valuelen);
964 if (!data)
965 goto release;
966 else if (size > buf_size) {
967 size = -ERANGE;
968 goto release;
970 value = ((char *) &ea->name) + ea->namelen + 1;
971 memcpy(data, value, size);
972 goto release;
974 not_found:
975 size = -ENODATA;
976 release:
977 ea_release(inode, &ea_buf);
978 out:
979 up_read(&JFS_IP(inode)->xattr_sem);
981 return size;
984 ssize_t jfs_getxattr(struct dentry *dentry, const char *name, void *data,
985 size_t buf_size)
987 int err;
989 if (strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN) == 0) {
991 * skip past "os2." prefix
993 name += XATTR_OS2_PREFIX_LEN;
995 * Don't allow retrieving properly prefixed attributes
996 * by prepending them with "os2."
998 if (is_known_namespace(name))
999 return -EOPNOTSUPP;
1002 err = __jfs_getxattr(dentry->d_inode, name, data, buf_size);
1004 return err;
1008 * No special permissions are needed to list attributes except for trusted.*
1010 static inline int can_list(struct jfs_ea *ea)
1012 return (strncmp(ea->name, XATTR_TRUSTED_PREFIX,
1013 XATTR_TRUSTED_PREFIX_LEN) ||
1014 capable(CAP_SYS_ADMIN));
1017 ssize_t jfs_listxattr(struct dentry * dentry, char *data, size_t buf_size)
1019 struct inode *inode = dentry->d_inode;
1020 char *buffer;
1021 ssize_t size = 0;
1022 int xattr_size;
1023 struct jfs_ea_list *ealist;
1024 struct jfs_ea *ea;
1025 struct ea_buffer ea_buf;
1027 down_read(&JFS_IP(inode)->xattr_sem);
1029 xattr_size = ea_get(inode, &ea_buf, 0);
1030 if (xattr_size < 0) {
1031 size = xattr_size;
1032 goto out;
1035 if (xattr_size == 0)
1036 goto release;
1038 ealist = (struct jfs_ea_list *) ea_buf.xattr;
1040 /* compute required size of list */
1041 for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea)) {
1042 if (can_list(ea))
1043 size += name_size(ea) + 1;
1046 if (!data)
1047 goto release;
1049 if (size > buf_size) {
1050 size = -ERANGE;
1051 goto release;
1054 /* Copy attribute names to buffer */
1055 buffer = data;
1056 for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea)) {
1057 if (can_list(ea)) {
1058 int namelen = copy_name(buffer, ea);
1059 buffer += namelen + 1;
1063 release:
1064 ea_release(inode, &ea_buf);
1065 out:
1066 up_read(&JFS_IP(inode)->xattr_sem);
1067 return size;
1070 int jfs_removexattr(struct dentry *dentry, const char *name)
1072 struct inode *inode = dentry->d_inode;
1073 struct jfs_inode_info *ji = JFS_IP(inode);
1074 int rc;
1075 tid_t tid;
1077 if ((rc = can_set_xattr(inode, name, NULL, 0)))
1078 return rc;
1080 tid = txBegin(inode->i_sb, 0);
1081 mutex_lock(&ji->commit_mutex);
1082 rc = __jfs_setxattr(tid, dentry->d_inode, name, NULL, 0, XATTR_REPLACE);
1083 if (!rc)
1084 rc = txCommit(tid, 1, &inode, 0);
1085 txEnd(tid);
1086 mutex_unlock(&ji->commit_mutex);
1088 return rc;
1091 #ifdef CONFIG_JFS_SECURITY
1092 int jfs_init_security(tid_t tid, struct inode *inode, struct inode *dir)
1094 int rc;
1095 size_t len;
1096 void *value;
1097 char *suffix;
1098 char *name;
1100 rc = security_inode_init_security(inode, dir, &suffix, &value, &len);
1101 if (rc) {
1102 if (rc == -EOPNOTSUPP)
1103 return 0;
1104 return rc;
1106 name = kmalloc(XATTR_SECURITY_PREFIX_LEN + 1 + strlen(suffix),
1107 GFP_NOFS);
1108 if (!name) {
1109 rc = -ENOMEM;
1110 goto kmalloc_failed;
1112 strcpy(name, XATTR_SECURITY_PREFIX);
1113 strcpy(name + XATTR_SECURITY_PREFIX_LEN, suffix);
1115 rc = __jfs_setxattr(tid, inode, name, value, len, 0);
1117 kfree(name);
1118 kmalloc_failed:
1119 kfree(suffix);
1120 kfree(value);
1122 return rc;
1124 #endif