The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/fs/smbfs/smbfs_subr.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    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$
   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 
   54 MALLOC_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  */
   86 static 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  */
   94 static 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  */
  103 static u_long  lasttime;
  104 static u_long  lastday;
  105 static u_short lastddate;
  106 static u_short lastdtime;
  107 
  108 void
  109 smb_time_local2server(struct timespec *tsp, int tzoff, u_long *seconds)
  110 {
  111         *seconds = tsp->tv_sec - tzoff * 60 /*- tz_minuteswest * 60 -
  112             (wall_cmos_clock ? adjkerntz : 0)*/;
  113 }
  114 
  115 void
  116 smb_time_server2local(u_long seconds, int tzoff, struct timespec *tsp)
  117 {
  118         tsp->tv_sec = seconds + tzoff * 60;
  119             /*+ tz_minuteswest * 60 + (wall_cmos_clock ? adjkerntz : 0)*/;
  120 }
  121 
  122 /*
  123  * Number of seconds between 1970 and 1601 year
  124  */
  125 static int64_t DIFF1970TO1601 = 11644473600ULL;
  126 
  127 /*
  128  * Time from server comes as UTC, so no need to use tz
  129  */
  130 void
  131 smb_time_NT2local(int64_t nsec, int tzoff, struct timespec *tsp)
  132 {
  133         smb_time_server2local(nsec / 10000000 - DIFF1970TO1601, 0, tsp);
  134 }
  135 
  136 void
  137 smb_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 
  145 void
  146 smb_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 
  209 static u_short lastdosdate;
  210 static u_long  lastseconds;
  211 
  212 void
  213 smb_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 
  257 static int
  258 smb_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                 if ((np->n_flag & NREFPARENT) == 0)
  274                         break;
  275                 np = VTOSMB(np->n_parent);
  276         }
  277 /*      if (i == 0)
  278                 return smb_put_dmem(mbp, vcp, "\\", 2, caseopt);*/
  279         while (i--) {
  280                 np = *--npp;
  281                 error = mb_put_uint8(mbp, '\\');
  282                 if (error)
  283                         break;
  284                 error = smb_put_dmem(mbp, vcp, np->n_name, np->n_nmlen, caseopt);
  285                 if (error)
  286                         break;
  287         }
  288 /*      simple_unlock(&smp->sm_npslock);*/
  289         return error;
  290 }
  291 
  292 int
  293 smbfs_fullpath(struct mbchain *mbp, struct smb_vc *vcp, struct smbnode *dnp,
  294         const char *name, int nmlen)
  295 {
  296         int caseopt = SMB_CS_NONE;
  297         int error;
  298 
  299         if (SMB_DIALECT(vcp) < SMB_DIALECT_LANMAN1_0)
  300                 caseopt |= SMB_CS_UPPER;
  301         if (dnp != NULL) {
  302                 error = smb_fphelp(mbp, vcp, dnp, caseopt);
  303                 if (error)
  304                         return error;
  305         }
  306         if (name) {
  307                 error = mb_put_uint8(mbp, '\\');
  308                 if (error)
  309                         return error;
  310                 error = smb_put_dmem(mbp, vcp, name, nmlen, caseopt);
  311                 if (error)
  312                         return error;
  313         }
  314         error = mb_put_uint8(mbp, 0);
  315         return error;
  316 }
  317 
  318 int
  319 smbfs_fname_tolocal(struct smb_vc *vcp, char *name, int *nmlen, int caseopt)
  320 {
  321         int copt = (caseopt == SMB_CS_LOWER ? KICONV_FROM_LOWER : 
  322                     (caseopt == SMB_CS_UPPER ? KICONV_FROM_UPPER : 0));
  323         int error = 0;
  324         size_t ilen = *nmlen;
  325         size_t olen;
  326         char *ibuf = name;
  327         char outbuf[SMB_MAXFNAMELEN];
  328         char *obuf = outbuf;
  329 
  330         if (vcp->vc_tolocal) {
  331                 olen = sizeof(outbuf);
  332                 bzero(outbuf, sizeof(outbuf));
  333 
  334                 /*
  335                 error = iconv_conv_case
  336                         (vcp->vc_tolocal, NULL, NULL, &obuf, &olen, copt);
  337                 if (error) return error;
  338                 */
  339 
  340                 error = iconv_conv_case
  341                         (vcp->vc_tolocal, (const char **)&ibuf, &ilen, &obuf, &olen, copt);
  342                 if (!error) {
  343                         *nmlen = sizeof(outbuf) - olen;
  344                         memcpy(name, outbuf, *nmlen);
  345                 }
  346         }
  347         return error;
  348 }

Cache object: 58007e314eaafc91491785040e7c2a73


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.