xattr.c 38 KB

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