Whamcloud - gitweb
Branch HEAD
[fs/lustre-release.git] / lustre / kernel_patches / patches / linux-2.4.20-xattr-0.8.54-hp.patch
1  Documentation/Configure.help  |   66 ++
2  arch/ia64/defconfig           |    7 
3  fs/Config.in                  |   14 
4  fs/Makefile                   |    3 
5  fs/ext2/Makefile              |    4 
6  fs/ext2/file.c                |    5 
7  fs/ext2/ialloc.c              |    2 
8  fs/ext2/inode.c               |   34 -
9  fs/ext2/namei.c               |   14 
10  fs/ext2/super.c               |   29 
11  fs/ext2/symlink.c             |   14 
12  fs/ext2/xattr.c               | 1212 +++++++++++++++++++++++++++++++++++++++++
13  fs/ext2/xattr_user.c          |  103 +++
14  fs/ext3/Makefile              |    9 
15  fs/ext3/ext3-exports.c        |   13 
16  fs/ext3/file.c                |    5 
17  fs/ext3/ialloc.c              |    2 
18  fs/ext3/inode.c               |   35 -
19  fs/ext3/namei.c               |   21 
20  fs/ext3/super.c               |   36 +
21  fs/ext3/symlink.c             |   14 
22  fs/ext3/xattr.c               | 1225 ++++++++++++++++++++++++++++++++++++++++++
23  fs/ext3/xattr_user.c          |  111 +++
24  fs/jfs/jfs_xattr.h            |    6 
25  fs/jfs/xattr.c                |    6 
26  fs/mbcache.c                  |  648 ++++++++++++++++++++++
27  include/linux/cache_def.h     |   15 
28  include/linux/errno.h         |    4 
29  include/linux/ext2_fs.h       |   31 -
30  include/linux/ext2_xattr.h    |  157 +++++
31  include/linux/ext3_fs.h       |   31 -
32  include/linux/ext3_jbd.h      |    8 
33  include/linux/ext3_xattr.h    |  157 +++++
34  include/linux/fs.h            |    2 
35  include/linux/mbcache.h       |   69 ++
36  kernel/ksyms.c                |    4 
37  mm/vmscan.c                   |   35 +
38  62 files changed, 4343 insertions(+), 182 deletions(-)
39
40 --- linux/Documentation/Configure.help~linux-2.4.20-xattr-0.8.54-hp     Fri May 16 08:39:23 2003
41 +++ linux-mmonroe/Documentation/Configure.help  Fri May 16 08:43:00 2003
42 @@ -15309,6 +15309,39 @@ CONFIG_EXT2_FS
43    be compiled as a module, and so this could be dangerous.  Most
44    everyone wants to say Y here.
45  
46 +Ext2 extended attributes
47 +CONFIG_EXT2_FS_XATTR
48 +  Extended attributes are name:value pairs associated with inodes by
49 +  the kernel or by users (see the attr(5) manual page, or visit
50 +  <http://acl.bestbits.at/> for details).
51 +
52 +  If unsure, say N.
53 +
54 +Ext2 extended attribute block sharing
55 +CONFIG_EXT2_FS_XATTR_SHARING
56 +  This options enables code for sharing identical extended attribute
57 +  blocks among multiple inodes.
58 +
59 +  Usually, say Y.
60 +
61 +Ext2 extended user attributes
62 +CONFIG_EXT2_FS_XATTR_USER
63 +  This option enables extended user attributes on ext2. Processes can
64 +  associate extended user attributes with inodes to store additional
65 +  information such as the character encoding of files, etc. (see the
66 +  attr(5) manual page, or visit <http://acl.bestbits.at/> for details).
67 +
68 +  If unsure, say N.
69 +
70 +Ext2 trusted extended attributes
71 +CONFIG_EXT2_FS_XATTR_TRUSTED
72 +  This option enables extended attributes on ext2 that are accessible
73 +  (and visible) only to users capable of CAP_SYS_ADMIN. Usually this
74 +  is only the super user. Trusted extended attributes are meant for
75 +  implementing system/security services.
76 +
77 +  If unsure, say N.
78 +
79  Ext3 journalling file system support (EXPERIMENTAL)
80  CONFIG_EXT3_FS
81    This is the journalling version of the Second extended file system
82 @@ -15341,6 +15374,39 @@ CONFIG_EXT3_FS
83    of your root partition (the one containing the directory /) cannot
84    be compiled as a module, and so this may be dangerous.
85  
86 +Ext3 extended attributes
87 +CONFIG_EXT3_FS_XATTR
88 +  Extended attributes are name:value pairs associated with inodes by
89 +  the kernel or by users (see the attr(5) manual page, or visit
90 +  <http://acl.bestbits.at/> for details).
91 +
92 +  If unsure, say N.
93 +
94 +Ext3 extended attribute block sharing
95 +CONFIG_EXT3_FS_XATTR_SHARING
96 +  This options enables code for sharing identical extended attribute
97 +  blocks among multiple inodes.
98 +
99 +  Usually, say Y.
100 +
101 +Ext3 extended user attributes
102 +CONFIG_EXT3_FS_XATTR_USER
103 +  This option enables extended user attributes on ext3. Processes can
104 +  associate extended user attributes with inodes to store additional
105 +  information such as the character encoding of files, etc. (see the
106 +  attr(5) manual page, or visit <http://acl.bestbits.at/> for details).
107 +
108 +  If unsure, say N.
109 +
110 +Ext3 trusted extended attributes
111 +CONFIG_EXT3_FS_XATTR_TRUSTED
112 +  This option enables extended attributes on ext3 that are accessible
113 +  (and visible) only to users capable of CAP_SYS_ADMIN. Usually this
114 +  is only the super user. Trusted extended attributes are meant for
115 +  implementing system/security services.
116 +
117 +  If unsure, say N.
118 +
119  Journal Block Device support (JBD for ext3) (EXPERIMENTAL)
120  CONFIG_JBD
121    This is a generic journalling layer for block devices.  It is
122 --- linux/arch/ia64/defconfig~linux-2.4.20-xattr-0.8.54-hp      Fri May 16 08:39:14 2003
123 +++ linux-mmonroe/arch/ia64/defconfig   Fri May 16 08:43:00 2003
124 @@ -1,6 +1,13 @@
125  #
126  # Automatically generated make config: don't edit
127  #
128 +CONFIG_EXT3_FS_XATTR=y
129 +# CONFIG_EXT3_FS_XATTR_SHARING is not set
130 +# CONFIG_EXT3_FS_XATTR_USER is not set
131 +# CONFIG_EXT2_FS_XATTR is not set
132 +# CONFIG_EXT2_FS_XATTR_SHARING is not set
133 +# CONFIG_EXT2_FS_XATTR_USER is not set
134 +# CONFIG_FS_MBCACHE is not set
135  
136  #
137  # Code maturity level options
138 --- linux/fs/Config.in~linux-2.4.20-xattr-0.8.54-hp     Fri May 16 08:39:14 2003
139 +++ linux-mmonroe/fs/Config.in  Fri May 16 08:43:01 2003
140 @@ -35,6 +35,11 @@ dep_mbool '  Debug Befs' CONFIG_BEFS_DEB
141  dep_tristate 'BFS file system support (EXPERIMENTAL)' CONFIG_BFS_FS $CONFIG_EXPERIMENTAL
142  
143  tristate 'Ext3 journalling file system support' CONFIG_EXT3_FS
144 +dep_mbool '  Ext3 extended attributes' CONFIG_EXT3_FS_XATTR $CONFIG_EXT3_FS
145 +dep_bool '    Ext3 extended attribute block sharing' \
146 +    CONFIG_EXT3_FS_XATTR_SHARING $CONFIG_EXT3_FS_XATTR
147 +dep_bool '    Ext3 extended user attributes' \
148 +    CONFIG_EXT3_FS_XATTR_USER $CONFIG_EXT3_FS_XATTR
149  # CONFIG_JBD could be its own option (even modular), but until there are
150  # other users than ext3, we will simply make it be the same as CONFIG_EXT3_FS
151  # dep_tristate '  Journal Block Device support (JBD for ext3)' CONFIG_JBD $CONFIG_EXT3_FS
152 @@ -98,6 +103,11 @@ dep_mbool '  QNX4FS write support (DANGE
153  tristate 'ROM file system support' CONFIG_ROMFS_FS
154  
155  tristate 'Second extended fs support' CONFIG_EXT2_FS
156 +dep_mbool '  Ext2 extended attributes' CONFIG_EXT2_FS_XATTR $CONFIG_EXT2_FS
157 +dep_bool '    Ext2 extended attribute block sharing' \
158 +    CONFIG_EXT2_FS_XATTR_SHARING $CONFIG_EXT2_FS_XATTR
159 +dep_bool '    Ext2 extended user attributes' \
160 +    CONFIG_EXT2_FS_XATTR_USER $CONFIG_EXT2_FS_XATTR
161  
162  tristate 'System V/Xenix/V7/Coherent file system support' CONFIG_SYSV_FS
163  
164 @@ -176,6 +186,10 @@ else
165     define_tristate CONFIG_ZISOFS_FS n
166  fi
167  
168 +# Meta block cache for Extended Attributes (ext2/ext3)
169 +#tristate 'Meta block cache' CONFIG_FS_MBCACHE
170 +define_tristate CONFIG_FS_MBCACHE y
171 +
172  mainmenu_option next_comment
173  comment 'Partition Types'
174  source fs/partitions/Config.in
175 --- linux/fs/Makefile~linux-2.4.20-xattr-0.8.54-hp      Fri May 16 08:42:46 2003
176 +++ linux-mmonroe/fs/Makefile   Fri May 16 08:43:01 2003
177 @@ -80,6 +80,9 @@ obj-y                         += binfmt_script.o
178  
179  obj-$(CONFIG_BINFMT_ELF)       += binfmt_elf.o
180  
181 +export-objs += mbcache.o
182 +obj-$(CONFIG_FS_MBCACHE)       += mbcache.o
183 +
184  # persistent filesystems
185  obj-y += $(join $(subdir-y),$(subdir-y:%=/%.o))
186  
187 --- linux/fs/ext2/Makefile~linux-2.4.20-xattr-0.8.54-hp Thu Oct 11 08:05:18 2001
188 +++ linux-mmonroe/fs/ext2/Makefile      Fri May 16 08:43:01 2003
189 @@ -13,4 +13,8 @@ obj-y    := balloc.o bitmap.o dir.o file
190                 ioctl.o namei.o super.o symlink.o
191  obj-m    := $(O_TARGET)
192  
193 +export-objs += xattr.o
194 +obj-$(CONFIG_EXT2_FS_XATTR) += xattr.o
195 +obj-$(CONFIG_EXT2_FS_XATTR_USER) += xattr_user.o
196 +
197  include $(TOPDIR)/Rules.make
198 --- linux/fs/ext2/file.c~linux-2.4.20-xattr-0.8.54-hp   Thu Oct 11 08:05:18 2001
199 +++ linux-mmonroe/fs/ext2/file.c        Fri May 16 08:43:01 2003
200 @@ -20,6 +20,7 @@
201  
202  #include <linux/fs.h>
203  #include <linux/ext2_fs.h>
204 +#include <linux/ext2_xattr.h>
205  #include <linux/sched.h>
206  
207  /*
208 @@ -51,4 +52,8 @@ struct file_operations ext2_file_operati
209  
210  struct inode_operations ext2_file_inode_operations = {
211         truncate:       ext2_truncate,
212 +       setxattr:       ext2_setxattr,
213 +       getxattr:       ext2_getxattr,
214 +       listxattr:      ext2_listxattr,
215 +       removexattr:    ext2_removexattr,
216  };
217 --- linux/fs/ext2/ialloc.c~linux-2.4.20-xattr-0.8.54-hp Thu Nov 28 15:53:15 2002
218 +++ linux-mmonroe/fs/ext2/ialloc.c      Fri May 16 08:43:01 2003
219 @@ -15,6 +15,7 @@
220  #include <linux/config.h>
221  #include <linux/fs.h>
222  #include <linux/ext2_fs.h>
223 +#include <linux/ext2_xattr.h>
224  #include <linux/locks.h>
225  #include <linux/quotaops.h>
226  
227 @@ -167,6 +168,7 @@ void ext2_free_inode (struct inode * ino
228          */
229         if (!is_bad_inode(inode)) {
230                 /* Quota is already initialized in iput() */
231 +               ext2_xattr_delete_inode(inode);
232                 DQUOT_FREE_INODE(inode);
233                 DQUOT_DROP(inode);
234         }
235 --- linux/fs/ext2/inode.c~linux-2.4.20-xattr-0.8.54-hp  Thu Nov 28 15:53:15 2002
236 +++ linux-mmonroe/fs/ext2/inode.c       Fri May 16 08:43:01 2003
237 @@ -39,6 +39,18 @@ MODULE_LICENSE("GPL");
238  static int ext2_update_inode(struct inode * inode, int do_sync);
239  
240  /*
241 + * Test whether an inode is a fast symlink.
242 + */
243 +static inline int ext2_inode_is_fast_symlink(struct inode *inode)
244 +{
245 +       int ea_blocks = inode->u.ext2_i.i_file_acl ?
246 +               (inode->i_sb->s_blocksize >> 9) : 0;
247 +
248 +       return (S_ISLNK(inode->i_mode) &&
249 +               inode->i_blocks - ea_blocks == 0);
250 +}
251 +
252 +/*
253   * Called at each iput()
254   */
255  void ext2_put_inode (struct inode * inode)
256 @@ -53,9 +65,7 @@ void ext2_delete_inode (struct inode * i
257  {
258         lock_kernel();
259  
260 -       if (is_bad_inode(inode) ||
261 -           inode->i_ino == EXT2_ACL_IDX_INO ||
262 -           inode->i_ino == EXT2_ACL_DATA_INO)
263 +       if (is_bad_inode(inode))
264                 goto no_delete;
265         inode->u.ext2_i.i_dtime = CURRENT_TIME;
266         mark_inode_dirty(inode);
267 @@ -801,6 +811,8 @@ void ext2_truncate (struct inode * inode
268         if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
269             S_ISLNK(inode->i_mode)))
270                 return;
271 +       if (ext2_inode_is_fast_symlink(inode))
272 +               return;
273         if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
274                 return;
275  
276 @@ -888,8 +900,7 @@ void ext2_read_inode (struct inode * ino
277         unsigned long offset;
278         struct ext2_group_desc * gdp;
279  
280 -       if ((inode->i_ino != EXT2_ROOT_INO && inode->i_ino != EXT2_ACL_IDX_INO &&
281 -            inode->i_ino != EXT2_ACL_DATA_INO &&
282 +       if ((inode->i_ino != EXT2_ROOT_INO &&
283              inode->i_ino < EXT2_FIRST_INO(inode->i_sb)) ||
284             inode->i_ino > le32_to_cpu(inode->i_sb->u.ext2_sb.s_es->s_inodes_count)) {
285                 ext2_error (inode->i_sb, "ext2_read_inode",
286 @@ -974,10 +985,7 @@ void ext2_read_inode (struct inode * ino
287         for (block = 0; block < EXT2_N_BLOCKS; block++)
288                 inode->u.ext2_i.i_data[block] = raw_inode->i_block[block];
289  
290 -       if (inode->i_ino == EXT2_ACL_IDX_INO ||
291 -           inode->i_ino == EXT2_ACL_DATA_INO)
292 -               /* Nothing to do */ ;
293 -       else if (S_ISREG(inode->i_mode)) {
294 +       if (S_ISREG(inode->i_mode)) {
295                 inode->i_op = &ext2_file_inode_operations;
296                 inode->i_fop = &ext2_file_operations;
297                 inode->i_mapping->a_ops = &ext2_aops;
298 @@ -986,15 +994,17 @@ void ext2_read_inode (struct inode * ino
299                 inode->i_fop = &ext2_dir_operations;
300                 inode->i_mapping->a_ops = &ext2_aops;
301         } else if (S_ISLNK(inode->i_mode)) {
302 -               if (!inode->i_blocks)
303 +               if (ext2_inode_is_fast_symlink(inode))
304                         inode->i_op = &ext2_fast_symlink_inode_operations;
305                 else {
306 -                       inode->i_op = &page_symlink_inode_operations;
307 +                       inode->i_op = &ext2_symlink_inode_operations;
308                         inode->i_mapping->a_ops = &ext2_aops;
309                 }
310 -       } else 
311 +       } else {
312 +               inode->i_op = &ext2_special_inode_operations;
313                 init_special_inode(inode, inode->i_mode,
314                                    le32_to_cpu(raw_inode->i_block[0]));
315 +       }
316         brelse (bh);
317         inode->i_attr_flags = 0;
318         if (inode->u.ext2_i.i_flags & EXT2_SYNC_FL) {
319 --- linux/fs/ext2/namei.c~linux-2.4.20-xattr-0.8.54-hp  Wed Oct  3 22:57:36 2001
320 +++ linux-mmonroe/fs/ext2/namei.c       Fri May 16 08:43:01 2003
321 @@ -31,6 +31,7 @@
322  
323  #include <linux/fs.h>
324  #include <linux/ext2_fs.h>
325 +#include <linux/ext2_xattr.h>
326  #include <linux/pagemap.h>
327  
328  /*
329 @@ -136,7 +137,7 @@ static int ext2_symlink (struct inode * 
330  
331         if (l > sizeof (inode->u.ext2_i.i_data)) {
332                 /* slow symlink */
333 -               inode->i_op = &page_symlink_inode_operations;
334 +               inode->i_op = &ext2_symlink_inode_operations;
335                 inode->i_mapping->a_ops = &ext2_aops;
336                 err = block_symlink(inode, symname, l);
337                 if (err)
338 @@ -345,4 +346,15 @@ struct inode_operations ext2_dir_inode_o
339         rmdir:          ext2_rmdir,
340         mknod:          ext2_mknod,
341         rename:         ext2_rename,
342 +       setxattr:       ext2_setxattr,
343 +       getxattr:       ext2_getxattr,
344 +       listxattr:      ext2_listxattr,
345 +       removexattr:    ext2_removexattr,
346 +};
347 +
348 +struct inode_operations ext2_special_inode_operations = {
349 +       setxattr:       ext2_setxattr,
350 +       getxattr:       ext2_getxattr,
351 +       listxattr:      ext2_listxattr,
352 +       removexattr:    ext2_removexattr,
353  };
354 --- linux/fs/ext2/super.c~linux-2.4.20-xattr-0.8.54-hp  Thu Nov 28 15:53:15 2002
355 +++ linux-mmonroe/fs/ext2/super.c       Fri May 16 08:43:01 2003
356 @@ -21,6 +21,7 @@
357  #include <linux/string.h>
358  #include <linux/fs.h>
359  #include <linux/ext2_fs.h>
360 +#include <linux/ext2_xattr.h>
361  #include <linux/slab.h>
362  #include <linux/init.h>
363  #include <linux/locks.h>
364 @@ -125,6 +126,7 @@ void ext2_put_super (struct super_block 
365         int db_count;
366         int i;
367  
368 +       ext2_xattr_put_super(sb);
369         if (!(sb->s_flags & MS_RDONLY)) {
370                 struct ext2_super_block *es = EXT2_SB(sb)->s_es;
371  
372 @@ -175,6 +177,13 @@ static int parse_options (char * options
373              this_char = strtok (NULL, ",")) {
374                 if ((value = strchr (this_char, '=')) != NULL)
375                         *value++ = 0;
376 +#ifdef CONFIG_EXT2_FS_XATTR_USER
377 +               if (!strcmp (this_char, "user_xattr"))
378 +                       set_opt (*mount_options, XATTR_USER);
379 +               else if (!strcmp (this_char, "nouser_xattr"))
380 +                       clear_opt (*mount_options, XATTR_USER);
381 +               else
382 +#endif
383                 if (!strcmp (this_char, "bsddf"))
384                         clear_opt (*mount_options, MINIX_DF);
385                 else if (!strcmp (this_char, "nouid32")) {
386 @@ -424,6 +433,9 @@ struct super_block * ext2_read_super (st
387             blocksize = BLOCK_SIZE;
388  
389         sb->u.ext2_sb.s_mount_opt = 0;
390 +#ifdef CONFIG_EXT2_FS_XATTR_USER
391 +       /* set_opt (sb->u.ext2_sb.s_mount_opt, XATTR_USER); */
392 +#endif
393         if (!parse_options ((char *) data, &sb_block, &resuid, &resgid,
394             &sb->u.ext2_sb.s_mount_opt)) {
395                 return NULL;
396 @@ -813,12 +825,27 @@ static DECLARE_FSTYPE_DEV(ext2_fs_type, 
397  
398  static int __init init_ext2_fs(void)
399  {
400 -        return register_filesystem(&ext2_fs_type);
401 +       int error = init_ext2_xattr();
402 +       if (error)
403 +               return error;
404 +       error = init_ext2_xattr_user();
405 +       if (error)
406 +               goto fail;
407 +       error = register_filesystem(&ext2_fs_type);
408 +       if (!error)
409 +               return 0;
410 +
411 +       exit_ext2_xattr_user();
412 +fail:
413 +       exit_ext2_xattr();
414 +       return error;
415  }
416  
417  static void __exit exit_ext2_fs(void)
418  {
419         unregister_filesystem(&ext2_fs_type);
420 +       exit_ext2_xattr_user();
421 +       exit_ext2_xattr();
422  }
423  
424  EXPORT_NO_SYMBOLS;
425 --- linux/fs/ext2/symlink.c~linux-2.4.20-xattr-0.8.54-hp        Wed Sep 27 13:41:33 2000
426 +++ linux-mmonroe/fs/ext2/symlink.c     Fri May 16 08:43:01 2003
427 @@ -19,6 +19,7 @@
428  
429  #include <linux/fs.h>
430  #include <linux/ext2_fs.h>
431 +#include <linux/ext2_xattr.h>
432  
433  static int ext2_readlink(struct dentry *dentry, char *buffer, int buflen)
434  {
435 @@ -32,7 +33,20 @@ static int ext2_follow_link(struct dentr
436         return vfs_follow_link(nd, s);
437  }
438  
439 +struct inode_operations ext2_symlink_inode_operations = {
440 +       readlink:       page_readlink,
441 +       follow_link:    page_follow_link,
442 +       setxattr:       ext2_setxattr,
443 +       getxattr:       ext2_getxattr,
444 +       listxattr:      ext2_listxattr,
445 +       removexattr:    ext2_removexattr,
446 +};
447 +
448  struct inode_operations ext2_fast_symlink_inode_operations = {
449         readlink:       ext2_readlink,
450         follow_link:    ext2_follow_link,
451 +       setxattr:       ext2_setxattr,
452 +       getxattr:       ext2_getxattr,
453 +       listxattr:      ext2_listxattr,
454 +       removexattr:    ext2_removexattr,
455  };
456 --- /dev/null   Mon May 20 21:11:23 2002
457 +++ linux-mmonroe/fs/ext2/xattr.c       Fri May 16 08:43:01 2003
458 @@ -0,0 +1,1212 @@
459 +/*
460 + * linux/fs/ext2/xattr.c
461 + *
462 + * Copyright (C) 2001 by Andreas Gruenbacher, <a.gruenbacher@computer.org>
463 + *
464 + * Fix by Harrison Xing <harrison@mountainviewdata.com>.
465 + * Extended attributes for symlinks and special files added per
466 + *  suggestion of Luka Renko <luka.renko@hermes.si>.
467 + */
468 +
469 +/*
470 + * Extended attributes are stored on disk blocks allocated outside of
471 + * any inode. The i_file_acl field is then made to point to this allocated
472 + * block. If all extended attributes of an inode are identical, these
473 + * inodes may share the same extended attribute block. Such situations
474 + * are automatically detected by keeping a cache of recent attribute block
475 + * numbers and hashes over the block's contents in memory.
476 + *
477 + *
478 + * Extended attribute block layout:
479 + *
480 + *   +------------------+
481 + *   | header           |
482 + *   | entry 1          | |
483 + *   | entry 2          | | growing downwards
484 + *   | entry 3          | v
485 + *   | four null bytes  |
486 + *   | . . .            |
487 + *   | value 1          | ^
488 + *   | value 3          | | growing upwards
489 + *   | value 2          | |
490 + *   +------------------+
491 + *
492 + * The block header is followed by multiple entry descriptors. These entry
493 + * descriptors are variable in size, and alligned to EXT2_XATTR_PAD
494 + * byte boundaries. The entry descriptors are sorted by attribute name,
495 + * so that two extended attribute blocks can be compared efficiently.
496 + *
497 + * Attribute values are aligned to the end of the block, stored in
498 + * no specific order. They are also padded to EXT2_XATTR_PAD byte
499 + * boundaries. No additional gaps are left between them.
500 + *
501 + * Locking strategy
502 + * ----------------
503 + * The VFS already holds the BKL and the inode->i_sem semaphore when any of
504 + * the xattr inode operations are called, so we are guaranteed that only one
505 + * processes accesses extended attributes of an inode at any time.
506 + *
507 + * For writing we also grab the ext2_xattr_sem semaphore. This ensures that
508 + * only a single process is modifying an extended attribute block, even
509 + * if the block is shared among inodes.
510 + *
511 + * Note for porting to 2.5
512 + * -----------------------
513 + * The BKL will no longer be held in the xattr inode operations.
514 + */
515 +
516 +#include <linux/module.h>
517 +#include <linux/locks.h>
518 +#include <linux/slab.h>
519 +#include <linux/fs.h>
520 +#include <linux/ext2_fs.h>
521 +#include <linux/ext2_xattr.h>
522 +#include <linux/mbcache.h>
523 +#include <linux/quotaops.h>
524 +#include <asm/semaphore.h>
525 +#include <linux/compatmac.h>
526 +
527 +/* These symbols may be needed by a module. */
528 +EXPORT_SYMBOL(ext2_xattr_register);
529 +EXPORT_SYMBOL(ext2_xattr_unregister);
530 +EXPORT_SYMBOL(ext2_xattr_get);
531 +EXPORT_SYMBOL(ext2_xattr_list);
532 +EXPORT_SYMBOL(ext2_xattr_set);
533 +
534 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0)
535 +# define mark_buffer_dirty(bh) mark_buffer_dirty(bh, 1)
536 +#endif
537 +
538 +#define HDR(bh) ((struct ext2_xattr_header *)((bh)->b_data))
539 +#define ENTRY(ptr) ((struct ext2_xattr_entry *)(ptr))
540 +#define FIRST_ENTRY(bh) ENTRY(HDR(bh)+1)
541 +#define IS_LAST_ENTRY(entry) (*(__u32 *)(entry) == 0)
542 +
543 +#ifdef EXT2_XATTR_DEBUG
544 +# define ea_idebug(inode, f...) do { \
545 +               printk(KERN_DEBUG "inode %s:%ld: ", \
546 +                       kdevname(inode->i_dev), inode->i_ino); \
547 +               printk(f); \
548 +               printk("\n"); \
549 +       } while (0)
550 +# define ea_bdebug(bh, f...) do { \
551 +               printk(KERN_DEBUG "block %s:%ld: ", \
552 +                       kdevname(bh->b_dev), bh->b_blocknr); \
553 +               printk(f); \
554 +               printk("\n"); \
555 +       } while (0)
556 +#else
557 +# define ea_idebug(f...)
558 +# define ea_bdebug(f...)
559 +#endif
560 +
561 +static int ext2_xattr_set2(struct inode *, struct buffer_head *,
562 +                          struct ext2_xattr_header *);
563 +
564 +#ifdef CONFIG_EXT2_FS_XATTR_SHARING
565 +
566 +static int ext2_xattr_cache_insert(struct buffer_head *);
567 +static struct buffer_head *ext2_xattr_cache_find(struct inode *,
568 +                                                struct ext2_xattr_header *);
569 +static void ext2_xattr_cache_remove(struct buffer_head *);
570 +static void ext2_xattr_rehash(struct ext2_xattr_header *,
571 +                             struct ext2_xattr_entry *);
572 +
573 +static struct mb_cache *ext2_xattr_cache;
574 +
575 +#else
576 +# define ext2_xattr_cache_insert(bh) 0
577 +# define ext2_xattr_cache_find(inode, header) NULL
578 +# define ext2_xattr_cache_remove(bh) while(0) {}
579 +# define ext2_xattr_rehash(header, entry) while(0) {}
580 +#endif
581 +
582 +/*
583 + * If a file system does not share extended attributes among inodes,
584 + * we should not need the ext2_xattr_sem semaphore. However, the
585 + * filesystem may still contain shared blocks, so we always take
586 + * the lock.
587 + */
588 +
589 +DECLARE_MUTEX(ext2_xattr_sem);
590 +
591 +static inline int
592 +ext2_xattr_new_block(struct inode *inode, int * errp, int force)
593 +{
594 +       struct super_block *sb = inode->i_sb;
595 +       int goal = le32_to_cpu(EXT2_SB(sb)->s_es->s_first_data_block) +
596 +               EXT2_I(inode)->i_block_group * EXT2_BLOCKS_PER_GROUP(sb);
597 +
598 +       /* How can we enforce the allocation? */
599 +       int block = ext2_new_block(inode, goal, 0, 0, errp);
600 +#ifdef OLD_QUOTAS
601 +       if (!*errp)
602 +               inode->i_blocks += inode->i_sb->s_blocksize >> 9;
603 +#endif
604 +       return block;
605 +}
606 +
607 +static inline int
608 +ext2_xattr_quota_alloc(struct inode *inode, int force)
609 +{
610 +       /* How can we enforce the allocation? */
611 +#ifdef OLD_QUOTAS
612 +       int error = DQUOT_ALLOC_BLOCK(inode->i_sb, inode, 1);
613 +       if (!error)
614 +               inode->i_blocks += inode->i_sb->s_blocksize >> 9;
615 +#else
616 +       int error = DQUOT_ALLOC_BLOCK(inode, 1);
617 +#endif
618 +       return error;
619 +}
620 +
621 +#ifdef OLD_QUOTAS
622 +
623 +static inline void
624 +ext2_xattr_quota_free(struct inode *inode)
625 +{
626 +       DQUOT_FREE_BLOCK(inode->i_sb, inode, 1);
627 +       inode->i_blocks -= inode->i_sb->s_blocksize >> 9;
628 +}
629 +
630 +static inline void
631 +ext2_xattr_free_block(struct inode * inode, unsigned long block)
632 +{
633 +       ext2_free_blocks(inode, block, 1);
634 +       inode->i_blocks -= inode->i_sb->s_blocksize >> 9;
635 +}
636 +
637 +#else
638 +# define ext2_xattr_quota_free(inode) \
639 +       DQUOT_FREE_BLOCK(inode, 1)
640 +# define ext2_xattr_free_block(inode, block) \
641 +       ext2_free_blocks(inode, block, 1)
642 +#endif
643 +
644 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,18)
645 +
646 +static inline struct buffer_head *
647 +sb_bread(struct super_block *sb, int block)
648 +{
649 +       return bread(sb->s_dev, block, sb->s_blocksize);
650 +}
651 +
652 +static inline struct buffer_head *
653 +sb_getblk(struct super_block *sb, int block)
654 +{
655 +       return getblk(sb->s_dev, block, sb->s_blocksize);
656 +}
657 +
658 +#endif
659 +
660 +struct ext2_xattr_handler *ext2_xattr_handlers[EXT2_XATTR_INDEX_MAX];
661 +rwlock_t ext2_handler_lock = RW_LOCK_UNLOCKED;
662 +
663 +int
664 +ext2_xattr_register(int name_index, struct ext2_xattr_handler *handler)
665 +{
666 +       int error = -EINVAL;
667 +
668 +       if (name_index > 0 && name_index <= EXT2_XATTR_INDEX_MAX) {
669 +               write_lock(&ext2_handler_lock);
670 +               if (!ext2_xattr_handlers[name_index-1]) {
671 +                       ext2_xattr_handlers[name_index-1] = handler;
672 +                       error = 0;
673 +               }
674 +               write_unlock(&ext2_handler_lock);
675 +       }
676 +       return error;
677 +}
678 +
679 +void
680 +ext2_xattr_unregister(int name_index, struct ext2_xattr_handler *handler)
681 +{
682 +       if (name_index > 0 && name_index <= EXT2_XATTR_INDEX_MAX) {
683 +               write_lock(&ext2_handler_lock);
684 +               ext2_xattr_handlers[name_index-1] = NULL;
685 +               write_unlock(&ext2_handler_lock);
686 +       }
687 +}
688 +
689 +static inline const char *
690 +strcmp_prefix(const char *a, const char *a_prefix)
691 +{
692 +       while (*a_prefix && *a == *a_prefix) {
693 +               a++;
694 +               a_prefix++;
695 +       }
696 +       return *a_prefix ? NULL : a;
697 +}
698 +
699 +/*
700 + * Decode the extended attribute name, and translate it into
701 + * the name_index and name suffix.
702 + */
703 +static struct ext2_xattr_handler *
704 +ext2_xattr_resolve_name(const char **name)
705 +{
706 +       struct ext2_xattr_handler *handler = NULL;
707 +       int i;
708 +
709 +       if (!*name)
710 +               return NULL;
711 +       read_lock(&ext2_handler_lock);
712 +       for (i=0; i<EXT2_XATTR_INDEX_MAX; i++) {
713 +               if (ext2_xattr_handlers[i]) {
714 +                       const char *n = strcmp_prefix(*name,
715 +                               ext2_xattr_handlers[i]->prefix);
716 +                       if (n) {
717 +                               handler = ext2_xattr_handlers[i];
718 +                               *name = n;
719 +                               break;
720 +                       }
721 +               }
722 +       }
723 +       read_unlock(&ext2_handler_lock);
724 +       return handler;
725 +}
726 +
727 +static inline struct ext2_xattr_handler *
728 +ext2_xattr_handler(int name_index)
729 +{
730 +       struct ext2_xattr_handler *handler = NULL;
731 +       if (name_index > 0 && name_index <= EXT2_XATTR_INDEX_MAX) {
732 +               read_lock(&ext2_handler_lock);
733 +               handler = ext2_xattr_handlers[name_index-1];
734 +               read_unlock(&ext2_handler_lock);
735 +       }
736 +       return handler;
737 +}
738 +
739 +/*
740 + * Inode operation getxattr()
741 + *
742 + * dentry->d_inode->i_sem down
743 + * BKL held [before 2.5.x]
744 + */
745 +ssize_t
746 +ext2_getxattr(struct dentry *dentry, const char *name,
747 +             void *buffer, size_t size)
748 +{
749 +       struct ext2_xattr_handler *handler;
750 +       struct inode *inode = dentry->d_inode;
751 +
752 +       handler = ext2_xattr_resolve_name(&name);
753 +       if (!handler)
754 +               return -ENOTSUP;
755 +       return handler->get(inode, name, buffer, size);
756 +}
757 +
758 +/*
759 + * Inode operation listxattr()
760 + *
761 + * dentry->d_inode->i_sem down
762 + * BKL held [before 2.5.x]
763 + */
764 +ssize_t
765 +ext2_listxattr(struct dentry *dentry, char *buffer, size_t size)
766 +{
767 +       return ext2_xattr_list(dentry->d_inode, buffer, size);
768 +}
769 +
770 +/*
771 + * Inode operation setxattr()
772 + *
773 + * dentry->d_inode->i_sem down
774 + * BKL held [before 2.5.x]
775 + */
776 +int
777 +ext2_setxattr(struct dentry *dentry, const char *name,
778 +             const void *value, size_t size, int flags)
779 +{
780 +       struct ext2_xattr_handler *handler;
781 +       struct inode *inode = dentry->d_inode;
782 +
783 +       if (size == 0)
784 +               value = "";  /* empty EA, do not remove */
785 +       handler = ext2_xattr_resolve_name(&name);
786 +       if (!handler)
787 +               return -ENOTSUP;
788 +       return handler->set(inode, name, value, size, flags);
789 +}
790 +
791 +/*
792 + * Inode operation removexattr()
793 + *
794 + * dentry->d_inode->i_sem down
795 + * BKL held [before 2.5.x]
796 + */
797 +int
798 +ext2_removexattr(struct dentry *dentry, const char *name)
799 +{
800 +       struct ext2_xattr_handler *handler;
801 +       struct inode *inode = dentry->d_inode;
802 +
803 +       handler = ext2_xattr_resolve_name(&name);
804 +       if (!handler)
805 +               return -ENOTSUP;
806 +       return handler->set(inode, name, NULL, 0, XATTR_REPLACE);
807 +}
808 +
809 +/*
810 + * ext2_xattr_get()
811 + *
812 + * Copy an extended attribute into the buffer
813 + * provided, or compute the buffer size required.
814 + * Buffer is NULL to compute the size of the buffer required.
815 + *
816 + * Returns a negative error number on failure, or the number of bytes
817 + * used / required on success.
818 + */
819 +int
820 +ext2_xattr_get(struct inode *inode, int name_index, const char *name,
821 +              void *buffer, size_t buffer_size)
822 +{
823 +       struct buffer_head *bh = NULL;
824 +       struct ext2_xattr_entry *entry;
825 +       unsigned int block, size;
826 +       char *end;
827 +       int name_len, error;
828 +
829 +       ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
830 +                 name_index, name, buffer, (long)buffer_size);
831 +
832 +       if (name == NULL)
833 +               return -EINVAL;
834 +       if (!EXT2_I(inode)->i_file_acl)
835 +               return -ENOATTR;
836 +       block = EXT2_I(inode)->i_file_acl;
837 +       ea_idebug(inode, "reading block %d", block);
838 +       bh = sb_bread(inode->i_sb, block);
839 +       if (!bh)
840 +               return -EIO;
841 +       ea_bdebug(bh, "b_count=%d, refcount=%d",
842 +               atomic_read(&(bh->b_count)), le32_to_cpu(HDR(bh)->h_refcount));
843 +       end = bh->b_data + bh->b_size;
844 +       if (HDR(bh)->h_magic != cpu_to_le32(EXT2_XATTR_MAGIC) ||
845 +           HDR(bh)->h_blocks != cpu_to_le32(1)) {
846 +bad_block:     ext2_error(inode->i_sb, "ext2_xattr_get",
847 +                       "inode %ld: bad block %d", inode->i_ino, block);
848 +               error = -EIO;
849 +               goto cleanup;
850 +       }
851 +       /* find named attribute */
852 +       name_len = strlen(name);
853 +
854 +       error = -ERANGE;
855 +       if (name_len > 255)
856 +               goto cleanup;
857 +       entry = FIRST_ENTRY(bh);
858 +       while (!IS_LAST_ENTRY(entry)) {
859 +               struct ext2_xattr_entry *next =
860 +                       EXT2_XATTR_NEXT(entry);
861 +               if ((char *)next >= end)
862 +                       goto bad_block;
863 +               if (name_index == entry->e_name_index &&
864 +                   name_len == entry->e_name_len &&
865 +                   memcmp(name, entry->e_name, name_len) == 0)
866 +                       goto found;
867 +               entry = next;
868 +       }
869 +       /* Check the remaining name entries */
870 +       while (!IS_LAST_ENTRY(entry)) {
871 +               struct ext2_xattr_entry *next =
872 +                       EXT2_XATTR_NEXT(entry);
873 +               if ((char *)next >= end)
874 +                       goto bad_block;
875 +               entry = next;
876 +       }
877 +       if (ext2_xattr_cache_insert(bh))
878 +               ea_idebug(inode, "cache insert failed");
879 +       error = -ENOATTR;
880 +       goto cleanup;
881 +found:
882 +       /* check the buffer size */
883 +       if (entry->e_value_block != 0)
884 +               goto bad_block;
885 +       size = le32_to_cpu(entry->e_value_size);
886 +       if (size > inode->i_sb->s_blocksize ||
887 +           le16_to_cpu(entry->e_value_offs) + size > inode->i_sb->s_blocksize)
888 +               goto bad_block;
889 +
890 +       if (ext2_xattr_cache_insert(bh))
891 +               ea_idebug(inode, "cache insert failed");
892 +       if (buffer) {
893 +               error = -ERANGE;
894 +               if (size > buffer_size)
895 +                       goto cleanup;
896 +               /* return value of attribute */
897 +               memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
898 +                       size);
899 +       }
900 +       error = size;
901 +
902 +cleanup:
903 +       brelse(bh);
904 +
905 +       return error;
906 +}
907 +
908 +/*
909 + * ext2_xattr_list()
910 + *
911 + * Copy a list of attribute names into the buffer
912 + * provided, or compute the buffer size required.
913 + * Buffer is NULL to compute the size of the buffer required.
914 + *
915 + * Returns a negative error number on failure, or the number of bytes
916 + * used / required on success.
917 + */
918 +int
919 +ext2_xattr_list(struct inode *inode, char *buffer, size_t buffer_size)
920 +{
921 +       struct buffer_head *bh = NULL;
922 +       struct ext2_xattr_entry *entry;
923 +       unsigned int block, size = 0;
924 +       char *buf, *end;
925 +       int error;
926 +
927 +       ea_idebug(inode, "buffer=%p, buffer_size=%ld",
928 +                 buffer, (long)buffer_size);
929 +
930 +       if (!EXT2_I(inode)->i_file_acl)
931 +               return 0;
932 +       block = EXT2_I(inode)->i_file_acl;
933 +       ea_idebug(inode, "reading block %d", block);
934 +       bh = sb_bread(inode->i_sb, block);
935 +       if (!bh)
936 +               return -EIO;
937 +       ea_bdebug(bh, "b_count=%d, refcount=%d",
938 +               atomic_read(&(bh->b_count)), le32_to_cpu(HDR(bh)->h_refcount));
939 +       end = bh->b_data + bh->b_size;
940 +       if (HDR(bh)->h_magic != cpu_to_le32(EXT2_XATTR_MAGIC) ||
941 +           HDR(bh)->h_blocks != cpu_to_le32(1)) {
942 +bad_block:     ext2_error(inode->i_sb, "ext2_xattr_list",
943 +                       "inode %ld: bad block %d", inode->i_ino, block);
944 +               error = -EIO;
945 +               goto cleanup;
946 +       }
947 +       /* compute the size required for the list of attribute names */
948 +       for (entry = FIRST_ENTRY(bh); !IS_LAST_ENTRY(entry);
949 +            entry = EXT2_XATTR_NEXT(entry)) {
950 +               struct ext2_xattr_handler *handler;
951 +               struct ext2_xattr_entry *next =
952 +                       EXT2_XATTR_NEXT(entry);
953 +               if ((char *)next >= end)
954 +                       goto bad_block;
955 +
956 +               handler = ext2_xattr_handler(entry->e_name_index);
957 +               if (handler)
958 +                       size += handler->list(NULL, inode, entry->e_name,
959 +                                             entry->e_name_len);
960 +       }
961 +
962 +       if (ext2_xattr_cache_insert(bh))
963 +               ea_idebug(inode, "cache insert failed");
964 +       if (!buffer) {
965 +               error = size;
966 +               goto cleanup;
967 +       } else {
968 +               error = -ERANGE;
969 +               if (size > buffer_size)
970 +                       goto cleanup;
971 +       }
972 +
973 +       /* list the attribute names */
974 +       buf = buffer;
975 +       for (entry = FIRST_ENTRY(bh); !IS_LAST_ENTRY(entry);
976 +            entry = EXT2_XATTR_NEXT(entry)) {
977 +               struct ext2_xattr_handler *handler;
978 +               
979 +               handler = ext2_xattr_handler(entry->e_name_index);
980 +               if (handler)
981 +                       buf += handler->list(buf, inode, entry->e_name,
982 +                                            entry->e_name_len);
983 +       }
984 +       error = size;
985 +
986 +cleanup:
987 +       brelse(bh);
988 +
989 +       return error;
990 +}
991 +
992 +/*
993 + * If the EXT2_FEATURE_COMPAT_EXT_ATTR feature of this file system is
994 + * not set, set it.
995 + */
996 +static void ext2_xattr_update_super_block(struct super_block *sb)
997 +{
998 +       if (EXT2_HAS_COMPAT_FEATURE(sb, EXT2_FEATURE_COMPAT_EXT_ATTR))
999 +               return;
1000 +
1001 +       lock_super(sb);
1002 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0)
1003 +       EXT2_SB(sb)->s_feature_compat |= EXT2_FEATURE_COMPAT_EXT_ATTR;
1004 +#endif
1005 +       EXT2_SB(sb)->s_es->s_feature_compat |=
1006 +               cpu_to_le32(EXT2_FEATURE_COMPAT_EXT_ATTR);
1007 +       sb->s_dirt = 1;
1008 +       mark_buffer_dirty(EXT2_SB(sb)->s_sbh);
1009 +       unlock_super(sb);
1010 +}
1011 +
1012 +/*
1013 + * ext2_xattr_set()
1014 + *
1015 + * Create, replace or remove an extended attribute for this inode. Buffer
1016 + * is NULL to remove an existing extended attribute, and non-NULL to
1017 + * either replace an existing extended attribute, or create a new extended
1018 + * attribute. The flags XATTR_REPLACE and XATTR_CREATE
1019 + * specify that an extended attribute must exist and must not exist
1020 + * previous to the call, respectively.
1021 + *
1022 + * Returns 0, or a negative error number on failure.
1023 + */
1024 +int
1025 +ext2_xattr_set(struct inode *inode, int name_index, const char *name,
1026 +              const void *value, size_t value_len, int flags)
1027 +{
1028 +       struct super_block *sb = inode->i_sb;
1029 +       struct buffer_head *bh = NULL;
1030 +       struct ext2_xattr_header *header = NULL;
1031 +       struct ext2_xattr_entry *here, *last;
1032 +       unsigned int name_len;
1033 +       int block = EXT2_I(inode)->i_file_acl;
1034 +       int min_offs = sb->s_blocksize, not_found = 1, free, error;
1035 +       char *end;
1036 +       
1037 +       /*
1038 +        * header -- Points either into bh, or to a temporarily
1039 +        *           allocated buffer.
1040 +        * here -- The named entry found, or the place for inserting, within
1041 +        *         the block pointed to by header.
1042 +        * last -- Points right after the last named entry within the block
1043 +        *         pointed to by header.
1044 +        * min_offs -- The offset of the first value (values are aligned
1045 +        *             towards the end of the block).
1046 +        * end -- Points right after the block pointed to by header.
1047 +        */
1048 +       
1049 +       ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
1050 +                 name_index, name, value, (long)value_len);
1051 +
1052 +       if (IS_RDONLY(inode))
1053 +               return -EROFS;
1054 +       if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
1055 +               return -EPERM;
1056 +       if (value == NULL)
1057 +               value_len = 0;
1058 +       if (name == NULL)
1059 +               return -EINVAL;
1060 +       name_len = strlen(name);
1061 +       if (name_len > 255 || value_len > sb->s_blocksize)
1062 +               return -ERANGE;
1063 +       down(&ext2_xattr_sem);
1064 +
1065 +       if (block) {
1066 +               /* The inode already has an extended attribute block. */
1067 +
1068 +               bh = sb_bread(sb, block);
1069 +               error = -EIO;
1070 +               if (!bh)
1071 +                       goto cleanup;
1072 +               ea_bdebug(bh, "b_count=%d, refcount=%d",
1073 +                       atomic_read(&(bh->b_count)),
1074 +                       le32_to_cpu(HDR(bh)->h_refcount));
1075 +               header = HDR(bh);
1076 +               end = bh->b_data + bh->b_size;
1077 +               if (header->h_magic != cpu_to_le32(EXT2_XATTR_MAGIC) ||
1078 +                   header->h_blocks != cpu_to_le32(1)) {
1079 +bad_block:             ext2_error(sb, "ext2_xattr_set",
1080 +                               "inode %ld: bad block %d", inode->i_ino, block);
1081 +                       error = -EIO;
1082 +                       goto cleanup;
1083 +               }
1084 +               /* Find the named attribute. */
1085 +               here = FIRST_ENTRY(bh);
1086 +               while (!IS_LAST_ENTRY(here)) {
1087 +                       struct ext2_xattr_entry *next = EXT2_XATTR_NEXT(here);
1088 +                       if ((char *)next >= end)
1089 +                               goto bad_block;
1090 +                       if (!here->e_value_block && here->e_value_size) {
1091 +                               int offs = le16_to_cpu(here->e_value_offs);
1092 +                               if (offs < min_offs)
1093 +                                       min_offs = offs;
1094 +                       }
1095 +                       not_found = name_index - here->e_name_index;
1096 +                       if (!not_found)
1097 +                               not_found = name_len - here->e_name_len;
1098 +                       if (!not_found)
1099 +                               not_found = memcmp(name, here->e_name,name_len);
1100 +                       if (not_found <= 0)
1101 +                               break;
1102 +                       here = next;
1103 +               }
1104 +               last = here;
1105 +               /* We still need to compute min_offs and last. */
1106 +               while (!IS_LAST_ENTRY(last)) {
1107 +                       struct ext2_xattr_entry *next = EXT2_XATTR_NEXT(last);
1108 +                       if ((char *)next >= end)
1109 +                               goto bad_block;
1110 +                       if (!last->e_value_block && last->e_value_size) {
1111 +                               int offs = le16_to_cpu(last->e_value_offs);
1112 +                               if (offs < min_offs)
1113 +                                       min_offs = offs;
1114 +                       }
1115 +                       last = next;
1116 +               }
1117 +
1118 +               /* Check whether we have enough space left. */
1119 +               free = min_offs - ((char*)last - (char*)header) - sizeof(__u32);
1120 +       } else {
1121 +               /* We will use a new extended attribute block. */
1122 +               free = sb->s_blocksize -
1123 +                       sizeof(struct ext2_xattr_header) - sizeof(__u32);
1124 +               here = last = NULL;  /* avoid gcc uninitialized warning. */
1125 +       }
1126 +
1127 +       if (not_found) {
1128 +               /* Request to remove a nonexistent attribute? */
1129 +               error = -ENOATTR;
1130 +               if (flags & XATTR_REPLACE)
1131 +                       goto cleanup;
1132 +               error = 0;
1133 +               if (value == NULL)
1134 +                       goto cleanup;
1135 +               else
1136 +                       free -= EXT2_XATTR_LEN(name_len);
1137 +       } else {
1138 +               /* Request to create an existing attribute? */
1139 +               error = -EEXIST;
1140 +               if (flags & XATTR_CREATE)
1141 +                       goto cleanup;
1142 +               if (!here->e_value_block && here->e_value_size) {
1143 +                       unsigned int size = le32_to_cpu(here->e_value_size);
1144 +
1145 +                       if (le16_to_cpu(here->e_value_offs) + size > 
1146 +                           sb->s_blocksize || size > sb->s_blocksize)
1147 +                               goto bad_block;
1148 +                       free += EXT2_XATTR_SIZE(size);
1149 +               }
1150 +       }
1151 +       free -= EXT2_XATTR_SIZE(value_len);
1152 +       error = -ENOSPC;
1153 +       if (free < 0)
1154 +               goto cleanup;
1155 +
1156 +       /* Here we know that we can set the new attribute. */
1157 +
1158 +       if (header) {
1159 +               if (header->h_refcount == cpu_to_le32(1)) {
1160 +                       ea_bdebug(bh, "modifying in-place");
1161 +                       ext2_xattr_cache_remove(bh);
1162 +               } else {
1163 +                       int offset;
1164 +
1165 +                       ea_bdebug(bh, "cloning");
1166 +                       header = kmalloc(bh->b_size, GFP_KERNEL);
1167 +                       error = -ENOMEM;
1168 +                       if (header == NULL)
1169 +                               goto cleanup;
1170 +                       memcpy(header, HDR(bh), bh->b_size);
1171 +                       header->h_refcount = cpu_to_le32(1);
1172 +                       offset = (char *)header - bh->b_data;
1173 +                       here = ENTRY((char *)here + offset);
1174 +                       last = ENTRY((char *)last + offset);
1175 +               }
1176 +       } else {
1177 +               /* Allocate a buffer where we construct the new block. */
1178 +               header = kmalloc(sb->s_blocksize, GFP_KERNEL);
1179 +               error = -ENOMEM;
1180 +               if (header == NULL)
1181 +                       goto cleanup;
1182 +               memset(header, 0, sb->s_blocksize);
1183 +               end = (char *)header + sb->s_blocksize;
1184 +               header->h_magic = cpu_to_le32(EXT2_XATTR_MAGIC);
1185 +               header->h_blocks = header->h_refcount = cpu_to_le32(1);
1186 +               last = here = ENTRY(header+1);
1187 +       }
1188 +
1189 +       if (not_found) {
1190 +               /* Insert the new name. */
1191 +               int size = EXT2_XATTR_LEN(name_len);
1192 +               int rest = (char *)last - (char *)here;
1193 +               memmove((char *)here + size, here, rest);
1194 +               memset(here, 0, size);
1195 +               here->e_name_index = name_index;
1196 +               here->e_name_len = name_len;
1197 +               memcpy(here->e_name, name, name_len);
1198 +       } else {
1199 +               /* Remove the old value. */
1200 +               if (!here->e_value_block && here->e_value_size) {
1201 +                       char *first_val = (char *)header + min_offs;
1202 +                       int offs = le16_to_cpu(here->e_value_offs);
1203 +                       char *val = (char *)header + offs;
1204 +                       size_t size = EXT2_XATTR_SIZE(
1205 +                               le32_to_cpu(here->e_value_size));
1206 +                       memmove(first_val + size, first_val, val - first_val);
1207 +                       memset(first_val, 0, size);
1208 +                       here->e_value_offs = 0;
1209 +                       min_offs += size;
1210 +
1211 +                       /* Adjust all value offsets. */
1212 +                       last = ENTRY(header+1);
1213 +                       while (!IS_LAST_ENTRY(last)) {
1214 +                               int o = le16_to_cpu(last->e_value_offs);
1215 +                               if (!last->e_value_block && o < offs)
1216 +                                       last->e_value_offs =
1217 +                                               cpu_to_le16(o + size);
1218 +                               last = EXT2_XATTR_NEXT(last);
1219 +                       }
1220 +               }
1221 +               if (value == NULL) {
1222 +                       /* Remove this attribute. */
1223 +                       if (EXT2_XATTR_NEXT(ENTRY(header+1)) == last) {
1224 +                               /* This block is now empty. */
1225 +                               error = ext2_xattr_set2(inode, bh, NULL);
1226 +                               goto cleanup;
1227 +                       } else {
1228 +                               /* Remove the old name. */
1229 +                               int size = EXT2_XATTR_LEN(name_len);
1230 +                               last = ENTRY((char *)last - size);
1231 +                               memmove(here, (char*)here + size,
1232 +                                       (char*)last - (char*)here);
1233 +                               memset(last, 0, size);
1234 +                       }
1235 +               }
1236 +       }
1237 +
1238 +       if (value != NULL) {
1239 +               /* Insert the new value. */
1240 +               here->e_value_size = cpu_to_le32(value_len);
1241 +               if (value_len) {
1242 +                       size_t size = EXT2_XATTR_SIZE(value_len);
1243 +                       char *val = (char *)header + min_offs - size;
1244 +                       here->e_value_offs =
1245 +                               cpu_to_le16((char *)val - (char *)header);
1246 +                       memset(val + size - EXT2_XATTR_PAD, 0,
1247 +                              EXT2_XATTR_PAD); /* Clear the pad bytes. */
1248 +                       memcpy(val, value, value_len);
1249 +               }
1250 +       }
1251 +       ext2_xattr_rehash(header, here);
1252 +
1253 +       error = ext2_xattr_set2(inode, bh, header);
1254 +
1255 +cleanup:
1256 +       brelse(bh);
1257 +       if (!(bh && header == HDR(bh)))
1258 +               kfree(header);
1259 +       up(&ext2_xattr_sem);
1260 +
1261 +       return error;
1262 +}
1263 +
1264 +/*
1265 + * Second half of ext2_xattr_set(): Update the file system.
1266 + */
1267 +static int
1268 +ext2_xattr_set2(struct inode *inode, struct buffer_head *old_bh,
1269 +               struct ext2_xattr_header *header)
1270 +{
1271 +       struct super_block *sb = inode->i_sb;
1272 +       struct buffer_head *new_bh = NULL;
1273 +       int error;
1274 +
1275 +       if (header) {
1276 +               new_bh = ext2_xattr_cache_find(inode, header);
1277 +               if (new_bh) {
1278 +                       /*
1279 +                        * We found an identical block in the cache.
1280 +                        * The old block will be released after updating
1281 +                        * the inode.
1282 +                        */
1283 +                       ea_bdebug(old_bh, "reusing block %ld",
1284 +                               new_bh->b_blocknr);
1285 +                       
1286 +                       error = -EDQUOT;
1287 +                       if (ext2_xattr_quota_alloc(inode, 1))
1288 +                               goto cleanup;
1289 +                       
1290 +                       HDR(new_bh)->h_refcount = cpu_to_le32(
1291 +                               le32_to_cpu(HDR(new_bh)->h_refcount) + 1);
1292 +                       ea_bdebug(new_bh, "refcount now=%d",
1293 +                               le32_to_cpu(HDR(new_bh)->h_refcount));
1294 +               } else if (old_bh && header == HDR(old_bh)) {
1295 +                       /* Keep this block. */
1296 +                       new_bh = old_bh;
1297 +                       (void)ext2_xattr_cache_insert(new_bh);
1298 +               } else {
1299 +                       /* We need to allocate a new block */
1300 +                       int force = EXT2_I(inode)->i_file_acl != 0;
1301 +                       int block = ext2_xattr_new_block(inode, &error, force);
1302 +                       if (error)
1303 +                               goto cleanup;
1304 +                       ea_idebug(inode, "creating block %d", block);
1305 +
1306 +                       new_bh = sb_getblk(sb, block);
1307 +                       if (!new_bh) {
1308 +                               ext2_xattr_free_block(inode, block);
1309 +                               error = -EIO;
1310 +                               goto cleanup;
1311 +                       }
1312 +                       lock_buffer(new_bh);
1313 +                       memcpy(new_bh->b_data, header, new_bh->b_size);
1314 +                       mark_buffer_uptodate(new_bh, 1);
1315 +                       unlock_buffer(new_bh);
1316 +                       (void)ext2_xattr_cache_insert(new_bh);
1317 +                       
1318 +                       ext2_xattr_update_super_block(sb);
1319 +               }
1320 +               mark_buffer_dirty(new_bh);
1321 +               if (IS_SYNC(inode)) {
1322 +                       ll_rw_block(WRITE, 1, &new_bh);
1323 +                       wait_on_buffer(new_bh); 
1324 +                       error = -EIO;
1325 +                       if (buffer_req(new_bh) && !buffer_uptodate(new_bh))
1326 +                               goto cleanup;
1327 +               }
1328 +       }
1329 +
1330 +       /* Update the inode. */
1331 +       EXT2_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
1332 +       inode->i_ctime = CURRENT_TIME;
1333 +       if (IS_SYNC(inode)) {
1334 +               error = ext2_sync_inode (inode);
1335 +               if (error)
1336 +                       goto cleanup;
1337 +       } else
1338 +               mark_inode_dirty(inode);
1339 +
1340 +       error = 0;
1341 +       if (old_bh && old_bh != new_bh) {
1342 +               /*
1343 +                * If there was an old block, and we are not still using it,
1344 +                * we now release the old block.
1345 +               */
1346 +               unsigned int refcount = le32_to_cpu(HDR(old_bh)->h_refcount);
1347 +
1348 +               if (refcount == 1) {
1349 +                       /* Free the old block. */
1350 +                       ea_bdebug(old_bh, "freeing");
1351 +                       ext2_xattr_free_block(inode, old_bh->b_blocknr);
1352 +                       mark_buffer_clean(old_bh);
1353 +               } else {
1354 +                       /* Decrement the refcount only. */
1355 +                       refcount--;
1356 +                       HDR(old_bh)->h_refcount = cpu_to_le32(refcount);
1357 +                       ext2_xattr_quota_free(inode);
1358 +                       mark_buffer_dirty(old_bh);
1359 +                       ea_bdebug(old_bh, "refcount now=%d", refcount);
1360 +               }
1361 +       }
1362 +
1363 +cleanup:
1364 +       if (old_bh != new_bh)
1365 +               brelse(new_bh);
1366 +
1367 +       return error;
1368 +}
1369 +
1370 +/*
1371 + * ext2_xattr_delete_inode()
1372 + *
1373 + * Free extended attribute resources associated with this inode. This
1374 + * is called immediately before an inode is freed.
1375 + */
1376 +void
1377 +ext2_xattr_delete_inode(struct inode *inode)
1378 +{
1379 +       struct buffer_head *bh;
1380 +       unsigned int block = EXT2_I(inode)->i_file_acl;
1381 +
1382 +       if (!block)
1383 +               return;
1384 +       down(&ext2_xattr_sem);
1385 +
1386 +       bh = sb_bread(inode->i_sb, block);
1387 +       if (!bh) {
1388 +               ext2_error(inode->i_sb, "ext2_xattr_delete_inode",
1389 +                       "inode %ld: block %d read error", inode->i_ino, block);
1390 +               goto cleanup;
1391 +       }
1392 +       ea_bdebug(bh, "b_count=%d", atomic_read(&(bh->b_count)));
1393 +       if (HDR(bh)->h_magic != cpu_to_le32(EXT2_XATTR_MAGIC) ||
1394 +           HDR(bh)->h_blocks != cpu_to_le32(1)) {
1395 +               ext2_error(inode->i_sb, "ext2_xattr_delete_inode",
1396 +                       "inode %ld: bad block %d", inode->i_ino, block);
1397 +               goto cleanup;
1398 +       }
1399 +       ea_bdebug(bh, "refcount now=%d", le32_to_cpu(HDR(bh)->h_refcount) - 1);
1400 +       if (HDR(bh)->h_refcount == cpu_to_le32(1)) {
1401 +               ext2_xattr_cache_remove(bh);
1402 +               ext2_xattr_free_block(inode, block);
1403 +               bforget(bh);
1404 +               bh = NULL;
1405 +       } else {
1406 +               HDR(bh)->h_refcount = cpu_to_le32(
1407 +                       le32_to_cpu(HDR(bh)->h_refcount) - 1);
1408 +               mark_buffer_dirty(bh);
1409 +               if (IS_SYNC(inode)) {
1410 +                       ll_rw_block(WRITE, 1, &bh);
1411 +                       wait_on_buffer(bh);
1412 +               }
1413 +               ext2_xattr_quota_free(inode);
1414 +       }
1415 +       EXT2_I(inode)->i_file_acl = 0;
1416 +
1417 +cleanup:
1418 +       brelse(bh);
1419 +       up(&ext2_xattr_sem);
1420 +}
1421 +
1422 +/*
1423 + * ext2_xattr_put_super()
1424 + *
1425 + * This is called when a file system is unmounted.
1426 + */
1427 +void
1428 +ext2_xattr_put_super(struct super_block *sb)
1429 +{
1430 +#ifdef CONFIG_EXT2_FS_XATTR_SHARING
1431 +       mb_cache_shrink(ext2_xattr_cache, sb->s_dev);
1432 +#endif
1433 +}
1434 +
1435 +#ifdef CONFIG_EXT2_FS_XATTR_SHARING
1436 +
1437 +/*
1438 + * ext2_xattr_cache_insert()
1439 + *
1440 + * Create a new entry in the extended attribute cache, and insert
1441 + * it unless such an entry is already in the cache.
1442 + *
1443 + * Returns 0, or a negative error number on failure.
1444 + */
1445 +static int
1446 +ext2_xattr_cache_insert(struct buffer_head *bh)
1447 +{
1448 +       __u32 hash = le32_to_cpu(HDR(bh)->h_hash);
1449 +       struct mb_cache_entry *ce;
1450 +       int error;
1451 +
1452 +       ce = mb_cache_entry_alloc(ext2_xattr_cache);
1453 +       if (!ce)
1454 +               return -ENOMEM;
1455 +       error = mb_cache_entry_insert(ce, bh->b_dev, bh->b_blocknr, &hash);
1456 +       if (error) {
1457 +               mb_cache_entry_free(ce);
1458 +               if (error == -EBUSY) {
1459 +                       ea_bdebug(bh, "already in cache (%d cache entries)",
1460 +                               atomic_read(&ext2_xattr_cache->c_entry_count));
1461 +                       error = 0;
1462 +               }
1463 +       } else {
1464 +               ea_bdebug(bh, "inserting [%x] (%d cache entries)", (int)hash,
1465 +                         atomic_read(&ext2_xattr_cache->c_entry_count));
1466 +               mb_cache_entry_release(ce);
1467 +       }
1468 +       return error;
1469 +}
1470 +
1471 +/*
1472 + * ext2_xattr_cmp()
1473 + *
1474 + * Compare two extended attribute blocks for equality.
1475 + *
1476 + * Returns 0 if the blocks are equal, 1 if they differ, and
1477 + * a negative error number on errors.
1478 + */
1479 +static int
1480 +ext2_xattr_cmp(struct ext2_xattr_header *header1,
1481 +              struct ext2_xattr_header *header2)
1482 +{
1483 +       struct ext2_xattr_entry *entry1, *entry2;
1484 +
1485 +       entry1 = ENTRY(header1+1);
1486 +       entry2 = ENTRY(header2+1);
1487 +       while (!IS_LAST_ENTRY(entry1)) {
1488 +               if (IS_LAST_ENTRY(entry2))
1489 +                       return 1;
1490 +               if (entry1->e_hash != entry2->e_hash ||
1491 +                   entry1->e_name_len != entry2->e_name_len ||
1492 +                   entry1->e_value_size != entry2->e_value_size ||
1493 +                   memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
1494 +                       return 1;
1495 +               if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
1496 +                       return -EIO;
1497 +               if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
1498 +                          (char *)header2 + le16_to_cpu(entry2->e_value_offs),
1499 +                          le32_to_cpu(entry1->e_value_size)))
1500 +                       return 1;
1501 +
1502 +               entry1 = EXT2_XATTR_NEXT(entry1);
1503 +               entry2 = EXT2_XATTR_NEXT(entry2);
1504 +       }
1505 +       if (!IS_LAST_ENTRY(entry2))
1506 +               return 1;
1507 +       return 0;
1508 +}
1509 +
1510 +/*
1511 + * ext2_xattr_cache_find()
1512 + *
1513 + * Find an identical extended attribute block.
1514 + *
1515 + * Returns a pointer to the block found, or NULL if such a block was
1516 + * not found or an error occurred.
1517 + */
1518 +static struct buffer_head *
1519 +ext2_xattr_cache_find(struct inode *inode, struct ext2_xattr_header *header)
1520 +{
1521 +       __u32 hash = le32_to_cpu(header->h_hash);
1522 +       struct mb_cache_entry *ce;
1523 +
1524 +       if (!header->h_hash)
1525 +               return NULL;  /* never share */
1526 +       ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
1527 +       ce = mb_cache_entry_find_first(ext2_xattr_cache, 0, inode->i_dev, hash);
1528 +       while (ce) {
1529 +               struct buffer_head *bh = sb_bread(inode->i_sb, ce->e_block);
1530 +
1531 +               if (!bh) {
1532 +                       ext2_error(inode->i_sb, "ext2_xattr_cache_find",
1533 +                               "inode %ld: block %ld read error",
1534 +                               inode->i_ino, ce->e_block);
1535 +               } else if (le32_to_cpu(HDR(bh)->h_refcount) >
1536 +                          EXT2_XATTR_REFCOUNT_MAX) {
1537 +                       ea_idebug(inode, "block %ld refcount %d>%d",ce->e_block,
1538 +                               le32_to_cpu(HDR(bh)->h_refcount),
1539 +                               EXT2_XATTR_REFCOUNT_MAX);
1540 +               } else if (!ext2_xattr_cmp(header, HDR(bh))) {
1541 +                       ea_bdebug(bh, "b_count=%d",atomic_read(&(bh->b_count)));
1542 +                       mb_cache_entry_release(ce);
1543 +                       return bh;
1544 +               }
1545 +               brelse(bh);
1546 +               ce = mb_cache_entry_find_next(ce, 0, inode->i_dev, hash);
1547 +       }
1548 +       return NULL;
1549 +}
1550 +
1551 +/*
1552 + * ext2_xattr_cache_remove()
1553 + *
1554 + * Remove the cache entry of a block from the cache. Called when a
1555 + * block becomes invalid.
1556 + */
1557 +static void
1558 +ext2_xattr_cache_remove(struct buffer_head *bh)
1559 +{
1560 +       struct mb_cache_entry *ce;
1561 +
1562 +       ce = mb_cache_entry_get(ext2_xattr_cache, bh->b_dev, bh->b_blocknr);
1563 +       if (ce) {
1564 +               ea_bdebug(bh, "removing (%d cache entries remaining)",
1565 +                         atomic_read(&ext2_xattr_cache->c_entry_count)-1);
1566 +               mb_cache_entry_free(ce);
1567 +       } else 
1568 +               ea_bdebug(bh, "no cache entry");
1569 +}
1570 +
1571 +#define NAME_HASH_SHIFT 5
1572 +#define VALUE_HASH_SHIFT 16
1573 +
1574 +/*
1575 + * ext2_xattr_hash_entry()
1576 + *
1577 + * Compute the hash of an extended attribute.
1578 + */
1579 +static inline void ext2_xattr_hash_entry(struct ext2_xattr_header *header,
1580 +                                        struct ext2_xattr_entry *entry)
1581 +{
1582 +       __u32 hash = 0;
1583 +       char *name = entry->e_name;
1584 +       int n;
1585 +
1586 +       for (n=0; n < entry->e_name_len; n++) {
1587 +               hash = (hash << NAME_HASH_SHIFT) ^
1588 +                      (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
1589 +                      *name++;
1590 +       }
1591 +
1592 +       if (entry->e_value_block == 0 && entry->e_value_size != 0) {
1593 +               __u32 *value = (__u32 *)((char *)header +
1594 +                       le16_to_cpu(entry->e_value_offs));
1595 +               for (n = (le32_to_cpu(entry->e_value_size) +
1596 +                    EXT2_XATTR_ROUND) >> EXT2_XATTR_PAD_BITS; n; n--) {
1597 +                       hash = (hash << VALUE_HASH_SHIFT) ^
1598 +                              (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
1599 +                              le32_to_cpu(*value++);
1600 +               }
1601 +       }
1602 +       entry->e_hash = cpu_to_le32(hash);
1603 +}
1604 +
1605 +#undef NAME_HASH_SHIFT
1606 +#undef VALUE_HASH_SHIFT
1607 +
1608 +#define BLOCK_HASH_SHIFT 16
1609 +
1610 +/*
1611 + * ext2_xattr_rehash()
1612 + *
1613 + * Re-compute the extended attribute hash value after an entry has changed.
1614 + */
1615 +static void ext2_xattr_rehash(struct ext2_xattr_header *header,
1616 +                             struct ext2_xattr_entry *entry)
1617 +{
1618 +       struct ext2_xattr_entry *here;
1619 +       __u32 hash = 0;
1620 +       
1621 +       ext2_xattr_hash_entry(header, entry);
1622 +       here = ENTRY(header+1);
1623 +       while (!IS_LAST_ENTRY(here)) {
1624 +               if (!here->e_hash) {
1625 +                       /* Block is not shared if an entry's hash value == 0 */
1626 +                       hash = 0;
1627 +                       break;
1628 +               }
1629 +               hash = (hash << BLOCK_HASH_SHIFT) ^
1630 +                      (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
1631 +                      le32_to_cpu(here->e_hash);
1632 +               here = EXT2_XATTR_NEXT(here);
1633 +       }
1634 +       header->h_hash = cpu_to_le32(hash);
1635 +}
1636 +
1637 +#undef BLOCK_HASH_SHIFT
1638 +
1639 +int __init
1640 +init_ext2_xattr(void)
1641 +{
1642 +       ext2_xattr_cache = mb_cache_create("ext2_xattr", NULL,
1643 +               sizeof(struct mb_cache_entry) +
1644 +               sizeof(struct mb_cache_entry_index), 1, 61);
1645 +       if (!ext2_xattr_cache)
1646 +               return -ENOMEM;
1647 +
1648 +       return 0;
1649 +}
1650 +
1651 +void
1652 +exit_ext2_xattr(void)
1653 +{
1654 +       mb_cache_destroy(ext2_xattr_cache);
1655 +}
1656 +
1657 +#else  /* CONFIG_EXT2_FS_XATTR_SHARING */
1658 +
1659 +int __init
1660 +init_ext2_xattr(void)
1661 +{
1662 +       return 0;
1663 +}
1664 +
1665 +void
1666 +exit_ext2_xattr(void)
1667 +{
1668 +}
1669 +
1670 +#endif  /* CONFIG_EXT2_FS_XATTR_SHARING */
1671 --- /dev/null   Mon May 20 21:11:23 2002
1672 +++ linux-mmonroe/fs/ext2/xattr_user.c  Fri May 16 08:43:01 2003
1673 @@ -0,0 +1,103 @@
1674 +/*
1675 + * linux/fs/ext2/xattr_user.c
1676 + * Handler for extended user attributes.
1677 + *
1678 + * Copyright (C) 2001 by Andreas Gruenbacher, <a.gruenbacher@computer.org>
1679 + */
1680 +
1681 +#include <linux/module.h>
1682 +#include <linux/string.h>
1683 +#include <linux/fs.h>
1684 +#include <linux/ext2_fs.h>
1685 +#include <linux/ext2_xattr.h>
1686 +
1687 +#ifdef CONFIG_EXT2_FS_POSIX_ACL
1688 +# include <linux/ext2_acl.h>
1689 +#endif
1690 +
1691 +#define XATTR_USER_PREFIX "user."
1692 +
1693 +static size_t
1694 +ext2_xattr_user_list(char *list, struct inode *inode,
1695 +                    const char *name, int name_len)
1696 +{
1697 +       const int prefix_len = sizeof(XATTR_USER_PREFIX)-1;
1698 +
1699 +       if (!test_opt(inode->i_sb, XATTR_USER))
1700 +               return 0;
1701 +
1702 +       if (list) {
1703 +               memcpy(list, XATTR_USER_PREFIX, prefix_len);
1704 +               memcpy(list+prefix_len, name, name_len);
1705 +               list[prefix_len + name_len] = '\0';
1706 +       }
1707 +       return prefix_len + name_len + 1;
1708 +}
1709 +
1710 +static int
1711 +ext2_xattr_user_get(struct inode *inode, const char *name,
1712 +                   void *buffer, size_t size)
1713 +{
1714 +       int error;
1715 +
1716 +       if (strcmp(name, "") == 0)
1717 +               return -EINVAL;
1718 +       if (!test_opt(inode->i_sb, XATTR_USER))
1719 +               return -ENOTSUP;
1720 +#ifdef CONFIG_EXT2_FS_POSIX_ACL
1721 +       error = ext2_permission_locked(inode, MAY_READ);
1722 +#else
1723 +       error = permission(inode, MAY_READ);
1724 +#endif
1725 +       if (error)
1726 +               return error;
1727 +
1728 +       return ext2_xattr_get(inode, EXT2_XATTR_INDEX_USER, name,
1729 +                             buffer, size);
1730 +}
1731 +
1732 +static int
1733 +ext2_xattr_user_set(struct inode *inode, const char *name,
1734 +                   const void *value, size_t size, int flags)
1735 +{
1736 +       int error;
1737 +
1738 +       if (strcmp(name, "") == 0)
1739 +               return -EINVAL;
1740 +       if (!test_opt(inode->i_sb, XATTR_USER))
1741 +               return -ENOTSUP;
1742 +       if ( !S_ISREG(inode->i_mode) &&
1743 +           (!S_ISDIR(inode->i_mode) || inode->i_mode & S_ISVTX))
1744 +               return -EPERM;
1745 +#ifdef CONFIG_EXT2_FS_POSIX_ACL
1746 +       error = ext2_permission_locked(inode, MAY_WRITE);
1747 +#else
1748 +       error = permission(inode, MAY_WRITE);
1749 +#endif
1750 +       if (error)
1751 +               return error;
1752 +
1753 +       return ext2_xattr_set(inode, EXT2_XATTR_INDEX_USER, name,
1754 +                             value, size, flags);
1755 +}
1756 +
1757 +struct ext2_xattr_handler ext2_xattr_user_handler = {
1758 +       prefix: XATTR_USER_PREFIX,
1759 +       list:   ext2_xattr_user_list,
1760 +       get:    ext2_xattr_user_get,
1761 +       set:    ext2_xattr_user_set,
1762 +};
1763 +
1764 +int __init
1765 +init_ext2_xattr_user(void)
1766 +{
1767 +       return ext2_xattr_register(EXT2_XATTR_INDEX_USER,
1768 +                                  &ext2_xattr_user_handler);
1769 +}
1770 +
1771 +void
1772 +exit_ext2_xattr_user(void)
1773 +{
1774 +       ext2_xattr_unregister(EXT2_XATTR_INDEX_USER,
1775 +                             &ext2_xattr_user_handler);
1776 +}
1777 --- linux/fs/ext3/Makefile~linux-2.4.20-xattr-0.8.54-hp Fri May 16 08:42:46 2003
1778 +++ linux-mmonroe/fs/ext3/Makefile      Fri May 16 08:43:01 2003
1779 @@ -1,5 +1,5 @@
1780  #
1781 -# Makefile for the linux ext2-filesystem routines.
1782 +# Makefile for the linux ext3-filesystem routines.
1783  #
1784  # Note! Dependencies are done automagically by 'make dep', which also
1785  # removes any old dependencies. DON'T put your own dependencies here
1786 @@ -9,10 +9,13 @@
1787  
1788  O_TARGET := ext3.o
1789  
1790 -export-objs := super.o inode.o
1791 +export-objs := ext3-exports.o
1792  
1793  obj-y    := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o \
1794 -               ioctl.o namei.o super.o symlink.o hash.o
1795 +               ioctl.o namei.o super.o symlink.o hash.o ext3-exports.o
1796  obj-m    := $(O_TARGET)
1797  
1798 +obj-$(CONFIG_EXT3_FS_XATTR) += xattr.o
1799 +obj-$(CONFIG_EXT3_FS_XATTR_USER) += xattr_user.o
1800 +
1801  include $(TOPDIR)/Rules.make
1802 --- linux/fs/ext3/file.c~linux-2.4.20-xattr-0.8.54-hp   Fri May 16 08:42:46 2003
1803 +++ linux-mmonroe/fs/ext3/file.c        Fri May 16 08:43:01 2003
1804 @@ -23,6 +23,7 @@
1805  #include <linux/locks.h>
1806  #include <linux/jbd.h>
1807  #include <linux/ext3_fs.h>
1808 +#include <linux/ext3_xattr.h>
1809  #include <linux/ext3_jbd.h>
1810  #include <linux/smp_lock.h>
1811  
1812 @@ -126,5 +127,9 @@ struct file_operations ext3_file_operati
1813  struct inode_operations ext3_file_inode_operations = {
1814         truncate:       ext3_truncate,          /* BKL held */
1815         setattr:        ext3_setattr,           /* BKL held */
1816 +       setxattr:       ext3_setxattr,          /* BKL held */
1817 +       getxattr:       ext3_getxattr,          /* BKL held */
1818 +       listxattr:      ext3_listxattr,         /* BKL held */
1819 +       removexattr:    ext3_removexattr,       /* BKL held */
1820  };
1821  
1822 --- linux/fs/ext3/ialloc.c~linux-2.4.20-xattr-0.8.54-hp Thu Nov 28 15:53:15 2002
1823 +++ linux-mmonroe/fs/ext3/ialloc.c      Fri May 16 08:43:01 2003
1824 @@ -17,6 +17,7 @@
1825  #include <linux/jbd.h>
1826  #include <linux/ext3_fs.h>
1827  #include <linux/ext3_jbd.h>
1828 +#include <linux/ext3_xattr.h>
1829  #include <linux/stat.h>
1830  #include <linux/string.h>
1831  #include <linux/locks.h>
1832 @@ -216,6 +217,7 @@ void ext3_free_inode (handle_t *handle, 
1833          * as writing the quota to disk may need the lock as well.
1834          */
1835         DQUOT_INIT(inode);
1836 +       ext3_xattr_delete_inode(handle, inode);
1837         DQUOT_FREE_INODE(inode);
1838         DQUOT_DROP(inode);
1839  
1840 --- linux/fs/ext3/inode.c~linux-2.4.20-xattr-0.8.54-hp  Thu Nov 28 15:53:15 2002
1841 +++ linux-mmonroe/fs/ext3/inode.c       Fri May 16 08:43:01 2003
1842 @@ -39,6 +39,18 @@
1843   */
1844  #undef SEARCH_FROM_ZERO
1845  
1846 +/*
1847 + * Test whether an inode is a fast symlink.
1848 + */
1849 +static inline int ext3_inode_is_fast_symlink(struct inode *inode)
1850 +{
1851 +       int ea_blocks = inode->u.ext3_i.i_file_acl ?
1852 +               (inode->i_sb->s_blocksize >> 9) : 0;
1853 +
1854 +       return (S_ISLNK(inode->i_mode) &&
1855 +               inode->i_blocks - ea_blocks == 0);
1856 +}
1857 +
1858  /* The ext3 forget function must perform a revoke if we are freeing data
1859   * which has been journaled.  Metadata (eg. indirect blocks) must be
1860   * revoked in all cases. 
1861 @@ -48,7 +60,7 @@
1862   * still needs to be revoked.
1863   */
1864  
1865 -static int ext3_forget(handle_t *handle, int is_metadata,
1866 +int ext3_forget(handle_t *handle, int is_metadata,
1867                        struct inode *inode, struct buffer_head *bh,
1868                        int blocknr)
1869  {
1870 @@ -164,9 +176,7 @@ void ext3_delete_inode (struct inode * i
1871  {
1872         handle_t *handle;
1873         
1874 -       if (is_bad_inode(inode) ||
1875 -           inode->i_ino == EXT3_ACL_IDX_INO ||
1876 -           inode->i_ino == EXT3_ACL_DATA_INO)
1877 +       if (is_bad_inode(inode))
1878                 goto no_delete;
1879  
1880         lock_kernel();
1881 @@ -1855,6 +1865,8 @@ void ext3_truncate(struct inode * inode)
1882         if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
1883             S_ISLNK(inode->i_mode)))
1884                 return;
1885 +       if (ext3_inode_is_fast_symlink(inode))
1886 +               return;
1887         if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1888                 return;
1889  
1890 @@ -2002,8 +2014,6 @@ int ext3_get_inode_loc (struct inode *in
1891         struct ext3_group_desc * gdp;
1892                 
1893         if ((inode->i_ino != EXT3_ROOT_INO &&
1894 -               inode->i_ino != EXT3_ACL_IDX_INO &&
1895 -               inode->i_ino != EXT3_ACL_DATA_INO &&
1896                 inode->i_ino != EXT3_JOURNAL_INO &&
1897                 inode->i_ino < EXT3_FIRST_INO(inode->i_sb)) ||
1898                 inode->i_ino > le32_to_cpu(
1899 @@ -2130,10 +2140,7 @@ void ext3_read_inode(struct inode * inod
1900  
1901         brelse (iloc.bh);
1902  
1903 -       if (inode->i_ino == EXT3_ACL_IDX_INO ||
1904 -           inode->i_ino == EXT3_ACL_DATA_INO)
1905 -               /* Nothing to do */ ;
1906 -       else if (S_ISREG(inode->i_mode)) {
1907 +       if (S_ISREG(inode->i_mode)) {
1908                 inode->i_op = &ext3_file_inode_operations;
1909                 inode->i_fop = &ext3_file_operations;
1910                 inode->i_mapping->a_ops = &ext3_aops;
1911 @@ -2141,15 +2148,17 @@ void ext3_read_inode(struct inode * inod
1912                 inode->i_op = &ext3_dir_inode_operations;
1913                 inode->i_fop = &ext3_dir_operations;
1914         } else if (S_ISLNK(inode->i_mode)) {
1915 -               if (!inode->i_blocks)
1916 +               if (ext3_inode_is_fast_symlink(inode))
1917                         inode->i_op = &ext3_fast_symlink_inode_operations;
1918                 else {
1919 -                       inode->i_op = &page_symlink_inode_operations;
1920 +                       inode->i_op = &ext3_symlink_inode_operations;
1921                         inode->i_mapping->a_ops = &ext3_aops;
1922                 }
1923 -       } else 
1924 +       } else {
1925 +               inode->i_op = &ext3_special_inode_operations;
1926                 init_special_inode(inode, inode->i_mode,
1927                                    le32_to_cpu(iloc.raw_inode->i_block[0]));
1928 +       }
1929         /* inode->i_attr_flags = 0;                             unused */
1930         if (inode->u.ext3_i.i_flags & EXT3_SYNC_FL) {
1931                 /* inode->i_attr_flags |= ATTR_FLAG_SYNCRONOUS; unused */
1932 --- linux/fs/ext3/namei.c~linux-2.4.20-xattr-0.8.54-hp  Fri May 16 08:42:47 2003
1933 +++ linux-mmonroe/fs/ext3/namei.c       Fri May 16 08:43:01 2003
1934 @@ -29,6 +29,7 @@
1935  #include <linux/sched.h>
1936  #include <linux/ext3_fs.h>
1937  #include <linux/ext3_jbd.h>
1938 +#include <linux/ext3_xattr.h>
1939  #include <linux/fcntl.h>
1940  #include <linux/stat.h>
1941  #include <linux/string.h>
1942 @@ -1611,7 +1612,7 @@ static int ext3_mkdir(struct inode * dir
1943         if (IS_SYNC(dir))
1944                 handle->h_sync = 1;
1945  
1946 -       inode = ext3_new_inode (handle, dir, S_IFDIR);
1947 +       inode = ext3_new_inode (handle, dir, S_IFDIR | mode);
1948         err = PTR_ERR(inode);
1949         if (IS_ERR(inode))
1950                 goto out_stop;
1951 @@ -1619,7 +1620,6 @@ static int ext3_mkdir(struct inode * dir
1952         inode->i_op = &ext3_dir_inode_operations;
1953         inode->i_fop = &ext3_dir_operations;
1954         inode->i_size = EXT3_I(inode)->i_disksize = inode->i_sb->s_blocksize;
1955 -       inode->i_blocks = 0;    
1956         dir_block = ext3_bread (handle, inode, 0, 1, &err);
1957         if (!dir_block) {
1958                 inode->i_nlink--; /* is this nlink == 0? */
1959 @@ -1646,9 +1646,6 @@ static int ext3_mkdir(struct inode * dir
1960         BUFFER_TRACE(dir_block, "call ext3_journal_dirty_metadata");
1961         ext3_journal_dirty_metadata(handle, dir_block);
1962         brelse (dir_block);
1963 -       inode->i_mode = S_IFDIR | mode;
1964 -       if (dir->i_mode & S_ISGID)
1965 -               inode->i_mode |= S_ISGID;
1966         ext3_mark_inode_dirty(handle, inode);
1967         err = ext3_add_entry (handle, dentry, inode);
1968         if (err) {
1969 @@ -2017,7 +2014,7 @@ static int ext3_symlink (struct inode * 
1970                 goto out_stop;
1971  
1972         if (l > sizeof (EXT3_I(inode)->i_data)) {
1973 -               inode->i_op = &page_symlink_inode_operations;
1974 +               inode->i_op = &ext3_symlink_inode_operations;
1975                 inode->i_mapping->a_ops = &ext3_aops;
1976                 /*
1977                  * block_symlink() calls back into ext3_prepare/commit_write.
1978 @@ -2244,4 +2241,16 @@ struct inode_operations ext3_dir_inode_o
1979         rmdir:          ext3_rmdir,             /* BKL held */
1980         mknod:          ext3_mknod,             /* BKL held */
1981         rename:         ext3_rename,            /* BKL held */
1982 +       setxattr:       ext3_setxattr,          /* BKL held */
1983 +       getxattr:       ext3_getxattr,          /* BKL held */
1984 +       listxattr:      ext3_listxattr,         /* BKL held */
1985 +       removexattr:    ext3_removexattr,       /* BKL held */
1986  };
1987 +
1988 +struct inode_operations ext3_special_inode_operations = {
1989 +       setxattr:       ext3_setxattr,          /* BKL held */
1990 +       getxattr:       ext3_getxattr,          /* BKL held */
1991 +       listxattr:      ext3_listxattr,         /* BKL held */
1992 +       removexattr:    ext3_removexattr,       /* BKL held */
1993 +};
1994 +
1995 --- linux/fs/ext3/super.c~linux-2.4.20-xattr-0.8.54-hp  Fri May 16 08:42:46 2003
1996 +++ linux-mmonroe/fs/ext3/super.c       Fri May 16 08:43:01 2003
1997 @@ -24,6 +24,7 @@
1998  #include <linux/jbd.h>
1999  #include <linux/ext3_fs.h>
2000  #include <linux/ext3_jbd.h>
2001 +#include <linux/ext3_xattr.h>
2002  #include <linux/slab.h>
2003  #include <linux/init.h>
2004  #include <linux/locks.h>
2005 @@ -406,6 +407,7 @@ void ext3_put_super (struct super_block 
2006         kdev_t j_dev = sbi->s_journal->j_dev;
2007         int i;
2008  
2009 +       ext3_xattr_put_super(sb);
2010         journal_destroy(sbi->s_journal);
2011         if (!(sb->s_flags & MS_RDONLY)) {
2012                 EXT3_CLEAR_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
2013 @@ -502,6 +504,7 @@ static int parse_options (char * options
2014                           int is_remount)
2015  {
2016         unsigned long *mount_options = &sbi->s_mount_opt;
2017 +       
2018         uid_t *resuid = &sbi->s_resuid;
2019         gid_t *resgid = &sbi->s_resgid;
2020         char * this_char;
2021 @@ -514,6 +517,13 @@ static int parse_options (char * options
2022              this_char = strtok (NULL, ",")) {
2023                 if ((value = strchr (this_char, '=')) != NULL)
2024                         *value++ = 0;
2025 +#ifdef CONFIG_EXT3_FS_XATTR_USER
2026 +               if (!strcmp (this_char, "user_xattr"))
2027 +                       set_opt (*mount_options, XATTR_USER);
2028 +               else if (!strcmp (this_char, "nouser_xattr"))
2029 +                       clear_opt (*mount_options, XATTR_USER);
2030 +               else
2031 +#endif
2032                 if (!strcmp (this_char, "bsddf"))
2033                         clear_opt (*mount_options, MINIX_DF);
2034                 else if (!strcmp (this_char, "nouid32")) {
2035 @@ -931,6 +941,12 @@ struct super_block * ext3_read_super (st
2036         sbi->s_mount_opt = 0;
2037         sbi->s_resuid = EXT3_DEF_RESUID;
2038         sbi->s_resgid = EXT3_DEF_RESGID;
2039 +
2040 +       /* Default extended attribute flags */
2041 +#ifdef CONFIG_EXT3_FS_XATTR_USER
2042 +       /* set_opt(sbi->s_mount_opt, XATTR_USER); */
2043 +#endif
2044 +
2045         if (!parse_options ((char *) data, &sb_block, sbi, &journal_inum, 0)) {
2046                 sb->s_dev = 0;
2047                 goto out_fail;
2048 @@ -1768,17 +1784,29 @@ static DECLARE_FSTYPE_DEV(ext3_fs_type, 
2049  
2050  static int __init init_ext3_fs(void)
2051  {
2052 -        return register_filesystem(&ext3_fs_type);
2053 +       int error = init_ext3_xattr();
2054 +       if (error)
2055 +               return error;
2056 +       error = init_ext3_xattr_user();
2057 +       if (error)
2058 +               goto fail;
2059 +       error = register_filesystem(&ext3_fs_type);
2060 +       if (!error)
2061 +               return 0;
2062 +       
2063 +       exit_ext3_xattr_user();
2064 +fail:
2065 +       exit_ext3_xattr();
2066 +       return error;
2067  }
2068  
2069  static void __exit exit_ext3_fs(void)
2070  {
2071         unregister_filesystem(&ext3_fs_type);
2072 +       exit_ext3_xattr_user();
2073 +       exit_ext3_xattr();
2074  }
2075  
2076 -EXPORT_SYMBOL(ext3_force_commit);
2077 -EXPORT_SYMBOL(ext3_bread);
2078 -
2079  MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
2080  MODULE_DESCRIPTION("Second Extended Filesystem with journaling extensions");
2081  MODULE_LICENSE("GPL");
2082 --- linux/fs/ext3/symlink.c~linux-2.4.20-xattr-0.8.54-hp        Fri Nov  9 14:25:04 2001
2083 +++ linux-mmonroe/fs/ext3/symlink.c     Fri May 16 08:43:01 2003
2084 @@ -20,6 +20,7 @@
2085  #include <linux/fs.h>
2086  #include <linux/jbd.h>
2087  #include <linux/ext3_fs.h>
2088 +#include <linux/ext3_xattr.h>
2089  
2090  static int ext3_readlink(struct dentry *dentry, char *buffer, int buflen)
2091  {
2092 @@ -33,7 +34,20 @@ static int ext3_follow_link(struct dentr
2093         return vfs_follow_link(nd, s);
2094  }
2095  
2096 +struct inode_operations ext3_symlink_inode_operations = {
2097 +       readlink:       page_readlink,          /* BKL not held.  Don't need */
2098 +       follow_link:    page_follow_link,       /* BKL not held.  Don't need */
2099 +       setxattr:       ext3_setxattr,          /* BKL held */
2100 +       getxattr:       ext3_getxattr,          /* BKL held */
2101 +       listxattr:      ext3_listxattr,         /* BKL held */
2102 +       removexattr:    ext3_removexattr,       /* BKL held */
2103 +};
2104 +
2105  struct inode_operations ext3_fast_symlink_inode_operations = {
2106         readlink:       ext3_readlink,          /* BKL not held.  Don't need */
2107         follow_link:    ext3_follow_link,       /* BKL not held.  Don't need */
2108 +       setxattr:       ext3_setxattr,          /* BKL held */
2109 +       getxattr:       ext3_getxattr,          /* BKL held */
2110 +       listxattr:      ext3_listxattr,         /* BKL held */
2111 +       removexattr:    ext3_removexattr,       /* BKL held */
2112  };
2113 --- /dev/null   Mon May 20 21:11:23 2002
2114 +++ linux-mmonroe/fs/ext3/xattr.c       Fri May 16 08:43:01 2003
2115 @@ -0,0 +1,1225 @@
2116 +/*
2117 + * linux/fs/ext3/xattr.c
2118 + *
2119 + * Copyright (C) 2001 by Andreas Gruenbacher, <a.gruenbacher@computer.org>
2120 + *
2121 + * Fix by Harrison Xing <harrison@mountainviewdata.com>.
2122 + * Ext3 code with a lot of help from Eric Jarman <ejarman@acm.org>.
2123 + * Extended attributes for symlinks and special files added per
2124 + *  suggestion of Luka Renko <luka.renko@hermes.si>.
2125 + */
2126 +
2127 +/*
2128 + * Extended attributes are stored on disk blocks allocated outside of
2129 + * any inode. The i_file_acl field is then made to point to this allocated
2130 + * block. If all extended attributes of an inode are identical, these
2131 + * inodes may share the same extended attribute block. Such situations
2132 + * are automatically detected by keeping a cache of recent attribute block
2133 + * numbers and hashes over the block's contents in memory.
2134 + *
2135 + *
2136 + * Extended attribute block layout:
2137 + *
2138 + *   +------------------+
2139 + *   | header           |
2140 + *   | entry 1          | |
2141 + *   | entry 2          | | growing downwards
2142 + *   | entry 3          | v
2143 + *   | four null bytes  |
2144 + *   | . . .            |
2145 + *   | value 1          | ^
2146 + *   | value 3          | | growing upwards
2147 + *   | value 2          | |
2148 + *   +------------------+
2149 + *
2150 + * The block header is followed by multiple entry descriptors. These entry
2151 + * descriptors are variable in size, and alligned to EXT3_XATTR_PAD
2152 + * byte boundaries. The entry descriptors are sorted by attribute name,
2153 + * so that two extended attribute blocks can be compared efficiently.
2154 + *
2155 + * Attribute values are aligned to the end of the block, stored in
2156 + * no specific order. They are also padded to EXT3_XATTR_PAD byte
2157 + * boundaries. No additional gaps are left between them.
2158 + *
2159 + * Locking strategy
2160 + * ----------------
2161 + * The VFS already holds the BKL and the inode->i_sem semaphore when any of
2162 + * the xattr inode operations are called, so we are guaranteed that only one
2163 + * processes accesses extended attributes of an inode at any time.
2164 + *
2165 + * For writing we also grab the ext3_xattr_sem semaphore. This ensures that
2166 + * only a single process is modifying an extended attribute block, even
2167 + * if the block is shared among inodes.
2168 + *
2169 + * Note for porting to 2.5
2170 + * -----------------------
2171 + * The BKL will no longer be held in the xattr inode operations.
2172 + */
2173 +
2174 +#include <linux/module.h>
2175 +#include <linux/fs.h>
2176 +#include <linux/locks.h>
2177 +#include <linux/slab.h>
2178 +#include <linux/ext3_jbd.h>
2179 +#include <linux/ext3_fs.h>
2180 +#include <linux/ext3_xattr.h>
2181 +#include <linux/mbcache.h>
2182 +#include <linux/quotaops.h>
2183 +#include <asm/semaphore.h>
2184 +#include <linux/compatmac.h>
2185 +
2186 +#define EXT3_EA_USER "user."
2187 +
2188 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0)
2189 +# define mark_buffer_dirty(bh) mark_buffer_dirty(bh, 1)
2190 +#endif
2191 +
2192 +#define HDR(bh) ((struct ext3_xattr_header *)((bh)->b_data))
2193 +#define ENTRY(ptr) ((struct ext3_xattr_entry *)(ptr))
2194 +#define FIRST_ENTRY(bh) ENTRY(HDR(bh)+1)
2195 +#define IS_LAST_ENTRY(entry) (*(__u32 *)(entry) == 0)
2196 +
2197 +#ifdef EXT3_XATTR_DEBUG
2198 +# define ea_idebug(inode, f...) do { \
2199 +               printk(KERN_DEBUG "inode %s:%ld: ", \
2200 +                       kdevname(inode->i_dev), inode->i_ino); \
2201 +               printk(f); \
2202 +               printk("\n"); \
2203 +       } while (0)
2204 +# define ea_bdebug(bh, f...) do { \
2205 +               printk(KERN_DEBUG "block %s:%ld: ", \
2206 +                       kdevname(bh->b_dev), bh->b_blocknr); \
2207 +               printk(f); \
2208 +               printk("\n"); \
2209 +       } while (0)
2210 +#else
2211 +# define ea_idebug(f...)
2212 +# define ea_bdebug(f...)
2213 +#endif
2214 +
2215 +static int ext3_xattr_set2(handle_t *, struct inode *, struct buffer_head *,
2216 +                          struct ext3_xattr_header *);
2217 +
2218 +#ifdef CONFIG_EXT3_FS_XATTR_SHARING
2219 +
2220 +static int ext3_xattr_cache_insert(struct buffer_head *);
2221 +static struct buffer_head *ext3_xattr_cache_find(struct inode *,
2222 +                                                struct ext3_xattr_header *);
2223 +static void ext3_xattr_cache_remove(struct buffer_head *);
2224 +static void ext3_xattr_rehash(struct ext3_xattr_header *,
2225 +                             struct ext3_xattr_entry *);
2226 +
2227 +static struct mb_cache *ext3_xattr_cache;
2228 +
2229 +#else
2230 +# define ext3_xattr_cache_insert(bh) 0
2231 +# define ext3_xattr_cache_find(inode, header) NULL
2232 +# define ext3_xattr_cache_remove(bh) while(0) {}
2233 +# define ext3_xattr_rehash(header, entry) while(0) {}
2234 +#endif
2235 +
2236 +/*
2237 + * If a file system does not share extended attributes among inodes,
2238 + * we should not need the ext3_xattr_sem semaphore. However, the
2239 + * filesystem may still contain shared blocks, so we always take
2240 + * the lock.
2241 + */
2242 +
2243 +DECLARE_MUTEX(ext3_xattr_sem);
2244 +
2245 +static inline int
2246 +ext3_xattr_new_block(handle_t *handle, struct inode *inode,
2247 +                    int * errp, int force)
2248 +{
2249 +       struct super_block *sb = inode->i_sb;
2250 +       int goal = le32_to_cpu(EXT3_SB(sb)->s_es->s_first_data_block) +
2251 +               EXT3_I(inode)->i_block_group * EXT3_BLOCKS_PER_GROUP(sb);
2252 +
2253 +       /* How can we enforce the allocation? */
2254 +       int block = ext3_new_block(handle, inode, goal, 0, 0, errp);
2255 +#ifdef OLD_QUOTAS
2256 +       if (!*errp)
2257 +               inode->i_blocks += inode->i_sb->s_blocksize >> 9;
2258 +#endif
2259 +       return block;
2260 +}
2261 +
2262 +static inline int
2263 +ext3_xattr_quota_alloc(struct inode *inode, int force)
2264 +{
2265 +       /* How can we enforce the allocation? */
2266 +#ifdef OLD_QUOTAS
2267 +       int error = DQUOT_ALLOC_BLOCK(inode->i_sb, inode, 1);
2268 +       if (!error)
2269 +               inode->i_blocks += inode->i_sb->s_blocksize >> 9;
2270 +#else
2271 +       int error = DQUOT_ALLOC_BLOCK(inode, 1);
2272 +#endif
2273 +       return error;
2274 +}
2275 +
2276 +#ifdef OLD_QUOTAS
2277 +
2278 +static inline void
2279 +ext3_xattr_quota_free(struct inode *inode)
2280 +{
2281 +       DQUOT_FREE_BLOCK(inode->i_sb, inode, 1);
2282 +       inode->i_blocks -= inode->i_sb->s_blocksize >> 9;
2283 +}
2284 +
2285 +static inline void
2286 +ext3_xattr_free_block(handle_t *handle, struct inode * inode,
2287 +                     unsigned long block)
2288 +{
2289 +       ext3_free_blocks(handle, inode, block, 1);
2290 +       inode->i_blocks -= inode->i_sb->s_blocksize >> 9;
2291 +}
2292 +
2293 +#else
2294 +# define ext3_xattr_quota_free(inode) \
2295 +       DQUOT_FREE_BLOCK(inode, 1)
2296 +# define ext3_xattr_free_block(handle, inode, block) \
2297 +       ext3_free_blocks(handle, inode, block, 1)
2298 +#endif
2299 +
2300 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,18)
2301 +
2302 +static inline struct buffer_head *
2303 +sb_bread(struct super_block *sb, int block)
2304 +{
2305 +       return bread(sb->s_dev, block, sb->s_blocksize);
2306 +}
2307 +
2308 +static inline struct buffer_head *
2309 +sb_getblk(struct super_block *sb, int block)
2310 +{
2311 +       return getblk(sb->s_dev, block, sb->s_blocksize);
2312 +}
2313 +
2314 +#endif
2315 +
2316 +struct ext3_xattr_handler *ext3_xattr_handlers[EXT3_XATTR_INDEX_MAX];
2317 +rwlock_t ext3_handler_lock = RW_LOCK_UNLOCKED;
2318 +
2319 +int
2320 +ext3_xattr_register(int name_index, struct ext3_xattr_handler *handler)
2321 +{
2322 +       int error = -EINVAL;
2323 +
2324 +       if (name_index > 0 && name_index <= EXT3_XATTR_INDEX_MAX) {
2325 +               write_lock(&ext3_handler_lock);
2326 +               if (!ext3_xattr_handlers[name_index-1]) {
2327 +                       ext3_xattr_handlers[name_index-1] = handler;
2328 +                       error = 0;
2329 +               }
2330 +               write_unlock(&ext3_handler_lock);
2331 +       }
2332 +       return error;
2333 +}
2334 +
2335 +void
2336 +ext3_xattr_unregister(int name_index, struct ext3_xattr_handler *handler)
2337 +{
2338 +       if (name_index > 0 || name_index <= EXT3_XATTR_INDEX_MAX) {
2339 +               write_lock(&ext3_handler_lock);
2340 +               ext3_xattr_handlers[name_index-1] = NULL;
2341 +               write_unlock(&ext3_handler_lock);
2342 +       }
2343 +}
2344 +
2345 +static inline const char *
2346 +strcmp_prefix(const char *a, const char *a_prefix)
2347 +{
2348 +       while (*a_prefix && *a == *a_prefix) {
2349 +               a++;
2350 +               a_prefix++;
2351 +       }
2352 +       return *a_prefix ? NULL : a;
2353 +}
2354 +
2355 +/*
2356 + * Decode the extended attribute name, and translate it into
2357 + * the name_index and name suffix.
2358 + */
2359 +static inline struct ext3_xattr_handler *
2360 +ext3_xattr_resolve_name(const char **name)
2361 +{
2362 +       struct ext3_xattr_handler *handler = NULL;
2363 +       int i;
2364 +
2365 +       if (!*name)
2366 +               return NULL;
2367 +       read_lock(&ext3_handler_lock);
2368 +       for (i=0; i<EXT3_XATTR_INDEX_MAX; i++) {
2369 +               if (ext3_xattr_handlers[i]) {
2370 +                       const char *n = strcmp_prefix(*name,
2371 +                               ext3_xattr_handlers[i]->prefix);
2372 +                       if (n) {
2373 +                               handler = ext3_xattr_handlers[i];
2374 +                               *name = n;
2375 +                               break;
2376 +                       }
2377 +               }
2378 +       }
2379 +       read_unlock(&ext3_handler_lock);
2380 +       return handler;
2381 +}
2382 +
2383 +static inline struct ext3_xattr_handler *
2384 +ext3_xattr_handler(int name_index)
2385 +{
2386 +       struct ext3_xattr_handler *handler = NULL;
2387 +       if (name_index > 0 && name_index <= EXT3_XATTR_INDEX_MAX) {
2388 +               read_lock(&ext3_handler_lock);
2389 +               handler = ext3_xattr_handlers[name_index-1];
2390 +               read_unlock(&ext3_handler_lock);
2391 +       }
2392 +       return handler;
2393 +}
2394 +
2395 +/*
2396 + * Inode operation getxattr()
2397 + *
2398 + * dentry->d_inode->i_sem down
2399 + * BKL held [before 2.5.x]
2400 + */
2401 +ssize_t
2402 +ext3_getxattr(struct dentry *dentry, const char *name,
2403 +             void *buffer, size_t size)
2404 +{
2405 +       struct ext3_xattr_handler *handler;
2406 +       struct inode *inode = dentry->d_inode;
2407 +
2408 +       handler = ext3_xattr_resolve_name(&name);
2409 +       if (!handler)
2410 +               return -ENOTSUP;
2411 +       return handler->get(inode, name, buffer, size);
2412 +}
2413 +
2414 +/*
2415 + * Inode operation listxattr()
2416 + *
2417 + * dentry->d_inode->i_sem down
2418 + * BKL held [before 2.5.x]
2419 + */
2420 +ssize_t
2421 +ext3_listxattr(struct dentry *dentry, char *buffer, size_t size)
2422 +{
2423 +       return ext3_xattr_list(dentry->d_inode, buffer, size);
2424 +}
2425 +
2426 +/*
2427 + * Inode operation setxattr()
2428 + *
2429 + * dentry->d_inode->i_sem down
2430 + * BKL held [before 2.5.x]
2431 + */
2432 +int
2433 +ext3_setxattr(struct dentry *dentry, const char *name,
2434 +             const void *value, size_t size, int flags)
2435 +{
2436 +       struct ext3_xattr_handler *handler;
2437 +       struct inode *inode = dentry->d_inode;
2438 +
2439 +       if (size == 0)
2440 +               value = "";  /* empty EA, do not remove */
2441 +       handler = ext3_xattr_resolve_name(&name);
2442 +       if (!handler)
2443 +               return -ENOTSUP;
2444 +       return handler->set(inode, name, value, size, flags);
2445 +}
2446 +
2447 +/*
2448 + * Inode operation removexattr()
2449 + *
2450 + * dentry->d_inode->i_sem down
2451 + * BKL held [before 2.5.x]
2452 + */
2453 +int
2454 +ext3_removexattr(struct dentry *dentry, const char *name)
2455 +{
2456 +       struct ext3_xattr_handler *handler;
2457 +       struct inode *inode = dentry->d_inode;
2458 +
2459 +       handler = ext3_xattr_resolve_name(&name);
2460 +       if (!handler)
2461 +               return -ENOTSUP;
2462 +       return handler->set(inode, name, NULL, 0, XATTR_REPLACE);
2463 +}
2464 +
2465 +/*
2466 + * ext3_xattr_get()
2467 + *
2468 + * Copy an extended attribute into the buffer
2469 + * provided, or compute the buffer size required.
2470 + * Buffer is NULL to compute the size of the buffer required.
2471 + *
2472 + * Returns a negative error number on failure, or the number of bytes
2473 + * used / required on success.
2474 + */
2475 +int
2476 +ext3_xattr_get(struct inode *inode, int name_index, const char *name,
2477 +              void *buffer, size_t buffer_size)
2478 +{
2479 +       struct buffer_head *bh = NULL;
2480 +       struct ext3_xattr_entry *entry;
2481 +       unsigned int block, size;
2482 +       char *end;
2483 +       int name_len, error;
2484 +
2485 +       ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
2486 +                 name_index, name, buffer, (long)buffer_size);
2487 +
2488 +       if (name == NULL)
2489 +               return -EINVAL;
2490 +       if (!EXT3_I(inode)->i_file_acl)
2491 +               return -ENOATTR;
2492 +       block = EXT3_I(inode)->i_file_acl;
2493 +       ea_idebug(inode, "reading block %d", block);
2494 +       bh = sb_bread(inode->i_sb, block);
2495 +       if (!bh)
2496 +               return -EIO;
2497 +       ea_bdebug(bh, "b_count=%d, refcount=%d",
2498 +               atomic_read(&(bh->b_count)), le32_to_cpu(HDR(bh)->h_refcount));
2499 +       end = bh->b_data + bh->b_size;
2500 +       if (HDR(bh)->h_magic != cpu_to_le32(EXT3_XATTR_MAGIC) ||
2501 +           HDR(bh)->h_blocks != cpu_to_le32(1)) {
2502 +bad_block:     ext3_error(inode->i_sb, "ext3_xattr_get",
2503 +                       "inode %ld: bad block %d", inode->i_ino, block);
2504 +               error = -EIO;
2505 +               goto cleanup;
2506 +       }
2507 +       /* find named attribute */
2508 +       name_len = strlen(name);
2509 +
2510 +       error = -ERANGE;
2511 +       if (name_len > 255)
2512 +               goto cleanup;
2513 +       entry = FIRST_ENTRY(bh);
2514 +       while (!IS_LAST_ENTRY(entry)) {
2515 +               struct ext3_xattr_entry *next =
2516 +                       EXT3_XATTR_NEXT(entry);
2517 +               if ((char *)next >= end)
2518 +                       goto bad_block;
2519 +               if (name_index == entry->e_name_index &&
2520 +                   name_len == entry->e_name_len &&
2521 +                   memcmp(name, entry->e_name, name_len) == 0)
2522 +                       goto found;
2523 +               entry = next;
2524 +       }
2525 +       /* Check the remaining name entries */
2526 +       while (!IS_LAST_ENTRY(entry)) {
2527 +               struct ext3_xattr_entry *next =
2528 +                       EXT3_XATTR_NEXT(entry);
2529 +               if ((char *)next >= end)
2530 +                       goto bad_block;
2531 +               entry = next;
2532 +       }
2533 +       if (ext3_xattr_cache_insert(bh))
2534 +               ea_idebug(inode, "cache insert failed");
2535 +       error = -ENOATTR;
2536 +       goto cleanup;
2537 +found:
2538 +       /* check the buffer size */
2539 +       if (entry->e_value_block != 0)
2540 +               goto bad_block;
2541 +       size = le32_to_cpu(entry->e_value_size);
2542 +       if (size > inode->i_sb->s_blocksize ||
2543 +           le16_to_cpu(entry->e_value_offs) + size > inode->i_sb->s_blocksize)
2544 +               goto bad_block;
2545 +
2546 +       if (ext3_xattr_cache_insert(bh))
2547 +               ea_idebug(inode, "cache insert failed");
2548 +       if (buffer) {
2549 +               error = -ERANGE;
2550 +               if (size > buffer_size)
2551 +                       goto cleanup;
2552 +               /* return value of attribute */
2553 +               memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
2554 +                       size);
2555 +       }
2556 +       error = size;
2557 +
2558 +cleanup:
2559 +       brelse(bh);
2560 +
2561 +       return error;
2562 +}
2563 +
2564 +/*
2565 + * ext3_xattr_list()
2566 + *
2567 + * Copy a list of attribute names into the buffer
2568 + * provided, or compute the buffer size required.
2569 + * Buffer is NULL to compute the size of the buffer required.
2570 + *
2571 + * Returns a negative error number on failure, or the number of bytes
2572 + * used / required on success.
2573 + */
2574 +int
2575 +ext3_xattr_list(struct inode *inode, char *buffer, size_t buffer_size)
2576 +{
2577 +       struct buffer_head *bh = NULL;
2578 +       struct ext3_xattr_entry *entry;
2579 +       unsigned int block, size = 0;
2580 +       char *buf, *end;
2581 +       int error;
2582 +
2583 +       ea_idebug(inode, "buffer=%p, buffer_size=%ld",
2584 +                 buffer, (long)buffer_size);
2585 +
2586 +       if (!EXT3_I(inode)->i_file_acl)
2587 +               return 0;
2588 +       block = EXT3_I(inode)->i_file_acl;
2589 +       ea_idebug(inode, "reading block %d", block);
2590 +       bh = sb_bread(inode->i_sb, block);
2591 +       if (!bh)
2592 +               return -EIO;
2593 +       ea_bdebug(bh, "b_count=%d, refcount=%d",
2594 +               atomic_read(&(bh->b_count)), le32_to_cpu(HDR(bh)->h_refcount));
2595 +       end = bh->b_data + bh->b_size;
2596 +       if (HDR(bh)->h_magic != cpu_to_le32(EXT3_XATTR_MAGIC) ||
2597 +           HDR(bh)->h_blocks != cpu_to_le32(1)) {
2598 +bad_block:     ext3_error(inode->i_sb, "ext3_xattr_list",
2599 +                       "inode %ld: bad block %d", inode->i_ino, block);
2600 +               error = -EIO;
2601 +               goto cleanup;
2602 +       }
2603 +       /* compute the size required for the list of attribute names */
2604 +       for (entry = FIRST_ENTRY(bh); !IS_LAST_ENTRY(entry);
2605 +            entry = EXT3_XATTR_NEXT(entry)) {
2606 +               struct ext3_xattr_handler *handler;
2607 +               struct ext3_xattr_entry *next =
2608 +                       EXT3_XATTR_NEXT(entry);
2609 +               if ((char *)next >= end)
2610 +                       goto bad_block;
2611 +
2612 +               handler = ext3_xattr_handler(entry->e_name_index);
2613 +               if (handler)
2614 +                       size += handler->list(NULL, inode, entry->e_name,
2615 +                                             entry->e_name_len);
2616 +       }
2617 +
2618 +       if (ext3_xattr_cache_insert(bh))
2619 +               ea_idebug(inode, "cache insert failed");
2620 +       if (!buffer) {
2621 +               error = size;
2622 +               goto cleanup;
2623 +       } else {
2624 +               error = -ERANGE;
2625 +               if (size > buffer_size)
2626 +                       goto cleanup;
2627 +       }
2628 +
2629 +       /* list the attribute names */
2630 +       buf = buffer;
2631 +       for (entry = FIRST_ENTRY(bh); !IS_LAST_ENTRY(entry);
2632 +            entry = EXT3_XATTR_NEXT(entry)) {
2633 +               struct ext3_xattr_handler *handler;
2634 +
2635 +               handler = ext3_xattr_handler(entry->e_name_index);
2636 +               if (handler)
2637 +                       buf += handler->list(buf, inode, entry->e_name,
2638 +                                            entry->e_name_len);
2639 +       }
2640 +       error = size;
2641 +
2642 +cleanup:
2643 +       brelse(bh);
2644 +
2645 +       return error;
2646 +}
2647 +
2648 +/*
2649 + * If the EXT3_FEATURE_COMPAT_EXT_ATTR feature of this file system is
2650 + * not set, set it.
2651 + */
2652 +static void ext3_xattr_update_super_block(handle_t *handle,
2653 +                                         struct super_block *sb)
2654 +{
2655 +       if (EXT3_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_EXT_ATTR))
2656 +               return;
2657 +
2658 +       lock_super(sb);
2659 +       ext3_journal_get_write_access(handle, EXT3_SB(sb)->s_sbh);
2660 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0)
2661 +       EXT3_SB(sb)->s_feature_compat |= EXT3_FEATURE_COMPAT_EXT_ATTR;
2662 +#endif
2663 +       EXT3_SB(sb)->s_es->s_feature_compat |=
2664 +               cpu_to_le32(EXT3_FEATURE_COMPAT_EXT_ATTR);
2665 +       sb->s_dirt = 1;
2666 +       ext3_journal_dirty_metadata(handle, EXT3_SB(sb)->s_sbh);
2667 +       unlock_super(sb);
2668 +}
2669 +
2670 +/*
2671 + * ext3_xattr_set()
2672 + *
2673 + * Create, replace or remove an extended attribute for this inode. Buffer
2674 + * is NULL to remove an existing extended attribute, and non-NULL to
2675 + * either replace an existing extended attribute, or create a new extended
2676 + * attribute. The flags XATTR_REPLACE and XATTR_CREATE
2677 + * specify that an extended attribute must exist and must not exist
2678 + * previous to the call, respectively.
2679 + *
2680 + * Returns 0, or a negative error number on failure.
2681 + */
2682 +int
2683 +ext3_xattr_set(handle_t *handle, struct inode *inode, int name_index,
2684 +              const char *name, const void *value, size_t value_len, int flags)
2685 +{
2686 +       struct super_block *sb = inode->i_sb;
2687 +       struct buffer_head *bh = NULL;
2688 +       struct ext3_xattr_header *header = NULL;
2689 +       struct ext3_xattr_entry *here, *last;
2690 +       unsigned int name_len;
2691 +       int block = EXT3_I(inode)->i_file_acl;
2692 +       int min_offs = sb->s_blocksize, not_found = 1, free, error;
2693 +       char *end;
2694 +       
2695 +       /*
2696 +        * header -- Points either into bh, or to a temporarily
2697 +        *           allocated buffer.
2698 +        * here -- The named entry found, or the place for inserting, within
2699 +        *         the block pointed to by header.
2700 +        * last -- Points right after the last named entry within the block
2701 +        *         pointed to by header.
2702 +        * min_offs -- The offset of the first value (values are aligned
2703 +        *             towards the end of the block).
2704 +        * end -- Points right after the block pointed to by header.
2705 +        */
2706 +       
2707 +       ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
2708 +                 name_index, name, value, (long)value_len);
2709 +
2710 +       if (IS_RDONLY(inode))
2711 +               return -EROFS;
2712 +       if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
2713 +               return -EPERM;
2714 +       if (value == NULL)
2715 +               value_len = 0;
2716 +       if (name == NULL)
2717 +               return -EINVAL;
2718 +       name_len = strlen(name);
2719 +       if (name_len > 255 || value_len > sb->s_blocksize)
2720 +               return -ERANGE;
2721 +       down(&ext3_xattr_sem);
2722 +
2723 +       if (block) {
2724 +               /* The inode already has an extended attribute block. */
2725 +               bh = sb_bread(sb, block);
2726 +               error = -EIO;
2727 +               if (!bh)
2728 +                       goto cleanup;
2729 +               ea_bdebug(bh, "b_count=%d, refcount=%d",
2730 +                       atomic_read(&(bh->b_count)),
2731 +                       le32_to_cpu(HDR(bh)->h_refcount));
2732 +               header = HDR(bh);
2733 +               end = bh->b_data + bh->b_size;
2734 +               if (header->h_magic != cpu_to_le32(EXT3_XATTR_MAGIC) ||
2735 +                   header->h_blocks != cpu_to_le32(1)) {
2736 +bad_block:             ext3_error(sb, "ext3_xattr_set",
2737 +                               "inode %ld: bad block %d", inode->i_ino, block);
2738 +                       error = -EIO;
2739 +                       goto cleanup;
2740 +               }
2741 +               /* Find the named attribute. */
2742 +               here = FIRST_ENTRY(bh);
2743 +               while (!IS_LAST_ENTRY(here)) {
2744 +                       struct ext3_xattr_entry *next = EXT3_XATTR_NEXT(here);
2745 +                       if ((char *)next >= end)
2746 +                               goto bad_block;
2747 +                       if (!here->e_value_block && here->e_value_size) {
2748 +                               int offs = le16_to_cpu(here->e_value_offs);
2749 +                               if (offs < min_offs)
2750 +                                       min_offs = offs;
2751 +                       }
2752 +                       not_found = name_index - here->e_name_index;
2753 +                       if (!not_found)
2754 +                               not_found = name_len - here->e_name_len;
2755 +                       if (!not_found)
2756 +                               not_found = memcmp(name, here->e_name,name_len);
2757 +                       if (not_found <= 0)
2758 +                               break;
2759 +                       here = next;
2760 +               }
2761 +               last = here;
2762 +               /* We still need to compute min_offs and last. */
2763 +               while (!IS_LAST_ENTRY(last)) {
2764 +                       struct ext3_xattr_entry *next = EXT3_XATTR_NEXT(last);
2765 +                       if ((char *)next >= end)
2766 +                               goto bad_block;
2767 +                       if (!last->e_value_block && last->e_value_size) {
2768 +                               int offs = le16_to_cpu(last->e_value_offs);
2769 +                               if (offs < min_offs)
2770 +                                       min_offs = offs;
2771 +                       }
2772 +                       last = next;
2773 +               }
2774 +
2775 +               /* Check whether we have enough space left. */
2776 +               free = min_offs - ((char*)last - (char*)header) - sizeof(__u32);
2777 +       } else {
2778 +               /* We will use a new extended attribute block. */
2779 +               free = sb->s_blocksize -
2780 +                       sizeof(struct ext3_xattr_header) - sizeof(__u32);
2781 +               here = last = NULL;  /* avoid gcc uninitialized warning. */
2782 +       }
2783 +
2784 +       if (not_found) {
2785 +               /* Request to remove a nonexistent attribute? */
2786 +               error = -ENOATTR;
2787 +               if (flags & XATTR_REPLACE)
2788 +                       goto cleanup;
2789 +               error = 0;
2790 +               if (value == NULL)
2791 +                       goto cleanup;
2792 +               else
2793 +                       free -= EXT3_XATTR_LEN(name_len);
2794 +       } else {
2795 +               /* Request to create an existing attribute? */
2796 +               error = -EEXIST;
2797 +               if (flags & XATTR_CREATE)
2798 +                       goto cleanup;
2799 +               if (!here->e_value_block && here->e_value_size) {
2800 +                       unsigned int size = le32_to_cpu(here->e_value_size);
2801 +
2802 +                       if (le16_to_cpu(here->e_value_offs) + size > 
2803 +                           sb->s_blocksize || size > sb->s_blocksize)
2804 +                               goto bad_block;
2805 +                       free += EXT3_XATTR_SIZE(size);
2806 +               }
2807 +       }
2808 +       free -= EXT3_XATTR_SIZE(value_len);
2809 +       error = -ENOSPC;
2810 +       if (free < 0)
2811 +               goto cleanup;
2812 +
2813 +       /* Here we know that we can set the new attribute. */
2814 +
2815 +       if (header) {
2816 +               if (header->h_refcount == cpu_to_le32(1)) {
2817 +                       ea_bdebug(bh, "modifying in-place");
2818 +                       ext3_xattr_cache_remove(bh);
2819 +                       error = ext3_journal_get_write_access(handle, bh);
2820 +                       if (error)
2821 +                               goto cleanup;
2822 +               } else {
2823 +                       int offset;
2824 +
2825 +                       ea_bdebug(bh, "cloning");
2826 +                       header = kmalloc(bh->b_size, GFP_KERNEL);
2827 +                       error = -ENOMEM;
2828 +                       if (header == NULL)
2829 +                               goto cleanup;
2830 +                       memcpy(header, HDR(bh), bh->b_size);
2831 +                       header->h_refcount = cpu_to_le32(1);
2832 +                       offset = (char *)header - bh->b_data;
2833 +                       here = ENTRY((char *)here + offset);
2834 +                       last = ENTRY((char *)last + offset);
2835 +               }
2836 +       } else {
2837 +               /* Allocate a buffer where we construct the new block. */
2838 +               header = kmalloc(sb->s_blocksize, GFP_KERNEL);
2839 +               error = -ENOMEM;
2840 +               if (header == NULL)
2841 +                       goto cleanup;
2842 +               memset(header, 0, sb->s_blocksize);
2843 +               end = (char *)header + sb->s_blocksize;
2844 +               header->h_magic = cpu_to_le32(EXT3_XATTR_MAGIC);
2845 +               header->h_blocks = header->h_refcount = cpu_to_le32(1);
2846 +               last = here = ENTRY(header+1);
2847 +       }
2848 +
2849 +       if (not_found) {
2850 +               /* Insert the new name. */
2851 +               int size = EXT3_XATTR_LEN(name_len);
2852 +               int rest = (char *)last - (char *)here;
2853 +               memmove((char *)here + size, here, rest);
2854 +               memset(here, 0, size);
2855 +               here->e_name_index = name_index;
2856 +               here->e_name_len = name_len;
2857 +               memcpy(here->e_name, name, name_len);
2858 +       } else {
2859 +               /* Remove the old value. */
2860 +               if (!here->e_value_block && here->e_value_size) {
2861 +                       char *first_val = (char *)header + min_offs;
2862 +                       int offs = le16_to_cpu(here->e_value_offs);
2863 +                       char *val = (char *)header + offs;
2864 +                       size_t size = EXT3_XATTR_SIZE(
2865 +                               le32_to_cpu(here->e_value_size));
2866 +                       memmove(first_val + size, first_val, val - first_val);
2867 +                       memset(first_val, 0, size);
2868 +                       here->e_value_offs = 0;
2869 +                       min_offs += size;
2870 +
2871 +                       /* Adjust all value offsets. */
2872 +                       last = ENTRY(header+1);
2873 +                       while (!IS_LAST_ENTRY(last)) {
2874 +                               int o = le16_to_cpu(last->e_value_offs);
2875 +                               if (!last->e_value_block && o < offs)
2876 +                                       last->e_value_offs =
2877 +                                               cpu_to_le16(o + size);
2878 +                               last = EXT3_XATTR_NEXT(last);
2879 +                       }
2880 +               }
2881 +               if (value == NULL) {
2882 +                       /* Remove this attribute. */
2883 +                       if (EXT3_XATTR_NEXT(ENTRY(header+1)) == last) {
2884 +                               /* This block is now empty. */
2885 +                               error = ext3_xattr_set2(handle, inode, bh,NULL);
2886 +                               goto cleanup;
2887 +                       } else {
2888 +                               /* Remove the old name. */
2889 +                               int size = EXT3_XATTR_LEN(name_len);
2890 +                               last = ENTRY((char *)last - size);
2891 +                               memmove(here, (char*)here + size,
2892 +                                       (char*)last - (char*)here);
2893 +                               memset(last, 0, size);
2894 +                       }
2895 +               }
2896 +       }
2897 +
2898 +       if (value != NULL) {
2899 +               /* Insert the new value. */
2900 +               here->e_value_size = cpu_to_le32(value_len);
2901 +               if (value_len) {
2902 +                       size_t size = EXT3_XATTR_SIZE(value_len);
2903 +                       char *val = (char *)header + min_offs - size;
2904 +                       here->e_value_offs =
2905 +                               cpu_to_le16((char *)val - (char *)header);
2906 +                       memset(val + size - EXT3_XATTR_PAD, 0,
2907 +                              EXT3_XATTR_PAD); /* Clear the pad bytes. */
2908 +                       memcpy(val, value, value_len);
2909 +               }
2910 +       }
2911 +       ext3_xattr_rehash(header, here);
2912 +
2913 +       error = ext3_xattr_set2(handle, inode, bh, header);
2914 +
2915 +cleanup:
2916 +       brelse(bh);
2917 +       if (!(bh && header == HDR(bh)))
2918 +               kfree(header);
2919 +       up(&ext3_xattr_sem);
2920 +
2921 +       return error;
2922 +}
2923 +
2924 +/*
2925 + * Second half of ext3_xattr_set(): Update the file system.
2926 + */
2927 +static int
2928 +ext3_xattr_set2(handle_t *handle, struct inode *inode,
2929 +               struct buffer_head *old_bh, struct ext3_xattr_header *header)
2930 +{
2931 +       struct super_block *sb = inode->i_sb;
2932 +       struct buffer_head *new_bh = NULL;
2933 +       int error;
2934 +
2935 +       if (header) {
2936 +               new_bh = ext3_xattr_cache_find(inode, header);
2937 +               if (new_bh) {
2938 +                       /*
2939 +                        * We found an identical block in the cache.
2940 +                        * The old block will be released after updating
2941 +                        * the inode.
2942 +                        */
2943 +                       ea_bdebug(old_bh, "reusing block %ld",
2944 +                               new_bh->b_blocknr);
2945 +                       
2946 +                       error = -EDQUOT;
2947 +                       if (ext3_xattr_quota_alloc(inode, 1))
2948 +                               goto cleanup;
2949 +                       
2950 +                       error = ext3_journal_get_write_access(handle, new_bh);
2951 +                       if (error)
2952 +                               goto cleanup;
2953 +                       HDR(new_bh)->h_refcount = cpu_to_le32(
2954 +                               le32_to_cpu(HDR(new_bh)->h_refcount) + 1);
2955 +                       ea_bdebug(new_bh, "refcount now=%d",
2956 +                               le32_to_cpu(HDR(new_bh)->h_refcount));
2957 +               } else if (old_bh && header == HDR(old_bh)) {
2958 +                       /* Keep this block. */
2959 +                       new_bh = old_bh;
2960 +                       (void)ext3_xattr_cache_insert(new_bh);
2961 +               } else {
2962 +                       /* We need to allocate a new block */
2963 +                       int force = EXT3_I(inode)->i_file_acl != 0;
2964 +                       int block = ext3_xattr_new_block(handle, inode,
2965 +                                                        &error, force);
2966 +                       if (error)
2967 +                               goto cleanup;
2968 +                       ea_idebug(inode, "creating block %d", block);
2969 +
2970 +                       new_bh = sb_getblk(sb, block);
2971 +                       if (!new_bh) {
2972 +getblk_failed:                 ext3_xattr_free_block(handle, inode, block);
2973 +                               error = -EIO;
2974 +                               goto cleanup;
2975 +                       }
2976 +                       lock_buffer(new_bh);
2977 +                       error = ext3_journal_get_create_access(handle, new_bh);
2978 +                       if (error) {
2979 +                               unlock_buffer(new_bh);
2980 +                               goto getblk_failed;
2981 +                       }
2982 +                       memcpy(new_bh->b_data, header, new_bh->b_size);
2983 +                       mark_buffer_uptodate(new_bh, 1);
2984 +                       unlock_buffer(new_bh);
2985 +                       (void)ext3_xattr_cache_insert(new_bh);
2986 +                       
2987 +                       ext3_xattr_update_super_block(handle, sb);
2988 +               }
2989 +               error = ext3_journal_dirty_metadata(handle, new_bh);
2990 +               if (error)
2991 +                       goto cleanup;
2992 +       }
2993 +
2994 +       /* Update the inode. */
2995 +       EXT3_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
2996 +       inode->i_ctime = CURRENT_TIME;
2997 +       ext3_mark_inode_dirty(handle, inode);
2998 +       if (IS_SYNC(inode))
2999 +               handle->h_sync = 1;
3000 +
3001 +       error = 0;
3002 +       if (old_bh && old_bh != new_bh) {
3003 +               /*
3004 +                * If there was an old block, and we are not still using it,
3005 +                * we now release the old block.
3006 +               */
3007 +               unsigned int refcount = le32_to_cpu(HDR(old_bh)->h_refcount);
3008 +
3009 +               error = ext3_journal_get_write_access(handle, old_bh);
3010 +               if (error)
3011 +                       goto cleanup;
3012 +               if (refcount == 1) {
3013 +                       /* Free the old block. */
3014 +                       ea_bdebug(old_bh, "freeing");
3015 +                       ext3_xattr_free_block(handle, inode, old_bh->b_blocknr);
3016 +
3017 +                       /* ext3_forget() calls bforget() for us, but we
3018 +                          let our caller release old_bh, so we need to
3019 +                          duplicate the handle before. */
3020 +                       get_bh(old_bh);
3021 +                       ext3_forget(handle, 1, inode, old_bh,old_bh->b_blocknr);
3022 +               } else {
3023 +                       /* Decrement the refcount only. */
3024 +                       refcount--;
3025 +                       HDR(old_bh)->h_refcount = cpu_to_le32(refcount);
3026 +                       ext3_xattr_quota_free(inode);
3027 +                       ext3_journal_dirty_metadata(handle, old_bh);
3028 +                       ea_bdebug(old_bh, "refcount now=%d", refcount);
3029 +               }
3030 +       }
3031 +
3032 +cleanup:
3033 +       if (old_bh != new_bh)
3034 +               brelse(new_bh);
3035 +
3036 +       return error;
3037 +}
3038 +
3039 +/*
3040 + * ext3_xattr_delete_inode()
3041 + *
3042 + * Free extended attribute resources associated with this inode. This
3043 + * is called immediately before an inode is freed.
3044 + */
3045 +void
3046 +ext3_xattr_delete_inode(handle_t *handle, struct inode *inode)
3047 +{
3048 +       struct buffer_head *bh;
3049 +       unsigned int block = EXT3_I(inode)->i_file_acl;
3050 +
3051 +       if (!block)
3052 +               return;
3053 +       down(&ext3_xattr_sem);
3054 +
3055 +       bh = sb_bread(inode->i_sb, block);
3056 +       if (!bh) {
3057 +               ext3_error(inode->i_sb, "ext3_xattr_delete_inode",
3058 +                       "inode %ld: block %d read error", inode->i_ino, block);
3059 +               goto cleanup;
3060 +       }
3061 +       ea_bdebug(bh, "b_count=%d", atomic_read(&(bh->b_count)));
3062 +       if (HDR(bh)->h_magic != cpu_to_le32(EXT3_XATTR_MAGIC) ||
3063 +           HDR(bh)->h_blocks != cpu_to_le32(1)) {
3064 +               ext3_error(inode->i_sb, "ext3_xattr_delete_inode",
3065 +                       "inode %ld: bad block %d", inode->i_ino, block);
3066 +               goto cleanup;
3067 +       }
3068 +       ext3_journal_get_write_access(handle, bh);
3069 +       ea_bdebug(bh, "refcount now=%d", le32_to_cpu(HDR(bh)->h_refcount) - 1);
3070 +       if (HDR(bh)->h_refcount == cpu_to_le32(1)) {
3071 +               ext3_xattr_cache_remove(bh);
3072 +               ext3_xattr_free_block(handle, inode, block);
3073 +               ext3_forget(handle, 1, inode, bh, block);
3074 +               bh = NULL;
3075 +       } else {
3076 +               HDR(bh)->h_refcount = cpu_to_le32(
3077 +                       le32_to_cpu(HDR(bh)->h_refcount) - 1);
3078 +               ext3_journal_dirty_metadata(handle, bh);
3079 +               if (IS_SYNC(inode))
3080 +                       handle->h_sync = 1;
3081 +               ext3_xattr_quota_free(inode);
3082 +       }
3083 +       EXT3_I(inode)->i_file_acl = 0;
3084 +
3085 +cleanup:
3086 +       brelse(bh);
3087 +       up(&ext3_xattr_sem);
3088 +}
3089 +
3090 +/*
3091 + * ext3_xattr_put_super()
3092 + *
3093 + * This is called when a file system is unmounted.
3094 + */
3095 +void
3096 +ext3_xattr_put_super(struct super_block *sb)
3097 +{
3098 +#ifdef CONFIG_EXT3_FS_XATTR_SHARING
3099 +       mb_cache_shrink(ext3_xattr_cache, sb->s_dev);
3100 +#endif
3101 +}
3102 +
3103 +#ifdef CONFIG_EXT3_FS_XATTR_SHARING
3104 +
3105 +/*
3106 + * ext3_xattr_cache_insert()
3107 + *
3108 + * Create a new entry in the extended attribute cache, and insert
3109 + * it unless such an entry is already in the cache.
3110 + *
3111 + * Returns 0, or a negative error number on failure.
3112 + */
3113 +static int
3114 +ext3_xattr_cache_insert(struct buffer_head *bh)
3115 +{
3116 +       __u32 hash = le32_to_cpu(HDR(bh)->h_hash);
3117 +       struct mb_cache_entry *ce;
3118 +       int error;
3119 +
3120 +       ce = mb_cache_entry_alloc(ext3_xattr_cache);
3121 +       if (!ce)
3122 +               return -ENOMEM;
3123 +       error = mb_cache_entry_insert(ce, bh->b_dev, bh->b_blocknr, &hash);
3124 +       if (error) {
3125 +               mb_cache_entry_free(ce);
3126 +               if (error == -EBUSY) {
3127 +                       ea_bdebug(bh, "already in cache (%d cache entries)",
3128 +                               atomic_read(&ext3_xattr_cache->c_entry_count));
3129 +                       error = 0;
3130 +               }
3131 +       } else {
3132 +               ea_bdebug(bh, "inserting [%x] (%d cache entries)", (int)hash,
3133 +                         atomic_read(&ext3_xattr_cache->c_entry_count));
3134 +               mb_cache_entry_release(ce);
3135 +       }
3136 +       return error;
3137 +}
3138 +
3139 +/*
3140 + * ext3_xattr_cmp()
3141 + *
3142 + * Compare two extended attribute blocks for equality.
3143 + *
3144 + * Returns 0 if the blocks are equal, 1 if they differ, and
3145 + * a negative error number on errors.
3146 + */
3147 +static int
3148 +ext3_xattr_cmp(struct ext3_xattr_header *header1,
3149 +              struct ext3_xattr_header *header2)
3150 +{
3151 +       struct ext3_xattr_entry *entry1, *entry2;
3152 +
3153 +       entry1 = ENTRY(header1+1);
3154 +       entry2 = ENTRY(header2+1);
3155 +       while (!IS_LAST_ENTRY(entry1)) {
3156 +               if (IS_LAST_ENTRY(entry2))
3157 +                       return 1;
3158 +               if (entry1->e_hash != entry2->e_hash ||
3159 +                   entry1->e_name_len != entry2->e_name_len ||
3160 +                   entry1->e_value_size != entry2->e_value_size ||
3161 +                   memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
3162 +                       return 1;
3163 +               if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
3164 +                       return -EIO;
3165 +               if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
3166 +                          (char *)header2 + le16_to_cpu(entry2->e_value_offs),
3167 +                          le32_to_cpu(entry1->e_value_size)))
3168 +                       return 1;
3169 +
3170 +               entry1 = EXT3_XATTR_NEXT(entry1);
3171 +               entry2 = EXT3_XATTR_NEXT(entry2);
3172 +       }
3173 +       if (!IS_LAST_ENTRY(entry2))
3174 +               return 1;
3175 +       return 0;
3176 +}
3177 +
3178 +/*
3179 + * ext3_xattr_cache_find()
3180 + *
3181 + * Find an identical extended attribute block.
3182 + *
3183 + * Returns a pointer to the block found, or NULL if such a block was
3184 + * not found or an error occurred.
3185 + */
3186 +static struct buffer_head *
3187 +ext3_xattr_cache_find(struct inode *inode, struct ext3_xattr_header *header)
3188 +{
3189 +       __u32 hash = le32_to_cpu(header->h_hash);
3190 +       struct mb_cache_entry *ce;
3191 +
3192 +       if (!header->h_hash)
3193 +               return NULL;  /* never share */
3194 +       ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
3195 +       ce = mb_cache_entry_find_first(ext3_xattr_cache, 0, inode->i_dev, hash);
3196 +       while (ce) {
3197 +               struct buffer_head *bh = sb_bread(inode->i_sb, ce->e_block);
3198 +
3199 +               if (!bh) {
3200 +                       ext3_error(inode->i_sb, "ext3_xattr_cache_find",
3201 +                               "inode %ld: block %ld read error",
3202 +                               inode->i_ino, ce->e_block);
3203 +               } else if (le32_to_cpu(HDR(bh)->h_refcount) >
3204 +                          EXT3_XATTR_REFCOUNT_MAX) {
3205 +                       ea_idebug(inode, "block %ld refcount %d>%d",ce->e_block,
3206 +                               le32_to_cpu(HDR(bh)->h_refcount),
3207 +                               EXT3_XATTR_REFCOUNT_MAX);
3208 +               } else if (!ext3_xattr_cmp(header, HDR(bh))) {
3209 +                       ea_bdebug(bh, "b_count=%d",atomic_read(&(bh->b_count)));
3210 +                       mb_cache_entry_release(ce);
3211 +                       return bh;
3212 +               }
3213 +               brelse(bh);
3214 +               ce = mb_cache_entry_find_next(ce, 0, inode->i_dev, hash);
3215 +       }
3216 +       return NULL;
3217 +}
3218 +
3219 +/*
3220 + * ext3_xattr_cache_remove()
3221 + *
3222 + * Remove the cache entry of a block from the cache. Called when a
3223 + * block becomes invalid.
3224 + */
3225 +static void
3226 +ext3_xattr_cache_remove(struct buffer_head *bh)
3227 +{
3228 +       struct mb_cache_entry *ce;
3229 +
3230 +       ce = mb_cache_entry_get(ext3_xattr_cache, bh->b_dev, bh->b_blocknr);
3231 +       if (ce) {
3232 +               ea_bdebug(bh, "removing (%d cache entries remaining)",
3233 +                         atomic_read(&ext3_xattr_cache->c_entry_count)-1);
3234 +               mb_cache_entry_free(ce);
3235 +       } else 
3236 +               ea_bdebug(bh, "no cache entry");
3237 +}
3238 +
3239 +#define NAME_HASH_SHIFT 5
3240 +#define VALUE_HASH_SHIFT 16
3241 +
3242 +/*
3243 + * ext3_xattr_hash_entry()
3244 + *
3245 + * Compute the hash of an extended attribute.
3246 + */
3247 +static inline void ext3_xattr_hash_entry(struct ext3_xattr_header *header,
3248 +                                        struct ext3_xattr_entry *entry)
3249 +{
3250 +       __u32 hash = 0;
3251 +       char *name = entry->e_name;
3252 +       int n;
3253 +
3254 +       for (n=0; n < entry->e_name_len; n++) {
3255 +               hash = (hash << NAME_HASH_SHIFT) ^
3256 +                      (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
3257 +                      *name++;
3258 +       }
3259 +
3260 +       if (entry->e_value_block == 0 && entry->e_value_size != 0) {
3261 +               __u32 *value = (__u32 *)((char *)header +
3262 +                       le16_to_cpu(entry->e_value_offs));
3263 +               for (n = (le32_to_cpu(entry->e_value_size) +
3264 +                    EXT3_XATTR_ROUND) >> EXT3_XATTR_PAD_BITS; n; n--) {
3265 +                       hash = (hash << VALUE_HASH_SHIFT) ^
3266 +                              (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
3267 +                              le32_to_cpu(*value++);
3268 +               }
3269 +       }
3270 +       entry->e_hash = cpu_to_le32(hash);
3271 +}
3272 +
3273 +#undef NAME_HASH_SHIFT
3274 +#undef VALUE_HASH_SHIFT
3275 +
3276 +#define BLOCK_HASH_SHIFT 16
3277 +
3278 +/*
3279 + * ext3_xattr_rehash()
3280 + *
3281 + * Re-compute the extended attribute hash value after an entry has changed.
3282 + */
3283 +static void ext3_xattr_rehash(struct ext3_xattr_header *header,
3284 +                             struct ext3_xattr_entry *entry)
3285 +{
3286 +       struct ext3_xattr_entry *here;
3287 +       __u32 hash = 0;
3288 +       
3289 +       ext3_xattr_hash_entry(header, entry);
3290 +       here = ENTRY(header+1);
3291 +       while (!IS_LAST_ENTRY(here)) {
3292 +               if (!here->e_hash) {
3293 +                       /* Block is not shared if an entry's hash value == 0 */
3294 +                       hash = 0;
3295 +                       break;
3296 +               }
3297 +               hash = (hash << BLOCK_HASH_SHIFT) ^
3298 +                      (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
3299 +                      le32_to_cpu(here->e_hash);
3300 +               here = EXT3_XATTR_NEXT(here);
3301 +       }
3302 +       header->h_hash = cpu_to_le32(hash);
3303 +}
3304 +
3305 +#undef BLOCK_HASH_SHIFT
3306 +
3307 +int __init
3308 +init_ext3_xattr(void)
3309 +{
3310 +       ext3_xattr_cache = mb_cache_create("ext3_xattr", NULL,
3311 +               sizeof(struct mb_cache_entry) +
3312 +               sizeof(struct mb_cache_entry_index), 1, 61);
3313 +       if (!ext3_xattr_cache)
3314 +               return -ENOMEM;
3315 +
3316 +       return 0;
3317 +}
3318 +
3319 +void
3320 +exit_ext3_xattr(void)
3321 +{
3322 +       if (ext3_xattr_cache)
3323 +               mb_cache_destroy(ext3_xattr_cache);
3324 +       ext3_xattr_cache = NULL;
3325 +}
3326 +
3327 +#else  /* CONFIG_EXT3_FS_XATTR_SHARING */
3328 +
3329 +int __init
3330 +init_ext3_xattr(void)
3331 +{
3332 +       return 0;
3333 +}
3334 +
3335 +void
3336 +exit_ext3_xattr(void)
3337 +{
3338 +}
3339 +
3340 +#endif  /* CONFIG_EXT3_FS_XATTR_SHARING */
3341 --- /dev/null   Mon May 20 21:11:23 2002
3342 +++ linux-mmonroe/fs/ext3/xattr_user.c  Fri May 16 08:43:01 2003
3343 @@ -0,0 +1,111 @@
3344 +/*
3345 + * linux/fs/ext3/xattr_user.c
3346 + * Handler for extended user attributes.
3347 + *
3348 + * Copyright (C) 2001 by Andreas Gruenbacher, <a.gruenbacher@computer.org>
3349 + */
3350 +
3351 +#include <linux/module.h>
3352 +#include <linux/string.h>
3353 +#include <linux/fs.h>
3354 +#include <linux/ext3_jbd.h>
3355 +#include <linux/ext3_fs.h>
3356 +#include <linux/ext3_xattr.h>
3357 +
3358 +#ifdef CONFIG_EXT3_FS_POSIX_ACL
3359 +# include <linux/ext3_acl.h>
3360 +#endif
3361 +
3362 +#define XATTR_USER_PREFIX "user."
3363 +
3364 +static size_t
3365 +ext3_xattr_user_list(char *list, struct inode *inode,
3366 +                    const char *name, int name_len)
3367 +{
3368 +       const int prefix_len = sizeof(XATTR_USER_PREFIX)-1;
3369 +
3370 +       if (!test_opt(inode->i_sb, XATTR_USER))
3371 +               return 0;
3372 +
3373 +       if (list) {
3374 +               memcpy(list, XATTR_USER_PREFIX, prefix_len);
3375 +               memcpy(list+prefix_len, name, name_len);
3376 +               list[prefix_len + name_len] = '\0';
3377 +       }
3378 +       return prefix_len + name_len + 1;
3379 +}
3380 +
3381 +static int
3382 +ext3_xattr_user_get(struct inode *inode, const char *name,
3383 +                   void *buffer, size_t size)
3384 +{
3385 +       int error;
3386 +
3387 +       if (strcmp(name, "") == 0)
3388 +               return -EINVAL;
3389 +       if (!test_opt(inode->i_sb, XATTR_USER))
3390 +               return -ENOTSUP;
3391 +#ifdef CONFIG_EXT3_FS_POSIX_ACL
3392 +       error = ext3_permission_locked(inode, MAY_READ);
3393 +#else
3394 +       error = permission(inode, MAY_READ);
3395 +#endif
3396 +       if (error)
3397 +               return error;
3398 +
3399 +       return ext3_xattr_get(inode, EXT3_XATTR_INDEX_USER, name,
3400 +                             buffer, size);
3401 +}
3402 +
3403 +static int
3404 +ext3_xattr_user_set(struct inode *inode, const char *name,
3405 +                   const void *value, size_t size, int flags)
3406 +{
3407 +       handle_t *handle;
3408 +       int error;
3409 +
3410 +       if (strcmp(name, "") == 0)
3411 +               return -EINVAL;
3412 +       if (!test_opt(inode->i_sb, XATTR_USER))
3413 +               return -ENOTSUP;
3414 +       if ( !S_ISREG(inode->i_mode) &&
3415 +           (!S_ISDIR(inode->i_mode) || inode->i_mode & S_ISVTX))
3416 +               return -EPERM;
3417 +#ifdef CONFIG_EXT3_FS_POSIX_ACL
3418 +       error = ext3_permission_locked(inode, MAY_WRITE);
3419 +#else
3420 +       error = permission(inode, MAY_WRITE);
3421 +#endif
3422 +       if (error)
3423 +               return error;
3424 +
3425 +       handle = ext3_journal_start(inode, EXT3_XATTR_TRANS_BLOCKS);
3426 +       if (IS_ERR(handle))
3427 +               return PTR_ERR(handle);
3428 +       error = ext3_xattr_set(handle, inode, EXT3_XATTR_INDEX_USER, name,
3429 +                              value, size, flags);
3430 +       ext3_journal_stop(handle, inode);
3431 +
3432 +       return error;
3433 +}
3434 +
3435 +struct ext3_xattr_handler ext3_xattr_user_handler = {
3436 +       prefix: XATTR_USER_PREFIX,
3437 +       list:   ext3_xattr_user_list,
3438 +       get:    ext3_xattr_user_get,
3439 +       set:    ext3_xattr_user_set,
3440 +};
3441 +
3442 +int __init
3443 +init_ext3_xattr_user(void)
3444 +{
3445 +       return ext3_xattr_register(EXT3_XATTR_INDEX_USER,
3446 +                                  &ext3_xattr_user_handler);
3447 +}
3448 +
3449 +void
3450 +exit_ext3_xattr_user(void)
3451 +{
3452 +       ext3_xattr_unregister(EXT3_XATTR_INDEX_USER,
3453 +                             &ext3_xattr_user_handler);
3454 +}
3455 --- /dev/null   Mon May 20 21:11:23 2002
3456 +++ linux-mmonroe/fs/ext3/ext3-exports.c        Fri May 16 08:43:01 2003
3457 @@ -0,0 +1,13 @@
3458 +#include <linux/config.h>
3459 +#include <linux/module.h>
3460 +#include <linux/ext3_fs.h>
3461 +#include <linux/ext3_jbd.h>
3462 +#include <linux/ext3_xattr.h>
3463 +
3464 +EXPORT_SYMBOL(ext3_force_commit);
3465 +EXPORT_SYMBOL(ext3_bread);
3466 +EXPORT_SYMBOL(ext3_xattr_register);
3467 +EXPORT_SYMBOL(ext3_xattr_unregister);
3468 +EXPORT_SYMBOL(ext3_xattr_get);
3469 +EXPORT_SYMBOL(ext3_xattr_list);
3470 +EXPORT_SYMBOL(ext3_xattr_set);
3471 --- linux/fs/jfs/jfs_xattr.h~linux-2.4.20-xattr-0.8.54-hp       Thu Nov 28 15:53:15 2002
3472 +++ linux-mmonroe/fs/jfs/jfs_xattr.h    Fri May 16 08:43:01 2003
3473 @@ -52,8 +52,10 @@ struct jfs_ea_list {
3474  #define        END_EALIST(ealist) \
3475         ((struct jfs_ea *) (((char *) (ealist)) + EALIST_SIZE(ealist)))
3476  
3477 -extern int __jfs_setxattr(struct inode *, const char *, void *, size_t, int);
3478 -extern int jfs_setxattr(struct dentry *, const char *, void *, size_t, int);
3479 +extern int __jfs_setxattr(struct inode *, const char *, const void *, size_t,
3480 +                         int);
3481 +extern int jfs_setxattr(struct dentry *, const char *, const void *, size_t,
3482 +                       int);
3483  extern ssize_t __jfs_getxattr(struct inode *, const char *, void *, size_t);
3484  extern ssize_t jfs_getxattr(struct dentry *, const char *, void *, size_t);
3485  extern ssize_t jfs_listxattr(struct dentry *, char *, size_t);
3486 --- linux/fs/jfs/xattr.c~linux-2.4.20-xattr-0.8.54-hp   Thu Nov 28 15:53:15 2002
3487 +++ linux-mmonroe/fs/jfs/xattr.c        Fri May 16 08:43:01 2003
3488 @@ -641,7 +641,7 @@ static int ea_put(struct inode *inode, s
3489  }
3490  
3491  static int can_set_xattr(struct inode *inode, const char *name,
3492 -                        void *value, size_t value_len)
3493 +                        const void *value, size_t value_len)
3494  {
3495         if (IS_RDONLY(inode))
3496                 return -EROFS;
3497 @@ -660,7 +660,7 @@ static int can_set_xattr(struct inode *i
3498         return permission(inode, MAY_WRITE);
3499  }
3500  
3501 -int __jfs_setxattr(struct inode *inode, const char *name, void *value,
3502 +int __jfs_setxattr(struct inode *inode, const char *name, const void *value,
3503                    size_t value_len, int flags)
3504  {
3505         struct jfs_ea_list *ealist;
3506 @@ -799,7 +799,7 @@ int __jfs_setxattr(struct inode *inode, 
3507         return rc;
3508  }
3509  
3510 -int jfs_setxattr(struct dentry *dentry, const char *name, void *value,
3511 +int jfs_setxattr(struct dentry *dentry, const char *name, const void *value,
3512                  size_t value_len, int flags)
3513  {
3514         if (value == NULL) {    /* empty EA, do not remove */
3515 --- /dev/null   Mon May 20 21:11:23 2002
3516 +++ linux-mmonroe/fs/mbcache.c  Fri May 16 08:43:01 2003
3517 @@ -0,0 +1,648 @@
3518 +/*
3519 + * linux/fs/mbcache.c
3520 + * (C) 2001-2002 Andreas Gruenbacher, <a.gruenbacher@computer.org>
3521 + */
3522 +
3523 +/*
3524 + * Filesystem Meta Information Block Cache (mbcache)
3525 + *
3526 + * The mbcache caches blocks of block devices that need to be located
3527 + * by their device/block number, as well as by other criteria (such
3528 + * as the block's contents).
3529 + *
3530 + * There can only be one cache entry in a cache per device and block number.
3531 + * Additional indexes need not be unique in this sense. The number of
3532 + * additional indexes (=other criteria) can be hardwired at compile time
3533 + * or specified at cache create time.
3534 + *
3535 + * Each cache entry is of fixed size. An entry may be `valid' or `invalid'
3536 + * in the cache. A valid entry is in the main hash tables of the cache,
3537 + * and may also be in the lru list. An invalid entry is not in any hashes
3538 + * or lists.
3539 + *
3540 + * A valid cache entry is only in the lru list if no handles refer to it.
3541 + * Invalid cache entries will be freed when the last handle to the cache
3542 + * entry is released. Entries that cannot be freed immediately are put
3543 + * back on the lru list.
3544 + */
3545 +
3546 +#include <linux/kernel.h>
3547 +#include <linux/module.h>
3548 +
3549 +#include <linux/fs.h>
3550 +#include <linux/slab.h>
3551 +#include <linux/sched.h>
3552 +#include <linux/cache_def.h>
3553 +#include <linux/version.h>
3554 +#include <linux/init.h>
3555 +#include <linux/mbcache.h>
3556 +
3557 +
3558 +#ifdef MB_CACHE_DEBUG
3559 +# define mb_debug(f...) do { \
3560 +               printk(KERN_DEBUG f); \
3561 +               printk("\n"); \
3562 +       } while (0)
3563 +#define mb_assert(c) do { if (!(c)) \
3564 +               printk(KERN_ERR "assertion " #c " failed\n"); \
3565 +       } while(0)
3566 +#else
3567 +# define mb_debug(f...) do { } while(0)
3568 +# define mb_assert(c) do { } while(0)
3569 +#endif
3570 +#define mb_error(f...) do { \
3571 +               printk(KERN_ERR f); \
3572 +               printk("\n"); \
3573 +       } while(0)
3574 +               
3575 +MODULE_AUTHOR("Andreas Gruenbacher <a.gruenbacher@computer.org>");
3576 +MODULE_DESCRIPTION("Meta block cache (for extended attributes)");
3577 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
3578 +MODULE_LICENSE("GPL");
3579 +#endif
3580 +
3581 +EXPORT_SYMBOL(mb_cache_create);
3582 +EXPORT_SYMBOL(mb_cache_shrink);
3583 +EXPORT_SYMBOL(mb_cache_destroy);
3584 +EXPORT_SYMBOL(mb_cache_entry_alloc);
3585 +EXPORT_SYMBOL(mb_cache_entry_insert);
3586 +EXPORT_SYMBOL(mb_cache_entry_release);
3587 +EXPORT_SYMBOL(mb_cache_entry_takeout);
3588 +EXPORT_SYMBOL(mb_cache_entry_free);
3589 +EXPORT_SYMBOL(mb_cache_entry_dup);
3590 +EXPORT_SYMBOL(mb_cache_entry_get);
3591 +#if !defined(MB_CACHE_INDEXES_COUNT) || (MB_CACHE_INDEXES_COUNT > 0)
3592 +EXPORT_SYMBOL(mb_cache_entry_find_first);
3593 +EXPORT_SYMBOL(mb_cache_entry_find_next);
3594 +#endif
3595 +
3596 +
3597 +/*
3598 + * Global data: list of all mbcache's, lru list, and a spinlock for
3599 + * accessing cache data structures on SMP machines. The lru list is
3600 + * global across all mbcaches.
3601 + */
3602 +
3603 +static LIST_HEAD(mb_cache_list);
3604 +static LIST_HEAD(mb_cache_lru_list);
3605 +static spinlock_t mb_cache_spinlock = SPIN_LOCK_UNLOCKED;
3606 +
3607 +static inline int
3608 +mb_cache_indexes(struct mb_cache *cache)
3609 +{
3610 +#ifdef MB_CACHE_INDEXES_COUNT
3611 +       return MB_CACHE_INDEXES_COUNT;
3612 +#else
3613 +       return cache->c_indexes_count;
3614 +#endif
3615 +}
3616 +
3617 +/*
3618 + * What the mbcache registers as to get shrunk dynamically.
3619 + */
3620 +
3621 +static void
3622 +mb_cache_memory_pressure(int priority, unsigned int gfp_mask);
3623 +
3624 +static struct cache_definition mb_cache_definition = {
3625 +       "mb_cache",
3626 +       mb_cache_memory_pressure
3627 +};
3628 +
3629 +
3630 +static inline int
3631 +__mb_cache_entry_is_hashed(struct mb_cache_entry *ce)
3632 +{
3633 +       return !list_empty(&ce->e_block_list);
3634 +}
3635 +
3636 +
3637 +static inline void
3638 +__mb_cache_entry_unhash(struct mb_cache_entry *ce)
3639 +{
3640 +       int n;
3641 +
3642 +       if (__mb_cache_entry_is_hashed(ce)) {
3643 +               list_del_init(&ce->e_block_list);
3644 +               for (n=0; n<mb_cache_indexes(ce->e_cache); n++)
3645 +                       list_del(&ce->e_indexes[n].o_list);
3646 +       }
3647 +}
3648 +
3649 +
3650 +static inline void
3651 +__mb_cache_entry_forget(struct mb_cache_entry *ce, int gfp_mask)
3652 +{
3653 +       struct mb_cache *cache = ce->e_cache;
3654 +
3655 +       mb_assert(atomic_read(&ce->e_used) == 0);
3656 +       if (cache->c_op.free && cache->c_op.free(ce, gfp_mask)) {
3657 +               /* free failed -- put back on the lru list
3658 +                  for freeing later. */
3659 +               spin_lock(&mb_cache_spinlock);
3660 +               list_add(&ce->e_lru_list, &mb_cache_lru_list);
3661 +               spin_unlock(&mb_cache_spinlock);
3662 +       } else {
3663 +               kmem_cache_free(cache->c_entry_cache, ce);
3664 +               atomic_dec(&cache->c_entry_count);
3665 +       }
3666 +}
3667 +
3668 +
3669 +static inline void
3670 +__mb_cache_entry_release_unlock(struct mb_cache_entry *ce)
3671 +{
3672 +       if (atomic_dec_and_test(&ce->e_used)) {
3673 +               if (__mb_cache_entry_is_hashed(ce))
3674 +                       list_add_tail(&ce->e_lru_list, &mb_cache_lru_list);
3675 +               else {
3676 +                       spin_unlock(&mb_cache_spinlock);
3677 +                       __mb_cache_entry_forget(ce, GFP_KERNEL);
3678 +                       return;
3679 +               }
3680 +       }
3681 +       spin_unlock(&mb_cache_spinlock);
3682 +}
3683 +
3684 +
3685 +/*
3686 + * mb_cache_memory_pressure()  memory pressure callback
3687 + *
3688 + * This function is called by the kernel memory management when memory
3689 + * gets low.
3690 + *
3691 + * @priority: Amount by which to shrink the cache (0 = highes priority)
3692 + * @gfp_mask: (ignored)
3693 + */
3694 +static void
3695 +mb_cache_memory_pressure(int priority, unsigned int gfp_mask)
3696 +{
3697 +       LIST_HEAD(free_list);
3698 +       struct list_head *l, *ltmp;
3699 +       int count = 0;
3700 +
3701 +       spin_lock(&mb_cache_spinlock);
3702 +       list_for_each(l, &mb_cache_list) {
3703 +               struct mb_cache *cache =
3704 +                       list_entry(l, struct mb_cache, c_cache_list);
3705 +               mb_debug("cache %s (%d)", cache->c_name,
3706 +                         atomic_read(&cache->c_entry_count));
3707 +               count += atomic_read(&cache->c_entry_count);
3708 +       }
3709 +       mb_debug("trying to free %d of %d entries",
3710 +                 count / (priority ? priority : 1), count);
3711 +       if (priority)
3712 +               count /= priority;
3713 +       while (count-- && !list_empty(&mb_cache_lru_list)) {
3714 +               struct mb_cache_entry *ce =
3715 +                       list_entry(mb_cache_lru_list.next,
3716 +                                  struct mb_cache_entry, e_lru_list);
3717 +               list_del(&ce->e_lru_list);
3718 +               __mb_cache_entry_unhash(ce);
3719 +               list_add_tail(&ce->e_lru_list, &free_list);
3720 +       }
3721 +       spin_unlock(&mb_cache_spinlock);
3722 +       list_for_each_safe(l, ltmp, &free_list) {
3723 +               __mb_cache_entry_forget(list_entry(l, struct mb_cache_entry,
3724 +                                                  e_lru_list), gfp_mask);
3725 +       }
3726 +}
3727 +
3728 +
3729 +/*
3730 + * mb_cache_create()  create a new cache
3731 + *
3732 + * All entries in one cache are equal size. Cache entries may be from
3733 + * multiple devices. If this is the first mbcache created, registers
3734 + * the cache with kernel memory management. Returns NULL if no more
3735 + * memory was available.
3736 + *
3737 + * @name: name of the cache (informal)
3738 + * @cache_op: contains the callback called when freeing a cache entry
3739 + * @entry_size: The size of a cache entry, including
3740 + *              struct mb_cache_entry
3741 + * @indexes_count: number of additional indexes in the cache. Must equal
3742 + *                 MB_CACHE_INDEXES_COUNT if the number of indexes is
3743 + *                 hardwired.
3744 + * @bucket_count: number of hash buckets
3745 + */
3746 +struct mb_cache *
3747 +mb_cache_create(const char *name, struct mb_cache_op *cache_op,
3748 +               size_t entry_size, int indexes_count, int bucket_count)
3749 +{
3750 +       int m=0, n;
3751 +       struct mb_cache *cache = NULL;
3752 +
3753 +       if(entry_size < sizeof(struct mb_cache_entry) +
3754 +          indexes_count * sizeof(struct mb_cache_entry_index))
3755 +               return NULL;
3756 +
3757 +       MOD_INC_USE_COUNT;
3758 +       cache = kmalloc(sizeof(struct mb_cache) +
3759 +                       indexes_count * sizeof(struct list_head), GFP_KERNEL);
3760 +       if (!cache)
3761 +               goto fail;
3762 +       cache->c_name = name;
3763 +       cache->c_op.free = NULL;
3764 +       if (cache_op)
3765 +               cache->c_op.free = cache_op->free;
3766 +       atomic_set(&cache->c_entry_count, 0);
3767 +       cache->c_bucket_count = bucket_count;
3768 +#ifdef MB_CACHE_INDEXES_COUNT
3769 +       mb_assert(indexes_count == MB_CACHE_INDEXES_COUNT);
3770 +#else
3771 +       cache->c_indexes_count = indexes_count;
3772 +#endif
3773 +       cache->c_block_hash = kmalloc(bucket_count * sizeof(struct list_head),
3774 +                                     GFP_KERNEL);
3775 +       if (!cache->c_block_hash)
3776 +               goto fail;
3777 +       for (n=0; n<bucket_count; n++)
3778 +               INIT_LIST_HEAD(&cache->c_block_hash[n]);
3779 +       for (m=0; m<indexes_count; m++) {
3780 +               cache->c_indexes_hash[m] = kmalloc(bucket_count *
3781 +                                                sizeof(struct list_head),
3782 +                                                GFP_KERNEL);
3783 +               if (!cache->c_indexes_hash[m])
3784 +                       goto fail;
3785 +               for (n=0; n<bucket_count; n++)
3786 +                       INIT_LIST_HEAD(&cache->c_indexes_hash[m][n]);
3787 +       }
3788 +       cache->c_entry_cache = kmem_cache_create(name, entry_size, 0,
3789 +               0 /*SLAB_POISON | SLAB_RED_ZONE*/, NULL, NULL);
3790 +       if (!cache->c_entry_cache)
3791 +               goto fail;
3792 +
3793 +       spin_lock(&mb_cache_spinlock);
3794 +       list_add(&cache->c_cache_list, &mb_cache_list);
3795 +       spin_unlock(&mb_cache_spinlock);
3796 +       return cache;
3797 +
3798 +fail:
3799 +       if (cache) {
3800 +               while (--m >= 0)
3801 +                       kfree(cache->c_indexes_hash[m]);
3802 +               if (cache->c_block_hash)
3803 +                       kfree(cache->c_block_hash);
3804 +               kfree(cache);
3805 +       }
3806 +       MOD_DEC_USE_COUNT;
3807 +       return NULL;
3808 +}
3809 +
3810 +
3811 +/*
3812 + * mb_cache_shrink()
3813 + *
3814 + * Removes all cache entires of a device from the cache. All cache entries
3815 + * currently in use cannot be freed, and thus remain in the cache.
3816 + *
3817 + * @cache: which cache to shrink
3818 + * @dev: which device's cache entries to shrink
3819 + */
3820 +void
3821 +mb_cache_shrink(struct mb_cache *cache, kdev_t dev)
3822 +{
3823 +       LIST_HEAD(free_list);
3824 +       struct list_head *l, *ltmp;
3825 +
3826 +       spin_lock(&mb_cache_spinlock);
3827 +       list_for_each_safe(l, ltmp, &mb_cache_lru_list) {
3828 +               struct mb_cache_entry *ce =
3829 +                       list_entry(l, struct mb_cache_entry, e_lru_list);
3830 +               if (ce->e_dev == dev) {
3831 +                       list_del(&ce->e_lru_list);
3832 +                       list_add_tail(&ce->e_lru_list, &free_list);
3833 +                       __mb_cache_entry_unhash(ce);
3834 +               }
3835 +       }
3836 +       spin_unlock(&mb_cache_spinlock);
3837 +       list_for_each_safe(l, ltmp, &free_list) {
3838 +               __mb_cache_entry_forget(list_entry(l, struct mb_cache_entry,
3839 +                                                  e_lru_list), GFP_KERNEL);
3840 +       }
3841 +}
3842 +
3843 +
3844 +/*
3845 + * mb_cache_destroy()
3846 + *
3847 + * Shrinks the cache to its minimum possible size (hopefully 0 entries),
3848 + * and then destroys it. If this was the last mbcache, un-registers the
3849 + * mbcache from kernel memory management.
3850 + */
3851 +void
3852 +mb_cache_destroy(struct mb_cache *cache)
3853 +{
3854 +       LIST_HEAD(free_list);
3855 +       struct list_head *l, *ltmp;
3856 +       int n;
3857 +
3858 +       spin_lock(&mb_cache_spinlock);
3859 +       list_for_each_safe(l, ltmp, &mb_cache_lru_list) {
3860 +               struct mb_cache_entry *ce =
3861 +                       list_entry(l, struct mb_cache_entry, e_lru_list);
3862 +               if (ce->e_cache == cache) {
3863 +                       list_del(&ce->e_lru_list);
3864 +                       list_add_tail(&ce->e_lru_list, &free_list);
3865 +                       __mb_cache_entry_unhash(ce);
3866 +               }
3867 +       }
3868 +       list_del(&cache->c_cache_list);
3869 +       spin_unlock(&mb_cache_spinlock);
3870 +       list_for_each_safe(l, ltmp, &free_list) {
3871 +               __mb_cache_entry_forget(list_entry(l, struct mb_cache_entry,
3872 +                                                  e_lru_list), GFP_KERNEL);
3873 +       }
3874 +
3875 +       if (atomic_read(&cache->c_entry_count) > 0) {
3876 +               mb_error("cache %s: %d orphaned entries",
3877 +                         cache->c_name,
3878 +                         atomic_read(&cache->c_entry_count));
3879 +       }
3880 +
3881 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0))
3882 +       /* We don't have kmem_cache_destroy() in 2.2.x */
3883 +       kmem_cache_shrink(cache->c_entry_cache);
3884 +#else
3885 +       kmem_cache_destroy(cache->c_entry_cache);
3886 +#endif
3887 +       for (n=0; n < mb_cache_indexes(cache); n++)
3888 +               kfree(cache->c_indexes_hash[n]);
3889 +       kfree(cache->c_block_hash);
3890 +       kfree(cache);
3891 +
3892 +       MOD_DEC_USE_COUNT;
3893 +}
3894 +
3895 +
3896 +/*
3897 + * mb_cache_entry_alloc()
3898 + *
3899 + * Allocates a new cache entry. The new entry will not be valid initially,
3900 + * and thus cannot be looked up yet. It should be filled with data, and
3901 + * then inserted into the cache using mb_cache_entry_insert(). Returns NULL
3902 + * if no more memory was available.
3903 + */
3904 +struct mb_cache_entry *
3905 +mb_cache_entry_alloc(struct mb_cache *cache)
3906 +{
3907 +       struct mb_cache_entry *ce;
3908 +
3909 +       atomic_inc(&cache->c_entry_count);
3910 +       ce = kmem_cache_alloc(cache->c_entry_cache, GFP_KERNEL);
3911 +       if (ce) {
3912 +               INIT_LIST_HEAD(&ce->e_lru_list);
3913 +               INIT_LIST_HEAD(&ce->e_block_list);
3914 +               ce->e_cache = cache;
3915 +               atomic_set(&ce->e_used, 1);
3916 +       }
3917 +       return ce;
3918 +}
3919 +
3920 +
3921 +/*
3922 + * mb_cache_entry_insert()
3923 + *
3924 + * Inserts an entry that was allocated using mb_cache_entry_alloc() into
3925 + * the cache. After this, the cache entry can be looked up, but is not yet
3926 + * in the lru list as the caller still holds a handle to it. Returns 0 on
3927 + * success, or -EBUSY if a cache entry for that device + inode exists
3928 + * already (this may happen after a failed lookup, if another process has
3929 + * inserted the same cache entry in the meantime).
3930 + *
3931 + * @dev: device the cache entry belongs to
3932 + * @block: block number
3933 + * @keys: array of additional keys. There must be indexes_count entries
3934 + *        in the array (as specified when creating the cache).
3935 + */
3936 +int
3937 +mb_cache_entry_insert(struct mb_cache_entry *ce, kdev_t dev,
3938 +                     unsigned long block, unsigned int keys[])
3939 +{
3940 +       struct mb_cache *cache = ce->e_cache;
3941 +       unsigned int bucket = (HASHDEV(dev) + block) % cache->c_bucket_count;
3942 +       struct list_head *l;
3943 +       int error = -EBUSY, n;
3944 +
3945 +       spin_lock(&mb_cache_spinlock);
3946 +       list_for_each(l, &cache->c_block_hash[bucket]) {
3947 +               struct mb_cache_entry *ce =
3948 +                       list_entry(l, struct mb_cache_entry, e_block_list);
3949 +               if (ce->e_dev == dev && ce->e_block == block)
3950 +                       goto out;
3951 +       }
3952 +       __mb_cache_entry_unhash(ce);
3953 +       ce->e_dev = dev;
3954 +       ce->e_block = block;
3955 +       list_add(&ce->e_block_list, &cache->c_block_hash[bucket]);
3956 +       for (n=0; n<mb_cache_indexes(cache); n++) {
3957 +               ce->e_indexes[n].o_key = keys[n];
3958 +               bucket = keys[n] % cache->c_bucket_count;
3959 +               list_add(&ce->e_indexes[n].o_list,
3960 +                        &cache->c_indexes_hash[n][bucket]);
3961 +       }
3962 +out:
3963 +       spin_unlock(&mb_cache_spinlock);
3964 +       return error;
3965 +}
3966 +
3967 +
3968 +/*
3969 + * mb_cache_entry_release()
3970 + *
3971 + * Release a handle to a cache entry. When the last handle to a cache entry
3972 + * is released it is either freed (if it is invalid) or otherwise inserted
3973 + * in to the lru list.
3974 + */
3975 +void
3976 +mb_cache_entry_release(struct mb_cache_entry *ce)
3977 +{
3978 +       spin_lock(&mb_cache_spinlock);
3979 +       __mb_cache_entry_release_unlock(ce);
3980 +}
3981 +
3982 +
3983 +/*
3984 + * mb_cache_entry_takeout()
3985 + *
3986 + * Take a cache entry out of the cache, making it invalid. The entry can later
3987 + * be re-inserted using mb_cache_entry_insert(), or released using
3988 + * mb_cache_entry_release().
3989 + */
3990 +void
3991 +mb_cache_entry_takeout(struct mb_cache_entry *ce)
3992 +{
3993 +       spin_lock(&mb_cache_spinlock);
3994 +       mb_assert(list_empty(&ce->e_lru_list));
3995 +       __mb_cache_entry_unhash(ce);
3996 +       spin_unlock(&mb_cache_spinlock);
3997 +}
3998 +
3999 +
4000 +/*
4001 + * mb_cache_entry_free()
4002 + *
4003 + * This is equivalent to the sequence mb_cache_entry_takeout() --
4004 + * mb_cache_entry_release().
4005 + */
4006 +void
4007 +mb_cache_entry_free(struct mb_cache_entry *ce)
4008 +{
4009 +       spin_lock(&mb_cache_spinlock);
4010 +       mb_assert(list_empty(&ce->e_lru_list));
4011 +       __mb_cache_entry_unhash(ce);
4012 +       __mb_cache_entry_release_unlock(ce);
4013 +}
4014 +
4015 +
4016 +/*
4017 + * mb_cache_entry_dup()
4018 + *
4019 + * Duplicate a handle to a cache entry (does not duplicate the cache entry
4020 + * itself). After the call, both the old and the new handle must be released.
4021 + */
4022 +struct mb_cache_entry *
4023 +mb_cache_entry_dup(struct mb_cache_entry *ce)
4024 +{
4025 +       atomic_inc(&ce->e_used);
4026 +       return ce;
4027 +}
4028 +
4029 +
4030 +/*
4031 + * mb_cache_entry_get()
4032 + *
4033 + * Get a cache entry  by device / block number. (There can only be one entry
4034 + * in the cache per device and block.) Returns NULL if no such cache entry
4035 + * exists.
4036 + */
4037 +struct mb_cache_entry *
4038 +mb_cache_entry_get(struct mb_cache *cache, kdev_t dev, unsigned long block)
4039 +{
4040 +       unsigned int bucket = (HASHDEV(dev) + block) % cache->c_bucket_count;
4041 +       struct list_head *l;
4042 +       struct mb_cache_entry *ce;
4043 +
4044 +       spin_lock(&mb_cache_spinlock);
4045 +       list_for_each(l, &cache->c_block_hash[bucket]) {
4046 +               ce = list_entry(l, struct mb_cache_entry, e_block_list);
4047 +               if (ce->e_dev == dev && ce->e_block == block) {
4048 +                       if (!list_empty(&ce->e_lru_list))
4049 +                               list_del_init(&ce->e_lru_list);
4050 +                       atomic_inc(&ce->e_used);
4051 +                       goto cleanup;
4052 +               }
4053 +       }
4054 +       ce = NULL;
4055 +
4056 +cleanup:
4057 +       spin_unlock(&mb_cache_spinlock);
4058 +       return ce;
4059 +}
4060 +
4061 +#if !defined(MB_CACHE_INDEXES_COUNT) || (MB_CACHE_INDEXES_COUNT > 0)
4062 +
4063 +static struct mb_cache_entry *
4064 +__mb_cache_entry_find(struct list_head *l, struct list_head *head,
4065 +                     int index, kdev_t dev, unsigned int key)
4066 +{
4067 +       while (l != head) {
4068 +               struct mb_cache_entry *ce =
4069 +                       list_entry(l, struct mb_cache_entry,
4070 +                                  e_indexes[index].o_list);
4071 +               if (ce->e_dev == dev && ce->e_indexes[index].o_key == key) {
4072 +                       if (!list_empty(&ce->e_lru_list))
4073 +                               list_del_init(&ce->e_lru_list);
4074 +                       atomic_inc(&ce->e_used);
4075 +                       return ce;
4076 +               }
4077 +               l = l->next;
4078 +       }
4079 +       return NULL;
4080 +}
4081 +
4082 +
4083 +/*
4084 + * mb_cache_entry_find_first()
4085 + *
4086 + * Find the first cache entry on a given device with a certain key in
4087 + * an additional index. Additonal matches can be found with
4088 + * mb_cache_entry_find_next(). Returns NULL if no match was found.
4089 + *
4090 + * @cache: the cache to search
4091 + * @index: the number of the additonal index to search (0<=index<indexes_count)
4092 + * @dev: the device the cache entry should belong to
4093 + * @key: the key in the index
4094 + */
4095 +struct mb_cache_entry *
4096 +mb_cache_entry_find_first(struct mb_cache *cache, int index, kdev_t dev,
4097 +                         unsigned int key)
4098 +{
4099 +       unsigned int bucket = key % cache->c_bucket_count;
4100 +       struct list_head *l;
4101 +       struct mb_cache_entry *ce;
4102 +
4103 +       mb_assert(index < mb_cache_indexes(cache));
4104 +       spin_lock(&mb_cache_spinlock);
4105 +       l = cache->c_indexes_hash[index][bucket].next;
4106 +       ce = __mb_cache_entry_find(l, &cache->c_indexes_hash[index][bucket],
4107 +                                  index, dev, key);
4108 +       spin_unlock(&mb_cache_spinlock);
4109 +       return ce;
4110 +}
4111 +
4112 +
4113 +/*
4114 + * mb_cache_entry_find_next()
4115 + *
4116 + * Find the next cache entry on a given device with a certain key in an
4117 + * additional index. Returns NULL if no match could be found. The previous
4118 + * entry is atomatically released, so that mb_cache_entry_find_next() can
4119 + * be called like this:
4120 + *
4121 + * entry = mb_cache_entry_find_first();
4122 + * while (entry) {
4123 + *     ...
4124 + *     entry = mb_cache_entry_find_next(entry, ...);
4125 + * }
4126 + *
4127 + * @prev: The previous match
4128 + * @index: the number of the additonal index to search (0<=index<indexes_count)
4129 + * @dev: the device the cache entry should belong to
4130 + * @key: the key in the index
4131 + */
4132 +struct mb_cache_entry *
4133 +mb_cache_entry_find_next(struct mb_cache_entry *prev, int index, kdev_t dev,
4134 +                        unsigned int key)
4135 +{
4136 +       struct mb_cache *cache = prev->e_cache;
4137 +       unsigned int bucket = key % cache->c_bucket_count;
4138 +       struct list_head *l;
4139 +       struct mb_cache_entry *ce;
4140 +
4141 +       mb_assert(index < mb_cache_indexes(cache));
4142 +       spin_lock(&mb_cache_spinlock);
4143 +       l = prev->e_indexes[index].o_list.next;
4144 +       ce = __mb_cache_entry_find(l, &cache->c_indexes_hash[index][bucket],
4145 +                                  index, dev, key);
4146 +       __mb_cache_entry_release_unlock(prev);
4147 +       return ce;
4148 +}
4149 +
4150 +#endif  /* !defined(MB_CACHE_INDEXES_COUNT) || (MB_CACHE_INDEXES_COUNT > 0) */
4151 +
4152 +static int __init init_mbcache(void)
4153 +{
4154 +       register_cache(&mb_cache_definition);
4155 +       return 0;
4156 +}
4157 +
4158 +static void __exit exit_mbcache(void)
4159 +{
4160 +       unregister_cache(&mb_cache_definition);
4161 +}
4162 +
4163 +module_init(init_mbcache)
4164 +module_exit(exit_mbcache)
4165 +
4166 --- /dev/null   Mon May 20 21:11:23 2002
4167 +++ linux-mmonroe/include/linux/cache_def.h     Fri May 16 08:43:01 2003
4168 @@ -0,0 +1,15 @@
4169 +/*
4170 + * linux/cache_def.h
4171 + * Handling of caches defined in drivers, filesystems, ...
4172 + *
4173 + * Copyright (C) 2002 by Andreas Gruenbacher, <a.gruenbacher@computer.org>
4174 + */
4175 +
4176 +struct cache_definition {
4177 +       const char *name;
4178 +       void (*shrink)(int, unsigned int);
4179 +       struct list_head link;
4180 +};
4181 +
4182 +extern void register_cache(struct cache_definition *);
4183 +extern void unregister_cache(struct cache_definition *);
4184 --- linux/include/linux/errno.h~linux-2.4.20-xattr-0.8.54-hp    Fri Feb  9 14:46:13 2001
4185 +++ linux-mmonroe/include/linux/errno.h Fri May 16 08:43:01 2003
4186 @@ -23,4 +23,8 @@
4187  
4188  #endif
4189  
4190 +/* Defined for extended attributes */
4191 +#define ENOATTR ENODATA                /* No such attribute */
4192 +#define ENOTSUP EOPNOTSUPP     /* Operation not supported */
4193 +
4194  #endif
4195 --- linux/include/linux/ext2_fs.h~linux-2.4.20-xattr-0.8.54-hp  Thu Nov 22 11:46:52 2001
4196 +++ linux-mmonroe/include/linux/ext2_fs.h       Fri May 16 08:43:01 2003
4197 @@ -57,8 +57,6 @@
4198   */
4199  #define        EXT2_BAD_INO             1      /* Bad blocks inode */
4200  #define EXT2_ROOT_INO           2      /* Root inode */
4201 -#define EXT2_ACL_IDX_INO        3      /* ACL inode */
4202 -#define EXT2_ACL_DATA_INO       4      /* ACL inode */
4203  #define EXT2_BOOT_LOADER_INO    5      /* Boot loader inode */
4204  #define EXT2_UNDEL_DIR_INO      6      /* Undelete directory inode */
4205  
4206 @@ -86,7 +84,6 @@
4207  #else
4208  # define EXT2_BLOCK_SIZE(s)            (EXT2_MIN_BLOCK_SIZE << (s)->s_log_block_size)
4209  #endif
4210 -#define EXT2_ACLE_PER_BLOCK(s)         (EXT2_BLOCK_SIZE(s) / sizeof (struct ext2_acl_entry))
4211  #define        EXT2_ADDR_PER_BLOCK(s)          (EXT2_BLOCK_SIZE(s) / sizeof (__u32))
4212  #ifdef __KERNEL__
4213  # define EXT2_BLOCK_SIZE_BITS(s)       ((s)->s_blocksize_bits)
4214 @@ -121,28 +118,6 @@
4215  #endif
4216  
4217  /*
4218 - * ACL structures
4219 - */
4220 -struct ext2_acl_header /* Header of Access Control Lists */
4221 -{
4222 -       __u32   aclh_size;
4223 -       __u32   aclh_file_count;
4224 -       __u32   aclh_acle_count;
4225 -       __u32   aclh_first_acle;
4226 -};
4227 -
4228 -struct ext2_acl_entry  /* Access Control List Entry */
4229 -{
4230 -       __u32   acle_size;
4231 -       __u16   acle_perms;     /* Access permissions */
4232 -       __u16   acle_type;      /* Type of entry */
4233 -       __u16   acle_tag;       /* User or group identity */
4234 -       __u16   acle_pad1;
4235 -       __u32   acle_next;      /* Pointer on next entry for the */
4236 -                                       /* same inode or on next free entry */
4237 -};
4238 -
4239 -/*
4240   * Structure of a blocks group descriptor
4241   */
4242  struct ext2_group_desc
4243 @@ -314,6 +289,7 @@ struct ext2_inode {
4244  #define EXT2_MOUNT_ERRORS_PANIC                0x0040  /* Panic on errors */
4245  #define EXT2_MOUNT_MINIX_DF            0x0080  /* Mimics the Minix statfs */
4246  #define EXT2_MOUNT_NO_UID32            0x0200  /* Disable 32-bit UIDs */
4247 +#define EXT2_MOUNT_XATTR_USER          0x4000  /* Extended user attributes */
4248  
4249  #define clear_opt(o, opt)              o &= ~EXT2_MOUNT_##opt
4250  #define set_opt(o, opt)                        o |= EXT2_MOUNT_##opt
4251 @@ -397,6 +373,7 @@ struct ext2_super_block {
4252  
4253  #ifdef __KERNEL__
4254  #define EXT2_SB(sb)    (&((sb)->u.ext2_sb))
4255 +#define EXT2_I(inode)  (&((inode)->u.ext2_i))
4256  #else
4257  /* Assume that user mode programs are passing in an ext2fs superblock, not
4258   * a kernel struct super_block.  This will allow us to call the feature-test
4259 @@ -466,7 +443,7 @@ struct ext2_super_block {
4260  #define EXT3_FEATURE_INCOMPAT_JOURNAL_DEV      0x0008
4261  #define EXT2_FEATURE_INCOMPAT_ANY              0xffffffff
4262  
4263 -#define EXT2_FEATURE_COMPAT_SUPP       0
4264 +#define EXT2_FEATURE_COMPAT_SUPP       EXT2_FEATURE_COMPAT_EXT_ATTR
4265  #define EXT2_FEATURE_INCOMPAT_SUPP     EXT2_FEATURE_INCOMPAT_FILETYPE
4266  #define EXT2_FEATURE_RO_COMPAT_SUPP    (EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER| \
4267                                          EXT2_FEATURE_RO_COMPAT_LARGE_FILE| \
4268 @@ -623,8 +600,10 @@ extern struct address_space_operations e
4269  
4270  /* namei.c */
4271  extern struct inode_operations ext2_dir_inode_operations;
4272 +extern struct inode_operations ext2_special_inode_operations;
4273  
4274  /* symlink.c */
4275 +extern struct inode_operations ext2_symlink_inode_operations;
4276  extern struct inode_operations ext2_fast_symlink_inode_operations;
4277  
4278  #endif /* __KERNEL__ */
4279 --- /dev/null   Mon May 20 21:11:23 2002
4280 +++ linux-mmonroe/include/linux/ext2_xattr.h    Fri May 16 08:43:01 2003
4281 @@ -0,0 +1,157 @@
4282 +/*
4283 +  File: linux/ext2_xattr.h
4284 +
4285 +  On-disk format of extended attributes for the ext2 filesystem.
4286 +
4287 +  (C) 2001 Andreas Gruenbacher, <a.gruenbacher@computer.org>
4288 +*/
4289 +
4290 +#include <linux/config.h>
4291 +#include <linux/init.h>
4292 +#include <linux/xattr.h>
4293 +
4294 +/* Magic value in attribute blocks */
4295 +#define EXT2_XATTR_MAGIC               0xEA020000
4296 +
4297 +/* Maximum number of references to one attribute block */
4298 +#define EXT2_XATTR_REFCOUNT_MAX                1024
4299 +
4300 +/* Name indexes */
4301 +#define EXT2_XATTR_INDEX_MAX                   10
4302 +#define EXT2_XATTR_INDEX_USER                  1
4303 +#define EXT2_XATTR_INDEX_POSIX_ACL_ACCESS      2
4304 +#define EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT     3
4305 +
4306 +struct ext2_xattr_header {
4307 +       __u32   h_magic;        /* magic number for identification */
4308 +       __u32   h_refcount;     /* reference count */
4309 +       __u32   h_blocks;       /* number of disk blocks used */
4310 +       __u32   h_hash;         /* hash value of all attributes */
4311 +       __u32   h_reserved[4];  /* zero right now */
4312 +};
4313 +
4314 +struct ext2_xattr_entry {
4315 +       __u8    e_name_len;     /* length of name */
4316 +       __u8    e_name_index;   /* attribute name index */
4317 +       __u16   e_value_offs;   /* offset in disk block of value */
4318 +       __u32   e_value_block;  /* disk block attribute is stored on (n/i) */
4319 +       __u32   e_value_size;   /* size of attribute value */
4320 +       __u32   e_hash;         /* hash value of name and value */
4321 +       char    e_name[0];      /* attribute name */
4322 +};
4323 +
4324 +#define EXT2_XATTR_PAD_BITS            2
4325 +#define EXT2_XATTR_PAD         (1<<EXT2_XATTR_PAD_BITS)
4326 +#define EXT2_XATTR_ROUND               (EXT2_XATTR_PAD-1)
4327 +#define EXT2_XATTR_LEN(name_len) \
4328 +       (((name_len) + EXT2_XATTR_ROUND + \
4329 +       sizeof(struct ext2_xattr_entry)) & ~EXT2_XATTR_ROUND)
4330 +#define EXT2_XATTR_NEXT(entry) \
4331 +       ( (struct ext2_xattr_entry *)( \
4332 +         (char *)(entry) + EXT2_XATTR_LEN((entry)->e_name_len)) )
4333 +#define EXT2_XATTR_SIZE(size) \
4334 +       (((size) + EXT2_XATTR_ROUND) & ~EXT2_XATTR_ROUND)
4335 +
4336 +#ifdef __KERNEL__
4337 +
4338 +# ifdef CONFIG_EXT2_FS_XATTR
4339 +
4340 +struct ext2_xattr_handler {
4341 +       char *prefix;
4342 +       size_t (*list)(char *list, struct inode *inode, const char *name,
4343 +                      int name_len);
4344 +       int (*get)(struct inode *inode, const char *name, void *buffer,
4345 +                  size_t size);
4346 +       int (*set)(struct inode *inode, const char *name, const void *buffer,
4347 +                  size_t size, int flags);
4348 +};
4349 +
4350 +extern int ext2_xattr_register(int, struct ext2_xattr_handler *);
4351 +extern void ext2_xattr_unregister(int, struct ext2_xattr_handler *);
4352 +
4353 +extern int ext2_setxattr(struct dentry *, const char *, const void *, size_t, int);
4354 +extern ssize_t ext2_getxattr(struct dentry *, const char *, void *, size_t);
4355 +extern ssize_t ext2_listxattr(struct dentry *, char *, size_t);
4356 +extern int ext2_removexattr(struct dentry *, const char *);
4357 +
4358 +extern int ext2_xattr_get(struct inode *, int, const char *, void *, size_t);
4359 +extern int ext2_xattr_list(struct inode *, char *, size_t);
4360 +extern int ext2_xattr_set(struct inode *, int, const char *, const void *, size_t, int);
4361 +
4362 +extern void ext2_xattr_delete_inode(struct inode *);
4363 +extern void ext2_xattr_put_super(struct super_block *);
4364 +
4365 +extern int init_ext2_xattr(void) __init;
4366 +extern void exit_ext2_xattr(void);
4367 +
4368 +# else  /* CONFIG_EXT2_FS_XATTR */
4369 +#  define ext2_setxattr                NULL
4370 +#  define ext2_getxattr                NULL
4371 +#  define ext2_listxattr       NULL
4372 +#  define ext2_removexattr     NULL
4373 +
4374 +static inline int
4375 +ext2_xattr_get(struct inode *inode, int name_index,
4376 +              const char *name, void *buffer, size_t size)
4377 +{
4378 +       return -ENOTSUP;
4379 +}
4380 +
4381 +static inline int
4382 +ext2_xattr_list(struct inode *inode, char *buffer, size_t size)
4383 +{
4384 +       return -ENOTSUP;
4385 +}
4386 +
4387 +static inline int
4388 +ext2_xattr_set(struct inode *inode, int name_index, const char *name,
4389 +              const void *value, size_t size, int flags)
4390 +{
4391 +       return -ENOTSUP;
4392 +}
4393 +
4394 +static inline void
4395 +ext2_xattr_delete_inode(struct inode *inode)
4396 +{
4397 +}
4398 +
4399 +static inline void
4400 +ext2_xattr_put_super(struct super_block *sb)
4401 +{
4402 +}
4403 +
4404 +static inline int
4405 +init_ext2_xattr(void)
4406 +{
4407 +       return 0;
4408 +}
4409 +
4410 +static inline void
4411 +exit_ext2_xattr(void)
4412 +{
4413 +}
4414 +
4415 +# endif  /* CONFIG_EXT2_FS_XATTR */
4416 +
4417 +# ifdef CONFIG_EXT2_FS_XATTR_USER
4418 +
4419 +extern int init_ext2_xattr_user(void) __init;
4420 +extern void exit_ext2_xattr_user(void);
4421 +
4422 +# else  /* CONFIG_EXT2_FS_XATTR_USER */
4423 +
4424 +static inline int
4425 +init_ext2_xattr_user(void)
4426 +{
4427 +       return 0;
4428 +}
4429 +
4430 +static inline void
4431 +exit_ext2_xattr_user(void)
4432 +{
4433 +}
4434 +
4435 +# endif  /* CONFIG_EXT2_FS_XATTR_USER */
4436 +
4437 +#endif  /* __KERNEL__ */
4438 +
4439 --- linux/include/linux/ext3_fs.h~linux-2.4.20-xattr-0.8.54-hp  Fri May 16 08:42:47 2003
4440 +++ linux-mmonroe/include/linux/ext3_fs.h       Fri May 16 08:43:01 2003
4441 @@ -63,8 +63,6 @@
4442   */
4443  #define        EXT3_BAD_INO             1      /* Bad blocks inode */
4444  #define EXT3_ROOT_INO           2      /* Root inode */
4445 -#define EXT3_ACL_IDX_INO        3      /* ACL inode */
4446 -#define EXT3_ACL_DATA_INO       4      /* ACL inode */
4447  #define EXT3_BOOT_LOADER_INO    5      /* Boot loader inode */
4448  #define EXT3_UNDEL_DIR_INO      6      /* Undelete directory inode */
4449  #define EXT3_RESIZE_INO                 7      /* Reserved group descriptors inode */
4450 @@ -94,7 +92,6 @@
4451  #else
4452  # define EXT3_BLOCK_SIZE(s)            (EXT3_MIN_BLOCK_SIZE << (s)->s_log_block_size)
4453  #endif
4454 -#define EXT3_ACLE_PER_BLOCK(s)         (EXT3_BLOCK_SIZE(s) / sizeof (struct ext3_acl_entry))
4455  #define        EXT3_ADDR_PER_BLOCK(s)          (EXT3_BLOCK_SIZE(s) / sizeof (__u32))
4456  #ifdef __KERNEL__
4457  # define EXT3_BLOCK_SIZE_BITS(s)       ((s)->s_blocksize_bits)
4458 @@ -129,28 +126,6 @@
4459  #endif
4460  
4461  /*
4462 - * ACL structures
4463 - */
4464 -struct ext3_acl_header /* Header of Access Control Lists */
4465 -{
4466 -       __u32   aclh_size;
4467 -       __u32   aclh_file_count;
4468 -       __u32   aclh_acle_count;
4469 -       __u32   aclh_first_acle;
4470 -};
4471 -
4472 -struct ext3_acl_entry  /* Access Control List Entry */
4473 -{
4474 -       __u32   acle_size;
4475 -       __u16   acle_perms;     /* Access permissions */
4476 -       __u16   acle_type;      /* Type of entry */
4477 -       __u16   acle_tag;       /* User or group identity */
4478 -       __u16   acle_pad1;
4479 -       __u32   acle_next;      /* Pointer on next entry for the */
4480 -                                       /* same inode or on next free entry */
4481 -};
4482 -
4483 -/*
4484   * Structure of a blocks group descriptor
4485   */
4486  struct ext3_group_desc
4487 @@ -344,6 +319,7 @@ struct ext3_inode {
4488    #define EXT3_MOUNT_WRITEBACK_DATA    0x0C00  /* No data ordering */
4489  #define EXT3_MOUNT_UPDATE_JOURNAL      0x1000  /* Update the journal format */
4490  #define EXT3_MOUNT_NO_UID32            0x2000  /* Disable 32-bit UIDs */
4491 +#define EXT3_MOUNT_XATTR_USER          0x4000  /* Extended user attributes */
4492  
4493  /* Compatibility, for having both ext2_fs.h and ext3_fs.h included at once */
4494  #ifndef _LINUX_EXT2_FS_H
4495 @@ -520,7 +496,7 @@ struct ext3_super_block {
4496  #define EXT3_FEATURE_INCOMPAT_RECOVER          0x0004 /* Needs recovery */
4497  #define EXT3_FEATURE_INCOMPAT_JOURNAL_DEV      0x0008 /* Journal device */
4498  
4499 -#define EXT3_FEATURE_COMPAT_SUPP       0
4500 +#define EXT3_FEATURE_COMPAT_SUPP       EXT2_FEATURE_COMPAT_EXT_ATTR
4501  #define EXT3_FEATURE_INCOMPAT_SUPP     (EXT3_FEATURE_INCOMPAT_FILETYPE| \
4502                                          EXT3_FEATURE_INCOMPAT_RECOVER)
4503  #define EXT3_FEATURE_RO_COMPAT_SUPP    (EXT3_FEATURE_RO_COMPAT_SPARSE_SUPER| \
4504 @@ -703,6 +679,7 @@ extern void ext3_check_inodes_bitmap (st
4505  extern unsigned long ext3_count_free (struct buffer_head *, unsigned);
4506  
4507  /* inode.c */
4508 +extern int ext3_forget(handle_t *, int, struct inode *, struct buffer_head *, int);
4509  extern struct buffer_head * ext3_getblk (handle_t *, struct inode *, long, int, int *);
4510  extern struct buffer_head * ext3_bread (handle_t *, struct inode *, int, int, int *);
4511  
4512 @@ -771,8 +748,10 @@ extern struct address_space_operations e
4513  
4514  /* namei.c */
4515  extern struct inode_operations ext3_dir_inode_operations;
4516 +extern struct inode_operations ext3_special_inode_operations;
4517  
4518  /* symlink.c */
4519 +extern struct inode_operations ext3_symlink_inode_operations;
4520  extern struct inode_operations ext3_fast_symlink_inode_operations;
4521  
4522  
4523 --- linux/include/linux/ext3_jbd.h~linux-2.4.20-xattr-0.8.54-hp Fri May 16 08:42:46 2003
4524 +++ linux-mmonroe/include/linux/ext3_jbd.h      Fri May 16 08:43:01 2003
4525 @@ -30,13 +30,19 @@
4526  
4527  #define EXT3_SINGLEDATA_TRANS_BLOCKS   8
4528  
4529 +/* Extended attributes may touch two data buffers, two bitmap buffers,
4530 + * and two group and summaries. */
4531 +
4532 +#define EXT3_XATTR_TRANS_BLOCKS                8
4533 +
4534  /* Define the minimum size for a transaction which modifies data.  This
4535   * needs to take into account the fact that we may end up modifying two
4536   * quota files too (one for the group, one for the user quota).  The
4537   * superblock only gets updated once, of course, so don't bother
4538   * counting that again for the quota updates. */
4539  
4540 -#define EXT3_DATA_TRANS_BLOCKS         (3 * EXT3_SINGLEDATA_TRANS_BLOCKS - 2)
4541 +#define EXT3_DATA_TRANS_BLOCKS         (3 * EXT3_SINGLEDATA_TRANS_BLOCKS + \
4542 +                                        EXT3_XATTR_TRANS_BLOCKS - 2)
4543  
4544  extern int ext3_writepage_trans_blocks(struct inode *inode);
4545  
4546 --- /dev/null   Mon May 20 21:11:23 2002
4547 +++ linux-mmonroe/include/linux/ext3_xattr.h    Fri May 16 08:43:01 2003
4548 @@ -0,0 +1,157 @@
4549 +/*
4550 +  File: linux/ext3_xattr.h
4551 +
4552 +  On-disk format of extended attributes for the ext3 filesystem.
4553 +
4554 +  (C) 2001 Andreas Gruenbacher, <a.gruenbacher@computer.org>
4555 +*/
4556 +
4557 +#include <linux/config.h>
4558 +#include <linux/init.h>
4559 +#include <linux/xattr.h>
4560 +
4561 +/* Magic value in attribute blocks */
4562 +#define EXT3_XATTR_MAGIC               0xEA020000
4563 +
4564 +/* Maximum number of references to one attribute block */
4565 +#define EXT3_XATTR_REFCOUNT_MAX                1024
4566 +
4567 +/* Name indexes */
4568 +#define EXT3_XATTR_INDEX_MAX                   10
4569 +#define EXT3_XATTR_INDEX_USER                  1
4570 +#define EXT3_XATTR_INDEX_POSIX_ACL_ACCESS      2
4571 +#define EXT3_XATTR_INDEX_POSIX_ACL_DEFAULT     3
4572 +
4573 +struct ext3_xattr_header {
4574 +       __u32   h_magic;        /* magic number for identification */
4575 +       __u32   h_refcount;     /* reference count */
4576 +       __u32   h_blocks;       /* number of disk blocks used */
4577 +       __u32   h_hash;         /* hash value of all attributes */
4578 +       __u32   h_reserved[4];  /* zero right now */
4579 +};
4580 +
4581 +struct ext3_xattr_entry {
4582 +       __u8    e_name_len;     /* length of name */
4583 +       __u8    e_name_index;   /* attribute name index */
4584 +       __u16   e_value_offs;   /* offset in disk block of value */
4585 +       __u32   e_value_block;  /* disk block attribute is stored on (n/i) */
4586 +       __u32   e_value_size;   /* size of attribute value */
4587 +       __u32   e_hash;         /* hash value of name and value */
4588 +       char    e_name[0];      /* attribute name */
4589 +};
4590 +
4591 +#define EXT3_XATTR_PAD_BITS            2
4592 +#define EXT3_XATTR_PAD         (1<<EXT3_XATTR_PAD_BITS)
4593 +#define EXT3_XATTR_ROUND               (EXT3_XATTR_PAD-1)
4594 +#define EXT3_XATTR_LEN(name_len) \
4595 +       (((name_len) + EXT3_XATTR_ROUND + \
4596 +       sizeof(struct ext3_xattr_entry)) & ~EXT3_XATTR_ROUND)
4597 +#define EXT3_XATTR_NEXT(entry) \
4598 +       ( (struct ext3_xattr_entry *)( \
4599 +         (char *)(entry) + EXT3_XATTR_LEN((entry)->e_name_len)) )
4600 +#define EXT3_XATTR_SIZE(size) \
4601 +       (((size) + EXT3_XATTR_ROUND) & ~EXT3_XATTR_ROUND)
4602 +
4603 +#ifdef __KERNEL__
4604 +
4605 +# ifdef CONFIG_EXT3_FS_XATTR
4606 +
4607 +struct ext3_xattr_handler {
4608 +       char *prefix;
4609 +       size_t (*list)(char *list, struct inode *inode, const char *name,
4610 +                      int name_len);
4611 +       int (*get)(struct inode *inode, const char *name, void *buffer,
4612 +                  size_t size);
4613 +       int (*set)(struct inode *inode, const char *name, const void *buffer,
4614 +                  size_t size, int flags);
4615 +};
4616 +
4617 +extern int ext3_xattr_register(int, struct ext3_xattr_handler *);
4618 +extern void ext3_xattr_unregister(int, struct ext3_xattr_handler *);
4619 +
4620 +extern int ext3_setxattr(struct dentry *, const char *, const void *, size_t, int);
4621 +extern ssize_t ext3_getxattr(struct dentry *, const char *, void *, size_t);
4622 +extern ssize_t ext3_listxattr(struct dentry *, char *, size_t);
4623 +extern int ext3_removexattr(struct dentry *, const char *);
4624 +
4625 +extern int ext3_xattr_get(struct inode *, int, const char *, void *, size_t);
4626 +extern int ext3_xattr_list(struct inode *, char *, size_t);
4627 +extern int ext3_xattr_set(handle_t *handle, struct inode *, int, const char *, const void *, size_t, int);
4628 +
4629 +extern void ext3_xattr_delete_inode(handle_t *, struct inode *);
4630 +extern void ext3_xattr_put_super(struct super_block *);
4631 +
4632 +extern int init_ext3_xattr(void) __init;
4633 +extern void exit_ext3_xattr(void);
4634 +
4635 +# else  /* CONFIG_EXT3_FS_XATTR */
4636 +#  define ext3_setxattr                NULL
4637 +#  define ext3_getxattr                NULL
4638 +#  define ext3_listxattr       NULL
4639 +#  define ext3_removexattr     NULL
4640 +
4641 +static inline int
4642 +ext3_xattr_get(struct inode *inode, int name_index, const char *name,
4643 +              void *buffer, size_t size)
4644 +{
4645 +       return -ENOTSUP;
4646 +}
4647 +
4648 +static inline int
4649 +ext3_xattr_list(struct inode *inode, void *buffer, size_t size)
4650 +{
4651 +       return -ENOTSUP;
4652 +}
4653 +
4654 +static inline int
4655 +ext3_xattr_set(handle_t *handle, struct inode *inode, int name_index,
4656 +              const char *name, const void *value, size_t size, int flags)
4657 +{
4658 +       return -ENOTSUP;
4659 +}
4660 +
4661 +static inline void
4662 +ext3_xattr_delete_inode(handle_t *handle, struct inode *inode)
4663 +{
4664 +}
4665 +
4666 +static inline void
4667 +ext3_xattr_put_super(struct super_block *sb)
4668 +{
4669 +}
4670 +
4671 +static inline int
4672 +init_ext3_xattr(void)
4673 +{
4674 +       return 0;
4675 +}
4676 +
4677 +static inline void
4678 +exit_ext3_xattr(void)
4679 +{
4680 +}
4681 +
4682 +# endif  /* CONFIG_EXT3_FS_XATTR */
4683 +
4684 +# ifdef CONFIG_EXT3_FS_XATTR_USER
4685 +
4686 +extern int init_ext3_xattr_user(void) __init;
4687 +extern void exit_ext3_xattr_user(void);
4688 +
4689 +# else  /* CONFIG_EXT3_FS_XATTR_USER */
4690 +
4691 +static inline int
4692 +init_ext3_xattr_user(void)
4693 +{
4694 +       return 0;
4695 +}
4696 +
4697 +static inline void
4698 +exit_ext3_xattr_user(void)
4699 +{
4700 +}
4701 +
4702 +#endif  /* CONFIG_EXT3_FS_XATTR_USER */
4703 +
4704 +#endif  /* __KERNEL__ */
4705 +
4706 --- linux/include/linux/fs.h~linux-2.4.20-xattr-0.8.54-hp       Fri May 16 08:42:46 2003
4707 +++ linux-mmonroe/include/linux/fs.h    Fri May 16 08:43:01 2003
4708 @@ -909,7 +909,7 @@ struct inode_operations {
4709         int (*setattr) (struct dentry *, struct iattr *);
4710         int (*setattr_raw) (struct inode *, struct iattr *);
4711         int (*getattr) (struct dentry *, struct iattr *);
4712 -       int (*setxattr) (struct dentry *, const char *, void *, size_t, int);
4713 +       int (*setxattr) (struct dentry *, const char *, const void *, size_t, int);
4714         ssize_t (*getxattr) (struct dentry *, const char *, void *, size_t);
4715         ssize_t (*listxattr) (struct dentry *, char *, size_t);
4716         int (*removexattr) (struct dentry *, const char *);
4717 --- /dev/null   Mon May 20 21:11:23 2002
4718 +++ linux-mmonroe/include/linux/mbcache.h       Fri May 16 08:43:01 2003
4719 @@ -0,0 +1,69 @@
4720 +/*
4721 +  File: linux/mbcache.h
4722 +
4723 +  (C) 2001 by Andreas Gruenbacher, <a.gruenbacher@computer.org>
4724 +*/
4725 +
4726 +/* Hardwire the number of additional indexes */
4727 +#define MB_CACHE_INDEXES_COUNT 1
4728 +
4729 +struct mb_cache_entry;
4730 +
4731 +struct mb_cache_op {
4732 +       int (*free)(struct mb_cache_entry *, int);
4733 +};
4734 +
4735 +struct mb_cache {
4736 +       struct list_head                c_cache_list;
4737 +       const char                      *c_name;
4738 +       struct mb_cache_op              c_op;
4739 +       atomic_t                        c_entry_count;
4740 +       int                             c_bucket_count;
4741 +#ifndef MB_CACHE_INDEXES_COUNT
4742 +       int                             c_indexes_count;
4743 +#endif
4744 +       kmem_cache_t                    *c_entry_cache;
4745 +       struct list_head                *c_block_hash;
4746 +       struct list_head                *c_indexes_hash[0];
4747 +};
4748 +
4749 +struct mb_cache_entry_index {
4750 +       struct list_head                o_list;
4751 +       unsigned int                    o_key;
4752 +};
4753 +
4754 +struct mb_cache_entry {
4755 +       struct list_head                e_lru_list;
4756 +       struct mb_cache                 *e_cache;
4757 +       atomic_t                        e_used;
4758 +       kdev_t                          e_dev;
4759 +       unsigned long                   e_block;
4760 +       struct list_head                e_block_list;
4761 +       struct mb_cache_entry_index     e_indexes[0];
4762 +};
4763 +
4764 +/* Functions on caches */
4765 +
4766 +struct mb_cache * mb_cache_create(const char *, struct mb_cache_op *, size_t,
4767 +                                 int, int);
4768 +void mb_cache_shrink(struct mb_cache *, kdev_t);
4769 +void mb_cache_destroy(struct mb_cache *);
4770 +
4771 +/* Functions on cache entries */
4772 +
4773 +struct mb_cache_entry *mb_cache_entry_alloc(struct mb_cache *);
4774 +int mb_cache_entry_insert(struct mb_cache_entry *, kdev_t, unsigned long,
4775 +                         unsigned int[]);
4776 +void mb_cache_entry_rehash(struct mb_cache_entry *, unsigned int[]);
4777 +void mb_cache_entry_release(struct mb_cache_entry *);
4778 +void mb_cache_entry_takeout(struct mb_cache_entry *);
4779 +void mb_cache_entry_free(struct mb_cache_entry *);
4780 +struct mb_cache_entry *mb_cache_entry_dup(struct mb_cache_entry *);
4781 +struct mb_cache_entry *mb_cache_entry_get(struct mb_cache *, kdev_t,
4782 +                                         unsigned long);
4783 +#if !defined(MB_CACHE_INDEXES_COUNT) || (MB_CACHE_INDEXES_COUNT > 0)
4784 +struct mb_cache_entry *mb_cache_entry_find_first(struct mb_cache *cache, int,
4785 +                                                kdev_t, unsigned int);
4786 +struct mb_cache_entry *mb_cache_entry_find_next(struct mb_cache_entry *, int,
4787 +                                               kdev_t, unsigned int);
4788 +#endif
4789 --- linux/kernel/ksyms.c~linux-2.4.20-xattr-0.8.54-hp   Fri May 16 08:42:45 2003
4790 +++ linux-mmonroe/kernel/ksyms.c        Fri May 16 08:43:52 2003
4791 @@ -11,6 +11,7 @@
4792  
4793  #include <linux/config.h>
4794  #include <linux/slab.h>
4795 +#include <linux/cache_def.h>
4796  #include <linux/module.h>
4797  #include <linux/blkdev.h>
4798  #include <linux/cdrom.h>
4799 @@ -106,6 +107,7 @@ EXPORT_SYMBOL(exit_mm);
4800  EXPORT_SYMBOL(exit_files);
4801  EXPORT_SYMBOL(exit_fs);
4802  EXPORT_SYMBOL(exit_sighand);
4803 +EXPORT_SYMBOL(copy_fs_struct);
4804  EXPORT_SYMBOL_GPL(make_pages_present);
4805  
4806  /* internal kernel memory management */
4807 @@ -126,6 +128,8 @@ EXPORT_SYMBOL(kmem_cache_validate);
4808  EXPORT_SYMBOL(kmem_cache_alloc);
4809  EXPORT_SYMBOL(kmem_cache_free);
4810  EXPORT_SYMBOL(kmem_cache_size);
4811 +EXPORT_SYMBOL(register_cache);
4812 +EXPORT_SYMBOL(unregister_cache);
4813  EXPORT_SYMBOL(kmalloc);
4814  EXPORT_SYMBOL(kfree);
4815  EXPORT_SYMBOL(vfree);
4816 --- linux/mm/vmscan.c~linux-2.4.20-xattr-0.8.54-hp      Fri May 16 08:39:23 2003
4817 +++ linux-mmonroe/mm/vmscan.c   Fri May 16 08:43:01 2003
4818 @@ -18,6 +18,7 @@
4819  #include <linux/kernel_stat.h>
4820  #include <linux/swap.h>
4821  #include <linux/swapctl.h>
4822 +#include <linux/cache_def.h>
4823  #include <linux/smp_lock.h>
4824  #include <linux/pagemap.h>
4825  #include <linux/init.h>
4826 @@ -35,6 +36,39 @@
4827   */
4828  #define DEF_PRIORITY (6)
4829  
4830 +static DECLARE_MUTEX(other_caches_sem);
4831 +static LIST_HEAD(cache_definitions);
4832 +
4833 +void register_cache(struct cache_definition *cache)
4834 +{
4835 +       down(&other_caches_sem);
4836 +       list_add(&cache->link, &cache_definitions);
4837 +       up(&other_caches_sem);
4838 +}
4839 +
4840 +void unregister_cache(struct cache_definition *cache)
4841 +{
4842 +       down(&other_caches_sem);
4843 +       list_del(&cache->link);
4844 +       up(&other_caches_sem);
4845 +}
4846 +
4847 +static void shrink_other_caches(unsigned int priority, int gfp_mask)
4848 +{
4849 +       struct list_head *p;
4850 +
4851 +       if (down_trylock(&other_caches_sem))
4852 +               return;
4853 +
4854 +       list_for_each_prev(p, &cache_definitions) {
4855 +               struct cache_definition *cache =
4856 +                       list_entry(p, struct cache_definition, link);
4857 +
4858 +               cache->shrink(priority, gfp_mask);
4859 +       }
4860 +       up(&other_caches_sem);
4861 +}
4862 +
4863  /*
4864   * The swap-out function returns 1 if it successfully
4865   * scanned all the pages it was asked to (`count').
4866 @@ -579,6 +613,7 @@ static int shrink_caches(zone_t * classz
4867  
4868         shrink_dcache_memory(priority, gfp_mask);
4869         shrink_icache_memory(priority, gfp_mask);
4870 +       shrink_other_caches(priority, gfp_mask);
4871  #ifdef CONFIG_QUOTA
4872         shrink_dqcache_memory(DEF_PRIORITY, gfp_mask);
4873  #endif
4874
4875 _