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