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