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