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