xattr.c 38 KB

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