]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/fs/nwfs/nwfs_vfsops.c
This commit was generated by cvs2svn to compensate for changes in r147894,
[FreeBSD/FreeBSD.git] / sys / fs / 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$
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_cmount_t     nwfs_cmount;
74 static vfs_mount_t      nwfs_mount;
75 static vfs_quotactl_t   nwfs_quotactl;
76 static vfs_root_t       nwfs_root;
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_cmount =           nwfs_cmount,
86         .vfs_quotactl =         nwfs_quotactl,
87         .vfs_root =             nwfs_root,
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 static int nwfs_cmount(struct mntarg *ma, void *data, int flags,
137                       struct thread *td)
138 {
139         struct nwfs_args args;    /* will hold data from mount request */
140         int error;
141
142         error = copyin(data, (caddr_t)&args, sizeof(struct nwfs_args));
143         if (error)
144                 return (error);
145
146         /*
147          * XXX: cheap cop-out here, args contains a structure I don't
148          * XXX: know how we should handle, and I don't see any immediate
149          * XXX: prospect of avoiding a mount_nwfs(8) binary anyway.
150          */
151         ma = mount_arg(ma, "nwfs_args", &args, sizeof args);
152
153         error = kernel_mount(ma, flags);
154
155         return (error);
156 }
157
158 /*
159  * mp - path - addr in user space of mount point (ie /usr or whatever)
160  * data - addr in user space of mount params 
161  */
162 static int nwfs_mount(struct mount *mp, struct thread *td)
163 {
164         struct nwfs_args args;    /* will hold data from mount request */
165         int error;
166         struct nwmount *nmp = NULL;
167         struct ncp_conn *conn = NULL;
168         struct ncp_handle *handle = NULL;
169         struct vnode *vp;
170         char *pc,*pe;
171
172         if (mp->mnt_flag & MNT_ROOTFS)
173                 return (EOPNOTSUPP);
174         if (mp->mnt_flag & MNT_UPDATE) {
175                 nwfs_printf("MNT_UPDATE not implemented");
176                 return (EOPNOTSUPP);
177         }
178         error = vfs_copyopt(mp->mnt_optnew, "nwfw_args", &args, sizeof args);
179         if (error)
180                 return (error);
181         if (args.version != NWFS_VERSION) {
182                 nwfs_printf("mount version mismatch: kernel=%d, mount=%d\n",NWFS_VERSION,args.version);
183                 return (1);
184         }
185         error = ncp_conn_getbyref(args.connRef, td , td->td_ucred,NCPM_EXECUTE,&conn);
186         if (error) {
187                 nwfs_printf("invalid connection refernce %d\n",args.connRef);
188                 return (error);
189         }
190         error = ncp_conn_gethandle(conn, NULL, &handle);
191         if (error) {
192                 nwfs_printf("can't get connection handle\n");
193                 return (error);
194         }
195         ncp_conn_unlock(conn, td);      /* we keep the ref */
196         mp->mnt_stat.f_iosize = conn->buffer_size;
197         /* We must malloc our own mount info */
198         MALLOC(nmp,struct nwmount *,sizeof(struct nwmount),M_NWFSDATA,
199             M_WAITOK | M_USE_RESERVE | M_ZERO);
200         if (nmp == NULL) {
201                 nwfs_printf("could not alloc nwmount\n");
202                 error = ENOMEM;
203                 goto bad;
204         }
205         mp->mnt_data = (qaddr_t)nmp;
206         nmp->connh = handle;
207         nmp->n_root = NULL;
208         nmp->n_id = nwfsid++;
209         nmp->m = args;
210         nmp->m.file_mode = (nmp->m.file_mode &
211                             (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFREG;
212         nmp->m.dir_mode  = (nmp->m.dir_mode &
213                             (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFDIR;
214         if ((error = nwfs_initnls(nmp)) != 0) goto bad;
215         pc = mp->mnt_stat.f_mntfromname;
216         pe = pc+sizeof(mp->mnt_stat.f_mntfromname);
217         bzero(pc, MNAMELEN);
218         *(pc++) = '/';
219         pc = index(strncpy(pc, conn->li.server, pe-pc-2),0);
220         if (pc < pe-1) {
221                 *(pc++) = ':';
222                 pc=index(strncpy(pc, conn->li.user, pe-pc-2),0);
223                 if (pc < pe-1) {
224                         *(pc++) = '/';
225                         strncpy(pc, nmp->m.mounted_vol, pe-pc-2);
226                 }
227         }
228         /* protect against invalid mount points */
229         nmp->m.mount_point[sizeof(nmp->m.mount_point)-1] = '\0';
230         vfs_getnewfsid(mp);
231         error = nwfs_root(mp, LK_EXCLUSIVE, &vp, td);
232         if (error)
233                 goto bad;
234         /*
235          * Lose the lock but keep the ref.
236          */
237         VOP_UNLOCK(vp, 0, curthread);
238         NCPVODEBUG("rootvp.vrefcnt=%d\n",vrefcnt(vp));
239         return error;
240 bad:
241         if (nmp)
242                 free(nmp, M_NWFSDATA);
243         if (handle)
244                 ncp_conn_puthandle(handle, NULL, 0);
245         return error;
246 }
247
248 /* Unmount the filesystem described by mp. */
249 static int
250 nwfs_unmount(struct mount *mp, int mntflags, struct thread *td)
251 {
252         struct nwmount *nmp = VFSTONWFS(mp);
253         struct ncp_conn *conn;
254         int error, flags;
255
256         NCPVODEBUG("nwfs_unmount: flags=%04x\n",mntflags);
257         flags = 0;
258         if (mntflags & MNT_FORCE)
259                 flags |= FORCECLOSE;
260         /* There is 1 extra root vnode reference from nwfs_mount(). */
261         error = vflush(mp, 1, flags, td);
262         if (error)
263                 return (error);
264         conn = NWFSTOCONN(nmp);
265         ncp_conn_puthandle(nmp->connh,NULL,0);
266         if (ncp_conn_lock(conn, td, td->td_ucred,NCPM_WRITE | NCPM_EXECUTE) == 0) {
267                 if(ncp_conn_free(conn))
268                         ncp_conn_unlock(conn, td);
269         }
270         mp->mnt_data = (qaddr_t)0;
271         if (nmp->m.flags & NWFS_MOUNT_HAVE_NLS)
272                 free(nmp->m.nls.to_lower, M_NWFSDATA);
273         free(nmp, M_NWFSDATA);
274         mp->mnt_flag &= ~MNT_LOCAL;
275         return (error);
276 }
277
278 /*  Return locked vnode to root of a filesystem */
279 static int
280 nwfs_root(struct mount *mp, int flags, struct vnode **vpp, struct thread *td) {
281         struct vnode *vp;
282         struct nwmount *nmp;
283         struct nwnode *np;
284         struct ncp_conn *conn;
285         struct nw_entry_info fattr;
286         struct ucred *cred =  td->td_ucred;
287         int error, nsf, opt;
288         u_char vol;
289
290         nmp = VFSTONWFS(mp);
291         conn = NWFSTOCONN(nmp);
292         if (nmp->n_root) {
293                 *vpp = NWTOV(nmp->n_root);
294                 while (vget(*vpp, LK_EXCLUSIVE, curthread) != 0)
295                         ;
296                 return 0;
297         }
298         error = ncp_lookup_volume(conn, nmp->m.mounted_vol, &vol, 
299                 &nmp->n_rootent.f_id, td, cred);
300         if (error)
301                 return ENOENT;
302         nmp->n_volume = vol;
303         error = ncp_get_namespaces(conn, vol, &nsf, td, cred);
304         if (error)
305                 return ENOENT;
306         if (nsf & NW_NSB_OS2) {
307                 NCPVODEBUG("volume %s has os2 namespace\n",nmp->m.mounted_vol);
308                 if ((nmp->m.flags & NWFS_MOUNT_NO_OS2) == 0) {
309                         nmp->name_space = NW_NS_OS2;
310                         nmp->m.nls.opt &= ~NWHP_DOS;
311                 }
312         }
313         opt = nmp->m.nls.opt;
314         nsf = opt & (NWHP_UPPER | NWHP_LOWER);
315         if (opt & NWHP_DOS) {
316                 if (nsf == (NWHP_UPPER | NWHP_LOWER)) {
317                         nmp->m.nls.opt &= ~(NWHP_LOWER | NWHP_UPPER);
318                 } else if (nsf == 0) {
319                         nmp->m.nls.opt |= NWHP_LOWER;
320                 }
321         } else {
322                 if (nsf == (NWHP_UPPER | NWHP_LOWER)) {
323                         nmp->m.nls.opt &= ~(NWHP_LOWER | NWHP_UPPER);
324                 }
325         }
326         if (nmp->m.root_path[0]) {
327                 nmp->m.root_path[0]--;
328                 error = ncp_obtain_info(nmp, nmp->n_rootent.f_id,
329                     -nmp->m.root_path[0], nmp->m.root_path, &fattr, td, cred);
330                 if (error) {
331                         NCPFATAL("Invalid root path specified\n");
332                         return ENOENT;
333                 }
334                 nmp->n_rootent.f_parent = fattr.dirEntNum;
335                 nmp->m.root_path[0]++;
336                 error = ncp_obtain_info(nmp, nmp->n_rootent.f_id,
337                     -nmp->m.root_path[0], nmp->m.root_path, &fattr, td, cred);
338                 if (error) {
339                         NCPFATAL("Invalid root path specified\n");
340                         return ENOENT;
341                 }
342                 nmp->n_rootent.f_id = fattr.dirEntNum;
343         } else {
344                 error = ncp_obtain_info(nmp, nmp->n_rootent.f_id,
345                     0, NULL, &fattr, td, cred);
346                 if (error) {
347                         NCPFATAL("Can't obtain volume info\n");
348                         return ENOENT;
349                 }
350                 fattr.nameLen = strlen(strcpy(fattr.entryName, "#.ROOT"));
351                 nmp->n_rootent.f_parent = nmp->n_rootent.f_id;
352         }
353         error = nwfs_nget(mp, nmp->n_rootent, &fattr, NULL, &vp);
354         if (error)
355                 return (error);
356         vp->v_vflag |= VV_ROOT;
357         np = VTONW(vp);
358         if (nmp->m.root_path[0] == 0)
359                 np->n_flag |= NVOLUME;
360         nmp->n_root = np;
361 /*      error = VOP_GETATTR(vp, &vattr, cred, td);
362         if (error) {
363                 vput(vp);
364                 NCPFATAL("Can't get root directory entry\n");
365                 return error;
366         }*/
367         *vpp = vp;
368         return (0);
369 }
370
371 /*
372  * Do operations associated with quotas, not supported
373  */
374 /* ARGSUSED */
375 static int
376 nwfs_quotactl(mp, cmd, uid, arg, td)
377         struct mount *mp;
378         int cmd;
379         uid_t uid;
380         caddr_t arg;
381         struct thread *td;
382 {
383         NCPVODEBUG("return EOPNOTSUPP\n");
384         return (EOPNOTSUPP);
385 }
386
387 /*ARGSUSED*/
388 int
389 nwfs_init(struct vfsconf *vfsp)
390 {
391         nwfs_hash_init();
392         nwfs_pbuf_freecnt = nswbuf / 2 + 1;
393         NCPVODEBUG("always happy to load!\n");
394         return (0);
395 }
396
397 /*ARGSUSED*/
398 int
399 nwfs_uninit(struct vfsconf *vfsp)
400 {
401
402         nwfs_hash_free();
403         NCPVODEBUG("unloaded\n");
404         return (0);
405 }
406
407 /*
408  * nwfs_statfs call
409  */
410 int
411 nwfs_statfs(mp, sbp, td)
412         struct mount *mp;
413         struct statfs *sbp;
414         struct thread *td;
415 {
416         struct nwmount *nmp = VFSTONWFS(mp);
417         int error = 0, secsize;
418         struct nwnode *np = nmp->n_root;
419         struct ncp_volume_info vi;
420
421         if (np == NULL) return EINVAL;
422         error = ncp_get_volume_info_with_number(NWFSTOCONN(nmp),
423             nmp->n_volume, &vi, td, td->td_ucred);
424         if (error) return error;
425         secsize = 512;                  /* XXX how to get real value ??? */
426         /* fundamental filesystem block size */
427         sbp->f_bsize = vi.sectors_per_block*secsize;
428         /* optimal transfer block size */
429         sbp->f_iosize = NWFSTOCONN(nmp)->buffer_size;
430         /* total data blocks in filesystem */
431         sbp->f_blocks= vi.total_blocks;
432         /* free blocks in fs */
433         sbp->f_bfree = vi.free_blocks + vi.purgeable_blocks;
434         /* free blocks avail to non-superuser */
435         sbp->f_bavail= vi.free_blocks+vi.purgeable_blocks;
436         /* total file nodes in filesystem */
437         sbp->f_files = vi.total_dir_entries;
438         /* free file nodes in fs */
439         sbp->f_ffree = vi.available_dir_entries;
440         sbp->f_flags = 0;               /* copy of mount exported flags */
441         return 0;
442 }