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.37 (Berkeley) 05/16/91 8 */ 9 10 #ifndef KERNEL 11 #include <machine/endian.h> 12 #endif 13 14 /* 15 * The vnode is the focus of all file activity in UNIX. 16 * There is a unique vnode allocated for each active file, 17 * each current directory, each mounted-on file, text file, and the root. 18 */ 19 20 /* 21 * vnode types. VNON means no type. 22 */ 23 enum vtype { VNON, VREG, VDIR, VBLK, VCHR, VLNK, VSOCK, VFIFO, VBAD }; 24 25 /* 26 * Vnode tag types. 27 * These are for the benefit of external programs only (e.g., pstat) 28 * and should NEVER be inspected inside the kernel. 29 */ 30 enum vtagtype { VT_NON, VT_UFS, VT_NFS, VT_MFS }; 31 32 /* 33 * This defines the maximum size of the private data area 34 * permitted for any file system type. A defined constant 35 * is used rather than a union structure to cut down on the 36 * number of header files that must be included. 37 */ 38 #define VN_MAXPRIVATE 188 39 40 struct vnode { 41 u_long v_flag; /* vnode flags (see below) */ 42 long v_usecount; /* reference count of users */ 43 long v_holdcnt; /* page & buffer references */ 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 (VREG) */ 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 VXLOCK 0x0100 /* vnode is locked to change underlying type */ 79 #define VXWANT 0x0200 /* process is waiting for vnode */ 80 #define VBWAIT 0x0400 /* waiting for output to complete */ 81 #define VALIASED 0x0800 /* vnode has an alias */ 82 83 /* 84 * Vnode attributes. A field value of VNOVAL 85 * represents a field whose value is unavailable 86 * (getattr) or which is not to be changed (setattr). 87 */ 88 struct vattr { 89 enum vtype va_type; /* vnode type (for create) */ 90 u_short va_mode; /* files access mode and type */ 91 short va_nlink; /* number of references to file */ 92 uid_t va_uid; /* owner user id */ 93 gid_t va_gid; /* owner group id */ 94 long va_fsid; /* file system id (dev for now) */ 95 long va_fileid; /* file id */ 96 u_quad va_qsize; /* file size in bytes */ 97 long va_blocksize; /* blocksize preferred for i/o */ 98 struct timeval va_atime; /* time of last access */ 99 struct timeval va_mtime; /* time of last modification */ 100 struct timeval va_ctime; /* time file changed */ 101 u_long va_gen; /* generation number of file */ 102 u_long va_flags; /* flags defined for file */ 103 dev_t va_rdev; /* device the special file represents */ 104 u_quad va_qbytes; /* bytes of disk space held by file */ 105 }; 106 #if BYTE_ORDER == LITTLE_ENDIAN 107 #define va_size va_qsize.val[0] 108 #define va_size_rsv va_qsize.val[1] 109 #define va_bytes va_qbytes.val[0] 110 #define va_bytes_rsv va_qbytes.val[1] 111 #else 112 #define va_size va_qsize.val[1] 113 #define va_size_rsv va_qsize.val[0] 114 #define va_bytes va_qbytes.val[1] 115 #define va_bytes_rsv va_qbytes.val[0] 116 #endif 117 118 /* 119 * Operations on vnodes. 120 */ 121 #ifdef __STDC__ 122 struct flock; 123 struct nameidata; 124 #endif 125 126 struct vnodeops { 127 int (*vop_lookup) __P((struct vnode *vp, struct nameidata *ndp, 128 struct proc *p)); 129 int (*vop_create) __P((struct nameidata *ndp, struct vattr *vap, 130 struct proc *p)); 131 int (*vop_mknod) __P((struct nameidata *ndp, struct vattr *vap, 132 struct ucred *cred, struct proc *p)); 133 int (*vop_open) __P((struct vnode *vp, int mode, 134 struct ucred *cred, struct proc *p)); 135 int (*vop_close) __P((struct vnode *vp, int fflag, 136 struct ucred *cred, struct proc *p)); 137 int (*vop_access) __P((struct vnode *vp, int mode, 138 struct ucred *cred, struct proc *p)); 139 int (*vop_getattr) __P((struct vnode *vp, struct vattr *vap, 140 struct ucred *cred, struct proc *p)); 141 int (*vop_setattr) __P((struct vnode *vp, struct vattr *vap, 142 struct ucred *cred, struct proc *p)); 143 int (*vop_read) __P((struct vnode *vp, struct uio *uio, 144 int ioflag, struct ucred *cred)); 145 int (*vop_write) __P((struct vnode *vp, struct uio *uio, 146 int ioflag, struct ucred *cred)); 147 int (*vop_ioctl) __P((struct vnode *vp, int command, 148 caddr_t data, int fflag, 149 struct ucred *cred, struct proc *p)); 150 int (*vop_select) __P((struct vnode *vp, int which, int fflags, 151 struct ucred *cred, struct proc *p)); 152 int (*vop_mmap) __P((struct vnode *vp, int fflags, 153 struct ucred *cred, struct proc *p)); 154 int (*vop_fsync) __P((struct vnode *vp, int fflags, 155 struct ucred *cred, int waitfor, 156 struct proc *p)); 157 int (*vop_seek) __P((struct vnode *vp, off_t oldoff, 158 off_t newoff, struct ucred *cred)); 159 int (*vop_remove) __P((struct nameidata *ndp, struct proc *p)); 160 int (*vop_link) __P((struct vnode *vp, struct nameidata *ndp, 161 struct proc *p)); 162 int (*vop_rename) __P((struct nameidata *fndp, 163 struct nameidata *tdnp, struct proc *p)); 164 int (*vop_mkdir) __P((struct nameidata *ndp, struct vattr *vap, 165 struct proc *p)); 166 int (*vop_rmdir) __P((struct nameidata *ndp, struct proc *p)); 167 int (*vop_symlink) __P((struct nameidata *ndp, struct vattr *vap, 168 char *target, struct proc *p)); 169 int (*vop_readdir) __P((struct vnode *vp, struct uio *uio, 170 struct ucred *cred, int *eofflagp)); 171 int (*vop_readlink) __P((struct vnode *vp, struct uio *uio, 172 struct ucred *cred)); 173 int (*vop_abortop) __P((struct nameidata *ndp)); 174 int (*vop_inactive) __P((struct vnode *vp, struct proc *p)); 175 int (*vop_reclaim) __P((struct vnode *vp)); 176 int (*vop_lock) __P((struct vnode *vp)); 177 int (*vop_unlock) __P((struct vnode *vp)); 178 int (*vop_bmap) __P((struct vnode *vp, daddr_t bn, 179 struct vnode **vpp, daddr_t *bnp)); 180 int (*vop_strategy) __P((struct buf *bp)); 181 int (*vop_print) __P((struct vnode *vp)); 182 int (*vop_islocked) __P((struct vnode *vp)); 183 int (*vop_advlock) __P((struct vnode *vp, caddr_t id, int op, 184 struct flock *fl, int flags)); 185 }; 186 187 /* Macros to call the vnode ops */ 188 #define VOP_LOOKUP(v,n,p) (*((v)->v_op->vop_lookup))(v,n,p) 189 #define VOP_CREATE(n,a,p) (*((n)->ni_dvp->v_op->vop_create))(n,a,p) 190 #define VOP_MKNOD(n,a,c,p) (*((n)->ni_dvp->v_op->vop_mknod))(n,a,c,p) 191 #define VOP_OPEN(v,f,c,p) (*((v)->v_op->vop_open))(v,f,c,p) 192 #define VOP_CLOSE(v,f,c,p) (*((v)->v_op->vop_close))(v,f,c,p) 193 #define VOP_ACCESS(v,f,c,p) (*((v)->v_op->vop_access))(v,f,c,p) 194 #define VOP_GETATTR(v,a,c,p) (*((v)->v_op->vop_getattr))(v,a,c,p) 195 #define VOP_SETATTR(v,a,c,p) (*((v)->v_op->vop_setattr))(v,a,c,p) 196 #define VOP_READ(v,u,i,c) (*((v)->v_op->vop_read))(v,u,i,c) 197 #define VOP_WRITE(v,u,i,c) (*((v)->v_op->vop_write))(v,u,i,c) 198 #define VOP_IOCTL(v,o,d,f,c,p) (*((v)->v_op->vop_ioctl))(v,o,d,f,c,p) 199 #define VOP_SELECT(v,w,f,c,p) (*((v)->v_op->vop_select))(v,w,f,c,p) 200 #define VOP_MMAP(v,c,p) (*((v)->v_op->vop_mmap))(v,c,p) 201 #define VOP_FSYNC(v,f,c,w,p) (*((v)->v_op->vop_fsync))(v,f,c,w,p) 202 #define VOP_SEEK(v,p,o,w) (*((v)->v_op->vop_seek))(v,p,o,w) 203 #define VOP_REMOVE(n,p) (*((n)->ni_dvp->v_op->vop_remove))(n,p) 204 #define VOP_LINK(v,n,p) (*((n)->ni_dvp->v_op->vop_link))(v,n,p) 205 #define VOP_RENAME(s,t,p) (*((s)->ni_dvp->v_op->vop_rename))(s,t,p) 206 #define VOP_MKDIR(n,a,p) (*((n)->ni_dvp->v_op->vop_mkdir))(n,a,p) 207 #define VOP_RMDIR(n,p) (*((n)->ni_dvp->v_op->vop_rmdir))(n,p) 208 #define VOP_SYMLINK(n,a,m,p) (*((n)->ni_dvp->v_op->vop_symlink))(n,a,m,p) 209 #define VOP_READDIR(v,u,c,e) (*((v)->v_op->vop_readdir))(v,u,c,e) 210 #define VOP_READLINK(v,u,c) (*((v)->v_op->vop_readlink))(v,u,c) 211 #define VOP_ABORTOP(n) (*((n)->ni_dvp->v_op->vop_abortop))(n) 212 #define VOP_INACTIVE(v,p) (*((v)->v_op->vop_inactive))(v,p) 213 #define VOP_RECLAIM(v) (*((v)->v_op->vop_reclaim))(v) 214 #define VOP_LOCK(v) (*((v)->v_op->vop_lock))(v) 215 #define VOP_UNLOCK(v) (*((v)->v_op->vop_unlock))(v) 216 #define VOP_BMAP(v,s,p,n) (*((v)->v_op->vop_bmap))(v,s,p,n) 217 #define VOP_STRATEGY(b) (*((b)->b_vp->v_op->vop_strategy))(b) 218 #define VOP_PRINT(v) (*((v)->v_op->vop_print))(v) 219 #define VOP_ISLOCKED(v) (((v)->v_flag & VXLOCK) || \ 220 (*((v)->v_op->vop_islocked))(v)) 221 #define VOP_ADVLOCK(v,p,o,l,f) (*((v)->v_op->vop_advlock))(v,p,o,l,f) 222 223 /* 224 * flags for ioflag 225 */ 226 #define IO_UNIT 0x01 /* do I/O as atomic unit */ 227 #define IO_APPEND 0x02 /* append write to end */ 228 #define IO_SYNC 0x04 /* do I/O synchronously */ 229 #define IO_NODELOCKED 0x08 /* underlying node already locked */ 230 #define IO_NDELAY 0x10 /* FNDELAY flag set in file table */ 231 232 /* 233 * Modes. Some values same as Ixxx entries from inode.h for now 234 */ 235 #define VSUID 04000 /* set user id on execution */ 236 #define VSGID 02000 /* set group id on execution */ 237 #define VSVTX 01000 /* save swapped text even after use */ 238 #define VREAD 0400 /* read, write, execute permissions */ 239 #define VWRITE 0200 240 #define VEXEC 0100 241 242 /* 243 * Token indicating no attribute value yet assigned 244 */ 245 #define VNOVAL ((unsigned)0xffffffff) 246 247 #ifdef KERNEL 248 /* 249 * public vnode manipulation functions 250 */ 251 int vn_open __P((struct nameidata *ndp, struct proc *p, int fmode, 252 int cmode)); 253 int vn_rdwr __P((enum uio_rw rw, struct vnode *vp, caddr_t base, 254 int len, off_t offset, enum uio_seg segflg, int ioflg, 255 struct ucred *cred, int *aresid, struct proc *p)); 256 int vn_read __P((struct file *fp, struct uio *uio, struct ucred *cred)); 257 int vn_write __P((struct file *fp, struct uio *uio, struct ucred *cred)); 258 int vn_ioctl __P((struct file *fp, int com, caddr_t data, struct proc *p)); 259 int vn_select __P((struct file *fp, int which, struct proc *p)); 260 int vn_close __P(( struct file *fp, struct proc *p)); 261 int getnewvnode __P((enum vtagtype tag, struct mount *mp, 262 struct vnodeops *vops, struct vnode **vpp)); 263 int bdevvp __P((int dev, struct vnode **vpp)); 264 /* check for special device aliases */ 265 /* XXX nvp_rdev should be type dev_t, not int */ 266 struct vnode *checkalias __P((struct vnode *vp, int nvp_rdev, 267 struct mount *mp)); 268 void vattr_null __P((struct vattr *vap)); 269 int vcount __P((struct vnode *vp)); /* total references to a device */ 270 int vget __P((struct vnode *vp)); /* get first reference to a vnode */ 271 void vref __P((struct vnode *vp)); /* increase reference to a vnode */ 272 void vput __P((struct vnode *vp)); /* unlock and release vnode */ 273 void vrele __P((struct vnode *vp)); /* release vnode */ 274 void vgone __P((struct vnode *vp)); /* completely recycle vnode */ 275 void vgoneall __P((struct vnode *vp));/* recycle vnode and all its aliases */ 276 277 /* 278 * Flags to various vnode functions. 279 */ 280 #define SKIPSYSTEM 0x0001 /* vflush: skip vnodes marked VSYSTEM */ 281 #define FORCECLOSE 0x0002 /* vflush: force file closeure */ 282 #define DOCLOSE 0x0004 /* vclean: close active files */ 283 284 #ifndef DIAGNOSTIC 285 #define VREF(vp) (vp)->v_usecount++ /* increase reference */ 286 #define VHOLD(vp) (vp)->v_holdcnt++ /* increase buf or page ref */ 287 #define HOLDRELE(vp) (vp)->v_holdcnt-- /* decrease buf or page ref */ 288 #define VATTR_NULL(vap) (*(vap) = va_null) /* initialize a vattr */ 289 #else /* DIAGNOSTIC */ 290 #define VREF(vp) vref(vp) 291 #define VHOLD(vp) vhold(vp) 292 #define HOLDRELE(vp) holdrele(vp) 293 #define VATTR_NULL(vap) vattr_null(vap) 294 #endif 295 296 #define NULLVP ((struct vnode *)NULL) 297 298 /* 299 * Global vnode data. 300 */ 301 extern struct vnode *rootdir; /* root (i.e. "/") vnode */ 302 extern int desiredvnodes; /* number of vnodes desired */ 303 extern struct vattr va_null; /* predefined null vattr structure */ 304 #endif 305