| 1 | /* |
| 2 | * Copyright (c) 1989 The Regents of the University of California. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * This code is derived from software contributed to Berkeley by |
| 6 | * Rick Macklem at The University of Guelph. |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms are permitted |
| 9 | * provided that the above copyright notice and this paragraph are |
| 10 | * duplicated in all such forms and that any documentation, |
| 11 | * advertising materials, and other materials related to such |
| 12 | * distribution and use acknowledge that the software was developed |
| 13 | * by the University of California, Berkeley. The name of the |
| 14 | * University may not be used to endorse or promote products derived |
| 15 | * from this software without specific prior written permission. |
| 16 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR |
| 17 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
| 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 19 | * |
| 20 | * @(#)nfs_node.c 7.12 (Berkeley) %G% |
| 21 | */ |
| 22 | |
| 23 | #include "param.h" |
| 24 | #include "systm.h" |
| 25 | #include "user.h" |
| 26 | #include "proc.h" |
| 27 | #include "mount.h" |
| 28 | #include "vnode.h" |
| 29 | #include "../ufs/dir.h" |
| 30 | #include "namei.h" |
| 31 | #include "errno.h" |
| 32 | #include "nfsv2.h" |
| 33 | #include "nfs.h" |
| 34 | #include "nfsnode.h" |
| 35 | #include "nfsmount.h" |
| 36 | #include "kernel.h" |
| 37 | #include "malloc.h" |
| 38 | |
| 39 | /* The request list head */ |
| 40 | extern struct nfsreq nfsreqh; |
| 41 | |
| 42 | #define NFSNOHSZ 512 |
| 43 | #if ((NFSNOHSZ&(NFSNOHSZ-1)) == 0) |
| 44 | #define NFSNOHASH(fhsum) ((fhsum)&(NFSNOHSZ-1)) |
| 45 | #else |
| 46 | #define NFSNOHASH(fhsum) (((unsigned)(fhsum))%NFSNOHSZ) |
| 47 | #endif |
| 48 | |
| 49 | union nhead { |
| 50 | union nhead *nh_head[2]; |
| 51 | struct nfsnode *nh_chain[2]; |
| 52 | } nhead[NFSNOHSZ]; |
| 53 | |
| 54 | #define TRUE 1 |
| 55 | #define FALSE 0 |
| 56 | |
| 57 | /* |
| 58 | * Initialize hash links for nfsnodes |
| 59 | * and build nfsnode free list. |
| 60 | */ |
| 61 | nfs_nhinit() |
| 62 | { |
| 63 | register int i; |
| 64 | register union nhead *nh = nhead; |
| 65 | |
| 66 | #ifndef lint |
| 67 | if (VN_MAXPRIVATE < sizeof(struct nfsnode)) |
| 68 | panic("nfs_nhinit: too small"); |
| 69 | #endif /* not lint */ |
| 70 | for (i = NFSNOHSZ; --i >= 0; nh++) { |
| 71 | nh->nh_head[0] = nh; |
| 72 | nh->nh_head[1] = nh; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /* |
| 77 | * Compute an entry in the NFS hash table structure |
| 78 | */ |
| 79 | union nhead * |
| 80 | nfs_hash(fhp) |
| 81 | register nfsv2fh_t *fhp; |
| 82 | { |
| 83 | register u_char *fhpp; |
| 84 | register u_long fhsum; |
| 85 | int i; |
| 86 | |
| 87 | fhpp = &fhp->fh_bytes[0]; |
| 88 | fhsum = 0; |
| 89 | for (i = 0; i < NFSX_FH; i++) |
| 90 | fhsum += *fhpp++; |
| 91 | return (&nhead[NFSNOHASH(fhsum)]); |
| 92 | } |
| 93 | |
| 94 | /* |
| 95 | * Look up a vnode/nfsnode by file handle. |
| 96 | * Callers must check for mount points!! |
| 97 | * In all cases, a pointer to a |
| 98 | * nfsnode structure is returned. |
| 99 | */ |
| 100 | nfs_nget(mntp, fhp, npp) |
| 101 | struct mount *mntp; |
| 102 | register nfsv2fh_t *fhp; |
| 103 | struct nfsnode **npp; |
| 104 | { |
| 105 | register struct nfsnode *np; |
| 106 | register struct vnode *vp; |
| 107 | extern struct vnodeops nfsv2_vnodeops; |
| 108 | struct vnode *nvp; |
| 109 | union nhead *nh; |
| 110 | int error; |
| 111 | |
| 112 | nh = nfs_hash(fhp); |
| 113 | loop: |
| 114 | for (np = nh->nh_chain[0]; np != (struct nfsnode *)nh; np = np->n_forw) { |
| 115 | if (mntp != NFSTOV(np)->v_mount || |
| 116 | bcmp((caddr_t)fhp, (caddr_t)&np->n_fh, NFSX_FH)) |
| 117 | continue; |
| 118 | if ((np->n_flag & NLOCKED) != 0) { |
| 119 | np->n_flag |= NWANT; |
| 120 | sleep((caddr_t)np, PINOD); |
| 121 | goto loop; |
| 122 | } |
| 123 | vp = NFSTOV(np); |
| 124 | if (vget(vp)) |
| 125 | goto loop; |
| 126 | *npp = np; |
| 127 | return(0); |
| 128 | } |
| 129 | if (error = getnewvnode(VT_NFS, mntp, &nfsv2_vnodeops, &nvp)) { |
| 130 | *npp = 0; |
| 131 | return (error); |
| 132 | } |
| 133 | vp = nvp; |
| 134 | np = VTONFS(vp); |
| 135 | np->n_vnode = vp; |
| 136 | /* |
| 137 | * Insert the nfsnode in the hash queue for its new file handle |
| 138 | */ |
| 139 | np->n_flag = NLOCKED; |
| 140 | insque(np, nh); |
| 141 | bcopy((caddr_t)fhp, (caddr_t)&np->n_fh, NFSX_FH); |
| 142 | np->n_attrstamp = 0; |
| 143 | np->n_sillyrename = (struct sillyrename *)0; |
| 144 | np->n_size = 0; |
| 145 | np->n_mtime = 0; |
| 146 | *npp = np; |
| 147 | return (0); |
| 148 | } |
| 149 | |
| 150 | nfs_inactive(vp) |
| 151 | struct vnode *vp; |
| 152 | { |
| 153 | register struct nfsnode *np; |
| 154 | register struct nameidata *ndp; |
| 155 | register struct sillyrename *sp; |
| 156 | struct nfsnode *dnp; |
| 157 | |
| 158 | np = VTONFS(vp); |
| 159 | if (vp->v_count != 0) |
| 160 | printf("nfs_inactive: pushing active fileid %d fsid 0x%x\n", |
| 161 | np->n_vattr.va_fileid, np->n_vattr.va_fsid); |
| 162 | nfs_lock(vp); |
| 163 | sp = np->n_sillyrename; |
| 164 | np->n_sillyrename = (struct sillyrename *)0; |
| 165 | if (sp) { |
| 166 | /* |
| 167 | * Remove the silly file that was rename'd earlier |
| 168 | */ |
| 169 | ndp = &sp->s_namei; |
| 170 | if (!nfs_nget(vp->v_mount, &sp->s_fh, &dnp)) { |
| 171 | ndp->ni_dvp = NFSTOV(dnp); |
| 172 | nfs_removeit(ndp); |
| 173 | nfs_nput(ndp->ni_dvp); |
| 174 | } |
| 175 | crfree(ndp->ni_cred); |
| 176 | free((caddr_t)sp, M_TEMP); |
| 177 | } |
| 178 | nfs_unlock(vp); |
| 179 | np->n_flag &= NBUFFERED; |
| 180 | #ifdef notdef |
| 181 | /* |
| 182 | * Scan the request list for any requests left hanging about |
| 183 | */ |
| 184 | s = splnet(); |
| 185 | rep = nfsreqh.r_next; |
| 186 | while (rep && rep != &nfsreqh) { |
| 187 | if (rep->r_vp == vp) { |
| 188 | rep->r_prev->r_next = rep2 = rep->r_next; |
| 189 | rep->r_next->r_prev = rep->r_prev; |
| 190 | m_freem(rep->r_mreq); |
| 191 | if (rep->r_mrep != NULL) |
| 192 | m_freem(rep->r_mrep); |
| 193 | free((caddr_t)rep, M_NFSREQ); |
| 194 | rep = rep2; |
| 195 | } else |
| 196 | rep = rep->r_next; |
| 197 | } |
| 198 | splx(s); |
| 199 | #endif |
| 200 | return (0); |
| 201 | } |
| 202 | |
| 203 | /* |
| 204 | * Reclaim an nfsnode so that it can be used for other purposes. |
| 205 | */ |
| 206 | nfs_reclaim(vp) |
| 207 | register struct vnode *vp; |
| 208 | { |
| 209 | register struct nfsnode *np = VTONFS(vp); |
| 210 | |
| 211 | /* |
| 212 | * Flush out any associated bio buffers that might be lying about |
| 213 | */ |
| 214 | if (vp->v_type == VREG && (np->n_flag & NBUFFERED)) { |
| 215 | nfs_lock(np); |
| 216 | nfs_blkflush(vp, (daddr_t)0, np->n_size, TRUE); |
| 217 | nfs_unlock(np); |
| 218 | } |
| 219 | if (vp->v_count != 0) |
| 220 | printf("nfs_reclaim: pushing active fileid %d fsid 0x%x\n", |
| 221 | np->n_vattr.va_fileid, np->n_vattr.va_fsid); |
| 222 | /* |
| 223 | * Remove the nfsnode from its hash chain. |
| 224 | */ |
| 225 | remque(np); |
| 226 | np->n_forw = np; |
| 227 | np->n_back = np; |
| 228 | cache_purge(vp); |
| 229 | return (0); |
| 230 | } |
| 231 | |
| 232 | /* |
| 233 | * Lock an nfsnode |
| 234 | */ |
| 235 | nfs_lock(vp) |
| 236 | struct vnode *vp; |
| 237 | { |
| 238 | register struct nfsnode *np = VTONFS(vp); |
| 239 | |
| 240 | while (np->n_flag & NLOCKED) { |
| 241 | np->n_flag |= NWANT; |
| 242 | sleep((caddr_t)np, PINOD); |
| 243 | } |
| 244 | np->n_flag |= NLOCKED; |
| 245 | } |
| 246 | |
| 247 | /* |
| 248 | * Unlock an nfsnode |
| 249 | */ |
| 250 | nfs_unlock(vp) |
| 251 | struct vnode *vp; |
| 252 | { |
| 253 | register struct nfsnode *np = VTONFS(vp); |
| 254 | |
| 255 | np->n_flag &= ~NLOCKED; |
| 256 | if (np->n_flag & NWANT) { |
| 257 | np->n_flag &= ~NWANT; |
| 258 | wakeup((caddr_t)np); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | /* |
| 263 | * Unlock and vrele() |
| 264 | * since I can't decide if dirs. should be locked, I will check for |
| 265 | * the lock and be flexible |
| 266 | */ |
| 267 | nfs_nput(vp) |
| 268 | struct vnode *vp; |
| 269 | { |
| 270 | register struct nfsnode *np = VTONFS(vp); |
| 271 | |
| 272 | if (np->n_flag & NLOCKED) |
| 273 | nfs_unlock(vp); |
| 274 | vrele(vp); |
| 275 | } |
| 276 | |
| 277 | nfs_abortop(ndp) |
| 278 | register struct nameidata *ndp; |
| 279 | { |
| 280 | register struct nfsnode *np; |
| 281 | |
| 282 | if (ndp->ni_vp != NULL) { |
| 283 | np = VTONFS(ndp->ni_vp); |
| 284 | if (np->n_flag & NLOCKED) |
| 285 | nfs_unlock(ndp->ni_vp); |
| 286 | vrele(ndp->ni_vp); |
| 287 | } |
| 288 | if (ndp->ni_dvp != NULL) { |
| 289 | np = VTONFS(ndp->ni_dvp); |
| 290 | if (np->n_flag & NLOCKED) |
| 291 | nfs_unlock(ndp->ni_dvp); |
| 292 | vrele(ndp->ni_dvp); |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | /* |
| 297 | * This is silly, but if you use a macro and try and use it in a file |
| 298 | * that has mbuf.h included, m_data --> m_hdr.mh_data and this is not |
| 299 | * a good thing |
| 300 | */ |
| 301 | struct nfsmount *vfs_to_nfs(mp) |
| 302 | struct mount *mp; |
| 303 | { |
| 304 | return ((struct nfsmount *)mp->m_data); |
| 305 | } |