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