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