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