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: releng/5.2/sys/fs/nwfs/nwfs_vfsops.c 122552 2003-11-12 09:54:07Z phk $
33 */
34 #include "opt_ncp.h"
35 #ifndef NCP
36 #error "NWFS requires NCP protocol"
37 #endif
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/proc.h>
42 #include <sys/kernel.h>
43 #include <sys/sysctl.h>
44 #include <sys/vnode.h>
45 #include <sys/mount.h>
46 #include <sys/stat.h>
47 #include <sys/malloc.h>
48 #include <sys/bio.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 <fs/nwfs/nwfs.h>
58 #include <fs/nwfs/nwfs_node.h>
59 #include <fs/nwfs/nwfs_subr.h>
60
61 int nwfs_debuglevel = 0;
62
63 static int nwfs_version = NWFS_VERSION;
64
65 SYSCTL_DECL(_vfs_nwfs);
66 SYSCTL_NODE(_vfs, OID_AUTO, nwfs, CTLFLAG_RW, 0, "Netware filesystem");
67 SYSCTL_INT(_vfs_nwfs, OID_AUTO, version, CTLFLAG_RD, &nwfs_version, 0, "");
68 SYSCTL_INT(_vfs_nwfs, OID_AUTO, debuglevel, CTLFLAG_RW, &nwfs_debuglevel, 0, "");
69
70 MODULE_DEPEND(nwfs, ncp, 1, 1, 1);
71 MODULE_DEPEND(nwfs, libmchain, 1, 1, 1);
72
73 static vfs_mount_t nwfs_mount;
74 static vfs_quotactl_t nwfs_quotactl;
75 static vfs_root_t nwfs_root;
76 static vfs_start_t nwfs_start;
77 static vfs_statfs_t nwfs_statfs;
78 static vfs_unmount_t nwfs_unmount;
79 static vfs_init_t nwfs_init;
80 static vfs_uninit_t nwfs_uninit;
81
82 static struct vfsops nwfs_vfsops = {
83 .vfs_init = nwfs_init,
84 .vfs_mount = nwfs_mount,
85 .vfs_quotactl = nwfs_quotactl,
86 .vfs_root = nwfs_root,
87 .vfs_start = nwfs_start,
88 .vfs_statfs = nwfs_statfs,
89 .vfs_sync = vfs_stdsync,
90 .vfs_uninit = nwfs_uninit,
91 .vfs_unmount = nwfs_unmount,
92 };
93
94
95 VFS_SET(nwfs_vfsops, nwfs, VFCF_NETWORK);
96
97 int nwfs_pbuf_freecnt = -1; /* start out unlimited */
98 static int nwfsid = 1;
99
100 static int
101 nwfs_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 }
121 MALLOC(pe, char *, 256 * 4, M_NWFSDATA, M_WAITOK);
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) {
130 free(pe, M_NWFSDATA);
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 */
139 static int nwfs_mount(struct mount *mp, char *path, caddr_t data,
140 struct nameidata *ndp, struct thread *td)
141 {
142 struct nwfs_args args; /* will hold data from mount request */
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 }
165 error = ncp_conn_getbyref(args.connRef, td , td->td_ucred,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,
179 M_WAITOK | M_USE_RESERVE | M_ZERO);
180 if (nmp == NULL) {
181 nwfs_printf("could not alloc nwmount\n");
182 error = ENOMEM;
183 goto bad;
184 }
185 mp->mnt_data = (qaddr_t)nmp;
186 nmp->connh = handle;
187 nmp->n_root = NULL;
188 nmp->n_id = nwfsid++;
189 nmp->m = args;
190 nmp->m.file_mode = (nmp->m.file_mode &
191 (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFREG;
192 nmp->m.dir_mode = (nmp->m.dir_mode &
193 (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFDIR;
194 if ((error = nwfs_initnls(nmp)) != 0) goto bad;
195 pc = mp->mnt_stat.f_mntfromname;
196 pe = pc+sizeof(mp->mnt_stat.f_mntfromname);
197 bzero(pc, MNAMELEN);
198 *(pc++) = '/';
199 pc = index(strncpy(pc, conn->li.server, pe-pc-2),0);
200 if (pc < pe-1) {
201 *(pc++) = ':';
202 pc=index(strncpy(pc, conn->li.user, pe-pc-2),0);
203 if (pc < pe-1) {
204 *(pc++) = '/';
205 strncpy(pc, nmp->m.mounted_vol, pe-pc-2);
206 }
207 }
208 /* protect against invalid mount points */
209 nmp->m.mount_point[sizeof(nmp->m.mount_point)-1] = '\0';
210 vfs_getnewfsid(mp);
211 error = nwfs_root(mp, &vp);
212 if (error)
213 goto bad;
214 /*
215 * Lose the lock but keep the ref.
216 */
217 VOP_UNLOCK(vp, 0, curthread);
218 NCPVODEBUG("rootvp.vrefcnt=%d\n",vrefcnt(vp));
219 return error;
220 bad:
221 if (nmp)
222 free(nmp, M_NWFSDATA);
223 if (handle)
224 ncp_conn_puthandle(handle, NULL, 0);
225 return error;
226 }
227
228 /* Unmount the filesystem described by mp. */
229 static int
230 nwfs_unmount(struct mount *mp, int mntflags, struct thread *td)
231 {
232 struct nwmount *nmp = VFSTONWFS(mp);
233 struct ncp_conn *conn;
234 int error, flags;
235
236 NCPVODEBUG("nwfs_unmount: flags=%04x\n",mntflags);
237 flags = 0;
238 if (mntflags & MNT_FORCE)
239 flags |= FORCECLOSE;
240 /* There is 1 extra root vnode reference from nwfs_mount(). */
241 error = vflush(mp, 1, flags);
242 if (error)
243 return (error);
244 conn = NWFSTOCONN(nmp);
245 ncp_conn_puthandle(nmp->connh,NULL,0);
246 if (ncp_conn_lock(conn, td, td->td_ucred,NCPM_WRITE | NCPM_EXECUTE) == 0) {
247 if(ncp_conn_free(conn))
248 ncp_conn_unlock(conn, td);
249 }
250 mp->mnt_data = (qaddr_t)0;
251 if (nmp->m.flags & NWFS_MOUNT_HAVE_NLS)
252 free(nmp->m.nls.to_lower, M_NWFSDATA);
253 free(nmp, M_NWFSDATA);
254 mp->mnt_flag &= ~MNT_LOCAL;
255 return (error);
256 }
257
258 /* Return locked vnode to root of a filesystem */
259 static int
260 nwfs_root(struct mount *mp, struct vnode **vpp) {
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;
267 struct ucred *cred = td->td_ucred;
268 int error, nsf, opt;
269 u_char vol;
270
271 nmp = VFSTONWFS(mp);
272 conn = NWFSTOCONN(nmp);
273 if (nmp->n_root) {
274 *vpp = NWTOV(nmp->n_root);
275 while (vget(*vpp, LK_EXCLUSIVE, curthread) != 0)
276 ;
277 return 0;
278 }
279 error = ncp_lookup_volume(conn, nmp->m.mounted_vol, &vol,
280 &nmp->n_rootent.f_id, td, cred);
281 if (error)
282 return ENOENT;
283 nmp->n_volume = vol;
284 error = ncp_get_namespaces(conn, vol, &nsf, td, cred);
285 if (error)
286 return ENOENT;
287 if (nsf & NW_NSB_OS2) {
288 NCPVODEBUG("volume %s has os2 namespace\n",nmp->m.mounted_vol);
289 if ((nmp->m.flags & NWFS_MOUNT_NO_OS2) == 0) {
290 nmp->name_space = NW_NS_OS2;
291 nmp->m.nls.opt &= ~NWHP_DOS;
292 }
293 }
294 opt = nmp->m.nls.opt;
295 nsf = opt & (NWHP_UPPER | NWHP_LOWER);
296 if (opt & NWHP_DOS) {
297 if (nsf == (NWHP_UPPER | NWHP_LOWER)) {
298 nmp->m.nls.opt &= ~(NWHP_LOWER | NWHP_UPPER);
299 } else if (nsf == 0) {
300 nmp->m.nls.opt |= NWHP_LOWER;
301 }
302 } else {
303 if (nsf == (NWHP_UPPER | NWHP_LOWER)) {
304 nmp->m.nls.opt &= ~(NWHP_LOWER | NWHP_UPPER);
305 }
306 }
307 if (nmp->m.root_path[0]) {
308 nmp->m.root_path[0]--;
309 error = ncp_obtain_info(nmp, nmp->n_rootent.f_id,
310 -nmp->m.root_path[0], nmp->m.root_path, &fattr, td, cred);
311 if (error) {
312 NCPFATAL("Invalid root path specified\n");
313 return ENOENT;
314 }
315 nmp->n_rootent.f_parent = fattr.dirEntNum;
316 nmp->m.root_path[0]++;
317 error = ncp_obtain_info(nmp, nmp->n_rootent.f_id,
318 -nmp->m.root_path[0], nmp->m.root_path, &fattr, td, cred);
319 if (error) {
320 NCPFATAL("Invalid root path specified\n");
321 return ENOENT;
322 }
323 nmp->n_rootent.f_id = fattr.dirEntNum;
324 } else {
325 error = ncp_obtain_info(nmp, nmp->n_rootent.f_id,
326 0, NULL, &fattr, td, cred);
327 if (error) {
328 NCPFATAL("Can't obtain volume info\n");
329 return ENOENT;
330 }
331 fattr.nameLen = strlen(strcpy(fattr.entryName, "#.ROOT"));
332 nmp->n_rootent.f_parent = nmp->n_rootent.f_id;
333 }
334 error = nwfs_nget(mp, nmp->n_rootent, &fattr, NULL, &vp);
335 if (error)
336 return (error);
337 vp->v_vflag |= VV_ROOT;
338 np = VTONW(vp);
339 if (nmp->m.root_path[0] == 0)
340 np->n_flag |= NVOLUME;
341 nmp->n_root = np;
342 /* error = VOP_GETATTR(vp, &vattr, cred, td);
343 if (error) {
344 vput(vp);
345 NCPFATAL("Can't get root directory entry\n");
346 return error;
347 }*/
348 *vpp = vp;
349 return (0);
350 }
351
352 /*
353 * Vfs start routine, a no-op.
354 */
355 /* ARGSUSED */
356 static int
357 nwfs_start(mp, flags, td)
358 struct mount *mp;
359 int flags;
360 struct thread *td;
361 {
362 NCPVODEBUG("flags=%04x\n",flags);
363 return (0);
364 }
365
366 /*
367 * Do operations associated with quotas, not supported
368 */
369 /* ARGSUSED */
370 static int
371 nwfs_quotactl(mp, cmd, uid, arg, td)
372 struct mount *mp;
373 int cmd;
374 uid_t uid;
375 caddr_t arg;
376 struct thread *td;
377 {
378 NCPVODEBUG("return EOPNOTSUPP\n");
379 return (EOPNOTSUPP);
380 }
381
382 /*ARGSUSED*/
383 int
384 nwfs_init(struct vfsconf *vfsp)
385 {
386 #ifndef SMP
387 int name[2];
388 int olen, ncpu, plen, error;
389
390 name[0] = CTL_HW;
391 name[1] = HW_NCPU;
392 error = kernel_sysctl(curthread, name, 2, &ncpu, &olen, NULL, 0, &plen);
393 if (error == 0 && ncpu > 1)
394 printf("warning: nwfs module compiled without SMP support.");
395 #endif
396 nwfs_hash_init();
397 nwfs_pbuf_freecnt = nswbuf / 2 + 1;
398 NCPVODEBUG("always happy to load!\n");
399 return (0);
400 }
401
402 /*ARGSUSED*/
403 int
404 nwfs_uninit(struct vfsconf *vfsp)
405 {
406
407 nwfs_hash_free();
408 NCPVODEBUG("unloaded\n");
409 return (0);
410 }
411
412 /*
413 * nwfs_statfs call
414 */
415 int
416 nwfs_statfs(mp, sbp, td)
417 struct mount *mp;
418 struct statfs *sbp;
419 struct thread *td;
420 {
421 struct nwmount *nmp = VFSTONWFS(mp);
422 int error = 0, secsize;
423 struct nwnode *np = nmp->n_root;
424 struct ncp_volume_info vi;
425
426 if (np == NULL) return EINVAL;
427 error = ncp_get_volume_info_with_number(NWFSTOCONN(nmp),
428 nmp->n_volume, &vi, td, td->td_ucred);
429 if (error) return error;
430 secsize = 512; /* XXX how to get real value ??? */
431 /* fundamental filesystem block size */
432 sbp->f_bsize = vi.sectors_per_block*secsize;
433 /* optimal transfer block size */
434 sbp->f_iosize = NWFSTOCONN(nmp)->buffer_size;
435 /* total data blocks in filesystem */
436 sbp->f_blocks= vi.total_blocks;
437 /* free blocks in fs */
438 sbp->f_bfree = vi.free_blocks + vi.purgeable_blocks;
439 /* free blocks avail to non-superuser */
440 sbp->f_bavail= vi.free_blocks+vi.purgeable_blocks;
441 /* total file nodes in filesystem */
442 sbp->f_files = vi.total_dir_entries;
443 /* free file nodes in fs */
444 sbp->f_ffree = vi.available_dir_entries;
445 sbp->f_flags = 0; /* copy of mount exported flags */
446 if (sbp != &mp->mnt_stat) {
447 sbp->f_fsid = mp->mnt_stat.f_fsid; /* filesystem id */
448 sbp->f_owner = mp->mnt_stat.f_owner; /* user that mounted the filesystem */
449 sbp->f_type = mp->mnt_vfc->vfc_typenum; /* type of filesystem */
450 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
451 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
452 }
453 strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN);
454 return 0;
455 }
Cache object: b06d4559c7641a2a163dc061e3c05546
|