]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - fs/jffs2/xattr.c
Merge branch 'next' into for-linus
[mirror_ubuntu-artful-kernel.git] / fs / jffs2 / xattr.c
1 /*
2 * JFFS2 -- Journalling Flash File System, Version 2.
3 *
4 * Copyright © 2006 NEC Corporation
5 *
6 * Created by KaiGai Kohei <kaigai@ak.jp.nec.com>
7 *
8 * For licensing information, see the file 'LICENCE' in this directory.
9 *
10 */
11
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
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).
29 * is_xattr_datum_unchecked(c, xd)
30 * returns 1, if xdatum contains any unchecked raw nodes. if all raw nodes are not
31 * unchecked, it returns 0.
32 * unload_xattr_datum(c, xd)
33 * is used to release xattr name/value pair and detach from c->xattrindex.
34 * reclaim_xattr_datum(c)
35 * is used to reclaim xattr name/value pairs on the xattr name/value pair cache when
36 * memory usage by cache is over c->xdatum_mem_threshold. Currently, this threshold
37 * is hard coded as 32KiB.
38 * do_verify_xattr_datum(c, xd)
39 * is used to load the xdatum informations without name/value pair from the medium.
40 * It's necessary once, because those informations are not collected during mounting
41 * process when EBS is enabled.
42 * 0 will be returned, if success. An negative return value means recoverable error, and
43 * positive return value means unrecoverable error. Thus, caller must remove this xdatum
44 * and xref when it returned positive value.
45 * do_load_xattr_datum(c, xd)
46 * is used to load name/value pair from the medium.
47 * The meanings of return value is same as do_verify_xattr_datum().
48 * load_xattr_datum(c, xd)
49 * is used to be as a wrapper of do_verify_xattr_datum() and do_load_xattr_datum().
50 * If xd need to call do_verify_xattr_datum() at first, it's called before calling
51 * do_load_xattr_datum(). The meanings of return value is same as do_verify_xattr_datum().
52 * save_xattr_datum(c, xd)
53 * is used to write xdatum to medium. xd->version will be incremented.
54 * create_xattr_datum(c, xprefix, xname, xvalue, xsize)
55 * is used to create new xdatum and write to medium.
56 * unrefer_xattr_datum(c, xd)
57 * is used to delete a xdatum. When nobody refers this xdatum, JFFS2_XFLAGS_DEAD
58 * is set on xd->flags and chained xattr_dead_list or release it immediately.
59 * In the first case, the garbage collector release it later.
60 * -------------------------------------------------- */
61 static uint32_t xattr_datum_hashkey(int xprefix, const char *xname, const char *xvalue, int xsize)
62 {
63 int name_len = strlen(xname);
64
65 return crc32(xprefix, xname, name_len) ^ crc32(xprefix, xvalue, xsize);
66 }
67
68 static 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
84 static void unload_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
85 {
86 /* must be called under down_write(xattr_sem) */
87 D1(dbg_xattr("%s: xid=%u, version=%u\n", __func__, xd->xid, xd->version));
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
99 static 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
129 static 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;
133 struct jffs2_raw_node_ref *raw;
134 struct jffs2_raw_xattr rx;
135 size_t readlen;
136 uint32_t crc, offset, totlen;
137 int rc;
138
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);
144
145 rc = jffs2_flash_read(c, offset, sizeof(rx), &readlen, (char *)&rx);
146 if (rc || readlen != sizeof(rx)) {
147 JFFS2_WARNING("jffs2_flash_read()=%d, req=%zu, read=%zu at %#08x\n",
148 rc, sizeof(rx), readlen, offset);
149 return rc ? rc : -EIO;
150 }
151 crc = crc32(0, &rx, sizeof(rx) - 4);
152 if (crc != je32_to_cpu(rx.node_crc)) {
153 JFFS2_ERROR("node CRC failed at %#08x, read=%#08x, calc=%#08x\n",
154 offset, je32_to_cpu(rx.hdr_crc), crc);
155 xd->flags |= JFFS2_XFLAGS_INVALID;
156 return -EIO;
157 }
158 totlen = PAD(sizeof(rx) + rx.name_len + 1 + je16_to_cpu(rx.value_len));
159 if (je16_to_cpu(rx.magic) != JFFS2_MAGIC_BITMASK
160 || je16_to_cpu(rx.nodetype) != JFFS2_NODETYPE_XATTR
161 || je32_to_cpu(rx.totlen) != totlen
162 || je32_to_cpu(rx.xid) != xd->xid
163 || je32_to_cpu(rx.version) != xd->version) {
164 JFFS2_ERROR("inconsistent xdatum at %#08x, magic=%#04x/%#04x, "
165 "nodetype=%#04x/%#04x, totlen=%u/%u, xid=%u/%u, version=%u/%u\n",
166 offset, je16_to_cpu(rx.magic), JFFS2_MAGIC_BITMASK,
167 je16_to_cpu(rx.nodetype), JFFS2_NODETYPE_XATTR,
168 je32_to_cpu(rx.totlen), totlen,
169 je32_to_cpu(rx.xid), xd->xid,
170 je32_to_cpu(rx.version), xd->version);
171 xd->flags |= JFFS2_XFLAGS_INVALID;
172 return -EIO;
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
179 spin_lock(&c->erase_completion_lock);
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 }
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
201 static 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
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) {
221 JFFS2_WARNING("jffs2_flash_read() returned %d, request=%d, readlen=%zu, at %#08x\n",
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);
234 xd->flags |= JFFS2_XFLAGS_INVALID;
235 return -EIO;
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
260 static 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;
268
269 BUG_ON(xd->flags & JFFS2_XFLAGS_DEAD);
270 if (xd->xname)
271 return 0;
272 if (xd->flags & JFFS2_XFLAGS_INVALID)
273 return -EIO;
274 if (unlikely(is_xattr_datum_unchecked(c, xd)))
275 rc = do_verify_xattr_datum(c, xd);
276 if (!rc)
277 rc = do_load_xattr_datum(c, xd);
278 return rc;
279 }
280
281 static int save_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
282 {
283 /* must be called under down_write(xattr_sem) */
284 struct jffs2_raw_xattr rx;
285 struct kvec vecs[2];
286 size_t length;
287 int rc, totlen;
288 uint32_t phys_ofs = write_ofs(c);
289
290 BUG_ON(!xd->xname);
291 BUG_ON(xd->flags & (JFFS2_XFLAGS_DEAD|JFFS2_XFLAGS_INVALID));
292
293 vecs[0].iov_base = &rx;
294 vecs[0].iov_len = sizeof(rx);
295 vecs[1].iov_base = xd->xname;
296 vecs[1].iov_len = xd->name_len + 1 + xd->value_len;
297 totlen = vecs[0].iov_len + vecs[1].iov_len;
298
299 /* Setup raw-xattr */
300 memset(&rx, 0, sizeof(rx));
301 rx.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
302 rx.nodetype = cpu_to_je16(JFFS2_NODETYPE_XATTR);
303 rx.totlen = cpu_to_je32(PAD(totlen));
304 rx.hdr_crc = cpu_to_je32(crc32(0, &rx, sizeof(struct jffs2_unknown_node) - 4));
305
306 rx.xid = cpu_to_je32(xd->xid);
307 rx.version = cpu_to_je32(++xd->version);
308 rx.xprefix = xd->xprefix;
309 rx.name_len = xd->name_len;
310 rx.value_len = cpu_to_je16(xd->value_len);
311 rx.data_crc = cpu_to_je32(crc32(0, vecs[1].iov_base, vecs[1].iov_len));
312 rx.node_crc = cpu_to_je32(crc32(0, &rx, sizeof(struct jffs2_raw_xattr) - 4));
313
314 rc = jffs2_flash_writev(c, vecs, 2, phys_ofs, &length, 0);
315 if (rc || totlen != length) {
316 JFFS2_WARNING("jffs2_flash_writev()=%d, req=%u, wrote=%zu, at %#08x\n",
317 rc, totlen, length, phys_ofs);
318 rc = rc ? rc : -EIO;
319 if (length)
320 jffs2_add_physical_node_ref(c, phys_ofs | REF_OBSOLETE, PAD(totlen), NULL);
321
322 return rc;
323 }
324 /* success */
325 jffs2_add_physical_node_ref(c, phys_ofs | REF_PRISTINE, PAD(totlen), (void *)xd);
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
333 static struct jffs2_xattr_datum *create_xattr_datum(struct jffs2_sb_info *c,
334 int xprefix, const char *xname,
335 const char *xvalue, int xsize)
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)) {
352 atomic_inc(&xd->refcnt);
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
372 atomic_set(&xd->refcnt, 1);
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
384 rc = save_xattr_datum(c, xd);
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
401 static void unrefer_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
402 {
403 /* must be called under down_write(xattr_sem) */
404 if (atomic_dec_and_lock(&xd->refcnt, &c->erase_completion_lock)) {
405 unload_xattr_datum(c, xd);
406 xd->flags |= JFFS2_XFLAGS_DEAD;
407 if (xd->node == (void *)xd) {
408 BUG_ON(!(xd->flags & JFFS2_XFLAGS_INVALID));
409 jffs2_free_xattr_datum(xd);
410 } else {
411 list_add(&xd->xindex, &c->xattr_dead_list);
412 }
413 spin_unlock(&c->erase_completion_lock);
414
415 dbg_xattr("xdatum(xid=%u, version=%u) was removed.\n",
416 xd->xid, xd->version);
417 }
418 }
419
420 /* -------- xref related functions ------------------
421 * verify_xattr_ref(c, ref)
422 * is used to load xref information from medium. Because summary data does not
423 * contain xid/ino, it's necessary to verify once while mounting process.
424 * save_xattr_ref(c, ref)
425 * is used to write xref to medium. If delete marker is marked, it write
426 * a delete marker of xref into medium.
427 * create_xattr_ref(c, ic, xd)
428 * is used to create a new xref and write to medium.
429 * delete_xattr_ref(c, ref)
430 * is used to delete jffs2_xattr_ref. It marks xref XREF_DELETE_MARKER,
431 * and allows GC to reclaim those physical nodes.
432 * jffs2_xattr_delete_inode(c, ic)
433 * is called to remove xrefs related to obsolete inode when inode is unlinked.
434 * jffs2_xattr_free_inode(c, ic)
435 * is called to release xattr related objects when unmounting.
436 * check_xattr_ref_inode(c, ic)
437 * is used to confirm inode does not have duplicate xattr name/value pair.
438 * -------------------------------------------------- */
439 static int verify_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
440 {
441 struct jffs2_eraseblock *jeb;
442 struct jffs2_raw_node_ref *raw;
443 struct jffs2_raw_xref rr;
444 size_t readlen;
445 uint32_t crc, offset, totlen;
446 int rc;
447
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);
453
454 rc = jffs2_flash_read(c, offset, sizeof(rr), &readlen, (char *)&rr);
455 if (rc || sizeof(rr) != readlen) {
456 JFFS2_WARNING("jffs2_flash_read()=%d, req=%zu, read=%zu, at %#08x\n",
457 rc, sizeof(rr), readlen, offset);
458 return rc ? rc : -EIO;
459 }
460 /* obsolete node */
461 crc = crc32(0, &rr, sizeof(rr) - 4);
462 if (crc != je32_to_cpu(rr.node_crc)) {
463 JFFS2_ERROR("node CRC failed at %#08x, read=%#08x, calc=%#08x\n",
464 offset, je32_to_cpu(rr.node_crc), crc);
465 return -EIO;
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, "
471 "nodetype=%#04x/%#04x, totlen=%u/%zu\n",
472 offset, je16_to_cpu(rr.magic), JFFS2_MAGIC_BITMASK,
473 je16_to_cpu(rr.nodetype), JFFS2_NODETYPE_XREF,
474 je32_to_cpu(rr.totlen), PAD(sizeof(rr)));
475 return -EIO;
476 }
477 ref->ino = je32_to_cpu(rr.ino);
478 ref->xid = je32_to_cpu(rr.xid);
479 ref->xseqno = je32_to_cpu(rr.xseqno);
480 if (ref->xseqno > c->highest_xseqno)
481 c->highest_xseqno = (ref->xseqno & ~XREF_DELETE_MARKER);
482
483 spin_lock(&c->erase_completion_lock);
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 }
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
501 static int save_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
502 {
503 /* must be called under down_write(xattr_sem) */
504 struct jffs2_raw_xref rr;
505 size_t length;
506 uint32_t xseqno, phys_ofs = write_ofs(c);
507 int ret;
508
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
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);
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) {
528 JFFS2_WARNING("jffs2_flash_write() returned %d, request=%zu, retlen=%zu, at %#08x\n",
529 ret, sizeof(rr), length, phys_ofs);
530 ret = ret ? ret : -EIO;
531 if (length)
532 jffs2_add_physical_node_ref(c, phys_ofs | REF_OBSOLETE, PAD(sizeof(rr)), NULL);
533
534 return ret;
535 }
536 /* success */
537 ref->xseqno = xseqno;
538 jffs2_add_physical_node_ref(c, phys_ofs | REF_PRISTINE, PAD(sizeof(rr)), (void *)ref);
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
545 static struct jffs2_xattr_ref *create_xattr_ref(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic,
546 struct jffs2_xattr_datum *xd)
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
558 ret = save_xattr_ref(c, ref);
559 if (ret) {
560 jffs2_free_xattr_ref(ref);
561 return ERR_PTR(ret);
562 }
563
564 /* Chain to inode */
565 ref->next = ic->xref;
566 ic->xref = ref;
567
568 return ref; /* success */
569 }
570
571 static void delete_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
572 {
573 /* must be called under down_write(xattr_sem) */
574 struct jffs2_xattr_datum *xd;
575
576 xd = ref->xd;
577 ref->xseqno |= XREF_DELETE_MARKER;
578 ref->ino = ref->ic->ino;
579 ref->xid = ref->xd->xid;
580 spin_lock(&c->erase_completion_lock);
581 ref->next = c->xref_dead_list;
582 c->xref_dead_list = ref;
583 spin_unlock(&c->erase_completion_lock);
584
585 dbg_xattr("xref(ino=%u, xid=%u, xseqno=%u) was removed.\n",
586 ref->ino, ref->xid, ref->xseqno);
587
588 unrefer_xattr_datum(c, xd);
589 }
590
591 void jffs2_xattr_delete_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
592 {
593 /* It's called from jffs2_evict_inode() on inode removing.
594 When an inode with XATTR is removed, those XATTRs must be removed. */
595 struct jffs2_xattr_ref *ref, *_ref;
596
597 if (!ic || ic->pino_nlink > 0)
598 return;
599
600 down_write(&c->xattr_sem);
601 for (ref = ic->xref; ref; ref = _ref) {
602 _ref = ref->next;
603 delete_xattr_ref(c, ref);
604 }
605 ic->xref = NULL;
606 up_write(&c->xattr_sem);
607 }
608
609 void 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);
616 for (ref = ic->xref; ref; ref = _ref) {
617 _ref = ref->next;
618 xd = ref->xd;
619 if (atomic_dec_and_test(&xd->refcnt)) {
620 unload_xattr_datum(c, xd);
621 jffs2_free_xattr_datum(xd);
622 }
623 jffs2_free_xattr_ref(ref);
624 }
625 ic->xref = NULL;
626 up_write(&c->xattr_sem);
627 }
628
629 static int check_xattr_ref_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
630 {
631 /* success of check_xattr_ref_inode() means that inode (ic) dose not have
632 * duplicate name/value pairs. If duplicate name/value pair would be found,
633 * one will be removed.
634 */
635 struct jffs2_xattr_ref *ref, *cmp, **pref, **pcmp;
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;
643 for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
644 if (!ref->xd->xname) {
645 rc = load_xattr_datum(c, ref->xd);
646 if (unlikely(rc > 0)) {
647 *pref = ref->next;
648 delete_xattr_ref(c, ref);
649 goto retry;
650 } else if (unlikely(rc < 0))
651 goto out;
652 }
653 for (cmp=ref->next, pcmp=&ref->next; cmp; pcmp=&cmp->next, cmp=cmp->next) {
654 if (!cmp->xd->xname) {
655 ref->xd->flags |= JFFS2_XFLAGS_BIND;
656 rc = load_xattr_datum(c, cmp->xd);
657 ref->xd->flags &= ~JFFS2_XFLAGS_BIND;
658 if (unlikely(rc > 0)) {
659 *pcmp = cmp->next;
660 delete_xattr_ref(c, cmp);
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)) {
667 if (ref->xseqno > cmp->xseqno) {
668 *pcmp = cmp->next;
669 delete_xattr_ref(c, cmp);
670 } else {
671 *pref = ref->next;
672 delete_xattr_ref(c, ref);
673 }
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 * -------------------------------------------------- */
697 void 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]);
703 INIT_LIST_HEAD(&c->xattr_unchecked);
704 INIT_LIST_HEAD(&c->xattr_dead_list);
705 c->xref_dead_list = NULL;
706 c->xref_temp = NULL;
707
708 init_rwsem(&c->xattr_sem);
709 c->highest_xid = 0;
710 c->highest_xseqno = 0;
711 c->xdatum_mem_usage = 0;
712 c->xdatum_mem_threshold = 32 * 1024; /* Default 32KB */
713 }
714
715 static 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
730 void 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
736 for (ref=c->xref_temp; ref; ref = _ref) {
737 _ref = ref->next;
738 jffs2_free_xattr_ref(ref);
739 }
740
741 for (ref=c->xref_dead_list; ref; ref = _ref) {
742 _ref = ref->next;
743 jffs2_free_xattr_ref(ref);
744 }
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 }
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 }
759 list_for_each_entry_safe(xd, _xd, &c->xattr_unchecked, xindex) {
760 list_del(&xd->xindex);
761 jffs2_free_xattr_datum(xd);
762 }
763 }
764
765 #define XREF_TMPHASH_SIZE (128)
766 void jffs2_build_xattr_subsystem(struct jffs2_sb_info *c)
767 {
768 struct jffs2_xattr_ref *ref, *_ref;
769 struct jffs2_xattr_ref *xref_tmphash[XREF_TMPHASH_SIZE];
770 struct jffs2_xattr_datum *xd, *_xd;
771 struct jffs2_inode_cache *ic;
772 struct jffs2_raw_node_ref *raw;
773 int i, xdatum_count = 0, xdatum_unchecked_count = 0, xref_count = 0;
774 int xdatum_orphan_count = 0, xref_orphan_count = 0, xref_dead_count = 0;
775
776 BUG_ON(!(c->flags & JFFS2_SB_FLAG_BUILDING));
777
778 /* Phase.1 : Merge same xref */
779 for (i=0; i < XREF_TMPHASH_SIZE; i++)
780 xref_tmphash[i] = NULL;
781 for (ref=c->xref_temp; ref; ref=_ref) {
782 struct jffs2_xattr_ref *tmp;
783
784 _ref = ref->next;
785 if (ref_flags(ref->node) != REF_PRISTINE) {
786 if (verify_xattr_ref(c, ref)) {
787 BUG_ON(ref->node->next_in_ino != (void *)ref);
788 ref->node->next_in_ino = NULL;
789 jffs2_mark_node_obsolete(c, ref->node);
790 jffs2_free_xattr_ref(ref);
791 continue;
792 }
793 }
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 }
810 jffs2_free_xattr_ref(ref);
811 continue;
812 } else {
813 ref->next = xref_tmphash[i];
814 xref_tmphash[i] = ref;
815 }
816 }
817 c->xref_temp = NULL;
818
819 /* Phase.2 : Bind xref with inode_cache and xattr_datum */
820 for (i=0; i < XREF_TMPHASH_SIZE; i++) {
821 for (ref=xref_tmphash[i]; ref; ref=_ref) {
822 xref_count++;
823 _ref = ref->next;
824 if (is_xattr_ref_dead(ref)) {
825 ref->next = c->xref_dead_list;
826 c->xref_dead_list = ref;
827 xref_dead_count++;
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);
834 if (!xd || !ic || !ic->pino_nlink) {
835 dbg_xattr("xref(ino=%u, xid=%u, xseqno=%u) is orphan.\n",
836 ref->ino, ref->xid, ref->xseqno);
837 ref->xseqno |= XREF_DELETE_MARKER;
838 ref->next = c->xref_dead_list;
839 c->xref_dead_list = ref;
840 xref_orphan_count++;
841 continue;
842 }
843 ref->xd = xd;
844 ref->ic = ic;
845 atomic_inc(&xd->refcnt);
846 ref->next = ic->xref;
847 ic->xref = ref;
848 }
849 }
850
851 /* Phase.3 : Link unchecked xdatum to xattr_unchecked list */
852 for (i=0; i < XATTRINDEX_HASHSIZE; i++) {
853 list_for_each_entry_safe(xd, _xd, &c->xattrindex[i], xindex) {
854 xdatum_count++;
855 list_del_init(&xd->xindex);
856 if (!atomic_read(&xd->refcnt)) {
857 dbg_xattr("xdatum(xid=%u, version=%u) is orphan.\n",
858 xd->xid, xd->version);
859 xd->flags |= JFFS2_XFLAGS_DEAD;
860 list_add(&xd->xindex, &c->xattr_unchecked);
861 xdatum_orphan_count++;
862 continue;
863 }
864 if (is_xattr_datum_unchecked(c, xd)) {
865 dbg_xattr("unchecked xdatum(xid=%u, version=%u)\n",
866 xd->xid, xd->version);
867 list_add(&xd->xindex, &c->xattr_unchecked);
868 xdatum_unchecked_count++;
869 }
870 }
871 }
872 /* build complete */
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);
878 }
879
880 struct jffs2_xattr_datum *jffs2_setup_xattr_datum(struct jffs2_sb_info *c,
881 uint32_t xid, uint32_t version)
882 {
883 struct jffs2_xattr_datum *xd;
884
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]);
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 * -------------------------------------------------- */
909 const struct xattr_handler *jffs2_xattr_handlers[] = {
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
922 static const struct xattr_handler *xprefix_to_handler(int xprefix) {
923 const struct xattr_handler *ret;
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
952 ssize_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;
958 struct jffs2_xattr_ref *ref, **pref;
959 struct jffs2_xattr_datum *xd;
960 const struct xattr_handler *xhandle;
961 ssize_t len, rc;
962 int retry = 0;
963
964 rc = check_xattr_ref_inode(c, ic);
965 if (unlikely(rc))
966 return rc;
967
968 down_read(&c->xattr_sem);
969 retry:
970 len = 0;
971 for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
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)) {
984 *pref = ref->next;
985 delete_xattr_ref(c, ref);
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) {
995 rc = xhandle->list(dentry, buffer+len, size-len,
996 xd->xname, xd->name_len, xd->flags);
997 } else {
998 rc = xhandle->list(dentry, NULL, 0, xd->xname,
999 xd->name_len, xd->flags);
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
1015 int 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;
1022 struct jffs2_xattr_ref *ref, **pref;
1023 int rc, retry = 0;
1024
1025 rc = check_xattr_ref_inode(c, ic);
1026 if (unlikely(rc))
1027 return rc;
1028
1029 down_read(&c->xattr_sem);
1030 retry:
1031 for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
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)) {
1047 *pref = ref->next;
1048 delete_xattr_ref(c, ref);
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
1077 int 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;
1084 struct jffs2_xattr_ref *ref, *newref, **pref;
1085 uint32_t length, request;
1086 int rc;
1087
1088 rc = check_xattr_ref_inode(c, ic);
1089 if (unlikely(rc))
1090 return rc;
1091
1092 request = PAD(sizeof(struct jffs2_raw_xattr) + strlen(xname) + 1 + size);
1093 rc = jffs2_reserve_space(c, request, &length,
1094 ALLOC_NORMAL, JFFS2_SUMMARY_XATTR_SIZE);
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:
1103 for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
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)) {
1110 *pref = ref->next;
1111 delete_xattr_ref(c, ref);
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) {
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);
1132 unrefer_xattr_datum(c, xd);
1133 } else {
1134 ref->ic = ic;
1135 ref->xd = xd;
1136 ref->xseqno &= ~XREF_DELETE_MARKER;
1137 }
1138 goto out;
1139 }
1140 goto found;
1141 }
1142 }
1143 /* not found */
1144 if (flags & XATTR_REPLACE) {
1145 rc = -ENODATA;
1146 goto out;
1147 }
1148 if (!buffer) {
1149 rc = -ENODATA;
1150 goto out;
1151 }
1152 found:
1153 xd = create_xattr_datum(c, xprefix, xname, buffer, size);
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));
1163 rc = jffs2_reserve_space(c, request, &length,
1164 ALLOC_NORMAL, JFFS2_SUMMARY_XREF_SIZE);
1165 down_write(&c->xattr_sem);
1166 if (rc) {
1167 JFFS2_WARNING("jffs2_reserve_space()=%d, request=%u\n", rc, request);
1168 unrefer_xattr_datum(c, xd);
1169 up_write(&c->xattr_sem);
1170 return rc;
1171 }
1172 if (ref)
1173 *pref = ref->next;
1174 newref = create_xattr_ref(c, ic, xd);
1175 if (IS_ERR(newref)) {
1176 if (ref) {
1177 ref->next = ic->xref;
1178 ic->xref = ref;
1179 }
1180 rc = PTR_ERR(newref);
1181 unrefer_xattr_datum(c, xd);
1182 } else if (ref) {
1183 delete_xattr_ref(c, ref);
1184 }
1185 out:
1186 up_write(&c->xattr_sem);
1187 jffs2_complete_reservation(c);
1188 return rc;
1189 }
1190
1191 /* -------- garbage collector functions -------------
1192 * jffs2_garbage_collect_xattr_datum(c, xd, raw)
1193 * is used to move xdatum into new node.
1194 * jffs2_garbage_collect_xattr_ref(c, ref, raw)
1195 * is used to move xref into new node.
1196 * jffs2_verify_xattr(c)
1197 * is used to call do_verify_xattr_datum() before garbage collecting.
1198 * jffs2_release_xattr_datum(c, xd)
1199 * is used to release an in-memory object of xdatum.
1200 * jffs2_release_xattr_ref(c, ref)
1201 * is used to release an in-memory object of xref.
1202 * -------------------------------------------------- */
1203 int jffs2_garbage_collect_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd,
1204 struct jffs2_raw_node_ref *raw)
1205 {
1206 uint32_t totlen, length, old_ofs;
1207 int rc = 0;
1208
1209 down_write(&c->xattr_sem);
1210 if (xd->node != raw)
1211 goto out;
1212 if (xd->flags & (JFFS2_XFLAGS_DEAD|JFFS2_XFLAGS_INVALID))
1213 goto out;
1214
1215 rc = load_xattr_datum(c, xd);
1216 if (unlikely(rc)) {
1217 rc = (rc > 0) ? 0 : rc;
1218 goto out;
1219 }
1220 old_ofs = ref_offset(xd->node);
1221 totlen = PAD(sizeof(struct jffs2_raw_xattr)
1222 + xd->name_len + 1 + xd->value_len);
1223 rc = jffs2_reserve_space_gc(c, totlen, &length, JFFS2_SUMMARY_XATTR_SIZE);
1224 if (rc) {
1225 JFFS2_WARNING("jffs2_reserve_space_gc()=%d, request=%u\n", rc, totlen);
1226 goto out;
1227 }
1228 rc = save_xattr_datum(c, xd);
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));
1232 out:
1233 if (!rc)
1234 jffs2_mark_node_obsolete(c, raw);
1235 up_write(&c->xattr_sem);
1236 return rc;
1237 }
1238
1239 int jffs2_garbage_collect_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref,
1240 struct jffs2_raw_node_ref *raw)
1241 {
1242 uint32_t totlen, length, old_ofs;
1243 int rc = 0;
1244
1245 down_write(&c->xattr_sem);
1246 BUG_ON(!ref->node);
1247
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
1253 old_ofs = ref_offset(ref->node);
1254 totlen = ref_totlen(c, c->gcblock, ref->node);
1255
1256 rc = jffs2_reserve_space_gc(c, totlen, &length, JFFS2_SUMMARY_XREF_SIZE);
1257 if (rc) {
1258 JFFS2_WARNING("%s: jffs2_reserve_space_gc() = %d, request = %u\n",
1259 __func__, rc, totlen);
1260 rc = rc ? rc : -EBADFD;
1261 goto out;
1262 }
1263 rc = save_xattr_ref(c, ref);
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));
1267 out:
1268 if (!rc)
1269 jffs2_mark_node_obsolete(c, raw);
1270 up_write(&c->xattr_sem);
1271 return rc;
1272 }
1273
1274 int jffs2_verify_xattr(struct jffs2_sb_info *c)
1275 {
1276 struct jffs2_xattr_datum *xd, *_xd;
1277 struct jffs2_eraseblock *jeb;
1278 struct jffs2_raw_node_ref *raw;
1279 uint32_t totlen;
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);
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);
1298 }
1299 if (xd->flags & JFFS2_XFLAGS_DEAD)
1300 list_add(&xd->xindex, &c->xattr_dead_list);
1301 spin_unlock(&c->erase_completion_lock);
1302 }
1303 up_write(&c->xattr_sem);
1304 return list_empty(&c->xattr_unchecked) ? 1 : 0;
1305 }
1306
1307 void 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) */
1310 if (atomic_read(&xd->refcnt) || xd->node != (void *)xd)
1311 return;
1312
1313 list_del(&xd->xindex);
1314 jffs2_free_xattr_datum(xd);
1315 }
1316
1317 void 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;
1328 break;
1329 }
1330 }
1331 jffs2_free_xattr_ref(ref);
1332 }