1 /* 2 * Copyright (c) 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 * 7 * @(#)vnode.h 8.15 (Berkeley) 03/22/95 8 */ 9 10 #include <sys/queue.h> 11 12 /* 13 * The vnode is the focus of all file activity in UNIX. There is a 14 * unique vnode allocated for each active file, each current directory, 15 * 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 by the kernel. 27 */ 28 enum vtagtype { 29 VT_NON, VT_UFS, VT_NFS, VT_MFS, VT_PC, VT_LFS, VT_LOFS, VT_FDESC, 30 VT_PORTAL, VT_NULL, VT_UMAP, VT_KERNFS, VT_PROCFS, VT_AFS, VT_ISOFS, 31 VT_UNION 32 }; 33 34 /* 35 * Each underlying filesystem allocates its own private area and hangs 36 * it from v_data. If non-null, this area is freed in getnewvnode(). 37 */ 38 LIST_HEAD(buflists, buf); 39 40 struct vnode { 41 u_long v_flag; /* vnode flags (see below) */ 42 short v_usecount; /* reference count of users */ 43 short v_writecount; /* reference count of writers */ 44 long v_holdcnt; /* page & buffer references */ 45 daddr_t v_lastr; /* last read (read-ahead) */ 46 u_long v_id; /* capability identifier */ 47 struct mount *v_mount; /* ptr to vfs we are in */ 48 int (**v_op)(); /* vnode operations vector */ 49 TAILQ_ENTRY(vnode) v_freelist; /* vnode freelist */ 50 LIST_ENTRY(vnode) v_mntvnodes; /* vnodes for mount point */ 51 struct buflists v_cleanblkhd; /* clean blocklist head */ 52 struct buflists v_dirtyblkhd; /* dirty blocklist head */ 53 long v_numoutput; /* num of writes in progress */ 54 enum vtype v_type; /* vnode type */ 55 union { 56 struct mount *vu_mountedhere;/* ptr to mounted vfs (VDIR) */ 57 struct socket *vu_socket; /* unix ipc (VSOCK) */ 58 caddr_t vu_vmdata; /* private data for vm (VREG) */ 59 struct specinfo *vu_specinfo; /* device (VCHR, VBLK) */ 60 struct fifoinfo *vu_fifoinfo; /* fifo (VFIFO) */ 61 } v_un; 62 struct nqlease *v_lease; /* Soft reference to lease */ 63 daddr_t v_lastw; /* last write (write cluster) */ 64 daddr_t v_cstart; /* start block of cluster */ 65 daddr_t v_lasta; /* last allocation */ 66 int v_clen; /* length of current cluster */ 67 int v_ralen; /* Read-ahead length */ 68 daddr_t v_maxra; /* last readahead block */ 69 long v_spare[7]; /* round to 128 bytes */ 70 enum vtagtype v_tag; /* type of underlying data */ 71 void *v_data; /* private data for fs */ 72 }; 73 #define v_mountedhere v_un.vu_mountedhere 74 #define v_socket v_un.vu_socket 75 #define v_vmdata v_un.vu_vmdata 76 #define v_specinfo v_un.vu_specinfo 77 #define v_fifoinfo v_un.vu_fifoinfo 78 79 /* 80 * Vnode flags. 81 */ 82 #define VROOT 0x0001 /* root of its file system */ 83 #define VTEXT 0x0002 /* vnode is a pure text prototype */ 84 #define VSYSTEM 0x0004 /* vnode being used by kernel */ 85 #define VISTTY 0x0008 /* vnode represents a tty */ 86 #define VXLOCK 0x0100 /* vnode is locked to change underlying type */ 87 #define VXWANT 0x0200 /* process is waiting for vnode */ 88 #define VBWAIT 0x0400 /* waiting for output to complete */ 89 #define VALIASED 0x0800 /* vnode has an alias */ 90 #define VDIROP 0x1000 /* LFS: vnode is involved in a directory op */ 91 92 /* 93 * Vnode attributes. A field value of VNOVAL represents a field whose value 94 * is unavailable (getattr) or which is not to be changed (setattr). 95 */ 96 struct vattr { 97 enum vtype va_type; /* vnode type (for create) */ 98 u_short va_mode; /* files access mode and type */ 99 short va_nlink; /* number of references to file */ 100 uid_t va_uid; /* owner user id */ 101 gid_t va_gid; /* owner group id */ 102 long va_fsid; /* file system id (dev for now) */ 103 long va_fileid; /* file id */ 104 u_quad_t va_size; /* file size in bytes */ 105 long va_blocksize; /* blocksize preferred for i/o */ 106 struct timespec va_atime; /* time of last access */ 107 struct timespec va_mtime; /* time of last modification */ 108 struct timespec va_ctime; /* time file changed */ 109 u_long va_gen; /* generation number of file */ 110 u_long va_flags; /* flags defined for file */ 111 dev_t va_rdev; /* device the special file represents */ 112 u_quad_t va_bytes; /* bytes of disk space held by file */ 113 u_quad_t va_filerev; /* file modification number */ 114 u_int va_vaflags; /* operations flags, see below */ 115 long va_spare; /* remain quad aligned */ 116 }; 117 118 /* 119 * Flags for va_vaflags. 120 */ 121 #define VA_UTIMES_NULL 0x01 /* utimes argument was NULL */ 122 #define VA_EXCLUSIVE 0x02 /* exclusive create request */ 123 124 /* 125 * Flags for ioflag. 126 */ 127 #define IO_UNIT 0x01 /* do I/O as atomic unit */ 128 #define IO_APPEND 0x02 /* append write to end */ 129 #define IO_SYNC 0x04 /* do I/O synchronously */ 130 #define IO_NODELOCKED 0x08 /* underlying node already locked */ 131 #define IO_NDELAY 0x10 /* FNDELAY flag set in file table */ 132 133 /* 134 * Modes. Some values same as Ixxx entries from inode.h for now. 135 */ 136 #define VSUID 04000 /* set user id on execution */ 137 #define VSGID 02000 /* set group id on execution */ 138 #define VSVTX 01000 /* save swapped text even after use */ 139 #define VREAD 00400 /* read, write, execute permissions */ 140 #define VWRITE 00200 141 #define VEXEC 00100 142 143 /* 144 * Token indicating no attribute value yet assigned. 145 */ 146 #define VNOVAL (-1) 147 148 #ifdef KERNEL 149 /* 150 * Convert between vnode types and inode formats (since POSIX.1 151 * defines mode word of stat structure in terms of inode formats). 152 */ 153 extern enum vtype iftovt_tab[]; 154 extern int vttoif_tab[]; 155 #define IFTOVT(mode) (iftovt_tab[((mode) & S_IFMT) >> 12]) 156 #define VTTOIF(indx) (vttoif_tab[(int)(indx)]) 157 #define MAKEIMODE(indx, mode) (int)(VTTOIF(indx) | (mode)) 158 159 /* 160 * Flags to various vnode functions. 161 */ 162 #define SKIPSYSTEM 0x0001 /* vflush: skip vnodes marked VSYSTEM */ 163 #define FORCECLOSE 0x0002 /* vflush: force file closeure */ 164 #define WRITECLOSE 0x0004 /* vflush: only close writeable files */ 165 #define DOCLOSE 0x0008 /* vclean: close active files */ 166 #define V_SAVE 0x0001 /* vinvalbuf: sync file first */ 167 #define V_SAVEMETA 0x0002 /* vinvalbuf: leave indirect blocks */ 168 #define REVOKEALL 0x0001 /* vop_revoke: revoke all aliases */ 169 170 #ifdef DIAGNOSTIC 171 #define HOLDRELE(vp) holdrele(vp) 172 #define VATTR_NULL(vap) vattr_null(vap) 173 #define VHOLD(vp) vhold(vp) 174 #define VREF(vp) vref(vp) 175 176 void holdrele __P((struct vnode *)); 177 void vattr_null __P((struct vattr *)); 178 void vhold __P((struct vnode *)); 179 void vref __P((struct vnode *)); 180 #else 181 #define HOLDRELE(vp) (vp)->v_holdcnt-- /* decrease buf or page ref */ 182 #define VATTR_NULL(vap) (*(vap) = va_null) /* initialize a vattr */ 183 #define VHOLD(vp) (vp)->v_holdcnt++ /* increase buf or page ref */ 184 #define VREF(vp) (vp)->v_usecount++ /* increase reference */ 185 #endif 186 187 #define NULLVP ((struct vnode *)NULL) 188 189 /* 190 * Global vnode data. 191 */ 192 extern struct vnode *rootvnode; /* root (i.e. "/") vnode */ 193 extern int desiredvnodes; /* number of vnodes desired */ 194 extern struct vattr va_null; /* predefined null vattr structure */ 195 196 /* 197 * Macro/function to check for client cache inconsistency w.r.t. leasing. 198 */ 199 #define LEASE_READ 0x1 /* Check lease for readers */ 200 #define LEASE_WRITE 0x2 /* Check lease for modifiers */ 201 202 #endif /* KERNEL */ 203 204 205 /* 206 * Mods for exensibility. 207 */ 208 209 /* 210 * Flags for vdesc_flags: 211 */ 212 #define VDESC_MAX_VPS 16 213 /* Low order 16 flag bits are reserved for willrele flags for vp arguments. */ 214 #define VDESC_VP0_WILLRELE 0x0001 215 #define VDESC_VP1_WILLRELE 0x0002 216 #define VDESC_VP2_WILLRELE 0x0004 217 #define VDESC_VP3_WILLRELE 0x0008 218 #define VDESC_NOMAP_VPP 0x0100 219 #define VDESC_VPP_WILLRELE 0x0200 220 221 /* 222 * VDESC_NO_OFFSET is used to identify the end of the offset list 223 * and in places where no such field exists. 224 */ 225 #define VDESC_NO_OFFSET -1 226 227 /* 228 * This structure describes the vnode operation taking place. 229 */ 230 struct vnodeop_desc { 231 int vdesc_offset; /* offset in vector--first for speed */ 232 char *vdesc_name; /* a readable name for debugging */ 233 int vdesc_flags; /* VDESC_* flags */ 234 235 /* 236 * These ops are used by bypass routines to map and locate arguments. 237 * Creds and procs are not needed in bypass routines, but sometimes 238 * they are useful to (for example) transport layers. 239 * Nameidata is useful because it has a cred in it. 240 */ 241 int *vdesc_vp_offsets; /* list ended by VDESC_NO_OFFSET */ 242 int vdesc_vpp_offset; /* return vpp location */ 243 int vdesc_cred_offset; /* cred location, if any */ 244 int vdesc_proc_offset; /* proc location, if any */ 245 int vdesc_componentname_offset; /* if any */ 246 /* 247 * Finally, we've got a list of private data (about each operation) 248 * for each transport layer. (Support to manage this list is not 249 * yet part of BSD.) 250 */ 251 caddr_t *vdesc_transports; 252 }; 253 254 #ifdef KERNEL 255 /* 256 * A list of all the operation descs. 257 */ 258 extern struct vnodeop_desc *vnodeop_descs[]; 259 260 261 /* 262 * This macro is very helpful in defining those offsets in the vdesc struct. 263 * 264 * This is stolen from X11R4. I ingored all the fancy stuff for 265 * Crays, so if you decide to port this to such a serious machine, 266 * you might want to consult Intrisics.h's XtOffset{,Of,To}. 267 */ 268 #define VOPARG_OFFSET(p_type,field) \ 269 ((int) (((char *) (&(((p_type)NULL)->field))) - ((char *) NULL))) 270 #define VOPARG_OFFSETOF(s_type,field) \ 271 VOPARG_OFFSET(s_type*,field) 272 #define VOPARG_OFFSETTO(S_TYPE,S_OFFSET,STRUCT_P) \ 273 ((S_TYPE)(((char*)(STRUCT_P))+(S_OFFSET))) 274 275 276 /* 277 * This structure is used to configure the new vnodeops vector. 278 */ 279 struct vnodeopv_entry_desc { 280 struct vnodeop_desc *opve_op; /* which operation this is */ 281 int (*opve_impl)(); /* code implementing this operation */ 282 }; 283 struct vnodeopv_desc { 284 /* ptr to the ptr to the vector where op should go */ 285 int (***opv_desc_vector_p)(); 286 struct vnodeopv_entry_desc *opv_desc_ops; /* null terminated list */ 287 }; 288 289 /* 290 * A default routine which just returns an error. 291 */ 292 int vn_default_error __P((void)); 293 294 /* 295 * A generic structure. 296 * This can be used by bypass routines to identify generic arguments. 297 */ 298 struct vop_generic_args { 299 struct vnodeop_desc *a_desc; 300 /* other random data follows, presumably */ 301 }; 302 303 /* 304 * VOCALL calls an op given an ops vector. We break it out because BSD's 305 * vclean changes the ops vector and then wants to call ops with the old 306 * vector. 307 */ 308 #define VOCALL(OPSV,OFF,AP) (( *((OPSV)[(OFF)])) (AP)) 309 310 /* 311 * This call works for vnodes in the kernel. 312 */ 313 #define VCALL(VP,OFF,AP) VOCALL((VP)->v_op,(OFF),(AP)) 314 #define VDESC(OP) (& __CONCAT(OP,_desc)) 315 #define VOFFSET(OP) (VDESC(OP)->vdesc_offset) 316 317 /* 318 * Finally, include the default set of vnode operations. 319 */ 320 #include <vnode_if.h> 321 322 /* 323 * Public vnode manipulation functions. 324 */ 325 struct file; 326 struct mount; 327 struct nameidata; 328 struct ostat; 329 struct proc; 330 struct stat; 331 struct ucred; 332 struct uio; 333 struct vattr; 334 struct vnode; 335 struct vop_bwrite_args; 336 337 int bdevvp __P((dev_t dev, struct vnode **vpp)); 338 void cvtstat __P((struct stat *st, struct ostat *ost)); 339 int getnewvnode __P((enum vtagtype tag, 340 struct mount *mp, int (**vops)(), struct vnode **vpp)); 341 void insmntque __P((struct vnode *vp, struct mount *mp)); 342 void vattr_null __P((struct vattr *vap)); 343 int vcount __P((struct vnode *vp)); 344 int vget __P((struct vnode *vp, int lockflag)); 345 void vgone __P((struct vnode *vp)); 346 int vinvalbuf __P((struct vnode *vp, int save, struct ucred *cred, 347 struct proc *p, int slpflag, int slptimeo)); 348 void vprint __P((char *label, struct vnode *vp)); 349 int vn_bwrite __P((struct vop_bwrite_args *ap)); 350 int vn_close __P((struct vnode *vp, 351 int flags, struct ucred *cred, struct proc *p)); 352 int vn_closefile __P((struct file *fp, struct proc *p)); 353 int vn_ioctl __P((struct file *fp, u_long com, caddr_t data, 354 struct proc *p)); 355 int vn_open __P((struct nameidata *ndp, int fmode, int cmode)); 356 int vn_rdwr __P((enum uio_rw rw, struct vnode *vp, caddr_t base, 357 int len, off_t offset, enum uio_seg segflg, int ioflg, 358 struct ucred *cred, int *aresid, struct proc *p)); 359 int vn_read __P((struct file *fp, struct uio *uio, struct ucred *cred)); 360 int vn_select __P((struct file *fp, int which, struct proc *p)); 361 int vn_stat __P((struct vnode *vp, struct stat *sb, struct proc *p)); 362 int vn_write __P((struct file *fp, struct uio *uio, struct ucred *cred)); 363 int vop_revoke __P((struct vop_revoke_args *)); 364 struct vnode * 365 checkalias __P((struct vnode *vp, dev_t nvp_rdev, struct mount *mp)); 366 void vput __P((struct vnode *vp)); 367 void vref __P((struct vnode *vp)); 368 void vrele __P((struct vnode *vp)); 369 #endif /* KERNEL */ 370