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