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; |
David Woodhouse | 89291a9 | 2006-05-25 13:30:24 +0100 | [diff] [blame] | 114 | size_t length; |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 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)) { |
David Woodhouse | 89291a9 | 2006-05-25 13:30:24 +0100 | [diff] [blame] | 127 | JFFS2_ERROR("jffs2_flash_read()=%d, req=%zu, read=%zu at %#08x\n", |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 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)) { |
David Woodhouse | 89291a9 | 2006-05-25 13:30:24 +0100 | [diff] [blame] | 134 | JFFS2_ERROR("jffs2_flash_write()=%d, req=%zu, wrote=%zu ar %#08x\n", |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 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)) { |
David Woodhouse | 89291a9 | 2006-05-25 13:30:24 +0100 | [diff] [blame] | 172 | JFFS2_WARNING("jffs2_flash_read()=%d, req=%zu, read=%zu at %#08x\n", |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 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) { |
David Woodhouse | 89291a9 | 2006-05-25 13:30:24 +0100 | [diff] [blame] | 243 | JFFS2_WARNING("jffs2_flash_read() returned %d, request=%d, readlen=%zu, at %#08x\n", |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 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) */ |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 307 | struct jffs2_raw_node_ref *raw; |
David Woodhouse | 2f78540 | 2006-05-24 02:04:45 +0100 | [diff] [blame] | 308 | struct jffs2_raw_xattr rx; |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 309 | struct kvec vecs[2]; |
David Woodhouse | 89291a9 | 2006-05-25 13:30:24 +0100 | [diff] [blame] | 310 | size_t length; |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 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 | |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 322 | /* Setup raw-xattr */ |
| 323 | rx.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK); |
| 324 | rx.nodetype = cpu_to_je16(JFFS2_NODETYPE_XATTR); |
| 325 | rx.totlen = cpu_to_je32(PAD(totlen)); |
| 326 | rx.hdr_crc = cpu_to_je32(crc32(0, &rx, sizeof(struct jffs2_unknown_node) - 4)); |
| 327 | |
| 328 | rx.xid = cpu_to_je32(xd->xid); |
| 329 | rx.version = cpu_to_je32(++xd->version); |
| 330 | rx.xprefix = xd->xprefix; |
| 331 | rx.name_len = xd->name_len; |
| 332 | rx.value_len = cpu_to_je16(xd->value_len); |
| 333 | rx.data_crc = cpu_to_je32(crc32(0, vecs[1].iov_base, vecs[1].iov_len)); |
| 334 | rx.node_crc = cpu_to_je32(crc32(0, &rx, sizeof(struct jffs2_raw_xattr) - 4)); |
| 335 | |
| 336 | rc = jffs2_flash_writev(c, vecs, 2, phys_ofs, &length, 0); |
| 337 | if (rc || totlen != length) { |
David Woodhouse | 89291a9 | 2006-05-25 13:30:24 +0100 | [diff] [blame] | 338 | JFFS2_WARNING("jffs2_flash_writev()=%d, req=%u, wrote=%zu, at %#08x\n", |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 339 | rc, totlen, length, phys_ofs); |
| 340 | rc = rc ? rc : -EIO; |
David Woodhouse | 2f78540 | 2006-05-24 02:04:45 +0100 | [diff] [blame] | 341 | if (length) |
| 342 | jffs2_add_physical_node_ref(c, phys_ofs | REF_OBSOLETE, PAD(totlen), NULL); |
| 343 | |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 344 | return rc; |
| 345 | } |
David Woodhouse | b64335f | 2006-05-21 04:36:45 +0100 | [diff] [blame] | 346 | |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 347 | /* success */ |
David Woodhouse | 2f78540 | 2006-05-24 02:04:45 +0100 | [diff] [blame] | 348 | raw = jffs2_add_physical_node_ref(c, phys_ofs | REF_PRISTINE, PAD(totlen), NULL); |
David Woodhouse | fcb7578 | 2006-05-22 15:23:10 +0100 | [diff] [blame] | 349 | /* FIXME */ raw->next_in_ino = (void *)xd; |
| 350 | |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 351 | if (xd->node) |
| 352 | delete_xattr_datum_node(c, xd); |
| 353 | xd->node = raw; |
| 354 | |
| 355 | dbg_xattr("success on saving xdatum (xid=%u, version=%u, xprefix=%u, xname='%s')\n", |
| 356 | xd->xid, xd->version, xd->xprefix, xd->xname); |
| 357 | |
| 358 | return 0; |
| 359 | } |
| 360 | |
| 361 | static struct jffs2_xattr_datum *create_xattr_datum(struct jffs2_sb_info *c, |
| 362 | int xprefix, const char *xname, |
David Woodhouse | 9fe4854 | 2006-05-23 00:38:06 +0100 | [diff] [blame] | 363 | const char *xvalue, int xsize) |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 364 | { |
| 365 | /* must be called under down_write(xattr_sem) */ |
| 366 | struct jffs2_xattr_datum *xd; |
| 367 | uint32_t hashkey, name_len; |
| 368 | char *data; |
| 369 | int i, rc; |
| 370 | |
| 371 | /* Search xattr_datum has same xname/xvalue by index */ |
| 372 | hashkey = xattr_datum_hashkey(xprefix, xname, xvalue, xsize); |
| 373 | i = hashkey % XATTRINDEX_HASHSIZE; |
| 374 | list_for_each_entry(xd, &c->xattrindex[i], xindex) { |
| 375 | if (xd->hashkey==hashkey |
| 376 | && xd->xprefix==xprefix |
| 377 | && xd->value_len==xsize |
| 378 | && !strcmp(xd->xname, xname) |
| 379 | && !memcmp(xd->xvalue, xvalue, xsize)) { |
| 380 | xd->refcnt++; |
| 381 | return xd; |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | /* Not found, Create NEW XATTR-Cache */ |
| 386 | name_len = strlen(xname); |
| 387 | |
| 388 | xd = jffs2_alloc_xattr_datum(); |
| 389 | if (!xd) |
| 390 | return ERR_PTR(-ENOMEM); |
| 391 | |
| 392 | data = kmalloc(name_len + 1 + xsize, GFP_KERNEL); |
| 393 | if (!data) { |
| 394 | jffs2_free_xattr_datum(xd); |
| 395 | return ERR_PTR(-ENOMEM); |
| 396 | } |
| 397 | strcpy(data, xname); |
| 398 | memcpy(data + name_len + 1, xvalue, xsize); |
| 399 | |
| 400 | xd->refcnt = 1; |
| 401 | xd->xid = ++c->highest_xid; |
| 402 | xd->flags |= JFFS2_XFLAGS_HOT; |
| 403 | xd->xprefix = xprefix; |
| 404 | |
| 405 | xd->hashkey = hashkey; |
| 406 | xd->xname = data; |
| 407 | xd->xvalue = data + name_len + 1; |
| 408 | xd->name_len = name_len; |
| 409 | xd->value_len = xsize; |
| 410 | xd->data_crc = crc32(0, data, xd->name_len + 1 + xd->value_len); |
| 411 | |
David Woodhouse | 9fe4854 | 2006-05-23 00:38:06 +0100 | [diff] [blame] | 412 | rc = save_xattr_datum(c, xd); |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 413 | if (rc) { |
| 414 | kfree(xd->xname); |
| 415 | jffs2_free_xattr_datum(xd); |
| 416 | return ERR_PTR(rc); |
| 417 | } |
| 418 | |
| 419 | /* Insert Hash Index */ |
| 420 | i = hashkey % XATTRINDEX_HASHSIZE; |
| 421 | list_add(&xd->xindex, &c->xattrindex[i]); |
| 422 | |
| 423 | c->xdatum_mem_usage += (xd->name_len + 1 + xd->value_len); |
| 424 | reclaim_xattr_datum(c); |
| 425 | |
| 426 | return xd; |
| 427 | } |
| 428 | |
KaiGai Kohei | 21b9879 | 2006-05-13 15:22:29 +0900 | [diff] [blame] | 429 | /* -------- xref related functions ------------------ |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 430 | * verify_xattr_ref(c, ref) |
| 431 | * is used to load xref information from medium. Because summary data does not |
| 432 | * contain xid/ino, it's necessary to verify once while mounting process. |
| 433 | * delete_xattr_ref_node(c, ref) |
| 434 | * is used to delete a jffs2 node is dominated by xref. When EBS is enabled, |
| 435 | * it overwrites the obsolete node by myself. |
| 436 | * delete_xattr_ref(c, ref) |
| 437 | * is used to delete jffs2_xattr_ref object. If the reference counter of xdatum |
| 438 | * 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] | 439 | * save_xattr_ref(c, ref) |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 440 | * is used to write xref to medium. |
David Woodhouse | 9fe4854 | 2006-05-23 00:38:06 +0100 | [diff] [blame] | 441 | * create_xattr_ref(c, ic, xd) |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 442 | * is used to create a new xref and write to medium. |
| 443 | * jffs2_xattr_delete_inode(c, ic) |
| 444 | * is called to remove xrefs related to obsolete inode when inode is unlinked. |
| 445 | * jffs2_xattr_free_inode(c, ic) |
| 446 | * is called to release xattr related objects when unmounting. |
KaiGai Kohei | 8f2b6f4 | 2006-05-13 15:15:07 +0900 | [diff] [blame] | 447 | * check_xattr_ref_inode(c, ic) |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 448 | * is used to confirm inode does not have duplicate xattr name/value pair. |
| 449 | * -------------------------------------------------- */ |
| 450 | static int verify_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref) |
| 451 | { |
| 452 | struct jffs2_eraseblock *jeb; |
| 453 | struct jffs2_raw_xref rr; |
| 454 | size_t readlen; |
| 455 | uint32_t crc, totlen; |
| 456 | int rc; |
| 457 | |
| 458 | BUG_ON(ref_flags(ref->node) != REF_UNCHECKED); |
| 459 | |
| 460 | rc = jffs2_flash_read(c, ref_offset(ref->node), sizeof(rr), &readlen, (char *)&rr); |
| 461 | if (rc || sizeof(rr) != readlen) { |
David Woodhouse | 89291a9 | 2006-05-25 13:30:24 +0100 | [diff] [blame] | 462 | JFFS2_WARNING("jffs2_flash_read()=%d, req=%zu, read=%zu, at %#08x\n", |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 463 | rc, sizeof(rr), readlen, ref_offset(ref->node)); |
| 464 | return rc ? rc : -EIO; |
| 465 | } |
| 466 | /* obsolete node */ |
| 467 | crc = crc32(0, &rr, sizeof(rr) - 4); |
| 468 | if (crc != je32_to_cpu(rr.node_crc)) { |
| 469 | if (je32_to_cpu(rr.node_crc) != 0xffffffff) |
| 470 | JFFS2_ERROR("node CRC failed at %#08x, read=%#08x, calc=%#08x\n", |
| 471 | ref_offset(ref->node), je32_to_cpu(rr.node_crc), crc); |
| 472 | return EIO; |
| 473 | } |
| 474 | if (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 | JFFS2_ERROR("inconsistent xref at %#08x, magic=%#04x/%#04x, " |
David Woodhouse | 89291a9 | 2006-05-25 13:30:24 +0100 | [diff] [blame] | 478 | "nodetype=%#04x/%#04x, totlen=%u/%zu\n", |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 479 | ref_offset(ref->node), je16_to_cpu(rr.magic), JFFS2_MAGIC_BITMASK, |
| 480 | je16_to_cpu(rr.nodetype), JFFS2_NODETYPE_XREF, |
| 481 | je32_to_cpu(rr.totlen), PAD(sizeof(rr))); |
| 482 | return EIO; |
| 483 | } |
| 484 | ref->ino = je32_to_cpu(rr.ino); |
| 485 | ref->xid = je32_to_cpu(rr.xid); |
| 486 | |
| 487 | /* fixup superblock/eraseblock info */ |
| 488 | jeb = &c->blocks[ref_offset(ref->node) / c->sector_size]; |
| 489 | totlen = PAD(sizeof(rr)); |
| 490 | |
| 491 | spin_lock(&c->erase_completion_lock); |
| 492 | c->unchecked_size -= totlen; c->used_size += totlen; |
| 493 | jeb->unchecked_size -= totlen; jeb->used_size += totlen; |
| 494 | ref->node->flash_offset = ref_offset(ref->node) | REF_PRISTINE; |
| 495 | spin_unlock(&c->erase_completion_lock); |
| 496 | |
| 497 | dbg_xattr("success on verifying xref (ino=%u, xid=%u) at %#08x\n", |
| 498 | ref->ino, ref->xid, ref_offset(ref->node)); |
| 499 | return 0; |
| 500 | } |
| 501 | |
| 502 | static void delete_xattr_ref_node(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref) |
| 503 | { |
| 504 | struct jffs2_raw_xref rr; |
David Woodhouse | 89291a9 | 2006-05-25 13:30:24 +0100 | [diff] [blame] | 505 | size_t length; |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 506 | int rc; |
| 507 | |
| 508 | if (jffs2_sum_active()) { |
| 509 | memset(&rr, 0xff, sizeof(rr)); |
| 510 | rc = jffs2_flash_read(c, ref_offset(ref->node), |
| 511 | sizeof(struct jffs2_unknown_node), |
| 512 | &length, (char *)&rr); |
| 513 | if (rc || length != sizeof(struct jffs2_unknown_node)) { |
David Woodhouse | 89291a9 | 2006-05-25 13:30:24 +0100 | [diff] [blame] | 514 | JFFS2_ERROR("jffs2_flash_read()=%d, req=%zu, read=%zu at %#08x\n", |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 515 | rc, sizeof(struct jffs2_unknown_node), |
| 516 | length, ref_offset(ref->node)); |
| 517 | } |
| 518 | rc = jffs2_flash_write(c, ref_offset(ref->node), sizeof(rr), |
| 519 | &length, (char *)&rr); |
| 520 | if (rc || length != sizeof(struct jffs2_raw_xref)) { |
David Woodhouse | 89291a9 | 2006-05-25 13:30:24 +0100 | [diff] [blame] | 521 | JFFS2_ERROR("jffs2_flash_write()=%d, req=%zu, wrote=%zu at %#08x\n", |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 522 | rc, sizeof(rr), length, ref_offset(ref->node)); |
| 523 | } |
| 524 | } |
| 525 | spin_lock(&c->erase_completion_lock); |
| 526 | ref->node->next_in_ino = NULL; |
| 527 | spin_unlock(&c->erase_completion_lock); |
| 528 | jffs2_mark_node_obsolete(c, ref->node); |
| 529 | ref->node = NULL; |
| 530 | } |
| 531 | |
| 532 | static void delete_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref) |
| 533 | { |
| 534 | /* must be called under down_write(xattr_sem) */ |
| 535 | struct jffs2_xattr_datum *xd; |
| 536 | |
| 537 | BUG_ON(!ref->node); |
| 538 | delete_xattr_ref_node(c, ref); |
| 539 | |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 540 | xd = ref->xd; |
| 541 | xd->refcnt--; |
| 542 | if (!xd->refcnt) |
| 543 | delete_xattr_datum(c, xd); |
| 544 | jffs2_free_xattr_ref(ref); |
| 545 | } |
| 546 | |
David Woodhouse | 9fe4854 | 2006-05-23 00:38:06 +0100 | [diff] [blame] | 547 | 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] | 548 | { |
| 549 | /* must be called under down_write(xattr_sem) */ |
| 550 | struct jffs2_raw_node_ref *raw; |
| 551 | struct jffs2_raw_xref rr; |
David Woodhouse | 89291a9 | 2006-05-25 13:30:24 +0100 | [diff] [blame] | 552 | size_t length; |
David Woodhouse | 9fe4854 | 2006-05-23 00:38:06 +0100 | [diff] [blame] | 553 | uint32_t phys_ofs = write_ofs(c); |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 554 | int ret; |
| 555 | |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 556 | rr.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK); |
| 557 | rr.nodetype = cpu_to_je16(JFFS2_NODETYPE_XREF); |
| 558 | rr.totlen = cpu_to_je32(PAD(sizeof(rr))); |
| 559 | rr.hdr_crc = cpu_to_je32(crc32(0, &rr, sizeof(struct jffs2_unknown_node) - 4)); |
| 560 | |
| 561 | rr.ino = cpu_to_je32(ref->ic->ino); |
| 562 | rr.xid = cpu_to_je32(ref->xd->xid); |
| 563 | rr.node_crc = cpu_to_je32(crc32(0, &rr, sizeof(rr) - 4)); |
| 564 | |
| 565 | ret = jffs2_flash_write(c, phys_ofs, sizeof(rr), &length, (char *)&rr); |
| 566 | if (ret || sizeof(rr) != length) { |
David Woodhouse | 89291a9 | 2006-05-25 13:30:24 +0100 | [diff] [blame] | 567 | JFFS2_WARNING("jffs2_flash_write() returned %d, request=%zu, retlen=%zu, at %#08x\n", |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 568 | ret, sizeof(rr), length, phys_ofs); |
| 569 | ret = ret ? ret : -EIO; |
David Woodhouse | 2f78540 | 2006-05-24 02:04:45 +0100 | [diff] [blame] | 570 | if (length) |
| 571 | jffs2_add_physical_node_ref(c, phys_ofs | REF_OBSOLETE, PAD(sizeof(rr)), NULL); |
| 572 | |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 573 | return ret; |
| 574 | } |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 575 | |
David Woodhouse | 2f78540 | 2006-05-24 02:04:45 +0100 | [diff] [blame] | 576 | raw = jffs2_add_physical_node_ref(c, phys_ofs | REF_PRISTINE, PAD(sizeof(rr)), NULL); |
David Woodhouse | fcb7578 | 2006-05-22 15:23:10 +0100 | [diff] [blame] | 577 | /* FIXME */ raw->next_in_ino = (void *)ref; |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 578 | if (ref->node) |
| 579 | delete_xattr_ref_node(c, ref); |
| 580 | ref->node = raw; |
| 581 | |
| 582 | dbg_xattr("success on saving xref (ino=%u, xid=%u)\n", ref->ic->ino, ref->xd->xid); |
| 583 | |
| 584 | return 0; |
| 585 | } |
| 586 | |
| 587 | 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] | 588 | struct jffs2_xattr_datum *xd) |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 589 | { |
| 590 | /* must be called under down_write(xattr_sem) */ |
| 591 | struct jffs2_xattr_ref *ref; |
| 592 | int ret; |
| 593 | |
| 594 | ref = jffs2_alloc_xattr_ref(); |
| 595 | if (!ref) |
| 596 | return ERR_PTR(-ENOMEM); |
| 597 | ref->ic = ic; |
| 598 | ref->xd = xd; |
| 599 | |
David Woodhouse | 9fe4854 | 2006-05-23 00:38:06 +0100 | [diff] [blame] | 600 | ret = save_xattr_ref(c, ref); |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 601 | if (ret) { |
| 602 | jffs2_free_xattr_ref(ref); |
| 603 | return ERR_PTR(ret); |
| 604 | } |
| 605 | |
| 606 | /* Chain to inode */ |
KaiGai Kohei | 8f2b6f4 | 2006-05-13 15:15:07 +0900 | [diff] [blame] | 607 | ref->next = ic->xref; |
| 608 | ic->xref = ref; |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 609 | |
| 610 | return ref; /* success */ |
| 611 | } |
| 612 | |
| 613 | void jffs2_xattr_delete_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic) |
| 614 | { |
| 615 | /* It's called from jffs2_clear_inode() on inode removing. |
| 616 | When an inode with XATTR is removed, those XATTRs must be removed. */ |
| 617 | struct jffs2_xattr_ref *ref, *_ref; |
| 618 | |
| 619 | if (!ic || ic->nlink > 0) |
| 620 | return; |
| 621 | |
| 622 | down_write(&c->xattr_sem); |
KaiGai Kohei | 8f2b6f4 | 2006-05-13 15:15:07 +0900 | [diff] [blame] | 623 | for (ref = ic->xref; ref; ref = _ref) { |
| 624 | _ref = ref->next; |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 625 | delete_xattr_ref(c, ref); |
KaiGai Kohei | 8f2b6f4 | 2006-05-13 15:15:07 +0900 | [diff] [blame] | 626 | } |
| 627 | ic->xref = NULL; |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 628 | up_write(&c->xattr_sem); |
| 629 | } |
| 630 | |
| 631 | void jffs2_xattr_free_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic) |
| 632 | { |
| 633 | /* It's called from jffs2_free_ino_caches() until unmounting FS. */ |
| 634 | struct jffs2_xattr_datum *xd; |
| 635 | struct jffs2_xattr_ref *ref, *_ref; |
| 636 | |
| 637 | down_write(&c->xattr_sem); |
KaiGai Kohei | 8f2b6f4 | 2006-05-13 15:15:07 +0900 | [diff] [blame] | 638 | for (ref = ic->xref; ref; ref = _ref) { |
| 639 | _ref = ref->next; |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 640 | xd = ref->xd; |
| 641 | xd->refcnt--; |
| 642 | if (!xd->refcnt) { |
| 643 | unload_xattr_datum(c, xd); |
| 644 | jffs2_free_xattr_datum(xd); |
| 645 | } |
| 646 | jffs2_free_xattr_ref(ref); |
| 647 | } |
KaiGai Kohei | 8f2b6f4 | 2006-05-13 15:15:07 +0900 | [diff] [blame] | 648 | ic->xref = NULL; |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 649 | up_write(&c->xattr_sem); |
| 650 | } |
| 651 | |
KaiGai Kohei | 8f2b6f4 | 2006-05-13 15:15:07 +0900 | [diff] [blame] | 652 | 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] | 653 | { |
KaiGai Kohei | 8f2b6f4 | 2006-05-13 15:15:07 +0900 | [diff] [blame] | 654 | /* 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] | 655 | * duplicate name/value pairs. If duplicate name/value pair would be found, |
| 656 | * one will be removed. |
| 657 | */ |
KaiGai Kohei | 8f2b6f4 | 2006-05-13 15:15:07 +0900 | [diff] [blame] | 658 | struct jffs2_xattr_ref *ref, *cmp, **pref; |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 659 | int rc = 0; |
| 660 | |
| 661 | if (likely(ic->flags & INO_FLAGS_XATTR_CHECKED)) |
| 662 | return 0; |
| 663 | down_write(&c->xattr_sem); |
| 664 | retry: |
| 665 | rc = 0; |
KaiGai Kohei | 8f2b6f4 | 2006-05-13 15:15:07 +0900 | [diff] [blame] | 666 | 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] | 667 | if (!ref->xd->xname) { |
| 668 | rc = load_xattr_datum(c, ref->xd); |
| 669 | if (unlikely(rc > 0)) { |
KaiGai Kohei | 8f2b6f4 | 2006-05-13 15:15:07 +0900 | [diff] [blame] | 670 | *pref = ref->next; |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 671 | delete_xattr_ref(c, ref); |
| 672 | goto retry; |
| 673 | } else if (unlikely(rc < 0)) |
| 674 | goto out; |
| 675 | } |
KaiGai Kohei | 8f2b6f4 | 2006-05-13 15:15:07 +0900 | [diff] [blame] | 676 | 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] | 677 | if (!cmp->xd->xname) { |
| 678 | ref->xd->flags |= JFFS2_XFLAGS_BIND; |
| 679 | rc = load_xattr_datum(c, cmp->xd); |
| 680 | ref->xd->flags &= ~JFFS2_XFLAGS_BIND; |
| 681 | if (unlikely(rc > 0)) { |
KaiGai Kohei | 8f2b6f4 | 2006-05-13 15:15:07 +0900 | [diff] [blame] | 682 | *pref = cmp->next; |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 683 | delete_xattr_ref(c, cmp); |
| 684 | goto retry; |
| 685 | } else if (unlikely(rc < 0)) |
| 686 | goto out; |
| 687 | } |
| 688 | if (ref->xd->xprefix == cmp->xd->xprefix |
| 689 | && !strcmp(ref->xd->xname, cmp->xd->xname)) { |
KaiGai Kohei | 8f2b6f4 | 2006-05-13 15:15:07 +0900 | [diff] [blame] | 690 | *pref = cmp->next; |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 691 | delete_xattr_ref(c, cmp); |
| 692 | goto retry; |
| 693 | } |
| 694 | } |
| 695 | } |
| 696 | ic->flags |= INO_FLAGS_XATTR_CHECKED; |
| 697 | out: |
| 698 | up_write(&c->xattr_sem); |
| 699 | |
| 700 | return rc; |
| 701 | } |
| 702 | |
| 703 | /* -------- xattr subsystem functions --------------- |
| 704 | * jffs2_init_xattr_subsystem(c) |
| 705 | * is used to initialize semaphore and list_head, and some variables. |
| 706 | * jffs2_find_xattr_datum(c, xid) |
| 707 | * is used to lookup xdatum while scanning process. |
| 708 | * jffs2_clear_xattr_subsystem(c) |
| 709 | * is used to release any xattr related objects. |
| 710 | * jffs2_build_xattr_subsystem(c) |
| 711 | * is used to associate xdatum and xref while super block building process. |
| 712 | * jffs2_setup_xattr_datum(c, xid, version) |
| 713 | * is used to insert xdatum while scanning process. |
| 714 | * -------------------------------------------------- */ |
| 715 | void jffs2_init_xattr_subsystem(struct jffs2_sb_info *c) |
| 716 | { |
| 717 | int i; |
| 718 | |
| 719 | for (i=0; i < XATTRINDEX_HASHSIZE; i++) |
| 720 | INIT_LIST_HEAD(&c->xattrindex[i]); |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 721 | INIT_LIST_HEAD(&c->xattr_unchecked); |
KaiGai Kohei | 8f2b6f4 | 2006-05-13 15:15:07 +0900 | [diff] [blame] | 722 | c->xref_temp = NULL; |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 723 | |
| 724 | init_rwsem(&c->xattr_sem); |
| 725 | c->xdatum_mem_usage = 0; |
| 726 | c->xdatum_mem_threshold = 32 * 1024; /* Default 32KB */ |
| 727 | } |
| 728 | |
| 729 | static struct jffs2_xattr_datum *jffs2_find_xattr_datum(struct jffs2_sb_info *c, uint32_t xid) |
| 730 | { |
| 731 | struct jffs2_xattr_datum *xd; |
| 732 | int i = xid % XATTRINDEX_HASHSIZE; |
| 733 | |
| 734 | /* It's only used in scanning/building process. */ |
| 735 | BUG_ON(!(c->flags & (JFFS2_SB_FLAG_SCANNING|JFFS2_SB_FLAG_BUILDING))); |
| 736 | |
| 737 | list_for_each_entry(xd, &c->xattrindex[i], xindex) { |
| 738 | if (xd->xid==xid) |
| 739 | return xd; |
| 740 | } |
| 741 | return NULL; |
| 742 | } |
| 743 | |
| 744 | void jffs2_clear_xattr_subsystem(struct jffs2_sb_info *c) |
| 745 | { |
| 746 | struct jffs2_xattr_datum *xd, *_xd; |
| 747 | struct jffs2_xattr_ref *ref, *_ref; |
| 748 | int i; |
| 749 | |
KaiGai Kohei | 8f2b6f4 | 2006-05-13 15:15:07 +0900 | [diff] [blame] | 750 | for (ref=c->xref_temp; ref; ref = _ref) { |
| 751 | _ref = ref->next; |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 752 | jffs2_free_xattr_ref(ref); |
KaiGai Kohei | 8f2b6f4 | 2006-05-13 15:15:07 +0900 | [diff] [blame] | 753 | } |
| 754 | c->xref_temp = NULL; |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 755 | |
| 756 | for (i=0; i < XATTRINDEX_HASHSIZE; i++) { |
| 757 | list_for_each_entry_safe(xd, _xd, &c->xattrindex[i], xindex) { |
| 758 | list_del(&xd->xindex); |
| 759 | if (xd->xname) |
| 760 | kfree(xd->xname); |
| 761 | jffs2_free_xattr_datum(xd); |
| 762 | } |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | void jffs2_build_xattr_subsystem(struct jffs2_sb_info *c) |
| 767 | { |
| 768 | struct jffs2_xattr_ref *ref, *_ref; |
| 769 | struct jffs2_xattr_datum *xd, *_xd; |
| 770 | struct jffs2_inode_cache *ic; |
| 771 | int i, xdatum_count =0, xdatum_unchecked_count = 0, xref_count = 0; |
| 772 | |
| 773 | BUG_ON(!(c->flags & JFFS2_SB_FLAG_BUILDING)); |
| 774 | |
| 775 | /* Phase.1 */ |
KaiGai Kohei | 8f2b6f4 | 2006-05-13 15:15:07 +0900 | [diff] [blame] | 776 | for (ref=c->xref_temp; ref; ref=_ref) { |
| 777 | _ref = ref->next; |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 778 | /* checking REF_UNCHECKED nodes */ |
| 779 | if (ref_flags(ref->node) != REF_PRISTINE) { |
| 780 | if (verify_xattr_ref(c, ref)) { |
| 781 | delete_xattr_ref_node(c, ref); |
| 782 | jffs2_free_xattr_ref(ref); |
| 783 | continue; |
| 784 | } |
| 785 | } |
| 786 | /* At this point, ref->xid and ref->ino contain XID and inode number. |
| 787 | ref->xd and ref->ic are not valid yet. */ |
| 788 | xd = jffs2_find_xattr_datum(c, ref->xid); |
| 789 | ic = jffs2_get_ino_cache(c, ref->ino); |
| 790 | if (!xd || !ic) { |
| 791 | if (ref_flags(ref->node) != REF_UNCHECKED) |
| 792 | JFFS2_WARNING("xref(ino=%u, xid=%u) is orphan. \n", |
| 793 | ref->ino, ref->xid); |
| 794 | delete_xattr_ref_node(c, ref); |
| 795 | jffs2_free_xattr_ref(ref); |
| 796 | continue; |
| 797 | } |
| 798 | ref->xd = xd; |
| 799 | ref->ic = ic; |
| 800 | xd->refcnt++; |
KaiGai Kohei | 8f2b6f4 | 2006-05-13 15:15:07 +0900 | [diff] [blame] | 801 | ref->next = ic->xref; |
| 802 | ic->xref = ref; |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 803 | xref_count++; |
| 804 | } |
KaiGai Kohei | 8f2b6f4 | 2006-05-13 15:15:07 +0900 | [diff] [blame] | 805 | c->xref_temp = NULL; |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 806 | /* After this, ref->xid/ino are NEVER used. */ |
| 807 | |
| 808 | /* Phase.2 */ |
| 809 | for (i=0; i < XATTRINDEX_HASHSIZE; i++) { |
| 810 | list_for_each_entry_safe(xd, _xd, &c->xattrindex[i], xindex) { |
| 811 | list_del_init(&xd->xindex); |
| 812 | if (!xd->refcnt) { |
| 813 | if (ref_flags(xd->node) != REF_UNCHECKED) |
| 814 | JFFS2_WARNING("orphan xdatum(xid=%u, version=%u) at %#08x\n", |
| 815 | xd->xid, xd->version, ref_offset(xd->node)); |
| 816 | delete_xattr_datum(c, xd); |
| 817 | continue; |
| 818 | } |
| 819 | if (ref_flags(xd->node) != REF_PRISTINE) { |
| 820 | dbg_xattr("unchecked xdatum(xid=%u) at %#08x\n", |
| 821 | xd->xid, ref_offset(xd->node)); |
| 822 | list_add(&xd->xindex, &c->xattr_unchecked); |
| 823 | xdatum_unchecked_count++; |
| 824 | } |
| 825 | xdatum_count++; |
| 826 | } |
| 827 | } |
| 828 | /* build complete */ |
| 829 | JFFS2_NOTICE("complete building xattr subsystem, %u of xdatum (%u unchecked) and " |
| 830 | "%u of xref found.\n", xdatum_count, xdatum_unchecked_count, xref_count); |
| 831 | } |
| 832 | |
| 833 | struct jffs2_xattr_datum *jffs2_setup_xattr_datum(struct jffs2_sb_info *c, |
| 834 | uint32_t xid, uint32_t version) |
| 835 | { |
| 836 | struct jffs2_xattr_datum *xd, *_xd; |
| 837 | |
| 838 | _xd = jffs2_find_xattr_datum(c, xid); |
| 839 | if (_xd) { |
| 840 | dbg_xattr("duplicate xdatum (xid=%u, version=%u/%u) at %#08x\n", |
| 841 | xid, version, _xd->version, ref_offset(_xd->node)); |
| 842 | if (version < _xd->version) |
| 843 | return ERR_PTR(-EEXIST); |
| 844 | } |
| 845 | xd = jffs2_alloc_xattr_datum(); |
| 846 | if (!xd) |
| 847 | return ERR_PTR(-ENOMEM); |
| 848 | xd->xid = xid; |
| 849 | xd->version = version; |
| 850 | if (xd->xid > c->highest_xid) |
| 851 | c->highest_xid = xd->xid; |
| 852 | list_add_tail(&xd->xindex, &c->xattrindex[xid % XATTRINDEX_HASHSIZE]); |
| 853 | |
| 854 | if (_xd) { |
| 855 | list_del_init(&_xd->xindex); |
| 856 | delete_xattr_datum_node(c, _xd); |
| 857 | jffs2_free_xattr_datum(_xd); |
| 858 | } |
| 859 | return xd; |
| 860 | } |
| 861 | |
| 862 | /* -------- xattr subsystem functions --------------- |
| 863 | * xprefix_to_handler(xprefix) |
| 864 | * is used to translate xprefix into xattr_handler. |
| 865 | * jffs2_listxattr(dentry, buffer, size) |
| 866 | * is an implementation of listxattr handler on jffs2. |
| 867 | * do_jffs2_getxattr(inode, xprefix, xname, buffer, size) |
| 868 | * is an implementation of getxattr handler on jffs2. |
| 869 | * do_jffs2_setxattr(inode, xprefix, xname, buffer, size, flags) |
| 870 | * is an implementation of setxattr handler on jffs2. |
| 871 | * -------------------------------------------------- */ |
| 872 | struct xattr_handler *jffs2_xattr_handlers[] = { |
| 873 | &jffs2_user_xattr_handler, |
| 874 | #ifdef CONFIG_JFFS2_FS_SECURITY |
| 875 | &jffs2_security_xattr_handler, |
| 876 | #endif |
| 877 | #ifdef CONFIG_JFFS2_FS_POSIX_ACL |
| 878 | &jffs2_acl_access_xattr_handler, |
| 879 | &jffs2_acl_default_xattr_handler, |
| 880 | #endif |
| 881 | &jffs2_trusted_xattr_handler, |
| 882 | NULL |
| 883 | }; |
| 884 | |
| 885 | static struct xattr_handler *xprefix_to_handler(int xprefix) { |
| 886 | struct xattr_handler *ret; |
| 887 | |
| 888 | switch (xprefix) { |
| 889 | case JFFS2_XPREFIX_USER: |
| 890 | ret = &jffs2_user_xattr_handler; |
| 891 | break; |
| 892 | #ifdef CONFIG_JFFS2_FS_SECURITY |
| 893 | case JFFS2_XPREFIX_SECURITY: |
| 894 | ret = &jffs2_security_xattr_handler; |
| 895 | break; |
| 896 | #endif |
| 897 | #ifdef CONFIG_JFFS2_FS_POSIX_ACL |
| 898 | case JFFS2_XPREFIX_ACL_ACCESS: |
| 899 | ret = &jffs2_acl_access_xattr_handler; |
| 900 | break; |
| 901 | case JFFS2_XPREFIX_ACL_DEFAULT: |
| 902 | ret = &jffs2_acl_default_xattr_handler; |
| 903 | break; |
| 904 | #endif |
| 905 | case JFFS2_XPREFIX_TRUSTED: |
| 906 | ret = &jffs2_trusted_xattr_handler; |
| 907 | break; |
| 908 | default: |
| 909 | ret = NULL; |
| 910 | break; |
| 911 | } |
| 912 | return ret; |
| 913 | } |
| 914 | |
| 915 | ssize_t jffs2_listxattr(struct dentry *dentry, char *buffer, size_t size) |
| 916 | { |
| 917 | struct inode *inode = dentry->d_inode; |
| 918 | struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode); |
| 919 | struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb); |
| 920 | struct jffs2_inode_cache *ic = f->inocache; |
KaiGai Kohei | 8f2b6f4 | 2006-05-13 15:15:07 +0900 | [diff] [blame] | 921 | struct jffs2_xattr_ref *ref, **pref; |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 922 | struct jffs2_xattr_datum *xd; |
| 923 | struct xattr_handler *xhandle; |
| 924 | ssize_t len, rc; |
| 925 | int retry = 0; |
| 926 | |
KaiGai Kohei | 8f2b6f4 | 2006-05-13 15:15:07 +0900 | [diff] [blame] | 927 | rc = check_xattr_ref_inode(c, ic); |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 928 | if (unlikely(rc)) |
| 929 | return rc; |
| 930 | |
| 931 | down_read(&c->xattr_sem); |
| 932 | retry: |
| 933 | len = 0; |
KaiGai Kohei | 8f2b6f4 | 2006-05-13 15:15:07 +0900 | [diff] [blame] | 934 | 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] | 935 | BUG_ON(ref->ic != ic); |
| 936 | xd = ref->xd; |
| 937 | if (!xd->xname) { |
| 938 | /* xdatum is unchached */ |
| 939 | if (!retry) { |
| 940 | retry = 1; |
| 941 | up_read(&c->xattr_sem); |
| 942 | down_write(&c->xattr_sem); |
| 943 | goto retry; |
| 944 | } else { |
| 945 | rc = load_xattr_datum(c, xd); |
| 946 | if (unlikely(rc > 0)) { |
KaiGai Kohei | 8f2b6f4 | 2006-05-13 15:15:07 +0900 | [diff] [blame] | 947 | *pref = ref->next; |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 948 | delete_xattr_ref(c, ref); |
| 949 | goto retry; |
| 950 | } else if (unlikely(rc < 0)) |
| 951 | goto out; |
| 952 | } |
| 953 | } |
| 954 | xhandle = xprefix_to_handler(xd->xprefix); |
| 955 | if (!xhandle) |
| 956 | continue; |
| 957 | if (buffer) { |
| 958 | rc = xhandle->list(inode, buffer+len, size-len, xd->xname, xd->name_len); |
| 959 | } else { |
| 960 | rc = xhandle->list(inode, NULL, 0, xd->xname, xd->name_len); |
| 961 | } |
| 962 | if (rc < 0) |
| 963 | goto out; |
| 964 | len += rc; |
| 965 | } |
| 966 | rc = len; |
| 967 | out: |
| 968 | if (!retry) { |
| 969 | up_read(&c->xattr_sem); |
| 970 | } else { |
| 971 | up_write(&c->xattr_sem); |
| 972 | } |
| 973 | return rc; |
| 974 | } |
| 975 | |
| 976 | int do_jffs2_getxattr(struct inode *inode, int xprefix, const char *xname, |
| 977 | char *buffer, size_t size) |
| 978 | { |
| 979 | struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode); |
| 980 | struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb); |
| 981 | struct jffs2_inode_cache *ic = f->inocache; |
| 982 | struct jffs2_xattr_datum *xd; |
KaiGai Kohei | 8f2b6f4 | 2006-05-13 15:15:07 +0900 | [diff] [blame] | 983 | struct jffs2_xattr_ref *ref, **pref; |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 984 | int rc, retry = 0; |
| 985 | |
KaiGai Kohei | 8f2b6f4 | 2006-05-13 15:15:07 +0900 | [diff] [blame] | 986 | rc = check_xattr_ref_inode(c, ic); |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 987 | if (unlikely(rc)) |
| 988 | return rc; |
| 989 | |
| 990 | down_read(&c->xattr_sem); |
| 991 | retry: |
KaiGai Kohei | 8f2b6f4 | 2006-05-13 15:15:07 +0900 | [diff] [blame] | 992 | 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] | 993 | BUG_ON(ref->ic!=ic); |
| 994 | |
| 995 | xd = ref->xd; |
| 996 | if (xd->xprefix != xprefix) |
| 997 | continue; |
| 998 | if (!xd->xname) { |
| 999 | /* xdatum is unchached */ |
| 1000 | if (!retry) { |
| 1001 | retry = 1; |
| 1002 | up_read(&c->xattr_sem); |
| 1003 | down_write(&c->xattr_sem); |
| 1004 | goto retry; |
| 1005 | } else { |
| 1006 | rc = load_xattr_datum(c, xd); |
| 1007 | if (unlikely(rc > 0)) { |
KaiGai Kohei | 8f2b6f4 | 2006-05-13 15:15:07 +0900 | [diff] [blame] | 1008 | *pref = ref->next; |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 1009 | delete_xattr_ref(c, ref); |
| 1010 | goto retry; |
| 1011 | } else if (unlikely(rc < 0)) { |
| 1012 | goto out; |
| 1013 | } |
| 1014 | } |
| 1015 | } |
| 1016 | if (!strcmp(xname, xd->xname)) { |
| 1017 | rc = xd->value_len; |
| 1018 | if (buffer) { |
| 1019 | if (size < rc) { |
| 1020 | rc = -ERANGE; |
| 1021 | } else { |
| 1022 | memcpy(buffer, xd->xvalue, rc); |
| 1023 | } |
| 1024 | } |
| 1025 | goto out; |
| 1026 | } |
| 1027 | } |
| 1028 | rc = -ENODATA; |
| 1029 | out: |
| 1030 | if (!retry) { |
| 1031 | up_read(&c->xattr_sem); |
| 1032 | } else { |
| 1033 | up_write(&c->xattr_sem); |
| 1034 | } |
| 1035 | return rc; |
| 1036 | } |
| 1037 | |
| 1038 | int do_jffs2_setxattr(struct inode *inode, int xprefix, const char *xname, |
| 1039 | const char *buffer, size_t size, int flags) |
| 1040 | { |
| 1041 | struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode); |
| 1042 | struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb); |
| 1043 | struct jffs2_inode_cache *ic = f->inocache; |
| 1044 | struct jffs2_xattr_datum *xd; |
KaiGai Kohei | 8f2b6f4 | 2006-05-13 15:15:07 +0900 | [diff] [blame] | 1045 | struct jffs2_xattr_ref *ref, *newref, **pref; |
David Woodhouse | 9fe4854 | 2006-05-23 00:38:06 +0100 | [diff] [blame] | 1046 | uint32_t length, request; |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 1047 | int rc; |
| 1048 | |
KaiGai Kohei | 8f2b6f4 | 2006-05-13 15:15:07 +0900 | [diff] [blame] | 1049 | rc = check_xattr_ref_inode(c, ic); |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 1050 | if (unlikely(rc)) |
| 1051 | return rc; |
| 1052 | |
| 1053 | request = PAD(sizeof(struct jffs2_raw_xattr) + strlen(xname) + 1 + size); |
David Woodhouse | 9fe4854 | 2006-05-23 00:38:06 +0100 | [diff] [blame] | 1054 | rc = jffs2_reserve_space(c, request, &length, |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 1055 | ALLOC_NORMAL, JFFS2_SUMMARY_XATTR_SIZE); |
| 1056 | if (rc) { |
| 1057 | JFFS2_WARNING("jffs2_reserve_space()=%d, request=%u\n", rc, request); |
| 1058 | return rc; |
| 1059 | } |
| 1060 | |
| 1061 | /* Find existing xattr */ |
| 1062 | down_write(&c->xattr_sem); |
| 1063 | retry: |
KaiGai Kohei | 8f2b6f4 | 2006-05-13 15:15:07 +0900 | [diff] [blame] | 1064 | 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] | 1065 | xd = ref->xd; |
| 1066 | if (xd->xprefix != xprefix) |
| 1067 | continue; |
| 1068 | if (!xd->xname) { |
| 1069 | rc = load_xattr_datum(c, xd); |
| 1070 | if (unlikely(rc > 0)) { |
KaiGai Kohei | 8f2b6f4 | 2006-05-13 15:15:07 +0900 | [diff] [blame] | 1071 | *pref = ref->next; |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 1072 | delete_xattr_ref(c, ref); |
| 1073 | goto retry; |
| 1074 | } else if (unlikely(rc < 0)) |
| 1075 | goto out; |
| 1076 | } |
| 1077 | if (!strcmp(xd->xname, xname)) { |
| 1078 | if (flags & XATTR_CREATE) { |
| 1079 | rc = -EEXIST; |
| 1080 | goto out; |
| 1081 | } |
| 1082 | if (!buffer) { |
KaiGai Kohei | 8f2b6f4 | 2006-05-13 15:15:07 +0900 | [diff] [blame] | 1083 | *pref = ref->next; |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 1084 | delete_xattr_ref(c, ref); |
| 1085 | rc = 0; |
| 1086 | goto out; |
| 1087 | } |
| 1088 | goto found; |
| 1089 | } |
| 1090 | } |
| 1091 | /* not found */ |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 1092 | if (flags & XATTR_REPLACE) { |
| 1093 | rc = -ENODATA; |
| 1094 | goto out; |
| 1095 | } |
| 1096 | if (!buffer) { |
| 1097 | rc = -EINVAL; |
| 1098 | goto out; |
| 1099 | } |
| 1100 | found: |
David Woodhouse | 9fe4854 | 2006-05-23 00:38:06 +0100 | [diff] [blame] | 1101 | xd = create_xattr_datum(c, xprefix, xname, buffer, size); |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 1102 | if (IS_ERR(xd)) { |
| 1103 | rc = PTR_ERR(xd); |
| 1104 | goto out; |
| 1105 | } |
| 1106 | up_write(&c->xattr_sem); |
| 1107 | jffs2_complete_reservation(c); |
| 1108 | |
| 1109 | /* create xattr_ref */ |
| 1110 | request = PAD(sizeof(struct jffs2_raw_xref)); |
David Woodhouse | 9fe4854 | 2006-05-23 00:38:06 +0100 | [diff] [blame] | 1111 | rc = jffs2_reserve_space(c, request, &length, |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 1112 | ALLOC_NORMAL, JFFS2_SUMMARY_XREF_SIZE); |
| 1113 | if (rc) { |
| 1114 | JFFS2_WARNING("jffs2_reserve_space()=%d, request=%u\n", rc, request); |
| 1115 | down_write(&c->xattr_sem); |
| 1116 | xd->refcnt--; |
| 1117 | if (!xd->refcnt) |
| 1118 | delete_xattr_datum(c, xd); |
| 1119 | up_write(&c->xattr_sem); |
| 1120 | return rc; |
| 1121 | } |
| 1122 | down_write(&c->xattr_sem); |
KaiGai Kohei | 8f2b6f4 | 2006-05-13 15:15:07 +0900 | [diff] [blame] | 1123 | if (ref) |
| 1124 | *pref = ref->next; |
David Woodhouse | 9fe4854 | 2006-05-23 00:38:06 +0100 | [diff] [blame] | 1125 | newref = create_xattr_ref(c, ic, xd); |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 1126 | if (IS_ERR(newref)) { |
KaiGai Kohei | 8f2b6f4 | 2006-05-13 15:15:07 +0900 | [diff] [blame] | 1127 | if (ref) { |
| 1128 | ref->next = ic->xref; |
| 1129 | ic->xref = ref; |
| 1130 | } |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 1131 | rc = PTR_ERR(newref); |
| 1132 | xd->refcnt--; |
| 1133 | if (!xd->refcnt) |
| 1134 | delete_xattr_datum(c, xd); |
| 1135 | } else if (ref) { |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 1136 | delete_xattr_ref(c, ref); |
| 1137 | } |
| 1138 | out: |
| 1139 | up_write(&c->xattr_sem); |
| 1140 | jffs2_complete_reservation(c); |
| 1141 | return rc; |
| 1142 | } |
| 1143 | |
| 1144 | /* -------- garbage collector functions ------------- |
| 1145 | * jffs2_garbage_collect_xattr_datum(c, xd) |
| 1146 | * is used to move xdatum into new node. |
| 1147 | * jffs2_garbage_collect_xattr_ref(c, ref) |
| 1148 | * is used to move xref into new node. |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 1149 | * jffs2_verify_xattr(c) |
| 1150 | * is used to call do_verify_xattr_datum() before garbage collecting. |
| 1151 | * -------------------------------------------------- */ |
KaiGai Kohei | 084702e | 2006-05-13 15:16:13 +0900 | [diff] [blame] | 1152 | 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] | 1153 | { |
David Woodhouse | 9fe4854 | 2006-05-23 00:38:06 +0100 | [diff] [blame] | 1154 | uint32_t totlen, length, old_ofs; |
KaiGai Kohei | 084702e | 2006-05-13 15:16:13 +0900 | [diff] [blame] | 1155 | int rc = -EINVAL; |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 1156 | |
KaiGai Kohei | 084702e | 2006-05-13 15:16:13 +0900 | [diff] [blame] | 1157 | down_write(&c->xattr_sem); |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 1158 | BUG_ON(!xd->node); |
| 1159 | |
| 1160 | old_ofs = ref_offset(xd->node); |
| 1161 | totlen = ref_totlen(c, c->gcblock, xd->node); |
| 1162 | if (totlen < sizeof(struct jffs2_raw_xattr)) |
KaiGai Kohei | 084702e | 2006-05-13 15:16:13 +0900 | [diff] [blame] | 1163 | goto out; |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 1164 | |
| 1165 | if (!xd->xname) { |
| 1166 | rc = load_xattr_datum(c, xd); |
| 1167 | if (unlikely(rc > 0)) { |
| 1168 | delete_xattr_datum_node(c, xd); |
KaiGai Kohei | 084702e | 2006-05-13 15:16:13 +0900 | [diff] [blame] | 1169 | rc = 0; |
| 1170 | goto out; |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 1171 | } else if (unlikely(rc < 0)) |
KaiGai Kohei | 084702e | 2006-05-13 15:16:13 +0900 | [diff] [blame] | 1172 | goto out; |
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 | rc = jffs2_reserve_space_gc(c, totlen, &length, JFFS2_SUMMARY_XATTR_SIZE); |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 1175 | if (rc || length < totlen) { |
| 1176 | JFFS2_WARNING("jffs2_reserve_space()=%d, request=%u\n", rc, totlen); |
KaiGai Kohei | 084702e | 2006-05-13 15:16:13 +0900 | [diff] [blame] | 1177 | rc = rc ? rc : -EBADFD; |
| 1178 | goto out; |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 1179 | } |
David Woodhouse | 9fe4854 | 2006-05-23 00:38:06 +0100 | [diff] [blame] | 1180 | rc = save_xattr_datum(c, xd); |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 1181 | if (!rc) |
| 1182 | dbg_xattr("xdatum (xid=%u, version=%u) GC'ed from %#08x to %08x\n", |
| 1183 | xd->xid, xd->version, old_ofs, ref_offset(xd->node)); |
KaiGai Kohei | 084702e | 2006-05-13 15:16:13 +0900 | [diff] [blame] | 1184 | out: |
| 1185 | up_write(&c->xattr_sem); |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 1186 | return rc; |
| 1187 | } |
| 1188 | |
| 1189 | |
KaiGai Kohei | 084702e | 2006-05-13 15:16:13 +0900 | [diff] [blame] | 1190 | 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] | 1191 | { |
David Woodhouse | 9fe4854 | 2006-05-23 00:38:06 +0100 | [diff] [blame] | 1192 | uint32_t totlen, length, old_ofs; |
KaiGai Kohei | 084702e | 2006-05-13 15:16:13 +0900 | [diff] [blame] | 1193 | int rc = -EINVAL; |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 1194 | |
KaiGai Kohei | 084702e | 2006-05-13 15:16:13 +0900 | [diff] [blame] | 1195 | down_write(&c->xattr_sem); |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 1196 | BUG_ON(!ref->node); |
| 1197 | |
| 1198 | old_ofs = ref_offset(ref->node); |
| 1199 | totlen = ref_totlen(c, c->gcblock, ref->node); |
| 1200 | if (totlen != sizeof(struct jffs2_raw_xref)) |
KaiGai Kohei | 084702e | 2006-05-13 15:16:13 +0900 | [diff] [blame] | 1201 | goto out; |
| 1202 | |
David Woodhouse | 9fe4854 | 2006-05-23 00:38:06 +0100 | [diff] [blame] | 1203 | rc = jffs2_reserve_space_gc(c, totlen, &length, JFFS2_SUMMARY_XREF_SIZE); |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 1204 | if (rc || length < totlen) { |
| 1205 | JFFS2_WARNING("%s: jffs2_reserve_space() = %d, request = %u\n", |
| 1206 | __FUNCTION__, rc, totlen); |
KaiGai Kohei | 084702e | 2006-05-13 15:16:13 +0900 | [diff] [blame] | 1207 | rc = rc ? rc : -EBADFD; |
| 1208 | goto out; |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 1209 | } |
David Woodhouse | 9fe4854 | 2006-05-23 00:38:06 +0100 | [diff] [blame] | 1210 | rc = save_xattr_ref(c, ref); |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 1211 | if (!rc) |
| 1212 | dbg_xattr("xref (ino=%u, xid=%u) GC'ed from %#08x to %08x\n", |
| 1213 | ref->ic->ino, ref->xd->xid, old_ofs, ref_offset(ref->node)); |
KaiGai Kohei | 084702e | 2006-05-13 15:16:13 +0900 | [diff] [blame] | 1214 | out: |
| 1215 | up_write(&c->xattr_sem); |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 1216 | return rc; |
| 1217 | } |
| 1218 | |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 1219 | int jffs2_verify_xattr(struct jffs2_sb_info *c) |
| 1220 | { |
| 1221 | struct jffs2_xattr_datum *xd, *_xd; |
| 1222 | int rc; |
| 1223 | |
| 1224 | down_write(&c->xattr_sem); |
| 1225 | list_for_each_entry_safe(xd, _xd, &c->xattr_unchecked, xindex) { |
| 1226 | rc = do_verify_xattr_datum(c, xd); |
| 1227 | if (rc == 0) { |
| 1228 | list_del_init(&xd->xindex); |
| 1229 | break; |
| 1230 | } else if (rc > 0) { |
| 1231 | list_del_init(&xd->xindex); |
| 1232 | delete_xattr_datum_node(c, xd); |
| 1233 | } |
| 1234 | } |
| 1235 | up_write(&c->xattr_sem); |
| 1236 | |
| 1237 | return list_empty(&c->xattr_unchecked) ? 1 : 0; |
| 1238 | } |