blob: c18c0ab70ea4e2899f4a10309ea7a8e352b8123e [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
Jean-Christophe DUBOIS9824f752012-05-10 17:13:44 +020014#define JFFS2_XATTR_IS_CORRUPTED 1
15
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +090016#include <linux/kernel.h>
17#include <linux/slab.h>
18#include <linux/fs.h>
19#include <linux/time.h>
20#include <linux/pagemap.h>
21#include <linux/highmem.h>
22#include <linux/crc32.h>
23#include <linux/jffs2.h>
24#include <linux/xattr.h>
25#include <linux/mtd/mtd.h>
26#include "nodelist.h"
27/* -------- xdatum related functions ----------------
28 * xattr_datum_hashkey(xprefix, xname, xvalue, xsize)
29 * is used to calcurate xdatum hashkey. The reminder of hashkey into XATTRINDEX_HASHSIZE is
30 * the index of the xattr name/value pair cache (c->xattrindex).
KaiGai Koheic9f700f2006-06-11 10:35:15 +090031 * is_xattr_datum_unchecked(c, xd)
32 * returns 1, if xdatum contains any unchecked raw nodes. if all raw nodes are not
33 * unchecked, it returns 0.
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +090034 * unload_xattr_datum(c, xd)
35 * is used to release xattr name/value pair and detach from c->xattrindex.
36 * reclaim_xattr_datum(c)
37 * is used to reclaim xattr name/value pairs on the xattr name/value pair cache when
Tracey Dentbea93122011-02-26 11:15:13 -050038 * memory usage by cache is over c->xdatum_mem_threshold. Currently, this threshold
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +090039 * is hard coded as 32KiB.
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +090040 * do_verify_xattr_datum(c, xd)
41 * is used to load the xdatum informations without name/value pair from the medium.
42 * It's necessary once, because those informations are not collected during mounting
43 * process when EBS is enabled.
44 * 0 will be returned, if success. An negative return value means recoverable error, and
45 * positive return value means unrecoverable error. Thus, caller must remove this xdatum
46 * and xref when it returned positive value.
47 * do_load_xattr_datum(c, xd)
48 * is used to load name/value pair from the medium.
49 * The meanings of return value is same as do_verify_xattr_datum().
50 * load_xattr_datum(c, xd)
51 * is used to be as a wrapper of do_verify_xattr_datum() and do_load_xattr_datum().
52 * If xd need to call do_verify_xattr_datum() at first, it's called before calling
53 * do_load_xattr_datum(). The meanings of return value is same as do_verify_xattr_datum().
David Woodhouse9fe48542006-05-23 00:38:06 +010054 * save_xattr_datum(c, xd)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +090055 * is used to write xdatum to medium. xd->version will be incremented.
David Woodhouse9fe48542006-05-23 00:38:06 +010056 * create_xattr_datum(c, xprefix, xname, xvalue, xsize)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +090057 * is used to create new xdatum and write to medium.
KaiGai Koheic6e8c6c2006-06-29 15:33:02 +010058 * unrefer_xattr_datum(c, xd)
59 * is used to delete a xdatum. When nobody refers this xdatum, JFFS2_XFLAGS_DEAD
60 * is set on xd->flags and chained xattr_dead_list or release it immediately.
61 * In the first case, the garbage collector release it later.
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +090062 * -------------------------------------------------- */
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +090063static uint32_t xattr_datum_hashkey(int xprefix, const char *xname, const char *xvalue, int xsize)
64{
65 int name_len = strlen(xname);
66
67 return crc32(xprefix, xname, name_len) ^ crc32(xprefix, xvalue, xsize);
68}
69
KaiGai Koheic9f700f2006-06-11 10:35:15 +090070static int is_xattr_datum_unchecked(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
71{
72 struct jffs2_raw_node_ref *raw;
73 int rc = 0;
74
75 spin_lock(&c->erase_completion_lock);
76 for (raw=xd->node; raw != (void *)xd; raw=raw->next_in_ino) {
77 if (ref_flags(raw) == REF_UNCHECKED) {
78 rc = 1;
79 break;
80 }
81 }
82 spin_unlock(&c->erase_completion_lock);
83 return rc;
84}
85
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +090086static void unload_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
87{
88 /* must be called under down_write(xattr_sem) */
Harvey Harrison8e24eea2008-04-30 00:55:09 -070089 D1(dbg_xattr("%s: xid=%u, version=%u\n", __func__, xd->xid, xd->version));
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +090090 if (xd->xname) {
91 c->xdatum_mem_usage -= (xd->name_len + 1 + xd->value_len);
92 kfree(xd->xname);
93 }
94
95 list_del_init(&xd->xindex);
96 xd->hashkey = 0;
97 xd->xname = NULL;
98 xd->xvalue = NULL;
99}
100
101static void reclaim_xattr_datum(struct jffs2_sb_info *c)
102{
103 /* must be called under down_write(xattr_sem) */
104 struct jffs2_xattr_datum *xd, *_xd;
105 uint32_t target, before;
106 static int index = 0;
107 int count;
108
109 if (c->xdatum_mem_threshold > c->xdatum_mem_usage)
110 return;
111
112 before = c->xdatum_mem_usage;
113 target = c->xdatum_mem_usage * 4 / 5; /* 20% reduction */
114 for (count = 0; count < XATTRINDEX_HASHSIZE; count++) {
115 list_for_each_entry_safe(xd, _xd, &c->xattrindex[index], xindex) {
116 if (xd->flags & JFFS2_XFLAGS_HOT) {
117 xd->flags &= ~JFFS2_XFLAGS_HOT;
118 } else if (!(xd->flags & JFFS2_XFLAGS_BIND)) {
119 unload_xattr_datum(c, xd);
120 }
121 if (c->xdatum_mem_usage <= target)
122 goto out;
123 }
124 index = (index+1) % XATTRINDEX_HASHSIZE;
125 }
126 out:
127 JFFS2_NOTICE("xdatum_mem_usage from %u byte to %u byte (%u byte reclaimed)\n",
128 before, c->xdatum_mem_usage, before - c->xdatum_mem_usage);
129}
130
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900131static int do_verify_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
132{
133 /* must be called under down_write(xattr_sem) */
134 struct jffs2_eraseblock *jeb;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900135 struct jffs2_raw_node_ref *raw;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900136 struct jffs2_raw_xattr rx;
137 size_t readlen;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900138 uint32_t crc, offset, totlen;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900139 int rc;
140
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900141 spin_lock(&c->erase_completion_lock);
142 offset = ref_offset(xd->node);
143 if (ref_flags(xd->node) == REF_PRISTINE)
144 goto complete;
145 spin_unlock(&c->erase_completion_lock);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900146
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900147 rc = jffs2_flash_read(c, offset, sizeof(rx), &readlen, (char *)&rx);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900148 if (rc || readlen != sizeof(rx)) {
David Woodhouse89291a92006-05-25 13:30:24 +0100149 JFFS2_WARNING("jffs2_flash_read()=%d, req=%zu, read=%zu at %#08x\n",
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900150 rc, sizeof(rx), readlen, offset);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900151 return rc ? rc : -EIO;
152 }
153 crc = crc32(0, &rx, sizeof(rx) - 4);
154 if (crc != je32_to_cpu(rx.node_crc)) {
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900155 JFFS2_ERROR("node CRC failed at %#08x, read=%#08x, calc=%#08x\n",
156 offset, je32_to_cpu(rx.hdr_crc), crc);
157 xd->flags |= JFFS2_XFLAGS_INVALID;
Jean-Christophe DUBOIS9824f752012-05-10 17:13:44 +0200158 return JFFS2_XATTR_IS_CORRUPTED;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900159 }
KaiGai Kohei8a136952006-06-24 09:14:13 +0900160 totlen = PAD(sizeof(rx) + rx.name_len + 1 + je16_to_cpu(rx.value_len));
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900161 if (je16_to_cpu(rx.magic) != JFFS2_MAGIC_BITMASK
162 || je16_to_cpu(rx.nodetype) != JFFS2_NODETYPE_XATTR
163 || je32_to_cpu(rx.totlen) != totlen
164 || je32_to_cpu(rx.xid) != xd->xid
165 || je32_to_cpu(rx.version) != xd->version) {
166 JFFS2_ERROR("inconsistent xdatum at %#08x, magic=%#04x/%#04x, "
167 "nodetype=%#04x/%#04x, totlen=%u/%u, xid=%u/%u, version=%u/%u\n",
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900168 offset, je16_to_cpu(rx.magic), JFFS2_MAGIC_BITMASK,
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900169 je16_to_cpu(rx.nodetype), JFFS2_NODETYPE_XATTR,
170 je32_to_cpu(rx.totlen), totlen,
171 je32_to_cpu(rx.xid), xd->xid,
172 je32_to_cpu(rx.version), xd->version);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900173 xd->flags |= JFFS2_XFLAGS_INVALID;
Jean-Christophe DUBOIS9824f752012-05-10 17:13:44 +0200174 return JFFS2_XATTR_IS_CORRUPTED;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900175 }
176 xd->xprefix = rx.xprefix;
177 xd->name_len = rx.name_len;
178 xd->value_len = je16_to_cpu(rx.value_len);
179 xd->data_crc = je32_to_cpu(rx.data_crc);
180
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900181 spin_lock(&c->erase_completion_lock);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900182 complete:
183 for (raw=xd->node; raw != (void *)xd; raw=raw->next_in_ino) {
184 jeb = &c->blocks[ref_offset(raw) / c->sector_size];
185 totlen = PAD(ref_totlen(c, jeb, raw));
186 if (ref_flags(raw) == REF_UNCHECKED) {
187 c->unchecked_size -= totlen; c->used_size += totlen;
188 jeb->unchecked_size -= totlen; jeb->used_size += totlen;
189 }
190 raw->flash_offset = ref_offset(raw) | ((xd->node==raw) ? REF_PRISTINE : REF_NORMAL);
191 }
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900192 spin_unlock(&c->erase_completion_lock);
193
194 /* unchecked xdatum is chained with c->xattr_unchecked */
195 list_del_init(&xd->xindex);
196
197 dbg_xattr("success on verfying xdatum (xid=%u, version=%u)\n",
198 xd->xid, xd->version);
199
200 return 0;
201}
202
203static int do_load_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
204{
205 /* must be called under down_write(xattr_sem) */
206 char *data;
207 size_t readlen;
208 uint32_t crc, length;
209 int i, ret, retry = 0;
210
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900211 BUG_ON(ref_flags(xd->node) != REF_PRISTINE);
212 BUG_ON(!list_empty(&xd->xindex));
213 retry:
214 length = xd->name_len + 1 + xd->value_len;
215 data = kmalloc(length, GFP_KERNEL);
216 if (!data)
217 return -ENOMEM;
218
219 ret = jffs2_flash_read(c, ref_offset(xd->node)+sizeof(struct jffs2_raw_xattr),
220 length, &readlen, data);
221
222 if (ret || length!=readlen) {
David Woodhouse89291a92006-05-25 13:30:24 +0100223 JFFS2_WARNING("jffs2_flash_read() returned %d, request=%d, readlen=%zu, at %#08x\n",
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900224 ret, length, readlen, ref_offset(xd->node));
225 kfree(data);
226 return ret ? ret : -EIO;
227 }
228
229 data[xd->name_len] = '\0';
230 crc = crc32(0, data, length);
231 if (crc != xd->data_crc) {
Jean-Christophe DUBOIS9824f752012-05-10 17:13:44 +0200232 JFFS2_WARNING("node CRC failed (JFFS2_NODETYPE_XATTR)"
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900233 " at %#08x, read: 0x%08x calculated: 0x%08x\n",
234 ref_offset(xd->node), xd->data_crc, crc);
235 kfree(data);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900236 xd->flags |= JFFS2_XFLAGS_INVALID;
Jean-Christophe DUBOIS9824f752012-05-10 17:13:44 +0200237 return JFFS2_XATTR_IS_CORRUPTED;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900238 }
239
240 xd->flags |= JFFS2_XFLAGS_HOT;
241 xd->xname = data;
242 xd->xvalue = data + xd->name_len+1;
243
244 c->xdatum_mem_usage += length;
245
246 xd->hashkey = xattr_datum_hashkey(xd->xprefix, xd->xname, xd->xvalue, xd->value_len);
247 i = xd->hashkey % XATTRINDEX_HASHSIZE;
248 list_add(&xd->xindex, &c->xattrindex[i]);
249 if (!retry) {
250 retry = 1;
251 reclaim_xattr_datum(c);
252 if (!xd->xname)
253 goto retry;
254 }
255
256 dbg_xattr("success on loading xdatum (xid=%u, xprefix=%u, xname='%s')\n",
257 xd->xid, xd->xprefix, xd->xname);
258
259 return 0;
260}
261
262static int load_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
263{
264 /* must be called under down_write(xattr_sem);
265 * rc < 0 : recoverable error, try again
266 * rc = 0 : success
267 * rc > 0 : Unrecoverable error, this node should be deleted.
268 */
269 int rc = 0;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900270
KaiGai Kohei8a136952006-06-24 09:14:13 +0900271 BUG_ON(xd->flags & JFFS2_XFLAGS_DEAD);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900272 if (xd->xname)
273 return 0;
274 if (xd->flags & JFFS2_XFLAGS_INVALID)
Jean-Christophe DUBOIS9824f752012-05-10 17:13:44 +0200275 return JFFS2_XATTR_IS_CORRUPTED;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900276 if (unlikely(is_xattr_datum_unchecked(c, xd)))
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900277 rc = do_verify_xattr_datum(c, xd);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900278 if (!rc)
279 rc = do_load_xattr_datum(c, xd);
280 return rc;
281}
282
David Woodhouse9fe48542006-05-23 00:38:06 +0100283static int save_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900284{
285 /* must be called under down_write(xattr_sem) */
David Woodhouse2f785402006-05-24 02:04:45 +0100286 struct jffs2_raw_xattr rx;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900287 struct kvec vecs[2];
David Woodhouse89291a92006-05-25 13:30:24 +0100288 size_t length;
KaiGai Kohei8a136952006-06-24 09:14:13 +0900289 int rc, totlen;
David Woodhouse9fe48542006-05-23 00:38:06 +0100290 uint32_t phys_ofs = write_ofs(c);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900291
KaiGai Kohei8a136952006-06-24 09:14:13 +0900292 BUG_ON(!xd->xname);
293 BUG_ON(xd->flags & (JFFS2_XFLAGS_DEAD|JFFS2_XFLAGS_INVALID));
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900294
295 vecs[0].iov_base = &rx;
KaiGai Kohei8a136952006-06-24 09:14:13 +0900296 vecs[0].iov_len = sizeof(rx);
297 vecs[1].iov_base = xd->xname;
298 vecs[1].iov_len = xd->name_len + 1 + xd->value_len;
299 totlen = vecs[0].iov_len + vecs[1].iov_len;
300
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900301 /* Setup raw-xattr */
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900302 memset(&rx, 0, sizeof(rx));
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900303 rx.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
304 rx.nodetype = cpu_to_je16(JFFS2_NODETYPE_XATTR);
305 rx.totlen = cpu_to_je32(PAD(totlen));
306 rx.hdr_crc = cpu_to_je32(crc32(0, &rx, sizeof(struct jffs2_unknown_node) - 4));
307
308 rx.xid = cpu_to_je32(xd->xid);
KaiGai Kohei8a136952006-06-24 09:14:13 +0900309 rx.version = cpu_to_je32(++xd->version);
310 rx.xprefix = xd->xprefix;
311 rx.name_len = xd->name_len;
312 rx.value_len = cpu_to_je16(xd->value_len);
313 rx.data_crc = cpu_to_je32(crc32(0, vecs[1].iov_base, vecs[1].iov_len));
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900314 rx.node_crc = cpu_to_je32(crc32(0, &rx, sizeof(struct jffs2_raw_xattr) - 4));
315
KaiGai Kohei8a136952006-06-24 09:14:13 +0900316 rc = jffs2_flash_writev(c, vecs, 2, phys_ofs, &length, 0);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900317 if (rc || totlen != length) {
David Woodhouse89291a92006-05-25 13:30:24 +0100318 JFFS2_WARNING("jffs2_flash_writev()=%d, req=%u, wrote=%zu, at %#08x\n",
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900319 rc, totlen, length, phys_ofs);
320 rc = rc ? rc : -EIO;
David Woodhouse2f785402006-05-24 02:04:45 +0100321 if (length)
322 jffs2_add_physical_node_ref(c, phys_ofs | REF_OBSOLETE, PAD(totlen), NULL);
323
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900324 return rc;
325 }
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900326 /* success */
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900327 jffs2_add_physical_node_ref(c, phys_ofs | REF_PRISTINE, PAD(totlen), (void *)xd);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900328
329 dbg_xattr("success on saving xdatum (xid=%u, version=%u, xprefix=%u, xname='%s')\n",
330 xd->xid, xd->version, xd->xprefix, xd->xname);
331
332 return 0;
333}
334
335static struct jffs2_xattr_datum *create_xattr_datum(struct jffs2_sb_info *c,
336 int xprefix, const char *xname,
David Woodhouse9fe48542006-05-23 00:38:06 +0100337 const char *xvalue, int xsize)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900338{
339 /* must be called under down_write(xattr_sem) */
340 struct jffs2_xattr_datum *xd;
341 uint32_t hashkey, name_len;
342 char *data;
343 int i, rc;
344
345 /* Search xattr_datum has same xname/xvalue by index */
346 hashkey = xattr_datum_hashkey(xprefix, xname, xvalue, xsize);
347 i = hashkey % XATTRINDEX_HASHSIZE;
348 list_for_each_entry(xd, &c->xattrindex[i], xindex) {
349 if (xd->hashkey==hashkey
350 && xd->xprefix==xprefix
351 && xd->value_len==xsize
352 && !strcmp(xd->xname, xname)
353 && !memcmp(xd->xvalue, xvalue, xsize)) {
KaiGai Kohei2c887e22006-06-24 09:16:50 +0900354 atomic_inc(&xd->refcnt);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900355 return xd;
356 }
357 }
358
359 /* Not found, Create NEW XATTR-Cache */
360 name_len = strlen(xname);
361
362 xd = jffs2_alloc_xattr_datum();
363 if (!xd)
364 return ERR_PTR(-ENOMEM);
365
366 data = kmalloc(name_len + 1 + xsize, GFP_KERNEL);
367 if (!data) {
368 jffs2_free_xattr_datum(xd);
369 return ERR_PTR(-ENOMEM);
370 }
371 strcpy(data, xname);
372 memcpy(data + name_len + 1, xvalue, xsize);
373
KaiGai Kohei2c887e22006-06-24 09:16:50 +0900374 atomic_set(&xd->refcnt, 1);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900375 xd->xid = ++c->highest_xid;
376 xd->flags |= JFFS2_XFLAGS_HOT;
377 xd->xprefix = xprefix;
378
379 xd->hashkey = hashkey;
380 xd->xname = data;
381 xd->xvalue = data + name_len + 1;
382 xd->name_len = name_len;
383 xd->value_len = xsize;
384 xd->data_crc = crc32(0, data, xd->name_len + 1 + xd->value_len);
385
David Woodhouse9fe48542006-05-23 00:38:06 +0100386 rc = save_xattr_datum(c, xd);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900387 if (rc) {
388 kfree(xd->xname);
389 jffs2_free_xattr_datum(xd);
390 return ERR_PTR(rc);
391 }
392
393 /* Insert Hash Index */
394 i = hashkey % XATTRINDEX_HASHSIZE;
395 list_add(&xd->xindex, &c->xattrindex[i]);
396
397 c->xdatum_mem_usage += (xd->name_len + 1 + xd->value_len);
398 reclaim_xattr_datum(c);
399
400 return xd;
401}
402
KaiGai Koheic6e8c6c2006-06-29 15:33:02 +0100403static void unrefer_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900404{
405 /* must be called under down_write(xattr_sem) */
KaiGai Koheic6e8c6c2006-06-29 15:33:02 +0100406 if (atomic_dec_and_lock(&xd->refcnt, &c->erase_completion_lock)) {
KaiGai Koheic6e8c6c2006-06-29 15:33:02 +0100407 unload_xattr_datum(c, xd);
408 xd->flags |= JFFS2_XFLAGS_DEAD;
409 if (xd->node == (void *)xd) {
410 BUG_ON(!(xd->flags & JFFS2_XFLAGS_INVALID));
411 jffs2_free_xattr_datum(xd);
412 } else {
413 list_add(&xd->xindex, &c->xattr_dead_list);
414 }
415 spin_unlock(&c->erase_completion_lock);
416
Jeff Garzika6b1d822006-10-04 07:57:18 -0400417 dbg_xattr("xdatum(xid=%u, version=%u) was removed.\n",
418 xd->xid, xd->version);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900419 }
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900420}
421
KaiGai Kohei21b98792006-05-13 15:22:29 +0900422/* -------- xref related functions ------------------
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900423 * verify_xattr_ref(c, ref)
424 * is used to load xref information from medium. Because summary data does not
425 * contain xid/ino, it's necessary to verify once while mounting process.
David Woodhouse9fe48542006-05-23 00:38:06 +0100426 * save_xattr_ref(c, ref)
KaiGai Kohei8a136952006-06-24 09:14:13 +0900427 * is used to write xref to medium. If delete marker is marked, it write
428 * a delete marker of xref into medium.
David Woodhouse9fe48542006-05-23 00:38:06 +0100429 * create_xattr_ref(c, ic, xd)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900430 * is used to create a new xref and write to medium.
KaiGai Kohei8a136952006-06-24 09:14:13 +0900431 * delete_xattr_ref(c, ref)
432 * is used to delete jffs2_xattr_ref. It marks xref XREF_DELETE_MARKER,
433 * and allows GC to reclaim those physical nodes.
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900434 * jffs2_xattr_delete_inode(c, ic)
435 * is called to remove xrefs related to obsolete inode when inode is unlinked.
436 * jffs2_xattr_free_inode(c, ic)
437 * is called to release xattr related objects when unmounting.
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900438 * check_xattr_ref_inode(c, ic)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900439 * is used to confirm inode does not have duplicate xattr name/value pair.
440 * -------------------------------------------------- */
441static int verify_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
442{
443 struct jffs2_eraseblock *jeb;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900444 struct jffs2_raw_node_ref *raw;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900445 struct jffs2_raw_xref rr;
446 size_t readlen;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900447 uint32_t crc, offset, totlen;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900448 int rc;
449
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900450 spin_lock(&c->erase_completion_lock);
451 if (ref_flags(ref->node) != REF_UNCHECKED)
452 goto complete;
453 offset = ref_offset(ref->node);
454 spin_unlock(&c->erase_completion_lock);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900455
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900456 rc = jffs2_flash_read(c, offset, sizeof(rr), &readlen, (char *)&rr);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900457 if (rc || sizeof(rr) != readlen) {
David Woodhouse89291a92006-05-25 13:30:24 +0100458 JFFS2_WARNING("jffs2_flash_read()=%d, req=%zu, read=%zu, at %#08x\n",
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900459 rc, sizeof(rr), readlen, offset);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900460 return rc ? rc : -EIO;
461 }
462 /* obsolete node */
463 crc = crc32(0, &rr, sizeof(rr) - 4);
464 if (crc != je32_to_cpu(rr.node_crc)) {
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900465 JFFS2_ERROR("node CRC failed at %#08x, read=%#08x, calc=%#08x\n",
466 offset, je32_to_cpu(rr.node_crc), crc);
Jean-Christophe DUBOIS9824f752012-05-10 17:13:44 +0200467 return JFFS2_XATTR_IS_CORRUPTED;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900468 }
469 if (je16_to_cpu(rr.magic) != JFFS2_MAGIC_BITMASK
470 || je16_to_cpu(rr.nodetype) != JFFS2_NODETYPE_XREF
471 || je32_to_cpu(rr.totlen) != PAD(sizeof(rr))) {
472 JFFS2_ERROR("inconsistent xref at %#08x, magic=%#04x/%#04x, "
David Woodhouse89291a92006-05-25 13:30:24 +0100473 "nodetype=%#04x/%#04x, totlen=%u/%zu\n",
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900474 offset, je16_to_cpu(rr.magic), JFFS2_MAGIC_BITMASK,
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900475 je16_to_cpu(rr.nodetype), JFFS2_NODETYPE_XREF,
476 je32_to_cpu(rr.totlen), PAD(sizeof(rr)));
Jean-Christophe DUBOIS9824f752012-05-10 17:13:44 +0200477 return JFFS2_XATTR_IS_CORRUPTED;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900478 }
479 ref->ino = je32_to_cpu(rr.ino);
480 ref->xid = je32_to_cpu(rr.xid);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900481 ref->xseqno = je32_to_cpu(rr.xseqno);
482 if (ref->xseqno > c->highest_xseqno)
483 c->highest_xseqno = (ref->xseqno & ~XREF_DELETE_MARKER);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900484
485 spin_lock(&c->erase_completion_lock);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900486 complete:
487 for (raw=ref->node; raw != (void *)ref; raw=raw->next_in_ino) {
488 jeb = &c->blocks[ref_offset(raw) / c->sector_size];
489 totlen = PAD(ref_totlen(c, jeb, raw));
490 if (ref_flags(raw) == REF_UNCHECKED) {
491 c->unchecked_size -= totlen; c->used_size += totlen;
492 jeb->unchecked_size -= totlen; jeb->used_size += totlen;
493 }
494 raw->flash_offset = ref_offset(raw) | ((ref->node==raw) ? REF_PRISTINE : REF_NORMAL);
495 }
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900496 spin_unlock(&c->erase_completion_lock);
497
498 dbg_xattr("success on verifying xref (ino=%u, xid=%u) at %#08x\n",
499 ref->ino, ref->xid, ref_offset(ref->node));
500 return 0;
501}
502
David Woodhouse9fe48542006-05-23 00:38:06 +0100503static int save_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900504{
505 /* must be called under down_write(xattr_sem) */
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900506 struct jffs2_raw_xref rr;
David Woodhouse89291a92006-05-25 13:30:24 +0100507 size_t length;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900508 uint32_t xseqno, phys_ofs = write_ofs(c);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900509 int ret;
510
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900511 rr.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
512 rr.nodetype = cpu_to_je16(JFFS2_NODETYPE_XREF);
513 rr.totlen = cpu_to_je32(PAD(sizeof(rr)));
514 rr.hdr_crc = cpu_to_je32(crc32(0, &rr, sizeof(struct jffs2_unknown_node) - 4));
515
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900516 xseqno = (c->highest_xseqno += 2);
517 if (is_xattr_ref_dead(ref)) {
518 xseqno |= XREF_DELETE_MARKER;
519 rr.ino = cpu_to_je32(ref->ino);
520 rr.xid = cpu_to_je32(ref->xid);
521 } else {
522 rr.ino = cpu_to_je32(ref->ic->ino);
523 rr.xid = cpu_to_je32(ref->xd->xid);
524 }
525 rr.xseqno = cpu_to_je32(xseqno);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900526 rr.node_crc = cpu_to_je32(crc32(0, &rr, sizeof(rr) - 4));
527
528 ret = jffs2_flash_write(c, phys_ofs, sizeof(rr), &length, (char *)&rr);
529 if (ret || sizeof(rr) != length) {
David Woodhouse89291a92006-05-25 13:30:24 +0100530 JFFS2_WARNING("jffs2_flash_write() returned %d, request=%zu, retlen=%zu, at %#08x\n",
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900531 ret, sizeof(rr), length, phys_ofs);
532 ret = ret ? ret : -EIO;
David Woodhouse2f785402006-05-24 02:04:45 +0100533 if (length)
534 jffs2_add_physical_node_ref(c, phys_ofs | REF_OBSOLETE, PAD(sizeof(rr)), NULL);
535
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900536 return ret;
537 }
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900538 /* success */
539 ref->xseqno = xseqno;
540 jffs2_add_physical_node_ref(c, phys_ofs | REF_PRISTINE, PAD(sizeof(rr)), (void *)ref);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900541
542 dbg_xattr("success on saving xref (ino=%u, xid=%u)\n", ref->ic->ino, ref->xd->xid);
543
544 return 0;
545}
546
547static struct jffs2_xattr_ref *create_xattr_ref(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic,
David Woodhouse9fe48542006-05-23 00:38:06 +0100548 struct jffs2_xattr_datum *xd)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900549{
550 /* must be called under down_write(xattr_sem) */
551 struct jffs2_xattr_ref *ref;
552 int ret;
553
554 ref = jffs2_alloc_xattr_ref();
555 if (!ref)
556 return ERR_PTR(-ENOMEM);
557 ref->ic = ic;
558 ref->xd = xd;
559
David Woodhouse9fe48542006-05-23 00:38:06 +0100560 ret = save_xattr_ref(c, ref);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900561 if (ret) {
562 jffs2_free_xattr_ref(ref);
563 return ERR_PTR(ret);
564 }
565
566 /* Chain to inode */
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900567 ref->next = ic->xref;
568 ic->xref = ref;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900569
570 return ref; /* success */
571}
572
KaiGai Kohei8a136952006-06-24 09:14:13 +0900573static void delete_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900574{
575 /* must be called under down_write(xattr_sem) */
576 struct jffs2_xattr_datum *xd;
577
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900578 xd = ref->xd;
KaiGai Kohei8a136952006-06-24 09:14:13 +0900579 ref->xseqno |= XREF_DELETE_MARKER;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900580 ref->ino = ref->ic->ino;
581 ref->xid = ref->xd->xid;
582 spin_lock(&c->erase_completion_lock);
583 ref->next = c->xref_dead_list;
584 c->xref_dead_list = ref;
585 spin_unlock(&c->erase_completion_lock);
586
KaiGai Kohei8a136952006-06-24 09:14:13 +0900587 dbg_xattr("xref(ino=%u, xid=%u, xseqno=%u) was removed.\n",
588 ref->ino, ref->xid, ref->xseqno);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900589
KaiGai Koheic6e8c6c2006-06-29 15:33:02 +0100590 unrefer_xattr_datum(c, xd);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900591}
592
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900593void jffs2_xattr_delete_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
594{
Al Virob57922d2010-06-07 14:34:48 -0400595 /* It's called from jffs2_evict_inode() on inode removing.
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900596 When an inode with XATTR is removed, those XATTRs must be removed. */
KaiGai Kohei8a136952006-06-24 09:14:13 +0900597 struct jffs2_xattr_ref *ref, *_ref;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900598
David Woodhouse27c72b02008-05-01 18:47:17 +0100599 if (!ic || ic->pino_nlink > 0)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900600 return;
601
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900602 down_write(&c->xattr_sem);
KaiGai Kohei8a136952006-06-24 09:14:13 +0900603 for (ref = ic->xref; ref; ref = _ref) {
604 _ref = ref->next;
605 delete_xattr_ref(c, ref);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900606 }
KaiGai Kohei8a136952006-06-24 09:14:13 +0900607 ic->xref = NULL;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900608 up_write(&c->xattr_sem);
609}
610
611void jffs2_xattr_free_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
612{
613 /* It's called from jffs2_free_ino_caches() until unmounting FS. */
614 struct jffs2_xattr_datum *xd;
615 struct jffs2_xattr_ref *ref, *_ref;
616
617 down_write(&c->xattr_sem);
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900618 for (ref = ic->xref; ref; ref = _ref) {
619 _ref = ref->next;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900620 xd = ref->xd;
KaiGai Kohei2c887e22006-06-24 09:16:50 +0900621 if (atomic_dec_and_test(&xd->refcnt)) {
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900622 unload_xattr_datum(c, xd);
623 jffs2_free_xattr_datum(xd);
624 }
625 jffs2_free_xattr_ref(ref);
626 }
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900627 ic->xref = NULL;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900628 up_write(&c->xattr_sem);
629}
630
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900631static int check_xattr_ref_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900632{
Linus Torvaldsa4ce96a2010-07-21 09:25:42 -0700633 /* success of check_xattr_ref_inode() means that inode (ic) dose not have
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900634 * duplicate name/value pairs. If duplicate name/value pair would be found,
635 * one will be removed.
636 */
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900637 struct jffs2_xattr_ref *ref, *cmp, **pref, **pcmp;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900638 int rc = 0;
639
640 if (likely(ic->flags & INO_FLAGS_XATTR_CHECKED))
641 return 0;
642 down_write(&c->xattr_sem);
643 retry:
644 rc = 0;
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900645 for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900646 if (!ref->xd->xname) {
647 rc = load_xattr_datum(c, ref->xd);
648 if (unlikely(rc > 0)) {
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900649 *pref = ref->next;
KaiGai Kohei8a136952006-06-24 09:14:13 +0900650 delete_xattr_ref(c, ref);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900651 goto retry;
652 } else if (unlikely(rc < 0))
653 goto out;
654 }
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900655 for (cmp=ref->next, pcmp=&ref->next; cmp; pcmp=&cmp->next, cmp=cmp->next) {
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900656 if (!cmp->xd->xname) {
657 ref->xd->flags |= JFFS2_XFLAGS_BIND;
658 rc = load_xattr_datum(c, cmp->xd);
659 ref->xd->flags &= ~JFFS2_XFLAGS_BIND;
660 if (unlikely(rc > 0)) {
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900661 *pcmp = cmp->next;
KaiGai Kohei8a136952006-06-24 09:14:13 +0900662 delete_xattr_ref(c, cmp);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900663 goto retry;
664 } else if (unlikely(rc < 0))
665 goto out;
666 }
667 if (ref->xd->xprefix == cmp->xd->xprefix
668 && !strcmp(ref->xd->xname, cmp->xd->xname)) {
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900669 if (ref->xseqno > cmp->xseqno) {
670 *pcmp = cmp->next;
KaiGai Kohei8a136952006-06-24 09:14:13 +0900671 delete_xattr_ref(c, cmp);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900672 } else {
673 *pref = ref->next;
KaiGai Kohei8a136952006-06-24 09:14:13 +0900674 delete_xattr_ref(c, ref);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900675 }
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900676 goto retry;
677 }
678 }
679 }
680 ic->flags |= INO_FLAGS_XATTR_CHECKED;
681 out:
682 up_write(&c->xattr_sem);
683
684 return rc;
685}
686
687/* -------- xattr subsystem functions ---------------
688 * jffs2_init_xattr_subsystem(c)
689 * is used to initialize semaphore and list_head, and some variables.
690 * jffs2_find_xattr_datum(c, xid)
691 * is used to lookup xdatum while scanning process.
692 * jffs2_clear_xattr_subsystem(c)
693 * is used to release any xattr related objects.
694 * jffs2_build_xattr_subsystem(c)
695 * is used to associate xdatum and xref while super block building process.
696 * jffs2_setup_xattr_datum(c, xid, version)
697 * is used to insert xdatum while scanning process.
698 * -------------------------------------------------- */
699void jffs2_init_xattr_subsystem(struct jffs2_sb_info *c)
700{
701 int i;
702
703 for (i=0; i < XATTRINDEX_HASHSIZE; i++)
704 INIT_LIST_HEAD(&c->xattrindex[i]);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900705 INIT_LIST_HEAD(&c->xattr_unchecked);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900706 INIT_LIST_HEAD(&c->xattr_dead_list);
707 c->xref_dead_list = NULL;
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900708 c->xref_temp = NULL;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900709
710 init_rwsem(&c->xattr_sem);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900711 c->highest_xid = 0;
712 c->highest_xseqno = 0;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900713 c->xdatum_mem_usage = 0;
714 c->xdatum_mem_threshold = 32 * 1024; /* Default 32KB */
715}
716
717static struct jffs2_xattr_datum *jffs2_find_xattr_datum(struct jffs2_sb_info *c, uint32_t xid)
718{
719 struct jffs2_xattr_datum *xd;
720 int i = xid % XATTRINDEX_HASHSIZE;
721
722 /* It's only used in scanning/building process. */
723 BUG_ON(!(c->flags & (JFFS2_SB_FLAG_SCANNING|JFFS2_SB_FLAG_BUILDING)));
724
725 list_for_each_entry(xd, &c->xattrindex[i], xindex) {
726 if (xd->xid==xid)
727 return xd;
728 }
729 return NULL;
730}
731
732void jffs2_clear_xattr_subsystem(struct jffs2_sb_info *c)
733{
734 struct jffs2_xattr_datum *xd, *_xd;
735 struct jffs2_xattr_ref *ref, *_ref;
736 int i;
737
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900738 for (ref=c->xref_temp; ref; ref = _ref) {
739 _ref = ref->next;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900740 jffs2_free_xattr_ref(ref);
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900741 }
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900742
743 for (ref=c->xref_dead_list; ref; ref = _ref) {
744 _ref = ref->next;
745 jffs2_free_xattr_ref(ref);
746 }
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900747
748 for (i=0; i < XATTRINDEX_HASHSIZE; i++) {
749 list_for_each_entry_safe(xd, _xd, &c->xattrindex[i], xindex) {
750 list_del(&xd->xindex);
751 if (xd->xname)
752 kfree(xd->xname);
753 jffs2_free_xattr_datum(xd);
754 }
755 }
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900756
757 list_for_each_entry_safe(xd, _xd, &c->xattr_dead_list, xindex) {
758 list_del(&xd->xindex);
759 jffs2_free_xattr_datum(xd);
760 }
David Woodhouse2ad8ee72007-05-08 00:12:58 +0100761 list_for_each_entry_safe(xd, _xd, &c->xattr_unchecked, xindex) {
762 list_del(&xd->xindex);
763 jffs2_free_xattr_datum(xd);
764 }
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900765}
766
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900767#define XREF_TMPHASH_SIZE (128)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900768void jffs2_build_xattr_subsystem(struct jffs2_sb_info *c)
769{
770 struct jffs2_xattr_ref *ref, *_ref;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900771 struct jffs2_xattr_ref *xref_tmphash[XREF_TMPHASH_SIZE];
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900772 struct jffs2_xattr_datum *xd, *_xd;
773 struct jffs2_inode_cache *ic;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900774 struct jffs2_raw_node_ref *raw;
775 int i, xdatum_count = 0, xdatum_unchecked_count = 0, xref_count = 0;
KaiGai Kohei8a136952006-06-24 09:14:13 +0900776 int xdatum_orphan_count = 0, xref_orphan_count = 0, xref_dead_count = 0;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900777
778 BUG_ON(!(c->flags & JFFS2_SB_FLAG_BUILDING));
779
KaiGai Kohei8a136952006-06-24 09:14:13 +0900780 /* Phase.1 : Merge same xref */
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900781 for (i=0; i < XREF_TMPHASH_SIZE; i++)
782 xref_tmphash[i] = NULL;
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900783 for (ref=c->xref_temp; ref; ref=_ref) {
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900784 struct jffs2_xattr_ref *tmp;
785
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900786 _ref = ref->next;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900787 if (ref_flags(ref->node) != REF_PRISTINE) {
788 if (verify_xattr_ref(c, ref)) {
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900789 BUG_ON(ref->node->next_in_ino != (void *)ref);
790 ref->node->next_in_ino = NULL;
791 jffs2_mark_node_obsolete(c, ref->node);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900792 jffs2_free_xattr_ref(ref);
793 continue;
794 }
795 }
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900796
797 i = (ref->ino ^ ref->xid) % XREF_TMPHASH_SIZE;
798 for (tmp=xref_tmphash[i]; tmp; tmp=tmp->next) {
799 if (tmp->ino == ref->ino && tmp->xid == ref->xid)
800 break;
801 }
802 if (tmp) {
803 raw = ref->node;
804 if (ref->xseqno > tmp->xseqno) {
805 tmp->xseqno = ref->xseqno;
806 raw->next_in_ino = tmp->node;
807 tmp->node = raw;
808 } else {
809 raw->next_in_ino = tmp->node->next_in_ino;
810 tmp->node->next_in_ino = raw;
811 }
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900812 jffs2_free_xattr_ref(ref);
813 continue;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900814 } else {
815 ref->next = xref_tmphash[i];
816 xref_tmphash[i] = ref;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900817 }
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900818 }
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900819 c->xref_temp = NULL;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900820
KaiGai Kohei8a136952006-06-24 09:14:13 +0900821 /* Phase.2 : Bind xref with inode_cache and xattr_datum */
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900822 for (i=0; i < XREF_TMPHASH_SIZE; i++) {
823 for (ref=xref_tmphash[i]; ref; ref=_ref) {
KaiGai Kohei8a136952006-06-24 09:14:13 +0900824 xref_count++;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900825 _ref = ref->next;
826 if (is_xattr_ref_dead(ref)) {
827 ref->next = c->xref_dead_list;
828 c->xref_dead_list = ref;
KaiGai Kohei8a136952006-06-24 09:14:13 +0900829 xref_dead_count++;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900830 continue;
831 }
832 /* At this point, ref->xid and ref->ino contain XID and inode number.
833 ref->xd and ref->ic are not valid yet. */
834 xd = jffs2_find_xattr_datum(c, ref->xid);
835 ic = jffs2_get_ino_cache(c, ref->ino);
David Woodhouse27c72b02008-05-01 18:47:17 +0100836 if (!xd || !ic || !ic->pino_nlink) {
KaiGai Kohei8a136952006-06-24 09:14:13 +0900837 dbg_xattr("xref(ino=%u, xid=%u, xseqno=%u) is orphan.\n",
838 ref->ino, ref->xid, ref->xseqno);
839 ref->xseqno |= XREF_DELETE_MARKER;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900840 ref->next = c->xref_dead_list;
841 c->xref_dead_list = ref;
KaiGai Kohei8a136952006-06-24 09:14:13 +0900842 xref_orphan_count++;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900843 continue;
844 }
845 ref->xd = xd;
846 ref->ic = ic;
KaiGai Kohei2c887e22006-06-24 09:16:50 +0900847 atomic_inc(&xd->refcnt);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900848 ref->next = ic->xref;
849 ic->xref = ref;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900850 }
851 }
852
KaiGai Kohei8a136952006-06-24 09:14:13 +0900853 /* Phase.3 : Link unchecked xdatum to xattr_unchecked list */
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900854 for (i=0; i < XATTRINDEX_HASHSIZE; i++) {
855 list_for_each_entry_safe(xd, _xd, &c->xattrindex[i], xindex) {
KaiGai Kohei8a136952006-06-24 09:14:13 +0900856 xdatum_count++;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900857 list_del_init(&xd->xindex);
KaiGai Kohei2c887e22006-06-24 09:16:50 +0900858 if (!atomic_read(&xd->refcnt)) {
KaiGai Kohei8a136952006-06-24 09:14:13 +0900859 dbg_xattr("xdatum(xid=%u, version=%u) is orphan.\n",
860 xd->xid, xd->version);
861 xd->flags |= JFFS2_XFLAGS_DEAD;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900862 list_add(&xd->xindex, &c->xattr_unchecked);
KaiGai Kohei8a136952006-06-24 09:14:13 +0900863 xdatum_orphan_count++;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900864 continue;
865 }
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900866 if (is_xattr_datum_unchecked(c, xd)) {
867 dbg_xattr("unchecked xdatum(xid=%u, version=%u)\n",
868 xd->xid, xd->version);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900869 list_add(&xd->xindex, &c->xattr_unchecked);
870 xdatum_unchecked_count++;
871 }
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900872 }
873 }
874 /* build complete */
KaiGai Kohei8a136952006-06-24 09:14:13 +0900875 JFFS2_NOTICE("complete building xattr subsystem, %u of xdatum"
876 " (%u unchecked, %u orphan) and "
877 "%u of xref (%u dead, %u orphan) found.\n",
878 xdatum_count, xdatum_unchecked_count, xdatum_orphan_count,
879 xref_count, xref_dead_count, xref_orphan_count);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900880}
881
882struct jffs2_xattr_datum *jffs2_setup_xattr_datum(struct jffs2_sb_info *c,
883 uint32_t xid, uint32_t version)
884{
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900885 struct jffs2_xattr_datum *xd;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900886
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900887 xd = jffs2_find_xattr_datum(c, xid);
888 if (!xd) {
889 xd = jffs2_alloc_xattr_datum();
890 if (!xd)
891 return ERR_PTR(-ENOMEM);
892 xd->xid = xid;
893 xd->version = version;
894 if (xd->xid > c->highest_xid)
895 c->highest_xid = xd->xid;
896 list_add_tail(&xd->xindex, &c->xattrindex[xid % XATTRINDEX_HASHSIZE]);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900897 }
898 return xd;
899}
900
901/* -------- xattr subsystem functions ---------------
902 * xprefix_to_handler(xprefix)
903 * is used to translate xprefix into xattr_handler.
904 * jffs2_listxattr(dentry, buffer, size)
905 * is an implementation of listxattr handler on jffs2.
906 * do_jffs2_getxattr(inode, xprefix, xname, buffer, size)
907 * is an implementation of getxattr handler on jffs2.
908 * do_jffs2_setxattr(inode, xprefix, xname, buffer, size, flags)
909 * is an implementation of setxattr handler on jffs2.
910 * -------------------------------------------------- */
Stephen Hemminger365f0cb2010-05-13 17:53:21 -0700911const struct xattr_handler *jffs2_xattr_handlers[] = {
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900912 &jffs2_user_xattr_handler,
913#ifdef CONFIG_JFFS2_FS_SECURITY
914 &jffs2_security_xattr_handler,
915#endif
916#ifdef CONFIG_JFFS2_FS_POSIX_ACL
917 &jffs2_acl_access_xattr_handler,
918 &jffs2_acl_default_xattr_handler,
919#endif
920 &jffs2_trusted_xattr_handler,
921 NULL
922};
923
Stephen Hemminger365f0cb2010-05-13 17:53:21 -0700924static const struct xattr_handler *xprefix_to_handler(int xprefix) {
925 const struct xattr_handler *ret;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900926
927 switch (xprefix) {
928 case JFFS2_XPREFIX_USER:
929 ret = &jffs2_user_xattr_handler;
930 break;
931#ifdef CONFIG_JFFS2_FS_SECURITY
932 case JFFS2_XPREFIX_SECURITY:
933 ret = &jffs2_security_xattr_handler;
934 break;
935#endif
936#ifdef CONFIG_JFFS2_FS_POSIX_ACL
937 case JFFS2_XPREFIX_ACL_ACCESS:
938 ret = &jffs2_acl_access_xattr_handler;
939 break;
940 case JFFS2_XPREFIX_ACL_DEFAULT:
941 ret = &jffs2_acl_default_xattr_handler;
942 break;
943#endif
944 case JFFS2_XPREFIX_TRUSTED:
945 ret = &jffs2_trusted_xattr_handler;
946 break;
947 default:
948 ret = NULL;
949 break;
950 }
951 return ret;
952}
953
954ssize_t jffs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
955{
956 struct inode *inode = dentry->d_inode;
957 struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
958 struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
959 struct jffs2_inode_cache *ic = f->inocache;
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900960 struct jffs2_xattr_ref *ref, **pref;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900961 struct jffs2_xattr_datum *xd;
Stephen Hemminger365f0cb2010-05-13 17:53:21 -0700962 const struct xattr_handler *xhandle;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900963 ssize_t len, rc;
964 int retry = 0;
965
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900966 rc = check_xattr_ref_inode(c, ic);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900967 if (unlikely(rc))
968 return rc;
969
970 down_read(&c->xattr_sem);
971 retry:
972 len = 0;
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900973 for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900974 BUG_ON(ref->ic != ic);
975 xd = ref->xd;
976 if (!xd->xname) {
977 /* xdatum is unchached */
978 if (!retry) {
979 retry = 1;
980 up_read(&c->xattr_sem);
981 down_write(&c->xattr_sem);
982 goto retry;
983 } else {
984 rc = load_xattr_datum(c, xd);
985 if (unlikely(rc > 0)) {
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900986 *pref = ref->next;
KaiGai Kohei8a136952006-06-24 09:14:13 +0900987 delete_xattr_ref(c, ref);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900988 goto retry;
989 } else if (unlikely(rc < 0))
990 goto out;
991 }
992 }
993 xhandle = xprefix_to_handler(xd->xprefix);
994 if (!xhandle)
995 continue;
996 if (buffer) {
Christoph Hellwig431547b2009-11-13 09:52:56 +0000997 rc = xhandle->list(dentry, buffer+len, size-len,
998 xd->xname, xd->name_len, xd->flags);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900999 } else {
Christoph Hellwig431547b2009-11-13 09:52:56 +00001000 rc = xhandle->list(dentry, NULL, 0, xd->xname,
1001 xd->name_len, xd->flags);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001002 }
1003 if (rc < 0)
1004 goto out;
1005 len += rc;
1006 }
1007 rc = len;
1008 out:
1009 if (!retry) {
1010 up_read(&c->xattr_sem);
1011 } else {
1012 up_write(&c->xattr_sem);
1013 }
1014 return rc;
1015}
1016
1017int do_jffs2_getxattr(struct inode *inode, int xprefix, const char *xname,
1018 char *buffer, size_t size)
1019{
1020 struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
1021 struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
1022 struct jffs2_inode_cache *ic = f->inocache;
1023 struct jffs2_xattr_datum *xd;
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +09001024 struct jffs2_xattr_ref *ref, **pref;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001025 int rc, retry = 0;
1026
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +09001027 rc = check_xattr_ref_inode(c, ic);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001028 if (unlikely(rc))
1029 return rc;
1030
1031 down_read(&c->xattr_sem);
1032 retry:
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +09001033 for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001034 BUG_ON(ref->ic!=ic);
1035
1036 xd = ref->xd;
1037 if (xd->xprefix != xprefix)
1038 continue;
1039 if (!xd->xname) {
1040 /* xdatum is unchached */
1041 if (!retry) {
1042 retry = 1;
1043 up_read(&c->xattr_sem);
1044 down_write(&c->xattr_sem);
1045 goto retry;
1046 } else {
1047 rc = load_xattr_datum(c, xd);
1048 if (unlikely(rc > 0)) {
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +09001049 *pref = ref->next;
KaiGai Kohei8a136952006-06-24 09:14:13 +09001050 delete_xattr_ref(c, ref);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001051 goto retry;
1052 } else if (unlikely(rc < 0)) {
1053 goto out;
1054 }
1055 }
1056 }
1057 if (!strcmp(xname, xd->xname)) {
1058 rc = xd->value_len;
1059 if (buffer) {
1060 if (size < rc) {
1061 rc = -ERANGE;
1062 } else {
1063 memcpy(buffer, xd->xvalue, rc);
1064 }
1065 }
1066 goto out;
1067 }
1068 }
1069 rc = -ENODATA;
1070 out:
1071 if (!retry) {
1072 up_read(&c->xattr_sem);
1073 } else {
1074 up_write(&c->xattr_sem);
1075 }
1076 return rc;
1077}
1078
1079int do_jffs2_setxattr(struct inode *inode, int xprefix, const char *xname,
1080 const char *buffer, size_t size, int flags)
1081{
1082 struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
1083 struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
1084 struct jffs2_inode_cache *ic = f->inocache;
1085 struct jffs2_xattr_datum *xd;
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +09001086 struct jffs2_xattr_ref *ref, *newref, **pref;
David Woodhouse9fe48542006-05-23 00:38:06 +01001087 uint32_t length, request;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001088 int rc;
1089
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +09001090 rc = check_xattr_ref_inode(c, ic);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001091 if (unlikely(rc))
1092 return rc;
1093
1094 request = PAD(sizeof(struct jffs2_raw_xattr) + strlen(xname) + 1 + size);
David Woodhouse9fe48542006-05-23 00:38:06 +01001095 rc = jffs2_reserve_space(c, request, &length,
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001096 ALLOC_NORMAL, JFFS2_SUMMARY_XATTR_SIZE);
1097 if (rc) {
1098 JFFS2_WARNING("jffs2_reserve_space()=%d, request=%u\n", rc, request);
1099 return rc;
1100 }
1101
1102 /* Find existing xattr */
1103 down_write(&c->xattr_sem);
1104 retry:
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +09001105 for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001106 xd = ref->xd;
1107 if (xd->xprefix != xprefix)
1108 continue;
1109 if (!xd->xname) {
1110 rc = load_xattr_datum(c, xd);
1111 if (unlikely(rc > 0)) {
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +09001112 *pref = ref->next;
KaiGai Kohei8a136952006-06-24 09:14:13 +09001113 delete_xattr_ref(c, ref);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001114 goto retry;
1115 } else if (unlikely(rc < 0))
1116 goto out;
1117 }
1118 if (!strcmp(xd->xname, xname)) {
1119 if (flags & XATTR_CREATE) {
1120 rc = -EEXIST;
1121 goto out;
1122 }
1123 if (!buffer) {
KaiGai Kohei8a136952006-06-24 09:14:13 +09001124 ref->ino = ic->ino;
1125 ref->xid = xd->xid;
1126 ref->xseqno |= XREF_DELETE_MARKER;
1127 rc = save_xattr_ref(c, ref);
1128 if (!rc) {
1129 *pref = ref->next;
1130 spin_lock(&c->erase_completion_lock);
1131 ref->next = c->xref_dead_list;
1132 c->xref_dead_list = ref;
1133 spin_unlock(&c->erase_completion_lock);
KaiGai Koheic6e8c6c2006-06-29 15:33:02 +01001134 unrefer_xattr_datum(c, xd);
KaiGai Kohei8a136952006-06-24 09:14:13 +09001135 } else {
1136 ref->ic = ic;
1137 ref->xd = xd;
1138 ref->xseqno &= ~XREF_DELETE_MARKER;
1139 }
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001140 goto out;
1141 }
1142 goto found;
1143 }
1144 }
1145 /* not found */
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001146 if (flags & XATTR_REPLACE) {
1147 rc = -ENODATA;
1148 goto out;
1149 }
1150 if (!buffer) {
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001151 rc = -ENODATA;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001152 goto out;
1153 }
1154 found:
David Woodhouse9fe48542006-05-23 00:38:06 +01001155 xd = create_xattr_datum(c, xprefix, xname, buffer, size);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001156 if (IS_ERR(xd)) {
1157 rc = PTR_ERR(xd);
1158 goto out;
1159 }
1160 up_write(&c->xattr_sem);
1161 jffs2_complete_reservation(c);
1162
1163 /* create xattr_ref */
1164 request = PAD(sizeof(struct jffs2_raw_xref));
David Woodhouse9fe48542006-05-23 00:38:06 +01001165 rc = jffs2_reserve_space(c, request, &length,
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001166 ALLOC_NORMAL, JFFS2_SUMMARY_XREF_SIZE);
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001167 down_write(&c->xattr_sem);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001168 if (rc) {
1169 JFFS2_WARNING("jffs2_reserve_space()=%d, request=%u\n", rc, request);
KaiGai Koheic6e8c6c2006-06-29 15:33:02 +01001170 unrefer_xattr_datum(c, xd);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001171 up_write(&c->xattr_sem);
1172 return rc;
1173 }
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +09001174 if (ref)
1175 *pref = ref->next;
David Woodhouse9fe48542006-05-23 00:38:06 +01001176 newref = create_xattr_ref(c, ic, xd);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001177 if (IS_ERR(newref)) {
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +09001178 if (ref) {
1179 ref->next = ic->xref;
1180 ic->xref = ref;
1181 }
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001182 rc = PTR_ERR(newref);
KaiGai Koheic6e8c6c2006-06-29 15:33:02 +01001183 unrefer_xattr_datum(c, xd);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001184 } else if (ref) {
KaiGai Kohei8a136952006-06-24 09:14:13 +09001185 delete_xattr_ref(c, ref);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001186 }
1187 out:
1188 up_write(&c->xattr_sem);
1189 jffs2_complete_reservation(c);
1190 return rc;
1191}
1192
1193/* -------- garbage collector functions -------------
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001194 * jffs2_garbage_collect_xattr_datum(c, xd, raw)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001195 * is used to move xdatum into new node.
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001196 * jffs2_garbage_collect_xattr_ref(c, ref, raw)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001197 * is used to move xref into new node.
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001198 * jffs2_verify_xattr(c)
1199 * is used to call do_verify_xattr_datum() before garbage collecting.
KaiGai Kohei8a136952006-06-24 09:14:13 +09001200 * jffs2_release_xattr_datum(c, xd)
1201 * is used to release an in-memory object of xdatum.
1202 * jffs2_release_xattr_ref(c, ref)
1203 * is used to release an in-memory object of xref.
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001204 * -------------------------------------------------- */
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001205int jffs2_garbage_collect_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd,
1206 struct jffs2_raw_node_ref *raw)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001207{
David Woodhouse9fe48542006-05-23 00:38:06 +01001208 uint32_t totlen, length, old_ofs;
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001209 int rc = 0;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001210
KaiGai Kohei084702e2006-05-13 15:16:13 +09001211 down_write(&c->xattr_sem);
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001212 if (xd->node != raw)
1213 goto out;
KaiGai Kohei8a136952006-06-24 09:14:13 +09001214 if (xd->flags & (JFFS2_XFLAGS_DEAD|JFFS2_XFLAGS_INVALID))
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001215 goto out;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001216
KaiGai Kohei8a136952006-06-24 09:14:13 +09001217 rc = load_xattr_datum(c, xd);
1218 if (unlikely(rc)) {
1219 rc = (rc > 0) ? 0 : rc;
1220 goto out;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001221 }
KaiGai Kohei8a136952006-06-24 09:14:13 +09001222 old_ofs = ref_offset(xd->node);
1223 totlen = PAD(sizeof(struct jffs2_raw_xattr)
1224 + xd->name_len + 1 + xd->value_len);
David Woodhouse9fe48542006-05-23 00:38:06 +01001225 rc = jffs2_reserve_space_gc(c, totlen, &length, JFFS2_SUMMARY_XATTR_SIZE);
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001226 if (rc) {
1227 JFFS2_WARNING("jffs2_reserve_space_gc()=%d, request=%u\n", rc, totlen);
KaiGai Kohei084702e2006-05-13 15:16:13 +09001228 goto out;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001229 }
David Woodhouse9fe48542006-05-23 00:38:06 +01001230 rc = save_xattr_datum(c, xd);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001231 if (!rc)
1232 dbg_xattr("xdatum (xid=%u, version=%u) GC'ed from %#08x to %08x\n",
1233 xd->xid, xd->version, old_ofs, ref_offset(xd->node));
KaiGai Kohei084702e2006-05-13 15:16:13 +09001234 out:
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001235 if (!rc)
1236 jffs2_mark_node_obsolete(c, raw);
KaiGai Kohei084702e2006-05-13 15:16:13 +09001237 up_write(&c->xattr_sem);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001238 return rc;
1239}
1240
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001241int jffs2_garbage_collect_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref,
1242 struct jffs2_raw_node_ref *raw)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001243{
David Woodhouse9fe48542006-05-23 00:38:06 +01001244 uint32_t totlen, length, old_ofs;
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001245 int rc = 0;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001246
KaiGai Kohei084702e2006-05-13 15:16:13 +09001247 down_write(&c->xattr_sem);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001248 BUG_ON(!ref->node);
1249
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001250 if (ref->node != raw)
1251 goto out;
1252 if (is_xattr_ref_dead(ref) && (raw->next_in_ino == (void *)ref))
KaiGai Kohei084702e2006-05-13 15:16:13 +09001253 goto out;
1254
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001255 old_ofs = ref_offset(ref->node);
1256 totlen = ref_totlen(c, c->gcblock, ref->node);
1257
David Woodhouse9fe48542006-05-23 00:38:06 +01001258 rc = jffs2_reserve_space_gc(c, totlen, &length, JFFS2_SUMMARY_XREF_SIZE);
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001259 if (rc) {
1260 JFFS2_WARNING("%s: jffs2_reserve_space_gc() = %d, request = %u\n",
Harvey Harrison8e24eea2008-04-30 00:55:09 -07001261 __func__, rc, totlen);
KaiGai Kohei084702e2006-05-13 15:16:13 +09001262 rc = rc ? rc : -EBADFD;
1263 goto out;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001264 }
David Woodhouse9fe48542006-05-23 00:38:06 +01001265 rc = save_xattr_ref(c, ref);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001266 if (!rc)
1267 dbg_xattr("xref (ino=%u, xid=%u) GC'ed from %#08x to %08x\n",
1268 ref->ic->ino, ref->xd->xid, old_ofs, ref_offset(ref->node));
KaiGai Kohei084702e2006-05-13 15:16:13 +09001269 out:
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001270 if (!rc)
1271 jffs2_mark_node_obsolete(c, raw);
KaiGai Kohei084702e2006-05-13 15:16:13 +09001272 up_write(&c->xattr_sem);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001273 return rc;
1274}
1275
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001276int jffs2_verify_xattr(struct jffs2_sb_info *c)
1277{
1278 struct jffs2_xattr_datum *xd, *_xd;
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001279 struct jffs2_eraseblock *jeb;
1280 struct jffs2_raw_node_ref *raw;
1281 uint32_t totlen;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001282 int rc;
1283
1284 down_write(&c->xattr_sem);
1285 list_for_each_entry_safe(xd, _xd, &c->xattr_unchecked, xindex) {
1286 rc = do_verify_xattr_datum(c, xd);
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001287 if (rc < 0)
1288 continue;
1289 list_del_init(&xd->xindex);
1290 spin_lock(&c->erase_completion_lock);
1291 for (raw=xd->node; raw != (void *)xd; raw=raw->next_in_ino) {
1292 if (ref_flags(raw) != REF_UNCHECKED)
1293 continue;
1294 jeb = &c->blocks[ref_offset(raw) / c->sector_size];
1295 totlen = PAD(ref_totlen(c, jeb, raw));
1296 c->unchecked_size -= totlen; c->used_size += totlen;
1297 jeb->unchecked_size -= totlen; jeb->used_size += totlen;
1298 raw->flash_offset = ref_offset(raw)
1299 | ((xd->node == (void *)raw) ? REF_PRISTINE : REF_NORMAL);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001300 }
KaiGai Kohei8a136952006-06-24 09:14:13 +09001301 if (xd->flags & JFFS2_XFLAGS_DEAD)
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001302 list_add(&xd->xindex, &c->xattr_dead_list);
1303 spin_unlock(&c->erase_completion_lock);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001304 }
1305 up_write(&c->xattr_sem);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001306 return list_empty(&c->xattr_unchecked) ? 1 : 0;
1307}
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001308
1309void jffs2_release_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
1310{
1311 /* must be called under spin_lock(&c->erase_completion_lock) */
KaiGai Kohei2c887e22006-06-24 09:16:50 +09001312 if (atomic_read(&xd->refcnt) || xd->node != (void *)xd)
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001313 return;
1314
1315 list_del(&xd->xindex);
1316 jffs2_free_xattr_datum(xd);
1317}
1318
1319void jffs2_release_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
1320{
1321 /* must be called under spin_lock(&c->erase_completion_lock) */
1322 struct jffs2_xattr_ref *tmp, **ptmp;
1323
1324 if (ref->node != (void *)ref)
1325 return;
1326
1327 for (tmp=c->xref_dead_list, ptmp=&c->xref_dead_list; tmp; ptmp=&tmp->next, tmp=tmp->next) {
1328 if (ref == tmp) {
1329 *ptmp = tmp->next;
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001330 break;
1331 }
1332 }
KaiGai Kohei8a136952006-06-24 09:14:13 +09001333 jffs2_free_xattr_ref(ref);
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001334}