GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / fs / jffs2 / dir.c
blobcaa9b4771033ef80623a07107f0f0458f93dc565
1 /*
2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright © 2001-2007 Red Hat, Inc.
5 * Copyright © 2004-2010 David Woodhouse <dwmw2@infradead.org>
7 * Created by David Woodhouse <dwmw2@infradead.org>
9 * For licensing information, see the file 'LICENCE' in this directory.
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"
23 static int jffs2_readdir (struct file *, void *, filldir_t);
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 *);
38 const struct file_operations jffs2_dir_operations =
40 .read = generic_read_dir,
41 .readdir = jffs2_readdir,
42 .unlocked_ioctl=jffs2_ioctl,
43 .fsync = jffs2_fsync,
44 .llseek = generic_file_llseek,
48 const struct inode_operations jffs2_dir_inode_operations =
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
67 /***********************************************************************/
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
74 static struct dentry *jffs2_lookup(struct inode *dir_i, struct dentry *target,
75 struct nameidata *nd)
77 struct jffs2_inode_info *dir_f;
78 struct jffs2_sb_info *c;
79 struct jffs2_full_dirent *fd = NULL, *fd_list;
80 uint32_t ino = 0;
81 struct inode *inode = NULL;
83 D1(printk(KERN_DEBUG "jffs2_lookup()\n"));
85 if (target->d_name.len > JFFS2_MAX_NAME_LEN)
86 return ERR_PTR(-ENAMETOOLONG);
88 dir_f = JFFS2_INODE_INFO(dir_i);
89 c = JFFS2_SB_INFO(dir_i->i_sb);
91 mutex_lock(&dir_f->sem);
93 /* NB: The 2.2 backport will need to explicitly check for '.' and '..' here */
94 for (fd_list = dir_f->dents; fd_list && fd_list->nhash <= target->d_name.hash; fd_list = fd_list->next) {
95 if (fd_list->nhash == target->d_name.hash &&
96 (!fd || fd_list->version > fd->version) &&
97 strlen(fd_list->name) == target->d_name.len &&
98 !strncmp(fd_list->name, target->d_name.name, target->d_name.len)) {
99 fd = fd_list;
102 if (fd)
103 ino = fd->ino;
104 mutex_unlock(&dir_f->sem);
105 if (ino) {
106 inode = jffs2_iget(dir_i->i_sb, ino);
107 if (IS_ERR(inode)) {
108 printk(KERN_WARNING "iget() failed for ino #%u\n", ino);
109 return ERR_CAST(inode);
113 return d_splice_alias(inode, target);
116 /***********************************************************************/
119 static int jffs2_readdir(struct file *filp, void *dirent, filldir_t filldir)
121 struct jffs2_inode_info *f;
122 struct jffs2_sb_info *c;
123 struct inode *inode = filp->f_path.dentry->d_inode;
124 struct jffs2_full_dirent *fd;
125 unsigned long offset, curofs;
127 D1(printk(KERN_DEBUG "jffs2_readdir() for dir_i #%lu\n", filp->f_path.dentry->d_inode->i_ino));
129 f = JFFS2_INODE_INFO(inode);
130 c = JFFS2_SB_INFO(inode->i_sb);
132 offset = filp->f_pos;
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++;
140 if (offset == 1) {
141 unsigned long pino = parent_ino(filp->f_path.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++;
148 curofs=1;
149 mutex_lock(&f->sem);
150 for (fd = f->dents; fd; fd = fd->next) {
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;
159 if (!fd->ino) {
160 D2(printk(KERN_DEBUG "Skipping deletion dirent \"%s\"\n", fd->name));
161 offset++;
162 continue;
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++;
169 mutex_unlock(&f->sem);
170 out:
171 filp->f_pos = offset;
172 return 0;
175 /***********************************************************************/
178 static int jffs2_create(struct inode *dir_i, struct dentry *dentry, int mode,
179 struct nameidata *nd)
181 struct jffs2_raw_inode *ri;
182 struct jffs2_inode_info *f, *dir_f;
183 struct jffs2_sb_info *c;
184 struct inode *inode;
185 int ret;
187 ri = jffs2_alloc_raw_inode();
188 if (!ri)
189 return -ENOMEM;
191 c = JFFS2_SB_INFO(dir_i->i_sb);
193 D1(printk(KERN_DEBUG "jffs2_create()\n"));
195 inode = jffs2_new_inode(dir_i, mode, ri);
197 if (IS_ERR(inode)) {
198 D1(printk(KERN_DEBUG "jffs2_new_inode() failed\n"));
199 jffs2_free_raw_inode(ri);
200 return PTR_ERR(inode);
203 inode->i_op = &jffs2_file_inode_operations;
204 inode->i_fop = &jffs2_file_operations;
205 inode->i_mapping->a_ops = &jffs2_file_address_operations;
206 inode->i_mapping->nrpages = 0;
208 f = JFFS2_INODE_INFO(inode);
209 dir_f = JFFS2_INODE_INFO(dir_i);
211 /* jffs2_do_create() will want to lock it, _after_ reserving
212 space and taking c-alloc_sem. If we keep it locked here,
213 lockdep gets unhappy (although it's a false positive;
214 nothing else will be looking at this inode yet so there's
215 no chance of AB-BA deadlock involving its f->sem). */
216 mutex_unlock(&f->sem);
218 ret = jffs2_do_create(c, dir_f, f, ri,
219 dentry->d_name.name, dentry->d_name.len);
220 if (ret)
221 goto fail;
223 dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(ri->ctime));
225 jffs2_free_raw_inode(ri);
227 D1(printk(KERN_DEBUG "jffs2_create: Created ino #%lu with mode %o, nlink %d(%d). nrpages %ld\n",
228 inode->i_ino, inode->i_mode, inode->i_nlink,
229 f->inocache->pino_nlink, inode->i_mapping->nrpages));
231 d_instantiate(dentry, inode);
232 unlock_new_inode(inode);
233 return 0;
235 fail:
236 iget_failed(inode);
237 jffs2_free_raw_inode(ri);
238 return ret;
241 /***********************************************************************/
244 static int jffs2_unlink(struct inode *dir_i, struct dentry *dentry)
246 struct jffs2_sb_info *c = JFFS2_SB_INFO(dir_i->i_sb);
247 struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i);
248 struct jffs2_inode_info *dead_f = JFFS2_INODE_INFO(dentry->d_inode);
249 int ret;
250 uint32_t now = get_seconds();
252 ret = jffs2_do_unlink(c, dir_f, dentry->d_name.name,
253 dentry->d_name.len, dead_f, now);
254 if (dead_f->inocache)
255 dentry->d_inode->i_nlink = dead_f->inocache->pino_nlink;
256 if (!ret)
257 dir_i->i_mtime = dir_i->i_ctime = ITIME(now);
258 return ret;
260 /***********************************************************************/
263 static int jffs2_link (struct dentry *old_dentry, struct inode *dir_i, struct dentry *dentry)
265 struct jffs2_sb_info *c = JFFS2_SB_INFO(old_dentry->d_inode->i_sb);
266 struct jffs2_inode_info *f = JFFS2_INODE_INFO(old_dentry->d_inode);
267 struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i);
268 int ret;
269 uint8_t type;
270 uint32_t now;
272 /* Don't let people make hard links to bad inodes. */
273 if (!f->inocache)
274 return -EIO;
276 if (S_ISDIR(old_dentry->d_inode->i_mode))
277 return -EPERM;
279 type = (old_dentry->d_inode->i_mode & S_IFMT) >> 12;
280 if (!type) type = DT_REG;
282 now = get_seconds();
283 ret = jffs2_do_link(c, dir_f, f->inocache->ino, type, dentry->d_name.name, dentry->d_name.len, now);
285 if (!ret) {
286 mutex_lock(&f->sem);
287 old_dentry->d_inode->i_nlink = ++f->inocache->pino_nlink;
288 mutex_unlock(&f->sem);
289 d_instantiate(dentry, old_dentry->d_inode);
290 dir_i->i_mtime = dir_i->i_ctime = ITIME(now);
291 atomic_inc(&old_dentry->d_inode->i_count);
293 return ret;
296 /***********************************************************************/
298 static int jffs2_symlink (struct inode *dir_i, struct dentry *dentry, const char *target)
300 struct jffs2_inode_info *f, *dir_f;
301 struct jffs2_sb_info *c;
302 struct inode *inode;
303 struct jffs2_raw_inode *ri;
304 struct jffs2_raw_dirent *rd;
305 struct jffs2_full_dnode *fn;
306 struct jffs2_full_dirent *fd;
307 int namelen;
308 uint32_t alloclen;
309 int ret, targetlen = strlen(target);
311 if (targetlen > 254)
312 return -ENAMETOOLONG;
314 ri = jffs2_alloc_raw_inode();
316 if (!ri)
317 return -ENOMEM;
319 c = JFFS2_SB_INFO(dir_i->i_sb);
321 /* Try to reserve enough space for both node and dirent.
322 * Just the node will do for now, though
324 namelen = dentry->d_name.len;
325 ret = jffs2_reserve_space(c, sizeof(*ri) + targetlen, &alloclen,
326 ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
328 if (ret) {
329 jffs2_free_raw_inode(ri);
330 return ret;
333 inode = jffs2_new_inode(dir_i, S_IFLNK | S_IRWXUGO, ri);
335 if (IS_ERR(inode)) {
336 jffs2_free_raw_inode(ri);
337 jffs2_complete_reservation(c);
338 return PTR_ERR(inode);
341 inode->i_op = &jffs2_symlink_inode_operations;
343 f = JFFS2_INODE_INFO(inode);
345 inode->i_size = targetlen;
346 ri->isize = ri->dsize = ri->csize = cpu_to_je32(inode->i_size);
347 ri->totlen = cpu_to_je32(sizeof(*ri) + inode->i_size);
348 ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
350 ri->compr = JFFS2_COMPR_NONE;
351 ri->data_crc = cpu_to_je32(crc32(0, target, targetlen));
352 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
354 fn = jffs2_write_dnode(c, f, ri, target, targetlen, ALLOC_NORMAL);
356 jffs2_free_raw_inode(ri);
358 if (IS_ERR(fn)) {
359 /* Eeek. Wave bye bye */
360 mutex_unlock(&f->sem);
361 jffs2_complete_reservation(c);
362 ret = PTR_ERR(fn);
363 goto fail;
366 /* We use f->target field to store the target path. */
367 f->target = kmalloc(targetlen + 1, GFP_KERNEL);
368 if (!f->target) {
369 printk(KERN_WARNING "Can't allocate %d bytes of memory\n", targetlen + 1);
370 mutex_unlock(&f->sem);
371 jffs2_complete_reservation(c);
372 ret = -ENOMEM;
373 goto fail;
376 memcpy(f->target, target, targetlen + 1);
377 D1(printk(KERN_DEBUG "jffs2_symlink: symlink's target '%s' cached\n", (char *)f->target));
379 /* No data here. Only a metadata node, which will be
380 obsoleted by the first data write
382 f->metadata = fn;
383 mutex_unlock(&f->sem);
385 jffs2_complete_reservation(c);
387 ret = jffs2_init_security(inode, dir_i);
388 if (ret)
389 goto fail;
391 ret = jffs2_init_acl_post(inode);
392 if (ret)
393 goto fail;
395 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
396 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
397 if (ret)
398 goto fail;
400 rd = jffs2_alloc_raw_dirent();
401 if (!rd) {
402 /* Argh. Now we treat it like a normal delete */
403 jffs2_complete_reservation(c);
404 ret = -ENOMEM;
405 goto fail;
408 dir_f = JFFS2_INODE_INFO(dir_i);
409 mutex_lock(&dir_f->sem);
411 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
412 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
413 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
414 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
416 rd->pino = cpu_to_je32(dir_i->i_ino);
417 rd->version = cpu_to_je32(++dir_f->highest_version);
418 rd->ino = cpu_to_je32(inode->i_ino);
419 rd->mctime = cpu_to_je32(get_seconds());
420 rd->nsize = namelen;
421 rd->type = DT_LNK;
422 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
423 rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
425 fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, ALLOC_NORMAL);
427 if (IS_ERR(fd)) {
428 /* dirent failed to write. Delete the inode normally
429 as if it were the final unlink() */
430 jffs2_complete_reservation(c);
431 jffs2_free_raw_dirent(rd);
432 mutex_unlock(&dir_f->sem);
433 ret = PTR_ERR(fd);
434 goto fail;
437 dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
439 jffs2_free_raw_dirent(rd);
441 /* Link the fd into the inode's list, obsoleting an old
442 one if necessary. */
443 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
445 mutex_unlock(&dir_f->sem);
446 jffs2_complete_reservation(c);
448 d_instantiate(dentry, inode);
449 unlock_new_inode(inode);
450 return 0;
452 fail:
453 iget_failed(inode);
454 return ret;
458 static int jffs2_mkdir (struct inode *dir_i, struct dentry *dentry, int mode)
460 struct jffs2_inode_info *f, *dir_f;
461 struct jffs2_sb_info *c;
462 struct inode *inode;
463 struct jffs2_raw_inode *ri;
464 struct jffs2_raw_dirent *rd;
465 struct jffs2_full_dnode *fn;
466 struct jffs2_full_dirent *fd;
467 int namelen;
468 uint32_t alloclen;
469 int ret;
471 mode |= S_IFDIR;
473 ri = jffs2_alloc_raw_inode();
474 if (!ri)
475 return -ENOMEM;
477 c = JFFS2_SB_INFO(dir_i->i_sb);
479 /* Try to reserve enough space for both node and dirent.
480 * Just the node will do for now, though
482 namelen = dentry->d_name.len;
483 ret = jffs2_reserve_space(c, sizeof(*ri), &alloclen, ALLOC_NORMAL,
484 JFFS2_SUMMARY_INODE_SIZE);
486 if (ret) {
487 jffs2_free_raw_inode(ri);
488 return ret;
491 inode = jffs2_new_inode(dir_i, mode, ri);
493 if (IS_ERR(inode)) {
494 jffs2_free_raw_inode(ri);
495 jffs2_complete_reservation(c);
496 return PTR_ERR(inode);
499 inode->i_op = &jffs2_dir_inode_operations;
500 inode->i_fop = &jffs2_dir_operations;
502 f = JFFS2_INODE_INFO(inode);
504 /* Directories get nlink 2 at start */
505 inode->i_nlink = 2;
506 /* but ic->pino_nlink is the parent ino# */
507 f->inocache->pino_nlink = dir_i->i_ino;
509 ri->data_crc = cpu_to_je32(0);
510 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
512 fn = jffs2_write_dnode(c, f, ri, NULL, 0, ALLOC_NORMAL);
514 jffs2_free_raw_inode(ri);
516 if (IS_ERR(fn)) {
517 /* Eeek. Wave bye bye */
518 mutex_unlock(&f->sem);
519 jffs2_complete_reservation(c);
520 ret = PTR_ERR(fn);
521 goto fail;
523 /* No data here. Only a metadata node, which will be
524 obsoleted by the first data write
526 f->metadata = fn;
527 mutex_unlock(&f->sem);
529 jffs2_complete_reservation(c);
531 ret = jffs2_init_security(inode, dir_i);
532 if (ret)
533 goto fail;
535 ret = jffs2_init_acl_post(inode);
536 if (ret)
537 goto fail;
539 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
540 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
541 if (ret)
542 goto fail;
544 rd = jffs2_alloc_raw_dirent();
545 if (!rd) {
546 /* Argh. Now we treat it like a normal delete */
547 jffs2_complete_reservation(c);
548 ret = -ENOMEM;
549 goto fail;
552 dir_f = JFFS2_INODE_INFO(dir_i);
553 mutex_lock(&dir_f->sem);
555 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
556 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
557 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
558 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
560 rd->pino = cpu_to_je32(dir_i->i_ino);
561 rd->version = cpu_to_je32(++dir_f->highest_version);
562 rd->ino = cpu_to_je32(inode->i_ino);
563 rd->mctime = cpu_to_je32(get_seconds());
564 rd->nsize = namelen;
565 rd->type = DT_DIR;
566 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
567 rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
569 fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, ALLOC_NORMAL);
571 if (IS_ERR(fd)) {
572 /* dirent failed to write. Delete the inode normally
573 as if it were the final unlink() */
574 jffs2_complete_reservation(c);
575 jffs2_free_raw_dirent(rd);
576 mutex_unlock(&dir_f->sem);
577 ret = PTR_ERR(fd);
578 goto fail;
581 dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
582 inc_nlink(dir_i);
584 jffs2_free_raw_dirent(rd);
586 /* Link the fd into the inode's list, obsoleting an old
587 one if necessary. */
588 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
590 mutex_unlock(&dir_f->sem);
591 jffs2_complete_reservation(c);
593 d_instantiate(dentry, inode);
594 unlock_new_inode(inode);
595 return 0;
597 fail:
598 iget_failed(inode);
599 return ret;
602 static int jffs2_rmdir (struct inode *dir_i, struct dentry *dentry)
604 struct jffs2_sb_info *c = JFFS2_SB_INFO(dir_i->i_sb);
605 struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i);
606 struct jffs2_inode_info *f = JFFS2_INODE_INFO(dentry->d_inode);
607 struct jffs2_full_dirent *fd;
608 int ret;
609 uint32_t now = get_seconds();
611 for (fd = f->dents ; fd; fd = fd->next) {
612 if (fd->ino)
613 return -ENOTEMPTY;
616 ret = jffs2_do_unlink(c, dir_f, dentry->d_name.name,
617 dentry->d_name.len, f, now);
618 if (!ret) {
619 dir_i->i_mtime = dir_i->i_ctime = ITIME(now);
620 clear_nlink(dentry->d_inode);
621 drop_nlink(dir_i);
623 return ret;
626 static int jffs2_mknod (struct inode *dir_i, struct dentry *dentry, int mode, dev_t rdev)
628 struct jffs2_inode_info *f, *dir_f;
629 struct jffs2_sb_info *c;
630 struct inode *inode;
631 struct jffs2_raw_inode *ri;
632 struct jffs2_raw_dirent *rd;
633 struct jffs2_full_dnode *fn;
634 struct jffs2_full_dirent *fd;
635 int namelen;
636 union jffs2_device_node dev;
637 int devlen = 0;
638 uint32_t alloclen;
639 int ret;
641 if (!new_valid_dev(rdev))
642 return -EINVAL;
644 ri = jffs2_alloc_raw_inode();
645 if (!ri)
646 return -ENOMEM;
648 c = JFFS2_SB_INFO(dir_i->i_sb);
650 if (S_ISBLK(mode) || S_ISCHR(mode))
651 devlen = jffs2_encode_dev(&dev, rdev);
653 /* Try to reserve enough space for both node and dirent.
654 * Just the node will do for now, though
656 namelen = dentry->d_name.len;
657 ret = jffs2_reserve_space(c, sizeof(*ri) + devlen, &alloclen,
658 ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
660 if (ret) {
661 jffs2_free_raw_inode(ri);
662 return ret;
665 inode = jffs2_new_inode(dir_i, mode, ri);
667 if (IS_ERR(inode)) {
668 jffs2_free_raw_inode(ri);
669 jffs2_complete_reservation(c);
670 return PTR_ERR(inode);
672 inode->i_op = &jffs2_file_inode_operations;
673 init_special_inode(inode, inode->i_mode, rdev);
675 f = JFFS2_INODE_INFO(inode);
677 ri->dsize = ri->csize = cpu_to_je32(devlen);
678 ri->totlen = cpu_to_je32(sizeof(*ri) + devlen);
679 ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
681 ri->compr = JFFS2_COMPR_NONE;
682 ri->data_crc = cpu_to_je32(crc32(0, &dev, devlen));
683 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
685 fn = jffs2_write_dnode(c, f, ri, (char *)&dev, devlen, ALLOC_NORMAL);
687 jffs2_free_raw_inode(ri);
689 if (IS_ERR(fn)) {
690 /* Eeek. Wave bye bye */
691 mutex_unlock(&f->sem);
692 jffs2_complete_reservation(c);
693 ret = PTR_ERR(fn);
694 goto fail;
696 /* No data here. Only a metadata node, which will be
697 obsoleted by the first data write
699 f->metadata = fn;
700 mutex_unlock(&f->sem);
702 jffs2_complete_reservation(c);
704 ret = jffs2_init_security(inode, dir_i);
705 if (ret)
706 goto fail;
708 ret = jffs2_init_acl_post(inode);
709 if (ret)
710 goto fail;
712 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
713 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
714 if (ret)
715 goto fail;
717 rd = jffs2_alloc_raw_dirent();
718 if (!rd) {
719 /* Argh. Now we treat it like a normal delete */
720 jffs2_complete_reservation(c);
721 ret = -ENOMEM;
722 goto fail;
725 dir_f = JFFS2_INODE_INFO(dir_i);
726 mutex_lock(&dir_f->sem);
728 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
729 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
730 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
731 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
733 rd->pino = cpu_to_je32(dir_i->i_ino);
734 rd->version = cpu_to_je32(++dir_f->highest_version);
735 rd->ino = cpu_to_je32(inode->i_ino);
736 rd->mctime = cpu_to_je32(get_seconds());
737 rd->nsize = namelen;
739 rd->type = (mode & S_IFMT) >> 12;
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));
744 fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, ALLOC_NORMAL);
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;
756 dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
758 jffs2_free_raw_dirent(rd);
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);
764 mutex_unlock(&dir_f->sem);
765 jffs2_complete_reservation(c);
767 d_instantiate(dentry, inode);
768 unlock_new_inode(inode);
769 return 0;
771 fail:
772 iget_failed(inode);
773 return ret;
776 static int jffs2_rename (struct inode *old_dir_i, struct dentry *old_dentry,
777 struct inode *new_dir_i, struct dentry *new_dentry)
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;
785 /* The VFS will check for us and prevent trying to rename a
786 * file over a directory and vice versa, but if it's a directory,
787 * the VFS can't check whether the victim is empty. The filesystem
788 * needs to do that for itself.
790 if (new_dentry->d_inode) {
791 victim_f = JFFS2_INODE_INFO(new_dentry->d_inode);
792 if (S_ISDIR(new_dentry->d_inode->i_mode)) {
793 struct jffs2_full_dirent *fd;
795 mutex_lock(&victim_f->sem);
796 for (fd = victim_f->dents; fd; fd = fd->next) {
797 if (fd->ino) {
798 mutex_unlock(&victim_f->sem);
799 return -ENOTEMPTY;
802 mutex_unlock(&victim_f->sem);
807 /* Make a hard link */
809 type = (old_dentry->d_inode->i_mode & S_IFMT) >> 12;
810 if (!type) type = DT_REG;
812 now = get_seconds();
813 ret = jffs2_do_link(c, JFFS2_INODE_INFO(new_dir_i),
814 old_dentry->d_inode->i_ino, type,
815 new_dentry->d_name.name, new_dentry->d_name.len, now);
817 if (ret)
818 return ret;
820 if (victim_f) {
821 /* There was a victim. Kill it off nicely */
822 drop_nlink(new_dentry->d_inode);
823 /* Don't oops if the victim was a dirent pointing to an
824 inode which didn't exist. */
825 if (victim_f->inocache) {
826 mutex_lock(&victim_f->sem);
827 if (S_ISDIR(new_dentry->d_inode->i_mode))
828 victim_f->inocache->pino_nlink = 0;
829 else
830 victim_f->inocache->pino_nlink--;
831 mutex_unlock(&victim_f->sem);
835 /* If it was a directory we moved, and there was no victim,
836 increase i_nlink on its new parent */
837 if (S_ISDIR(old_dentry->d_inode->i_mode) && !victim_f)
838 inc_nlink(new_dir_i);
840 /* Unlink the original */
841 ret = jffs2_do_unlink(c, JFFS2_INODE_INFO(old_dir_i),
842 old_dentry->d_name.name, old_dentry->d_name.len, NULL, now);
844 /* We don't touch inode->i_nlink */
846 if (ret) {
847 /* Oh shit. We really ought to make a single node which can do both atomically */
848 struct jffs2_inode_info *f = JFFS2_INODE_INFO(old_dentry->d_inode);
849 mutex_lock(&f->sem);
850 inc_nlink(old_dentry->d_inode);
851 if (f->inocache && !S_ISDIR(old_dentry->d_inode->i_mode))
852 f->inocache->pino_nlink++;
853 mutex_unlock(&f->sem);
855 printk(KERN_NOTICE "jffs2_rename(): Link succeeded, unlink failed (err %d). You now have a hard link\n", ret);
856 /* Might as well let the VFS know */
857 d_instantiate(new_dentry, old_dentry->d_inode);
858 atomic_inc(&old_dentry->d_inode->i_count);
859 new_dir_i->i_mtime = new_dir_i->i_ctime = ITIME(now);
860 return ret;
863 if (S_ISDIR(old_dentry->d_inode->i_mode))
864 drop_nlink(old_dir_i);
866 new_dir_i->i_mtime = new_dir_i->i_ctime = old_dir_i->i_mtime = old_dir_i->i_ctime = ITIME(now);
868 return 0;