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