HID: hiddev: Fix slab-out-of-bounds write in hiddev_ioctl_usage()
[linux/fpc-iii.git] / fs / jfs / xattr.c
blob40a26a542341de148a29885a6321ad7735261183
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
89 static int is_known_namespace(const char *name)
91 if (strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN) &&
92 strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN) &&
93 strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) &&
94 strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN))
95 return false;
97 return true;
101 * These three routines are used to recognize on-disk extended attributes
102 * that are in a recognized namespace. If the attribute is not recognized,
103 * "os2." is prepended to the name
105 static int is_os2_xattr(struct jfs_ea *ea)
107 return !is_known_namespace(ea->name);
110 static inline int name_size(struct jfs_ea *ea)
112 if (is_os2_xattr(ea))
113 return ea->namelen + XATTR_OS2_PREFIX_LEN;
114 else
115 return ea->namelen;
118 static inline int copy_name(char *buffer, struct jfs_ea *ea)
120 int len = ea->namelen;
122 if (is_os2_xattr(ea)) {
123 memcpy(buffer, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN);
124 buffer += XATTR_OS2_PREFIX_LEN;
125 len += XATTR_OS2_PREFIX_LEN;
127 memcpy(buffer, ea->name, ea->namelen);
128 buffer[ea->namelen] = 0;
130 return len;
133 /* Forward references */
134 static void ea_release(struct inode *inode, struct ea_buffer *ea_buf);
137 * NAME: ea_write_inline
139 * FUNCTION: Attempt to write an EA inline if area is available
141 * PRE CONDITIONS:
142 * Already verified that the specified EA is small enough to fit inline
144 * PARAMETERS:
145 * ip - Inode pointer
146 * ealist - EA list pointer
147 * size - size of ealist in bytes
148 * ea - dxd_t structure to be filled in with necessary EA information
149 * if we successfully copy the EA inline
151 * NOTES:
152 * Checks if the inode's inline area is available. If so, copies EA inline
153 * and sets <ea> fields appropriately. Otherwise, returns failure, EA will
154 * have to be put into an extent.
156 * RETURNS: 0 for successful copy to inline area; -1 if area not available
158 static int ea_write_inline(struct inode *ip, struct jfs_ea_list *ealist,
159 int size, dxd_t * ea)
161 struct jfs_inode_info *ji = JFS_IP(ip);
164 * Make sure we have an EA -- the NULL EA list is valid, but you
165 * can't copy it!
167 if (ealist && size > sizeof (struct jfs_ea_list)) {
168 assert(size <= sizeof (ji->i_inline_ea));
171 * See if the space is available or if it is already being
172 * used for an inline EA.
174 if (!(ji->mode2 & INLINEEA) && !(ji->ea.flag & DXD_INLINE))
175 return -EPERM;
177 DXDsize(ea, size);
178 DXDlength(ea, 0);
179 DXDaddress(ea, 0);
180 memcpy(ji->i_inline_ea, ealist, size);
181 ea->flag = DXD_INLINE;
182 ji->mode2 &= ~INLINEEA;
183 } else {
184 ea->flag = 0;
185 DXDsize(ea, 0);
186 DXDlength(ea, 0);
187 DXDaddress(ea, 0);
189 /* Free up INLINE area */
190 if (ji->ea.flag & DXD_INLINE)
191 ji->mode2 |= INLINEEA;
194 return 0;
198 * NAME: ea_write
200 * FUNCTION: Write an EA for an inode
202 * PRE CONDITIONS: EA has been verified
204 * PARAMETERS:
205 * ip - Inode pointer
206 * ealist - EA list pointer
207 * size - size of ealist in bytes
208 * ea - dxd_t structure to be filled in appropriately with where the
209 * EA was copied
211 * NOTES: Will write EA inline if able to, otherwise allocates blocks for an
212 * extent and synchronously writes it to those blocks.
214 * RETURNS: 0 for success; Anything else indicates failure
216 static int ea_write(struct inode *ip, struct jfs_ea_list *ealist, int size,
217 dxd_t * ea)
219 struct super_block *sb = ip->i_sb;
220 struct jfs_inode_info *ji = JFS_IP(ip);
221 struct jfs_sb_info *sbi = JFS_SBI(sb);
222 int nblocks;
223 s64 blkno;
224 int rc = 0, i;
225 char *cp;
226 s32 nbytes, nb;
227 s32 bytes_to_write;
228 struct metapage *mp;
231 * Quick check to see if this is an in-linable EA. Short EAs
232 * and empty EAs are all in-linable, provided the space exists.
234 if (!ealist || size <= sizeof (ji->i_inline_ea)) {
235 if (!ea_write_inline(ip, ealist, size, ea))
236 return 0;
239 /* figure out how many blocks we need */
240 nblocks = (size + (sb->s_blocksize - 1)) >> sb->s_blocksize_bits;
242 /* Allocate new blocks to quota. */
243 rc = dquot_alloc_block(ip, nblocks);
244 if (rc)
245 return rc;
247 rc = dbAlloc(ip, INOHINT(ip), nblocks, &blkno);
248 if (rc) {
249 /*Rollback quota allocation. */
250 dquot_free_block(ip, nblocks);
251 return rc;
255 * Now have nblocks worth of storage to stuff into the FEALIST.
256 * loop over the FEALIST copying data into the buffer one page at
257 * a time.
259 cp = (char *) ealist;
260 nbytes = size;
261 for (i = 0; i < nblocks; i += sbi->nbperpage) {
263 * Determine how many bytes for this request, and round up to
264 * the nearest aggregate block size
266 nb = min(PSIZE, nbytes);
267 bytes_to_write =
268 ((((nb + sb->s_blocksize - 1)) >> sb->s_blocksize_bits))
269 << sb->s_blocksize_bits;
271 if (!(mp = get_metapage(ip, blkno + i, bytes_to_write, 1))) {
272 rc = -EIO;
273 goto failed;
276 memcpy(mp->data, cp, nb);
279 * We really need a way to propagate errors for
280 * forced writes like this one. --hch
282 * (__write_metapage => release_metapage => flush_metapage)
284 #ifdef _JFS_FIXME
285 if ((rc = flush_metapage(mp))) {
287 * the write failed -- this means that the buffer
288 * is still assigned and the blocks are not being
289 * used. this seems like the best error recovery
290 * we can get ...
292 goto failed;
294 #else
295 flush_metapage(mp);
296 #endif
298 cp += PSIZE;
299 nbytes -= nb;
302 ea->flag = DXD_EXTENT;
303 DXDsize(ea, le32_to_cpu(ealist->size));
304 DXDlength(ea, nblocks);
305 DXDaddress(ea, blkno);
307 /* Free up INLINE area */
308 if (ji->ea.flag & DXD_INLINE)
309 ji->mode2 |= INLINEEA;
311 return 0;
313 failed:
314 /* Rollback quota allocation. */
315 dquot_free_block(ip, nblocks);
317 dbFree(ip, blkno, nblocks);
318 return rc;
322 * NAME: ea_read_inline
324 * FUNCTION: Read an inlined EA into user's buffer
326 * PARAMETERS:
327 * ip - Inode pointer
328 * ealist - Pointer to buffer to fill in with EA
330 * RETURNS: 0
332 static int ea_read_inline(struct inode *ip, struct jfs_ea_list *ealist)
334 struct jfs_inode_info *ji = JFS_IP(ip);
335 int ea_size = sizeDXD(&ji->ea);
337 if (ea_size == 0) {
338 ealist->size = 0;
339 return 0;
342 /* Sanity Check */
343 if ((sizeDXD(&ji->ea) > sizeof (ji->i_inline_ea)))
344 return -EIO;
345 if (le32_to_cpu(((struct jfs_ea_list *) &ji->i_inline_ea)->size)
346 != ea_size)
347 return -EIO;
349 memcpy(ealist, ji->i_inline_ea, ea_size);
350 return 0;
354 * NAME: ea_read
356 * FUNCTION: copy EA data into user's buffer
358 * PARAMETERS:
359 * ip - Inode pointer
360 * ealist - Pointer to buffer to fill in with EA
362 * NOTES: If EA is inline calls ea_read_inline() to copy EA.
364 * RETURNS: 0 for success; other indicates failure
366 static int ea_read(struct inode *ip, struct jfs_ea_list *ealist)
368 struct super_block *sb = ip->i_sb;
369 struct jfs_inode_info *ji = JFS_IP(ip);
370 struct jfs_sb_info *sbi = JFS_SBI(sb);
371 int nblocks;
372 s64 blkno;
373 char *cp = (char *) ealist;
374 int i;
375 int nbytes, nb;
376 s32 bytes_to_read;
377 struct metapage *mp;
379 /* quick check for in-line EA */
380 if (ji->ea.flag & DXD_INLINE)
381 return ea_read_inline(ip, ealist);
383 nbytes = sizeDXD(&ji->ea);
384 if (!nbytes) {
385 jfs_error(sb, "nbytes is 0\n");
386 return -EIO;
390 * Figure out how many blocks were allocated when this EA list was
391 * originally written to disk.
393 nblocks = lengthDXD(&ji->ea) << sbi->l2nbperpage;
394 blkno = addressDXD(&ji->ea) << sbi->l2nbperpage;
397 * I have found the disk blocks which were originally used to store
398 * the FEALIST. now i loop over each contiguous block copying the
399 * data into the buffer.
401 for (i = 0; i < nblocks; i += sbi->nbperpage) {
403 * Determine how many bytes for this request, and round up to
404 * the nearest aggregate block size
406 nb = min(PSIZE, nbytes);
407 bytes_to_read =
408 ((((nb + sb->s_blocksize - 1)) >> sb->s_blocksize_bits))
409 << sb->s_blocksize_bits;
411 if (!(mp = read_metapage(ip, blkno + i, bytes_to_read, 1)))
412 return -EIO;
414 memcpy(cp, mp->data, nb);
415 release_metapage(mp);
417 cp += PSIZE;
418 nbytes -= nb;
421 return 0;
425 * NAME: ea_get
427 * FUNCTION: Returns buffer containing existing extended attributes.
428 * The size of the buffer will be the larger of the existing
429 * attributes size, or min_size.
431 * The buffer, which may be inlined in the inode or in the
432 * page cache must be release by calling ea_release or ea_put
434 * PARAMETERS:
435 * inode - Inode pointer
436 * ea_buf - Structure to be populated with ealist and its metadata
437 * min_size- minimum size of buffer to be returned
439 * RETURNS: 0 for success; Other indicates failure
441 static int ea_get(struct inode *inode, struct ea_buffer *ea_buf, int min_size)
443 struct jfs_inode_info *ji = JFS_IP(inode);
444 struct super_block *sb = inode->i_sb;
445 int size;
446 int ea_size = sizeDXD(&ji->ea);
447 int blocks_needed, current_blocks;
448 s64 blkno;
449 int rc;
450 int quota_allocation = 0;
452 /* When fsck.jfs clears a bad ea, it doesn't clear the size */
453 if (ji->ea.flag == 0)
454 ea_size = 0;
456 if (ea_size == 0) {
457 if (min_size == 0) {
458 ea_buf->flag = 0;
459 ea_buf->max_size = 0;
460 ea_buf->xattr = NULL;
461 return 0;
463 if ((min_size <= sizeof (ji->i_inline_ea)) &&
464 (ji->mode2 & INLINEEA)) {
465 ea_buf->flag = EA_INLINE | EA_NEW;
466 ea_buf->max_size = sizeof (ji->i_inline_ea);
467 ea_buf->xattr = (struct jfs_ea_list *) ji->i_inline_ea;
468 DXDlength(&ea_buf->new_ea, 0);
469 DXDaddress(&ea_buf->new_ea, 0);
470 ea_buf->new_ea.flag = DXD_INLINE;
471 DXDsize(&ea_buf->new_ea, min_size);
472 return 0;
474 current_blocks = 0;
475 } else if (ji->ea.flag & DXD_INLINE) {
476 if (min_size <= sizeof (ji->i_inline_ea)) {
477 ea_buf->flag = EA_INLINE;
478 ea_buf->max_size = sizeof (ji->i_inline_ea);
479 ea_buf->xattr = (struct jfs_ea_list *) ji->i_inline_ea;
480 goto size_check;
482 current_blocks = 0;
483 } else {
484 if (!(ji->ea.flag & DXD_EXTENT)) {
485 jfs_error(sb, "invalid ea.flag\n");
486 return -EIO;
488 current_blocks = (ea_size + sb->s_blocksize - 1) >>
489 sb->s_blocksize_bits;
491 size = max(min_size, ea_size);
493 if (size > PSIZE) {
495 * To keep the rest of the code simple. Allocate a
496 * contiguous buffer to work with. Make the buffer large
497 * enough to make use of the whole extent.
499 ea_buf->max_size = (size + sb->s_blocksize - 1) &
500 ~(sb->s_blocksize - 1);
502 ea_buf->xattr = kmalloc(ea_buf->max_size, GFP_KERNEL);
503 if (ea_buf->xattr == NULL)
504 return -ENOMEM;
506 ea_buf->flag = EA_MALLOC;
508 if (ea_size == 0)
509 return 0;
511 if ((rc = ea_read(inode, ea_buf->xattr))) {
512 kfree(ea_buf->xattr);
513 ea_buf->xattr = NULL;
514 return rc;
516 goto size_check;
518 blocks_needed = (min_size + sb->s_blocksize - 1) >>
519 sb->s_blocksize_bits;
521 if (blocks_needed > current_blocks) {
522 /* Allocate new blocks to quota. */
523 rc = dquot_alloc_block(inode, blocks_needed);
524 if (rc)
525 return -EDQUOT;
527 quota_allocation = blocks_needed;
529 rc = dbAlloc(inode, INOHINT(inode), (s64) blocks_needed,
530 &blkno);
531 if (rc)
532 goto clean_up;
534 DXDlength(&ea_buf->new_ea, blocks_needed);
535 DXDaddress(&ea_buf->new_ea, blkno);
536 ea_buf->new_ea.flag = DXD_EXTENT;
537 DXDsize(&ea_buf->new_ea, min_size);
539 ea_buf->flag = EA_EXTENT | EA_NEW;
541 ea_buf->mp = get_metapage(inode, blkno,
542 blocks_needed << sb->s_blocksize_bits,
544 if (ea_buf->mp == NULL) {
545 dbFree(inode, blkno, (s64) blocks_needed);
546 rc = -EIO;
547 goto clean_up;
549 ea_buf->xattr = ea_buf->mp->data;
550 ea_buf->max_size = (min_size + sb->s_blocksize - 1) &
551 ~(sb->s_blocksize - 1);
552 if (ea_size == 0)
553 return 0;
554 if ((rc = ea_read(inode, ea_buf->xattr))) {
555 discard_metapage(ea_buf->mp);
556 dbFree(inode, blkno, (s64) blocks_needed);
557 goto clean_up;
559 goto size_check;
561 ea_buf->flag = EA_EXTENT;
562 ea_buf->mp = read_metapage(inode, addressDXD(&ji->ea),
563 lengthDXD(&ji->ea) << sb->s_blocksize_bits,
565 if (ea_buf->mp == NULL) {
566 rc = -EIO;
567 goto clean_up;
569 ea_buf->xattr = ea_buf->mp->data;
570 ea_buf->max_size = (ea_size + sb->s_blocksize - 1) &
571 ~(sb->s_blocksize - 1);
573 size_check:
574 if (EALIST_SIZE(ea_buf->xattr) != ea_size) {
575 printk(KERN_ERR "ea_get: invalid extended attribute\n");
576 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_ADDRESS, 16, 1,
577 ea_buf->xattr, ea_size, 1);
578 ea_release(inode, ea_buf);
579 rc = -EIO;
580 goto clean_up;
583 return ea_size;
585 clean_up:
586 /* Rollback quota allocation */
587 if (quota_allocation)
588 dquot_free_block(inode, quota_allocation);
590 return (rc);
593 static void ea_release(struct inode *inode, struct ea_buffer *ea_buf)
595 if (ea_buf->flag & EA_MALLOC)
596 kfree(ea_buf->xattr);
597 else if (ea_buf->flag & EA_EXTENT) {
598 assert(ea_buf->mp);
599 release_metapage(ea_buf->mp);
601 if (ea_buf->flag & EA_NEW)
602 dbFree(inode, addressDXD(&ea_buf->new_ea),
603 lengthDXD(&ea_buf->new_ea));
607 static int ea_put(tid_t tid, struct inode *inode, struct ea_buffer *ea_buf,
608 int new_size)
610 struct jfs_inode_info *ji = JFS_IP(inode);
611 unsigned long old_blocks, new_blocks;
612 int rc = 0;
614 if (new_size == 0) {
615 ea_release(inode, ea_buf);
616 ea_buf = NULL;
617 } else if (ea_buf->flag & EA_INLINE) {
618 assert(new_size <= sizeof (ji->i_inline_ea));
619 ji->mode2 &= ~INLINEEA;
620 ea_buf->new_ea.flag = DXD_INLINE;
621 DXDsize(&ea_buf->new_ea, new_size);
622 DXDaddress(&ea_buf->new_ea, 0);
623 DXDlength(&ea_buf->new_ea, 0);
624 } else if (ea_buf->flag & EA_MALLOC) {
625 rc = ea_write(inode, ea_buf->xattr, new_size, &ea_buf->new_ea);
626 kfree(ea_buf->xattr);
627 } else if (ea_buf->flag & EA_NEW) {
628 /* We have already allocated a new dxd */
629 flush_metapage(ea_buf->mp);
630 } else {
631 /* ->xattr must point to original ea's metapage */
632 rc = ea_write(inode, ea_buf->xattr, new_size, &ea_buf->new_ea);
633 discard_metapage(ea_buf->mp);
635 if (rc)
636 return rc;
638 old_blocks = new_blocks = 0;
640 if (ji->ea.flag & DXD_EXTENT) {
641 invalidate_dxd_metapages(inode, ji->ea);
642 old_blocks = lengthDXD(&ji->ea);
645 if (ea_buf) {
646 txEA(tid, inode, &ji->ea, &ea_buf->new_ea);
647 if (ea_buf->new_ea.flag & DXD_EXTENT) {
648 new_blocks = lengthDXD(&ea_buf->new_ea);
649 if (ji->ea.flag & DXD_INLINE)
650 ji->mode2 |= INLINEEA;
652 ji->ea = ea_buf->new_ea;
653 } else {
654 txEA(tid, inode, &ji->ea, NULL);
655 if (ji->ea.flag & DXD_INLINE)
656 ji->mode2 |= INLINEEA;
657 ji->ea.flag = 0;
658 ji->ea.size = 0;
661 /* If old blocks exist, they must be removed from quota allocation. */
662 if (old_blocks)
663 dquot_free_block(inode, old_blocks);
665 inode->i_ctime = CURRENT_TIME;
667 return 0;
671 * Most of the permission checking is done by xattr_permission in the vfs.
672 * We also need to verify that this is a namespace that we recognize.
674 static int can_set_xattr(struct inode *inode, const char *name,
675 const void *value, size_t value_len)
677 if (!strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN)) {
679 * This makes sure that we aren't trying to set an
680 * attribute in a different namespace by prefixing it
681 * with "os2."
683 if (is_known_namespace(name + XATTR_OS2_PREFIX_LEN))
684 return -EOPNOTSUPP;
685 return 0;
689 * Don't allow setting an attribute in an unknown namespace.
691 if (strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) &&
692 strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) &&
693 strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
694 return -EOPNOTSUPP;
696 return 0;
699 int __jfs_setxattr(tid_t tid, struct inode *inode, const char *name,
700 const void *value, size_t value_len, int flags)
702 struct jfs_ea_list *ealist;
703 struct jfs_ea *ea, *old_ea = NULL, *next_ea = NULL;
704 struct ea_buffer ea_buf;
705 int old_ea_size = 0;
706 int xattr_size;
707 int new_size;
708 int namelen = strlen(name);
709 char *os2name = NULL;
710 int found = 0;
711 int rc;
712 int length;
714 if (strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN) == 0) {
715 os2name = kmalloc(namelen - XATTR_OS2_PREFIX_LEN + 1,
716 GFP_KERNEL);
717 if (!os2name)
718 return -ENOMEM;
719 strcpy(os2name, name + XATTR_OS2_PREFIX_LEN);
720 name = os2name;
721 namelen -= XATTR_OS2_PREFIX_LEN;
724 down_write(&JFS_IP(inode)->xattr_sem);
726 xattr_size = ea_get(inode, &ea_buf, 0);
727 if (xattr_size < 0) {
728 rc = xattr_size;
729 goto out;
732 again:
733 ealist = (struct jfs_ea_list *) ea_buf.xattr;
734 new_size = sizeof (struct jfs_ea_list);
736 if (xattr_size) {
737 for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist);
738 ea = NEXT_EA(ea)) {
739 if ((namelen == ea->namelen) &&
740 (memcmp(name, ea->name, namelen) == 0)) {
741 found = 1;
742 if (flags & XATTR_CREATE) {
743 rc = -EEXIST;
744 goto release;
746 old_ea = ea;
747 old_ea_size = EA_SIZE(ea);
748 next_ea = NEXT_EA(ea);
749 } else
750 new_size += EA_SIZE(ea);
754 if (!found) {
755 if (flags & XATTR_REPLACE) {
756 rc = -ENODATA;
757 goto release;
759 if (value == NULL) {
760 rc = 0;
761 goto release;
764 if (value)
765 new_size += sizeof (struct jfs_ea) + namelen + 1 + value_len;
767 if (new_size > ea_buf.max_size) {
769 * We need to allocate more space for merged ea list.
770 * We should only have loop to again: once.
772 ea_release(inode, &ea_buf);
773 xattr_size = ea_get(inode, &ea_buf, new_size);
774 if (xattr_size < 0) {
775 rc = xattr_size;
776 goto out;
778 goto again;
781 /* Remove old ea of the same name */
782 if (found) {
783 /* number of bytes following target EA */
784 length = (char *) END_EALIST(ealist) - (char *) next_ea;
785 if (length > 0)
786 memmove(old_ea, next_ea, length);
787 xattr_size -= old_ea_size;
790 /* Add new entry to the end */
791 if (value) {
792 if (xattr_size == 0)
793 /* Completely new ea list */
794 xattr_size = sizeof (struct jfs_ea_list);
797 * The size of EA value is limitted by on-disk format up to
798 * __le16, there would be an overflow if the size is equal
799 * to XATTR_SIZE_MAX (65536). In order to avoid this issue,
800 * we can pre-checkup the value size against USHRT_MAX, and
801 * return -E2BIG in this case, which is consistent with the
802 * VFS setxattr interface.
804 if (value_len >= USHRT_MAX) {
805 rc = -E2BIG;
806 goto release;
809 ea = (struct jfs_ea *) ((char *) ealist + xattr_size);
810 ea->flag = 0;
811 ea->namelen = namelen;
812 ea->valuelen = (cpu_to_le16(value_len));
813 memcpy(ea->name, name, namelen);
814 ea->name[namelen] = 0;
815 if (value_len)
816 memcpy(&ea->name[namelen + 1], value, value_len);
817 xattr_size += EA_SIZE(ea);
820 /* DEBUG - If we did this right, these number match */
821 if (xattr_size != new_size) {
822 printk(KERN_ERR
823 "__jfs_setxattr: xattr_size = %d, new_size = %d\n",
824 xattr_size, new_size);
826 rc = -EINVAL;
827 goto release;
831 * If we're left with an empty list, there's no ea
833 if (new_size == sizeof (struct jfs_ea_list))
834 new_size = 0;
836 ealist->size = cpu_to_le32(new_size);
838 rc = ea_put(tid, inode, &ea_buf, new_size);
840 goto out;
841 release:
842 ea_release(inode, &ea_buf);
843 out:
844 up_write(&JFS_IP(inode)->xattr_sem);
846 kfree(os2name);
848 return rc;
851 int jfs_setxattr(struct dentry *dentry, const char *name, const void *value,
852 size_t value_len, int flags)
854 struct inode *inode = d_inode(dentry);
855 struct jfs_inode_info *ji = JFS_IP(inode);
856 int rc;
857 tid_t tid;
860 * If this is a request for a synthetic attribute in the system.*
861 * namespace use the generic infrastructure to resolve a handler
862 * for it via sb->s_xattr.
864 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
865 return generic_setxattr(dentry, name, value, value_len, flags);
867 if ((rc = can_set_xattr(inode, name, value, value_len)))
868 return rc;
870 if (value == NULL) { /* empty EA, do not remove */
871 value = "";
872 value_len = 0;
875 tid = txBegin(inode->i_sb, 0);
876 mutex_lock(&ji->commit_mutex);
877 rc = __jfs_setxattr(tid, d_inode(dentry), name, value, value_len,
878 flags);
879 if (!rc)
880 rc = txCommit(tid, 1, &inode, 0);
881 txEnd(tid);
882 mutex_unlock(&ji->commit_mutex);
884 return rc;
887 ssize_t __jfs_getxattr(struct inode *inode, const char *name, void *data,
888 size_t buf_size)
890 struct jfs_ea_list *ealist;
891 struct jfs_ea *ea;
892 struct ea_buffer ea_buf;
893 int xattr_size;
894 ssize_t size;
895 int namelen = strlen(name);
896 char *value;
898 down_read(&JFS_IP(inode)->xattr_sem);
900 xattr_size = ea_get(inode, &ea_buf, 0);
902 if (xattr_size < 0) {
903 size = xattr_size;
904 goto out;
907 if (xattr_size == 0)
908 goto not_found;
910 ealist = (struct jfs_ea_list *) ea_buf.xattr;
912 /* Find the named attribute */
913 for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea))
914 if ((namelen == ea->namelen) &&
915 memcmp(name, ea->name, namelen) == 0) {
916 /* Found it */
917 size = le16_to_cpu(ea->valuelen);
918 if (!data)
919 goto release;
920 else if (size > buf_size) {
921 size = -ERANGE;
922 goto release;
924 value = ((char *) &ea->name) + ea->namelen + 1;
925 memcpy(data, value, size);
926 goto release;
928 not_found:
929 size = -ENODATA;
930 release:
931 ea_release(inode, &ea_buf);
932 out:
933 up_read(&JFS_IP(inode)->xattr_sem);
935 return size;
938 ssize_t jfs_getxattr(struct dentry *dentry, const char *name, void *data,
939 size_t buf_size)
941 int err;
944 * If this is a request for a synthetic attribute in the system.*
945 * namespace use the generic infrastructure to resolve a handler
946 * for it via sb->s_xattr.
948 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
949 return generic_getxattr(dentry, name, data, buf_size);
951 if (strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN) == 0) {
953 * skip past "os2." prefix
955 name += XATTR_OS2_PREFIX_LEN;
957 * Don't allow retrieving properly prefixed attributes
958 * by prepending them with "os2."
960 if (is_known_namespace(name))
961 return -EOPNOTSUPP;
964 err = __jfs_getxattr(d_inode(dentry), name, data, buf_size);
966 return err;
970 * No special permissions are needed to list attributes except for trusted.*
972 static inline int can_list(struct jfs_ea *ea)
974 return (strncmp(ea->name, XATTR_TRUSTED_PREFIX,
975 XATTR_TRUSTED_PREFIX_LEN) ||
976 capable(CAP_SYS_ADMIN));
979 ssize_t jfs_listxattr(struct dentry * dentry, char *data, size_t buf_size)
981 struct inode *inode = d_inode(dentry);
982 char *buffer;
983 ssize_t size = 0;
984 int xattr_size;
985 struct jfs_ea_list *ealist;
986 struct jfs_ea *ea;
987 struct ea_buffer ea_buf;
989 down_read(&JFS_IP(inode)->xattr_sem);
991 xattr_size = ea_get(inode, &ea_buf, 0);
992 if (xattr_size < 0) {
993 size = xattr_size;
994 goto out;
997 if (xattr_size == 0)
998 goto release;
1000 ealist = (struct jfs_ea_list *) ea_buf.xattr;
1002 /* compute required size of list */
1003 for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea)) {
1004 if (can_list(ea))
1005 size += name_size(ea) + 1;
1008 if (!data)
1009 goto release;
1011 if (size > buf_size) {
1012 size = -ERANGE;
1013 goto release;
1016 /* Copy attribute names to buffer */
1017 buffer = data;
1018 for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea)) {
1019 if (can_list(ea)) {
1020 int namelen = copy_name(buffer, ea);
1021 buffer += namelen + 1;
1025 release:
1026 ea_release(inode, &ea_buf);
1027 out:
1028 up_read(&JFS_IP(inode)->xattr_sem);
1029 return size;
1032 int jfs_removexattr(struct dentry *dentry, const char *name)
1034 struct inode *inode = d_inode(dentry);
1035 struct jfs_inode_info *ji = JFS_IP(inode);
1036 int rc;
1037 tid_t tid;
1040 * If this is a request for a synthetic attribute in the system.*
1041 * namespace use the generic infrastructure to resolve a handler
1042 * for it via sb->s_xattr.
1044 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
1045 return generic_removexattr(dentry, name);
1047 if ((rc = can_set_xattr(inode, name, NULL, 0)))
1048 return rc;
1050 tid = txBegin(inode->i_sb, 0);
1051 mutex_lock(&ji->commit_mutex);
1052 rc = __jfs_setxattr(tid, d_inode(dentry), name, NULL, 0, XATTR_REPLACE);
1053 if (!rc)
1054 rc = txCommit(tid, 1, &inode, 0);
1055 txEnd(tid);
1056 mutex_unlock(&ji->commit_mutex);
1058 return rc;
1062 * List of handlers for synthetic system.* attributes. All real ondisk
1063 * attributes are handled directly.
1065 const struct xattr_handler *jfs_xattr_handlers[] = {
1066 #ifdef CONFIG_JFS_POSIX_ACL
1067 &posix_acl_access_xattr_handler,
1068 &posix_acl_default_xattr_handler,
1069 #endif
1070 NULL,
1074 #ifdef CONFIG_JFS_SECURITY
1075 static int jfs_initxattrs(struct inode *inode, const struct xattr *xattr_array,
1076 void *fs_info)
1078 const struct xattr *xattr;
1079 tid_t *tid = fs_info;
1080 char *name;
1081 int err = 0;
1083 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
1084 name = kmalloc(XATTR_SECURITY_PREFIX_LEN +
1085 strlen(xattr->name) + 1, GFP_NOFS);
1086 if (!name) {
1087 err = -ENOMEM;
1088 break;
1090 strcpy(name, XATTR_SECURITY_PREFIX);
1091 strcpy(name + XATTR_SECURITY_PREFIX_LEN, xattr->name);
1093 err = __jfs_setxattr(*tid, inode, name,
1094 xattr->value, xattr->value_len, 0);
1095 kfree(name);
1096 if (err < 0)
1097 break;
1099 return err;
1102 int jfs_init_security(tid_t tid, struct inode *inode, struct inode *dir,
1103 const struct qstr *qstr)
1105 return security_inode_init_security(inode, dir, qstr,
1106 &jfs_initxattrs, &tid);
1108 #endif