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