blob: ad0f2e2a1700835d596e656b540ec0ac06cafd90 [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>
Christoph Hellwigf2963d42013-12-20 05:16:47 -080025#include <linux/posix_acl_xattr.h>
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +090026#include <linux/mtd/mtd.h>
27#include "nodelist.h"
28/* -------- xdatum related functions ----------------
29 * xattr_datum_hashkey(xprefix, xname, xvalue, xsize)
30 * is used to calcurate xdatum hashkey. The reminder of hashkey into XATTRINDEX_HASHSIZE is
31 * the index of the xattr name/value pair cache (c->xattrindex).
KaiGai Koheic9f700f2006-06-11 10:35:15 +090032 * is_xattr_datum_unchecked(c, xd)
33 * returns 1, if xdatum contains any unchecked raw nodes. if all raw nodes are not
34 * unchecked, it returns 0.
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +090035 * unload_xattr_datum(c, xd)
36 * is used to release xattr name/value pair and detach from c->xattrindex.
37 * reclaim_xattr_datum(c)
38 * is used to reclaim xattr name/value pairs on the xattr name/value pair cache when
Tracey Dentbea93122011-02-26 11:15:13 -050039 * memory usage by cache is over c->xdatum_mem_threshold. Currently, this threshold
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +090040 * is hard coded as 32KiB.
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +090041 * do_verify_xattr_datum(c, xd)
42 * is used to load the xdatum informations without name/value pair from the medium.
43 * It's necessary once, because those informations are not collected during mounting
44 * process when EBS is enabled.
45 * 0 will be returned, if success. An negative return value means recoverable error, and
46 * positive return value means unrecoverable error. Thus, caller must remove this xdatum
47 * and xref when it returned positive value.
48 * do_load_xattr_datum(c, xd)
49 * is used to load name/value pair from the medium.
50 * The meanings of return value is same as do_verify_xattr_datum().
51 * load_xattr_datum(c, xd)
52 * is used to be as a wrapper of do_verify_xattr_datum() and do_load_xattr_datum().
53 * If xd need to call do_verify_xattr_datum() at first, it's called before calling
54 * do_load_xattr_datum(). The meanings of return value is same as do_verify_xattr_datum().
David Woodhouse9fe48542006-05-23 00:38:06 +010055 * save_xattr_datum(c, xd)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +090056 * is used to write xdatum to medium. xd->version will be incremented.
David Woodhouse9fe48542006-05-23 00:38:06 +010057 * create_xattr_datum(c, xprefix, xname, xvalue, xsize)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +090058 * is used to create new xdatum and write to medium.
KaiGai Koheic6e8c6c2006-06-29 15:33:02 +010059 * unrefer_xattr_datum(c, xd)
60 * is used to delete a xdatum. When nobody refers this xdatum, JFFS2_XFLAGS_DEAD
61 * is set on xd->flags and chained xattr_dead_list or release it immediately.
62 * In the first case, the garbage collector release it later.
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +090063 * -------------------------------------------------- */
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +090064static uint32_t xattr_datum_hashkey(int xprefix, const char *xname, const char *xvalue, int xsize)
65{
66 int name_len = strlen(xname);
67
68 return crc32(xprefix, xname, name_len) ^ crc32(xprefix, xvalue, xsize);
69}
70
KaiGai Koheic9f700f2006-06-11 10:35:15 +090071static int is_xattr_datum_unchecked(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
72{
73 struct jffs2_raw_node_ref *raw;
74 int rc = 0;
75
76 spin_lock(&c->erase_completion_lock);
77 for (raw=xd->node; raw != (void *)xd; raw=raw->next_in_ino) {
78 if (ref_flags(raw) == REF_UNCHECKED) {
79 rc = 1;
80 break;
81 }
82 }
83 spin_unlock(&c->erase_completion_lock);
84 return rc;
85}
86
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +090087static void unload_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
88{
89 /* must be called under down_write(xattr_sem) */
Harvey Harrison8e24eea2008-04-30 00:55:09 -070090 D1(dbg_xattr("%s: xid=%u, version=%u\n", __func__, xd->xid, xd->version));
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +090091 if (xd->xname) {
92 c->xdatum_mem_usage -= (xd->name_len + 1 + xd->value_len);
93 kfree(xd->xname);
94 }
95
96 list_del_init(&xd->xindex);
97 xd->hashkey = 0;
98 xd->xname = NULL;
99 xd->xvalue = NULL;
100}
101
102static void reclaim_xattr_datum(struct jffs2_sb_info *c)
103{
104 /* must be called under down_write(xattr_sem) */
105 struct jffs2_xattr_datum *xd, *_xd;
106 uint32_t target, before;
107 static int index = 0;
108 int count;
109
110 if (c->xdatum_mem_threshold > c->xdatum_mem_usage)
111 return;
112
113 before = c->xdatum_mem_usage;
114 target = c->xdatum_mem_usage * 4 / 5; /* 20% reduction */
115 for (count = 0; count < XATTRINDEX_HASHSIZE; count++) {
116 list_for_each_entry_safe(xd, _xd, &c->xattrindex[index], xindex) {
117 if (xd->flags & JFFS2_XFLAGS_HOT) {
118 xd->flags &= ~JFFS2_XFLAGS_HOT;
119 } else if (!(xd->flags & JFFS2_XFLAGS_BIND)) {
120 unload_xattr_datum(c, xd);
121 }
122 if (c->xdatum_mem_usage <= target)
123 goto out;
124 }
125 index = (index+1) % XATTRINDEX_HASHSIZE;
126 }
127 out:
128 JFFS2_NOTICE("xdatum_mem_usage from %u byte to %u byte (%u byte reclaimed)\n",
129 before, c->xdatum_mem_usage, before - c->xdatum_mem_usage);
130}
131
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900132static int do_verify_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
133{
134 /* must be called under down_write(xattr_sem) */
135 struct jffs2_eraseblock *jeb;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900136 struct jffs2_raw_node_ref *raw;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900137 struct jffs2_raw_xattr rx;
138 size_t readlen;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900139 uint32_t crc, offset, totlen;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900140 int rc;
141
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900142 spin_lock(&c->erase_completion_lock);
143 offset = ref_offset(xd->node);
144 if (ref_flags(xd->node) == REF_PRISTINE)
145 goto complete;
146 spin_unlock(&c->erase_completion_lock);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900147
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900148 rc = jffs2_flash_read(c, offset, sizeof(rx), &readlen, (char *)&rx);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900149 if (rc || readlen != sizeof(rx)) {
David Woodhouse89291a92006-05-25 13:30:24 +0100150 JFFS2_WARNING("jffs2_flash_read()=%d, req=%zu, read=%zu at %#08x\n",
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900151 rc, sizeof(rx), readlen, offset);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900152 return rc ? rc : -EIO;
153 }
154 crc = crc32(0, &rx, sizeof(rx) - 4);
155 if (crc != je32_to_cpu(rx.node_crc)) {
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900156 JFFS2_ERROR("node CRC failed at %#08x, read=%#08x, calc=%#08x\n",
157 offset, je32_to_cpu(rx.hdr_crc), crc);
158 xd->flags |= JFFS2_XFLAGS_INVALID;
Jean-Christophe DUBOIS9824f752012-05-10 17:13:44 +0200159 return JFFS2_XATTR_IS_CORRUPTED;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900160 }
KaiGai Kohei8a136952006-06-24 09:14:13 +0900161 totlen = PAD(sizeof(rx) + rx.name_len + 1 + je16_to_cpu(rx.value_len));
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900162 if (je16_to_cpu(rx.magic) != JFFS2_MAGIC_BITMASK
163 || je16_to_cpu(rx.nodetype) != JFFS2_NODETYPE_XATTR
164 || je32_to_cpu(rx.totlen) != totlen
165 || je32_to_cpu(rx.xid) != xd->xid
166 || je32_to_cpu(rx.version) != xd->version) {
167 JFFS2_ERROR("inconsistent xdatum at %#08x, magic=%#04x/%#04x, "
168 "nodetype=%#04x/%#04x, totlen=%u/%u, xid=%u/%u, version=%u/%u\n",
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900169 offset, je16_to_cpu(rx.magic), JFFS2_MAGIC_BITMASK,
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900170 je16_to_cpu(rx.nodetype), JFFS2_NODETYPE_XATTR,
171 je32_to_cpu(rx.totlen), totlen,
172 je32_to_cpu(rx.xid), xd->xid,
173 je32_to_cpu(rx.version), xd->version);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900174 xd->flags |= JFFS2_XFLAGS_INVALID;
Jean-Christophe DUBOIS9824f752012-05-10 17:13:44 +0200175 return JFFS2_XATTR_IS_CORRUPTED;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900176 }
177 xd->xprefix = rx.xprefix;
178 xd->name_len = rx.name_len;
179 xd->value_len = je16_to_cpu(rx.value_len);
180 xd->data_crc = je32_to_cpu(rx.data_crc);
181
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900182 spin_lock(&c->erase_completion_lock);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900183 complete:
184 for (raw=xd->node; raw != (void *)xd; raw=raw->next_in_ino) {
185 jeb = &c->blocks[ref_offset(raw) / c->sector_size];
186 totlen = PAD(ref_totlen(c, jeb, raw));
187 if (ref_flags(raw) == REF_UNCHECKED) {
188 c->unchecked_size -= totlen; c->used_size += totlen;
189 jeb->unchecked_size -= totlen; jeb->used_size += totlen;
190 }
191 raw->flash_offset = ref_offset(raw) | ((xd->node==raw) ? REF_PRISTINE : REF_NORMAL);
192 }
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900193 spin_unlock(&c->erase_completion_lock);
194
195 /* unchecked xdatum is chained with c->xattr_unchecked */
196 list_del_init(&xd->xindex);
197
198 dbg_xattr("success on verfying xdatum (xid=%u, version=%u)\n",
199 xd->xid, xd->version);
200
201 return 0;
202}
203
204static int do_load_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
205{
206 /* must be called under down_write(xattr_sem) */
207 char *data;
208 size_t readlen;
209 uint32_t crc, length;
210 int i, ret, retry = 0;
211
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900212 BUG_ON(ref_flags(xd->node) != REF_PRISTINE);
213 BUG_ON(!list_empty(&xd->xindex));
214 retry:
215 length = xd->name_len + 1 + xd->value_len;
216 data = kmalloc(length, GFP_KERNEL);
217 if (!data)
218 return -ENOMEM;
219
220 ret = jffs2_flash_read(c, ref_offset(xd->node)+sizeof(struct jffs2_raw_xattr),
221 length, &readlen, data);
222
223 if (ret || length!=readlen) {
David Woodhouse89291a92006-05-25 13:30:24 +0100224 JFFS2_WARNING("jffs2_flash_read() returned %d, request=%d, readlen=%zu, at %#08x\n",
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900225 ret, length, readlen, ref_offset(xd->node));
226 kfree(data);
227 return ret ? ret : -EIO;
228 }
229
230 data[xd->name_len] = '\0';
231 crc = crc32(0, data, length);
232 if (crc != xd->data_crc) {
Jean-Christophe DUBOIS9824f752012-05-10 17:13:44 +0200233 JFFS2_WARNING("node CRC failed (JFFS2_NODETYPE_XATTR)"
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900234 " at %#08x, read: 0x%08x calculated: 0x%08x\n",
235 ref_offset(xd->node), xd->data_crc, crc);
236 kfree(data);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900237 xd->flags |= JFFS2_XFLAGS_INVALID;
Jean-Christophe DUBOIS9824f752012-05-10 17:13:44 +0200238 return JFFS2_XATTR_IS_CORRUPTED;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900239 }
240
241 xd->flags |= JFFS2_XFLAGS_HOT;
242 xd->xname = data;
243 xd->xvalue = data + xd->name_len+1;
244
245 c->xdatum_mem_usage += length;
246
247 xd->hashkey = xattr_datum_hashkey(xd->xprefix, xd->xname, xd->xvalue, xd->value_len);
248 i = xd->hashkey % XATTRINDEX_HASHSIZE;
249 list_add(&xd->xindex, &c->xattrindex[i]);
250 if (!retry) {
251 retry = 1;
252 reclaim_xattr_datum(c);
253 if (!xd->xname)
254 goto retry;
255 }
256
257 dbg_xattr("success on loading xdatum (xid=%u, xprefix=%u, xname='%s')\n",
258 xd->xid, xd->xprefix, xd->xname);
259
260 return 0;
261}
262
263static int load_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
264{
265 /* must be called under down_write(xattr_sem);
266 * rc < 0 : recoverable error, try again
267 * rc = 0 : success
268 * rc > 0 : Unrecoverable error, this node should be deleted.
269 */
270 int rc = 0;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900271
KaiGai Kohei8a136952006-06-24 09:14:13 +0900272 BUG_ON(xd->flags & JFFS2_XFLAGS_DEAD);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900273 if (xd->xname)
274 return 0;
275 if (xd->flags & JFFS2_XFLAGS_INVALID)
Jean-Christophe DUBOIS9824f752012-05-10 17:13:44 +0200276 return JFFS2_XATTR_IS_CORRUPTED;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900277 if (unlikely(is_xattr_datum_unchecked(c, xd)))
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900278 rc = do_verify_xattr_datum(c, xd);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900279 if (!rc)
280 rc = do_load_xattr_datum(c, xd);
281 return rc;
282}
283
David Woodhouse9fe48542006-05-23 00:38:06 +0100284static int save_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900285{
286 /* must be called under down_write(xattr_sem) */
David Woodhouse2f785402006-05-24 02:04:45 +0100287 struct jffs2_raw_xattr rx;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900288 struct kvec vecs[2];
David Woodhouse89291a92006-05-25 13:30:24 +0100289 size_t length;
KaiGai Kohei8a136952006-06-24 09:14:13 +0900290 int rc, totlen;
David Woodhouse9fe48542006-05-23 00:38:06 +0100291 uint32_t phys_ofs = write_ofs(c);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900292
KaiGai Kohei8a136952006-06-24 09:14:13 +0900293 BUG_ON(!xd->xname);
294 BUG_ON(xd->flags & (JFFS2_XFLAGS_DEAD|JFFS2_XFLAGS_INVALID));
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900295
296 vecs[0].iov_base = &rx;
KaiGai Kohei8a136952006-06-24 09:14:13 +0900297 vecs[0].iov_len = sizeof(rx);
298 vecs[1].iov_base = xd->xname;
299 vecs[1].iov_len = xd->name_len + 1 + xd->value_len;
300 totlen = vecs[0].iov_len + vecs[1].iov_len;
301
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900302 /* Setup raw-xattr */
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900303 memset(&rx, 0, sizeof(rx));
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900304 rx.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
305 rx.nodetype = cpu_to_je16(JFFS2_NODETYPE_XATTR);
306 rx.totlen = cpu_to_je32(PAD(totlen));
307 rx.hdr_crc = cpu_to_je32(crc32(0, &rx, sizeof(struct jffs2_unknown_node) - 4));
308
309 rx.xid = cpu_to_je32(xd->xid);
KaiGai Kohei8a136952006-06-24 09:14:13 +0900310 rx.version = cpu_to_je32(++xd->version);
311 rx.xprefix = xd->xprefix;
312 rx.name_len = xd->name_len;
313 rx.value_len = cpu_to_je16(xd->value_len);
314 rx.data_crc = cpu_to_je32(crc32(0, vecs[1].iov_base, vecs[1].iov_len));
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900315 rx.node_crc = cpu_to_je32(crc32(0, &rx, sizeof(struct jffs2_raw_xattr) - 4));
316
KaiGai Kohei8a136952006-06-24 09:14:13 +0900317 rc = jffs2_flash_writev(c, vecs, 2, phys_ofs, &length, 0);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900318 if (rc || totlen != length) {
David Woodhouse89291a92006-05-25 13:30:24 +0100319 JFFS2_WARNING("jffs2_flash_writev()=%d, req=%u, wrote=%zu, at %#08x\n",
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900320 rc, totlen, length, phys_ofs);
321 rc = rc ? rc : -EIO;
David Woodhouse2f785402006-05-24 02:04:45 +0100322 if (length)
323 jffs2_add_physical_node_ref(c, phys_ofs | REF_OBSOLETE, PAD(totlen), NULL);
324
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900325 return rc;
326 }
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900327 /* success */
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900328 jffs2_add_physical_node_ref(c, phys_ofs | REF_PRISTINE, PAD(totlen), (void *)xd);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900329
330 dbg_xattr("success on saving xdatum (xid=%u, version=%u, xprefix=%u, xname='%s')\n",
331 xd->xid, xd->version, xd->xprefix, xd->xname);
332
333 return 0;
334}
335
336static struct jffs2_xattr_datum *create_xattr_datum(struct jffs2_sb_info *c,
337 int xprefix, const char *xname,
David Woodhouse9fe48542006-05-23 00:38:06 +0100338 const char *xvalue, int xsize)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900339{
340 /* must be called under down_write(xattr_sem) */
341 struct jffs2_xattr_datum *xd;
342 uint32_t hashkey, name_len;
343 char *data;
344 int i, rc;
345
346 /* Search xattr_datum has same xname/xvalue by index */
347 hashkey = xattr_datum_hashkey(xprefix, xname, xvalue, xsize);
348 i = hashkey % XATTRINDEX_HASHSIZE;
349 list_for_each_entry(xd, &c->xattrindex[i], xindex) {
350 if (xd->hashkey==hashkey
351 && xd->xprefix==xprefix
352 && xd->value_len==xsize
353 && !strcmp(xd->xname, xname)
354 && !memcmp(xd->xvalue, xvalue, xsize)) {
KaiGai Kohei2c887e22006-06-24 09:16:50 +0900355 atomic_inc(&xd->refcnt);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900356 return xd;
357 }
358 }
359
360 /* Not found, Create NEW XATTR-Cache */
361 name_len = strlen(xname);
362
363 xd = jffs2_alloc_xattr_datum();
364 if (!xd)
365 return ERR_PTR(-ENOMEM);
366
367 data = kmalloc(name_len + 1 + xsize, GFP_KERNEL);
368 if (!data) {
369 jffs2_free_xattr_datum(xd);
370 return ERR_PTR(-ENOMEM);
371 }
372 strcpy(data, xname);
373 memcpy(data + name_len + 1, xvalue, xsize);
374
KaiGai Kohei2c887e22006-06-24 09:16:50 +0900375 atomic_set(&xd->refcnt, 1);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900376 xd->xid = ++c->highest_xid;
377 xd->flags |= JFFS2_XFLAGS_HOT;
378 xd->xprefix = xprefix;
379
380 xd->hashkey = hashkey;
381 xd->xname = data;
382 xd->xvalue = data + name_len + 1;
383 xd->name_len = name_len;
384 xd->value_len = xsize;
385 xd->data_crc = crc32(0, data, xd->name_len + 1 + xd->value_len);
386
David Woodhouse9fe48542006-05-23 00:38:06 +0100387 rc = save_xattr_datum(c, xd);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900388 if (rc) {
389 kfree(xd->xname);
390 jffs2_free_xattr_datum(xd);
391 return ERR_PTR(rc);
392 }
393
394 /* Insert Hash Index */
395 i = hashkey % XATTRINDEX_HASHSIZE;
396 list_add(&xd->xindex, &c->xattrindex[i]);
397
398 c->xdatum_mem_usage += (xd->name_len + 1 + xd->value_len);
399 reclaim_xattr_datum(c);
400
401 return xd;
402}
403
KaiGai Koheic6e8c6c2006-06-29 15:33:02 +0100404static void unrefer_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900405{
406 /* must be called under down_write(xattr_sem) */
KaiGai Koheic6e8c6c2006-06-29 15:33:02 +0100407 if (atomic_dec_and_lock(&xd->refcnt, &c->erase_completion_lock)) {
KaiGai Koheic6e8c6c2006-06-29 15:33:02 +0100408 unload_xattr_datum(c, xd);
409 xd->flags |= JFFS2_XFLAGS_DEAD;
410 if (xd->node == (void *)xd) {
411 BUG_ON(!(xd->flags & JFFS2_XFLAGS_INVALID));
412 jffs2_free_xattr_datum(xd);
413 } else {
414 list_add(&xd->xindex, &c->xattr_dead_list);
415 }
416 spin_unlock(&c->erase_completion_lock);
417
Jeff Garzika6b1d822006-10-04 07:57:18 -0400418 dbg_xattr("xdatum(xid=%u, version=%u) was removed.\n",
419 xd->xid, xd->version);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900420 }
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900421}
422
KaiGai Kohei21b98792006-05-13 15:22:29 +0900423/* -------- xref related functions ------------------
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900424 * verify_xattr_ref(c, ref)
425 * is used to load xref information from medium. Because summary data does not
426 * contain xid/ino, it's necessary to verify once while mounting process.
David Woodhouse9fe48542006-05-23 00:38:06 +0100427 * save_xattr_ref(c, ref)
KaiGai Kohei8a136952006-06-24 09:14:13 +0900428 * is used to write xref to medium. If delete marker is marked, it write
429 * a delete marker of xref into medium.
David Woodhouse9fe48542006-05-23 00:38:06 +0100430 * create_xattr_ref(c, ic, xd)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900431 * is used to create a new xref and write to medium.
KaiGai Kohei8a136952006-06-24 09:14:13 +0900432 * delete_xattr_ref(c, ref)
433 * is used to delete jffs2_xattr_ref. It marks xref XREF_DELETE_MARKER,
434 * and allows GC to reclaim those physical nodes.
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900435 * jffs2_xattr_delete_inode(c, ic)
436 * is called to remove xrefs related to obsolete inode when inode is unlinked.
437 * jffs2_xattr_free_inode(c, ic)
438 * is called to release xattr related objects when unmounting.
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900439 * check_xattr_ref_inode(c, ic)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900440 * is used to confirm inode does not have duplicate xattr name/value pair.
Jean-Christophe DUBOIS8c5a0362012-05-10 17:14:03 +0200441 * jffs2_xattr_do_crccheck_inode(c, ic)
442 * is used to force xattr data integrity check during the initial gc scan.
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900443 * -------------------------------------------------- */
444static int verify_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
445{
446 struct jffs2_eraseblock *jeb;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900447 struct jffs2_raw_node_ref *raw;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900448 struct jffs2_raw_xref rr;
449 size_t readlen;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900450 uint32_t crc, offset, totlen;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900451 int rc;
452
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900453 spin_lock(&c->erase_completion_lock);
454 if (ref_flags(ref->node) != REF_UNCHECKED)
455 goto complete;
456 offset = ref_offset(ref->node);
457 spin_unlock(&c->erase_completion_lock);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900458
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900459 rc = jffs2_flash_read(c, offset, sizeof(rr), &readlen, (char *)&rr);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900460 if (rc || sizeof(rr) != readlen) {
David Woodhouse89291a92006-05-25 13:30:24 +0100461 JFFS2_WARNING("jffs2_flash_read()=%d, req=%zu, read=%zu, at %#08x\n",
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900462 rc, sizeof(rr), readlen, offset);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900463 return rc ? rc : -EIO;
464 }
465 /* obsolete node */
466 crc = crc32(0, &rr, sizeof(rr) - 4);
467 if (crc != je32_to_cpu(rr.node_crc)) {
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900468 JFFS2_ERROR("node CRC failed at %#08x, read=%#08x, calc=%#08x\n",
469 offset, je32_to_cpu(rr.node_crc), crc);
Jean-Christophe DUBOIS9824f752012-05-10 17:13:44 +0200470 return JFFS2_XATTR_IS_CORRUPTED;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900471 }
472 if (je16_to_cpu(rr.magic) != JFFS2_MAGIC_BITMASK
473 || je16_to_cpu(rr.nodetype) != JFFS2_NODETYPE_XREF
474 || je32_to_cpu(rr.totlen) != PAD(sizeof(rr))) {
475 JFFS2_ERROR("inconsistent xref at %#08x, magic=%#04x/%#04x, "
David Woodhouse89291a92006-05-25 13:30:24 +0100476 "nodetype=%#04x/%#04x, totlen=%u/%zu\n",
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900477 offset, je16_to_cpu(rr.magic), JFFS2_MAGIC_BITMASK,
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900478 je16_to_cpu(rr.nodetype), JFFS2_NODETYPE_XREF,
479 je32_to_cpu(rr.totlen), PAD(sizeof(rr)));
Jean-Christophe DUBOIS9824f752012-05-10 17:13:44 +0200480 return JFFS2_XATTR_IS_CORRUPTED;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900481 }
482 ref->ino = je32_to_cpu(rr.ino);
483 ref->xid = je32_to_cpu(rr.xid);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900484 ref->xseqno = je32_to_cpu(rr.xseqno);
485 if (ref->xseqno > c->highest_xseqno)
486 c->highest_xseqno = (ref->xseqno & ~XREF_DELETE_MARKER);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900487
488 spin_lock(&c->erase_completion_lock);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900489 complete:
490 for (raw=ref->node; raw != (void *)ref; raw=raw->next_in_ino) {
491 jeb = &c->blocks[ref_offset(raw) / c->sector_size];
492 totlen = PAD(ref_totlen(c, jeb, raw));
493 if (ref_flags(raw) == REF_UNCHECKED) {
494 c->unchecked_size -= totlen; c->used_size += totlen;
495 jeb->unchecked_size -= totlen; jeb->used_size += totlen;
496 }
497 raw->flash_offset = ref_offset(raw) | ((ref->node==raw) ? REF_PRISTINE : REF_NORMAL);
498 }
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900499 spin_unlock(&c->erase_completion_lock);
500
501 dbg_xattr("success on verifying xref (ino=%u, xid=%u) at %#08x\n",
502 ref->ino, ref->xid, ref_offset(ref->node));
503 return 0;
504}
505
David Woodhouse9fe48542006-05-23 00:38:06 +0100506static int save_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900507{
508 /* must be called under down_write(xattr_sem) */
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900509 struct jffs2_raw_xref rr;
David Woodhouse89291a92006-05-25 13:30:24 +0100510 size_t length;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900511 uint32_t xseqno, phys_ofs = write_ofs(c);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900512 int ret;
513
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900514 rr.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
515 rr.nodetype = cpu_to_je16(JFFS2_NODETYPE_XREF);
516 rr.totlen = cpu_to_je32(PAD(sizeof(rr)));
517 rr.hdr_crc = cpu_to_je32(crc32(0, &rr, sizeof(struct jffs2_unknown_node) - 4));
518
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900519 xseqno = (c->highest_xseqno += 2);
520 if (is_xattr_ref_dead(ref)) {
521 xseqno |= XREF_DELETE_MARKER;
522 rr.ino = cpu_to_je32(ref->ino);
523 rr.xid = cpu_to_je32(ref->xid);
524 } else {
525 rr.ino = cpu_to_je32(ref->ic->ino);
526 rr.xid = cpu_to_je32(ref->xd->xid);
527 }
528 rr.xseqno = cpu_to_je32(xseqno);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900529 rr.node_crc = cpu_to_je32(crc32(0, &rr, sizeof(rr) - 4));
530
531 ret = jffs2_flash_write(c, phys_ofs, sizeof(rr), &length, (char *)&rr);
532 if (ret || sizeof(rr) != length) {
David Woodhouse89291a92006-05-25 13:30:24 +0100533 JFFS2_WARNING("jffs2_flash_write() returned %d, request=%zu, retlen=%zu, at %#08x\n",
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900534 ret, sizeof(rr), length, phys_ofs);
535 ret = ret ? ret : -EIO;
David Woodhouse2f785402006-05-24 02:04:45 +0100536 if (length)
537 jffs2_add_physical_node_ref(c, phys_ofs | REF_OBSOLETE, PAD(sizeof(rr)), NULL);
538
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900539 return ret;
540 }
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900541 /* success */
542 ref->xseqno = xseqno;
543 jffs2_add_physical_node_ref(c, phys_ofs | REF_PRISTINE, PAD(sizeof(rr)), (void *)ref);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900544
545 dbg_xattr("success on saving xref (ino=%u, xid=%u)\n", ref->ic->ino, ref->xd->xid);
546
547 return 0;
548}
549
550static struct jffs2_xattr_ref *create_xattr_ref(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic,
David Woodhouse9fe48542006-05-23 00:38:06 +0100551 struct jffs2_xattr_datum *xd)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900552{
553 /* must be called under down_write(xattr_sem) */
554 struct jffs2_xattr_ref *ref;
555 int ret;
556
557 ref = jffs2_alloc_xattr_ref();
558 if (!ref)
559 return ERR_PTR(-ENOMEM);
560 ref->ic = ic;
561 ref->xd = xd;
562
David Woodhouse9fe48542006-05-23 00:38:06 +0100563 ret = save_xattr_ref(c, ref);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900564 if (ret) {
565 jffs2_free_xattr_ref(ref);
566 return ERR_PTR(ret);
567 }
568
569 /* Chain to inode */
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900570 ref->next = ic->xref;
571 ic->xref = ref;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900572
573 return ref; /* success */
574}
575
KaiGai Kohei8a136952006-06-24 09:14:13 +0900576static void delete_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900577{
578 /* must be called under down_write(xattr_sem) */
579 struct jffs2_xattr_datum *xd;
580
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900581 xd = ref->xd;
KaiGai Kohei8a136952006-06-24 09:14:13 +0900582 ref->xseqno |= XREF_DELETE_MARKER;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900583 ref->ino = ref->ic->ino;
584 ref->xid = ref->xd->xid;
585 spin_lock(&c->erase_completion_lock);
586 ref->next = c->xref_dead_list;
587 c->xref_dead_list = ref;
588 spin_unlock(&c->erase_completion_lock);
589
KaiGai Kohei8a136952006-06-24 09:14:13 +0900590 dbg_xattr("xref(ino=%u, xid=%u, xseqno=%u) was removed.\n",
591 ref->ino, ref->xid, ref->xseqno);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900592
KaiGai Koheic6e8c6c2006-06-29 15:33:02 +0100593 unrefer_xattr_datum(c, xd);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900594}
595
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900596void jffs2_xattr_delete_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
597{
Al Virob57922d2010-06-07 14:34:48 -0400598 /* It's called from jffs2_evict_inode() on inode removing.
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900599 When an inode with XATTR is removed, those XATTRs must be removed. */
KaiGai Kohei8a136952006-06-24 09:14:13 +0900600 struct jffs2_xattr_ref *ref, *_ref;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900601
David Woodhouse27c72b02008-05-01 18:47:17 +0100602 if (!ic || ic->pino_nlink > 0)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900603 return;
604
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900605 down_write(&c->xattr_sem);
KaiGai Kohei8a136952006-06-24 09:14:13 +0900606 for (ref = ic->xref; ref; ref = _ref) {
607 _ref = ref->next;
608 delete_xattr_ref(c, ref);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900609 }
KaiGai Kohei8a136952006-06-24 09:14:13 +0900610 ic->xref = NULL;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900611 up_write(&c->xattr_sem);
612}
613
614void jffs2_xattr_free_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
615{
616 /* It's called from jffs2_free_ino_caches() until unmounting FS. */
617 struct jffs2_xattr_datum *xd;
618 struct jffs2_xattr_ref *ref, *_ref;
619
620 down_write(&c->xattr_sem);
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900621 for (ref = ic->xref; ref; ref = _ref) {
622 _ref = ref->next;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900623 xd = ref->xd;
KaiGai Kohei2c887e22006-06-24 09:16:50 +0900624 if (atomic_dec_and_test(&xd->refcnt)) {
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900625 unload_xattr_datum(c, xd);
626 jffs2_free_xattr_datum(xd);
627 }
628 jffs2_free_xattr_ref(ref);
629 }
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900630 ic->xref = NULL;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900631 up_write(&c->xattr_sem);
632}
633
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900634static int check_xattr_ref_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900635{
Linus Torvaldsa4ce96a2010-07-21 09:25:42 -0700636 /* success of check_xattr_ref_inode() means that inode (ic) dose not have
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900637 * duplicate name/value pairs. If duplicate name/value pair would be found,
638 * one will be removed.
639 */
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900640 struct jffs2_xattr_ref *ref, *cmp, **pref, **pcmp;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900641 int rc = 0;
642
643 if (likely(ic->flags & INO_FLAGS_XATTR_CHECKED))
644 return 0;
645 down_write(&c->xattr_sem);
646 retry:
647 rc = 0;
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900648 for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900649 if (!ref->xd->xname) {
650 rc = load_xattr_datum(c, ref->xd);
651 if (unlikely(rc > 0)) {
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900652 *pref = ref->next;
KaiGai Kohei8a136952006-06-24 09:14:13 +0900653 delete_xattr_ref(c, ref);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900654 goto retry;
655 } else if (unlikely(rc < 0))
656 goto out;
657 }
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900658 for (cmp=ref->next, pcmp=&ref->next; cmp; pcmp=&cmp->next, cmp=cmp->next) {
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900659 if (!cmp->xd->xname) {
660 ref->xd->flags |= JFFS2_XFLAGS_BIND;
661 rc = load_xattr_datum(c, cmp->xd);
662 ref->xd->flags &= ~JFFS2_XFLAGS_BIND;
663 if (unlikely(rc > 0)) {
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900664 *pcmp = cmp->next;
KaiGai Kohei8a136952006-06-24 09:14:13 +0900665 delete_xattr_ref(c, cmp);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900666 goto retry;
667 } else if (unlikely(rc < 0))
668 goto out;
669 }
670 if (ref->xd->xprefix == cmp->xd->xprefix
671 && !strcmp(ref->xd->xname, cmp->xd->xname)) {
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900672 if (ref->xseqno > cmp->xseqno) {
673 *pcmp = cmp->next;
KaiGai Kohei8a136952006-06-24 09:14:13 +0900674 delete_xattr_ref(c, cmp);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900675 } else {
676 *pref = ref->next;
KaiGai Kohei8a136952006-06-24 09:14:13 +0900677 delete_xattr_ref(c, ref);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900678 }
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900679 goto retry;
680 }
681 }
682 }
683 ic->flags |= INO_FLAGS_XATTR_CHECKED;
684 out:
685 up_write(&c->xattr_sem);
686
687 return rc;
688}
689
Jean-Christophe DUBOIS8c5a0362012-05-10 17:14:03 +0200690void jffs2_xattr_do_crccheck_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
691{
692 check_xattr_ref_inode(c, ic);
693}
694
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900695/* -------- xattr subsystem functions ---------------
696 * jffs2_init_xattr_subsystem(c)
697 * is used to initialize semaphore and list_head, and some variables.
698 * jffs2_find_xattr_datum(c, xid)
699 * is used to lookup xdatum while scanning process.
700 * jffs2_clear_xattr_subsystem(c)
701 * is used to release any xattr related objects.
702 * jffs2_build_xattr_subsystem(c)
703 * is used to associate xdatum and xref while super block building process.
704 * jffs2_setup_xattr_datum(c, xid, version)
705 * is used to insert xdatum while scanning process.
706 * -------------------------------------------------- */
707void jffs2_init_xattr_subsystem(struct jffs2_sb_info *c)
708{
709 int i;
710
711 for (i=0; i < XATTRINDEX_HASHSIZE; i++)
712 INIT_LIST_HEAD(&c->xattrindex[i]);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900713 INIT_LIST_HEAD(&c->xattr_unchecked);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900714 INIT_LIST_HEAD(&c->xattr_dead_list);
715 c->xref_dead_list = NULL;
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900716 c->xref_temp = NULL;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900717
718 init_rwsem(&c->xattr_sem);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900719 c->highest_xid = 0;
720 c->highest_xseqno = 0;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900721 c->xdatum_mem_usage = 0;
722 c->xdatum_mem_threshold = 32 * 1024; /* Default 32KB */
723}
724
725static struct jffs2_xattr_datum *jffs2_find_xattr_datum(struct jffs2_sb_info *c, uint32_t xid)
726{
727 struct jffs2_xattr_datum *xd;
728 int i = xid % XATTRINDEX_HASHSIZE;
729
730 /* It's only used in scanning/building process. */
731 BUG_ON(!(c->flags & (JFFS2_SB_FLAG_SCANNING|JFFS2_SB_FLAG_BUILDING)));
732
733 list_for_each_entry(xd, &c->xattrindex[i], xindex) {
734 if (xd->xid==xid)
735 return xd;
736 }
737 return NULL;
738}
739
740void jffs2_clear_xattr_subsystem(struct jffs2_sb_info *c)
741{
742 struct jffs2_xattr_datum *xd, *_xd;
743 struct jffs2_xattr_ref *ref, *_ref;
744 int i;
745
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900746 for (ref=c->xref_temp; ref; ref = _ref) {
747 _ref = ref->next;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900748 jffs2_free_xattr_ref(ref);
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900749 }
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900750
751 for (ref=c->xref_dead_list; ref; ref = _ref) {
752 _ref = ref->next;
753 jffs2_free_xattr_ref(ref);
754 }
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900755
756 for (i=0; i < XATTRINDEX_HASHSIZE; i++) {
757 list_for_each_entry_safe(xd, _xd, &c->xattrindex[i], xindex) {
758 list_del(&xd->xindex);
759 if (xd->xname)
760 kfree(xd->xname);
761 jffs2_free_xattr_datum(xd);
762 }
763 }
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900764
765 list_for_each_entry_safe(xd, _xd, &c->xattr_dead_list, xindex) {
766 list_del(&xd->xindex);
767 jffs2_free_xattr_datum(xd);
768 }
David Woodhouse2ad8ee72007-05-08 00:12:58 +0100769 list_for_each_entry_safe(xd, _xd, &c->xattr_unchecked, xindex) {
770 list_del(&xd->xindex);
771 jffs2_free_xattr_datum(xd);
772 }
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900773}
774
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900775#define XREF_TMPHASH_SIZE (128)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900776void jffs2_build_xattr_subsystem(struct jffs2_sb_info *c)
777{
778 struct jffs2_xattr_ref *ref, *_ref;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900779 struct jffs2_xattr_ref *xref_tmphash[XREF_TMPHASH_SIZE];
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900780 struct jffs2_xattr_datum *xd, *_xd;
781 struct jffs2_inode_cache *ic;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900782 struct jffs2_raw_node_ref *raw;
783 int i, xdatum_count = 0, xdatum_unchecked_count = 0, xref_count = 0;
KaiGai Kohei8a136952006-06-24 09:14:13 +0900784 int xdatum_orphan_count = 0, xref_orphan_count = 0, xref_dead_count = 0;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900785
786 BUG_ON(!(c->flags & JFFS2_SB_FLAG_BUILDING));
787
KaiGai Kohei8a136952006-06-24 09:14:13 +0900788 /* Phase.1 : Merge same xref */
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900789 for (i=0; i < XREF_TMPHASH_SIZE; i++)
790 xref_tmphash[i] = NULL;
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900791 for (ref=c->xref_temp; ref; ref=_ref) {
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900792 struct jffs2_xattr_ref *tmp;
793
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900794 _ref = ref->next;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900795 if (ref_flags(ref->node) != REF_PRISTINE) {
796 if (verify_xattr_ref(c, ref)) {
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900797 BUG_ON(ref->node->next_in_ino != (void *)ref);
798 ref->node->next_in_ino = NULL;
799 jffs2_mark_node_obsolete(c, ref->node);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900800 jffs2_free_xattr_ref(ref);
801 continue;
802 }
803 }
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900804
805 i = (ref->ino ^ ref->xid) % XREF_TMPHASH_SIZE;
806 for (tmp=xref_tmphash[i]; tmp; tmp=tmp->next) {
807 if (tmp->ino == ref->ino && tmp->xid == ref->xid)
808 break;
809 }
810 if (tmp) {
811 raw = ref->node;
812 if (ref->xseqno > tmp->xseqno) {
813 tmp->xseqno = ref->xseqno;
814 raw->next_in_ino = tmp->node;
815 tmp->node = raw;
816 } else {
817 raw->next_in_ino = tmp->node->next_in_ino;
818 tmp->node->next_in_ino = raw;
819 }
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900820 jffs2_free_xattr_ref(ref);
821 continue;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900822 } else {
823 ref->next = xref_tmphash[i];
824 xref_tmphash[i] = ref;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900825 }
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900826 }
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900827 c->xref_temp = NULL;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900828
KaiGai Kohei8a136952006-06-24 09:14:13 +0900829 /* Phase.2 : Bind xref with inode_cache and xattr_datum */
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900830 for (i=0; i < XREF_TMPHASH_SIZE; i++) {
831 for (ref=xref_tmphash[i]; ref; ref=_ref) {
KaiGai Kohei8a136952006-06-24 09:14:13 +0900832 xref_count++;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900833 _ref = ref->next;
834 if (is_xattr_ref_dead(ref)) {
835 ref->next = c->xref_dead_list;
836 c->xref_dead_list = ref;
KaiGai Kohei8a136952006-06-24 09:14:13 +0900837 xref_dead_count++;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900838 continue;
839 }
840 /* At this point, ref->xid and ref->ino contain XID and inode number.
841 ref->xd and ref->ic are not valid yet. */
842 xd = jffs2_find_xattr_datum(c, ref->xid);
843 ic = jffs2_get_ino_cache(c, ref->ino);
David Woodhouse27c72b02008-05-01 18:47:17 +0100844 if (!xd || !ic || !ic->pino_nlink) {
KaiGai Kohei8a136952006-06-24 09:14:13 +0900845 dbg_xattr("xref(ino=%u, xid=%u, xseqno=%u) is orphan.\n",
846 ref->ino, ref->xid, ref->xseqno);
847 ref->xseqno |= XREF_DELETE_MARKER;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900848 ref->next = c->xref_dead_list;
849 c->xref_dead_list = ref;
KaiGai Kohei8a136952006-06-24 09:14:13 +0900850 xref_orphan_count++;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900851 continue;
852 }
853 ref->xd = xd;
854 ref->ic = ic;
KaiGai Kohei2c887e22006-06-24 09:16:50 +0900855 atomic_inc(&xd->refcnt);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900856 ref->next = ic->xref;
857 ic->xref = ref;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900858 }
859 }
860
KaiGai Kohei8a136952006-06-24 09:14:13 +0900861 /* Phase.3 : Link unchecked xdatum to xattr_unchecked list */
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900862 for (i=0; i < XATTRINDEX_HASHSIZE; i++) {
863 list_for_each_entry_safe(xd, _xd, &c->xattrindex[i], xindex) {
KaiGai Kohei8a136952006-06-24 09:14:13 +0900864 xdatum_count++;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900865 list_del_init(&xd->xindex);
KaiGai Kohei2c887e22006-06-24 09:16:50 +0900866 if (!atomic_read(&xd->refcnt)) {
KaiGai Kohei8a136952006-06-24 09:14:13 +0900867 dbg_xattr("xdatum(xid=%u, version=%u) is orphan.\n",
868 xd->xid, xd->version);
869 xd->flags |= JFFS2_XFLAGS_DEAD;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900870 list_add(&xd->xindex, &c->xattr_unchecked);
KaiGai Kohei8a136952006-06-24 09:14:13 +0900871 xdatum_orphan_count++;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900872 continue;
873 }
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900874 if (is_xattr_datum_unchecked(c, xd)) {
875 dbg_xattr("unchecked xdatum(xid=%u, version=%u)\n",
876 xd->xid, xd->version);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900877 list_add(&xd->xindex, &c->xattr_unchecked);
878 xdatum_unchecked_count++;
879 }
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900880 }
881 }
882 /* build complete */
KaiGai Kohei8a136952006-06-24 09:14:13 +0900883 JFFS2_NOTICE("complete building xattr subsystem, %u of xdatum"
884 " (%u unchecked, %u orphan) and "
885 "%u of xref (%u dead, %u orphan) found.\n",
886 xdatum_count, xdatum_unchecked_count, xdatum_orphan_count,
887 xref_count, xref_dead_count, xref_orphan_count);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900888}
889
890struct jffs2_xattr_datum *jffs2_setup_xattr_datum(struct jffs2_sb_info *c,
891 uint32_t xid, uint32_t version)
892{
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900893 struct jffs2_xattr_datum *xd;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900894
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900895 xd = jffs2_find_xattr_datum(c, xid);
896 if (!xd) {
897 xd = jffs2_alloc_xattr_datum();
898 if (!xd)
899 return ERR_PTR(-ENOMEM);
900 xd->xid = xid;
901 xd->version = version;
902 if (xd->xid > c->highest_xid)
903 c->highest_xid = xd->xid;
904 list_add_tail(&xd->xindex, &c->xattrindex[xid % XATTRINDEX_HASHSIZE]);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900905 }
906 return xd;
907}
908
909/* -------- xattr subsystem functions ---------------
910 * xprefix_to_handler(xprefix)
911 * is used to translate xprefix into xattr_handler.
912 * jffs2_listxattr(dentry, buffer, size)
913 * is an implementation of listxattr handler on jffs2.
914 * do_jffs2_getxattr(inode, xprefix, xname, buffer, size)
915 * is an implementation of getxattr handler on jffs2.
916 * do_jffs2_setxattr(inode, xprefix, xname, buffer, size, flags)
917 * is an implementation of setxattr handler on jffs2.
918 * -------------------------------------------------- */
Stephen Hemminger365f0cb2010-05-13 17:53:21 -0700919const struct xattr_handler *jffs2_xattr_handlers[] = {
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900920 &jffs2_user_xattr_handler,
921#ifdef CONFIG_JFFS2_FS_SECURITY
922 &jffs2_security_xattr_handler,
923#endif
924#ifdef CONFIG_JFFS2_FS_POSIX_ACL
Christoph Hellwigf2963d42013-12-20 05:16:47 -0800925 &posix_acl_access_xattr_handler,
926 &posix_acl_default_xattr_handler,
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900927#endif
928 &jffs2_trusted_xattr_handler,
929 NULL
930};
931
Stephen Hemminger365f0cb2010-05-13 17:53:21 -0700932static const struct xattr_handler *xprefix_to_handler(int xprefix) {
933 const struct xattr_handler *ret;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900934
935 switch (xprefix) {
936 case JFFS2_XPREFIX_USER:
937 ret = &jffs2_user_xattr_handler;
938 break;
939#ifdef CONFIG_JFFS2_FS_SECURITY
940 case JFFS2_XPREFIX_SECURITY:
941 ret = &jffs2_security_xattr_handler;
942 break;
943#endif
944#ifdef CONFIG_JFFS2_FS_POSIX_ACL
945 case JFFS2_XPREFIX_ACL_ACCESS:
Christoph Hellwigf2963d42013-12-20 05:16:47 -0800946 ret = &posix_acl_access_xattr_handler;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900947 break;
948 case JFFS2_XPREFIX_ACL_DEFAULT:
Christoph Hellwigf2963d42013-12-20 05:16:47 -0800949 ret = &posix_acl_default_xattr_handler;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900950 break;
951#endif
952 case JFFS2_XPREFIX_TRUSTED:
953 ret = &jffs2_trusted_xattr_handler;
954 break;
955 default:
956 ret = NULL;
957 break;
958 }
959 return ret;
960}
961
962ssize_t jffs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
963{
964 struct inode *inode = dentry->d_inode;
965 struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
966 struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
967 struct jffs2_inode_cache *ic = f->inocache;
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900968 struct jffs2_xattr_ref *ref, **pref;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900969 struct jffs2_xattr_datum *xd;
Stephen Hemminger365f0cb2010-05-13 17:53:21 -0700970 const struct xattr_handler *xhandle;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900971 ssize_t len, rc;
972 int retry = 0;
973
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900974 rc = check_xattr_ref_inode(c, ic);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900975 if (unlikely(rc))
976 return rc;
977
978 down_read(&c->xattr_sem);
979 retry:
980 len = 0;
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900981 for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900982 BUG_ON(ref->ic != ic);
983 xd = ref->xd;
984 if (!xd->xname) {
985 /* xdatum is unchached */
986 if (!retry) {
987 retry = 1;
988 up_read(&c->xattr_sem);
989 down_write(&c->xattr_sem);
990 goto retry;
991 } else {
992 rc = load_xattr_datum(c, xd);
993 if (unlikely(rc > 0)) {
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900994 *pref = ref->next;
KaiGai Kohei8a136952006-06-24 09:14:13 +0900995 delete_xattr_ref(c, ref);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900996 goto retry;
997 } else if (unlikely(rc < 0))
998 goto out;
999 }
1000 }
1001 xhandle = xprefix_to_handler(xd->xprefix);
1002 if (!xhandle)
1003 continue;
1004 if (buffer) {
Christoph Hellwig431547b2009-11-13 09:52:56 +00001005 rc = xhandle->list(dentry, buffer+len, size-len,
1006 xd->xname, xd->name_len, xd->flags);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001007 } else {
Christoph Hellwig431547b2009-11-13 09:52:56 +00001008 rc = xhandle->list(dentry, NULL, 0, xd->xname,
1009 xd->name_len, xd->flags);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001010 }
1011 if (rc < 0)
1012 goto out;
1013 len += rc;
1014 }
1015 rc = len;
1016 out:
1017 if (!retry) {
1018 up_read(&c->xattr_sem);
1019 } else {
1020 up_write(&c->xattr_sem);
1021 }
1022 return rc;
1023}
1024
1025int do_jffs2_getxattr(struct inode *inode, int xprefix, const char *xname,
1026 char *buffer, size_t size)
1027{
1028 struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
1029 struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
1030 struct jffs2_inode_cache *ic = f->inocache;
1031 struct jffs2_xattr_datum *xd;
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +09001032 struct jffs2_xattr_ref *ref, **pref;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001033 int rc, retry = 0;
1034
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +09001035 rc = check_xattr_ref_inode(c, ic);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001036 if (unlikely(rc))
1037 return rc;
1038
1039 down_read(&c->xattr_sem);
1040 retry:
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +09001041 for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001042 BUG_ON(ref->ic!=ic);
1043
1044 xd = ref->xd;
1045 if (xd->xprefix != xprefix)
1046 continue;
1047 if (!xd->xname) {
1048 /* xdatum is unchached */
1049 if (!retry) {
1050 retry = 1;
1051 up_read(&c->xattr_sem);
1052 down_write(&c->xattr_sem);
1053 goto retry;
1054 } else {
1055 rc = load_xattr_datum(c, xd);
1056 if (unlikely(rc > 0)) {
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +09001057 *pref = ref->next;
KaiGai Kohei8a136952006-06-24 09:14:13 +09001058 delete_xattr_ref(c, ref);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001059 goto retry;
1060 } else if (unlikely(rc < 0)) {
1061 goto out;
1062 }
1063 }
1064 }
1065 if (!strcmp(xname, xd->xname)) {
1066 rc = xd->value_len;
1067 if (buffer) {
1068 if (size < rc) {
1069 rc = -ERANGE;
1070 } else {
1071 memcpy(buffer, xd->xvalue, rc);
1072 }
1073 }
1074 goto out;
1075 }
1076 }
1077 rc = -ENODATA;
1078 out:
1079 if (!retry) {
1080 up_read(&c->xattr_sem);
1081 } else {
1082 up_write(&c->xattr_sem);
1083 }
1084 return rc;
1085}
1086
1087int do_jffs2_setxattr(struct inode *inode, int xprefix, const char *xname,
1088 const char *buffer, size_t size, int flags)
1089{
1090 struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
1091 struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
1092 struct jffs2_inode_cache *ic = f->inocache;
1093 struct jffs2_xattr_datum *xd;
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +09001094 struct jffs2_xattr_ref *ref, *newref, **pref;
David Woodhouse9fe48542006-05-23 00:38:06 +01001095 uint32_t length, request;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001096 int rc;
1097
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +09001098 rc = check_xattr_ref_inode(c, ic);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001099 if (unlikely(rc))
1100 return rc;
1101
1102 request = PAD(sizeof(struct jffs2_raw_xattr) + strlen(xname) + 1 + size);
David Woodhouse9fe48542006-05-23 00:38:06 +01001103 rc = jffs2_reserve_space(c, request, &length,
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001104 ALLOC_NORMAL, JFFS2_SUMMARY_XATTR_SIZE);
1105 if (rc) {
1106 JFFS2_WARNING("jffs2_reserve_space()=%d, request=%u\n", rc, request);
1107 return rc;
1108 }
1109
1110 /* Find existing xattr */
1111 down_write(&c->xattr_sem);
1112 retry:
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +09001113 for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001114 xd = ref->xd;
1115 if (xd->xprefix != xprefix)
1116 continue;
1117 if (!xd->xname) {
1118 rc = load_xattr_datum(c, xd);
1119 if (unlikely(rc > 0)) {
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +09001120 *pref = ref->next;
KaiGai Kohei8a136952006-06-24 09:14:13 +09001121 delete_xattr_ref(c, ref);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001122 goto retry;
1123 } else if (unlikely(rc < 0))
1124 goto out;
1125 }
1126 if (!strcmp(xd->xname, xname)) {
1127 if (flags & XATTR_CREATE) {
1128 rc = -EEXIST;
1129 goto out;
1130 }
1131 if (!buffer) {
KaiGai Kohei8a136952006-06-24 09:14:13 +09001132 ref->ino = ic->ino;
1133 ref->xid = xd->xid;
1134 ref->xseqno |= XREF_DELETE_MARKER;
1135 rc = save_xattr_ref(c, ref);
1136 if (!rc) {
1137 *pref = ref->next;
1138 spin_lock(&c->erase_completion_lock);
1139 ref->next = c->xref_dead_list;
1140 c->xref_dead_list = ref;
1141 spin_unlock(&c->erase_completion_lock);
KaiGai Koheic6e8c6c2006-06-29 15:33:02 +01001142 unrefer_xattr_datum(c, xd);
KaiGai Kohei8a136952006-06-24 09:14:13 +09001143 } else {
1144 ref->ic = ic;
1145 ref->xd = xd;
1146 ref->xseqno &= ~XREF_DELETE_MARKER;
1147 }
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001148 goto out;
1149 }
1150 goto found;
1151 }
1152 }
1153 /* not found */
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001154 if (flags & XATTR_REPLACE) {
1155 rc = -ENODATA;
1156 goto out;
1157 }
1158 if (!buffer) {
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001159 rc = -ENODATA;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001160 goto out;
1161 }
1162 found:
David Woodhouse9fe48542006-05-23 00:38:06 +01001163 xd = create_xattr_datum(c, xprefix, xname, buffer, size);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001164 if (IS_ERR(xd)) {
1165 rc = PTR_ERR(xd);
1166 goto out;
1167 }
1168 up_write(&c->xattr_sem);
1169 jffs2_complete_reservation(c);
1170
1171 /* create xattr_ref */
1172 request = PAD(sizeof(struct jffs2_raw_xref));
David Woodhouse9fe48542006-05-23 00:38:06 +01001173 rc = jffs2_reserve_space(c, request, &length,
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001174 ALLOC_NORMAL, JFFS2_SUMMARY_XREF_SIZE);
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001175 down_write(&c->xattr_sem);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001176 if (rc) {
1177 JFFS2_WARNING("jffs2_reserve_space()=%d, request=%u\n", rc, request);
KaiGai Koheic6e8c6c2006-06-29 15:33:02 +01001178 unrefer_xattr_datum(c, xd);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001179 up_write(&c->xattr_sem);
1180 return rc;
1181 }
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +09001182 if (ref)
1183 *pref = ref->next;
David Woodhouse9fe48542006-05-23 00:38:06 +01001184 newref = create_xattr_ref(c, ic, xd);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001185 if (IS_ERR(newref)) {
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +09001186 if (ref) {
1187 ref->next = ic->xref;
1188 ic->xref = ref;
1189 }
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001190 rc = PTR_ERR(newref);
KaiGai Koheic6e8c6c2006-06-29 15:33:02 +01001191 unrefer_xattr_datum(c, xd);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001192 } else if (ref) {
KaiGai Kohei8a136952006-06-24 09:14:13 +09001193 delete_xattr_ref(c, ref);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001194 }
1195 out:
1196 up_write(&c->xattr_sem);
1197 jffs2_complete_reservation(c);
1198 return rc;
1199}
1200
1201/* -------- garbage collector functions -------------
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001202 * jffs2_garbage_collect_xattr_datum(c, xd, raw)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001203 * is used to move xdatum into new node.
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001204 * jffs2_garbage_collect_xattr_ref(c, ref, raw)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001205 * is used to move xref into new node.
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001206 * jffs2_verify_xattr(c)
1207 * is used to call do_verify_xattr_datum() before garbage collecting.
KaiGai Kohei8a136952006-06-24 09:14:13 +09001208 * jffs2_release_xattr_datum(c, xd)
1209 * is used to release an in-memory object of xdatum.
1210 * jffs2_release_xattr_ref(c, ref)
1211 * is used to release an in-memory object of xref.
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001212 * -------------------------------------------------- */
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001213int jffs2_garbage_collect_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd,
1214 struct jffs2_raw_node_ref *raw)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001215{
David Woodhouse9fe48542006-05-23 00:38:06 +01001216 uint32_t totlen, length, old_ofs;
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001217 int rc = 0;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001218
KaiGai Kohei084702e2006-05-13 15:16:13 +09001219 down_write(&c->xattr_sem);
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001220 if (xd->node != raw)
1221 goto out;
KaiGai Kohei8a136952006-06-24 09:14:13 +09001222 if (xd->flags & (JFFS2_XFLAGS_DEAD|JFFS2_XFLAGS_INVALID))
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001223 goto out;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001224
KaiGai Kohei8a136952006-06-24 09:14:13 +09001225 rc = load_xattr_datum(c, xd);
1226 if (unlikely(rc)) {
1227 rc = (rc > 0) ? 0 : rc;
1228 goto out;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001229 }
KaiGai Kohei8a136952006-06-24 09:14:13 +09001230 old_ofs = ref_offset(xd->node);
1231 totlen = PAD(sizeof(struct jffs2_raw_xattr)
1232 + xd->name_len + 1 + xd->value_len);
David Woodhouse9fe48542006-05-23 00:38:06 +01001233 rc = jffs2_reserve_space_gc(c, totlen, &length, JFFS2_SUMMARY_XATTR_SIZE);
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001234 if (rc) {
1235 JFFS2_WARNING("jffs2_reserve_space_gc()=%d, request=%u\n", rc, totlen);
KaiGai Kohei084702e2006-05-13 15:16:13 +09001236 goto out;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001237 }
David Woodhouse9fe48542006-05-23 00:38:06 +01001238 rc = save_xattr_datum(c, xd);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001239 if (!rc)
1240 dbg_xattr("xdatum (xid=%u, version=%u) GC'ed from %#08x to %08x\n",
1241 xd->xid, xd->version, old_ofs, ref_offset(xd->node));
KaiGai Kohei084702e2006-05-13 15:16:13 +09001242 out:
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001243 if (!rc)
1244 jffs2_mark_node_obsolete(c, raw);
KaiGai Kohei084702e2006-05-13 15:16:13 +09001245 up_write(&c->xattr_sem);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001246 return rc;
1247}
1248
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001249int jffs2_garbage_collect_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref,
1250 struct jffs2_raw_node_ref *raw)
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001251{
David Woodhouse9fe48542006-05-23 00:38:06 +01001252 uint32_t totlen, length, old_ofs;
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001253 int rc = 0;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001254
KaiGai Kohei084702e2006-05-13 15:16:13 +09001255 down_write(&c->xattr_sem);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001256 BUG_ON(!ref->node);
1257
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001258 if (ref->node != raw)
1259 goto out;
1260 if (is_xattr_ref_dead(ref) && (raw->next_in_ino == (void *)ref))
KaiGai Kohei084702e2006-05-13 15:16:13 +09001261 goto out;
1262
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001263 old_ofs = ref_offset(ref->node);
1264 totlen = ref_totlen(c, c->gcblock, ref->node);
1265
David Woodhouse9fe48542006-05-23 00:38:06 +01001266 rc = jffs2_reserve_space_gc(c, totlen, &length, JFFS2_SUMMARY_XREF_SIZE);
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001267 if (rc) {
1268 JFFS2_WARNING("%s: jffs2_reserve_space_gc() = %d, request = %u\n",
Harvey Harrison8e24eea2008-04-30 00:55:09 -07001269 __func__, rc, totlen);
KaiGai Kohei084702e2006-05-13 15:16:13 +09001270 rc = rc ? rc : -EBADFD;
1271 goto out;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001272 }
David Woodhouse9fe48542006-05-23 00:38:06 +01001273 rc = save_xattr_ref(c, ref);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001274 if (!rc)
1275 dbg_xattr("xref (ino=%u, xid=%u) GC'ed from %#08x to %08x\n",
1276 ref->ic->ino, ref->xd->xid, old_ofs, ref_offset(ref->node));
KaiGai Kohei084702e2006-05-13 15:16:13 +09001277 out:
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001278 if (!rc)
1279 jffs2_mark_node_obsolete(c, raw);
KaiGai Kohei084702e2006-05-13 15:16:13 +09001280 up_write(&c->xattr_sem);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001281 return rc;
1282}
1283
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001284int jffs2_verify_xattr(struct jffs2_sb_info *c)
1285{
1286 struct jffs2_xattr_datum *xd, *_xd;
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001287 struct jffs2_eraseblock *jeb;
1288 struct jffs2_raw_node_ref *raw;
1289 uint32_t totlen;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001290 int rc;
1291
1292 down_write(&c->xattr_sem);
1293 list_for_each_entry_safe(xd, _xd, &c->xattr_unchecked, xindex) {
1294 rc = do_verify_xattr_datum(c, xd);
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001295 if (rc < 0)
1296 continue;
1297 list_del_init(&xd->xindex);
1298 spin_lock(&c->erase_completion_lock);
1299 for (raw=xd->node; raw != (void *)xd; raw=raw->next_in_ino) {
1300 if (ref_flags(raw) != REF_UNCHECKED)
1301 continue;
1302 jeb = &c->blocks[ref_offset(raw) / c->sector_size];
1303 totlen = PAD(ref_totlen(c, jeb, raw));
1304 c->unchecked_size -= totlen; c->used_size += totlen;
1305 jeb->unchecked_size -= totlen; jeb->used_size += totlen;
1306 raw->flash_offset = ref_offset(raw)
1307 | ((xd->node == (void *)raw) ? REF_PRISTINE : REF_NORMAL);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001308 }
KaiGai Kohei8a136952006-06-24 09:14:13 +09001309 if (xd->flags & JFFS2_XFLAGS_DEAD)
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001310 list_add(&xd->xindex, &c->xattr_dead_list);
1311 spin_unlock(&c->erase_completion_lock);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001312 }
1313 up_write(&c->xattr_sem);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09001314 return list_empty(&c->xattr_unchecked) ? 1 : 0;
1315}
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001316
1317void jffs2_release_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
1318{
1319 /* must be called under spin_lock(&c->erase_completion_lock) */
KaiGai Kohei2c887e22006-06-24 09:16:50 +09001320 if (atomic_read(&xd->refcnt) || xd->node != (void *)xd)
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001321 return;
1322
1323 list_del(&xd->xindex);
1324 jffs2_free_xattr_datum(xd);
1325}
1326
1327void jffs2_release_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
1328{
1329 /* must be called under spin_lock(&c->erase_completion_lock) */
1330 struct jffs2_xattr_ref *tmp, **ptmp;
1331
1332 if (ref->node != (void *)ref)
1333 return;
1334
1335 for (tmp=c->xref_dead_list, ptmp=&c->xref_dead_list; tmp; ptmp=&tmp->next, tmp=tmp->next) {
1336 if (ref == tmp) {
1337 *ptmp = tmp->next;
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001338 break;
1339 }
1340 }
KaiGai Kohei8a136952006-06-24 09:14:13 +09001341 jffs2_free_xattr_ref(ref);
KaiGai Koheic9f700f2006-06-11 10:35:15 +09001342}