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