blob: 7622f79cb5b418e96eeb51bf15246888f1b5d98f [file] [log] [blame]
KaiGai Kohei652ecc22006-05-13 15:18:27 +09001/*
2 * JFFS2 -- Journalling Flash File System, Version 2.
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09003 *
KaiGai Kohei652ecc22006-05-13 15:18:27 +09004 * Copyright (C) 2006 NEC Corporation
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09005 *
KaiGai Kohei652ecc22006-05-13 15:18:27 +09006 * Created by KaiGai Kohei <kaigai@ak.jp.nec.com>
7 *
8 * For licensing information, see the file 'LICENCE' in this directory.
9 *
10 */
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +090011#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).
KaiGai Koheic9f700f2006-06-11 10:35:15 +090026 * 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.
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +090029 * 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.
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +090035 * 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().
David Woodhouse9fe48542006-05-23 00:38:06 +010049 * save_xattr_datum(c, xd)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +090050 * is used to write xdatum to medium. xd->version will be incremented.
David Woodhouse9fe48542006-05-23 00:38:06 +010051 * create_xattr_datum(c, xprefix, xname, xvalue, xsize)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +090052 * is used to create new xdatum and write to medium.
KaiGai Koheic9f700f2006-06-11 10:35:15 +090053 * delete_xattr_datum(c, xd)
KaiGai Kohei8a136952006-06-24 09:14:13 +090054 * is used to delete a xdatum. It marks xd JFFS2_XFLAGS_DEAD, and allows
55 * GC to reclaim those physical nodes.
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +090056 * -------------------------------------------------- */
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +090057static uint32_t xattr_datum_hashkey(int xprefix, const char *xname, const char *xvalue, int xsize)
58{
59 int name_len = strlen(xname);
60
61 return crc32(xprefix, xname, name_len) ^ crc32(xprefix, xvalue, xsize);
62}
63
KaiGai Koheic9f700f2006-06-11 10:35:15 +090064static int is_xattr_datum_unchecked(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
65{
66 struct jffs2_raw_node_ref *raw;
67 int rc = 0;
68
69 spin_lock(&c->erase_completion_lock);
70 for (raw=xd->node; raw != (void *)xd; raw=raw->next_in_ino) {
71 if (ref_flags(raw) == REF_UNCHECKED) {
72 rc = 1;
73 break;
74 }
75 }
76 spin_unlock(&c->erase_completion_lock);
77 return rc;
78}
79
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +090080static void unload_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
81{
82 /* must be called under down_write(xattr_sem) */
83 D1(dbg_xattr("%s: xid=%u, version=%u\n", __FUNCTION__, xd->xid, xd->version));
84 if (xd->xname) {
85 c->xdatum_mem_usage -= (xd->name_len + 1 + xd->value_len);
86 kfree(xd->xname);
87 }
88
89 list_del_init(&xd->xindex);
90 xd->hashkey = 0;
91 xd->xname = NULL;
92 xd->xvalue = NULL;
93}
94
95static void reclaim_xattr_datum(struct jffs2_sb_info *c)
96{
97 /* must be called under down_write(xattr_sem) */
98 struct jffs2_xattr_datum *xd, *_xd;
99 uint32_t target, before;
100 static int index = 0;
101 int count;
102
103 if (c->xdatum_mem_threshold > c->xdatum_mem_usage)
104 return;
105
106 before = c->xdatum_mem_usage;
107 target = c->xdatum_mem_usage * 4 / 5; /* 20% reduction */
108 for (count = 0; count < XATTRINDEX_HASHSIZE; count++) {
109 list_for_each_entry_safe(xd, _xd, &c->xattrindex[index], xindex) {
110 if (xd->flags & JFFS2_XFLAGS_HOT) {
111 xd->flags &= ~JFFS2_XFLAGS_HOT;
112 } else if (!(xd->flags & JFFS2_XFLAGS_BIND)) {
113 unload_xattr_datum(c, xd);
114 }
115 if (c->xdatum_mem_usage <= target)
116 goto out;
117 }
118 index = (index+1) % XATTRINDEX_HASHSIZE;
119 }
120 out:
121 JFFS2_NOTICE("xdatum_mem_usage from %u byte to %u byte (%u byte reclaimed)\n",
122 before, c->xdatum_mem_usage, before - c->xdatum_mem_usage);
123}
124
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900125static int do_verify_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
126{
127 /* must be called under down_write(xattr_sem) */
128 struct jffs2_eraseblock *jeb;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900129 struct jffs2_raw_node_ref *raw;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900130 struct jffs2_raw_xattr rx;
131 size_t readlen;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900132 uint32_t crc, offset, totlen;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900133 int rc;
134
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900135 spin_lock(&c->erase_completion_lock);
136 offset = ref_offset(xd->node);
137 if (ref_flags(xd->node) == REF_PRISTINE)
138 goto complete;
139 spin_unlock(&c->erase_completion_lock);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900140
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900141 rc = jffs2_flash_read(c, offset, sizeof(rx), &readlen, (char *)&rx);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900142 if (rc || readlen != sizeof(rx)) {
David Woodhouse89291a92006-05-25 13:30:24 +0100143 JFFS2_WARNING("jffs2_flash_read()=%d, req=%zu, read=%zu at %#08x\n",
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900144 rc, sizeof(rx), readlen, offset);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900145 return rc ? rc : -EIO;
146 }
147 crc = crc32(0, &rx, sizeof(rx) - 4);
148 if (crc != je32_to_cpu(rx.node_crc)) {
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900149 JFFS2_ERROR("node CRC failed at %#08x, read=%#08x, calc=%#08x\n",
150 offset, je32_to_cpu(rx.hdr_crc), crc);
151 xd->flags |= JFFS2_XFLAGS_INVALID;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900152 return EIO;
153 }
KaiGai Kohei8a136952006-06-24 09:14:13 +0900154 totlen = PAD(sizeof(rx) + rx.name_len + 1 + je16_to_cpu(rx.value_len));
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900155 if (je16_to_cpu(rx.magic) != JFFS2_MAGIC_BITMASK
156 || je16_to_cpu(rx.nodetype) != JFFS2_NODETYPE_XATTR
157 || je32_to_cpu(rx.totlen) != totlen
158 || je32_to_cpu(rx.xid) != xd->xid
159 || je32_to_cpu(rx.version) != xd->version) {
160 JFFS2_ERROR("inconsistent xdatum at %#08x, magic=%#04x/%#04x, "
161 "nodetype=%#04x/%#04x, totlen=%u/%u, xid=%u/%u, version=%u/%u\n",
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900162 offset, je16_to_cpu(rx.magic), JFFS2_MAGIC_BITMASK,
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900163 je16_to_cpu(rx.nodetype), JFFS2_NODETYPE_XATTR,
164 je32_to_cpu(rx.totlen), totlen,
165 je32_to_cpu(rx.xid), xd->xid,
166 je32_to_cpu(rx.version), xd->version);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900167 xd->flags |= JFFS2_XFLAGS_INVALID;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900168 return EIO;
169 }
170 xd->xprefix = rx.xprefix;
171 xd->name_len = rx.name_len;
172 xd->value_len = je16_to_cpu(rx.value_len);
173 xd->data_crc = je32_to_cpu(rx.data_crc);
174
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900175 spin_lock(&c->erase_completion_lock);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900176 complete:
177 for (raw=xd->node; raw != (void *)xd; raw=raw->next_in_ino) {
178 jeb = &c->blocks[ref_offset(raw) / c->sector_size];
179 totlen = PAD(ref_totlen(c, jeb, raw));
180 if (ref_flags(raw) == REF_UNCHECKED) {
181 c->unchecked_size -= totlen; c->used_size += totlen;
182 jeb->unchecked_size -= totlen; jeb->used_size += totlen;
183 }
184 raw->flash_offset = ref_offset(raw) | ((xd->node==raw) ? REF_PRISTINE : REF_NORMAL);
185 }
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900186 spin_unlock(&c->erase_completion_lock);
187
188 /* unchecked xdatum is chained with c->xattr_unchecked */
189 list_del_init(&xd->xindex);
190
191 dbg_xattr("success on verfying xdatum (xid=%u, version=%u)\n",
192 xd->xid, xd->version);
193
194 return 0;
195}
196
197static int do_load_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
198{
199 /* must be called under down_write(xattr_sem) */
200 char *data;
201 size_t readlen;
202 uint32_t crc, length;
203 int i, ret, retry = 0;
204
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900205 BUG_ON(ref_flags(xd->node) != REF_PRISTINE);
206 BUG_ON(!list_empty(&xd->xindex));
207 retry:
208 length = xd->name_len + 1 + xd->value_len;
209 data = kmalloc(length, GFP_KERNEL);
210 if (!data)
211 return -ENOMEM;
212
213 ret = jffs2_flash_read(c, ref_offset(xd->node)+sizeof(struct jffs2_raw_xattr),
214 length, &readlen, data);
215
216 if (ret || length!=readlen) {
David Woodhouse89291a92006-05-25 13:30:24 +0100217 JFFS2_WARNING("jffs2_flash_read() returned %d, request=%d, readlen=%zu, at %#08x\n",
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900218 ret, length, readlen, ref_offset(xd->node));
219 kfree(data);
220 return ret ? ret : -EIO;
221 }
222
223 data[xd->name_len] = '\0';
224 crc = crc32(0, data, length);
225 if (crc != xd->data_crc) {
226 JFFS2_WARNING("node CRC failed (JFFS2_NODETYPE_XREF)"
227 " at %#08x, read: 0x%08x calculated: 0x%08x\n",
228 ref_offset(xd->node), xd->data_crc, crc);
229 kfree(data);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900230 xd->flags |= JFFS2_XFLAGS_INVALID;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900231 return EIO;
232 }
233
234 xd->flags |= JFFS2_XFLAGS_HOT;
235 xd->xname = data;
236 xd->xvalue = data + xd->name_len+1;
237
238 c->xdatum_mem_usage += length;
239
240 xd->hashkey = xattr_datum_hashkey(xd->xprefix, xd->xname, xd->xvalue, xd->value_len);
241 i = xd->hashkey % XATTRINDEX_HASHSIZE;
242 list_add(&xd->xindex, &c->xattrindex[i]);
243 if (!retry) {
244 retry = 1;
245 reclaim_xattr_datum(c);
246 if (!xd->xname)
247 goto retry;
248 }
249
250 dbg_xattr("success on loading xdatum (xid=%u, xprefix=%u, xname='%s')\n",
251 xd->xid, xd->xprefix, xd->xname);
252
253 return 0;
254}
255
256static int load_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
257{
258 /* must be called under down_write(xattr_sem);
259 * rc < 0 : recoverable error, try again
260 * rc = 0 : success
261 * rc > 0 : Unrecoverable error, this node should be deleted.
262 */
263 int rc = 0;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900264
KaiGai Kohei8a136952006-06-24 09:14:13 +0900265 BUG_ON(xd->flags & JFFS2_XFLAGS_DEAD);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900266 if (xd->xname)
267 return 0;
268 if (xd->flags & JFFS2_XFLAGS_INVALID)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900269 return EIO;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900270 if (unlikely(is_xattr_datum_unchecked(c, xd)))
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900271 rc = do_verify_xattr_datum(c, xd);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900272 if (!rc)
273 rc = do_load_xattr_datum(c, xd);
274 return rc;
275}
276
David Woodhouse9fe48542006-05-23 00:38:06 +0100277static int save_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900278{
279 /* must be called under down_write(xattr_sem) */
David Woodhouse2f785402006-05-24 02:04:45 +0100280 struct jffs2_raw_xattr rx;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900281 struct kvec vecs[2];
David Woodhouse89291a92006-05-25 13:30:24 +0100282 size_t length;
KaiGai Kohei8a136952006-06-24 09:14:13 +0900283 int rc, totlen;
David Woodhouse9fe48542006-05-23 00:38:06 +0100284 uint32_t phys_ofs = write_ofs(c);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900285
KaiGai Kohei8a136952006-06-24 09:14:13 +0900286 BUG_ON(!xd->xname);
287 BUG_ON(xd->flags & (JFFS2_XFLAGS_DEAD|JFFS2_XFLAGS_INVALID));
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900288
289 vecs[0].iov_base = &rx;
KaiGai Kohei8a136952006-06-24 09:14:13 +0900290 vecs[0].iov_len = sizeof(rx);
291 vecs[1].iov_base = xd->xname;
292 vecs[1].iov_len = xd->name_len + 1 + xd->value_len;
293 totlen = vecs[0].iov_len + vecs[1].iov_len;
294
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900295 /* Setup raw-xattr */
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900296 memset(&rx, 0, sizeof(rx));
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900297 rx.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
298 rx.nodetype = cpu_to_je16(JFFS2_NODETYPE_XATTR);
299 rx.totlen = cpu_to_je32(PAD(totlen));
300 rx.hdr_crc = cpu_to_je32(crc32(0, &rx, sizeof(struct jffs2_unknown_node) - 4));
301
302 rx.xid = cpu_to_je32(xd->xid);
KaiGai Kohei8a136952006-06-24 09:14:13 +0900303 rx.version = cpu_to_je32(++xd->version);
304 rx.xprefix = xd->xprefix;
305 rx.name_len = xd->name_len;
306 rx.value_len = cpu_to_je16(xd->value_len);
307 rx.data_crc = cpu_to_je32(crc32(0, vecs[1].iov_base, vecs[1].iov_len));
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900308 rx.node_crc = cpu_to_je32(crc32(0, &rx, sizeof(struct jffs2_raw_xattr) - 4));
309
KaiGai Kohei8a136952006-06-24 09:14:13 +0900310 rc = jffs2_flash_writev(c, vecs, 2, phys_ofs, &length, 0);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900311 if (rc || totlen != length) {
David Woodhouse89291a92006-05-25 13:30:24 +0100312 JFFS2_WARNING("jffs2_flash_writev()=%d, req=%u, wrote=%zu, at %#08x\n",
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900313 rc, totlen, length, phys_ofs);
314 rc = rc ? rc : -EIO;
David Woodhouse2f785402006-05-24 02:04:45 +0100315 if (length)
316 jffs2_add_physical_node_ref(c, phys_ofs | REF_OBSOLETE, PAD(totlen), NULL);
317
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900318 return rc;
319 }
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900320 /* success */
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900321 jffs2_add_physical_node_ref(c, phys_ofs | REF_PRISTINE, PAD(totlen), (void *)xd);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900322
323 dbg_xattr("success on saving xdatum (xid=%u, version=%u, xprefix=%u, xname='%s')\n",
324 xd->xid, xd->version, xd->xprefix, xd->xname);
325
326 return 0;
327}
328
329static struct jffs2_xattr_datum *create_xattr_datum(struct jffs2_sb_info *c,
330 int xprefix, const char *xname,
David Woodhouse9fe48542006-05-23 00:38:06 +0100331 const char *xvalue, int xsize)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900332{
333 /* must be called under down_write(xattr_sem) */
334 struct jffs2_xattr_datum *xd;
335 uint32_t hashkey, name_len;
336 char *data;
337 int i, rc;
338
339 /* Search xattr_datum has same xname/xvalue by index */
340 hashkey = xattr_datum_hashkey(xprefix, xname, xvalue, xsize);
341 i = hashkey % XATTRINDEX_HASHSIZE;
342 list_for_each_entry(xd, &c->xattrindex[i], xindex) {
343 if (xd->hashkey==hashkey
344 && xd->xprefix==xprefix
345 && xd->value_len==xsize
346 && !strcmp(xd->xname, xname)
347 && !memcmp(xd->xvalue, xvalue, xsize)) {
348 xd->refcnt++;
349 return xd;
350 }
351 }
352
353 /* Not found, Create NEW XATTR-Cache */
354 name_len = strlen(xname);
355
356 xd = jffs2_alloc_xattr_datum();
357 if (!xd)
358 return ERR_PTR(-ENOMEM);
359
360 data = kmalloc(name_len + 1 + xsize, GFP_KERNEL);
361 if (!data) {
362 jffs2_free_xattr_datum(xd);
363 return ERR_PTR(-ENOMEM);
364 }
365 strcpy(data, xname);
366 memcpy(data + name_len + 1, xvalue, xsize);
367
368 xd->refcnt = 1;
369 xd->xid = ++c->highest_xid;
370 xd->flags |= JFFS2_XFLAGS_HOT;
371 xd->xprefix = xprefix;
372
373 xd->hashkey = hashkey;
374 xd->xname = data;
375 xd->xvalue = data + name_len + 1;
376 xd->name_len = name_len;
377 xd->value_len = xsize;
378 xd->data_crc = crc32(0, data, xd->name_len + 1 + xd->value_len);
379
David Woodhouse9fe48542006-05-23 00:38:06 +0100380 rc = save_xattr_datum(c, xd);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900381 if (rc) {
382 kfree(xd->xname);
383 jffs2_free_xattr_datum(xd);
384 return ERR_PTR(rc);
385 }
386
387 /* Insert Hash Index */
388 i = hashkey % XATTRINDEX_HASHSIZE;
389 list_add(&xd->xindex, &c->xattrindex[i]);
390
391 c->xdatum_mem_usage += (xd->name_len + 1 + xd->value_len);
392 reclaim_xattr_datum(c);
393
394 return xd;
395}
396
KaiGai Kohei8a136952006-06-24 09:14:13 +0900397static void delete_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900398{
399 /* must be called under down_write(xattr_sem) */
400 BUG_ON(xd->refcnt);
401
402 unload_xattr_datum(c, xd);
KaiGai Kohei8a136952006-06-24 09:14:13 +0900403 xd->flags |= JFFS2_XFLAGS_DEAD;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900404 spin_lock(&c->erase_completion_lock);
KaiGai Kohei8a136952006-06-24 09:14:13 +0900405 if (xd->node == (void *)xd) {
406 BUG_ON(!(xd->flags & JFFS2_XFLAGS_INVALID));
407 jffs2_free_xattr_datum(xd);
408 } else {
409 list_add(&xd->xindex, &c->xattr_dead_list);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900410 }
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900411 spin_unlock(&c->erase_completion_lock);
KaiGai Kohei8a136952006-06-24 09:14:13 +0900412 dbg_xattr("xdatum(xid=%u, version=%u) was removed.\n", xd->xid, xd->version);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900413}
414
KaiGai Kohei21b98792006-05-13 15:22:29 +0900415/* -------- xref related functions ------------------
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900416 * verify_xattr_ref(c, ref)
417 * is used to load xref information from medium. Because summary data does not
418 * contain xid/ino, it's necessary to verify once while mounting process.
David Woodhouse9fe48542006-05-23 00:38:06 +0100419 * save_xattr_ref(c, ref)
KaiGai Kohei8a136952006-06-24 09:14:13 +0900420 * is used to write xref to medium. If delete marker is marked, it write
421 * a delete marker of xref into medium.
David Woodhouse9fe48542006-05-23 00:38:06 +0100422 * create_xattr_ref(c, ic, xd)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900423 * is used to create a new xref and write to medium.
KaiGai Kohei8a136952006-06-24 09:14:13 +0900424 * delete_xattr_ref(c, ref)
425 * is used to delete jffs2_xattr_ref. It marks xref XREF_DELETE_MARKER,
426 * and allows GC to reclaim those physical nodes.
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900427 * jffs2_xattr_delete_inode(c, ic)
428 * is called to remove xrefs related to obsolete inode when inode is unlinked.
429 * jffs2_xattr_free_inode(c, ic)
430 * is called to release xattr related objects when unmounting.
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900431 * check_xattr_ref_inode(c, ic)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900432 * is used to confirm inode does not have duplicate xattr name/value pair.
433 * -------------------------------------------------- */
434static int verify_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
435{
436 struct jffs2_eraseblock *jeb;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900437 struct jffs2_raw_node_ref *raw;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900438 struct jffs2_raw_xref rr;
439 size_t readlen;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900440 uint32_t crc, offset, totlen;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900441 int rc;
442
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900443 spin_lock(&c->erase_completion_lock);
444 if (ref_flags(ref->node) != REF_UNCHECKED)
445 goto complete;
446 offset = ref_offset(ref->node);
447 spin_unlock(&c->erase_completion_lock);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900448
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900449 rc = jffs2_flash_read(c, offset, sizeof(rr), &readlen, (char *)&rr);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900450 if (rc || sizeof(rr) != readlen) {
David Woodhouse89291a92006-05-25 13:30:24 +0100451 JFFS2_WARNING("jffs2_flash_read()=%d, req=%zu, read=%zu, at %#08x\n",
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900452 rc, sizeof(rr), readlen, offset);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900453 return rc ? rc : -EIO;
454 }
455 /* obsolete node */
456 crc = crc32(0, &rr, sizeof(rr) - 4);
457 if (crc != je32_to_cpu(rr.node_crc)) {
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900458 JFFS2_ERROR("node CRC failed at %#08x, read=%#08x, calc=%#08x\n",
459 offset, je32_to_cpu(rr.node_crc), crc);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900460 return EIO;
461 }
462 if (je16_to_cpu(rr.magic) != JFFS2_MAGIC_BITMASK
463 || je16_to_cpu(rr.nodetype) != JFFS2_NODETYPE_XREF
464 || je32_to_cpu(rr.totlen) != PAD(sizeof(rr))) {
465 JFFS2_ERROR("inconsistent xref at %#08x, magic=%#04x/%#04x, "
David Woodhouse89291a92006-05-25 13:30:24 +0100466 "nodetype=%#04x/%#04x, totlen=%u/%zu\n",
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900467 offset, je16_to_cpu(rr.magic), JFFS2_MAGIC_BITMASK,
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900468 je16_to_cpu(rr.nodetype), JFFS2_NODETYPE_XREF,
469 je32_to_cpu(rr.totlen), PAD(sizeof(rr)));
470 return EIO;
471 }
472 ref->ino = je32_to_cpu(rr.ino);
473 ref->xid = je32_to_cpu(rr.xid);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900474 ref->xseqno = je32_to_cpu(rr.xseqno);
475 if (ref->xseqno > c->highest_xseqno)
476 c->highest_xseqno = (ref->xseqno & ~XREF_DELETE_MARKER);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900477
478 spin_lock(&c->erase_completion_lock);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900479 complete:
480 for (raw=ref->node; raw != (void *)ref; raw=raw->next_in_ino) {
481 jeb = &c->blocks[ref_offset(raw) / c->sector_size];
482 totlen = PAD(ref_totlen(c, jeb, raw));
483 if (ref_flags(raw) == REF_UNCHECKED) {
484 c->unchecked_size -= totlen; c->used_size += totlen;
485 jeb->unchecked_size -= totlen; jeb->used_size += totlen;
486 }
487 raw->flash_offset = ref_offset(raw) | ((ref->node==raw) ? REF_PRISTINE : REF_NORMAL);
488 }
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900489 spin_unlock(&c->erase_completion_lock);
490
491 dbg_xattr("success on verifying xref (ino=%u, xid=%u) at %#08x\n",
492 ref->ino, ref->xid, ref_offset(ref->node));
493 return 0;
494}
495
David Woodhouse9fe48542006-05-23 00:38:06 +0100496static int save_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900497{
498 /* must be called under down_write(xattr_sem) */
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900499 struct jffs2_raw_xref rr;
David Woodhouse89291a92006-05-25 13:30:24 +0100500 size_t length;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900501 uint32_t xseqno, phys_ofs = write_ofs(c);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900502 int ret;
503
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900504 rr.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
505 rr.nodetype = cpu_to_je16(JFFS2_NODETYPE_XREF);
506 rr.totlen = cpu_to_je32(PAD(sizeof(rr)));
507 rr.hdr_crc = cpu_to_je32(crc32(0, &rr, sizeof(struct jffs2_unknown_node) - 4));
508
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900509 xseqno = (c->highest_xseqno += 2);
510 if (is_xattr_ref_dead(ref)) {
511 xseqno |= XREF_DELETE_MARKER;
512 rr.ino = cpu_to_je32(ref->ino);
513 rr.xid = cpu_to_je32(ref->xid);
514 } else {
515 rr.ino = cpu_to_je32(ref->ic->ino);
516 rr.xid = cpu_to_je32(ref->xd->xid);
517 }
518 rr.xseqno = cpu_to_je32(xseqno);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900519 rr.node_crc = cpu_to_je32(crc32(0, &rr, sizeof(rr) - 4));
520
521 ret = jffs2_flash_write(c, phys_ofs, sizeof(rr), &length, (char *)&rr);
522 if (ret || sizeof(rr) != length) {
David Woodhouse89291a92006-05-25 13:30:24 +0100523 JFFS2_WARNING("jffs2_flash_write() returned %d, request=%zu, retlen=%zu, at %#08x\n",
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900524 ret, sizeof(rr), length, phys_ofs);
525 ret = ret ? ret : -EIO;
David Woodhouse2f785402006-05-24 02:04:45 +0100526 if (length)
527 jffs2_add_physical_node_ref(c, phys_ofs | REF_OBSOLETE, PAD(sizeof(rr)), NULL);
528
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900529 return ret;
530 }
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900531 /* success */
532 ref->xseqno = xseqno;
533 jffs2_add_physical_node_ref(c, phys_ofs | REF_PRISTINE, PAD(sizeof(rr)), (void *)ref);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900534
535 dbg_xattr("success on saving xref (ino=%u, xid=%u)\n", ref->ic->ino, ref->xd->xid);
536
537 return 0;
538}
539
540static struct jffs2_xattr_ref *create_xattr_ref(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic,
David Woodhouse9fe48542006-05-23 00:38:06 +0100541 struct jffs2_xattr_datum *xd)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900542{
543 /* must be called under down_write(xattr_sem) */
544 struct jffs2_xattr_ref *ref;
545 int ret;
546
547 ref = jffs2_alloc_xattr_ref();
548 if (!ref)
549 return ERR_PTR(-ENOMEM);
550 ref->ic = ic;
551 ref->xd = xd;
552
David Woodhouse9fe48542006-05-23 00:38:06 +0100553 ret = save_xattr_ref(c, ref);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900554 if (ret) {
555 jffs2_free_xattr_ref(ref);
556 return ERR_PTR(ret);
557 }
558
559 /* Chain to inode */
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900560 ref->next = ic->xref;
561 ic->xref = ref;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900562
563 return ref; /* success */
564}
565
KaiGai Kohei8a136952006-06-24 09:14:13 +0900566static void delete_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900567{
568 /* must be called under down_write(xattr_sem) */
569 struct jffs2_xattr_datum *xd;
570
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900571 xd = ref->xd;
KaiGai Kohei8a136952006-06-24 09:14:13 +0900572 ref->xseqno |= XREF_DELETE_MARKER;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900573 ref->ino = ref->ic->ino;
574 ref->xid = ref->xd->xid;
575 spin_lock(&c->erase_completion_lock);
576 ref->next = c->xref_dead_list;
577 c->xref_dead_list = ref;
578 spin_unlock(&c->erase_completion_lock);
579
KaiGai Kohei8a136952006-06-24 09:14:13 +0900580 dbg_xattr("xref(ino=%u, xid=%u, xseqno=%u) was removed.\n",
581 ref->ino, ref->xid, ref->xseqno);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900582
583 if (!--xd->refcnt)
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900584 delete_xattr_datum(c, xd);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900585}
586
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900587void jffs2_xattr_delete_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
588{
589 /* It's called from jffs2_clear_inode() on inode removing.
590 When an inode with XATTR is removed, those XATTRs must be removed. */
KaiGai Kohei8a136952006-06-24 09:14:13 +0900591 struct jffs2_xattr_ref *ref, *_ref;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900592
593 if (!ic || ic->nlink > 0)
594 return;
595
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900596 down_write(&c->xattr_sem);
KaiGai Kohei8a136952006-06-24 09:14:13 +0900597 for (ref = ic->xref; ref; ref = _ref) {
598 _ref = ref->next;
599 delete_xattr_ref(c, ref);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900600 }
KaiGai Kohei8a136952006-06-24 09:14:13 +0900601 ic->xref = NULL;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900602 up_write(&c->xattr_sem);
603}
604
605void jffs2_xattr_free_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
606{
607 /* It's called from jffs2_free_ino_caches() until unmounting FS. */
608 struct jffs2_xattr_datum *xd;
609 struct jffs2_xattr_ref *ref, *_ref;
610
611 down_write(&c->xattr_sem);
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900612 for (ref = ic->xref; ref; ref = _ref) {
613 _ref = ref->next;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900614 xd = ref->xd;
615 xd->refcnt--;
616 if (!xd->refcnt) {
617 unload_xattr_datum(c, xd);
618 jffs2_free_xattr_datum(xd);
619 }
620 jffs2_free_xattr_ref(ref);
621 }
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900622 ic->xref = NULL;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900623 up_write(&c->xattr_sem);
624}
625
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900626static int check_xattr_ref_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900627{
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900628 /* success of check_xattr_ref_inode() means taht inode (ic) dose not have
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900629 * duplicate name/value pairs. If duplicate name/value pair would be found,
630 * one will be removed.
631 */
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900632 struct jffs2_xattr_ref *ref, *cmp, **pref, **pcmp;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900633 int rc = 0;
634
635 if (likely(ic->flags & INO_FLAGS_XATTR_CHECKED))
636 return 0;
637 down_write(&c->xattr_sem);
638 retry:
639 rc = 0;
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900640 for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900641 if (!ref->xd->xname) {
642 rc = load_xattr_datum(c, ref->xd);
643 if (unlikely(rc > 0)) {
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900644 *pref = ref->next;
KaiGai Kohei8a136952006-06-24 09:14:13 +0900645 delete_xattr_ref(c, ref);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900646 goto retry;
647 } else if (unlikely(rc < 0))
648 goto out;
649 }
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900650 for (cmp=ref->next, pcmp=&ref->next; cmp; pcmp=&cmp->next, cmp=cmp->next) {
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900651 if (!cmp->xd->xname) {
652 ref->xd->flags |= JFFS2_XFLAGS_BIND;
653 rc = load_xattr_datum(c, cmp->xd);
654 ref->xd->flags &= ~JFFS2_XFLAGS_BIND;
655 if (unlikely(rc > 0)) {
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900656 *pcmp = cmp->next;
KaiGai Kohei8a136952006-06-24 09:14:13 +0900657 delete_xattr_ref(c, cmp);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900658 goto retry;
659 } else if (unlikely(rc < 0))
660 goto out;
661 }
662 if (ref->xd->xprefix == cmp->xd->xprefix
663 && !strcmp(ref->xd->xname, cmp->xd->xname)) {
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900664 if (ref->xseqno > cmp->xseqno) {
665 *pcmp = cmp->next;
KaiGai Kohei8a136952006-06-24 09:14:13 +0900666 delete_xattr_ref(c, cmp);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900667 } else {
668 *pref = ref->next;
KaiGai Kohei8a136952006-06-24 09:14:13 +0900669 delete_xattr_ref(c, ref);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900670 }
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900671 goto retry;
672 }
673 }
674 }
675 ic->flags |= INO_FLAGS_XATTR_CHECKED;
676 out:
677 up_write(&c->xattr_sem);
678
679 return rc;
680}
681
682/* -------- xattr subsystem functions ---------------
683 * jffs2_init_xattr_subsystem(c)
684 * is used to initialize semaphore and list_head, and some variables.
685 * jffs2_find_xattr_datum(c, xid)
686 * is used to lookup xdatum while scanning process.
687 * jffs2_clear_xattr_subsystem(c)
688 * is used to release any xattr related objects.
689 * jffs2_build_xattr_subsystem(c)
690 * is used to associate xdatum and xref while super block building process.
691 * jffs2_setup_xattr_datum(c, xid, version)
692 * is used to insert xdatum while scanning process.
693 * -------------------------------------------------- */
694void jffs2_init_xattr_subsystem(struct jffs2_sb_info *c)
695{
696 int i;
697
698 for (i=0; i < XATTRINDEX_HASHSIZE; i++)
699 INIT_LIST_HEAD(&c->xattrindex[i]);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900700 INIT_LIST_HEAD(&c->xattr_unchecked);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900701 INIT_LIST_HEAD(&c->xattr_dead_list);
702 c->xref_dead_list = NULL;
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900703 c->xref_temp = NULL;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900704
705 init_rwsem(&c->xattr_sem);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900706 c->highest_xid = 0;
707 c->highest_xseqno = 0;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900708 c->xdatum_mem_usage = 0;
709 c->xdatum_mem_threshold = 32 * 1024; /* Default 32KB */
710}
711
712static struct jffs2_xattr_datum *jffs2_find_xattr_datum(struct jffs2_sb_info *c, uint32_t xid)
713{
714 struct jffs2_xattr_datum *xd;
715 int i = xid % XATTRINDEX_HASHSIZE;
716
717 /* It's only used in scanning/building process. */
718 BUG_ON(!(c->flags & (JFFS2_SB_FLAG_SCANNING|JFFS2_SB_FLAG_BUILDING)));
719
720 list_for_each_entry(xd, &c->xattrindex[i], xindex) {
721 if (xd->xid==xid)
722 return xd;
723 }
724 return NULL;
725}
726
727void jffs2_clear_xattr_subsystem(struct jffs2_sb_info *c)
728{
729 struct jffs2_xattr_datum *xd, *_xd;
730 struct jffs2_xattr_ref *ref, *_ref;
731 int i;
732
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900733 for (ref=c->xref_temp; ref; ref = _ref) {
734 _ref = ref->next;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900735 jffs2_free_xattr_ref(ref);
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900736 }
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900737
738 for (ref=c->xref_dead_list; ref; ref = _ref) {
739 _ref = ref->next;
740 jffs2_free_xattr_ref(ref);
741 }
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900742
743 for (i=0; i < XATTRINDEX_HASHSIZE; i++) {
744 list_for_each_entry_safe(xd, _xd, &c->xattrindex[i], xindex) {
745 list_del(&xd->xindex);
746 if (xd->xname)
747 kfree(xd->xname);
748 jffs2_free_xattr_datum(xd);
749 }
750 }
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900751
752 list_for_each_entry_safe(xd, _xd, &c->xattr_dead_list, xindex) {
753 list_del(&xd->xindex);
754 jffs2_free_xattr_datum(xd);
755 }
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900756}
757
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900758#define XREF_TMPHASH_SIZE (128)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900759void jffs2_build_xattr_subsystem(struct jffs2_sb_info *c)
760{
761 struct jffs2_xattr_ref *ref, *_ref;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900762 struct jffs2_xattr_ref *xref_tmphash[XREF_TMPHASH_SIZE];
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900763 struct jffs2_xattr_datum *xd, *_xd;
764 struct jffs2_inode_cache *ic;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900765 struct jffs2_raw_node_ref *raw;
766 int i, xdatum_count = 0, xdatum_unchecked_count = 0, xref_count = 0;
KaiGai Kohei8a136952006-06-24 09:14:13 +0900767 int xdatum_orphan_count = 0, xref_orphan_count = 0, xref_dead_count = 0;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900768
769 BUG_ON(!(c->flags & JFFS2_SB_FLAG_BUILDING));
770
KaiGai Kohei8a136952006-06-24 09:14:13 +0900771 /* Phase.1 : Merge same xref */
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900772 for (i=0; i < XREF_TMPHASH_SIZE; i++)
773 xref_tmphash[i] = NULL;
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900774 for (ref=c->xref_temp; ref; ref=_ref) {
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900775 struct jffs2_xattr_ref *tmp;
776
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900777 _ref = ref->next;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900778 if (ref_flags(ref->node) != REF_PRISTINE) {
779 if (verify_xattr_ref(c, ref)) {
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900780 BUG_ON(ref->node->next_in_ino != (void *)ref);
781 ref->node->next_in_ino = NULL;
782 jffs2_mark_node_obsolete(c, ref->node);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900783 jffs2_free_xattr_ref(ref);
784 continue;
785 }
786 }
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900787
788 i = (ref->ino ^ ref->xid) % XREF_TMPHASH_SIZE;
789 for (tmp=xref_tmphash[i]; tmp; tmp=tmp->next) {
790 if (tmp->ino == ref->ino && tmp->xid == ref->xid)
791 break;
792 }
793 if (tmp) {
794 raw = ref->node;
795 if (ref->xseqno > tmp->xseqno) {
796 tmp->xseqno = ref->xseqno;
797 raw->next_in_ino = tmp->node;
798 tmp->node = raw;
799 } else {
800 raw->next_in_ino = tmp->node->next_in_ino;
801 tmp->node->next_in_ino = raw;
802 }
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900803 jffs2_free_xattr_ref(ref);
804 continue;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900805 } else {
806 ref->next = xref_tmphash[i];
807 xref_tmphash[i] = ref;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900808 }
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900809 }
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900810 c->xref_temp = NULL;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900811
KaiGai Kohei8a136952006-06-24 09:14:13 +0900812 /* Phase.2 : Bind xref with inode_cache and xattr_datum */
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900813 for (i=0; i < XREF_TMPHASH_SIZE; i++) {
814 for (ref=xref_tmphash[i]; ref; ref=_ref) {
KaiGai Kohei8a136952006-06-24 09:14:13 +0900815 xref_count++;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900816 _ref = ref->next;
817 if (is_xattr_ref_dead(ref)) {
818 ref->next = c->xref_dead_list;
819 c->xref_dead_list = ref;
KaiGai Kohei8a136952006-06-24 09:14:13 +0900820 xref_dead_count++;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900821 continue;
822 }
823 /* At this point, ref->xid and ref->ino contain XID and inode number.
824 ref->xd and ref->ic are not valid yet. */
825 xd = jffs2_find_xattr_datum(c, ref->xid);
826 ic = jffs2_get_ino_cache(c, ref->ino);
827 if (!xd || !ic) {
KaiGai Kohei8a136952006-06-24 09:14:13 +0900828 dbg_xattr("xref(ino=%u, xid=%u, xseqno=%u) is orphan.\n",
829 ref->ino, ref->xid, ref->xseqno);
830 ref->xseqno |= XREF_DELETE_MARKER;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900831 ref->next = c->xref_dead_list;
832 c->xref_dead_list = ref;
KaiGai Kohei8a136952006-06-24 09:14:13 +0900833 xref_orphan_count++;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900834 continue;
835 }
836 ref->xd = xd;
837 ref->ic = ic;
838 xd->refcnt++;
839 ref->next = ic->xref;
840 ic->xref = ref;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900841 }
842 }
843
KaiGai Kohei8a136952006-06-24 09:14:13 +0900844 /* Phase.3 : Link unchecked xdatum to xattr_unchecked list */
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900845 for (i=0; i < XATTRINDEX_HASHSIZE; i++) {
846 list_for_each_entry_safe(xd, _xd, &c->xattrindex[i], xindex) {
KaiGai Kohei8a136952006-06-24 09:14:13 +0900847 xdatum_count++;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900848 list_del_init(&xd->xindex);
849 if (!xd->refcnt) {
KaiGai Kohei8a136952006-06-24 09:14:13 +0900850 dbg_xattr("xdatum(xid=%u, version=%u) is orphan.\n",
851 xd->xid, xd->version);
852 xd->flags |= JFFS2_XFLAGS_DEAD;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900853 list_add(&xd->xindex, &c->xattr_unchecked);
KaiGai Kohei8a136952006-06-24 09:14:13 +0900854 xdatum_orphan_count++;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900855 continue;
856 }
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900857 if (is_xattr_datum_unchecked(c, xd)) {
858 dbg_xattr("unchecked xdatum(xid=%u, version=%u)\n",
859 xd->xid, xd->version);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900860 list_add(&xd->xindex, &c->xattr_unchecked);
861 xdatum_unchecked_count++;
862 }
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900863 }
864 }
865 /* build complete */
KaiGai Kohei8a136952006-06-24 09:14:13 +0900866 JFFS2_NOTICE("complete building xattr subsystem, %u of xdatum"
867 " (%u unchecked, %u orphan) and "
868 "%u of xref (%u dead, %u orphan) found.\n",
869 xdatum_count, xdatum_unchecked_count, xdatum_orphan_count,
870 xref_count, xref_dead_count, xref_orphan_count);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900871}
872
873struct jffs2_xattr_datum *jffs2_setup_xattr_datum(struct jffs2_sb_info *c,
874 uint32_t xid, uint32_t version)
875{
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900876 struct jffs2_xattr_datum *xd;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900877
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900878 xd = jffs2_find_xattr_datum(c, xid);
879 if (!xd) {
880 xd = jffs2_alloc_xattr_datum();
881 if (!xd)
882 return ERR_PTR(-ENOMEM);
883 xd->xid = xid;
884 xd->version = version;
885 if (xd->xid > c->highest_xid)
886 c->highest_xid = xd->xid;
887 list_add_tail(&xd->xindex, &c->xattrindex[xid % XATTRINDEX_HASHSIZE]);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900888 }
889 return xd;
890}
891
892/* -------- xattr subsystem functions ---------------
893 * xprefix_to_handler(xprefix)
894 * is used to translate xprefix into xattr_handler.
895 * jffs2_listxattr(dentry, buffer, size)
896 * is an implementation of listxattr handler on jffs2.
897 * do_jffs2_getxattr(inode, xprefix, xname, buffer, size)
898 * is an implementation of getxattr handler on jffs2.
899 * do_jffs2_setxattr(inode, xprefix, xname, buffer, size, flags)
900 * is an implementation of setxattr handler on jffs2.
901 * -------------------------------------------------- */
902struct xattr_handler *jffs2_xattr_handlers[] = {
903 &jffs2_user_xattr_handler,
904#ifdef CONFIG_JFFS2_FS_SECURITY
905 &jffs2_security_xattr_handler,
906#endif
907#ifdef CONFIG_JFFS2_FS_POSIX_ACL
908 &jffs2_acl_access_xattr_handler,
909 &jffs2_acl_default_xattr_handler,
910#endif
911 &jffs2_trusted_xattr_handler,
912 NULL
913};
914
915static struct xattr_handler *xprefix_to_handler(int xprefix) {
916 struct xattr_handler *ret;
917
918 switch (xprefix) {
919 case JFFS2_XPREFIX_USER:
920 ret = &jffs2_user_xattr_handler;
921 break;
922#ifdef CONFIG_JFFS2_FS_SECURITY
923 case JFFS2_XPREFIX_SECURITY:
924 ret = &jffs2_security_xattr_handler;
925 break;
926#endif
927#ifdef CONFIG_JFFS2_FS_POSIX_ACL
928 case JFFS2_XPREFIX_ACL_ACCESS:
929 ret = &jffs2_acl_access_xattr_handler;
930 break;
931 case JFFS2_XPREFIX_ACL_DEFAULT:
932 ret = &jffs2_acl_default_xattr_handler;
933 break;
934#endif
935 case JFFS2_XPREFIX_TRUSTED:
936 ret = &jffs2_trusted_xattr_handler;
937 break;
938 default:
939 ret = NULL;
940 break;
941 }
942 return ret;
943}
944
945ssize_t jffs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
946{
947 struct inode *inode = dentry->d_inode;
948 struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
949 struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
950 struct jffs2_inode_cache *ic = f->inocache;
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900951 struct jffs2_xattr_ref *ref, **pref;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900952 struct jffs2_xattr_datum *xd;
953 struct xattr_handler *xhandle;
954 ssize_t len, rc;
955 int retry = 0;
956
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900957 rc = check_xattr_ref_inode(c, ic);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900958 if (unlikely(rc))
959 return rc;
960
961 down_read(&c->xattr_sem);
962 retry:
963 len = 0;
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900964 for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900965 BUG_ON(ref->ic != ic);
966 xd = ref->xd;
967 if (!xd->xname) {
968 /* xdatum is unchached */
969 if (!retry) {
970 retry = 1;
971 up_read(&c->xattr_sem);
972 down_write(&c->xattr_sem);
973 goto retry;
974 } else {
975 rc = load_xattr_datum(c, xd);
976 if (unlikely(rc > 0)) {
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900977 *pref = ref->next;
KaiGai Kohei8a136952006-06-24 09:14:13 +0900978 delete_xattr_ref(c, ref);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900979 goto retry;
980 } else if (unlikely(rc < 0))
981 goto out;
982 }
983 }
984 xhandle = xprefix_to_handler(xd->xprefix);
985 if (!xhandle)
986 continue;
987 if (buffer) {
988 rc = xhandle->list(inode, buffer+len, size-len, xd->xname, xd->name_len);
989 } else {
990 rc = xhandle->list(inode, NULL, 0, xd->xname, xd->name_len);
991 }
992 if (rc < 0)
993 goto out;
994 len += rc;
995 }
996 rc = len;
997 out:
998 if (!retry) {
999 up_read(&c->xattr_sem);
1000 } else {
1001 up_write(&c->xattr_sem);
1002 }
1003 return rc;
1004}
1005
1006int do_jffs2_getxattr(struct inode *inode, int xprefix, const char *xname,
1007 char *buffer, size_t size)
1008{
1009 struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
1010 struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
1011 struct jffs2_inode_cache *ic = f->inocache;
1012 struct jffs2_xattr_datum *xd;
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +09001013 struct jffs2_xattr_ref *ref, **pref;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001014 int rc, retry = 0;
1015
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +09001016 rc = check_xattr_ref_inode(c, ic);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001017 if (unlikely(rc))
1018 return rc;
1019
1020 down_read(&c->xattr_sem);
1021 retry:
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +09001022 for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001023 BUG_ON(ref->ic!=ic);
1024
1025 xd = ref->xd;
1026 if (xd->xprefix != xprefix)
1027 continue;
1028 if (!xd->xname) {
1029 /* xdatum is unchached */
1030 if (!retry) {
1031 retry = 1;
1032 up_read(&c->xattr_sem);
1033 down_write(&c->xattr_sem);
1034 goto retry;
1035 } else {
1036 rc = load_xattr_datum(c, xd);
1037 if (unlikely(rc > 0)) {
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +09001038 *pref = ref->next;
KaiGai Kohei8a136952006-06-24 09:14:13 +09001039 delete_xattr_ref(c, ref);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001040 goto retry;
1041 } else if (unlikely(rc < 0)) {
1042 goto out;
1043 }
1044 }
1045 }
1046 if (!strcmp(xname, xd->xname)) {
1047 rc = xd->value_len;
1048 if (buffer) {
1049 if (size < rc) {
1050 rc = -ERANGE;
1051 } else {
1052 memcpy(buffer, xd->xvalue, rc);
1053 }
1054 }
1055 goto out;
1056 }
1057 }
1058 rc = -ENODATA;
1059 out:
1060 if (!retry) {
1061 up_read(&c->xattr_sem);
1062 } else {
1063 up_write(&c->xattr_sem);
1064 }
1065 return rc;
1066}
1067
1068int do_jffs2_setxattr(struct inode *inode, int xprefix, const char *xname,
1069 const char *buffer, size_t size, int flags)
1070{
1071 struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
1072 struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
1073 struct jffs2_inode_cache *ic = f->inocache;
1074 struct jffs2_xattr_datum *xd;
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +09001075 struct jffs2_xattr_ref *ref, *newref, **pref;
David Woodhouse9fe48542006-05-23 00:38:06 +01001076 uint32_t length, request;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001077 int rc;
1078
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +09001079 rc = check_xattr_ref_inode(c, ic);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001080 if (unlikely(rc))
1081 return rc;
1082
1083 request = PAD(sizeof(struct jffs2_raw_xattr) + strlen(xname) + 1 + size);
David Woodhouse9fe48542006-05-23 00:38:06 +01001084 rc = jffs2_reserve_space(c, request, &length,
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001085 ALLOC_NORMAL, JFFS2_SUMMARY_XATTR_SIZE);
1086 if (rc) {
1087 JFFS2_WARNING("jffs2_reserve_space()=%d, request=%u\n", rc, request);
1088 return rc;
1089 }
1090
1091 /* Find existing xattr */
1092 down_write(&c->xattr_sem);
1093 retry:
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +09001094 for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001095 xd = ref->xd;
1096 if (xd->xprefix != xprefix)
1097 continue;
1098 if (!xd->xname) {
1099 rc = load_xattr_datum(c, xd);
1100 if (unlikely(rc > 0)) {
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +09001101 *pref = ref->next;
KaiGai Kohei8a136952006-06-24 09:14:13 +09001102 delete_xattr_ref(c, ref);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001103 goto retry;
1104 } else if (unlikely(rc < 0))
1105 goto out;
1106 }
1107 if (!strcmp(xd->xname, xname)) {
1108 if (flags & XATTR_CREATE) {
1109 rc = -EEXIST;
1110 goto out;
1111 }
1112 if (!buffer) {
KaiGai Kohei8a136952006-06-24 09:14:13 +09001113 ref->ino = ic->ino;
1114 ref->xid = xd->xid;
1115 ref->xseqno |= XREF_DELETE_MARKER;
1116 rc = save_xattr_ref(c, ref);
1117 if (!rc) {
1118 *pref = ref->next;
1119 spin_lock(&c->erase_completion_lock);
1120 ref->next = c->xref_dead_list;
1121 c->xref_dead_list = ref;
1122 spin_unlock(&c->erase_completion_lock);
1123 if (!--xd->refcnt)
1124 delete_xattr_datum(c, xd);
1125 } else {
1126 ref->ic = ic;
1127 ref->xd = xd;
1128 ref->xseqno &= ~XREF_DELETE_MARKER;
1129 }
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001130 goto out;
1131 }
1132 goto found;
1133 }
1134 }
1135 /* not found */
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001136 if (flags & XATTR_REPLACE) {
1137 rc = -ENODATA;
1138 goto out;
1139 }
1140 if (!buffer) {
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001141 rc = -ENODATA;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001142 goto out;
1143 }
1144 found:
David Woodhouse9fe48542006-05-23 00:38:06 +01001145 xd = create_xattr_datum(c, xprefix, xname, buffer, size);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001146 if (IS_ERR(xd)) {
1147 rc = PTR_ERR(xd);
1148 goto out;
1149 }
1150 up_write(&c->xattr_sem);
1151 jffs2_complete_reservation(c);
1152
1153 /* create xattr_ref */
1154 request = PAD(sizeof(struct jffs2_raw_xref));
David Woodhouse9fe48542006-05-23 00:38:06 +01001155 rc = jffs2_reserve_space(c, request, &length,
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001156 ALLOC_NORMAL, JFFS2_SUMMARY_XREF_SIZE);
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001157 down_write(&c->xattr_sem);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001158 if (rc) {
1159 JFFS2_WARNING("jffs2_reserve_space()=%d, request=%u\n", rc, request);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001160 xd->refcnt--;
1161 if (!xd->refcnt)
KaiGai Kohei8a136952006-06-24 09:14:13 +09001162 delete_xattr_datum(c, xd);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001163 up_write(&c->xattr_sem);
1164 return rc;
1165 }
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +09001166 if (ref)
1167 *pref = ref->next;
David Woodhouse9fe48542006-05-23 00:38:06 +01001168 newref = create_xattr_ref(c, ic, xd);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001169 if (IS_ERR(newref)) {
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +09001170 if (ref) {
1171 ref->next = ic->xref;
1172 ic->xref = ref;
1173 }
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001174 rc = PTR_ERR(newref);
1175 xd->refcnt--;
1176 if (!xd->refcnt)
KaiGai Kohei8a136952006-06-24 09:14:13 +09001177 delete_xattr_datum(c, xd);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001178 } else if (ref) {
KaiGai Kohei8a136952006-06-24 09:14:13 +09001179 delete_xattr_ref(c, ref);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001180 }
1181 out:
1182 up_write(&c->xattr_sem);
1183 jffs2_complete_reservation(c);
1184 return rc;
1185}
1186
1187/* -------- garbage collector functions -------------
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001188 * jffs2_garbage_collect_xattr_datum(c, xd, raw)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001189 * is used to move xdatum into new node.
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001190 * jffs2_garbage_collect_xattr_ref(c, ref, raw)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001191 * is used to move xref into new node.
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001192 * jffs2_verify_xattr(c)
1193 * is used to call do_verify_xattr_datum() before garbage collecting.
KaiGai Kohei8a136952006-06-24 09:14:13 +09001194 * jffs2_release_xattr_datum(c, xd)
1195 * is used to release an in-memory object of xdatum.
1196 * jffs2_release_xattr_ref(c, ref)
1197 * is used to release an in-memory object of xref.
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001198 * -------------------------------------------------- */
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001199int jffs2_garbage_collect_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd,
1200 struct jffs2_raw_node_ref *raw)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001201{
David Woodhouse9fe48542006-05-23 00:38:06 +01001202 uint32_t totlen, length, old_ofs;
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001203 int rc = 0;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001204
KaiGai Kohei084702e2006-05-13 15:16:13 +09001205 down_write(&c->xattr_sem);
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001206 if (xd->node != raw)
1207 goto out;
KaiGai Kohei8a136952006-06-24 09:14:13 +09001208 if (xd->flags & (JFFS2_XFLAGS_DEAD|JFFS2_XFLAGS_INVALID))
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001209 goto out;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001210
KaiGai Kohei8a136952006-06-24 09:14:13 +09001211 rc = load_xattr_datum(c, xd);
1212 if (unlikely(rc)) {
1213 rc = (rc > 0) ? 0 : rc;
1214 goto out;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001215 }
KaiGai Kohei8a136952006-06-24 09:14:13 +09001216 old_ofs = ref_offset(xd->node);
1217 totlen = PAD(sizeof(struct jffs2_raw_xattr)
1218 + xd->name_len + 1 + xd->value_len);
David Woodhouse9fe48542006-05-23 00:38:06 +01001219 rc = jffs2_reserve_space_gc(c, totlen, &length, JFFS2_SUMMARY_XATTR_SIZE);
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001220 if (rc) {
1221 JFFS2_WARNING("jffs2_reserve_space_gc()=%d, request=%u\n", rc, totlen);
KaiGai Kohei084702e2006-05-13 15:16:13 +09001222 rc = rc ? rc : -EBADFD;
1223 goto out;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001224 }
David Woodhouse9fe48542006-05-23 00:38:06 +01001225 rc = save_xattr_datum(c, xd);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001226 if (!rc)
1227 dbg_xattr("xdatum (xid=%u, version=%u) GC'ed from %#08x to %08x\n",
1228 xd->xid, xd->version, old_ofs, ref_offset(xd->node));
KaiGai Kohei084702e2006-05-13 15:16:13 +09001229 out:
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001230 if (!rc)
1231 jffs2_mark_node_obsolete(c, raw);
KaiGai Kohei084702e2006-05-13 15:16:13 +09001232 up_write(&c->xattr_sem);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001233 return rc;
1234}
1235
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001236int jffs2_garbage_collect_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref,
1237 struct jffs2_raw_node_ref *raw)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001238{
David Woodhouse9fe48542006-05-23 00:38:06 +01001239 uint32_t totlen, length, old_ofs;
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001240 int rc = 0;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001241
KaiGai Kohei084702e2006-05-13 15:16:13 +09001242 down_write(&c->xattr_sem);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001243 BUG_ON(!ref->node);
1244
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001245 if (ref->node != raw)
1246 goto out;
1247 if (is_xattr_ref_dead(ref) && (raw->next_in_ino == (void *)ref))
KaiGai Kohei084702e2006-05-13 15:16:13 +09001248 goto out;
1249
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001250 old_ofs = ref_offset(ref->node);
1251 totlen = ref_totlen(c, c->gcblock, ref->node);
1252
David Woodhouse9fe48542006-05-23 00:38:06 +01001253 rc = jffs2_reserve_space_gc(c, totlen, &length, JFFS2_SUMMARY_XREF_SIZE);
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001254 if (rc) {
1255 JFFS2_WARNING("%s: jffs2_reserve_space_gc() = %d, request = %u\n",
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001256 __FUNCTION__, rc, totlen);
KaiGai Kohei084702e2006-05-13 15:16:13 +09001257 rc = rc ? rc : -EBADFD;
1258 goto out;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001259 }
David Woodhouse9fe48542006-05-23 00:38:06 +01001260 rc = save_xattr_ref(c, ref);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001261 if (!rc)
1262 dbg_xattr("xref (ino=%u, xid=%u) GC'ed from %#08x to %08x\n",
1263 ref->ic->ino, ref->xd->xid, old_ofs, ref_offset(ref->node));
KaiGai Kohei084702e2006-05-13 15:16:13 +09001264 out:
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001265 if (!rc)
1266 jffs2_mark_node_obsolete(c, raw);
KaiGai Kohei084702e2006-05-13 15:16:13 +09001267 up_write(&c->xattr_sem);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001268 return rc;
1269}
1270
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001271int jffs2_verify_xattr(struct jffs2_sb_info *c)
1272{
1273 struct jffs2_xattr_datum *xd, *_xd;
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001274 struct jffs2_eraseblock *jeb;
1275 struct jffs2_raw_node_ref *raw;
1276 uint32_t totlen;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001277 int rc;
1278
1279 down_write(&c->xattr_sem);
1280 list_for_each_entry_safe(xd, _xd, &c->xattr_unchecked, xindex) {
1281 rc = do_verify_xattr_datum(c, xd);
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001282 if (rc < 0)
1283 continue;
1284 list_del_init(&xd->xindex);
1285 spin_lock(&c->erase_completion_lock);
1286 for (raw=xd->node; raw != (void *)xd; raw=raw->next_in_ino) {
1287 if (ref_flags(raw) != REF_UNCHECKED)
1288 continue;
1289 jeb = &c->blocks[ref_offset(raw) / c->sector_size];
1290 totlen = PAD(ref_totlen(c, jeb, raw));
1291 c->unchecked_size -= totlen; c->used_size += totlen;
1292 jeb->unchecked_size -= totlen; jeb->used_size += totlen;
1293 raw->flash_offset = ref_offset(raw)
1294 | ((xd->node == (void *)raw) ? REF_PRISTINE : REF_NORMAL);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001295 }
KaiGai Kohei8a136952006-06-24 09:14:13 +09001296 if (xd->flags & JFFS2_XFLAGS_DEAD)
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001297 list_add(&xd->xindex, &c->xattr_dead_list);
1298 spin_unlock(&c->erase_completion_lock);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001299 }
1300 up_write(&c->xattr_sem);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001301 return list_empty(&c->xattr_unchecked) ? 1 : 0;
1302}
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001303
1304void jffs2_release_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
1305{
1306 /* must be called under spin_lock(&c->erase_completion_lock) */
KaiGai Kohei8a136952006-06-24 09:14:13 +09001307 if (xd->refcnt > 0 || xd->node != (void *)xd)
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001308 return;
1309
1310 list_del(&xd->xindex);
1311 jffs2_free_xattr_datum(xd);
1312}
1313
1314void jffs2_release_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
1315{
1316 /* must be called under spin_lock(&c->erase_completion_lock) */
1317 struct jffs2_xattr_ref *tmp, **ptmp;
1318
1319 if (ref->node != (void *)ref)
1320 return;
1321
1322 for (tmp=c->xref_dead_list, ptmp=&c->xref_dead_list; tmp; ptmp=&tmp->next, tmp=tmp->next) {
1323 if (ref == tmp) {
1324 *ptmp = tmp->next;
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001325 break;
1326 }
1327 }
KaiGai Kohei8a136952006-06-24 09:14:13 +09001328 jffs2_free_xattr_ref(ref);
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001329}