Merge from vendor branch OPENSSL:
[dragonfly.git] / sys / vfs / smbfs / smbfs_vfsops.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_vfsops.c,v 1.2.2.5 2003/01/17 08:20:26 tjr Exp $
33  * $DragonFly: src/sys/vfs/smbfs/smbfs_vfsops.c,v 1.13 2004/08/17 18:57:35 dillon Exp $
34  */
35 #include "opt_netsmb.h"
36 #ifndef NETSMB
37 #error "SMBFS requires option NETSMB"
38 #endif
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/proc.h>
43 #include <sys/kernel.h>
44 #include <sys/sysctl.h>
45 #include <sys/vnode.h>
46 #include <sys/mount.h>
47 #include <sys/stat.h>
48 #include <sys/malloc.h>
49 #include <sys/module.h>
50
51
52 #include <netproto/smb/smb.h>
53 #include <netproto/smb/smb_conn.h>
54 #include <netproto/smb/smb_subr.h>
55 #include <netproto/smb/smb_dev.h>
56
57 #include "smbfs.h"
58 #include "smbfs_node.h"
59 #include "smbfs_subr.h"
60
61 #include <sys/buf.h>
62
63 extern struct vnodeopv_entry_desc smbfs_vnodeop_entries[];
64
65 int smbfs_debuglevel = 0;
66
67 static int smbfs_version = SMBFS_VERSION;
68
69 #ifdef SMBFS_USEZONE
70 #include <vm/vm.h>
71 #include <vm/vm_extern.h>
72 #include <vm/vm_zone.h>
73
74 vm_zone_t smbfsmount_zone;
75 #endif
76
77 SYSCTL_NODE(_vfs, OID_AUTO, smbfs, CTLFLAG_RW, 0, "SMB/CIFS file system");
78 SYSCTL_INT(_vfs_smbfs, OID_AUTO, version, CTLFLAG_RD, &smbfs_version, 0, "");
79 SYSCTL_INT(_vfs_smbfs, OID_AUTO, debuglevel, CTLFLAG_RW, &smbfs_debuglevel, 0, "");
80
81 static MALLOC_DEFINE(M_SMBFSHASH, "SMBFS hash", "SMBFS hash table");
82
83
84 static int smbfs_mount(struct mount *, char *, caddr_t,
85                         struct nameidata *, struct thread *);
86 static int smbfs_quotactl(struct mount *, int, uid_t, caddr_t, struct thread *);
87 static int smbfs_root(struct mount *, struct vnode **);
88 static int smbfs_start(struct mount *, int, struct thread *);
89 static int smbfs_statfs(struct mount *, struct statfs *, struct thread *);
90 static int smbfs_sync(struct mount *, int, struct thread *);
91 static int smbfs_unmount(struct mount *, int, struct thread *);
92 static int smbfs_init(struct vfsconf *vfsp);
93 static int smbfs_uninit(struct vfsconf *vfsp);
94
95 #if defined(__FreeBSD__) && __FreeBSD_version < 400009
96 static int smbfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp);
97 static int smbfs_fhtovp(struct mount *, struct fid *,
98                         struct sockaddr *, struct vnode **, int *,
99                         struct ucred **);
100 static int smbfs_vptofh(struct vnode *, struct fid *);
101 #endif
102
103 static struct vfsops smbfs_vfsops = {
104         smbfs_mount,
105         smbfs_start,
106         smbfs_unmount,
107         smbfs_root,
108         smbfs_quotactl,
109         smbfs_statfs,
110         smbfs_sync,
111 #if defined(__DragonFly__) || __FreeBSD_version > 400008
112         vfs_stdvget,
113         vfs_stdfhtovp,          /* shouldn't happen */
114         vfs_stdcheckexp,
115         vfs_stdvptofh,          /* shouldn't happen */
116 #else
117         smbfs_vget,
118         smbfs_fhtovp,
119         smbfs_vptofh,
120 #endif
121         smbfs_init,
122         smbfs_uninit,
123         vfs_stdextattrctl
124 };
125
126
127 VFS_SET(smbfs_vfsops, smbfs, VFCF_NETWORK);
128
129 MODULE_DEPEND(smbfs, netsmb, NSMB_VERSION, NSMB_VERSION, NSMB_VERSION);
130 MODULE_DEPEND(smbfs, libiconv, 1, 1, 1);
131 MODULE_DEPEND(smbfs, libmchain, 1, 1, 1);
132
133 int smbfs_pbuf_freecnt = -1;    /* start out unlimited */
134
135 static int
136 smbfs_mount(struct mount *mp, char *path, caddr_t data, 
137             struct nameidata *ndp, struct thread *td)
138 {
139         struct smbfs_args args;           /* will hold data from mount request */
140         struct smbmount *smp = NULL;
141         struct smb_vc *vcp;
142         struct smb_share *ssp = NULL;
143         struct vnode *vp;
144         struct smb_cred scred;
145         struct ucred *cred;
146         size_t size;
147         int error;
148         char *pc, *pe;
149
150         KKASSERT(td->td_proc);
151         cred = td->td_proc->p_ucred;
152
153         if (data == NULL) {
154                 printf("missing data argument\n");
155                 return EINVAL;
156         }
157         if (mp->mnt_flag & MNT_UPDATE) {
158                 printf("MNT_UPDATE not implemented");
159                 return EOPNOTSUPP;
160         }
161         error = copyin(data, (caddr_t)&args, sizeof(struct smbfs_args));
162         if (error)
163                 return error;
164         if (args.version != SMBFS_VERSION) {
165                 printf("mount version mismatch: kernel=%d, mount=%d\n",
166                     SMBFS_VERSION, args.version);
167                 return EINVAL;
168         }
169         smb_makescred(&scred, td, cred);
170         error = smb_dev2share(args.dev, SMBM_EXEC, &scred, &ssp);
171         if (error) {
172                 printf("invalid device handle %d (%d)\n", args.dev, error);
173                 return error;
174         }
175         vcp = SSTOVC(ssp);
176         smb_share_unlock(ssp, 0, td);
177         mp->mnt_stat.f_iosize = SSTOVC(ssp)->vc_txmax;
178
179 #ifdef SMBFS_USEZONE
180         smp = zalloc(smbfsmount_zone);
181 #else
182         MALLOC(smp, struct smbmount*, sizeof(*smp), M_SMBFSDATA, M_WAITOK|M_USE_RESERVE);
183 #endif
184         if (smp == NULL) {
185                 printf("could not alloc smbmount\n");
186                 error = ENOMEM;
187                 goto bad;
188         }
189         bzero(smp, sizeof(*smp));
190         mp->mnt_data = (qaddr_t)smp;
191         smp->sm_hash = hashinit(desiredvnodes, M_SMBFSHASH, &smp->sm_hashlen);
192         if (smp->sm_hash == NULL)
193                 goto bad;
194         lockinit(&smp->sm_hashlock, 0, "smbfsh", 0, 0);
195         smp->sm_share = ssp;
196         smp->sm_root = NULL;
197         smp->sm_args = args;
198         smp->sm_caseopt = args.caseopt;
199         smp->sm_args.file_mode = (smp->sm_args.file_mode &
200                             (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFREG;
201         smp->sm_args.dir_mode  = (smp->sm_args.dir_mode &
202                             (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFDIR;
203
204 /*      simple_lock_init(&smp->sm_npslock);*/
205         error = copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
206         if (error)
207                 goto bad;
208         bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
209         pc = mp->mnt_stat.f_mntfromname;
210         pe = pc + sizeof(mp->mnt_stat.f_mntfromname);
211         bzero(pc, MNAMELEN);
212         *pc++ = '/';
213         *pc++ = '/';
214         pc=index(strncpy(pc, vcp->vc_username, pe - pc - 2), 0);
215         if (pc < pe-1) {
216                 *(pc++) = '@';
217                 pc = index(strncpy(pc, vcp->vc_srvname, pe - pc - 2), 0);
218                 if (pc < pe - 1) {
219                         *(pc++) = '/';
220                         strncpy(pc, ssp->ss_name, pe - pc - 2);
221                 }
222         }
223         /* protect against invalid mount points */
224         smp->sm_args.mount_point[sizeof(smp->sm_args.mount_point) - 1] = '\0';
225         vfs_getnewfsid(mp);
226
227         vfs_add_vnodeops(&mp->mnt_vn_ops, smbfs_vnodeop_entries);
228
229         error = smbfs_root(mp, &vp);
230         if (error)
231                 goto bad;
232         VOP_UNLOCK(vp, NULL, 0, td);
233         SMBVDEBUG("root.v_usecount = %d\n", vp->v_usecount);
234
235 #ifdef DIAGNOSTICS
236         SMBERROR("mp=%p\n", mp);
237 #endif
238         return error;
239 bad:
240         if (smp) {
241                 if (smp->sm_hash)
242                         free(smp->sm_hash, M_SMBFSHASH);
243                 lockdestroy(&smp->sm_hashlock);
244 #ifdef SMBFS_USEZONE
245                 zfree(smbfsmount_zone, smp);
246 #else
247                 free(smp, M_SMBFSDATA);
248 #endif
249         }
250         if (ssp)
251                 smb_share_put(ssp, &scred);
252         return error;
253 }
254
255 /* Unmount the filesystem described by mp. */
256 static int
257 smbfs_unmount(struct mount *mp, int mntflags, struct thread *td)
258 {
259         struct smbmount *smp = VFSTOSMBFS(mp);
260         struct smb_cred scred;
261         struct ucred *cred;
262         int error, flags;
263
264         KKASSERT(td->td_proc);
265         cred = td->td_proc->p_ucred;
266
267         SMBVDEBUG("smbfs_unmount: flags=%04x\n", mntflags);
268         flags = 0;
269         if (mntflags & MNT_FORCE)
270                 flags |= FORCECLOSE;
271         /*
272          * Keep trying to flush the vnode list for the mount while 
273          * some are still busy and we are making progress towards
274          * making them not busy. This is needed because smbfs vnodes
275          * reference their parent directory but may appear after their
276          * parent in the list; one pass over the vnode list is not
277          * sufficient in this case.
278          */
279         do {
280                 smp->sm_didrele = 0;
281                 /* There is 1 extra root vnode reference from smbfs_mount(). */
282                 error = vflush(mp, 1, flags);
283         } while (error == EBUSY && smp->sm_didrele != 0);
284         if (error)
285                 return error;
286         smb_makescred(&scred, td, cred);
287         smb_share_put(smp->sm_share, &scred);
288         mp->mnt_data = (qaddr_t)0;
289
290         if (smp->sm_hash)
291                 free(smp->sm_hash, M_SMBFSHASH);
292         lockdestroy(&smp->sm_hashlock);
293 #ifdef SMBFS_USEZONE
294         zfree(smbfsmount_zone, smp);
295 #else
296         free(smp, M_SMBFSDATA);
297 #endif
298         mp->mnt_flag &= ~MNT_LOCAL;
299         return error;
300 }
301
302 /* 
303  * Return locked root vnode of a filesystem
304  */
305 static int
306 smbfs_root(struct mount *mp, struct vnode **vpp)
307 {
308         struct thread *td = curthread;  /* XXX */
309         struct smbmount *smp = VFSTOSMBFS(mp);
310         struct vnode *vp;
311         struct smbnode *np;
312         struct smbfattr fattr;
313         struct ucred *cred;
314         struct smb_cred scred;
315         int error;
316
317         KKASSERT(td->td_proc);
318         cred = td->td_proc->p_ucred;
319
320         if (smp == NULL) {
321                 SMBERROR("smp == NULL (bug in umount)\n");
322                 return EINVAL;
323         }
324         if (smp->sm_root) {
325                 *vpp = SMBTOV(smp->sm_root);
326                 return vget(*vpp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
327         }
328         smb_makescred(&scred, td, cred);
329         error = smbfs_smb_lookup(NULL, NULL, 0, &fattr, &scred);
330         if (error)
331                 return error;
332         error = smbfs_nget(mp, NULL, "TheRooT", 7, &fattr, &vp);
333         if (error)
334                 return error;
335         vp->v_flag |= VROOT;
336         np = VTOSMB(vp);
337         smp->sm_root = np;
338         *vpp = vp;
339         return 0;
340 }
341
342 /*
343  * Vfs start routine, a no-op.
344  */
345 /* ARGSUSED */
346 static int
347 smbfs_start(struct mount *mp, int flags, struct thread *td)
348 {
349         SMBVDEBUG("flags=%04x\n", flags);
350         return 0;
351 }
352
353 /*
354  * Do operations associated with quotas, not supported
355  */
356 /* ARGSUSED */
357 static int
358 smbfs_quotactl(struct mount *mp, int cmd, uid_t uid, caddr_t arg,
359                 struct thread *td)
360 {
361         SMBVDEBUG("return EOPNOTSUPP\n");
362         return EOPNOTSUPP;
363 }
364
365 /*ARGSUSED*/
366 int
367 smbfs_init(struct vfsconf *vfsp)
368 {
369 #ifdef SMBFS_USEZONE
370         smbfsmount_zone = zinit("SMBFSMOUNT", sizeof(struct smbmount), 0, 0, 1);
371 #endif
372         smbfs_pbuf_freecnt = nswbuf / 2 + 1;
373         SMBVDEBUG("done.\n");
374         return 0;
375 }
376
377 /*ARGSUSED*/
378 int
379 smbfs_uninit(struct vfsconf *vfsp)
380 {
381         SMBVDEBUG("done.\n");
382         return 0;
383 }
384
385 /*
386  * smbfs_statfs call
387  */
388 int
389 smbfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
390 {
391         struct smbmount *smp = VFSTOSMBFS(mp);
392         struct smbnode *np = smp->sm_root;
393         struct smb_share *ssp = smp->sm_share;
394         struct smb_cred scred;
395         struct ucred *cred;
396         int error = 0;
397
398         KKASSERT(td->td_proc);
399         cred = td->td_proc->p_ucred;
400
401         if (np == NULL)
402                 return EINVAL;
403         
404         sbp->f_iosize = SSTOVC(ssp)->vc_txmax;          /* optimal transfer block size */
405         sbp->f_spare2 = 0;                      /* placeholder */
406         smb_makescred(&scred, td, cred);
407
408         if (SMB_DIALECT(SSTOVC(ssp)) >= SMB_DIALECT_LANMAN2_0)
409                 error = smbfs_smb_statfs2(ssp, sbp, &scred);
410         else
411                 error = smbfs_smb_statfs(ssp, sbp, &scred);
412         if (error)
413                 return error;
414         sbp->f_flags = 0;               /* copy of mount exported flags */
415         if (sbp != &mp->mnt_stat) {
416                 sbp->f_fsid = mp->mnt_stat.f_fsid;      /* file system id */
417                 sbp->f_owner = mp->mnt_stat.f_owner;    /* user that mounted the filesystem */
418                 sbp->f_type = mp->mnt_vfc->vfc_typenum; /* type of filesystem */
419                 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
420                 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
421         }
422         strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN);
423         return 0;
424 }
425
426 /*
427  * Flush out the buffer cache
428  */
429 /* ARGSUSED */
430 static int
431 smbfs_sync(struct mount *mp, int waitfor, struct thread *td)
432 {
433         struct vnode *vp;
434         int error, allerror = 0;
435         /*
436          * Force stale buffer cache information to be flushed.
437          */
438 loop:
439         for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
440              vp != NULL;
441              vp = TAILQ_NEXT(vp, v_nmntvnodes)) {
442                 if (vp->v_flag & VPLACEMARKER)  /* ZZZ */
443                         continue;
444                 /*
445                  * If the vnode that we are about to sync is no longer
446                  * associated with this mount point, start over.
447                  */
448                 if (vp->v_mount != mp)
449                         goto loop;
450                 if (VOP_ISLOCKED(vp, NULL) || TAILQ_EMPTY(&vp->v_dirtyblkhd) ||
451                     waitfor == MNT_LAZY)
452                         continue;
453                 if (vget(vp, NULL, LK_EXCLUSIVE, td))
454                         goto loop;
455                 error = VOP_FSYNC(vp, waitfor, td);
456                 if (error)
457                         allerror = error;
458                 vput(vp);
459         }
460         return (allerror);
461 }
462
463 #if defined(__FreeBSD__) && __FreeBSD_version < 400009
464 /*
465  * smbfs flat namespace lookup. Unsupported.
466  */
467 /* ARGSUSED */
468 static int smbfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
469 {
470         return (EOPNOTSUPP);
471 }
472
473 /* ARGSUSED */
474 static int smbfs_fhtovp(struct mount *mp, struct fid *fhp,
475                         struct sockaddr *nam, struct vnode **vpp,
476                         int *exflagsp, struct ucred **credanonp)
477 {
478         return (EINVAL);
479 }
480
481 /*
482  * Vnode pointer to File handle, should never happen either
483  */
484 /* ARGSUSED */
485 static int
486 smbfs_vptofh(struct vnode *vp, struct fid *fhp)
487 {
488         return (EINVAL);
489 }
490
491 #endif /* __FreeBSD_version < 400009 */