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