[TG3]: Set minimal hw interrupt mitigation.
[linux-2.6/verdex.git] / fs / jffs2 / dir.c
blob757306fa3ff477aebcfa0ab861d45bfd7360acdd
1 /*
2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright (C) 2001-2003 Red Hat, Inc.
6 * Created by David Woodhouse <dwmw2@infradead.org>
8 * For licensing information, see the file 'LICENCE' in this directory.
10 * $Id: dir.c,v 1.84 2004/11/16 20:36:11 dwmw2 Exp $
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"
25 /* Urgh. Please tell me there's a nicer way of doing these. */
26 #include <linux/version.h>
27 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,48)
28 typedef int mknod_arg_t;
29 #define NAMEI_COMPAT(x) ((void *)x)
30 #else
31 typedef dev_t mknod_arg_t;
32 #define NAMEI_COMPAT(x) (x)
33 #endif
35 static int jffs2_readdir (struct file *, void *, filldir_t);
37 static int jffs2_create (struct inode *,struct dentry *,int,
38 struct nameidata *);
39 static struct dentry *jffs2_lookup (struct inode *,struct dentry *,
40 struct nameidata *);
41 static int jffs2_link (struct dentry *,struct inode *,struct dentry *);
42 static int jffs2_unlink (struct inode *,struct dentry *);
43 static int jffs2_symlink (struct inode *,struct dentry *,const char *);
44 static int jffs2_mkdir (struct inode *,struct dentry *,int);
45 static int jffs2_rmdir (struct inode *,struct dentry *);
46 static int jffs2_mknod (struct inode *,struct dentry *,int,mknod_arg_t);
47 static int jffs2_rename (struct inode *, struct dentry *,
48 struct inode *, struct dentry *);
50 struct file_operations jffs2_dir_operations =
52 .read = generic_read_dir,
53 .readdir = jffs2_readdir,
54 .ioctl = jffs2_ioctl,
55 .fsync = jffs2_fsync
59 struct inode_operations jffs2_dir_inode_operations =
61 .create = NAMEI_COMPAT(jffs2_create),
62 .lookup = NAMEI_COMPAT(jffs2_lookup),
63 .link = jffs2_link,
64 .unlink = jffs2_unlink,
65 .symlink = jffs2_symlink,
66 .mkdir = jffs2_mkdir,
67 .rmdir = jffs2_rmdir,
68 .mknod = jffs2_mknod,
69 .rename = jffs2_rename,
70 .setattr = jffs2_setattr,
73 /***********************************************************************/
76 /* We keep the dirent list sorted in increasing order of name hash,
77 and we use the same hash function as the dentries. Makes this
78 nice and simple
80 static struct dentry *jffs2_lookup(struct inode *dir_i, struct dentry *target,
81 struct nameidata *nd)
83 struct jffs2_inode_info *dir_f;
84 struct jffs2_sb_info *c;
85 struct jffs2_full_dirent *fd = NULL, *fd_list;
86 uint32_t ino = 0;
87 struct inode *inode = NULL;
89 D1(printk(KERN_DEBUG "jffs2_lookup()\n"));
91 dir_f = JFFS2_INODE_INFO(dir_i);
92 c = JFFS2_SB_INFO(dir_i->i_sb);
94 down(&dir_f->sem);
96 /* NB: The 2.2 backport will need to explicitly check for '.' and '..' here */
97 for (fd_list = dir_f->dents; fd_list && fd_list->nhash <= target->d_name.hash; fd_list = fd_list->next) {
98 if (fd_list->nhash == target->d_name.hash &&
99 (!fd || fd_list->version > fd->version) &&
100 strlen(fd_list->name) == target->d_name.len &&
101 !strncmp(fd_list->name, target->d_name.name, target->d_name.len)) {
102 fd = fd_list;
105 if (fd)
106 ino = fd->ino;
107 up(&dir_f->sem);
108 if (ino) {
109 inode = iget(dir_i->i_sb, ino);
110 if (!inode) {
111 printk(KERN_WARNING "iget() failed for ino #%u\n", ino);
112 return (ERR_PTR(-EIO));
116 d_add(target, inode);
118 return NULL;
121 /***********************************************************************/
124 static int jffs2_readdir(struct file *filp, void *dirent, filldir_t filldir)
126 struct jffs2_inode_info *f;
127 struct jffs2_sb_info *c;
128 struct inode *inode = filp->f_dentry->d_inode;
129 struct jffs2_full_dirent *fd;
130 unsigned long offset, curofs;
132 D1(printk(KERN_DEBUG "jffs2_readdir() for dir_i #%lu\n", filp->f_dentry->d_inode->i_ino));
134 f = JFFS2_INODE_INFO(inode);
135 c = JFFS2_SB_INFO(inode->i_sb);
137 offset = filp->f_pos;
139 if (offset == 0) {
140 D1(printk(KERN_DEBUG "Dirent 0: \".\", ino #%lu\n", inode->i_ino));
141 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
142 goto out;
143 offset++;
145 if (offset == 1) {
146 unsigned long pino = parent_ino(filp->f_dentry);
147 D1(printk(KERN_DEBUG "Dirent 1: \"..\", ino #%lu\n", pino));
148 if (filldir(dirent, "..", 2, 1, pino, DT_DIR) < 0)
149 goto out;
150 offset++;
153 curofs=1;
154 down(&f->sem);
155 for (fd = f->dents; fd; fd = fd->next) {
157 curofs++;
158 /* First loop: curofs = 2; offset = 2 */
159 if (curofs < offset) {
160 D2(printk(KERN_DEBUG "Skipping dirent: \"%s\", ino #%u, type %d, because curofs %ld < offset %ld\n",
161 fd->name, fd->ino, fd->type, curofs, offset));
162 continue;
164 if (!fd->ino) {
165 D2(printk(KERN_DEBUG "Skipping deletion dirent \"%s\"\n", fd->name));
166 offset++;
167 continue;
169 D2(printk(KERN_DEBUG "Dirent %ld: \"%s\", ino #%u, type %d\n", offset, fd->name, fd->ino, fd->type));
170 if (filldir(dirent, fd->name, strlen(fd->name), offset, fd->ino, fd->type) < 0)
171 break;
172 offset++;
174 up(&f->sem);
175 out:
176 filp->f_pos = offset;
177 return 0;
180 /***********************************************************************/
183 static int jffs2_create(struct inode *dir_i, struct dentry *dentry, int mode,
184 struct nameidata *nd)
186 struct jffs2_raw_inode *ri;
187 struct jffs2_inode_info *f, *dir_f;
188 struct jffs2_sb_info *c;
189 struct inode *inode;
190 int ret;
192 ri = jffs2_alloc_raw_inode();
193 if (!ri)
194 return -ENOMEM;
196 c = JFFS2_SB_INFO(dir_i->i_sb);
198 D1(printk(KERN_DEBUG "jffs2_create()\n"));
200 inode = jffs2_new_inode(dir_i, mode, ri);
202 if (IS_ERR(inode)) {
203 D1(printk(KERN_DEBUG "jffs2_new_inode() failed\n"));
204 jffs2_free_raw_inode(ri);
205 return PTR_ERR(inode);
208 inode->i_op = &jffs2_file_inode_operations;
209 inode->i_fop = &jffs2_file_operations;
210 inode->i_mapping->a_ops = &jffs2_file_address_operations;
211 inode->i_mapping->nrpages = 0;
213 f = JFFS2_INODE_INFO(inode);
214 dir_f = JFFS2_INODE_INFO(dir_i);
216 ret = jffs2_do_create(c, dir_f, f, ri,
217 dentry->d_name.name, dentry->d_name.len);
219 if (ret) {
220 make_bad_inode(inode);
221 iput(inode);
222 jffs2_free_raw_inode(ri);
223 return ret;
226 dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(ri->ctime));
228 jffs2_free_raw_inode(ri);
229 d_instantiate(dentry, inode);
231 D1(printk(KERN_DEBUG "jffs2_create: Created ino #%lu with mode %o, nlink %d(%d). nrpages %ld\n",
232 inode->i_ino, inode->i_mode, inode->i_nlink, f->inocache->nlink, inode->i_mapping->nrpages));
233 return 0;
236 /***********************************************************************/
239 static int jffs2_unlink(struct inode *dir_i, struct dentry *dentry)
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;
246 ret = jffs2_do_unlink(c, dir_f, dentry->d_name.name,
247 dentry->d_name.len, dead_f);
248 if (dead_f->inocache)
249 dentry->d_inode->i_nlink = dead_f->inocache->nlink;
250 return ret;
252 /***********************************************************************/
255 static int jffs2_link (struct dentry *old_dentry, struct inode *dir_i, struct dentry *dentry)
257 struct jffs2_sb_info *c = JFFS2_SB_INFO(old_dentry->d_inode->i_sb);
258 struct jffs2_inode_info *f = JFFS2_INODE_INFO(old_dentry->d_inode);
259 struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i);
260 int ret;
261 uint8_t type;
263 /* Don't let people make hard links to bad inodes. */
264 if (!f->inocache)
265 return -EIO;
267 if (S_ISDIR(old_dentry->d_inode->i_mode))
268 return -EPERM;
270 /* XXX: This is ugly */
271 type = (old_dentry->d_inode->i_mode & S_IFMT) >> 12;
272 if (!type) type = DT_REG;
274 ret = jffs2_do_link(c, dir_f, f->inocache->ino, type, dentry->d_name.name, dentry->d_name.len);
276 if (!ret) {
277 down(&f->sem);
278 old_dentry->d_inode->i_nlink = ++f->inocache->nlink;
279 up(&f->sem);
280 d_instantiate(dentry, old_dentry->d_inode);
281 atomic_inc(&old_dentry->d_inode->i_count);
283 return ret;
286 /***********************************************************************/
288 static int jffs2_symlink (struct inode *dir_i, struct dentry *dentry, const char *target)
290 struct jffs2_inode_info *f, *dir_f;
291 struct jffs2_sb_info *c;
292 struct inode *inode;
293 struct jffs2_raw_inode *ri;
294 struct jffs2_raw_dirent *rd;
295 struct jffs2_full_dnode *fn;
296 struct jffs2_full_dirent *fd;
297 int namelen;
298 uint32_t alloclen, phys_ofs;
299 int ret;
301 /* FIXME: If you care. We'd need to use frags for the target
302 if it grows much more than this */
303 if (strlen(target) > 254)
304 return -EINVAL;
306 ri = jffs2_alloc_raw_inode();
308 if (!ri)
309 return -ENOMEM;
311 c = JFFS2_SB_INFO(dir_i->i_sb);
313 /* Try to reserve enough space for both node and dirent.
314 * Just the node will do for now, though
316 namelen = dentry->d_name.len;
317 ret = jffs2_reserve_space(c, sizeof(*ri) + strlen(target), &phys_ofs, &alloclen, ALLOC_NORMAL);
319 if (ret) {
320 jffs2_free_raw_inode(ri);
321 return ret;
324 inode = jffs2_new_inode(dir_i, S_IFLNK | S_IRWXUGO, ri);
326 if (IS_ERR(inode)) {
327 jffs2_free_raw_inode(ri);
328 jffs2_complete_reservation(c);
329 return PTR_ERR(inode);
332 inode->i_op = &jffs2_symlink_inode_operations;
334 f = JFFS2_INODE_INFO(inode);
336 inode->i_size = strlen(target);
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));
341 ri->compr = JFFS2_COMPR_NONE;
342 ri->data_crc = cpu_to_je32(crc32(0, target, strlen(target)));
343 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
345 fn = jffs2_write_dnode(c, f, ri, target, strlen(target), phys_ofs, ALLOC_NORMAL);
347 jffs2_free_raw_inode(ri);
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);
356 /* No data here. Only a metadata node, which will be
357 obsoleted by the first data write
359 f->metadata = fn;
360 up(&f->sem);
362 jffs2_complete_reservation(c);
363 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen, ALLOC_NORMAL);
364 if (ret) {
365 /* Eep. */
366 jffs2_clear_inode(inode);
367 return ret;
370 rd = jffs2_alloc_raw_dirent();
371 if (!rd) {
372 /* Argh. Now we treat it like a normal delete */
373 jffs2_complete_reservation(c);
374 jffs2_clear_inode(inode);
375 return -ENOMEM;
378 dir_f = JFFS2_INODE_INFO(dir_i);
379 down(&dir_f->sem);
381 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
382 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
383 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
384 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
386 rd->pino = cpu_to_je32(dir_i->i_ino);
387 rd->version = cpu_to_je32(++dir_f->highest_version);
388 rd->ino = cpu_to_je32(inode->i_ino);
389 rd->mctime = cpu_to_je32(get_seconds());
390 rd->nsize = namelen;
391 rd->type = DT_LNK;
392 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
393 rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
395 fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, phys_ofs, ALLOC_NORMAL);
397 if (IS_ERR(fd)) {
398 /* dirent failed to write. Delete the inode normally
399 as if it were the final unlink() */
400 jffs2_complete_reservation(c);
401 jffs2_free_raw_dirent(rd);
402 up(&dir_f->sem);
403 jffs2_clear_inode(inode);
404 return PTR_ERR(fd);
407 dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
409 jffs2_free_raw_dirent(rd);
411 /* Link the fd into the inode's list, obsoleting an old
412 one if necessary. */
413 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
415 up(&dir_f->sem);
416 jffs2_complete_reservation(c);
418 d_instantiate(dentry, inode);
419 return 0;
423 static int jffs2_mkdir (struct inode *dir_i, struct dentry *dentry, int mode)
425 struct jffs2_inode_info *f, *dir_f;
426 struct jffs2_sb_info *c;
427 struct inode *inode;
428 struct jffs2_raw_inode *ri;
429 struct jffs2_raw_dirent *rd;
430 struct jffs2_full_dnode *fn;
431 struct jffs2_full_dirent *fd;
432 int namelen;
433 uint32_t alloclen, phys_ofs;
434 int ret;
436 mode |= S_IFDIR;
438 ri = jffs2_alloc_raw_inode();
439 if (!ri)
440 return -ENOMEM;
442 c = JFFS2_SB_INFO(dir_i->i_sb);
444 /* Try to reserve enough space for both node and dirent.
445 * Just the node will do for now, though
447 namelen = dentry->d_name.len;
448 ret = jffs2_reserve_space(c, sizeof(*ri), &phys_ofs, &alloclen, ALLOC_NORMAL);
450 if (ret) {
451 jffs2_free_raw_inode(ri);
452 return ret;
455 inode = jffs2_new_inode(dir_i, mode, ri);
457 if (IS_ERR(inode)) {
458 jffs2_free_raw_inode(ri);
459 jffs2_complete_reservation(c);
460 return PTR_ERR(inode);
463 inode->i_op = &jffs2_dir_inode_operations;
464 inode->i_fop = &jffs2_dir_operations;
465 /* Directories get nlink 2 at start */
466 inode->i_nlink = 2;
468 f = JFFS2_INODE_INFO(inode);
470 ri->data_crc = cpu_to_je32(0);
471 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
473 fn = jffs2_write_dnode(c, f, ri, NULL, 0, phys_ofs, ALLOC_NORMAL);
475 jffs2_free_raw_inode(ri);
477 if (IS_ERR(fn)) {
478 /* Eeek. Wave bye bye */
479 up(&f->sem);
480 jffs2_complete_reservation(c);
481 jffs2_clear_inode(inode);
482 return PTR_ERR(fn);
484 /* No data here. Only a metadata node, which will be
485 obsoleted by the first data write
487 f->metadata = fn;
488 up(&f->sem);
490 jffs2_complete_reservation(c);
491 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen, ALLOC_NORMAL);
492 if (ret) {
493 /* Eep. */
494 jffs2_clear_inode(inode);
495 return ret;
498 rd = jffs2_alloc_raw_dirent();
499 if (!rd) {
500 /* Argh. Now we treat it like a normal delete */
501 jffs2_complete_reservation(c);
502 jffs2_clear_inode(inode);
503 return -ENOMEM;
506 dir_f = JFFS2_INODE_INFO(dir_i);
507 down(&dir_f->sem);
509 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
510 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
511 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
512 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
514 rd->pino = cpu_to_je32(dir_i->i_ino);
515 rd->version = cpu_to_je32(++dir_f->highest_version);
516 rd->ino = cpu_to_je32(inode->i_ino);
517 rd->mctime = cpu_to_je32(get_seconds());
518 rd->nsize = namelen;
519 rd->type = DT_DIR;
520 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
521 rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
523 fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, phys_ofs, ALLOC_NORMAL);
525 if (IS_ERR(fd)) {
526 /* dirent failed to write. Delete the inode normally
527 as if it were the final unlink() */
528 jffs2_complete_reservation(c);
529 jffs2_free_raw_dirent(rd);
530 up(&dir_f->sem);
531 jffs2_clear_inode(inode);
532 return PTR_ERR(fd);
535 dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
536 dir_i->i_nlink++;
538 jffs2_free_raw_dirent(rd);
540 /* Link the fd into the inode's list, obsoleting an old
541 one if necessary. */
542 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
544 up(&dir_f->sem);
545 jffs2_complete_reservation(c);
547 d_instantiate(dentry, inode);
548 return 0;
551 static int jffs2_rmdir (struct inode *dir_i, struct dentry *dentry)
553 struct jffs2_inode_info *f = JFFS2_INODE_INFO(dentry->d_inode);
554 struct jffs2_full_dirent *fd;
555 int ret;
557 for (fd = f->dents ; fd; fd = fd->next) {
558 if (fd->ino)
559 return -ENOTEMPTY;
561 ret = jffs2_unlink(dir_i, dentry);
562 if (!ret)
563 dir_i->i_nlink--;
564 return ret;
567 static int jffs2_mknod (struct inode *dir_i, struct dentry *dentry, int mode, mknod_arg_t rdev)
569 struct jffs2_inode_info *f, *dir_f;
570 struct jffs2_sb_info *c;
571 struct inode *inode;
572 struct jffs2_raw_inode *ri;
573 struct jffs2_raw_dirent *rd;
574 struct jffs2_full_dnode *fn;
575 struct jffs2_full_dirent *fd;
576 int namelen;
577 jint16_t dev;
578 int devlen = 0;
579 uint32_t alloclen, phys_ofs;
580 int ret;
582 if (!old_valid_dev(rdev))
583 return -EINVAL;
585 ri = jffs2_alloc_raw_inode();
586 if (!ri)
587 return -ENOMEM;
589 c = JFFS2_SB_INFO(dir_i->i_sb);
591 if (S_ISBLK(mode) || S_ISCHR(mode)) {
592 dev = cpu_to_je16(old_encode_dev(rdev));
593 devlen = sizeof(dev);
596 /* Try to reserve enough space for both node and dirent.
597 * Just the node will do for now, though
599 namelen = dentry->d_name.len;
600 ret = jffs2_reserve_space(c, sizeof(*ri) + devlen, &phys_ofs, &alloclen, ALLOC_NORMAL);
602 if (ret) {
603 jffs2_free_raw_inode(ri);
604 return ret;
607 inode = jffs2_new_inode(dir_i, mode, ri);
609 if (IS_ERR(inode)) {
610 jffs2_free_raw_inode(ri);
611 jffs2_complete_reservation(c);
612 return PTR_ERR(inode);
614 inode->i_op = &jffs2_file_inode_operations;
615 init_special_inode(inode, inode->i_mode, rdev);
617 f = JFFS2_INODE_INFO(inode);
619 ri->dsize = ri->csize = cpu_to_je32(devlen);
620 ri->totlen = cpu_to_je32(sizeof(*ri) + devlen);
621 ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
623 ri->compr = JFFS2_COMPR_NONE;
624 ri->data_crc = cpu_to_je32(crc32(0, &dev, devlen));
625 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
627 fn = jffs2_write_dnode(c, f, ri, (char *)&dev, devlen, phys_ofs, ALLOC_NORMAL);
629 jffs2_free_raw_inode(ri);
631 if (IS_ERR(fn)) {
632 /* Eeek. Wave bye bye */
633 up(&f->sem);
634 jffs2_complete_reservation(c);
635 jffs2_clear_inode(inode);
636 return PTR_ERR(fn);
638 /* No data here. Only a metadata node, which will be
639 obsoleted by the first data write
641 f->metadata = fn;
642 up(&f->sem);
644 jffs2_complete_reservation(c);
645 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen, ALLOC_NORMAL);
646 if (ret) {
647 /* Eep. */
648 jffs2_clear_inode(inode);
649 return ret;
652 rd = jffs2_alloc_raw_dirent();
653 if (!rd) {
654 /* Argh. Now we treat it like a normal delete */
655 jffs2_complete_reservation(c);
656 jffs2_clear_inode(inode);
657 return -ENOMEM;
660 dir_f = JFFS2_INODE_INFO(dir_i);
661 down(&dir_f->sem);
663 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
664 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
665 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
666 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
668 rd->pino = cpu_to_je32(dir_i->i_ino);
669 rd->version = cpu_to_je32(++dir_f->highest_version);
670 rd->ino = cpu_to_je32(inode->i_ino);
671 rd->mctime = cpu_to_je32(get_seconds());
672 rd->nsize = namelen;
674 /* XXX: This is ugly. */
675 rd->type = (mode & S_IFMT) >> 12;
677 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
678 rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
680 fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, phys_ofs, ALLOC_NORMAL);
682 if (IS_ERR(fd)) {
683 /* dirent failed to write. Delete the inode normally
684 as if it were the final unlink() */
685 jffs2_complete_reservation(c);
686 jffs2_free_raw_dirent(rd);
687 up(&dir_f->sem);
688 jffs2_clear_inode(inode);
689 return PTR_ERR(fd);
692 dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
694 jffs2_free_raw_dirent(rd);
696 /* Link the fd into the inode's list, obsoleting an old
697 one if necessary. */
698 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
700 up(&dir_f->sem);
701 jffs2_complete_reservation(c);
703 d_instantiate(dentry, inode);
705 return 0;
708 static int jffs2_rename (struct inode *old_dir_i, struct dentry *old_dentry,
709 struct inode *new_dir_i, struct dentry *new_dentry)
711 int ret;
712 struct jffs2_sb_info *c = JFFS2_SB_INFO(old_dir_i->i_sb);
713 struct jffs2_inode_info *victim_f = NULL;
714 uint8_t type;
716 /* The VFS will check for us and prevent trying to rename a
717 * file over a directory and vice versa, but if it's a directory,
718 * the VFS can't check whether the victim is empty. The filesystem
719 * needs to do that for itself.
721 if (new_dentry->d_inode) {
722 victim_f = JFFS2_INODE_INFO(new_dentry->d_inode);
723 if (S_ISDIR(new_dentry->d_inode->i_mode)) {
724 struct jffs2_full_dirent *fd;
726 down(&victim_f->sem);
727 for (fd = victim_f->dents; fd; fd = fd->next) {
728 if (fd->ino) {
729 up(&victim_f->sem);
730 return -ENOTEMPTY;
733 up(&victim_f->sem);
737 /* XXX: We probably ought to alloc enough space for
738 both nodes at the same time. Writing the new link,
739 then getting -ENOSPC, is quite bad :)
742 /* Make a hard link */
744 /* XXX: This is ugly */
745 type = (old_dentry->d_inode->i_mode & S_IFMT) >> 12;
746 if (!type) type = DT_REG;
748 ret = jffs2_do_link(c, JFFS2_INODE_INFO(new_dir_i),
749 old_dentry->d_inode->i_ino, type,
750 new_dentry->d_name.name, new_dentry->d_name.len);
752 if (ret)
753 return ret;
755 if (victim_f) {
756 /* There was a victim. Kill it off nicely */
757 new_dentry->d_inode->i_nlink--;
758 /* Don't oops if the victim was a dirent pointing to an
759 inode which didn't exist. */
760 if (victim_f->inocache) {
761 down(&victim_f->sem);
762 victim_f->inocache->nlink--;
763 up(&victim_f->sem);
767 /* If it was a directory we moved, and there was no victim,
768 increase i_nlink on its new parent */
769 if (S_ISDIR(old_dentry->d_inode->i_mode) && !victim_f)
770 new_dir_i->i_nlink++;
772 /* Unlink the original */
773 ret = jffs2_do_unlink(c, JFFS2_INODE_INFO(old_dir_i),
774 old_dentry->d_name.name, old_dentry->d_name.len, NULL);
776 /* We don't touch inode->i_nlink */
778 if (ret) {
779 /* Oh shit. We really ought to make a single node which can do both atomically */
780 struct jffs2_inode_info *f = JFFS2_INODE_INFO(old_dentry->d_inode);
781 down(&f->sem);
782 old_dentry->d_inode->i_nlink++;
783 if (f->inocache)
784 f->inocache->nlink++;
785 up(&f->sem);
787 printk(KERN_NOTICE "jffs2_rename(): Link succeeded, unlink failed (err %d). You now have a hard link\n", ret);
788 /* Might as well let the VFS know */
789 d_instantiate(new_dentry, old_dentry->d_inode);
790 atomic_inc(&old_dentry->d_inode->i_count);
791 return ret;
794 if (S_ISDIR(old_dentry->d_inode->i_mode))
795 old_dir_i->i_nlink--;
797 return 0;