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
44 * now). fields related to the actual FS, like the sb and the mnt_root are
45 * set in 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);
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);
143 warn("OOM in do_lookup(), probably wasn't expected\n");
146 result = dcache_get(parent->d_sb, query);
148 __dentry_free(query);
151 /* No result, check for negative */
152 if (query->d_flags & DENTRY_NEGATIVE) {
153 __dentry_free(query);
156 /* not in the dcache at all, need to consult the FS */
157 result = parent->d_inode->i_op->lookup(parent->d_inode, query, 0);
159 /* Note the USED flag will get turned off when this gets added to the
160 * LRU in dentry_release(). There's a slight race here that we'll panic
161 * on, but I want to catch it (in dcache_put()) for now. */
162 query->d_flags |= DENTRY_NEGATIVE;
163 dcache_put(parent->d_sb, query);
164 kref_put(&query->d_kref);
167 dcache_put(parent->d_sb, result);
168 /* This is because KFS doesn't return the same dentry, but ext2 does. this
169 * is ugly and needs to be fixed. (TODO) */
171 __dentry_free(query);
173 /* TODO: if the following are done by us, how do we know the i_ino?
174 * also need to handle inodes that are already read in! For now, we're
175 * going to have the FS handle it in it's lookup() method:
177 * - read in the inode
178 * - put in the inode cache */
182 /* Update ND such that it represents having followed dentry. IAW the nd
183 * refcnting rules, we need to decref any references that were in there before
184 * they get clobbered. */
185 static int next_link(struct dentry *dentry, struct nameidata *nd)
187 assert(nd->dentry && nd->mnt);
188 /* update the dentry */
189 kref_get(&dentry->d_kref, 1);
190 kref_put(&nd->dentry->d_kref);
192 /* update the mount, if we need to */
193 if (dentry->d_sb->s_mount != nd->mnt) {
194 kref_get(&dentry->d_sb->s_mount->mnt_kref, 1);
195 kref_put(&nd->mnt->mnt_kref);
196 nd->mnt = dentry->d_sb->s_mount;
201 /* Walk up one directory, being careful of mountpoints, namespaces, and the top
203 static int climb_up(struct nameidata *nd)
205 printd("CLIMB_UP, from %s\n", nd->dentry->d_name.name);
206 /* Top of the world, just return. Should also check for being at the top of
207 * the current process's namespace (TODO) */
208 if (!nd->dentry->d_parent || (nd->dentry->d_parent == nd->dentry))
210 /* Check if we are at the top of a mount, if so, we need to follow
211 * backwards, and then climb_up from that one. We might need to climb
212 * multiple times if we mount multiple FSs at the same spot (highly
213 * unlikely). This is completely untested. Might recurse instead. */
214 while (nd->mnt->mnt_root == nd->dentry) {
215 if (!nd->mnt->mnt_parent) {
216 warn("Might have expected a parent vfsmount (dentry had a parent)");
219 next_link(nd->mnt->mnt_mountpoint, nd);
221 /* Backwards walk (no mounts or any other issues now). */
222 next_link(nd->dentry->d_parent, nd);
223 printd("CLIMB_UP, to %s\n", nd->dentry->d_name.name);
227 /* nd->dentry might be on a mount point, so we need to move on to the child
229 static int follow_mount(struct nameidata *nd)
231 if (!nd->dentry->d_mount_point)
233 next_link(nd->dentry->d_mounted_fs->mnt_root, nd);
237 static int link_path_walk(char *path, struct nameidata *nd);
239 /* When nd->dentry is for a symlink, this will recurse and follow that symlink,
240 * so that nd contains the results of following the symlink (dentry and mnt).
241 * Returns when it isn't a symlink, 1 on following a link, and < 0 on error. */
242 static int follow_symlink(struct nameidata *nd)
246 if (!S_ISLNK(nd->dentry->d_inode->i_mode))
248 if (nd->depth > MAX_SYMLINK_DEPTH)
250 printd("Following symlink for dentry %p %s\n", nd->dentry,
251 nd->dentry->d_name.name);
253 symname = nd->dentry->d_inode->i_op->readlink(nd->dentry);
254 /* We need to pin in nd->dentry (the dentry of the symlink), since we need
255 * it's symname's storage to stay in memory throughout the upcoming
256 * link_path_walk(). The last_sym gets decreffed when we path_release() or
257 * follow another symlink. */
259 kref_put(&nd->last_sym->d_kref);
260 kref_get(&nd->dentry->d_kref, 1);
261 nd->last_sym = nd->dentry;
262 /* If this an absolute path in the symlink, we need to free the old path and
263 * start over, otherwise, we continue from the PARENT of nd (the symlink) */
264 if (symname[0] == '/') {
267 nd->dentry = default_ns.root->mnt_root;
269 nd->dentry = current->fs_env.root;
270 nd->mnt = nd->dentry->d_sb->s_mount;
271 kref_get(&nd->mnt->mnt_kref, 1);
272 kref_get(&nd->dentry->d_kref, 1);
276 /* either way, keep on walking in the free world! */
277 retval = link_path_walk(symname, nd);
278 return (retval == 0 ? 1 : retval);
281 /* Little helper, to make it easier to break out of the nested loops. Will also
282 * '\0' out the first slash if it's slashes all the way down. Or turtles. */
283 static bool packed_trailing_slashes(char *first_slash)
285 for (char *i = first_slash; *i == '/'; i++) {
286 if (*(i + 1) == '\0') {
294 /* Simple helper to set nd to track it's last name to be Name. Also be careful
295 * with the storage of name. Don't use and nd's name past the lifetime of the
296 * string used in the path_lookup()/link_path_walk/whatever. Consider replacing
297 * parts of this with a qstr builder. Note this uses the dentry's d_op, which
298 * might not be the dentry we care about. */
299 static void stash_nd_name(struct nameidata *nd, char *name)
301 nd->last.name = name;
302 nd->last.len = strlen(name);
303 nd->last.hash = nd->dentry->d_op->d_hash(nd->dentry, &nd->last);
306 /* Resolves the links in a basic path walk. 0 for success, -EWHATEVER
307 * otherwise. The final lookup is returned via nd. */
308 static int link_path_walk(char *path, struct nameidata *nd)
310 struct dentry *link_dentry;
311 struct inode *link_inode, *nd_inode;
316 /* Prevent crazy recursion */
317 if (nd->depth > MAX_SYMLINK_DEPTH)
319 /* skip all leading /'s */
322 /* if there's nothing left (null terminated), we're done. This should only
323 * happen for "/", which if we wanted a PARENT, should fail (there is no
326 if (nd->flags & LOOKUP_PARENT) {
330 /* o/w, we're good */
333 /* iterate through each intermediate link of the path. in general, nd
334 * tracks where we are in the path, as far as dentries go. once we have the
335 * next dentry, we try to update nd based on that dentry. link is the part
336 * of the path string that we are looking up */
338 nd_inode = nd->dentry->d_inode;
339 if ((error = check_perms(nd_inode, nd->intent)))
341 /* find the next link, break out if it is the end */
342 next_slash = strchr(link, '/');
346 if (packed_trailing_slashes(next_slash)) {
347 nd->flags |= LOOKUP_DIRECTORY;
351 /* skip over any interim ./ */
352 if (!strncmp("./", link, 2))
354 /* Check for "../", walk up */
355 if (!strncmp("../", link, 3)) {
360 link_dentry = do_lookup(nd->dentry, link);
364 /* make link_dentry the current step/answer */
365 next_link(link_dentry, nd);
366 kref_put(&link_dentry->d_kref); /* do_lookup gave us a refcnt dentry */
367 /* we could be on a mountpoint or a symlink - need to follow them */
369 if ((error = follow_symlink(nd)) < 0)
371 /* Turn off a possible DIRECTORY lookup, which could have been set
372 * during the follow_symlink (a symlink could have had a directory at
373 * the end), though it was in the middle of the real path. */
374 nd->flags &= ~LOOKUP_DIRECTORY;
375 if (!S_ISDIR(nd->dentry->d_inode->i_mode))
378 /* move through the path string to the next entry */
379 link = next_slash + 1;
380 /* advance past any other interim slashes. we know we won't hit the end
381 * due to the for loop check above */
385 /* Now, we're on the last link of the path. We need to deal with with . and
386 * .. . This might be weird with PARENT lookups - not sure what semantics
387 * we want exactly. This will give the parent of whatever the PATH was
388 * supposed to look like. Note that ND currently points to the parent of
389 * the last item (link). */
390 if (!strcmp(".", link)) {
391 if (nd->flags & LOOKUP_PARENT) {
392 assert(nd->dentry->d_name.name);
393 stash_nd_name(nd, nd->dentry->d_name.name);
398 if (!strcmp("..", link)) {
400 if (nd->flags & LOOKUP_PARENT) {
401 assert(nd->dentry->d_name.name);
402 stash_nd_name(nd, nd->dentry->d_name.name);
407 /* need to attempt to look it up, in case it's a symlink */
408 link_dentry = do_lookup(nd->dentry, link);
410 /* if there's no dentry, we are okay if we are looking for the parent */
411 if (nd->flags & LOOKUP_PARENT) {
412 assert(strcmp(link, ""));
413 stash_nd_name(nd, link);
419 next_link(link_dentry, nd);
420 kref_put(&link_dentry->d_kref); /* do_lookup gave us a refcnt'd dentry */
421 /* at this point, nd is on the final link, but it might be a symlink */
422 if (nd->flags & LOOKUP_FOLLOW) {
423 error = follow_symlink(nd);
426 /* if we actually followed a symlink, then nd is set and we're done */
430 /* One way or another, nd is on the last element of the path, symlinks and
431 * all. Now we need to climb up to set nd back on the parent, if that's
433 if (nd->flags & LOOKUP_PARENT) {
434 assert(nd->dentry->d_name.name);
435 stash_nd_name(nd, link_dentry->d_name.name);
439 /* now, we have the dentry set, and don't want the parent, but might be on a
440 * mountpoint still. FYI: this hasn't been thought through completely. */
442 /* If we wanted a directory, but didn't get one, error out */
443 if ((nd->flags & LOOKUP_DIRECTORY) && !S_ISDIR(nd->dentry->d_inode->i_mode))
448 /* Given path, return the inode for the final dentry. The ND should be
449 * initialized for the first call - specifically, we need the intent.
450 * LOOKUP_PARENT and friends go in the flags var, which is not the intent.
452 * If path_lookup wants a PARENT, but hits the top of the FS (root or
453 * otherwise), we want it to error out. It's still unclear how we want to
454 * handle processes with roots that aren't root, but at the very least, we don't
455 * want to think we have the parent of /, but have / itself. Due to the way
456 * link_path_walk works, if that happened, we probably don't have a
457 * nd->last.name. This needs more thought (TODO).
459 * Need to be careful too. While the path has been copied-in to the kernel,
460 * it's still user input. */
461 int path_lookup(char *path, int flags, struct nameidata *nd)
464 printd("Path lookup for %s\n", path);
465 /* we allow absolute lookups with no process context */
466 /* TODO: RCU read lock on pwd or kref_not_zero in a loop. concurrent chdir
467 * could decref nd->dentry before we get to incref it below. */
468 if (path[0] == '/') { /* absolute lookup */
470 nd->dentry = default_ns.root->mnt_root;
472 nd->dentry = current->fs_env.root;
473 } else { /* relative lookup */
475 /* Don't need to lock on the fs_env since we're reading one item */
476 nd->dentry = current->fs_env.pwd;
478 nd->mnt = nd->dentry->d_sb->s_mount;
479 /* Whenever references get put in the nd, incref them. Whenever they are
480 * removed, decref them. */
481 kref_get(&nd->mnt->mnt_kref, 1);
482 kref_get(&nd->dentry->d_kref, 1);
484 nd->depth = 0; /* used in symlink following */
485 retval = link_path_walk(path, nd);
486 /* make sure our PARENT lookup worked */
487 if (!retval && (flags & LOOKUP_PARENT))
488 assert(nd->last.name);
492 /* Call this after any use of path_lookup when you are done with its results,
493 * regardless of whether it succeeded or not. It will free any references */
494 void path_release(struct nameidata *nd)
496 kref_put(&nd->dentry->d_kref);
497 kref_put(&nd->mnt->mnt_kref);
498 /* Free the last symlink dentry used, if there was one */
500 kref_put(&nd->last_sym->d_kref);
501 nd->last_sym = 0; /* catch reuse bugs */
505 /* External version of mount, only call this after having a / mount */
506 int mount_fs(struct fs_type *fs, char *dev_name, char *path, int flags)
508 struct nameidata nd_r = {0}, *nd = &nd_r;
510 retval = path_lookup(path, LOOKUP_DIRECTORY, nd);
513 /* taking the namespace of the vfsmount of path */
514 if (!__mount_fs(fs, dev_name, nd->dentry, flags, nd->mnt->mnt_namespace))
521 /* Superblock functions */
523 /* Dentry "hash" function for the hash table to use. Since we already have the
524 * hash in the qstr, we don't need to rehash. Also, note we'll be using the
525 * dentry in question as both the key and the value. */
526 static size_t __dcache_hash(void *k)
528 return (size_t)((struct dentry*)k)->d_name.hash;
531 /* Dentry cache hashtable equality function. This means we need to pass in some
532 * minimal dentry when doing a lookup. */
533 static ssize_t __dcache_eq(void *k1, void *k2)
535 if (((struct dentry*)k1)->d_parent != ((struct dentry*)k2)->d_parent)
537 /* TODO: use the FS-specific string comparison */
538 return !strcmp(((struct dentry*)k1)->d_name.name,
539 ((struct dentry*)k2)->d_name.name);
542 /* Helper to alloc and initialize a generic superblock. This handles all the
543 * VFS related things, like lists. Each FS will need to handle its own things
544 * in it's *_get_sb(), usually involving reading off the disc. */
545 struct super_block *get_sb(void)
547 struct super_block *sb = kmalloc(sizeof(struct super_block), 0);
549 spinlock_init(&sb->s_lock);
550 kref_init(&sb->s_kref, fake_release, 1); /* for the ref passed out */
551 TAILQ_INIT(&sb->s_inodes);
552 TAILQ_INIT(&sb->s_dirty_i);
553 TAILQ_INIT(&sb->s_io_wb);
554 TAILQ_INIT(&sb->s_lru_d);
555 TAILQ_INIT(&sb->s_files);
556 sb->s_dcache = create_hashtable(100, __dcache_hash, __dcache_eq);
557 sb->s_icache = create_hashtable(100, __generic_hash, __generic_eq);
558 spinlock_init(&sb->s_lru_lock);
559 spinlock_init(&sb->s_dcache_lock);
560 spinlock_init(&sb->s_icache_lock);
561 sb->s_fs_info = 0; // can override somewhere else
565 /* Final stages of initializing a super block, including creating and linking
566 * the root dentry, root inode, vmnt, and sb. The d_op and root_ino are
567 * FS-specific, but otherwise it's FS-independent, tricky, and not worth having
568 * around multiple times.
570 * Not the world's best interface, so it's subject to change, esp since we're
571 * passing (now 3) FS-specific things. */
572 void init_sb(struct super_block *sb, struct vfsmount *vmnt,
573 struct dentry_operations *d_op, unsigned long root_ino,
576 /* Build and init the first dentry / inode. The dentry ref is stored later
577 * by vfsmount's mnt_root. The parent is dealt with later. */
578 struct dentry *d_root = get_dentry_with_ops(sb, 0, "/", d_op);
581 panic("OOM! init_sb() can't fail yet!");
582 /* a lot of here on down is normally done in lookup() or create, since
583 * get_dentry isn't a fully usable dentry. The two FS-specific settings are
584 * normally inherited from a parent within the same FS in get_dentry, but we
587 d_root->d_fs_info = d_fs_info;
588 struct inode *inode = get_inode(d_root);
590 panic("This FS sucks!");
591 inode->i_ino = root_ino;
592 /* TODO: add the inode to the appropriate list (off i_list) */
593 /* TODO: do we need to read in the inode? can we do this on demand? */
594 /* if this FS is already mounted, we'll need to do something different. */
595 sb->s_op->read_inode(inode);
596 icache_put(sb, inode);
597 /* Link the dentry and SB to the VFS mount */
598 vmnt->mnt_root = d_root; /* ref comes from get_dentry */
600 /* If there is no mount point, there is no parent. This is true only for
602 if (vmnt->mnt_mountpoint) {
603 kref_get(&vmnt->mnt_mountpoint->d_kref, 1); /* held by d_root */
604 d_root->d_parent = vmnt->mnt_mountpoint; /* dentry of the root */
606 d_root->d_parent = d_root; /* set root as its own parent */
608 /* insert the dentry into the dentry cache. when's the earliest we can?
609 * when's the earliest we should? what about concurrent accesses to the
610 * same dentry? should be locking the dentry... */
611 dcache_put(sb, d_root);
612 kref_put(&inode->i_kref); /* give up the ref from get_inode() */
615 /* Dentry Functions */
617 static void dentry_set_name(struct dentry *dentry, char *name)
619 size_t name_len = strnlen(name, MAX_FILENAME_SZ); /* not including \0! */
621 if (name_len < DNAME_INLINE_LEN) {
622 strncpy(dentry->d_iname, name, name_len);
623 dentry->d_iname[name_len] = '\0';
624 qstr_builder(dentry, 0);
626 l_name = kmalloc(name_len + 1, 0);
628 strncpy(l_name, name, name_len);
629 l_name[name_len] = '\0';
630 qstr_builder(dentry, l_name);
634 /* Gets a dentry. If there is no parent, use d_op. Only called directly by
635 * superblock init code. */
636 struct dentry *get_dentry_with_ops(struct super_block *sb,
637 struct dentry *parent, char *name,
638 struct dentry_operations *d_op)
641 struct dentry *dentry = kmem_cache_alloc(dentry_kcache, 0);
647 //memset(dentry, 0, sizeof(struct dentry));
648 kref_init(&dentry->d_kref, dentry_release, 1); /* this ref is returned */
649 spinlock_init(&dentry->d_lock);
650 TAILQ_INIT(&dentry->d_subdirs);
652 kref_get(&sb->s_kref, 1);
653 dentry->d_sb = sb; /* storing a ref here... */
654 dentry->d_mount_point = FALSE;
655 dentry->d_mounted_fs = 0;
656 if (parent) { /* no parent for rootfs mount */
657 kref_get(&parent->d_kref, 1);
658 dentry->d_op = parent->d_op; /* d_op set in init_sb for parentless */
662 dentry->d_parent = parent;
663 dentry->d_flags = DENTRY_USED;
664 dentry->d_fs_info = 0;
665 dentry_set_name(dentry, name);
666 /* Catch bugs by aggressively zeroing this (o/w we use old stuff) */
671 /* Helper to alloc and initialize a generic dentry. The following needs to be
672 * set still: d_op (if no parent), d_fs_info (opt), d_inode, connect the inode
673 * to the dentry (and up the d_kref again), maybe dcache_put(). The inode
674 * stitching is done in get_inode() or lookup (depending on the FS).
675 * The setting of the d_op might be problematic when dealing with mounts. Just
678 * If the name is longer than the inline name, it will kmalloc a buffer, so
679 * don't worry about the storage for *name after calling this. */
680 struct dentry *get_dentry(struct super_block *sb, struct dentry *parent,
683 return get_dentry_with_ops(sb, parent, name, 0);
686 /* Called when the dentry is unreferenced (after kref == 0). This works closely
687 * with the resurrection in dcache_get().
689 * The dentry is still in the dcache, but needs to be un-USED and added to the
690 * LRU dentry list. Even dentries that were used in a failed lookup need to be
691 * cached - they ought to be the negative dentries. Note that all dentries have
692 * parents, even negative ones (it is needed to find it in the dcache). */
693 void dentry_release(struct kref *kref)
695 struct dentry *dentry = container_of(kref, struct dentry, d_kref);
697 printd("'Releasing' dentry %p: %s\n", dentry, dentry->d_name.name);
698 /* DYING dentries (recently unlinked / rmdir'd) just get freed */
699 if (dentry->d_flags & DENTRY_DYING) {
700 __dentry_free(dentry);
703 /* This lock ensures the USED state and the TAILQ membership is in sync.
704 * Also used to check the refcnt, though that might not be necessary. */
705 spin_lock(&dentry->d_lock);
706 /* While locked, we need to double check the kref, in case someone already
707 * reup'd it. Re-up? you're crazy! Reee-up, you're outta yo mind! */
708 if (!kref_refcnt(&dentry->d_kref)) {
709 /* Note this is where negative dentries get set UNUSED */
710 if (dentry->d_flags & DENTRY_USED) {
711 dentry->d_flags &= ~DENTRY_USED;
712 spin_lock(&dentry->d_sb->s_lru_lock);
713 TAILQ_INSERT_TAIL(&dentry->d_sb->s_lru_d, dentry, d_lru);
714 spin_unlock(&dentry->d_sb->s_lru_lock);
716 /* and make sure it wasn't USED, then UNUSED again */
717 /* TODO: think about issues with this */
718 warn("This should be rare. Tell brho this happened.");
721 spin_unlock(&dentry->d_lock);
724 /* Called when we really dealloc and get rid of a dentry (like when it is
725 * removed from the dcache, either for memory or correctness reasons)
727 * This has to handle two types of dentries: full ones (ones that had been used)
728 * and ones that had been just for lookups - hence the check for d_inode.
730 * Note that dentries pin and kref their inodes. When all the dentries are
731 * gone, we want the inode to be released via kref. The inode has internal /
732 * weak references to the dentry, which are not refcounted. */
733 void __dentry_free(struct dentry *dentry)
736 printd("Freeing dentry %p: %s\n", dentry, dentry->d_name.name);
737 assert(dentry->d_op); /* catch bugs. a while back, some lacked d_op */
738 dentry->d_op->d_release(dentry);
739 /* TODO: check/test the boundaries on this. */
740 if (dentry->d_name.len > DNAME_INLINE_LEN)
741 kfree((void*)dentry->d_name.name);
742 kref_put(&dentry->d_sb->s_kref);
743 if (dentry->d_parent)
744 kref_put(&dentry->d_parent->d_kref);
745 if (dentry->d_mounted_fs)
746 kref_put(&dentry->d_mounted_fs->mnt_kref);
747 if (dentry->d_inode) {
748 TAILQ_REMOVE(&dentry->d_inode->i_dentry, dentry, d_alias);
749 kref_put(&dentry->d_inode->i_kref); /* dentries kref inodes */
751 kmem_cache_free(dentry_kcache, dentry);
754 /* Looks up the dentry for the given path, returning a refcnt'd dentry (or 0).
755 * Permissions are applied for the current user, which is quite a broken system
756 * at the moment. Flags are lookup flags. */
757 struct dentry *lookup_dentry(char *path, int flags)
759 struct dentry *dentry;
760 struct nameidata nd_r = {0}, *nd = &nd_r;
763 error = path_lookup(path, flags, nd);
770 kref_get(&dentry->d_kref, 1);
775 /* Get a dentry from the dcache. At a minimum, we need the name hash and parent
776 * in what_i_want, though most uses will probably be from a get_dentry() call.
777 * We pass in the SB in the off chance that we don't want to use a get'd dentry.
779 * The unusual variable name (instead of just "key" or something) is named after
780 * ex-SPC Castro's porn folder. Caller deals with the memory for what_i_want.
782 * If the dentry is negative, we don't return the actual result - instead, we
783 * set the negative flag in 'what i want'. The reason is we don't want to
784 * kref_get() and then immediately put (causing dentry_release()). This also
785 * means that dentry_release() should never get someone who wasn't USED (barring
786 * the race, which it handles). And we don't need to ever have a dentry set as
787 * USED and NEGATIVE (which is always wrong, but would be needed for a cleaner
790 * This is where we do the "kref resurrection" - we are returning a kref'd
791 * object, even if it wasn't kref'd before. This means the dcache does NOT hold
792 * krefs (it is a weak/internal ref), but it is a source of kref generation. We
793 * sync up with the possible freeing of the dentry by locking the table. See
794 * Doc/kref for more info. */
795 struct dentry *dcache_get(struct super_block *sb, struct dentry *what_i_want)
797 struct dentry *found;
798 /* This lock protects the hash, as well as ensures the returned object
799 * doesn't get deleted/freed out from under us */
800 spin_lock(&sb->s_dcache_lock);
801 found = hashtable_search(sb->s_dcache, what_i_want);
803 if (found->d_flags & DENTRY_NEGATIVE) {
804 what_i_want->d_flags |= DENTRY_NEGATIVE;
805 spin_unlock(&sb->s_dcache_lock);
808 spin_lock(&found->d_lock);
809 __kref_get(&found->d_kref, 1); /* prob could be done outside the lock*/
810 /* If we're here (after kreffing) and it is not USED, we are the one who
811 * should resurrect */
812 if (!(found->d_flags & DENTRY_USED)) {
813 found->d_flags |= DENTRY_USED;
814 spin_lock(&sb->s_lru_lock);
815 TAILQ_REMOVE(&sb->s_lru_d, found, d_lru);
816 spin_unlock(&sb->s_lru_lock);
818 spin_unlock(&found->d_lock);
820 spin_unlock(&sb->s_dcache_lock);
824 /* Adds a dentry to the dcache. Note the *dentry is both the key and the value.
825 * If the value was already in there (which can happen iff it was negative), for
826 * now we'll remove it and put the new one in there. */
827 void dcache_put(struct super_block *sb, struct dentry *key_val)
831 spin_lock(&sb->s_dcache_lock);
832 old = hashtable_remove(sb->s_dcache, key_val);
833 /* if it is old and non-negative, our caller lost a race with someone else
834 * adding the dentry. but since we yanked it out, like a bunch of idiots,
835 * we still have to put it back. should be fairly rare. */
836 if (old && (old->d_flags & DENTRY_NEGATIVE)) {
837 /* This is possible, but rare for now (about to be put on the LRU) */
838 assert(!(old->d_flags & DENTRY_USED));
839 assert(!kref_refcnt(&old->d_kref));
840 spin_lock(&sb->s_lru_lock);
841 TAILQ_REMOVE(&sb->s_lru_d, old, d_lru);
842 spin_unlock(&sb->s_lru_lock);
843 /* TODO: this seems suspect. isn't this the same memory as key_val?
844 * in which case, we just adjust the flags (remove NEG) and reinsert? */
845 assert(old != key_val); // checking TODO comment
848 /* this returns 0 on failure (TODO: Fix this ghetto shit) */
849 retval = hashtable_insert(sb->s_dcache, key_val, key_val);
851 spin_unlock(&sb->s_dcache_lock);
854 /* Will remove and return the dentry. Caller deallocs the key, but the retval
855 * won't have a reference. * Returns 0 if it wasn't found. Callers can't
856 * assume much - they should not use the reference they *get back*, (if they
857 * already had one for key, they can use that). There may be other users out
859 struct dentry *dcache_remove(struct super_block *sb, struct dentry *key)
861 struct dentry *retval;
862 spin_lock(&sb->s_dcache_lock);
863 retval = hashtable_remove(sb->s_dcache, key);
864 spin_unlock(&sb->s_dcache_lock);
868 /* This will clean out the LRU list, which are the unused dentries of the dentry
869 * cache. This will optionally only free the negative ones. Note that we grab
870 * the hash lock for the time we traverse the LRU list - this prevents someone
871 * from getting a kref from the dcache, which could cause us trouble (we rip
872 * someone off the list, who isn't unused, and they try to rip them off the
874 void dcache_prune(struct super_block *sb, bool negative_only)
876 struct dentry *d_i, *temp;
877 struct dentry_tailq victims = TAILQ_HEAD_INITIALIZER(victims);
879 spin_lock(&sb->s_dcache_lock);
880 spin_lock(&sb->s_lru_lock);
881 TAILQ_FOREACH_SAFE(d_i, &sb->s_lru_d, d_lru, temp) {
882 if (!(d_i->d_flags & DENTRY_USED)) {
883 if (negative_only && !(d_i->d_flags & DENTRY_NEGATIVE))
885 /* another place where we'd be better off with tools, not sol'ns */
886 hashtable_remove(sb->s_dcache, d_i);
887 TAILQ_REMOVE(&sb->s_lru_d, d_i, d_lru);
888 TAILQ_INSERT_HEAD(&victims, d_i, d_lru);
891 spin_unlock(&sb->s_lru_lock);
892 spin_unlock(&sb->s_dcache_lock);
893 /* Now do the actual freeing, outside of the hash/LRU list locks. This is
894 * necessary since __dentry_free() will decref its parent, which may get
895 * released and try to add itself to the LRU. */
896 TAILQ_FOREACH_SAFE(d_i, &victims, d_lru, temp) {
897 TAILQ_REMOVE(&victims, d_i, d_lru);
898 assert(!kref_refcnt(&d_i->d_kref));
901 /* It is possible at this point that there are new items on the LRU. We
902 * could loop back until that list is empty, if we care about this. */
905 /* Inode Functions */
907 /* Creates and initializes a new inode. Generic fields are filled in.
908 * FS-specific fields are filled in by the callout. Specific fields are filled
909 * in in read_inode() based on what's on the disk for a given i_no, or when the
910 * inode is created (for new objects).
912 * i_no is set by the caller. Note that this means this inode can be for an
913 * inode that is already on disk, or it can be used when creating. */
914 struct inode *get_inode(struct dentry *dentry)
916 struct super_block *sb = dentry->d_sb;
917 /* FS allocs and sets the following: i_op, i_fop, i_pm.pm_op, and any FS
919 struct inode *inode = sb->s_op->alloc_inode(sb);
924 TAILQ_INSERT_HEAD(&sb->s_inodes, inode, i_sb_list); /* weak inode ref */
925 TAILQ_INIT(&inode->i_dentry);
926 TAILQ_INSERT_TAIL(&inode->i_dentry, dentry, d_alias); /* weak dentry ref*/
927 /* one for the dentry->d_inode, one passed out */
928 kref_init(&inode->i_kref, inode_release, 2);
929 dentry->d_inode = inode;
930 inode->i_ino = 0; /* set by caller later */
931 inode->i_blksize = sb->s_blocksize;
932 spinlock_init(&inode->i_lock);
933 kref_get(&sb->s_kref, 1); /* could allow the dentry to pin it */
935 inode->i_rdev = 0; /* this has no real meaning yet */
936 inode->i_bdev = sb->s_bdev; /* storing an uncounted ref */
937 inode->i_state = 0; /* need real states, like I_NEW */
938 inode->dirtied_when = 0;
940 atomic_set(&inode->i_writecount, 0);
941 /* Set up the page_map structures. Default is to use the embedded one.
942 * Might push some of this back into specific FSs. For now, the FS tells us
943 * what pm_op they want via i_pm.pm_op, which we set again in pm_init() */
944 inode->i_mapping = &inode->i_pm;
945 pm_init(inode->i_mapping, inode->i_pm.pm_op, inode);
949 /* Helper: loads/ reads in the inode numbered ino and attaches it to dentry */
950 void load_inode(struct dentry *dentry, unsigned long ino)
954 /* look it up in the inode cache first */
955 inode = icache_get(dentry->d_sb, ino);
957 /* connect the dentry to its inode */
958 TAILQ_INSERT_TAIL(&inode->i_dentry, dentry, d_alias);
959 dentry->d_inode = inode; /* storing the ref we got from icache_get */
962 /* otherwise, we need to do it manually */
963 inode = get_inode(dentry);
965 dentry->d_sb->s_op->read_inode(inode);
966 /* TODO: race here, two creators could miss in the cache, and then get here.
967 * need a way to sync across a blocking call. needs to be either at this
968 * point in the code or per the ino (dentries could be different) */
969 icache_put(dentry->d_sb, inode);
970 kref_put(&inode->i_kref);
973 /* Helper op, used when creating regular files, directories, symlinks, etc.
974 * Note we make a distinction between the mode and the file type (for now).
975 * After calling this, call the FS specific version (create or mkdir), which
976 * will set the i_ino, the filetype, and do any other FS-specific stuff. Also
977 * note that a lot of inode stuff was initialized in get_inode/alloc_inode. The
978 * stuff here is pertinent to the specific creator (user), mode, and time. Also
979 * note we don't pass this an nd, like Linux does... */
980 static struct inode *create_inode(struct dentry *dentry, int mode)
982 uint64_t now = epoch_seconds();
983 /* note it is the i_ino that uniquely identifies a file in the specific
984 * filesystem. there's a diff between creating an inode (even for an in-use
985 * ino) and then filling it in, and vs creating a brand new one.
986 * get_inode() sets it to 0, and it should be filled in later in an
987 * FS-specific manner. */
988 struct inode *inode = get_inode(dentry);
991 inode->i_mode = mode & S_PMASK; /* note that after this, we have no type */
995 inode->i_atime.tv_sec = now;
996 inode->i_ctime.tv_sec = now;
997 inode->i_mtime.tv_sec = now;
998 inode->i_atime.tv_nsec = 0;
999 inode->i_ctime.tv_nsec = 0;
1000 inode->i_mtime.tv_nsec = 0;
1001 inode->i_bdev = inode->i_sb->s_bdev;
1002 /* when we have notions of users, do something here: */
1008 /* Create a new disk inode in dir associated with dentry, with the given mode.
1009 * called when creating a regular file. dir is the directory/parent. dentry is
1010 * the dentry of the inode we are creating. Note the lack of the nd... */
1011 int create_file(struct inode *dir, struct dentry *dentry, int mode)
1013 struct inode *new_file = create_inode(dentry, mode);
1016 dir->i_op->create(dir, dentry, mode, 0);
1017 icache_put(new_file->i_sb, new_file);
1018 kref_put(&new_file->i_kref);
1022 /* Creates a new inode for a directory associated with dentry in dir with the
1024 int create_dir(struct inode *dir, struct dentry *dentry, int mode)
1026 struct inode *new_dir = create_inode(dentry, mode);
1029 dir->i_op->mkdir(dir, dentry, mode);
1030 dir->i_nlink++; /* Directories get a hardlink for every child dir */
1031 /* Make sure my parent tracks me. This is okay, since no directory (dir)
1032 * can have more than one dentry */
1033 struct dentry *parent = TAILQ_FIRST(&dir->i_dentry);
1034 assert(parent && parent == TAILQ_LAST(&dir->i_dentry, dentry_tailq));
1035 /* parent dentry tracks dentry as a subdir, weak reference */
1036 TAILQ_INSERT_TAIL(&parent->d_subdirs, dentry, d_subdirs_link);
1037 icache_put(new_dir->i_sb, new_dir);
1038 kref_put(&new_dir->i_kref);
1042 /* Creates a new inode for a symlink associated with dentry in dir, containing
1043 * the symlink symname */
1044 int create_symlink(struct inode *dir, struct dentry *dentry,
1045 const char *symname, int mode)
1047 struct inode *new_sym = create_inode(dentry, mode);
1050 dir->i_op->symlink(dir, dentry, symname);
1051 icache_put(new_sym->i_sb, new_sym);
1052 kref_put(&new_sym->i_kref);
1056 /* Returns 0 if the given mode is acceptable for the inode, and an appropriate
1057 * error code if not. Needs to be writen, based on some sensible rules, and
1058 * will also probably use 'current' */
1059 int check_perms(struct inode *inode, int access_mode)
1061 return 0; /* anything goes! */
1064 /* Called after all external refs are gone to clean up the inode. Once this is
1065 * called, all dentries pointing here are already done (one of them triggered
1066 * this via kref_put(). */
1067 void inode_release(struct kref *kref)
1069 struct inode *inode = container_of(kref, struct inode, i_kref);
1070 TAILQ_REMOVE(&inode->i_sb->s_inodes, inode, i_sb_list);
1071 icache_remove(inode->i_sb, inode->i_ino);
1072 /* Might need to write back or delete the file/inode */
1073 if (inode->i_nlink) {
1074 if (inode->i_state & I_STATE_DIRTY)
1075 inode->i_sb->s_op->write_inode(inode, TRUE);
1077 inode->i_sb->s_op->delete_inode(inode);
1079 if (S_ISFIFO(inode->i_mode)) {
1080 page_decref(kva2page(inode->i_pipe->p_buf));
1081 kfree(inode->i_pipe);
1084 // kref_put(inode->i_bdev->kref); /* assuming it's a bdev, could be a pipe*/
1085 /* Either way, we dealloc the in-memory version */
1086 inode->i_sb->s_op->dealloc_inode(inode); /* FS-specific clean-up */
1087 kref_put(&inode->i_sb->s_kref);
1088 /* TODO: clean this up */
1089 assert(inode->i_mapping == &inode->i_pm);
1090 kmem_cache_free(inode_kcache, inode);
1093 /* Fills in kstat with the stat information for the inode */
1094 void stat_inode(struct inode *inode, struct kstat *kstat)
1096 kstat->st_dev = inode->i_sb->s_dev;
1097 kstat->st_ino = inode->i_ino;
1098 kstat->st_mode = inode->i_mode;
1099 kstat->st_nlink = inode->i_nlink;
1100 kstat->st_uid = inode->i_uid;
1101 kstat->st_gid = inode->i_gid;
1102 kstat->st_rdev = inode->i_rdev;
1103 kstat->st_size = inode->i_size;
1104 kstat->st_blksize = inode->i_blksize;
1105 kstat->st_blocks = inode->i_blocks;
1106 kstat->st_atime = inode->i_atime;
1107 kstat->st_mtime = inode->i_mtime;
1108 kstat->st_ctime = inode->i_ctime;
1111 void print_kstat(struct kstat *kstat)
1113 printk("kstat info for %p:\n", kstat);
1114 printk("\tst_dev : %p\n", kstat->st_dev);
1115 printk("\tst_ino : %p\n", kstat->st_ino);
1116 printk("\tst_mode : %p\n", kstat->st_mode);
1117 printk("\tst_nlink : %p\n", kstat->st_nlink);
1118 printk("\tst_uid : %p\n", kstat->st_uid);
1119 printk("\tst_gid : %p\n", kstat->st_gid);
1120 printk("\tst_rdev : %p\n", kstat->st_rdev);
1121 printk("\tst_size : %p\n", kstat->st_size);
1122 printk("\tst_blksize: %p\n", kstat->st_blksize);
1123 printk("\tst_blocks : %p\n", kstat->st_blocks);
1124 printk("\tst_atime : %p\n", kstat->st_atime);
1125 printk("\tst_mtime : %p\n", kstat->st_mtime);
1126 printk("\tst_ctime : %p\n", kstat->st_ctime);
1129 /* Inode Cache management. In general, search on the ino, get a refcnt'd value
1130 * back. Remove does not give you a reference back - it should only be called
1131 * in inode_release(). */
1132 struct inode *icache_get(struct super_block *sb, unsigned long ino)
1134 /* This is the same style as in pid2proc, it's the "safely create a strong
1135 * reference from a weak one, so long as other strong ones exist" pattern */
1136 spin_lock(&sb->s_icache_lock);
1137 struct inode *inode = hashtable_search(sb->s_icache, (void*)ino);
1139 if (!kref_get_not_zero(&inode->i_kref, 1))
1141 spin_unlock(&sb->s_icache_lock);
1145 void icache_put(struct super_block *sb, struct inode *inode)
1147 spin_lock(&sb->s_icache_lock);
1148 /* there's a race in load_ino() that could trigger this */
1149 assert(!hashtable_search(sb->s_icache, (void*)inode->i_ino));
1150 hashtable_insert(sb->s_icache, (void*)inode->i_ino, inode);
1151 spin_unlock(&sb->s_icache_lock);
1154 struct inode *icache_remove(struct super_block *sb, unsigned long ino)
1156 struct inode *inode;
1157 /* Presumably these hashtable removals could be easier since callers
1158 * actually know who they are (same with the pid2proc hash) */
1159 spin_lock(&sb->s_icache_lock);
1160 inode = hashtable_remove(sb->s_icache, (void*)ino);
1161 spin_unlock(&sb->s_icache_lock);
1162 assert(inode && !kref_refcnt(&inode->i_kref));
1166 /* File functions */
1168 /* Read count bytes from the file into buf, starting at *offset, which is
1169 * increased accordingly, returning the number of bytes transfered. Most
1170 * filesystems will use this function for their f_op->read.
1171 * Note, this uses the page cache. */
1172 ssize_t generic_file_read(struct file *file, char *buf, size_t count,
1178 unsigned long first_idx, last_idx;
1181 /* read in offset, in case of a concurrent reader/writer, so we don't screw
1182 * up our math for count, the idxs, etc. */
1183 off64_t orig_off = ACCESS_ONCE(*offset);
1185 /* Consider pushing some error checking higher in the VFS */
1188 if (orig_off >= file->f_dentry->d_inode->i_size)
1190 /* Make sure we don't go past the end of the file */
1191 if (orig_off + count > file->f_dentry->d_inode->i_size) {
1192 count = file->f_dentry->d_inode->i_size - orig_off;
1194 assert((long)count > 0);
1195 page_off = orig_off & (PGSIZE - 1);
1196 first_idx = orig_off >> PGSHIFT;
1197 last_idx = (orig_off + count) >> PGSHIFT;
1198 buf_end = buf + count;
1199 /* For each file page, make sure it's in the page cache, then copy it out.
1200 * TODO: will probably need to consider concurrently truncated files here.*/
1201 for (int i = first_idx; i <= last_idx; i++) {
1202 error = pm_load_page(file->f_mapping, i, &page);
1203 assert(!error); /* TODO: handle ENOMEM and friends */
1204 copy_amt = MIN(PGSIZE - page_off, buf_end - buf);
1205 /* TODO: (UMEM) think about this. if it's a user buffer, we're relying
1206 * on current to detect whose it is (which should work for async calls).
1207 * Also, need to propagate errors properly... Probably should do a
1208 * user_mem_check, then free, and also to make a distinction between
1209 * when the kernel wants a read/write (TODO: KFOP) */
1211 memcpy_to_user(current, buf, page2kva(page) + page_off, copy_amt);
1213 memcpy(buf, page2kva(page) + page_off, copy_amt);
1217 pm_put_page(page); /* it's still in the cache, we just don't need it */
1219 assert(buf == buf_end);
1220 /* could have concurrent file ops that screw with offset, so userspace isn't
1221 * safe. but at least it'll be a value that one of the concurrent ops could
1222 * have produced (compared to *offset_changed_concurrently += count. */
1223 *offset = orig_off + count;
1227 /* Write count bytes from buf to the file, starting at *offset, which is
1228 * increased accordingly, returning the number of bytes transfered. Most
1229 * filesystems will use this function for their f_op->write. Note, this uses
1232 * Changes don't get flushed to disc til there is an fsync, page cache eviction,
1233 * or other means of trying to writeback the pages. */
1234 ssize_t generic_file_write(struct file *file, const char *buf, size_t count,
1240 unsigned long first_idx, last_idx;
1242 const char *buf_end;
1243 off64_t orig_off = ACCESS_ONCE(*offset);
1245 /* Consider pushing some error checking higher in the VFS */
1248 if (file->f_flags & O_APPEND) {
1249 spin_lock(&file->f_dentry->d_inode->i_lock);
1250 orig_off = file->f_dentry->d_inode->i_size;
1251 /* setting the filesize here, instead of during the extend-check, since
1252 * we need to atomically reserve space and set our write position. */
1253 file->f_dentry->d_inode->i_size += count;
1254 spin_unlock(&file->f_dentry->d_inode->i_lock);
1256 if (orig_off + count > file->f_dentry->d_inode->i_size) {
1257 /* lock for writes to i_size. we allow lockless reads. recheck
1258 * i_size in case of concurrent writers since our orig check. */
1259 spin_lock(&file->f_dentry->d_inode->i_lock);
1260 if (orig_off + count > file->f_dentry->d_inode->i_size)
1261 file->f_dentry->d_inode->i_size = orig_off + count;
1262 spin_unlock(&file->f_dentry->d_inode->i_lock);
1265 page_off = orig_off & (PGSIZE - 1);
1266 first_idx = orig_off >> PGSHIFT;
1267 last_idx = (orig_off + count) >> PGSHIFT;
1268 buf_end = buf + count;
1269 /* For each file page, make sure it's in the page cache, then write it.*/
1270 for (int i = first_idx; i <= last_idx; i++) {
1271 error = pm_load_page(file->f_mapping, i, &page);
1272 assert(!error); /* TODO: handle ENOMEM and friends */
1273 copy_amt = MIN(PGSIZE - page_off, buf_end - buf);
1274 /* TODO: (UMEM) (KFOP) think about this. if it's a user buffer, we're
1275 * relying on current to detect whose it is (which should work for async
1278 memcpy_from_user(current, page2kva(page) + page_off, buf, copy_amt);
1280 memcpy(page2kva(page) + page_off, buf, copy_amt);
1284 atomic_or(&page->pg_flags, PG_DIRTY);
1285 pm_put_page(page); /* it's still in the cache, we just don't need it */
1287 assert(buf == buf_end);
1288 *offset = orig_off + count;
1292 /* Directories usually use this for their read method, which is the way glibc
1293 * currently expects us to do a readdir (short of doing linux's getdents). Will
1294 * probably need work, based on whatever real programs want. */
1295 ssize_t generic_dir_read(struct file *file, char *u_buf, size_t count,
1298 struct kdirent dir_r = {0}, *dirent = &dir_r;
1300 size_t amt_copied = 0;
1301 char *buf_end = u_buf + count;
1303 if (!S_ISDIR(file->f_dentry->d_inode->i_mode)) {
1309 /* start readdir from where it left off: */
1310 dirent->d_off = *offset;
1312 u_buf + sizeof(struct kdirent) <= buf_end;
1313 u_buf += sizeof(struct kdirent)) {
1314 /* TODO: UMEM/KFOP (pin the u_buf in the syscall, ditch the local copy,
1315 * get rid of this memcpy and reliance on current, etc). Might be
1316 * tricky with the dirent->d_off and trust issues */
1317 retval = file->f_op->readdir(file, dirent);
1322 /* Slight info exposure: could be extra crap after the name in the
1323 * dirent (like the name of a deleted file) */
1325 memcpy_to_user(current, u_buf, dirent, sizeof(struct dirent));
1327 memcpy(u_buf, dirent, sizeof(struct dirent));
1329 amt_copied += sizeof(struct dirent);
1330 /* 0 signals end of directory */
1334 /* Next time read is called, we pick up where we left off */
1335 *offset = dirent->d_off; /* UMEM */
1336 /* important to tell them how much they got. they often keep going til they
1337 * get 0 back (in the case of ls). it's also how much has been read, but it
1338 * isn't how much the f_pos has moved (which is opaque to the VFS). */
1342 /* Opens the file, using permissions from current for lack of a better option.
1343 * It will attempt to create the file if it does not exist and O_CREAT is
1344 * specified. This will return 0 on failure, and set errno. TODO: There's some
1345 * stuff that we don't do, esp related file truncating/creation. flags are for
1346 * opening, the mode is for creating. The flags related to how to create
1347 * (O_CREAT_FLAGS) are handled in this function, not in create_file().
1349 * It's tempting to split this into a do_file_create and a do_file_open, based
1350 * on the O_CREAT flag, but the O_CREAT flag can be ignored if the file exists
1351 * already and O_EXCL isn't specified. We could have open call create if it
1352 * fails, but for now we'll keep it as is. */
1353 struct file *do_file_open(char *path, int flags, int mode)
1355 struct file *file = 0;
1356 struct dentry *file_d;
1357 struct inode *parent_i;
1358 struct nameidata nd_r = {0}, *nd = &nd_r;
1360 unsigned long nr_pages;
1362 /* The file might exist, lets try to just open it right away */
1363 nd->intent = LOOKUP_OPEN;
1364 error = path_lookup(path, LOOKUP_FOLLOW, nd);
1366 /* If this is a directory, make sure we are opening with O_RDONLY.
1367 * Unfortunately we can't just check for O_RDONLY directly because its
1368 * value is 0x0. We instead have to make sure it's not O_WRONLY and
1369 * not O_RDWR explicitly. */
1370 if (S_ISDIR(nd->dentry->d_inode->i_mode) &&
1371 ((flags & O_WRONLY) || (flags & O_RDWR))) {
1375 /* Also need to make sure we didn't want to O_EXCL create */
1376 if ((flags & O_CREAT) && (flags & O_EXCL)) {
1380 file_d = nd->dentry;
1381 kref_get(&file_d->d_kref, 1);
1384 /* So it didn't already exist, release the path from the previous lookup,
1385 * and then we try to create it. */
1387 /* get the parent, following links. this means you get the parent of the
1388 * final link (which may not be in 'path' in the first place. */
1389 nd->intent = LOOKUP_CREATE;
1390 error = path_lookup(path, LOOKUP_PARENT | LOOKUP_FOLLOW, nd);
1395 /* see if the target is there (shouldn't be), and handle accordingly */
1396 file_d = do_lookup(nd->dentry, nd->last.name);
1398 if (!(flags & O_CREAT)) {
1402 /* Create the inode/file. get a fresh dentry too: */
1403 file_d = get_dentry(nd->dentry->d_sb, nd->dentry, nd->last.name);
1406 parent_i = nd->dentry->d_inode;
1407 /* Note that the mode technically should only apply to future opens,
1408 * but we apply it immediately. */
1409 if (create_file(parent_i, file_d, mode)) /* sets errno */
1411 dcache_put(file_d->d_sb, file_d);
1412 } else { /* something already exists */
1413 /* this can happen due to concurrent access, but needs to be thought
1415 panic("File shouldn't be here!");
1416 if ((flags & O_CREAT) && (flags & O_EXCL)) {
1417 /* wanted to create, not open, bail out */
1423 /* now open the file (freshly created or if it already existed). At this
1424 * point, file_d is a refcnt'd dentry, regardless of which branch we took.*/
1425 if (flags & O_TRUNC) {
1426 spin_lock(&file_d->d_inode->i_lock);
1427 nr_pages = ROUNDUP(file_d->d_inode->i_size, PGSIZE) >> PGSHIFT;
1428 file_d->d_inode->i_size = 0;
1429 spin_unlock(&file_d->d_inode->i_lock);
1430 pm_remove_contig(file_d->d_inode->i_mapping, 0, nr_pages);
1432 file = dentry_open(file_d, flags); /* sets errno */
1433 /* Note the fall through to the exit paths. File is 0 by default and if
1434 * dentry_open fails. */
1436 kref_put(&file_d->d_kref);
1442 /* Path is the location of the symlink, sometimes called the "new path", and
1443 * symname is who we link to, sometimes called the "old path". */
1444 int do_symlink(char *path, const char *symname, int mode)
1446 struct dentry *sym_d;
1447 struct inode *parent_i;
1448 struct nameidata nd_r = {0}, *nd = &nd_r;
1452 nd->intent = LOOKUP_CREATE;
1453 /* get the parent, but don't follow links */
1454 error = path_lookup(path, LOOKUP_PARENT, nd);
1459 /* see if the target is already there, handle accordingly */
1460 sym_d = do_lookup(nd->dentry, nd->last.name);
1465 /* Doesn't already exist, let's try to make it: */
1466 sym_d = get_dentry(nd->dentry->d_sb, nd->dentry, nd->last.name);
1469 parent_i = nd->dentry->d_inode;
1470 if (create_symlink(parent_i, sym_d, symname, mode))
1472 dcache_put(sym_d->d_sb, sym_d);
1473 retval = 0; /* Note the fall through to the exit paths */
1475 kref_put(&sym_d->d_kref);
1481 /* Makes a hard link for the file behind old_path to new_path */
1482 int do_link(char *old_path, char *new_path)
1484 struct dentry *link_d, *old_d;
1485 struct inode *inode, *parent_dir;
1486 struct nameidata nd_r = {0}, *nd = &nd_r;
1490 nd->intent = LOOKUP_CREATE;
1491 /* get the absolute parent of the new_path */
1492 error = path_lookup(new_path, LOOKUP_PARENT | LOOKUP_FOLLOW, nd);
1497 parent_dir = nd->dentry->d_inode;
1498 /* see if the new target is already there, handle accordingly */
1499 link_d = do_lookup(nd->dentry, nd->last.name);
1504 /* Doesn't already exist, let's try to make it. Still need to stitch it to
1505 * an inode and set its FS-specific stuff after this.*/
1506 link_d = get_dentry(nd->dentry->d_sb, nd->dentry, nd->last.name);
1509 /* Now let's get the old_path target */
1510 old_d = lookup_dentry(old_path, LOOKUP_FOLLOW);
1511 if (!old_d) /* errno set by lookup_dentry */
1513 /* For now, can only link to files */
1514 if (!S_ISREG(old_d->d_inode->i_mode)) {
1518 /* Must be on the same FS */
1519 if (old_d->d_sb != link_d->d_sb) {
1523 /* Do whatever FS specific stuff there is first (which is also a chance to
1525 error = parent_dir->i_op->link(old_d, parent_dir, link_d);
1530 /* Finally stitch it up */
1531 inode = old_d->d_inode;
1532 kref_get(&inode->i_kref, 1);
1533 link_d->d_inode = inode;
1535 TAILQ_INSERT_TAIL(&inode->i_dentry, link_d, d_alias); /* weak ref */
1536 dcache_put(link_d->d_sb, link_d);
1537 retval = 0; /* Note the fall through to the exit paths */
1539 kref_put(&old_d->d_kref);
1541 kref_put(&link_d->d_kref);
1547 /* Unlinks path from the directory tree. Read the Documentation for more info.
1549 int do_unlink(char *path)
1551 struct dentry *dentry;
1552 struct inode *parent_dir;
1553 struct nameidata nd_r = {0}, *nd = &nd_r;
1557 /* get the parent of the target, and don't follow a final link */
1558 error = path_lookup(path, LOOKUP_PARENT, nd);
1563 parent_dir = nd->dentry->d_inode;
1564 /* make sure the target is there */
1565 dentry = do_lookup(nd->dentry, nd->last.name);
1570 /* Make sure the target is not a directory */
1571 if (S_ISDIR(dentry->d_inode->i_mode)) {
1575 /* Remove the dentry from its parent */
1576 error = parent_dir->i_op->unlink(parent_dir, dentry);
1581 /* Now that our parent doesn't track us, we need to make sure we aren't
1582 * findable via the dentry cache. DYING, so we will be freed in
1583 * dentry_release() */
1584 dentry->d_flags |= DENTRY_DYING;
1585 dcache_remove(dentry->d_sb, dentry);
1586 dentry->d_inode->i_nlink--; /* TODO: race here, esp with a decref */
1587 /* At this point, the dentry is unlinked from the FS, and the inode has one
1588 * less link. When the in-memory objects (dentry, inode) are going to be
1589 * released (after all open files are closed, and maybe after entries are
1590 * evicted from the cache), then nlinks will get checked and the FS-file
1591 * will get removed from the disk */
1592 retval = 0; /* Note the fall through to the exit paths */
1594 kref_put(&dentry->d_kref);
1600 /* Checks to see if path can be accessed via mode. Need to actually send the
1601 * mode along somehow, so this doesn't do much now. This is an example of
1602 * decent error propagation from the lower levels via int retvals. */
1603 int do_access(char *path, int mode)
1605 struct nameidata nd_r = {0}, *nd = &nd_r;
1607 nd->intent = LOOKUP_ACCESS;
1608 retval = path_lookup(path, 0, nd);
1613 int do_file_chmod(struct file *file, int mode)
1615 int old_mode_ftype = file->f_dentry->d_inode->i_mode & __S_IFMT;
1617 /* TODO: when we have notions of uid, check for the proc's uid */
1618 if (file->f_dentry->d_inode->i_uid != UID_OF_ME)
1622 file->f_dentry->d_inode->i_mode = (mode & S_PMASK) | old_mode_ftype;
1626 /* Make a directory at path with mode. Returns -1 and sets errno on errors */
1627 int do_mkdir(char *path, int mode)
1629 struct dentry *dentry;
1630 struct inode *parent_i;
1631 struct nameidata nd_r = {0}, *nd = &nd_r;
1635 nd->intent = LOOKUP_CREATE;
1636 /* get the parent, but don't follow links */
1637 error = path_lookup(path, LOOKUP_PARENT, nd);
1642 /* see if the target is already there, handle accordingly */
1643 dentry = do_lookup(nd->dentry, nd->last.name);
1648 /* Doesn't already exist, let's try to make it: */
1649 dentry = get_dentry(nd->dentry->d_sb, nd->dentry, nd->last.name);
1652 parent_i = nd->dentry->d_inode;
1653 if (create_dir(parent_i, dentry, mode))
1655 dcache_put(dentry->d_sb, dentry);
1656 retval = 0; /* Note the fall through to the exit paths */
1658 kref_put(&dentry->d_kref);
1664 int do_rmdir(char *path)
1666 struct dentry *dentry;
1667 struct inode *parent_i;
1668 struct nameidata nd_r = {0}, *nd = &nd_r;
1672 /* get the parent, following links (probably want this), and we must get a
1673 * directory. Note, current versions of path_lookup can't handle both
1674 * PARENT and DIRECTORY, at least, it doesn't check that *path is a
1676 error = path_lookup(path, LOOKUP_PARENT | LOOKUP_FOLLOW | LOOKUP_DIRECTORY,
1682 /* make sure the target is already there, handle accordingly */
1683 dentry = do_lookup(nd->dentry, nd->last.name);
1688 if (!S_ISDIR(dentry->d_inode->i_mode)) {
1692 if (dentry->d_mount_point) {
1696 /* TODO: make sure we aren't a mount or processes root (EBUSY) */
1697 /* Now for the removal. the FSs will check if they are empty */
1698 parent_i = nd->dentry->d_inode;
1699 error = parent_i->i_op->rmdir(parent_i, dentry);
1704 /* Now that our parent doesn't track us, we need to make sure we aren't
1705 * findable via the dentry cache. DYING, so we will be freed in
1706 * dentry_release() */
1707 dentry->d_flags |= DENTRY_DYING;
1708 dcache_remove(dentry->d_sb, dentry);
1709 /* Decref ourselves, so inode_release() knows we are done */
1710 dentry->d_inode->i_nlink--;
1711 TAILQ_REMOVE(&nd->dentry->d_subdirs, dentry, d_subdirs_link);
1712 parent_i->i_nlink--; /* TODO: race on this, esp since its a decref */
1713 /* we still have d_parent and a kref on our parent, which will go away when
1714 * the in-memory dentry object goes away. */
1715 retval = 0; /* Note the fall through to the exit paths */
1717 kref_put(&dentry->d_kref);
1723 /* Pipes: Doing a simple buffer with reader and writer offsets. Size is power
1724 * of two, so we can easily compute its status and whatnot. */
1726 #define PIPE_SZ (1 << PGSHIFT)
1728 static size_t pipe_get_rd_idx(struct pipe_inode_info *pii)
1730 return pii->p_rd_off & (PIPE_SZ - 1);
1733 static size_t pipe_get_wr_idx(struct pipe_inode_info *pii)
1736 return pii->p_wr_off & (PIPE_SZ - 1);
1739 static bool pipe_is_empty(struct pipe_inode_info *pii)
1741 return __ring_empty(pii->p_wr_off, pii->p_rd_off);
1744 static bool pipe_is_full(struct pipe_inode_info *pii)
1746 return __ring_full(PIPE_SZ, pii->p_wr_off, pii->p_rd_off);
1749 static size_t pipe_nr_full(struct pipe_inode_info *pii)
1751 return __ring_nr_full(pii->p_wr_off, pii->p_rd_off);
1754 static size_t pipe_nr_empty(struct pipe_inode_info *pii)
1756 return __ring_nr_empty(PIPE_SZ, pii->p_wr_off, pii->p_rd_off);
1759 ssize_t pipe_file_read(struct file *file, char *buf, size_t count,
1762 struct pipe_inode_info *pii = file->f_dentry->d_inode->i_pipe;
1763 size_t copy_amt, amt_copied = 0;
1765 cv_lock(&pii->p_cv);
1766 while (pipe_is_empty(pii)) {
1767 /* We wait til the pipe is drained before sending EOF if there are no
1768 * writers (instead of aborting immediately) */
1769 if (!pii->p_nr_writers) {
1770 cv_unlock(&pii->p_cv);
1773 if (file->f_flags & O_NONBLOCK) {
1774 cv_unlock(&pii->p_cv);
1778 cv_wait(&pii->p_cv);
1781 /* We might need to wrap-around with our copy, so we'll do the copy in two
1782 * passes. This will copy up to the end of the buffer, then on the next
1783 * pass will copy the rest to the beginning of the buffer (if necessary) */
1784 for (int i = 0; i < 2; i++) {
1785 copy_amt = MIN(PIPE_SZ - pipe_get_rd_idx(pii),
1786 MIN(pipe_nr_full(pii), count));
1787 assert(current); /* shouldn't pipe from the kernel */
1788 memcpy_to_user(current, buf, pii->p_buf + pipe_get_rd_idx(pii),
1792 pii->p_rd_off += copy_amt;
1793 amt_copied += copy_amt;
1795 /* Just using one CV for both readers and writers. We should rarely have
1796 * multiple readers or writers. */
1798 __cv_broadcast(&pii->p_cv);
1799 cv_unlock(&pii->p_cv);
1803 /* Note: we're not dealing with PIPE_BUF and minimum atomic chunks, unless I
1805 ssize_t pipe_file_write(struct file *file, const char *buf, size_t count,
1808 struct pipe_inode_info *pii = file->f_dentry->d_inode->i_pipe;
1809 size_t copy_amt, amt_copied = 0;
1811 cv_lock(&pii->p_cv);
1812 /* Write aborts right away if there are no readers, regardless of pipe
1814 if (!pii->p_nr_readers) {
1815 cv_unlock(&pii->p_cv);
1819 while (pipe_is_full(pii)) {
1820 if (file->f_flags & O_NONBLOCK) {
1821 cv_unlock(&pii->p_cv);
1825 cv_wait(&pii->p_cv);
1827 /* Still need to check in the loop, in case the last reader left while
1829 if (!pii->p_nr_readers) {
1830 cv_unlock(&pii->p_cv);
1835 /* We might need to wrap-around with our copy, so we'll do the copy in two
1836 * passes. This will copy up to the end of the buffer, then on the next
1837 * pass will copy the rest to the beginning of the buffer (if necessary) */
1838 for (int i = 0; i < 2; i++) {
1839 copy_amt = MIN(PIPE_SZ - pipe_get_wr_idx(pii),
1840 MIN(pipe_nr_empty(pii), count));
1841 assert(current); /* shouldn't pipe from the kernel */
1842 memcpy_from_user(current, pii->p_buf + pipe_get_wr_idx(pii), buf,
1846 pii->p_wr_off += copy_amt;
1847 amt_copied += copy_amt;
1849 /* Just using one CV for both readers and writers. We should rarely have
1850 * multiple readers or writers. */
1852 __cv_broadcast(&pii->p_cv);
1853 cv_unlock(&pii->p_cv);
1857 /* In open and release, we need to track the number of readers and writers,
1858 * which we can differentiate by the file flags. */
1859 int pipe_open(struct inode *inode, struct file *file)
1861 struct pipe_inode_info *pii = inode->i_pipe;
1862 cv_lock(&pii->p_cv);
1863 /* Ugliness due to not using flags for O_RDONLY and friends... */
1864 if (file->f_mode == S_IRUSR) {
1865 pii->p_nr_readers++;
1866 } else if (file->f_mode == S_IWUSR) {
1867 pii->p_nr_writers++;
1869 warn("Bad pipe file flags 0x%x\n", file->f_flags);
1871 cv_unlock(&pii->p_cv);
1875 int pipe_release(struct inode *inode, struct file *file)
1877 struct pipe_inode_info *pii = inode->i_pipe;
1878 cv_lock(&pii->p_cv);
1879 /* Ugliness due to not using flags for O_RDONLY and friends... */
1880 if (file->f_mode == S_IRUSR) {
1881 pii->p_nr_readers--;
1882 } else if (file->f_mode == S_IWUSR) {
1883 pii->p_nr_writers--;
1885 warn("Bad pipe file flags 0x%x\n", file->f_flags);
1887 /* need to wake up any sleeping readers/writers, since we might be done */
1888 __cv_broadcast(&pii->p_cv);
1889 cv_unlock(&pii->p_cv);
1893 struct file_operations pipe_f_op = {
1894 .read = pipe_file_read,
1895 .write = pipe_file_write,
1897 .release = pipe_release,
1901 void pipe_debug(struct file *f)
1903 struct pipe_inode_info *pii = f->f_dentry->d_inode->i_pipe;
1905 printk("PIPE %p\n", pii);
1906 printk("\trdoff %p\n", pii->p_rd_off);
1907 printk("\twroff %p\n", pii->p_wr_off);
1908 printk("\tnr_rds %d\n", pii->p_nr_readers);
1909 printk("\tnr_wrs %d\n", pii->p_nr_writers);
1910 printk("\tcv waiters %d\n", pii->p_cv.nr_waiters);
1914 /* General plan: get a dentry/inode to represent the pipe. We'll alloc it from
1915 * the default_ns SB, but won't actually link it anywhere. It'll only be held
1916 * alive by the krefs, til all the FDs are closed. */
1917 int do_pipe(struct file **pipe_files, int flags)
1919 struct dentry *pipe_d;
1920 struct inode *pipe_i;
1921 struct file *pipe_f_read, *pipe_f_write;
1922 struct super_block *def_sb = default_ns.root->mnt_sb;
1923 struct pipe_inode_info *pii;
1925 pipe_d = get_dentry(def_sb, 0, "pipe");
1928 pipe_d->d_op = &dummy_d_op;
1929 pipe_i = get_inode(pipe_d);
1931 goto error_post_dentry;
1932 /* preemptively mark the dentry for deletion. we have an unlinked dentry
1933 * right off the bat, held in only by the kref chain (pipe_d is the ref). */
1934 pipe_d->d_flags |= DENTRY_DYING;
1935 /* pipe_d->d_inode still has one ref to pipe_i, keeping the inode alive */
1936 kref_put(&pipe_i->i_kref);
1937 /* init inode fields. note we're using the dummy ops for i_op and d_op */
1938 pipe_i->i_mode = S_IRWXU | S_IRWXG | S_IRWXO;
1939 SET_FTYPE(pipe_i->i_mode, __S_IFIFO); /* using type == FIFO */
1940 pipe_i->i_nlink = 1; /* one for the dentry */
1943 pipe_i->i_size = PGSIZE;
1944 pipe_i->i_blocks = 0;
1945 pipe_i->i_atime.tv_sec = 0;
1946 pipe_i->i_atime.tv_nsec = 0;
1947 pipe_i->i_mtime.tv_sec = 0;
1948 pipe_i->i_mtime.tv_nsec = 0;
1949 pipe_i->i_ctime.tv_sec = 0;
1950 pipe_i->i_ctime.tv_nsec = 0;
1951 pipe_i->i_fs_info = 0;
1952 pipe_i->i_op = &dummy_i_op;
1953 pipe_i->i_fop = &pipe_f_op;
1954 pipe_i->i_socket = FALSE;
1955 /* Actually build the pipe. We're using one page, hanging off the
1956 * pipe_inode_info struct. When we release the inode, we free the pipe
1958 pipe_i->i_pipe = kmalloc(sizeof(struct pipe_inode_info), KMALLOC_WAIT);
1959 pii = pipe_i->i_pipe;
1964 pii->p_buf = kpage_zalloc_addr();
1971 pii->p_nr_readers = 0;
1972 pii->p_nr_writers = 0;
1973 cv_init(&pii->p_cv); /* must do this before dentry_open / pipe_open */
1974 /* Now we have an inode for the pipe. We need two files for the read and
1975 * write ends of the pipe. */
1976 flags &= ~(O_ACCMODE); /* avoid user bugs */
1977 pipe_f_read = dentry_open(pipe_d, flags | O_RDONLY);
1980 pipe_f_write = dentry_open(pipe_d, flags | O_WRONLY);
1983 pipe_files[0] = pipe_f_read;
1984 pipe_files[1] = pipe_f_write;
1988 kref_put(&pipe_f_read->f_kref);
1990 page_decref(kva2page(pii->p_buf));
1992 kfree(pipe_i->i_pipe);
1994 /* We don't need to free the pipe_i; putting the dentry will free it */
1996 /* Note we only free the dentry on failure. */
1997 kref_put(&pipe_d->d_kref);
2001 int do_rename(char *old_path, char *new_path)
2003 struct nameidata nd_old = {0}, *nd_o = &nd_old;
2004 struct nameidata nd_new = {0}, *nd_n = &nd_new;
2005 struct dentry *old_dir_d, *new_dir_d;
2006 struct inode *old_dir_i, *new_dir_i;
2007 struct dentry *old_d, *new_d, *unlink_d;
2012 nd_o->intent = LOOKUP_ACCESS; /* maybe, might need another type */
2014 /* get the parent, but don't follow links */
2015 error = path_lookup(old_path, LOOKUP_PARENT | LOOKUP_DIRECTORY, nd_o);
2021 old_dir_d = nd_o->dentry;
2022 old_dir_i = old_dir_d->d_inode;
2024 old_d = do_lookup(old_dir_d, nd_o->last.name);
2031 nd_n->intent = LOOKUP_CREATE;
2032 error = path_lookup(new_path, LOOKUP_PARENT | LOOKUP_DIRECTORY, nd_n);
2036 goto out_paths_and_src;
2038 new_dir_d = nd_n->dentry;
2039 new_dir_i = new_dir_d->d_inode;
2040 /* TODO if new_dir == old_dir, we might be able to simplify things */
2042 if (new_dir_i->i_sb != old_dir_i->i_sb) {
2045 goto out_paths_and_src;
2047 /* TODO: check_perms is lousy, want to just say "writable" here */
2048 if (check_perms(old_dir_i, S_IWUSR) || check_perms(new_dir_i, S_IWUSR)) {
2051 goto out_paths_and_src;
2053 /* TODO: if we're doing a rename that moves a directory, we need to make
2054 * sure the new_path doesn't include the old_path. it's not as simple as
2055 * just checking, since there could be a concurrent rename that breaks the
2056 * check later. e.g. what if new_dir's parent is being moved into a child
2059 * linux has a per-fs rename mutex for these scenarios, so only one can
2060 * proceed at a time. i don't see another way to deal with it either.
2061 * maybe something like flagging all dentries on the new_path with "do not
2064 /* TODO: this is all very racy. right after we do a new_d lookup, someone
2065 * else could create or unlink new_d. need to lock here, or else push this
2068 * For any locking scheme, we probably need to lock both the old and new
2069 * dirs. To prevent deadlock, we need a total ordering of all inodes (or
2070 * dentries, if we locking them instead). inode number or struct inode*
2071 * will work for this. */
2072 new_d = do_lookup(new_dir_d, nd_n->last.name);
2074 if (new_d->d_inode == old_d->d_inode)
2075 goto out_paths_and_refs; /* rename does nothing */
2076 /* TODO: Here's a bunch of other racy checks we need to do, maybe in the
2079 * if src is a dir, dst must be an empty dir if it exists (RACYx2)
2080 * racing on dst being created and it getting new entries
2081 * if src is a file, dst must be a file if it exists (RACY)
2082 * racing on dst being created and still being a file
2083 * racing on dst being unlinked and a new one being added
2085 /* TODO: we should allow empty dirs */
2086 if (S_ISDIR(new_d->d_inode->i_mode)) {
2089 goto out_paths_and_refs;
2091 /* TODO: need this to be atomic with rename */
2092 error = new_dir_i->i_op->unlink(new_dir_i, new_d);
2096 goto out_paths_and_refs;
2098 new_d->d_flags |= DENTRY_DYING;
2099 /* TODO: racy with other lookups on new_d */
2100 dcache_remove(new_d->d_sb, new_d);
2101 new_d->d_inode->i_nlink--; /* TODO: race here, esp with a decref */
2102 kref_put(&new_d->d_kref);
2104 /* new_d is just a vessel for the name. somewhat lousy. */
2105 new_d = get_dentry(new_dir_d->d_sb, new_dir_d, nd_n->last.name);
2107 /* TODO: more races. need to remove old_d from the dcache, since we're
2108 * about to change its parentage. could be readded concurrently. */
2109 dcache_remove(old_dir_d->d_sb, old_d);
2110 error = new_dir_i->i_op->rename(old_dir_i, old_d, new_dir_i, new_d);
2112 /* TODO: oh crap, we already unlinked! now we're screwed, and violated
2113 * our atomicity requirements. */
2114 printk("[kernel] rename failed, you might have lost data\n");
2117 goto out_paths_and_refs;
2120 /* old_dir loses old_d, new_dir gains old_d, renamed to new_d. this is
2121 * particularly cumbersome since there are two levels here: the FS has its
2122 * info about where things are, and the VFS has its dentry tree. and it's
2123 * all racy (TODO). */
2124 dentry_set_name(old_d, new_d->d_name.name);
2125 old_d->d_parent = new_d->d_parent;
2126 if (S_ISDIR(old_d->d_inode->i_mode)) {
2127 TAILQ_REMOVE(&old_dir_d->d_subdirs, old_d, d_subdirs_link);
2128 old_dir_i->i_nlink--; /* TODO: racy, etc */
2129 TAILQ_INSERT_TAIL(&new_dir_d->d_subdirs, old_d, d_subdirs_link);
2130 new_dir_i->i_nlink--; /* TODO: racy, etc */
2133 /* and then the third level: dcache stuff. we could have old versions of
2134 * old_d or negative versions of new_d sitting around. dcache_put should
2135 * replace a potentially negative dentry for new_d (now called old_d) */
2136 dcache_put(old_dir_d->d_sb, old_d);
2138 /* TODO could have a helper for this, but it's going away soon */
2139 now = epoch_seconds();
2140 old_dir_i->i_ctime.tv_sec = now;
2141 old_dir_i->i_mtime.tv_sec = now;
2142 old_dir_i->i_ctime.tv_nsec = 0;
2143 old_dir_i->i_mtime.tv_nsec = 0;
2144 new_dir_i->i_ctime.tv_sec = now;
2145 new_dir_i->i_mtime.tv_sec = now;
2146 new_dir_i->i_ctime.tv_nsec = 0;
2147 new_dir_i->i_mtime.tv_nsec = 0;
2151 kref_put(&new_d->d_kref);
2153 kref_put(&old_d->d_kref);
2161 int do_truncate(struct inode *inode, off64_t len)
2170 printk("[kernel] truncate for > petabyte, probably a bug\n");
2171 /* continuing, not too concerned. could set EINVAL or EFBIG */
2173 spin_lock(&inode->i_lock);
2174 old_len = inode->i_size;
2175 if (old_len == len) {
2176 spin_unlock(&inode->i_lock);
2179 inode->i_size = len;
2180 /* truncate can't block, since we're holding the spinlock. but it can rely
2181 * on that lock being held */
2182 inode->i_op->truncate(inode);
2183 spin_unlock(&inode->i_lock);
2185 if (old_len < len) {
2186 pm_remove_contig(inode->i_mapping, old_len >> PGSHIFT,
2187 (len >> PGSHIFT) - (old_len >> PGSHIFT));
2189 now = epoch_seconds();
2190 inode->i_ctime.tv_sec = now;
2191 inode->i_mtime.tv_sec = now;
2192 inode->i_ctime.tv_nsec = 0;
2193 inode->i_mtime.tv_nsec = 0;
2197 struct file *alloc_file(void)
2199 struct file *file = kmem_cache_alloc(file_kcache, 0);
2204 /* one for the ref passed out*/
2205 kref_init(&file->f_kref, file_release, 1);
2209 /* Opens and returns the file specified by dentry */
2210 struct file *dentry_open(struct dentry *dentry, int flags)
2212 struct inode *inode;
2215 inode = dentry->d_inode;
2216 /* Do the mode first, since we can still error out. f_mode stores how the
2217 * OS file is open, which can be more restrictive than the i_mode */
2218 switch (flags & (O_RDONLY | O_WRONLY | O_RDWR)) {
2220 desired_mode = S_IRUSR;
2223 desired_mode = S_IWUSR;
2226 desired_mode = S_IRUSR | S_IWUSR;
2231 if (check_perms(inode, desired_mode))
2233 file = alloc_file();
2236 file->f_mode = desired_mode;
2237 /* Add to the list of all files of this SB */
2238 TAILQ_INSERT_TAIL(&inode->i_sb->s_files, file, f_list);
2239 kref_get(&dentry->d_kref, 1);
2240 file->f_dentry = dentry;
2241 kref_get(&inode->i_sb->s_mount->mnt_kref, 1);
2242 file->f_vfsmnt = inode->i_sb->s_mount; /* saving a ref to the vmnt...*/
2243 file->f_op = inode->i_fop;
2244 /* Don't store creation flags */
2245 file->f_flags = flags & ~O_CREAT_FLAGS;
2247 file->f_uid = inode->i_uid;
2248 file->f_gid = inode->i_gid;
2250 // struct event_poll_tailq f_ep_links;
2251 spinlock_init(&file->f_ep_lock);
2252 file->f_privdata = 0; /* prob overriden by the fs */
2253 file->f_mapping = inode->i_mapping;
2254 file->f_op->open(inode, file);
2261 /* Closes a file, fsync, whatever else is necessary. Called when the kref hits
2262 * 0. Note that the file is not refcounted on the s_files list, nor is the
2263 * f_mapping refcounted (it is pinned by the i_mapping). */
2264 void file_release(struct kref *kref)
2266 struct file *file = container_of(kref, struct file, f_kref);
2268 struct super_block *sb = file->f_dentry->d_sb;
2269 spin_lock(&sb->s_lock);
2270 TAILQ_REMOVE(&sb->s_files, file, f_list);
2271 spin_unlock(&sb->s_lock);
2273 /* TODO: fsync (BLK). also, we may want to parallelize the blocking that
2274 * could happen in here (spawn kernel threads)... */
2275 file->f_op->release(file->f_dentry->d_inode, file);
2276 /* Clean up the other refs we hold */
2277 kref_put(&file->f_dentry->d_kref);
2278 kref_put(&file->f_vfsmnt->mnt_kref);
2279 kmem_cache_free(file_kcache, file);
2282 /* Process-related File management functions */
2284 /* Given any FD, get the appropriate file, 0 o/w */
2285 struct file *get_file_from_fd(struct files_struct *open_files, int file_desc)
2287 struct file *retval = 0;
2290 spin_lock(&open_files->lock);
2291 if (open_files->closed) {
2292 spin_unlock(&open_files->lock);
2295 if (file_desc < open_files->max_fdset) {
2296 if (GET_BITMASK_BIT(open_files->open_fds->fds_bits, file_desc)) {
2297 /* while max_files and max_fdset might not line up, we should never
2298 * have a valid fdset higher than files */
2299 assert(file_desc < open_files->max_files);
2300 retval = open_files->fd[file_desc].fd_file;
2301 /* 9ns might be using this one, in which case file == 0 */
2303 kref_get(&retval->f_kref, 1);
2306 spin_unlock(&open_files->lock);
2310 /* Grow the vfs fd set */
2311 static int grow_fd_set(struct files_struct *open_files) {
2313 struct file_desc *nfd, *ofd;
2315 /* Only update open_fds once. If currently pointing to open_fds_init, then
2316 * update it to point to a newly allocated fd_set with space for
2317 * NR_FILE_DESC_MAX */
2318 if (open_files->open_fds == (struct fd_set*)&open_files->open_fds_init) {
2319 open_files->open_fds = kzmalloc(sizeof(struct fd_set), 0);
2320 memmove(open_files->open_fds, &open_files->open_fds_init,
2321 sizeof(struct small_fd_set));
2324 /* Grow the open_files->fd array in increments of NR_OPEN_FILES_DEFAULT */
2325 n = open_files->max_files + NR_OPEN_FILES_DEFAULT;
2326 if (n > NR_FILE_DESC_MAX)
2327 n = NR_FILE_DESC_MAX;
2328 nfd = kzmalloc(n * sizeof(struct file_desc), 0);
2332 /* Move the old array on top of the new one */
2333 ofd = open_files->fd;
2334 memmove(nfd, ofd, open_files->max_files * sizeof(struct file_desc));
2336 /* Update the array and the maxes for both max_files and max_fdset */
2337 open_files->fd = nfd;
2338 open_files->max_files = n;
2339 open_files->max_fdset = n;
2341 /* Only free the old one if it wasn't pointing to open_files->fd_array */
2342 if (ofd != open_files->fd_array)
2347 /* Free the vfs fd set if necessary */
2348 static void free_fd_set(struct files_struct *open_files) {
2349 if (open_files->open_fds != (struct fd_set*)&open_files->open_fds_init) {
2350 kfree(open_files->open_fds);
2351 assert(open_files->fd != open_files->fd_array);
2352 kfree(open_files->fd);
2356 /* 9ns: puts back an FD from the VFS-FD-space. */
2357 int put_fd(struct files_struct *open_files, int file_desc)
2359 if (file_desc < 0) {
2360 warn("Negative FD!\n");
2363 spin_lock(&open_files->lock);
2364 if (file_desc < open_files->max_fdset) {
2365 if (GET_BITMASK_BIT(open_files->open_fds->fds_bits, file_desc)) {
2366 /* while max_files and max_fdset might not line up, we should never
2367 * have a valid fdset higher than files */
2368 assert(file_desc < open_files->max_files);
2369 CLR_BITMASK_BIT(open_files->open_fds->fds_bits, file_desc);
2372 spin_unlock(&open_files->lock);
2376 /* Remove FD from the open files, if it was there, and return f. Currently,
2377 * this decref's f, so the return value is not consumable or even usable. This
2378 * hasn't been thought through yet. */
2379 struct file *put_file_from_fd(struct files_struct *open_files, int file_desc)
2381 struct file *file = 0;
2384 spin_lock(&open_files->lock);
2385 if (file_desc < open_files->max_fdset) {
2386 if (GET_BITMASK_BIT(open_files->open_fds->fds_bits, file_desc)) {
2387 /* while max_files and max_fdset might not line up, we should never
2388 * have a valid fdset higher than files */
2389 assert(file_desc < open_files->max_files);
2390 file = open_files->fd[file_desc].fd_file;
2391 open_files->fd[file_desc].fd_file = 0;
2392 assert(file); /* 9ns shouldn't call this put */
2393 kref_put(&file->f_kref);
2394 CLR_BITMASK_BIT(open_files->open_fds->fds_bits, file_desc);
2397 spin_unlock(&open_files->lock);
2401 static int __get_fd(struct files_struct *open_files, int low_fd)
2404 if ((low_fd < 0) || (low_fd > NR_FILE_DESC_MAX))
2406 if (open_files->closed)
2407 return -EINVAL; /* won't matter, they are dying */
2409 /* Loop until we have a valid slot (we grow the fd_array at the bottom of
2410 * the loop if we haven't found a slot in the current array */
2411 while (slot == -1) {
2412 for (low_fd; low_fd < open_files->max_fdset; low_fd++) {
2413 if (GET_BITMASK_BIT(open_files->open_fds->fds_bits, low_fd))
2416 SET_BITMASK_BIT(open_files->open_fds->fds_bits, slot);
2417 assert(slot < open_files->max_files &&
2418 open_files->fd[slot].fd_file == 0);
2419 if (slot >= open_files->next_fd)
2420 open_files->next_fd = slot + 1;
2424 /* Expand the FD array and fd_set */
2425 if (grow_fd_set(open_files) == -1)
2427 /* loop after growing */
2433 /* Gets and claims a free FD, used by 9ns. < 0 == error. */
2434 int get_fd(struct files_struct *open_files, int low_fd)
2437 spin_lock(&open_files->lock);
2438 slot = __get_fd(open_files, low_fd);
2439 spin_unlock(&open_files->lock);
2443 static int __claim_fd(struct files_struct *open_files, int file_desc)
2445 if ((file_desc < 0) || (file_desc > NR_FILE_DESC_MAX))
2447 if (open_files->closed)
2448 return -EINVAL; /* won't matter, they are dying */
2450 /* Grow the open_files->fd_set until the file_desc can fit inside it */
2451 while(file_desc >= open_files->max_files) {
2452 grow_fd_set(open_files);
2456 /* If we haven't grown, this could be a problem, so check for it */
2457 if (GET_BITMASK_BIT(open_files->open_fds->fds_bits, file_desc))
2458 return -ENFILE; /* Should never really happen. Here to catch bugs. */
2460 SET_BITMASK_BIT(open_files->open_fds->fds_bits, file_desc);
2461 assert(file_desc < open_files->max_files &&
2462 open_files->fd[file_desc].fd_file == 0);
2463 if (file_desc >= open_files->next_fd)
2464 open_files->next_fd = file_desc + 1;
2468 /* Claims a specific FD when duping FDs. used by 9ns. < 0 == error. */
2469 int claim_fd(struct files_struct *open_files, int file_desc)
2472 spin_lock(&open_files->lock);
2473 ret = __claim_fd(open_files, file_desc);
2474 spin_unlock(&open_files->lock);
2478 /* Inserts the file in the files_struct, returning the corresponding new file
2479 * descriptor, or an error code. We start looking for open fds from low_fd. */
2480 int insert_file(struct files_struct *open_files, struct file *file, int low_fd,
2484 spin_lock(&open_files->lock);
2486 ret = __claim_fd(open_files, low_fd);
2488 spin_unlock(&open_files->lock);
2491 assert(!ret); /* issues with claim_fd returning status, not the fd */
2494 slot = __get_fd(open_files, low_fd);
2498 spin_unlock(&open_files->lock);
2501 assert(slot < open_files->max_files &&
2502 open_files->fd[slot].fd_file == 0);
2503 kref_get(&file->f_kref, 1);
2504 open_files->fd[slot].fd_file = file;
2505 open_files->fd[slot].fd_flags = 0;
2506 spin_unlock(&open_files->lock);
2510 /* Closes all open files. Mostly just a "put" for all files. If cloexec, it
2511 * will only close files that are opened with O_CLOEXEC. */
2512 void close_all_files(struct files_struct *open_files, bool cloexec)
2515 spin_lock(&open_files->lock);
2516 if (open_files->closed) {
2517 spin_unlock(&open_files->lock);
2520 for (int i = 0; i < open_files->max_fdset; i++) {
2521 if (GET_BITMASK_BIT(open_files->open_fds->fds_bits, i)) {
2522 /* while max_files and max_fdset might not line up, we should never
2523 * have a valid fdset higher than files */
2524 assert(i < open_files->max_files);
2525 file = open_files->fd[i].fd_file;
2526 /* no file == 9ns uses the FD. they will deal with it */
2529 if (cloexec && !(open_files->fd[i].fd_flags & O_CLOEXEC))
2531 /* Actually close the file */
2532 open_files->fd[i].fd_file = 0;
2534 kref_put(&file->f_kref);
2535 CLR_BITMASK_BIT(open_files->open_fds->fds_bits, i);
2539 free_fd_set(open_files);
2540 open_files->closed = TRUE;
2542 spin_unlock(&open_files->lock);
2545 /* Inserts all of the files from src into dst, used by sys_fork(). */
2546 void clone_files(struct files_struct *src, struct files_struct *dst)
2549 spin_lock(&src->lock);
2551 spin_unlock(&src->lock);
2554 spin_lock(&dst->lock);
2556 warn("Destination closed before it opened");
2557 spin_unlock(&dst->lock);
2558 spin_unlock(&src->lock);
2561 for (int i = 0; i < src->max_fdset; i++) {
2562 if (GET_BITMASK_BIT(src->open_fds->fds_bits, i)) {
2563 /* while max_files and max_fdset might not line up, we should never
2564 * have a valid fdset higher than files */
2565 assert(i < src->max_files);
2566 file = src->fd[i].fd_file;
2567 assert(i < dst->max_files && dst->fd[i].fd_file == 0);
2568 SET_BITMASK_BIT(dst->open_fds->fds_bits, i);
2569 dst->fd[i].fd_file = file;
2570 /* no file means 9ns is using it, they clone separately */
2572 kref_get(&file->f_kref, 1);
2573 if (i >= dst->next_fd)
2574 dst->next_fd = i + 1;
2577 spin_unlock(&dst->lock);
2578 spin_unlock(&src->lock);
2581 static void __chpwd(struct fs_struct *fs_env, struct dentry *new_pwd)
2583 struct dentry *old_pwd;
2584 kref_get(&new_pwd->d_kref, 1);
2585 /* writer lock, make sure we replace pwd with ours. could also CAS.
2586 * readers don't lock at all, so they need to either loop, or we need to
2587 * delay releasing old_pwd til an RCU grace period. */
2588 spin_lock(&fs_env->lock);
2589 old_pwd = fs_env->pwd;
2590 fs_env->pwd = new_pwd;
2591 spin_unlock(&fs_env->lock);
2592 kref_put(&old_pwd->d_kref);
2595 /* Change the working directory of the given fs env (one per process, at this
2596 * point). Returns 0 for success, sets errno and returns -1 otherwise. */
2597 int do_chdir(struct fs_struct *fs_env, char *path)
2599 struct nameidata nd_r = {0}, *nd = &nd_r;
2601 error = path_lookup(path, LOOKUP_DIRECTORY, nd);
2607 /* nd->dentry is the place we want our PWD to be */
2608 __chpwd(fs_env, nd->dentry);
2613 int do_fchdir(struct fs_struct *fs_env, struct file *file)
2615 if ((file->f_dentry->d_inode->i_mode & __S_IFMT) != __S_IFDIR) {
2619 __chpwd(fs_env, file->f_dentry);
2623 /* Returns a null-terminated string of up to length cwd_l containing the
2624 * absolute path of fs_env, (up to fs_env's root). Be sure to kfree the char*
2625 * "kfree_this" when you are done with it. We do this since it's easier to
2626 * build this string going backwards. Note cwd_l is not a strlen, it's an
2628 char *do_getcwd(struct fs_struct *fs_env, char **kfree_this, size_t cwd_l)
2630 struct dentry *dentry = fs_env->pwd;
2632 char *path_start, *kbuf;
2638 kbuf = kmalloc(cwd_l, 0);
2644 kbuf[cwd_l - 1] = '\0';
2645 kbuf[cwd_l - 2] = '/';
2646 /* for each dentry in the path, all the way back to the root of fs_env, we
2647 * grab the dentry name, push path_start back enough, and write in the name,
2648 * using /'s to terminate. We skip the root, since we don't want it's
2649 * actual name, just "/", which is set before each loop. */
2650 path_start = kbuf + cwd_l - 2; /* the last byte written */
2651 while (dentry != fs_env->root) {
2652 link_len = dentry->d_name.len; /* this does not count the \0 */
2653 if (path_start - (link_len + 2) < kbuf) {
2658 path_start -= link_len;
2659 strncpy(path_start, dentry->d_name.name, link_len);
2662 dentry = dentry->d_parent;
2667 static void print_dir(struct dentry *dentry, char *buf, int depth)
2669 struct dentry *child_d;
2670 struct dirent next = {0};
2674 if (!S_ISDIR(dentry->d_inode->i_mode)) {
2675 warn("Thought this was only directories!!");
2678 /* Print this dentry */
2679 printk("%s%s/ nlink: %d\n", buf, dentry->d_name.name,
2680 dentry->d_inode->i_nlink);
2681 if (dentry->d_mount_point) {
2682 dentry = dentry->d_mounted_fs->mnt_root;
2686 /* Set buffer for our kids */
2688 dir = dentry_open(dentry, 0);
2690 panic("Filesystem seems inconsistent - unable to open a dir!");
2691 /* Process every child, recursing on directories */
2693 retval = dir->f_op->readdir(dir, &next);
2695 /* Skip .., ., and empty entries */
2696 if (!strcmp("..", next.d_name) || !strcmp(".", next.d_name) ||
2699 /* there is an entry, now get its dentry */
2700 child_d = do_lookup(dentry, next.d_name);
2702 panic("Inconsistent FS, dirent doesn't have a dentry!");
2703 /* Recurse for directories, or just print the name for others */
2704 switch (child_d->d_inode->i_mode & __S_IFMT) {
2706 print_dir(child_d, buf, depth + 1);
2709 printk("%s%s size(B): %d nlink: %d\n", buf, next.d_name,
2710 child_d->d_inode->i_size, child_d->d_inode->i_nlink);
2713 printk("%s%s -> %s\n", buf, next.d_name,
2714 child_d->d_inode->i_op->readlink(child_d));
2717 printk("%s%s (char device) nlink: %d\n", buf, next.d_name,
2718 child_d->d_inode->i_nlink);
2721 printk("%s%s (block device) nlink: %d\n", buf, next.d_name,
2722 child_d->d_inode->i_nlink);
2725 warn("Look around you! Unknown filetype!");
2727 kref_put(&child_d->d_kref);
2733 /* Reset buffer to the way it was */
2735 kref_put(&dir->f_kref);
2739 int ls_dash_r(char *path)
2741 struct nameidata nd_r = {0}, *nd = &nd_r;
2745 error = path_lookup(path, LOOKUP_ACCESS | LOOKUP_DIRECTORY, nd);
2750 print_dir(nd->dentry, buf, 0);
2755 /* Dummy ops, to catch weird operations we weren't expecting */
2756 int dummy_create(struct inode *dir, struct dentry *dentry, int mode,
2757 struct nameidata *nd)
2759 printk("Dummy VFS function %s called!\n", __FUNCTION__);
2763 struct dentry *dummy_lookup(struct inode *dir, struct dentry *dentry,
2764 struct nameidata *nd)
2766 printk("Dummy VFS function %s called!\n", __FUNCTION__);
2770 int dummy_link(struct dentry *old_dentry, struct inode *dir,
2771 struct dentry *new_dentry)
2773 printk("Dummy VFS function %s called!\n", __FUNCTION__);
2777 int dummy_unlink(struct inode *dir, struct dentry *dentry)
2779 printk("Dummy VFS function %s called!\n", __FUNCTION__);
2783 int dummy_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
2785 printk("Dummy VFS function %s called!\n", __FUNCTION__);
2789 int dummy_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2791 printk("Dummy VFS function %s called!\n", __FUNCTION__);
2795 int dummy_rmdir(struct inode *dir, struct dentry *dentry)
2797 printk("Dummy VFS function %s called!\n", __FUNCTION__);
2801 int dummy_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
2803 printk("Dummy VFS function %s called!\n", __FUNCTION__);
2807 int dummy_rename(struct inode *old_dir, struct dentry *old_dentry,
2808 struct inode *new_dir, struct dentry *new_dentry)
2810 printk("Dummy VFS function %s called!\n", __FUNCTION__);
2814 char *dummy_readlink(struct dentry *dentry)
2816 printk("Dummy VFS function %s called!\n", __FUNCTION__);
2820 void dummy_truncate(struct inode *inode)
2822 printk("Dummy VFS function %s called!\n", __FUNCTION__);
2825 int dummy_permission(struct inode *inode, int mode, struct nameidata *nd)
2827 printk("Dummy VFS function %s called!\n", __FUNCTION__);
2831 int dummy_d_revalidate(struct dentry *dir, struct nameidata *nd)
2833 printk("Dummy VFS function %s called!\n", __FUNCTION__);
2837 int dummy_d_hash(struct dentry *dentry, struct qstr *name)
2839 printk("Dummy VFS function %s called!\n", __FUNCTION__);
2843 int dummy_d_compare(struct dentry *dir, struct qstr *name1, struct qstr *name2)
2845 printk("Dummy VFS function %s called!\n", __FUNCTION__);
2849 int dummy_d_delete(struct dentry *dentry)
2851 printk("Dummy VFS function %s called!\n", __FUNCTION__);
2855 int dummy_d_release(struct dentry *dentry)
2857 printk("Dummy VFS function %s called!\n", __FUNCTION__);
2861 void dummy_d_iput(struct dentry *dentry, struct inode *inode)
2863 printk("Dummy VFS function %s called!\n", __FUNCTION__);
2866 struct inode_operations dummy_i_op = {
2881 struct dentry_operations dummy_d_op = {