]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/jfs/xattr.c
[PATCH] move xattr permission checks into the VFS
[net-next-2.6.git] / fs / jfs / xattr.c
CommitLineData
1da177e4
LT
1/*
2 * Copyright (C) International Business Machines Corp., 2000-2004
3 * Copyright (C) Christoph Hellwig, 2002
4 *
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.
14 *
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
18 */
19
20#include <linux/fs.h>
21#include <linux/xattr.h>
9a59f452 22#include <linux/posix_acl_xattr.h>
1da177e4 23#include <linux/quotaops.h>
1d15b10f 24#include <linux/security.h>
1da177e4
LT
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"
34
35/*
36 * jfs_xattr.c: extended attribute service
37 *
38 * Overall design --
39 *
40 * Format:
41 *
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
48 *
49 * 0 1 2 4 4 + namelen + 1
50 * +-------+--------+--------+----------------+-------------------+
51 * | Flags | Name | Value | Name String \0 | Data . . . . |
52 * | | Length | Length | | |
53 * +-------+--------+--------+----------------+-------------------+
54 *
55 * A jfs_ea_list then is structured as
56 *
57 * 0 4 4 + EA_SIZE(ea1)
58 * +------------+-------------------+--------------------+-----
59 * | Overall EA | First FEA Element | Second FEA Element | .....
60 * | List Size | | |
61 * +------------+-------------------+--------------------+-----
62 *
63 * On-disk:
64 *
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.
68 */
69
70struct 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 */
76};
77
78/*
79 * ea_buffer.flag values
80 */
81#define EA_INLINE 0x0001
82#define EA_EXTENT 0x0002
83#define EA_NEW 0x0004
84#define EA_MALLOC 0x0008
85
1da177e4
LT
86
87/*
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
91 */
92static inline int is_os2_xattr(struct jfs_ea *ea)
93{
94 /*
95 * Check for "system."
96 */
97 if ((ea->namelen >= XATTR_SYSTEM_PREFIX_LEN) &&
98 !strncmp(ea->name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
99 return FALSE;
100 /*
101 * Check for "user."
102 */
103 if ((ea->namelen >= XATTR_USER_PREFIX_LEN) &&
104 !strncmp(ea->name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
105 return FALSE;
106 /*
107 * Check for "security."
108 */
109 if ((ea->namelen >= XATTR_SECURITY_PREFIX_LEN) &&
110 !strncmp(ea->name, XATTR_SECURITY_PREFIX,
111 XATTR_SECURITY_PREFIX_LEN))
112 return FALSE;
113 /*
114 * Check for "trusted."
115 */
116 if ((ea->namelen >= XATTR_TRUSTED_PREFIX_LEN) &&
117 !strncmp(ea->name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN))
118 return FALSE;
119 /*
120 * Add any other valid namespace prefixes here
121 */
122
123 /*
124 * We assume it's OS/2's flat namespace
125 */
126 return TRUE;
127}
128
129static inline int name_size(struct jfs_ea *ea)
130{
131 if (is_os2_xattr(ea))
132 return ea->namelen + XATTR_OS2_PREFIX_LEN;
133 else
134 return ea->namelen;
135}
136
137static inline int copy_name(char *buffer, struct jfs_ea *ea)
138{
139 int len = ea->namelen;
140
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;
145 }
146 memcpy(buffer, ea->name, ea->namelen);
147 buffer[ea->namelen] = 0;
148
149 return len;
150}
151
152/* Forward references */
153static void ea_release(struct inode *inode, struct ea_buffer *ea_buf);
154
155/*
156 * NAME: ea_write_inline
157 *
158 * FUNCTION: Attempt to write an EA inline if area is available
159 *
160 * PRE CONDITIONS:
161 * Already verified that the specified EA is small enough to fit inline
162 *
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
169 *
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.
174 *
175 * RETURNS: 0 for successful copy to inline area; -1 if area not available
176 */
177static int ea_write_inline(struct inode *ip, struct jfs_ea_list *ealist,
178 int size, dxd_t * ea)
179{
180 struct jfs_inode_info *ji = JFS_IP(ip);
181
182 /*
183 * Make sure we have an EA -- the NULL EA list is valid, but you
184 * can't copy it!
185 */
186 if (ealist && size > sizeof (struct jfs_ea_list)) {
187 assert(size <= sizeof (ji->i_inline_ea));
188
189 /*
190 * See if the space is available or if it is already being
191 * used for an inline EA.
192 */
193 if (!(ji->mode2 & INLINEEA) && !(ji->ea.flag & DXD_INLINE))
194 return -EPERM;
195
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);
207
208 /* Free up INLINE area */
209 if (ji->ea.flag & DXD_INLINE)
210 ji->mode2 |= INLINEEA;
211 }
212
213 return 0;
214}
215
216/*
217 * NAME: ea_write
218 *
219 * FUNCTION: Write an EA for an inode
220 *
221 * PRE CONDITIONS: EA has been verified
222 *
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
229 *
230 * NOTES: Will write EA inline if able to, otherwise allocates blocks for an
231 * extent and synchronously writes it to those blocks.
232 *
233 * RETURNS: 0 for success; Anything else indicates failure
234 */
235static int ea_write(struct inode *ip, struct jfs_ea_list *ealist, int size,
236 dxd_t * ea)
237{
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;
248
249 /*
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.
252 */
253 if (!ealist || size <= sizeof (ji->i_inline_ea)) {
254 if (!ea_write_inline(ip, ealist, size, ea))
255 return 0;
256 }
257
258 /* figure out how many blocks we need */
259 nblocks = (size + (sb->s_blocksize - 1)) >> sb->s_blocksize_bits;
260
261 /* Allocate new blocks to quota. */
262 if (DQUOT_ALLOC_BLOCK(ip, nblocks)) {
263 return -EDQUOT;
264 }
265
266 rc = dbAlloc(ip, INOHINT(ip), nblocks, &blkno);
267 if (rc) {
268 /*Rollback quota allocation. */
269 DQUOT_FREE_BLOCK(ip, nblocks);
270 return rc;
271 }
272
273 /*
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.
277 */
278 cp = (char *) ealist;
279 nbytes = size;
280 for (i = 0; i < nblocks; i += sbi->nbperpage) {
281 /*
282 * Determine how many bytes for this request, and round up to
283 * the nearest aggregate block size
284 */
285 nb = min(PSIZE, nbytes);
286 bytes_to_write =
287 ((((nb + sb->s_blocksize - 1)) >> sb->s_blocksize_bits))
288 << sb->s_blocksize_bits;
289
290 if (!(mp = get_metapage(ip, blkno + i, bytes_to_write, 1))) {
291 rc = -EIO;
292 goto failed;
293 }
294
295 memcpy(mp->data, cp, nb);
296
297 /*
298 * We really need a way to propagate errors for
299 * forced writes like this one. --hch
300 *
301 * (__write_metapage => release_metapage => flush_metapage)
302 */
303#ifdef _JFS_FIXME
304 if ((rc = flush_metapage(mp))) {
305 /*
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 ...
310 */
311 goto failed;
312 }
313#else
314 flush_metapage(mp);
315#endif
316
317 cp += PSIZE;
318 nbytes -= nb;
319 }
320
321 ea->flag = DXD_EXTENT;
322 DXDsize(ea, le32_to_cpu(ealist->size));
323 DXDlength(ea, nblocks);
324 DXDaddress(ea, blkno);
325
326 /* Free up INLINE area */
327 if (ji->ea.flag & DXD_INLINE)
328 ji->mode2 |= INLINEEA;
329
330 return 0;
331
332 failed:
333 /* Rollback quota allocation. */
334 DQUOT_FREE_BLOCK(ip, nblocks);
335
336 dbFree(ip, blkno, nblocks);
337 return rc;
338}
339
340/*
341 * NAME: ea_read_inline
342 *
343 * FUNCTION: Read an inlined EA into user's buffer
344 *
345 * PARAMETERS:
346 * ip - Inode pointer
347 * ealist - Pointer to buffer to fill in with EA
348 *
349 * RETURNS: 0
350 */
351static int ea_read_inline(struct inode *ip, struct jfs_ea_list *ealist)
352{
353 struct jfs_inode_info *ji = JFS_IP(ip);
354 int ea_size = sizeDXD(&ji->ea);
355
356 if (ea_size == 0) {
357 ealist->size = 0;
358 return 0;
359 }
360
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;
367
368 memcpy(ealist, ji->i_inline_ea, ea_size);
369 return 0;
370}
371
372/*
373 * NAME: ea_read
374 *
375 * FUNCTION: copy EA data into user's buffer
376 *
377 * PARAMETERS:
378 * ip - Inode pointer
379 * ealist - Pointer to buffer to fill in with EA
380 *
381 * NOTES: If EA is inline calls ea_read_inline() to copy EA.
382 *
383 * RETURNS: 0 for success; other indicates failure
384 */
385static int ea_read(struct inode *ip, struct jfs_ea_list *ealist)
386{
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;
397
398 /* quick check for in-line EA */
399 if (ji->ea.flag & DXD_INLINE)
400 return ea_read_inline(ip, ealist);
401
402 nbytes = sizeDXD(&ji->ea);
403 if (!nbytes) {
404 jfs_error(sb, "ea_read: nbytes is 0");
405 return -EIO;
406 }
407
408 /*
409 * Figure out how many blocks were allocated when this EA list was
410 * originally written to disk.
411 */
412 nblocks = lengthDXD(&ji->ea) << sbi->l2nbperpage;
413 blkno = addressDXD(&ji->ea) << sbi->l2nbperpage;
414
415 /*
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.
419 */
420 for (i = 0; i < nblocks; i += sbi->nbperpage) {
421 /*
422 * Determine how many bytes for this request, and round up to
423 * the nearest aggregate block size
424 */
425 nb = min(PSIZE, nbytes);
426 bytes_to_read =
427 ((((nb + sb->s_blocksize - 1)) >> sb->s_blocksize_bits))
428 << sb->s_blocksize_bits;
429
430 if (!(mp = read_metapage(ip, blkno + i, bytes_to_read, 1)))
431 return -EIO;
432
433 memcpy(cp, mp->data, nb);
434 release_metapage(mp);
435
436 cp += PSIZE;
437 nbytes -= nb;
438 }
439
440 return 0;
441}
442
443/*
444 * NAME: ea_get
445 *
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.
449 *
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
452 *
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
457 *
458 * RETURNS: 0 for success; Other indicates failure
459 */
460static int ea_get(struct inode *inode, struct ea_buffer *ea_buf, int min_size)
461{
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;
470
471 /* When fsck.jfs clears a bad ea, it doesn't clear the size */
472 if (ji->ea.flag == 0)
473 ea_size = 0;
474
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;
481 }
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;
492 }
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;
500 }
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;
506 }
507 current_blocks = (ea_size + sb->s_blocksize - 1) >>
508 sb->s_blocksize_bits;
509 }
510 size = max(min_size, ea_size);
511
512 if (size > PSIZE) {
513 /*
514 * To keep the rest of the code simple. Allocate a
515 * contiguous buffer to work with
516 */
517 ea_buf->xattr = kmalloc(size, GFP_KERNEL);
518 if (ea_buf->xattr == NULL)
519 return -ENOMEM;
520
521 ea_buf->flag = EA_MALLOC;
522 ea_buf->max_size = (size + sb->s_blocksize - 1) &
523 ~(sb->s_blocksize - 1);
524
525 if (ea_size == 0)
526 return 0;
527
528 if ((rc = ea_read(inode, ea_buf->xattr))) {
529 kfree(ea_buf->xattr);
530 ea_buf->xattr = NULL;
531 return rc;
532 }
533 goto size_check;
534 }
535 blocks_needed = (min_size + sb->s_blocksize - 1) >>
536 sb->s_blocksize_bits;
537
538 if (blocks_needed > current_blocks) {
539 /* Allocate new blocks to quota. */
540 if (DQUOT_ALLOC_BLOCK(inode, blocks_needed))
541 return -EDQUOT;
542
543 quota_allocation = blocks_needed;
544
545 rc = dbAlloc(inode, INOHINT(inode), (s64) blocks_needed,
546 &blkno);
547 if (rc)
548 goto clean_up;
549
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);
554
555 ea_buf->flag = EA_EXTENT | EA_NEW;
556
557 ea_buf->mp = get_metapage(inode, blkno,
558 blocks_needed << sb->s_blocksize_bits,
559 1);
560 if (ea_buf->mp == NULL) {
561 dbFree(inode, blkno, (s64) blocks_needed);
562 rc = -EIO;
563 goto clean_up;
564 }
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;
574 }
575 goto size_check;
576 }
577 ea_buf->flag = EA_EXTENT;
578 ea_buf->mp = read_metapage(inode, addressDXD(&ji->ea),
579 lengthDXD(&ji->ea) << sb->s_blocksize_bits,
580 1);
581 if (ea_buf->mp == NULL) {
582 rc = -EIO;
583 goto clean_up;
584 }
585 ea_buf->xattr = ea_buf->mp->data;
586 ea_buf->max_size = (ea_size + sb->s_blocksize - 1) &
587 ~(sb->s_blocksize - 1);
588
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;
596 }
597
598 return ea_size;
599
600 clean_up:
601 /* Rollback quota allocation */
602 if (quota_allocation)
603 DQUOT_FREE_BLOCK(inode, quota_allocation);
604
605 return (rc);
606}
607
608static void ea_release(struct inode *inode, struct ea_buffer *ea_buf)
609{
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);
615
616 if (ea_buf->flag & EA_NEW)
617 dbFree(inode, addressDXD(&ea_buf->new_ea),
618 lengthDXD(&ea_buf->new_ea));
619 }
620}
621
4f4b401b
DK
622static int ea_put(tid_t tid, struct inode *inode, struct ea_buffer *ea_buf,
623 int new_size)
1da177e4
LT
624{
625 struct jfs_inode_info *ji = JFS_IP(inode);
626 unsigned long old_blocks, new_blocks;
627 int rc = 0;
1da177e4
LT
628
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);
649 }
650 if (rc)
651 return rc;
652
1da177e4
LT
653 old_blocks = new_blocks = 0;
654
655 if (ji->ea.flag & DXD_EXTENT) {
656 invalidate_dxd_metapages(inode, ji->ea);
657 old_blocks = lengthDXD(&ji->ea);
658 }
659
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;
666 }
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;
674 }
675
676 /* If old blocks exist, they must be removed from quota allocation. */
677 if (old_blocks)
678 DQUOT_FREE_BLOCK(inode, old_blocks);
679
680 inode->i_ctime = CURRENT_TIME;
1da177e4 681
4f4b401b 682 return 0;
1da177e4
LT
683}
684
685/*
686 * can_set_system_xattr
687 *
688 * This code is specific to the system.* namespace. It contains policy
689 * which doesn't belong in the main xattr codepath.
690 */
691static int can_set_system_xattr(struct inode *inode, const char *name,
692 const void *value, size_t value_len)
693{
694#ifdef CONFIG_JFS_POSIX_ACL
695 struct posix_acl *acl;
696 int rc;
697
698 if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
699 return -EPERM;
700
701 /*
9a59f452 702 * POSIX_ACL_XATTR_ACCESS is tied to i_mode
1da177e4 703 */
9a59f452 704 if (strcmp(name, POSIX_ACL_XATTR_ACCESS) == 0) {
1da177e4
LT
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;
711 }
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;
721 }
722 inode->i_mode = mode;
723 mark_inode_dirty(inode);
724 }
725 /*
726 * We're changing the ACL. Get rid of the cached one
727 */
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;
732
733 return 0;
9a59f452 734 } else if (strcmp(name, POSIX_ACL_XATTR_DEFAULT) == 0) {
1da177e4
LT
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;
741 }
742 posix_acl_release(acl);
743
744 /*
745 * We're changing the default ACL. Get rid of the cached one
746 */
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;
751
752 return 0;
753 }
754#endif /* CONFIG_JFS_POSIX_ACL */
755 return -EOPNOTSUPP;
756}
757
758static int can_set_xattr(struct inode *inode, const char *name,
759 const void *value, size_t value_len)
760{
761 if (IS_RDONLY(inode))
762 return -EROFS;
763
6211502d 764 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
1da177e4
LT
765 return -EPERM;
766
767 if(strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN) == 0)
768 /*
769 * "system.*"
770 */
771 return can_set_system_xattr(inode, name, value, value_len);
772
59192ed9 773 if(strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) == 0)
1da177e4
LT
774 return (capable(CAP_SYS_ADMIN) ? 0 : -EPERM);
775
776#ifdef CONFIG_JFS_SECURITY
777 if (strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN)
59192ed9 778 == 0)
1da177e4
LT
779 return 0; /* Leave it to the security module */
780#endif
781
782 if((strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN) != 0) &&
783 (strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN) != 0))
784 return -EOPNOTSUPP;
785
786 if (!S_ISREG(inode->i_mode) &&
787 (!S_ISDIR(inode->i_mode) || inode->i_mode &S_ISVTX))
788 return -EPERM;
789
790 return permission(inode, MAY_WRITE, NULL);
791}
792
4f4b401b
DK
793int __jfs_setxattr(tid_t tid, struct inode *inode, const char *name,
794 const void *value, size_t value_len, int flags)
1da177e4
LT
795{
796 struct jfs_ea_list *ealist;
797 struct jfs_ea *ea, *old_ea = NULL, *next_ea = NULL;
798 struct ea_buffer ea_buf;
799 int old_ea_size = 0;
800 int xattr_size;
801 int new_size;
802 int namelen = strlen(name);
803 char *os2name = NULL;
804 int found = 0;
805 int rc;
806 int length;
807
1da177e4
LT
808 if (strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN) == 0) {
809 os2name = kmalloc(namelen - XATTR_OS2_PREFIX_LEN + 1,
810 GFP_KERNEL);
811 if (!os2name)
812 return -ENOMEM;
813 strcpy(os2name, name + XATTR_OS2_PREFIX_LEN);
814 name = os2name;
815 namelen -= XATTR_OS2_PREFIX_LEN;
816 }
817
818 down_write(&JFS_IP(inode)->xattr_sem);
819
820 xattr_size = ea_get(inode, &ea_buf, 0);
821 if (xattr_size < 0) {
822 rc = xattr_size;
823 goto out;
824 }
825
826 again:
827 ealist = (struct jfs_ea_list *) ea_buf.xattr;
828 new_size = sizeof (struct jfs_ea_list);
829
830 if (xattr_size) {
831 for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist);
832 ea = NEXT_EA(ea)) {
833 if ((namelen == ea->namelen) &&
834 (memcmp(name, ea->name, namelen) == 0)) {
835 found = 1;
836 if (flags & XATTR_CREATE) {
837 rc = -EEXIST;
838 goto release;
839 }
840 old_ea = ea;
841 old_ea_size = EA_SIZE(ea);
842 next_ea = NEXT_EA(ea);
843 } else
844 new_size += EA_SIZE(ea);
845 }
846 }
847
848 if (!found) {
849 if (flags & XATTR_REPLACE) {
850 rc = -ENODATA;
851 goto release;
852 }
853 if (value == NULL) {
854 rc = 0;
855 goto release;
856 }
857 }
858 if (value)
859 new_size += sizeof (struct jfs_ea) + namelen + 1 + value_len;
860
861 if (new_size > ea_buf.max_size) {
862 /*
863 * We need to allocate more space for merged ea list.
864 * We should only have loop to again: once.
865 */
866 ea_release(inode, &ea_buf);
867 xattr_size = ea_get(inode, &ea_buf, new_size);
868 if (xattr_size < 0) {
869 rc = xattr_size;
870 goto out;
871 }
872 goto again;
873 }
874
875 /* Remove old ea of the same name */
876 if (found) {
877 /* number of bytes following target EA */
878 length = (char *) END_EALIST(ealist) - (char *) next_ea;
879 if (length > 0)
880 memmove(old_ea, next_ea, length);
881 xattr_size -= old_ea_size;
882 }
883
884 /* Add new entry to the end */
885 if (value) {
886 if (xattr_size == 0)
887 /* Completely new ea list */
888 xattr_size = sizeof (struct jfs_ea_list);
889
890 ea = (struct jfs_ea *) ((char *) ealist + xattr_size);
891 ea->flag = 0;
892 ea->namelen = namelen;
893 ea->valuelen = (cpu_to_le16(value_len));
894 memcpy(ea->name, name, namelen);
895 ea->name[namelen] = 0;
896 if (value_len)
897 memcpy(&ea->name[namelen + 1], value, value_len);
898 xattr_size += EA_SIZE(ea);
899 }
900
901 /* DEBUG - If we did this right, these number match */
902 if (xattr_size != new_size) {
903 printk(KERN_ERR
904 "jfs_xsetattr: xattr_size = %d, new_size = %d\n",
905 xattr_size, new_size);
906
907 rc = -EINVAL;
908 goto release;
909 }
910
911 /*
912 * If we're left with an empty list, there's no ea
913 */
914 if (new_size == sizeof (struct jfs_ea_list))
915 new_size = 0;
916
917 ealist->size = cpu_to_le32(new_size);
918
4f4b401b 919 rc = ea_put(tid, inode, &ea_buf, new_size);
1da177e4
LT
920
921 goto out;
922 release:
923 ea_release(inode, &ea_buf);
924 out:
925 up_write(&JFS_IP(inode)->xattr_sem);
926
259692bd 927 kfree(os2name);
1da177e4
LT
928
929 return rc;
930}
931
932int jfs_setxattr(struct dentry *dentry, const char *name, const void *value,
933 size_t value_len, int flags)
934{
4f4b401b
DK
935 struct inode *inode = dentry->d_inode;
936 struct jfs_inode_info *ji = JFS_IP(inode);
937 int rc;
938 tid_t tid;
939
940 if ((rc = can_set_xattr(inode, name, value, value_len)))
941 return rc;
942
1da177e4
LT
943 if (value == NULL) { /* empty EA, do not remove */
944 value = "";
945 value_len = 0;
946 }
947
4f4b401b
DK
948 tid = txBegin(inode->i_sb, 0);
949 down(&ji->commit_sem);
950 rc = __jfs_setxattr(tid, dentry->d_inode, name, value, value_len,
951 flags);
952 if (!rc)
953 rc = txCommit(tid, 1, &inode, 0);
954 txEnd(tid);
955 up(&ji->commit_sem);
956
957 return rc;
1da177e4
LT
958}
959
960static int can_get_xattr(struct inode *inode, const char *name)
961{
962#ifdef CONFIG_JFS_SECURITY
963 if(strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) == 0)
964 return 0;
965#endif
966
967 if(strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) == 0)
968 return (capable(CAP_SYS_ADMIN) ? 0 : -EPERM);
969
970 if(strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN) == 0)
971 return 0;
972
973 return permission(inode, MAY_READ, NULL);
974}
975
976ssize_t __jfs_getxattr(struct inode *inode, const char *name, void *data,
977 size_t buf_size)
978{
979 struct jfs_ea_list *ealist;
980 struct jfs_ea *ea;
981 struct ea_buffer ea_buf;
982 int xattr_size;
983 ssize_t size;
984 int namelen = strlen(name);
985 char *os2name = NULL;
986 int rc;
987 char *value;
988
989 if ((rc = can_get_xattr(inode, name)))
990 return rc;
991
992 if (strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN) == 0) {
993 os2name = kmalloc(namelen - XATTR_OS2_PREFIX_LEN + 1,
994 GFP_KERNEL);
995 if (!os2name)
996 return -ENOMEM;
997 strcpy(os2name, name + XATTR_OS2_PREFIX_LEN);
998 name = os2name;
999 namelen -= XATTR_OS2_PREFIX_LEN;
1000 }
1001
1002 down_read(&JFS_IP(inode)->xattr_sem);
1003
1004 xattr_size = ea_get(inode, &ea_buf, 0);
1005
1006 if (xattr_size < 0) {
1007 size = xattr_size;
1008 goto out;
1009 }
1010
1011 if (xattr_size == 0)
1012 goto not_found;
1013
1014 ealist = (struct jfs_ea_list *) ea_buf.xattr;
1015
1016 /* Find the named attribute */
1017 for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea))
1018 if ((namelen == ea->namelen) &&
1019 memcmp(name, ea->name, namelen) == 0) {
1020 /* Found it */
1021 size = le16_to_cpu(ea->valuelen);
1022 if (!data)
1023 goto release;
1024 else if (size > buf_size) {
1025 size = -ERANGE;
1026 goto release;
1027 }
1028 value = ((char *) &ea->name) + ea->namelen + 1;
1029 memcpy(data, value, size);
1030 goto release;
1031 }
1032 not_found:
1033 size = -ENODATA;
1034 release:
1035 ea_release(inode, &ea_buf);
1036 out:
1037 up_read(&JFS_IP(inode)->xattr_sem);
1038
259692bd 1039 kfree(os2name);
1da177e4
LT
1040
1041 return size;
1042}
1043
1044ssize_t jfs_getxattr(struct dentry *dentry, const char *name, void *data,
1045 size_t buf_size)
1046{
1047 int err;
1048
1049 err = __jfs_getxattr(dentry->d_inode, name, data, buf_size);
1050
1051 return err;
1052}
1053
1054/*
1055 * No special permissions are needed to list attributes except for trusted.*
1056 */
1057static inline int can_list(struct jfs_ea *ea)
1058{
1059 return (strncmp(ea->name, XATTR_TRUSTED_PREFIX,
1060 XATTR_TRUSTED_PREFIX_LEN) ||
1061 capable(CAP_SYS_ADMIN));
1062}
1063
1064ssize_t jfs_listxattr(struct dentry * dentry, char *data, size_t buf_size)
1065{
1066 struct inode *inode = dentry->d_inode;
1067 char *buffer;
1068 ssize_t size = 0;
1069 int xattr_size;
1070 struct jfs_ea_list *ealist;
1071 struct jfs_ea *ea;
1072 struct ea_buffer ea_buf;
1073
1074 down_read(&JFS_IP(inode)->xattr_sem);
1075
1076 xattr_size = ea_get(inode, &ea_buf, 0);
1077 if (xattr_size < 0) {
1078 size = xattr_size;
1079 goto out;
1080 }
1081
1082 if (xattr_size == 0)
1083 goto release;
1084
1085 ealist = (struct jfs_ea_list *) ea_buf.xattr;
1086
1087 /* compute required size of list */
1088 for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea)) {
1089 if (can_list(ea))
1090 size += name_size(ea) + 1;
1091 }
1092
1093 if (!data)
1094 goto release;
1095
1096 if (size > buf_size) {
1097 size = -ERANGE;
1098 goto release;
1099 }
1100
1101 /* Copy attribute names to buffer */
1102 buffer = data;
1103 for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea)) {
1104 if (can_list(ea)) {
1105 int namelen = copy_name(buffer, ea);
1106 buffer += namelen + 1;
1107 }
1108 }
1109
1110 release:
1111 ea_release(inode, &ea_buf);
1112 out:
1113 up_read(&JFS_IP(inode)->xattr_sem);
1114 return size;
1115}
1116
1117int jfs_removexattr(struct dentry *dentry, const char *name)
1118{
4f4b401b
DK
1119 struct inode *inode = dentry->d_inode;
1120 struct jfs_inode_info *ji = JFS_IP(inode);
1121 int rc;
1122 tid_t tid;
1123
1124 if ((rc = can_set_xattr(inode, name, NULL, 0)))
1125 return rc;
1126
1127 tid = txBegin(inode->i_sb, 0);
1128 down(&ji->commit_sem);
1129 rc = __jfs_setxattr(tid, dentry->d_inode, name, NULL, 0, XATTR_REPLACE);
1130 if (!rc)
1131 rc = txCommit(tid, 1, &inode, 0);
1132 txEnd(tid);
1133 up(&ji->commit_sem);
1134
1135 return rc;
1da177e4 1136}
1d15b10f
DK
1137
1138#ifdef CONFIG_JFS_SECURITY
1139int jfs_init_security(tid_t tid, struct inode *inode, struct inode *dir)
1140{
1141 int rc;
1142 size_t len;
1143 void *value;
1144 char *suffix;
1145 char *name;
1146
1147 rc = security_inode_init_security(inode, dir, &suffix, &value, &len);
1148 if (rc) {
1149 if (rc == -EOPNOTSUPP)
1150 return 0;
1151 return rc;
1152 }
1153 name = kmalloc(XATTR_SECURITY_PREFIX_LEN + 1 + strlen(suffix),
1154 GFP_NOFS);
1155 if (!name) {
1156 rc = -ENOMEM;
1157 goto kmalloc_failed;
1158 }
1159 strcpy(name, XATTR_SECURITY_PREFIX);
1160 strcpy(name + XATTR_SECURITY_PREFIX_LEN, suffix);
1161
1162 rc = __jfs_setxattr(tid, inode, name, value, len, 0);
1163
1164 kfree(name);
1165kmalloc_failed:
1166 kfree(suffix);
1167 kfree(value);
1168
1169 return rc;
1170}
1171#endif