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.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Boris Popov.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * $FreeBSD: src/sys/fs/smbfs/smbfs_smb.c,v 1.1.2.2 2003/01/17 08:20:26 tjr Exp $
33 * $DragonFly: src/sys/vfs/smbfs/smbfs_smb.c,v 1.9 2006/12/23 00:41:30 swildner Exp $
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
41 #include <sys/vnode.h>
43 #include <sys/mount.h>
49 #include <netproto/smb/smb.h>
50 #include <netproto/smb/smb_subr.h>
51 #include <netproto/smb/smb_rq.h>
52 #include <netproto/smb/smb_conn.h>
55 #include "smbfs_node.h"
56 #include "smbfs_subr.h"
59 * Lack of inode numbers leads us to the problem of generating them.
60 * Partially this problem can be solved by having a dir/file cache
61 * with inode numbers generated from the incremented by one counter.
62 * However this way will require too much kernel memory, gives all
63 * sorts of locking and consistency problems, not to mentinon counter overflows.
64 * So, I'm decided to use a hash function to generate pseudo random (and unique)
68 smbfs_getino(struct smbnode *dnp, const char *name, int nmlen)
77 MD5Update(&md5, name, nmlen);
78 MD5Final((u_char *)state, &md5);
79 for (i = 0, ino = 0; i < 4; i++)
81 return dnp->n_ino + ino;
85 ino = dnp->n_ino + smbfs_hash(name, nmlen);
92 smbfs_smb_lockandx(struct smbnode *np, int op, u_int32_t pid, off_t start, off_t end,
93 struct smb_cred *scred)
95 struct smb_share *ssp = np->n_mount->sm_share;
96 struct smb_rq rq, *rqp = &rq;
101 if (op == SMB_LOCK_SHARED)
102 ltype |= SMB_LOCKING_ANDX_SHARED_LOCK;
103 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_LOCKING_ANDX, scred);
106 smb_rq_getrequest(rqp, &mbp);
108 mb_put_uint8(mbp, 0xff); /* secondary command */
109 mb_put_uint8(mbp, 0); /* MBZ */
110 mb_put_uint16le(mbp, 0);
111 mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
112 mb_put_uint8(mbp, ltype); /* locktype */
113 mb_put_uint8(mbp, 0); /* oplocklevel - 0 seems is NO_OPLOCK */
114 mb_put_uint32le(mbp, 0); /* timeout - break immediately */
115 mb_put_uint16le(mbp, op == SMB_LOCK_RELEASE ? 1 : 0);
116 mb_put_uint16le(mbp, op == SMB_LOCK_RELEASE ? 0 : 1);
119 mb_put_uint16le(mbp, pid);
120 mb_put_uint32le(mbp, start);
121 mb_put_uint32le(mbp, end - start);
123 error = smb_rq_simple(rqp);
129 smbfs_smb_lock(struct smbnode *np, int op, caddr_t id,
130 off_t start, off_t end, struct smb_cred *scred)
132 struct smb_share *ssp = np->n_mount->sm_share;
134 if (SMB_DIALECT(SSTOVC(ssp)) < SMB_DIALECT_LANMAN1_0)
136 * TODO: use LOCK_BYTE_RANGE here.
140 return smbfs_smb_lockandx(np, op, (u_int32_t)id, start, end, scred);
144 smbfs_smb_statfs2(struct smb_share *ssp, struct statfs *sbp,
145 struct smb_cred *scred)
147 struct smb_t2rq *t2p;
151 u_int32_t units, bpu, funits;
154 error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_QUERY_FS_INFORMATION,
158 mbp = &t2p->t2_tparam;
160 mb_put_uint16le(mbp, SMB_INFO_ALLOCATION);
161 t2p->t2_maxpcount = 4;
162 t2p->t2_maxdcount = 4 * 4 + 2;
163 error = smb_t2_request(t2p);
168 mdp = &t2p->t2_rdata;
169 md_get_uint32(mdp, NULL); /* fs id */
170 md_get_uint32le(mdp, &bpu);
171 md_get_uint32le(mdp, &units);
172 md_get_uint32le(mdp, &funits);
173 md_get_uint16le(mdp, &bsize);
174 sbp->f_bsize = bpu * bsize; /* fundamental file system block size */
175 sbp->f_blocks= units; /* total data blocks in file system */
176 sbp->f_bfree = funits; /* free blocks in fs */
177 sbp->f_bavail= funits; /* free blocks avail to non-superuser */
178 sbp->f_files = 0xffff; /* total file nodes in file system */
179 sbp->f_ffree = 0xffff; /* free file nodes in fs */
185 smbfs_smb_statfs(struct smb_share *ssp, struct statfs *sbp,
186 struct smb_cred *scred)
188 struct smb_rq rq, *rqp = &rq;
190 u_int16_t units, bpu, bsize, funits;
193 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_QUERY_INFORMATION_DISK, scred);
200 error = smb_rq_simple(rqp);
205 smb_rq_getreply(rqp, &mdp);
206 md_get_uint16le(mdp, &units);
207 md_get_uint16le(mdp, &bpu);
208 md_get_uint16le(mdp, &bsize);
209 md_get_uint16le(mdp, &funits);
210 sbp->f_bsize = bpu * bsize; /* fundamental file system block size */
211 sbp->f_blocks= units; /* total data blocks in file system */
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 file system */
215 sbp->f_ffree = 0xffff; /* free file nodes in fs */
221 smbfs_smb_setfsize(struct smbnode *np, int newsize, struct smb_cred *scred)
223 struct smb_share *ssp = np->n_mount->sm_share;
224 struct smb_rq rq, *rqp = &rq;
228 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_WRITE, scred);
231 smb_rq_getrequest(rqp, &mbp);
233 mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
234 mb_put_uint16le(mbp, 0);
235 mb_put_uint32le(mbp, newsize);
236 mb_put_uint16le(mbp, 0);
239 mb_put_uint8(mbp, SMB_DT_DATA);
240 mb_put_uint16le(mbp, 0);
242 error = smb_rq_simple(rqp);
249 * Set DOS file attributes. mtime should be NULL for dialects above lm10
252 smbfs_smb_setpattr(struct smbnode *np, u_int16_t attr, struct timespec *mtime,
253 struct smb_cred *scred)
255 struct smb_rq rq, *rqp = &rq;
256 struct smb_share *ssp = np->n_mount->sm_share;
261 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_SET_INFORMATION, scred);
264 svtz = SSTOVC(ssp)->vc_sopt.sv_tz;
265 smb_rq_getrequest(rqp, &mbp);
267 mb_put_uint16le(mbp, attr);
269 smb_time_local2server(mtime, svtz, &time);
272 mb_put_uint32le(mbp, time); /* mtime */
273 mb_put_mem(mbp, NULL, 5 * 2, MB_MZERO);
276 mb_put_uint8(mbp, SMB_DT_ASCII);
278 error = smbfs_fullpath(mbp, SSTOVC(ssp), np, NULL, 0);
281 mb_put_uint8(mbp, SMB_DT_ASCII);
282 mb_put_uint8(mbp, 0);
284 error = smb_rq_simple(rqp);
285 SMBERROR("%d\n", error);
294 * Note, win95 doesn't support this call.
297 smbfs_smb_setptime2(struct smbnode *np, struct timespec *mtime,
298 struct timespec *atime, int attr, struct smb_cred *scred)
300 struct smb_t2rq *t2p;
301 struct smb_share *ssp = np->n_mount->sm_share;
302 struct smb_vc *vcp = SSTOVC(ssp);
304 u_int16_t date, time;
307 error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_SET_PATH_INFORMATION,
311 mbp = &t2p->t2_tparam;
313 mb_put_uint16le(mbp, SMB_INFO_STANDARD);
314 mb_put_uint32le(mbp, 0); /* MBZ */
315 error = smbfs_fullpath(mbp, vcp, np, NULL, 0);
320 tzoff = vcp->vc_sopt.sv_tz;
321 mbp = &t2p->t2_tdata;
323 mb_put_uint32le(mbp, 0); /* creation time */
325 smb_time_unix2dos(atime, tzoff, &date, &time, NULL);
328 mb_put_uint16le(mbp, date);
329 mb_put_uint16le(mbp, time);
331 smb_time_unix2dos(mtime, tzoff, &date, &time, NULL);
334 mb_put_uint16le(mbp, date);
335 mb_put_uint16le(mbp, time);
336 mb_put_uint32le(mbp, 0); /* file size */
337 mb_put_uint32le(mbp, 0); /* allocation unit size */
338 mb_put_uint16le(mbp, attr); /* DOS attr */
339 mb_put_uint32le(mbp, 0); /* EA size */
340 t2p->t2_maxpcount = 5 * 2;
341 t2p->t2_maxdcount = vcp->vc_txmax;
342 error = smb_t2_request(t2p);
348 * NT level. Specially for win9x
351 smbfs_smb_setpattrNT(struct smbnode *np, u_short attr, struct timespec *mtime,
352 struct timespec *atime, struct smb_cred *scred)
354 struct smb_t2rq *t2p;
355 struct smb_share *ssp = np->n_mount->sm_share;
356 struct smb_vc *vcp = SSTOVC(ssp);
361 error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_SET_PATH_INFORMATION,
365 mbp = &t2p->t2_tparam;
367 mb_put_uint16le(mbp, SMB_SET_FILE_BASIC_INFO);
368 mb_put_uint32le(mbp, 0); /* MBZ */
369 error = smbfs_fullpath(mbp, vcp, np, NULL, 0);
374 tzoff = vcp->vc_sopt.sv_tz;
375 mbp = &t2p->t2_tdata;
377 mb_put_int64le(mbp, 0); /* creation time */
379 smb_time_local2NT(atime, tzoff, &tm);
382 mb_put_int64le(mbp, tm);
384 smb_time_local2NT(mtime, tzoff, &tm);
387 mb_put_int64le(mbp, tm);
388 mb_put_int64le(mbp, tm); /* change time */
389 mb_put_uint32le(mbp, attr); /* attr */
390 t2p->t2_maxpcount = 24;
391 t2p->t2_maxdcount = 56;
392 error = smb_t2_request(t2p);
398 * Set file atime and mtime. Doesn't supported by core dialect.
401 smbfs_smb_setftime(struct smbnode *np, struct timespec *mtime,
402 struct timespec *atime, struct smb_cred *scred)
404 struct smb_rq rq, *rqp = &rq;
405 struct smb_share *ssp = np->n_mount->sm_share;
407 u_int16_t date, time;
410 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_SET_INFORMATION2, scred);
413 tzoff = SSTOVC(ssp)->vc_sopt.sv_tz;
414 smb_rq_getrequest(rqp, &mbp);
416 mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
417 mb_put_uint32le(mbp, 0); /* creation time */
420 smb_time_unix2dos(atime, tzoff, &date, &time, NULL);
423 mb_put_uint16le(mbp, date);
424 mb_put_uint16le(mbp, time);
426 smb_time_unix2dos(mtime, tzoff, &date, &time, NULL);
429 mb_put_uint16le(mbp, date);
430 mb_put_uint16le(mbp, time);
434 error = smb_rq_simple(rqp);
435 SMBSDEBUG("%d\n", error);
441 * Set DOS file attributes.
442 * Looks like this call can be used only if CAP_NT_SMBS bit is on.
445 smbfs_smb_setfattrNT(struct smbnode *np, u_int16_t attr, struct timespec *mtime,
446 struct timespec *atime, struct smb_cred *scred)
448 struct smb_t2rq *t2p;
449 struct smb_share *ssp = np->n_mount->sm_share;
454 error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_SET_FILE_INFORMATION,
458 svtz = SSTOVC(ssp)->vc_sopt.sv_tz;
459 mbp = &t2p->t2_tparam;
461 mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
462 mb_put_uint16le(mbp, SMB_SET_FILE_BASIC_INFO);
463 mb_put_uint32le(mbp, 0);
464 mbp = &t2p->t2_tdata;
466 mb_put_int64le(mbp, 0); /* creation time */
468 smb_time_local2NT(atime, svtz, &tm);
471 mb_put_int64le(mbp, tm);
473 smb_time_local2NT(mtime, svtz, &tm);
476 mb_put_int64le(mbp, tm);
477 mb_put_int64le(mbp, tm); /* change time */
478 mb_put_uint16le(mbp, attr);
479 mb_put_uint32le(mbp, 0); /* padding */
480 mb_put_uint16le(mbp, 0);
481 t2p->t2_maxpcount = 2;
482 t2p->t2_maxdcount = 0;
483 error = smb_t2_request(t2p);
490 smbfs_smb_open(struct smbnode *np, int accmode, struct smb_cred *scred)
492 struct smb_rq rq, *rqp = &rq;
493 struct smb_share *ssp = np->n_mount->sm_share;
497 u_int16_t fid, wattr, grantedmode;
500 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_OPEN, scred);
503 smb_rq_getrequest(rqp, &mbp);
505 mb_put_uint16le(mbp, accmode);
506 mb_put_uint16le(mbp, SMB_FA_SYSTEM | SMB_FA_HIDDEN);
509 mb_put_uint8(mbp, SMB_DT_ASCII);
511 error = smbfs_fullpath(mbp, SSTOVC(ssp), np, NULL, 0);
515 error = smb_rq_simple(rqp);
518 smb_rq_getreply(rqp, &mdp);
519 if (md_get_uint8(mdp, &wc) != 0 || wc != 7) {
523 md_get_uint16(mdp, &fid);
524 md_get_uint16le(mdp, &wattr);
525 md_get_uint32(mdp, NULL); /* mtime */
526 md_get_uint32(mdp, NULL); /* fsize */
527 md_get_uint16le(mdp, &grantedmode);
529 * TODO: refresh attributes from this reply
536 np->n_rwstate = grantedmode;
542 smbfs_smb_close(struct smb_share *ssp, u_int16_t fid, struct timespec *mtime,
543 struct smb_cred *scred)
545 struct smb_rq rq, *rqp = &rq;
550 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_CLOSE, scred);
553 smb_rq_getrequest(rqp, &mbp);
555 mb_put_mem(mbp, (caddr_t)&fid, sizeof(fid), MB_MSYSTEM);
557 smb_time_local2server(mtime, SSTOVC(ssp)->vc_sopt.sv_tz, &time);
560 mb_put_uint32le(mbp, time);
564 error = smb_rq_simple(rqp);
570 smbfs_smb_create(struct smbnode *dnp, const char *name, int nmlen,
571 struct smb_cred *scred)
573 struct smb_rq rq, *rqp = &rq;
574 struct smb_share *ssp = dnp->n_mount->sm_share;
577 struct timespec ctime;
583 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_CREATE, scred);
586 smb_rq_getrequest(rqp, &mbp);
588 mb_put_uint16le(mbp, SMB_FA_ARCHIVE); /* attributes */
590 smb_time_local2server(&ctime, SSTOVC(ssp)->vc_sopt.sv_tz, &tm);
591 mb_put_uint32le(mbp, tm);
594 mb_put_uint8(mbp, SMB_DT_ASCII);
595 error = smbfs_fullpath(mbp, SSTOVC(ssp), dnp, name, nmlen);
598 error = smb_rq_simple(rqp);
600 smb_rq_getreply(rqp, &mdp);
601 md_get_uint8(mdp, &wc);
603 md_get_uint16(mdp, &fid);
611 smbfs_smb_close(ssp, fid, &ctime, scred);
616 smbfs_smb_delete(struct smbnode *np, struct smb_cred *scred)
618 struct smb_rq rq, *rqp = &rq;
619 struct smb_share *ssp = np->n_mount->sm_share;
623 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_DELETE, scred);
626 smb_rq_getrequest(rqp, &mbp);
628 mb_put_uint16le(mbp, SMB_FA_SYSTEM | SMB_FA_HIDDEN);
631 mb_put_uint8(mbp, SMB_DT_ASCII);
632 error = smbfs_fullpath(mbp, SSTOVC(ssp), np, NULL, 0);
635 error = smb_rq_simple(rqp);
642 smbfs_smb_rename(struct smbnode *src, struct smbnode *tdnp,
643 const char *tname, int tnmlen, struct smb_cred *scred)
645 struct smb_rq rq, *rqp = &rq;
646 struct smb_share *ssp = src->n_mount->sm_share;
650 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_RENAME, scred);
653 smb_rq_getrequest(rqp, &mbp);
655 mb_put_uint16le(mbp, SMB_FA_SYSTEM | SMB_FA_HIDDEN);
658 mb_put_uint8(mbp, SMB_DT_ASCII);
660 error = smbfs_fullpath(mbp, SSTOVC(ssp), src, NULL, 0);
663 mb_put_uint8(mbp, SMB_DT_ASCII);
664 error = smbfs_fullpath(mbp, SSTOVC(ssp), tdnp, tname, tnmlen);
668 error = smb_rq_simple(rqp);
675 smbfs_smb_move(struct smbnode *src, struct smbnode *tdnp,
676 const char *tname, int tnmlen, u_int16_t flags, struct smb_cred *scred)
678 struct smb_rq rq, *rqp = &rq;
679 struct smb_share *ssp = src->n_mount->sm_share;
683 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_MOVE, scred);
686 smb_rq_getrequest(rqp, &mbp);
688 mb_put_uint16le(mbp, SMB_TID_UNKNOWN);
689 mb_put_uint16le(mbp, 0x20); /* delete target file */
690 mb_put_uint16le(mbp, flags);
693 mb_put_uint8(mbp, SMB_DT_ASCII);
695 error = smbfs_fullpath(mbp, SSTOVC(ssp), src, NULL, 0);
698 mb_put_uint8(mbp, SMB_DT_ASCII);
699 error = smbfs_fullpath(mbp, SSTOVC(ssp), tdnp, tname, tnmlen);
703 error = smb_rq_simple(rqp);
710 smbfs_smb_mkdir(struct smbnode *dnp, const char *name, int len,
711 struct smb_cred *scred)
713 struct smb_rq rq, *rqp = &rq;
714 struct smb_share *ssp = dnp->n_mount->sm_share;
718 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_CREATE_DIRECTORY, scred);
721 smb_rq_getrequest(rqp, &mbp);
725 mb_put_uint8(mbp, SMB_DT_ASCII);
726 error = smbfs_fullpath(mbp, SSTOVC(ssp), dnp, name, len);
729 error = smb_rq_simple(rqp);
736 smbfs_smb_rmdir(struct smbnode *np, struct smb_cred *scred)
738 struct smb_rq rq, *rqp = &rq;
739 struct smb_share *ssp = np->n_mount->sm_share;
743 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_DELETE_DIRECTORY, scred);
746 smb_rq_getrequest(rqp, &mbp);
750 mb_put_uint8(mbp, SMB_DT_ASCII);
751 error = smbfs_fullpath(mbp, SSTOVC(ssp), np, NULL, 0);
754 error = smb_rq_simple(rqp);
761 smbfs_smb_search(struct smbfs_fctx *ctx)
763 struct smb_vc *vcp = SSTOVC(ctx->f_ssp);
768 u_int16_t ec, dlen, bc;
769 int maxent, error, iseof = 0;
771 maxent = min(ctx->f_left, (vcp->vc_txmax - SMB_HDRLEN - 3) / SMB_DENTRYLEN);
773 smb_rq_done(ctx->f_rq);
776 error = smb_rq_alloc(SSTOCP(ctx->f_ssp), SMB_COM_SEARCH, ctx->f_scred, &rqp);
780 smb_rq_getrequest(rqp, &mbp);
782 mb_put_uint16le(mbp, maxent); /* max entries to return */
783 mb_put_uint16le(mbp, ctx->f_attrmask);
786 mb_put_uint8(mbp, SMB_DT_ASCII); /* buffer format */
787 if (ctx->f_flags & SMBFS_RDD_FINDFIRST) {
788 error = smbfs_fullpath(mbp, vcp, ctx->f_dnp, ctx->f_wildcard, ctx->f_wclen);
791 mb_put_uint8(mbp, SMB_DT_VARIABLE);
792 mb_put_uint16le(mbp, 0); /* context length */
793 ctx->f_flags &= ~SMBFS_RDD_FINDFIRST;
795 mb_put_uint8(mbp, 0); /* file name length */
796 mb_put_uint8(mbp, SMB_DT_VARIABLE);
797 mb_put_uint16le(mbp, SMB_SKEYLEN);
798 mb_put_mem(mbp, ctx->f_skey, SMB_SKEYLEN, MB_MSYSTEM);
801 error = smb_rq_simple(rqp);
803 if (rqp->sr_errclass == ERRDOS && rqp->sr_serror == ERRnofiles) {
806 ctx->f_flags |= SMBFS_RDD_EOF;
810 smb_rq_getreply(rqp, &mdp);
811 md_get_uint8(mdp, &wc);
813 return iseof ? ENOENT : EBADRPC;
814 md_get_uint16le(mdp, &ec);
818 md_get_uint16le(mdp, &bc);
822 md_get_uint8(mdp, &bt);
823 if (bt != SMB_DT_VARIABLE)
825 md_get_uint16le(mdp, &dlen);
826 if (dlen != bc || dlen % SMB_DENTRYLEN != 0)
832 smbfs_findopenLM1(struct smbfs_fctx *ctx, struct smbnode *dnp,
833 const char *wildcard, int wclen, int attr, struct smb_cred *scred)
835 ctx->f_attrmask = attr;
837 if (wclen == 1 && wildcard[0] == '*') {
838 ctx->f_wildcard = "*.*";
841 ctx->f_wildcard = wildcard;
842 ctx->f_wclen = wclen;
845 ctx->f_wildcard = NULL;
848 ctx->f_name = ctx->f_fname;
853 smbfs_findnextLM1(struct smbfs_fctx *ctx, int limit)
859 u_int16_t date, time;
863 if (ctx->f_ecnt == 0) {
864 if (ctx->f_flags & SMBFS_RDD_EOF)
866 ctx->f_left = ctx->f_limit = limit;
867 error = smbfs_smb_search(ctx);
872 smb_rq_getreply(rqp, &mbp);
873 md_get_mem(mbp, ctx->f_skey, SMB_SKEYLEN, MB_MSYSTEM);
874 md_get_uint8(mbp, &battr);
875 md_get_uint16le(mbp, &time);
876 md_get_uint16le(mbp, &date);
877 md_get_uint32le(mbp, &size);
879 md_get_mem(mbp, cp, sizeof(ctx->f_fname), MB_MSYSTEM);
880 cp[sizeof(ctx->f_fname) - 1] = 0;
881 cp += strlen(cp) - 1;
882 while (*cp == ' ' && cp >= ctx->f_name)
884 ctx->f_attr.fa_attr = battr;
885 smb_dos2unixtime(date, time, 0, rqp->sr_vc->vc_sopt.sv_tz,
886 &ctx->f_attr.fa_mtime);
887 ctx->f_attr.fa_size = size;
888 ctx->f_nmlen = strlen(ctx->f_name);
895 smbfs_findcloseLM1(struct smbfs_fctx *ctx)
898 smb_rq_done(ctx->f_rq);
903 * TRANS2_FIND_FIRST2/NEXT2, used for NT LM12 dialect
906 smbfs_smb_trans2find2(struct smbfs_fctx *ctx)
908 struct smb_t2rq *t2p;
909 struct smb_vc *vcp = SSTOVC(ctx->f_ssp);
916 smb_t2_done(ctx->f_t2);
919 ctx->f_flags &= ~SMBFS_RDD_GOTRNAME;
920 flags = 8 | 2; /* <resume> | <close if EOS> */
921 if (ctx->f_flags & SMBFS_RDD_FINDSINGLE) {
922 flags |= 1; /* close search after this request */
923 ctx->f_flags |= SMBFS_RDD_NOCLOSE;
925 if (ctx->f_flags & SMBFS_RDD_FINDFIRST) {
926 error = smb_t2_alloc(SSTOCP(ctx->f_ssp), SMB_TRANS2_FIND_FIRST2,
931 mbp = &t2p->t2_tparam;
933 mb_put_uint16le(mbp, ctx->f_attrmask);
934 mb_put_uint16le(mbp, ctx->f_limit);
935 mb_put_uint16le(mbp, flags);
936 mb_put_uint16le(mbp, ctx->f_infolevel);
937 mb_put_uint32le(mbp, 0);
938 error = smbfs_fullpath(mbp, vcp, ctx->f_dnp, ctx->f_wildcard, ctx->f_wclen);
942 error = smb_t2_alloc(SSTOCP(ctx->f_ssp), SMB_TRANS2_FIND_NEXT2,
947 mbp = &t2p->t2_tparam;
949 mb_put_mem(mbp, (caddr_t)&ctx->f_Sid, 2, MB_MSYSTEM);
950 mb_put_uint16le(mbp, ctx->f_limit);
951 mb_put_uint16le(mbp, ctx->f_infolevel);
952 mb_put_uint32le(mbp, 0); /* resume key */
953 mb_put_uint16le(mbp, flags);
955 mb_put_mem(mbp, ctx->f_rname, strlen(ctx->f_rname) + 1, MB_MSYSTEM);
957 mb_put_uint8(mbp, 0); /* resume file name */
961 tv.tv_usec = 200 * 1000; /* 200ms */
962 if (vcp->vc_flags & SMBC_WIN95) {
964 * some implementations suggests to sleep here
965 * for 200ms, due to the bug in the Win95.
966 * I've didn't notice any problem, but put code
969 tsleep(&flags, 0, "fix95", tvtohz_high(&tv));
973 t2p->t2_maxpcount = 5 * 2;
974 t2p->t2_maxdcount = vcp->vc_txmax;
975 error = smb_t2_request(t2p);
978 mdp = &t2p->t2_rparam;
979 if (ctx->f_flags & SMBFS_RDD_FINDFIRST) {
980 if ((error = md_get_uint16(mdp, &ctx->f_Sid)) != 0)
982 ctx->f_flags &= ~SMBFS_RDD_FINDFIRST;
984 if ((error = md_get_uint16le(mdp, &tw)) != 0)
987 if ((error = md_get_uint16le(mdp, &tw)) != 0)
990 ctx->f_flags |= SMBFS_RDD_EOF | SMBFS_RDD_NOCLOSE;
991 if ((error = md_get_uint16le(mdp, &tw)) != 0)
993 if ((error = md_get_uint16le(mdp, &tw)) != 0)
995 if (ctx->f_ecnt == 0)
997 ctx->f_rnameofs = tw;
998 mdp = &t2p->t2_rdata;
999 if (mdp->md_top == NULL) {
1000 kprintf("bug: ecnt = %d, but data is NULL (please report)\n", ctx->f_ecnt);
1003 if (mdp->md_top->m_len == 0) {
1004 kprintf("bug: ecnt = %d, but m_len = 0 and m_next = %p (please report)\n", ctx->f_ecnt,mbp->mb_top->m_next);
1012 smbfs_smb_findclose2(struct smbfs_fctx *ctx)
1014 struct smb_rq rq, *rqp = &rq;
1015 struct mbchain *mbp;
1018 error = smb_rq_init(rqp, SSTOCP(ctx->f_ssp), SMB_COM_FIND_CLOSE2, ctx->f_scred);
1021 smb_rq_getrequest(rqp, &mbp);
1023 mb_put_mem(mbp, (caddr_t)&ctx->f_Sid, 2, MB_MSYSTEM);
1027 error = smb_rq_simple(rqp);
1033 smbfs_findopenLM2(struct smbfs_fctx *ctx, struct smbnode *dnp,
1034 const char *wildcard, int wclen, int attr, struct smb_cred *scred)
1036 ctx->f_name = kmalloc(SMB_MAXFNAMELEN, M_SMBFSDATA, M_WAITOK);
1037 if (ctx->f_name == NULL)
1039 ctx->f_infolevel = SMB_DIALECT(SSTOVC(ctx->f_ssp)) < SMB_DIALECT_NTLM0_12 ?
1040 SMB_INFO_STANDARD : SMB_FIND_FILE_DIRECTORY_INFO;
1041 ctx->f_attrmask = attr;
1042 ctx->f_wildcard = wildcard;
1043 ctx->f_wclen = wclen;
1048 smbfs_findnextLM2(struct smbfs_fctx *ctx, int limit)
1050 struct mdchain *mbp;
1051 struct smb_t2rq *t2p;
1054 u_int16_t date, time, wattr;
1055 u_int32_t size, next, dattr;
1057 int error, svtz, cnt, fxsz, nmlen, recsz;
1059 if (ctx->f_ecnt == 0) {
1060 if (ctx->f_flags & SMBFS_RDD_EOF)
1062 ctx->f_left = ctx->f_limit = limit;
1063 error = smbfs_smb_trans2find2(ctx);
1068 mbp = &t2p->t2_rdata;
1069 svtz = SSTOVC(ctx->f_ssp)->vc_sopt.sv_tz;
1070 switch (ctx->f_infolevel) {
1071 case SMB_INFO_STANDARD:
1074 md_get_uint16le(mbp, &date);
1075 md_get_uint16le(mbp, &time); /* creation time */
1076 md_get_uint16le(mbp, &date);
1077 md_get_uint16le(mbp, &time); /* access time */
1078 smb_dos2unixtime(date, time, 0, svtz, &ctx->f_attr.fa_atime);
1079 md_get_uint16le(mbp, &date);
1080 md_get_uint16le(mbp, &time); /* access time */
1081 smb_dos2unixtime(date, time, 0, svtz, &ctx->f_attr.fa_mtime);
1082 md_get_uint32le(mbp, &size);
1083 ctx->f_attr.fa_size = size;
1084 md_get_uint32(mbp, NULL); /* allocation size */
1085 md_get_uint16le(mbp, &wattr);
1086 ctx->f_attr.fa_attr = wattr;
1087 md_get_uint8(mbp, &tb);
1090 recsz = next = 24 + nmlen; /* docs misses zero byte at end */
1092 case SMB_FIND_FILE_DIRECTORY_INFO:
1093 md_get_uint32le(mbp, &next);
1094 md_get_uint32(mbp, NULL); /* file index */
1095 md_get_int64(mbp, NULL); /* creation time */
1096 md_get_int64le(mbp, &lint);
1097 smb_time_NT2local(lint, svtz, &ctx->f_attr.fa_atime);
1098 md_get_int64le(mbp, &lint);
1099 smb_time_NT2local(lint, svtz, &ctx->f_attr.fa_mtime);
1100 md_get_int64le(mbp, &lint);
1101 smb_time_NT2local(lint, svtz, &ctx->f_attr.fa_ctime);
1102 md_get_int64le(mbp, &lint); /* file size */
1103 ctx->f_attr.fa_size = lint;
1104 md_get_int64(mbp, NULL); /* real size (should use) */
1105 md_get_uint32(mbp, &dattr); /* EA */
1106 ctx->f_attr.fa_attr = dattr;
1107 md_get_uint32le(mbp, &size); /* name len */
1109 recsz = next ? next : fxsz + size;
1112 SMBERROR("unexpected info level %d\n", ctx->f_infolevel);
1115 nmlen = min(size, SMB_MAXFNAMELEN);
1117 error = md_get_mem(mbp, cp, nmlen, MB_MSYSTEM);
1121 cnt = next - nmlen - fxsz;
1123 md_get_mem(mbp, NULL, cnt, MB_MSYSTEM);
1125 SMBERROR("out of sync\n");
1129 if (nmlen && cp[nmlen - 1] == 0)
1134 next = ctx->f_eofs + recsz;
1135 if (ctx->f_rnameofs && (ctx->f_flags & SMBFS_RDD_GOTRNAME) == 0 &&
1136 (ctx->f_rnameofs >= ctx->f_eofs && ctx->f_rnameofs < next)) {
1138 * Server needs a resume filename.
1140 if (ctx->f_rnamelen <= nmlen) {
1142 kfree(ctx->f_rname, M_SMBFSDATA);
1143 ctx->f_rname = kmalloc(nmlen + 1, M_SMBFSDATA, M_WAITOK);
1144 ctx->f_rnamelen = nmlen;
1146 bcopy(ctx->f_name, ctx->f_rname, nmlen);
1147 ctx->f_rname[nmlen] = 0;
1148 ctx->f_flags |= SMBFS_RDD_GOTRNAME;
1150 ctx->f_nmlen = nmlen;
1158 smbfs_findcloseLM2(struct smbfs_fctx *ctx)
1161 kfree(ctx->f_name, M_SMBFSDATA);
1163 smb_t2_done(ctx->f_t2);
1164 if ((ctx->f_flags & SMBFS_RDD_NOCLOSE) == 0)
1165 smbfs_smb_findclose2(ctx);
1170 smbfs_findopen(struct smbnode *dnp, const char *wildcard, int wclen, int attr,
1171 struct smb_cred *scred, struct smbfs_fctx **ctxpp)
1173 struct smbfs_fctx *ctx;
1176 ctx = kmalloc(sizeof(*ctx), M_SMBFSDATA, M_WAITOK);
1179 bzero(ctx, sizeof(*ctx));
1180 ctx->f_ssp = dnp->n_mount->sm_share;
1182 ctx->f_flags = SMBFS_RDD_FINDFIRST;
1183 ctx->f_scred = scred;
1184 if (SMB_DIALECT(SSTOVC(ctx->f_ssp)) < SMB_DIALECT_LANMAN2_0 ||
1185 (dnp->n_mount->sm_args.flags & SMBFS_MOUNT_NO_LONG)) {
1186 ctx->f_flags |= SMBFS_RDD_USESEARCH;
1187 error = smbfs_findopenLM1(ctx, dnp, wildcard, wclen, attr, scred);
1189 error = smbfs_findopenLM2(ctx, dnp, wildcard, wclen, attr, scred);
1191 smbfs_findclose(ctx, scred);
1198 smbfs_findnext(struct smbfs_fctx *ctx, int limit, struct smb_cred *scred)
1205 limit *= 4; /* imperical */
1206 ctx->f_scred = scred;
1208 if (ctx->f_flags & SMBFS_RDD_USESEARCH) {
1209 error = smbfs_findnextLM1(ctx, limit);
1211 error = smbfs_findnextLM2(ctx, limit);
1214 if ((ctx->f_nmlen == 1 && ctx->f_name[0] == '.') ||
1215 (ctx->f_nmlen == 2 && ctx->f_name[0] == '.' &&
1216 ctx->f_name[1] == '.'))
1220 smbfs_fname_tolocal(SSTOVC(ctx->f_ssp), ctx->f_name, ctx->f_nmlen,
1221 ctx->f_dnp->n_mount->sm_caseopt);
1222 ctx->f_attr.fa_ino = smbfs_getino(ctx->f_dnp, ctx->f_name, ctx->f_nmlen);
1227 smbfs_findclose(struct smbfs_fctx *ctx, struct smb_cred *scred)
1229 ctx->f_scred = scred;
1230 if (ctx->f_flags & SMBFS_RDD_USESEARCH) {
1231 smbfs_findcloseLM1(ctx);
1233 smbfs_findcloseLM2(ctx);
1235 kfree(ctx->f_rname, M_SMBFSDATA);
1236 kfree(ctx, M_SMBFSDATA);
1241 smbfs_smb_lookup(struct smbnode *dnp, const char *name, int nmlen,
1242 struct smbfattr *fap, struct smb_cred *scred)
1244 struct smbfs_fctx *ctx;
1247 if (dnp == NULL || (dnp->n_ino == 2 && name == NULL)) {
1248 bzero(fap, sizeof(*fap));
1249 fap->fa_attr = SMB_FA_DIR;
1253 if (nmlen == 1 && name[0] == '.') {
1254 error = smbfs_smb_lookup(dnp, NULL, 0, fap, scred);
1256 } else if (nmlen == 2 && name[0] == '.' && name[1] == '.') {
1257 error = smbfs_smb_lookup(VTOSMB(dnp->n_parent), NULL, 0, fap,
1259 kprintf("%s: knows NOTHING about '..'\n", __func__);
1262 error = smbfs_findopen(dnp, name, nmlen,
1263 SMB_FA_SYSTEM | SMB_FA_HIDDEN | SMB_FA_DIR, scred, &ctx);
1266 ctx->f_flags |= SMBFS_RDD_FINDSINGLE;
1267 error = smbfs_findnext(ctx, 1, scred);
1271 fap->fa_ino = dnp->n_ino;
1273 smbfs_findclose(ctx, scred);