xattr.c 38 KB

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