c8421c0912b7a64c0ddbf645bb796ed51695003a
[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.12 2004/08/17 18:57:35 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 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         lwkt_tokref vlock;
172         int error;
173
174         *vpp = NULL;
175         if (smp->sm_root != NULL && dvp == NULL) {
176                 SMBERROR("do not allocate root vnode twice!\n");
177                 return EINVAL;
178         }
179         if (nmlen == 2 && bcmp(name, "..", 2) == 0) {
180                 if (dvp == NULL)
181                         return EINVAL;
182                 vp = VTOSMB(VTOSMB(dvp)->n_parent)->n_vnode;
183                 error = vget(vp, NULL, LK_EXCLUSIVE, td);
184                 if (error == 0)
185                         *vpp = vp;
186                 return error;
187         } else if (nmlen == 1 && name[0] == '.') {
188                 SMBERROR("do not call me with dot!\n");
189                 return EINVAL;
190         }
191         dnp = dvp ? VTOSMB(dvp) : NULL;
192         if (dnp == NULL && dvp != NULL) {
193                 vprint("smbfs_node_alloc: dead parent vnode", dvp);
194                 return EINVAL;
195         }
196         hashval = smbfs_hash(name, nmlen);
197 retry:
198         smbfs_hash_lock(smp, td);
199 loop:
200         nhpp = SMBFS_NOHASH(smp, hashval);
201         LIST_FOREACH(np, nhpp, n_hash) {
202                 vp = SMBTOV(np);
203                 if (np->n_parent != dvp ||
204                     np->n_nmlen != nmlen || bcmp(name, np->n_name, nmlen) != 0)
205                         continue;
206                 VI_LOCK(&vlock, vp);
207                 smbfs_hash_unlock(smp, td);
208                 if (vget(vp, &vlock, LK_EXCLUSIVE | LK_INTERLOCK, td) != 0)
209                         goto retry;
210                 *vpp = vp;
211                 return 0;
212         }
213         smbfs_hash_unlock(smp, td);
214         /*
215          * If we don't have node attributes, then it is an explicit lookup
216          * for an existing vnode.
217          */
218         if (fap == NULL)
219                 return ENOENT;
220
221         MALLOC(np, struct smbnode *, sizeof *np, M_SMBNODE, M_WAITOK);
222         error = getnewvnode(VT_SMBFS, mp, mp->mnt_vn_ops, &vp);
223         if (error) {
224                 FREE(np, M_SMBNODE);
225                 return error;
226         }
227         vp->v_type = fap->fa_attr & SMB_FA_DIR ? VDIR : VREG;
228         bzero(np, sizeof(*np));
229         vp->v_data = np;
230         np->n_vnode = vp;
231         np->n_mount = VFSTOSMBFS(mp);
232         np->n_nmlen = nmlen;
233         np->n_name = smbfs_name_alloc(name, nmlen);
234         np->n_ino = fap->fa_ino;
235
236         if (dvp) {
237                 np->n_parent = dvp;
238                 if (/*vp->v_type == VDIR &&*/ (dvp->v_flag & VROOT) == 0) {
239                         vref(dvp);
240                         np->n_flag |= NREFPARENT;
241                 }
242         } else if (vp->v_type == VREG)
243                 SMBERROR("new vnode '%s' born without parent ?\n", np->n_name);
244
245         lockinit(&np->n_lock, 0, "smbnode", VLKTIMEOUT, LK_CANRECURSE);
246         vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
247
248         smbfs_hash_lock(smp, td);
249         LIST_FOREACH(np2, nhpp, n_hash) {
250                 if (np2->n_parent != dvp ||
251                     np2->n_nmlen != nmlen || bcmp(name, np2->n_name, nmlen) != 0)
252                         continue;
253                 vput(vp);
254 /*              smb_name_free(np->n_name);
255                 FREE(np, M_SMBNODE);*/
256                 goto loop;
257         }
258         LIST_INSERT_HEAD(nhpp, np, n_hash);
259         smbfs_hash_unlock(smp, td);
260         *vpp = vp;
261         return 0;
262 }
263
264 int
265 smbfs_nget(struct mount *mp, struct vnode *dvp, const char *name, int nmlen,
266            struct smbfattr *fap, struct vnode **vpp)
267 {
268         struct smbnode *np;
269         struct vnode *vp;
270         int error;
271
272         *vpp = NULL;
273         error = smbfs_node_alloc(mp, dvp, name, nmlen, fap, &vp);
274         if (error)
275                 return error;
276         np = VTOSMB(vp);
277         if (fap)
278                 smbfs_attr_cacheenter(vp, fap);
279         *vpp = vp;
280         return 0;
281 }
282
283 /*
284  * Free smbnode, and give vnode back to system
285  *
286  * smbfs_reclaim(struct vnode *a_vp, struct thread *a_td)
287  */
288 int
289 smbfs_reclaim(struct vop_reclaim_args *ap)
290 {
291         struct vnode *vp = ap->a_vp;
292         struct thread *td = ap->a_td;
293         struct vnode *dvp;
294         struct smbnode *np = VTOSMB(vp);
295         struct smbmount *smp = VTOSMBFS(vp);
296         lwkt_tokref vlock;
297         
298         SMBVDEBUG("%s,%d\n", np->n_name, vp->v_usecount);
299
300         smbfs_hash_lock(smp, td);
301
302         dvp = (np->n_parent && (np->n_flag & NREFPARENT)) ?
303             np->n_parent : NULL;
304
305         if (np->n_hash.le_prev)
306                 LIST_REMOVE(np, n_hash);
307         cache_purge(vp);
308         if (smp->sm_root == np) {
309                 SMBVDEBUG("root vnode\n");
310                 smp->sm_root = NULL;
311         }
312         vp->v_data = NULL;
313         smbfs_hash_unlock(smp, td);
314         if (np->n_name)
315                 smbfs_name_free(np->n_name);
316         FREE(np, M_SMBNODE);
317         if (dvp) {
318                 VI_LOCK(&vlock, dvp);
319                 if (dvp->v_usecount >= 1) {
320                         VI_UNLOCK(&vlock, dvp);
321                         vrele(dvp);
322                         /*
323                          * Indicate that we released something; see comment
324                          * in smbfs_unmount().
325                          */
326                         smp->sm_didrele = 1;
327                 } else {
328                         VI_UNLOCK(&vlock, dvp);
329                         SMBERROR("BUG: negative use count for parent!\n");
330                 }
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         KKASSERT(td->td_proc);
349         cred = td->td_proc->p_ucred;
350
351         SMBVDEBUG("%s: %d\n", VTOSMB(vp)->n_name, vp->v_usecount);
352         if (np->n_opencount) {
353                 error = smbfs_vinvalbuf(vp, V_SAVE, td, 1);
354                 smb_makescred(&scred, td, cred);
355                 error = smbfs_smb_close(np->n_mount->sm_share, np->n_fid, 
356                    &np->n_mtime, &scred);
357                 np->n_opencount = 0;
358         }
359         VOP_UNLOCK(vp, NULL, 0, td);
360         return (0);
361 }
362 /*
363  * routines to maintain vnode attributes cache
364  * smbfs_attr_cacheenter: unpack np.i to vattr structure
365  */
366 void
367 smbfs_attr_cacheenter(struct vnode *vp, struct smbfattr *fap)
368 {
369         struct smbnode *np = VTOSMB(vp);
370
371         if (vp->v_type == VREG) {
372                 if (np->n_size != fap->fa_size) {
373                         np->n_size = fap->fa_size;
374                         vnode_pager_setsize(vp, np->n_size);
375                 }
376         } else if (vp->v_type == VDIR) {
377                 np->n_size = 16384;             /* should be a better way ... */
378         } else
379                 return;
380         np->n_mtime = fap->fa_mtime;
381         np->n_dosattr = fap->fa_attr;
382         np->n_attrage = time_second;
383         return;
384 }
385
386 int
387 smbfs_attr_cachelookup(struct vnode *vp, struct vattr *va)
388 {
389         struct smbnode *np = VTOSMB(vp);
390         struct smbmount *smp = VTOSMBFS(vp);
391         int diff;
392
393         diff = time_second - np->n_attrage;
394         if (diff > 2)   /* XXX should be configurable */
395                 return ENOENT;
396         va->va_type = vp->v_type;               /* vnode type (for create) */
397         if (vp->v_type == VREG) {
398                 va->va_mode = smp->sm_args.file_mode;   /* files access mode and type */
399         } else if (vp->v_type == VDIR) {
400                 va->va_mode = smp->sm_args.dir_mode;    /* files access mode and type */
401         } else
402                 return EINVAL;
403         va->va_size = np->n_size;
404         va->va_nlink = 1;               /* number of references to file */
405         va->va_uid = smp->sm_args.uid;  /* owner user id */
406         va->va_gid = smp->sm_args.gid;  /* owner group id */
407         va->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
408         va->va_fileid = np->n_ino;      /* file id */
409         if (va->va_fileid == 0)
410                 va->va_fileid = 2;
411         va->va_blocksize = SSTOVC(smp->sm_share)->vc_txmax;
412         va->va_mtime = np->n_mtime;
413         va->va_atime = va->va_ctime = va->va_mtime;     /* time file changed */
414         va->va_gen = VNOVAL;            /* generation number of file */
415         va->va_flags = 0;               /* flags defined for file */
416         va->va_rdev = VNOVAL;           /* device the special file represents */
417         va->va_bytes = va->va_size;     /* bytes of disk space held by file */
418         va->va_filerev = 0;             /* file modification number */
419         va->va_vaflags = 0;             /* operations flags */
420         return 0;
421 }