Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/fs/tmpfs - Describe some locking.



details:   https://anonhg.NetBSD.org/src/rev/6ef9b51ec6cb
branches:  trunk
changeset: 765385:6ef9b51ec6cb
user:      rmind <rmind%NetBSD.org@localhost>
date:      Tue May 24 20:17:49 2011 +0000

description:
- Describe some locking.
- Add VOP argument comments, add some asserts.
- Update/fix/remove outdated/missleading comments.
- Clean up, de-indent, KNF, misc.

No functional changes intended.

diffstat:

 sys/fs/tmpfs/tmpfs.h         |  331 ++++----------
 sys/fs/tmpfs/tmpfs_fifoops.c |   45 +-
 sys/fs/tmpfs/tmpfs_fifoops.h |    5 +-
 sys/fs/tmpfs/tmpfs_specops.c |   47 +-
 sys/fs/tmpfs/tmpfs_specops.h |    6 +-
 sys/fs/tmpfs/tmpfs_subr.c    |  589 +++++++++-----------------
 sys/fs/tmpfs/tmpfs_vfsops.c  |  108 +---
 sys/fs/tmpfs/tmpfs_vnops.c   |  933 ++++++++++++++++++++----------------------
 sys/fs/tmpfs/tmpfs_vnops.h   |    6 +-
 9 files changed, 844 insertions(+), 1226 deletions(-)

diffs (truncated from 3710 to 300 lines):

diff -r e9a3cb322966 -r 6ef9b51ec6cb sys/fs/tmpfs/tmpfs.h
--- a/sys/fs/tmpfs/tmpfs.h      Tue May 24 19:12:53 2011 +0000
+++ b/sys/fs/tmpfs/tmpfs.h      Tue May 24 20:17:49 2011 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: tmpfs.h,v 1.40 2011/05/19 03:21:23 rmind Exp $ */
+/*     $NetBSD: tmpfs.h,v 1.41 2011/05/24 20:17:49 rmind Exp $ */
 
 /*
  * Copyright (c) 2005, 2006, 2007 The NetBSD Foundation, Inc.
@@ -41,93 +41,46 @@
 
 /*
  * Internal representation of a tmpfs directory entry.
+ *
+ * All fields are protected by vnode lock.
  */
-struct tmpfs_dirent {
+typedef struct tmpfs_dirent {
        TAILQ_ENTRY(tmpfs_dirent)       td_entries;
 
-       /* Length of the name stored in this directory entry.  This avoids
-        * the need to recalculate it every time the name is used. */
-       uint16_t                        td_namelen;
-
-       /* The name of the entry, allocated from a string pool.  This
-       * string is not required to be zero-terminated; therefore, the
-       * td_namelen field must always be used when accessing its value. */
-       char *                          td_name;
-
-       /* Pointer to the node this entry refers to. */
+       /* Pointer to the inode this entry refers to. */
        struct tmpfs_node *             td_node;
-};
 
-/* A directory in tmpfs holds a sorted list of directory entries, which in
- * turn point to other files (which can be directories themselves).
- *
- * In tmpfs, this list is managed by a tail queue, whose head is defined by
- * the struct tmpfs_dir type.
- *
- * It is imporant to notice that directories do not have entries for . and
- * .. as other file systems do.  These can be generated when requested
- * based on information available by other means, such as the pointer to
- * the node itself in the former case or the pointer to the parent directory
- * in the latter case.  This is done to simplify tmpfs's code and, more
- * importantly, to remove redundancy. */
+       /* Name and its length. */
+       char *                          td_name;
+       uint16_t                        td_namelen;
+} tmpfs_dirent_t;
+
 TAILQ_HEAD(tmpfs_dir, tmpfs_dirent);
 
-/* Each entry in a directory has a cookie that identifies it.  Cookies
- * supersede offsets within directories because, given how tmpfs stores
- * directories in memory, there is no such thing as an offset.  (Emulating
- * a real offset could be very difficult.)
- *
- * The '.', '..' and the end of directory markers have fixed cookies which
- * cannot collide with the cookies generated by other entries.  The cookies
- * fot the other entries are generated based on the memory address on which
- * stores their information is stored.
- *
- * Ideally, using the entry's memory pointer as the cookie would be enough
- * to represent it and it wouldn't cause collisions in any system.
- * Unfortunately, this results in "offsets" with very large values which
- * later raise problems in the Linux compatibility layer (and maybe in other
- * places) as described in PR kern/32034.  Hence we need to workaround this
- * with a rather ugly hack.
- *
- * Linux 32-bit binaries, unless built with _FILE_OFFSET_BITS=64, have off_t
- * set to 'long', which is a 32-bit *signed* long integer.  Regardless of
- * the macro value, GLIBC (2.3 at least) always uses the getdents64
- * system call (when calling readdir) which internally returns off64_t
- * offsets.  In order to make 32-bit binaries work, *GLIBC* converts the
- * 64-bit values returned by the kernel to 32-bit ones and aborts with
- * EOVERFLOW if the conversion results in values that won't fit in 32-bit
- * integers (which it assumes is because the directory is extremely large).
- * This wouldn't cause problems if we were dealing with unsigned integers,
- * but as we have signed integers, this check fails due to sign expansion.
- *
- * For example, consider that the kernel returns the 0xc1234567 cookie to
- * userspace in a off64_t integer.  Later on, GLIBC casts this value to
- * off_t (remember, signed) with code similar to:
- *     system call returns the offset in kernel_value;
- *     off_t casted_value = kernel_value;
- *     if (sizeof(off_t) != sizeof(off64_t) &&
- *         kernel_value != casted_value)
- *             error!
- * In this case, casted_value still has 0xc1234567, but when it is compared
- * for equality against kernel_value, it is promoted to a 64-bit integer and
- * becomes 0xffffffffc1234567, which is different than 0x00000000c1234567.
- * Then, GLIBC assumes this is because the directory is very large.
- *
- * Given that all the above happens in user-space, we have no control over
- * it; therefore we must workaround the issue here.  We do this by
- * truncating the pointer value to a 32-bit integer and hope that there
- * won't be collisions.  In fact, this will not cause any problems in
- * 32-bit platforms but some might arise in 64-bit machines (I'm not sure
- * if they can happen at all in practice).
- *
- * XXX A nicer solution shall be attempted. */
 #if defined(_KERNEL)
+
+/* Validate maximum td_namelen length. */
+CTASSERT(MAXNAMLEN < UINT16_MAX);
+
 #define        TMPFS_DIRCOOKIE_DOT     0
 #define        TMPFS_DIRCOOKIE_DOTDOT  1
 #define        TMPFS_DIRCOOKIE_EOF     2
-static __inline
-off_t
-tmpfs_dircookie(struct tmpfs_dirent *de)
+
+/*
+ * Each entry in a directory has a cookie that identifies it.  Cookies
+ * supersede offsets within directories, as tmpfs has no offsets as such.
+ *
+ * The '.', '..' and the end of directory markers have fixed cookies,
+ * which cannot collide with the cookies generated by other entries.
+ *
+ * The cookies for the other entries are generated based on the memory
+ * address of their representative meta-data structure.
+ *
+ * XXX: Truncating directory cookies to 31 bits now - workaround for
+ * problem with Linux compat, see PR/32034.
+ */
+static inline off_t
+tmpfs_dircookie(tmpfs_dirent_t *de)
 {
        off_t cookie;
 
@@ -138,43 +91,31 @@
 
        return cookie;
 }
-#endif /* defined(_KERNEL) */
-
-/* --------------------------------------------------------------------- */
+#endif
 
 /*
- * Internal representation of a tmpfs file system node.
+ * Internal representation of a tmpfs file system node -- inode.
  *
  * This structure is splitted in two parts: one holds attributes common
  * to all file types and the other holds data that is only applicable to
- * a particular type.  The code must be careful to only access those
- * attributes that are actually allowed by the node's type.
+ * a particular type.
+ *
+ * All fields are protected by vnode lock.  The vnode association itself
+ * is protected by tmpfs_node_t::tn_vlock.
  */
-struct tmpfs_node {
-       /* Doubly-linked list entry which links all existing nodes for a
-        * single file system.  This is provided to ease the removal of
-        * all nodes during the unmount operation. */
+typedef struct tmpfs_node {
        LIST_ENTRY(tmpfs_node)  tn_entries;
 
-       /* The node's type.  Any of 'VBLK', 'VCHR', 'VDIR', 'VFIFO',
-        * 'VLNK', 'VREG' and 'VSOCK' is allowed.  The usage of vnode
-        * types instead of a custom enumeration is to make things simpler
-        * and faster, as we do not need to convert between two types. */
+       /* The inode type: VBLK, VCHR, VDIR, VFIFO, VLNK, VREG or VSOCK. */
        enum vtype              tn_type;
 
-       /* Node identifier. */
+       /* Inode identifier. */
        ino_t                   tn_id;
 
-       /* Node's internal status.  This is used by several file system
-        * operations to do modifications to the node in a delayed
-        * fashion. */
+       /* Inode status flags (for operations in delayed manner). */
        int                     tn_status;
-#define        TMPFS_NODE_ACCESSED     (1 << 1)
-#define        TMPFS_NODE_MODIFIED     (1 << 2)
-#define        TMPFS_NODE_CHANGED      (1 << 3)
 
-       /* The node size.  It does not necessarily match the real amount
-        * of memory consumed by it. */
+       /* The inode size. */
        off_t                   tn_size;
 
        /* Generic node attributes. */
@@ -192,128 +133,105 @@
        /* Head of byte-level lock list (used by tmpfs_advlock). */
        struct lockf *          tn_lockf;
 
-       /* As there is a single vnode for each active file within the
-        * system, care has to be taken to avoid allocating more than one
-        * vnode per file.  In order to do this, a bidirectional association
-        * is kept between vnodes and nodes.
+       /*
+        * Each inode has a corresponding vnode.  It is a bi-directional
+        * association.  Whenever vnode is allocated, its v_data field is
+        * set to the inode it reference, and tmpfs_node_t::tn_vnode is
+        * set to point to the said vnode.
         *
-        * Whenever a vnode is allocated, its v_data field is updated to
-        * point to the node it references.  At the same time, the node's
-        * tn_vnode field is modified to point to the new vnode representing
-        * it.  Further attempts to allocate a vnode for this same node will
+        * Further attempts to allocate a vnode for this same node will
         * result in returning a new reference to the value stored in
-        * tn_vnode.
-        *
-        * May be NULL when the node is unused (that is, no vnode has been
-        * allocated for it or it has been reclaimed). */
+        * tn_vnode.  It may be NULL when the node is unused (that is,
+        * no vnode has been allocated or it has been reclaimed).
+        */
        kmutex_t                tn_vlock;
-       struct vnode *          tn_vnode;
+       vnode_t *               tn_vnode;
 
        union {
-               /* Valid when tn_type == VBLK || tn_type == VCHR. */
+               /* Type case: VBLK or VCHR. */
                struct {
                        dev_t                   tn_rdev;
                } tn_dev;
 
-               /* Valid when tn_type == VDIR. */
+               /* Type case: VDIR. */
                struct {
-                       /* Pointer to the parent directory.  The root
-                        * directory has a pointer to itself in this field;
-                        * this property identifies the root node. */
+                       /* Parent directory (root inode points to itself). */
                        struct tmpfs_node *     tn_parent;
 
-                       /* Head of a tail-queue that links the contents of
-                        * the directory together.  See above for a
-                        * description of its contents. */
+                       /* List of directory entries. */
                        struct tmpfs_dir        tn_dir;
 
-                       /* Number and pointer of the first directory entry
-                        * returned by the readdir operation if it were
-                        * called again to continue reading data from the
-                        * same directory as before.  This is used to speed
-                        * up reads of long directories, assuming that no
-                        * more than one read is in progress at a given time.
-                        * Otherwise, these values are discarded and a linear
-                        * scan is performed from the beginning up to the
-                        * point where readdir starts returning values. */
+                       /*
+                        * Number and pointer of the last directory entry
+                        * returned by the readdir(3) operation.
+                        */
                        off_t                   tn_readdir_lastn;
                        struct tmpfs_dirent *   tn_readdir_lastp;
                } tn_dir;
 
-               /* Valid when tn_type == VLNK. */
+               /* Type case: VLNK. */
                struct tn_lnk {
-                       /* The link's target, allocated from a string pool. */
+                       /* The link's target. */
                        char *                  tn_link;
                } tn_lnk;
 
-               /* Valid when tn_type == VREG. */
+               /* Type case: VREG. */
                struct tn_reg {
-                       /* The contents of regular files stored in a tmpfs
-                        * file system are represented by a single anonymous
-                        * memory object (aobj, for short).  The aobj provides
-                        * direct access to any position within the file,
-                        * because its contents are always mapped in a
-                        * contiguous region of virtual memory.  It is a task
-                        * of the memory management subsystem (see uvm(9)) to
-                        * issue the required page ins or page outs whenever
-                        * a position within the file is accessed. */
+                       /* Underlying UVM object to store contents. */
                        struct uvm_object *     tn_aobj;
                        size_t                  tn_aobj_pages;
                } tn_reg;
        } tn_spec;
-};
-#define TMPFS_NODE_WHITEOUT ((struct tmpfs_node *)-1)
+} tmpfs_node_t;
 
 #if defined(_KERNEL)
+
 LIST_HEAD(tmpfs_node_list, tmpfs_node);
 
-/* --------------------------------------------------------------------- */
+/* Status flags. */
+#define        TMPFS_NODE_ACCESSED     0x01
+#define        TMPFS_NODE_MODIFIED     0x02
+#define        TMPFS_NODE_CHANGED      0x04
+
+#define        TMPFS_NODE_STATUSALL    \
+    (TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED | TMPFS_NODE_CHANGED)
+
+/* White-out inode indicator. */



Home | Main Index | Thread Index | Old Index