2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright (C) 2001-2003 Red Hat, Inc.
6 * Created by Dominic Ostrowski <dominic.ostrowski@3glab.com>
7 * Contributors: David Woodhouse, Nick Garnett, Richard Panton.
9 * For licensing information, see the file 'LICENCE' in this directory.
15 #include <linux/kernel.h>
17 #include <linux/pagemap.h>
18 #include <linux/crc32.h>
22 #include <cyg/io/config_keys.h>
24 #if (__GNUC__ == 3) && (__GNUC_MINOR__ == 2) && \
25 (defined (__arm__) || defined (_mips))
26 #error This compiler is known to be broken. Please see:
27 #error "http://ecos.sourceware.org/ml/ecos-patches/2003-08/msg00006.html"
30 //==========================================================================
31 // Forward definitions
33 // Filesystem operations
34 static int jffs2_mount(cyg_fstab_entry * fste, cyg_mtab_entry * mte);
35 static int jffs2_umount(cyg_mtab_entry * mte);
36 static int jffs2_open(cyg_mtab_entry * mte, cyg_dir dir, const char *name,
37 int mode, cyg_file * fte);
38 #ifdef CYGOPT_FS_JFFS2_WRITE
39 static int jffs2_ops_unlink(cyg_mtab_entry * mte, cyg_dir dir,
41 static int jffs2_ops_mkdir(cyg_mtab_entry * mte, cyg_dir dir, const char *name);
42 static int jffs2_ops_rmdir(cyg_mtab_entry * mte, cyg_dir dir, const char *name);
43 static int jffs2_ops_rename(cyg_mtab_entry * mte, cyg_dir dir1,
44 const char *name1, cyg_dir dir2, const char *name2);
45 static int jffs2_ops_link(cyg_mtab_entry * mte, cyg_dir dir1, const char *name1,
46 cyg_dir dir2, const char *name2, int type);
48 static int jffs2_opendir(cyg_mtab_entry * mte, cyg_dir dir, const char *name,
50 static int jffs2_chdir(cyg_mtab_entry * mte, cyg_dir dir, const char *name,
52 static int jffs2_stat(cyg_mtab_entry * mte, cyg_dir dir, const char *name,
54 static int jffs2_getinfo(cyg_mtab_entry * mte, cyg_dir dir, const char *name,
55 int key, void *buf, int len);
56 static int jffs2_setinfo(cyg_mtab_entry * mte, cyg_dir dir, const char *name,
57 int key, void *buf, int len);
60 static int jffs2_fo_read(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio);
61 #ifdef CYGOPT_FS_JFFS2_WRITE
62 static int jffs2_fo_write(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio);
64 static int jffs2_fo_lseek(struct CYG_FILE_TAG *fp, off_t * pos, int whence);
65 static int jffs2_fo_ioctl(struct CYG_FILE_TAG *fp, CYG_ADDRWORD com,
67 static int jffs2_fo_fsync(struct CYG_FILE_TAG *fp, int mode);
68 static int jffs2_fo_close(struct CYG_FILE_TAG *fp);
69 static int jffs2_fo_fstat(struct CYG_FILE_TAG *fp, struct stat *buf);
70 static int jffs2_fo_getinfo(struct CYG_FILE_TAG *fp, int key, void *buf,
72 static int jffs2_fo_setinfo(struct CYG_FILE_TAG *fp, int key, void *buf,
75 // Directory operations
76 static int jffs2_fo_dirread(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio);
77 static int jffs2_fo_dirlseek(struct CYG_FILE_TAG *fp, off_t * pos, int whence);
80 static int jffs2_read_inode (struct _inode *inode);
81 static void jffs2_clear_inode (struct _inode *inode);
82 static int jffs2_truncate_file (struct _inode *inode);
84 //==========================================================================
85 // Filesystem table entries
87 // -------------------------------------------------------------------------
89 // This defines the entry in the filesystem table.
90 // For simplicity we use _FILESYSTEM synchronization for all accesses since
91 // we should never block in any filesystem operations.
93 #ifdef CYGOPT_FS_JFFS2_WRITE
94 FSTAB_ENTRY(jffs2_fste, "jffs2", 0,
95 CYG_SYNCMODE_FILE_FILESYSTEM | CYG_SYNCMODE_IO_FILESYSTEM,
105 jffs2_chdir, jffs2_stat, jffs2_getinfo, jffs2_setinfo);
107 FSTAB_ENTRY(jffs2_fste, "jffs2", 0,
108 CYG_SYNCMODE_FILE_FILESYSTEM | CYG_SYNCMODE_IO_FILESYSTEM,
112 (cyg_fsop_unlink *)cyg_fileio_erofs,
113 (cyg_fsop_mkdir *)cyg_fileio_erofs,
114 (cyg_fsop_rmdir *)cyg_fileio_erofs,
115 (cyg_fsop_rename *)cyg_fileio_erofs,
116 (cyg_fsop_link *)cyg_fileio_erofs,
118 jffs2_chdir, jffs2_stat, jffs2_getinfo, jffs2_setinfo);
121 // -------------------------------------------------------------------------
123 // This set of file operations are used for normal open files.
125 static cyg_fileops jffs2_fileops = {
127 #ifdef CYGOPT_FS_JFFS2_WRITE
130 (cyg_fileop_write *) cyg_fileio_erofs,
142 // -------------------------------------------------------------------------
143 // Directory file operations.
144 // This set of operations are used for open directories. Most entries
145 // point to error-returning stub functions. Only the read, lseek and
146 // close entries are functional.
148 static cyg_fileops jffs2_dirops = {
150 (cyg_fileop_write *) cyg_fileio_enosys,
152 (cyg_fileop_ioctl *) cyg_fileio_enosys,
154 (cyg_fileop_fsync *) cyg_fileio_enosys,
156 (cyg_fileop_fstat *) cyg_fileio_enosys,
157 (cyg_fileop_getinfo *) cyg_fileio_enosys,
158 (cyg_fileop_setinfo *) cyg_fileio_enosys
161 //==========================================================================
162 // STATIC VARIABLES !!!
164 static unsigned char gc_buffer[PAGE_CACHE_SIZE]; //avoids malloc when user may be under memory pressure
165 static unsigned char n_fs_mounted = 0; // a counter to track the number of jffs2 instances mounted
167 //==========================================================================
168 // Directory operations
170 struct jffs2_dirsearch {
171 struct _inode *dir; // directory to search
172 const unsigned char *path; // path to follow
173 struct _inode *node; // Node found
174 const unsigned char *name; // last name fragment used
175 int namelen; // name fragment length
176 cyg_bool last; // last name in path?
179 typedef struct jffs2_dirsearch jffs2_dirsearch;
181 //==========================================================================
182 // Ref count and nlink management
185 // FIXME: This seems like real cruft. Wouldn't it be better just to do the
187 static void icache_evict(struct _inode *root_i, struct _inode *i)
189 struct _inode *this = root_i, *next;
192 D2(printf("icache_evict\n"));
193 // If this is an absolute search path from the root,
194 // remove all cached inodes with i_count of zero (these are only
195 // held where needed for dotdot filepaths)
197 next = this->i_cache_next;
198 if (this != i && this->i_count == 0) {
199 struct _inode *parent = this->i_parent;
200 if (this->i_cache_next)
201 this->i_cache_next->i_cache_prev = this->i_cache_prev;
202 if (this->i_cache_prev)
203 this->i_cache_prev->i_cache_next = this->i_cache_next;
204 jffs2_clear_inode(this);
205 memset(this, 0x5a, sizeof(*this));
207 if (parent && parent != this) {
217 //==========================================================================
220 // -------------------------------------------------------------------------
222 // Initialize a dirsearch object to start a search
224 static void init_dirsearch(jffs2_dirsearch * ds,
225 struct _inode *dir, const unsigned char *name)
227 D2(printf("init_dirsearch name = %s\n", name));
228 D2(printf("init_dirsearch dir = %x\n", dir));
239 // -------------------------------------------------------------------------
241 // Search a single directory for the next name in a path and update the
242 // dirsearch object appropriately.
244 static int find_entry(jffs2_dirsearch * ds)
246 struct _inode *dir = ds->dir;
247 const unsigned char *name = ds->path;
248 const unsigned char *n = name;
252 D2(printf("find_entry\n"));
254 // check that we really have a directory
255 if (!S_ISDIR(dir->i_mode))
258 // Isolate the next element of the path name.
259 while (*n != '\0' && *n != '/')
262 // Check if this is the last path element.
263 while( *n == '/') n++;
267 // update name in dirsearch object
269 ds->namelen = namelen;
276 // Dot followed by not Dot, treat as any other name
280 // Move back up the search path
281 D2(printf("find_entry found ..\n"));
283 ds->node = ds->dir->i_parent;
288 D2(printf("find_entry found .\n"));
294 // Here we have the name and its length set up.
295 // Search the directory for a matching entry
297 D2(printf("find_entry for name = %s\n", ds->path));
298 d = jffs2_lookup(dir, name, namelen);
299 D2(printf("find_entry got dir = %x\n", d));
306 // If it's a new directory inode, increase refcount on its parent
307 if (S_ISDIR(d->i_mode) && !d->i_parent) {
312 // pass back the node we have found
318 // -------------------------------------------------------------------------
320 // Main interface to directory search code. This is used in all file
321 // level operations to locate the object named by the pathname.
323 // Returns with use count incremented on both the sought object and
324 // the directory it was found in
325 static int jffs2_find(jffs2_dirsearch * d)
329 D2(printf("jffs2_find for path =%s\n", d->path));
331 // Short circuit empty paths
332 if (*(d->path) == '\0') {
337 // iterate down directory tree until we find the object
348 /* We're done with it, although it we found a subdir that
349 will have caused the refcount to have been increased */
352 // Update dirsearch object to search next directory.
354 d->path += d->namelen;
355 while (*(d->path) == '/')
356 d->path++; // skip dirname separators
360 //==========================================================================
362 // This function provides support for pathconf() and fpathconf().
364 static int jffs2_pathconf(struct _inode *node, struct cyg_pathconf_info *info)
367 D2(printf("jffs2_pathconf\n"));
369 switch (info->name) {
371 info->value = LINK_MAX;
375 info->value = -1; // not supported
380 info->value = -1; // not supported
385 info->value = NAME_MAX;
389 info->value = PATH_MAX;
393 info->value = -1; // not supported
398 info->value = -1; // not supported
402 case _PC_CHOWN_RESTRICTED:
403 info->value = -1; // not supported
420 info->value = -1; // not supported
432 //==========================================================================
433 // Filesystem operations
435 // -------------------------------------------------------------------------
437 // Process a mount request. This mainly creates a root for the
439 static int jffs2_read_super(struct super_block *sb)
441 struct jffs2_sb_info *c;
444 cyg_io_flash_getconfig_devsize_t ds;
445 cyg_io_flash_getconfig_blocksize_t bs;
447 D1(printk(KERN_DEBUG "jffs2: read_super\n"));
449 c = JFFS2_SB_INFO(sb);
452 err = cyg_io_get_config(sb->s_dev,
453 CYG_IO_GET_CONFIG_FLASH_DEVSIZE, &ds, &len);
456 ("jffs2: cyg_io_get_config failed to get dev size: %d\n",
462 err = cyg_io_get_config(sb->s_dev,
463 CYG_IO_GET_CONFIG_FLASH_BLOCKSIZE, &bs, &len);
466 ("jffs2: cyg_io_get_config failed to get block size: %d\n",
471 c->sector_size = bs.block_size;
472 c->flash_size = ds.dev_size;
473 c->cleanmarker_size = sizeof(struct jffs2_unknown_node);
475 err = jffs2_do_mount_fs(c);
479 D1(printk(KERN_DEBUG "jffs2_read_super(): Getting root inode\n"));
480 sb->s_root = jffs2_iget(sb, 1);
481 if (IS_ERR(sb->s_root)) {
482 D1(printk(KERN_WARNING "get root inode failed\n"));
483 err = PTR_ERR(sb->s_root);
491 jffs2_free_ino_caches(c);
492 jffs2_free_raw_node_refs(c);
498 static int jffs2_mount(cyg_fstab_entry * fste, cyg_mtab_entry * mte)
500 extern cyg_mtab_entry cyg_mtab[], cyg_mtab_end;
501 struct super_block *jffs2_sb = NULL;
502 struct jffs2_sb_info *c;
507 D2(printf("jffs2_mount\n"));
509 err = cyg_io_lookup(mte->devname, &t);
513 // Iterate through the mount table to see if we're mounted
514 // FIXME: this should be done better - perhaps if the superblock
515 // can be stored as an inode in the icache.
516 for (m = &cyg_mtab[0]; m != &cyg_mtab_end; m++) {
517 // stop if there are more than the configured maximum
518 if (m - &cyg_mtab[0] >= CYGNUM_FILEIO_MTAB_MAX) {
522 if (m->valid && strcmp(m->fsname, "jffs2") == 0 &&
523 strcmp(m->devname, mte->devname) == 0) {
524 jffs2_sb = (struct super_block *) m->data;
528 if (jffs2_sb == NULL) {
529 jffs2_sb = malloc(sizeof (struct super_block));
531 if (jffs2_sb == NULL)
534 c = JFFS2_SB_INFO(jffs2_sb);
535 memset(jffs2_sb, 0, sizeof (struct super_block));
538 c->inocache_list = malloc(sizeof(struct jffs2_inode_cache *) * INOCACHE_HASHSIZE);
539 if (!c->inocache_list) {
543 memset(c->inocache_list, 0, sizeof(struct jffs2_inode_cache *) * INOCACHE_HASHSIZE);
544 if (n_fs_mounted++ == 0) {
545 jffs2_create_slab_caches(); // No error check, cannot fail
546 jffs2_compressors_init();
549 err = jffs2_read_super(jffs2_sb);
552 if (--n_fs_mounted == 0) {
553 jffs2_destroy_slab_caches();
554 jffs2_compressors_exit();
558 free(c->inocache_list);
562 jffs2_sb->s_root->i_parent = jffs2_sb->s_root; // points to itself, no dotdot paths above mountpoint
563 jffs2_sb->s_root->i_cache_prev = NULL; // root inode, so always null
564 jffs2_sb->s_root->i_cache_next = NULL;
565 jffs2_sb->s_root->i_count = 1; // Ensures the root inode is always in ram until umount
567 D2(printf("jffs2_mount erasing pending blocks\n"));
568 #ifdef CYGOPT_FS_JFFS2_WRITE
569 if (!jffs2_is_readonly(c))
570 jffs2_erase_pending_blocks(c,0);
572 #ifdef CYGOPT_FS_JFFS2_GCTHREAD
573 jffs2_start_garbage_collect_thread(c);
576 mte->data = (CYG_ADDRWORD) jffs2_sb;
578 jffs2_sb->s_mount_count++;
579 mte->root = (cyg_dir) jffs2_sb->s_root;
580 D2(printf("jffs2_mounted superblock at %x\n", mte->root));
585 extern cyg_dir cyg_cdir_dir;
586 extern cyg_mtab_entry *cyg_cdir_mtab_entry;
588 // -------------------------------------------------------------------------
590 // Unmount the filesystem.
592 static int jffs2_umount(cyg_mtab_entry * mte)
594 struct _inode *root = (struct _inode *) mte->root;
595 struct super_block *jffs2_sb = root->i_sb;
596 struct jffs2_sb_info *c = JFFS2_SB_INFO(jffs2_sb);
597 struct jffs2_full_dirent *fd, *next;
599 D2(printf("jffs2_umount\n"));
601 // Only really umount if this is the only mount
602 if (jffs2_sb->s_mount_count == 1) {
603 icache_evict(root, NULL);
604 if (root->i_cache_next != NULL) {
605 struct _inode *inode = root;
606 printf("Refuse to unmount.\n");
608 printf("Ino #%u has use count %d\n",
609 inode->i_ino, inode->i_count);
610 inode = inode->i_cache_next;
612 // root icount was set to 1 on mount
615 if (root->i_count == 2 &&
616 cyg_cdir_mtab_entry == mte &&
617 cyg_cdir_dir == (cyg_dir)root &&
618 !strcmp(mte->name, "/")) {
619 /* If we were mounted on root, there's no
620 way for the cwd to change out and free
621 the file system for unmounting. So we hack
622 it -- if cwd is '/' we unset it. Perhaps
623 we should allow chdir(NULL) to unset
625 cyg_cdir_dir = CYG_DIR_NULL;
628 /* Argh. The fileio code sets this; never clears it */
629 if (cyg_cdir_mtab_entry == mte)
630 cyg_cdir_mtab_entry = NULL;
632 if (root->i_count != 1) {
633 printf("Ino #1 has use count %d\n",
637 #ifdef CYGOPT_FS_JFFS2_GCTHREAD
638 jffs2_stop_garbage_collect_thread(c);
640 jffs2_iput(root); // Time to free the root inode
642 // free directory entries
643 for (fd = root->jffs2_i.dents; fd; fd = next) {
645 jffs2_free_full_dirent(fd);
652 // Clean up the super block and root inode
653 jffs2_free_ino_caches(c);
654 jffs2_free_raw_node_refs(c);
656 free(c->inocache_list);
658 // Clear superblock & root pointer
659 mte->root = CYG_DIR_NULL;
661 mte->fs->data = 0; // fstab entry, visible to all mounts. No current mount
663 D2(printf("jffs2_umount No current mounts\n"));
665 jffs2_sb->s_mount_count--;
667 if (--n_fs_mounted == 0) {
668 jffs2_destroy_slab_caches();
669 jffs2_compressors_exit();
674 // -------------------------------------------------------------------------
676 // Open a file for reading or writing.
678 static int jffs2_open(cyg_mtab_entry * mte, cyg_dir dir, const char *name,
679 int mode, cyg_file * file)
683 struct _inode *node = NULL;
686 D2(printf("jffs2_open\n"));
688 /* If no chdir has been called and we were the first file system
689 mounted, we get called with dir == NULL. Deal with it */
693 #ifndef CYGOPT_FS_JFFS2_WRITE
694 if (mode & (O_CREAT|O_TRUNC|O_WRONLY))
697 init_dirsearch(&ds, (struct _inode *) dir,
698 (const unsigned char *) name);
700 err = jffs2_find(&ds);
703 #ifdef CYGOPT_FS_JFFS2_WRITE
704 if (ds.last && (mode & O_CREAT)) {
706 // No node there, if the O_CREAT bit is set then we must
707 // create a new one. The dir and name fields of the dirsearch
708 // object will have been updated so we know where to put it.
710 err = jffs2_create(ds.dir, ds.name, S_IRUGO|S_IXUGO|S_IWUSR|S_IFREG, &node);
713 //Possible orphaned inode on the flash - but will be gc'd
721 } else if (err == ENOERR) {
722 // The node exists. If the O_CREAT and O_EXCL bits are set, we
723 // must fail the open.
725 if ((mode & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) {
732 // Finished with the directory now
738 // Check that we actually have a file here
739 if (S_ISDIR(node->i_mode)) {
744 // If the O_TRUNC bit is set we must clean out the file data.
745 if (mode & O_TRUNC) {
746 #ifdef CYGOPT_FS_JFFS2_WRITE
747 err = jffs2_truncate_file(node);
758 // Initialise the file object
759 file->f_flag |= mode & CYG_FILE_MODE_MASK;
760 file->f_type = CYG_FILE_TYPE_FILE;
761 file->f_ops = &jffs2_fileops;
762 file->f_offset = (mode & O_APPEND) ? node->i_size : 0;
763 file->f_data = (CYG_ADDRWORD) node;
769 #ifdef CYGOPT_FS_JFFS2_WRITE
770 // -------------------------------------------------------------------------
771 // jffs2_ops_unlink()
772 // Remove a file link from its directory.
774 static int jffs2_ops_unlink(cyg_mtab_entry * mte, cyg_dir dir, const char *name)
779 D2(printf("jffs2_ops_unlink\n"));
781 init_dirsearch(&ds, (struct _inode *) dir,
782 (const unsigned char *)name);
784 err = jffs2_find(&ds);
791 // Cannot unlink directories, use rmdir() instead
792 if (S_ISDIR(ds.node->i_mode)) {
798 // Delete it from its directory
800 err = jffs2_unlink(ds.dir, ds.node, ds.name);
807 // -------------------------------------------------------------------------
809 // Create a new directory.
811 static int jffs2_ops_mkdir(cyg_mtab_entry * mte, cyg_dir dir, const char *name)
816 D2(printf("jffs2_ops_mkdir\n"));
818 init_dirsearch(&ds, (struct _inode *) dir,
819 (const unsigned char *)name);
821 err = jffs2_find(&ds);
825 // The entry does not exist, and it is the last element in
826 // the pathname, so we can create it here.
828 err = -jffs2_mkdir(ds.dir, ds.name, S_IRUGO|S_IXUGO|S_IWUSR);
830 // If this was not the last element, then an intermediate
831 // directory does not exist.
833 // If there we no error, something already exists with that
834 // name, so we cannot create another one.
844 // -------------------------------------------------------------------------
846 // Remove a directory.
848 static int jffs2_ops_rmdir(cyg_mtab_entry * mte, cyg_dir dir, const char *name)
853 D2(printf("jffs2_ops_rmdir\n"));
855 init_dirsearch(&ds, (struct _inode *) dir,
856 (const unsigned char *)name);
858 err = jffs2_find(&ds);
865 // Check that this is actually a directory.
866 if (!S_ISDIR(ds.node->i_mode)) {
872 err = jffs2_rmdir(ds.dir, ds.node, ds.name);
879 // -------------------------------------------------------------------------
880 // jffs2_ops_rename()
881 // Rename a file/dir.
883 static int jffs2_ops_rename(cyg_mtab_entry * mte, cyg_dir dir1,
884 const char *name1, cyg_dir dir2, const char *name2)
886 jffs2_dirsearch ds1, ds2;
889 D2(printf("jffs2_ops_rename\n"));
891 init_dirsearch(&ds1, (struct _inode *) dir1,
892 (const unsigned char *)name1);
894 err = jffs2_find(&ds1);
901 init_dirsearch(&ds2, (struct _inode *) dir2,
902 (const unsigned char *)name2);
904 err = jffs2_find(&ds2);
906 // Allow through renames to non-existent objects.
907 if (ds2.last && err == ENOENT) {
914 jffs2_iput(ds1.node);
919 // Null rename, just return
920 if (ds1.node == ds2.node) {
925 // First deal with any entry that is at the destination
927 // Check that we are renaming like-for-like
929 if (!S_ISDIR(ds1.node->i_mode) && S_ISDIR(ds2.node->i_mode)) {
934 if (S_ISDIR(ds1.node->i_mode) && !S_ISDIR(ds2.node->i_mode)) {
939 // Now delete the destination directory entry
940 /* Er, what happened to atomicity of rename()? */
941 err = -jffs2_unlink(ds2.dir, ds2.node, ds2.name);
947 // Now we know that there is no clashing node at the destination,
948 // make a new direntry at the destination and delete the old entry
951 err = -jffs2_rename(ds1.dir, ds1.node, ds1.name, ds2.dir, ds2.name);
953 // Update directory times
957 ds2.dir->i_ctime = ds2.dir->i_mtime = cyg_timestamp();
960 if (S_ISDIR(ds1.node->i_mode)) {
961 /* Renamed a directory to elsewhere... so fix up its
962 i_parent pointer and the i_counts of its old and
964 jffs2_iput(ds1.node->i_parent);
965 ds1.node->i_parent = ds2.dir;
966 /* We effectively increase its use count by not... */
968 jffs2_iput(ds2.dir); /* ... doing this */
970 jffs2_iput(ds1.node);
972 jffs2_iput(ds2.node);
977 // -------------------------------------------------------------------------
979 // Make a new directory entry for a file.
981 static int jffs2_ops_link(cyg_mtab_entry * mte, cyg_dir dir1, const char *name1,
982 cyg_dir dir2, const char *name2, int type)
984 jffs2_dirsearch ds1, ds2;
987 D2(printf("jffs2_ops_link\n"));
989 // Only do hard links for now in this filesystem
990 if (type != CYG_FSLINK_HARD)
993 init_dirsearch(&ds1, (struct _inode *) dir1,
994 (const unsigned char *) name1);
996 err = jffs2_find(&ds1);
1003 init_dirsearch(&ds2, (struct _inode *) dir2,
1004 (const unsigned char *) name2);
1006 err = jffs2_find(&ds2);
1008 // Don't allow links to existing objects
1009 if (err == ENOERR) {
1010 jffs2_iput(ds1.dir);
1011 jffs2_iput(ds1.node);
1012 jffs2_iput(ds2.dir);
1013 jffs2_iput(ds2.node);
1017 // Allow through links to non-existing terminal objects
1018 if (ds2.last && err == ENOENT) {
1023 if (err != ENOERR) {
1024 jffs2_iput(ds1.dir);
1025 jffs2_iput(ds1.node);
1026 jffs2_iput(ds2.dir);
1030 // Now we know that there is no existing node at the destination,
1031 // make a new direntry at the destination.
1033 err = jffs2_link(ds1.node, ds2.dir, ds2.name);
1037 ds2.dir->i_ctime = ds2.dir->i_mtime = cyg_timestamp();
1039 jffs2_iput(ds1.dir);
1040 jffs2_iput(ds1.node);
1041 jffs2_iput(ds2.dir);
1045 #endif /* CYGOPT_FS_JFFS2_WRITE */
1046 // -------------------------------------------------------------------------
1048 // Open a directory for reading.
1050 static int jffs2_opendir(cyg_mtab_entry * mte, cyg_dir dir, const char *name,
1056 D2(printf("jffs2_opendir\n"));
1058 init_dirsearch(&ds, (struct _inode *) dir,
1059 (const unsigned char *) name);
1061 err = jffs2_find(&ds);
1068 // check it is really a directory.
1069 if (!S_ISDIR(ds.node->i_mode)) {
1070 jffs2_iput(ds.node);
1074 // Initialize the file object, setting the f_ops field to a
1075 // special set of file ops.
1077 file->f_type = CYG_FILE_TYPE_FILE;
1078 file->f_ops = &jffs2_dirops;
1080 file->f_data = (CYG_ADDRWORD) ds.node;
1087 // -------------------------------------------------------------------------
1089 // Change directory support.
1091 static int jffs2_chdir(cyg_mtab_entry * mte, cyg_dir dir, const char *name,
1094 D2(printf("jffs2_chdir\n"));
1096 if (dir_out != NULL) {
1097 // This is a request to get a new directory pointer in
1103 init_dirsearch(&ds, (struct _inode *) dir,
1104 (const unsigned char *) name);
1106 err = jffs2_find(&ds);
1112 // check it is a directory
1113 if (!S_ISDIR(ds.node->i_mode)) {
1114 jffs2_iput(ds.node);
1119 *dir_out = (cyg_dir) ds.node;
1121 // If no output dir is required, this means that the mte and
1122 // dir arguments are the current cdir setting and we should
1123 // forget this fact.
1125 struct _inode *node = (struct _inode *) dir;
1127 // Just decrement directory reference count.
1134 // -------------------------------------------------------------------------
1136 // Get struct stat info for named object.
1138 static int jffs2_stat(cyg_mtab_entry * mte, cyg_dir dir, const char *name,
1144 D2(printf("jffs2_stat\n"));
1146 init_dirsearch(&ds, (struct _inode *) dir,
1147 (const unsigned char *) name);
1149 err = jffs2_find(&ds);
1155 // Fill in the status
1156 buf->st_mode = ds.node->i_mode;
1157 buf->st_ino = ds.node->i_ino;
1159 buf->st_nlink = ds.node->i_nlink;
1160 buf->st_uid = ds.node->i_uid;
1161 buf->st_gid = ds.node->i_gid;
1162 buf->st_size = ds.node->i_size;
1163 buf->st_atime = ds.node->i_atime;
1164 buf->st_mtime = ds.node->i_mtime;
1165 buf->st_ctime = ds.node->i_ctime;
1167 jffs2_iput(ds.node);
1172 // -------------------------------------------------------------------------
1174 // Getinfo. Currently only support pathconf().
1176 static int jffs2_getinfo(cyg_mtab_entry * mte, cyg_dir dir, const char *name,
1177 int key, void *buf, int len)
1182 D2(printf("jffs2_getinfo\n"));
1184 init_dirsearch(&ds, (struct _inode *) dir,
1185 (const unsigned char *) name);
1187 err = jffs2_find(&ds);
1195 err = jffs2_pathconf(ds.node, (struct cyg_pathconf_info *) buf);
1202 jffs2_iput(ds.node);
1206 // -------------------------------------------------------------------------
1208 // Setinfo. Nothing to support here at present.
1210 static int jffs2_setinfo(cyg_mtab_entry * mte, cyg_dir dir, const char *name,
1211 int key, void *buf, int len)
1213 // No setinfo keys supported at present
1215 D2(printf("jffs2_setinfo\n"));
1220 //==========================================================================
1223 // -------------------------------------------------------------------------
1225 // Read data from the file.
1227 static int jffs2_fo_read(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio)
1229 struct _inode *inode = (struct _inode *) fp->f_data;
1230 struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
1231 struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
1233 ssize_t resid = uio->uio_resid;
1234 off_t pos = fp->f_offset;
1238 // Loop over the io vectors until there are none left
1239 for (i = 0; i < uio->uio_iovcnt && pos < inode->i_size; i++) {
1241 cyg_iovec *iov = &uio->uio_iov[i];
1242 off_t len = min(iov->iov_len, inode->i_size - pos);
1244 D2(printf("jffs2_fo_read inode size %d\n", inode->i_size));
1247 jffs2_read_inode_range(c, f,
1248 (unsigned char *) iov->iov_base, pos,
1252 ("jffs2_fo_read(): read_inode_range failed %d\n",
1254 uio->uio_resid = resid;
1262 // We successfully read some data, update the node's access time
1263 // and update the file offset and transfer residue.
1265 inode->i_atime = cyg_timestamp();
1267 uio->uio_resid = resid;
1276 #ifdef CYGOPT_FS_JFFS2_WRITE
1277 // -------------------------------------------------------------------------
1279 // Write data to file.
1280 static int jffs2_extend_file (struct _inode *inode, struct jffs2_raw_inode *ri,
1281 unsigned long offset)
1283 struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
1284 struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
1285 struct jffs2_full_dnode *fn;
1286 uint32_t phys_ofs, alloc_len;
1289 /* Make new hole frag from old EOF to new page */
1290 D1(printk(KERN_DEBUG "Writing new hole frag 0x%x-0x%x between current EOF and new page\n",
1291 (unsigned int)inode->i_size, offset));
1293 ret = jffs2_reserve_space(c, sizeof(*ri), &phys_ofs, &alloc_len, ALLOC_NORMAL);
1299 ri->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
1300 ri->nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
1301 ri->totlen = cpu_to_je32(sizeof(*ri));
1302 ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
1304 ri->version = cpu_to_je32(++f->highest_version);
1305 ri->isize = cpu_to_je32(max((uint32_t)inode->i_size, offset));
1307 ri->offset = cpu_to_je32(inode->i_size);
1308 ri->dsize = cpu_to_je32(offset - inode->i_size);
1309 ri->csize = cpu_to_je32(0);
1310 ri->compr = JFFS2_COMPR_ZERO;
1311 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
1312 ri->data_crc = cpu_to_je32(0);
1314 fn = jffs2_write_dnode(c, f, ri, NULL, 0, phys_ofs, ALLOC_NORMAL);
1315 jffs2_complete_reservation(c);
1321 ret = jffs2_add_full_dnode_to_inode(c, f, fn);
1323 jffs2_mark_node_obsolete(c, f->metadata->raw);
1324 jffs2_free_full_dnode(f->metadata);
1328 D1(printk(KERN_DEBUG "Eep. add_full_dnode_to_inode() failed in prepare_write, returned %d\n", ret));
1329 jffs2_mark_node_obsolete(c, fn->raw);
1330 jffs2_free_full_dnode(fn);
1334 inode->i_size = offset;
1341 static int jffs2_truncate_file (struct _inode *inode)
1343 struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
1344 struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
1345 struct jffs2_full_dnode *new_metadata, * old_metadata;
1346 struct jffs2_raw_inode *ri;
1347 uint32_t phys_ofs, alloclen;
1350 ri = jffs2_alloc_raw_inode();
1354 err = jffs2_reserve_space(c, sizeof(*ri), &phys_ofs, &alloclen, ALLOC_NORMAL);
1357 jffs2_free_raw_inode(ri);
1361 ri->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
1362 ri->nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
1363 ri->totlen = cpu_to_je32(sizeof(*ri));
1364 ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
1366 ri->ino = cpu_to_je32(inode->i_ino);
1367 ri->version = cpu_to_je32(++f->highest_version);
1369 ri->uid = cpu_to_je16(inode->i_uid);
1370 ri->gid = cpu_to_je16(inode->i_gid);
1371 ri->mode = cpu_to_jemode(inode->i_mode);
1372 ri->isize = cpu_to_je32(0);
1373 ri->atime = cpu_to_je32(inode->i_atime);
1374 ri->mtime = cpu_to_je32(cyg_timestamp());
1375 ri->offset = cpu_to_je32(0);
1376 ri->csize = ri->dsize = cpu_to_je32(0);
1377 ri->compr = JFFS2_COMPR_NONE;
1378 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
1379 ri->data_crc = cpu_to_je32(0);
1380 new_metadata = jffs2_write_dnode(c, f, ri, NULL, 0,
1381 phys_ofs, ALLOC_NORMAL);
1382 if (IS_ERR(new_metadata)) {
1383 jffs2_complete_reservation(c);
1384 jffs2_free_raw_inode(ri);
1386 return PTR_ERR(new_metadata);
1389 /* It worked. Update the inode */
1390 inode->i_mtime = cyg_timestamp();
1392 old_metadata = f->metadata;
1393 jffs2_truncate_fragtree (c, &f->fragtree, 0);
1394 f->metadata = new_metadata;
1396 jffs2_mark_node_obsolete(c, old_metadata->raw);
1397 jffs2_free_full_dnode(old_metadata);
1399 jffs2_free_raw_inode(ri);
1402 jffs2_complete_reservation(c);
1407 static int jffs2_fo_write(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio)
1409 struct _inode *inode = (struct _inode *) fp->f_data;
1410 off_t pos = fp->f_offset;
1411 ssize_t resid = uio->uio_resid;
1412 struct jffs2_raw_inode ri;
1413 struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
1414 struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
1417 // If the APPEND mode bit was supplied, force all writes to
1418 // the end of the file.
1419 if (fp->f_flag & CYG_FAPPEND)
1420 pos = fp->f_offset = inode->i_size;
1425 memset(&ri, 0, sizeof(ri));
1427 ri.ino = cpu_to_je32(f->inocache->ino);
1428 ri.mode = cpu_to_jemode(inode->i_mode);
1429 ri.uid = cpu_to_je16(inode->i_uid);
1430 ri.gid = cpu_to_je16(inode->i_gid);
1431 ri.atime = ri.ctime = ri.mtime = cpu_to_je32(cyg_timestamp());
1433 if (pos > inode->i_size) {
1435 ri.version = cpu_to_je32(++f->highest_version);
1436 err = jffs2_extend_file(inode, &ri, pos);
1440 ri.isize = cpu_to_je32(inode->i_size);
1442 // Now loop over the iovecs until they are all done, or
1444 for (i = 0; i < uio->uio_iovcnt; i++) {
1445 cyg_iovec *iov = &uio->uio_iov[i];
1446 unsigned char *buf = iov->iov_base;
1447 off_t len = iov->iov_len;
1449 uint32_t writtenlen;
1452 D2(printf("jffs2_fo_write page_start_pos %d\n", pos));
1453 D2(printf("jffs2_fo_write transfer size %d\n", len));
1455 err = jffs2_write_inode_range(c, f, &ri, buf,
1456 pos, len, &writtenlen);
1460 if (writtenlen != len)
1467 // We wrote some data successfully, update the modified and access
1468 // times of the inode, increase its size appropriately, and update
1469 // the file offset and transfer residue.
1470 inode->i_mtime = inode->i_ctime = je32_to_cpu(ri.mtime);
1471 if (pos > inode->i_size)
1472 inode->i_size = pos;
1474 uio->uio_resid = resid;
1479 #endif /* CYGOPT_FS_JFFS2_WRITE */
1481 // -------------------------------------------------------------------------
1483 // Seek to a new file position.
1485 static int jffs2_fo_lseek(struct CYG_FILE_TAG *fp, off_t * apos, int whence)
1487 struct _inode *node = (struct _inode *) fp->f_data;
1490 D2(printf("jffs2_fo_lseek\n"));
1494 // Pos is already where we want to be.
1498 // Add pos to current offset.
1499 pos += fp->f_offset;
1503 // Add pos to file size.
1504 pos += node->i_size;
1514 // All OK, set fp offset and return new position.
1515 *apos = fp->f_offset = pos;
1520 // -------------------------------------------------------------------------
1522 // Handle ioctls. Currently none are defined.
1524 static int jffs2_fo_ioctl(struct CYG_FILE_TAG *fp, CYG_ADDRWORD com,
1527 // No Ioctls currenly defined.
1529 D2(printf("jffs2_fo_ioctl\n"));
1534 // -------------------------------------------------------------------------
1535 // jffs2_fo_fsync().
1536 // Force the file out to data storage.
1538 static int jffs2_fo_fsync(struct CYG_FILE_TAG *fp, int mode)
1540 // Data is always permanently where it belongs, nothing to do
1543 D2(printf("jffs2_fo_fsync\n"));
1548 // -------------------------------------------------------------------------
1550 // Close a file. We just decrement the refcnt and let it go away if
1551 // that is all that is keeping it here.
1553 static int jffs2_fo_close(struct CYG_FILE_TAG *fp)
1555 struct _inode *node = (struct _inode *) fp->f_data;
1557 D2(printf("jffs2_fo_close\n"));
1561 fp->f_data = 0; // zero data pointer
1566 // -------------------------------------------------------------------------
1570 static int jffs2_fo_fstat(struct CYG_FILE_TAG *fp, struct stat *buf)
1572 struct _inode *node = (struct _inode *) fp->f_data;
1574 D2(printf("jffs2_fo_fstat\n"));
1576 // Fill in the status
1577 buf->st_mode = node->i_mode;
1578 buf->st_ino = node->i_ino;
1580 buf->st_nlink = node->i_nlink;
1581 buf->st_uid = node->i_uid;
1582 buf->st_gid = node->i_gid;
1583 buf->st_size = node->i_size;
1584 buf->st_atime = node->i_atime;
1585 buf->st_mtime = node->i_mtime;
1586 buf->st_ctime = node->i_ctime;
1591 // -------------------------------------------------------------------------
1592 // jffs2_fo_getinfo()
1593 // Get info. Currently only supports fpathconf().
1595 static int jffs2_fo_getinfo(struct CYG_FILE_TAG *fp, int key, void *buf,
1598 struct _inode *node = (struct _inode *) fp->f_data;
1601 D2(printf("jffs2_fo_getinfo\n"));
1605 err = jffs2_pathconf(node, (struct cyg_pathconf_info *) buf);
1616 // -------------------------------------------------------------------------
1617 // jffs2_fo_setinfo()
1618 // Set info. Nothing supported here.
1620 static int jffs2_fo_setinfo(struct CYG_FILE_TAG *fp, int key, void *buf,
1623 // No setinfo key supported at present
1625 D2(printf("jffs2_fo_setinfo\n"));
1630 //==========================================================================
1631 // Directory operations
1633 // -------------------------------------------------------------------------
1634 // jffs2_fo_dirread()
1635 // Read a single directory entry from a file.
1637 static __inline void filldir(char *nbuf, int nlen, const unsigned char *name, int namlen)
1639 int len = nlen < namlen ? nlen : namlen;
1640 memcpy(nbuf, name, len);
1644 static int jffs2_fo_dirread(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio)
1646 struct _inode *d_inode = (struct _inode *) fp->f_data;
1647 struct dirent *ent = (struct dirent *) uio->uio_iov[0].iov_base;
1648 char *nbuf = ent->d_name;
1649 #ifdef CYGPKG_FS_JFFS2_RET_DIRENT_DTYPE
1650 struct _inode *c_ino;
1652 int nlen = sizeof (ent->d_name) - 1;
1653 off_t len = uio->uio_iov[0].iov_len;
1654 struct jffs2_inode_info *f;
1655 struct jffs2_sb_info *c;
1656 struct _inode *inode = d_inode;
1657 struct jffs2_full_dirent *fd;
1658 unsigned long offset, curofs;
1661 if (len < sizeof (struct dirent))
1665 (KERN_DEBUG "jffs2_readdir() for dir_i #%lu\n", d_inode->i_ino));
1667 f = JFFS2_INODE_INFO(inode);
1668 c = JFFS2_SB_INFO(inode->i_sb);
1670 offset = fp->f_offset;
1674 (KERN_DEBUG "Dirent 0: \".\", ino #%lu\n", inode->i_ino));
1675 filldir(nbuf, nlen, (const unsigned char *) ".", 1);
1676 #ifdef CYGPKG_FS_JFFS2_RET_DIRENT_DTYPE
1677 // Flags here are the same as jffs2_mkdir. Make sure
1678 // d_type is the same as st_mode of calling stat.
1680 jemode_to_cpu(cpu_to_jemode(S_IRUGO|S_IXUGO|S_IWUSR|S_IFDIR));
1685 filldir(nbuf, nlen, (const unsigned char *) "..", 2);
1686 #ifdef CYGPKG_FS_JFFS2_RET_DIRENT_DTYPE
1687 // Flags here are the same as jffs2_mkdir. Make sure
1688 // d_type is the same as st_mode of calling stat.
1690 jemode_to_cpu(cpu_to_jemode(S_IRUGO|S_IXUGO|S_IWUSR|S_IFDIR));
1697 for (fd = f->dents; fd; fd = fd->next) {
1700 /* First loop: curofs = 2; offset = 2 */
1701 if (curofs < offset) {
1704 "Skipping dirent: \"%s\", ino #%u, type %d, because curofs %ld < offset %ld\n",
1705 fd->name, fd->ino, fd->type, curofs, offset));
1710 (KERN_DEBUG "Skipping deletion dirent \"%s\"\n",
1716 (KERN_DEBUG "Dirent %ld: \"%s\", ino #%u, type %d\n", offset,
1717 fd->name, fd->ino, fd->type));
1718 filldir(nbuf, nlen, fd->name, strlen((char *)fd->name));
1719 #ifdef CYGPKG_FS_JFFS2_RET_DIRENT_DTYPE
1720 c_ino = jffs2_iget(inode->i_sb, fd->ino);
1722 D1(printk(KERN_WARNING "get entry inode failed\n"));
1723 // fileio already set it to zero, so not needed here
1727 ent->d_type = c_ino->i_mode;
1733 /* Reached the end of the directory */
1738 fp->f_offset = ++offset;
1740 uio->uio_resid -= sizeof (struct dirent);
1745 // -------------------------------------------------------------------------
1746 // jffs2_fo_dirlseek()
1747 // Seek directory to start.
1749 static int jffs2_fo_dirlseek(struct CYG_FILE_TAG *fp, off_t * pos, int whence)
1751 // Only allow SEEK_SET to zero
1753 D2(printf("jffs2_fo_dirlseek\n"));
1755 if (whence != SEEK_SET || *pos != 0)
1758 *pos = fp->f_offset = 0;
1763 //==========================================================================
1769 //==========================================================================
1771 unsigned char *jffs2_gc_fetch_page(struct jffs2_sb_info *c,
1772 struct jffs2_inode_info *f,
1773 unsigned long offset,
1774 unsigned long *priv)
1776 /* FIXME: This works only with one file system mounted at a time */
1779 ret = jffs2_read_inode_range(c, f, gc_buffer,
1780 offset & ~(PAGE_CACHE_SIZE-1), PAGE_CACHE_SIZE);
1782 return ERR_PTR(ret);
1787 void jffs2_gc_release_page(struct jffs2_sb_info *c,
1789 unsigned long *priv)
1794 static struct _inode *new_inode(struct super_block *sb)
1797 // Only called in write.c jffs2_new_inode
1798 // Always adds itself to inode cache
1800 struct _inode *inode;
1801 struct _inode *cached_inode;
1803 inode = malloc(sizeof (struct _inode));
1808 ("malloc new_inode %x ####################################\n",
1811 memset(inode, 0, sizeof (struct _inode));
1815 inode->i_nlink = 1; // Let JFFS2 manage the link count
1818 inode->i_cache_next = NULL; // Newest inode, about to be cached
1820 // Add to the icache
1821 for (cached_inode = sb->s_root; cached_inode != NULL;
1822 cached_inode = cached_inode->i_cache_next) {
1823 if (cached_inode->i_cache_next == NULL) {
1824 cached_inode->i_cache_next = inode; // Current last in cache points to newcomer
1825 inode->i_cache_prev = cached_inode; // Newcomer points back to last
1832 static struct _inode *ilookup(struct super_block *sb, cyg_uint32 ino)
1834 struct _inode *inode = NULL;
1836 D2(printf("ilookup\n"));
1837 // Check for this inode in the cache
1838 for (inode = sb->s_root; inode != NULL; inode = inode->i_cache_next) {
1839 if (inode->i_ino == ino) {
1847 struct _inode *jffs2_iget(struct super_block *sb, cyg_uint32 ino)
1849 // Called in super.c jffs2_read_super, dir.c jffs2_lookup,
1850 // and gc.c jffs2_garbage_collect_pass
1852 // Must first check for cached inode
1853 // If this fails let new_inode create one
1855 struct _inode *inode;
1858 D2(printf("jffs2_iget\n"));
1860 inode = ilookup(sb, ino);
1864 // Not cached, so malloc it
1865 inode = new_inode(sb);
1867 return ERR_PTR(-ENOMEM);
1871 err = jffs2_read_inode(inode);
1873 printf("jffs2_read_inode() failed\n");
1874 inode->i_nlink = 0; // free _this_ bad inode right now
1877 return ERR_PTR(err);
1882 // -------------------------------------------------------------------------
1883 // Decrement the reference count on an inode. If this makes the ref count
1884 // zero, then this inode can be freed.
1886 void jffs2_iput(struct _inode *i)
1888 // Called in jffs2_find
1889 // (and jffs2_open and jffs2_ops_mkdir?)
1890 // super.c jffs2_read_super,
1891 // and gc.c jffs2_garbage_collect_pass
1894 printf("jffs2_iput() called with NULL inode\n");
1895 // and let it fault...
1907 struct _inode *parent;
1909 // Remove from the icache linked list and free immediately
1910 if (i->i_cache_prev)
1911 i->i_cache_prev->i_cache_next = i->i_cache_next;
1912 if (i->i_cache_next)
1913 i->i_cache_next->i_cache_prev = i->i_cache_prev;
1915 parent = i->i_parent;
1916 jffs2_clear_inode(i);
1917 memset(i, 0x5a, sizeof(*i));
1920 if (parent && parent != i) {
1926 // Evict some _other_ inode with i_count zero, leaving
1927 // this latest one in the cache for a while
1928 icache_evict(i->i_sb->s_root, i);
1933 // -------------------------------------------------------------------------
1937 static inline void jffs2_init_inode_info(struct jffs2_inode_info *f)
1939 memset(f, 0, sizeof(*f));
1940 init_MUTEX_LOCKED(&f->sem);
1943 static void jffs2_clear_inode (struct _inode *inode)
1945 /* We can forget about this inode for now - drop all
1946 * the nodelists associated with it, etc.
1948 struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
1949 struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
1951 D1(printk(KERN_DEBUG "jffs2_clear_inode(): ino #%lu mode %o\n", inode->i_ino, inode->i_mode));
1953 jffs2_do_clear_inode(c, f);
1957 /* jffs2_new_inode: allocate a new inode and inocache, add it to the hash,
1958 fill in the raw_inode while you're at it. */
1959 struct _inode *jffs2_new_inode (struct _inode *dir_i, int mode, struct jffs2_raw_inode *ri)
1961 struct _inode *inode;
1962 struct super_block *sb = dir_i->i_sb;
1963 struct jffs2_sb_info *c;
1964 struct jffs2_inode_info *f;
1967 D1(printk(KERN_DEBUG "jffs2_new_inode(): dir_i %ld, mode 0x%x\n", dir_i->i_ino, mode));
1969 c = JFFS2_SB_INFO(sb);
1971 inode = new_inode(sb);
1974 return ERR_PTR(-ENOMEM);
1976 f = JFFS2_INODE_INFO(inode);
1977 jffs2_init_inode_info(f);
1979 memset(ri, 0, sizeof(*ri));
1980 /* Set OS-specific defaults for new inodes */
1981 ri->uid = ri->gid = cpu_to_je16(0);
1982 ri->mode = cpu_to_jemode(mode);
1983 ret = jffs2_do_new_inode (c, f, mode, ri);
1985 // forceful evict: f->sem is locked already, and the
1987 if (inode->i_cache_prev)
1988 inode->i_cache_prev->i_cache_next = inode->i_cache_next;
1989 if (inode->i_cache_next)
1990 inode->i_cache_next->i_cache_prev = inode->i_cache_prev;
1992 jffs2_clear_inode(inode);
1993 memset(inode, 0x6a, sizeof(*inode));
1995 return ERR_PTR(ret);
1998 inode->i_ino = je32_to_cpu(ri->ino);
1999 inode->i_mode = jemode_to_cpu(ri->mode);
2000 inode->i_gid = je16_to_cpu(ri->gid);
2001 inode->i_uid = je16_to_cpu(ri->uid);
2002 inode->i_atime = inode->i_ctime = inode->i_mtime = cyg_timestamp();
2003 ri->atime = ri->mtime = ri->ctime = cpu_to_je32(inode->i_mtime);
2011 static int jffs2_read_inode (struct _inode *inode)
2013 struct jffs2_inode_info *f;
2014 struct jffs2_sb_info *c;
2015 struct jffs2_raw_inode latest_node;
2018 D1(printk(KERN_DEBUG "jffs2_read_inode(): inode->i_ino == %lu\n", inode->i_ino));
2020 f = JFFS2_INODE_INFO(inode);
2021 c = JFFS2_SB_INFO(inode->i_sb);
2023 jffs2_init_inode_info(f);
2025 ret = jffs2_do_read_inode(c, f, inode->i_ino, &latest_node);
2031 inode->i_mode = jemode_to_cpu(latest_node.mode);
2032 inode->i_uid = je16_to_cpu(latest_node.uid);
2033 inode->i_gid = je16_to_cpu(latest_node.gid);
2034 inode->i_size = je32_to_cpu(latest_node.isize);
2035 inode->i_atime = je32_to_cpu(latest_node.atime);
2036 inode->i_mtime = je32_to_cpu(latest_node.mtime);
2037 inode->i_ctime = je32_to_cpu(latest_node.ctime);
2039 inode->i_nlink = f->inocache->nlink;
2042 D1(printk(KERN_DEBUG "jffs2_read_inode() returning\n"));
2047 void jffs2_gc_release_inode(struct jffs2_sb_info *c,
2048 struct jffs2_inode_info *f)
2050 jffs2_iput(OFNI_EDONI_2SFFJ(f));
2053 struct jffs2_inode_info *jffs2_gc_fetch_inode(struct jffs2_sb_info *c,
2054 int inum, int nlink)
2056 struct _inode *inode;
2057 struct jffs2_inode_cache *ic;
2059 /* The inode has zero nlink but its nodes weren't yet marked
2060 obsolete. This has to be because we're still waiting for
2061 the final (close() and) jffs2_iput() to happen.
2063 There's a possibility that the final jffs2_iput() could have
2064 happened while we were contemplating. In order to ensure
2065 that we don't cause a new read_inode() (which would fail)
2066 for the inode in question, we use ilookup() in this case
2067 instead of jffs2_iget().
2069 The nlink can't _become_ zero at this point because we're
2070 holding the alloc_sem, and jffs2_do_unlink() would also
2071 need that while decrementing nlink on any inode.
2073 inode = ilookup(OFNI_BS_2SFFJ(c), inum);
2075 D1(printk(KERN_DEBUG "ilookup() failed for ino #%u; inode is probably deleted.\n",
2078 spin_lock(&c->inocache_lock);
2079 ic = jffs2_get_ino_cache(c, inum);
2081 D1(printk(KERN_DEBUG "Inode cache for ino #%u is gone.\n", inum));
2082 spin_unlock(&c->inocache_lock);
2085 if (ic->state != INO_STATE_CHECKEDABSENT) {
2086 /* Wait for progress. Don't just loop */
2087 D1(printk(KERN_DEBUG "Waiting for ino #%u in state %d\n",
2088 ic->ino, ic->state));
2089 sleep_on_spinunlock(&c->inocache_wq, &c->inocache_lock);
2091 spin_unlock(&c->inocache_lock);
2097 /* Inode has links to it still; they're not going away because
2098 jffs2_do_unlink() would need the alloc_sem and we have it.
2099 Just jffs2_iget() it, and if read_inode() is necessary that's OK.
2101 inode = jffs2_iget(OFNI_BS_2SFFJ(c), inum);
2103 return (void *)inode;
2106 return JFFS2_INODE_INFO(inode);
2111 uint32_t jffs2_from_os_mode(uint32_t osmode)
2113 uint32_t jmode = ((osmode & S_IRUSR)?00400:0) |
2114 ((osmode & S_IWUSR)?00200:0) |
2115 ((osmode & S_IXUSR)?00100:0) |
2116 ((osmode & S_IRGRP)?00040:0) |
2117 ((osmode & S_IWGRP)?00020:0) |
2118 ((osmode & S_IXGRP)?00010:0) |
2119 ((osmode & S_IROTH)?00004:0) |
2120 ((osmode & S_IWOTH)?00002:0) |
2121 ((osmode & S_IXOTH)?00001:0);
2123 switch (osmode & S_IFMT) {
2125 return jmode | 0140000;
2127 return jmode | 0120000;
2129 return jmode | 0100000;
2131 return jmode | 0060000;
2133 return jmode | 0040000;
2135 return jmode | 0020000;
2137 return jmode | 0010000;
2139 return jmode | 0004000;
2141 return jmode | 0002000;
2144 return jmode | 0001000;
2147 printf("os_to_jffs2_mode() cannot convert 0x%x\n", osmode);
2152 uint32_t jffs2_to_os_mode (uint32_t jmode)
2154 uint32_t osmode = ((jmode & 00400)?S_IRUSR:0) |
2155 ((jmode & 00200)?S_IWUSR:0) |
2156 ((jmode & 00100)?S_IXUSR:0) |
2157 ((jmode & 00040)?S_IRGRP:0) |
2158 ((jmode & 00020)?S_IWGRP:0) |
2159 ((jmode & 00010)?S_IXGRP:0) |
2160 ((jmode & 00004)?S_IROTH:0) |
2161 ((jmode & 00002)?S_IWOTH:0) |
2162 ((jmode & 00001)?S_IXOTH:0);
2164 switch(jmode & 00170000) {
2166 return osmode | S_IFSOCK;
2168 return osmode | S_IFLNK;
2170 return osmode | S_IFREG;
2172 return osmode | S_IFBLK;
2174 return osmode | S_IFDIR;
2176 return osmode | S_IFCHR;
2178 return osmode | S_IFIFO;
2180 return osmode | S_ISUID;
2182 return osmode | S_ISGID;
2185 return osmode | S_ISVTX;
2188 printf("jffs2_to_os_mode() cannot convert 0x%x\n", osmode);