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