VFS messaging/interfacing work stage 2/99. This stage retools the vnode ops
[dragonfly.git] / sys / vfs / nwfs / nwfs_vfsops.c
1 /*
2  * Copyright (c) 1999, 2000 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/nwfs/nwfs_vfsops.c,v 1.6.2.6 2001/10/25 19:18:54 dillon Exp $
33  * $DragonFly: src/sys/vfs/nwfs/nwfs_vfsops.c,v 1.11 2004/08/17 18:57:35 dillon Exp $
34  */
35 #include "opt_ncp.h"
36 #ifndef NCP
37 #error "NWFS requires NCP protocol"
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/buf.h>
50
51 #include <netproto/ncp/ncp.h>
52 #include <netproto/ncp/ncp_conn.h>
53 #include <netproto/ncp/ncp_subr.h>
54 #include <netproto/ncp/ncp_ncp.h>
55 #include <netproto/ncp/ncp_nls.h>
56
57 #include "nwfs.h"
58 #include "nwfs_node.h"
59 #include "nwfs_subr.h"
60
61 extern struct vnodeopv_entry_desc nwfs_vnodeop_entries[];
62
63 int nwfs_debuglevel = 0;
64
65 static int nwfs_version = NWFS_VERSION;
66
67 SYSCTL_DECL(_vfs_nwfs);
68 SYSCTL_NODE(_vfs, OID_AUTO, nwfs, CTLFLAG_RW, 0, "Netware file system");
69 SYSCTL_INT(_vfs_nwfs, OID_AUTO, version, CTLFLAG_RD, &nwfs_version, 0, "");
70 SYSCTL_INT(_vfs_nwfs, OID_AUTO, debuglevel, CTLFLAG_RW, &nwfs_debuglevel, 0, "");
71
72 static int nwfs_mount(struct mount *, char *, caddr_t,
73                         struct nameidata *, struct thread *);
74 static int nwfs_quotactl(struct mount *, int, uid_t, caddr_t, struct thread *);
75 static int nwfs_root(struct mount *, struct vnode **);
76 static int nwfs_start(struct mount *, int, struct thread *);
77 static int nwfs_statfs(struct mount *, struct statfs *, struct thread *);
78 static int nwfs_sync(struct mount *, int, struct thread *);
79 static int nwfs_unmount(struct mount *, int, struct thread *);
80 static int nwfs_init(struct vfsconf *vfsp);
81 static int nwfs_uninit(struct vfsconf *vfsp);
82
83 static struct vfsops nwfs_vfsops = {
84         nwfs_mount,
85         nwfs_start,
86         nwfs_unmount,
87         nwfs_root,
88         nwfs_quotactl,
89         nwfs_statfs,
90         nwfs_sync,
91         vfs_stdvget,
92         vfs_stdfhtovp,          /* shouldn't happen */
93         vfs_stdcheckexp,
94         vfs_stdvptofh,          /* shouldn't happen */
95         nwfs_init,
96         nwfs_uninit,
97         vfs_stdextattrctl,
98 };
99
100
101 VFS_SET(nwfs_vfsops, nwfs, VFCF_NETWORK);
102
103 int nwfs_pbuf_freecnt = -1;     /* start out unlimited */
104 static int nwfsid = 1;
105
106 static int
107 nwfs_initnls(struct nwmount *nmp) {
108         char    *pc, *pe;
109         int     error = 0;
110 #define COPY_TABLE(t,d) { \
111                 if (t) { \
112                         error = copyin((t), pc, 256); \
113                         if (error) break; \
114                 } else \
115                         bcopy(d, pc, 256); \
116                 (t) = pc; pc += 256; \
117         }
118
119         nmp->m.nls.opt |= NWHP_NLS | NWHP_DOS;
120         if ((nmp->m.flags & NWFS_MOUNT_HAVE_NLS) == 0) {
121                 nmp->m.nls.to_lower = ncp_defnls.to_lower;
122                 nmp->m.nls.to_upper = ncp_defnls.to_upper;
123                 nmp->m.nls.n2u = ncp_defnls.n2u;
124                 nmp->m.nls.u2n = ncp_defnls.u2n;
125                 return 0;
126         }
127         MALLOC(pe, char *, 256 * 4, M_NWFSDATA, M_WAITOK);
128         pc = pe;
129         do {
130                 COPY_TABLE(nmp->m.nls.to_lower, ncp_defnls.to_lower);
131                 COPY_TABLE(nmp->m.nls.to_upper, ncp_defnls.to_upper);
132                 COPY_TABLE(nmp->m.nls.n2u, ncp_defnls.n2u);
133                 COPY_TABLE(nmp->m.nls.u2n, ncp_defnls.u2n);
134         } while(0);
135         if (error) {
136                 free(pe, M_NWFSDATA);
137                 return error;
138         }
139         return 0;
140 }
141 /*
142  * mp - path - addr in user space of mount point (ie /usr or whatever)
143  * data - addr in user space of mount params 
144  */
145 static int
146 nwfs_mount(struct mount *mp, char *path, caddr_t data, 
147            struct nameidata *ndp, struct thread *td)
148 {
149         struct nwfs_args args;    /* will hold data from mount request */
150         size_t size;
151         int error;
152         struct nwmount *nmp = NULL;
153         struct ncp_conn *conn = NULL;
154         struct ncp_handle *handle = NULL;
155         struct vnode *vp;
156         char *pc,*pe;
157         struct ucred *cred;
158
159         KKASSERT(td->td_proc);
160         cred = td->td_proc->p_ucred;
161
162         if (data == NULL) {
163                 nwfs_printf("missing data argument\n");
164                 return 1;
165         }
166         if (mp->mnt_flag & MNT_UPDATE) {
167                 nwfs_printf("MNT_UPDATE not implemented");
168                 return (EOPNOTSUPP);
169         }
170         error = copyin(data, (caddr_t)&args, sizeof(struct nwfs_args));
171         if (error)
172                 return (error);
173         if (args.version != NWFS_VERSION) {
174                 nwfs_printf("mount version mismatch: kernel=%d, mount=%d\n",NWFS_VERSION,args.version);
175                 return (1);
176         }
177         error = ncp_conn_getbyref(args.connRef,td,cred,NCPM_EXECUTE,&conn);
178         if (error) {
179                 nwfs_printf("invalid connection refernce %d\n",args.connRef);
180                 return (error);
181         }
182         error = ncp_conn_gethandle(conn, NULL, &handle);
183         if (error) {
184                 nwfs_printf("can't get connection handle\n");
185                 return (error);
186         }
187         ncp_conn_unlock(conn,td);       /* we keep the ref */
188         mp->mnt_stat.f_iosize = conn->buffer_size;
189         /* We must malloc our own mount info */
190         MALLOC(nmp,struct nwmount *,sizeof(struct nwmount),M_NWFSDATA, M_WAITOK|M_USE_RESERVE|M_ZERO);
191         if (nmp == NULL) {
192                 nwfs_printf("could not alloc nwmount\n");
193                 error = ENOMEM;
194                 goto bad;
195         }
196         mp->mnt_data = (qaddr_t)nmp;
197         nmp->connh = handle;
198         nmp->n_root = NULL;
199         nmp->n_id = nwfsid++;
200         nmp->m = args;
201         nmp->m.file_mode = (nmp->m.file_mode &
202                             (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFREG;
203         nmp->m.dir_mode  = (nmp->m.dir_mode &
204                             (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFDIR;
205         if ((error = nwfs_initnls(nmp)) != 0) goto bad;
206         (void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
207         bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
208         pc = mp->mnt_stat.f_mntfromname;
209         pe = pc+sizeof(mp->mnt_stat.f_mntfromname);
210         bzero(pc, MNAMELEN);
211         *(pc++) = '/';
212         pc = index(strncpy(pc, conn->li.server, pe-pc-2),0);
213         if (pc < pe-1) {
214                 *(pc++) = ':';
215                 pc=index(strncpy(pc, conn->li.user, pe-pc-2),0);
216                 if (pc < pe-1) {
217                         *(pc++) = '/';
218                         strncpy(pc, nmp->m.mounted_vol, pe-pc-2);
219                 }
220         }
221         /* protect against invalid mount points */
222         nmp->m.mount_point[sizeof(nmp->m.mount_point)-1] = '\0';
223
224         vfs_add_vnodeops(&mp->mnt_vn_ops, nwfs_vnodeop_entries);
225
226         vfs_getnewfsid(mp);
227         error = nwfs_root(mp, &vp);
228         if (error)
229                 goto bad;
230         /*
231          * Lose the lock but keep the ref.
232          */
233         VOP_UNLOCK(vp, NULL, 0, curthread);
234         NCPVODEBUG("rootvp.vrefcnt=%d\n",vp->v_usecount);
235         return error;
236 bad:
237         if (nmp)
238                 free(nmp, M_NWFSDATA);
239         if (handle)
240                 ncp_conn_puthandle(handle, NULL, 0);
241         return error;
242 }
243
244 /* Unmount the filesystem described by mp. */
245 static int
246 nwfs_unmount(struct mount *mp, int mntflags, struct thread *td)
247 {
248         struct nwmount *nmp = VFSTONWFS(mp);
249         struct ncp_conn *conn;
250         int error, flags;
251         struct ucred *cred;
252
253         KKASSERT(td->td_proc);
254         cred = td->td_proc->p_ucred;
255
256         NCPVODEBUG("nwfs_unmount: flags=%04x\n",mntflags);
257         flags = 0;
258         if (mntflags & MNT_FORCE)
259                 flags |= FORCECLOSE;
260         /* There is 1 extra root vnode reference from nwfs_mount(). */
261         error = vflush(mp, 1, flags);
262         if (error)
263                 return (error);
264         conn = NWFSTOCONN(nmp);
265         ncp_conn_puthandle(nmp->connh,NULL,0);
266         if (ncp_conn_lock(conn,td,cred,NCPM_WRITE | NCPM_EXECUTE) == 0) {
267                 if(ncp_disconnect(conn))
268                         ncp_conn_unlock(conn,td);
269         }
270         mp->mnt_data = (qaddr_t)0;
271         if (nmp->m.flags & NWFS_MOUNT_HAVE_NLS)
272                 free(nmp->m.nls.to_lower, M_NWFSDATA);
273         free(nmp, M_NWFSDATA);
274         mp->mnt_flag &= ~MNT_LOCAL;
275         return (error);
276 }
277
278 /*  Return locked vnode to root of a filesystem */
279 static int
280 nwfs_root(struct mount *mp, struct vnode **vpp)
281 {
282         struct vnode *vp;
283         struct nwmount *nmp;
284         struct nwnode *np;
285         struct ncp_conn *conn;
286         struct nw_entry_info fattr;
287         struct thread *td = curthread;  /* XXX */
288         struct ucred *cred;
289         int error, nsf, opt;
290         u_char vol;
291
292         KKASSERT(td->td_proc);
293         cred = td->td_proc->p_ucred;
294
295         nmp = VFSTONWFS(mp);
296         conn = NWFSTOCONN(nmp);
297         if (nmp->n_root) {
298                 *vpp = NWTOV(nmp->n_root);
299                 while (vget(*vpp, NULL, LK_EXCLUSIVE, curthread) != 0) /* XXX */
300                         ;
301                 return 0;
302         }
303         error = ncp_lookup_volume(conn, nmp->m.mounted_vol, &vol, 
304                 &nmp->n_rootent.f_id, td, cred);
305         if (error)
306                 return ENOENT;
307         nmp->n_volume = vol;
308         error = ncp_get_namespaces(conn, vol, &nsf, td, cred);
309         if (error)
310                 return ENOENT;
311         if (nsf & NW_NSB_OS2) {
312                 NCPVODEBUG("volume %s has os2 namespace\n",nmp->m.mounted_vol);
313                 if ((nmp->m.flags & NWFS_MOUNT_NO_OS2) == 0) {
314                         nmp->name_space = NW_NS_OS2;
315                         nmp->m.nls.opt &= ~NWHP_DOS;
316                 }
317         }
318         opt = nmp->m.nls.opt;
319         nsf = opt & (NWHP_UPPER | NWHP_LOWER);
320         if (opt & NWHP_DOS) {
321                 if (nsf == (NWHP_UPPER | NWHP_LOWER)) {
322                         nmp->m.nls.opt &= ~(NWHP_LOWER | NWHP_UPPER);
323                 } else if (nsf == 0) {
324                         nmp->m.nls.opt |= NWHP_LOWER;
325                 }
326         } else {
327                 if (nsf == (NWHP_UPPER | NWHP_LOWER)) {
328                         nmp->m.nls.opt &= ~(NWHP_LOWER | NWHP_UPPER);
329                 }
330         }
331         if (nmp->m.root_path[0]) {
332                 nmp->m.root_path[0]--;
333                 error = ncp_obtain_info(nmp, nmp->n_rootent.f_id,
334                     -nmp->m.root_path[0], nmp->m.root_path, &fattr, td, cred);
335                 if (error) {
336                         NCPFATAL("Invalid root path specified\n");
337                         return ENOENT;
338                 }
339                 nmp->n_rootent.f_parent = fattr.dirEntNum;
340                 nmp->m.root_path[0]++;
341                 error = ncp_obtain_info(nmp, nmp->n_rootent.f_id,
342                     -nmp->m.root_path[0], nmp->m.root_path, &fattr, td, cred);
343                 if (error) {
344                         NCPFATAL("Invalid root path specified\n");
345                         return ENOENT;
346                 }
347                 nmp->n_rootent.f_id = fattr.dirEntNum;
348         } else {
349                 error = ncp_obtain_info(nmp, nmp->n_rootent.f_id,
350                     0, NULL, &fattr, td, cred);
351                 if (error) {
352                         NCPFATAL("Can't obtain volume info\n");
353                         return ENOENT;
354                 }
355                 fattr.nameLen = strlen(strcpy(fattr.entryName, NWFS_ROOTVOL));
356                 nmp->n_rootent.f_parent = nmp->n_rootent.f_id;
357         }
358         error = nwfs_nget(mp, nmp->n_rootent, &fattr, NULL, &vp);
359         if (error)
360                 return (error);
361         vp->v_flag |= VROOT;
362         np = VTONW(vp);
363         if (nmp->m.root_path[0] == 0)
364                 np->n_flag |= NVOLUME;
365         nmp->n_root = np;
366 /*      error = VOP_GETATTR(vp, &vattr, cred, td);
367         if (error) {
368                 vput(vp);
369                 NCPFATAL("Can't get root directory entry\n");
370                 return error;
371         }*/
372         *vpp = vp;
373         return (0);
374 }
375
376 /*
377  * Vfs start routine, a no-op.
378  */
379 /* ARGSUSED */
380 static int
381 nwfs_start(struct mount *mp, int flags, struct thread *td)
382 {
383         NCPVODEBUG("flags=%04x\n",flags);
384         return (0);
385 }
386
387 /*
388  * Do operations associated with quotas, not supported
389  */
390 /* ARGSUSED */
391 static int
392 nwfs_quotactl(struct mount *mp, int cmd, uid_t uid, caddr_t arg,
393               struct thread *td)
394 {
395         NCPVODEBUG("return EOPNOTSUPP\n");
396         return (EOPNOTSUPP);
397 }
398
399 /*ARGSUSED*/
400 int
401 nwfs_init(struct vfsconf *vfsp)
402 {
403 #ifndef SMP
404         int name[2];
405         int olen, ncpu, plen, error;
406
407         name[0] = CTL_HW;
408         name[1] = HW_NCPU;
409         error = kernel_sysctl(name, 2, &ncpu, &olen, NULL, 0, &plen);
410         if (error == 0 && ncpu > 1)
411                 printf("warning: nwfs module compiled without SMP support.");
412 #endif
413         nwfs_hash_init();
414         nwfs_pbuf_freecnt = nswbuf / 2 + 1;
415         NCPVODEBUG("always happy to load!\n");
416         return (0);
417 }
418
419 /*ARGSUSED*/
420 int
421 nwfs_uninit(struct vfsconf *vfsp)
422 {
423
424         nwfs_hash_free();
425         NCPVODEBUG("unloaded\n");
426         return (0);
427 }
428
429 /*
430  * nwfs_statfs call
431  */
432 int
433 nwfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
434 {
435         struct nwmount *nmp = VFSTONWFS(mp);
436         int error = 0, secsize;
437         struct nwnode *np = nmp->n_root;
438         struct ncp_volume_info vi;
439         struct ucred *cred;
440
441         KKASSERT(td->td_proc);
442         cred = td->td_proc->p_ucred;
443
444         if (np == NULL) return EINVAL;
445         error = ncp_get_volume_info_with_number(NWFSTOCONN(nmp), nmp->n_volume, &vi,td,cred);
446         if (error) return error;
447         secsize = 512;                  /* XXX how to get real value ??? */
448         sbp->f_spare2=0;                /* placeholder */
449         /* fundamental file system block size */
450         sbp->f_bsize = vi.sectors_per_block*secsize;
451         /* optimal transfer block size */
452         sbp->f_iosize = NWFSTOCONN(nmp)->buffer_size;
453         /* total data blocks in file system */
454         sbp->f_blocks= vi.total_blocks;
455         /* free blocks in fs */
456         sbp->f_bfree = vi.free_blocks + vi.purgeable_blocks;
457         /* free blocks avail to non-superuser */
458         sbp->f_bavail= vi.free_blocks+vi.purgeable_blocks;
459         /* total file nodes in file system */
460         sbp->f_files = vi.total_dir_entries;
461         /* free file nodes in fs */
462         sbp->f_ffree = vi.available_dir_entries;
463         sbp->f_flags = 0;               /* copy of mount exported flags */
464         if (sbp != &mp->mnt_stat) {
465                 sbp->f_fsid = mp->mnt_stat.f_fsid;      /* file system id */
466                 sbp->f_owner = mp->mnt_stat.f_owner;    /* user that mounted the filesystem */
467                 sbp->f_type = mp->mnt_vfc->vfc_typenum; /* type of filesystem */
468                 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
469                 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
470         }
471         strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN);
472         return 0;
473 }
474
475 /*
476  * Flush out the buffer cache
477  */
478 /* ARGSUSED */
479 static int
480 nwfs_sync(struct mount *mp, int waitfor, struct thread *td)
481 {
482         struct vnode *vp;
483         int error, allerror = 0;
484         /*
485          * Force stale buffer cache information to be flushed.
486          */
487 loop:
488         for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
489              vp != NULL;
490              vp = TAILQ_NEXT(vp, v_nmntvnodes)) {
491                 if (vp->v_flag & VPLACEMARKER)  /* ZZZ */
492                         continue;
493                 /*
494                  * If the vnode that we are about to sync is no longer
495                  * associated with this mount point, start over.
496                  */
497                 if (vp->v_mount != mp)
498                         goto loop;
499                 if (VOP_ISLOCKED(vp, NULL) || TAILQ_EMPTY(&vp->v_dirtyblkhd) ||
500                     waitfor == MNT_LAZY)
501                         continue;
502                 if (vget(vp, NULL, LK_EXCLUSIVE, td))
503                         goto loop;
504                 error = VOP_FSYNC(vp, waitfor, td);
505                 if (error)
506                         allerror = error;
507                 vput(vp);
508         }
509         return (allerror);
510 }