xref: /freebsd/sys/fs/smbfs/smbfs_node.c (revision a854ed98931b2e365eddd24cd2a1bb53a3f1828f)
1681a5bbeSBoris Popov /*
2681a5bbeSBoris Popov  * Copyright (c) 2000-2001 Boris Popov
3681a5bbeSBoris Popov  * All rights reserved.
4681a5bbeSBoris Popov  *
5681a5bbeSBoris Popov  * Redistribution and use in source and binary forms, with or without
6681a5bbeSBoris Popov  * modification, are permitted provided that the following conditions
7681a5bbeSBoris Popov  * are met:
8681a5bbeSBoris Popov  * 1. Redistributions of source code must retain the above copyright
9681a5bbeSBoris Popov  *    notice, this list of conditions and the following disclaimer.
10681a5bbeSBoris Popov  * 2. Redistributions in binary form must reproduce the above copyright
11681a5bbeSBoris Popov  *    notice, this list of conditions and the following disclaimer in the
12681a5bbeSBoris Popov  *    documentation and/or other materials provided with the distribution.
13681a5bbeSBoris Popov  * 3. All advertising materials mentioning features or use of this software
14681a5bbeSBoris Popov  *    must display the following acknowledgement:
15681a5bbeSBoris Popov  *    This product includes software developed by Boris Popov.
16681a5bbeSBoris Popov  * 4. Neither the name of the author nor the names of any co-contributors
17681a5bbeSBoris Popov  *    may be used to endorse or promote products derived from this software
18681a5bbeSBoris Popov  *    without specific prior written permission.
19681a5bbeSBoris Popov  *
20681a5bbeSBoris Popov  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21681a5bbeSBoris Popov  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22681a5bbeSBoris Popov  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23681a5bbeSBoris Popov  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24681a5bbeSBoris Popov  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25681a5bbeSBoris Popov  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26681a5bbeSBoris Popov  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27681a5bbeSBoris Popov  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28681a5bbeSBoris Popov  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29681a5bbeSBoris Popov  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30681a5bbeSBoris Popov  * SUCH DAMAGE.
31681a5bbeSBoris Popov  *
32681a5bbeSBoris Popov  * $FreeBSD$
33681a5bbeSBoris Popov  */
34681a5bbeSBoris Popov #include <sys/param.h>
35681a5bbeSBoris Popov #include <sys/systm.h>
36681a5bbeSBoris Popov #include <sys/kernel.h>
37fb919e4dSMark Murray #include <sys/lock.h>
38681a5bbeSBoris Popov #include <sys/malloc.h>
39fb919e4dSMark Murray #include <sys/mount.h>
40fb919e4dSMark Murray #include <sys/proc.h>
41681a5bbeSBoris Popov #include <sys/queue.h>
42fb919e4dSMark Murray #include <sys/sysctl.h>
43fb919e4dSMark Murray #include <sys/time.h>
44fb919e4dSMark Murray #include <sys/vnode.h>
45681a5bbeSBoris Popov 
46681a5bbeSBoris Popov #include <netsmb/smb.h>
47681a5bbeSBoris Popov #include <netsmb/smb_conn.h>
48681a5bbeSBoris Popov #include <netsmb/smb_subr.h>
49681a5bbeSBoris Popov 
50fb919e4dSMark Murray #include <vm/vm.h>
51fb919e4dSMark Murray #include <vm/vm_extern.h>
52fb919e4dSMark Murray /*#include <vm/vm_page.h>
53fb919e4dSMark Murray #include <vm/vm_object.h>*/
54fb919e4dSMark Murray 
55681a5bbeSBoris Popov #include <fs/smbfs/smbfs.h>
56681a5bbeSBoris Popov #include <fs/smbfs/smbfs_node.h>
57681a5bbeSBoris Popov #include <fs/smbfs/smbfs_subr.h>
58681a5bbeSBoris Popov 
59681a5bbeSBoris Popov #define	SMBFS_NOHASH(smp, hval)	(&(smp)->sm_hash[(hval) & (smp)->sm_hashlen])
60b1c996c4SBoris Popov #define	smbfs_hash_lock(smp, td)	lockmgr(&smp->sm_hashlock, LK_EXCLUSIVE, NULL, td)
61b1c996c4SBoris Popov #define	smbfs_hash_unlock(smp, td)	lockmgr(&smp->sm_hashlock, LK_RELEASE, NULL, td)
62681a5bbeSBoris Popov 
63681a5bbeSBoris Popov 
64681a5bbeSBoris Popov extern vop_t **smbfs_vnodeop_p;
65681a5bbeSBoris Popov 
66681a5bbeSBoris Popov MALLOC_DEFINE(M_SMBNODE, "SMBFS node", "SMBFS vnode private part");
67681a5bbeSBoris Popov static MALLOC_DEFINE(M_SMBNODENAME, "SMBFS nname", "SMBFS node name");
68681a5bbeSBoris Popov 
69681a5bbeSBoris Popov int smbfs_hashprint(struct mount *mp);
70681a5bbeSBoris Popov 
71681a5bbeSBoris Popov #if 0
72681a5bbeSBoris Popov #ifdef SYSCTL_DECL
73681a5bbeSBoris Popov SYSCTL_DECL(_vfs_smbfs);
74681a5bbeSBoris Popov #endif
75681a5bbeSBoris Popov SYSCTL_PROC(_vfs_smbfs, OID_AUTO, vnprint, CTLFLAG_WR|CTLTYPE_OPAQUE,
76681a5bbeSBoris Popov 	    NULL, 0, smbfs_hashprint, "S,vnlist", "vnode hash");
77681a5bbeSBoris Popov #endif
78681a5bbeSBoris Popov 
79681a5bbeSBoris Popov #define	FNV_32_PRIME ((u_int32_t) 0x01000193UL)
80681a5bbeSBoris Popov #define	FNV1_32_INIT ((u_int32_t) 33554467UL)
81681a5bbeSBoris Popov 
82681a5bbeSBoris Popov u_int32_t
83681a5bbeSBoris Popov smbfs_hash(const u_char *name, int nmlen)
84681a5bbeSBoris Popov {
85681a5bbeSBoris Popov 	u_int32_t v;
86681a5bbeSBoris Popov 
87681a5bbeSBoris Popov 	for (v = FNV1_32_INIT; nmlen; name++, nmlen--) {
88681a5bbeSBoris Popov 		v *= FNV_32_PRIME;
89681a5bbeSBoris Popov 		v ^= (u_int32_t)*name;
90681a5bbeSBoris Popov 	}
91681a5bbeSBoris Popov 	return v;
92681a5bbeSBoris Popov }
93681a5bbeSBoris Popov 
94681a5bbeSBoris Popov int
95681a5bbeSBoris Popov smbfs_hashprint(struct mount *mp)
96681a5bbeSBoris Popov {
97681a5bbeSBoris Popov 	struct smbmount *smp = VFSTOSMBFS(mp);
98681a5bbeSBoris Popov 	struct smbnode_hashhead *nhpp;
99681a5bbeSBoris Popov 	struct smbnode *np;
100681a5bbeSBoris Popov 	int i;
101681a5bbeSBoris Popov 
102681a5bbeSBoris Popov 	for(i = 0; i <= smp->sm_hashlen; i++) {
103681a5bbeSBoris Popov 		nhpp = &smp->sm_hash[i];
104681a5bbeSBoris Popov 		LIST_FOREACH(np, nhpp, n_hash)
105681a5bbeSBoris Popov 			vprint(NULL, SMBTOV(np));
106681a5bbeSBoris Popov 	}
107681a5bbeSBoris Popov 	return 0;
108681a5bbeSBoris Popov }
109681a5bbeSBoris Popov 
110681a5bbeSBoris Popov static char *
111681a5bbeSBoris Popov smbfs_name_alloc(const u_char *name, int nmlen)
112681a5bbeSBoris Popov {
113681a5bbeSBoris Popov 	u_char *cp;
114681a5bbeSBoris Popov 
115681a5bbeSBoris Popov 	nmlen++;
116681a5bbeSBoris Popov #ifdef SMBFS_NAME_DEBUG
117681a5bbeSBoris Popov 	cp = malloc(nmlen + 2 + sizeof(int), M_SMBNODENAME, M_WAITOK);
118681a5bbeSBoris Popov 	*(int*)cp = nmlen;
119681a5bbeSBoris Popov 	cp += sizeof(int);
120681a5bbeSBoris Popov 	cp[0] = 0xfc;
121681a5bbeSBoris Popov 	cp++;
122681a5bbeSBoris Popov 	bcopy(name, cp, nmlen - 1);
123681a5bbeSBoris Popov 	cp[nmlen] = 0xfe;
124681a5bbeSBoris Popov #else
125681a5bbeSBoris Popov 	cp = malloc(nmlen, M_SMBNODENAME, M_WAITOK);
126681a5bbeSBoris Popov 	bcopy(name, cp, nmlen - 1);
127681a5bbeSBoris Popov #endif
128681a5bbeSBoris Popov 	cp[nmlen - 1] = 0;
129681a5bbeSBoris Popov 	return cp;
130681a5bbeSBoris Popov }
131681a5bbeSBoris Popov 
132681a5bbeSBoris Popov static void
133681a5bbeSBoris Popov smbfs_name_free(u_char *name)
134681a5bbeSBoris Popov {
135681a5bbeSBoris Popov #ifdef SMBFS_NAME_DEBUG
136681a5bbeSBoris Popov 	int nmlen, slen;
137681a5bbeSBoris Popov 	u_char *cp;
138681a5bbeSBoris Popov 
139681a5bbeSBoris Popov 	cp = name;
140681a5bbeSBoris Popov 	cp--;
141681a5bbeSBoris Popov 	if (*cp != 0xfc) {
142681a5bbeSBoris Popov 		printf("First byte of name entry '%s' corrupted\n", name);
143681a5bbeSBoris Popov 		Debugger("ditto");
144681a5bbeSBoris Popov 	}
145681a5bbeSBoris Popov 	cp -= sizeof(int);
146681a5bbeSBoris Popov 	nmlen = *(int*)cp;
147681a5bbeSBoris Popov 	slen = strlen(name) + 1;
148681a5bbeSBoris Popov 	if (nmlen != slen) {
149681a5bbeSBoris Popov 		printf("Name length mismatch: was %d, now %d name '%s'\n",
150681a5bbeSBoris Popov 		    nmlen, slen, name);
151681a5bbeSBoris Popov 		Debugger("ditto");
152681a5bbeSBoris Popov 	}
153681a5bbeSBoris Popov 	if (name[nmlen] != 0xfe) {
154681a5bbeSBoris Popov 		printf("Last byte of name entry '%s' corrupted\n", name);
155681a5bbeSBoris Popov 		Debugger("ditto");
156681a5bbeSBoris Popov 	}
157681a5bbeSBoris Popov 	free(cp, M_SMBNODENAME);
158681a5bbeSBoris Popov #else
159681a5bbeSBoris Popov 	free(name, M_SMBNODENAME);
160681a5bbeSBoris Popov #endif
161681a5bbeSBoris Popov }
162681a5bbeSBoris Popov 
163681a5bbeSBoris Popov static int
164681a5bbeSBoris Popov smbfs_node_alloc(struct mount *mp, struct vnode *dvp,
165681a5bbeSBoris Popov 	const char *name, int nmlen, struct smbfattr *fap, struct vnode **vpp)
166681a5bbeSBoris Popov {
167b1c996c4SBoris Popov 	struct thread *td = curthread;	/* XXX */
168681a5bbeSBoris Popov 	struct smbmount *smp = VFSTOSMBFS(mp);
169681a5bbeSBoris Popov 	struct smbnode_hashhead *nhpp;
170681a5bbeSBoris Popov 	struct smbnode *np, *np2, *dnp;
171681a5bbeSBoris Popov 	struct vnode *vp;
172681a5bbeSBoris Popov 	u_long hashval;
173681a5bbeSBoris Popov 	int error;
174681a5bbeSBoris Popov 
175681a5bbeSBoris Popov 	*vpp = NULL;
176681a5bbeSBoris Popov 	if (smp->sm_root != NULL && dvp == NULL) {
177681a5bbeSBoris Popov 		SMBERROR("do not allocate root vnode twice!\n");
178681a5bbeSBoris Popov 		return EINVAL;
179681a5bbeSBoris Popov 	}
180681a5bbeSBoris Popov 	if (nmlen == 2 && bcmp(name, "..", 2) == 0) {
181681a5bbeSBoris Popov 		if (dvp == NULL)
182681a5bbeSBoris Popov 			return EINVAL;
183681a5bbeSBoris Popov 		vp = VTOSMB(dvp)->n_parent->n_vnode;
184b1c996c4SBoris Popov 		error = vget(vp, LK_EXCLUSIVE, td);
185681a5bbeSBoris Popov 		if (error == 0)
186681a5bbeSBoris Popov 			*vpp = vp;
187681a5bbeSBoris Popov 		return error;
188681a5bbeSBoris Popov 	} else if (nmlen == 1 && name[0] == '.') {
189681a5bbeSBoris Popov 		SMBERROR("do not call me with dot!\n");
190681a5bbeSBoris Popov 		return EINVAL;
191681a5bbeSBoris Popov 	}
192681a5bbeSBoris Popov 	dnp = dvp ? VTOSMB(dvp) : NULL;
193681a5bbeSBoris Popov 	if (dnp == NULL && dvp != NULL) {
194681a5bbeSBoris Popov 		vprint("smbfs_node_alloc: dead parent vnode", dvp);
195681a5bbeSBoris Popov 		return EINVAL;
196681a5bbeSBoris Popov 	}
197681a5bbeSBoris Popov 	hashval = smbfs_hash(name, nmlen);
198681a5bbeSBoris Popov retry:
199b1c996c4SBoris Popov 	smbfs_hash_lock(smp, td);
200681a5bbeSBoris Popov loop:
201681a5bbeSBoris Popov 	nhpp = SMBFS_NOHASH(smp, hashval);
202681a5bbeSBoris Popov 	LIST_FOREACH(np, nhpp, n_hash) {
203681a5bbeSBoris Popov 		vp = SMBTOV(np);
204681a5bbeSBoris Popov 		if (np->n_parent != dnp ||
205681a5bbeSBoris Popov 		    np->n_nmlen != nmlen || bcmp(name, np->n_name, nmlen) != 0)
206681a5bbeSBoris Popov 			continue;
207681a5bbeSBoris Popov 		VI_LOCK(vp);
208b1c996c4SBoris Popov 		smbfs_hash_unlock(smp, td);
209b1c996c4SBoris Popov 		if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td) != 0)
210681a5bbeSBoris Popov 			goto retry;
211681a5bbeSBoris Popov 		*vpp = vp;
212681a5bbeSBoris Popov 		return 0;
213681a5bbeSBoris Popov 	}
214b1c996c4SBoris Popov 	smbfs_hash_unlock(smp, td);
215681a5bbeSBoris Popov 	/*
216681a5bbeSBoris Popov 	 * If we don't have node attributes, then it is an explicit lookup
217681a5bbeSBoris Popov 	 * for an existing vnode.
218681a5bbeSBoris Popov 	 */
219681a5bbeSBoris Popov 	if (fap == NULL)
220681a5bbeSBoris Popov 		return ENOENT;
221681a5bbeSBoris Popov 
222681a5bbeSBoris Popov 	MALLOC(np, struct smbnode *, sizeof *np, M_SMBNODE, M_WAITOK);
223681a5bbeSBoris Popov 	error = getnewvnode(VT_SMBFS, mp, smbfs_vnodeop_p, &vp);
224681a5bbeSBoris Popov 	if (error) {
225681a5bbeSBoris Popov 		FREE(np, M_SMBNODE);
226681a5bbeSBoris Popov 		return error;
227681a5bbeSBoris Popov 	}
228681a5bbeSBoris Popov 	vp->v_type = fap->fa_attr & SMB_FA_DIR ? VDIR : VREG;
229681a5bbeSBoris Popov 	bzero(np, sizeof(*np));
230681a5bbeSBoris Popov 	vp->v_data = np;
231681a5bbeSBoris Popov 	np->n_vnode = vp;
232681a5bbeSBoris Popov 	np->n_mount = VFSTOSMBFS(mp);
233681a5bbeSBoris Popov 	np->n_nmlen = nmlen;
234681a5bbeSBoris Popov 	np->n_name = smbfs_name_alloc(name, nmlen);
235681a5bbeSBoris Popov 	np->n_ino = fap->fa_ino;
236681a5bbeSBoris Popov 
237681a5bbeSBoris Popov 	if (dvp) {
238681a5bbeSBoris Popov 		np->n_parent = dnp;
239681a5bbeSBoris Popov 		if (/*vp->v_type == VDIR &&*/ (dvp->v_flag & VROOT) == 0) {
240681a5bbeSBoris Popov 			vref(dvp);
241681a5bbeSBoris Popov 			np->n_flag |= NREFPARENT;
242681a5bbeSBoris Popov 		}
243681a5bbeSBoris Popov 	} else if (vp->v_type == VREG)
244681a5bbeSBoris Popov 		SMBERROR("new vnode '%s' born without parent ?\n", np->n_name);
245681a5bbeSBoris Popov 
24623b59018SMatthew Dillon 	lockinit(&vp->v_lock, PINOD, "smbnode", VLKTIMEOUT, LK_CANRECURSE);
247b1c996c4SBoris Popov 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
248681a5bbeSBoris Popov 
249b1c996c4SBoris Popov 	smbfs_hash_lock(smp, td);
250681a5bbeSBoris Popov 	LIST_FOREACH(np2, nhpp, n_hash) {
251681a5bbeSBoris Popov 		if (np2->n_parent != dnp ||
252681a5bbeSBoris Popov 		    np2->n_nmlen != nmlen || bcmp(name, np2->n_name, nmlen) != 0)
253681a5bbeSBoris Popov 			continue;
254681a5bbeSBoris Popov 		vput(vp);
255681a5bbeSBoris Popov /*		smb_name_free(np->n_name);
256681a5bbeSBoris Popov 		FREE(np, M_SMBNODE);*/
257681a5bbeSBoris Popov 		goto loop;
258681a5bbeSBoris Popov 	}
259681a5bbeSBoris Popov 	LIST_INSERT_HEAD(nhpp, np, n_hash);
260b1c996c4SBoris Popov 	smbfs_hash_unlock(smp, td);
261681a5bbeSBoris Popov 	*vpp = vp;
262681a5bbeSBoris Popov 	return 0;
263681a5bbeSBoris Popov }
264681a5bbeSBoris Popov 
265681a5bbeSBoris Popov int
266681a5bbeSBoris Popov smbfs_nget(struct mount *mp, struct vnode *dvp, const char *name, int nmlen,
267681a5bbeSBoris Popov 	struct smbfattr *fap, struct vnode **vpp)
268681a5bbeSBoris Popov {
269681a5bbeSBoris Popov 	struct smbnode *np;
270681a5bbeSBoris Popov 	struct vnode *vp;
271681a5bbeSBoris Popov 	int error;
272681a5bbeSBoris Popov 
273681a5bbeSBoris Popov 	*vpp = NULL;
274681a5bbeSBoris Popov 	error = smbfs_node_alloc(mp, dvp, name, nmlen, fap, &vp);
275681a5bbeSBoris Popov 	if (error)
276681a5bbeSBoris Popov 		return error;
277681a5bbeSBoris Popov 	np = VTOSMB(vp);
278681a5bbeSBoris Popov 	if (fap)
279681a5bbeSBoris Popov 		smbfs_attr_cacheenter(vp, fap);
280681a5bbeSBoris Popov 	*vpp = vp;
281681a5bbeSBoris Popov 	return 0;
282681a5bbeSBoris Popov }
283681a5bbeSBoris Popov 
284681a5bbeSBoris Popov /*
285681a5bbeSBoris Popov  * Free smbnode, and give vnode back to system
286681a5bbeSBoris Popov  */
287681a5bbeSBoris Popov int
288681a5bbeSBoris Popov smbfs_reclaim(ap)
289681a5bbeSBoris Popov         struct vop_reclaim_args /* {
290681a5bbeSBoris Popov 		struct vnode *a_vp;
291b1c996c4SBoris Popov 		struct thread *a_p;
292681a5bbeSBoris Popov         } */ *ap;
293681a5bbeSBoris Popov {
294681a5bbeSBoris Popov 	struct vnode *vp = ap->a_vp;
295b1c996c4SBoris Popov 	struct thread *td = ap->a_td;
296681a5bbeSBoris Popov 	struct vnode *dvp;
297681a5bbeSBoris Popov 	struct smbnode *np = VTOSMB(vp);
298681a5bbeSBoris Popov 	struct smbmount *smp = VTOSMBFS(vp);
299681a5bbeSBoris Popov 
300681a5bbeSBoris Popov 	SMBVDEBUG("%s,%d\n", np->n_name, vp->v_usecount);
301681a5bbeSBoris Popov 
302b1c996c4SBoris Popov 	smbfs_hash_lock(smp, td);
303681a5bbeSBoris Popov 
304681a5bbeSBoris Popov 	dvp = (np->n_parent && (np->n_flag & NREFPARENT)) ?
305681a5bbeSBoris Popov 	    np->n_parent->n_vnode : NULL;
306681a5bbeSBoris Popov 
307681a5bbeSBoris Popov 	if (np->n_hash.le_prev)
308681a5bbeSBoris Popov 		LIST_REMOVE(np, n_hash);
309681a5bbeSBoris Popov 	cache_purge(vp);
310681a5bbeSBoris Popov 	if (smp->sm_root == np) {
311681a5bbeSBoris Popov 		SMBVDEBUG("root vnode\n");
312681a5bbeSBoris Popov 		smp->sm_root = NULL;
313681a5bbeSBoris Popov 	}
314681a5bbeSBoris Popov 	vp->v_data = NULL;
315b1c996c4SBoris Popov 	smbfs_hash_unlock(smp, td);
316681a5bbeSBoris Popov 	if (np->n_name)
317681a5bbeSBoris Popov 		smbfs_name_free(np->n_name);
318681a5bbeSBoris Popov 	FREE(np, M_SMBNODE);
319681a5bbeSBoris Popov 	if (dvp) {
320681a5bbeSBoris Popov 		VI_LOCK(dvp);
321681a5bbeSBoris Popov 		if (dvp->v_usecount >= 1) {
322681a5bbeSBoris Popov 			VI_UNLOCK(dvp);
323681a5bbeSBoris Popov 			vrele(dvp);
324681a5bbeSBoris Popov 		} else {
325681a5bbeSBoris Popov 			VI_UNLOCK(dvp);
326681a5bbeSBoris Popov 			SMBERROR("BUG: negative use count for parent!\n");
327681a5bbeSBoris Popov 		}
328681a5bbeSBoris Popov 	}
329681a5bbeSBoris Popov 	return 0;
330681a5bbeSBoris Popov }
331681a5bbeSBoris Popov 
332681a5bbeSBoris Popov int
333681a5bbeSBoris Popov smbfs_inactive(ap)
334681a5bbeSBoris Popov 	struct vop_inactive_args /* {
335681a5bbeSBoris Popov 		struct vnode *a_vp;
336b1c996c4SBoris Popov 		struct thread *a_td;
337681a5bbeSBoris Popov 	} */ *ap;
338681a5bbeSBoris Popov {
339b1c996c4SBoris Popov 	struct thread *td = ap->a_td;
340a854ed98SJohn Baldwin 	struct ucred *cred = td->td_ucred;
341681a5bbeSBoris Popov 	struct vnode *vp = ap->a_vp;
342681a5bbeSBoris Popov 	struct smbnode *np = VTOSMB(vp);
343681a5bbeSBoris Popov 	struct smb_cred scred;
344681a5bbeSBoris Popov 	int error;
345681a5bbeSBoris Popov 
346681a5bbeSBoris Popov 	SMBVDEBUG("%s: %d\n", VTOSMB(vp)->n_name, vp->v_usecount);
347681a5bbeSBoris Popov 	if (np->n_opencount) {
348b1c996c4SBoris Popov 		error = smbfs_vinvalbuf(vp, V_SAVE, cred, td, 1);
349b1c996c4SBoris Popov 		smb_makescred(&scred, td, cred);
350681a5bbeSBoris Popov 		error = smbfs_smb_close(np->n_mount->sm_share, np->n_fid,
351681a5bbeSBoris Popov 		   &np->n_mtime, &scred);
352681a5bbeSBoris Popov 		np->n_opencount = 0;
353681a5bbeSBoris Popov 	}
354b1c996c4SBoris Popov 	VOP_UNLOCK(vp, 0, td);
355681a5bbeSBoris Popov 	return (0);
356681a5bbeSBoris Popov }
357681a5bbeSBoris Popov /*
358681a5bbeSBoris Popov  * routines to maintain vnode attributes cache
359681a5bbeSBoris Popov  * smbfs_attr_cacheenter: unpack np.i to vattr structure
360681a5bbeSBoris Popov  */
361681a5bbeSBoris Popov void
362681a5bbeSBoris Popov smbfs_attr_cacheenter(struct vnode *vp, struct smbfattr *fap)
363681a5bbeSBoris Popov {
364681a5bbeSBoris Popov 	struct smbnode *np = VTOSMB(vp);
365681a5bbeSBoris Popov 
366681a5bbeSBoris Popov 	if (vp->v_type == VREG) {
367681a5bbeSBoris Popov 		if (np->n_size != fap->fa_size) {
368681a5bbeSBoris Popov 			np->n_size = fap->fa_size;
369681a5bbeSBoris Popov 			vnode_pager_setsize(vp, np->n_size);
370681a5bbeSBoris Popov 		}
371681a5bbeSBoris Popov 	} else if (vp->v_type == VDIR) {
372681a5bbeSBoris Popov 		np->n_size = 16384; 		/* should be a better way ... */
373681a5bbeSBoris Popov 	} else
374681a5bbeSBoris Popov 		return;
375681a5bbeSBoris Popov 	np->n_mtime = fap->fa_mtime;
376681a5bbeSBoris Popov 	np->n_dosattr = fap->fa_attr;
377681a5bbeSBoris Popov 	np->n_attrage = time_second;
378681a5bbeSBoris Popov 	return;
379681a5bbeSBoris Popov }
380681a5bbeSBoris Popov 
381681a5bbeSBoris Popov int
382681a5bbeSBoris Popov smbfs_attr_cachelookup(struct vnode *vp, struct vattr *va)
383681a5bbeSBoris Popov {
384681a5bbeSBoris Popov 	struct smbnode *np = VTOSMB(vp);
385681a5bbeSBoris Popov 	struct smbmount *smp = VTOSMBFS(vp);
386681a5bbeSBoris Popov 	int diff;
387681a5bbeSBoris Popov 
388681a5bbeSBoris Popov 	diff = time_second - np->n_attrage;
389681a5bbeSBoris Popov 	if (diff > 2)	/* XXX should be configurable */
390681a5bbeSBoris Popov 		return ENOENT;
391681a5bbeSBoris Popov 	va->va_type = vp->v_type;		/* vnode type (for create) */
392681a5bbeSBoris Popov 	if (vp->v_type == VREG) {
393681a5bbeSBoris Popov 		va->va_mode = smp->sm_args.file_mode;	/* files access mode and type */
394681a5bbeSBoris Popov 	} else if (vp->v_type == VDIR) {
395681a5bbeSBoris Popov 		va->va_mode = smp->sm_args.dir_mode;	/* files access mode and type */
396681a5bbeSBoris Popov 	} else
397681a5bbeSBoris Popov 		return EINVAL;
398681a5bbeSBoris Popov 	va->va_size = np->n_size;
399681a5bbeSBoris Popov 	va->va_nlink = 1;		/* number of references to file */
400681a5bbeSBoris Popov 	va->va_uid = smp->sm_args.uid;	/* owner user id */
401681a5bbeSBoris Popov 	va->va_gid = smp->sm_args.gid;	/* owner group id */
402681a5bbeSBoris Popov 	va->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
403681a5bbeSBoris Popov 	va->va_fileid = np->n_ino;	/* file id */
404681a5bbeSBoris Popov 	if (va->va_fileid == 0)
405681a5bbeSBoris Popov 		va->va_fileid = 2;
406681a5bbeSBoris Popov 	va->va_blocksize = SSTOVC(smp->sm_share)->vc_txmax;
407681a5bbeSBoris Popov 	va->va_mtime = np->n_mtime;
408681a5bbeSBoris Popov 	va->va_atime = va->va_ctime = va->va_mtime;	/* time file changed */
409681a5bbeSBoris Popov 	va->va_gen = VNOVAL;		/* generation number of file */
410681a5bbeSBoris Popov 	va->va_flags = 0;		/* flags defined for file */
411681a5bbeSBoris Popov 	va->va_rdev = VNOVAL;		/* device the special file represents */
412681a5bbeSBoris Popov 	va->va_bytes = va->va_size;	/* bytes of disk space held by file */
413681a5bbeSBoris Popov 	va->va_filerev = 0;		/* file modification number */
414681a5bbeSBoris Popov 	va->va_vaflags = 0;		/* operations flags */
415681a5bbeSBoris Popov 	return 0;
416681a5bbeSBoris Popov }
417