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