xattr.c 38 KB

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