xattr.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324
  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. * unrefer_xattr_datum(c, xd)
  54. * is used to delete a xdatum. When nobody refers this xdatum, JFFS2_XFLAGS_DEAD
  55. * is set on xd->flags and chained xattr_dead_list or release it immediately.
  56. * In the first case, the garbage collector release it later.
  57. * -------------------------------------------------- */
  58. static uint32_t xattr_datum_hashkey(int xprefix, const char *xname, const char *xvalue, int xsize)
  59. {
  60. int name_len = strlen(xname);
  61. return crc32(xprefix, xname, name_len) ^ crc32(xprefix, xvalue, xsize);
  62. }
  63. static int is_xattr_datum_unchecked(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
  64. {
  65. struct jffs2_raw_node_ref *raw;
  66. int rc = 0;
  67. spin_lock(&c->erase_completion_lock);
  68. for (raw=xd->node; raw != (void *)xd; raw=raw->next_in_ino) {
  69. if (ref_flags(raw) == REF_UNCHECKED) {
  70. rc = 1;
  71. break;
  72. }
  73. }
  74. spin_unlock(&c->erase_completion_lock);
  75. return rc;
  76. }
  77. static void unload_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
  78. {
  79. /* must be called under down_write(xattr_sem) */
  80. D1(dbg_xattr("%s: xid=%u, version=%u\n", __FUNCTION__, xd->xid, xd->version));
  81. if (xd->xname) {
  82. c->xdatum_mem_usage -= (xd->name_len + 1 + xd->value_len);
  83. kfree(xd->xname);
  84. }
  85. list_del_init(&xd->xindex);
  86. xd->hashkey = 0;
  87. xd->xname = NULL;
  88. xd->xvalue = NULL;
  89. }
  90. static void reclaim_xattr_datum(struct jffs2_sb_info *c)
  91. {
  92. /* must be called under down_write(xattr_sem) */
  93. struct jffs2_xattr_datum *xd, *_xd;
  94. uint32_t target, before;
  95. static int index = 0;
  96. int count;
  97. if (c->xdatum_mem_threshold > c->xdatum_mem_usage)
  98. return;
  99. before = c->xdatum_mem_usage;
  100. target = c->xdatum_mem_usage * 4 / 5; /* 20% reduction */
  101. for (count = 0; count < XATTRINDEX_HASHSIZE; count++) {
  102. list_for_each_entry_safe(xd, _xd, &c->xattrindex[index], xindex) {
  103. if (xd->flags & JFFS2_XFLAGS_HOT) {
  104. xd->flags &= ~JFFS2_XFLAGS_HOT;
  105. } else if (!(xd->flags & JFFS2_XFLAGS_BIND)) {
  106. unload_xattr_datum(c, xd);
  107. }
  108. if (c->xdatum_mem_usage <= target)
  109. goto out;
  110. }
  111. index = (index+1) % XATTRINDEX_HASHSIZE;
  112. }
  113. out:
  114. JFFS2_NOTICE("xdatum_mem_usage from %u byte to %u byte (%u byte reclaimed)\n",
  115. before, c->xdatum_mem_usage, before - c->xdatum_mem_usage);
  116. }
  117. static int do_verify_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
  118. {
  119. /* must be called under down_write(xattr_sem) */
  120. struct jffs2_eraseblock *jeb;
  121. struct jffs2_raw_node_ref *raw;
  122. struct jffs2_raw_xattr rx;
  123. size_t readlen;
  124. uint32_t crc, offset, totlen;
  125. int rc;
  126. spin_lock(&c->erase_completion_lock);
  127. offset = ref_offset(xd->node);
  128. if (ref_flags(xd->node) == REF_PRISTINE)
  129. goto complete;
  130. spin_unlock(&c->erase_completion_lock);
  131. rc = jffs2_flash_read(c, offset, sizeof(rx), &readlen, (char *)&rx);
  132. if (rc || readlen != sizeof(rx)) {
  133. JFFS2_WARNING("jffs2_flash_read()=%d, req=%zu, read=%zu at %#08x\n",
  134. rc, sizeof(rx), readlen, offset);
  135. return rc ? rc : -EIO;
  136. }
  137. crc = crc32(0, &rx, sizeof(rx) - 4);
  138. if (crc != je32_to_cpu(rx.node_crc)) {
  139. JFFS2_ERROR("node CRC failed at %#08x, read=%#08x, calc=%#08x\n",
  140. offset, je32_to_cpu(rx.hdr_crc), crc);
  141. xd->flags |= JFFS2_XFLAGS_INVALID;
  142. return EIO;
  143. }
  144. totlen = PAD(sizeof(rx) + rx.name_len + 1 + je16_to_cpu(rx.value_len));
  145. if (je16_to_cpu(rx.magic) != JFFS2_MAGIC_BITMASK
  146. || je16_to_cpu(rx.nodetype) != JFFS2_NODETYPE_XATTR
  147. || je32_to_cpu(rx.totlen) != totlen
  148. || je32_to_cpu(rx.xid) != xd->xid
  149. || je32_to_cpu(rx.version) != xd->version) {
  150. JFFS2_ERROR("inconsistent xdatum at %#08x, magic=%#04x/%#04x, "
  151. "nodetype=%#04x/%#04x, totlen=%u/%u, xid=%u/%u, version=%u/%u\n",
  152. offset, je16_to_cpu(rx.magic), JFFS2_MAGIC_BITMASK,
  153. je16_to_cpu(rx.nodetype), JFFS2_NODETYPE_XATTR,
  154. je32_to_cpu(rx.totlen), totlen,
  155. je32_to_cpu(rx.xid), xd->xid,
  156. je32_to_cpu(rx.version), xd->version);
  157. xd->flags |= JFFS2_XFLAGS_INVALID;
  158. return EIO;
  159. }
  160. xd->xprefix = rx.xprefix;
  161. xd->name_len = rx.name_len;
  162. xd->value_len = je16_to_cpu(rx.value_len);
  163. xd->data_crc = je32_to_cpu(rx.data_crc);
  164. spin_lock(&c->erase_completion_lock);
  165. complete:
  166. for (raw=xd->node; raw != (void *)xd; raw=raw->next_in_ino) {
  167. jeb = &c->blocks[ref_offset(raw) / c->sector_size];
  168. totlen = PAD(ref_totlen(c, jeb, raw));
  169. if (ref_flags(raw) == REF_UNCHECKED) {
  170. c->unchecked_size -= totlen; c->used_size += totlen;
  171. jeb->unchecked_size -= totlen; jeb->used_size += totlen;
  172. }
  173. raw->flash_offset = ref_offset(raw) | ((xd->node==raw) ? REF_PRISTINE : REF_NORMAL);
  174. }
  175. spin_unlock(&c->erase_completion_lock);
  176. /* unchecked xdatum is chained with c->xattr_unchecked */
  177. list_del_init(&xd->xindex);
  178. dbg_xattr("success on verfying xdatum (xid=%u, version=%u)\n",
  179. xd->xid, xd->version);
  180. return 0;
  181. }
  182. static int do_load_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
  183. {
  184. /* must be called under down_write(xattr_sem) */
  185. char *data;
  186. size_t readlen;
  187. uint32_t crc, length;
  188. int i, ret, retry = 0;
  189. BUG_ON(ref_flags(xd->node) != REF_PRISTINE);
  190. BUG_ON(!list_empty(&xd->xindex));
  191. retry:
  192. length = xd->name_len + 1 + xd->value_len;
  193. data = kmalloc(length, GFP_KERNEL);
  194. if (!data)
  195. return -ENOMEM;
  196. ret = jffs2_flash_read(c, ref_offset(xd->node)+sizeof(struct jffs2_raw_xattr),
  197. length, &readlen, data);
  198. if (ret || length!=readlen) {
  199. JFFS2_WARNING("jffs2_flash_read() returned %d, request=%d, readlen=%zu, at %#08x\n",
  200. ret, length, readlen, ref_offset(xd->node));
  201. kfree(data);
  202. return ret ? ret : -EIO;
  203. }
  204. data[xd->name_len] = '\0';
  205. crc = crc32(0, data, length);
  206. if (crc != xd->data_crc) {
  207. JFFS2_WARNING("node CRC failed (JFFS2_NODETYPE_XREF)"
  208. " at %#08x, read: 0x%08x calculated: 0x%08x\n",
  209. ref_offset(xd->node), xd->data_crc, crc);
  210. kfree(data);
  211. xd->flags |= JFFS2_XFLAGS_INVALID;
  212. return EIO;
  213. }
  214. xd->flags |= JFFS2_XFLAGS_HOT;
  215. xd->xname = data;
  216. xd->xvalue = data + xd->name_len+1;
  217. c->xdatum_mem_usage += length;
  218. xd->hashkey = xattr_datum_hashkey(xd->xprefix, xd->xname, xd->xvalue, xd->value_len);
  219. i = xd->hashkey % XATTRINDEX_HASHSIZE;
  220. list_add(&xd->xindex, &c->xattrindex[i]);
  221. if (!retry) {
  222. retry = 1;
  223. reclaim_xattr_datum(c);
  224. if (!xd->xname)
  225. goto retry;
  226. }
  227. dbg_xattr("success on loading xdatum (xid=%u, xprefix=%u, xname='%s')\n",
  228. xd->xid, xd->xprefix, xd->xname);
  229. return 0;
  230. }
  231. static int load_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
  232. {
  233. /* must be called under down_write(xattr_sem);
  234. * rc < 0 : recoverable error, try again
  235. * rc = 0 : success
  236. * rc > 0 : Unrecoverable error, this node should be deleted.
  237. */
  238. int rc = 0;
  239. BUG_ON(xd->flags & JFFS2_XFLAGS_DEAD);
  240. if (xd->xname)
  241. return 0;
  242. if (xd->flags & JFFS2_XFLAGS_INVALID)
  243. return EIO;
  244. if (unlikely(is_xattr_datum_unchecked(c, xd)))
  245. rc = do_verify_xattr_datum(c, xd);
  246. if (!rc)
  247. rc = do_load_xattr_datum(c, xd);
  248. return rc;
  249. }
  250. static int save_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
  251. {
  252. /* must be called under down_write(xattr_sem) */
  253. struct jffs2_raw_xattr rx;
  254. struct kvec vecs[2];
  255. size_t length;
  256. int rc, totlen;
  257. uint32_t phys_ofs = write_ofs(c);
  258. BUG_ON(!xd->xname);
  259. BUG_ON(xd->flags & (JFFS2_XFLAGS_DEAD|JFFS2_XFLAGS_INVALID));
  260. vecs[0].iov_base = &rx;
  261. vecs[0].iov_len = sizeof(rx);
  262. vecs[1].iov_base = xd->xname;
  263. vecs[1].iov_len = xd->name_len + 1 + xd->value_len;
  264. totlen = vecs[0].iov_len + vecs[1].iov_len;
  265. /* Setup raw-xattr */
  266. memset(&rx, 0, sizeof(rx));
  267. rx.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
  268. rx.nodetype = cpu_to_je16(JFFS2_NODETYPE_XATTR);
  269. rx.totlen = cpu_to_je32(PAD(totlen));
  270. rx.hdr_crc = cpu_to_je32(crc32(0, &rx, sizeof(struct jffs2_unknown_node) - 4));
  271. rx.xid = cpu_to_je32(xd->xid);
  272. rx.version = cpu_to_je32(++xd->version);
  273. rx.xprefix = xd->xprefix;
  274. rx.name_len = xd->name_len;
  275. rx.value_len = cpu_to_je16(xd->value_len);
  276. rx.data_crc = cpu_to_je32(crc32(0, vecs[1].iov_base, vecs[1].iov_len));
  277. rx.node_crc = cpu_to_je32(crc32(0, &rx, sizeof(struct jffs2_raw_xattr) - 4));
  278. rc = jffs2_flash_writev(c, vecs, 2, phys_ofs, &length, 0);
  279. if (rc || totlen != length) {
  280. JFFS2_WARNING("jffs2_flash_writev()=%d, req=%u, wrote=%zu, at %#08x\n",
  281. rc, totlen, length, phys_ofs);
  282. rc = rc ? rc : -EIO;
  283. if (length)
  284. jffs2_add_physical_node_ref(c, phys_ofs | REF_OBSOLETE, PAD(totlen), NULL);
  285. return rc;
  286. }
  287. /* success */
  288. jffs2_add_physical_node_ref(c, phys_ofs | REF_PRISTINE, PAD(totlen), (void *)xd);
  289. dbg_xattr("success on saving xdatum (xid=%u, version=%u, xprefix=%u, xname='%s')\n",
  290. xd->xid, xd->version, xd->xprefix, xd->xname);
  291. return 0;
  292. }
  293. static struct jffs2_xattr_datum *create_xattr_datum(struct jffs2_sb_info *c,
  294. int xprefix, const char *xname,
  295. const char *xvalue, int xsize)
  296. {
  297. /* must be called under down_write(xattr_sem) */
  298. struct jffs2_xattr_datum *xd;
  299. uint32_t hashkey, name_len;
  300. char *data;
  301. int i, rc;
  302. /* Search xattr_datum has same xname/xvalue by index */
  303. hashkey = xattr_datum_hashkey(xprefix, xname, xvalue, xsize);
  304. i = hashkey % XATTRINDEX_HASHSIZE;
  305. list_for_each_entry(xd, &c->xattrindex[i], xindex) {
  306. if (xd->hashkey==hashkey
  307. && xd->xprefix==xprefix
  308. && xd->value_len==xsize
  309. && !strcmp(xd->xname, xname)
  310. && !memcmp(xd->xvalue, xvalue, xsize)) {
  311. atomic_inc(&xd->refcnt);
  312. return xd;
  313. }
  314. }
  315. /* Not found, Create NEW XATTR-Cache */
  316. name_len = strlen(xname);
  317. xd = jffs2_alloc_xattr_datum();
  318. if (!xd)
  319. return ERR_PTR(-ENOMEM);
  320. data = kmalloc(name_len + 1 + xsize, GFP_KERNEL);
  321. if (!data) {
  322. jffs2_free_xattr_datum(xd);
  323. return ERR_PTR(-ENOMEM);
  324. }
  325. strcpy(data, xname);
  326. memcpy(data + name_len + 1, xvalue, xsize);
  327. atomic_set(&xd->refcnt, 1);
  328. xd->xid = ++c->highest_xid;
  329. xd->flags |= JFFS2_XFLAGS_HOT;
  330. xd->xprefix = xprefix;
  331. xd->hashkey = hashkey;
  332. xd->xname = data;
  333. xd->xvalue = data + name_len + 1;
  334. xd->name_len = name_len;
  335. xd->value_len = xsize;
  336. xd->data_crc = crc32(0, data, xd->name_len + 1 + xd->value_len);
  337. rc = save_xattr_datum(c, xd);
  338. if (rc) {
  339. kfree(xd->xname);
  340. jffs2_free_xattr_datum(xd);
  341. return ERR_PTR(rc);
  342. }
  343. /* Insert Hash Index */
  344. i = hashkey % XATTRINDEX_HASHSIZE;
  345. list_add(&xd->xindex, &c->xattrindex[i]);
  346. c->xdatum_mem_usage += (xd->name_len + 1 + xd->value_len);
  347. reclaim_xattr_datum(c);
  348. return xd;
  349. }
  350. static void unrefer_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
  351. {
  352. /* must be called under down_write(xattr_sem) */
  353. if (atomic_dec_and_lock(&xd->refcnt, &c->erase_completion_lock)) {
  354. uint32_t xid = xd->xid, version = xd->version;
  355. unload_xattr_datum(c, xd);
  356. xd->flags |= JFFS2_XFLAGS_DEAD;
  357. if (xd->node == (void *)xd) {
  358. BUG_ON(!(xd->flags & JFFS2_XFLAGS_INVALID));
  359. jffs2_free_xattr_datum(xd);
  360. } else {
  361. list_add(&xd->xindex, &c->xattr_dead_list);
  362. }
  363. spin_unlock(&c->erase_completion_lock);
  364. dbg_xattr("xdatum(xid=%u, version=%u) was removed.\n", xid, version);
  365. }
  366. }
  367. /* -------- xref related functions ------------------
  368. * verify_xattr_ref(c, ref)
  369. * is used to load xref information from medium. Because summary data does not
  370. * contain xid/ino, it's necessary to verify once while mounting process.
  371. * save_xattr_ref(c, ref)
  372. * is used to write xref to medium. If delete marker is marked, it write
  373. * a delete marker of xref into medium.
  374. * create_xattr_ref(c, ic, xd)
  375. * is used to create a new xref and write to medium.
  376. * delete_xattr_ref(c, ref)
  377. * is used to delete jffs2_xattr_ref. It marks xref XREF_DELETE_MARKER,
  378. * and allows GC to reclaim those physical nodes.
  379. * jffs2_xattr_delete_inode(c, ic)
  380. * is called to remove xrefs related to obsolete inode when inode is unlinked.
  381. * jffs2_xattr_free_inode(c, ic)
  382. * is called to release xattr related objects when unmounting.
  383. * check_xattr_ref_inode(c, ic)
  384. * is used to confirm inode does not have duplicate xattr name/value pair.
  385. * -------------------------------------------------- */
  386. static int verify_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
  387. {
  388. struct jffs2_eraseblock *jeb;
  389. struct jffs2_raw_node_ref *raw;
  390. struct jffs2_raw_xref rr;
  391. size_t readlen;
  392. uint32_t crc, offset, totlen;
  393. int rc;
  394. spin_lock(&c->erase_completion_lock);
  395. if (ref_flags(ref->node) != REF_UNCHECKED)
  396. goto complete;
  397. offset = ref_offset(ref->node);
  398. spin_unlock(&c->erase_completion_lock);
  399. rc = jffs2_flash_read(c, offset, sizeof(rr), &readlen, (char *)&rr);
  400. if (rc || sizeof(rr) != readlen) {
  401. JFFS2_WARNING("jffs2_flash_read()=%d, req=%zu, read=%zu, at %#08x\n",
  402. rc, sizeof(rr), readlen, offset);
  403. return rc ? rc : -EIO;
  404. }
  405. /* obsolete node */
  406. crc = crc32(0, &rr, sizeof(rr) - 4);
  407. if (crc != je32_to_cpu(rr.node_crc)) {
  408. JFFS2_ERROR("node CRC failed at %#08x, read=%#08x, calc=%#08x\n",
  409. offset, je32_to_cpu(rr.node_crc), crc);
  410. return EIO;
  411. }
  412. if (je16_to_cpu(rr.magic) != JFFS2_MAGIC_BITMASK
  413. || je16_to_cpu(rr.nodetype) != JFFS2_NODETYPE_XREF
  414. || je32_to_cpu(rr.totlen) != PAD(sizeof(rr))) {
  415. JFFS2_ERROR("inconsistent xref at %#08x, magic=%#04x/%#04x, "
  416. "nodetype=%#04x/%#04x, totlen=%u/%zu\n",
  417. offset, je16_to_cpu(rr.magic), JFFS2_MAGIC_BITMASK,
  418. je16_to_cpu(rr.nodetype), JFFS2_NODETYPE_XREF,
  419. je32_to_cpu(rr.totlen), PAD(sizeof(rr)));
  420. return EIO;
  421. }
  422. ref->ino = je32_to_cpu(rr.ino);
  423. ref->xid = je32_to_cpu(rr.xid);
  424. ref->xseqno = je32_to_cpu(rr.xseqno);
  425. if (ref->xseqno > c->highest_xseqno)
  426. c->highest_xseqno = (ref->xseqno & ~XREF_DELETE_MARKER);
  427. spin_lock(&c->erase_completion_lock);
  428. complete:
  429. for (raw=ref->node; raw != (void *)ref; raw=raw->next_in_ino) {
  430. jeb = &c->blocks[ref_offset(raw) / c->sector_size];
  431. totlen = PAD(ref_totlen(c, jeb, raw));
  432. if (ref_flags(raw) == REF_UNCHECKED) {
  433. c->unchecked_size -= totlen; c->used_size += totlen;
  434. jeb->unchecked_size -= totlen; jeb->used_size += totlen;
  435. }
  436. raw->flash_offset = ref_offset(raw) | ((ref->node==raw) ? REF_PRISTINE : REF_NORMAL);
  437. }
  438. spin_unlock(&c->erase_completion_lock);
  439. dbg_xattr("success on verifying xref (ino=%u, xid=%u) at %#08x\n",
  440. ref->ino, ref->xid, ref_offset(ref->node));
  441. return 0;
  442. }
  443. static int save_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
  444. {
  445. /* must be called under down_write(xattr_sem) */
  446. struct jffs2_raw_xref rr;
  447. size_t length;
  448. uint32_t xseqno, phys_ofs = write_ofs(c);
  449. int ret;
  450. rr.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
  451. rr.nodetype = cpu_to_je16(JFFS2_NODETYPE_XREF);
  452. rr.totlen = cpu_to_je32(PAD(sizeof(rr)));
  453. rr.hdr_crc = cpu_to_je32(crc32(0, &rr, sizeof(struct jffs2_unknown_node) - 4));
  454. xseqno = (c->highest_xseqno += 2);
  455. if (is_xattr_ref_dead(ref)) {
  456. xseqno |= XREF_DELETE_MARKER;
  457. rr.ino = cpu_to_je32(ref->ino);
  458. rr.xid = cpu_to_je32(ref->xid);
  459. } else {
  460. rr.ino = cpu_to_je32(ref->ic->ino);
  461. rr.xid = cpu_to_je32(ref->xd->xid);
  462. }
  463. rr.xseqno = cpu_to_je32(xseqno);
  464. rr.node_crc = cpu_to_je32(crc32(0, &rr, sizeof(rr) - 4));
  465. ret = jffs2_flash_write(c, phys_ofs, sizeof(rr), &length, (char *)&rr);
  466. if (ret || sizeof(rr) != length) {
  467. JFFS2_WARNING("jffs2_flash_write() returned %d, request=%zu, retlen=%zu, at %#08x\n",
  468. ret, sizeof(rr), length, phys_ofs);
  469. ret = ret ? ret : -EIO;
  470. if (length)
  471. jffs2_add_physical_node_ref(c, phys_ofs | REF_OBSOLETE, PAD(sizeof(rr)), NULL);
  472. return ret;
  473. }
  474. /* success */
  475. ref->xseqno = xseqno;
  476. jffs2_add_physical_node_ref(c, phys_ofs | REF_PRISTINE, PAD(sizeof(rr)), (void *)ref);
  477. dbg_xattr("success on saving xref (ino=%u, xid=%u)\n", ref->ic->ino, ref->xd->xid);
  478. return 0;
  479. }
  480. static struct jffs2_xattr_ref *create_xattr_ref(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic,
  481. struct jffs2_xattr_datum *xd)
  482. {
  483. /* must be called under down_write(xattr_sem) */
  484. struct jffs2_xattr_ref *ref;
  485. int ret;
  486. ref = jffs2_alloc_xattr_ref();
  487. if (!ref)
  488. return ERR_PTR(-ENOMEM);
  489. ref->ic = ic;
  490. ref->xd = xd;
  491. ret = save_xattr_ref(c, ref);
  492. if (ret) {
  493. jffs2_free_xattr_ref(ref);
  494. return ERR_PTR(ret);
  495. }
  496. /* Chain to inode */
  497. ref->next = ic->xref;
  498. ic->xref = ref;
  499. return ref; /* success */
  500. }
  501. static void delete_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
  502. {
  503. /* must be called under down_write(xattr_sem) */
  504. struct jffs2_xattr_datum *xd;
  505. xd = ref->xd;
  506. ref->xseqno |= XREF_DELETE_MARKER;
  507. ref->ino = ref->ic->ino;
  508. ref->xid = ref->xd->xid;
  509. spin_lock(&c->erase_completion_lock);
  510. ref->next = c->xref_dead_list;
  511. c->xref_dead_list = ref;
  512. spin_unlock(&c->erase_completion_lock);
  513. dbg_xattr("xref(ino=%u, xid=%u, xseqno=%u) was removed.\n",
  514. ref->ino, ref->xid, ref->xseqno);
  515. unrefer_xattr_datum(c, xd);
  516. }
  517. void jffs2_xattr_delete_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
  518. {
  519. /* It's called from jffs2_clear_inode() on inode removing.
  520. When an inode with XATTR is removed, those XATTRs must be removed. */
  521. struct jffs2_xattr_ref *ref, *_ref;
  522. if (!ic || ic->nlink > 0)
  523. return;
  524. down_write(&c->xattr_sem);
  525. for (ref = ic->xref; ref; ref = _ref) {
  526. _ref = ref->next;
  527. delete_xattr_ref(c, ref);
  528. }
  529. ic->xref = NULL;
  530. up_write(&c->xattr_sem);
  531. }
  532. void jffs2_xattr_free_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
  533. {
  534. /* It's called from jffs2_free_ino_caches() until unmounting FS. */
  535. struct jffs2_xattr_datum *xd;
  536. struct jffs2_xattr_ref *ref, *_ref;
  537. down_write(&c->xattr_sem);
  538. for (ref = ic->xref; ref; ref = _ref) {
  539. _ref = ref->next;
  540. xd = ref->xd;
  541. if (atomic_dec_and_test(&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. atomic_inc(&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 (!atomic_read(&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. unrefer_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. unrefer_xattr_datum(c, xd);
  1047. up_write(&c->xattr_sem);
  1048. return rc;
  1049. }
  1050. if (ref)
  1051. *pref = ref->next;
  1052. newref = create_xattr_ref(c, ic, xd);
  1053. if (IS_ERR(newref)) {
  1054. if (ref) {
  1055. ref->next = ic->xref;
  1056. ic->xref = ref;
  1057. }
  1058. rc = PTR_ERR(newref);
  1059. unrefer_xattr_datum(c, xd);
  1060. } else if (ref) {
  1061. delete_xattr_ref(c, ref);
  1062. }
  1063. out:
  1064. up_write(&c->xattr_sem);
  1065. jffs2_complete_reservation(c);
  1066. return rc;
  1067. }
  1068. /* -------- garbage collector functions -------------
  1069. * jffs2_garbage_collect_xattr_datum(c, xd, raw)
  1070. * is used to move xdatum into new node.
  1071. * jffs2_garbage_collect_xattr_ref(c, ref, raw)
  1072. * is used to move xref into new node.
  1073. * jffs2_verify_xattr(c)
  1074. * is used to call do_verify_xattr_datum() before garbage collecting.
  1075. * jffs2_release_xattr_datum(c, xd)
  1076. * is used to release an in-memory object of xdatum.
  1077. * jffs2_release_xattr_ref(c, ref)
  1078. * is used to release an in-memory object of xref.
  1079. * -------------------------------------------------- */
  1080. int jffs2_garbage_collect_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd,
  1081. struct jffs2_raw_node_ref *raw)
  1082. {
  1083. uint32_t totlen, length, old_ofs;
  1084. int rc = 0;
  1085. down_write(&c->xattr_sem);
  1086. if (xd->node != raw)
  1087. goto out;
  1088. if (xd->flags & (JFFS2_XFLAGS_DEAD|JFFS2_XFLAGS_INVALID))
  1089. goto out;
  1090. rc = load_xattr_datum(c, xd);
  1091. if (unlikely(rc)) {
  1092. rc = (rc > 0) ? 0 : rc;
  1093. goto out;
  1094. }
  1095. old_ofs = ref_offset(xd->node);
  1096. totlen = PAD(sizeof(struct jffs2_raw_xattr)
  1097. + xd->name_len + 1 + xd->value_len);
  1098. rc = jffs2_reserve_space_gc(c, totlen, &length, JFFS2_SUMMARY_XATTR_SIZE);
  1099. if (rc) {
  1100. JFFS2_WARNING("jffs2_reserve_space_gc()=%d, request=%u\n", rc, totlen);
  1101. goto out;
  1102. }
  1103. rc = save_xattr_datum(c, xd);
  1104. if (!rc)
  1105. dbg_xattr("xdatum (xid=%u, version=%u) GC'ed from %#08x to %08x\n",
  1106. xd->xid, xd->version, old_ofs, ref_offset(xd->node));
  1107. out:
  1108. if (!rc)
  1109. jffs2_mark_node_obsolete(c, raw);
  1110. up_write(&c->xattr_sem);
  1111. return rc;
  1112. }
  1113. int jffs2_garbage_collect_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref,
  1114. struct jffs2_raw_node_ref *raw)
  1115. {
  1116. uint32_t totlen, length, old_ofs;
  1117. int rc = 0;
  1118. down_write(&c->xattr_sem);
  1119. BUG_ON(!ref->node);
  1120. if (ref->node != raw)
  1121. goto out;
  1122. if (is_xattr_ref_dead(ref) && (raw->next_in_ino == (void *)ref))
  1123. goto out;
  1124. old_ofs = ref_offset(ref->node);
  1125. totlen = ref_totlen(c, c->gcblock, ref->node);
  1126. rc = jffs2_reserve_space_gc(c, totlen, &length, JFFS2_SUMMARY_XREF_SIZE);
  1127. if (rc) {
  1128. JFFS2_WARNING("%s: jffs2_reserve_space_gc() = %d, request = %u\n",
  1129. __FUNCTION__, rc, totlen);
  1130. rc = rc ? rc : -EBADFD;
  1131. goto out;
  1132. }
  1133. rc = save_xattr_ref(c, ref);
  1134. if (!rc)
  1135. dbg_xattr("xref (ino=%u, xid=%u) GC'ed from %#08x to %08x\n",
  1136. ref->ic->ino, ref->xd->xid, old_ofs, ref_offset(ref->node));
  1137. out:
  1138. if (!rc)
  1139. jffs2_mark_node_obsolete(c, raw);
  1140. up_write(&c->xattr_sem);
  1141. return rc;
  1142. }
  1143. int jffs2_verify_xattr(struct jffs2_sb_info *c)
  1144. {
  1145. struct jffs2_xattr_datum *xd, *_xd;
  1146. struct jffs2_eraseblock *jeb;
  1147. struct jffs2_raw_node_ref *raw;
  1148. uint32_t totlen;
  1149. int rc;
  1150. down_write(&c->xattr_sem);
  1151. list_for_each_entry_safe(xd, _xd, &c->xattr_unchecked, xindex) {
  1152. rc = do_verify_xattr_datum(c, xd);
  1153. if (rc < 0)
  1154. continue;
  1155. list_del_init(&xd->xindex);
  1156. spin_lock(&c->erase_completion_lock);
  1157. for (raw=xd->node; raw != (void *)xd; raw=raw->next_in_ino) {
  1158. if (ref_flags(raw) != REF_UNCHECKED)
  1159. continue;
  1160. jeb = &c->blocks[ref_offset(raw) / c->sector_size];
  1161. totlen = PAD(ref_totlen(c, jeb, raw));
  1162. c->unchecked_size -= totlen; c->used_size += totlen;
  1163. jeb->unchecked_size -= totlen; jeb->used_size += totlen;
  1164. raw->flash_offset = ref_offset(raw)
  1165. | ((xd->node == (void *)raw) ? REF_PRISTINE : REF_NORMAL);
  1166. }
  1167. if (xd->flags & JFFS2_XFLAGS_DEAD)
  1168. list_add(&xd->xindex, &c->xattr_dead_list);
  1169. spin_unlock(&c->erase_completion_lock);
  1170. }
  1171. up_write(&c->xattr_sem);
  1172. return list_empty(&c->xattr_unchecked) ? 1 : 0;
  1173. }
  1174. void jffs2_release_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
  1175. {
  1176. /* must be called under spin_lock(&c->erase_completion_lock) */
  1177. if (atomic_read(&xd->refcnt) || xd->node != (void *)xd)
  1178. return;
  1179. list_del(&xd->xindex);
  1180. jffs2_free_xattr_datum(xd);
  1181. }
  1182. void jffs2_release_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
  1183. {
  1184. /* must be called under spin_lock(&c->erase_completion_lock) */
  1185. struct jffs2_xattr_ref *tmp, **ptmp;
  1186. if (ref->node != (void *)ref)
  1187. return;
  1188. for (tmp=c->xref_dead_list, ptmp=&c->xref_dead_list; tmp; ptmp=&tmp->next, tmp=tmp->next) {
  1189. if (ref == tmp) {
  1190. *ptmp = tmp->next;
  1191. break;
  1192. }
  1193. }
  1194. jffs2_free_xattr_ref(ref);
  1195. }