kernel: Make SMP support default (and non-optional).
[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 $
33 */
888b1d32
SW
34
35#ifndef KLD_MODULE
984263bc
MD
36#include "opt_ncp.h"
37#ifndef NCP
38#error "NWFS requires NCP protocol"
39#endif
888b1d32 40#endif
984263bc
MD
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/proc.h>
45#include <sys/kernel.h>
46#include <sys/sysctl.h>
47#include <sys/vnode.h>
48#include <sys/mount.h>
49#include <sys/stat.h>
50#include <sys/malloc.h>
51#include <sys/buf.h>
52
d2438d69
MD
53#include <netproto/ncp/ncp.h>
54#include <netproto/ncp/ncp_conn.h>
55#include <netproto/ncp/ncp_subr.h>
56#include <netproto/ncp/ncp_ncp.h>
57#include <netproto/ncp/ncp_nls.h>
984263bc 58
1f2de5d4
MD
59#include "nwfs.h"
60#include "nwfs_node.h"
61#include "nwfs_subr.h"
984263bc 62
66a1ddf5 63extern struct vop_ops nwfs_vnode_vops;
0961aa92 64
984263bc
MD
65int nwfs_debuglevel = 0;
66
67static int nwfs_version = NWFS_VERSION;
68
69SYSCTL_DECL(_vfs_nwfs);
70SYSCTL_NODE(_vfs, OID_AUTO, nwfs, CTLFLAG_RW, 0, "Netware file system");
71SYSCTL_INT(_vfs_nwfs, OID_AUTO, version, CTLFLAG_RD, &nwfs_version, 0, "");
72SYSCTL_INT(_vfs_nwfs, OID_AUTO, debuglevel, CTLFLAG_RW, &nwfs_debuglevel, 0, "");
73
acde96db 74static int nwfs_mount(struct mount *, char *, caddr_t, struct ucred *);
984263bc 75static int nwfs_root(struct mount *, struct vnode **);
acde96db 76static int nwfs_statfs(struct mount *, struct statfs *, struct ucred *);
87de5057 77static int nwfs_sync(struct mount *, int);
acde96db 78static int nwfs_unmount(struct mount *, int);
984263bc
MD
79static int nwfs_init(struct vfsconf *vfsp);
80static int nwfs_uninit(struct vfsconf *vfsp);
81
82static struct vfsops nwfs_vfsops = {
43c45e8f
HP
83 .vfs_mount = nwfs_mount,
84 .vfs_unmount = nwfs_unmount,
85 .vfs_root = nwfs_root,
86 .vfs_statfs = nwfs_statfs,
87 .vfs_sync = nwfs_sync,
88 .vfs_init = nwfs_init,
89 .vfs_uninit = nwfs_uninit
984263bc
MD
90};
91
92
93VFS_SET(nwfs_vfsops, nwfs, VFCF_NETWORK);
e5e63c20 94MODULE_VERSION(nwfs, 1);
888b1d32 95MODULE_DEPEND(nwfs, ncp, 1, 1, 1);
984263bc 96
74d62460 97long nwfs_pbuf_freecnt = -1; /* start out unlimited */
984263bc
MD
98static int nwfsid = 1;
99
100static int
101nwfs_initnls(struct nwmount *nmp) {
102 char *pc, *pe;
103 int error = 0;
104#define COPY_TABLE(t,d) { \
105 if (t) { \
106 error = copyin((t), pc, 256); \
107 if (error) break; \
108 } else \
109 bcopy(d, pc, 256); \
110 (t) = pc; pc += 256; \
111 }
112
113 nmp->m.nls.opt |= NWHP_NLS | NWHP_DOS;
114 if ((nmp->m.flags & NWFS_MOUNT_HAVE_NLS) == 0) {
115 nmp->m.nls.to_lower = ncp_defnls.to_lower;
116 nmp->m.nls.to_upper = ncp_defnls.to_upper;
117 nmp->m.nls.n2u = ncp_defnls.n2u;
118 nmp->m.nls.u2n = ncp_defnls.u2n;
119 return 0;
120 }
884717e1 121 pe = kmalloc(256 * 4, M_NWFSDATA, M_WAITOK);
984263bc
MD
122 pc = pe;
123 do {
124 COPY_TABLE(nmp->m.nls.to_lower, ncp_defnls.to_lower);
125 COPY_TABLE(nmp->m.nls.to_upper, ncp_defnls.to_upper);
126 COPY_TABLE(nmp->m.nls.n2u, ncp_defnls.n2u);
127 COPY_TABLE(nmp->m.nls.u2n, ncp_defnls.u2n);
128 } while(0);
129 if (error) {
efda3bd0 130 kfree(pe, M_NWFSDATA);
984263bc
MD
131 return error;
132 }
133 return 0;
134}
135/*
136 * mp - path - addr in user space of mount point (ie /usr or whatever)
137 * data - addr in user space of mount params
138 */
a9bb80e0 139static int
acde96db 140nwfs_mount(struct mount *mp, char *path, caddr_t data, struct ucred *cred)
984263bc
MD
141{
142 struct nwfs_args args; /* will hold data from mount request */
984263bc
MD
143 int error;
144 struct nwmount *nmp = NULL;
145 struct ncp_conn *conn = NULL;
146 struct ncp_handle *handle = NULL;
147 struct vnode *vp;
148 char *pc,*pe;
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 }
acde96db 165 error = ncp_conn_getbyref(args.connRef,curthread,cred,NCPM_EXECUTE,&conn);
984263bc 166 if (error) {
3f625015 167 nwfs_printf("invalid connection reference %d\n",args.connRef);
984263bc
MD
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 }
acde96db 175 ncp_conn_unlock(conn,curthread); /* we keep the ref */
984263bc
MD
176 mp->mnt_stat.f_iosize = conn->buffer_size;
177 /* We must malloc our own mount info */
884717e1
SW
178 nmp = kmalloc(sizeof(struct nwmount), M_NWFSDATA,
179 M_WAITOK | M_USE_RESERVE | M_ZERO);
984263bc
MD
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;
984263bc
MD
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';
0961aa92 205
66a1ddf5 206 vfs_add_vnodeops(mp, &nwfs_vnode_vops, &mp->mnt_vn_norm_ops);
0961aa92 207
984263bc
MD
208 vfs_getnewfsid(mp);
209 error = nwfs_root(mp, &vp);
210 if (error)
211 goto bad;
212 /*
213 * Lose the lock but keep the ref.
214 */
a11aaa81 215 vn_unlock(vp);
3c37c940 216 NCPVODEBUG("rootvp.vrefcnt=%d\n",vp->v_sysref.refcnt);
984263bc
MD
217 return error;
218bad:
219 if (nmp)
efda3bd0 220 kfree(nmp, M_NWFSDATA);
984263bc
MD
221 if (handle)
222 ncp_conn_puthandle(handle, NULL, 0);
223 return error;
224}
225
226/* Unmount the filesystem described by mp. */
227static int
acde96db 228nwfs_unmount(struct mount *mp, int mntflags)
984263bc
MD
229{
230 struct nwmount *nmp = VFSTONWFS(mp);
231 struct ncp_conn *conn;
232 int error, flags;
233
234 NCPVODEBUG("nwfs_unmount: flags=%04x\n",mntflags);
235 flags = 0;
236 if (mntflags & MNT_FORCE)
237 flags |= FORCECLOSE;
238 /* There is 1 extra root vnode reference from nwfs_mount(). */
239 error = vflush(mp, 1, flags);
240 if (error)
241 return (error);
242 conn = NWFSTOCONN(nmp);
243 ncp_conn_puthandle(nmp->connh,NULL,0);
acde96db 244 if (ncp_conn_lock(conn, curthread, proc0.p_ucred, NCPM_WRITE | NCPM_EXECUTE) == 0) {
984263bc 245 if(ncp_disconnect(conn))
acde96db 246 ncp_conn_unlock(conn, curthread);
984263bc
MD
247 }
248 mp->mnt_data = (qaddr_t)0;
249 if (nmp->m.flags & NWFS_MOUNT_HAVE_NLS)
efda3bd0
MD
250 kfree(nmp->m.nls.to_lower, M_NWFSDATA);
251 kfree(nmp, M_NWFSDATA);
984263bc
MD
252 mp->mnt_flag &= ~MNT_LOCAL;
253 return (error);
254}
255
256/* Return locked vnode to root of a filesystem */
257static int
a9bb80e0
CP
258nwfs_root(struct mount *mp, struct vnode **vpp)
259{
984263bc
MD
260 struct vnode *vp;
261 struct nwmount *nmp;
262 struct nwnode *np;
263 struct ncp_conn *conn;
264 struct nw_entry_info fattr;
dadab5e9
MD
265 struct thread *td = curthread; /* XXX */
266 struct ucred *cred;
984263bc
MD
267 int error, nsf, opt;
268 u_char vol;
269
dadab5e9
MD
270 KKASSERT(td->td_proc);
271 cred = td->td_proc->p_ucred;
272
984263bc
MD
273 nmp = VFSTONWFS(mp);
274 conn = NWFSTOCONN(nmp);
275 if (nmp->n_root) {
276 *vpp = NWTOV(nmp->n_root);
87de5057 277 while (vget(*vpp, LK_EXCLUSIVE) != 0) /* XXX */
984263bc
MD
278 ;
279 return 0;
280 }
281 error = ncp_lookup_volume(conn, nmp->m.mounted_vol, &vol,
dadab5e9 282 &nmp->n_rootent.f_id, td, cred);
984263bc
MD
283 if (error)
284 return ENOENT;
285 nmp->n_volume = vol;
dadab5e9 286 error = ncp_get_namespaces(conn, vol, &nsf, td, cred);
984263bc
MD
287 if (error)
288 return ENOENT;
289 if (nsf & NW_NSB_OS2) {
290 NCPVODEBUG("volume %s has os2 namespace\n",nmp->m.mounted_vol);
291 if ((nmp->m.flags & NWFS_MOUNT_NO_OS2) == 0) {
292 nmp->name_space = NW_NS_OS2;
293 nmp->m.nls.opt &= ~NWHP_DOS;
294 }
295 }
296 opt = nmp->m.nls.opt;
297 nsf = opt & (NWHP_UPPER | NWHP_LOWER);
298 if (opt & NWHP_DOS) {
299 if (nsf == (NWHP_UPPER | NWHP_LOWER)) {
300 nmp->m.nls.opt &= ~(NWHP_LOWER | NWHP_UPPER);
301 } else if (nsf == 0) {
302 nmp->m.nls.opt |= NWHP_LOWER;
303 }
304 } else {
305 if (nsf == (NWHP_UPPER | NWHP_LOWER)) {
306 nmp->m.nls.opt &= ~(NWHP_LOWER | NWHP_UPPER);
307 }
308 }
309 if (nmp->m.root_path[0]) {
310 nmp->m.root_path[0]--;
311 error = ncp_obtain_info(nmp, nmp->n_rootent.f_id,
dadab5e9 312 -nmp->m.root_path[0], nmp->m.root_path, &fattr, td, cred);
984263bc
MD
313 if (error) {
314 NCPFATAL("Invalid root path specified\n");
315 return ENOENT;
316 }
317 nmp->n_rootent.f_parent = fattr.dirEntNum;
318 nmp->m.root_path[0]++;
319 error = ncp_obtain_info(nmp, nmp->n_rootent.f_id,
dadab5e9 320 -nmp->m.root_path[0], nmp->m.root_path, &fattr, td, cred);
984263bc
MD
321 if (error) {
322 NCPFATAL("Invalid root path specified\n");
323 return ENOENT;
324 }
325 nmp->n_rootent.f_id = fattr.dirEntNum;
326 } else {
327 error = ncp_obtain_info(nmp, nmp->n_rootent.f_id,
dadab5e9 328 0, NULL, &fattr, td, cred);
984263bc
MD
329 if (error) {
330 NCPFATAL("Can't obtain volume info\n");
331 return ENOENT;
332 }
333 fattr.nameLen = strlen(strcpy(fattr.entryName, NWFS_ROOTVOL));
334 nmp->n_rootent.f_parent = nmp->n_rootent.f_id;
335 }
336 error = nwfs_nget(mp, nmp->n_rootent, &fattr, NULL, &vp);
337 if (error)
338 return (error);
2247fe02 339 vsetflags(vp, VROOT);
984263bc
MD
340 np = VTONW(vp);
341 if (nmp->m.root_path[0] == 0)
342 np->n_flag |= NVOLUME;
343 nmp->n_root = np;
87de5057 344/* error = VOP_GETATTR(vp, &vattr);
984263bc
MD
345 if (error) {
346 vput(vp);
347 NCPFATAL("Can't get root directory entry\n");
348 return error;
349 }*/
350 *vpp = vp;
351 return (0);
352}
353
984263bc
MD
354/*ARGSUSED*/
355int
356nwfs_init(struct vfsconf *vfsp)
357{
984263bc
MD
358 nwfs_hash_init();
359 nwfs_pbuf_freecnt = nswbuf / 2 + 1;
360 NCPVODEBUG("always happy to load!\n");
361 return (0);
362}
363
364/*ARGSUSED*/
365int
366nwfs_uninit(struct vfsconf *vfsp)
367{
368
369 nwfs_hash_free();
370 NCPVODEBUG("unloaded\n");
371 return (0);
372}
373
374/*
375 * nwfs_statfs call
376 */
377int
acde96db 378nwfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
984263bc
MD
379{
380 struct nwmount *nmp = VFSTONWFS(mp);
381 int error = 0, secsize;
382 struct nwnode *np = nmp->n_root;
383 struct ncp_volume_info vi;
384
385 if (np == NULL) return EINVAL;
acde96db
MD
386 error = ncp_get_volume_info_with_number(NWFSTOCONN(nmp), nmp->n_volume,
387 &vi, curthread, cred);
984263bc
MD
388 if (error) return error;
389 secsize = 512; /* XXX how to get real value ??? */
390 sbp->f_spare2=0; /* placeholder */
391 /* fundamental file system block size */
392 sbp->f_bsize = vi.sectors_per_block*secsize;
393 /* optimal transfer block size */
394 sbp->f_iosize = NWFSTOCONN(nmp)->buffer_size;
395 /* total data blocks in file system */
396 sbp->f_blocks= vi.total_blocks;
397 /* free blocks in fs */
398 sbp->f_bfree = vi.free_blocks + vi.purgeable_blocks;
399 /* free blocks avail to non-superuser */
400 sbp->f_bavail= vi.free_blocks+vi.purgeable_blocks;
401 /* total file nodes in file system */
402 sbp->f_files = vi.total_dir_entries;
403 /* free file nodes in fs */
404 sbp->f_ffree = vi.available_dir_entries;
405 sbp->f_flags = 0; /* copy of mount exported flags */
406 if (sbp != &mp->mnt_stat) {
407 sbp->f_fsid = mp->mnt_stat.f_fsid; /* file system id */
408 sbp->f_owner = mp->mnt_stat.f_owner; /* user that mounted the filesystem */
409 sbp->f_type = mp->mnt_vfc->vfc_typenum; /* type of filesystem */
984263bc
MD
410 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
411 }
412 strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN);
413 return 0;
414}
415
416/*
417 * Flush out the buffer cache
418 */
419/* ARGSUSED */
420static int
87de5057 421nwfs_sync(struct mount *mp, int waitfor)
984263bc
MD
422{
423 struct vnode *vp;
424 int error, allerror = 0;
425 /*
426 * Force stale buffer cache information to be flushed.
427 */
428loop:
429 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
430 vp != NULL;
431 vp = TAILQ_NEXT(vp, v_nmntvnodes)) {
432 /*
433 * If the vnode that we are about to sync is no longer
434 * associated with this mount point, start over.
435 */
436 if (vp->v_mount != mp)
437 goto loop;
a11aaa81 438 if (vn_islocked(vp) || RB_EMPTY(&vp->v_rbdirty_tree) ||
28271622 439 (waitfor & MNT_LAZY))
984263bc 440 continue;
87de5057 441 if (vget(vp, LK_EXCLUSIVE))
984263bc 442 goto loop;
5fd012e0 443 /* XXX vp may not be retained */
52174f71 444 error = VOP_FSYNC(vp, waitfor, 0);
984263bc
MD
445 if (error)
446 allerror = error;
447 vput(vp);
448 }
449 return (allerror);
450}