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;
90 struct smb_rq rq, *rqp = &rq;
95 if (op == SMB_LOCK_SHARED)
96 ltype |= SMB_LOCKING_ANDX_SHARED_LOCK;
97 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_LOCKING_ANDX, scred);
100 smb_rq_getrequest(rqp, &mbp);
102 mb_put_uint8(mbp, 0xff); /* secondary command */
103 mb_put_uint8(mbp, 0); /* MBZ */
104 mb_put_uint16le(mbp, 0);
105 mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
106 mb_put_uint8(mbp, ltype); /* locktype */
107 mb_put_uint8(mbp, 0); /* oplocklevel - 0 seems is NO_OPLOCK */
108 mb_put_uint32le(mbp, 0); /* timeout - break immediately */
109 mb_put_uint16le(mbp, op == SMB_LOCK_RELEASE ? 1 : 0);
110 mb_put_uint16le(mbp, op == SMB_LOCK_RELEASE ? 0 : 1);
113 mb_put_uint16le(mbp, pid);
114 mb_put_uint32le(mbp, start);
115 mb_put_uint32le(mbp, end - start);
117 error = smb_rq_simple(rqp);
123 smbfs_smb_lock(struct smbnode *np, int op, caddr_t id,
124 off_t start, off_t end, struct smb_cred *scred)
126 struct smb_share *ssp = np->n_mount->sm_share;
128 if (SMB_DIALECT(SSTOVC(ssp)) < SMB_DIALECT_LANMAN1_0)
130 * TODO: use LOCK_BYTE_RANGE here.
134 return smbfs_smb_lockandx(np, op, (uintptr_t)id, start, end, scred);
138 smbfs_query_info_fs(struct smb_share *ssp, struct statfs *sbp,
139 struct smb_cred *scred)
141 struct smb_t2rq *t2p;
145 int64_t units, funits;
148 error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_QUERY_FS_INFORMATION,
152 mbp = &t2p->t2_tparam;
154 mb_put_uint16le(mbp, SMB_QUERY_FS_SIZE_INFO);
155 t2p->t2_maxpcount = 2;
156 t2p->t2_maxdcount = sizeof(int64_t) * 2 + sizeof(uint32_t) * 2;
157 error = smb_t2_request(t2p);
162 mdp = &t2p->t2_rdata;
163 md_get_int64le(mdp, &units);
164 md_get_int64le(mdp, &funits);
165 md_get_uint32le(mdp, &bpu);
166 md_get_uint32le(mdp, &bsize);
167 sbp->f_bsize = bpu * bsize; /* fundamental filesystem block size */
168 sbp->f_blocks= (uint64_t)units; /* total data blocks in filesystem */
169 sbp->f_bfree = (uint64_t)funits;/* free blocks in fs */
170 sbp->f_bavail= (uint64_t)funits;/* free blocks avail to non-superuser */
171 sbp->f_files = 0xffff; /* total file nodes in filesystem */
172 sbp->f_ffree = 0xffff; /* free file nodes in fs */
179 smbfs_query_info_alloc(struct smb_share *ssp, struct statfs *sbp,
180 struct smb_cred *scred)
182 struct smb_t2rq *t2p;
186 u_int32_t units, bpu, funits;
189 error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_QUERY_FS_INFORMATION,
193 mbp = &t2p->t2_tparam;
195 mb_put_uint16le(mbp, SMB_INFO_ALLOCATION);
196 t2p->t2_maxpcount = 4;
197 t2p->t2_maxdcount = 4 * 4 + 2;
198 error = smb_t2_request(t2p);
203 mdp = &t2p->t2_rdata;
204 md_get_uint32(mdp, NULL); /* fs id */
205 md_get_uint32le(mdp, &bpu);
206 md_get_uint32le(mdp, &units);
207 md_get_uint32le(mdp, &funits);
208 md_get_uint16le(mdp, &bsize);
209 sbp->f_bsize = bpu * bsize; /* fundamental filesystem block size */
210 sbp->f_blocks= units; /* total data blocks in filesystem */
211 sbp->f_bfree = funits; /* free blocks in fs */
212 sbp->f_bavail= funits; /* free blocks avail to non-superuser */
213 sbp->f_files = 0xffff; /* total file nodes in filesystem */
214 sbp->f_ffree = 0xffff; /* free file nodes in fs */
220 smbfs_query_info_disk(struct smb_share *ssp, struct statfs *sbp,
221 struct smb_cred *scred)
223 struct smb_rq rq, *rqp = &rq;
225 u_int16_t units, bpu, bsize, funits;
228 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_QUERY_INFORMATION_DISK, scred);
235 error = smb_rq_simple(rqp);
240 smb_rq_getreply(rqp, &mdp);
241 md_get_uint16le(mdp, &units);
242 md_get_uint16le(mdp, &bpu);
243 md_get_uint16le(mdp, &bsize);
244 md_get_uint16le(mdp, &funits);
245 sbp->f_bsize = bpu * bsize; /* fundamental filesystem block size */
246 sbp->f_blocks= units; /* total data blocks in filesystem */
247 sbp->f_bfree = funits; /* free blocks in fs */
248 sbp->f_bavail= funits; /* free blocks avail to non-superuser */
249 sbp->f_files = 0xffff; /* total file nodes in filesystem */
250 sbp->f_ffree = 0xffff; /* free file nodes in fs */
256 smbfs_smb_statfs(struct smb_share *ssp, struct statfs *sbp,
257 struct smb_cred *scred)
260 if (SMB_DIALECT(SSTOVC(ssp)) >= SMB_DIALECT_LANMAN2_0) {
261 if (smbfs_query_info_fs(ssp, sbp, scred) == 0)
263 if (smbfs_query_info_alloc(ssp, sbp, scred) == 0)
266 return (smbfs_query_info_disk(ssp, sbp, scred));
270 smbfs_smb_seteof(struct smbnode *np, int64_t newsize, struct smb_cred *scred)
272 struct smb_t2rq *t2p;
273 struct smb_share *ssp = np->n_mount->sm_share;
277 error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_SET_FILE_INFORMATION,
281 mbp = &t2p->t2_tparam;
283 mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
284 mb_put_uint16le(mbp, SMB_SET_FILE_END_OF_FILE_INFO);
285 mb_put_uint32le(mbp, 0);
286 mbp = &t2p->t2_tdata;
288 mb_put_int64le(mbp, newsize);
289 mb_put_uint32le(mbp, 0); /* padding */
290 mb_put_uint16le(mbp, 0);
291 t2p->t2_maxpcount = 2;
292 t2p->t2_maxdcount = 0;
293 error = smb_t2_request(t2p);
299 smb_smb_flush(struct smbnode *np, struct smb_cred *scred)
301 struct smb_share *ssp = np->n_mount->sm_share;
302 struct smb_rq rq, *rqp = &rq;
306 if ((np->n_flag & NOPEN) == 0 || !SMBTOV(np) ||
307 SMBTOV(np)->v_type != VREG)
308 return 0; /* not a regular open file */
309 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_FLUSH, scred);
312 smb_rq_getrequest(rqp, &mbp);
314 mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
318 error = smb_rq_simple(rqp);
321 np->n_flag &= ~NFLUSHWIRE;
326 smbfs_smb_flush(struct smbnode *np, struct smb_cred *scred)
328 if (np->n_flag & NFLUSHWIRE)
329 return (smb_smb_flush(np, scred));
334 smbfs_smb_setfsize(struct smbnode *np, int newsize, struct smb_cred *scred)
336 struct smb_share *ssp = np->n_mount->sm_share;
337 struct smb_rq rq, *rqp = &rq;
341 if (!smbfs_smb_seteof(np, (int64_t) newsize, scred)) {
342 np->n_flag |= NFLUSHWIRE;
346 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_WRITE, scred);
349 smb_rq_getrequest(rqp, &mbp);
351 mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
352 mb_put_uint16le(mbp, 0);
353 mb_put_uint32le(mbp, newsize);
354 mb_put_uint16le(mbp, 0);
357 mb_put_uint8(mbp, SMB_DT_DATA);
358 mb_put_uint16le(mbp, 0);
360 error = smb_rq_simple(rqp);
366 smbfs_smb_query_info(struct smbnode *np, const char *name, int len,
367 struct smbfattr *fap, struct smb_cred *scred)
369 struct smb_rq rq, *rqp = &rq;
370 struct smb_share *ssp = np->n_mount->sm_share;
378 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_QUERY_INFORMATION, scred);
381 smb_rq_getrequest(rqp, &mbp);
385 mb_put_uint8(mbp, SMB_DT_ASCII);
387 error = smbfs_fullpath(mbp, SSTOVC(ssp), np, name, len);
391 error = smb_rq_simple(rqp);
394 smb_rq_getreply(rqp, &mdp);
395 if (md_get_uint8(mdp, &wc) != 0 || wc != 10) {
399 md_get_uint16le(mdp, &wattr);
400 fap->fa_attr = wattr;
402 * Be careful using the time returned here, as
403 * with FAT on NT4SP6, at least, the time returned is low
404 * 32 bits of 100s of nanoseconds (since 1601) so it rolls
405 * over about every seven minutes!
407 md_get_uint32le(mdp, &lint); /* specs: secs since 1970 */
408 if (lint) /* avoid bogus zero returns */
409 smb_time_server2local(lint, SSTOVC(ssp)->vc_sopt.sv_tz,
411 md_get_uint32le(mdp, &lint);
419 * Set DOS file attributes. mtime should be NULL for dialects above lm10
422 smbfs_smb_setpattr(struct smbnode *np, u_int16_t attr, struct timespec *mtime,
423 struct smb_cred *scred)
425 struct smb_rq rq, *rqp = &rq;
426 struct smb_share *ssp = np->n_mount->sm_share;
431 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_SET_INFORMATION, scred);
434 svtz = SSTOVC(ssp)->vc_sopt.sv_tz;
435 smb_rq_getrequest(rqp, &mbp);
437 mb_put_uint16le(mbp, attr);
439 smb_time_local2server(mtime, svtz, &time);
442 mb_put_uint32le(mbp, time); /* mtime */
443 mb_put_mem(mbp, NULL, 5 * 2, MB_MZERO);
446 mb_put_uint8(mbp, SMB_DT_ASCII);
448 error = smbfs_fullpath(mbp, SSTOVC(ssp), np, NULL, 0);
451 mb_put_uint8(mbp, SMB_DT_ASCII);
452 if (SMB_UNICODE_STRINGS(SSTOVC(ssp))) {
454 mb_put_uint8(mbp, 0); /* 1st byte of NULL Unicode char */
456 mb_put_uint8(mbp, 0);
458 error = smb_rq_simple(rqp);
460 SMBERROR("smb_rq_simple(rqp) => error %d\n", error);
469 * Note, win95 doesn't support this call.
472 smbfs_smb_setptime2(struct smbnode *np, struct timespec *mtime,
473 struct timespec *atime, int attr, struct smb_cred *scred)
475 struct smb_t2rq *t2p;
476 struct smb_share *ssp = np->n_mount->sm_share;
477 struct smb_vc *vcp = SSTOVC(ssp);
479 u_int16_t date, time;
482 error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_SET_PATH_INFORMATION,
486 mbp = &t2p->t2_tparam;
488 mb_put_uint16le(mbp, SMB_INFO_STANDARD);
489 mb_put_uint32le(mbp, 0); /* MBZ */
490 /* mb_put_uint8(mbp, SMB_DT_ASCII); specs incorrect */
491 error = smbfs_fullpath(mbp, vcp, np, NULL, 0);
496 tzoff = vcp->vc_sopt.sv_tz;
497 mbp = &t2p->t2_tdata;
499 mb_put_uint32le(mbp, 0); /* creation time */
501 smb_time_unix2dos(atime, tzoff, &date, &time, NULL);
504 mb_put_uint16le(mbp, date);
505 mb_put_uint16le(mbp, time);
507 smb_time_unix2dos(mtime, tzoff, &date, &time, NULL);
510 mb_put_uint16le(mbp, date);
511 mb_put_uint16le(mbp, time);
512 mb_put_uint32le(mbp, 0); /* file size */
513 mb_put_uint32le(mbp, 0); /* allocation unit size */
514 mb_put_uint16le(mbp, attr); /* DOS attr */
515 mb_put_uint32le(mbp, 0); /* EA size */
516 t2p->t2_maxpcount = 5 * 2;
517 t2p->t2_maxdcount = vcp->vc_txmax;
518 error = smb_t2_request(t2p);
524 * NT level. Specially for win9x
527 smbfs_smb_setpattrNT(struct smbnode *np, u_short attr, struct timespec *mtime,
528 struct timespec *atime, struct smb_cred *scred)
530 struct smb_t2rq *t2p;
531 struct smb_share *ssp = np->n_mount->sm_share;
532 struct smb_vc *vcp = SSTOVC(ssp);
537 error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_SET_PATH_INFORMATION,
541 mbp = &t2p->t2_tparam;
543 mb_put_uint16le(mbp, SMB_SET_FILE_BASIC_INFO);
544 mb_put_uint32le(mbp, 0); /* MBZ */
545 /* mb_put_uint8(mbp, SMB_DT_ASCII); specs incorrect */
546 error = smbfs_fullpath(mbp, vcp, np, NULL, 0);
551 tzoff = vcp->vc_sopt.sv_tz;
552 mbp = &t2p->t2_tdata;
554 mb_put_int64le(mbp, 0); /* creation time */
556 smb_time_local2NT(atime, tzoff, &tm);
559 mb_put_int64le(mbp, tm);
561 smb_time_local2NT(mtime, tzoff, &tm);
564 mb_put_int64le(mbp, tm);
565 mb_put_int64le(mbp, tm); /* change time */
566 mb_put_uint32le(mbp, attr); /* attr */
567 t2p->t2_maxpcount = 24;
568 t2p->t2_maxdcount = 56;
569 error = smb_t2_request(t2p);
575 * Set file atime and mtime. Doesn't supported by core dialect.
578 smbfs_smb_setftime(struct smbnode *np, struct timespec *mtime,
579 struct timespec *atime, struct smb_cred *scred)
581 struct smb_rq rq, *rqp = &rq;
582 struct smb_share *ssp = np->n_mount->sm_share;
584 u_int16_t date, time;
587 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_SET_INFORMATION2, scred);
590 tzoff = SSTOVC(ssp)->vc_sopt.sv_tz;
591 smb_rq_getrequest(rqp, &mbp);
593 mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
594 mb_put_uint32le(mbp, 0); /* creation time */
597 smb_time_unix2dos(atime, tzoff, &date, &time, NULL);
600 mb_put_uint16le(mbp, date);
601 mb_put_uint16le(mbp, time);
603 smb_time_unix2dos(mtime, tzoff, &date, &time, NULL);
606 mb_put_uint16le(mbp, date);
607 mb_put_uint16le(mbp, time);
611 error = smb_rq_simple(rqp);
612 SMBSDEBUG("%d\n", error);
618 * Set DOS file attributes.
619 * Looks like this call can be used only if SMB_CAP_NT_SMBS bit is on.
622 smbfs_smb_setfattrNT(struct smbnode *np, u_int16_t attr, struct timespec *mtime,
623 struct timespec *atime, struct smb_cred *scred)
625 struct smb_t2rq *t2p;
626 struct smb_share *ssp = np->n_mount->sm_share;
631 error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_SET_FILE_INFORMATION,
635 svtz = SSTOVC(ssp)->vc_sopt.sv_tz;
636 mbp = &t2p->t2_tparam;
638 mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
639 mb_put_uint16le(mbp, SMB_SET_FILE_BASIC_INFO);
640 mb_put_uint32le(mbp, 0);
641 mbp = &t2p->t2_tdata;
643 mb_put_int64le(mbp, 0); /* creation time */
645 smb_time_local2NT(atime, svtz, &tm);
648 mb_put_int64le(mbp, tm);
650 smb_time_local2NT(mtime, svtz, &tm);
653 mb_put_int64le(mbp, tm);
654 mb_put_int64le(mbp, tm); /* change time */
655 mb_put_uint16le(mbp, attr);
656 mb_put_uint32le(mbp, 0); /* padding */
657 mb_put_uint16le(mbp, 0);
658 t2p->t2_maxpcount = 2;
659 t2p->t2_maxdcount = 0;
660 error = smb_t2_request(t2p);
667 smbfs_smb_open(struct smbnode *np, int accmode, struct smb_cred *scred)
669 struct smb_rq rq, *rqp = &rq;
670 struct smb_share *ssp = np->n_mount->sm_share;
674 u_int16_t fid, wattr, grantedmode;
677 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_OPEN, scred);
680 smb_rq_getrequest(rqp, &mbp);
682 mb_put_uint16le(mbp, accmode);
683 mb_put_uint16le(mbp, SMB_FA_SYSTEM | SMB_FA_HIDDEN);
686 mb_put_uint8(mbp, SMB_DT_ASCII);
688 error = smbfs_fullpath(mbp, SSTOVC(ssp), np, NULL, 0);
692 error = smb_rq_simple(rqp);
695 smb_rq_getreply(rqp, &mdp);
696 if (md_get_uint8(mdp, &wc) != 0 || wc != 7) {
700 md_get_uint16(mdp, &fid);
701 md_get_uint16le(mdp, &wattr);
702 md_get_uint32(mdp, NULL); /* mtime */
703 md_get_uint32(mdp, NULL); /* fsize */
704 md_get_uint16le(mdp, &grantedmode);
706 * TODO: refresh attributes from this reply
713 np->n_rwstate = grantedmode;
719 smbfs_smb_close(struct smb_share *ssp, u_int16_t fid, struct timespec *mtime,
720 struct smb_cred *scred)
722 struct smb_rq rq, *rqp = &rq;
727 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_CLOSE, scred);
730 smb_rq_getrequest(rqp, &mbp);
732 mb_put_mem(mbp, (caddr_t)&fid, sizeof(fid), MB_MSYSTEM);
734 smb_time_local2server(mtime, SSTOVC(ssp)->vc_sopt.sv_tz, &time);
737 mb_put_uint32le(mbp, time);
741 error = smb_rq_simple(rqp);
747 smbfs_smb_create(struct smbnode *dnp, const char *name, int nmlen,
748 struct smb_cred *scred)
750 struct smb_rq rq, *rqp = &rq;
751 struct smb_share *ssp = dnp->n_mount->sm_share;
754 struct timespec ctime;
760 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_CREATE, scred);
763 smb_rq_getrequest(rqp, &mbp);
765 mb_put_uint16le(mbp, SMB_FA_ARCHIVE); /* attributes */
767 smb_time_local2server(&ctime, SSTOVC(ssp)->vc_sopt.sv_tz, &tm);
768 mb_put_uint32le(mbp, tm);
771 mb_put_uint8(mbp, SMB_DT_ASCII);
772 error = smbfs_fullpath(mbp, SSTOVC(ssp), dnp, name, nmlen);
775 error = smb_rq_simple(rqp);
777 smb_rq_getreply(rqp, &mdp);
778 md_get_uint8(mdp, &wc);
780 md_get_uint16(mdp, &fid);
788 smbfs_smb_close(ssp, fid, &ctime, scred);
793 smbfs_smb_delete(struct smbnode *np, struct smb_cred *scred)
795 struct smb_rq rq, *rqp = &rq;
796 struct smb_share *ssp = np->n_mount->sm_share;
800 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_DELETE, scred);
803 smb_rq_getrequest(rqp, &mbp);
805 mb_put_uint16le(mbp, SMB_FA_SYSTEM | SMB_FA_HIDDEN);
808 mb_put_uint8(mbp, SMB_DT_ASCII);
809 error = smbfs_fullpath(mbp, SSTOVC(ssp), np, NULL, 0);
812 error = smb_rq_simple(rqp);
819 smbfs_smb_rename(struct smbnode *src, struct smbnode *tdnp,
820 const char *tname, int tnmlen, struct smb_cred *scred)
822 struct smb_rq rq, *rqp = &rq;
823 struct smb_share *ssp = src->n_mount->sm_share;
827 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_RENAME, scred);
830 smb_rq_getrequest(rqp, &mbp);
832 mb_put_uint16le(mbp, SMB_FA_SYSTEM | SMB_FA_HIDDEN);
835 mb_put_uint8(mbp, SMB_DT_ASCII);
837 error = smbfs_fullpath(mbp, SSTOVC(ssp), src, NULL, 0);
840 mb_put_uint8(mbp, SMB_DT_ASCII);
841 error = smbfs_fullpath(mbp, SSTOVC(ssp), tdnp, tname, tnmlen);
845 error = smb_rq_simple(rqp);
852 smbfs_smb_move(struct smbnode *src, struct smbnode *tdnp,
853 const char *tname, int tnmlen, u_int16_t flags, struct smb_cred *scred)
855 struct smb_rq rq, *rqp = &rq;
856 struct smb_share *ssp = src->n_mount->sm_share;
860 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_MOVE, scred);
863 smb_rq_getrequest(rqp, &mbp);
865 mb_put_uint16le(mbp, SMB_TID_UNKNOWN);
866 mb_put_uint16le(mbp, 0x20); /* delete target file */
867 mb_put_uint16le(mbp, flags);
870 mb_put_uint8(mbp, SMB_DT_ASCII);
872 error = smbfs_fullpath(mbp, SSTOVC(ssp), src, NULL, 0);
875 mb_put_uint8(mbp, SMB_DT_ASCII);
876 error = smbfs_fullpath(mbp, SSTOVC(ssp), tdnp, tname, tnmlen);
880 error = smb_rq_simple(rqp);
887 smbfs_smb_mkdir(struct smbnode *dnp, const char *name, int len,
888 struct smb_cred *scred)
890 struct smb_rq rq, *rqp = &rq;
891 struct smb_share *ssp = dnp->n_mount->sm_share;
895 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_CREATE_DIRECTORY, scred);
898 smb_rq_getrequest(rqp, &mbp);
902 mb_put_uint8(mbp, SMB_DT_ASCII);
903 error = smbfs_fullpath(mbp, SSTOVC(ssp), dnp, name, len);
906 error = smb_rq_simple(rqp);
913 smbfs_smb_rmdir(struct smbnode *np, struct smb_cred *scred)
915 struct smb_rq rq, *rqp = &rq;
916 struct smb_share *ssp = np->n_mount->sm_share;
920 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_DELETE_DIRECTORY, scred);
923 smb_rq_getrequest(rqp, &mbp);
927 mb_put_uint8(mbp, SMB_DT_ASCII);
928 error = smbfs_fullpath(mbp, SSTOVC(ssp), np, NULL, 0);
931 error = smb_rq_simple(rqp);
938 smbfs_smb_search(struct smbfs_fctx *ctx)
940 struct smb_vc *vcp = SSTOVC(ctx->f_ssp);
945 u_int16_t ec, dlen, bc;
946 int maxent, error, iseof = 0;
948 maxent = min(ctx->f_left, (vcp->vc_txmax - SMB_HDRLEN - 3) / SMB_DENTRYLEN);
950 smb_rq_done(ctx->f_rq);
953 error = smb_rq_alloc(SSTOCP(ctx->f_ssp), SMB_COM_SEARCH, ctx->f_scred, &rqp);
957 smb_rq_getrequest(rqp, &mbp);
959 mb_put_uint16le(mbp, maxent); /* max entries to return */
960 mb_put_uint16le(mbp, ctx->f_attrmask);
963 mb_put_uint8(mbp, SMB_DT_ASCII); /* buffer format */
964 if (ctx->f_flags & SMBFS_RDD_FINDFIRST) {
965 error = smbfs_fullpath(mbp, vcp, ctx->f_dnp, ctx->f_wildcard, ctx->f_wclen);
968 mb_put_uint8(mbp, SMB_DT_VARIABLE);
969 mb_put_uint16le(mbp, 0); /* context length */
970 ctx->f_flags &= ~SMBFS_RDD_FINDFIRST;
972 if (SMB_UNICODE_STRINGS(vcp)) {
974 mb_put_uint8(mbp, 0);
976 mb_put_uint8(mbp, 0); /* file name length */
977 mb_put_uint8(mbp, SMB_DT_VARIABLE);
978 mb_put_uint16le(mbp, SMB_SKEYLEN);
979 mb_put_mem(mbp, ctx->f_skey, SMB_SKEYLEN, MB_MSYSTEM);
982 error = smb_rq_simple(rqp);
984 if (rqp->sr_errclass == ERRDOS && rqp->sr_serror == ERRnofiles) {
987 ctx->f_flags |= SMBFS_RDD_EOF;
991 smb_rq_getreply(rqp, &mdp);
992 md_get_uint8(mdp, &wc);
994 return iseof ? ENOENT : EBADRPC;
995 md_get_uint16le(mdp, &ec);
999 md_get_uint16le(mdp, &bc);
1003 md_get_uint8(mdp, &bt);
1004 if (bt != SMB_DT_VARIABLE)
1006 md_get_uint16le(mdp, &dlen);
1007 if (dlen != bc || dlen % SMB_DENTRYLEN != 0)
1013 smbfs_findopenLM1(struct smbfs_fctx *ctx, struct smbnode *dnp,
1014 const char *wildcard, int wclen, int attr, struct smb_cred *scred)
1016 ctx->f_attrmask = attr;
1018 if (wclen == 1 && wildcard[0] == '*') {
1019 ctx->f_wildcard = "*.*";
1022 ctx->f_wildcard = wildcard;
1023 ctx->f_wclen = wclen;
1026 ctx->f_wildcard = NULL;
1029 ctx->f_name = ctx->f_fname;
1034 smbfs_findnextLM1(struct smbfs_fctx *ctx, int limit)
1036 struct mdchain *mbp;
1040 u_int16_t date, time;
1044 if (ctx->f_ecnt == 0) {
1045 if (ctx->f_flags & SMBFS_RDD_EOF)
1047 ctx->f_left = ctx->f_limit = limit;
1048 error = smbfs_smb_search(ctx);
1053 smb_rq_getreply(rqp, &mbp);
1054 md_get_mem(mbp, ctx->f_skey, SMB_SKEYLEN, MB_MSYSTEM);
1055 md_get_uint8(mbp, &battr);
1056 md_get_uint16le(mbp, &time);
1057 md_get_uint16le(mbp, &date);
1058 md_get_uint32le(mbp, &size);
1060 md_get_mem(mbp, cp, sizeof(ctx->f_fname), MB_MSYSTEM);
1061 cp[sizeof(ctx->f_fname) - 1] = 0;
1062 cp += strlen(cp) - 1;
1063 while (*cp == ' ' && cp >= ctx->f_name)
1065 ctx->f_attr.fa_attr = battr;
1066 smb_dos2unixtime(date, time, 0, rqp->sr_vc->vc_sopt.sv_tz,
1067 &ctx->f_attr.fa_mtime);
1068 ctx->f_attr.fa_size = size;
1069 ctx->f_nmlen = strlen(ctx->f_name);
1076 smbfs_findcloseLM1(struct smbfs_fctx *ctx)
1079 smb_rq_done(ctx->f_rq);
1084 * TRANS2_FIND_FIRST2/NEXT2, used for NT LM12 dialect
1087 smbfs_smb_trans2find2(struct smbfs_fctx *ctx)
1089 struct smb_t2rq *t2p;
1090 struct smb_vc *vcp = SSTOVC(ctx->f_ssp);
1091 struct mbchain *mbp;
1092 struct mdchain *mdp;
1093 u_int16_t tw, flags;
1097 smb_t2_done(ctx->f_t2);
1100 ctx->f_flags &= ~SMBFS_RDD_GOTRNAME;
1101 flags = 8 | 2; /* <resume> | <close if EOS> */
1102 if (ctx->f_flags & SMBFS_RDD_FINDSINGLE) {
1103 flags |= 1; /* close search after this request */
1104 ctx->f_flags |= SMBFS_RDD_NOCLOSE;
1106 if (ctx->f_flags & SMBFS_RDD_FINDFIRST) {
1107 error = smb_t2_alloc(SSTOCP(ctx->f_ssp), SMB_TRANS2_FIND_FIRST2,
1108 ctx->f_scred, &t2p);
1112 mbp = &t2p->t2_tparam;
1114 mb_put_uint16le(mbp, ctx->f_attrmask);
1115 mb_put_uint16le(mbp, ctx->f_limit);
1116 mb_put_uint16le(mbp, flags);
1117 mb_put_uint16le(mbp, ctx->f_infolevel);
1118 mb_put_uint32le(mbp, 0);
1119 error = smbfs_fullpath(mbp, vcp, ctx->f_dnp, ctx->f_wildcard, ctx->f_wclen);
1123 error = smb_t2_alloc(SSTOCP(ctx->f_ssp), SMB_TRANS2_FIND_NEXT2,
1124 ctx->f_scred, &t2p);
1128 mbp = &t2p->t2_tparam;
1130 mb_put_mem(mbp, (caddr_t)&ctx->f_Sid, 2, MB_MSYSTEM);
1131 mb_put_uint16le(mbp, ctx->f_limit);
1132 mb_put_uint16le(mbp, ctx->f_infolevel);
1133 mb_put_uint32le(mbp, 0); /* resume key */
1134 mb_put_uint16le(mbp, flags);
1136 mb_put_mem(mbp, ctx->f_rname, ctx->f_rnamelen + 1, MB_MSYSTEM);
1138 mb_put_uint8(mbp, 0); /* resume file name */
1142 tv.tv_usec = 200 * 1000; /* 200ms */
1143 if (vcp->vc_flags & SMBC_WIN95) {
1145 * some implementations suggests to sleep here
1146 * for 200ms, due to the bug in the Win95.
1147 * I've didn't notice any problem, but put code
1150 pause("fix95", tvtohz(&tv));
1154 t2p->t2_maxpcount = 5 * 2;
1155 t2p->t2_maxdcount = vcp->vc_txmax;
1156 error = smb_t2_request(t2p);
1159 mdp = &t2p->t2_rparam;
1160 if (ctx->f_flags & SMBFS_RDD_FINDFIRST) {
1161 if ((error = md_get_uint16(mdp, &ctx->f_Sid)) != 0)
1163 ctx->f_flags &= ~SMBFS_RDD_FINDFIRST;
1165 if ((error = md_get_uint16le(mdp, &tw)) != 0)
1168 if ((error = md_get_uint16le(mdp, &tw)) != 0)
1171 ctx->f_flags |= SMBFS_RDD_EOF | SMBFS_RDD_NOCLOSE;
1172 if ((error = md_get_uint16le(mdp, &tw)) != 0)
1174 if ((error = md_get_uint16le(mdp, &tw)) != 0)
1176 if (ctx->f_ecnt == 0) {
1177 ctx->f_flags |= SMBFS_RDD_EOF | SMBFS_RDD_NOCLOSE;
1180 ctx->f_rnameofs = tw;
1181 mdp = &t2p->t2_rdata;
1182 if (mdp->md_top == NULL) {
1183 printf("bug: ecnt = %d, but data is NULL (please report)\n", ctx->f_ecnt);
1186 if (mdp->md_top->m_len == 0) {
1187 printf("bug: ecnt = %d, but m_len = 0 and m_next = %p (please report)\n", ctx->f_ecnt,mbp->mb_top->m_next);
1195 smbfs_smb_findclose2(struct smbfs_fctx *ctx)
1197 struct smb_rq rq, *rqp = &rq;
1198 struct mbchain *mbp;
1201 error = smb_rq_init(rqp, SSTOCP(ctx->f_ssp), SMB_COM_FIND_CLOSE2, ctx->f_scred);
1204 smb_rq_getrequest(rqp, &mbp);
1206 mb_put_mem(mbp, (caddr_t)&ctx->f_Sid, 2, MB_MSYSTEM);
1210 error = smb_rq_simple(rqp);
1216 smbfs_findopenLM2(struct smbfs_fctx *ctx, struct smbnode *dnp,
1217 const char *wildcard, int wclen, int attr, struct smb_cred *scred)
1219 if (SMB_UNICODE_STRINGS(SSTOVC(ctx->f_ssp))) {
1220 ctx->f_name = malloc(SMB_MAXFNAMELEN * 2, M_SMBFSDATA, M_WAITOK);
1222 ctx->f_name = malloc(SMB_MAXFNAMELEN, M_SMBFSDATA, M_WAITOK);
1223 if (ctx->f_name == NULL)
1225 ctx->f_infolevel = SMB_DIALECT(SSTOVC(ctx->f_ssp)) < SMB_DIALECT_NTLM0_12 ?
1226 SMB_INFO_STANDARD : SMB_FIND_FILE_DIRECTORY_INFO;
1227 ctx->f_attrmask = attr;
1228 ctx->f_wildcard = wildcard;
1229 ctx->f_wclen = wclen;
1234 smbfs_findnextLM2(struct smbfs_fctx *ctx, int limit)
1236 struct mdchain *mbp;
1237 struct smb_t2rq *t2p;
1240 u_int16_t date, time, wattr;
1241 u_int32_t size, next, dattr;
1243 int error, svtz, cnt, fxsz, nmlen, recsz;
1245 if (ctx->f_ecnt == 0) {
1246 if (ctx->f_flags & SMBFS_RDD_EOF)
1248 ctx->f_left = ctx->f_limit = limit;
1249 error = smbfs_smb_trans2find2(ctx);
1254 mbp = &t2p->t2_rdata;
1255 svtz = SSTOVC(ctx->f_ssp)->vc_sopt.sv_tz;
1256 switch (ctx->f_infolevel) {
1257 case SMB_INFO_STANDARD:
1260 md_get_uint16le(mbp, &date);
1261 md_get_uint16le(mbp, &time); /* creation time */
1262 md_get_uint16le(mbp, &date);
1263 md_get_uint16le(mbp, &time); /* access time */
1264 smb_dos2unixtime(date, time, 0, svtz, &ctx->f_attr.fa_atime);
1265 md_get_uint16le(mbp, &date);
1266 md_get_uint16le(mbp, &time); /* access time */
1267 smb_dos2unixtime(date, time, 0, svtz, &ctx->f_attr.fa_mtime);
1268 md_get_uint32le(mbp, &size);
1269 ctx->f_attr.fa_size = size;
1270 md_get_uint32(mbp, NULL); /* allocation size */
1271 md_get_uint16le(mbp, &wattr);
1272 ctx->f_attr.fa_attr = wattr;
1273 md_get_uint8(mbp, &tb);
1276 recsz = next = 24 + nmlen; /* docs misses zero byte at end */
1278 case SMB_FIND_FILE_DIRECTORY_INFO:
1279 md_get_uint32le(mbp, &next);
1280 md_get_uint32(mbp, NULL); /* file index */
1281 md_get_int64(mbp, NULL); /* creation time */
1282 md_get_int64le(mbp, &lint);
1283 smb_time_NT2local(lint, svtz, &ctx->f_attr.fa_atime);
1284 md_get_int64le(mbp, &lint);
1285 smb_time_NT2local(lint, svtz, &ctx->f_attr.fa_mtime);
1286 md_get_int64le(mbp, &lint);
1287 smb_time_NT2local(lint, svtz, &ctx->f_attr.fa_ctime);
1288 md_get_int64le(mbp, &lint); /* file size */
1289 ctx->f_attr.fa_size = lint;
1290 md_get_int64(mbp, NULL); /* real size (should use) */
1291 md_get_uint32le(mbp, &dattr); /* EA */
1292 ctx->f_attr.fa_attr = dattr;
1293 md_get_uint32le(mbp, &size); /* name len */
1295 recsz = next ? next : fxsz + size;
1298 SMBERROR("unexpected info level %d\n", ctx->f_infolevel);
1301 if (SMB_UNICODE_STRINGS(SSTOVC(ctx->f_ssp))) {
1302 nmlen = min(size, SMB_MAXFNAMELEN * 2);
1304 nmlen = min(size, SMB_MAXFNAMELEN);
1306 error = md_get_mem(mbp, cp, nmlen, MB_MSYSTEM);
1310 cnt = next - nmlen - fxsz;
1312 md_get_mem(mbp, NULL, cnt, MB_MSYSTEM);
1314 SMBERROR("out of sync\n");
1318 if (SMB_UNICODE_STRINGS(SSTOVC(ctx->f_ssp))) {
1319 if (nmlen > 1 && cp[nmlen - 1] == 0 && cp[nmlen - 2] == 0)
1322 if (nmlen && cp[nmlen - 1] == 0)
1327 next = ctx->f_eofs + recsz;
1328 if (ctx->f_rnameofs && (ctx->f_flags & SMBFS_RDD_GOTRNAME) == 0 &&
1329 (ctx->f_rnameofs >= ctx->f_eofs && ctx->f_rnameofs < next)) {
1331 * Server needs a resume filename.
1333 if (ctx->f_rnamelen <= nmlen) {
1335 free(ctx->f_rname, M_SMBFSDATA);
1336 ctx->f_rname = malloc(nmlen + 1, M_SMBFSDATA, M_WAITOK);
1337 ctx->f_rnamelen = nmlen;
1339 bcopy(ctx->f_name, ctx->f_rname, nmlen);
1340 ctx->f_rname[nmlen] = 0;
1341 ctx->f_flags |= SMBFS_RDD_GOTRNAME;
1343 ctx->f_nmlen = nmlen;
1351 smbfs_findcloseLM2(struct smbfs_fctx *ctx)
1354 free(ctx->f_name, M_SMBFSDATA);
1356 smb_t2_done(ctx->f_t2);
1357 if ((ctx->f_flags & SMBFS_RDD_NOCLOSE) == 0)
1358 smbfs_smb_findclose2(ctx);
1363 smbfs_findopen(struct smbnode *dnp, const char *wildcard, int wclen, int attr,
1364 struct smb_cred *scred, struct smbfs_fctx **ctxpp)
1366 struct smbfs_fctx *ctx;
1369 ctx = malloc(sizeof(*ctx), M_SMBFSDATA, M_WAITOK);
1372 bzero(ctx, sizeof(*ctx));
1373 ctx->f_ssp = dnp->n_mount->sm_share;
1375 ctx->f_flags = SMBFS_RDD_FINDFIRST;
1376 ctx->f_scred = scred;
1377 if (SMB_DIALECT(SSTOVC(ctx->f_ssp)) < SMB_DIALECT_LANMAN2_0 ||
1378 (dnp->n_mount->sm_flags & SMBFS_MOUNT_NO_LONG)) {
1379 ctx->f_flags |= SMBFS_RDD_USESEARCH;
1380 error = smbfs_findopenLM1(ctx, dnp, wildcard, wclen, attr, scred);
1382 error = smbfs_findopenLM2(ctx, dnp, wildcard, wclen, attr, scred);
1384 smbfs_findclose(ctx, scred);
1391 smbfs_findnext(struct smbfs_fctx *ctx, int limit, struct smb_cred *scred)
1398 limit *= 4; /* imperical */
1399 ctx->f_scred = scred;
1401 if (ctx->f_flags & SMBFS_RDD_USESEARCH) {
1402 error = smbfs_findnextLM1(ctx, limit);
1404 error = smbfs_findnextLM2(ctx, limit);
1407 if (SMB_UNICODE_STRINGS(SSTOVC(ctx->f_ssp))) {
1408 if ((ctx->f_nmlen == 2 &&
1409 *(u_int16_t *)ctx->f_name == htole16(0x002e)) ||
1410 (ctx->f_nmlen == 4 &&
1411 *(u_int32_t *)ctx->f_name == htole32(0x002e002e)))
1414 if ((ctx->f_nmlen == 1 && ctx->f_name[0] == '.') ||
1415 (ctx->f_nmlen == 2 && ctx->f_name[0] == '.' &&
1416 ctx->f_name[1] == '.'))
1420 smbfs_fname_tolocal(SSTOVC(ctx->f_ssp), ctx->f_name, &ctx->f_nmlen,
1421 ctx->f_dnp->n_mount->sm_caseopt);
1422 ctx->f_attr.fa_ino = smbfs_getino(ctx->f_dnp, ctx->f_name, ctx->f_nmlen);
1427 smbfs_findclose(struct smbfs_fctx *ctx, struct smb_cred *scred)
1429 ctx->f_scred = scred;
1430 if (ctx->f_flags & SMBFS_RDD_USESEARCH) {
1431 smbfs_findcloseLM1(ctx);
1433 smbfs_findcloseLM2(ctx);
1435 free(ctx->f_rname, M_SMBFSDATA);
1436 free(ctx, M_SMBFSDATA);
1441 smbfs_smb_lookup(struct smbnode *dnp, const char *name, int nmlen,
1442 struct smbfattr *fap, struct smb_cred *scred)
1444 struct smbfs_fctx *ctx;
1447 if (dnp == NULL || (dnp->n_ino == 2 && name == NULL)) {
1448 bzero(fap, sizeof(*fap));
1449 fap->fa_attr = SMB_FA_DIR;
1453 if (nmlen == 1 && name[0] == '.') {
1454 error = smbfs_smb_lookup(dnp, NULL, 0, fap, scred);
1456 } else if (nmlen == 2 && name[0] == '.' && name[1] == '.') {
1457 error = smbfs_smb_lookup(VTOSMB(dnp->n_parent), NULL, 0, fap,
1459 printf("%s: knows NOTHING about '..'\n", __func__);
1462 error = smbfs_findopen(dnp, name, nmlen,
1463 SMB_FA_SYSTEM | SMB_FA_HIDDEN | SMB_FA_DIR, scred, &ctx);
1466 ctx->f_flags |= SMBFS_RDD_FINDSINGLE;
1467 error = smbfs_findnext(ctx, 1, scred);
1471 fap->fa_ino = dnp->n_ino;
1473 smbfs_findclose(ctx, scred);