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