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;
97 rqp = malloc(sizeof(struct smb_rq), M_SMBFSDATA, M_WAITOK);
98 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_LOCKING_ANDX, scred);
100 free(rqp, M_SMBFSDATA);
103 smb_rq_getrequest(rqp, &mbp);
105 mb_put_uint8(mbp, 0xff); /* secondary command */
106 mb_put_uint8(mbp, 0); /* MBZ */
107 mb_put_uint16le(mbp, 0);
108 mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
109 mb_put_uint8(mbp, ltype); /* locktype */
110 mb_put_uint8(mbp, 0); /* oplocklevel - 0 seems is NO_OPLOCK */
111 mb_put_uint32le(mbp, 0); /* timeout - break immediately */
112 mb_put_uint16le(mbp, op == SMB_LOCK_RELEASE ? 1 : 0);
113 mb_put_uint16le(mbp, op == SMB_LOCK_RELEASE ? 0 : 1);
116 mb_put_uint16le(mbp, pid);
117 mb_put_uint32le(mbp, start);
118 mb_put_uint32le(mbp, end - start);
120 error = smb_rq_simple(rqp);
122 free(rqp, M_SMBFSDATA);
127 smbfs_smb_lock(struct smbnode *np, int op, caddr_t id,
128 off_t start, off_t end, struct smb_cred *scred)
130 struct smb_share *ssp = np->n_mount->sm_share;
132 if (SMB_DIALECT(SSTOVC(ssp)) < SMB_DIALECT_LANMAN1_0)
134 * TODO: use LOCK_BYTE_RANGE here.
138 return smbfs_smb_lockandx(np, op, (uintptr_t)id, start, end, scred);
142 smbfs_smb_statfs2(struct smb_share *ssp, struct statfs *sbp,
143 struct smb_cred *scred)
145 struct smb_t2rq *t2p;
149 u_int32_t units, bpu, funits;
152 error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_QUERY_FS_INFORMATION,
156 mbp = &t2p->t2_tparam;
158 mb_put_uint16le(mbp, SMB_INFO_ALLOCATION);
159 t2p->t2_maxpcount = 4;
160 t2p->t2_maxdcount = 4 * 4 + 2;
161 error = smb_t2_request(t2p);
166 mdp = &t2p->t2_rdata;
167 md_get_uint32(mdp, NULL); /* fs id */
168 md_get_uint32le(mdp, &bpu);
169 md_get_uint32le(mdp, &units);
170 md_get_uint32le(mdp, &funits);
171 md_get_uint16le(mdp, &bsize);
172 sbp->f_bsize = bpu * bsize; /* fundamental filesystem block size */
173 sbp->f_blocks= units; /* total data blocks in filesystem */
174 sbp->f_bfree = funits; /* free blocks in fs */
175 sbp->f_bavail= funits; /* free blocks avail to non-superuser */
176 sbp->f_files = 0xffff; /* total file nodes in filesystem */
177 sbp->f_ffree = 0xffff; /* free file nodes in fs */
183 smbfs_smb_statfs(struct smb_share *ssp, struct statfs *sbp,
184 struct smb_cred *scred)
188 u_int16_t units, bpu, bsize, funits;
191 rqp = malloc(sizeof(struct smb_rq), M_SMBFSDATA, M_WAITOK);
192 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_QUERY_INFORMATION_DISK, scred);
194 free(rqp, M_SMBFSDATA);
201 error = smb_rq_simple(rqp);
203 free(rqp, M_SMBFSDATA);
207 smb_rq_getreply(rqp, &mdp);
208 md_get_uint16le(mdp, &units);
209 md_get_uint16le(mdp, &bpu);
210 md_get_uint16le(mdp, &bsize);
211 md_get_uint16le(mdp, &funits);
212 sbp->f_bsize = bpu * bsize; /* fundamental filesystem block size */
213 sbp->f_blocks= units; /* total data blocks in filesystem */
214 sbp->f_bfree = funits; /* free blocks in fs */
215 sbp->f_bavail= funits; /* free blocks avail to non-superuser */
216 sbp->f_files = 0xffff; /* total file nodes in filesystem */
217 sbp->f_ffree = 0xffff; /* free file nodes in fs */
219 free(rqp, M_SMBFSDATA);
224 smbfs_smb_seteof(struct smbnode *np, int64_t newsize, struct smb_cred *scred)
226 struct smb_t2rq *t2p;
227 struct smb_share *ssp = np->n_mount->sm_share;
231 error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_SET_FILE_INFORMATION,
235 mbp = &t2p->t2_tparam;
237 mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
238 mb_put_uint16le(mbp, SMB_SET_FILE_END_OF_FILE_INFO);
239 mb_put_uint32le(mbp, 0);
240 mbp = &t2p->t2_tdata;
242 mb_put_int64le(mbp, newsize);
243 mb_put_uint32le(mbp, 0); /* padding */
244 mb_put_uint16le(mbp, 0);
245 t2p->t2_maxpcount = 2;
246 t2p->t2_maxdcount = 0;
247 error = smb_t2_request(t2p);
253 smb_smb_flush(struct smbnode *np, struct smb_cred *scred)
255 struct smb_share *ssp = np->n_mount->sm_share;
260 if ((np->n_flag & NOPEN) == 0 || !SMBTOV(np) ||
261 SMBTOV(np)->v_type != VREG)
262 return 0; /* not a regular open file */
263 rqp = malloc(sizeof(struct smb_rq), M_SMBFSDATA, M_WAITOK);
264 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_FLUSH, scred);
266 free(rqp, M_SMBFSDATA);
269 smb_rq_getrequest(rqp, &mbp);
271 mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
275 error = smb_rq_simple(rqp);
277 free(rqp, M_SMBFSDATA);
279 np->n_flag &= ~NFLUSHWIRE;
284 smbfs_smb_flush(struct smbnode *np, struct smb_cred *scred)
286 if (np->n_flag & NFLUSHWIRE)
287 return (smb_smb_flush(np, scred));
292 smbfs_smb_setfsize(struct smbnode *np, int newsize, struct smb_cred *scred)
294 struct smb_share *ssp = np->n_mount->sm_share;
299 if (!smbfs_smb_seteof(np, (int64_t) newsize, scred)) {
300 np->n_flag |= NFLUSHWIRE;
304 rqp = malloc(sizeof(struct smb_rq), M_SMBFSDATA, M_WAITOK);
305 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_WRITE, scred);
307 free(rqp, M_SMBFSDATA);
310 smb_rq_getrequest(rqp, &mbp);
312 mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
313 mb_put_uint16le(mbp, 0);
314 mb_put_uint32le(mbp, newsize);
315 mb_put_uint16le(mbp, 0);
318 mb_put_uint8(mbp, SMB_DT_DATA);
319 mb_put_uint16le(mbp, 0);
321 error = smb_rq_simple(rqp);
323 free(rqp, M_SMBFSDATA);
328 smbfs_smb_query_info(struct smbnode *np, const char *name, int len,
329 struct smbfattr *fap, struct smb_cred *scred)
332 struct smb_share *ssp = np->n_mount->sm_share;
340 rqp = malloc(sizeof(struct smb_rq), M_SMBFSDATA, M_WAITOK);
341 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_QUERY_INFORMATION, scred);
343 free(rqp, M_SMBFSDATA);
346 smb_rq_getrequest(rqp, &mbp);
350 mb_put_uint8(mbp, SMB_DT_ASCII);
352 error = smbfs_fullpath(mbp, SSTOVC(ssp), np, name, len);
356 error = smb_rq_simple(rqp);
359 smb_rq_getreply(rqp, &mdp);
360 if (md_get_uint8(mdp, &wc) != 0 || wc != 10) {
364 md_get_uint16le(mdp, &wattr);
365 fap->fa_attr = wattr;
367 * Be careful using the time returned here, as
368 * with FAT on NT4SP6, at least, the time returned is low
369 * 32 bits of 100s of nanoseconds (since 1601) so it rolls
370 * over about every seven minutes!
372 md_get_uint32le(mdp, &lint); /* specs: secs since 1970 */
373 if (lint) /* avoid bogus zero returns */
374 smb_time_server2local(lint, SSTOVC(ssp)->vc_sopt.sv_tz,
376 md_get_uint32le(mdp, &lint);
380 free(rqp, M_SMBFSDATA);
385 * Set DOS file attributes. mtime should be NULL for dialects above lm10
388 smbfs_smb_setpattr(struct smbnode *np, u_int16_t attr, struct timespec *mtime,
389 struct smb_cred *scred)
392 struct smb_share *ssp = np->n_mount->sm_share;
397 rqp = malloc(sizeof(struct smb_rq), M_SMBFSDATA, M_WAITOK);
398 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_SET_INFORMATION, scred);
400 free(rqp, M_SMBFSDATA);
403 svtz = SSTOVC(ssp)->vc_sopt.sv_tz;
404 smb_rq_getrequest(rqp, &mbp);
406 mb_put_uint16le(mbp, attr);
408 smb_time_local2server(mtime, svtz, &time);
411 mb_put_uint32le(mbp, time); /* mtime */
412 mb_put_mem(mbp, NULL, 5 * 2, MB_MZERO);
415 mb_put_uint8(mbp, SMB_DT_ASCII);
417 error = smbfs_fullpath(mbp, SSTOVC(ssp), np, NULL, 0);
420 mb_put_uint8(mbp, SMB_DT_ASCII);
421 if (SMB_UNICODE_STRINGS(SSTOVC(ssp))) {
423 mb_put_uint8(mbp, 0); /* 1st byte of NULL Unicode char */
425 mb_put_uint8(mbp, 0);
427 error = smb_rq_simple(rqp);
429 SMBERROR("smb_rq_simple(rqp) => error %d\n", error);
434 free(rqp, M_SMBFSDATA);
439 * Note, win95 doesn't support this call.
442 smbfs_smb_setptime2(struct smbnode *np, struct timespec *mtime,
443 struct timespec *atime, int attr, struct smb_cred *scred)
445 struct smb_t2rq *t2p;
446 struct smb_share *ssp = np->n_mount->sm_share;
447 struct smb_vc *vcp = SSTOVC(ssp);
449 u_int16_t date, time;
452 error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_SET_PATH_INFORMATION,
456 mbp = &t2p->t2_tparam;
458 mb_put_uint16le(mbp, SMB_INFO_STANDARD);
459 mb_put_uint32le(mbp, 0); /* MBZ */
460 /* mb_put_uint8(mbp, SMB_DT_ASCII); specs incorrect */
461 error = smbfs_fullpath(mbp, vcp, np, NULL, 0);
466 tzoff = vcp->vc_sopt.sv_tz;
467 mbp = &t2p->t2_tdata;
469 mb_put_uint32le(mbp, 0); /* creation time */
471 smb_time_unix2dos(atime, tzoff, &date, &time, NULL);
474 mb_put_uint16le(mbp, date);
475 mb_put_uint16le(mbp, time);
477 smb_time_unix2dos(mtime, tzoff, &date, &time, NULL);
480 mb_put_uint16le(mbp, date);
481 mb_put_uint16le(mbp, time);
482 mb_put_uint32le(mbp, 0); /* file size */
483 mb_put_uint32le(mbp, 0); /* allocation unit size */
484 mb_put_uint16le(mbp, attr); /* DOS attr */
485 mb_put_uint32le(mbp, 0); /* EA size */
486 t2p->t2_maxpcount = 5 * 2;
487 t2p->t2_maxdcount = vcp->vc_txmax;
488 error = smb_t2_request(t2p);
494 * NT level. Specially for win9x
497 smbfs_smb_setpattrNT(struct smbnode *np, u_short attr, struct timespec *mtime,
498 struct timespec *atime, struct smb_cred *scred)
500 struct smb_t2rq *t2p;
501 struct smb_share *ssp = np->n_mount->sm_share;
502 struct smb_vc *vcp = SSTOVC(ssp);
507 error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_SET_PATH_INFORMATION,
511 mbp = &t2p->t2_tparam;
513 mb_put_uint16le(mbp, SMB_SET_FILE_BASIC_INFO);
514 mb_put_uint32le(mbp, 0); /* MBZ */
515 /* mb_put_uint8(mbp, SMB_DT_ASCII); specs incorrect */
516 error = smbfs_fullpath(mbp, vcp, np, NULL, 0);
521 tzoff = vcp->vc_sopt.sv_tz;
522 mbp = &t2p->t2_tdata;
524 mb_put_int64le(mbp, 0); /* creation time */
526 smb_time_local2NT(atime, tzoff, &tm);
529 mb_put_int64le(mbp, tm);
531 smb_time_local2NT(mtime, tzoff, &tm);
534 mb_put_int64le(mbp, tm);
535 mb_put_int64le(mbp, tm); /* change time */
536 mb_put_uint32le(mbp, attr); /* attr */
537 t2p->t2_maxpcount = 24;
538 t2p->t2_maxdcount = 56;
539 error = smb_t2_request(t2p);
545 * Set file atime and mtime. Doesn't supported by core dialect.
548 smbfs_smb_setftime(struct smbnode *np, struct timespec *mtime,
549 struct timespec *atime, struct smb_cred *scred)
552 struct smb_share *ssp = np->n_mount->sm_share;
554 u_int16_t date, time;
557 rqp = malloc(sizeof(struct smb_rq), M_SMBFSDATA, M_WAITOK);
558 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_SET_INFORMATION2, scred);
560 free(rqp, M_SMBFSDATA);
563 tzoff = SSTOVC(ssp)->vc_sopt.sv_tz;
564 smb_rq_getrequest(rqp, &mbp);
566 mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
567 mb_put_uint32le(mbp, 0); /* creation time */
570 smb_time_unix2dos(atime, tzoff, &date, &time, NULL);
573 mb_put_uint16le(mbp, date);
574 mb_put_uint16le(mbp, time);
576 smb_time_unix2dos(mtime, tzoff, &date, &time, NULL);
579 mb_put_uint16le(mbp, date);
580 mb_put_uint16le(mbp, time);
584 error = smb_rq_simple(rqp);
585 SMBSDEBUG("%d\n", error);
587 free(rqp, M_SMBFSDATA);
592 * Set DOS file attributes.
593 * Looks like this call can be used only if SMB_CAP_NT_SMBS bit is on.
596 smbfs_smb_setfattrNT(struct smbnode *np, u_int16_t attr, struct timespec *mtime,
597 struct timespec *atime, struct smb_cred *scred)
599 struct smb_t2rq *t2p;
600 struct smb_share *ssp = np->n_mount->sm_share;
605 error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_SET_FILE_INFORMATION,
609 svtz = SSTOVC(ssp)->vc_sopt.sv_tz;
610 mbp = &t2p->t2_tparam;
612 mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
613 mb_put_uint16le(mbp, SMB_SET_FILE_BASIC_INFO);
614 mb_put_uint32le(mbp, 0);
615 mbp = &t2p->t2_tdata;
617 mb_put_int64le(mbp, 0); /* creation time */
619 smb_time_local2NT(atime, svtz, &tm);
622 mb_put_int64le(mbp, tm);
624 smb_time_local2NT(mtime, svtz, &tm);
627 mb_put_int64le(mbp, tm);
628 mb_put_int64le(mbp, tm); /* change time */
629 mb_put_uint16le(mbp, attr);
630 mb_put_uint32le(mbp, 0); /* padding */
631 mb_put_uint16le(mbp, 0);
632 t2p->t2_maxpcount = 2;
633 t2p->t2_maxdcount = 0;
634 error = smb_t2_request(t2p);
641 smbfs_smb_open(struct smbnode *np, int accmode, struct smb_cred *scred)
644 struct smb_share *ssp = np->n_mount->sm_share;
648 u_int16_t fid, wattr, grantedmode;
651 rqp = malloc(sizeof(struct smb_rq), M_SMBFSDATA, M_WAITOK);
652 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_OPEN, scred);
654 free(rqp, M_SMBFSDATA);
657 smb_rq_getrequest(rqp, &mbp);
659 mb_put_uint16le(mbp, accmode);
660 mb_put_uint16le(mbp, SMB_FA_SYSTEM | SMB_FA_HIDDEN);
663 mb_put_uint8(mbp, SMB_DT_ASCII);
665 error = smbfs_fullpath(mbp, SSTOVC(ssp), np, NULL, 0);
669 error = smb_rq_simple(rqp);
672 smb_rq_getreply(rqp, &mdp);
673 if (md_get_uint8(mdp, &wc) != 0 || wc != 7) {
677 md_get_uint16(mdp, &fid);
678 md_get_uint16le(mdp, &wattr);
679 md_get_uint32(mdp, NULL); /* mtime */
680 md_get_uint32(mdp, NULL); /* fsize */
681 md_get_uint16le(mdp, &grantedmode);
683 * TODO: refresh attributes from this reply
687 free(rqp, M_SMBFSDATA);
691 np->n_rwstate = grantedmode;
697 smbfs_smb_close(struct smb_share *ssp, u_int16_t fid, struct timespec *mtime,
698 struct smb_cred *scred)
705 rqp = malloc(sizeof(struct smb_rq), M_SMBFSDATA, M_WAITOK);
706 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_CLOSE, scred);
708 free(rqp, M_SMBFSDATA);
711 smb_rq_getrequest(rqp, &mbp);
713 mb_put_mem(mbp, (caddr_t)&fid, sizeof(fid), MB_MSYSTEM);
715 smb_time_local2server(mtime, SSTOVC(ssp)->vc_sopt.sv_tz, &time);
718 mb_put_uint32le(mbp, time);
722 error = smb_rq_simple(rqp);
724 free(rqp, M_SMBFSDATA);
729 smbfs_smb_create(struct smbnode *dnp, const char *name, int nmlen,
730 struct smb_cred *scred)
733 struct smb_share *ssp = dnp->n_mount->sm_share;
736 struct timespec ctime;
742 rqp = malloc(sizeof(struct smb_rq), M_SMBFSDATA, M_WAITOK);
743 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_CREATE, scred);
745 free(rqp, M_SMBFSDATA);
748 smb_rq_getrequest(rqp, &mbp);
750 mb_put_uint16le(mbp, SMB_FA_ARCHIVE); /* attributes */
752 smb_time_local2server(&ctime, SSTOVC(ssp)->vc_sopt.sv_tz, &tm);
753 mb_put_uint32le(mbp, tm);
756 mb_put_uint8(mbp, SMB_DT_ASCII);
757 error = smbfs_fullpath(mbp, SSTOVC(ssp), dnp, name, nmlen);
760 error = smb_rq_simple(rqp);
762 smb_rq_getreply(rqp, &mdp);
763 md_get_uint8(mdp, &wc);
765 md_get_uint16(mdp, &fid);
773 smbfs_smb_close(ssp, fid, &ctime, scred);
774 free(rqp, M_SMBFSDATA);
779 smbfs_smb_delete(struct smbnode *np, struct smb_cred *scred)
782 struct smb_share *ssp = np->n_mount->sm_share;
786 rqp = malloc(sizeof(struct smb_rq), M_SMBFSDATA, M_WAITOK);
787 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_DELETE, scred);
789 free(rqp, M_SMBFSDATA);
792 smb_rq_getrequest(rqp, &mbp);
794 mb_put_uint16le(mbp, SMB_FA_SYSTEM | SMB_FA_HIDDEN);
797 mb_put_uint8(mbp, SMB_DT_ASCII);
798 error = smbfs_fullpath(mbp, SSTOVC(ssp), np, NULL, 0);
801 error = smb_rq_simple(rqp);
804 free(rqp, M_SMBFSDATA);
809 smbfs_smb_rename(struct smbnode *src, struct smbnode *tdnp,
810 const char *tname, int tnmlen, struct smb_cred *scred)
813 struct smb_share *ssp = src->n_mount->sm_share;
817 rqp = malloc(sizeof(struct smb_rq), M_SMBFSDATA, M_WAITOK);
818 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_RENAME, scred);
820 free(rqp, M_SMBFSDATA);
823 smb_rq_getrequest(rqp, &mbp);
825 mb_put_uint16le(mbp, SMB_FA_SYSTEM | SMB_FA_HIDDEN);
828 mb_put_uint8(mbp, SMB_DT_ASCII);
830 error = smbfs_fullpath(mbp, SSTOVC(ssp), src, NULL, 0);
833 mb_put_uint8(mbp, SMB_DT_ASCII);
834 error = smbfs_fullpath(mbp, SSTOVC(ssp), tdnp, tname, tnmlen);
838 error = smb_rq_simple(rqp);
841 free(rqp, M_SMBFSDATA);
846 smbfs_smb_move(struct smbnode *src, struct smbnode *tdnp,
847 const char *tname, int tnmlen, u_int16_t flags, struct smb_cred *scred)
850 struct smb_share *ssp = src->n_mount->sm_share;
854 rqp = malloc(sizeof(struct smb_rq), M_SMBFSDATA, M_WAITOK);
855 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_MOVE, scred);
857 free(rqp, M_SMBFSDATA);
860 smb_rq_getrequest(rqp, &mbp);
862 mb_put_uint16le(mbp, SMB_TID_UNKNOWN);
863 mb_put_uint16le(mbp, 0x20); /* delete target file */
864 mb_put_uint16le(mbp, flags);
867 mb_put_uint8(mbp, SMB_DT_ASCII);
869 error = smbfs_fullpath(mbp, SSTOVC(ssp), src, NULL, 0);
872 mb_put_uint8(mbp, SMB_DT_ASCII);
873 error = smbfs_fullpath(mbp, SSTOVC(ssp), tdnp, tname, tnmlen);
877 error = smb_rq_simple(rqp);
880 free(rqp, M_SMBFSDATA);
885 smbfs_smb_mkdir(struct smbnode *dnp, const char *name, int len,
886 struct smb_cred *scred)
889 struct smb_share *ssp = dnp->n_mount->sm_share;
893 rqp = malloc(sizeof(struct smb_rq), M_SMBFSDATA, M_WAITOK);
894 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_CREATE_DIRECTORY, scred);
896 free(rqp, M_SMBFSDATA);
899 smb_rq_getrequest(rqp, &mbp);
903 mb_put_uint8(mbp, SMB_DT_ASCII);
904 error = smbfs_fullpath(mbp, SSTOVC(ssp), dnp, name, len);
907 error = smb_rq_simple(rqp);
910 free(rqp, M_SMBFSDATA);
915 smbfs_smb_rmdir(struct smbnode *np, struct smb_cred *scred)
918 struct smb_share *ssp = np->n_mount->sm_share;
922 rqp = malloc(sizeof(struct smb_rq), M_SMBFSDATA, M_WAITOK);
923 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_DELETE_DIRECTORY, scred);
925 free(rqp, M_SMBFSDATA);
928 smb_rq_getrequest(rqp, &mbp);
932 mb_put_uint8(mbp, SMB_DT_ASCII);
933 error = smbfs_fullpath(mbp, SSTOVC(ssp), np, NULL, 0);
936 error = smb_rq_simple(rqp);
939 free(rqp, M_SMBFSDATA);
944 smbfs_smb_search(struct smbfs_fctx *ctx)
946 struct smb_vc *vcp = SSTOVC(ctx->f_ssp);
951 u_int16_t ec, dlen, bc;
952 int maxent, error, iseof = 0;
954 maxent = min(ctx->f_left, (vcp->vc_txmax - SMB_HDRLEN - 3) / SMB_DENTRYLEN);
956 smb_rq_done(ctx->f_rq);
959 error = smb_rq_alloc(SSTOCP(ctx->f_ssp), SMB_COM_SEARCH, ctx->f_scred, &rqp);
963 smb_rq_getrequest(rqp, &mbp);
965 mb_put_uint16le(mbp, maxent); /* max entries to return */
966 mb_put_uint16le(mbp, ctx->f_attrmask);
969 mb_put_uint8(mbp, SMB_DT_ASCII); /* buffer format */
970 if (ctx->f_flags & SMBFS_RDD_FINDFIRST) {
971 error = smbfs_fullpath(mbp, vcp, ctx->f_dnp, ctx->f_wildcard, ctx->f_wclen);
974 mb_put_uint8(mbp, SMB_DT_VARIABLE);
975 mb_put_uint16le(mbp, 0); /* context length */
976 ctx->f_flags &= ~SMBFS_RDD_FINDFIRST;
978 if (SMB_UNICODE_STRINGS(vcp)) {
980 mb_put_uint8(mbp, 0);
982 mb_put_uint8(mbp, 0); /* file name length */
983 mb_put_uint8(mbp, SMB_DT_VARIABLE);
984 mb_put_uint16le(mbp, SMB_SKEYLEN);
985 mb_put_mem(mbp, ctx->f_skey, SMB_SKEYLEN, MB_MSYSTEM);
988 error = smb_rq_simple(rqp);
990 if (rqp->sr_errclass == ERRDOS && rqp->sr_serror == ERRnofiles) {
993 ctx->f_flags |= SMBFS_RDD_EOF;
997 smb_rq_getreply(rqp, &mdp);
998 md_get_uint8(mdp, &wc);
1000 return iseof ? ENOENT : EBADRPC;
1001 md_get_uint16le(mdp, &ec);
1005 md_get_uint16le(mdp, &bc);
1009 md_get_uint8(mdp, &bt);
1010 if (bt != SMB_DT_VARIABLE)
1012 md_get_uint16le(mdp, &dlen);
1013 if (dlen != bc || dlen % SMB_DENTRYLEN != 0)
1019 smbfs_findopenLM1(struct smbfs_fctx *ctx, struct smbnode *dnp,
1020 const char *wildcard, int wclen, int attr, struct smb_cred *scred)
1022 ctx->f_attrmask = attr;
1024 if (wclen == 1 && wildcard[0] == '*') {
1025 ctx->f_wildcard = "*.*";
1028 ctx->f_wildcard = wildcard;
1029 ctx->f_wclen = wclen;
1032 ctx->f_wildcard = NULL;
1035 ctx->f_name = ctx->f_fname;
1040 smbfs_findnextLM1(struct smbfs_fctx *ctx, int limit)
1042 struct mdchain *mbp;
1046 u_int16_t date, time;
1050 if (ctx->f_ecnt == 0) {
1051 if (ctx->f_flags & SMBFS_RDD_EOF)
1053 ctx->f_left = ctx->f_limit = limit;
1054 error = smbfs_smb_search(ctx);
1059 smb_rq_getreply(rqp, &mbp);
1060 md_get_mem(mbp, ctx->f_skey, SMB_SKEYLEN, MB_MSYSTEM);
1061 md_get_uint8(mbp, &battr);
1062 md_get_uint16le(mbp, &time);
1063 md_get_uint16le(mbp, &date);
1064 md_get_uint32le(mbp, &size);
1066 md_get_mem(mbp, cp, sizeof(ctx->f_fname), MB_MSYSTEM);
1067 cp[sizeof(ctx->f_fname) - 1] = 0;
1068 cp += strlen(cp) - 1;
1069 while (*cp == ' ' && cp >= ctx->f_name)
1071 ctx->f_attr.fa_attr = battr;
1072 smb_dos2unixtime(date, time, 0, rqp->sr_vc->vc_sopt.sv_tz,
1073 &ctx->f_attr.fa_mtime);
1074 ctx->f_attr.fa_size = size;
1075 ctx->f_nmlen = strlen(ctx->f_name);
1082 smbfs_findcloseLM1(struct smbfs_fctx *ctx)
1085 smb_rq_done(ctx->f_rq);
1090 * TRANS2_FIND_FIRST2/NEXT2, used for NT LM12 dialect
1093 smbfs_smb_trans2find2(struct smbfs_fctx *ctx)
1095 struct smb_t2rq *t2p;
1096 struct smb_vc *vcp = SSTOVC(ctx->f_ssp);
1097 struct mbchain *mbp;
1098 struct mdchain *mdp;
1099 u_int16_t tw, flags;
1103 smb_t2_done(ctx->f_t2);
1106 ctx->f_flags &= ~SMBFS_RDD_GOTRNAME;
1107 flags = 8 | 2; /* <resume> | <close if EOS> */
1108 if (ctx->f_flags & SMBFS_RDD_FINDSINGLE) {
1109 flags |= 1; /* close search after this request */
1110 ctx->f_flags |= SMBFS_RDD_NOCLOSE;
1112 if (ctx->f_flags & SMBFS_RDD_FINDFIRST) {
1113 error = smb_t2_alloc(SSTOCP(ctx->f_ssp), SMB_TRANS2_FIND_FIRST2,
1114 ctx->f_scred, &t2p);
1118 mbp = &t2p->t2_tparam;
1120 mb_put_uint16le(mbp, ctx->f_attrmask);
1121 mb_put_uint16le(mbp, ctx->f_limit);
1122 mb_put_uint16le(mbp, flags);
1123 mb_put_uint16le(mbp, ctx->f_infolevel);
1124 mb_put_uint32le(mbp, 0);
1125 error = smbfs_fullpath(mbp, vcp, ctx->f_dnp, ctx->f_wildcard, ctx->f_wclen);
1129 error = smb_t2_alloc(SSTOCP(ctx->f_ssp), SMB_TRANS2_FIND_NEXT2,
1130 ctx->f_scred, &t2p);
1134 mbp = &t2p->t2_tparam;
1136 mb_put_mem(mbp, (caddr_t)&ctx->f_Sid, 2, MB_MSYSTEM);
1137 mb_put_uint16le(mbp, ctx->f_limit);
1138 mb_put_uint16le(mbp, ctx->f_infolevel);
1139 mb_put_uint32le(mbp, 0); /* resume key */
1140 mb_put_uint16le(mbp, flags);
1142 mb_put_mem(mbp, ctx->f_rname, ctx->f_rnamelen + 1, MB_MSYSTEM);
1144 mb_put_uint8(mbp, 0); /* resume file name */
1148 tv.tv_usec = 200 * 1000; /* 200ms */
1149 if (vcp->vc_flags & SMBC_WIN95) {
1151 * some implementations suggests to sleep here
1152 * for 200ms, due to the bug in the Win95.
1153 * I've didn't notice any problem, but put code
1156 pause("fix95", tvtohz(&tv));
1160 t2p->t2_maxpcount = 5 * 2;
1161 t2p->t2_maxdcount = vcp->vc_txmax;
1162 error = smb_t2_request(t2p);
1165 mdp = &t2p->t2_rparam;
1166 if (ctx->f_flags & SMBFS_RDD_FINDFIRST) {
1167 if ((error = md_get_uint16(mdp, &ctx->f_Sid)) != 0)
1169 ctx->f_flags &= ~SMBFS_RDD_FINDFIRST;
1171 if ((error = md_get_uint16le(mdp, &tw)) != 0)
1174 if ((error = md_get_uint16le(mdp, &tw)) != 0)
1177 ctx->f_flags |= SMBFS_RDD_EOF | SMBFS_RDD_NOCLOSE;
1178 if ((error = md_get_uint16le(mdp, &tw)) != 0)
1180 if ((error = md_get_uint16le(mdp, &tw)) != 0)
1182 if (ctx->f_ecnt == 0) {
1183 ctx->f_flags |= SMBFS_RDD_EOF | SMBFS_RDD_NOCLOSE;
1186 ctx->f_rnameofs = tw;
1187 mdp = &t2p->t2_rdata;
1188 if (mdp->md_top == NULL) {
1189 printf("bug: ecnt = %d, but data is NULL (please report)\n", ctx->f_ecnt);
1192 if (mdp->md_top->m_len == 0) {
1193 printf("bug: ecnt = %d, but m_len = 0 and m_next = %p (please report)\n", ctx->f_ecnt,mbp->mb_top->m_next);
1201 smbfs_smb_findclose2(struct smbfs_fctx *ctx)
1204 struct mbchain *mbp;
1207 rqp = malloc(sizeof(struct smb_rq), M_SMBFSDATA, M_WAITOK);
1208 error = smb_rq_init(rqp, SSTOCP(ctx->f_ssp), SMB_COM_FIND_CLOSE2, ctx->f_scred);
1210 free(rqp, M_SMBFSDATA);
1213 smb_rq_getrequest(rqp, &mbp);
1215 mb_put_mem(mbp, (caddr_t)&ctx->f_Sid, 2, MB_MSYSTEM);
1219 error = smb_rq_simple(rqp);
1221 free(rqp, M_SMBFSDATA);
1226 smbfs_findopenLM2(struct smbfs_fctx *ctx, struct smbnode *dnp,
1227 const char *wildcard, int wclen, int attr, struct smb_cred *scred)
1229 if (SMB_UNICODE_STRINGS(SSTOVC(ctx->f_ssp))) {
1230 ctx->f_name = malloc(SMB_MAXFNAMELEN * 2, M_SMBFSDATA, M_WAITOK);
1232 ctx->f_name = malloc(SMB_MAXFNAMELEN, M_SMBFSDATA, M_WAITOK);
1233 ctx->f_infolevel = SMB_DIALECT(SSTOVC(ctx->f_ssp)) < SMB_DIALECT_NTLM0_12 ?
1234 SMB_INFO_STANDARD : SMB_FIND_FILE_DIRECTORY_INFO;
1235 ctx->f_attrmask = attr;
1236 ctx->f_wildcard = wildcard;
1237 ctx->f_wclen = wclen;
1242 smbfs_findnextLM2(struct smbfs_fctx *ctx, int limit)
1244 struct mdchain *mbp;
1245 struct smb_t2rq *t2p;
1248 u_int16_t date, time, wattr;
1249 u_int32_t size, next, dattr;
1251 int error, svtz, cnt, fxsz, nmlen, recsz;
1253 if (ctx->f_ecnt == 0) {
1254 if (ctx->f_flags & SMBFS_RDD_EOF)
1256 ctx->f_left = ctx->f_limit = limit;
1257 error = smbfs_smb_trans2find2(ctx);
1262 mbp = &t2p->t2_rdata;
1263 svtz = SSTOVC(ctx->f_ssp)->vc_sopt.sv_tz;
1264 switch (ctx->f_infolevel) {
1265 case SMB_INFO_STANDARD:
1268 md_get_uint16le(mbp, &date);
1269 md_get_uint16le(mbp, &time); /* creation time */
1270 md_get_uint16le(mbp, &date);
1271 md_get_uint16le(mbp, &time); /* access time */
1272 smb_dos2unixtime(date, time, 0, svtz, &ctx->f_attr.fa_atime);
1273 md_get_uint16le(mbp, &date);
1274 md_get_uint16le(mbp, &time); /* access time */
1275 smb_dos2unixtime(date, time, 0, svtz, &ctx->f_attr.fa_mtime);
1276 md_get_uint32le(mbp, &size);
1277 ctx->f_attr.fa_size = size;
1278 md_get_uint32(mbp, NULL); /* allocation size */
1279 md_get_uint16le(mbp, &wattr);
1280 ctx->f_attr.fa_attr = wattr;
1281 md_get_uint8(mbp, &tb);
1284 recsz = next = 24 + nmlen; /* docs misses zero byte at end */
1286 case SMB_FIND_FILE_DIRECTORY_INFO:
1287 md_get_uint32le(mbp, &next);
1288 md_get_uint32(mbp, NULL); /* file index */
1289 md_get_int64(mbp, NULL); /* creation time */
1290 md_get_int64le(mbp, &lint);
1291 smb_time_NT2local(lint, svtz, &ctx->f_attr.fa_atime);
1292 md_get_int64le(mbp, &lint);
1293 smb_time_NT2local(lint, svtz, &ctx->f_attr.fa_mtime);
1294 md_get_int64le(mbp, &lint);
1295 smb_time_NT2local(lint, svtz, &ctx->f_attr.fa_ctime);
1296 md_get_int64le(mbp, &lint); /* file size */
1297 ctx->f_attr.fa_size = lint;
1298 md_get_int64(mbp, NULL); /* real size (should use) */
1299 md_get_uint32le(mbp, &dattr); /* EA */
1300 ctx->f_attr.fa_attr = dattr;
1301 md_get_uint32le(mbp, &size); /* name len */
1303 recsz = next ? next : fxsz + size;
1306 SMBERROR("unexpected info level %d\n", ctx->f_infolevel);
1309 if (SMB_UNICODE_STRINGS(SSTOVC(ctx->f_ssp))) {
1310 nmlen = min(size, SMB_MAXFNAMELEN * 2);
1312 nmlen = min(size, SMB_MAXFNAMELEN);
1314 error = md_get_mem(mbp, cp, nmlen, MB_MSYSTEM);
1318 cnt = next - nmlen - fxsz;
1320 md_get_mem(mbp, NULL, cnt, MB_MSYSTEM);
1322 SMBERROR("out of sync\n");
1326 if (SMB_UNICODE_STRINGS(SSTOVC(ctx->f_ssp))) {
1327 if (nmlen > 1 && cp[nmlen - 1] == 0 && cp[nmlen - 2] == 0)
1330 if (nmlen && cp[nmlen - 1] == 0)
1335 next = ctx->f_eofs + recsz;
1336 if (ctx->f_rnameofs && (ctx->f_flags & SMBFS_RDD_GOTRNAME) == 0 &&
1337 (ctx->f_rnameofs >= ctx->f_eofs && ctx->f_rnameofs < next)) {
1339 * Server needs a resume filename.
1341 if (ctx->f_rnamelen <= nmlen) {
1343 free(ctx->f_rname, M_SMBFSDATA);
1344 ctx->f_rname = malloc(nmlen + 1, M_SMBFSDATA, M_WAITOK);
1345 ctx->f_rnamelen = nmlen;
1347 bcopy(ctx->f_name, ctx->f_rname, nmlen);
1348 ctx->f_rname[nmlen] = 0;
1349 ctx->f_flags |= SMBFS_RDD_GOTRNAME;
1351 ctx->f_nmlen = nmlen;
1359 smbfs_findcloseLM2(struct smbfs_fctx *ctx)
1362 free(ctx->f_name, M_SMBFSDATA);
1364 smb_t2_done(ctx->f_t2);
1365 if ((ctx->f_flags & SMBFS_RDD_NOCLOSE) == 0)
1366 smbfs_smb_findclose2(ctx);
1371 smbfs_findopen(struct smbnode *dnp, const char *wildcard, int wclen, int attr,
1372 struct smb_cred *scred, struct smbfs_fctx **ctxpp)
1374 struct smbfs_fctx *ctx;
1377 ctx = malloc(sizeof(*ctx), M_SMBFSDATA, M_WAITOK | M_ZERO);
1378 ctx->f_ssp = dnp->n_mount->sm_share;
1380 ctx->f_flags = SMBFS_RDD_FINDFIRST;
1381 ctx->f_scred = scred;
1382 if (SMB_DIALECT(SSTOVC(ctx->f_ssp)) < SMB_DIALECT_LANMAN2_0 ||
1383 (dnp->n_mount->sm_flags & SMBFS_MOUNT_NO_LONG)) {
1384 ctx->f_flags |= SMBFS_RDD_USESEARCH;
1385 error = smbfs_findopenLM1(ctx, dnp, wildcard, wclen, attr, scred);
1387 error = smbfs_findopenLM2(ctx, dnp, wildcard, wclen, attr, scred);
1389 smbfs_findclose(ctx, scred);
1396 smbfs_findnext(struct smbfs_fctx *ctx, int limit, struct smb_cred *scred)
1403 limit *= 4; /* imperical */
1404 ctx->f_scred = scred;
1406 if (ctx->f_flags & SMBFS_RDD_USESEARCH) {
1407 error = smbfs_findnextLM1(ctx, limit);
1409 error = smbfs_findnextLM2(ctx, limit);
1412 if (SMB_UNICODE_STRINGS(SSTOVC(ctx->f_ssp))) {
1413 if ((ctx->f_nmlen == 2 &&
1414 *(u_int16_t *)ctx->f_name == htole16(0x002e)) ||
1415 (ctx->f_nmlen == 4 &&
1416 *(u_int32_t *)ctx->f_name == htole32(0x002e002e)))
1419 if ((ctx->f_nmlen == 1 && ctx->f_name[0] == '.') ||
1420 (ctx->f_nmlen == 2 && ctx->f_name[0] == '.' &&
1421 ctx->f_name[1] == '.'))
1425 smbfs_fname_tolocal(SSTOVC(ctx->f_ssp), ctx->f_name, &ctx->f_nmlen,
1426 ctx->f_dnp->n_mount->sm_caseopt);
1427 ctx->f_attr.fa_ino = smbfs_getino(ctx->f_dnp, ctx->f_name, ctx->f_nmlen);
1432 smbfs_findclose(struct smbfs_fctx *ctx, struct smb_cred *scred)
1434 ctx->f_scred = scred;
1435 if (ctx->f_flags & SMBFS_RDD_USESEARCH) {
1436 smbfs_findcloseLM1(ctx);
1438 smbfs_findcloseLM2(ctx);
1440 free(ctx->f_rname, M_SMBFSDATA);
1441 free(ctx, M_SMBFSDATA);
1446 smbfs_smb_lookup(struct smbnode *dnp, const char *name, int nmlen,
1447 struct smbfattr *fap, struct smb_cred *scred)
1449 struct smbfs_fctx *ctx;
1452 if (dnp == NULL || (dnp->n_ino == 2 && name == NULL)) {
1453 bzero(fap, sizeof(*fap));
1454 fap->fa_attr = SMB_FA_DIR;
1458 MPASS(!(nmlen == 2 && name[0] == '.' && name[1] == '.'));
1459 MPASS(!(nmlen == 1 && name[0] == '.'));
1460 ASSERT_VOP_ELOCKED(dnp->n_vnode, "smbfs_smb_lookup");
1461 error = smbfs_findopen(dnp, name, nmlen,
1462 SMB_FA_SYSTEM | SMB_FA_HIDDEN | SMB_FA_DIR, scred, &ctx);
1465 ctx->f_flags |= SMBFS_RDD_FINDSINGLE;
1466 error = smbfs_findnext(ctx, 1, scred);
1470 fap->fa_ino = dnp->n_ino;
1472 smbfs_findclose(ctx, scred);