Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[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 static int can_set_xattr(struct inode *inode, const char *name,
782                          const void *value, size_t value_len)
783 {
784         if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
785                 return can_set_system_xattr(inode, name, value, value_len);
786
787         /*
788          * Don't allow setting an attribute in an unknown namespace.
789          */
790         if (strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) &&
791             strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) &&
792             strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN) &&
793             strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN))
794                 return -EOPNOTSUPP;
795
796         if (!S_ISREG(inode->i_mode) &&
797             (!S_ISDIR(inode->i_mode) || inode->i_mode &S_ISVTX))
798                 return -EPERM;
799
800         return 0;
801 }
802
803 int __jfs_setxattr(tid_t tid, struct inode *inode, const char *name,
804                    const void *value, size_t value_len, int flags)
805 {
806         struct jfs_ea_list *ealist;
807         struct jfs_ea *ea, *old_ea = NULL, *next_ea = NULL;
808         struct ea_buffer ea_buf;
809         int old_ea_size = 0;
810         int xattr_size;
811         int new_size;
812         int namelen = strlen(name);
813         char *os2name = NULL;
814         int found = 0;
815         int rc;
816         int length;
817
818         if (strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN) == 0) {
819                 os2name = kmalloc(namelen - XATTR_OS2_PREFIX_LEN + 1,
820                                   GFP_KERNEL);
821                 if (!os2name)
822                         return -ENOMEM;
823                 strcpy(os2name, name + XATTR_OS2_PREFIX_LEN);
824                 name = os2name;
825                 namelen -= XATTR_OS2_PREFIX_LEN;
826         }
827
828         down_write(&JFS_IP(inode)->xattr_sem);
829
830         xattr_size = ea_get(inode, &ea_buf, 0);
831         if (xattr_size < 0) {
832                 rc = xattr_size;
833                 goto out;
834         }
835
836       again:
837         ealist = (struct jfs_ea_list *) ea_buf.xattr;
838         new_size = sizeof (struct jfs_ea_list);
839
840         if (xattr_size) {
841                 for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist);
842                      ea = NEXT_EA(ea)) {
843                         if ((namelen == ea->namelen) &&
844                             (memcmp(name, ea->name, namelen) == 0)) {
845                                 found = 1;
846                                 if (flags & XATTR_CREATE) {
847                                         rc = -EEXIST;
848                                         goto release;
849                                 }
850                                 old_ea = ea;
851                                 old_ea_size = EA_SIZE(ea);
852                                 next_ea = NEXT_EA(ea);
853                         } else
854                                 new_size += EA_SIZE(ea);
855                 }
856         }
857
858         if (!found) {
859                 if (flags & XATTR_REPLACE) {
860                         rc = -ENODATA;
861                         goto release;
862                 }
863                 if (value == NULL) {
864                         rc = 0;
865                         goto release;
866                 }
867         }
868         if (value)
869                 new_size += sizeof (struct jfs_ea) + namelen + 1 + value_len;
870
871         if (new_size > ea_buf.max_size) {
872                 /*
873                  * We need to allocate more space for merged ea list.
874                  * We should only have loop to again: once.
875                  */
876                 ea_release(inode, &ea_buf);
877                 xattr_size = ea_get(inode, &ea_buf, new_size);
878                 if (xattr_size < 0) {
879                         rc = xattr_size;
880                         goto out;
881                 }
882                 goto again;
883         }
884
885         /* Remove old ea of the same name */
886         if (found) {
887                 /* number of bytes following target EA */
888                 length = (char *) END_EALIST(ealist) - (char *) next_ea;
889                 if (length > 0)
890                         memmove(old_ea, next_ea, length);
891                 xattr_size -= old_ea_size;
892         }
893
894         /* Add new entry to the end */
895         if (value) {
896                 if (xattr_size == 0)
897                         /* Completely new ea list */
898                         xattr_size = sizeof (struct jfs_ea_list);
899
900                 ea = (struct jfs_ea *) ((char *) ealist + xattr_size);
901                 ea->flag = 0;
902                 ea->namelen = namelen;
903                 ea->valuelen = (cpu_to_le16(value_len));
904                 memcpy(ea->name, name, namelen);
905                 ea->name[namelen] = 0;
906                 if (value_len)
907                         memcpy(&ea->name[namelen + 1], value, value_len);
908                 xattr_size += EA_SIZE(ea);
909         }
910
911         /* DEBUG - If we did this right, these number match */
912         if (xattr_size != new_size) {
913                 printk(KERN_ERR
914                        "jfs_xsetattr: xattr_size = %d, new_size = %d\n",
915                        xattr_size, new_size);
916
917                 rc = -EINVAL;
918                 goto release;
919         }
920
921         /*
922          * If we're left with an empty list, there's no ea
923          */
924         if (new_size == sizeof (struct jfs_ea_list))
925                 new_size = 0;
926
927         ealist->size = cpu_to_le32(new_size);
928
929         rc = ea_put(tid, inode, &ea_buf, new_size);
930
931         goto out;
932       release:
933         ea_release(inode, &ea_buf);
934       out:
935         up_write(&JFS_IP(inode)->xattr_sem);
936
937         kfree(os2name);
938
939         return rc;
940 }
941
942 int jfs_setxattr(struct dentry *dentry, const char *name, const void *value,
943                  size_t value_len, int flags)
944 {
945         struct inode *inode = dentry->d_inode;
946         struct jfs_inode_info *ji = JFS_IP(inode);
947         int rc;
948         tid_t tid;
949
950         if ((rc = can_set_xattr(inode, name, value, value_len)))
951                 return rc;
952
953         if (value == NULL) {    /* empty EA, do not remove */
954                 value = "";
955                 value_len = 0;
956         }
957
958         tid = txBegin(inode->i_sb, 0);
959         mutex_lock(&ji->commit_mutex);
960         rc = __jfs_setxattr(tid, dentry->d_inode, name, value, value_len,
961                             flags);
962         if (!rc)
963                 rc = txCommit(tid, 1, &inode, 0);
964         txEnd(tid);
965         mutex_unlock(&ji->commit_mutex);
966
967         return rc;
968 }
969
970 ssize_t __jfs_getxattr(struct inode *inode, const char *name, void *data,
971                        size_t buf_size)
972 {
973         struct jfs_ea_list *ealist;
974         struct jfs_ea *ea;
975         struct ea_buffer ea_buf;
976         int xattr_size;
977         ssize_t size;
978         int namelen = strlen(name);
979         char *os2name = NULL;
980         char *value;
981
982         if (strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN) == 0) {
983                 os2name = kmalloc(namelen - XATTR_OS2_PREFIX_LEN + 1,
984                                   GFP_KERNEL);
985                 if (!os2name)
986                         return -ENOMEM;
987                 strcpy(os2name, name + XATTR_OS2_PREFIX_LEN);
988                 name = os2name;
989                 namelen -= XATTR_OS2_PREFIX_LEN;
990         }
991
992         down_read(&JFS_IP(inode)->xattr_sem);
993
994         xattr_size = ea_get(inode, &ea_buf, 0);
995
996         if (xattr_size < 0) {
997                 size = xattr_size;
998                 goto out;
999         }
1000
1001         if (xattr_size == 0)
1002                 goto not_found;
1003
1004         ealist = (struct jfs_ea_list *) ea_buf.xattr;
1005
1006         /* Find the named attribute */
1007         for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea))
1008                 if ((namelen == ea->namelen) &&
1009                     memcmp(name, ea->name, namelen) == 0) {
1010                         /* Found it */
1011                         size = le16_to_cpu(ea->valuelen);
1012                         if (!data)
1013                                 goto release;
1014                         else if (size > buf_size) {
1015                                 size = -ERANGE;
1016                                 goto release;
1017                         }
1018                         value = ((char *) &ea->name) + ea->namelen + 1;
1019                         memcpy(data, value, size);
1020                         goto release;
1021                 }
1022       not_found:
1023         size = -ENODATA;
1024       release:
1025         ea_release(inode, &ea_buf);
1026       out:
1027         up_read(&JFS_IP(inode)->xattr_sem);
1028
1029         kfree(os2name);
1030
1031         return size;
1032 }
1033
1034 ssize_t jfs_getxattr(struct dentry *dentry, const char *name, void *data,
1035                      size_t buf_size)
1036 {
1037         int err;
1038
1039         err = __jfs_getxattr(dentry->d_inode, name, data, buf_size);
1040
1041         return err;
1042 }
1043
1044 /*
1045  * No special permissions are needed to list attributes except for trusted.*
1046  */
1047 static inline int can_list(struct jfs_ea *ea)
1048 {
1049         return (strncmp(ea->name, XATTR_TRUSTED_PREFIX,
1050                             XATTR_TRUSTED_PREFIX_LEN) ||
1051                 capable(CAP_SYS_ADMIN));
1052 }
1053
1054 ssize_t jfs_listxattr(struct dentry * dentry, char *data, size_t buf_size)
1055 {
1056         struct inode *inode = dentry->d_inode;
1057         char *buffer;
1058         ssize_t size = 0;
1059         int xattr_size;
1060         struct jfs_ea_list *ealist;
1061         struct jfs_ea *ea;
1062         struct ea_buffer ea_buf;
1063
1064         down_read(&JFS_IP(inode)->xattr_sem);
1065
1066         xattr_size = ea_get(inode, &ea_buf, 0);
1067         if (xattr_size < 0) {
1068                 size = xattr_size;
1069                 goto out;
1070         }
1071
1072         if (xattr_size == 0)
1073                 goto release;
1074
1075         ealist = (struct jfs_ea_list *) ea_buf.xattr;
1076
1077         /* compute required size of list */
1078         for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea)) {
1079                 if (can_list(ea))
1080                         size += name_size(ea) + 1;
1081         }
1082
1083         if (!data)
1084                 goto release;
1085
1086         if (size > buf_size) {
1087                 size = -ERANGE;
1088                 goto release;
1089         }
1090
1091         /* Copy attribute names to buffer */
1092         buffer = data;
1093         for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea)) {
1094                 if (can_list(ea)) {
1095                         int namelen = copy_name(buffer, ea);
1096                         buffer += namelen + 1;
1097                 }
1098         }
1099
1100       release:
1101         ea_release(inode, &ea_buf);
1102       out:
1103         up_read(&JFS_IP(inode)->xattr_sem);
1104         return size;
1105 }
1106
1107 int jfs_removexattr(struct dentry *dentry, const char *name)
1108 {
1109         struct inode *inode = dentry->d_inode;
1110         struct jfs_inode_info *ji = JFS_IP(inode);
1111         int rc;
1112         tid_t tid;
1113
1114         if ((rc = can_set_xattr(inode, name, NULL, 0)))
1115                 return rc;
1116
1117         tid = txBegin(inode->i_sb, 0);
1118         mutex_lock(&ji->commit_mutex);
1119         rc = __jfs_setxattr(tid, dentry->d_inode, name, NULL, 0, XATTR_REPLACE);
1120         if (!rc)
1121                 rc = txCommit(tid, 1, &inode, 0);
1122         txEnd(tid);
1123         mutex_unlock(&ji->commit_mutex);
1124
1125         return rc;
1126 }
1127
1128 #ifdef CONFIG_JFS_SECURITY
1129 int jfs_init_security(tid_t tid, struct inode *inode, struct inode *dir)
1130 {
1131         int rc;
1132         size_t len;
1133         void *value;
1134         char *suffix;
1135         char *name;
1136
1137         rc = security_inode_init_security(inode, dir, &suffix, &value, &len);
1138         if (rc) {
1139                 if (rc == -EOPNOTSUPP)
1140                         return 0;
1141                 return rc;
1142         }
1143         name = kmalloc(XATTR_SECURITY_PREFIX_LEN + 1 + strlen(suffix),
1144                        GFP_NOFS);
1145         if (!name) {
1146                 rc = -ENOMEM;
1147                 goto kmalloc_failed;
1148         }
1149         strcpy(name, XATTR_SECURITY_PREFIX);
1150         strcpy(name + XATTR_SECURITY_PREFIX_LEN, suffix);
1151
1152         rc = __jfs_setxattr(tid, inode, name, value, len, 0);
1153
1154         kfree(name);
1155 kmalloc_failed:
1156         kfree(suffix);
1157         kfree(value);
1158
1159         return rc;
1160 }
1161 #endif