xattr.c 37 KB

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