2 * Copyright (c) 2000-2001 Boris Popov
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/kernel.h>
31 #include <sys/malloc.h>
34 #include <sys/vnode.h>
36 #include <sys/mount.h>
37 #include <sys/endian.h>
43 #include <netsmb/smb.h>
44 #include <netsmb/smb_subr.h>
45 #include <netsmb/smb_rq.h>
46 #include <netsmb/smb_conn.h>
48 #include <fs/smbfs/smbfs.h>
49 #include <fs/smbfs/smbfs_node.h>
50 #include <fs/smbfs/smbfs_subr.h>
53 * Lack of inode numbers leads us to the problem of generating them.
54 * Partially this problem can be solved by having a dir/file cache
55 * with inode numbers generated from the incremented by one counter.
56 * However this way will require too much kernel memory, gives all
57 * sorts of locking and consistency problems, not to mentinon counter overflows.
58 * So, I'm decided to use a hash function to generate pseudo random (and unique)
62 smbfs_getino(struct smbnode *dnp, const char *name, int nmlen)
71 MD5Update(&md5, name, nmlen);
72 MD5Final((u_char *)state, &md5);
73 for (i = 0, ino = 0; i < 4; i++)
75 return dnp->n_ino + ino;
79 ino = dnp->n_ino + smbfs_hash(name, nmlen);
86 smbfs_smb_lockandx(struct smbnode *np, int op, u_int32_t pid, off_t start, off_t end,
87 struct smb_cred *scred)
89 struct smb_share *ssp = np->n_mount->sm_share;
95 if (op == SMB_LOCK_SHARED)
96 ltype |= SMB_LOCKING_ANDX_SHARED_LOCK;
98 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_LOCKING_ANDX, scred, &rqp);
101 smb_rq_getrequest(rqp, &mbp);
103 mb_put_uint8(mbp, 0xff); /* secondary command */
104 mb_put_uint8(mbp, 0); /* MBZ */
105 mb_put_uint16le(mbp, 0);
106 mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
107 mb_put_uint8(mbp, ltype); /* locktype */
108 mb_put_uint8(mbp, 0); /* oplocklevel - 0 seems is NO_OPLOCK */
109 mb_put_uint32le(mbp, 0); /* timeout - break immediately */
110 mb_put_uint16le(mbp, op == SMB_LOCK_RELEASE ? 1 : 0);
111 mb_put_uint16le(mbp, op == SMB_LOCK_RELEASE ? 0 : 1);
114 mb_put_uint16le(mbp, pid);
115 mb_put_uint32le(mbp, start);
116 mb_put_uint32le(mbp, end - start);
118 error = smb_rq_simple(rqp);
124 smbfs_smb_lock(struct smbnode *np, int op, caddr_t id,
125 off_t start, off_t end, struct smb_cred *scred)
127 struct smb_share *ssp = np->n_mount->sm_share;
129 if (SMB_DIALECT(SSTOVC(ssp)) < SMB_DIALECT_LANMAN1_0)
131 * TODO: use LOCK_BYTE_RANGE here.
135 return smbfs_smb_lockandx(np, op, (uintptr_t)id, start, end, scred);
139 smbfs_query_info_fs(struct smb_share *ssp, struct statfs *sbp,
140 struct smb_cred *scred)
142 struct smb_t2rq *t2p;
146 int64_t units, funits;
149 error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_QUERY_FS_INFORMATION,
153 mbp = &t2p->t2_tparam;
155 mb_put_uint16le(mbp, SMB_QUERY_FS_SIZE_INFO);
156 t2p->t2_maxpcount = 2;
157 t2p->t2_maxdcount = sizeof(int64_t) * 2 + sizeof(uint32_t) * 2;
158 error = smb_t2_request(t2p);
163 mdp = &t2p->t2_rdata;
164 md_get_int64le(mdp, &units);
165 md_get_int64le(mdp, &funits);
166 md_get_uint32le(mdp, &bpu);
167 md_get_uint32le(mdp, &bsize);
168 sbp->f_bsize = bpu * bsize; /* fundamental filesystem block size */
169 sbp->f_blocks= (uint64_t)units; /* total data blocks in filesystem */
170 sbp->f_bfree = (uint64_t)funits;/* free blocks in fs */
171 sbp->f_bavail= (uint64_t)funits;/* free blocks avail to non-superuser */
172 sbp->f_files = 0xffff; /* total file nodes in filesystem */
173 sbp->f_ffree = 0xffff; /* free file nodes in fs */
180 smbfs_query_info_alloc(struct smb_share *ssp, struct statfs *sbp,
181 struct smb_cred *scred)
183 struct smb_t2rq *t2p;
187 u_int32_t units, bpu, funits;
190 error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_QUERY_FS_INFORMATION,
194 mbp = &t2p->t2_tparam;
196 mb_put_uint16le(mbp, SMB_INFO_ALLOCATION);
197 t2p->t2_maxpcount = 4;
198 t2p->t2_maxdcount = 4 * 4 + 2;
199 error = smb_t2_request(t2p);
204 mdp = &t2p->t2_rdata;
205 md_get_uint32(mdp, NULL); /* fs id */
206 md_get_uint32le(mdp, &bpu);
207 md_get_uint32le(mdp, &units);
208 md_get_uint32le(mdp, &funits);
209 md_get_uint16le(mdp, &bsize);
210 sbp->f_bsize = bpu * bsize; /* fundamental filesystem block size */
211 sbp->f_blocks= units; /* total data blocks in filesystem */
212 sbp->f_bfree = funits; /* free blocks in fs */
213 sbp->f_bavail= funits; /* free blocks avail to non-superuser */
214 sbp->f_files = 0xffff; /* total file nodes in filesystem */
215 sbp->f_ffree = 0xffff; /* free file nodes in fs */
221 smbfs_query_info_disk(struct smb_share *ssp, struct statfs *sbp,
222 struct smb_cred *scred)
226 u_int16_t units, bpu, bsize, funits;
229 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_QUERY_INFORMATION_DISK,
237 error = smb_rq_simple(rqp);
242 smb_rq_getreply(rqp, &mdp);
243 md_get_uint16le(mdp, &units);
244 md_get_uint16le(mdp, &bpu);
245 md_get_uint16le(mdp, &bsize);
246 md_get_uint16le(mdp, &funits);
247 sbp->f_bsize = bpu * bsize; /* fundamental filesystem block size */
248 sbp->f_blocks= units; /* total data blocks in filesystem */
249 sbp->f_bfree = funits; /* free blocks in fs */
250 sbp->f_bavail= funits; /* free blocks avail to non-superuser */
251 sbp->f_files = 0xffff; /* total file nodes in filesystem */
252 sbp->f_ffree = 0xffff; /* free file nodes in fs */
258 smbfs_smb_statfs(struct smb_share *ssp, struct statfs *sbp,
259 struct smb_cred *scred)
262 if (SMB_DIALECT(SSTOVC(ssp)) >= SMB_DIALECT_LANMAN2_0) {
263 if (smbfs_query_info_fs(ssp, sbp, scred) == 0)
265 if (smbfs_query_info_alloc(ssp, sbp, scred) == 0)
268 return (smbfs_query_info_disk(ssp, sbp, scred));
272 smbfs_smb_seteof(struct smbnode *np, int64_t newsize, struct smb_cred *scred)
274 struct smb_t2rq *t2p;
275 struct smb_share *ssp = np->n_mount->sm_share;
279 error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_SET_FILE_INFORMATION,
283 mbp = &t2p->t2_tparam;
285 mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
286 mb_put_uint16le(mbp, SMB_SET_FILE_END_OF_FILE_INFO);
287 mb_put_uint32le(mbp, 0);
288 mbp = &t2p->t2_tdata;
290 mb_put_int64le(mbp, newsize);
291 mb_put_uint32le(mbp, 0); /* padding */
292 mb_put_uint16le(mbp, 0);
293 t2p->t2_maxpcount = 2;
294 t2p->t2_maxdcount = 0;
295 error = smb_t2_request(t2p);
301 smb_smb_flush(struct smbnode *np, struct smb_cred *scred)
303 struct smb_share *ssp = np->n_mount->sm_share;
308 if ((np->n_flag & NOPEN) == 0 || !SMBTOV(np) ||
309 SMBTOV(np)->v_type != VREG)
310 return 0; /* not a regular open file */
311 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_FLUSH, scred, &rqp);
314 smb_rq_getrequest(rqp, &mbp);
316 mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
320 error = smb_rq_simple(rqp);
323 np->n_flag &= ~NFLUSHWIRE;
328 smbfs_smb_flush(struct smbnode *np, struct smb_cred *scred)
330 if (np->n_flag & NFLUSHWIRE)
331 return (smb_smb_flush(np, scred));
336 smbfs_smb_setfsize(struct smbnode *np, int64_t newsize, struct smb_cred *scred)
338 struct smb_share *ssp = np->n_mount->sm_share;
343 if (!smbfs_smb_seteof(np, newsize, scred)) {
344 np->n_flag |= NFLUSHWIRE;
347 /* XXX: We should use SMB_COM_WRITE_ANDX to support large offsets */
348 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_WRITE, scred, &rqp);
351 smb_rq_getrequest(rqp, &mbp);
353 mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
354 mb_put_uint16le(mbp, 0);
355 mb_put_uint32le(mbp, (uint32_t)newsize);
356 mb_put_uint16le(mbp, 0);
359 mb_put_uint8(mbp, SMB_DT_DATA);
360 mb_put_uint16le(mbp, 0);
362 error = smb_rq_simple(rqp);
368 smbfs_smb_query_info(struct smbnode *np, const char *name, int len,
369 struct smbfattr *fap, struct smb_cred *scred)
372 struct smb_share *ssp = np->n_mount->sm_share;
380 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_QUERY_INFORMATION, scred,
384 smb_rq_getrequest(rqp, &mbp);
388 mb_put_uint8(mbp, SMB_DT_ASCII);
390 error = smbfs_fullpath(mbp, SSTOVC(ssp), np, name, len);
394 error = smb_rq_simple(rqp);
397 smb_rq_getreply(rqp, &mdp);
398 if (md_get_uint8(mdp, &wc) != 0 || wc != 10) {
402 md_get_uint16le(mdp, &wattr);
403 fap->fa_attr = wattr;
405 * Be careful using the time returned here, as
406 * with FAT on NT4SP6, at least, the time returned is low
407 * 32 bits of 100s of nanoseconds (since 1601) so it rolls
408 * over about every seven minutes!
410 md_get_uint32le(mdp, &lint); /* specs: secs since 1970 */
411 if (lint) /* avoid bogus zero returns */
412 smb_time_server2local(lint, SSTOVC(ssp)->vc_sopt.sv_tz,
414 md_get_uint32le(mdp, &lint);
422 * Set DOS file attributes. mtime should be NULL for dialects above lm10
425 smbfs_smb_setpattr(struct smbnode *np, u_int16_t attr, struct timespec *mtime,
426 struct smb_cred *scred)
429 struct smb_share *ssp = np->n_mount->sm_share;
434 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_SET_INFORMATION, scred,
438 svtz = SSTOVC(ssp)->vc_sopt.sv_tz;
439 smb_rq_getrequest(rqp, &mbp);
441 mb_put_uint16le(mbp, attr);
443 smb_time_local2server(mtime, svtz, &time);
446 mb_put_uint32le(mbp, time); /* mtime */
447 mb_put_mem(mbp, NULL, 5 * 2, MB_MZERO);
450 mb_put_uint8(mbp, SMB_DT_ASCII);
452 error = smbfs_fullpath(mbp, SSTOVC(ssp), np, NULL, 0);
455 mb_put_uint8(mbp, SMB_DT_ASCII);
456 if (SMB_UNICODE_STRINGS(SSTOVC(ssp))) {
458 mb_put_uint8(mbp, 0); /* 1st byte of NULL Unicode char */
460 mb_put_uint8(mbp, 0);
462 error = smb_rq_simple(rqp);
464 SMBERROR("smb_rq_simple(rqp) => error %d\n", error);
473 * Note, win95 doesn't support this call.
476 smbfs_smb_setptime2(struct smbnode *np, struct timespec *mtime,
477 struct timespec *atime, int attr, struct smb_cred *scred)
479 struct smb_t2rq *t2p;
480 struct smb_share *ssp = np->n_mount->sm_share;
481 struct smb_vc *vcp = SSTOVC(ssp);
483 u_int16_t date, time;
486 error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_SET_PATH_INFORMATION,
490 mbp = &t2p->t2_tparam;
492 mb_put_uint16le(mbp, SMB_INFO_STANDARD);
493 mb_put_uint32le(mbp, 0); /* MBZ */
494 /* mb_put_uint8(mbp, SMB_DT_ASCII); specs incorrect */
495 error = smbfs_fullpath(mbp, vcp, np, NULL, 0);
500 tzoff = vcp->vc_sopt.sv_tz;
501 mbp = &t2p->t2_tdata;
503 mb_put_uint32le(mbp, 0); /* creation time */
505 smb_time_unix2dos(atime, tzoff, &date, &time, NULL);
508 mb_put_uint16le(mbp, date);
509 mb_put_uint16le(mbp, time);
511 smb_time_unix2dos(mtime, tzoff, &date, &time, NULL);
514 mb_put_uint16le(mbp, date);
515 mb_put_uint16le(mbp, time);
516 mb_put_uint32le(mbp, 0); /* file size */
517 mb_put_uint32le(mbp, 0); /* allocation unit size */
518 mb_put_uint16le(mbp, attr); /* DOS attr */
519 mb_put_uint32le(mbp, 0); /* EA size */
520 t2p->t2_maxpcount = 5 * 2;
521 t2p->t2_maxdcount = vcp->vc_txmax;
522 error = smb_t2_request(t2p);
528 * NT level. Specially for win9x
531 smbfs_smb_setpattrNT(struct smbnode *np, u_short attr, struct timespec *mtime,
532 struct timespec *atime, struct smb_cred *scred)
534 struct smb_t2rq *t2p;
535 struct smb_share *ssp = np->n_mount->sm_share;
536 struct smb_vc *vcp = SSTOVC(ssp);
541 error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_SET_PATH_INFORMATION,
545 mbp = &t2p->t2_tparam;
547 mb_put_uint16le(mbp, SMB_SET_FILE_BASIC_INFO);
548 mb_put_uint32le(mbp, 0); /* MBZ */
549 /* mb_put_uint8(mbp, SMB_DT_ASCII); specs incorrect */
550 error = smbfs_fullpath(mbp, vcp, np, NULL, 0);
555 tzoff = vcp->vc_sopt.sv_tz;
556 mbp = &t2p->t2_tdata;
558 mb_put_int64le(mbp, 0); /* creation time */
560 smb_time_local2NT(atime, tzoff, &tm);
563 mb_put_int64le(mbp, tm);
565 smb_time_local2NT(mtime, tzoff, &tm);
568 mb_put_int64le(mbp, tm);
569 mb_put_int64le(mbp, tm); /* change time */
570 mb_put_uint32le(mbp, attr); /* attr */
571 t2p->t2_maxpcount = 24;
572 t2p->t2_maxdcount = 56;
573 error = smb_t2_request(t2p);
579 * Set file atime and mtime. Doesn't supported by core dialect.
582 smbfs_smb_setftime(struct smbnode *np, struct timespec *mtime,
583 struct timespec *atime, struct smb_cred *scred)
586 struct smb_share *ssp = np->n_mount->sm_share;
588 u_int16_t date, time;
591 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_SET_INFORMATION2, scred,
595 tzoff = SSTOVC(ssp)->vc_sopt.sv_tz;
596 smb_rq_getrequest(rqp, &mbp);
598 mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
599 mb_put_uint32le(mbp, 0); /* creation time */
602 smb_time_unix2dos(atime, tzoff, &date, &time, NULL);
605 mb_put_uint16le(mbp, date);
606 mb_put_uint16le(mbp, time);
608 smb_time_unix2dos(mtime, tzoff, &date, &time, NULL);
611 mb_put_uint16le(mbp, date);
612 mb_put_uint16le(mbp, time);
616 error = smb_rq_simple(rqp);
617 SMBSDEBUG("%d\n", error);
623 * Set DOS file attributes.
624 * Looks like this call can be used only if SMB_CAP_NT_SMBS bit is on.
627 smbfs_smb_setfattrNT(struct smbnode *np, u_int16_t attr, struct timespec *mtime,
628 struct timespec *atime, struct smb_cred *scred)
630 struct smb_t2rq *t2p;
631 struct smb_share *ssp = np->n_mount->sm_share;
636 error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_SET_FILE_INFORMATION,
640 svtz = SSTOVC(ssp)->vc_sopt.sv_tz;
641 mbp = &t2p->t2_tparam;
643 mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
644 mb_put_uint16le(mbp, SMB_SET_FILE_BASIC_INFO);
645 mb_put_uint32le(mbp, 0);
646 mbp = &t2p->t2_tdata;
648 mb_put_int64le(mbp, 0); /* creation time */
650 smb_time_local2NT(atime, svtz, &tm);
653 mb_put_int64le(mbp, tm);
655 smb_time_local2NT(mtime, svtz, &tm);
658 mb_put_int64le(mbp, tm);
659 mb_put_int64le(mbp, tm); /* change time */
660 mb_put_uint16le(mbp, attr);
661 mb_put_uint32le(mbp, 0); /* padding */
662 mb_put_uint16le(mbp, 0);
663 t2p->t2_maxpcount = 2;
664 t2p->t2_maxdcount = 0;
665 error = smb_t2_request(t2p);
672 smbfs_smb_open(struct smbnode *np, int accmode, struct smb_cred *scred)
675 struct smb_share *ssp = np->n_mount->sm_share;
679 u_int16_t fid, wattr, grantedmode;
682 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_OPEN, scred, &rqp);
685 smb_rq_getrequest(rqp, &mbp);
687 mb_put_uint16le(mbp, accmode);
688 mb_put_uint16le(mbp, SMB_FA_SYSTEM | SMB_FA_HIDDEN);
691 mb_put_uint8(mbp, SMB_DT_ASCII);
693 error = smbfs_fullpath(mbp, SSTOVC(ssp), np, NULL, 0);
697 error = smb_rq_simple(rqp);
700 smb_rq_getreply(rqp, &mdp);
701 if (md_get_uint8(mdp, &wc) != 0 || wc != 7) {
705 md_get_uint16(mdp, &fid);
706 md_get_uint16le(mdp, &wattr);
707 md_get_uint32(mdp, NULL); /* mtime */
708 md_get_uint32(mdp, NULL); /* fsize */
709 md_get_uint16le(mdp, &grantedmode);
711 * TODO: refresh attributes from this reply
718 np->n_rwstate = grantedmode;
724 smbfs_smb_close(struct smb_share *ssp, u_int16_t fid, struct timespec *mtime,
725 struct smb_cred *scred)
732 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_CLOSE, scred, &rqp);
735 smb_rq_getrequest(rqp, &mbp);
737 mb_put_mem(mbp, (caddr_t)&fid, sizeof(fid), MB_MSYSTEM);
739 smb_time_local2server(mtime, SSTOVC(ssp)->vc_sopt.sv_tz, &time);
742 mb_put_uint32le(mbp, time);
746 error = smb_rq_simple(rqp);
752 smbfs_smb_create(struct smbnode *dnp, const char *name, int nmlen,
753 struct smb_cred *scred)
756 struct smb_share *ssp = dnp->n_mount->sm_share;
759 struct timespec ctime;
765 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_CREATE, scred, &rqp);
768 smb_rq_getrequest(rqp, &mbp);
770 mb_put_uint16le(mbp, SMB_FA_ARCHIVE); /* attributes */
772 smb_time_local2server(&ctime, SSTOVC(ssp)->vc_sopt.sv_tz, &tm);
773 mb_put_uint32le(mbp, tm);
776 mb_put_uint8(mbp, SMB_DT_ASCII);
777 error = smbfs_fullpath(mbp, SSTOVC(ssp), dnp, name, nmlen);
780 error = smb_rq_simple(rqp);
782 smb_rq_getreply(rqp, &mdp);
783 md_get_uint8(mdp, &wc);
785 md_get_uint16(mdp, &fid);
793 smbfs_smb_close(ssp, fid, &ctime, scred);
798 smbfs_smb_delete(struct smbnode *np, struct smb_cred *scred)
801 struct smb_share *ssp = np->n_mount->sm_share;
805 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_DELETE, scred, &rqp);
808 smb_rq_getrequest(rqp, &mbp);
810 mb_put_uint16le(mbp, SMB_FA_SYSTEM | SMB_FA_HIDDEN);
813 mb_put_uint8(mbp, SMB_DT_ASCII);
814 error = smbfs_fullpath(mbp, SSTOVC(ssp), np, NULL, 0);
817 error = smb_rq_simple(rqp);
824 smbfs_smb_rename(struct smbnode *src, struct smbnode *tdnp,
825 const char *tname, int tnmlen, struct smb_cred *scred)
828 struct smb_share *ssp = src->n_mount->sm_share;
832 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_RENAME, scred, &rqp);
835 smb_rq_getrequest(rqp, &mbp);
837 mb_put_uint16le(mbp, SMB_FA_SYSTEM | SMB_FA_HIDDEN);
840 mb_put_uint8(mbp, SMB_DT_ASCII);
842 error = smbfs_fullpath(mbp, SSTOVC(ssp), src, NULL, 0);
845 mb_put_uint8(mbp, SMB_DT_ASCII);
846 error = smbfs_fullpath(mbp, SSTOVC(ssp), tdnp, tname, tnmlen);
850 error = smb_rq_simple(rqp);
857 smbfs_smb_move(struct smbnode *src, struct smbnode *tdnp,
858 const char *tname, int tnmlen, u_int16_t flags, struct smb_cred *scred)
861 struct smb_share *ssp = src->n_mount->sm_share;
865 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_MOVE, scred, &rqp);
868 smb_rq_getrequest(rqp, &mbp);
870 mb_put_uint16le(mbp, SMB_TID_UNKNOWN);
871 mb_put_uint16le(mbp, 0x20); /* delete target file */
872 mb_put_uint16le(mbp, flags);
875 mb_put_uint8(mbp, SMB_DT_ASCII);
877 error = smbfs_fullpath(mbp, SSTOVC(ssp), src, NULL, 0);
880 mb_put_uint8(mbp, SMB_DT_ASCII);
881 error = smbfs_fullpath(mbp, SSTOVC(ssp), tdnp, tname, tnmlen);
885 error = smb_rq_simple(rqp);
892 smbfs_smb_mkdir(struct smbnode *dnp, const char *name, int len,
893 struct smb_cred *scred)
896 struct smb_share *ssp = dnp->n_mount->sm_share;
900 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_CREATE_DIRECTORY, scred,
904 smb_rq_getrequest(rqp, &mbp);
908 mb_put_uint8(mbp, SMB_DT_ASCII);
909 error = smbfs_fullpath(mbp, SSTOVC(ssp), dnp, name, len);
912 error = smb_rq_simple(rqp);
919 smbfs_smb_rmdir(struct smbnode *np, struct smb_cred *scred)
922 struct smb_share *ssp = np->n_mount->sm_share;
926 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_DELETE_DIRECTORY, scred,
930 smb_rq_getrequest(rqp, &mbp);
934 mb_put_uint8(mbp, SMB_DT_ASCII);
935 error = smbfs_fullpath(mbp, SSTOVC(ssp), np, NULL, 0);
938 error = smb_rq_simple(rqp);
945 smbfs_smb_search(struct smbfs_fctx *ctx)
947 struct smb_vc *vcp = SSTOVC(ctx->f_ssp);
952 u_int16_t ec, dlen, bc;
953 int maxent, error, iseof = 0;
955 maxent = min(ctx->f_left, (vcp->vc_txmax - SMB_HDRLEN - 3) / SMB_DENTRYLEN);
957 smb_rq_done(ctx->f_rq);
960 error = smb_rq_alloc(SSTOCP(ctx->f_ssp), SMB_COM_SEARCH, ctx->f_scred, &rqp);
964 smb_rq_getrequest(rqp, &mbp);
966 mb_put_uint16le(mbp, maxent); /* max entries to return */
967 mb_put_uint16le(mbp, ctx->f_attrmask);
970 mb_put_uint8(mbp, SMB_DT_ASCII); /* buffer format */
971 if (ctx->f_flags & SMBFS_RDD_FINDFIRST) {
972 error = smbfs_fullpath(mbp, vcp, ctx->f_dnp, ctx->f_wildcard, ctx->f_wclen);
975 mb_put_uint8(mbp, SMB_DT_VARIABLE);
976 mb_put_uint16le(mbp, 0); /* context length */
977 ctx->f_flags &= ~SMBFS_RDD_FINDFIRST;
979 if (SMB_UNICODE_STRINGS(vcp)) {
981 mb_put_uint8(mbp, 0);
983 mb_put_uint8(mbp, 0); /* file name length */
984 mb_put_uint8(mbp, SMB_DT_VARIABLE);
985 mb_put_uint16le(mbp, SMB_SKEYLEN);
986 mb_put_mem(mbp, ctx->f_skey, SMB_SKEYLEN, MB_MSYSTEM);
989 error = smb_rq_simple(rqp);
991 if (rqp->sr_errclass == ERRDOS && rqp->sr_serror == ERRnofiles) {
994 ctx->f_flags |= SMBFS_RDD_EOF;
998 smb_rq_getreply(rqp, &mdp);
999 md_get_uint8(mdp, &wc);
1001 return iseof ? ENOENT : EBADRPC;
1002 md_get_uint16le(mdp, &ec);
1006 md_get_uint16le(mdp, &bc);
1010 md_get_uint8(mdp, &bt);
1011 if (bt != SMB_DT_VARIABLE)
1013 md_get_uint16le(mdp, &dlen);
1014 if (dlen != bc || dlen % SMB_DENTRYLEN != 0)
1020 smbfs_findopenLM1(struct smbfs_fctx *ctx, struct smbnode *dnp,
1021 const char *wildcard, int wclen, int attr, struct smb_cred *scred)
1023 ctx->f_attrmask = attr;
1025 if (wclen == 1 && wildcard[0] == '*') {
1026 ctx->f_wildcard = "*.*";
1029 ctx->f_wildcard = wildcard;
1030 ctx->f_wclen = wclen;
1033 ctx->f_wildcard = NULL;
1036 ctx->f_name = ctx->f_fname;
1041 smbfs_findnextLM1(struct smbfs_fctx *ctx, int limit)
1043 struct mdchain *mbp;
1047 u_int16_t date, time;
1051 if (ctx->f_ecnt == 0) {
1052 if (ctx->f_flags & SMBFS_RDD_EOF)
1054 ctx->f_left = ctx->f_limit = limit;
1055 error = smbfs_smb_search(ctx);
1060 smb_rq_getreply(rqp, &mbp);
1061 md_get_mem(mbp, ctx->f_skey, SMB_SKEYLEN, MB_MSYSTEM);
1062 md_get_uint8(mbp, &battr);
1063 md_get_uint16le(mbp, &time);
1064 md_get_uint16le(mbp, &date);
1065 md_get_uint32le(mbp, &size);
1067 md_get_mem(mbp, cp, sizeof(ctx->f_fname), MB_MSYSTEM);
1068 cp[sizeof(ctx->f_fname) - 1] = 0;
1069 cp += strlen(cp) - 1;
1070 while (*cp == ' ' && cp >= ctx->f_name)
1072 ctx->f_attr.fa_attr = battr;
1073 smb_dos2unixtime(date, time, 0, rqp->sr_vc->vc_sopt.sv_tz,
1074 &ctx->f_attr.fa_mtime);
1075 ctx->f_attr.fa_size = size;
1076 ctx->f_nmlen = strlen(ctx->f_name);
1083 smbfs_findcloseLM1(struct smbfs_fctx *ctx)
1086 smb_rq_done(ctx->f_rq);
1091 * TRANS2_FIND_FIRST2/NEXT2, used for NT LM12 dialect
1094 smbfs_smb_trans2find2(struct smbfs_fctx *ctx)
1096 struct smb_t2rq *t2p;
1097 struct smb_vc *vcp = SSTOVC(ctx->f_ssp);
1098 struct mbchain *mbp;
1099 struct mdchain *mdp;
1100 u_int16_t tw, flags;
1104 smb_t2_done(ctx->f_t2);
1107 ctx->f_flags &= ~SMBFS_RDD_GOTRNAME;
1108 flags = 8 | 2; /* <resume> | <close if EOS> */
1109 if (ctx->f_flags & SMBFS_RDD_FINDSINGLE) {
1110 flags |= 1; /* close search after this request */
1111 ctx->f_flags |= SMBFS_RDD_NOCLOSE;
1113 if (ctx->f_flags & SMBFS_RDD_FINDFIRST) {
1114 error = smb_t2_alloc(SSTOCP(ctx->f_ssp), SMB_TRANS2_FIND_FIRST2,
1115 ctx->f_scred, &t2p);
1119 mbp = &t2p->t2_tparam;
1121 mb_put_uint16le(mbp, ctx->f_attrmask);
1122 mb_put_uint16le(mbp, ctx->f_limit);
1123 mb_put_uint16le(mbp, flags);
1124 mb_put_uint16le(mbp, ctx->f_infolevel);
1125 mb_put_uint32le(mbp, 0);
1126 error = smbfs_fullpath(mbp, vcp, ctx->f_dnp, ctx->f_wildcard, ctx->f_wclen);
1130 error = smb_t2_alloc(SSTOCP(ctx->f_ssp), SMB_TRANS2_FIND_NEXT2,
1131 ctx->f_scred, &t2p);
1135 mbp = &t2p->t2_tparam;
1137 mb_put_mem(mbp, (caddr_t)&ctx->f_Sid, 2, MB_MSYSTEM);
1138 mb_put_uint16le(mbp, ctx->f_limit);
1139 mb_put_uint16le(mbp, ctx->f_infolevel);
1140 mb_put_uint32le(mbp, 0); /* resume key */
1141 mb_put_uint16le(mbp, flags);
1143 mb_put_mem(mbp, ctx->f_rname, ctx->f_rnamelen + 1, MB_MSYSTEM);
1145 mb_put_uint8(mbp, 0); /* resume file name */
1149 tv.tv_usec = 200 * 1000; /* 200ms */
1150 if (vcp->vc_flags & SMBC_WIN95) {
1152 * some implementations suggests to sleep here
1153 * for 200ms, due to the bug in the Win95.
1154 * I've didn't notice any problem, but put code
1157 pause("fix95", tvtohz(&tv));
1161 t2p->t2_maxpcount = 5 * 2;
1162 t2p->t2_maxdcount = vcp->vc_txmax;
1163 error = smb_t2_request(t2p);
1166 mdp = &t2p->t2_rparam;
1167 if (ctx->f_flags & SMBFS_RDD_FINDFIRST) {
1168 if ((error = md_get_uint16(mdp, &ctx->f_Sid)) != 0)
1170 ctx->f_flags &= ~SMBFS_RDD_FINDFIRST;
1172 if ((error = md_get_uint16le(mdp, &tw)) != 0)
1175 if ((error = md_get_uint16le(mdp, &tw)) != 0)
1178 ctx->f_flags |= SMBFS_RDD_EOF | SMBFS_RDD_NOCLOSE;
1179 if ((error = md_get_uint16le(mdp, &tw)) != 0)
1181 if ((error = md_get_uint16le(mdp, &tw)) != 0)
1183 if (ctx->f_ecnt == 0) {
1184 ctx->f_flags |= SMBFS_RDD_EOF | SMBFS_RDD_NOCLOSE;
1187 ctx->f_rnameofs = tw;
1188 mdp = &t2p->t2_rdata;
1189 if (mdp->md_top == NULL) {
1190 printf("bug: ecnt = %d, but data is NULL (please report)\n", ctx->f_ecnt);
1193 if (mdp->md_top->m_len == 0) {
1194 printf("bug: ecnt = %d, but m_len = 0 and m_next = %p (please report)\n", ctx->f_ecnt,mbp->mb_top->m_next);
1202 smbfs_smb_findclose2(struct smbfs_fctx *ctx)
1205 struct mbchain *mbp;
1208 error = smb_rq_alloc(SSTOCP(ctx->f_ssp), SMB_COM_FIND_CLOSE2,
1209 ctx->f_scred, &rqp);
1212 smb_rq_getrequest(rqp, &mbp);
1214 mb_put_mem(mbp, (caddr_t)&ctx->f_Sid, 2, MB_MSYSTEM);
1218 error = smb_rq_simple(rqp);
1224 smbfs_findopenLM2(struct smbfs_fctx *ctx, struct smbnode *dnp,
1225 const char *wildcard, int wclen, int attr, struct smb_cred *scred)
1227 if (SMB_UNICODE_STRINGS(SSTOVC(ctx->f_ssp))) {
1228 ctx->f_name = malloc(SMB_MAXFNAMELEN * 2, M_SMBFSDATA, M_WAITOK);
1230 ctx->f_name = malloc(SMB_MAXFNAMELEN, M_SMBFSDATA, M_WAITOK);
1231 ctx->f_infolevel = SMB_DIALECT(SSTOVC(ctx->f_ssp)) < SMB_DIALECT_NTLM0_12 ?
1232 SMB_INFO_STANDARD : SMB_FIND_FILE_DIRECTORY_INFO;
1233 ctx->f_attrmask = attr;
1234 ctx->f_wildcard = wildcard;
1235 ctx->f_wclen = wclen;
1240 smbfs_findnextLM2(struct smbfs_fctx *ctx, int limit)
1242 struct mdchain *mbp;
1243 struct smb_t2rq *t2p;
1246 u_int16_t date, time, wattr;
1247 u_int32_t size, next, dattr;
1249 int error, svtz, cnt, fxsz, nmlen, recsz;
1251 if (ctx->f_ecnt == 0) {
1252 if (ctx->f_flags & SMBFS_RDD_EOF)
1254 ctx->f_left = ctx->f_limit = limit;
1255 error = smbfs_smb_trans2find2(ctx);
1260 mbp = &t2p->t2_rdata;
1261 svtz = SSTOVC(ctx->f_ssp)->vc_sopt.sv_tz;
1262 switch (ctx->f_infolevel) {
1263 case SMB_INFO_STANDARD:
1266 md_get_uint16le(mbp, &date);
1267 md_get_uint16le(mbp, &time); /* creation time */
1268 md_get_uint16le(mbp, &date);
1269 md_get_uint16le(mbp, &time); /* access time */
1270 smb_dos2unixtime(date, time, 0, svtz, &ctx->f_attr.fa_atime);
1271 md_get_uint16le(mbp, &date);
1272 md_get_uint16le(mbp, &time); /* access time */
1273 smb_dos2unixtime(date, time, 0, svtz, &ctx->f_attr.fa_mtime);
1274 md_get_uint32le(mbp, &size);
1275 ctx->f_attr.fa_size = size;
1276 md_get_uint32(mbp, NULL); /* allocation size */
1277 md_get_uint16le(mbp, &wattr);
1278 ctx->f_attr.fa_attr = wattr;
1279 md_get_uint8(mbp, &tb);
1282 recsz = next = 24 + nmlen; /* docs misses zero byte at end */
1284 case SMB_FIND_FILE_DIRECTORY_INFO:
1285 md_get_uint32le(mbp, &next);
1286 md_get_uint32(mbp, NULL); /* file index */
1287 md_get_int64(mbp, NULL); /* creation time */
1288 md_get_int64le(mbp, &lint);
1289 smb_time_NT2local(lint, svtz, &ctx->f_attr.fa_atime);
1290 md_get_int64le(mbp, &lint);
1291 smb_time_NT2local(lint, svtz, &ctx->f_attr.fa_mtime);
1292 md_get_int64le(mbp, &lint);
1293 smb_time_NT2local(lint, svtz, &ctx->f_attr.fa_ctime);
1294 md_get_int64le(mbp, &lint); /* file size */
1295 ctx->f_attr.fa_size = lint;
1296 md_get_int64(mbp, NULL); /* real size (should use) */
1297 md_get_uint32le(mbp, &dattr); /* EA */
1298 ctx->f_attr.fa_attr = dattr;
1299 md_get_uint32le(mbp, &size); /* name len */
1301 recsz = next ? next : fxsz + size;
1304 SMBERROR("unexpected info level %d\n", ctx->f_infolevel);
1307 if (SMB_UNICODE_STRINGS(SSTOVC(ctx->f_ssp))) {
1308 nmlen = min(size, SMB_MAXFNAMELEN * 2);
1310 nmlen = min(size, SMB_MAXFNAMELEN);
1312 error = md_get_mem(mbp, cp, nmlen, MB_MSYSTEM);
1316 cnt = next - nmlen - fxsz;
1318 md_get_mem(mbp, NULL, cnt, MB_MSYSTEM);
1320 SMBERROR("out of sync\n");
1324 if (SMB_UNICODE_STRINGS(SSTOVC(ctx->f_ssp))) {
1325 if (nmlen > 1 && cp[nmlen - 1] == 0 && cp[nmlen - 2] == 0)
1328 if (nmlen && cp[nmlen - 1] == 0)
1333 next = ctx->f_eofs + recsz;
1334 if (ctx->f_rnameofs && (ctx->f_flags & SMBFS_RDD_GOTRNAME) == 0 &&
1335 (ctx->f_rnameofs >= ctx->f_eofs && ctx->f_rnameofs < next)) {
1337 * Server needs a resume filename.
1339 if (ctx->f_rnamelen <= nmlen) {
1341 free(ctx->f_rname, M_SMBFSDATA);
1342 ctx->f_rname = malloc(nmlen + 1, M_SMBFSDATA, M_WAITOK);
1343 ctx->f_rnamelen = nmlen;
1345 bcopy(ctx->f_name, ctx->f_rname, nmlen);
1346 ctx->f_rname[nmlen] = 0;
1347 ctx->f_flags |= SMBFS_RDD_GOTRNAME;
1349 ctx->f_nmlen = nmlen;
1357 smbfs_findcloseLM2(struct smbfs_fctx *ctx)
1360 free(ctx->f_name, M_SMBFSDATA);
1362 smb_t2_done(ctx->f_t2);
1363 if ((ctx->f_flags & SMBFS_RDD_NOCLOSE) == 0)
1364 smbfs_smb_findclose2(ctx);
1369 smbfs_findopen(struct smbnode *dnp, const char *wildcard, int wclen, int attr,
1370 struct smb_cred *scred, struct smbfs_fctx **ctxpp)
1372 struct smbfs_fctx *ctx;
1375 ctx = malloc(sizeof(*ctx), M_SMBFSDATA, M_WAITOK | M_ZERO);
1376 ctx->f_ssp = dnp->n_mount->sm_share;
1378 ctx->f_flags = SMBFS_RDD_FINDFIRST;
1379 ctx->f_scred = scred;
1380 if (SMB_DIALECT(SSTOVC(ctx->f_ssp)) < SMB_DIALECT_LANMAN2_0 ||
1381 (dnp->n_mount->sm_flags & SMBFS_MOUNT_NO_LONG)) {
1382 ctx->f_flags |= SMBFS_RDD_USESEARCH;
1383 error = smbfs_findopenLM1(ctx, dnp, wildcard, wclen, attr, scred);
1385 error = smbfs_findopenLM2(ctx, dnp, wildcard, wclen, attr, scred);
1387 smbfs_findclose(ctx, scred);
1394 smbfs_findnext(struct smbfs_fctx *ctx, int limit, struct smb_cred *scred)
1401 limit *= 4; /* imperical */
1402 ctx->f_scred = scred;
1404 if (ctx->f_flags & SMBFS_RDD_USESEARCH) {
1405 error = smbfs_findnextLM1(ctx, limit);
1407 error = smbfs_findnextLM2(ctx, limit);
1410 if (SMB_UNICODE_STRINGS(SSTOVC(ctx->f_ssp))) {
1411 if ((ctx->f_nmlen == 2 &&
1412 *(u_int16_t *)ctx->f_name == htole16(0x002e)) ||
1413 (ctx->f_nmlen == 4 &&
1414 *(u_int32_t *)ctx->f_name == htole32(0x002e002e)))
1417 if ((ctx->f_nmlen == 1 && ctx->f_name[0] == '.') ||
1418 (ctx->f_nmlen == 2 && ctx->f_name[0] == '.' &&
1419 ctx->f_name[1] == '.'))
1423 smbfs_fname_tolocal(SSTOVC(ctx->f_ssp), ctx->f_name, &ctx->f_nmlen,
1424 ctx->f_dnp->n_mount->sm_caseopt);
1425 ctx->f_attr.fa_ino = smbfs_getino(ctx->f_dnp, ctx->f_name, ctx->f_nmlen);
1430 smbfs_findclose(struct smbfs_fctx *ctx, struct smb_cred *scred)
1432 ctx->f_scred = scred;
1433 if (ctx->f_flags & SMBFS_RDD_USESEARCH) {
1434 smbfs_findcloseLM1(ctx);
1436 smbfs_findcloseLM2(ctx);
1438 free(ctx->f_rname, M_SMBFSDATA);
1439 free(ctx, M_SMBFSDATA);
1444 smbfs_smb_lookup(struct smbnode *dnp, const char *name, int nmlen,
1445 struct smbfattr *fap, struct smb_cred *scred)
1447 struct smbfs_fctx *ctx;
1450 if (dnp == NULL || (dnp->n_ino == 2 && name == NULL)) {
1451 bzero(fap, sizeof(*fap));
1452 fap->fa_attr = SMB_FA_DIR;
1456 MPASS(!(nmlen == 2 && name[0] == '.' && name[1] == '.'));
1457 MPASS(!(nmlen == 1 && name[0] == '.'));
1458 ASSERT_VOP_ELOCKED(dnp->n_vnode, "smbfs_smb_lookup");
1459 error = smbfs_findopen(dnp, name, nmlen,
1460 SMB_FA_SYSTEM | SMB_FA_HIDDEN | SMB_FA_DIR, scred, &ctx);
1463 ctx->f_flags |= SMBFS_RDD_FINDSINGLE;
1464 error = smbfs_findnext(ctx, 1, scred);
1468 fap->fa_ino = dnp->n_ino;
1470 smbfs_findclose(ctx, scred);