1 /* 2 * Copyright (c) 1989 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 * 7 * @(#)vnode.h 7.31 (Berkeley) 12/05/90 8 */ 9 10 #include <machine/endian.h> 11 12 /* 13 * The vnode is the focus of all file activity in UNIX. 14 * There is a unique vnode allocated for each active file, 15 * each current directory, each mounted-on file, text file, and the root. 16 */ 17 18 /* 19 * vnode types. VNON means no type. 20 */ 21 enum vtype { VNON, VREG, VDIR, VBLK, VCHR, VLNK, VSOCK, VFIFO, VBAD }; 22 23 /* 24 * Vnode tag types. 25 * These are for the benefit of external programs only (e.g., pstat) 26 * and should NEVER be inspected inside the kernel. 27 */ 28 enum vtagtype { VT_NON, VT_UFS, VT_NFS, VT_MFS }; 29 30 /* 31 * This defines the maximum size of the private data area 32 * permitted for any file system type. A defined constant 33 * is used rather than a union structure to cut down on the 34 * number of header files that must be included. 35 */ 36 #define VN_MAXPRIVATE 184 37 38 struct vnode { 39 u_long v_flag; /* vnode flags (see below) */ 40 long v_usecount; /* reference count of users */ 41 long v_holdcnt; /* page & buffer references */ 42 u_short v_shlockc; /* count of shared locks */ 43 u_short v_exlockc; /* count of exclusive locks */ 44 off_t v_lastr; /* last read (read-ahead) */ 45 u_long v_id; /* capability identifier */ 46 struct mount *v_mount; /* ptr to vfs we are in */ 47 struct vnodeops *v_op; /* vnode operations */ 48 struct vnode *v_freef; /* vnode freelist forward */ 49 struct vnode **v_freeb; /* vnode freelist back */ 50 struct vnode *v_mountf; /* vnode mountlist forward */ 51 struct vnode **v_mountb; /* vnode mountlist back */ 52 struct buf *v_cleanblkhd; /* clean blocklist head */ 53 struct buf *v_dirtyblkhd; /* dirty blocklist head */ 54 long v_numoutput; /* num of writes in progress */ 55 enum vtype v_type; /* vnode type */ 56 union { 57 struct mount *vu_mountedhere;/* ptr to mounted vfs (VDIR) */ 58 struct socket *vu_socket; /* unix ipc (VSOCK) */ 59 caddr_t vu_vmdata; /* private data for vm */ 60 struct specinfo *vu_specinfo; /* device (VCHR, VBLK) */ 61 struct fifoinfo *vu_fifoinfo; /* fifo (VFIFO) */ 62 } v_un; 63 enum vtagtype v_tag; /* type of underlying data */ 64 char v_data[VN_MAXPRIVATE]; /* private data for fs */ 65 }; 66 #define v_mountedhere v_un.vu_mountedhere 67 #define v_socket v_un.vu_socket 68 #define v_vmdata v_un.vu_vmdata 69 #define v_specinfo v_un.vu_specinfo 70 #define v_fifoinfo v_un.vu_fifoinfo 71 72 /* 73 * vnode flags. 74 */ 75 #define VROOT 0x0001 /* root of its file system */ 76 #define VTEXT 0x0002 /* vnode is a pure text prototype */ 77 #define VSYSTEM 0x0004 /* vnode being used by kernel */ 78 #define VEXLOCK 0x0010 /* exclusive lock */ 79 #define VSHLOCK 0x0020 /* shared lock */ 80 #define VLWAIT 0x0040 /* proc is waiting on shared or excl. lock */ 81 #define VXLOCK 0x0100 /* vnode is locked to change underlying type */ 82 #define VXWANT 0x0200 /* process is waiting for vnode */ 83 #define VBWAIT 0x0400 /* waiting for output to complete */ 84 #define VALIASED 0x0800 /* vnode has an alias */ 85 86 /* 87 * Operations on vnodes. 88 */ 89 struct vnodeops { 90 int (*vn_lookup)( /* ndp */ ); 91 int (*vn_create)( /* ndp, fflags, vap, cred */ ); 92 int (*vn_mknod)( /* ndp, vap, cred */ ); 93 int (*vn_open)( /* vp, fflags, cred */ ); 94 int (*vn_close)( /* vp, fflags, cred */ ); 95 int (*vn_access)( /* vp, fflags, cred */ ); 96 int (*vn_getattr)( /* vp, vap, cred */ ); 97 int (*vn_setattr)( /* vp, vap, cred */ ); 98 99 int (*vn_read)( /* vp, uiop, offp, ioflag, cred */ ); 100 int (*vn_write)( /* vp, uiop, offp, ioflag, cred */ ); 101 int (*vn_ioctl)( /* vp, com, data, fflag, cred */ ); 102 int (*vn_select)( /* vp, which, cred */ ); 103 int (*vn_mmap)( /* vp, ..., cred */ ); 104 int (*vn_fsync)( /* vp, fflags, cred */ ); 105 int (*vn_seek)( /* vp, (old)offp, off, whence */ ); 106 107 int (*vn_remove)( /* ndp */ ); 108 int (*vn_link)( /* vp, ndp */ ); 109 int (*vn_rename)( /* ndp, ndp */ ); 110 int (*vn_mkdir)( /* ndp, vap */ ); 111 int (*vn_rmdir)( /* ndp */ ); 112 int (*vn_symlink)( /* ndp, vap, nm */ ); 113 int (*vn_readdir)( /* vp, uiop, cred, eofflagp */ ); 114 int (*vn_readlink)( /* vp, uiop, cred */ ); 115 116 int (*vn_abortop)( /* ndp */ ); 117 int (*vn_inactive)( /* vp */ ); 118 int (*vn_reclaim)( /* vp */ ); 119 int (*vn_lock)( /* vp */ ); 120 int (*vn_unlock)( /* vp */ ); 121 122 int (*vn_bmap)( /* vp, bn, vpp, bnp */ ); 123 int (*vn_strategy)( /* bp */ ); 124 125 int (*vn_print)( /* vp */ ); 126 int (*vn_islocked)( /* vp */ ); 127 }; 128 129 /* Macros to call the vnode ops */ 130 #define VOP_LOOKUP(v,n) (*((v)->v_op->vn_lookup))((v),(n)) 131 #define VOP_CREATE(n,a) (*((n)->ni_dvp->v_op->vn_create))((n),(a)) 132 #define VOP_MKNOD(n,a,c) (*((n)->ni_dvp->v_op->vn_mknod))((n),(a),(c)) 133 #define VOP_OPEN(v,f,c) (*((v)->v_op->vn_open))((v),(f),(c)) 134 #define VOP_CLOSE(v,f,c) (*((v)->v_op->vn_close))((v),(f),(c)) 135 #define VOP_ACCESS(v,f,c) (*((v)->v_op->vn_access))((v),(f),(c)) 136 #define VOP_GETATTR(v,a,c) (*((v)->v_op->vn_getattr))((v),(a),(c)) 137 #define VOP_SETATTR(v,a,c) (*((v)->v_op->vn_setattr))((v),(a),(c)) 138 #define VOP_READ(v,u,i,c) (*((v)->v_op->vn_read))((v),(u),(i),(c)) 139 #define VOP_WRITE(v,u,i,c) (*((v)->v_op->vn_write))((v),(u),(i),(c)) 140 #define VOP_IOCTL(v,o,d,f,c) (*((v)->v_op->vn_ioctl))((v),(o),(d),(f),(c)) 141 #define VOP_SELECT(v,w,f,c) (*((v)->v_op->vn_select))((v),(w),(f),(c)) 142 #define VOP_MMAP(v,c) (*((v)->v_op->vn_mmap))((v),(c)) 143 #define VOP_FSYNC(v,f,c,w) (*((v)->v_op->vn_fsync))((v),(f),(c),(w)) 144 #define VOP_SEEK(v,p,o,w) (*((v)->v_op->vn_seek))((v),(p),(o),(w)) 145 #define VOP_REMOVE(n) (*((n)->ni_dvp->v_op->vn_remove))(n) 146 #define VOP_LINK(v,n) (*((n)->ni_dvp->v_op->vn_link))((v),(n)) 147 #define VOP_RENAME(s,t) (*((s)->ni_dvp->v_op->vn_rename))((s),(t)) 148 #define VOP_MKDIR(n,a) (*((n)->ni_dvp->v_op->vn_mkdir))((n),(a)) 149 #define VOP_RMDIR(n) (*((n)->ni_dvp->v_op->vn_rmdir))(n) 150 #define VOP_SYMLINK(n,a,m) (*((n)->ni_dvp->v_op->vn_symlink))((n),(a),(m)) 151 #define VOP_READDIR(v,u,c,e) (*((v)->v_op->vn_readdir))((v),(u),(c),(e)) 152 #define VOP_READLINK(v,u,c) (*((v)->v_op->vn_readlink))((v),(u),(c)) 153 #define VOP_ABORTOP(n) (*((n)->ni_dvp->v_op->vn_abortop))(n) 154 #define VOP_INACTIVE(v) (*((v)->v_op->vn_inactive))(v) 155 #define VOP_RECLAIM(v) (*((v)->v_op->vn_reclaim))(v) 156 #define VOP_LOCK(v) (*((v)->v_op->vn_lock))(v) 157 #define VOP_UNLOCK(v) (*((v)->v_op->vn_unlock))(v) 158 #define VOP_BMAP(v,s,p,n) (*((v)->v_op->vn_bmap))((v),(s),(p),(n)) 159 #define VOP_STRATEGY(b) (*((b)->b_vp->v_op->vn_strategy))(b) 160 #define VOP_PRINT(v) (*((v)->v_op->vn_print))(v) 161 #define VOP_ISLOCKED(v) (*((v)->v_op->vn_islocked))(v) 162 163 /* 164 * flags for ioflag 165 */ 166 #define IO_UNIT 0x01 /* do I/O as atomic unit */ 167 #define IO_APPEND 0x02 /* append write to end */ 168 #define IO_SYNC 0x04 /* do I/O synchronously */ 169 #define IO_NODELOCKED 0x08 /* underlying node already locked */ 170 #define IO_NDELAY 0x10 /* FNDELAY flag set in file table */ 171 172 /* 173 * Vnode attributes. A field value of VNOVAL 174 * represents a field whose value is unavailable 175 * (getattr) or which is not to be changed (setattr). 176 */ 177 struct vattr { 178 enum vtype va_type; /* vnode type (for create) */ 179 u_short va_mode; /* files access mode and type */ 180 short va_nlink; /* number of references to file */ 181 uid_t va_uid; /* owner user id */ 182 gid_t va_gid; /* owner group id */ 183 long va_fsid; /* file system id (dev for now) */ 184 long va_fileid; /* file id */ 185 u_quad va_qsize; /* file size in bytes */ 186 long va_blocksize; /* blocksize preferred for i/o */ 187 struct timeval va_atime; /* time of last access */ 188 struct timeval va_mtime; /* time of last modification */ 189 struct timeval va_ctime; /* time file changed */ 190 u_long va_gen; /* generation number of file */ 191 u_long va_flags; /* flags defined for file */ 192 dev_t va_rdev; /* device the special file represents */ 193 u_quad va_qbytes; /* bytes of disk space held by file */ 194 }; 195 #if BYTE_ORDER == LITTLE_ENDIAN 196 #define va_size va_qsize.val[0] 197 #define va_size_rsv va_qsize.val[1] 198 #define va_bytes va_qbytes.val[0] 199 #define va_bytes_rsv va_qbytes.val[1] 200 #else 201 #define va_size va_qsize.val[1] 202 #define va_size_rsv va_qsize.val[0] 203 #define va_bytes va_qbytes.val[1] 204 #define va_bytes_rsv va_qbytes.val[0] 205 #endif 206 207 /* 208 * Modes. Some values same as Ixxx entries from inode.h for now 209 */ 210 #define VSUID 04000 /* set user id on execution */ 211 #define VSGID 02000 /* set group id on execution */ 212 #define VSVTX 01000 /* save swapped text even after use */ 213 #define VREAD 0400 /* read, write, execute permissions */ 214 #define VWRITE 0200 215 #define VEXEC 0100 216 217 /* 218 * Token indicating no attribute value yet assigned 219 */ 220 #define VNOVAL ((unsigned)0xffffffff) 221 222 #ifdef KERNEL 223 /* 224 * public vnode manipulation functions 225 */ 226 extern int vn_open(); /* open vnode */ 227 extern int vn_rdwr(); /* read or write vnode */ 228 extern int vn_close(); /* close vnode */ 229 extern void vattr_null(); /* set attributes to null */ 230 extern int getnewvnode(); /* allocate a new vnode */ 231 extern int bdevvp(); /* allocate a new special dev vnode */ 232 extern struct vnode *checkalias(); /* check for special device aliases */ 233 extern int vcount(); /* total references to a device */ 234 extern int vget(); /* get first reference to a vnode */ 235 extern void vref(); /* increase reference to a vnode */ 236 extern void vput(); /* unlock and release vnode */ 237 extern void vrele(); /* release vnode */ 238 extern void vgone(); /* completely recycle vnode */ 239 extern void vgoneall(); /* recycle vnode and all its aliases */ 240 241 /* 242 * Flags to various vnode functions. 243 */ 244 #define SKIPSYSTEM 0x0001 /* vflush: skip vnodes marked VSYSTEM */ 245 #define FORCECLOSE 0x0002 /* vflush: force file closeure */ 246 #define DOCLOSE 0x0004 /* vclean: close active files */ 247 248 #ifndef DIAGNOSTIC 249 #define VREF(vp) (vp)->v_usecount++ /* increase reference to a vnode */ 250 #define VHOLD(vp) (vp)->v_holdcnt++ /* increase buf or page ref to vnode */ 251 #define HOLDRELE(vp) (vp)->v_holdcnt-- /* decrease buf or page ref to vnode */ 252 #define VATTR_NULL(vap) *(vap) = va_null /* initialize a vattr stucture */ 253 #else /* DIAGNOSTIC */ 254 #define VREF(vp) vref(vp) 255 #define VHOLD(vp) vhold(vp) 256 #define HOLDRELE(vp) holdrele(vp) 257 #define VATTR_NULL(vap) vattr_null(vap) 258 #endif 259 260 #define NULLVP ((struct vnode *)0) 261 262 /* 263 * Global vnode data. 264 */ 265 extern struct vnode *rootdir; /* root (i.e. "/") vnode */ 266 extern int desiredvnodes; /* number of vnodes desired */ 267 extern struct vattr va_null; /* predefined null vattr structure */ 268 #endif 269