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