1 /*-
2 * Copyright (c) 2000-2001, 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/bio.h>
39 #include <sys/buf.h>
40 #include <sys/kernel.h>
41 #include <sys/sysctl.h>
42 #include <sys/vnode.h>
43 #include <sys/mount.h>
44 #include <sys/stat.h>
45 #include <sys/malloc.h>
46 #include <sys/module.h>
47
48
49 #include <netsmb/smb.h>
50 #include <netsmb/smb_conn.h>
51 #include <netsmb/smb_subr.h>
52 #include <netsmb/smb_dev.h>
53
54 #include <fs/smbfs/smbfs.h>
55 #include <fs/smbfs/smbfs_node.h>
56 #include <fs/smbfs/smbfs_subr.h>
57
58 static int smbfs_debuglevel = 0;
59
60 static int smbfs_version = SMBFS_VERSION;
61
62 #ifdef SMBFS_USEZONE
63 #include <vm/vm.h>
64 #include <vm/vm_extern.h>
65
66 vm_zone_t smbfsmount_zone;
67 #endif
68
69 SYSCTL_NODE(_vfs, OID_AUTO, smbfs, CTLFLAG_RW, 0, "SMB/CIFS filesystem");
70 SYSCTL_INT(_vfs_smbfs, OID_AUTO, version, CTLFLAG_RD, &smbfs_version, 0, "");
71 SYSCTL_INT(_vfs_smbfs, OID_AUTO, debuglevel, CTLFLAG_RW, &smbfs_debuglevel, 0, "");
72
73 static MALLOC_DEFINE(M_SMBFSHASH, "SMBFS hash", "SMBFS hash table");
74
75 static vfs_init_t smbfs_init;
76 static vfs_uninit_t smbfs_uninit;
77 static vfs_cmount_t smbfs_cmount;
78 static vfs_mount_t smbfs_mount;
79 static vfs_root_t smbfs_root;
80 static vfs_quotactl_t smbfs_quotactl;
81 static vfs_statfs_t smbfs_statfs;
82 static vfs_unmount_t smbfs_unmount;
83
84 static struct vfsops smbfs_vfsops = {
85 .vfs_init = smbfs_init,
86 .vfs_cmount = smbfs_cmount,
87 .vfs_mount = smbfs_mount,
88 .vfs_quotactl = smbfs_quotactl,
89 .vfs_root = smbfs_root,
90 .vfs_statfs = smbfs_statfs,
91 .vfs_sync = vfs_stdsync,
92 .vfs_uninit = smbfs_uninit,
93 .vfs_unmount = smbfs_unmount,
94 };
95
96
97 VFS_SET(smbfs_vfsops, smbfs, VFCF_NETWORK);
98
99 MODULE_DEPEND(smbfs, netsmb, NSMB_VERSION, NSMB_VERSION, NSMB_VERSION);
100 MODULE_DEPEND(smbfs, libiconv, 1, 1, 2);
101 MODULE_DEPEND(smbfs, libmchain, 1, 1, 1);
102
103 int smbfs_pbuf_freecnt = -1; /* start out unlimited */
104
105 static int
106 smbfs_cmount(struct mntarg *ma, void * data, int flags, struct thread *td)
107 {
108 struct smbfs_args args;
109 int error;
110
111 error = copyin(data, (caddr_t)&args, sizeof(struct smbfs_args));
112 if (error)
113 return error;
114
115 if (args.version != SMBFS_VERSION) {
116 printf("mount version mismatch: kernel=%d, mount=%d\n",
117 SMBFS_VERSION, args.version);
118 return EINVAL;
119 }
120 ma = mount_argf(ma, "dev", "%d", args.dev);
121 ma = mount_argb(ma, args.flags & SMBFS_MOUNT_SOFT, "nosoft");
122 ma = mount_argb(ma, args.flags & SMBFS_MOUNT_INTR, "nointr");
123 ma = mount_argb(ma, args.flags & SMBFS_MOUNT_STRONG, "nostrong");
124 ma = mount_argb(ma, args.flags & SMBFS_MOUNT_HAVE_NLS, "nohave_nls");
125 ma = mount_argb(ma, !(args.flags & SMBFS_MOUNT_NO_LONG), "nolong");
126 ma = mount_arg(ma, "rootpath", args.root_path, -1);
127 ma = mount_argf(ma, "uid", "%d", args.uid);
128 ma = mount_argf(ma, "gid", "%d", args.gid);
129 ma = mount_argf(ma, "file_mode", "%d", args.file_mode);
130 ma = mount_argf(ma, "dir_mode", "%d", args.dir_mode);
131 ma = mount_argf(ma, "caseopt", "%d", args.caseopt);
132
133 error = kernel_mount(ma, flags);
134
135 return (error);
136 }
137
138 static const char *smbfs_opts[] = {
139 "dev", "soft", "intr", "strong", "have_nls", "long",
140 "mountpoint", "rootpath", "uid", "gid", "file_mode", "dir_mode",
141 "caseopt", NULL
142 };
143
144 static int
145 smbfs_mount(struct mount *mp, struct thread *td)
146 {
147 struct smbmount *smp = NULL;
148 struct smb_vc *vcp;
149 struct smb_share *ssp = NULL;
150 struct vnode *vp;
151 struct smb_cred scred;
152 int error, v;
153 char *pc, *pe;
154
155 if (mp->mnt_flag & (MNT_UPDATE | MNT_ROOTFS))
156 return EOPNOTSUPP;
157
158 if (vfs_filteropt(mp->mnt_optnew, smbfs_opts))
159 return (EINVAL);
160
161 smb_makescred(&scred, td, td->td_ucred);
162 if (1 != vfs_scanopt(mp->mnt_optnew, "dev", "%d", &v))
163 return (EINVAL);
164 error = smb_dev2share(v, SMBM_EXEC, &scred, &ssp);
165 if (error) {
166 printf("invalid device handle %d (%d)\n", v, error);
167 return error;
168 }
169 vcp = SSTOVC(ssp);
170 smb_share_unlock(ssp, 0, td);
171 mp->mnt_stat.f_iosize = SSTOVC(ssp)->vc_txmax;
172
173 #ifdef SMBFS_USEZONE
174 smp = zalloc(smbfsmount_zone);
175 #else
176 MALLOC(smp, struct smbmount*, sizeof(*smp), M_SMBFSDATA,
177 M_WAITOK|M_USE_RESERVE);
178 #endif
179 if (smp == NULL) {
180 printf("could not alloc smbmount\n");
181 error = ENOMEM;
182 goto bad;
183 }
184 bzero(smp, sizeof(*smp));
185 mp->mnt_data = (qaddr_t)smp;
186 smp->sm_hash = hashinit(desiredvnodes, M_SMBFSHASH, &smp->sm_hashlen);
187 if (smp->sm_hash == NULL)
188 goto bad;
189 lockinit(&smp->sm_hashlock, PVFS, "smbfsh", 0, 0);
190 smp->sm_share = ssp;
191 smp->sm_root = NULL;
192 if (1 != vfs_scanopt(mp->mnt_optnew,
193 "caseopt", "%d", &smp->sm_caseopt)) {
194 error = EINVAL;
195 goto bad;
196 }
197 if (1 != vfs_scanopt(mp->mnt_optnew, "uid", "%d", &v)) {
198 error = EINVAL;
199 goto bad;
200 }
201 smp->sm_uid = v;
202
203 if (1 != vfs_scanopt(mp->mnt_optnew, "gid", "%d", &v)) {
204 error = EINVAL;
205 goto bad;
206 }
207 smp->sm_gid = v;
208
209 if (1 != vfs_scanopt(mp->mnt_optnew, "file_mode", "%d", &v)) {
210 error = EINVAL;
211 goto bad;
212 }
213 smp->sm_file_mode = (v & (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFREG;
214
215 if (1 != vfs_scanopt(mp->mnt_optnew, "dir_mode", "%d", &v)) {
216 error = EINVAL;
217 goto bad;
218 }
219 smp->sm_dir_mode = (v & (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFDIR;
220
221 vfs_flagopt(mp->mnt_optnew,
222 "long", &smp->sm_flags, SMBFS_MOUNT_NO_LONG);
223
224 smp->sm_flags ^= SMBFS_MOUNT_NO_LONG;
225
226 /* simple_lock_init(&smp->sm_npslock);*/
227 pc = mp->mnt_stat.f_mntfromname;
228 pe = pc + sizeof(mp->mnt_stat.f_mntfromname);
229 bzero(pc, MNAMELEN);
230 *pc++ = '/';
231 *pc++ = '/';
232 pc=index(strncpy(pc, vcp->vc_username, pe - pc - 2), 0);
233 if (pc < pe-1) {
234 *(pc++) = '@';
235 pc = index(strncpy(pc, vcp->vc_srvname, pe - pc - 2), 0);
236 if (pc < pe - 1) {
237 *(pc++) = '/';
238 strncpy(pc, ssp->ss_name, pe - pc - 2);
239 }
240 }
241 vfs_getnewfsid(mp);
242 error = smbfs_root(mp, LK_EXCLUSIVE, &vp, td);
243 if (error)
244 goto bad;
245 VOP_UNLOCK(vp, 0, td);
246 SMBVDEBUG("root.v_usecount = %d\n", vrefcnt(vp));
247
248 #ifdef DIAGNOSTICS
249 SMBERROR("mp=%p\n", mp);
250 #endif
251 return error;
252 bad:
253 if (smp) {
254 if (smp->sm_hash)
255 free(smp->sm_hash, M_SMBFSHASH);
256 lockdestroy(&smp->sm_hashlock);
257 #ifdef SMBFS_USEZONE
258 zfree(smbfsmount_zone, smp);
259 #else
260 free(smp, M_SMBFSDATA);
261 #endif
262 }
263 if (ssp)
264 smb_share_put(ssp, &scred);
265 return error;
266 }
267
268 /* Unmount the filesystem described by mp. */
269 static int
270 smbfs_unmount(struct mount *mp, int mntflags, struct thread *td)
271 {
272 struct smbmount *smp = VFSTOSMBFS(mp);
273 struct smb_cred scred;
274 int error, flags;
275
276 SMBVDEBUG("smbfs_unmount: flags=%04x\n", mntflags);
277 flags = 0;
278 if (mntflags & MNT_FORCE)
279 flags |= FORCECLOSE;
280 /*
281 * Keep trying to flush the vnode list for the mount while
282 * some are still busy and we are making progress towards
283 * making them not busy. This is needed because smbfs vnodes
284 * reference their parent directory but may appear after their
285 * parent in the list; one pass over the vnode list is not
286 * sufficient in this case.
287 */
288 do {
289 smp->sm_didrele = 0;
290 /* There is 1 extra root vnode reference from smbfs_mount(). */
291 error = vflush(mp, 1, flags, td);
292 } while (error == EBUSY && smp->sm_didrele != 0);
293 if (error)
294 return error;
295 smb_makescred(&scred, td, td->td_ucred);
296 smb_share_put(smp->sm_share, &scred);
297 mp->mnt_data = (qaddr_t)0;
298
299 if (smp->sm_hash)
300 free(smp->sm_hash, M_SMBFSHASH);
301 lockdestroy(&smp->sm_hashlock);
302 #ifdef SMBFS_USEZONE
303 zfree(smbfsmount_zone, smp);
304 #else
305 free(smp, M_SMBFSDATA);
306 #endif
307 MNT_ILOCK(mp);
308 mp->mnt_flag &= ~MNT_LOCAL;
309 MNT_IUNLOCK(mp);
310 return error;
311 }
312
313 /*
314 * Return locked root vnode of a filesystem
315 */
316 static int
317 smbfs_root(struct mount *mp, int flags, struct vnode **vpp, struct thread *td)
318 {
319 struct smbmount *smp = VFSTOSMBFS(mp);
320 struct vnode *vp;
321 struct smbnode *np;
322 struct smbfattr fattr;
323 struct ucred *cred = td->td_ucred;
324 struct smb_cred scred;
325 int error;
326
327 if (smp == NULL) {
328 SMBERROR("smp == NULL (bug in umount)\n");
329 return EINVAL;
330 }
331 if (smp->sm_root) {
332 *vpp = SMBTOV(smp->sm_root);
333 return vget(*vpp, LK_EXCLUSIVE | LK_RETRY, td);
334 }
335 smb_makescred(&scred, td, cred);
336 error = smbfs_smb_lookup(NULL, NULL, 0, &fattr, &scred);
337 if (error)
338 return error;
339 error = smbfs_nget(mp, NULL, "TheRooT", 7, &fattr, &vp);
340 if (error)
341 return error;
342 ASSERT_VOP_LOCKED(vp, "smbfs_root");
343 vp->v_vflag |= VV_ROOT;
344 np = VTOSMB(vp);
345 smp->sm_root = np;
346 *vpp = vp;
347 return 0;
348 }
349
350 /*
351 * Do operations associated with quotas, not supported
352 */
353 /* ARGSUSED */
354 static int
355 smbfs_quotactl(mp, cmd, uid, arg, td)
356 struct mount *mp;
357 int cmd;
358 uid_t uid;
359 caddr_t arg;
360 struct thread *td;
361 {
362 SMBVDEBUG("return EOPNOTSUPP\n");
363 return EOPNOTSUPP;
364 }
365
366 /*ARGSUSED*/
367 int
368 smbfs_init(struct vfsconf *vfsp)
369 {
370 #ifdef SMBFS_USEZONE
371 smbfsmount_zone = zinit("SMBFSMOUNT", sizeof(struct smbmount), 0, 0, 1);
372 #endif
373 smbfs_pbuf_freecnt = nswbuf / 2 + 1;
374 SMBVDEBUG("done.\n");
375 return 0;
376 }
377
378 /*ARGSUSED*/
379 int
380 smbfs_uninit(struct vfsconf *vfsp)
381 {
382
383 SMBVDEBUG("done.\n");
384 return 0;
385 }
386
387 /*
388 * smbfs_statfs call
389 */
390 int
391 smbfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
392 {
393 struct smbmount *smp = VFSTOSMBFS(mp);
394 struct smbnode *np = smp->sm_root;
395 struct smb_share *ssp = smp->sm_share;
396 struct smb_cred scred;
397 int error = 0;
398
399 if (np == NULL)
400 return EINVAL;
401
402 sbp->f_iosize = SSTOVC(ssp)->vc_txmax; /* optimal transfer block size */
403 smb_makescred(&scred, td, td->td_ucred);
404
405 if (SMB_DIALECT(SSTOVC(ssp)) >= SMB_DIALECT_LANMAN2_0)
406 error = smbfs_smb_statfs2(ssp, sbp, &scred);
407 else
408 error = smbfs_smb_statfs(ssp, sbp, &scred);
409 if (error)
410 return error;
411 sbp->f_flags = 0; /* copy of mount exported flags */
412 return 0;
413 }
Cache object: 4a82baab8c37f885a62b090db70154f4
|