Blame view

fs/jffs2/xattr.c 38.3 KB
652ecc20d   KaiGai Kohei   [JFFS2][XATTR] Un...
1
2
  /*
   * JFFS2 -- Journalling Flash File System, Version 2.
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
3
   *
c00c310ea   David Woodhouse   [JFFS2] Tidy up l...
4
   * Copyright © 2006  NEC Corporation
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
5
   *
652ecc20d   KaiGai Kohei   [JFFS2][XATTR] Un...
6
7
8
9
10
   * Created by KaiGai Kohei <kaigai@ak.jp.nec.com>
   *
   * For licensing information, see the file 'LICENCE' in this directory.
   *
   */
c00c310ea   David Woodhouse   [JFFS2] Tidy up l...
11

9bbf29e47   Joe Perches   jffs2: Standardiz...
12
  #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9824f75d5   Jean-Christophe DUBOIS   jffs2: allow to d...
13
  #define JFFS2_XATTR_IS_CORRUPTED	1
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
14
15
16
17
18
19
20
21
22
  #include <linux/kernel.h>
  #include <linux/slab.h>
  #include <linux/fs.h>
  #include <linux/time.h>
  #include <linux/pagemap.h>
  #include <linux/highmem.h>
  #include <linux/crc32.h>
  #include <linux/jffs2.h>
  #include <linux/xattr.h>
f2963d455   Christoph Hellwig   jffs2: use generi...
23
  #include <linux/posix_acl_xattr.h>
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
24
25
26
27
28
29
  #include <linux/mtd/mtd.h>
  #include "nodelist.h"
  /* -------- xdatum related functions ----------------
   * xattr_datum_hashkey(xprefix, xname, xvalue, xsize)
   *   is used to calcurate xdatum hashkey. The reminder of hashkey into XATTRINDEX_HASHSIZE is
   *   the index of the xattr name/value pair cache (c->xattrindex).
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
30
31
32
   * is_xattr_datum_unchecked(c, xd)
   *   returns 1, if xdatum contains any unchecked raw nodes. if all raw nodes are not
   *   unchecked, it returns 0.
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
33
34
35
36
   * unload_xattr_datum(c, xd)
   *   is used to release xattr name/value pair and detach from c->xattrindex.
   * reclaim_xattr_datum(c)
   *   is used to reclaim xattr name/value pairs on the xattr name/value pair cache when
bea931283   Tracey Dent   jffs2: remove a t...
37
   *   memory usage by cache is over c->xdatum_mem_threshold. Currently, this threshold
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
38
   *   is hard coded as 32KiB.
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
39
40
41
42
43
44
45
46
47
48
49
50
51
52
   * do_verify_xattr_datum(c, xd)
   *   is used to load the xdatum informations without name/value pair from the medium.
   *   It's necessary once, because those informations are not collected during mounting
   *   process when EBS is enabled.
   *   0 will be returned, if success. An negative return value means recoverable error, and
   *   positive return value means unrecoverable error. Thus, caller must remove this xdatum
   *   and xref when it returned positive value.
   * do_load_xattr_datum(c, xd)
   *   is used to load name/value pair from the medium.
   *   The meanings of return value is same as do_verify_xattr_datum().
   * load_xattr_datum(c, xd)
   *   is used to be as a wrapper of do_verify_xattr_datum() and do_load_xattr_datum().
   *   If xd need to call do_verify_xattr_datum() at first, it's called before calling
   *   do_load_xattr_datum(). The meanings of return value is same as do_verify_xattr_datum().
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
53
   * save_xattr_datum(c, xd)
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
54
   *   is used to write xdatum to medium. xd->version will be incremented.
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
55
   * create_xattr_datum(c, xprefix, xname, xvalue, xsize)
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
56
   *   is used to create new xdatum and write to medium.
c6e8c6ccf   KaiGai Kohei   [JFFS2][XATTR] Fi...
57
58
59
60
   * unrefer_xattr_datum(c, xd)
   *   is used to delete a xdatum. When nobody refers this xdatum, JFFS2_XFLAGS_DEAD
   *   is set on xd->flags and chained xattr_dead_list or release it immediately.
   *   In the first case, the garbage collector release it later.
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
61
   * -------------------------------------------------- */
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
62
63
64
65
66
67
  static uint32_t xattr_datum_hashkey(int xprefix, const char *xname, const char *xvalue, int xsize)
  {
  	int name_len = strlen(xname);
  
  	return crc32(xprefix, xname, name_len) ^ crc32(xprefix, xvalue, xsize);
  }
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
  static int is_xattr_datum_unchecked(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
  {
  	struct jffs2_raw_node_ref *raw;
  	int rc = 0;
  
  	spin_lock(&c->erase_completion_lock);
  	for (raw=xd->node; raw != (void *)xd; raw=raw->next_in_ino) {
  		if (ref_flags(raw) == REF_UNCHECKED) {
  			rc = 1;
  			break;
  		}
  	}
  	spin_unlock(&c->erase_completion_lock);
  	return rc;
  }
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
83
84
85
  static void unload_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
  {
  	/* must be called under down_write(xattr_sem) */
8e24eea72   Harvey Harrison   fs: replace remai...
86
87
  	D1(dbg_xattr("%s: xid=%u, version=%u
  ", __func__, xd->xid, xd->version));
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
  	if (xd->xname) {
  		c->xdatum_mem_usage -= (xd->name_len + 1 + xd->value_len);
  		kfree(xd->xname);
  	}
  
  	list_del_init(&xd->xindex);
  	xd->hashkey = 0;
  	xd->xname = NULL;
  	xd->xvalue = NULL;
  }
  
  static void reclaim_xattr_datum(struct jffs2_sb_info *c)
  {
  	/* must be called under down_write(xattr_sem) */
  	struct jffs2_xattr_datum *xd, *_xd;
  	uint32_t target, before;
  	static int index = 0;
  	int count;
  
  	if (c->xdatum_mem_threshold > c->xdatum_mem_usage)
  		return;
  
  	before = c->xdatum_mem_usage;
  	target = c->xdatum_mem_usage * 4 / 5; /* 20% reduction */
  	for (count = 0; count < XATTRINDEX_HASHSIZE; count++) {
  		list_for_each_entry_safe(xd, _xd, &c->xattrindex[index], xindex) {
  			if (xd->flags & JFFS2_XFLAGS_HOT) {
  				xd->flags &= ~JFFS2_XFLAGS_HOT;
  			} else if (!(xd->flags & JFFS2_XFLAGS_BIND)) {
  				unload_xattr_datum(c, xd);
  			}
  			if (c->xdatum_mem_usage <= target)
  				goto out;
  		}
  		index = (index+1) % XATTRINDEX_HASHSIZE;
  	}
   out:
  	JFFS2_NOTICE("xdatum_mem_usage from %u byte to %u byte (%u byte reclaimed)
  ",
  		     before, c->xdatum_mem_usage, before - c->xdatum_mem_usage);
  }
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
129
130
131
132
  static int do_verify_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
  {
  	/* must be called under down_write(xattr_sem) */
  	struct jffs2_eraseblock *jeb;
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
133
  	struct jffs2_raw_node_ref *raw;
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
134
135
  	struct jffs2_raw_xattr rx;
  	size_t readlen;
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
136
  	uint32_t crc, offset, totlen;
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
137
  	int rc;
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
138
139
140
141
142
  	spin_lock(&c->erase_completion_lock);
  	offset = ref_offset(xd->node);
  	if (ref_flags(xd->node) == REF_PRISTINE)
  		goto complete;
  	spin_unlock(&c->erase_completion_lock);
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
143

c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
144
  	rc = jffs2_flash_read(c, offset, sizeof(rx), &readlen, (char *)&rx);
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
145
  	if (rc || readlen != sizeof(rx)) {
89291a9d5   David Woodhouse   [JFFS2] Fix 64-bi...
146
147
  		JFFS2_WARNING("jffs2_flash_read()=%d, req=%zu, read=%zu at %#08x
  ",
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
148
  			      rc, sizeof(rx), readlen, offset);
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
149
150
151
152
  		return rc ? rc : -EIO;
  	}
  	crc = crc32(0, &rx, sizeof(rx) - 4);
  	if (crc != je32_to_cpu(rx.node_crc)) {
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
153
154
155
156
  		JFFS2_ERROR("node CRC failed at %#08x, read=%#08x, calc=%#08x
  ",
  			    offset, je32_to_cpu(rx.hdr_crc), crc);
  		xd->flags |= JFFS2_XFLAGS_INVALID;
9824f75d5   Jean-Christophe DUBOIS   jffs2: allow to d...
157
  		return JFFS2_XATTR_IS_CORRUPTED;
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
158
  	}
8a13695cb   KaiGai Kohei   [JFFS2][XATTR] ri...
159
  	totlen = PAD(sizeof(rx) + rx.name_len + 1 + je16_to_cpu(rx.value_len));
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
160
161
162
163
164
165
166
167
  	if (je16_to_cpu(rx.magic) != JFFS2_MAGIC_BITMASK
  	    || je16_to_cpu(rx.nodetype) != JFFS2_NODETYPE_XATTR
  	    || je32_to_cpu(rx.totlen) != totlen
  	    || je32_to_cpu(rx.xid) != xd->xid
  	    || je32_to_cpu(rx.version) != xd->version) {
  		JFFS2_ERROR("inconsistent xdatum at %#08x, magic=%#04x/%#04x, "
  			    "nodetype=%#04x/%#04x, totlen=%u/%u, xid=%u/%u, version=%u/%u
  ",
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
168
  			    offset, je16_to_cpu(rx.magic), JFFS2_MAGIC_BITMASK,
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
169
170
171
172
  			    je16_to_cpu(rx.nodetype), JFFS2_NODETYPE_XATTR,
  			    je32_to_cpu(rx.totlen), totlen,
  			    je32_to_cpu(rx.xid), xd->xid,
  			    je32_to_cpu(rx.version), xd->version);
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
173
  		xd->flags |= JFFS2_XFLAGS_INVALID;
9824f75d5   Jean-Christophe DUBOIS   jffs2: allow to d...
174
  		return JFFS2_XATTR_IS_CORRUPTED;
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
175
176
177
178
179
  	}
  	xd->xprefix = rx.xprefix;
  	xd->name_len = rx.name_len;
  	xd->value_len = je16_to_cpu(rx.value_len);
  	xd->data_crc = je32_to_cpu(rx.data_crc);
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
180
  	spin_lock(&c->erase_completion_lock);
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
181
182
183
184
185
186
187
188
189
190
   complete:
  	for (raw=xd->node; raw != (void *)xd; raw=raw->next_in_ino) {
  		jeb = &c->blocks[ref_offset(raw) / c->sector_size];
  		totlen = PAD(ref_totlen(c, jeb, raw));
  		if (ref_flags(raw) == REF_UNCHECKED) {
  			c->unchecked_size -= totlen; c->used_size += totlen;
  			jeb->unchecked_size -= totlen; jeb->used_size += totlen;
  		}
  		raw->flash_offset = ref_offset(raw) | ((xd->node==raw) ? REF_PRISTINE : REF_NORMAL);
  	}
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
191
192
193
194
  	spin_unlock(&c->erase_completion_lock);
  
  	/* unchecked xdatum is chained with c->xattr_unchecked */
  	list_del_init(&xd->xindex);
f42cf8d6a   Masanari Iida   treewide: Fix typ...
195
196
  	dbg_xattr("success on verifying xdatum (xid=%u, version=%u)
  ",
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
197
198
199
200
201
202
203
204
205
206
207
208
  		  xd->xid, xd->version);
  
  	return 0;
  }
  
  static int do_load_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
  {
  	/* must be called under down_write(xattr_sem) */
  	char *data;
  	size_t readlen;
  	uint32_t crc, length;
  	int i, ret, retry = 0;
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
209
210
211
212
213
214
215
216
217
218
219
220
  	BUG_ON(ref_flags(xd->node) != REF_PRISTINE);
  	BUG_ON(!list_empty(&xd->xindex));
   retry:
  	length = xd->name_len + 1 + xd->value_len;
  	data = kmalloc(length, GFP_KERNEL);
  	if (!data)
  		return -ENOMEM;
  
  	ret = jffs2_flash_read(c, ref_offset(xd->node)+sizeof(struct jffs2_raw_xattr),
  			       length, &readlen, data);
  
  	if (ret || length!=readlen) {
89291a9d5   David Woodhouse   [JFFS2] Fix 64-bi...
221
222
  		JFFS2_WARNING("jffs2_flash_read() returned %d, request=%d, readlen=%zu, at %#08x
  ",
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
223
224
225
226
227
228
229
230
  			      ret, length, readlen, ref_offset(xd->node));
  		kfree(data);
  		return ret ? ret : -EIO;
  	}
  
  	data[xd->name_len] = '\0';
  	crc = crc32(0, data, length);
  	if (crc != xd->data_crc) {
9824f75d5   Jean-Christophe DUBOIS   jffs2: allow to d...
231
  		JFFS2_WARNING("node CRC failed (JFFS2_NODETYPE_XATTR)"
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
232
233
234
235
  			      " at %#08x, read: 0x%08x calculated: 0x%08x
  ",
  			      ref_offset(xd->node), xd->data_crc, crc);
  		kfree(data);
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
236
  		xd->flags |= JFFS2_XFLAGS_INVALID;
9824f75d5   Jean-Christophe DUBOIS   jffs2: allow to d...
237
  		return JFFS2_XATTR_IS_CORRUPTED;
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
  	}
  
  	xd->flags |= JFFS2_XFLAGS_HOT;
  	xd->xname = data;
  	xd->xvalue = data + xd->name_len+1;
  
  	c->xdatum_mem_usage += length;
  
  	xd->hashkey = xattr_datum_hashkey(xd->xprefix, xd->xname, xd->xvalue, xd->value_len);
  	i = xd->hashkey % XATTRINDEX_HASHSIZE;
  	list_add(&xd->xindex, &c->xattrindex[i]);
  	if (!retry) {
  		retry = 1;
  		reclaim_xattr_datum(c);
  		if (!xd->xname)
  			goto retry;
  	}
  
  	dbg_xattr("success on loading xdatum (xid=%u, xprefix=%u, xname='%s')
  ",
  		  xd->xid, xd->xprefix, xd->xname);
  
  	return 0;
  }
  
  static int load_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
  {
  	/* must be called under down_write(xattr_sem);
  	 * rc < 0 : recoverable error, try again
  	 * rc = 0 : success
  	 * rc > 0 : Unrecoverable error, this node should be deleted.
  	 */
  	int rc = 0;
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
271

8a13695cb   KaiGai Kohei   [JFFS2][XATTR] ri...
272
  	BUG_ON(xd->flags & JFFS2_XFLAGS_DEAD);
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
273
274
275
  	if (xd->xname)
  		return 0;
  	if (xd->flags & JFFS2_XFLAGS_INVALID)
9824f75d5   Jean-Christophe DUBOIS   jffs2: allow to d...
276
  		return JFFS2_XATTR_IS_CORRUPTED;
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
277
  	if (unlikely(is_xattr_datum_unchecked(c, xd)))
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
278
  		rc = do_verify_xattr_datum(c, xd);
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
279
280
281
282
  	if (!rc)
  		rc = do_load_xattr_datum(c, xd);
  	return rc;
  }
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
283
  static int save_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
284
285
  {
  	/* must be called under down_write(xattr_sem) */
2f785402f   David Woodhouse   [JFFS2] Reduce vi...
286
  	struct jffs2_raw_xattr rx;
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
287
  	struct kvec vecs[2];
89291a9d5   David Woodhouse   [JFFS2] Fix 64-bi...
288
  	size_t length;
8a13695cb   KaiGai Kohei   [JFFS2][XATTR] ri...
289
  	int rc, totlen;
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
290
  	uint32_t phys_ofs = write_ofs(c);
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
291

8a13695cb   KaiGai Kohei   [JFFS2][XATTR] ri...
292
293
  	BUG_ON(!xd->xname);
  	BUG_ON(xd->flags & (JFFS2_XFLAGS_DEAD|JFFS2_XFLAGS_INVALID));
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
294
295
  
  	vecs[0].iov_base = &rx;
8a13695cb   KaiGai Kohei   [JFFS2][XATTR] ri...
296
297
298
299
  	vecs[0].iov_len = sizeof(rx);
  	vecs[1].iov_base = xd->xname;
  	vecs[1].iov_len = xd->name_len + 1 + xd->value_len;
  	totlen = vecs[0].iov_len + vecs[1].iov_len;
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
300
  	/* Setup raw-xattr */
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
301
  	memset(&rx, 0, sizeof(rx));
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
302
303
304
305
306
307
  	rx.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
  	rx.nodetype = cpu_to_je16(JFFS2_NODETYPE_XATTR);
  	rx.totlen = cpu_to_je32(PAD(totlen));
  	rx.hdr_crc = cpu_to_je32(crc32(0, &rx, sizeof(struct jffs2_unknown_node) - 4));
  
  	rx.xid = cpu_to_je32(xd->xid);
8a13695cb   KaiGai Kohei   [JFFS2][XATTR] ri...
308
309
310
311
312
  	rx.version = cpu_to_je32(++xd->version);
  	rx.xprefix = xd->xprefix;
  	rx.name_len = xd->name_len;
  	rx.value_len = cpu_to_je16(xd->value_len);
  	rx.data_crc = cpu_to_je32(crc32(0, vecs[1].iov_base, vecs[1].iov_len));
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
313
  	rx.node_crc = cpu_to_je32(crc32(0, &rx, sizeof(struct jffs2_raw_xattr) - 4));
8a13695cb   KaiGai Kohei   [JFFS2][XATTR] ri...
314
  	rc = jffs2_flash_writev(c, vecs, 2, phys_ofs, &length, 0);
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
315
  	if (rc || totlen != length) {
89291a9d5   David Woodhouse   [JFFS2] Fix 64-bi...
316
317
  		JFFS2_WARNING("jffs2_flash_writev()=%d, req=%u, wrote=%zu, at %#08x
  ",
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
318
319
  			      rc, totlen, length, phys_ofs);
  		rc = rc ? rc : -EIO;
2f785402f   David Woodhouse   [JFFS2] Reduce vi...
320
321
  		if (length)
  			jffs2_add_physical_node_ref(c, phys_ofs | REF_OBSOLETE, PAD(totlen), NULL);
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
322
323
  		return rc;
  	}
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
324
  	/* success */
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
325
  	jffs2_add_physical_node_ref(c, phys_ofs | REF_PRISTINE, PAD(totlen), (void *)xd);
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
326
327
328
329
330
331
332
333
334
335
  
  	dbg_xattr("success on saving xdatum (xid=%u, version=%u, xprefix=%u, xname='%s')
  ",
  		  xd->xid, xd->version, xd->xprefix, xd->xname);
  
  	return 0;
  }
  
  static struct jffs2_xattr_datum *create_xattr_datum(struct jffs2_sb_info *c,
  						    int xprefix, const char *xname,
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
336
  						    const char *xvalue, int xsize)
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
  {
  	/* must be called under down_write(xattr_sem) */
  	struct jffs2_xattr_datum *xd;
  	uint32_t hashkey, name_len;
  	char *data;
  	int i, rc;
  
  	/* Search xattr_datum has same xname/xvalue by index */
  	hashkey = xattr_datum_hashkey(xprefix, xname, xvalue, xsize);
  	i = hashkey % XATTRINDEX_HASHSIZE;
  	list_for_each_entry(xd, &c->xattrindex[i], xindex) {
  		if (xd->hashkey==hashkey
  		    && xd->xprefix==xprefix
  		    && xd->value_len==xsize
  		    && !strcmp(xd->xname, xname)
  		    && !memcmp(xd->xvalue, xvalue, xsize)) {
2c887e235   KaiGai Kohei   [JFFS2][XATTR] Re...
353
  			atomic_inc(&xd->refcnt);
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
  			return xd;
  		}
  	}
  
  	/* Not found, Create NEW XATTR-Cache */
  	name_len = strlen(xname);
  
  	xd = jffs2_alloc_xattr_datum();
  	if (!xd)
  		return ERR_PTR(-ENOMEM);
  
  	data = kmalloc(name_len + 1 + xsize, GFP_KERNEL);
  	if (!data) {
  		jffs2_free_xattr_datum(xd);
  		return ERR_PTR(-ENOMEM);
  	}
  	strcpy(data, xname);
  	memcpy(data + name_len + 1, xvalue, xsize);
2c887e235   KaiGai Kohei   [JFFS2][XATTR] Re...
372
  	atomic_set(&xd->refcnt, 1);
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
373
374
375
376
377
378
379
380
381
382
  	xd->xid = ++c->highest_xid;
  	xd->flags |= JFFS2_XFLAGS_HOT;
  	xd->xprefix = xprefix;
  
  	xd->hashkey = hashkey;
  	xd->xname = data;
  	xd->xvalue = data + name_len + 1;
  	xd->name_len = name_len;
  	xd->value_len = xsize;
  	xd->data_crc = crc32(0, data, xd->name_len + 1 + xd->value_len);
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
383
  	rc = save_xattr_datum(c, xd);
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
  	if (rc) {
  		kfree(xd->xname);
  		jffs2_free_xattr_datum(xd);
  		return ERR_PTR(rc);
  	}
  
  	/* Insert Hash Index */
  	i = hashkey % XATTRINDEX_HASHSIZE;
  	list_add(&xd->xindex, &c->xattrindex[i]);
  
  	c->xdatum_mem_usage += (xd->name_len + 1 + xd->value_len);
  	reclaim_xattr_datum(c);
  
  	return xd;
  }
c6e8c6ccf   KaiGai Kohei   [JFFS2][XATTR] Fi...
399
  static void unrefer_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
400
401
  {
  	/* must be called under down_write(xattr_sem) */
c6e8c6ccf   KaiGai Kohei   [JFFS2][XATTR] Fi...
402
  	if (atomic_dec_and_lock(&xd->refcnt, &c->erase_completion_lock)) {
c6e8c6ccf   KaiGai Kohei   [JFFS2][XATTR] Fi...
403
404
405
406
407
408
409
410
411
  		unload_xattr_datum(c, xd);
  		xd->flags |= JFFS2_XFLAGS_DEAD;
  		if (xd->node == (void *)xd) {
  			BUG_ON(!(xd->flags & JFFS2_XFLAGS_INVALID));
  			jffs2_free_xattr_datum(xd);
  		} else {
  			list_add(&xd->xindex, &c->xattr_dead_list);
  		}
  		spin_unlock(&c->erase_completion_lock);
a6b1d82d0   Jeff Garzik   [JFFS2] kill warn...
412
413
414
  		dbg_xattr("xdatum(xid=%u, version=%u) was removed.
  ",
  			  xd->xid, xd->version);
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
415
  	}
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
416
  }
21b9879bf   KaiGai Kohei   [JFFS2][XATTR] Fi...
417
  /* -------- xref related functions ------------------
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
418
419
420
   * verify_xattr_ref(c, ref)
   *   is used to load xref information from medium. Because summary data does not
   *   contain xid/ino, it's necessary to verify once while mounting process.
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
421
   * save_xattr_ref(c, ref)
8a13695cb   KaiGai Kohei   [JFFS2][XATTR] ri...
422
423
   *   is used to write xref to medium. If delete marker is marked, it write
   *   a delete marker of xref into medium.
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
424
   * create_xattr_ref(c, ic, xd)
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
425
   *   is used to create a new xref and write to medium.
8a13695cb   KaiGai Kohei   [JFFS2][XATTR] ri...
426
427
428
   * delete_xattr_ref(c, ref)
   *   is used to delete jffs2_xattr_ref. It marks xref XREF_DELETE_MARKER,
   *   and allows GC to reclaim those physical nodes.
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
429
430
431
432
   * jffs2_xattr_delete_inode(c, ic)
   *   is called to remove xrefs related to obsolete inode when inode is unlinked.
   * jffs2_xattr_free_inode(c, ic)
   *   is called to release xattr related objects when unmounting. 
8f2b6f49c   KaiGai Kohei   [JFFS2][XATTR] Re...
433
   * check_xattr_ref_inode(c, ic)
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
434
   *   is used to confirm inode does not have duplicate xattr name/value pair.
8c5a03664   Jean-Christophe DUBOIS   jffs2: allow to c...
435
436
   * jffs2_xattr_do_crccheck_inode(c, ic)
   *   is used to force xattr data integrity check during the initial gc scan.
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
437
438
439
440
   * -------------------------------------------------- */
  static int verify_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
  {
  	struct jffs2_eraseblock *jeb;
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
441
  	struct jffs2_raw_node_ref *raw;
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
442
443
  	struct jffs2_raw_xref rr;
  	size_t readlen;
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
444
  	uint32_t crc, offset, totlen;
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
445
  	int rc;
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
446
447
448
449
450
  	spin_lock(&c->erase_completion_lock);
  	if (ref_flags(ref->node) != REF_UNCHECKED)
  		goto complete;
  	offset = ref_offset(ref->node);
  	spin_unlock(&c->erase_completion_lock);
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
451

c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
452
  	rc = jffs2_flash_read(c, offset, sizeof(rr), &readlen, (char *)&rr);
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
453
  	if (rc || sizeof(rr) != readlen) {
89291a9d5   David Woodhouse   [JFFS2] Fix 64-bi...
454
455
  		JFFS2_WARNING("jffs2_flash_read()=%d, req=%zu, read=%zu, at %#08x
  ",
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
456
  			      rc, sizeof(rr), readlen, offset);
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
457
458
459
460
461
  		return rc ? rc : -EIO;
  	}
  	/* obsolete node */
  	crc = crc32(0, &rr, sizeof(rr) - 4);
  	if (crc != je32_to_cpu(rr.node_crc)) {
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
462
463
464
  		JFFS2_ERROR("node CRC failed at %#08x, read=%#08x, calc=%#08x
  ",
  			    offset, je32_to_cpu(rr.node_crc), crc);
9824f75d5   Jean-Christophe DUBOIS   jffs2: allow to d...
465
  		return JFFS2_XATTR_IS_CORRUPTED;
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
466
467
468
469
470
  	}
  	if (je16_to_cpu(rr.magic) != JFFS2_MAGIC_BITMASK
  	    || je16_to_cpu(rr.nodetype) != JFFS2_NODETYPE_XREF
  	    || je32_to_cpu(rr.totlen) != PAD(sizeof(rr))) {
  		JFFS2_ERROR("inconsistent xref at %#08x, magic=%#04x/%#04x, "
89291a9d5   David Woodhouse   [JFFS2] Fix 64-bi...
471
472
  			    "nodetype=%#04x/%#04x, totlen=%u/%zu
  ",
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
473
  			    offset, je16_to_cpu(rr.magic), JFFS2_MAGIC_BITMASK,
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
474
475
  			    je16_to_cpu(rr.nodetype), JFFS2_NODETYPE_XREF,
  			    je32_to_cpu(rr.totlen), PAD(sizeof(rr)));
9824f75d5   Jean-Christophe DUBOIS   jffs2: allow to d...
476
  		return JFFS2_XATTR_IS_CORRUPTED;
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
477
478
479
  	}
  	ref->ino = je32_to_cpu(rr.ino);
  	ref->xid = je32_to_cpu(rr.xid);
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
480
481
482
  	ref->xseqno = je32_to_cpu(rr.xseqno);
  	if (ref->xseqno > c->highest_xseqno)
  		c->highest_xseqno = (ref->xseqno & ~XREF_DELETE_MARKER);
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
483
484
  
  	spin_lock(&c->erase_completion_lock);
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
485
486
487
488
489
490
491
492
493
494
   complete:
  	for (raw=ref->node; raw != (void *)ref; raw=raw->next_in_ino) {
  		jeb = &c->blocks[ref_offset(raw) / c->sector_size];
  		totlen = PAD(ref_totlen(c, jeb, raw));
  		if (ref_flags(raw) == REF_UNCHECKED) {
  			c->unchecked_size -= totlen; c->used_size += totlen;
  			jeb->unchecked_size -= totlen; jeb->used_size += totlen;
  		}
  		raw->flash_offset = ref_offset(raw) | ((ref->node==raw) ? REF_PRISTINE : REF_NORMAL);
  	}
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
495
496
497
498
499
500
501
  	spin_unlock(&c->erase_completion_lock);
  
  	dbg_xattr("success on verifying xref (ino=%u, xid=%u) at %#08x
  ",
  		  ref->ino, ref->xid, ref_offset(ref->node));
  	return 0;
  }
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
502
  static int save_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
503
504
  {
  	/* must be called under down_write(xattr_sem) */
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
505
  	struct jffs2_raw_xref rr;
89291a9d5   David Woodhouse   [JFFS2] Fix 64-bi...
506
  	size_t length;
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
507
  	uint32_t xseqno, phys_ofs = write_ofs(c);
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
508
  	int ret;
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
509
510
511
512
  	rr.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
  	rr.nodetype = cpu_to_je16(JFFS2_NODETYPE_XREF);
  	rr.totlen = cpu_to_je32(PAD(sizeof(rr)));
  	rr.hdr_crc = cpu_to_je32(crc32(0, &rr, sizeof(struct jffs2_unknown_node) - 4));
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
513
514
515
516
517
518
519
520
521
522
  	xseqno = (c->highest_xseqno += 2);
  	if (is_xattr_ref_dead(ref)) {
  		xseqno |= XREF_DELETE_MARKER;
  		rr.ino = cpu_to_je32(ref->ino);
  		rr.xid = cpu_to_je32(ref->xid);
  	} else {
  		rr.ino = cpu_to_je32(ref->ic->ino);
  		rr.xid = cpu_to_je32(ref->xd->xid);
  	}
  	rr.xseqno = cpu_to_je32(xseqno);
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
523
524
525
526
  	rr.node_crc = cpu_to_je32(crc32(0, &rr, sizeof(rr) - 4));
  
  	ret = jffs2_flash_write(c, phys_ofs, sizeof(rr), &length, (char *)&rr);
  	if (ret || sizeof(rr) != length) {
89291a9d5   David Woodhouse   [JFFS2] Fix 64-bi...
527
528
  		JFFS2_WARNING("jffs2_flash_write() returned %d, request=%zu, retlen=%zu, at %#08x
  ",
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
529
530
  			      ret, sizeof(rr), length, phys_ofs);
  		ret = ret ? ret : -EIO;
2f785402f   David Woodhouse   [JFFS2] Reduce vi...
531
532
  		if (length)
  			jffs2_add_physical_node_ref(c, phys_ofs | REF_OBSOLETE, PAD(sizeof(rr)), NULL);
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
533
534
  		return ret;
  	}
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
535
536
537
  	/* success */
  	ref->xseqno = xseqno;
  	jffs2_add_physical_node_ref(c, phys_ofs | REF_PRISTINE, PAD(sizeof(rr)), (void *)ref);
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
538
539
540
541
542
543
544
545
  
  	dbg_xattr("success on saving xref (ino=%u, xid=%u)
  ", ref->ic->ino, ref->xd->xid);
  
  	return 0;
  }
  
  static struct jffs2_xattr_ref *create_xattr_ref(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic,
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
546
  						struct jffs2_xattr_datum *xd)
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
547
548
549
550
551
552
553
554
555
556
  {
  	/* must be called under down_write(xattr_sem) */
  	struct jffs2_xattr_ref *ref;
  	int ret;
  
  	ref = jffs2_alloc_xattr_ref();
  	if (!ref)
  		return ERR_PTR(-ENOMEM);
  	ref->ic = ic;
  	ref->xd = xd;
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
557
  	ret = save_xattr_ref(c, ref);
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
558
559
560
561
562
563
  	if (ret) {
  		jffs2_free_xattr_ref(ref);
  		return ERR_PTR(ret);
  	}
  
  	/* Chain to inode */
8f2b6f49c   KaiGai Kohei   [JFFS2][XATTR] Re...
564
565
  	ref->next = ic->xref;
  	ic->xref = ref;
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
566
567
568
  
  	return ref; /* success */
  }
8a13695cb   KaiGai Kohei   [JFFS2][XATTR] ri...
569
  static void delete_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
570
571
572
  {
  	/* must be called under down_write(xattr_sem) */
  	struct jffs2_xattr_datum *xd;
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
573
  	xd = ref->xd;
8a13695cb   KaiGai Kohei   [JFFS2][XATTR] ri...
574
  	ref->xseqno |= XREF_DELETE_MARKER;
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
575
576
577
578
579
580
  	ref->ino = ref->ic->ino;
  	ref->xid = ref->xd->xid;
  	spin_lock(&c->erase_completion_lock);
  	ref->next = c->xref_dead_list;
  	c->xref_dead_list = ref;
  	spin_unlock(&c->erase_completion_lock);
8a13695cb   KaiGai Kohei   [JFFS2][XATTR] ri...
581
582
583
  	dbg_xattr("xref(ino=%u, xid=%u, xseqno=%u) was removed.
  ",
  		  ref->ino, ref->xid, ref->xseqno);
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
584

c6e8c6ccf   KaiGai Kohei   [JFFS2][XATTR] Fi...
585
  	unrefer_xattr_datum(c, xd);
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
586
  }
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
587
588
  void jffs2_xattr_delete_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
  {
b57922d97   Al Viro   convert remaining...
589
  	/* It's called from jffs2_evict_inode() on inode removing.
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
590
  	   When an inode with XATTR is removed, those XATTRs must be removed. */
8a13695cb   KaiGai Kohei   [JFFS2][XATTR] ri...
591
  	struct jffs2_xattr_ref *ref, *_ref;
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
592

27c72b040   David Woodhouse   [JFFS2] Track par...
593
  	if (!ic || ic->pino_nlink > 0)
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
594
595
596
  		return;
  
  	down_write(&c->xattr_sem);
8a13695cb   KaiGai Kohei   [JFFS2][XATTR] ri...
597
598
599
  	for (ref = ic->xref; ref; ref = _ref) {
  		_ref = ref->next;
  		delete_xattr_ref(c, ref);
8f2b6f49c   KaiGai Kohei   [JFFS2][XATTR] Re...
600
  	}
8a13695cb   KaiGai Kohei   [JFFS2][XATTR] ri...
601
  	ic->xref = NULL;
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
602
603
604
605
606
607
608
609
610
611
  	up_write(&c->xattr_sem);
  }
  
  void jffs2_xattr_free_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
  {
  	/* It's called from jffs2_free_ino_caches() until unmounting FS. */
  	struct jffs2_xattr_datum *xd;
  	struct jffs2_xattr_ref *ref, *_ref;
  
  	down_write(&c->xattr_sem);
8f2b6f49c   KaiGai Kohei   [JFFS2][XATTR] Re...
612
613
  	for (ref = ic->xref; ref; ref = _ref) {
  		_ref = ref->next;
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
614
  		xd = ref->xd;
2c887e235   KaiGai Kohei   [JFFS2][XATTR] Re...
615
  		if (atomic_dec_and_test(&xd->refcnt)) {
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
616
617
618
619
620
  			unload_xattr_datum(c, xd);
  			jffs2_free_xattr_datum(xd);
  		}
  		jffs2_free_xattr_ref(ref);
  	}
8f2b6f49c   KaiGai Kohei   [JFFS2][XATTR] Re...
621
  	ic->xref = NULL;
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
622
623
  	up_write(&c->xattr_sem);
  }
8f2b6f49c   KaiGai Kohei   [JFFS2][XATTR] Re...
624
  static int check_xattr_ref_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
625
  {
a4ce96ac3   Linus Torvalds   Fix up trivial sp...
626
  	/* success of check_xattr_ref_inode() means that inode (ic) dose not have
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
627
628
629
  	 * duplicate name/value pairs. If duplicate name/value pair would be found,
  	 * one will be removed.
  	 */
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
630
  	struct jffs2_xattr_ref *ref, *cmp, **pref, **pcmp;
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
631
632
633
634
635
636
637
  	int rc = 0;
  
  	if (likely(ic->flags & INO_FLAGS_XATTR_CHECKED))
  		return 0;
  	down_write(&c->xattr_sem);
   retry:
  	rc = 0;
8f2b6f49c   KaiGai Kohei   [JFFS2][XATTR] Re...
638
  	for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
639
640
641
  		if (!ref->xd->xname) {
  			rc = load_xattr_datum(c, ref->xd);
  			if (unlikely(rc > 0)) {
8f2b6f49c   KaiGai Kohei   [JFFS2][XATTR] Re...
642
  				*pref = ref->next;
8a13695cb   KaiGai Kohei   [JFFS2][XATTR] ri...
643
  				delete_xattr_ref(c, ref);
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
644
645
646
647
  				goto retry;
  			} else if (unlikely(rc < 0))
  				goto out;
  		}
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
648
  		for (cmp=ref->next, pcmp=&ref->next; cmp; pcmp=&cmp->next, cmp=cmp->next) {
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
649
650
651
652
653
  			if (!cmp->xd->xname) {
  				ref->xd->flags |= JFFS2_XFLAGS_BIND;
  				rc = load_xattr_datum(c, cmp->xd);
  				ref->xd->flags &= ~JFFS2_XFLAGS_BIND;
  				if (unlikely(rc > 0)) {
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
654
  					*pcmp = cmp->next;
8a13695cb   KaiGai Kohei   [JFFS2][XATTR] ri...
655
  					delete_xattr_ref(c, cmp);
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
656
657
658
659
660
661
  					goto retry;
  				} else if (unlikely(rc < 0))
  					goto out;
  			}
  			if (ref->xd->xprefix == cmp->xd->xprefix
  			    && !strcmp(ref->xd->xname, cmp->xd->xname)) {
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
662
663
  				if (ref->xseqno > cmp->xseqno) {
  					*pcmp = cmp->next;
8a13695cb   KaiGai Kohei   [JFFS2][XATTR] ri...
664
  					delete_xattr_ref(c, cmp);
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
665
666
  				} else {
  					*pref = ref->next;
8a13695cb   KaiGai Kohei   [JFFS2][XATTR] ri...
667
  					delete_xattr_ref(c, ref);
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
668
  				}
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
669
670
671
672
673
674
675
676
677
678
  				goto retry;
  			}
  		}
  	}
  	ic->flags |= INO_FLAGS_XATTR_CHECKED;
   out:
  	up_write(&c->xattr_sem);
  
  	return rc;
  }
8c5a03664   Jean-Christophe DUBOIS   jffs2: allow to c...
679
680
681
682
  void jffs2_xattr_do_crccheck_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
  {
  	check_xattr_ref_inode(c, ic);
  }
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
  /* -------- xattr subsystem functions ---------------
   * jffs2_init_xattr_subsystem(c)
   *   is used to initialize semaphore and list_head, and some variables.
   * jffs2_find_xattr_datum(c, xid)
   *   is used to lookup xdatum while scanning process.
   * jffs2_clear_xattr_subsystem(c)
   *   is used to release any xattr related objects.
   * jffs2_build_xattr_subsystem(c)
   *   is used to associate xdatum and xref while super block building process.
   * jffs2_setup_xattr_datum(c, xid, version)
   *   is used to insert xdatum while scanning process.
   * -------------------------------------------------- */
  void jffs2_init_xattr_subsystem(struct jffs2_sb_info *c)
  {
  	int i;
  
  	for (i=0; i < XATTRINDEX_HASHSIZE; i++)
  		INIT_LIST_HEAD(&c->xattrindex[i]);
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
701
  	INIT_LIST_HEAD(&c->xattr_unchecked);
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
702
703
  	INIT_LIST_HEAD(&c->xattr_dead_list);
  	c->xref_dead_list = NULL;
8f2b6f49c   KaiGai Kohei   [JFFS2][XATTR] Re...
704
  	c->xref_temp = NULL;
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
705
706
  
  	init_rwsem(&c->xattr_sem);
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
707
708
  	c->highest_xid = 0;
  	c->highest_xseqno = 0;
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
  	c->xdatum_mem_usage = 0;
  	c->xdatum_mem_threshold = 32 * 1024;	/* Default 32KB */
  }
  
  static struct jffs2_xattr_datum *jffs2_find_xattr_datum(struct jffs2_sb_info *c, uint32_t xid)
  {
  	struct jffs2_xattr_datum *xd;
  	int i = xid % XATTRINDEX_HASHSIZE;
  
  	/* It's only used in scanning/building process. */
  	BUG_ON(!(c->flags & (JFFS2_SB_FLAG_SCANNING|JFFS2_SB_FLAG_BUILDING)));
  
  	list_for_each_entry(xd, &c->xattrindex[i], xindex) {
  		if (xd->xid==xid)
  			return xd;
  	}
  	return NULL;
  }
  
  void jffs2_clear_xattr_subsystem(struct jffs2_sb_info *c)
  {
  	struct jffs2_xattr_datum *xd, *_xd;
  	struct jffs2_xattr_ref *ref, *_ref;
  	int i;
8f2b6f49c   KaiGai Kohei   [JFFS2][XATTR] Re...
733
734
  	for (ref=c->xref_temp; ref; ref = _ref) {
  		_ref = ref->next;
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
735
  		jffs2_free_xattr_ref(ref);
8f2b6f49c   KaiGai Kohei   [JFFS2][XATTR] Re...
736
  	}
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
737
738
739
740
741
  
  	for (ref=c->xref_dead_list; ref; ref = _ref) {
  		_ref = ref->next;
  		jffs2_free_xattr_ref(ref);
  	}
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
742
743
744
745
  
  	for (i=0; i < XATTRINDEX_HASHSIZE; i++) {
  		list_for_each_entry_safe(xd, _xd, &c->xattrindex[i], xindex) {
  			list_del(&xd->xindex);
086f2f76f   Fabian Frederick   fs/jffs2/xattr.c:...
746
  			kfree(xd->xname);
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
747
748
749
  			jffs2_free_xattr_datum(xd);
  		}
  	}
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
750
751
752
753
754
  
  	list_for_each_entry_safe(xd, _xd, &c->xattr_dead_list, xindex) {
  		list_del(&xd->xindex);
  		jffs2_free_xattr_datum(xd);
  	}
2ad8ee713   David Woodhouse   [JFFS2] Fix poten...
755
756
757
758
  	list_for_each_entry_safe(xd, _xd, &c->xattr_unchecked, xindex) {
  		list_del(&xd->xindex);
  		jffs2_free_xattr_datum(xd);
  	}
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
759
  }
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
760
  #define XREF_TMPHASH_SIZE	(128)
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
761
762
763
  void jffs2_build_xattr_subsystem(struct jffs2_sb_info *c)
  {
  	struct jffs2_xattr_ref *ref, *_ref;
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
764
  	struct jffs2_xattr_ref *xref_tmphash[XREF_TMPHASH_SIZE];
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
765
766
  	struct jffs2_xattr_datum *xd, *_xd;
  	struct jffs2_inode_cache *ic;
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
767
768
  	struct jffs2_raw_node_ref *raw;
  	int i, xdatum_count = 0, xdatum_unchecked_count = 0, xref_count = 0;
8a13695cb   KaiGai Kohei   [JFFS2][XATTR] ri...
769
  	int xdatum_orphan_count = 0, xref_orphan_count = 0, xref_dead_count = 0;
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
770
771
  
  	BUG_ON(!(c->flags & JFFS2_SB_FLAG_BUILDING));
8a13695cb   KaiGai Kohei   [JFFS2][XATTR] ri...
772
  	/* Phase.1 : Merge same xref */
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
773
774
  	for (i=0; i < XREF_TMPHASH_SIZE; i++)
  		xref_tmphash[i] = NULL;
8f2b6f49c   KaiGai Kohei   [JFFS2][XATTR] Re...
775
  	for (ref=c->xref_temp; ref; ref=_ref) {
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
776
  		struct jffs2_xattr_ref *tmp;
8f2b6f49c   KaiGai Kohei   [JFFS2][XATTR] Re...
777
  		_ref = ref->next;
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
778
779
  		if (ref_flags(ref->node) != REF_PRISTINE) {
  			if (verify_xattr_ref(c, ref)) {
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
780
781
782
  				BUG_ON(ref->node->next_in_ino != (void *)ref);
  				ref->node->next_in_ino = NULL;
  				jffs2_mark_node_obsolete(c, ref->node);
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
783
784
785
786
  				jffs2_free_xattr_ref(ref);
  				continue;
  			}
  		}
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
  
  		i = (ref->ino ^ ref->xid) % XREF_TMPHASH_SIZE;
  		for (tmp=xref_tmphash[i]; tmp; tmp=tmp->next) {
  			if (tmp->ino == ref->ino && tmp->xid == ref->xid)
  				break;
  		}
  		if (tmp) {
  			raw = ref->node;
  			if (ref->xseqno > tmp->xseqno) {
  				tmp->xseqno = ref->xseqno;
  				raw->next_in_ino = tmp->node;
  				tmp->node = raw;
  			} else {
  				raw->next_in_ino = tmp->node->next_in_ino;
  				tmp->node->next_in_ino = raw;
  			}
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
803
804
  			jffs2_free_xattr_ref(ref);
  			continue;
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
805
806
807
  		} else {
  			ref->next = xref_tmphash[i];
  			xref_tmphash[i] = ref;
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
808
  		}
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
809
  	}
8f2b6f49c   KaiGai Kohei   [JFFS2][XATTR] Re...
810
  	c->xref_temp = NULL;
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
811

8a13695cb   KaiGai Kohei   [JFFS2][XATTR] ri...
812
  	/* Phase.2 : Bind xref with inode_cache and xattr_datum */
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
813
814
  	for (i=0; i < XREF_TMPHASH_SIZE; i++) {
  		for (ref=xref_tmphash[i]; ref; ref=_ref) {
8a13695cb   KaiGai Kohei   [JFFS2][XATTR] ri...
815
  			xref_count++;
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
816
817
818
819
  			_ref = ref->next;
  			if (is_xattr_ref_dead(ref)) {
  				ref->next = c->xref_dead_list;
  				c->xref_dead_list = ref;
8a13695cb   KaiGai Kohei   [JFFS2][XATTR] ri...
820
  				xref_dead_count++;
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
821
822
823
824
825
826
  				continue;
  			}
  			/* At this point, ref->xid and ref->ino contain XID and inode number.
  			   ref->xd and ref->ic are not valid yet. */
  			xd = jffs2_find_xattr_datum(c, ref->xid);
  			ic = jffs2_get_ino_cache(c, ref->ino);
27c72b040   David Woodhouse   [JFFS2] Track par...
827
  			if (!xd || !ic || !ic->pino_nlink) {
8a13695cb   KaiGai Kohei   [JFFS2][XATTR] ri...
828
829
830
831
  				dbg_xattr("xref(ino=%u, xid=%u, xseqno=%u) is orphan.
  ",
  					  ref->ino, ref->xid, ref->xseqno);
  				ref->xseqno |= XREF_DELETE_MARKER;
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
832
833
  				ref->next = c->xref_dead_list;
  				c->xref_dead_list = ref;
8a13695cb   KaiGai Kohei   [JFFS2][XATTR] ri...
834
  				xref_orphan_count++;
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
835
836
837
838
  				continue;
  			}
  			ref->xd = xd;
  			ref->ic = ic;
2c887e235   KaiGai Kohei   [JFFS2][XATTR] Re...
839
  			atomic_inc(&xd->refcnt);
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
840
841
  			ref->next = ic->xref;
  			ic->xref = ref;
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
842
843
  		}
  	}
8a13695cb   KaiGai Kohei   [JFFS2][XATTR] ri...
844
  	/* Phase.3 : Link unchecked xdatum to xattr_unchecked list */
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
845
846
  	for (i=0; i < XATTRINDEX_HASHSIZE; i++) {
  		list_for_each_entry_safe(xd, _xd, &c->xattrindex[i], xindex) {
8a13695cb   KaiGai Kohei   [JFFS2][XATTR] ri...
847
  			xdatum_count++;
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
848
  			list_del_init(&xd->xindex);
2c887e235   KaiGai Kohei   [JFFS2][XATTR] Re...
849
  			if (!atomic_read(&xd->refcnt)) {
8a13695cb   KaiGai Kohei   [JFFS2][XATTR] ri...
850
851
852
853
  				dbg_xattr("xdatum(xid=%u, version=%u) is orphan.
  ",
  					  xd->xid, xd->version);
  				xd->flags |= JFFS2_XFLAGS_DEAD;
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
854
  				list_add(&xd->xindex, &c->xattr_unchecked);
8a13695cb   KaiGai Kohei   [JFFS2][XATTR] ri...
855
  				xdatum_orphan_count++;
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
856
857
  				continue;
  			}
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
858
859
860
861
  			if (is_xattr_datum_unchecked(c, xd)) {
  				dbg_xattr("unchecked xdatum(xid=%u, version=%u)
  ",
  					  xd->xid, xd->version);
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
862
863
864
  				list_add(&xd->xindex, &c->xattr_unchecked);
  				xdatum_unchecked_count++;
  			}
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
865
866
867
  		}
  	}
  	/* build complete */
8a13695cb   KaiGai Kohei   [JFFS2][XATTR] ri...
868
869
870
871
872
873
  	JFFS2_NOTICE("complete building xattr subsystem, %u of xdatum"
  		     " (%u unchecked, %u orphan) and "
  		     "%u of xref (%u dead, %u orphan) found.
  ",
  		     xdatum_count, xdatum_unchecked_count, xdatum_orphan_count,
  		     xref_count, xref_dead_count, xref_orphan_count);
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
874
875
876
877
878
  }
  
  struct jffs2_xattr_datum *jffs2_setup_xattr_datum(struct jffs2_sb_info *c,
  						  uint32_t xid, uint32_t version)
  {
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
879
  	struct jffs2_xattr_datum *xd;
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
880

c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
881
882
883
884
885
886
887
888
889
890
  	xd = jffs2_find_xattr_datum(c, xid);
  	if (!xd) {
  		xd = jffs2_alloc_xattr_datum();
  		if (!xd)
  			return ERR_PTR(-ENOMEM);
  		xd->xid = xid;
  		xd->version = version;
  		if (xd->xid > c->highest_xid)
  			c->highest_xid = xd->xid;
  		list_add_tail(&xd->xindex, &c->xattrindex[xid % XATTRINDEX_HASHSIZE]);
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
891
892
893
894
895
896
897
898
899
900
901
902
903
904
  	}
  	return xd;
  }
  
  /* -------- xattr subsystem functions ---------------
   * xprefix_to_handler(xprefix)
   *   is used to translate xprefix into xattr_handler.
   * jffs2_listxattr(dentry, buffer, size)
   *   is an implementation of listxattr handler on jffs2.
   * do_jffs2_getxattr(inode, xprefix, xname, buffer, size)
   *   is an implementation of getxattr handler on jffs2.
   * do_jffs2_setxattr(inode, xprefix, xname, buffer, size, flags)
   *   is an implementation of setxattr handler on jffs2.
   * -------------------------------------------------- */
365f0cb9d   Stephen Hemminger   jffs2: constify x...
905
  const struct xattr_handler *jffs2_xattr_handlers[] = {
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
906
907
908
909
910
  	&jffs2_user_xattr_handler,
  #ifdef CONFIG_JFFS2_FS_SECURITY
  	&jffs2_security_xattr_handler,
  #endif
  #ifdef CONFIG_JFFS2_FS_POSIX_ACL
f2963d455   Christoph Hellwig   jffs2: use generi...
911
912
  	&posix_acl_access_xattr_handler,
  	&posix_acl_default_xattr_handler,
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
913
914
915
916
  #endif
  	&jffs2_trusted_xattr_handler,
  	NULL
  };
365f0cb9d   Stephen Hemminger   jffs2: constify x...
917
918
  static const struct xattr_handler *xprefix_to_handler(int xprefix) {
  	const struct xattr_handler *ret;
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
919
920
921
922
923
924
925
926
927
928
929
930
  
  	switch (xprefix) {
  	case JFFS2_XPREFIX_USER:
  		ret = &jffs2_user_xattr_handler;
  		break;
  #ifdef CONFIG_JFFS2_FS_SECURITY
  	case JFFS2_XPREFIX_SECURITY:
  		ret = &jffs2_security_xattr_handler;
  		break;
  #endif
  #ifdef CONFIG_JFFS2_FS_POSIX_ACL
  	case JFFS2_XPREFIX_ACL_ACCESS:
f2963d455   Christoph Hellwig   jffs2: use generi...
931
  		ret = &posix_acl_access_xattr_handler;
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
932
933
  		break;
  	case JFFS2_XPREFIX_ACL_DEFAULT:
f2963d455   Christoph Hellwig   jffs2: use generi...
934
  		ret = &posix_acl_default_xattr_handler;
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
935
936
937
938
939
940
941
942
943
944
945
946
947
948
  		break;
  #endif
  	case JFFS2_XPREFIX_TRUSTED:
  		ret = &jffs2_trusted_xattr_handler;
  		break;
  	default:
  		ret = NULL;
  		break;
  	}
  	return ret;
  }
  
  ssize_t jffs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
  {
2b0143b5c   David Howells   VFS: normal files...
949
  	struct inode *inode = d_inode(dentry);
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
950
951
952
  	struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
  	struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
  	struct jffs2_inode_cache *ic = f->inocache;
8f2b6f49c   KaiGai Kohei   [JFFS2][XATTR] Re...
953
  	struct jffs2_xattr_ref *ref, **pref;
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
954
  	struct jffs2_xattr_datum *xd;
365f0cb9d   Stephen Hemminger   jffs2: constify x...
955
  	const struct xattr_handler *xhandle;
764a5c6b1   Andreas Gruenbacher   xattr handlers: S...
956
957
  	const char *prefix;
  	ssize_t prefix_len, len, rc;
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
958
  	int retry = 0;
8f2b6f49c   KaiGai Kohei   [JFFS2][XATTR] Re...
959
  	rc = check_xattr_ref_inode(c, ic);
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
960
961
962
963
964
965
  	if (unlikely(rc))
  		return rc;
  
  	down_read(&c->xattr_sem);
   retry:
  	len = 0;
8f2b6f49c   KaiGai Kohei   [JFFS2][XATTR] Re...
966
  	for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
967
968
969
970
971
972
973
974
975
976
977
978
  		BUG_ON(ref->ic != ic);
  		xd = ref->xd;
  		if (!xd->xname) {
  			/* xdatum is unchached */
  			if (!retry) {
  				retry = 1;
  				up_read(&c->xattr_sem);
  				down_write(&c->xattr_sem);
  				goto retry;
  			} else {
  				rc = load_xattr_datum(c, xd);
  				if (unlikely(rc > 0)) {
8f2b6f49c   KaiGai Kohei   [JFFS2][XATTR] Re...
979
  					*pref = ref->next;
8a13695cb   KaiGai Kohei   [JFFS2][XATTR] ri...
980
  					delete_xattr_ref(c, ref);
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
981
982
983
984
985
986
  					goto retry;
  				} else if (unlikely(rc < 0))
  					goto out;
  			}
  		}
  		xhandle = xprefix_to_handler(xd->xprefix);
764a5c6b1   Andreas Gruenbacher   xattr handlers: S...
987
  		if (!xhandle || (xhandle->list && !xhandle->list(dentry)))
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
988
  			continue;
764a5c6b1   Andreas Gruenbacher   xattr handlers: S...
989
990
991
  		prefix = xhandle->prefix ?: xhandle->name;
  		prefix_len = strlen(prefix);
  		rc = prefix_len + xd->name_len + 1;
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
992
  		if (buffer) {
764a5c6b1   Andreas Gruenbacher   xattr handlers: S...
993
994
995
996
997
998
999
1000
1001
  			if (rc > size - len) {
  				rc = -ERANGE;
  				goto out;
  			}
  			memcpy(buffer, prefix, prefix_len);
  			buffer += prefix_len;
  			memcpy(buffer, xd->xname, xd->name_len);
  			buffer += xd->name_len;
  			*buffer++ = 0;
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
1002
  		}
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
  		len += rc;
  	}
  	rc = len;
   out:
  	if (!retry) {
  		up_read(&c->xattr_sem);
  	} else {
  		up_write(&c->xattr_sem);
  	}
  	return rc;
  }
  
  int do_jffs2_getxattr(struct inode *inode, int xprefix, const char *xname,
  		      char *buffer, size_t size)
  {
  	struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
  	struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
  	struct jffs2_inode_cache *ic = f->inocache;
  	struct jffs2_xattr_datum *xd;
8f2b6f49c   KaiGai Kohei   [JFFS2][XATTR] Re...
1022
  	struct jffs2_xattr_ref *ref, **pref;
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
1023
  	int rc, retry = 0;
8f2b6f49c   KaiGai Kohei   [JFFS2][XATTR] Re...
1024
  	rc = check_xattr_ref_inode(c, ic);
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
1025
1026
1027
1028
1029
  	if (unlikely(rc))
  		return rc;
  
  	down_read(&c->xattr_sem);
   retry:
8f2b6f49c   KaiGai Kohei   [JFFS2][XATTR] Re...
1030
  	for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
  		BUG_ON(ref->ic!=ic);
  
  		xd = ref->xd;
  		if (xd->xprefix != xprefix)
  			continue;
  		if (!xd->xname) {
  			/* xdatum is unchached */
  			if (!retry) {
  				retry = 1;
  				up_read(&c->xattr_sem);
  				down_write(&c->xattr_sem);
  				goto retry;
  			} else {
  				rc = load_xattr_datum(c, xd);
  				if (unlikely(rc > 0)) {
8f2b6f49c   KaiGai Kohei   [JFFS2][XATTR] Re...
1046
  					*pref = ref->next;
8a13695cb   KaiGai Kohei   [JFFS2][XATTR] ri...
1047
  					delete_xattr_ref(c, ref);
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
  					goto retry;
  				} else if (unlikely(rc < 0)) {
  					goto out;
  				}
  			}
  		}
  		if (!strcmp(xname, xd->xname)) {
  			rc = xd->value_len;
  			if (buffer) {
  				if (size < rc) {
  					rc = -ERANGE;
  				} else {
  					memcpy(buffer, xd->xvalue, rc);
  				}
  			}
  			goto out;
  		}
  	}
  	rc = -ENODATA;
   out:
  	if (!retry) {
  		up_read(&c->xattr_sem);
  	} else {
  		up_write(&c->xattr_sem);
  	}
  	return rc;
  }
  
  int do_jffs2_setxattr(struct inode *inode, int xprefix, const char *xname,
  		      const char *buffer, size_t size, int flags)
  {
  	struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
  	struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
  	struct jffs2_inode_cache *ic = f->inocache;
  	struct jffs2_xattr_datum *xd;
8f2b6f49c   KaiGai Kohei   [JFFS2][XATTR] Re...
1083
  	struct jffs2_xattr_ref *ref, *newref, **pref;
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
1084
  	uint32_t length, request;
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
1085
  	int rc;
8f2b6f49c   KaiGai Kohei   [JFFS2][XATTR] Re...
1086
  	rc = check_xattr_ref_inode(c, ic);
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
1087
1088
1089
1090
  	if (unlikely(rc))
  		return rc;
  
  	request = PAD(sizeof(struct jffs2_raw_xattr) + strlen(xname) + 1 + size);
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
1091
  	rc = jffs2_reserve_space(c, request, &length,
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
  				 ALLOC_NORMAL, JFFS2_SUMMARY_XATTR_SIZE);
  	if (rc) {
  		JFFS2_WARNING("jffs2_reserve_space()=%d, request=%u
  ", rc, request);
  		return rc;
  	}
  
  	/* Find existing xattr */
  	down_write(&c->xattr_sem);
   retry:
8f2b6f49c   KaiGai Kohei   [JFFS2][XATTR] Re...
1102
  	for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
1103
1104
1105
1106
1107
1108
  		xd = ref->xd;
  		if (xd->xprefix != xprefix)
  			continue;
  		if (!xd->xname) {
  			rc = load_xattr_datum(c, xd);
  			if (unlikely(rc > 0)) {
8f2b6f49c   KaiGai Kohei   [JFFS2][XATTR] Re...
1109
  				*pref = ref->next;
8a13695cb   KaiGai Kohei   [JFFS2][XATTR] ri...
1110
  				delete_xattr_ref(c, ref);
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
  				goto retry;
  			} else if (unlikely(rc < 0))
  				goto out;
  		}
  		if (!strcmp(xd->xname, xname)) {
  			if (flags & XATTR_CREATE) {
  				rc = -EEXIST;
  				goto out;
  			}
  			if (!buffer) {
8a13695cb   KaiGai Kohei   [JFFS2][XATTR] ri...
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
  				ref->ino = ic->ino;
  				ref->xid = xd->xid;
  				ref->xseqno |= XREF_DELETE_MARKER;
  				rc = save_xattr_ref(c, ref);
  				if (!rc) {
  					*pref = ref->next;
  					spin_lock(&c->erase_completion_lock);
  					ref->next = c->xref_dead_list;
  					c->xref_dead_list = ref;
  					spin_unlock(&c->erase_completion_lock);
c6e8c6ccf   KaiGai Kohei   [JFFS2][XATTR] Fi...
1131
  					unrefer_xattr_datum(c, xd);
8a13695cb   KaiGai Kohei   [JFFS2][XATTR] ri...
1132
1133
1134
1135
1136
  				} else {
  					ref->ic = ic;
  					ref->xd = xd;
  					ref->xseqno &= ~XREF_DELETE_MARKER;
  				}
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
1137
1138
1139
1140
1141
1142
  				goto out;
  			}
  			goto found;
  		}
  	}
  	/* not found */
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
1143
1144
1145
1146
1147
  	if (flags & XATTR_REPLACE) {
  		rc = -ENODATA;
  		goto out;
  	}
  	if (!buffer) {
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
1148
  		rc = -ENODATA;
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
1149
1150
1151
  		goto out;
  	}
   found:
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
1152
  	xd = create_xattr_datum(c, xprefix, xname, buffer, size);
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
1153
1154
1155
1156
1157
1158
1159
1160
1161
  	if (IS_ERR(xd)) {
  		rc = PTR_ERR(xd);
  		goto out;
  	}
  	up_write(&c->xattr_sem);
  	jffs2_complete_reservation(c);
  
  	/* create xattr_ref */
  	request = PAD(sizeof(struct jffs2_raw_xref));
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
1162
  	rc = jffs2_reserve_space(c, request, &length,
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
1163
  				 ALLOC_NORMAL, JFFS2_SUMMARY_XREF_SIZE);
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
1164
  	down_write(&c->xattr_sem);
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
1165
1166
1167
  	if (rc) {
  		JFFS2_WARNING("jffs2_reserve_space()=%d, request=%u
  ", rc, request);
c6e8c6ccf   KaiGai Kohei   [JFFS2][XATTR] Fi...
1168
  		unrefer_xattr_datum(c, xd);
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
1169
1170
1171
  		up_write(&c->xattr_sem);
  		return rc;
  	}
8f2b6f49c   KaiGai Kohei   [JFFS2][XATTR] Re...
1172
1173
  	if (ref)
  		*pref = ref->next;
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
1174
  	newref = create_xattr_ref(c, ic, xd);
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
1175
  	if (IS_ERR(newref)) {
8f2b6f49c   KaiGai Kohei   [JFFS2][XATTR] Re...
1176
1177
1178
1179
  		if (ref) {
  			ref->next = ic->xref;
  			ic->xref = ref;
  		}
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
1180
  		rc = PTR_ERR(newref);
c6e8c6ccf   KaiGai Kohei   [JFFS2][XATTR] Fi...
1181
  		unrefer_xattr_datum(c, xd);
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
1182
  	} else if (ref) {
8a13695cb   KaiGai Kohei   [JFFS2][XATTR] ri...
1183
  		delete_xattr_ref(c, ref);
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
1184
1185
1186
1187
1188
1189
1190
1191
  	}
   out:
  	up_write(&c->xattr_sem);
  	jffs2_complete_reservation(c);
  	return rc;
  }
  
  /* -------- garbage collector functions -------------
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
1192
   * jffs2_garbage_collect_xattr_datum(c, xd, raw)
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
1193
   *   is used to move xdatum into new node.
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
1194
   * jffs2_garbage_collect_xattr_ref(c, ref, raw)
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
1195
   *   is used to move xref into new node.
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
1196
1197
   * jffs2_verify_xattr(c)
   *   is used to call do_verify_xattr_datum() before garbage collecting.
8a13695cb   KaiGai Kohei   [JFFS2][XATTR] ri...
1198
1199
1200
1201
   * jffs2_release_xattr_datum(c, xd)
   *   is used to release an in-memory object of xdatum.
   * jffs2_release_xattr_ref(c, ref)
   *   is used to release an in-memory object of xref.
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
1202
   * -------------------------------------------------- */
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
1203
1204
  int jffs2_garbage_collect_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd,
  				      struct jffs2_raw_node_ref *raw)
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
1205
  {
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
1206
  	uint32_t totlen, length, old_ofs;
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
1207
  	int rc = 0;
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
1208

084702e00   KaiGai Kohei   [JFFS2][XATTR] Re...
1209
  	down_write(&c->xattr_sem);
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
1210
1211
  	if (xd->node != raw)
  		goto out;
8a13695cb   KaiGai Kohei   [JFFS2][XATTR] ri...
1212
  	if (xd->flags & (JFFS2_XFLAGS_DEAD|JFFS2_XFLAGS_INVALID))
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
1213
  		goto out;
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
1214

8a13695cb   KaiGai Kohei   [JFFS2][XATTR] ri...
1215
1216
1217
1218
  	rc = load_xattr_datum(c, xd);
  	if (unlikely(rc)) {
  		rc = (rc > 0) ? 0 : rc;
  		goto out;
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
1219
  	}
8a13695cb   KaiGai Kohei   [JFFS2][XATTR] ri...
1220
1221
1222
  	old_ofs = ref_offset(xd->node);
  	totlen = PAD(sizeof(struct jffs2_raw_xattr)
  			+ xd->name_len + 1 + xd->value_len);
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
1223
  	rc = jffs2_reserve_space_gc(c, totlen, &length, JFFS2_SUMMARY_XATTR_SIZE);
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
1224
1225
1226
  	if (rc) {
  		JFFS2_WARNING("jffs2_reserve_space_gc()=%d, request=%u
  ", rc, totlen);
084702e00   KaiGai Kohei   [JFFS2][XATTR] Re...
1227
  		goto out;
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
1228
  	}
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
1229
  	rc = save_xattr_datum(c, xd);
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
1230
1231
1232
1233
  	if (!rc)
  		dbg_xattr("xdatum (xid=%u, version=%u) GC'ed from %#08x to %08x
  ",
  			  xd->xid, xd->version, old_ofs, ref_offset(xd->node));
084702e00   KaiGai Kohei   [JFFS2][XATTR] Re...
1234
   out:
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
1235
1236
  	if (!rc)
  		jffs2_mark_node_obsolete(c, raw);
084702e00   KaiGai Kohei   [JFFS2][XATTR] Re...
1237
  	up_write(&c->xattr_sem);
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
1238
1239
  	return rc;
  }
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
1240
1241
  int jffs2_garbage_collect_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref,
  				    struct jffs2_raw_node_ref *raw)
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
1242
  {
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
1243
  	uint32_t totlen, length, old_ofs;
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
1244
  	int rc = 0;
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
1245

084702e00   KaiGai Kohei   [JFFS2][XATTR] Re...
1246
  	down_write(&c->xattr_sem);
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
1247
  	BUG_ON(!ref->node);
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
1248
1249
1250
1251
  	if (ref->node != raw)
  		goto out;
  	if (is_xattr_ref_dead(ref) && (raw->next_in_ino == (void *)ref))
  		goto out;
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
1252
1253
  	old_ofs = ref_offset(ref->node);
  	totlen = ref_totlen(c, c->gcblock, ref->node);
084702e00   KaiGai Kohei   [JFFS2][XATTR] Re...
1254

9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
1255
  	rc = jffs2_reserve_space_gc(c, totlen, &length, JFFS2_SUMMARY_XREF_SIZE);
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
1256
1257
1258
  	if (rc) {
  		JFFS2_WARNING("%s: jffs2_reserve_space_gc() = %d, request = %u
  ",
8e24eea72   Harvey Harrison   fs: replace remai...
1259
  			      __func__, rc, totlen);
084702e00   KaiGai Kohei   [JFFS2][XATTR] Re...
1260
  		goto out;
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
1261
  	}
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
1262
  	rc = save_xattr_ref(c, ref);
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
1263
1264
1265
1266
  	if (!rc)
  		dbg_xattr("xref (ino=%u, xid=%u) GC'ed from %#08x to %08x
  ",
  			  ref->ic->ino, ref->xd->xid, old_ofs, ref_offset(ref->node));
084702e00   KaiGai Kohei   [JFFS2][XATTR] Re...
1267
   out:
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
1268
1269
  	if (!rc)
  		jffs2_mark_node_obsolete(c, raw);
084702e00   KaiGai Kohei   [JFFS2][XATTR] Re...
1270
  	up_write(&c->xattr_sem);
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
1271
1272
  	return rc;
  }
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
1273
1274
1275
  int jffs2_verify_xattr(struct jffs2_sb_info *c)
  {
  	struct jffs2_xattr_datum *xd, *_xd;
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
1276
1277
1278
  	struct jffs2_eraseblock *jeb;
  	struct jffs2_raw_node_ref *raw;
  	uint32_t totlen;
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
1279
1280
1281
1282
1283
  	int rc;
  
  	down_write(&c->xattr_sem);
  	list_for_each_entry_safe(xd, _xd, &c->xattr_unchecked, xindex) {
  		rc = do_verify_xattr_datum(c, xd);
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
  		if (rc < 0)
  			continue;
  		list_del_init(&xd->xindex);
  		spin_lock(&c->erase_completion_lock);
  		for (raw=xd->node; raw != (void *)xd; raw=raw->next_in_ino) {
  			if (ref_flags(raw) != REF_UNCHECKED)
  				continue;
  			jeb = &c->blocks[ref_offset(raw) / c->sector_size];
  			totlen = PAD(ref_totlen(c, jeb, raw));
  			c->unchecked_size -= totlen; c->used_size += totlen;
  			jeb->unchecked_size -= totlen; jeb->used_size += totlen;
  			raw->flash_offset = ref_offset(raw)
  				| ((xd->node == (void *)raw) ? REF_PRISTINE : REF_NORMAL);
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
1297
  		}
8a13695cb   KaiGai Kohei   [JFFS2][XATTR] ri...
1298
  		if (xd->flags & JFFS2_XFLAGS_DEAD)
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
1299
1300
  			list_add(&xd->xindex, &c->xattr_dead_list);
  		spin_unlock(&c->erase_completion_lock);
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
1301
1302
  	}
  	up_write(&c->xattr_sem);
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
1303
1304
  	return list_empty(&c->xattr_unchecked) ? 1 : 0;
  }
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
1305
1306
1307
1308
  
  void jffs2_release_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
  {
  	/* must be called under spin_lock(&c->erase_completion_lock) */
2c887e235   KaiGai Kohei   [JFFS2][XATTR] Re...
1309
  	if (atomic_read(&xd->refcnt) || xd->node != (void *)xd)
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
  		return;
  
  	list_del(&xd->xindex);
  	jffs2_free_xattr_datum(xd);
  }
  
  void jffs2_release_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
  {
  	/* must be called under spin_lock(&c->erase_completion_lock) */
  	struct jffs2_xattr_ref *tmp, **ptmp;
  
  	if (ref->node != (void *)ref)
  		return;
  
  	for (tmp=c->xref_dead_list, ptmp=&c->xref_dead_list; tmp; ptmp=&tmp->next, tmp=tmp->next) {
  		if (ref == tmp) {
  			*ptmp = tmp->next;
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
1327
1328
1329
  			break;
  		}
  	}
8a13695cb   KaiGai Kohei   [JFFS2][XATTR] ri...
1330
  	jffs2_free_xattr_ref(ref);
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
1331
  }