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