Blame view

fs/jffs2/write.c 21.3 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
  /*
   * JFFS2 -- Journalling Flash File System, Version 2.
   *
c00c310ea   David Woodhouse   [JFFS2] Tidy up l...
4
   * Copyright © 2001-2007 Red Hat, Inc.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
5
6
7
8
9
   *
   * Created by David Woodhouse <dwmw2@infradead.org>
   *
   * For licensing information, see the file 'LICENCE' in this directory.
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
10
11
12
13
14
   */
  
  #include <linux/kernel.h>
  #include <linux/fs.h>
  #include <linux/crc32.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
15
16
17
18
  #include <linux/pagemap.h>
  #include <linux/mtd/mtd.h>
  #include "nodelist.h"
  #include "compr.h"
27c72b040   David Woodhouse   [JFFS2] Track par...
19
20
  int jffs2_do_new_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
  		       uint32_t mode, struct jffs2_raw_inode *ri)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
21
22
23
24
25
26
27
28
29
30
31
  {
  	struct jffs2_inode_cache *ic;
  
  	ic = jffs2_alloc_inode_cache();
  	if (!ic) {
  		return -ENOMEM;
  	}
  
  	memset(ic, 0, sizeof(*ic));
  
  	f->inocache = ic;
27c72b040   David Woodhouse   [JFFS2] Track par...
32
  	f->inocache->pino_nlink = 1; /* Will be overwritten shortly for directories */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
33
  	f->inocache->nodes = (struct jffs2_raw_node_ref *)f->inocache;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
34
  	f->inocache->state = INO_STATE_PRESENT;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
35

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
36
  	jffs2_add_ino_cache(c, f->inocache);
7d200960d   David Woodhouse   [JFFS2] Fix inode...
37
38
39
  	D1(printk(KERN_DEBUG "jffs2_do_new_inode(): Assigned ino# %d
  ", f->inocache->ino));
  	ri->ino = cpu_to_je32(f->inocache->ino);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
40
41
42
43
44
45
46
47
48
49
50
51
  
  	ri->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
  	ri->nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
  	ri->totlen = cpu_to_je32(PAD(sizeof(*ri)));
  	ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
  	ri->mode = cpu_to_jemode(mode);
  
  	f->highest_version = 1;
  	ri->version = cpu_to_je32(f->highest_version);
  
  	return 0;
  }
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
52
  /* jffs2_write_dnode - given a raw_inode, allocate a full_dnode for it,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
53
     write it to the flash, link it into the existing inode/fragment list */
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
54
55
56
  struct jffs2_full_dnode *jffs2_write_dnode(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
  					   struct jffs2_raw_inode *ri, const unsigned char *data,
  					   uint32_t datalen, int alloc_mode)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
57
58
  
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
59
60
  	struct jffs2_full_dnode *fn;
  	size_t retlen;
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
61
  	uint32_t flash_ofs;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
  	struct kvec vecs[2];
  	int ret;
  	int retried = 0;
  	unsigned long cnt = 2;
  
  	D1(if(je32_to_cpu(ri->hdr_crc) != crc32(0, ri, sizeof(struct jffs2_unknown_node)-4)) {
  		printk(KERN_CRIT "Eep. CRC not correct in jffs2_write_dnode()
  ");
  		BUG();
  	}
  	   );
  	vecs[0].iov_base = ri;
  	vecs[0].iov_len = sizeof(*ri);
  	vecs[1].iov_base = (unsigned char *)data;
  	vecs[1].iov_len = datalen;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
77
78
79
80
  	if (je32_to_cpu(ri->totlen) != sizeof(*ri) + datalen) {
  		printk(KERN_WARNING "jffs2_write_dnode: ri->totlen (0x%08x) != sizeof(*ri) (0x%08zx) + datalen (0x%08x)
  ", je32_to_cpu(ri->totlen), sizeof(*ri), datalen);
  	}
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
81

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
82
  	fn = jffs2_alloc_full_dnode();
2f785402f   David Woodhouse   [JFFS2] Reduce vi...
83
  	if (!fn)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
84
  		return ERR_PTR(-ENOMEM);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
85
86
87
88
89
  
  	/* check number of valid vecs */
  	if (!datalen || !data)
  		cnt = 1;
   retry:
2f785402f   David Woodhouse   [JFFS2] Reduce vi...
90
  	flash_ofs = write_ofs(c);
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
91
92
  
  	jffs2_dbg_prewrite_paranoia_check(c, flash_ofs, vecs[0].iov_len + vecs[1].iov_len);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
93

9b88f4739   Estelle Hammache   [JFFS2] Code clea...
94
95
96
  	if ((alloc_mode!=ALLOC_GC) && (je32_to_cpu(ri->version) < f->highest_version)) {
  		BUG_ON(!retried);
  		D1(printk(KERN_DEBUG "jffs2_write_dnode : dnode_version %d, "
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
97
98
  				"highest version %d -> updating dnode
  ",
9b88f4739   Estelle Hammache   [JFFS2] Code clea...
99
100
101
  				je32_to_cpu(ri->version), f->highest_version));
  		ri->version = cpu_to_je32(++f->highest_version);
  		ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
e4803c30d   Estelle Hammache   [JFFS2] Fix write...
102
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
103
104
105
106
  	ret = jffs2_flash_writev(c, vecs, cnt, flash_ofs, &retlen,
  				 (alloc_mode==ALLOC_GC)?0:f->inocache->ino);
  
  	if (ret || (retlen != sizeof(*ri) + datalen)) {
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
107
108
  		printk(KERN_NOTICE "Write of %zd bytes at 0x%08x failed. returned %d, retlen %zd
  ",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
109
110
111
112
  		       sizeof(*ri)+datalen, flash_ofs, ret, retlen);
  
  		/* Mark the space as dirtied */
  		if (retlen) {
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
113
  			/* Don't change raw->size to match retlen. We may have
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
114
115
  			   written the node header already, and only the data will
  			   seem corrupted, in which case the scan would skip over
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
116
  			   any node we write before the original intended end of
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
117
  			   this node */
2f785402f   David Woodhouse   [JFFS2] Reduce vi...
118
  			jffs2_add_physical_node_ref(c, flash_ofs | REF_OBSOLETE, PAD(sizeof(*ri)+datalen), NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
119
  		} else {
2f785402f   David Woodhouse   [JFFS2] Reduce vi...
120
121
  			printk(KERN_NOTICE "Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero
  ", flash_ofs);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
122
  		}
2f785402f   David Woodhouse   [JFFS2] Reduce vi...
123
  		if (!retried && alloc_mode != ALLOC_NORETRY) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
124
125
126
127
128
129
130
131
  			/* Try to reallocate space and retry */
  			uint32_t dummy;
  			struct jffs2_eraseblock *jeb = &c->blocks[flash_ofs / c->sector_size];
  
  			retried = 1;
  
  			D1(printk(KERN_DEBUG "Retrying failed write.
  "));
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
132

730554d94   Artem B. Bityutskiy   [JFFS2] Debug cod...
133
134
  			jffs2_dbg_acct_sanity_check(c,jeb);
  			jffs2_dbg_acct_paranoia_check(c, jeb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
135
136
  
  			if (alloc_mode == ALLOC_GC) {
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
137
138
  				ret = jffs2_reserve_space_gc(c, sizeof(*ri) + datalen, &dummy,
  							     JFFS2_SUMMARY_INODE_SIZE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
139
140
  			} else {
  				/* Locking pain */
ced220703   David Woodhouse   [JFFS2] semaphore...
141
  				mutex_unlock(&f->sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
142
  				jffs2_complete_reservation(c);
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
143

9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
144
145
  				ret = jffs2_reserve_space(c, sizeof(*ri) + datalen, &dummy,
  							  alloc_mode, JFFS2_SUMMARY_INODE_SIZE);
ced220703   David Woodhouse   [JFFS2] semaphore...
146
  				mutex_lock(&f->sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
147
148
149
  			}
  
  			if (!ret) {
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
150
  				flash_ofs = write_ofs(c);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
151
152
  				D1(printk(KERN_DEBUG "Allocated space at 0x%08x to retry failed write.
  ", flash_ofs));
730554d94   Artem B. Bityutskiy   [JFFS2] Debug cod...
153
154
  				jffs2_dbg_acct_sanity_check(c,jeb);
  				jffs2_dbg_acct_paranoia_check(c, jeb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
155
156
157
158
159
  
  				goto retry;
  			}
  			D1(printk(KERN_DEBUG "Failed to allocate space to retry failed write: %d!
  ", ret));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
160
161
162
163
164
165
  		}
  		/* Release the full_dnode which is now useless, and return */
  		jffs2_free_full_dnode(fn);
  		return ERR_PTR(ret?ret:-EIO);
  	}
  	/* Mark the space used */
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
166
167
168
  	/* If node covers at least a whole page, or if it starts at the
  	   beginning of a page and runs to the end of the file, or if
  	   it's a hole node, mark it REF_PRISTINE, else REF_NORMAL.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
169
170
171
172
  	*/
  	if ((je32_to_cpu(ri->dsize) >= PAGE_CACHE_SIZE) ||
  	    ( ((je32_to_cpu(ri->offset)&(PAGE_CACHE_SIZE-1))==0) &&
  	      (je32_to_cpu(ri->dsize)+je32_to_cpu(ri->offset) ==  je32_to_cpu(ri->isize)))) {
2f785402f   David Woodhouse   [JFFS2] Reduce vi...
173
  		flash_ofs |= REF_PRISTINE;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
174
  	} else {
2f785402f   David Woodhouse   [JFFS2] Reduce vi...
175
  		flash_ofs |= REF_NORMAL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
176
  	}
2f785402f   David Woodhouse   [JFFS2] Reduce vi...
177
  	fn->raw = jffs2_add_physical_node_ref(c, flash_ofs, PAD(sizeof(*ri)+datalen), f->inocache);
5bd5c03c3   Joakim Tjernlund   [JFFS2] Prevent o...
178
179
180
181
  	if (IS_ERR(fn->raw)) {
  		void *hold_err = fn->raw;
  		/* Release the full_dnode which is now useless, and return */
  		jffs2_free_full_dnode(fn);
e231c2ee6   David Howells   Convert ERR_PTR(P...
182
  		return ERR_CAST(hold_err);
5bd5c03c3   Joakim Tjernlund   [JFFS2] Prevent o...
183
  	}
2f785402f   David Woodhouse   [JFFS2] Reduce vi...
184
185
186
  	fn->ofs = je32_to_cpu(ri->offset);
  	fn->size = je32_to_cpu(ri->dsize);
  	fn->frags = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
187
188
189
  
  	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
  ",
2f785402f   David Woodhouse   [JFFS2] Reduce vi...
190
  		  flash_ofs & ~3, flash_ofs & 3, je32_to_cpu(ri->dsize),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
191
192
193
194
  		  je32_to_cpu(ri->csize), je32_to_cpu(ri->node_crc),
  		  je32_to_cpu(ri->data_crc), je32_to_cpu(ri->totlen)));
  
  	if (retried) {
730554d94   Artem B. Bityutskiy   [JFFS2] Debug cod...
195
  		jffs2_dbg_acct_sanity_check(c,NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
196
197
198
199
  	}
  
  	return fn;
  }
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
200
201
202
  struct jffs2_full_dirent *jffs2_write_dirent(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
  					     struct jffs2_raw_dirent *rd, const unsigned char *name,
  					     uint32_t namelen, int alloc_mode)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
203
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
204
205
206
  	struct jffs2_full_dirent *fd;
  	size_t retlen;
  	struct kvec vecs[2];
2f785402f   David Woodhouse   [JFFS2] Reduce vi...
207
  	uint32_t flash_ofs;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
208
209
  	int retried = 0;
  	int ret;
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
210
211
  	D1(printk(KERN_DEBUG "jffs2_write_dirent(ino #%u, name at *0x%p \"%s\"->ino #%u, name_crc 0x%08x)
  ",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
212
213
  		  je32_to_cpu(rd->pino), name, name, je32_to_cpu(rd->ino),
  		  je32_to_cpu(rd->name_crc)));
730554d94   Artem B. Bityutskiy   [JFFS2] Debug cod...
214

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
215
216
217
218
  	D1(if(je32_to_cpu(rd->hdr_crc) != crc32(0, rd, sizeof(struct jffs2_unknown_node)-4)) {
  		printk(KERN_CRIT "Eep. CRC not correct in jffs2_write_dirent()
  ");
  		BUG();
2f785402f   David Woodhouse   [JFFS2] Reduce vi...
219
  	   });
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
220

69ca4378a   David Woodhouse   [JFFS2] Check for...
221
222
223
224
225
226
227
228
229
230
231
232
  	if (strnlen(name, namelen) != namelen) {
  		/* This should never happen, but seems to have done on at least one
  		   occasion: https://dev.laptop.org/ticket/4184 */
  		printk(KERN_CRIT "Error in jffs2_write_dirent() -- name contains zero bytes!
  ");
  		printk(KERN_CRIT "Directory inode #%u, name at *0x%p \"%s\"->ino #%u, name_crc 0x%08x
  ",
  		       je32_to_cpu(rd->pino), name, name, je32_to_cpu(rd->ino),
  		       je32_to_cpu(rd->name_crc));
  		WARN_ON(1);
  		return ERR_PTR(-EIO);
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
233
234
235
236
  	vecs[0].iov_base = rd;
  	vecs[0].iov_len = sizeof(*rd);
  	vecs[1].iov_base = (unsigned char *)name;
  	vecs[1].iov_len = namelen;
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
237

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
238
  	fd = jffs2_alloc_full_dirent(namelen+1);
2f785402f   David Woodhouse   [JFFS2] Reduce vi...
239
  	if (!fd)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
240
  		return ERR_PTR(-ENOMEM);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
241
242
243
  
  	fd->version = je32_to_cpu(rd->version);
  	fd->ino = je32_to_cpu(rd->ino);
69ca4378a   David Woodhouse   [JFFS2] Check for...
244
  	fd->nhash = full_name_hash(name, namelen);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
245
246
247
248
249
  	fd->type = rd->type;
  	memcpy(fd->name, name, namelen);
  	fd->name[namelen]=0;
  
   retry:
2f785402f   David Woodhouse   [JFFS2] Reduce vi...
250
  	flash_ofs = write_ofs(c);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
251

2f785402f   David Woodhouse   [JFFS2] Reduce vi...
252
  	jffs2_dbg_prewrite_paranoia_check(c, flash_ofs, vecs[0].iov_len + vecs[1].iov_len);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
253

9b88f4739   Estelle Hammache   [JFFS2] Code clea...
254
255
256
257
258
259
260
261
262
  	if ((alloc_mode!=ALLOC_GC) && (je32_to_cpu(rd->version) < f->highest_version)) {
  		BUG_ON(!retried);
  		D1(printk(KERN_DEBUG "jffs2_write_dirent : dirent_version %d, "
  				     "highest version %d -> updating dirent
  ",
  				     je32_to_cpu(rd->version), f->highest_version));
  		rd->version = cpu_to_je32(++f->highest_version);
  		fd->version = je32_to_cpu(rd->version);
  		rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
e4803c30d   Estelle Hammache   [JFFS2] Fix write...
263
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
264
265
266
  	ret = jffs2_flash_writev(c, vecs, 2, flash_ofs, &retlen,
  				 (alloc_mode==ALLOC_GC)?0:je32_to_cpu(rd->pino));
  	if (ret || (retlen != sizeof(*rd) + namelen)) {
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
267
268
  		printk(KERN_NOTICE "Write of %zd bytes at 0x%08x failed. returned %d, retlen %zd
  ",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
269
270
271
  			       sizeof(*rd)+namelen, flash_ofs, ret, retlen);
  		/* Mark the space as dirtied */
  		if (retlen) {
2f785402f   David Woodhouse   [JFFS2] Reduce vi...
272
  			jffs2_add_physical_node_ref(c, flash_ofs | REF_OBSOLETE, PAD(sizeof(*rd)+namelen), NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
273
  		} else {
2f785402f   David Woodhouse   [JFFS2] Reduce vi...
274
275
  			printk(KERN_NOTICE "Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero
  ", flash_ofs);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
276
  		}
2f785402f   David Woodhouse   [JFFS2] Reduce vi...
277
  		if (!retried) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
278
279
280
281
282
283
284
285
  			/* Try to reallocate space and retry */
  			uint32_t dummy;
  			struct jffs2_eraseblock *jeb = &c->blocks[flash_ofs / c->sector_size];
  
  			retried = 1;
  
  			D1(printk(KERN_DEBUG "Retrying failed write.
  "));
730554d94   Artem B. Bityutskiy   [JFFS2] Debug cod...
286
287
  			jffs2_dbg_acct_sanity_check(c,jeb);
  			jffs2_dbg_acct_paranoia_check(c, jeb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
288
289
  
  			if (alloc_mode == ALLOC_GC) {
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
290
291
  				ret = jffs2_reserve_space_gc(c, sizeof(*rd) + namelen, &dummy,
  							     JFFS2_SUMMARY_DIRENT_SIZE(namelen));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
292
293
  			} else {
  				/* Locking pain */
ced220703   David Woodhouse   [JFFS2] semaphore...
294
  				mutex_unlock(&f->sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
295
  				jffs2_complete_reservation(c);
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
296

9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
297
298
  				ret = jffs2_reserve_space(c, sizeof(*rd) + namelen, &dummy,
  							  alloc_mode, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
ced220703   David Woodhouse   [JFFS2] semaphore...
299
  				mutex_lock(&f->sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
300
301
302
  			}
  
  			if (!ret) {
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
303
  				flash_ofs = write_ofs(c);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
304
305
  				D1(printk(KERN_DEBUG "Allocated space at 0x%08x to retry failed write.
  ", flash_ofs));
730554d94   Artem B. Bityutskiy   [JFFS2] Debug cod...
306
307
  				jffs2_dbg_acct_sanity_check(c,jeb);
  				jffs2_dbg_acct_paranoia_check(c, jeb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
308
309
310
311
  				goto retry;
  			}
  			D1(printk(KERN_DEBUG "Failed to allocate space to retry failed write: %d!
  ", ret));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
312
313
314
315
316
317
  		}
  		/* Release the full_dnode which is now useless, and return */
  		jffs2_free_full_dirent(fd);
  		return ERR_PTR(ret?ret:-EIO);
  	}
  	/* Mark the space used */
71c233977   David Woodhouse   [JFFS2] Deletion ...
318
319
  	fd->raw = jffs2_add_physical_node_ref(c, flash_ofs | dirent_node_state(rd),
  					      PAD(sizeof(*rd)+namelen), f->inocache);
5bd5c03c3   Joakim Tjernlund   [JFFS2] Prevent o...
320
321
322
323
  	if (IS_ERR(fd->raw)) {
  		void *hold_err = fd->raw;
  		/* Release the full_dirent which is now useless, and return */
  		jffs2_free_full_dirent(fd);
e231c2ee6   David Howells   Convert ERR_PTR(P...
324
  		return ERR_CAST(hold_err);
5bd5c03c3   Joakim Tjernlund   [JFFS2] Prevent o...
325
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
326
327
  
  	if (retried) {
730554d94   Artem B. Bityutskiy   [JFFS2] Debug cod...
328
  		jffs2_dbg_acct_sanity_check(c,NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
329
330
331
332
333
334
335
336
337
  	}
  
  	return fd;
  }
  
  /* The OS-specific code fills in the metadata in the jffs2_raw_inode for us, so that
     we don't have to go digging in struct inode or its equivalent. It should set:
     mode, uid, gid, (starting)isize, atime, ctime, mtime */
  int jffs2_write_inode_range(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
338
  			    struct jffs2_raw_inode *ri, unsigned char *buf,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
339
340
341
342
343
344
345
346
  			    uint32_t offset, uint32_t writelen, uint32_t *retlen)
  {
  	int ret = 0;
  	uint32_t writtenlen = 0;
  
         	D1(printk(KERN_DEBUG "jffs2_write_inode_range(): Ino #%u, ofs 0x%x, len 0x%x
  ",
  		  f->inocache->ino, offset, writelen));
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
347

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
348
349
350
351
  	while(writelen) {
  		struct jffs2_full_dnode *fn;
  		unsigned char *comprbuf = NULL;
  		uint16_t comprtype = JFFS2_COMPR_NONE;
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
352
  		uint32_t alloclen;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
353
354
355
356
357
358
  		uint32_t datalen, cdatalen;
  		int retried = 0;
  
  	retry:
  		D2(printk(KERN_DEBUG "jffs2_commit_write() loop: 0x%x to write to 0x%x
  ", writelen, offset));
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
359
  		ret = jffs2_reserve_space(c, sizeof(*ri) + JFFS2_MIN_DATA_LEN,
e631ddba5   Ferenc Havasi   [JFFS2] Add erase...
360
  					&alloclen, ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
361
362
363
364
365
  		if (ret) {
  			D1(printk(KERN_DEBUG "jffs2_reserve_space returned %d
  ", ret));
  			break;
  		}
ced220703   David Woodhouse   [JFFS2] semaphore...
366
  		mutex_lock(&f->sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
  		datalen = min_t(uint32_t, writelen, PAGE_CACHE_SIZE - (offset & (PAGE_CACHE_SIZE-1)));
  		cdatalen = min_t(uint32_t, alloclen - sizeof(*ri), datalen);
  
  		comprtype = jffs2_compress(c, f, buf, &comprbuf, &datalen, &cdatalen);
  
  		ri->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
  		ri->nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
  		ri->totlen = cpu_to_je32(sizeof(*ri) + cdatalen);
  		ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
  
  		ri->ino = cpu_to_je32(f->inocache->ino);
  		ri->version = cpu_to_je32(++f->highest_version);
  		ri->isize = cpu_to_je32(max(je32_to_cpu(ri->isize), offset + datalen));
  		ri->offset = cpu_to_je32(offset);
  		ri->csize = cpu_to_je32(cdatalen);
  		ri->dsize = cpu_to_je32(datalen);
  		ri->compr = comprtype & 0xff;
  		ri->usercompr = (comprtype >> 8 ) & 0xff;
  		ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
  		ri->data_crc = cpu_to_je32(crc32(0, comprbuf, cdatalen));
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
387
  		fn = jffs2_write_dnode(c, f, ri, comprbuf, cdatalen, ALLOC_NORETRY);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
388
389
390
391
392
  
  		jffs2_free_comprbuf(comprbuf, buf);
  
  		if (IS_ERR(fn)) {
  			ret = PTR_ERR(fn);
ced220703   David Woodhouse   [JFFS2] semaphore...
393
  			mutex_unlock(&f->sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
  			jffs2_complete_reservation(c);
  			if (!retried) {
  				/* Write error to be retried */
  				retried = 1;
  				D1(printk(KERN_DEBUG "Retrying node write in jffs2_write_inode_range()
  "));
  				goto retry;
  			}
  			break;
  		}
  		ret = jffs2_add_full_dnode_to_inode(c, f, fn);
  		if (f->metadata) {
  			jffs2_mark_node_obsolete(c, f->metadata->raw);
  			jffs2_free_full_dnode(f->metadata);
  			f->metadata = NULL;
  		}
  		if (ret) {
  			/* Eep */
  			D1(printk(KERN_DEBUG "Eep. add_full_dnode_to_inode() failed in commit_write, returned %d
  ", ret));
  			jffs2_mark_node_obsolete(c, fn->raw);
  			jffs2_free_full_dnode(fn);
ced220703   David Woodhouse   [JFFS2] semaphore...
416
  			mutex_unlock(&f->sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
417
418
419
  			jffs2_complete_reservation(c);
  			break;
  		}
ced220703   David Woodhouse   [JFFS2] semaphore...
420
  		mutex_unlock(&f->sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
  		jffs2_complete_reservation(c);
  		if (!datalen) {
  			printk(KERN_WARNING "Eep. We didn't actually write any data in jffs2_write_inode_range()
  ");
  			ret = -EIO;
  			break;
  		}
  		D1(printk(KERN_DEBUG "increasing writtenlen by %d
  ", datalen));
  		writtenlen += datalen;
  		offset += datalen;
  		writelen -= datalen;
  		buf += datalen;
  	}
  	*retlen = writtenlen;
  	return ret;
  }
2a7dba391   Eric Paris   fs/vfs/security: ...
438
439
440
  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 struct qstr *qstr)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
441
442
443
444
  {
  	struct jffs2_raw_dirent *rd;
  	struct jffs2_full_dnode *fn;
  	struct jffs2_full_dirent *fd;
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
445
  	uint32_t alloclen;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
446
  	int ret;
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
447
448
  	/* Try to reserve enough space for both node and dirent.
  	 * Just the node will do for now, though
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
449
  	 */
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
450
  	ret = jffs2_reserve_space(c, sizeof(*ri), &alloclen, ALLOC_NORMAL,
e631ddba5   Ferenc Havasi   [JFFS2] Add erase...
451
  				JFFS2_SUMMARY_INODE_SIZE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
452
453
  	D1(printk(KERN_DEBUG "jffs2_do_create(): reserved 0x%x bytes
  ", alloclen));
590fe34c4   David Woodhouse   [JFFS2] Quiet loc...
454
  	if (ret)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
455
  		return ret;
590fe34c4   David Woodhouse   [JFFS2] Quiet loc...
456
457
  
  	mutex_lock(&f->sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
458
459
460
  
  	ri->data_crc = cpu_to_je32(0);
  	ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
461
  	fn = jffs2_write_dnode(c, f, ri, NULL, 0, ALLOC_NORMAL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
462
463
464
465
466
467
468
469
470
  
  	D1(printk(KERN_DEBUG "jffs2_do_create created file with mode 0x%x
  ",
  		  jemode_to_cpu(ri->mode)));
  
  	if (IS_ERR(fn)) {
  		D1(printk(KERN_DEBUG "jffs2_write_dnode() failed
  "));
  		/* Eeek. Wave bye bye */
ced220703   David Woodhouse   [JFFS2] semaphore...
471
  		mutex_unlock(&f->sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
472
473
474
  		jffs2_complete_reservation(c);
  		return PTR_ERR(fn);
  	}
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
475
  	/* No data here. Only a metadata node, which will be
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
476
477
478
  	   obsoleted by the first data write
  	*/
  	f->metadata = fn;
ced220703   David Woodhouse   [JFFS2] semaphore...
479
  	mutex_unlock(&f->sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
480
  	jffs2_complete_reservation(c);
cfc8dc6f6   KaiGai Kohei   [JFFS2] Tidy up f...
481

2a7dba391   Eric Paris   fs/vfs/security: ...
482
  	ret = jffs2_init_security(&f->vfs_inode, &dir_f->vfs_inode, qstr);
cfc8dc6f6   KaiGai Kohei   [JFFS2] Tidy up f...
483
484
485
486
487
  	if (ret)
  		return ret;
  	ret = jffs2_init_acl_post(&f->vfs_inode);
  	if (ret)
  		return ret;
2a7dba391   Eric Paris   fs/vfs/security: ...
488
489
  	ret = jffs2_reserve_space(c, sizeof(*rd)+qstr->len, &alloclen,
  				ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(qstr->len));
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
490

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
491
492
493
494
495
496
497
498
499
500
501
502
503
  	if (ret) {
  		/* Eep. */
  		D1(printk(KERN_DEBUG "jffs2_reserve_space() for dirent failed
  "));
  		return ret;
  	}
  
  	rd = jffs2_alloc_raw_dirent();
  	if (!rd) {
  		/* Argh. Now we treat it like a normal delete */
  		jffs2_complete_reservation(c);
  		return -ENOMEM;
  	}
ced220703   David Woodhouse   [JFFS2] semaphore...
504
  	mutex_lock(&dir_f->sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
505
506
507
  
  	rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
  	rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
2a7dba391   Eric Paris   fs/vfs/security: ...
508
  	rd->totlen = cpu_to_je32(sizeof(*rd) + qstr->len);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
509
510
511
512
513
514
  	rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
  
  	rd->pino = cpu_to_je32(dir_f->inocache->ino);
  	rd->version = cpu_to_je32(++dir_f->highest_version);
  	rd->ino = ri->ino;
  	rd->mctime = ri->ctime;
2a7dba391   Eric Paris   fs/vfs/security: ...
515
  	rd->nsize = qstr->len;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
516
517
  	rd->type = DT_REG;
  	rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
2a7dba391   Eric Paris   fs/vfs/security: ...
518
  	rd->name_crc = cpu_to_je32(crc32(0, qstr->name, qstr->len));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
519

2a7dba391   Eric Paris   fs/vfs/security: ...
520
  	fd = jffs2_write_dirent(c, dir_f, rd, qstr->name, qstr->len, ALLOC_NORMAL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
521
522
  
  	jffs2_free_raw_dirent(rd);
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
523

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
524
  	if (IS_ERR(fd)) {
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
525
  		/* dirent failed to write. Delete the inode normally
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
526
527
  		   as if it were the final unlink() */
  		jffs2_complete_reservation(c);
ced220703   David Woodhouse   [JFFS2] semaphore...
528
  		mutex_unlock(&dir_f->sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
529
530
531
532
533
534
535
536
  		return PTR_ERR(fd);
  	}
  
  	/* Link the fd into the inode's list, obsoleting an old
  	   one if necessary. */
  	jffs2_add_fd_to_list(c, fd, &dir_f->dents);
  
  	jffs2_complete_reservation(c);
ced220703   David Woodhouse   [JFFS2] semaphore...
537
  	mutex_unlock(&dir_f->sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
538
539
540
541
542
543
  
  	return 0;
  }
  
  
  int jffs2_do_unlink(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f,
3a69e0cd2   Artem B. Bityutskiy   [JFFS2] Fix JFFS2...
544
545
  		    const char *name, int namelen, struct jffs2_inode_info *dead_f,
  		    uint32_t time)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
546
547
548
  {
  	struct jffs2_raw_dirent *rd;
  	struct jffs2_full_dirent *fd;
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
549
  	uint32_t alloclen;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
550
  	int ret;
a491486a2   Joakim Tjernlund   [JFFS2] Obsolete ...
551
  	if (!jffs2_can_mark_obsolete(c)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
552
553
554
555
556
  		/* We can't mark stuff obsolete on the medium. We need to write a deletion dirent */
  
  		rd = jffs2_alloc_raw_dirent();
  		if (!rd)
  			return -ENOMEM;
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
557
  		ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
e631ddba5   Ferenc Havasi   [JFFS2] Add erase...
558
  					ALLOC_DELETION, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
559
560
561
562
  		if (ret) {
  			jffs2_free_raw_dirent(rd);
  			return ret;
  		}
ced220703   David Woodhouse   [JFFS2] semaphore...
563
  		mutex_lock(&dir_f->sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
564
565
566
567
568
569
  
  		/* Build a deletion node */
  		rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
  		rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
  		rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
  		rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
570

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
571
572
573
  		rd->pino = cpu_to_je32(dir_f->inocache->ino);
  		rd->version = cpu_to_je32(++dir_f->highest_version);
  		rd->ino = cpu_to_je32(0);
3a69e0cd2   Artem B. Bityutskiy   [JFFS2] Fix JFFS2...
574
  		rd->mctime = cpu_to_je32(time);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
575
576
577
578
  		rd->nsize = namelen;
  		rd->type = DT_UNKNOWN;
  		rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
  		rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
579
  		fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, ALLOC_DELETION);
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
580

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
581
582
583
584
  		jffs2_free_raw_dirent(rd);
  
  		if (IS_ERR(fd)) {
  			jffs2_complete_reservation(c);
ced220703   David Woodhouse   [JFFS2] semaphore...
585
  			mutex_unlock(&dir_f->sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
586
587
588
589
590
  			return PTR_ERR(fd);
  		}
  
  		/* File it. This will mark the old one obsolete. */
  		jffs2_add_fd_to_list(c, fd, &dir_f->dents);
ced220703   David Woodhouse   [JFFS2] semaphore...
591
  		mutex_unlock(&dir_f->sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
592
  	} else {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
593
  		uint32_t nhash = full_name_hash(name, namelen);
bf66737ca   Harvey Harrison   [JFFS2] fix spars...
594
  		fd = dir_f->dents;
b57486433   David Woodhouse   JFFS2 locking reg...
595
596
  		/* We don't actually want to reserve any space, but we do
  		   want to be holding the alloc_sem when we write to flash */
ced220703   David Woodhouse   [JFFS2] semaphore...
597
598
  		mutex_lock(&c->alloc_sem);
  		mutex_lock(&dir_f->sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
599

15953580e   David Woodhouse   [JFFS2] Improve g...
600
601
602
603
  		for (fd = dir_f->dents; fd; fd = fd->next) {
  			if (fd->nhash == nhash &&
  			    !memcmp(fd->name, name, namelen) &&
  			    !fd->name[namelen]) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
604
605
606
  
  				D1(printk(KERN_DEBUG "Marking old dirent node (ino #%u) @%08x obsolete
  ",
15953580e   David Woodhouse   [JFFS2] Improve g...
607
608
609
610
611
612
613
614
  					  fd->ino, ref_offset(fd->raw)));
  				jffs2_mark_node_obsolete(c, fd->raw);
  				/* We don't want to remove it from the list immediately,
  				   because that screws up getdents()/seek() semantics even
  				   more than they're screwed already. Turn it into a
  				   node-less deletion dirent instead -- a placeholder */
  				fd->raw = NULL;
  				fd->ino = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
615
616
  				break;
  			}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
617
  		}
ced220703   David Woodhouse   [JFFS2] semaphore...
618
  		mutex_unlock(&dir_f->sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
619
620
621
622
623
  	}
  
  	/* dead_f is NULL if this was a rename not a real unlink */
  	/* Also catch the !f->inocache case, where there was a dirent
  	   pointing to an inode which didn't exist. */
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
624
  	if (dead_f && dead_f->inocache) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
625

ced220703   David Woodhouse   [JFFS2] semaphore...
626
  		mutex_lock(&dead_f->sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
627

32f1a95d5   Artem B. Bityuckiy   [JFFS2] Add symli...
628
629
630
631
  		if (S_ISDIR(OFNI_EDONI_2SFFJ(dead_f)->i_mode)) {
  			while (dead_f->dents) {
  				/* There can be only deleted ones */
  				fd = dead_f->dents;
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
632

32f1a95d5   Artem B. Bityuckiy   [JFFS2] Add symli...
633
  				dead_f->dents = fd->next;
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
634

32f1a95d5   Artem B. Bityuckiy   [JFFS2] Add symli...
635
636
637
638
639
640
641
642
643
  				if (fd->ino) {
  					printk(KERN_WARNING "Deleting inode #%u with active dentry \"%s\"->ino #%u
  ",
  					       dead_f->inocache->ino, fd->name, fd->ino);
  				} else {
  					D1(printk(KERN_DEBUG "Removing deletion dirent for \"%s\" from dir ino #%u
  ",
  						fd->name, dead_f->inocache->ino));
  				}
15953580e   David Woodhouse   [JFFS2] Improve g...
644
645
  				if (fd->raw)
  					jffs2_mark_node_obsolete(c, fd->raw);
32f1a95d5   Artem B. Bityuckiy   [JFFS2] Add symli...
646
  				jffs2_free_full_dirent(fd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
647
  			}
27c72b040   David Woodhouse   [JFFS2] Track par...
648
649
650
  			dead_f->inocache->pino_nlink = 0;
  		} else
  			dead_f->inocache->pino_nlink--;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
651
  		/* NB: Caller must set inode nlink if appropriate */
ced220703   David Woodhouse   [JFFS2] semaphore...
652
  		mutex_unlock(&dead_f->sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
653
654
655
656
657
658
  	}
  
  	jffs2_complete_reservation(c);
  
  	return 0;
  }
3a69e0cd2   Artem B. Bityutskiy   [JFFS2] Fix JFFS2...
659
  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)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
660
661
662
  {
  	struct jffs2_raw_dirent *rd;
  	struct jffs2_full_dirent *fd;
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
663
  	uint32_t alloclen;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
664
665
666
667
668
  	int ret;
  
  	rd = jffs2_alloc_raw_dirent();
  	if (!rd)
  		return -ENOMEM;
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
669
  	ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
e631ddba5   Ferenc Havasi   [JFFS2] Add erase...
670
  				ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
671
672
673
674
  	if (ret) {
  		jffs2_free_raw_dirent(rd);
  		return ret;
  	}
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
675

ced220703   David Woodhouse   [JFFS2] semaphore...
676
  	mutex_lock(&dir_f->sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
677
678
679
680
681
682
683
684
685
686
  
  	/* Build a deletion node */
  	rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
  	rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
  	rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
  	rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
  
  	rd->pino = cpu_to_je32(dir_f->inocache->ino);
  	rd->version = cpu_to_je32(++dir_f->highest_version);
  	rd->ino = cpu_to_je32(ino);
3a69e0cd2   Artem B. Bityutskiy   [JFFS2] Fix JFFS2...
687
  	rd->mctime = cpu_to_je32(time);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
688
689
690
691
692
693
  	rd->nsize = namelen;
  
  	rd->type = type;
  
  	rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
  	rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
694
  	fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, ALLOC_NORMAL);
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
695

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
696
697
698
699
  	jffs2_free_raw_dirent(rd);
  
  	if (IS_ERR(fd)) {
  		jffs2_complete_reservation(c);
ced220703   David Woodhouse   [JFFS2] semaphore...
700
  		mutex_unlock(&dir_f->sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
701
702
703
704
705
706
707
  		return PTR_ERR(fd);
  	}
  
  	/* File it. This will mark the old one obsolete. */
  	jffs2_add_fd_to_list(c, fd, &dir_f->dents);
  
  	jffs2_complete_reservation(c);
ced220703   David Woodhouse   [JFFS2] semaphore...
708
  	mutex_unlock(&dir_f->sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
709
710
711
  
  	return 0;
  }