epca.c: reformat comments and coding style improvements
[linux/fpc-iii.git] / fs / jffs2 / write.c
blob2f5695446d0f56eb92d0e2b8f26e90a7ed2938c3
1 /*
2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright © 2001-2007 Red Hat, Inc.
6 * Created by David Woodhouse <dwmw2@infradead.org>
8 * For licensing information, see the file 'LICENCE' in this directory.
12 #include <linux/kernel.h>
13 #include <linux/fs.h>
14 #include <linux/crc32.h>
15 #include <linux/slab.h>
16 #include <linux/pagemap.h>
17 #include <linux/mtd/mtd.h>
18 #include "nodelist.h"
19 #include "compr.h"
22 int jffs2_do_new_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f, uint32_t mode, struct jffs2_raw_inode *ri)
24 struct jffs2_inode_cache *ic;
26 ic = jffs2_alloc_inode_cache();
27 if (!ic) {
28 return -ENOMEM;
31 memset(ic, 0, sizeof(*ic));
33 f->inocache = ic;
34 f->inocache->nlink = 1;
35 f->inocache->nodes = (struct jffs2_raw_node_ref *)f->inocache;
36 f->inocache->state = INO_STATE_PRESENT;
38 jffs2_add_ino_cache(c, f->inocache);
39 D1(printk(KERN_DEBUG "jffs2_do_new_inode(): Assigned ino# %d\n", f->inocache->ino));
40 ri->ino = cpu_to_je32(f->inocache->ino);
42 ri->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
43 ri->nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
44 ri->totlen = cpu_to_je32(PAD(sizeof(*ri)));
45 ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
46 ri->mode = cpu_to_jemode(mode);
48 f->highest_version = 1;
49 ri->version = cpu_to_je32(f->highest_version);
51 return 0;
54 /* jffs2_write_dnode - given a raw_inode, allocate a full_dnode for it,
55 write it to the flash, link it into the existing inode/fragment list */
57 struct jffs2_full_dnode *jffs2_write_dnode(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
58 struct jffs2_raw_inode *ri, const unsigned char *data,
59 uint32_t datalen, int alloc_mode)
62 struct jffs2_full_dnode *fn;
63 size_t retlen;
64 uint32_t flash_ofs;
65 struct kvec vecs[2];
66 int ret;
67 int retried = 0;
68 unsigned long cnt = 2;
70 D1(if(je32_to_cpu(ri->hdr_crc) != crc32(0, ri, sizeof(struct jffs2_unknown_node)-4)) {
71 printk(KERN_CRIT "Eep. CRC not correct in jffs2_write_dnode()\n");
72 BUG();
75 vecs[0].iov_base = ri;
76 vecs[0].iov_len = sizeof(*ri);
77 vecs[1].iov_base = (unsigned char *)data;
78 vecs[1].iov_len = datalen;
80 if (je32_to_cpu(ri->totlen) != sizeof(*ri) + datalen) {
81 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);
84 fn = jffs2_alloc_full_dnode();
85 if (!fn)
86 return ERR_PTR(-ENOMEM);
88 /* check number of valid vecs */
89 if (!datalen || !data)
90 cnt = 1;
91 retry:
92 flash_ofs = write_ofs(c);
94 jffs2_dbg_prewrite_paranoia_check(c, flash_ofs, vecs[0].iov_len + vecs[1].iov_len);
96 if ((alloc_mode!=ALLOC_GC) && (je32_to_cpu(ri->version) < f->highest_version)) {
97 BUG_ON(!retried);
98 D1(printk(KERN_DEBUG "jffs2_write_dnode : dnode_version %d, "
99 "highest version %d -> updating dnode\n",
100 je32_to_cpu(ri->version), f->highest_version));
101 ri->version = cpu_to_je32(++f->highest_version);
102 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
105 ret = jffs2_flash_writev(c, vecs, cnt, flash_ofs, &retlen,
106 (alloc_mode==ALLOC_GC)?0:f->inocache->ino);
108 if (ret || (retlen != sizeof(*ri) + datalen)) {
109 printk(KERN_NOTICE "Write of %zd bytes at 0x%08x failed. returned %d, retlen %zd\n",
110 sizeof(*ri)+datalen, flash_ofs, ret, retlen);
112 /* Mark the space as dirtied */
113 if (retlen) {
114 /* Don't change raw->size to match retlen. We may have
115 written the node header already, and only the data will
116 seem corrupted, in which case the scan would skip over
117 any node we write before the original intended end of
118 this node */
119 jffs2_add_physical_node_ref(c, flash_ofs | REF_OBSOLETE, PAD(sizeof(*ri)+datalen), NULL);
120 } else {
121 printk(KERN_NOTICE "Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n", flash_ofs);
123 if (!retried && alloc_mode != ALLOC_NORETRY) {
124 /* Try to reallocate space and retry */
125 uint32_t dummy;
126 struct jffs2_eraseblock *jeb = &c->blocks[flash_ofs / c->sector_size];
128 retried = 1;
130 D1(printk(KERN_DEBUG "Retrying failed write.\n"));
132 jffs2_dbg_acct_sanity_check(c,jeb);
133 jffs2_dbg_acct_paranoia_check(c, jeb);
135 if (alloc_mode == ALLOC_GC) {
136 ret = jffs2_reserve_space_gc(c, sizeof(*ri) + datalen, &dummy,
137 JFFS2_SUMMARY_INODE_SIZE);
138 } else {
139 /* Locking pain */
140 up(&f->sem);
141 jffs2_complete_reservation(c);
143 ret = jffs2_reserve_space(c, sizeof(*ri) + datalen, &dummy,
144 alloc_mode, JFFS2_SUMMARY_INODE_SIZE);
145 down(&f->sem);
148 if (!ret) {
149 flash_ofs = write_ofs(c);
150 D1(printk(KERN_DEBUG "Allocated space at 0x%08x to retry failed write.\n", flash_ofs));
152 jffs2_dbg_acct_sanity_check(c,jeb);
153 jffs2_dbg_acct_paranoia_check(c, jeb);
155 goto retry;
157 D1(printk(KERN_DEBUG "Failed to allocate space to retry failed write: %d!\n", ret));
159 /* Release the full_dnode which is now useless, and return */
160 jffs2_free_full_dnode(fn);
161 return ERR_PTR(ret?ret:-EIO);
163 /* Mark the space used */
164 /* If node covers at least a whole page, or if it starts at the
165 beginning of a page and runs to the end of the file, or if
166 it's a hole node, mark it REF_PRISTINE, else REF_NORMAL.
168 if ((je32_to_cpu(ri->dsize) >= PAGE_CACHE_SIZE) ||
169 ( ((je32_to_cpu(ri->offset)&(PAGE_CACHE_SIZE-1))==0) &&
170 (je32_to_cpu(ri->dsize)+je32_to_cpu(ri->offset) == je32_to_cpu(ri->isize)))) {
171 flash_ofs |= REF_PRISTINE;
172 } else {
173 flash_ofs |= REF_NORMAL;
175 fn->raw = jffs2_add_physical_node_ref(c, flash_ofs, PAD(sizeof(*ri)+datalen), f->inocache);
176 if (IS_ERR(fn->raw)) {
177 void *hold_err = fn->raw;
178 /* Release the full_dnode which is now useless, and return */
179 jffs2_free_full_dnode(fn);
180 return ERR_PTR(PTR_ERR(hold_err));
182 fn->ofs = je32_to_cpu(ri->offset);
183 fn->size = je32_to_cpu(ri->dsize);
184 fn->frags = 0;
186 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",
187 flash_ofs & ~3, flash_ofs & 3, je32_to_cpu(ri->dsize),
188 je32_to_cpu(ri->csize), je32_to_cpu(ri->node_crc),
189 je32_to_cpu(ri->data_crc), je32_to_cpu(ri->totlen)));
191 if (retried) {
192 jffs2_dbg_acct_sanity_check(c,NULL);
195 return fn;
198 struct jffs2_full_dirent *jffs2_write_dirent(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
199 struct jffs2_raw_dirent *rd, const unsigned char *name,
200 uint32_t namelen, int alloc_mode)
202 struct jffs2_full_dirent *fd;
203 size_t retlen;
204 struct kvec vecs[2];
205 uint32_t flash_ofs;
206 int retried = 0;
207 int ret;
209 D1(printk(KERN_DEBUG "jffs2_write_dirent(ino #%u, name at *0x%p \"%s\"->ino #%u, name_crc 0x%08x)\n",
210 je32_to_cpu(rd->pino), name, name, je32_to_cpu(rd->ino),
211 je32_to_cpu(rd->name_crc)));
213 D1(if(je32_to_cpu(rd->hdr_crc) != crc32(0, rd, sizeof(struct jffs2_unknown_node)-4)) {
214 printk(KERN_CRIT "Eep. CRC not correct in jffs2_write_dirent()\n");
215 BUG();
218 if (strnlen(name, namelen) != namelen) {
219 /* This should never happen, but seems to have done on at least one
220 occasion: https://dev.laptop.org/ticket/4184 */
221 printk(KERN_CRIT "Error in jffs2_write_dirent() -- name contains zero bytes!\n");
222 printk(KERN_CRIT "Directory inode #%u, name at *0x%p \"%s\"->ino #%u, name_crc 0x%08x\n",
223 je32_to_cpu(rd->pino), name, name, je32_to_cpu(rd->ino),
224 je32_to_cpu(rd->name_crc));
225 WARN_ON(1);
226 return ERR_PTR(-EIO);
229 vecs[0].iov_base = rd;
230 vecs[0].iov_len = sizeof(*rd);
231 vecs[1].iov_base = (unsigned char *)name;
232 vecs[1].iov_len = namelen;
234 fd = jffs2_alloc_full_dirent(namelen+1);
235 if (!fd)
236 return ERR_PTR(-ENOMEM);
238 fd->version = je32_to_cpu(rd->version);
239 fd->ino = je32_to_cpu(rd->ino);
240 fd->nhash = full_name_hash(name, namelen);
241 fd->type = rd->type;
242 memcpy(fd->name, name, namelen);
243 fd->name[namelen]=0;
245 retry:
246 flash_ofs = write_ofs(c);
248 jffs2_dbg_prewrite_paranoia_check(c, flash_ofs, vecs[0].iov_len + vecs[1].iov_len);
250 if ((alloc_mode!=ALLOC_GC) && (je32_to_cpu(rd->version) < f->highest_version)) {
251 BUG_ON(!retried);
252 D1(printk(KERN_DEBUG "jffs2_write_dirent : dirent_version %d, "
253 "highest version %d -> updating dirent\n",
254 je32_to_cpu(rd->version), f->highest_version));
255 rd->version = cpu_to_je32(++f->highest_version);
256 fd->version = je32_to_cpu(rd->version);
257 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
260 ret = jffs2_flash_writev(c, vecs, 2, flash_ofs, &retlen,
261 (alloc_mode==ALLOC_GC)?0:je32_to_cpu(rd->pino));
262 if (ret || (retlen != sizeof(*rd) + namelen)) {
263 printk(KERN_NOTICE "Write of %zd bytes at 0x%08x failed. returned %d, retlen %zd\n",
264 sizeof(*rd)+namelen, flash_ofs, ret, retlen);
265 /* Mark the space as dirtied */
266 if (retlen) {
267 jffs2_add_physical_node_ref(c, flash_ofs | REF_OBSOLETE, PAD(sizeof(*rd)+namelen), NULL);
268 } else {
269 printk(KERN_NOTICE "Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n", flash_ofs);
271 if (!retried) {
272 /* Try to reallocate space and retry */
273 uint32_t dummy;
274 struct jffs2_eraseblock *jeb = &c->blocks[flash_ofs / c->sector_size];
276 retried = 1;
278 D1(printk(KERN_DEBUG "Retrying failed write.\n"));
280 jffs2_dbg_acct_sanity_check(c,jeb);
281 jffs2_dbg_acct_paranoia_check(c, jeb);
283 if (alloc_mode == ALLOC_GC) {
284 ret = jffs2_reserve_space_gc(c, sizeof(*rd) + namelen, &dummy,
285 JFFS2_SUMMARY_DIRENT_SIZE(namelen));
286 } else {
287 /* Locking pain */
288 up(&f->sem);
289 jffs2_complete_reservation(c);
291 ret = jffs2_reserve_space(c, sizeof(*rd) + namelen, &dummy,
292 alloc_mode, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
293 down(&f->sem);
296 if (!ret) {
297 flash_ofs = write_ofs(c);
298 D1(printk(KERN_DEBUG "Allocated space at 0x%08x to retry failed write.\n", flash_ofs));
299 jffs2_dbg_acct_sanity_check(c,jeb);
300 jffs2_dbg_acct_paranoia_check(c, jeb);
301 goto retry;
303 D1(printk(KERN_DEBUG "Failed to allocate space to retry failed write: %d!\n", ret));
305 /* Release the full_dnode which is now useless, and return */
306 jffs2_free_full_dirent(fd);
307 return ERR_PTR(ret?ret:-EIO);
309 /* Mark the space used */
310 fd->raw = jffs2_add_physical_node_ref(c, flash_ofs | dirent_node_state(rd),
311 PAD(sizeof(*rd)+namelen), f->inocache);
312 if (IS_ERR(fd->raw)) {
313 void *hold_err = fd->raw;
314 /* Release the full_dirent which is now useless, and return */
315 jffs2_free_full_dirent(fd);
316 return ERR_PTR(PTR_ERR(hold_err));
319 if (retried) {
320 jffs2_dbg_acct_sanity_check(c,NULL);
323 return fd;
326 /* The OS-specific code fills in the metadata in the jffs2_raw_inode for us, so that
327 we don't have to go digging in struct inode or its equivalent. It should set:
328 mode, uid, gid, (starting)isize, atime, ctime, mtime */
329 int jffs2_write_inode_range(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
330 struct jffs2_raw_inode *ri, unsigned char *buf,
331 uint32_t offset, uint32_t writelen, uint32_t *retlen)
333 int ret = 0;
334 uint32_t writtenlen = 0;
336 D1(printk(KERN_DEBUG "jffs2_write_inode_range(): Ino #%u, ofs 0x%x, len 0x%x\n",
337 f->inocache->ino, offset, writelen));
339 while(writelen) {
340 struct jffs2_full_dnode *fn;
341 unsigned char *comprbuf = NULL;
342 uint16_t comprtype = JFFS2_COMPR_NONE;
343 uint32_t alloclen;
344 uint32_t datalen, cdatalen;
345 int retried = 0;
347 retry:
348 D2(printk(KERN_DEBUG "jffs2_commit_write() loop: 0x%x to write to 0x%x\n", writelen, offset));
350 ret = jffs2_reserve_space(c, sizeof(*ri) + JFFS2_MIN_DATA_LEN,
351 &alloclen, ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
352 if (ret) {
353 D1(printk(KERN_DEBUG "jffs2_reserve_space returned %d\n", ret));
354 break;
356 down(&f->sem);
357 datalen = min_t(uint32_t, writelen, PAGE_CACHE_SIZE - (offset & (PAGE_CACHE_SIZE-1)));
358 cdatalen = min_t(uint32_t, alloclen - sizeof(*ri), datalen);
360 comprtype = jffs2_compress(c, f, buf, &comprbuf, &datalen, &cdatalen);
362 ri->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
363 ri->nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
364 ri->totlen = cpu_to_je32(sizeof(*ri) + cdatalen);
365 ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
367 ri->ino = cpu_to_je32(f->inocache->ino);
368 ri->version = cpu_to_je32(++f->highest_version);
369 ri->isize = cpu_to_je32(max(je32_to_cpu(ri->isize), offset + datalen));
370 ri->offset = cpu_to_je32(offset);
371 ri->csize = cpu_to_je32(cdatalen);
372 ri->dsize = cpu_to_je32(datalen);
373 ri->compr = comprtype & 0xff;
374 ri->usercompr = (comprtype >> 8 ) & 0xff;
375 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
376 ri->data_crc = cpu_to_je32(crc32(0, comprbuf, cdatalen));
378 fn = jffs2_write_dnode(c, f, ri, comprbuf, cdatalen, ALLOC_NORETRY);
380 jffs2_free_comprbuf(comprbuf, buf);
382 if (IS_ERR(fn)) {
383 ret = PTR_ERR(fn);
384 up(&f->sem);
385 jffs2_complete_reservation(c);
386 if (!retried) {
387 /* Write error to be retried */
388 retried = 1;
389 D1(printk(KERN_DEBUG "Retrying node write in jffs2_write_inode_range()\n"));
390 goto retry;
392 break;
394 ret = jffs2_add_full_dnode_to_inode(c, f, fn);
395 if (f->metadata) {
396 jffs2_mark_node_obsolete(c, f->metadata->raw);
397 jffs2_free_full_dnode(f->metadata);
398 f->metadata = NULL;
400 if (ret) {
401 /* Eep */
402 D1(printk(KERN_DEBUG "Eep. add_full_dnode_to_inode() failed in commit_write, returned %d\n", ret));
403 jffs2_mark_node_obsolete(c, fn->raw);
404 jffs2_free_full_dnode(fn);
406 up(&f->sem);
407 jffs2_complete_reservation(c);
408 break;
410 up(&f->sem);
411 jffs2_complete_reservation(c);
412 if (!datalen) {
413 printk(KERN_WARNING "Eep. We didn't actually write any data in jffs2_write_inode_range()\n");
414 ret = -EIO;
415 break;
417 D1(printk(KERN_DEBUG "increasing writtenlen by %d\n", datalen));
418 writtenlen += datalen;
419 offset += datalen;
420 writelen -= datalen;
421 buf += datalen;
423 *retlen = writtenlen;
424 return ret;
427 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)
429 struct jffs2_raw_dirent *rd;
430 struct jffs2_full_dnode *fn;
431 struct jffs2_full_dirent *fd;
432 uint32_t alloclen;
433 int ret;
435 /* Try to reserve enough space for both node and dirent.
436 * Just the node will do for now, though
438 ret = jffs2_reserve_space(c, sizeof(*ri), &alloclen, ALLOC_NORMAL,
439 JFFS2_SUMMARY_INODE_SIZE);
440 D1(printk(KERN_DEBUG "jffs2_do_create(): reserved 0x%x bytes\n", alloclen));
441 if (ret) {
442 up(&f->sem);
443 return ret;
446 ri->data_crc = cpu_to_je32(0);
447 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
449 fn = jffs2_write_dnode(c, f, ri, NULL, 0, ALLOC_NORMAL);
451 D1(printk(KERN_DEBUG "jffs2_do_create created file with mode 0x%x\n",
452 jemode_to_cpu(ri->mode)));
454 if (IS_ERR(fn)) {
455 D1(printk(KERN_DEBUG "jffs2_write_dnode() failed\n"));
456 /* Eeek. Wave bye bye */
457 up(&f->sem);
458 jffs2_complete_reservation(c);
459 return PTR_ERR(fn);
461 /* No data here. Only a metadata node, which will be
462 obsoleted by the first data write
464 f->metadata = fn;
466 up(&f->sem);
467 jffs2_complete_reservation(c);
468 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
469 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
471 if (ret) {
472 /* Eep. */
473 D1(printk(KERN_DEBUG "jffs2_reserve_space() for dirent failed\n"));
474 return ret;
477 rd = jffs2_alloc_raw_dirent();
478 if (!rd) {
479 /* Argh. Now we treat it like a normal delete */
480 jffs2_complete_reservation(c);
481 return -ENOMEM;
484 down(&dir_f->sem);
486 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
487 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
488 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
489 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
491 rd->pino = cpu_to_je32(dir_f->inocache->ino);
492 rd->version = cpu_to_je32(++dir_f->highest_version);
493 rd->ino = ri->ino;
494 rd->mctime = ri->ctime;
495 rd->nsize = namelen;
496 rd->type = DT_REG;
497 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
498 rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
500 fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, ALLOC_NORMAL);
502 jffs2_free_raw_dirent(rd);
504 if (IS_ERR(fd)) {
505 /* dirent failed to write. Delete the inode normally
506 as if it were the final unlink() */
507 jffs2_complete_reservation(c);
508 up(&dir_f->sem);
509 return PTR_ERR(fd);
512 /* Link the fd into the inode's list, obsoleting an old
513 one if necessary. */
514 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
516 jffs2_complete_reservation(c);
517 up(&dir_f->sem);
519 return 0;
523 int jffs2_do_unlink(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f,
524 const char *name, int namelen, struct jffs2_inode_info *dead_f,
525 uint32_t time)
527 struct jffs2_raw_dirent *rd;
528 struct jffs2_full_dirent *fd;
529 uint32_t alloclen;
530 int ret;
532 if (!jffs2_can_mark_obsolete(c)) {
533 /* We can't mark stuff obsolete on the medium. We need to write a deletion dirent */
535 rd = jffs2_alloc_raw_dirent();
536 if (!rd)
537 return -ENOMEM;
539 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
540 ALLOC_DELETION, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
541 if (ret) {
542 jffs2_free_raw_dirent(rd);
543 return ret;
546 down(&dir_f->sem);
548 /* Build a deletion node */
549 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
550 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
551 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
552 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
554 rd->pino = cpu_to_je32(dir_f->inocache->ino);
555 rd->version = cpu_to_je32(++dir_f->highest_version);
556 rd->ino = cpu_to_je32(0);
557 rd->mctime = cpu_to_je32(time);
558 rd->nsize = namelen;
559 rd->type = DT_UNKNOWN;
560 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
561 rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
563 fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, ALLOC_DELETION);
565 jffs2_free_raw_dirent(rd);
567 if (IS_ERR(fd)) {
568 jffs2_complete_reservation(c);
569 up(&dir_f->sem);
570 return PTR_ERR(fd);
573 /* File it. This will mark the old one obsolete. */
574 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
575 up(&dir_f->sem);
576 } else {
577 struct jffs2_full_dirent **prev = &dir_f->dents;
578 uint32_t nhash = full_name_hash(name, namelen);
580 /* We don't actually want to reserve any space, but we do
581 want to be holding the alloc_sem when we write to flash */
582 down(&c->alloc_sem);
583 down(&dir_f->sem);
585 while ((*prev) && (*prev)->nhash <= nhash) {
586 if ((*prev)->nhash == nhash &&
587 !memcmp((*prev)->name, name, namelen) &&
588 !(*prev)->name[namelen]) {
589 struct jffs2_full_dirent *this = *prev;
591 D1(printk(KERN_DEBUG "Marking old dirent node (ino #%u) @%08x obsolete\n",
592 this->ino, ref_offset(this->raw)));
594 *prev = this->next;
595 jffs2_mark_node_obsolete(c, (this->raw));
596 jffs2_free_full_dirent(this);
597 break;
599 prev = &((*prev)->next);
601 up(&dir_f->sem);
604 /* dead_f is NULL if this was a rename not a real unlink */
605 /* Also catch the !f->inocache case, where there was a dirent
606 pointing to an inode which didn't exist. */
607 if (dead_f && dead_f->inocache) {
609 down(&dead_f->sem);
611 if (S_ISDIR(OFNI_EDONI_2SFFJ(dead_f)->i_mode)) {
612 while (dead_f->dents) {
613 /* There can be only deleted ones */
614 fd = dead_f->dents;
616 dead_f->dents = fd->next;
618 if (fd->ino) {
619 printk(KERN_WARNING "Deleting inode #%u with active dentry \"%s\"->ino #%u\n",
620 dead_f->inocache->ino, fd->name, fd->ino);
621 } else {
622 D1(printk(KERN_DEBUG "Removing deletion dirent for \"%s\" from dir ino #%u\n",
623 fd->name, dead_f->inocache->ino));
625 jffs2_mark_node_obsolete(c, fd->raw);
626 jffs2_free_full_dirent(fd);
630 dead_f->inocache->nlink--;
631 /* NB: Caller must set inode nlink if appropriate */
632 up(&dead_f->sem);
635 jffs2_complete_reservation(c);
637 return 0;
641 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)
643 struct jffs2_raw_dirent *rd;
644 struct jffs2_full_dirent *fd;
645 uint32_t alloclen;
646 int ret;
648 rd = jffs2_alloc_raw_dirent();
649 if (!rd)
650 return -ENOMEM;
652 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
653 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
654 if (ret) {
655 jffs2_free_raw_dirent(rd);
656 return ret;
659 down(&dir_f->sem);
661 /* Build a deletion node */
662 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
663 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
664 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
665 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
667 rd->pino = cpu_to_je32(dir_f->inocache->ino);
668 rd->version = cpu_to_je32(++dir_f->highest_version);
669 rd->ino = cpu_to_je32(ino);
670 rd->mctime = cpu_to_je32(time);
671 rd->nsize = namelen;
673 rd->type = type;
675 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
676 rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
678 fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, ALLOC_NORMAL);
680 jffs2_free_raw_dirent(rd);
682 if (IS_ERR(fd)) {
683 jffs2_complete_reservation(c);
684 up(&dir_f->sem);
685 return PTR_ERR(fd);
688 /* File it. This will mark the old one obsolete. */
689 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
691 jffs2_complete_reservation(c);
692 up(&dir_f->sem);
694 return 0;