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