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