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 #include <linux/kernel.h>
15 #include <linux/slab.h>
17 #include <linux/time.h>
18 #include <linux/pagemap.h>
19 #include <linux/highmem.h>
20 #include <linux/crc32.h>
21 #include <linux/jffs2.h>
22 #include <linux/xattr.h>
23 #include <linux/mtd/mtd.h>
25 /* -------- xdatum related functions ----------------
26 * xattr_datum_hashkey(xprefix, xname, xvalue, xsize)
27 * is used to calcurate xdatum hashkey. The reminder of hashkey into XATTRINDEX_HASHSIZE is
28 * the index of the xattr name/value pair cache (c->xattrindex).
29 * is_xattr_datum_unchecked(c, xd)
30 * returns 1, if xdatum contains any unchecked raw nodes. if all raw nodes are not
31 * unchecked, it returns 0.
32 * unload_xattr_datum(c, xd)
33 * is used to release xattr name/value pair and detach from c->xattrindex.
34 * reclaim_xattr_datum(c)
35 * is used to reclaim xattr name/value pairs on the xattr name/value pair cache when
36 * memory usage by cache is over c->xdatum_mem_threshold. Currently, this threshold
37 * is hard coded as 32KiB.
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)
53 * is used to write xdatum to medium. xd->version will be incremented.
54 * create_xattr_datum(c, xprefix, xname, xvalue, xsize)
55 * is used to create new xdatum and write to medium.
56 * unrefer_xattr_datum(c, xd)
57 * is used to delete a xdatum. When nobody refers this xdatum, JFFS2_XFLAGS_DEAD
58 * is set on xd->flags and chained xattr_dead_list or release it immediately.
59 * In the first case, the garbage collector release it later.
60 * -------------------------------------------------- */
61 static uint32_t xattr_datum_hashkey(int xprefix
, const char *xname
, const char *xvalue
, int xsize
)
63 int name_len
= strlen(xname
);
65 return crc32(xprefix
, xname
, name_len
) ^ crc32(xprefix
, xvalue
, xsize
);
68 static int is_xattr_datum_unchecked(struct jffs2_sb_info
*c
, struct jffs2_xattr_datum
*xd
)
70 struct jffs2_raw_node_ref
*raw
;
73 spin_lock(&c
->erase_completion_lock
);
74 for (raw
=xd
->node
; raw
!= (void *)xd
; raw
=raw
->next_in_ino
) {
75 if (ref_flags(raw
) == REF_UNCHECKED
) {
80 spin_unlock(&c
->erase_completion_lock
);
84 static void unload_xattr_datum(struct jffs2_sb_info
*c
, struct jffs2_xattr_datum
*xd
)
86 /* must be called under down_write(xattr_sem) */
87 D1(dbg_xattr("%s: xid=%u, version=%u\n", __func__
, xd
->xid
, xd
->version
));
89 c
->xdatum_mem_usage
-= (xd
->name_len
+ 1 + xd
->value_len
);
93 list_del_init(&xd
->xindex
);
99 static void reclaim_xattr_datum(struct jffs2_sb_info
*c
)
101 /* must be called under down_write(xattr_sem) */
102 struct jffs2_xattr_datum
*xd
, *_xd
;
103 uint32_t target
, before
;
104 static int index
= 0;
107 if (c
->xdatum_mem_threshold
> c
->xdatum_mem_usage
)
110 before
= c
->xdatum_mem_usage
;
111 target
= c
->xdatum_mem_usage
* 4 / 5; /* 20% reduction */
112 for (count
= 0; count
< XATTRINDEX_HASHSIZE
; count
++) {
113 list_for_each_entry_safe(xd
, _xd
, &c
->xattrindex
[index
], xindex
) {
114 if (xd
->flags
& JFFS2_XFLAGS_HOT
) {
115 xd
->flags
&= ~JFFS2_XFLAGS_HOT
;
116 } else if (!(xd
->flags
& JFFS2_XFLAGS_BIND
)) {
117 unload_xattr_datum(c
, xd
);
119 if (c
->xdatum_mem_usage
<= target
)
122 index
= (index
+1) % XATTRINDEX_HASHSIZE
;
125 JFFS2_NOTICE("xdatum_mem_usage from %u byte to %u byte (%u byte reclaimed)\n",
126 before
, c
->xdatum_mem_usage
, before
- c
->xdatum_mem_usage
);
129 static int do_verify_xattr_datum(struct jffs2_sb_info
*c
, struct jffs2_xattr_datum
*xd
)
131 /* must be called under down_write(xattr_sem) */
132 struct jffs2_eraseblock
*jeb
;
133 struct jffs2_raw_node_ref
*raw
;
134 struct jffs2_raw_xattr rx
;
136 uint32_t crc
, offset
, totlen
;
139 spin_lock(&c
->erase_completion_lock
);
140 offset
= ref_offset(xd
->node
);
141 if (ref_flags(xd
->node
) == REF_PRISTINE
)
143 spin_unlock(&c
->erase_completion_lock
);
145 rc
= jffs2_flash_read(c
, offset
, sizeof(rx
), &readlen
, (char *)&rx
);
146 if (rc
|| readlen
!= sizeof(rx
)) {
147 JFFS2_WARNING("jffs2_flash_read()=%d, req=%zu, read=%zu at %#08x\n",
148 rc
, sizeof(rx
), readlen
, offset
);
149 return rc
? rc
: -EIO
;
151 crc
= crc32(0, &rx
, sizeof(rx
) - 4);
152 if (crc
!= je32_to_cpu(rx
.node_crc
)) {
153 JFFS2_ERROR("node CRC failed at %#08x, read=%#08x, calc=%#08x\n",
154 offset
, je32_to_cpu(rx
.hdr_crc
), crc
);
155 xd
->flags
|= JFFS2_XFLAGS_INVALID
;
158 totlen
= PAD(sizeof(rx
) + rx
.name_len
+ 1 + je16_to_cpu(rx
.value_len
));
159 if (je16_to_cpu(rx
.magic
) != JFFS2_MAGIC_BITMASK
160 || je16_to_cpu(rx
.nodetype
) != JFFS2_NODETYPE_XATTR
161 || je32_to_cpu(rx
.totlen
) != totlen
162 || je32_to_cpu(rx
.xid
) != xd
->xid
163 || je32_to_cpu(rx
.version
) != xd
->version
) {
164 JFFS2_ERROR("inconsistent xdatum at %#08x, magic=%#04x/%#04x, "
165 "nodetype=%#04x/%#04x, totlen=%u/%u, xid=%u/%u, version=%u/%u\n",
166 offset
, je16_to_cpu(rx
.magic
), JFFS2_MAGIC_BITMASK
,
167 je16_to_cpu(rx
.nodetype
), JFFS2_NODETYPE_XATTR
,
168 je32_to_cpu(rx
.totlen
), totlen
,
169 je32_to_cpu(rx
.xid
), xd
->xid
,
170 je32_to_cpu(rx
.version
), xd
->version
);
171 xd
->flags
|= JFFS2_XFLAGS_INVALID
;
174 xd
->xprefix
= rx
.xprefix
;
175 xd
->name_len
= rx
.name_len
;
176 xd
->value_len
= je16_to_cpu(rx
.value_len
);
177 xd
->data_crc
= je32_to_cpu(rx
.data_crc
);
179 spin_lock(&c
->erase_completion_lock
);
181 for (raw
=xd
->node
; raw
!= (void *)xd
; raw
=raw
->next_in_ino
) {
182 jeb
= &c
->blocks
[ref_offset(raw
) / c
->sector_size
];
183 totlen
= PAD(ref_totlen(c
, jeb
, raw
));
184 if (ref_flags(raw
) == REF_UNCHECKED
) {
185 c
->unchecked_size
-= totlen
; c
->used_size
+= totlen
;
186 jeb
->unchecked_size
-= totlen
; jeb
->used_size
+= totlen
;
188 raw
->flash_offset
= ref_offset(raw
) | ((xd
->node
==raw
) ? REF_PRISTINE
: REF_NORMAL
);
190 spin_unlock(&c
->erase_completion_lock
);
192 /* unchecked xdatum is chained with c->xattr_unchecked */
193 list_del_init(&xd
->xindex
);
195 dbg_xattr("success on verfying xdatum (xid=%u, version=%u)\n",
196 xd
->xid
, xd
->version
);
201 static int do_load_xattr_datum(struct jffs2_sb_info
*c
, struct jffs2_xattr_datum
*xd
)
203 /* must be called under down_write(xattr_sem) */
206 uint32_t crc
, length
;
207 int i
, ret
, retry
= 0;
209 BUG_ON(ref_flags(xd
->node
) != REF_PRISTINE
);
210 BUG_ON(!list_empty(&xd
->xindex
));
212 length
= xd
->name_len
+ 1 + xd
->value_len
;
213 data
= kmalloc(length
, GFP_KERNEL
);
217 ret
= jffs2_flash_read(c
, ref_offset(xd
->node
)+sizeof(struct jffs2_raw_xattr
),
218 length
, &readlen
, data
);
220 if (ret
|| length
!=readlen
) {
221 JFFS2_WARNING("jffs2_flash_read() returned %d, request=%d, readlen=%zu, at %#08x\n",
222 ret
, length
, readlen
, ref_offset(xd
->node
));
224 return ret
? ret
: -EIO
;
227 data
[xd
->name_len
] = '\0';
228 crc
= crc32(0, data
, length
);
229 if (crc
!= xd
->data_crc
) {
230 JFFS2_WARNING("node CRC failed (JFFS2_NODETYPE_XREF)"
231 " at %#08x, read: 0x%08x calculated: 0x%08x\n",
232 ref_offset(xd
->node
), xd
->data_crc
, crc
);
234 xd
->flags
|= JFFS2_XFLAGS_INVALID
;
238 xd
->flags
|= JFFS2_XFLAGS_HOT
;
240 xd
->xvalue
= data
+ xd
->name_len
+1;
242 c
->xdatum_mem_usage
+= length
;
244 xd
->hashkey
= xattr_datum_hashkey(xd
->xprefix
, xd
->xname
, xd
->xvalue
, xd
->value_len
);
245 i
= xd
->hashkey
% XATTRINDEX_HASHSIZE
;
246 list_add(&xd
->xindex
, &c
->xattrindex
[i
]);
249 reclaim_xattr_datum(c
);
254 dbg_xattr("success on loading xdatum (xid=%u, xprefix=%u, xname='%s')\n",
255 xd
->xid
, xd
->xprefix
, xd
->xname
);
260 static int load_xattr_datum(struct jffs2_sb_info
*c
, struct jffs2_xattr_datum
*xd
)
262 /* must be called under down_write(xattr_sem);
263 * rc < 0 : recoverable error, try again
265 * rc > 0 : Unrecoverable error, this node should be deleted.
269 BUG_ON(xd
->flags
& JFFS2_XFLAGS_DEAD
);
272 if (xd
->flags
& JFFS2_XFLAGS_INVALID
)
274 if (unlikely(is_xattr_datum_unchecked(c
, xd
)))
275 rc
= do_verify_xattr_datum(c
, xd
);
277 rc
= do_load_xattr_datum(c
, xd
);
281 static int save_xattr_datum(struct jffs2_sb_info
*c
, struct jffs2_xattr_datum
*xd
)
283 /* must be called under down_write(xattr_sem) */
284 struct jffs2_raw_xattr rx
;
288 uint32_t phys_ofs
= write_ofs(c
);
291 BUG_ON(xd
->flags
& (JFFS2_XFLAGS_DEAD
|JFFS2_XFLAGS_INVALID
));
293 vecs
[0].iov_base
= &rx
;
294 vecs
[0].iov_len
= sizeof(rx
);
295 vecs
[1].iov_base
= xd
->xname
;
296 vecs
[1].iov_len
= xd
->name_len
+ 1 + xd
->value_len
;
297 totlen
= vecs
[0].iov_len
+ vecs
[1].iov_len
;
299 /* Setup raw-xattr */
300 memset(&rx
, 0, sizeof(rx
));
301 rx
.magic
= cpu_to_je16(JFFS2_MAGIC_BITMASK
);
302 rx
.nodetype
= cpu_to_je16(JFFS2_NODETYPE_XATTR
);
303 rx
.totlen
= cpu_to_je32(PAD(totlen
));
304 rx
.hdr_crc
= cpu_to_je32(crc32(0, &rx
, sizeof(struct jffs2_unknown_node
) - 4));
306 rx
.xid
= cpu_to_je32(xd
->xid
);
307 rx
.version
= cpu_to_je32(++xd
->version
);
308 rx
.xprefix
= xd
->xprefix
;
309 rx
.name_len
= xd
->name_len
;
310 rx
.value_len
= cpu_to_je16(xd
->value_len
);
311 rx
.data_crc
= cpu_to_je32(crc32(0, vecs
[1].iov_base
, vecs
[1].iov_len
));
312 rx
.node_crc
= cpu_to_je32(crc32(0, &rx
, sizeof(struct jffs2_raw_xattr
) - 4));
314 rc
= jffs2_flash_writev(c
, vecs
, 2, phys_ofs
, &length
, 0);
315 if (rc
|| totlen
!= length
) {
316 JFFS2_WARNING("jffs2_flash_writev()=%d, req=%u, wrote=%zu, at %#08x\n",
317 rc
, totlen
, length
, phys_ofs
);
320 jffs2_add_physical_node_ref(c
, phys_ofs
| REF_OBSOLETE
, PAD(totlen
), NULL
);
325 jffs2_add_physical_node_ref(c
, phys_ofs
| REF_PRISTINE
, PAD(totlen
), (void *)xd
);
327 dbg_xattr("success on saving xdatum (xid=%u, version=%u, xprefix=%u, xname='%s')\n",
328 xd
->xid
, xd
->version
, xd
->xprefix
, xd
->xname
);
333 static struct jffs2_xattr_datum
*create_xattr_datum(struct jffs2_sb_info
*c
,
334 int xprefix
, const char *xname
,
335 const char *xvalue
, int xsize
)
337 /* must be called under down_write(xattr_sem) */
338 struct jffs2_xattr_datum
*xd
;
339 uint32_t hashkey
, name_len
;
343 /* Search xattr_datum has same xname/xvalue by index */
344 hashkey
= xattr_datum_hashkey(xprefix
, xname
, xvalue
, xsize
);
345 i
= hashkey
% XATTRINDEX_HASHSIZE
;
346 list_for_each_entry(xd
, &c
->xattrindex
[i
], xindex
) {
347 if (xd
->hashkey
==hashkey
348 && xd
->xprefix
==xprefix
349 && xd
->value_len
==xsize
350 && !strcmp(xd
->xname
, xname
)
351 && !memcmp(xd
->xvalue
, xvalue
, xsize
)) {
352 atomic_inc(&xd
->refcnt
);
357 /* Not found, Create NEW XATTR-Cache */
358 name_len
= strlen(xname
);
360 xd
= jffs2_alloc_xattr_datum();
362 return ERR_PTR(-ENOMEM
);
364 data
= kmalloc(name_len
+ 1 + xsize
, GFP_KERNEL
);
366 jffs2_free_xattr_datum(xd
);
367 return ERR_PTR(-ENOMEM
);
370 memcpy(data
+ name_len
+ 1, xvalue
, xsize
);
372 atomic_set(&xd
->refcnt
, 1);
373 xd
->xid
= ++c
->highest_xid
;
374 xd
->flags
|= JFFS2_XFLAGS_HOT
;
375 xd
->xprefix
= xprefix
;
377 xd
->hashkey
= hashkey
;
379 xd
->xvalue
= data
+ name_len
+ 1;
380 xd
->name_len
= name_len
;
381 xd
->value_len
= xsize
;
382 xd
->data_crc
= crc32(0, data
, xd
->name_len
+ 1 + xd
->value_len
);
384 rc
= save_xattr_datum(c
, xd
);
387 jffs2_free_xattr_datum(xd
);
391 /* Insert Hash Index */
392 i
= hashkey
% XATTRINDEX_HASHSIZE
;
393 list_add(&xd
->xindex
, &c
->xattrindex
[i
]);
395 c
->xdatum_mem_usage
+= (xd
->name_len
+ 1 + xd
->value_len
);
396 reclaim_xattr_datum(c
);
401 static void unrefer_xattr_datum(struct jffs2_sb_info
*c
, struct jffs2_xattr_datum
*xd
)
403 /* must be called under down_write(xattr_sem) */
404 if (atomic_dec_and_lock(&xd
->refcnt
, &c
->erase_completion_lock
)) {
405 unload_xattr_datum(c
, xd
);
406 xd
->flags
|= JFFS2_XFLAGS_DEAD
;
407 if (xd
->node
== (void *)xd
) {
408 BUG_ON(!(xd
->flags
& JFFS2_XFLAGS_INVALID
));
409 jffs2_free_xattr_datum(xd
);
411 list_add(&xd
->xindex
, &c
->xattr_dead_list
);
413 spin_unlock(&c
->erase_completion_lock
);
415 dbg_xattr("xdatum(xid=%u, version=%u) was removed.\n",
416 xd
->xid
, xd
->version
);
420 /* -------- xref related functions ------------------
421 * verify_xattr_ref(c, ref)
422 * is used to load xref information from medium. Because summary data does not
423 * contain xid/ino, it's necessary to verify once while mounting process.
424 * save_xattr_ref(c, ref)
425 * is used to write xref to medium. If delete marker is marked, it write
426 * a delete marker of xref into medium.
427 * create_xattr_ref(c, ic, xd)
428 * is used to create a new xref and write to medium.
429 * delete_xattr_ref(c, ref)
430 * is used to delete jffs2_xattr_ref. It marks xref XREF_DELETE_MARKER,
431 * and allows GC to reclaim those physical nodes.
432 * jffs2_xattr_delete_inode(c, ic)
433 * is called to remove xrefs related to obsolete inode when inode is unlinked.
434 * jffs2_xattr_free_inode(c, ic)
435 * is called to release xattr related objects when unmounting.
436 * check_xattr_ref_inode(c, ic)
437 * is used to confirm inode does not have duplicate xattr name/value pair.
438 * -------------------------------------------------- */
439 static int verify_xattr_ref(struct jffs2_sb_info
*c
, struct jffs2_xattr_ref
*ref
)
441 struct jffs2_eraseblock
*jeb
;
442 struct jffs2_raw_node_ref
*raw
;
443 struct jffs2_raw_xref rr
;
445 uint32_t crc
, offset
, totlen
;
448 spin_lock(&c
->erase_completion_lock
);
449 if (ref_flags(ref
->node
) != REF_UNCHECKED
)
451 offset
= ref_offset(ref
->node
);
452 spin_unlock(&c
->erase_completion_lock
);
454 rc
= jffs2_flash_read(c
, offset
, sizeof(rr
), &readlen
, (char *)&rr
);
455 if (rc
|| sizeof(rr
) != readlen
) {
456 JFFS2_WARNING("jffs2_flash_read()=%d, req=%zu, read=%zu, at %#08x\n",
457 rc
, sizeof(rr
), readlen
, offset
);
458 return rc
? rc
: -EIO
;
461 crc
= crc32(0, &rr
, sizeof(rr
) - 4);
462 if (crc
!= je32_to_cpu(rr
.node_crc
)) {
463 JFFS2_ERROR("node CRC failed at %#08x, read=%#08x, calc=%#08x\n",
464 offset
, je32_to_cpu(rr
.node_crc
), crc
);
467 if (je16_to_cpu(rr
.magic
) != JFFS2_MAGIC_BITMASK
468 || je16_to_cpu(rr
.nodetype
) != JFFS2_NODETYPE_XREF
469 || je32_to_cpu(rr
.totlen
) != PAD(sizeof(rr
))) {
470 JFFS2_ERROR("inconsistent xref at %#08x, magic=%#04x/%#04x, "
471 "nodetype=%#04x/%#04x, totlen=%u/%zu\n",
472 offset
, 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
)));
477 ref
->ino
= je32_to_cpu(rr
.ino
);
478 ref
->xid
= je32_to_cpu(rr
.xid
);
479 ref
->xseqno
= je32_to_cpu(rr
.xseqno
);
480 if (ref
->xseqno
> c
->highest_xseqno
)
481 c
->highest_xseqno
= (ref
->xseqno
& ~XREF_DELETE_MARKER
);
483 spin_lock(&c
->erase_completion_lock
);
485 for (raw
=ref
->node
; raw
!= (void *)ref
; raw
=raw
->next_in_ino
) {
486 jeb
= &c
->blocks
[ref_offset(raw
) / c
->sector_size
];
487 totlen
= PAD(ref_totlen(c
, jeb
, raw
));
488 if (ref_flags(raw
) == REF_UNCHECKED
) {
489 c
->unchecked_size
-= totlen
; c
->used_size
+= totlen
;
490 jeb
->unchecked_size
-= totlen
; jeb
->used_size
+= totlen
;
492 raw
->flash_offset
= ref_offset(raw
) | ((ref
->node
==raw
) ? REF_PRISTINE
: REF_NORMAL
);
494 spin_unlock(&c
->erase_completion_lock
);
496 dbg_xattr("success on verifying xref (ino=%u, xid=%u) at %#08x\n",
497 ref
->ino
, ref
->xid
, ref_offset(ref
->node
));
501 static int save_xattr_ref(struct jffs2_sb_info
*c
, struct jffs2_xattr_ref
*ref
)
503 /* must be called under down_write(xattr_sem) */
504 struct jffs2_raw_xref rr
;
506 uint32_t xseqno
, phys_ofs
= write_ofs(c
);
509 rr
.magic
= cpu_to_je16(JFFS2_MAGIC_BITMASK
);
510 rr
.nodetype
= cpu_to_je16(JFFS2_NODETYPE_XREF
);
511 rr
.totlen
= cpu_to_je32(PAD(sizeof(rr
)));
512 rr
.hdr_crc
= cpu_to_je32(crc32(0, &rr
, sizeof(struct jffs2_unknown_node
) - 4));
514 xseqno
= (c
->highest_xseqno
+= 2);
515 if (is_xattr_ref_dead(ref
)) {
516 xseqno
|= XREF_DELETE_MARKER
;
517 rr
.ino
= cpu_to_je32(ref
->ino
);
518 rr
.xid
= cpu_to_je32(ref
->xid
);
520 rr
.ino
= cpu_to_je32(ref
->ic
->ino
);
521 rr
.xid
= cpu_to_je32(ref
->xd
->xid
);
523 rr
.xseqno
= cpu_to_je32(xseqno
);
524 rr
.node_crc
= cpu_to_je32(crc32(0, &rr
, sizeof(rr
) - 4));
526 ret
= jffs2_flash_write(c
, phys_ofs
, sizeof(rr
), &length
, (char *)&rr
);
527 if (ret
|| sizeof(rr
) != length
) {
528 JFFS2_WARNING("jffs2_flash_write() returned %d, request=%zu, retlen=%zu, at %#08x\n",
529 ret
, sizeof(rr
), length
, phys_ofs
);
530 ret
= ret
? ret
: -EIO
;
532 jffs2_add_physical_node_ref(c
, phys_ofs
| REF_OBSOLETE
, PAD(sizeof(rr
)), NULL
);
537 ref
->xseqno
= xseqno
;
538 jffs2_add_physical_node_ref(c
, phys_ofs
| REF_PRISTINE
, PAD(sizeof(rr
)), (void *)ref
);
540 dbg_xattr("success on saving xref (ino=%u, xid=%u)\n", ref
->ic
->ino
, ref
->xd
->xid
);
545 static struct jffs2_xattr_ref
*create_xattr_ref(struct jffs2_sb_info
*c
, struct jffs2_inode_cache
*ic
,
546 struct jffs2_xattr_datum
*xd
)
548 /* must be called under down_write(xattr_sem) */
549 struct jffs2_xattr_ref
*ref
;
552 ref
= jffs2_alloc_xattr_ref();
554 return ERR_PTR(-ENOMEM
);
558 ret
= save_xattr_ref(c
, ref
);
560 jffs2_free_xattr_ref(ref
);
565 ref
->next
= ic
->xref
;
568 return ref
; /* success */
571 static void delete_xattr_ref(struct jffs2_sb_info
*c
, struct jffs2_xattr_ref
*ref
)
573 /* must be called under down_write(xattr_sem) */
574 struct jffs2_xattr_datum
*xd
;
577 ref
->xseqno
|= XREF_DELETE_MARKER
;
578 ref
->ino
= ref
->ic
->ino
;
579 ref
->xid
= ref
->xd
->xid
;
580 spin_lock(&c
->erase_completion_lock
);
581 ref
->next
= c
->xref_dead_list
;
582 c
->xref_dead_list
= ref
;
583 spin_unlock(&c
->erase_completion_lock
);
585 dbg_xattr("xref(ino=%u, xid=%u, xseqno=%u) was removed.\n",
586 ref
->ino
, ref
->xid
, ref
->xseqno
);
588 unrefer_xattr_datum(c
, xd
);
591 void jffs2_xattr_delete_inode(struct jffs2_sb_info
*c
, struct jffs2_inode_cache
*ic
)
593 /* It's called from jffs2_evict_inode() on inode removing.
594 When an inode with XATTR is removed, those XATTRs must be removed. */
595 struct jffs2_xattr_ref
*ref
, *_ref
;
597 if (!ic
|| ic
->pino_nlink
> 0)
600 down_write(&c
->xattr_sem
);
601 for (ref
= ic
->xref
; ref
; ref
= _ref
) {
603 delete_xattr_ref(c
, ref
);
606 up_write(&c
->xattr_sem
);
609 void jffs2_xattr_free_inode(struct jffs2_sb_info
*c
, struct jffs2_inode_cache
*ic
)
611 /* It's called from jffs2_free_ino_caches() until unmounting FS. */
612 struct jffs2_xattr_datum
*xd
;
613 struct jffs2_xattr_ref
*ref
, *_ref
;
615 down_write(&c
->xattr_sem
);
616 for (ref
= ic
->xref
; ref
; ref
= _ref
) {
619 if (atomic_dec_and_test(&xd
->refcnt
)) {
620 unload_xattr_datum(c
, xd
);
621 jffs2_free_xattr_datum(xd
);
623 jffs2_free_xattr_ref(ref
);
626 up_write(&c
->xattr_sem
);
629 static int check_xattr_ref_inode(struct jffs2_sb_info
*c
, struct jffs2_inode_cache
*ic
)
631 /* success of check_xattr_ref_inode() means that inode (ic) dose not have
632 * duplicate name/value pairs. If duplicate name/value pair would be found,
633 * one will be removed.
635 struct jffs2_xattr_ref
*ref
, *cmp
, **pref
, **pcmp
;
638 if (likely(ic
->flags
& INO_FLAGS_XATTR_CHECKED
))
640 down_write(&c
->xattr_sem
);
643 for (ref
=ic
->xref
, pref
=&ic
->xref
; ref
; pref
=&ref
->next
, ref
=ref
->next
) {
644 if (!ref
->xd
->xname
) {
645 rc
= load_xattr_datum(c
, ref
->xd
);
646 if (unlikely(rc
> 0)) {
648 delete_xattr_ref(c
, ref
);
650 } else if (unlikely(rc
< 0))
653 for (cmp
=ref
->next
, pcmp
=&ref
->next
; cmp
; pcmp
=&cmp
->next
, cmp
=cmp
->next
) {
654 if (!cmp
->xd
->xname
) {
655 ref
->xd
->flags
|= JFFS2_XFLAGS_BIND
;
656 rc
= load_xattr_datum(c
, cmp
->xd
);
657 ref
->xd
->flags
&= ~JFFS2_XFLAGS_BIND
;
658 if (unlikely(rc
> 0)) {
660 delete_xattr_ref(c
, cmp
);
662 } else if (unlikely(rc
< 0))
665 if (ref
->xd
->xprefix
== cmp
->xd
->xprefix
666 && !strcmp(ref
->xd
->xname
, cmp
->xd
->xname
)) {
667 if (ref
->xseqno
> cmp
->xseqno
) {
669 delete_xattr_ref(c
, cmp
);
672 delete_xattr_ref(c
, ref
);
678 ic
->flags
|= INO_FLAGS_XATTR_CHECKED
;
680 up_write(&c
->xattr_sem
);
685 /* -------- xattr subsystem functions ---------------
686 * jffs2_init_xattr_subsystem(c)
687 * is used to initialize semaphore and list_head, and some variables.
688 * jffs2_find_xattr_datum(c, xid)
689 * is used to lookup xdatum while scanning process.
690 * jffs2_clear_xattr_subsystem(c)
691 * is used to release any xattr related objects.
692 * jffs2_build_xattr_subsystem(c)
693 * is used to associate xdatum and xref while super block building process.
694 * jffs2_setup_xattr_datum(c, xid, version)
695 * is used to insert xdatum while scanning process.
696 * -------------------------------------------------- */
697 void jffs2_init_xattr_subsystem(struct jffs2_sb_info
*c
)
701 for (i
=0; i
< XATTRINDEX_HASHSIZE
; i
++)
702 INIT_LIST_HEAD(&c
->xattrindex
[i
]);
703 INIT_LIST_HEAD(&c
->xattr_unchecked
);
704 INIT_LIST_HEAD(&c
->xattr_dead_list
);
705 c
->xref_dead_list
= NULL
;
708 init_rwsem(&c
->xattr_sem
);
710 c
->highest_xseqno
= 0;
711 c
->xdatum_mem_usage
= 0;
712 c
->xdatum_mem_threshold
= 32 * 1024; /* Default 32KB */
715 static struct jffs2_xattr_datum
*jffs2_find_xattr_datum(struct jffs2_sb_info
*c
, uint32_t xid
)
717 struct jffs2_xattr_datum
*xd
;
718 int i
= xid
% XATTRINDEX_HASHSIZE
;
720 /* It's only used in scanning/building process. */
721 BUG_ON(!(c
->flags
& (JFFS2_SB_FLAG_SCANNING
|JFFS2_SB_FLAG_BUILDING
)));
723 list_for_each_entry(xd
, &c
->xattrindex
[i
], xindex
) {
730 void jffs2_clear_xattr_subsystem(struct jffs2_sb_info
*c
)
732 struct jffs2_xattr_datum
*xd
, *_xd
;
733 struct jffs2_xattr_ref
*ref
, *_ref
;
736 for (ref
=c
->xref_temp
; ref
; ref
= _ref
) {
738 jffs2_free_xattr_ref(ref
);
741 for (ref
=c
->xref_dead_list
; ref
; ref
= _ref
) {
743 jffs2_free_xattr_ref(ref
);
746 for (i
=0; i
< XATTRINDEX_HASHSIZE
; i
++) {
747 list_for_each_entry_safe(xd
, _xd
, &c
->xattrindex
[i
], xindex
) {
748 list_del(&xd
->xindex
);
751 jffs2_free_xattr_datum(xd
);
755 list_for_each_entry_safe(xd
, _xd
, &c
->xattr_dead_list
, xindex
) {
756 list_del(&xd
->xindex
);
757 jffs2_free_xattr_datum(xd
);
759 list_for_each_entry_safe(xd
, _xd
, &c
->xattr_unchecked
, xindex
) {
760 list_del(&xd
->xindex
);
761 jffs2_free_xattr_datum(xd
);
765 #define XREF_TMPHASH_SIZE (128)
766 void jffs2_build_xattr_subsystem(struct jffs2_sb_info
*c
)
768 struct jffs2_xattr_ref
*ref
, *_ref
;
769 struct jffs2_xattr_ref
*xref_tmphash
[XREF_TMPHASH_SIZE
];
770 struct jffs2_xattr_datum
*xd
, *_xd
;
771 struct jffs2_inode_cache
*ic
;
772 struct jffs2_raw_node_ref
*raw
;
773 int i
, xdatum_count
= 0, xdatum_unchecked_count
= 0, xref_count
= 0;
774 int xdatum_orphan_count
= 0, xref_orphan_count
= 0, xref_dead_count
= 0;
776 BUG_ON(!(c
->flags
& JFFS2_SB_FLAG_BUILDING
));
778 /* Phase.1 : Merge same xref */
779 for (i
=0; i
< XREF_TMPHASH_SIZE
; i
++)
780 xref_tmphash
[i
] = NULL
;
781 for (ref
=c
->xref_temp
; ref
; ref
=_ref
) {
782 struct jffs2_xattr_ref
*tmp
;
785 if (ref_flags(ref
->node
) != REF_PRISTINE
) {
786 if (verify_xattr_ref(c
, ref
)) {
787 BUG_ON(ref
->node
->next_in_ino
!= (void *)ref
);
788 ref
->node
->next_in_ino
= NULL
;
789 jffs2_mark_node_obsolete(c
, ref
->node
);
790 jffs2_free_xattr_ref(ref
);
795 i
= (ref
->ino
^ ref
->xid
) % XREF_TMPHASH_SIZE
;
796 for (tmp
=xref_tmphash
[i
]; tmp
; tmp
=tmp
->next
) {
797 if (tmp
->ino
== ref
->ino
&& tmp
->xid
== ref
->xid
)
802 if (ref
->xseqno
> tmp
->xseqno
) {
803 tmp
->xseqno
= ref
->xseqno
;
804 raw
->next_in_ino
= tmp
->node
;
807 raw
->next_in_ino
= tmp
->node
->next_in_ino
;
808 tmp
->node
->next_in_ino
= raw
;
810 jffs2_free_xattr_ref(ref
);
813 ref
->next
= xref_tmphash
[i
];
814 xref_tmphash
[i
] = ref
;
819 /* Phase.2 : Bind xref with inode_cache and xattr_datum */
820 for (i
=0; i
< XREF_TMPHASH_SIZE
; i
++) {
821 for (ref
=xref_tmphash
[i
]; ref
; ref
=_ref
) {
824 if (is_xattr_ref_dead(ref
)) {
825 ref
->next
= c
->xref_dead_list
;
826 c
->xref_dead_list
= ref
;
830 /* At this point, ref->xid and ref->ino contain XID and inode number.
831 ref->xd and ref->ic are not valid yet. */
832 xd
= jffs2_find_xattr_datum(c
, ref
->xid
);
833 ic
= jffs2_get_ino_cache(c
, ref
->ino
);
834 if (!xd
|| !ic
|| !ic
->pino_nlink
) {
835 dbg_xattr("xref(ino=%u, xid=%u, xseqno=%u) is orphan.\n",
836 ref
->ino
, ref
->xid
, ref
->xseqno
);
837 ref
->xseqno
|= XREF_DELETE_MARKER
;
838 ref
->next
= c
->xref_dead_list
;
839 c
->xref_dead_list
= ref
;
845 atomic_inc(&xd
->refcnt
);
846 ref
->next
= ic
->xref
;
851 /* Phase.3 : Link unchecked xdatum to xattr_unchecked list */
852 for (i
=0; i
< XATTRINDEX_HASHSIZE
; i
++) {
853 list_for_each_entry_safe(xd
, _xd
, &c
->xattrindex
[i
], xindex
) {
855 list_del_init(&xd
->xindex
);
856 if (!atomic_read(&xd
->refcnt
)) {
857 dbg_xattr("xdatum(xid=%u, version=%u) is orphan.\n",
858 xd
->xid
, xd
->version
);
859 xd
->flags
|= JFFS2_XFLAGS_DEAD
;
860 list_add(&xd
->xindex
, &c
->xattr_unchecked
);
861 xdatum_orphan_count
++;
864 if (is_xattr_datum_unchecked(c
, xd
)) {
865 dbg_xattr("unchecked xdatum(xid=%u, version=%u)\n",
866 xd
->xid
, xd
->version
);
867 list_add(&xd
->xindex
, &c
->xattr_unchecked
);
868 xdatum_unchecked_count
++;
873 JFFS2_NOTICE("complete building xattr subsystem, %u of xdatum"
874 " (%u unchecked, %u orphan) and "
875 "%u of xref (%u dead, %u orphan) found.\n",
876 xdatum_count
, xdatum_unchecked_count
, xdatum_orphan_count
,
877 xref_count
, xref_dead_count
, xref_orphan_count
);
880 struct jffs2_xattr_datum
*jffs2_setup_xattr_datum(struct jffs2_sb_info
*c
,
881 uint32_t xid
, uint32_t version
)
883 struct jffs2_xattr_datum
*xd
;
885 xd
= jffs2_find_xattr_datum(c
, xid
);
887 xd
= jffs2_alloc_xattr_datum();
889 return ERR_PTR(-ENOMEM
);
891 xd
->version
= version
;
892 if (xd
->xid
> c
->highest_xid
)
893 c
->highest_xid
= xd
->xid
;
894 list_add_tail(&xd
->xindex
, &c
->xattrindex
[xid
% XATTRINDEX_HASHSIZE
]);
899 /* -------- xattr subsystem functions ---------------
900 * xprefix_to_handler(xprefix)
901 * is used to translate xprefix into xattr_handler.
902 * jffs2_listxattr(dentry, buffer, size)
903 * is an implementation of listxattr handler on jffs2.
904 * do_jffs2_getxattr(inode, xprefix, xname, buffer, size)
905 * is an implementation of getxattr handler on jffs2.
906 * do_jffs2_setxattr(inode, xprefix, xname, buffer, size, flags)
907 * is an implementation of setxattr handler on jffs2.
908 * -------------------------------------------------- */
909 const struct xattr_handler
*jffs2_xattr_handlers
[] = {
910 &jffs2_user_xattr_handler
,
911 #ifdef CONFIG_JFFS2_FS_SECURITY
912 &jffs2_security_xattr_handler
,
914 #ifdef CONFIG_JFFS2_FS_POSIX_ACL
915 &jffs2_acl_access_xattr_handler
,
916 &jffs2_acl_default_xattr_handler
,
918 &jffs2_trusted_xattr_handler
,
922 static const struct xattr_handler
*xprefix_to_handler(int xprefix
) {
923 const struct xattr_handler
*ret
;
926 case JFFS2_XPREFIX_USER
:
927 ret
= &jffs2_user_xattr_handler
;
929 #ifdef CONFIG_JFFS2_FS_SECURITY
930 case JFFS2_XPREFIX_SECURITY
:
931 ret
= &jffs2_security_xattr_handler
;
934 #ifdef CONFIG_JFFS2_FS_POSIX_ACL
935 case JFFS2_XPREFIX_ACL_ACCESS
:
936 ret
= &jffs2_acl_access_xattr_handler
;
938 case JFFS2_XPREFIX_ACL_DEFAULT
:
939 ret
= &jffs2_acl_default_xattr_handler
;
942 case JFFS2_XPREFIX_TRUSTED
:
943 ret
= &jffs2_trusted_xattr_handler
;
952 ssize_t
jffs2_listxattr(struct dentry
*dentry
, char *buffer
, size_t size
)
954 struct inode
*inode
= dentry
->d_inode
;
955 struct jffs2_inode_info
*f
= JFFS2_INODE_INFO(inode
);
956 struct jffs2_sb_info
*c
= JFFS2_SB_INFO(inode
->i_sb
);
957 struct jffs2_inode_cache
*ic
= f
->inocache
;
958 struct jffs2_xattr_ref
*ref
, **pref
;
959 struct jffs2_xattr_datum
*xd
;
960 const struct xattr_handler
*xhandle
;
964 rc
= check_xattr_ref_inode(c
, ic
);
968 down_read(&c
->xattr_sem
);
971 for (ref
=ic
->xref
, pref
=&ic
->xref
; ref
; pref
=&ref
->next
, ref
=ref
->next
) {
972 BUG_ON(ref
->ic
!= ic
);
975 /* xdatum is unchached */
978 up_read(&c
->xattr_sem
);
979 down_write(&c
->xattr_sem
);
982 rc
= load_xattr_datum(c
, xd
);
983 if (unlikely(rc
> 0)) {
985 delete_xattr_ref(c
, ref
);
987 } else if (unlikely(rc
< 0))
991 xhandle
= xprefix_to_handler(xd
->xprefix
);
995 rc
= xhandle
->list(dentry
, buffer
+len
, size
-len
,
996 xd
->xname
, xd
->name_len
, xd
->flags
);
998 rc
= xhandle
->list(dentry
, NULL
, 0, xd
->xname
,
999 xd
->name_len
, xd
->flags
);
1008 up_read(&c
->xattr_sem
);
1010 up_write(&c
->xattr_sem
);
1015 int do_jffs2_getxattr(struct inode
*inode
, int xprefix
, const char *xname
,
1016 char *buffer
, size_t size
)
1018 struct jffs2_inode_info
*f
= JFFS2_INODE_INFO(inode
);
1019 struct jffs2_sb_info
*c
= JFFS2_SB_INFO(inode
->i_sb
);
1020 struct jffs2_inode_cache
*ic
= f
->inocache
;
1021 struct jffs2_xattr_datum
*xd
;
1022 struct jffs2_xattr_ref
*ref
, **pref
;
1025 rc
= check_xattr_ref_inode(c
, ic
);
1029 down_read(&c
->xattr_sem
);
1031 for (ref
=ic
->xref
, pref
=&ic
->xref
; ref
; pref
=&ref
->next
, ref
=ref
->next
) {
1032 BUG_ON(ref
->ic
!=ic
);
1035 if (xd
->xprefix
!= xprefix
)
1038 /* xdatum is unchached */
1041 up_read(&c
->xattr_sem
);
1042 down_write(&c
->xattr_sem
);
1045 rc
= load_xattr_datum(c
, xd
);
1046 if (unlikely(rc
> 0)) {
1048 delete_xattr_ref(c
, ref
);
1050 } else if (unlikely(rc
< 0)) {
1055 if (!strcmp(xname
, xd
->xname
)) {
1061 memcpy(buffer
, xd
->xvalue
, rc
);
1070 up_read(&c
->xattr_sem
);
1072 up_write(&c
->xattr_sem
);
1077 int do_jffs2_setxattr(struct inode
*inode
, int xprefix
, const char *xname
,
1078 const char *buffer
, size_t size
, int flags
)
1080 struct jffs2_inode_info
*f
= JFFS2_INODE_INFO(inode
);
1081 struct jffs2_sb_info
*c
= JFFS2_SB_INFO(inode
->i_sb
);
1082 struct jffs2_inode_cache
*ic
= f
->inocache
;
1083 struct jffs2_xattr_datum
*xd
;
1084 struct jffs2_xattr_ref
*ref
, *newref
, **pref
;
1085 uint32_t length
, request
;
1088 rc
= check_xattr_ref_inode(c
, ic
);
1092 request
= PAD(sizeof(struct jffs2_raw_xattr
) + strlen(xname
) + 1 + size
);
1093 rc
= jffs2_reserve_space(c
, request
, &length
,
1094 ALLOC_NORMAL
, JFFS2_SUMMARY_XATTR_SIZE
);
1096 JFFS2_WARNING("jffs2_reserve_space()=%d, request=%u\n", rc
, request
);
1100 /* Find existing xattr */
1101 down_write(&c
->xattr_sem
);
1103 for (ref
=ic
->xref
, pref
=&ic
->xref
; ref
; pref
=&ref
->next
, ref
=ref
->next
) {
1105 if (xd
->xprefix
!= xprefix
)
1108 rc
= load_xattr_datum(c
, xd
);
1109 if (unlikely(rc
> 0)) {
1111 delete_xattr_ref(c
, ref
);
1113 } else if (unlikely(rc
< 0))
1116 if (!strcmp(xd
->xname
, xname
)) {
1117 if (flags
& XATTR_CREATE
) {
1124 ref
->xseqno
|= XREF_DELETE_MARKER
;
1125 rc
= save_xattr_ref(c
, ref
);
1128 spin_lock(&c
->erase_completion_lock
);
1129 ref
->next
= c
->xref_dead_list
;
1130 c
->xref_dead_list
= ref
;
1131 spin_unlock(&c
->erase_completion_lock
);
1132 unrefer_xattr_datum(c
, xd
);
1136 ref
->xseqno
&= ~XREF_DELETE_MARKER
;
1144 if (flags
& XATTR_REPLACE
) {
1153 xd
= create_xattr_datum(c
, xprefix
, xname
, buffer
, size
);
1158 up_write(&c
->xattr_sem
);
1159 jffs2_complete_reservation(c
);
1161 /* create xattr_ref */
1162 request
= PAD(sizeof(struct jffs2_raw_xref
));
1163 rc
= jffs2_reserve_space(c
, request
, &length
,
1164 ALLOC_NORMAL
, JFFS2_SUMMARY_XREF_SIZE
);
1165 down_write(&c
->xattr_sem
);
1167 JFFS2_WARNING("jffs2_reserve_space()=%d, request=%u\n", rc
, request
);
1168 unrefer_xattr_datum(c
, xd
);
1169 up_write(&c
->xattr_sem
);
1174 newref
= create_xattr_ref(c
, ic
, xd
);
1175 if (IS_ERR(newref
)) {
1177 ref
->next
= ic
->xref
;
1180 rc
= PTR_ERR(newref
);
1181 unrefer_xattr_datum(c
, xd
);
1183 delete_xattr_ref(c
, ref
);
1186 up_write(&c
->xattr_sem
);
1187 jffs2_complete_reservation(c
);
1191 /* -------- garbage collector functions -------------
1192 * jffs2_garbage_collect_xattr_datum(c, xd, raw)
1193 * is used to move xdatum into new node.
1194 * jffs2_garbage_collect_xattr_ref(c, ref, raw)
1195 * is used to move xref into new node.
1196 * jffs2_verify_xattr(c)
1197 * is used to call do_verify_xattr_datum() before garbage collecting.
1198 * jffs2_release_xattr_datum(c, xd)
1199 * is used to release an in-memory object of xdatum.
1200 * jffs2_release_xattr_ref(c, ref)
1201 * is used to release an in-memory object of xref.
1202 * -------------------------------------------------- */
1203 int jffs2_garbage_collect_xattr_datum(struct jffs2_sb_info
*c
, struct jffs2_xattr_datum
*xd
,
1204 struct jffs2_raw_node_ref
*raw
)
1206 uint32_t totlen
, length
, old_ofs
;
1209 down_write(&c
->xattr_sem
);
1210 if (xd
->node
!= raw
)
1212 if (xd
->flags
& (JFFS2_XFLAGS_DEAD
|JFFS2_XFLAGS_INVALID
))
1215 rc
= load_xattr_datum(c
, xd
);
1217 rc
= (rc
> 0) ? 0 : rc
;
1220 old_ofs
= ref_offset(xd
->node
);
1221 totlen
= PAD(sizeof(struct jffs2_raw_xattr
)
1222 + xd
->name_len
+ 1 + xd
->value_len
);
1223 rc
= jffs2_reserve_space_gc(c
, totlen
, &length
, JFFS2_SUMMARY_XATTR_SIZE
);
1225 JFFS2_WARNING("jffs2_reserve_space_gc()=%d, request=%u\n", rc
, totlen
);
1228 rc
= save_xattr_datum(c
, xd
);
1230 dbg_xattr("xdatum (xid=%u, version=%u) GC'ed from %#08x to %08x\n",
1231 xd
->xid
, xd
->version
, old_ofs
, ref_offset(xd
->node
));
1234 jffs2_mark_node_obsolete(c
, raw
);
1235 up_write(&c
->xattr_sem
);
1239 int jffs2_garbage_collect_xattr_ref(struct jffs2_sb_info
*c
, struct jffs2_xattr_ref
*ref
,
1240 struct jffs2_raw_node_ref
*raw
)
1242 uint32_t totlen
, length
, old_ofs
;
1245 down_write(&c
->xattr_sem
);
1248 if (ref
->node
!= raw
)
1250 if (is_xattr_ref_dead(ref
) && (raw
->next_in_ino
== (void *)ref
))
1253 old_ofs
= ref_offset(ref
->node
);
1254 totlen
= ref_totlen(c
, c
->gcblock
, ref
->node
);
1256 rc
= jffs2_reserve_space_gc(c
, totlen
, &length
, JFFS2_SUMMARY_XREF_SIZE
);
1258 JFFS2_WARNING("%s: jffs2_reserve_space_gc() = %d, request = %u\n",
1259 __func__
, rc
, totlen
);
1260 rc
= rc
? rc
: -EBADFD
;
1263 rc
= save_xattr_ref(c
, ref
);
1265 dbg_xattr("xref (ino=%u, xid=%u) GC'ed from %#08x to %08x\n",
1266 ref
->ic
->ino
, ref
->xd
->xid
, old_ofs
, ref_offset(ref
->node
));
1269 jffs2_mark_node_obsolete(c
, raw
);
1270 up_write(&c
->xattr_sem
);
1274 int jffs2_verify_xattr(struct jffs2_sb_info
*c
)
1276 struct jffs2_xattr_datum
*xd
, *_xd
;
1277 struct jffs2_eraseblock
*jeb
;
1278 struct jffs2_raw_node_ref
*raw
;
1282 down_write(&c
->xattr_sem
);
1283 list_for_each_entry_safe(xd
, _xd
, &c
->xattr_unchecked
, xindex
) {
1284 rc
= do_verify_xattr_datum(c
, xd
);
1287 list_del_init(&xd
->xindex
);
1288 spin_lock(&c
->erase_completion_lock
);
1289 for (raw
=xd
->node
; raw
!= (void *)xd
; raw
=raw
->next_in_ino
) {
1290 if (ref_flags(raw
) != REF_UNCHECKED
)
1292 jeb
= &c
->blocks
[ref_offset(raw
) / c
->sector_size
];
1293 totlen
= PAD(ref_totlen(c
, jeb
, raw
));
1294 c
->unchecked_size
-= totlen
; c
->used_size
+= totlen
;
1295 jeb
->unchecked_size
-= totlen
; jeb
->used_size
+= totlen
;
1296 raw
->flash_offset
= ref_offset(raw
)
1297 | ((xd
->node
== (void *)raw
) ? REF_PRISTINE
: REF_NORMAL
);
1299 if (xd
->flags
& JFFS2_XFLAGS_DEAD
)
1300 list_add(&xd
->xindex
, &c
->xattr_dead_list
);
1301 spin_unlock(&c
->erase_completion_lock
);
1303 up_write(&c
->xattr_sem
);
1304 return list_empty(&c
->xattr_unchecked
) ? 1 : 0;
1307 void jffs2_release_xattr_datum(struct jffs2_sb_info
*c
, struct jffs2_xattr_datum
*xd
)
1309 /* must be called under spin_lock(&c->erase_completion_lock) */
1310 if (atomic_read(&xd
->refcnt
) || xd
->node
!= (void *)xd
)
1313 list_del(&xd
->xindex
);
1314 jffs2_free_xattr_datum(xd
);
1317 void jffs2_release_xattr_ref(struct jffs2_sb_info
*c
, struct jffs2_xattr_ref
*ref
)
1319 /* must be called under spin_lock(&c->erase_completion_lock) */
1320 struct jffs2_xattr_ref
*tmp
, **ptmp
;
1322 if (ref
->node
!= (void *)ref
)
1325 for (tmp
=c
->xref_dead_list
, ptmp
=&c
->xref_dead_list
; tmp
; ptmp
=&tmp
->next
, tmp
=tmp
->next
) {
1331 jffs2_free_xattr_ref(ref
);