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 * Default implementations and global values for the VFS. */
7 #include <vfs.h> // keep this first
20 struct sb_tailq super_blocks = TAILQ_HEAD_INITIALIZER(super_blocks);
21 spinlock_t super_blocks_lock = SPINLOCK_INITIALIZER;
22 struct fs_type_tailq file_systems = TAILQ_HEAD_INITIALIZER(file_systems);
23 struct namespace default_ns;
25 struct kmem_cache *dentry_kcache; // not to be confused with the dcache
26 struct kmem_cache *inode_kcache;
27 struct kmem_cache *file_kcache;
29 /* Mounts fs from dev_name at mnt_pt in namespace ns. There could be no mnt_pt,
30 * such as with the root of (the default) namespace. Not sure how it would work
31 * with multiple namespaces on the same FS yet. Note if you mount the same FS
32 * multiple times, you only have one FS still (and one SB). If we ever support
34 struct vfsmount *__mount_fs(struct fs_type *fs, char *dev_name,
35 struct dentry *mnt_pt, int flags,
38 struct super_block *sb;
39 struct vfsmount *vmnt = kmalloc(sizeof(struct vfsmount), 0);
41 /* this first ref is stored in the NS tailq below */
42 kref_init(&vmnt->mnt_kref, fake_release, 1);
43 /* Build the vfsmount, if there is no mnt_pt, mnt is the root vfsmount (for now).
44 * fields related to the actual FS, like the sb and the mnt_root are set in
45 * the fs-specific get_sb() call. */
47 vmnt->mnt_parent = NULL;
48 vmnt->mnt_mountpoint = NULL;
49 } else { /* common case, but won't be tested til we try to mount another FS */
50 mnt_pt->d_mount_point = TRUE;
51 mnt_pt->d_mounted_fs = vmnt;
52 kref_get(&vmnt->mnt_kref, 1); /* held by mnt_pt */
53 vmnt->mnt_parent = mnt_pt->d_sb->s_mount;
54 vmnt->mnt_mountpoint = mnt_pt;
56 TAILQ_INIT(&vmnt->mnt_child_mounts);
57 vmnt->mnt_flags = flags;
58 vmnt->mnt_devname = dev_name;
59 vmnt->mnt_namespace = ns;
60 kref_get(&ns->kref, 1); /* held by vmnt */
62 /* Read in / create the SB */
63 sb = fs->get_sb(fs, flags, dev_name, vmnt);
65 panic("You're FS sucks");
67 /* TODO: consider moving this into get_sb or something, in case the SB
68 * already exists (mounting again) (if we support that) */
69 spin_lock(&super_blocks_lock);
70 TAILQ_INSERT_TAIL(&super_blocks, sb, s_list); /* storing a ref here... */
71 spin_unlock(&super_blocks_lock);
73 /* Update holding NS */
75 TAILQ_INSERT_TAIL(&ns->vfsmounts, vmnt, mnt_list);
76 spin_unlock(&ns->lock);
77 /* note to self: so, right after this point, the NS points to the root FS
78 * mount (we return the mnt, which gets assigned), the root mnt has a dentry
79 * for /, backed by an inode, with a SB prepped and in memory. */
87 dentry_kcache = kmem_cache_create("dentry", sizeof(struct dentry),
88 __alignof__(struct dentry), 0, 0, 0);
89 inode_kcache = kmem_cache_create("inode", sizeof(struct inode),
90 __alignof__(struct inode), 0, 0, 0);
91 file_kcache = kmem_cache_create("file", sizeof(struct file),
92 __alignof__(struct file), 0, 0, 0);
93 /* default NS never dies, +1 to exist */
94 kref_init(&default_ns.kref, fake_release, 1);
95 spinlock_init(&default_ns.lock);
96 default_ns.root = NULL;
97 TAILQ_INIT(&default_ns.vfsmounts);
99 /* build list of all FS's in the system. put yours here. if this is ever
100 * done on the fly, we'll need to lock. */
101 TAILQ_INSERT_TAIL(&file_systems, &kfs_fs_type, list);
102 #ifdef __CONFIG_EXT2FS__
103 TAILQ_INSERT_TAIL(&file_systems, &ext2_fs_type, list);
105 TAILQ_FOREACH(fs, &file_systems, list)
106 printk("Supports the %s Filesystem\n", fs->name);
108 /* mounting KFS at the root (/), pending root= parameters */
109 // TODO: linux creates a temp root_fs, then mounts the real root onto that
110 default_ns.root = __mount_fs(&kfs_fs_type, "RAM", NULL, 0, &default_ns);
112 printk("vfs_init() completed\n");
115 /* Builds / populates the qstr of a dentry based on its d_iname. If there is an
116 * l_name, (long), it will use that instead of the inline name. This will
117 * probably change a bit. */
118 void qstr_builder(struct dentry *dentry, char *l_name)
120 dentry->d_name.name = l_name ? l_name : dentry->d_iname;
121 // TODO: pending what we actually do in d_hash
122 //dentry->d_name.hash = dentry->d_op->d_hash(dentry, &dentry->d_name);
123 dentry->d_name.hash = 0xcafebabe;
124 dentry->d_name.len = strnlen(dentry->d_name.name, MAX_FILENAME_SZ);
127 /* Useful little helper - return the string ptr for a given file */
128 char *file_name(struct file *file)
130 return file->f_dentry->d_name.name;
133 /* Some issues with this, coupled closely to fs_lookup.
135 * Note the use of __dentry_free, instead of kref_put. In those cases, we don't
136 * want to treat it like a kref and we have the only reference to it, so it is
137 * okay to do this. It makes dentry_release() easier too. */
138 static struct dentry *do_lookup(struct dentry *parent, char *name)
140 struct dentry *result, *query;
141 query = get_dentry(parent->d_sb, parent, name);
142 result = dcache_get(parent->d_sb, query);
144 __dentry_free(query);
147 /* No result, check for negative */
148 if (query->d_flags & DENTRY_NEGATIVE) {
149 __dentry_free(query);
152 /* not in the dcache at all, need to consult the FS */
153 result = parent->d_inode->i_op->lookup(parent->d_inode, query, 0);
155 /* Note the USED flag will get turned off when this gets added to the
156 * LRU in dentry_release(). There's a slight race here that we'll panic
157 * on, but I want to catch it (in dcache_put()) for now. */
158 query->d_flags |= DENTRY_NEGATIVE;
159 dcache_put(parent->d_sb, query);
160 kref_put(&query->d_kref);
163 dcache_put(parent->d_sb, result);
164 /* This is because KFS doesn't return the same dentry, but ext2 does. this
165 * is ugly and needs to be fixed. (TODO) */
167 __dentry_free(query);
169 /* TODO: if the following are done by us, how do we know the i_ino?
170 * also need to handle inodes that are already read in! For now, we're
171 * going to have the FS handle it in it's lookup() method:
173 * - read in the inode
174 * - put in the inode cache */
178 /* Update ND such that it represents having followed dentry. IAW the nd
179 * refcnting rules, we need to decref any references that were in there before
180 * they get clobbered. */
181 static int next_link(struct dentry *dentry, struct nameidata *nd)
183 assert(nd->dentry && nd->mnt);
184 /* update the dentry */
185 kref_get(&dentry->d_kref, 1);
186 kref_put(&nd->dentry->d_kref);
188 /* update the mount, if we need to */
189 if (dentry->d_sb->s_mount != nd->mnt) {
190 kref_get(&dentry->d_sb->s_mount->mnt_kref, 1);
191 kref_put(&nd->mnt->mnt_kref);
192 nd->mnt = dentry->d_sb->s_mount;
197 /* Walk up one directory, being careful of mountpoints, namespaces, and the top
199 static int climb_up(struct nameidata *nd)
201 printd("CLIMB_UP, from %s\n", nd->dentry->d_name.name);
202 /* Top of the world, just return. Should also check for being at the top of
203 * the current process's namespace (TODO) */
204 if (!nd->dentry->d_parent || (nd->dentry->d_parent == nd->dentry))
206 /* Check if we are at the top of a mount, if so, we need to follow
207 * backwards, and then climb_up from that one. We might need to climb
208 * multiple times if we mount multiple FSs at the same spot (highly
209 * unlikely). This is completely untested. Might recurse instead. */
210 while (nd->mnt->mnt_root == nd->dentry) {
211 if (!nd->mnt->mnt_parent) {
212 warn("Might have expected a parent vfsmount (dentry had a parent)");
215 next_link(nd->mnt->mnt_mountpoint, nd);
217 /* Backwards walk (no mounts or any other issues now). */
218 next_link(nd->dentry->d_parent, nd);
219 printd("CLIMB_UP, to %s\n", nd->dentry->d_name.name);
223 /* nd->dentry might be on a mount point, so we need to move on to the child
225 static int follow_mount(struct nameidata *nd)
227 if (!nd->dentry->d_mount_point)
229 next_link(nd->dentry->d_mounted_fs->mnt_root, nd);
233 static int link_path_walk(char *path, struct nameidata *nd);
235 /* When nd->dentry is for a symlink, this will recurse and follow that symlink,
236 * so that nd contains the results of following the symlink (dentry and mnt).
237 * Returns when it isn't a symlink, 1 on following a link, and < 0 on error. */
238 static int follow_symlink(struct nameidata *nd)
242 if (!S_ISLNK(nd->dentry->d_inode->i_mode))
244 if (nd->depth > MAX_SYMLINK_DEPTH)
246 printd("Following symlink for dentry %08p %s\n", nd->dentry,
247 nd->dentry->d_name.name);
249 symname = nd->dentry->d_inode->i_op->readlink(nd->dentry);
250 /* We need to pin in nd->dentry (the dentry of the symlink), since we need
251 * it's symname's storage to stay in memory throughout the upcoming
252 * link_path_walk(). The last_sym gets decreffed when we path_release() or
253 * follow another symlink. */
255 kref_put(&nd->last_sym->d_kref);
256 kref_get(&nd->dentry->d_kref, 1);
257 nd->last_sym = nd->dentry;
258 /* If this an absolute path in the symlink, we need to free the old path and
259 * start over, otherwise, we continue from the PARENT of nd (the symlink) */
260 if (symname[0] == '/') {
263 nd->dentry = default_ns.root->mnt_root;
265 nd->dentry = current->fs_env.root;
266 nd->mnt = nd->dentry->d_sb->s_mount;
267 kref_get(&nd->mnt->mnt_kref, 1);
268 kref_get(&nd->dentry->d_kref, 1);
272 /* either way, keep on walking in the free world! */
273 retval = link_path_walk(symname, nd);
274 return (retval == 0 ? 1 : retval);
277 /* Little helper, to make it easier to break out of the nested loops. Will also
278 * '\0' out the first slash if it's slashes all the way down. Or turtles. */
279 static bool packed_trailing_slashes(char *first_slash)
281 for (char *i = first_slash; *i == '/'; i++) {
282 if (*(i + 1) == '\0') {
290 /* Simple helper to set nd to track it's last name to be Name. Also be careful
291 * with the storage of name. Don't use and nd's name past the lifetime of the
292 * string used in the path_lookup()/link_path_walk/whatever. Consider replacing
293 * parts of this with a qstr builder. Note this uses the dentry's d_op, which
294 * might not be the dentry we care about. */
295 static void stash_nd_name(struct nameidata *nd, char *name)
297 nd->last.name = name;
298 nd->last.len = strlen(name);
299 nd->last.hash = nd->dentry->d_op->d_hash(nd->dentry, &nd->last);
302 /* Resolves the links in a basic path walk. 0 for success, -EWHATEVER
303 * otherwise. The final lookup is returned via nd. */
304 static int link_path_walk(char *path, struct nameidata *nd)
306 struct dentry *link_dentry;
307 struct inode *link_inode, *nd_inode;
312 /* Prevent crazy recursion */
313 if (nd->depth > MAX_SYMLINK_DEPTH)
315 /* skip all leading /'s */
318 /* if there's nothing left (null terminated), we're done. This should only
319 * happen for "/", which if we wanted a PARENT, should fail (there is no
322 if (nd->flags & LOOKUP_PARENT) {
326 /* o/w, we're good */
329 /* iterate through each intermediate link of the path. in general, nd
330 * tracks where we are in the path, as far as dentries go. once we have the
331 * next dentry, we try to update nd based on that dentry. link is the part
332 * of the path string that we are looking up */
334 nd_inode = nd->dentry->d_inode;
335 if ((error = check_perms(nd_inode, nd->intent)))
337 /* find the next link, break out if it is the end */
338 next_slash = strchr(link, '/');
342 if (packed_trailing_slashes(next_slash)) {
343 nd->flags |= LOOKUP_DIRECTORY;
347 /* skip over any interim ./ */
348 if (!strncmp("./", link, 2))
350 /* Check for "../", walk up */
351 if (!strncmp("../", link, 3)) {
356 link_dentry = do_lookup(nd->dentry, link);
360 /* make link_dentry the current step/answer */
361 next_link(link_dentry, nd);
362 kref_put(&link_dentry->d_kref); /* do_lookup gave us a refcnt dentry */
363 /* we could be on a mountpoint or a symlink - need to follow them */
365 if ((error = follow_symlink(nd)) < 0)
367 /* Turn off a possible DIRECTORY lookup, which could have been set
368 * during the follow_symlink (a symlink could have had a directory at
369 * the end), though it was in the middle of the real path. */
370 nd->flags &= ~LOOKUP_DIRECTORY;
371 if (!S_ISDIR(nd->dentry->d_inode->i_mode))
374 /* move through the path string to the next entry */
375 link = next_slash + 1;
376 /* advance past any other interim slashes. we know we won't hit the end
377 * due to the for loop check above */
381 /* Now, we're on the last link of the path. We need to deal with with . and
382 * .. . This might be weird with PARENT lookups - not sure what semantics
383 * we want exactly. This will give the parent of whatever the PATH was
384 * supposed to look like. Note that ND currently points to the parent of
385 * the last item (link). */
386 if (!strcmp(".", link)) {
387 if (nd->flags & LOOKUP_PARENT) {
388 assert(nd->dentry->d_name.name);
389 stash_nd_name(nd, nd->dentry->d_name.name);
394 if (!strcmp("..", link)) {
396 if (nd->flags & LOOKUP_PARENT) {
397 assert(nd->dentry->d_name.name);
398 stash_nd_name(nd, nd->dentry->d_name.name);
403 /* need to attempt to look it up, in case it's a symlink */
404 link_dentry = do_lookup(nd->dentry, link);
406 /* if there's no dentry, we are okay if we are looking for the parent */
407 if (nd->flags & LOOKUP_PARENT) {
408 assert(strcmp(link, ""));
409 stash_nd_name(nd, link);
415 next_link(link_dentry, nd);
416 kref_put(&link_dentry->d_kref); /* do_lookup gave us a refcnt'd dentry */
417 /* at this point, nd is on the final link, but it might be a symlink */
418 if (nd->flags & LOOKUP_FOLLOW) {
419 error = follow_symlink(nd);
422 /* if we actually followed a symlink, then nd is set and we're done */
426 /* One way or another, nd is on the last element of the path, symlinks and
427 * all. Now we need to climb up to set nd back on the parent, if that's
429 if (nd->flags & LOOKUP_PARENT) {
430 assert(nd->dentry->d_name.name);
431 stash_nd_name(nd, link_dentry->d_name.name);
435 /* now, we have the dentry set, and don't want the parent, but might be on a
436 * mountpoint still. FYI: this hasn't been thought through completely. */
438 /* If we wanted a directory, but didn't get one, error out */
439 if ((nd->flags & LOOKUP_DIRECTORY) && !S_ISDIR(nd->dentry->d_inode->i_mode))
444 /* Given path, return the inode for the final dentry. The ND should be
445 * initialized for the first call - specifically, we need the intent.
446 * LOOKUP_PARENT and friends go in the flags var, which is not the intent.
448 * If path_lookup wants a PARENT, but hits the top of the FS (root or
449 * otherwise), we want it to error out. It's still unclear how we want to
450 * handle processes with roots that aren't root, but at the very least, we don't
451 * want to think we have the parent of /, but have / itself. Due to the way
452 * link_path_walk works, if that happened, we probably don't have a
453 * nd->last.name. This needs more thought (TODO).
455 * Need to be careful too. While the path has been copied-in to the kernel,
456 * it's still user input. */
457 int path_lookup(char *path, int flags, struct nameidata *nd)
460 printd("Path lookup for %s\n", path);
461 /* we allow absolute lookups with no process context */
462 if (path[0] == '/') { /* absolute lookup */
464 nd->dentry = default_ns.root->mnt_root;
466 nd->dentry = current->fs_env.root;
467 } else { /* relative lookup */
469 /* Don't need to lock on the fs_env since we're reading one item */
470 nd->dentry = current->fs_env.pwd;
472 nd->mnt = nd->dentry->d_sb->s_mount;
473 /* Whenever references get put in the nd, incref them. Whenever they are
474 * removed, decref them. */
475 kref_get(&nd->mnt->mnt_kref, 1);
476 kref_get(&nd->dentry->d_kref, 1);
478 nd->depth = 0; /* used in symlink following */
479 retval = link_path_walk(path, nd);
480 /* make sure our PARENT lookup worked */
481 if (!retval && (flags & LOOKUP_PARENT))
482 assert(nd->last.name);
486 /* Call this after any use of path_lookup when you are done with its results,
487 * regardless of whether it succeeded or not. It will free any references */
488 void path_release(struct nameidata *nd)
490 kref_put(&nd->dentry->d_kref);
491 kref_put(&nd->mnt->mnt_kref);
492 /* Free the last symlink dentry used, if there was one */
494 kref_put(&nd->last_sym->d_kref);
495 nd->last_sym = 0; /* catch reuse bugs */
499 /* External version of mount, only call this after having a / mount */
500 int mount_fs(struct fs_type *fs, char *dev_name, char *path, int flags)
502 struct nameidata nd_r = {0}, *nd = &nd_r;
504 retval = path_lookup(path, LOOKUP_DIRECTORY, nd);
507 /* taking the namespace of the vfsmount of path */
508 if (!__mount_fs(fs, dev_name, nd->dentry, flags, nd->mnt->mnt_namespace))
515 /* Superblock functions */
517 /* Dentry "hash" function for the hash table to use. Since we already have the
518 * hash in the qstr, we don't need to rehash. Also, note we'll be using the
519 * dentry in question as both the key and the value. */
520 static size_t __dcache_hash(void *k)
522 return (size_t)((struct dentry*)k)->d_name.hash;
525 /* Dentry cache hashtable equality function. This means we need to pass in some
526 * minimal dentry when doing a lookup. */
527 static ssize_t __dcache_eq(void *k1, void *k2)
529 if (((struct dentry*)k1)->d_parent != ((struct dentry*)k2)->d_parent)
531 /* TODO: use the FS-specific string comparison */
532 return !strcmp(((struct dentry*)k1)->d_name.name,
533 ((struct dentry*)k2)->d_name.name);
536 /* Helper to alloc and initialize a generic superblock. This handles all the
537 * VFS related things, like lists. Each FS will need to handle its own things
538 * in it's *_get_sb(), usually involving reading off the disc. */
539 struct super_block *get_sb(void)
541 struct super_block *sb = kmalloc(sizeof(struct super_block), 0);
543 spinlock_init(&sb->s_lock);
544 kref_init(&sb->s_kref, fake_release, 1); /* for the ref passed out */
545 TAILQ_INIT(&sb->s_inodes);
546 TAILQ_INIT(&sb->s_dirty_i);
547 TAILQ_INIT(&sb->s_io_wb);
548 TAILQ_INIT(&sb->s_lru_d);
549 TAILQ_INIT(&sb->s_files);
550 sb->s_dcache = create_hashtable(100, __dcache_hash, __dcache_eq);
551 sb->s_icache = create_hashtable(100, __generic_hash, __generic_eq);
552 spinlock_init(&sb->s_lru_lock);
553 spinlock_init(&sb->s_dcache_lock);
554 spinlock_init(&sb->s_icache_lock);
555 sb->s_fs_info = 0; // can override somewhere else
559 /* Final stages of initializing a super block, including creating and linking
560 * the root dentry, root inode, vmnt, and sb. The d_op and root_ino are
561 * FS-specific, but otherwise it's FS-independent, tricky, and not worth having
562 * around multiple times.
564 * Not the world's best interface, so it's subject to change, esp since we're
565 * passing (now 3) FS-specific things. */
566 void init_sb(struct super_block *sb, struct vfsmount *vmnt,
567 struct dentry_operations *d_op, unsigned long root_ino,
570 /* Build and init the first dentry / inode. The dentry ref is stored later
571 * by vfsmount's mnt_root. The parent is dealt with later. */
572 struct dentry *d_root = get_dentry(sb, 0, "/"); /* probably right */
574 /* a lot of here on down is normally done in lookup() or create, since
575 * get_dentry isn't a fully usable dentry. The two FS-specific settings are
576 * normally inherited from a parent within the same FS in get_dentry, but we
579 d_root->d_fs_info = d_fs_info;
580 struct inode *inode = get_inode(d_root);
582 panic("This FS sucks!");
583 inode->i_ino = root_ino;
584 /* TODO: add the inode to the appropriate list (off i_list) */
585 /* TODO: do we need to read in the inode? can we do this on demand? */
586 /* if this FS is already mounted, we'll need to do something different. */
587 sb->s_op->read_inode(inode);
588 icache_put(sb, inode);
589 /* Link the dentry and SB to the VFS mount */
590 vmnt->mnt_root = d_root; /* ref comes from get_dentry */
592 /* If there is no mount point, there is no parent. This is true only for
594 if (vmnt->mnt_mountpoint) {
595 kref_get(&vmnt->mnt_mountpoint->d_kref, 1); /* held by d_root */
596 d_root->d_parent = vmnt->mnt_mountpoint; /* dentry of the root */
598 d_root->d_parent = d_root; /* set root as its own parent */
600 /* insert the dentry into the dentry cache. when's the earliest we can?
601 * when's the earliest we should? what about concurrent accesses to the
602 * same dentry? should be locking the dentry... */
603 dcache_put(sb, d_root);
604 kref_put(&inode->i_kref); /* give up the ref from get_inode() */
607 /* Dentry Functions */
609 /* Helper to alloc and initialize a generic dentry. The following needs to be
610 * set still: d_op (if no parent), d_fs_info (opt), d_inode, connect the inode
611 * to the dentry (and up the d_kref again), maybe dcache_put(). The inode
612 * stitching is done in get_inode() or lookup (depending on the FS).
613 * The setting of the d_op might be problematic when dealing with mounts. Just
616 * If the name is longer than the inline name, it will kmalloc a buffer, so
617 * don't worry about the storage for *name after calling this. */
618 struct dentry *get_dentry(struct super_block *sb, struct dentry *parent,
622 size_t name_len = strnlen(name, MAX_FILENAME_SZ); /* not including \0! */
623 struct dentry *dentry = kmem_cache_alloc(dentry_kcache, 0);
628 //memset(dentry, 0, sizeof(struct dentry));
629 kref_init(&dentry->d_kref, dentry_release, 1); /* this ref is returned */
630 spinlock_init(&dentry->d_lock);
631 TAILQ_INIT(&dentry->d_subdirs);
633 kref_get(&sb->s_kref, 1);
634 dentry->d_sb = sb; /* storing a ref here... */
635 dentry->d_mount_point = FALSE;
636 dentry->d_mounted_fs = 0;
637 if (parent) { /* no parent for rootfs mount */
638 kref_get(&parent->d_kref, 1);
639 dentry->d_op = parent->d_op; /* d_op set in init_sb for parentless */
641 dentry->d_parent = parent;
642 dentry->d_flags = DENTRY_USED;
643 dentry->d_fs_info = 0;
644 if (name_len < DNAME_INLINE_LEN) {
645 strncpy(dentry->d_iname, name, name_len);
646 dentry->d_iname[name_len] = '\0';
647 qstr_builder(dentry, 0);
649 l_name = kmalloc(name_len + 1, 0);
651 strncpy(l_name, name, name_len);
652 l_name[name_len] = '\0';
653 qstr_builder(dentry, l_name);
655 /* Catch bugs by aggressively zeroing this (o/w we use old stuff) */
660 /* Called when the dentry is unreferenced (after kref == 0). This works closely
661 * with the resurrection in dcache_get().
663 * The dentry is still in the dcache, but needs to be un-USED and added to the
664 * LRU dentry list. Even dentries that were used in a failed lookup need to be
665 * cached - they ought to be the negative dentries. Note that all dentries have
666 * parents, even negative ones (it is needed to find it in the dcache). */
667 void dentry_release(struct kref *kref)
669 struct dentry *dentry = container_of(kref, struct dentry, d_kref);
671 printd("'Releasing' dentry %08p: %s\n", dentry, dentry->d_name.name);
672 /* DYING dentries (recently unlinked / rmdir'd) just get freed */
673 if (dentry->d_flags & DENTRY_DYING) {
674 __dentry_free(dentry);
677 /* This lock ensures the USED state and the TAILQ membership is in sync.
678 * Also used to check the refcnt, though that might not be necessary. */
679 spin_lock(&dentry->d_lock);
680 /* While locked, we need to double check the kref, in case someone already
681 * reup'd it. Re-up? you're crazy! Reee-up, you're outta yo mind! */
682 if (!kref_refcnt(&dentry->d_kref)) {
683 /* Note this is where negative dentries get set UNUSED */
684 if (dentry->d_flags & DENTRY_USED) {
685 dentry->d_flags &= ~DENTRY_USED;
686 spin_lock(&dentry->d_sb->s_lru_lock);
687 TAILQ_INSERT_TAIL(&dentry->d_sb->s_lru_d, dentry, d_lru);
688 spin_unlock(&dentry->d_sb->s_lru_lock);
690 /* and make sure it wasn't USED, then UNUSED again */
691 /* TODO: think about issues with this */
692 warn("This should be rare. Tell brho this happened.");
695 spin_unlock(&dentry->d_lock);
698 /* Called when we really dealloc and get rid of a dentry (like when it is
699 * removed from the dcache, either for memory or correctness reasons)
701 * This has to handle two types of dentries: full ones (ones that had been used)
702 * and ones that had been just for lookups - hence the check for d_inode.
704 * Note that dentries pin and kref their inodes. When all the dentries are
705 * gone, we want the inode to be released via kref. The inode has internal /
706 * weak references to the dentry, which are not refcounted. */
707 void __dentry_free(struct dentry *dentry)
710 printk("Freeing dentry %08p: %s\n", dentry, dentry->d_name.name);
711 assert(dentry->d_op); /* catch bugs. a while back, some lacked d_op */
712 dentry->d_op->d_release(dentry);
713 /* TODO: check/test the boundaries on this. */
714 if (dentry->d_name.len > DNAME_INLINE_LEN)
715 kfree((void*)dentry->d_name.name);
716 kref_put(&dentry->d_sb->s_kref);
717 if (dentry->d_parent)
718 kref_put(&dentry->d_parent->d_kref);
719 if (dentry->d_mounted_fs)
720 kref_put(&dentry->d_mounted_fs->mnt_kref);
721 if (dentry->d_inode) {
722 TAILQ_REMOVE(&dentry->d_inode->i_dentry, dentry, d_alias);
723 kref_put(&dentry->d_inode->i_kref); /* dentries kref inodes */
725 kmem_cache_free(dentry_kcache, dentry);
728 /* Looks up the dentry for the given path, returning a refcnt'd dentry (or 0).
729 * Permissions are applied for the current user, which is quite a broken system
730 * at the moment. Flags are lookup flags. */
731 struct dentry *lookup_dentry(char *path, int flags)
733 struct dentry *dentry;
734 struct nameidata nd_r = {0}, *nd = &nd_r;
737 error = path_lookup(path, flags, nd);
744 kref_get(&dentry->d_kref, 1);
749 /* Get a dentry from the dcache. At a minimum, we need the name hash and parent
750 * in what_i_want, though most uses will probably be from a get_dentry() call.
751 * We pass in the SB in the off chance that we don't want to use a get'd dentry.
753 * The unusual variable name (instead of just "key" or something) is named after
754 * ex-SPC Castro's porn folder. Caller deals with the memory for what_i_want.
756 * If the dentry is negative, we don't return the actual result - instead, we
757 * set the negative flag in 'what i want'. The reason is we don't want to
758 * kref_get() and then immediately put (causing dentry_release()). This also
759 * means that dentry_release() should never get someone who wasn't USED (barring
760 * the race, which it handles). And we don't need to ever have a dentry set as
761 * USED and NEGATIVE (which is always wrong, but would be needed for a cleaner
764 * This is where we do the "kref resurrection" - we are returning a kref'd
765 * object, even if it wasn't kref'd before. This means the dcache does NOT hold
766 * krefs (it is a weak/internal ref), but it is a source of kref generation. We
767 * sync up with the possible freeing of the dentry by locking the table. See
768 * Doc/kref for more info. */
769 struct dentry *dcache_get(struct super_block *sb, struct dentry *what_i_want)
771 struct dentry *found;
772 /* This lock protects the hash, as well as ensures the returned object
773 * doesn't get deleted/freed out from under us */
774 spin_lock(&sb->s_dcache_lock);
775 found = hashtable_search(sb->s_dcache, what_i_want);
777 if (found->d_flags & DENTRY_NEGATIVE) {
778 what_i_want->d_flags |= DENTRY_NEGATIVE;
779 spin_unlock(&sb->s_dcache_lock);
782 spin_lock(&found->d_lock);
783 __kref_get(&found->d_kref, 1); /* prob could be done outside the lock*/
784 /* If we're here (after kreffing) and it is not USED, we are the one who
785 * should resurrect */
786 if (!(found->d_flags & DENTRY_USED)) {
787 found->d_flags |= DENTRY_USED;
788 spin_lock(&sb->s_lru_lock);
789 TAILQ_REMOVE(&sb->s_lru_d, found, d_lru);
790 spin_unlock(&sb->s_lru_lock);
792 spin_unlock(&found->d_lock);
794 spin_unlock(&sb->s_dcache_lock);
798 /* Adds a dentry to the dcache. Note the *dentry is both the key and the value.
799 * If the value was already in there (which can happen iff it was negative), for
800 * now we'll remove it and put the new one in there. */
801 void dcache_put(struct super_block *sb, struct dentry *key_val)
805 spin_lock(&sb->s_dcache_lock);
806 old = hashtable_remove(sb->s_dcache, key_val);
808 assert(old->d_flags & DENTRY_NEGATIVE);
809 /* This is possible, but rare for now (about to be put on the LRU) */
810 assert(!(old->d_flags & DENTRY_USED));
811 assert(!kref_refcnt(&old->d_kref));
812 spin_lock(&sb->s_lru_lock);
813 TAILQ_REMOVE(&sb->s_lru_d, old, d_lru);
814 spin_unlock(&sb->s_lru_lock);
817 /* this returns 0 on failure (TODO: Fix this ghetto shit) */
818 retval = hashtable_insert(sb->s_dcache, key_val, key_val);
820 spin_unlock(&sb->s_dcache_lock);
823 /* Will remove and return the dentry. Caller deallocs the key, but the retval
824 * won't have a reference. * Returns 0 if it wasn't found. Callers can't
825 * assume much - they should not use the reference they *get back*, (if they
826 * already had one for key, they can use that). There may be other users out
828 struct dentry *dcache_remove(struct super_block *sb, struct dentry *key)
830 struct dentry *retval;
831 spin_lock(&sb->s_dcache_lock);
832 retval = hashtable_remove(sb->s_dcache, key);
833 spin_unlock(&sb->s_dcache_lock);
837 /* This will clean out the LRU list, which are the unused dentries of the dentry
838 * cache. This will optionally only free the negative ones. Note that we grab
839 * the hash lock for the time we traverse the LRU list - this prevents someone
840 * from getting a kref from the dcache, which could cause us trouble (we rip
841 * someone off the list, who isn't unused, and they try to rip them off the
843 void dcache_prune(struct super_block *sb, bool negative_only)
845 struct dentry *d_i, *temp;
846 struct dentry_tailq victims = TAILQ_HEAD_INITIALIZER(victims);
848 spin_lock(&sb->s_dcache_lock);
849 spin_lock(&sb->s_lru_lock);
850 TAILQ_FOREACH_SAFE(d_i, &sb->s_lru_d, d_lru, temp) {
851 if (!(d_i->d_flags & DENTRY_USED)) {
852 if (negative_only && !(d_i->d_flags & DENTRY_NEGATIVE))
854 /* another place where we'd be better off with tools, not sol'ns */
855 hashtable_remove(sb->s_dcache, d_i);
856 TAILQ_REMOVE(&sb->s_lru_d, d_i, d_lru);
857 TAILQ_INSERT_HEAD(&victims, d_i, d_lru);
860 spin_unlock(&sb->s_lru_lock);
861 spin_unlock(&sb->s_dcache_lock);
862 /* Now do the actual freeing, outside of the hash/LRU list locks. This is
863 * necessary since __dentry_free() will decref its parent, which may get
864 * released and try to add itself to the LRU. */
865 TAILQ_FOREACH_SAFE(d_i, &victims, d_lru, temp) {
866 TAILQ_REMOVE(&victims, d_i, d_lru);
867 assert(!kref_refcnt(&d_i->d_kref));
870 /* It is possible at this point that there are new items on the LRU. We
871 * could loop back until that list is empty, if we care about this. */
874 /* Inode Functions */
876 /* Creates and initializes a new inode. Generic fields are filled in.
877 * FS-specific fields are filled in by the callout. Specific fields are filled
878 * in in read_inode() based on what's on the disk for a given i_no, or when the
879 * inode is created (for new objects).
881 * i_no is set by the caller. Note that this means this inode can be for an
882 * inode that is already on disk, or it can be used when creating. */
883 struct inode *get_inode(struct dentry *dentry)
885 struct super_block *sb = dentry->d_sb;
886 /* FS allocs and sets the following: i_op, i_fop, i_pm.pm_op, and any FS
888 struct inode *inode = sb->s_op->alloc_inode(sb);
893 TAILQ_INSERT_HEAD(&sb->s_inodes, inode, i_sb_list); /* weak inode ref */
894 TAILQ_INIT(&inode->i_dentry);
895 TAILQ_INSERT_TAIL(&inode->i_dentry, dentry, d_alias); /* weak dentry ref*/
896 /* one for the dentry->d_inode, one passed out */
897 kref_init(&inode->i_kref, inode_release, 2);
898 dentry->d_inode = inode;
899 inode->i_ino = 0; /* set by caller later */
900 inode->i_blksize = sb->s_blocksize;
901 spinlock_init(&inode->i_lock);
902 kref_get(&sb->s_kref, 1); /* could allow the dentry to pin it */
904 inode->i_rdev = 0; /* this has no real meaning yet */
905 inode->i_bdev = sb->s_bdev; /* storing an uncounted ref */
906 inode->i_state = 0; /* need real states, like I_NEW */
907 inode->dirtied_when = 0;
909 atomic_set(&inode->i_writecount, 0);
910 /* Set up the page_map structures. Default is to use the embedded one.
911 * Might push some of this back into specific FSs. For now, the FS tells us
912 * what pm_op they want via i_pm.pm_op, which we set again in pm_init() */
913 inode->i_mapping = &inode->i_pm;
914 pm_init(inode->i_mapping, inode->i_pm.pm_op, inode);
918 /* Helper: loads/ reads in the inode numbered ino and attaches it to dentry */
919 void load_inode(struct dentry *dentry, unsigned long ino)
923 /* look it up in the inode cache first */
924 inode = icache_get(dentry->d_sb, ino);
926 /* connect the dentry to its inode */
927 TAILQ_INSERT_TAIL(&inode->i_dentry, dentry, d_alias);
928 dentry->d_inode = inode; /* storing the ref we got from icache_get */
931 /* otherwise, we need to do it manually */
932 inode = get_inode(dentry);
934 dentry->d_sb->s_op->read_inode(inode);
935 /* TODO: race here, two creators could miss in the cache, and then get here.
936 * need a way to sync across a blocking call. needs to be either at this
937 * point in the code or per the ino (dentries could be different) */
938 icache_put(dentry->d_sb, inode);
939 kref_put(&inode->i_kref);
942 /* Helper op, used when creating regular files, directories, symlinks, etc.
943 * Note we make a distinction between the mode and the file type (for now).
944 * After calling this, call the FS specific version (create or mkdir), which
945 * will set the i_ino, the filetype, and do any other FS-specific stuff. Also
946 * note that a lot of inode stuff was initialized in get_inode/alloc_inode. The
947 * stuff here is pertinent to the specific creator (user), mode, and time. Also
948 * note we don't pass this an nd, like Linux does... */
949 static struct inode *create_inode(struct dentry *dentry, int mode)
951 /* note it is the i_ino that uniquely identifies a file in the specific
952 * filesystem. there's a diff between creating an inode (even for an in-use
953 * ino) and then filling it in, and vs creating a brand new one.
954 * get_inode() sets it to 0, and it should be filled in later in an
955 * FS-specific manner. */
956 struct inode *inode = get_inode(dentry);
959 inode->i_mode = mode & S_PMASK; /* note that after this, we have no type */
963 inode->i_atime.tv_sec = 0; /* TODO: now! */
964 inode->i_ctime.tv_sec = 0;
965 inode->i_mtime.tv_sec = 0;
966 inode->i_atime.tv_nsec = 0; /* are these supposed to be the extra ns? */
967 inode->i_ctime.tv_nsec = 0;
968 inode->i_mtime.tv_nsec = 0;
969 inode->i_bdev = inode->i_sb->s_bdev;
970 /* when we have notions of users, do something here: */
976 /* Create a new disk inode in dir associated with dentry, with the given mode.
977 * called when creating a regular file. dir is the directory/parent. dentry is
978 * the dentry of the inode we are creating. Note the lack of the nd... */
979 int create_file(struct inode *dir, struct dentry *dentry, int mode)
981 struct inode *new_file = create_inode(dentry, mode);
984 dir->i_op->create(dir, dentry, mode, 0);
985 icache_put(new_file->i_sb, new_file);
986 kref_put(&new_file->i_kref);
990 /* Creates a new inode for a directory associated with dentry in dir with the
992 int create_dir(struct inode *dir, struct dentry *dentry, int mode)
994 struct inode *new_dir = create_inode(dentry, mode);
997 dir->i_op->mkdir(dir, dentry, mode);
998 dir->i_nlink++; /* Directories get a hardlink for every child dir */
999 /* Make sure my parent tracks me. This is okay, since no directory (dir)
1000 * can have more than one dentry */
1001 struct dentry *parent = TAILQ_FIRST(&dir->i_dentry);
1002 assert(parent && parent == TAILQ_LAST(&dir->i_dentry, dentry_tailq));
1003 /* parent dentry tracks dentry as a subdir, weak reference */
1004 TAILQ_INSERT_TAIL(&parent->d_subdirs, dentry, d_subdirs_link);
1005 icache_put(new_dir->i_sb, new_dir);
1006 kref_put(&new_dir->i_kref);
1010 /* Creates a new inode for a symlink associated with dentry in dir, containing
1011 * the symlink symname */
1012 int create_symlink(struct inode *dir, struct dentry *dentry,
1013 const char *symname, int mode)
1015 struct inode *new_sym = create_inode(dentry, mode);
1018 dir->i_op->symlink(dir, dentry, symname);
1019 icache_put(new_sym->i_sb, new_sym);
1020 kref_put(&new_sym->i_kref);
1024 /* Returns 0 if the given mode is acceptable for the inode, and an appropriate
1025 * error code if not. Needs to be writen, based on some sensible rules, and
1026 * will also probably use 'current' */
1027 int check_perms(struct inode *inode, int access_mode)
1029 return 0; /* anything goes! */
1032 /* Called after all external refs are gone to clean up the inode. Once this is
1033 * called, all dentries pointing here are already done (one of them triggered
1034 * this via kref_put(). */
1035 void inode_release(struct kref *kref)
1037 struct inode *inode = container_of(kref, struct inode, i_kref);
1038 TAILQ_REMOVE(&inode->i_sb->s_inodes, inode, i_sb_list);
1039 icache_remove(inode->i_sb, inode->i_ino);
1040 /* Might need to write back or delete the file/inode */
1041 if (inode->i_nlink) {
1042 if (inode->i_state & I_STATE_DIRTY)
1043 inode->i_sb->s_op->write_inode(inode, TRUE);
1045 inode->i_sb->s_op->delete_inode(inode);
1047 /* Either way, we dealloc the in-memory version */
1048 inode->i_sb->s_op->dealloc_inode(inode); /* FS-specific clean-up */
1049 kref_put(&inode->i_sb->s_kref);
1050 /* TODO: clean this up */
1051 assert(inode->i_mapping == &inode->i_pm);
1052 kmem_cache_free(inode_kcache, inode);
1054 // kref_put(inode->i_bdev->kref); /* assuming it's a bdev */
1057 /* Fills in kstat with the stat information for the inode */
1058 void stat_inode(struct inode *inode, struct kstat *kstat)
1060 kstat->st_dev = inode->i_sb->s_dev;
1061 kstat->st_ino = inode->i_ino;
1062 kstat->st_mode = inode->i_mode;
1063 kstat->st_nlink = inode->i_nlink;
1064 kstat->st_uid = inode->i_uid;
1065 kstat->st_gid = inode->i_gid;
1066 kstat->st_rdev = inode->i_rdev;
1067 kstat->st_size = inode->i_size;
1068 kstat->st_blksize = inode->i_blksize;
1069 kstat->st_blocks = inode->i_blocks;
1070 kstat->st_atime = inode->i_atime;
1071 kstat->st_mtime = inode->i_mtime;
1072 kstat->st_ctime = inode->i_ctime;
1075 /* Inode Cache management. In general, search on the ino, get a refcnt'd value
1076 * back. Remove does not give you a reference back - it should only be called
1077 * in inode_release(). */
1078 struct inode *icache_get(struct super_block *sb, unsigned long ino)
1080 /* This is the same style as in pid2proc, it's the "safely create a strong
1081 * reference from a weak one, so long as other strong ones exist" pattern */
1082 spin_lock(&sb->s_icache_lock);
1083 struct inode *inode = hashtable_search(sb->s_icache, (void*)ino);
1085 if (!kref_get_not_zero(&inode->i_kref, 1))
1087 spin_unlock(&sb->s_icache_lock);
1091 void icache_put(struct super_block *sb, struct inode *inode)
1093 spin_lock(&sb->s_icache_lock);
1094 /* there's a race in load_ino() that could trigger this */
1095 assert(!hashtable_search(sb->s_icache, (void*)inode->i_ino));
1096 hashtable_insert(sb->s_icache, (void*)inode->i_ino, inode);
1097 spin_unlock(&sb->s_icache_lock);
1100 struct inode *icache_remove(struct super_block *sb, unsigned long ino)
1102 struct inode *inode;
1103 /* Presumably these hashtable removals could be easier since callers
1104 * actually know who they are (same with the pid2proc hash) */
1105 spin_lock(&sb->s_icache_lock);
1106 inode = hashtable_remove(sb->s_icache, (void*)ino);
1107 spin_unlock(&sb->s_icache_lock);
1108 assert(inode && !kref_refcnt(&inode->i_kref));
1112 /* File functions */
1114 /* Read count bytes from the file into buf, starting at *offset, which is
1115 * increased accordingly, returning the number of bytes transfered. Most
1116 * filesystems will use this function for their f_op->read.
1117 * Note, this uses the page cache. */
1118 ssize_t generic_file_read(struct file *file, char *buf, size_t count,
1124 unsigned long first_idx, last_idx;
1128 /* Consider pushing some error checking higher in the VFS */
1131 if (*offset == file->f_dentry->d_inode->i_size)
1133 /* Make sure we don't go past the end of the file */
1134 if (*offset + count > file->f_dentry->d_inode->i_size) {
1135 count = file->f_dentry->d_inode->i_size - *offset;
1137 page_off = *offset & (PGSIZE - 1);
1138 first_idx = *offset >> PGSHIFT;
1139 last_idx = (*offset + count) >> PGSHIFT;
1140 buf_end = buf + count;
1141 /* For each file page, make sure it's in the page cache, then copy it out.
1142 * TODO: will probably need to consider concurrently truncated files here.*/
1143 for (int i = first_idx; i <= last_idx; i++) {
1144 error = pm_load_page(file->f_mapping, i, &page);
1145 assert(!error); /* TODO: handle ENOMEM and friends */
1146 copy_amt = MIN(PGSIZE - page_off, buf_end - buf);
1147 /* TODO: (UMEM) think about this. if it's a user buffer, we're relying
1148 * on current to detect whose it is (which should work for async calls).
1149 * Also, need to propagate errors properly... Probably should do a
1150 * user_mem_check, then free, and also to make a distinction between
1151 * when the kernel wants a read/write (TODO: KFOP) */
1153 memcpy_to_user(current, buf, page2kva(page) + page_off, copy_amt);
1155 memcpy(buf, page2kva(page) + page_off, copy_amt);
1159 page_decref(page); /* it's still in the cache, we just don't need it */
1161 assert(buf == buf_end);
1166 /* Write count bytes from buf to the file, starting at *offset, which is
1167 * increased accordingly, returning the number of bytes transfered. Most
1168 * filesystems will use this function for their f_op->write. Note, this uses
1171 * Changes don't get flushed to disc til there is an fsync, page cache eviction,
1172 * or other means of trying to writeback the pages. */
1173 ssize_t generic_file_write(struct file *file, const char *buf, size_t count,
1179 unsigned long first_idx, last_idx;
1181 const char *buf_end;
1183 /* Consider pushing some error checking higher in the VFS */
1186 /* Extend the file. Should put more checks in here, and maybe do this per
1187 * page in the for loop below. */
1188 if (*offset + count > file->f_dentry->d_inode->i_size)
1189 file->f_dentry->d_inode->i_size = *offset + count;
1190 page_off = *offset & (PGSIZE - 1);
1191 first_idx = *offset >> PGSHIFT;
1192 last_idx = (*offset + count) >> PGSHIFT;
1193 buf_end = buf + count;
1194 /* For each file page, make sure it's in the page cache, then write it.*/
1195 for (int i = first_idx; i <= last_idx; i++) {
1196 error = pm_load_page(file->f_mapping, i, &page);
1197 assert(!error); /* TODO: handle ENOMEM and friends */
1198 copy_amt = MIN(PGSIZE - page_off, buf_end - buf);
1199 /* TODO: (UMEM) (KFOP) think about this. if it's a user buffer, we're
1200 * relying on current to detect whose it is (which should work for async
1203 memcpy_from_user(current, page2kva(page) + page_off, buf, copy_amt);
1205 memcpy(page2kva(page) + page_off, buf, copy_amt);
1209 page_decref(page); /* it's still in the cache, we just don't need it */
1211 assert(buf == buf_end);
1216 /* Directories usually use this for their read method, which is the way glibc
1217 * currently expects us to do a readdir (short of doing linux's getdents). Will
1218 * probably need work, based on whatever real programs want. */
1219 ssize_t generic_dir_read(struct file *file, char *u_buf, size_t count,
1222 struct kdirent dir_r = {0}, *dirent = &dir_r;
1224 size_t amt_copied = 0;
1225 char *buf_end = u_buf + count;
1227 if (!S_ISDIR(file->f_dentry->d_inode->i_mode)) {
1233 /* start readdir from where it left off: */
1234 dirent->d_off = *offset;
1236 u_buf + sizeof(struct kdirent) <= buf_end;
1237 u_buf += sizeof(struct kdirent)) {
1238 /* TODO: UMEM/KFOP (pin the u_buf in the syscall, ditch the local copy,
1239 * get rid of this memcpy and reliance on current, etc). Might be
1240 * tricky with the dirent->d_off and trust issues */
1241 retval = file->f_op->readdir(file, dirent);
1246 /* Slight info exposure: could be extra crap after the name in the
1247 * dirent (like the name of a deleted file) */
1249 memcpy_to_user(current, u_buf, dirent, sizeof(struct dirent));
1251 memcpy(u_buf, dirent, sizeof(struct dirent));
1253 amt_copied += sizeof(struct dirent);
1254 /* 0 signals end of directory */
1258 /* Next time read is called, we pick up where we left off */
1259 *offset = dirent->d_off; /* UMEM */
1260 /* important to tell them how much they got. they often keep going til they
1261 * get 0 back (in the case of ls). it's also how much has been read, but it
1262 * isn't how much the f_pos has moved (which is opaque to the VFS). */
1266 /* Opens the file, using permissions from current for lack of a better option.
1267 * It will attempt to create the file if it does not exist and O_CREAT is
1268 * specified. This will return 0 on failure, and set errno. TODO: There's some
1269 * stuff that we don't do, esp related file truncating/creation. flags are for
1270 * opening, the mode is for creating. The flags related to how to create
1271 * (O_CREAT_FLAGS) are handled in this function, not in create_file().
1273 * It's tempting to split this into a do_file_create and a do_file_open, based
1274 * on the O_CREAT flag, but the O_CREAT flag can be ignored if the file exists
1275 * already and O_EXCL isn't specified. We could have open call create if it
1276 * fails, but for now we'll keep it as is. */
1277 struct file *do_file_open(char *path, int flags, int mode)
1279 struct file *file = 0;
1280 struct dentry *file_d;
1281 struct inode *parent_i;
1282 struct nameidata nd_r = {0}, *nd = &nd_r;
1285 /* The file might exist, lets try to just open it right away */
1286 nd->intent = LOOKUP_OPEN;
1287 error = path_lookup(path, LOOKUP_FOLLOW, nd);
1289 /* Still need to make sure we didn't want to O_EXCL create */
1290 if ((flags & O_CREAT) && (flags & O_EXCL)) {
1294 file_d = nd->dentry;
1295 kref_get(&file_d->d_kref, 1);
1298 /* So it didn't already exist, release the path from the previous lookup,
1299 * and then we try to create it. */
1301 /* get the parent, following links. this means you get the parent of the
1302 * final link (which may not be in 'path' in the first place. */
1303 nd->intent = LOOKUP_CREATE;
1304 error = path_lookup(path, LOOKUP_PARENT | LOOKUP_FOLLOW, nd);
1309 /* see if the target is there (shouldn't be), and handle accordingly */
1310 file_d = do_lookup(nd->dentry, nd->last.name);
1312 if (!(flags & O_CREAT)) {
1316 /* Create the inode/file. get a fresh dentry too: */
1317 file_d = get_dentry(nd->dentry->d_sb, nd->dentry, nd->last.name);
1318 parent_i = nd->dentry->d_inode;
1319 /* Note that the mode technically should only apply to future opens,
1320 * but we apply it immediately. */
1321 if (create_file(parent_i, file_d, mode)) /* sets errno */
1323 dcache_put(file_d->d_sb, file_d);
1324 } else { /* something already exists */
1325 /* this can happen due to concurrent access, but needs to be thought
1327 panic("File shouldn't be here!");
1328 if ((flags & O_CREAT) && (flags & O_EXCL)) {
1329 /* wanted to create, not open, bail out */
1335 /* now open the file (freshly created or if it already existed). At this
1336 * point, file_d is a refcnt'd dentry, regardless of which branch we took.*/
1337 if (flags & O_TRUNC)
1338 warn("File truncation not supported yet.");
1339 file = dentry_open(file_d, flags); /* sets errno */
1340 /* Note the fall through to the exit paths. File is 0 by default and if
1341 * dentry_open fails. */
1343 kref_put(&file_d->d_kref);
1349 /* Path is the location of the symlink, sometimes called the "new path", and
1350 * symname is who we link to, sometimes called the "old path". */
1351 int do_symlink(char *path, const char *symname, int mode)
1353 struct dentry *sym_d;
1354 struct inode *parent_i;
1355 struct nameidata nd_r = {0}, *nd = &nd_r;
1359 nd->intent = LOOKUP_CREATE;
1360 /* get the parent, but don't follow links */
1361 error = path_lookup(path, LOOKUP_PARENT, nd);
1366 /* see if the target is already there, handle accordingly */
1367 sym_d = do_lookup(nd->dentry, nd->last.name);
1372 /* Doesn't already exist, let's try to make it: */
1373 sym_d = get_dentry(nd->dentry->d_sb, nd->dentry, nd->last.name);
1378 parent_i = nd->dentry->d_inode;
1379 if (create_symlink(parent_i, sym_d, symname, mode))
1381 dcache_put(sym_d->d_sb, sym_d);
1382 retval = 0; /* Note the fall through to the exit paths */
1384 kref_put(&sym_d->d_kref);
1390 /* Makes a hard link for the file behind old_path to new_path */
1391 int do_link(char *old_path, char *new_path)
1393 struct dentry *link_d, *old_d;
1394 struct inode *inode, *parent_dir;
1395 struct nameidata nd_r = {0}, *nd = &nd_r;
1399 nd->intent = LOOKUP_CREATE;
1400 /* get the absolute parent of the new_path */
1401 error = path_lookup(new_path, LOOKUP_PARENT | LOOKUP_FOLLOW, nd);
1406 parent_dir = nd->dentry->d_inode;
1407 /* see if the new target is already there, handle accordingly */
1408 link_d = do_lookup(nd->dentry, nd->last.name);
1413 /* Doesn't already exist, let's try to make it. Still need to stitch it to
1414 * an inode and set its FS-specific stuff after this.*/
1415 link_d = get_dentry(nd->dentry->d_sb, nd->dentry, nd->last.name);
1420 /* Now let's get the old_path target */
1421 old_d = lookup_dentry(old_path, LOOKUP_FOLLOW);
1422 if (!old_d) /* errno set by lookup_dentry */
1424 /* For now, can only link to files */
1425 if (!S_ISREG(old_d->d_inode->i_mode)) {
1429 /* Must be on the same FS */
1430 if (old_d->d_sb != link_d->d_sb) {
1434 /* Do whatever FS specific stuff there is first (which is also a chance to
1436 error = parent_dir->i_op->link(old_d, parent_dir, link_d);
1441 /* Finally stitch it up */
1442 inode = old_d->d_inode;
1443 kref_get(&inode->i_kref, 1);
1444 link_d->d_inode = inode;
1446 TAILQ_INSERT_TAIL(&inode->i_dentry, link_d, d_alias); /* weak ref */
1447 dcache_put(link_d->d_sb, link_d);
1448 retval = 0; /* Note the fall through to the exit paths */
1450 kref_put(&old_d->d_kref);
1452 kref_put(&link_d->d_kref);
1458 /* Unlinks path from the directory tree. Read the Documentation for more info.
1460 int do_unlink(char *path)
1462 struct dentry *dentry;
1463 struct inode *parent_dir;
1464 struct nameidata nd_r = {0}, *nd = &nd_r;
1468 /* get the parent of the target, and don't follow a final link */
1469 error = path_lookup(path, LOOKUP_PARENT, nd);
1474 parent_dir = nd->dentry->d_inode;
1475 /* make sure the target is there */
1476 dentry = do_lookup(nd->dentry, nd->last.name);
1481 /* Make sure the target is not a directory */
1482 if (S_ISDIR(dentry->d_inode->i_mode)) {
1486 /* Remove the dentry from its parent */
1487 error = parent_dir->i_op->unlink(parent_dir, dentry);
1492 /* Now that our parent doesn't track us, we need to make sure we aren't
1493 * findable via the dentry cache. DYING, so we will be freed in
1494 * dentry_release() */
1495 dentry->d_flags |= DENTRY_DYING;
1496 dcache_remove(dentry->d_sb, dentry);
1497 dentry->d_inode->i_nlink--; /* TODO: race here, esp with a decref */
1498 /* At this point, the dentry is unlinked from the FS, and the inode has one
1499 * less link. When the in-memory objects (dentry, inode) are going to be
1500 * released (after all open files are closed, and maybe after entries are
1501 * evicted from the cache), then nlinks will get checked and the FS-file
1502 * will get removed from the disk */
1503 retval = 0; /* Note the fall through to the exit paths */
1505 kref_put(&dentry->d_kref);
1511 /* Checks to see if path can be accessed via mode. Need to actually send the
1512 * mode along somehow, so this doesn't do much now. This is an example of
1513 * decent error propagation from the lower levels via int retvals. */
1514 int do_access(char *path, int mode)
1516 struct nameidata nd_r = {0}, *nd = &nd_r;
1518 nd->intent = LOOKUP_ACCESS;
1519 retval = path_lookup(path, 0, nd);
1524 int do_chmod(char *path, int mode)
1526 struct nameidata nd_r = {0}, *nd = &nd_r;
1528 retval = path_lookup(path, 0, nd);
1531 /* TODO: when we have notions of uid, check for the proc's uid */
1532 if (nd->dentry->d_inode->i_uid != UID_OF_ME)
1536 nd->dentry->d_inode->i_mode |= mode & S_PMASK;
1542 /* Make a directory at path with mode. Returns -1 and sets errno on errors */
1543 int do_mkdir(char *path, int mode)
1545 struct dentry *dentry;
1546 struct inode *parent_i;
1547 struct nameidata nd_r = {0}, *nd = &nd_r;
1551 nd->intent = LOOKUP_CREATE;
1552 /* get the parent, but don't follow links */
1553 error = path_lookup(path, LOOKUP_PARENT, nd);
1558 /* see if the target is already there, handle accordingly */
1559 dentry = do_lookup(nd->dentry, nd->last.name);
1564 /* Doesn't already exist, let's try to make it: */
1565 dentry = get_dentry(nd->dentry->d_sb, nd->dentry, nd->last.name);
1570 parent_i = nd->dentry->d_inode;
1571 if (create_dir(parent_i, dentry, mode))
1573 dcache_put(dentry->d_sb, dentry);
1574 retval = 0; /* Note the fall through to the exit paths */
1576 kref_put(&dentry->d_kref);
1582 int do_rmdir(char *path)
1584 struct dentry *dentry;
1585 struct inode *parent_i;
1586 struct nameidata nd_r = {0}, *nd = &nd_r;
1590 /* get the parent, following links (probably want this), and we must get a
1591 * directory. Note, current versions of path_lookup can't handle both
1592 * PARENT and DIRECTORY, at least, it doesn't check that *path is a
1594 error = path_lookup(path, LOOKUP_PARENT | LOOKUP_FOLLOW | LOOKUP_DIRECTORY,
1600 /* make sure the target is already there, handle accordingly */
1601 dentry = do_lookup(nd->dentry, nd->last.name);
1606 if (!S_ISDIR(dentry->d_inode->i_mode)) {
1610 if (dentry->d_mount_point) {
1614 /* TODO: make sure we aren't a mount or processes root (EBUSY) */
1615 /* Now for the removal. the FSs will check if they are empty */
1616 parent_i = nd->dentry->d_inode;
1617 error = parent_i->i_op->rmdir(parent_i, dentry);
1622 /* Now that our parent doesn't track us, we need to make sure we aren't
1623 * findable via the dentry cache. DYING, so we will be freed in
1624 * dentry_release() */
1625 dentry->d_flags |= DENTRY_DYING;
1626 dcache_remove(dentry->d_sb, dentry);
1627 /* Decref ourselves, so inode_release() knows we are done */
1628 dentry->d_inode->i_nlink--;
1629 TAILQ_REMOVE(&nd->dentry->d_subdirs, dentry, d_subdirs_link);
1630 parent_i->i_nlink--; /* TODO: race on this, esp since its a decref */
1631 /* we still have d_parent and a kref on our parent, which will go away when
1632 * the in-memory dentry object goes away. */
1633 retval = 0; /* Note the fall through to the exit paths */
1635 kref_put(&dentry->d_kref);
1641 struct file *alloc_file(void)
1643 struct file *file = kmem_cache_alloc(file_kcache, 0);
1648 /* one for the ref passed out*/
1649 kref_init(&file->f_kref, file_release, 1);
1653 /* Opens and returns the file specified by dentry */
1654 struct file *dentry_open(struct dentry *dentry, int flags)
1656 struct inode *inode;
1659 inode = dentry->d_inode;
1660 /* Do the mode first, since we can still error out. f_mode stores how the
1661 * OS file is open, which can be more restrictive than the i_mode */
1662 switch (flags & (O_RDONLY | O_WRONLY | O_RDWR)) {
1664 desired_mode = S_IRUSR;
1667 desired_mode = S_IWUSR;
1670 desired_mode = S_IRUSR | S_IWUSR;
1675 if (check_perms(inode, desired_mode))
1677 file = alloc_file();
1680 file->f_mode = desired_mode;
1681 /* Add to the list of all files of this SB */
1682 TAILQ_INSERT_TAIL(&inode->i_sb->s_files, file, f_list);
1683 kref_get(&dentry->d_kref, 1);
1684 file->f_dentry = dentry;
1685 kref_get(&inode->i_sb->s_mount->mnt_kref, 1);
1686 file->f_vfsmnt = inode->i_sb->s_mount; /* saving a ref to the vmnt...*/
1687 file->f_op = inode->i_fop;
1688 /* Don't store open mode or creation flags */
1689 file->f_flags = flags & ~(O_ACCMODE | O_CREAT_FLAGS);
1691 file->f_uid = inode->i_uid;
1692 file->f_gid = inode->i_gid;
1694 // struct event_poll_tailq f_ep_links;
1695 spinlock_init(&file->f_ep_lock);
1696 file->f_privdata = 0; /* prob overriden by the fs */
1697 file->f_mapping = inode->i_mapping;
1698 file->f_op->open(inode, file);
1705 /* Closes a file, fsync, whatever else is necessary. Called when the kref hits
1706 * 0. Note that the file is not refcounted on the s_files list, nor is the
1707 * f_mapping refcounted (it is pinned by the i_mapping). */
1708 void file_release(struct kref *kref)
1710 struct file *file = container_of(kref, struct file, f_kref);
1712 struct super_block *sb = file->f_dentry->d_sb;
1713 spin_lock(&sb->s_lock);
1714 TAILQ_REMOVE(&sb->s_files, file, f_list);
1715 spin_unlock(&sb->s_lock);
1717 /* TODO: fsync (BLK). also, we may want to parallelize the blocking that
1718 * could happen in here (spawn kernel threads)... */
1719 file->f_op->release(file->f_dentry->d_inode, file);
1720 /* Clean up the other refs we hold */
1721 kref_put(&file->f_dentry->d_kref);
1722 kref_put(&file->f_vfsmnt->mnt_kref);
1723 kmem_cache_free(file_kcache, file);
1726 /* Process-related File management functions */
1728 /* Given any FD, get the appropriate file, 0 o/w */
1729 struct file *get_file_from_fd(struct files_struct *open_files, int file_desc)
1731 struct file *retval = 0;
1734 spin_lock(&open_files->lock);
1735 if (file_desc < open_files->max_fdset) {
1736 if (GET_BITMASK_BIT(open_files->open_fds->fds_bits, file_desc)) {
1737 /* while max_files and max_fdset might not line up, we should never
1738 * have a valid fdset higher than files */
1739 assert(file_desc < open_files->max_files);
1740 retval = open_files->fd[file_desc].fd_file;
1742 kref_get(&retval->f_kref, 1);
1745 spin_unlock(&open_files->lock);
1749 /* Remove FD from the open files, if it was there, and return f. Currently,
1750 * this decref's f, so the return value is not consumable or even usable. This
1751 * hasn't been thought through yet. */
1752 struct file *put_file_from_fd(struct files_struct *open_files, int file_desc)
1754 struct file *file = 0;
1757 spin_lock(&open_files->lock);
1758 if (file_desc < open_files->max_fdset) {
1759 if (GET_BITMASK_BIT(open_files->open_fds->fds_bits, file_desc)) {
1760 /* while max_files and max_fdset might not line up, we should never
1761 * have a valid fdset higher than files */
1762 assert(file_desc < open_files->max_files);
1763 file = open_files->fd[file_desc].fd_file;
1764 open_files->fd[file_desc].fd_file = 0;
1766 kref_put(&file->f_kref);
1767 CLR_BITMASK_BIT(open_files->open_fds->fds_bits, file_desc);
1770 spin_unlock(&open_files->lock);
1773 /* Inserts the file in the files_struct, returning the corresponding new file
1774 * descriptor, or an error code. We start looking for open fds from low_fd. */
1775 int insert_file(struct files_struct *open_files, struct file *file, int low_fd)
1778 if ((low_fd < 0) || (low_fd > NR_FILE_DESC_MAX))
1780 spin_lock(&open_files->lock);
1781 for (int i = low_fd; i < open_files->max_fdset; i++) {
1782 if (GET_BITMASK_BIT(open_files->open_fds->fds_bits, i))
1785 SET_BITMASK_BIT(open_files->open_fds->fds_bits, slot);
1786 assert(slot < open_files->max_files &&
1787 open_files->fd[slot].fd_file == 0);
1788 kref_get(&file->f_kref, 1);
1789 open_files->fd[slot].fd_file = file;
1790 open_files->fd[slot].fd_flags = 0;
1791 if (slot >= open_files->next_fd)
1792 open_files->next_fd = slot + 1;
1795 if (slot == -1) /* should expand the FD array and fd_set */
1796 warn("Ran out of file descriptors, deal with me!");
1797 spin_unlock(&open_files->lock);
1801 /* Closes all open files. Mostly just a "put" for all files. If cloexec, it
1802 * will only close files that are opened with O_CLOEXEC. */
1803 void close_all_files(struct files_struct *open_files, bool cloexec)
1806 spin_lock(&open_files->lock);
1807 for (int i = 0; i < open_files->max_fdset; i++) {
1808 if (GET_BITMASK_BIT(open_files->open_fds->fds_bits, i)) {
1809 /* while max_files and max_fdset might not line up, we should never
1810 * have a valid fdset higher than files */
1811 assert(i < open_files->max_files);
1812 file = open_files->fd[i].fd_file;
1813 if (cloexec && !(open_files->fd[i].fd_flags & O_CLOEXEC))
1815 /* Actually close the file */
1816 open_files->fd[i].fd_file = 0;
1818 kref_put(&file->f_kref);
1819 CLR_BITMASK_BIT(open_files->open_fds->fds_bits, i);
1822 spin_unlock(&open_files->lock);
1825 /* Inserts all of the files from src into dst, used by sys_fork(). */
1826 void clone_files(struct files_struct *src, struct files_struct *dst)
1829 spin_lock(&src->lock);
1830 spin_lock(&dst->lock);
1831 for (int i = 0; i < src->max_fdset; i++) {
1832 if (GET_BITMASK_BIT(src->open_fds->fds_bits, i)) {
1833 /* while max_files and max_fdset might not line up, we should never
1834 * have a valid fdset higher than files */
1835 assert(i < src->max_files);
1836 file = src->fd[i].fd_file;
1837 assert(i < dst->max_files && dst->fd[i].fd_file == 0);
1838 SET_BITMASK_BIT(dst->open_fds->fds_bits, i);
1839 dst->fd[i].fd_file = file;
1841 kref_get(&file->f_kref, 1);
1842 if (i >= dst->next_fd)
1843 dst->next_fd = i + 1;
1846 spin_unlock(&dst->lock);
1847 spin_unlock(&src->lock);
1850 /* Change the working directory of the given fs env (one per process, at this
1851 * point). Returns 0 for success, -ERROR for whatever error. */
1852 int do_chdir(struct fs_struct *fs_env, char *path)
1854 struct nameidata nd_r = {0}, *nd = &nd_r;
1856 retval = path_lookup(path, LOOKUP_DIRECTORY, nd);
1858 /* nd->dentry is the place we want our PWD to be */
1859 kref_get(&nd->dentry->d_kref, 1);
1860 kref_put(&fs_env->pwd->d_kref);
1861 fs_env->pwd = nd->dentry;
1867 /* Returns a null-terminated string of up to length cwd_l containing the
1868 * absolute path of fs_env, (up to fs_env's root). Be sure to kfree the char*
1869 * "kfree_this" when you are done with it. We do this since it's easier to
1870 * build this string going backwards. Note cwd_l is not a strlen, it's an
1872 char *do_getcwd(struct fs_struct *fs_env, char **kfree_this, size_t cwd_l)
1874 struct dentry *dentry = fs_env->pwd;
1876 char *path_start, *kbuf;
1882 kbuf = kmalloc(cwd_l, 0);
1888 kbuf[cwd_l - 1] = '\0';
1889 kbuf[cwd_l - 2] = '/';
1890 /* for each dentry in the path, all the way back to the root of fs_env, we
1891 * grab the dentry name, push path_start back enough, and write in the name,
1892 * using /'s to terminate. We skip the root, since we don't want it's
1893 * actual name, just "/", which is set before each loop. */
1894 path_start = kbuf + cwd_l - 2; /* the last byte written */
1895 while (dentry != fs_env->root) {
1896 link_len = dentry->d_name.len; /* this does not count the \0 */
1897 if (path_start - (link_len + 2) < kbuf) {
1902 path_start -= link_len + 1; /* the 1 is for the \0 */
1903 strncpy(path_start, dentry->d_name.name, link_len);
1906 dentry = dentry->d_parent;
1911 static void print_dir(struct dentry *dentry, char *buf, int depth)
1913 struct dentry *child_d;
1914 struct dirent next = {0};
1918 if (!S_ISDIR(dentry->d_inode->i_mode)) {
1919 warn("Thought this was only directories!!");
1922 /* Print this dentry */
1923 printk("%s%s/ nlink: %d\n", buf, dentry->d_name.name,
1924 dentry->d_inode->i_nlink);
1925 if (dentry->d_mount_point) {
1926 dentry = dentry->d_mounted_fs->mnt_root;
1930 /* Set buffer for our kids */
1932 dir = dentry_open(dentry, 0);
1934 panic("Filesystem seems inconsistent - unable to open a dir!");
1935 /* Process every child, recursing on directories */
1937 retval = dir->f_op->readdir(dir, &next);
1939 /* Skip .., ., and empty entries */
1940 if (!strcmp("..", next.d_name) || !strcmp(".", next.d_name) ||
1943 /* there is an entry, now get its dentry */
1944 child_d = do_lookup(dentry, next.d_name);
1946 panic("Inconsistent FS, dirent doesn't have a dentry!");
1947 /* Recurse for directories, or just print the name for others */
1948 switch (child_d->d_inode->i_mode & __S_IFMT) {
1950 print_dir(child_d, buf, depth + 1);
1953 printk("%s%s size(B): %d nlink: %d\n", buf, next.d_name,
1954 child_d->d_inode->i_size, child_d->d_inode->i_nlink);
1957 printk("%s%s -> %s\n", buf, next.d_name,
1958 child_d->d_inode->i_op->readlink(child_d));
1961 printk("%s%s (char device) nlink: %d\n", buf, next.d_name,
1962 child_d->d_inode->i_nlink);
1965 printk("%s%s (block device) nlink: %d\n", buf, next.d_name,
1966 child_d->d_inode->i_nlink);
1969 warn("Look around you! Unknown filetype!");
1971 kref_put(&child_d->d_kref);
1977 /* Reset buffer to the way it was */
1979 kref_put(&dir->f_kref);
1983 int ls_dash_r(char *path)
1985 struct nameidata nd_r = {0}, *nd = &nd_r;
1989 error = path_lookup(path, LOOKUP_ACCESS | LOOKUP_DIRECTORY, nd);
1994 print_dir(nd->dentry, buf, 0);