jffs2: allow to discriminate between recoverable and non-recoverable errors
[deliverable/linux.git] / fs / jffs2 / xattr.c
CommitLineData
652ecc20
KK
1/*
2 * JFFS2 -- Journalling Flash File System, Version 2.
aa98d7cf 3 *
c00c310e 4 * Copyright © 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 */
c00c310e 11
9bbf29e4
JP
12#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
9824f75d
JCD
14#define JFFS2_XATTR_IS_CORRUPTED 1
15
aa98d7cf
KK
16#include <linux/kernel.h>
17#include <linux/slab.h>
18#include <linux/fs.h>
19#include <linux/time.h>
20#include <linux/pagemap.h>
21#include <linux/highmem.h>
22#include <linux/crc32.h>
23#include <linux/jffs2.h>
24#include <linux/xattr.h>
25#include <linux/mtd/mtd.h>
26#include "nodelist.h"
27/* -------- xdatum related functions ----------------
28 * xattr_datum_hashkey(xprefix, xname, xvalue, xsize)
29 * is used to calcurate xdatum hashkey. The reminder of hashkey into XATTRINDEX_HASHSIZE is
30 * the index of the xattr name/value pair cache (c->xattrindex).
c9f700f8
KK
31 * is_xattr_datum_unchecked(c, xd)
32 * returns 1, if xdatum contains any unchecked raw nodes. if all raw nodes are not
33 * unchecked, it returns 0.
aa98d7cf
KK
34 * unload_xattr_datum(c, xd)
35 * is used to release xattr name/value pair and detach from c->xattrindex.
36 * reclaim_xattr_datum(c)
37 * is used to reclaim xattr name/value pairs on the xattr name/value pair cache when
bea93128 38 * memory usage by cache is over c->xdatum_mem_threshold. Currently, this threshold
aa98d7cf 39 * is hard coded as 32KiB.
aa98d7cf
KK
40 * do_verify_xattr_datum(c, xd)
41 * is used to load the xdatum informations without name/value pair from the medium.
42 * It's necessary once, because those informations are not collected during mounting
43 * process when EBS is enabled.
44 * 0 will be returned, if success. An negative return value means recoverable error, and
45 * positive return value means unrecoverable error. Thus, caller must remove this xdatum
46 * and xref when it returned positive value.
47 * do_load_xattr_datum(c, xd)
48 * is used to load name/value pair from the medium.
49 * The meanings of return value is same as do_verify_xattr_datum().
50 * load_xattr_datum(c, xd)
51 * is used to be as a wrapper of do_verify_xattr_datum() and do_load_xattr_datum().
52 * If xd need to call do_verify_xattr_datum() at first, it's called before calling
53 * do_load_xattr_datum(). The meanings of return value is same as do_verify_xattr_datum().
9fe4854c 54 * save_xattr_datum(c, xd)
aa98d7cf 55 * is used to write xdatum to medium. xd->version will be incremented.
9fe4854c 56 * create_xattr_datum(c, xprefix, xname, xvalue, xsize)
aa98d7cf 57 * is used to create new xdatum and write to medium.
c6e8c6cc
KK
58 * unrefer_xattr_datum(c, xd)
59 * is used to delete a xdatum. When nobody refers this xdatum, JFFS2_XFLAGS_DEAD
60 * is set on xd->flags and chained xattr_dead_list or release it immediately.
61 * In the first case, the garbage collector release it later.
aa98d7cf 62 * -------------------------------------------------- */
aa98d7cf
KK
63static uint32_t xattr_datum_hashkey(int xprefix, const char *xname, const char *xvalue, int xsize)
64{
65 int name_len = strlen(xname);
66
67 return crc32(xprefix, xname, name_len) ^ crc32(xprefix, xvalue, xsize);
68}
69
c9f700f8
KK
70static int is_xattr_datum_unchecked(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
71{
72 struct jffs2_raw_node_ref *raw;
73 int rc = 0;
74
75 spin_lock(&c->erase_completion_lock);
76 for (raw=xd->node; raw != (void *)xd; raw=raw->next_in_ino) {
77 if (ref_flags(raw) == REF_UNCHECKED) {
78 rc = 1;
79 break;
80 }
81 }
82 spin_unlock(&c->erase_completion_lock);
83 return rc;
84}
85
aa98d7cf
KK
86static void unload_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
87{
88 /* must be called under down_write(xattr_sem) */
8e24eea7 89 D1(dbg_xattr("%s: xid=%u, version=%u\n", __func__, xd->xid, xd->version));
aa98d7cf
KK
90 if (xd->xname) {
91 c->xdatum_mem_usage -= (xd->name_len + 1 + xd->value_len);
92 kfree(xd->xname);
93 }
94
95 list_del_init(&xd->xindex);
96 xd->hashkey = 0;
97 xd->xname = NULL;
98 xd->xvalue = NULL;
99}
100
101static void reclaim_xattr_datum(struct jffs2_sb_info *c)
102{
103 /* must be called under down_write(xattr_sem) */
104 struct jffs2_xattr_datum *xd, *_xd;
105 uint32_t target, before;
106 static int index = 0;
107 int count;
108
109 if (c->xdatum_mem_threshold > c->xdatum_mem_usage)
110 return;
111
112 before = c->xdatum_mem_usage;
113 target = c->xdatum_mem_usage * 4 / 5; /* 20% reduction */
114 for (count = 0; count < XATTRINDEX_HASHSIZE; count++) {
115 list_for_each_entry_safe(xd, _xd, &c->xattrindex[index], xindex) {
116 if (xd->flags & JFFS2_XFLAGS_HOT) {
117 xd->flags &= ~JFFS2_XFLAGS_HOT;
118 } else if (!(xd->flags & JFFS2_XFLAGS_BIND)) {
119 unload_xattr_datum(c, xd);
120 }
121 if (c->xdatum_mem_usage <= target)
122 goto out;
123 }
124 index = (index+1) % XATTRINDEX_HASHSIZE;
125 }
126 out:
127 JFFS2_NOTICE("xdatum_mem_usage from %u byte to %u byte (%u byte reclaimed)\n",
128 before, c->xdatum_mem_usage, before - c->xdatum_mem_usage);
129}
130
aa98d7cf
KK
131static int do_verify_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
132{
133 /* must be called under down_write(xattr_sem) */
134 struct jffs2_eraseblock *jeb;
c9f700f8 135 struct jffs2_raw_node_ref *raw;
aa98d7cf
KK
136 struct jffs2_raw_xattr rx;
137 size_t readlen;
c9f700f8 138 uint32_t crc, offset, totlen;
aa98d7cf
KK
139 int rc;
140
c9f700f8
KK
141 spin_lock(&c->erase_completion_lock);
142 offset = ref_offset(xd->node);
143 if (ref_flags(xd->node) == REF_PRISTINE)
144 goto complete;
145 spin_unlock(&c->erase_completion_lock);
aa98d7cf 146
c9f700f8 147 rc = jffs2_flash_read(c, offset, sizeof(rx), &readlen, (char *)&rx);
aa98d7cf 148 if (rc || readlen != sizeof(rx)) {
89291a9d 149 JFFS2_WARNING("jffs2_flash_read()=%d, req=%zu, read=%zu at %#08x\n",
c9f700f8 150 rc, sizeof(rx), readlen, offset);
aa98d7cf
KK
151 return rc ? rc : -EIO;
152 }
153 crc = crc32(0, &rx, sizeof(rx) - 4);
154 if (crc != je32_to_cpu(rx.node_crc)) {
c9f700f8
KK
155 JFFS2_ERROR("node CRC failed at %#08x, read=%#08x, calc=%#08x\n",
156 offset, je32_to_cpu(rx.hdr_crc), crc);
157 xd->flags |= JFFS2_XFLAGS_INVALID;
9824f75d 158 return JFFS2_XATTR_IS_CORRUPTED;
aa98d7cf 159 }
8a13695c 160 totlen = PAD(sizeof(rx) + rx.name_len + 1 + je16_to_cpu(rx.value_len));
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;
9824f75d 174 return JFFS2_XATTR_IS_CORRUPTED;
aa98d7cf
KK
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
203static 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) {
9824f75d 232 JFFS2_WARNING("node CRC failed (JFFS2_NODETYPE_XATTR)"
aa98d7cf
KK
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;
9824f75d 237 return JFFS2_XATTR_IS_CORRUPTED;
aa98d7cf
KK
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
262static 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 270
8a13695c 271 BUG_ON(xd->flags & JFFS2_XFLAGS_DEAD);
c9f700f8
KK
272 if (xd->xname)
273 return 0;
274 if (xd->flags & JFFS2_XFLAGS_INVALID)
9824f75d 275 return JFFS2_XATTR_IS_CORRUPTED;
c9f700f8 276 if (unlikely(is_xattr_datum_unchecked(c, xd)))
aa98d7cf 277 rc = do_verify_xattr_datum(c, xd);
aa98d7cf
KK
278 if (!rc)
279 rc = do_load_xattr_datum(c, xd);
280 return rc;
281}
282
9fe4854c 283static int save_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
aa98d7cf
KK
284{
285 /* must be called under down_write(xattr_sem) */
2f785402 286 struct jffs2_raw_xattr rx;
aa98d7cf 287 struct kvec vecs[2];
89291a9d 288 size_t length;
8a13695c 289 int rc, totlen;
9fe4854c 290 uint32_t phys_ofs = write_ofs(c);
aa98d7cf 291
8a13695c
KK
292 BUG_ON(!xd->xname);
293 BUG_ON(xd->flags & (JFFS2_XFLAGS_DEAD|JFFS2_XFLAGS_INVALID));
aa98d7cf
KK
294
295 vecs[0].iov_base = &rx;
8a13695c
KK
296 vecs[0].iov_len = sizeof(rx);
297 vecs[1].iov_base = xd->xname;
298 vecs[1].iov_len = xd->name_len + 1 + xd->value_len;
299 totlen = vecs[0].iov_len + vecs[1].iov_len;
300
aa98d7cf 301 /* Setup raw-xattr */
c9f700f8 302 memset(&rx, 0, sizeof(rx));
aa98d7cf
KK
303 rx.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
304 rx.nodetype = cpu_to_je16(JFFS2_NODETYPE_XATTR);
305 rx.totlen = cpu_to_je32(PAD(totlen));
306 rx.hdr_crc = cpu_to_je32(crc32(0, &rx, sizeof(struct jffs2_unknown_node) - 4));
307
308 rx.xid = cpu_to_je32(xd->xid);
8a13695c
KK
309 rx.version = cpu_to_je32(++xd->version);
310 rx.xprefix = xd->xprefix;
311 rx.name_len = xd->name_len;
312 rx.value_len = cpu_to_je16(xd->value_len);
313 rx.data_crc = cpu_to_je32(crc32(0, vecs[1].iov_base, vecs[1].iov_len));
aa98d7cf
KK
314 rx.node_crc = cpu_to_je32(crc32(0, &rx, sizeof(struct jffs2_raw_xattr) - 4));
315
8a13695c 316 rc = jffs2_flash_writev(c, vecs, 2, phys_ofs, &length, 0);
aa98d7cf 317 if (rc || totlen != length) {
89291a9d 318 JFFS2_WARNING("jffs2_flash_writev()=%d, req=%u, wrote=%zu, at %#08x\n",
aa98d7cf
KK
319 rc, totlen, length, phys_ofs);
320 rc = rc ? rc : -EIO;
2f785402
DW
321 if (length)
322 jffs2_add_physical_node_ref(c, phys_ofs | REF_OBSOLETE, PAD(totlen), NULL);
323
aa98d7cf
KK
324 return rc;
325 }
aa98d7cf 326 /* success */
c9f700f8 327 jffs2_add_physical_node_ref(c, phys_ofs | REF_PRISTINE, PAD(totlen), (void *)xd);
aa98d7cf
KK
328
329 dbg_xattr("success on saving xdatum (xid=%u, version=%u, xprefix=%u, xname='%s')\n",
330 xd->xid, xd->version, xd->xprefix, xd->xname);
331
332 return 0;
333}
334
335static struct jffs2_xattr_datum *create_xattr_datum(struct jffs2_sb_info *c,
336 int xprefix, const char *xname,
9fe4854c 337 const char *xvalue, int xsize)
aa98d7cf
KK
338{
339 /* must be called under down_write(xattr_sem) */
340 struct jffs2_xattr_datum *xd;
341 uint32_t hashkey, name_len;
342 char *data;
343 int i, rc;
344
345 /* Search xattr_datum has same xname/xvalue by index */
346 hashkey = xattr_datum_hashkey(xprefix, xname, xvalue, xsize);
347 i = hashkey % XATTRINDEX_HASHSIZE;
348 list_for_each_entry(xd, &c->xattrindex[i], xindex) {
349 if (xd->hashkey==hashkey
350 && xd->xprefix==xprefix
351 && xd->value_len==xsize
352 && !strcmp(xd->xname, xname)
353 && !memcmp(xd->xvalue, xvalue, xsize)) {
2c887e23 354 atomic_inc(&xd->refcnt);
aa98d7cf
KK
355 return xd;
356 }
357 }
358
359 /* Not found, Create NEW XATTR-Cache */
360 name_len = strlen(xname);
361
362 xd = jffs2_alloc_xattr_datum();
363 if (!xd)
364 return ERR_PTR(-ENOMEM);
365
366 data = kmalloc(name_len + 1 + xsize, GFP_KERNEL);
367 if (!data) {
368 jffs2_free_xattr_datum(xd);
369 return ERR_PTR(-ENOMEM);
370 }
371 strcpy(data, xname);
372 memcpy(data + name_len + 1, xvalue, xsize);
373
2c887e23 374 atomic_set(&xd->refcnt, 1);
aa98d7cf
KK
375 xd->xid = ++c->highest_xid;
376 xd->flags |= JFFS2_XFLAGS_HOT;
377 xd->xprefix = xprefix;
378
379 xd->hashkey = hashkey;
380 xd->xname = data;
381 xd->xvalue = data + name_len + 1;
382 xd->name_len = name_len;
383 xd->value_len = xsize;
384 xd->data_crc = crc32(0, data, xd->name_len + 1 + xd->value_len);
385
9fe4854c 386 rc = save_xattr_datum(c, xd);
aa98d7cf
KK
387 if (rc) {
388 kfree(xd->xname);
389 jffs2_free_xattr_datum(xd);
390 return ERR_PTR(rc);
391 }
392
393 /* Insert Hash Index */
394 i = hashkey % XATTRINDEX_HASHSIZE;
395 list_add(&xd->xindex, &c->xattrindex[i]);
396
397 c->xdatum_mem_usage += (xd->name_len + 1 + xd->value_len);
398 reclaim_xattr_datum(c);
399
400 return xd;
401}
402
c6e8c6cc 403static void unrefer_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
c9f700f8
KK
404{
405 /* must be called under down_write(xattr_sem) */
c6e8c6cc 406 if (atomic_dec_and_lock(&xd->refcnt, &c->erase_completion_lock)) {
c6e8c6cc
KK
407 unload_xattr_datum(c, xd);
408 xd->flags |= JFFS2_XFLAGS_DEAD;
409 if (xd->node == (void *)xd) {
410 BUG_ON(!(xd->flags & JFFS2_XFLAGS_INVALID));
411 jffs2_free_xattr_datum(xd);
412 } else {
413 list_add(&xd->xindex, &c->xattr_dead_list);
414 }
415 spin_unlock(&c->erase_completion_lock);
416
a6b1d82d
JG
417 dbg_xattr("xdatum(xid=%u, version=%u) was removed.\n",
418 xd->xid, xd->version);
c9f700f8 419 }
c9f700f8
KK
420}
421
21b9879b 422/* -------- xref related functions ------------------
aa98d7cf
KK
423 * verify_xattr_ref(c, ref)
424 * is used to load xref information from medium. Because summary data does not
425 * contain xid/ino, it's necessary to verify once while mounting process.
9fe4854c 426 * save_xattr_ref(c, ref)
8a13695c
KK
427 * is used to write xref to medium. If delete marker is marked, it write
428 * a delete marker of xref into medium.
9fe4854c 429 * create_xattr_ref(c, ic, xd)
aa98d7cf 430 * is used to create a new xref and write to medium.
8a13695c
KK
431 * delete_xattr_ref(c, ref)
432 * is used to delete jffs2_xattr_ref. It marks xref XREF_DELETE_MARKER,
433 * and allows GC to reclaim those physical nodes.
aa98d7cf
KK
434 * jffs2_xattr_delete_inode(c, ic)
435 * is called to remove xrefs related to obsolete inode when inode is unlinked.
436 * jffs2_xattr_free_inode(c, ic)
437 * is called to release xattr related objects when unmounting.
8f2b6f49 438 * check_xattr_ref_inode(c, ic)
aa98d7cf
KK
439 * is used to confirm inode does not have duplicate xattr name/value pair.
440 * -------------------------------------------------- */
441static int verify_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
442{
443 struct jffs2_eraseblock *jeb;
c9f700f8 444 struct jffs2_raw_node_ref *raw;
aa98d7cf
KK
445 struct jffs2_raw_xref rr;
446 size_t readlen;
c9f700f8 447 uint32_t crc, offset, totlen;
aa98d7cf
KK
448 int rc;
449
c9f700f8
KK
450 spin_lock(&c->erase_completion_lock);
451 if (ref_flags(ref->node) != REF_UNCHECKED)
452 goto complete;
453 offset = ref_offset(ref->node);
454 spin_unlock(&c->erase_completion_lock);
aa98d7cf 455
c9f700f8 456 rc = jffs2_flash_read(c, offset, sizeof(rr), &readlen, (char *)&rr);
aa98d7cf 457 if (rc || sizeof(rr) != readlen) {
89291a9d 458 JFFS2_WARNING("jffs2_flash_read()=%d, req=%zu, read=%zu, at %#08x\n",
c9f700f8 459 rc, sizeof(rr), readlen, offset);
aa98d7cf
KK
460 return rc ? rc : -EIO;
461 }
462 /* obsolete node */
463 crc = crc32(0, &rr, sizeof(rr) - 4);
464 if (crc != je32_to_cpu(rr.node_crc)) {
c9f700f8
KK
465 JFFS2_ERROR("node CRC failed at %#08x, read=%#08x, calc=%#08x\n",
466 offset, je32_to_cpu(rr.node_crc), crc);
9824f75d 467 return JFFS2_XATTR_IS_CORRUPTED;
aa98d7cf
KK
468 }
469 if (je16_to_cpu(rr.magic) != JFFS2_MAGIC_BITMASK
470 || je16_to_cpu(rr.nodetype) != JFFS2_NODETYPE_XREF
471 || je32_to_cpu(rr.totlen) != PAD(sizeof(rr))) {
472 JFFS2_ERROR("inconsistent xref at %#08x, magic=%#04x/%#04x, "
89291a9d 473 "nodetype=%#04x/%#04x, totlen=%u/%zu\n",
c9f700f8 474 offset, je16_to_cpu(rr.magic), JFFS2_MAGIC_BITMASK,
aa98d7cf
KK
475 je16_to_cpu(rr.nodetype), JFFS2_NODETYPE_XREF,
476 je32_to_cpu(rr.totlen), PAD(sizeof(rr)));
9824f75d 477 return JFFS2_XATTR_IS_CORRUPTED;
aa98d7cf
KK
478 }
479 ref->ino = je32_to_cpu(rr.ino);
480 ref->xid = je32_to_cpu(rr.xid);
c9f700f8
KK
481 ref->xseqno = je32_to_cpu(rr.xseqno);
482 if (ref->xseqno > c->highest_xseqno)
483 c->highest_xseqno = (ref->xseqno & ~XREF_DELETE_MARKER);
aa98d7cf
KK
484
485 spin_lock(&c->erase_completion_lock);
c9f700f8
KK
486 complete:
487 for (raw=ref->node; raw != (void *)ref; raw=raw->next_in_ino) {
488 jeb = &c->blocks[ref_offset(raw) / c->sector_size];
489 totlen = PAD(ref_totlen(c, jeb, raw));
490 if (ref_flags(raw) == REF_UNCHECKED) {
491 c->unchecked_size -= totlen; c->used_size += totlen;
492 jeb->unchecked_size -= totlen; jeb->used_size += totlen;
493 }
494 raw->flash_offset = ref_offset(raw) | ((ref->node==raw) ? REF_PRISTINE : REF_NORMAL);
495 }
aa98d7cf
KK
496 spin_unlock(&c->erase_completion_lock);
497
498 dbg_xattr("success on verifying xref (ino=%u, xid=%u) at %#08x\n",
499 ref->ino, ref->xid, ref_offset(ref->node));
500 return 0;
501}
502
9fe4854c 503static int save_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
aa98d7cf
KK
504{
505 /* must be called under down_write(xattr_sem) */
aa98d7cf 506 struct jffs2_raw_xref rr;
89291a9d 507 size_t length;
c9f700f8 508 uint32_t xseqno, phys_ofs = write_ofs(c);
aa98d7cf
KK
509 int ret;
510
aa98d7cf
KK
511 rr.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
512 rr.nodetype = cpu_to_je16(JFFS2_NODETYPE_XREF);
513 rr.totlen = cpu_to_je32(PAD(sizeof(rr)));
514 rr.hdr_crc = cpu_to_je32(crc32(0, &rr, sizeof(struct jffs2_unknown_node) - 4));
515
c9f700f8
KK
516 xseqno = (c->highest_xseqno += 2);
517 if (is_xattr_ref_dead(ref)) {
518 xseqno |= XREF_DELETE_MARKER;
519 rr.ino = cpu_to_je32(ref->ino);
520 rr.xid = cpu_to_je32(ref->xid);
521 } else {
522 rr.ino = cpu_to_je32(ref->ic->ino);
523 rr.xid = cpu_to_je32(ref->xd->xid);
524 }
525 rr.xseqno = cpu_to_je32(xseqno);
aa98d7cf
KK
526 rr.node_crc = cpu_to_je32(crc32(0, &rr, sizeof(rr) - 4));
527
528 ret = jffs2_flash_write(c, phys_ofs, sizeof(rr), &length, (char *)&rr);
529 if (ret || sizeof(rr) != length) {
89291a9d 530 JFFS2_WARNING("jffs2_flash_write() returned %d, request=%zu, retlen=%zu, at %#08x\n",
aa98d7cf
KK
531 ret, sizeof(rr), length, phys_ofs);
532 ret = ret ? ret : -EIO;
2f785402
DW
533 if (length)
534 jffs2_add_physical_node_ref(c, phys_ofs | REF_OBSOLETE, PAD(sizeof(rr)), NULL);
535
aa98d7cf
KK
536 return ret;
537 }
c9f700f8
KK
538 /* success */
539 ref->xseqno = xseqno;
540 jffs2_add_physical_node_ref(c, phys_ofs | REF_PRISTINE, PAD(sizeof(rr)), (void *)ref);
aa98d7cf
KK
541
542 dbg_xattr("success on saving xref (ino=%u, xid=%u)\n", ref->ic->ino, ref->xd->xid);
543
544 return 0;
545}
546
547static struct jffs2_xattr_ref *create_xattr_ref(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic,
9fe4854c 548 struct jffs2_xattr_datum *xd)
aa98d7cf
KK
549{
550 /* must be called under down_write(xattr_sem) */
551 struct jffs2_xattr_ref *ref;
552 int ret;
553
554 ref = jffs2_alloc_xattr_ref();
555 if (!ref)
556 return ERR_PTR(-ENOMEM);
557 ref->ic = ic;
558 ref->xd = xd;
559
9fe4854c 560 ret = save_xattr_ref(c, ref);
aa98d7cf
KK
561 if (ret) {
562 jffs2_free_xattr_ref(ref);
563 return ERR_PTR(ret);
564 }
565
566 /* Chain to inode */
8f2b6f49
KK
567 ref->next = ic->xref;
568 ic->xref = ref;
aa98d7cf
KK
569
570 return ref; /* success */
571}
572
8a13695c 573static void delete_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
c9f700f8
KK
574{
575 /* must be called under down_write(xattr_sem) */
576 struct jffs2_xattr_datum *xd;
577
c9f700f8 578 xd = ref->xd;
8a13695c 579 ref->xseqno |= XREF_DELETE_MARKER;
c9f700f8
KK
580 ref->ino = ref->ic->ino;
581 ref->xid = ref->xd->xid;
582 spin_lock(&c->erase_completion_lock);
583 ref->next = c->xref_dead_list;
584 c->xref_dead_list = ref;
585 spin_unlock(&c->erase_completion_lock);
586
8a13695c
KK
587 dbg_xattr("xref(ino=%u, xid=%u, xseqno=%u) was removed.\n",
588 ref->ino, ref->xid, ref->xseqno);
c9f700f8 589
c6e8c6cc 590 unrefer_xattr_datum(c, xd);
c9f700f8
KK
591}
592
aa98d7cf
KK
593void jffs2_xattr_delete_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
594{
b57922d9 595 /* It's called from jffs2_evict_inode() on inode removing.
aa98d7cf 596 When an inode with XATTR is removed, those XATTRs must be removed. */
8a13695c 597 struct jffs2_xattr_ref *ref, *_ref;
aa98d7cf 598
27c72b04 599 if (!ic || ic->pino_nlink > 0)
aa98d7cf
KK
600 return;
601
602 down_write(&c->xattr_sem);
8a13695c
KK
603 for (ref = ic->xref; ref; ref = _ref) {
604 _ref = ref->next;
605 delete_xattr_ref(c, ref);
8f2b6f49 606 }
8a13695c 607 ic->xref = NULL;
aa98d7cf
KK
608 up_write(&c->xattr_sem);
609}
610
611void jffs2_xattr_free_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
612{
613 /* It's called from jffs2_free_ino_caches() until unmounting FS. */
614 struct jffs2_xattr_datum *xd;
615 struct jffs2_xattr_ref *ref, *_ref;
616
617 down_write(&c->xattr_sem);
8f2b6f49
KK
618 for (ref = ic->xref; ref; ref = _ref) {
619 _ref = ref->next;
aa98d7cf 620 xd = ref->xd;
2c887e23 621 if (atomic_dec_and_test(&xd->refcnt)) {
aa98d7cf
KK
622 unload_xattr_datum(c, xd);
623 jffs2_free_xattr_datum(xd);
624 }
625 jffs2_free_xattr_ref(ref);
626 }
8f2b6f49 627 ic->xref = NULL;
aa98d7cf
KK
628 up_write(&c->xattr_sem);
629}
630
8f2b6f49 631static int check_xattr_ref_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
aa98d7cf 632{
a4ce96ac 633 /* success of check_xattr_ref_inode() means that inode (ic) dose not have
aa98d7cf
KK
634 * duplicate name/value pairs. If duplicate name/value pair would be found,
635 * one will be removed.
636 */
c9f700f8 637 struct jffs2_xattr_ref *ref, *cmp, **pref, **pcmp;
aa98d7cf
KK
638 int rc = 0;
639
640 if (likely(ic->flags & INO_FLAGS_XATTR_CHECKED))
641 return 0;
642 down_write(&c->xattr_sem);
643 retry:
644 rc = 0;
8f2b6f49 645 for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
aa98d7cf
KK
646 if (!ref->xd->xname) {
647 rc = load_xattr_datum(c, ref->xd);
648 if (unlikely(rc > 0)) {
8f2b6f49 649 *pref = ref->next;
8a13695c 650 delete_xattr_ref(c, ref);
aa98d7cf
KK
651 goto retry;
652 } else if (unlikely(rc < 0))
653 goto out;
654 }
c9f700f8 655 for (cmp=ref->next, pcmp=&ref->next; cmp; pcmp=&cmp->next, cmp=cmp->next) {
aa98d7cf
KK
656 if (!cmp->xd->xname) {
657 ref->xd->flags |= JFFS2_XFLAGS_BIND;
658 rc = load_xattr_datum(c, cmp->xd);
659 ref->xd->flags &= ~JFFS2_XFLAGS_BIND;
660 if (unlikely(rc > 0)) {
c9f700f8 661 *pcmp = cmp->next;
8a13695c 662 delete_xattr_ref(c, cmp);
aa98d7cf
KK
663 goto retry;
664 } else if (unlikely(rc < 0))
665 goto out;
666 }
667 if (ref->xd->xprefix == cmp->xd->xprefix
668 && !strcmp(ref->xd->xname, cmp->xd->xname)) {
c9f700f8
KK
669 if (ref->xseqno > cmp->xseqno) {
670 *pcmp = cmp->next;
8a13695c 671 delete_xattr_ref(c, cmp);
c9f700f8
KK
672 } else {
673 *pref = ref->next;
8a13695c 674 delete_xattr_ref(c, ref);
c9f700f8 675 }
aa98d7cf
KK
676 goto retry;
677 }
678 }
679 }
680 ic->flags |= INO_FLAGS_XATTR_CHECKED;
681 out:
682 up_write(&c->xattr_sem);
683
684 return rc;
685}
686
687/* -------- xattr subsystem functions ---------------
688 * jffs2_init_xattr_subsystem(c)
689 * is used to initialize semaphore and list_head, and some variables.
690 * jffs2_find_xattr_datum(c, xid)
691 * is used to lookup xdatum while scanning process.
692 * jffs2_clear_xattr_subsystem(c)
693 * is used to release any xattr related objects.
694 * jffs2_build_xattr_subsystem(c)
695 * is used to associate xdatum and xref while super block building process.
696 * jffs2_setup_xattr_datum(c, xid, version)
697 * is used to insert xdatum while scanning process.
698 * -------------------------------------------------- */
699void jffs2_init_xattr_subsystem(struct jffs2_sb_info *c)
700{
701 int i;
702
703 for (i=0; i < XATTRINDEX_HASHSIZE; i++)
704 INIT_LIST_HEAD(&c->xattrindex[i]);
aa98d7cf 705 INIT_LIST_HEAD(&c->xattr_unchecked);
c9f700f8
KK
706 INIT_LIST_HEAD(&c->xattr_dead_list);
707 c->xref_dead_list = NULL;
8f2b6f49 708 c->xref_temp = NULL;
aa98d7cf
KK
709
710 init_rwsem(&c->xattr_sem);
c9f700f8
KK
711 c->highest_xid = 0;
712 c->highest_xseqno = 0;
aa98d7cf
KK
713 c->xdatum_mem_usage = 0;
714 c->xdatum_mem_threshold = 32 * 1024; /* Default 32KB */
715}
716
717static struct jffs2_xattr_datum *jffs2_find_xattr_datum(struct jffs2_sb_info *c, uint32_t xid)
718{
719 struct jffs2_xattr_datum *xd;
720 int i = xid % XATTRINDEX_HASHSIZE;
721
722 /* It's only used in scanning/building process. */
723 BUG_ON(!(c->flags & (JFFS2_SB_FLAG_SCANNING|JFFS2_SB_FLAG_BUILDING)));
724
725 list_for_each_entry(xd, &c->xattrindex[i], xindex) {
726 if (xd->xid==xid)
727 return xd;
728 }
729 return NULL;
730}
731
732void jffs2_clear_xattr_subsystem(struct jffs2_sb_info *c)
733{
734 struct jffs2_xattr_datum *xd, *_xd;
735 struct jffs2_xattr_ref *ref, *_ref;
736 int i;
737
8f2b6f49
KK
738 for (ref=c->xref_temp; ref; ref = _ref) {
739 _ref = ref->next;
aa98d7cf 740 jffs2_free_xattr_ref(ref);
8f2b6f49 741 }
c9f700f8
KK
742
743 for (ref=c->xref_dead_list; ref; ref = _ref) {
744 _ref = ref->next;
745 jffs2_free_xattr_ref(ref);
746 }
aa98d7cf
KK
747
748 for (i=0; i < XATTRINDEX_HASHSIZE; i++) {
749 list_for_each_entry_safe(xd, _xd, &c->xattrindex[i], xindex) {
750 list_del(&xd->xindex);
751 if (xd->xname)
752 kfree(xd->xname);
753 jffs2_free_xattr_datum(xd);
754 }
755 }
c9f700f8
KK
756
757 list_for_each_entry_safe(xd, _xd, &c->xattr_dead_list, xindex) {
758 list_del(&xd->xindex);
759 jffs2_free_xattr_datum(xd);
760 }
2ad8ee71
DW
761 list_for_each_entry_safe(xd, _xd, &c->xattr_unchecked, xindex) {
762 list_del(&xd->xindex);
763 jffs2_free_xattr_datum(xd);
764 }
aa98d7cf
KK
765}
766
c9f700f8 767#define XREF_TMPHASH_SIZE (128)
aa98d7cf
KK
768void jffs2_build_xattr_subsystem(struct jffs2_sb_info *c)
769{
770 struct jffs2_xattr_ref *ref, *_ref;
c9f700f8 771 struct jffs2_xattr_ref *xref_tmphash[XREF_TMPHASH_SIZE];
aa98d7cf
KK
772 struct jffs2_xattr_datum *xd, *_xd;
773 struct jffs2_inode_cache *ic;
c9f700f8
KK
774 struct jffs2_raw_node_ref *raw;
775 int i, xdatum_count = 0, xdatum_unchecked_count = 0, xref_count = 0;
8a13695c 776 int xdatum_orphan_count = 0, xref_orphan_count = 0, xref_dead_count = 0;
aa98d7cf
KK
777
778 BUG_ON(!(c->flags & JFFS2_SB_FLAG_BUILDING));
779
8a13695c 780 /* Phase.1 : Merge same xref */
c9f700f8
KK
781 for (i=0; i < XREF_TMPHASH_SIZE; i++)
782 xref_tmphash[i] = NULL;
8f2b6f49 783 for (ref=c->xref_temp; ref; ref=_ref) {
c9f700f8
KK
784 struct jffs2_xattr_ref *tmp;
785
8f2b6f49 786 _ref = ref->next;
aa98d7cf
KK
787 if (ref_flags(ref->node) != REF_PRISTINE) {
788 if (verify_xattr_ref(c, ref)) {
c9f700f8
KK
789 BUG_ON(ref->node->next_in_ino != (void *)ref);
790 ref->node->next_in_ino = NULL;
791 jffs2_mark_node_obsolete(c, ref->node);
aa98d7cf
KK
792 jffs2_free_xattr_ref(ref);
793 continue;
794 }
795 }
c9f700f8
KK
796
797 i = (ref->ino ^ ref->xid) % XREF_TMPHASH_SIZE;
798 for (tmp=xref_tmphash[i]; tmp; tmp=tmp->next) {
799 if (tmp->ino == ref->ino && tmp->xid == ref->xid)
800 break;
801 }
802 if (tmp) {
803 raw = ref->node;
804 if (ref->xseqno > tmp->xseqno) {
805 tmp->xseqno = ref->xseqno;
806 raw->next_in_ino = tmp->node;
807 tmp->node = raw;
808 } else {
809 raw->next_in_ino = tmp->node->next_in_ino;
810 tmp->node->next_in_ino = raw;
811 }
aa98d7cf
KK
812 jffs2_free_xattr_ref(ref);
813 continue;
c9f700f8
KK
814 } else {
815 ref->next = xref_tmphash[i];
816 xref_tmphash[i] = ref;
aa98d7cf 817 }
aa98d7cf 818 }
8f2b6f49 819 c->xref_temp = NULL;
aa98d7cf 820
8a13695c 821 /* Phase.2 : Bind xref with inode_cache and xattr_datum */
c9f700f8
KK
822 for (i=0; i < XREF_TMPHASH_SIZE; i++) {
823 for (ref=xref_tmphash[i]; ref; ref=_ref) {
8a13695c 824 xref_count++;
c9f700f8
KK
825 _ref = ref->next;
826 if (is_xattr_ref_dead(ref)) {
827 ref->next = c->xref_dead_list;
828 c->xref_dead_list = ref;
8a13695c 829 xref_dead_count++;
c9f700f8
KK
830 continue;
831 }
832 /* At this point, ref->xid and ref->ino contain XID and inode number.
833 ref->xd and ref->ic are not valid yet. */
834 xd = jffs2_find_xattr_datum(c, ref->xid);
835 ic = jffs2_get_ino_cache(c, ref->ino);
27c72b04 836 if (!xd || !ic || !ic->pino_nlink) {
8a13695c
KK
837 dbg_xattr("xref(ino=%u, xid=%u, xseqno=%u) is orphan.\n",
838 ref->ino, ref->xid, ref->xseqno);
839 ref->xseqno |= XREF_DELETE_MARKER;
c9f700f8
KK
840 ref->next = c->xref_dead_list;
841 c->xref_dead_list = ref;
8a13695c 842 xref_orphan_count++;
c9f700f8
KK
843 continue;
844 }
845 ref->xd = xd;
846 ref->ic = ic;
2c887e23 847 atomic_inc(&xd->refcnt);
c9f700f8
KK
848 ref->next = ic->xref;
849 ic->xref = ref;
c9f700f8
KK
850 }
851 }
852
8a13695c 853 /* Phase.3 : Link unchecked xdatum to xattr_unchecked list */
aa98d7cf
KK
854 for (i=0; i < XATTRINDEX_HASHSIZE; i++) {
855 list_for_each_entry_safe(xd, _xd, &c->xattrindex[i], xindex) {
8a13695c 856 xdatum_count++;
aa98d7cf 857 list_del_init(&xd->xindex);
2c887e23 858 if (!atomic_read(&xd->refcnt)) {
8a13695c
KK
859 dbg_xattr("xdatum(xid=%u, version=%u) is orphan.\n",
860 xd->xid, xd->version);
861 xd->flags |= JFFS2_XFLAGS_DEAD;
c9f700f8 862 list_add(&xd->xindex, &c->xattr_unchecked);
8a13695c 863 xdatum_orphan_count++;
aa98d7cf
KK
864 continue;
865 }
c9f700f8
KK
866 if (is_xattr_datum_unchecked(c, xd)) {
867 dbg_xattr("unchecked xdatum(xid=%u, version=%u)\n",
868 xd->xid, xd->version);
aa98d7cf
KK
869 list_add(&xd->xindex, &c->xattr_unchecked);
870 xdatum_unchecked_count++;
871 }
aa98d7cf
KK
872 }
873 }
874 /* build complete */
8a13695c
KK
875 JFFS2_NOTICE("complete building xattr subsystem, %u of xdatum"
876 " (%u unchecked, %u orphan) and "
877 "%u of xref (%u dead, %u orphan) found.\n",
878 xdatum_count, xdatum_unchecked_count, xdatum_orphan_count,
879 xref_count, xref_dead_count, xref_orphan_count);
aa98d7cf
KK
880}
881
882struct jffs2_xattr_datum *jffs2_setup_xattr_datum(struct jffs2_sb_info *c,
883 uint32_t xid, uint32_t version)
884{
c9f700f8 885 struct jffs2_xattr_datum *xd;
aa98d7cf 886
c9f700f8
KK
887 xd = jffs2_find_xattr_datum(c, xid);
888 if (!xd) {
889 xd = jffs2_alloc_xattr_datum();
890 if (!xd)
891 return ERR_PTR(-ENOMEM);
892 xd->xid = xid;
893 xd->version = version;
894 if (xd->xid > c->highest_xid)
895 c->highest_xid = xd->xid;
896 list_add_tail(&xd->xindex, &c->xattrindex[xid % XATTRINDEX_HASHSIZE]);
aa98d7cf
KK
897 }
898 return xd;
899}
900
901/* -------- xattr subsystem functions ---------------
902 * xprefix_to_handler(xprefix)
903 * is used to translate xprefix into xattr_handler.
904 * jffs2_listxattr(dentry, buffer, size)
905 * is an implementation of listxattr handler on jffs2.
906 * do_jffs2_getxattr(inode, xprefix, xname, buffer, size)
907 * is an implementation of getxattr handler on jffs2.
908 * do_jffs2_setxattr(inode, xprefix, xname, buffer, size, flags)
909 * is an implementation of setxattr handler on jffs2.
910 * -------------------------------------------------- */
365f0cb9 911const struct xattr_handler *jffs2_xattr_handlers[] = {
aa98d7cf
KK
912 &jffs2_user_xattr_handler,
913#ifdef CONFIG_JFFS2_FS_SECURITY
914 &jffs2_security_xattr_handler,
915#endif
916#ifdef CONFIG_JFFS2_FS_POSIX_ACL
917 &jffs2_acl_access_xattr_handler,
918 &jffs2_acl_default_xattr_handler,
919#endif
920 &jffs2_trusted_xattr_handler,
921 NULL
922};
923
365f0cb9
SH
924static const struct xattr_handler *xprefix_to_handler(int xprefix) {
925 const struct xattr_handler *ret;
aa98d7cf
KK
926
927 switch (xprefix) {
928 case JFFS2_XPREFIX_USER:
929 ret = &jffs2_user_xattr_handler;
930 break;
931#ifdef CONFIG_JFFS2_FS_SECURITY
932 case JFFS2_XPREFIX_SECURITY:
933 ret = &jffs2_security_xattr_handler;
934 break;
935#endif
936#ifdef CONFIG_JFFS2_FS_POSIX_ACL
937 case JFFS2_XPREFIX_ACL_ACCESS:
938 ret = &jffs2_acl_access_xattr_handler;
939 break;
940 case JFFS2_XPREFIX_ACL_DEFAULT:
941 ret = &jffs2_acl_default_xattr_handler;
942 break;
943#endif
944 case JFFS2_XPREFIX_TRUSTED:
945 ret = &jffs2_trusted_xattr_handler;
946 break;
947 default:
948 ret = NULL;
949 break;
950 }
951 return ret;
952}
953
954ssize_t jffs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
955{
956 struct inode *inode = dentry->d_inode;
957 struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
958 struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
959 struct jffs2_inode_cache *ic = f->inocache;
8f2b6f49 960 struct jffs2_xattr_ref *ref, **pref;
aa98d7cf 961 struct jffs2_xattr_datum *xd;
365f0cb9 962 const struct xattr_handler *xhandle;
aa98d7cf
KK
963 ssize_t len, rc;
964 int retry = 0;
965
8f2b6f49 966 rc = check_xattr_ref_inode(c, ic);
aa98d7cf
KK
967 if (unlikely(rc))
968 return rc;
969
970 down_read(&c->xattr_sem);
971 retry:
972 len = 0;
8f2b6f49 973 for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
aa98d7cf
KK
974 BUG_ON(ref->ic != ic);
975 xd = ref->xd;
976 if (!xd->xname) {
977 /* xdatum is unchached */
978 if (!retry) {
979 retry = 1;
980 up_read(&c->xattr_sem);
981 down_write(&c->xattr_sem);
982 goto retry;
983 } else {
984 rc = load_xattr_datum(c, xd);
985 if (unlikely(rc > 0)) {
8f2b6f49 986 *pref = ref->next;
8a13695c 987 delete_xattr_ref(c, ref);
aa98d7cf
KK
988 goto retry;
989 } else if (unlikely(rc < 0))
990 goto out;
991 }
992 }
993 xhandle = xprefix_to_handler(xd->xprefix);
994 if (!xhandle)
995 continue;
996 if (buffer) {
431547b3
CH
997 rc = xhandle->list(dentry, buffer+len, size-len,
998 xd->xname, xd->name_len, xd->flags);
aa98d7cf 999 } else {
431547b3
CH
1000 rc = xhandle->list(dentry, NULL, 0, xd->xname,
1001 xd->name_len, xd->flags);
aa98d7cf
KK
1002 }
1003 if (rc < 0)
1004 goto out;
1005 len += rc;
1006 }
1007 rc = len;
1008 out:
1009 if (!retry) {
1010 up_read(&c->xattr_sem);
1011 } else {
1012 up_write(&c->xattr_sem);
1013 }
1014 return rc;
1015}
1016
1017int do_jffs2_getxattr(struct inode *inode, int xprefix, const char *xname,
1018 char *buffer, size_t size)
1019{
1020 struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
1021 struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
1022 struct jffs2_inode_cache *ic = f->inocache;
1023 struct jffs2_xattr_datum *xd;
8f2b6f49 1024 struct jffs2_xattr_ref *ref, **pref;
aa98d7cf
KK
1025 int rc, retry = 0;
1026
8f2b6f49 1027 rc = check_xattr_ref_inode(c, ic);
aa98d7cf
KK
1028 if (unlikely(rc))
1029 return rc;
1030
1031 down_read(&c->xattr_sem);
1032 retry:
8f2b6f49 1033 for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
aa98d7cf
KK
1034 BUG_ON(ref->ic!=ic);
1035
1036 xd = ref->xd;
1037 if (xd->xprefix != xprefix)
1038 continue;
1039 if (!xd->xname) {
1040 /* xdatum is unchached */
1041 if (!retry) {
1042 retry = 1;
1043 up_read(&c->xattr_sem);
1044 down_write(&c->xattr_sem);
1045 goto retry;
1046 } else {
1047 rc = load_xattr_datum(c, xd);
1048 if (unlikely(rc > 0)) {
8f2b6f49 1049 *pref = ref->next;
8a13695c 1050 delete_xattr_ref(c, ref);
aa98d7cf
KK
1051 goto retry;
1052 } else if (unlikely(rc < 0)) {
1053 goto out;
1054 }
1055 }
1056 }
1057 if (!strcmp(xname, xd->xname)) {
1058 rc = xd->value_len;
1059 if (buffer) {
1060 if (size < rc) {
1061 rc = -ERANGE;
1062 } else {
1063 memcpy(buffer, xd->xvalue, rc);
1064 }
1065 }
1066 goto out;
1067 }
1068 }
1069 rc = -ENODATA;
1070 out:
1071 if (!retry) {
1072 up_read(&c->xattr_sem);
1073 } else {
1074 up_write(&c->xattr_sem);
1075 }
1076 return rc;
1077}
1078
1079int do_jffs2_setxattr(struct inode *inode, int xprefix, const char *xname,
1080 const char *buffer, size_t size, int flags)
1081{
1082 struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
1083 struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
1084 struct jffs2_inode_cache *ic = f->inocache;
1085 struct jffs2_xattr_datum *xd;
8f2b6f49 1086 struct jffs2_xattr_ref *ref, *newref, **pref;
9fe4854c 1087 uint32_t length, request;
aa98d7cf
KK
1088 int rc;
1089
8f2b6f49 1090 rc = check_xattr_ref_inode(c, ic);
aa98d7cf
KK
1091 if (unlikely(rc))
1092 return rc;
1093
1094 request = PAD(sizeof(struct jffs2_raw_xattr) + strlen(xname) + 1 + size);
9fe4854c 1095 rc = jffs2_reserve_space(c, request, &length,
aa98d7cf
KK
1096 ALLOC_NORMAL, JFFS2_SUMMARY_XATTR_SIZE);
1097 if (rc) {
1098 JFFS2_WARNING("jffs2_reserve_space()=%d, request=%u\n", rc, request);
1099 return rc;
1100 }
1101
1102 /* Find existing xattr */
1103 down_write(&c->xattr_sem);
1104 retry:
8f2b6f49 1105 for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
aa98d7cf
KK
1106 xd = ref->xd;
1107 if (xd->xprefix != xprefix)
1108 continue;
1109 if (!xd->xname) {
1110 rc = load_xattr_datum(c, xd);
1111 if (unlikely(rc > 0)) {
8f2b6f49 1112 *pref = ref->next;
8a13695c 1113 delete_xattr_ref(c, ref);
aa98d7cf
KK
1114 goto retry;
1115 } else if (unlikely(rc < 0))
1116 goto out;
1117 }
1118 if (!strcmp(xd->xname, xname)) {
1119 if (flags & XATTR_CREATE) {
1120 rc = -EEXIST;
1121 goto out;
1122 }
1123 if (!buffer) {
8a13695c
KK
1124 ref->ino = ic->ino;
1125 ref->xid = xd->xid;
1126 ref->xseqno |= XREF_DELETE_MARKER;
1127 rc = save_xattr_ref(c, ref);
1128 if (!rc) {
1129 *pref = ref->next;
1130 spin_lock(&c->erase_completion_lock);
1131 ref->next = c->xref_dead_list;
1132 c->xref_dead_list = ref;
1133 spin_unlock(&c->erase_completion_lock);
c6e8c6cc 1134 unrefer_xattr_datum(c, xd);
8a13695c
KK
1135 } else {
1136 ref->ic = ic;
1137 ref->xd = xd;
1138 ref->xseqno &= ~XREF_DELETE_MARKER;
1139 }
aa98d7cf
KK
1140 goto out;
1141 }
1142 goto found;
1143 }
1144 }
1145 /* not found */
aa98d7cf
KK
1146 if (flags & XATTR_REPLACE) {
1147 rc = -ENODATA;
1148 goto out;
1149 }
1150 if (!buffer) {
c9f700f8 1151 rc = -ENODATA;
aa98d7cf
KK
1152 goto out;
1153 }
1154 found:
9fe4854c 1155 xd = create_xattr_datum(c, xprefix, xname, buffer, size);
aa98d7cf
KK
1156 if (IS_ERR(xd)) {
1157 rc = PTR_ERR(xd);
1158 goto out;
1159 }
1160 up_write(&c->xattr_sem);
1161 jffs2_complete_reservation(c);
1162
1163 /* create xattr_ref */
1164 request = PAD(sizeof(struct jffs2_raw_xref));
9fe4854c 1165 rc = jffs2_reserve_space(c, request, &length,
aa98d7cf 1166 ALLOC_NORMAL, JFFS2_SUMMARY_XREF_SIZE);
c9f700f8 1167 down_write(&c->xattr_sem);
aa98d7cf
KK
1168 if (rc) {
1169 JFFS2_WARNING("jffs2_reserve_space()=%d, request=%u\n", rc, request);
c6e8c6cc 1170 unrefer_xattr_datum(c, xd);
aa98d7cf
KK
1171 up_write(&c->xattr_sem);
1172 return rc;
1173 }
8f2b6f49
KK
1174 if (ref)
1175 *pref = ref->next;
9fe4854c 1176 newref = create_xattr_ref(c, ic, xd);
aa98d7cf 1177 if (IS_ERR(newref)) {
8f2b6f49
KK
1178 if (ref) {
1179 ref->next = ic->xref;
1180 ic->xref = ref;
1181 }
aa98d7cf 1182 rc = PTR_ERR(newref);
c6e8c6cc 1183 unrefer_xattr_datum(c, xd);
aa98d7cf 1184 } else if (ref) {
8a13695c 1185 delete_xattr_ref(c, ref);
aa98d7cf
KK
1186 }
1187 out:
1188 up_write(&c->xattr_sem);
1189 jffs2_complete_reservation(c);
1190 return rc;
1191}
1192
1193/* -------- garbage collector functions -------------
c9f700f8 1194 * jffs2_garbage_collect_xattr_datum(c, xd, raw)
aa98d7cf 1195 * is used to move xdatum into new node.
c9f700f8 1196 * jffs2_garbage_collect_xattr_ref(c, ref, raw)
aa98d7cf 1197 * is used to move xref into new node.
aa98d7cf
KK
1198 * jffs2_verify_xattr(c)
1199 * is used to call do_verify_xattr_datum() before garbage collecting.
8a13695c
KK
1200 * jffs2_release_xattr_datum(c, xd)
1201 * is used to release an in-memory object of xdatum.
1202 * jffs2_release_xattr_ref(c, ref)
1203 * is used to release an in-memory object of xref.
aa98d7cf 1204 * -------------------------------------------------- */
c9f700f8
KK
1205int jffs2_garbage_collect_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd,
1206 struct jffs2_raw_node_ref *raw)
aa98d7cf 1207{
9fe4854c 1208 uint32_t totlen, length, old_ofs;
c9f700f8 1209 int rc = 0;
aa98d7cf 1210
084702e0 1211 down_write(&c->xattr_sem);
c9f700f8
KK
1212 if (xd->node != raw)
1213 goto out;
8a13695c 1214 if (xd->flags & (JFFS2_XFLAGS_DEAD|JFFS2_XFLAGS_INVALID))
c9f700f8 1215 goto out;
aa98d7cf 1216
8a13695c
KK
1217 rc = load_xattr_datum(c, xd);
1218 if (unlikely(rc)) {
1219 rc = (rc > 0) ? 0 : rc;
1220 goto out;
aa98d7cf 1221 }
8a13695c
KK
1222 old_ofs = ref_offset(xd->node);
1223 totlen = PAD(sizeof(struct jffs2_raw_xattr)
1224 + xd->name_len + 1 + xd->value_len);
9fe4854c 1225 rc = jffs2_reserve_space_gc(c, totlen, &length, JFFS2_SUMMARY_XATTR_SIZE);
c9f700f8
KK
1226 if (rc) {
1227 JFFS2_WARNING("jffs2_reserve_space_gc()=%d, request=%u\n", rc, totlen);
084702e0 1228 goto out;
aa98d7cf 1229 }
9fe4854c 1230 rc = save_xattr_datum(c, xd);
aa98d7cf
KK
1231 if (!rc)
1232 dbg_xattr("xdatum (xid=%u, version=%u) GC'ed from %#08x to %08x\n",
1233 xd->xid, xd->version, old_ofs, ref_offset(xd->node));
084702e0 1234 out:
c9f700f8
KK
1235 if (!rc)
1236 jffs2_mark_node_obsolete(c, raw);
084702e0 1237 up_write(&c->xattr_sem);
aa98d7cf
KK
1238 return rc;
1239}
1240
c9f700f8
KK
1241int jffs2_garbage_collect_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref,
1242 struct jffs2_raw_node_ref *raw)
aa98d7cf 1243{
9fe4854c 1244 uint32_t totlen, length, old_ofs;
c9f700f8 1245 int rc = 0;
aa98d7cf 1246
084702e0 1247 down_write(&c->xattr_sem);
aa98d7cf
KK
1248 BUG_ON(!ref->node);
1249
c9f700f8
KK
1250 if (ref->node != raw)
1251 goto out;
1252 if (is_xattr_ref_dead(ref) && (raw->next_in_ino == (void *)ref))
1253 goto out;
1254
aa98d7cf
KK
1255 old_ofs = ref_offset(ref->node);
1256 totlen = ref_totlen(c, c->gcblock, ref->node);
084702e0 1257
9fe4854c 1258 rc = jffs2_reserve_space_gc(c, totlen, &length, JFFS2_SUMMARY_XREF_SIZE);
c9f700f8
KK
1259 if (rc) {
1260 JFFS2_WARNING("%s: jffs2_reserve_space_gc() = %d, request = %u\n",
8e24eea7 1261 __func__, rc, totlen);
084702e0
KK
1262 rc = rc ? rc : -EBADFD;
1263 goto out;
aa98d7cf 1264 }
9fe4854c 1265 rc = save_xattr_ref(c, ref);
aa98d7cf
KK
1266 if (!rc)
1267 dbg_xattr("xref (ino=%u, xid=%u) GC'ed from %#08x to %08x\n",
1268 ref->ic->ino, ref->xd->xid, old_ofs, ref_offset(ref->node));
084702e0 1269 out:
c9f700f8
KK
1270 if (!rc)
1271 jffs2_mark_node_obsolete(c, raw);
084702e0 1272 up_write(&c->xattr_sem);
aa98d7cf
KK
1273 return rc;
1274}
1275
aa98d7cf
KK
1276int jffs2_verify_xattr(struct jffs2_sb_info *c)
1277{
1278 struct jffs2_xattr_datum *xd, *_xd;
c9f700f8
KK
1279 struct jffs2_eraseblock *jeb;
1280 struct jffs2_raw_node_ref *raw;
1281 uint32_t totlen;
aa98d7cf
KK
1282 int rc;
1283
1284 down_write(&c->xattr_sem);
1285 list_for_each_entry_safe(xd, _xd, &c->xattr_unchecked, xindex) {
1286 rc = do_verify_xattr_datum(c, xd);
c9f700f8
KK
1287 if (rc < 0)
1288 continue;
1289 list_del_init(&xd->xindex);
1290 spin_lock(&c->erase_completion_lock);
1291 for (raw=xd->node; raw != (void *)xd; raw=raw->next_in_ino) {
1292 if (ref_flags(raw) != REF_UNCHECKED)
1293 continue;
1294 jeb = &c->blocks[ref_offset(raw) / c->sector_size];
1295 totlen = PAD(ref_totlen(c, jeb, raw));
1296 c->unchecked_size -= totlen; c->used_size += totlen;
1297 jeb->unchecked_size -= totlen; jeb->used_size += totlen;
1298 raw->flash_offset = ref_offset(raw)
1299 | ((xd->node == (void *)raw) ? REF_PRISTINE : REF_NORMAL);
aa98d7cf 1300 }
8a13695c 1301 if (xd->flags & JFFS2_XFLAGS_DEAD)
c9f700f8
KK
1302 list_add(&xd->xindex, &c->xattr_dead_list);
1303 spin_unlock(&c->erase_completion_lock);
aa98d7cf
KK
1304 }
1305 up_write(&c->xattr_sem);
aa98d7cf
KK
1306 return list_empty(&c->xattr_unchecked) ? 1 : 0;
1307}
c9f700f8
KK
1308
1309void jffs2_release_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
1310{
1311 /* must be called under spin_lock(&c->erase_completion_lock) */
2c887e23 1312 if (atomic_read(&xd->refcnt) || xd->node != (void *)xd)
c9f700f8
KK
1313 return;
1314
1315 list_del(&xd->xindex);
1316 jffs2_free_xattr_datum(xd);
1317}
1318
1319void jffs2_release_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
1320{
1321 /* must be called under spin_lock(&c->erase_completion_lock) */
1322 struct jffs2_xattr_ref *tmp, **ptmp;
1323
1324 if (ref->node != (void *)ref)
1325 return;
1326
1327 for (tmp=c->xref_dead_list, ptmp=&c->xref_dead_list; tmp; ptmp=&tmp->next, tmp=tmp->next) {
1328 if (ref == tmp) {
1329 *ptmp = tmp->next;
c9f700f8
KK
1330 break;
1331 }
1332 }
8a13695c 1333 jffs2_free_xattr_ref(ref);
c9f700f8 1334}
This page took 0.49444 seconds and 5 git commands to generate.