xattr.c 36 KB

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