]>
Commit | Line | Data |
---|---|---|
652ecc20 KK |
1 | /* |
2 | * JFFS2 -- Journalling Flash File System, Version 2. | |
aa98d7cf | 3 | * |
652ecc20 | 4 | * Copyright (C) 2006 NEC Corporation |
aa98d7cf | 5 | * |
652ecc20 KK |
6 | * Created by KaiGai Kohei <kaigai@ak.jp.nec.com> |
7 | * | |
8 | * For licensing information, see the file 'LICENCE' in this directory. | |
9 | * | |
10 | */ | |
aa98d7cf KK |
11 | #include <linux/kernel.h> |
12 | #include <linux/slab.h> | |
13 | #include <linux/fs.h> | |
14 | #include <linux/time.h> | |
15 | #include <linux/pagemap.h> | |
16 | #include <linux/highmem.h> | |
17 | #include <linux/crc32.h> | |
18 | #include <linux/jffs2.h> | |
19 | #include <linux/xattr.h> | |
20 | #include <linux/mtd/mtd.h> | |
21 | #include "nodelist.h" | |
22 | /* -------- xdatum related functions ---------------- | |
23 | * xattr_datum_hashkey(xprefix, xname, xvalue, xsize) | |
24 | * is used to calcurate xdatum hashkey. The reminder of hashkey into XATTRINDEX_HASHSIZE is | |
25 | * the index of the xattr name/value pair cache (c->xattrindex). | |
c9f700f8 KK |
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. | |
aa98d7cf KK |
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. | |
aa98d7cf KK |
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(). | |
9fe4854c | 49 | * save_xattr_datum(c, xd) |
aa98d7cf | 50 | * is used to write xdatum to medium. xd->version will be incremented. |
9fe4854c | 51 | * create_xattr_datum(c, xprefix, xname, xvalue, xsize) |
aa98d7cf | 52 | * is used to create new xdatum and write to medium. |
c9f700f8 KK |
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. | |
aa98d7cf | 59 | * -------------------------------------------------- */ |
aa98d7cf KK |
60 | static uint32_t xattr_datum_hashkey(int xprefix, const char *xname, const char *xvalue, int xsize) |
61 | { | |
62 | int name_len = strlen(xname); | |
63 | ||
64 | return crc32(xprefix, xname, name_len) ^ crc32(xprefix, xvalue, xsize); | |
65 | } | |
66 | ||
c9f700f8 KK |
67 | static int is_xattr_datum_unchecked(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd) |
68 | { | |
69 | struct jffs2_raw_node_ref *raw; | |
70 | int rc = 0; | |
71 | ||
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) { | |
75 | rc = 1; | |
76 | break; | |
77 | } | |
78 | } | |
79 | spin_unlock(&c->erase_completion_lock); | |
80 | return rc; | |
81 | } | |
82 | ||
aa98d7cf KK |
83 | static void unload_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd) |
84 | { | |
85 | /* must be called under down_write(xattr_sem) */ | |
86 | D1(dbg_xattr("%s: xid=%u, version=%u\n", __FUNCTION__, xd->xid, xd->version)); | |
87 | if (xd->xname) { | |
88 | c->xdatum_mem_usage -= (xd->name_len + 1 + xd->value_len); | |
89 | kfree(xd->xname); | |
90 | } | |
91 | ||
92 | list_del_init(&xd->xindex); | |
93 | xd->hashkey = 0; | |
94 | xd->xname = NULL; | |
95 | xd->xvalue = NULL; | |
96 | } | |
97 | ||
98 | static void reclaim_xattr_datum(struct jffs2_sb_info *c) | |
99 | { | |
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; | |
104 | int count; | |
105 | ||
106 | if (c->xdatum_mem_threshold > c->xdatum_mem_usage) | |
107 | return; | |
108 | ||
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); | |
117 | } | |
118 | if (c->xdatum_mem_usage <= target) | |
119 | goto out; | |
120 | } | |
121 | index = (index+1) % XATTRINDEX_HASHSIZE; | |
122 | } | |
123 | out: | |
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); | |
126 | } | |
127 | ||
aa98d7cf KK |
128 | static int do_verify_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd) |
129 | { | |
130 | /* must be called under down_write(xattr_sem) */ | |
131 | struct jffs2_eraseblock *jeb; | |
c9f700f8 | 132 | struct jffs2_raw_node_ref *raw; |
aa98d7cf KK |
133 | struct jffs2_raw_xattr rx; |
134 | size_t readlen; | |
c9f700f8 | 135 | uint32_t crc, offset, totlen; |
aa98d7cf KK |
136 | int rc; |
137 | ||
c9f700f8 KK |
138 | spin_lock(&c->erase_completion_lock); |
139 | offset = ref_offset(xd->node); | |
140 | if (ref_flags(xd->node) == REF_PRISTINE) | |
141 | goto complete; | |
142 | spin_unlock(&c->erase_completion_lock); | |
aa98d7cf | 143 | |
c9f700f8 | 144 | rc = jffs2_flash_read(c, offset, sizeof(rx), &readlen, (char *)&rx); |
aa98d7cf | 145 | if (rc || readlen != sizeof(rx)) { |
89291a9d | 146 | JFFS2_WARNING("jffs2_flash_read()=%d, req=%zu, read=%zu at %#08x\n", |
c9f700f8 | 147 | rc, sizeof(rx), readlen, offset); |
aa98d7cf KK |
148 | return rc ? rc : -EIO; |
149 | } | |
150 | crc = crc32(0, &rx, sizeof(rx) - 4); | |
151 | if (crc != je32_to_cpu(rx.node_crc)) { | |
c9f700f8 KK |
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; | |
aa98d7cf KK |
155 | return EIO; |
156 | } | |
c9f700f8 KK |
157 | totlen = sizeof(rx); |
158 | if (xd->version != XDATUM_DELETE_MARKER) | |
159 | totlen += rx.name_len + 1 + je16_to_cpu(rx.value_len); | |
160 | totlen = PAD(totlen); | |
aa98d7cf KK |
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", | |
c9f700f8 | 168 | offset, je16_to_cpu(rx.magic), JFFS2_MAGIC_BITMASK, |
aa98d7cf KK |
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); | |
c9f700f8 | 173 | xd->flags |= JFFS2_XFLAGS_INVALID; |
aa98d7cf KK |
174 | return EIO; |
175 | } | |
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); | |
180 | ||
aa98d7cf | 181 | spin_lock(&c->erase_completion_lock); |
c9f700f8 KK |
182 | complete: |
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; | |
189 | } | |
190 | raw->flash_offset = ref_offset(raw) | ((xd->node==raw) ? REF_PRISTINE : REF_NORMAL); | |
191 | } | |
aa98d7cf KK |
192 | spin_unlock(&c->erase_completion_lock); |
193 | ||
194 | /* unchecked xdatum is chained with c->xattr_unchecked */ | |
195 | list_del_init(&xd->xindex); | |
196 | ||
197 | dbg_xattr("success on verfying xdatum (xid=%u, version=%u)\n", | |
198 | xd->xid, xd->version); | |
199 | ||
200 | return 0; | |
201 | } | |
202 | ||
203 | static int do_load_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd) | |
204 | { | |
205 | /* must be called under down_write(xattr_sem) */ | |
206 | char *data; | |
207 | size_t readlen; | |
208 | uint32_t crc, length; | |
209 | int i, ret, retry = 0; | |
210 | ||
aa98d7cf KK |
211 | BUG_ON(ref_flags(xd->node) != REF_PRISTINE); |
212 | BUG_ON(!list_empty(&xd->xindex)); | |
213 | retry: | |
214 | length = xd->name_len + 1 + xd->value_len; | |
215 | data = kmalloc(length, GFP_KERNEL); | |
216 | if (!data) | |
217 | return -ENOMEM; | |
218 | ||
219 | ret = jffs2_flash_read(c, ref_offset(xd->node)+sizeof(struct jffs2_raw_xattr), | |
220 | length, &readlen, data); | |
221 | ||
222 | if (ret || length!=readlen) { | |
89291a9d | 223 | JFFS2_WARNING("jffs2_flash_read() returned %d, request=%d, readlen=%zu, at %#08x\n", |
aa98d7cf KK |
224 | ret, length, readlen, ref_offset(xd->node)); |
225 | kfree(data); | |
226 | return ret ? ret : -EIO; | |
227 | } | |
228 | ||
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); | |
235 | kfree(data); | |
c9f700f8 | 236 | xd->flags |= JFFS2_XFLAGS_INVALID; |
aa98d7cf KK |
237 | return EIO; |
238 | } | |
239 | ||
240 | xd->flags |= JFFS2_XFLAGS_HOT; | |
241 | xd->xname = data; | |
242 | xd->xvalue = data + xd->name_len+1; | |
243 | ||
244 | c->xdatum_mem_usage += length; | |
245 | ||
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]); | |
249 | if (!retry) { | |
250 | retry = 1; | |
251 | reclaim_xattr_datum(c); | |
252 | if (!xd->xname) | |
253 | goto retry; | |
254 | } | |
255 | ||
256 | dbg_xattr("success on loading xdatum (xid=%u, xprefix=%u, xname='%s')\n", | |
257 | xd->xid, xd->xprefix, xd->xname); | |
258 | ||
259 | return 0; | |
260 | } | |
261 | ||
262 | static int load_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd) | |
263 | { | |
264 | /* must be called under down_write(xattr_sem); | |
265 | * rc < 0 : recoverable error, try again | |
266 | * rc = 0 : success | |
267 | * rc > 0 : Unrecoverable error, this node should be deleted. | |
268 | */ | |
269 | int rc = 0; | |
c9f700f8 KK |
270 | |
271 | if (xd->xname) | |
272 | return 0; | |
273 | if (xd->flags & JFFS2_XFLAGS_INVALID) | |
aa98d7cf | 274 | return EIO; |
c9f700f8 | 275 | if (unlikely(is_xattr_datum_unchecked(c, xd))) |
aa98d7cf | 276 | rc = do_verify_xattr_datum(c, xd); |
aa98d7cf KK |
277 | if (!rc) |
278 | rc = do_load_xattr_datum(c, xd); | |
279 | return rc; | |
280 | } | |
281 | ||
9fe4854c | 282 | static int save_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd) |
aa98d7cf KK |
283 | { |
284 | /* must be called under down_write(xattr_sem) */ | |
2f785402 | 285 | struct jffs2_raw_xattr rx; |
aa98d7cf | 286 | struct kvec vecs[2]; |
89291a9d | 287 | size_t length; |
c9f700f8 | 288 | int rc, totlen, nvecs = 1; |
9fe4854c | 289 | uint32_t phys_ofs = write_ofs(c); |
aa98d7cf | 290 | |
c9f700f8 KK |
291 | BUG_ON(is_xattr_datum_dead(xd) || (xd->flags & JFFS2_XFLAGS_INVALID) |
292 | ? !!xd->xname : !xd->xname); | |
aa98d7cf KK |
293 | |
294 | vecs[0].iov_base = ℞ | |
c9f700f8 KK |
295 | vecs[0].iov_len = totlen = sizeof(rx); |
296 | if (!is_xattr_datum_dead(xd) && !(xd->flags & JFFS2_XFLAGS_INVALID)) { | |
297 | nvecs++; | |
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; | |
301 | } | |
aa98d7cf | 302 | /* Setup raw-xattr */ |
c9f700f8 | 303 | memset(&rx, 0, sizeof(rx)); |
aa98d7cf KK |
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)); | |
308 | ||
309 | rx.xid = cpu_to_je32(xd->xid); | |
c9f700f8 KK |
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)); | |
316 | } else { | |
317 | rx.version = cpu_to_je32(XDATUM_DELETE_MARKER); | |
318 | } | |
aa98d7cf KK |
319 | rx.node_crc = cpu_to_je32(crc32(0, &rx, sizeof(struct jffs2_raw_xattr) - 4)); |
320 | ||
c9f700f8 | 321 | rc = jffs2_flash_writev(c, vecs, nvecs, phys_ofs, &length, 0); |
aa98d7cf | 322 | if (rc || totlen != length) { |
89291a9d | 323 | JFFS2_WARNING("jffs2_flash_writev()=%d, req=%u, wrote=%zu, at %#08x\n", |
aa98d7cf KK |
324 | rc, totlen, length, phys_ofs); |
325 | rc = rc ? rc : -EIO; | |
2f785402 DW |
326 | if (length) |
327 | jffs2_add_physical_node_ref(c, phys_ofs | REF_OBSOLETE, PAD(totlen), NULL); | |
328 | ||
aa98d7cf KK |
329 | return rc; |
330 | } | |
aa98d7cf | 331 | /* success */ |
c9f700f8 | 332 | jffs2_add_physical_node_ref(c, phys_ofs | REF_PRISTINE, PAD(totlen), (void *)xd); |
aa98d7cf KK |
333 | |
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); | |
336 | ||
337 | return 0; | |
338 | } | |
339 | ||
340 | static struct jffs2_xattr_datum *create_xattr_datum(struct jffs2_sb_info *c, | |
341 | int xprefix, const char *xname, | |
9fe4854c | 342 | const char *xvalue, int xsize) |
aa98d7cf KK |
343 | { |
344 | /* must be called under down_write(xattr_sem) */ | |
345 | struct jffs2_xattr_datum *xd; | |
346 | uint32_t hashkey, name_len; | |
347 | char *data; | |
348 | int i, rc; | |
349 | ||
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)) { | |
359 | xd->refcnt++; | |
360 | return xd; | |
361 | } | |
362 | } | |
363 | ||
364 | /* Not found, Create NEW XATTR-Cache */ | |
365 | name_len = strlen(xname); | |
366 | ||
367 | xd = jffs2_alloc_xattr_datum(); | |
368 | if (!xd) | |
369 | return ERR_PTR(-ENOMEM); | |
370 | ||
371 | data = kmalloc(name_len + 1 + xsize, GFP_KERNEL); | |
372 | if (!data) { | |
373 | jffs2_free_xattr_datum(xd); | |
374 | return ERR_PTR(-ENOMEM); | |
375 | } | |
376 | strcpy(data, xname); | |
377 | memcpy(data + name_len + 1, xvalue, xsize); | |
378 | ||
379 | xd->refcnt = 1; | |
380 | xd->xid = ++c->highest_xid; | |
381 | xd->flags |= JFFS2_XFLAGS_HOT; | |
382 | xd->xprefix = xprefix; | |
383 | ||
384 | xd->hashkey = hashkey; | |
385 | xd->xname = data; | |
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); | |
390 | ||
9fe4854c | 391 | rc = save_xattr_datum(c, xd); |
aa98d7cf KK |
392 | if (rc) { |
393 | kfree(xd->xname); | |
394 | jffs2_free_xattr_datum(xd); | |
395 | return ERR_PTR(rc); | |
396 | } | |
397 | ||
398 | /* Insert Hash Index */ | |
399 | i = hashkey % XATTRINDEX_HASHSIZE; | |
400 | list_add(&xd->xindex, &c->xattrindex[i]); | |
401 | ||
402 | c->xdatum_mem_usage += (xd->name_len + 1 + xd->value_len); | |
403 | reclaim_xattr_datum(c); | |
404 | ||
405 | return xd; | |
406 | } | |
407 | ||
c9f700f8 KK |
408 | static void delete_xattr_datum_delay(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd) |
409 | { | |
410 | /* must be called under down_write(xattr_sem) */ | |
411 | BUG_ON(xd->refcnt); | |
412 | ||
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); | |
420 | } | |
421 | ||
422 | static void delete_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd) | |
423 | { | |
424 | /* must be called under jffs2_reserve_space() and down_write(xattr_sem) */ | |
425 | int rc; | |
426 | BUG_ON(xd->refcnt); | |
427 | ||
428 | unload_xattr_datum(c, xd); | |
429 | set_xattr_datum_dead(xd); | |
430 | rc = save_xattr_datum(c, xd); | |
431 | if (rc) { | |
432 | JFFS2_NOTICE("xdatum(xid=%u) was removed without delete marker. " | |
433 | "An orphan xdatum may be detected on next mounting.\n", | |
434 | xd->xid); | |
435 | } | |
436 | spin_lock(&c->erase_completion_lock); | |
437 | list_add(&xd->xindex, &c->xattr_dead_list); | |
438 | spin_unlock(&c->erase_completion_lock); | |
439 | } | |
440 | ||
21b9879b | 441 | /* -------- xref related functions ------------------ |
aa98d7cf KK |
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. | |
9fe4854c | 451 | * save_xattr_ref(c, ref) |
aa98d7cf | 452 | * is used to write xref to medium. |
9fe4854c | 453 | * create_xattr_ref(c, ic, xd) |
aa98d7cf KK |
454 | * is used to create a new xref and write to medium. |
455 | * jffs2_xattr_delete_inode(c, ic) | |
456 | * is called to remove xrefs related to obsolete inode when inode is unlinked. | |
457 | * jffs2_xattr_free_inode(c, ic) | |
458 | * is called to release xattr related objects when unmounting. | |
8f2b6f49 | 459 | * check_xattr_ref_inode(c, ic) |
aa98d7cf KK |
460 | * is used to confirm inode does not have duplicate xattr name/value pair. |
461 | * -------------------------------------------------- */ | |
462 | static int verify_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref) | |
463 | { | |
464 | struct jffs2_eraseblock *jeb; | |
c9f700f8 | 465 | struct jffs2_raw_node_ref *raw; |
aa98d7cf KK |
466 | struct jffs2_raw_xref rr; |
467 | size_t readlen; | |
c9f700f8 | 468 | uint32_t crc, offset, totlen; |
aa98d7cf KK |
469 | int rc; |
470 | ||
c9f700f8 KK |
471 | spin_lock(&c->erase_completion_lock); |
472 | if (ref_flags(ref->node) != REF_UNCHECKED) | |
473 | goto complete; | |
474 | offset = ref_offset(ref->node); | |
475 | spin_unlock(&c->erase_completion_lock); | |
aa98d7cf | 476 | |
c9f700f8 | 477 | rc = jffs2_flash_read(c, offset, sizeof(rr), &readlen, (char *)&rr); |
aa98d7cf | 478 | if (rc || sizeof(rr) != readlen) { |
89291a9d | 479 | JFFS2_WARNING("jffs2_flash_read()=%d, req=%zu, read=%zu, at %#08x\n", |
c9f700f8 | 480 | rc, sizeof(rr), readlen, offset); |
aa98d7cf KK |
481 | return rc ? rc : -EIO; |
482 | } | |
483 | /* obsolete node */ | |
484 | crc = crc32(0, &rr, sizeof(rr) - 4); | |
485 | if (crc != je32_to_cpu(rr.node_crc)) { | |
c9f700f8 KK |
486 | JFFS2_ERROR("node CRC failed at %#08x, read=%#08x, calc=%#08x\n", |
487 | offset, je32_to_cpu(rr.node_crc), crc); | |
aa98d7cf KK |
488 | return EIO; |
489 | } | |
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, " | |
89291a9d | 494 | "nodetype=%#04x/%#04x, totlen=%u/%zu\n", |
c9f700f8 | 495 | offset, je16_to_cpu(rr.magic), JFFS2_MAGIC_BITMASK, |
aa98d7cf KK |
496 | je16_to_cpu(rr.nodetype), JFFS2_NODETYPE_XREF, |
497 | je32_to_cpu(rr.totlen), PAD(sizeof(rr))); | |
498 | return EIO; | |
499 | } | |
500 | ref->ino = je32_to_cpu(rr.ino); | |
501 | ref->xid = je32_to_cpu(rr.xid); | |
c9f700f8 KK |
502 | ref->xseqno = je32_to_cpu(rr.xseqno); |
503 | if (ref->xseqno > c->highest_xseqno) | |
504 | c->highest_xseqno = (ref->xseqno & ~XREF_DELETE_MARKER); | |
aa98d7cf KK |
505 | |
506 | spin_lock(&c->erase_completion_lock); | |
c9f700f8 KK |
507 | complete: |
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; | |
514 | } | |
515 | raw->flash_offset = ref_offset(raw) | ((ref->node==raw) ? REF_PRISTINE : REF_NORMAL); | |
516 | } | |
aa98d7cf KK |
517 | spin_unlock(&c->erase_completion_lock); |
518 | ||
519 | dbg_xattr("success on verifying xref (ino=%u, xid=%u) at %#08x\n", | |
520 | ref->ino, ref->xid, ref_offset(ref->node)); | |
521 | return 0; | |
522 | } | |
523 | ||
9fe4854c | 524 | static int save_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref) |
aa98d7cf KK |
525 | { |
526 | /* must be called under down_write(xattr_sem) */ | |
aa98d7cf | 527 | struct jffs2_raw_xref rr; |
89291a9d | 528 | size_t length; |
c9f700f8 | 529 | uint32_t xseqno, phys_ofs = write_ofs(c); |
aa98d7cf KK |
530 | int ret; |
531 | ||
aa98d7cf KK |
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)); | |
536 | ||
c9f700f8 KK |
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); | |
542 | } else { | |
543 | rr.ino = cpu_to_je32(ref->ic->ino); | |
544 | rr.xid = cpu_to_je32(ref->xd->xid); | |
545 | } | |
546 | rr.xseqno = cpu_to_je32(xseqno); | |
aa98d7cf KK |
547 | rr.node_crc = cpu_to_je32(crc32(0, &rr, sizeof(rr) - 4)); |
548 | ||
549 | ret = jffs2_flash_write(c, phys_ofs, sizeof(rr), &length, (char *)&rr); | |
550 | if (ret || sizeof(rr) != length) { | |
89291a9d | 551 | JFFS2_WARNING("jffs2_flash_write() returned %d, request=%zu, retlen=%zu, at %#08x\n", |
aa98d7cf KK |
552 | ret, sizeof(rr), length, phys_ofs); |
553 | ret = ret ? ret : -EIO; | |
2f785402 DW |
554 | if (length) |
555 | jffs2_add_physical_node_ref(c, phys_ofs | REF_OBSOLETE, PAD(sizeof(rr)), NULL); | |
556 | ||
aa98d7cf KK |
557 | return ret; |
558 | } | |
c9f700f8 KK |
559 | /* success */ |
560 | ref->xseqno = xseqno; | |
561 | jffs2_add_physical_node_ref(c, phys_ofs | REF_PRISTINE, PAD(sizeof(rr)), (void *)ref); | |
aa98d7cf KK |
562 | |
563 | dbg_xattr("success on saving xref (ino=%u, xid=%u)\n", ref->ic->ino, ref->xd->xid); | |
564 | ||
565 | return 0; | |
566 | } | |
567 | ||
568 | static struct jffs2_xattr_ref *create_xattr_ref(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic, | |
9fe4854c | 569 | struct jffs2_xattr_datum *xd) |
aa98d7cf KK |
570 | { |
571 | /* must be called under down_write(xattr_sem) */ | |
572 | struct jffs2_xattr_ref *ref; | |
573 | int ret; | |
574 | ||
575 | ref = jffs2_alloc_xattr_ref(); | |
576 | if (!ref) | |
577 | return ERR_PTR(-ENOMEM); | |
578 | ref->ic = ic; | |
579 | ref->xd = xd; | |
580 | ||
9fe4854c | 581 | ret = save_xattr_ref(c, ref); |
aa98d7cf KK |
582 | if (ret) { |
583 | jffs2_free_xattr_ref(ref); | |
584 | return ERR_PTR(ret); | |
585 | } | |
586 | ||
587 | /* Chain to inode */ | |
8f2b6f49 KK |
588 | ref->next = ic->xref; |
589 | ic->xref = ref; | |
aa98d7cf KK |
590 | |
591 | return ref; /* success */ | |
592 | } | |
593 | ||
c9f700f8 KK |
594 | static void delete_xattr_ref_delay(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref) |
595 | { | |
596 | /* must be called under down_write(xattr_sem) */ | |
597 | struct jffs2_xattr_datum *xd; | |
598 | ||
599 | set_xattr_ref_dead(ref); | |
600 | xd = ref->xd; | |
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); | |
607 | ||
608 | JFFS2_NOTICE("xref(ino=%u, xid=%u) was removed without delete marker. " | |
609 | "An orphan xref may be detected on next mounting.\n", | |
610 | ref->ino, ref->xid); | |
611 | ||
612 | if (!--xd->refcnt) | |
613 | delete_xattr_datum_delay(c, xd); | |
614 | } | |
615 | ||
616 | static int delete_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref, int enforce) | |
617 | { | |
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; | |
621 | uint32_t length; | |
622 | int rc; | |
623 | ||
624 | set_xattr_ref_dead(ref); | |
625 | ic = ref->ic; | |
626 | xd = ref->xd; | |
627 | ref->ino = ic->ino; | |
628 | ref->xid = xd->xid; | |
629 | rc = save_xattr_ref(c, ref); | |
630 | if (rc) { | |
631 | if (!enforce) { | |
632 | clr_xattr_ref_dead(ref); | |
633 | ref->ic = ic; | |
634 | ref->xd = xd; | |
635 | return rc; | |
636 | } | |
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); | |
640 | } | |
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); | |
645 | ||
646 | xd->refcnt--; | |
647 | if (xd->refcnt) | |
648 | return 0; | |
649 | ||
650 | /* delete xdatum */ | |
651 | unload_xattr_datum(c, xd); | |
652 | up_write(&c->xattr_sem); | |
653 | jffs2_complete_reservation(c); | |
654 | ||
655 | rc = jffs2_reserve_space(c, PAD(sizeof(struct jffs2_raw_xattr)), &length, | |
656 | ALLOC_DELETION, JFFS2_SUMMARY_XATTR_SIZE); | |
657 | if (rc) { | |
658 | down(&c->alloc_sem); | |
659 | down_write(&c->xattr_sem); | |
660 | delete_xattr_datum_delay(c, xd); | |
661 | } else { | |
662 | down_write(&c->xattr_sem); | |
663 | delete_xattr_datum(c, xd); | |
664 | } | |
665 | return 0; | |
666 | } | |
667 | ||
aa98d7cf KK |
668 | void jffs2_xattr_delete_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic) |
669 | { | |
670 | /* It's called from jffs2_clear_inode() on inode removing. | |
671 | When an inode with XATTR is removed, those XATTRs must be removed. */ | |
c9f700f8 KK |
672 | struct jffs2_xattr_ref *ref; |
673 | uint32_t length; | |
674 | int rc, retry; | |
aa98d7cf KK |
675 | |
676 | if (!ic || ic->nlink > 0) | |
677 | return; | |
678 | ||
c9f700f8 KK |
679 | down_read(&c->xattr_sem); |
680 | if (!ic->xref) { | |
681 | up_read(&c->xattr_sem); | |
682 | return; | |
683 | } | |
684 | up_read(&c->xattr_sem); | |
685 | retry: | |
686 | rc = jffs2_reserve_space(c, PAD(sizeof(struct jffs2_raw_xref)), &length, | |
687 | ALLOC_DELETION, JFFS2_SUMMARY_XREF_SIZE); | |
aa98d7cf | 688 | down_write(&c->xattr_sem); |
c9f700f8 KK |
689 | if (ic->xref) { |
690 | ref = ic->xref; | |
691 | ic->xref = ref->next; | |
692 | if (rc) { | |
693 | delete_xattr_ref_delay(c, ref); | |
694 | } else { | |
695 | delete_xattr_ref(c, ref, 1); | |
696 | } | |
8f2b6f49 | 697 | } |
c9f700f8 | 698 | retry = ic->xref ? 1 : 0; |
aa98d7cf | 699 | up_write(&c->xattr_sem); |
c9f700f8 KK |
700 | if (!rc) |
701 | jffs2_complete_reservation(c); | |
702 | if (retry) | |
703 | goto retry; | |
aa98d7cf KK |
704 | } |
705 | ||
706 | void jffs2_xattr_free_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic) | |
707 | { | |
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; | |
711 | ||
712 | down_write(&c->xattr_sem); | |
8f2b6f49 KK |
713 | for (ref = ic->xref; ref; ref = _ref) { |
714 | _ref = ref->next; | |
aa98d7cf KK |
715 | xd = ref->xd; |
716 | xd->refcnt--; | |
717 | if (!xd->refcnt) { | |
718 | unload_xattr_datum(c, xd); | |
719 | jffs2_free_xattr_datum(xd); | |
720 | } | |
721 | jffs2_free_xattr_ref(ref); | |
722 | } | |
8f2b6f49 | 723 | ic->xref = NULL; |
aa98d7cf KK |
724 | up_write(&c->xattr_sem); |
725 | } | |
726 | ||
8f2b6f49 | 727 | static int check_xattr_ref_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic) |
aa98d7cf | 728 | { |
8f2b6f49 | 729 | /* success of check_xattr_ref_inode() means taht inode (ic) dose not have |
aa98d7cf KK |
730 | * duplicate name/value pairs. If duplicate name/value pair would be found, |
731 | * one will be removed. | |
732 | */ | |
c9f700f8 | 733 | struct jffs2_xattr_ref *ref, *cmp, **pref, **pcmp; |
aa98d7cf KK |
734 | int rc = 0; |
735 | ||
736 | if (likely(ic->flags & INO_FLAGS_XATTR_CHECKED)) | |
737 | return 0; | |
738 | down_write(&c->xattr_sem); | |
739 | retry: | |
740 | rc = 0; | |
8f2b6f49 | 741 | for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) { |
aa98d7cf KK |
742 | if (!ref->xd->xname) { |
743 | rc = load_xattr_datum(c, ref->xd); | |
744 | if (unlikely(rc > 0)) { | |
8f2b6f49 | 745 | *pref = ref->next; |
c9f700f8 | 746 | delete_xattr_ref_delay(c, ref); |
aa98d7cf KK |
747 | goto retry; |
748 | } else if (unlikely(rc < 0)) | |
749 | goto out; | |
750 | } | |
c9f700f8 | 751 | for (cmp=ref->next, pcmp=&ref->next; cmp; pcmp=&cmp->next, cmp=cmp->next) { |
aa98d7cf KK |
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)) { | |
c9f700f8 KK |
757 | *pcmp = cmp->next; |
758 | delete_xattr_ref_delay(c, cmp); | |
aa98d7cf KK |
759 | goto retry; |
760 | } else if (unlikely(rc < 0)) | |
761 | goto out; | |
762 | } | |
763 | if (ref->xd->xprefix == cmp->xd->xprefix | |
764 | && !strcmp(ref->xd->xname, cmp->xd->xname)) { | |
c9f700f8 KK |
765 | if (ref->xseqno > cmp->xseqno) { |
766 | *pcmp = cmp->next; | |
767 | delete_xattr_ref_delay(c, cmp); | |
768 | } else { | |
769 | *pref = ref->next; | |
770 | delete_xattr_ref_delay(c, ref); | |
771 | } | |
aa98d7cf KK |
772 | goto retry; |
773 | } | |
774 | } | |
775 | } | |
776 | ic->flags |= INO_FLAGS_XATTR_CHECKED; | |
777 | out: | |
778 | up_write(&c->xattr_sem); | |
779 | ||
780 | return rc; | |
781 | } | |
782 | ||
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) | |
796 | { | |
797 | int i; | |
798 | ||
799 | for (i=0; i < XATTRINDEX_HASHSIZE; i++) | |
800 | INIT_LIST_HEAD(&c->xattrindex[i]); | |
aa98d7cf | 801 | INIT_LIST_HEAD(&c->xattr_unchecked); |
c9f700f8 KK |
802 | INIT_LIST_HEAD(&c->xattr_dead_list); |
803 | c->xref_dead_list = NULL; | |
8f2b6f49 | 804 | c->xref_temp = NULL; |
aa98d7cf KK |
805 | |
806 | init_rwsem(&c->xattr_sem); | |
c9f700f8 KK |
807 | c->highest_xid = 0; |
808 | c->highest_xseqno = 0; | |
aa98d7cf KK |
809 | c->xdatum_mem_usage = 0; |
810 | c->xdatum_mem_threshold = 32 * 1024; /* Default 32KB */ | |
811 | } | |
812 | ||
813 | static struct jffs2_xattr_datum *jffs2_find_xattr_datum(struct jffs2_sb_info *c, uint32_t xid) | |
814 | { | |
815 | struct jffs2_xattr_datum *xd; | |
816 | int i = xid % XATTRINDEX_HASHSIZE; | |
817 | ||
818 | /* It's only used in scanning/building process. */ | |
819 | BUG_ON(!(c->flags & (JFFS2_SB_FLAG_SCANNING|JFFS2_SB_FLAG_BUILDING))); | |
820 | ||
821 | list_for_each_entry(xd, &c->xattrindex[i], xindex) { | |
822 | if (xd->xid==xid) | |
823 | return xd; | |
824 | } | |
825 | return NULL; | |
826 | } | |
827 | ||
828 | void jffs2_clear_xattr_subsystem(struct jffs2_sb_info *c) | |
829 | { | |
830 | struct jffs2_xattr_datum *xd, *_xd; | |
831 | struct jffs2_xattr_ref *ref, *_ref; | |
832 | int i; | |
833 | ||
8f2b6f49 KK |
834 | for (ref=c->xref_temp; ref; ref = _ref) { |
835 | _ref = ref->next; | |
aa98d7cf | 836 | jffs2_free_xattr_ref(ref); |
8f2b6f49 | 837 | } |
c9f700f8 KK |
838 | |
839 | for (ref=c->xref_dead_list; ref; ref = _ref) { | |
840 | _ref = ref->next; | |
841 | jffs2_free_xattr_ref(ref); | |
842 | } | |
aa98d7cf KK |
843 | |
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); | |
847 | if (xd->xname) | |
848 | kfree(xd->xname); | |
849 | jffs2_free_xattr_datum(xd); | |
850 | } | |
851 | } | |
c9f700f8 KK |
852 | |
853 | list_for_each_entry_safe(xd, _xd, &c->xattr_dead_list, xindex) { | |
854 | list_del(&xd->xindex); | |
855 | jffs2_free_xattr_datum(xd); | |
856 | } | |
aa98d7cf KK |
857 | } |
858 | ||
c9f700f8 | 859 | #define XREF_TMPHASH_SIZE (128) |
aa98d7cf KK |
860 | void jffs2_build_xattr_subsystem(struct jffs2_sb_info *c) |
861 | { | |
862 | struct jffs2_xattr_ref *ref, *_ref; | |
c9f700f8 | 863 | struct jffs2_xattr_ref *xref_tmphash[XREF_TMPHASH_SIZE]; |
aa98d7cf KK |
864 | struct jffs2_xattr_datum *xd, *_xd; |
865 | struct jffs2_inode_cache *ic; | |
c9f700f8 KK |
866 | struct jffs2_raw_node_ref *raw; |
867 | int i, xdatum_count = 0, xdatum_unchecked_count = 0, xref_count = 0; | |
aa98d7cf KK |
868 | |
869 | BUG_ON(!(c->flags & JFFS2_SB_FLAG_BUILDING)); | |
c9f700f8 KK |
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); | |
877 | } | |
878 | } | |
879 | } | |
aa98d7cf | 880 | |
c9f700f8 KK |
881 | /* Phase.2 : Merge same xref */ |
882 | for (i=0; i < XREF_TMPHASH_SIZE; i++) | |
883 | xref_tmphash[i] = NULL; | |
8f2b6f49 | 884 | for (ref=c->xref_temp; ref; ref=_ref) { |
c9f700f8 KK |
885 | struct jffs2_xattr_ref *tmp; |
886 | ||
8f2b6f49 | 887 | _ref = ref->next; |
aa98d7cf KK |
888 | if (ref_flags(ref->node) != REF_PRISTINE) { |
889 | if (verify_xattr_ref(c, ref)) { | |
c9f700f8 KK |
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); | |
aa98d7cf KK |
893 | jffs2_free_xattr_ref(ref); |
894 | continue; | |
895 | } | |
896 | } | |
c9f700f8 KK |
897 | |
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) | |
901 | break; | |
902 | } | |
903 | if (tmp) { | |
904 | raw = ref->node; | |
905 | if (ref->xseqno > tmp->xseqno) { | |
906 | tmp->xseqno = ref->xseqno; | |
907 | raw->next_in_ino = tmp->node; | |
908 | tmp->node = raw; | |
909 | } else { | |
910 | raw->next_in_ino = tmp->node->next_in_ino; | |
911 | tmp->node->next_in_ino = raw; | |
912 | } | |
aa98d7cf KK |
913 | jffs2_free_xattr_ref(ref); |
914 | continue; | |
c9f700f8 KK |
915 | } else { |
916 | ref->next = xref_tmphash[i]; | |
917 | xref_tmphash[i] = ref; | |
aa98d7cf | 918 | } |
aa98d7cf | 919 | } |
8f2b6f49 | 920 | c->xref_temp = NULL; |
aa98d7cf | 921 | |
c9f700f8 KK |
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) { | |
925 | _ref = ref->next; | |
926 | if (is_xattr_ref_dead(ref)) { | |
927 | ref->next = c->xref_dead_list; | |
928 | c->xref_dead_list = ref; | |
929 | continue; | |
930 | } | |
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); | |
935 | if (!xd || !ic) { | |
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; | |
941 | continue; | |
942 | } | |
943 | ref->xd = xd; | |
944 | ref->ic = ic; | |
945 | xd->refcnt++; | |
946 | ref->next = ic->xref; | |
947 | ic->xref = ref; | |
948 | xref_count++; | |
949 | } | |
950 | } | |
951 | ||
952 | /* Phase.4 : Link unchecked xdatum to xattr_unchecked list */ | |
aa98d7cf KK |
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); | |
956 | if (!xd->refcnt) { | |
c9f700f8 KK |
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); | |
aa98d7cf KK |
961 | continue; |
962 | } | |
c9f700f8 KK |
963 | if (is_xattr_datum_unchecked(c, xd)) { |
964 | dbg_xattr("unchecked xdatum(xid=%u, version=%u)\n", | |
965 | xd->xid, xd->version); | |
aa98d7cf KK |
966 | list_add(&xd->xindex, &c->xattr_unchecked); |
967 | xdatum_unchecked_count++; | |
968 | } | |
969 | xdatum_count++; | |
970 | } | |
971 | } | |
972 | /* build complete */ | |
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); | |
975 | } | |
976 | ||
977 | struct jffs2_xattr_datum *jffs2_setup_xattr_datum(struct jffs2_sb_info *c, | |
978 | uint32_t xid, uint32_t version) | |
979 | { | |
c9f700f8 | 980 | struct jffs2_xattr_datum *xd; |
aa98d7cf | 981 | |
c9f700f8 KK |
982 | xd = jffs2_find_xattr_datum(c, xid); |
983 | if (!xd) { | |
984 | xd = jffs2_alloc_xattr_datum(); | |
985 | if (!xd) | |
986 | return ERR_PTR(-ENOMEM); | |
987 | xd->xid = xid; | |
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]); | |
aa98d7cf KK |
992 | } |
993 | return xd; | |
994 | } | |
995 | ||
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, | |
1010 | #endif | |
1011 | #ifdef CONFIG_JFFS2_FS_POSIX_ACL | |
1012 | &jffs2_acl_access_xattr_handler, | |
1013 | &jffs2_acl_default_xattr_handler, | |
1014 | #endif | |
1015 | &jffs2_trusted_xattr_handler, | |
1016 | NULL | |
1017 | }; | |
1018 | ||
1019 | static struct xattr_handler *xprefix_to_handler(int xprefix) { | |
1020 | struct xattr_handler *ret; | |
1021 | ||
1022 | switch (xprefix) { | |
1023 | case JFFS2_XPREFIX_USER: | |
1024 | ret = &jffs2_user_xattr_handler; | |
1025 | break; | |
1026 | #ifdef CONFIG_JFFS2_FS_SECURITY | |
1027 | case JFFS2_XPREFIX_SECURITY: | |
1028 | ret = &jffs2_security_xattr_handler; | |
1029 | break; | |
1030 | #endif | |
1031 | #ifdef CONFIG_JFFS2_FS_POSIX_ACL | |
1032 | case JFFS2_XPREFIX_ACL_ACCESS: | |
1033 | ret = &jffs2_acl_access_xattr_handler; | |
1034 | break; | |
1035 | case JFFS2_XPREFIX_ACL_DEFAULT: | |
1036 | ret = &jffs2_acl_default_xattr_handler; | |
1037 | break; | |
1038 | #endif | |
1039 | case JFFS2_XPREFIX_TRUSTED: | |
1040 | ret = &jffs2_trusted_xattr_handler; | |
1041 | break; | |
1042 | default: | |
1043 | ret = NULL; | |
1044 | break; | |
1045 | } | |
1046 | return ret; | |
1047 | } | |
1048 | ||
1049 | ssize_t jffs2_listxattr(struct dentry *dentry, char *buffer, size_t size) | |
1050 | { | |
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; | |
8f2b6f49 | 1055 | struct jffs2_xattr_ref *ref, **pref; |
aa98d7cf KK |
1056 | struct jffs2_xattr_datum *xd; |
1057 | struct xattr_handler *xhandle; | |
1058 | ssize_t len, rc; | |
1059 | int retry = 0; | |
1060 | ||
8f2b6f49 | 1061 | rc = check_xattr_ref_inode(c, ic); |
aa98d7cf KK |
1062 | if (unlikely(rc)) |
1063 | return rc; | |
1064 | ||
1065 | down_read(&c->xattr_sem); | |
1066 | retry: | |
1067 | len = 0; | |
8f2b6f49 | 1068 | for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) { |
aa98d7cf KK |
1069 | BUG_ON(ref->ic != ic); |
1070 | xd = ref->xd; | |
1071 | if (!xd->xname) { | |
1072 | /* xdatum is unchached */ | |
1073 | if (!retry) { | |
1074 | retry = 1; | |
1075 | up_read(&c->xattr_sem); | |
1076 | down_write(&c->xattr_sem); | |
1077 | goto retry; | |
1078 | } else { | |
1079 | rc = load_xattr_datum(c, xd); | |
1080 | if (unlikely(rc > 0)) { | |
8f2b6f49 | 1081 | *pref = ref->next; |
c9f700f8 | 1082 | delete_xattr_ref_delay(c, ref); |
aa98d7cf KK |
1083 | goto retry; |
1084 | } else if (unlikely(rc < 0)) | |
1085 | goto out; | |
1086 | } | |
1087 | } | |
1088 | xhandle = xprefix_to_handler(xd->xprefix); | |
1089 | if (!xhandle) | |
1090 | continue; | |
1091 | if (buffer) { | |
1092 | rc = xhandle->list(inode, buffer+len, size-len, xd->xname, xd->name_len); | |
1093 | } else { | |
1094 | rc = xhandle->list(inode, NULL, 0, xd->xname, xd->name_len); | |
1095 | } | |
1096 | if (rc < 0) | |
1097 | goto out; | |
1098 | len += rc; | |
1099 | } | |
1100 | rc = len; | |
1101 | out: | |
1102 | if (!retry) { | |
1103 | up_read(&c->xattr_sem); | |
1104 | } else { | |
1105 | up_write(&c->xattr_sem); | |
1106 | } | |
1107 | return rc; | |
1108 | } | |
1109 | ||
1110 | int do_jffs2_getxattr(struct inode *inode, int xprefix, const char *xname, | |
1111 | char *buffer, size_t size) | |
1112 | { | |
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; | |
8f2b6f49 | 1117 | struct jffs2_xattr_ref *ref, **pref; |
aa98d7cf KK |
1118 | int rc, retry = 0; |
1119 | ||
8f2b6f49 | 1120 | rc = check_xattr_ref_inode(c, ic); |
aa98d7cf KK |
1121 | if (unlikely(rc)) |
1122 | return rc; | |
1123 | ||
1124 | down_read(&c->xattr_sem); | |
1125 | retry: | |
8f2b6f49 | 1126 | for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) { |
aa98d7cf KK |
1127 | BUG_ON(ref->ic!=ic); |
1128 | ||
1129 | xd = ref->xd; | |
1130 | if (xd->xprefix != xprefix) | |
1131 | continue; | |
1132 | if (!xd->xname) { | |
1133 | /* xdatum is unchached */ | |
1134 | if (!retry) { | |
1135 | retry = 1; | |
1136 | up_read(&c->xattr_sem); | |
1137 | down_write(&c->xattr_sem); | |
1138 | goto retry; | |
1139 | } else { | |
1140 | rc = load_xattr_datum(c, xd); | |
1141 | if (unlikely(rc > 0)) { | |
8f2b6f49 | 1142 | *pref = ref->next; |
c9f700f8 | 1143 | delete_xattr_ref_delay(c, ref); |
aa98d7cf KK |
1144 | goto retry; |
1145 | } else if (unlikely(rc < 0)) { | |
1146 | goto out; | |
1147 | } | |
1148 | } | |
1149 | } | |
1150 | if (!strcmp(xname, xd->xname)) { | |
1151 | rc = xd->value_len; | |
1152 | if (buffer) { | |
1153 | if (size < rc) { | |
1154 | rc = -ERANGE; | |
1155 | } else { | |
1156 | memcpy(buffer, xd->xvalue, rc); | |
1157 | } | |
1158 | } | |
1159 | goto out; | |
1160 | } | |
1161 | } | |
1162 | rc = -ENODATA; | |
1163 | out: | |
1164 | if (!retry) { | |
1165 | up_read(&c->xattr_sem); | |
1166 | } else { | |
1167 | up_write(&c->xattr_sem); | |
1168 | } | |
1169 | return rc; | |
1170 | } | |
1171 | ||
1172 | int do_jffs2_setxattr(struct inode *inode, int xprefix, const char *xname, | |
1173 | const char *buffer, size_t size, int flags) | |
1174 | { | |
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; | |
8f2b6f49 | 1179 | struct jffs2_xattr_ref *ref, *newref, **pref; |
9fe4854c | 1180 | uint32_t length, request; |
aa98d7cf KK |
1181 | int rc; |
1182 | ||
8f2b6f49 | 1183 | rc = check_xattr_ref_inode(c, ic); |
aa98d7cf KK |
1184 | if (unlikely(rc)) |
1185 | return rc; | |
1186 | ||
1187 | request = PAD(sizeof(struct jffs2_raw_xattr) + strlen(xname) + 1 + size); | |
9fe4854c | 1188 | rc = jffs2_reserve_space(c, request, &length, |
aa98d7cf KK |
1189 | ALLOC_NORMAL, JFFS2_SUMMARY_XATTR_SIZE); |
1190 | if (rc) { | |
1191 | JFFS2_WARNING("jffs2_reserve_space()=%d, request=%u\n", rc, request); | |
1192 | return rc; | |
1193 | } | |
1194 | ||
1195 | /* Find existing xattr */ | |
1196 | down_write(&c->xattr_sem); | |
1197 | retry: | |
8f2b6f49 | 1198 | for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) { |
aa98d7cf KK |
1199 | xd = ref->xd; |
1200 | if (xd->xprefix != xprefix) | |
1201 | continue; | |
1202 | if (!xd->xname) { | |
1203 | rc = load_xattr_datum(c, xd); | |
1204 | if (unlikely(rc > 0)) { | |
8f2b6f49 | 1205 | *pref = ref->next; |
c9f700f8 | 1206 | delete_xattr_ref_delay(c, ref); |
aa98d7cf KK |
1207 | goto retry; |
1208 | } else if (unlikely(rc < 0)) | |
1209 | goto out; | |
1210 | } | |
1211 | if (!strcmp(xd->xname, xname)) { | |
1212 | if (flags & XATTR_CREATE) { | |
1213 | rc = -EEXIST; | |
1214 | goto out; | |
1215 | } | |
1216 | if (!buffer) { | |
8f2b6f49 | 1217 | *pref = ref->next; |
c9f700f8 | 1218 | rc = delete_xattr_ref(c, ref, 0); |
aa98d7cf KK |
1219 | goto out; |
1220 | } | |
1221 | goto found; | |
1222 | } | |
1223 | } | |
1224 | /* not found */ | |
aa98d7cf KK |
1225 | if (flags & XATTR_REPLACE) { |
1226 | rc = -ENODATA; | |
1227 | goto out; | |
1228 | } | |
1229 | if (!buffer) { | |
c9f700f8 | 1230 | rc = -ENODATA; |
aa98d7cf KK |
1231 | goto out; |
1232 | } | |
1233 | found: | |
9fe4854c | 1234 | xd = create_xattr_datum(c, xprefix, xname, buffer, size); |
aa98d7cf KK |
1235 | if (IS_ERR(xd)) { |
1236 | rc = PTR_ERR(xd); | |
1237 | goto out; | |
1238 | } | |
1239 | up_write(&c->xattr_sem); | |
1240 | jffs2_complete_reservation(c); | |
1241 | ||
1242 | /* create xattr_ref */ | |
1243 | request = PAD(sizeof(struct jffs2_raw_xref)); | |
9fe4854c | 1244 | rc = jffs2_reserve_space(c, request, &length, |
aa98d7cf | 1245 | ALLOC_NORMAL, JFFS2_SUMMARY_XREF_SIZE); |
c9f700f8 | 1246 | down_write(&c->xattr_sem); |
aa98d7cf KK |
1247 | if (rc) { |
1248 | JFFS2_WARNING("jffs2_reserve_space()=%d, request=%u\n", rc, request); | |
aa98d7cf KK |
1249 | xd->refcnt--; |
1250 | if (!xd->refcnt) | |
c9f700f8 | 1251 | delete_xattr_datum_delay(c, xd); |
aa98d7cf KK |
1252 | up_write(&c->xattr_sem); |
1253 | return rc; | |
1254 | } | |
8f2b6f49 KK |
1255 | if (ref) |
1256 | *pref = ref->next; | |
9fe4854c | 1257 | newref = create_xattr_ref(c, ic, xd); |
aa98d7cf | 1258 | if (IS_ERR(newref)) { |
8f2b6f49 KK |
1259 | if (ref) { |
1260 | ref->next = ic->xref; | |
1261 | ic->xref = ref; | |
1262 | } | |
aa98d7cf KK |
1263 | rc = PTR_ERR(newref); |
1264 | xd->refcnt--; | |
1265 | if (!xd->refcnt) | |
c9f700f8 | 1266 | delete_xattr_datum_delay(c, xd); |
aa98d7cf | 1267 | } else if (ref) { |
c9f700f8 KK |
1268 | up_write(&c->xattr_sem); |
1269 | jffs2_complete_reservation(c); | |
1270 | ||
1271 | rc = jffs2_reserve_space(c, request, &length, | |
1272 | ALLOC_DELETION, JFFS2_SUMMARY_XREF_SIZE); | |
1273 | down_write(&c->xattr_sem); | |
1274 | if (rc) { | |
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); | |
1278 | return 0; | |
1279 | } | |
1280 | delete_xattr_ref(c, ref, 1); | |
aa98d7cf KK |
1281 | } |
1282 | out: | |
1283 | up_write(&c->xattr_sem); | |
1284 | jffs2_complete_reservation(c); | |
1285 | return rc; | |
1286 | } | |
1287 | ||
1288 | /* -------- garbage collector functions ------------- | |
c9f700f8 | 1289 | * jffs2_garbage_collect_xattr_datum(c, xd, raw) |
aa98d7cf | 1290 | * is used to move xdatum into new node. |
c9f700f8 | 1291 | * jffs2_garbage_collect_xattr_ref(c, ref, raw) |
aa98d7cf | 1292 | * is used to move xref into new node. |
aa98d7cf KK |
1293 | * jffs2_verify_xattr(c) |
1294 | * is used to call do_verify_xattr_datum() before garbage collecting. | |
1295 | * -------------------------------------------------- */ | |
c9f700f8 KK |
1296 | int jffs2_garbage_collect_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd, |
1297 | struct jffs2_raw_node_ref *raw) | |
aa98d7cf | 1298 | { |
9fe4854c | 1299 | uint32_t totlen, length, old_ofs; |
c9f700f8 | 1300 | int rc = 0; |
aa98d7cf | 1301 | |
084702e0 | 1302 | down_write(&c->xattr_sem); |
c9f700f8 KK |
1303 | if (xd->node != raw) |
1304 | goto out; | |
1305 | if (is_xattr_datum_dead(xd) && (raw->next_in_ino == (void *)xd)) | |
1306 | goto out; | |
aa98d7cf KK |
1307 | |
1308 | old_ofs = ref_offset(xd->node); | |
1309 | totlen = ref_totlen(c, c->gcblock, xd->node); | |
aa98d7cf | 1310 | |
c9f700f8 | 1311 | if (!is_xattr_datum_dead(xd)) { |
aa98d7cf | 1312 | rc = load_xattr_datum(c, xd); |
c9f700f8 | 1313 | if (unlikely(rc < 0)) |
084702e0 | 1314 | goto out; |
aa98d7cf | 1315 | } |
c9f700f8 | 1316 | |
9fe4854c | 1317 | rc = jffs2_reserve_space_gc(c, totlen, &length, JFFS2_SUMMARY_XATTR_SIZE); |
c9f700f8 KK |
1318 | if (rc) { |
1319 | JFFS2_WARNING("jffs2_reserve_space_gc()=%d, request=%u\n", rc, totlen); | |
084702e0 KK |
1320 | rc = rc ? rc : -EBADFD; |
1321 | goto out; | |
aa98d7cf | 1322 | } |
9fe4854c | 1323 | rc = save_xattr_datum(c, xd); |
aa98d7cf KK |
1324 | if (!rc) |
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)); | |
084702e0 | 1327 | out: |
c9f700f8 KK |
1328 | if (!rc) |
1329 | jffs2_mark_node_obsolete(c, raw); | |
084702e0 | 1330 | up_write(&c->xattr_sem); |
aa98d7cf KK |
1331 | return rc; |
1332 | } | |
1333 | ||
1334 | ||
c9f700f8 KK |
1335 | int jffs2_garbage_collect_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref, |
1336 | struct jffs2_raw_node_ref *raw) | |
aa98d7cf | 1337 | { |
9fe4854c | 1338 | uint32_t totlen, length, old_ofs; |
c9f700f8 | 1339 | int rc = 0; |
aa98d7cf | 1340 | |
084702e0 | 1341 | down_write(&c->xattr_sem); |
aa98d7cf KK |
1342 | BUG_ON(!ref->node); |
1343 | ||
c9f700f8 KK |
1344 | if (ref->node != raw) |
1345 | goto out; | |
1346 | if (is_xattr_ref_dead(ref) && (raw->next_in_ino == (void *)ref)) | |
1347 | goto out; | |
1348 | ||
aa98d7cf KK |
1349 | old_ofs = ref_offset(ref->node); |
1350 | totlen = ref_totlen(c, c->gcblock, ref->node); | |
084702e0 | 1351 | |
9fe4854c | 1352 | rc = jffs2_reserve_space_gc(c, totlen, &length, JFFS2_SUMMARY_XREF_SIZE); |
c9f700f8 KK |
1353 | if (rc) { |
1354 | JFFS2_WARNING("%s: jffs2_reserve_space_gc() = %d, request = %u\n", | |
aa98d7cf | 1355 | __FUNCTION__, rc, totlen); |
084702e0 KK |
1356 | rc = rc ? rc : -EBADFD; |
1357 | goto out; | |
aa98d7cf | 1358 | } |
9fe4854c | 1359 | rc = save_xattr_ref(c, ref); |
aa98d7cf KK |
1360 | if (!rc) |
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)); | |
084702e0 | 1363 | out: |
c9f700f8 KK |
1364 | if (!rc) |
1365 | jffs2_mark_node_obsolete(c, raw); | |
084702e0 | 1366 | up_write(&c->xattr_sem); |
aa98d7cf KK |
1367 | return rc; |
1368 | } | |
1369 | ||
aa98d7cf KK |
1370 | int jffs2_verify_xattr(struct jffs2_sb_info *c) |
1371 | { | |
1372 | struct jffs2_xattr_datum *xd, *_xd; | |
c9f700f8 KK |
1373 | struct jffs2_eraseblock *jeb; |
1374 | struct jffs2_raw_node_ref *raw; | |
1375 | uint32_t totlen; | |
aa98d7cf KK |
1376 | int rc; |
1377 | ||
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); | |
c9f700f8 KK |
1381 | if (rc < 0) |
1382 | continue; | |
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) | |
1387 | continue; | |
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); | |
aa98d7cf | 1394 | } |
c9f700f8 KK |
1395 | if (is_xattr_datum_dead(xd)) |
1396 | list_add(&xd->xindex, &c->xattr_dead_list); | |
1397 | spin_unlock(&c->erase_completion_lock); | |
aa98d7cf KK |
1398 | } |
1399 | up_write(&c->xattr_sem); | |
aa98d7cf KK |
1400 | return list_empty(&c->xattr_unchecked) ? 1 : 0; |
1401 | } | |
c9f700f8 KK |
1402 | |
1403 | void jffs2_release_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd) | |
1404 | { | |
1405 | /* must be called under spin_lock(&c->erase_completion_lock) */ | |
1406 | if (xd->node != (void *)xd) | |
1407 | return; | |
1408 | ||
1409 | list_del(&xd->xindex); | |
1410 | jffs2_free_xattr_datum(xd); | |
1411 | } | |
1412 | ||
1413 | void jffs2_release_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref) | |
1414 | { | |
1415 | /* must be called under spin_lock(&c->erase_completion_lock) */ | |
1416 | struct jffs2_xattr_ref *tmp, **ptmp; | |
1417 | ||
1418 | if (ref->node != (void *)ref) | |
1419 | return; | |
1420 | ||
1421 | for (tmp=c->xref_dead_list, ptmp=&c->xref_dead_list; tmp; ptmp=&tmp->next, tmp=tmp->next) { | |
1422 | if (ref == tmp) { | |
1423 | *ptmp = tmp->next; | |
1424 | jffs2_free_xattr_ref(ref); | |
1425 | break; | |
1426 | } | |
1427 | } | |
1428 | } |