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