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