~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/jffs2/xattr.c

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /fs/jffs2/xattr.c (Version linux-6.12-rc7) and /fs/jffs2/xattr.c (Version linux-5.6.19)


  1 /*                                                  1 /*
  2  * JFFS2 -- Journalling Flash File System, Ver      2  * JFFS2 -- Journalling Flash File System, Version 2.
  3  *                                                  3  *
  4  * Copyright © 2006  NEC Corporation               4  * Copyright © 2006  NEC Corporation
  5  *                                                  5  *
  6  * Created by KaiGai Kohei <kaigai@ak.jp.nec.c      6  * Created by KaiGai Kohei <kaigai@ak.jp.nec.com>
  7  *                                                  7  *
  8  * For licensing information, see the file 'LI      8  * For licensing information, see the file 'LICENCE' in this directory.
  9  *                                                  9  *
 10  */                                                10  */
 11                                                    11 
 12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt        12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 13                                                    13 
 14 #define JFFS2_XATTR_IS_CORRUPTED        1          14 #define JFFS2_XATTR_IS_CORRUPTED        1
 15                                                    15 
 16 #include <linux/kernel.h>                          16 #include <linux/kernel.h>
 17 #include <linux/slab.h>                            17 #include <linux/slab.h>
 18 #include <linux/fs.h>                              18 #include <linux/fs.h>
 19 #include <linux/time.h>                            19 #include <linux/time.h>
 20 #include <linux/pagemap.h>                         20 #include <linux/pagemap.h>
 21 #include <linux/highmem.h>                         21 #include <linux/highmem.h>
 22 #include <linux/crc32.h>                           22 #include <linux/crc32.h>
 23 #include <linux/jffs2.h>                           23 #include <linux/jffs2.h>
 24 #include <linux/xattr.h>                           24 #include <linux/xattr.h>
 25 #include <linux/posix_acl_xattr.h>                 25 #include <linux/posix_acl_xattr.h>
 26 #include <linux/mtd/mtd.h>                         26 #include <linux/mtd/mtd.h>
 27 #include "nodelist.h"                              27 #include "nodelist.h"
 28 /* -------- xdatum related functions ---------     28 /* -------- xdatum related functions ----------------
 29  * xattr_datum_hashkey(xprefix, xname, xvalue,     29  * xattr_datum_hashkey(xprefix, xname, xvalue, xsize)
 30  *   is used to calcurate xdatum hashkey. The      30  *   is used to calcurate xdatum hashkey. The reminder of hashkey into XATTRINDEX_HASHSIZE is
 31  *   the index of the xattr name/value pair ca     31  *   the index of the xattr name/value pair cache (c->xattrindex).
 32  * is_xattr_datum_unchecked(c, xd)                 32  * is_xattr_datum_unchecked(c, xd)
 33  *   returns 1, if xdatum contains any uncheck     33  *   returns 1, if xdatum contains any unchecked raw nodes. if all raw nodes are not
 34  *   unchecked, it returns 0.                      34  *   unchecked, it returns 0.
 35  * unload_xattr_datum(c, xd)                       35  * unload_xattr_datum(c, xd)
 36  *   is used to release xattr name/value pair      36  *   is used to release xattr name/value pair and detach from c->xattrindex.
 37  * reclaim_xattr_datum(c)                          37  * reclaim_xattr_datum(c)
 38  *   is used to reclaim xattr name/value pairs     38  *   is used to reclaim xattr name/value pairs on the xattr name/value pair cache when
 39  *   memory usage by cache is over c->xdatum_m     39  *   memory usage by cache is over c->xdatum_mem_threshold. Currently, this threshold
 40  *   is hard coded as 32KiB.                       40  *   is hard coded as 32KiB.
 41  * do_verify_xattr_datum(c, xd)                    41  * do_verify_xattr_datum(c, xd)
 42  *   is used to load the xdatum informations w     42  *   is used to load the xdatum informations without name/value pair from the medium.
 43  *   It's necessary once, because those inform     43  *   It's necessary once, because those informations are not collected during mounting
 44  *   process when EBS is enabled.                  44  *   process when EBS is enabled.
 45  *   0 will be returned, if success. An negati     45  *   0 will be returned, if success. An negative return value means recoverable error, and
 46  *   positive return value means unrecoverable     46  *   positive return value means unrecoverable error. Thus, caller must remove this xdatum
 47  *   and xref when it returned positive value.     47  *   and xref when it returned positive value.
 48  * do_load_xattr_datum(c, xd)                      48  * do_load_xattr_datum(c, xd)
 49  *   is used to load name/value pair from the      49  *   is used to load name/value pair from the medium.
 50  *   The meanings of return value is same as d     50  *   The meanings of return value is same as do_verify_xattr_datum().
 51  * load_xattr_datum(c, xd)                         51  * load_xattr_datum(c, xd)
 52  *   is used to be as a wrapper of do_verify_x     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(     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 re     54  *   do_load_xattr_datum(). The meanings of return value is same as do_verify_xattr_datum().
 55  * save_xattr_datum(c, xd)                         55  * save_xattr_datum(c, xd)
 56  *   is used to write xdatum to medium. xd->ve     56  *   is used to write xdatum to medium. xd->version will be incremented.
 57  * create_xattr_datum(c, xprefix, xname, xvalu     57  * create_xattr_datum(c, xprefix, xname, xvalue, xsize)
 58  *   is used to create new xdatum and write to     58  *   is used to create new xdatum and write to medium.
 59  * unrefer_xattr_datum(c, xd)                      59  * unrefer_xattr_datum(c, xd)
 60  *   is used to delete a xdatum. When nobody r     60  *   is used to delete a xdatum. When nobody refers this xdatum, JFFS2_XFLAGS_DEAD
 61  *   is set on xd->flags and chained xattr_dea     61  *   is set on xd->flags and chained xattr_dead_list or release it immediately.
 62  *   In the first case, the garbage collector      62  *   In the first case, the garbage collector release it later.
 63  * -------------------------------------------     63  * -------------------------------------------------- */
 64 static uint32_t xattr_datum_hashkey(int xprefi     64 static uint32_t xattr_datum_hashkey(int xprefix, const char *xname, const char *xvalue, int xsize)
 65 {                                                  65 {
 66         int name_len = strlen(xname);              66         int name_len = strlen(xname);
 67                                                    67 
 68         return crc32(xprefix, xname, name_len)     68         return crc32(xprefix, xname, name_len) ^ crc32(xprefix, xvalue, xsize);
 69 }                                                  69 }
 70                                                    70 
 71 static int is_xattr_datum_unchecked(struct jff     71 static int is_xattr_datum_unchecked(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
 72 {                                                  72 {
 73         struct jffs2_raw_node_ref *raw;            73         struct jffs2_raw_node_ref *raw;
 74         int rc = 0;                                74         int rc = 0;
 75                                                    75 
 76         spin_lock(&c->erase_completion_lock);      76         spin_lock(&c->erase_completion_lock);
 77         for (raw=xd->node; raw != (void *)xd;      77         for (raw=xd->node; raw != (void *)xd; raw=raw->next_in_ino) {
 78                 if (ref_flags(raw) == REF_UNCH     78                 if (ref_flags(raw) == REF_UNCHECKED) {
 79                         rc = 1;                    79                         rc = 1;
 80                         break;                     80                         break;
 81                 }                                  81                 }
 82         }                                          82         }
 83         spin_unlock(&c->erase_completion_lock)     83         spin_unlock(&c->erase_completion_lock);
 84         return rc;                                 84         return rc;
 85 }                                                  85 }
 86                                                    86 
 87 static void unload_xattr_datum(struct jffs2_sb     87 static void unload_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
 88 {                                                  88 {
 89         /* must be called under down_write(xat     89         /* must be called under down_write(xattr_sem) */
 90         D1(dbg_xattr("%s: xid=%u, version=%u\n     90         D1(dbg_xattr("%s: xid=%u, version=%u\n", __func__, xd->xid, xd->version));
 91         if (xd->xname) {                           91         if (xd->xname) {
 92                 c->xdatum_mem_usage -= (xd->na     92                 c->xdatum_mem_usage -= (xd->name_len + 1 + xd->value_len);
 93                 kfree(xd->xname);                  93                 kfree(xd->xname);
 94         }                                          94         }
 95                                                    95 
 96         list_del_init(&xd->xindex);                96         list_del_init(&xd->xindex);
 97         xd->hashkey = 0;                           97         xd->hashkey = 0;
 98         xd->xname = NULL;                          98         xd->xname = NULL;
 99         xd->xvalue = NULL;                         99         xd->xvalue = NULL;
100 }                                                 100 }
101                                                   101 
102 static void reclaim_xattr_datum(struct jffs2_s    102 static void reclaim_xattr_datum(struct jffs2_sb_info *c)
103 {                                                 103 {
104         /* must be called under down_write(xat    104         /* must be called under down_write(xattr_sem) */
105         struct jffs2_xattr_datum *xd, *_xd;       105         struct jffs2_xattr_datum *xd, *_xd;
106         uint32_t target, before;                  106         uint32_t target, before;
107         static int index = 0;                     107         static int index = 0;
108         int count;                                108         int count;
109                                                   109 
110         if (c->xdatum_mem_threshold > c->xdatu    110         if (c->xdatum_mem_threshold > c->xdatum_mem_usage)
111                 return;                           111                 return;
112                                                   112 
113         before = c->xdatum_mem_usage;             113         before = c->xdatum_mem_usage;
114         target = c->xdatum_mem_usage * 4 / 5;     114         target = c->xdatum_mem_usage * 4 / 5; /* 20% reduction */
115         for (count = 0; count < XATTRINDEX_HAS    115         for (count = 0; count < XATTRINDEX_HASHSIZE; count++) {
116                 list_for_each_entry_safe(xd, _    116                 list_for_each_entry_safe(xd, _xd, &c->xattrindex[index], xindex) {
117                         if (xd->flags & JFFS2_    117                         if (xd->flags & JFFS2_XFLAGS_HOT) {
118                                 xd->flags &= ~    118                                 xd->flags &= ~JFFS2_XFLAGS_HOT;
119                         } else if (!(xd->flags    119                         } else if (!(xd->flags & JFFS2_XFLAGS_BIND)) {
120                                 unload_xattr_d    120                                 unload_xattr_datum(c, xd);
121                         }                         121                         }
122                         if (c->xdatum_mem_usag    122                         if (c->xdatum_mem_usage <= target)
123                                 goto out;         123                                 goto out;
124                 }                                 124                 }
125                 index = (index+1) % XATTRINDEX    125                 index = (index+1) % XATTRINDEX_HASHSIZE;
126         }                                         126         }
127  out:                                             127  out:
128         JFFS2_NOTICE("xdatum_mem_usage from %u    128         JFFS2_NOTICE("xdatum_mem_usage from %u byte to %u byte (%u byte reclaimed)\n",
129                      before, c->xdatum_mem_usa    129                      before, c->xdatum_mem_usage, before - c->xdatum_mem_usage);
130 }                                                 130 }
131                                                   131 
132 static int do_verify_xattr_datum(struct jffs2_    132 static int do_verify_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
133 {                                                 133 {
134         /* must be called under down_write(xat    134         /* must be called under down_write(xattr_sem) */
135         struct jffs2_eraseblock *jeb;             135         struct jffs2_eraseblock *jeb;
136         struct jffs2_raw_node_ref *raw;           136         struct jffs2_raw_node_ref *raw;
137         struct jffs2_raw_xattr rx;                137         struct jffs2_raw_xattr rx;
138         size_t readlen;                           138         size_t readlen;
139         uint32_t crc, offset, totlen;             139         uint32_t crc, offset, totlen;
140         int rc;                                   140         int rc;
141                                                   141 
142         spin_lock(&c->erase_completion_lock);     142         spin_lock(&c->erase_completion_lock);
143         offset = ref_offset(xd->node);            143         offset = ref_offset(xd->node);
144         if (ref_flags(xd->node) == REF_PRISTIN    144         if (ref_flags(xd->node) == REF_PRISTINE)
145                 goto complete;                    145                 goto complete;
146         spin_unlock(&c->erase_completion_lock)    146         spin_unlock(&c->erase_completion_lock);
147                                                   147 
148         rc = jffs2_flash_read(c, offset, sizeo    148         rc = jffs2_flash_read(c, offset, sizeof(rx), &readlen, (char *)&rx);
149         if (rc || readlen != sizeof(rx)) {        149         if (rc || readlen != sizeof(rx)) {
150                 JFFS2_WARNING("jffs2_flash_rea    150                 JFFS2_WARNING("jffs2_flash_read()=%d, req=%zu, read=%zu at %#08x\n",
151                               rc, sizeof(rx),     151                               rc, sizeof(rx), readlen, offset);
152                 return rc ? rc : -EIO;            152                 return rc ? rc : -EIO;
153         }                                         153         }
154         crc = crc32(0, &rx, sizeof(rx) - 4);      154         crc = crc32(0, &rx, sizeof(rx) - 4);
155         if (crc != je32_to_cpu(rx.node_crc)) {    155         if (crc != je32_to_cpu(rx.node_crc)) {
156                 JFFS2_ERROR("node CRC failed a    156                 JFFS2_ERROR("node CRC failed at %#08x, read=%#08x, calc=%#08x\n",
157                             offset, je32_to_cp    157                             offset, je32_to_cpu(rx.hdr_crc), crc);
158                 xd->flags |= JFFS2_XFLAGS_INVA    158                 xd->flags |= JFFS2_XFLAGS_INVALID;
159                 return JFFS2_XATTR_IS_CORRUPTE    159                 return JFFS2_XATTR_IS_CORRUPTED;
160         }                                         160         }
161         totlen = PAD(sizeof(rx) + rx.name_len     161         totlen = PAD(sizeof(rx) + rx.name_len + 1 + je16_to_cpu(rx.value_len));
162         if (je16_to_cpu(rx.magic) != JFFS2_MAG    162         if (je16_to_cpu(rx.magic) != JFFS2_MAGIC_BITMASK
163             || je16_to_cpu(rx.nodetype) != JFF    163             || je16_to_cpu(rx.nodetype) != JFFS2_NODETYPE_XATTR
164             || je32_to_cpu(rx.totlen) != totle    164             || je32_to_cpu(rx.totlen) != totlen
165             || je32_to_cpu(rx.xid) != xd->xid     165             || je32_to_cpu(rx.xid) != xd->xid
166             || je32_to_cpu(rx.version) != xd->    166             || je32_to_cpu(rx.version) != xd->version) {
167                 JFFS2_ERROR("inconsistent xdat    167                 JFFS2_ERROR("inconsistent xdatum at %#08x, magic=%#04x/%#04x, "
168                             "nodetype=%#04x/%#    168                             "nodetype=%#04x/%#04x, totlen=%u/%u, xid=%u/%u, version=%u/%u\n",
169                             offset, je16_to_cp    169                             offset, je16_to_cpu(rx.magic), JFFS2_MAGIC_BITMASK,
170                             je16_to_cpu(rx.nod    170                             je16_to_cpu(rx.nodetype), JFFS2_NODETYPE_XATTR,
171                             je32_to_cpu(rx.tot    171                             je32_to_cpu(rx.totlen), totlen,
172                             je32_to_cpu(rx.xid    172                             je32_to_cpu(rx.xid), xd->xid,
173                             je32_to_cpu(rx.ver    173                             je32_to_cpu(rx.version), xd->version);
174                 xd->flags |= JFFS2_XFLAGS_INVA    174                 xd->flags |= JFFS2_XFLAGS_INVALID;
175                 return JFFS2_XATTR_IS_CORRUPTE    175                 return JFFS2_XATTR_IS_CORRUPTED;
176         }                                         176         }
177         xd->xprefix = rx.xprefix;                 177         xd->xprefix = rx.xprefix;
178         xd->name_len = rx.name_len;               178         xd->name_len = rx.name_len;
179         xd->value_len = je16_to_cpu(rx.value_l    179         xd->value_len = je16_to_cpu(rx.value_len);
180         xd->data_crc = je32_to_cpu(rx.data_crc    180         xd->data_crc = je32_to_cpu(rx.data_crc);
181                                                   181 
182         spin_lock(&c->erase_completion_lock);     182         spin_lock(&c->erase_completion_lock);
183  complete:                                        183  complete:
184         for (raw=xd->node; raw != (void *)xd;     184         for (raw=xd->node; raw != (void *)xd; raw=raw->next_in_ino) {
185                 jeb = &c->blocks[ref_offset(ra    185                 jeb = &c->blocks[ref_offset(raw) / c->sector_size];
186                 totlen = PAD(ref_totlen(c, jeb    186                 totlen = PAD(ref_totlen(c, jeb, raw));
187                 if (ref_flags(raw) == REF_UNCH    187                 if (ref_flags(raw) == REF_UNCHECKED) {
188                         c->unchecked_size -= t    188                         c->unchecked_size -= totlen; c->used_size += totlen;
189                         jeb->unchecked_size -=    189                         jeb->unchecked_size -= totlen; jeb->used_size += totlen;
190                 }                                 190                 }
191                 raw->flash_offset = ref_offset    191                 raw->flash_offset = ref_offset(raw) | ((xd->node==raw) ? REF_PRISTINE : REF_NORMAL);
192         }                                         192         }
193         spin_unlock(&c->erase_completion_lock)    193         spin_unlock(&c->erase_completion_lock);
194                                                   194 
195         /* unchecked xdatum is chained with c-    195         /* unchecked xdatum is chained with c->xattr_unchecked */
196         list_del_init(&xd->xindex);               196         list_del_init(&xd->xindex);
197                                                   197 
198         dbg_xattr("success on verifying xdatum    198         dbg_xattr("success on verifying xdatum (xid=%u, version=%u)\n",
199                   xd->xid, xd->version);          199                   xd->xid, xd->version);
200                                                   200 
201         return 0;                                 201         return 0;
202 }                                                 202 }
203                                                   203 
204 static int do_load_xattr_datum(struct jffs2_sb    204 static int do_load_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
205 {                                                 205 {
206         /* must be called under down_write(xat    206         /* must be called under down_write(xattr_sem) */
207         char *data;                               207         char *data;
208         size_t readlen;                           208         size_t readlen;
209         uint32_t crc, length;                     209         uint32_t crc, length;
210         int i, ret, retry = 0;                    210         int i, ret, retry = 0;
211                                                   211 
212         BUG_ON(ref_flags(xd->node) != REF_PRIS    212         BUG_ON(ref_flags(xd->node) != REF_PRISTINE);
213         BUG_ON(!list_empty(&xd->xindex));         213         BUG_ON(!list_empty(&xd->xindex));
214  retry:                                           214  retry:
215         length = xd->name_len + 1 + xd->value_    215         length = xd->name_len + 1 + xd->value_len;
216         data = kmalloc(length, GFP_KERNEL);       216         data = kmalloc(length, GFP_KERNEL);
217         if (!data)                                217         if (!data)
218                 return -ENOMEM;                   218                 return -ENOMEM;
219                                                   219 
220         ret = jffs2_flash_read(c, ref_offset(x    220         ret = jffs2_flash_read(c, ref_offset(xd->node)+sizeof(struct jffs2_raw_xattr),
221                                length, &readle    221                                length, &readlen, data);
222                                                   222 
223         if (ret || length!=readlen) {             223         if (ret || length!=readlen) {
224                 JFFS2_WARNING("jffs2_flash_rea    224                 JFFS2_WARNING("jffs2_flash_read() returned %d, request=%d, readlen=%zu, at %#08x\n",
225                               ret, length, rea    225                               ret, length, readlen, ref_offset(xd->node));
226                 kfree(data);                      226                 kfree(data);
227                 return ret ? ret : -EIO;          227                 return ret ? ret : -EIO;
228         }                                         228         }
229                                                   229 
230         data[xd->name_len] = '\0';                230         data[xd->name_len] = '\0';
231         crc = crc32(0, data, length);             231         crc = crc32(0, data, length);
232         if (crc != xd->data_crc) {                232         if (crc != xd->data_crc) {
233                 JFFS2_WARNING("node CRC failed    233                 JFFS2_WARNING("node CRC failed (JFFS2_NODETYPE_XATTR)"
234                               " at %#08x, read    234                               " at %#08x, read: 0x%08x calculated: 0x%08x\n",
235                               ref_offset(xd->n    235                               ref_offset(xd->node), xd->data_crc, crc);
236                 kfree(data);                      236                 kfree(data);
237                 xd->flags |= JFFS2_XFLAGS_INVA    237                 xd->flags |= JFFS2_XFLAGS_INVALID;
238                 return JFFS2_XATTR_IS_CORRUPTE    238                 return JFFS2_XATTR_IS_CORRUPTED;
239         }                                         239         }
240                                                   240 
241         xd->flags |= JFFS2_XFLAGS_HOT;            241         xd->flags |= JFFS2_XFLAGS_HOT;
242         xd->xname = data;                         242         xd->xname = data;
243         xd->xvalue = data + xd->name_len+1;       243         xd->xvalue = data + xd->name_len+1;
244                                                   244 
245         c->xdatum_mem_usage += length;            245         c->xdatum_mem_usage += length;
246                                                   246 
247         xd->hashkey = xattr_datum_hashkey(xd->    247         xd->hashkey = xattr_datum_hashkey(xd->xprefix, xd->xname, xd->xvalue, xd->value_len);
248         i = xd->hashkey % XATTRINDEX_HASHSIZE;    248         i = xd->hashkey % XATTRINDEX_HASHSIZE;
249         list_add(&xd->xindex, &c->xattrindex[i    249         list_add(&xd->xindex, &c->xattrindex[i]);
250         if (!retry) {                             250         if (!retry) {
251                 retry = 1;                        251                 retry = 1;
252                 reclaim_xattr_datum(c);           252                 reclaim_xattr_datum(c);
253                 if (!xd->xname)                   253                 if (!xd->xname)
254                         goto retry;               254                         goto retry;
255         }                                         255         }
256                                                   256 
257         dbg_xattr("success on loading xdatum (    257         dbg_xattr("success on loading xdatum (xid=%u, xprefix=%u, xname='%s')\n",
258                   xd->xid, xd->xprefix, xd->xn    258                   xd->xid, xd->xprefix, xd->xname);
259                                                   259 
260         return 0;                                 260         return 0;
261 }                                                 261 }
262                                                   262 
263 static int load_xattr_datum(struct jffs2_sb_in    263 static int load_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
264 {                                                 264 {
265         /* must be called under down_write(xat    265         /* must be called under down_write(xattr_sem);
266          * rc < 0 : recoverable error, try aga    266          * rc < 0 : recoverable error, try again
267          * rc = 0 : success                       267          * rc = 0 : success
268          * rc > 0 : Unrecoverable error, this     268          * rc > 0 : Unrecoverable error, this node should be deleted.
269          */                                       269          */
270         int rc = 0;                               270         int rc = 0;
271                                                   271 
272         BUG_ON(xd->flags & JFFS2_XFLAGS_DEAD);    272         BUG_ON(xd->flags & JFFS2_XFLAGS_DEAD);
273         if (xd->xname)                            273         if (xd->xname)
274                 return 0;                         274                 return 0;
275         if (xd->flags & JFFS2_XFLAGS_INVALID)     275         if (xd->flags & JFFS2_XFLAGS_INVALID)
276                 return JFFS2_XATTR_IS_CORRUPTE    276                 return JFFS2_XATTR_IS_CORRUPTED;
277         if (unlikely(is_xattr_datum_unchecked(    277         if (unlikely(is_xattr_datum_unchecked(c, xd)))
278                 rc = do_verify_xattr_datum(c,     278                 rc = do_verify_xattr_datum(c, xd);
279         if (!rc)                                  279         if (!rc)
280                 rc = do_load_xattr_datum(c, xd    280                 rc = do_load_xattr_datum(c, xd);
281         return rc;                                281         return rc;
282 }                                                 282 }
283                                                   283 
284 static int save_xattr_datum(struct jffs2_sb_in    284 static int save_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
285 {                                                 285 {
286         /* must be called under down_write(xat    286         /* must be called under down_write(xattr_sem) */
287         struct jffs2_raw_xattr rx;                287         struct jffs2_raw_xattr rx;
288         struct kvec vecs[2];                      288         struct kvec vecs[2];
289         size_t length;                            289         size_t length;
290         int rc, totlen;                           290         int rc, totlen;
291         uint32_t phys_ofs = write_ofs(c);         291         uint32_t phys_ofs = write_ofs(c);
292                                                   292 
293         BUG_ON(!xd->xname);                       293         BUG_ON(!xd->xname);
294         BUG_ON(xd->flags & (JFFS2_XFLAGS_DEAD|    294         BUG_ON(xd->flags & (JFFS2_XFLAGS_DEAD|JFFS2_XFLAGS_INVALID));
295                                                   295 
296         vecs[0].iov_base = &rx;                   296         vecs[0].iov_base = &rx;
297         vecs[0].iov_len = sizeof(rx);             297         vecs[0].iov_len = sizeof(rx);
298         vecs[1].iov_base = xd->xname;             298         vecs[1].iov_base = xd->xname;
299         vecs[1].iov_len = xd->name_len + 1 + x    299         vecs[1].iov_len = xd->name_len + 1 + xd->value_len;
300         totlen = vecs[0].iov_len + vecs[1].iov    300         totlen = vecs[0].iov_len + vecs[1].iov_len;
301                                                   301 
302         /* Setup raw-xattr */                     302         /* Setup raw-xattr */
303         memset(&rx, 0, sizeof(rx));               303         memset(&rx, 0, sizeof(rx));
304         rx.magic = cpu_to_je16(JFFS2_MAGIC_BIT    304         rx.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
305         rx.nodetype = cpu_to_je16(JFFS2_NODETY    305         rx.nodetype = cpu_to_je16(JFFS2_NODETYPE_XATTR);
306         rx.totlen = cpu_to_je32(PAD(totlen));     306         rx.totlen = cpu_to_je32(PAD(totlen));
307         rx.hdr_crc = cpu_to_je32(crc32(0, &rx,    307         rx.hdr_crc = cpu_to_je32(crc32(0, &rx, sizeof(struct jffs2_unknown_node) - 4));
308                                                   308 
309         rx.xid = cpu_to_je32(xd->xid);            309         rx.xid = cpu_to_je32(xd->xid);
310         rx.version = cpu_to_je32(++xd->version    310         rx.version = cpu_to_je32(++xd->version);
311         rx.xprefix = xd->xprefix;                 311         rx.xprefix = xd->xprefix;
312         rx.name_len = xd->name_len;               312         rx.name_len = xd->name_len;
313         rx.value_len = cpu_to_je16(xd->value_l    313         rx.value_len = cpu_to_je16(xd->value_len);
314         rx.data_crc = cpu_to_je32(crc32(0, vec    314         rx.data_crc = cpu_to_je32(crc32(0, vecs[1].iov_base, vecs[1].iov_len));
315         rx.node_crc = cpu_to_je32(crc32(0, &rx    315         rx.node_crc = cpu_to_je32(crc32(0, &rx, sizeof(struct jffs2_raw_xattr) - 4));
316                                                   316 
317         rc = jffs2_flash_writev(c, vecs, 2, ph    317         rc = jffs2_flash_writev(c, vecs, 2, phys_ofs, &length, 0);
318         if (rc || totlen != length) {             318         if (rc || totlen != length) {
319                 JFFS2_WARNING("jffs2_flash_wri    319                 JFFS2_WARNING("jffs2_flash_writev()=%d, req=%u, wrote=%zu, at %#08x\n",
320                               rc, totlen, leng    320                               rc, totlen, length, phys_ofs);
321                 rc = rc ? rc : -EIO;              321                 rc = rc ? rc : -EIO;
322                 if (length)                       322                 if (length)
323                         jffs2_add_physical_nod    323                         jffs2_add_physical_node_ref(c, phys_ofs | REF_OBSOLETE, PAD(totlen), NULL);
324                                                   324 
325                 return rc;                        325                 return rc;
326         }                                         326         }
327         /* success */                             327         /* success */
328         jffs2_add_physical_node_ref(c, phys_of    328         jffs2_add_physical_node_ref(c, phys_ofs | REF_PRISTINE, PAD(totlen), (void *)xd);
329                                                   329 
330         dbg_xattr("success on saving xdatum (x    330         dbg_xattr("success on saving xdatum (xid=%u, version=%u, xprefix=%u, xname='%s')\n",
331                   xd->xid, xd->version, xd->xp    331                   xd->xid, xd->version, xd->xprefix, xd->xname);
332                                                   332 
333         return 0;                                 333         return 0;
334 }                                                 334 }
335                                                   335 
336 static struct jffs2_xattr_datum *create_xattr_    336 static struct jffs2_xattr_datum *create_xattr_datum(struct jffs2_sb_info *c,
337                                                   337                                                     int xprefix, const char *xname,
338                                                   338                                                     const char *xvalue, int xsize)
339 {                                                 339 {
340         /* must be called under down_write(xat    340         /* must be called under down_write(xattr_sem) */
341         struct jffs2_xattr_datum *xd;             341         struct jffs2_xattr_datum *xd;
342         uint32_t hashkey, name_len;               342         uint32_t hashkey, name_len;
343         char *data;                               343         char *data;
344         int i, rc;                                344         int i, rc;
345                                                   345 
346         /* Search xattr_datum has same xname/x    346         /* Search xattr_datum has same xname/xvalue by index */
347         hashkey = xattr_datum_hashkey(xprefix,    347         hashkey = xattr_datum_hashkey(xprefix, xname, xvalue, xsize);
348         i = hashkey % XATTRINDEX_HASHSIZE;        348         i = hashkey % XATTRINDEX_HASHSIZE;
349         list_for_each_entry(xd, &c->xattrindex    349         list_for_each_entry(xd, &c->xattrindex[i], xindex) {
350                 if (xd->hashkey==hashkey          350                 if (xd->hashkey==hashkey
351                     && xd->xprefix==xprefix       351                     && xd->xprefix==xprefix
352                     && xd->value_len==xsize       352                     && xd->value_len==xsize
353                     && !strcmp(xd->xname, xnam    353                     && !strcmp(xd->xname, xname)
354                     && !memcmp(xd->xvalue, xva    354                     && !memcmp(xd->xvalue, xvalue, xsize)) {
355                         atomic_inc(&xd->refcnt    355                         atomic_inc(&xd->refcnt);
356                         return xd;                356                         return xd;
357                 }                                 357                 }
358         }                                         358         }
359                                                   359 
360         /* Not found, Create NEW XATTR-Cache *    360         /* Not found, Create NEW XATTR-Cache */
361         name_len = strlen(xname);                 361         name_len = strlen(xname);
362                                                   362 
363         xd = jffs2_alloc_xattr_datum();           363         xd = jffs2_alloc_xattr_datum();
364         if (!xd)                                  364         if (!xd)
365                 return ERR_PTR(-ENOMEM);          365                 return ERR_PTR(-ENOMEM);
366                                                   366 
367         data = kmalloc(name_len + 1 + xsize, G    367         data = kmalloc(name_len + 1 + xsize, GFP_KERNEL);
368         if (!data) {                              368         if (!data) {
369                 jffs2_free_xattr_datum(xd);       369                 jffs2_free_xattr_datum(xd);
370                 return ERR_PTR(-ENOMEM);          370                 return ERR_PTR(-ENOMEM);
371         }                                         371         }
372         strcpy(data, xname);                      372         strcpy(data, xname);
373         memcpy(data + name_len + 1, xvalue, xs    373         memcpy(data + name_len + 1, xvalue, xsize);
374                                                   374 
375         atomic_set(&xd->refcnt, 1);               375         atomic_set(&xd->refcnt, 1);
376         xd->xid = ++c->highest_xid;               376         xd->xid = ++c->highest_xid;
377         xd->flags |= JFFS2_XFLAGS_HOT;            377         xd->flags |= JFFS2_XFLAGS_HOT;
378         xd->xprefix = xprefix;                    378         xd->xprefix = xprefix;
379                                                   379 
380         xd->hashkey = hashkey;                    380         xd->hashkey = hashkey;
381         xd->xname = data;                         381         xd->xname = data;
382         xd->xvalue = data + name_len + 1;         382         xd->xvalue = data + name_len + 1;
383         xd->name_len = name_len;                  383         xd->name_len = name_len;
384         xd->value_len = xsize;                    384         xd->value_len = xsize;
385         xd->data_crc = crc32(0, data, xd->name    385         xd->data_crc = crc32(0, data, xd->name_len + 1 + xd->value_len);
386                                                   386 
387         rc = save_xattr_datum(c, xd);             387         rc = save_xattr_datum(c, xd);
388         if (rc) {                                 388         if (rc) {
389                 kfree(xd->xname);                 389                 kfree(xd->xname);
390                 jffs2_free_xattr_datum(xd);       390                 jffs2_free_xattr_datum(xd);
391                 return ERR_PTR(rc);               391                 return ERR_PTR(rc);
392         }                                         392         }
393                                                   393 
394         /* Insert Hash Index */                   394         /* Insert Hash Index */
395         i = hashkey % XATTRINDEX_HASHSIZE;        395         i = hashkey % XATTRINDEX_HASHSIZE;
396         list_add(&xd->xindex, &c->xattrindex[i    396         list_add(&xd->xindex, &c->xattrindex[i]);
397                                                   397 
398         c->xdatum_mem_usage += (xd->name_len +    398         c->xdatum_mem_usage += (xd->name_len + 1 + xd->value_len);
399         reclaim_xattr_datum(c);                   399         reclaim_xattr_datum(c);
400                                                   400 
401         return xd;                                401         return xd;
402 }                                                 402 }
403                                                   403 
404 static void unrefer_xattr_datum(struct jffs2_s    404 static void unrefer_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
405 {                                                 405 {
406         /* must be called under down_write(xat    406         /* must be called under down_write(xattr_sem) */
407         if (atomic_dec_and_lock(&xd->refcnt, &    407         if (atomic_dec_and_lock(&xd->refcnt, &c->erase_completion_lock)) {
408                 unload_xattr_datum(c, xd);        408                 unload_xattr_datum(c, xd);
409                 xd->flags |= JFFS2_XFLAGS_DEAD    409                 xd->flags |= JFFS2_XFLAGS_DEAD;
410                 if (xd->node == (void *)xd) {     410                 if (xd->node == (void *)xd) {
411                         BUG_ON(!(xd->flags & J    411                         BUG_ON(!(xd->flags & JFFS2_XFLAGS_INVALID));
412                         jffs2_free_xattr_datum    412                         jffs2_free_xattr_datum(xd);
413                 } else {                          413                 } else {
414                         list_add(&xd->xindex,     414                         list_add(&xd->xindex, &c->xattr_dead_list);
415                 }                                 415                 }
416                 spin_unlock(&c->erase_completi    416                 spin_unlock(&c->erase_completion_lock);
417                                                   417 
418                 dbg_xattr("xdatum(xid=%u, vers    418                 dbg_xattr("xdatum(xid=%u, version=%u) was removed.\n",
419                           xd->xid, xd->version    419                           xd->xid, xd->version);
420         }                                         420         }
421 }                                                 421 }
422                                                   422 
423 /* -------- xref related functions -----------    423 /* -------- xref related functions ------------------
424  * verify_xattr_ref(c, ref)                       424  * verify_xattr_ref(c, ref)
425  *   is used to load xref information from med    425  *   is used to load xref information from medium. Because summary data does not
426  *   contain xid/ino, it's necessary to verify    426  *   contain xid/ino, it's necessary to verify once while mounting process.
427  * save_xattr_ref(c, ref)                         427  * save_xattr_ref(c, ref)
428  *   is used to write xref to medium. If delet    428  *   is used to write xref to medium. If delete marker is marked, it write
429  *   a delete marker of xref into medium.         429  *   a delete marker of xref into medium.
430  * create_xattr_ref(c, ic, xd)                    430  * create_xattr_ref(c, ic, xd)
431  *   is used to create a new xref and write to    431  *   is used to create a new xref and write to medium.
432  * delete_xattr_ref(c, ref)                       432  * delete_xattr_ref(c, ref)
433  *   is used to delete jffs2_xattr_ref. It mar    433  *   is used to delete jffs2_xattr_ref. It marks xref XREF_DELETE_MARKER,
434  *   and allows GC to reclaim those physical n    434  *   and allows GC to reclaim those physical nodes.
435  * jffs2_xattr_delete_inode(c, ic)                435  * jffs2_xattr_delete_inode(c, ic)
436  *   is called to remove xrefs related to obso    436  *   is called to remove xrefs related to obsolete inode when inode is unlinked.
437  * jffs2_xattr_free_inode(c, ic)                  437  * jffs2_xattr_free_inode(c, ic)
438  *   is called to release xattr related object    438  *   is called to release xattr related objects when unmounting. 
439  * check_xattr_ref_inode(c, ic)                   439  * check_xattr_ref_inode(c, ic)
440  *   is used to confirm inode does not have du    440  *   is used to confirm inode does not have duplicate xattr name/value pair.
441  * jffs2_xattr_do_crccheck_inode(c, ic)           441  * jffs2_xattr_do_crccheck_inode(c, ic)
442  *   is used to force xattr data integrity che    442  *   is used to force xattr data integrity check during the initial gc scan.
443  * -------------------------------------------    443  * -------------------------------------------------- */
444 static int verify_xattr_ref(struct jffs2_sb_in    444 static int verify_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
445 {                                                 445 {
446         struct jffs2_eraseblock *jeb;             446         struct jffs2_eraseblock *jeb;
447         struct jffs2_raw_node_ref *raw;           447         struct jffs2_raw_node_ref *raw;
448         struct jffs2_raw_xref rr;                 448         struct jffs2_raw_xref rr;
449         size_t readlen;                           449         size_t readlen;
450         uint32_t crc, offset, totlen;             450         uint32_t crc, offset, totlen;
451         int rc;                                   451         int rc;
452                                                   452 
453         spin_lock(&c->erase_completion_lock);     453         spin_lock(&c->erase_completion_lock);
454         if (ref_flags(ref->node) != REF_UNCHEC    454         if (ref_flags(ref->node) != REF_UNCHECKED)
455                 goto complete;                    455                 goto complete;
456         offset = ref_offset(ref->node);           456         offset = ref_offset(ref->node);
457         spin_unlock(&c->erase_completion_lock)    457         spin_unlock(&c->erase_completion_lock);
458                                                   458 
459         rc = jffs2_flash_read(c, offset, sizeo    459         rc = jffs2_flash_read(c, offset, sizeof(rr), &readlen, (char *)&rr);
460         if (rc || sizeof(rr) != readlen) {        460         if (rc || sizeof(rr) != readlen) {
461                 JFFS2_WARNING("jffs2_flash_rea    461                 JFFS2_WARNING("jffs2_flash_read()=%d, req=%zu, read=%zu, at %#08x\n",
462                               rc, sizeof(rr),     462                               rc, sizeof(rr), readlen, offset);
463                 return rc ? rc : -EIO;            463                 return rc ? rc : -EIO;
464         }                                         464         }
465         /* obsolete node */                       465         /* obsolete node */
466         crc = crc32(0, &rr, sizeof(rr) - 4);      466         crc = crc32(0, &rr, sizeof(rr) - 4);
467         if (crc != je32_to_cpu(rr.node_crc)) {    467         if (crc != je32_to_cpu(rr.node_crc)) {
468                 JFFS2_ERROR("node CRC failed a    468                 JFFS2_ERROR("node CRC failed at %#08x, read=%#08x, calc=%#08x\n",
469                             offset, je32_to_cp    469                             offset, je32_to_cpu(rr.node_crc), crc);
470                 return JFFS2_XATTR_IS_CORRUPTE    470                 return JFFS2_XATTR_IS_CORRUPTED;
471         }                                         471         }
472         if (je16_to_cpu(rr.magic) != JFFS2_MAG    472         if (je16_to_cpu(rr.magic) != JFFS2_MAGIC_BITMASK
473             || je16_to_cpu(rr.nodetype) != JFF    473             || je16_to_cpu(rr.nodetype) != JFFS2_NODETYPE_XREF
474             || je32_to_cpu(rr.totlen) != PAD(s    474             || je32_to_cpu(rr.totlen) != PAD(sizeof(rr))) {
475                 JFFS2_ERROR("inconsistent xref    475                 JFFS2_ERROR("inconsistent xref at %#08x, magic=%#04x/%#04x, "
476                             "nodetype=%#04x/%#    476                             "nodetype=%#04x/%#04x, totlen=%u/%zu\n",
477                             offset, je16_to_cp    477                             offset, je16_to_cpu(rr.magic), JFFS2_MAGIC_BITMASK,
478                             je16_to_cpu(rr.nod    478                             je16_to_cpu(rr.nodetype), JFFS2_NODETYPE_XREF,
479                             je32_to_cpu(rr.tot    479                             je32_to_cpu(rr.totlen), PAD(sizeof(rr)));
480                 return JFFS2_XATTR_IS_CORRUPTE    480                 return JFFS2_XATTR_IS_CORRUPTED;
481         }                                         481         }
482         ref->ino = je32_to_cpu(rr.ino);           482         ref->ino = je32_to_cpu(rr.ino);
483         ref->xid = je32_to_cpu(rr.xid);           483         ref->xid = je32_to_cpu(rr.xid);
484         ref->xseqno = je32_to_cpu(rr.xseqno);     484         ref->xseqno = je32_to_cpu(rr.xseqno);
485         if (ref->xseqno > c->highest_xseqno)      485         if (ref->xseqno > c->highest_xseqno)
486                 c->highest_xseqno = (ref->xseq    486                 c->highest_xseqno = (ref->xseqno & ~XREF_DELETE_MARKER);
487                                                   487 
488         spin_lock(&c->erase_completion_lock);     488         spin_lock(&c->erase_completion_lock);
489  complete:                                        489  complete:
490         for (raw=ref->node; raw != (void *)ref    490         for (raw=ref->node; raw != (void *)ref; raw=raw->next_in_ino) {
491                 jeb = &c->blocks[ref_offset(ra    491                 jeb = &c->blocks[ref_offset(raw) / c->sector_size];
492                 totlen = PAD(ref_totlen(c, jeb    492                 totlen = PAD(ref_totlen(c, jeb, raw));
493                 if (ref_flags(raw) == REF_UNCH    493                 if (ref_flags(raw) == REF_UNCHECKED) {
494                         c->unchecked_size -= t    494                         c->unchecked_size -= totlen; c->used_size += totlen;
495                         jeb->unchecked_size -=    495                         jeb->unchecked_size -= totlen; jeb->used_size += totlen;
496                 }                                 496                 }
497                 raw->flash_offset = ref_offset    497                 raw->flash_offset = ref_offset(raw) | ((ref->node==raw) ? REF_PRISTINE : REF_NORMAL);
498         }                                         498         }
499         spin_unlock(&c->erase_completion_lock)    499         spin_unlock(&c->erase_completion_lock);
500                                                   500 
501         dbg_xattr("success on verifying xref (    501         dbg_xattr("success on verifying xref (ino=%u, xid=%u) at %#08x\n",
502                   ref->ino, ref->xid, ref_offs    502                   ref->ino, ref->xid, ref_offset(ref->node));
503         return 0;                                 503         return 0;
504 }                                                 504 }
505                                                   505 
506 static int save_xattr_ref(struct jffs2_sb_info    506 static int save_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
507 {                                                 507 {
508         /* must be called under down_write(xat    508         /* must be called under down_write(xattr_sem) */
509         struct jffs2_raw_xref rr;                 509         struct jffs2_raw_xref rr;
510         size_t length;                            510         size_t length;
511         uint32_t xseqno, phys_ofs = write_ofs(    511         uint32_t xseqno, phys_ofs = write_ofs(c);
512         int ret;                                  512         int ret;
513                                                   513 
514         rr.magic = cpu_to_je16(JFFS2_MAGIC_BIT    514         rr.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
515         rr.nodetype = cpu_to_je16(JFFS2_NODETY    515         rr.nodetype = cpu_to_je16(JFFS2_NODETYPE_XREF);
516         rr.totlen = cpu_to_je32(PAD(sizeof(rr)    516         rr.totlen = cpu_to_je32(PAD(sizeof(rr)));
517         rr.hdr_crc = cpu_to_je32(crc32(0, &rr,    517         rr.hdr_crc = cpu_to_je32(crc32(0, &rr, sizeof(struct jffs2_unknown_node) - 4));
518                                                   518 
519         xseqno = (c->highest_xseqno += 2);        519         xseqno = (c->highest_xseqno += 2);
520         if (is_xattr_ref_dead(ref)) {             520         if (is_xattr_ref_dead(ref)) {
521                 xseqno |= XREF_DELETE_MARKER;     521                 xseqno |= XREF_DELETE_MARKER;
522                 rr.ino = cpu_to_je32(ref->ino)    522                 rr.ino = cpu_to_je32(ref->ino);
523                 rr.xid = cpu_to_je32(ref->xid)    523                 rr.xid = cpu_to_je32(ref->xid);
524         } else {                                  524         } else {
525                 rr.ino = cpu_to_je32(ref->ic->    525                 rr.ino = cpu_to_je32(ref->ic->ino);
526                 rr.xid = cpu_to_je32(ref->xd->    526                 rr.xid = cpu_to_je32(ref->xd->xid);
527         }                                         527         }
528         rr.xseqno = cpu_to_je32(xseqno);          528         rr.xseqno = cpu_to_je32(xseqno);
529         rr.node_crc = cpu_to_je32(crc32(0, &rr    529         rr.node_crc = cpu_to_je32(crc32(0, &rr, sizeof(rr) - 4));
530                                                   530 
531         ret = jffs2_flash_write(c, phys_ofs, s    531         ret = jffs2_flash_write(c, phys_ofs, sizeof(rr), &length, (char *)&rr);
532         if (ret || sizeof(rr) != length) {        532         if (ret || sizeof(rr) != length) {
533                 JFFS2_WARNING("jffs2_flash_wri    533                 JFFS2_WARNING("jffs2_flash_write() returned %d, request=%zu, retlen=%zu, at %#08x\n",
534                               ret, sizeof(rr),    534                               ret, sizeof(rr), length, phys_ofs);
535                 ret = ret ? ret : -EIO;           535                 ret = ret ? ret : -EIO;
536                 if (length)                       536                 if (length)
537                         jffs2_add_physical_nod    537                         jffs2_add_physical_node_ref(c, phys_ofs | REF_OBSOLETE, PAD(sizeof(rr)), NULL);
538                                                   538 
539                 return ret;                       539                 return ret;
540         }                                         540         }
541         /* success */                             541         /* success */
542         ref->xseqno = xseqno;                     542         ref->xseqno = xseqno;
543         jffs2_add_physical_node_ref(c, phys_of    543         jffs2_add_physical_node_ref(c, phys_ofs | REF_PRISTINE, PAD(sizeof(rr)), (void *)ref);
544                                                   544 
545         dbg_xattr("success on saving xref (ino    545         dbg_xattr("success on saving xref (ino=%u, xid=%u)\n", ref->ic->ino, ref->xd->xid);
546                                                   546 
547         return 0;                                 547         return 0;
548 }                                                 548 }
549                                                   549 
550 static struct jffs2_xattr_ref *create_xattr_re    550 static struct jffs2_xattr_ref *create_xattr_ref(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic,
551                                                   551                                                 struct jffs2_xattr_datum *xd)
552 {                                                 552 {
553         /* must be called under down_write(xat    553         /* must be called under down_write(xattr_sem) */
554         struct jffs2_xattr_ref *ref;              554         struct jffs2_xattr_ref *ref;
555         int ret;                                  555         int ret;
556                                                   556 
557         ref = jffs2_alloc_xattr_ref();            557         ref = jffs2_alloc_xattr_ref();
558         if (!ref)                                 558         if (!ref)
559                 return ERR_PTR(-ENOMEM);          559                 return ERR_PTR(-ENOMEM);
560         ref->ic = ic;                             560         ref->ic = ic;
561         ref->xd = xd;                             561         ref->xd = xd;
562                                                   562 
563         ret = save_xattr_ref(c, ref);             563         ret = save_xattr_ref(c, ref);
564         if (ret) {                                564         if (ret) {
565                 jffs2_free_xattr_ref(ref);        565                 jffs2_free_xattr_ref(ref);
566                 return ERR_PTR(ret);              566                 return ERR_PTR(ret);
567         }                                         567         }
568                                                   568 
569         /* Chain to inode */                      569         /* Chain to inode */
570         ref->next = ic->xref;                     570         ref->next = ic->xref;
571         ic->xref = ref;                           571         ic->xref = ref;
572                                                   572 
573         return ref; /* success */                 573         return ref; /* success */
574 }                                                 574 }
575                                                   575 
576 static void delete_xattr_ref(struct jffs2_sb_i    576 static void delete_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
577 {                                                 577 {
578         /* must be called under down_write(xat    578         /* must be called under down_write(xattr_sem) */
579         struct jffs2_xattr_datum *xd;             579         struct jffs2_xattr_datum *xd;
580                                                   580 
581         xd = ref->xd;                             581         xd = ref->xd;
582         ref->xseqno |= XREF_DELETE_MARKER;        582         ref->xseqno |= XREF_DELETE_MARKER;
583         ref->ino = ref->ic->ino;                  583         ref->ino = ref->ic->ino;
584         ref->xid = ref->xd->xid;                  584         ref->xid = ref->xd->xid;
585         spin_lock(&c->erase_completion_lock);     585         spin_lock(&c->erase_completion_lock);
586         ref->next = c->xref_dead_list;            586         ref->next = c->xref_dead_list;
587         c->xref_dead_list = ref;                  587         c->xref_dead_list = ref;
588         spin_unlock(&c->erase_completion_lock)    588         spin_unlock(&c->erase_completion_lock);
589                                                   589 
590         dbg_xattr("xref(ino=%u, xid=%u, xseqno    590         dbg_xattr("xref(ino=%u, xid=%u, xseqno=%u) was removed.\n",
591                   ref->ino, ref->xid, ref->xse    591                   ref->ino, ref->xid, ref->xseqno);
592                                                   592 
593         unrefer_xattr_datum(c, xd);               593         unrefer_xattr_datum(c, xd);
594 }                                                 594 }
595                                                   595 
596 void jffs2_xattr_delete_inode(struct jffs2_sb_    596 void jffs2_xattr_delete_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
597 {                                                 597 {
598         /* It's called from jffs2_evict_inode(    598         /* It's called from jffs2_evict_inode() on inode removing.
599            When an inode with XATTR is removed    599            When an inode with XATTR is removed, those XATTRs must be removed. */
600         struct jffs2_xattr_ref *ref, *_ref;       600         struct jffs2_xattr_ref *ref, *_ref;
601                                                   601 
602         if (!ic || ic->pino_nlink > 0)            602         if (!ic || ic->pino_nlink > 0)
603                 return;                           603                 return;
604                                                   604 
605         down_write(&c->xattr_sem);                605         down_write(&c->xattr_sem);
606         for (ref = ic->xref; ref; ref = _ref)     606         for (ref = ic->xref; ref; ref = _ref) {
607                 _ref = ref->next;                 607                 _ref = ref->next;
608                 delete_xattr_ref(c, ref);         608                 delete_xattr_ref(c, ref);
609         }                                         609         }
610         ic->xref = NULL;                          610         ic->xref = NULL;
611         up_write(&c->xattr_sem);                  611         up_write(&c->xattr_sem);
612 }                                                 612 }
613                                                   613 
614 void jffs2_xattr_free_inode(struct jffs2_sb_in    614 void jffs2_xattr_free_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
615 {                                                 615 {
616         /* It's called from jffs2_free_ino_cac    616         /* It's called from jffs2_free_ino_caches() until unmounting FS. */
617         struct jffs2_xattr_datum *xd;             617         struct jffs2_xattr_datum *xd;
618         struct jffs2_xattr_ref *ref, *_ref;       618         struct jffs2_xattr_ref *ref, *_ref;
619                                                   619 
620         down_write(&c->xattr_sem);                620         down_write(&c->xattr_sem);
621         for (ref = ic->xref; ref; ref = _ref)     621         for (ref = ic->xref; ref; ref = _ref) {
622                 _ref = ref->next;                 622                 _ref = ref->next;
623                 xd = ref->xd;                     623                 xd = ref->xd;
624                 if (atomic_dec_and_test(&xd->r    624                 if (atomic_dec_and_test(&xd->refcnt)) {
625                         unload_xattr_datum(c,     625                         unload_xattr_datum(c, xd);
626                         jffs2_free_xattr_datum    626                         jffs2_free_xattr_datum(xd);
627                 }                                 627                 }
628                 jffs2_free_xattr_ref(ref);        628                 jffs2_free_xattr_ref(ref);
629         }                                         629         }
630         ic->xref = NULL;                          630         ic->xref = NULL;
631         up_write(&c->xattr_sem);                  631         up_write(&c->xattr_sem);
632 }                                                 632 }
633                                                   633 
634 static int check_xattr_ref_inode(struct jffs2_    634 static int check_xattr_ref_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
635 {                                                 635 {
636         /* success of check_xattr_ref_inode()     636         /* success of check_xattr_ref_inode() means that inode (ic) dose not have
637          * duplicate name/value pairs. If dupl    637          * duplicate name/value pairs. If duplicate name/value pair would be found,
638          * one will be removed.                   638          * one will be removed.
639          */                                       639          */
640         struct jffs2_xattr_ref *ref, *cmp, **p    640         struct jffs2_xattr_ref *ref, *cmp, **pref, **pcmp;
641         int rc = 0;                               641         int rc = 0;
642                                                   642 
643         if (likely(ic->flags & INO_FLAGS_XATTR    643         if (likely(ic->flags & INO_FLAGS_XATTR_CHECKED))
644                 return 0;                         644                 return 0;
645         down_write(&c->xattr_sem);                645         down_write(&c->xattr_sem);
646  retry:                                           646  retry:
647         rc = 0;                                   647         rc = 0;
648         for (ref=ic->xref, pref=&ic->xref; ref    648         for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
649                 if (!ref->xd->xname) {            649                 if (!ref->xd->xname) {
650                         rc = load_xattr_datum(    650                         rc = load_xattr_datum(c, ref->xd);
651                         if (unlikely(rc > 0))     651                         if (unlikely(rc > 0)) {
652                                 *pref = ref->n    652                                 *pref = ref->next;
653                                 delete_xattr_r    653                                 delete_xattr_ref(c, ref);
654                                 goto retry;       654                                 goto retry;
655                         } else if (unlikely(rc    655                         } else if (unlikely(rc < 0))
656                                 goto out;         656                                 goto out;
657                 }                                 657                 }
658                 for (cmp=ref->next, pcmp=&ref-    658                 for (cmp=ref->next, pcmp=&ref->next; cmp; pcmp=&cmp->next, cmp=cmp->next) {
659                         if (!cmp->xd->xname) {    659                         if (!cmp->xd->xname) {
660                                 ref->xd->flags    660                                 ref->xd->flags |= JFFS2_XFLAGS_BIND;
661                                 rc = load_xatt    661                                 rc = load_xattr_datum(c, cmp->xd);
662                                 ref->xd->flags    662                                 ref->xd->flags &= ~JFFS2_XFLAGS_BIND;
663                                 if (unlikely(r    663                                 if (unlikely(rc > 0)) {
664                                         *pcmp     664                                         *pcmp = cmp->next;
665                                         delete    665                                         delete_xattr_ref(c, cmp);
666                                         goto r    666                                         goto retry;
667                                 } else if (unl    667                                 } else if (unlikely(rc < 0))
668                                         goto o    668                                         goto out;
669                         }                         669                         }
670                         if (ref->xd->xprefix =    670                         if (ref->xd->xprefix == cmp->xd->xprefix
671                             && !strcmp(ref->xd    671                             && !strcmp(ref->xd->xname, cmp->xd->xname)) {
672                                 if (ref->xseqn    672                                 if (ref->xseqno > cmp->xseqno) {
673                                         *pcmp     673                                         *pcmp = cmp->next;
674                                         delete    674                                         delete_xattr_ref(c, cmp);
675                                 } else {          675                                 } else {
676                                         *pref     676                                         *pref = ref->next;
677                                         delete    677                                         delete_xattr_ref(c, ref);
678                                 }                 678                                 }
679                                 goto retry;       679                                 goto retry;
680                         }                         680                         }
681                 }                                 681                 }
682         }                                         682         }
683         ic->flags |= INO_FLAGS_XATTR_CHECKED;     683         ic->flags |= INO_FLAGS_XATTR_CHECKED;
684  out:                                             684  out:
685         up_write(&c->xattr_sem);                  685         up_write(&c->xattr_sem);
686                                                   686 
687         return rc;                                687         return rc;
688 }                                                 688 }
689                                                   689 
690 void jffs2_xattr_do_crccheck_inode(struct jffs    690 void jffs2_xattr_do_crccheck_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
691 {                                                 691 {
692         check_xattr_ref_inode(c, ic);             692         check_xattr_ref_inode(c, ic);
693 }                                                 693 }
694                                                   694 
695 /* -------- xattr subsystem functions --------    695 /* -------- xattr subsystem functions ---------------
696  * jffs2_init_xattr_subsystem(c)                  696  * jffs2_init_xattr_subsystem(c)
697  *   is used to initialize semaphore and list_    697  *   is used to initialize semaphore and list_head, and some variables.
698  * jffs2_find_xattr_datum(c, xid)                 698  * jffs2_find_xattr_datum(c, xid)
699  *   is used to lookup xdatum while scanning p    699  *   is used to lookup xdatum while scanning process.
700  * jffs2_clear_xattr_subsystem(c)                 700  * jffs2_clear_xattr_subsystem(c)
701  *   is used to release any xattr related obje    701  *   is used to release any xattr related objects.
702  * jffs2_build_xattr_subsystem(c)                 702  * jffs2_build_xattr_subsystem(c)
703  *   is used to associate xdatum and xref whil    703  *   is used to associate xdatum and xref while super block building process.
704  * jffs2_setup_xattr_datum(c, xid, version)       704  * jffs2_setup_xattr_datum(c, xid, version)
705  *   is used to insert xdatum while scanning p    705  *   is used to insert xdatum while scanning process.
706  * -------------------------------------------    706  * -------------------------------------------------- */
707 void jffs2_init_xattr_subsystem(struct jffs2_s    707 void jffs2_init_xattr_subsystem(struct jffs2_sb_info *c)
708 {                                                 708 {
709         int i;                                    709         int i;
710                                                   710 
711         for (i=0; i < XATTRINDEX_HASHSIZE; i++    711         for (i=0; i < XATTRINDEX_HASHSIZE; i++)
712                 INIT_LIST_HEAD(&c->xattrindex[    712                 INIT_LIST_HEAD(&c->xattrindex[i]);
713         INIT_LIST_HEAD(&c->xattr_unchecked);      713         INIT_LIST_HEAD(&c->xattr_unchecked);
714         INIT_LIST_HEAD(&c->xattr_dead_list);      714         INIT_LIST_HEAD(&c->xattr_dead_list);
715         c->xref_dead_list = NULL;                 715         c->xref_dead_list = NULL;
716         c->xref_temp = NULL;                      716         c->xref_temp = NULL;
717                                                   717 
718         init_rwsem(&c->xattr_sem);                718         init_rwsem(&c->xattr_sem);
719         c->highest_xid = 0;                       719         c->highest_xid = 0;
720         c->highest_xseqno = 0;                    720         c->highest_xseqno = 0;
721         c->xdatum_mem_usage = 0;                  721         c->xdatum_mem_usage = 0;
722         c->xdatum_mem_threshold = 32 * 1024;      722         c->xdatum_mem_threshold = 32 * 1024;    /* Default 32KB */
723 }                                                 723 }
724                                                   724 
725 static struct jffs2_xattr_datum *jffs2_find_xa    725 static struct jffs2_xattr_datum *jffs2_find_xattr_datum(struct jffs2_sb_info *c, uint32_t xid)
726 {                                                 726 {
727         struct jffs2_xattr_datum *xd;             727         struct jffs2_xattr_datum *xd;
728         int i = xid % XATTRINDEX_HASHSIZE;        728         int i = xid % XATTRINDEX_HASHSIZE;
729                                                   729 
730         /* It's only used in scanning/building    730         /* It's only used in scanning/building process. */
731         BUG_ON(!(c->flags & (JFFS2_SB_FLAG_SCA    731         BUG_ON(!(c->flags & (JFFS2_SB_FLAG_SCANNING|JFFS2_SB_FLAG_BUILDING)));
732                                                   732 
733         list_for_each_entry(xd, &c->xattrindex    733         list_for_each_entry(xd, &c->xattrindex[i], xindex) {
734                 if (xd->xid==xid)                 734                 if (xd->xid==xid)
735                         return xd;                735                         return xd;
736         }                                         736         }
737         return NULL;                              737         return NULL;
738 }                                                 738 }
739                                                   739 
740 void jffs2_clear_xattr_subsystem(struct jffs2_    740 void jffs2_clear_xattr_subsystem(struct jffs2_sb_info *c)
741 {                                                 741 {
742         struct jffs2_xattr_datum *xd, *_xd;       742         struct jffs2_xattr_datum *xd, *_xd;
743         struct jffs2_xattr_ref *ref, *_ref;       743         struct jffs2_xattr_ref *ref, *_ref;
744         int i;                                    744         int i;
745                                                   745 
746         for (ref=c->xref_temp; ref; ref = _ref    746         for (ref=c->xref_temp; ref; ref = _ref) {
747                 _ref = ref->next;                 747                 _ref = ref->next;
748                 jffs2_free_xattr_ref(ref);        748                 jffs2_free_xattr_ref(ref);
749         }                                         749         }
750                                                   750 
751         for (ref=c->xref_dead_list; ref; ref =    751         for (ref=c->xref_dead_list; ref; ref = _ref) {
752                 _ref = ref->next;                 752                 _ref = ref->next;
753                 jffs2_free_xattr_ref(ref);        753                 jffs2_free_xattr_ref(ref);
754         }                                         754         }
755                                                   755 
756         for (i=0; i < XATTRINDEX_HASHSIZE; i++    756         for (i=0; i < XATTRINDEX_HASHSIZE; i++) {
757                 list_for_each_entry_safe(xd, _    757                 list_for_each_entry_safe(xd, _xd, &c->xattrindex[i], xindex) {
758                         list_del(&xd->xindex);    758                         list_del(&xd->xindex);
759                         kfree(xd->xname);         759                         kfree(xd->xname);
760                         jffs2_free_xattr_datum    760                         jffs2_free_xattr_datum(xd);
761                 }                                 761                 }
762         }                                         762         }
763                                                   763 
764         list_for_each_entry_safe(xd, _xd, &c->    764         list_for_each_entry_safe(xd, _xd, &c->xattr_dead_list, xindex) {
765                 list_del(&xd->xindex);            765                 list_del(&xd->xindex);
766                 jffs2_free_xattr_datum(xd);       766                 jffs2_free_xattr_datum(xd);
767         }                                         767         }
768         list_for_each_entry_safe(xd, _xd, &c->    768         list_for_each_entry_safe(xd, _xd, &c->xattr_unchecked, xindex) {
769                 list_del(&xd->xindex);            769                 list_del(&xd->xindex);
770                 jffs2_free_xattr_datum(xd);       770                 jffs2_free_xattr_datum(xd);
771         }                                         771         }
772 }                                                 772 }
773                                                   773 
774 #define XREF_TMPHASH_SIZE       (128)             774 #define XREF_TMPHASH_SIZE       (128)
775 int jffs2_build_xattr_subsystem(struct jffs2_s !! 775 void jffs2_build_xattr_subsystem(struct jffs2_sb_info *c)
776 {                                                 776 {
777         struct jffs2_xattr_ref *ref, *_ref;       777         struct jffs2_xattr_ref *ref, *_ref;
778         struct jffs2_xattr_ref **xref_tmphash; !! 778         struct jffs2_xattr_ref *xref_tmphash[XREF_TMPHASH_SIZE];
779         struct jffs2_xattr_datum *xd, *_xd;       779         struct jffs2_xattr_datum *xd, *_xd;
780         struct jffs2_inode_cache *ic;             780         struct jffs2_inode_cache *ic;
781         struct jffs2_raw_node_ref *raw;           781         struct jffs2_raw_node_ref *raw;
782         int i, xdatum_count = 0, xdatum_unchec    782         int i, xdatum_count = 0, xdatum_unchecked_count = 0, xref_count = 0;
783         int xdatum_orphan_count = 0, xref_orph    783         int xdatum_orphan_count = 0, xref_orphan_count = 0, xref_dead_count = 0;
784                                                   784 
785         BUG_ON(!(c->flags & JFFS2_SB_FLAG_BUIL    785         BUG_ON(!(c->flags & JFFS2_SB_FLAG_BUILDING));
786                                                   786 
787         xref_tmphash = kcalloc(XREF_TMPHASH_SI << 
788                                sizeof(struct j << 
789         if (!xref_tmphash)                     << 
790                 return -ENOMEM;                << 
791                                                << 
792         /* Phase.1 : Merge same xref */           787         /* Phase.1 : Merge same xref */
                                                   >> 788         for (i=0; i < XREF_TMPHASH_SIZE; i++)
                                                   >> 789                 xref_tmphash[i] = NULL;
793         for (ref=c->xref_temp; ref; ref=_ref)     790         for (ref=c->xref_temp; ref; ref=_ref) {
794                 struct jffs2_xattr_ref *tmp;      791                 struct jffs2_xattr_ref *tmp;
795                                                   792 
796                 _ref = ref->next;                 793                 _ref = ref->next;
797                 if (ref_flags(ref->node) != RE    794                 if (ref_flags(ref->node) != REF_PRISTINE) {
798                         if (verify_xattr_ref(c    795                         if (verify_xattr_ref(c, ref)) {
799                                 BUG_ON(ref->no    796                                 BUG_ON(ref->node->next_in_ino != (void *)ref);
800                                 ref->node->nex    797                                 ref->node->next_in_ino = NULL;
801                                 jffs2_mark_nod    798                                 jffs2_mark_node_obsolete(c, ref->node);
802                                 jffs2_free_xat    799                                 jffs2_free_xattr_ref(ref);
803                                 continue;         800                                 continue;
804                         }                         801                         }
805                 }                                 802                 }
806                                                   803 
807                 i = (ref->ino ^ ref->xid) % XR    804                 i = (ref->ino ^ ref->xid) % XREF_TMPHASH_SIZE;
808                 for (tmp=xref_tmphash[i]; tmp;    805                 for (tmp=xref_tmphash[i]; tmp; tmp=tmp->next) {
809                         if (tmp->ino == ref->i    806                         if (tmp->ino == ref->ino && tmp->xid == ref->xid)
810                                 break;            807                                 break;
811                 }                                 808                 }
812                 if (tmp) {                        809                 if (tmp) {
813                         raw = ref->node;          810                         raw = ref->node;
814                         if (ref->xseqno > tmp-    811                         if (ref->xseqno > tmp->xseqno) {
815                                 tmp->xseqno =     812                                 tmp->xseqno = ref->xseqno;
816                                 raw->next_in_i    813                                 raw->next_in_ino = tmp->node;
817                                 tmp->node = ra    814                                 tmp->node = raw;
818                         } else {                  815                         } else {
819                                 raw->next_in_i    816                                 raw->next_in_ino = tmp->node->next_in_ino;
820                                 tmp->node->nex    817                                 tmp->node->next_in_ino = raw;
821                         }                         818                         }
822                         jffs2_free_xattr_ref(r    819                         jffs2_free_xattr_ref(ref);
823                         continue;                 820                         continue;
824                 } else {                          821                 } else {
825                         ref->next = xref_tmpha    822                         ref->next = xref_tmphash[i];
826                         xref_tmphash[i] = ref;    823                         xref_tmphash[i] = ref;
827                 }                                 824                 }
828         }                                         825         }
829         c->xref_temp = NULL;                      826         c->xref_temp = NULL;
830                                                   827 
831         /* Phase.2 : Bind xref with inode_cach    828         /* Phase.2 : Bind xref with inode_cache and xattr_datum */
832         for (i=0; i < XREF_TMPHASH_SIZE; i++)     829         for (i=0; i < XREF_TMPHASH_SIZE; i++) {
833                 for (ref=xref_tmphash[i]; ref;    830                 for (ref=xref_tmphash[i]; ref; ref=_ref) {
834                         xref_count++;             831                         xref_count++;
835                         _ref = ref->next;         832                         _ref = ref->next;
836                         if (is_xattr_ref_dead(    833                         if (is_xattr_ref_dead(ref)) {
837                                 ref->next = c-    834                                 ref->next = c->xref_dead_list;
838                                 c->xref_dead_l    835                                 c->xref_dead_list = ref;
839                                 xref_dead_coun    836                                 xref_dead_count++;
840                                 continue;         837                                 continue;
841                         }                         838                         }
842                         /* At this point, ref-    839                         /* At this point, ref->xid and ref->ino contain XID and inode number.
843                            ref->xd and ref->ic    840                            ref->xd and ref->ic are not valid yet. */
844                         xd = jffs2_find_xattr_    841                         xd = jffs2_find_xattr_datum(c, ref->xid);
845                         ic = jffs2_get_ino_cac    842                         ic = jffs2_get_ino_cache(c, ref->ino);
846                         if (!xd || !ic || !ic-    843                         if (!xd || !ic || !ic->pino_nlink) {
847                                 dbg_xattr("xre    844                                 dbg_xattr("xref(ino=%u, xid=%u, xseqno=%u) is orphan.\n",
848                                           ref-    845                                           ref->ino, ref->xid, ref->xseqno);
849                                 ref->xseqno |=    846                                 ref->xseqno |= XREF_DELETE_MARKER;
850                                 ref->next = c-    847                                 ref->next = c->xref_dead_list;
851                                 c->xref_dead_l    848                                 c->xref_dead_list = ref;
852                                 xref_orphan_co    849                                 xref_orphan_count++;
853                                 continue;         850                                 continue;
854                         }                         851                         }
855                         ref->xd = xd;             852                         ref->xd = xd;
856                         ref->ic = ic;             853                         ref->ic = ic;
857                         atomic_inc(&xd->refcnt    854                         atomic_inc(&xd->refcnt);
858                         ref->next = ic->xref;     855                         ref->next = ic->xref;
859                         ic->xref = ref;           856                         ic->xref = ref;
860                 }                                 857                 }
861         }                                         858         }
862                                                   859 
863         /* Phase.3 : Link unchecked xdatum to     860         /* Phase.3 : Link unchecked xdatum to xattr_unchecked list */
864         for (i=0; i < XATTRINDEX_HASHSIZE; i++    861         for (i=0; i < XATTRINDEX_HASHSIZE; i++) {
865                 list_for_each_entry_safe(xd, _    862                 list_for_each_entry_safe(xd, _xd, &c->xattrindex[i], xindex) {
866                         xdatum_count++;           863                         xdatum_count++;
867                         list_del_init(&xd->xin    864                         list_del_init(&xd->xindex);
868                         if (!atomic_read(&xd->    865                         if (!atomic_read(&xd->refcnt)) {
869                                 dbg_xattr("xda    866                                 dbg_xattr("xdatum(xid=%u, version=%u) is orphan.\n",
870                                           xd->    867                                           xd->xid, xd->version);
871                                 xd->flags |= J    868                                 xd->flags |= JFFS2_XFLAGS_DEAD;
872                                 list_add(&xd->    869                                 list_add(&xd->xindex, &c->xattr_unchecked);
873                                 xdatum_orphan_    870                                 xdatum_orphan_count++;
874                                 continue;         871                                 continue;
875                         }                         872                         }
876                         if (is_xattr_datum_unc    873                         if (is_xattr_datum_unchecked(c, xd)) {
877                                 dbg_xattr("unc    874                                 dbg_xattr("unchecked xdatum(xid=%u, version=%u)\n",
878                                           xd->    875                                           xd->xid, xd->version);
879                                 list_add(&xd->    876                                 list_add(&xd->xindex, &c->xattr_unchecked);
880                                 xdatum_uncheck    877                                 xdatum_unchecked_count++;
881                         }                         878                         }
882                 }                                 879                 }
883         }                                         880         }
884         /* build complete */                      881         /* build complete */
885         JFFS2_NOTICE("complete building xattr     882         JFFS2_NOTICE("complete building xattr subsystem, %u of xdatum"
886                      " (%u unchecked, %u orpha    883                      " (%u unchecked, %u orphan) and "
887                      "%u of xref (%u dead, %u     884                      "%u of xref (%u dead, %u orphan) found.\n",
888                      xdatum_count, xdatum_unch    885                      xdatum_count, xdatum_unchecked_count, xdatum_orphan_count,
889                      xref_count, xref_dead_cou    886                      xref_count, xref_dead_count, xref_orphan_count);
890         kfree(xref_tmphash);                   << 
891         return 0;                              << 
892 }                                                 887 }
893                                                   888 
894 struct jffs2_xattr_datum *jffs2_setup_xattr_da    889 struct jffs2_xattr_datum *jffs2_setup_xattr_datum(struct jffs2_sb_info *c,
895                                                   890                                                   uint32_t xid, uint32_t version)
896 {                                                 891 {
897         struct jffs2_xattr_datum *xd;             892         struct jffs2_xattr_datum *xd;
898                                                   893 
899         xd = jffs2_find_xattr_datum(c, xid);      894         xd = jffs2_find_xattr_datum(c, xid);
900         if (!xd) {                                895         if (!xd) {
901                 xd = jffs2_alloc_xattr_datum()    896                 xd = jffs2_alloc_xattr_datum();
902                 if (!xd)                          897                 if (!xd)
903                         return ERR_PTR(-ENOMEM    898                         return ERR_PTR(-ENOMEM);
904                 xd->xid = xid;                    899                 xd->xid = xid;
905                 xd->version = version;            900                 xd->version = version;
906                 if (xd->xid > c->highest_xid)     901                 if (xd->xid > c->highest_xid)
907                         c->highest_xid = xd->x    902                         c->highest_xid = xd->xid;
908                 list_add_tail(&xd->xindex, &c-    903                 list_add_tail(&xd->xindex, &c->xattrindex[xid % XATTRINDEX_HASHSIZE]);
909         }                                         904         }
910         return xd;                                905         return xd;
911 }                                                 906 }
912                                                   907 
913 /* -------- xattr subsystem functions --------    908 /* -------- xattr subsystem functions ---------------
914  * xprefix_to_handler(xprefix)                    909  * xprefix_to_handler(xprefix)
915  *   is used to translate xprefix into xattr_h    910  *   is used to translate xprefix into xattr_handler.
916  * jffs2_listxattr(dentry, buffer, size)          911  * jffs2_listxattr(dentry, buffer, size)
917  *   is an implementation of listxattr handler    912  *   is an implementation of listxattr handler on jffs2.
918  * do_jffs2_getxattr(inode, xprefix, xname, bu    913  * do_jffs2_getxattr(inode, xprefix, xname, buffer, size)
919  *   is an implementation of getxattr handler     914  *   is an implementation of getxattr handler on jffs2.
920  * do_jffs2_setxattr(inode, xprefix, xname, bu    915  * do_jffs2_setxattr(inode, xprefix, xname, buffer, size, flags)
921  *   is an implementation of setxattr handler     916  *   is an implementation of setxattr handler on jffs2.
922  * -------------------------------------------    917  * -------------------------------------------------- */
923 const struct xattr_handler * const jffs2_xattr !! 918 const struct xattr_handler *jffs2_xattr_handlers[] = {
924         &jffs2_user_xattr_handler,                919         &jffs2_user_xattr_handler,
925 #ifdef CONFIG_JFFS2_FS_SECURITY                   920 #ifdef CONFIG_JFFS2_FS_SECURITY
926         &jffs2_security_xattr_handler,            921         &jffs2_security_xattr_handler,
927 #endif                                            922 #endif
                                                   >> 923 #ifdef CONFIG_JFFS2_FS_POSIX_ACL
                                                   >> 924         &posix_acl_access_xattr_handler,
                                                   >> 925         &posix_acl_default_xattr_handler,
                                                   >> 926 #endif
928         &jffs2_trusted_xattr_handler,             927         &jffs2_trusted_xattr_handler,
929         NULL                                      928         NULL
930 };                                                929 };
931                                                   930 
932 static const char *jffs2_xattr_prefix(int xpre !! 931 static const struct xattr_handler *xprefix_to_handler(int xprefix) {
933 {                                              !! 932         const struct xattr_handler *ret;
934         const struct xattr_handler *ret = NULL << 
935                                                   933 
936         switch (xprefix) {                        934         switch (xprefix) {
937         case JFFS2_XPREFIX_USER:                  935         case JFFS2_XPREFIX_USER:
938                 ret = &jffs2_user_xattr_handle    936                 ret = &jffs2_user_xattr_handler;
939                 break;                            937                 break;
940 #ifdef CONFIG_JFFS2_FS_SECURITY                   938 #ifdef CONFIG_JFFS2_FS_SECURITY
941         case JFFS2_XPREFIX_SECURITY:              939         case JFFS2_XPREFIX_SECURITY:
942                 ret = &jffs2_security_xattr_ha    940                 ret = &jffs2_security_xattr_handler;
943                 break;                            941                 break;
944 #endif                                            942 #endif
945 #ifdef CONFIG_JFFS2_FS_POSIX_ACL                  943 #ifdef CONFIG_JFFS2_FS_POSIX_ACL
946         case JFFS2_XPREFIX_ACL_ACCESS:            944         case JFFS2_XPREFIX_ACL_ACCESS:
947                 ret = &nop_posix_acl_access;   !! 945                 ret = &posix_acl_access_xattr_handler;
948                 break;                            946                 break;
949         case JFFS2_XPREFIX_ACL_DEFAULT:           947         case JFFS2_XPREFIX_ACL_DEFAULT:
950                 ret = &nop_posix_acl_default;  !! 948                 ret = &posix_acl_default_xattr_handler;
951                 break;                            949                 break;
952 #endif                                            950 #endif
953         case JFFS2_XPREFIX_TRUSTED:               951         case JFFS2_XPREFIX_TRUSTED:
954                 ret = &jffs2_trusted_xattr_han    952                 ret = &jffs2_trusted_xattr_handler;
955                 break;                            953                 break;
956         default:                                  954         default:
957                 return NULL;                   !! 955                 ret = NULL;
                                                   >> 956                 break;
958         }                                         957         }
959                                                !! 958         return ret;
960         if (!xattr_handler_can_list(ret, dentr << 
961                 return NULL;                   << 
962                                                << 
963         return xattr_prefix(ret);              << 
964 }                                                 959 }
965                                                   960 
966 ssize_t jffs2_listxattr(struct dentry *dentry,    961 ssize_t jffs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
967 {                                                 962 {
968         struct inode *inode = d_inode(dentry);    963         struct inode *inode = d_inode(dentry);
969         struct jffs2_inode_info *f = JFFS2_INO    964         struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
970         struct jffs2_sb_info *c = JFFS2_SB_INF    965         struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
971         struct jffs2_inode_cache *ic = f->inoc    966         struct jffs2_inode_cache *ic = f->inocache;
972         struct jffs2_xattr_ref *ref, **pref;      967         struct jffs2_xattr_ref *ref, **pref;
973         struct jffs2_xattr_datum *xd;             968         struct jffs2_xattr_datum *xd;
                                                   >> 969         const struct xattr_handler *xhandle;
974         const char *prefix;                       970         const char *prefix;
975         ssize_t prefix_len, len, rc;              971         ssize_t prefix_len, len, rc;
976         int retry = 0;                            972         int retry = 0;
977                                                   973 
978         rc = check_xattr_ref_inode(c, ic);        974         rc = check_xattr_ref_inode(c, ic);
979         if (unlikely(rc))                         975         if (unlikely(rc))
980                 return rc;                        976                 return rc;
981                                                   977 
982         down_read(&c->xattr_sem);                 978         down_read(&c->xattr_sem);
983  retry:                                           979  retry:
984         len = 0;                                  980         len = 0;
985         for (ref=ic->xref, pref=&ic->xref; ref    981         for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
986                 BUG_ON(ref->ic != ic);            982                 BUG_ON(ref->ic != ic);
987                 xd = ref->xd;                     983                 xd = ref->xd;
988                 if (!xd->xname) {                 984                 if (!xd->xname) {
989                         /* xdatum is unchached    985                         /* xdatum is unchached */
990                         if (!retry) {             986                         if (!retry) {
991                                 retry = 1;        987                                 retry = 1;
992                                 up_read(&c->xa    988                                 up_read(&c->xattr_sem);
993                                 down_write(&c-    989                                 down_write(&c->xattr_sem);
994                                 goto retry;       990                                 goto retry;
995                         } else {                  991                         } else {
996                                 rc = load_xatt    992                                 rc = load_xattr_datum(c, xd);
997                                 if (unlikely(r    993                                 if (unlikely(rc > 0)) {
998                                         *pref     994                                         *pref = ref->next;
999                                         delete    995                                         delete_xattr_ref(c, ref);
1000                                         goto     996                                         goto retry;
1001                                 } else if (un    997                                 } else if (unlikely(rc < 0))
1002                                         goto     998                                         goto out;
1003                         }                        999                         }
1004                 }                                1000                 }
1005                                               !! 1001                 xhandle = xprefix_to_handler(xd->xprefix);
1006                 prefix = jffs2_xattr_prefix(x !! 1002                 if (!xhandle || (xhandle->list && !xhandle->list(dentry)))
1007                 if (!prefix)                  << 
1008                         continue;                1003                         continue;
                                                   >> 1004                 prefix = xhandle->prefix ?: xhandle->name;
1009                 prefix_len = strlen(prefix);     1005                 prefix_len = strlen(prefix);
1010                 rc = prefix_len + xd->name_le    1006                 rc = prefix_len + xd->name_len + 1;
1011                                                  1007 
1012                 if (buffer) {                    1008                 if (buffer) {
1013                         if (rc > size - len)     1009                         if (rc > size - len) {
1014                                 rc = -ERANGE;    1010                                 rc = -ERANGE;
1015                                 goto out;        1011                                 goto out;
1016                         }                        1012                         }
1017                         memcpy(buffer, prefix    1013                         memcpy(buffer, prefix, prefix_len);
1018                         buffer += prefix_len;    1014                         buffer += prefix_len;
1019                         memcpy(buffer, xd->xn    1015                         memcpy(buffer, xd->xname, xd->name_len);
1020                         buffer += xd->name_le    1016                         buffer += xd->name_len;
1021                         *buffer++ = 0;           1017                         *buffer++ = 0;
1022                 }                                1018                 }
1023                 len += rc;                       1019                 len += rc;
1024         }                                        1020         }
1025         rc = len;                                1021         rc = len;
1026  out:                                            1022  out:
1027         if (!retry) {                            1023         if (!retry) {
1028                 up_read(&c->xattr_sem);          1024                 up_read(&c->xattr_sem);
1029         } else {                                 1025         } else {
1030                 up_write(&c->xattr_sem);         1026                 up_write(&c->xattr_sem);
1031         }                                        1027         }
1032         return rc;                               1028         return rc;
1033 }                                                1029 }
1034                                                  1030 
1035 int do_jffs2_getxattr(struct inode *inode, in    1031 int do_jffs2_getxattr(struct inode *inode, int xprefix, const char *xname,
1036                       char *buffer, size_t si    1032                       char *buffer, size_t size)
1037 {                                                1033 {
1038         struct jffs2_inode_info *f = JFFS2_IN    1034         struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
1039         struct jffs2_sb_info *c = JFFS2_SB_IN    1035         struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
1040         struct jffs2_inode_cache *ic = f->ino    1036         struct jffs2_inode_cache *ic = f->inocache;
1041         struct jffs2_xattr_datum *xd;            1037         struct jffs2_xattr_datum *xd;
1042         struct jffs2_xattr_ref *ref, **pref;     1038         struct jffs2_xattr_ref *ref, **pref;
1043         int rc, retry = 0;                       1039         int rc, retry = 0;
1044                                                  1040 
1045         rc = check_xattr_ref_inode(c, ic);       1041         rc = check_xattr_ref_inode(c, ic);
1046         if (unlikely(rc))                        1042         if (unlikely(rc))
1047                 return rc;                       1043                 return rc;
1048                                                  1044 
1049         down_read(&c->xattr_sem);                1045         down_read(&c->xattr_sem);
1050  retry:                                          1046  retry:
1051         for (ref=ic->xref, pref=&ic->xref; re    1047         for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
1052                 BUG_ON(ref->ic!=ic);             1048                 BUG_ON(ref->ic!=ic);
1053                                                  1049 
1054                 xd = ref->xd;                    1050                 xd = ref->xd;
1055                 if (xd->xprefix != xprefix)      1051                 if (xd->xprefix != xprefix)
1056                         continue;                1052                         continue;
1057                 if (!xd->xname) {                1053                 if (!xd->xname) {
1058                         /* xdatum is unchache    1054                         /* xdatum is unchached */
1059                         if (!retry) {            1055                         if (!retry) {
1060                                 retry = 1;       1056                                 retry = 1;
1061                                 up_read(&c->x    1057                                 up_read(&c->xattr_sem);
1062                                 down_write(&c    1058                                 down_write(&c->xattr_sem);
1063                                 goto retry;      1059                                 goto retry;
1064                         } else {                 1060                         } else {
1065                                 rc = load_xat    1061                                 rc = load_xattr_datum(c, xd);
1066                                 if (unlikely(    1062                                 if (unlikely(rc > 0)) {
1067                                         *pref    1063                                         *pref = ref->next;
1068                                         delet    1064                                         delete_xattr_ref(c, ref);
1069                                         goto     1065                                         goto retry;
1070                                 } else if (un    1066                                 } else if (unlikely(rc < 0)) {
1071                                         goto     1067                                         goto out;
1072                                 }                1068                                 }
1073                         }                        1069                         }
1074                 }                                1070                 }
1075                 if (!strcmp(xname, xd->xname)    1071                 if (!strcmp(xname, xd->xname)) {
1076                         rc = xd->value_len;      1072                         rc = xd->value_len;
1077                         if (buffer) {            1073                         if (buffer) {
1078                                 if (size < rc    1074                                 if (size < rc) {
1079                                         rc =     1075                                         rc = -ERANGE;
1080                                 } else {         1076                                 } else {
1081                                         memcp    1077                                         memcpy(buffer, xd->xvalue, rc);
1082                                 }                1078                                 }
1083                         }                        1079                         }
1084                         goto out;                1080                         goto out;
1085                 }                                1081                 }
1086         }                                        1082         }
1087         rc = -ENODATA;                           1083         rc = -ENODATA;
1088  out:                                            1084  out:
1089         if (!retry) {                            1085         if (!retry) {
1090                 up_read(&c->xattr_sem);          1086                 up_read(&c->xattr_sem);
1091         } else {                                 1087         } else {
1092                 up_write(&c->xattr_sem);         1088                 up_write(&c->xattr_sem);
1093         }                                        1089         }
1094         return rc;                               1090         return rc;
1095 }                                                1091 }
1096                                                  1092 
1097 int do_jffs2_setxattr(struct inode *inode, in    1093 int do_jffs2_setxattr(struct inode *inode, int xprefix, const char *xname,
1098                       const char *buffer, siz    1094                       const char *buffer, size_t size, int flags)
1099 {                                                1095 {
1100         struct jffs2_inode_info *f = JFFS2_IN    1096         struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
1101         struct jffs2_sb_info *c = JFFS2_SB_IN    1097         struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
1102         struct jffs2_inode_cache *ic = f->ino    1098         struct jffs2_inode_cache *ic = f->inocache;
1103         struct jffs2_xattr_datum *xd;            1099         struct jffs2_xattr_datum *xd;
1104         struct jffs2_xattr_ref *ref, *newref,    1100         struct jffs2_xattr_ref *ref, *newref, **pref;
1105         uint32_t length, request;                1101         uint32_t length, request;
1106         int rc;                                  1102         int rc;
1107                                                  1103 
1108         rc = check_xattr_ref_inode(c, ic);       1104         rc = check_xattr_ref_inode(c, ic);
1109         if (unlikely(rc))                        1105         if (unlikely(rc))
1110                 return rc;                       1106                 return rc;
1111                                                  1107 
1112         request = PAD(sizeof(struct jffs2_raw    1108         request = PAD(sizeof(struct jffs2_raw_xattr) + strlen(xname) + 1 + size);
1113         if (request > c->sector_size - c->cle << 
1114                 return -ERANGE;               << 
1115                                               << 
1116         rc = jffs2_reserve_space(c, request,     1109         rc = jffs2_reserve_space(c, request, &length,
1117                                  ALLOC_NORMAL    1110                                  ALLOC_NORMAL, JFFS2_SUMMARY_XATTR_SIZE);
1118         if (rc) {                                1111         if (rc) {
1119                 JFFS2_WARNING("jffs2_reserve_    1112                 JFFS2_WARNING("jffs2_reserve_space()=%d, request=%u\n", rc, request);
1120                 return rc;                       1113                 return rc;
1121         }                                        1114         }
1122                                                  1115 
1123         /* Find existing xattr */                1116         /* Find existing xattr */
1124         down_write(&c->xattr_sem);               1117         down_write(&c->xattr_sem);
1125  retry:                                          1118  retry:
1126         for (ref=ic->xref, pref=&ic->xref; re    1119         for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
1127                 xd = ref->xd;                    1120                 xd = ref->xd;
1128                 if (xd->xprefix != xprefix)      1121                 if (xd->xprefix != xprefix)
1129                         continue;                1122                         continue;
1130                 if (!xd->xname) {                1123                 if (!xd->xname) {
1131                         rc = load_xattr_datum    1124                         rc = load_xattr_datum(c, xd);
1132                         if (unlikely(rc > 0))    1125                         if (unlikely(rc > 0)) {
1133                                 *pref = ref->    1126                                 *pref = ref->next;
1134                                 delete_xattr_    1127                                 delete_xattr_ref(c, ref);
1135                                 goto retry;      1128                                 goto retry;
1136                         } else if (unlikely(r    1129                         } else if (unlikely(rc < 0))
1137                                 goto out;        1130                                 goto out;
1138                 }                                1131                 }
1139                 if (!strcmp(xd->xname, xname)    1132                 if (!strcmp(xd->xname, xname)) {
1140                         if (flags & XATTR_CRE    1133                         if (flags & XATTR_CREATE) {
1141                                 rc = -EEXIST;    1134                                 rc = -EEXIST;
1142                                 goto out;        1135                                 goto out;
1143                         }                        1136                         }
1144                         if (!buffer) {           1137                         if (!buffer) {
1145                                 ref->ino = ic    1138                                 ref->ino = ic->ino;
1146                                 ref->xid = xd    1139                                 ref->xid = xd->xid;
1147                                 ref->xseqno |    1140                                 ref->xseqno |= XREF_DELETE_MARKER;
1148                                 rc = save_xat    1141                                 rc = save_xattr_ref(c, ref);
1149                                 if (!rc) {       1142                                 if (!rc) {
1150                                         *pref    1143                                         *pref = ref->next;
1151                                         spin_    1144                                         spin_lock(&c->erase_completion_lock);
1152                                         ref->    1145                                         ref->next = c->xref_dead_list;
1153                                         c->xr    1146                                         c->xref_dead_list = ref;
1154                                         spin_    1147                                         spin_unlock(&c->erase_completion_lock);
1155                                         unref    1148                                         unrefer_xattr_datum(c, xd);
1156                                 } else {         1149                                 } else {
1157                                         ref->    1150                                         ref->ic = ic;
1158                                         ref->    1151                                         ref->xd = xd;
1159                                         ref->    1152                                         ref->xseqno &= ~XREF_DELETE_MARKER;
1160                                 }                1153                                 }
1161                                 goto out;        1154                                 goto out;
1162                         }                        1155                         }
1163                         goto found;              1156                         goto found;
1164                 }                                1157                 }
1165         }                                        1158         }
1166         /* not found */                          1159         /* not found */
1167         if (flags & XATTR_REPLACE) {             1160         if (flags & XATTR_REPLACE) {
1168                 rc = -ENODATA;                   1161                 rc = -ENODATA;
1169                 goto out;                        1162                 goto out;
1170         }                                        1163         }
1171         if (!buffer) {                           1164         if (!buffer) {
1172                 rc = -ENODATA;                   1165                 rc = -ENODATA;
1173                 goto out;                        1166                 goto out;
1174         }                                        1167         }
1175  found:                                          1168  found:
1176         xd = create_xattr_datum(c, xprefix, x    1169         xd = create_xattr_datum(c, xprefix, xname, buffer, size);
1177         if (IS_ERR(xd)) {                        1170         if (IS_ERR(xd)) {
1178                 rc = PTR_ERR(xd);                1171                 rc = PTR_ERR(xd);
1179                 goto out;                        1172                 goto out;
1180         }                                        1173         }
1181         up_write(&c->xattr_sem);                 1174         up_write(&c->xattr_sem);
1182         jffs2_complete_reservation(c);           1175         jffs2_complete_reservation(c);
1183                                                  1176 
1184         /* create xattr_ref */                   1177         /* create xattr_ref */
1185         request = PAD(sizeof(struct jffs2_raw    1178         request = PAD(sizeof(struct jffs2_raw_xref));
1186         rc = jffs2_reserve_space(c, request,     1179         rc = jffs2_reserve_space(c, request, &length,
1187                                  ALLOC_NORMAL    1180                                  ALLOC_NORMAL, JFFS2_SUMMARY_XREF_SIZE);
1188         down_write(&c->xattr_sem);               1181         down_write(&c->xattr_sem);
1189         if (rc) {                                1182         if (rc) {
1190                 JFFS2_WARNING("jffs2_reserve_    1183                 JFFS2_WARNING("jffs2_reserve_space()=%d, request=%u\n", rc, request);
1191                 unrefer_xattr_datum(c, xd);      1184                 unrefer_xattr_datum(c, xd);
1192                 up_write(&c->xattr_sem);         1185                 up_write(&c->xattr_sem);
1193                 return rc;                       1186                 return rc;
1194         }                                        1187         }
1195         if (ref)                                 1188         if (ref)
1196                 *pref = ref->next;               1189                 *pref = ref->next;
1197         newref = create_xattr_ref(c, ic, xd);    1190         newref = create_xattr_ref(c, ic, xd);
1198         if (IS_ERR(newref)) {                    1191         if (IS_ERR(newref)) {
1199                 if (ref) {                       1192                 if (ref) {
1200                         ref->next = ic->xref;    1193                         ref->next = ic->xref;
1201                         ic->xref = ref;          1194                         ic->xref = ref;
1202                 }                                1195                 }
1203                 rc = PTR_ERR(newref);            1196                 rc = PTR_ERR(newref);
1204                 unrefer_xattr_datum(c, xd);      1197                 unrefer_xattr_datum(c, xd);
1205         } else if (ref) {                        1198         } else if (ref) {
1206                 delete_xattr_ref(c, ref);        1199                 delete_xattr_ref(c, ref);
1207         }                                        1200         }
1208  out:                                            1201  out:
1209         up_write(&c->xattr_sem);                 1202         up_write(&c->xattr_sem);
1210         jffs2_complete_reservation(c);           1203         jffs2_complete_reservation(c);
1211         return rc;                               1204         return rc;
1212 }                                                1205 }
1213                                                  1206 
1214 /* -------- garbage collector functions -----    1207 /* -------- garbage collector functions -------------
1215  * jffs2_garbage_collect_xattr_datum(c, xd, r    1208  * jffs2_garbage_collect_xattr_datum(c, xd, raw)
1216  *   is used to move xdatum into new node.       1209  *   is used to move xdatum into new node.
1217  * jffs2_garbage_collect_xattr_ref(c, ref, ra    1210  * jffs2_garbage_collect_xattr_ref(c, ref, raw)
1218  *   is used to move xref into new node.         1211  *   is used to move xref into new node.
1219  * jffs2_verify_xattr(c)                         1212  * jffs2_verify_xattr(c)
1220  *   is used to call do_verify_xattr_datum()     1213  *   is used to call do_verify_xattr_datum() before garbage collecting.
1221  * jffs2_release_xattr_datum(c, xd)              1214  * jffs2_release_xattr_datum(c, xd)
1222  *   is used to release an in-memory object o    1215  *   is used to release an in-memory object of xdatum.
1223  * jffs2_release_xattr_ref(c, ref)               1216  * jffs2_release_xattr_ref(c, ref)
1224  *   is used to release an in-memory object o    1217  *   is used to release an in-memory object of xref.
1225  * ------------------------------------------    1218  * -------------------------------------------------- */
1226 int jffs2_garbage_collect_xattr_datum(struct     1219 int jffs2_garbage_collect_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd,
1227                                       struct     1220                                       struct jffs2_raw_node_ref *raw)
1228 {                                                1221 {
1229         uint32_t totlen, length, old_ofs;        1222         uint32_t totlen, length, old_ofs;
1230         int rc = 0;                              1223         int rc = 0;
1231                                                  1224 
1232         down_write(&c->xattr_sem);               1225         down_write(&c->xattr_sem);
1233         if (xd->node != raw)                     1226         if (xd->node != raw)
1234                 goto out;                        1227                 goto out;
1235         if (xd->flags & (JFFS2_XFLAGS_DEAD|JF    1228         if (xd->flags & (JFFS2_XFLAGS_DEAD|JFFS2_XFLAGS_INVALID))
1236                 goto out;                        1229                 goto out;
1237                                                  1230 
1238         rc = load_xattr_datum(c, xd);            1231         rc = load_xattr_datum(c, xd);
1239         if (unlikely(rc)) {                      1232         if (unlikely(rc)) {
1240                 rc = (rc > 0) ? 0 : rc;          1233                 rc = (rc > 0) ? 0 : rc;
1241                 goto out;                        1234                 goto out;
1242         }                                        1235         }
1243         old_ofs = ref_offset(xd->node);          1236         old_ofs = ref_offset(xd->node);
1244         totlen = PAD(sizeof(struct jffs2_raw_    1237         totlen = PAD(sizeof(struct jffs2_raw_xattr)
1245                         + xd->name_len + 1 +     1238                         + xd->name_len + 1 + xd->value_len);
1246         rc = jffs2_reserve_space_gc(c, totlen    1239         rc = jffs2_reserve_space_gc(c, totlen, &length, JFFS2_SUMMARY_XATTR_SIZE);
1247         if (rc) {                                1240         if (rc) {
1248                 JFFS2_WARNING("jffs2_reserve_    1241                 JFFS2_WARNING("jffs2_reserve_space_gc()=%d, request=%u\n", rc, totlen);
1249                 goto out;                        1242                 goto out;
1250         }                                        1243         }
1251         rc = save_xattr_datum(c, xd);            1244         rc = save_xattr_datum(c, xd);
1252         if (!rc)                                 1245         if (!rc)
1253                 dbg_xattr("xdatum (xid=%u, ve    1246                 dbg_xattr("xdatum (xid=%u, version=%u) GC'ed from %#08x to %08x\n",
1254                           xd->xid, xd->versio    1247                           xd->xid, xd->version, old_ofs, ref_offset(xd->node));
1255  out:                                            1248  out:
1256         if (!rc)                                 1249         if (!rc)
1257                 jffs2_mark_node_obsolete(c, r    1250                 jffs2_mark_node_obsolete(c, raw);
1258         up_write(&c->xattr_sem);                 1251         up_write(&c->xattr_sem);
1259         return rc;                               1252         return rc;
1260 }                                                1253 }
1261                                                  1254 
1262 int jffs2_garbage_collect_xattr_ref(struct jf    1255 int jffs2_garbage_collect_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref,
1263                                     struct jf    1256                                     struct jffs2_raw_node_ref *raw)
1264 {                                                1257 {
1265         uint32_t totlen, length, old_ofs;        1258         uint32_t totlen, length, old_ofs;
1266         int rc = 0;                              1259         int rc = 0;
1267                                                  1260 
1268         down_write(&c->xattr_sem);               1261         down_write(&c->xattr_sem);
1269         BUG_ON(!ref->node);                      1262         BUG_ON(!ref->node);
1270                                                  1263 
1271         if (ref->node != raw)                    1264         if (ref->node != raw)
1272                 goto out;                        1265                 goto out;
1273         if (is_xattr_ref_dead(ref) && (raw->n    1266         if (is_xattr_ref_dead(ref) && (raw->next_in_ino == (void *)ref))
1274                 goto out;                        1267                 goto out;
1275                                                  1268 
1276         old_ofs = ref_offset(ref->node);         1269         old_ofs = ref_offset(ref->node);
1277         totlen = ref_totlen(c, c->gcblock, re    1270         totlen = ref_totlen(c, c->gcblock, ref->node);
1278                                                  1271 
1279         rc = jffs2_reserve_space_gc(c, totlen    1272         rc = jffs2_reserve_space_gc(c, totlen, &length, JFFS2_SUMMARY_XREF_SIZE);
1280         if (rc) {                                1273         if (rc) {
1281                 JFFS2_WARNING("%s: jffs2_rese    1274                 JFFS2_WARNING("%s: jffs2_reserve_space_gc() = %d, request = %u\n",
1282                               __func__, rc, t    1275                               __func__, rc, totlen);
1283                 goto out;                        1276                 goto out;
1284         }                                        1277         }
1285         rc = save_xattr_ref(c, ref);             1278         rc = save_xattr_ref(c, ref);
1286         if (!rc)                                 1279         if (!rc)
1287                 dbg_xattr("xref (ino=%u, xid=    1280                 dbg_xattr("xref (ino=%u, xid=%u) GC'ed from %#08x to %08x\n",
1288                           ref->ic->ino, ref->    1281                           ref->ic->ino, ref->xd->xid, old_ofs, ref_offset(ref->node));
1289  out:                                            1282  out:
1290         if (!rc)                                 1283         if (!rc)
1291                 jffs2_mark_node_obsolete(c, r    1284                 jffs2_mark_node_obsolete(c, raw);
1292         up_write(&c->xattr_sem);                 1285         up_write(&c->xattr_sem);
1293         return rc;                               1286         return rc;
1294 }                                                1287 }
1295                                                  1288 
1296 int jffs2_verify_xattr(struct jffs2_sb_info *    1289 int jffs2_verify_xattr(struct jffs2_sb_info *c)
1297 {                                                1290 {
1298         struct jffs2_xattr_datum *xd, *_xd;      1291         struct jffs2_xattr_datum *xd, *_xd;
1299         struct jffs2_eraseblock *jeb;            1292         struct jffs2_eraseblock *jeb;
1300         struct jffs2_raw_node_ref *raw;          1293         struct jffs2_raw_node_ref *raw;
1301         uint32_t totlen;                         1294         uint32_t totlen;
1302         int rc;                                  1295         int rc;
1303                                                  1296 
1304         down_write(&c->xattr_sem);               1297         down_write(&c->xattr_sem);
1305         list_for_each_entry_safe(xd, _xd, &c-    1298         list_for_each_entry_safe(xd, _xd, &c->xattr_unchecked, xindex) {
1306                 rc = do_verify_xattr_datum(c,    1299                 rc = do_verify_xattr_datum(c, xd);
1307                 if (rc < 0)                      1300                 if (rc < 0)
1308                         continue;                1301                         continue;
1309                 list_del_init(&xd->xindex);      1302                 list_del_init(&xd->xindex);
1310                 spin_lock(&c->erase_completio    1303                 spin_lock(&c->erase_completion_lock);
1311                 for (raw=xd->node; raw != (vo    1304                 for (raw=xd->node; raw != (void *)xd; raw=raw->next_in_ino) {
1312                         if (ref_flags(raw) !=    1305                         if (ref_flags(raw) != REF_UNCHECKED)
1313                                 continue;        1306                                 continue;
1314                         jeb = &c->blocks[ref_    1307                         jeb = &c->blocks[ref_offset(raw) / c->sector_size];
1315                         totlen = PAD(ref_totl    1308                         totlen = PAD(ref_totlen(c, jeb, raw));
1316                         c->unchecked_size -=     1309                         c->unchecked_size -= totlen; c->used_size += totlen;
1317                         jeb->unchecked_size -    1310                         jeb->unchecked_size -= totlen; jeb->used_size += totlen;
1318                         raw->flash_offset = r    1311                         raw->flash_offset = ref_offset(raw)
1319                                 | ((xd->node     1312                                 | ((xd->node == (void *)raw) ? REF_PRISTINE : REF_NORMAL);
1320                 }                                1313                 }
1321                 if (xd->flags & JFFS2_XFLAGS_    1314                 if (xd->flags & JFFS2_XFLAGS_DEAD)
1322                         list_add(&xd->xindex,    1315                         list_add(&xd->xindex, &c->xattr_dead_list);
1323                 spin_unlock(&c->erase_complet    1316                 spin_unlock(&c->erase_completion_lock);
1324         }                                        1317         }
1325         up_write(&c->xattr_sem);                 1318         up_write(&c->xattr_sem);
1326         return list_empty(&c->xattr_unchecked    1319         return list_empty(&c->xattr_unchecked) ? 1 : 0;
1327 }                                                1320 }
1328                                                  1321 
1329 void jffs2_release_xattr_datum(struct jffs2_s    1322 void jffs2_release_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
1330 {                                                1323 {
1331         /* must be called under spin_lock(&c-    1324         /* must be called under spin_lock(&c->erase_completion_lock) */
1332         if (atomic_read(&xd->refcnt) || xd->n    1325         if (atomic_read(&xd->refcnt) || xd->node != (void *)xd)
1333                 return;                          1326                 return;
1334                                                  1327 
1335         list_del(&xd->xindex);                   1328         list_del(&xd->xindex);
1336         jffs2_free_xattr_datum(xd);              1329         jffs2_free_xattr_datum(xd);
1337 }                                                1330 }
1338                                                  1331 
1339 void jffs2_release_xattr_ref(struct jffs2_sb_    1332 void jffs2_release_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
1340 {                                                1333 {
1341         /* must be called under spin_lock(&c-    1334         /* must be called under spin_lock(&c->erase_completion_lock) */
1342         struct jffs2_xattr_ref *tmp, **ptmp;     1335         struct jffs2_xattr_ref *tmp, **ptmp;
1343                                                  1336 
1344         if (ref->node != (void *)ref)            1337         if (ref->node != (void *)ref)
1345                 return;                          1338                 return;
1346                                                  1339 
1347         for (tmp=c->xref_dead_list, ptmp=&c->    1340         for (tmp=c->xref_dead_list, ptmp=&c->xref_dead_list; tmp; ptmp=&tmp->next, tmp=tmp->next) {
1348                 if (ref == tmp) {                1341                 if (ref == tmp) {
1349                         *ptmp = tmp->next;       1342                         *ptmp = tmp->next;
1350                         break;                   1343                         break;
1351                 }                                1344                 }
1352         }                                        1345         }
1353         jffs2_free_xattr_ref(ref);               1346         jffs2_free_xattr_ref(ref);
1354 }                                                1347 }
1355                                                  1348 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php