2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright (C) 2006 NEC Corporation
6 * Created by KaiGai Kohei <kaigai@ak.jp.nec.com>
8 * For licensing information, see the file 'LICENCE' in this directory.
11 #include <linux/kernel.h>
12 #include <linux/slab.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>
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 * -------------------------------------------------- */
58 static uint32_t xattr_datum_hashkey(int xprefix, const char *xname, const char *xvalue, int xsize)
60 int name_len = strlen(xname);
62 return crc32(xprefix, xname, name_len) ^ crc32(xprefix, xvalue, xsize);
65 static void unload_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
67 /* must be called under down_write(xattr_sem) */
68 D1(dbg_xattr("%s: xid=%u, version=%u\n", __FUNCTION__, xd->xid, xd->version));
70 c->xdatum_mem_usage -= (xd->name_len + 1 + xd->value_len);
74 list_del_init(&xd->xindex);
80 static void reclaim_xattr_datum(struct jffs2_sb_info *c)
82 /* must be called under down_write(xattr_sem) */
83 struct jffs2_xattr_datum *xd, *_xd;
84 uint32_t target, before;
88 if (c->xdatum_mem_threshold > c->xdatum_mem_usage)
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);
100 if (c->xdatum_mem_usage <= target)
103 index = (index+1) % XATTRINDEX_HASHSIZE;
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);
110 static void delete_xattr_datum_node(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
112 /* must be called under down_write(xattr_sem) */
113 struct jffs2_raw_xattr rx;
118 JFFS2_WARNING("xdatum (xid=%u) is removed twice.\n", xd->xid);
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));
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));
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);
145 static void delete_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
147 /* must be called under down_write(xattr_sem) */
150 unload_xattr_datum(c, xd);
152 delete_xattr_datum_node(c, xd);
155 jffs2_free_xattr_datum(xd);
158 static int do_verify_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
160 /* must be called under down_write(xattr_sem) */
161 struct jffs2_eraseblock *jeb;
162 struct jffs2_raw_xattr rx;
164 uint32_t crc, totlen;
168 BUG_ON(ref_flags(xd->node) != REF_UNCHECKED);
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;
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);
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);
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);
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));
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);
213 /* unchecked xdatum is chained with c->xattr_unchecked */
214 list_del_init(&xd->xindex);
216 dbg_xattr("success on verfying xdatum (xid=%u, version=%u)\n",
217 xd->xid, xd->version);
222 static int do_load_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
224 /* must be called under down_write(xattr_sem) */
227 uint32_t crc, length;
228 int i, ret, retry = 0;
231 BUG_ON(ref_flags(xd->node) != REF_PRISTINE);
232 BUG_ON(!list_empty(&xd->xindex));
234 length = xd->name_len + 1 + xd->value_len;
235 data = kmalloc(length, GFP_KERNEL);
239 ret = jffs2_flash_read(c, ref_offset(xd->node)+sizeof(struct jffs2_raw_xattr),
240 length, &readlen, data);
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));
246 return ret ? ret : -EIO;
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);
259 xd->flags |= JFFS2_XFLAGS_HOT;
261 xd->xvalue = data + xd->name_len+1;
263 c->xdatum_mem_usage += length;
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]);
270 reclaim_xattr_datum(c);
275 dbg_xattr("success on loading xdatum (xid=%u, xprefix=%u, xname='%s')\n",
276 xd->xid, xd->xprefix, xd->xname);
281 static int load_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
283 /* must be called under down_write(xattr_sem);
284 * rc < 0 : recoverable error, try again
286 * rc > 0 : Unrecoverable error, this node should be deleted.
292 if (unlikely(ref_flags(xd->node) != REF_PRISTINE)) {
293 rc = do_verify_xattr_datum(c, xd);
295 list_del_init(&xd->xindex);
296 delete_xattr_datum_node(c, xd);
300 rc = do_load_xattr_datum(c, xd);
304 static int save_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd, uint32_t phys_ofs)
306 /* must be called under down_write(xattr_sem) */
307 struct jffs2_raw_xattr rx;
308 struct jffs2_raw_node_ref *raw;
315 vecs[0].iov_base = ℞
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;
321 raw = jffs2_alloc_raw_node_ref();
324 raw->flash_offset = phys_ofs;
326 /* Setup raw-xattr */
327 rx.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
328 rx.nodetype = cpu_to_je16(JFFS2_NODETYPE_XATTR);
329 rx.totlen = cpu_to_je32(PAD(totlen));
330 rx.hdr_crc = cpu_to_je32(crc32(0, &rx, sizeof(struct jffs2_unknown_node) - 4));
332 rx.xid = cpu_to_je32(xd->xid);
333 rx.version = cpu_to_je32(++xd->version);
334 rx.xprefix = xd->xprefix;
335 rx.name_len = xd->name_len;
336 rx.value_len = cpu_to_je16(xd->value_len);
337 rx.data_crc = cpu_to_je32(crc32(0, vecs[1].iov_base, vecs[1].iov_len));
338 rx.node_crc = cpu_to_je32(crc32(0, &rx, sizeof(struct jffs2_raw_xattr) - 4));
340 rc = jffs2_flash_writev(c, vecs, 2, phys_ofs, &length, 0);
341 if (rc || totlen != length) {
342 JFFS2_WARNING("jffs2_flash_writev()=%d, req=%u, wrote=%u, at %#08x\n",
343 rc, totlen, length, phys_ofs);
346 raw->flash_offset |= REF_OBSOLETE;
347 jffs2_add_physical_node_ref(c, raw, PAD(totlen), NULL);
348 jffs2_mark_node_obsolete(c, raw);
350 jffs2_free_raw_node_ref(raw);
356 raw->flash_offset |= REF_PRISTINE;
357 jffs2_add_physical_node_ref(c, raw, PAD(totlen), NULL);
358 /* FIXME */ raw->next_in_ino = (void *)xd;
361 delete_xattr_datum_node(c, xd);
364 dbg_xattr("success on saving xdatum (xid=%u, version=%u, xprefix=%u, xname='%s')\n",
365 xd->xid, xd->version, xd->xprefix, xd->xname);
370 static struct jffs2_xattr_datum *create_xattr_datum(struct jffs2_sb_info *c,
371 int xprefix, const char *xname,
372 const char *xvalue, int xsize,
375 /* must be called under down_write(xattr_sem) */
376 struct jffs2_xattr_datum *xd;
377 uint32_t hashkey, name_len;
381 /* Search xattr_datum has same xname/xvalue by index */
382 hashkey = xattr_datum_hashkey(xprefix, xname, xvalue, xsize);
383 i = hashkey % XATTRINDEX_HASHSIZE;
384 list_for_each_entry(xd, &c->xattrindex[i], xindex) {
385 if (xd->hashkey==hashkey
386 && xd->xprefix==xprefix
387 && xd->value_len==xsize
388 && !strcmp(xd->xname, xname)
389 && !memcmp(xd->xvalue, xvalue, xsize)) {
395 /* Not found, Create NEW XATTR-Cache */
396 name_len = strlen(xname);
398 xd = jffs2_alloc_xattr_datum();
400 return ERR_PTR(-ENOMEM);
402 data = kmalloc(name_len + 1 + xsize, GFP_KERNEL);
404 jffs2_free_xattr_datum(xd);
405 return ERR_PTR(-ENOMEM);
408 memcpy(data + name_len + 1, xvalue, xsize);
411 xd->xid = ++c->highest_xid;
412 xd->flags |= JFFS2_XFLAGS_HOT;
413 xd->xprefix = xprefix;
415 xd->hashkey = hashkey;
417 xd->xvalue = data + name_len + 1;
418 xd->name_len = name_len;
419 xd->value_len = xsize;
420 xd->data_crc = crc32(0, data, xd->name_len + 1 + xd->value_len);
422 rc = save_xattr_datum(c, xd, phys_ofs);
425 jffs2_free_xattr_datum(xd);
429 /* Insert Hash Index */
430 i = hashkey % XATTRINDEX_HASHSIZE;
431 list_add(&xd->xindex, &c->xattrindex[i]);
433 c->xdatum_mem_usage += (xd->name_len + 1 + xd->value_len);
434 reclaim_xattr_datum(c);
439 /* -------- xref related functions ------------------
440 * verify_xattr_ref(c, ref)
441 * is used to load xref information from medium. Because summary data does not
442 * contain xid/ino, it's necessary to verify once while mounting process.
443 * delete_xattr_ref_node(c, ref)
444 * is used to delete a jffs2 node is dominated by xref. When EBS is enabled,
445 * it overwrites the obsolete node by myself.
446 * delete_xattr_ref(c, ref)
447 * is used to delete jffs2_xattr_ref object. If the reference counter of xdatum
448 * is refered by this xref become 0, delete_xattr_datum() is called later.
449 * save_xattr_ref(c, ref, phys_ofs)
450 * is used to write xref to medium.
451 * create_xattr_ref(c, ic, xd, phys_ofs)
452 * is used to create a new xref and write to medium.
453 * jffs2_xattr_delete_inode(c, ic)
454 * is called to remove xrefs related to obsolete inode when inode is unlinked.
455 * jffs2_xattr_free_inode(c, ic)
456 * is called to release xattr related objects when unmounting.
457 * check_xattr_ref_inode(c, ic)
458 * is used to confirm inode does not have duplicate xattr name/value pair.
459 * -------------------------------------------------- */
460 static int verify_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
462 struct jffs2_eraseblock *jeb;
463 struct jffs2_raw_xref rr;
465 uint32_t crc, totlen;
468 BUG_ON(ref_flags(ref->node) != REF_UNCHECKED);
470 rc = jffs2_flash_read(c, ref_offset(ref->node), sizeof(rr), &readlen, (char *)&rr);
471 if (rc || sizeof(rr) != readlen) {
472 JFFS2_WARNING("jffs2_flash_read()=%d, req=%u, read=%u, at %#08x\n",
473 rc, sizeof(rr), readlen, ref_offset(ref->node));
474 return rc ? rc : -EIO;
477 crc = crc32(0, &rr, sizeof(rr) - 4);
478 if (crc != je32_to_cpu(rr.node_crc)) {
479 if (je32_to_cpu(rr.node_crc) != 0xffffffff)
480 JFFS2_ERROR("node CRC failed at %#08x, read=%#08x, calc=%#08x\n",
481 ref_offset(ref->node), je32_to_cpu(rr.node_crc), crc);
484 if (je16_to_cpu(rr.magic) != JFFS2_MAGIC_BITMASK
485 || je16_to_cpu(rr.nodetype) != JFFS2_NODETYPE_XREF
486 || je32_to_cpu(rr.totlen) != PAD(sizeof(rr))) {
487 JFFS2_ERROR("inconsistent xref at %#08x, magic=%#04x/%#04x, "
488 "nodetype=%#04x/%#04x, totlen=%u/%u\n",
489 ref_offset(ref->node), je16_to_cpu(rr.magic), JFFS2_MAGIC_BITMASK,
490 je16_to_cpu(rr.nodetype), JFFS2_NODETYPE_XREF,
491 je32_to_cpu(rr.totlen), PAD(sizeof(rr)));
494 ref->ino = je32_to_cpu(rr.ino);
495 ref->xid = je32_to_cpu(rr.xid);
497 /* fixup superblock/eraseblock info */
498 jeb = &c->blocks[ref_offset(ref->node) / c->sector_size];
499 totlen = PAD(sizeof(rr));
501 spin_lock(&c->erase_completion_lock);
502 c->unchecked_size -= totlen; c->used_size += totlen;
503 jeb->unchecked_size -= totlen; jeb->used_size += totlen;
504 ref->node->flash_offset = ref_offset(ref->node) | REF_PRISTINE;
505 spin_unlock(&c->erase_completion_lock);
507 dbg_xattr("success on verifying xref (ino=%u, xid=%u) at %#08x\n",
508 ref->ino, ref->xid, ref_offset(ref->node));
512 static void delete_xattr_ref_node(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
514 struct jffs2_raw_xref rr;
518 if (jffs2_sum_active()) {
519 memset(&rr, 0xff, sizeof(rr));
520 rc = jffs2_flash_read(c, ref_offset(ref->node),
521 sizeof(struct jffs2_unknown_node),
522 &length, (char *)&rr);
523 if (rc || length != sizeof(struct jffs2_unknown_node)) {
524 JFFS2_ERROR("jffs2_flash_read()=%d, req=%u, read=%u at %#08x\n",
525 rc, sizeof(struct jffs2_unknown_node),
526 length, ref_offset(ref->node));
528 rc = jffs2_flash_write(c, ref_offset(ref->node), sizeof(rr),
529 &length, (char *)&rr);
530 if (rc || length != sizeof(struct jffs2_raw_xref)) {
531 JFFS2_ERROR("jffs2_flash_write()=%d, req=%u, wrote=%u at %#08x\n",
532 rc, sizeof(rr), length, ref_offset(ref->node));
535 spin_lock(&c->erase_completion_lock);
536 ref->node->next_in_ino = NULL;
537 spin_unlock(&c->erase_completion_lock);
538 jffs2_mark_node_obsolete(c, ref->node);
542 static void delete_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
544 /* must be called under down_write(xattr_sem) */
545 struct jffs2_xattr_datum *xd;
548 delete_xattr_ref_node(c, ref);
553 delete_xattr_datum(c, xd);
554 jffs2_free_xattr_ref(ref);
557 static int save_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref, uint32_t phys_ofs)
559 /* must be called under down_write(xattr_sem) */
560 struct jffs2_raw_node_ref *raw;
561 struct jffs2_raw_xref rr;
565 raw = jffs2_alloc_raw_node_ref();
568 raw->flash_offset = phys_ofs;
570 rr.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
571 rr.nodetype = cpu_to_je16(JFFS2_NODETYPE_XREF);
572 rr.totlen = cpu_to_je32(PAD(sizeof(rr)));
573 rr.hdr_crc = cpu_to_je32(crc32(0, &rr, sizeof(struct jffs2_unknown_node) - 4));
575 rr.ino = cpu_to_je32(ref->ic->ino);
576 rr.xid = cpu_to_je32(ref->xd->xid);
577 rr.node_crc = cpu_to_je32(crc32(0, &rr, sizeof(rr) - 4));
579 ret = jffs2_flash_write(c, phys_ofs, sizeof(rr), &length, (char *)&rr);
580 if (ret || sizeof(rr) != length) {
581 JFFS2_WARNING("jffs2_flash_write() returned %d, request=%u, retlen=%u, at %#08x\n",
582 ret, sizeof(rr), length, phys_ofs);
583 ret = ret ? ret : -EIO;
585 raw->flash_offset |= REF_OBSOLETE;
586 jffs2_add_physical_node_ref(c, raw, PAD(sizeof(rr)), NULL);
587 jffs2_mark_node_obsolete(c, raw);
589 jffs2_free_raw_node_ref(raw);
593 raw->flash_offset |= REF_PRISTINE;
595 jffs2_add_physical_node_ref(c, raw, PAD(sizeof(rr)), NULL);
596 /* FIXME */ raw->next_in_ino = (void *)ref;
598 delete_xattr_ref_node(c, ref);
601 dbg_xattr("success on saving xref (ino=%u, xid=%u)\n", ref->ic->ino, ref->xd->xid);
606 static struct jffs2_xattr_ref *create_xattr_ref(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic,
607 struct jffs2_xattr_datum *xd, uint32_t phys_ofs)
609 /* must be called under down_write(xattr_sem) */
610 struct jffs2_xattr_ref *ref;
613 ref = jffs2_alloc_xattr_ref();
615 return ERR_PTR(-ENOMEM);
619 ret = save_xattr_ref(c, ref, phys_ofs);
621 jffs2_free_xattr_ref(ref);
626 ref->next = ic->xref;
629 return ref; /* success */
632 void jffs2_xattr_delete_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
634 /* It's called from jffs2_clear_inode() on inode removing.
635 When an inode with XATTR is removed, those XATTRs must be removed. */
636 struct jffs2_xattr_ref *ref, *_ref;
638 if (!ic || ic->nlink > 0)
641 down_write(&c->xattr_sem);
642 for (ref = ic->xref; ref; ref = _ref) {
644 delete_xattr_ref(c, ref);
647 up_write(&c->xattr_sem);
650 void jffs2_xattr_free_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
652 /* It's called from jffs2_free_ino_caches() until unmounting FS. */
653 struct jffs2_xattr_datum *xd;
654 struct jffs2_xattr_ref *ref, *_ref;
656 down_write(&c->xattr_sem);
657 for (ref = ic->xref; ref; ref = _ref) {
662 unload_xattr_datum(c, xd);
663 jffs2_free_xattr_datum(xd);
665 jffs2_free_xattr_ref(ref);
668 up_write(&c->xattr_sem);
671 static int check_xattr_ref_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
673 /* success of check_xattr_ref_inode() means taht inode (ic) dose not have
674 * duplicate name/value pairs. If duplicate name/value pair would be found,
675 * one will be removed.
677 struct jffs2_xattr_ref *ref, *cmp, **pref;
680 if (likely(ic->flags & INO_FLAGS_XATTR_CHECKED))
682 down_write(&c->xattr_sem);
685 for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
686 if (!ref->xd->xname) {
687 rc = load_xattr_datum(c, ref->xd);
688 if (unlikely(rc > 0)) {
690 delete_xattr_ref(c, ref);
692 } else if (unlikely(rc < 0))
695 for (cmp=ref->next, pref=&ref->next; cmp; pref=&cmp->next, cmp=cmp->next) {
696 if (!cmp->xd->xname) {
697 ref->xd->flags |= JFFS2_XFLAGS_BIND;
698 rc = load_xattr_datum(c, cmp->xd);
699 ref->xd->flags &= ~JFFS2_XFLAGS_BIND;
700 if (unlikely(rc > 0)) {
702 delete_xattr_ref(c, cmp);
704 } else if (unlikely(rc < 0))
707 if (ref->xd->xprefix == cmp->xd->xprefix
708 && !strcmp(ref->xd->xname, cmp->xd->xname)) {
710 delete_xattr_ref(c, cmp);
715 ic->flags |= INO_FLAGS_XATTR_CHECKED;
717 up_write(&c->xattr_sem);
722 /* -------- xattr subsystem functions ---------------
723 * jffs2_init_xattr_subsystem(c)
724 * is used to initialize semaphore and list_head, and some variables.
725 * jffs2_find_xattr_datum(c, xid)
726 * is used to lookup xdatum while scanning process.
727 * jffs2_clear_xattr_subsystem(c)
728 * is used to release any xattr related objects.
729 * jffs2_build_xattr_subsystem(c)
730 * is used to associate xdatum and xref while super block building process.
731 * jffs2_setup_xattr_datum(c, xid, version)
732 * is used to insert xdatum while scanning process.
733 * -------------------------------------------------- */
734 void jffs2_init_xattr_subsystem(struct jffs2_sb_info *c)
738 for (i=0; i < XATTRINDEX_HASHSIZE; i++)
739 INIT_LIST_HEAD(&c->xattrindex[i]);
740 INIT_LIST_HEAD(&c->xattr_unchecked);
743 init_rwsem(&c->xattr_sem);
744 c->xdatum_mem_usage = 0;
745 c->xdatum_mem_threshold = 32 * 1024; /* Default 32KB */
748 static struct jffs2_xattr_datum *jffs2_find_xattr_datum(struct jffs2_sb_info *c, uint32_t xid)
750 struct jffs2_xattr_datum *xd;
751 int i = xid % XATTRINDEX_HASHSIZE;
753 /* It's only used in scanning/building process. */
754 BUG_ON(!(c->flags & (JFFS2_SB_FLAG_SCANNING|JFFS2_SB_FLAG_BUILDING)));
756 list_for_each_entry(xd, &c->xattrindex[i], xindex) {
763 void jffs2_clear_xattr_subsystem(struct jffs2_sb_info *c)
765 struct jffs2_xattr_datum *xd, *_xd;
766 struct jffs2_xattr_ref *ref, *_ref;
769 for (ref=c->xref_temp; ref; ref = _ref) {
771 jffs2_free_xattr_ref(ref);
775 for (i=0; i < XATTRINDEX_HASHSIZE; i++) {
776 list_for_each_entry_safe(xd, _xd, &c->xattrindex[i], xindex) {
777 list_del(&xd->xindex);
780 jffs2_free_xattr_datum(xd);
785 void jffs2_build_xattr_subsystem(struct jffs2_sb_info *c)
787 struct jffs2_xattr_ref *ref, *_ref;
788 struct jffs2_xattr_datum *xd, *_xd;
789 struct jffs2_inode_cache *ic;
790 int i, xdatum_count =0, xdatum_unchecked_count = 0, xref_count = 0;
792 BUG_ON(!(c->flags & JFFS2_SB_FLAG_BUILDING));
795 for (ref=c->xref_temp; ref; ref=_ref) {
797 /* checking REF_UNCHECKED nodes */
798 if (ref_flags(ref->node) != REF_PRISTINE) {
799 if (verify_xattr_ref(c, ref)) {
800 delete_xattr_ref_node(c, ref);
801 jffs2_free_xattr_ref(ref);
805 /* At this point, ref->xid and ref->ino contain XID and inode number.
806 ref->xd and ref->ic are not valid yet. */
807 xd = jffs2_find_xattr_datum(c, ref->xid);
808 ic = jffs2_get_ino_cache(c, ref->ino);
810 if (ref_flags(ref->node) != REF_UNCHECKED)
811 JFFS2_WARNING("xref(ino=%u, xid=%u) is orphan. \n",
813 delete_xattr_ref_node(c, ref);
814 jffs2_free_xattr_ref(ref);
820 ref->next = ic->xref;
825 /* After this, ref->xid/ino are NEVER used. */
828 for (i=0; i < XATTRINDEX_HASHSIZE; i++) {
829 list_for_each_entry_safe(xd, _xd, &c->xattrindex[i], xindex) {
830 list_del_init(&xd->xindex);
832 if (ref_flags(xd->node) != REF_UNCHECKED)
833 JFFS2_WARNING("orphan xdatum(xid=%u, version=%u) at %#08x\n",
834 xd->xid, xd->version, ref_offset(xd->node));
835 delete_xattr_datum(c, xd);
838 if (ref_flags(xd->node) != REF_PRISTINE) {
839 dbg_xattr("unchecked xdatum(xid=%u) at %#08x\n",
840 xd->xid, ref_offset(xd->node));
841 list_add(&xd->xindex, &c->xattr_unchecked);
842 xdatum_unchecked_count++;
848 JFFS2_NOTICE("complete building xattr subsystem, %u of xdatum (%u unchecked) and "
849 "%u of xref found.\n", xdatum_count, xdatum_unchecked_count, xref_count);
852 struct jffs2_xattr_datum *jffs2_setup_xattr_datum(struct jffs2_sb_info *c,
853 uint32_t xid, uint32_t version)
855 struct jffs2_xattr_datum *xd, *_xd;
857 _xd = jffs2_find_xattr_datum(c, xid);
859 dbg_xattr("duplicate xdatum (xid=%u, version=%u/%u) at %#08x\n",
860 xid, version, _xd->version, ref_offset(_xd->node));
861 if (version < _xd->version)
862 return ERR_PTR(-EEXIST);
864 xd = jffs2_alloc_xattr_datum();
866 return ERR_PTR(-ENOMEM);
868 xd->version = version;
869 if (xd->xid > c->highest_xid)
870 c->highest_xid = xd->xid;
871 list_add_tail(&xd->xindex, &c->xattrindex[xid % XATTRINDEX_HASHSIZE]);
874 list_del_init(&_xd->xindex);
875 delete_xattr_datum_node(c, _xd);
876 jffs2_free_xattr_datum(_xd);
881 /* -------- xattr subsystem functions ---------------
882 * xprefix_to_handler(xprefix)
883 * is used to translate xprefix into xattr_handler.
884 * jffs2_listxattr(dentry, buffer, size)
885 * is an implementation of listxattr handler on jffs2.
886 * do_jffs2_getxattr(inode, xprefix, xname, buffer, size)
887 * is an implementation of getxattr handler on jffs2.
888 * do_jffs2_setxattr(inode, xprefix, xname, buffer, size, flags)
889 * is an implementation of setxattr handler on jffs2.
890 * -------------------------------------------------- */
891 struct xattr_handler *jffs2_xattr_handlers[] = {
892 &jffs2_user_xattr_handler,
893 #ifdef CONFIG_JFFS2_FS_SECURITY
894 &jffs2_security_xattr_handler,
896 #ifdef CONFIG_JFFS2_FS_POSIX_ACL
897 &jffs2_acl_access_xattr_handler,
898 &jffs2_acl_default_xattr_handler,
900 &jffs2_trusted_xattr_handler,
904 static struct xattr_handler *xprefix_to_handler(int xprefix) {
905 struct xattr_handler *ret;
908 case JFFS2_XPREFIX_USER:
909 ret = &jffs2_user_xattr_handler;
911 #ifdef CONFIG_JFFS2_FS_SECURITY
912 case JFFS2_XPREFIX_SECURITY:
913 ret = &jffs2_security_xattr_handler;
916 #ifdef CONFIG_JFFS2_FS_POSIX_ACL
917 case JFFS2_XPREFIX_ACL_ACCESS:
918 ret = &jffs2_acl_access_xattr_handler;
920 case JFFS2_XPREFIX_ACL_DEFAULT:
921 ret = &jffs2_acl_default_xattr_handler;
924 case JFFS2_XPREFIX_TRUSTED:
925 ret = &jffs2_trusted_xattr_handler;
934 ssize_t jffs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
936 struct inode *inode = dentry->d_inode;
937 struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
938 struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
939 struct jffs2_inode_cache *ic = f->inocache;
940 struct jffs2_xattr_ref *ref, **pref;
941 struct jffs2_xattr_datum *xd;
942 struct xattr_handler *xhandle;
946 rc = check_xattr_ref_inode(c, ic);
950 down_read(&c->xattr_sem);
953 for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
954 BUG_ON(ref->ic != ic);
957 /* xdatum is unchached */
960 up_read(&c->xattr_sem);
961 down_write(&c->xattr_sem);
964 rc = load_xattr_datum(c, xd);
965 if (unlikely(rc > 0)) {
967 delete_xattr_ref(c, ref);
969 } else if (unlikely(rc < 0))
973 xhandle = xprefix_to_handler(xd->xprefix);
977 rc = xhandle->list(inode, buffer+len, size-len, xd->xname, xd->name_len);
979 rc = xhandle->list(inode, NULL, 0, xd->xname, xd->name_len);
988 up_read(&c->xattr_sem);
990 up_write(&c->xattr_sem);
995 int do_jffs2_getxattr(struct inode *inode, int xprefix, const char *xname,
996 char *buffer, size_t size)
998 struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
999 struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
1000 struct jffs2_inode_cache *ic = f->inocache;
1001 struct jffs2_xattr_datum *xd;
1002 struct jffs2_xattr_ref *ref, **pref;
1005 rc = check_xattr_ref_inode(c, ic);
1009 down_read(&c->xattr_sem);
1011 for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
1012 BUG_ON(ref->ic!=ic);
1015 if (xd->xprefix != xprefix)
1018 /* xdatum is unchached */
1021 up_read(&c->xattr_sem);
1022 down_write(&c->xattr_sem);
1025 rc = load_xattr_datum(c, xd);
1026 if (unlikely(rc > 0)) {
1028 delete_xattr_ref(c, ref);
1030 } else if (unlikely(rc < 0)) {
1035 if (!strcmp(xname, xd->xname)) {
1041 memcpy(buffer, xd->xvalue, rc);
1050 up_read(&c->xattr_sem);
1052 up_write(&c->xattr_sem);
1057 int do_jffs2_setxattr(struct inode *inode, int xprefix, const char *xname,
1058 const char *buffer, size_t size, int flags)
1060 struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
1061 struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
1062 struct jffs2_inode_cache *ic = f->inocache;
1063 struct jffs2_xattr_datum *xd;
1064 struct jffs2_xattr_ref *ref, *newref, **pref;
1065 uint32_t phys_ofs, length, request;
1068 rc = check_xattr_ref_inode(c, ic);
1072 request = PAD(sizeof(struct jffs2_raw_xattr) + strlen(xname) + 1 + size);
1073 rc = jffs2_reserve_space(c, request, &phys_ofs, &length,
1074 ALLOC_NORMAL, JFFS2_SUMMARY_XATTR_SIZE);
1076 JFFS2_WARNING("jffs2_reserve_space()=%d, request=%u\n", rc, request);
1080 /* Find existing xattr */
1081 down_write(&c->xattr_sem);
1083 for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
1085 if (xd->xprefix != xprefix)
1088 rc = load_xattr_datum(c, xd);
1089 if (unlikely(rc > 0)) {
1091 delete_xattr_ref(c, ref);
1093 } else if (unlikely(rc < 0))
1096 if (!strcmp(xd->xname, xname)) {
1097 if (flags & XATTR_CREATE) {
1103 delete_xattr_ref(c, ref);
1111 if (flags & XATTR_REPLACE) {
1120 xd = create_xattr_datum(c, xprefix, xname, buffer, size, phys_ofs);
1125 up_write(&c->xattr_sem);
1126 jffs2_complete_reservation(c);
1128 /* create xattr_ref */
1129 request = PAD(sizeof(struct jffs2_raw_xref));
1130 rc = jffs2_reserve_space(c, request, &phys_ofs, &length,
1131 ALLOC_NORMAL, JFFS2_SUMMARY_XREF_SIZE);
1133 JFFS2_WARNING("jffs2_reserve_space()=%d, request=%u\n", rc, request);
1134 down_write(&c->xattr_sem);
1137 delete_xattr_datum(c, xd);
1138 up_write(&c->xattr_sem);
1141 down_write(&c->xattr_sem);
1144 newref = create_xattr_ref(c, ic, xd, phys_ofs);
1145 if (IS_ERR(newref)) {
1147 ref->next = ic->xref;
1150 rc = PTR_ERR(newref);
1153 delete_xattr_datum(c, xd);
1155 delete_xattr_ref(c, ref);
1158 up_write(&c->xattr_sem);
1159 jffs2_complete_reservation(c);
1163 /* -------- garbage collector functions -------------
1164 * jffs2_garbage_collect_xattr_datum(c, xd)
1165 * is used to move xdatum into new node.
1166 * jffs2_garbage_collect_xattr_ref(c, ref)
1167 * is used to move xref into new node.
1168 * jffs2_verify_xattr(c)
1169 * is used to call do_verify_xattr_datum() before garbage collecting.
1170 * -------------------------------------------------- */
1171 int jffs2_garbage_collect_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
1173 uint32_t phys_ofs, totlen, length, old_ofs;
1176 down_write(&c->xattr_sem);
1179 old_ofs = ref_offset(xd->node);
1180 totlen = ref_totlen(c, c->gcblock, xd->node);
1181 if (totlen < sizeof(struct jffs2_raw_xattr))
1185 rc = load_xattr_datum(c, xd);
1186 if (unlikely(rc > 0)) {
1187 delete_xattr_datum_node(c, xd);
1190 } else if (unlikely(rc < 0))
1193 rc = jffs2_reserve_space_gc(c, totlen, &phys_ofs, &length, JFFS2_SUMMARY_XATTR_SIZE);
1194 if (rc || length < totlen) {
1195 JFFS2_WARNING("jffs2_reserve_space()=%d, request=%u\n", rc, totlen);
1196 rc = rc ? rc : -EBADFD;
1199 rc = save_xattr_datum(c, xd, phys_ofs);
1201 dbg_xattr("xdatum (xid=%u, version=%u) GC'ed from %#08x to %08x\n",
1202 xd->xid, xd->version, old_ofs, ref_offset(xd->node));
1204 up_write(&c->xattr_sem);
1209 int jffs2_garbage_collect_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
1211 uint32_t phys_ofs, totlen, length, old_ofs;
1214 down_write(&c->xattr_sem);
1217 old_ofs = ref_offset(ref->node);
1218 totlen = ref_totlen(c, c->gcblock, ref->node);
1219 if (totlen != sizeof(struct jffs2_raw_xref))
1222 rc = jffs2_reserve_space_gc(c, totlen, &phys_ofs, &length, JFFS2_SUMMARY_XREF_SIZE);
1223 if (rc || length < totlen) {
1224 JFFS2_WARNING("%s: jffs2_reserve_space() = %d, request = %u\n",
1225 __FUNCTION__, rc, totlen);
1226 rc = rc ? rc : -EBADFD;
1229 rc = save_xattr_ref(c, ref, phys_ofs);
1231 dbg_xattr("xref (ino=%u, xid=%u) GC'ed from %#08x to %08x\n",
1232 ref->ic->ino, ref->xd->xid, old_ofs, ref_offset(ref->node));
1234 up_write(&c->xattr_sem);
1238 int jffs2_verify_xattr(struct jffs2_sb_info *c)
1240 struct jffs2_xattr_datum *xd, *_xd;
1243 down_write(&c->xattr_sem);
1244 list_for_each_entry_safe(xd, _xd, &c->xattr_unchecked, xindex) {
1245 rc = do_verify_xattr_datum(c, xd);
1247 list_del_init(&xd->xindex);
1249 } else if (rc > 0) {
1250 list_del_init(&xd->xindex);
1251 delete_xattr_datum_node(c, xd);
1254 up_write(&c->xattr_sem);
1256 return list_empty(&c->xattr_unchecked) ? 1 : 0;