xref: /openbmc/linux/fs/jffs2/write.c (revision 9c261b33)
1 /*
2  * JFFS2 -- Journalling Flash File System, Version 2.
3  *
4  * Copyright © 2001-2007 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  */
11 
12 #include <linux/kernel.h>
13 #include <linux/fs.h>
14 #include <linux/crc32.h>
15 #include <linux/pagemap.h>
16 #include <linux/mtd/mtd.h>
17 #include "nodelist.h"
18 #include "compr.h"
19 
20 
21 int jffs2_do_new_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
22 		       uint32_t mode, struct jffs2_raw_inode *ri)
23 {
24 	struct jffs2_inode_cache *ic;
25 
26 	ic = jffs2_alloc_inode_cache();
27 	if (!ic) {
28 		return -ENOMEM;
29 	}
30 
31 	memset(ic, 0, sizeof(*ic));
32 
33 	f->inocache = ic;
34 	f->inocache->pino_nlink = 1; /* Will be overwritten shortly for directories */
35 	f->inocache->nodes = (struct jffs2_raw_node_ref *)f->inocache;
36 	f->inocache->state = INO_STATE_PRESENT;
37 
38 	jffs2_add_ino_cache(c, f->inocache);
39 	jffs2_dbg(1, "%s(): Assigned ino# %d\n", __func__, f->inocache->ino);
40 	ri->ino = cpu_to_je32(f->inocache->ino);
41 
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);
47 
48 	f->highest_version = 1;
49 	ri->version = cpu_to_je32(f->highest_version);
50 
51 	return 0;
52 }
53 
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 */
56 
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)
60 
61 {
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;
69 
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();
73 	}
74 	   );
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;
79 
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);
82 	}
83 
84 	fn = jffs2_alloc_full_dnode();
85 	if (!fn)
86 		return ERR_PTR(-ENOMEM);
87 
88 	/* check number of valid vecs */
89 	if (!datalen || !data)
90 		cnt = 1;
91  retry:
92 	flash_ofs = write_ofs(c);
93 
94 	jffs2_dbg_prewrite_paranoia_check(c, flash_ofs, vecs[0].iov_len + vecs[1].iov_len);
95 
96 	if ((alloc_mode!=ALLOC_GC) && (je32_to_cpu(ri->version) < f->highest_version)) {
97 		BUG_ON(!retried);
98 		jffs2_dbg(1, "%s(): dnode_version %d, highest version %d -> updating dnode\n",
99 			  __func__,
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));
103 	}
104 
105 	ret = jffs2_flash_writev(c, vecs, cnt, flash_ofs, &retlen,
106 				 (alloc_mode==ALLOC_GC)?0:f->inocache->ino);
107 
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);
111 
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);
122 		}
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];
127 
128 			retried = 1;
129 
130 			jffs2_dbg(1, "Retrying failed write.\n");
131 
132 			jffs2_dbg_acct_sanity_check(c,jeb);
133 			jffs2_dbg_acct_paranoia_check(c, jeb);
134 
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 				mutex_unlock(&f->sem);
141 				jffs2_complete_reservation(c);
142 
143 				ret = jffs2_reserve_space(c, sizeof(*ri) + datalen, &dummy,
144 							  alloc_mode, JFFS2_SUMMARY_INODE_SIZE);
145 				mutex_lock(&f->sem);
146 			}
147 
148 			if (!ret) {
149 				flash_ofs = write_ofs(c);
150 				jffs2_dbg(1, "Allocated space at 0x%08x to retry failed write.\n",
151 					  flash_ofs);
152 
153 				jffs2_dbg_acct_sanity_check(c,jeb);
154 				jffs2_dbg_acct_paranoia_check(c, jeb);
155 
156 				goto retry;
157 			}
158 			jffs2_dbg(1, "Failed to allocate space to retry failed write: %d!\n",
159 				  ret);
160 		}
161 		/* Release the full_dnode which is now useless, and return */
162 		jffs2_free_full_dnode(fn);
163 		return ERR_PTR(ret?ret:-EIO);
164 	}
165 	/* Mark the space used */
166 	/* If node covers at least a whole page, or if it starts at the
167 	   beginning of a page and runs to the end of the file, or if
168 	   it's a hole node, mark it REF_PRISTINE, else REF_NORMAL.
169 	*/
170 	if ((je32_to_cpu(ri->dsize) >= PAGE_CACHE_SIZE) ||
171 	    ( ((je32_to_cpu(ri->offset)&(PAGE_CACHE_SIZE-1))==0) &&
172 	      (je32_to_cpu(ri->dsize)+je32_to_cpu(ri->offset) ==  je32_to_cpu(ri->isize)))) {
173 		flash_ofs |= REF_PRISTINE;
174 	} else {
175 		flash_ofs |= REF_NORMAL;
176 	}
177 	fn->raw = jffs2_add_physical_node_ref(c, flash_ofs, PAD(sizeof(*ri)+datalen), f->inocache);
178 	if (IS_ERR(fn->raw)) {
179 		void *hold_err = fn->raw;
180 		/* Release the full_dnode which is now useless, and return */
181 		jffs2_free_full_dnode(fn);
182 		return ERR_CAST(hold_err);
183 	}
184 	fn->ofs = je32_to_cpu(ri->offset);
185 	fn->size = je32_to_cpu(ri->dsize);
186 	fn->frags = 0;
187 
188 	jffs2_dbg(1, "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",
189 		  flash_ofs & ~3, flash_ofs & 3, je32_to_cpu(ri->dsize),
190 		  je32_to_cpu(ri->csize), je32_to_cpu(ri->node_crc),
191 		  je32_to_cpu(ri->data_crc), je32_to_cpu(ri->totlen));
192 
193 	if (retried) {
194 		jffs2_dbg_acct_sanity_check(c,NULL);
195 	}
196 
197 	return fn;
198 }
199 
200 struct jffs2_full_dirent *jffs2_write_dirent(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
201 					     struct jffs2_raw_dirent *rd, const unsigned char *name,
202 					     uint32_t namelen, int alloc_mode)
203 {
204 	struct jffs2_full_dirent *fd;
205 	size_t retlen;
206 	struct kvec vecs[2];
207 	uint32_t flash_ofs;
208 	int retried = 0;
209 	int ret;
210 
211 	jffs2_dbg(1, "%s(ino #%u, name at *0x%p \"%s\"->ino #%u, name_crc 0x%08x)\n",
212 		  __func__,
213 		  je32_to_cpu(rd->pino), name, name, je32_to_cpu(rd->ino),
214 		  je32_to_cpu(rd->name_crc));
215 
216 	D1(if(je32_to_cpu(rd->hdr_crc) != crc32(0, rd, sizeof(struct jffs2_unknown_node)-4)) {
217 		printk(KERN_CRIT "Eep. CRC not correct in jffs2_write_dirent()\n");
218 		BUG();
219 	   });
220 
221 	if (strnlen(name, namelen) != namelen) {
222 		/* This should never happen, but seems to have done on at least one
223 		   occasion: https://dev.laptop.org/ticket/4184 */
224 		printk(KERN_CRIT "Error in jffs2_write_dirent() -- name contains zero bytes!\n");
225 		printk(KERN_CRIT "Directory inode #%u, name at *0x%p \"%s\"->ino #%u, name_crc 0x%08x\n",
226 		       je32_to_cpu(rd->pino), name, name, je32_to_cpu(rd->ino),
227 		       je32_to_cpu(rd->name_crc));
228 		WARN_ON(1);
229 		return ERR_PTR(-EIO);
230 	}
231 
232 	vecs[0].iov_base = rd;
233 	vecs[0].iov_len = sizeof(*rd);
234 	vecs[1].iov_base = (unsigned char *)name;
235 	vecs[1].iov_len = namelen;
236 
237 	fd = jffs2_alloc_full_dirent(namelen+1);
238 	if (!fd)
239 		return ERR_PTR(-ENOMEM);
240 
241 	fd->version = je32_to_cpu(rd->version);
242 	fd->ino = je32_to_cpu(rd->ino);
243 	fd->nhash = full_name_hash(name, namelen);
244 	fd->type = rd->type;
245 	memcpy(fd->name, name, namelen);
246 	fd->name[namelen]=0;
247 
248  retry:
249 	flash_ofs = write_ofs(c);
250 
251 	jffs2_dbg_prewrite_paranoia_check(c, flash_ofs, vecs[0].iov_len + vecs[1].iov_len);
252 
253 	if ((alloc_mode!=ALLOC_GC) && (je32_to_cpu(rd->version) < f->highest_version)) {
254 		BUG_ON(!retried);
255 		jffs2_dbg(1, "%s(): dirent_version %d, highest version %d -> updating dirent\n",
256 			  __func__,
257 			  je32_to_cpu(rd->version), f->highest_version);
258 		rd->version = cpu_to_je32(++f->highest_version);
259 		fd->version = je32_to_cpu(rd->version);
260 		rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
261 	}
262 
263 	ret = jffs2_flash_writev(c, vecs, 2, flash_ofs, &retlen,
264 				 (alloc_mode==ALLOC_GC)?0:je32_to_cpu(rd->pino));
265 	if (ret || (retlen != sizeof(*rd) + namelen)) {
266 		printk(KERN_NOTICE "Write of %zd bytes at 0x%08x failed. returned %d, retlen %zd\n",
267 			       sizeof(*rd)+namelen, flash_ofs, ret, retlen);
268 		/* Mark the space as dirtied */
269 		if (retlen) {
270 			jffs2_add_physical_node_ref(c, flash_ofs | REF_OBSOLETE, PAD(sizeof(*rd)+namelen), NULL);
271 		} else {
272 			printk(KERN_NOTICE "Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n", flash_ofs);
273 		}
274 		if (!retried) {
275 			/* Try to reallocate space and retry */
276 			uint32_t dummy;
277 			struct jffs2_eraseblock *jeb = &c->blocks[flash_ofs / c->sector_size];
278 
279 			retried = 1;
280 
281 			jffs2_dbg(1, "Retrying failed write.\n");
282 
283 			jffs2_dbg_acct_sanity_check(c,jeb);
284 			jffs2_dbg_acct_paranoia_check(c, jeb);
285 
286 			if (alloc_mode == ALLOC_GC) {
287 				ret = jffs2_reserve_space_gc(c, sizeof(*rd) + namelen, &dummy,
288 							     JFFS2_SUMMARY_DIRENT_SIZE(namelen));
289 			} else {
290 				/* Locking pain */
291 				mutex_unlock(&f->sem);
292 				jffs2_complete_reservation(c);
293 
294 				ret = jffs2_reserve_space(c, sizeof(*rd) + namelen, &dummy,
295 							  alloc_mode, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
296 				mutex_lock(&f->sem);
297 			}
298 
299 			if (!ret) {
300 				flash_ofs = write_ofs(c);
301 				jffs2_dbg(1, "Allocated space at 0x%08x to retry failed write\n",
302 					  flash_ofs);
303 				jffs2_dbg_acct_sanity_check(c,jeb);
304 				jffs2_dbg_acct_paranoia_check(c, jeb);
305 				goto retry;
306 			}
307 			jffs2_dbg(1, "Failed to allocate space to retry failed write: %d!\n",
308 				  ret);
309 		}
310 		/* Release the full_dnode which is now useless, and return */
311 		jffs2_free_full_dirent(fd);
312 		return ERR_PTR(ret?ret:-EIO);
313 	}
314 	/* Mark the space used */
315 	fd->raw = jffs2_add_physical_node_ref(c, flash_ofs | dirent_node_state(rd),
316 					      PAD(sizeof(*rd)+namelen), f->inocache);
317 	if (IS_ERR(fd->raw)) {
318 		void *hold_err = fd->raw;
319 		/* Release the full_dirent which is now useless, and return */
320 		jffs2_free_full_dirent(fd);
321 		return ERR_CAST(hold_err);
322 	}
323 
324 	if (retried) {
325 		jffs2_dbg_acct_sanity_check(c,NULL);
326 	}
327 
328 	return fd;
329 }
330 
331 /* The OS-specific code fills in the metadata in the jffs2_raw_inode for us, so that
332    we don't have to go digging in struct inode or its equivalent. It should set:
333    mode, uid, gid, (starting)isize, atime, ctime, mtime */
334 int jffs2_write_inode_range(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
335 			    struct jffs2_raw_inode *ri, unsigned char *buf,
336 			    uint32_t offset, uint32_t writelen, uint32_t *retlen)
337 {
338 	int ret = 0;
339 	uint32_t writtenlen = 0;
340 
341 	jffs2_dbg(1, "%s(): Ino #%u, ofs 0x%x, len 0x%x\n",
342 		  __func__, f->inocache->ino, offset, writelen);
343 
344 	while(writelen) {
345 		struct jffs2_full_dnode *fn;
346 		unsigned char *comprbuf = NULL;
347 		uint16_t comprtype = JFFS2_COMPR_NONE;
348 		uint32_t alloclen;
349 		uint32_t datalen, cdatalen;
350 		int retried = 0;
351 
352 	retry:
353 		jffs2_dbg(2, "jffs2_commit_write() loop: 0x%x to write to 0x%x\n",
354 			  writelen, offset);
355 
356 		ret = jffs2_reserve_space(c, sizeof(*ri) + JFFS2_MIN_DATA_LEN,
357 					&alloclen, ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
358 		if (ret) {
359 			jffs2_dbg(1, "jffs2_reserve_space returned %d\n", ret);
360 			break;
361 		}
362 		mutex_lock(&f->sem);
363 		datalen = min_t(uint32_t, writelen, PAGE_CACHE_SIZE - (offset & (PAGE_CACHE_SIZE-1)));
364 		cdatalen = min_t(uint32_t, alloclen - sizeof(*ri), datalen);
365 
366 		comprtype = jffs2_compress(c, f, buf, &comprbuf, &datalen, &cdatalen);
367 
368 		ri->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
369 		ri->nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
370 		ri->totlen = cpu_to_je32(sizeof(*ri) + cdatalen);
371 		ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
372 
373 		ri->ino = cpu_to_je32(f->inocache->ino);
374 		ri->version = cpu_to_je32(++f->highest_version);
375 		ri->isize = cpu_to_je32(max(je32_to_cpu(ri->isize), offset + datalen));
376 		ri->offset = cpu_to_je32(offset);
377 		ri->csize = cpu_to_je32(cdatalen);
378 		ri->dsize = cpu_to_je32(datalen);
379 		ri->compr = comprtype & 0xff;
380 		ri->usercompr = (comprtype >> 8 ) & 0xff;
381 		ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
382 		ri->data_crc = cpu_to_je32(crc32(0, comprbuf, cdatalen));
383 
384 		fn = jffs2_write_dnode(c, f, ri, comprbuf, cdatalen, ALLOC_NORETRY);
385 
386 		jffs2_free_comprbuf(comprbuf, buf);
387 
388 		if (IS_ERR(fn)) {
389 			ret = PTR_ERR(fn);
390 			mutex_unlock(&f->sem);
391 			jffs2_complete_reservation(c);
392 			if (!retried) {
393 				/* Write error to be retried */
394 				retried = 1;
395 				jffs2_dbg(1, "Retrying node write in jffs2_write_inode_range()\n");
396 				goto retry;
397 			}
398 			break;
399 		}
400 		ret = jffs2_add_full_dnode_to_inode(c, f, fn);
401 		if (f->metadata) {
402 			jffs2_mark_node_obsolete(c, f->metadata->raw);
403 			jffs2_free_full_dnode(f->metadata);
404 			f->metadata = NULL;
405 		}
406 		if (ret) {
407 			/* Eep */
408 			jffs2_dbg(1, "Eep. add_full_dnode_to_inode() failed in commit_write, returned %d\n",
409 				  ret);
410 			jffs2_mark_node_obsolete(c, fn->raw);
411 			jffs2_free_full_dnode(fn);
412 
413 			mutex_unlock(&f->sem);
414 			jffs2_complete_reservation(c);
415 			break;
416 		}
417 		mutex_unlock(&f->sem);
418 		jffs2_complete_reservation(c);
419 		if (!datalen) {
420 			printk(KERN_WARNING "Eep. We didn't actually write any data in jffs2_write_inode_range()\n");
421 			ret = -EIO;
422 			break;
423 		}
424 		jffs2_dbg(1, "increasing writtenlen by %d\n", datalen);
425 		writtenlen += datalen;
426 		offset += datalen;
427 		writelen -= datalen;
428 		buf += datalen;
429 	}
430 	*retlen = writtenlen;
431 	return ret;
432 }
433 
434 int jffs2_do_create(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f,
435 		    struct jffs2_inode_info *f, struct jffs2_raw_inode *ri,
436 		    const struct qstr *qstr)
437 {
438 	struct jffs2_raw_dirent *rd;
439 	struct jffs2_full_dnode *fn;
440 	struct jffs2_full_dirent *fd;
441 	uint32_t alloclen;
442 	int ret;
443 
444 	/* Try to reserve enough space for both node and dirent.
445 	 * Just the node will do for now, though
446 	 */
447 	ret = jffs2_reserve_space(c, sizeof(*ri), &alloclen, ALLOC_NORMAL,
448 				JFFS2_SUMMARY_INODE_SIZE);
449 	jffs2_dbg(1, "%s(): reserved 0x%x bytes\n", __func__, alloclen);
450 	if (ret)
451 		return ret;
452 
453 	mutex_lock(&f->sem);
454 
455 	ri->data_crc = cpu_to_je32(0);
456 	ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
457 
458 	fn = jffs2_write_dnode(c, f, ri, NULL, 0, ALLOC_NORMAL);
459 
460 	jffs2_dbg(1, "jffs2_do_create created file with mode 0x%x\n",
461 		  jemode_to_cpu(ri->mode));
462 
463 	if (IS_ERR(fn)) {
464 		jffs2_dbg(1, "jffs2_write_dnode() failed\n");
465 		/* Eeek. Wave bye bye */
466 		mutex_unlock(&f->sem);
467 		jffs2_complete_reservation(c);
468 		return PTR_ERR(fn);
469 	}
470 	/* No data here. Only a metadata node, which will be
471 	   obsoleted by the first data write
472 	*/
473 	f->metadata = fn;
474 
475 	mutex_unlock(&f->sem);
476 	jffs2_complete_reservation(c);
477 
478 	ret = jffs2_init_security(&f->vfs_inode, &dir_f->vfs_inode, qstr);
479 	if (ret)
480 		return ret;
481 	ret = jffs2_init_acl_post(&f->vfs_inode);
482 	if (ret)
483 		return ret;
484 
485 	ret = jffs2_reserve_space(c, sizeof(*rd)+qstr->len, &alloclen,
486 				ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(qstr->len));
487 
488 	if (ret) {
489 		/* Eep. */
490 		jffs2_dbg(1, "jffs2_reserve_space() for dirent failed\n");
491 		return ret;
492 	}
493 
494 	rd = jffs2_alloc_raw_dirent();
495 	if (!rd) {
496 		/* Argh. Now we treat it like a normal delete */
497 		jffs2_complete_reservation(c);
498 		return -ENOMEM;
499 	}
500 
501 	mutex_lock(&dir_f->sem);
502 
503 	rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
504 	rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
505 	rd->totlen = cpu_to_je32(sizeof(*rd) + qstr->len);
506 	rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
507 
508 	rd->pino = cpu_to_je32(dir_f->inocache->ino);
509 	rd->version = cpu_to_je32(++dir_f->highest_version);
510 	rd->ino = ri->ino;
511 	rd->mctime = ri->ctime;
512 	rd->nsize = qstr->len;
513 	rd->type = DT_REG;
514 	rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
515 	rd->name_crc = cpu_to_je32(crc32(0, qstr->name, qstr->len));
516 
517 	fd = jffs2_write_dirent(c, dir_f, rd, qstr->name, qstr->len, ALLOC_NORMAL);
518 
519 	jffs2_free_raw_dirent(rd);
520 
521 	if (IS_ERR(fd)) {
522 		/* dirent failed to write. Delete the inode normally
523 		   as if it were the final unlink() */
524 		jffs2_complete_reservation(c);
525 		mutex_unlock(&dir_f->sem);
526 		return PTR_ERR(fd);
527 	}
528 
529 	/* Link the fd into the inode's list, obsoleting an old
530 	   one if necessary. */
531 	jffs2_add_fd_to_list(c, fd, &dir_f->dents);
532 
533 	jffs2_complete_reservation(c);
534 	mutex_unlock(&dir_f->sem);
535 
536 	return 0;
537 }
538 
539 
540 int jffs2_do_unlink(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f,
541 		    const char *name, int namelen, struct jffs2_inode_info *dead_f,
542 		    uint32_t time)
543 {
544 	struct jffs2_raw_dirent *rd;
545 	struct jffs2_full_dirent *fd;
546 	uint32_t alloclen;
547 	int ret;
548 
549 	if (!jffs2_can_mark_obsolete(c)) {
550 		/* We can't mark stuff obsolete on the medium. We need to write a deletion dirent */
551 
552 		rd = jffs2_alloc_raw_dirent();
553 		if (!rd)
554 			return -ENOMEM;
555 
556 		ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
557 					ALLOC_DELETION, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
558 		if (ret) {
559 			jffs2_free_raw_dirent(rd);
560 			return ret;
561 		}
562 
563 		mutex_lock(&dir_f->sem);
564 
565 		/* Build a deletion node */
566 		rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
567 		rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
568 		rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
569 		rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
570 
571 		rd->pino = cpu_to_je32(dir_f->inocache->ino);
572 		rd->version = cpu_to_je32(++dir_f->highest_version);
573 		rd->ino = cpu_to_je32(0);
574 		rd->mctime = cpu_to_je32(time);
575 		rd->nsize = namelen;
576 		rd->type = DT_UNKNOWN;
577 		rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
578 		rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
579 
580 		fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, ALLOC_DELETION);
581 
582 		jffs2_free_raw_dirent(rd);
583 
584 		if (IS_ERR(fd)) {
585 			jffs2_complete_reservation(c);
586 			mutex_unlock(&dir_f->sem);
587 			return PTR_ERR(fd);
588 		}
589 
590 		/* File it. This will mark the old one obsolete. */
591 		jffs2_add_fd_to_list(c, fd, &dir_f->dents);
592 		mutex_unlock(&dir_f->sem);
593 	} else {
594 		uint32_t nhash = full_name_hash(name, namelen);
595 
596 		fd = dir_f->dents;
597 		/* We don't actually want to reserve any space, but we do
598 		   want to be holding the alloc_sem when we write to flash */
599 		mutex_lock(&c->alloc_sem);
600 		mutex_lock(&dir_f->sem);
601 
602 		for (fd = dir_f->dents; fd; fd = fd->next) {
603 			if (fd->nhash == nhash &&
604 			    !memcmp(fd->name, name, namelen) &&
605 			    !fd->name[namelen]) {
606 
607 				jffs2_dbg(1, "Marking old dirent node (ino #%u) @%08x obsolete\n",
608 					  fd->ino, ref_offset(fd->raw));
609 				jffs2_mark_node_obsolete(c, fd->raw);
610 				/* We don't want to remove it from the list immediately,
611 				   because that screws up getdents()/seek() semantics even
612 				   more than they're screwed already. Turn it into a
613 				   node-less deletion dirent instead -- a placeholder */
614 				fd->raw = NULL;
615 				fd->ino = 0;
616 				break;
617 			}
618 		}
619 		mutex_unlock(&dir_f->sem);
620 	}
621 
622 	/* dead_f is NULL if this was a rename not a real unlink */
623 	/* Also catch the !f->inocache case, where there was a dirent
624 	   pointing to an inode which didn't exist. */
625 	if (dead_f && dead_f->inocache) {
626 
627 		mutex_lock(&dead_f->sem);
628 
629 		if (S_ISDIR(OFNI_EDONI_2SFFJ(dead_f)->i_mode)) {
630 			while (dead_f->dents) {
631 				/* There can be only deleted ones */
632 				fd = dead_f->dents;
633 
634 				dead_f->dents = fd->next;
635 
636 				if (fd->ino) {
637 					printk(KERN_WARNING "Deleting inode #%u with active dentry \"%s\"->ino #%u\n",
638 					       dead_f->inocache->ino, fd->name, fd->ino);
639 				} else {
640 					jffs2_dbg(1, "Removing deletion dirent for \"%s\" from dir ino #%u\n",
641 						  fd->name,
642 						  dead_f->inocache->ino);
643 				}
644 				if (fd->raw)
645 					jffs2_mark_node_obsolete(c, fd->raw);
646 				jffs2_free_full_dirent(fd);
647 			}
648 			dead_f->inocache->pino_nlink = 0;
649 		} else
650 			dead_f->inocache->pino_nlink--;
651 		/* NB: Caller must set inode nlink if appropriate */
652 		mutex_unlock(&dead_f->sem);
653 	}
654 
655 	jffs2_complete_reservation(c);
656 
657 	return 0;
658 }
659 
660 
661 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)
662 {
663 	struct jffs2_raw_dirent *rd;
664 	struct jffs2_full_dirent *fd;
665 	uint32_t alloclen;
666 	int ret;
667 
668 	rd = jffs2_alloc_raw_dirent();
669 	if (!rd)
670 		return -ENOMEM;
671 
672 	ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
673 				ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
674 	if (ret) {
675 		jffs2_free_raw_dirent(rd);
676 		return ret;
677 	}
678 
679 	mutex_lock(&dir_f->sem);
680 
681 	/* Build a deletion node */
682 	rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
683 	rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
684 	rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
685 	rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
686 
687 	rd->pino = cpu_to_je32(dir_f->inocache->ino);
688 	rd->version = cpu_to_je32(++dir_f->highest_version);
689 	rd->ino = cpu_to_je32(ino);
690 	rd->mctime = cpu_to_je32(time);
691 	rd->nsize = namelen;
692 
693 	rd->type = type;
694 
695 	rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
696 	rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
697 
698 	fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, ALLOC_NORMAL);
699 
700 	jffs2_free_raw_dirent(rd);
701 
702 	if (IS_ERR(fd)) {
703 		jffs2_complete_reservation(c);
704 		mutex_unlock(&dir_f->sem);
705 		return PTR_ERR(fd);
706 	}
707 
708 	/* File it. This will mark the old one obsolete. */
709 	jffs2_add_fd_to_list(c, fd, &dir_f->dents);
710 
711 	jffs2_complete_reservation(c);
712 	mutex_unlock(&dir_f->sem);
713 
714 	return 0;
715 }
716