xref: /freebsd/sys/fs/smbfs/smbfs_smb.c (revision fb8e9ead3789a74596144d6ffb43c98f805bb5c9)
1d167cf6fSWarner Losh /*-
2681a5bbeSBoris Popov  * Copyright (c) 2000-2001 Boris Popov
3681a5bbeSBoris Popov  * All rights reserved.
4681a5bbeSBoris Popov  *
5681a5bbeSBoris Popov  * Redistribution and use in source and binary forms, with or without
6681a5bbeSBoris Popov  * modification, are permitted provided that the following conditions
7681a5bbeSBoris Popov  * are met:
8681a5bbeSBoris Popov  * 1. Redistributions of source code must retain the above copyright
9681a5bbeSBoris Popov  *    notice, this list of conditions and the following disclaimer.
10681a5bbeSBoris Popov  * 2. Redistributions in binary form must reproduce the above copyright
11681a5bbeSBoris Popov  *    notice, this list of conditions and the following disclaimer in the
12681a5bbeSBoris Popov  *    documentation and/or other materials provided with the distribution.
13681a5bbeSBoris Popov  * 3. All advertising materials mentioning features or use of this software
14681a5bbeSBoris Popov  *    must display the following acknowledgement:
15681a5bbeSBoris Popov  *    This product includes software developed by Boris Popov.
16681a5bbeSBoris Popov  * 4. Neither the name of the author nor the names of any co-contributors
17681a5bbeSBoris Popov  *    may be used to endorse or promote products derived from this software
18681a5bbeSBoris Popov  *    without specific prior written permission.
19681a5bbeSBoris Popov  *
20681a5bbeSBoris Popov  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21681a5bbeSBoris Popov  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22681a5bbeSBoris Popov  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23681a5bbeSBoris Popov  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24681a5bbeSBoris Popov  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25681a5bbeSBoris Popov  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26681a5bbeSBoris Popov  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27681a5bbeSBoris Popov  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28681a5bbeSBoris Popov  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29681a5bbeSBoris Popov  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30681a5bbeSBoris Popov  * SUCH DAMAGE.
31681a5bbeSBoris Popov  *
32681a5bbeSBoris Popov  * $FreeBSD$
33681a5bbeSBoris Popov  */
34681a5bbeSBoris Popov #include <sys/param.h>
35681a5bbeSBoris Popov #include <sys/systm.h>
36681a5bbeSBoris Popov #include <sys/kernel.h>
37681a5bbeSBoris Popov #include <sys/malloc.h>
38681a5bbeSBoris Popov #include <sys/proc.h>
39681a5bbeSBoris Popov #include <sys/lock.h>
40681a5bbeSBoris Popov #include <sys/vnode.h>
41681a5bbeSBoris Popov #include <sys/mbuf.h>
42681a5bbeSBoris Popov #include <sys/mount.h>
43681a5bbeSBoris Popov 
44681a5bbeSBoris Popov #ifdef USE_MD5_HASH
45681a5bbeSBoris Popov #include <sys/md5.h>
46681a5bbeSBoris Popov #endif
47681a5bbeSBoris Popov 
48681a5bbeSBoris Popov #include <netsmb/smb.h>
49681a5bbeSBoris Popov #include <netsmb/smb_subr.h>
50681a5bbeSBoris Popov #include <netsmb/smb_rq.h>
51681a5bbeSBoris Popov #include <netsmb/smb_conn.h>
52681a5bbeSBoris Popov 
53681a5bbeSBoris Popov #include <fs/smbfs/smbfs.h>
54681a5bbeSBoris Popov #include <fs/smbfs/smbfs_node.h>
55681a5bbeSBoris Popov #include <fs/smbfs/smbfs_subr.h>
56681a5bbeSBoris Popov 
57681a5bbeSBoris Popov /*
58681a5bbeSBoris Popov  * Lack of inode numbers leads us to the problem of generating them.
59681a5bbeSBoris Popov  * Partially this problem can be solved by having a dir/file cache
60681a5bbeSBoris Popov  * with inode numbers generated from the incremented by one counter.
61681a5bbeSBoris Popov  * However this way will require too much kernel memory, gives all
62681a5bbeSBoris Popov  * sorts of locking and consistency problems, not to mentinon counter overflows.
63681a5bbeSBoris Popov  * So, I'm decided to use a hash function to generate pseudo random (and unique)
64681a5bbeSBoris Popov  * inode numbers.
65681a5bbeSBoris Popov  */
66681a5bbeSBoris Popov static long
67681a5bbeSBoris Popov smbfs_getino(struct smbnode *dnp, const char *name, int nmlen)
68681a5bbeSBoris Popov {
69681a5bbeSBoris Popov #ifdef USE_MD5_HASH
70681a5bbeSBoris Popov 	MD5_CTX md5;
71681a5bbeSBoris Popov 	u_int32_t state[4];
72681a5bbeSBoris Popov 	long ino;
73681a5bbeSBoris Popov 	int i;
74681a5bbeSBoris Popov 
75681a5bbeSBoris Popov 	MD5Init(&md5);
76681a5bbeSBoris Popov 	MD5Update(&md5, name, nmlen);
77681a5bbeSBoris Popov 	MD5Final((u_char *)state, &md5);
78681a5bbeSBoris Popov 	for (i = 0, ino = 0; i < 4; i++)
79681a5bbeSBoris Popov 		ino += state[i];
80681a5bbeSBoris Popov 	return dnp->n_ino + ino;
81681a5bbeSBoris Popov #endif
82681a5bbeSBoris Popov 	u_int32_t ino;
83681a5bbeSBoris Popov 
84681a5bbeSBoris Popov 	ino = dnp->n_ino + smbfs_hash(name, nmlen);
85681a5bbeSBoris Popov 	if (ino <= 2)
86681a5bbeSBoris Popov 		ino += 3;
87681a5bbeSBoris Popov 	return ino;
88681a5bbeSBoris Popov }
89681a5bbeSBoris Popov 
90681a5bbeSBoris Popov static int
91681a5bbeSBoris Popov smbfs_smb_lockandx(struct smbnode *np, int op, u_int32_t pid, off_t start, off_t end,
92681a5bbeSBoris Popov 	struct smb_cred *scred)
93681a5bbeSBoris Popov {
94681a5bbeSBoris Popov 	struct smb_share *ssp = np->n_mount->sm_share;
95681a5bbeSBoris Popov 	struct smb_rq rq, *rqp = &rq;
96681a5bbeSBoris Popov 	struct mbchain *mbp;
97681a5bbeSBoris Popov 	u_char ltype = 0;
98681a5bbeSBoris Popov 	int error;
99681a5bbeSBoris Popov 
100681a5bbeSBoris Popov 	if (op == SMB_LOCK_SHARED)
101681a5bbeSBoris Popov 		ltype |= SMB_LOCKING_ANDX_SHARED_LOCK;
102681a5bbeSBoris Popov 	error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_LOCKING_ANDX, scred);
103681a5bbeSBoris Popov 	if (error)
104681a5bbeSBoris Popov 		return error;
105681a5bbeSBoris Popov 	smb_rq_getrequest(rqp, &mbp);
106681a5bbeSBoris Popov 	smb_rq_wstart(rqp);
107681a5bbeSBoris Popov 	mb_put_uint8(mbp, 0xff);	/* secondary command */
108681a5bbeSBoris Popov 	mb_put_uint8(mbp, 0);		/* MBZ */
109681a5bbeSBoris Popov 	mb_put_uint16le(mbp, 0);
110681a5bbeSBoris Popov 	mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
111681a5bbeSBoris Popov 	mb_put_uint8(mbp, ltype);	/* locktype */
112681a5bbeSBoris Popov 	mb_put_uint8(mbp, 0);		/* oplocklevel - 0 seems is NO_OPLOCK */
113681a5bbeSBoris Popov 	mb_put_uint32le(mbp, 0);	/* timeout - break immediately */
114681a5bbeSBoris Popov 	mb_put_uint16le(mbp, op == SMB_LOCK_RELEASE ? 1 : 0);
115681a5bbeSBoris Popov 	mb_put_uint16le(mbp, op == SMB_LOCK_RELEASE ? 0 : 1);
116681a5bbeSBoris Popov 	smb_rq_wend(rqp);
117681a5bbeSBoris Popov 	smb_rq_bstart(rqp);
118681a5bbeSBoris Popov 	mb_put_uint16le(mbp, pid);
119681a5bbeSBoris Popov 	mb_put_uint32le(mbp, start);
120681a5bbeSBoris Popov 	mb_put_uint32le(mbp, end - start);
121681a5bbeSBoris Popov 	smb_rq_bend(rqp);
122681a5bbeSBoris Popov 	error = smb_rq_simple(rqp);
123681a5bbeSBoris Popov 	smb_rq_done(rqp);
124681a5bbeSBoris Popov 	return error;
125681a5bbeSBoris Popov }
126681a5bbeSBoris Popov 
127681a5bbeSBoris Popov int
128681a5bbeSBoris Popov smbfs_smb_lock(struct smbnode *np, int op, caddr_t id,
129681a5bbeSBoris Popov 	off_t start, off_t end,	struct smb_cred *scred)
130681a5bbeSBoris Popov {
131681a5bbeSBoris Popov 	struct smb_share *ssp = np->n_mount->sm_share;
132681a5bbeSBoris Popov 
133681a5bbeSBoris Popov 	if (SMB_DIALECT(SSTOVC(ssp)) < SMB_DIALECT_LANMAN1_0)
134681a5bbeSBoris Popov 		/*
135681a5bbeSBoris Popov 		 * TODO: use LOCK_BYTE_RANGE here.
136681a5bbeSBoris Popov 		 */
137681a5bbeSBoris Popov 		return EINVAL;
138681a5bbeSBoris Popov 	else
13995c2dc84SJohn Baldwin 		return smbfs_smb_lockandx(np, op, (uintptr_t)id, start, end, scred);
140681a5bbeSBoris Popov }
141681a5bbeSBoris Popov 
1423c2f5c3cSBoris Popov static int
1433c2f5c3cSBoris Popov smbfs_smb_qpathinfo(struct smbnode *np, struct smbfattr *fap,
1443c2f5c3cSBoris Popov 		    struct smb_cred *scred, short infolevel)
1453c2f5c3cSBoris Popov {
1463c2f5c3cSBoris Popov 	struct smb_share *ssp = np->n_mount->sm_share;
1473c2f5c3cSBoris Popov 	struct smb_vc *vcp = SSTOVC(ssp);
1483c2f5c3cSBoris Popov 	struct smb_t2rq *t2p;
1493c2f5c3cSBoris Popov 	int error, svtz, timesok = 1;
1503c2f5c3cSBoris Popov 	struct mbchain *mbp;
1513c2f5c3cSBoris Popov 	struct mdchain *mdp;
1523c2f5c3cSBoris Popov 	u_int16_t date, time, wattr;
1533c2f5c3cSBoris Popov 	int64_t lint;
1543c2f5c3cSBoris Popov 	u_int32_t size, dattr;
1553c2f5c3cSBoris Popov 
1563c2f5c3cSBoris Popov 	error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_QUERY_PATH_INFORMATION,
1573c2f5c3cSBoris Popov 			     scred, &t2p);
1583c2f5c3cSBoris Popov 	if (error)
1593c2f5c3cSBoris Popov 		return error;
1603c2f5c3cSBoris Popov 	mbp = &t2p->t2_tparam;
1613c2f5c3cSBoris Popov 	mb_init(mbp);
1623c2f5c3cSBoris Popov 	if (!infolevel) {
1633c2f5c3cSBoris Popov 		if (SMB_DIALECT(vcp) < SMB_DIALECT_NTLM0_12)
1643c2f5c3cSBoris Popov 			infolevel = SMB_QUERY_FILE_STANDARD;
1653c2f5c3cSBoris Popov 		else
1663c2f5c3cSBoris Popov 			infolevel = SMB_QUERY_FILE_BASIC_INFO;
1673c2f5c3cSBoris Popov 	}
1683c2f5c3cSBoris Popov 	mb_put_uint16le(mbp, infolevel);
1693c2f5c3cSBoris Popov 	mb_put_uint32le(mbp, 0);
1703c2f5c3cSBoris Popov 	/* mb_put_uint8(mbp, SMB_DT_ASCII); specs are wrong */
1713c2f5c3cSBoris Popov 	error = smbfs_fullpath(mbp, vcp, np, NULL, 0);
1723c2f5c3cSBoris Popov 	if (error) {
1733c2f5c3cSBoris Popov 		smb_t2_done(t2p);
1743c2f5c3cSBoris Popov 		return error;
1753c2f5c3cSBoris Popov 	}
1763c2f5c3cSBoris Popov 	t2p->t2_maxpcount = 2;
1773c2f5c3cSBoris Popov 	t2p->t2_maxdcount = vcp->vc_txmax;
1783c2f5c3cSBoris Popov 	error = smb_t2_request(t2p);
1793c2f5c3cSBoris Popov 	if (error) {
1803c2f5c3cSBoris Popov 		smb_t2_done(t2p);
1813c2f5c3cSBoris Popov 		if (infolevel == SMB_QUERY_FILE_STANDARD || error != EINVAL)
1823c2f5c3cSBoris Popov 			return error;
1833c2f5c3cSBoris Popov 		return smbfs_smb_qpathinfo(np, fap, scred,
1843c2f5c3cSBoris Popov 					   SMB_QUERY_FILE_STANDARD);
1853c2f5c3cSBoris Popov 	}
1863c2f5c3cSBoris Popov 	mdp = &t2p->t2_rdata;
1873c2f5c3cSBoris Popov 	svtz = vcp->vc_sopt.sv_tz;
1883c2f5c3cSBoris Popov 	switch (infolevel) {
1893c2f5c3cSBoris Popov 	    case SMB_QUERY_FILE_STANDARD:
1903c2f5c3cSBoris Popov 		timesok = 0;
1913c2f5c3cSBoris Popov 		md_get_uint16le(mdp, NULL);
1923c2f5c3cSBoris Popov 		md_get_uint16le(mdp, NULL);	/* creation time */
1933c2f5c3cSBoris Popov 		md_get_uint16le(mdp, &date);
1943c2f5c3cSBoris Popov 		md_get_uint16le(mdp, &time);	/* access time */
1953c2f5c3cSBoris Popov 		if (date || time) {
1963c2f5c3cSBoris Popov 			timesok++;
1973c2f5c3cSBoris Popov 			smb_dos2unixtime(date, time, 0, svtz, &fap->fa_atime);
1983c2f5c3cSBoris Popov 		}
1993c2f5c3cSBoris Popov 		md_get_uint16le(mdp, &date);
2003c2f5c3cSBoris Popov 		md_get_uint16le(mdp, &time);	/* modify time */
2013c2f5c3cSBoris Popov 		if (date || time) {
2023c2f5c3cSBoris Popov 			timesok++;
2033c2f5c3cSBoris Popov 			smb_dos2unixtime(date, time, 0, svtz, &fap->fa_mtime);
2043c2f5c3cSBoris Popov 		}
2053c2f5c3cSBoris Popov 		md_get_uint32le(mdp, &size);
2063c2f5c3cSBoris Popov 		fap->fa_size = size;
2073c2f5c3cSBoris Popov 		md_get_uint32(mdp, NULL);	/* allocation size */
2083c2f5c3cSBoris Popov 		md_get_uint16le(mdp, &wattr);
2093c2f5c3cSBoris Popov 		fap->fa_attr = wattr;
2103c2f5c3cSBoris Popov 		break;
2113c2f5c3cSBoris Popov 	    case SMB_QUERY_FILE_BASIC_INFO:
2123c2f5c3cSBoris Popov 		timesok = 0;
2133c2f5c3cSBoris Popov 		md_get_int64(mdp, NULL);	/* creation time */
2143c2f5c3cSBoris Popov 		md_get_int64le(mdp, &lint);
2153c2f5c3cSBoris Popov 		if (lint) {
2163c2f5c3cSBoris Popov 			timesok++;
2173c2f5c3cSBoris Popov 			smb_time_NT2local(lint, svtz, &fap->fa_atime);
2183c2f5c3cSBoris Popov 		}
2193c2f5c3cSBoris Popov 		md_get_int64le(mdp, &lint);
2203c2f5c3cSBoris Popov 		if (lint) {
2213c2f5c3cSBoris Popov 			timesok++;
2223c2f5c3cSBoris Popov 			smb_time_NT2local(lint, svtz, &fap->fa_mtime);
2233c2f5c3cSBoris Popov 		}
2243c2f5c3cSBoris Popov 		md_get_int64le(mdp, &lint);
2253c2f5c3cSBoris Popov 		if (lint) {
2263c2f5c3cSBoris Popov 			timesok++;
2273c2f5c3cSBoris Popov 			smb_time_NT2local(lint, svtz, &fap->fa_ctime);
2283c2f5c3cSBoris Popov 		}
2293c2f5c3cSBoris Popov 		md_get_uint32le(mdp, &dattr);
2303c2f5c3cSBoris Popov 		fap->fa_attr = dattr;
2313c2f5c3cSBoris Popov 		md_get_uint32(mdp, NULL);
2323c2f5c3cSBoris Popov 		/* XXX could use ALL_INFO to get size */
2333c2f5c3cSBoris Popov 		break;
2343c2f5c3cSBoris Popov 	    default:
2353c2f5c3cSBoris Popov 		SMBERROR("unexpected info level %d\n", infolevel);
2363c2f5c3cSBoris Popov 		error = EINVAL;
2373c2f5c3cSBoris Popov 	}
2383c2f5c3cSBoris Popov 	smb_t2_done(t2p);
2393c2f5c3cSBoris Popov 	/*
2403c2f5c3cSBoris Popov 	 * if all times are zero (observed with FAT on NT4SP6)
2413c2f5c3cSBoris Popov 	 * then fall back to older info level
2423c2f5c3cSBoris Popov 	 */
2433c2f5c3cSBoris Popov 	if (!timesok) {
2443c2f5c3cSBoris Popov 		if (infolevel != SMB_QUERY_FILE_STANDARD)
2453c2f5c3cSBoris Popov 			return smbfs_smb_qpathinfo(np, fap, scred,
2463c2f5c3cSBoris Popov 						   SMB_QUERY_FILE_STANDARD);
2473c2f5c3cSBoris Popov 		error = EINVAL;
2483c2f5c3cSBoris Popov 	}
2493c2f5c3cSBoris Popov 	return error;
2503c2f5c3cSBoris Popov }
2513c2f5c3cSBoris Popov 
252681a5bbeSBoris Popov int
253681a5bbeSBoris Popov smbfs_smb_statfs2(struct smb_share *ssp, struct statfs *sbp,
254681a5bbeSBoris Popov 	struct smb_cred *scred)
255681a5bbeSBoris Popov {
256681a5bbeSBoris Popov 	struct smb_t2rq *t2p;
257681a5bbeSBoris Popov 	struct mbchain *mbp;
258681a5bbeSBoris Popov 	struct mdchain *mdp;
259681a5bbeSBoris Popov 	u_int16_t bsize;
260681a5bbeSBoris Popov 	u_int32_t units, bpu, funits;
261681a5bbeSBoris Popov 	int error;
262681a5bbeSBoris Popov 
263681a5bbeSBoris Popov 	error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_QUERY_FS_INFORMATION,
264681a5bbeSBoris Popov 	    scred, &t2p);
265681a5bbeSBoris Popov 	if (error)
266681a5bbeSBoris Popov 		return error;
267681a5bbeSBoris Popov 	mbp = &t2p->t2_tparam;
268681a5bbeSBoris Popov 	mb_init(mbp);
269681a5bbeSBoris Popov 	mb_put_uint16le(mbp, SMB_INFO_ALLOCATION);
270681a5bbeSBoris Popov 	t2p->t2_maxpcount = 4;
271681a5bbeSBoris Popov 	t2p->t2_maxdcount = 4 * 4 + 2;
272681a5bbeSBoris Popov 	error = smb_t2_request(t2p);
273681a5bbeSBoris Popov 	if (error) {
274681a5bbeSBoris Popov 		smb_t2_done(t2p);
275681a5bbeSBoris Popov 		return error;
276681a5bbeSBoris Popov 	}
277681a5bbeSBoris Popov 	mdp = &t2p->t2_rdata;
278681a5bbeSBoris Popov 	md_get_uint32(mdp, NULL);	/* fs id */
279681a5bbeSBoris Popov 	md_get_uint32le(mdp, &bpu);
280681a5bbeSBoris Popov 	md_get_uint32le(mdp, &units);
281681a5bbeSBoris Popov 	md_get_uint32le(mdp, &funits);
282681a5bbeSBoris Popov 	md_get_uint16le(mdp, &bsize);
283681a5bbeSBoris Popov 	sbp->f_bsize = bpu * bsize;	/* fundamental filesystem block size */
284681a5bbeSBoris Popov 	sbp->f_blocks= units;		/* total data blocks in filesystem */
285681a5bbeSBoris Popov 	sbp->f_bfree = funits;		/* free blocks in fs */
286681a5bbeSBoris Popov 	sbp->f_bavail= funits;		/* free blocks avail to non-superuser */
287681a5bbeSBoris Popov 	sbp->f_files = 0xffff;		/* total file nodes in filesystem */
288681a5bbeSBoris Popov 	sbp->f_ffree = 0xffff;		/* free file nodes in fs */
289681a5bbeSBoris Popov 	smb_t2_done(t2p);
290681a5bbeSBoris Popov 	return 0;
291681a5bbeSBoris Popov }
292681a5bbeSBoris Popov 
293681a5bbeSBoris Popov int
294681a5bbeSBoris Popov smbfs_smb_statfs(struct smb_share *ssp, struct statfs *sbp,
295681a5bbeSBoris Popov 	struct smb_cred *scred)
296681a5bbeSBoris Popov {
297681a5bbeSBoris Popov 	struct smb_rq rq, *rqp = &rq;
298681a5bbeSBoris Popov 	struct mdchain *mdp;
299681a5bbeSBoris Popov 	u_int16_t units, bpu, bsize, funits;
300681a5bbeSBoris Popov 	int error;
301681a5bbeSBoris Popov 
302681a5bbeSBoris Popov 	error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_QUERY_INFORMATION_DISK, scred);
303681a5bbeSBoris Popov 	if (error)
304681a5bbeSBoris Popov 		return error;
305681a5bbeSBoris Popov 	smb_rq_wstart(rqp);
306681a5bbeSBoris Popov 	smb_rq_wend(rqp);
307681a5bbeSBoris Popov 	smb_rq_bstart(rqp);
308681a5bbeSBoris Popov 	smb_rq_bend(rqp);
309681a5bbeSBoris Popov 	error = smb_rq_simple(rqp);
310681a5bbeSBoris Popov 	if (error) {
311681a5bbeSBoris Popov 		smb_rq_done(rqp);
312681a5bbeSBoris Popov 		return error;
313681a5bbeSBoris Popov 	}
314681a5bbeSBoris Popov 	smb_rq_getreply(rqp, &mdp);
315681a5bbeSBoris Popov 	md_get_uint16le(mdp, &units);
316681a5bbeSBoris Popov 	md_get_uint16le(mdp, &bpu);
317681a5bbeSBoris Popov 	md_get_uint16le(mdp, &bsize);
318681a5bbeSBoris Popov 	md_get_uint16le(mdp, &funits);
319681a5bbeSBoris Popov 	sbp->f_bsize = bpu * bsize;	/* fundamental filesystem block size */
320681a5bbeSBoris Popov 	sbp->f_blocks= units;		/* total data blocks in filesystem */
321681a5bbeSBoris Popov 	sbp->f_bfree = funits;		/* free blocks in fs */
322681a5bbeSBoris Popov 	sbp->f_bavail= funits;		/* free blocks avail to non-superuser */
323681a5bbeSBoris Popov 	sbp->f_files = 0xffff;		/* total file nodes in filesystem */
324681a5bbeSBoris Popov 	sbp->f_ffree = 0xffff;		/* free file nodes in fs */
325681a5bbeSBoris Popov 	smb_rq_done(rqp);
326681a5bbeSBoris Popov 	return 0;
327681a5bbeSBoris Popov }
328681a5bbeSBoris Popov 
3293c2f5c3cSBoris Popov static int
3303c2f5c3cSBoris Popov smbfs_smb_seteof(struct smbnode *np, int64_t newsize, struct smb_cred *scred)
3313c2f5c3cSBoris Popov {
3323c2f5c3cSBoris Popov 	struct smb_t2rq *t2p;
3333c2f5c3cSBoris Popov 	struct smb_share *ssp = np->n_mount->sm_share;
3343c2f5c3cSBoris Popov 	struct mbchain *mbp;
3353c2f5c3cSBoris Popov 	int error;
3363c2f5c3cSBoris Popov 
3373c2f5c3cSBoris Popov 	error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_SET_FILE_INFORMATION,
3383c2f5c3cSBoris Popov 	    scred, &t2p);
3393c2f5c3cSBoris Popov 	if (error)
3403c2f5c3cSBoris Popov 		return error;
3413c2f5c3cSBoris Popov 	mbp = &t2p->t2_tparam;
3423c2f5c3cSBoris Popov 	mb_init(mbp);
3433c2f5c3cSBoris Popov 	mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
3443c2f5c3cSBoris Popov 	mb_put_uint16le(mbp, SMB_SET_FILE_END_OF_FILE_INFO);
3453c2f5c3cSBoris Popov 	mb_put_uint32le(mbp, 0);
3463c2f5c3cSBoris Popov 	mbp = &t2p->t2_tdata;
3473c2f5c3cSBoris Popov 	mb_init(mbp);
3483c2f5c3cSBoris Popov 	mb_put_int64le(mbp, newsize);
3493c2f5c3cSBoris Popov 	mb_put_uint32le(mbp, 0);			/* padding */
3503c2f5c3cSBoris Popov 	mb_put_uint16le(mbp, 0);
3513c2f5c3cSBoris Popov 	t2p->t2_maxpcount = 2;
3523c2f5c3cSBoris Popov 	t2p->t2_maxdcount = 0;
3533c2f5c3cSBoris Popov 	error = smb_t2_request(t2p);
3543c2f5c3cSBoris Popov 	smb_t2_done(t2p);
3553c2f5c3cSBoris Popov 	return error;
3563c2f5c3cSBoris Popov }
3573c2f5c3cSBoris Popov 
3583c2f5c3cSBoris Popov static int
3593c2f5c3cSBoris Popov smb_smb_flush(struct smbnode *np, struct smb_cred *scred)
3603c2f5c3cSBoris Popov {
3613c2f5c3cSBoris Popov 	struct smb_share *ssp = np->n_mount->sm_share;
3623c2f5c3cSBoris Popov 	struct smb_rq rq, *rqp = &rq;
3633c2f5c3cSBoris Popov 	struct mbchain *mbp;
3643c2f5c3cSBoris Popov 	int error;
3653c2f5c3cSBoris Popov 
36608fe4bfbSTim J. Robbins 	if ((np->n_flag & NOPEN) == 0 || !SMBTOV(np) ||
3672a4ad258STim J. Robbins 	    SMBTOV(np)->v_type != VREG)
368d64ada50SJens Schweikhardt 		return 0; /* not a regular open file */
3693c2f5c3cSBoris Popov 	error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_FLUSH, scred);
3703c2f5c3cSBoris Popov 	if (error)
3713c2f5c3cSBoris Popov 		return (error);
3723c2f5c3cSBoris Popov 	smb_rq_getrequest(rqp, &mbp);
3733c2f5c3cSBoris Popov 	smb_rq_wstart(rqp);
3743c2f5c3cSBoris Popov 	mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
3753c2f5c3cSBoris Popov 	smb_rq_wend(rqp);
3763c2f5c3cSBoris Popov 	smb_rq_bstart(rqp);
3773c2f5c3cSBoris Popov 	smb_rq_bend(rqp);
3783c2f5c3cSBoris Popov 	error = smb_rq_simple(rqp);
3793c2f5c3cSBoris Popov 	smb_rq_done(rqp);
3803c2f5c3cSBoris Popov 	if (!error)
3813c2f5c3cSBoris Popov 		np->n_flag &= ~NFLUSHWIRE;
3823c2f5c3cSBoris Popov 	return (error);
3833c2f5c3cSBoris Popov }
3843c2f5c3cSBoris Popov 
3853c2f5c3cSBoris Popov int
3863c2f5c3cSBoris Popov smbfs_smb_flush(struct smbnode *np, struct smb_cred *scred)
3873c2f5c3cSBoris Popov {
3883c2f5c3cSBoris Popov 	if (np->n_flag & NFLUSHWIRE)
3893c2f5c3cSBoris Popov 		return (smb_smb_flush(np, scred));
3903c2f5c3cSBoris Popov 	return (0);
3913c2f5c3cSBoris Popov }
3923c2f5c3cSBoris Popov 
393681a5bbeSBoris Popov int
394681a5bbeSBoris Popov smbfs_smb_setfsize(struct smbnode *np, int newsize, struct smb_cred *scred)
395681a5bbeSBoris Popov {
396681a5bbeSBoris Popov 	struct smb_share *ssp = np->n_mount->sm_share;
397681a5bbeSBoris Popov 	struct smb_rq rq, *rqp = &rq;
398681a5bbeSBoris Popov 	struct mbchain *mbp;
399681a5bbeSBoris Popov 	int error;
400681a5bbeSBoris Popov 
4013c2f5c3cSBoris Popov 	if (!smbfs_smb_seteof(np, (int64_t) newsize, scred)) {
4023c2f5c3cSBoris Popov 		np->n_flag |= NFLUSHWIRE;
4033c2f5c3cSBoris Popov 		return (0);
4043c2f5c3cSBoris Popov 	}
4053c2f5c3cSBoris Popov 
406681a5bbeSBoris Popov 	error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_WRITE, scred);
407681a5bbeSBoris Popov 	if (error)
408681a5bbeSBoris Popov 		return error;
409681a5bbeSBoris Popov 	smb_rq_getrequest(rqp, &mbp);
410681a5bbeSBoris Popov 	smb_rq_wstart(rqp);
411681a5bbeSBoris Popov 	mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
412681a5bbeSBoris Popov 	mb_put_uint16le(mbp, 0);
413681a5bbeSBoris Popov 	mb_put_uint32le(mbp, newsize);
414681a5bbeSBoris Popov 	mb_put_uint16le(mbp, 0);
415681a5bbeSBoris Popov 	smb_rq_wend(rqp);
416681a5bbeSBoris Popov 	smb_rq_bstart(rqp);
417681a5bbeSBoris Popov 	mb_put_uint8(mbp, SMB_DT_DATA);
418681a5bbeSBoris Popov 	mb_put_uint16le(mbp, 0);
419681a5bbeSBoris Popov 	smb_rq_bend(rqp);
420681a5bbeSBoris Popov 	error = smb_rq_simple(rqp);
421681a5bbeSBoris Popov 	smb_rq_done(rqp);
422681a5bbeSBoris Popov 	return error;
423681a5bbeSBoris Popov }
424681a5bbeSBoris Popov 
4253c2f5c3cSBoris Popov int
4263c2f5c3cSBoris Popov smbfs_smb_query_info(struct smbnode *np, const char *name, int len,
4273c2f5c3cSBoris Popov 		     struct smbfattr *fap, struct smb_cred *scred)
4283c2f5c3cSBoris Popov {
4293c2f5c3cSBoris Popov 	struct smb_rq rq, *rqp = &rq;
4303c2f5c3cSBoris Popov 	struct smb_share *ssp = np->n_mount->sm_share;
4313c2f5c3cSBoris Popov 	struct mbchain *mbp;
4323c2f5c3cSBoris Popov 	struct mdchain *mdp;
4333c2f5c3cSBoris Popov 	u_int8_t wc;
4343c2f5c3cSBoris Popov 	int error;
4353c2f5c3cSBoris Popov 	u_int16_t wattr;
4363c2f5c3cSBoris Popov 	u_int32_t lint;
4373c2f5c3cSBoris Popov 
4383c2f5c3cSBoris Popov 	error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_QUERY_INFORMATION, scred);
4393c2f5c3cSBoris Popov 	if (error)
4403c2f5c3cSBoris Popov 		return error;
4413c2f5c3cSBoris Popov 	smb_rq_getrequest(rqp, &mbp);
4423c2f5c3cSBoris Popov 	smb_rq_wstart(rqp);
4433c2f5c3cSBoris Popov 	smb_rq_wend(rqp);
4443c2f5c3cSBoris Popov 	smb_rq_bstart(rqp);
4453c2f5c3cSBoris Popov 	mb_put_uint8(mbp, SMB_DT_ASCII);
4463c2f5c3cSBoris Popov 	do {
4473c2f5c3cSBoris Popov 		error = smbfs_fullpath(mbp, SSTOVC(ssp), np, name, len);
4483c2f5c3cSBoris Popov 		if (error)
4493c2f5c3cSBoris Popov 			break;
4503c2f5c3cSBoris Popov 		smb_rq_bend(rqp);
4513c2f5c3cSBoris Popov 		error = smb_rq_simple(rqp);
4523c2f5c3cSBoris Popov 		if (error)
4533c2f5c3cSBoris Popov 			break;
4543c2f5c3cSBoris Popov 		smb_rq_getreply(rqp, &mdp);
4553c2f5c3cSBoris Popov 		if (md_get_uint8(mdp, &wc) != 0 || wc != 10) {
4563c2f5c3cSBoris Popov 			error = EBADRPC;
4573c2f5c3cSBoris Popov 			break;
4583c2f5c3cSBoris Popov 		}
4593c2f5c3cSBoris Popov 		md_get_uint16le(mdp, &wattr);
4603c2f5c3cSBoris Popov 		fap->fa_attr = wattr;
4613c2f5c3cSBoris Popov 		/*
4623c2f5c3cSBoris Popov 		 * Be careful using the time returned here, as
4633c2f5c3cSBoris Popov 		 * with FAT on NT4SP6, at least, the time returned is low
4643c2f5c3cSBoris Popov 		 * 32 bits of 100s of nanoseconds (since 1601) so it rolls
4653c2f5c3cSBoris Popov 		 * over about every seven minutes!
4663c2f5c3cSBoris Popov 		 */
4673c2f5c3cSBoris Popov 		md_get_uint32le(mdp, &lint); /* specs: secs since 1970 */
4683c2f5c3cSBoris Popov 		if (lint)	/* avoid bogus zero returns */
4693c2f5c3cSBoris Popov 			smb_time_server2local(lint, SSTOVC(ssp)->vc_sopt.sv_tz,
4703c2f5c3cSBoris Popov 					      &fap->fa_mtime);
4713c2f5c3cSBoris Popov 		md_get_uint32le(mdp, &lint);
4723c2f5c3cSBoris Popov 		fap->fa_size = lint;
4733c2f5c3cSBoris Popov 	} while(0);
4743c2f5c3cSBoris Popov 	smb_rq_done(rqp);
4753c2f5c3cSBoris Popov 	return error;
4763c2f5c3cSBoris Popov }
477681a5bbeSBoris Popov 
478681a5bbeSBoris Popov /*
479681a5bbeSBoris Popov  * Set DOS file attributes. mtime should be NULL for dialects above lm10
480681a5bbeSBoris Popov  */
481681a5bbeSBoris Popov int
482681a5bbeSBoris Popov smbfs_smb_setpattr(struct smbnode *np, u_int16_t attr, struct timespec *mtime,
483681a5bbeSBoris Popov 	struct smb_cred *scred)
484681a5bbeSBoris Popov {
485681a5bbeSBoris Popov 	struct smb_rq rq, *rqp = &rq;
486681a5bbeSBoris Popov 	struct smb_share *ssp = np->n_mount->sm_share;
487681a5bbeSBoris Popov 	struct mbchain *mbp;
488681a5bbeSBoris Popov 	u_long time;
489681a5bbeSBoris Popov 	int error, svtz;
490681a5bbeSBoris Popov 
491681a5bbeSBoris Popov 	error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_SET_INFORMATION, scred);
492681a5bbeSBoris Popov 	if (error)
493681a5bbeSBoris Popov 		return error;
494681a5bbeSBoris Popov 	svtz = SSTOVC(ssp)->vc_sopt.sv_tz;
495681a5bbeSBoris Popov 	smb_rq_getrequest(rqp, &mbp);
496681a5bbeSBoris Popov 	smb_rq_wstart(rqp);
497681a5bbeSBoris Popov 	mb_put_uint16le(mbp, attr);
498681a5bbeSBoris Popov 	if (mtime) {
499681a5bbeSBoris Popov 		smb_time_local2server(mtime, svtz, &time);
500681a5bbeSBoris Popov 	} else
501681a5bbeSBoris Popov 		time = 0;
502681a5bbeSBoris Popov 	mb_put_uint32le(mbp, time);		/* mtime */
503681a5bbeSBoris Popov 	mb_put_mem(mbp, NULL, 5 * 2, MB_MZERO);
504681a5bbeSBoris Popov 	smb_rq_wend(rqp);
505681a5bbeSBoris Popov 	smb_rq_bstart(rqp);
506681a5bbeSBoris Popov 	mb_put_uint8(mbp, SMB_DT_ASCII);
507681a5bbeSBoris Popov 	do {
508681a5bbeSBoris Popov 		error = smbfs_fullpath(mbp, SSTOVC(ssp), np, NULL, 0);
509681a5bbeSBoris Popov 		if (error)
510681a5bbeSBoris Popov 			break;
511681a5bbeSBoris Popov 		mb_put_uint8(mbp, SMB_DT_ASCII);
512681a5bbeSBoris Popov 		mb_put_uint8(mbp, 0);
513681a5bbeSBoris Popov 		smb_rq_bend(rqp);
514681a5bbeSBoris Popov 		error = smb_rq_simple(rqp);
515fb8e9eadSBoris Popov 		if (error) {
516fb8e9eadSBoris Popov 			SMBERROR("smb_rq_simple(rqp) => error %d\n", error);
517681a5bbeSBoris Popov 			break;
518fb8e9eadSBoris Popov 		}
519681a5bbeSBoris Popov 	} while(0);
520681a5bbeSBoris Popov 	smb_rq_done(rqp);
521681a5bbeSBoris Popov 	return error;
522681a5bbeSBoris Popov }
523681a5bbeSBoris Popov 
524681a5bbeSBoris Popov /*
525681a5bbeSBoris Popov  * Note, win95 doesn't support this call.
526681a5bbeSBoris Popov  */
527681a5bbeSBoris Popov int
528681a5bbeSBoris Popov smbfs_smb_setptime2(struct smbnode *np, struct timespec *mtime,
529681a5bbeSBoris Popov 	struct timespec *atime, int attr, struct smb_cred *scred)
530681a5bbeSBoris Popov {
531681a5bbeSBoris Popov 	struct smb_t2rq *t2p;
532681a5bbeSBoris Popov 	struct smb_share *ssp = np->n_mount->sm_share;
533681a5bbeSBoris Popov 	struct smb_vc *vcp = SSTOVC(ssp);
534681a5bbeSBoris Popov 	struct mbchain *mbp;
535681a5bbeSBoris Popov 	u_int16_t date, time;
536681a5bbeSBoris Popov 	int error, tzoff;
537681a5bbeSBoris Popov 
538681a5bbeSBoris Popov 	error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_SET_PATH_INFORMATION,
539681a5bbeSBoris Popov 	    scred, &t2p);
540681a5bbeSBoris Popov 	if (error)
541681a5bbeSBoris Popov 		return error;
542681a5bbeSBoris Popov 	mbp = &t2p->t2_tparam;
543681a5bbeSBoris Popov 	mb_init(mbp);
544681a5bbeSBoris Popov 	mb_put_uint16le(mbp, SMB_INFO_STANDARD);
545681a5bbeSBoris Popov 	mb_put_uint32le(mbp, 0);		/* MBZ */
5463c2f5c3cSBoris Popov 	/* mb_put_uint8(mbp, SMB_DT_ASCII); specs incorrect */
547681a5bbeSBoris Popov 	error = smbfs_fullpath(mbp, vcp, np, NULL, 0);
548681a5bbeSBoris Popov 	if (error) {
549681a5bbeSBoris Popov 		smb_t2_done(t2p);
550681a5bbeSBoris Popov 		return error;
551681a5bbeSBoris Popov 	}
552681a5bbeSBoris Popov 	tzoff = vcp->vc_sopt.sv_tz;
553681a5bbeSBoris Popov 	mbp = &t2p->t2_tdata;
554681a5bbeSBoris Popov 	mb_init(mbp);
555681a5bbeSBoris Popov 	mb_put_uint32le(mbp, 0);		/* creation time */
556681a5bbeSBoris Popov 	if (atime)
557681a5bbeSBoris Popov 		smb_time_unix2dos(atime, tzoff, &date, &time, NULL);
558681a5bbeSBoris Popov 	else
559681a5bbeSBoris Popov 		time = date = 0;
560681a5bbeSBoris Popov 	mb_put_uint16le(mbp, date);
561681a5bbeSBoris Popov 	mb_put_uint16le(mbp, time);
562681a5bbeSBoris Popov 	if (mtime)
563681a5bbeSBoris Popov 		smb_time_unix2dos(mtime, tzoff, &date, &time, NULL);
564681a5bbeSBoris Popov 	else
565681a5bbeSBoris Popov 		time = date = 0;
566681a5bbeSBoris Popov 	mb_put_uint16le(mbp, date);
567681a5bbeSBoris Popov 	mb_put_uint16le(mbp, time);
568681a5bbeSBoris Popov 	mb_put_uint32le(mbp, 0);		/* file size */
569681a5bbeSBoris Popov 	mb_put_uint32le(mbp, 0);		/* allocation unit size */
570681a5bbeSBoris Popov 	mb_put_uint16le(mbp, attr);	/* DOS attr */
571681a5bbeSBoris Popov 	mb_put_uint32le(mbp, 0);		/* EA size */
572681a5bbeSBoris Popov 	t2p->t2_maxpcount = 5 * 2;
573681a5bbeSBoris Popov 	t2p->t2_maxdcount = vcp->vc_txmax;
574681a5bbeSBoris Popov 	error = smb_t2_request(t2p);
575681a5bbeSBoris Popov 	smb_t2_done(t2p);
576681a5bbeSBoris Popov 	return error;
577681a5bbeSBoris Popov }
578681a5bbeSBoris Popov 
579681a5bbeSBoris Popov /*
580681a5bbeSBoris Popov  * NT level. Specially for win9x
581681a5bbeSBoris Popov  */
582681a5bbeSBoris Popov int
583681a5bbeSBoris Popov smbfs_smb_setpattrNT(struct smbnode *np, u_short attr, struct timespec *mtime,
584681a5bbeSBoris Popov 	struct timespec *atime, struct smb_cred *scred)
585681a5bbeSBoris Popov {
586681a5bbeSBoris Popov 	struct smb_t2rq *t2p;
587681a5bbeSBoris Popov 	struct smb_share *ssp = np->n_mount->sm_share;
588681a5bbeSBoris Popov 	struct smb_vc *vcp = SSTOVC(ssp);
589681a5bbeSBoris Popov 	struct mbchain *mbp;
590681a5bbeSBoris Popov 	int64_t tm;
591681a5bbeSBoris Popov 	int error, tzoff;
592681a5bbeSBoris Popov 
593681a5bbeSBoris Popov 	error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_SET_PATH_INFORMATION,
594681a5bbeSBoris Popov 	    scred, &t2p);
595681a5bbeSBoris Popov 	if (error)
596681a5bbeSBoris Popov 		return error;
597681a5bbeSBoris Popov 	mbp = &t2p->t2_tparam;
598681a5bbeSBoris Popov 	mb_init(mbp);
599681a5bbeSBoris Popov 	mb_put_uint16le(mbp, SMB_SET_FILE_BASIC_INFO);
600681a5bbeSBoris Popov 	mb_put_uint32le(mbp, 0);		/* MBZ */
6013c2f5c3cSBoris Popov 	/* mb_put_uint8(mbp, SMB_DT_ASCII); specs incorrect */
602681a5bbeSBoris Popov 	error = smbfs_fullpath(mbp, vcp, np, NULL, 0);
603681a5bbeSBoris Popov 	if (error) {
604681a5bbeSBoris Popov 		smb_t2_done(t2p);
605681a5bbeSBoris Popov 		return error;
606681a5bbeSBoris Popov 	}
607681a5bbeSBoris Popov 	tzoff = vcp->vc_sopt.sv_tz;
608681a5bbeSBoris Popov 	mbp = &t2p->t2_tdata;
609681a5bbeSBoris Popov 	mb_init(mbp);
610681a5bbeSBoris Popov 	mb_put_int64le(mbp, 0);		/* creation time */
611681a5bbeSBoris Popov 	if (atime) {
612681a5bbeSBoris Popov 		smb_time_local2NT(atime, tzoff, &tm);
613681a5bbeSBoris Popov 	} else
614681a5bbeSBoris Popov 		tm = 0;
615681a5bbeSBoris Popov 	mb_put_int64le(mbp, tm);
616681a5bbeSBoris Popov 	if (mtime) {
617681a5bbeSBoris Popov 		smb_time_local2NT(mtime, tzoff, &tm);
618681a5bbeSBoris Popov 	} else
619681a5bbeSBoris Popov 		tm = 0;
620681a5bbeSBoris Popov 	mb_put_int64le(mbp, tm);
621681a5bbeSBoris Popov 	mb_put_int64le(mbp, tm);		/* change time */
622681a5bbeSBoris Popov 	mb_put_uint32le(mbp, attr);		/* attr */
623681a5bbeSBoris Popov 	t2p->t2_maxpcount = 24;
624681a5bbeSBoris Popov 	t2p->t2_maxdcount = 56;
625681a5bbeSBoris Popov 	error = smb_t2_request(t2p);
626681a5bbeSBoris Popov 	smb_t2_done(t2p);
627681a5bbeSBoris Popov 	return error;
628681a5bbeSBoris Popov }
629681a5bbeSBoris Popov 
630681a5bbeSBoris Popov /*
631681a5bbeSBoris Popov  * Set file atime and mtime. Doesn't supported by core dialect.
632681a5bbeSBoris Popov  */
633681a5bbeSBoris Popov int
634681a5bbeSBoris Popov smbfs_smb_setftime(struct smbnode *np, struct timespec *mtime,
635681a5bbeSBoris Popov 	struct timespec *atime, struct smb_cred *scred)
636681a5bbeSBoris Popov {
637681a5bbeSBoris Popov 	struct smb_rq rq, *rqp = &rq;
638681a5bbeSBoris Popov 	struct smb_share *ssp = np->n_mount->sm_share;
639681a5bbeSBoris Popov 	struct mbchain *mbp;
640681a5bbeSBoris Popov 	u_int16_t date, time;
641681a5bbeSBoris Popov 	int error, tzoff;
642681a5bbeSBoris Popov 
643681a5bbeSBoris Popov 	error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_SET_INFORMATION2, scred);
644681a5bbeSBoris Popov 	if (error)
645681a5bbeSBoris Popov 		return error;
646681a5bbeSBoris Popov 	tzoff = SSTOVC(ssp)->vc_sopt.sv_tz;
647681a5bbeSBoris Popov 	smb_rq_getrequest(rqp, &mbp);
648681a5bbeSBoris Popov 	smb_rq_wstart(rqp);
649681a5bbeSBoris Popov 	mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
650681a5bbeSBoris Popov 	mb_put_uint32le(mbp, 0);		/* creation time */
651681a5bbeSBoris Popov 
652681a5bbeSBoris Popov 	if (atime)
653681a5bbeSBoris Popov 		smb_time_unix2dos(atime, tzoff, &date, &time, NULL);
654681a5bbeSBoris Popov 	else
655681a5bbeSBoris Popov 		time = date = 0;
656681a5bbeSBoris Popov 	mb_put_uint16le(mbp, date);
657681a5bbeSBoris Popov 	mb_put_uint16le(mbp, time);
658681a5bbeSBoris Popov 	if (mtime)
659681a5bbeSBoris Popov 		smb_time_unix2dos(mtime, tzoff, &date, &time, NULL);
660681a5bbeSBoris Popov 	else
661681a5bbeSBoris Popov 		time = date = 0;
662681a5bbeSBoris Popov 	mb_put_uint16le(mbp, date);
663681a5bbeSBoris Popov 	mb_put_uint16le(mbp, time);
664681a5bbeSBoris Popov 	smb_rq_wend(rqp);
665681a5bbeSBoris Popov 	smb_rq_bstart(rqp);
666681a5bbeSBoris Popov 	smb_rq_bend(rqp);
667681a5bbeSBoris Popov 	error = smb_rq_simple(rqp);
668681a5bbeSBoris Popov 	SMBSDEBUG("%d\n", error);
669681a5bbeSBoris Popov 	smb_rq_done(rqp);
670681a5bbeSBoris Popov 	return error;
671681a5bbeSBoris Popov }
672681a5bbeSBoris Popov 
673681a5bbeSBoris Popov /*
674681a5bbeSBoris Popov  * Set DOS file attributes.
675681a5bbeSBoris Popov  * Looks like this call can be used only if CAP_NT_SMBS bit is on.
676681a5bbeSBoris Popov  */
677681a5bbeSBoris Popov int
678681a5bbeSBoris Popov smbfs_smb_setfattrNT(struct smbnode *np, u_int16_t attr, struct timespec *mtime,
679681a5bbeSBoris Popov 	struct timespec *atime, struct smb_cred *scred)
680681a5bbeSBoris Popov {
681681a5bbeSBoris Popov 	struct smb_t2rq *t2p;
682681a5bbeSBoris Popov 	struct smb_share *ssp = np->n_mount->sm_share;
683681a5bbeSBoris Popov 	struct mbchain *mbp;
684681a5bbeSBoris Popov 	int64_t tm;
685681a5bbeSBoris Popov 	int error, svtz;
686681a5bbeSBoris Popov 
687681a5bbeSBoris Popov 	error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_SET_FILE_INFORMATION,
688681a5bbeSBoris Popov 	    scred, &t2p);
689681a5bbeSBoris Popov 	if (error)
690681a5bbeSBoris Popov 		return error;
691681a5bbeSBoris Popov 	svtz = SSTOVC(ssp)->vc_sopt.sv_tz;
692681a5bbeSBoris Popov 	mbp = &t2p->t2_tparam;
693681a5bbeSBoris Popov 	mb_init(mbp);
694681a5bbeSBoris Popov 	mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
695681a5bbeSBoris Popov 	mb_put_uint16le(mbp, SMB_SET_FILE_BASIC_INFO);
696681a5bbeSBoris Popov 	mb_put_uint32le(mbp, 0);
697681a5bbeSBoris Popov 	mbp = &t2p->t2_tdata;
698681a5bbeSBoris Popov 	mb_init(mbp);
699681a5bbeSBoris Popov 	mb_put_int64le(mbp, 0);		/* creation time */
700681a5bbeSBoris Popov 	if (atime) {
701681a5bbeSBoris Popov 		smb_time_local2NT(atime, svtz, &tm);
702681a5bbeSBoris Popov 	} else
703681a5bbeSBoris Popov 		tm = 0;
704681a5bbeSBoris Popov 	mb_put_int64le(mbp, tm);
705681a5bbeSBoris Popov 	if (mtime) {
706681a5bbeSBoris Popov 		smb_time_local2NT(mtime, svtz, &tm);
707681a5bbeSBoris Popov 	} else
708681a5bbeSBoris Popov 		tm = 0;
709681a5bbeSBoris Popov 	mb_put_int64le(mbp, tm);
710681a5bbeSBoris Popov 	mb_put_int64le(mbp, tm);		/* change time */
711681a5bbeSBoris Popov 	mb_put_uint16le(mbp, attr);
712681a5bbeSBoris Popov 	mb_put_uint32le(mbp, 0);			/* padding */
713681a5bbeSBoris Popov 	mb_put_uint16le(mbp, 0);
714681a5bbeSBoris Popov 	t2p->t2_maxpcount = 2;
715681a5bbeSBoris Popov 	t2p->t2_maxdcount = 0;
716681a5bbeSBoris Popov 	error = smb_t2_request(t2p);
717681a5bbeSBoris Popov 	smb_t2_done(t2p);
718681a5bbeSBoris Popov 	return error;
719681a5bbeSBoris Popov }
720681a5bbeSBoris Popov 
721681a5bbeSBoris Popov 
722681a5bbeSBoris Popov int
723681a5bbeSBoris Popov smbfs_smb_open(struct smbnode *np, int accmode, struct smb_cred *scred)
724681a5bbeSBoris Popov {
725681a5bbeSBoris Popov 	struct smb_rq rq, *rqp = &rq;
726681a5bbeSBoris Popov 	struct smb_share *ssp = np->n_mount->sm_share;
727681a5bbeSBoris Popov 	struct mbchain *mbp;
728681a5bbeSBoris Popov 	struct mdchain *mdp;
729681a5bbeSBoris Popov 	u_int8_t wc;
730681a5bbeSBoris Popov 	u_int16_t fid, wattr, grantedmode;
731681a5bbeSBoris Popov 	int error;
732681a5bbeSBoris Popov 
733681a5bbeSBoris Popov 	error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_OPEN, scred);
734681a5bbeSBoris Popov 	if (error)
735681a5bbeSBoris Popov 		return error;
736681a5bbeSBoris Popov 	smb_rq_getrequest(rqp, &mbp);
737681a5bbeSBoris Popov 	smb_rq_wstart(rqp);
738681a5bbeSBoris Popov 	mb_put_uint16le(mbp, accmode);
739681a5bbeSBoris Popov 	mb_put_uint16le(mbp, SMB_FA_SYSTEM | SMB_FA_HIDDEN);
740681a5bbeSBoris Popov 	smb_rq_wend(rqp);
741681a5bbeSBoris Popov 	smb_rq_bstart(rqp);
742681a5bbeSBoris Popov 	mb_put_uint8(mbp, SMB_DT_ASCII);
743681a5bbeSBoris Popov 	do {
744681a5bbeSBoris Popov 		error = smbfs_fullpath(mbp, SSTOVC(ssp), np, NULL, 0);
745681a5bbeSBoris Popov 		if (error)
746681a5bbeSBoris Popov 			break;
747681a5bbeSBoris Popov 		smb_rq_bend(rqp);
748681a5bbeSBoris Popov 		error = smb_rq_simple(rqp);
749681a5bbeSBoris Popov 		if (error)
750681a5bbeSBoris Popov 			break;
751681a5bbeSBoris Popov 		smb_rq_getreply(rqp, &mdp);
752681a5bbeSBoris Popov 		if (md_get_uint8(mdp, &wc) != 0 || wc != 7) {
753681a5bbeSBoris Popov 			error = EBADRPC;
754681a5bbeSBoris Popov 			break;
755681a5bbeSBoris Popov 		}
756681a5bbeSBoris Popov 		md_get_uint16(mdp, &fid);
757681a5bbeSBoris Popov 		md_get_uint16le(mdp, &wattr);
758681a5bbeSBoris Popov 		md_get_uint32(mdp, NULL);	/* mtime */
759681a5bbeSBoris Popov 		md_get_uint32(mdp, NULL);	/* fsize */
760681a5bbeSBoris Popov 		md_get_uint16le(mdp, &grantedmode);
761681a5bbeSBoris Popov 		/*
762681a5bbeSBoris Popov 		 * TODO: refresh attributes from this reply
763681a5bbeSBoris Popov 		 */
764681a5bbeSBoris Popov 	} while(0);
765681a5bbeSBoris Popov 	smb_rq_done(rqp);
766681a5bbeSBoris Popov 	if (error)
767681a5bbeSBoris Popov 		return error;
768681a5bbeSBoris Popov 	np->n_fid = fid;
769681a5bbeSBoris Popov 	np->n_rwstate = grantedmode;
770681a5bbeSBoris Popov 	return 0;
771681a5bbeSBoris Popov }
772681a5bbeSBoris Popov 
773681a5bbeSBoris Popov 
774681a5bbeSBoris Popov int
775681a5bbeSBoris Popov smbfs_smb_close(struct smb_share *ssp, u_int16_t fid, struct timespec *mtime,
776681a5bbeSBoris Popov 	struct smb_cred *scred)
777681a5bbeSBoris Popov {
778681a5bbeSBoris Popov 	struct smb_rq rq, *rqp = &rq;
779681a5bbeSBoris Popov 	struct mbchain *mbp;
780681a5bbeSBoris Popov 	u_long time;
781681a5bbeSBoris Popov 	int error;
782681a5bbeSBoris Popov 
783681a5bbeSBoris Popov 	error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_CLOSE, scred);
784681a5bbeSBoris Popov 	if (error)
785681a5bbeSBoris Popov 		return error;
786681a5bbeSBoris Popov 	smb_rq_getrequest(rqp, &mbp);
787681a5bbeSBoris Popov 	smb_rq_wstart(rqp);
788681a5bbeSBoris Popov 	mb_put_mem(mbp, (caddr_t)&fid, sizeof(fid), MB_MSYSTEM);
789681a5bbeSBoris Popov 	if (mtime) {
790681a5bbeSBoris Popov 		smb_time_local2server(mtime, SSTOVC(ssp)->vc_sopt.sv_tz, &time);
791681a5bbeSBoris Popov 	} else
792681a5bbeSBoris Popov 		time = 0;
793681a5bbeSBoris Popov 	mb_put_uint32le(mbp, time);
794681a5bbeSBoris Popov 	smb_rq_wend(rqp);
795681a5bbeSBoris Popov 	smb_rq_bstart(rqp);
796681a5bbeSBoris Popov 	smb_rq_bend(rqp);
797681a5bbeSBoris Popov 	error = smb_rq_simple(rqp);
798681a5bbeSBoris Popov 	smb_rq_done(rqp);
799681a5bbeSBoris Popov 	return error;
800681a5bbeSBoris Popov }
801681a5bbeSBoris Popov 
802681a5bbeSBoris Popov int
803681a5bbeSBoris Popov smbfs_smb_create(struct smbnode *dnp, const char *name, int nmlen,
804681a5bbeSBoris Popov 	struct smb_cred *scred)
805681a5bbeSBoris Popov {
806681a5bbeSBoris Popov 	struct smb_rq rq, *rqp = &rq;
807681a5bbeSBoris Popov 	struct smb_share *ssp = dnp->n_mount->sm_share;
808681a5bbeSBoris Popov 	struct mbchain *mbp;
809681a5bbeSBoris Popov 	struct mdchain *mdp;
810681a5bbeSBoris Popov 	struct timespec ctime;
811681a5bbeSBoris Popov 	u_int8_t wc;
812681a5bbeSBoris Popov 	u_int16_t fid;
813681a5bbeSBoris Popov 	u_long tm;
814681a5bbeSBoris Popov 	int error;
815681a5bbeSBoris Popov 
816681a5bbeSBoris Popov 	error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_CREATE, scred);
817681a5bbeSBoris Popov 	if (error)
818681a5bbeSBoris Popov 		return error;
819681a5bbeSBoris Popov 	smb_rq_getrequest(rqp, &mbp);
820681a5bbeSBoris Popov 	smb_rq_wstart(rqp);
821681a5bbeSBoris Popov 	mb_put_uint16le(mbp, SMB_FA_ARCHIVE);		/* attributes  */
822681a5bbeSBoris Popov 	nanotime(&ctime);
823681a5bbeSBoris Popov 	smb_time_local2server(&ctime, SSTOVC(ssp)->vc_sopt.sv_tz, &tm);
824681a5bbeSBoris Popov 	mb_put_uint32le(mbp, tm);
825681a5bbeSBoris Popov 	smb_rq_wend(rqp);
826681a5bbeSBoris Popov 	smb_rq_bstart(rqp);
827681a5bbeSBoris Popov 	mb_put_uint8(mbp, SMB_DT_ASCII);
828681a5bbeSBoris Popov 	error = smbfs_fullpath(mbp, SSTOVC(ssp), dnp, name, nmlen);
829681a5bbeSBoris Popov 	if (!error) {
830681a5bbeSBoris Popov 		smb_rq_bend(rqp);
831681a5bbeSBoris Popov 		error = smb_rq_simple(rqp);
832681a5bbeSBoris Popov 		if (!error) {
833681a5bbeSBoris Popov 			smb_rq_getreply(rqp, &mdp);
834681a5bbeSBoris Popov 			md_get_uint8(mdp, &wc);
835681a5bbeSBoris Popov 			if (wc == 1)
836681a5bbeSBoris Popov 				md_get_uint16(mdp, &fid);
837681a5bbeSBoris Popov 			else
838681a5bbeSBoris Popov 				error = EBADRPC;
839681a5bbeSBoris Popov 		}
840681a5bbeSBoris Popov 	}
841681a5bbeSBoris Popov 	smb_rq_done(rqp);
842681a5bbeSBoris Popov 	if (error)
843681a5bbeSBoris Popov 		return error;
844681a5bbeSBoris Popov 	smbfs_smb_close(ssp, fid, &ctime, scred);
845681a5bbeSBoris Popov 	return error;
846681a5bbeSBoris Popov }
847681a5bbeSBoris Popov 
848681a5bbeSBoris Popov int
849681a5bbeSBoris Popov smbfs_smb_delete(struct smbnode *np, struct smb_cred *scred)
850681a5bbeSBoris Popov {
851681a5bbeSBoris Popov 	struct smb_rq rq, *rqp = &rq;
852681a5bbeSBoris Popov 	struct smb_share *ssp = np->n_mount->sm_share;
853681a5bbeSBoris Popov 	struct mbchain *mbp;
854681a5bbeSBoris Popov 	int error;
855681a5bbeSBoris Popov 
856681a5bbeSBoris Popov 	error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_DELETE, scred);
857681a5bbeSBoris Popov 	if (error)
858681a5bbeSBoris Popov 		return error;
859681a5bbeSBoris Popov 	smb_rq_getrequest(rqp, &mbp);
860681a5bbeSBoris Popov 	smb_rq_wstart(rqp);
861681a5bbeSBoris Popov 	mb_put_uint16le(mbp, SMB_FA_SYSTEM | SMB_FA_HIDDEN);
862681a5bbeSBoris Popov 	smb_rq_wend(rqp);
863681a5bbeSBoris Popov 	smb_rq_bstart(rqp);
864681a5bbeSBoris Popov 	mb_put_uint8(mbp, SMB_DT_ASCII);
865681a5bbeSBoris Popov 	error = smbfs_fullpath(mbp, SSTOVC(ssp), np, NULL, 0);
866681a5bbeSBoris Popov 	if (!error) {
867681a5bbeSBoris Popov 		smb_rq_bend(rqp);
868681a5bbeSBoris Popov 		error = smb_rq_simple(rqp);
869681a5bbeSBoris Popov 	}
870681a5bbeSBoris Popov 	smb_rq_done(rqp);
871681a5bbeSBoris Popov 	return error;
872681a5bbeSBoris Popov }
873681a5bbeSBoris Popov 
874681a5bbeSBoris Popov int
875681a5bbeSBoris Popov smbfs_smb_rename(struct smbnode *src, struct smbnode *tdnp,
876681a5bbeSBoris Popov 	const char *tname, int tnmlen, struct smb_cred *scred)
877681a5bbeSBoris Popov {
878681a5bbeSBoris Popov 	struct smb_rq rq, *rqp = &rq;
879681a5bbeSBoris Popov 	struct smb_share *ssp = src->n_mount->sm_share;
880681a5bbeSBoris Popov 	struct mbchain *mbp;
881681a5bbeSBoris Popov 	int error;
882681a5bbeSBoris Popov 
883681a5bbeSBoris Popov 	error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_RENAME, scred);
884681a5bbeSBoris Popov 	if (error)
885681a5bbeSBoris Popov 		return error;
886681a5bbeSBoris Popov 	smb_rq_getrequest(rqp, &mbp);
887681a5bbeSBoris Popov 	smb_rq_wstart(rqp);
888681a5bbeSBoris Popov 	mb_put_uint16le(mbp, SMB_FA_SYSTEM | SMB_FA_HIDDEN);
889681a5bbeSBoris Popov 	smb_rq_wend(rqp);
890681a5bbeSBoris Popov 	smb_rq_bstart(rqp);
891681a5bbeSBoris Popov 	mb_put_uint8(mbp, SMB_DT_ASCII);
892681a5bbeSBoris Popov 	do {
893681a5bbeSBoris Popov 		error = smbfs_fullpath(mbp, SSTOVC(ssp), src, NULL, 0);
894681a5bbeSBoris Popov 		if (error)
895681a5bbeSBoris Popov 			break;
896681a5bbeSBoris Popov 		mb_put_uint8(mbp, SMB_DT_ASCII);
897681a5bbeSBoris Popov 		error = smbfs_fullpath(mbp, SSTOVC(ssp), tdnp, tname, tnmlen);
898681a5bbeSBoris Popov 		if (error)
899681a5bbeSBoris Popov 			break;
900681a5bbeSBoris Popov 		smb_rq_bend(rqp);
901681a5bbeSBoris Popov 		error = smb_rq_simple(rqp);
902681a5bbeSBoris Popov 	} while(0);
903681a5bbeSBoris Popov 	smb_rq_done(rqp);
904681a5bbeSBoris Popov 	return error;
905681a5bbeSBoris Popov }
906681a5bbeSBoris Popov 
907681a5bbeSBoris Popov int
908681a5bbeSBoris Popov smbfs_smb_move(struct smbnode *src, struct smbnode *tdnp,
909681a5bbeSBoris Popov 	const char *tname, int tnmlen, u_int16_t flags, struct smb_cred *scred)
910681a5bbeSBoris Popov {
911681a5bbeSBoris Popov 	struct smb_rq rq, *rqp = &rq;
912681a5bbeSBoris Popov 	struct smb_share *ssp = src->n_mount->sm_share;
913681a5bbeSBoris Popov 	struct mbchain *mbp;
914681a5bbeSBoris Popov 	int error;
915681a5bbeSBoris Popov 
916681a5bbeSBoris Popov 	error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_MOVE, scred);
917681a5bbeSBoris Popov 	if (error)
918681a5bbeSBoris Popov 		return error;
919681a5bbeSBoris Popov 	smb_rq_getrequest(rqp, &mbp);
920681a5bbeSBoris Popov 	smb_rq_wstart(rqp);
921681a5bbeSBoris Popov 	mb_put_uint16le(mbp, SMB_TID_UNKNOWN);
922681a5bbeSBoris Popov 	mb_put_uint16le(mbp, 0x20);	/* delete target file */
923681a5bbeSBoris Popov 	mb_put_uint16le(mbp, flags);
924681a5bbeSBoris Popov 	smb_rq_wend(rqp);
925681a5bbeSBoris Popov 	smb_rq_bstart(rqp);
926681a5bbeSBoris Popov 	mb_put_uint8(mbp, SMB_DT_ASCII);
927681a5bbeSBoris Popov 	do {
928681a5bbeSBoris Popov 		error = smbfs_fullpath(mbp, SSTOVC(ssp), src, NULL, 0);
929681a5bbeSBoris Popov 		if (error)
930681a5bbeSBoris Popov 			break;
931681a5bbeSBoris Popov 		mb_put_uint8(mbp, SMB_DT_ASCII);
932681a5bbeSBoris Popov 		error = smbfs_fullpath(mbp, SSTOVC(ssp), tdnp, tname, tnmlen);
933681a5bbeSBoris Popov 		if (error)
934681a5bbeSBoris Popov 			break;
935681a5bbeSBoris Popov 		smb_rq_bend(rqp);
936681a5bbeSBoris Popov 		error = smb_rq_simple(rqp);
937681a5bbeSBoris Popov 	} while(0);
938681a5bbeSBoris Popov 	smb_rq_done(rqp);
939681a5bbeSBoris Popov 	return error;
940681a5bbeSBoris Popov }
941681a5bbeSBoris Popov 
942681a5bbeSBoris Popov int
943681a5bbeSBoris Popov smbfs_smb_mkdir(struct smbnode *dnp, const char *name, int len,
944681a5bbeSBoris Popov 	struct smb_cred *scred)
945681a5bbeSBoris Popov {
946681a5bbeSBoris Popov 	struct smb_rq rq, *rqp = &rq;
947681a5bbeSBoris Popov 	struct smb_share *ssp = dnp->n_mount->sm_share;
948681a5bbeSBoris Popov 	struct mbchain *mbp;
949681a5bbeSBoris Popov 	int error;
950681a5bbeSBoris Popov 
951681a5bbeSBoris Popov 	error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_CREATE_DIRECTORY, scred);
952681a5bbeSBoris Popov 	if (error)
953681a5bbeSBoris Popov 		return error;
954681a5bbeSBoris Popov 	smb_rq_getrequest(rqp, &mbp);
955681a5bbeSBoris Popov 	smb_rq_wstart(rqp);
956681a5bbeSBoris Popov 	smb_rq_wend(rqp);
957681a5bbeSBoris Popov 	smb_rq_bstart(rqp);
958681a5bbeSBoris Popov 	mb_put_uint8(mbp, SMB_DT_ASCII);
959681a5bbeSBoris Popov 	error = smbfs_fullpath(mbp, SSTOVC(ssp), dnp, name, len);
960681a5bbeSBoris Popov 	if (!error) {
961681a5bbeSBoris Popov 		smb_rq_bend(rqp);
962681a5bbeSBoris Popov 		error = smb_rq_simple(rqp);
963681a5bbeSBoris Popov 	}
964681a5bbeSBoris Popov 	smb_rq_done(rqp);
965681a5bbeSBoris Popov 	return error;
966681a5bbeSBoris Popov }
967681a5bbeSBoris Popov 
968681a5bbeSBoris Popov int
969681a5bbeSBoris Popov smbfs_smb_rmdir(struct smbnode *np, struct smb_cred *scred)
970681a5bbeSBoris Popov {
971681a5bbeSBoris Popov 	struct smb_rq rq, *rqp = &rq;
972681a5bbeSBoris Popov 	struct smb_share *ssp = np->n_mount->sm_share;
973681a5bbeSBoris Popov 	struct mbchain *mbp;
974681a5bbeSBoris Popov 	int error;
975681a5bbeSBoris Popov 
976681a5bbeSBoris Popov 	error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_DELETE_DIRECTORY, scred);
977681a5bbeSBoris Popov 	if (error)
978681a5bbeSBoris Popov 		return error;
979681a5bbeSBoris Popov 	smb_rq_getrequest(rqp, &mbp);
980681a5bbeSBoris Popov 	smb_rq_wstart(rqp);
981681a5bbeSBoris Popov 	smb_rq_wend(rqp);
982681a5bbeSBoris Popov 	smb_rq_bstart(rqp);
983681a5bbeSBoris Popov 	mb_put_uint8(mbp, SMB_DT_ASCII);
984681a5bbeSBoris Popov 	error = smbfs_fullpath(mbp, SSTOVC(ssp), np, NULL, 0);
985681a5bbeSBoris Popov 	if (!error) {
986681a5bbeSBoris Popov 		smb_rq_bend(rqp);
987681a5bbeSBoris Popov 		error = smb_rq_simple(rqp);
988681a5bbeSBoris Popov 	}
989681a5bbeSBoris Popov 	smb_rq_done(rqp);
990681a5bbeSBoris Popov 	return error;
991681a5bbeSBoris Popov }
992681a5bbeSBoris Popov 
993681a5bbeSBoris Popov static int
994681a5bbeSBoris Popov smbfs_smb_search(struct smbfs_fctx *ctx)
995681a5bbeSBoris Popov {
996681a5bbeSBoris Popov 	struct smb_vc *vcp = SSTOVC(ctx->f_ssp);
997681a5bbeSBoris Popov 	struct smb_rq *rqp;
998681a5bbeSBoris Popov 	struct mbchain *mbp;
999681a5bbeSBoris Popov 	struct mdchain *mdp;
1000681a5bbeSBoris Popov 	u_int8_t wc, bt;
1001681a5bbeSBoris Popov 	u_int16_t ec, dlen, bc;
1002681a5bbeSBoris Popov 	int maxent, error, iseof = 0;
1003681a5bbeSBoris Popov 
1004681a5bbeSBoris Popov 	maxent = min(ctx->f_left, (vcp->vc_txmax - SMB_HDRLEN - 3) / SMB_DENTRYLEN);
1005681a5bbeSBoris Popov 	if (ctx->f_rq) {
1006681a5bbeSBoris Popov 		smb_rq_done(ctx->f_rq);
1007681a5bbeSBoris Popov 		ctx->f_rq = NULL;
1008681a5bbeSBoris Popov 	}
1009681a5bbeSBoris Popov 	error = smb_rq_alloc(SSTOCP(ctx->f_ssp), SMB_COM_SEARCH, ctx->f_scred, &rqp);
1010681a5bbeSBoris Popov 	if (error)
1011681a5bbeSBoris Popov 		return error;
1012681a5bbeSBoris Popov 	ctx->f_rq = rqp;
1013681a5bbeSBoris Popov 	smb_rq_getrequest(rqp, &mbp);
1014681a5bbeSBoris Popov 	smb_rq_wstart(rqp);
1015681a5bbeSBoris Popov 	mb_put_uint16le(mbp, maxent);	/* max entries to return */
1016681a5bbeSBoris Popov 	mb_put_uint16le(mbp, ctx->f_attrmask);
1017681a5bbeSBoris Popov 	smb_rq_wend(rqp);
1018681a5bbeSBoris Popov 	smb_rq_bstart(rqp);
1019681a5bbeSBoris Popov 	mb_put_uint8(mbp, SMB_DT_ASCII);	/* buffer format */
1020681a5bbeSBoris Popov 	if (ctx->f_flags & SMBFS_RDD_FINDFIRST) {
1021681a5bbeSBoris Popov 		error = smbfs_fullpath(mbp, vcp, ctx->f_dnp, ctx->f_wildcard, ctx->f_wclen);
1022681a5bbeSBoris Popov 		if (error)
1023681a5bbeSBoris Popov 			return error;
1024681a5bbeSBoris Popov 		mb_put_uint8(mbp, SMB_DT_VARIABLE);
1025681a5bbeSBoris Popov 		mb_put_uint16le(mbp, 0);	/* context length */
1026681a5bbeSBoris Popov 		ctx->f_flags &= ~SMBFS_RDD_FINDFIRST;
1027681a5bbeSBoris Popov 	} else {
1028681a5bbeSBoris Popov 		mb_put_uint8(mbp, 0);	/* file name length */
1029681a5bbeSBoris Popov 		mb_put_uint8(mbp, SMB_DT_VARIABLE);
1030681a5bbeSBoris Popov 		mb_put_uint16le(mbp, SMB_SKEYLEN);
1031681a5bbeSBoris Popov 		mb_put_mem(mbp, ctx->f_skey, SMB_SKEYLEN, MB_MSYSTEM);
1032681a5bbeSBoris Popov 	}
1033681a5bbeSBoris Popov 	smb_rq_bend(rqp);
1034681a5bbeSBoris Popov 	error = smb_rq_simple(rqp);
1035681a5bbeSBoris Popov 	if (error) {
1036681a5bbeSBoris Popov 		if (rqp->sr_errclass == ERRDOS && rqp->sr_serror == ERRnofiles) {
1037681a5bbeSBoris Popov 			error = 0;
1038681a5bbeSBoris Popov 			iseof = 1;
1039681a5bbeSBoris Popov 			ctx->f_flags |= SMBFS_RDD_EOF;
1040681a5bbeSBoris Popov 		} else
1041681a5bbeSBoris Popov 			return error;
1042681a5bbeSBoris Popov 	}
1043681a5bbeSBoris Popov 	smb_rq_getreply(rqp, &mdp);
1044681a5bbeSBoris Popov 	md_get_uint8(mdp, &wc);
1045681a5bbeSBoris Popov 	if (wc != 1)
1046681a5bbeSBoris Popov 		return iseof ? ENOENT : EBADRPC;
1047681a5bbeSBoris Popov 	md_get_uint16le(mdp, &ec);
1048681a5bbeSBoris Popov 	if (ec == 0)
1049681a5bbeSBoris Popov 		return ENOENT;
1050681a5bbeSBoris Popov 	ctx->f_ecnt = ec;
1051681a5bbeSBoris Popov 	md_get_uint16le(mdp, &bc);
1052681a5bbeSBoris Popov 	if (bc < 3)
1053681a5bbeSBoris Popov 		return EBADRPC;
1054681a5bbeSBoris Popov 	bc -= 3;
1055681a5bbeSBoris Popov 	md_get_uint8(mdp, &bt);
1056681a5bbeSBoris Popov 	if (bt != SMB_DT_VARIABLE)
1057681a5bbeSBoris Popov 		return EBADRPC;
1058681a5bbeSBoris Popov 	md_get_uint16le(mdp, &dlen);
1059681a5bbeSBoris Popov 	if (dlen != bc || dlen % SMB_DENTRYLEN != 0)
1060681a5bbeSBoris Popov 		return EBADRPC;
1061681a5bbeSBoris Popov 	return 0;
1062681a5bbeSBoris Popov }
1063681a5bbeSBoris Popov 
1064681a5bbeSBoris Popov static int
1065681a5bbeSBoris Popov smbfs_findopenLM1(struct smbfs_fctx *ctx, struct smbnode *dnp,
1066681a5bbeSBoris Popov 	const char *wildcard, int wclen, int attr, struct smb_cred *scred)
1067681a5bbeSBoris Popov {
1068681a5bbeSBoris Popov 	ctx->f_attrmask = attr;
1069681a5bbeSBoris Popov 	if (wildcard) {
1070681a5bbeSBoris Popov 		if (wclen == 1 && wildcard[0] == '*') {
1071681a5bbeSBoris Popov 			ctx->f_wildcard = "*.*";
1072681a5bbeSBoris Popov 			ctx->f_wclen = 3;
1073681a5bbeSBoris Popov 		} else {
1074681a5bbeSBoris Popov 			ctx->f_wildcard = wildcard;
1075681a5bbeSBoris Popov 			ctx->f_wclen = wclen;
1076681a5bbeSBoris Popov 		}
1077681a5bbeSBoris Popov 	} else {
1078681a5bbeSBoris Popov 		ctx->f_wildcard = NULL;
1079681a5bbeSBoris Popov 		ctx->f_wclen = 0;
1080681a5bbeSBoris Popov 	}
1081681a5bbeSBoris Popov 	ctx->f_name = ctx->f_fname;
1082681a5bbeSBoris Popov 	return 0;
1083681a5bbeSBoris Popov }
1084681a5bbeSBoris Popov 
1085681a5bbeSBoris Popov static int
1086681a5bbeSBoris Popov smbfs_findnextLM1(struct smbfs_fctx *ctx, int limit)
1087681a5bbeSBoris Popov {
1088681a5bbeSBoris Popov 	struct mdchain *mbp;
1089681a5bbeSBoris Popov 	struct smb_rq *rqp;
1090681a5bbeSBoris Popov 	char *cp;
1091681a5bbeSBoris Popov 	u_int8_t battr;
1092681a5bbeSBoris Popov 	u_int16_t date, time;
1093681a5bbeSBoris Popov 	u_int32_t size;
1094681a5bbeSBoris Popov 	int error;
1095681a5bbeSBoris Popov 
1096681a5bbeSBoris Popov 	if (ctx->f_ecnt == 0) {
1097681a5bbeSBoris Popov 		if (ctx->f_flags & SMBFS_RDD_EOF)
1098681a5bbeSBoris Popov 			return ENOENT;
1099681a5bbeSBoris Popov 		ctx->f_left = ctx->f_limit = limit;
1100681a5bbeSBoris Popov 		error = smbfs_smb_search(ctx);
1101681a5bbeSBoris Popov 		if (error)
1102681a5bbeSBoris Popov 			return error;
1103681a5bbeSBoris Popov 	}
1104681a5bbeSBoris Popov 	rqp = ctx->f_rq;
1105681a5bbeSBoris Popov 	smb_rq_getreply(rqp, &mbp);
1106681a5bbeSBoris Popov 	md_get_mem(mbp, ctx->f_skey, SMB_SKEYLEN, MB_MSYSTEM);
1107681a5bbeSBoris Popov 	md_get_uint8(mbp, &battr);
1108681a5bbeSBoris Popov 	md_get_uint16le(mbp, &time);
1109681a5bbeSBoris Popov 	md_get_uint16le(mbp, &date);
1110681a5bbeSBoris Popov 	md_get_uint32le(mbp, &size);
1111681a5bbeSBoris Popov 	cp = ctx->f_name;
1112681a5bbeSBoris Popov 	md_get_mem(mbp, cp, sizeof(ctx->f_fname), MB_MSYSTEM);
1113681a5bbeSBoris Popov 	cp[sizeof(ctx->f_fname) - 1] = 0;
1114681a5bbeSBoris Popov 	cp += strlen(cp) - 1;
1115681a5bbeSBoris Popov 	while (*cp == ' ' && cp >= ctx->f_name)
1116681a5bbeSBoris Popov 		*cp-- = 0;
1117681a5bbeSBoris Popov 	ctx->f_attr.fa_attr = battr;
1118681a5bbeSBoris Popov 	smb_dos2unixtime(date, time, 0, rqp->sr_vc->vc_sopt.sv_tz,
1119681a5bbeSBoris Popov 	    &ctx->f_attr.fa_mtime);
1120681a5bbeSBoris Popov 	ctx->f_attr.fa_size = size;
1121681a5bbeSBoris Popov 	ctx->f_nmlen = strlen(ctx->f_name);
1122681a5bbeSBoris Popov 	ctx->f_ecnt--;
1123681a5bbeSBoris Popov 	ctx->f_left--;
1124681a5bbeSBoris Popov 	return 0;
1125681a5bbeSBoris Popov }
1126681a5bbeSBoris Popov 
1127681a5bbeSBoris Popov static int
1128681a5bbeSBoris Popov smbfs_findcloseLM1(struct smbfs_fctx *ctx)
1129681a5bbeSBoris Popov {
1130681a5bbeSBoris Popov 	if (ctx->f_rq)
1131681a5bbeSBoris Popov 		smb_rq_done(ctx->f_rq);
1132681a5bbeSBoris Popov 	return 0;
1133681a5bbeSBoris Popov }
1134681a5bbeSBoris Popov 
1135681a5bbeSBoris Popov /*
1136681a5bbeSBoris Popov  * TRANS2_FIND_FIRST2/NEXT2, used for NT LM12 dialect
1137681a5bbeSBoris Popov  */
1138681a5bbeSBoris Popov static int
1139681a5bbeSBoris Popov smbfs_smb_trans2find2(struct smbfs_fctx *ctx)
1140681a5bbeSBoris Popov {
1141681a5bbeSBoris Popov 	struct smb_t2rq *t2p;
1142681a5bbeSBoris Popov 	struct smb_vc *vcp = SSTOVC(ctx->f_ssp);
1143681a5bbeSBoris Popov 	struct mbchain *mbp;
1144681a5bbeSBoris Popov 	struct mdchain *mdp;
1145681a5bbeSBoris Popov 	u_int16_t tw, flags;
1146681a5bbeSBoris Popov 	int error;
1147681a5bbeSBoris Popov 
1148681a5bbeSBoris Popov 	if (ctx->f_t2) {
1149681a5bbeSBoris Popov 		smb_t2_done(ctx->f_t2);
1150681a5bbeSBoris Popov 		ctx->f_t2 = NULL;
1151681a5bbeSBoris Popov 	}
1152681a5bbeSBoris Popov 	ctx->f_flags &= ~SMBFS_RDD_GOTRNAME;
1153681a5bbeSBoris Popov 	flags = 8 | 2;			/* <resume> | <close if EOS> */
1154681a5bbeSBoris Popov 	if (ctx->f_flags & SMBFS_RDD_FINDSINGLE) {
1155681a5bbeSBoris Popov 		flags |= 1;		/* close search after this request */
1156681a5bbeSBoris Popov 		ctx->f_flags |= SMBFS_RDD_NOCLOSE;
1157681a5bbeSBoris Popov 	}
1158681a5bbeSBoris Popov 	if (ctx->f_flags & SMBFS_RDD_FINDFIRST) {
1159681a5bbeSBoris Popov 		error = smb_t2_alloc(SSTOCP(ctx->f_ssp), SMB_TRANS2_FIND_FIRST2,
1160681a5bbeSBoris Popov 		    ctx->f_scred, &t2p);
1161681a5bbeSBoris Popov 		if (error)
1162681a5bbeSBoris Popov 			return error;
1163681a5bbeSBoris Popov 		ctx->f_t2 = t2p;
1164681a5bbeSBoris Popov 		mbp = &t2p->t2_tparam;
1165681a5bbeSBoris Popov 		mb_init(mbp);
1166681a5bbeSBoris Popov 		mb_put_uint16le(mbp, ctx->f_attrmask);
1167681a5bbeSBoris Popov 		mb_put_uint16le(mbp, ctx->f_limit);
1168681a5bbeSBoris Popov 		mb_put_uint16le(mbp, flags);
1169681a5bbeSBoris Popov 		mb_put_uint16le(mbp, ctx->f_infolevel);
1170681a5bbeSBoris Popov 		mb_put_uint32le(mbp, 0);
1171681a5bbeSBoris Popov 		error = smbfs_fullpath(mbp, vcp, ctx->f_dnp, ctx->f_wildcard, ctx->f_wclen);
1172681a5bbeSBoris Popov 		if (error)
1173681a5bbeSBoris Popov 			return error;
1174681a5bbeSBoris Popov 	} else	{
1175681a5bbeSBoris Popov 		error = smb_t2_alloc(SSTOCP(ctx->f_ssp), SMB_TRANS2_FIND_NEXT2,
1176681a5bbeSBoris Popov 		    ctx->f_scred, &t2p);
1177681a5bbeSBoris Popov 		if (error)
1178681a5bbeSBoris Popov 			return error;
1179681a5bbeSBoris Popov 		ctx->f_t2 = t2p;
1180681a5bbeSBoris Popov 		mbp = &t2p->t2_tparam;
1181681a5bbeSBoris Popov 		mb_init(mbp);
1182681a5bbeSBoris Popov 		mb_put_mem(mbp, (caddr_t)&ctx->f_Sid, 2, MB_MSYSTEM);
1183681a5bbeSBoris Popov 		mb_put_uint16le(mbp, ctx->f_limit);
1184681a5bbeSBoris Popov 		mb_put_uint16le(mbp, ctx->f_infolevel);
1185681a5bbeSBoris Popov 		mb_put_uint32le(mbp, 0);		/* resume key */
1186681a5bbeSBoris Popov 		mb_put_uint16le(mbp, flags);
1187681a5bbeSBoris Popov 		if (ctx->f_rname)
1188681a5bbeSBoris Popov 			mb_put_mem(mbp, ctx->f_rname, strlen(ctx->f_rname) + 1, MB_MSYSTEM);
1189681a5bbeSBoris Popov 		else
1190681a5bbeSBoris Popov 			mb_put_uint8(mbp, 0);	/* resume file name */
1191681a5bbeSBoris Popov #if 0
1192681a5bbeSBoris Popov 	struct timeval tv;
1193681a5bbeSBoris Popov 	tv.tv_sec = 0;
1194681a5bbeSBoris Popov 	tv.tv_usec = 200 * 1000;	/* 200ms */
1195681a5bbeSBoris Popov 		if (vcp->vc_flags & SMBC_WIN95) {
1196681a5bbeSBoris Popov 			/*
1197681a5bbeSBoris Popov 			 * some implementations suggests to sleep here
1198681a5bbeSBoris Popov 			 * for 200ms, due to the bug in the Win95.
1199681a5bbeSBoris Popov 			 * I've didn't notice any problem, but put code
1200681a5bbeSBoris Popov 			 * for it.
1201681a5bbeSBoris Popov 			 */
1202681a5bbeSBoris Popov 			 tsleep(&flags, PVFS, "fix95", tvtohz(&tv));
1203681a5bbeSBoris Popov 		}
1204681a5bbeSBoris Popov #endif
1205681a5bbeSBoris Popov 	}
1206681a5bbeSBoris Popov 	t2p->t2_maxpcount = 5 * 2;
1207681a5bbeSBoris Popov 	t2p->t2_maxdcount = vcp->vc_txmax;
1208681a5bbeSBoris Popov 	error = smb_t2_request(t2p);
1209681a5bbeSBoris Popov 	if (error)
1210681a5bbeSBoris Popov 		return error;
1211681a5bbeSBoris Popov 	mdp = &t2p->t2_rparam;
1212681a5bbeSBoris Popov 	if (ctx->f_flags & SMBFS_RDD_FINDFIRST) {
1213681a5bbeSBoris Popov 		if ((error = md_get_uint16(mdp, &ctx->f_Sid)) != 0)
1214681a5bbeSBoris Popov 			return error;
1215681a5bbeSBoris Popov 		ctx->f_flags &= ~SMBFS_RDD_FINDFIRST;
1216681a5bbeSBoris Popov 	}
1217681a5bbeSBoris Popov 	if ((error = md_get_uint16le(mdp, &tw)) != 0)
1218681a5bbeSBoris Popov 		return error;
1219681a5bbeSBoris Popov 	ctx->f_ecnt = tw;
1220681a5bbeSBoris Popov 	if ((error = md_get_uint16le(mdp, &tw)) != 0)
1221681a5bbeSBoris Popov 		return error;
1222681a5bbeSBoris Popov 	if (tw)
1223681a5bbeSBoris Popov 		ctx->f_flags |= SMBFS_RDD_EOF | SMBFS_RDD_NOCLOSE;
1224681a5bbeSBoris Popov 	if ((error = md_get_uint16le(mdp, &tw)) != 0)
1225681a5bbeSBoris Popov 		return error;
1226681a5bbeSBoris Popov 	if ((error = md_get_uint16le(mdp, &tw)) != 0)
1227681a5bbeSBoris Popov 		return error;
1228cc518d3bSBoris Popov 	if (ctx->f_ecnt == 0) {
1229cc518d3bSBoris Popov 		ctx->f_flags |= SMBFS_RDD_EOF | SMBFS_RDD_NOCLOSE;
1230681a5bbeSBoris Popov 		return ENOENT;
1231cc518d3bSBoris Popov 	}
1232681a5bbeSBoris Popov 	ctx->f_rnameofs = tw;
1233681a5bbeSBoris Popov 	mdp = &t2p->t2_rdata;
1234681a5bbeSBoris Popov 	if (mdp->md_top == NULL) {
1235681a5bbeSBoris Popov 		printf("bug: ecnt = %d, but data is NULL (please report)\n", ctx->f_ecnt);
1236681a5bbeSBoris Popov 		return ENOENT;
1237681a5bbeSBoris Popov 	}
1238681a5bbeSBoris Popov 	if (mdp->md_top->m_len == 0) {
1239681a5bbeSBoris Popov 		printf("bug: ecnt = %d, but m_len = 0 and m_next = %p (please report)\n", ctx->f_ecnt,mbp->mb_top->m_next);
1240681a5bbeSBoris Popov 		return ENOENT;
1241681a5bbeSBoris Popov 	}
1242681a5bbeSBoris Popov 	ctx->f_eofs = 0;
1243681a5bbeSBoris Popov 	return 0;
1244681a5bbeSBoris Popov }
1245681a5bbeSBoris Popov 
1246681a5bbeSBoris Popov static int
1247681a5bbeSBoris Popov smbfs_smb_findclose2(struct smbfs_fctx *ctx)
1248681a5bbeSBoris Popov {
1249681a5bbeSBoris Popov 	struct smb_rq rq, *rqp = &rq;
1250681a5bbeSBoris Popov 	struct mbchain *mbp;
1251681a5bbeSBoris Popov 	int error;
1252681a5bbeSBoris Popov 
1253681a5bbeSBoris Popov 	error = smb_rq_init(rqp, SSTOCP(ctx->f_ssp), SMB_COM_FIND_CLOSE2, ctx->f_scred);
1254681a5bbeSBoris Popov 	if (error)
1255681a5bbeSBoris Popov 		return error;
1256681a5bbeSBoris Popov 	smb_rq_getrequest(rqp, &mbp);
1257681a5bbeSBoris Popov 	smb_rq_wstart(rqp);
1258681a5bbeSBoris Popov 	mb_put_mem(mbp, (caddr_t)&ctx->f_Sid, 2, MB_MSYSTEM);
1259681a5bbeSBoris Popov 	smb_rq_wend(rqp);
1260681a5bbeSBoris Popov 	smb_rq_bstart(rqp);
1261681a5bbeSBoris Popov 	smb_rq_bend(rqp);
1262681a5bbeSBoris Popov 	error = smb_rq_simple(rqp);
1263681a5bbeSBoris Popov 	smb_rq_done(rqp);
1264681a5bbeSBoris Popov 	return error;
1265681a5bbeSBoris Popov }
1266681a5bbeSBoris Popov 
1267681a5bbeSBoris Popov static int
1268681a5bbeSBoris Popov smbfs_findopenLM2(struct smbfs_fctx *ctx, struct smbnode *dnp,
1269681a5bbeSBoris Popov 	const char *wildcard, int wclen, int attr, struct smb_cred *scred)
1270681a5bbeSBoris Popov {
1271a163d034SWarner Losh 	ctx->f_name = malloc(SMB_MAXFNAMELEN, M_SMBFSDATA, M_WAITOK);
1272681a5bbeSBoris Popov 	if (ctx->f_name == NULL)
1273681a5bbeSBoris Popov 		return ENOMEM;
1274681a5bbeSBoris Popov 	ctx->f_infolevel = SMB_DIALECT(SSTOVC(ctx->f_ssp)) < SMB_DIALECT_NTLM0_12 ?
1275681a5bbeSBoris Popov 	    SMB_INFO_STANDARD : SMB_FIND_FILE_DIRECTORY_INFO;
1276681a5bbeSBoris Popov 	ctx->f_attrmask = attr;
1277681a5bbeSBoris Popov 	ctx->f_wildcard = wildcard;
1278681a5bbeSBoris Popov 	ctx->f_wclen = wclen;
1279681a5bbeSBoris Popov 	return 0;
1280681a5bbeSBoris Popov }
1281681a5bbeSBoris Popov 
1282681a5bbeSBoris Popov static int
1283681a5bbeSBoris Popov smbfs_findnextLM2(struct smbfs_fctx *ctx, int limit)
1284681a5bbeSBoris Popov {
1285681a5bbeSBoris Popov 	struct mdchain *mbp;
1286681a5bbeSBoris Popov 	struct smb_t2rq *t2p;
1287681a5bbeSBoris Popov 	char *cp;
1288681a5bbeSBoris Popov 	u_int8_t tb;
1289681a5bbeSBoris Popov 	u_int16_t date, time, wattr;
1290681a5bbeSBoris Popov 	u_int32_t size, next, dattr;
1291681a5bbeSBoris Popov 	int64_t lint;
1292681a5bbeSBoris Popov 	int error, svtz, cnt, fxsz, nmlen, recsz;
1293681a5bbeSBoris Popov 
1294681a5bbeSBoris Popov 	if (ctx->f_ecnt == 0) {
1295681a5bbeSBoris Popov 		if (ctx->f_flags & SMBFS_RDD_EOF)
1296681a5bbeSBoris Popov 			return ENOENT;
1297681a5bbeSBoris Popov 		ctx->f_left = ctx->f_limit = limit;
1298681a5bbeSBoris Popov 		error = smbfs_smb_trans2find2(ctx);
1299681a5bbeSBoris Popov 		if (error)
1300681a5bbeSBoris Popov 			return error;
1301681a5bbeSBoris Popov 	}
1302681a5bbeSBoris Popov 	t2p = ctx->f_t2;
1303681a5bbeSBoris Popov 	mbp = &t2p->t2_rdata;
1304681a5bbeSBoris Popov 	svtz = SSTOVC(ctx->f_ssp)->vc_sopt.sv_tz;
1305681a5bbeSBoris Popov 	switch (ctx->f_infolevel) {
1306681a5bbeSBoris Popov 	    case SMB_INFO_STANDARD:
1307681a5bbeSBoris Popov 		next = 0;
1308681a5bbeSBoris Popov 		fxsz = 0;
1309681a5bbeSBoris Popov 		md_get_uint16le(mbp, &date);
1310681a5bbeSBoris Popov 		md_get_uint16le(mbp, &time);	/* creation time */
1311681a5bbeSBoris Popov 		md_get_uint16le(mbp, &date);
1312681a5bbeSBoris Popov 		md_get_uint16le(mbp, &time);	/* access time */
1313681a5bbeSBoris Popov 		smb_dos2unixtime(date, time, 0, svtz, &ctx->f_attr.fa_atime);
1314681a5bbeSBoris Popov 		md_get_uint16le(mbp, &date);
1315681a5bbeSBoris Popov 		md_get_uint16le(mbp, &time);	/* access time */
1316681a5bbeSBoris Popov 		smb_dos2unixtime(date, time, 0, svtz, &ctx->f_attr.fa_mtime);
1317681a5bbeSBoris Popov 		md_get_uint32le(mbp, &size);
1318681a5bbeSBoris Popov 		ctx->f_attr.fa_size = size;
1319681a5bbeSBoris Popov 		md_get_uint32(mbp, NULL);	/* allocation size */
1320681a5bbeSBoris Popov 		md_get_uint16le(mbp, &wattr);
1321681a5bbeSBoris Popov 		ctx->f_attr.fa_attr = wattr;
1322681a5bbeSBoris Popov 		md_get_uint8(mbp, &tb);
1323681a5bbeSBoris Popov 		size = nmlen = tb;
1324681a5bbeSBoris Popov 		fxsz = 23;
1325681a5bbeSBoris Popov 		recsz = next = 24 + nmlen;	/* docs misses zero byte at end */
1326681a5bbeSBoris Popov 		break;
1327681a5bbeSBoris Popov 	    case SMB_FIND_FILE_DIRECTORY_INFO:
1328681a5bbeSBoris Popov 		md_get_uint32le(mbp, &next);
1329681a5bbeSBoris Popov 		md_get_uint32(mbp, NULL);	/* file index */
1330681a5bbeSBoris Popov 		md_get_int64(mbp, NULL);	/* creation time */
1331681a5bbeSBoris Popov 		md_get_int64le(mbp, &lint);
1332681a5bbeSBoris Popov 		smb_time_NT2local(lint, svtz, &ctx->f_attr.fa_atime);
1333681a5bbeSBoris Popov 		md_get_int64le(mbp, &lint);
1334681a5bbeSBoris Popov 		smb_time_NT2local(lint, svtz, &ctx->f_attr.fa_mtime);
1335681a5bbeSBoris Popov 		md_get_int64le(mbp, &lint);
1336681a5bbeSBoris Popov 		smb_time_NT2local(lint, svtz, &ctx->f_attr.fa_ctime);
1337681a5bbeSBoris Popov 		md_get_int64le(mbp, &lint);	/* file size */
1338681a5bbeSBoris Popov 		ctx->f_attr.fa_size = lint;
1339681a5bbeSBoris Popov 		md_get_int64(mbp, NULL);	/* real size (should use) */
1340798bb23eSBoris Popov 		md_get_uint32le(mbp, &dattr);	/* EA */
1341681a5bbeSBoris Popov 		ctx->f_attr.fa_attr = dattr;
1342681a5bbeSBoris Popov 		md_get_uint32le(mbp, &size);	/* name len */
1343681a5bbeSBoris Popov 		fxsz = 64;
1344681a5bbeSBoris Popov 		recsz = next ? next : fxsz + size;
1345681a5bbeSBoris Popov 		break;
1346681a5bbeSBoris Popov 	    default:
1347681a5bbeSBoris Popov 		SMBERROR("unexpected info level %d\n", ctx->f_infolevel);
1348681a5bbeSBoris Popov 		return EINVAL;
1349681a5bbeSBoris Popov 	}
1350681a5bbeSBoris Popov 	nmlen = min(size, SMB_MAXFNAMELEN);
1351681a5bbeSBoris Popov 	cp = ctx->f_name;
1352681a5bbeSBoris Popov 	error = md_get_mem(mbp, cp, nmlen, MB_MSYSTEM);
1353681a5bbeSBoris Popov 	if (error)
1354681a5bbeSBoris Popov 		return error;
1355681a5bbeSBoris Popov 	if (next) {
1356681a5bbeSBoris Popov 		cnt = next - nmlen - fxsz;
1357681a5bbeSBoris Popov 		if (cnt > 0)
1358681a5bbeSBoris Popov 			md_get_mem(mbp, NULL, cnt, MB_MSYSTEM);
1359681a5bbeSBoris Popov 		else if (cnt < 0) {
1360681a5bbeSBoris Popov 			SMBERROR("out of sync\n");
1361681a5bbeSBoris Popov 			return EBADRPC;
1362681a5bbeSBoris Popov 		}
1363681a5bbeSBoris Popov 	}
1364681a5bbeSBoris Popov 	if (nmlen && cp[nmlen - 1] == 0)
1365681a5bbeSBoris Popov 		nmlen--;
1366681a5bbeSBoris Popov 	if (nmlen == 0)
1367681a5bbeSBoris Popov 		return EBADRPC;
1368681a5bbeSBoris Popov 
1369681a5bbeSBoris Popov 	next = ctx->f_eofs + recsz;
1370681a5bbeSBoris Popov 	if (ctx->f_rnameofs && (ctx->f_flags & SMBFS_RDD_GOTRNAME) == 0 &&
1371681a5bbeSBoris Popov 	    (ctx->f_rnameofs >= ctx->f_eofs && ctx->f_rnameofs < next)) {
1372681a5bbeSBoris Popov 		/*
1373681a5bbeSBoris Popov 		 * Server needs a resume filename.
1374681a5bbeSBoris Popov 		 */
1375681a5bbeSBoris Popov 		if (ctx->f_rnamelen <= nmlen) {
1376681a5bbeSBoris Popov 			if (ctx->f_rname)
1377681a5bbeSBoris Popov 				free(ctx->f_rname, M_SMBFSDATA);
1378a163d034SWarner Losh 			ctx->f_rname = malloc(nmlen + 1, M_SMBFSDATA, M_WAITOK);
1379681a5bbeSBoris Popov 			ctx->f_rnamelen = nmlen;
1380681a5bbeSBoris Popov 		}
1381681a5bbeSBoris Popov 		bcopy(ctx->f_name, ctx->f_rname, nmlen);
1382681a5bbeSBoris Popov 		ctx->f_rname[nmlen] = 0;
1383681a5bbeSBoris Popov 		ctx->f_flags |= SMBFS_RDD_GOTRNAME;
1384681a5bbeSBoris Popov 	}
1385681a5bbeSBoris Popov 	ctx->f_nmlen = nmlen;
1386681a5bbeSBoris Popov 	ctx->f_eofs = next;
1387681a5bbeSBoris Popov 	ctx->f_ecnt--;
1388681a5bbeSBoris Popov 	ctx->f_left--;
1389681a5bbeSBoris Popov 	return 0;
1390681a5bbeSBoris Popov }
1391681a5bbeSBoris Popov 
1392681a5bbeSBoris Popov static int
1393681a5bbeSBoris Popov smbfs_findcloseLM2(struct smbfs_fctx *ctx)
1394681a5bbeSBoris Popov {
1395681a5bbeSBoris Popov 	if (ctx->f_name)
1396681a5bbeSBoris Popov 		free(ctx->f_name, M_SMBFSDATA);
1397681a5bbeSBoris Popov 	if (ctx->f_t2)
1398681a5bbeSBoris Popov 		smb_t2_done(ctx->f_t2);
1399681a5bbeSBoris Popov 	if ((ctx->f_flags & SMBFS_RDD_NOCLOSE) == 0)
1400681a5bbeSBoris Popov 		smbfs_smb_findclose2(ctx);
1401681a5bbeSBoris Popov 	return 0;
1402681a5bbeSBoris Popov }
1403681a5bbeSBoris Popov 
1404681a5bbeSBoris Popov int
1405681a5bbeSBoris Popov smbfs_findopen(struct smbnode *dnp, const char *wildcard, int wclen, int attr,
1406681a5bbeSBoris Popov 	struct smb_cred *scred, struct smbfs_fctx **ctxpp)
1407681a5bbeSBoris Popov {
1408681a5bbeSBoris Popov 	struct smbfs_fctx *ctx;
1409681a5bbeSBoris Popov 	int error;
1410681a5bbeSBoris Popov 
1411a163d034SWarner Losh 	ctx = malloc(sizeof(*ctx), M_SMBFSDATA, M_WAITOK);
1412681a5bbeSBoris Popov 	if (ctx == NULL)
1413681a5bbeSBoris Popov 		return ENOMEM;
1414681a5bbeSBoris Popov 	bzero(ctx, sizeof(*ctx));
1415681a5bbeSBoris Popov 	ctx->f_ssp = dnp->n_mount->sm_share;
1416681a5bbeSBoris Popov 	ctx->f_dnp = dnp;
1417681a5bbeSBoris Popov 	ctx->f_flags = SMBFS_RDD_FINDFIRST;
1418681a5bbeSBoris Popov 	ctx->f_scred = scred;
1419681a5bbeSBoris Popov 	if (SMB_DIALECT(SSTOVC(ctx->f_ssp)) < SMB_DIALECT_LANMAN2_0 ||
1420d14c8441SPoul-Henning Kamp 	    (dnp->n_mount->sm_flags & SMBFS_MOUNT_NO_LONG)) {
1421681a5bbeSBoris Popov 		ctx->f_flags |= SMBFS_RDD_USESEARCH;
1422681a5bbeSBoris Popov 		error = smbfs_findopenLM1(ctx, dnp, wildcard, wclen, attr, scred);
1423681a5bbeSBoris Popov 	} else
1424681a5bbeSBoris Popov 		error = smbfs_findopenLM2(ctx, dnp, wildcard, wclen, attr, scred);
1425681a5bbeSBoris Popov 	if (error)
1426681a5bbeSBoris Popov 		smbfs_findclose(ctx, scred);
1427681a5bbeSBoris Popov 	else
1428681a5bbeSBoris Popov 		*ctxpp = ctx;
1429681a5bbeSBoris Popov 	return error;
1430681a5bbeSBoris Popov }
1431681a5bbeSBoris Popov 
1432681a5bbeSBoris Popov int
1433681a5bbeSBoris Popov smbfs_findnext(struct smbfs_fctx *ctx, int limit, struct smb_cred *scred)
1434681a5bbeSBoris Popov {
1435681a5bbeSBoris Popov 	int error;
1436681a5bbeSBoris Popov 
1437681a5bbeSBoris Popov 	if (limit == 0)
1438681a5bbeSBoris Popov 		limit = 1000000;
1439681a5bbeSBoris Popov 	else if (limit > 1)
1440681a5bbeSBoris Popov 		limit *= 4;	/* imperical */
1441681a5bbeSBoris Popov 	ctx->f_scred = scred;
1442681a5bbeSBoris Popov 	for (;;) {
1443681a5bbeSBoris Popov 		if (ctx->f_flags & SMBFS_RDD_USESEARCH) {
1444681a5bbeSBoris Popov 			error = smbfs_findnextLM1(ctx, limit);
1445681a5bbeSBoris Popov 		} else
1446681a5bbeSBoris Popov 			error = smbfs_findnextLM2(ctx, limit);
1447681a5bbeSBoris Popov 		if (error)
1448681a5bbeSBoris Popov 			return error;
1449681a5bbeSBoris Popov 		if ((ctx->f_nmlen == 1 && ctx->f_name[0] == '.') ||
1450681a5bbeSBoris Popov 		    (ctx->f_nmlen == 2 && ctx->f_name[0] == '.' &&
1451681a5bbeSBoris Popov 		     ctx->f_name[1] == '.'))
1452681a5bbeSBoris Popov 			continue;
1453681a5bbeSBoris Popov 		break;
1454681a5bbeSBoris Popov 	}
14554ebd3ea1STakanori Watanabe 	smbfs_fname_tolocal(SSTOVC(ctx->f_ssp), ctx->f_name, &ctx->f_nmlen,
1456681a5bbeSBoris Popov 			    ctx->f_dnp->n_mount->sm_caseopt);
1457681a5bbeSBoris Popov 	ctx->f_attr.fa_ino = smbfs_getino(ctx->f_dnp, ctx->f_name, ctx->f_nmlen);
1458681a5bbeSBoris Popov 	return 0;
1459681a5bbeSBoris Popov }
1460681a5bbeSBoris Popov 
1461681a5bbeSBoris Popov int
1462681a5bbeSBoris Popov smbfs_findclose(struct smbfs_fctx *ctx, struct smb_cred *scred)
1463681a5bbeSBoris Popov {
1464681a5bbeSBoris Popov 	ctx->f_scred = scred;
1465681a5bbeSBoris Popov 	if (ctx->f_flags & SMBFS_RDD_USESEARCH) {
1466681a5bbeSBoris Popov 		smbfs_findcloseLM1(ctx);
1467681a5bbeSBoris Popov 	} else
1468681a5bbeSBoris Popov 		smbfs_findcloseLM2(ctx);
1469681a5bbeSBoris Popov 	if (ctx->f_rname)
1470681a5bbeSBoris Popov 		free(ctx->f_rname, M_SMBFSDATA);
1471681a5bbeSBoris Popov 	free(ctx, M_SMBFSDATA);
1472681a5bbeSBoris Popov 	return 0;
1473681a5bbeSBoris Popov }
1474681a5bbeSBoris Popov 
1475681a5bbeSBoris Popov int
1476681a5bbeSBoris Popov smbfs_smb_lookup(struct smbnode *dnp, const char *name, int nmlen,
1477681a5bbeSBoris Popov 	struct smbfattr *fap, struct smb_cred *scred)
1478681a5bbeSBoris Popov {
1479681a5bbeSBoris Popov 	struct smbfs_fctx *ctx;
1480681a5bbeSBoris Popov 	int error;
1481681a5bbeSBoris Popov 
1482681a5bbeSBoris Popov 	if (dnp == NULL || (dnp->n_ino == 2 && name == NULL)) {
1483681a5bbeSBoris Popov 		bzero(fap, sizeof(*fap));
1484681a5bbeSBoris Popov 		fap->fa_attr = SMB_FA_DIR;
1485681a5bbeSBoris Popov 		fap->fa_ino = 2;
1486681a5bbeSBoris Popov 		return 0;
1487681a5bbeSBoris Popov 	}
1488681a5bbeSBoris Popov 	if (nmlen == 1 && name[0] == '.') {
1489681a5bbeSBoris Popov 		error = smbfs_smb_lookup(dnp, NULL, 0, fap, scred);
1490681a5bbeSBoris Popov 		return error;
1491681a5bbeSBoris Popov 	} else if (nmlen == 2 && name[0] == '.' && name[1] == '.') {
149211de0c59STim J. Robbins 		error = smbfs_smb_lookup(VTOSMB(dnp->n_parent), NULL, 0, fap,
149311de0c59STim J. Robbins 		    scred);
14946e551fb6SDavid E. O'Brien 		printf("%s: knows NOTHING about '..'\n", __func__);
1495681a5bbeSBoris Popov 		return error;
1496681a5bbeSBoris Popov 	}
1497681a5bbeSBoris Popov 	error = smbfs_findopen(dnp, name, nmlen,
1498681a5bbeSBoris Popov 	    SMB_FA_SYSTEM | SMB_FA_HIDDEN | SMB_FA_DIR, scred, &ctx);
1499681a5bbeSBoris Popov 	if (error)
1500681a5bbeSBoris Popov 		return error;
1501681a5bbeSBoris Popov 	ctx->f_flags |= SMBFS_RDD_FINDSINGLE;
1502681a5bbeSBoris Popov 	error = smbfs_findnext(ctx, 1, scred);
1503681a5bbeSBoris Popov 	if (error == 0) {
1504681a5bbeSBoris Popov 		*fap = ctx->f_attr;
1505681a5bbeSBoris Popov 		if (name == NULL)
1506681a5bbeSBoris Popov 			fap->fa_ino = dnp->n_ino;
1507681a5bbeSBoris Popov 	}
1508681a5bbeSBoris Popov 	smbfs_findclose(ctx, scred);
1509681a5bbeSBoris Popov 	return error;
1510681a5bbeSBoris Popov }
1511