blob: b55b803eddcb92081908aa1f53da50cbdfc6babb [file] [log] [blame]
KaiGai Kohei652ecc22006-05-13 15:18:27 +09001/*
2 * JFFS2 -- Journalling Flash File System, Version 2.
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09003 *
David Woodhousec00c3102007-04-25 14:16:47 +01004 * Copyright © 2006 NEC Corporation
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09005 *
KaiGai Kohei652ecc22006-05-13 15:18:27 +09006 * Created by KaiGai Kohei <kaigai@ak.jp.nec.com>
7 *
8 * For licensing information, see the file 'LICENCE' in this directory.
9 *
10 */
David Woodhousec00c3102007-04-25 14:16:47 +010011
Joe Perches9bbf29e2012-02-15 15:56:46 -080012#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +090014#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).
KaiGai Koheic9f700f2006-06-11 10:35:15 +090029 * 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.
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +090032 * 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
Tracey Dentbea93122011-02-26 11:15:13 -050036 * memory usage by cache is over c->xdatum_mem_threshold. Currently, this threshold
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +090037 * is hard coded as 32KiB.
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +090038 * 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().
David Woodhouse9fe48542006-05-23 00:38:06 +010052 * save_xattr_datum(c, xd)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +090053 * is used to write xdatum to medium. xd->version will be incremented.
David Woodhouse9fe48542006-05-23 00:38:06 +010054 * create_xattr_datum(c, xprefix, xname, xvalue, xsize)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +090055 * is used to create new xdatum and write to medium.
KaiGai Koheic6e8c6c2006-06-29 15:33:02 +010056 * 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.
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +090060 * -------------------------------------------------- */
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +090061static 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
KaiGai Koheic9f700f2006-06-11 10:35:15 +090068static 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
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +090084static void unload_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
85{
86 /* must be called under down_write(xattr_sem) */
Harvey Harrison8e24eea2008-04-30 00:55:09 -070087 D1(dbg_xattr("%s: xid=%u, version=%u\n", __func__, xd->xid, xd->version));
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +090088 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
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900129static 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;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900133 struct jffs2_raw_node_ref *raw;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900134 struct jffs2_raw_xattr rx;
135 size_t readlen;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900136 uint32_t crc, offset, totlen;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900137 int rc;
138
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900139 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);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900144
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900145 rc = jffs2_flash_read(c, offset, sizeof(rx), &readlen, (char *)&rx);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900146 if (rc || readlen != sizeof(rx)) {
David Woodhouse89291a92006-05-25 13:30:24 +0100147 JFFS2_WARNING("jffs2_flash_read()=%d, req=%zu, read=%zu at %#08x\n",
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900148 rc, sizeof(rx), readlen, offset);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900149 return rc ? rc : -EIO;
150 }
151 crc = crc32(0, &rx, sizeof(rx) - 4);
152 if (crc != je32_to_cpu(rx.node_crc)) {
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900153 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;
Vasiliy Kulikovf3269662010-11-14 23:08:39 +0300156 return -EIO;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900157 }
KaiGai Kohei8a136952006-06-24 09:14:13 +0900158 totlen = PAD(sizeof(rx) + rx.name_len + 1 + je16_to_cpu(rx.value_len));
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900159 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",
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900166 offset, je16_to_cpu(rx.magic), JFFS2_MAGIC_BITMASK,
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900167 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);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900171 xd->flags |= JFFS2_XFLAGS_INVALID;
Vasiliy Kulikovf3269662010-11-14 23:08:39 +0300172 return -EIO;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900173 }
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
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900179 spin_lock(&c->erase_completion_lock);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900180 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 }
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900190 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
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900209 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) {
David Woodhouse89291a92006-05-25 13:30:24 +0100221 JFFS2_WARNING("jffs2_flash_read() returned %d, request=%d, readlen=%zu, at %#08x\n",
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900222 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);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900234 xd->flags |= JFFS2_XFLAGS_INVALID;
Vasiliy Kulikovf3269662010-11-14 23:08:39 +0300235 return -EIO;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900236 }
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;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900268
KaiGai Kohei8a136952006-06-24 09:14:13 +0900269 BUG_ON(xd->flags & JFFS2_XFLAGS_DEAD);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900270 if (xd->xname)
271 return 0;
272 if (xd->flags & JFFS2_XFLAGS_INVALID)
Vasiliy Kulikovf3269662010-11-14 23:08:39 +0300273 return -EIO;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900274 if (unlikely(is_xattr_datum_unchecked(c, xd)))
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900275 rc = do_verify_xattr_datum(c, xd);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900276 if (!rc)
277 rc = do_load_xattr_datum(c, xd);
278 return rc;
279}
280
David Woodhouse9fe48542006-05-23 00:38:06 +0100281static int save_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900282{
283 /* must be called under down_write(xattr_sem) */
David Woodhouse2f785402006-05-24 02:04:45 +0100284 struct jffs2_raw_xattr rx;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900285 struct kvec vecs[2];
David Woodhouse89291a92006-05-25 13:30:24 +0100286 size_t length;
KaiGai Kohei8a136952006-06-24 09:14:13 +0900287 int rc, totlen;
David Woodhouse9fe48542006-05-23 00:38:06 +0100288 uint32_t phys_ofs = write_ofs(c);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900289
KaiGai Kohei8a136952006-06-24 09:14:13 +0900290 BUG_ON(!xd->xname);
291 BUG_ON(xd->flags & (JFFS2_XFLAGS_DEAD|JFFS2_XFLAGS_INVALID));
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900292
293 vecs[0].iov_base = &rx;
KaiGai Kohei8a136952006-06-24 09:14:13 +0900294 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
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900299 /* Setup raw-xattr */
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900300 memset(&rx, 0, sizeof(rx));
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900301 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);
KaiGai Kohei8a136952006-06-24 09:14:13 +0900307 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));
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900312 rx.node_crc = cpu_to_je32(crc32(0, &rx, sizeof(struct jffs2_raw_xattr) - 4));
313
KaiGai Kohei8a136952006-06-24 09:14:13 +0900314 rc = jffs2_flash_writev(c, vecs, 2, phys_ofs, &length, 0);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900315 if (rc || totlen != length) {
David Woodhouse89291a92006-05-25 13:30:24 +0100316 JFFS2_WARNING("jffs2_flash_writev()=%d, req=%u, wrote=%zu, at %#08x\n",
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900317 rc, totlen, length, phys_ofs);
318 rc = rc ? rc : -EIO;
David Woodhouse2f785402006-05-24 02:04:45 +0100319 if (length)
320 jffs2_add_physical_node_ref(c, phys_ofs | REF_OBSOLETE, PAD(totlen), NULL);
321
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900322 return rc;
323 }
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900324 /* success */
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900325 jffs2_add_physical_node_ref(c, phys_ofs | REF_PRISTINE, PAD(totlen), (void *)xd);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900326
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,
David Woodhouse9fe48542006-05-23 00:38:06 +0100335 const char *xvalue, int xsize)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900336{
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)) {
KaiGai Kohei2c887e22006-06-24 09:16:50 +0900352 atomic_inc(&xd->refcnt);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900353 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
KaiGai Kohei2c887e22006-06-24 09:16:50 +0900372 atomic_set(&xd->refcnt, 1);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900373 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
David Woodhouse9fe48542006-05-23 00:38:06 +0100384 rc = save_xattr_datum(c, xd);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900385 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
KaiGai Koheic6e8c6c2006-06-29 15:33:02 +0100401static void unrefer_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900402{
403 /* must be called under down_write(xattr_sem) */
KaiGai Koheic6e8c6c2006-06-29 15:33:02 +0100404 if (atomic_dec_and_lock(&xd->refcnt, &c->erase_completion_lock)) {
KaiGai Koheic6e8c6c2006-06-29 15:33:02 +0100405 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
Jeff Garzika6b1d822006-10-04 07:57:18 -0400415 dbg_xattr("xdatum(xid=%u, version=%u) was removed.\n",
416 xd->xid, xd->version);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900417 }
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900418}
419
KaiGai Kohei21b98792006-05-13 15:22:29 +0900420/* -------- xref related functions ------------------
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900421 * 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.
David Woodhouse9fe48542006-05-23 00:38:06 +0100424 * save_xattr_ref(c, ref)
KaiGai Kohei8a136952006-06-24 09:14:13 +0900425 * is used to write xref to medium. If delete marker is marked, it write
426 * a delete marker of xref into medium.
David Woodhouse9fe48542006-05-23 00:38:06 +0100427 * create_xattr_ref(c, ic, xd)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900428 * is used to create a new xref and write to medium.
KaiGai Kohei8a136952006-06-24 09:14:13 +0900429 * 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.
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900432 * 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.
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900436 * check_xattr_ref_inode(c, ic)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900437 * 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;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900442 struct jffs2_raw_node_ref *raw;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900443 struct jffs2_raw_xref rr;
444 size_t readlen;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900445 uint32_t crc, offset, totlen;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900446 int rc;
447
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900448 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);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900453
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900454 rc = jffs2_flash_read(c, offset, sizeof(rr), &readlen, (char *)&rr);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900455 if (rc || sizeof(rr) != readlen) {
David Woodhouse89291a92006-05-25 13:30:24 +0100456 JFFS2_WARNING("jffs2_flash_read()=%d, req=%zu, read=%zu, at %#08x\n",
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900457 rc, sizeof(rr), readlen, offset);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900458 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)) {
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900463 JFFS2_ERROR("node CRC failed at %#08x, read=%#08x, calc=%#08x\n",
464 offset, je32_to_cpu(rr.node_crc), crc);
Vasiliy Kulikovf3269662010-11-14 23:08:39 +0300465 return -EIO;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900466 }
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, "
David Woodhouse89291a92006-05-25 13:30:24 +0100471 "nodetype=%#04x/%#04x, totlen=%u/%zu\n",
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900472 offset, je16_to_cpu(rr.magic), JFFS2_MAGIC_BITMASK,
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900473 je16_to_cpu(rr.nodetype), JFFS2_NODETYPE_XREF,
474 je32_to_cpu(rr.totlen), PAD(sizeof(rr)));
Vasiliy Kulikovf3269662010-11-14 23:08:39 +0300475 return -EIO;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900476 }
477 ref->ino = je32_to_cpu(rr.ino);
478 ref->xid = je32_to_cpu(rr.xid);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900479 ref->xseqno = je32_to_cpu(rr.xseqno);
480 if (ref->xseqno > c->highest_xseqno)
481 c->highest_xseqno = (ref->xseqno & ~XREF_DELETE_MARKER);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900482
483 spin_lock(&c->erase_completion_lock);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900484 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 }
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900494 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
David Woodhouse9fe48542006-05-23 00:38:06 +0100501static int save_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900502{
503 /* must be called under down_write(xattr_sem) */
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900504 struct jffs2_raw_xref rr;
David Woodhouse89291a92006-05-25 13:30:24 +0100505 size_t length;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900506 uint32_t xseqno, phys_ofs = write_ofs(c);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900507 int ret;
508
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900509 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
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900514 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);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900524 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) {
David Woodhouse89291a92006-05-25 13:30:24 +0100528 JFFS2_WARNING("jffs2_flash_write() returned %d, request=%zu, retlen=%zu, at %#08x\n",
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900529 ret, sizeof(rr), length, phys_ofs);
530 ret = ret ? ret : -EIO;
David Woodhouse2f785402006-05-24 02:04:45 +0100531 if (length)
532 jffs2_add_physical_node_ref(c, phys_ofs | REF_OBSOLETE, PAD(sizeof(rr)), NULL);
533
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900534 return ret;
535 }
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900536 /* success */
537 ref->xseqno = xseqno;
538 jffs2_add_physical_node_ref(c, phys_ofs | REF_PRISTINE, PAD(sizeof(rr)), (void *)ref);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900539
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,
David Woodhouse9fe48542006-05-23 00:38:06 +0100546 struct jffs2_xattr_datum *xd)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900547{
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
David Woodhouse9fe48542006-05-23 00:38:06 +0100558 ret = save_xattr_ref(c, ref);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900559 if (ret) {
560 jffs2_free_xattr_ref(ref);
561 return ERR_PTR(ret);
562 }
563
564 /* Chain to inode */
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900565 ref->next = ic->xref;
566 ic->xref = ref;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900567
568 return ref; /* success */
569}
570
KaiGai Kohei8a136952006-06-24 09:14:13 +0900571static void delete_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900572{
573 /* must be called under down_write(xattr_sem) */
574 struct jffs2_xattr_datum *xd;
575
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900576 xd = ref->xd;
KaiGai Kohei8a136952006-06-24 09:14:13 +0900577 ref->xseqno |= XREF_DELETE_MARKER;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900578 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
KaiGai Kohei8a136952006-06-24 09:14:13 +0900585 dbg_xattr("xref(ino=%u, xid=%u, xseqno=%u) was removed.\n",
586 ref->ino, ref->xid, ref->xseqno);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900587
KaiGai Koheic6e8c6c2006-06-29 15:33:02 +0100588 unrefer_xattr_datum(c, xd);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900589}
590
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900591void jffs2_xattr_delete_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
592{
Al Virob57922d2010-06-07 14:34:48 -0400593 /* It's called from jffs2_evict_inode() on inode removing.
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900594 When an inode with XATTR is removed, those XATTRs must be removed. */
KaiGai Kohei8a136952006-06-24 09:14:13 +0900595 struct jffs2_xattr_ref *ref, *_ref;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900596
David Woodhouse27c72b02008-05-01 18:47:17 +0100597 if (!ic || ic->pino_nlink > 0)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900598 return;
599
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900600 down_write(&c->xattr_sem);
KaiGai Kohei8a136952006-06-24 09:14:13 +0900601 for (ref = ic->xref; ref; ref = _ref) {
602 _ref = ref->next;
603 delete_xattr_ref(c, ref);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900604 }
KaiGai Kohei8a136952006-06-24 09:14:13 +0900605 ic->xref = NULL;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900606 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);
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900616 for (ref = ic->xref; ref; ref = _ref) {
617 _ref = ref->next;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900618 xd = ref->xd;
KaiGai Kohei2c887e22006-06-24 09:16:50 +0900619 if (atomic_dec_and_test(&xd->refcnt)) {
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900620 unload_xattr_datum(c, xd);
621 jffs2_free_xattr_datum(xd);
622 }
623 jffs2_free_xattr_ref(ref);
624 }
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900625 ic->xref = NULL;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900626 up_write(&c->xattr_sem);
627}
628
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900629static int check_xattr_ref_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900630{
Linus Torvaldsa4ce96a2010-07-21 09:25:42 -0700631 /* success of check_xattr_ref_inode() means that inode (ic) dose not have
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900632 * duplicate name/value pairs. If duplicate name/value pair would be found,
633 * one will be removed.
634 */
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900635 struct jffs2_xattr_ref *ref, *cmp, **pref, **pcmp;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900636 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;
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900643 for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900644 if (!ref->xd->xname) {
645 rc = load_xattr_datum(c, ref->xd);
646 if (unlikely(rc > 0)) {
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900647 *pref = ref->next;
KaiGai Kohei8a136952006-06-24 09:14:13 +0900648 delete_xattr_ref(c, ref);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900649 goto retry;
650 } else if (unlikely(rc < 0))
651 goto out;
652 }
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900653 for (cmp=ref->next, pcmp=&ref->next; cmp; pcmp=&cmp->next, cmp=cmp->next) {
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900654 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)) {
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900659 *pcmp = cmp->next;
KaiGai Kohei8a136952006-06-24 09:14:13 +0900660 delete_xattr_ref(c, cmp);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900661 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)) {
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900667 if (ref->xseqno > cmp->xseqno) {
668 *pcmp = cmp->next;
KaiGai Kohei8a136952006-06-24 09:14:13 +0900669 delete_xattr_ref(c, cmp);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900670 } else {
671 *pref = ref->next;
KaiGai Kohei8a136952006-06-24 09:14:13 +0900672 delete_xattr_ref(c, ref);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900673 }
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900674 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]);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900703 INIT_LIST_HEAD(&c->xattr_unchecked);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900704 INIT_LIST_HEAD(&c->xattr_dead_list);
705 c->xref_dead_list = NULL;
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900706 c->xref_temp = NULL;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900707
708 init_rwsem(&c->xattr_sem);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900709 c->highest_xid = 0;
710 c->highest_xseqno = 0;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900711 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
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900736 for (ref=c->xref_temp; ref; ref = _ref) {
737 _ref = ref->next;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900738 jffs2_free_xattr_ref(ref);
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900739 }
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900740
741 for (ref=c->xref_dead_list; ref; ref = _ref) {
742 _ref = ref->next;
743 jffs2_free_xattr_ref(ref);
744 }
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900745
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 }
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900754
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 }
David Woodhouse2ad8ee72007-05-08 00:12:58 +0100759 list_for_each_entry_safe(xd, _xd, &c->xattr_unchecked, xindex) {
760 list_del(&xd->xindex);
761 jffs2_free_xattr_datum(xd);
762 }
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900763}
764
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900765#define XREF_TMPHASH_SIZE (128)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900766void jffs2_build_xattr_subsystem(struct jffs2_sb_info *c)
767{
768 struct jffs2_xattr_ref *ref, *_ref;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900769 struct jffs2_xattr_ref *xref_tmphash[XREF_TMPHASH_SIZE];
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900770 struct jffs2_xattr_datum *xd, *_xd;
771 struct jffs2_inode_cache *ic;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900772 struct jffs2_raw_node_ref *raw;
773 int i, xdatum_count = 0, xdatum_unchecked_count = 0, xref_count = 0;
KaiGai Kohei8a136952006-06-24 09:14:13 +0900774 int xdatum_orphan_count = 0, xref_orphan_count = 0, xref_dead_count = 0;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900775
776 BUG_ON(!(c->flags & JFFS2_SB_FLAG_BUILDING));
777
KaiGai Kohei8a136952006-06-24 09:14:13 +0900778 /* Phase.1 : Merge same xref */
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900779 for (i=0; i < XREF_TMPHASH_SIZE; i++)
780 xref_tmphash[i] = NULL;
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900781 for (ref=c->xref_temp; ref; ref=_ref) {
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900782 struct jffs2_xattr_ref *tmp;
783
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900784 _ref = ref->next;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900785 if (ref_flags(ref->node) != REF_PRISTINE) {
786 if (verify_xattr_ref(c, ref)) {
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900787 BUG_ON(ref->node->next_in_ino != (void *)ref);
788 ref->node->next_in_ino = NULL;
789 jffs2_mark_node_obsolete(c, ref->node);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900790 jffs2_free_xattr_ref(ref);
791 continue;
792 }
793 }
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900794
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 }
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900810 jffs2_free_xattr_ref(ref);
811 continue;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900812 } else {
813 ref->next = xref_tmphash[i];
814 xref_tmphash[i] = ref;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900815 }
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900816 }
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900817 c->xref_temp = NULL;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900818
KaiGai Kohei8a136952006-06-24 09:14:13 +0900819 /* Phase.2 : Bind xref with inode_cache and xattr_datum */
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900820 for (i=0; i < XREF_TMPHASH_SIZE; i++) {
821 for (ref=xref_tmphash[i]; ref; ref=_ref) {
KaiGai Kohei8a136952006-06-24 09:14:13 +0900822 xref_count++;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900823 _ref = ref->next;
824 if (is_xattr_ref_dead(ref)) {
825 ref->next = c->xref_dead_list;
826 c->xref_dead_list = ref;
KaiGai Kohei8a136952006-06-24 09:14:13 +0900827 xref_dead_count++;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900828 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);
David Woodhouse27c72b02008-05-01 18:47:17 +0100834 if (!xd || !ic || !ic->pino_nlink) {
KaiGai Kohei8a136952006-06-24 09:14:13 +0900835 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;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900838 ref->next = c->xref_dead_list;
839 c->xref_dead_list = ref;
KaiGai Kohei8a136952006-06-24 09:14:13 +0900840 xref_orphan_count++;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900841 continue;
842 }
843 ref->xd = xd;
844 ref->ic = ic;
KaiGai Kohei2c887e22006-06-24 09:16:50 +0900845 atomic_inc(&xd->refcnt);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900846 ref->next = ic->xref;
847 ic->xref = ref;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900848 }
849 }
850
KaiGai Kohei8a136952006-06-24 09:14:13 +0900851 /* Phase.3 : Link unchecked xdatum to xattr_unchecked list */
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900852 for (i=0; i < XATTRINDEX_HASHSIZE; i++) {
853 list_for_each_entry_safe(xd, _xd, &c->xattrindex[i], xindex) {
KaiGai Kohei8a136952006-06-24 09:14:13 +0900854 xdatum_count++;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900855 list_del_init(&xd->xindex);
KaiGai Kohei2c887e22006-06-24 09:16:50 +0900856 if (!atomic_read(&xd->refcnt)) {
KaiGai Kohei8a136952006-06-24 09:14:13 +0900857 dbg_xattr("xdatum(xid=%u, version=%u) is orphan.\n",
858 xd->xid, xd->version);
859 xd->flags |= JFFS2_XFLAGS_DEAD;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900860 list_add(&xd->xindex, &c->xattr_unchecked);
KaiGai Kohei8a136952006-06-24 09:14:13 +0900861 xdatum_orphan_count++;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900862 continue;
863 }
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900864 if (is_xattr_datum_unchecked(c, xd)) {
865 dbg_xattr("unchecked xdatum(xid=%u, version=%u)\n",
866 xd->xid, xd->version);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900867 list_add(&xd->xindex, &c->xattr_unchecked);
868 xdatum_unchecked_count++;
869 }
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900870 }
871 }
872 /* build complete */
KaiGai Kohei8a136952006-06-24 09:14:13 +0900873 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);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900878}
879
880struct jffs2_xattr_datum *jffs2_setup_xattr_datum(struct jffs2_sb_info *c,
881 uint32_t xid, uint32_t version)
882{
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900883 struct jffs2_xattr_datum *xd;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900884
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900885 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]);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900895 }
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 * -------------------------------------------------- */
Stephen Hemminger365f0cb2010-05-13 17:53:21 -0700909const struct xattr_handler *jffs2_xattr_handlers[] = {
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900910 &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
Stephen Hemminger365f0cb2010-05-13 17:53:21 -0700922static const struct xattr_handler *xprefix_to_handler(int xprefix) {
923 const struct xattr_handler *ret;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900924
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;
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900958 struct jffs2_xattr_ref *ref, **pref;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900959 struct jffs2_xattr_datum *xd;
Stephen Hemminger365f0cb2010-05-13 17:53:21 -0700960 const struct xattr_handler *xhandle;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900961 ssize_t len, rc;
962 int retry = 0;
963
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900964 rc = check_xattr_ref_inode(c, ic);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900965 if (unlikely(rc))
966 return rc;
967
968 down_read(&c->xattr_sem);
969 retry:
970 len = 0;
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900971 for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900972 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)) {
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900984 *pref = ref->next;
KaiGai Kohei8a136952006-06-24 09:14:13 +0900985 delete_xattr_ref(c, ref);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900986 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) {
Christoph Hellwig431547b2009-11-13 09:52:56 +0000995 rc = xhandle->list(dentry, buffer+len, size-len,
996 xd->xname, xd->name_len, xd->flags);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900997 } else {
Christoph Hellwig431547b2009-11-13 09:52:56 +0000998 rc = xhandle->list(dentry, NULL, 0, xd->xname,
999 xd->name_len, xd->flags);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001000 }
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;
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +09001022 struct jffs2_xattr_ref *ref, **pref;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001023 int rc, retry = 0;
1024
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +09001025 rc = check_xattr_ref_inode(c, ic);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001026 if (unlikely(rc))
1027 return rc;
1028
1029 down_read(&c->xattr_sem);
1030 retry:
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +09001031 for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001032 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)) {
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +09001047 *pref = ref->next;
KaiGai Kohei8a136952006-06-24 09:14:13 +09001048 delete_xattr_ref(c, ref);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001049 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;
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +09001084 struct jffs2_xattr_ref *ref, *newref, **pref;
David Woodhouse9fe48542006-05-23 00:38:06 +01001085 uint32_t length, request;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001086 int rc;
1087
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +09001088 rc = check_xattr_ref_inode(c, ic);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001089 if (unlikely(rc))
1090 return rc;
1091
1092 request = PAD(sizeof(struct jffs2_raw_xattr) + strlen(xname) + 1 + size);
David Woodhouse9fe48542006-05-23 00:38:06 +01001093 rc = jffs2_reserve_space(c, request, &length,
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001094 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:
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +09001103 for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001104 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)) {
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +09001110 *pref = ref->next;
KaiGai Kohei8a136952006-06-24 09:14:13 +09001111 delete_xattr_ref(c, ref);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001112 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) {
KaiGai Kohei8a136952006-06-24 09:14:13 +09001122 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);
KaiGai Koheic6e8c6c2006-06-29 15:33:02 +01001132 unrefer_xattr_datum(c, xd);
KaiGai Kohei8a136952006-06-24 09:14:13 +09001133 } else {
1134 ref->ic = ic;
1135 ref->xd = xd;
1136 ref->xseqno &= ~XREF_DELETE_MARKER;
1137 }
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001138 goto out;
1139 }
1140 goto found;
1141 }
1142 }
1143 /* not found */
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001144 if (flags & XATTR_REPLACE) {
1145 rc = -ENODATA;
1146 goto out;
1147 }
1148 if (!buffer) {
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001149 rc = -ENODATA;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001150 goto out;
1151 }
1152 found:
David Woodhouse9fe48542006-05-23 00:38:06 +01001153 xd = create_xattr_datum(c, xprefix, xname, buffer, size);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001154 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));
David Woodhouse9fe48542006-05-23 00:38:06 +01001163 rc = jffs2_reserve_space(c, request, &length,
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001164 ALLOC_NORMAL, JFFS2_SUMMARY_XREF_SIZE);
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001165 down_write(&c->xattr_sem);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001166 if (rc) {
1167 JFFS2_WARNING("jffs2_reserve_space()=%d, request=%u\n", rc, request);
KaiGai Koheic6e8c6c2006-06-29 15:33:02 +01001168 unrefer_xattr_datum(c, xd);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001169 up_write(&c->xattr_sem);
1170 return rc;
1171 }
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +09001172 if (ref)
1173 *pref = ref->next;
David Woodhouse9fe48542006-05-23 00:38:06 +01001174 newref = create_xattr_ref(c, ic, xd);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001175 if (IS_ERR(newref)) {
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +09001176 if (ref) {
1177 ref->next = ic->xref;
1178 ic->xref = ref;
1179 }
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001180 rc = PTR_ERR(newref);
KaiGai Koheic6e8c6c2006-06-29 15:33:02 +01001181 unrefer_xattr_datum(c, xd);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001182 } else if (ref) {
KaiGai Kohei8a136952006-06-24 09:14:13 +09001183 delete_xattr_ref(c, ref);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001184 }
1185 out:
1186 up_write(&c->xattr_sem);
1187 jffs2_complete_reservation(c);
1188 return rc;
1189}
1190
1191/* -------- garbage collector functions -------------
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001192 * jffs2_garbage_collect_xattr_datum(c, xd, raw)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001193 * is used to move xdatum into new node.
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001194 * jffs2_garbage_collect_xattr_ref(c, ref, raw)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001195 * is used to move xref into new node.
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001196 * jffs2_verify_xattr(c)
1197 * is used to call do_verify_xattr_datum() before garbage collecting.
KaiGai Kohei8a136952006-06-24 09:14:13 +09001198 * 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.
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001202 * -------------------------------------------------- */
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001203int jffs2_garbage_collect_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd,
1204 struct jffs2_raw_node_ref *raw)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001205{
David Woodhouse9fe48542006-05-23 00:38:06 +01001206 uint32_t totlen, length, old_ofs;
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001207 int rc = 0;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001208
KaiGai Kohei084702e2006-05-13 15:16:13 +09001209 down_write(&c->xattr_sem);
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001210 if (xd->node != raw)
1211 goto out;
KaiGai Kohei8a136952006-06-24 09:14:13 +09001212 if (xd->flags & (JFFS2_XFLAGS_DEAD|JFFS2_XFLAGS_INVALID))
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001213 goto out;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001214
KaiGai Kohei8a136952006-06-24 09:14:13 +09001215 rc = load_xattr_datum(c, xd);
1216 if (unlikely(rc)) {
1217 rc = (rc > 0) ? 0 : rc;
1218 goto out;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001219 }
KaiGai Kohei8a136952006-06-24 09:14:13 +09001220 old_ofs = ref_offset(xd->node);
1221 totlen = PAD(sizeof(struct jffs2_raw_xattr)
1222 + xd->name_len + 1 + xd->value_len);
David Woodhouse9fe48542006-05-23 00:38:06 +01001223 rc = jffs2_reserve_space_gc(c, totlen, &length, JFFS2_SUMMARY_XATTR_SIZE);
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001224 if (rc) {
1225 JFFS2_WARNING("jffs2_reserve_space_gc()=%d, request=%u\n", rc, totlen);
KaiGai Kohei084702e2006-05-13 15:16:13 +09001226 goto out;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001227 }
David Woodhouse9fe48542006-05-23 00:38:06 +01001228 rc = save_xattr_datum(c, xd);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001229 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));
KaiGai Kohei084702e2006-05-13 15:16:13 +09001232 out:
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001233 if (!rc)
1234 jffs2_mark_node_obsolete(c, raw);
KaiGai Kohei084702e2006-05-13 15:16:13 +09001235 up_write(&c->xattr_sem);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001236 return rc;
1237}
1238
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001239int jffs2_garbage_collect_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref,
1240 struct jffs2_raw_node_ref *raw)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001241{
David Woodhouse9fe48542006-05-23 00:38:06 +01001242 uint32_t totlen, length, old_ofs;
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001243 int rc = 0;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001244
KaiGai Kohei084702e2006-05-13 15:16:13 +09001245 down_write(&c->xattr_sem);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001246 BUG_ON(!ref->node);
1247
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001248 if (ref->node != raw)
1249 goto out;
1250 if (is_xattr_ref_dead(ref) && (raw->next_in_ino == (void *)ref))
KaiGai Kohei084702e2006-05-13 15:16:13 +09001251 goto out;
1252
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001253 old_ofs = ref_offset(ref->node);
1254 totlen = ref_totlen(c, c->gcblock, ref->node);
1255
David Woodhouse9fe48542006-05-23 00:38:06 +01001256 rc = jffs2_reserve_space_gc(c, totlen, &length, JFFS2_SUMMARY_XREF_SIZE);
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001257 if (rc) {
1258 JFFS2_WARNING("%s: jffs2_reserve_space_gc() = %d, request = %u\n",
Harvey Harrison8e24eea2008-04-30 00:55:09 -07001259 __func__, rc, totlen);
KaiGai Kohei084702e2006-05-13 15:16:13 +09001260 rc = rc ? rc : -EBADFD;
1261 goto out;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001262 }
David Woodhouse9fe48542006-05-23 00:38:06 +01001263 rc = save_xattr_ref(c, ref);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001264 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));
KaiGai Kohei084702e2006-05-13 15:16:13 +09001267 out:
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001268 if (!rc)
1269 jffs2_mark_node_obsolete(c, raw);
KaiGai Kohei084702e2006-05-13 15:16:13 +09001270 up_write(&c->xattr_sem);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001271 return rc;
1272}
1273
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001274int jffs2_verify_xattr(struct jffs2_sb_info *c)
1275{
1276 struct jffs2_xattr_datum *xd, *_xd;
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001277 struct jffs2_eraseblock *jeb;
1278 struct jffs2_raw_node_ref *raw;
1279 uint32_t totlen;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001280 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);
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001285 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);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001298 }
KaiGai Kohei8a136952006-06-24 09:14:13 +09001299 if (xd->flags & JFFS2_XFLAGS_DEAD)
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001300 list_add(&xd->xindex, &c->xattr_dead_list);
1301 spin_unlock(&c->erase_completion_lock);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001302 }
1303 up_write(&c->xattr_sem);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001304 return list_empty(&c->xattr_unchecked) ? 1 : 0;
1305}
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001306
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) */
KaiGai Kohei2c887e22006-06-24 09:16:50 +09001310 if (atomic_read(&xd->refcnt) || xd->node != (void *)xd)
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001311 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;
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001328 break;
1329 }
1330 }
KaiGai Kohei8a136952006-06-24 09:14:13 +09001331 jffs2_free_xattr_ref(ref);
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001332}