xattr.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329
  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. 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. 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(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 (!--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. xd->refcnt--;
  541. if (!xd->refcnt) {
  542. unload_xattr_datum(c, xd);
  543. jffs2_free_xattr_datum(xd);
  544. }
  545. jffs2_free_xattr_ref(ref);
  546. }
  547. ic->xref = NULL;
  548. up_write(&c->xattr_sem);
  549. }
  550. static int check_xattr_ref_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
  551. {
  552. /* success of check_xattr_ref_inode() means taht inode (ic) dose not have
  553. * duplicate name/value pairs. If duplicate name/value pair would be found,
  554. * one will be removed.
  555. */
  556. struct jffs2_xattr_ref *ref, *cmp, **pref, **pcmp;
  557. int rc = 0;
  558. if (likely(ic->flags & INO_FLAGS_XATTR_CHECKED))
  559. return 0;
  560. down_write(&c->xattr_sem);
  561. retry:
  562. rc = 0;
  563. for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
  564. if (!ref->xd->xname) {
  565. rc = load_xattr_datum(c, ref->xd);
  566. if (unlikely(rc > 0)) {
  567. *pref = ref->next;
  568. delete_xattr_ref(c, ref);
  569. goto retry;
  570. } else if (unlikely(rc < 0))
  571. goto out;
  572. }
  573. for (cmp=ref->next, pcmp=&ref->next; cmp; pcmp=&cmp->next, cmp=cmp->next) {
  574. if (!cmp->xd->xname) {
  575. ref->xd->flags |= JFFS2_XFLAGS_BIND;
  576. rc = load_xattr_datum(c, cmp->xd);
  577. ref->xd->flags &= ~JFFS2_XFLAGS_BIND;
  578. if (unlikely(rc > 0)) {
  579. *pcmp = cmp->next;
  580. delete_xattr_ref(c, cmp);
  581. goto retry;
  582. } else if (unlikely(rc < 0))
  583. goto out;
  584. }
  585. if (ref->xd->xprefix == cmp->xd->xprefix
  586. && !strcmp(ref->xd->xname, cmp->xd->xname)) {
  587. if (ref->xseqno > cmp->xseqno) {
  588. *pcmp = cmp->next;
  589. delete_xattr_ref(c, cmp);
  590. } else {
  591. *pref = ref->next;
  592. delete_xattr_ref(c, ref);
  593. }
  594. goto retry;
  595. }
  596. }
  597. }
  598. ic->flags |= INO_FLAGS_XATTR_CHECKED;
  599. out:
  600. up_write(&c->xattr_sem);
  601. return rc;
  602. }
  603. /* -------- xattr subsystem functions ---------------
  604. * jffs2_init_xattr_subsystem(c)
  605. * is used to initialize semaphore and list_head, and some variables.
  606. * jffs2_find_xattr_datum(c, xid)
  607. * is used to lookup xdatum while scanning process.
  608. * jffs2_clear_xattr_subsystem(c)
  609. * is used to release any xattr related objects.
  610. * jffs2_build_xattr_subsystem(c)
  611. * is used to associate xdatum and xref while super block building process.
  612. * jffs2_setup_xattr_datum(c, xid, version)
  613. * is used to insert xdatum while scanning process.
  614. * -------------------------------------------------- */
  615. void jffs2_init_xattr_subsystem(struct jffs2_sb_info *c)
  616. {
  617. int i;
  618. for (i=0; i < XATTRINDEX_HASHSIZE; i++)
  619. INIT_LIST_HEAD(&c->xattrindex[i]);
  620. INIT_LIST_HEAD(&c->xattr_unchecked);
  621. INIT_LIST_HEAD(&c->xattr_dead_list);
  622. c->xref_dead_list = NULL;
  623. c->xref_temp = NULL;
  624. init_rwsem(&c->xattr_sem);
  625. c->highest_xid = 0;
  626. c->highest_xseqno = 0;
  627. c->xdatum_mem_usage = 0;
  628. c->xdatum_mem_threshold = 32 * 1024; /* Default 32KB */
  629. }
  630. static struct jffs2_xattr_datum *jffs2_find_xattr_datum(struct jffs2_sb_info *c, uint32_t xid)
  631. {
  632. struct jffs2_xattr_datum *xd;
  633. int i = xid % XATTRINDEX_HASHSIZE;
  634. /* It's only used in scanning/building process. */
  635. BUG_ON(!(c->flags & (JFFS2_SB_FLAG_SCANNING|JFFS2_SB_FLAG_BUILDING)));
  636. list_for_each_entry(xd, &c->xattrindex[i], xindex) {
  637. if (xd->xid==xid)
  638. return xd;
  639. }
  640. return NULL;
  641. }
  642. void jffs2_clear_xattr_subsystem(struct jffs2_sb_info *c)
  643. {
  644. struct jffs2_xattr_datum *xd, *_xd;
  645. struct jffs2_xattr_ref *ref, *_ref;
  646. int i;
  647. for (ref=c->xref_temp; ref; ref = _ref) {
  648. _ref = ref->next;
  649. jffs2_free_xattr_ref(ref);
  650. }
  651. for (ref=c->xref_dead_list; ref; ref = _ref) {
  652. _ref = ref->next;
  653. jffs2_free_xattr_ref(ref);
  654. }
  655. for (i=0; i < XATTRINDEX_HASHSIZE; i++) {
  656. list_for_each_entry_safe(xd, _xd, &c->xattrindex[i], xindex) {
  657. list_del(&xd->xindex);
  658. if (xd->xname)
  659. kfree(xd->xname);
  660. jffs2_free_xattr_datum(xd);
  661. }
  662. }
  663. list_for_each_entry_safe(xd, _xd, &c->xattr_dead_list, xindex) {
  664. list_del(&xd->xindex);
  665. jffs2_free_xattr_datum(xd);
  666. }
  667. }
  668. #define XREF_TMPHASH_SIZE (128)
  669. void jffs2_build_xattr_subsystem(struct jffs2_sb_info *c)
  670. {
  671. struct jffs2_xattr_ref *ref, *_ref;
  672. struct jffs2_xattr_ref *xref_tmphash[XREF_TMPHASH_SIZE];
  673. struct jffs2_xattr_datum *xd, *_xd;
  674. struct jffs2_inode_cache *ic;
  675. struct jffs2_raw_node_ref *raw;
  676. int i, xdatum_count = 0, xdatum_unchecked_count = 0, xref_count = 0;
  677. int xdatum_orphan_count = 0, xref_orphan_count = 0, xref_dead_count = 0;
  678. BUG_ON(!(c->flags & JFFS2_SB_FLAG_BUILDING));
  679. /* Phase.1 : Merge same xref */
  680. for (i=0; i < XREF_TMPHASH_SIZE; i++)
  681. xref_tmphash[i] = NULL;
  682. for (ref=c->xref_temp; ref; ref=_ref) {
  683. struct jffs2_xattr_ref *tmp;
  684. _ref = ref->next;
  685. if (ref_flags(ref->node) != REF_PRISTINE) {
  686. if (verify_xattr_ref(c, ref)) {
  687. BUG_ON(ref->node->next_in_ino != (void *)ref);
  688. ref->node->next_in_ino = NULL;
  689. jffs2_mark_node_obsolete(c, ref->node);
  690. jffs2_free_xattr_ref(ref);
  691. continue;
  692. }
  693. }
  694. i = (ref->ino ^ ref->xid) % XREF_TMPHASH_SIZE;
  695. for (tmp=xref_tmphash[i]; tmp; tmp=tmp->next) {
  696. if (tmp->ino == ref->ino && tmp->xid == ref->xid)
  697. break;
  698. }
  699. if (tmp) {
  700. raw = ref->node;
  701. if (ref->xseqno > tmp->xseqno) {
  702. tmp->xseqno = ref->xseqno;
  703. raw->next_in_ino = tmp->node;
  704. tmp->node = raw;
  705. } else {
  706. raw->next_in_ino = tmp->node->next_in_ino;
  707. tmp->node->next_in_ino = raw;
  708. }
  709. jffs2_free_xattr_ref(ref);
  710. continue;
  711. } else {
  712. ref->next = xref_tmphash[i];
  713. xref_tmphash[i] = ref;
  714. }
  715. }
  716. c->xref_temp = NULL;
  717. /* Phase.2 : Bind xref with inode_cache and xattr_datum */
  718. for (i=0; i < XREF_TMPHASH_SIZE; i++) {
  719. for (ref=xref_tmphash[i]; ref; ref=_ref) {
  720. xref_count++;
  721. _ref = ref->next;
  722. if (is_xattr_ref_dead(ref)) {
  723. ref->next = c->xref_dead_list;
  724. c->xref_dead_list = ref;
  725. xref_dead_count++;
  726. continue;
  727. }
  728. /* At this point, ref->xid and ref->ino contain XID and inode number.
  729. ref->xd and ref->ic are not valid yet. */
  730. xd = jffs2_find_xattr_datum(c, ref->xid);
  731. ic = jffs2_get_ino_cache(c, ref->ino);
  732. if (!xd || !ic) {
  733. dbg_xattr("xref(ino=%u, xid=%u, xseqno=%u) is orphan.\n",
  734. ref->ino, ref->xid, ref->xseqno);
  735. ref->xseqno |= XREF_DELETE_MARKER;
  736. ref->next = c->xref_dead_list;
  737. c->xref_dead_list = ref;
  738. xref_orphan_count++;
  739. continue;
  740. }
  741. ref->xd = xd;
  742. ref->ic = ic;
  743. xd->refcnt++;
  744. ref->next = ic->xref;
  745. ic->xref = ref;
  746. }
  747. }
  748. /* Phase.3 : Link unchecked xdatum to xattr_unchecked list */
  749. for (i=0; i < XATTRINDEX_HASHSIZE; i++) {
  750. list_for_each_entry_safe(xd, _xd, &c->xattrindex[i], xindex) {
  751. xdatum_count++;
  752. list_del_init(&xd->xindex);
  753. if (!xd->refcnt) {
  754. dbg_xattr("xdatum(xid=%u, version=%u) is orphan.\n",
  755. xd->xid, xd->version);
  756. xd->flags |= JFFS2_XFLAGS_DEAD;
  757. list_add(&xd->xindex, &c->xattr_unchecked);
  758. xdatum_orphan_count++;
  759. continue;
  760. }
  761. if (is_xattr_datum_unchecked(c, xd)) {
  762. dbg_xattr("unchecked xdatum(xid=%u, version=%u)\n",
  763. xd->xid, xd->version);
  764. list_add(&xd->xindex, &c->xattr_unchecked);
  765. xdatum_unchecked_count++;
  766. }
  767. }
  768. }
  769. /* build complete */
  770. JFFS2_NOTICE("complete building xattr subsystem, %u of xdatum"
  771. " (%u unchecked, %u orphan) and "
  772. "%u of xref (%u dead, %u orphan) found.\n",
  773. xdatum_count, xdatum_unchecked_count, xdatum_orphan_count,
  774. xref_count, xref_dead_count, xref_orphan_count);
  775. }
  776. struct jffs2_xattr_datum *jffs2_setup_xattr_datum(struct jffs2_sb_info *c,
  777. uint32_t xid, uint32_t version)
  778. {
  779. struct jffs2_xattr_datum *xd;
  780. xd = jffs2_find_xattr_datum(c, xid);
  781. if (!xd) {
  782. xd = jffs2_alloc_xattr_datum();
  783. if (!xd)
  784. return ERR_PTR(-ENOMEM);
  785. xd->xid = xid;
  786. xd->version = version;
  787. if (xd->xid > c->highest_xid)
  788. c->highest_xid = xd->xid;
  789. list_add_tail(&xd->xindex, &c->xattrindex[xid % XATTRINDEX_HASHSIZE]);
  790. }
  791. return xd;
  792. }
  793. /* -------- xattr subsystem functions ---------------
  794. * xprefix_to_handler(xprefix)
  795. * is used to translate xprefix into xattr_handler.
  796. * jffs2_listxattr(dentry, buffer, size)
  797. * is an implementation of listxattr handler on jffs2.
  798. * do_jffs2_getxattr(inode, xprefix, xname, buffer, size)
  799. * is an implementation of getxattr handler on jffs2.
  800. * do_jffs2_setxattr(inode, xprefix, xname, buffer, size, flags)
  801. * is an implementation of setxattr handler on jffs2.
  802. * -------------------------------------------------- */
  803. struct xattr_handler *jffs2_xattr_handlers[] = {
  804. &jffs2_user_xattr_handler,
  805. #ifdef CONFIG_JFFS2_FS_SECURITY
  806. &jffs2_security_xattr_handler,
  807. #endif
  808. #ifdef CONFIG_JFFS2_FS_POSIX_ACL
  809. &jffs2_acl_access_xattr_handler,
  810. &jffs2_acl_default_xattr_handler,
  811. #endif
  812. &jffs2_trusted_xattr_handler,
  813. NULL
  814. };
  815. static struct xattr_handler *xprefix_to_handler(int xprefix) {
  816. struct xattr_handler *ret;
  817. switch (xprefix) {
  818. case JFFS2_XPREFIX_USER:
  819. ret = &jffs2_user_xattr_handler;
  820. break;
  821. #ifdef CONFIG_JFFS2_FS_SECURITY
  822. case JFFS2_XPREFIX_SECURITY:
  823. ret = &jffs2_security_xattr_handler;
  824. break;
  825. #endif
  826. #ifdef CONFIG_JFFS2_FS_POSIX_ACL
  827. case JFFS2_XPREFIX_ACL_ACCESS:
  828. ret = &jffs2_acl_access_xattr_handler;
  829. break;
  830. case JFFS2_XPREFIX_ACL_DEFAULT:
  831. ret = &jffs2_acl_default_xattr_handler;
  832. break;
  833. #endif
  834. case JFFS2_XPREFIX_TRUSTED:
  835. ret = &jffs2_trusted_xattr_handler;
  836. break;
  837. default:
  838. ret = NULL;
  839. break;
  840. }
  841. return ret;
  842. }
  843. ssize_t jffs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
  844. {
  845. struct inode *inode = dentry->d_inode;
  846. struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
  847. struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
  848. struct jffs2_inode_cache *ic = f->inocache;
  849. struct jffs2_xattr_ref *ref, **pref;
  850. struct jffs2_xattr_datum *xd;
  851. struct xattr_handler *xhandle;
  852. ssize_t len, rc;
  853. int retry = 0;
  854. rc = check_xattr_ref_inode(c, ic);
  855. if (unlikely(rc))
  856. return rc;
  857. down_read(&c->xattr_sem);
  858. retry:
  859. len = 0;
  860. for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
  861. BUG_ON(ref->ic != ic);
  862. xd = ref->xd;
  863. if (!xd->xname) {
  864. /* xdatum is unchached */
  865. if (!retry) {
  866. retry = 1;
  867. up_read(&c->xattr_sem);
  868. down_write(&c->xattr_sem);
  869. goto retry;
  870. } else {
  871. rc = load_xattr_datum(c, xd);
  872. if (unlikely(rc > 0)) {
  873. *pref = ref->next;
  874. delete_xattr_ref(c, ref);
  875. goto retry;
  876. } else if (unlikely(rc < 0))
  877. goto out;
  878. }
  879. }
  880. xhandle = xprefix_to_handler(xd->xprefix);
  881. if (!xhandle)
  882. continue;
  883. if (buffer) {
  884. rc = xhandle->list(inode, buffer+len, size-len, xd->xname, xd->name_len);
  885. } else {
  886. rc = xhandle->list(inode, NULL, 0, xd->xname, xd->name_len);
  887. }
  888. if (rc < 0)
  889. goto out;
  890. len += rc;
  891. }
  892. rc = len;
  893. out:
  894. if (!retry) {
  895. up_read(&c->xattr_sem);
  896. } else {
  897. up_write(&c->xattr_sem);
  898. }
  899. return rc;
  900. }
  901. int do_jffs2_getxattr(struct inode *inode, int xprefix, const char *xname,
  902. char *buffer, size_t size)
  903. {
  904. struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
  905. struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
  906. struct jffs2_inode_cache *ic = f->inocache;
  907. struct jffs2_xattr_datum *xd;
  908. struct jffs2_xattr_ref *ref, **pref;
  909. int rc, retry = 0;
  910. rc = check_xattr_ref_inode(c, ic);
  911. if (unlikely(rc))
  912. return rc;
  913. down_read(&c->xattr_sem);
  914. retry:
  915. for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
  916. BUG_ON(ref->ic!=ic);
  917. xd = ref->xd;
  918. if (xd->xprefix != xprefix)
  919. continue;
  920. if (!xd->xname) {
  921. /* xdatum is unchached */
  922. if (!retry) {
  923. retry = 1;
  924. up_read(&c->xattr_sem);
  925. down_write(&c->xattr_sem);
  926. goto retry;
  927. } else {
  928. rc = load_xattr_datum(c, xd);
  929. if (unlikely(rc > 0)) {
  930. *pref = ref->next;
  931. delete_xattr_ref(c, ref);
  932. goto retry;
  933. } else if (unlikely(rc < 0)) {
  934. goto out;
  935. }
  936. }
  937. }
  938. if (!strcmp(xname, xd->xname)) {
  939. rc = xd->value_len;
  940. if (buffer) {
  941. if (size < rc) {
  942. rc = -ERANGE;
  943. } else {
  944. memcpy(buffer, xd->xvalue, rc);
  945. }
  946. }
  947. goto out;
  948. }
  949. }
  950. rc = -ENODATA;
  951. out:
  952. if (!retry) {
  953. up_read(&c->xattr_sem);
  954. } else {
  955. up_write(&c->xattr_sem);
  956. }
  957. return rc;
  958. }
  959. int do_jffs2_setxattr(struct inode *inode, int xprefix, const char *xname,
  960. const char *buffer, size_t size, int flags)
  961. {
  962. struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
  963. struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
  964. struct jffs2_inode_cache *ic = f->inocache;
  965. struct jffs2_xattr_datum *xd;
  966. struct jffs2_xattr_ref *ref, *newref, **pref;
  967. uint32_t length, request;
  968. int rc;
  969. rc = check_xattr_ref_inode(c, ic);
  970. if (unlikely(rc))
  971. return rc;
  972. request = PAD(sizeof(struct jffs2_raw_xattr) + strlen(xname) + 1 + size);
  973. rc = jffs2_reserve_space(c, request, &length,
  974. ALLOC_NORMAL, JFFS2_SUMMARY_XATTR_SIZE);
  975. if (rc) {
  976. JFFS2_WARNING("jffs2_reserve_space()=%d, request=%u\n", rc, request);
  977. return rc;
  978. }
  979. /* Find existing xattr */
  980. down_write(&c->xattr_sem);
  981. retry:
  982. for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
  983. xd = ref->xd;
  984. if (xd->xprefix != xprefix)
  985. continue;
  986. if (!xd->xname) {
  987. rc = load_xattr_datum(c, xd);
  988. if (unlikely(rc > 0)) {
  989. *pref = ref->next;
  990. delete_xattr_ref(c, ref);
  991. goto retry;
  992. } else if (unlikely(rc < 0))
  993. goto out;
  994. }
  995. if (!strcmp(xd->xname, xname)) {
  996. if (flags & XATTR_CREATE) {
  997. rc = -EEXIST;
  998. goto out;
  999. }
  1000. if (!buffer) {
  1001. ref->ino = ic->ino;
  1002. ref->xid = xd->xid;
  1003. ref->xseqno |= XREF_DELETE_MARKER;
  1004. rc = save_xattr_ref(c, ref);
  1005. if (!rc) {
  1006. *pref = ref->next;
  1007. spin_lock(&c->erase_completion_lock);
  1008. ref->next = c->xref_dead_list;
  1009. c->xref_dead_list = ref;
  1010. spin_unlock(&c->erase_completion_lock);
  1011. if (!--xd->refcnt)
  1012. delete_xattr_datum(c, xd);
  1013. } else {
  1014. ref->ic = ic;
  1015. ref->xd = xd;
  1016. ref->xseqno &= ~XREF_DELETE_MARKER;
  1017. }
  1018. goto out;
  1019. }
  1020. goto found;
  1021. }
  1022. }
  1023. /* not found */
  1024. if (flags & XATTR_REPLACE) {
  1025. rc = -ENODATA;
  1026. goto out;
  1027. }
  1028. if (!buffer) {
  1029. rc = -ENODATA;
  1030. goto out;
  1031. }
  1032. found:
  1033. xd = create_xattr_datum(c, xprefix, xname, buffer, size);
  1034. if (IS_ERR(xd)) {
  1035. rc = PTR_ERR(xd);
  1036. goto out;
  1037. }
  1038. up_write(&c->xattr_sem);
  1039. jffs2_complete_reservation(c);
  1040. /* create xattr_ref */
  1041. request = PAD(sizeof(struct jffs2_raw_xref));
  1042. rc = jffs2_reserve_space(c, request, &length,
  1043. ALLOC_NORMAL, JFFS2_SUMMARY_XREF_SIZE);
  1044. down_write(&c->xattr_sem);
  1045. if (rc) {
  1046. JFFS2_WARNING("jffs2_reserve_space()=%d, request=%u\n", rc, request);
  1047. xd->refcnt--;
  1048. if (!xd->refcnt)
  1049. delete_xattr_datum(c, xd);
  1050. up_write(&c->xattr_sem);
  1051. return rc;
  1052. }
  1053. if (ref)
  1054. *pref = ref->next;
  1055. newref = create_xattr_ref(c, ic, xd);
  1056. if (IS_ERR(newref)) {
  1057. if (ref) {
  1058. ref->next = ic->xref;
  1059. ic->xref = ref;
  1060. }
  1061. rc = PTR_ERR(newref);
  1062. xd->refcnt--;
  1063. if (!xd->refcnt)
  1064. delete_xattr_datum(c, xd);
  1065. } else if (ref) {
  1066. delete_xattr_ref(c, ref);
  1067. }
  1068. out:
  1069. up_write(&c->xattr_sem);
  1070. jffs2_complete_reservation(c);
  1071. return rc;
  1072. }
  1073. /* -------- garbage collector functions -------------
  1074. * jffs2_garbage_collect_xattr_datum(c, xd, raw)
  1075. * is used to move xdatum into new node.
  1076. * jffs2_garbage_collect_xattr_ref(c, ref, raw)
  1077. * is used to move xref into new node.
  1078. * jffs2_verify_xattr(c)
  1079. * is used to call do_verify_xattr_datum() before garbage collecting.
  1080. * jffs2_release_xattr_datum(c, xd)
  1081. * is used to release an in-memory object of xdatum.
  1082. * jffs2_release_xattr_ref(c, ref)
  1083. * is used to release an in-memory object of xref.
  1084. * -------------------------------------------------- */
  1085. int jffs2_garbage_collect_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd,
  1086. struct jffs2_raw_node_ref *raw)
  1087. {
  1088. uint32_t totlen, length, old_ofs;
  1089. int rc = 0;
  1090. down_write(&c->xattr_sem);
  1091. if (xd->node != raw)
  1092. goto out;
  1093. if (xd->flags & (JFFS2_XFLAGS_DEAD|JFFS2_XFLAGS_INVALID))
  1094. goto out;
  1095. rc = load_xattr_datum(c, xd);
  1096. if (unlikely(rc)) {
  1097. rc = (rc > 0) ? 0 : rc;
  1098. goto out;
  1099. }
  1100. old_ofs = ref_offset(xd->node);
  1101. totlen = PAD(sizeof(struct jffs2_raw_xattr)
  1102. + xd->name_len + 1 + xd->value_len);
  1103. rc = jffs2_reserve_space_gc(c, totlen, &length, JFFS2_SUMMARY_XATTR_SIZE);
  1104. if (rc) {
  1105. JFFS2_WARNING("jffs2_reserve_space_gc()=%d, request=%u\n", rc, totlen);
  1106. rc = rc ? rc : -EBADFD;
  1107. goto out;
  1108. }
  1109. rc = save_xattr_datum(c, xd);
  1110. if (!rc)
  1111. dbg_xattr("xdatum (xid=%u, version=%u) GC'ed from %#08x to %08x\n",
  1112. xd->xid, xd->version, old_ofs, ref_offset(xd->node));
  1113. out:
  1114. if (!rc)
  1115. jffs2_mark_node_obsolete(c, raw);
  1116. up_write(&c->xattr_sem);
  1117. return rc;
  1118. }
  1119. int jffs2_garbage_collect_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref,
  1120. struct jffs2_raw_node_ref *raw)
  1121. {
  1122. uint32_t totlen, length, old_ofs;
  1123. int rc = 0;
  1124. down_write(&c->xattr_sem);
  1125. BUG_ON(!ref->node);
  1126. if (ref->node != raw)
  1127. goto out;
  1128. if (is_xattr_ref_dead(ref) && (raw->next_in_ino == (void *)ref))
  1129. goto out;
  1130. old_ofs = ref_offset(ref->node);
  1131. totlen = ref_totlen(c, c->gcblock, ref->node);
  1132. rc = jffs2_reserve_space_gc(c, totlen, &length, JFFS2_SUMMARY_XREF_SIZE);
  1133. if (rc) {
  1134. JFFS2_WARNING("%s: jffs2_reserve_space_gc() = %d, request = %u\n",
  1135. __FUNCTION__, rc, totlen);
  1136. rc = rc ? rc : -EBADFD;
  1137. goto out;
  1138. }
  1139. rc = save_xattr_ref(c, ref);
  1140. if (!rc)
  1141. dbg_xattr("xref (ino=%u, xid=%u) GC'ed from %#08x to %08x\n",
  1142. ref->ic->ino, ref->xd->xid, old_ofs, ref_offset(ref->node));
  1143. out:
  1144. if (!rc)
  1145. jffs2_mark_node_obsolete(c, raw);
  1146. up_write(&c->xattr_sem);
  1147. return rc;
  1148. }
  1149. int jffs2_verify_xattr(struct jffs2_sb_info *c)
  1150. {
  1151. struct jffs2_xattr_datum *xd, *_xd;
  1152. struct jffs2_eraseblock *jeb;
  1153. struct jffs2_raw_node_ref *raw;
  1154. uint32_t totlen;
  1155. int rc;
  1156. down_write(&c->xattr_sem);
  1157. list_for_each_entry_safe(xd, _xd, &c->xattr_unchecked, xindex) {
  1158. rc = do_verify_xattr_datum(c, xd);
  1159. if (rc < 0)
  1160. continue;
  1161. list_del_init(&xd->xindex);
  1162. spin_lock(&c->erase_completion_lock);
  1163. for (raw=xd->node; raw != (void *)xd; raw=raw->next_in_ino) {
  1164. if (ref_flags(raw) != REF_UNCHECKED)
  1165. continue;
  1166. jeb = &c->blocks[ref_offset(raw) / c->sector_size];
  1167. totlen = PAD(ref_totlen(c, jeb, raw));
  1168. c->unchecked_size -= totlen; c->used_size += totlen;
  1169. jeb->unchecked_size -= totlen; jeb->used_size += totlen;
  1170. raw->flash_offset = ref_offset(raw)
  1171. | ((xd->node == (void *)raw) ? REF_PRISTINE : REF_NORMAL);
  1172. }
  1173. if (xd->flags & JFFS2_XFLAGS_DEAD)
  1174. list_add(&xd->xindex, &c->xattr_dead_list);
  1175. spin_unlock(&c->erase_completion_lock);
  1176. }
  1177. up_write(&c->xattr_sem);
  1178. return list_empty(&c->xattr_unchecked) ? 1 : 0;
  1179. }
  1180. void jffs2_release_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
  1181. {
  1182. /* must be called under spin_lock(&c->erase_completion_lock) */
  1183. if (xd->refcnt > 0 || xd->node != (void *)xd)
  1184. return;
  1185. list_del(&xd->xindex);
  1186. jffs2_free_xattr_datum(xd);
  1187. }
  1188. void jffs2_release_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
  1189. {
  1190. /* must be called under spin_lock(&c->erase_completion_lock) */
  1191. struct jffs2_xattr_ref *tmp, **ptmp;
  1192. if (ref->node != (void *)ref)
  1193. return;
  1194. for (tmp=c->xref_dead_list, ptmp=&c->xref_dead_list; tmp; ptmp=&tmp->next, tmp=tmp->next) {
  1195. if (ref == tmp) {
  1196. *ptmp = tmp->next;
  1197. break;
  1198. }
  1199. }
  1200. jffs2_free_xattr_ref(ref);
  1201. }