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