xattr.c 38 KB

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