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