JFS: allow extended attributes to be set within a existing transaction
[deliverable/linux.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
LT
23#include <linux/quotaops.h>
24#include "jfs_incore.h"
25#include "jfs_superblock.h"
26#include "jfs_dmap.h"
27#include "jfs_debug.h"
28#include "jfs_dinode.h"
29#include "jfs_extent.h"
30#include "jfs_metapage.h"
31#include "jfs_xattr.h"
32#include "jfs_acl.h"
33
34/*
35 * jfs_xattr.c: extended attribute service
36 *
37 * Overall design --
38 *
39 * Format:
40 *
41 * Extended attribute lists (jfs_ea_list) consist of an overall size (32 bit
42 * value) and a variable (0 or more) number of extended attribute
43 * entries. Each extended attribute entry (jfs_ea) is a <name,value> double
44 * where <name> is constructed from a null-terminated ascii string
45 * (1 ... 255 bytes in the name) and <value> is arbitrary 8 bit data
46 * (1 ... 65535 bytes). The in-memory format is
47 *
48 * 0 1 2 4 4 + namelen + 1
49 * +-------+--------+--------+----------------+-------------------+
50 * | Flags | Name | Value | Name String \0 | Data . . . . |
51 * | | Length | Length | | |
52 * +-------+--------+--------+----------------+-------------------+
53 *
54 * A jfs_ea_list then is structured as
55 *
56 * 0 4 4 + EA_SIZE(ea1)
57 * +------------+-------------------+--------------------+-----
58 * | Overall EA | First FEA Element | Second FEA Element | .....
59 * | List Size | | |
60 * +------------+-------------------+--------------------+-----
61 *
62 * On-disk:
63 *
64 * FEALISTs are stored on disk using blocks allocated by dbAlloc() and
65 * written directly. An EA list may be in-lined in the inode if there is
66 * sufficient room available.
67 */
68
69struct ea_buffer {
70 int flag; /* Indicates what storage xattr points to */
71 int max_size; /* largest xattr that fits in current buffer */
72 dxd_t new_ea; /* dxd to replace ea when modifying xattr */
73 struct metapage *mp; /* metapage containing ea list */
74 struct jfs_ea_list *xattr; /* buffer containing ea list */
75};
76
77/*
78 * ea_buffer.flag values
79 */
80#define EA_INLINE 0x0001
81#define EA_EXTENT 0x0002
82#define EA_NEW 0x0004
83#define EA_MALLOC 0x0008
84
85/* Namespaces */
86#define XATTR_SYSTEM_PREFIX "system."
87#define XATTR_SYSTEM_PREFIX_LEN (sizeof (XATTR_SYSTEM_PREFIX) - 1)
88
89#define XATTR_USER_PREFIX "user."
90#define XATTR_USER_PREFIX_LEN (sizeof (XATTR_USER_PREFIX) - 1)
91
92#define XATTR_OS2_PREFIX "os2."
93#define XATTR_OS2_PREFIX_LEN (sizeof (XATTR_OS2_PREFIX) - 1)
94
95/* XATTR_SECURITY_PREFIX is defined in include/linux/xattr.h */
96#define XATTR_SECURITY_PREFIX_LEN (sizeof (XATTR_SECURITY_PREFIX) - 1)
97
98#define XATTR_TRUSTED_PREFIX "trusted."
99#define XATTR_TRUSTED_PREFIX_LEN (sizeof (XATTR_TRUSTED_PREFIX) - 1)
100
101/*
102 * These three routines are used to recognize on-disk extended attributes
103 * that are in a recognized namespace. If the attribute is not recognized,
104 * "os2." is prepended to the name
105 */
106static inline int is_os2_xattr(struct jfs_ea *ea)
107{
108 /*
109 * Check for "system."
110 */
111 if ((ea->namelen >= XATTR_SYSTEM_PREFIX_LEN) &&
112 !strncmp(ea->name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
113 return FALSE;
114 /*
115 * Check for "user."
116 */
117 if ((ea->namelen >= XATTR_USER_PREFIX_LEN) &&
118 !strncmp(ea->name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
119 return FALSE;
120 /*
121 * Check for "security."
122 */
123 if ((ea->namelen >= XATTR_SECURITY_PREFIX_LEN) &&
124 !strncmp(ea->name, XATTR_SECURITY_PREFIX,
125 XATTR_SECURITY_PREFIX_LEN))
126 return FALSE;
127 /*
128 * Check for "trusted."
129 */
130 if ((ea->namelen >= XATTR_TRUSTED_PREFIX_LEN) &&
131 !strncmp(ea->name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN))
132 return FALSE;
133 /*
134 * Add any other valid namespace prefixes here
135 */
136
137 /*
138 * We assume it's OS/2's flat namespace
139 */
140 return TRUE;
141}
142
143static inline int name_size(struct jfs_ea *ea)
144{
145 if (is_os2_xattr(ea))
146 return ea->namelen + XATTR_OS2_PREFIX_LEN;
147 else
148 return ea->namelen;
149}
150
151static inline int copy_name(char *buffer, struct jfs_ea *ea)
152{
153 int len = ea->namelen;
154
155 if (is_os2_xattr(ea)) {
156 memcpy(buffer, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN);
157 buffer += XATTR_OS2_PREFIX_LEN;
158 len += XATTR_OS2_PREFIX_LEN;
159 }
160 memcpy(buffer, ea->name, ea->namelen);
161 buffer[ea->namelen] = 0;
162
163 return len;
164}
165
166/* Forward references */
167static void ea_release(struct inode *inode, struct ea_buffer *ea_buf);
168
169/*
170 * NAME: ea_write_inline
171 *
172 * FUNCTION: Attempt to write an EA inline if area is available
173 *
174 * PRE CONDITIONS:
175 * Already verified that the specified EA is small enough to fit inline
176 *
177 * PARAMETERS:
178 * ip - Inode pointer
179 * ealist - EA list pointer
180 * size - size of ealist in bytes
181 * ea - dxd_t structure to be filled in with necessary EA information
182 * if we successfully copy the EA inline
183 *
184 * NOTES:
185 * Checks if the inode's inline area is available. If so, copies EA inline
186 * and sets <ea> fields appropriately. Otherwise, returns failure, EA will
187 * have to be put into an extent.
188 *
189 * RETURNS: 0 for successful copy to inline area; -1 if area not available
190 */
191static int ea_write_inline(struct inode *ip, struct jfs_ea_list *ealist,
192 int size, dxd_t * ea)
193{
194 struct jfs_inode_info *ji = JFS_IP(ip);
195
196 /*
197 * Make sure we have an EA -- the NULL EA list is valid, but you
198 * can't copy it!
199 */
200 if (ealist && size > sizeof (struct jfs_ea_list)) {
201 assert(size <= sizeof (ji->i_inline_ea));
202
203 /*
204 * See if the space is available or if it is already being
205 * used for an inline EA.
206 */
207 if (!(ji->mode2 & INLINEEA) && !(ji->ea.flag & DXD_INLINE))
208 return -EPERM;
209
210 DXDsize(ea, size);
211 DXDlength(ea, 0);
212 DXDaddress(ea, 0);
213 memcpy(ji->i_inline_ea, ealist, size);
214 ea->flag = DXD_INLINE;
215 ji->mode2 &= ~INLINEEA;
216 } else {
217 ea->flag = 0;
218 DXDsize(ea, 0);
219 DXDlength(ea, 0);
220 DXDaddress(ea, 0);
221
222 /* Free up INLINE area */
223 if (ji->ea.flag & DXD_INLINE)
224 ji->mode2 |= INLINEEA;
225 }
226
227 return 0;
228}
229
230/*
231 * NAME: ea_write
232 *
233 * FUNCTION: Write an EA for an inode
234 *
235 * PRE CONDITIONS: EA has been verified
236 *
237 * PARAMETERS:
238 * ip - Inode pointer
239 * ealist - EA list pointer
240 * size - size of ealist in bytes
241 * ea - dxd_t structure to be filled in appropriately with where the
242 * EA was copied
243 *
244 * NOTES: Will write EA inline if able to, otherwise allocates blocks for an
245 * extent and synchronously writes it to those blocks.
246 *
247 * RETURNS: 0 for success; Anything else indicates failure
248 */
249static int ea_write(struct inode *ip, struct jfs_ea_list *ealist, int size,
250 dxd_t * ea)
251{
252 struct super_block *sb = ip->i_sb;
253 struct jfs_inode_info *ji = JFS_IP(ip);
254 struct jfs_sb_info *sbi = JFS_SBI(sb);
255 int nblocks;
256 s64 blkno;
257 int rc = 0, i;
258 char *cp;
259 s32 nbytes, nb;
260 s32 bytes_to_write;
261 struct metapage *mp;
262
263 /*
264 * Quick check to see if this is an in-linable EA. Short EAs
265 * and empty EAs are all in-linable, provided the space exists.
266 */
267 if (!ealist || size <= sizeof (ji->i_inline_ea)) {
268 if (!ea_write_inline(ip, ealist, size, ea))
269 return 0;
270 }
271
272 /* figure out how many blocks we need */
273 nblocks = (size + (sb->s_blocksize - 1)) >> sb->s_blocksize_bits;
274
275 /* Allocate new blocks to quota. */
276 if (DQUOT_ALLOC_BLOCK(ip, nblocks)) {
277 return -EDQUOT;
278 }
279
280 rc = dbAlloc(ip, INOHINT(ip), nblocks, &blkno);
281 if (rc) {
282 /*Rollback quota allocation. */
283 DQUOT_FREE_BLOCK(ip, nblocks);
284 return rc;
285 }
286
287 /*
288 * Now have nblocks worth of storage to stuff into the FEALIST.
289 * loop over the FEALIST copying data into the buffer one page at
290 * a time.
291 */
292 cp = (char *) ealist;
293 nbytes = size;
294 for (i = 0; i < nblocks; i += sbi->nbperpage) {
295 /*
296 * Determine how many bytes for this request, and round up to
297 * the nearest aggregate block size
298 */
299 nb = min(PSIZE, nbytes);
300 bytes_to_write =
301 ((((nb + sb->s_blocksize - 1)) >> sb->s_blocksize_bits))
302 << sb->s_blocksize_bits;
303
304 if (!(mp = get_metapage(ip, blkno + i, bytes_to_write, 1))) {
305 rc = -EIO;
306 goto failed;
307 }
308
309 memcpy(mp->data, cp, nb);
310
311 /*
312 * We really need a way to propagate errors for
313 * forced writes like this one. --hch
314 *
315 * (__write_metapage => release_metapage => flush_metapage)
316 */
317#ifdef _JFS_FIXME
318 if ((rc = flush_metapage(mp))) {
319 /*
320 * the write failed -- this means that the buffer
321 * is still assigned and the blocks are not being
322 * used. this seems like the best error recovery
323 * we can get ...
324 */
325 goto failed;
326 }
327#else
328 flush_metapage(mp);
329#endif
330
331 cp += PSIZE;
332 nbytes -= nb;
333 }
334
335 ea->flag = DXD_EXTENT;
336 DXDsize(ea, le32_to_cpu(ealist->size));
337 DXDlength(ea, nblocks);
338 DXDaddress(ea, blkno);
339
340 /* Free up INLINE area */
341 if (ji->ea.flag & DXD_INLINE)
342 ji->mode2 |= INLINEEA;
343
344 return 0;
345
346 failed:
347 /* Rollback quota allocation. */
348 DQUOT_FREE_BLOCK(ip, nblocks);
349
350 dbFree(ip, blkno, nblocks);
351 return rc;
352}
353
354/*
355 * NAME: ea_read_inline
356 *
357 * FUNCTION: Read an inlined EA into user's buffer
358 *
359 * PARAMETERS:
360 * ip - Inode pointer
361 * ealist - Pointer to buffer to fill in with EA
362 *
363 * RETURNS: 0
364 */
365static int ea_read_inline(struct inode *ip, struct jfs_ea_list *ealist)
366{
367 struct jfs_inode_info *ji = JFS_IP(ip);
368 int ea_size = sizeDXD(&ji->ea);
369
370 if (ea_size == 0) {
371 ealist->size = 0;
372 return 0;
373 }
374
375 /* Sanity Check */
376 if ((sizeDXD(&ji->ea) > sizeof (ji->i_inline_ea)))
377 return -EIO;
378 if (le32_to_cpu(((struct jfs_ea_list *) &ji->i_inline_ea)->size)
379 != ea_size)
380 return -EIO;
381
382 memcpy(ealist, ji->i_inline_ea, ea_size);
383 return 0;
384}
385
386/*
387 * NAME: ea_read
388 *
389 * FUNCTION: copy EA data into user's buffer
390 *
391 * PARAMETERS:
392 * ip - Inode pointer
393 * ealist - Pointer to buffer to fill in with EA
394 *
395 * NOTES: If EA is inline calls ea_read_inline() to copy EA.
396 *
397 * RETURNS: 0 for success; other indicates failure
398 */
399static int ea_read(struct inode *ip, struct jfs_ea_list *ealist)
400{
401 struct super_block *sb = ip->i_sb;
402 struct jfs_inode_info *ji = JFS_IP(ip);
403 struct jfs_sb_info *sbi = JFS_SBI(sb);
404 int nblocks;
405 s64 blkno;
406 char *cp = (char *) ealist;
407 int i;
408 int nbytes, nb;
409 s32 bytes_to_read;
410 struct metapage *mp;
411
412 /* quick check for in-line EA */
413 if (ji->ea.flag & DXD_INLINE)
414 return ea_read_inline(ip, ealist);
415
416 nbytes = sizeDXD(&ji->ea);
417 if (!nbytes) {
418 jfs_error(sb, "ea_read: nbytes is 0");
419 return -EIO;
420 }
421
422 /*
423 * Figure out how many blocks were allocated when this EA list was
424 * originally written to disk.
425 */
426 nblocks = lengthDXD(&ji->ea) << sbi->l2nbperpage;
427 blkno = addressDXD(&ji->ea) << sbi->l2nbperpage;
428
429 /*
430 * I have found the disk blocks which were originally used to store
431 * the FEALIST. now i loop over each contiguous block copying the
432 * data into the buffer.
433 */
434 for (i = 0; i < nblocks; i += sbi->nbperpage) {
435 /*
436 * Determine how many bytes for this request, and round up to
437 * the nearest aggregate block size
438 */
439 nb = min(PSIZE, nbytes);
440 bytes_to_read =
441 ((((nb + sb->s_blocksize - 1)) >> sb->s_blocksize_bits))
442 << sb->s_blocksize_bits;
443
444 if (!(mp = read_metapage(ip, blkno + i, bytes_to_read, 1)))
445 return -EIO;
446
447 memcpy(cp, mp->data, nb);
448 release_metapage(mp);
449
450 cp += PSIZE;
451 nbytes -= nb;
452 }
453
454 return 0;
455}
456
457/*
458 * NAME: ea_get
459 *
460 * FUNCTION: Returns buffer containing existing extended attributes.
461 * The size of the buffer will be the larger of the existing
462 * attributes size, or min_size.
463 *
464 * The buffer, which may be inlined in the inode or in the
465 * page cache must be release by calling ea_release or ea_put
466 *
467 * PARAMETERS:
468 * inode - Inode pointer
469 * ea_buf - Structure to be populated with ealist and its metadata
470 * min_size- minimum size of buffer to be returned
471 *
472 * RETURNS: 0 for success; Other indicates failure
473 */
474static int ea_get(struct inode *inode, struct ea_buffer *ea_buf, int min_size)
475{
476 struct jfs_inode_info *ji = JFS_IP(inode);
477 struct super_block *sb = inode->i_sb;
478 int size;
479 int ea_size = sizeDXD(&ji->ea);
480 int blocks_needed, current_blocks;
481 s64 blkno;
482 int rc;
483 int quota_allocation = 0;
484
485 /* When fsck.jfs clears a bad ea, it doesn't clear the size */
486 if (ji->ea.flag == 0)
487 ea_size = 0;
488
489 if (ea_size == 0) {
490 if (min_size == 0) {
491 ea_buf->flag = 0;
492 ea_buf->max_size = 0;
493 ea_buf->xattr = NULL;
494 return 0;
495 }
496 if ((min_size <= sizeof (ji->i_inline_ea)) &&
497 (ji->mode2 & INLINEEA)) {
498 ea_buf->flag = EA_INLINE | EA_NEW;
499 ea_buf->max_size = sizeof (ji->i_inline_ea);
500 ea_buf->xattr = (struct jfs_ea_list *) ji->i_inline_ea;
501 DXDlength(&ea_buf->new_ea, 0);
502 DXDaddress(&ea_buf->new_ea, 0);
503 ea_buf->new_ea.flag = DXD_INLINE;
504 DXDsize(&ea_buf->new_ea, min_size);
505 return 0;
506 }
507 current_blocks = 0;
508 } else if (ji->ea.flag & DXD_INLINE) {
509 if (min_size <= sizeof (ji->i_inline_ea)) {
510 ea_buf->flag = EA_INLINE;
511 ea_buf->max_size = sizeof (ji->i_inline_ea);
512 ea_buf->xattr = (struct jfs_ea_list *) ji->i_inline_ea;
513 goto size_check;
514 }
515 current_blocks = 0;
516 } else {
517 if (!(ji->ea.flag & DXD_EXTENT)) {
518 jfs_error(sb, "ea_get: invalid ea.flag)");
519 return -EIO;
520 }
521 current_blocks = (ea_size + sb->s_blocksize - 1) >>
522 sb->s_blocksize_bits;
523 }
524 size = max(min_size, ea_size);
525
526 if (size > PSIZE) {
527 /*
528 * To keep the rest of the code simple. Allocate a
529 * contiguous buffer to work with
530 */
531 ea_buf->xattr = kmalloc(size, GFP_KERNEL);
532 if (ea_buf->xattr == NULL)
533 return -ENOMEM;
534
535 ea_buf->flag = EA_MALLOC;
536 ea_buf->max_size = (size + sb->s_blocksize - 1) &
537 ~(sb->s_blocksize - 1);
538
539 if (ea_size == 0)
540 return 0;
541
542 if ((rc = ea_read(inode, ea_buf->xattr))) {
543 kfree(ea_buf->xattr);
544 ea_buf->xattr = NULL;
545 return rc;
546 }
547 goto size_check;
548 }
549 blocks_needed = (min_size + sb->s_blocksize - 1) >>
550 sb->s_blocksize_bits;
551
552 if (blocks_needed > current_blocks) {
553 /* Allocate new blocks to quota. */
554 if (DQUOT_ALLOC_BLOCK(inode, blocks_needed))
555 return -EDQUOT;
556
557 quota_allocation = blocks_needed;
558
559 rc = dbAlloc(inode, INOHINT(inode), (s64) blocks_needed,
560 &blkno);
561 if (rc)
562 goto clean_up;
563
564 DXDlength(&ea_buf->new_ea, blocks_needed);
565 DXDaddress(&ea_buf->new_ea, blkno);
566 ea_buf->new_ea.flag = DXD_EXTENT;
567 DXDsize(&ea_buf->new_ea, min_size);
568
569 ea_buf->flag = EA_EXTENT | EA_NEW;
570
571 ea_buf->mp = get_metapage(inode, blkno,
572 blocks_needed << sb->s_blocksize_bits,
573 1);
574 if (ea_buf->mp == NULL) {
575 dbFree(inode, blkno, (s64) blocks_needed);
576 rc = -EIO;
577 goto clean_up;
578 }
579 ea_buf->xattr = ea_buf->mp->data;
580 ea_buf->max_size = (min_size + sb->s_blocksize - 1) &
581 ~(sb->s_blocksize - 1);
582 if (ea_size == 0)
583 return 0;
584 if ((rc = ea_read(inode, ea_buf->xattr))) {
585 discard_metapage(ea_buf->mp);
586 dbFree(inode, blkno, (s64) blocks_needed);
587 goto clean_up;
588 }
589 goto size_check;
590 }
591 ea_buf->flag = EA_EXTENT;
592 ea_buf->mp = read_metapage(inode, addressDXD(&ji->ea),
593 lengthDXD(&ji->ea) << sb->s_blocksize_bits,
594 1);
595 if (ea_buf->mp == NULL) {
596 rc = -EIO;
597 goto clean_up;
598 }
599 ea_buf->xattr = ea_buf->mp->data;
600 ea_buf->max_size = (ea_size + sb->s_blocksize - 1) &
601 ~(sb->s_blocksize - 1);
602
603 size_check:
604 if (EALIST_SIZE(ea_buf->xattr) != ea_size) {
605 printk(KERN_ERR "ea_get: invalid extended attribute\n");
606 dump_mem("xattr", ea_buf->xattr, ea_size);
607 ea_release(inode, ea_buf);
608 rc = -EIO;
609 goto clean_up;
610 }
611
612 return ea_size;
613
614 clean_up:
615 /* Rollback quota allocation */
616 if (quota_allocation)
617 DQUOT_FREE_BLOCK(inode, quota_allocation);
618
619 return (rc);
620}
621
622static void ea_release(struct inode *inode, struct ea_buffer *ea_buf)
623{
624 if (ea_buf->flag & EA_MALLOC)
625 kfree(ea_buf->xattr);
626 else if (ea_buf->flag & EA_EXTENT) {
627 assert(ea_buf->mp);
628 release_metapage(ea_buf->mp);
629
630 if (ea_buf->flag & EA_NEW)
631 dbFree(inode, addressDXD(&ea_buf->new_ea),
632 lengthDXD(&ea_buf->new_ea));
633 }
634}
635
4f4b401b
DK
636static int ea_put(tid_t tid, struct inode *inode, struct ea_buffer *ea_buf,
637 int new_size)
1da177e4
LT
638{
639 struct jfs_inode_info *ji = JFS_IP(inode);
640 unsigned long old_blocks, new_blocks;
641 int rc = 0;
1da177e4
LT
642
643 if (new_size == 0) {
644 ea_release(inode, ea_buf);
645 ea_buf = NULL;
646 } else if (ea_buf->flag & EA_INLINE) {
647 assert(new_size <= sizeof (ji->i_inline_ea));
648 ji->mode2 &= ~INLINEEA;
649 ea_buf->new_ea.flag = DXD_INLINE;
650 DXDsize(&ea_buf->new_ea, new_size);
651 DXDaddress(&ea_buf->new_ea, 0);
652 DXDlength(&ea_buf->new_ea, 0);
653 } else if (ea_buf->flag & EA_MALLOC) {
654 rc = ea_write(inode, ea_buf->xattr, new_size, &ea_buf->new_ea);
655 kfree(ea_buf->xattr);
656 } else if (ea_buf->flag & EA_NEW) {
657 /* We have already allocated a new dxd */
658 flush_metapage(ea_buf->mp);
659 } else {
660 /* ->xattr must point to original ea's metapage */
661 rc = ea_write(inode, ea_buf->xattr, new_size, &ea_buf->new_ea);
662 discard_metapage(ea_buf->mp);
663 }
664 if (rc)
665 return rc;
666
1da177e4
LT
667 old_blocks = new_blocks = 0;
668
669 if (ji->ea.flag & DXD_EXTENT) {
670 invalidate_dxd_metapages(inode, ji->ea);
671 old_blocks = lengthDXD(&ji->ea);
672 }
673
674 if (ea_buf) {
675 txEA(tid, inode, &ji->ea, &ea_buf->new_ea);
676 if (ea_buf->new_ea.flag & DXD_EXTENT) {
677 new_blocks = lengthDXD(&ea_buf->new_ea);
678 if (ji->ea.flag & DXD_INLINE)
679 ji->mode2 |= INLINEEA;
680 }
681 ji->ea = ea_buf->new_ea;
682 } else {
683 txEA(tid, inode, &ji->ea, NULL);
684 if (ji->ea.flag & DXD_INLINE)
685 ji->mode2 |= INLINEEA;
686 ji->ea.flag = 0;
687 ji->ea.size = 0;
688 }
689
690 /* If old blocks exist, they must be removed from quota allocation. */
691 if (old_blocks)
692 DQUOT_FREE_BLOCK(inode, old_blocks);
693
694 inode->i_ctime = CURRENT_TIME;
1da177e4 695
4f4b401b 696 return 0;
1da177e4
LT
697}
698
699/*
700 * can_set_system_xattr
701 *
702 * This code is specific to the system.* namespace. It contains policy
703 * which doesn't belong in the main xattr codepath.
704 */
705static int can_set_system_xattr(struct inode *inode, const char *name,
706 const void *value, size_t value_len)
707{
708#ifdef CONFIG_JFS_POSIX_ACL
709 struct posix_acl *acl;
710 int rc;
711
712 if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
713 return -EPERM;
714
715 /*
9a59f452 716 * POSIX_ACL_XATTR_ACCESS is tied to i_mode
1da177e4 717 */
9a59f452 718 if (strcmp(name, POSIX_ACL_XATTR_ACCESS) == 0) {
1da177e4
LT
719 acl = posix_acl_from_xattr(value, value_len);
720 if (IS_ERR(acl)) {
721 rc = PTR_ERR(acl);
722 printk(KERN_ERR "posix_acl_from_xattr returned %d\n",
723 rc);
724 return rc;
725 }
726 if (acl) {
727 mode_t mode = inode->i_mode;
728 rc = posix_acl_equiv_mode(acl, &mode);
729 posix_acl_release(acl);
730 if (rc < 0) {
731 printk(KERN_ERR
732 "posix_acl_equiv_mode returned %d\n",
733 rc);
734 return rc;
735 }
736 inode->i_mode = mode;
737 mark_inode_dirty(inode);
738 }
739 /*
740 * We're changing the ACL. Get rid of the cached one
741 */
742 acl =JFS_IP(inode)->i_acl;
743 if (acl != JFS_ACL_NOT_CACHED)
744 posix_acl_release(acl);
745 JFS_IP(inode)->i_acl = JFS_ACL_NOT_CACHED;
746
747 return 0;
9a59f452 748 } else if (strcmp(name, POSIX_ACL_XATTR_DEFAULT) == 0) {
1da177e4
LT
749 acl = posix_acl_from_xattr(value, value_len);
750 if (IS_ERR(acl)) {
751 rc = PTR_ERR(acl);
752 printk(KERN_ERR "posix_acl_from_xattr returned %d\n",
753 rc);
754 return rc;
755 }
756 posix_acl_release(acl);
757
758 /*
759 * We're changing the default ACL. Get rid of the cached one
760 */
761 acl =JFS_IP(inode)->i_default_acl;
762 if (acl && (acl != JFS_ACL_NOT_CACHED))
763 posix_acl_release(acl);
764 JFS_IP(inode)->i_default_acl = JFS_ACL_NOT_CACHED;
765
766 return 0;
767 }
768#endif /* CONFIG_JFS_POSIX_ACL */
769 return -EOPNOTSUPP;
770}
771
772static int can_set_xattr(struct inode *inode, const char *name,
773 const void *value, size_t value_len)
774{
775 if (IS_RDONLY(inode))
776 return -EROFS;
777
6211502d 778 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
1da177e4
LT
779 return -EPERM;
780
781 if(strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN) == 0)
782 /*
783 * "system.*"
784 */
785 return can_set_system_xattr(inode, name, value, value_len);
786
59192ed9 787 if(strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) == 0)
1da177e4
LT
788 return (capable(CAP_SYS_ADMIN) ? 0 : -EPERM);
789
790#ifdef CONFIG_JFS_SECURITY
791 if (strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN)
59192ed9 792 == 0)
1da177e4
LT
793 return 0; /* Leave it to the security module */
794#endif
795
796 if((strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN) != 0) &&
797 (strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN) != 0))
798 return -EOPNOTSUPP;
799
800 if (!S_ISREG(inode->i_mode) &&
801 (!S_ISDIR(inode->i_mode) || inode->i_mode &S_ISVTX))
802 return -EPERM;
803
804 return permission(inode, MAY_WRITE, NULL);
805}
806
4f4b401b
DK
807int __jfs_setxattr(tid_t tid, struct inode *inode, const char *name,
808 const void *value, size_t value_len, int flags)
1da177e4
LT
809{
810 struct jfs_ea_list *ealist;
811 struct jfs_ea *ea, *old_ea = NULL, *next_ea = NULL;
812 struct ea_buffer ea_buf;
813 int old_ea_size = 0;
814 int xattr_size;
815 int new_size;
816 int namelen = strlen(name);
817 char *os2name = NULL;
818 int found = 0;
819 int rc;
820 int length;
821
1da177e4
LT
822 if (strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN) == 0) {
823 os2name = kmalloc(namelen - XATTR_OS2_PREFIX_LEN + 1,
824 GFP_KERNEL);
825 if (!os2name)
826 return -ENOMEM;
827 strcpy(os2name, name + XATTR_OS2_PREFIX_LEN);
828 name = os2name;
829 namelen -= XATTR_OS2_PREFIX_LEN;
830 }
831
832 down_write(&JFS_IP(inode)->xattr_sem);
833
834 xattr_size = ea_get(inode, &ea_buf, 0);
835 if (xattr_size < 0) {
836 rc = xattr_size;
837 goto out;
838 }
839
840 again:
841 ealist = (struct jfs_ea_list *) ea_buf.xattr;
842 new_size = sizeof (struct jfs_ea_list);
843
844 if (xattr_size) {
845 for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist);
846 ea = NEXT_EA(ea)) {
847 if ((namelen == ea->namelen) &&
848 (memcmp(name, ea->name, namelen) == 0)) {
849 found = 1;
850 if (flags & XATTR_CREATE) {
851 rc = -EEXIST;
852 goto release;
853 }
854 old_ea = ea;
855 old_ea_size = EA_SIZE(ea);
856 next_ea = NEXT_EA(ea);
857 } else
858 new_size += EA_SIZE(ea);
859 }
860 }
861
862 if (!found) {
863 if (flags & XATTR_REPLACE) {
864 rc = -ENODATA;
865 goto release;
866 }
867 if (value == NULL) {
868 rc = 0;
869 goto release;
870 }
871 }
872 if (value)
873 new_size += sizeof (struct jfs_ea) + namelen + 1 + value_len;
874
875 if (new_size > ea_buf.max_size) {
876 /*
877 * We need to allocate more space for merged ea list.
878 * We should only have loop to again: once.
879 */
880 ea_release(inode, &ea_buf);
881 xattr_size = ea_get(inode, &ea_buf, new_size);
882 if (xattr_size < 0) {
883 rc = xattr_size;
884 goto out;
885 }
886 goto again;
887 }
888
889 /* Remove old ea of the same name */
890 if (found) {
891 /* number of bytes following target EA */
892 length = (char *) END_EALIST(ealist) - (char *) next_ea;
893 if (length > 0)
894 memmove(old_ea, next_ea, length);
895 xattr_size -= old_ea_size;
896 }
897
898 /* Add new entry to the end */
899 if (value) {
900 if (xattr_size == 0)
901 /* Completely new ea list */
902 xattr_size = sizeof (struct jfs_ea_list);
903
904 ea = (struct jfs_ea *) ((char *) ealist + xattr_size);
905 ea->flag = 0;
906 ea->namelen = namelen;
907 ea->valuelen = (cpu_to_le16(value_len));
908 memcpy(ea->name, name, namelen);
909 ea->name[namelen] = 0;
910 if (value_len)
911 memcpy(&ea->name[namelen + 1], value, value_len);
912 xattr_size += EA_SIZE(ea);
913 }
914
915 /* DEBUG - If we did this right, these number match */
916 if (xattr_size != new_size) {
917 printk(KERN_ERR
918 "jfs_xsetattr: xattr_size = %d, new_size = %d\n",
919 xattr_size, new_size);
920
921 rc = -EINVAL;
922 goto release;
923 }
924
925 /*
926 * If we're left with an empty list, there's no ea
927 */
928 if (new_size == sizeof (struct jfs_ea_list))
929 new_size = 0;
930
931 ealist->size = cpu_to_le32(new_size);
932
4f4b401b 933 rc = ea_put(tid, inode, &ea_buf, new_size);
1da177e4
LT
934
935 goto out;
936 release:
937 ea_release(inode, &ea_buf);
938 out:
939 up_write(&JFS_IP(inode)->xattr_sem);
940
259692bd 941 kfree(os2name);
1da177e4
LT
942
943 return rc;
944}
945
946int jfs_setxattr(struct dentry *dentry, const char *name, const void *value,
947 size_t value_len, int flags)
948{
4f4b401b
DK
949 struct inode *inode = dentry->d_inode;
950 struct jfs_inode_info *ji = JFS_IP(inode);
951 int rc;
952 tid_t tid;
953
954 if ((rc = can_set_xattr(inode, name, value, value_len)))
955 return rc;
956
1da177e4
LT
957 if (value == NULL) { /* empty EA, do not remove */
958 value = "";
959 value_len = 0;
960 }
961
4f4b401b
DK
962 tid = txBegin(inode->i_sb, 0);
963 down(&ji->commit_sem);
964 rc = __jfs_setxattr(tid, dentry->d_inode, name, value, value_len,
965 flags);
966 if (!rc)
967 rc = txCommit(tid, 1, &inode, 0);
968 txEnd(tid);
969 up(&ji->commit_sem);
970
971 return rc;
1da177e4
LT
972}
973
974static int can_get_xattr(struct inode *inode, const char *name)
975{
976#ifdef CONFIG_JFS_SECURITY
977 if(strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) == 0)
978 return 0;
979#endif
980
981 if(strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) == 0)
982 return (capable(CAP_SYS_ADMIN) ? 0 : -EPERM);
983
984 if(strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN) == 0)
985 return 0;
986
987 return permission(inode, MAY_READ, NULL);
988}
989
990ssize_t __jfs_getxattr(struct inode *inode, const char *name, void *data,
991 size_t buf_size)
992{
993 struct jfs_ea_list *ealist;
994 struct jfs_ea *ea;
995 struct ea_buffer ea_buf;
996 int xattr_size;
997 ssize_t size;
998 int namelen = strlen(name);
999 char *os2name = NULL;
1000 int rc;
1001 char *value;
1002
1003 if ((rc = can_get_xattr(inode, name)))
1004 return rc;
1005
1006 if (strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN) == 0) {
1007 os2name = kmalloc(namelen - XATTR_OS2_PREFIX_LEN + 1,
1008 GFP_KERNEL);
1009 if (!os2name)
1010 return -ENOMEM;
1011 strcpy(os2name, name + XATTR_OS2_PREFIX_LEN);
1012 name = os2name;
1013 namelen -= XATTR_OS2_PREFIX_LEN;
1014 }
1015
1016 down_read(&JFS_IP(inode)->xattr_sem);
1017
1018 xattr_size = ea_get(inode, &ea_buf, 0);
1019
1020 if (xattr_size < 0) {
1021 size = xattr_size;
1022 goto out;
1023 }
1024
1025 if (xattr_size == 0)
1026 goto not_found;
1027
1028 ealist = (struct jfs_ea_list *) ea_buf.xattr;
1029
1030 /* Find the named attribute */
1031 for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea))
1032 if ((namelen == ea->namelen) &&
1033 memcmp(name, ea->name, namelen) == 0) {
1034 /* Found it */
1035 size = le16_to_cpu(ea->valuelen);
1036 if (!data)
1037 goto release;
1038 else if (size > buf_size) {
1039 size = -ERANGE;
1040 goto release;
1041 }
1042 value = ((char *) &ea->name) + ea->namelen + 1;
1043 memcpy(data, value, size);
1044 goto release;
1045 }
1046 not_found:
1047 size = -ENODATA;
1048 release:
1049 ea_release(inode, &ea_buf);
1050 out:
1051 up_read(&JFS_IP(inode)->xattr_sem);
1052
259692bd 1053 kfree(os2name);
1da177e4
LT
1054
1055 return size;
1056}
1057
1058ssize_t jfs_getxattr(struct dentry *dentry, const char *name, void *data,
1059 size_t buf_size)
1060{
1061 int err;
1062
1063 err = __jfs_getxattr(dentry->d_inode, name, data, buf_size);
1064
1065 return err;
1066}
1067
1068/*
1069 * No special permissions are needed to list attributes except for trusted.*
1070 */
1071static inline int can_list(struct jfs_ea *ea)
1072{
1073 return (strncmp(ea->name, XATTR_TRUSTED_PREFIX,
1074 XATTR_TRUSTED_PREFIX_LEN) ||
1075 capable(CAP_SYS_ADMIN));
1076}
1077
1078ssize_t jfs_listxattr(struct dentry * dentry, char *data, size_t buf_size)
1079{
1080 struct inode *inode = dentry->d_inode;
1081 char *buffer;
1082 ssize_t size = 0;
1083 int xattr_size;
1084 struct jfs_ea_list *ealist;
1085 struct jfs_ea *ea;
1086 struct ea_buffer ea_buf;
1087
1088 down_read(&JFS_IP(inode)->xattr_sem);
1089
1090 xattr_size = ea_get(inode, &ea_buf, 0);
1091 if (xattr_size < 0) {
1092 size = xattr_size;
1093 goto out;
1094 }
1095
1096 if (xattr_size == 0)
1097 goto release;
1098
1099 ealist = (struct jfs_ea_list *) ea_buf.xattr;
1100
1101 /* compute required size of list */
1102 for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea)) {
1103 if (can_list(ea))
1104 size += name_size(ea) + 1;
1105 }
1106
1107 if (!data)
1108 goto release;
1109
1110 if (size > buf_size) {
1111 size = -ERANGE;
1112 goto release;
1113 }
1114
1115 /* Copy attribute names to buffer */
1116 buffer = data;
1117 for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea)) {
1118 if (can_list(ea)) {
1119 int namelen = copy_name(buffer, ea);
1120 buffer += namelen + 1;
1121 }
1122 }
1123
1124 release:
1125 ea_release(inode, &ea_buf);
1126 out:
1127 up_read(&JFS_IP(inode)->xattr_sem);
1128 return size;
1129}
1130
1131int jfs_removexattr(struct dentry *dentry, const char *name)
1132{
4f4b401b
DK
1133 struct inode *inode = dentry->d_inode;
1134 struct jfs_inode_info *ji = JFS_IP(inode);
1135 int rc;
1136 tid_t tid;
1137
1138 if ((rc = can_set_xattr(inode, name, NULL, 0)))
1139 return rc;
1140
1141 tid = txBegin(inode->i_sb, 0);
1142 down(&ji->commit_sem);
1143 rc = __jfs_setxattr(tid, dentry->d_inode, name, NULL, 0, XATTR_REPLACE);
1144 if (!rc)
1145 rc = txCommit(tid, 1, &inode, 0);
1146 txEnd(tid);
1147 up(&ji->commit_sem);
1148
1149 return rc;
1da177e4 1150}
This page took 0.08806 seconds and 5 git commands to generate.