forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proc.c
3507 lines (3034 loc) · 83.8 KB
/
proc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* proc.c
*
* Copyright (C) 1995, 1996 by Paal-Kr. Engstad and Volker Lendecke
* Copyright (C) 1997 by Volker Lendecke
*
* Please add a note about your changes to smbfs in the ChangeLog file.
*/
#include <linux/types.h>
#include <linux/capability.h>
#include <linux/errno.h>
#include <linux/slab.h>
#include <linux/fs.h>
#include <linux/file.h>
#include <linux/stat.h>
#include <linux/fcntl.h>
#include <linux/dcache.h>
#include <linux/nls.h>
#include <linux/smp_lock.h>
#include <linux/net.h>
#include <linux/vfs.h>
#include <linux/smb_fs.h>
#include <linux/smbno.h>
#include <linux/smb_mount.h>
#include <net/sock.h>
#include <asm/string.h>
#include <asm/div64.h>
#include "smb_debug.h"
#include "proto.h"
#include "request.h"
/* Features. Undefine if they cause problems, this should perhaps be a
config option. */
#define SMBFS_POSIX_UNLINK 1
/* Allow smb_retry to be interrupted. */
#define SMB_RETRY_INTR
#define SMB_VWV(packet) ((packet) + SMB_HEADER_LEN)
#define SMB_CMD(packet) (*(packet+8))
#define SMB_WCT(packet) (*(packet+SMB_HEADER_LEN - 1))
#define SMB_DIRINFO_SIZE 43
#define SMB_STATUS_SIZE 21
#define SMB_ST_BLKSIZE (PAGE_SIZE)
#define SMB_ST_BLKSHIFT (PAGE_SHIFT)
static struct smb_ops smb_ops_core;
static struct smb_ops smb_ops_os2;
static struct smb_ops smb_ops_win95;
static struct smb_ops smb_ops_winNT;
static struct smb_ops smb_ops_unix;
static struct smb_ops smb_ops_null;
static void
smb_init_dirent(struct smb_sb_info *server, struct smb_fattr *fattr);
static void
smb_finish_dirent(struct smb_sb_info *server, struct smb_fattr *fattr);
static int
smb_proc_getattr_core(struct smb_sb_info *server, struct dentry *dir,
struct smb_fattr *fattr);
static int
smb_proc_getattr_ff(struct smb_sb_info *server, struct dentry *dentry,
struct smb_fattr *fattr);
static int
smb_proc_setattr_core(struct smb_sb_info *server, struct dentry *dentry,
u16 attr);
static int
smb_proc_setattr_ext(struct smb_sb_info *server,
struct inode *inode, struct smb_fattr *fattr);
static int
smb_proc_query_cifsunix(struct smb_sb_info *server);
static void
install_ops(struct smb_ops *dst, struct smb_ops *src);
static void
str_upper(char *name, int len)
{
while (len--)
{
if (*name >= 'a' && *name <= 'z')
*name -= ('a' - 'A');
name++;
}
}
#if 0
static void
str_lower(char *name, int len)
{
while (len--)
{
if (*name >= 'A' && *name <= 'Z')
*name += ('a' - 'A');
name++;
}
}
#endif
/* reverse a string inline. This is used by the dircache walking routines */
static void reverse_string(char *buf, int len)
{
char c;
char *end = buf+len-1;
while(buf < end) {
c = *buf;
*(buf++) = *end;
*(end--) = c;
}
}
/* no conversion, just a wrapper for memcpy. */
static int convert_memcpy(unsigned char *output, int olen,
const unsigned char *input, int ilen,
struct nls_table *nls_from,
struct nls_table *nls_to)
{
if (olen < ilen)
return -ENAMETOOLONG;
memcpy(output, input, ilen);
return ilen;
}
static inline int write_char(unsigned char ch, char *output, int olen)
{
if (olen < 4)
return -ENAMETOOLONG;
sprintf(output, ":x%02x", ch);
return 4;
}
static inline int write_unichar(wchar_t ch, char *output, int olen)
{
if (olen < 5)
return -ENAMETOOLONG;
sprintf(output, ":%04x", ch);
return 5;
}
/* convert from one "codepage" to another (possibly being utf8). */
static int convert_cp(unsigned char *output, int olen,
const unsigned char *input, int ilen,
struct nls_table *nls_from,
struct nls_table *nls_to)
{
int len = 0;
int n;
wchar_t ch;
while (ilen > 0) {
/* convert by changing to unicode and back to the new cp */
n = nls_from->char2uni(input, ilen, &ch);
if (n == -EINVAL) {
ilen--;
n = write_char(*input++, output, olen);
if (n < 0)
goto fail;
output += n;
olen -= n;
len += n;
continue;
} else if (n < 0)
goto fail;
input += n;
ilen -= n;
n = nls_to->uni2char(ch, output, olen);
if (n == -EINVAL)
n = write_unichar(ch, output, olen);
if (n < 0)
goto fail;
output += n;
olen -= n;
len += n;
}
return len;
fail:
return n;
}
/* ----------------------------------------------------------- */
/*
* nls_unicode
*
* This encodes/decodes little endian unicode format
*/
static int uni2char(wchar_t uni, unsigned char *out, int boundlen)
{
if (boundlen < 2)
return -EINVAL;
*out++ = uni & 0xff;
*out++ = uni >> 8;
return 2;
}
static int char2uni(const unsigned char *rawstring, int boundlen, wchar_t *uni)
{
if (boundlen < 2)
return -EINVAL;
*uni = (rawstring[1] << 8) | rawstring[0];
return 2;
}
static struct nls_table unicode_table = {
.charset = "unicode",
.uni2char = uni2char,
.char2uni = char2uni,
};
/* ----------------------------------------------------------- */
static int setcodepage(struct nls_table **p, char *name)
{
struct nls_table *nls;
if (!name || !*name) {
nls = NULL;
} else if ( (nls = load_nls(name)) == NULL) {
printk (KERN_ERR "smbfs: failed to load nls '%s'\n", name);
return -EINVAL;
}
/* if already set, unload the previous one. */
if (*p && *p != &unicode_table)
unload_nls(*p);
*p = nls;
return 0;
}
/* Handles all changes to codepage settings. */
int smb_setcodepage(struct smb_sb_info *server, struct smb_nls_codepage *cp)
{
int n = 0;
smb_lock_server(server);
/* Don't load any nls_* at all, if no remote is requested */
if (!*cp->remote_name)
goto out;
/* local */
n = setcodepage(&server->local_nls, cp->local_name);
if (n != 0)
goto out;
/* remote */
if (!strcmp(cp->remote_name, "unicode")) {
server->remote_nls = &unicode_table;
} else {
n = setcodepage(&server->remote_nls, cp->remote_name);
if (n != 0)
setcodepage(&server->local_nls, NULL);
}
out:
if (server->local_nls != NULL && server->remote_nls != NULL)
server->ops->convert = convert_cp;
else
server->ops->convert = convert_memcpy;
smb_unlock_server(server);
return n;
}
/*****************************************************************************/
/* */
/* Encoding/Decoding section */
/* */
/*****************************************************************************/
static __u8 *
smb_encode_smb_length(__u8 * p, __u32 len)
{
*p = 0;
*(p+1) = 0;
*(p+2) = (len & 0xFF00) >> 8;
*(p+3) = (len & 0xFF);
if (len > 0xFFFF)
{
*(p+1) = 1;
}
return p + 4;
}
/*
* smb_build_path: build the path to entry and name storing it in buf.
* The path returned will have the trailing '\0'.
*/
static int smb_build_path(struct smb_sb_info *server, unsigned char *buf,
int maxlen,
struct dentry *entry, struct qstr *name)
{
unsigned char *path = buf;
int len;
int unicode = (server->mnt->flags & SMB_MOUNT_UNICODE) != 0;
if (maxlen < (2<<unicode))
return -ENAMETOOLONG;
if (maxlen > SMB_MAXPATHLEN + 1)
maxlen = SMB_MAXPATHLEN + 1;
if (entry == NULL)
goto test_name_and_out;
/*
* If IS_ROOT, we have to do no walking at all.
*/
if (IS_ROOT(entry) && !name) {
*path++ = '\\';
if (unicode) *path++ = '\0';
*path++ = '\0';
if (unicode) *path++ = '\0';
return path-buf;
}
/*
* Build the path string walking the tree backward from end to ROOT
* and store it in reversed order [see reverse_string()]
*/
dget(entry);
spin_lock(&entry->d_lock);
while (!IS_ROOT(entry)) {
struct dentry *parent;
if (maxlen < (3<<unicode)) {
spin_unlock(&entry->d_lock);
dput(entry);
return -ENAMETOOLONG;
}
len = server->ops->convert(path, maxlen-2,
entry->d_name.name, entry->d_name.len,
server->local_nls, server->remote_nls);
if (len < 0) {
spin_unlock(&entry->d_lock);
dput(entry);
return len;
}
reverse_string(path, len);
path += len;
if (unicode) {
/* Note: reverse order */
*path++ = '\0';
maxlen--;
}
*path++ = '\\';
maxlen -= len+1;
parent = entry->d_parent;
dget(parent);
spin_unlock(&entry->d_lock);
dput(entry);
entry = parent;
spin_lock(&entry->d_lock);
}
spin_unlock(&entry->d_lock);
dput(entry);
reverse_string(buf, path-buf);
/* maxlen has space for at least one char */
test_name_and_out:
if (name) {
if (maxlen < (3<<unicode))
return -ENAMETOOLONG;
*path++ = '\\';
if (unicode) {
*path++ = '\0';
maxlen--;
}
len = server->ops->convert(path, maxlen-2,
name->name, name->len,
server->local_nls, server->remote_nls);
if (len < 0)
return len;
path += len;
maxlen -= len+1;
}
/* maxlen has space for at least one char */
*path++ = '\0';
if (unicode) *path++ = '\0';
return path-buf;
}
static int smb_encode_path(struct smb_sb_info *server, char *buf, int maxlen,
struct dentry *dir, struct qstr *name)
{
int result;
result = smb_build_path(server, buf, maxlen, dir, name);
if (result < 0)
goto out;
if (server->opt.protocol <= SMB_PROTOCOL_COREPLUS)
str_upper(buf, result);
out:
return result;
}
/* encode_path for non-trans2 request SMBs */
static int smb_simple_encode_path(struct smb_request *req, char **p,
struct dentry * entry, struct qstr * name)
{
struct smb_sb_info *server = req->rq_server;
char *s = *p;
int res;
int maxlen = ((char *)req->rq_buffer + req->rq_bufsize) - s;
int unicode = (server->mnt->flags & SMB_MOUNT_UNICODE);
if (!maxlen)
return -ENAMETOOLONG;
*s++ = 4; /* ASCII data format */
/*
* SMB Unicode strings must be 16bit aligned relative the start of the
* packet. If they are not they must be padded with 0.
*/
if (unicode) {
int align = s - (char *)req->rq_buffer;
if (!(align & 1)) {
*s++ = '\0';
maxlen--;
}
}
res = smb_encode_path(server, s, maxlen-1, entry, name);
if (res < 0)
return res;
*p = s + res;
return 0;
}
/* The following are taken directly from msdos-fs */
/* Linear day numbers of the respective 1sts in non-leap years. */
static int day_n[] =
{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 0, 0, 0, 0};
/* JanFebMarApr May Jun Jul Aug Sep Oct Nov Dec */
static time_t
utc2local(struct smb_sb_info *server, time_t time)
{
return time - server->opt.serverzone*60;
}
static time_t
local2utc(struct smb_sb_info *server, time_t time)
{
return time + server->opt.serverzone*60;
}
/* Convert a MS-DOS time/date pair to a UNIX date (seconds since 1 1 70). */
static time_t
date_dos2unix(struct smb_sb_info *server, __u16 date, __u16 time)
{
int month, year;
time_t secs;
/* first subtract and mask after that... Otherwise, if
date == 0, bad things happen */
month = ((date >> 5) - 1) & 15;
year = date >> 9;
secs = (time & 31) * 2 + 60 * ((time >> 5) & 63) + (time >> 11) * 3600 + 86400 *
((date & 31) - 1 + day_n[month] + (year / 4) + year * 365 - ((year & 3) == 0 &&
month < 2 ? 1 : 0) + 3653);
/* days since 1.1.70 plus 80's leap day */
return local2utc(server, secs);
}
/* Convert linear UNIX date to a MS-DOS time/date pair. */
static void
date_unix2dos(struct smb_sb_info *server,
int unix_date, __u16 *date, __u16 *time)
{
int day, year, nl_day, month;
unix_date = utc2local(server, unix_date);
if (unix_date < 315532800)
unix_date = 315532800;
*time = (unix_date % 60) / 2 +
(((unix_date / 60) % 60) << 5) +
(((unix_date / 3600) % 24) << 11);
day = unix_date / 86400 - 3652;
year = day / 365;
if ((year + 3) / 4 + 365 * year > day)
year--;
day -= (year + 3) / 4 + 365 * year;
if (day == 59 && !(year & 3)) {
nl_day = day;
month = 2;
} else {
nl_day = (year & 3) || day <= 59 ? day : day - 1;
for (month = 1; month < 12; month++)
if (day_n[month] > nl_day)
break;
}
*date = nl_day - day_n[month - 1] + 1 + (month << 5) + (year << 9);
}
/* The following are taken from fs/ntfs/util.c */
#define NTFS_TIME_OFFSET ((u64)(369*365 + 89) * 24 * 3600 * 10000000)
/*
* Convert the NT UTC (based 1601-01-01, in hundred nanosecond units)
* into Unix UTC (based 1970-01-01, in seconds).
*/
static struct timespec
smb_ntutc2unixutc(u64 ntutc)
{
struct timespec ts;
/* FIXME: what about the timezone difference? */
/* Subtract the NTFS time offset, then convert to 1s intervals. */
u64 t = ntutc - NTFS_TIME_OFFSET;
ts.tv_nsec = do_div(t, 10000000) * 100;
ts.tv_sec = t;
return ts;
}
/* Convert the Unix UTC into NT time */
static u64
smb_unixutc2ntutc(struct timespec ts)
{
/* Note: timezone conversion is probably wrong. */
/* return ((u64)utc2local(server, t)) * 10000000 + NTFS_TIME_OFFSET; */
return ((u64)ts.tv_sec) * 10000000 + ts.tv_nsec/100 + NTFS_TIME_OFFSET;
}
#define MAX_FILE_MODE 6
static mode_t file_mode[] = {
S_IFREG, S_IFDIR, S_IFLNK, S_IFCHR, S_IFBLK, S_IFIFO, S_IFSOCK
};
static int smb_filetype_to_mode(u32 filetype)
{
if (filetype > MAX_FILE_MODE) {
PARANOIA("Filetype out of range: %d\n", filetype);
return S_IFREG;
}
return file_mode[filetype];
}
static u32 smb_filetype_from_mode(int mode)
{
if (S_ISREG(mode))
return UNIX_TYPE_FILE;
if (S_ISDIR(mode))
return UNIX_TYPE_DIR;
if (S_ISLNK(mode))
return UNIX_TYPE_SYMLINK;
if (S_ISCHR(mode))
return UNIX_TYPE_CHARDEV;
if (S_ISBLK(mode))
return UNIX_TYPE_BLKDEV;
if (S_ISFIFO(mode))
return UNIX_TYPE_FIFO;
if (S_ISSOCK(mode))
return UNIX_TYPE_SOCKET;
return UNIX_TYPE_UNKNOWN;
}
/*****************************************************************************/
/* */
/* Support section. */
/* */
/*****************************************************************************/
__u32
smb_len(__u8 * p)
{
return ((*(p+1) & 0x1) << 16L) | (*(p+2) << 8L) | *(p+3);
}
static __u16
smb_bcc(__u8 * packet)
{
int pos = SMB_HEADER_LEN + SMB_WCT(packet) * sizeof(__u16);
return WVAL(packet, pos);
}
/* smb_valid_packet: We check if packet fulfills the basic
requirements of a smb packet */
static int
smb_valid_packet(__u8 * packet)
{
return (packet[4] == 0xff
&& packet[5] == 'S'
&& packet[6] == 'M'
&& packet[7] == 'B'
&& (smb_len(packet) + 4 == SMB_HEADER_LEN
+ SMB_WCT(packet) * 2 + smb_bcc(packet)));
}
/* smb_verify: We check if we got the answer we expected, and if we
got enough data. If bcc == -1, we don't care. */
static int
smb_verify(__u8 * packet, int command, int wct, int bcc)
{
if (SMB_CMD(packet) != command)
goto bad_command;
if (SMB_WCT(packet) < wct)
goto bad_wct;
if (bcc != -1 && smb_bcc(packet) < bcc)
goto bad_bcc;
return 0;
bad_command:
printk(KERN_ERR "smb_verify: command=%x, SMB_CMD=%x??\n",
command, SMB_CMD(packet));
goto fail;
bad_wct:
printk(KERN_ERR "smb_verify: command=%x, wct=%d, SMB_WCT=%d??\n",
command, wct, SMB_WCT(packet));
goto fail;
bad_bcc:
printk(KERN_ERR "smb_verify: command=%x, bcc=%d, SMB_BCC=%d??\n",
command, bcc, smb_bcc(packet));
fail:
return -EIO;
}
/*
* Returns the maximum read or write size for the "payload". Making all of the
* packet fit within the negotiated max_xmit size.
*
* N.B. Since this value is usually computed before locking the server,
* the server's packet size must never be decreased!
*/
static inline int
smb_get_xmitsize(struct smb_sb_info *server, int overhead)
{
return server->opt.max_xmit - overhead;
}
/*
* Calculate the maximum read size
*/
int
smb_get_rsize(struct smb_sb_info *server)
{
/* readX has 12 parameters, read has 5 */
int overhead = SMB_HEADER_LEN + 12 * sizeof(__u16) + 2 + 1 + 2;
int size = smb_get_xmitsize(server, overhead);
VERBOSE("xmit=%d, size=%d\n", server->opt.max_xmit, size);
return size;
}
/*
* Calculate the maximum write size
*/
int
smb_get_wsize(struct smb_sb_info *server)
{
/* writeX has 14 parameters, write has 5 */
int overhead = SMB_HEADER_LEN + 14 * sizeof(__u16) + 2 + 1 + 2;
int size = smb_get_xmitsize(server, overhead);
VERBOSE("xmit=%d, size=%d\n", server->opt.max_xmit, size);
return size;
}
/*
* Convert SMB error codes to -E... errno values.
*/
int
smb_errno(struct smb_request *req)
{
int errcls = req->rq_rcls;
int error = req->rq_err;
char *class = "Unknown";
VERBOSE("errcls %d code %d from command 0x%x\n",
errcls, error, SMB_CMD(req->rq_header));
if (errcls == ERRDOS) {
switch (error) {
case ERRbadfunc:
return -EINVAL;
case ERRbadfile:
case ERRbadpath:
return -ENOENT;
case ERRnofids:
return -EMFILE;
case ERRnoaccess:
return -EACCES;
case ERRbadfid:
return -EBADF;
case ERRbadmcb:
return -EREMOTEIO;
case ERRnomem:
return -ENOMEM;
case ERRbadmem:
return -EFAULT;
case ERRbadenv:
case ERRbadformat:
return -EREMOTEIO;
case ERRbadaccess:
return -EACCES;
case ERRbaddata:
return -E2BIG;
case ERRbaddrive:
return -ENXIO;
case ERRremcd:
return -EREMOTEIO;
case ERRdiffdevice:
return -EXDEV;
case ERRnofiles:
return -ENOENT;
case ERRbadshare:
return -ETXTBSY;
case ERRlock:
return -EDEADLK;
case ERRfilexists:
return -EEXIST;
case ERROR_INVALID_PARAMETER:
return -EINVAL;
case ERROR_DISK_FULL:
return -ENOSPC;
case ERROR_INVALID_NAME:
return -ENOENT;
case ERROR_DIR_NOT_EMPTY:
return -ENOTEMPTY;
case ERROR_NOT_LOCKED:
return -ENOLCK;
case ERROR_ALREADY_EXISTS:
return -EEXIST;
default:
class = "ERRDOS";
goto err_unknown;
}
} else if (errcls == ERRSRV) {
switch (error) {
/* N.B. This is wrong ... EIO ? */
case ERRerror:
return -ENFILE;
case ERRbadpw:
return -EINVAL;
case ERRbadtype:
case ERRtimeout:
return -EIO;
case ERRaccess:
return -EACCES;
/*
* This is a fatal error, as it means the "tree ID"
* for this connection is no longer valid. We map
* to a special error code and get a new connection.
*/
case ERRinvnid:
return -EBADSLT;
default:
class = "ERRSRV";
goto err_unknown;
}
} else if (errcls == ERRHRD) {
switch (error) {
case ERRnowrite:
return -EROFS;
case ERRbadunit:
return -ENODEV;
case ERRnotready:
return -EUCLEAN;
case ERRbadcmd:
case ERRdata:
return -EIO;
case ERRbadreq:
return -ERANGE;
case ERRbadshare:
return -ETXTBSY;
case ERRlock:
return -EDEADLK;
case ERRdiskfull:
return -ENOSPC;
default:
class = "ERRHRD";
goto err_unknown;
}
} else if (errcls == ERRCMD) {
class = "ERRCMD";
} else if (errcls == SUCCESS) {
return 0; /* This is the only valid 0 return */
}
err_unknown:
printk(KERN_ERR "smb_errno: class %s, code %d from command 0x%x\n",
class, error, SMB_CMD(req->rq_header));
return -EIO;
}
/* smb_request_ok: We expect the server to be locked. Then we do the
request and check the answer completely. When smb_request_ok
returns 0, you can be quite sure that everything went well. When
the answer is <=0, the returned number is a valid unix errno. */
static int
smb_request_ok(struct smb_request *req, int command, int wct, int bcc)
{
int result;
req->rq_resp_wct = wct;
req->rq_resp_bcc = bcc;
result = smb_add_request(req);
if (result != 0) {
DEBUG1("smb_request failed\n");
goto out;
}
if (smb_valid_packet(req->rq_header) != 0) {
PARANOIA("invalid packet!\n");
goto out;
}
result = smb_verify(req->rq_header, command, wct, bcc);
out:
return result;
}
/*
* This implements the NEWCONN ioctl. It installs the server pid,
* sets server->state to CONN_VALID, and wakes up the waiting process.
*/
int
smb_newconn(struct smb_sb_info *server, struct smb_conn_opt *opt)
{
struct file *filp;
struct sock *sk;
int error;
VERBOSE("fd=%d, pid=%d\n", opt->fd, current->pid);
smb_lock_server(server);
/*
* Make sure we don't already have a valid connection ...
*/
error = -EINVAL;
if (server->state == CONN_VALID)
goto out;
error = -EACCES;
if (current_uid() != server->mnt->mounted_uid &&
!capable(CAP_SYS_ADMIN))
goto out;
error = -EBADF;
filp = fget(opt->fd);
if (!filp)
goto out;
if (!smb_valid_socket(filp->f_path.dentry->d_inode))
goto out_putf;
server->sock_file = filp;
server->conn_pid = get_pid(task_pid(current));
server->opt = *opt;
server->generation += 1;
server->state = CONN_VALID;
error = 0;
if (server->conn_error) {
/*
* conn_error is the returncode we originally decided to
* drop the old connection on. This message should be positive
* and not make people ask questions on why smbfs is printing
* error messages ...
*/
printk(KERN_INFO "SMB connection re-established (%d)\n",
server->conn_error);
server->conn_error = 0;
}
/*
* Store the server in sock user_data (Only used by sunrpc)
*/
sk = SOCKET_I(filp->f_path.dentry->d_inode)->sk;
sk->sk_user_data = server;
/* chain into the data_ready callback */
server->data_ready = xchg(&sk->sk_data_ready, smb_data_ready);
/* check if we have an old smbmount that uses seconds for the
serverzone */
if (server->opt.serverzone > 12*60 || server->opt.serverzone < -12*60)
server->opt.serverzone /= 60;
/* now that we have an established connection we can detect the server
type and enable bug workarounds */
if (server->opt.protocol < SMB_PROTOCOL_LANMAN2)
install_ops(server->ops, &smb_ops_core);
else if (server->opt.protocol == SMB_PROTOCOL_LANMAN2)
install_ops(server->ops, &smb_ops_os2);
else if (server->opt.protocol == SMB_PROTOCOL_NT1 &&
(server->opt.max_xmit < 0x1000) &&
!(server->opt.capabilities & SMB_CAP_NT_SMBS)) {
/* FIXME: can we kill the WIN95 flag now? */
server->mnt->flags |= SMB_MOUNT_WIN95;
VERBOSE("detected WIN95 server\n");
install_ops(server->ops, &smb_ops_win95);
} else {
/*
* Samba has max_xmit 65535
* NT4spX has max_xmit 4536 (or something like that)
* win2k has ...
*/
VERBOSE("detected NT1 (Samba, NT4/5) server\n");
install_ops(server->ops, &smb_ops_winNT);
}
/* FIXME: the win9x code wants to modify these ... (seek/trunc bug) */
if (server->mnt->flags & SMB_MOUNT_OLDATTR) {
server->ops->getattr = smb_proc_getattr_core;
} else if (server->mnt->flags & SMB_MOUNT_DIRATTR) {
server->ops->getattr = smb_proc_getattr_ff;
}
/* Decode server capabilities */
if (server->opt.capabilities & SMB_CAP_LARGE_FILES) {
/* Should be ok to set this now, as no one can access the
mount until the connection has been established. */
SB_of(server)->s_maxbytes = ~0ULL >> 1;
VERBOSE("LFS enabled\n");
}
if (server->opt.capabilities & SMB_CAP_UNICODE) {
server->mnt->flags |= SMB_MOUNT_UNICODE;
VERBOSE("Unicode enabled\n");
} else {
server->mnt->flags &= ~SMB_MOUNT_UNICODE;
}
#if 0
/* flags we may test for other patches ... */
if (server->opt.capabilities & SMB_CAP_LARGE_READX) {
VERBOSE("Large reads enabled\n");
}
if (server->opt.capabilities & SMB_CAP_LARGE_WRITEX) {
VERBOSE("Large writes enabled\n");
}
#endif
if (server->opt.capabilities & SMB_CAP_UNIX) {
struct inode *inode;
VERBOSE("Using UNIX CIFS extensions\n");
install_ops(server->ops, &smb_ops_unix);
inode = SB_of(server)->s_root->d_inode;
if (inode)
inode->i_op = &smb_dir_inode_operations_unix;
}
VERBOSE("protocol=%d, max_xmit=%d, pid=%d capabilities=0x%x\n",
server->opt.protocol, server->opt.max_xmit,
pid_nr(server->conn_pid), server->opt.capabilities);
/* FIXME: this really should be done by smbmount. */
if (server->opt.max_xmit > SMB_MAX_PACKET_SIZE) {
server->opt.max_xmit = SMB_MAX_PACKET_SIZE;
}
smb_unlock_server(server);
smbiod_wake_up();
if (server->opt.capabilities & SMB_CAP_UNIX)
smb_proc_query_cifsunix(server);
server->conn_complete++;
wake_up_interruptible_all(&server->conn_wq);
return error;
out:
smb_unlock_server(server);
smbiod_wake_up();
return error;
out_putf:
fput(filp);
goto out;
}
/* smb_setup_header: We completely set up the packet. You only have to