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