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