2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright (C) 2001 Red Hat, Inc.
6 * Created by David Woodhouse <dwmw2@cambridge.redhat.com>
8 * The original JFFS, from which the design for JFFS2 was derived,
9 * was designed and implemented by Axis Communications AB.
11 * The contents of this file are subject to the Red Hat eCos Public
12 * License Version 1.1 (the "Licence"); you may not use this file
13 * except in compliance with the Licence. You may obtain a copy of
14 * the Licence at http://www.redhat.com/
16 * Software distributed under the Licence is distributed on an "AS IS"
17 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
18 * See the Licence for the specific language governing rights and
19 * limitations under the Licence.
21 * The Original Code is JFFS2 - Journalling Flash File System, version 2
23 * Alternatively, the contents of this file may be used under the
24 * terms of the GNU General Public License version 2 (the "GPL"), in
25 * which case the provisions of the GPL are applicable instead of the
26 * above. If you wish to allow the use of your version of this file
27 * only under the terms of the GPL and not to allow others to use your
28 * version of this file under the RHEPL, indicate your decision by
29 * deleting the provisions above and replace them with the notice and
30 * other provisions required by the GPL. If you do not delete the
31 * provisions above, a recipient may use your version of this file
32 * under either the RHEPL or the GPL.
34 * $Id: dir.c,v 1.1.1.1 2005/04/11 02:50:46 jack Exp $
38 #include <linux/kernel.h>
39 #include <linux/slab.h>
41 #include <linux/mtd/compatmac.h> /* For completion */
42 #include <linux/jffs2.h>
43 #include <linux/jffs2_fs_i.h>
44 #include <linux/jffs2_fs_sb.h>
48 static int jffs2_readdir (struct file *, void *, filldir_t);
50 static int jffs2_create (struct inode *,struct dentry *,int);
51 static struct dentry *jffs2_lookup (struct inode *,struct dentry *);
52 static int jffs2_link (struct dentry *,struct inode *,struct dentry *);
53 static int jffs2_unlink (struct inode *,struct dentry *);
54 static int jffs2_symlink (struct inode *,struct dentry *,const char *);
55 static int jffs2_mkdir (struct inode *,struct dentry *,int);
56 static int jffs2_rmdir (struct inode *,struct dentry *);
57 static int jffs2_mknod (struct inode *,struct dentry *,int,int);
58 static int jffs2_rename (struct inode *, struct dentry *,
59 struct inode *, struct dentry *);
61 struct file_operations jffs2_dir_operations =
63 read: generic_read_dir,
64 readdir: jffs2_readdir,
66 fsync: jffs2_null_fsync
70 struct inode_operations jffs2_dir_inode_operations =
76 symlink: jffs2_symlink,
81 setattr: jffs2_setattr,
84 /***********************************************************************/
87 /* We keep the dirent list sorted in increasing order of name hash,
88 and we use the same hash function as the dentries. Makes this
91 static struct dentry *jffs2_lookup(struct inode *dir_i, struct dentry *target)
93 struct jffs2_inode_info *dir_f;
94 struct jffs2_sb_info *c;
95 struct jffs2_full_dirent *fd = NULL, *fd_list;
97 struct inode *inode = NULL;
99 D1(printk(KERN_DEBUG "jffs2_lookup()\n"));
101 dir_f = JFFS2_INODE_INFO(dir_i);
102 c = JFFS2_SB_INFO(dir_i->i_sb);
106 /* NB: The 2.2 backport will need to explicitly check for '.' and '..' here */
107 for (fd_list = dir_f->dents; fd_list && fd_list->nhash <= target->d_name.hash; fd_list = fd_list->next) {
108 if (fd_list->nhash == target->d_name.hash &&
109 (!fd || fd_list->version > fd->version) &&
110 strlen(fd_list->name) == target->d_name.len &&
111 !strncmp(fd_list->name, target->d_name.name, target->d_name.len)) {
119 inode = iget(dir_i->i_sb, ino);
121 printk(KERN_WARNING "iget() failed for ino #%u\n", ino);
122 return (ERR_PTR(-EIO));
126 d_add(target, inode);
131 /***********************************************************************/
134 static int jffs2_readdir(struct file *filp, void *dirent, filldir_t filldir)
136 struct jffs2_inode_info *f;
137 struct jffs2_sb_info *c;
138 struct inode *inode = filp->f_dentry->d_inode;
139 struct jffs2_full_dirent *fd;
140 unsigned long offset, curofs;
142 D1(printk(KERN_DEBUG "jffs2_readdir() for dir_i #%lu\n", filp->f_dentry->d_inode->i_ino));
144 f = JFFS2_INODE_INFO(inode);
145 c = JFFS2_SB_INFO(inode->i_sb);
147 offset = filp->f_pos;
150 D1(printk(KERN_DEBUG "Dirent 0: \".\", ino #%lu\n", inode->i_ino));
151 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
156 D1(printk(KERN_DEBUG "Dirent 1: \"..\", ino #%lu\n", filp->f_dentry->d_parent->d_inode->i_ino));
157 if (filldir(dirent, "..", 2, 1, filp->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0)
164 for (fd = f->dents; fd; fd = fd->next) {
167 /* First loop: curofs = 2; offset = 2 */
168 if (curofs < offset) {
169 D2(printk(KERN_DEBUG "Skipping dirent: \"%s\", ino #%u, type %d, because curofs %ld < offset %ld\n",
170 fd->name, fd->ino, fd->type, curofs, offset));
174 D2(printk(KERN_DEBUG "Skipping deletion dirent \"%s\"\n", fd->name));
178 D2(printk(KERN_DEBUG "Dirent %ld: \"%s\", ino #%u, type %d\n", offset, fd->name, fd->ino, fd->type));
179 if (filldir(dirent, fd->name, strlen(fd->name), offset, fd->ino, fd->type) < 0)
185 filp->f_pos = offset;
189 /***********************************************************************/
191 static int jffs2_create(struct inode *dir_i, struct dentry *dentry, int mode)
193 struct jffs2_inode_info *f, *dir_f;
194 struct jffs2_sb_info *c;
196 struct jffs2_raw_inode *ri;
197 struct jffs2_raw_dirent *rd;
198 struct jffs2_full_dnode *fn;
199 struct jffs2_full_dirent *fd;
201 __u32 alloclen, phys_ofs;
205 ri = jffs2_alloc_raw_inode();
209 c = JFFS2_SB_INFO(dir_i->i_sb);
211 D1(printk(KERN_DEBUG "jffs2_create()\n"));
213 /* Try to reserve enough space for both node and dirent.
214 * Just the node will do for now, though
216 namelen = dentry->d_name.len;
217 ret = jffs2_reserve_space(c, sizeof(*ri), &phys_ofs, &alloclen, ALLOC_NORMAL);
218 D1(printk(KERN_DEBUG "jffs2_create(): reserved 0x%x bytes\n", alloclen));
220 jffs2_free_raw_inode(ri);
224 inode = jffs2_new_inode(dir_i, mode, ri);
227 D1(printk(KERN_DEBUG "jffs2_new_inode() failed\n"));
228 jffs2_free_raw_inode(ri);
229 jffs2_complete_reservation(c);
230 return PTR_ERR(inode);
233 inode->i_op = &jffs2_file_inode_operations;
234 inode->i_fop = &jffs2_file_operations;
235 inode->i_mapping->a_ops = &jffs2_file_address_operations;
236 inode->i_mapping->nrpages = 0;
238 f = JFFS2_INODE_INFO(inode);
241 ri->node_crc = crc32(0, ri, sizeof(*ri)-8);
243 fn = jffs2_write_dnode(inode, ri, NULL, 0, phys_ofs, &writtenlen);
244 D1(printk(KERN_DEBUG "jffs2_create created file with mode 0x%x\n", ri->mode));
245 jffs2_free_raw_inode(ri);
248 D1(printk(KERN_DEBUG "jffs2_write_dnode() failed\n"));
249 /* Eeek. Wave bye bye */
251 jffs2_complete_reservation(c);
252 jffs2_clear_inode(inode);
255 /* No data here. Only a metadata node, which will be
256 obsoleted by the first data write
260 /* Work out where to put the dirent node now. */
261 writtenlen = PAD(writtenlen);
262 phys_ofs += writtenlen;
263 alloclen -= writtenlen;
266 if (alloclen < sizeof(*rd)+namelen) {
267 /* Not enough space left in this chunk. Get some more */
268 jffs2_complete_reservation(c);
269 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen, ALLOC_NORMAL);
273 D1(printk(KERN_DEBUG "jffs2_reserve_space() for dirent failed\n"));
274 jffs2_clear_inode(inode);
279 rd = jffs2_alloc_raw_dirent();
281 /* Argh. Now we treat it like a normal delete */
282 jffs2_complete_reservation(c);
283 jffs2_clear_inode(inode);
287 dir_f = JFFS2_INODE_INFO(dir_i);
290 rd->magic = JFFS2_MAGIC_BITMASK;
291 rd->nodetype = JFFS2_NODETYPE_DIRENT;
292 rd->totlen = sizeof(*rd) + namelen;
293 rd->hdr_crc = crc32(0, rd, sizeof(struct jffs2_unknown_node)-4);
295 rd->pino = dir_i->i_ino;
296 rd->version = ++dir_f->highest_version;
297 rd->ino = inode->i_ino;
298 rd->mctime = CURRENT_TIME;
301 rd->node_crc = crc32(0, rd, sizeof(*rd)-8);
302 rd->name_crc = crc32(0, dentry->d_name.name, namelen);
304 fd = jffs2_write_dirent(dir_i, rd, dentry->d_name.name, namelen, phys_ofs, &writtenlen);
306 jffs2_complete_reservation(c);
309 /* dirent failed to write. Delete the inode normally
310 as if it were the final unlink() */
311 jffs2_free_raw_dirent(rd);
313 jffs2_clear_inode(inode);
317 dir_i->i_mtime = dir_i->i_ctime = rd->mctime;
319 jffs2_free_raw_dirent(rd);
321 /* Link the fd into the inode's list, obsoleting an old
323 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
326 d_instantiate(dentry, inode);
328 D1(printk(KERN_DEBUG "jffs2_create: Created ino #%lu with mode %o, nlink %d(%d). nrpages %ld\n",
329 inode->i_ino, inode->i_mode, inode->i_nlink, f->inocache->nlink, inode->i_mapping->nrpages));
333 /***********************************************************************/
335 static int jffs2_do_unlink(struct inode *dir_i, struct dentry *dentry, int rename)
337 struct jffs2_inode_info *dir_f, *f;
338 struct jffs2_sb_info *c;
339 struct jffs2_raw_dirent *rd;
340 struct jffs2_full_dirent *fd;
341 __u32 alloclen, phys_ofs;
344 c = JFFS2_SB_INFO(dir_i->i_sb);
346 rd = jffs2_alloc_raw_dirent();
350 ret = jffs2_reserve_space(c, sizeof(*rd)+dentry->d_name.len, &phys_ofs, &alloclen, ALLOC_DELETION);
352 jffs2_free_raw_dirent(rd);
356 dir_f = JFFS2_INODE_INFO(dir_i);
359 /* Build a deletion node */
360 rd->magic = JFFS2_MAGIC_BITMASK;
361 rd->nodetype = JFFS2_NODETYPE_DIRENT;
362 rd->totlen = sizeof(*rd) + dentry->d_name.len;
363 rd->hdr_crc = crc32(0, rd, sizeof(struct jffs2_unknown_node)-4);
365 rd->pino = dir_i->i_ino;
366 rd->version = ++dir_f->highest_version;
368 rd->mctime = CURRENT_TIME;
369 rd->nsize = dentry->d_name.len;
370 rd->type = DT_UNKNOWN;
371 rd->node_crc = crc32(0, rd, sizeof(*rd)-8);
372 rd->name_crc = crc32(0, dentry->d_name.name, dentry->d_name.len);
374 fd = jffs2_write_dirent(dir_i, rd, dentry->d_name.name, dentry->d_name.len, phys_ofs, NULL);
376 jffs2_complete_reservation(c);
377 jffs2_free_raw_dirent(rd);
384 /* File it. This will mark the old one obsolete. */
385 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
389 f = JFFS2_INODE_INFO(dentry->d_inode);
393 /* There can be only deleted ones */
399 printk(KERN_WARNING "Deleting inode #%u with active dentry \"%s\"->ino #%u\n",
400 f->inocache->ino, fd->name, fd->ino);
402 D1(printk(KERN_DEBUG "Removing deletion dirent for \"%s\" from dir ino #%u\n", fd->name, f->inocache->ino));
404 jffs2_mark_node_obsolete(c, fd->raw);
405 jffs2_free_full_dirent(fd);
407 /* Don't oops on unlinking a bad inode */
409 f->inocache->nlink--;
410 dentry->d_inode->i_nlink--;
417 static int jffs2_unlink(struct inode *dir_i, struct dentry *dentry)
419 return jffs2_do_unlink(dir_i, dentry, 0);
421 /***********************************************************************/
423 static int jffs2_do_link (struct dentry *old_dentry, struct inode *dir_i, struct dentry *dentry, int rename)
425 struct jffs2_inode_info *dir_f, *f;
426 struct jffs2_sb_info *c;
427 struct jffs2_raw_dirent *rd;
428 struct jffs2_full_dirent *fd;
429 __u32 alloclen, phys_ofs;
432 c = JFFS2_SB_INFO(dir_i->i_sb);
434 rd = jffs2_alloc_raw_dirent();
438 ret = jffs2_reserve_space(c, sizeof(*rd)+dentry->d_name.len, &phys_ofs, &alloclen, ALLOC_NORMAL);
440 jffs2_free_raw_dirent(rd);
444 dir_f = JFFS2_INODE_INFO(dir_i);
447 /* Build a deletion node */
448 rd->magic = JFFS2_MAGIC_BITMASK;
449 rd->nodetype = JFFS2_NODETYPE_DIRENT;
450 rd->totlen = sizeof(*rd) + dentry->d_name.len;
451 rd->hdr_crc = crc32(0, rd, sizeof(struct jffs2_unknown_node)-4);
453 rd->pino = dir_i->i_ino;
454 rd->version = ++dir_f->highest_version;
455 rd->ino = old_dentry->d_inode->i_ino;
456 rd->mctime = CURRENT_TIME;
457 rd->nsize = dentry->d_name.len;
459 /* XXX: This is ugly. */
460 rd->type = (old_dentry->d_inode->i_mode & S_IFMT) >> 12;
461 if (!rd->type) rd->type = DT_REG;
463 rd->node_crc = crc32(0, rd, sizeof(*rd)-8);
464 rd->name_crc = crc32(0, dentry->d_name.name, dentry->d_name.len);
466 fd = jffs2_write_dirent(dir_i, rd, dentry->d_name.name, dentry->d_name.len, phys_ofs, NULL);
468 jffs2_complete_reservation(c);
469 jffs2_free_raw_dirent(rd);
476 /* File it. This will mark the old one obsolete. */
477 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
481 f = JFFS2_INODE_INFO(old_dentry->d_inode);
483 old_dentry->d_inode->i_nlink = ++f->inocache->nlink;
489 static int jffs2_link (struct dentry *old_dentry, struct inode *dir_i, struct dentry *dentry)
493 /* Can't link a bad inode. */
494 if (!JFFS2_INODE_INFO(old_dentry->d_inode)->inocache)
497 if (S_ISDIR(old_dentry->d_inode->i_mode))
500 ret = jffs2_do_link(old_dentry, dir_i, dentry, 0);
502 d_instantiate(dentry, old_dentry->d_inode);
503 atomic_inc(&old_dentry->d_inode->i_count);
508 /***********************************************************************/
510 static int jffs2_symlink (struct inode *dir_i, struct dentry *dentry, const char *target)
512 struct jffs2_inode_info *f, *dir_f;
513 struct jffs2_sb_info *c;
515 struct jffs2_raw_inode *ri;
516 struct jffs2_raw_dirent *rd;
517 struct jffs2_full_dnode *fn;
518 struct jffs2_full_dirent *fd;
520 __u32 alloclen, phys_ofs;
524 /* FIXME: If you care. We'd need to use frags for the target
525 if it grows much more than this */
526 if (strlen(target) > 254)
529 ri = jffs2_alloc_raw_inode();
534 c = JFFS2_SB_INFO(dir_i->i_sb);
536 /* Try to reserve enough space for both node and dirent.
537 * Just the node will do for now, though
539 namelen = dentry->d_name.len;
540 ret = jffs2_reserve_space(c, sizeof(*ri) + strlen(target), &phys_ofs, &alloclen, ALLOC_NORMAL);
543 jffs2_free_raw_inode(ri);
547 inode = jffs2_new_inode(dir_i, S_IFLNK | S_IRWXUGO, ri);
550 jffs2_free_raw_inode(ri);
551 jffs2_complete_reservation(c);
552 return PTR_ERR(inode);
555 inode->i_op = &jffs2_symlink_inode_operations;
557 f = JFFS2_INODE_INFO(inode);
559 inode->i_size = ri->isize = ri->dsize = ri->csize = strlen(target);
560 ri->totlen = sizeof(*ri) + ri->dsize;
561 ri->hdr_crc = crc32(0, ri, sizeof(struct jffs2_unknown_node)-4);
563 ri->compr = JFFS2_COMPR_NONE;
564 ri->data_crc = crc32(0, target, strlen(target));
565 ri->node_crc = crc32(0, ri, sizeof(*ri)-8);
567 fn = jffs2_write_dnode(inode, ri, target, strlen(target), phys_ofs, &writtenlen);
569 jffs2_free_raw_inode(ri);
572 /* Eeek. Wave bye bye */
574 jffs2_complete_reservation(c);
575 jffs2_clear_inode(inode);
578 /* No data here. Only a metadata node, which will be
579 obsoleted by the first data write
584 /* Work out where to put the dirent node now. */
585 writtenlen = (writtenlen+3)&~3;
586 phys_ofs += writtenlen;
587 alloclen -= writtenlen;
589 if (alloclen < sizeof(*rd)+namelen) {
590 /* Not enough space left in this chunk. Get some more */
591 jffs2_complete_reservation(c);
592 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen, ALLOC_NORMAL);
595 jffs2_clear_inode(inode);
600 rd = jffs2_alloc_raw_dirent();
602 /* Argh. Now we treat it like a normal delete */
603 jffs2_complete_reservation(c);
604 jffs2_clear_inode(inode);
608 dir_f = JFFS2_INODE_INFO(dir_i);
611 rd->magic = JFFS2_MAGIC_BITMASK;
612 rd->nodetype = JFFS2_NODETYPE_DIRENT;
613 rd->totlen = sizeof(*rd) + namelen;
614 rd->hdr_crc = crc32(0, rd, sizeof(struct jffs2_unknown_node)-4);
616 rd->pino = dir_i->i_ino;
617 rd->version = ++dir_f->highest_version;
618 rd->ino = inode->i_ino;
619 rd->mctime = CURRENT_TIME;
622 rd->node_crc = crc32(0, rd, sizeof(*rd)-8);
623 rd->name_crc = crc32(0, dentry->d_name.name, namelen);
625 fd = jffs2_write_dirent(dir_i, rd, dentry->d_name.name, namelen, phys_ofs, &writtenlen);
627 jffs2_complete_reservation(c);
630 /* dirent failed to write. Delete the inode normally
631 as if it were the final unlink() */
632 jffs2_free_raw_dirent(rd);
634 jffs2_clear_inode(inode);
638 dir_i->i_mtime = dir_i->i_ctime = rd->mctime;
640 jffs2_free_raw_dirent(rd);
642 /* Link the fd into the inode's list, obsoleting an old
644 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
647 d_instantiate(dentry, inode);
652 static int jffs2_mkdir (struct inode *dir_i, struct dentry *dentry, int mode)
654 struct jffs2_inode_info *f, *dir_f;
655 struct jffs2_sb_info *c;
657 struct jffs2_raw_inode *ri;
658 struct jffs2_raw_dirent *rd;
659 struct jffs2_full_dnode *fn;
660 struct jffs2_full_dirent *fd;
662 __u32 alloclen, phys_ofs;
668 ri = jffs2_alloc_raw_inode();
672 c = JFFS2_SB_INFO(dir_i->i_sb);
674 /* Try to reserve enough space for both node and dirent.
675 * Just the node will do for now, though
677 namelen = dentry->d_name.len;
678 ret = jffs2_reserve_space(c, sizeof(*ri), &phys_ofs, &alloclen, ALLOC_NORMAL);
681 jffs2_free_raw_inode(ri);
685 inode = jffs2_new_inode(dir_i, mode, ri);
688 jffs2_free_raw_inode(ri);
689 jffs2_complete_reservation(c);
690 return PTR_ERR(inode);
693 inode->i_op = &jffs2_dir_inode_operations;
694 inode->i_fop = &jffs2_dir_operations;
696 f = JFFS2_INODE_INFO(inode);
699 ri->node_crc = crc32(0, ri, sizeof(*ri)-8);
701 fn = jffs2_write_dnode(inode, ri, NULL, 0, phys_ofs, &writtenlen);
703 jffs2_free_raw_inode(ri);
706 /* Eeek. Wave bye bye */
708 jffs2_complete_reservation(c);
709 jffs2_clear_inode(inode);
712 /* No data here. Only a metadata node, which will be
713 obsoleted by the first data write
718 /* Work out where to put the dirent node now. */
719 writtenlen = PAD(writtenlen);
720 phys_ofs += writtenlen;
721 alloclen -= writtenlen;
723 if (alloclen < sizeof(*rd)+namelen) {
724 /* Not enough space left in this chunk. Get some more */
725 jffs2_complete_reservation(c);
726 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen, ALLOC_NORMAL);
729 jffs2_clear_inode(inode);
734 rd = jffs2_alloc_raw_dirent();
736 /* Argh. Now we treat it like a normal delete */
737 jffs2_complete_reservation(c);
738 jffs2_clear_inode(inode);
742 dir_f = JFFS2_INODE_INFO(dir_i);
745 rd->magic = JFFS2_MAGIC_BITMASK;
746 rd->nodetype = JFFS2_NODETYPE_DIRENT;
747 rd->totlen = sizeof(*rd) + namelen;
748 rd->hdr_crc = crc32(0, rd, sizeof(struct jffs2_unknown_node)-4);
750 rd->pino = dir_i->i_ino;
751 rd->version = ++dir_f->highest_version;
752 rd->ino = inode->i_ino;
753 rd->mctime = CURRENT_TIME;
756 rd->node_crc = crc32(0, rd, sizeof(*rd)-8);
757 rd->name_crc = crc32(0, dentry->d_name.name, namelen);
759 fd = jffs2_write_dirent(dir_i, rd, dentry->d_name.name, namelen, phys_ofs, &writtenlen);
761 jffs2_complete_reservation(c);
764 /* dirent failed to write. Delete the inode normally
765 as if it were the final unlink() */
766 jffs2_free_raw_dirent(rd);
768 jffs2_clear_inode(inode);
772 dir_i->i_mtime = dir_i->i_ctime = rd->mctime;
774 jffs2_free_raw_dirent(rd);
776 /* Link the fd into the inode's list, obsoleting an old
778 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
781 d_instantiate(dentry, inode);
785 static int jffs2_rmdir (struct inode *dir_i, struct dentry *dentry)
787 struct jffs2_inode_info *f = JFFS2_INODE_INFO(dentry->d_inode);
788 struct jffs2_full_dirent *fd;
790 for (fd = f->dents ; fd; fd = fd->next) {
794 return jffs2_unlink(dir_i, dentry);
797 static int jffs2_mknod (struct inode *dir_i, struct dentry *dentry, int mode, int rdev)
799 struct jffs2_inode_info *f, *dir_f;
800 struct jffs2_sb_info *c;
802 struct jffs2_raw_inode *ri;
803 struct jffs2_raw_dirent *rd;
804 struct jffs2_full_dnode *fn;
805 struct jffs2_full_dirent *fd;
809 __u32 alloclen, phys_ofs;
813 ri = jffs2_alloc_raw_inode();
817 c = JFFS2_SB_INFO(dir_i->i_sb);
819 if (S_ISBLK(mode) || S_ISCHR(mode)) {
820 dev = (MAJOR(to_kdev_t(rdev)) << 8) | MINOR(to_kdev_t(rdev));
821 devlen = sizeof(dev);
824 /* Try to reserve enough space for both node and dirent.
825 * Just the node will do for now, though
827 namelen = dentry->d_name.len;
828 ret = jffs2_reserve_space(c, sizeof(*ri) + devlen, &phys_ofs, &alloclen, ALLOC_NORMAL);
831 jffs2_free_raw_inode(ri);
835 inode = jffs2_new_inode(dir_i, mode, ri);
838 jffs2_free_raw_inode(ri);
839 jffs2_complete_reservation(c);
840 return PTR_ERR(inode);
842 inode->i_op = &jffs2_file_inode_operations;
843 init_special_inode(inode, inode->i_mode, rdev);
845 f = JFFS2_INODE_INFO(inode);
847 ri->dsize = ri->csize = devlen;
848 ri->totlen = sizeof(*ri) + ri->csize;
849 ri->hdr_crc = crc32(0, ri, sizeof(struct jffs2_unknown_node)-4);
851 ri->compr = JFFS2_COMPR_NONE;
852 ri->data_crc = crc32(0, &dev, devlen);
853 ri->node_crc = crc32(0, ri, sizeof(*ri)-8);
855 fn = jffs2_write_dnode(inode, ri, (char *)&dev, devlen, phys_ofs, &writtenlen);
857 jffs2_free_raw_inode(ri);
860 /* Eeek. Wave bye bye */
862 jffs2_complete_reservation(c);
863 jffs2_clear_inode(inode);
866 /* No data here. Only a metadata node, which will be
867 obsoleted by the first data write
872 /* Work out where to put the dirent node now. */
873 writtenlen = (writtenlen+3)&~3;
874 phys_ofs += writtenlen;
875 alloclen -= writtenlen;
877 if (alloclen < sizeof(*rd)+namelen) {
878 /* Not enough space left in this chunk. Get some more */
879 jffs2_complete_reservation(c);
880 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen, ALLOC_NORMAL);
883 jffs2_clear_inode(inode);
888 rd = jffs2_alloc_raw_dirent();
890 /* Argh. Now we treat it like a normal delete */
891 jffs2_complete_reservation(c);
892 jffs2_clear_inode(inode);
896 dir_f = JFFS2_INODE_INFO(dir_i);
899 rd->magic = JFFS2_MAGIC_BITMASK;
900 rd->nodetype = JFFS2_NODETYPE_DIRENT;
901 rd->totlen = sizeof(*rd) + namelen;
902 rd->hdr_crc = crc32(0, rd, sizeof(struct jffs2_unknown_node)-4);
904 rd->pino = dir_i->i_ino;
905 rd->version = ++dir_f->highest_version;
906 rd->ino = inode->i_ino;
907 rd->mctime = CURRENT_TIME;
910 /* XXX: This is ugly. */
911 rd->type = (mode & S_IFMT) >> 12;
913 rd->node_crc = crc32(0, rd, sizeof(*rd)-8);
914 rd->name_crc = crc32(0, dentry->d_name.name, namelen);
916 fd = jffs2_write_dirent(dir_i, rd, dentry->d_name.name, namelen, phys_ofs, &writtenlen);
918 jffs2_complete_reservation(c);
921 /* dirent failed to write. Delete the inode normally
922 as if it were the final unlink() */
923 jffs2_free_raw_dirent(rd);
925 jffs2_clear_inode(inode);
929 dir_i->i_mtime = dir_i->i_ctime = rd->mctime;
931 jffs2_free_raw_dirent(rd);
933 /* Link the fd into the inode's list, obsoleting an old
935 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
938 d_instantiate(dentry, inode);
943 static int jffs2_rename (struct inode *old_dir_i, struct dentry *old_dentry,
944 struct inode *new_dir_i, struct dentry *new_dentry)
947 struct jffs2_inode_info *victim_f = NULL;
949 /* The VFS will check for us and prevent trying to rename a
950 * file over a directory and vice versa, but if it's a directory,
951 * the VFS can't check whether the victim is empty. The filesystem
952 * needs to do that for itself.
954 if (new_dentry->d_inode) {
955 victim_f = JFFS2_INODE_INFO(new_dentry->d_inode);
956 if (S_ISDIR(new_dentry->d_inode->i_mode)) {
957 struct jffs2_full_dirent *fd;
959 down(&victim_f->sem);
960 for (fd = victim_f->dents; fd; fd = fd->next) {
970 /* XXX: We probably ought to alloc enough space for
971 both nodes at the same time. Writing the new link,
972 then getting -ENOSPC, is quite bad :)
975 /* Make a hard link */
976 ret = jffs2_do_link(old_dentry, new_dir_i, new_dentry, 1);
981 /* There was a victim. Kill it off nicely */
982 new_dentry->d_inode->i_nlink--;
983 /* Don't oops if the victim was a dirent pointing to an
984 inode which didn't exist. */
985 if (victim_f->inocache) {
986 down(&victim_f->sem);
987 victim_f->inocache->nlink--;
992 /* Unlink the original */
993 ret = jffs2_do_unlink(old_dir_i, old_dentry, 1);
996 /* Oh shit. We really ought to make a single node which can do both atomically */
997 struct jffs2_inode_info *f = JFFS2_INODE_INFO(old_dentry->d_inode);
1000 old_dentry->d_inode->i_nlink = f->inocache->nlink++;
1003 printk(KERN_NOTICE "jffs2_rename(): Link succeeded, unlink failed (err %d). You now have a hard link\n", ret);
1004 /* Might as well let the VFS know */
1005 d_instantiate(new_dentry, old_dentry->d_inode);
1006 atomic_inc(&old_dentry->d_inode->i_count);