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