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