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