]>
Commit | Line | Data |
---|---|---|
2ae1bebf JR |
1 | -------------------------------------------------------------------- |
2 | This patch adds extended attribute support for JFS 1.1.1. | |
3 | ||
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 | |
9 | @@ -12,7 +12,7 @@ | |
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 | |
15 | obj-m := $(O_TARGET) | |
16 | ||
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 | |
21 | @@ -22,6 +22,7 @@ | |
22 | #include "jfs_incore.h" | |
23 | #include "jfs_dmap.h" | |
24 | #include "jfs_txnmgr.h" | |
25 | +#include "jfs_xattr.h" | |
26 | #include "jfs_debug.h" | |
27 | ||
28 | ||
29 | @@ -91,6 +92,10 @@ | |
30 | ||
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, | |
37 | }; | |
38 | ||
39 | struct file_operations jfs_file_operations = { | |
40 | @@ -102,3 +107,11 @@ | |
41 | .fsync = jfs_fsync, | |
42 | .release = jfs_release, | |
43 | }; | |
44 | + | |
45 | +struct inode_operations jfs_special_inode_operations = { | |
46 | + .setxattr = jfs_setxattr, | |
47 | + .getxattr = jfs_getxattr, | |
48 | + .listxattr = jfs_listxattr, | |
49 | + .removexattr = jfs_removexattr, | |
50 | +}; | |
51 | + | |
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 | |
55 | @@ -31,6 +31,7 @@ | |
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; | |
63 | @@ -96,6 +97,7 @@ | |
64 | } else | |
65 | inode->i_op = &jfs_symlink_inode_operations; | |
66 | } else { | |
67 | + inode->i_op = &jfs_special_inode_operations; | |
68 | init_special_inode(inode, inode->i_mode, | |
69 | kdev_t_to_nr(inode->i_rdev)); | |
70 | } | |
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 | |
74 | @@ -813,21 +813,19 @@ | |
75 | memcpy(&dp->di_fastsymlink, jfs_ip->i_inline, IDATASIZE); | |
76 | dilinelock->index++; | |
77 | } | |
78 | -#ifdef _STILL_TO_PORT | |
79 | /* | |
80 | * copy inline data from in-memory inode to on-disk inode: | |
81 | * 128 byte slot granularity | |
82 | */ | |
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; | |
87 | lv->length = 1; | |
88 | - memcpy(&dp->di_inlineea, &ip->i_inlineea, INODESLOTSIZE); | |
89 | + memcpy(&dp->di_inlineea, &jfs_ip->i_inline_ea, INODESLOTSIZE); | |
90 | dilinelock->index++; | |
91 | ||
92 | clear_cflag(COMMIT_Inlineea, ip); | |
93 | } | |
94 | -#endif /* _STILL_TO_PORT */ | |
95 | ||
96 | /* | |
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); | |
102 | - /* | |
103 | - * We may only need to do this for "special" inodes (dmap, imap) | |
104 | - */ | |
105 | + | |
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)) { | |
109 | + | |
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); | |
115 | - } | |
116 | + } else | |
117 | + memcpy(&jfs_ip->i_inline_ea, &dip->di_inlineea, 128); | |
118 | + | |
119 | /* Zero the in-memory-only stuff */ | |
120 | jfs_ip->cflag = 0; | |
121 | jfs_ip->btindex = 0; | |
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 | |
125 | @@ -82,6 +82,10 @@ | |
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 | |
131 | + */ | |
132 | + unchar _inline_ea[128]; /* 128: inline extended attr */ | |
133 | } link; | |
134 | } u; | |
135 | }; | |
136 | @@ -90,6 +94,7 @@ | |
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 | |
141 | ||
142 | ||
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 | |
147 | @@ -106,7 +106,7 @@ | |
148 | } | |
149 | ||
150 | /* | |
151 | - * This routines invalidate all pages for an extent. | |
152 | + * These routines invalidate all pages for an extent. | |
153 | */ | |
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 | |
159 | @@ -144,6 +144,8 @@ | |
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) | |
165 | ||
166 | /* | |
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 | |
171 | @@ -0,0 +1,64 @@ | |
172 | +/* | |
173 | + * Copyright (c) International Business Machines Corp., 2000-2002 | |
174 | + * | |
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. | |
179 | + * | |
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. | |
184 | + * | |
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 | |
188 | + */ | |
189 | + | |
190 | +#ifndef H_JFS_XATTR | |
191 | +#define H_JFS_XATTR | |
192 | + | |
193 | +/* | |
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. | |
197 | + */ | |
198 | +struct jfs_ea { | |
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 */ | |
204 | + | |
205 | +struct jfs_ea_list { | |
206 | + u32 size; /* overall size */ | |
207 | + struct jfs_ea ea[0]; /* Variable length list */ | |
208 | +}; | |
209 | + | |
210 | +/* Macros for defining maxiumum number of bytes supported for EAs */ | |
211 | +#define MAXEASIZE 65535 | |
212 | +#define MAXEALISTSIZE MAXEASIZE | |
213 | + | |
214 | +/* | |
215 | + * some macros for dealing with variable length EA lists. | |
216 | + */ | |
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))) | |
225 | + | |
226 | +extern int __jfs_setxattr(struct inode *, const char *, const void *, size_t, | |
227 | + int); | |
228 | +extern int jfs_setxattr(struct dentry *, const char *, const void *, size_t, | |
229 | + int); | |
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); | |
234 | + | |
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 | |
239 | @@ -24,10 +24,12 @@ | |
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" | |
245 | ||
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; | |
251 | ||
252 | @@ -915,6 +917,14 @@ | |
253 | i_fastsymlink = JFS_IP(ip)->i_inline; | |
254 | memcpy(i_fastsymlink, name, ssize); | |
255 | ip->i_size = ssize - 1; | |
256 | + | |
257 | + /* | |
258 | + * if symlink is > 128 bytes, we don't have the space to | |
259 | + * store inline extended attributes | |
260 | + */ | |
261 | + if (ssize > sizeof (JFS_IP(ip)->i_inline)) | |
262 | + JFS_IP(ip)->mode2 &= ~INLINEEA; | |
263 | + | |
264 | jFYI(1, | |
265 | ("jfs_symlink: fast symlink added ssize:%d name:%s \n", | |
266 | ssize, name)); | |
267 | @@ -1322,6 +1332,7 @@ | |
268 | if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack))) | |
269 | goto out3; | |
270 | ||
271 | + ip->i_op = &jfs_special_inode_operations; | |
272 | init_special_inode(ip, ip->i_mode, rdev); | |
273 | ||
274 | insert_inode_hash(ip); | |
275 | @@ -1409,6 +1420,10 @@ | |
276 | .rmdir = jfs_rmdir, | |
277 | .mknod = jfs_mknod, | |
278 | .rename = jfs_rename, | |
279 | + .setxattr = jfs_setxattr, | |
280 | + .getxattr = jfs_getxattr, | |
281 | + .listxattr = jfs_listxattr, | |
282 | + .removexattr = jfs_removexattr, | |
283 | }; | |
284 | ||
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 | |
289 | @@ -18,6 +18,7 @@ | |
290 | ||
291 | #include <linux/fs.h> | |
292 | #include "jfs_incore.h" | |
293 | +#include "jfs_xattr.h" | |
294 | ||
295 | static int jfs_follow_link(struct dentry *dentry, struct nameidata *nd) | |
296 | { | |
297 | @@ -34,5 +35,9 @@ | |
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, | |
305 | }; | |
306 | ||
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 | |
310 | @@ -0,0 +1,938 @@ | |
311 | +/* | |
312 | + * Copyright (c) International Business Machines Corp., 2000-2002 | |
313 | + * Copyright (c) Christoph Hellwig, 2002 | |
314 | + * | |
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. | |
319 | + * | |
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. | |
324 | + * | |
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 | |
328 | + */ | |
329 | + | |
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" | |
339 | + | |
340 | +/* | |
341 | + * jfs_xattr.c: extended attribute service | |
342 | + * | |
343 | + * Overall design -- | |
344 | + * | |
345 | + * Format: | |
346 | + * | |
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 | |
353 | + * | |
354 | + * 0 1 2 4 4 + namelen + 1 | |
355 | + * +-------+--------+--------+----------------+-------------------+ | |
356 | + * | Flags | Name | Value | Name String \0 | Data . . . . | | |
357 | + * | | Length | Length | | | | |
358 | + * +-------+--------+--------+----------------+-------------------+ | |
359 | + * | |
360 | + * A jfs_ea_list then is structured as | |
361 | + * | |
362 | + * 0 4 4 + EA_SIZE(ea1) | |
363 | + * +------------+-------------------+--------------------+----- | |
364 | + * | Overall EA | First FEA Element | Second FEA Element | ..... | |
365 | + * | List Size | | | | |
366 | + * +------------+-------------------+--------------------+----- | |
367 | + * | |
368 | + * On-disk: | |
369 | + * | |
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. | |
373 | + */ | |
374 | + | |
375 | +struct ea_buffer { | |
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 */ | |
381 | +}; | |
382 | + | |
383 | +/* | |
384 | + * ea_buffer.flag values | |
385 | + */ | |
386 | +#define EA_INLINE 0x0001 | |
387 | +#define EA_EXTENT 0x0002 | |
388 | +#define EA_NEW 0x0004 | |
389 | +#define EA_MALLOC 0x0008 | |
390 | + | |
391 | +/* Namespaces */ | |
392 | +#define XATTR_SYSTEM_PREFIX "system." | |
393 | +#define XATTR_SYSTEM_PREFIX_LEN (sizeof (XATTR_SYSTEM_PREFIX) - 1) | |
394 | + | |
395 | +#define XATTR_USER_PREFIX "user." | |
396 | +#define XATTR_USER_PREFIX_LEN (sizeof (XATTR_USER_PREFIX) - 1) | |
397 | + | |
398 | +#define XATTR_OS2_PREFIX "os2." | |
399 | +#define XATTR_OS2_PREFIX_LEN (sizeof (XATTR_OS2_PREFIX) - 1) | |
400 | + | |
401 | +/* | |
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 | |
405 | + */ | |
406 | +static inline int is_os2_xattr(struct jfs_ea *ea) | |
407 | +{ | |
408 | + /* | |
409 | + * Check for "system." | |
410 | + */ | |
411 | + if ((ea->namelen >= XATTR_SYSTEM_PREFIX_LEN) && | |
412 | + !strncmp(ea->name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) | |
413 | + return FALSE; | |
414 | + /* | |
415 | + * Check for "user." | |
416 | + */ | |
417 | + if ((ea->namelen >= XATTR_USER_PREFIX_LEN) && | |
418 | + !strncmp(ea->name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) | |
419 | + return FALSE; | |
420 | + /* | |
421 | + * Add any other valid namespace prefixes here | |
422 | + */ | |
423 | + | |
424 | + /* | |
425 | + * We assume it's OS/2's flat namespace | |
426 | + */ | |
427 | + return TRUE; | |
428 | +} | |
429 | + | |
430 | +static inline int name_size(struct jfs_ea *ea) | |
431 | +{ | |
432 | + if (is_os2_xattr(ea)) | |
433 | + return ea->namelen + XATTR_OS2_PREFIX_LEN; | |
434 | + else | |
435 | + return ea->namelen; | |
436 | +} | |
437 | + | |
438 | +static inline int copy_name(char *buffer, struct jfs_ea *ea) | |
439 | +{ | |
440 | + int len = ea->namelen; | |
441 | + | |
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; | |
446 | + } | |
447 | + memcpy(buffer, ea->name, ea->namelen); | |
448 | + buffer[ea->namelen] = 0; | |
449 | + | |
450 | + return len; | |
451 | +} | |
452 | + | |
453 | +/* Forward references */ | |
454 | +static void ea_release(struct inode *inode, struct ea_buffer *ea_buf); | |
455 | + | |
456 | +/* | |
457 | + * NAME: ea_write_inline | |
458 | + * | |
459 | + * FUNCTION: Attempt to write an EA inline if area is available | |
460 | + * | |
461 | + * PRE CONDITIONS: | |
462 | + * Already verified that the specified EA is small enough to fit inline | |
463 | + * | |
464 | + * PARAMETERS: | |
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 | |
470 | + * | |
471 | + * NOTES: | |
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. | |
475 | + * | |
476 | + * RETURNS: 0 for successful copy to inline area; -1 if area not available | |
477 | + */ | |
478 | +static int ea_write_inline(struct inode *ip, struct jfs_ea_list *ealist, | |
479 | + int size, dxd_t * ea) | |
480 | +{ | |
481 | + struct jfs_inode_info *ji = JFS_IP(ip); | |
482 | + | |
483 | + /* | |
484 | + * Make sure we have an EA -- the NULL EA list is valid, but you | |
485 | + * can't copy it! | |
486 | + */ | |
487 | + if (ealist && size > sizeof (struct jfs_ea_list)) { | |
488 | + assert(size <= sizeof (ji->i_inline_ea)); | |
489 | + | |
490 | + /* | |
491 | + * See if the space is available or if it is already being | |
492 | + * used for an inline EA. | |
493 | + */ | |
494 | + if (!(ji->mode2 & INLINEEA) && !(ji->ea.flag & DXD_INLINE)) | |
495 | + return -1; | |
496 | + | |
497 | + DXDsize(ea, size); | |
498 | + DXDlength(ea, 0); | |
499 | + DXDaddress(ea, 0); | |
500 | + memcpy(ji->i_inline_ea, ealist, size); | |
501 | + ea->flag = DXD_INLINE; | |
502 | + ji->mode2 &= ~INLINEEA; | |
503 | + } else { | |
504 | + ea->flag = 0; | |
505 | + DXDsize(ea, 0); | |
506 | + DXDlength(ea, 0); | |
507 | + DXDaddress(ea, 0); | |
508 | + | |
509 | + /* Free up INLINE area */ | |
510 | + if (ji->ea.flag & DXD_INLINE) | |
511 | + ji->mode2 |= INLINEEA; | |
512 | + } | |
513 | + | |
514 | + mark_inode_dirty(ip); | |
515 | + return 0; | |
516 | +} | |
517 | + | |
518 | +/* | |
519 | + * NAME: ea_write | |
520 | + * | |
521 | + * FUNCTION: Write an EA for an inode | |
522 | + * | |
523 | + * PRE CONDITIONS: EA has been verified | |
524 | + * | |
525 | + * PARAMETERS: | |
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 | |
530 | + * EA was copied | |
531 | + * | |
532 | + * NOTES: Will write EA inline if able to, otherwise allocates blocks for an | |
533 | + * extent and synchronously writes it to those blocks. | |
534 | + * | |
535 | + * RETURNS: 0 for success; Anything else indicates failure | |
536 | + */ | |
537 | +static int ea_write(struct inode *ip, struct jfs_ea_list *ealist, int size, | |
538 | + dxd_t * ea) | |
539 | +{ | |
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); | |
543 | + int nblocks; | |
544 | + s64 blkno; | |
545 | + int rc = 0, i; | |
546 | + char *cp; | |
547 | + s32 nbytes, nb; | |
548 | + s32 bytes_to_write; | |
549 | + struct metapage *mp; | |
550 | + | |
551 | + /* | |
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. | |
554 | + */ | |
555 | + if (!ealist || size <= sizeof (ji->i_inline_ea)) { | |
556 | + if (!ea_write_inline(ip, ealist, size, ea)) | |
557 | + return 0; | |
558 | + } | |
559 | + | |
560 | + /* figure out how many blocks we need */ | |
561 | + nblocks = (size + (sb->s_blocksize - 1)) >> sb->s_blocksize_bits; | |
562 | + | |
563 | + rc = dbAlloc(ip, INOHINT(ip), nblocks, &blkno); | |
564 | + if (rc) | |
565 | + return -rc; | |
566 | + | |
567 | + /* | |
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 | |
570 | + * a time. | |
571 | + */ | |
572 | + cp = (char *) ealist; | |
573 | + nbytes = size; | |
574 | + for (i = 0; i < nblocks; i += sbi->nbperpage) { | |
575 | + /* | |
576 | + * Determine how many bytes for this request, and round up to | |
577 | + * the nearest aggregate block size | |
578 | + */ | |
579 | + nb = min(PSIZE, nbytes); | |
580 | + bytes_to_write = | |
581 | + ((((nb + sb->s_blocksize - 1)) >> sb->s_blocksize_bits)) | |
582 | + << sb->s_blocksize_bits; | |
583 | + | |
584 | + if (!(mp = get_metapage(ip, blkno + i, bytes_to_write, 1))) { | |
585 | + rc = -EIO; | |
586 | + goto failed; | |
587 | + } | |
588 | + | |
589 | + memcpy(mp->data, cp, nb); | |
590 | + | |
591 | + /* | |
592 | + * We really need a way to propagate errors for | |
593 | + * forced writes like this one. --hch | |
594 | + * | |
595 | + * (__write_metapage => release_metapage => flush_metapage) | |
596 | + */ | |
597 | +#ifdef _JFS_FIXME | |
598 | + if ((rc = flush_metapage(mp))) { | |
599 | + /* | |
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 | |
603 | + * we can get ... | |
604 | + */ | |
605 | + goto failed; | |
606 | + } | |
607 | +#else | |
608 | + flush_metapage(mp); | |
609 | +#endif | |
610 | + | |
611 | + cp += PSIZE; | |
612 | + nbytes -= nb; | |
613 | + } | |
614 | + | |
615 | + ea->flag = DXD_EXTENT; | |
616 | + DXDsize(ea, le32_to_cpu(ealist->size)); | |
617 | + DXDlength(ea, nblocks); | |
618 | + DXDaddress(ea, blkno); | |
619 | + | |
620 | + /* Free up INLINE area */ | |
621 | + if (ji->ea.flag & DXD_INLINE) | |
622 | + ji->mode2 |= INLINEEA; | |
623 | + | |
624 | + return 0; | |
625 | + | |
626 | + failed: | |
627 | + dbFree(ip, blkno, nblocks); | |
628 | + return rc; | |
629 | +} | |
630 | + | |
631 | +/* | |
632 | + * NAME: ea_read_inline | |
633 | + * | |
634 | + * FUNCTION: Read an inlined EA into user's buffer | |
635 | + * | |
636 | + * PARAMETERS: | |
637 | + * ip - Inode pointer | |
638 | + * ealist - Pointer to buffer to fill in with EA | |
639 | + * | |
640 | + * RETURNS: 0 | |
641 | + */ | |
642 | +static int ea_read_inline(struct inode *ip, struct jfs_ea_list *ealist) | |
643 | +{ | |
644 | + struct jfs_inode_info *ji = JFS_IP(ip); | |
645 | + int ea_size = sizeDXD(&ji->ea); | |
646 | + | |
647 | + if (ea_size == 0) { | |
648 | + ealist->size = 0; | |
649 | + return 0; | |
650 | + } | |
651 | + | |
652 | + /* Sanity Check */ | |
653 | + if ((sizeDXD(&ji->ea) > sizeof (ji->i_inline_ea))) | |
654 | + return -EIO; | |
655 | + if (le32_to_cpu(((struct jfs_ea_list *) &ji->i_inline_ea)->size) | |
656 | + != ea_size) | |
657 | + return -EIO; | |
658 | + | |
659 | + memcpy(ealist, ji->i_inline_ea, ea_size); | |
660 | + return 0; | |
661 | +} | |
662 | + | |
663 | +/* | |
664 | + * NAME: ea_read | |
665 | + * | |
666 | + * FUNCTION: copy EA data into user's buffer | |
667 | + * | |
668 | + * PARAMETERS: | |
669 | + * ip - Inode pointer | |
670 | + * ealist - Pointer to buffer to fill in with EA | |
671 | + * | |
672 | + * NOTES: If EA is inline calls ea_read_inline() to copy EA. | |
673 | + * | |
674 | + * RETURNS: 0 for success; other indicates failure | |
675 | + */ | |
676 | +static int ea_read(struct inode *ip, struct jfs_ea_list *ealist) | |
677 | +{ | |
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); | |
681 | + int nblocks; | |
682 | + s64 blkno; | |
683 | + char *cp = (char *) ealist; | |
684 | + int i; | |
685 | + int nbytes, nb; | |
686 | + s32 bytes_to_read; | |
687 | + struct metapage *mp; | |
688 | + | |
689 | + /* quick check for in-line EA */ | |
690 | + if (ji->ea.flag & DXD_INLINE) | |
691 | + return ea_read_inline(ip, ealist); | |
692 | + | |
693 | + nbytes = sizeDXD(&ji->ea); | |
694 | + assert(nbytes); | |
695 | + | |
696 | + /* | |
697 | + * Figure out how many blocks were allocated when this EA list was | |
698 | + * originally written to disk. | |
699 | + */ | |
700 | + nblocks = lengthDXD(&ji->ea) << sbi->l2nbperpage; | |
701 | + blkno = addressDXD(&ji->ea) << sbi->l2nbperpage; | |
702 | + | |
703 | + /* | |
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. | |
707 | + */ | |
708 | + for (i = 0; i < nblocks; i += sbi->nbperpage) { | |
709 | + /* | |
710 | + * Determine how many bytes for this request, and round up to | |
711 | + * the nearest aggregate block size | |
712 | + */ | |
713 | + nb = min(PSIZE, nbytes); | |
714 | + bytes_to_read = | |
715 | + ((((nb + sb->s_blocksize - 1)) >> sb->s_blocksize_bits)) | |
716 | + << sb->s_blocksize_bits; | |
717 | + | |
718 | + if (!(mp = read_metapage(ip, blkno + i, bytes_to_read, 1))) | |
719 | + return -EIO; | |
720 | + | |
721 | + memcpy(cp, mp->data, nb); | |
722 | + release_metapage(mp); | |
723 | + | |
724 | + cp += PSIZE; | |
725 | + nbytes -= nb; | |
726 | + } | |
727 | + | |
728 | + return 0; | |
729 | +} | |
730 | + | |
731 | +/* | |
732 | + * NAME: ea_get | |
733 | + * | |
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. | |
737 | + * | |
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 | |
740 | + * | |
741 | + * PARAMETERS: | |
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 | |
745 | + * | |
746 | + * RETURNS: 0 for success; Other indicates failure | |
747 | + */ | |
748 | +static int ea_get(struct inode *inode, struct ea_buffer *ea_buf, int min_size) | |
749 | +{ | |
750 | + struct jfs_inode_info *ji = JFS_IP(inode); | |
751 | + struct super_block *sb = inode->i_sb; | |
752 | + int size; | |
753 | + int ea_size = sizeDXD(&ji->ea); | |
754 | + int blocks_needed, current_blocks; | |
755 | + s64 blkno; | |
756 | + int rc; | |
757 | + | |
758 | + /* When fsck.jfs clears a bad ea, it doesn't clear the size */ | |
759 | + if (ji->ea.flag == 0) | |
760 | + ea_size = 0; | |
761 | + | |
762 | + if (ea_size == 0) { | |
763 | + if (min_size == 0) { | |
764 | + ea_buf->flag = 0; | |
765 | + ea_buf->max_size = 0; | |
766 | + ea_buf->xattr = NULL; | |
767 | + return 0; | |
768 | + } | |
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); | |
778 | + return 0; | |
779 | + } | |
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; | |
786 | + goto size_check; | |
787 | + } | |
788 | + current_blocks = 0; | |
789 | + } else { | |
790 | + assert(ji->ea.flag & DXD_EXTENT); | |
791 | + current_blocks = (ea_size + sb->s_blocksize - 1) >> | |
792 | + sb->s_blocksize_bits; | |
793 | + } | |
794 | + size = max(min_size, ea_size); | |
795 | + | |
796 | + if (size > PSIZE) { | |
797 | + /* | |
798 | + * To keep the rest of the code simple. Allocate a | |
799 | + * contiguous buffer to work with | |
800 | + */ | |
801 | + ea_buf->xattr = kmalloc(size, GFP_KERNEL); | |
802 | + if (ea_buf->xattr == NULL) | |
803 | + return -ENOMEM; | |
804 | + | |
805 | + ea_buf->flag |= EA_MALLOC; | |
806 | + ea_buf->max_size = (size + sb->s_blocksize - 1) & | |
807 | + ~(sb->s_blocksize - 1); | |
808 | + | |
809 | + if (ea_size == 0) | |
810 | + return 0; | |
811 | + | |
812 | + if ((rc = ea_read(inode, ea_buf->xattr))) { | |
813 | + kfree(ea_buf->xattr); | |
814 | + ea_buf->xattr = NULL; | |
815 | + return rc; | |
816 | + } | |
817 | + goto size_check; | |
818 | + } | |
819 | + blocks_needed = (min_size + sb->s_blocksize - 1) >> | |
820 | + sb->s_blocksize_bits; | |
821 | + | |
822 | + if (blocks_needed > current_blocks) { | |
823 | + rc = dbAlloc(inode, INOHINT(inode), (s64) blocks_needed, | |
824 | + &blkno); | |
825 | + if (rc) | |
826 | + return -rc; | |
827 | + | |
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); | |
832 | + | |
833 | + ea_buf->flag = EA_EXTENT | EA_NEW; | |
834 | + | |
835 | + ea_buf->mp = get_metapage(inode, blkno, | |
836 | + blocks_needed << sb->s_blocksize_bits, | |
837 | + 1); | |
838 | + if (ea_buf->mp == NULL) { | |
839 | + dbFree(inode, blkno, (s64) blocks_needed); | |
840 | + return -EIO; | |
841 | + } | |
842 | + ea_buf->xattr = ea_buf->mp->data; | |
843 | + ea_buf->max_size = (min_size + sb->s_blocksize - 1) & | |
844 | + ~(sb->s_blocksize - 1); | |
845 | + if (ea_size == 0) | |
846 | + return 0; | |
847 | + if ((rc = ea_read(inode, ea_buf->xattr))) { | |
848 | + discard_metapage(ea_buf->mp); | |
849 | + dbFree(inode, blkno, (s64) blocks_needed); | |
850 | + return rc; | |
851 | + } | |
852 | + goto size_check; | |
853 | + } | |
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) | |
858 | + return -EIO; | |
859 | + ea_buf->xattr = ea_buf->mp->data; | |
860 | + ea_buf->max_size = (ea_size + sb->s_blocksize - 1) & | |
861 | + ~(sb->s_blocksize - 1); | |
862 | + | |
863 | + size_check: | |
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); | |
868 | + return -EIO; | |
869 | + } | |
870 | + | |
871 | + return ea_size; | |
872 | +} | |
873 | + | |
874 | +static void ea_release(struct inode *inode, struct ea_buffer *ea_buf) | |
875 | +{ | |
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); | |
881 | + | |
882 | + if (ea_buf->flag & EA_NEW) | |
883 | + dbFree(inode, addressDXD(&ea_buf->new_ea), | |
884 | + lengthDXD(&ea_buf->new_ea)); | |
885 | + } | |
886 | +} | |
887 | + | |
888 | +static int ea_put(struct inode *inode, struct ea_buffer *ea_buf, int new_size) | |
889 | +{ | |
890 | + struct jfs_inode_info *ji = JFS_IP(inode); | |
891 | + unsigned long old_blocks, new_blocks; | |
892 | + int rc = 0; | |
893 | + tid_t tid; | |
894 | + | |
895 | + if (new_size == 0) { | |
896 | + ea_release(inode, ea_buf); | |
897 | + ea_buf = 0; | |
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); | |
911 | + } else { | |
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); | |
915 | + } | |
916 | + if (rc) | |
917 | + return rc; | |
918 | + | |
919 | + tid = txBegin(inode->i_sb, 0); | |
920 | + down(&ji->commit_sem); | |
921 | + | |
922 | + old_blocks = new_blocks = 0; | |
923 | + | |
924 | + if (ji->ea.flag & DXD_EXTENT) { | |
925 | + invalidate_dxd_metapages(inode, ji->ea); | |
926 | + old_blocks = lengthDXD(&ji->ea); | |
927 | + } | |
928 | + | |
929 | + if (ea_buf) { | |
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; | |
935 | + } | |
936 | + ji->ea = ea_buf->new_ea; | |
937 | + } else { | |
938 | + txEA(tid, inode, &ji->ea, 0); | |
939 | + if (ji->ea.flag & DXD_INLINE) | |
940 | + ji->mode2 |= INLINEEA; | |
941 | + ji->ea.flag = 0; | |
942 | + ji->ea.size = 0; | |
943 | + } | |
944 | + | |
945 | + inode->i_blocks += LBLK2PBLK(inode->i_sb, new_blocks - old_blocks); | |
946 | + rc = txCommit(tid, 1, &inode, 0); | |
947 | + txEnd(tid); | |
948 | + up(&ji->commit_sem); | |
949 | + | |
950 | + return rc; | |
951 | +} | |
952 | + | |
953 | +static int can_set_xattr(struct inode *inode, const char *name, | |
954 | + const void *value, size_t value_len) | |
955 | +{ | |
956 | + if (IS_RDONLY(inode)) | |
957 | + return -EROFS; | |
958 | + | |
959 | + if (IS_IMMUTABLE(inode) || IS_APPEND(inode) || S_ISLNK(inode->i_mode)) | |
960 | + return -EPERM; | |
961 | + | |
962 | + if((strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN) != 0) && | |
963 | + (strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN) != 0)) | |
964 | + return -ENOTSUP; | |
965 | + | |
966 | + if (!S_ISREG(inode->i_mode) && | |
967 | + (!S_ISDIR(inode->i_mode) || inode->i_mode &S_ISVTX)) | |
968 | + return -EPERM; | |
969 | + | |
970 | + return permission(inode, MAY_WRITE); | |
971 | +} | |
972 | + | |
973 | +int __jfs_setxattr(struct inode *inode, const char *name, const void *value, | |
974 | + size_t value_len, int flags) | |
975 | +{ | |
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; | |
980 | + int xattr_size; | |
981 | + int new_size; | |
982 | + int namelen = strlen(name); | |
983 | + char *os2name = NULL; | |
984 | + int found = 0; | |
985 | + int rc; | |
986 | + int length; | |
987 | + | |
988 | + if ((rc = can_set_xattr(inode, name, value, value_len))) | |
989 | + return rc; | |
990 | + | |
991 | + if (strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN) == 0) { | |
992 | + os2name = kmalloc(namelen - XATTR_OS2_PREFIX_LEN + 1, | |
993 | + GFP_KERNEL); | |
994 | + if (!os2name) | |
995 | + return -ENOMEM; | |
996 | + strcpy(os2name, name + XATTR_OS2_PREFIX_LEN); | |
997 | + name = os2name; | |
998 | + namelen -= XATTR_OS2_PREFIX_LEN; | |
999 | + } | |
1000 | + | |
1001 | + xattr_size = ea_get(inode, &ea_buf, 0); | |
1002 | + if (xattr_size < 0) { | |
1003 | + rc = xattr_size; | |
1004 | + goto out; | |
1005 | + } | |
1006 | + | |
1007 | + again: | |
1008 | + ealist = (struct jfs_ea_list *) ea_buf.xattr; | |
1009 | + new_size = sizeof (struct jfs_ea_list); | |
1010 | + | |
1011 | + if (xattr_size) { | |
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)) { | |
1016 | + found = 1; | |
1017 | + if (flags & XATTR_CREATE) { | |
1018 | + rc = -EEXIST; | |
1019 | + goto release; | |
1020 | + } | |
1021 | + old_ea = ea; | |
1022 | + old_ea_size = EA_SIZE(ea); | |
1023 | + next_ea = NEXT_EA(ea); | |
1024 | + } else | |
1025 | + new_size += EA_SIZE(ea); | |
1026 | + } | |
1027 | + } | |
1028 | + | |
1029 | + if (!found) { | |
1030 | + if (flags & XATTR_REPLACE) { | |
1031 | + rc = -ENOATTR; | |
1032 | + goto release; | |
1033 | + } | |
1034 | + if (value == NULL) { | |
1035 | + rc = 0; | |
1036 | + goto release; | |
1037 | + } | |
1038 | + } | |
1039 | + if (value) | |
1040 | + new_size += sizeof (struct jfs_ea) + namelen + 1 + value_len; | |
1041 | + | |
1042 | + if (new_size > ea_buf.max_size) { | |
1043 | + /* | |
1044 | + * We need to allocate more space for merged ea list. | |
1045 | + * We should only have loop to again: once. | |
1046 | + */ | |
1047 | + ea_release(inode, &ea_buf); | |
1048 | + xattr_size = ea_get(inode, &ea_buf, new_size); | |
1049 | + if (xattr_size < 0) { | |
1050 | + rc = xattr_size; | |
1051 | + goto out; | |
1052 | + } | |
1053 | + goto again; | |
1054 | + } | |
1055 | + | |
1056 | + /* Remove old ea of the same name */ | |
1057 | + if (found) { | |
1058 | + /* number of bytes following target EA */ | |
1059 | + length = (char *) END_EALIST(ealist) - (char *) next_ea; | |
1060 | + if (length > 0) | |
1061 | + memmove(old_ea, next_ea, length); | |
1062 | + xattr_size -= old_ea_size; | |
1063 | + } | |
1064 | + | |
1065 | + /* Add new entry to the end */ | |
1066 | + if (value) { | |
1067 | + if (xattr_size == 0) | |
1068 | + /* Completely new ea list */ | |
1069 | + xattr_size = sizeof (struct jfs_ea_list); | |
1070 | + | |
1071 | + ea = (struct jfs_ea *) ((char *) ealist + xattr_size); | |
1072 | + ea->flag = 0; | |
1073 | + ea->namelen = namelen; | |
1074 | + ea->valuelen = (cpu_to_le16(value_len)); | |
1075 | + memcpy(ea->name, name, namelen); | |
1076 | + ea->name[namelen] = 0; | |
1077 | + if (value_len) | |
1078 | + memcpy(&ea->name[namelen + 1], value, value_len); | |
1079 | + xattr_size += EA_SIZE(ea); | |
1080 | + } | |
1081 | + | |
1082 | + /* DEBUG - If we did this right, these number match */ | |
1083 | + if (xattr_size != new_size) { | |
1084 | + printk(KERN_ERR | |
1085 | + "jfs_xsetattr: xattr_size = %d, new_size = %d\n", | |
1086 | + xattr_size, new_size); | |
1087 | + | |
1088 | + rc = -EINVAL; | |
1089 | + goto release; | |
1090 | + } | |
1091 | + | |
1092 | + /* | |
1093 | + * If we're left with an empty list, there's no ea | |
1094 | + */ | |
1095 | + if (new_size == sizeof (struct jfs_ea_list)) | |
1096 | + new_size = 0; | |
1097 | + | |
1098 | + ealist->size = cpu_to_le32(new_size); | |
1099 | + | |
1100 | + rc = ea_put(inode, &ea_buf, new_size); | |
1101 | + | |
1102 | + goto out; | |
1103 | + release: | |
1104 | + ea_release(inode, &ea_buf); | |
1105 | + out: | |
1106 | + if (os2name) | |
1107 | + kfree(os2name); | |
1108 | + | |
1109 | + return rc; | |
1110 | +} | |
1111 | + | |
1112 | +int jfs_setxattr(struct dentry *dentry, const char *name, const void *value, | |
1113 | + size_t value_len, int flags) | |
1114 | +{ | |
1115 | + if (value == NULL) { /* empty EA, do not remove */ | |
1116 | + value = ""; | |
1117 | + value_len = 0; | |
1118 | + } | |
1119 | + | |
1120 | + return __jfs_setxattr(dentry->d_inode, name, value, value_len, flags); | |
1121 | +} | |
1122 | + | |
1123 | +static inline int can_get_xattr(struct inode *inode, const char *name) | |
1124 | +{ | |
1125 | + return permission(inode, MAY_READ); | |
1126 | +} | |
1127 | + | |
1128 | +ssize_t __jfs_getxattr(struct inode *inode, const char *name, void *data, | |
1129 | + size_t buf_size) | |
1130 | +{ | |
1131 | + struct jfs_ea_list *ealist; | |
1132 | + struct jfs_ea *ea; | |
1133 | + struct ea_buffer ea_buf; | |
1134 | + int xattr_size; | |
1135 | + ssize_t size; | |
1136 | + int namelen = strlen(name); | |
1137 | + char *os2name = NULL; | |
1138 | + int rc; | |
1139 | + char *value; | |
1140 | + | |
1141 | + if ((rc = can_get_xattr(inode, name))) | |
1142 | + return rc; | |
1143 | + | |
1144 | + if (strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN) == 0) { | |
1145 | + os2name = kmalloc(namelen - XATTR_OS2_PREFIX_LEN + 1, | |
1146 | + GFP_KERNEL); | |
1147 | + if (!os2name) | |
1148 | + return -ENOMEM; | |
1149 | + strcpy(os2name, name + XATTR_OS2_PREFIX_LEN); | |
1150 | + name = os2name; | |
1151 | + namelen -= XATTR_OS2_PREFIX_LEN; | |
1152 | + } | |
1153 | + | |
1154 | + xattr_size = ea_get(inode, &ea_buf, 0); | |
1155 | + if (xattr_size < 0) { | |
1156 | + size = xattr_size; | |
1157 | + goto out; | |
1158 | + } | |
1159 | + | |
1160 | + if (xattr_size == 0) | |
1161 | + goto not_found; | |
1162 | + | |
1163 | + ealist = (struct jfs_ea_list *) ea_buf.xattr; | |
1164 | + | |
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) { | |
1169 | + /* Found it */ | |
1170 | + size = le16_to_cpu(ea->valuelen); | |
1171 | + if (!data) | |
1172 | + goto release; | |
1173 | + else if (size > buf_size) { | |
1174 | + size = -ERANGE; | |
1175 | + goto release; | |
1176 | + } | |
1177 | + value = ((char *) &ea->name) + ea->namelen + 1; | |
1178 | + memcpy(data, value, size); | |
1179 | + goto release; | |
1180 | + } | |
1181 | + not_found: | |
1182 | + size = -ENOATTR; | |
1183 | + release: | |
1184 | + ea_release(inode, &ea_buf); | |
1185 | + out: | |
1186 | + if (os2name) | |
1187 | + kfree(os2name); | |
1188 | + | |
1189 | + return size; | |
1190 | +} | |
1191 | + | |
1192 | +ssize_t jfs_getxattr(struct dentry *dentry, const char *name, void *data, | |
1193 | + size_t buf_size, int flags) | |
1194 | +{ | |
1195 | + return __jfs_getxattr(dentry->d_inode, name, data, buf_size); | |
1196 | +} | |
1197 | + | |
1198 | +ssize_t jfs_listxattr(struct dentry * dentry, char *data, size_t buf_size, | |
1199 | + int flags) | |
1200 | +{ | |
1201 | + struct inode *inode = dentry->d_inode; | |
1202 | + char *buffer; | |
1203 | + ssize_t size = 0; | |
1204 | + int xattr_size; | |
1205 | + struct jfs_ea_list *ealist; | |
1206 | + struct jfs_ea *ea; | |
1207 | + struct ea_buffer ea_buf; | |
1208 | + | |
1209 | + xattr_size = ea_get(inode, &ea_buf, 0); | |
1210 | + if (xattr_size < 0) { | |
1211 | + size = xattr_size; | |
1212 | + goto out; | |
1213 | + } | |
1214 | + | |
1215 | + if (xattr_size == 0) | |
1216 | + goto release; | |
1217 | + | |
1218 | + ealist = (struct jfs_ea_list *) ea_buf.xattr; | |
1219 | + | |
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; | |
1223 | + | |
1224 | + if (!data) | |
1225 | + goto release; | |
1226 | + | |
1227 | + if (size > buf_size) { | |
1228 | + size = -ERANGE; | |
1229 | + goto release; | |
1230 | + } | |
1231 | + | |
1232 | + /* Copy attribute names to buffer */ | |
1233 | + buffer = data; | |
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; | |
1237 | + } | |
1238 | + | |
1239 | + release: | |
1240 | + ea_release(inode, &ea_buf); | |
1241 | + out: | |
1242 | + return size; | |
1243 | +} | |
1244 | + | |
1245 | +int jfs_removexattr(struct dentry *dentry, const char *name, int flags) | |
1246 | +{ | |
1247 | + return __jfs_setxattr(dentry->d_inode, name, 0, 0, XATTR_REPLACE); | |
1248 | +} |