xref: /freebsd/sys/fs/smbfs/smbfs_node.c (revision d745c852becf3792a2185003947324721209195a)
1d167cf6fSWarner Losh /*-
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  *
14681a5bbeSBoris Popov  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15681a5bbeSBoris Popov  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16681a5bbeSBoris Popov  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17681a5bbeSBoris Popov  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18681a5bbeSBoris Popov  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19681a5bbeSBoris Popov  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20681a5bbeSBoris Popov  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21681a5bbeSBoris Popov  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22681a5bbeSBoris Popov  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23681a5bbeSBoris Popov  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24681a5bbeSBoris Popov  * SUCH DAMAGE.
25681a5bbeSBoris Popov  *
26681a5bbeSBoris Popov  * $FreeBSD$
27681a5bbeSBoris Popov  */
28681a5bbeSBoris Popov #include <sys/param.h>
29681a5bbeSBoris Popov #include <sys/systm.h>
30681a5bbeSBoris Popov #include <sys/kernel.h>
31fb919e4dSMark Murray #include <sys/lock.h>
32681a5bbeSBoris Popov #include <sys/malloc.h>
33fb919e4dSMark Murray #include <sys/mount.h>
34fb919e4dSMark Murray #include <sys/proc.h>
35681a5bbeSBoris Popov #include <sys/queue.h>
36fb8e9eadSBoris Popov #include <sys/stat.h>
377947229fSRobert Watson #include <sys/sx.h>
38fb919e4dSMark Murray #include <sys/sysctl.h>
39fb919e4dSMark Murray #include <sys/time.h>
40fb919e4dSMark Murray #include <sys/vnode.h>
41681a5bbeSBoris Popov 
42681a5bbeSBoris Popov #include <netsmb/smb.h>
43681a5bbeSBoris Popov #include <netsmb/smb_conn.h>
44681a5bbeSBoris Popov #include <netsmb/smb_subr.h>
45681a5bbeSBoris Popov 
46fb919e4dSMark Murray #include <vm/vm.h>
47fb919e4dSMark Murray #include <vm/vm_extern.h>
48fb919e4dSMark Murray /*#include <vm/vm_page.h>
49fb919e4dSMark Murray #include <vm/vm_object.h>*/
50fb919e4dSMark Murray 
51681a5bbeSBoris Popov #include <fs/smbfs/smbfs.h>
52681a5bbeSBoris Popov #include <fs/smbfs/smbfs_node.h>
53681a5bbeSBoris Popov #include <fs/smbfs/smbfs_subr.h>
54681a5bbeSBoris Popov 
55681a5bbeSBoris Popov #define	SMBFS_NOHASH(smp, hval)	(&(smp)->sm_hash[(hval) & (smp)->sm_hashlen])
56e54fdca2SRobert Watson #define	smbfs_hash_lock(smp)	sx_xlock(&smp->sm_hashlock)
57e54fdca2SRobert Watson #define	smbfs_hash_unlock(smp)	sx_xunlock(&smp->sm_hashlock)
58681a5bbeSBoris Popov 
59aec0fb7bSPoul-Henning Kamp extern struct vop_vector smbfs_vnodeops;	/* XXX -> .h file */
60681a5bbeSBoris Popov 
61*d745c852SEd Schouten static MALLOC_DEFINE(M_SMBNODE, "smbufs_node", "SMBFS vnode private part");
625bb84bc8SRobert Watson static MALLOC_DEFINE(M_SMBNODENAME, "smbufs_nname", "SMBFS node name");
63681a5bbeSBoris Popov 
64681a5bbeSBoris Popov int smbfs_hashprint(struct mount *mp);
65681a5bbeSBoris Popov 
66681a5bbeSBoris Popov #if 0
67681a5bbeSBoris Popov #ifdef SYSCTL_DECL
68681a5bbeSBoris Popov SYSCTL_DECL(_vfs_smbfs);
69681a5bbeSBoris Popov #endif
70681a5bbeSBoris Popov SYSCTL_PROC(_vfs_smbfs, OID_AUTO, vnprint, CTLFLAG_WR|CTLTYPE_OPAQUE,
71681a5bbeSBoris Popov 	    NULL, 0, smbfs_hashprint, "S,vnlist", "vnode hash");
72681a5bbeSBoris Popov #endif
73681a5bbeSBoris Popov 
74681a5bbeSBoris Popov #define	FNV_32_PRIME ((u_int32_t) 0x01000193UL)
75681a5bbeSBoris Popov #define	FNV1_32_INIT ((u_int32_t) 33554467UL)
76681a5bbeSBoris Popov 
77681a5bbeSBoris Popov u_int32_t
78681a5bbeSBoris Popov smbfs_hash(const u_char *name, int nmlen)
79681a5bbeSBoris Popov {
80681a5bbeSBoris Popov 	u_int32_t v;
81681a5bbeSBoris Popov 
82681a5bbeSBoris Popov 	for (v = FNV1_32_INIT; nmlen; name++, nmlen--) {
83681a5bbeSBoris Popov 		v *= FNV_32_PRIME;
84681a5bbeSBoris Popov 		v ^= (u_int32_t)*name;
85681a5bbeSBoris Popov 	}
86681a5bbeSBoris Popov 	return v;
87681a5bbeSBoris Popov }
88681a5bbeSBoris Popov 
89681a5bbeSBoris Popov int
90681a5bbeSBoris Popov smbfs_hashprint(struct mount *mp)
91681a5bbeSBoris Popov {
92681a5bbeSBoris Popov 	struct smbmount *smp = VFSTOSMBFS(mp);
93681a5bbeSBoris Popov 	struct smbnode_hashhead *nhpp;
94681a5bbeSBoris Popov 	struct smbnode *np;
95681a5bbeSBoris Popov 	int i;
96681a5bbeSBoris Popov 
97681a5bbeSBoris Popov 	for(i = 0; i <= smp->sm_hashlen; i++) {
98681a5bbeSBoris Popov 		nhpp = &smp->sm_hash[i];
99681a5bbeSBoris Popov 		LIST_FOREACH(np, nhpp, n_hash)
100271c679cSPoul-Henning Kamp 			vprint("", SMBTOV(np));
101681a5bbeSBoris Popov 	}
102681a5bbeSBoris Popov 	return 0;
103681a5bbeSBoris Popov }
104681a5bbeSBoris Popov 
105681a5bbeSBoris Popov static char *
106681a5bbeSBoris Popov smbfs_name_alloc(const u_char *name, int nmlen)
107681a5bbeSBoris Popov {
108681a5bbeSBoris Popov 	u_char *cp;
109681a5bbeSBoris Popov 
110681a5bbeSBoris Popov 	nmlen++;
111681a5bbeSBoris Popov #ifdef SMBFS_NAME_DEBUG
112a163d034SWarner Losh 	cp = malloc(nmlen + 2 + sizeof(int), M_SMBNODENAME, M_WAITOK);
113681a5bbeSBoris Popov 	*(int*)cp = nmlen;
114681a5bbeSBoris Popov 	cp += sizeof(int);
115681a5bbeSBoris Popov 	cp[0] = 0xfc;
116681a5bbeSBoris Popov 	cp++;
117681a5bbeSBoris Popov 	bcopy(name, cp, nmlen - 1);
118681a5bbeSBoris Popov 	cp[nmlen] = 0xfe;
119681a5bbeSBoris Popov #else
120a163d034SWarner Losh 	cp = malloc(nmlen, M_SMBNODENAME, M_WAITOK);
121681a5bbeSBoris Popov 	bcopy(name, cp, nmlen - 1);
122681a5bbeSBoris Popov #endif
123681a5bbeSBoris Popov 	cp[nmlen - 1] = 0;
124681a5bbeSBoris Popov 	return cp;
125681a5bbeSBoris Popov }
126681a5bbeSBoris Popov 
127681a5bbeSBoris Popov static void
128681a5bbeSBoris Popov smbfs_name_free(u_char *name)
129681a5bbeSBoris Popov {
130681a5bbeSBoris Popov #ifdef SMBFS_NAME_DEBUG
131681a5bbeSBoris Popov 	int nmlen, slen;
132681a5bbeSBoris Popov 	u_char *cp;
133681a5bbeSBoris Popov 
134681a5bbeSBoris Popov 	cp = name;
135681a5bbeSBoris Popov 	cp--;
13686fc5557SRobert Watson 	if (*cp != 0xfc)
13786fc5557SRobert Watson 		panic("First byte of name entry '%s' corrupted", name);
138681a5bbeSBoris Popov 	cp -= sizeof(int);
139681a5bbeSBoris Popov 	nmlen = *(int*)cp;
140681a5bbeSBoris Popov 	slen = strlen(name) + 1;
14186fc5557SRobert Watson 	if (nmlen != slen)
14286fc5557SRobert Watson 		panic("Name length mismatch: was %d, now %d name '%s'",
143681a5bbeSBoris Popov 		    nmlen, slen, name);
14486fc5557SRobert Watson 	if (name[nmlen] != 0xfe)
14586fc5557SRobert Watson 		panic("Last byte of name entry '%s' corrupted\n", name);
146681a5bbeSBoris Popov 	free(cp, M_SMBNODENAME);
147681a5bbeSBoris Popov #else
148681a5bbeSBoris Popov 	free(name, M_SMBNODENAME);
149681a5bbeSBoris Popov #endif
150681a5bbeSBoris Popov }
151681a5bbeSBoris Popov 
152681a5bbeSBoris Popov static int
153681a5bbeSBoris Popov smbfs_node_alloc(struct mount *mp, struct vnode *dvp,
154681a5bbeSBoris Popov 	const char *name, int nmlen, struct smbfattr *fap, struct vnode **vpp)
155681a5bbeSBoris Popov {
156b4484bf0STim J. Robbins 	struct vattr vattr;
157b1c996c4SBoris Popov 	struct thread *td = curthread;	/* XXX */
158681a5bbeSBoris Popov 	struct smbmount *smp = VFSTOSMBFS(mp);
159681a5bbeSBoris Popov 	struct smbnode_hashhead *nhpp;
160681a5bbeSBoris Popov 	struct smbnode *np, *np2, *dnp;
161681a5bbeSBoris Popov 	struct vnode *vp;
162681a5bbeSBoris Popov 	u_long hashval;
163681a5bbeSBoris Popov 	int error;
164681a5bbeSBoris Popov 
165681a5bbeSBoris Popov 	*vpp = NULL;
166681a5bbeSBoris Popov 	if (smp->sm_root != NULL && dvp == NULL) {
167681a5bbeSBoris Popov 		SMBERROR("do not allocate root vnode twice!\n");
168681a5bbeSBoris Popov 		return EINVAL;
169681a5bbeSBoris Popov 	}
170681a5bbeSBoris Popov 	if (nmlen == 2 && bcmp(name, "..", 2) == 0) {
171681a5bbeSBoris Popov 		if (dvp == NULL)
172681a5bbeSBoris Popov 			return EINVAL;
17311de0c59STim J. Robbins 		vp = VTOSMB(VTOSMB(dvp)->n_parent)->n_vnode;
174b1c996c4SBoris Popov 		error = vget(vp, LK_EXCLUSIVE, td);
175681a5bbeSBoris Popov 		if (error == 0)
176681a5bbeSBoris Popov 			*vpp = vp;
177681a5bbeSBoris Popov 		return error;
178681a5bbeSBoris Popov 	} else if (nmlen == 1 && name[0] == '.') {
179681a5bbeSBoris Popov 		SMBERROR("do not call me with dot!\n");
180681a5bbeSBoris Popov 		return EINVAL;
181681a5bbeSBoris Popov 	}
182681a5bbeSBoris Popov 	dnp = dvp ? VTOSMB(dvp) : NULL;
183681a5bbeSBoris Popov 	if (dnp == NULL && dvp != NULL) {
184681a5bbeSBoris Popov 		vprint("smbfs_node_alloc: dead parent vnode", dvp);
185681a5bbeSBoris Popov 		return EINVAL;
186681a5bbeSBoris Popov 	}
187681a5bbeSBoris Popov 	hashval = smbfs_hash(name, nmlen);
188681a5bbeSBoris Popov retry:
189e54fdca2SRobert Watson 	smbfs_hash_lock(smp);
190681a5bbeSBoris Popov loop:
191681a5bbeSBoris Popov 	nhpp = SMBFS_NOHASH(smp, hashval);
192681a5bbeSBoris Popov 	LIST_FOREACH(np, nhpp, n_hash) {
193681a5bbeSBoris Popov 		vp = SMBTOV(np);
19411de0c59STim J. Robbins 		if (np->n_parent != dvp ||
195681a5bbeSBoris Popov 		    np->n_nmlen != nmlen || bcmp(name, np->n_name, nmlen) != 0)
196681a5bbeSBoris Popov 			continue;
197681a5bbeSBoris Popov 		VI_LOCK(vp);
198e54fdca2SRobert Watson 		smbfs_hash_unlock(smp);
199b1c996c4SBoris Popov 		if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td) != 0)
200681a5bbeSBoris Popov 			goto retry;
201b4484bf0STim J. Robbins 		/* Force cached attributes to be refreshed if stale. */
2020359a12eSAttilio Rao 		(void)VOP_GETATTR(vp, &vattr, td->td_ucred);
203b4484bf0STim J. Robbins 		/*
204b4484bf0STim J. Robbins 		 * If the file type on the server is inconsistent with
205b4484bf0STim J. Robbins 		 * what it was when we created the vnode, kill the
206b4484bf0STim J. Robbins 		 * bogus vnode now and fall through to the code below
207b4484bf0STim J. Robbins 		 * to create a new one with the right type.
208b4484bf0STim J. Robbins 		 */
209b4484bf0STim J. Robbins 		if ((vp->v_type == VDIR && (np->n_dosattr & SMB_FA_DIR) == 0) ||
210b4484bf0STim J. Robbins 		    (vp->v_type == VREG && (np->n_dosattr & SMB_FA_DIR) != 0)) {
211b4484bf0STim J. Robbins 			vgone(vp);
2128da00465SJeff Roberson 			vput(vp);
213b4484bf0STim J. Robbins 			break;
214b4484bf0STim J. Robbins 		}
215681a5bbeSBoris Popov 		*vpp = vp;
216681a5bbeSBoris Popov 		return 0;
217681a5bbeSBoris Popov 	}
218e54fdca2SRobert Watson 	smbfs_hash_unlock(smp);
219681a5bbeSBoris Popov 	/*
220681a5bbeSBoris Popov 	 * If we don't have node attributes, then it is an explicit lookup
221681a5bbeSBoris Popov 	 * for an existing vnode.
222681a5bbeSBoris Popov 	 */
223681a5bbeSBoris Popov 	if (fap == NULL)
224681a5bbeSBoris Popov 		return ENOENT;
225681a5bbeSBoris Popov 
2261ede983cSDag-Erling Smørgrav 	np = malloc(sizeof *np, M_SMBNODE, M_WAITOK);
227aec0fb7bSPoul-Henning Kamp 	error = getnewvnode("smbfs", mp, &smbfs_vnodeops, &vp);
228681a5bbeSBoris Popov 	if (error) {
2291ede983cSDag-Erling Smørgrav 		free(np, M_SMBNODE);
230681a5bbeSBoris Popov 		return error;
231681a5bbeSBoris Popov 	}
23261b9d89fSTor Egge 	error = insmntque(vp, mp);	/* XXX: Too early for mpsafe fs */
23361b9d89fSTor Egge 	if (error != 0) {
2341ede983cSDag-Erling Smørgrav 		free(np, M_SMBNODE);
23561b9d89fSTor Egge 		return (error);
23661b9d89fSTor Egge 	}
237681a5bbeSBoris Popov 	vp->v_type = fap->fa_attr & SMB_FA_DIR ? VDIR : VREG;
238681a5bbeSBoris Popov 	bzero(np, sizeof(*np));
239681a5bbeSBoris Popov 	vp->v_data = np;
240681a5bbeSBoris Popov 	np->n_vnode = vp;
241681a5bbeSBoris Popov 	np->n_mount = VFSTOSMBFS(mp);
242681a5bbeSBoris Popov 	np->n_nmlen = nmlen;
243681a5bbeSBoris Popov 	np->n_name = smbfs_name_alloc(name, nmlen);
244681a5bbeSBoris Popov 	np->n_ino = fap->fa_ino;
245681a5bbeSBoris Popov 
246681a5bbeSBoris Popov 	if (dvp) {
247e6e370a7SJeff Roberson 		ASSERT_VOP_LOCKED(dvp, "smbfs_node_alloc");
24811de0c59STim J. Robbins 		np->n_parent = dvp;
249e6e370a7SJeff Roberson 		if (/*vp->v_type == VDIR &&*/ (dvp->v_vflag & VV_ROOT) == 0) {
250681a5bbeSBoris Popov 			vref(dvp);
251681a5bbeSBoris Popov 			np->n_flag |= NREFPARENT;
252681a5bbeSBoris Popov 		}
253681a5bbeSBoris Popov 	} else if (vp->v_type == VREG)
254681a5bbeSBoris Popov 		SMBERROR("new vnode '%s' born without parent ?\n", np->n_name);
255681a5bbeSBoris Popov 
256cb05b60aSAttilio Rao 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2573634d5b2SJohn Baldwin 	VN_LOCK_AREC(vp);
258681a5bbeSBoris Popov 
259e54fdca2SRobert Watson 	smbfs_hash_lock(smp);
260681a5bbeSBoris Popov 	LIST_FOREACH(np2, nhpp, n_hash) {
26111de0c59STim J. Robbins 		if (np2->n_parent != dvp ||
262681a5bbeSBoris Popov 		    np2->n_nmlen != nmlen || bcmp(name, np2->n_name, nmlen) != 0)
263681a5bbeSBoris Popov 			continue;
264681a5bbeSBoris Popov 		vput(vp);
265681a5bbeSBoris Popov /*		smb_name_free(np->n_name);
2661ede983cSDag-Erling Smørgrav 		free(np, M_SMBNODE);*/
267681a5bbeSBoris Popov 		goto loop;
268681a5bbeSBoris Popov 	}
269681a5bbeSBoris Popov 	LIST_INSERT_HEAD(nhpp, np, n_hash);
270e54fdca2SRobert Watson 	smbfs_hash_unlock(smp);
271681a5bbeSBoris Popov 	*vpp = vp;
272681a5bbeSBoris Popov 	return 0;
273681a5bbeSBoris Popov }
274681a5bbeSBoris Popov 
275681a5bbeSBoris Popov int
276681a5bbeSBoris Popov smbfs_nget(struct mount *mp, struct vnode *dvp, const char *name, int nmlen,
277681a5bbeSBoris Popov 	struct smbfattr *fap, struct vnode **vpp)
278681a5bbeSBoris Popov {
279681a5bbeSBoris Popov 	struct smbnode *np;
280681a5bbeSBoris Popov 	struct vnode *vp;
281681a5bbeSBoris Popov 	int error;
282681a5bbeSBoris Popov 
283681a5bbeSBoris Popov 	*vpp = NULL;
284681a5bbeSBoris Popov 	error = smbfs_node_alloc(mp, dvp, name, nmlen, fap, &vp);
285681a5bbeSBoris Popov 	if (error)
286681a5bbeSBoris Popov 		return error;
287681a5bbeSBoris Popov 	np = VTOSMB(vp);
288681a5bbeSBoris Popov 	if (fap)
289681a5bbeSBoris Popov 		smbfs_attr_cacheenter(vp, fap);
290681a5bbeSBoris Popov 	*vpp = vp;
291681a5bbeSBoris Popov 	return 0;
292681a5bbeSBoris Popov }
293681a5bbeSBoris Popov 
294681a5bbeSBoris Popov /*
295681a5bbeSBoris Popov  * Free smbnode, and give vnode back to system
296681a5bbeSBoris Popov  */
297681a5bbeSBoris Popov int
298681a5bbeSBoris Popov smbfs_reclaim(ap)
299681a5bbeSBoris Popov         struct vop_reclaim_args /* {
300681a5bbeSBoris Popov 		struct vnode *a_vp;
301b1c996c4SBoris Popov 		struct thread *a_p;
302681a5bbeSBoris Popov         } */ *ap;
303681a5bbeSBoris Popov {
304681a5bbeSBoris Popov 	struct vnode *vp = ap->a_vp;
305681a5bbeSBoris Popov 	struct vnode *dvp;
306681a5bbeSBoris Popov 	struct smbnode *np = VTOSMB(vp);
307681a5bbeSBoris Popov 	struct smbmount *smp = VTOSMBFS(vp);
308681a5bbeSBoris Popov 
3094d93c0beSJeff Roberson 	SMBVDEBUG("%s,%d\n", np->n_name, vrefcnt(vp));
310681a5bbeSBoris Popov 
3112a4ad258STim J. Robbins 	KASSERT((np->n_flag & NOPEN) == 0, ("file not closed before reclaim"));
3122a4ad258STim J. Robbins 
313e54fdca2SRobert Watson 	smbfs_hash_lock(smp);
31492e73f57SAlfred Perlstein 	/*
31592e73f57SAlfred Perlstein 	 * Destroy the vm object and flush associated pages.
31692e73f57SAlfred Perlstein 	 */
31792e73f57SAlfred Perlstein 	vnode_destroy_vobject(vp);
318681a5bbeSBoris Popov 
319681a5bbeSBoris Popov 	dvp = (np->n_parent && (np->n_flag & NREFPARENT)) ?
32011de0c59STim J. Robbins 	    np->n_parent : NULL;
321681a5bbeSBoris Popov 
322681a5bbeSBoris Popov 	if (np->n_hash.le_prev)
323681a5bbeSBoris Popov 		LIST_REMOVE(np, n_hash);
324681a5bbeSBoris Popov 	if (smp->sm_root == np) {
325681a5bbeSBoris Popov 		SMBVDEBUG("root vnode\n");
326681a5bbeSBoris Popov 		smp->sm_root = NULL;
327681a5bbeSBoris Popov 	}
328681a5bbeSBoris Popov 	vp->v_data = NULL;
329e54fdca2SRobert Watson 	smbfs_hash_unlock(smp);
330681a5bbeSBoris Popov 	if (np->n_name)
331681a5bbeSBoris Popov 		smbfs_name_free(np->n_name);
3321ede983cSDag-Erling Smørgrav 	free(np, M_SMBNODE);
33370a3c70aSTim J. Robbins 	if (dvp != NULL) {
334681a5bbeSBoris Popov 		vrele(dvp);
335578dcf0cSTim J. Robbins 		/*
336578dcf0cSTim J. Robbins 		 * Indicate that we released something; see comment
337578dcf0cSTim J. Robbins 		 * in smbfs_unmount().
338578dcf0cSTim J. Robbins 		 */
339578dcf0cSTim J. Robbins 		smp->sm_didrele = 1;
340681a5bbeSBoris Popov 	}
341681a5bbeSBoris Popov 	return 0;
342681a5bbeSBoris Popov }
343681a5bbeSBoris Popov 
344681a5bbeSBoris Popov int
345681a5bbeSBoris Popov smbfs_inactive(ap)
346681a5bbeSBoris Popov 	struct vop_inactive_args /* {
347681a5bbeSBoris Popov 		struct vnode *a_vp;
348b1c996c4SBoris Popov 		struct thread *a_td;
349681a5bbeSBoris Popov 	} */ *ap;
350681a5bbeSBoris Popov {
351b1c996c4SBoris Popov 	struct thread *td = ap->a_td;
352a854ed98SJohn Baldwin 	struct ucred *cred = td->td_ucred;
353681a5bbeSBoris Popov 	struct vnode *vp = ap->a_vp;
354681a5bbeSBoris Popov 	struct smbnode *np = VTOSMB(vp);
355681a5bbeSBoris Popov 	struct smb_cred scred;
3562a4ad258STim J. Robbins 	struct vattr va;
357681a5bbeSBoris Popov 
3584d93c0beSJeff Roberson 	SMBVDEBUG("%s: %d\n", VTOSMB(vp)->n_name, vrefcnt(vp));
3592a4ad258STim J. Robbins 	if ((np->n_flag & NOPEN) != 0) {
360b1c996c4SBoris Popov 		smb_makescred(&scred, td, cred);
361e50508dfSPoul-Henning Kamp 		smbfs_vinvalbuf(vp, td);
3622a4ad258STim J. Robbins 		if (vp->v_type == VREG) {
3630359a12eSAttilio Rao 			VOP_GETATTR(vp, &va, cred);
3642a4ad258STim J. Robbins 			smbfs_smb_close(np->n_mount->sm_share, np->n_fid,
365681a5bbeSBoris Popov 			    &np->n_mtime, &scred);
3662a4ad258STim J. Robbins 		} else if (vp->v_type == VDIR) {
3672a4ad258STim J. Robbins 			if (np->n_dirseq != NULL) {
3682a4ad258STim J. Robbins 				smbfs_findclose(np->n_dirseq, &scred);
3692a4ad258STim J. Robbins 				np->n_dirseq = NULL;
370681a5bbeSBoris Popov 			}
3712a4ad258STim J. Robbins 		}
3722a4ad258STim J. Robbins 		np->n_flag &= ~NOPEN;
3732a4ad258STim J. Robbins 		smbfs_attr_cacheremove(vp);
374208a7a97STim J. Robbins 	}
375b4484bf0STim J. Robbins 	if (np->n_flag & NGONE)
376d4eb29baSPoul-Henning Kamp 		vrecycle(vp, td);
377681a5bbeSBoris Popov 	return (0);
378681a5bbeSBoris Popov }
379681a5bbeSBoris Popov /*
380681a5bbeSBoris Popov  * routines to maintain vnode attributes cache
381681a5bbeSBoris Popov  * smbfs_attr_cacheenter: unpack np.i to vattr structure
382681a5bbeSBoris Popov  */
383681a5bbeSBoris Popov void
384681a5bbeSBoris Popov smbfs_attr_cacheenter(struct vnode *vp, struct smbfattr *fap)
385681a5bbeSBoris Popov {
386681a5bbeSBoris Popov 	struct smbnode *np = VTOSMB(vp);
387681a5bbeSBoris Popov 
388681a5bbeSBoris Popov 	if (vp->v_type == VREG) {
389681a5bbeSBoris Popov 		if (np->n_size != fap->fa_size) {
390681a5bbeSBoris Popov 			np->n_size = fap->fa_size;
391681a5bbeSBoris Popov 			vnode_pager_setsize(vp, np->n_size);
392681a5bbeSBoris Popov 		}
393681a5bbeSBoris Popov 	} else if (vp->v_type == VDIR) {
394681a5bbeSBoris Popov 		np->n_size = 16384; 		/* should be a better way ... */
395681a5bbeSBoris Popov 	} else
396681a5bbeSBoris Popov 		return;
397681a5bbeSBoris Popov 	np->n_mtime = fap->fa_mtime;
398681a5bbeSBoris Popov 	np->n_dosattr = fap->fa_attr;
399681a5bbeSBoris Popov 	np->n_attrage = time_second;
400681a5bbeSBoris Popov 	return;
401681a5bbeSBoris Popov }
402681a5bbeSBoris Popov 
403681a5bbeSBoris Popov int
404681a5bbeSBoris Popov smbfs_attr_cachelookup(struct vnode *vp, struct vattr *va)
405681a5bbeSBoris Popov {
406681a5bbeSBoris Popov 	struct smbnode *np = VTOSMB(vp);
407681a5bbeSBoris Popov 	struct smbmount *smp = VTOSMBFS(vp);
408681a5bbeSBoris Popov 	int diff;
409681a5bbeSBoris Popov 
410681a5bbeSBoris Popov 	diff = time_second - np->n_attrage;
411681a5bbeSBoris Popov 	if (diff > 2)	/* XXX should be configurable */
412681a5bbeSBoris Popov 		return ENOENT;
413681a5bbeSBoris Popov 	va->va_type = vp->v_type;		/* vnode type (for create) */
414681a5bbeSBoris Popov 	if (vp->v_type == VREG) {
415d14c8441SPoul-Henning Kamp 		va->va_mode = smp->sm_file_mode; /* files access mode and type */
416fb8e9eadSBoris Popov 		if (np->n_dosattr & SMB_FA_RDONLY)
417fb8e9eadSBoris Popov 			va->va_mode &= ~(S_IWUSR|S_IWGRP|S_IWOTH);
418681a5bbeSBoris Popov 	} else if (vp->v_type == VDIR) {
419d14c8441SPoul-Henning Kamp 		va->va_mode = smp->sm_dir_mode;	/* files access mode and type */
420681a5bbeSBoris Popov 	} else
421681a5bbeSBoris Popov 		return EINVAL;
422681a5bbeSBoris Popov 	va->va_size = np->n_size;
423681a5bbeSBoris Popov 	va->va_nlink = 1;		/* number of references to file */
424d14c8441SPoul-Henning Kamp 	va->va_uid = smp->sm_uid;	/* owner user id */
425d14c8441SPoul-Henning Kamp 	va->va_gid = smp->sm_gid;	/* owner group id */
426681a5bbeSBoris Popov 	va->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
427681a5bbeSBoris Popov 	va->va_fileid = np->n_ino;	/* file id */
428681a5bbeSBoris Popov 	if (va->va_fileid == 0)
429681a5bbeSBoris Popov 		va->va_fileid = 2;
430681a5bbeSBoris Popov 	va->va_blocksize = SSTOVC(smp->sm_share)->vc_txmax;
431681a5bbeSBoris Popov 	va->va_mtime = np->n_mtime;
432681a5bbeSBoris Popov 	va->va_atime = va->va_ctime = va->va_mtime;	/* time file changed */
433681a5bbeSBoris Popov 	va->va_gen = VNOVAL;		/* generation number of file */
434681a5bbeSBoris Popov 	va->va_flags = 0;		/* flags defined for file */
4354c5a20e3SKonstantin Belousov 	va->va_rdev = NODEV;		/* device the special file represents */
436681a5bbeSBoris Popov 	va->va_bytes = va->va_size;	/* bytes of disk space held by file */
437681a5bbeSBoris Popov 	va->va_filerev = 0;		/* file modification number */
438681a5bbeSBoris Popov 	va->va_vaflags = 0;		/* operations flags */
439681a5bbeSBoris Popov 	return 0;
440681a5bbeSBoris Popov }
441