Merge 'staging-next' to Linus's tree
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / smbfs / proc.c
blobba37b1fae182f08fa23e7891ee2efe08b35b3976
1 /*
2 * proc.c
4 * Copyright (C) 1995, 1996 by Paal-Kr. Engstad and Volker Lendecke
5 * Copyright (C) 1997 by Volker Lendecke
7 * Please add a note about your changes to smbfs in the ChangeLog file.
8 */
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>
25 #include <asm/string.h>
26 #include <asm/div64.h>
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"
36 /* Features. Undefine if they cause problems, this should perhaps be a
37 config option. */
38 #define SMBFS_POSIX_UNLINK 1
40 /* Allow smb_retry to be interrupted. */
41 #define SMB_RETRY_INTR
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))
47 #define SMB_DIRINFO_SIZE 43
48 #define SMB_STATUS_SIZE 21
50 #define SMB_ST_BLKSIZE (PAGE_SIZE)
51 #define SMB_ST_BLKSHIFT (PAGE_SHIFT)
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;
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);
82 static void
83 str_upper(char *name, int len)
85 while (len--)
87 if (*name >= 'a' && *name <= 'z')
88 *name -= ('a' - 'A');
89 name++;
93 #if 0
94 static void
95 str_lower(char *name, int len)
97 while (len--)
99 if (*name >= 'A' && *name <= 'Z')
100 *name += ('a' - 'A');
101 name++;
104 #endif
106 /* reverse a string inline. This is used by the dircache walking routines */
107 static void reverse_string(char *buf, int len)
109 char c;
110 char *end = buf+len-1;
112 while(buf < end) {
113 c = *buf;
114 *(buf++) = *end;
115 *(end--) = c;
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)
125 if (olen < ilen)
126 return -ENAMETOOLONG;
127 memcpy(output, input, ilen);
128 return ilen;
131 static inline int write_char(unsigned char ch, char *output, int olen)
133 if (olen < 4)
134 return -ENAMETOOLONG;
135 sprintf(output, ":x%02x", ch);
136 return 4;
139 static inline int write_unichar(wchar_t ch, char *output, int olen)
141 if (olen < 5)
142 return -ENAMETOOLONG;
143 sprintf(output, ":%04x", ch);
144 return 5;
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)
153 int len = 0;
154 int n;
155 wchar_t ch;
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;
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;
182 len += n;
184 return len;
185 fail:
186 return n;
189 /* ----------------------------------------------------------- */
192 * nls_unicode
194 * This encodes/decodes little endian unicode format
197 static int uni2char(wchar_t uni, unsigned char *out, int boundlen)
199 if (boundlen < 2)
200 return -EINVAL;
201 *out++ = uni & 0xff;
202 *out++ = uni >> 8;
203 return 2;
206 static int char2uni(const unsigned char *rawstring, int boundlen, wchar_t *uni)
208 if (boundlen < 2)
209 return -EINVAL;
210 *uni = (rawstring[1] << 8) | rawstring[0];
211 return 2;
214 static struct nls_table unicode_table = {
215 .charset = "unicode",
216 .uni2char = uni2char,
217 .char2uni = char2uni,
220 /* ----------------------------------------------------------- */
222 static int setcodepage(struct nls_table **p, char *name)
224 struct nls_table *nls;
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;
233 /* if already set, unload the previous one. */
234 if (*p && *p != &unicode_table)
235 unload_nls(*p);
236 *p = nls;
238 return 0;
241 /* Handles all changes to codepage settings. */
242 int smb_setcodepage(struct smb_sb_info *server, struct smb_nls_codepage *cp)
244 int n = 0;
246 smb_lock_server(server);
248 /* Don't load any nls_* at all, if no remote is requested */
249 if (!*cp->remote_name)
250 goto out;
252 /* local */
253 n = setcodepage(&server->local_nls, cp->local_name);
254 if (n != 0)
255 goto out;
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);
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;
272 smb_unlock_server(server);
273 return n;
277 /*****************************************************************************/
278 /* */
279 /* Encoding/Decoding section */
280 /* */
281 /*****************************************************************************/
283 static __u8 *
284 smb_encode_smb_length(__u8 * p, __u32 len)
286 *p = 0;
287 *(p+1) = 0;
288 *(p+2) = (len & 0xFF00) >> 8;
289 *(p+3) = (len & 0xFF);
290 if (len > 0xFFFF)
292 *(p+1) = 1;
294 return p + 4;
298 * smb_build_path: build the path to entry and name storing it in buf.
299 * The path returned will have the trailing '\0'.
301 static int smb_build_path(struct smb_sb_info *server, unsigned char *buf,
302 int maxlen,
303 struct dentry *entry, struct qstr *name)
305 unsigned char *path = buf;
306 int len;
307 int unicode = (server->mnt->flags & SMB_MOUNT_UNICODE) != 0;
309 if (maxlen < (2<<unicode))
310 return -ENAMETOOLONG;
312 if (maxlen > SMB_MAXPATHLEN + 1)
313 maxlen = SMB_MAXPATHLEN + 1;
315 if (entry == NULL)
316 goto test_name_and_out;
319 * If IS_ROOT, we have to do no walking at all.
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;
330 * Build the path string walking the tree backward from end to ROOT
331 * and store it in reversed order [see reverse_string()]
333 dget(entry);
334 while (!IS_ROOT(entry)) {
335 struct dentry *parent;
337 if (maxlen < (3<<unicode)) {
338 dput(entry);
339 return -ENAMETOOLONG;
342 spin_lock(&entry->d_lock);
343 len = server->ops->convert(path, maxlen-2,
344 entry->d_name.name, entry->d_name.len,
345 server->local_nls, server->remote_nls);
346 if (len < 0) {
347 spin_unlock(&entry->d_lock);
348 dput(entry);
349 return len;
351 reverse_string(path, len);
352 path += len;
353 if (unicode) {
354 /* Note: reverse order */
355 *path++ = '\0';
356 maxlen--;
358 *path++ = '\\';
359 maxlen -= len+1;
360 spin_unlock(&entry->d_lock);
362 parent = dget_parent(entry);
363 dput(entry);
364 entry = parent;
366 dput(entry);
367 reverse_string(buf, path-buf);
369 /* maxlen has space for at least one char */
370 test_name_and_out:
371 if (name) {
372 if (maxlen < (3<<unicode))
373 return -ENAMETOOLONG;
374 *path++ = '\\';
375 if (unicode) {
376 *path++ = '\0';
377 maxlen--;
379 len = server->ops->convert(path, maxlen-2,
380 name->name, name->len,
381 server->local_nls, server->remote_nls);
382 if (len < 0)
383 return len;
384 path += len;
385 maxlen -= len+1;
387 /* maxlen has space for at least one char */
388 *path++ = '\0';
389 if (unicode) *path++ = '\0';
390 return path-buf;
393 static int smb_encode_path(struct smb_sb_info *server, char *buf, int maxlen,
394 struct dentry *dir, struct qstr *name)
396 int result;
398 result = smb_build_path(server, buf, maxlen, dir, name);
399 if (result < 0)
400 goto out;
401 if (server->opt.protocol <= SMB_PROTOCOL_COREPLUS)
402 str_upper(buf, result);
403 out:
404 return result;
407 /* encode_path for non-trans2 request SMBs */
408 static int smb_simple_encode_path(struct smb_request *req, char **p,
409 struct dentry * entry, struct qstr * name)
411 struct smb_sb_info *server = req->rq_server;
412 char *s = *p;
413 int res;
414 int maxlen = ((char *)req->rq_buffer + req->rq_bufsize) - s;
415 int unicode = (server->mnt->flags & SMB_MOUNT_UNICODE);
417 if (!maxlen)
418 return -ENAMETOOLONG;
419 *s++ = 4; /* ASCII data format */
422 * SMB Unicode strings must be 16bit aligned relative the start of the
423 * packet. If they are not they must be padded with 0.
425 if (unicode) {
426 int align = s - (char *)req->rq_buffer;
427 if (!(align & 1)) {
428 *s++ = '\0';
429 maxlen--;
433 res = smb_encode_path(server, s, maxlen-1, entry, name);
434 if (res < 0)
435 return res;
436 *p = s + res;
437 return 0;
440 /* The following are taken directly from msdos-fs */
442 /* Linear day numbers of the respective 1sts in non-leap years. */
444 static int day_n[] =
445 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 0, 0, 0, 0};
446 /* JanFebMarApr May Jun Jul Aug Sep Oct Nov Dec */
449 static time_t
450 utc2local(struct smb_sb_info *server, time_t time)
452 return time - server->opt.serverzone*60;
455 static time_t
456 local2utc(struct smb_sb_info *server, time_t time)
458 return time + server->opt.serverzone*60;
461 /* Convert a MS-DOS time/date pair to a UNIX date (seconds since 1 1 70). */
463 static time_t
464 date_dos2unix(struct smb_sb_info *server, __u16 date, __u16 time)
466 int month, year;
467 time_t secs;
469 /* first subtract and mask after that... Otherwise, if
470 date == 0, bad things happen */
471 month = ((date >> 5) - 1) & 15;
472 year = date >> 9;
473 secs = (time & 31) * 2 + 60 * ((time >> 5) & 63) + (time >> 11) * 3600 + 86400 *
474 ((date & 31) - 1 + day_n[month] + (year / 4) + year * 365 - ((year & 3) == 0 &&
475 month < 2 ? 1 : 0) + 3653);
476 /* days since 1.1.70 plus 80's leap day */
477 return local2utc(server, secs);
481 /* Convert linear UNIX date to a MS-DOS time/date pair. */
483 static void
484 date_unix2dos(struct smb_sb_info *server,
485 int unix_date, __u16 *date, __u16 *time)
487 int day, year, nl_day, month;
489 unix_date = utc2local(server, unix_date);
490 if (unix_date < 315532800)
491 unix_date = 315532800;
493 *time = (unix_date % 60) / 2 +
494 (((unix_date / 60) % 60) << 5) +
495 (((unix_date / 3600) % 24) << 11);
497 day = unix_date / 86400 - 3652;
498 year = day / 365;
499 if ((year + 3) / 4 + 365 * year > day)
500 year--;
501 day -= (year + 3) / 4 + 365 * year;
502 if (day == 59 && !(year & 3)) {
503 nl_day = day;
504 month = 2;
505 } else {
506 nl_day = (year & 3) || day <= 59 ? day : day - 1;
507 for (month = 1; month < 12; month++)
508 if (day_n[month] > nl_day)
509 break;
511 *date = nl_day - day_n[month - 1] + 1 + (month << 5) + (year << 9);
514 /* The following are taken from fs/ntfs/util.c */
516 #define NTFS_TIME_OFFSET ((u64)(369*365 + 89) * 24 * 3600 * 10000000)
519 * Convert the NT UTC (based 1601-01-01, in hundred nanosecond units)
520 * into Unix UTC (based 1970-01-01, in seconds).
522 static struct timespec
523 smb_ntutc2unixutc(u64 ntutc)
525 struct timespec ts;
526 /* FIXME: what about the timezone difference? */
527 /* Subtract the NTFS time offset, then convert to 1s intervals. */
528 u64 t = ntutc - NTFS_TIME_OFFSET;
529 ts.tv_nsec = do_div(t, 10000000) * 100;
530 ts.tv_sec = t;
531 return ts;
534 /* Convert the Unix UTC into NT time */
535 static u64
536 smb_unixutc2ntutc(struct timespec ts)
538 /* Note: timezone conversion is probably wrong. */
539 /* return ((u64)utc2local(server, t)) * 10000000 + NTFS_TIME_OFFSET; */
540 return ((u64)ts.tv_sec) * 10000000 + ts.tv_nsec/100 + NTFS_TIME_OFFSET;
543 #define MAX_FILE_MODE 6
544 static mode_t file_mode[] = {
545 S_IFREG, S_IFDIR, S_IFLNK, S_IFCHR, S_IFBLK, S_IFIFO, S_IFSOCK
548 static int smb_filetype_to_mode(u32 filetype)
550 if (filetype > MAX_FILE_MODE) {
551 PARANOIA("Filetype out of range: %d\n", filetype);
552 return S_IFREG;
554 return file_mode[filetype];
557 static u32 smb_filetype_from_mode(int mode)
559 if (S_ISREG(mode))
560 return UNIX_TYPE_FILE;
561 if (S_ISDIR(mode))
562 return UNIX_TYPE_DIR;
563 if (S_ISLNK(mode))
564 return UNIX_TYPE_SYMLINK;
565 if (S_ISCHR(mode))
566 return UNIX_TYPE_CHARDEV;
567 if (S_ISBLK(mode))
568 return UNIX_TYPE_BLKDEV;
569 if (S_ISFIFO(mode))
570 return UNIX_TYPE_FIFO;
571 if (S_ISSOCK(mode))
572 return UNIX_TYPE_SOCKET;
573 return UNIX_TYPE_UNKNOWN;
577 /*****************************************************************************/
578 /* */
579 /* Support section. */
580 /* */
581 /*****************************************************************************/
583 __u32
584 smb_len(__u8 * p)
586 return ((*(p+1) & 0x1) << 16L) | (*(p+2) << 8L) | *(p+3);
589 static __u16
590 smb_bcc(__u8 * packet)
592 int pos = SMB_HEADER_LEN + SMB_WCT(packet) * sizeof(__u16);
593 return WVAL(packet, pos);
596 /* smb_valid_packet: We check if packet fulfills the basic
597 requirements of a smb packet */
599 static int
600 smb_valid_packet(__u8 * packet)
602 return (packet[4] == 0xff
603 && packet[5] == 'S'
604 && packet[6] == 'M'
605 && packet[7] == 'B'
606 && (smb_len(packet) + 4 == SMB_HEADER_LEN
607 + SMB_WCT(packet) * 2 + smb_bcc(packet)));
610 /* smb_verify: We check if we got the answer we expected, and if we
611 got enough data. If bcc == -1, we don't care. */
613 static int
614 smb_verify(__u8 * packet, int command, int wct, int bcc)
616 if (SMB_CMD(packet) != command)
617 goto bad_command;
618 if (SMB_WCT(packet) < wct)
619 goto bad_wct;
620 if (bcc != -1 && smb_bcc(packet) < bcc)
621 goto bad_bcc;
622 return 0;
624 bad_command:
625 printk(KERN_ERR "smb_verify: command=%x, SMB_CMD=%x??\n",
626 command, SMB_CMD(packet));
627 goto fail;
628 bad_wct:
629 printk(KERN_ERR "smb_verify: command=%x, wct=%d, SMB_WCT=%d??\n",
630 command, wct, SMB_WCT(packet));
631 goto fail;
632 bad_bcc:
633 printk(KERN_ERR "smb_verify: command=%x, bcc=%d, SMB_BCC=%d??\n",
634 command, bcc, smb_bcc(packet));
635 fail:
636 return -EIO;
640 * Returns the maximum read or write size for the "payload". Making all of the
641 * packet fit within the negotiated max_xmit size.
643 * N.B. Since this value is usually computed before locking the server,
644 * the server's packet size must never be decreased!
646 static inline int
647 smb_get_xmitsize(struct smb_sb_info *server, int overhead)
649 return server->opt.max_xmit - overhead;
653 * Calculate the maximum read size
656 smb_get_rsize(struct smb_sb_info *server)
658 /* readX has 12 parameters, read has 5 */
659 int overhead = SMB_HEADER_LEN + 12 * sizeof(__u16) + 2 + 1 + 2;
660 int size = smb_get_xmitsize(server, overhead);
662 VERBOSE("xmit=%d, size=%d\n", server->opt.max_xmit, size);
664 return size;
668 * Calculate the maximum write size
671 smb_get_wsize(struct smb_sb_info *server)
673 /* writeX has 14 parameters, write has 5 */
674 int overhead = SMB_HEADER_LEN + 14 * sizeof(__u16) + 2 + 1 + 2;
675 int size = smb_get_xmitsize(server, overhead);
677 VERBOSE("xmit=%d, size=%d\n", server->opt.max_xmit, size);
679 return size;
683 * Convert SMB error codes to -E... errno values.
686 smb_errno(struct smb_request *req)
688 int errcls = req->rq_rcls;
689 int error = req->rq_err;
690 char *class = "Unknown";
692 VERBOSE("errcls %d code %d from command 0x%x\n",
693 errcls, error, SMB_CMD(req->rq_header));
695 if (errcls == ERRDOS) {
696 switch (error) {
697 case ERRbadfunc:
698 return -EINVAL;
699 case ERRbadfile:
700 case ERRbadpath:
701 return -ENOENT;
702 case ERRnofids:
703 return -EMFILE;
704 case ERRnoaccess:
705 return -EACCES;
706 case ERRbadfid:
707 return -EBADF;
708 case ERRbadmcb:
709 return -EREMOTEIO;
710 case ERRnomem:
711 return -ENOMEM;
712 case ERRbadmem:
713 return -EFAULT;
714 case ERRbadenv:
715 case ERRbadformat:
716 return -EREMOTEIO;
717 case ERRbadaccess:
718 return -EACCES;
719 case ERRbaddata:
720 return -E2BIG;
721 case ERRbaddrive:
722 return -ENXIO;
723 case ERRremcd:
724 return -EREMOTEIO;
725 case ERRdiffdevice:
726 return -EXDEV;
727 case ERRnofiles:
728 return -ENOENT;
729 case ERRbadshare:
730 return -ETXTBSY;
731 case ERRlock:
732 return -EDEADLK;
733 case ERRfilexists:
734 return -EEXIST;
735 case ERROR_INVALID_PARAMETER:
736 return -EINVAL;
737 case ERROR_DISK_FULL:
738 return -ENOSPC;
739 case ERROR_INVALID_NAME:
740 return -ENOENT;
741 case ERROR_DIR_NOT_EMPTY:
742 return -ENOTEMPTY;
743 case ERROR_NOT_LOCKED:
744 return -ENOLCK;
745 case ERROR_ALREADY_EXISTS:
746 return -EEXIST;
747 default:
748 class = "ERRDOS";
749 goto err_unknown;
751 } else if (errcls == ERRSRV) {
752 switch (error) {
753 /* N.B. This is wrong ... EIO ? */
754 case ERRerror:
755 return -ENFILE;
756 case ERRbadpw:
757 return -EINVAL;
758 case ERRbadtype:
759 case ERRtimeout:
760 return -EIO;
761 case ERRaccess:
762 return -EACCES;
764 * This is a fatal error, as it means the "tree ID"
765 * for this connection is no longer valid. We map
766 * to a special error code and get a new connection.
768 case ERRinvnid:
769 return -EBADSLT;
770 default:
771 class = "ERRSRV";
772 goto err_unknown;
774 } else if (errcls == ERRHRD) {
775 switch (error) {
776 case ERRnowrite:
777 return -EROFS;
778 case ERRbadunit:
779 return -ENODEV;
780 case ERRnotready:
781 return -EUCLEAN;
782 case ERRbadcmd:
783 case ERRdata:
784 return -EIO;
785 case ERRbadreq:
786 return -ERANGE;
787 case ERRbadshare:
788 return -ETXTBSY;
789 case ERRlock:
790 return -EDEADLK;
791 case ERRdiskfull:
792 return -ENOSPC;
793 default:
794 class = "ERRHRD";
795 goto err_unknown;
797 } else if (errcls == ERRCMD) {
798 class = "ERRCMD";
799 } else if (errcls == SUCCESS) {
800 return 0; /* This is the only valid 0 return */
803 err_unknown:
804 printk(KERN_ERR "smb_errno: class %s, code %d from command 0x%x\n",
805 class, error, SMB_CMD(req->rq_header));
806 return -EIO;
809 /* smb_request_ok: We expect the server to be locked. Then we do the
810 request and check the answer completely. When smb_request_ok
811 returns 0, you can be quite sure that everything went well. When
812 the answer is <=0, the returned number is a valid unix errno. */
814 static int
815 smb_request_ok(struct smb_request *req, int command, int wct, int bcc)
817 int result;
819 req->rq_resp_wct = wct;
820 req->rq_resp_bcc = bcc;
822 result = smb_add_request(req);
823 if (result != 0) {
824 DEBUG1("smb_request failed\n");
825 goto out;
828 if (smb_valid_packet(req->rq_header) != 0) {
829 PARANOIA("invalid packet!\n");
830 goto out;
833 result = smb_verify(req->rq_header, command, wct, bcc);
835 out:
836 return result;
840 * This implements the NEWCONN ioctl. It installs the server pid,
841 * sets server->state to CONN_VALID, and wakes up the waiting process.
844 smb_newconn(struct smb_sb_info *server, struct smb_conn_opt *opt)
846 struct file *filp;
847 struct sock *sk;
848 int error;
850 VERBOSE("fd=%d, pid=%d\n", opt->fd, current->pid);
852 smb_lock_server(server);
855 * Make sure we don't already have a valid connection ...
857 error = -EINVAL;
858 if (server->state == CONN_VALID)
859 goto out;
861 error = -EACCES;
862 if (current_uid() != server->mnt->mounted_uid &&
863 !capable(CAP_SYS_ADMIN))
864 goto out;
866 error = -EBADF;
867 filp = fget(opt->fd);
868 if (!filp)
869 goto out;
870 if (!smb_valid_socket(filp->f_path.dentry->d_inode))
871 goto out_putf;
873 server->sock_file = filp;
874 server->conn_pid = get_pid(task_pid(current));
875 server->opt = *opt;
876 server->generation += 1;
877 server->state = CONN_VALID;
878 error = 0;
880 if (server->conn_error) {
882 * conn_error is the returncode we originally decided to
883 * drop the old connection on. This message should be positive
884 * and not make people ask questions on why smbfs is printing
885 * error messages ...
887 printk(KERN_INFO "SMB connection re-established (%d)\n",
888 server->conn_error);
889 server->conn_error = 0;
893 * Store the server in sock user_data (Only used by sunrpc)
895 sk = SOCKET_I(filp->f_path.dentry->d_inode)->sk;
896 sk->sk_user_data = server;
898 /* chain into the data_ready callback */
899 server->data_ready = xchg(&sk->sk_data_ready, smb_data_ready);
901 /* check if we have an old smbmount that uses seconds for the
902 serverzone */
903 if (server->opt.serverzone > 12*60 || server->opt.serverzone < -12*60)
904 server->opt.serverzone /= 60;
906 /* now that we have an established connection we can detect the server
907 type and enable bug workarounds */
908 if (server->opt.protocol < SMB_PROTOCOL_LANMAN2)
909 install_ops(server->ops, &smb_ops_core);
910 else if (server->opt.protocol == SMB_PROTOCOL_LANMAN2)
911 install_ops(server->ops, &smb_ops_os2);
912 else if (server->opt.protocol == SMB_PROTOCOL_NT1 &&
913 (server->opt.max_xmit < 0x1000) &&
914 !(server->opt.capabilities & SMB_CAP_NT_SMBS)) {
915 /* FIXME: can we kill the WIN95 flag now? */
916 server->mnt->flags |= SMB_MOUNT_WIN95;
917 VERBOSE("detected WIN95 server\n");
918 install_ops(server->ops, &smb_ops_win95);
919 } else {
921 * Samba has max_xmit 65535
922 * NT4spX has max_xmit 4536 (or something like that)
923 * win2k has ...
925 VERBOSE("detected NT1 (Samba, NT4/5) server\n");
926 install_ops(server->ops, &smb_ops_winNT);
929 /* FIXME: the win9x code wants to modify these ... (seek/trunc bug) */
930 if (server->mnt->flags & SMB_MOUNT_OLDATTR) {
931 server->ops->getattr = smb_proc_getattr_core;
932 } else if (server->mnt->flags & SMB_MOUNT_DIRATTR) {
933 server->ops->getattr = smb_proc_getattr_ff;
936 /* Decode server capabilities */
937 if (server->opt.capabilities & SMB_CAP_LARGE_FILES) {
938 /* Should be ok to set this now, as no one can access the
939 mount until the connection has been established. */
940 SB_of(server)->s_maxbytes = ~0ULL >> 1;
941 VERBOSE("LFS enabled\n");
943 if (server->opt.capabilities & SMB_CAP_UNICODE) {
944 server->mnt->flags |= SMB_MOUNT_UNICODE;
945 VERBOSE("Unicode enabled\n");
946 } else {
947 server->mnt->flags &= ~SMB_MOUNT_UNICODE;
949 #if 0
950 /* flags we may test for other patches ... */
951 if (server->opt.capabilities & SMB_CAP_LARGE_READX) {
952 VERBOSE("Large reads enabled\n");
954 if (server->opt.capabilities & SMB_CAP_LARGE_WRITEX) {
955 VERBOSE("Large writes enabled\n");
957 #endif
958 if (server->opt.capabilities & SMB_CAP_UNIX) {
959 struct inode *inode;
960 VERBOSE("Using UNIX CIFS extensions\n");
961 install_ops(server->ops, &smb_ops_unix);
962 inode = SB_of(server)->s_root->d_inode;
963 if (inode)
964 inode->i_op = &smb_dir_inode_operations_unix;
967 VERBOSE("protocol=%d, max_xmit=%d, pid=%d capabilities=0x%x\n",
968 server->opt.protocol, server->opt.max_xmit,
969 pid_nr(server->conn_pid), server->opt.capabilities);
971 /* FIXME: this really should be done by smbmount. */
972 if (server->opt.max_xmit > SMB_MAX_PACKET_SIZE) {
973 server->opt.max_xmit = SMB_MAX_PACKET_SIZE;
976 smb_unlock_server(server);
977 smbiod_wake_up();
978 if (server->opt.capabilities & SMB_CAP_UNIX)
979 smb_proc_query_cifsunix(server);
981 server->conn_complete++;
982 wake_up_interruptible_all(&server->conn_wq);
983 return error;
985 out:
986 smb_unlock_server(server);
987 smbiod_wake_up();
988 return error;
990 out_putf:
991 fput(filp);
992 goto out;
995 /* smb_setup_header: We completely set up the packet. You only have to
996 insert the command-specific fields */
998 __u8 *
999 smb_setup_header(struct smb_request *req, __u8 command, __u16 wct, __u16 bcc)
1001 __u32 xmit_len = SMB_HEADER_LEN + wct * sizeof(__u16) + bcc + 2;
1002 __u8 *p = req->rq_header;
1003 struct smb_sb_info *server = req->rq_server;
1005 p = smb_encode_smb_length(p, xmit_len - 4);
1007 *p++ = 0xff;
1008 *p++ = 'S';
1009 *p++ = 'M';
1010 *p++ = 'B';
1011 *p++ = command;
1013 memset(p, '\0', 19);
1014 p += 19;
1015 p += 8;
1017 if (server->opt.protocol > SMB_PROTOCOL_CORE) {
1018 int flags = SMB_FLAGS_CASELESS_PATHNAMES;
1019 int flags2 = SMB_FLAGS2_LONG_PATH_COMPONENTS |
1020 SMB_FLAGS2_EXTENDED_ATTRIBUTES; /* EA? not really ... */
1022 *(req->rq_header + smb_flg) = flags;
1023 if (server->mnt->flags & SMB_MOUNT_UNICODE)
1024 flags2 |= SMB_FLAGS2_UNICODE_STRINGS;
1025 WSET(req->rq_header, smb_flg2, flags2);
1027 *p++ = wct; /* wct */
1028 p += 2 * wct;
1029 WSET(p, 0, bcc);
1031 /* Include the header in the data to send */
1032 req->rq_iovlen = 1;
1033 req->rq_iov[0].iov_base = req->rq_header;
1034 req->rq_iov[0].iov_len = xmit_len - bcc;
1036 return req->rq_buffer;
1039 static void
1040 smb_setup_bcc(struct smb_request *req, __u8 *p)
1042 u16 bcc = p - req->rq_buffer;
1043 u8 *pbcc = req->rq_header + SMB_HEADER_LEN + 2*SMB_WCT(req->rq_header);
1045 WSET(pbcc, 0, bcc);
1047 smb_encode_smb_length(req->rq_header, SMB_HEADER_LEN +
1048 2*SMB_WCT(req->rq_header) - 2 + bcc);
1050 /* Include the "bytes" in the data to send */
1051 req->rq_iovlen = 2;
1052 req->rq_iov[1].iov_base = req->rq_buffer;
1053 req->rq_iov[1].iov_len = bcc;
1056 static int
1057 smb_proc_seek(struct smb_sb_info *server, __u16 fileid,
1058 __u16 mode, off_t offset)
1060 int result;
1061 struct smb_request *req;
1063 result = -ENOMEM;
1064 if (! (req = smb_alloc_request(server, 0)))
1065 goto out;
1067 smb_setup_header(req, SMBlseek, 4, 0);
1068 WSET(req->rq_header, smb_vwv0, fileid);
1069 WSET(req->rq_header, smb_vwv1, mode);
1070 DSET(req->rq_header, smb_vwv2, offset);
1071 req->rq_flags |= SMB_REQ_NORETRY;
1073 result = smb_request_ok(req, SMBlseek, 2, 0);
1074 if (result < 0) {
1075 result = 0;
1076 goto out_free;
1079 result = DVAL(req->rq_header, smb_vwv0);
1080 out_free:
1081 smb_rput(req);
1082 out:
1083 return result;
1086 static int
1087 smb_proc_open(struct smb_sb_info *server, struct dentry *dentry, int wish)
1089 struct inode *ino = dentry->d_inode;
1090 struct smb_inode_info *ei = SMB_I(ino);
1091 int mode, read_write = 0x42, read_only = 0x40;
1092 int res;
1093 char *p;
1094 struct smb_request *req;
1097 * Attempt to open r/w, unless there are no write privileges.
1099 mode = read_write;
1100 if (!(ino->i_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1101 mode = read_only;
1102 #if 0
1103 /* FIXME: why is this code not in? below we fix it so that a caller
1104 wanting RO doesn't get RW. smb_revalidate_inode does some
1105 optimization based on access mode. tail -f needs it to be correct.
1107 We must open rw since we don't do the open if called a second time
1108 with different 'wish'. Is that not supported by smb servers? */
1109 if (!(wish & (O_WRONLY | O_RDWR)))
1110 mode = read_only;
1111 #endif
1113 res = -ENOMEM;
1114 if (! (req = smb_alloc_request(server, PAGE_SIZE)))
1115 goto out;
1117 retry:
1118 p = smb_setup_header(req, SMBopen, 2, 0);
1119 WSET(req->rq_header, smb_vwv0, mode);
1120 WSET(req->rq_header, smb_vwv1, aSYSTEM | aHIDDEN | aDIR);
1121 res = smb_simple_encode_path(req, &p, dentry, NULL);
1122 if (res < 0)
1123 goto out_free;
1124 smb_setup_bcc(req, p);
1126 res = smb_request_ok(req, SMBopen, 7, 0);
1127 if (res != 0) {
1128 if (mode == read_write &&
1129 (res == -EACCES || res == -ETXTBSY || res == -EROFS))
1131 VERBOSE("%s/%s R/W failed, error=%d, retrying R/O\n",
1132 DENTRY_PATH(dentry), res);
1133 mode = read_only;
1134 req->rq_flags = 0;
1135 goto retry;
1137 goto out_free;
1139 /* We should now have data in vwv[0..6]. */
1141 ei->fileid = WVAL(req->rq_header, smb_vwv0);
1142 ei->attr = WVAL(req->rq_header, smb_vwv1);
1143 /* smb_vwv2 has mtime */
1144 /* smb_vwv4 has size */
1145 ei->access = (WVAL(req->rq_header, smb_vwv6) & SMB_ACCMASK);
1146 ei->open = server->generation;
1148 out_free:
1149 smb_rput(req);
1150 out:
1151 return res;
1155 * Make sure the file is open, and check that the access
1156 * is compatible with the desired access.
1159 smb_open(struct dentry *dentry, int wish)
1161 struct inode *inode = dentry->d_inode;
1162 int result;
1163 __u16 access;
1165 result = -ENOENT;
1166 if (!inode) {
1167 printk(KERN_ERR "smb_open: no inode for dentry %s/%s\n",
1168 DENTRY_PATH(dentry));
1169 goto out;
1172 if (!smb_is_open(inode)) {
1173 struct smb_sb_info *server = server_from_inode(inode);
1174 result = 0;
1175 if (!smb_is_open(inode))
1176 result = smb_proc_open(server, dentry, wish);
1177 if (result)
1178 goto out;
1180 * A successful open means the path is still valid ...
1182 smb_renew_times(dentry);
1186 * Check whether the access is compatible with the desired mode.
1188 result = 0;
1189 access = SMB_I(inode)->access;
1190 if (access != wish && access != SMB_O_RDWR) {
1191 PARANOIA("%s/%s access denied, access=%x, wish=%x\n",
1192 DENTRY_PATH(dentry), access, wish);
1193 result = -EACCES;
1195 out:
1196 return result;
1199 static int
1200 smb_proc_close(struct smb_sb_info *server, __u16 fileid, __u32 mtime)
1202 struct smb_request *req;
1203 int result = -ENOMEM;
1205 if (! (req = smb_alloc_request(server, 0)))
1206 goto out;
1208 smb_setup_header(req, SMBclose, 3, 0);
1209 WSET(req->rq_header, smb_vwv0, fileid);
1210 DSET(req->rq_header, smb_vwv1, utc2local(server, mtime));
1211 req->rq_flags |= SMB_REQ_NORETRY;
1212 result = smb_request_ok(req, SMBclose, 0, 0);
1214 smb_rput(req);
1215 out:
1216 return result;
1220 * Win NT 4.0 has an apparent bug in that it fails to update the
1221 * modify time when writing to a file. As a workaround, we update
1222 * both modify and access time locally, and post the times to the
1223 * server when closing the file.
1225 static int
1226 smb_proc_close_inode(struct smb_sb_info *server, struct inode * ino)
1228 struct smb_inode_info *ei = SMB_I(ino);
1229 int result = 0;
1230 if (smb_is_open(ino))
1233 * We clear the open flag in advance, in case another
1234 * process observes the value while we block below.
1236 ei->open = 0;
1239 * Kludge alert: SMB timestamps are accurate only to
1240 * two seconds ... round the times to avoid needless
1241 * cache invalidations!
1243 if (ino->i_mtime.tv_sec & 1) {
1244 ino->i_mtime.tv_sec--;
1245 ino->i_mtime.tv_nsec = 0;
1247 if (ino->i_atime.tv_sec & 1) {
1248 ino->i_atime.tv_sec--;
1249 ino->i_atime.tv_nsec = 0;
1252 * If the file is open with write permissions,
1253 * update the time stamps to sync mtime and atime.
1255 if ((server->opt.capabilities & SMB_CAP_UNIX) == 0 &&
1256 (server->opt.protocol >= SMB_PROTOCOL_LANMAN2) &&
1257 !(ei->access == SMB_O_RDONLY))
1259 struct smb_fattr fattr;
1260 smb_get_inode_attr(ino, &fattr);
1261 smb_proc_setattr_ext(server, ino, &fattr);
1264 result = smb_proc_close(server, ei->fileid, ino->i_mtime.tv_sec);
1266 * Force a revalidation after closing ... some servers
1267 * don't post the size until the file has been closed.
1269 if (server->opt.protocol < SMB_PROTOCOL_NT1)
1270 ei->oldmtime = 0;
1271 ei->closed = jiffies;
1273 return result;
1277 smb_close(struct inode *ino)
1279 int result = 0;
1281 if (smb_is_open(ino)) {
1282 struct smb_sb_info *server = server_from_inode(ino);
1283 result = smb_proc_close_inode(server, ino);
1285 return result;
1289 * This is used to close a file following a failed instantiate.
1290 * Since we don't have an inode, we can't use any of the above.
1293 smb_close_fileid(struct dentry *dentry, __u16 fileid)
1295 struct smb_sb_info *server = server_from_dentry(dentry);
1296 int result;
1298 result = smb_proc_close(server, fileid, get_seconds());
1299 return result;
1302 /* In smb_proc_read and smb_proc_write we do not retry, because the
1303 file-id would not be valid after a reconnection. */
1305 static void
1306 smb_proc_read_data(struct smb_request *req)
1308 req->rq_iov[0].iov_base = req->rq_buffer;
1309 req->rq_iov[0].iov_len = 3;
1311 req->rq_iov[1].iov_base = req->rq_page;
1312 req->rq_iov[1].iov_len = req->rq_rsize;
1313 req->rq_iovlen = 2;
1315 req->rq_rlen = smb_len(req->rq_header) + 4 - req->rq_bytes_recvd;
1318 static int
1319 smb_proc_read(struct inode *inode, loff_t offset, int count, char *data)
1321 struct smb_sb_info *server = server_from_inode(inode);
1322 __u16 returned_count, data_len;
1323 unsigned char *buf;
1324 int result;
1325 struct smb_request *req;
1326 u8 rbuf[4];
1328 result = -ENOMEM;
1329 if (! (req = smb_alloc_request(server, 0)))
1330 goto out;
1332 smb_setup_header(req, SMBread, 5, 0);
1333 buf = req->rq_header;
1334 WSET(buf, smb_vwv0, SMB_I(inode)->fileid);
1335 WSET(buf, smb_vwv1, count);
1336 DSET(buf, smb_vwv2, offset);
1337 WSET(buf, smb_vwv4, 0);
1339 req->rq_page = data;
1340 req->rq_rsize = count;
1341 req->rq_callback = smb_proc_read_data;
1342 req->rq_buffer = rbuf;
1343 req->rq_flags |= SMB_REQ_NORETRY | SMB_REQ_STATIC;
1345 result = smb_request_ok(req, SMBread, 5, -1);
1346 if (result < 0)
1347 goto out_free;
1348 returned_count = WVAL(req->rq_header, smb_vwv0);
1350 data_len = WVAL(rbuf, 1);
1352 if (returned_count != data_len) {
1353 printk(KERN_NOTICE "smb_proc_read: returned != data_len\n");
1354 printk(KERN_NOTICE "smb_proc_read: ret_c=%d, data_len=%d\n",
1355 returned_count, data_len);
1357 result = data_len;
1359 out_free:
1360 smb_rput(req);
1361 out:
1362 VERBOSE("ino=%ld, fileid=%d, count=%d, result=%d\n",
1363 inode->i_ino, SMB_I(inode)->fileid, count, result);
1364 return result;
1367 static int
1368 smb_proc_write(struct inode *inode, loff_t offset, int count, const char *data)
1370 struct smb_sb_info *server = server_from_inode(inode);
1371 int result;
1372 u16 fileid = SMB_I(inode)->fileid;
1373 u8 buf[4];
1374 struct smb_request *req;
1376 result = -ENOMEM;
1377 if (! (req = smb_alloc_request(server, 0)))
1378 goto out;
1380 VERBOSE("ino=%ld, fileid=%d, count=%d@%Ld\n",
1381 inode->i_ino, fileid, count, offset);
1383 smb_setup_header(req, SMBwrite, 5, count + 3);
1384 WSET(req->rq_header, smb_vwv0, fileid);
1385 WSET(req->rq_header, smb_vwv1, count);
1386 DSET(req->rq_header, smb_vwv2, offset);
1387 WSET(req->rq_header, smb_vwv4, 0);
1389 buf[0] = 1;
1390 WSET(buf, 1, count); /* yes, again ... */
1391 req->rq_iov[1].iov_base = buf;
1392 req->rq_iov[1].iov_len = 3;
1393 req->rq_iov[2].iov_base = (char *) data;
1394 req->rq_iov[2].iov_len = count;
1395 req->rq_iovlen = 3;
1396 req->rq_flags |= SMB_REQ_NORETRY;
1398 result = smb_request_ok(req, SMBwrite, 1, 0);
1399 if (result >= 0)
1400 result = WVAL(req->rq_header, smb_vwv0);
1402 smb_rput(req);
1403 out:
1404 return result;
1408 * In smb_proc_readX and smb_proc_writeX we do not retry, because the
1409 * file-id would not be valid after a reconnection.
1412 #define SMB_READX_MAX_PAD 64
1413 static void
1414 smb_proc_readX_data(struct smb_request *req)
1416 /* header length, excluding the netbios length (-4) */
1417 int hdrlen = SMB_HEADER_LEN + req->rq_resp_wct*2 - 2;
1418 int data_off = WVAL(req->rq_header, smb_vwv6);
1421 * Some genius made the padding to the data bytes arbitrary.
1422 * So we must first calculate the amount of padding used by the server.
1424 data_off -= hdrlen;
1425 if (data_off > SMB_READX_MAX_PAD || data_off < 0) {
1426 PARANOIA("offset is larger than SMB_READX_MAX_PAD or negative!\n");
1427 PARANOIA("%d > %d || %d < 0\n", data_off, SMB_READX_MAX_PAD, data_off);
1428 req->rq_rlen = req->rq_bufsize + 1;
1429 return;
1431 req->rq_iov[0].iov_base = req->rq_buffer;
1432 req->rq_iov[0].iov_len = data_off;
1434 req->rq_iov[1].iov_base = req->rq_page;
1435 req->rq_iov[1].iov_len = req->rq_rsize;
1436 req->rq_iovlen = 2;
1438 req->rq_rlen = smb_len(req->rq_header) + 4 - req->rq_bytes_recvd;
1441 static int
1442 smb_proc_readX(struct inode *inode, loff_t offset, int count, char *data)
1444 struct smb_sb_info *server = server_from_inode(inode);
1445 unsigned char *buf;
1446 int result;
1447 struct smb_request *req;
1448 static char pad[SMB_READX_MAX_PAD];
1450 result = -ENOMEM;
1451 if (! (req = smb_alloc_request(server, 0)))
1452 goto out;
1454 smb_setup_header(req, SMBreadX, 12, 0);
1455 buf = req->rq_header;
1456 WSET(buf, smb_vwv0, 0x00ff);
1457 WSET(buf, smb_vwv1, 0);
1458 WSET(buf, smb_vwv2, SMB_I(inode)->fileid);
1459 DSET(buf, smb_vwv3, (u32)offset); /* low 32 bits */
1460 WSET(buf, smb_vwv5, count);
1461 WSET(buf, smb_vwv6, 0);
1462 DSET(buf, smb_vwv7, 0);
1463 WSET(buf, smb_vwv9, 0);
1464 DSET(buf, smb_vwv10, (u32)(offset >> 32)); /* high 32 bits */
1465 WSET(buf, smb_vwv11, 0);
1467 req->rq_page = data;
1468 req->rq_rsize = count;
1469 req->rq_callback = smb_proc_readX_data;
1470 req->rq_buffer = pad;
1471 req->rq_bufsize = SMB_READX_MAX_PAD;
1472 req->rq_flags |= SMB_REQ_STATIC | SMB_REQ_NORETRY;
1474 result = smb_request_ok(req, SMBreadX, 12, -1);
1475 if (result < 0)
1476 goto out_free;
1477 result = WVAL(req->rq_header, smb_vwv5);
1479 out_free:
1480 smb_rput(req);
1481 out:
1482 VERBOSE("ino=%ld, fileid=%d, count=%d, result=%d\n",
1483 inode->i_ino, SMB_I(inode)->fileid, count, result);
1484 return result;
1487 static int
1488 smb_proc_writeX(struct inode *inode, loff_t offset, int count, const char *data)
1490 struct smb_sb_info *server = server_from_inode(inode);
1491 int result;
1492 u8 *p;
1493 static u8 pad[4];
1494 struct smb_request *req;
1496 result = -ENOMEM;
1497 if (! (req = smb_alloc_request(server, 0)))
1498 goto out;
1500 VERBOSE("ino=%ld, fileid=%d, count=%d@%Ld\n",
1501 inode->i_ino, SMB_I(inode)->fileid, count, offset);
1503 p = smb_setup_header(req, SMBwriteX, 14, count + 1);
1504 WSET(req->rq_header, smb_vwv0, 0x00ff);
1505 WSET(req->rq_header, smb_vwv1, 0);
1506 WSET(req->rq_header, smb_vwv2, SMB_I(inode)->fileid);
1507 DSET(req->rq_header, smb_vwv3, (u32)offset); /* low 32 bits */
1508 DSET(req->rq_header, smb_vwv5, 0);
1509 WSET(req->rq_header, smb_vwv7, 0); /* write mode */
1510 WSET(req->rq_header, smb_vwv8, 0);
1511 WSET(req->rq_header, smb_vwv9, 0);
1512 WSET(req->rq_header, smb_vwv10, count); /* data length */
1513 WSET(req->rq_header, smb_vwv11, smb_vwv12 + 2 + 1);
1514 DSET(req->rq_header, smb_vwv12, (u32)(offset >> 32));
1516 req->rq_iov[1].iov_base = pad;
1517 req->rq_iov[1].iov_len = 1;
1518 req->rq_iov[2].iov_base = (char *) data;
1519 req->rq_iov[2].iov_len = count;
1520 req->rq_iovlen = 3;
1521 req->rq_flags |= SMB_REQ_NORETRY;
1523 result = smb_request_ok(req, SMBwriteX, 6, 0);
1524 if (result >= 0)
1525 result = WVAL(req->rq_header, smb_vwv2);
1527 smb_rput(req);
1528 out:
1529 return result;
1533 smb_proc_create(struct dentry *dentry, __u16 attr, time_t ctime, __u16 *fileid)
1535 struct smb_sb_info *server = server_from_dentry(dentry);
1536 char *p;
1537 int result;
1538 struct smb_request *req;
1540 result = -ENOMEM;
1541 if (! (req = smb_alloc_request(server, PAGE_SIZE)))
1542 goto out;
1544 p = smb_setup_header(req, SMBcreate, 3, 0);
1545 WSET(req->rq_header, smb_vwv0, attr);
1546 DSET(req->rq_header, smb_vwv1, utc2local(server, ctime));
1547 result = smb_simple_encode_path(req, &p, dentry, NULL);
1548 if (result < 0)
1549 goto out_free;
1550 smb_setup_bcc(req, p);
1552 result = smb_request_ok(req, SMBcreate, 1, 0);
1553 if (result < 0)
1554 goto out_free;
1556 *fileid = WVAL(req->rq_header, smb_vwv0);
1557 result = 0;
1559 out_free:
1560 smb_rput(req);
1561 out:
1562 return result;
1566 smb_proc_mv(struct dentry *old_dentry, struct dentry *new_dentry)
1568 struct smb_sb_info *server = server_from_dentry(old_dentry);
1569 char *p;
1570 int result;
1571 struct smb_request *req;
1573 result = -ENOMEM;
1574 if (! (req = smb_alloc_request(server, PAGE_SIZE)))
1575 goto out;
1577 p = smb_setup_header(req, SMBmv, 1, 0);
1578 WSET(req->rq_header, smb_vwv0, aSYSTEM | aHIDDEN | aDIR);
1579 result = smb_simple_encode_path(req, &p, old_dentry, NULL);
1580 if (result < 0)
1581 goto out_free;
1582 result = smb_simple_encode_path(req, &p, new_dentry, NULL);
1583 if (result < 0)
1584 goto out_free;
1585 smb_setup_bcc(req, p);
1587 if ((result = smb_request_ok(req, SMBmv, 0, 0)) < 0)
1588 goto out_free;
1589 result = 0;
1591 out_free:
1592 smb_rput(req);
1593 out:
1594 return result;
1598 * Code common to mkdir and rmdir.
1600 static int
1601 smb_proc_generic_command(struct dentry *dentry, __u8 command)
1603 struct smb_sb_info *server = server_from_dentry(dentry);
1604 char *p;
1605 int result;
1606 struct smb_request *req;
1608 result = -ENOMEM;
1609 if (! (req = smb_alloc_request(server, PAGE_SIZE)))
1610 goto out;
1612 p = smb_setup_header(req, command, 0, 0);
1613 result = smb_simple_encode_path(req, &p, dentry, NULL);
1614 if (result < 0)
1615 goto out_free;
1616 smb_setup_bcc(req, p);
1618 result = smb_request_ok(req, command, 0, 0);
1619 if (result < 0)
1620 goto out_free;
1621 result = 0;
1623 out_free:
1624 smb_rput(req);
1625 out:
1626 return result;
1630 smb_proc_mkdir(struct dentry *dentry)
1632 return smb_proc_generic_command(dentry, SMBmkdir);
1636 smb_proc_rmdir(struct dentry *dentry)
1638 return smb_proc_generic_command(dentry, SMBrmdir);
1641 #if SMBFS_POSIX_UNLINK
1643 * Removes readonly attribute from a file. Used by unlink to give posix
1644 * semantics.
1646 static int
1647 smb_set_rw(struct dentry *dentry,struct smb_sb_info *server)
1649 int result;
1650 struct smb_fattr fattr;
1652 /* FIXME: cifsUE should allow removing a readonly file. */
1654 /* first get current attribute */
1655 smb_init_dirent(server, &fattr);
1656 result = server->ops->getattr(server, dentry, &fattr);
1657 smb_finish_dirent(server, &fattr);
1658 if (result < 0)
1659 return result;
1661 /* if RONLY attribute is set, remove it */
1662 if (fattr.attr & aRONLY) { /* read only attribute is set */
1663 fattr.attr &= ~aRONLY;
1664 result = smb_proc_setattr_core(server, dentry, fattr.attr);
1666 return result;
1668 #endif
1671 smb_proc_unlink(struct dentry *dentry)
1673 struct smb_sb_info *server = server_from_dentry(dentry);
1674 int flag = 0;
1675 char *p;
1676 int result;
1677 struct smb_request *req;
1679 result = -ENOMEM;
1680 if (! (req = smb_alloc_request(server, PAGE_SIZE)))
1681 goto out;
1683 retry:
1684 p = smb_setup_header(req, SMBunlink, 1, 0);
1685 WSET(req->rq_header, smb_vwv0, aSYSTEM | aHIDDEN);
1686 result = smb_simple_encode_path(req, &p, dentry, NULL);
1687 if (result < 0)
1688 goto out_free;
1689 smb_setup_bcc(req, p);
1691 if ((result = smb_request_ok(req, SMBunlink, 0, 0)) < 0) {
1692 #if SMBFS_POSIX_UNLINK
1693 if (result == -EACCES && !flag) {
1694 /* Posix semantics is for the read-only state
1695 of a file to be ignored in unlink(). In the
1696 SMB world a unlink() is refused on a
1697 read-only file. To make things easier for
1698 unix users we try to override the files
1699 permission if the unlink fails with the
1700 right error.
1701 This introduces a race condition that could
1702 lead to a file being written by someone who
1703 shouldn't have access, but as far as I can
1704 tell that is unavoidable */
1706 /* remove RONLY attribute and try again */
1707 result = smb_set_rw(dentry,server);
1708 if (result == 0) {
1709 flag = 1;
1710 req->rq_flags = 0;
1711 goto retry;
1714 #endif
1715 goto out_free;
1717 result = 0;
1719 out_free:
1720 smb_rput(req);
1721 out:
1722 return result;
1726 smb_proc_flush(struct smb_sb_info *server, __u16 fileid)
1728 int result;
1729 struct smb_request *req;
1731 result = -ENOMEM;
1732 if (! (req = smb_alloc_request(server, 0)))
1733 goto out;
1735 smb_setup_header(req, SMBflush, 1, 0);
1736 WSET(req->rq_header, smb_vwv0, fileid);
1737 req->rq_flags |= SMB_REQ_NORETRY;
1738 result = smb_request_ok(req, SMBflush, 0, 0);
1740 smb_rput(req);
1741 out:
1742 return result;
1745 static int
1746 smb_proc_trunc32(struct inode *inode, loff_t length)
1749 * Writing 0bytes is old-SMB magic for truncating files.
1750 * MAX_NON_LFS should prevent this from being called with a too
1751 * large offset.
1753 return smb_proc_write(inode, length, 0, NULL);
1756 static int
1757 smb_proc_trunc64(struct inode *inode, loff_t length)
1759 struct smb_sb_info *server = server_from_inode(inode);
1760 int result;
1761 char *param;
1762 char *data;
1763 struct smb_request *req;
1765 result = -ENOMEM;
1766 if (! (req = smb_alloc_request(server, 14)))
1767 goto out;
1769 param = req->rq_buffer;
1770 data = req->rq_buffer + 6;
1772 /* FIXME: must we also set allocation size? winNT seems to do that */
1773 WSET(param, 0, SMB_I(inode)->fileid);
1774 WSET(param, 2, SMB_SET_FILE_END_OF_FILE_INFO);
1775 WSET(param, 4, 0);
1776 LSET(data, 0, length);
1778 req->rq_trans2_command = TRANSACT2_SETFILEINFO;
1779 req->rq_ldata = 8;
1780 req->rq_data = data;
1781 req->rq_lparm = 6;
1782 req->rq_parm = param;
1783 req->rq_flags |= SMB_REQ_NORETRY;
1784 result = smb_add_request(req);
1785 if (result < 0)
1786 goto out_free;
1788 result = 0;
1789 if (req->rq_rcls != 0)
1790 result = smb_errno(req);
1792 out_free:
1793 smb_rput(req);
1794 out:
1795 return result;
1798 static int
1799 smb_proc_trunc95(struct inode *inode, loff_t length)
1801 struct smb_sb_info *server = server_from_inode(inode);
1802 int result = smb_proc_trunc32(inode, length);
1805 * win9x doesn't appear to update the size immediately.
1806 * It will return the old file size after the truncate,
1807 * confusing smbfs. So we force an update.
1809 * FIXME: is this still necessary?
1811 smb_proc_flush(server, SMB_I(inode)->fileid);
1812 return result;
1815 static void
1816 smb_init_dirent(struct smb_sb_info *server, struct smb_fattr *fattr)
1818 memset(fattr, 0, sizeof(*fattr));
1820 fattr->f_nlink = 1;
1821 fattr->f_uid = server->mnt->uid;
1822 fattr->f_gid = server->mnt->gid;
1823 fattr->f_unix = 0;
1826 static void
1827 smb_finish_dirent(struct smb_sb_info *server, struct smb_fattr *fattr)
1829 if (fattr->f_unix)
1830 return;
1832 fattr->f_mode = server->mnt->file_mode;
1833 if (fattr->attr & aDIR) {
1834 fattr->f_mode = server->mnt->dir_mode;
1835 fattr->f_size = SMB_ST_BLKSIZE;
1837 /* Check the read-only flag */
1838 if (fattr->attr & aRONLY)
1839 fattr->f_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
1841 /* How many 512 byte blocks do we need for this file? */
1842 fattr->f_blocks = 0;
1843 if (fattr->f_size != 0)
1844 fattr->f_blocks = 1 + ((fattr->f_size-1) >> 9);
1845 return;
1848 void
1849 smb_init_root_dirent(struct smb_sb_info *server, struct smb_fattr *fattr,
1850 struct super_block *sb)
1852 smb_init_dirent(server, fattr);
1853 fattr->attr = aDIR;
1854 fattr->f_ino = 2; /* traditional root inode number */
1855 fattr->f_mtime = current_fs_time(sb);
1856 smb_finish_dirent(server, fattr);
1860 * Decode a dirent for old protocols
1862 * qname is filled with the decoded, and possibly translated, name.
1863 * fattr receives decoded attributes
1865 * Bugs Noted:
1866 * (1) Pathworks servers may pad the name with extra spaces.
1868 static char *
1869 smb_decode_short_dirent(struct smb_sb_info *server, char *p,
1870 struct qstr *qname, struct smb_fattr *fattr,
1871 unsigned char *name_buf)
1873 int len;
1876 * SMB doesn't have a concept of inode numbers ...
1878 smb_init_dirent(server, fattr);
1879 fattr->f_ino = 0; /* FIXME: do we need this? */
1881 p += SMB_STATUS_SIZE; /* reserved (search_status) */
1882 fattr->attr = *p;
1883 fattr->f_mtime.tv_sec = date_dos2unix(server, WVAL(p, 3), WVAL(p, 1));
1884 fattr->f_mtime.tv_nsec = 0;
1885 fattr->f_size = DVAL(p, 5);
1886 fattr->f_ctime = fattr->f_mtime;
1887 fattr->f_atime = fattr->f_mtime;
1888 qname->name = p + 9;
1889 len = strnlen(qname->name, 12);
1892 * Trim trailing blanks for Pathworks servers
1894 while (len > 2 && qname->name[len-1] == ' ')
1895 len--;
1897 smb_finish_dirent(server, fattr);
1899 #if 0
1900 /* FIXME: These only work for ascii chars, and recent smbmount doesn't
1901 allow the flag to be set anyway. It kills const. Remove? */
1902 switch (server->opt.case_handling) {
1903 case SMB_CASE_UPPER:
1904 str_upper(entry->name, len);
1905 break;
1906 case SMB_CASE_LOWER:
1907 str_lower(entry->name, len);
1908 break;
1909 default:
1910 break;
1912 #endif
1914 qname->len = 0;
1915 len = server->ops->convert(name_buf, SMB_MAXNAMELEN,
1916 qname->name, len,
1917 server->remote_nls, server->local_nls);
1918 if (len > 0) {
1919 qname->len = len;
1920 qname->name = name_buf;
1921 DEBUG1("len=%d, name=%.*s\n",qname->len,qname->len,qname->name);
1924 return p + 22;
1928 * This routine is used to read in directory entries from the network.
1929 * Note that it is for short directory name seeks, i.e.: protocol <
1930 * SMB_PROTOCOL_LANMAN2
1932 static int
1933 smb_proc_readdir_short(struct file *filp, void *dirent, filldir_t filldir,
1934 struct smb_cache_control *ctl)
1936 struct dentry *dir = filp->f_path.dentry;
1937 struct smb_sb_info *server = server_from_dentry(dir);
1938 struct qstr qname;
1939 struct smb_fattr fattr;
1940 char *p;
1941 int result;
1942 int i, first, entries_seen, entries;
1943 int entries_asked = (server->opt.max_xmit - 100) / SMB_DIRINFO_SIZE;
1944 __u16 bcc;
1945 __u16 count;
1946 char status[SMB_STATUS_SIZE];
1947 static struct qstr mask = {
1948 .name = "*.*",
1949 .len = 3,
1951 unsigned char *last_status;
1952 struct smb_request *req;
1953 unsigned char *name_buf;
1955 VERBOSE("%s/%s\n", DENTRY_PATH(dir));
1957 lock_kernel();
1959 result = -ENOMEM;
1960 if (! (name_buf = kmalloc(SMB_MAXNAMELEN, GFP_KERNEL)))
1961 goto out;
1963 first = 1;
1964 entries = 0;
1965 entries_seen = 2; /* implicit . and .. */
1967 result = -ENOMEM;
1968 if (! (req = smb_alloc_request(server, server->opt.max_xmit)))
1969 goto out_name;
1971 while (1) {
1972 p = smb_setup_header(req, SMBsearch, 2, 0);
1973 WSET(req->rq_header, smb_vwv0, entries_asked);
1974 WSET(req->rq_header, smb_vwv1, aDIR);
1975 if (first == 1) {
1976 result = smb_simple_encode_path(req, &p, dir, &mask);
1977 if (result < 0)
1978 goto out_free;
1979 if (p + 3 > (char *)req->rq_buffer + req->rq_bufsize) {
1980 result = -ENAMETOOLONG;
1981 goto out_free;
1983 *p++ = 5;
1984 WSET(p, 0, 0);
1985 p += 2;
1986 first = 0;
1987 } else {
1988 if (p + 5 + SMB_STATUS_SIZE >
1989 (char *)req->rq_buffer + req->rq_bufsize) {
1990 result = -ENAMETOOLONG;
1991 goto out_free;
1994 *p++ = 4;
1995 *p++ = 0;
1996 *p++ = 5;
1997 WSET(p, 0, SMB_STATUS_SIZE);
1998 p += 2;
1999 memcpy(p, status, SMB_STATUS_SIZE);
2000 p += SMB_STATUS_SIZE;
2003 smb_setup_bcc(req, p);
2005 result = smb_request_ok(req, SMBsearch, 1, -1);
2006 if (result < 0) {
2007 if ((req->rq_rcls == ERRDOS) &&
2008 (req->rq_err == ERRnofiles))
2009 break;
2010 goto out_free;
2012 count = WVAL(req->rq_header, smb_vwv0);
2013 if (count <= 0)
2014 break;
2016 result = -EIO;
2017 bcc = smb_bcc(req->rq_header);
2018 if (bcc != count * SMB_DIRINFO_SIZE + 3)
2019 goto out_free;
2020 p = req->rq_buffer + 3;
2023 /* Make sure the response fits in the buffer. Fixed sized
2024 entries means we don't have to check in the decode loop. */
2026 last_status = req->rq_buffer + 3 + (count-1) * SMB_DIRINFO_SIZE;
2028 if (last_status + SMB_DIRINFO_SIZE >=
2029 req->rq_buffer + req->rq_bufsize) {
2030 printk(KERN_ERR "smb_proc_readdir_short: "
2031 "last dir entry outside buffer! "
2032 "%d@%p %d@%p\n", SMB_DIRINFO_SIZE, last_status,
2033 req->rq_bufsize, req->rq_buffer);
2034 goto out_free;
2037 /* Read the last entry into the status field. */
2038 memcpy(status, last_status, SMB_STATUS_SIZE);
2041 /* Now we are ready to parse smb directory entries. */
2043 for (i = 0; i < count; i++) {
2044 p = smb_decode_short_dirent(server, p,
2045 &qname, &fattr, name_buf);
2046 if (qname.len == 0)
2047 continue;
2049 if (entries_seen == 2 && qname.name[0] == '.') {
2050 if (qname.len == 1)
2051 continue;
2052 if (qname.name[1] == '.' && qname.len == 2)
2053 continue;
2055 if (!smb_fill_cache(filp, dirent, filldir, ctl,
2056 &qname, &fattr))
2057 ; /* stop reading? */
2058 entries_seen++;
2061 result = entries;
2063 out_free:
2064 smb_rput(req);
2065 out_name:
2066 kfree(name_buf);
2067 out:
2068 unlock_kernel();
2069 return result;
2072 static void smb_decode_unix_basic(struct smb_fattr *fattr, struct smb_sb_info *server, char *p)
2074 u64 size, disk_bytes;
2076 /* FIXME: verify nls support. all is sent as utf8? */
2078 fattr->f_unix = 1;
2079 fattr->f_mode = 0;
2081 /* FIXME: use the uniqueID from the remote instead? */
2082 /* 0 L file size in bytes */
2083 /* 8 L file size on disk in bytes (block count) */
2084 /* 40 L uid */
2085 /* 48 L gid */
2086 /* 56 W file type */
2087 /* 60 L devmajor */
2088 /* 68 L devminor */
2089 /* 76 L unique ID (inode) */
2090 /* 84 L permissions */
2091 /* 92 L link count */
2093 size = LVAL(p, 0);
2094 disk_bytes = LVAL(p, 8);
2097 * Some samba versions round up on-disk byte usage
2098 * to 1MB boundaries, making it useless. When seeing
2099 * that, use the size instead.
2101 if (!(disk_bytes & 0xfffff))
2102 disk_bytes = size+511;
2104 fattr->f_size = size;
2105 fattr->f_blocks = disk_bytes >> 9;
2106 fattr->f_ctime = smb_ntutc2unixutc(LVAL(p, 16));
2107 fattr->f_atime = smb_ntutc2unixutc(LVAL(p, 24));
2108 fattr->f_mtime = smb_ntutc2unixutc(LVAL(p, 32));
2110 if (server->mnt->flags & SMB_MOUNT_UID)
2111 fattr->f_uid = server->mnt->uid;
2112 else
2113 fattr->f_uid = LVAL(p, 40);
2115 if (server->mnt->flags & SMB_MOUNT_GID)
2116 fattr->f_gid = server->mnt->gid;
2117 else
2118 fattr->f_gid = LVAL(p, 48);
2120 fattr->f_mode |= smb_filetype_to_mode(WVAL(p, 56));
2122 if (S_ISBLK(fattr->f_mode) || S_ISCHR(fattr->f_mode)) {
2123 __u64 major = LVAL(p, 60);
2124 __u64 minor = LVAL(p, 68);
2126 fattr->f_rdev = MKDEV(major & 0xffffffff, minor & 0xffffffff);
2127 if (MAJOR(fattr->f_rdev) != (major & 0xffffffff) ||
2128 MINOR(fattr->f_rdev) != (minor & 0xffffffff))
2129 fattr->f_rdev = 0;
2132 fattr->f_mode |= LVAL(p, 84);
2134 if ( (server->mnt->flags & SMB_MOUNT_DMODE) &&
2135 (S_ISDIR(fattr->f_mode)) )
2136 fattr->f_mode = (server->mnt->dir_mode & S_IRWXUGO) | S_IFDIR;
2137 else if ( (server->mnt->flags & SMB_MOUNT_FMODE) &&
2138 !(S_ISDIR(fattr->f_mode)) )
2139 fattr->f_mode = (server->mnt->file_mode & S_IRWXUGO) |
2140 (fattr->f_mode & S_IFMT);
2145 * Interpret a long filename structure using the specified info level:
2146 * level 1 for anything below NT1 protocol
2147 * level 260 for NT1 protocol
2149 * qname is filled with the decoded, and possibly translated, name
2150 * fattr receives decoded attributes.
2152 * Bugs Noted:
2153 * (1) Win NT 4.0 appends a null byte to names and counts it in the length!
2155 static char *
2156 smb_decode_long_dirent(struct smb_sb_info *server, char *p, int level,
2157 struct qstr *qname, struct smb_fattr *fattr,
2158 unsigned char *name_buf)
2160 char *result;
2161 unsigned int len = 0;
2162 int n;
2163 __u16 date, time;
2164 int unicode = (server->mnt->flags & SMB_MOUNT_UNICODE);
2167 * SMB doesn't have a concept of inode numbers ...
2169 smb_init_dirent(server, fattr);
2170 fattr->f_ino = 0; /* FIXME: do we need this? */
2172 switch (level) {
2173 case 1:
2174 len = *((unsigned char *) p + 22);
2175 qname->name = p + 23;
2176 result = p + 24 + len;
2178 date = WVAL(p, 0);
2179 time = WVAL(p, 2);
2180 fattr->f_ctime.tv_sec = date_dos2unix(server, date, time);
2181 fattr->f_ctime.tv_nsec = 0;
2183 date = WVAL(p, 4);
2184 time = WVAL(p, 6);
2185 fattr->f_atime.tv_sec = date_dos2unix(server, date, time);
2186 fattr->f_atime.tv_nsec = 0;
2188 date = WVAL(p, 8);
2189 time = WVAL(p, 10);
2190 fattr->f_mtime.tv_sec = date_dos2unix(server, date, time);
2191 fattr->f_mtime.tv_nsec = 0;
2192 fattr->f_size = DVAL(p, 12);
2193 /* ULONG allocation size */
2194 fattr->attr = WVAL(p, 20);
2196 VERBOSE("info 1 at %p, len=%d, name=%.*s\n",
2197 p, len, len, qname->name);
2198 break;
2199 case 260:
2200 result = p + WVAL(p, 0);
2201 len = DVAL(p, 60);
2202 if (len > 255) len = 255;
2203 /* NT4 null terminates, unless we are using unicode ... */
2204 qname->name = p + 94;
2205 if (!unicode && len && qname->name[len-1] == '\0')
2206 len--;
2208 fattr->f_ctime = smb_ntutc2unixutc(LVAL(p, 8));
2209 fattr->f_atime = smb_ntutc2unixutc(LVAL(p, 16));
2210 fattr->f_mtime = smb_ntutc2unixutc(LVAL(p, 24));
2211 /* change time (32) */
2212 fattr->f_size = LVAL(p, 40);
2213 /* alloc size (48) */
2214 fattr->attr = DVAL(p, 56);
2216 VERBOSE("info 260 at %p, len=%d, name=%.*s\n",
2217 p, len, len, qname->name);
2218 break;
2219 case SMB_FIND_FILE_UNIX:
2220 result = p + WVAL(p, 0);
2221 qname->name = p + 108;
2223 len = strlen(qname->name);
2224 /* FIXME: should we check the length?? */
2226 p += 8;
2227 smb_decode_unix_basic(fattr, server, p);
2228 VERBOSE("info SMB_FIND_FILE_UNIX at %p, len=%d, name=%.*s\n",
2229 p, len, len, qname->name);
2230 break;
2231 default:
2232 PARANOIA("Unknown info level %d\n", level);
2233 result = p + WVAL(p, 0);
2234 goto out;
2237 smb_finish_dirent(server, fattr);
2239 #if 0
2240 /* FIXME: These only work for ascii chars, and recent smbmount doesn't
2241 allow the flag to be set anyway. Remove? */
2242 switch (server->opt.case_handling) {
2243 case SMB_CASE_UPPER:
2244 str_upper(qname->name, len);
2245 break;
2246 case SMB_CASE_LOWER:
2247 str_lower(qname->name, len);
2248 break;
2249 default:
2250 break;
2252 #endif
2254 qname->len = 0;
2255 n = server->ops->convert(name_buf, SMB_MAXNAMELEN,
2256 qname->name, len,
2257 server->remote_nls, server->local_nls);
2258 if (n > 0) {
2259 qname->len = n;
2260 qname->name = name_buf;
2263 out:
2264 return result;
2267 /* findfirst/findnext flags */
2268 #define SMB_CLOSE_AFTER_FIRST (1<<0)
2269 #define SMB_CLOSE_IF_END (1<<1)
2270 #define SMB_REQUIRE_RESUME_KEY (1<<2)
2271 #define SMB_CONTINUE_BIT (1<<3)
2274 * Note: samba-2.0.7 (at least) has a very similar routine, cli_list, in
2275 * source/libsmb/clilist.c. When looking for smb bugs in the readdir code,
2276 * go there for advise.
2278 * Bugs Noted:
2279 * (1) When using Info Level 1 Win NT 4.0 truncates directory listings
2280 * for certain patterns of names and/or lengths. The breakage pattern
2281 * is completely reproducible and can be toggled by the creation of a
2282 * single file. (E.g. echo hi >foo breaks, rm -f foo works.)
2284 static int
2285 smb_proc_readdir_long(struct file *filp, void *dirent, filldir_t filldir,
2286 struct smb_cache_control *ctl)
2288 struct dentry *dir = filp->f_path.dentry;
2289 struct smb_sb_info *server = server_from_dentry(dir);
2290 struct qstr qname;
2291 struct smb_fattr fattr;
2293 unsigned char *p, *lastname;
2294 char *mask, *param;
2295 __u16 command;
2296 int first, entries_seen;
2298 /* Both NT and OS/2 accept info level 1 (but see note below). */
2299 int info_level = 260;
2300 const int max_matches = 512;
2302 unsigned int ff_searchcount = 0;
2303 unsigned int ff_eos = 0;
2304 unsigned int ff_lastname = 0;
2305 unsigned int ff_dir_handle = 0;
2306 unsigned int loop_count = 0;
2307 unsigned int mask_len, i;
2308 int result;
2309 struct smb_request *req;
2310 unsigned char *name_buf;
2311 static struct qstr star = {
2312 .name = "*",
2313 .len = 1,
2316 lock_kernel();
2319 * We always prefer unix style. Use info level 1 for older
2320 * servers that don't do 260.
2322 if (server->opt.capabilities & SMB_CAP_UNIX)
2323 info_level = SMB_FIND_FILE_UNIX;
2324 else if (server->opt.protocol < SMB_PROTOCOL_NT1)
2325 info_level = 1;
2327 result = -ENOMEM;
2328 if (! (name_buf = kmalloc(SMB_MAXNAMELEN+2, GFP_KERNEL)))
2329 goto out;
2330 if (! (req = smb_alloc_request(server, server->opt.max_xmit)))
2331 goto out_name;
2332 param = req->rq_buffer;
2335 * Encode the initial path
2337 mask = param + 12;
2339 result = smb_encode_path(server, mask, SMB_MAXPATHLEN+1, dir, &star);
2340 if (result <= 0)
2341 goto out_free;
2342 mask_len = result - 1; /* mask_len is strlen, not #bytes */
2343 result = 0;
2344 first = 1;
2345 VERBOSE("starting mask_len=%d, mask=%s\n", mask_len, mask);
2347 entries_seen = 2;
2348 ff_eos = 0;
2350 while (ff_eos == 0) {
2351 loop_count += 1;
2352 if (loop_count > 10) {
2353 printk(KERN_WARNING "smb_proc_readdir_long: "
2354 "Looping in FIND_NEXT??\n");
2355 result = -EIO;
2356 break;
2359 if (first != 0) {
2360 command = TRANSACT2_FINDFIRST;
2361 WSET(param, 0, aSYSTEM | aHIDDEN | aDIR);
2362 WSET(param, 2, max_matches); /* max count */
2363 WSET(param, 4, SMB_CLOSE_IF_END);
2364 WSET(param, 6, info_level);
2365 DSET(param, 8, 0);
2366 } else {
2367 command = TRANSACT2_FINDNEXT;
2369 VERBOSE("handle=0x%X, lastname=%d, mask=%.*s\n",
2370 ff_dir_handle, ff_lastname, mask_len, mask);
2372 WSET(param, 0, ff_dir_handle); /* search handle */
2373 WSET(param, 2, max_matches); /* max count */
2374 WSET(param, 4, info_level);
2375 DSET(param, 6, 0);
2376 WSET(param, 10, SMB_CONTINUE_BIT|SMB_CLOSE_IF_END);
2379 req->rq_trans2_command = command;
2380 req->rq_ldata = 0;
2381 req->rq_data = NULL;
2382 req->rq_lparm = 12 + mask_len + 1;
2383 req->rq_parm = param;
2384 req->rq_flags = 0;
2385 result = smb_add_request(req);
2386 if (result < 0) {
2387 PARANOIA("error=%d, breaking\n", result);
2388 break;
2391 if (req->rq_rcls == ERRSRV && req->rq_err == ERRerror) {
2392 /* a damn Win95 bug - sometimes it clags if you
2393 ask it too fast */
2394 schedule_timeout_interruptible(msecs_to_jiffies(200));
2395 continue;
2398 if (req->rq_rcls != 0) {
2399 result = smb_errno(req);
2400 PARANOIA("name=%s, result=%d, rcls=%d, err=%d\n",
2401 mask, result, req->rq_rcls, req->rq_err);
2402 break;
2405 /* parse out some important return info */
2406 if (first != 0) {
2407 ff_dir_handle = WVAL(req->rq_parm, 0);
2408 ff_searchcount = WVAL(req->rq_parm, 2);
2409 ff_eos = WVAL(req->rq_parm, 4);
2410 ff_lastname = WVAL(req->rq_parm, 8);
2411 } else {
2412 ff_searchcount = WVAL(req->rq_parm, 0);
2413 ff_eos = WVAL(req->rq_parm, 2);
2414 ff_lastname = WVAL(req->rq_parm, 6);
2417 if (ff_searchcount == 0)
2418 break;
2420 /* Now we are ready to parse smb directory entries. */
2422 /* point to the data bytes */
2423 p = req->rq_data;
2424 for (i = 0; i < ff_searchcount; i++) {
2425 /* make sure we stay within the buffer */
2426 if (p >= req->rq_data + req->rq_ldata) {
2427 printk(KERN_ERR "smb_proc_readdir_long: "
2428 "dirent pointer outside buffer! "
2429 "%p %d@%p\n",
2430 p, req->rq_ldata, req->rq_data);
2431 result = -EIO; /* always a comm. error? */
2432 goto out_free;
2435 p = smb_decode_long_dirent(server, p, info_level,
2436 &qname, &fattr, name_buf);
2438 /* ignore . and .. from the server */
2439 if (entries_seen == 2 && qname.name[0] == '.') {
2440 if (qname.len == 1)
2441 continue;
2442 if (qname.name[1] == '.' && qname.len == 2)
2443 continue;
2446 if (!smb_fill_cache(filp, dirent, filldir, ctl,
2447 &qname, &fattr))
2448 ; /* stop reading? */
2449 entries_seen++;
2452 VERBOSE("received %d entries, eos=%d\n", ff_searchcount,ff_eos);
2455 * We might need the lastname for continuations.
2457 * Note that some servers (win95?) point to the filename and
2458 * others (NT4, Samba using NT1) to the dir entry. We assume
2459 * here that those who do not point to a filename do not need
2460 * this info to continue the listing.
2462 * OS/2 needs this and talks infolevel 1.
2463 * NetApps want lastname with infolevel 260.
2464 * win2k want lastname with infolevel 260, and points to
2465 * the record not to the name.
2466 * Samba+CifsUnixExt doesn't need lastname.
2468 * Both are happy if we return the data they point to. So we do.
2469 * (FIXME: above is not true with win2k)
2471 mask_len = 0;
2472 if (info_level != SMB_FIND_FILE_UNIX &&
2473 ff_lastname > 0 && ff_lastname < req->rq_ldata) {
2474 lastname = req->rq_data + ff_lastname;
2476 switch (info_level) {
2477 case 260:
2478 mask_len = req->rq_ldata - ff_lastname;
2479 break;
2480 case 1:
2481 /* lastname points to a length byte */
2482 mask_len = *lastname++;
2483 if (ff_lastname + 1 + mask_len > req->rq_ldata)
2484 mask_len = req->rq_ldata - ff_lastname - 1;
2485 break;
2489 * Update the mask string for the next message.
2491 if (mask_len > 255)
2492 mask_len = 255;
2493 if (mask_len)
2494 strncpy(mask, lastname, mask_len);
2496 mask_len = strnlen(mask, mask_len);
2497 VERBOSE("new mask, len=%d@%d of %d, mask=%.*s\n",
2498 mask_len, ff_lastname, req->rq_ldata, mask_len, mask);
2500 first = 0;
2501 loop_count = 0;
2504 out_free:
2505 smb_rput(req);
2506 out_name:
2507 kfree(name_buf);
2508 out:
2509 unlock_kernel();
2510 return result;
2514 * This version uses the trans2 TRANSACT2_FINDFIRST message
2515 * to get the attribute data.
2517 * Bugs Noted:
2519 static int
2520 smb_proc_getattr_ff(struct smb_sb_info *server, struct dentry *dentry,
2521 struct smb_fattr *fattr)
2523 char *param, *mask;
2524 __u16 date, time;
2525 int mask_len, result;
2526 struct smb_request *req;
2528 result = -ENOMEM;
2529 if (! (req = smb_alloc_request(server, PAGE_SIZE)))
2530 goto out;
2531 param = req->rq_buffer;
2532 mask = param + 12;
2534 mask_len = smb_encode_path(server, mask, SMB_MAXPATHLEN+1, dentry,NULL);
2535 if (mask_len < 0) {
2536 result = mask_len;
2537 goto out_free;
2539 VERBOSE("name=%s, len=%d\n", mask, mask_len);
2540 WSET(param, 0, aSYSTEM | aHIDDEN | aDIR);
2541 WSET(param, 2, 1); /* max count */
2542 WSET(param, 4, 1); /* close after this call */
2543 WSET(param, 6, 1); /* info_level */
2544 DSET(param, 8, 0);
2546 req->rq_trans2_command = TRANSACT2_FINDFIRST;
2547 req->rq_ldata = 0;
2548 req->rq_data = NULL;
2549 req->rq_lparm = 12 + mask_len;
2550 req->rq_parm = param;
2551 req->rq_flags = 0;
2552 result = smb_add_request(req);
2553 if (result < 0)
2554 goto out_free;
2555 if (req->rq_rcls != 0) {
2556 result = smb_errno(req);
2557 #ifdef SMBFS_PARANOIA
2558 if (result != -ENOENT)
2559 PARANOIA("error for %s, rcls=%d, err=%d\n",
2560 mask, req->rq_rcls, req->rq_err);
2561 #endif
2562 goto out_free;
2564 /* Make sure we got enough data ... */
2565 result = -EINVAL;
2566 if (req->rq_ldata < 22 || WVAL(req->rq_parm, 2) != 1) {
2567 PARANOIA("bad result for %s, len=%d, count=%d\n",
2568 mask, req->rq_ldata, WVAL(req->rq_parm, 2));
2569 goto out_free;
2573 * Decode the response into the fattr ...
2575 date = WVAL(req->rq_data, 0);
2576 time = WVAL(req->rq_data, 2);
2577 fattr->f_ctime.tv_sec = date_dos2unix(server, date, time);
2578 fattr->f_ctime.tv_nsec = 0;
2580 date = WVAL(req->rq_data, 4);
2581 time = WVAL(req->rq_data, 6);
2582 fattr->f_atime.tv_sec = date_dos2unix(server, date, time);
2583 fattr->f_atime.tv_nsec = 0;
2585 date = WVAL(req->rq_data, 8);
2586 time = WVAL(req->rq_data, 10);
2587 fattr->f_mtime.tv_sec = date_dos2unix(server, date, time);
2588 fattr->f_mtime.tv_nsec = 0;
2589 VERBOSE("name=%s, date=%x, time=%x, mtime=%ld\n",
2590 mask, date, time, fattr->f_mtime.tv_sec);
2591 fattr->f_size = DVAL(req->rq_data, 12);
2592 /* ULONG allocation size */
2593 fattr->attr = WVAL(req->rq_data, 20);
2594 result = 0;
2596 out_free:
2597 smb_rput(req);
2598 out:
2599 return result;
2602 static int
2603 smb_proc_getattr_core(struct smb_sb_info *server, struct dentry *dir,
2604 struct smb_fattr *fattr)
2606 int result;
2607 char *p;
2608 struct smb_request *req;
2610 result = -ENOMEM;
2611 if (! (req = smb_alloc_request(server, PAGE_SIZE)))
2612 goto out;
2614 p = smb_setup_header(req, SMBgetatr, 0, 0);
2615 result = smb_simple_encode_path(req, &p, dir, NULL);
2616 if (result < 0)
2617 goto out_free;
2618 smb_setup_bcc(req, p);
2620 if ((result = smb_request_ok(req, SMBgetatr, 10, 0)) < 0)
2621 goto out_free;
2622 fattr->attr = WVAL(req->rq_header, smb_vwv0);
2623 fattr->f_mtime.tv_sec = local2utc(server, DVAL(req->rq_header, smb_vwv1));
2624 fattr->f_mtime.tv_nsec = 0;
2625 fattr->f_size = DVAL(req->rq_header, smb_vwv3);
2626 fattr->f_ctime = fattr->f_mtime;
2627 fattr->f_atime = fattr->f_mtime;
2628 #ifdef SMBFS_DEBUG_TIMESTAMP
2629 printk("getattr_core: %s/%s, mtime=%ld\n",
2630 DENTRY_PATH(dir), fattr->f_mtime);
2631 #endif
2632 result = 0;
2634 out_free:
2635 smb_rput(req);
2636 out:
2637 return result;
2641 * Bugs Noted:
2642 * (1) Win 95 swaps the date and time fields in the standard info level.
2644 static int
2645 smb_proc_getattr_trans2(struct smb_sb_info *server, struct dentry *dir,
2646 struct smb_request *req, int infolevel)
2648 char *p, *param;
2649 int result;
2651 param = req->rq_buffer;
2652 WSET(param, 0, infolevel);
2653 DSET(param, 2, 0);
2654 result = smb_encode_path(server, param+6, SMB_MAXPATHLEN+1, dir, NULL);
2655 if (result < 0)
2656 goto out;
2657 p = param + 6 + result;
2659 req->rq_trans2_command = TRANSACT2_QPATHINFO;
2660 req->rq_ldata = 0;
2661 req->rq_data = NULL;
2662 req->rq_lparm = p - param;
2663 req->rq_parm = param;
2664 req->rq_flags = 0;
2665 result = smb_add_request(req);
2666 if (result < 0)
2667 goto out;
2668 if (req->rq_rcls != 0) {
2669 VERBOSE("for %s: result=%d, rcls=%d, err=%d\n",
2670 &param[6], result, req->rq_rcls, req->rq_err);
2671 result = smb_errno(req);
2672 goto out;
2674 result = -ENOENT;
2675 if (req->rq_ldata < 22) {
2676 PARANOIA("not enough data for %s, len=%d\n",
2677 &param[6], req->rq_ldata);
2678 goto out;
2681 result = 0;
2682 out:
2683 return result;
2686 static int
2687 smb_proc_getattr_trans2_std(struct smb_sb_info *server, struct dentry *dir,
2688 struct smb_fattr *attr)
2690 u16 date, time;
2691 int off_date = 0, off_time = 2;
2692 int result;
2693 struct smb_request *req;
2695 result = -ENOMEM;
2696 if (! (req = smb_alloc_request(server, PAGE_SIZE)))
2697 goto out;
2699 result = smb_proc_getattr_trans2(server, dir, req, SMB_INFO_STANDARD);
2700 if (result < 0)
2701 goto out_free;
2704 * Kludge alert: Win 95 swaps the date and time field,
2705 * contrary to the CIFS docs and Win NT practice.
2707 if (server->mnt->flags & SMB_MOUNT_WIN95) {
2708 off_date = 2;
2709 off_time = 0;
2711 date = WVAL(req->rq_data, off_date);
2712 time = WVAL(req->rq_data, off_time);
2713 attr->f_ctime.tv_sec = date_dos2unix(server, date, time);
2714 attr->f_ctime.tv_nsec = 0;
2716 date = WVAL(req->rq_data, 4 + off_date);
2717 time = WVAL(req->rq_data, 4 + off_time);
2718 attr->f_atime.tv_sec = date_dos2unix(server, date, time);
2719 attr->f_atime.tv_nsec = 0;
2721 date = WVAL(req->rq_data, 8 + off_date);
2722 time = WVAL(req->rq_data, 8 + off_time);
2723 attr->f_mtime.tv_sec = date_dos2unix(server, date, time);
2724 attr->f_mtime.tv_nsec = 0;
2725 #ifdef SMBFS_DEBUG_TIMESTAMP
2726 printk(KERN_DEBUG "getattr_trans2: %s/%s, date=%x, time=%x, mtime=%ld\n",
2727 DENTRY_PATH(dir), date, time, attr->f_mtime);
2728 #endif
2729 attr->f_size = DVAL(req->rq_data, 12);
2730 attr->attr = WVAL(req->rq_data, 20);
2732 out_free:
2733 smb_rput(req);
2734 out:
2735 return result;
2738 static int
2739 smb_proc_getattr_trans2_all(struct smb_sb_info *server, struct dentry *dir,
2740 struct smb_fattr *attr)
2742 struct smb_request *req;
2743 int result;
2745 result = -ENOMEM;
2746 if (! (req = smb_alloc_request(server, PAGE_SIZE)))
2747 goto out;
2749 result = smb_proc_getattr_trans2(server, dir, req,
2750 SMB_QUERY_FILE_ALL_INFO);
2751 if (result < 0)
2752 goto out_free;
2754 attr->f_ctime = smb_ntutc2unixutc(LVAL(req->rq_data, 0));
2755 attr->f_atime = smb_ntutc2unixutc(LVAL(req->rq_data, 8));
2756 attr->f_mtime = smb_ntutc2unixutc(LVAL(req->rq_data, 16));
2757 /* change (24) */
2758 attr->attr = WVAL(req->rq_data, 32);
2759 /* pad? (34) */
2760 /* allocated size (40) */
2761 attr->f_size = LVAL(req->rq_data, 48);
2763 out_free:
2764 smb_rput(req);
2765 out:
2766 return result;
2769 static int
2770 smb_proc_getattr_unix(struct smb_sb_info *server, struct dentry *dir,
2771 struct smb_fattr *attr)
2773 struct smb_request *req;
2774 int result;
2776 result = -ENOMEM;
2777 if (! (req = smb_alloc_request(server, PAGE_SIZE)))
2778 goto out;
2780 result = smb_proc_getattr_trans2(server, dir, req,
2781 SMB_QUERY_FILE_UNIX_BASIC);
2782 if (result < 0)
2783 goto out_free;
2785 smb_decode_unix_basic(attr, server, req->rq_data);
2787 out_free:
2788 smb_rput(req);
2789 out:
2790 return result;
2793 static int
2794 smb_proc_getattr_95(struct smb_sb_info *server, struct dentry *dir,
2795 struct smb_fattr *attr)
2797 struct inode *inode = dir->d_inode;
2798 int result;
2800 /* FIXME: why not use the "all" version? */
2801 result = smb_proc_getattr_trans2_std(server, dir, attr);
2802 if (result < 0)
2803 goto out;
2806 * None of the getattr versions here can make win9x return the right
2807 * filesize if there are changes made to an open file.
2808 * A seek-to-end does return the right size, but we only need to do
2809 * that on files we have written.
2811 if (inode && SMB_I(inode)->flags & SMB_F_LOCALWRITE &&
2812 smb_is_open(inode))
2814 __u16 fileid = SMB_I(inode)->fileid;
2815 attr->f_size = smb_proc_seek(server, fileid, 2, 0);
2818 out:
2819 return result;
2822 static int
2823 smb_proc_ops_wait(struct smb_sb_info *server)
2825 int result;
2827 result = wait_event_interruptible_timeout(server->conn_wq,
2828 server->conn_complete, 30*HZ);
2830 if (!result || signal_pending(current))
2831 return -EIO;
2833 return 0;
2836 static int
2837 smb_proc_getattr_null(struct smb_sb_info *server, struct dentry *dir,
2838 struct smb_fattr *fattr)
2840 int result;
2842 if (smb_proc_ops_wait(server) < 0)
2843 return -EIO;
2845 smb_init_dirent(server, fattr);
2846 result = server->ops->getattr(server, dir, fattr);
2847 smb_finish_dirent(server, fattr);
2849 return result;
2852 static int
2853 smb_proc_readdir_null(struct file *filp, void *dirent, filldir_t filldir,
2854 struct smb_cache_control *ctl)
2856 struct smb_sb_info *server = server_from_dentry(filp->f_path.dentry);
2858 if (smb_proc_ops_wait(server) < 0)
2859 return -EIO;
2861 return server->ops->readdir(filp, dirent, filldir, ctl);
2865 smb_proc_getattr(struct dentry *dir, struct smb_fattr *fattr)
2867 struct smb_sb_info *server = server_from_dentry(dir);
2868 int result;
2870 smb_init_dirent(server, fattr);
2871 result = server->ops->getattr(server, dir, fattr);
2872 smb_finish_dirent(server, fattr);
2874 return result;
2879 * Because of bugs in the core protocol, we use this only to set
2880 * attributes. See smb_proc_settime() below for timestamp handling.
2882 * Bugs Noted:
2883 * (1) If mtime is non-zero, both Win 3.1 and Win 95 fail
2884 * with an undocumented error (ERRDOS code 50). Setting
2885 * mtime to 0 allows the attributes to be set.
2886 * (2) The extra parameters following the name string aren't
2887 * in the CIFS docs, but seem to be necessary for operation.
2889 static int
2890 smb_proc_setattr_core(struct smb_sb_info *server, struct dentry *dentry,
2891 __u16 attr)
2893 char *p;
2894 int result;
2895 struct smb_request *req;
2897 result = -ENOMEM;
2898 if (! (req = smb_alloc_request(server, PAGE_SIZE)))
2899 goto out;
2901 p = smb_setup_header(req, SMBsetatr, 8, 0);
2902 WSET(req->rq_header, smb_vwv0, attr);
2903 DSET(req->rq_header, smb_vwv1, 0); /* mtime */
2904 WSET(req->rq_header, smb_vwv3, 0); /* reserved values */
2905 WSET(req->rq_header, smb_vwv4, 0);
2906 WSET(req->rq_header, smb_vwv5, 0);
2907 WSET(req->rq_header, smb_vwv6, 0);
2908 WSET(req->rq_header, smb_vwv7, 0);
2909 result = smb_simple_encode_path(req, &p, dentry, NULL);
2910 if (result < 0)
2911 goto out_free;
2912 if (p + 2 > (char *)req->rq_buffer + req->rq_bufsize) {
2913 result = -ENAMETOOLONG;
2914 goto out_free;
2916 *p++ = 4;
2917 *p++ = 0;
2918 smb_setup_bcc(req, p);
2920 result = smb_request_ok(req, SMBsetatr, 0, 0);
2921 if (result < 0)
2922 goto out_free;
2923 result = 0;
2925 out_free:
2926 smb_rput(req);
2927 out:
2928 return result;
2932 * Because of bugs in the trans2 setattr messages, we must set
2933 * attributes and timestamps separately. The core SMBsetatr
2934 * message seems to be the only reliable way to set attributes.
2937 smb_proc_setattr(struct dentry *dir, struct smb_fattr *fattr)
2939 struct smb_sb_info *server = server_from_dentry(dir);
2940 int result;
2942 VERBOSE("setting %s/%s, open=%d\n",
2943 DENTRY_PATH(dir), smb_is_open(dir->d_inode));
2944 result = smb_proc_setattr_core(server, dir, fattr->attr);
2945 return result;
2949 * Sets the timestamps for an file open with write permissions.
2951 static int
2952 smb_proc_setattr_ext(struct smb_sb_info *server,
2953 struct inode *inode, struct smb_fattr *fattr)
2955 __u16 date, time;
2956 int result;
2957 struct smb_request *req;
2959 result = -ENOMEM;
2960 if (! (req = smb_alloc_request(server, 0)))
2961 goto out;
2963 smb_setup_header(req, SMBsetattrE, 7, 0);
2964 WSET(req->rq_header, smb_vwv0, SMB_I(inode)->fileid);
2965 /* We don't change the creation time */
2966 WSET(req->rq_header, smb_vwv1, 0);
2967 WSET(req->rq_header, smb_vwv2, 0);
2968 date_unix2dos(server, fattr->f_atime.tv_sec, &date, &time);
2969 WSET(req->rq_header, smb_vwv3, date);
2970 WSET(req->rq_header, smb_vwv4, time);
2971 date_unix2dos(server, fattr->f_mtime.tv_sec, &date, &time);
2972 WSET(req->rq_header, smb_vwv5, date);
2973 WSET(req->rq_header, smb_vwv6, time);
2974 #ifdef SMBFS_DEBUG_TIMESTAMP
2975 printk(KERN_DEBUG "smb_proc_setattr_ext: date=%d, time=%d, mtime=%ld\n",
2976 date, time, fattr->f_mtime);
2977 #endif
2979 req->rq_flags |= SMB_REQ_NORETRY;
2980 result = smb_request_ok(req, SMBsetattrE, 0, 0);
2981 if (result < 0)
2982 goto out_free;
2983 result = 0;
2984 out_free:
2985 smb_rput(req);
2986 out:
2987 return result;
2991 * Bugs Noted:
2992 * (1) The TRANSACT2_SETPATHINFO message under Win NT 4.0 doesn't
2993 * set the file's attribute flags.
2995 static int
2996 smb_proc_setattr_trans2(struct smb_sb_info *server,
2997 struct dentry *dir, struct smb_fattr *fattr)
2999 __u16 date, time;
3000 char *p, *param;
3001 int result;
3002 char data[26];
3003 struct smb_request *req;
3005 result = -ENOMEM;
3006 if (! (req = smb_alloc_request(server, PAGE_SIZE)))
3007 goto out;
3008 param = req->rq_buffer;
3010 WSET(param, 0, 1); /* Info level SMB_INFO_STANDARD */
3011 DSET(param, 2, 0);
3012 result = smb_encode_path(server, param+6, SMB_MAXPATHLEN+1, dir, NULL);
3013 if (result < 0)
3014 goto out_free;
3015 p = param + 6 + result;
3017 WSET(data, 0, 0); /* creation time */
3018 WSET(data, 2, 0);
3019 date_unix2dos(server, fattr->f_atime.tv_sec, &date, &time);
3020 WSET(data, 4, date);
3021 WSET(data, 6, time);
3022 date_unix2dos(server, fattr->f_mtime.tv_sec, &date, &time);
3023 WSET(data, 8, date);
3024 WSET(data, 10, time);
3025 #ifdef SMBFS_DEBUG_TIMESTAMP
3026 printk(KERN_DEBUG "setattr_trans2: %s/%s, date=%x, time=%x, mtime=%ld\n",
3027 DENTRY_PATH(dir), date, time, fattr->f_mtime);
3028 #endif
3029 DSET(data, 12, 0); /* size */
3030 DSET(data, 16, 0); /* blksize */
3031 WSET(data, 20, 0); /* attr */
3032 DSET(data, 22, 0); /* ULONG EA size */
3034 req->rq_trans2_command = TRANSACT2_SETPATHINFO;
3035 req->rq_ldata = 26;
3036 req->rq_data = data;
3037 req->rq_lparm = p - param;
3038 req->rq_parm = param;
3039 req->rq_flags = 0;
3040 result = smb_add_request(req);
3041 if (result < 0)
3042 goto out_free;
3043 result = 0;
3044 if (req->rq_rcls != 0)
3045 result = smb_errno(req);
3047 out_free:
3048 smb_rput(req);
3049 out:
3050 return result;
3054 * ATTR_MODE 0x001
3055 * ATTR_UID 0x002
3056 * ATTR_GID 0x004
3057 * ATTR_SIZE 0x008
3058 * ATTR_ATIME 0x010
3059 * ATTR_MTIME 0x020
3060 * ATTR_CTIME 0x040
3061 * ATTR_ATIME_SET 0x080
3062 * ATTR_MTIME_SET 0x100
3063 * ATTR_FORCE 0x200
3064 * ATTR_ATTR_FLAG 0x400
3066 * major/minor should only be set by mknod.
3069 smb_proc_setattr_unix(struct dentry *d, struct iattr *attr,
3070 unsigned int major, unsigned int minor)
3072 struct smb_sb_info *server = server_from_dentry(d);
3073 u64 nttime;
3074 char *p, *param;
3075 int result;
3076 char data[100];
3077 struct smb_request *req;
3079 result = -ENOMEM;
3080 if (! (req = smb_alloc_request(server, PAGE_SIZE)))
3081 goto out;
3082 param = req->rq_buffer;
3084 DEBUG1("valid flags = 0x%04x\n", attr->ia_valid);
3086 WSET(param, 0, SMB_SET_FILE_UNIX_BASIC);
3087 DSET(param, 2, 0);
3088 result = smb_encode_path(server, param+6, SMB_MAXPATHLEN+1, d, NULL);
3089 if (result < 0)
3090 goto out_free;
3091 p = param + 6 + result;
3093 /* 0 L file size in bytes */
3094 /* 8 L file size on disk in bytes (block count) */
3095 /* 40 L uid */
3096 /* 48 L gid */
3097 /* 56 W file type enum */
3098 /* 60 L devmajor */
3099 /* 68 L devminor */
3100 /* 76 L unique ID (inode) */
3101 /* 84 L permissions */
3102 /* 92 L link count */
3103 LSET(data, 0, SMB_SIZE_NO_CHANGE);
3104 LSET(data, 8, SMB_SIZE_NO_CHANGE);
3105 LSET(data, 16, SMB_TIME_NO_CHANGE);
3106 LSET(data, 24, SMB_TIME_NO_CHANGE);
3107 LSET(data, 32, SMB_TIME_NO_CHANGE);
3108 LSET(data, 40, SMB_UID_NO_CHANGE);
3109 LSET(data, 48, SMB_GID_NO_CHANGE);
3110 DSET(data, 56, smb_filetype_from_mode(attr->ia_mode));
3111 LSET(data, 60, major);
3112 LSET(data, 68, minor);
3113 LSET(data, 76, 0);
3114 LSET(data, 84, SMB_MODE_NO_CHANGE);
3115 LSET(data, 92, 0);
3117 if (attr->ia_valid & ATTR_SIZE) {
3118 LSET(data, 0, attr->ia_size);
3119 LSET(data, 8, 0); /* can't set anyway */
3123 * FIXME: check the conversion function it the correct one
3125 * we can't set ctime but we might as well pass this to the server
3126 * and let it ignore it.
3128 if (attr->ia_valid & ATTR_CTIME) {
3129 nttime = smb_unixutc2ntutc(attr->ia_ctime);
3130 LSET(data, 16, nttime);
3132 if (attr->ia_valid & ATTR_ATIME) {
3133 nttime = smb_unixutc2ntutc(attr->ia_atime);
3134 LSET(data, 24, nttime);
3136 if (attr->ia_valid & ATTR_MTIME) {
3137 nttime = smb_unixutc2ntutc(attr->ia_mtime);
3138 LSET(data, 32, nttime);
3141 if (attr->ia_valid & ATTR_UID) {
3142 LSET(data, 40, attr->ia_uid);
3144 if (attr->ia_valid & ATTR_GID) {
3145 LSET(data, 48, attr->ia_gid);
3148 if (attr->ia_valid & ATTR_MODE) {
3149 LSET(data, 84, attr->ia_mode);
3152 req->rq_trans2_command = TRANSACT2_SETPATHINFO;
3153 req->rq_ldata = 100;
3154 req->rq_data = data;
3155 req->rq_lparm = p - param;
3156 req->rq_parm = param;
3157 req->rq_flags = 0;
3158 result = smb_add_request(req);
3160 out_free:
3161 smb_rput(req);
3162 out:
3163 return result;
3168 * Set the modify and access timestamps for a file.
3170 * Incredibly enough, in all of SMB there is no message to allow
3171 * setting both attributes and timestamps at once.
3173 * Bugs Noted:
3174 * (1) Win 95 doesn't support the TRANSACT2_SETFILEINFO message
3175 * with info level 1 (INFO_STANDARD).
3176 * (2) Win 95 seems not to support setting directory timestamps.
3177 * (3) Under the core protocol apparently the only way to set the
3178 * timestamp is to open and close the file.
3181 smb_proc_settime(struct dentry *dentry, struct smb_fattr *fattr)
3183 struct smb_sb_info *server = server_from_dentry(dentry);
3184 struct inode *inode = dentry->d_inode;
3185 int result;
3187 VERBOSE("setting %s/%s, open=%d\n",
3188 DENTRY_PATH(dentry), smb_is_open(inode));
3190 /* setting the time on a Win95 server fails (tridge) */
3191 if (server->opt.protocol >= SMB_PROTOCOL_LANMAN2 &&
3192 !(server->mnt->flags & SMB_MOUNT_WIN95)) {
3193 if (smb_is_open(inode) && SMB_I(inode)->access != SMB_O_RDONLY)
3194 result = smb_proc_setattr_ext(server, inode, fattr);
3195 else
3196 result = smb_proc_setattr_trans2(server, dentry, fattr);
3197 } else {
3199 * Fail silently on directories ... timestamp can't be set?
3201 result = 0;
3202 if (S_ISREG(inode->i_mode)) {
3204 * Set the mtime by opening and closing the file.
3205 * Note that the file is opened read-only, but this
3206 * still allows us to set the date (tridge)
3208 result = -EACCES;
3209 if (!smb_is_open(inode))
3210 smb_proc_open(server, dentry, SMB_O_RDONLY);
3211 if (smb_is_open(inode)) {
3212 inode->i_mtime = fattr->f_mtime;
3213 result = smb_proc_close_inode(server, inode);
3218 return result;
3222 smb_proc_dskattr(struct dentry *dentry, struct kstatfs *attr)
3224 struct smb_sb_info *server = SMB_SB(dentry->d_sb);
3225 int result;
3226 char *p;
3227 long unit;
3228 struct smb_request *req;
3230 result = -ENOMEM;
3231 if (! (req = smb_alloc_request(server, 0)))
3232 goto out;
3234 smb_setup_header(req, SMBdskattr, 0, 0);
3235 if ((result = smb_request_ok(req, SMBdskattr, 5, 0)) < 0)
3236 goto out_free;
3237 p = SMB_VWV(req->rq_header);
3238 unit = (WVAL(p, 2) * WVAL(p, 4)) >> SMB_ST_BLKSHIFT;
3239 attr->f_blocks = WVAL(p, 0) * unit;
3240 attr->f_bsize = SMB_ST_BLKSIZE;
3241 attr->f_bavail = attr->f_bfree = WVAL(p, 6) * unit;
3242 result = 0;
3244 out_free:
3245 smb_rput(req);
3246 out:
3247 return result;
3251 smb_proc_read_link(struct smb_sb_info *server, struct dentry *d,
3252 char *buffer, int len)
3254 char *p, *param;
3255 int result;
3256 struct smb_request *req;
3258 DEBUG1("readlink of %s/%s\n", DENTRY_PATH(d));
3260 result = -ENOMEM;
3261 if (! (req = smb_alloc_request(server, PAGE_SIZE)))
3262 goto out;
3263 param = req->rq_buffer;
3265 WSET(param, 0, SMB_QUERY_FILE_UNIX_LINK);
3266 DSET(param, 2, 0);
3267 result = smb_encode_path(server, param+6, SMB_MAXPATHLEN+1, d, NULL);
3268 if (result < 0)
3269 goto out_free;
3270 p = param + 6 + result;
3272 req->rq_trans2_command = TRANSACT2_QPATHINFO;
3273 req->rq_ldata = 0;
3274 req->rq_data = NULL;
3275 req->rq_lparm = p - param;
3276 req->rq_parm = param;
3277 req->rq_flags = 0;
3278 result = smb_add_request(req);
3279 if (result < 0)
3280 goto out_free;
3281 DEBUG1("for %s: result=%d, rcls=%d, err=%d\n",
3282 &param[6], result, req->rq_rcls, req->rq_err);
3284 /* copy data up to the \0 or buffer length */
3285 result = len;
3286 if (req->rq_ldata < len)
3287 result = req->rq_ldata;
3288 strncpy(buffer, req->rq_data, result);
3290 out_free:
3291 smb_rput(req);
3292 out:
3293 return result;
3298 * Create a symlink object called dentry which points to oldpath.
3299 * Samba does not permit dangling links but returns a suitable error message.
3302 smb_proc_symlink(struct smb_sb_info *server, struct dentry *d,
3303 const char *oldpath)
3305 char *p, *param;
3306 int result;
3307 struct smb_request *req;
3309 result = -ENOMEM;
3310 if (! (req = smb_alloc_request(server, PAGE_SIZE)))
3311 goto out;
3312 param = req->rq_buffer;
3314 WSET(param, 0, SMB_SET_FILE_UNIX_LINK);
3315 DSET(param, 2, 0);
3316 result = smb_encode_path(server, param + 6, SMB_MAXPATHLEN+1, d, NULL);
3317 if (result < 0)
3318 goto out_free;
3319 p = param + 6 + result;
3321 req->rq_trans2_command = TRANSACT2_SETPATHINFO;
3322 req->rq_ldata = strlen(oldpath) + 1;
3323 req->rq_data = (char *) oldpath;
3324 req->rq_lparm = p - param;
3325 req->rq_parm = param;
3326 req->rq_flags = 0;
3327 result = smb_add_request(req);
3328 if (result < 0)
3329 goto out_free;
3331 DEBUG1("for %s: result=%d, rcls=%d, err=%d\n",
3332 &param[6], result, req->rq_rcls, req->rq_err);
3333 result = 0;
3335 out_free:
3336 smb_rput(req);
3337 out:
3338 return result;
3342 * Create a hard link object called new_dentry which points to dentry.
3345 smb_proc_link(struct smb_sb_info *server, struct dentry *dentry,
3346 struct dentry *new_dentry)
3348 char *p, *param;
3349 int result;
3350 struct smb_request *req;
3352 result = -ENOMEM;
3353 if (! (req = smb_alloc_request(server, PAGE_SIZE)))
3354 goto out;
3355 param = req->rq_buffer;
3357 WSET(param, 0, SMB_SET_FILE_UNIX_HLINK);
3358 DSET(param, 2, 0);
3359 result = smb_encode_path(server, param + 6, SMB_MAXPATHLEN+1,
3360 new_dentry, NULL);
3361 if (result < 0)
3362 goto out_free;
3363 p = param + 6 + result;
3365 /* Grr, pointless separation of parameters and data ... */
3366 req->rq_data = p;
3367 req->rq_ldata = smb_encode_path(server, p, SMB_MAXPATHLEN+1,
3368 dentry, NULL);
3370 req->rq_trans2_command = TRANSACT2_SETPATHINFO;
3371 req->rq_lparm = p - param;
3372 req->rq_parm = param;
3373 req->rq_flags = 0;
3374 result = smb_add_request(req);
3375 if (result < 0)
3376 goto out_free;
3378 DEBUG1("for %s: result=%d, rcls=%d, err=%d\n",
3379 &param[6], result, req->rq_rcls, req->rq_err);
3380 result = 0;
3382 out_free:
3383 smb_rput(req);
3384 out:
3385 return result;
3388 static int
3389 smb_proc_query_cifsunix(struct smb_sb_info *server)
3391 int result;
3392 int major, minor;
3393 u64 caps;
3394 char param[2];
3395 struct smb_request *req;
3397 result = -ENOMEM;
3398 if (! (req = smb_alloc_request(server, 100)))
3399 goto out;
3401 WSET(param, 0, SMB_QUERY_CIFS_UNIX_INFO);
3403 req->rq_trans2_command = TRANSACT2_QFSINFO;
3404 req->rq_ldata = 0;
3405 req->rq_data = NULL;
3406 req->rq_lparm = 2;
3407 req->rq_parm = param;
3408 req->rq_flags = 0;
3409 result = smb_add_request(req);
3410 if (result < 0)
3411 goto out_free;
3413 if (req->rq_ldata < 12) {
3414 PARANOIA("Not enough data\n");
3415 goto out_free;
3417 major = WVAL(req->rq_data, 0);
3418 minor = WVAL(req->rq_data, 2);
3420 DEBUG1("Server implements CIFS Extensions for UNIX systems v%d.%d\n",
3421 major, minor);
3422 /* FIXME: verify that we are ok with this major/minor? */
3424 caps = LVAL(req->rq_data, 4);
3425 DEBUG1("Server capabilities 0x%016llx\n", caps);
3427 out_free:
3428 smb_rput(req);
3429 out:
3430 return result;
3434 static void
3435 install_ops(struct smb_ops *dst, struct smb_ops *src)
3437 memcpy(dst, src, sizeof(void *) * SMB_OPS_NUM_STATIC);
3440 /* < LANMAN2 */
3441 static struct smb_ops smb_ops_core =
3443 .read = smb_proc_read,
3444 .write = smb_proc_write,
3445 .readdir = smb_proc_readdir_short,
3446 .getattr = smb_proc_getattr_core,
3447 .truncate = smb_proc_trunc32,
3450 /* LANMAN2, OS/2, others? */
3451 static struct smb_ops smb_ops_os2 =
3453 .read = smb_proc_read,
3454 .write = smb_proc_write,
3455 .readdir = smb_proc_readdir_long,
3456 .getattr = smb_proc_getattr_trans2_std,
3457 .truncate = smb_proc_trunc32,
3460 /* Win95, and possibly some NetApp versions too */
3461 static struct smb_ops smb_ops_win95 =
3463 .read = smb_proc_read, /* does not support 12word readX */
3464 .write = smb_proc_write,
3465 .readdir = smb_proc_readdir_long,
3466 .getattr = smb_proc_getattr_95,
3467 .truncate = smb_proc_trunc95,
3470 /* Samba, NT4 and NT5 */
3471 static struct smb_ops smb_ops_winNT =
3473 .read = smb_proc_readX,
3474 .write = smb_proc_writeX,
3475 .readdir = smb_proc_readdir_long,
3476 .getattr = smb_proc_getattr_trans2_all,
3477 .truncate = smb_proc_trunc64,
3480 /* Samba w/ unix extensions. Others? */
3481 static struct smb_ops smb_ops_unix =
3483 .read = smb_proc_readX,
3484 .write = smb_proc_writeX,
3485 .readdir = smb_proc_readdir_long,
3486 .getattr = smb_proc_getattr_unix,
3487 /* FIXME: core/ext/time setattr needs to be cleaned up! */
3488 /* .setattr = smb_proc_setattr_unix, */
3489 .truncate = smb_proc_trunc64,
3492 /* Place holder until real ops are in place */
3493 static struct smb_ops smb_ops_null =
3495 .readdir = smb_proc_readdir_null,
3496 .getattr = smb_proc_getattr_null,
3499 void smb_install_null_ops(struct smb_ops *ops)
3501 install_ops(ops, &smb_ops_null);