1 --------------------------------------------------------------------
2 This patch adds extended attribute support for JFS 1.1.1.
4 This patch was built against linux-2.4.20 + JFS 1.1.1
5 --------------------------------------------------------------------
6 diff -Nur linux-2.4.20/fs/jfs/Makefile linux-2.4.20-xattr/fs/jfs/Makefile
7 --- linux-2.4.20/fs/jfs/Makefile 2003-02-03 14:36:39.000000000 -0600
8 +++ linux-2.4.20-xattr/fs/jfs/Makefile 2003-02-03 14:37:52.000000000 -0600
10 jfs_xtree.o jfs_imap.o jfs_debug.o jfs_dmap.o \
11 jfs_unicode.o jfs_dtree.o jfs_inode.o \
12 jfs_extent.o symlink.o jfs_metapage.o \
13 - jfs_logmgr.o jfs_txnmgr.o jfs_uniupr.o resize.o
14 + jfs_logmgr.o jfs_txnmgr.o jfs_uniupr.o resize.o xattr.o
17 EXTRA_CFLAGS += -D_JFS_4K
18 diff -Nur linux-2.4.20/fs/jfs/file.c linux-2.4.20-xattr/fs/jfs/file.c
19 --- linux-2.4.20/fs/jfs/file.c 2003-02-03 14:36:39.000000000 -0600
20 +++ linux-2.4.20-xattr/fs/jfs/file.c 2003-02-03 14:37:52.000000000 -0600
22 #include "jfs_incore.h"
24 #include "jfs_txnmgr.h"
25 +#include "jfs_xattr.h"
26 #include "jfs_debug.h"
31 struct inode_operations jfs_file_inode_operations = {
32 .truncate = jfs_truncate,
33 + .setxattr = jfs_setxattr,
34 + .getxattr = jfs_getxattr,
35 + .listxattr = jfs_listxattr,
36 + .removexattr = jfs_removexattr,
39 struct file_operations jfs_file_operations = {
42 .release = jfs_release,
45 +struct inode_operations jfs_special_inode_operations = {
46 + .setxattr = jfs_setxattr,
47 + .getxattr = jfs_getxattr,
48 + .listxattr = jfs_listxattr,
49 + .removexattr = jfs_removexattr,
52 diff -Nur linux-2.4.20/fs/jfs/inode.c linux-2.4.20-xattr/fs/jfs/inode.c
53 --- linux-2.4.20/fs/jfs/inode.c 2003-02-03 14:36:39.000000000 -0600
54 +++ linux-2.4.20-xattr/fs/jfs/inode.c 2003-02-03 14:37:52.000000000 -0600
56 extern struct inode_operations jfs_dir_inode_operations;
57 extern struct inode_operations jfs_file_inode_operations;
58 extern struct inode_operations jfs_symlink_inode_operations;
59 +extern struct inode_operations jfs_special_inode_operations;
60 extern struct file_operations jfs_dir_operations;
61 extern struct file_operations jfs_file_operations;
62 struct address_space_operations jfs_aops;
65 inode->i_op = &jfs_symlink_inode_operations;
67 + inode->i_op = &jfs_special_inode_operations;
68 init_special_inode(inode, inode->i_mode,
69 kdev_t_to_nr(inode->i_rdev));
71 diff -Nur linux-2.4.20/fs/jfs/jfs_imap.c linux-2.4.20-xattr/fs/jfs/jfs_imap.c
72 --- linux-2.4.20/fs/jfs/jfs_imap.c 2003-02-03 14:36:39.000000000 -0600
73 +++ linux-2.4.20-xattr/fs/jfs/jfs_imap.c 2003-02-03 14:37:52.000000000 -0600
75 memcpy(&dp->di_fastsymlink, jfs_ip->i_inline, IDATASIZE);
78 -#ifdef _STILL_TO_PORT
80 * copy inline data from in-memory inode to on-disk inode:
81 * 128 byte slot granularity
83 - if (test_cflag(COMMIT_Inlineea, ip))
84 + if (test_cflag(COMMIT_Inlineea, ip)) {
85 lv = & dilinelock->lv[dilinelock->index];
86 lv->offset = (dioffset + 3 * 128) >> L2INODESLOTSIZE;
88 - memcpy(&dp->di_inlineea, &ip->i_inlineea, INODESLOTSIZE);
89 + memcpy(&dp->di_inlineea, &jfs_ip->i_inline_ea, INODESLOTSIZE);
92 clear_cflag(COMMIT_Inlineea, ip);
94 -#endif /* _STILL_TO_PORT */
97 * lock/copy inode base: 128 byte slot granularity
98 @@ -3058,16 +3056,17 @@
99 jfs_ip->next_index = le32_to_cpu(dip->di_next_index);
100 jfs_ip->otime = le32_to_cpu(dip->di_otime.tv_sec);
101 jfs_ip->acltype = le32_to_cpu(dip->di_acltype);
103 - * We may only need to do this for "special" inodes (dmap, imap)
106 if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode))
107 ip->i_rdev = to_kdev_t(le32_to_cpu(dip->di_rdev));
108 - else if (S_ISDIR(ip->i_mode)) {
110 + if (S_ISDIR(ip->i_mode)) {
111 memcpy(&jfs_ip->i_dirtable, &dip->di_dirtable, 384);
112 - } else if (!S_ISFIFO(ip->i_mode)) {
113 + } else if (S_ISREG(ip->i_mode) || S_ISLNK(ip->i_mode)) {
114 memcpy(&jfs_ip->i_xtroot, &dip->di_xtroot, 288);
117 + memcpy(&jfs_ip->i_inline_ea, &dip->di_inlineea, 128);
119 /* Zero the in-memory-only stuff */
122 diff -Nur linux-2.4.20/fs/jfs/jfs_incore.h linux-2.4.20-xattr/fs/jfs/jfs_incore.h
123 --- linux-2.4.20/fs/jfs/jfs_incore.h 2003-02-03 14:36:39.000000000 -0600
124 +++ linux-2.4.20-xattr/fs/jfs/jfs_incore.h 2003-02-03 14:37:52.000000000 -0600
126 unchar _unused[16]; /* 16: */
127 dxd_t _dxd; /* 16: */
128 unchar _inline[128]; /* 128: inline symlink */
129 + /* _inline_ea may overlay the last part of
130 + * file._xtroot if maxentry = XTROOTINITSLOT
132 + unchar _inline_ea[128]; /* 128: inline extended attr */
137 #define i_dirtable u.dir._table
138 #define i_dtroot u.dir._dtroot
139 #define i_inline u.link._inline
140 +#define i_inline_ea u.link._inline_ea
143 #define IREAD_LOCK(ip) down_read(&JFS_IP(ip)->rdwrlock)
144 diff -Nur linux-2.4.20/fs/jfs/jfs_metapage.h linux-2.4.20-xattr/fs/jfs/jfs_metapage.h
145 --- linux-2.4.20/fs/jfs/jfs_metapage.h 2003-02-03 14:36:39.000000000 -0600
146 +++ linux-2.4.20-xattr/fs/jfs/jfs_metapage.h 2003-02-03 14:37:52.000000000 -0600
151 - * This routines invalidate all pages for an extent.
152 + * These routines invalidate all pages for an extent.
154 extern void __invalidate_metapages(struct inode *, s64, int);
155 #define invalidate_pxd_metapages(ip, pxd) \
156 diff -Nur linux-2.4.20/fs/jfs/jfs_types.h linux-2.4.20-xattr/fs/jfs/jfs_types.h
157 --- linux-2.4.20/fs/jfs/jfs_types.h 2003-02-03 14:36:39.000000000 -0600
158 +++ linux-2.4.20-xattr/fs/jfs/jfs_types.h 2003-02-03 14:37:52.000000000 -0600
160 #define DXDaddress PXDaddress
161 #define lengthDXD lengthPXD
162 #define addressDXD addressPXD
163 +#define DXDsize(dxd, size32) ((dxd)->size = cpu_to_le32(size32))
164 +#define sizeDXD(dxd) le32_to_cpu((dxd)->size)
167 * directory entry argument
168 diff -Nur linux-2.4.20/fs/jfs/jfs_xattr.h linux-2.4.20-xattr/fs/jfs/jfs_xattr.h
169 --- linux-2.4.20/fs/jfs/jfs_xattr.h 1969-12-31 18:00:00.000000000 -0600
170 +++ linux-2.4.20-xattr/fs/jfs/jfs_xattr.h 2003-02-03 15:28:33.000000000 -0600
173 + * Copyright (c) International Business Machines Corp., 2000-2002
175 + * This program is free software; you can redistribute it and/or modify
176 + * it under the terms of the GNU General Public License as published by
177 + * the Free Software Foundation; either version 2 of the License, or
178 + * (at your option) any later version.
180 + * This program is distributed in the hope that it will be useful,
181 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
182 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
183 + * the GNU General Public License for more details.
185 + * You should have received a copy of the GNU General Public License
186 + * along with this program; if not, write to the Free Software
187 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
194 + * jfs_ea_list describe the on-disk format of the extended attributes.
195 + * I know the null-terminator is redundant since namelen is stored, but
196 + * I am maintaining compatibility with OS/2 where possible.
199 + u8 flag; /* Unused? */
200 + u8 namelen; /* Length of name */
201 + u16 valuelen; /* Length of value */
202 + char name[0]; /* Attribute name (includes null-terminator) */
203 +}; /* Value immediately follows name */
205 +struct jfs_ea_list {
206 + u32 size; /* overall size */
207 + struct jfs_ea ea[0]; /* Variable length list */
210 +/* Macros for defining maxiumum number of bytes supported for EAs */
211 +#define MAXEASIZE 65535
212 +#define MAXEALISTSIZE MAXEASIZE
215 + * some macros for dealing with variable length EA lists.
217 +#define EA_SIZE(ea) \
218 + (sizeof (struct jfs_ea) + (ea)->namelen + 1 + \
219 + le16_to_cpu((ea)->valuelen))
220 +#define NEXT_EA(ea) ((struct jfs_ea *) (((char *) (ea)) + (EA_SIZE (ea))))
221 +#define FIRST_EA(ealist) ((ealist)->ea)
222 +#define EALIST_SIZE(ealist) le32_to_cpu((ealist)->size)
223 +#define END_EALIST(ealist) \
224 + ((struct jfs_ea *) (((char *) (ealist)) + EALIST_SIZE(ealist)))
226 +extern int __jfs_setxattr(struct inode *, const char *, const void *, size_t,
228 +extern int jfs_setxattr(struct dentry *, const char *, const void *, size_t,
230 +extern ssize_t __jfs_getxattr(struct inode *, const char *, void *, size_t);
231 +extern ssize_t jfs_getxattr(struct dentry *, const char *, void *, size_t, int);
232 +extern ssize_t jfs_listxattr(struct dentry *, char *, size_t, int);
233 +extern int jfs_removexattr(struct dentry *, const char *, int);
235 +#endif /* H_JFS_XATTR */
236 diff -Nur linux-2.4.20/fs/jfs/namei.c linux-2.4.20-xattr/fs/jfs/namei.c
237 --- linux-2.4.20/fs/jfs/namei.c 2003-02-03 14:36:39.000000000 -0600
238 +++ linux-2.4.20-xattr/fs/jfs/namei.c 2003-02-03 14:37:52.000000000 -0600
240 #include "jfs_dmap.h"
241 #include "jfs_unicode.h"
242 #include "jfs_metapage.h"
243 +#include "jfs_xattr.h"
244 #include "jfs_debug.h"
246 extern struct inode_operations jfs_file_inode_operations;
247 extern struct inode_operations jfs_symlink_inode_operations;
248 +extern struct inode_operations jfs_special_inode_operations;
249 extern struct file_operations jfs_file_operations;
250 extern struct address_space_operations jfs_aops;
253 i_fastsymlink = JFS_IP(ip)->i_inline;
254 memcpy(i_fastsymlink, name, ssize);
255 ip->i_size = ssize - 1;
258 + * if symlink is > 128 bytes, we don't have the space to
259 + * store inline extended attributes
261 + if (ssize > sizeof (JFS_IP(ip)->i_inline))
262 + JFS_IP(ip)->mode2 &= ~INLINEEA;
265 ("jfs_symlink: fast symlink added ssize:%d name:%s \n",
267 @@ -1322,6 +1332,7 @@
268 if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack)))
271 + ip->i_op = &jfs_special_inode_operations;
272 init_special_inode(ip, ip->i_mode, rdev);
274 insert_inode_hash(ip);
275 @@ -1409,6 +1420,10 @@
278 .rename = jfs_rename,
279 + .setxattr = jfs_setxattr,
280 + .getxattr = jfs_getxattr,
281 + .listxattr = jfs_listxattr,
282 + .removexattr = jfs_removexattr,
285 struct file_operations jfs_dir_operations = {
286 diff -Nur linux-2.4.20/fs/jfs/symlink.c linux-2.4.20-xattr/fs/jfs/symlink.c
287 --- linux-2.4.20/fs/jfs/symlink.c 2003-02-03 14:36:39.000000000 -0600
288 +++ linux-2.4.20-xattr/fs/jfs/symlink.c 2003-02-03 14:37:52.000000000 -0600
291 #include <linux/fs.h>
292 #include "jfs_incore.h"
293 +#include "jfs_xattr.h"
295 static int jfs_follow_link(struct dentry *dentry, struct nameidata *nd)
298 struct inode_operations jfs_symlink_inode_operations = {
299 .readlink = jfs_readlink,
300 .follow_link = jfs_follow_link,
301 + .setxattr = jfs_setxattr,
302 + .getxattr = jfs_getxattr,
303 + .listxattr = jfs_listxattr,
304 + .removexattr = jfs_removexattr,
307 diff -Nur linux-2.4.20/fs/jfs/xattr.c linux-2.4.20-xattr/fs/jfs/xattr.c
308 --- linux-2.4.20/fs/jfs/xattr.c 1969-12-31 18:00:00.000000000 -0600
309 +++ linux-2.4.20-xattr/fs/jfs/xattr.c 2003-02-03 15:29:05.000000000 -0600
312 + * Copyright (c) International Business Machines Corp., 2000-2002
313 + * Copyright (c) Christoph Hellwig, 2002
315 + * This program is free software; you can redistribute it and/or modify
316 + * it under the terms of the GNU General Public License as published by
317 + * the Free Software Foundation; either version 2 of the License, or
318 + * (at your option) any later version.
320 + * This program is distributed in the hope that it will be useful,
321 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
322 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
323 + * the GNU General Public License for more details.
325 + * You should have received a copy of the GNU General Public License
326 + * along with this program; if not, write to the Free Software
327 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
330 +#include <linux/fs.h>
331 +#include <linux/xattr.h>
332 +#include "jfs_incore.h"
333 +#include "jfs_dmap.h"
334 +#include "jfs_debug.h"
335 +#include "jfs_dinode.h"
336 +#include "jfs_extent.h"
337 +#include "jfs_metapage.h"
338 +#include "jfs_xattr.h"
341 + * jfs_xattr.c: extended attribute service
343 + * Overall design --
347 + * Extended attribute lists (jfs_ea_list) consist of an overall size (32 bit
348 + * value) and a variable (0 or more) number of extended attribute
349 + * entries. Each extended attribute entry (jfs_ea) is a <name,value> double
350 + * where <name> is constructed from a null-terminated ascii string
351 + * (1 ... 255 bytes in the name) and <value> is arbitrary 8 bit data
352 + * (1 ... 65535 bytes). The in-memory format is
354 + * 0 1 2 4 4 + namelen + 1
355 + * +-------+--------+--------+----------------+-------------------+
356 + * | Flags | Name | Value | Name String \0 | Data . . . . |
357 + * | | Length | Length | | |
358 + * +-------+--------+--------+----------------+-------------------+
360 + * A jfs_ea_list then is structured as
362 + * 0 4 4 + EA_SIZE(ea1)
363 + * +------------+-------------------+--------------------+-----
364 + * | Overall EA | First FEA Element | Second FEA Element | .....
365 + * | List Size | | |
366 + * +------------+-------------------+--------------------+-----
370 + * FEALISTs are stored on disk using blocks allocated by dbAlloc() and
371 + * written directly. An EA list may be in-lined in the inode if there is
372 + * sufficient room available.
376 + int flag; /* Indicates what storage xattr points to */
377 + int max_size; /* largest xattr that fits in current buffer */
378 + dxd_t new_ea; /* dxd to replace ea when modifying xattr */
379 + struct metapage *mp; /* metapage containing ea list */
380 + struct jfs_ea_list *xattr; /* buffer containing ea list */
384 + * ea_buffer.flag values
386 +#define EA_INLINE 0x0001
387 +#define EA_EXTENT 0x0002
388 +#define EA_NEW 0x0004
389 +#define EA_MALLOC 0x0008
392 +#define XATTR_SYSTEM_PREFIX "system."
393 +#define XATTR_SYSTEM_PREFIX_LEN (sizeof (XATTR_SYSTEM_PREFIX) - 1)
395 +#define XATTR_USER_PREFIX "user."
396 +#define XATTR_USER_PREFIX_LEN (sizeof (XATTR_USER_PREFIX) - 1)
398 +#define XATTR_OS2_PREFIX "os2."
399 +#define XATTR_OS2_PREFIX_LEN (sizeof (XATTR_OS2_PREFIX) - 1)
402 + * These three routines are used to recognize on-disk extended attributes
403 + * that are in a recognized namespace. If the attribute is not recognized,
404 + * "os2." is prepended to the name
406 +static inline int is_os2_xattr(struct jfs_ea *ea)
409 + * Check for "system."
411 + if ((ea->namelen >= XATTR_SYSTEM_PREFIX_LEN) &&
412 + !strncmp(ea->name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
415 + * Check for "user."
417 + if ((ea->namelen >= XATTR_USER_PREFIX_LEN) &&
418 + !strncmp(ea->name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
421 + * Add any other valid namespace prefixes here
425 + * We assume it's OS/2's flat namespace
430 +static inline int name_size(struct jfs_ea *ea)
432 + if (is_os2_xattr(ea))
433 + return ea->namelen + XATTR_OS2_PREFIX_LEN;
435 + return ea->namelen;
438 +static inline int copy_name(char *buffer, struct jfs_ea *ea)
440 + int len = ea->namelen;
442 + if (is_os2_xattr(ea)) {
443 + memcpy(buffer, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN);
444 + buffer += XATTR_OS2_PREFIX_LEN;
445 + len += XATTR_OS2_PREFIX_LEN;
447 + memcpy(buffer, ea->name, ea->namelen);
448 + buffer[ea->namelen] = 0;
453 +/* Forward references */
454 +static void ea_release(struct inode *inode, struct ea_buffer *ea_buf);
457 + * NAME: ea_write_inline
459 + * FUNCTION: Attempt to write an EA inline if area is available
462 + * Already verified that the specified EA is small enough to fit inline
465 + * ip - Inode pointer
466 + * ealist - EA list pointer
467 + * size - size of ealist in bytes
468 + * ea - dxd_t structure to be filled in with necessary EA information
469 + * if we successfully copy the EA inline
472 + * Checks if the inode's inline area is available. If so, copies EA inline
473 + * and sets <ea> fields appropriately. Otherwise, returns failure, EA will
474 + * have to be put into an extent.
476 + * RETURNS: 0 for successful copy to inline area; -1 if area not available
478 +static int ea_write_inline(struct inode *ip, struct jfs_ea_list *ealist,
479 + int size, dxd_t * ea)
481 + struct jfs_inode_info *ji = JFS_IP(ip);
484 + * Make sure we have an EA -- the NULL EA list is valid, but you
487 + if (ealist && size > sizeof (struct jfs_ea_list)) {
488 + assert(size <= sizeof (ji->i_inline_ea));
491 + * See if the space is available or if it is already being
492 + * used for an inline EA.
494 + if (!(ji->mode2 & INLINEEA) && !(ji->ea.flag & DXD_INLINE))
500 + memcpy(ji->i_inline_ea, ealist, size);
501 + ea->flag = DXD_INLINE;
502 + ji->mode2 &= ~INLINEEA;
509 + /* Free up INLINE area */
510 + if (ji->ea.flag & DXD_INLINE)
511 + ji->mode2 |= INLINEEA;
514 + mark_inode_dirty(ip);
521 + * FUNCTION: Write an EA for an inode
523 + * PRE CONDITIONS: EA has been verified
526 + * ip - Inode pointer
527 + * ealist - EA list pointer
528 + * size - size of ealist in bytes
529 + * ea - dxd_t structure to be filled in appropriately with where the
532 + * NOTES: Will write EA inline if able to, otherwise allocates blocks for an
533 + * extent and synchronously writes it to those blocks.
535 + * RETURNS: 0 for success; Anything else indicates failure
537 +static int ea_write(struct inode *ip, struct jfs_ea_list *ealist, int size,
540 + struct super_block *sb = ip->i_sb;
541 + struct jfs_inode_info *ji = JFS_IP(ip);
542 + struct jfs_sb_info *sbi = JFS_SBI(sb);
548 + s32 bytes_to_write;
549 + struct metapage *mp;
552 + * Quick check to see if this is an in-linable EA. Short EAs
553 + * and empty EAs are all in-linable, provided the space exists.
555 + if (!ealist || size <= sizeof (ji->i_inline_ea)) {
556 + if (!ea_write_inline(ip, ealist, size, ea))
560 + /* figure out how many blocks we need */
561 + nblocks = (size + (sb->s_blocksize - 1)) >> sb->s_blocksize_bits;
563 + rc = dbAlloc(ip, INOHINT(ip), nblocks, &blkno);
568 + * Now have nblocks worth of storage to stuff into the FEALIST.
569 + * loop over the FEALIST copying data into the buffer one page at
572 + cp = (char *) ealist;
574 + for (i = 0; i < nblocks; i += sbi->nbperpage) {
576 + * Determine how many bytes for this request, and round up to
577 + * the nearest aggregate block size
579 + nb = min(PSIZE, nbytes);
581 + ((((nb + sb->s_blocksize - 1)) >> sb->s_blocksize_bits))
582 + << sb->s_blocksize_bits;
584 + if (!(mp = get_metapage(ip, blkno + i, bytes_to_write, 1))) {
589 + memcpy(mp->data, cp, nb);
592 + * We really need a way to propagate errors for
593 + * forced writes like this one. --hch
595 + * (__write_metapage => release_metapage => flush_metapage)
598 + if ((rc = flush_metapage(mp))) {
600 + * the write failed -- this means that the buffer
601 + * is still assigned and the blocks are not being
602 + * used. this seems like the best error recovery
608 + flush_metapage(mp);
615 + ea->flag = DXD_EXTENT;
616 + DXDsize(ea, le32_to_cpu(ealist->size));
617 + DXDlength(ea, nblocks);
618 + DXDaddress(ea, blkno);
620 + /* Free up INLINE area */
621 + if (ji->ea.flag & DXD_INLINE)
622 + ji->mode2 |= INLINEEA;
627 + dbFree(ip, blkno, nblocks);
632 + * NAME: ea_read_inline
634 + * FUNCTION: Read an inlined EA into user's buffer
637 + * ip - Inode pointer
638 + * ealist - Pointer to buffer to fill in with EA
642 +static int ea_read_inline(struct inode *ip, struct jfs_ea_list *ealist)
644 + struct jfs_inode_info *ji = JFS_IP(ip);
645 + int ea_size = sizeDXD(&ji->ea);
647 + if (ea_size == 0) {
653 + if ((sizeDXD(&ji->ea) > sizeof (ji->i_inline_ea)))
655 + if (le32_to_cpu(((struct jfs_ea_list *) &ji->i_inline_ea)->size)
659 + memcpy(ealist, ji->i_inline_ea, ea_size);
666 + * FUNCTION: copy EA data into user's buffer
669 + * ip - Inode pointer
670 + * ealist - Pointer to buffer to fill in with EA
672 + * NOTES: If EA is inline calls ea_read_inline() to copy EA.
674 + * RETURNS: 0 for success; other indicates failure
676 +static int ea_read(struct inode *ip, struct jfs_ea_list *ealist)
678 + struct super_block *sb = ip->i_sb;
679 + struct jfs_inode_info *ji = JFS_IP(ip);
680 + struct jfs_sb_info *sbi = JFS_SBI(sb);
683 + char *cp = (char *) ealist;
687 + struct metapage *mp;
689 + /* quick check for in-line EA */
690 + if (ji->ea.flag & DXD_INLINE)
691 + return ea_read_inline(ip, ealist);
693 + nbytes = sizeDXD(&ji->ea);
697 + * Figure out how many blocks were allocated when this EA list was
698 + * originally written to disk.
700 + nblocks = lengthDXD(&ji->ea) << sbi->l2nbperpage;
701 + blkno = addressDXD(&ji->ea) << sbi->l2nbperpage;
704 + * I have found the disk blocks which were originally used to store
705 + * the FEALIST. now i loop over each contiguous block copying the
706 + * data into the buffer.
708 + for (i = 0; i < nblocks; i += sbi->nbperpage) {
710 + * Determine how many bytes for this request, and round up to
711 + * the nearest aggregate block size
713 + nb = min(PSIZE, nbytes);
715 + ((((nb + sb->s_blocksize - 1)) >> sb->s_blocksize_bits))
716 + << sb->s_blocksize_bits;
718 + if (!(mp = read_metapage(ip, blkno + i, bytes_to_read, 1)))
721 + memcpy(cp, mp->data, nb);
722 + release_metapage(mp);
734 + * FUNCTION: Returns buffer containing existing extended attributes.
735 + * The size of the buffer will be the larger of the existing
736 + * attributes size, or min_size.
738 + * The buffer, which may be inlined in the inode or in the
739 + * page cache must be release by calling ea_release or ea_put
742 + * inode - Inode pointer
743 + * ea_buf - Structure to be populated with ealist and its metadata
744 + * min_size- minimum size of buffer to be returned
746 + * RETURNS: 0 for success; Other indicates failure
748 +static int ea_get(struct inode *inode, struct ea_buffer *ea_buf, int min_size)
750 + struct jfs_inode_info *ji = JFS_IP(inode);
751 + struct super_block *sb = inode->i_sb;
753 + int ea_size = sizeDXD(&ji->ea);
754 + int blocks_needed, current_blocks;
758 + /* When fsck.jfs clears a bad ea, it doesn't clear the size */
759 + if (ji->ea.flag == 0)
762 + if (ea_size == 0) {
763 + if (min_size == 0) {
765 + ea_buf->max_size = 0;
766 + ea_buf->xattr = NULL;
769 + if ((min_size <= sizeof (ji->i_inline_ea)) &&
770 + (ji->mode2 & INLINEEA)) {
771 + ea_buf->flag = EA_INLINE | EA_NEW;
772 + ea_buf->max_size = sizeof (ji->i_inline_ea);
773 + ea_buf->xattr = (struct jfs_ea_list *) ji->i_inline_ea;
774 + DXDlength(&ea_buf->new_ea, 0);
775 + DXDaddress(&ea_buf->new_ea, 0);
776 + ea_buf->new_ea.flag = DXD_INLINE;
777 + DXDsize(&ea_buf->new_ea, min_size);
780 + current_blocks = 0;
781 + } else if (ji->ea.flag & DXD_INLINE) {
782 + if (min_size <= sizeof (ji->i_inline_ea)) {
783 + ea_buf->flag = EA_INLINE;
784 + ea_buf->max_size = sizeof (ji->i_inline_ea);
785 + ea_buf->xattr = (struct jfs_ea_list *) ji->i_inline_ea;
788 + current_blocks = 0;
790 + assert(ji->ea.flag & DXD_EXTENT);
791 + current_blocks = (ea_size + sb->s_blocksize - 1) >>
792 + sb->s_blocksize_bits;
794 + size = max(min_size, ea_size);
796 + if (size > PSIZE) {
798 + * To keep the rest of the code simple. Allocate a
799 + * contiguous buffer to work with
801 + ea_buf->xattr = kmalloc(size, GFP_KERNEL);
802 + if (ea_buf->xattr == NULL)
805 + ea_buf->flag |= EA_MALLOC;
806 + ea_buf->max_size = (size + sb->s_blocksize - 1) &
807 + ~(sb->s_blocksize - 1);
812 + if ((rc = ea_read(inode, ea_buf->xattr))) {
813 + kfree(ea_buf->xattr);
814 + ea_buf->xattr = NULL;
819 + blocks_needed = (min_size + sb->s_blocksize - 1) >>
820 + sb->s_blocksize_bits;
822 + if (blocks_needed > current_blocks) {
823 + rc = dbAlloc(inode, INOHINT(inode), (s64) blocks_needed,
828 + DXDlength(&ea_buf->new_ea, blocks_needed);
829 + DXDaddress(&ea_buf->new_ea, blkno);
830 + ea_buf->new_ea.flag = DXD_EXTENT;
831 + DXDsize(&ea_buf->new_ea, min_size);
833 + ea_buf->flag = EA_EXTENT | EA_NEW;
835 + ea_buf->mp = get_metapage(inode, blkno,
836 + blocks_needed << sb->s_blocksize_bits,
838 + if (ea_buf->mp == NULL) {
839 + dbFree(inode, blkno, (s64) blocks_needed);
842 + ea_buf->xattr = ea_buf->mp->data;
843 + ea_buf->max_size = (min_size + sb->s_blocksize - 1) &
844 + ~(sb->s_blocksize - 1);
847 + if ((rc = ea_read(inode, ea_buf->xattr))) {
848 + discard_metapage(ea_buf->mp);
849 + dbFree(inode, blkno, (s64) blocks_needed);
854 + ea_buf->flag = EA_EXTENT;
855 + ea_buf->mp = read_metapage(inode, addressDXD(&ji->ea),
856 + lengthDXD(&ji->ea), 1);
857 + if (ea_buf->mp == NULL)
859 + ea_buf->xattr = ea_buf->mp->data;
860 + ea_buf->max_size = (ea_size + sb->s_blocksize - 1) &
861 + ~(sb->s_blocksize - 1);
864 + if (EALIST_SIZE(ea_buf->xattr) != ea_size) {
865 + printk(KERN_ERR "ea_get: invalid extended attribute\n");
866 + dump_mem("xattr", ea_buf->xattr, ea_size);
867 + ea_release(inode, ea_buf);
874 +static void ea_release(struct inode *inode, struct ea_buffer *ea_buf)
876 + if (ea_buf->flag & EA_MALLOC)
877 + kfree(ea_buf->xattr);
878 + else if (ea_buf->flag & EA_EXTENT) {
879 + assert(ea_buf->mp);
880 + release_metapage(ea_buf->mp);
882 + if (ea_buf->flag & EA_NEW)
883 + dbFree(inode, addressDXD(&ea_buf->new_ea),
884 + lengthDXD(&ea_buf->new_ea));
888 +static int ea_put(struct inode *inode, struct ea_buffer *ea_buf, int new_size)
890 + struct jfs_inode_info *ji = JFS_IP(inode);
891 + unsigned long old_blocks, new_blocks;
895 + if (new_size == 0) {
896 + ea_release(inode, ea_buf);
898 + } else if (ea_buf->flag & EA_INLINE) {
899 + assert(new_size <= sizeof (ji->i_inline_ea));
900 + ji->mode2 &= ~INLINEEA;
901 + ea_buf->new_ea.flag = DXD_INLINE;
902 + DXDsize(&ea_buf->new_ea, new_size);
903 + DXDaddress(&ea_buf->new_ea, 0);
904 + DXDlength(&ea_buf->new_ea, 0);
905 + } else if (ea_buf->flag & EA_MALLOC) {
906 + rc = ea_write(inode, ea_buf->xattr, new_size, &ea_buf->new_ea);
907 + kfree(ea_buf->xattr);
908 + } else if (ea_buf->flag & EA_NEW) {
909 + /* We have already allocated a new dxd */
910 + flush_metapage(ea_buf->mp);
912 + /* ->xattr must point to original ea's metapage */
913 + rc = ea_write(inode, ea_buf->xattr, new_size, &ea_buf->new_ea);
914 + discard_metapage(ea_buf->mp);
919 + tid = txBegin(inode->i_sb, 0);
920 + down(&ji->commit_sem);
922 + old_blocks = new_blocks = 0;
924 + if (ji->ea.flag & DXD_EXTENT) {
925 + invalidate_dxd_metapages(inode, ji->ea);
926 + old_blocks = lengthDXD(&ji->ea);
930 + txEA(tid, inode, &ji->ea, &ea_buf->new_ea);
931 + if (ea_buf->new_ea.flag & DXD_EXTENT) {
932 + new_blocks = lengthDXD(&ea_buf->new_ea);
933 + if (ji->ea.flag & DXD_INLINE)
934 + ji->mode2 |= INLINEEA;
936 + ji->ea = ea_buf->new_ea;
938 + txEA(tid, inode, &ji->ea, 0);
939 + if (ji->ea.flag & DXD_INLINE)
940 + ji->mode2 |= INLINEEA;
945 + inode->i_blocks += LBLK2PBLK(inode->i_sb, new_blocks - old_blocks);
946 + rc = txCommit(tid, 1, &inode, 0);
948 + up(&ji->commit_sem);
953 +static int can_set_xattr(struct inode *inode, const char *name,
954 + const void *value, size_t value_len)
956 + if (IS_RDONLY(inode))
959 + if (IS_IMMUTABLE(inode) || IS_APPEND(inode) || S_ISLNK(inode->i_mode))
962 + if((strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN) != 0) &&
963 + (strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN) != 0))
966 + if (!S_ISREG(inode->i_mode) &&
967 + (!S_ISDIR(inode->i_mode) || inode->i_mode &S_ISVTX))
970 + return permission(inode, MAY_WRITE);
973 +int __jfs_setxattr(struct inode *inode, const char *name, const void *value,
974 + size_t value_len, int flags)
976 + struct jfs_ea_list *ealist;
977 + struct jfs_ea *ea, *old_ea = NULL, *next_ea = NULL;
978 + struct ea_buffer ea_buf;
979 + int old_ea_size = 0;
982 + int namelen = strlen(name);
983 + char *os2name = NULL;
988 + if ((rc = can_set_xattr(inode, name, value, value_len)))
991 + if (strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN) == 0) {
992 + os2name = kmalloc(namelen - XATTR_OS2_PREFIX_LEN + 1,
996 + strcpy(os2name, name + XATTR_OS2_PREFIX_LEN);
998 + namelen -= XATTR_OS2_PREFIX_LEN;
1001 + xattr_size = ea_get(inode, &ea_buf, 0);
1002 + if (xattr_size < 0) {
1008 + ealist = (struct jfs_ea_list *) ea_buf.xattr;
1009 + new_size = sizeof (struct jfs_ea_list);
1012 + for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist);
1013 + ea = NEXT_EA(ea)) {
1014 + if ((namelen == ea->namelen) &&
1015 + (memcmp(name, ea->name, namelen) == 0)) {
1017 + if (flags & XATTR_CREATE) {
1022 + old_ea_size = EA_SIZE(ea);
1023 + next_ea = NEXT_EA(ea);
1025 + new_size += EA_SIZE(ea);
1030 + if (flags & XATTR_REPLACE) {
1034 + if (value == NULL) {
1040 + new_size += sizeof (struct jfs_ea) + namelen + 1 + value_len;
1042 + if (new_size > ea_buf.max_size) {
1044 + * We need to allocate more space for merged ea list.
1045 + * We should only have loop to again: once.
1047 + ea_release(inode, &ea_buf);
1048 + xattr_size = ea_get(inode, &ea_buf, new_size);
1049 + if (xattr_size < 0) {
1056 + /* Remove old ea of the same name */
1058 + /* number of bytes following target EA */
1059 + length = (char *) END_EALIST(ealist) - (char *) next_ea;
1061 + memmove(old_ea, next_ea, length);
1062 + xattr_size -= old_ea_size;
1065 + /* Add new entry to the end */
1067 + if (xattr_size == 0)
1068 + /* Completely new ea list */
1069 + xattr_size = sizeof (struct jfs_ea_list);
1071 + ea = (struct jfs_ea *) ((char *) ealist + xattr_size);
1073 + ea->namelen = namelen;
1074 + ea->valuelen = (cpu_to_le16(value_len));
1075 + memcpy(ea->name, name, namelen);
1076 + ea->name[namelen] = 0;
1078 + memcpy(&ea->name[namelen + 1], value, value_len);
1079 + xattr_size += EA_SIZE(ea);
1082 + /* DEBUG - If we did this right, these number match */
1083 + if (xattr_size != new_size) {
1085 + "jfs_xsetattr: xattr_size = %d, new_size = %d\n",
1086 + xattr_size, new_size);
1093 + * If we're left with an empty list, there's no ea
1095 + if (new_size == sizeof (struct jfs_ea_list))
1098 + ealist->size = cpu_to_le32(new_size);
1100 + rc = ea_put(inode, &ea_buf, new_size);
1104 + ea_release(inode, &ea_buf);
1112 +int jfs_setxattr(struct dentry *dentry, const char *name, const void *value,
1113 + size_t value_len, int flags)
1115 + if (value == NULL) { /* empty EA, do not remove */
1120 + return __jfs_setxattr(dentry->d_inode, name, value, value_len, flags);
1123 +static inline int can_get_xattr(struct inode *inode, const char *name)
1125 + return permission(inode, MAY_READ);
1128 +ssize_t __jfs_getxattr(struct inode *inode, const char *name, void *data,
1131 + struct jfs_ea_list *ealist;
1132 + struct jfs_ea *ea;
1133 + struct ea_buffer ea_buf;
1136 + int namelen = strlen(name);
1137 + char *os2name = NULL;
1141 + if ((rc = can_get_xattr(inode, name)))
1144 + if (strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN) == 0) {
1145 + os2name = kmalloc(namelen - XATTR_OS2_PREFIX_LEN + 1,
1149 + strcpy(os2name, name + XATTR_OS2_PREFIX_LEN);
1151 + namelen -= XATTR_OS2_PREFIX_LEN;
1154 + xattr_size = ea_get(inode, &ea_buf, 0);
1155 + if (xattr_size < 0) {
1156 + size = xattr_size;
1160 + if (xattr_size == 0)
1163 + ealist = (struct jfs_ea_list *) ea_buf.xattr;
1165 + /* Find the named attribute */
1166 + for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea))
1167 + if ((namelen == ea->namelen) &&
1168 + memcmp(name, ea->name, namelen) == 0) {
1170 + size = le16_to_cpu(ea->valuelen);
1173 + else if (size > buf_size) {
1177 + value = ((char *) &ea->name) + ea->namelen + 1;
1178 + memcpy(data, value, size);
1184 + ea_release(inode, &ea_buf);
1192 +ssize_t jfs_getxattr(struct dentry *dentry, const char *name, void *data,
1193 + size_t buf_size, int flags)
1195 + return __jfs_getxattr(dentry->d_inode, name, data, buf_size);
1198 +ssize_t jfs_listxattr(struct dentry * dentry, char *data, size_t buf_size,
1201 + struct inode *inode = dentry->d_inode;
1205 + struct jfs_ea_list *ealist;
1206 + struct jfs_ea *ea;
1207 + struct ea_buffer ea_buf;
1209 + xattr_size = ea_get(inode, &ea_buf, 0);
1210 + if (xattr_size < 0) {
1211 + size = xattr_size;
1215 + if (xattr_size == 0)
1218 + ealist = (struct jfs_ea_list *) ea_buf.xattr;
1220 + /* compute required size of list */
1221 + for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea))
1222 + size += name_size(ea) + 1;
1227 + if (size > buf_size) {
1232 + /* Copy attribute names to buffer */
1234 + for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea)) {
1235 + int namelen = copy_name(buffer, ea);
1236 + buffer += namelen + 1;
1240 + ea_release(inode, &ea_buf);
1245 +int jfs_removexattr(struct dentry *dentry, const char *name, int flags)
1247 + return __jfs_setxattr(dentry->d_inode, name, 0, 0, XATTR_REPLACE);