write.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. /*
  2. * JFFS2 -- Journalling Flash File System, Version 2.
  3. *
  4. * Copyright (C) 2001-2003 Red Hat, Inc.
  5. *
  6. * Created by David Woodhouse <dwmw2@infradead.org>
  7. *
  8. * For licensing information, see the file 'LICENCE' in this directory.
  9. *
  10. * $Id: write.c,v 1.1.1.1 2007/06/12 07:27:13 eyryu Exp $
  11. *
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/fs.h>
  15. #include <linux/crc32.h>
  16. #include <linux/slab.h>
  17. #include <linux/pagemap.h>
  18. #include <linux/mtd/mtd.h>
  19. #include "nodelist.h"
  20. #include "compr.h"
  21. int jffs2_do_new_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f, uint32_t mode, struct jffs2_raw_inode *ri)
  22. {
  23. struct jffs2_inode_cache *ic;
  24. ic = jffs2_alloc_inode_cache();
  25. if (!ic) {
  26. return -ENOMEM;
  27. }
  28. memset(ic, 0, sizeof(*ic));
  29. f->inocache = ic;
  30. f->inocache->nlink = 1;
  31. f->inocache->nodes = (struct jffs2_raw_node_ref *)f->inocache;
  32. f->inocache->state = INO_STATE_PRESENT;
  33. jffs2_add_ino_cache(c, f->inocache);
  34. D1(printk(KERN_DEBUG "jffs2_do_new_inode(): Assigned ino# %d\n", f->inocache->ino));
  35. ri->ino = cpu_to_je32(f->inocache->ino);
  36. ri->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
  37. ri->nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
  38. ri->totlen = cpu_to_je32(PAD(sizeof(*ri)));
  39. ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
  40. ri->mode = cpu_to_jemode(mode);
  41. f->highest_version = 1;
  42. ri->version = cpu_to_je32(f->highest_version);
  43. return 0;
  44. }
  45. /* jffs2_write_dnode - given a raw_inode, allocate a full_dnode for it,
  46. write it to the flash, link it into the existing inode/fragment list */
  47. struct jffs2_full_dnode *jffs2_write_dnode(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
  48. struct jffs2_raw_inode *ri, const unsigned char *data,
  49. uint32_t datalen, int alloc_mode)
  50. {
  51. struct jffs2_full_dnode *fn;
  52. size_t retlen;
  53. uint32_t flash_ofs;
  54. struct kvec vecs[2];
  55. int ret;
  56. int retried = 0;
  57. unsigned long cnt = 2;
  58. D1(if(je32_to_cpu(ri->hdr_crc) != crc32(0, ri, sizeof(struct jffs2_unknown_node)-4)) {
  59. printk(KERN_CRIT "Eep. CRC not correct in jffs2_write_dnode()\n");
  60. BUG();
  61. }
  62. );
  63. vecs[0].iov_base = ri;
  64. vecs[0].iov_len = sizeof(*ri);
  65. vecs[1].iov_base = (unsigned char *)data;
  66. vecs[1].iov_len = datalen;
  67. if (je32_to_cpu(ri->totlen) != sizeof(*ri) + datalen) {
  68. printk(KERN_WARNING "jffs2_write_dnode: ri->totlen (0x%08x) != sizeof(*ri) (0x%08zx) + datalen (0x%08x)\n", je32_to_cpu(ri->totlen), sizeof(*ri), datalen);
  69. }
  70. fn = jffs2_alloc_full_dnode();
  71. if (!fn)
  72. return ERR_PTR(-ENOMEM);
  73. /* check number of valid vecs */
  74. if (!datalen || !data)
  75. cnt = 1;
  76. retry:
  77. flash_ofs = write_ofs(c);
  78. jffs2_dbg_prewrite_paranoia_check(c, flash_ofs, vecs[0].iov_len + vecs[1].iov_len);
  79. if ((alloc_mode!=ALLOC_GC) && (je32_to_cpu(ri->version) < f->highest_version)) {
  80. BUG_ON(!retried);
  81. D1(printk(KERN_DEBUG "jffs2_write_dnode : dnode_version %d, "
  82. "highest version %d -> updating dnode\n",
  83. je32_to_cpu(ri->version), f->highest_version));
  84. ri->version = cpu_to_je32(++f->highest_version);
  85. ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
  86. }
  87. ret = jffs2_flash_writev(c, vecs, cnt, flash_ofs, &retlen,
  88. (alloc_mode==ALLOC_GC)?0:f->inocache->ino);
  89. if (ret || (retlen != sizeof(*ri) + datalen)) {
  90. printk(KERN_NOTICE "Write of %zd bytes at 0x%08x failed. returned %d, retlen %zd\n",
  91. sizeof(*ri)+datalen, flash_ofs, ret, retlen);
  92. /* Mark the space as dirtied */
  93. if (retlen) {
  94. /* Don't change raw->size to match retlen. We may have
  95. written the node header already, and only the data will
  96. seem corrupted, in which case the scan would skip over
  97. any node we write before the original intended end of
  98. this node */
  99. jffs2_add_physical_node_ref(c, flash_ofs | REF_OBSOLETE, PAD(sizeof(*ri)+datalen), NULL);
  100. } else {
  101. printk(KERN_NOTICE "Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n", flash_ofs);
  102. }
  103. if (!retried && alloc_mode != ALLOC_NORETRY) {
  104. /* Try to reallocate space and retry */
  105. uint32_t dummy;
  106. struct jffs2_eraseblock *jeb = &c->blocks[flash_ofs / c->sector_size];
  107. retried = 1;
  108. D1(printk(KERN_DEBUG "Retrying failed write.\n"));
  109. jffs2_dbg_acct_sanity_check(c,jeb);
  110. jffs2_dbg_acct_paranoia_check(c, jeb);
  111. if (alloc_mode == ALLOC_GC) {
  112. ret = jffs2_reserve_space_gc(c, sizeof(*ri) + datalen, &dummy,
  113. JFFS2_SUMMARY_INODE_SIZE);
  114. } else {
  115. /* Locking pain */
  116. up(&f->sem);
  117. jffs2_complete_reservation(c);
  118. ret = jffs2_reserve_space(c, sizeof(*ri) + datalen, &dummy,
  119. alloc_mode, JFFS2_SUMMARY_INODE_SIZE);
  120. down(&f->sem);
  121. }
  122. if (!ret) {
  123. flash_ofs = write_ofs(c);
  124. D1(printk(KERN_DEBUG "Allocated space at 0x%08x to retry failed write.\n", flash_ofs));
  125. jffs2_dbg_acct_sanity_check(c,jeb);
  126. jffs2_dbg_acct_paranoia_check(c, jeb);
  127. goto retry;
  128. }
  129. D1(printk(KERN_DEBUG "Failed to allocate space to retry failed write: %d!\n", ret));
  130. }
  131. /* Release the full_dnode which is now useless, and return */
  132. jffs2_free_full_dnode(fn);
  133. return ERR_PTR(ret?ret:-EIO);
  134. }
  135. /* Mark the space used */
  136. /* If node covers at least a whole page, or if it starts at the
  137. beginning of a page and runs to the end of the file, or if
  138. it's a hole node, mark it REF_PRISTINE, else REF_NORMAL.
  139. */
  140. if ((je32_to_cpu(ri->dsize) >= PAGE_CACHE_SIZE) ||
  141. ( ((je32_to_cpu(ri->offset)&(PAGE_CACHE_SIZE-1))==0) &&
  142. (je32_to_cpu(ri->dsize)+je32_to_cpu(ri->offset) == je32_to_cpu(ri->isize)))) {
  143. flash_ofs |= REF_PRISTINE;
  144. } else {
  145. flash_ofs |= REF_NORMAL;
  146. }
  147. fn->raw = jffs2_add_physical_node_ref(c, flash_ofs, PAD(sizeof(*ri)+datalen), f->inocache);
  148. fn->ofs = je32_to_cpu(ri->offset);
  149. fn->size = je32_to_cpu(ri->dsize);
  150. fn->frags = 0;
  151. D1(printk(KERN_DEBUG "jffs2_write_dnode wrote node at 0x%08x(%d) with dsize 0x%x, csize 0x%x, node_crc 0x%08x, data_crc 0x%08x, totlen 0x%08x\n",
  152. flash_ofs & ~3, flash_ofs & 3, je32_to_cpu(ri->dsize),
  153. je32_to_cpu(ri->csize), je32_to_cpu(ri->node_crc),
  154. je32_to_cpu(ri->data_crc), je32_to_cpu(ri->totlen)));
  155. if (retried) {
  156. jffs2_dbg_acct_sanity_check(c,NULL);
  157. }
  158. return fn;
  159. }
  160. struct jffs2_full_dirent *jffs2_write_dirent(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
  161. struct jffs2_raw_dirent *rd, const unsigned char *name,
  162. uint32_t namelen, int alloc_mode)
  163. {
  164. struct jffs2_full_dirent *fd;
  165. size_t retlen;
  166. struct kvec vecs[2];
  167. uint32_t flash_ofs;
  168. int retried = 0;
  169. int ret;
  170. D1(printk(KERN_DEBUG "jffs2_write_dirent(ino #%u, name at *0x%p \"%s\"->ino #%u, name_crc 0x%08x)\n",
  171. je32_to_cpu(rd->pino), name, name, je32_to_cpu(rd->ino),
  172. je32_to_cpu(rd->name_crc)));
  173. D1(if(je32_to_cpu(rd->hdr_crc) != crc32(0, rd, sizeof(struct jffs2_unknown_node)-4)) {
  174. printk(KERN_CRIT "Eep. CRC not correct in jffs2_write_dirent()\n");
  175. BUG();
  176. });
  177. vecs[0].iov_base = rd;
  178. vecs[0].iov_len = sizeof(*rd);
  179. vecs[1].iov_base = (unsigned char *)name;
  180. vecs[1].iov_len = namelen;
  181. fd = jffs2_alloc_full_dirent(namelen+1);
  182. if (!fd)
  183. return ERR_PTR(-ENOMEM);
  184. fd->version = je32_to_cpu(rd->version);
  185. fd->ino = je32_to_cpu(rd->ino);
  186. fd->nhash = full_name_hash(name, strlen(name));
  187. fd->type = rd->type;
  188. memcpy(fd->name, name, namelen);
  189. fd->name[namelen]=0;
  190. retry:
  191. flash_ofs = write_ofs(c);
  192. jffs2_dbg_prewrite_paranoia_check(c, flash_ofs, vecs[0].iov_len + vecs[1].iov_len);
  193. if ((alloc_mode!=ALLOC_GC) && (je32_to_cpu(rd->version) < f->highest_version)) {
  194. BUG_ON(!retried);
  195. D1(printk(KERN_DEBUG "jffs2_write_dirent : dirent_version %d, "
  196. "highest version %d -> updating dirent\n",
  197. je32_to_cpu(rd->version), f->highest_version));
  198. rd->version = cpu_to_je32(++f->highest_version);
  199. fd->version = je32_to_cpu(rd->version);
  200. rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
  201. }
  202. ret = jffs2_flash_writev(c, vecs, 2, flash_ofs, &retlen,
  203. (alloc_mode==ALLOC_GC)?0:je32_to_cpu(rd->pino));
  204. if (ret || (retlen != sizeof(*rd) + namelen)) {
  205. printk(KERN_NOTICE "Write of %zd bytes at 0x%08x failed. returned %d, retlen %zd\n",
  206. sizeof(*rd)+namelen, flash_ofs, ret, retlen);
  207. /* Mark the space as dirtied */
  208. if (retlen) {
  209. jffs2_add_physical_node_ref(c, flash_ofs | REF_OBSOLETE, PAD(sizeof(*rd)+namelen), NULL);
  210. } else {
  211. printk(KERN_NOTICE "Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n", flash_ofs);
  212. }
  213. if (!retried) {
  214. /* Try to reallocate space and retry */
  215. uint32_t dummy;
  216. struct jffs2_eraseblock *jeb = &c->blocks[flash_ofs / c->sector_size];
  217. retried = 1;
  218. D1(printk(KERN_DEBUG "Retrying failed write.\n"));
  219. jffs2_dbg_acct_sanity_check(c,jeb);
  220. jffs2_dbg_acct_paranoia_check(c, jeb);
  221. if (alloc_mode == ALLOC_GC) {
  222. ret = jffs2_reserve_space_gc(c, sizeof(*rd) + namelen, &dummy,
  223. JFFS2_SUMMARY_DIRENT_SIZE(namelen));
  224. } else {
  225. /* Locking pain */
  226. up(&f->sem);
  227. jffs2_complete_reservation(c);
  228. ret = jffs2_reserve_space(c, sizeof(*rd) + namelen, &dummy,
  229. alloc_mode, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
  230. down(&f->sem);
  231. }
  232. if (!ret) {
  233. flash_ofs = write_ofs(c);
  234. D1(printk(KERN_DEBUG "Allocated space at 0x%08x to retry failed write.\n", flash_ofs));
  235. jffs2_dbg_acct_sanity_check(c,jeb);
  236. jffs2_dbg_acct_paranoia_check(c, jeb);
  237. goto retry;
  238. }
  239. D1(printk(KERN_DEBUG "Failed to allocate space to retry failed write: %d!\n", ret));
  240. }
  241. /* Release the full_dnode which is now useless, and return */
  242. jffs2_free_full_dirent(fd);
  243. return ERR_PTR(ret?ret:-EIO);
  244. }
  245. /* Mark the space used */
  246. fd->raw = jffs2_add_physical_node_ref(c, flash_ofs | REF_PRISTINE, PAD(sizeof(*rd)+namelen), f->inocache);
  247. if (retried) {
  248. jffs2_dbg_acct_sanity_check(c,NULL);
  249. }
  250. return fd;
  251. }
  252. /* The OS-specific code fills in the metadata in the jffs2_raw_inode for us, so that
  253. we don't have to go digging in struct inode or its equivalent. It should set:
  254. mode, uid, gid, (starting)isize, atime, ctime, mtime */
  255. int jffs2_write_inode_range(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
  256. struct jffs2_raw_inode *ri, unsigned char *buf,
  257. uint32_t offset, uint32_t writelen, uint32_t *retlen)
  258. {
  259. int ret = 0;
  260. uint32_t writtenlen = 0;
  261. D1(printk(KERN_DEBUG "jffs2_write_inode_range(): Ino #%u, ofs 0x%x, len 0x%x\n",
  262. f->inocache->ino, offset, writelen));
  263. while(writelen) {
  264. struct jffs2_full_dnode *fn;
  265. unsigned char *comprbuf = NULL;
  266. uint16_t comprtype = JFFS2_COMPR_NONE;
  267. uint32_t alloclen;
  268. uint32_t datalen, cdatalen;
  269. int retried = 0;
  270. retry:
  271. D2(printk(KERN_DEBUG "jffs2_commit_write() loop: 0x%x to write to 0x%x\n", writelen, offset));
  272. ret = jffs2_reserve_space(c, sizeof(*ri) + JFFS2_MIN_DATA_LEN,
  273. &alloclen, ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
  274. if (ret) {
  275. D1(printk(KERN_DEBUG "jffs2_reserve_space returned %d\n", ret));
  276. break;
  277. }
  278. down(&f->sem);
  279. datalen = min_t(uint32_t, writelen, PAGE_CACHE_SIZE - (offset & (PAGE_CACHE_SIZE-1)));
  280. cdatalen = min_t(uint32_t, alloclen - sizeof(*ri), datalen);
  281. comprtype = jffs2_compress(c, f, buf, &comprbuf, &datalen, &cdatalen);
  282. ri->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
  283. ri->nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
  284. ri->totlen = cpu_to_je32(sizeof(*ri) + cdatalen);
  285. ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
  286. ri->ino = cpu_to_je32(f->inocache->ino);
  287. ri->version = cpu_to_je32(++f->highest_version);
  288. ri->isize = cpu_to_je32(max(je32_to_cpu(ri->isize), offset + datalen));
  289. ri->offset = cpu_to_je32(offset);
  290. ri->csize = cpu_to_je32(cdatalen);
  291. ri->dsize = cpu_to_je32(datalen);
  292. ri->compr = comprtype & 0xff;
  293. ri->usercompr = (comprtype >> 8 ) & 0xff;
  294. ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
  295. ri->data_crc = cpu_to_je32(crc32(0, comprbuf, cdatalen));
  296. fn = jffs2_write_dnode(c, f, ri, comprbuf, cdatalen, ALLOC_NORETRY);
  297. jffs2_free_comprbuf(comprbuf, buf);
  298. if (IS_ERR(fn)) {
  299. ret = PTR_ERR(fn);
  300. up(&f->sem);
  301. jffs2_complete_reservation(c);
  302. if (!retried) {
  303. /* Write error to be retried */
  304. retried = 1;
  305. D1(printk(KERN_DEBUG "Retrying node write in jffs2_write_inode_range()\n"));
  306. goto retry;
  307. }
  308. break;
  309. }
  310. ret = jffs2_add_full_dnode_to_inode(c, f, fn);
  311. if (f->metadata) {
  312. jffs2_mark_node_obsolete(c, f->metadata->raw);
  313. jffs2_free_full_dnode(f->metadata);
  314. f->metadata = NULL;
  315. }
  316. if (ret) {
  317. /* Eep */
  318. D1(printk(KERN_DEBUG "Eep. add_full_dnode_to_inode() failed in commit_write, returned %d\n", ret));
  319. jffs2_mark_node_obsolete(c, fn->raw);
  320. jffs2_free_full_dnode(fn);
  321. up(&f->sem);
  322. jffs2_complete_reservation(c);
  323. break;
  324. }
  325. up(&f->sem);
  326. jffs2_complete_reservation(c);
  327. if (!datalen) {
  328. printk(KERN_WARNING "Eep. We didn't actually write any data in jffs2_write_inode_range()\n");
  329. ret = -EIO;
  330. break;
  331. }
  332. D1(printk(KERN_DEBUG "increasing writtenlen by %d\n", datalen));
  333. writtenlen += datalen;
  334. offset += datalen;
  335. writelen -= datalen;
  336. buf += datalen;
  337. }
  338. *retlen = writtenlen;
  339. return ret;
  340. }
  341. int jffs2_do_create(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f, struct jffs2_inode_info *f, struct jffs2_raw_inode *ri, const char *name, int namelen)
  342. {
  343. struct jffs2_raw_dirent *rd;
  344. struct jffs2_full_dnode *fn;
  345. struct jffs2_full_dirent *fd;
  346. uint32_t alloclen;
  347. int ret;
  348. /* Try to reserve enough space for both node and dirent.
  349. * Just the node will do for now, though
  350. */
  351. ret = jffs2_reserve_space(c, sizeof(*ri), &alloclen, ALLOC_NORMAL,
  352. JFFS2_SUMMARY_INODE_SIZE);
  353. D1(printk(KERN_DEBUG "jffs2_do_create(): reserved 0x%x bytes\n", alloclen));
  354. if (ret) {
  355. up(&f->sem);
  356. return ret;
  357. }
  358. ri->data_crc = cpu_to_je32(0);
  359. ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
  360. fn = jffs2_write_dnode(c, f, ri, NULL, 0, ALLOC_NORMAL);
  361. D1(printk(KERN_DEBUG "jffs2_do_create created file with mode 0x%x\n",
  362. jemode_to_cpu(ri->mode)));
  363. if (IS_ERR(fn)) {
  364. D1(printk(KERN_DEBUG "jffs2_write_dnode() failed\n"));
  365. /* Eeek. Wave bye bye */
  366. up(&f->sem);
  367. jffs2_complete_reservation(c);
  368. return PTR_ERR(fn);
  369. }
  370. /* No data here. Only a metadata node, which will be
  371. obsoleted by the first data write
  372. */
  373. f->metadata = fn;
  374. up(&f->sem);
  375. jffs2_complete_reservation(c);
  376. ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
  377. ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
  378. if (ret) {
  379. /* Eep. */
  380. D1(printk(KERN_DEBUG "jffs2_reserve_space() for dirent failed\n"));
  381. return ret;
  382. }
  383. rd = jffs2_alloc_raw_dirent();
  384. if (!rd) {
  385. /* Argh. Now we treat it like a normal delete */
  386. jffs2_complete_reservation(c);
  387. return -ENOMEM;
  388. }
  389. down(&dir_f->sem);
  390. rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
  391. rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
  392. rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
  393. rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
  394. rd->pino = cpu_to_je32(dir_f->inocache->ino);
  395. rd->version = cpu_to_je32(++dir_f->highest_version);
  396. rd->ino = ri->ino;
  397. rd->mctime = ri->ctime;
  398. rd->nsize = namelen;
  399. rd->type = DT_REG;
  400. rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
  401. rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
  402. fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, ALLOC_NORMAL);
  403. jffs2_free_raw_dirent(rd);
  404. if (IS_ERR(fd)) {
  405. /* dirent failed to write. Delete the inode normally
  406. as if it were the final unlink() */
  407. jffs2_complete_reservation(c);
  408. up(&dir_f->sem);
  409. return PTR_ERR(fd);
  410. }
  411. /* Link the fd into the inode's list, obsoleting an old
  412. one if necessary. */
  413. jffs2_add_fd_to_list(c, fd, &dir_f->dents);
  414. jffs2_complete_reservation(c);
  415. up(&dir_f->sem);
  416. return 0;
  417. }
  418. int jffs2_do_unlink(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f,
  419. const char *name, int namelen, struct jffs2_inode_info *dead_f,
  420. uint32_t time)
  421. {
  422. struct jffs2_raw_dirent *rd;
  423. struct jffs2_full_dirent *fd;
  424. uint32_t alloclen;
  425. int ret;
  426. if (1 /* alternative branch needs testing */ ||
  427. !jffs2_can_mark_obsolete(c)) {
  428. /* We can't mark stuff obsolete on the medium. We need to write a deletion dirent */
  429. rd = jffs2_alloc_raw_dirent();
  430. if (!rd)
  431. return -ENOMEM;
  432. ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
  433. ALLOC_DELETION, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
  434. if (ret) {
  435. jffs2_free_raw_dirent(rd);
  436. return ret;
  437. }
  438. down(&dir_f->sem);
  439. /* Build a deletion node */
  440. rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
  441. rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
  442. rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
  443. rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
  444. rd->pino = cpu_to_je32(dir_f->inocache->ino);
  445. rd->version = cpu_to_je32(++dir_f->highest_version);
  446. rd->ino = cpu_to_je32(0);
  447. rd->mctime = cpu_to_je32(time);
  448. rd->nsize = namelen;
  449. rd->type = DT_UNKNOWN;
  450. rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
  451. rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
  452. fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, ALLOC_DELETION);
  453. jffs2_free_raw_dirent(rd);
  454. if (IS_ERR(fd)) {
  455. jffs2_complete_reservation(c);
  456. up(&dir_f->sem);
  457. return PTR_ERR(fd);
  458. }
  459. /* File it. This will mark the old one obsolete. */
  460. jffs2_add_fd_to_list(c, fd, &dir_f->dents);
  461. up(&dir_f->sem);
  462. } else {
  463. struct jffs2_full_dirent **prev = &dir_f->dents;
  464. uint32_t nhash = full_name_hash(name, namelen);
  465. down(&dir_f->sem);
  466. while ((*prev) && (*prev)->nhash <= nhash) {
  467. if ((*prev)->nhash == nhash &&
  468. !memcmp((*prev)->name, name, namelen) &&
  469. !(*prev)->name[namelen]) {
  470. struct jffs2_full_dirent *this = *prev;
  471. D1(printk(KERN_DEBUG "Marking old dirent node (ino #%u) @%08x obsolete\n",
  472. this->ino, ref_offset(this->raw)));
  473. *prev = this->next;
  474. jffs2_mark_node_obsolete(c, (this->raw));
  475. jffs2_free_full_dirent(this);
  476. break;
  477. }
  478. prev = &((*prev)->next);
  479. }
  480. up(&dir_f->sem);
  481. }
  482. /* dead_f is NULL if this was a rename not a real unlink */
  483. /* Also catch the !f->inocache case, where there was a dirent
  484. pointing to an inode which didn't exist. */
  485. if (dead_f && dead_f->inocache) {
  486. down(&dead_f->sem);
  487. if (S_ISDIR(OFNI_EDONI_2SFFJ(dead_f)->i_mode)) {
  488. while (dead_f->dents) {
  489. /* There can be only deleted ones */
  490. fd = dead_f->dents;
  491. dead_f->dents = fd->next;
  492. if (fd->ino) {
  493. printk(KERN_WARNING "Deleting inode #%u with active dentry \"%s\"->ino #%u\n",
  494. dead_f->inocache->ino, fd->name, fd->ino);
  495. } else {
  496. D1(printk(KERN_DEBUG "Removing deletion dirent for \"%s\" from dir ino #%u\n",
  497. fd->name, dead_f->inocache->ino));
  498. }
  499. jffs2_mark_node_obsolete(c, fd->raw);
  500. jffs2_free_full_dirent(fd);
  501. }
  502. }
  503. dead_f->inocache->nlink--;
  504. /* NB: Caller must set inode nlink if appropriate */
  505. up(&dead_f->sem);
  506. }
  507. jffs2_complete_reservation(c);
  508. return 0;
  509. }
  510. int jffs2_do_link (struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f, uint32_t ino, uint8_t type, const char *name, int namelen, uint32_t time)
  511. {
  512. struct jffs2_raw_dirent *rd;
  513. struct jffs2_full_dirent *fd;
  514. uint32_t alloclen;
  515. int ret;
  516. rd = jffs2_alloc_raw_dirent();
  517. if (!rd)
  518. return -ENOMEM;
  519. ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
  520. ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
  521. if (ret) {
  522. jffs2_free_raw_dirent(rd);
  523. return ret;
  524. }
  525. down(&dir_f->sem);
  526. /* Build a deletion node */
  527. rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
  528. rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
  529. rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
  530. rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
  531. rd->pino = cpu_to_je32(dir_f->inocache->ino);
  532. rd->version = cpu_to_je32(++dir_f->highest_version);
  533. rd->ino = cpu_to_je32(ino);
  534. rd->mctime = cpu_to_je32(time);
  535. rd->nsize = namelen;
  536. rd->type = type;
  537. rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
  538. rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
  539. fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, ALLOC_NORMAL);
  540. jffs2_free_raw_dirent(rd);
  541. if (IS_ERR(fd)) {
  542. jffs2_complete_reservation(c);
  543. up(&dir_f->sem);
  544. return PTR_ERR(fd);
  545. }
  546. /* File it. This will mark the old one obsolete. */
  547. jffs2_add_fd_to_list(c, fd, &dir_f->dents);
  548. jffs2_complete_reservation(c);
  549. up(&dir_f->sem);
  550. return 0;
  551. }