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.43 (Berkeley) 12/19/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. There is a unique 16 * vnode allocated for each active file, each current directory, each 17 * 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, VT_LFS }; 31 32 /* 33 * Each underlying filesystem allocates its own private area and hangs 34 * it from v_data. If non-null, this area is free in getnewvnode(). 35 */ 36 struct vnode { 37 u_long v_flag; /* vnode flags (see below) */ 38 short v_usecount; /* reference count of users */ 39 short v_writecount; /* reference count of writers */ 40 long v_holdcnt; /* page & buffer references */ 41 off_t v_lastr; /* last read (read-ahead) */ 42 u_long v_id; /* capability identifier */ 43 struct mount *v_mount; /* ptr to vfs we are in */ 44 struct vnodeops *v_op; /* vnode operations */ 45 struct vnode *v_freef; /* vnode freelist forward */ 46 struct vnode **v_freeb; /* vnode freelist back */ 47 struct vnode *v_mountf; /* vnode mountlist forward */ 48 struct vnode **v_mountb; /* vnode mountlist back */ 49 struct buf *v_cleanblkhd; /* clean blocklist head */ 50 struct buf *v_dirtyblkhd; /* dirty blocklist head */ 51 long v_numoutput; /* num of writes in progress */ 52 enum vtype v_type; /* vnode type */ 53 union { 54 struct mount *vu_mountedhere;/* ptr to mounted vfs (VDIR) */ 55 struct socket *vu_socket; /* unix ipc (VSOCK) */ 56 caddr_t vu_vmdata; /* private data for vm (VREG) */ 57 struct specinfo *vu_specinfo; /* device (VCHR, VBLK) */ 58 struct fifoinfo *vu_fifoinfo; /* fifo (VFIFO) */ 59 } v_un; 60 long v_spare[14]; /* round to 128 bytes */ 61 enum vtagtype v_tag; /* type of underlying data */ 62 void *v_data; /* private data for fs */ 63 }; 64 #define v_mountedhere v_un.vu_mountedhere 65 #define v_socket v_un.vu_socket 66 #define v_vmdata v_un.vu_vmdata 67 #define v_specinfo v_un.vu_specinfo 68 #define v_fifoinfo v_un.vu_fifoinfo 69 70 /* 71 * vnode flags. 72 */ 73 #define VROOT 0x0001 /* root of its file system */ 74 #define VTEXT 0x0002 /* vnode is a pure text prototype */ 75 #define VSYSTEM 0x0004 /* vnode being used by kernel */ 76 #define VXLOCK 0x0100 /* vnode is locked to change underlying type */ 77 #define VXWANT 0x0200 /* process is waiting for vnode */ 78 #define VBWAIT 0x0400 /* waiting for output to complete */ 79 #define VALIASED 0x0800 /* vnode has an alias */ 80 81 /* 82 * Vnode attributes. A field value of VNOVAL represents a field whose value 83 * is unavailable (getattr) or which is not to be changed (setattr). 84 */ 85 struct vattr { 86 enum vtype va_type; /* vnode type (for create) */ 87 u_short va_mode; /* files access mode and type */ 88 short va_nlink; /* number of references to file */ 89 uid_t va_uid; /* owner user id */ 90 gid_t va_gid; /* owner group id */ 91 long va_fsid; /* file system id (dev for now) */ 92 long va_fileid; /* file id */ 93 u_quad_t va_qsize; /* file size in bytes */ 94 long va_blocksize; /* blocksize preferred for i/o */ 95 struct timeval va_atime; /* time of last access */ 96 struct timeval va_mtime; /* time of last modification */ 97 struct timeval va_ctime; /* time file changed */ 98 u_long va_gen; /* generation number of file */ 99 u_long va_flags; /* flags defined for file */ 100 dev_t va_rdev; /* device the special file represents */ 101 short va_pad; /* pad out to long */ 102 u_quad_t va_qbytes; /* bytes of disk space held by file */ 103 u_quad_t va_filerev; /* file modification number */ 104 }; 105 #ifdef _NOQUAD 106 #define va_size va_qsize.val[_QUAD_LOWWORD] 107 #define va_size_rsv va_qsize.val[_QUAD_HIGHWORD] 108 #define va_bytes va_qbytes.val[_QUAD_LOWWORD] 109 #define va_bytes_rsv va_qbytes.val[_QUAD_HIGHWORD] 110 #else 111 #define va_size va_qsize 112 #define va_bytes va_qbytes 113 #endif 114 115 /* 116 * Operations on vnodes. 117 */ 118 #ifdef __STDC__ 119 struct flock; 120 struct nameidata; 121 #endif 122 123 struct vnodeops { 124 #define VOP_LOOKUP(v,n,p) (*((v)->v_op->vop_lookup))(v,n,p) 125 int (*vop_lookup) __P((struct vnode *vp, struct nameidata *ndp, 126 struct proc *p)); 127 #define VOP_CREATE(n,a,p) (*((n)->ni_dvp->v_op->vop_create))(n,a,p) 128 int (*vop_create) __P((struct nameidata *ndp, struct vattr *vap, 129 struct proc *p)); 130 #define VOP_MKNOD(n,a,c,p) (*((n)->ni_dvp->v_op->vop_mknod))(n,a,c,p) 131 int (*vop_mknod) __P((struct nameidata *ndp, struct vattr *vap, 132 struct ucred *cred, struct proc *p)); 133 #define VOP_OPEN(v,f,c,p) (*((v)->v_op->vop_open))(v,f,c,p) 134 int (*vop_open) __P((struct vnode *vp, int mode, 135 struct ucred *cred, struct proc *p)); 136 #define VOP_CLOSE(v,f,c,p) (*((v)->v_op->vop_close))(v,f,c,p) 137 int (*vop_close) __P((struct vnode *vp, int fflag, 138 struct ucred *cred, struct proc *p)); 139 #define VOP_ACCESS(v,f,c,p) (*((v)->v_op->vop_access))(v,f,c,p) 140 int (*vop_access) __P((struct vnode *vp, int mode, 141 struct ucred *cred, struct proc *p)); 142 #define VOP_GETATTR(v,a,c,p) (*((v)->v_op->vop_getattr))(v,a,c,p) 143 int (*vop_getattr) __P((struct vnode *vp, struct vattr *vap, 144 struct ucred *cred, struct proc *p)); 145 #define VOP_SETATTR(v,a,c,p) (*((v)->v_op->vop_setattr))(v,a,c,p) 146 int (*vop_setattr) __P((struct vnode *vp, struct vattr *vap, 147 struct ucred *cred, struct proc *p)); 148 #define VOP_READ(v,u,i,c) (*((v)->v_op->vop_read))(v,u,i,c) 149 int (*vop_read) __P((struct vnode *vp, struct uio *uio, 150 int ioflag, struct ucred *cred)); 151 #define VOP_WRITE(v,u,i,c) (*((v)->v_op->vop_write))(v,u,i,c) 152 int (*vop_write) __P((struct vnode *vp, struct uio *uio, 153 int ioflag, struct ucred *cred)); 154 #define VOP_IOCTL(v,o,d,f,c,p) (*((v)->v_op->vop_ioctl))(v,o,d,f,c,p) 155 int (*vop_ioctl) __P((struct vnode *vp, int command, 156 caddr_t data, int fflag, 157 struct ucred *cred, struct proc *p)); 158 #define VOP_SELECT(v,w,f,c,p) (*((v)->v_op->vop_select))(v,w,f,c,p) 159 int (*vop_select) __P((struct vnode *vp, int which, int fflags, 160 struct ucred *cred, struct proc *p)); 161 #define VOP_MMAP(v,c,p) (*((v)->v_op->vop_mmap))(v,c,p) 162 int (*vop_mmap) __P((struct vnode *vp, int fflags, 163 struct ucred *cred, struct proc *p)); 164 #define VOP_FSYNC(v,f,c,w,p) (*((v)->v_op->vop_fsync))(v,f,c,w,p) 165 int (*vop_fsync) __P((struct vnode *vp, int fflags, 166 struct ucred *cred, int waitfor, 167 struct proc *p)); 168 #define VOP_SEEK(v,p,o,w) (*((v)->v_op->vop_seek))(v,p,o,w) 169 int (*vop_seek) __P((struct vnode *vp, off_t oldoff, 170 off_t newoff, struct ucred *cred)); 171 #define VOP_REMOVE(n,p) (*((n)->ni_dvp->v_op->vop_remove))(n,p) 172 int (*vop_remove) __P((struct nameidata *ndp, struct proc *p)); 173 #define VOP_LINK(v,n,p) (*((n)->ni_dvp->v_op->vop_link))(v,n,p) 174 int (*vop_link) __P((struct vnode *vp, struct nameidata *ndp, 175 struct proc *p)); 176 #define VOP_RENAME(s,t,p) (*((s)->ni_dvp->v_op->vop_rename))(s,t,p) 177 int (*vop_rename) __P((struct nameidata *fndp, 178 struct nameidata *tdnp, struct proc *p)); 179 #define VOP_MKDIR(n,a,p) (*((n)->ni_dvp->v_op->vop_mkdir))(n,a,p) 180 int (*vop_mkdir) __P((struct nameidata *ndp, struct vattr *vap, 181 struct proc *p)); 182 #define VOP_RMDIR(n,p) (*((n)->ni_dvp->v_op->vop_rmdir))(n,p) 183 int (*vop_rmdir) __P((struct nameidata *ndp, struct proc *p)); 184 #define VOP_SYMLINK(n,a,m,p) (*((n)->ni_dvp->v_op->vop_symlink))(n,a,m,p) 185 int (*vop_symlink) __P((struct nameidata *ndp, struct vattr *vap, 186 char *target, struct proc *p)); 187 #define VOP_READDIR(v,u,c,e) (*((v)->v_op->vop_readdir))(v,u,c,e) 188 int (*vop_readdir) __P((struct vnode *vp, struct uio *uio, 189 struct ucred *cred, int *eofflagp)); 190 #define VOP_READLINK(v,u,c) (*((v)->v_op->vop_readlink))(v,u,c) 191 int (*vop_readlink) __P((struct vnode *vp, struct uio *uio, 192 struct ucred *cred)); 193 #define VOP_ABORTOP(n) (*((n)->ni_dvp->v_op->vop_abortop))(n) 194 int (*vop_abortop) __P((struct nameidata *ndp)); 195 #define VOP_INACTIVE(v,p) (*((v)->v_op->vop_inactive))(v,p) 196 int (*vop_inactive) __P((struct vnode *vp, struct proc *p)); 197 #define VOP_RECLAIM(v) (*((v)->v_op->vop_reclaim))(v) 198 int (*vop_reclaim) __P((struct vnode *vp)); 199 #define VOP_LOCK(v) (*((v)->v_op->vop_lock))(v) 200 int (*vop_lock) __P((struct vnode *vp)); 201 #define VOP_UNLOCK(v) (*((v)->v_op->vop_unlock))(v) 202 int (*vop_unlock) __P((struct vnode *vp)); 203 #define VOP_BMAP(v,s,p,n) (*((v)->v_op->vop_bmap))(v,s,p,n) 204 int (*vop_bmap) __P((struct vnode *vp, daddr_t bn, 205 struct vnode **vpp, daddr_t *bnp)); 206 #define VOP_STRATEGY(b) (*((b)->b_vp->v_op->vop_strategy))(b) 207 int (*vop_strategy) __P((struct buf *bp)); 208 #define VOP_PRINT(v) (*((v)->v_op->vop_print))(v) 209 int (*vop_print) __P((struct vnode *vp)); 210 #define VOP_ISLOCKED(v) (((v)->v_flag & VXLOCK) || \ 211 (*((v)->v_op->vop_islocked))(v)) 212 int (*vop_islocked) __P((struct vnode *vp)); 213 #define VOP_ADVLOCK(v,p,o,l,f) (*((v)->v_op->vop_advlock))(v,p,o,l,f) 214 int (*vop_advlock) __P((struct vnode *vp, caddr_t id, int op, 215 struct flock *fl, int flags)); 216 #define VOP_BLKATOFF(v,o,r,b) (*((v)->v_op->vop_blkatoff))(v,o,r,b) 217 int (*vop_blkatoff) __P((struct vnode *vp, 218 off_t offset, char **res, struct buf **bpp)); 219 #define VOP_VGET(v,i,vp) (*((v)->v_op->vop_vget))((v)->v_mount,i,vp) 220 int (*vop_vget) 221 __P((struct mount *mp, ino_t ino, struct vnode **vpp)); 222 #define VOP_VALLOC(v,m,c,vp) (*((v)->v_op->vop_valloc))(v,m,c,vp) 223 int (*vop_valloc) __P((struct vnode *pvp, 224 int mode, struct ucred *cred, struct vnode **vpp)); 225 #define VOP_VFREE(v,i,m) (*((v)->v_op->vop_vfree))(v,i,m) 226 void (*vop_vfree) __P((struct vnode *pvp, ino_t ino, int mode)); 227 #define VOP_TRUNCATE(v,l,f) (*((v)->v_op->vop_truncate))(v,l,f) 228 int (*vop_truncate) 229 __P((struct vnode *vp, u_long length, int flags)); 230 #define VOP_UPDATE(v,ta,tm,w) (*((v)->v_op->vop_update))(v,ta,tm,w) 231 int (*vop_update) __P((struct vnode *vp, 232 struct timeval *ta, struct timeval *tm, int waitfor)); 233 #define VOP_BWRITE(b) (*((b)->b_vp->v_op->vop_bwrite))(b) 234 int (*vop_bwrite) __P((struct buf *bp)); 235 }; 236 237 /* 238 * flags for ioflag 239 */ 240 #define IO_UNIT 0x01 /* do I/O as atomic unit */ 241 #define IO_APPEND 0x02 /* append write to end */ 242 #define IO_SYNC 0x04 /* do I/O synchronously */ 243 #define IO_NODELOCKED 0x08 /* underlying node already locked */ 244 #define IO_NDELAY 0x10 /* FNDELAY flag set in file table */ 245 246 /* 247 * Modes. Some values same as Ixxx entries from inode.h for now 248 */ 249 #define VSUID 04000 /* set user id on execution */ 250 #define VSGID 02000 /* set group id on execution */ 251 #define VSVTX 01000 /* save swapped text even after use */ 252 #define VREAD 0400 /* read, write, execute permissions */ 253 #define VWRITE 0200 254 #define VEXEC 0100 255 256 /* 257 * Token indicating no attribute value yet assigned 258 */ 259 #define VNOVAL (-1) 260 261 #ifdef KERNEL 262 /* 263 * public vnode manipulation functions 264 */ 265 int vn_open __P((struct nameidata *ndp, struct proc *p, int fmode, 266 int cmode)); 267 int vn_close __P((struct vnode *vp, int flags, struct ucred *cred, 268 struct proc *p)); 269 int vn_rdwr __P((enum uio_rw rw, struct vnode *vp, caddr_t base, 270 int len, off_t offset, enum uio_seg segflg, int ioflg, 271 struct ucred *cred, int *aresid, struct proc *p)); 272 int vn_read __P((struct file *fp, struct uio *uio, struct ucred *cred)); 273 int vn_write __P((struct file *fp, struct uio *uio, struct ucred *cred)); 274 int vn_ioctl __P((struct file *fp, int com, caddr_t data, struct proc *p)); 275 int vn_select __P((struct file *fp, int which, struct proc *p)); 276 int vn_closefile __P((struct file *fp, struct proc *p)); 277 int getnewvnode __P((enum vtagtype tag, struct mount *mp, 278 struct vnodeops *vops, struct vnode **vpp)); 279 int bdevvp __P((int dev, struct vnode **vpp)); 280 /* check for special device aliases */ 281 /* XXX nvp_rdev should be type dev_t, not int */ 282 struct vnode *checkalias __P((struct vnode *vp, int nvp_rdev, 283 struct mount *mp)); 284 void vattr_null __P((struct vattr *vap)); 285 int vcount __P((struct vnode *vp)); /* total references to a device */ 286 int vget __P((struct vnode *vp)); /* get first reference to a vnode */ 287 void vref __P((struct vnode *vp)); /* increase reference to a vnode */ 288 void vput __P((struct vnode *vp)); /* unlock and release vnode */ 289 void vrele __P((struct vnode *vp)); /* release vnode */ 290 void vgone __P((struct vnode *vp)); /* completely recycle vnode */ 291 void vgoneall __P((struct vnode *vp));/* recycle vnode and all its aliases */ 292 293 /* 294 * Flags to various vnode functions. 295 */ 296 #define SKIPSYSTEM 0x0001 /* vflush: skip vnodes marked VSYSTEM */ 297 #define FORCECLOSE 0x0002 /* vflush: force file closeure */ 298 #define DOCLOSE 0x0004 /* vclean: close active files */ 299 300 #ifndef DIAGNOSTIC 301 #define VREF(vp) (vp)->v_usecount++ /* increase reference */ 302 #define VHOLD(vp) (vp)->v_holdcnt++ /* increase buf or page ref */ 303 #define HOLDRELE(vp) (vp)->v_holdcnt-- /* decrease buf or page ref */ 304 #define VATTR_NULL(vap) (*(vap) = va_null) /* initialize a vattr */ 305 #else 306 #define VREF(vp) vref(vp) 307 #define VHOLD(vp) vhold(vp) 308 #define HOLDRELE(vp) holdrele(vp) 309 #define VATTR_NULL(vap) vattr_null(vap) 310 #endif 311 312 #define NULLVP ((struct vnode *)NULL) 313 314 /* 315 * Global vnode data. 316 */ 317 extern struct vnode *rootdir; /* root (i.e. "/") vnode */ 318 extern int desiredvnodes; /* number of vnodes desired */ 319 extern struct vattr va_null; /* predefined null vattr structure */ 320 #endif 321