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