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