xref: /freebsd/sys/fs/smbfs/smbfs_node.c (revision f4b37ed0f8b307b1f3f0f630ca725d68f1dff30d)
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  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/fnv_hash.h>
31 #include <sys/kernel.h>
32 #include <sys/lock.h>
33 #include <sys/malloc.h>
34 #include <sys/mount.h>
35 #include <sys/proc.h>
36 #include <sys/queue.h>
37 #include <sys/stat.h>
38 #include <sys/sx.h>
39 #include <sys/sysctl.h>
40 #include <sys/time.h>
41 #include <sys/vnode.h>
42 
43 #include <netsmb/smb.h>
44 #include <netsmb/smb_conn.h>
45 #include <netsmb/smb_subr.h>
46 
47 #include <vm/vm.h>
48 #include <vm/vm_extern.h>
49 /*#include <vm/vm_page.h>
50 #include <vm/vm_object.h>*/
51 
52 #include <fs/smbfs/smbfs.h>
53 #include <fs/smbfs/smbfs_node.h>
54 #include <fs/smbfs/smbfs_subr.h>
55 
56 extern struct vop_vector smbfs_vnodeops;	/* XXX -> .h file */
57 
58 static MALLOC_DEFINE(M_SMBNODE, "smbufs_node", "SMBFS vnode private part");
59 static MALLOC_DEFINE(M_SMBNODENAME, "smbufs_nname", "SMBFS node name");
60 
61 u_int32_t __inline
62 smbfs_hash(const u_char *name, int nmlen)
63 {
64 	return (fnv_32_buf(name, nmlen, FNV1_32_INIT));
65 }
66 
67 static char *
68 smbfs_name_alloc(const u_char *name, int nmlen)
69 {
70 	u_char *cp;
71 
72 	nmlen++;
73 	cp = malloc(nmlen, M_SMBNODENAME, M_WAITOK);
74 	bcopy(name, cp, nmlen - 1);
75 	cp[nmlen - 1] = 0;
76 	return cp;
77 }
78 
79 static void
80 smbfs_name_free(u_char *name)
81 {
82 
83 	free(name, M_SMBNODENAME);
84 }
85 
86 static int __inline
87 smbfs_vnode_cmp(struct vnode *vp, void *_sc)
88 {
89 	struct smbnode *np;
90 	struct smbcmp *sc;
91 
92 	np = (struct smbnode *) vp->v_data;
93 	sc = (struct smbcmp *) _sc;
94 	if (np->n_parent != sc->n_parent || np->n_nmlen != sc->n_nmlen ||
95 	    bcmp(sc->n_name, np->n_name, sc->n_nmlen) != 0)
96 		return 1;
97 	return 0;
98 }
99 
100 static int
101 smbfs_node_alloc(struct mount *mp, struct vnode *dvp, const char *dirnm,
102 	int dirlen, const char *name, int nmlen, char sep,
103 	struct smbfattr *fap, struct vnode **vpp)
104 {
105 	struct vattr vattr;
106 	struct thread *td = curthread;	/* XXX */
107 	struct smbmount *smp = VFSTOSMBFS(mp);
108 	struct smbnode *np, *dnp;
109 	struct vnode *vp, *vp2;
110 	struct smbcmp sc;
111 	char *p, *rpath;
112 	int error, rplen;
113 
114 	sc.n_parent = dvp;
115 	sc.n_nmlen = nmlen;
116 	sc.n_name = name;
117 	if (smp->sm_root != NULL && dvp == NULL) {
118 		SMBERROR("do not allocate root vnode twice!\n");
119 		return EINVAL;
120 	}
121 	if (nmlen == 2 && bcmp(name, "..", 2) == 0) {
122 		if (dvp == NULL)
123 			return EINVAL;
124 		vp = VTOSMB(VTOSMB(dvp)->n_parent)->n_vnode;
125 		error = vget(vp, LK_EXCLUSIVE, td);
126 		if (error == 0)
127 			*vpp = vp;
128 		return error;
129 	} else if (nmlen == 1 && name[0] == '.') {
130 		SMBERROR("do not call me with dot!\n");
131 		return EINVAL;
132 	}
133 	dnp = dvp ? VTOSMB(dvp) : NULL;
134 	if (dnp == NULL && dvp != NULL) {
135 		vprint("smbfs_node_alloc: dead parent vnode", dvp);
136 		return EINVAL;
137 	}
138 	error = vfs_hash_get(mp, smbfs_hash(name, nmlen), LK_EXCLUSIVE, td,
139 	    vpp, smbfs_vnode_cmp, &sc);
140 	if (error)
141 		return (error);
142 	if (*vpp) {
143 		np = VTOSMB(*vpp);
144 		/* Force cached attributes to be refreshed if stale. */
145 		(void)VOP_GETATTR(*vpp, &vattr, td->td_ucred);
146 		/*
147 		 * If the file type on the server is inconsistent with
148 		 * what it was when we created the vnode, kill the
149 		 * bogus vnode now and fall through to the code below
150 		 * to create a new one with the right type.
151 		 */
152 		if (((*vpp)->v_type == VDIR &&
153 		    (np->n_dosattr & SMB_FA_DIR) == 0) ||
154 	    	    ((*vpp)->v_type == VREG &&
155 		    (np->n_dosattr & SMB_FA_DIR) != 0)) {
156 			vgone(*vpp);
157 			vput(*vpp);
158 		}
159 		else {
160 			SMBVDEBUG("vnode taken from the hashtable\n");
161 			return (0);
162 		}
163 	}
164 	/*
165 	 * If we don't have node attributes, then it is an explicit lookup
166 	 * for an existing vnode.
167 	 */
168 	if (fap == NULL)
169 		return ENOENT;
170 
171 	error = getnewvnode("smbfs", mp, &smbfs_vnodeops, vpp);
172 	if (error)
173 		return (error);
174 	vp = *vpp;
175 	np = malloc(sizeof *np, M_SMBNODE, M_WAITOK | M_ZERO);
176 	rplen = dirlen;
177 	if (sep != '\0')
178 		rplen++;
179 	rplen += nmlen;
180 	rpath = malloc(rplen + 1, M_SMBNODENAME, M_WAITOK);
181 	p = rpath;
182 	bcopy(dirnm, p, dirlen);
183 	p += dirlen;
184 	if (sep != '\0')
185 		*p++ = sep;
186 	if (name != NULL) {
187 		bcopy(name, p, nmlen);
188 		p += nmlen;
189 	}
190 	*p = '\0';
191 	MPASS(p == rpath + rplen);
192 	lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL);
193 	/* Vnode initialization */
194 	vp->v_type = fap->fa_attr & SMB_FA_DIR ? VDIR : VREG;
195 	vp->v_data = np;
196 	np->n_vnode = vp;
197 	np->n_mount = VFSTOSMBFS(mp);
198 	np->n_rpath = rpath;
199 	np->n_rplen = rplen;
200 	np->n_nmlen = nmlen;
201 	np->n_name = smbfs_name_alloc(name, nmlen);
202 	np->n_ino = fap->fa_ino;
203 	if (dvp) {
204 		ASSERT_VOP_LOCKED(dvp, "smbfs_node_alloc");
205 		np->n_parent = dvp;
206 		np->n_parentino = VTOSMB(dvp)->n_ino;
207 		if (/*vp->v_type == VDIR &&*/ (dvp->v_vflag & VV_ROOT) == 0) {
208 			vref(dvp);
209 			np->n_flag |= NREFPARENT;
210 		}
211 	} else if (vp->v_type == VREG)
212 		SMBERROR("new vnode '%s' born without parent ?\n", np->n_name);
213 	error = insmntque(vp, mp);
214 	if (error) {
215 		free(np, M_SMBNODE);
216 		return (error);
217 	}
218 	error = vfs_hash_insert(vp, smbfs_hash(name, nmlen), LK_EXCLUSIVE,
219 	    td, &vp2, smbfs_vnode_cmp, &sc);
220 	if (error)
221 		return (error);
222 	if (vp2 != NULL)
223 		*vpp = vp2;
224 	return (0);
225 }
226 
227 int
228 smbfs_nget(struct mount *mp, struct vnode *dvp, const char *name, int nmlen,
229 	struct smbfattr *fap, struct vnode **vpp)
230 {
231 	struct smbnode *dnp, *np;
232 	struct vnode *vp;
233 	int error, sep;
234 
235 	dnp = (dvp) ? VTOSMB(dvp) : NULL;
236 	sep = 0;
237 	if (dnp != NULL) {
238 		sep = SMBFS_DNP_SEP(dnp);
239 		error = smbfs_node_alloc(mp, dvp, dnp->n_rpath, dnp->n_rplen,
240 		    name, nmlen, sep, fap, &vp);
241 	} else
242 		error = smbfs_node_alloc(mp, NULL, "\\", 1, name, nmlen,
243 		    sep, fap, &vp);
244 	if (error)
245 		return error;
246 	MPASS(vp != NULL);
247 	np = VTOSMB(vp);
248 	if (fap)
249 		smbfs_attr_cacheenter(vp, fap);
250 	*vpp = vp;
251 	return 0;
252 }
253 
254 /*
255  * Free smbnode, and give vnode back to system
256  */
257 int
258 smbfs_reclaim(ap)
259         struct vop_reclaim_args /* {
260 		struct vnode *a_vp;
261 		struct thread *a_p;
262         } */ *ap;
263 {
264 	struct vnode *vp = ap->a_vp;
265 	struct vnode *dvp;
266 	struct smbnode *np = VTOSMB(vp);
267 	struct smbmount *smp = VTOSMBFS(vp);
268 
269 	SMBVDEBUG("%s,%d\n", np->n_name, vrefcnt(vp));
270 
271 	KASSERT((np->n_flag & NOPEN) == 0, ("file not closed before reclaim"));
272 
273 	/*
274 	 * Destroy the vm object and flush associated pages.
275 	 */
276 	vnode_destroy_vobject(vp);
277 	dvp = (np->n_parent && (np->n_flag & NREFPARENT)) ?
278 	    np->n_parent : NULL;
279 
280 	/*
281 	 * Remove the vnode from its hash chain.
282 	 */
283 	vfs_hash_remove(vp);
284 	if (np->n_name)
285 		smbfs_name_free(np->n_name);
286 	if (np->n_rpath)
287 		free(np->n_rpath, M_SMBNODENAME);
288 	free(np, M_SMBNODE);
289 	vp->v_data = NULL;
290 	if (dvp != NULL) {
291 		vrele(dvp);
292 		/*
293 		 * Indicate that we released something; see comment
294 		 * in smbfs_unmount().
295 		 */
296 		smp->sm_didrele = 1;
297 	}
298 	return 0;
299 }
300 
301 int
302 smbfs_inactive(ap)
303 	struct vop_inactive_args /* {
304 		struct vnode *a_vp;
305 		struct thread *a_td;
306 	} */ *ap;
307 {
308 	struct thread *td = ap->a_td;
309 	struct ucred *cred = td->td_ucred;
310 	struct vnode *vp = ap->a_vp;
311 	struct smbnode *np = VTOSMB(vp);
312 	struct smb_cred *scred;
313 	struct vattr va;
314 
315 	SMBVDEBUG("%s: %d\n", VTOSMB(vp)->n_name, vrefcnt(vp));
316 	if ((np->n_flag & NOPEN) != 0) {
317 		scred = smbfs_malloc_scred();
318 		smb_makescred(scred, td, cred);
319 		smbfs_vinvalbuf(vp, td);
320 		if (vp->v_type == VREG) {
321 			VOP_GETATTR(vp, &va, cred);
322 			smbfs_smb_close(np->n_mount->sm_share, np->n_fid,
323 			    &np->n_mtime, scred);
324 		} else if (vp->v_type == VDIR) {
325 			if (np->n_dirseq != NULL) {
326 				smbfs_findclose(np->n_dirseq, scred);
327 				np->n_dirseq = NULL;
328 			}
329 		}
330 		np->n_flag &= ~NOPEN;
331 		smbfs_attr_cacheremove(vp);
332 		smbfs_free_scred(scred);
333 	}
334 	if (np->n_flag & NGONE)
335 		vrecycle(vp);
336 	return (0);
337 }
338 /*
339  * routines to maintain vnode attributes cache
340  * smbfs_attr_cacheenter: unpack np.i to vattr structure
341  */
342 void
343 smbfs_attr_cacheenter(struct vnode *vp, struct smbfattr *fap)
344 {
345 	struct smbnode *np = VTOSMB(vp);
346 
347 	if (vp->v_type == VREG) {
348 		if (np->n_size != fap->fa_size) {
349 			np->n_size = fap->fa_size;
350 			vnode_pager_setsize(vp, np->n_size);
351 		}
352 	} else if (vp->v_type == VDIR) {
353 		np->n_size = 16384; 		/* should be a better way ... */
354 	} else
355 		return;
356 	np->n_mtime = fap->fa_mtime;
357 	np->n_dosattr = fap->fa_attr;
358 	np->n_attrage = time_second;
359 	return;
360 }
361 
362 int
363 smbfs_attr_cachelookup(struct vnode *vp, struct vattr *va)
364 {
365 	struct smbnode *np = VTOSMB(vp);
366 	struct smbmount *smp = VTOSMBFS(vp);
367 	int diff;
368 
369 	diff = time_second - np->n_attrage;
370 	if (diff > 2)	/* XXX should be configurable */
371 		return ENOENT;
372 	va->va_type = vp->v_type;		/* vnode type (for create) */
373 	va->va_flags = 0;			/* flags defined for file */
374 	if (vp->v_type == VREG) {
375 		va->va_mode = smp->sm_file_mode; /* files access mode and type */
376 		if (np->n_dosattr & SMB_FA_RDONLY) {
377 			va->va_mode &= ~(S_IWUSR|S_IWGRP|S_IWOTH);
378 			va->va_flags |= UF_READONLY;
379 		}
380 	} else if (vp->v_type == VDIR) {
381 		va->va_mode = smp->sm_dir_mode;	/* files access mode and type */
382 	} else
383 		return EINVAL;
384 	va->va_size = np->n_size;
385 	va->va_nlink = 1;		/* number of references to file */
386 	va->va_uid = smp->sm_uid;	/* owner user id */
387 	va->va_gid = smp->sm_gid;	/* owner group id */
388 	va->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
389 	va->va_fileid = np->n_ino;	/* file id */
390 	if (va->va_fileid == 0)
391 		va->va_fileid = 2;
392 	va->va_blocksize = SSTOVC(smp->sm_share)->vc_txmax;
393 	va->va_mtime = np->n_mtime;
394 	va->va_atime = va->va_ctime = va->va_mtime;	/* time file changed */
395 	va->va_gen = VNOVAL;		/* generation number of file */
396 	if (np->n_dosattr & SMB_FA_HIDDEN)
397 		va->va_flags |= UF_HIDDEN;
398 	if (np->n_dosattr & SMB_FA_SYSTEM)
399 		va->va_flags |= UF_SYSTEM;
400 	/*
401 	 * We don't set the archive bit for directories.
402 	 */
403 	if ((vp->v_type != VDIR) && (np->n_dosattr & SMB_FA_ARCHIVE))
404 		va->va_flags |= UF_ARCHIVE;
405 	va->va_rdev = NODEV;		/* device the special file represents */
406 	va->va_bytes = va->va_size;	/* bytes of disk space held by file */
407 	va->va_filerev = 0;		/* file modification number */
408 	va->va_vaflags = 0;		/* operations flags */
409 	return 0;
410 }
411