Linux Audio

Check our new training course

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