1 /* Copyright (c) 2009, 2010 The Regents of the University of California
2 * Barret Rhoden <brho@cs.berkeley.edu>
3 * See LICENSE for details.
5 * Implementation of the KFS file system. It is a RAM based, read-only FS
6 * consisting of files that are added to the kernel binary image. Might turn
7 * this into a read/write FS with directories someday. */
29 #define KFS_MAX_FILE_SIZE 1024*1024*128
30 #define KFS_MAGIC 0xdead0001
32 /* VFS required Functions */
33 /* These structs are declared again and initialized farther down */
34 struct page_map_operations kfs_pm_op;
35 struct super_operations kfs_s_op;
36 struct inode_operations kfs_i_op;
37 struct dentry_operations kfs_d_op;
38 struct file_operations kfs_f_op_file;
39 struct file_operations kfs_f_op_dir;
40 struct file_operations kfs_f_op_sym;
42 /* TODO: something more better. Prob something like the vmem cache, for this,
43 * pids, etc. Good enough for now. This also means we can only have one
44 * KFS instance, and we also aren't synchronizing access. */
45 static unsigned long kfs_get_free_ino(void)
47 static unsigned long last_ino = 1; /* 1 is reserved for the root */
50 panic("Out of inos in KFS!");
54 /* Slabs for KFS specific info chunks */
55 struct kmem_cache *kfs_i_kcache;
57 static void kfs_init(void)
59 kfs_i_kcache = kmem_cache_create("kfs_ino_info", sizeof(struct kfs_i_info),
60 __alignof__(struct kfs_i_info), 0, 0, 0);
63 /* Creates the SB (normally would read in from disc and create). Passes it's
64 * ref out to whoever consumes this. Returns 0 on failure.
65 * TODO: consider pulling out more of the FS-independent stuff, if possible.
66 * There are only two things, but the pain in the ass is that you'd need to read
67 * the disc to get that first inode, and it's a FS-specific thing. */
68 struct super_block *kfs_get_sb(struct fs_type *fs, int flags,
69 char *dev_name, struct vfsmount *vmnt)
71 /* Ought to check that dev_name has our FS on it. in this case, it's
75 static bool ran_once = FALSE;
81 /* Build and init the SB. No need to read off disc. */
82 struct super_block *sb = get_sb();
85 sb->s_maxbytes = KFS_MAX_FILE_SIZE;
86 sb->s_type = &kfs_fs_type;
89 sb->s_magic = KFS_MAGIC;
91 sb->s_syncing = FALSE;
93 strlcpy(sb->s_name, "KFS", 32);
94 /* store the location of the CPIO archive. make this more generic later. */
95 extern uint8_t _binary_obj_kern_initramfs_cpio_size[];
96 extern uint8_t _binary_obj_kern_initramfs_cpio_start[];
97 sb->s_fs_info = (void*)_binary_obj_kern_initramfs_cpio_start;
99 /* Final stages of initializing the sb, mostly FS-independent */
100 /* 1 is the KFS root ino (inode number) */
101 init_sb(sb, vmnt, &kfs_d_op, 1, 0);
102 /* Parses the CPIO entries and builds the in-memory KFS tree. */
103 parse_cpio_entries(sb, sb->s_fs_info);
104 printk("KFS superblock loaded\n");
108 void kfs_kill_sb(struct super_block *sb)
110 panic("Killing KFS is not supported!");
113 /* Every FS must have a static FS Type, with which the VFS code can bootstrap */
114 struct fs_type kfs_fs_type = {"KFS", 0, kfs_get_sb, kfs_kill_sb, {0, 0},
115 TAILQ_HEAD_INITIALIZER(kfs_fs_type.fs_supers)};
117 /* Page Map Operations */
119 /* Fills page with its contents from its backing store file. Note that we do
120 * the zero padding here, instead of higher in the VFS. Might change in the
122 int kfs_readpage(struct page_map *pm, struct page *page)
124 size_t pg_idx_byte = page->pg_index * PGSIZE;
125 struct kfs_i_info *k_i_info = (struct kfs_i_info*)
126 pm->pm_host->i_fs_info;
127 uintptr_t begin = (size_t)k_i_info->filestart + pg_idx_byte;
128 /* If we're beyond the initial start point, we just need a zero page. This
129 * is for a hole or for extending a file (even though it won't be saved).
130 * Otherwise, we want the data from KFS, being careful to not copy from
131 * beyond the original EOF (and zero padding anything extra). */
132 if (pg_idx_byte >= k_i_info->init_size) {
133 memset(page2kva(page), 0, PGSIZE);
135 size_t copy_amt = MIN(PGSIZE, k_i_info->init_size - pg_idx_byte);
136 memcpy(page2kva(page), (void*)begin, copy_amt);
137 memset(page2kva(page) + copy_amt, 0, PGSIZE - copy_amt);
139 struct buffer_head *bh = kmem_cache_alloc(bh_kcache, 0);
141 return -1; /* untested, un-thought-through */
142 atomic_or(&page->pg_flags, PG_BUFFER);
143 /* KFS does a 1:1 BH to page mapping */
144 bh->bh_page = page; /* weak ref */
145 bh->bh_buffer = page2kva(page);
146 bh->bh_flags = 0; /* whatever... */
147 bh->bh_next = 0; /* only one BH needed */
148 bh->bh_bdev = pm->pm_host->i_sb->s_bdev; /* uncounted */
149 bh->bh_sector = page->pg_index;
150 bh->bh_nr_sector = 1; /* sector size = PGSIZE */
151 page->pg_private = bh;
152 /* This is supposed to be done in the IO system when the operation is
153 * complete. Since we aren't doing a real IO request, and it is already
154 * done, we can do it here. */
155 atomic_or(&page->pg_flags, PG_UPTODATE);
159 int kfs_writepage(struct page_map *pm, struct page *page)
161 warn_once("KFS writepage does not save file contents!\n");
165 /* Super Operations */
167 /* Creates and initializes a new inode. FS specific, yet inode-generic fields
168 * are filled in. inode-specific fields are filled in in read_inode() based on
169 * what's on the disk for a given i_no. i_no and i_fop are set by the caller.
171 * Note that this means this inode can be for an inode that is already on disk,
172 * or it can be used when creating. The i_fop depends on the type of file
173 * (file, directory, symlink, etc). */
174 struct inode *kfs_alloc_inode(struct super_block *sb)
176 struct inode *inode = kmem_cache_alloc(inode_kcache, 0);
177 memset(inode, 0, sizeof(struct inode));
178 inode->i_op = &kfs_i_op;
179 inode->i_pm.pm_op = &kfs_pm_op;
180 inode->i_fs_info = kmem_cache_alloc(kfs_i_kcache, 0);
181 TAILQ_INIT(&((struct kfs_i_info*)inode->i_fs_info)->children);
182 ((struct kfs_i_info*)inode->i_fs_info)->filestart = 0;
183 ((struct kfs_i_info*)inode->i_fs_info)->init_size = 0;
187 /* FS-specific clean up when an inode is dealloced. this is just cleaning up
188 * the in-memory version, and only the FS-specific parts. whether or not the
189 * inode is still on disc is irrelevant. */
190 void kfs_dealloc_inode(struct inode *inode)
192 /* If we're a symlink, give up our storage for the symname */
193 if (S_ISLNK(inode->i_mode))
194 kfree(((struct kfs_i_info*)inode->i_fs_info)->filestart);
195 kmem_cache_free(kfs_i_kcache, inode->i_fs_info);
198 /* reads the inode data on disk specified by inode->i_ino into the inode.
199 * basically, it's a "make this inode the one for i_ino (i number)" */
200 void kfs_read_inode(struct inode *inode)
202 /* need to do something to link this inode/file to the actual "blocks" on
205 /* TODO: what does it mean to ask for an inode->i_ino that doesn't exist?
206 * possibly a bug, since these inos come from directories */
207 if (inode->i_ino == 1) {
208 inode->i_mode = S_IRWXU | S_IRWXG | S_IRWXO;
209 SET_FTYPE(inode->i_mode, __S_IFDIR);
210 inode->i_fop = &kfs_f_op_dir;
211 inode->i_nlink = 1; /* assuming only one hardlink */
214 inode->i_size = 0; /* make sense for KFS? */
215 inode->i_atime.tv_sec = 0;
216 inode->i_atime.tv_nsec = 0;
217 inode->i_mtime.tv_sec = 0;
218 inode->i_mtime.tv_nsec = 0;
219 inode->i_ctime.tv_sec = 0;
220 inode->i_ctime.tv_nsec = 0;
223 inode->i_socket = FALSE;
225 panic("Not implemented");
227 /* TODO: unused: inode->i_hash add to hash (saves on disc reading) */
230 /* called when an inode in memory is modified (journalling FS's care) */
231 void kfs_dirty_inode(struct inode *inode)
232 { // KFS doesn't care
235 /* write the inode to disk (specifically, to inode inode->i_ino), synchronously
236 * if we're asked to wait */
237 void kfs_write_inode(struct inode *inode, bool wait)
238 { // KFS doesn't care
241 /* called when an inode is decref'd, to do any FS specific work */
242 void kfs_put_inode(struct inode *inode)
243 { // KFS doesn't care
246 /* called when an inode is about to be destroyed. the generic version ought to
247 * remove every reference to the inode from the VFS, and if the inode isn't in
248 * any directory, calls delete_inode */
249 void kfs_drop_inode(struct inode *inode)
250 { // TODO: should call a generic one instead. or at least do something...
254 /* delete the inode from disk (all data) */
255 void kfs_delete_inode(struct inode *inode)
257 // would remove from "disk" here
258 /* TODO: give up our i_ino */
261 /* unmount and release the super block */
262 void kfs_put_super(struct super_block *sb)
264 panic("Shazbot! KFS can't be unmounted yet!");
267 /* updates the on-disk SB with the in-memory SB */
268 void kfs_write_super(struct super_block *sb)
269 { // KFS doesn't care
272 /* syncs FS metadata with the disc, synchronously if we're waiting. this info
273 * also includes anything pointed to by s_fs_info. */
274 int kfs_sync_fs(struct super_block *sb, bool wait)
279 /* remount the FS with the new flags */
280 int kfs_remount_fs(struct super_block *sb, int flags, char *data)
282 warn("KFS will not remount.");
283 return -1; // can't remount
286 /* interrupts a mount operation - used by NFS and friends */
287 void kfs_umount_begin(struct super_block *sb)
289 panic("Cannot abort a KFS mount, and why would you?");
292 /* inode_operations */
294 /* Little helper, used for initializing new inodes for file-like objects (files,
295 * symlinks, etc). We pass the dentry, since we need to up it. */
296 static void kfs_init_inode(struct inode *dir, struct dentry *dentry)
298 struct inode *inode = dentry->d_inode;
299 kref_get(&dentry->d_kref, 1); /* to pin the dentry in RAM, KFS-style... */
300 inode->i_ino = kfs_get_free_ino();
301 /* our parent dentry's inode tracks our dentry info. We do this
302 * since it's all in memory and we aren't using the dcache yet.
303 * We're reusing the subdirs link, which is used by the VFS when
304 * we're a directory. But since we're a file, it's okay to reuse
306 TAILQ_INSERT_TAIL(&((struct kfs_i_info*)dir->i_fs_info)->children,
307 dentry, d_subdirs_link);
310 /* Called when creating a new disk inode in dir associated with dentry. We need
311 * to fill out the i_ino, set the type, and do whatever else we need */
312 int kfs_create(struct inode *dir, struct dentry *dentry, int mode,
313 struct nameidata *nd)
315 struct inode *inode = dentry->d_inode;
316 kfs_init_inode(dir, dentry);
317 SET_FTYPE(inode->i_mode, __S_IFREG);
318 inode->i_fop = &kfs_f_op_file;
319 /* fs_info->filestart is set by the caller, or else when first written (for
320 * new files. it was set to 0 in alloc_inode(). */
324 /* Searches the directory for the filename in the dentry, filling in the dentry
325 * with the FS specific info of this file. If it succeeds, it will pass back
326 * the *dentry you should use. If this fails, it will return 0. It will NOT
327 * take your dentry ref (it used to). It probably will not be the same dentry
328 * you passed in. This is ugly.
330 * Callers, make sure you alloc and fill out the name parts of the dentry, and
331 * an initialized nameidata. TODO: not sure why we need an ND. Don't use it in
332 * a fs_lookup for now!
334 * Because of the way KFS currently works, if there is ever a dentry, it's
335 * already in memory, along with its inode (all path's pinned). So we just find
336 * it and return it, freeing the one that came in. */
337 struct dentry *kfs_lookup(struct inode *dir, struct dentry *dentry,
338 struct nameidata *nd)
340 struct kfs_i_info *k_i_info = (struct kfs_i_info*)dir->i_fs_info;
341 struct dentry *dir_dent = TAILQ_FIRST(&dir->i_dentry);
344 assert(dir_dent && dir_dent == TAILQ_LAST(&dir->i_dentry, dentry_tailq));
345 /* had this fail when kern/kfs has a symlink go -> ../../../go, though
346 * a symlink like lib2 -> lib work okay. */
347 assert(S_ISDIR(dir->i_mode));
348 assert(kref_refcnt(&dentry->d_kref) == 1);
349 TAILQ_FOREACH(d_i, &dir_dent->d_subdirs, d_subdirs_link) {
350 if (!strcmp(d_i->d_name.name, dentry->d_name.name)) {
351 /* since this dentry is already in memory (that's how KFS works), we
352 * just return the real one (with another refcnt) */
353 kref_get(&d_i->d_kref, 1);
357 TAILQ_FOREACH(d_i, &k_i_info->children, d_subdirs_link) {
358 if (!strcmp(d_i->d_name.name, dentry->d_name.name)) {
359 /* since this dentry is already in memory (that's how KFS works), we
360 * just return the real one (with another refcnt) */
361 kref_get(&d_i->d_kref, 1);
365 printd("Not Found %s!!\n", dentry->d_name.name);
369 /* Hard link to old_dentry in directory dir with a name specified by new_dentry.
370 * At the very least, set the new_dentry's FS-specific fields. */
371 int kfs_link(struct dentry *old_dentry, struct inode *dir,
372 struct dentry *new_dentry)
374 assert(new_dentry->d_op = &kfs_d_op);
375 kref_get(&new_dentry->d_kref, 1); /* pin the dentry, KFS-style */
376 /* KFS-style directory-tracking-of-kids */
377 TAILQ_INSERT_TAIL(&((struct kfs_i_info*)dir->i_fs_info)->children,
378 new_dentry, d_subdirs_link);
382 /* Removes the link from the dentry in the directory */
383 int kfs_unlink(struct inode *dir, struct dentry *dentry)
385 /* Stop tracking our child */
386 TAILQ_REMOVE(&((struct kfs_i_info*)dir->i_fs_info)->children, dentry,
388 kref_put(&dentry->d_kref); /* unpin the dentry, KFS-style */
392 /* Creates a new inode for a symlink dir, linking to / containing the name
393 * symname. dentry is the controlling dentry of the inode. */
394 int kfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
396 struct inode *inode = dentry->d_inode;
397 struct kfs_i_info *k_i_info = (struct kfs_i_info*)inode->i_fs_info;
398 size_t len = strlen(symname);
399 char *string = kmalloc(len + 1, 0);
401 kfs_init_inode(dir, dentry);
402 SET_FTYPE(inode->i_mode, __S_IFLNK);
403 inode->i_fop = &kfs_f_op_sym;
404 strncpy(string, symname, len);
405 string[len] = '\0'; /* symname should be \0d anyway, but just in case */
406 k_i_info->filestart = string; /* reusing this void* to hold the char* */
410 /* Called when creating a new inode for a directory associated with dentry in
411 * dir with the given mode. Note, we might (later) need to track subdirs within
412 * the parent inode, like we do with regular files. I'd rather not, so we'll
413 * see if we need it. */
414 int kfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
416 struct inode *inode = dentry->d_inode;
417 kref_get(&dentry->d_kref, 1); /* to pin the dentry in RAM, KFS-style... */
418 inode->i_ino = kfs_get_free_ino();
419 SET_FTYPE(inode->i_mode, __S_IFDIR);
420 inode->i_fop = &kfs_f_op_dir;
421 /* get ready to have our own kids */
422 TAILQ_INIT(&((struct kfs_i_info*)inode->i_fs_info)->children);
423 ((struct kfs_i_info*)inode->i_fs_info)->filestart = 0;
427 /* Removes from dir the directory 'dentry.' KFS doesn't store anything in the
428 * inode for which children it has. It probably should, but since everything is
429 * pinned, it just relies on the dentry connections. */
430 int kfs_rmdir(struct inode *dir, struct dentry *dentry)
432 struct kfs_i_info *d_info = (struct kfs_i_info*)dentry->d_inode->i_fs_info;
435 /* Check if we are empty. If not, error out, need to check the sub-dirs as
436 * well as the sub-"files" */
437 TAILQ_FOREACH(d_i, &dentry->d_subdirs, d_subdirs_link) {
441 TAILQ_FOREACH(d_i, &d_info->children, d_subdirs_link) {
447 kref_put(&dentry->d_kref); /* unpin the dentry, KFS-style */
448 printd("DENTRY %s REFCNT %d\n", dentry->d_name.name, kref_refcnt(&dentry->d_kref));
452 /* Used to make a generic file, based on the type and the major/minor numbers
453 * (in rdev), with the given mode. As with others, this creates a new disk
454 * inode for the file */
455 int kfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
460 /* Moves old_d from old_dir to new_d in new_dir. TODO: super racy */
461 int kfs_rename(struct inode *old_dir, struct dentry *old_d,
462 struct inode *new_dir, struct dentry *new_d)
464 /* new_d is already gone, we just use it for its name. kfs might not care
465 * about the name. it might just use whatever the dentry says. */
466 struct kfs_i_info *old_info = (struct kfs_i_info*)old_dir->i_fs_info;
467 struct kfs_i_info *new_info = (struct kfs_i_info*)new_dir->i_fs_info;
468 printd("KFS rename: %s/%s -> %s/%s\n",
469 TAILQ_FIRST(&old_dir->i_dentry)->d_name.name, old_d->d_name.name,
470 TAILQ_FIRST(&new_dir->i_dentry)->d_name.name, new_d->d_name.name);
471 /* we want to remove from the old and add to the new. for non-directories,
472 * we need to adjust parent's children lists (which reuses subdirs_link,
473 * yikes!). directories aren't actually tracked by KFS; it just hopes the
474 * VFS's pinned dentry tree is enough (aka, "all paths pinned"). */
475 if (!S_ISDIR(old_d->d_inode->i_mode)) {
476 TAILQ_REMOVE(&old_info->children, old_d, d_subdirs_link);
477 TAILQ_INSERT_TAIL(&new_info->children, old_d, d_subdirs_link);
482 /* Returns the char* for the symname for the given dentry. The VFS code that
483 * calls this for real FS's might assume it's already read in, so if the char *
484 * isn't already in memory, we'd need to read it in here. Regarding the char*
485 * storage, the char* only will last as long as the dentry and inode are in
487 char *kfs_readlink(struct dentry *dentry)
489 struct inode *inode = dentry->d_inode;
490 struct kfs_i_info *k_i_info = (struct kfs_i_info*)inode->i_fs_info;
491 if (!S_ISLNK(inode->i_mode))
493 return k_i_info->filestart;
496 /* Modifies the size of the file of inode to whatever its i_size is set to */
497 void kfs_truncate(struct inode *inode)
499 struct kfs_i_info *k_i_info = (struct kfs_i_info*)inode->i_fs_info;
500 /* init_size tracks how much of the file KFS has. everything else is 0s.
501 * we only need to update it if we are dropping data. as with other data
502 * beyond init_size, KFS will not save it during a write page! */
503 k_i_info->init_size = MIN(k_i_info->init_size, inode->i_size);
506 /* Checks whether the the access mode is allowed for the file belonging to the
507 * inode. Implies that the permissions are on the file, and not the hardlink */
508 int kfs_permission(struct inode *inode, int mode, struct nameidata *nd)
514 /* dentry_operations */
515 /* Determines if the dentry is still valid before using it to translate a path.
516 * Network FS's need to deal with this. */
517 int kfs_d_revalidate(struct dentry *dir, struct nameidata *nd)
518 { // default, nothing
522 /* Compares name1 and name2. name1 should be a member of dir. */
523 int kfs_d_compare(struct dentry *dir, struct qstr *name1, struct qstr *name2)
524 { // default, string comp (case sensitive)
528 /* Called when the last ref is deleted (refcnt == 0) */
529 int kfs_d_delete(struct dentry *dentry)
534 /* Called when it's about to be slab-freed */
535 int kfs_d_release(struct dentry *dentry)
540 /* Called when the dentry loses it's inode (becomes "negative") */
541 void kfs_d_iput(struct dentry *dentry, struct inode *inode)
542 { // default, call i_put to release the inode object
546 /* file_operations */
548 /* Updates the file pointer. TODO: think about locking. */
549 int kfs_llseek(struct file *file, off64_t offset, off64_t *ret, int whence)
551 off64_t temp_off = 0;
557 temp_off = file->f_pos + offset;
560 temp_off = file->f_dentry->d_inode->i_size + offset;
564 warn("Unknown 'whence' in llseek()!\n");
567 file->f_pos = temp_off;
572 /* Fills in the next directory entry (dirent), starting with d_off. KFS treats
573 * the size of each dirent as 1 byte, which we can get away with since the d_off
574 * is a way of communicating with future calls to readdir (FS-specific).
576 * Like with read and write, there will be issues with userspace and the *dirent
577 * buf. TODO: we don't really do anything with userspace concerns here, in part
578 * because memcpy_to doesn't work well. When we fix how we want to handle the
579 * userbuffers, we can write this accordingly. (UMEM) */
580 int kfs_readdir(struct file *dir, struct dirent *dirent)
582 int count = 2; /* total num dirents, gets incremented in check_entry() */
583 int desired_file = dirent->d_off;
585 struct dentry *subent;
586 struct dentry *dir_d = dir->f_dentry;
587 struct kfs_i_info *k_i_info = (struct kfs_i_info*)dir_d->d_inode->i_fs_info;
589 /* how we check inside the for loops below. moderately ghetto. */
590 void check_entry(void)
592 if (count++ == desired_file) {
593 dirent->d_ino = subent->d_inode->i_ino;
594 dirent->d_off = count;
595 dirent->d_reclen = subent->d_name.len;
596 /* d_name.name is null terminated, the byte after d_name.len */
597 assert(subent->d_name.len <= MAX_FILENAME_SZ);
598 strncpy(dirent->d_name, subent->d_name.name, subent->d_name.len +1);
603 /* Handle . and .. (first two dirents) */
604 if (desired_file == 0) {
605 dirent->d_ino = dir_d->d_inode->i_ino;
607 dirent->d_reclen = 1;
608 strncpy(dirent->d_name, ".", 2); /* the extra is for the null term */
610 } else if (desired_file == 1) {
611 dirent->d_ino = dir_d->d_parent->d_inode->i_ino;
613 dirent->d_reclen = 2;
614 strncpy(dirent->d_name, "..", 3); /* the extra is for the null term */
617 /* need to check the sub-dirs as well as the sub-"files". The main
618 * ghetto-ness with this is that we check even though we have our result,
619 * simply to figure out how big our directory is. It's just not worth
620 * changing at this point. */
621 TAILQ_FOREACH(subent, &dir_d->d_subdirs, d_subdirs_link)
623 TAILQ_FOREACH(subent, &k_i_info->children, d_subdirs_link)
627 if (count - 1 == desired_file) /* found the last dir in the list */
629 return 1; /* normal success for readdir */
632 /* This is called when a VMR is mapping a particular file. The FS needs to do
633 * whatever it needs so that faults can be handled by read_page(), and handle all
634 * of the cases of MAP_SHARED, MAP_PRIVATE, whatever. It also needs to ensure
635 * the file is not being mmaped in a way that conflicts with the manner in which
636 * the file was opened or the file type. */
637 int kfs_mmap(struct file *file, struct vm_region *vmr)
639 if (S_ISREG(file->f_dentry->d_inode->i_mode))
644 /* Called by the VFS while opening the file, which corresponds to inode, for
645 * the FS to do whatever it needs. */
646 int kfs_open(struct inode *inode, struct file *file)
651 /* Called when a file descriptor is closed. */
652 int kfs_flush(struct file *file)
657 /* Called when the file is about to be closed (file obj freed) */
658 int kfs_release(struct inode *inode, struct file *file)
663 /* Flushes the file's dirty contents to disc */
664 int kfs_fsync(struct file *file, struct dentry *dentry, int datasync)
669 /* Traditionally, sleeps until there is file activity. We probably won't
670 * support this, or we'll handle it differently. */
671 unsigned int kfs_poll(struct file *file, struct poll_table_struct *poll_table)
676 /* Reads count bytes from a file, starting from (and modifiying) offset, and
677 * putting the bytes into buffers described by vector */
678 ssize_t kfs_readv(struct file *file, const struct iovec *vector,
679 unsigned long count, off64_t *offset)
684 /* Writes count bytes to a file, starting from (and modifiying) offset, and
685 * taking the bytes from buffers described by vector */
686 ssize_t kfs_writev(struct file *file, const struct iovec *vector,
687 unsigned long count, off64_t *offset)
692 /* Write the contents of file to the page. Will sort the params later */
693 ssize_t kfs_sendpage(struct file *file, struct page *page, int offset,
694 size_t size, off64_t pos, int more)
699 /* Checks random FS flags. Used by NFS. */
700 int kfs_check_flags(int flags)
701 { // default, nothing
705 /* Redeclaration and initialization of the FS ops structures */
706 struct page_map_operations kfs_pm_op = {
711 struct super_operations kfs_s_op = {
727 struct inode_operations kfs_i_op = {
742 struct dentry_operations kfs_d_op = {
751 struct file_operations kfs_f_op_file = {
768 struct file_operations kfs_f_op_dir = {
785 struct file_operations kfs_f_op_sym = {
802 /* KFS Specific Internal Functions */
804 /* Need to pass path separately, since we'll recurse on it. TODO: this recurses,
805 * and takes up a lot of stack space (~270 bytes). Core 0's KSTACK is 8 pages,
806 * which can handle about 120 levels deep... Other cores are not so fortunate.
807 * Can rework this if it becomes an issue. */
808 static int __add_kfs_entry(struct dentry *parent, char *path,
809 struct cpio_bin_hdr *c_bhdr)
811 char *first_slash = strchr(path, '/');
812 char dir[MAX_FILENAME_SZ + 1]; /* room for the \0 */
813 size_t dirname_sz; /* not counting the \0 */
814 struct dentry *dentry = 0;
817 char *symname, old_end; /* for symlink manipulation */
820 /* get the first part, find that dentry, pass in the second part,
821 * recurse. this isn't being smart about extra slashes, dots, or
822 * anything like that. */
823 dirname_sz = first_slash - path;
824 assert(dirname_sz <= MAX_FILENAME_SZ);
825 strncpy(dir, path, dirname_sz);
826 dir[dirname_sz] = '\0';
827 printd("Finding DIR %s in dentry %s (start: %p, size %d)\n", dir,
828 parent->d_name.name, c_bhdr->c_filestart, c_bhdr->c_filesize);
829 /* Need to create a dentry for the lookup, and fill in the basic nd */
830 dentry = get_dentry(parent->d_sb, parent, dir);
831 /* TODO: use a VFS lookup instead, to use the dcache, thought its not a
832 * big deal since KFS currently pins all metadata. */
833 dentry = kfs_lookup(parent->d_inode, dentry, 0);
835 printk("Missing dir in CPIO archive or something, aborting.\n");
838 retval = __add_kfs_entry(dentry, first_slash + 1, c_bhdr);
839 kref_put(&dentry->d_kref);
842 /* no directories left in the path. add the 'file' to the dentry */
843 printd("Adding file/dir %s to dentry %s (start: %p, size %d)\n", path,
844 parent->d_name.name, c_bhdr->c_filestart, c_bhdr->c_filesize);
845 /* Init the dentry for this path */
846 dentry = get_dentry(parent->d_sb, parent, path);
847 // want to test the regular/natural dentry caching paths
848 //dcache_put(dentry->d_sb, dentry);
849 /* build the inode */
850 switch (c_bhdr->c_mode & CPIO_FILE_MASK) {
851 case (CPIO_DIRECTORY):
852 err = create_dir(parent->d_inode, dentry, c_bhdr->c_mode);
856 /* writing the '\0' is safe since the next entry is always still
857 * in the CPIO (and we are processing sequentially). */
858 symname = c_bhdr->c_filestart;
859 old_end = symname[c_bhdr->c_filesize];
860 symname[c_bhdr->c_filesize] = '\0';
861 err = create_symlink(parent->d_inode, dentry, symname,
862 c_bhdr->c_mode & CPIO_PERM_MASK);
864 symname[c_bhdr->c_filesize] = old_end;
866 case (CPIO_REG_FILE):
867 err = create_file(parent->d_inode, dentry,
868 c_bhdr->c_mode & CPIO_PERM_MASK);
870 ((struct kfs_i_info*)dentry->d_inode->i_fs_info)->filestart =
872 ((struct kfs_i_info*)dentry->d_inode->i_fs_info)->init_size =
876 printk("Unknown file type %d in the CPIO!",
877 c_bhdr->c_mode & CPIO_FILE_MASK);
878 kref_put(&dentry->d_kref);
881 inode = dentry->d_inode;
882 /* Set other info from the CPIO entry */
883 inode->i_uid = c_bhdr->c_uid;
884 inode->i_gid = c_bhdr->c_gid;
885 inode->i_atime.tv_sec = c_bhdr->c_mtime;
886 inode->i_ctime.tv_sec = c_bhdr->c_mtime;
887 inode->i_mtime.tv_sec = c_bhdr->c_mtime;
888 inode->i_size = c_bhdr->c_filesize;
889 //inode->i_XXX = c_bhdr->c_dev; /* and friends */
890 inode->i_bdev = 0; /* assuming blockdev? */
891 inode->i_socket = FALSE;
892 inode->i_blocks = c_bhdr->c_filesize; /* blocksize == 1 */
893 kref_put(&dentry->d_kref);
898 /* Adds an entry (from a CPIO archive) to KFS. This will put all the FS
899 * metadata in memory, instead of having to reparse the entire archive each time
900 * we need to traverse.
902 * The other option is to just maintain a LL of {FN, FS}, and O(n) scan it.
904 * The path is a complete path, interpreted from the root of the mount point.
905 * Directories have a size of 0. so do symlinks, but we don't handle those yet.
907 * If a directory does not exist for a file, this will return an error. Don't
908 * use the -depth flag to find when building the CPIO archive, and this won't be
909 * a problem. (Maybe) */
910 static int add_kfs_entry(struct super_block *sb, struct cpio_bin_hdr *c_bhdr)
912 char *path = c_bhdr->c_filename;
913 /* Root of the FS, already part of KFS */
914 if (!strcmp(path, "."))
916 return __add_kfs_entry(sb->s_mount->mnt_root, path, c_bhdr);
919 void parse_cpio_entries(struct super_block *sb, void *cpio_b)
921 struct cpio_newc_header *c_hdr = (struct cpio_newc_header*)cpio_b;
923 char buf[9] = {0}; /* temp space for strol conversions */
925 int offset = 0; /* offset in the cpio archive */
926 struct cpio_bin_hdr *c_bhdr = kmalloc(sizeof(*c_bhdr), 0);
927 memset(c_bhdr, 0, sizeof(*c_bhdr));
929 /* read all files and paths */
930 for (; ; c_hdr = (struct cpio_newc_header*)(cpio_b + offset)) {
931 offset += sizeof(*c_hdr);
932 if (strncmp(c_hdr->c_magic, "070701", 6)) {
933 printk("Invalid magic number in CPIO header, aborting.\n");
936 c_bhdr->c_filename = (char*)c_hdr + sizeof(*c_hdr);
937 namesize = cpio_strntol(buf, c_hdr->c_namesize, 8);
938 printd("Namesize: %d\n", namesize);
939 if (!strcmp(c_bhdr->c_filename, "TRAILER!!!"))
941 c_bhdr->c_ino = cpio_strntol(buf, c_hdr->c_ino, 8);
942 c_bhdr->c_mode = (int)cpio_strntol(buf, c_hdr->c_mode, 8);
943 c_bhdr->c_uid = cpio_strntol(buf, c_hdr->c_uid, 8);
944 c_bhdr->c_gid = cpio_strntol(buf, c_hdr->c_gid, 8);
945 c_bhdr->c_nlink = (unsigned int)cpio_strntol(buf, c_hdr->c_nlink, 8);
946 c_bhdr->c_mtime = cpio_strntol(buf, c_hdr->c_mtime, 8);
947 c_bhdr->c_filesize = cpio_strntol(buf, c_hdr->c_filesize, 8);
948 c_bhdr->c_dev_maj = cpio_strntol(buf, c_hdr->c_dev_maj, 8);
949 c_bhdr->c_dev_min = cpio_strntol(buf, c_hdr->c_dev_min, 8);
950 c_bhdr->c_rdev_maj = cpio_strntol(buf, c_hdr->c_rdev_maj, 8);
951 c_bhdr->c_rdev_min = cpio_strntol(buf, c_hdr->c_rdev_min, 8);
952 printd("File: %s: %d Bytes\n", c_bhdr->c_filename, c_bhdr->c_filesize);
954 /* header + name will be padded out to 4-byte alignment */
955 offset = ROUNDUP(offset, 4);
956 c_bhdr->c_filestart = cpio_b + offset;
957 /* make this a function pointer or something */
958 if (add_kfs_entry(sb, c_bhdr)) {
959 printk("Failed to add an entry to KFS!\n");
962 offset += c_bhdr->c_filesize;
963 offset = ROUNDUP(offset, 4);
964 //printk("offset is %d bytes\n", offset);
965 c_hdr = (struct cpio_newc_header*)(cpio_b + offset);