parlib: Expand our printf hacks
[akaros.git] / kern / src / kfs.c
index 3d9c457..eef0470 100644 (file)
@@ -5,15 +5,6 @@
  * Implementation of the KFS file system.  It is a RAM based, read-only FS
  * consisting of files that are added to the kernel binary image.  Might turn
  * this into a read/write FS with directories someday. */
-
-#ifdef __SHARC__
-#pragma nosharc
-#endif
-
-#ifdef __DEPUTY__
-#pragma nodeputy
-#endif
-
 #include <vfs.h>
 #include <kfs.h>
 #include <slab.h>
@@ -56,11 +47,13 @@ struct kmem_cache *kfs_i_kcache;
 
 static void kfs_init(void)
 {
-       kfs_i_kcache = kmem_cache_create("kfs_ino_info", sizeof(struct kfs_i_info),
-                                        __alignof__(struct kfs_i_info), 0, 0, 0);
+       kfs_i_kcache = kmem_cache_create("kfs_ino_info",
+                                        sizeof(struct kfs_i_info),
+                                        __alignof__(struct kfs_i_info), 0,
+                                        NULL, 0, 0, NULL);
 }
 
-/* Creates the SB (normally would read in from disc and create).  Passes it's
+/* Creates the SB (normally would read in from disc and create).  Passes its
  * ref out to whoever consumes this.  Returns 0 on failure.
  * TODO: consider pulling out more of the FS-independent stuff, if possible.
  * There are only two things, but the pain in the ass is that you'd need to read
@@ -125,6 +118,12 @@ int kfs_readpage(struct page_map *pm, struct page *page)
        struct kfs_i_info *k_i_info = (struct kfs_i_info*)
                                      pm->pm_host->i_fs_info;
        uintptr_t begin = (size_t)k_i_info->filestart + pg_idx_byte;
+
+       /* Pretend that we blocked while filing this page.  This catches a lot of
+        * bugs.  It does slightly slow down the kernel, but it's only when filling
+        * the page cache, and considering we are using a RAMFS, you shouldn't
+        * measure things that actually rely on KFS's performance. */
+       kthread_usleep(1);
        /* If we're beyond the initial start point, we just need a zero page.  This
         * is for a hole or for extending a file (even though it won't be saved).
         * Otherwise, we want the data from KFS, being careful to not copy from
@@ -401,8 +400,7 @@ int kfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
        kfs_init_inode(dir, dentry);
        SET_FTYPE(inode->i_mode, __S_IFLNK);
        inode->i_fop = &kfs_f_op_sym;
-       strncpy(string, symname, len);
-       string[len] = '\0';             /* symname should be \0d anyway, but just in case */
+       strlcpy(string, symname, len + 1);
        k_i_info->filestart = string;   /* reusing this void* to hold the char* */
        return 0;
 }
@@ -537,7 +535,7 @@ int kfs_d_release(struct dentry *dentry)
        return -1;
 }
 
-/* Called when the dentry loses it's inode (becomes "negative") */
+/* Called when the dentry loses its inode (becomes "negative") */
 void kfs_d_iput(struct dentry *dentry, struct inode *inode)
 { // default, call i_put to release the inode object
 }
@@ -593,9 +591,14 @@ int kfs_readdir(struct file *dir, struct dirent *dirent)
                        dirent->d_ino = subent->d_inode->i_ino;
                        dirent->d_off = count;
                        dirent->d_reclen = subent->d_name.len;
-                       /* d_name.name is null terminated, the byte after d_name.len */
-                       assert(subent->d_name.len <= MAX_FILENAME_SZ);
-                       strncpy(dirent->d_name, subent->d_name.name, subent->d_name.len +1);
+                       /* d_name.name is null terminated, the byte after d_name.len.
+                        * Regardless, exercise caution as we copy into d_name, should
+                        * the size of the quickstring buffer and the size of d_name
+                        * fall out of sync with one another. */
+                       assert(subent->d_name.len < sizeof(dirent->d_name));
+                       strncpy(dirent->d_name, subent->d_name.name,
+                               sizeof(dirent->d_name) - 1);
+                       dirent->d_name[sizeof(dirent->d_name) - 1] = '\0';
                        found = TRUE;
                }
        }
@@ -605,13 +608,13 @@ int kfs_readdir(struct file *dir, struct dirent *dirent)
                dirent->d_ino = dir_d->d_inode->i_ino;
                dirent->d_off = 1;
                dirent->d_reclen = 1;
-               strncpy(dirent->d_name, ".", 2);        /* the extra is for the null term */
+               strlcpy(dirent->d_name, ".", sizeof(dirent->d_name));
                found = TRUE;
        } else if (desired_file == 1) {
                dirent->d_ino = dir_d->d_parent->d_inode->i_ino;
                dirent->d_off = 2;
                dirent->d_reclen = 2;
-               strncpy(dirent->d_name, "..", 3);       /* the extra is for the null term */
+               strlcpy(dirent->d_name, "..", sizeof(dirent->d_name));
                found = TRUE;
        }
        /* need to check the sub-dirs as well as the sub-"files".  The main
@@ -808,7 +811,7 @@ struct file_operations kfs_f_op_sym = {
 static int __add_kfs_entry(struct dentry *parent, char *path,
                            struct cpio_bin_hdr *c_bhdr)
 {
-       char *first_slash = strchr(path, '/');  
+       char *first_slash = strchr(path, '/');
        char dir[MAX_FILENAME_SZ + 1];  /* room for the \0 */
        size_t dirname_sz;                              /* not counting the \0 */
        struct dentry *dentry = 0;
@@ -822,7 +825,7 @@ static int __add_kfs_entry(struct dentry *parent, char *path,
                 * anything like that. */
                dirname_sz = first_slash - path;
                assert(dirname_sz <= MAX_FILENAME_SZ);
-               strncpy(dir, path, dirname_sz);
+               memmove(dir, path, dirname_sz);
                dir[dirname_sz] = '\0';
                printd("Finding DIR %s in dentry %s (start: %p, size %d)\n", dir,
                       parent->d_name.name, c_bhdr->c_filestart, c_bhdr->c_filesize);