xref: /freebsd/sys/fs/smbfs/smbfs_node.c (revision 995dc984471c92c03daad19a1d35af46c086ef3e)
1 /*-
2  * Copyright (c) 2000-2001 Boris Popov
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *    This product includes software developed by Boris Popov.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * $FreeBSD$
33  */
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/lock.h>
38 #include <sys/malloc.h>
39 #include <sys/mount.h>
40 #include <sys/proc.h>
41 #include <sys/queue.h>
42 #include <sys/stat.h>
43 #include <sys/sysctl.h>
44 #include <sys/time.h>
45 #include <sys/vnode.h>
46 
47 #include <netsmb/smb.h>
48 #include <netsmb/smb_conn.h>
49 #include <netsmb/smb_subr.h>
50 
51 #include <vm/vm.h>
52 #include <vm/vm_extern.h>
53 /*#include <vm/vm_page.h>
54 #include <vm/vm_object.h>*/
55 
56 #include <fs/smbfs/smbfs.h>
57 #include <fs/smbfs/smbfs_node.h>
58 #include <fs/smbfs/smbfs_subr.h>
59 
60 #define	SMBFS_NOHASH(smp, hval)	(&(smp)->sm_hash[(hval) & (smp)->sm_hashlen])
61 #define	smbfs_hash_lock(smp, td)					\
62 	lockmgr(&smp->sm_hashlock, LK_EXCLUSIVE, NULL)
63 #define	smbfs_hash_unlock(smp, td)					\
64 	lockmgr(&smp->sm_hashlock, LK_RELEASE, NULL)
65 
66 
67 extern struct vop_vector smbfs_vnodeops;	/* XXX -> .h file */
68 
69 MALLOC_DEFINE(M_SMBNODE, "smbufs_node", "SMBFS vnode private part");
70 static MALLOC_DEFINE(M_SMBNODENAME, "smbufs_nname", "SMBFS node name");
71 
72 int smbfs_hashprint(struct mount *mp);
73 
74 #if 0
75 #ifdef SYSCTL_DECL
76 SYSCTL_DECL(_vfs_smbfs);
77 #endif
78 SYSCTL_PROC(_vfs_smbfs, OID_AUTO, vnprint, CTLFLAG_WR|CTLTYPE_OPAQUE,
79 	    NULL, 0, smbfs_hashprint, "S,vnlist", "vnode hash");
80 #endif
81 
82 #define	FNV_32_PRIME ((u_int32_t) 0x01000193UL)
83 #define	FNV1_32_INIT ((u_int32_t) 33554467UL)
84 
85 u_int32_t
86 smbfs_hash(const u_char *name, int nmlen)
87 {
88 	u_int32_t v;
89 
90 	for (v = FNV1_32_INIT; nmlen; name++, nmlen--) {
91 		v *= FNV_32_PRIME;
92 		v ^= (u_int32_t)*name;
93 	}
94 	return v;
95 }
96 
97 int
98 smbfs_hashprint(struct mount *mp)
99 {
100 	struct smbmount *smp = VFSTOSMBFS(mp);
101 	struct smbnode_hashhead *nhpp;
102 	struct smbnode *np;
103 	int i;
104 
105 	for(i = 0; i <= smp->sm_hashlen; i++) {
106 		nhpp = &smp->sm_hash[i];
107 		LIST_FOREACH(np, nhpp, n_hash)
108 			vprint("", SMBTOV(np));
109 	}
110 	return 0;
111 }
112 
113 static char *
114 smbfs_name_alloc(const u_char *name, int nmlen)
115 {
116 	u_char *cp;
117 
118 	nmlen++;
119 #ifdef SMBFS_NAME_DEBUG
120 	cp = malloc(nmlen + 2 + sizeof(int), M_SMBNODENAME, M_WAITOK);
121 	*(int*)cp = nmlen;
122 	cp += sizeof(int);
123 	cp[0] = 0xfc;
124 	cp++;
125 	bcopy(name, cp, nmlen - 1);
126 	cp[nmlen] = 0xfe;
127 #else
128 	cp = malloc(nmlen, M_SMBNODENAME, M_WAITOK);
129 	bcopy(name, cp, nmlen - 1);
130 #endif
131 	cp[nmlen - 1] = 0;
132 	return cp;
133 }
134 
135 static void
136 smbfs_name_free(u_char *name)
137 {
138 #ifdef SMBFS_NAME_DEBUG
139 	int nmlen, slen;
140 	u_char *cp;
141 
142 	cp = name;
143 	cp--;
144 	if (*cp != 0xfc)
145 		panic("First byte of name entry '%s' corrupted", name);
146 	cp -= sizeof(int);
147 	nmlen = *(int*)cp;
148 	slen = strlen(name) + 1;
149 	if (nmlen != slen)
150 		panic("Name length mismatch: was %d, now %d name '%s'",
151 		    nmlen, slen, name);
152 	if (name[nmlen] != 0xfe)
153 		panic("Last byte of name entry '%s' corrupted\n", name);
154 	free(cp, M_SMBNODENAME);
155 #else
156 	free(name, M_SMBNODENAME);
157 #endif
158 }
159 
160 static int
161 smbfs_node_alloc(struct mount *mp, struct vnode *dvp,
162 	const char *name, int nmlen, struct smbfattr *fap, struct vnode **vpp)
163 {
164 	struct vattr vattr;
165 	struct thread *td = curthread;	/* XXX */
166 	struct smbmount *smp = VFSTOSMBFS(mp);
167 	struct smbnode_hashhead *nhpp;
168 	struct smbnode *np, *np2, *dnp;
169 	struct vnode *vp;
170 	u_long hashval;
171 	int error;
172 
173 	*vpp = NULL;
174 	if (smp->sm_root != NULL && dvp == NULL) {
175 		SMBERROR("do not allocate root vnode twice!\n");
176 		return EINVAL;
177 	}
178 	if (nmlen == 2 && bcmp(name, "..", 2) == 0) {
179 		if (dvp == NULL)
180 			return EINVAL;
181 		vp = VTOSMB(VTOSMB(dvp)->n_parent)->n_vnode;
182 		error = vget(vp, LK_EXCLUSIVE, td);
183 		if (error == 0)
184 			*vpp = vp;
185 		return error;
186 	} else if (nmlen == 1 && name[0] == '.') {
187 		SMBERROR("do not call me with dot!\n");
188 		return EINVAL;
189 	}
190 	dnp = dvp ? VTOSMB(dvp) : NULL;
191 	if (dnp == NULL && dvp != NULL) {
192 		vprint("smbfs_node_alloc: dead parent vnode", dvp);
193 		return EINVAL;
194 	}
195 	hashval = smbfs_hash(name, nmlen);
196 retry:
197 	smbfs_hash_lock(smp, td);
198 loop:
199 	nhpp = SMBFS_NOHASH(smp, hashval);
200 	LIST_FOREACH(np, nhpp, n_hash) {
201 		vp = SMBTOV(np);
202 		if (np->n_parent != dvp ||
203 		    np->n_nmlen != nmlen || bcmp(name, np->n_name, nmlen) != 0)
204 			continue;
205 		VI_LOCK(vp);
206 		smbfs_hash_unlock(smp, td);
207 		if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td) != 0)
208 			goto retry;
209 		/* Force cached attributes to be refreshed if stale. */
210 		(void)VOP_GETATTR(vp, &vattr, td->td_ucred, td);
211 		/*
212 		 * If the file type on the server is inconsistent with
213 		 * what it was when we created the vnode, kill the
214 		 * bogus vnode now and fall through to the code below
215 		 * to create a new one with the right type.
216 		 */
217 		if ((vp->v_type == VDIR && (np->n_dosattr & SMB_FA_DIR) == 0) ||
218 		    (vp->v_type == VREG && (np->n_dosattr & SMB_FA_DIR) != 0)) {
219 			vgone(vp);
220 			vput(vp);
221 			break;
222 		}
223 		*vpp = vp;
224 		return 0;
225 	}
226 	smbfs_hash_unlock(smp, td);
227 	/*
228 	 * If we don't have node attributes, then it is an explicit lookup
229 	 * for an existing vnode.
230 	 */
231 	if (fap == NULL)
232 		return ENOENT;
233 
234 	MALLOC(np, struct smbnode *, sizeof *np, M_SMBNODE, M_WAITOK);
235 	error = getnewvnode("smbfs", mp, &smbfs_vnodeops, &vp);
236 	if (error) {
237 		FREE(np, M_SMBNODE);
238 		return error;
239 	}
240 	error = insmntque(vp, mp);	/* XXX: Too early for mpsafe fs */
241 	if (error != 0) {
242 		FREE(np, M_SMBNODE);
243 		return (error);
244 	}
245 	vp->v_type = fap->fa_attr & SMB_FA_DIR ? VDIR : VREG;
246 	bzero(np, sizeof(*np));
247 	vp->v_data = np;
248 	np->n_vnode = vp;
249 	np->n_mount = VFSTOSMBFS(mp);
250 	np->n_nmlen = nmlen;
251 	np->n_name = smbfs_name_alloc(name, nmlen);
252 	np->n_ino = fap->fa_ino;
253 
254 	if (dvp) {
255 		ASSERT_VOP_LOCKED(dvp, "smbfs_node_alloc");
256 		np->n_parent = dvp;
257 		if (/*vp->v_type == VDIR &&*/ (dvp->v_vflag & VV_ROOT) == 0) {
258 			vref(dvp);
259 			np->n_flag |= NREFPARENT;
260 		}
261 	} else if (vp->v_type == VREG)
262 		SMBERROR("new vnode '%s' born without parent ?\n", np->n_name);
263 
264 	vp->v_vnlock->lk_flags |= LK_CANRECURSE;
265 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
266 
267 	smbfs_hash_lock(smp, td);
268 	LIST_FOREACH(np2, nhpp, n_hash) {
269 		if (np2->n_parent != dvp ||
270 		    np2->n_nmlen != nmlen || bcmp(name, np2->n_name, nmlen) != 0)
271 			continue;
272 		vput(vp);
273 /*		smb_name_free(np->n_name);
274 		FREE(np, M_SMBNODE);*/
275 		goto loop;
276 	}
277 	LIST_INSERT_HEAD(nhpp, np, n_hash);
278 	smbfs_hash_unlock(smp, td);
279 	*vpp = vp;
280 	return 0;
281 }
282 
283 int
284 smbfs_nget(struct mount *mp, struct vnode *dvp, const char *name, int nmlen,
285 	struct smbfattr *fap, struct vnode **vpp)
286 {
287 	struct smbnode *np;
288 	struct vnode *vp;
289 	int error;
290 
291 	*vpp = NULL;
292 	error = smbfs_node_alloc(mp, dvp, name, nmlen, fap, &vp);
293 	if (error)
294 		return error;
295 	np = VTOSMB(vp);
296 	if (fap)
297 		smbfs_attr_cacheenter(vp, fap);
298 	*vpp = vp;
299 	return 0;
300 }
301 
302 /*
303  * Free smbnode, and give vnode back to system
304  */
305 int
306 smbfs_reclaim(ap)
307         struct vop_reclaim_args /* {
308 		struct vnode *a_vp;
309 		struct thread *a_p;
310         } */ *ap;
311 {
312 	struct vnode *vp = ap->a_vp;
313 	struct vnode *dvp;
314 	struct smbnode *np = VTOSMB(vp);
315 	struct smbmount *smp = VTOSMBFS(vp);
316 
317 	SMBVDEBUG("%s,%d\n", np->n_name, vrefcnt(vp));
318 
319 	KASSERT((np->n_flag & NOPEN) == 0, ("file not closed before reclaim"));
320 
321 	smbfs_hash_lock(smp, td);
322 	/*
323 	 * Destroy the vm object and flush associated pages.
324 	 */
325 	vnode_destroy_vobject(vp);
326 
327 	dvp = (np->n_parent && (np->n_flag & NREFPARENT)) ?
328 	    np->n_parent : NULL;
329 
330 	if (np->n_hash.le_prev)
331 		LIST_REMOVE(np, n_hash);
332 	if (smp->sm_root == np) {
333 		SMBVDEBUG("root vnode\n");
334 		smp->sm_root = NULL;
335 	}
336 	vp->v_data = NULL;
337 	smbfs_hash_unlock(smp, td);
338 	if (np->n_name)
339 		smbfs_name_free(np->n_name);
340 	FREE(np, M_SMBNODE);
341 	if (dvp != NULL) {
342 		vrele(dvp);
343 		/*
344 		 * Indicate that we released something; see comment
345 		 * in smbfs_unmount().
346 		 */
347 		smp->sm_didrele = 1;
348 	}
349 	return 0;
350 }
351 
352 int
353 smbfs_inactive(ap)
354 	struct vop_inactive_args /* {
355 		struct vnode *a_vp;
356 		struct thread *a_td;
357 	} */ *ap;
358 {
359 	struct thread *td = ap->a_td;
360 	struct ucred *cred = td->td_ucred;
361 	struct vnode *vp = ap->a_vp;
362 	struct smbnode *np = VTOSMB(vp);
363 	struct smb_cred scred;
364 	struct vattr va;
365 
366 	SMBVDEBUG("%s: %d\n", VTOSMB(vp)->n_name, vrefcnt(vp));
367 	if ((np->n_flag & NOPEN) != 0) {
368 		smb_makescred(&scred, td, cred);
369 		smbfs_vinvalbuf(vp, td);
370 		if (vp->v_type == VREG) {
371 			VOP_GETATTR(vp, &va, cred, td);
372 			smbfs_smb_close(np->n_mount->sm_share, np->n_fid,
373 			    &np->n_mtime, &scred);
374 		} else if (vp->v_type == VDIR) {
375 			if (np->n_dirseq != NULL) {
376 				smbfs_findclose(np->n_dirseq, &scred);
377 				np->n_dirseq = NULL;
378 			}
379 		}
380 		np->n_flag &= ~NOPEN;
381 		smbfs_attr_cacheremove(vp);
382 	}
383 	if (np->n_flag & NGONE)
384 		vrecycle(vp, td);
385 	return (0);
386 }
387 /*
388  * routines to maintain vnode attributes cache
389  * smbfs_attr_cacheenter: unpack np.i to vattr structure
390  */
391 void
392 smbfs_attr_cacheenter(struct vnode *vp, struct smbfattr *fap)
393 {
394 	struct smbnode *np = VTOSMB(vp);
395 
396 	if (vp->v_type == VREG) {
397 		if (np->n_size != fap->fa_size) {
398 			np->n_size = fap->fa_size;
399 			vnode_pager_setsize(vp, np->n_size);
400 		}
401 	} else if (vp->v_type == VDIR) {
402 		np->n_size = 16384; 		/* should be a better way ... */
403 	} else
404 		return;
405 	np->n_mtime = fap->fa_mtime;
406 	np->n_dosattr = fap->fa_attr;
407 	np->n_attrage = time_second;
408 	return;
409 }
410 
411 int
412 smbfs_attr_cachelookup(struct vnode *vp, struct vattr *va)
413 {
414 	struct smbnode *np = VTOSMB(vp);
415 	struct smbmount *smp = VTOSMBFS(vp);
416 	int diff;
417 
418 	diff = time_second - np->n_attrage;
419 	if (diff > 2)	/* XXX should be configurable */
420 		return ENOENT;
421 	va->va_type = vp->v_type;		/* vnode type (for create) */
422 	if (vp->v_type == VREG) {
423 		va->va_mode = smp->sm_file_mode; /* files access mode and type */
424 		if (np->n_dosattr & SMB_FA_RDONLY)
425 			va->va_mode &= ~(S_IWUSR|S_IWGRP|S_IWOTH);
426 	} else if (vp->v_type == VDIR) {
427 		va->va_mode = smp->sm_dir_mode;	/* files access mode and type */
428 	} else
429 		return EINVAL;
430 	va->va_size = np->n_size;
431 	va->va_nlink = 1;		/* number of references to file */
432 	va->va_uid = smp->sm_uid;	/* owner user id */
433 	va->va_gid = smp->sm_gid;	/* owner group id */
434 	va->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
435 	va->va_fileid = np->n_ino;	/* file id */
436 	if (va->va_fileid == 0)
437 		va->va_fileid = 2;
438 	va->va_blocksize = SSTOVC(smp->sm_share)->vc_txmax;
439 	va->va_mtime = np->n_mtime;
440 	va->va_atime = va->va_ctime = va->va_mtime;	/* time file changed */
441 	va->va_gen = VNOVAL;		/* generation number of file */
442 	va->va_flags = 0;		/* flags defined for file */
443 	va->va_rdev = VNOVAL;		/* device the special file represents */
444 	va->va_bytes = va->va_size;	/* bytes of disk space held by file */
445 	va->va_filerev = 0;		/* file modification number */
446 	va->va_vaflags = 0;		/* operations flags */
447 	return 0;
448 }
449