Move jffs2_fs_i.h and jffs2_fs_sb.h from include/linux/ to fs/jffs2/
[linux-2.6.22.y-op.git] / fs / jffs2 / dir.c
blob1c8e8c0f6ceac9ec2cf2fdf058dffea177559547
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.90 2005/11/07 11:14:39 gleixner 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 "jffs2_fs_i.h"
21 #include "jffs2_fs_sb.h"
22 #include <linux/time.h>
23 #include "nodelist.h"
25 static int jffs2_readdir (struct file *, void *, filldir_t);
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 *);
40 const struct file_operations jffs2_dir_operations =
42 .read = generic_read_dir,
43 .readdir = jffs2_readdir,
44 .ioctl = jffs2_ioctl,
45 .fsync = jffs2_fsync
49 struct inode_operations jffs2_dir_inode_operations =
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,
63 /***********************************************************************/
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
70 static struct dentry *jffs2_lookup(struct inode *dir_i, struct dentry *target,
71 struct nameidata *nd)
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;
79 D1(printk(KERN_DEBUG "jffs2_lookup()\n"));
81 if (target->d_name.len > JFFS2_MAX_NAME_LEN)
82 return ERR_PTR(-ENAMETOOLONG);
84 dir_f = JFFS2_INODE_INFO(dir_i);
85 c = JFFS2_SB_INFO(dir_i->i_sb);
87 down(&dir_f->sem);
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;
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));
109 d_add(target, inode);
111 return NULL;
114 /***********************************************************************/
117 static int jffs2_readdir(struct file *filp, void *dirent, filldir_t filldir)
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;
125 D1(printk(KERN_DEBUG "jffs2_readdir() for dir_i #%lu\n", filp->f_dentry->d_inode->i_ino));
127 f = JFFS2_INODE_INFO(inode);
128 c = JFFS2_SB_INFO(inode->i_sb);
130 offset = filp->f_pos;
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++;
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++;
146 curofs=1;
147 down(&f->sem);
148 for (fd = f->dents; fd; fd = fd->next) {
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;
157 if (!fd->ino) {
158 D2(printk(KERN_DEBUG "Skipping deletion dirent \"%s\"\n", fd->name));
159 offset++;
160 continue;
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++;
167 up(&f->sem);
168 out:
169 filp->f_pos = offset;
170 return 0;
173 /***********************************************************************/
176 static int jffs2_create(struct inode *dir_i, struct dentry *dentry, int mode,
177 struct nameidata *nd)
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;
185 ri = jffs2_alloc_raw_inode();
186 if (!ri)
187 return -ENOMEM;
189 c = JFFS2_SB_INFO(dir_i->i_sb);
191 D1(printk(KERN_DEBUG "jffs2_create()\n"));
193 inode = jffs2_new_inode(dir_i, mode, ri);
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);
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;
206 f = JFFS2_INODE_INFO(inode);
207 dir_f = JFFS2_INODE_INFO(dir_i);
209 ret = jffs2_do_create(c, dir_f, f, ri,
210 dentry->d_name.name, dentry->d_name.len);
212 if (ret) {
213 make_bad_inode(inode);
214 iput(inode);
215 jffs2_free_raw_inode(ri);
216 return ret;
219 dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(ri->ctime));
221 jffs2_free_raw_inode(ri);
222 d_instantiate(dentry, inode);
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;
229 /***********************************************************************/
232 static int jffs2_unlink(struct inode *dir_i, struct dentry *dentry)
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();
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;
248 /***********************************************************************/
251 static int jffs2_link (struct dentry *old_dentry, struct inode *dir_i, struct dentry *dentry)
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;
260 /* Don't let people make hard links to bad inodes. */
261 if (!f->inocache)
262 return -EIO;
264 if (S_ISDIR(old_dentry->d_inode->i_mode))
265 return -EPERM;
267 /* XXX: This is ugly */
268 type = (old_dentry->d_inode->i_mode & S_IFMT) >> 12;
269 if (!type) type = DT_REG;
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);
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);
282 return ret;
285 /***********************************************************************/
287 static int jffs2_symlink (struct inode *dir_i, struct dentry *dentry, const char *target)
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);
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;
305 ri = jffs2_alloc_raw_inode();
307 if (!ri)
308 return -ENOMEM;
310 c = JFFS2_SB_INFO(dir_i->i_sb);
312 /* Try to reserve enough space for both node and dirent.
313 * Just the node will do for now, though
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);
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 = 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));
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));
345 fn = jffs2_write_dnode(c, f, ri, target, targetlen, 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);
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;
367 memcpy(f->target, target, targetlen + 1);
368 D1(printk(KERN_DEBUG "jffs2_symlink: symlink's target '%s' cached\n", (char *)f->target));
370 /* No data here. Only a metadata node, which will be
371 obsoleted by the first data write
373 f->metadata = fn;
374 up(&f->sem);
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;
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;
393 dir_f = JFFS2_INODE_INFO(dir_i);
394 down(&dir_f->sem);
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));
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));
410 fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, phys_ofs, ALLOC_NORMAL);
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);
422 dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
424 jffs2_free_raw_dirent(rd);
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);
430 up(&dir_f->sem);
431 jffs2_complete_reservation(c);
433 d_instantiate(dentry, inode);
434 return 0;
438 static int jffs2_mkdir (struct inode *dir_i, struct dentry *dentry, int mode)
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;
451 mode |= S_IFDIR;
453 ri = jffs2_alloc_raw_inode();
454 if (!ri)
455 return -ENOMEM;
457 c = JFFS2_SB_INFO(dir_i->i_sb);
459 /* Try to reserve enough space for both node and dirent.
460 * Just the node will do for now, though
462 namelen = dentry->d_name.len;
463 ret = jffs2_reserve_space(c, sizeof(*ri), &phys_ofs, &alloclen, ALLOC_NORMAL,
464 JFFS2_SUMMARY_INODE_SIZE);
466 if (ret) {
467 jffs2_free_raw_inode(ri);
468 return ret;
471 inode = jffs2_new_inode(dir_i, mode, ri);
473 if (IS_ERR(inode)) {
474 jffs2_free_raw_inode(ri);
475 jffs2_complete_reservation(c);
476 return PTR_ERR(inode);
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;
484 f = JFFS2_INODE_INFO(inode);
486 ri->data_crc = cpu_to_je32(0);
487 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
489 fn = jffs2_write_dnode(c, f, ri, NULL, 0, phys_ofs, ALLOC_NORMAL);
491 jffs2_free_raw_inode(ri);
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);
500 /* No data here. Only a metadata node, which will be
501 obsoleted by the first data write
503 f->metadata = fn;
504 up(&f->sem);
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;
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;
523 dir_f = JFFS2_INODE_INFO(dir_i);
524 down(&dir_f->sem);
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));
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));
540 fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, phys_ofs, ALLOC_NORMAL);
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);
552 dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
553 dir_i->i_nlink++;
555 jffs2_free_raw_dirent(rd);
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);
561 up(&dir_f->sem);
562 jffs2_complete_reservation(c);
564 d_instantiate(dentry, inode);
565 return 0;
568 static int jffs2_rmdir (struct inode *dir_i, struct dentry *dentry)
570 struct jffs2_inode_info *f = JFFS2_INODE_INFO(dentry->d_inode);
571 struct jffs2_full_dirent *fd;
572 int ret;
574 for (fd = f->dents ; fd; fd = fd->next) {
575 if (fd->ino)
576 return -ENOTEMPTY;
578 ret = jffs2_unlink(dir_i, dentry);
579 if (!ret)
580 dir_i->i_nlink--;
581 return ret;
584 static int jffs2_mknod (struct inode *dir_i, struct dentry *dentry, int mode, dev_t rdev)
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 jint16_t dev;
595 int devlen = 0;
596 uint32_t alloclen, phys_ofs;
597 int ret;
599 if (!old_valid_dev(rdev))
600 return -EINVAL;
602 ri = jffs2_alloc_raw_inode();
603 if (!ri)
604 return -ENOMEM;
606 c = JFFS2_SB_INFO(dir_i->i_sb);
608 if (S_ISBLK(mode) || S_ISCHR(mode)) {
609 dev = cpu_to_je16(old_encode_dev(rdev));
610 devlen = sizeof(dev);
613 /* Try to reserve enough space for both node and dirent.
614 * Just the node will do for now, though
616 namelen = dentry->d_name.len;
617 ret = jffs2_reserve_space(c, sizeof(*ri) + devlen, &phys_ofs, &alloclen,
618 ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
620 if (ret) {
621 jffs2_free_raw_inode(ri);
622 return ret;
625 inode = jffs2_new_inode(dir_i, mode, ri);
627 if (IS_ERR(inode)) {
628 jffs2_free_raw_inode(ri);
629 jffs2_complete_reservation(c);
630 return PTR_ERR(inode);
632 inode->i_op = &jffs2_file_inode_operations;
633 init_special_inode(inode, inode->i_mode, rdev);
635 f = JFFS2_INODE_INFO(inode);
637 ri->dsize = ri->csize = cpu_to_je32(devlen);
638 ri->totlen = cpu_to_je32(sizeof(*ri) + devlen);
639 ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
641 ri->compr = JFFS2_COMPR_NONE;
642 ri->data_crc = cpu_to_je32(crc32(0, &dev, devlen));
643 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
645 fn = jffs2_write_dnode(c, f, ri, (char *)&dev, devlen, phys_ofs, ALLOC_NORMAL);
647 jffs2_free_raw_inode(ri);
649 if (IS_ERR(fn)) {
650 /* Eeek. Wave bye bye */
651 up(&f->sem);
652 jffs2_complete_reservation(c);
653 jffs2_clear_inode(inode);
654 return PTR_ERR(fn);
656 /* No data here. Only a metadata node, which will be
657 obsoleted by the first data write
659 f->metadata = fn;
660 up(&f->sem);
662 jffs2_complete_reservation(c);
663 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen,
664 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
665 if (ret) {
666 /* Eep. */
667 jffs2_clear_inode(inode);
668 return ret;
671 rd = jffs2_alloc_raw_dirent();
672 if (!rd) {
673 /* Argh. Now we treat it like a normal delete */
674 jffs2_complete_reservation(c);
675 jffs2_clear_inode(inode);
676 return -ENOMEM;
679 dir_f = JFFS2_INODE_INFO(dir_i);
680 down(&dir_f->sem);
682 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
683 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
684 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
685 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
687 rd->pino = cpu_to_je32(dir_i->i_ino);
688 rd->version = cpu_to_je32(++dir_f->highest_version);
689 rd->ino = cpu_to_je32(inode->i_ino);
690 rd->mctime = cpu_to_je32(get_seconds());
691 rd->nsize = namelen;
693 /* XXX: This is ugly. */
694 rd->type = (mode & S_IFMT) >> 12;
696 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
697 rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
699 fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, phys_ofs, ALLOC_NORMAL);
701 if (IS_ERR(fd)) {
702 /* dirent failed to write. Delete the inode normally
703 as if it were the final unlink() */
704 jffs2_complete_reservation(c);
705 jffs2_free_raw_dirent(rd);
706 up(&dir_f->sem);
707 jffs2_clear_inode(inode);
708 return PTR_ERR(fd);
711 dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
713 jffs2_free_raw_dirent(rd);
715 /* Link the fd into the inode's list, obsoleting an old
716 one if necessary. */
717 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
719 up(&dir_f->sem);
720 jffs2_complete_reservation(c);
722 d_instantiate(dentry, inode);
724 return 0;
727 static int jffs2_rename (struct inode *old_dir_i, struct dentry *old_dentry,
728 struct inode *new_dir_i, struct dentry *new_dentry)
730 int ret;
731 struct jffs2_sb_info *c = JFFS2_SB_INFO(old_dir_i->i_sb);
732 struct jffs2_inode_info *victim_f = NULL;
733 uint8_t type;
734 uint32_t now;
736 /* The VFS will check for us and prevent trying to rename a
737 * file over a directory and vice versa, but if it's a directory,
738 * the VFS can't check whether the victim is empty. The filesystem
739 * needs to do that for itself.
741 if (new_dentry->d_inode) {
742 victim_f = JFFS2_INODE_INFO(new_dentry->d_inode);
743 if (S_ISDIR(new_dentry->d_inode->i_mode)) {
744 struct jffs2_full_dirent *fd;
746 down(&victim_f->sem);
747 for (fd = victim_f->dents; fd; fd = fd->next) {
748 if (fd->ino) {
749 up(&victim_f->sem);
750 return -ENOTEMPTY;
753 up(&victim_f->sem);
757 /* XXX: We probably ought to alloc enough space for
758 both nodes at the same time. Writing the new link,
759 then getting -ENOSPC, is quite bad :)
762 /* Make a hard link */
764 /* XXX: This is ugly */
765 type = (old_dentry->d_inode->i_mode & S_IFMT) >> 12;
766 if (!type) type = DT_REG;
768 now = get_seconds();
769 ret = jffs2_do_link(c, JFFS2_INODE_INFO(new_dir_i),
770 old_dentry->d_inode->i_ino, type,
771 new_dentry->d_name.name, new_dentry->d_name.len, now);
773 if (ret)
774 return ret;
776 if (victim_f) {
777 /* There was a victim. Kill it off nicely */
778 new_dentry->d_inode->i_nlink--;
779 /* Don't oops if the victim was a dirent pointing to an
780 inode which didn't exist. */
781 if (victim_f->inocache) {
782 down(&victim_f->sem);
783 victim_f->inocache->nlink--;
784 up(&victim_f->sem);
788 /* If it was a directory we moved, and there was no victim,
789 increase i_nlink on its new parent */
790 if (S_ISDIR(old_dentry->d_inode->i_mode) && !victim_f)
791 new_dir_i->i_nlink++;
793 /* Unlink the original */
794 ret = jffs2_do_unlink(c, JFFS2_INODE_INFO(old_dir_i),
795 old_dentry->d_name.name, old_dentry->d_name.len, NULL, now);
797 /* We don't touch inode->i_nlink */
799 if (ret) {
800 /* Oh shit. We really ought to make a single node which can do both atomically */
801 struct jffs2_inode_info *f = JFFS2_INODE_INFO(old_dentry->d_inode);
802 down(&f->sem);
803 old_dentry->d_inode->i_nlink++;
804 if (f->inocache)
805 f->inocache->nlink++;
806 up(&f->sem);
808 printk(KERN_NOTICE "jffs2_rename(): Link succeeded, unlink failed (err %d). You now have a hard link\n", ret);
809 /* Might as well let the VFS know */
810 d_instantiate(new_dentry, old_dentry->d_inode);
811 atomic_inc(&old_dentry->d_inode->i_count);
812 new_dir_i->i_mtime = new_dir_i->i_ctime = ITIME(now);
813 return ret;
816 if (S_ISDIR(old_dentry->d_inode->i_mode))
817 old_dir_i->i_nlink--;
819 new_dir_i->i_mtime = new_dir_i->i_ctime = old_dir_i->i_mtime = old_dir_i->i_ctime = ITIME(now);
821 return 0;