smbfs_subr.c revision 75374
1/*
2 * Copyright (c) 2000-2001, Boris Popov
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
19 *
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
30 * SUCH DAMAGE.
31 *
32 * $FreeBSD: head/sys/fs/smbfs/smbfs_subr.c 75374 2001-04-10 07:59:06Z bp $
33 */
34#include <sys/param.h>
35#include <sys/systm.h>
36#include <sys/kernel.h>
37#include <sys/malloc.h>
38#include <machine/clock.h>
39#include <sys/time.h>
40#include <sys/vnode.h>
41#include <sys/sysctl.h>
42#include <sys/iconv.h>
43
44#include <netsmb/smb.h>
45#include <netsmb/smb_conn.h>
46#include <netsmb/smb_subr.h>
47#include <netsmb/smb_rq.h>
48#include <netsmb/smb_dev.h>
49
50#include <fs/smbfs/smbfs.h>
51#include <fs/smbfs/smbfs_node.h>
52#include <fs/smbfs/smbfs_subr.h>
53
54MALLOC_DEFINE(M_SMBFSDATA, "SMBFS data", "SMBFS private data");
55
56/*
57 * Time & date conversion routines taken from msdosfs. Although leap
58 * year calculation is bogus, it's sufficient before 2100 :)
59 */
60/*
61 * This is the format of the contents of the deTime field in the direntry
62 * structure.
63 * We don't use bitfields because we don't know how compilers for
64 * arbitrary machines will lay them out.
65 */
66#define DT_2SECONDS_MASK	0x1F	/* seconds divided by 2 */
67#define DT_2SECONDS_SHIFT	0
68#define DT_MINUTES_MASK		0x7E0	/* minutes */
69#define DT_MINUTES_SHIFT	5
70#define DT_HOURS_MASK		0xF800	/* hours */
71#define DT_HOURS_SHIFT		11
72
73/*
74 * This is the format of the contents of the deDate field in the direntry
75 * structure.
76 */
77#define DD_DAY_MASK		0x1F	/* day of month */
78#define DD_DAY_SHIFT		0
79#define DD_MONTH_MASK		0x1E0	/* month */
80#define DD_MONTH_SHIFT		5
81#define DD_YEAR_MASK		0xFE00	/* year - 1980 */
82#define DD_YEAR_SHIFT		9
83/*
84 * Total number of days that have passed for each month in a regular year.
85 */
86static u_short regyear[] = {
87	31, 59, 90, 120, 151, 181,
88	212, 243, 273, 304, 334, 365
89};
90
91/*
92 * Total number of days that have passed for each month in a leap year.
93 */
94static u_short leapyear[] = {
95	31, 60, 91, 121, 152, 182,
96	213, 244, 274, 305, 335, 366
97};
98
99/*
100 * Variables used to remember parts of the last time conversion.  Maybe we
101 * can avoid a full conversion.
102 */
103static u_long  lasttime;
104static u_long  lastday;
105static u_short lastddate;
106static u_short lastdtime;
107
108void
109smb_time_local2server(struct timespec *tsp, int tzoff, u_long *seconds)
110{
111	*seconds = tsp->tv_sec - tzoff * 60 /*- tz.tz_minuteswest * 60 -
112	    (wall_cmos_clock ? adjkerntz : 0)*/;
113}
114
115void
116smb_time_server2local(u_long seconds, int tzoff, struct timespec *tsp)
117{
118	tsp->tv_sec = seconds + tzoff * 60;
119	    /*+ tz.tz_minuteswest * 60 + (wall_cmos_clock ? adjkerntz : 0)*/;
120}
121
122/*
123 * Number of seconds between 1970 and 1601 year
124 */
125int64_t DIFF1970TO1601 = 11644473600ULL;
126
127/*
128 * Time from server comes as UTC, so no need to use tz
129 */
130void
131smb_time_NT2local(int64_t nsec, int tzoff, struct timespec *tsp)
132{
133	smb_time_server2local(nsec / 10000000 - DIFF1970TO1601, 0, tsp);
134}
135
136void
137smb_time_local2NT(struct timespec *tsp, int tzoff, int64_t *nsec)
138{
139	u_long seconds;
140
141	smb_time_local2server(tsp, 0, &seconds);
142	*nsec = (((int64_t)(seconds) & ~1) + DIFF1970TO1601) * (int64_t)10000000;
143}
144
145void
146smb_time_unix2dos(struct timespec *tsp, int tzoff, u_int16_t *ddp,
147	u_int16_t *dtp,	u_int8_t *dhp)
148{
149	u_long t, days, year, month, inc;
150	u_short *months;
151
152	/*
153	 * If the time from the last conversion is the same as now, then
154	 * skip the computations and use the saved result.
155	 */
156	smb_time_local2server(tsp, tzoff, &t);
157	t &= ~1;
158	if (lasttime != t) {
159		lasttime = t;
160		lastdtime = (((t / 2) % 30) << DT_2SECONDS_SHIFT)
161		    + (((t / 60) % 60) << DT_MINUTES_SHIFT)
162		    + (((t / 3600) % 24) << DT_HOURS_SHIFT);
163
164		/*
165		 * If the number of days since 1970 is the same as the last
166		 * time we did the computation then skip all this leap year
167		 * and month stuff.
168		 */
169		days = t / (24 * 60 * 60);
170		if (days != lastday) {
171			lastday = days;
172			for (year = 1970;; year++) {
173				inc = year & 0x03 ? 365 : 366;
174				if (days < inc)
175					break;
176				days -= inc;
177			}
178			months = year & 0x03 ? regyear : leapyear;
179			for (month = 0; days >= months[month]; month++)
180				;
181			if (month > 0)
182				days -= months[month - 1];
183			lastddate = ((days + 1) << DD_DAY_SHIFT)
184			    + ((month + 1) << DD_MONTH_SHIFT);
185			/*
186			 * Remember dos's idea of time is relative to 1980.
187			 * unix's is relative to 1970.  If somehow we get a
188			 * time before 1980 then don't give totally crazy
189			 * results.
190			 */
191			if (year > 1980)
192				lastddate += (year - 1980) << DD_YEAR_SHIFT;
193		}
194	}
195	if (dtp)
196		*dtp = lastdtime;
197	if (dhp)
198		*dhp = (tsp->tv_sec & 1) * 100 + tsp->tv_nsec / 10000000;
199
200	*ddp = lastddate;
201}
202
203/*
204 * The number of seconds between Jan 1, 1970 and Jan 1, 1980. In that
205 * interval there were 8 regular years and 2 leap years.
206 */
207#define	SECONDSTO1980	(((8 * 365) + (2 * 366)) * (24 * 60 * 60))
208
209static u_short lastdosdate;
210static u_long  lastseconds;
211
212void
213smb_dos2unixtime(u_int dd, u_int dt, u_int dh, int tzoff,
214	struct timespec *tsp)
215{
216	u_long seconds;
217	u_long month;
218	u_long year;
219	u_long days;
220	u_short *months;
221
222	if (dd == 0) {
223		tsp->tv_sec = 0;
224		tsp->tv_nsec = 0;
225		return;
226	}
227	seconds = (((dt & DT_2SECONDS_MASK) >> DT_2SECONDS_SHIFT) << 1)
228	    + ((dt & DT_MINUTES_MASK) >> DT_MINUTES_SHIFT) * 60
229	    + ((dt & DT_HOURS_MASK) >> DT_HOURS_SHIFT) * 3600
230	    + dh / 100;
231	/*
232	 * If the year, month, and day from the last conversion are the
233	 * same then use the saved value.
234	 */
235	if (lastdosdate != dd) {
236		lastdosdate = dd;
237		days = 0;
238		year = (dd & DD_YEAR_MASK) >> DD_YEAR_SHIFT;
239		days = year * 365;
240		days += year / 4 + 1;	/* add in leap days */
241		if ((year & 0x03) == 0)
242			days--;		/* if year is a leap year */
243		months = year & 0x03 ? regyear : leapyear;
244		month = (dd & DD_MONTH_MASK) >> DD_MONTH_SHIFT;
245		if (month < 1 || month > 12) {
246			month = 1;
247		}
248		if (month > 1)
249			days += months[month - 2];
250		days += ((dd & DD_DAY_MASK) >> DD_DAY_SHIFT) - 1;
251		lastseconds = (days * 24 * 60 * 60) + SECONDSTO1980;
252	}
253	smb_time_server2local(seconds + lastseconds, tzoff, tsp);
254	tsp->tv_nsec = (dh % 100) * 10000000;
255}
256
257static int
258smb_fphelp(struct mbchain *mbp, struct smb_vc *vcp, struct smbnode *np,
259	int caseopt)
260{
261	struct smbmount *smp= np->n_mount;
262	struct smbnode **npp = smp->sm_npstack;
263	int i, error = 0;
264
265/*	simple_lock(&smp->sm_npslock);*/
266	i = 0;
267	while (np->n_parent) {
268		if (i++ == SMBFS_MAXPATHCOMP) {
269/*			simple_unlock(&smp->sm_npslock);*/
270			return ENAMETOOLONG;
271		}
272		*npp++ = np;
273		np = np->n_parent;
274	}
275/*	if (i == 0)
276		return smb_put_dmem(mbp, vcp, "\\", 2, caseopt);*/
277	while (i--) {
278		np = *--npp;
279		error = mb_put_uint8(mbp, '\\');
280		if (error)
281			break;
282		error = smb_put_dmem(mbp, vcp, np->n_name, np->n_nmlen, caseopt);
283		if (error)
284			break;
285	}
286/*	simple_unlock(&smp->sm_npslock);*/
287	return error;
288}
289
290int
291smbfs_fullpath(struct mbchain *mbp, struct smb_vc *vcp, struct smbnode *dnp,
292	const char *name, int nmlen)
293{
294	int caseopt = SMB_CS_NONE;
295	int error;
296
297	if (SMB_DIALECT(vcp) < SMB_DIALECT_LANMAN1_0)
298		caseopt |= SMB_CS_UPPER;
299	if (dnp != NULL) {
300		error = smb_fphelp(mbp, vcp, dnp, caseopt);
301		if (error)
302			return error;
303	}
304	if (name) {
305		error = mb_put_uint8(mbp, '\\');
306		if (error)
307			return error;
308		error = smb_put_dmem(mbp, vcp, name, nmlen, caseopt);
309		if (error)
310			return error;
311	}
312	error = mb_put_uint8(mbp, 0);
313	return error;
314}
315
316int
317smbfs_fname_tolocal(struct smb_vc *vcp, char *name, int nmlen, int caseopt)
318{
319/*	if (caseopt & SMB_CS_UPPER)
320		iconv_convmem(vcp->vc_toupper, name, name, nmlen);
321	else if (caseopt & SMB_CS_LOWER)
322		iconv_convmem(vcp->vc_tolower, name, name, nmlen);*/
323	if (vcp->vc_tolocal)
324		iconv_convmem(vcp->vc_tolocal, name, name, nmlen);
325	return 0;
326}
327