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 * is_xattr_datum_unchecked(c, xd)
27 * returns 1, if xdatum contains any unchecked raw nodes. if all raw nodes are not
28 * unchecked, it returns 0.
29 * unload_xattr_datum(c, xd)
30 * is used to release xattr name/value pair and detach from c->xattrindex.
31 * reclaim_xattr_datum(c)
32 * is used to reclaim xattr name/value pairs on the xattr name/value pair cache when
33 * memory usage by cache is over c->xdatum_mem_threshold. Currentry, this threshold
34 * is hard coded as 32KiB.
35 * do_verify_xattr_datum(c, xd)
36 * is used to load the xdatum informations without name/value pair from the medium.
37 * It's necessary once, because those informations are not collected during mounting
38 * process when EBS is enabled.
39 * 0 will be returned, if success. An negative return value means recoverable error, and
40 * positive return value means unrecoverable error. Thus, caller must remove this xdatum
41 * and xref when it returned positive value.
42 * do_load_xattr_datum(c, xd)
43 * is used to load name/value pair from the medium.
44 * The meanings of return value is same as do_verify_xattr_datum().
45 * load_xattr_datum(c, xd)
46 * is used to be as a wrapper of do_verify_xattr_datum() and do_load_xattr_datum().
47 * If xd need to call do_verify_xattr_datum() at first, it's called before calling
48 * do_load_xattr_datum(). The meanings of return value is same as do_verify_xattr_datum().
49 * save_xattr_datum(c, xd)
50 * is used to write xdatum to medium. xd->version will be incremented.
51 * create_xattr_datum(c, xprefix, xname, xvalue, xsize)
52 * is used to create new xdatum and write to medium.
53 * delete_xattr_datum_delay(c, xd)
54 * is used to delete a xdatum without 'delete marker'. It has a possibility to detect
55 * orphan xdatum on next mounting.
56 * delete_xattr_datum(c, xd)
57 * is used to delete a xdatum with 'delete marker'. Calling jffs2_reserve_space() is
58 * necessary before this function.
59 * -------------------------------------------------- */
60 static uint32_t xattr_datum_hashkey(int xprefix
, const char *xname
, const char *xvalue
, int xsize
)
62 int name_len
= strlen(xname
);
64 return crc32(xprefix
, xname
, name_len
) ^ crc32(xprefix
, xvalue
, xsize
);
67 static int is_xattr_datum_unchecked(struct jffs2_sb_info
*c
, struct jffs2_xattr_datum
*xd
)
69 struct jffs2_raw_node_ref
*raw
;
72 spin_lock(&c
->erase_completion_lock
);
73 for (raw
=xd
->node
; raw
!= (void *)xd
; raw
=raw
->next_in_ino
) {
74 if (ref_flags(raw
) == REF_UNCHECKED
) {
79 spin_unlock(&c
->erase_completion_lock
);
83 static void unload_xattr_datum(struct jffs2_sb_info
*c
, struct jffs2_xattr_datum
*xd
)
85 /* must be called under down_write(xattr_sem) */
86 D1(dbg_xattr("%s: xid=%u, version=%u\n", __FUNCTION__
, xd
->xid
, xd
->version
));
88 c
->xdatum_mem_usage
-= (xd
->name_len
+ 1 + xd
->value_len
);
92 list_del_init(&xd
->xindex
);
98 static void reclaim_xattr_datum(struct jffs2_sb_info
*c
)
100 /* must be called under down_write(xattr_sem) */
101 struct jffs2_xattr_datum
*xd
, *_xd
;
102 uint32_t target
, before
;
103 static int index
= 0;
106 if (c
->xdatum_mem_threshold
> c
->xdatum_mem_usage
)
109 before
= c
->xdatum_mem_usage
;
110 target
= c
->xdatum_mem_usage
* 4 / 5; /* 20% reduction */
111 for (count
= 0; count
< XATTRINDEX_HASHSIZE
; count
++) {
112 list_for_each_entry_safe(xd
, _xd
, &c
->xattrindex
[index
], xindex
) {
113 if (xd
->flags
& JFFS2_XFLAGS_HOT
) {
114 xd
->flags
&= ~JFFS2_XFLAGS_HOT
;
115 } else if (!(xd
->flags
& JFFS2_XFLAGS_BIND
)) {
116 unload_xattr_datum(c
, xd
);
118 if (c
->xdatum_mem_usage
<= target
)
121 index
= (index
+1) % XATTRINDEX_HASHSIZE
;
124 JFFS2_NOTICE("xdatum_mem_usage from %u byte to %u byte (%u byte reclaimed)\n",
125 before
, c
->xdatum_mem_usage
, before
- c
->xdatum_mem_usage
);
128 static int do_verify_xattr_datum(struct jffs2_sb_info
*c
, struct jffs2_xattr_datum
*xd
)
130 /* must be called under down_write(xattr_sem) */
131 struct jffs2_eraseblock
*jeb
;
132 struct jffs2_raw_node_ref
*raw
;
133 struct jffs2_raw_xattr rx
;
135 uint32_t crc
, offset
, totlen
;
138 spin_lock(&c
->erase_completion_lock
);
139 offset
= ref_offset(xd
->node
);
140 if (ref_flags(xd
->node
) == REF_PRISTINE
)
142 spin_unlock(&c
->erase_completion_lock
);
144 rc
= jffs2_flash_read(c
, offset
, sizeof(rx
), &readlen
, (char *)&rx
);
145 if (rc
|| readlen
!= sizeof(rx
)) {
146 JFFS2_WARNING("jffs2_flash_read()=%d, req=%zu, read=%zu at %#08x\n",
147 rc
, sizeof(rx
), readlen
, offset
);
148 return rc
? rc
: -EIO
;
150 crc
= crc32(0, &rx
, sizeof(rx
) - 4);
151 if (crc
!= je32_to_cpu(rx
.node_crc
)) {
152 JFFS2_ERROR("node CRC failed at %#08x, read=%#08x, calc=%#08x\n",
153 offset
, je32_to_cpu(rx
.hdr_crc
), crc
);
154 xd
->flags
|= JFFS2_XFLAGS_INVALID
;
158 if (xd
->version
!= XDATUM_DELETE_MARKER
)
159 totlen
+= rx
.name_len
+ 1 + je16_to_cpu(rx
.value_len
);
160 totlen
= PAD(totlen
);
161 if (je16_to_cpu(rx
.magic
) != JFFS2_MAGIC_BITMASK
162 || je16_to_cpu(rx
.nodetype
) != JFFS2_NODETYPE_XATTR
163 || je32_to_cpu(rx
.totlen
) != totlen
164 || je32_to_cpu(rx
.xid
) != xd
->xid
165 || je32_to_cpu(rx
.version
) != xd
->version
) {
166 JFFS2_ERROR("inconsistent xdatum at %#08x, magic=%#04x/%#04x, "
167 "nodetype=%#04x/%#04x, totlen=%u/%u, xid=%u/%u, version=%u/%u\n",
168 offset
, je16_to_cpu(rx
.magic
), JFFS2_MAGIC_BITMASK
,
169 je16_to_cpu(rx
.nodetype
), JFFS2_NODETYPE_XATTR
,
170 je32_to_cpu(rx
.totlen
), totlen
,
171 je32_to_cpu(rx
.xid
), xd
->xid
,
172 je32_to_cpu(rx
.version
), xd
->version
);
173 xd
->flags
|= JFFS2_XFLAGS_INVALID
;
176 xd
->xprefix
= rx
.xprefix
;
177 xd
->name_len
= rx
.name_len
;
178 xd
->value_len
= je16_to_cpu(rx
.value_len
);
179 xd
->data_crc
= je32_to_cpu(rx
.data_crc
);
181 spin_lock(&c
->erase_completion_lock
);
183 for (raw
=xd
->node
; raw
!= (void *)xd
; raw
=raw
->next_in_ino
) {
184 jeb
= &c
->blocks
[ref_offset(raw
) / c
->sector_size
];
185 totlen
= PAD(ref_totlen(c
, jeb
, raw
));
186 if (ref_flags(raw
) == REF_UNCHECKED
) {
187 c
->unchecked_size
-= totlen
; c
->used_size
+= totlen
;
188 jeb
->unchecked_size
-= totlen
; jeb
->used_size
+= totlen
;
190 raw
->flash_offset
= ref_offset(raw
) | ((xd
->node
==raw
) ? REF_PRISTINE
: REF_NORMAL
);
192 spin_unlock(&c
->erase_completion_lock
);
194 /* unchecked xdatum is chained with c->xattr_unchecked */
195 list_del_init(&xd
->xindex
);
197 dbg_xattr("success on verfying xdatum (xid=%u, version=%u)\n",
198 xd
->xid
, xd
->version
);
203 static int do_load_xattr_datum(struct jffs2_sb_info
*c
, struct jffs2_xattr_datum
*xd
)
205 /* must be called under down_write(xattr_sem) */
208 uint32_t crc
, length
;
209 int i
, ret
, retry
= 0;
211 BUG_ON(ref_flags(xd
->node
) != REF_PRISTINE
);
212 BUG_ON(!list_empty(&xd
->xindex
));
214 length
= xd
->name_len
+ 1 + xd
->value_len
;
215 data
= kmalloc(length
, GFP_KERNEL
);
219 ret
= jffs2_flash_read(c
, ref_offset(xd
->node
)+sizeof(struct jffs2_raw_xattr
),
220 length
, &readlen
, data
);
222 if (ret
|| length
!=readlen
) {
223 JFFS2_WARNING("jffs2_flash_read() returned %d, request=%d, readlen=%zu, at %#08x\n",
224 ret
, length
, readlen
, ref_offset(xd
->node
));
226 return ret
? ret
: -EIO
;
229 data
[xd
->name_len
] = '\0';
230 crc
= crc32(0, data
, length
);
231 if (crc
!= xd
->data_crc
) {
232 JFFS2_WARNING("node CRC failed (JFFS2_NODETYPE_XREF)"
233 " at %#08x, read: 0x%08x calculated: 0x%08x\n",
234 ref_offset(xd
->node
), xd
->data_crc
, crc
);
236 xd
->flags
|= JFFS2_XFLAGS_INVALID
;
240 xd
->flags
|= JFFS2_XFLAGS_HOT
;
242 xd
->xvalue
= data
+ xd
->name_len
+1;
244 c
->xdatum_mem_usage
+= length
;
246 xd
->hashkey
= xattr_datum_hashkey(xd
->xprefix
, xd
->xname
, xd
->xvalue
, xd
->value_len
);
247 i
= xd
->hashkey
% XATTRINDEX_HASHSIZE
;
248 list_add(&xd
->xindex
, &c
->xattrindex
[i
]);
251 reclaim_xattr_datum(c
);
256 dbg_xattr("success on loading xdatum (xid=%u, xprefix=%u, xname='%s')\n",
257 xd
->xid
, xd
->xprefix
, xd
->xname
);
262 static int load_xattr_datum(struct jffs2_sb_info
*c
, struct jffs2_xattr_datum
*xd
)
264 /* must be called under down_write(xattr_sem);
265 * rc < 0 : recoverable error, try again
267 * rc > 0 : Unrecoverable error, this node should be deleted.
273 if (xd
->flags
& JFFS2_XFLAGS_INVALID
)
275 if (unlikely(is_xattr_datum_unchecked(c
, xd
)))
276 rc
= do_verify_xattr_datum(c
, xd
);
278 rc
= do_load_xattr_datum(c
, xd
);
282 static int save_xattr_datum(struct jffs2_sb_info
*c
, struct jffs2_xattr_datum
*xd
)
284 /* must be called under down_write(xattr_sem) */
285 struct jffs2_raw_xattr rx
;
288 int rc
, totlen
, nvecs
= 1;
289 uint32_t phys_ofs
= write_ofs(c
);
291 BUG_ON(is_xattr_datum_dead(xd
) || (xd
->flags
& JFFS2_XFLAGS_INVALID
)
292 ? !!xd
->xname
: !xd
->xname
);
294 vecs
[0].iov_base
= &rx
;
295 vecs
[0].iov_len
= totlen
= sizeof(rx
);
296 if (!is_xattr_datum_dead(xd
) && !(xd
->flags
& JFFS2_XFLAGS_INVALID
)) {
298 vecs
[1].iov_base
= xd
->xname
;
299 vecs
[1].iov_len
= xd
->name_len
+ 1 + xd
->value_len
;
300 totlen
+= 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 if (!is_xattr_datum_dead(xd
) && !(xd
->flags
& JFFS2_XFLAGS_INVALID
)) {
311 rx
.version
= cpu_to_je32(++xd
->version
);
312 rx
.xprefix
= xd
->xprefix
;
313 rx
.name_len
= xd
->name_len
;
314 rx
.value_len
= cpu_to_je16(xd
->value_len
);
315 rx
.data_crc
= cpu_to_je32(crc32(0, vecs
[1].iov_base
, vecs
[1].iov_len
));
317 rx
.version
= cpu_to_je32(XDATUM_DELETE_MARKER
);
319 rx
.node_crc
= cpu_to_je32(crc32(0, &rx
, sizeof(struct jffs2_raw_xattr
) - 4));
321 rc
= jffs2_flash_writev(c
, vecs
, nvecs
, phys_ofs
, &length
, 0);
322 if (rc
|| totlen
!= length
) {
323 JFFS2_WARNING("jffs2_flash_writev()=%d, req=%u, wrote=%zu, at %#08x\n",
324 rc
, totlen
, length
, phys_ofs
);
327 jffs2_add_physical_node_ref(c
, phys_ofs
| REF_OBSOLETE
, PAD(totlen
), NULL
);
332 jffs2_add_physical_node_ref(c
, phys_ofs
| REF_PRISTINE
, PAD(totlen
), (void *)xd
);
334 dbg_xattr("success on saving xdatum (xid=%u, version=%u, xprefix=%u, xname='%s')\n",
335 xd
->xid
, xd
->version
, xd
->xprefix
, xd
->xname
);
340 static struct jffs2_xattr_datum
*create_xattr_datum(struct jffs2_sb_info
*c
,
341 int xprefix
, const char *xname
,
342 const char *xvalue
, int xsize
)
344 /* must be called under down_write(xattr_sem) */
345 struct jffs2_xattr_datum
*xd
;
346 uint32_t hashkey
, name_len
;
350 /* Search xattr_datum has same xname/xvalue by index */
351 hashkey
= xattr_datum_hashkey(xprefix
, xname
, xvalue
, xsize
);
352 i
= hashkey
% XATTRINDEX_HASHSIZE
;
353 list_for_each_entry(xd
, &c
->xattrindex
[i
], xindex
) {
354 if (xd
->hashkey
==hashkey
355 && xd
->xprefix
==xprefix
356 && xd
->value_len
==xsize
357 && !strcmp(xd
->xname
, xname
)
358 && !memcmp(xd
->xvalue
, xvalue
, xsize
)) {
364 /* Not found, Create NEW XATTR-Cache */
365 name_len
= strlen(xname
);
367 xd
= jffs2_alloc_xattr_datum();
369 return ERR_PTR(-ENOMEM
);
371 data
= kmalloc(name_len
+ 1 + xsize
, GFP_KERNEL
);
373 jffs2_free_xattr_datum(xd
);
374 return ERR_PTR(-ENOMEM
);
377 memcpy(data
+ name_len
+ 1, xvalue
, xsize
);
380 xd
->xid
= ++c
->highest_xid
;
381 xd
->flags
|= JFFS2_XFLAGS_HOT
;
382 xd
->xprefix
= xprefix
;
384 xd
->hashkey
= hashkey
;
386 xd
->xvalue
= data
+ name_len
+ 1;
387 xd
->name_len
= name_len
;
388 xd
->value_len
= xsize
;
389 xd
->data_crc
= crc32(0, data
, xd
->name_len
+ 1 + xd
->value_len
);
391 rc
= save_xattr_datum(c
, xd
);
394 jffs2_free_xattr_datum(xd
);
398 /* Insert Hash Index */
399 i
= hashkey
% XATTRINDEX_HASHSIZE
;
400 list_add(&xd
->xindex
, &c
->xattrindex
[i
]);
402 c
->xdatum_mem_usage
+= (xd
->name_len
+ 1 + xd
->value_len
);
403 reclaim_xattr_datum(c
);
408 static void delete_xattr_datum_delay(struct jffs2_sb_info
*c
, struct jffs2_xattr_datum
*xd
)
410 /* must be called under down_write(xattr_sem) */
413 unload_xattr_datum(c
, xd
);
414 set_xattr_datum_dead(xd
);
415 spin_lock(&c
->erase_completion_lock
);
416 list_add(&xd
->xindex
, &c
->xattr_dead_list
);
417 spin_unlock(&c
->erase_completion_lock
);
418 JFFS2_NOTICE("xdatum(xid=%u) was removed without delete marker. "
419 "An orphan xdatum may be detected on next mounting.\n", xd
->xid
);
422 static void delete_xattr_datum(struct jffs2_sb_info
*c
, struct jffs2_xattr_datum
*xd
)
424 /* must be called under jffs2_reserve_space() and down_write(xattr_sem) */
428 unload_xattr_datum(c
, xd
);
429 set_xattr_datum_dead(xd
);
430 rc
= save_xattr_datum(c
, xd
);
432 JFFS2_NOTICE("xdatum(xid=%u) was removed without delete marker. "
433 "An orphan xdatum may be detected on next mounting.\n",
436 spin_lock(&c
->erase_completion_lock
);
437 list_add(&xd
->xindex
, &c
->xattr_dead_list
);
438 spin_unlock(&c
->erase_completion_lock
);
441 /* -------- xref 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)
452 * is used to write xref to medium.
453 * create_xattr_ref(c, ic, xd)
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.
459 * check_xattr_ref_inode(c, ic)
460 * is used to confirm inode does not have duplicate xattr name/value pair.
461 * -------------------------------------------------- */
462 static int verify_xattr_ref(struct jffs2_sb_info
*c
, struct jffs2_xattr_ref
*ref
)
464 struct jffs2_eraseblock
*jeb
;
465 struct jffs2_raw_node_ref
*raw
;
466 struct jffs2_raw_xref rr
;
468 uint32_t crc
, offset
, totlen
;
471 spin_lock(&c
->erase_completion_lock
);
472 if (ref_flags(ref
->node
) != REF_UNCHECKED
)
474 offset
= ref_offset(ref
->node
);
475 spin_unlock(&c
->erase_completion_lock
);
477 rc
= jffs2_flash_read(c
, offset
, sizeof(rr
), &readlen
, (char *)&rr
);
478 if (rc
|| sizeof(rr
) != readlen
) {
479 JFFS2_WARNING("jffs2_flash_read()=%d, req=%zu, read=%zu, at %#08x\n",
480 rc
, sizeof(rr
), readlen
, offset
);
481 return rc
? rc
: -EIO
;
484 crc
= crc32(0, &rr
, sizeof(rr
) - 4);
485 if (crc
!= je32_to_cpu(rr
.node_crc
)) {
486 JFFS2_ERROR("node CRC failed at %#08x, read=%#08x, calc=%#08x\n",
487 offset
, je32_to_cpu(rr
.node_crc
), crc
);
490 if (je16_to_cpu(rr
.magic
) != JFFS2_MAGIC_BITMASK
491 || je16_to_cpu(rr
.nodetype
) != JFFS2_NODETYPE_XREF
492 || je32_to_cpu(rr
.totlen
) != PAD(sizeof(rr
))) {
493 JFFS2_ERROR("inconsistent xref at %#08x, magic=%#04x/%#04x, "
494 "nodetype=%#04x/%#04x, totlen=%u/%zu\n",
495 offset
, je16_to_cpu(rr
.magic
), JFFS2_MAGIC_BITMASK
,
496 je16_to_cpu(rr
.nodetype
), JFFS2_NODETYPE_XREF
,
497 je32_to_cpu(rr
.totlen
), PAD(sizeof(rr
)));
500 ref
->ino
= je32_to_cpu(rr
.ino
);
501 ref
->xid
= je32_to_cpu(rr
.xid
);
502 ref
->xseqno
= je32_to_cpu(rr
.xseqno
);
503 if (ref
->xseqno
> c
->highest_xseqno
)
504 c
->highest_xseqno
= (ref
->xseqno
& ~XREF_DELETE_MARKER
);
506 spin_lock(&c
->erase_completion_lock
);
508 for (raw
=ref
->node
; raw
!= (void *)ref
; raw
=raw
->next_in_ino
) {
509 jeb
= &c
->blocks
[ref_offset(raw
) / c
->sector_size
];
510 totlen
= PAD(ref_totlen(c
, jeb
, raw
));
511 if (ref_flags(raw
) == REF_UNCHECKED
) {
512 c
->unchecked_size
-= totlen
; c
->used_size
+= totlen
;
513 jeb
->unchecked_size
-= totlen
; jeb
->used_size
+= totlen
;
515 raw
->flash_offset
= ref_offset(raw
) | ((ref
->node
==raw
) ? REF_PRISTINE
: REF_NORMAL
);
517 spin_unlock(&c
->erase_completion_lock
);
519 dbg_xattr("success on verifying xref (ino=%u, xid=%u) at %#08x\n",
520 ref
->ino
, ref
->xid
, ref_offset(ref
->node
));
524 static int save_xattr_ref(struct jffs2_sb_info
*c
, struct jffs2_xattr_ref
*ref
)
526 /* must be called under down_write(xattr_sem) */
527 struct jffs2_raw_xref rr
;
529 uint32_t xseqno
, phys_ofs
= write_ofs(c
);
532 rr
.magic
= cpu_to_je16(JFFS2_MAGIC_BITMASK
);
533 rr
.nodetype
= cpu_to_je16(JFFS2_NODETYPE_XREF
);
534 rr
.totlen
= cpu_to_je32(PAD(sizeof(rr
)));
535 rr
.hdr_crc
= cpu_to_je32(crc32(0, &rr
, sizeof(struct jffs2_unknown_node
) - 4));
537 xseqno
= (c
->highest_xseqno
+= 2);
538 if (is_xattr_ref_dead(ref
)) {
539 xseqno
|= XREF_DELETE_MARKER
;
540 rr
.ino
= cpu_to_je32(ref
->ino
);
541 rr
.xid
= cpu_to_je32(ref
->xid
);
543 rr
.ino
= cpu_to_je32(ref
->ic
->ino
);
544 rr
.xid
= cpu_to_je32(ref
->xd
->xid
);
546 rr
.xseqno
= cpu_to_je32(xseqno
);
547 rr
.node_crc
= cpu_to_je32(crc32(0, &rr
, sizeof(rr
) - 4));
549 ret
= jffs2_flash_write(c
, phys_ofs
, sizeof(rr
), &length
, (char *)&rr
);
550 if (ret
|| sizeof(rr
) != length
) {
551 JFFS2_WARNING("jffs2_flash_write() returned %d, request=%zu, retlen=%zu, at %#08x\n",
552 ret
, sizeof(rr
), length
, phys_ofs
);
553 ret
= ret
? ret
: -EIO
;
555 jffs2_add_physical_node_ref(c
, phys_ofs
| REF_OBSOLETE
, PAD(sizeof(rr
)), NULL
);
560 ref
->xseqno
= xseqno
;
561 jffs2_add_physical_node_ref(c
, phys_ofs
| REF_PRISTINE
, PAD(sizeof(rr
)), (void *)ref
);
563 dbg_xattr("success on saving xref (ino=%u, xid=%u)\n", ref
->ic
->ino
, ref
->xd
->xid
);
568 static struct jffs2_xattr_ref
*create_xattr_ref(struct jffs2_sb_info
*c
, struct jffs2_inode_cache
*ic
,
569 struct jffs2_xattr_datum
*xd
)
571 /* must be called under down_write(xattr_sem) */
572 struct jffs2_xattr_ref
*ref
;
575 ref
= jffs2_alloc_xattr_ref();
577 return ERR_PTR(-ENOMEM
);
581 ret
= save_xattr_ref(c
, ref
);
583 jffs2_free_xattr_ref(ref
);
588 ref
->next
= ic
->xref
;
591 return ref
; /* success */
594 static void delete_xattr_ref_delay(struct jffs2_sb_info
*c
, struct jffs2_xattr_ref
*ref
)
596 /* must be called under down_write(xattr_sem) */
597 struct jffs2_xattr_datum
*xd
;
599 set_xattr_ref_dead(ref
);
601 ref
->ino
= ref
->ic
->ino
;
602 ref
->xid
= ref
->xd
->xid
;
603 spin_lock(&c
->erase_completion_lock
);
604 ref
->next
= c
->xref_dead_list
;
605 c
->xref_dead_list
= ref
;
606 spin_unlock(&c
->erase_completion_lock
);
608 JFFS2_NOTICE("xref(ino=%u, xid=%u) was removed without delete marker. "
609 "An orphan xref may be detected on next mounting.\n",
613 delete_xattr_datum_delay(c
, xd
);
616 static int delete_xattr_ref(struct jffs2_sb_info
*c
, struct jffs2_xattr_ref
*ref
, int enforce
)
618 /* must be called under jffs2_reserve_space() and down_write(xattr_sem) */
619 struct jffs2_inode_cache
*ic
;
620 struct jffs2_xattr_datum
*xd
;
624 set_xattr_ref_dead(ref
);
629 rc
= save_xattr_ref(c
, ref
);
632 clr_xattr_ref_dead(ref
);
637 JFFS2_WARNING("could not write delete marker of xref(ino=%u, xid=%u). "
638 "An orphan xref may be detected on next mounting.\n",
639 ref
->ic
->ino
, ref
->xd
->xid
);
641 spin_lock(&c
->erase_completion_lock
);
642 ref
->next
= c
->xref_dead_list
;
643 c
->xref_dead_list
= ref
;
644 spin_unlock(&c
->erase_completion_lock
);
651 unload_xattr_datum(c
, xd
);
652 up_write(&c
->xattr_sem
);
653 jffs2_complete_reservation(c
);
655 rc
= jffs2_reserve_space(c
, PAD(sizeof(struct jffs2_raw_xattr
)), &length
,
656 ALLOC_DELETION
, JFFS2_SUMMARY_XATTR_SIZE
);
659 down_write(&c
->xattr_sem
);
660 delete_xattr_datum_delay(c
, xd
);
662 down_write(&c
->xattr_sem
);
663 delete_xattr_datum(c
, xd
);
668 void jffs2_xattr_delete_inode(struct jffs2_sb_info
*c
, struct jffs2_inode_cache
*ic
)
670 /* It's called from jffs2_clear_inode() on inode removing.
671 When an inode with XATTR is removed, those XATTRs must be removed. */
672 struct jffs2_xattr_ref
*ref
;
676 if (!ic
|| ic
->nlink
> 0)
679 down_read(&c
->xattr_sem
);
681 up_read(&c
->xattr_sem
);
684 up_read(&c
->xattr_sem
);
686 rc
= jffs2_reserve_space(c
, PAD(sizeof(struct jffs2_raw_xref
)), &length
,
687 ALLOC_DELETION
, JFFS2_SUMMARY_XREF_SIZE
);
688 down_write(&c
->xattr_sem
);
691 ic
->xref
= ref
->next
;
693 delete_xattr_ref_delay(c
, ref
);
695 delete_xattr_ref(c
, ref
, 1);
698 retry
= ic
->xref
? 1 : 0;
699 up_write(&c
->xattr_sem
);
701 jffs2_complete_reservation(c
);
706 void jffs2_xattr_free_inode(struct jffs2_sb_info
*c
, struct jffs2_inode_cache
*ic
)
708 /* It's called from jffs2_free_ino_caches() until unmounting FS. */
709 struct jffs2_xattr_datum
*xd
;
710 struct jffs2_xattr_ref
*ref
, *_ref
;
712 down_write(&c
->xattr_sem
);
713 for (ref
= ic
->xref
; ref
; ref
= _ref
) {
718 unload_xattr_datum(c
, xd
);
719 jffs2_free_xattr_datum(xd
);
721 jffs2_free_xattr_ref(ref
);
724 up_write(&c
->xattr_sem
);
727 static int check_xattr_ref_inode(struct jffs2_sb_info
*c
, struct jffs2_inode_cache
*ic
)
729 /* success of check_xattr_ref_inode() means taht inode (ic) dose not have
730 * duplicate name/value pairs. If duplicate name/value pair would be found,
731 * one will be removed.
733 struct jffs2_xattr_ref
*ref
, *cmp
, **pref
, **pcmp
;
736 if (likely(ic
->flags
& INO_FLAGS_XATTR_CHECKED
))
738 down_write(&c
->xattr_sem
);
741 for (ref
=ic
->xref
, pref
=&ic
->xref
; ref
; pref
=&ref
->next
, ref
=ref
->next
) {
742 if (!ref
->xd
->xname
) {
743 rc
= load_xattr_datum(c
, ref
->xd
);
744 if (unlikely(rc
> 0)) {
746 delete_xattr_ref_delay(c
, ref
);
748 } else if (unlikely(rc
< 0))
751 for (cmp
=ref
->next
, pcmp
=&ref
->next
; cmp
; pcmp
=&cmp
->next
, cmp
=cmp
->next
) {
752 if (!cmp
->xd
->xname
) {
753 ref
->xd
->flags
|= JFFS2_XFLAGS_BIND
;
754 rc
= load_xattr_datum(c
, cmp
->xd
);
755 ref
->xd
->flags
&= ~JFFS2_XFLAGS_BIND
;
756 if (unlikely(rc
> 0)) {
758 delete_xattr_ref_delay(c
, cmp
);
760 } else if (unlikely(rc
< 0))
763 if (ref
->xd
->xprefix
== cmp
->xd
->xprefix
764 && !strcmp(ref
->xd
->xname
, cmp
->xd
->xname
)) {
765 if (ref
->xseqno
> cmp
->xseqno
) {
767 delete_xattr_ref_delay(c
, cmp
);
770 delete_xattr_ref_delay(c
, ref
);
776 ic
->flags
|= INO_FLAGS_XATTR_CHECKED
;
778 up_write(&c
->xattr_sem
);
783 /* -------- xattr subsystem functions ---------------
784 * jffs2_init_xattr_subsystem(c)
785 * is used to initialize semaphore and list_head, and some variables.
786 * jffs2_find_xattr_datum(c, xid)
787 * is used to lookup xdatum while scanning process.
788 * jffs2_clear_xattr_subsystem(c)
789 * is used to release any xattr related objects.
790 * jffs2_build_xattr_subsystem(c)
791 * is used to associate xdatum and xref while super block building process.
792 * jffs2_setup_xattr_datum(c, xid, version)
793 * is used to insert xdatum while scanning process.
794 * -------------------------------------------------- */
795 void jffs2_init_xattr_subsystem(struct jffs2_sb_info
*c
)
799 for (i
=0; i
< XATTRINDEX_HASHSIZE
; i
++)
800 INIT_LIST_HEAD(&c
->xattrindex
[i
]);
801 INIT_LIST_HEAD(&c
->xattr_unchecked
);
802 INIT_LIST_HEAD(&c
->xattr_dead_list
);
803 c
->xref_dead_list
= NULL
;
806 init_rwsem(&c
->xattr_sem
);
808 c
->highest_xseqno
= 0;
809 c
->xdatum_mem_usage
= 0;
810 c
->xdatum_mem_threshold
= 32 * 1024; /* Default 32KB */
813 static struct jffs2_xattr_datum
*jffs2_find_xattr_datum(struct jffs2_sb_info
*c
, uint32_t xid
)
815 struct jffs2_xattr_datum
*xd
;
816 int i
= xid
% XATTRINDEX_HASHSIZE
;
818 /* It's only used in scanning/building process. */
819 BUG_ON(!(c
->flags
& (JFFS2_SB_FLAG_SCANNING
|JFFS2_SB_FLAG_BUILDING
)));
821 list_for_each_entry(xd
, &c
->xattrindex
[i
], xindex
) {
828 void jffs2_clear_xattr_subsystem(struct jffs2_sb_info
*c
)
830 struct jffs2_xattr_datum
*xd
, *_xd
;
831 struct jffs2_xattr_ref
*ref
, *_ref
;
834 for (ref
=c
->xref_temp
; ref
; ref
= _ref
) {
836 jffs2_free_xattr_ref(ref
);
839 for (ref
=c
->xref_dead_list
; ref
; ref
= _ref
) {
841 jffs2_free_xattr_ref(ref
);
844 for (i
=0; i
< XATTRINDEX_HASHSIZE
; i
++) {
845 list_for_each_entry_safe(xd
, _xd
, &c
->xattrindex
[i
], xindex
) {
846 list_del(&xd
->xindex
);
849 jffs2_free_xattr_datum(xd
);
853 list_for_each_entry_safe(xd
, _xd
, &c
->xattr_dead_list
, xindex
) {
854 list_del(&xd
->xindex
);
855 jffs2_free_xattr_datum(xd
);
859 #define XREF_TMPHASH_SIZE (128)
860 void jffs2_build_xattr_subsystem(struct jffs2_sb_info
*c
)
862 struct jffs2_xattr_ref
*ref
, *_ref
;
863 struct jffs2_xattr_ref
*xref_tmphash
[XREF_TMPHASH_SIZE
];
864 struct jffs2_xattr_datum
*xd
, *_xd
;
865 struct jffs2_inode_cache
*ic
;
866 struct jffs2_raw_node_ref
*raw
;
867 int i
, xdatum_count
= 0, xdatum_unchecked_count
= 0, xref_count
= 0;
869 BUG_ON(!(c
->flags
& JFFS2_SB_FLAG_BUILDING
));
870 /* Phase.1 : Drop dead xdatum */
871 for (i
=0; i
< XATTRINDEX_HASHSIZE
; i
++) {
872 list_for_each_entry_safe(xd
, _xd
, &c
->xattrindex
[i
], xindex
) {
873 BUG_ON(xd
->node
== (void *)xd
);
874 if (is_xattr_datum_dead(xd
)) {
875 list_del_init(&xd
->xindex
);
876 list_add(&xd
->xindex
, &c
->xattr_unchecked
);
881 /* Phase.2 : Merge same xref */
882 for (i
=0; i
< XREF_TMPHASH_SIZE
; i
++)
883 xref_tmphash
[i
] = NULL
;
884 for (ref
=c
->xref_temp
; ref
; ref
=_ref
) {
885 struct jffs2_xattr_ref
*tmp
;
888 if (ref_flags(ref
->node
) != REF_PRISTINE
) {
889 if (verify_xattr_ref(c
, ref
)) {
890 BUG_ON(ref
->node
->next_in_ino
!= (void *)ref
);
891 ref
->node
->next_in_ino
= NULL
;
892 jffs2_mark_node_obsolete(c
, ref
->node
);
893 jffs2_free_xattr_ref(ref
);
898 i
= (ref
->ino
^ ref
->xid
) % XREF_TMPHASH_SIZE
;
899 for (tmp
=xref_tmphash
[i
]; tmp
; tmp
=tmp
->next
) {
900 if (tmp
->ino
== ref
->ino
&& tmp
->xid
== ref
->xid
)
905 if (ref
->xseqno
> tmp
->xseqno
) {
906 tmp
->xseqno
= ref
->xseqno
;
907 raw
->next_in_ino
= tmp
->node
;
910 raw
->next_in_ino
= tmp
->node
->next_in_ino
;
911 tmp
->node
->next_in_ino
= raw
;
913 jffs2_free_xattr_ref(ref
);
916 ref
->next
= xref_tmphash
[i
];
917 xref_tmphash
[i
] = ref
;
922 /* Phase.3 : Bind xref with inode_cache and xattr_datum */
923 for (i
=0; i
< XREF_TMPHASH_SIZE
; i
++) {
924 for (ref
=xref_tmphash
[i
]; ref
; ref
=_ref
) {
926 if (is_xattr_ref_dead(ref
)) {
927 ref
->next
= c
->xref_dead_list
;
928 c
->xref_dead_list
= ref
;
931 /* At this point, ref->xid and ref->ino contain XID and inode number.
932 ref->xd and ref->ic are not valid yet. */
933 xd
= jffs2_find_xattr_datum(c
, ref
->xid
);
934 ic
= jffs2_get_ino_cache(c
, ref
->ino
);
936 JFFS2_WARNING("xref(ino=%u, xid=%u, xseqno=%u) is orphan. \n",
937 ref
->ino
, ref
->xid
, ref
->xseqno
);
938 set_xattr_ref_dead(ref
);
939 ref
->next
= c
->xref_dead_list
;
940 c
->xref_dead_list
= ref
;
946 ref
->next
= ic
->xref
;
952 /* Phase.4 : Link unchecked xdatum to xattr_unchecked list */
953 for (i
=0; i
< XATTRINDEX_HASHSIZE
; i
++) {
954 list_for_each_entry_safe(xd
, _xd
, &c
->xattrindex
[i
], xindex
) {
955 list_del_init(&xd
->xindex
);
957 JFFS2_WARNING("orphan xdatum(xid=%u, version=%u)\n",
958 xd
->xid
, xd
->version
);
959 set_xattr_datum_dead(xd
);
960 list_add(&xd
->xindex
, &c
->xattr_unchecked
);
963 if (is_xattr_datum_unchecked(c
, xd
)) {
964 dbg_xattr("unchecked xdatum(xid=%u, version=%u)\n",
965 xd
->xid
, xd
->version
);
966 list_add(&xd
->xindex
, &c
->xattr_unchecked
);
967 xdatum_unchecked_count
++;
973 JFFS2_NOTICE("complete building xattr subsystem, %u of xdatum (%u unchecked) and "
974 "%u of xref found.\n", xdatum_count
, xdatum_unchecked_count
, xref_count
);
977 struct jffs2_xattr_datum
*jffs2_setup_xattr_datum(struct jffs2_sb_info
*c
,
978 uint32_t xid
, uint32_t version
)
980 struct jffs2_xattr_datum
*xd
;
982 xd
= jffs2_find_xattr_datum(c
, xid
);
984 xd
= jffs2_alloc_xattr_datum();
986 return ERR_PTR(-ENOMEM
);
988 xd
->version
= version
;
989 if (xd
->xid
> c
->highest_xid
)
990 c
->highest_xid
= xd
->xid
;
991 list_add_tail(&xd
->xindex
, &c
->xattrindex
[xid
% XATTRINDEX_HASHSIZE
]);
996 /* -------- xattr subsystem functions ---------------
997 * xprefix_to_handler(xprefix)
998 * is used to translate xprefix into xattr_handler.
999 * jffs2_listxattr(dentry, buffer, size)
1000 * is an implementation of listxattr handler on jffs2.
1001 * do_jffs2_getxattr(inode, xprefix, xname, buffer, size)
1002 * is an implementation of getxattr handler on jffs2.
1003 * do_jffs2_setxattr(inode, xprefix, xname, buffer, size, flags)
1004 * is an implementation of setxattr handler on jffs2.
1005 * -------------------------------------------------- */
1006 struct xattr_handler
*jffs2_xattr_handlers
[] = {
1007 &jffs2_user_xattr_handler
,
1008 #ifdef CONFIG_JFFS2_FS_SECURITY
1009 &jffs2_security_xattr_handler
,
1011 #ifdef CONFIG_JFFS2_FS_POSIX_ACL
1012 &jffs2_acl_access_xattr_handler
,
1013 &jffs2_acl_default_xattr_handler
,
1015 &jffs2_trusted_xattr_handler
,
1019 static struct xattr_handler
*xprefix_to_handler(int xprefix
) {
1020 struct xattr_handler
*ret
;
1023 case JFFS2_XPREFIX_USER
:
1024 ret
= &jffs2_user_xattr_handler
;
1026 #ifdef CONFIG_JFFS2_FS_SECURITY
1027 case JFFS2_XPREFIX_SECURITY
:
1028 ret
= &jffs2_security_xattr_handler
;
1031 #ifdef CONFIG_JFFS2_FS_POSIX_ACL
1032 case JFFS2_XPREFIX_ACL_ACCESS
:
1033 ret
= &jffs2_acl_access_xattr_handler
;
1035 case JFFS2_XPREFIX_ACL_DEFAULT
:
1036 ret
= &jffs2_acl_default_xattr_handler
;
1039 case JFFS2_XPREFIX_TRUSTED
:
1040 ret
= &jffs2_trusted_xattr_handler
;
1049 ssize_t
jffs2_listxattr(struct dentry
*dentry
, char *buffer
, size_t size
)
1051 struct inode
*inode
= dentry
->d_inode
;
1052 struct jffs2_inode_info
*f
= JFFS2_INODE_INFO(inode
);
1053 struct jffs2_sb_info
*c
= JFFS2_SB_INFO(inode
->i_sb
);
1054 struct jffs2_inode_cache
*ic
= f
->inocache
;
1055 struct jffs2_xattr_ref
*ref
, **pref
;
1056 struct jffs2_xattr_datum
*xd
;
1057 struct xattr_handler
*xhandle
;
1061 rc
= check_xattr_ref_inode(c
, ic
);
1065 down_read(&c
->xattr_sem
);
1068 for (ref
=ic
->xref
, pref
=&ic
->xref
; ref
; pref
=&ref
->next
, ref
=ref
->next
) {
1069 BUG_ON(ref
->ic
!= ic
);
1072 /* xdatum is unchached */
1075 up_read(&c
->xattr_sem
);
1076 down_write(&c
->xattr_sem
);
1079 rc
= load_xattr_datum(c
, xd
);
1080 if (unlikely(rc
> 0)) {
1082 delete_xattr_ref_delay(c
, ref
);
1084 } else if (unlikely(rc
< 0))
1088 xhandle
= xprefix_to_handler(xd
->xprefix
);
1092 rc
= xhandle
->list(inode
, buffer
+len
, size
-len
, xd
->xname
, xd
->name_len
);
1094 rc
= xhandle
->list(inode
, NULL
, 0, xd
->xname
, xd
->name_len
);
1103 up_read(&c
->xattr_sem
);
1105 up_write(&c
->xattr_sem
);
1110 int do_jffs2_getxattr(struct inode
*inode
, int xprefix
, const char *xname
,
1111 char *buffer
, size_t size
)
1113 struct jffs2_inode_info
*f
= JFFS2_INODE_INFO(inode
);
1114 struct jffs2_sb_info
*c
= JFFS2_SB_INFO(inode
->i_sb
);
1115 struct jffs2_inode_cache
*ic
= f
->inocache
;
1116 struct jffs2_xattr_datum
*xd
;
1117 struct jffs2_xattr_ref
*ref
, **pref
;
1120 rc
= check_xattr_ref_inode(c
, ic
);
1124 down_read(&c
->xattr_sem
);
1126 for (ref
=ic
->xref
, pref
=&ic
->xref
; ref
; pref
=&ref
->next
, ref
=ref
->next
) {
1127 BUG_ON(ref
->ic
!=ic
);
1130 if (xd
->xprefix
!= xprefix
)
1133 /* xdatum is unchached */
1136 up_read(&c
->xattr_sem
);
1137 down_write(&c
->xattr_sem
);
1140 rc
= load_xattr_datum(c
, xd
);
1141 if (unlikely(rc
> 0)) {
1143 delete_xattr_ref_delay(c
, ref
);
1145 } else if (unlikely(rc
< 0)) {
1150 if (!strcmp(xname
, xd
->xname
)) {
1156 memcpy(buffer
, xd
->xvalue
, rc
);
1165 up_read(&c
->xattr_sem
);
1167 up_write(&c
->xattr_sem
);
1172 int do_jffs2_setxattr(struct inode
*inode
, int xprefix
, const char *xname
,
1173 const char *buffer
, size_t size
, int flags
)
1175 struct jffs2_inode_info
*f
= JFFS2_INODE_INFO(inode
);
1176 struct jffs2_sb_info
*c
= JFFS2_SB_INFO(inode
->i_sb
);
1177 struct jffs2_inode_cache
*ic
= f
->inocache
;
1178 struct jffs2_xattr_datum
*xd
;
1179 struct jffs2_xattr_ref
*ref
, *newref
, **pref
;
1180 uint32_t length
, request
;
1183 rc
= check_xattr_ref_inode(c
, ic
);
1187 request
= PAD(sizeof(struct jffs2_raw_xattr
) + strlen(xname
) + 1 + size
);
1188 rc
= jffs2_reserve_space(c
, request
, &length
,
1189 ALLOC_NORMAL
, JFFS2_SUMMARY_XATTR_SIZE
);
1191 JFFS2_WARNING("jffs2_reserve_space()=%d, request=%u\n", rc
, request
);
1195 /* Find existing xattr */
1196 down_write(&c
->xattr_sem
);
1198 for (ref
=ic
->xref
, pref
=&ic
->xref
; ref
; pref
=&ref
->next
, ref
=ref
->next
) {
1200 if (xd
->xprefix
!= xprefix
)
1203 rc
= load_xattr_datum(c
, xd
);
1204 if (unlikely(rc
> 0)) {
1206 delete_xattr_ref_delay(c
, ref
);
1208 } else if (unlikely(rc
< 0))
1211 if (!strcmp(xd
->xname
, xname
)) {
1212 if (flags
& XATTR_CREATE
) {
1218 rc
= delete_xattr_ref(c
, ref
, 0);
1225 if (flags
& XATTR_REPLACE
) {
1234 xd
= create_xattr_datum(c
, xprefix
, xname
, buffer
, size
);
1239 up_write(&c
->xattr_sem
);
1240 jffs2_complete_reservation(c
);
1242 /* create xattr_ref */
1243 request
= PAD(sizeof(struct jffs2_raw_xref
));
1244 rc
= jffs2_reserve_space(c
, request
, &length
,
1245 ALLOC_NORMAL
, JFFS2_SUMMARY_XREF_SIZE
);
1246 down_write(&c
->xattr_sem
);
1248 JFFS2_WARNING("jffs2_reserve_space()=%d, request=%u\n", rc
, request
);
1251 delete_xattr_datum_delay(c
, xd
);
1252 up_write(&c
->xattr_sem
);
1257 newref
= create_xattr_ref(c
, ic
, xd
);
1258 if (IS_ERR(newref
)) {
1260 ref
->next
= ic
->xref
;
1263 rc
= PTR_ERR(newref
);
1266 delete_xattr_datum_delay(c
, xd
);
1268 up_write(&c
->xattr_sem
);
1269 jffs2_complete_reservation(c
);
1271 rc
= jffs2_reserve_space(c
, request
, &length
,
1272 ALLOC_DELETION
, JFFS2_SUMMARY_XREF_SIZE
);
1273 down_write(&c
->xattr_sem
);
1275 JFFS2_WARNING("jffs2_reserve_space()=%d, request=%u\n", rc
, request
);
1276 delete_xattr_ref_delay(c
, ref
);
1277 up_write(&c
->xattr_sem
);
1280 delete_xattr_ref(c
, ref
, 1);
1283 up_write(&c
->xattr_sem
);
1284 jffs2_complete_reservation(c
);
1288 /* -------- garbage collector functions -------------
1289 * jffs2_garbage_collect_xattr_datum(c, xd, raw)
1290 * is used to move xdatum into new node.
1291 * jffs2_garbage_collect_xattr_ref(c, ref, raw)
1292 * is used to move xref into new node.
1293 * jffs2_verify_xattr(c)
1294 * is used to call do_verify_xattr_datum() before garbage collecting.
1295 * -------------------------------------------------- */
1296 int jffs2_garbage_collect_xattr_datum(struct jffs2_sb_info
*c
, struct jffs2_xattr_datum
*xd
,
1297 struct jffs2_raw_node_ref
*raw
)
1299 uint32_t totlen
, length
, old_ofs
;
1302 down_write(&c
->xattr_sem
);
1303 if (xd
->node
!= raw
)
1305 if (is_xattr_datum_dead(xd
) && (raw
->next_in_ino
== (void *)xd
))
1308 old_ofs
= ref_offset(xd
->node
);
1309 totlen
= ref_totlen(c
, c
->gcblock
, xd
->node
);
1311 if (!is_xattr_datum_dead(xd
)) {
1312 rc
= load_xattr_datum(c
, xd
);
1313 if (unlikely(rc
< 0))
1317 rc
= jffs2_reserve_space_gc(c
, totlen
, &length
, JFFS2_SUMMARY_XATTR_SIZE
);
1319 JFFS2_WARNING("jffs2_reserve_space_gc()=%d, request=%u\n", rc
, totlen
);
1320 rc
= rc
? rc
: -EBADFD
;
1323 rc
= save_xattr_datum(c
, xd
);
1325 dbg_xattr("xdatum (xid=%u, version=%u) GC'ed from %#08x to %08x\n",
1326 xd
->xid
, xd
->version
, old_ofs
, ref_offset(xd
->node
));
1329 jffs2_mark_node_obsolete(c
, raw
);
1330 up_write(&c
->xattr_sem
);
1335 int jffs2_garbage_collect_xattr_ref(struct jffs2_sb_info
*c
, struct jffs2_xattr_ref
*ref
,
1336 struct jffs2_raw_node_ref
*raw
)
1338 uint32_t totlen
, length
, old_ofs
;
1341 down_write(&c
->xattr_sem
);
1344 if (ref
->node
!= raw
)
1346 if (is_xattr_ref_dead(ref
) && (raw
->next_in_ino
== (void *)ref
))
1349 old_ofs
= ref_offset(ref
->node
);
1350 totlen
= ref_totlen(c
, c
->gcblock
, ref
->node
);
1352 rc
= jffs2_reserve_space_gc(c
, totlen
, &length
, JFFS2_SUMMARY_XREF_SIZE
);
1354 JFFS2_WARNING("%s: jffs2_reserve_space_gc() = %d, request = %u\n",
1355 __FUNCTION__
, rc
, totlen
);
1356 rc
= rc
? rc
: -EBADFD
;
1359 rc
= save_xattr_ref(c
, ref
);
1361 dbg_xattr("xref (ino=%u, xid=%u) GC'ed from %#08x to %08x\n",
1362 ref
->ic
->ino
, ref
->xd
->xid
, old_ofs
, ref_offset(ref
->node
));
1365 jffs2_mark_node_obsolete(c
, raw
);
1366 up_write(&c
->xattr_sem
);
1370 int jffs2_verify_xattr(struct jffs2_sb_info
*c
)
1372 struct jffs2_xattr_datum
*xd
, *_xd
;
1373 struct jffs2_eraseblock
*jeb
;
1374 struct jffs2_raw_node_ref
*raw
;
1378 down_write(&c
->xattr_sem
);
1379 list_for_each_entry_safe(xd
, _xd
, &c
->xattr_unchecked
, xindex
) {
1380 rc
= do_verify_xattr_datum(c
, xd
);
1383 list_del_init(&xd
->xindex
);
1384 spin_lock(&c
->erase_completion_lock
);
1385 for (raw
=xd
->node
; raw
!= (void *)xd
; raw
=raw
->next_in_ino
) {
1386 if (ref_flags(raw
) != REF_UNCHECKED
)
1388 jeb
= &c
->blocks
[ref_offset(raw
) / c
->sector_size
];
1389 totlen
= PAD(ref_totlen(c
, jeb
, raw
));
1390 c
->unchecked_size
-= totlen
; c
->used_size
+= totlen
;
1391 jeb
->unchecked_size
-= totlen
; jeb
->used_size
+= totlen
;
1392 raw
->flash_offset
= ref_offset(raw
)
1393 | ((xd
->node
== (void *)raw
) ? REF_PRISTINE
: REF_NORMAL
);
1395 if (is_xattr_datum_dead(xd
))
1396 list_add(&xd
->xindex
, &c
->xattr_dead_list
);
1397 spin_unlock(&c
->erase_completion_lock
);
1399 up_write(&c
->xattr_sem
);
1400 return list_empty(&c
->xattr_unchecked
) ? 1 : 0;
1403 void jffs2_release_xattr_datum(struct jffs2_sb_info
*c
, struct jffs2_xattr_datum
*xd
)
1405 /* must be called under spin_lock(&c->erase_completion_lock) */
1406 if (xd
->node
!= (void *)xd
)
1409 list_del(&xd
->xindex
);
1410 jffs2_free_xattr_datum(xd
);
1413 void jffs2_release_xattr_ref(struct jffs2_sb_info
*c
, struct jffs2_xattr_ref
*ref
)
1415 /* must be called under spin_lock(&c->erase_completion_lock) */
1416 struct jffs2_xattr_ref
*tmp
, **ptmp
;
1418 if (ref
->node
!= (void *)ref
)
1421 for (tmp
=c
->xref_dead_list
, ptmp
=&c
->xref_dead_list
; tmp
; ptmp
=&tmp
->next
, tmp
=tmp
->next
) {
1424 jffs2_free_xattr_ref(ref
);