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 /* FS's can provide another, if they want */
116 int generic_dentry_hash(struct dentry *dentry, struct qstr *qstr)
118 unsigned long hash = 5381;
120 for (int i = 0; i < qstr->len; i++) {
121 /* hash * 33 + c, djb2's technique */
122 hash = ((hash << 5) + hash) + qstr->name[i];
127 /* Builds / populates the qstr of a dentry based on its d_iname. If there is an
128 * l_name, (long), it will use that instead of the inline name. This will
129 * probably change a bit. */
130 void qstr_builder(struct dentry *dentry, char *l_name)
132 dentry->d_name.name = l_name ? l_name : dentry->d_iname;
133 dentry->d_name.len = strnlen(dentry->d_name.name, MAX_FILENAME_SZ);
134 dentry->d_name.hash = dentry->d_op->d_hash(dentry, &dentry->d_name);
137 /* Useful little helper - return the string ptr for a given file */
138 char *file_name(struct file *file)
140 return file->f_dentry->d_name.name;
143 /* Some issues with this, coupled closely to fs_lookup.
145 * Note the use of __dentry_free, instead of kref_put. In those cases, we don't
146 * want to treat it like a kref and we have the only reference to it, so it is
147 * okay to do this. It makes dentry_release() easier too. */
148 static struct dentry *do_lookup(struct dentry *parent, char *name)
150 struct dentry *result, *query;
151 query = get_dentry(parent->d_sb, parent, name);
153 warn("OOM in do_lookup(), probably wasn't expected\n");
156 result = dcache_get(parent->d_sb, query);
158 __dentry_free(query);
161 /* No result, check for negative */
162 if (query->d_flags & DENTRY_NEGATIVE) {
163 __dentry_free(query);
166 /* not in the dcache at all, need to consult the FS */
167 result = parent->d_inode->i_op->lookup(parent->d_inode, query, 0);
169 /* Note the USED flag will get turned off when this gets added to the
170 * LRU in dentry_release(). There's a slight race here that we'll panic
171 * on, but I want to catch it (in dcache_put()) for now. */
172 query->d_flags |= DENTRY_NEGATIVE;
173 dcache_put(parent->d_sb, query);
174 kref_put(&query->d_kref);
177 dcache_put(parent->d_sb, result);
178 /* This is because KFS doesn't return the same dentry, but ext2 does. this
179 * is ugly and needs to be fixed. (TODO) */
181 __dentry_free(query);
183 /* TODO: if the following are done by us, how do we know the i_ino?
184 * also need to handle inodes that are already read in! For now, we're
185 * going to have the FS handle it in it's lookup() method:
187 * - read in the inode
188 * - put in the inode cache */
192 /* Update ND such that it represents having followed dentry. IAW the nd
193 * refcnting rules, we need to decref any references that were in there before
194 * they get clobbered. */
195 static int next_link(struct dentry *dentry, struct nameidata *nd)
197 assert(nd->dentry && nd->mnt);
198 /* update the dentry */
199 kref_get(&dentry->d_kref, 1);
200 kref_put(&nd->dentry->d_kref);
202 /* update the mount, if we need to */
203 if (dentry->d_sb->s_mount != nd->mnt) {
204 kref_get(&dentry->d_sb->s_mount->mnt_kref, 1);
205 kref_put(&nd->mnt->mnt_kref);
206 nd->mnt = dentry->d_sb->s_mount;
211 /* Walk up one directory, being careful of mountpoints, namespaces, and the top
213 static int climb_up(struct nameidata *nd)
215 printd("CLIMB_UP, from %s\n", nd->dentry->d_name.name);
216 /* Top of the world, just return. Should also check for being at the top of
217 * the current process's namespace (TODO) */
218 if (!nd->dentry->d_parent || (nd->dentry->d_parent == nd->dentry))
220 /* Check if we are at the top of a mount, if so, we need to follow
221 * backwards, and then climb_up from that one. We might need to climb
222 * multiple times if we mount multiple FSs at the same spot (highly
223 * unlikely). This is completely untested. Might recurse instead. */
224 while (nd->mnt->mnt_root == nd->dentry) {
225 if (!nd->mnt->mnt_parent) {
226 warn("Might have expected a parent vfsmount (dentry had a parent)");
229 next_link(nd->mnt->mnt_mountpoint, nd);
231 /* Backwards walk (no mounts or any other issues now). */
232 next_link(nd->dentry->d_parent, nd);
233 printd("CLIMB_UP, to %s\n", nd->dentry->d_name.name);
237 /* nd->dentry might be on a mount point, so we need to move on to the child
239 static int follow_mount(struct nameidata *nd)
241 if (!nd->dentry->d_mount_point)
243 next_link(nd->dentry->d_mounted_fs->mnt_root, nd);
247 static int link_path_walk(char *path, struct nameidata *nd);
249 /* When nd->dentry is for a symlink, this will recurse and follow that symlink,
250 * so that nd contains the results of following the symlink (dentry and mnt).
251 * Returns when it isn't a symlink, 1 on following a link, and < 0 on error. */
252 static int follow_symlink(struct nameidata *nd)
256 if (!S_ISLNK(nd->dentry->d_inode->i_mode))
258 if (nd->depth > MAX_SYMLINK_DEPTH)
260 printd("Following symlink for dentry %p %s\n", nd->dentry,
261 nd->dentry->d_name.name);
263 symname = nd->dentry->d_inode->i_op->readlink(nd->dentry);
264 /* We need to pin in nd->dentry (the dentry of the symlink), since we need
265 * it's symname's storage to stay in memory throughout the upcoming
266 * link_path_walk(). The last_sym gets decreffed when we path_release() or
267 * follow another symlink. */
269 kref_put(&nd->last_sym->d_kref);
270 kref_get(&nd->dentry->d_kref, 1);
271 nd->last_sym = nd->dentry;
272 /* If this an absolute path in the symlink, we need to free the old path and
273 * start over, otherwise, we continue from the PARENT of nd (the symlink) */
274 if (symname[0] == '/') {
277 nd->dentry = default_ns.root->mnt_root;
279 nd->dentry = current->fs_env.root;
280 nd->mnt = nd->dentry->d_sb->s_mount;
281 kref_get(&nd->mnt->mnt_kref, 1);
282 kref_get(&nd->dentry->d_kref, 1);
286 /* either way, keep on walking in the free world! */
287 retval = link_path_walk(symname, nd);
288 return (retval == 0 ? 1 : retval);
291 /* Little helper, to make it easier to break out of the nested loops. Will also
292 * '\0' out the first slash if it's slashes all the way down. Or turtles. */
293 static bool packed_trailing_slashes(char *first_slash)
295 for (char *i = first_slash; *i == '/'; i++) {
296 if (*(i + 1) == '\0') {
304 /* Simple helper to set nd to track it's last name to be Name. Also be careful
305 * with the storage of name. Don't use and nd's name past the lifetime of the
306 * string used in the path_lookup()/link_path_walk/whatever. Consider replacing
307 * parts of this with a qstr builder. Note this uses the dentry's d_op, which
308 * might not be the dentry we care about. */
309 static void stash_nd_name(struct nameidata *nd, char *name)
311 nd->last.name = name;
312 nd->last.len = strlen(name);
313 nd->last.hash = nd->dentry->d_op->d_hash(nd->dentry, &nd->last);
316 /* Resolves the links in a basic path walk. 0 for success, -EWHATEVER
317 * otherwise. The final lookup is returned via nd. */
318 static int link_path_walk(char *path, struct nameidata *nd)
320 struct dentry *link_dentry;
321 struct inode *link_inode, *nd_inode;
326 /* Prevent crazy recursion */
327 if (nd->depth > MAX_SYMLINK_DEPTH)
329 /* skip all leading /'s */
332 /* if there's nothing left (null terminated), we're done. This should only
333 * happen for "/", which if we wanted a PARENT, should fail (there is no
336 if (nd->flags & LOOKUP_PARENT) {
340 /* o/w, we're good */
343 /* iterate through each intermediate link of the path. in general, nd
344 * tracks where we are in the path, as far as dentries go. once we have the
345 * next dentry, we try to update nd based on that dentry. link is the part
346 * of the path string that we are looking up */
348 nd_inode = nd->dentry->d_inode;
349 if ((error = check_perms(nd_inode, nd->intent)))
351 /* find the next link, break out if it is the end */
352 next_slash = strchr(link, '/');
356 if (packed_trailing_slashes(next_slash)) {
357 nd->flags |= LOOKUP_DIRECTORY;
361 /* skip over any interim ./ */
362 if (!strncmp("./", link, 2))
364 /* Check for "../", walk up */
365 if (!strncmp("../", link, 3)) {
370 link_dentry = do_lookup(nd->dentry, link);
374 /* make link_dentry the current step/answer */
375 next_link(link_dentry, nd);
376 kref_put(&link_dentry->d_kref); /* do_lookup gave us a refcnt dentry */
377 /* we could be on a mountpoint or a symlink - need to follow them */
379 if ((error = follow_symlink(nd)) < 0)
381 /* Turn off a possible DIRECTORY lookup, which could have been set
382 * during the follow_symlink (a symlink could have had a directory at
383 * the end), though it was in the middle of the real path. */
384 nd->flags &= ~LOOKUP_DIRECTORY;
385 if (!S_ISDIR(nd->dentry->d_inode->i_mode))
388 /* move through the path string to the next entry */
389 link = next_slash + 1;
390 /* advance past any other interim slashes. we know we won't hit the end
391 * due to the for loop check above */
395 /* Now, we're on the last link of the path. We need to deal with with . and
396 * .. . This might be weird with PARENT lookups - not sure what semantics
397 * we want exactly. This will give the parent of whatever the PATH was
398 * supposed to look like. Note that ND currently points to the parent of
399 * the last item (link). */
400 if (!strcmp(".", link)) {
401 if (nd->flags & LOOKUP_PARENT) {
402 assert(nd->dentry->d_name.name);
403 stash_nd_name(nd, nd->dentry->d_name.name);
408 if (!strcmp("..", link)) {
410 if (nd->flags & LOOKUP_PARENT) {
411 assert(nd->dentry->d_name.name);
412 stash_nd_name(nd, nd->dentry->d_name.name);
417 /* need to attempt to look it up, in case it's a symlink */
418 link_dentry = do_lookup(nd->dentry, link);
420 /* if there's no dentry, we are okay if we are looking for the parent */
421 if (nd->flags & LOOKUP_PARENT) {
422 assert(strcmp(link, ""));
423 stash_nd_name(nd, link);
429 next_link(link_dentry, nd);
430 kref_put(&link_dentry->d_kref); /* do_lookup gave us a refcnt'd dentry */
431 /* at this point, nd is on the final link, but it might be a symlink */
432 if (nd->flags & LOOKUP_FOLLOW) {
433 error = follow_symlink(nd);
436 /* if we actually followed a symlink, then nd is set and we're done */
440 /* One way or another, nd is on the last element of the path, symlinks and
441 * all. Now we need to climb up to set nd back on the parent, if that's
443 if (nd->flags & LOOKUP_PARENT) {
444 assert(nd->dentry->d_name.name);
445 stash_nd_name(nd, link_dentry->d_name.name);
449 /* now, we have the dentry set, and don't want the parent, but might be on a
450 * mountpoint still. FYI: this hasn't been thought through completely. */
452 /* If we wanted a directory, but didn't get one, error out */
453 if ((nd->flags & LOOKUP_DIRECTORY) && !S_ISDIR(nd->dentry->d_inode->i_mode))
458 /* Given path, return the inode for the final dentry. The ND should be
459 * initialized for the first call - specifically, we need the intent.
460 * LOOKUP_PARENT and friends go in the flags var, which is not the intent.
462 * If path_lookup wants a PARENT, but hits the top of the FS (root or
463 * otherwise), we want it to error out. It's still unclear how we want to
464 * handle processes with roots that aren't root, but at the very least, we don't
465 * want to think we have the parent of /, but have / itself. Due to the way
466 * link_path_walk works, if that happened, we probably don't have a
467 * nd->last.name. This needs more thought (TODO).
469 * Need to be careful too. While the path has been copied-in to the kernel,
470 * it's still user input. */
471 int path_lookup(char *path, int flags, struct nameidata *nd)
474 printd("Path lookup for %s\n", path);
475 /* we allow absolute lookups with no process context */
476 /* TODO: RCU read lock on pwd or kref_not_zero in a loop. concurrent chdir
477 * could decref nd->dentry before we get to incref it below. */
478 if (path[0] == '/') { /* absolute lookup */
480 nd->dentry = default_ns.root->mnt_root;
482 nd->dentry = current->fs_env.root;
483 } else { /* relative lookup */
485 /* Don't need to lock on the fs_env since we're reading one item */
486 nd->dentry = current->fs_env.pwd;
488 nd->mnt = nd->dentry->d_sb->s_mount;
489 /* Whenever references get put in the nd, incref them. Whenever they are
490 * removed, decref them. */
491 kref_get(&nd->mnt->mnt_kref, 1);
492 kref_get(&nd->dentry->d_kref, 1);
494 nd->depth = 0; /* used in symlink following */
495 retval = link_path_walk(path, nd);
496 /* make sure our PARENT lookup worked */
497 if (!retval && (flags & LOOKUP_PARENT))
498 assert(nd->last.name);
502 /* Call this after any use of path_lookup when you are done with its results,
503 * regardless of whether it succeeded or not. It will free any references */
504 void path_release(struct nameidata *nd)
506 kref_put(&nd->dentry->d_kref);
507 kref_put(&nd->mnt->mnt_kref);
508 /* Free the last symlink dentry used, if there was one */
510 kref_put(&nd->last_sym->d_kref);
511 nd->last_sym = 0; /* catch reuse bugs */
515 /* External version of mount, only call this after having a / mount */
516 int mount_fs(struct fs_type *fs, char *dev_name, char *path, int flags)
518 struct nameidata nd_r = {0}, *nd = &nd_r;
520 retval = path_lookup(path, LOOKUP_DIRECTORY, nd);
523 /* taking the namespace of the vfsmount of path */
524 if (!__mount_fs(fs, dev_name, nd->dentry, flags, nd->mnt->mnt_namespace))
531 /* Superblock functions */
533 /* Dentry "hash" function for the hash table to use. Since we already have the
534 * hash in the qstr, we don't need to rehash. Also, note we'll be using the
535 * dentry in question as both the key and the value. */
536 static size_t __dcache_hash(void *k)
538 return (size_t)((struct dentry*)k)->d_name.hash;
541 /* Dentry cache hashtable equality function. This means we need to pass in some
542 * minimal dentry when doing a lookup. */
543 static ssize_t __dcache_eq(void *k1, void *k2)
545 if (((struct dentry*)k1)->d_parent != ((struct dentry*)k2)->d_parent)
547 /* TODO: use the FS-specific string comparison */
548 return !strcmp(((struct dentry*)k1)->d_name.name,
549 ((struct dentry*)k2)->d_name.name);
552 /* Helper to alloc and initialize a generic superblock. This handles all the
553 * VFS related things, like lists. Each FS will need to handle its own things
554 * in it's *_get_sb(), usually involving reading off the disc. */
555 struct super_block *get_sb(void)
557 struct super_block *sb = kmalloc(sizeof(struct super_block), 0);
559 spinlock_init(&sb->s_lock);
560 kref_init(&sb->s_kref, fake_release, 1); /* for the ref passed out */
561 TAILQ_INIT(&sb->s_inodes);
562 TAILQ_INIT(&sb->s_dirty_i);
563 TAILQ_INIT(&sb->s_io_wb);
564 TAILQ_INIT(&sb->s_lru_d);
565 TAILQ_INIT(&sb->s_files);
566 sb->s_dcache = create_hashtable(100, __dcache_hash, __dcache_eq);
567 sb->s_icache = create_hashtable(100, __generic_hash, __generic_eq);
568 spinlock_init(&sb->s_lru_lock);
569 spinlock_init(&sb->s_dcache_lock);
570 spinlock_init(&sb->s_icache_lock);
571 sb->s_fs_info = 0; // can override somewhere else
575 /* Final stages of initializing a super block, including creating and linking
576 * the root dentry, root inode, vmnt, and sb. The d_op and root_ino are
577 * FS-specific, but otherwise it's FS-independent, tricky, and not worth having
578 * around multiple times.
580 * Not the world's best interface, so it's subject to change, esp since we're
581 * passing (now 3) FS-specific things. */
582 void init_sb(struct super_block *sb, struct vfsmount *vmnt,
583 struct dentry_operations *d_op, unsigned long root_ino,
586 /* Build and init the first dentry / inode. The dentry ref is stored later
587 * by vfsmount's mnt_root. The parent is dealt with later. */
588 struct dentry *d_root = get_dentry_with_ops(sb, 0, "/", d_op);
591 panic("OOM! init_sb() can't fail yet!");
592 /* a lot of here on down is normally done in lookup() or create, since
593 * get_dentry isn't a fully usable dentry. The two FS-specific settings are
594 * normally inherited from a parent within the same FS in get_dentry, but we
597 d_root->d_fs_info = d_fs_info;
598 struct inode *inode = get_inode(d_root);
600 panic("This FS sucks!");
601 inode->i_ino = root_ino;
602 /* TODO: add the inode to the appropriate list (off i_list) */
603 /* TODO: do we need to read in the inode? can we do this on demand? */
604 /* if this FS is already mounted, we'll need to do something different. */
605 sb->s_op->read_inode(inode);
606 icache_put(sb, inode);
607 /* Link the dentry and SB to the VFS mount */
608 vmnt->mnt_root = d_root; /* ref comes from get_dentry */
610 /* If there is no mount point, there is no parent. This is true only for
612 if (vmnt->mnt_mountpoint) {
613 kref_get(&vmnt->mnt_mountpoint->d_kref, 1); /* held by d_root */
614 d_root->d_parent = vmnt->mnt_mountpoint; /* dentry of the root */
616 d_root->d_parent = d_root; /* set root as its own parent */
618 /* insert the dentry into the dentry cache. when's the earliest we can?
619 * when's the earliest we should? what about concurrent accesses to the
620 * same dentry? should be locking the dentry... */
621 dcache_put(sb, d_root);
622 kref_put(&inode->i_kref); /* give up the ref from get_inode() */
625 /* Dentry Functions */
627 static void dentry_set_name(struct dentry *dentry, char *name)
629 size_t name_len = strnlen(name, MAX_FILENAME_SZ); /* not including \0! */
631 if (name_len < DNAME_INLINE_LEN) {
632 strncpy(dentry->d_iname, name, name_len);
633 dentry->d_iname[name_len] = '\0';
634 qstr_builder(dentry, 0);
636 l_name = kmalloc(name_len + 1, 0);
638 strncpy(l_name, name, name_len);
639 l_name[name_len] = '\0';
640 qstr_builder(dentry, l_name);
644 /* Gets a dentry. If there is no parent, use d_op. Only called directly by
645 * superblock init code. */
646 struct dentry *get_dentry_with_ops(struct super_block *sb,
647 struct dentry *parent, char *name,
648 struct dentry_operations *d_op)
651 struct dentry *dentry = kmem_cache_alloc(dentry_kcache, 0);
657 //memset(dentry, 0, sizeof(struct dentry));
658 kref_init(&dentry->d_kref, dentry_release, 1); /* this ref is returned */
659 spinlock_init(&dentry->d_lock);
660 TAILQ_INIT(&dentry->d_subdirs);
662 kref_get(&sb->s_kref, 1);
663 dentry->d_sb = sb; /* storing a ref here... */
664 dentry->d_mount_point = FALSE;
665 dentry->d_mounted_fs = 0;
666 if (parent) { /* no parent for rootfs mount */
667 kref_get(&parent->d_kref, 1);
668 dentry->d_op = parent->d_op; /* d_op set in init_sb for parentless */
672 dentry->d_parent = parent;
673 dentry->d_flags = DENTRY_USED;
674 dentry->d_fs_info = 0;
675 dentry_set_name(dentry, name);
676 /* Catch bugs by aggressively zeroing this (o/w we use old stuff) */
681 /* Helper to alloc and initialize a generic dentry. The following needs to be
682 * set still: d_op (if no parent), d_fs_info (opt), d_inode, connect the inode
683 * to the dentry (and up the d_kref again), maybe dcache_put(). The inode
684 * stitching is done in get_inode() or lookup (depending on the FS).
685 * The setting of the d_op might be problematic when dealing with mounts. Just
688 * If the name is longer than the inline name, it will kmalloc a buffer, so
689 * don't worry about the storage for *name after calling this. */
690 struct dentry *get_dentry(struct super_block *sb, struct dentry *parent,
693 return get_dentry_with_ops(sb, parent, name, 0);
696 /* Called when the dentry is unreferenced (after kref == 0). This works closely
697 * with the resurrection in dcache_get().
699 * The dentry is still in the dcache, but needs to be un-USED and added to the
700 * LRU dentry list. Even dentries that were used in a failed lookup need to be
701 * cached - they ought to be the negative dentries. Note that all dentries have
702 * parents, even negative ones (it is needed to find it in the dcache). */
703 void dentry_release(struct kref *kref)
705 struct dentry *dentry = container_of(kref, struct dentry, d_kref);
707 printd("'Releasing' dentry %p: %s\n", dentry, dentry->d_name.name);
708 /* DYING dentries (recently unlinked / rmdir'd) just get freed */
709 if (dentry->d_flags & DENTRY_DYING) {
710 __dentry_free(dentry);
713 /* This lock ensures the USED state and the TAILQ membership is in sync.
714 * Also used to check the refcnt, though that might not be necessary. */
715 spin_lock(&dentry->d_lock);
716 /* While locked, we need to double check the kref, in case someone already
717 * reup'd it. Re-up? you're crazy! Reee-up, you're outta yo mind! */
718 if (!kref_refcnt(&dentry->d_kref)) {
719 /* Note this is where negative dentries get set UNUSED */
720 if (dentry->d_flags & DENTRY_USED) {
721 dentry->d_flags &= ~DENTRY_USED;
722 spin_lock(&dentry->d_sb->s_lru_lock);
723 TAILQ_INSERT_TAIL(&dentry->d_sb->s_lru_d, dentry, d_lru);
724 spin_unlock(&dentry->d_sb->s_lru_lock);
726 /* and make sure it wasn't USED, then UNUSED again */
727 /* TODO: think about issues with this */
728 warn("This should be rare. Tell brho this happened.");
731 spin_unlock(&dentry->d_lock);
734 /* Called when we really dealloc and get rid of a dentry (like when it is
735 * removed from the dcache, either for memory or correctness reasons)
737 * This has to handle two types of dentries: full ones (ones that had been used)
738 * and ones that had been just for lookups - hence the check for d_inode.
740 * Note that dentries pin and kref their inodes. When all the dentries are
741 * gone, we want the inode to be released via kref. The inode has internal /
742 * weak references to the dentry, which are not refcounted. */
743 void __dentry_free(struct dentry *dentry)
746 printd("Freeing dentry %p: %s\n", dentry, dentry->d_name.name);
747 assert(dentry->d_op); /* catch bugs. a while back, some lacked d_op */
748 dentry->d_op->d_release(dentry);
749 /* TODO: check/test the boundaries on this. */
750 if (dentry->d_name.len > DNAME_INLINE_LEN)
751 kfree((void*)dentry->d_name.name);
752 kref_put(&dentry->d_sb->s_kref);
753 if (dentry->d_parent)
754 kref_put(&dentry->d_parent->d_kref);
755 if (dentry->d_mounted_fs)
756 kref_put(&dentry->d_mounted_fs->mnt_kref);
757 if (dentry->d_inode) {
758 TAILQ_REMOVE(&dentry->d_inode->i_dentry, dentry, d_alias);
759 kref_put(&dentry->d_inode->i_kref); /* dentries kref inodes */
761 kmem_cache_free(dentry_kcache, dentry);
764 /* Looks up the dentry for the given path, returning a refcnt'd dentry (or 0).
765 * Permissions are applied for the current user, which is quite a broken system
766 * at the moment. Flags are lookup flags. */
767 struct dentry *lookup_dentry(char *path, int flags)
769 struct dentry *dentry;
770 struct nameidata nd_r = {0}, *nd = &nd_r;
773 error = path_lookup(path, flags, nd);
780 kref_get(&dentry->d_kref, 1);
785 /* Get a dentry from the dcache. At a minimum, we need the name hash and parent
786 * in what_i_want, though most uses will probably be from a get_dentry() call.
787 * We pass in the SB in the off chance that we don't want to use a get'd dentry.
789 * The unusual variable name (instead of just "key" or something) is named after
790 * ex-SPC Castro's porn folder. Caller deals with the memory for what_i_want.
792 * If the dentry is negative, we don't return the actual result - instead, we
793 * set the negative flag in 'what i want'. The reason is we don't want to
794 * kref_get() and then immediately put (causing dentry_release()). This also
795 * means that dentry_release() should never get someone who wasn't USED (barring
796 * the race, which it handles). And we don't need to ever have a dentry set as
797 * USED and NEGATIVE (which is always wrong, but would be needed for a cleaner
800 * This is where we do the "kref resurrection" - we are returning a kref'd
801 * object, even if it wasn't kref'd before. This means the dcache does NOT hold
802 * krefs (it is a weak/internal ref), but it is a source of kref generation. We
803 * sync up with the possible freeing of the dentry by locking the table. See
804 * Doc/kref for more info. */
805 struct dentry *dcache_get(struct super_block *sb, struct dentry *what_i_want)
807 struct dentry *found;
808 /* This lock protects the hash, as well as ensures the returned object
809 * doesn't get deleted/freed out from under us */
810 spin_lock(&sb->s_dcache_lock);
811 found = hashtable_search(sb->s_dcache, what_i_want);
813 if (found->d_flags & DENTRY_NEGATIVE) {
814 what_i_want->d_flags |= DENTRY_NEGATIVE;
815 spin_unlock(&sb->s_dcache_lock);
818 spin_lock(&found->d_lock);
819 __kref_get(&found->d_kref, 1); /* prob could be done outside the lock*/
820 /* If we're here (after kreffing) and it is not USED, we are the one who
821 * should resurrect */
822 if (!(found->d_flags & DENTRY_USED)) {
823 found->d_flags |= DENTRY_USED;
824 spin_lock(&sb->s_lru_lock);
825 TAILQ_REMOVE(&sb->s_lru_d, found, d_lru);
826 spin_unlock(&sb->s_lru_lock);
828 spin_unlock(&found->d_lock);
830 spin_unlock(&sb->s_dcache_lock);
834 /* Adds a dentry to the dcache. Note the *dentry is both the key and the value.
835 * If the value was already in there (which can happen iff it was negative), for
836 * now we'll remove it and put the new one in there. */
837 void dcache_put(struct super_block *sb, struct dentry *key_val)
841 spin_lock(&sb->s_dcache_lock);
842 old = hashtable_remove(sb->s_dcache, key_val);
843 /* if it is old and non-negative, our caller lost a race with someone else
844 * adding the dentry. but since we yanked it out, like a bunch of idiots,
845 * we still have to put it back. should be fairly rare. */
846 if (old && (old->d_flags & DENTRY_NEGATIVE)) {
847 /* This is possible, but rare for now (about to be put on the LRU) */
848 assert(!(old->d_flags & DENTRY_USED));
849 assert(!kref_refcnt(&old->d_kref));
850 spin_lock(&sb->s_lru_lock);
851 TAILQ_REMOVE(&sb->s_lru_d, old, d_lru);
852 spin_unlock(&sb->s_lru_lock);
853 /* TODO: this seems suspect. isn't this the same memory as key_val?
854 * in which case, we just adjust the flags (remove NEG) and reinsert? */
855 assert(old != key_val); // checking TODO comment
858 /* this returns 0 on failure (TODO: Fix this ghetto shit) */
859 retval = hashtable_insert(sb->s_dcache, key_val, key_val);
861 spin_unlock(&sb->s_dcache_lock);
864 /* Will remove and return the dentry. Caller deallocs the key, but the retval
865 * won't have a reference. * Returns 0 if it wasn't found. Callers can't
866 * assume much - they should not use the reference they *get back*, (if they
867 * already had one for key, they can use that). There may be other users out
869 struct dentry *dcache_remove(struct super_block *sb, struct dentry *key)
871 struct dentry *retval;
872 spin_lock(&sb->s_dcache_lock);
873 retval = hashtable_remove(sb->s_dcache, key);
874 spin_unlock(&sb->s_dcache_lock);
878 /* This will clean out the LRU list, which are the unused dentries of the dentry
879 * cache. This will optionally only free the negative ones. Note that we grab
880 * the hash lock for the time we traverse the LRU list - this prevents someone
881 * from getting a kref from the dcache, which could cause us trouble (we rip
882 * someone off the list, who isn't unused, and they try to rip them off the
884 void dcache_prune(struct super_block *sb, bool negative_only)
886 struct dentry *d_i, *temp;
887 struct dentry_tailq victims = TAILQ_HEAD_INITIALIZER(victims);
889 spin_lock(&sb->s_dcache_lock);
890 spin_lock(&sb->s_lru_lock);
891 TAILQ_FOREACH_SAFE(d_i, &sb->s_lru_d, d_lru, temp) {
892 if (!(d_i->d_flags & DENTRY_USED)) {
893 if (negative_only && !(d_i->d_flags & DENTRY_NEGATIVE))
895 /* another place where we'd be better off with tools, not sol'ns */
896 hashtable_remove(sb->s_dcache, d_i);
897 TAILQ_REMOVE(&sb->s_lru_d, d_i, d_lru);
898 TAILQ_INSERT_HEAD(&victims, d_i, d_lru);
901 spin_unlock(&sb->s_lru_lock);
902 spin_unlock(&sb->s_dcache_lock);
903 /* Now do the actual freeing, outside of the hash/LRU list locks. This is
904 * necessary since __dentry_free() will decref its parent, which may get
905 * released and try to add itself to the LRU. */
906 TAILQ_FOREACH_SAFE(d_i, &victims, d_lru, temp) {
907 TAILQ_REMOVE(&victims, d_i, d_lru);
908 assert(!kref_refcnt(&d_i->d_kref));
911 /* It is possible at this point that there are new items on the LRU. We
912 * could loop back until that list is empty, if we care about this. */
915 /* Inode Functions */
917 /* Creates and initializes a new inode. Generic fields are filled in.
918 * FS-specific fields are filled in by the callout. Specific fields are filled
919 * in in read_inode() based on what's on the disk for a given i_no, or when the
920 * inode is created (for new objects).
922 * i_no is set by the caller. Note that this means this inode can be for an
923 * inode that is already on disk, or it can be used when creating. */
924 struct inode *get_inode(struct dentry *dentry)
926 struct super_block *sb = dentry->d_sb;
927 /* FS allocs and sets the following: i_op, i_fop, i_pm.pm_op, and any FS
929 struct inode *inode = sb->s_op->alloc_inode(sb);
934 TAILQ_INSERT_HEAD(&sb->s_inodes, inode, i_sb_list); /* weak inode ref */
935 TAILQ_INIT(&inode->i_dentry);
936 TAILQ_INSERT_TAIL(&inode->i_dentry, dentry, d_alias); /* weak dentry ref*/
937 /* one for the dentry->d_inode, one passed out */
938 kref_init(&inode->i_kref, inode_release, 2);
939 dentry->d_inode = inode;
940 inode->i_ino = 0; /* set by caller later */
941 inode->i_blksize = sb->s_blocksize;
942 spinlock_init(&inode->i_lock);
943 kref_get(&sb->s_kref, 1); /* could allow the dentry to pin it */
945 inode->i_rdev = 0; /* this has no real meaning yet */
946 inode->i_bdev = sb->s_bdev; /* storing an uncounted ref */
947 inode->i_state = 0; /* need real states, like I_NEW */
948 inode->dirtied_when = 0;
950 atomic_set(&inode->i_writecount, 0);
951 /* Set up the page_map structures. Default is to use the embedded one.
952 * Might push some of this back into specific FSs. For now, the FS tells us
953 * what pm_op they want via i_pm.pm_op, which we set again in pm_init() */
954 inode->i_mapping = &inode->i_pm;
955 pm_init(inode->i_mapping, inode->i_pm.pm_op, inode);
959 /* Helper: loads/ reads in the inode numbered ino and attaches it to dentry */
960 void load_inode(struct dentry *dentry, unsigned long ino)
964 /* look it up in the inode cache first */
965 inode = icache_get(dentry->d_sb, ino);
967 /* connect the dentry to its inode */
968 TAILQ_INSERT_TAIL(&inode->i_dentry, dentry, d_alias);
969 dentry->d_inode = inode; /* storing the ref we got from icache_get */
972 /* otherwise, we need to do it manually */
973 inode = get_inode(dentry);
975 dentry->d_sb->s_op->read_inode(inode);
976 /* TODO: race here, two creators could miss in the cache, and then get here.
977 * need a way to sync across a blocking call. needs to be either at this
978 * point in the code or per the ino (dentries could be different) */
979 icache_put(dentry->d_sb, inode);
980 kref_put(&inode->i_kref);
983 /* Helper op, used when creating regular files, directories, symlinks, etc.
984 * Note we make a distinction between the mode and the file type (for now).
985 * After calling this, call the FS specific version (create or mkdir), which
986 * will set the i_ino, the filetype, and do any other FS-specific stuff. Also
987 * note that a lot of inode stuff was initialized in get_inode/alloc_inode. The
988 * stuff here is pertinent to the specific creator (user), mode, and time. Also
989 * note we don't pass this an nd, like Linux does... */
990 static struct inode *create_inode(struct dentry *dentry, int mode)
992 uint64_t now = epoch_sec();
993 /* note it is the i_ino that uniquely identifies a file in the specific
994 * filesystem. there's a diff between creating an inode (even for an in-use
995 * ino) and then filling it in, and vs creating a brand new one.
996 * get_inode() sets it to 0, and it should be filled in later in an
997 * FS-specific manner. */
998 struct inode *inode = get_inode(dentry);
1001 inode->i_mode = mode & S_PMASK; /* note that after this, we have no type */
1004 inode->i_blocks = 0;
1005 inode->i_atime.tv_sec = now;
1006 inode->i_ctime.tv_sec = now;
1007 inode->i_mtime.tv_sec = now;
1008 inode->i_atime.tv_nsec = 0;
1009 inode->i_ctime.tv_nsec = 0;
1010 inode->i_mtime.tv_nsec = 0;
1011 inode->i_bdev = inode->i_sb->s_bdev;
1012 /* when we have notions of users, do something here: */
1018 /* Create a new disk inode in dir associated with dentry, with the given mode.
1019 * called when creating a regular file. dir is the directory/parent. dentry is
1020 * the dentry of the inode we are creating. Note the lack of the nd... */
1021 int create_file(struct inode *dir, struct dentry *dentry, int mode)
1023 struct inode *new_file = create_inode(dentry, mode);
1026 dir->i_op->create(dir, dentry, mode, 0);
1027 icache_put(new_file->i_sb, new_file);
1028 kref_put(&new_file->i_kref);
1032 /* Creates a new inode for a directory associated with dentry in dir with the
1034 int create_dir(struct inode *dir, struct dentry *dentry, int mode)
1036 struct inode *new_dir = create_inode(dentry, mode);
1039 dir->i_op->mkdir(dir, dentry, mode);
1040 dir->i_nlink++; /* Directories get a hardlink for every child dir */
1041 /* Make sure my parent tracks me. This is okay, since no directory (dir)
1042 * can have more than one dentry */
1043 struct dentry *parent = TAILQ_FIRST(&dir->i_dentry);
1044 assert(parent && parent == TAILQ_LAST(&dir->i_dentry, dentry_tailq));
1045 /* parent dentry tracks dentry as a subdir, weak reference */
1046 TAILQ_INSERT_TAIL(&parent->d_subdirs, dentry, d_subdirs_link);
1047 icache_put(new_dir->i_sb, new_dir);
1048 kref_put(&new_dir->i_kref);
1052 /* Creates a new inode for a symlink associated with dentry in dir, containing
1053 * the symlink symname */
1054 int create_symlink(struct inode *dir, struct dentry *dentry,
1055 const char *symname, int mode)
1057 struct inode *new_sym = create_inode(dentry, mode);
1060 dir->i_op->symlink(dir, dentry, symname);
1061 icache_put(new_sym->i_sb, new_sym);
1062 kref_put(&new_sym->i_kref);
1066 /* Returns 0 if the given mode is acceptable for the inode, and an appropriate
1067 * error code if not. Needs to be writen, based on some sensible rules, and
1068 * will also probably use 'current' */
1069 int check_perms(struct inode *inode, int access_mode)
1071 return 0; /* anything goes! */
1074 /* Called after all external refs are gone to clean up the inode. Once this is
1075 * called, all dentries pointing here are already done (one of them triggered
1076 * this via kref_put(). */
1077 void inode_release(struct kref *kref)
1079 struct inode *inode = container_of(kref, struct inode, i_kref);
1080 TAILQ_REMOVE(&inode->i_sb->s_inodes, inode, i_sb_list);
1081 icache_remove(inode->i_sb, inode->i_ino);
1082 /* Might need to write back or delete the file/inode */
1083 if (inode->i_nlink) {
1084 if (inode->i_state & I_STATE_DIRTY)
1085 inode->i_sb->s_op->write_inode(inode, TRUE);
1087 inode->i_sb->s_op->delete_inode(inode);
1089 if (S_ISFIFO(inode->i_mode)) {
1090 page_decref(kva2page(inode->i_pipe->p_buf));
1091 kfree(inode->i_pipe);
1094 // kref_put(inode->i_bdev->kref); /* assuming it's a bdev, could be a pipe*/
1095 /* Either way, we dealloc the in-memory version */
1096 inode->i_sb->s_op->dealloc_inode(inode); /* FS-specific clean-up */
1097 kref_put(&inode->i_sb->s_kref);
1098 /* TODO: clean this up */
1099 assert(inode->i_mapping == &inode->i_pm);
1100 kmem_cache_free(inode_kcache, inode);
1103 /* Fills in kstat with the stat information for the inode */
1104 void stat_inode(struct inode *inode, struct kstat *kstat)
1106 kstat->st_dev = inode->i_sb->s_dev;
1107 kstat->st_ino = inode->i_ino;
1108 kstat->st_mode = inode->i_mode;
1109 kstat->st_nlink = inode->i_nlink;
1110 kstat->st_uid = inode->i_uid;
1111 kstat->st_gid = inode->i_gid;
1112 kstat->st_rdev = inode->i_rdev;
1113 kstat->st_size = inode->i_size;
1114 kstat->st_blksize = inode->i_blksize;
1115 kstat->st_blocks = inode->i_blocks;
1116 kstat->st_atime = inode->i_atime;
1117 kstat->st_mtime = inode->i_mtime;
1118 kstat->st_ctime = inode->i_ctime;
1121 void print_kstat(struct kstat *kstat)
1123 printk("kstat info for %p:\n", kstat);
1124 printk("\tst_dev : %p\n", kstat->st_dev);
1125 printk("\tst_ino : %p\n", kstat->st_ino);
1126 printk("\tst_mode : %p\n", kstat->st_mode);
1127 printk("\tst_nlink : %p\n", kstat->st_nlink);
1128 printk("\tst_uid : %p\n", kstat->st_uid);
1129 printk("\tst_gid : %p\n", kstat->st_gid);
1130 printk("\tst_rdev : %p\n", kstat->st_rdev);
1131 printk("\tst_size : %p\n", kstat->st_size);
1132 printk("\tst_blksize: %p\n", kstat->st_blksize);
1133 printk("\tst_blocks : %p\n", kstat->st_blocks);
1134 printk("\tst_atime : %p\n", kstat->st_atime);
1135 printk("\tst_mtime : %p\n", kstat->st_mtime);
1136 printk("\tst_ctime : %p\n", kstat->st_ctime);
1139 /* Inode Cache management. In general, search on the ino, get a refcnt'd value
1140 * back. Remove does not give you a reference back - it should only be called
1141 * in inode_release(). */
1142 struct inode *icache_get(struct super_block *sb, unsigned long ino)
1144 /* This is the same style as in pid2proc, it's the "safely create a strong
1145 * reference from a weak one, so long as other strong ones exist" pattern */
1146 spin_lock(&sb->s_icache_lock);
1147 struct inode *inode = hashtable_search(sb->s_icache, (void*)ino);
1149 if (!kref_get_not_zero(&inode->i_kref, 1))
1151 spin_unlock(&sb->s_icache_lock);
1155 void icache_put(struct super_block *sb, struct inode *inode)
1157 spin_lock(&sb->s_icache_lock);
1158 /* there's a race in load_ino() that could trigger this */
1159 assert(!hashtable_search(sb->s_icache, (void*)inode->i_ino));
1160 hashtable_insert(sb->s_icache, (void*)inode->i_ino, inode);
1161 spin_unlock(&sb->s_icache_lock);
1164 struct inode *icache_remove(struct super_block *sb, unsigned long ino)
1166 struct inode *inode;
1167 /* Presumably these hashtable removals could be easier since callers
1168 * actually know who they are (same with the pid2proc hash) */
1169 spin_lock(&sb->s_icache_lock);
1170 inode = hashtable_remove(sb->s_icache, (void*)ino);
1171 spin_unlock(&sb->s_icache_lock);
1172 assert(inode && !kref_refcnt(&inode->i_kref));
1176 /* File functions */
1178 /* Read count bytes from the file into buf, starting at *offset, which is
1179 * increased accordingly, returning the number of bytes transfered. Most
1180 * filesystems will use this function for their f_op->read.
1181 * Note, this uses the page cache. */
1182 ssize_t generic_file_read(struct file *file, char *buf, size_t count,
1188 unsigned long first_idx, last_idx;
1191 /* read in offset, in case of a concurrent reader/writer, so we don't screw
1192 * up our math for count, the idxs, etc. */
1193 off64_t orig_off = ACCESS_ONCE(*offset);
1195 /* Consider pushing some error checking higher in the VFS */
1198 if (orig_off >= file->f_dentry->d_inode->i_size)
1200 /* Make sure we don't go past the end of the file */
1201 if (orig_off + count > file->f_dentry->d_inode->i_size) {
1202 count = file->f_dentry->d_inode->i_size - orig_off;
1204 assert((long)count > 0);
1205 page_off = orig_off & (PGSIZE - 1);
1206 first_idx = orig_off >> PGSHIFT;
1207 last_idx = (orig_off + count) >> PGSHIFT;
1208 buf_end = buf + count;
1209 /* For each file page, make sure it's in the page cache, then copy it out.
1210 * TODO: will probably need to consider concurrently truncated files here.*/
1211 for (int i = first_idx; i <= last_idx; i++) {
1212 error = pm_load_page(file->f_mapping, i, &page);
1213 assert(!error); /* TODO: handle ENOMEM and friends */
1214 copy_amt = MIN(PGSIZE - page_off, buf_end - buf);
1215 /* TODO: (UMEM) think about this. if it's a user buffer, we're relying
1216 * on current to detect whose it is (which should work for async calls).
1217 * Also, need to propagate errors properly... Probably should do a
1218 * user_mem_check, then free, and also to make a distinction between
1219 * when the kernel wants a read/write (TODO: KFOP) */
1221 memcpy_to_user(current, buf, page2kva(page) + page_off, copy_amt);
1223 memcpy(buf, page2kva(page) + page_off, copy_amt);
1227 pm_put_page(page); /* it's still in the cache, we just don't need it */
1229 assert(buf == buf_end);
1230 /* could have concurrent file ops that screw with offset, so userspace isn't
1231 * safe. but at least it'll be a value that one of the concurrent ops could
1232 * have produced (compared to *offset_changed_concurrently += count. */
1233 *offset = orig_off + count;
1237 /* Write count bytes from buf to the file, starting at *offset, which is
1238 * increased accordingly, returning the number of bytes transfered. Most
1239 * filesystems will use this function for their f_op->write. Note, this uses
1242 * Changes don't get flushed to disc til there is an fsync, page cache eviction,
1243 * or other means of trying to writeback the pages. */
1244 ssize_t generic_file_write(struct file *file, const char *buf, size_t count,
1250 unsigned long first_idx, last_idx;
1252 const char *buf_end;
1253 off64_t orig_off = ACCESS_ONCE(*offset);
1255 /* Consider pushing some error checking higher in the VFS */
1258 if (file->f_flags & O_APPEND) {
1259 spin_lock(&file->f_dentry->d_inode->i_lock);
1260 orig_off = file->f_dentry->d_inode->i_size;
1261 /* setting the filesize here, instead of during the extend-check, since
1262 * we need to atomically reserve space and set our write position. */
1263 file->f_dentry->d_inode->i_size += count;
1264 spin_unlock(&file->f_dentry->d_inode->i_lock);
1266 if (orig_off + count > file->f_dentry->d_inode->i_size) {
1267 /* lock for writes to i_size. we allow lockless reads. recheck
1268 * i_size in case of concurrent writers since our orig check. */
1269 spin_lock(&file->f_dentry->d_inode->i_lock);
1270 if (orig_off + count > file->f_dentry->d_inode->i_size)
1271 file->f_dentry->d_inode->i_size = orig_off + count;
1272 spin_unlock(&file->f_dentry->d_inode->i_lock);
1275 page_off = orig_off & (PGSIZE - 1);
1276 first_idx = orig_off >> PGSHIFT;
1277 last_idx = (orig_off + count) >> PGSHIFT;
1278 buf_end = buf + count;
1279 /* For each file page, make sure it's in the page cache, then write it.*/
1280 for (int i = first_idx; i <= last_idx; i++) {
1281 error = pm_load_page(file->f_mapping, i, &page);
1282 assert(!error); /* TODO: handle ENOMEM and friends */
1283 copy_amt = MIN(PGSIZE - page_off, buf_end - buf);
1284 /* TODO: (UMEM) (KFOP) think about this. if it's a user buffer, we're
1285 * relying on current to detect whose it is (which should work for async
1288 memcpy_from_user(current, page2kva(page) + page_off, buf, copy_amt);
1290 memcpy(page2kva(page) + page_off, buf, copy_amt);
1294 atomic_or(&page->pg_flags, PG_DIRTY);
1295 pm_put_page(page); /* it's still in the cache, we just don't need it */
1297 assert(buf == buf_end);
1298 *offset = orig_off + count;
1302 /* Directories usually use this for their read method, which is the way glibc
1303 * currently expects us to do a readdir (short of doing linux's getdents). Will
1304 * probably need work, based on whatever real programs want. */
1305 ssize_t generic_dir_read(struct file *file, char *u_buf, size_t count,
1308 struct kdirent dir_r = {0}, *dirent = &dir_r;
1310 size_t amt_copied = 0;
1311 char *buf_end = u_buf + count;
1313 if (!S_ISDIR(file->f_dentry->d_inode->i_mode)) {
1319 /* start readdir from where it left off: */
1320 dirent->d_off = *offset;
1322 u_buf + sizeof(struct kdirent) <= buf_end;
1323 u_buf += sizeof(struct kdirent)) {
1324 /* TODO: UMEM/KFOP (pin the u_buf in the syscall, ditch the local copy,
1325 * get rid of this memcpy and reliance on current, etc). Might be
1326 * tricky with the dirent->d_off and trust issues */
1327 retval = file->f_op->readdir(file, dirent);
1332 /* Slight info exposure: could be extra crap after the name in the
1333 * dirent (like the name of a deleted file) */
1335 memcpy_to_user(current, u_buf, dirent, sizeof(struct dirent));
1337 memcpy(u_buf, dirent, sizeof(struct dirent));
1339 amt_copied += sizeof(struct dirent);
1340 /* 0 signals end of directory */
1344 /* Next time read is called, we pick up where we left off */
1345 *offset = dirent->d_off; /* UMEM */
1346 /* important to tell them how much they got. they often keep going til they
1347 * get 0 back (in the case of ls). it's also how much has been read, but it
1348 * isn't how much the f_pos has moved (which is opaque to the VFS). */
1352 /* Opens the file, using permissions from current for lack of a better option.
1353 * It will attempt to create the file if it does not exist and O_CREAT is
1354 * specified. This will return 0 on failure, and set errno. TODO: There's some
1355 * stuff that we don't do, esp related file truncating/creation. flags are for
1356 * opening, the mode is for creating. The flags related to how to create
1357 * (O_CREAT_FLAGS) are handled in this function, not in create_file().
1359 * It's tempting to split this into a do_file_create and a do_file_open, based
1360 * on the O_CREAT flag, but the O_CREAT flag can be ignored if the file exists
1361 * already and O_EXCL isn't specified. We could have open call create if it
1362 * fails, but for now we'll keep it as is. */
1363 struct file *do_file_open(char *path, int flags, int mode)
1365 struct file *file = 0;
1366 struct dentry *file_d;
1367 struct inode *parent_i;
1368 struct nameidata nd_r = {0}, *nd = &nd_r;
1370 unsigned long nr_pages;
1372 /* The file might exist, lets try to just open it right away */
1373 nd->intent = LOOKUP_OPEN;
1374 error = path_lookup(path, LOOKUP_FOLLOW, nd);
1376 /* If this is a directory, make sure we are opening with O_RDONLY.
1377 * Unfortunately we can't just check for O_RDONLY directly because its
1378 * value is 0x0. We instead have to make sure it's not O_WRONLY and
1379 * not O_RDWR explicitly. */
1380 if (S_ISDIR(nd->dentry->d_inode->i_mode) &&
1381 ((flags & O_WRONLY) || (flags & O_RDWR))) {
1385 /* Also need to make sure we didn't want to O_EXCL create */
1386 if ((flags & O_CREAT) && (flags & O_EXCL)) {
1390 file_d = nd->dentry;
1391 kref_get(&file_d->d_kref, 1);
1394 if (!(flags & O_CREAT)) {
1398 /* So it didn't already exist, release the path from the previous lookup,
1399 * and then we try to create it. */
1401 /* get the parent, following links. this means you get the parent of the
1402 * final link (which may not be in 'path' in the first place. */
1403 nd->intent = LOOKUP_CREATE;
1404 error = path_lookup(path, LOOKUP_PARENT | LOOKUP_FOLLOW, nd);
1409 /* see if the target is there (shouldn't be), and handle accordingly */
1410 file_d = do_lookup(nd->dentry, nd->last.name);
1412 if (!(flags & O_CREAT)) {
1413 warn("Extremely unlikely race, probably a bug");
1417 /* Create the inode/file. get a fresh dentry too: */
1418 file_d = get_dentry(nd->dentry->d_sb, nd->dentry, nd->last.name);
1421 parent_i = nd->dentry->d_inode;
1422 /* Note that the mode technically should only apply to future opens,
1423 * but we apply it immediately. */
1424 if (create_file(parent_i, file_d, mode)) /* sets errno */
1426 dcache_put(file_d->d_sb, file_d);
1427 } else { /* something already exists */
1428 /* this can happen due to concurrent access, but needs to be thought
1430 panic("File shouldn't be here!");
1431 if ((flags & O_CREAT) && (flags & O_EXCL)) {
1432 /* wanted to create, not open, bail out */
1438 /* now open the file (freshly created or if it already existed). At this
1439 * point, file_d is a refcnt'd dentry, regardless of which branch we took.*/
1440 if (flags & O_TRUNC) {
1441 spin_lock(&file_d->d_inode->i_lock);
1442 nr_pages = ROUNDUP(file_d->d_inode->i_size, PGSIZE) >> PGSHIFT;
1443 file_d->d_inode->i_size = 0;
1444 spin_unlock(&file_d->d_inode->i_lock);
1445 pm_remove_contig(file_d->d_inode->i_mapping, 0, nr_pages);
1447 file = dentry_open(file_d, flags); /* sets errno */
1448 /* Note the fall through to the exit paths. File is 0 by default and if
1449 * dentry_open fails. */
1451 kref_put(&file_d->d_kref);
1457 /* Path is the location of the symlink, sometimes called the "new path", and
1458 * symname is who we link to, sometimes called the "old path". */
1459 int do_symlink(char *path, const char *symname, int mode)
1461 struct dentry *sym_d;
1462 struct inode *parent_i;
1463 struct nameidata nd_r = {0}, *nd = &nd_r;
1467 nd->intent = LOOKUP_CREATE;
1468 /* get the parent, but don't follow links */
1469 error = path_lookup(path, LOOKUP_PARENT, nd);
1474 /* see if the target is already there, handle accordingly */
1475 sym_d = do_lookup(nd->dentry, nd->last.name);
1480 /* Doesn't already exist, let's try to make it: */
1481 sym_d = get_dentry(nd->dentry->d_sb, nd->dentry, nd->last.name);
1484 parent_i = nd->dentry->d_inode;
1485 if (create_symlink(parent_i, sym_d, symname, mode))
1487 dcache_put(sym_d->d_sb, sym_d);
1488 retval = 0; /* Note the fall through to the exit paths */
1490 kref_put(&sym_d->d_kref);
1496 /* Makes a hard link for the file behind old_path to new_path */
1497 int do_link(char *old_path, char *new_path)
1499 struct dentry *link_d, *old_d;
1500 struct inode *inode, *parent_dir;
1501 struct nameidata nd_r = {0}, *nd = &nd_r;
1505 nd->intent = LOOKUP_CREATE;
1506 /* get the absolute parent of the new_path */
1507 error = path_lookup(new_path, LOOKUP_PARENT | LOOKUP_FOLLOW, nd);
1512 parent_dir = nd->dentry->d_inode;
1513 /* see if the new target is already there, handle accordingly */
1514 link_d = do_lookup(nd->dentry, nd->last.name);
1519 /* Doesn't already exist, let's try to make it. Still need to stitch it to
1520 * an inode and set its FS-specific stuff after this.*/
1521 link_d = get_dentry(nd->dentry->d_sb, nd->dentry, nd->last.name);
1524 /* Now let's get the old_path target */
1525 old_d = lookup_dentry(old_path, LOOKUP_FOLLOW);
1526 if (!old_d) /* errno set by lookup_dentry */
1528 /* For now, can only link to files */
1529 if (!S_ISREG(old_d->d_inode->i_mode)) {
1533 /* Must be on the same FS */
1534 if (old_d->d_sb != link_d->d_sb) {
1538 /* Do whatever FS specific stuff there is first (which is also a chance to
1540 error = parent_dir->i_op->link(old_d, parent_dir, link_d);
1545 /* Finally stitch it up */
1546 inode = old_d->d_inode;
1547 kref_get(&inode->i_kref, 1);
1548 link_d->d_inode = inode;
1550 TAILQ_INSERT_TAIL(&inode->i_dentry, link_d, d_alias); /* weak ref */
1551 dcache_put(link_d->d_sb, link_d);
1552 retval = 0; /* Note the fall through to the exit paths */
1554 kref_put(&old_d->d_kref);
1556 kref_put(&link_d->d_kref);
1562 /* Unlinks path from the directory tree. Read the Documentation for more info.
1564 int do_unlink(char *path)
1566 struct dentry *dentry;
1567 struct inode *parent_dir;
1568 struct nameidata nd_r = {0}, *nd = &nd_r;
1572 /* get the parent of the target, and don't follow a final link */
1573 error = path_lookup(path, LOOKUP_PARENT, nd);
1578 parent_dir = nd->dentry->d_inode;
1579 /* make sure the target is there */
1580 dentry = do_lookup(nd->dentry, nd->last.name);
1585 /* Make sure the target is not a directory */
1586 if (S_ISDIR(dentry->d_inode->i_mode)) {
1590 /* Remove the dentry from its parent */
1591 error = parent_dir->i_op->unlink(parent_dir, dentry);
1596 /* Now that our parent doesn't track us, we need to make sure we aren't
1597 * findable via the dentry cache. DYING, so we will be freed in
1598 * dentry_release() */
1599 dentry->d_flags |= DENTRY_DYING;
1600 dcache_remove(dentry->d_sb, dentry);
1601 dentry->d_inode->i_nlink--; /* TODO: race here, esp with a decref */
1602 /* At this point, the dentry is unlinked from the FS, and the inode has one
1603 * less link. When the in-memory objects (dentry, inode) are going to be
1604 * released (after all open files are closed, and maybe after entries are
1605 * evicted from the cache), then nlinks will get checked and the FS-file
1606 * will get removed from the disk */
1607 retval = 0; /* Note the fall through to the exit paths */
1609 kref_put(&dentry->d_kref);
1615 /* Checks to see if path can be accessed via mode. Need to actually send the
1616 * mode along somehow, so this doesn't do much now. This is an example of
1617 * decent error propagation from the lower levels via int retvals. */
1618 int do_access(char *path, int mode)
1620 struct nameidata nd_r = {0}, *nd = &nd_r;
1622 nd->intent = LOOKUP_ACCESS;
1623 retval = path_lookup(path, 0, nd);
1628 int do_file_chmod(struct file *file, int mode)
1630 int old_mode_ftype = file->f_dentry->d_inode->i_mode & __S_IFMT;
1632 /* TODO: when we have notions of uid, check for the proc's uid */
1633 if (file->f_dentry->d_inode->i_uid != UID_OF_ME)
1637 file->f_dentry->d_inode->i_mode = (mode & S_PMASK) | old_mode_ftype;
1641 /* Make a directory at path with mode. Returns -1 and sets errno on errors */
1642 int do_mkdir(char *path, int mode)
1644 struct dentry *dentry;
1645 struct inode *parent_i;
1646 struct nameidata nd_r = {0}, *nd = &nd_r;
1650 nd->intent = LOOKUP_CREATE;
1651 /* get the parent, but don't follow links */
1652 error = path_lookup(path, LOOKUP_PARENT, nd);
1657 /* see if the target is already there, handle accordingly */
1658 dentry = do_lookup(nd->dentry, nd->last.name);
1663 /* Doesn't already exist, let's try to make it: */
1664 dentry = get_dentry(nd->dentry->d_sb, nd->dentry, nd->last.name);
1667 parent_i = nd->dentry->d_inode;
1668 if (create_dir(parent_i, dentry, mode))
1670 dcache_put(dentry->d_sb, dentry);
1671 retval = 0; /* Note the fall through to the exit paths */
1673 kref_put(&dentry->d_kref);
1679 int do_rmdir(char *path)
1681 struct dentry *dentry;
1682 struct inode *parent_i;
1683 struct nameidata nd_r = {0}, *nd = &nd_r;
1687 /* get the parent, following links (probably want this), and we must get a
1688 * directory. Note, current versions of path_lookup can't handle both
1689 * PARENT and DIRECTORY, at least, it doesn't check that *path is a
1691 error = path_lookup(path, LOOKUP_PARENT | LOOKUP_FOLLOW | LOOKUP_DIRECTORY,
1697 /* make sure the target is already there, handle accordingly */
1698 dentry = do_lookup(nd->dentry, nd->last.name);
1703 if (!S_ISDIR(dentry->d_inode->i_mode)) {
1707 if (dentry->d_mount_point) {
1711 /* TODO: make sure we aren't a mount or processes root (EBUSY) */
1712 /* Now for the removal. the FSs will check if they are empty */
1713 parent_i = nd->dentry->d_inode;
1714 error = parent_i->i_op->rmdir(parent_i, dentry);
1719 /* Now that our parent doesn't track us, we need to make sure we aren't
1720 * findable via the dentry cache. DYING, so we will be freed in
1721 * dentry_release() */
1722 dentry->d_flags |= DENTRY_DYING;
1723 dcache_remove(dentry->d_sb, dentry);
1724 /* Decref ourselves, so inode_release() knows we are done */
1725 dentry->d_inode->i_nlink--;
1726 TAILQ_REMOVE(&nd->dentry->d_subdirs, dentry, d_subdirs_link);
1727 parent_i->i_nlink--; /* TODO: race on this, esp since its a decref */
1728 /* we still have d_parent and a kref on our parent, which will go away when
1729 * the in-memory dentry object goes away. */
1730 retval = 0; /* Note the fall through to the exit paths */
1732 kref_put(&dentry->d_kref);
1738 /* Pipes: Doing a simple buffer with reader and writer offsets. Size is power
1739 * of two, so we can easily compute its status and whatnot. */
1741 #define PIPE_SZ (1 << PGSHIFT)
1743 static size_t pipe_get_rd_idx(struct pipe_inode_info *pii)
1745 return pii->p_rd_off & (PIPE_SZ - 1);
1748 static size_t pipe_get_wr_idx(struct pipe_inode_info *pii)
1751 return pii->p_wr_off & (PIPE_SZ - 1);
1754 static bool pipe_is_empty(struct pipe_inode_info *pii)
1756 return __ring_empty(pii->p_wr_off, pii->p_rd_off);
1759 static bool pipe_is_full(struct pipe_inode_info *pii)
1761 return __ring_full(PIPE_SZ, pii->p_wr_off, pii->p_rd_off);
1764 static size_t pipe_nr_full(struct pipe_inode_info *pii)
1766 return __ring_nr_full(pii->p_wr_off, pii->p_rd_off);
1769 static size_t pipe_nr_empty(struct pipe_inode_info *pii)
1771 return __ring_nr_empty(PIPE_SZ, pii->p_wr_off, pii->p_rd_off);
1774 ssize_t pipe_file_read(struct file *file, char *buf, size_t count,
1777 struct pipe_inode_info *pii = file->f_dentry->d_inode->i_pipe;
1778 size_t copy_amt, amt_copied = 0;
1780 cv_lock(&pii->p_cv);
1781 while (pipe_is_empty(pii)) {
1782 /* We wait til the pipe is drained before sending EOF if there are no
1783 * writers (instead of aborting immediately) */
1784 if (!pii->p_nr_writers) {
1785 cv_unlock(&pii->p_cv);
1788 if (file->f_flags & O_NONBLOCK) {
1789 cv_unlock(&pii->p_cv);
1793 cv_wait(&pii->p_cv);
1796 /* We might need to wrap-around with our copy, so we'll do the copy in two
1797 * passes. This will copy up to the end of the buffer, then on the next
1798 * pass will copy the rest to the beginning of the buffer (if necessary) */
1799 for (int i = 0; i < 2; i++) {
1800 copy_amt = MIN(PIPE_SZ - pipe_get_rd_idx(pii),
1801 MIN(pipe_nr_full(pii), count));
1802 assert(current); /* shouldn't pipe from the kernel */
1803 memcpy_to_user(current, buf, pii->p_buf + pipe_get_rd_idx(pii),
1807 pii->p_rd_off += copy_amt;
1808 amt_copied += copy_amt;
1810 /* Just using one CV for both readers and writers. We should rarely have
1811 * multiple readers or writers. */
1813 __cv_broadcast(&pii->p_cv);
1814 cv_unlock(&pii->p_cv);
1818 /* Note: we're not dealing with PIPE_BUF and minimum atomic chunks, unless I
1820 ssize_t pipe_file_write(struct file *file, const char *buf, size_t count,
1823 struct pipe_inode_info *pii = file->f_dentry->d_inode->i_pipe;
1824 size_t copy_amt, amt_copied = 0;
1826 cv_lock(&pii->p_cv);
1827 /* Write aborts right away if there are no readers, regardless of pipe
1829 if (!pii->p_nr_readers) {
1830 cv_unlock(&pii->p_cv);
1834 while (pipe_is_full(pii)) {
1835 if (file->f_flags & O_NONBLOCK) {
1836 cv_unlock(&pii->p_cv);
1840 cv_wait(&pii->p_cv);
1842 /* Still need to check in the loop, in case the last reader left while
1844 if (!pii->p_nr_readers) {
1845 cv_unlock(&pii->p_cv);
1850 /* We might need to wrap-around with our copy, so we'll do the copy in two
1851 * passes. This will copy up to the end of the buffer, then on the next
1852 * pass will copy the rest to the beginning of the buffer (if necessary) */
1853 for (int i = 0; i < 2; i++) {
1854 copy_amt = MIN(PIPE_SZ - pipe_get_wr_idx(pii),
1855 MIN(pipe_nr_empty(pii), count));
1856 assert(current); /* shouldn't pipe from the kernel */
1857 memcpy_from_user(current, pii->p_buf + pipe_get_wr_idx(pii), buf,
1861 pii->p_wr_off += copy_amt;
1862 amt_copied += copy_amt;
1864 /* Just using one CV for both readers and writers. We should rarely have
1865 * multiple readers or writers. */
1867 __cv_broadcast(&pii->p_cv);
1868 cv_unlock(&pii->p_cv);
1872 /* In open and release, we need to track the number of readers and writers,
1873 * which we can differentiate by the file flags. */
1874 int pipe_open(struct inode *inode, struct file *file)
1876 struct pipe_inode_info *pii = inode->i_pipe;
1877 cv_lock(&pii->p_cv);
1878 /* Ugliness due to not using flags for O_RDONLY and friends... */
1879 if (file->f_mode == S_IRUSR) {
1880 pii->p_nr_readers++;
1881 } else if (file->f_mode == S_IWUSR) {
1882 pii->p_nr_writers++;
1884 warn("Bad pipe file flags 0x%x\n", file->f_flags);
1886 cv_unlock(&pii->p_cv);
1890 int pipe_release(struct inode *inode, struct file *file)
1892 struct pipe_inode_info *pii = inode->i_pipe;
1893 cv_lock(&pii->p_cv);
1894 /* Ugliness due to not using flags for O_RDONLY and friends... */
1895 if (file->f_mode == S_IRUSR) {
1896 pii->p_nr_readers--;
1897 } else if (file->f_mode == S_IWUSR) {
1898 pii->p_nr_writers--;
1900 warn("Bad pipe file flags 0x%x\n", file->f_flags);
1902 /* need to wake up any sleeping readers/writers, since we might be done */
1903 __cv_broadcast(&pii->p_cv);
1904 cv_unlock(&pii->p_cv);
1908 struct file_operations pipe_f_op = {
1909 .read = pipe_file_read,
1910 .write = pipe_file_write,
1912 .release = pipe_release,
1916 void pipe_debug(struct file *f)
1918 struct pipe_inode_info *pii = f->f_dentry->d_inode->i_pipe;
1920 printk("PIPE %p\n", pii);
1921 printk("\trdoff %p\n", pii->p_rd_off);
1922 printk("\twroff %p\n", pii->p_wr_off);
1923 printk("\tnr_rds %d\n", pii->p_nr_readers);
1924 printk("\tnr_wrs %d\n", pii->p_nr_writers);
1925 printk("\tcv waiters %d\n", pii->p_cv.nr_waiters);
1929 /* General plan: get a dentry/inode to represent the pipe. We'll alloc it from
1930 * the default_ns SB, but won't actually link it anywhere. It'll only be held
1931 * alive by the krefs, til all the FDs are closed. */
1932 int do_pipe(struct file **pipe_files, int flags)
1934 struct dentry *pipe_d;
1935 struct inode *pipe_i;
1936 struct file *pipe_f_read, *pipe_f_write;
1937 struct super_block *def_sb = default_ns.root->mnt_sb;
1938 struct pipe_inode_info *pii;
1940 pipe_d = get_dentry(def_sb, 0, "pipe");
1943 pipe_d->d_op = &dummy_d_op;
1944 pipe_i = get_inode(pipe_d);
1946 goto error_post_dentry;
1947 /* preemptively mark the dentry for deletion. we have an unlinked dentry
1948 * right off the bat, held in only by the kref chain (pipe_d is the ref). */
1949 pipe_d->d_flags |= DENTRY_DYING;
1950 /* pipe_d->d_inode still has one ref to pipe_i, keeping the inode alive */
1951 kref_put(&pipe_i->i_kref);
1952 /* init inode fields. note we're using the dummy ops for i_op and d_op */
1953 pipe_i->i_mode = S_IRWXU | S_IRWXG | S_IRWXO;
1954 SET_FTYPE(pipe_i->i_mode, __S_IFIFO); /* using type == FIFO */
1955 pipe_i->i_nlink = 1; /* one for the dentry */
1958 pipe_i->i_size = PGSIZE;
1959 pipe_i->i_blocks = 0;
1960 pipe_i->i_atime.tv_sec = 0;
1961 pipe_i->i_atime.tv_nsec = 0;
1962 pipe_i->i_mtime.tv_sec = 0;
1963 pipe_i->i_mtime.tv_nsec = 0;
1964 pipe_i->i_ctime.tv_sec = 0;
1965 pipe_i->i_ctime.tv_nsec = 0;
1966 pipe_i->i_fs_info = 0;
1967 pipe_i->i_op = &dummy_i_op;
1968 pipe_i->i_fop = &pipe_f_op;
1969 pipe_i->i_socket = FALSE;
1970 /* Actually build the pipe. We're using one page, hanging off the
1971 * pipe_inode_info struct. When we release the inode, we free the pipe
1973 pipe_i->i_pipe = kmalloc(sizeof(struct pipe_inode_info), KMALLOC_WAIT);
1974 pii = pipe_i->i_pipe;
1979 pii->p_buf = kpage_zalloc_addr();
1986 pii->p_nr_readers = 0;
1987 pii->p_nr_writers = 0;
1988 cv_init(&pii->p_cv); /* must do this before dentry_open / pipe_open */
1989 /* Now we have an inode for the pipe. We need two files for the read and
1990 * write ends of the pipe. */
1991 flags &= ~(O_ACCMODE); /* avoid user bugs */
1992 pipe_f_read = dentry_open(pipe_d, flags | O_RDONLY);
1995 pipe_f_write = dentry_open(pipe_d, flags | O_WRONLY);
1998 pipe_files[0] = pipe_f_read;
1999 pipe_files[1] = pipe_f_write;
2003 kref_put(&pipe_f_read->f_kref);
2005 page_decref(kva2page(pii->p_buf));
2007 kfree(pipe_i->i_pipe);
2009 /* We don't need to free the pipe_i; putting the dentry will free it */
2011 /* Note we only free the dentry on failure. */
2012 kref_put(&pipe_d->d_kref);
2016 int do_rename(char *old_path, char *new_path)
2018 struct nameidata nd_old = {0}, *nd_o = &nd_old;
2019 struct nameidata nd_new = {0}, *nd_n = &nd_new;
2020 struct dentry *old_dir_d, *new_dir_d;
2021 struct inode *old_dir_i, *new_dir_i;
2022 struct dentry *old_d, *new_d, *unlink_d;
2027 nd_o->intent = LOOKUP_ACCESS; /* maybe, might need another type */
2029 /* get the parent, but don't follow links */
2030 error = path_lookup(old_path, LOOKUP_PARENT | LOOKUP_DIRECTORY, nd_o);
2036 old_dir_d = nd_o->dentry;
2037 old_dir_i = old_dir_d->d_inode;
2039 old_d = do_lookup(old_dir_d, nd_o->last.name);
2046 nd_n->intent = LOOKUP_CREATE;
2047 error = path_lookup(new_path, LOOKUP_PARENT | LOOKUP_DIRECTORY, nd_n);
2051 goto out_paths_and_src;
2053 new_dir_d = nd_n->dentry;
2054 new_dir_i = new_dir_d->d_inode;
2055 /* TODO if new_dir == old_dir, we might be able to simplify things */
2057 if (new_dir_i->i_sb != old_dir_i->i_sb) {
2060 goto out_paths_and_src;
2062 /* TODO: check_perms is lousy, want to just say "writable" here */
2063 if (check_perms(old_dir_i, S_IWUSR) || check_perms(new_dir_i, S_IWUSR)) {
2066 goto out_paths_and_src;
2068 /* TODO: if we're doing a rename that moves a directory, we need to make
2069 * sure the new_path doesn't include the old_path. it's not as simple as
2070 * just checking, since there could be a concurrent rename that breaks the
2071 * check later. e.g. what if new_dir's parent is being moved into a child
2074 * linux has a per-fs rename mutex for these scenarios, so only one can
2075 * proceed at a time. i don't see another way to deal with it either.
2076 * maybe something like flagging all dentries on the new_path with "do not
2079 /* TODO: this is all very racy. right after we do a new_d lookup, someone
2080 * else could create or unlink new_d. need to lock here, or else push this
2083 * For any locking scheme, we probably need to lock both the old and new
2084 * dirs. To prevent deadlock, we need a total ordering of all inodes (or
2085 * dentries, if we locking them instead). inode number or struct inode*
2086 * will work for this. */
2087 new_d = do_lookup(new_dir_d, nd_n->last.name);
2089 if (new_d->d_inode == old_d->d_inode)
2090 goto out_paths_and_refs; /* rename does nothing */
2091 /* TODO: Here's a bunch of other racy checks we need to do, maybe in the
2094 * if src is a dir, dst must be an empty dir if it exists (RACYx2)
2095 * racing on dst being created and it getting new entries
2096 * if src is a file, dst must be a file if it exists (RACY)
2097 * racing on dst being created and still being a file
2098 * racing on dst being unlinked and a new one being added
2100 /* TODO: we should allow empty dirs */
2101 if (S_ISDIR(new_d->d_inode->i_mode)) {
2104 goto out_paths_and_refs;
2106 /* TODO: need this to be atomic with rename */
2107 error = new_dir_i->i_op->unlink(new_dir_i, new_d);
2111 goto out_paths_and_refs;
2113 new_d->d_flags |= DENTRY_DYING;
2114 /* TODO: racy with other lookups on new_d */
2115 dcache_remove(new_d->d_sb, new_d);
2116 new_d->d_inode->i_nlink--; /* TODO: race here, esp with a decref */
2117 kref_put(&new_d->d_kref);
2119 /* new_d is just a vessel for the name. somewhat lousy. */
2120 new_d = get_dentry(new_dir_d->d_sb, new_dir_d, nd_n->last.name);
2122 /* TODO: more races. need to remove old_d from the dcache, since we're
2123 * about to change its parentage. could be readded concurrently. */
2124 dcache_remove(old_dir_d->d_sb, old_d);
2125 error = new_dir_i->i_op->rename(old_dir_i, old_d, new_dir_i, new_d);
2127 /* TODO: oh crap, we already unlinked! now we're screwed, and violated
2128 * our atomicity requirements. */
2129 printk("[kernel] rename failed, you might have lost data\n");
2132 goto out_paths_and_refs;
2135 /* old_dir loses old_d, new_dir gains old_d, renamed to new_d. this is
2136 * particularly cumbersome since there are two levels here: the FS has its
2137 * info about where things are, and the VFS has its dentry tree. and it's
2138 * all racy (TODO). */
2139 dentry_set_name(old_d, new_d->d_name.name);
2140 old_d->d_parent = new_d->d_parent;
2141 if (S_ISDIR(old_d->d_inode->i_mode)) {
2142 TAILQ_REMOVE(&old_dir_d->d_subdirs, old_d, d_subdirs_link);
2143 old_dir_i->i_nlink--; /* TODO: racy, etc */
2144 TAILQ_INSERT_TAIL(&new_dir_d->d_subdirs, old_d, d_subdirs_link);
2145 new_dir_i->i_nlink--; /* TODO: racy, etc */
2148 /* and then the third level: dcache stuff. we could have old versions of
2149 * old_d or negative versions of new_d sitting around. dcache_put should
2150 * replace a potentially negative dentry for new_d (now called old_d) */
2151 dcache_put(old_dir_d->d_sb, old_d);
2153 /* TODO could have a helper for this, but it's going away soon */
2155 old_dir_i->i_ctime.tv_sec = now;
2156 old_dir_i->i_mtime.tv_sec = now;
2157 old_dir_i->i_ctime.tv_nsec = 0;
2158 old_dir_i->i_mtime.tv_nsec = 0;
2159 new_dir_i->i_ctime.tv_sec = now;
2160 new_dir_i->i_mtime.tv_sec = now;
2161 new_dir_i->i_ctime.tv_nsec = 0;
2162 new_dir_i->i_mtime.tv_nsec = 0;
2166 kref_put(&new_d->d_kref);
2168 kref_put(&old_d->d_kref);
2176 int do_truncate(struct inode *inode, off64_t len)
2185 printk("[kernel] truncate for > petabyte, probably a bug\n");
2186 /* continuing, not too concerned. could set EINVAL or EFBIG */
2188 spin_lock(&inode->i_lock);
2189 old_len = inode->i_size;
2190 if (old_len == len) {
2191 spin_unlock(&inode->i_lock);
2194 inode->i_size = len;
2195 /* truncate can't block, since we're holding the spinlock. but it can rely
2196 * on that lock being held */
2197 inode->i_op->truncate(inode);
2198 spin_unlock(&inode->i_lock);
2200 if (old_len < len) {
2201 pm_remove_contig(inode->i_mapping, old_len >> PGSHIFT,
2202 (len >> PGSHIFT) - (old_len >> PGSHIFT));
2205 inode->i_ctime.tv_sec = now;
2206 inode->i_mtime.tv_sec = now;
2207 inode->i_ctime.tv_nsec = 0;
2208 inode->i_mtime.tv_nsec = 0;
2212 struct file *alloc_file(void)
2214 struct file *file = kmem_cache_alloc(file_kcache, 0);
2219 /* one for the ref passed out*/
2220 kref_init(&file->f_kref, file_release, 1);
2224 /* Opens and returns the file specified by dentry */
2225 struct file *dentry_open(struct dentry *dentry, int flags)
2227 struct inode *inode;
2230 inode = dentry->d_inode;
2231 /* Do the mode first, since we can still error out. f_mode stores how the
2232 * OS file is open, which can be more restrictive than the i_mode */
2233 switch (flags & (O_RDONLY | O_WRONLY | O_RDWR)) {
2235 desired_mode = S_IRUSR;
2238 desired_mode = S_IWUSR;
2241 desired_mode = S_IRUSR | S_IWUSR;
2246 if (check_perms(inode, desired_mode))
2248 file = alloc_file();
2251 file->f_mode = desired_mode;
2252 /* Add to the list of all files of this SB */
2253 TAILQ_INSERT_TAIL(&inode->i_sb->s_files, file, f_list);
2254 kref_get(&dentry->d_kref, 1);
2255 file->f_dentry = dentry;
2256 kref_get(&inode->i_sb->s_mount->mnt_kref, 1);
2257 file->f_vfsmnt = inode->i_sb->s_mount; /* saving a ref to the vmnt...*/
2258 file->f_op = inode->i_fop;
2259 /* Don't store creation flags */
2260 file->f_flags = flags & ~O_CREAT_FLAGS;
2262 file->f_uid = inode->i_uid;
2263 file->f_gid = inode->i_gid;
2265 // struct event_poll_tailq f_ep_links;
2266 spinlock_init(&file->f_ep_lock);
2267 file->f_privdata = 0; /* prob overriden by the fs */
2268 file->f_mapping = inode->i_mapping;
2269 file->f_op->open(inode, file);
2276 /* Closes a file, fsync, whatever else is necessary. Called when the kref hits
2277 * 0. Note that the file is not refcounted on the s_files list, nor is the
2278 * f_mapping refcounted (it is pinned by the i_mapping). */
2279 void file_release(struct kref *kref)
2281 struct file *file = container_of(kref, struct file, f_kref);
2283 struct super_block *sb = file->f_dentry->d_sb;
2284 spin_lock(&sb->s_lock);
2285 TAILQ_REMOVE(&sb->s_files, file, f_list);
2286 spin_unlock(&sb->s_lock);
2288 /* TODO: fsync (BLK). also, we may want to parallelize the blocking that
2289 * could happen in here (spawn kernel threads)... */
2290 file->f_op->release(file->f_dentry->d_inode, file);
2291 /* Clean up the other refs we hold */
2292 kref_put(&file->f_dentry->d_kref);
2293 kref_put(&file->f_vfsmnt->mnt_kref);
2294 kmem_cache_free(file_kcache, file);
2297 /* Process-related File management functions */
2299 /* Given any FD, get the appropriate file, 0 o/w */
2300 struct file *get_file_from_fd(struct files_struct *open_files, int file_desc)
2302 struct file *retval = 0;
2305 spin_lock(&open_files->lock);
2306 if (open_files->closed) {
2307 spin_unlock(&open_files->lock);
2310 if (file_desc < open_files->max_fdset) {
2311 if (GET_BITMASK_BIT(open_files->open_fds->fds_bits, file_desc)) {
2312 /* while max_files and max_fdset might not line up, we should never
2313 * have a valid fdset higher than files */
2314 assert(file_desc < open_files->max_files);
2315 retval = open_files->fd[file_desc].fd_file;
2316 /* 9ns might be using this one, in which case file == 0 */
2318 kref_get(&retval->f_kref, 1);
2321 spin_unlock(&open_files->lock);
2325 /* Grow the vfs fd set */
2326 static int grow_fd_set(struct files_struct *open_files)
2329 struct file_desc *nfd, *ofd;
2331 /* Only update open_fds once. If currently pointing to open_fds_init, then
2332 * update it to point to a newly allocated fd_set with space for
2333 * NR_FILE_DESC_MAX */
2334 if (open_files->open_fds == (struct fd_set*)&open_files->open_fds_init) {
2335 open_files->open_fds = kzmalloc(sizeof(struct fd_set), 0);
2336 memmove(open_files->open_fds, &open_files->open_fds_init,
2337 sizeof(struct small_fd_set));
2340 /* Grow the open_files->fd array in increments of NR_OPEN_FILES_DEFAULT */
2341 n = open_files->max_files + NR_OPEN_FILES_DEFAULT;
2342 if (n > NR_FILE_DESC_MAX)
2344 nfd = kzmalloc(n * sizeof(struct file_desc), 0);
2348 /* Move the old array on top of the new one */
2349 ofd = open_files->fd;
2350 memmove(nfd, ofd, open_files->max_files * sizeof(struct file_desc));
2352 /* Update the array and the maxes for both max_files and max_fdset */
2353 open_files->fd = nfd;
2354 open_files->max_files = n;
2355 open_files->max_fdset = n;
2357 /* Only free the old one if it wasn't pointing to open_files->fd_array */
2358 if (ofd != open_files->fd_array)
2363 /* Free the vfs fd set if necessary */
2364 static void free_fd_set(struct files_struct *open_files)
2367 if (open_files->open_fds != (struct fd_set*)&open_files->open_fds_init) {
2368 assert(open_files->fd != open_files->fd_array);
2369 /* need to reset the pointers to the internal addrs, in case we take a
2370 * look while debugging. 0 them out, since they have old data. our
2371 * current versions should all be closed. */
2372 memset(&open_files->open_fds_init, 0, sizeof(struct small_fd_set));
2373 memset(&open_files->fd_array, 0, sizeof(open_files->fd_array));
2375 free_me = open_files->open_fds;
2376 open_files->open_fds = (struct fd_set*)&open_files->open_fds_init;
2379 free_me = open_files->fd;
2380 open_files->fd = open_files->fd_array;
2385 /* 9ns: puts back an FD from the VFS-FD-space. */
2386 int put_fd(struct files_struct *open_files, int file_desc)
2388 if (file_desc < 0) {
2389 warn("Negative FD!\n");
2392 spin_lock(&open_files->lock);
2393 if (file_desc < open_files->max_fdset) {
2394 if (GET_BITMASK_BIT(open_files->open_fds->fds_bits, file_desc)) {
2395 /* while max_files and max_fdset might not line up, we should never
2396 * have a valid fdset higher than files */
2397 assert(file_desc < open_files->max_files);
2398 CLR_BITMASK_BIT(open_files->open_fds->fds_bits, file_desc);
2401 spin_unlock(&open_files->lock);
2405 /* Remove FD from the open files, if it was there, and return f. Currently,
2406 * this decref's f, so the return value is not consumable or even usable. This
2407 * hasn't been thought through yet. */
2408 struct file *put_file_from_fd(struct files_struct *open_files, int file_desc)
2410 struct file *file = 0;
2413 spin_lock(&open_files->lock);
2414 if (file_desc < open_files->max_fdset) {
2415 if (GET_BITMASK_BIT(open_files->open_fds->fds_bits, file_desc)) {
2416 /* while max_files and max_fdset might not line up, we should never
2417 * have a valid fdset higher than files */
2418 assert(file_desc < open_files->max_files);
2419 file = open_files->fd[file_desc].fd_file;
2420 open_files->fd[file_desc].fd_file = 0;
2421 assert(file); /* 9ns shouldn't call this put */
2422 kref_put(&file->f_kref);
2423 CLR_BITMASK_BIT(open_files->open_fds->fds_bits, file_desc);
2426 spin_unlock(&open_files->lock);
2430 static int __get_fd(struct files_struct *open_files, int low_fd)
2434 if ((low_fd < 0) || (low_fd > NR_FILE_DESC_MAX))
2436 if (open_files->closed)
2437 return -EINVAL; /* won't matter, they are dying */
2439 /* Loop until we have a valid slot (we grow the fd_array at the bottom of
2440 * the loop if we haven't found a slot in the current array */
2441 while (slot == -1) {
2442 for (low_fd; low_fd < open_files->max_fdset; low_fd++) {
2443 if (GET_BITMASK_BIT(open_files->open_fds->fds_bits, low_fd))
2446 SET_BITMASK_BIT(open_files->open_fds->fds_bits, slot);
2447 assert(slot < open_files->max_files &&
2448 open_files->fd[slot].fd_file == 0);
2449 if (slot >= open_files->next_fd)
2450 open_files->next_fd = slot + 1;
2454 if ((error = grow_fd_set(open_files)))
2461 /* Gets and claims a free FD, used by 9ns. < 0 == error. cloexec is tracked on
2462 * the VFS FD. It's value will be O_CLOEXEC (not 1) or 0. */
2463 int get_fd(struct files_struct *open_files, int low_fd, int cloexec)
2466 spin_lock(&open_files->lock);
2467 slot = __get_fd(open_files, low_fd);
2468 if (cloexec && (slot >= 0))
2469 open_files->fd[slot].fd_flags |= FD_CLOEXEC;
2470 spin_unlock(&open_files->lock);
2474 static int __claim_fd(struct files_struct *open_files, int file_desc)
2477 if ((file_desc < 0) || (file_desc > NR_FILE_DESC_MAX))
2479 if (open_files->closed)
2480 return -EINVAL; /* won't matter, they are dying */
2482 /* Grow the open_files->fd_set until the file_desc can fit inside it */
2483 while(file_desc >= open_files->max_files) {
2484 if ((error = grow_fd_set(open_files)))
2489 /* If we haven't grown, this could be a problem, so check for it */
2490 if (GET_BITMASK_BIT(open_files->open_fds->fds_bits, file_desc))
2491 return -ENFILE; /* Should never really happen. Here to catch bugs. */
2493 SET_BITMASK_BIT(open_files->open_fds->fds_bits, file_desc);
2494 assert(file_desc < open_files->max_files &&
2495 open_files->fd[file_desc].fd_file == 0);
2496 if (file_desc >= open_files->next_fd)
2497 open_files->next_fd = file_desc + 1;
2501 /* Claims a specific FD when duping FDs. used by 9ns. < 0 == error. No need
2502 * for cloexec here, since it's not used during dup. */
2503 int claim_fd(struct files_struct *open_files, int file_desc)
2506 spin_lock(&open_files->lock);
2507 ret = __claim_fd(open_files, file_desc);
2508 spin_unlock(&open_files->lock);
2512 /* Inserts the file in the files_struct, returning the corresponding new file
2513 * descriptor, or an error code. We start looking for open fds from low_fd.
2515 * Passing cloexec is a bit cheap, since we might want to expand it to support
2516 * more FD options in the future. */
2517 int insert_file(struct files_struct *open_files, struct file *file, int low_fd,
2518 bool must, bool cloexec)
2521 spin_lock(&open_files->lock);
2523 ret = __claim_fd(open_files, low_fd);
2525 spin_unlock(&open_files->lock);
2528 assert(!ret); /* issues with claim_fd returning status, not the fd */
2531 slot = __get_fd(open_files, low_fd);
2535 spin_unlock(&open_files->lock);
2538 assert(slot < open_files->max_files &&
2539 open_files->fd[slot].fd_file == 0);
2540 kref_get(&file->f_kref, 1);
2541 open_files->fd[slot].fd_file = file;
2542 open_files->fd[slot].fd_flags = 0;
2544 open_files->fd[slot].fd_flags |= FD_CLOEXEC;
2545 spin_unlock(&open_files->lock);
2549 /* Closes all open files. Mostly just a "put" for all files. If cloexec, it
2550 * will only close the FDs with FD_CLOEXEC (opened with O_CLOEXEC or fcntld). */
2551 void close_all_files(struct files_struct *open_files, bool cloexec)
2554 spin_lock(&open_files->lock);
2555 if (open_files->closed) {
2556 spin_unlock(&open_files->lock);
2559 for (int i = 0; i < open_files->max_fdset; i++) {
2560 if (GET_BITMASK_BIT(open_files->open_fds->fds_bits, i)) {
2561 /* while max_files and max_fdset might not line up, we should never
2562 * have a valid fdset higher than files */
2563 assert(i < open_files->max_files);
2564 file = open_files->fd[i].fd_file;
2565 /* no file == 9ns uses the FD. they will deal with it */
2568 if (cloexec && !(open_files->fd[i].fd_flags & FD_CLOEXEC))
2570 /* Actually close the file */
2571 open_files->fd[i].fd_file = 0;
2573 kref_put(&file->f_kref);
2574 CLR_BITMASK_BIT(open_files->open_fds->fds_bits, i);
2578 free_fd_set(open_files);
2579 open_files->closed = TRUE;
2581 spin_unlock(&open_files->lock);
2584 /* Inserts all of the files from src into dst, used by sys_fork(). */
2585 void clone_files(struct files_struct *src, struct files_struct *dst)
2588 spin_lock(&src->lock);
2590 spin_unlock(&src->lock);
2593 spin_lock(&dst->lock);
2595 warn("Destination closed before it opened");
2596 spin_unlock(&dst->lock);
2597 spin_unlock(&src->lock);
2600 for (int i = 0; i < src->max_fdset; i++) {
2601 if (GET_BITMASK_BIT(src->open_fds->fds_bits, i)) {
2602 /* while max_files and max_fdset might not line up, we should never
2603 * have a valid fdset higher than files */
2604 assert(i < src->max_files);
2605 file = src->fd[i].fd_file;
2606 assert(i < dst->max_files && dst->fd[i].fd_file == 0);
2607 SET_BITMASK_BIT(dst->open_fds->fds_bits, i);
2608 dst->fd[i].fd_file = file;
2609 /* no file means 9ns is using it, they clone separately */
2611 kref_get(&file->f_kref, 1);
2612 if (i >= dst->next_fd)
2613 dst->next_fd = i + 1;
2616 spin_unlock(&dst->lock);
2617 spin_unlock(&src->lock);
2620 static void __chpwd(struct fs_struct *fs_env, struct dentry *new_pwd)
2622 struct dentry *old_pwd;
2623 kref_get(&new_pwd->d_kref, 1);
2624 /* writer lock, make sure we replace pwd with ours. could also CAS.
2625 * readers don't lock at all, so they need to either loop, or we need to
2626 * delay releasing old_pwd til an RCU grace period. */
2627 spin_lock(&fs_env->lock);
2628 old_pwd = fs_env->pwd;
2629 fs_env->pwd = new_pwd;
2630 spin_unlock(&fs_env->lock);
2631 kref_put(&old_pwd->d_kref);
2634 /* Change the working directory of the given fs env (one per process, at this
2635 * point). Returns 0 for success, sets errno and returns -1 otherwise. */
2636 int do_chdir(struct fs_struct *fs_env, char *path)
2638 struct nameidata nd_r = {0}, *nd = &nd_r;
2640 error = path_lookup(path, LOOKUP_DIRECTORY, nd);
2646 /* nd->dentry is the place we want our PWD to be */
2647 __chpwd(fs_env, nd->dentry);
2652 int do_fchdir(struct fs_struct *fs_env, struct file *file)
2654 if ((file->f_dentry->d_inode->i_mode & __S_IFMT) != __S_IFDIR) {
2658 __chpwd(fs_env, file->f_dentry);
2662 /* Returns a null-terminated string of up to length cwd_l containing the
2663 * absolute path of fs_env, (up to fs_env's root). Be sure to kfree the char*
2664 * "kfree_this" when you are done with it. We do this since it's easier to
2665 * build this string going backwards. Note cwd_l is not a strlen, it's an
2667 char *do_getcwd(struct fs_struct *fs_env, char **kfree_this, size_t cwd_l)
2669 struct dentry *dentry = fs_env->pwd;
2671 char *path_start, *kbuf;
2677 kbuf = kmalloc(cwd_l, 0);
2683 kbuf[cwd_l - 1] = '\0';
2684 kbuf[cwd_l - 2] = '/';
2685 /* for each dentry in the path, all the way back to the root of fs_env, we
2686 * grab the dentry name, push path_start back enough, and write in the name,
2687 * using /'s to terminate. We skip the root, since we don't want it's
2688 * actual name, just "/", which is set before each loop. */
2689 path_start = kbuf + cwd_l - 2; /* the last byte written */
2690 while (dentry != fs_env->root) {
2691 link_len = dentry->d_name.len; /* this does not count the \0 */
2692 if (path_start - (link_len + 2) < kbuf) {
2697 path_start -= link_len;
2698 strncpy(path_start, dentry->d_name.name, link_len);
2701 dentry = dentry->d_parent;
2706 static void print_dir(struct dentry *dentry, char *buf, int depth)
2708 struct dentry *child_d;
2709 struct dirent next = {0};
2713 if (!S_ISDIR(dentry->d_inode->i_mode)) {
2714 warn("Thought this was only directories!!");
2717 /* Print this dentry */
2718 printk("%s%s/ nlink: %d\n", buf, dentry->d_name.name,
2719 dentry->d_inode->i_nlink);
2720 if (dentry->d_mount_point) {
2721 dentry = dentry->d_mounted_fs->mnt_root;
2725 /* Set buffer for our kids */
2727 dir = dentry_open(dentry, 0);
2729 panic("Filesystem seems inconsistent - unable to open a dir!");
2730 /* Process every child, recursing on directories */
2732 retval = dir->f_op->readdir(dir, &next);
2734 /* Skip .., ., and empty entries */
2735 if (!strcmp("..", next.d_name) || !strcmp(".", next.d_name) ||
2738 /* there is an entry, now get its dentry */
2739 child_d = do_lookup(dentry, next.d_name);
2741 panic("Inconsistent FS, dirent doesn't have a dentry!");
2742 /* Recurse for directories, or just print the name for others */
2743 switch (child_d->d_inode->i_mode & __S_IFMT) {
2745 print_dir(child_d, buf, depth + 1);
2748 printk("%s%s size(B): %d nlink: %d\n", buf, next.d_name,
2749 child_d->d_inode->i_size, child_d->d_inode->i_nlink);
2752 printk("%s%s -> %s\n", buf, next.d_name,
2753 child_d->d_inode->i_op->readlink(child_d));
2756 printk("%s%s (char device) nlink: %d\n", buf, next.d_name,
2757 child_d->d_inode->i_nlink);
2760 printk("%s%s (block device) nlink: %d\n", buf, next.d_name,
2761 child_d->d_inode->i_nlink);
2764 warn("Look around you! Unknown filetype!");
2766 kref_put(&child_d->d_kref);
2772 /* Reset buffer to the way it was */
2774 kref_put(&dir->f_kref);
2778 int ls_dash_r(char *path)
2780 struct nameidata nd_r = {0}, *nd = &nd_r;
2784 error = path_lookup(path, LOOKUP_ACCESS | LOOKUP_DIRECTORY, nd);
2789 print_dir(nd->dentry, buf, 0);
2794 /* Dummy ops, to catch weird operations we weren't expecting */
2795 int dummy_create(struct inode *dir, struct dentry *dentry, int mode,
2796 struct nameidata *nd)
2798 printk("Dummy VFS function %s called!\n", __FUNCTION__);
2802 struct dentry *dummy_lookup(struct inode *dir, struct dentry *dentry,
2803 struct nameidata *nd)
2805 printk("Dummy VFS function %s called!\n", __FUNCTION__);
2809 int dummy_link(struct dentry *old_dentry, struct inode *dir,
2810 struct dentry *new_dentry)
2812 printk("Dummy VFS function %s called!\n", __FUNCTION__);
2816 int dummy_unlink(struct inode *dir, struct dentry *dentry)
2818 printk("Dummy VFS function %s called!\n", __FUNCTION__);
2822 int dummy_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
2824 printk("Dummy VFS function %s called!\n", __FUNCTION__);
2828 int dummy_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2830 printk("Dummy VFS function %s called!\n", __FUNCTION__);
2834 int dummy_rmdir(struct inode *dir, struct dentry *dentry)
2836 printk("Dummy VFS function %s called!\n", __FUNCTION__);
2840 int dummy_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
2842 printk("Dummy VFS function %s called!\n", __FUNCTION__);
2846 int dummy_rename(struct inode *old_dir, struct dentry *old_dentry,
2847 struct inode *new_dir, struct dentry *new_dentry)
2849 printk("Dummy VFS function %s called!\n", __FUNCTION__);
2853 char *dummy_readlink(struct dentry *dentry)
2855 printk("Dummy VFS function %s called!\n", __FUNCTION__);
2859 void dummy_truncate(struct inode *inode)
2861 printk("Dummy VFS function %s called!\n", __FUNCTION__);
2864 int dummy_permission(struct inode *inode, int mode, struct nameidata *nd)
2866 printk("Dummy VFS function %s called!\n", __FUNCTION__);
2870 int dummy_d_revalidate(struct dentry *dir, struct nameidata *nd)
2872 printk("Dummy VFS function %s called!\n", __FUNCTION__);
2876 int dummy_d_hash(struct dentry *dentry, struct qstr *name)
2878 printk("Dummy VFS function %s called!\n", __FUNCTION__);
2882 int dummy_d_compare(struct dentry *dir, struct qstr *name1, struct qstr *name2)
2884 printk("Dummy VFS function %s called!\n", __FUNCTION__);
2888 int dummy_d_delete(struct dentry *dentry)
2890 printk("Dummy VFS function %s called!\n", __FUNCTION__);
2894 int dummy_d_release(struct dentry *dentry)
2896 printk("Dummy VFS function %s called!\n", __FUNCTION__);
2900 void dummy_d_iput(struct dentry *dentry, struct inode *inode)
2902 printk("Dummy VFS function %s called!\n", __FUNCTION__);
2905 struct inode_operations dummy_i_op = {
2920 struct dentry_operations dummy_d_op = {