xref: /openbmc/linux/fs/jffs2/dir.c (revision 1da177e4)
1 /*
2  * JFFS2 -- Journalling Flash File System, Version 2.
3  *
4  * Copyright (C) 2001-2003 Red Hat, Inc.
5  *
6  * Created by David Woodhouse <dwmw2@infradead.org>
7  *
8  * For licensing information, see the file 'LICENCE' in this directory.
9  *
10  * $Id: dir.c,v 1.84 2004/11/16 20:36:11 dwmw2 Exp $
11  *
12  */
13 
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
16 #include <linux/sched.h>
17 #include <linux/fs.h>
18 #include <linux/crc32.h>
19 #include <linux/jffs2.h>
20 #include <linux/jffs2_fs_i.h>
21 #include <linux/jffs2_fs_sb.h>
22 #include <linux/time.h>
23 #include "nodelist.h"
24 
25 /* Urgh. Please tell me there's a nicer way of doing these. */
26 #include <linux/version.h>
27 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,48)
28 typedef int mknod_arg_t;
29 #define NAMEI_COMPAT(x) ((void *)x)
30 #else
31 typedef dev_t mknod_arg_t;
32 #define NAMEI_COMPAT(x) (x)
33 #endif
34 
35 static int jffs2_readdir (struct file *, void *, filldir_t);
36 
37 static int jffs2_create (struct inode *,struct dentry *,int,
38 			 struct nameidata *);
39 static struct dentry *jffs2_lookup (struct inode *,struct dentry *,
40 				    struct nameidata *);
41 static int jffs2_link (struct dentry *,struct inode *,struct dentry *);
42 static int jffs2_unlink (struct inode *,struct dentry *);
43 static int jffs2_symlink (struct inode *,struct dentry *,const char *);
44 static int jffs2_mkdir (struct inode *,struct dentry *,int);
45 static int jffs2_rmdir (struct inode *,struct dentry *);
46 static int jffs2_mknod (struct inode *,struct dentry *,int,mknod_arg_t);
47 static int jffs2_rename (struct inode *, struct dentry *,
48                         struct inode *, struct dentry *);
49 
50 struct file_operations jffs2_dir_operations =
51 {
52 	.read =		generic_read_dir,
53 	.readdir =	jffs2_readdir,
54 	.ioctl =	jffs2_ioctl,
55 	.fsync =	jffs2_fsync
56 };
57 
58 
59 struct inode_operations jffs2_dir_inode_operations =
60 {
61 	.create =	NAMEI_COMPAT(jffs2_create),
62 	.lookup =	NAMEI_COMPAT(jffs2_lookup),
63 	.link =		jffs2_link,
64 	.unlink =	jffs2_unlink,
65 	.symlink =	jffs2_symlink,
66 	.mkdir =	jffs2_mkdir,
67 	.rmdir =	jffs2_rmdir,
68 	.mknod =	jffs2_mknod,
69 	.rename =	jffs2_rename,
70 	.setattr =	jffs2_setattr,
71 };
72 
73 /***********************************************************************/
74 
75 
76 /* We keep the dirent list sorted in increasing order of name hash,
77    and we use the same hash function as the dentries. Makes this
78    nice and simple
79 */
80 static struct dentry *jffs2_lookup(struct inode *dir_i, struct dentry *target,
81 				   struct nameidata *nd)
82 {
83 	struct jffs2_inode_info *dir_f;
84 	struct jffs2_sb_info *c;
85 	struct jffs2_full_dirent *fd = NULL, *fd_list;
86 	uint32_t ino = 0;
87 	struct inode *inode = NULL;
88 
89 	D1(printk(KERN_DEBUG "jffs2_lookup()\n"));
90 
91 	dir_f = JFFS2_INODE_INFO(dir_i);
92 	c = JFFS2_SB_INFO(dir_i->i_sb);
93 
94 	down(&dir_f->sem);
95 
96 	/* NB: The 2.2 backport will need to explicitly check for '.' and '..' here */
97 	for (fd_list = dir_f->dents; fd_list && fd_list->nhash <= target->d_name.hash; fd_list = fd_list->next) {
98 		if (fd_list->nhash == target->d_name.hash &&
99 		    (!fd || fd_list->version > fd->version) &&
100 		    strlen(fd_list->name) == target->d_name.len &&
101 		    !strncmp(fd_list->name, target->d_name.name, target->d_name.len)) {
102 			fd = fd_list;
103 		}
104 	}
105 	if (fd)
106 		ino = fd->ino;
107 	up(&dir_f->sem);
108 	if (ino) {
109 		inode = iget(dir_i->i_sb, ino);
110 		if (!inode) {
111 			printk(KERN_WARNING "iget() failed for ino #%u\n", ino);
112 			return (ERR_PTR(-EIO));
113 		}
114 	}
115 
116 	d_add(target, inode);
117 
118 	return NULL;
119 }
120 
121 /***********************************************************************/
122 
123 
124 static int jffs2_readdir(struct file *filp, void *dirent, filldir_t filldir)
125 {
126 	struct jffs2_inode_info *f;
127 	struct jffs2_sb_info *c;
128 	struct inode *inode = filp->f_dentry->d_inode;
129 	struct jffs2_full_dirent *fd;
130 	unsigned long offset, curofs;
131 
132 	D1(printk(KERN_DEBUG "jffs2_readdir() for dir_i #%lu\n", filp->f_dentry->d_inode->i_ino));
133 
134 	f = JFFS2_INODE_INFO(inode);
135 	c = JFFS2_SB_INFO(inode->i_sb);
136 
137 	offset = filp->f_pos;
138 
139 	if (offset == 0) {
140 		D1(printk(KERN_DEBUG "Dirent 0: \".\", ino #%lu\n", inode->i_ino));
141 		if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
142 			goto out;
143 		offset++;
144 	}
145 	if (offset == 1) {
146 		unsigned long pino = parent_ino(filp->f_dentry);
147 		D1(printk(KERN_DEBUG "Dirent 1: \"..\", ino #%lu\n", pino));
148 		if (filldir(dirent, "..", 2, 1, pino, DT_DIR) < 0)
149 			goto out;
150 		offset++;
151 	}
152 
153 	curofs=1;
154 	down(&f->sem);
155 	for (fd = f->dents; fd; fd = fd->next) {
156 
157 		curofs++;
158 		/* First loop: curofs = 2; offset = 2 */
159 		if (curofs < offset) {
160 			D2(printk(KERN_DEBUG "Skipping dirent: \"%s\", ino #%u, type %d, because curofs %ld < offset %ld\n",
161 				  fd->name, fd->ino, fd->type, curofs, offset));
162 			continue;
163 		}
164 		if (!fd->ino) {
165 			D2(printk(KERN_DEBUG "Skipping deletion dirent \"%s\"\n", fd->name));
166 			offset++;
167 			continue;
168 		}
169 		D2(printk(KERN_DEBUG "Dirent %ld: \"%s\", ino #%u, type %d\n", offset, fd->name, fd->ino, fd->type));
170 		if (filldir(dirent, fd->name, strlen(fd->name), offset, fd->ino, fd->type) < 0)
171 			break;
172 		offset++;
173 	}
174 	up(&f->sem);
175  out:
176 	filp->f_pos = offset;
177 	return 0;
178 }
179 
180 /***********************************************************************/
181 
182 
183 static int jffs2_create(struct inode *dir_i, struct dentry *dentry, int mode,
184 			struct nameidata *nd)
185 {
186 	struct jffs2_raw_inode *ri;
187 	struct jffs2_inode_info *f, *dir_f;
188 	struct jffs2_sb_info *c;
189 	struct inode *inode;
190 	int ret;
191 
192 	ri = jffs2_alloc_raw_inode();
193 	if (!ri)
194 		return -ENOMEM;
195 
196 	c = JFFS2_SB_INFO(dir_i->i_sb);
197 
198 	D1(printk(KERN_DEBUG "jffs2_create()\n"));
199 
200 	inode = jffs2_new_inode(dir_i, mode, ri);
201 
202 	if (IS_ERR(inode)) {
203 		D1(printk(KERN_DEBUG "jffs2_new_inode() failed\n"));
204 		jffs2_free_raw_inode(ri);
205 		return PTR_ERR(inode);
206 	}
207 
208 	inode->i_op = &jffs2_file_inode_operations;
209 	inode->i_fop = &jffs2_file_operations;
210 	inode->i_mapping->a_ops = &jffs2_file_address_operations;
211 	inode->i_mapping->nrpages = 0;
212 
213 	f = JFFS2_INODE_INFO(inode);
214 	dir_f = JFFS2_INODE_INFO(dir_i);
215 
216 	ret = jffs2_do_create(c, dir_f, f, ri,
217 			      dentry->d_name.name, dentry->d_name.len);
218 
219 	if (ret) {
220 		make_bad_inode(inode);
221 		iput(inode);
222 		jffs2_free_raw_inode(ri);
223 		return ret;
224 	}
225 
226 	dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(ri->ctime));
227 
228 	jffs2_free_raw_inode(ri);
229 	d_instantiate(dentry, inode);
230 
231 	D1(printk(KERN_DEBUG "jffs2_create: Created ino #%lu with mode %o, nlink %d(%d). nrpages %ld\n",
232 		  inode->i_ino, inode->i_mode, inode->i_nlink, f->inocache->nlink, inode->i_mapping->nrpages));
233 	return 0;
234 }
235 
236 /***********************************************************************/
237 
238 
239 static int jffs2_unlink(struct inode *dir_i, struct dentry *dentry)
240 {
241 	struct jffs2_sb_info *c = JFFS2_SB_INFO(dir_i->i_sb);
242 	struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i);
243 	struct jffs2_inode_info *dead_f = JFFS2_INODE_INFO(dentry->d_inode);
244 	int ret;
245 
246 	ret = jffs2_do_unlink(c, dir_f, dentry->d_name.name,
247 			       dentry->d_name.len, dead_f);
248 	if (dead_f->inocache)
249 		dentry->d_inode->i_nlink = dead_f->inocache->nlink;
250 	return ret;
251 }
252 /***********************************************************************/
253 
254 
255 static int jffs2_link (struct dentry *old_dentry, struct inode *dir_i, struct dentry *dentry)
256 {
257 	struct jffs2_sb_info *c = JFFS2_SB_INFO(old_dentry->d_inode->i_sb);
258 	struct jffs2_inode_info *f = JFFS2_INODE_INFO(old_dentry->d_inode);
259 	struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i);
260 	int ret;
261 	uint8_t type;
262 
263 	/* Don't let people make hard links to bad inodes. */
264 	if (!f->inocache)
265 		return -EIO;
266 
267 	if (S_ISDIR(old_dentry->d_inode->i_mode))
268 		return -EPERM;
269 
270 	/* XXX: This is ugly */
271 	type = (old_dentry->d_inode->i_mode & S_IFMT) >> 12;
272 	if (!type) type = DT_REG;
273 
274 	ret = jffs2_do_link(c, dir_f, f->inocache->ino, type, dentry->d_name.name, dentry->d_name.len);
275 
276 	if (!ret) {
277 		down(&f->sem);
278 		old_dentry->d_inode->i_nlink = ++f->inocache->nlink;
279 		up(&f->sem);
280 		d_instantiate(dentry, old_dentry->d_inode);
281 		atomic_inc(&old_dentry->d_inode->i_count);
282 	}
283 	return ret;
284 }
285 
286 /***********************************************************************/
287 
288 static int jffs2_symlink (struct inode *dir_i, struct dentry *dentry, const char *target)
289 {
290 	struct jffs2_inode_info *f, *dir_f;
291 	struct jffs2_sb_info *c;
292 	struct inode *inode;
293 	struct jffs2_raw_inode *ri;
294 	struct jffs2_raw_dirent *rd;
295 	struct jffs2_full_dnode *fn;
296 	struct jffs2_full_dirent *fd;
297 	int namelen;
298 	uint32_t alloclen, phys_ofs;
299 	int ret;
300 
301 	/* FIXME: If you care. We'd need to use frags for the target
302 	   if it grows much more than this */
303 	if (strlen(target) > 254)
304 		return -EINVAL;
305 
306 	ri = jffs2_alloc_raw_inode();
307 
308 	if (!ri)
309 		return -ENOMEM;
310 
311 	c = JFFS2_SB_INFO(dir_i->i_sb);
312 
313 	/* Try to reserve enough space for both node and dirent.
314 	 * Just the node will do for now, though
315 	 */
316 	namelen = dentry->d_name.len;
317 	ret = jffs2_reserve_space(c, sizeof(*ri) + strlen(target), &phys_ofs, &alloclen, ALLOC_NORMAL);
318 
319 	if (ret) {
320 		jffs2_free_raw_inode(ri);
321 		return ret;
322 	}
323 
324 	inode = jffs2_new_inode(dir_i, S_IFLNK | S_IRWXUGO, ri);
325 
326 	if (IS_ERR(inode)) {
327 		jffs2_free_raw_inode(ri);
328 		jffs2_complete_reservation(c);
329 		return PTR_ERR(inode);
330 	}
331 
332 	inode->i_op = &jffs2_symlink_inode_operations;
333 
334 	f = JFFS2_INODE_INFO(inode);
335 
336 	inode->i_size = strlen(target);
337 	ri->isize = ri->dsize = ri->csize = cpu_to_je32(inode->i_size);
338 	ri->totlen = cpu_to_je32(sizeof(*ri) + inode->i_size);
339 	ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
340 
341 	ri->compr = JFFS2_COMPR_NONE;
342 	ri->data_crc = cpu_to_je32(crc32(0, target, strlen(target)));
343 	ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
344 
345 	fn = jffs2_write_dnode(c, f, ri, target, strlen(target), phys_ofs, ALLOC_NORMAL);
346 
347 	jffs2_free_raw_inode(ri);
348 
349 	if (IS_ERR(fn)) {
350 		/* Eeek. Wave bye bye */
351 		up(&f->sem);
352 		jffs2_complete_reservation(c);
353 		jffs2_clear_inode(inode);
354 		return PTR_ERR(fn);
355 	}
356 	/* No data here. Only a metadata node, which will be
357 	   obsoleted by the first data write
358 	*/
359 	f->metadata = fn;
360 	up(&f->sem);
361 
362 	jffs2_complete_reservation(c);
363 	ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen, ALLOC_NORMAL);
364 	if (ret) {
365 		/* Eep. */
366 		jffs2_clear_inode(inode);
367 		return ret;
368 	}
369 
370 	rd = jffs2_alloc_raw_dirent();
371 	if (!rd) {
372 		/* Argh. Now we treat it like a normal delete */
373 		jffs2_complete_reservation(c);
374 		jffs2_clear_inode(inode);
375 		return -ENOMEM;
376 	}
377 
378 	dir_f = JFFS2_INODE_INFO(dir_i);
379 	down(&dir_f->sem);
380 
381 	rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
382 	rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
383 	rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
384 	rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
385 
386 	rd->pino = cpu_to_je32(dir_i->i_ino);
387 	rd->version = cpu_to_je32(++dir_f->highest_version);
388 	rd->ino = cpu_to_je32(inode->i_ino);
389 	rd->mctime = cpu_to_je32(get_seconds());
390 	rd->nsize = namelen;
391 	rd->type = DT_LNK;
392 	rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
393 	rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
394 
395 	fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, phys_ofs, ALLOC_NORMAL);
396 
397 	if (IS_ERR(fd)) {
398 		/* dirent failed to write. Delete the inode normally
399 		   as if it were the final unlink() */
400 		jffs2_complete_reservation(c);
401 		jffs2_free_raw_dirent(rd);
402 		up(&dir_f->sem);
403 		jffs2_clear_inode(inode);
404 		return PTR_ERR(fd);
405 	}
406 
407 	dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
408 
409 	jffs2_free_raw_dirent(rd);
410 
411 	/* Link the fd into the inode's list, obsoleting an old
412 	   one if necessary. */
413 	jffs2_add_fd_to_list(c, fd, &dir_f->dents);
414 
415 	up(&dir_f->sem);
416 	jffs2_complete_reservation(c);
417 
418 	d_instantiate(dentry, inode);
419 	return 0;
420 }
421 
422 
423 static int jffs2_mkdir (struct inode *dir_i, struct dentry *dentry, int mode)
424 {
425 	struct jffs2_inode_info *f, *dir_f;
426 	struct jffs2_sb_info *c;
427 	struct inode *inode;
428 	struct jffs2_raw_inode *ri;
429 	struct jffs2_raw_dirent *rd;
430 	struct jffs2_full_dnode *fn;
431 	struct jffs2_full_dirent *fd;
432 	int namelen;
433 	uint32_t alloclen, phys_ofs;
434 	int ret;
435 
436 	mode |= S_IFDIR;
437 
438 	ri = jffs2_alloc_raw_inode();
439 	if (!ri)
440 		return -ENOMEM;
441 
442 	c = JFFS2_SB_INFO(dir_i->i_sb);
443 
444 	/* Try to reserve enough space for both node and dirent.
445 	 * Just the node will do for now, though
446 	 */
447 	namelen = dentry->d_name.len;
448 	ret = jffs2_reserve_space(c, sizeof(*ri), &phys_ofs, &alloclen, ALLOC_NORMAL);
449 
450 	if (ret) {
451 		jffs2_free_raw_inode(ri);
452 		return ret;
453 	}
454 
455 	inode = jffs2_new_inode(dir_i, mode, ri);
456 
457 	if (IS_ERR(inode)) {
458 		jffs2_free_raw_inode(ri);
459 		jffs2_complete_reservation(c);
460 		return PTR_ERR(inode);
461 	}
462 
463 	inode->i_op = &jffs2_dir_inode_operations;
464 	inode->i_fop = &jffs2_dir_operations;
465 	/* Directories get nlink 2 at start */
466 	inode->i_nlink = 2;
467 
468 	f = JFFS2_INODE_INFO(inode);
469 
470 	ri->data_crc = cpu_to_je32(0);
471 	ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
472 
473 	fn = jffs2_write_dnode(c, f, ri, NULL, 0, phys_ofs, ALLOC_NORMAL);
474 
475 	jffs2_free_raw_inode(ri);
476 
477 	if (IS_ERR(fn)) {
478 		/* Eeek. Wave bye bye */
479 		up(&f->sem);
480 		jffs2_complete_reservation(c);
481 		jffs2_clear_inode(inode);
482 		return PTR_ERR(fn);
483 	}
484 	/* No data here. Only a metadata node, which will be
485 	   obsoleted by the first data write
486 	*/
487 	f->metadata = fn;
488 	up(&f->sem);
489 
490 	jffs2_complete_reservation(c);
491 	ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen, ALLOC_NORMAL);
492 	if (ret) {
493 		/* Eep. */
494 		jffs2_clear_inode(inode);
495 		return ret;
496 	}
497 
498 	rd = jffs2_alloc_raw_dirent();
499 	if (!rd) {
500 		/* Argh. Now we treat it like a normal delete */
501 		jffs2_complete_reservation(c);
502 		jffs2_clear_inode(inode);
503 		return -ENOMEM;
504 	}
505 
506 	dir_f = JFFS2_INODE_INFO(dir_i);
507 	down(&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) + namelen);
512 	rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
513 
514 	rd->pino = cpu_to_je32(dir_i->i_ino);
515 	rd->version = cpu_to_je32(++dir_f->highest_version);
516 	rd->ino = cpu_to_je32(inode->i_ino);
517 	rd->mctime = cpu_to_je32(get_seconds());
518 	rd->nsize = namelen;
519 	rd->type = DT_DIR;
520 	rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
521 	rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
522 
523 	fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, phys_ofs, ALLOC_NORMAL);
524 
525 	if (IS_ERR(fd)) {
526 		/* dirent failed to write. Delete the inode normally
527 		   as if it were the final unlink() */
528 		jffs2_complete_reservation(c);
529 		jffs2_free_raw_dirent(rd);
530 		up(&dir_f->sem);
531 		jffs2_clear_inode(inode);
532 		return PTR_ERR(fd);
533 	}
534 
535 	dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
536 	dir_i->i_nlink++;
537 
538 	jffs2_free_raw_dirent(rd);
539 
540 	/* Link the fd into the inode's list, obsoleting an old
541 	   one if necessary. */
542 	jffs2_add_fd_to_list(c, fd, &dir_f->dents);
543 
544 	up(&dir_f->sem);
545 	jffs2_complete_reservation(c);
546 
547 	d_instantiate(dentry, inode);
548 	return 0;
549 }
550 
551 static int jffs2_rmdir (struct inode *dir_i, struct dentry *dentry)
552 {
553 	struct jffs2_inode_info *f = JFFS2_INODE_INFO(dentry->d_inode);
554 	struct jffs2_full_dirent *fd;
555 	int ret;
556 
557 	for (fd = f->dents ; fd; fd = fd->next) {
558 		if (fd->ino)
559 			return -ENOTEMPTY;
560 	}
561 	ret = jffs2_unlink(dir_i, dentry);
562 	if (!ret)
563 		dir_i->i_nlink--;
564 	return ret;
565 }
566 
567 static int jffs2_mknod (struct inode *dir_i, struct dentry *dentry, int mode, mknod_arg_t rdev)
568 {
569 	struct jffs2_inode_info *f, *dir_f;
570 	struct jffs2_sb_info *c;
571 	struct inode *inode;
572 	struct jffs2_raw_inode *ri;
573 	struct jffs2_raw_dirent *rd;
574 	struct jffs2_full_dnode *fn;
575 	struct jffs2_full_dirent *fd;
576 	int namelen;
577 	jint16_t dev;
578 	int devlen = 0;
579 	uint32_t alloclen, phys_ofs;
580 	int ret;
581 
582 	if (!old_valid_dev(rdev))
583 		return -EINVAL;
584 
585 	ri = jffs2_alloc_raw_inode();
586 	if (!ri)
587 		return -ENOMEM;
588 
589 	c = JFFS2_SB_INFO(dir_i->i_sb);
590 
591 	if (S_ISBLK(mode) || S_ISCHR(mode)) {
592 		dev = cpu_to_je16(old_encode_dev(rdev));
593 		devlen = sizeof(dev);
594 	}
595 
596 	/* Try to reserve enough space for both node and dirent.
597 	 * Just the node will do for now, though
598 	 */
599 	namelen = dentry->d_name.len;
600 	ret = jffs2_reserve_space(c, sizeof(*ri) + devlen, &phys_ofs, &alloclen, ALLOC_NORMAL);
601 
602 	if (ret) {
603 		jffs2_free_raw_inode(ri);
604 		return ret;
605 	}
606 
607 	inode = jffs2_new_inode(dir_i, mode, ri);
608 
609 	if (IS_ERR(inode)) {
610 		jffs2_free_raw_inode(ri);
611 		jffs2_complete_reservation(c);
612 		return PTR_ERR(inode);
613 	}
614 	inode->i_op = &jffs2_file_inode_operations;
615 	init_special_inode(inode, inode->i_mode, rdev);
616 
617 	f = JFFS2_INODE_INFO(inode);
618 
619 	ri->dsize = ri->csize = cpu_to_je32(devlen);
620 	ri->totlen = cpu_to_je32(sizeof(*ri) + devlen);
621 	ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
622 
623 	ri->compr = JFFS2_COMPR_NONE;
624 	ri->data_crc = cpu_to_je32(crc32(0, &dev, devlen));
625 	ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
626 
627 	fn = jffs2_write_dnode(c, f, ri, (char *)&dev, devlen, phys_ofs, ALLOC_NORMAL);
628 
629 	jffs2_free_raw_inode(ri);
630 
631 	if (IS_ERR(fn)) {
632 		/* Eeek. Wave bye bye */
633 		up(&f->sem);
634 		jffs2_complete_reservation(c);
635 		jffs2_clear_inode(inode);
636 		return PTR_ERR(fn);
637 	}
638 	/* No data here. Only a metadata node, which will be
639 	   obsoleted by the first data write
640 	*/
641 	f->metadata = fn;
642 	up(&f->sem);
643 
644 	jffs2_complete_reservation(c);
645 	ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen, ALLOC_NORMAL);
646 	if (ret) {
647 		/* Eep. */
648 		jffs2_clear_inode(inode);
649 		return ret;
650 	}
651 
652 	rd = jffs2_alloc_raw_dirent();
653 	if (!rd) {
654 		/* Argh. Now we treat it like a normal delete */
655 		jffs2_complete_reservation(c);
656 		jffs2_clear_inode(inode);
657 		return -ENOMEM;
658 	}
659 
660 	dir_f = JFFS2_INODE_INFO(dir_i);
661 	down(&dir_f->sem);
662 
663 	rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
664 	rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
665 	rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
666 	rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
667 
668 	rd->pino = cpu_to_je32(dir_i->i_ino);
669 	rd->version = cpu_to_je32(++dir_f->highest_version);
670 	rd->ino = cpu_to_je32(inode->i_ino);
671 	rd->mctime = cpu_to_je32(get_seconds());
672 	rd->nsize = namelen;
673 
674 	/* XXX: This is ugly. */
675 	rd->type = (mode & S_IFMT) >> 12;
676 
677 	rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
678 	rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
679 
680 	fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, phys_ofs, ALLOC_NORMAL);
681 
682 	if (IS_ERR(fd)) {
683 		/* dirent failed to write. Delete the inode normally
684 		   as if it were the final unlink() */
685 		jffs2_complete_reservation(c);
686 		jffs2_free_raw_dirent(rd);
687 		up(&dir_f->sem);
688 		jffs2_clear_inode(inode);
689 		return PTR_ERR(fd);
690 	}
691 
692 	dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
693 
694 	jffs2_free_raw_dirent(rd);
695 
696 	/* Link the fd into the inode's list, obsoleting an old
697 	   one if necessary. */
698 	jffs2_add_fd_to_list(c, fd, &dir_f->dents);
699 
700 	up(&dir_f->sem);
701 	jffs2_complete_reservation(c);
702 
703 	d_instantiate(dentry, inode);
704 
705 	return 0;
706 }
707 
708 static int jffs2_rename (struct inode *old_dir_i, struct dentry *old_dentry,
709                         struct inode *new_dir_i, struct dentry *new_dentry)
710 {
711 	int ret;
712 	struct jffs2_sb_info *c = JFFS2_SB_INFO(old_dir_i->i_sb);
713 	struct jffs2_inode_info *victim_f = NULL;
714 	uint8_t type;
715 
716 	/* The VFS will check for us and prevent trying to rename a
717 	 * file over a directory and vice versa, but if it's a directory,
718 	 * the VFS can't check whether the victim is empty. The filesystem
719 	 * needs to do that for itself.
720 	 */
721 	if (new_dentry->d_inode) {
722 		victim_f = JFFS2_INODE_INFO(new_dentry->d_inode);
723 		if (S_ISDIR(new_dentry->d_inode->i_mode)) {
724 			struct jffs2_full_dirent *fd;
725 
726 			down(&victim_f->sem);
727 			for (fd = victim_f->dents; fd; fd = fd->next) {
728 				if (fd->ino) {
729 					up(&victim_f->sem);
730 					return -ENOTEMPTY;
731 				}
732 			}
733 			up(&victim_f->sem);
734 		}
735 	}
736 
737 	/* XXX: We probably ought to alloc enough space for
738 	   both nodes at the same time. Writing the new link,
739 	   then getting -ENOSPC, is quite bad :)
740 	*/
741 
742 	/* Make a hard link */
743 
744 	/* XXX: This is ugly */
745 	type = (old_dentry->d_inode->i_mode & S_IFMT) >> 12;
746 	if (!type) type = DT_REG;
747 
748 	ret = jffs2_do_link(c, JFFS2_INODE_INFO(new_dir_i),
749 			    old_dentry->d_inode->i_ino, type,
750 			    new_dentry->d_name.name, new_dentry->d_name.len);
751 
752 	if (ret)
753 		return ret;
754 
755 	if (victim_f) {
756 		/* There was a victim. Kill it off nicely */
757 		new_dentry->d_inode->i_nlink--;
758 		/* Don't oops if the victim was a dirent pointing to an
759 		   inode which didn't exist. */
760 		if (victim_f->inocache) {
761 			down(&victim_f->sem);
762 			victim_f->inocache->nlink--;
763 			up(&victim_f->sem);
764 		}
765 	}
766 
767 	/* If it was a directory we moved, and there was no victim,
768 	   increase i_nlink on its new parent */
769 	if (S_ISDIR(old_dentry->d_inode->i_mode) && !victim_f)
770 		new_dir_i->i_nlink++;
771 
772 	/* Unlink the original */
773 	ret = jffs2_do_unlink(c, JFFS2_INODE_INFO(old_dir_i),
774 		      old_dentry->d_name.name, old_dentry->d_name.len, NULL);
775 
776 	/* We don't touch inode->i_nlink */
777 
778 	if (ret) {
779 		/* Oh shit. We really ought to make a single node which can do both atomically */
780 		struct jffs2_inode_info *f = JFFS2_INODE_INFO(old_dentry->d_inode);
781 		down(&f->sem);
782 		old_dentry->d_inode->i_nlink++;
783 		if (f->inocache)
784 			f->inocache->nlink++;
785 		up(&f->sem);
786 
787 		printk(KERN_NOTICE "jffs2_rename(): Link succeeded, unlink failed (err %d). You now have a hard link\n", ret);
788 		/* Might as well let the VFS know */
789 		d_instantiate(new_dentry, old_dentry->d_inode);
790 		atomic_inc(&old_dentry->d_inode->i_count);
791 		return ret;
792 	}
793 
794 	if (S_ISDIR(old_dentry->d_inode->i_mode))
795 		old_dir_i->i_nlink--;
796 
797 	return 0;
798 }
799 
800