xattr.c 38 KB

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