ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.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@redhat.com>
7  *
8  * For licensing information, see the file 'LICENCE' in this directory.
9  *
10  * $Id: dir.c,v 1.82 2003/10/11 11:47:23 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                 jffs2_clear_inode(inode);
221                 make_bad_inode(inode);
222                 iput(inode);
223                 jffs2_free_raw_inode(ri);
224                 return ret;
225         }
226
227         dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(ri->ctime));
228
229         jffs2_free_raw_inode(ri);
230         d_instantiate(dentry, inode);
231
232         D1(printk(KERN_DEBUG "jffs2_create: Created ino #%lu with mode %o, nlink %d(%d). nrpages %ld\n",
233                   inode->i_ino, inode->i_mode, inode->i_nlink, f->inocache->nlink, inode->i_mapping->nrpages));
234         return 0;
235 }
236
237 /***********************************************************************/
238
239
240 static int jffs2_unlink(struct inode *dir_i, struct dentry *dentry)
241 {
242         struct jffs2_sb_info *c = JFFS2_SB_INFO(dir_i->i_sb);
243         struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i);
244         struct jffs2_inode_info *dead_f = JFFS2_INODE_INFO(dentry->d_inode);
245         int ret;
246
247         ret = jffs2_do_unlink(c, dir_f, dentry->d_name.name, 
248                                dentry->d_name.len, dead_f);
249         if (dead_f->inocache)
250                 dentry->d_inode->i_nlink = dead_f->inocache->nlink;
251         return ret;
252 }
253 /***********************************************************************/
254
255
256 static int jffs2_link (struct dentry *old_dentry, struct inode *dir_i, struct dentry *dentry)
257 {
258         struct jffs2_sb_info *c = JFFS2_SB_INFO(old_dentry->d_inode->i_sb);
259         struct jffs2_inode_info *f = JFFS2_INODE_INFO(old_dentry->d_inode);
260         struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i);
261         int ret;
262         uint8_t type;
263
264         /* Don't let people make hard links to bad inodes. */
265         if (!f->inocache)
266                 return -EIO;
267
268         if (S_ISDIR(old_dentry->d_inode->i_mode))
269                 return -EPERM;
270
271         /* XXX: This is ugly */
272         type = (old_dentry->d_inode->i_mode & S_IFMT) >> 12;
273         if (!type) type = DT_REG;
274
275         ret = jffs2_do_link(c, dir_f, f->inocache->ino, type, dentry->d_name.name, dentry->d_name.len);
276
277         if (!ret) {
278                 down(&f->sem);
279                 old_dentry->d_inode->i_nlink = ++f->inocache->nlink;
280                 up(&f->sem);
281                 d_instantiate(dentry, old_dentry->d_inode);
282                 atomic_inc(&old_dentry->d_inode->i_count);
283         }
284         return ret;
285 }
286
287 /***********************************************************************/
288
289 static int jffs2_symlink (struct inode *dir_i, struct dentry *dentry, const char *target)
290 {
291         struct jffs2_inode_info *f, *dir_f;
292         struct jffs2_sb_info *c;
293         struct inode *inode;
294         struct jffs2_raw_inode *ri;
295         struct jffs2_raw_dirent *rd;
296         struct jffs2_full_dnode *fn;
297         struct jffs2_full_dirent *fd;
298         int namelen;
299         uint32_t alloclen, phys_ofs;
300         int ret;
301
302         /* FIXME: If you care. We'd need to use frags for the target
303            if it grows much more than this */
304         if (strlen(target) > 254)
305                 return -EINVAL;
306
307         ri = jffs2_alloc_raw_inode();
308
309         if (!ri)
310                 return -ENOMEM;
311         
312         c = JFFS2_SB_INFO(dir_i->i_sb);
313         
314         /* Try to reserve enough space for both node and dirent. 
315          * Just the node will do for now, though 
316          */
317         namelen = dentry->d_name.len;
318         ret = jffs2_reserve_space(c, sizeof(*ri) + strlen(target), &phys_ofs, &alloclen, ALLOC_NORMAL);
319
320         if (ret) {
321                 jffs2_free_raw_inode(ri);
322                 return ret;
323         }
324
325         inode = jffs2_new_inode(dir_i, S_IFLNK | S_IRWXUGO, ri);
326
327         if (IS_ERR(inode)) {
328                 jffs2_free_raw_inode(ri);
329                 jffs2_complete_reservation(c);
330                 return PTR_ERR(inode);
331         }
332
333         inode->i_op = &jffs2_symlink_inode_operations;
334
335         f = JFFS2_INODE_INFO(inode);
336
337         inode->i_size = strlen(target);
338         ri->isize = ri->dsize = ri->csize = cpu_to_je32(inode->i_size);
339         ri->totlen = cpu_to_je32(sizeof(*ri) + inode->i_size);
340         ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
341
342         ri->compr = JFFS2_COMPR_NONE;
343         ri->data_crc = cpu_to_je32(crc32(0, target, strlen(target)));
344         ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
345         
346         fn = jffs2_write_dnode(c, f, ri, target, strlen(target), phys_ofs, ALLOC_NORMAL);
347
348         jffs2_free_raw_inode(ri);
349
350         if (IS_ERR(fn)) {
351                 /* Eeek. Wave bye bye */
352                 up(&f->sem);
353                 jffs2_complete_reservation(c);
354                 jffs2_clear_inode(inode);
355                 return PTR_ERR(fn);
356         }
357         /* No data here. Only a metadata node, which will be 
358            obsoleted by the first data write
359         */
360         f->metadata = fn;
361         up(&f->sem);
362
363         jffs2_complete_reservation(c);
364         ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen, ALLOC_NORMAL);
365         if (ret) {
366                 /* Eep. */
367                 jffs2_clear_inode(inode);
368                 return ret;
369         }
370
371         rd = jffs2_alloc_raw_dirent();
372         if (!rd) {
373                 /* Argh. Now we treat it like a normal delete */
374                 jffs2_complete_reservation(c);
375                 jffs2_clear_inode(inode);
376                 return -ENOMEM;
377         }
378
379         dir_f = JFFS2_INODE_INFO(dir_i);
380         down(&dir_f->sem);
381
382         rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
383         rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
384         rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
385         rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
386
387         rd->pino = cpu_to_je32(dir_i->i_ino);
388         rd->version = cpu_to_je32(++dir_f->highest_version);
389         rd->ino = cpu_to_je32(inode->i_ino);
390         rd->mctime = cpu_to_je32(get_seconds());
391         rd->nsize = namelen;
392         rd->type = DT_LNK;
393         rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
394         rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
395
396         fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, phys_ofs, ALLOC_NORMAL);
397
398         if (IS_ERR(fd)) {
399                 /* dirent failed to write. Delete the inode normally 
400                    as if it were the final unlink() */
401                 jffs2_complete_reservation(c);
402                 jffs2_free_raw_dirent(rd);
403                 up(&dir_f->sem);
404                 jffs2_clear_inode(inode);
405                 return PTR_ERR(fd);
406         }
407
408         dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
409
410         jffs2_free_raw_dirent(rd);
411
412         /* Link the fd into the inode's list, obsoleting an old
413            one if necessary. */
414         jffs2_add_fd_to_list(c, fd, &dir_f->dents);
415
416         up(&dir_f->sem);
417         jffs2_complete_reservation(c);
418
419         d_instantiate(dentry, inode);
420         return 0;
421 }
422
423
424 static int jffs2_mkdir (struct inode *dir_i, struct dentry *dentry, int mode)
425 {
426         struct jffs2_inode_info *f, *dir_f;
427         struct jffs2_sb_info *c;
428         struct inode *inode;
429         struct jffs2_raw_inode *ri;
430         struct jffs2_raw_dirent *rd;
431         struct jffs2_full_dnode *fn;
432         struct jffs2_full_dirent *fd;
433         int namelen;
434         uint32_t alloclen, phys_ofs;
435         int ret;
436
437         mode |= S_IFDIR;
438
439         ri = jffs2_alloc_raw_inode();
440         if (!ri)
441                 return -ENOMEM;
442         
443         c = JFFS2_SB_INFO(dir_i->i_sb);
444
445         /* Try to reserve enough space for both node and dirent. 
446          * Just the node will do for now, though 
447          */
448         namelen = dentry->d_name.len;
449         ret = jffs2_reserve_space(c, sizeof(*ri), &phys_ofs, &alloclen, ALLOC_NORMAL);
450
451         if (ret) {
452                 jffs2_free_raw_inode(ri);
453                 return ret;
454         }
455
456         inode = jffs2_new_inode(dir_i, mode, ri);
457
458         if (IS_ERR(inode)) {
459                 jffs2_free_raw_inode(ri);
460                 jffs2_complete_reservation(c);
461                 return PTR_ERR(inode);
462         }
463
464         inode->i_op = &jffs2_dir_inode_operations;
465         inode->i_fop = &jffs2_dir_operations;
466         /* Directories get nlink 2 at start */
467         inode->i_nlink = 2;
468
469         f = JFFS2_INODE_INFO(inode);
470
471         ri->data_crc = cpu_to_je32(0);
472         ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
473         
474         fn = jffs2_write_dnode(c, f, ri, NULL, 0, phys_ofs, ALLOC_NORMAL);
475
476         jffs2_free_raw_inode(ri);
477
478         if (IS_ERR(fn)) {
479                 /* Eeek. Wave bye bye */
480                 up(&f->sem);
481                 jffs2_complete_reservation(c);
482                 jffs2_clear_inode(inode);
483                 return PTR_ERR(fn);
484         }
485         /* No data here. Only a metadata node, which will be 
486            obsoleted by the first data write
487         */
488         f->metadata = fn;
489         up(&f->sem);
490
491         jffs2_complete_reservation(c);
492         ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen, ALLOC_NORMAL);
493         if (ret) {
494                 /* Eep. */
495                 jffs2_clear_inode(inode);
496                 return ret;
497         }
498         
499         rd = jffs2_alloc_raw_dirent();
500         if (!rd) {
501                 /* Argh. Now we treat it like a normal delete */
502                 jffs2_complete_reservation(c);
503                 jffs2_clear_inode(inode);
504                 return -ENOMEM;
505         }
506
507         dir_f = JFFS2_INODE_INFO(dir_i);
508         down(&dir_f->sem);
509
510         rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
511         rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
512         rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
513         rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
514
515         rd->pino = cpu_to_je32(dir_i->i_ino);
516         rd->version = cpu_to_je32(++dir_f->highest_version);
517         rd->ino = cpu_to_je32(inode->i_ino);
518         rd->mctime = cpu_to_je32(get_seconds());
519         rd->nsize = namelen;
520         rd->type = DT_DIR;
521         rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
522         rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
523
524         fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, phys_ofs, ALLOC_NORMAL);
525         
526         if (IS_ERR(fd)) {
527                 /* dirent failed to write. Delete the inode normally 
528                    as if it were the final unlink() */
529                 jffs2_complete_reservation(c);
530                 jffs2_free_raw_dirent(rd);
531                 up(&dir_f->sem);
532                 jffs2_clear_inode(inode);
533                 return PTR_ERR(fd);
534         }
535
536         dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
537         dir_i->i_nlink++;
538
539         jffs2_free_raw_dirent(rd);
540
541         /* Link the fd into the inode's list, obsoleting an old
542            one if necessary. */
543         jffs2_add_fd_to_list(c, fd, &dir_f->dents);
544
545         up(&dir_f->sem);
546         jffs2_complete_reservation(c);
547
548         d_instantiate(dentry, inode);
549         return 0;
550 }
551
552 static int jffs2_rmdir (struct inode *dir_i, struct dentry *dentry)
553 {
554         struct jffs2_inode_info *f = JFFS2_INODE_INFO(dentry->d_inode);
555         struct jffs2_full_dirent *fd;
556         int ret;
557
558         for (fd = f->dents ; fd; fd = fd->next) {
559                 if (fd->ino)
560                         return -ENOTEMPTY;
561         }
562         ret = jffs2_unlink(dir_i, dentry);
563         if (!ret)
564                 dir_i->i_nlink--;
565         return ret;
566 }
567
568 static int jffs2_mknod (struct inode *dir_i, struct dentry *dentry, int mode, mknod_arg_t rdev)
569 {
570         struct jffs2_inode_info *f, *dir_f;
571         struct jffs2_sb_info *c;
572         struct inode *inode;
573         struct jffs2_raw_inode *ri;
574         struct jffs2_raw_dirent *rd;
575         struct jffs2_full_dnode *fn;
576         struct jffs2_full_dirent *fd;
577         int namelen;
578         jint16_t dev;
579         int devlen = 0;
580         uint32_t alloclen, phys_ofs;
581         int ret;
582
583         if (!old_valid_dev(rdev))
584                 return -EINVAL;
585
586         ri = jffs2_alloc_raw_inode();
587         if (!ri)
588                 return -ENOMEM;
589         
590         c = JFFS2_SB_INFO(dir_i->i_sb);
591         
592         if (S_ISBLK(mode) || S_ISCHR(mode)) {
593                 dev = cpu_to_je16(old_encode_dev(rdev));
594                 devlen = sizeof(dev);
595         }
596         
597         /* Try to reserve enough space for both node and dirent. 
598          * Just the node will do for now, though 
599          */
600         namelen = dentry->d_name.len;
601         ret = jffs2_reserve_space(c, sizeof(*ri) + devlen, &phys_ofs, &alloclen, ALLOC_NORMAL);
602
603         if (ret) {
604                 jffs2_free_raw_inode(ri);
605                 return ret;
606         }
607
608         inode = jffs2_new_inode(dir_i, mode, ri);
609
610         if (IS_ERR(inode)) {
611                 jffs2_free_raw_inode(ri);
612                 jffs2_complete_reservation(c);
613                 return PTR_ERR(inode);
614         }
615         inode->i_op = &jffs2_file_inode_operations;
616         init_special_inode(inode, inode->i_mode, rdev);
617
618         f = JFFS2_INODE_INFO(inode);
619
620         ri->dsize = ri->csize = cpu_to_je32(devlen);
621         ri->totlen = cpu_to_je32(sizeof(*ri) + devlen);
622         ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
623
624         ri->compr = JFFS2_COMPR_NONE;
625         ri->data_crc = cpu_to_je32(crc32(0, &dev, devlen));
626         ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
627         
628         fn = jffs2_write_dnode(c, f, ri, (char *)&dev, devlen, phys_ofs, ALLOC_NORMAL);
629
630         jffs2_free_raw_inode(ri);
631
632         if (IS_ERR(fn)) {
633                 /* Eeek. Wave bye bye */
634                 up(&f->sem);
635                 jffs2_complete_reservation(c);
636                 jffs2_clear_inode(inode);
637                 return PTR_ERR(fn);
638         }
639         /* No data here. Only a metadata node, which will be 
640            obsoleted by the first data write
641         */
642         f->metadata = fn;
643         up(&f->sem);
644
645         jffs2_complete_reservation(c);
646         ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen, ALLOC_NORMAL);
647         if (ret) {
648                 /* Eep. */
649                 jffs2_clear_inode(inode);
650                 return ret;
651         }
652
653         rd = jffs2_alloc_raw_dirent();
654         if (!rd) {
655                 /* Argh. Now we treat it like a normal delete */
656                 jffs2_complete_reservation(c);
657                 jffs2_clear_inode(inode);
658                 return -ENOMEM;
659         }
660
661         dir_f = JFFS2_INODE_INFO(dir_i);
662         down(&dir_f->sem);
663
664         rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
665         rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
666         rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
667         rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
668
669         rd->pino = cpu_to_je32(dir_i->i_ino);
670         rd->version = cpu_to_je32(++dir_f->highest_version);
671         rd->ino = cpu_to_je32(inode->i_ino);
672         rd->mctime = cpu_to_je32(get_seconds());
673         rd->nsize = namelen;
674
675         /* XXX: This is ugly. */
676         rd->type = (mode & S_IFMT) >> 12;
677
678         rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
679         rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
680
681         fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, phys_ofs, ALLOC_NORMAL);
682         
683         if (IS_ERR(fd)) {
684                 /* dirent failed to write. Delete the inode normally 
685                    as if it were the final unlink() */
686                 jffs2_complete_reservation(c);
687                 jffs2_free_raw_dirent(rd);
688                 up(&dir_f->sem);
689                 jffs2_clear_inode(inode);
690                 return PTR_ERR(fd);
691         }
692
693         dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
694
695         jffs2_free_raw_dirent(rd);
696
697         /* Link the fd into the inode's list, obsoleting an old
698            one if necessary. */
699         jffs2_add_fd_to_list(c, fd, &dir_f->dents);
700
701         up(&dir_f->sem);
702         jffs2_complete_reservation(c);
703
704         d_instantiate(dentry, inode);
705
706         return 0;
707 }
708
709 static int jffs2_rename (struct inode *old_dir_i, struct dentry *old_dentry,
710                         struct inode *new_dir_i, struct dentry *new_dentry)
711 {
712         int ret;
713         struct jffs2_sb_info *c = JFFS2_SB_INFO(old_dir_i->i_sb);
714         struct jffs2_inode_info *victim_f = NULL;
715         uint8_t type;
716
717         /* The VFS will check for us and prevent trying to rename a 
718          * file over a directory and vice versa, but if it's a directory,
719          * the VFS can't check whether the victim is empty. The filesystem
720          * needs to do that for itself.
721          */
722         if (new_dentry->d_inode) {
723                 victim_f = JFFS2_INODE_INFO(new_dentry->d_inode);
724                 if (S_ISDIR(new_dentry->d_inode->i_mode)) {
725                         struct jffs2_full_dirent *fd;
726
727                         down(&victim_f->sem);
728                         for (fd = victim_f->dents; fd; fd = fd->next) {
729                                 if (fd->ino) {
730                                         up(&victim_f->sem);
731                                         return -ENOTEMPTY;
732                                 }
733                         }
734                         up(&victim_f->sem);
735                 }
736         }
737
738         /* XXX: We probably ought to alloc enough space for
739            both nodes at the same time. Writing the new link, 
740            then getting -ENOSPC, is quite bad :)
741         */
742
743         /* Make a hard link */
744         
745         /* XXX: This is ugly */
746         type = (old_dentry->d_inode->i_mode & S_IFMT) >> 12;
747         if (!type) type = DT_REG;
748
749         ret = jffs2_do_link(c, JFFS2_INODE_INFO(new_dir_i), 
750                             old_dentry->d_inode->i_ino, type,
751                             new_dentry->d_name.name, new_dentry->d_name.len);
752
753         if (ret)
754                 return ret;
755
756         if (victim_f) {
757                 /* There was a victim. Kill it off nicely */
758                 new_dentry->d_inode->i_nlink--;
759                 /* Don't oops if the victim was a dirent pointing to an
760                    inode which didn't exist. */
761                 if (victim_f->inocache) {
762                         down(&victim_f->sem);
763                         victim_f->inocache->nlink--;
764                         up(&victim_f->sem);
765                 }
766         }
767
768         /* If it was a directory we moved, and there was no victim, 
769            increase i_nlink on its new parent */
770         if (S_ISDIR(old_dentry->d_inode->i_mode) && !victim_f)
771                 new_dir_i->i_nlink++;
772
773         /* Unlink the original */
774         ret = jffs2_do_unlink(c, JFFS2_INODE_INFO(old_dir_i), 
775                       old_dentry->d_name.name, old_dentry->d_name.len, NULL);
776
777         /* We don't touch inode->i_nlink */
778
779         if (ret) {
780                 /* Oh shit. We really ought to make a single node which can do both atomically */
781                 struct jffs2_inode_info *f = JFFS2_INODE_INFO(old_dentry->d_inode);
782                 down(&f->sem);
783                 old_dentry->d_inode->i_nlink++;
784                 if (f->inocache)
785                         f->inocache->nlink++;
786                 up(&f->sem);
787
788                 printk(KERN_NOTICE "jffs2_rename(): Link succeeded, unlink failed (err %d). You now have a hard link\n", ret);
789                 /* Might as well let the VFS know */
790                 d_instantiate(new_dentry, old_dentry->d_inode);
791                 atomic_inc(&old_dentry->d_inode->i_count);
792                 return ret;
793         }
794
795         if (S_ISDIR(old_dentry->d_inode->i_mode))
796                 old_dir_i->i_nlink--;
797
798         return 0;
799 }
800