[JFFS2][XATTR] '#include <linux/list.h>' was added into xattr.h.
[deliverable/linux.git] / fs / jffs2 / xattr.c
CommitLineData
aa98d7cf
KK
1/* -------------------------------------------------------------------------
2 * File: fs/jffs2/xattr.c
3 * XATTR support on JFFS2 FileSystem
4 *
5 * Implemented by KaiGai Kohei <kaigai@ak.jp.nec.com>
6 * Copyright (C) 2006 NEC Corporation
7 *
8 * For licensing information, see the file 'LICENCE' in the jffs2 directory.
9 * ------------------------------------------------------------------------- */
10
11#include <linux/kernel.h>
12#include <linux/slab.h>
13#include <linux/fs.h>
14#include <linux/time.h>
15#include <linux/pagemap.h>
16#include <linux/highmem.h>
17#include <linux/crc32.h>
18#include <linux/jffs2.h>
19#include <linux/xattr.h>
20#include <linux/mtd/mtd.h>
21#include "nodelist.h"
22/* -------- xdatum related functions ----------------
23 * xattr_datum_hashkey(xprefix, xname, xvalue, xsize)
24 * is used to calcurate xdatum hashkey. The reminder of hashkey into XATTRINDEX_HASHSIZE is
25 * the index of the xattr name/value pair cache (c->xattrindex).
26 * unload_xattr_datum(c, xd)
27 * is used to release xattr name/value pair and detach from c->xattrindex.
28 * reclaim_xattr_datum(c)
29 * is used to reclaim xattr name/value pairs on the xattr name/value pair cache when
30 * memory usage by cache is over c->xdatum_mem_threshold. Currentry, this threshold
31 * is hard coded as 32KiB.
32 * delete_xattr_datum_node(c, xd)
33 * is used to delete a jffs2 node is dominated by xdatum. When EBS(Erase Block Summary) is
34 * enabled, it overwrites the obsolete node by myself.
35 * delete_xattr_datum(c, xd)
36 * is used to delete jffs2_xattr_datum object. It must be called with 0-value of reference
37 * counter. (It means how many jffs2_xattr_ref object refers this xdatum.)
38 * do_verify_xattr_datum(c, xd)
39 * is used to load the xdatum informations without name/value pair from the medium.
40 * It's necessary once, because those informations are not collected during mounting
41 * process when EBS is enabled.
42 * 0 will be returned, if success. An negative return value means recoverable error, and
43 * positive return value means unrecoverable error. Thus, caller must remove this xdatum
44 * and xref when it returned positive value.
45 * do_load_xattr_datum(c, xd)
46 * is used to load name/value pair from the medium.
47 * The meanings of return value is same as do_verify_xattr_datum().
48 * load_xattr_datum(c, xd)
49 * is used to be as a wrapper of do_verify_xattr_datum() and do_load_xattr_datum().
50 * If xd need to call do_verify_xattr_datum() at first, it's called before calling
51 * do_load_xattr_datum(). The meanings of return value is same as do_verify_xattr_datum().
52 * save_xattr_datum(c, xd, phys_ofs)
53 * is used to write xdatum to medium. xd->version will be incremented.
54 * create_xattr_datum(c, xprefix, xname, xvalue, xsize, phys_ofs)
55 * is used to create new xdatum and write to medium.
56 * -------------------------------------------------- */
57
58static uint32_t xattr_datum_hashkey(int xprefix, const char *xname, const char *xvalue, int xsize)
59{
60 int name_len = strlen(xname);
61
62 return crc32(xprefix, xname, name_len) ^ crc32(xprefix, xvalue, xsize);
63}
64
65static void unload_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
66{
67 /* must be called under down_write(xattr_sem) */
68 D1(dbg_xattr("%s: xid=%u, version=%u\n", __FUNCTION__, xd->xid, xd->version));
69 if (xd->xname) {
70 c->xdatum_mem_usage -= (xd->name_len + 1 + xd->value_len);
71 kfree(xd->xname);
72 }
73
74 list_del_init(&xd->xindex);
75 xd->hashkey = 0;
76 xd->xname = NULL;
77 xd->xvalue = NULL;
78}
79
80static void reclaim_xattr_datum(struct jffs2_sb_info *c)
81{
82 /* must be called under down_write(xattr_sem) */
83 struct jffs2_xattr_datum *xd, *_xd;
84 uint32_t target, before;
85 static int index = 0;
86 int count;
87
88 if (c->xdatum_mem_threshold > c->xdatum_mem_usage)
89 return;
90
91 before = c->xdatum_mem_usage;
92 target = c->xdatum_mem_usage * 4 / 5; /* 20% reduction */
93 for (count = 0; count < XATTRINDEX_HASHSIZE; count++) {
94 list_for_each_entry_safe(xd, _xd, &c->xattrindex[index], xindex) {
95 if (xd->flags & JFFS2_XFLAGS_HOT) {
96 xd->flags &= ~JFFS2_XFLAGS_HOT;
97 } else if (!(xd->flags & JFFS2_XFLAGS_BIND)) {
98 unload_xattr_datum(c, xd);
99 }
100 if (c->xdatum_mem_usage <= target)
101 goto out;
102 }
103 index = (index+1) % XATTRINDEX_HASHSIZE;
104 }
105 out:
106 JFFS2_NOTICE("xdatum_mem_usage from %u byte to %u byte (%u byte reclaimed)\n",
107 before, c->xdatum_mem_usage, before - c->xdatum_mem_usage);
108}
109
110static void delete_xattr_datum_node(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
111{
112 /* must be called under down_write(xattr_sem) */
113 struct jffs2_raw_xattr rx;
114 uint32_t length;
115 int rc;
116
117 if (!xd->node) {
118 JFFS2_WARNING("xdatum (xid=%u) is removed twice.\n", xd->xid);
119 return;
120 }
121 if (jffs2_sum_active()) {
122 memset(&rx, 0xff, sizeof(struct jffs2_raw_xattr));
123 rc = jffs2_flash_read(c, ref_offset(xd->node),
124 sizeof(struct jffs2_unknown_node),
125 &length, (char *)&rx);
126 if (rc || length != sizeof(struct jffs2_unknown_node)) {
127 JFFS2_ERROR("jffs2_flash_read()=%d, req=%u, read=%u at %#08x\n",
128 rc, sizeof(struct jffs2_unknown_node),
129 length, ref_offset(xd->node));
130 }
131 rc = jffs2_flash_write(c, ref_offset(xd->node), sizeof(rx),
132 &length, (char *)&rx);
133 if (rc || length != sizeof(struct jffs2_raw_xattr)) {
134 JFFS2_ERROR("jffs2_flash_write()=%d, req=%u, wrote=%u ar %#08x\n",
135 rc, sizeof(rx), length, ref_offset(xd->node));
136 }
137 }
138 spin_lock(&c->erase_completion_lock);
139 xd->node->next_in_ino = NULL;
140 spin_unlock(&c->erase_completion_lock);
141 jffs2_mark_node_obsolete(c, xd->node);
142 xd->node = NULL;
143}
144
145static void delete_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
146{
147 /* must be called under down_write(xattr_sem) */
148 BUG_ON(xd->refcnt);
149
150 unload_xattr_datum(c, xd);
151 if (xd->node) {
152 delete_xattr_datum_node(c, xd);
153 xd->node = NULL;
154 }
155 jffs2_free_xattr_datum(xd);
156}
157
158static int do_verify_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
159{
160 /* must be called under down_write(xattr_sem) */
161 struct jffs2_eraseblock *jeb;
162 struct jffs2_raw_xattr rx;
163 size_t readlen;
164 uint32_t crc, totlen;
165 int rc;
166
167 BUG_ON(!xd->node);
168 BUG_ON(ref_flags(xd->node) != REF_UNCHECKED);
169
170 rc = jffs2_flash_read(c, ref_offset(xd->node), sizeof(rx), &readlen, (char *)&rx);
171 if (rc || readlen != sizeof(rx)) {
172 JFFS2_WARNING("jffs2_flash_read()=%d, req=%u, read=%u at %#08x\n",
173 rc, sizeof(rx), readlen, ref_offset(xd->node));
174 return rc ? rc : -EIO;
175 }
176 crc = crc32(0, &rx, sizeof(rx) - 4);
177 if (crc != je32_to_cpu(rx.node_crc)) {
178 if (je32_to_cpu(rx.node_crc) != 0xffffffff)
179 JFFS2_ERROR("node CRC failed at %#08x, read=%#08x, calc=%#08x\n",
180 ref_offset(xd->node), je32_to_cpu(rx.hdr_crc), crc);
181 return EIO;
182 }
183 totlen = PAD(sizeof(rx) + rx.name_len + 1 + je16_to_cpu(rx.value_len));
184 if (je16_to_cpu(rx.magic) != JFFS2_MAGIC_BITMASK
185 || je16_to_cpu(rx.nodetype) != JFFS2_NODETYPE_XATTR
186 || je32_to_cpu(rx.totlen) != totlen
187 || je32_to_cpu(rx.xid) != xd->xid
188 || je32_to_cpu(rx.version) != xd->version) {
189 JFFS2_ERROR("inconsistent xdatum at %#08x, magic=%#04x/%#04x, "
190 "nodetype=%#04x/%#04x, totlen=%u/%u, xid=%u/%u, version=%u/%u\n",
191 ref_offset(xd->node), je16_to_cpu(rx.magic), JFFS2_MAGIC_BITMASK,
192 je16_to_cpu(rx.nodetype), JFFS2_NODETYPE_XATTR,
193 je32_to_cpu(rx.totlen), totlen,
194 je32_to_cpu(rx.xid), xd->xid,
195 je32_to_cpu(rx.version), xd->version);
196 return EIO;
197 }
198 xd->xprefix = rx.xprefix;
199 xd->name_len = rx.name_len;
200 xd->value_len = je16_to_cpu(rx.value_len);
201 xd->data_crc = je32_to_cpu(rx.data_crc);
202
203 /* This JFFS2_NODETYPE_XATTR node is checked */
204 jeb = &c->blocks[ref_offset(xd->node) / c->sector_size];
205 totlen = PAD(je32_to_cpu(rx.totlen));
206
207 spin_lock(&c->erase_completion_lock);
208 c->unchecked_size -= totlen; c->used_size += totlen;
209 jeb->unchecked_size -= totlen; jeb->used_size += totlen;
210 xd->node->flash_offset = ref_offset(xd->node) | REF_PRISTINE;
211 spin_unlock(&c->erase_completion_lock);
212
213 /* unchecked xdatum is chained with c->xattr_unchecked */
214 list_del_init(&xd->xindex);
215
216 dbg_xattr("success on verfying xdatum (xid=%u, version=%u)\n",
217 xd->xid, xd->version);
218
219 return 0;
220}
221
222static int do_load_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
223{
224 /* must be called under down_write(xattr_sem) */
225 char *data;
226 size_t readlen;
227 uint32_t crc, length;
228 int i, ret, retry = 0;
229
230 BUG_ON(!xd->node);
231 BUG_ON(ref_flags(xd->node) != REF_PRISTINE);
232 BUG_ON(!list_empty(&xd->xindex));
233 retry:
234 length = xd->name_len + 1 + xd->value_len;
235 data = kmalloc(length, GFP_KERNEL);
236 if (!data)
237 return -ENOMEM;
238
239 ret = jffs2_flash_read(c, ref_offset(xd->node)+sizeof(struct jffs2_raw_xattr),
240 length, &readlen, data);
241
242 if (ret || length!=readlen) {
243 JFFS2_WARNING("jffs2_flash_read() returned %d, request=%d, readlen=%d, at %#08x\n",
244 ret, length, readlen, ref_offset(xd->node));
245 kfree(data);
246 return ret ? ret : -EIO;
247 }
248
249 data[xd->name_len] = '\0';
250 crc = crc32(0, data, length);
251 if (crc != xd->data_crc) {
252 JFFS2_WARNING("node CRC failed (JFFS2_NODETYPE_XREF)"
253 " at %#08x, read: 0x%08x calculated: 0x%08x\n",
254 ref_offset(xd->node), xd->data_crc, crc);
255 kfree(data);
256 return EIO;
257 }
258
259 xd->flags |= JFFS2_XFLAGS_HOT;
260 xd->xname = data;
261 xd->xvalue = data + xd->name_len+1;
262
263 c->xdatum_mem_usage += length;
264
265 xd->hashkey = xattr_datum_hashkey(xd->xprefix, xd->xname, xd->xvalue, xd->value_len);
266 i = xd->hashkey % XATTRINDEX_HASHSIZE;
267 list_add(&xd->xindex, &c->xattrindex[i]);
268 if (!retry) {
269 retry = 1;
270 reclaim_xattr_datum(c);
271 if (!xd->xname)
272 goto retry;
273 }
274
275 dbg_xattr("success on loading xdatum (xid=%u, xprefix=%u, xname='%s')\n",
276 xd->xid, xd->xprefix, xd->xname);
277
278 return 0;
279}
280
281static int load_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
282{
283 /* must be called under down_write(xattr_sem);
284 * rc < 0 : recoverable error, try again
285 * rc = 0 : success
286 * rc > 0 : Unrecoverable error, this node should be deleted.
287 */
288 int rc = 0;
289 BUG_ON(xd->xname);
290 if (!xd->node)
291 return EIO;
292 if (unlikely(ref_flags(xd->node) != REF_PRISTINE)) {
293 rc = do_verify_xattr_datum(c, xd);
294 if (rc > 0) {
295 list_del_init(&xd->xindex);
296 delete_xattr_datum_node(c, xd);
297 }
298 }
299 if (!rc)
300 rc = do_load_xattr_datum(c, xd);
301 return rc;
302}
303
304static int save_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd, uint32_t phys_ofs)
305{
306 /* must be called under down_write(xattr_sem) */
307 struct jffs2_raw_xattr rx;
308 struct jffs2_raw_node_ref *raw;
309 struct kvec vecs[2];
310 uint32_t length;
311 int rc, totlen;
312
313 BUG_ON(!xd->xname);
314
315 vecs[0].iov_base = &rx;
316 vecs[0].iov_len = PAD(sizeof(rx));
317 vecs[1].iov_base = xd->xname;
318 vecs[1].iov_len = xd->name_len + 1 + xd->value_len;
319 totlen = vecs[0].iov_len + vecs[1].iov_len;
320
321 raw = jffs2_alloc_raw_node_ref();
322 if (!raw)
323 return -ENOMEM;
324 raw->flash_offset = phys_ofs;
325 raw->__totlen = PAD(totlen);
326 raw->next_phys = NULL;
327 raw->next_in_ino = (void *)xd;
328
329 /* Setup raw-xattr */
330 rx.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
331 rx.nodetype = cpu_to_je16(JFFS2_NODETYPE_XATTR);
332 rx.totlen = cpu_to_je32(PAD(totlen));
333 rx.hdr_crc = cpu_to_je32(crc32(0, &rx, sizeof(struct jffs2_unknown_node) - 4));
334
335 rx.xid = cpu_to_je32(xd->xid);
336 rx.version = cpu_to_je32(++xd->version);
337 rx.xprefix = xd->xprefix;
338 rx.name_len = xd->name_len;
339 rx.value_len = cpu_to_je16(xd->value_len);
340 rx.data_crc = cpu_to_je32(crc32(0, vecs[1].iov_base, vecs[1].iov_len));
341 rx.node_crc = cpu_to_je32(crc32(0, &rx, sizeof(struct jffs2_raw_xattr) - 4));
342
343 rc = jffs2_flash_writev(c, vecs, 2, phys_ofs, &length, 0);
344 if (rc || totlen != length) {
345 JFFS2_WARNING("jffs2_flash_writev()=%d, req=%u, wrote=%u, at %#08x\n",
346 rc, totlen, length, phys_ofs);
347 rc = rc ? rc : -EIO;
348 if (length) {
349 raw->flash_offset |= REF_OBSOLETE;
350 raw->next_in_ino = NULL;
351 jffs2_add_physical_node_ref(c, raw);
352 jffs2_mark_node_obsolete(c, raw);
353 } else {
354 jffs2_free_raw_node_ref(raw);
355 }
356 return rc;
357 }
358 BUG_ON(raw->__totlen < sizeof(struct jffs2_raw_xattr));
359 /* success */
360 raw->flash_offset |= REF_PRISTINE;
361 jffs2_add_physical_node_ref(c, raw);
362 if (xd->node)
363 delete_xattr_datum_node(c, xd);
364 xd->node = raw;
365
366 dbg_xattr("success on saving xdatum (xid=%u, version=%u, xprefix=%u, xname='%s')\n",
367 xd->xid, xd->version, xd->xprefix, xd->xname);
368
369 return 0;
370}
371
372static struct jffs2_xattr_datum *create_xattr_datum(struct jffs2_sb_info *c,
373 int xprefix, const char *xname,
374 const char *xvalue, int xsize,
375 uint32_t phys_ofs)
376{
377 /* must be called under down_write(xattr_sem) */
378 struct jffs2_xattr_datum *xd;
379 uint32_t hashkey, name_len;
380 char *data;
381 int i, rc;
382
383 /* Search xattr_datum has same xname/xvalue by index */
384 hashkey = xattr_datum_hashkey(xprefix, xname, xvalue, xsize);
385 i = hashkey % XATTRINDEX_HASHSIZE;
386 list_for_each_entry(xd, &c->xattrindex[i], xindex) {
387 if (xd->hashkey==hashkey
388 && xd->xprefix==xprefix
389 && xd->value_len==xsize
390 && !strcmp(xd->xname, xname)
391 && !memcmp(xd->xvalue, xvalue, xsize)) {
392 xd->refcnt++;
393 return xd;
394 }
395 }
396
397 /* Not found, Create NEW XATTR-Cache */
398 name_len = strlen(xname);
399
400 xd = jffs2_alloc_xattr_datum();
401 if (!xd)
402 return ERR_PTR(-ENOMEM);
403
404 data = kmalloc(name_len + 1 + xsize, GFP_KERNEL);
405 if (!data) {
406 jffs2_free_xattr_datum(xd);
407 return ERR_PTR(-ENOMEM);
408 }
409 strcpy(data, xname);
410 memcpy(data + name_len + 1, xvalue, xsize);
411
412 xd->refcnt = 1;
413 xd->xid = ++c->highest_xid;
414 xd->flags |= JFFS2_XFLAGS_HOT;
415 xd->xprefix = xprefix;
416
417 xd->hashkey = hashkey;
418 xd->xname = data;
419 xd->xvalue = data + name_len + 1;
420 xd->name_len = name_len;
421 xd->value_len = xsize;
422 xd->data_crc = crc32(0, data, xd->name_len + 1 + xd->value_len);
423
424 rc = save_xattr_datum(c, xd, phys_ofs);
425 if (rc) {
426 kfree(xd->xname);
427 jffs2_free_xattr_datum(xd);
428 return ERR_PTR(rc);
429 }
430
431 /* Insert Hash Index */
432 i = hashkey % XATTRINDEX_HASHSIZE;
433 list_add(&xd->xindex, &c->xattrindex[i]);
434
435 c->xdatum_mem_usage += (xd->name_len + 1 + xd->value_len);
436 reclaim_xattr_datum(c);
437
438 return xd;
439}
440
441/* -------- xdatum related functions ----------------
442 * verify_xattr_ref(c, ref)
443 * is used to load xref information from medium. Because summary data does not
444 * contain xid/ino, it's necessary to verify once while mounting process.
445 * delete_xattr_ref_node(c, ref)
446 * is used to delete a jffs2 node is dominated by xref. When EBS is enabled,
447 * it overwrites the obsolete node by myself.
448 * delete_xattr_ref(c, ref)
449 * is used to delete jffs2_xattr_ref object. If the reference counter of xdatum
450 * is refered by this xref become 0, delete_xattr_datum() is called later.
451 * save_xattr_ref(c, ref, phys_ofs)
452 * is used to write xref to medium.
453 * create_xattr_ref(c, ic, xd, phys_ofs)
454 * is used to create a new xref and write to medium.
455 * jffs2_xattr_delete_inode(c, ic)
456 * is called to remove xrefs related to obsolete inode when inode is unlinked.
457 * jffs2_xattr_free_inode(c, ic)
458 * is called to release xattr related objects when unmounting.
8f2b6f49 459 * check_xattr_ref_inode(c, ic)
aa98d7cf
KK
460 * is used to confirm inode does not have duplicate xattr name/value pair.
461 * -------------------------------------------------- */
462static int verify_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
463{
464 struct jffs2_eraseblock *jeb;
465 struct jffs2_raw_xref rr;
466 size_t readlen;
467 uint32_t crc, totlen;
468 int rc;
469
470 BUG_ON(ref_flags(ref->node) != REF_UNCHECKED);
471
472 rc = jffs2_flash_read(c, ref_offset(ref->node), sizeof(rr), &readlen, (char *)&rr);
473 if (rc || sizeof(rr) != readlen) {
474 JFFS2_WARNING("jffs2_flash_read()=%d, req=%u, read=%u, at %#08x\n",
475 rc, sizeof(rr), readlen, ref_offset(ref->node));
476 return rc ? rc : -EIO;
477 }
478 /* obsolete node */
479 crc = crc32(0, &rr, sizeof(rr) - 4);
480 if (crc != je32_to_cpu(rr.node_crc)) {
481 if (je32_to_cpu(rr.node_crc) != 0xffffffff)
482 JFFS2_ERROR("node CRC failed at %#08x, read=%#08x, calc=%#08x\n",
483 ref_offset(ref->node), je32_to_cpu(rr.node_crc), crc);
484 return EIO;
485 }
486 if (je16_to_cpu(rr.magic) != JFFS2_MAGIC_BITMASK
487 || je16_to_cpu(rr.nodetype) != JFFS2_NODETYPE_XREF
488 || je32_to_cpu(rr.totlen) != PAD(sizeof(rr))) {
489 JFFS2_ERROR("inconsistent xref at %#08x, magic=%#04x/%#04x, "
490 "nodetype=%#04x/%#04x, totlen=%u/%u\n",
491 ref_offset(ref->node), je16_to_cpu(rr.magic), JFFS2_MAGIC_BITMASK,
492 je16_to_cpu(rr.nodetype), JFFS2_NODETYPE_XREF,
493 je32_to_cpu(rr.totlen), PAD(sizeof(rr)));
494 return EIO;
495 }
496 ref->ino = je32_to_cpu(rr.ino);
497 ref->xid = je32_to_cpu(rr.xid);
498
499 /* fixup superblock/eraseblock info */
500 jeb = &c->blocks[ref_offset(ref->node) / c->sector_size];
501 totlen = PAD(sizeof(rr));
502
503 spin_lock(&c->erase_completion_lock);
504 c->unchecked_size -= totlen; c->used_size += totlen;
505 jeb->unchecked_size -= totlen; jeb->used_size += totlen;
506 ref->node->flash_offset = ref_offset(ref->node) | REF_PRISTINE;
507 spin_unlock(&c->erase_completion_lock);
508
509 dbg_xattr("success on verifying xref (ino=%u, xid=%u) at %#08x\n",
510 ref->ino, ref->xid, ref_offset(ref->node));
511 return 0;
512}
513
514static void delete_xattr_ref_node(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
515{
516 struct jffs2_raw_xref rr;
517 uint32_t length;
518 int rc;
519
520 if (jffs2_sum_active()) {
521 memset(&rr, 0xff, sizeof(rr));
522 rc = jffs2_flash_read(c, ref_offset(ref->node),
523 sizeof(struct jffs2_unknown_node),
524 &length, (char *)&rr);
525 if (rc || length != sizeof(struct jffs2_unknown_node)) {
526 JFFS2_ERROR("jffs2_flash_read()=%d, req=%u, read=%u at %#08x\n",
527 rc, sizeof(struct jffs2_unknown_node),
528 length, ref_offset(ref->node));
529 }
530 rc = jffs2_flash_write(c, ref_offset(ref->node), sizeof(rr),
531 &length, (char *)&rr);
532 if (rc || length != sizeof(struct jffs2_raw_xref)) {
533 JFFS2_ERROR("jffs2_flash_write()=%d, req=%u, wrote=%u at %#08x\n",
534 rc, sizeof(rr), length, ref_offset(ref->node));
535 }
536 }
537 spin_lock(&c->erase_completion_lock);
538 ref->node->next_in_ino = NULL;
539 spin_unlock(&c->erase_completion_lock);
540 jffs2_mark_node_obsolete(c, ref->node);
541 ref->node = NULL;
542}
543
544static void delete_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
545{
546 /* must be called under down_write(xattr_sem) */
547 struct jffs2_xattr_datum *xd;
548
549 BUG_ON(!ref->node);
550 delete_xattr_ref_node(c, ref);
551
aa98d7cf
KK
552 xd = ref->xd;
553 xd->refcnt--;
554 if (!xd->refcnt)
555 delete_xattr_datum(c, xd);
556 jffs2_free_xattr_ref(ref);
557}
558
559static int save_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref, uint32_t phys_ofs)
560{
561 /* must be called under down_write(xattr_sem) */
562 struct jffs2_raw_node_ref *raw;
563 struct jffs2_raw_xref rr;
564 uint32_t length;
565 int ret;
566
567 raw = jffs2_alloc_raw_node_ref();
568 if (!raw)
569 return -ENOMEM;
570 raw->flash_offset = phys_ofs;
571 raw->__totlen = PAD(sizeof(rr));
572 raw->next_phys = NULL;
573 raw->next_in_ino = (void *)ref;
574
575 rr.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
576 rr.nodetype = cpu_to_je16(JFFS2_NODETYPE_XREF);
577 rr.totlen = cpu_to_je32(PAD(sizeof(rr)));
578 rr.hdr_crc = cpu_to_je32(crc32(0, &rr, sizeof(struct jffs2_unknown_node) - 4));
579
580 rr.ino = cpu_to_je32(ref->ic->ino);
581 rr.xid = cpu_to_je32(ref->xd->xid);
582 rr.node_crc = cpu_to_je32(crc32(0, &rr, sizeof(rr) - 4));
583
584 ret = jffs2_flash_write(c, phys_ofs, sizeof(rr), &length, (char *)&rr);
585 if (ret || sizeof(rr) != length) {
586 JFFS2_WARNING("jffs2_flash_write() returned %d, request=%u, retlen=%u, at %#08x\n",
587 ret, sizeof(rr), length, phys_ofs);
588 ret = ret ? ret : -EIO;
589 if (length) {
590 raw->flash_offset |= REF_OBSOLETE;
591 raw->next_in_ino = NULL;
592 jffs2_add_physical_node_ref(c, raw);
593 jffs2_mark_node_obsolete(c, raw);
594 } else {
595 jffs2_free_raw_node_ref(raw);
596 }
597 return ret;
598 }
599 raw->flash_offset |= REF_PRISTINE;
600
601 jffs2_add_physical_node_ref(c, raw);
602 if (ref->node)
603 delete_xattr_ref_node(c, ref);
604 ref->node = raw;
605
606 dbg_xattr("success on saving xref (ino=%u, xid=%u)\n", ref->ic->ino, ref->xd->xid);
607
608 return 0;
609}
610
611static struct jffs2_xattr_ref *create_xattr_ref(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic,
612 struct jffs2_xattr_datum *xd, uint32_t phys_ofs)
613{
614 /* must be called under down_write(xattr_sem) */
615 struct jffs2_xattr_ref *ref;
616 int ret;
617
618 ref = jffs2_alloc_xattr_ref();
619 if (!ref)
620 return ERR_PTR(-ENOMEM);
621 ref->ic = ic;
622 ref->xd = xd;
623
624 ret = save_xattr_ref(c, ref, phys_ofs);
625 if (ret) {
626 jffs2_free_xattr_ref(ref);
627 return ERR_PTR(ret);
628 }
629
630 /* Chain to inode */
8f2b6f49
KK
631 ref->next = ic->xref;
632 ic->xref = ref;
aa98d7cf
KK
633
634 return ref; /* success */
635}
636
637void jffs2_xattr_delete_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
638{
639 /* It's called from jffs2_clear_inode() on inode removing.
640 When an inode with XATTR is removed, those XATTRs must be removed. */
641 struct jffs2_xattr_ref *ref, *_ref;
642
643 if (!ic || ic->nlink > 0)
644 return;
645
646 down_write(&c->xattr_sem);
8f2b6f49
KK
647 for (ref = ic->xref; ref; ref = _ref) {
648 _ref = ref->next;
aa98d7cf 649 delete_xattr_ref(c, ref);
8f2b6f49
KK
650 }
651 ic->xref = NULL;
aa98d7cf
KK
652 up_write(&c->xattr_sem);
653}
654
655void jffs2_xattr_free_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
656{
657 /* It's called from jffs2_free_ino_caches() until unmounting FS. */
658 struct jffs2_xattr_datum *xd;
659 struct jffs2_xattr_ref *ref, *_ref;
660
661 down_write(&c->xattr_sem);
8f2b6f49
KK
662 for (ref = ic->xref; ref; ref = _ref) {
663 _ref = ref->next;
aa98d7cf
KK
664 xd = ref->xd;
665 xd->refcnt--;
666 if (!xd->refcnt) {
667 unload_xattr_datum(c, xd);
668 jffs2_free_xattr_datum(xd);
669 }
670 jffs2_free_xattr_ref(ref);
671 }
8f2b6f49 672 ic->xref = NULL;
aa98d7cf
KK
673 up_write(&c->xattr_sem);
674}
675
8f2b6f49 676static int check_xattr_ref_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
aa98d7cf 677{
8f2b6f49 678 /* success of check_xattr_ref_inode() means taht inode (ic) dose not have
aa98d7cf
KK
679 * duplicate name/value pairs. If duplicate name/value pair would be found,
680 * one will be removed.
681 */
8f2b6f49 682 struct jffs2_xattr_ref *ref, *cmp, **pref;
aa98d7cf
KK
683 int rc = 0;
684
685 if (likely(ic->flags & INO_FLAGS_XATTR_CHECKED))
686 return 0;
687 down_write(&c->xattr_sem);
688 retry:
689 rc = 0;
8f2b6f49 690 for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
aa98d7cf
KK
691 if (!ref->xd->xname) {
692 rc = load_xattr_datum(c, ref->xd);
693 if (unlikely(rc > 0)) {
8f2b6f49 694 *pref = ref->next;
aa98d7cf
KK
695 delete_xattr_ref(c, ref);
696 goto retry;
697 } else if (unlikely(rc < 0))
698 goto out;
699 }
8f2b6f49 700 for (cmp=ref->next, pref=&ref->next; cmp; pref=&cmp->next, cmp=cmp->next) {
aa98d7cf
KK
701 if (!cmp->xd->xname) {
702 ref->xd->flags |= JFFS2_XFLAGS_BIND;
703 rc = load_xattr_datum(c, cmp->xd);
704 ref->xd->flags &= ~JFFS2_XFLAGS_BIND;
705 if (unlikely(rc > 0)) {
8f2b6f49 706 *pref = cmp->next;
aa98d7cf
KK
707 delete_xattr_ref(c, cmp);
708 goto retry;
709 } else if (unlikely(rc < 0))
710 goto out;
711 }
712 if (ref->xd->xprefix == cmp->xd->xprefix
713 && !strcmp(ref->xd->xname, cmp->xd->xname)) {
8f2b6f49 714 *pref = cmp->next;
aa98d7cf
KK
715 delete_xattr_ref(c, cmp);
716 goto retry;
717 }
718 }
719 }
720 ic->flags |= INO_FLAGS_XATTR_CHECKED;
721 out:
722 up_write(&c->xattr_sem);
723
724 return rc;
725}
726
727/* -------- xattr subsystem functions ---------------
728 * jffs2_init_xattr_subsystem(c)
729 * is used to initialize semaphore and list_head, and some variables.
730 * jffs2_find_xattr_datum(c, xid)
731 * is used to lookup xdatum while scanning process.
732 * jffs2_clear_xattr_subsystem(c)
733 * is used to release any xattr related objects.
734 * jffs2_build_xattr_subsystem(c)
735 * is used to associate xdatum and xref while super block building process.
736 * jffs2_setup_xattr_datum(c, xid, version)
737 * is used to insert xdatum while scanning process.
738 * -------------------------------------------------- */
739void jffs2_init_xattr_subsystem(struct jffs2_sb_info *c)
740{
741 int i;
742
743 for (i=0; i < XATTRINDEX_HASHSIZE; i++)
744 INIT_LIST_HEAD(&c->xattrindex[i]);
aa98d7cf 745 INIT_LIST_HEAD(&c->xattr_unchecked);
8f2b6f49 746 c->xref_temp = NULL;
aa98d7cf
KK
747
748 init_rwsem(&c->xattr_sem);
749 c->xdatum_mem_usage = 0;
750 c->xdatum_mem_threshold = 32 * 1024; /* Default 32KB */
751}
752
753static struct jffs2_xattr_datum *jffs2_find_xattr_datum(struct jffs2_sb_info *c, uint32_t xid)
754{
755 struct jffs2_xattr_datum *xd;
756 int i = xid % XATTRINDEX_HASHSIZE;
757
758 /* It's only used in scanning/building process. */
759 BUG_ON(!(c->flags & (JFFS2_SB_FLAG_SCANNING|JFFS2_SB_FLAG_BUILDING)));
760
761 list_for_each_entry(xd, &c->xattrindex[i], xindex) {
762 if (xd->xid==xid)
763 return xd;
764 }
765 return NULL;
766}
767
768void jffs2_clear_xattr_subsystem(struct jffs2_sb_info *c)
769{
770 struct jffs2_xattr_datum *xd, *_xd;
771 struct jffs2_xattr_ref *ref, *_ref;
772 int i;
773
8f2b6f49
KK
774 for (ref=c->xref_temp; ref; ref = _ref) {
775 _ref = ref->next;
aa98d7cf 776 jffs2_free_xattr_ref(ref);
8f2b6f49
KK
777 }
778 c->xref_temp = NULL;
aa98d7cf
KK
779
780 for (i=0; i < XATTRINDEX_HASHSIZE; i++) {
781 list_for_each_entry_safe(xd, _xd, &c->xattrindex[i], xindex) {
782 list_del(&xd->xindex);
783 if (xd->xname)
784 kfree(xd->xname);
785 jffs2_free_xattr_datum(xd);
786 }
787 }
788}
789
790void jffs2_build_xattr_subsystem(struct jffs2_sb_info *c)
791{
792 struct jffs2_xattr_ref *ref, *_ref;
793 struct jffs2_xattr_datum *xd, *_xd;
794 struct jffs2_inode_cache *ic;
795 int i, xdatum_count =0, xdatum_unchecked_count = 0, xref_count = 0;
796
797 BUG_ON(!(c->flags & JFFS2_SB_FLAG_BUILDING));
798
799 /* Phase.1 */
8f2b6f49
KK
800 for (ref=c->xref_temp; ref; ref=_ref) {
801 _ref = ref->next;
aa98d7cf
KK
802 /* checking REF_UNCHECKED nodes */
803 if (ref_flags(ref->node) != REF_PRISTINE) {
804 if (verify_xattr_ref(c, ref)) {
805 delete_xattr_ref_node(c, ref);
806 jffs2_free_xattr_ref(ref);
807 continue;
808 }
809 }
810 /* At this point, ref->xid and ref->ino contain XID and inode number.
811 ref->xd and ref->ic are not valid yet. */
812 xd = jffs2_find_xattr_datum(c, ref->xid);
813 ic = jffs2_get_ino_cache(c, ref->ino);
814 if (!xd || !ic) {
815 if (ref_flags(ref->node) != REF_UNCHECKED)
816 JFFS2_WARNING("xref(ino=%u, xid=%u) is orphan. \n",
817 ref->ino, ref->xid);
818 delete_xattr_ref_node(c, ref);
819 jffs2_free_xattr_ref(ref);
820 continue;
821 }
822 ref->xd = xd;
823 ref->ic = ic;
824 xd->refcnt++;
8f2b6f49
KK
825 ref->next = ic->xref;
826 ic->xref = ref;
aa98d7cf
KK
827 xref_count++;
828 }
8f2b6f49 829 c->xref_temp = NULL;
aa98d7cf
KK
830 /* After this, ref->xid/ino are NEVER used. */
831
832 /* Phase.2 */
833 for (i=0; i < XATTRINDEX_HASHSIZE; i++) {
834 list_for_each_entry_safe(xd, _xd, &c->xattrindex[i], xindex) {
835 list_del_init(&xd->xindex);
836 if (!xd->refcnt) {
837 if (ref_flags(xd->node) != REF_UNCHECKED)
838 JFFS2_WARNING("orphan xdatum(xid=%u, version=%u) at %#08x\n",
839 xd->xid, xd->version, ref_offset(xd->node));
840 delete_xattr_datum(c, xd);
841 continue;
842 }
843 if (ref_flags(xd->node) != REF_PRISTINE) {
844 dbg_xattr("unchecked xdatum(xid=%u) at %#08x\n",
845 xd->xid, ref_offset(xd->node));
846 list_add(&xd->xindex, &c->xattr_unchecked);
847 xdatum_unchecked_count++;
848 }
849 xdatum_count++;
850 }
851 }
852 /* build complete */
853 JFFS2_NOTICE("complete building xattr subsystem, %u of xdatum (%u unchecked) and "
854 "%u of xref found.\n", xdatum_count, xdatum_unchecked_count, xref_count);
855}
856
857struct jffs2_xattr_datum *jffs2_setup_xattr_datum(struct jffs2_sb_info *c,
858 uint32_t xid, uint32_t version)
859{
860 struct jffs2_xattr_datum *xd, *_xd;
861
862 _xd = jffs2_find_xattr_datum(c, xid);
863 if (_xd) {
864 dbg_xattr("duplicate xdatum (xid=%u, version=%u/%u) at %#08x\n",
865 xid, version, _xd->version, ref_offset(_xd->node));
866 if (version < _xd->version)
867 return ERR_PTR(-EEXIST);
868 }
869 xd = jffs2_alloc_xattr_datum();
870 if (!xd)
871 return ERR_PTR(-ENOMEM);
872 xd->xid = xid;
873 xd->version = version;
874 if (xd->xid > c->highest_xid)
875 c->highest_xid = xd->xid;
876 list_add_tail(&xd->xindex, &c->xattrindex[xid % XATTRINDEX_HASHSIZE]);
877
878 if (_xd) {
879 list_del_init(&_xd->xindex);
880 delete_xattr_datum_node(c, _xd);
881 jffs2_free_xattr_datum(_xd);
882 }
883 return xd;
884}
885
886/* -------- xattr subsystem functions ---------------
887 * xprefix_to_handler(xprefix)
888 * is used to translate xprefix into xattr_handler.
889 * jffs2_listxattr(dentry, buffer, size)
890 * is an implementation of listxattr handler on jffs2.
891 * do_jffs2_getxattr(inode, xprefix, xname, buffer, size)
892 * is an implementation of getxattr handler on jffs2.
893 * do_jffs2_setxattr(inode, xprefix, xname, buffer, size, flags)
894 * is an implementation of setxattr handler on jffs2.
895 * -------------------------------------------------- */
896struct xattr_handler *jffs2_xattr_handlers[] = {
897 &jffs2_user_xattr_handler,
898#ifdef CONFIG_JFFS2_FS_SECURITY
899 &jffs2_security_xattr_handler,
900#endif
901#ifdef CONFIG_JFFS2_FS_POSIX_ACL
902 &jffs2_acl_access_xattr_handler,
903 &jffs2_acl_default_xattr_handler,
904#endif
905 &jffs2_trusted_xattr_handler,
906 NULL
907};
908
909static struct xattr_handler *xprefix_to_handler(int xprefix) {
910 struct xattr_handler *ret;
911
912 switch (xprefix) {
913 case JFFS2_XPREFIX_USER:
914 ret = &jffs2_user_xattr_handler;
915 break;
916#ifdef CONFIG_JFFS2_FS_SECURITY
917 case JFFS2_XPREFIX_SECURITY:
918 ret = &jffs2_security_xattr_handler;
919 break;
920#endif
921#ifdef CONFIG_JFFS2_FS_POSIX_ACL
922 case JFFS2_XPREFIX_ACL_ACCESS:
923 ret = &jffs2_acl_access_xattr_handler;
924 break;
925 case JFFS2_XPREFIX_ACL_DEFAULT:
926 ret = &jffs2_acl_default_xattr_handler;
927 break;
928#endif
929 case JFFS2_XPREFIX_TRUSTED:
930 ret = &jffs2_trusted_xattr_handler;
931 break;
932 default:
933 ret = NULL;
934 break;
935 }
936 return ret;
937}
938
939ssize_t jffs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
940{
941 struct inode *inode = dentry->d_inode;
942 struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
943 struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
944 struct jffs2_inode_cache *ic = f->inocache;
8f2b6f49 945 struct jffs2_xattr_ref *ref, **pref;
aa98d7cf
KK
946 struct jffs2_xattr_datum *xd;
947 struct xattr_handler *xhandle;
948 ssize_t len, rc;
949 int retry = 0;
950
8f2b6f49 951 rc = check_xattr_ref_inode(c, ic);
aa98d7cf
KK
952 if (unlikely(rc))
953 return rc;
954
955 down_read(&c->xattr_sem);
956 retry:
957 len = 0;
8f2b6f49 958 for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
aa98d7cf
KK
959 BUG_ON(ref->ic != ic);
960 xd = ref->xd;
961 if (!xd->xname) {
962 /* xdatum is unchached */
963 if (!retry) {
964 retry = 1;
965 up_read(&c->xattr_sem);
966 down_write(&c->xattr_sem);
967 goto retry;
968 } else {
969 rc = load_xattr_datum(c, xd);
970 if (unlikely(rc > 0)) {
8f2b6f49 971 *pref = ref->next;
aa98d7cf
KK
972 delete_xattr_ref(c, ref);
973 goto retry;
974 } else if (unlikely(rc < 0))
975 goto out;
976 }
977 }
978 xhandle = xprefix_to_handler(xd->xprefix);
979 if (!xhandle)
980 continue;
981 if (buffer) {
982 rc = xhandle->list(inode, buffer+len, size-len, xd->xname, xd->name_len);
983 } else {
984 rc = xhandle->list(inode, NULL, 0, xd->xname, xd->name_len);
985 }
986 if (rc < 0)
987 goto out;
988 len += rc;
989 }
990 rc = len;
991 out:
992 if (!retry) {
993 up_read(&c->xattr_sem);
994 } else {
995 up_write(&c->xattr_sem);
996 }
997 return rc;
998}
999
1000int do_jffs2_getxattr(struct inode *inode, int xprefix, const char *xname,
1001 char *buffer, size_t size)
1002{
1003 struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
1004 struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
1005 struct jffs2_inode_cache *ic = f->inocache;
1006 struct jffs2_xattr_datum *xd;
8f2b6f49 1007 struct jffs2_xattr_ref *ref, **pref;
aa98d7cf
KK
1008 int rc, retry = 0;
1009
8f2b6f49 1010 rc = check_xattr_ref_inode(c, ic);
aa98d7cf
KK
1011 if (unlikely(rc))
1012 return rc;
1013
1014 down_read(&c->xattr_sem);
1015 retry:
8f2b6f49 1016 for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
aa98d7cf
KK
1017 BUG_ON(ref->ic!=ic);
1018
1019 xd = ref->xd;
1020 if (xd->xprefix != xprefix)
1021 continue;
1022 if (!xd->xname) {
1023 /* xdatum is unchached */
1024 if (!retry) {
1025 retry = 1;
1026 up_read(&c->xattr_sem);
1027 down_write(&c->xattr_sem);
1028 goto retry;
1029 } else {
1030 rc = load_xattr_datum(c, xd);
1031 if (unlikely(rc > 0)) {
8f2b6f49 1032 *pref = ref->next;
aa98d7cf
KK
1033 delete_xattr_ref(c, ref);
1034 goto retry;
1035 } else if (unlikely(rc < 0)) {
1036 goto out;
1037 }
1038 }
1039 }
1040 if (!strcmp(xname, xd->xname)) {
1041 rc = xd->value_len;
1042 if (buffer) {
1043 if (size < rc) {
1044 rc = -ERANGE;
1045 } else {
1046 memcpy(buffer, xd->xvalue, rc);
1047 }
1048 }
1049 goto out;
1050 }
1051 }
1052 rc = -ENODATA;
1053 out:
1054 if (!retry) {
1055 up_read(&c->xattr_sem);
1056 } else {
1057 up_write(&c->xattr_sem);
1058 }
1059 return rc;
1060}
1061
1062int do_jffs2_setxattr(struct inode *inode, int xprefix, const char *xname,
1063 const char *buffer, size_t size, int flags)
1064{
1065 struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
1066 struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
1067 struct jffs2_inode_cache *ic = f->inocache;
1068 struct jffs2_xattr_datum *xd;
8f2b6f49 1069 struct jffs2_xattr_ref *ref, *newref, **pref;
aa98d7cf
KK
1070 uint32_t phys_ofs, length, request;
1071 int rc;
1072
8f2b6f49 1073 rc = check_xattr_ref_inode(c, ic);
aa98d7cf
KK
1074 if (unlikely(rc))
1075 return rc;
1076
1077 request = PAD(sizeof(struct jffs2_raw_xattr) + strlen(xname) + 1 + size);
1078 rc = jffs2_reserve_space(c, request, &phys_ofs, &length,
1079 ALLOC_NORMAL, JFFS2_SUMMARY_XATTR_SIZE);
1080 if (rc) {
1081 JFFS2_WARNING("jffs2_reserve_space()=%d, request=%u\n", rc, request);
1082 return rc;
1083 }
1084
1085 /* Find existing xattr */
1086 down_write(&c->xattr_sem);
1087 retry:
8f2b6f49 1088 for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
aa98d7cf
KK
1089 xd = ref->xd;
1090 if (xd->xprefix != xprefix)
1091 continue;
1092 if (!xd->xname) {
1093 rc = load_xattr_datum(c, xd);
1094 if (unlikely(rc > 0)) {
8f2b6f49 1095 *pref = ref->next;
aa98d7cf
KK
1096 delete_xattr_ref(c, ref);
1097 goto retry;
1098 } else if (unlikely(rc < 0))
1099 goto out;
1100 }
1101 if (!strcmp(xd->xname, xname)) {
1102 if (flags & XATTR_CREATE) {
1103 rc = -EEXIST;
1104 goto out;
1105 }
1106 if (!buffer) {
8f2b6f49 1107 *pref = ref->next;
aa98d7cf
KK
1108 delete_xattr_ref(c, ref);
1109 rc = 0;
1110 goto out;
1111 }
1112 goto found;
1113 }
1114 }
1115 /* not found */
aa98d7cf
KK
1116 if (flags & XATTR_REPLACE) {
1117 rc = -ENODATA;
1118 goto out;
1119 }
1120 if (!buffer) {
1121 rc = -EINVAL;
1122 goto out;
1123 }
1124 found:
1125 xd = create_xattr_datum(c, xprefix, xname, buffer, size, phys_ofs);
1126 if (IS_ERR(xd)) {
1127 rc = PTR_ERR(xd);
1128 goto out;
1129 }
1130 up_write(&c->xattr_sem);
1131 jffs2_complete_reservation(c);
1132
1133 /* create xattr_ref */
1134 request = PAD(sizeof(struct jffs2_raw_xref));
1135 rc = jffs2_reserve_space(c, request, &phys_ofs, &length,
1136 ALLOC_NORMAL, JFFS2_SUMMARY_XREF_SIZE);
1137 if (rc) {
1138 JFFS2_WARNING("jffs2_reserve_space()=%d, request=%u\n", rc, request);
1139 down_write(&c->xattr_sem);
1140 xd->refcnt--;
1141 if (!xd->refcnt)
1142 delete_xattr_datum(c, xd);
1143 up_write(&c->xattr_sem);
1144 return rc;
1145 }
1146 down_write(&c->xattr_sem);
8f2b6f49
KK
1147 if (ref)
1148 *pref = ref->next;
aa98d7cf
KK
1149 newref = create_xattr_ref(c, ic, xd, phys_ofs);
1150 if (IS_ERR(newref)) {
8f2b6f49
KK
1151 if (ref) {
1152 ref->next = ic->xref;
1153 ic->xref = ref;
1154 }
aa98d7cf
KK
1155 rc = PTR_ERR(newref);
1156 xd->refcnt--;
1157 if (!xd->refcnt)
1158 delete_xattr_datum(c, xd);
1159 } else if (ref) {
aa98d7cf
KK
1160 delete_xattr_ref(c, ref);
1161 }
1162 out:
1163 up_write(&c->xattr_sem);
1164 jffs2_complete_reservation(c);
1165 return rc;
1166}
1167
1168/* -------- garbage collector functions -------------
1169 * jffs2_garbage_collect_xattr_datum(c, xd)
1170 * is used to move xdatum into new node.
1171 * jffs2_garbage_collect_xattr_ref(c, ref)
1172 * is used to move xref into new node.
aa98d7cf
KK
1173 * jffs2_verify_xattr(c)
1174 * is used to call do_verify_xattr_datum() before garbage collecting.
1175 * -------------------------------------------------- */
084702e0 1176int jffs2_garbage_collect_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
aa98d7cf 1177{
aa98d7cf 1178 uint32_t phys_ofs, totlen, length, old_ofs;
084702e0 1179 int rc = -EINVAL;
aa98d7cf 1180
084702e0 1181 down_write(&c->xattr_sem);
aa98d7cf
KK
1182 BUG_ON(!xd->node);
1183
1184 old_ofs = ref_offset(xd->node);
1185 totlen = ref_totlen(c, c->gcblock, xd->node);
1186 if (totlen < sizeof(struct jffs2_raw_xattr))
084702e0 1187 goto out;
aa98d7cf
KK
1188
1189 if (!xd->xname) {
1190 rc = load_xattr_datum(c, xd);
1191 if (unlikely(rc > 0)) {
1192 delete_xattr_datum_node(c, xd);
084702e0
KK
1193 rc = 0;
1194 goto out;
aa98d7cf 1195 } else if (unlikely(rc < 0))
084702e0 1196 goto out;
aa98d7cf
KK
1197 }
1198 rc = jffs2_reserve_space_gc(c, totlen, &phys_ofs, &length, JFFS2_SUMMARY_XATTR_SIZE);
1199 if (rc || length < totlen) {
1200 JFFS2_WARNING("jffs2_reserve_space()=%d, request=%u\n", rc, totlen);
084702e0
KK
1201 rc = rc ? rc : -EBADFD;
1202 goto out;
aa98d7cf
KK
1203 }
1204 rc = save_xattr_datum(c, xd, phys_ofs);
1205 if (!rc)
1206 dbg_xattr("xdatum (xid=%u, version=%u) GC'ed from %#08x to %08x\n",
1207 xd->xid, xd->version, old_ofs, ref_offset(xd->node));
084702e0
KK
1208 out:
1209 up_write(&c->xattr_sem);
aa98d7cf
KK
1210 return rc;
1211}
1212
1213
084702e0 1214int jffs2_garbage_collect_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
aa98d7cf 1215{
aa98d7cf 1216 uint32_t phys_ofs, totlen, length, old_ofs;
084702e0 1217 int rc = -EINVAL;
aa98d7cf 1218
084702e0 1219 down_write(&c->xattr_sem);
aa98d7cf
KK
1220 BUG_ON(!ref->node);
1221
1222 old_ofs = ref_offset(ref->node);
1223 totlen = ref_totlen(c, c->gcblock, ref->node);
1224 if (totlen != sizeof(struct jffs2_raw_xref))
084702e0
KK
1225 goto out;
1226
aa98d7cf
KK
1227 rc = jffs2_reserve_space_gc(c, totlen, &phys_ofs, &length, JFFS2_SUMMARY_XREF_SIZE);
1228 if (rc || length < totlen) {
1229 JFFS2_WARNING("%s: jffs2_reserve_space() = %d, request = %u\n",
1230 __FUNCTION__, rc, totlen);
084702e0
KK
1231 rc = rc ? rc : -EBADFD;
1232 goto out;
aa98d7cf
KK
1233 }
1234 rc = save_xattr_ref(c, ref, phys_ofs);
1235 if (!rc)
1236 dbg_xattr("xref (ino=%u, xid=%u) GC'ed from %#08x to %08x\n",
1237 ref->ic->ino, ref->xd->xid, old_ofs, ref_offset(ref->node));
084702e0
KK
1238 out:
1239 up_write(&c->xattr_sem);
aa98d7cf
KK
1240 return rc;
1241}
1242
aa98d7cf
KK
1243int jffs2_verify_xattr(struct jffs2_sb_info *c)
1244{
1245 struct jffs2_xattr_datum *xd, *_xd;
1246 int rc;
1247
1248 down_write(&c->xattr_sem);
1249 list_for_each_entry_safe(xd, _xd, &c->xattr_unchecked, xindex) {
1250 rc = do_verify_xattr_datum(c, xd);
1251 if (rc == 0) {
1252 list_del_init(&xd->xindex);
1253 break;
1254 } else if (rc > 0) {
1255 list_del_init(&xd->xindex);
1256 delete_xattr_datum_node(c, xd);
1257 }
1258 }
1259 up_write(&c->xattr_sem);
1260
1261 return list_empty(&c->xattr_unchecked) ? 1 : 0;
1262}
This page took 0.117927 seconds and 5 git commands to generate.