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