ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / fs / smbfs / proc.c
1 /*
2  *  proc.c
3  *
4  *  Copyright (C) 1995, 1996 by Paal-Kr. Engstad and Volker Lendecke
5  *  Copyright (C) 1997 by Volker Lendecke
6  *
7  *  Please add a note about your changes to smbfs in the ChangeLog file.
8  */
9
10 #include <linux/types.h>
11 #include <linux/errno.h>
12 #include <linux/slab.h>
13 #include <linux/fs.h>
14 #include <linux/file.h>
15 #include <linux/stat.h>
16 #include <linux/fcntl.h>
17 #include <linux/dcache.h>
18 #include <linux/dirent.h>
19 #include <linux/nls.h>
20 #include <linux/smp_lock.h>
21 #include <linux/net.h>
22 #include <linux/vfs.h>
23 #include <linux/smb_fs.h>
24 #include <linux/smbno.h>
25 #include <linux/smb_mount.h>
26
27 #include <net/sock.h>
28
29 #include <asm/string.h>
30 #include <asm/div64.h>
31
32 #include "smb_debug.h"
33 #include "proto.h"
34 #include "request.h"
35
36
37 /* Features. Undefine if they cause problems, this should perhaps be a
38    config option. */
39 #define SMBFS_POSIX_UNLINK 1
40
41 /* Allow smb_retry to be interrupted. */
42 #define SMB_RETRY_INTR
43
44 #define SMB_VWV(packet)  ((packet) + SMB_HEADER_LEN)
45 #define SMB_CMD(packet)  (*(packet+8))
46 #define SMB_WCT(packet)  (*(packet+SMB_HEADER_LEN - 1))
47
48 #define SMB_DIRINFO_SIZE 43
49 #define SMB_STATUS_SIZE  21
50
51 #define SMB_ST_BLKSIZE  (PAGE_SIZE)
52 #define SMB_ST_BLKSHIFT (PAGE_SHIFT)
53
54 static struct smb_ops smb_ops_core;
55 static struct smb_ops smb_ops_os2;
56 static struct smb_ops smb_ops_win95;
57 static struct smb_ops smb_ops_winNT;
58 static struct smb_ops smb_ops_unix;
59
60 static void
61 smb_init_dirent(struct smb_sb_info *server, struct smb_fattr *fattr);
62 static void
63 smb_finish_dirent(struct smb_sb_info *server, struct smb_fattr *fattr);
64 static int
65 smb_proc_getattr_core(struct smb_sb_info *server, struct dentry *dir,
66                       struct smb_fattr *fattr);
67 static int
68 smb_proc_getattr_ff(struct smb_sb_info *server, struct dentry *dentry,
69                     struct smb_fattr *fattr);
70 static int
71 smb_proc_setattr_core(struct smb_sb_info *server, struct dentry *dentry,
72                       u16 attr);
73 static int
74 smb_proc_setattr_ext(struct smb_sb_info *server,
75                      struct inode *inode, struct smb_fattr *fattr);
76 int
77 smb_proc_query_cifsunix(struct smb_sb_info *server);
78 static void
79 install_ops(struct smb_ops *dst, struct smb_ops *src);
80
81
82 static void
83 str_upper(char *name, int len)
84 {
85         while (len--)
86         {
87                 if (*name >= 'a' && *name <= 'z')
88                         *name -= ('a' - 'A');
89                 name++;
90         }
91 }
92
93 #if 0
94 static void
95 str_lower(char *name, int len)
96 {
97         while (len--)
98         {
99                 if (*name >= 'A' && *name <= 'Z')
100                         *name += ('a' - 'A');
101                 name++;
102         }
103 }
104 #endif
105
106 /* reverse a string inline. This is used by the dircache walking routines */
107 static void reverse_string(char *buf, int len)
108 {
109         char c;
110         char *end = buf+len-1;
111
112         while(buf < end) {
113                 c = *buf;
114                 *(buf++) = *end;
115                 *(end--) = c;
116         }
117 }
118
119 /* no conversion, just a wrapper for memcpy. */
120 static int convert_memcpy(unsigned char *output, int olen,
121                           const unsigned char *input, int ilen,
122                           struct nls_table *nls_from,
123                           struct nls_table *nls_to)
124 {
125         if (olen < ilen)
126                 return -ENAMETOOLONG;
127         memcpy(output, input, ilen);
128         return ilen;
129 }
130
131 static inline int write_char(unsigned char ch, char *output, int olen)
132 {
133         if (olen < 4)
134                 return -ENAMETOOLONG;
135         sprintf(output, ":x%02x", ch);
136         return 4;
137 }
138
139 static inline int write_unichar(wchar_t ch, char *output, int olen)
140 {
141         if (olen < 5)
142                 return -ENAMETOOLONG;
143         sprintf(output, ":%04x", ch);
144         return 5;
145 }
146
147 /* convert from one "codepage" to another (possibly being utf8). */
148 static int convert_cp(unsigned char *output, int olen,
149                       const unsigned char *input, int ilen,
150                       struct nls_table *nls_from,
151                       struct nls_table *nls_to)
152 {
153         int len = 0;
154         int n;
155         wchar_t ch;
156
157         while (ilen > 0) {
158                 /* convert by changing to unicode and back to the new cp */
159                 n = nls_from->char2uni(input, ilen, &ch);
160                 if (n == -EINVAL) {
161                         ilen--;
162                         n = write_char(*input++, output, olen);
163                         if (n < 0)
164                                 goto fail;
165                         output += n;
166                         olen -= n;
167                         len += n;
168                         continue;
169                 } else if (n < 0)
170                         goto fail;
171                 input += n;
172                 ilen -= n;
173
174                 n = nls_to->uni2char(ch, output, olen);
175                 if (n == -EINVAL)
176                         n = write_unichar(ch, output, olen);
177                 if (n < 0)
178                         goto fail;
179                 output += n;
180                 olen -= n;
181
182                 len += n;
183         }
184         return len;
185 fail:
186         return n;
187 }
188
189 /* ----------------------------------------------------------- */
190
191 /*
192  * nls_unicode
193  *
194  * This encodes/decodes little endian unicode format
195  */
196
197 static int uni2char(wchar_t uni, unsigned char *out, int boundlen)
198 {
199         if (boundlen < 2)
200                 return -EINVAL;
201         *out++ = uni & 0xff;
202         *out++ = uni >> 8;
203         return 2;
204 }
205
206 static int char2uni(const unsigned char *rawstring, int boundlen, wchar_t *uni)
207 {
208         if (boundlen < 2)
209                 return -EINVAL;
210         *uni = (rawstring[1] << 8) | rawstring[0];
211         return 2;
212 }
213
214 static struct nls_table unicode_table = {
215         .charset        = "unicode",
216         .uni2char       = uni2char,
217         .char2uni       = char2uni,
218 };
219
220 /* ----------------------------------------------------------- */
221
222 static int setcodepage(struct nls_table **p, char *name)
223 {
224         struct nls_table *nls;
225
226         if (!name || !*name) {
227                 nls = NULL;
228         } else if ( (nls = load_nls(name)) == NULL) {
229                 printk (KERN_ERR "smbfs: failed to load nls '%s'\n", name);
230                 return -EINVAL;
231         }
232
233         /* if already set, unload the previous one. */
234         if (*p && *p != &unicode_table)
235                 unload_nls(*p);
236         *p = nls;
237
238         return 0;
239 }
240
241 /* Handles all changes to codepage settings. */
242 int smb_setcodepage(struct smb_sb_info *server, struct smb_nls_codepage *cp)
243 {
244         int n = 0;
245
246         smb_lock_server(server);
247
248         /* Don't load any nls_* at all, if no remote is requested */
249         if (!*cp->remote_name)
250                 goto out;
251
252         /* local */
253         n = setcodepage(&server->local_nls, cp->local_name);
254         if (n != 0)
255                 goto out;
256
257         /* remote */
258         if (!strcmp(cp->remote_name, "unicode")) {
259                 server->remote_nls = &unicode_table;
260         } else {
261                 n = setcodepage(&server->remote_nls, cp->remote_name);
262                 if (n != 0)
263                         setcodepage(&server->local_nls, NULL);
264         }
265
266 out:
267         if (server->local_nls != NULL && server->remote_nls != NULL)
268                 server->ops->convert = convert_cp;
269         else
270                 server->ops->convert = convert_memcpy;
271
272         smb_unlock_server(server);
273         return n;
274 }
275
276
277 /*****************************************************************************/
278 /*                                                                           */
279 /*  Encoding/Decoding section                                                */
280 /*                                                                           */
281 /*****************************************************************************/
282
283 static __u8 *
284 smb_encode_smb_length(__u8 * p, __u32 len)
285 {
286         *p = 0;
287         *(p+1) = 0;
288         *(p+2) = (len & 0xFF00) >> 8;
289         *(p+3) = (len & 0xFF);
290         if (len > 0xFFFF)
291         {
292                 *(p+1) = 1;
293         }
294         return p + 4;
295 }
296
297 /*
298  * smb_build_path: build the path to entry and name storing it in buf.
299  * The path returned will have the trailing '\0'.
300  */
301 static int smb_build_path(struct smb_sb_info *server, unsigned char *buf,
302                           int maxlen,
303                           struct dentry *entry, struct qstr *name)
304 {
305         unsigned char *path = buf;
306         int len;
307         int unicode = (server->mnt->flags & SMB_MOUNT_UNICODE) != 0;
308
309         if (maxlen < (2<<unicode))
310                 return -ENAMETOOLONG;
311
312         if (maxlen > SMB_MAXPATHLEN + 1)
313                 maxlen = SMB_MAXPATHLEN + 1;
314
315         if (entry == NULL)
316                 goto test_name_and_out;
317
318         /*
319          * If IS_ROOT, we have to do no walking at all.
320          */
321         if (IS_ROOT(entry) && !name) {
322                 *path++ = '\\';
323                 if (unicode) *path++ = '\0';
324                 *path++ = '\0';
325                 if (unicode) *path++ = '\0';
326                 return path-buf;
327         }
328
329         /*
330          * Build the path string walking the tree backward from end to ROOT
331          * and store it in reversed order [see reverse_string()]
332          */
333         dget(entry);
334         spin_lock(&entry->d_lock);
335         while (!IS_ROOT(entry)) {
336                 struct dentry *parent;
337
338                 if (maxlen < (3<<unicode)) {
339                         spin_unlock(&entry->d_lock);
340                         dput(entry);
341                         return -ENAMETOOLONG;
342                 }
343
344                 len = server->ops->convert(path, maxlen-2, 
345                                       entry->d_name.name, entry->d_name.len,
346                                       server->local_nls, server->remote_nls);
347                 if (len < 0) {
348                         spin_unlock(&entry->d_lock);
349                         dput(entry);
350                         return len;
351                 }
352                 reverse_string(path, len);
353                 path += len;
354                 if (unicode) {
355                         /* Note: reverse order */
356                         *path++ = '\0';
357                         maxlen--;
358                 }
359                 *path++ = '\\';
360                 maxlen -= len+1;
361
362                 parent = entry->d_parent;
363                 dget(parent);
364                 spin_unlock(&entry->d_lock);
365                 dput(entry);
366                 entry = parent;
367                 spin_lock(&entry->d_lock);
368         }
369         spin_unlock(&entry->d_lock);
370         dput(entry);
371         reverse_string(buf, path-buf);
372
373         /* maxlen has space for at least one char */
374 test_name_and_out:
375         if (name) {
376                 if (maxlen < (3<<unicode))
377                         return -ENAMETOOLONG;
378                 *path++ = '\\';
379                 if (unicode) {
380                         *path++ = '\0';
381                         maxlen--;
382                 }
383                 len = server->ops->convert(path, maxlen-2, 
384                                       name->name, name->len,
385                                       server->local_nls, server->remote_nls);
386                 if (len < 0)
387                         return len;
388                 path += len;
389                 maxlen -= len+1;
390         }
391         /* maxlen has space for at least one char */
392         *path++ = '\0';
393         if (unicode) *path++ = '\0';
394         return path-buf;
395 }
396
397 static int smb_encode_path(struct smb_sb_info *server, char *buf, int maxlen,
398                            struct dentry *dir, struct qstr *name)
399 {
400         int result;
401
402         result = smb_build_path(server, buf, maxlen, dir, name);
403         if (result < 0)
404                 goto out;
405         if (server->opt.protocol <= SMB_PROTOCOL_COREPLUS)
406                 str_upper(buf, result);
407 out:
408         return result;
409 }
410
411 /* encode_path for non-trans2 request SMBs */
412 static int smb_simple_encode_path(struct smb_request *req, char **p,
413                                   struct dentry * entry, struct qstr * name)
414 {
415         struct smb_sb_info *server = req->rq_server;
416         char *s = *p;
417         int res;
418         int maxlen = ((char *)req->rq_buffer + req->rq_bufsize) - s;
419         int unicode = (server->mnt->flags & SMB_MOUNT_UNICODE);
420
421         if (!maxlen)
422                 return -ENAMETOOLONG;
423         *s++ = 4;       /* ASCII data format */
424
425         /*
426          * SMB Unicode strings must be 16bit aligned relative the start of the
427          * packet. If they are not they must be padded with 0.
428          */
429         if (unicode) {
430                 int align = s - (char *)req->rq_buffer;
431                 if (!(align & 1)) {
432                         *s++ = '\0';
433                         maxlen--;
434                 }
435         }
436
437         res = smb_encode_path(server, s, maxlen-1, entry, name);
438         if (res < 0)
439                 return res;
440         *p = s + res;
441         return 0;
442 }
443
444 /* The following are taken directly from msdos-fs */
445
446 /* Linear day numbers of the respective 1sts in non-leap years. */
447
448 static int day_n[] =
449 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 0, 0, 0, 0};
450                   /* JanFebMarApr May Jun Jul Aug Sep Oct Nov Dec */
451
452
453 static time_t
454 utc2local(struct smb_sb_info *server, time_t time)
455 {
456         return time - server->opt.serverzone*60;
457 }
458
459 static time_t
460 local2utc(struct smb_sb_info *server, time_t time)
461 {
462         return time + server->opt.serverzone*60;
463 }
464
465 /* Convert a MS-DOS time/date pair to a UNIX date (seconds since 1 1 70). */
466
467 static time_t
468 date_dos2unix(struct smb_sb_info *server, __u16 date, __u16 time)
469 {
470         int month, year;
471         time_t secs;
472
473         /* first subtract and mask after that... Otherwise, if
474            date == 0, bad things happen */
475         month = ((date >> 5) - 1) & 15;
476         year = date >> 9;
477         secs = (time & 31) * 2 + 60 * ((time >> 5) & 63) + (time >> 11) * 3600 + 86400 *
478             ((date & 31) - 1 + day_n[month] + (year / 4) + year * 365 - ((year & 3) == 0 &&
479                                                    month < 2 ? 1 : 0) + 3653);
480         /* days since 1.1.70 plus 80's leap day */
481         return local2utc(server, secs);
482 }
483
484
485 /* Convert linear UNIX date to a MS-DOS time/date pair. */
486
487 static void
488 date_unix2dos(struct smb_sb_info *server,
489               int unix_date, __u16 *date, __u16 *time)
490 {
491         int day, year, nl_day, month;
492
493         unix_date = utc2local(server, unix_date);
494         if (unix_date < 315532800)
495                 unix_date = 315532800;
496
497         *time = (unix_date % 60) / 2 +
498                 (((unix_date / 60) % 60) << 5) +
499                 (((unix_date / 3600) % 24) << 11);
500
501         day = unix_date / 86400 - 3652;
502         year = day / 365;
503         if ((year + 3) / 4 + 365 * year > day)
504                 year--;
505         day -= (year + 3) / 4 + 365 * year;
506         if (day == 59 && !(year & 3)) {
507                 nl_day = day;
508                 month = 2;
509         } else {
510                 nl_day = (year & 3) || day <= 59 ? day : day - 1;
511                 for (month = 0; month < 12; month++)
512                         if (day_n[month] > nl_day)
513                                 break;
514         }
515         *date = nl_day - day_n[month - 1] + 1 + (month << 5) + (year << 9);
516 }
517
518 /* The following are taken from fs/ntfs/util.c */
519
520 #define NTFS_TIME_OFFSET ((u64)(369*365 + 89) * 24 * 3600 * 10000000)
521
522 /*
523  * Convert the NT UTC (based 1601-01-01, in hundred nanosecond units)
524  * into Unix UTC (based 1970-01-01, in seconds).
525  */
526 static struct timespec
527 smb_ntutc2unixutc(u64 ntutc)
528 {
529         struct timespec ts;
530         /* FIXME: what about the timezone difference? */
531         /* Subtract the NTFS time offset, then convert to 1s intervals. */
532         u64 t = ntutc - NTFS_TIME_OFFSET;
533         ts.tv_nsec = do_div(t, 10000000) * 100;
534         ts.tv_sec = t; 
535         return ts;
536 }
537
538 /* Convert the Unix UTC into NT time */
539 static u64
540 smb_unixutc2ntutc(struct timespec ts)
541 {
542         /* Note: timezone conversion is probably wrong. */
543         /* return ((u64)utc2local(server, t)) * 10000000 + NTFS_TIME_OFFSET; */
544         return ((u64)ts.tv_sec) * 10000000 + ts.tv_nsec/100 + NTFS_TIME_OFFSET;
545 }
546
547 #define MAX_FILE_MODE   6
548 static mode_t file_mode[] = {
549         S_IFREG, S_IFDIR, S_IFLNK, S_IFCHR, S_IFBLK, S_IFIFO, S_IFSOCK
550 };
551
552 static int smb_filetype_to_mode(u32 filetype)
553 {
554         if (filetype > MAX_FILE_MODE) {
555                 PARANOIA("Filetype out of range: %d\n", filetype);
556                 return S_IFREG;
557         }
558         return file_mode[filetype];
559 }
560
561 static u32 smb_filetype_from_mode(int mode)
562 {
563         if (S_ISREG(mode))
564                 return UNIX_TYPE_FILE;
565         if (S_ISDIR(mode))
566                 return UNIX_TYPE_DIR;
567         if (S_ISLNK(mode))
568                 return UNIX_TYPE_SYMLINK;
569         if (S_ISCHR(mode))
570                 return UNIX_TYPE_CHARDEV;
571         if (S_ISBLK(mode))
572                 return UNIX_TYPE_BLKDEV;
573         if (S_ISFIFO(mode))
574                 return UNIX_TYPE_FIFO;
575         if (S_ISSOCK(mode))
576                 return UNIX_TYPE_SOCKET;
577         return UNIX_TYPE_UNKNOWN;
578 }
579
580
581 /*****************************************************************************/
582 /*                                                                           */
583 /*  Support section.                                                         */
584 /*                                                                           */
585 /*****************************************************************************/
586
587 __u32
588 smb_len(__u8 * p)
589 {
590         return ((*(p+1) & 0x1) << 16L) | (*(p+2) << 8L) | *(p+3);
591 }
592
593 static __u16
594 smb_bcc(__u8 * packet)
595 {
596         int pos = SMB_HEADER_LEN + SMB_WCT(packet) * sizeof(__u16);
597         return WVAL(packet, pos);
598 }
599
600 /* smb_valid_packet: We check if packet fulfills the basic
601    requirements of a smb packet */
602
603 static int
604 smb_valid_packet(__u8 * packet)
605 {
606         return (packet[4] == 0xff
607                 && packet[5] == 'S'
608                 && packet[6] == 'M'
609                 && packet[7] == 'B'
610                 && (smb_len(packet) + 4 == SMB_HEADER_LEN
611                     + SMB_WCT(packet) * 2 + smb_bcc(packet)));
612 }
613
614 /* smb_verify: We check if we got the answer we expected, and if we
615    got enough data. If bcc == -1, we don't care. */
616
617 static int
618 smb_verify(__u8 * packet, int command, int wct, int bcc)
619 {
620         if (SMB_CMD(packet) != command)
621                 goto bad_command;
622         if (SMB_WCT(packet) < wct)
623                 goto bad_wct;
624         if (bcc != -1 && smb_bcc(packet) < bcc)
625                 goto bad_bcc;
626         return 0;
627
628 bad_command:
629         printk(KERN_ERR "smb_verify: command=%x, SMB_CMD=%x??\n",
630                command, SMB_CMD(packet));
631         goto fail;
632 bad_wct:
633         printk(KERN_ERR "smb_verify: command=%x, wct=%d, SMB_WCT=%d??\n",
634                command, wct, SMB_WCT(packet));
635         goto fail;
636 bad_bcc:
637         printk(KERN_ERR "smb_verify: command=%x, bcc=%d, SMB_BCC=%d??\n",
638                command, bcc, smb_bcc(packet));
639 fail:
640         return -EIO;
641 }
642
643 /*
644  * Returns the maximum read or write size for the "payload". Making all of the
645  * packet fit within the negotiated max_xmit size.
646  *
647  * N.B. Since this value is usually computed before locking the server,
648  * the server's packet size must never be decreased!
649  */
650 static inline int
651 smb_get_xmitsize(struct smb_sb_info *server, int overhead)
652 {
653         return server->opt.max_xmit - overhead;
654 }
655
656 /*
657  * Calculate the maximum read size
658  */
659 int
660 smb_get_rsize(struct smb_sb_info *server)
661 {
662         /* readX has 12 parameters, read has 5 */
663         int overhead = SMB_HEADER_LEN + 12 * sizeof(__u16) + 2 + 1 + 2;
664         int size = smb_get_xmitsize(server, overhead);
665
666         VERBOSE("xmit=%d, size=%d\n", server->opt.max_xmit, size);
667
668         return size;
669 }
670
671 /*
672  * Calculate the maximum write size
673  */
674 int
675 smb_get_wsize(struct smb_sb_info *server)
676 {
677         /* writeX has 14 parameters, write has 5 */
678         int overhead = SMB_HEADER_LEN + 14 * sizeof(__u16) + 2 + 1 + 2;
679         int size = smb_get_xmitsize(server, overhead);
680
681         VERBOSE("xmit=%d, size=%d\n", server->opt.max_xmit, size);
682
683         return size;
684 }
685
686 /*
687  * Convert SMB error codes to -E... errno values.
688  */
689 int
690 smb_errno(struct smb_request *req)
691 {
692         int errcls = req->rq_rcls;
693         int error  = req->rq_err;
694         char *class = "Unknown";
695
696         VERBOSE("errcls %d  code %d  from command 0x%x\n",
697                 errcls, error, SMB_CMD(req->rq_header));
698
699         if (errcls == ERRDOS) {
700                 switch (error) {
701                 case ERRbadfunc:
702                         return -EINVAL;
703                 case ERRbadfile:
704                 case ERRbadpath:
705                         return -ENOENT;
706                 case ERRnofids:
707                         return -EMFILE;
708                 case ERRnoaccess:
709                         return -EACCES;
710                 case ERRbadfid:
711                         return -EBADF;
712                 case ERRbadmcb:
713                         return -EREMOTEIO;
714                 case ERRnomem:
715                         return -ENOMEM;
716                 case ERRbadmem:
717                         return -EFAULT;
718                 case ERRbadenv:
719                 case ERRbadformat:
720                         return -EREMOTEIO;
721                 case ERRbadaccess:
722                         return -EACCES;
723                 case ERRbaddata:
724                         return -E2BIG;
725                 case ERRbaddrive:
726                         return -ENXIO;
727                 case ERRremcd:
728                         return -EREMOTEIO;
729                 case ERRdiffdevice:
730                         return -EXDEV;
731                 case ERRnofiles:
732                         return -ENOENT;
733                 case ERRbadshare:
734                         return -ETXTBSY;
735                 case ERRlock:
736                         return -EDEADLK;
737                 case ERRfilexists:
738                         return -EEXIST;
739                 case ERROR_INVALID_PARAMETER:
740                         return -EINVAL;
741                 case ERROR_DISK_FULL:
742                         return -ENOSPC;
743                 case ERROR_INVALID_NAME:
744                         return -ENOENT;
745                 case ERROR_DIR_NOT_EMPTY:
746                         return -ENOTEMPTY;
747                 case ERROR_NOT_LOCKED:
748                        return -ENOLCK;
749                 case ERROR_ALREADY_EXISTS:
750                         return -EEXIST;
751                 default:
752                         class = "ERRDOS";
753                         goto err_unknown;
754                 }
755         } else if (errcls == ERRSRV) {
756                 switch (error) {
757                 /* N.B. This is wrong ... EIO ? */
758                 case ERRerror:
759                         return -ENFILE;
760                 case ERRbadpw:
761                         return -EINVAL;
762                 case ERRbadtype:
763                 case ERRtimeout:
764                         return -EIO;
765                 case ERRaccess:
766                         return -EACCES;
767                 /*
768                  * This is a fatal error, as it means the "tree ID"
769                  * for this connection is no longer valid. We map
770                  * to a special error code and get a new connection.
771                  */
772                 case ERRinvnid:
773                         return -EBADSLT;
774                 default:
775                         class = "ERRSRV";
776                         goto err_unknown;
777                 }
778         } else if (errcls == ERRHRD) {
779                 switch (error) {
780                 case ERRnowrite:
781                         return -EROFS;
782                 case ERRbadunit:
783                         return -ENODEV;
784                 case ERRnotready:
785                         return -EUCLEAN;
786                 case ERRbadcmd:
787                 case ERRdata:
788                         return -EIO;
789                 case ERRbadreq:
790                         return -ERANGE;
791                 case ERRbadshare:
792                         return -ETXTBSY;
793                 case ERRlock:
794                         return -EDEADLK;
795                 case ERRdiskfull:
796                         return -ENOSPC;
797                 default:
798                         class = "ERRHRD";
799                         goto err_unknown;
800                 }
801         } else if (errcls == ERRCMD) {
802                 class = "ERRCMD";
803         } else if (errcls == SUCCESS) {
804                 return 0;       /* This is the only valid 0 return */
805         }
806
807 err_unknown:
808         printk(KERN_ERR "smb_errno: class %s, code %d from command 0x%x\n",
809                class, error, SMB_CMD(req->rq_header));
810         return -EIO;
811 }
812
813 /* smb_request_ok: We expect the server to be locked. Then we do the
814    request and check the answer completely. When smb_request_ok
815    returns 0, you can be quite sure that everything went well. When
816    the answer is <=0, the returned number is a valid unix errno. */
817
818 static int
819 smb_request_ok(struct smb_request *req, int command, int wct, int bcc)
820 {
821         int result;
822
823         req->rq_resp_wct = wct;
824         req->rq_resp_bcc = bcc;
825
826         result = smb_add_request(req);
827         if (result != 0) {
828                 DEBUG1("smb_request failed\n");
829                 goto out;
830         }
831
832         if (smb_valid_packet(req->rq_header) != 0) {
833                 PARANOIA("invalid packet!\n");
834                 goto out;
835         }
836
837         result = smb_verify(req->rq_header, command, wct, bcc);
838
839 out:
840         return result;
841 }
842
843 /*
844  * This implements the NEWCONN ioctl. It installs the server pid,
845  * sets server->state to CONN_VALID, and wakes up the waiting process.
846  */
847 int
848 smb_newconn(struct smb_sb_info *server, struct smb_conn_opt *opt)
849 {
850         struct file *filp;
851         struct sock *sk;
852         int error;
853
854         VERBOSE("fd=%d, pid=%d\n", opt->fd, current->pid);
855
856         smb_lock_server(server);
857
858         /*
859          * Make sure we don't already have a valid connection ...
860          */
861         error = -EINVAL;
862         if (server->state == CONN_VALID)
863                 goto out;
864
865         error = -EACCES;
866         if (current->uid != server->mnt->mounted_uid && 
867             !capable(CAP_SYS_ADMIN))
868                 goto out;
869
870         error = -EBADF;
871         filp = fget(opt->fd);
872         if (!filp)
873                 goto out;
874         if (!smb_valid_socket(filp->f_dentry->d_inode))
875                 goto out_putf;
876
877         server->sock_file = filp;
878         server->conn_pid = current->pid;
879         server->opt = *opt;
880         server->generation += 1;
881         server->state = CONN_VALID;
882         error = 0;
883
884         if (server->conn_error) {
885                 /*
886                  * conn_error is the returncode we originally decided to
887                  * drop the old connection on. This message should be positive
888                  * and not make people ask questions on why smbfs is printing
889                  * error messages ...
890                  */
891                 printk(KERN_INFO "SMB connection re-established (%d)\n",
892                        server->conn_error);
893                 server->conn_error = 0;
894         }
895
896         /*
897          * Store the server in sock user_data (Only used by sunrpc)
898          */
899         sk = SOCKET_I(filp->f_dentry->d_inode)->sk;
900         sk->sk_user_data = server;
901
902         /* chain into the data_ready callback */
903         server->data_ready = xchg(&sk->sk_data_ready, smb_data_ready);
904
905         /* check if we have an old smbmount that uses seconds for the 
906            serverzone */
907         if (server->opt.serverzone > 12*60 || server->opt.serverzone < -12*60)
908                 server->opt.serverzone /= 60;
909
910         /* now that we have an established connection we can detect the server
911            type and enable bug workarounds */
912         if (server->opt.protocol < SMB_PROTOCOL_LANMAN2)
913                 install_ops(server->ops, &smb_ops_core);
914         else if (server->opt.protocol == SMB_PROTOCOL_LANMAN2)
915                 install_ops(server->ops, &smb_ops_os2);
916         else if (server->opt.protocol == SMB_PROTOCOL_NT1 &&
917                  (server->opt.max_xmit < 0x1000) &&
918                  !(server->opt.capabilities & SMB_CAP_NT_SMBS)) {
919                 /* FIXME: can we kill the WIN95 flag now? */
920                 server->mnt->flags |= SMB_MOUNT_WIN95;
921                 VERBOSE("detected WIN95 server\n");
922                 install_ops(server->ops, &smb_ops_win95);
923         } else {
924                 /*
925                  * Samba has max_xmit 65535
926                  * NT4spX has max_xmit 4536 (or something like that)
927                  * win2k has ...
928                  */
929                 VERBOSE("detected NT1 (Samba, NT4/5) server\n");
930                 install_ops(server->ops, &smb_ops_winNT);
931         }
932
933         /* FIXME: the win9x code wants to modify these ... (seek/trunc bug) */
934         if (server->mnt->flags & SMB_MOUNT_OLDATTR) {
935                 server->ops->getattr = smb_proc_getattr_core;
936         } else if (server->mnt->flags & SMB_MOUNT_DIRATTR) {
937                 server->ops->getattr = smb_proc_getattr_ff;
938         }
939
940         /* Decode server capabilities */
941         if (server->opt.capabilities & SMB_CAP_LARGE_FILES) {
942                 /* Should be ok to set this now, as no one can access the
943                    mount until the connection has been established. */
944                 SB_of(server)->s_maxbytes = ~0ULL >> 1;
945                 VERBOSE("LFS enabled\n");
946         }
947         if (server->opt.capabilities & SMB_CAP_UNICODE) {
948                 server->mnt->flags |= SMB_MOUNT_UNICODE;
949                 VERBOSE("Unicode enabled\n");
950         } else {
951                 server->mnt->flags &= ~SMB_MOUNT_UNICODE;
952         }
953 #if 0
954         /* flags we may test for other patches ... */
955         if (server->opt.capabilities & SMB_CAP_LARGE_READX) {
956                 VERBOSE("Large reads enabled\n");
957         }
958         if (server->opt.capabilities & SMB_CAP_LARGE_WRITEX) {
959                 VERBOSE("Large writes enabled\n");
960         }
961 #endif
962         if (server->opt.capabilities & SMB_CAP_UNIX) {
963                 struct inode *inode;
964                 VERBOSE("Using UNIX CIFS extensions\n");
965                 install_ops(server->ops, &smb_ops_unix);
966                 inode = SB_of(server)->s_root->d_inode;
967                 if (inode)
968                         inode->i_op = &smb_dir_inode_operations_unix;
969         }
970
971         VERBOSE("protocol=%d, max_xmit=%d, pid=%d capabilities=0x%x\n",
972                 server->opt.protocol, server->opt.max_xmit, server->conn_pid,
973                 server->opt.capabilities);
974
975         /* FIXME: this really should be done by smbmount. */
976         if (server->opt.max_xmit > SMB_MAX_PACKET_SIZE) {
977                 server->opt.max_xmit = SMB_MAX_PACKET_SIZE;
978         }
979
980         smb_unlock_server(server);
981         smbiod_wake_up();
982         if (server->opt.capabilities & SMB_CAP_UNIX)
983                 smb_proc_query_cifsunix(server);
984         return error;
985
986 out:
987         smb_unlock_server(server);
988         smbiod_wake_up();
989         return error;
990
991 out_putf:
992         fput(filp);
993         goto out;
994 }
995
996 /* smb_setup_header: We completely set up the packet. You only have to
997    insert the command-specific fields */
998
999 __u8 *
1000 smb_setup_header(struct smb_request *req, __u8 command, __u16 wct, __u16 bcc)
1001 {
1002         __u32 xmit_len = SMB_HEADER_LEN + wct * sizeof(__u16) + bcc + 2;
1003         __u8 *p = req->rq_header;
1004         struct smb_sb_info *server = req->rq_server;
1005
1006         p = smb_encode_smb_length(p, xmit_len - 4);
1007
1008         *p++ = 0xff;
1009         *p++ = 'S';
1010         *p++ = 'M';
1011         *p++ = 'B';
1012         *p++ = command;
1013
1014         memset(p, '\0', 19);
1015         p += 19;
1016         p += 8;
1017
1018         if (server->opt.protocol > SMB_PROTOCOL_CORE) {
1019                 int flags = SMB_FLAGS_CASELESS_PATHNAMES;
1020                 int flags2 = SMB_FLAGS2_LONG_PATH_COMPONENTS |
1021                         SMB_FLAGS2_EXTENDED_ATTRIBUTES; /* EA? not really ... */
1022
1023                 *(req->rq_header + smb_flg) = flags;
1024                 if (server->mnt->flags & SMB_MOUNT_UNICODE)
1025                         flags2 |= SMB_FLAGS2_UNICODE_STRINGS;
1026                 WSET(req->rq_header, smb_flg2, flags2);
1027         }
1028         *p++ = wct;             /* wct */
1029         p += 2 * wct;
1030         WSET(p, 0, bcc);
1031
1032         /* Include the header in the data to send */
1033         req->rq_iovlen = 1;
1034         req->rq_iov[0].iov_base = req->rq_header;
1035         req->rq_iov[0].iov_len  = xmit_len - bcc;
1036
1037         return req->rq_buffer;
1038 }
1039
1040 static void
1041 smb_setup_bcc(struct smb_request *req, __u8 *p)
1042 {
1043         u16 bcc = p - req->rq_buffer;
1044         u8 *pbcc = req->rq_header + SMB_HEADER_LEN + 2*SMB_WCT(req->rq_header);
1045
1046         WSET(pbcc, 0, bcc);
1047
1048         smb_encode_smb_length(req->rq_header, SMB_HEADER_LEN + 
1049                               2*SMB_WCT(req->rq_header) - 2 + bcc);
1050
1051         /* Include the "bytes" in the data to send */
1052         req->rq_iovlen = 2;
1053         req->rq_iov[1].iov_base = req->rq_buffer;
1054         req->rq_iov[1].iov_len  = bcc;
1055 }
1056
1057 static int
1058 smb_proc_seek(struct smb_sb_info *server, __u16 fileid,
1059               __u16 mode, off_t offset)
1060 {
1061         int result;
1062         struct smb_request *req;
1063
1064         result = -ENOMEM;
1065         if (! (req = smb_alloc_request(server, 0)))
1066                 goto out;
1067
1068         smb_setup_header(req, SMBlseek, 4, 0);
1069         WSET(req->rq_header, smb_vwv0, fileid);
1070         WSET(req->rq_header, smb_vwv1, mode);
1071         DSET(req->rq_header, smb_vwv2, offset);
1072         req->rq_flags |= SMB_REQ_NORETRY;
1073
1074         result = smb_request_ok(req, SMBlseek, 2, 0);
1075         if (result < 0) {
1076                 result = 0;
1077                 goto out_free;
1078         }
1079
1080         result = DVAL(req->rq_header, smb_vwv0);
1081 out_free:
1082         smb_rput(req);
1083 out:
1084         return result;
1085 }
1086
1087 static int
1088 smb_proc_open(struct smb_sb_info *server, struct dentry *dentry, int wish)
1089 {
1090         struct inode *ino = dentry->d_inode;
1091         struct smb_inode_info *ei = SMB_I(ino);
1092         int mode, read_write = 0x42, read_only = 0x40;
1093         int res;
1094         char *p;
1095         struct smb_request *req;
1096
1097         /*
1098          * Attempt to open r/w, unless there are no write privileges.
1099          */
1100         mode = read_write;
1101         if (!(ino->i_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1102                 mode = read_only;
1103 #if 0
1104         /* FIXME: why is this code not in? below we fix it so that a caller
1105            wanting RO doesn't get RW. smb_revalidate_inode does some 
1106            optimization based on access mode. tail -f needs it to be correct.
1107
1108            We must open rw since we don't do the open if called a second time
1109            with different 'wish'. Is that not supported by smb servers? */
1110         if (!(wish & (O_WRONLY | O_RDWR)))
1111                 mode = read_only;
1112 #endif
1113
1114         res = -ENOMEM;
1115         if (! (req = smb_alloc_request(server, PAGE_SIZE)))
1116                 goto out;
1117
1118       retry:
1119         p = smb_setup_header(req, SMBopen, 2, 0);
1120         WSET(req->rq_header, smb_vwv0, mode);
1121         WSET(req->rq_header, smb_vwv1, aSYSTEM | aHIDDEN | aDIR);
1122         res = smb_simple_encode_path(req, &p, dentry, NULL);
1123         if (res < 0)
1124                 goto out_free;
1125         smb_setup_bcc(req, p);
1126
1127         res = smb_request_ok(req, SMBopen, 7, 0);
1128         if (res != 0) {
1129                 if (mode == read_write &&
1130                     (res == -EACCES || res == -ETXTBSY || res == -EROFS))
1131                 {
1132                         VERBOSE("%s/%s R/W failed, error=%d, retrying R/O\n",
1133                                 DENTRY_PATH(dentry), res);
1134                         mode = read_only;
1135                         req->rq_flags = 0;
1136                         goto retry;
1137                 }
1138                 goto out_free;
1139         }
1140         /* We should now have data in vwv[0..6]. */
1141
1142         ei->fileid = WVAL(req->rq_header, smb_vwv0);
1143         ei->attr   = WVAL(req->rq_header, smb_vwv1);
1144         /* smb_vwv2 has mtime */
1145         /* smb_vwv4 has size  */
1146         ei->access = (WVAL(req->rq_header, smb_vwv6) & SMB_ACCMASK);
1147         ei->open = server->generation;
1148
1149 out_free:
1150         smb_rput(req);
1151 out:
1152         return res;
1153 }
1154
1155 /*
1156  * Make sure the file is open, and check that the access
1157  * is compatible with the desired access.
1158  */
1159 int
1160 smb_open(struct dentry *dentry, int wish)
1161 {
1162         struct inode *inode = dentry->d_inode;
1163         int result;
1164         __u16 access;
1165
1166         result = -ENOENT;
1167         if (!inode) {
1168                 printk(KERN_ERR "smb_open: no inode for dentry %s/%s\n",
1169                        DENTRY_PATH(dentry));
1170                 goto out;
1171         }
1172
1173         if (!smb_is_open(inode)) {
1174                 struct smb_sb_info *server = server_from_inode(inode);
1175                 result = 0;
1176                 if (!smb_is_open(inode))
1177                         result = smb_proc_open(server, dentry, wish);
1178                 if (result)
1179                         goto out;
1180                 /*
1181                  * A successful open means the path is still valid ...
1182                  */
1183                 smb_renew_times(dentry);
1184         }
1185
1186         /*
1187          * Check whether the access is compatible with the desired mode.
1188          */
1189         result = 0;
1190         access = SMB_I(inode)->access;
1191         if (access != wish && access != SMB_O_RDWR) {
1192                 PARANOIA("%s/%s access denied, access=%x, wish=%x\n",
1193                          DENTRY_PATH(dentry), access, wish);
1194                 result = -EACCES;
1195         }
1196 out:
1197         return result;
1198 }
1199
1200 static int 
1201 smb_proc_close(struct smb_sb_info *server, __u16 fileid, __u32 mtime)
1202 {
1203         struct smb_request *req;
1204         int result = -ENOMEM;
1205
1206         if (! (req = smb_alloc_request(server, 0)))
1207                 goto out;
1208
1209         smb_setup_header(req, SMBclose, 3, 0);
1210         WSET(req->rq_header, smb_vwv0, fileid);
1211         DSET(req->rq_header, smb_vwv1, utc2local(server, mtime));
1212         req->rq_flags |= SMB_REQ_NORETRY;
1213         result = smb_request_ok(req, SMBclose, 0, 0);
1214
1215         smb_rput(req);
1216 out:
1217         return result;
1218 }
1219
1220 /*
1221  * Win NT 4.0 has an apparent bug in that it fails to update the
1222  * modify time when writing to a file. As a workaround, we update
1223  * both modify and access time locally, and post the times to the
1224  * server when closing the file.
1225  */
1226 static int 
1227 smb_proc_close_inode(struct smb_sb_info *server, struct inode * ino)
1228 {
1229         struct smb_inode_info *ei = SMB_I(ino);
1230         int result = 0;
1231         if (smb_is_open(ino))
1232         {
1233                 /*
1234                  * We clear the open flag in advance, in case another
1235                  * process observes the value while we block below.
1236                  */
1237                 ei->open = 0;
1238
1239                 /*
1240                  * Kludge alert: SMB timestamps are accurate only to
1241                  * two seconds ... round the times to avoid needless
1242                  * cache invalidations!
1243                  */
1244                 if (ino->i_mtime.tv_sec & 1) { 
1245                         ino->i_mtime.tv_sec--;
1246                         ino->i_mtime.tv_nsec = 0; 
1247                 }
1248                 if (ino->i_atime.tv_sec & 1) {
1249                         ino->i_atime.tv_sec--;
1250                         ino->i_atime.tv_nsec = 0;
1251                 }
1252                 /*
1253                  * If the file is open with write permissions,
1254                  * update the time stamps to sync mtime and atime.
1255                  */
1256                 if ((server->opt.capabilities & SMB_CAP_UNIX) == 0 &&
1257                     (server->opt.protocol >= SMB_PROTOCOL_LANMAN2) &&
1258                     !(ei->access == SMB_O_RDONLY))
1259                 {
1260                         struct smb_fattr fattr;
1261                         smb_get_inode_attr(ino, &fattr);
1262                         smb_proc_setattr_ext(server, ino, &fattr);
1263                 }
1264
1265                 result = smb_proc_close(server, ei->fileid, ino->i_mtime.tv_sec);
1266                 /*
1267                  * Force a revalidation after closing ... some servers
1268                  * don't post the size until the file has been closed.
1269                  */
1270                 if (server->opt.protocol < SMB_PROTOCOL_NT1)
1271                         ei->oldmtime = 0;
1272                 ei->closed = jiffies;
1273         }
1274         return result;
1275 }
1276
1277 int
1278 smb_close(struct inode *ino)
1279 {
1280         int result = 0;
1281
1282         if (smb_is_open(ino)) {
1283                 struct smb_sb_info *server = server_from_inode(ino);
1284                 result = smb_proc_close_inode(server, ino);
1285         }
1286         return result;
1287 }
1288
1289 /*
1290  * This is used to close a file following a failed instantiate.
1291  * Since we don't have an inode, we can't use any of the above.
1292  */
1293 int
1294 smb_close_fileid(struct dentry *dentry, __u16 fileid)
1295 {
1296         struct smb_sb_info *server = server_from_dentry(dentry);
1297         int result;
1298
1299         result = smb_proc_close(server, fileid, get_seconds());
1300         return result;
1301 }
1302
1303 /* In smb_proc_read and smb_proc_write we do not retry, because the
1304    file-id would not be valid after a reconnection. */
1305
1306 static void
1307 smb_proc_read_data(struct smb_request *req)
1308 {
1309         req->rq_iov[0].iov_base = req->rq_buffer;
1310         req->rq_iov[0].iov_len  = 3;
1311
1312         req->rq_iov[1].iov_base = req->rq_page;
1313         req->rq_iov[1].iov_len  = req->rq_rsize;
1314         req->rq_iovlen = 2;
1315
1316         req->rq_rlen = smb_len(req->rq_header) + 4 - req->rq_bytes_recvd;
1317 }
1318
1319 static int
1320 smb_proc_read(struct inode *inode, loff_t offset, int count, char *data)
1321 {
1322         struct smb_sb_info *server = server_from_inode(inode);
1323         __u16 returned_count, data_len;
1324         unsigned char *buf;
1325         int result;
1326         struct smb_request *req;
1327         u8 rbuf[4];
1328
1329         result = -ENOMEM;
1330         if (! (req = smb_alloc_request(server, 0)))
1331                 goto out;
1332
1333         smb_setup_header(req, SMBread, 5, 0);
1334         buf = req->rq_header;
1335         WSET(buf, smb_vwv0, SMB_I(inode)->fileid);
1336         WSET(buf, smb_vwv1, count);
1337         DSET(buf, smb_vwv2, offset);
1338         WSET(buf, smb_vwv4, 0);
1339
1340         req->rq_page = data;
1341         req->rq_rsize = count;
1342         req->rq_callback = smb_proc_read_data;
1343         req->rq_buffer = rbuf;
1344         req->rq_flags |= SMB_REQ_NORETRY | SMB_REQ_STATIC;
1345
1346         result = smb_request_ok(req, SMBread, 5, -1);
1347         if (result < 0)
1348                 goto out_free;
1349         returned_count = WVAL(req->rq_header, smb_vwv0);
1350
1351         data_len = WVAL(rbuf, 1);
1352
1353         if (returned_count != data_len) {
1354                 printk(KERN_NOTICE "smb_proc_read: returned != data_len\n");
1355                 printk(KERN_NOTICE "smb_proc_read: ret_c=%d, data_len=%d\n",
1356                        returned_count, data_len);
1357         }
1358         result = data_len;
1359
1360 out_free:
1361         smb_rput(req);
1362 out:
1363         VERBOSE("ino=%ld, fileid=%d, count=%d, result=%d\n",
1364                 inode->i_ino, SMB_I(inode)->fileid, count, result);
1365         return result;
1366 }
1367
1368 static int
1369 smb_proc_write(struct inode *inode, loff_t offset, int count, const char *data)
1370 {
1371         struct smb_sb_info *server = server_from_inode(inode);
1372         int result;
1373         u16 fileid = SMB_I(inode)->fileid;
1374         u8 buf[4];
1375         struct smb_request *req;
1376
1377         result = -ENOMEM;
1378         if (! (req = smb_alloc_request(server, 0)))
1379                 goto out;
1380
1381         VERBOSE("ino=%ld, fileid=%d, count=%d@%Ld\n",
1382                 inode->i_ino, fileid, count, offset);
1383
1384         smb_setup_header(req, SMBwrite, 5, count + 3);
1385         WSET(req->rq_header, smb_vwv0, fileid);
1386         WSET(req->rq_header, smb_vwv1, count);
1387         DSET(req->rq_header, smb_vwv2, offset);
1388         WSET(req->rq_header, smb_vwv4, 0);
1389
1390         buf[0] = 1;
1391         WSET(buf, 1, count);    /* yes, again ... */
1392         req->rq_iov[1].iov_base = buf;
1393         req->rq_iov[1].iov_len = 3;
1394         req->rq_iov[2].iov_base = (char *) data;
1395         req->rq_iov[2].iov_len = count;
1396         req->rq_iovlen = 3;
1397         req->rq_flags |= SMB_REQ_NORETRY;
1398
1399         result = smb_request_ok(req, SMBwrite, 1, 0);
1400         if (result >= 0)
1401                 result = WVAL(req->rq_header, smb_vwv0);
1402
1403         smb_rput(req);
1404 out:
1405         return result;
1406 }
1407
1408 /*
1409  * In smb_proc_readX and smb_proc_writeX we do not retry, because the
1410  * file-id would not be valid after a reconnection.
1411  */
1412
1413 #define SMB_READX_MAX_PAD      64
1414 static void
1415 smb_proc_readX_data(struct smb_request *req)
1416 {
1417         /* header length, excluding the netbios length (-4) */
1418         int hdrlen = SMB_HEADER_LEN + req->rq_resp_wct*2 - 2;
1419         int data_off = WVAL(req->rq_header, smb_vwv6);
1420
1421         /*
1422          * Some genius made the padding to the data bytes arbitrary.
1423          * So we must first calculate the amount of padding used by the server.
1424          */
1425         data_off -= hdrlen;
1426         if (data_off > SMB_READX_MAX_PAD) {
1427                 PARANOIA("offset is larger than max pad!\n");
1428                 PARANOIA("%d > %d\n", data_off, SMB_READX_MAX_PAD);
1429                 req->rq_rlen = req->rq_bufsize + 1;
1430                 return;
1431         }
1432         req->rq_iov[0].iov_base = req->rq_buffer;
1433         req->rq_iov[0].iov_len  = data_off;
1434
1435         req->rq_iov[1].iov_base = req->rq_page;
1436         req->rq_iov[1].iov_len  = req->rq_rsize;
1437         req->rq_iovlen = 2;
1438
1439         req->rq_rlen = smb_len(req->rq_header) + 4 - req->rq_bytes_recvd;
1440 }
1441
1442 static int
1443 smb_proc_readX(struct inode *inode, loff_t offset, int count, char *data)
1444 {
1445         struct smb_sb_info *server = server_from_inode(inode);
1446         unsigned char *buf;
1447         int result;
1448         struct smb_request *req;
1449         static char pad[SMB_READX_MAX_PAD];
1450
1451         result = -ENOMEM;
1452         if (! (req = smb_alloc_request(server, 0)))
1453                 goto out;
1454
1455         smb_setup_header(req, SMBreadX, 12, 0);
1456         buf = req->rq_header;
1457         WSET(buf, smb_vwv0, 0x00ff);
1458         WSET(buf, smb_vwv1, 0);
1459         WSET(buf, smb_vwv2, SMB_I(inode)->fileid);
1460         DSET(buf, smb_vwv3, (u32)offset);               /* low 32 bits */
1461         WSET(buf, smb_vwv5, count);
1462         WSET(buf, smb_vwv6, 0);
1463         DSET(buf, smb_vwv7, 0);
1464         WSET(buf, smb_vwv9, 0);
1465         DSET(buf, smb_vwv10, (u32)(offset >> 32));      /* high 32 bits */
1466         WSET(buf, smb_vwv11, 0);
1467
1468         req->rq_page = data;
1469         req->rq_rsize = count;
1470         req->rq_callback = smb_proc_readX_data;
1471         req->rq_buffer = pad;
1472         req->rq_bufsize = SMB_READX_MAX_PAD;
1473         req->rq_flags |= SMB_REQ_STATIC | SMB_REQ_NORETRY;
1474
1475         result = smb_request_ok(req, SMBreadX, 12, -1);
1476         if (result < 0)
1477                 goto out_free;
1478         result = WVAL(req->rq_header, smb_vwv5);
1479
1480 out_free:
1481         smb_rput(req);
1482 out:
1483         VERBOSE("ino=%ld, fileid=%d, count=%d, result=%d\n",
1484                 inode->i_ino, SMB_I(inode)->fileid, count, result);
1485         return result;
1486 }
1487
1488 static int
1489 smb_proc_writeX(struct inode *inode, loff_t offset, int count, const char *data)
1490 {
1491         struct smb_sb_info *server = server_from_inode(inode);
1492         int result;
1493         u8 *p;
1494         static u8 pad[4];
1495         struct smb_request *req;
1496
1497         result = -ENOMEM;
1498         if (! (req = smb_alloc_request(server, 0)))
1499                 goto out;
1500
1501         VERBOSE("ino=%ld, fileid=%d, count=%d@%Ld\n",
1502                 inode->i_ino, SMB_I(inode)->fileid, count, offset);
1503
1504         p = smb_setup_header(req, SMBwriteX, 14, count + 1);
1505         WSET(req->rq_header, smb_vwv0, 0x00ff);
1506         WSET(req->rq_header, smb_vwv1, 0);
1507         WSET(req->rq_header, smb_vwv2, SMB_I(inode)->fileid);
1508         DSET(req->rq_header, smb_vwv3, (u32)offset);    /* low 32 bits */
1509         DSET(req->rq_header, smb_vwv5, 0);
1510         WSET(req->rq_header, smb_vwv7, 0);              /* write mode */
1511         WSET(req->rq_header, smb_vwv8, 0);
1512         WSET(req->rq_header, smb_vwv9, 0);
1513         WSET(req->rq_header, smb_vwv10, count);         /* data length */
1514         WSET(req->rq_header, smb_vwv11, smb_vwv12 + 2 + 1);
1515         DSET(req->rq_header, smb_vwv12, (u32)(offset >> 32));
1516
1517         req->rq_iov[1].iov_base = pad;
1518         req->rq_iov[1].iov_len = 1;
1519         req->rq_iov[2].iov_base = (char *) data;
1520         req->rq_iov[2].iov_len = count;
1521         req->rq_iovlen = 3;
1522         req->rq_flags |= SMB_REQ_NORETRY;
1523
1524         result = smb_request_ok(req, SMBwriteX, 6, 0);
1525         if (result >= 0)
1526                 result = WVAL(req->rq_header, smb_vwv2);
1527
1528         smb_rput(req);
1529 out:
1530         return result;
1531 }
1532
1533 int
1534 smb_proc_create(struct dentry *dentry, __u16 attr, time_t ctime, __u16 *fileid)
1535 {
1536         struct smb_sb_info *server = server_from_dentry(dentry);
1537         char *p;
1538         int result;
1539         struct smb_request *req;
1540
1541         result = -ENOMEM;
1542         if (! (req = smb_alloc_request(server, PAGE_SIZE)))
1543                 goto out;
1544
1545         p = smb_setup_header(req, SMBcreate, 3, 0);
1546         WSET(req->rq_header, smb_vwv0, attr);
1547         DSET(req->rq_header, smb_vwv1, utc2local(server, ctime));
1548         result = smb_simple_encode_path(req, &p, dentry, NULL);
1549         if (result < 0)
1550                 goto out_free;
1551         smb_setup_bcc(req, p);
1552
1553         result = smb_request_ok(req, SMBcreate, 1, 0);
1554         if (result < 0)
1555                 goto out_free;
1556
1557         *fileid = WVAL(req->rq_header, smb_vwv0);
1558         result = 0;
1559
1560 out_free:
1561         smb_rput(req);
1562 out:
1563         return result;
1564 }
1565
1566 int
1567 smb_proc_mv(struct dentry *old_dentry, struct dentry *new_dentry)
1568 {
1569         struct smb_sb_info *server = server_from_dentry(old_dentry);
1570         char *p;
1571         int result;
1572         struct smb_request *req;
1573
1574         result = -ENOMEM;
1575         if (! (req = smb_alloc_request(server, PAGE_SIZE)))
1576                 goto out;
1577
1578         p = smb_setup_header(req, SMBmv, 1, 0);
1579         WSET(req->rq_header, smb_vwv0, aSYSTEM | aHIDDEN | aDIR);
1580         result = smb_simple_encode_path(req, &p, old_dentry, NULL);
1581         if (result < 0)
1582                 goto out_free;
1583         result = smb_simple_encode_path(req, &p, new_dentry, NULL);
1584         if (result < 0)
1585                 goto out_free;
1586         smb_setup_bcc(req, p);
1587
1588         if ((result = smb_request_ok(req, SMBmv, 0, 0)) < 0)
1589                 goto out_free;
1590         result = 0;
1591
1592 out_free:
1593         smb_rput(req);
1594 out:
1595         return result;
1596 }
1597
1598 /*
1599  * Code common to mkdir and rmdir.
1600  */
1601 static int
1602 smb_proc_generic_command(struct dentry *dentry, __u8 command)
1603 {
1604         struct smb_sb_info *server = server_from_dentry(dentry);
1605         char *p;
1606         int result;
1607         struct smb_request *req;
1608
1609         result = -ENOMEM;
1610         if (! (req = smb_alloc_request(server, PAGE_SIZE)))
1611                 goto out;
1612
1613         p = smb_setup_header(req, command, 0, 0);
1614         result = smb_simple_encode_path(req, &p, dentry, NULL);
1615         if (result < 0)
1616                 goto out_free;
1617         smb_setup_bcc(req, p);
1618
1619         result = smb_request_ok(req, command, 0, 0);
1620         if (result < 0)
1621                 goto out_free;
1622         result = 0;
1623
1624 out_free:
1625         smb_rput(req);
1626 out:
1627         return result;
1628 }
1629
1630 int
1631 smb_proc_mkdir(struct dentry *dentry)
1632 {
1633         return smb_proc_generic_command(dentry, SMBmkdir);
1634 }
1635
1636 int
1637 smb_proc_rmdir(struct dentry *dentry)
1638 {
1639         return smb_proc_generic_command(dentry, SMBrmdir);
1640 }
1641
1642 #if SMBFS_POSIX_UNLINK
1643 /*
1644  * Removes readonly attribute from a file. Used by unlink to give posix
1645  * semantics.
1646  */
1647 static int
1648 smb_set_rw(struct dentry *dentry,struct smb_sb_info *server)
1649 {
1650         int result;
1651         struct smb_fattr fattr;
1652
1653         /* FIXME: cifsUE should allow removing a readonly file. */
1654
1655         /* first get current attribute */
1656         smb_init_dirent(server, &fattr);
1657         result = server->ops->getattr(server, dentry, &fattr);
1658         smb_finish_dirent(server, &fattr);
1659         if (result < 0)
1660                 return result;
1661
1662         /* if RONLY attribute is set, remove it */
1663         if (fattr.attr & aRONLY) {  /* read only attribute is set */
1664                 fattr.attr &= ~aRONLY;
1665                 result = smb_proc_setattr_core(server, dentry, fattr.attr);
1666         }
1667         return result;
1668 }
1669 #endif
1670
1671 int
1672 smb_proc_unlink(struct dentry *dentry)
1673 {
1674         struct smb_sb_info *server = server_from_dentry(dentry);
1675         int flag = 0;
1676         char *p;
1677         int result;
1678         struct smb_request *req;
1679
1680         result = -ENOMEM;
1681         if (! (req = smb_alloc_request(server, PAGE_SIZE)))
1682                 goto out;
1683
1684       retry:
1685         p = smb_setup_header(req, SMBunlink, 1, 0);
1686         WSET(req->rq_header, smb_vwv0, aSYSTEM | aHIDDEN);
1687         result = smb_simple_encode_path(req, &p, dentry, NULL);
1688         if (result < 0)
1689                 goto out_free;
1690         smb_setup_bcc(req, p);
1691
1692         if ((result = smb_request_ok(req, SMBunlink, 0, 0)) < 0) {
1693 #if SMBFS_POSIX_UNLINK
1694                 if (result == -EACCES && !flag) {
1695                         /* Posix semantics is for the read-only state
1696                            of a file to be ignored in unlink(). In the
1697                            SMB world a unlink() is refused on a
1698                            read-only file. To make things easier for
1699                            unix users we try to override the files
1700                            permission if the unlink fails with the
1701                            right error.
1702                            This introduces a race condition that could
1703                            lead to a file being written by someone who
1704                            shouldn't have access, but as far as I can
1705                            tell that is unavoidable */
1706
1707                         /* remove RONLY attribute and try again */
1708                         result = smb_set_rw(dentry,server);
1709                         if (result == 0) {
1710                                 flag = 1;
1711                                 req->rq_flags = 0;
1712                                 goto retry;
1713                         }
1714                 }
1715 #endif
1716                 goto out_free;
1717         }
1718         result = 0;
1719
1720 out_free:
1721         smb_rput(req);
1722 out:
1723         return result;
1724 }
1725
1726 int
1727 smb_proc_flush(struct smb_sb_info *server, __u16 fileid)
1728 {
1729         int result;
1730         struct smb_request *req;
1731
1732         result = -ENOMEM;
1733         if (! (req = smb_alloc_request(server, 0)))
1734                 goto out;
1735
1736         smb_setup_header(req, SMBflush, 1, 0);
1737         WSET(req->rq_header, smb_vwv0, fileid);
1738         req->rq_flags |= SMB_REQ_NORETRY;
1739         result = smb_request_ok(req, SMBflush, 0, 0);
1740
1741         smb_rput(req);
1742 out:
1743         return result;
1744 }
1745
1746 static int
1747 smb_proc_trunc32(struct inode *inode, loff_t length)
1748 {
1749         /*
1750          * Writing 0bytes is old-SMB magic for truncating files.
1751          * MAX_NON_LFS should prevent this from being called with a too
1752          * large offset.
1753          */
1754         return smb_proc_write(inode, length, 0, NULL);
1755 }
1756
1757 static int
1758 smb_proc_trunc64(struct inode *inode, loff_t length)
1759 {
1760         struct smb_sb_info *server = server_from_inode(inode);
1761         int result;
1762         char *param;
1763         char *data;
1764         struct smb_request *req;
1765
1766         result = -ENOMEM;
1767         if (! (req = smb_alloc_request(server, 14)))
1768                 goto out;
1769
1770         param = req->rq_buffer;
1771         data = req->rq_buffer + 6;
1772
1773         /* FIXME: must we also set allocation size? winNT seems to do that */
1774         WSET(param, 0, SMB_I(inode)->fileid);
1775         WSET(param, 2, SMB_SET_FILE_END_OF_FILE_INFO);
1776         WSET(param, 4, 0);
1777         LSET(data, 0, length);
1778
1779         req->rq_trans2_command = TRANSACT2_SETFILEINFO;
1780         req->rq_ldata = 8;
1781         req->rq_data  = data;
1782         req->rq_lparm = 6;
1783         req->rq_parm  = param;
1784         req->rq_flags |= SMB_REQ_NORETRY;
1785         result = smb_add_request(req);
1786         if (result < 0)
1787                 goto out_free;
1788
1789         result = 0;
1790         if (req->rq_rcls != 0)
1791                 result = smb_errno(req);
1792
1793 out_free:
1794         smb_rput(req);
1795 out:
1796         return result;
1797 }
1798
1799 static int
1800 smb_proc_trunc95(struct inode *inode, loff_t length)
1801 {
1802         struct smb_sb_info *server = server_from_inode(inode);
1803         int result = smb_proc_trunc32(inode, length);
1804  
1805         /*
1806          * win9x doesn't appear to update the size immediately.
1807          * It will return the old file size after the truncate,
1808          * confusing smbfs. So we force an update.
1809          *
1810          * FIXME: is this still necessary?
1811          */
1812         smb_proc_flush(server, SMB_I(inode)->fileid);
1813         return result;
1814 }
1815
1816 static void
1817 smb_init_dirent(struct smb_sb_info *server, struct smb_fattr *fattr)
1818 {
1819         memset(fattr, 0, sizeof(*fattr));
1820
1821         fattr->f_nlink = 1;
1822         fattr->f_uid = server->mnt->uid;
1823         fattr->f_gid = server->mnt->gid;
1824         fattr->f_blksize = SMB_ST_BLKSIZE;
1825         fattr->f_unix = 0;
1826 }
1827
1828 static void
1829 smb_finish_dirent(struct smb_sb_info *server, struct smb_fattr *fattr)
1830 {
1831         if (fattr->f_unix)
1832                 return;
1833
1834         fattr->f_mode = server->mnt->file_mode;
1835         if (fattr->attr & aDIR) {
1836                 fattr->f_mode = server->mnt->dir_mode;
1837                 fattr->f_size = SMB_ST_BLKSIZE;
1838         }
1839         /* Check the read-only flag */
1840         if (fattr->attr & aRONLY)
1841                 fattr->f_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
1842
1843         /* How many 512 byte blocks do we need for this file? */
1844         fattr->f_blocks = 0;
1845         if (fattr->f_size != 0)
1846                 fattr->f_blocks = 1 + ((fattr->f_size-1) >> 9);
1847         return;
1848 }
1849
1850 void
1851 smb_init_root_dirent(struct smb_sb_info *server, struct smb_fattr *fattr)
1852 {
1853         smb_init_dirent(server, fattr);
1854         fattr->attr = aDIR;
1855         fattr->f_ino = 2; /* traditional root inode number */
1856         fattr->f_mtime = CURRENT_TIME;
1857         smb_finish_dirent(server, fattr);
1858 }
1859
1860 /*
1861  * Decode a dirent for old protocols
1862  *
1863  * qname is filled with the decoded, and possibly translated, name.
1864  * fattr receives decoded attributes
1865  *
1866  * Bugs Noted:
1867  * (1) Pathworks servers may pad the name with extra spaces.
1868  */
1869 static char *
1870 smb_decode_short_dirent(struct smb_sb_info *server, char *p,
1871                         struct qstr *qname, struct smb_fattr *fattr,
1872                         unsigned char *name_buf)
1873 {
1874         int len;
1875
1876         /*
1877          * SMB doesn't have a concept of inode numbers ...
1878          */
1879         smb_init_dirent(server, fattr);
1880         fattr->f_ino = 0;       /* FIXME: do we need this? */
1881
1882         p += SMB_STATUS_SIZE;   /* reserved (search_status) */
1883         fattr->attr = *p;
1884         fattr->f_mtime.tv_sec = date_dos2unix(server, WVAL(p, 3), WVAL(p, 1));
1885         fattr->f_mtime.tv_nsec = 0;
1886         fattr->f_size = DVAL(p, 5);
1887         fattr->f_ctime = fattr->f_mtime;
1888         fattr->f_atime = fattr->f_mtime;
1889         qname->name = p + 9;
1890         len = strnlen(qname->name, 12);
1891
1892         /*
1893          * Trim trailing blanks for Pathworks servers
1894          */
1895         while (len > 2 && qname->name[len-1] == ' ')
1896                 len--;
1897
1898         smb_finish_dirent(server, fattr);
1899
1900 #if 0
1901         /* FIXME: These only work for ascii chars, and recent smbmount doesn't
1902            allow the flag to be set anyway. It kills const. Remove? */
1903         switch (server->opt.case_handling) {
1904         case SMB_CASE_UPPER:
1905                 str_upper(entry->name, len);
1906                 break;
1907         case SMB_CASE_LOWER:
1908                 str_lower(entry->name, len);
1909                 break;
1910         default:
1911                 break;
1912         }
1913 #endif
1914
1915         qname->len = 0;
1916         len = server->ops->convert(name_buf, SMB_MAXNAMELEN,
1917                                    qname->name, len,
1918                                    server->remote_nls, server->local_nls);
1919         if (len > 0) {
1920                 qname->len = len;
1921                 qname->name = name_buf;
1922                 DEBUG1("len=%d, name=%.*s\n",qname->len,qname->len,qname->name);
1923         }
1924
1925         return p + 22;
1926 }
1927
1928 /*
1929  * This routine is used to read in directory entries from the network.
1930  * Note that it is for short directory name seeks, i.e.: protocol <
1931  * SMB_PROTOCOL_LANMAN2
1932  */
1933 static int
1934 smb_proc_readdir_short(struct file *filp, void *dirent, filldir_t filldir,
1935                        struct smb_cache_control *ctl)
1936 {
1937         struct dentry *dir = filp->f_dentry;
1938         struct smb_sb_info *server = server_from_dentry(dir);
1939         struct qstr qname;
1940         struct smb_fattr fattr;
1941         char *p;
1942         int result;
1943         int i, first, entries_seen, entries;
1944         int entries_asked = (server->opt.max_xmit - 100) / SMB_DIRINFO_SIZE;
1945         __u16 bcc;
1946         __u16 count;
1947         char status[SMB_STATUS_SIZE];
1948         static struct qstr mask = {
1949                 .name   = "*.*",
1950                 .len    = 3,
1951         };
1952         unsigned char *last_status;
1953         struct smb_request *req;
1954         unsigned char *name_buf;
1955
1956         VERBOSE("%s/%s\n", DENTRY_PATH(dir));
1957
1958         lock_kernel();
1959
1960         result = -ENOMEM;
1961         if (! (name_buf = kmalloc(SMB_MAXNAMELEN, GFP_KERNEL)))
1962                 goto out;
1963
1964         first = 1;
1965         entries = 0;
1966         entries_seen = 2; /* implicit . and .. */
1967
1968         result = -ENOMEM;
1969         if (! (req = smb_alloc_request(server, server->opt.max_xmit)))
1970                 goto out_name;
1971
1972         while (1) {
1973                 p = smb_setup_header(req, SMBsearch, 2, 0);
1974                 WSET(req->rq_header, smb_vwv0, entries_asked);
1975                 WSET(req->rq_header, smb_vwv1, aDIR);
1976                 if (first == 1) {
1977                         result = smb_simple_encode_path(req, &p, dir, &mask);
1978                         if (result < 0)
1979                                 goto out_free;
1980                         if (p + 3 > (char *)req->rq_buffer + req->rq_bufsize) {
1981                                 result = -ENAMETOOLONG;
1982                                 goto out_free;
1983                         }
1984                         *p++ = 5;
1985                         WSET(p, 0, 0);
1986                         p += 2;
1987                         first = 0;
1988                 } else {
1989                         if (p + 5 + SMB_STATUS_SIZE >
1990                             (char *)req->rq_buffer + req->rq_bufsize) {
1991                                 result = -ENAMETOOLONG;
1992                                 goto out_free;
1993                         }
1994                                 
1995                         *p++ = 4;
1996                         *p++ = 0;
1997                         *p++ = 5;
1998                         WSET(p, 0, SMB_STATUS_SIZE);
1999                         p += 2;
2000                         memcpy(p, status, SMB_STATUS_SIZE);
2001                         p += SMB_STATUS_SIZE;
2002                 }
2003
2004                 smb_setup_bcc(req, p);
2005
2006                 result = smb_request_ok(req, SMBsearch, 1, -1);
2007                 if (result < 0) {
2008                         if ((req->rq_rcls == ERRDOS) && 
2009                             (req->rq_err  == ERRnofiles))
2010                                 break;
2011                         goto out_free;
2012                 }
2013                 count = WVAL(req->rq_header, smb_vwv0);
2014                 if (count <= 0)
2015                         break;
2016
2017                 result = -EIO;
2018                 bcc = smb_bcc(req->rq_header);
2019                 if (bcc != count * SMB_DIRINFO_SIZE + 3)
2020                         goto out_free;
2021                 p = req->rq_buffer + 3;
2022
2023
2024                 /* Make sure the response fits in the buffer. Fixed sized 
2025                    entries means we don't have to check in the decode loop. */
2026
2027                 last_status = req->rq_buffer + 3 + (count-1) * SMB_DIRINFO_SIZE;
2028
2029                 if (last_status + SMB_DIRINFO_SIZE >=
2030                     req->rq_buffer + req->rq_bufsize) {
2031                         printk(KERN_ERR "smb_proc_readdir_short: "
2032                                "last dir entry outside buffer! "
2033                                "%d@%p  %d@%p\n", SMB_DIRINFO_SIZE, last_status,
2034                                req->rq_bufsize, req->rq_buffer);
2035                         goto out_free;
2036                 }
2037
2038                 /* Read the last entry into the status field. */
2039                 memcpy(status, last_status, SMB_STATUS_SIZE);
2040
2041
2042                 /* Now we are ready to parse smb directory entries. */
2043
2044                 for (i = 0; i < count; i++) {
2045                         p = smb_decode_short_dirent(server, p, 
2046                                                     &qname, &fattr, name_buf);
2047                         if (qname.len == 0)
2048                                 continue;
2049
2050                         if (entries_seen == 2 && qname.name[0] == '.') {
2051                                 if (qname.len == 1)
2052                                         continue;
2053                                 if (qname.name[1] == '.' && qname.len == 2)
2054                                         continue;
2055                         }
2056                         if (!smb_fill_cache(filp, dirent, filldir, ctl, 
2057                                             &qname, &fattr))
2058                                 ;       /* stop reading? */
2059                         entries_seen++;
2060                 }
2061         }
2062         result = entries;
2063
2064 out_free:
2065         smb_rput(req);
2066 out_name:
2067         kfree(name_buf);
2068 out:
2069         unlock_kernel();
2070         return result;
2071 }
2072
2073 void smb_decode_unix_basic(struct smb_fattr *fattr, char *p)
2074 {
2075         /* FIXME: verify nls support. all is sent as utf8? */
2076
2077         fattr->f_unix = 1;
2078         fattr->f_mode = 0;
2079
2080         /* FIXME: use the uniqueID from the remote instead? */
2081         /* 0 L file size in bytes */
2082         /* 8 L file size on disk in bytes (block count) */
2083         /* 40 L uid */
2084         /* 48 L gid */
2085         /* 56 W file type */
2086         /* 60 L devmajor */
2087         /* 68 L devminor */
2088         /* 76 L unique ID (inode) */
2089         /* 84 L permissions */
2090         /* 92 L link count */
2091
2092         fattr->f_size = LVAL(p, 0);
2093         fattr->f_blocks = LVAL(p, 8);
2094         fattr->f_ctime = smb_ntutc2unixutc(LVAL(p, 16));
2095         fattr->f_atime = smb_ntutc2unixutc(LVAL(p, 24));
2096         fattr->f_mtime = smb_ntutc2unixutc(LVAL(p, 32));
2097         fattr->f_uid = LVAL(p, 40); 
2098         fattr->f_gid = LVAL(p, 48); 
2099         fattr->f_mode |= smb_filetype_to_mode(WVAL(p, 56));
2100
2101         if (S_ISBLK(fattr->f_mode) || S_ISCHR(fattr->f_mode)) {
2102                 __u64 major = LVAL(p, 60);
2103                 __u64 minor = LVAL(p, 68);
2104
2105                 fattr->f_rdev = MKDEV(major & 0xffffffff, minor & 0xffffffff);
2106                 if (MAJOR(fattr->f_rdev) != (major & 0xffffffff) ||
2107                     MINOR(fattr->f_rdev) != (minor & 0xffffffff))
2108                         fattr->f_rdev = 0;
2109         }
2110         fattr->f_mode |= LVAL(p, 84);
2111 }
2112
2113 /*
2114  * Interpret a long filename structure using the specified info level:
2115  *   level 1 for anything below NT1 protocol
2116  *   level 260 for NT1 protocol
2117  *
2118  * qname is filled with the decoded, and possibly translated, name
2119  * fattr receives decoded attributes.
2120  *
2121  * Bugs Noted:
2122  * (1) Win NT 4.0 appends a null byte to names and counts it in the length!
2123  */
2124 static char *
2125 smb_decode_long_dirent(struct smb_sb_info *server, char *p, int level,
2126                        struct qstr *qname, struct smb_fattr *fattr,
2127                        unsigned char *name_buf)
2128 {
2129         char *result;
2130         unsigned int len = 0;
2131         int n;
2132         __u16 date, time;
2133         int unicode = (server->mnt->flags & SMB_MOUNT_UNICODE);
2134
2135         /*
2136          * SMB doesn't have a concept of inode numbers ...
2137          */
2138         smb_init_dirent(server, fattr);
2139         fattr->f_ino = 0;       /* FIXME: do we need this? */
2140
2141         switch (level) {
2142         case 1:
2143                 len = *((unsigned char *) p + 22);
2144                 qname->name = p + 23;
2145                 result = p + 24 + len;
2146
2147                 date = WVAL(p, 0);
2148                 time = WVAL(p, 2);
2149                 fattr->f_ctime.tv_sec = date_dos2unix(server, date, time);
2150                 fattr->f_ctime.tv_nsec = 0;
2151
2152                 date = WVAL(p, 4);
2153                 time = WVAL(p, 6);
2154                 fattr->f_atime.tv_sec = date_dos2unix(server, date, time);
2155                 fattr->f_atime.tv_nsec = 0;
2156
2157                 date = WVAL(p, 8);
2158                 time = WVAL(p, 10);
2159                 fattr->f_mtime.tv_sec = date_dos2unix(server, date, time);
2160                 fattr->f_mtime.tv_nsec = 0;
2161                 fattr->f_size = DVAL(p, 12);
2162                 /* ULONG allocation size */
2163                 fattr->attr = WVAL(p, 20);
2164
2165                 VERBOSE("info 1 at %p, len=%d, name=%.*s\n",
2166                         p, len, len, qname->name);
2167                 break;
2168         case 260:
2169                 result = p + WVAL(p, 0);
2170                 len = DVAL(p, 60);
2171                 if (len > 255) len = 255;
2172                 /* NT4 null terminates, unless we are using unicode ... */
2173                 qname->name = p + 94;
2174                 if (!unicode && len && qname->name[len-1] == '\0')
2175                         len--;
2176
2177                 fattr->f_ctime = smb_ntutc2unixutc(LVAL(p, 8));
2178                 fattr->f_atime = smb_ntutc2unixutc(LVAL(p, 16));
2179                 fattr->f_mtime = smb_ntutc2unixutc(LVAL(p, 24));
2180                 /* change time (32) */
2181                 fattr->f_size = LVAL(p, 40);
2182                 /* alloc size (48) */
2183                 fattr->attr = DVAL(p, 56);
2184
2185                 VERBOSE("info 260 at %p, len=%d, name=%.*s\n",
2186                         p, len, len, qname->name);
2187                 break;
2188         case SMB_FIND_FILE_UNIX:
2189                 result = p + WVAL(p, 0);
2190                 qname->name = p + 108;
2191
2192                 len = strlen(qname->name);
2193                 /* FIXME: should we check the length?? */
2194
2195                 p += 8;
2196                 smb_decode_unix_basic(fattr, p);
2197                 VERBOSE("info SMB_FIND_FILE_UNIX at %p, len=%d, name=%.*s\n",
2198                         p, len, len, qname->name);
2199                 break;
2200         default:
2201                 PARANOIA("Unknown info level %d\n", level);
2202                 result = p + WVAL(p, 0);
2203                 goto out;
2204         }
2205
2206         smb_finish_dirent(server, fattr);
2207
2208 #if 0
2209         /* FIXME: These only work for ascii chars, and recent smbmount doesn't
2210            allow the flag to be set anyway. Remove? */
2211         switch (server->opt.case_handling) {
2212         case SMB_CASE_UPPER:
2213                 str_upper(qname->name, len);
2214                 break;
2215         case SMB_CASE_LOWER:
2216                 str_lower(qname->name, len);
2217                 break;
2218         default:
2219                 break;
2220         }
2221 #endif
2222
2223         qname->len = 0;
2224         n = server->ops->convert(name_buf, SMB_MAXNAMELEN,
2225                                  qname->name, len,
2226                                  server->remote_nls, server->local_nls);
2227         if (n > 0) {
2228                 qname->len = n;
2229                 qname->name = name_buf;
2230         }
2231
2232 out:
2233         return result;
2234 }
2235
2236 /* findfirst/findnext flags */
2237 #define SMB_CLOSE_AFTER_FIRST (1<<0)
2238 #define SMB_CLOSE_IF_END (1<<1)
2239 #define SMB_REQUIRE_RESUME_KEY (1<<2)
2240 #define SMB_CONTINUE_BIT (1<<3)
2241
2242 /*
2243  * Note: samba-2.0.7 (at least) has a very similar routine, cli_list, in
2244  * source/libsmb/clilist.c. When looking for smb bugs in the readdir code,
2245  * go there for advise.
2246  *
2247  * Bugs Noted:
2248  * (1) When using Info Level 1 Win NT 4.0 truncates directory listings 
2249  * for certain patterns of names and/or lengths. The breakage pattern
2250  * is completely reproducible and can be toggled by the creation of a
2251  * single file. (E.g. echo hi >foo breaks, rm -f foo works.)
2252  */
2253 static int
2254 smb_proc_readdir_long(struct file *filp, void *dirent, filldir_t filldir,
2255                       struct smb_cache_control *ctl)
2256 {
2257         struct dentry *dir = filp->f_dentry;
2258         struct smb_sb_info *server = server_from_dentry(dir);
2259         struct qstr qname;
2260         struct smb_fattr fattr;
2261
2262         unsigned char *p, *lastname;
2263         char *mask, *param;
2264         __u16 command;
2265         int first, entries_seen;
2266
2267         /* Both NT and OS/2 accept info level 1 (but see note below). */
2268         int info_level = 260;
2269         const int max_matches = 512;
2270
2271         unsigned int ff_searchcount = 0;
2272         unsigned int ff_eos = 0;
2273         unsigned int ff_lastname = 0;
2274         unsigned int ff_dir_handle = 0;
2275         unsigned int loop_count = 0;
2276         unsigned int mask_len, i;
2277         int result;
2278         struct smb_request *req;
2279         unsigned char *name_buf;
2280         static struct qstr star = {
2281                 .name   = "*",
2282                 .len    = 1,
2283         };
2284
2285         lock_kernel();
2286
2287         /*
2288          * We always prefer unix style. Use info level 1 for older
2289          * servers that don't do 260.
2290          */
2291         if (server->opt.capabilities & SMB_CAP_UNIX)
2292                 info_level = SMB_FIND_FILE_UNIX;
2293         else if (server->opt.protocol < SMB_PROTOCOL_NT1)
2294                 info_level = 1;
2295
2296         result = -ENOMEM;
2297         if (! (name_buf = kmalloc(SMB_MAXNAMELEN+2, GFP_KERNEL)))
2298                 goto out;
2299         if (! (req = smb_alloc_request(server, server->opt.max_xmit)))
2300                 goto out_name;
2301         param = req->rq_buffer;
2302
2303         /*
2304          * Encode the initial path
2305          */
2306         mask = param + 12;
2307
2308         mask_len = smb_encode_path(server, mask, SMB_MAXPATHLEN+1, dir, &star);
2309         if (mask_len < 0) {
2310                 result = mask_len;
2311                 goto out_free;
2312         }
2313         mask_len--;     /* mask_len is strlen, not #bytes */
2314         first = 1;
2315         VERBOSE("starting mask_len=%d, mask=%s\n", mask_len, mask);
2316
2317         result = 0;
2318         entries_seen = 2;
2319         ff_eos = 0;
2320
2321         while (ff_eos == 0) {
2322                 loop_count += 1;
2323                 if (loop_count > 10) {
2324                         printk(KERN_WARNING "smb_proc_readdir_long: "
2325                                "Looping in FIND_NEXT??\n");
2326                         result = -EIO;
2327                         break;
2328                 }
2329
2330                 if (first != 0) {
2331                         command = TRANSACT2_FINDFIRST;
2332                         WSET(param, 0, aSYSTEM | aHIDDEN | aDIR);
2333                         WSET(param, 2, max_matches);    /* max count */
2334                         WSET(param, 4, SMB_CLOSE_IF_END);
2335                         WSET(param, 6, info_level);
2336                         DSET(param, 8, 0);
2337                 } else {
2338                         command = TRANSACT2_FINDNEXT;
2339
2340                         VERBOSE("handle=0x%X, lastname=%d, mask=%.*s\n",
2341                                 ff_dir_handle, ff_lastname, mask_len, mask);
2342
2343                         WSET(param, 0, ff_dir_handle);  /* search handle */
2344                         WSET(param, 2, max_matches);    /* max count */
2345                         WSET(param, 4, info_level);
2346                         DSET(param, 6, 0);
2347                         WSET(param, 10, SMB_CONTINUE_BIT|SMB_CLOSE_IF_END);
2348                 }
2349
2350                 req->rq_trans2_command = command;
2351                 req->rq_ldata = 0;
2352                 req->rq_data  = NULL;
2353                 req->rq_lparm = 12 + mask_len + 1;
2354                 req->rq_parm  = param;
2355                 req->rq_flags = 0;
2356                 result = smb_add_request(req);
2357                 if (result < 0) {
2358                         PARANOIA("error=%d, breaking\n", result);
2359                         break;
2360                 }
2361
2362                 if (req->rq_rcls == ERRSRV && req->rq_err == ERRerror) {
2363                         /* a damn Win95 bug - sometimes it clags if you 
2364                            ask it too fast */
2365                         current->state = TASK_INTERRUPTIBLE;
2366                         schedule_timeout(HZ/5);
2367                         continue;
2368                 }
2369
2370                 if (req->rq_rcls != 0) {
2371                         result = smb_errno(req);
2372                         PARANOIA("name=%s, result=%d, rcls=%d, err=%d\n",
2373                                  mask, result, server->rcls, server->err);
2374                         break;
2375                 }
2376
2377                 /* parse out some important return info */
2378                 if (first != 0) {
2379                         ff_dir_handle = WVAL(req->rq_parm, 0);
2380                         ff_searchcount = WVAL(req->rq_parm, 2);
2381                         ff_eos = WVAL(req->rq_parm, 4);
2382                         ff_lastname = WVAL(req->rq_parm, 8);
2383                 } else {
2384                         ff_searchcount = WVAL(req->rq_parm, 0);
2385                         ff_eos = WVAL(req->rq_parm, 2);
2386                         ff_lastname = WVAL(req->rq_parm, 6);
2387                 }
2388
2389                 if (ff_searchcount == 0)
2390                         break;
2391
2392                 /* Now we are ready to parse smb directory entries. */
2393
2394                 /* point to the data bytes */
2395                 p = req->rq_data;
2396                 for (i = 0; i < ff_searchcount; i++) {
2397                         /* make sure we stay within the buffer */
2398                         if (p >= req->rq_data + req->rq_ldata) {
2399                                 printk(KERN_ERR "smb_proc_readdir_long: "
2400                                        "dirent pointer outside buffer! "
2401                                        "%p  %d@%p\n",
2402                                        p, req->rq_ldata, req->rq_data);
2403                                 result = -EIO; /* always a comm. error? */
2404                                 goto out_free;
2405                         }
2406
2407                         p = smb_decode_long_dirent(server, p, info_level,
2408                                                    &qname, &fattr, name_buf);
2409
2410                         /* ignore . and .. from the server */
2411                         if (entries_seen == 2 && qname.name[0] == '.') {
2412                                 if (qname.len == 1)
2413                                         continue;
2414                                 if (qname.name[1] == '.' && qname.len == 2)
2415                                         continue;
2416                         }
2417
2418                         if (!smb_fill_cache(filp, dirent, filldir, ctl, 
2419                                             &qname, &fattr))
2420                                 ;       /* stop reading? */
2421                         entries_seen++;
2422                 }
2423
2424                 VERBOSE("received %d entries, eos=%d\n", ff_searchcount,ff_eos);
2425
2426                 /*
2427                  * We might need the lastname for continuations.
2428                  *
2429                  * Note that some servers (win95?) point to the filename and
2430                  * others (NT4, Samba using NT1) to the dir entry. We assume
2431                  * here that those who do not point to a filename do not need
2432                  * this info to continue the listing.
2433                  *
2434                  * OS/2 needs this and talks infolevel 1.
2435                  * NetApps want lastname with infolevel 260.
2436                  * win2k want lastname with infolevel 260, and points to
2437                  *       the record not to the name.
2438                  * Samba+CifsUnixExt doesn't need lastname.
2439                  *
2440                  * Both are happy if we return the data they point to. So we do.
2441                  * (FIXME: above is not true with win2k)
2442                  */
2443                 mask_len = 0;
2444                 if (info_level != SMB_FIND_FILE_UNIX &&
2445                     ff_lastname > 0 && ff_lastname < req->rq_ldata) {
2446                         lastname = req->rq_data + ff_lastname;
2447
2448                         switch (info_level) {
2449                         case 260:
2450                                 mask_len = req->rq_ldata - ff_lastname;
2451                                 break;
2452                         case 1:
2453                                 /* lastname points to a length byte */
2454                                 mask_len = *lastname++;
2455                                 if (ff_lastname + 1 + mask_len > req->rq_ldata)
2456                                         mask_len = req->rq_ldata - ff_lastname - 1;
2457                                 break;
2458                         }
2459
2460                         /*
2461                          * Update the mask string for the next message.
2462                          */
2463                         if (mask_len < 0)
2464                                 mask_len = 0;
2465                         if (mask_len > 255)
2466                                 mask_len = 255;
2467                         if (mask_len)
2468                                 strncpy(mask, lastname, mask_len);
2469                 }
2470                 mask_len = strnlen(mask, mask_len);
2471                 VERBOSE("new mask, len=%d@%d of %d, mask=%.*s\n",
2472                         mask_len, ff_lastname, req->rq_ldata, mask_len, mask);
2473
2474                 first = 0;
2475                 loop_count = 0;
2476         }
2477
2478 out_free:
2479         smb_rput(req);
2480 out_name:
2481         kfree(name_buf);
2482 out:
2483         unlock_kernel();
2484         return result;
2485 }
2486
2487 /*
2488  * This version uses the trans2 TRANSACT2_FINDFIRST message 
2489  * to get the attribute data.
2490  *
2491  * Bugs Noted:
2492  */
2493 static int
2494 smb_proc_getattr_ff(struct smb_sb_info *server, struct dentry *dentry,
2495                         struct smb_fattr *fattr)
2496 {
2497         char *param, *mask;
2498         __u16 date, time;
2499         int mask_len, result;
2500         struct smb_request *req;
2501
2502         result = -ENOMEM;
2503         if (! (req = smb_alloc_request(server, PAGE_SIZE)))
2504                 goto out;
2505         param = req->rq_buffer;
2506         mask = param + 12;
2507
2508         mask_len = smb_encode_path(server, mask, SMB_MAXPATHLEN+1, dentry,NULL);
2509         if (mask_len < 0) {
2510                 result = mask_len;
2511                 goto out_free;
2512         }
2513         VERBOSE("name=%s, len=%d\n", mask, mask_len);
2514         WSET(param, 0, aSYSTEM | aHIDDEN | aDIR);
2515         WSET(param, 2, 1);      /* max count */
2516         WSET(param, 4, 1);      /* close after this call */
2517         WSET(param, 6, 1);      /* info_level */
2518         DSET(param, 8, 0);
2519
2520         req->rq_trans2_command = TRANSACT2_FINDFIRST;
2521         req->rq_ldata = 0;
2522         req->rq_data  = NULL;
2523         req->rq_lparm = 12 + mask_len;
2524         req->rq_parm  = param;
2525         req->rq_flags = 0;
2526         result = smb_add_request(req);
2527         if (result < 0)
2528                 goto out_free;
2529         if (server->rcls != 0) {
2530                 result = smb_errno(req);
2531 #ifdef SMBFS_PARANOIA
2532                 if (result != -ENOENT)
2533                         PARANOIA("error for %s, rcls=%d, err=%d\n",
2534                                  mask, req->rq_rcls, req->rq_err);
2535 #endif
2536                 goto out_free;
2537         }
2538         /* Make sure we got enough data ... */
2539         result = -EINVAL;
2540         if (req->rq_ldata < 22 || WVAL(req->rq_parm, 2) != 1) {
2541                 PARANOIA("bad result for %s, len=%d, count=%d\n",
2542                          mask, req->rq_ldata, WVAL(req->rq_parm, 2));
2543                 goto out_free;
2544         }
2545
2546         /*
2547          * Decode the response into the fattr ...
2548          */
2549         date = WVAL(req->rq_data, 0);
2550         time = WVAL(req->rq_data, 2);
2551         fattr->f_ctime.tv_sec = date_dos2unix(server, date, time);
2552         fattr->f_ctime.tv_nsec = 0;
2553
2554         date = WVAL(req->rq_data, 4);
2555         time = WVAL(req->rq_data, 6);
2556         fattr->f_atime.tv_sec = date_dos2unix(server, date, time);
2557         fattr->f_atime.tv_nsec = 0;
2558
2559         date = WVAL(req->rq_data, 8);
2560         time = WVAL(req->rq_data, 10);
2561         fattr->f_mtime.tv_sec = date_dos2unix(server, date, time);
2562         fattr->f_mtime.tv_nsec = 0;
2563         VERBOSE("name=%s, date=%x, time=%x, mtime=%ld\n",
2564                 mask, date, time, fattr->f_mtime);
2565         fattr->f_size = DVAL(req->rq_data, 12);
2566         /* ULONG allocation size */
2567         fattr->attr = WVAL(req->rq_data, 20);
2568         result = 0;
2569
2570 out_free:
2571         smb_rput(req);
2572 out:
2573         return result;
2574 }
2575
2576 static int
2577 smb_proc_getattr_core(struct smb_sb_info *server, struct dentry *dir,
2578                       struct smb_fattr *fattr)
2579 {
2580         int result;
2581         char *p;
2582         struct smb_request *req;
2583
2584         result = -ENOMEM;
2585         if (! (req = smb_alloc_request(server, PAGE_SIZE)))
2586                 goto out;
2587
2588         p = smb_setup_header(req, SMBgetatr, 0, 0);
2589         result = smb_simple_encode_path(req, &p, dir, NULL);
2590         if (result < 0)
2591                 goto out_free;
2592         smb_setup_bcc(req, p);
2593
2594         if ((result = smb_request_ok(req, SMBgetatr, 10, 0)) < 0)
2595                 goto out_free;
2596         fattr->attr    = WVAL(req->rq_header, smb_vwv0);
2597         fattr->f_mtime.tv_sec = local2utc(server, DVAL(req->rq_header, smb_vwv1));
2598         fattr->f_mtime.tv_nsec = 0;
2599         fattr->f_size  = DVAL(req->rq_header, smb_vwv3);
2600         fattr->f_ctime = fattr->f_mtime; 
2601         fattr->f_atime = fattr->f_mtime; 
2602 #ifdef SMBFS_DEBUG_TIMESTAMP
2603         printk("getattr_core: %s/%s, mtime=%ld\n",
2604                DENTRY_PATH(dir), fattr->f_mtime);
2605 #endif
2606         result = 0;
2607
2608 out_free:
2609         smb_rput(req);
2610 out:
2611         return result;
2612 }
2613
2614 /*
2615  * Bugs Noted:
2616  * (1) Win 95 swaps the date and time fields in the standard info level.
2617  */
2618 static int
2619 smb_proc_getattr_trans2(struct smb_sb_info *server, struct dentry *dir,
2620                         struct smb_request *req, int infolevel)
2621 {
2622         char *p, *param;
2623         int result;
2624
2625         param = req->rq_buffer;
2626         WSET(param, 0, infolevel);
2627         DSET(param, 2, 0);
2628         result = smb_encode_path(server, param+6, SMB_MAXPATHLEN+1, dir, NULL);
2629         if (result < 0)
2630                 goto out;
2631         p = param + 6 + result;
2632
2633         req->rq_trans2_command = TRANSACT2_QPATHINFO;
2634         req->rq_ldata = 0;
2635         req->rq_data  = NULL;
2636         req->rq_lparm = p - param;
2637         req->rq_parm  = param;
2638         req->rq_flags = 0;
2639         result = smb_add_request(req);
2640         if (result < 0)
2641                 goto out;
2642         if (server->rcls != 0) {
2643                 VERBOSE("for %s: result=%d, rcls=%d, err=%d\n",
2644                         &param[6], result, req->rq_rcls, req->rq_err);
2645                 result = smb_errno(req);
2646                 goto out;
2647         }
2648         result = -ENOENT;
2649         if (req->rq_ldata < 22) {
2650                 PARANOIA("not enough data for %s, len=%d\n",
2651                          &param[6], req->rq_ldata);
2652                 goto out;
2653         }
2654
2655         result = 0;
2656 out:
2657         return result;
2658 }
2659
2660 static int
2661 smb_proc_getattr_trans2_std(struct smb_sb_info *server, struct dentry *dir,
2662                             struct smb_fattr *attr)
2663 {
2664         u16 date, time;
2665         int off_date = 0, off_time = 2;
2666         int result;
2667         struct smb_request *req;
2668
2669         result = -ENOMEM;
2670         if (! (req = smb_alloc_request(server, PAGE_SIZE)))
2671                 goto out;
2672
2673         result = smb_proc_getattr_trans2(server, dir, req, SMB_INFO_STANDARD);
2674         if (result < 0)
2675                 goto out_free;
2676
2677         /*
2678          * Kludge alert: Win 95 swaps the date and time field,
2679          * contrary to the CIFS docs and Win NT practice.
2680          */
2681         if (server->mnt->flags & SMB_MOUNT_WIN95) {
2682                 off_date = 2;
2683                 off_time = 0;
2684         }
2685         date = WVAL(req->rq_data, off_date);
2686         time = WVAL(req->rq_data, off_time);
2687         attr->f_ctime.tv_sec = date_dos2unix(server, date, time);
2688         attr->f_ctime.tv_nsec = 0;
2689
2690         date = WVAL(req->rq_data, 4 + off_date);
2691         time = WVAL(req->rq_data, 4 + off_time);
2692         attr->f_atime.tv_sec = date_dos2unix(server, date, time);
2693         attr->f_atime.tv_nsec = 0;
2694
2695         date = WVAL(req->rq_data, 8 + off_date);
2696         time = WVAL(req->rq_data, 8 + off_time);
2697         attr->f_mtime.tv_sec = date_dos2unix(server, date, time);
2698         attr->f_mtime.tv_nsec = 0;
2699 #ifdef SMBFS_DEBUG_TIMESTAMP
2700         printk(KERN_DEBUG "getattr_trans2: %s/%s, date=%x, time=%x, mtime=%ld\n",
2701                DENTRY_PATH(dir), date, time, attr->f_mtime);
2702 #endif
2703         attr->f_size = DVAL(req->rq_data, 12);
2704         attr->attr = WVAL(req->rq_data, 20);
2705
2706 out_free:
2707         smb_rput(req);
2708 out:
2709         return result;
2710 }
2711
2712 static int
2713 smb_proc_getattr_trans2_all(struct smb_sb_info *server, struct dentry *dir,
2714                             struct smb_fattr *attr)
2715 {
2716         struct smb_request *req;
2717         int result;
2718
2719         result = -ENOMEM;
2720         if (! (req = smb_alloc_request(server, PAGE_SIZE)))
2721                 goto out;
2722
2723         result = smb_proc_getattr_trans2(server, dir, req,
2724                                          SMB_QUERY_FILE_ALL_INFO);
2725         if (result < 0)
2726                 goto out_free;
2727
2728         attr->f_ctime = smb_ntutc2unixutc(LVAL(req->rq_data, 0));
2729         attr->f_atime = smb_ntutc2unixutc(LVAL(req->rq_data, 8));
2730         attr->f_mtime = smb_ntutc2unixutc(LVAL(req->rq_data, 16));
2731         /* change (24) */
2732         attr->attr = WVAL(req->rq_data, 32);
2733         /* pad? (34) */
2734         /* allocated size (40) */
2735         attr->f_size = LVAL(req->rq_data, 48);
2736
2737 out_free:
2738         smb_rput(req);
2739 out:
2740         return result;
2741 }
2742
2743 static int
2744 smb_proc_getattr_unix(struct smb_sb_info *server, struct dentry *dir,
2745                       struct smb_fattr *attr)
2746 {
2747         struct smb_request *req;
2748         int result;
2749
2750         result = -ENOMEM;
2751         if (! (req = smb_alloc_request(server, PAGE_SIZE)))
2752                 goto out;
2753
2754         result = smb_proc_getattr_trans2(server, dir, req,
2755                                          SMB_QUERY_FILE_UNIX_BASIC);
2756         if (result < 0)
2757                 goto out_free;
2758
2759         smb_decode_unix_basic(attr, req->rq_data);
2760
2761 out_free:
2762         smb_rput(req);
2763 out:
2764         return result;
2765 }
2766
2767 static int
2768 smb_proc_getattr_95(struct smb_sb_info *server, struct dentry *dir,
2769                     struct smb_fattr *attr)
2770 {
2771         struct inode *inode = dir->d_inode;
2772         int result;
2773
2774         /* FIXME: why not use the "all" version? */
2775         result = smb_proc_getattr_trans2_std(server, dir, attr);
2776         if (result < 0)
2777                 goto out;
2778
2779         /*
2780          * None of the getattr versions here can make win9x return the right
2781          * filesize if there are changes made to an open file.
2782          * A seek-to-end does return the right size, but we only need to do
2783          * that on files we have written.
2784          */
2785         if (inode && SMB_I(inode)->flags & SMB_F_LOCALWRITE &&
2786             smb_is_open(inode))
2787         {
2788                 __u16 fileid = SMB_I(inode)->fileid;
2789                 attr->f_size = smb_proc_seek(server, fileid, 2, 0);
2790         }
2791
2792 out:
2793         return result;
2794 }
2795
2796 static int
2797 smb_proc_getattr_null(struct smb_sb_info *server, struct dentry *dir,
2798                       struct smb_fattr *attr)
2799 {
2800         return -EIO;
2801 }
2802
2803 int
2804 smb_proc_getattr(struct dentry *dir, struct smb_fattr *fattr)
2805 {
2806         struct smb_sb_info *server = server_from_dentry(dir);
2807         int result;
2808
2809         smb_init_dirent(server, fattr);
2810         result = server->ops->getattr(server, dir, fattr);
2811         smb_finish_dirent(server, fattr);
2812
2813         return result;
2814 }
2815
2816
2817 /*
2818  * Because of bugs in the core protocol, we use this only to set
2819  * attributes. See smb_proc_settime() below for timestamp handling.
2820  *
2821  * Bugs Noted:
2822  * (1) If mtime is non-zero, both Win 3.1 and Win 95 fail
2823  * with an undocumented error (ERRDOS code 50). Setting
2824  * mtime to 0 allows the attributes to be set.
2825  * (2) The extra parameters following the name string aren't
2826  * in the CIFS docs, but seem to be necessary for operation.
2827  */
2828 static int
2829 smb_proc_setattr_core(struct smb_sb_info *server, struct dentry *dentry,
2830                       __u16 attr)
2831 {
2832         char *p;
2833         int result;
2834         struct smb_request *req;
2835
2836         result = -ENOMEM;
2837         if (! (req = smb_alloc_request(server, PAGE_SIZE)))
2838                 goto out;
2839
2840         p = smb_setup_header(req, SMBsetatr, 8, 0);
2841         WSET(req->rq_header, smb_vwv0, attr);
2842         DSET(req->rq_header, smb_vwv1, 0); /* mtime */
2843         WSET(req->rq_header, smb_vwv3, 0); /* reserved values */
2844         WSET(req->rq_header, smb_vwv4, 0);
2845         WSET(req->rq_header, smb_vwv5, 0);
2846         WSET(req->rq_header, smb_vwv6, 0);
2847         WSET(req->rq_header, smb_vwv7, 0);
2848         result = smb_simple_encode_path(req, &p, dentry, NULL);
2849         if (result < 0)
2850                 goto out_free;
2851         if (p + 2 > (char *)req->rq_buffer + req->rq_bufsize) {
2852                 result = -ENAMETOOLONG;
2853                 goto out_free;
2854         }
2855         *p++ = 4;
2856         *p++ = 0;
2857         smb_setup_bcc(req, p);
2858
2859         result = smb_request_ok(req, SMBsetatr, 0, 0);
2860         if (result < 0)
2861                 goto out_free;
2862         result = 0;
2863
2864 out_free:
2865         smb_rput(req);
2866 out:
2867         return result;
2868 }
2869
2870 /*
2871  * Because of bugs in the trans2 setattr messages, we must set
2872  * attributes and timestamps separately. The core SMBsetatr
2873  * message seems to be the only reliable way to set attributes.
2874  */
2875 int
2876 smb_proc_setattr(struct dentry *dir, struct smb_fattr *fattr)
2877 {
2878         struct smb_sb_info *server = server_from_dentry(dir);
2879         int result;
2880
2881         VERBOSE("setting %s/%s, open=%d\n", 
2882                 DENTRY_PATH(dir), smb_is_open(dir->d_inode));
2883         result = smb_proc_setattr_core(server, dir, fattr->attr);
2884         return result;
2885 }
2886
2887 /*
2888  * Sets the timestamps for an file open with write permissions.
2889  */
2890 static int
2891 smb_proc_setattr_ext(struct smb_sb_info *server,
2892                       struct inode *inode, struct smb_fattr *fattr)
2893 {
2894         __u16 date, time;
2895         int result;
2896         struct smb_request *req;
2897
2898         result = -ENOMEM;
2899         if (! (req = smb_alloc_request(server, 0)))
2900                 goto out;
2901
2902         smb_setup_header(req, SMBsetattrE, 7, 0);
2903         WSET(req->rq_header, smb_vwv0, SMB_I(inode)->fileid);
2904         /* We don't change the creation time */
2905         WSET(req->rq_header, smb_vwv1, 0);
2906         WSET(req->rq_header, smb_vwv2, 0);
2907         date_unix2dos(server, fattr->f_atime.tv_sec, &date, &time);
2908         WSET(req->rq_header, smb_vwv3, date);
2909         WSET(req->rq_header, smb_vwv4, time);
2910         date_unix2dos(server, fattr->f_mtime.tv_sec, &date, &time);
2911         WSET(req->rq_header, smb_vwv5, date);
2912         WSET(req->rq_header, smb_vwv6, time);
2913 #ifdef SMBFS_DEBUG_TIMESTAMP
2914         printk(KERN_DEBUG "smb_proc_setattr_ext: date=%d, time=%d, mtime=%ld\n",
2915                date, time, fattr->f_mtime);
2916 #endif
2917
2918         req->rq_flags |= SMB_REQ_NORETRY;
2919         result = smb_request_ok(req, SMBsetattrE, 0, 0);
2920         if (result < 0)
2921                 goto out_free;
2922         result = 0;
2923 out_free:
2924         smb_rput(req);
2925 out:
2926         return result;
2927 }
2928
2929 /*
2930  * Bugs Noted:
2931  * (1) The TRANSACT2_SETPATHINFO message under Win NT 4.0 doesn't
2932  * set the file's attribute flags.
2933  */
2934 static int
2935 smb_proc_setattr_trans2(struct smb_sb_info *server,
2936                         struct dentry *dir, struct smb_fattr *fattr)
2937 {
2938         __u16 date, time;
2939         char *p, *param;
2940         int result;
2941         char data[26];
2942         struct smb_request *req;
2943
2944         result = -ENOMEM;
2945         if (! (req = smb_alloc_request(server, PAGE_SIZE)))
2946                 goto out;
2947         param = req->rq_buffer;
2948
2949         WSET(param, 0, 1);      /* Info level SMB_INFO_STANDARD */
2950         DSET(param, 2, 0);
2951         result = smb_encode_path(server, param+6, SMB_MAXPATHLEN+1, dir, NULL);
2952         if (result < 0)
2953                 goto out_free;
2954         p = param + 6 + result;
2955
2956         WSET(data, 0, 0); /* creation time */
2957         WSET(data, 2, 0);
2958         date_unix2dos(server, fattr->f_atime.tv_sec, &date, &time);
2959         WSET(data, 4, date);
2960         WSET(data, 6, time);
2961         date_unix2dos(server, fattr->f_mtime.tv_sec, &date, &time);
2962         WSET(data, 8, date);
2963         WSET(data, 10, time);
2964 #ifdef SMBFS_DEBUG_TIMESTAMP
2965         printk(KERN_DEBUG "setattr_trans2: %s/%s, date=%x, time=%x, mtime=%ld\n", 
2966                DENTRY_PATH(dir), date, time, fattr->f_mtime);
2967 #endif
2968         DSET(data, 12, 0); /* size */
2969         DSET(data, 16, 0); /* blksize */
2970         WSET(data, 20, 0); /* attr */
2971         DSET(data, 22, 0); /* ULONG EA size */
2972
2973         req->rq_trans2_command = TRANSACT2_SETPATHINFO;
2974         req->rq_ldata = 26;
2975         req->rq_data  = data;
2976         req->rq_lparm = p - param;
2977         req->rq_parm  = param;
2978         req->rq_flags = 0;
2979         result = smb_add_request(req);
2980         if (result < 0)
2981                 goto out_free;
2982         result = 0;
2983         if (req->rq_rcls != 0)
2984                 result = smb_errno(req);
2985
2986 out_free:
2987         smb_rput(req);
2988 out:
2989         return result;
2990 }
2991
2992 /*
2993  * ATTR_MODE      0x001
2994  * ATTR_UID       0x002
2995  * ATTR_GID       0x004
2996  * ATTR_SIZE      0x008
2997  * ATTR_ATIME     0x010
2998  * ATTR_MTIME     0x020
2999  * ATTR_CTIME     0x040
3000  * ATTR_ATIME_SET 0x080
3001  * ATTR_MTIME_SET 0x100
3002  * ATTR_FORCE     0x200 
3003  * ATTR_ATTR_FLAG 0x400
3004  *
3005  * major/minor should only be set by mknod.
3006  */
3007 int
3008 smb_proc_setattr_unix(struct dentry *d, struct iattr *attr,
3009                       unsigned int major, unsigned int minor)
3010 {
3011         struct smb_sb_info *server = server_from_dentry(d);
3012         u64 nttime;
3013         char *p, *param;
3014         int result;
3015         char data[100];
3016         struct smb_request *req;
3017
3018         result = -ENOMEM;
3019         if (! (req = smb_alloc_request(server, PAGE_SIZE)))
3020                 goto out;
3021         param = req->rq_buffer;
3022
3023         DEBUG1("valid flags = 0x%04x\n", attr->ia_valid);
3024
3025         WSET(param, 0, SMB_SET_FILE_UNIX_BASIC);
3026         DSET(param, 2, 0);
3027         result = smb_encode_path(server, param+6, SMB_MAXPATHLEN+1, d, NULL);
3028         if (result < 0)
3029                 goto out_free;
3030         p = param + 6 + result;
3031
3032         /* 0 L file size in bytes */
3033         /* 8 L file size on disk in bytes (block count) */
3034         /* 40 L uid */
3035         /* 48 L gid */
3036         /* 56 W file type enum */
3037         /* 60 L devmajor */
3038         /* 68 L devminor */
3039         /* 76 L unique ID (inode) */
3040         /* 84 L permissions */
3041         /* 92 L link count */
3042         LSET(data, 0, SMB_SIZE_NO_CHANGE);
3043         LSET(data, 8, SMB_SIZE_NO_CHANGE);
3044         LSET(data, 16, SMB_TIME_NO_CHANGE);
3045         LSET(data, 24, SMB_TIME_NO_CHANGE);
3046         LSET(data, 32, SMB_TIME_NO_CHANGE);
3047         LSET(data, 40, SMB_UID_NO_CHANGE);
3048         LSET(data, 48, SMB_GID_NO_CHANGE);
3049         LSET(data, 56, smb_filetype_from_mode(attr->ia_mode));
3050         LSET(data, 60, major);
3051         LSET(data, 68, minor);
3052         LSET(data, 76, 0);
3053         LSET(data, 84, SMB_MODE_NO_CHANGE);
3054         LSET(data, 92, 0);
3055
3056         if (attr->ia_valid & ATTR_SIZE) {
3057                 LSET(data, 0, attr->ia_size);
3058                 LSET(data, 8, 0); /* can't set anyway */
3059         }
3060
3061         /*
3062          * FIXME: check the conversion function it the correct one
3063          *
3064          * we can't set ctime but we might as well pass this to the server
3065          * and let it ignore it.
3066          */
3067         if (attr->ia_valid & ATTR_CTIME) {
3068                 nttime = smb_unixutc2ntutc(attr->ia_ctime);
3069                 LSET(data, 16, nttime);
3070         }
3071         if (attr->ia_valid & ATTR_ATIME) {
3072                 nttime = smb_unixutc2ntutc(attr->ia_atime);
3073                 LSET(data, 24, nttime);
3074         }
3075         if (attr->ia_valid & ATTR_MTIME) {
3076                 nttime = smb_unixutc2ntutc(attr->ia_mtime);
3077                 LSET(data, 32, nttime);
3078         }
3079         
3080         if (attr->ia_valid & ATTR_UID) {
3081                 LSET(data, 40, attr->ia_uid);
3082         }
3083         if (attr->ia_valid & ATTR_GID) {
3084                 LSET(data, 48, attr->ia_gid); 
3085         }
3086         
3087         if (attr->ia_valid & ATTR_MODE) {
3088                 LSET(data, 84, attr->ia_mode);
3089         }
3090
3091         req->rq_trans2_command = TRANSACT2_SETPATHINFO;
3092         req->rq_ldata = 100;
3093         req->rq_data  = data;
3094         req->rq_lparm = p - param;
3095         req->rq_parm  = param;
3096         req->rq_flags = 0;
3097         result = smb_add_request(req);
3098
3099 out_free:
3100         smb_rput(req);
3101 out:
3102         return result;
3103 }
3104
3105
3106 /*
3107  * Set the modify and access timestamps for a file.
3108  *
3109  * Incredibly enough, in all of SMB there is no message to allow
3110  * setting both attributes and timestamps at once. 
3111  *
3112  * Bugs Noted:
3113  * (1) Win 95 doesn't support the TRANSACT2_SETFILEINFO message 
3114  * with info level 1 (INFO_STANDARD).
3115  * (2) Win 95 seems not to support setting directory timestamps.
3116  * (3) Under the core protocol apparently the only way to set the
3117  * timestamp is to open and close the file.
3118  */
3119 int
3120 smb_proc_settime(struct dentry *dentry, struct smb_fattr *fattr)
3121 {
3122         struct smb_sb_info *server = server_from_dentry(dentry);
3123         struct inode *inode = dentry->d_inode;
3124         int result;
3125
3126         VERBOSE("setting %s/%s, open=%d\n",
3127                 DENTRY_PATH(dentry), smb_is_open(inode));
3128
3129         /* setting the time on a Win95 server fails (tridge) */
3130         if (server->opt.protocol >= SMB_PROTOCOL_LANMAN2 && 
3131             !(server->mnt->flags & SMB_MOUNT_WIN95)) {
3132                 if (smb_is_open(inode) && SMB_I(inode)->access != SMB_O_RDONLY)
3133                         result = smb_proc_setattr_ext(server, inode, fattr);
3134                 else
3135                         result = smb_proc_setattr_trans2(server, dentry, fattr);
3136         } else {
3137                 /*
3138                  * Fail silently on directories ... timestamp can't be set?
3139                  */
3140                 result = 0;
3141                 if (S_ISREG(inode->i_mode)) {
3142                         /*
3143                          * Set the mtime by opening and closing the file.
3144                          * Note that the file is opened read-only, but this
3145                          * still allows us to set the date (tridge)
3146                          */
3147                         result = -EACCES;
3148                         if (!smb_is_open(inode))
3149                                 smb_proc_open(server, dentry, SMB_O_RDONLY);
3150                         if (smb_is_open(inode)) {
3151                                 inode->i_mtime = fattr->f_mtime;
3152                                 result = smb_proc_close_inode(server, inode);
3153                         }
3154                 }
3155         }
3156
3157         return result;
3158 }
3159
3160 int
3161 smb_proc_dskattr(struct super_block *sb, struct kstatfs *attr)
3162 {
3163         struct smb_sb_info *server = SMB_SB(sb);
3164         int result;
3165         char *p;
3166         long unit;
3167         struct smb_request *req;
3168
3169         result = -ENOMEM;
3170         if (! (req = smb_alloc_request(server, 0)))
3171                 goto out;
3172
3173         smb_setup_header(req, SMBdskattr, 0, 0);
3174         if ((result = smb_request_ok(req, SMBdskattr, 5, 0)) < 0)
3175                 goto out_free;
3176         p = SMB_VWV(req->rq_header);
3177         unit = (WVAL(p, 2) * WVAL(p, 4)) >> SMB_ST_BLKSHIFT;
3178         attr->f_blocks = WVAL(p, 0) * unit;
3179         attr->f_bsize  = SMB_ST_BLKSIZE;
3180         attr->f_bavail = attr->f_bfree = WVAL(p, 6) * unit;
3181         result = 0;
3182
3183 out_free:
3184         smb_rput(req);
3185 out:
3186         return result;
3187 }
3188
3189 int
3190 smb_proc_read_link(struct smb_sb_info *server, struct dentry *d,
3191                    char *buffer, int len)
3192 {
3193         char *p, *param;
3194         int result;
3195         struct smb_request *req;
3196
3197         DEBUG1("readlink of %s/%s\n", DENTRY_PATH(d));
3198
3199         result = -ENOMEM;
3200         if (! (req = smb_alloc_request(server, PAGE_SIZE)))
3201                 goto out;
3202         param = req->rq_buffer;
3203
3204         WSET(param, 0, SMB_QUERY_FILE_UNIX_LINK);
3205         DSET(param, 2, 0);
3206         result = smb_encode_path(server, param+6, SMB_MAXPATHLEN+1, d, NULL);
3207         if (result < 0)
3208                 goto out_free;
3209         p = param + 6 + result;
3210
3211         req->rq_trans2_command = TRANSACT2_QPATHINFO;
3212         req->rq_ldata = 0;
3213         req->rq_data  = NULL;
3214         req->rq_lparm = p - param;
3215         req->rq_parm  = param;
3216         req->rq_flags = 0;
3217         result = smb_add_request(req);
3218         if (result < 0)
3219                 goto out_free;
3220         DEBUG1("for %s: result=%d, rcls=%d, err=%d\n",
3221                 &param[6], result, server->rcls, server->err);
3222
3223         /* copy data up to the \0 or buffer length */
3224         result = len;
3225         if (req->rq_ldata < len)
3226                 result = req->rq_ldata;
3227         strncpy(buffer, req->rq_data, result);
3228
3229 out_free:
3230         smb_rput(req);
3231 out:
3232         return result;
3233 }
3234
3235
3236 /*
3237  * Create a symlink object called dentry which points to oldpath.
3238  * Samba does not permit dangling links but returns a suitable error message.
3239  */
3240 int
3241 smb_proc_symlink(struct smb_sb_info *server, struct dentry *d,
3242                  const char *oldpath)
3243 {
3244         char *p, *param;
3245         int result;
3246         struct smb_request *req;
3247
3248         result = -ENOMEM;
3249         if (! (req = smb_alloc_request(server, PAGE_SIZE)))
3250                 goto out;
3251         param = req->rq_buffer;
3252
3253         WSET(param, 0, SMB_SET_FILE_UNIX_LINK);
3254         DSET(param, 2, 0);
3255         result = smb_encode_path(server, param + 6, SMB_MAXPATHLEN+1, d, NULL);
3256         if (result < 0)
3257                 goto out_free;
3258         p = param + 6 + result;
3259
3260         req->rq_trans2_command = TRANSACT2_SETPATHINFO;
3261         req->rq_ldata = strlen(oldpath) + 1;
3262         req->rq_data  = (char *) oldpath;
3263         req->rq_lparm = p - param;
3264         req->rq_parm  = param;
3265         req->rq_flags = 0;
3266         result = smb_add_request(req);
3267         if (result < 0)
3268                 goto out_free;
3269
3270         DEBUG1("for %s: result=%d, rcls=%d, err=%d\n",
3271                 &param[6], result, server->rcls, server->err);
3272         result = 0;
3273
3274 out_free:
3275         smb_rput(req);
3276 out:
3277         return result;
3278 }
3279
3280 /*
3281  * Create a hard link object called new_dentry which points to dentry.
3282  */
3283 int
3284 smb_proc_link(struct smb_sb_info *server, struct dentry *dentry,
3285               struct dentry *new_dentry)
3286 {
3287         char *p, *param;
3288         int result;
3289         struct smb_request *req;
3290
3291         result = -ENOMEM;
3292         if (! (req = smb_alloc_request(server, PAGE_SIZE)))
3293                 goto out;
3294         param = req->rq_buffer;
3295
3296         WSET(param, 0, SMB_SET_FILE_UNIX_HLINK);
3297         DSET(param, 2, 0);
3298         result = smb_encode_path(server, param + 6, SMB_MAXPATHLEN+1,
3299                                  new_dentry, NULL);
3300         if (result < 0)
3301                 goto out_free;
3302         p = param + 6 + result;
3303
3304         /* Grr, pointless separation of parameters and data ... */
3305         req->rq_data = p;
3306         req->rq_ldata = smb_encode_path(server, p, SMB_MAXPATHLEN+1,
3307                                         dentry, NULL);
3308
3309         req->rq_trans2_command = TRANSACT2_SETPATHINFO;
3310         req->rq_lparm = p - param;
3311         req->rq_parm  = param;
3312         req->rq_flags = 0;
3313         result = smb_add_request(req);
3314         if (result < 0)
3315                 goto out_free;
3316
3317         DEBUG1("for %s: result=%d, rcls=%d, err=%d\n",
3318                &param[6], result, server->rcls, server->err);
3319         result = 0;
3320
3321 out_free:
3322         smb_rput(req);
3323 out:
3324         return result;
3325 }
3326
3327 int
3328 smb_proc_query_cifsunix(struct smb_sb_info *server)
3329 {
3330         int result;
3331         int major, minor;
3332         u64 caps;
3333         char param[2];
3334         struct smb_request *req;
3335
3336         result = -ENOMEM;
3337         if (! (req = smb_alloc_request(server, 100)))
3338                 goto out;
3339
3340         WSET(param, 0, SMB_QUERY_CIFS_UNIX_INFO);
3341
3342         req->rq_trans2_command = TRANSACT2_QFSINFO;
3343         req->rq_ldata = 0;
3344         req->rq_data  = NULL;
3345         req->rq_lparm = 2;
3346         req->rq_parm  = param;
3347         req->rq_flags = 0;
3348         result = smb_add_request(req);
3349         if (result < 0)
3350                 goto out_free;
3351
3352         if (req->rq_ldata < 12) {
3353                 PARANOIA("Not enough data\n");
3354                 goto out_free;
3355         }
3356         major = WVAL(req->rq_data, 0);
3357         minor = WVAL(req->rq_data, 2);
3358
3359         DEBUG1("Server implements CIFS Extensions for UNIX systems v%d.%d\n",
3360                major, minor);
3361         /* FIXME: verify that we are ok with this major/minor? */
3362
3363         caps = LVAL(req->rq_data, 4);
3364         DEBUG1("Server capabilities 0x%016llx\n", caps);
3365
3366 out_free:
3367         smb_rput(req);
3368 out:
3369         return result;
3370 }
3371
3372
3373 static void
3374 install_ops(struct smb_ops *dst, struct smb_ops *src)
3375 {
3376         memcpy(dst, src, sizeof(void *) * SMB_OPS_NUM_STATIC);
3377 }
3378
3379 /* < LANMAN2 */
3380 static struct smb_ops smb_ops_core =
3381 {
3382         .read           = smb_proc_read,
3383         .write          = smb_proc_write,
3384         .readdir        = smb_proc_readdir_short,
3385         .getattr        = smb_proc_getattr_core,
3386         .truncate       = smb_proc_trunc32,
3387 };
3388
3389 /* LANMAN2, OS/2, others? */
3390 static struct smb_ops smb_ops_os2 =
3391 {
3392         .read           = smb_proc_read,
3393         .write          = smb_proc_write,
3394         .readdir        = smb_proc_readdir_long,
3395         .getattr        = smb_proc_getattr_trans2_std,
3396         .truncate       = smb_proc_trunc32,
3397 };
3398
3399 /* Win95, and possibly some NetApp versions too */
3400 static struct smb_ops smb_ops_win95 =
3401 {
3402         .read           = smb_proc_read,    /* does not support 12word readX */
3403         .write          = smb_proc_write,
3404         .readdir        = smb_proc_readdir_long,
3405         .getattr        = smb_proc_getattr_95,
3406         .truncate       = smb_proc_trunc95,
3407 };
3408
3409 /* Samba, NT4 and NT5 */
3410 static struct smb_ops smb_ops_winNT =
3411 {
3412         .read           = smb_proc_readX,
3413         .write          = smb_proc_writeX,
3414         .readdir        = smb_proc_readdir_long,
3415         .getattr        = smb_proc_getattr_trans2_all,
3416         .truncate       = smb_proc_trunc64,
3417 };
3418
3419 /* Samba w/ unix extensions. Others? */
3420 static struct smb_ops smb_ops_unix =
3421 {
3422         .read           = smb_proc_readX,
3423         .write          = smb_proc_writeX,
3424         .readdir        = smb_proc_readdir_long,
3425         .getattr        = smb_proc_getattr_unix,
3426         /* FIXME: core/ext/time setattr needs to be cleaned up! */
3427         /* .setattr     = smb_proc_setattr_unix, */
3428         .truncate       = smb_proc_trunc64,
3429 };
3430
3431 /* Place holder until real ops are in place */
3432 static struct smb_ops smb_ops_null =
3433 {
3434         .getattr        = smb_proc_getattr_null,
3435 };
3436
3437 void smb_install_null_ops(struct smb_ops *ops)
3438 {
3439         install_ops(ops, &smb_ops_null);
3440 }