2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright © 2006 NEC Corporation
6 * Created by KaiGai Kohei <kaigai@ak.jp.nec.com>
8 * For licensing information, see the file 'LICENCE' in this directory.
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #define JFFS2_XATTR_IS_CORRUPTED 1
16 #include <linux/kernel.h>
17 #include <linux/slab.h>
19 #include <linux/time.h>
20 #include <linux/pagemap.h>
21 #include <linux/highmem.h>
22 #include <linux/crc32.h>
23 #include <linux/jffs2.h>
24 #include <linux/xattr.h>
25 #include <linux/posix_acl_xattr.h>
26 #include <linux/mtd/mtd.h>
28 /* -------- xdatum related functions ----------------
29 * xattr_datum_hashkey(xprefix, xname, xvalue, xsize)
30 * is used to calcurate xdatum hashkey. The reminder of hashkey into XATTRINDEX_HASHSIZE is
31 * the index of the xattr name/value pair cache (c->xattrindex).
32 * is_xattr_datum_unchecked(c, xd)
33 * returns 1, if xdatum contains any unchecked raw nodes. if all raw nodes are not
34 * unchecked, it returns 0.
35 * unload_xattr_datum(c, xd)
36 * is used to release xattr name/value pair and detach from c->xattrindex.
37 * reclaim_xattr_datum(c)
38 * is used to reclaim xattr name/value pairs on the xattr name/value pair cache when
39 * memory usage by cache is over c->xdatum_mem_threshold. Currently, this threshold
40 * is hard coded as 32KiB.
41 * do_verify_xattr_datum(c, xd)
42 * is used to load the xdatum informations without name/value pair from the medium.
43 * It's necessary once, because those informations are not collected during mounting
44 * process when EBS is enabled.
45 * 0 will be returned, if success. An negative return value means recoverable error, and
46 * positive return value means unrecoverable error. Thus, caller must remove this xdatum
47 * and xref when it returned positive value.
48 * do_load_xattr_datum(c, xd)
49 * is used to load name/value pair from the medium.
50 * The meanings of return value is same as do_verify_xattr_datum().
51 * load_xattr_datum(c, xd)
52 * is used to be as a wrapper of do_verify_xattr_datum() and do_load_xattr_datum().
53 * If xd need to call do_verify_xattr_datum() at first, it's called before calling
54 * do_load_xattr_datum(). The meanings of return value is same as do_verify_xattr_datum().
55 * save_xattr_datum(c, xd)
56 * is used to write xdatum to medium. xd->version will be incremented.
57 * create_xattr_datum(c, xprefix, xname, xvalue, xsize)
58 * is used to create new xdatum and write to medium.
59 * unrefer_xattr_datum(c, xd)
60 * is used to delete a xdatum. When nobody refers this xdatum, JFFS2_XFLAGS_DEAD
61 * is set on xd->flags and chained xattr_dead_list or release it immediately.
62 * In the first case, the garbage collector release it later.
63 * -------------------------------------------------- */
64 static uint32_t xattr_datum_hashkey(int xprefix, const char *xname, const char *xvalue, int xsize)
66 int name_len = strlen(xname);
68 return crc32(xprefix, xname, name_len) ^ crc32(xprefix, xvalue, xsize);
71 static int is_xattr_datum_unchecked(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
73 struct jffs2_raw_node_ref *raw;
76 spin_lock(&c->erase_completion_lock);
77 for (raw=xd->node; raw != (void *)xd; raw=raw->next_in_ino) {
78 if (ref_flags(raw) == REF_UNCHECKED) {
83 spin_unlock(&c->erase_completion_lock);
87 static void unload_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
89 /* must be called under down_write(xattr_sem) */
90 D1(dbg_xattr("%s: xid=%u, version=%u\n", __func__, xd->xid, xd->version));
92 c->xdatum_mem_usage -= (xd->name_len + 1 + xd->value_len);
96 list_del_init(&xd->xindex);
102 static void reclaim_xattr_datum(struct jffs2_sb_info *c)
104 /* must be called under down_write(xattr_sem) */
105 struct jffs2_xattr_datum *xd, *_xd;
106 uint32_t target, before;
107 static int index = 0;
110 if (c->xdatum_mem_threshold > c->xdatum_mem_usage)
113 before = c->xdatum_mem_usage;
114 target = c->xdatum_mem_usage * 4 / 5; /* 20% reduction */
115 for (count = 0; count < XATTRINDEX_HASHSIZE; count++) {
116 list_for_each_entry_safe(xd, _xd, &c->xattrindex[index], xindex) {
117 if (xd->flags & JFFS2_XFLAGS_HOT) {
118 xd->flags &= ~JFFS2_XFLAGS_HOT;
119 } else if (!(xd->flags & JFFS2_XFLAGS_BIND)) {
120 unload_xattr_datum(c, xd);
122 if (c->xdatum_mem_usage <= target)
125 index = (index+1) % XATTRINDEX_HASHSIZE;
128 JFFS2_NOTICE("xdatum_mem_usage from %u byte to %u byte (%u byte reclaimed)\n",
129 before, c->xdatum_mem_usage, before - c->xdatum_mem_usage);
132 static int do_verify_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
134 /* must be called under down_write(xattr_sem) */
135 struct jffs2_eraseblock *jeb;
136 struct jffs2_raw_node_ref *raw;
137 struct jffs2_raw_xattr rx;
139 uint32_t crc, offset, totlen;
142 spin_lock(&c->erase_completion_lock);
143 offset = ref_offset(xd->node);
144 if (ref_flags(xd->node) == REF_PRISTINE)
146 spin_unlock(&c->erase_completion_lock);
148 rc = jffs2_flash_read(c, offset, sizeof(rx), &readlen, (char *)&rx);
149 if (rc || readlen != sizeof(rx)) {
150 JFFS2_WARNING("jffs2_flash_read()=%d, req=%zu, read=%zu at %#08x\n",
151 rc, sizeof(rx), readlen, offset);
152 return rc ? rc : -EIO;
154 crc = crc32(0, &rx, sizeof(rx) - 4);
155 if (crc != je32_to_cpu(rx.node_crc)) {
156 JFFS2_ERROR("node CRC failed at %#08x, read=%#08x, calc=%#08x\n",
157 offset, je32_to_cpu(rx.hdr_crc), crc);
158 xd->flags |= JFFS2_XFLAGS_INVALID;
159 return JFFS2_XATTR_IS_CORRUPTED;
161 totlen = PAD(sizeof(rx) + rx.name_len + 1 + je16_to_cpu(rx.value_len));
162 if (je16_to_cpu(rx.magic) != JFFS2_MAGIC_BITMASK
163 || je16_to_cpu(rx.nodetype) != JFFS2_NODETYPE_XATTR
164 || je32_to_cpu(rx.totlen) != totlen
165 || je32_to_cpu(rx.xid) != xd->xid
166 || je32_to_cpu(rx.version) != xd->version) {
167 JFFS2_ERROR("inconsistent xdatum at %#08x, magic=%#04x/%#04x, "
168 "nodetype=%#04x/%#04x, totlen=%u/%u, xid=%u/%u, version=%u/%u\n",
169 offset, je16_to_cpu(rx.magic), JFFS2_MAGIC_BITMASK,
170 je16_to_cpu(rx.nodetype), JFFS2_NODETYPE_XATTR,
171 je32_to_cpu(rx.totlen), totlen,
172 je32_to_cpu(rx.xid), xd->xid,
173 je32_to_cpu(rx.version), xd->version);
174 xd->flags |= JFFS2_XFLAGS_INVALID;
175 return JFFS2_XATTR_IS_CORRUPTED;
177 xd->xprefix = rx.xprefix;
178 xd->name_len = rx.name_len;
179 xd->value_len = je16_to_cpu(rx.value_len);
180 xd->data_crc = je32_to_cpu(rx.data_crc);
182 spin_lock(&c->erase_completion_lock);
184 for (raw=xd->node; raw != (void *)xd; raw=raw->next_in_ino) {
185 jeb = &c->blocks[ref_offset(raw) / c->sector_size];
186 totlen = PAD(ref_totlen(c, jeb, raw));
187 if (ref_flags(raw) == REF_UNCHECKED) {
188 c->unchecked_size -= totlen; c->used_size += totlen;
189 jeb->unchecked_size -= totlen; jeb->used_size += totlen;
191 raw->flash_offset = ref_offset(raw) | ((xd->node==raw) ? REF_PRISTINE : REF_NORMAL);
193 spin_unlock(&c->erase_completion_lock);
195 /* unchecked xdatum is chained with c->xattr_unchecked */
196 list_del_init(&xd->xindex);
198 dbg_xattr("success on verifying xdatum (xid=%u, version=%u)\n",
199 xd->xid, xd->version);
204 static int do_load_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
206 /* must be called under down_write(xattr_sem) */
209 uint32_t crc, length;
210 int i, ret, retry = 0;
212 BUG_ON(ref_flags(xd->node) != REF_PRISTINE);
213 BUG_ON(!list_empty(&xd->xindex));
215 length = xd->name_len + 1 + xd->value_len;
216 data = kmalloc(length, GFP_KERNEL);
220 ret = jffs2_flash_read(c, ref_offset(xd->node)+sizeof(struct jffs2_raw_xattr),
221 length, &readlen, data);
223 if (ret || length!=readlen) {
224 JFFS2_WARNING("jffs2_flash_read() returned %d, request=%d, readlen=%zu, at %#08x\n",
225 ret, length, readlen, ref_offset(xd->node));
227 return ret ? ret : -EIO;
230 data[xd->name_len] = '\0';
231 crc = crc32(0, data, length);
232 if (crc != xd->data_crc) {
233 JFFS2_WARNING("node CRC failed (JFFS2_NODETYPE_XATTR)"
234 " at %#08x, read: 0x%08x calculated: 0x%08x\n",
235 ref_offset(xd->node), xd->data_crc, crc);
237 xd->flags |= JFFS2_XFLAGS_INVALID;
238 return JFFS2_XATTR_IS_CORRUPTED;
241 xd->flags |= JFFS2_XFLAGS_HOT;
243 xd->xvalue = data + xd->name_len+1;
245 c->xdatum_mem_usage += length;
247 xd->hashkey = xattr_datum_hashkey(xd->xprefix, xd->xname, xd->xvalue, xd->value_len);
248 i = xd->hashkey % XATTRINDEX_HASHSIZE;
249 list_add(&xd->xindex, &c->xattrindex[i]);
252 reclaim_xattr_datum(c);
257 dbg_xattr("success on loading xdatum (xid=%u, xprefix=%u, xname='%s')\n",
258 xd->xid, xd->xprefix, xd->xname);
263 static int load_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
265 /* must be called under down_write(xattr_sem);
266 * rc < 0 : recoverable error, try again
268 * rc > 0 : Unrecoverable error, this node should be deleted.
272 BUG_ON(xd->flags & JFFS2_XFLAGS_DEAD);
275 if (xd->flags & JFFS2_XFLAGS_INVALID)
276 return JFFS2_XATTR_IS_CORRUPTED;
277 if (unlikely(is_xattr_datum_unchecked(c, xd)))
278 rc = do_verify_xattr_datum(c, xd);
280 rc = do_load_xattr_datum(c, xd);
284 static int save_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
286 /* must be called under down_write(xattr_sem) */
287 struct jffs2_raw_xattr rx;
291 uint32_t phys_ofs = write_ofs(c);
294 BUG_ON(xd->flags & (JFFS2_XFLAGS_DEAD|JFFS2_XFLAGS_INVALID));
296 vecs[0].iov_base = ℞
297 vecs[0].iov_len = sizeof(rx);
298 vecs[1].iov_base = xd->xname;
299 vecs[1].iov_len = xd->name_len + 1 + xd->value_len;
300 totlen = vecs[0].iov_len + vecs[1].iov_len;
302 /* Setup raw-xattr */
303 memset(&rx, 0, sizeof(rx));
304 rx.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
305 rx.nodetype = cpu_to_je16(JFFS2_NODETYPE_XATTR);
306 rx.totlen = cpu_to_je32(PAD(totlen));
307 rx.hdr_crc = cpu_to_je32(crc32(0, &rx, sizeof(struct jffs2_unknown_node) - 4));
309 rx.xid = cpu_to_je32(xd->xid);
310 rx.version = cpu_to_je32(++xd->version);
311 rx.xprefix = xd->xprefix;
312 rx.name_len = xd->name_len;
313 rx.value_len = cpu_to_je16(xd->value_len);
314 rx.data_crc = cpu_to_je32(crc32(0, vecs[1].iov_base, vecs[1].iov_len));
315 rx.node_crc = cpu_to_je32(crc32(0, &rx, sizeof(struct jffs2_raw_xattr) - 4));
317 rc = jffs2_flash_writev(c, vecs, 2, phys_ofs, &length, 0);
318 if (rc || totlen != length) {
319 JFFS2_WARNING("jffs2_flash_writev()=%d, req=%u, wrote=%zu, at %#08x\n",
320 rc, totlen, length, phys_ofs);
323 jffs2_add_physical_node_ref(c, phys_ofs | REF_OBSOLETE, PAD(totlen), NULL);
328 jffs2_add_physical_node_ref(c, phys_ofs | REF_PRISTINE, PAD(totlen), (void *)xd);
330 dbg_xattr("success on saving xdatum (xid=%u, version=%u, xprefix=%u, xname='%s')\n",
331 xd->xid, xd->version, xd->xprefix, xd->xname);
336 static struct jffs2_xattr_datum *create_xattr_datum(struct jffs2_sb_info *c,
337 int xprefix, const char *xname,
338 const char *xvalue, int xsize)
340 /* must be called under down_write(xattr_sem) */
341 struct jffs2_xattr_datum *xd;
342 uint32_t hashkey, name_len;
346 /* Search xattr_datum has same xname/xvalue by index */
347 hashkey = xattr_datum_hashkey(xprefix, xname, xvalue, xsize);
348 i = hashkey % XATTRINDEX_HASHSIZE;
349 list_for_each_entry(xd, &c->xattrindex[i], xindex) {
350 if (xd->hashkey==hashkey
351 && xd->xprefix==xprefix
352 && xd->value_len==xsize
353 && !strcmp(xd->xname, xname)
354 && !memcmp(xd->xvalue, xvalue, xsize)) {
355 atomic_inc(&xd->refcnt);
360 /* Not found, Create NEW XATTR-Cache */
361 name_len = strlen(xname);
363 xd = jffs2_alloc_xattr_datum();
365 return ERR_PTR(-ENOMEM);
367 data = kmalloc(name_len + 1 + xsize, GFP_KERNEL);
369 jffs2_free_xattr_datum(xd);
370 return ERR_PTR(-ENOMEM);
373 memcpy(data + name_len + 1, xvalue, xsize);
375 atomic_set(&xd->refcnt, 1);
376 xd->xid = ++c->highest_xid;
377 xd->flags |= JFFS2_XFLAGS_HOT;
378 xd->xprefix = xprefix;
380 xd->hashkey = hashkey;
382 xd->xvalue = data + name_len + 1;
383 xd->name_len = name_len;
384 xd->value_len = xsize;
385 xd->data_crc = crc32(0, data, xd->name_len + 1 + xd->value_len);
387 rc = save_xattr_datum(c, xd);
390 jffs2_free_xattr_datum(xd);
394 /* Insert Hash Index */
395 i = hashkey % XATTRINDEX_HASHSIZE;
396 list_add(&xd->xindex, &c->xattrindex[i]);
398 c->xdatum_mem_usage += (xd->name_len + 1 + xd->value_len);
399 reclaim_xattr_datum(c);
404 static void unrefer_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
406 /* must be called under down_write(xattr_sem) */
407 if (atomic_dec_and_lock(&xd->refcnt, &c->erase_completion_lock)) {
408 unload_xattr_datum(c, xd);
409 xd->flags |= JFFS2_XFLAGS_DEAD;
410 if (xd->node == (void *)xd) {
411 BUG_ON(!(xd->flags & JFFS2_XFLAGS_INVALID));
412 jffs2_free_xattr_datum(xd);
414 list_add(&xd->xindex, &c->xattr_dead_list);
416 spin_unlock(&c->erase_completion_lock);
418 dbg_xattr("xdatum(xid=%u, version=%u) was removed.\n",
419 xd->xid, xd->version);
423 /* -------- xref related functions ------------------
424 * verify_xattr_ref(c, ref)
425 * is used to load xref information from medium. Because summary data does not
426 * contain xid/ino, it's necessary to verify once while mounting process.
427 * save_xattr_ref(c, ref)
428 * is used to write xref to medium. If delete marker is marked, it write
429 * a delete marker of xref into medium.
430 * create_xattr_ref(c, ic, xd)
431 * is used to create a new xref and write to medium.
432 * delete_xattr_ref(c, ref)
433 * is used to delete jffs2_xattr_ref. It marks xref XREF_DELETE_MARKER,
434 * and allows GC to reclaim those physical nodes.
435 * jffs2_xattr_delete_inode(c, ic)
436 * is called to remove xrefs related to obsolete inode when inode is unlinked.
437 * jffs2_xattr_free_inode(c, ic)
438 * is called to release xattr related objects when unmounting.
439 * check_xattr_ref_inode(c, ic)
440 * is used to confirm inode does not have duplicate xattr name/value pair.
441 * jffs2_xattr_do_crccheck_inode(c, ic)
442 * is used to force xattr data integrity check during the initial gc scan.
443 * -------------------------------------------------- */
444 static int verify_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
446 struct jffs2_eraseblock *jeb;
447 struct jffs2_raw_node_ref *raw;
448 struct jffs2_raw_xref rr;
450 uint32_t crc, offset, totlen;
453 spin_lock(&c->erase_completion_lock);
454 if (ref_flags(ref->node) != REF_UNCHECKED)
456 offset = ref_offset(ref->node);
457 spin_unlock(&c->erase_completion_lock);
459 rc = jffs2_flash_read(c, offset, sizeof(rr), &readlen, (char *)&rr);
460 if (rc || sizeof(rr) != readlen) {
461 JFFS2_WARNING("jffs2_flash_read()=%d, req=%zu, read=%zu, at %#08x\n",
462 rc, sizeof(rr), readlen, offset);
463 return rc ? rc : -EIO;
466 crc = crc32(0, &rr, sizeof(rr) - 4);
467 if (crc != je32_to_cpu(rr.node_crc)) {
468 JFFS2_ERROR("node CRC failed at %#08x, read=%#08x, calc=%#08x\n",
469 offset, je32_to_cpu(rr.node_crc), crc);
470 return JFFS2_XATTR_IS_CORRUPTED;
472 if (je16_to_cpu(rr.magic) != JFFS2_MAGIC_BITMASK
473 || je16_to_cpu(rr.nodetype) != JFFS2_NODETYPE_XREF
474 || je32_to_cpu(rr.totlen) != PAD(sizeof(rr))) {
475 JFFS2_ERROR("inconsistent xref at %#08x, magic=%#04x/%#04x, "
476 "nodetype=%#04x/%#04x, totlen=%u/%zu\n",
477 offset, je16_to_cpu(rr.magic), JFFS2_MAGIC_BITMASK,
478 je16_to_cpu(rr.nodetype), JFFS2_NODETYPE_XREF,
479 je32_to_cpu(rr.totlen), PAD(sizeof(rr)));
480 return JFFS2_XATTR_IS_CORRUPTED;
482 ref->ino = je32_to_cpu(rr.ino);
483 ref->xid = je32_to_cpu(rr.xid);
484 ref->xseqno = je32_to_cpu(rr.xseqno);
485 if (ref->xseqno > c->highest_xseqno)
486 c->highest_xseqno = (ref->xseqno & ~XREF_DELETE_MARKER);
488 spin_lock(&c->erase_completion_lock);
490 for (raw=ref->node; raw != (void *)ref; raw=raw->next_in_ino) {
491 jeb = &c->blocks[ref_offset(raw) / c->sector_size];
492 totlen = PAD(ref_totlen(c, jeb, raw));
493 if (ref_flags(raw) == REF_UNCHECKED) {
494 c->unchecked_size -= totlen; c->used_size += totlen;
495 jeb->unchecked_size -= totlen; jeb->used_size += totlen;
497 raw->flash_offset = ref_offset(raw) | ((ref->node==raw) ? REF_PRISTINE : REF_NORMAL);
499 spin_unlock(&c->erase_completion_lock);
501 dbg_xattr("success on verifying xref (ino=%u, xid=%u) at %#08x\n",
502 ref->ino, ref->xid, ref_offset(ref->node));
506 static int save_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
508 /* must be called under down_write(xattr_sem) */
509 struct jffs2_raw_xref rr;
511 uint32_t xseqno, phys_ofs = write_ofs(c);
514 rr.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
515 rr.nodetype = cpu_to_je16(JFFS2_NODETYPE_XREF);
516 rr.totlen = cpu_to_je32(PAD(sizeof(rr)));
517 rr.hdr_crc = cpu_to_je32(crc32(0, &rr, sizeof(struct jffs2_unknown_node) - 4));
519 xseqno = (c->highest_xseqno += 2);
520 if (is_xattr_ref_dead(ref)) {
521 xseqno |= XREF_DELETE_MARKER;
522 rr.ino = cpu_to_je32(ref->ino);
523 rr.xid = cpu_to_je32(ref->xid);
525 rr.ino = cpu_to_je32(ref->ic->ino);
526 rr.xid = cpu_to_je32(ref->xd->xid);
528 rr.xseqno = cpu_to_je32(xseqno);
529 rr.node_crc = cpu_to_je32(crc32(0, &rr, sizeof(rr) - 4));
531 ret = jffs2_flash_write(c, phys_ofs, sizeof(rr), &length, (char *)&rr);
532 if (ret || sizeof(rr) != length) {
533 JFFS2_WARNING("jffs2_flash_write() returned %d, request=%zu, retlen=%zu, at %#08x\n",
534 ret, sizeof(rr), length, phys_ofs);
535 ret = ret ? ret : -EIO;
537 jffs2_add_physical_node_ref(c, phys_ofs | REF_OBSOLETE, PAD(sizeof(rr)), NULL);
542 ref->xseqno = xseqno;
543 jffs2_add_physical_node_ref(c, phys_ofs | REF_PRISTINE, PAD(sizeof(rr)), (void *)ref);
545 dbg_xattr("success on saving xref (ino=%u, xid=%u)\n", ref->ic->ino, ref->xd->xid);
550 static struct jffs2_xattr_ref *create_xattr_ref(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic,
551 struct jffs2_xattr_datum *xd)
553 /* must be called under down_write(xattr_sem) */
554 struct jffs2_xattr_ref *ref;
557 ref = jffs2_alloc_xattr_ref();
559 return ERR_PTR(-ENOMEM);
563 ret = save_xattr_ref(c, ref);
565 jffs2_free_xattr_ref(ref);
570 ref->next = ic->xref;
573 return ref; /* success */
576 static void delete_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
578 /* must be called under down_write(xattr_sem) */
579 struct jffs2_xattr_datum *xd;
582 ref->xseqno |= XREF_DELETE_MARKER;
583 ref->ino = ref->ic->ino;
584 ref->xid = ref->xd->xid;
585 spin_lock(&c->erase_completion_lock);
586 ref->next = c->xref_dead_list;
587 c->xref_dead_list = ref;
588 spin_unlock(&c->erase_completion_lock);
590 dbg_xattr("xref(ino=%u, xid=%u, xseqno=%u) was removed.\n",
591 ref->ino, ref->xid, ref->xseqno);
593 unrefer_xattr_datum(c, xd);
596 void jffs2_xattr_delete_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
598 /* It's called from jffs2_evict_inode() on inode removing.
599 When an inode with XATTR is removed, those XATTRs must be removed. */
600 struct jffs2_xattr_ref *ref, *_ref;
602 if (!ic || ic->pino_nlink > 0)
605 down_write(&c->xattr_sem);
606 for (ref = ic->xref; ref; ref = _ref) {
608 delete_xattr_ref(c, ref);
611 up_write(&c->xattr_sem);
614 void jffs2_xattr_free_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
616 /* It's called from jffs2_free_ino_caches() until unmounting FS. */
617 struct jffs2_xattr_datum *xd;
618 struct jffs2_xattr_ref *ref, *_ref;
620 down_write(&c->xattr_sem);
621 for (ref = ic->xref; ref; ref = _ref) {
624 if (atomic_dec_and_test(&xd->refcnt)) {
625 unload_xattr_datum(c, xd);
626 jffs2_free_xattr_datum(xd);
628 jffs2_free_xattr_ref(ref);
631 up_write(&c->xattr_sem);
634 static int check_xattr_ref_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
636 /* success of check_xattr_ref_inode() means that inode (ic) dose not have
637 * duplicate name/value pairs. If duplicate name/value pair would be found,
638 * one will be removed.
640 struct jffs2_xattr_ref *ref, *cmp, **pref, **pcmp;
643 if (likely(ic->flags & INO_FLAGS_XATTR_CHECKED))
645 down_write(&c->xattr_sem);
648 for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
649 if (!ref->xd->xname) {
650 rc = load_xattr_datum(c, ref->xd);
651 if (unlikely(rc > 0)) {
653 delete_xattr_ref(c, ref);
655 } else if (unlikely(rc < 0))
658 for (cmp=ref->next, pcmp=&ref->next; cmp; pcmp=&cmp->next, cmp=cmp->next) {
659 if (!cmp->xd->xname) {
660 ref->xd->flags |= JFFS2_XFLAGS_BIND;
661 rc = load_xattr_datum(c, cmp->xd);
662 ref->xd->flags &= ~JFFS2_XFLAGS_BIND;
663 if (unlikely(rc > 0)) {
665 delete_xattr_ref(c, cmp);
667 } else if (unlikely(rc < 0))
670 if (ref->xd->xprefix == cmp->xd->xprefix
671 && !strcmp(ref->xd->xname, cmp->xd->xname)) {
672 if (ref->xseqno > cmp->xseqno) {
674 delete_xattr_ref(c, cmp);
677 delete_xattr_ref(c, ref);
683 ic->flags |= INO_FLAGS_XATTR_CHECKED;
685 up_write(&c->xattr_sem);
690 void jffs2_xattr_do_crccheck_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
692 check_xattr_ref_inode(c, ic);
695 /* -------- xattr subsystem functions ---------------
696 * jffs2_init_xattr_subsystem(c)
697 * is used to initialize semaphore and list_head, and some variables.
698 * jffs2_find_xattr_datum(c, xid)
699 * is used to lookup xdatum while scanning process.
700 * jffs2_clear_xattr_subsystem(c)
701 * is used to release any xattr related objects.
702 * jffs2_build_xattr_subsystem(c)
703 * is used to associate xdatum and xref while super block building process.
704 * jffs2_setup_xattr_datum(c, xid, version)
705 * is used to insert xdatum while scanning process.
706 * -------------------------------------------------- */
707 void jffs2_init_xattr_subsystem(struct jffs2_sb_info *c)
711 for (i=0; i < XATTRINDEX_HASHSIZE; i++)
712 INIT_LIST_HEAD(&c->xattrindex[i]);
713 INIT_LIST_HEAD(&c->xattr_unchecked);
714 INIT_LIST_HEAD(&c->xattr_dead_list);
715 c->xref_dead_list = NULL;
718 init_rwsem(&c->xattr_sem);
720 c->highest_xseqno = 0;
721 c->xdatum_mem_usage = 0;
722 c->xdatum_mem_threshold = 32 * 1024; /* Default 32KB */
725 static struct jffs2_xattr_datum *jffs2_find_xattr_datum(struct jffs2_sb_info *c, uint32_t xid)
727 struct jffs2_xattr_datum *xd;
728 int i = xid % XATTRINDEX_HASHSIZE;
730 /* It's only used in scanning/building process. */
731 BUG_ON(!(c->flags & (JFFS2_SB_FLAG_SCANNING|JFFS2_SB_FLAG_BUILDING)));
733 list_for_each_entry(xd, &c->xattrindex[i], xindex) {
740 void jffs2_clear_xattr_subsystem(struct jffs2_sb_info *c)
742 struct jffs2_xattr_datum *xd, *_xd;
743 struct jffs2_xattr_ref *ref, *_ref;
746 for (ref=c->xref_temp; ref; ref = _ref) {
748 jffs2_free_xattr_ref(ref);
751 for (ref=c->xref_dead_list; ref; ref = _ref) {
753 jffs2_free_xattr_ref(ref);
756 for (i=0; i < XATTRINDEX_HASHSIZE; i++) {
757 list_for_each_entry_safe(xd, _xd, &c->xattrindex[i], xindex) {
758 list_del(&xd->xindex);
760 jffs2_free_xattr_datum(xd);
764 list_for_each_entry_safe(xd, _xd, &c->xattr_dead_list, xindex) {
765 list_del(&xd->xindex);
766 jffs2_free_xattr_datum(xd);
768 list_for_each_entry_safe(xd, _xd, &c->xattr_unchecked, xindex) {
769 list_del(&xd->xindex);
770 jffs2_free_xattr_datum(xd);
774 #define XREF_TMPHASH_SIZE (128)
775 int jffs2_build_xattr_subsystem(struct jffs2_sb_info *c)
777 struct jffs2_xattr_ref *ref, *_ref;
778 struct jffs2_xattr_ref **xref_tmphash;
779 struct jffs2_xattr_datum *xd, *_xd;
780 struct jffs2_inode_cache *ic;
781 struct jffs2_raw_node_ref *raw;
782 int i, xdatum_count = 0, xdatum_unchecked_count = 0, xref_count = 0;
783 int xdatum_orphan_count = 0, xref_orphan_count = 0, xref_dead_count = 0;
785 BUG_ON(!(c->flags & JFFS2_SB_FLAG_BUILDING));
787 xref_tmphash = kcalloc(XREF_TMPHASH_SIZE,
788 sizeof(struct jffs2_xattr_ref *), GFP_KERNEL);
792 /* Phase.1 : Merge same xref */
793 for (ref=c->xref_temp; ref; ref=_ref) {
794 struct jffs2_xattr_ref *tmp;
797 if (ref_flags(ref->node) != REF_PRISTINE) {
798 if (verify_xattr_ref(c, ref)) {
799 BUG_ON(ref->node->next_in_ino != (void *)ref);
800 ref->node->next_in_ino = NULL;
801 jffs2_mark_node_obsolete(c, ref->node);
802 jffs2_free_xattr_ref(ref);
807 i = (ref->ino ^ ref->xid) % XREF_TMPHASH_SIZE;
808 for (tmp=xref_tmphash[i]; tmp; tmp=tmp->next) {
809 if (tmp->ino == ref->ino && tmp->xid == ref->xid)
814 if (ref->xseqno > tmp->xseqno) {
815 tmp->xseqno = ref->xseqno;
816 raw->next_in_ino = tmp->node;
819 raw->next_in_ino = tmp->node->next_in_ino;
820 tmp->node->next_in_ino = raw;
822 jffs2_free_xattr_ref(ref);
825 ref->next = xref_tmphash[i];
826 xref_tmphash[i] = ref;
831 /* Phase.2 : Bind xref with inode_cache and xattr_datum */
832 for (i=0; i < XREF_TMPHASH_SIZE; i++) {
833 for (ref=xref_tmphash[i]; ref; ref=_ref) {
836 if (is_xattr_ref_dead(ref)) {
837 ref->next = c->xref_dead_list;
838 c->xref_dead_list = ref;
842 /* At this point, ref->xid and ref->ino contain XID and inode number.
843 ref->xd and ref->ic are not valid yet. */
844 xd = jffs2_find_xattr_datum(c, ref->xid);
845 ic = jffs2_get_ino_cache(c, ref->ino);
846 if (!xd || !ic || !ic->pino_nlink) {
847 dbg_xattr("xref(ino=%u, xid=%u, xseqno=%u) is orphan.\n",
848 ref->ino, ref->xid, ref->xseqno);
849 ref->xseqno |= XREF_DELETE_MARKER;
850 ref->next = c->xref_dead_list;
851 c->xref_dead_list = ref;
857 atomic_inc(&xd->refcnt);
858 ref->next = ic->xref;
863 /* Phase.3 : Link unchecked xdatum to xattr_unchecked list */
864 for (i=0; i < XATTRINDEX_HASHSIZE; i++) {
865 list_for_each_entry_safe(xd, _xd, &c->xattrindex[i], xindex) {
867 list_del_init(&xd->xindex);
868 if (!atomic_read(&xd->refcnt)) {
869 dbg_xattr("xdatum(xid=%u, version=%u) is orphan.\n",
870 xd->xid, xd->version);
871 xd->flags |= JFFS2_XFLAGS_DEAD;
872 list_add(&xd->xindex, &c->xattr_unchecked);
873 xdatum_orphan_count++;
876 if (is_xattr_datum_unchecked(c, xd)) {
877 dbg_xattr("unchecked xdatum(xid=%u, version=%u)\n",
878 xd->xid, xd->version);
879 list_add(&xd->xindex, &c->xattr_unchecked);
880 xdatum_unchecked_count++;
885 JFFS2_NOTICE("complete building xattr subsystem, %u of xdatum"
886 " (%u unchecked, %u orphan) and "
887 "%u of xref (%u dead, %u orphan) found.\n",
888 xdatum_count, xdatum_unchecked_count, xdatum_orphan_count,
889 xref_count, xref_dead_count, xref_orphan_count);
894 struct jffs2_xattr_datum *jffs2_setup_xattr_datum(struct jffs2_sb_info *c,
895 uint32_t xid, uint32_t version)
897 struct jffs2_xattr_datum *xd;
899 xd = jffs2_find_xattr_datum(c, xid);
901 xd = jffs2_alloc_xattr_datum();
903 return ERR_PTR(-ENOMEM);
905 xd->version = version;
906 if (xd->xid > c->highest_xid)
907 c->highest_xid = xd->xid;
908 list_add_tail(&xd->xindex, &c->xattrindex[xid % XATTRINDEX_HASHSIZE]);
913 /* -------- xattr subsystem functions ---------------
914 * xprefix_to_handler(xprefix)
915 * is used to translate xprefix into xattr_handler.
916 * jffs2_listxattr(dentry, buffer, size)
917 * is an implementation of listxattr handler on jffs2.
918 * do_jffs2_getxattr(inode, xprefix, xname, buffer, size)
919 * is an implementation of getxattr handler on jffs2.
920 * do_jffs2_setxattr(inode, xprefix, xname, buffer, size, flags)
921 * is an implementation of setxattr handler on jffs2.
922 * -------------------------------------------------- */
923 const struct xattr_handler *jffs2_xattr_handlers[] = {
924 &jffs2_user_xattr_handler,
925 #ifdef CONFIG_JFFS2_FS_SECURITY
926 &jffs2_security_xattr_handler,
928 &jffs2_trusted_xattr_handler,
932 static const char *jffs2_xattr_prefix(int xprefix, struct dentry *dentry)
934 const struct xattr_handler *ret = NULL;
937 case JFFS2_XPREFIX_USER:
938 ret = &jffs2_user_xattr_handler;
940 #ifdef CONFIG_JFFS2_FS_SECURITY
941 case JFFS2_XPREFIX_SECURITY:
942 ret = &jffs2_security_xattr_handler;
945 #ifdef CONFIG_JFFS2_FS_POSIX_ACL
946 case JFFS2_XPREFIX_ACL_ACCESS:
947 ret = &nop_posix_acl_access;
949 case JFFS2_XPREFIX_ACL_DEFAULT:
950 ret = &nop_posix_acl_default;
953 case JFFS2_XPREFIX_TRUSTED:
954 ret = &jffs2_trusted_xattr_handler;
960 if (!xattr_handler_can_list(ret, dentry))
963 return xattr_prefix(ret);
966 ssize_t jffs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
968 struct inode *inode = d_inode(dentry);
969 struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
970 struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
971 struct jffs2_inode_cache *ic = f->inocache;
972 struct jffs2_xattr_ref *ref, **pref;
973 struct jffs2_xattr_datum *xd;
975 ssize_t prefix_len, len, rc;
978 rc = check_xattr_ref_inode(c, ic);
982 down_read(&c->xattr_sem);
985 for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
986 BUG_ON(ref->ic != ic);
989 /* xdatum is unchached */
992 up_read(&c->xattr_sem);
993 down_write(&c->xattr_sem);
996 rc = load_xattr_datum(c, xd);
997 if (unlikely(rc > 0)) {
999 delete_xattr_ref(c, ref);
1001 } else if (unlikely(rc < 0))
1006 prefix = jffs2_xattr_prefix(xd->xprefix, dentry);
1009 prefix_len = strlen(prefix);
1010 rc = prefix_len + xd->name_len + 1;
1013 if (rc > size - len) {
1017 memcpy(buffer, prefix, prefix_len);
1018 buffer += prefix_len;
1019 memcpy(buffer, xd->xname, xd->name_len);
1020 buffer += xd->name_len;
1028 up_read(&c->xattr_sem);
1030 up_write(&c->xattr_sem);
1035 int do_jffs2_getxattr(struct inode *inode, int xprefix, const char *xname,
1036 char *buffer, size_t size)
1038 struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
1039 struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
1040 struct jffs2_inode_cache *ic = f->inocache;
1041 struct jffs2_xattr_datum *xd;
1042 struct jffs2_xattr_ref *ref, **pref;
1045 rc = check_xattr_ref_inode(c, ic);
1049 down_read(&c->xattr_sem);
1051 for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
1052 BUG_ON(ref->ic!=ic);
1055 if (xd->xprefix != xprefix)
1058 /* xdatum is unchached */
1061 up_read(&c->xattr_sem);
1062 down_write(&c->xattr_sem);
1065 rc = load_xattr_datum(c, xd);
1066 if (unlikely(rc > 0)) {
1068 delete_xattr_ref(c, ref);
1070 } else if (unlikely(rc < 0)) {
1075 if (!strcmp(xname, xd->xname)) {
1081 memcpy(buffer, xd->xvalue, rc);
1090 up_read(&c->xattr_sem);
1092 up_write(&c->xattr_sem);
1097 int do_jffs2_setxattr(struct inode *inode, int xprefix, const char *xname,
1098 const char *buffer, size_t size, int flags)
1100 struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
1101 struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
1102 struct jffs2_inode_cache *ic = f->inocache;
1103 struct jffs2_xattr_datum *xd;
1104 struct jffs2_xattr_ref *ref, *newref, **pref;
1105 uint32_t length, request;
1108 rc = check_xattr_ref_inode(c, ic);
1112 request = PAD(sizeof(struct jffs2_raw_xattr) + strlen(xname) + 1 + size);
1113 rc = jffs2_reserve_space(c, request, &length,
1114 ALLOC_NORMAL, JFFS2_SUMMARY_XATTR_SIZE);
1116 JFFS2_WARNING("jffs2_reserve_space()=%d, request=%u\n", rc, request);
1120 /* Find existing xattr */
1121 down_write(&c->xattr_sem);
1123 for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
1125 if (xd->xprefix != xprefix)
1128 rc = load_xattr_datum(c, xd);
1129 if (unlikely(rc > 0)) {
1131 delete_xattr_ref(c, ref);
1133 } else if (unlikely(rc < 0))
1136 if (!strcmp(xd->xname, xname)) {
1137 if (flags & XATTR_CREATE) {
1144 ref->xseqno |= XREF_DELETE_MARKER;
1145 rc = save_xattr_ref(c, ref);
1148 spin_lock(&c->erase_completion_lock);
1149 ref->next = c->xref_dead_list;
1150 c->xref_dead_list = ref;
1151 spin_unlock(&c->erase_completion_lock);
1152 unrefer_xattr_datum(c, xd);
1156 ref->xseqno &= ~XREF_DELETE_MARKER;
1164 if (flags & XATTR_REPLACE) {
1173 xd = create_xattr_datum(c, xprefix, xname, buffer, size);
1178 up_write(&c->xattr_sem);
1179 jffs2_complete_reservation(c);
1181 /* create xattr_ref */
1182 request = PAD(sizeof(struct jffs2_raw_xref));
1183 rc = jffs2_reserve_space(c, request, &length,
1184 ALLOC_NORMAL, JFFS2_SUMMARY_XREF_SIZE);
1185 down_write(&c->xattr_sem);
1187 JFFS2_WARNING("jffs2_reserve_space()=%d, request=%u\n", rc, request);
1188 unrefer_xattr_datum(c, xd);
1189 up_write(&c->xattr_sem);
1194 newref = create_xattr_ref(c, ic, xd);
1195 if (IS_ERR(newref)) {
1197 ref->next = ic->xref;
1200 rc = PTR_ERR(newref);
1201 unrefer_xattr_datum(c, xd);
1203 delete_xattr_ref(c, ref);
1206 up_write(&c->xattr_sem);
1207 jffs2_complete_reservation(c);
1211 /* -------- garbage collector functions -------------
1212 * jffs2_garbage_collect_xattr_datum(c, xd, raw)
1213 * is used to move xdatum into new node.
1214 * jffs2_garbage_collect_xattr_ref(c, ref, raw)
1215 * is used to move xref into new node.
1216 * jffs2_verify_xattr(c)
1217 * is used to call do_verify_xattr_datum() before garbage collecting.
1218 * jffs2_release_xattr_datum(c, xd)
1219 * is used to release an in-memory object of xdatum.
1220 * jffs2_release_xattr_ref(c, ref)
1221 * is used to release an in-memory object of xref.
1222 * -------------------------------------------------- */
1223 int jffs2_garbage_collect_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd,
1224 struct jffs2_raw_node_ref *raw)
1226 uint32_t totlen, length, old_ofs;
1229 down_write(&c->xattr_sem);
1230 if (xd->node != raw)
1232 if (xd->flags & (JFFS2_XFLAGS_DEAD|JFFS2_XFLAGS_INVALID))
1235 rc = load_xattr_datum(c, xd);
1237 rc = (rc > 0) ? 0 : rc;
1240 old_ofs = ref_offset(xd->node);
1241 totlen = PAD(sizeof(struct jffs2_raw_xattr)
1242 + xd->name_len + 1 + xd->value_len);
1243 rc = jffs2_reserve_space_gc(c, totlen, &length, JFFS2_SUMMARY_XATTR_SIZE);
1245 JFFS2_WARNING("jffs2_reserve_space_gc()=%d, request=%u\n", rc, totlen);
1248 rc = save_xattr_datum(c, xd);
1250 dbg_xattr("xdatum (xid=%u, version=%u) GC'ed from %#08x to %08x\n",
1251 xd->xid, xd->version, old_ofs, ref_offset(xd->node));
1254 jffs2_mark_node_obsolete(c, raw);
1255 up_write(&c->xattr_sem);
1259 int jffs2_garbage_collect_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref,
1260 struct jffs2_raw_node_ref *raw)
1262 uint32_t totlen, length, old_ofs;
1265 down_write(&c->xattr_sem);
1268 if (ref->node != raw)
1270 if (is_xattr_ref_dead(ref) && (raw->next_in_ino == (void *)ref))
1273 old_ofs = ref_offset(ref->node);
1274 totlen = ref_totlen(c, c->gcblock, ref->node);
1276 rc = jffs2_reserve_space_gc(c, totlen, &length, JFFS2_SUMMARY_XREF_SIZE);
1278 JFFS2_WARNING("%s: jffs2_reserve_space_gc() = %d, request = %u\n",
1279 __func__, rc, totlen);
1282 rc = save_xattr_ref(c, ref);
1284 dbg_xattr("xref (ino=%u, xid=%u) GC'ed from %#08x to %08x\n",
1285 ref->ic->ino, ref->xd->xid, old_ofs, ref_offset(ref->node));
1288 jffs2_mark_node_obsolete(c, raw);
1289 up_write(&c->xattr_sem);
1293 int jffs2_verify_xattr(struct jffs2_sb_info *c)
1295 struct jffs2_xattr_datum *xd, *_xd;
1296 struct jffs2_eraseblock *jeb;
1297 struct jffs2_raw_node_ref *raw;
1301 down_write(&c->xattr_sem);
1302 list_for_each_entry_safe(xd, _xd, &c->xattr_unchecked, xindex) {
1303 rc = do_verify_xattr_datum(c, xd);
1306 list_del_init(&xd->xindex);
1307 spin_lock(&c->erase_completion_lock);
1308 for (raw=xd->node; raw != (void *)xd; raw=raw->next_in_ino) {
1309 if (ref_flags(raw) != REF_UNCHECKED)
1311 jeb = &c->blocks[ref_offset(raw) / c->sector_size];
1312 totlen = PAD(ref_totlen(c, jeb, raw));
1313 c->unchecked_size -= totlen; c->used_size += totlen;
1314 jeb->unchecked_size -= totlen; jeb->used_size += totlen;
1315 raw->flash_offset = ref_offset(raw)
1316 | ((xd->node == (void *)raw) ? REF_PRISTINE : REF_NORMAL);
1318 if (xd->flags & JFFS2_XFLAGS_DEAD)
1319 list_add(&xd->xindex, &c->xattr_dead_list);
1320 spin_unlock(&c->erase_completion_lock);
1322 up_write(&c->xattr_sem);
1323 return list_empty(&c->xattr_unchecked) ? 1 : 0;
1326 void jffs2_release_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
1328 /* must be called under spin_lock(&c->erase_completion_lock) */
1329 if (atomic_read(&xd->refcnt) || xd->node != (void *)xd)
1332 list_del(&xd->xindex);
1333 jffs2_free_xattr_datum(xd);
1336 void jffs2_release_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
1338 /* must be called under spin_lock(&c->erase_completion_lock) */
1339 struct jffs2_xattr_ref *tmp, **ptmp;
1341 if (ref->node != (void *)ref)
1344 for (tmp=c->xref_dead_list, ptmp=&c->xref_dead_list; tmp; ptmp=&tmp->next, tmp=tmp->next) {
1350 jffs2_free_xattr_ref(ref);