Merge with Linux 2.5.48.
[linux-2.6/linux-mips.git] / fs / smbfs / proc.c
blobcd495b55b0aac7564b91ea0d20ec18feb9fe4b35
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/errno.h>
12 #include <linux/slab.h>
13 #include <linux/fs.h>
14 #include <linux/file.h>
15 #include <linux/stat.h>
16 #include <linux/fcntl.h>
17 #include <linux/dcache.h>
18 #include <linux/dirent.h>
19 #include <linux/nls.h>
20 #include <linux/smp_lock.h>
21 #include <linux/net.h>
23 #include <linux/smb_fs.h>
24 #include <linux/smbno.h>
25 #include <linux/smb_mount.h>
27 #include <net/sock.h>
29 #include <asm/string.h>
30 #include <asm/div64.h>
32 #include "smb_debug.h"
33 #include "proto.h"
34 #include "request.h"
37 /* Features. Undefine if they cause problems, this should perhaps be a
38 config option. */
39 #define SMBFS_POSIX_UNLINK 1
41 /* Allow smb_retry to be interrupted. */
42 #define SMB_RETRY_INTR
44 #define SMB_VWV(packet) ((packet) + SMB_HEADER_LEN)
45 #define SMB_CMD(packet) (*(packet+8))
46 #define SMB_WCT(packet) (*(packet+SMB_HEADER_LEN - 1))
48 #define SMB_DIRINFO_SIZE 43
49 #define SMB_STATUS_SIZE 21
51 #define SMB_ST_BLKSIZE (PAGE_SIZE)
52 #define SMB_ST_BLKSHIFT (PAGE_SHIFT)
54 static struct smb_ops smb_ops_core;
55 static struct smb_ops smb_ops_os2;
56 static struct smb_ops smb_ops_win95;
57 static struct smb_ops smb_ops_winNT;
58 static struct smb_ops smb_ops_unix;
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 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 "unicode",
216 uni2char,
217 char2uni,
218 NULL, /* not used by smbfs */
219 NULL,
220 NULL, /* not a module */
223 /* ----------------------------------------------------------- */
225 static int setcodepage(struct nls_table **p, char *name)
227 struct nls_table *nls;
229 if (!name || !*name) {
230 nls = NULL;
231 } else if ( (nls = load_nls(name)) == NULL) {
232 printk (KERN_ERR "smbfs: failed to load nls '%s'\n", name);
233 return -EINVAL;
236 /* if already set, unload the previous one. */
237 if (*p && *p != &unicode_table)
238 unload_nls(*p);
239 *p = nls;
241 return 0;
244 /* Handles all changes to codepage settings. */
245 int smb_setcodepage(struct smb_sb_info *server, struct smb_nls_codepage *cp)
247 int n = 0;
249 smb_lock_server(server);
251 /* Don't load any nls_* at all, if no remote is requested */
252 if (!*cp->remote_name)
253 goto out;
255 /* local */
256 n = setcodepage(&server->local_nls, cp->local_name);
257 if (n != 0)
258 goto out;
260 /* remote */
261 if (!strcmp(cp->remote_name, "unicode")) {
262 server->remote_nls = &unicode_table;
263 } else {
264 n = setcodepage(&server->remote_nls, cp->remote_name);
265 if (n != 0)
266 setcodepage(&server->local_nls, NULL);
269 out:
270 if (server->local_nls != NULL && server->remote_nls != NULL)
271 server->ops->convert = convert_cp;
272 else
273 server->ops->convert = convert_memcpy;
275 smb_unlock_server(server);
276 return n;
280 /*****************************************************************************/
281 /* */
282 /* Encoding/Decoding section */
283 /* */
284 /*****************************************************************************/
286 static __u8 *
287 smb_encode_smb_length(__u8 * p, __u32 len)
289 *p = 0;
290 *(p+1) = 0;
291 *(p+2) = (len & 0xFF00) >> 8;
292 *(p+3) = (len & 0xFF);
293 if (len > 0xFFFF)
295 *(p+1) = 1;
297 return p + 4;
301 * smb_build_path: build the path to entry and name storing it in buf.
302 * The path returned will have the trailing '\0'.
304 static int smb_build_path(struct smb_sb_info *server, unsigned char *buf,
305 int maxlen,
306 struct dentry *entry, struct qstr *name)
308 unsigned char *path = buf;
309 int len;
310 int unicode = (server->mnt->flags & SMB_MOUNT_UNICODE) != 0;
312 if (maxlen < (2<<unicode))
313 return -ENAMETOOLONG;
315 if (maxlen > SMB_MAXPATHLEN + 1)
316 maxlen = SMB_MAXPATHLEN + 1;
318 if (entry == NULL)
319 goto test_name_and_out;
322 * If IS_ROOT, we have to do no walking at all.
324 if (IS_ROOT(entry) && !name) {
325 *path++ = '\\';
326 if (unicode) *path++ = '\0';
327 *path++ = '\0';
328 if (unicode) *path++ = '\0';
329 return path-buf;
333 * Build the path string walking the tree backward from end to ROOT
334 * and store it in reversed order [see reverse_string()]
336 read_lock(&dparent_lock);
337 while (!IS_ROOT(entry)) {
338 if (maxlen < (3<<unicode)) {
339 read_unlock(&dparent_lock);
340 return -ENAMETOOLONG;
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 read_unlock(&dparent_lock);
348 return len;
350 reverse_string(path, len);
351 path += len;
352 if (unicode) {
353 /* Note: reverse order */
354 *path++ = '\0';
355 maxlen--;
357 *path++ = '\\';
358 maxlen -= len+1;
360 entry = entry->d_parent;
362 read_unlock(&dparent_lock);
363 reverse_string(buf, path-buf);
365 /* maxlen has space for at least one char */
366 test_name_and_out:
367 if (name) {
368 if (maxlen < (3<<unicode))
369 return -ENAMETOOLONG;
370 *path++ = '\\';
371 if (unicode) {
372 *path++ = '\0';
373 maxlen--;
375 len = server->ops->convert(path, maxlen-2,
376 name->name, name->len,
377 server->local_nls, server->remote_nls);
378 if (len < 0)
379 return len;
380 path += len;
381 maxlen -= len+1;
383 /* maxlen has space for at least one char */
384 *path++ = '\0';
385 if (unicode) *path++ = '\0';
386 return path-buf;
389 static int smb_encode_path(struct smb_sb_info *server, char *buf, int maxlen,
390 struct dentry *dir, struct qstr *name)
392 int result;
394 result = smb_build_path(server, buf, maxlen, dir, name);
395 if (result < 0)
396 goto out;
397 if (server->opt.protocol <= SMB_PROTOCOL_COREPLUS)
398 str_upper(buf, result);
399 out:
400 return result;
403 /* encode_path for non-trans2 request SMBs */
404 static int smb_simple_encode_path(struct smb_request *req, char **p,
405 struct dentry * entry, struct qstr * name)
407 struct smb_sb_info *server = req->rq_server;
408 char *s = *p;
409 int res;
410 int maxlen = ((char *)req->rq_buffer + req->rq_bufsize) - s;
411 int unicode = (server->mnt->flags & SMB_MOUNT_UNICODE);
413 if (!maxlen)
414 return -ENAMETOOLONG;
415 *s++ = 4; /* ASCII data format */
418 * SMB Unicode strings must be 16bit aligned relative the start of the
419 * packet. If they are not they must be padded with 0.
421 if (unicode) {
422 int align = s - (char *)req->rq_buffer;
423 if (!(align & 1)) {
424 *s++ = '\0';
425 maxlen--;
429 res = smb_encode_path(server, s, maxlen-1, entry, name);
430 if (res < 0)
431 return res;
432 *p = s + res;
433 return 0;
436 /* The following are taken directly from msdos-fs */
438 /* Linear day numbers of the respective 1sts in non-leap years. */
440 static int day_n[] =
441 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 0, 0, 0, 0};
442 /* JanFebMarApr May Jun Jul Aug Sep Oct Nov Dec */
445 static time_t
446 utc2local(struct smb_sb_info *server, time_t time)
448 return time - server->opt.serverzone*60;
451 static time_t
452 local2utc(struct smb_sb_info *server, time_t time)
454 return time + server->opt.serverzone*60;
457 /* Convert a MS-DOS time/date pair to a UNIX date (seconds since 1 1 70). */
459 static time_t
460 date_dos2unix(struct smb_sb_info *server, __u16 date, __u16 time)
462 int month, year;
463 time_t secs;
465 /* first subtract and mask after that... Otherwise, if
466 date == 0, bad things happen */
467 month = ((date >> 5) - 1) & 15;
468 year = date >> 9;
469 secs = (time & 31) * 2 + 60 * ((time >> 5) & 63) + (time >> 11) * 3600 + 86400 *
470 ((date & 31) - 1 + day_n[month] + (year / 4) + year * 365 - ((year & 3) == 0 &&
471 month < 2 ? 1 : 0) + 3653);
472 /* days since 1.1.70 plus 80's leap day */
473 return local2utc(server, secs);
477 /* Convert linear UNIX date to a MS-DOS time/date pair. */
479 static void
480 date_unix2dos(struct smb_sb_info *server,
481 int unix_date, __u16 *date, __u16 *time)
483 int day, year, nl_day, month;
485 unix_date = utc2local(server, unix_date);
486 if (unix_date < 315532800)
487 unix_date = 315532800;
489 *time = (unix_date % 60) / 2 +
490 (((unix_date / 60) % 60) << 5) +
491 (((unix_date / 3600) % 24) << 11);
493 day = unix_date / 86400 - 3652;
494 year = day / 365;
495 if ((year + 3) / 4 + 365 * year > day)
496 year--;
497 day -= (year + 3) / 4 + 365 * year;
498 if (day == 59 && !(year & 3)) {
499 nl_day = day;
500 month = 2;
501 } else {
502 nl_day = (year & 3) || day <= 59 ? day : day - 1;
503 for (month = 0; month < 12; month++)
504 if (day_n[month] > nl_day)
505 break;
507 *date = nl_day - day_n[month - 1] + 1 + (month << 5) + (year << 9);
510 /* The following are taken from fs/ntfs/util.c */
512 #define NTFS_TIME_OFFSET ((u64)(369*365 + 89) * 24 * 3600 * 10000000)
515 * Convert the NT UTC (based 1601-01-01, in hundred nanosecond units)
516 * into Unix UTC (based 1970-01-01, in seconds).
518 static struct timespec
519 smb_ntutc2unixutc(u64 ntutc)
521 struct timespec ts;
522 /* FIXME: what about the timezone difference? */
523 /* Subtract the NTFS time offset, then convert to 1s intervals. */
524 u64 t = ntutc - NTFS_TIME_OFFSET;
525 ts.tv_nsec = do_div(t, 10000000) * 100;
526 ts.tv_sec = t;
527 return ts;
530 /* Convert the Unix UTC into NT time */
531 static u64
532 smb_unixutc2ntutc(struct timespec ts)
534 /* Note: timezone conversion is probably wrong. */
535 /* return ((u64)utc2local(server, t)) * 10000000 + NTFS_TIME_OFFSET; */
536 return ((u64)ts.tv_sec) * 10000000 + ts.tv_nsec/100 + NTFS_TIME_OFFSET;
539 #define MAX_FILE_MODE 6
540 static mode_t file_mode[] = {
541 S_IFREG, S_IFDIR, S_IFLNK, S_IFCHR, S_IFBLK, S_IFIFO, S_IFSOCK
544 static int smb_filetype_to_mode(u32 filetype)
546 if (filetype > MAX_FILE_MODE) {
547 PARANOIA("Filetype out of range: %d\n", filetype);
548 return S_IFREG;
550 return file_mode[filetype];
553 static u32 smb_filetype_from_mode(int mode)
555 if (mode & S_IFREG)
556 return UNIX_TYPE_FILE;
557 if (mode & S_IFDIR)
558 return UNIX_TYPE_DIR;
559 if (mode & S_IFLNK)
560 return UNIX_TYPE_SYMLINK;
561 if (mode & S_IFCHR)
562 return UNIX_TYPE_CHARDEV;
563 if (mode & S_IFBLK)
564 return UNIX_TYPE_BLKDEV;
565 if (mode & S_IFIFO)
566 return UNIX_TYPE_FIFO;
567 if (mode & S_IFSOCK)
568 return UNIX_TYPE_SOCKET;
569 return UNIX_TYPE_UNKNOWN;
573 /*****************************************************************************/
574 /* */
575 /* Support section. */
576 /* */
577 /*****************************************************************************/
579 __u32
580 smb_len(__u8 * p)
582 return ((*(p+1) & 0x1) << 16L) | (*(p+2) << 8L) | *(p+3);
585 static __u16
586 smb_bcc(__u8 * packet)
588 int pos = SMB_HEADER_LEN + SMB_WCT(packet) * sizeof(__u16);
589 return WVAL(packet, pos);
592 /* smb_valid_packet: We check if packet fulfills the basic
593 requirements of a smb packet */
595 static int
596 smb_valid_packet(__u8 * packet)
598 return (packet[4] == 0xff
599 && packet[5] == 'S'
600 && packet[6] == 'M'
601 && packet[7] == 'B'
602 && (smb_len(packet) + 4 == SMB_HEADER_LEN
603 + SMB_WCT(packet) * 2 + smb_bcc(packet)));
606 /* smb_verify: We check if we got the answer we expected, and if we
607 got enough data. If bcc == -1, we don't care. */
609 static int
610 smb_verify(__u8 * packet, int command, int wct, int bcc)
612 if (SMB_CMD(packet) != command)
613 goto bad_command;
614 if (SMB_WCT(packet) < wct)
615 goto bad_wct;
616 if (bcc != -1 && smb_bcc(packet) < bcc)
617 goto bad_bcc;
618 return 0;
620 bad_command:
621 printk(KERN_ERR "smb_verify: command=%x, SMB_CMD=%x??\n",
622 command, SMB_CMD(packet));
623 goto fail;
624 bad_wct:
625 printk(KERN_ERR "smb_verify: command=%x, wct=%d, SMB_WCT=%d??\n",
626 command, wct, SMB_WCT(packet));
627 goto fail;
628 bad_bcc:
629 printk(KERN_ERR "smb_verify: command=%x, bcc=%d, SMB_BCC=%d??\n",
630 command, bcc, smb_bcc(packet));
631 fail:
632 return -EIO;
636 * Returns the maximum read or write size for the "payload". Making all of the
637 * packet fit within the negotiated max_xmit size.
639 * N.B. Since this value is usually computed before locking the server,
640 * the server's packet size must never be decreased!
642 static inline int
643 smb_get_xmitsize(struct smb_sb_info *server, int overhead)
645 return server->opt.max_xmit - overhead;
649 * Calculate the maximum read size
652 smb_get_rsize(struct smb_sb_info *server)
654 /* readX has 12 parameters, read has 5 */
655 int overhead = SMB_HEADER_LEN + 12 * sizeof(__u16) + 2 + 1 + 2;
656 int size = smb_get_xmitsize(server, overhead);
658 VERBOSE("xmit=%d, size=%d\n", server->opt.max_xmit, size);
660 return size;
664 * Calculate the maximum write size
667 smb_get_wsize(struct smb_sb_info *server)
669 /* writeX has 14 parameters, write has 5 */
670 int overhead = SMB_HEADER_LEN + 14 * sizeof(__u16) + 2 + 1 + 2;
671 int size = smb_get_xmitsize(server, overhead);
673 VERBOSE("xmit=%d, size=%d\n", server->opt.max_xmit, size);
675 return size;
679 * Convert SMB error codes to -E... errno values.
682 smb_errno(struct smb_request *req)
684 int errcls = req->rq_rcls;
685 int error = req->rq_err;
686 char *class = "Unknown";
688 VERBOSE("errcls %d code %d from command 0x%x\n",
689 errcls, error, SMB_CMD(req->rq_header));
691 if (errcls == ERRDOS) {
692 switch (error) {
693 case ERRbadfunc:
694 return -EINVAL;
695 case ERRbadfile:
696 case ERRbadpath:
697 return -ENOENT;
698 case ERRnofids:
699 return -EMFILE;
700 case ERRnoaccess:
701 return -EACCES;
702 case ERRbadfid:
703 return -EBADF;
704 case ERRbadmcb:
705 return -EREMOTEIO;
706 case ERRnomem:
707 return -ENOMEM;
708 case ERRbadmem:
709 return -EFAULT;
710 case ERRbadenv:
711 case ERRbadformat:
712 return -EREMOTEIO;
713 case ERRbadaccess:
714 return -EACCES;
715 case ERRbaddata:
716 return -E2BIG;
717 case ERRbaddrive:
718 return -ENXIO;
719 case ERRremcd:
720 return -EREMOTEIO;
721 case ERRdiffdevice:
722 return -EXDEV;
723 case ERRnofiles:
724 return -ENOENT;
725 case ERRbadshare:
726 return -ETXTBSY;
727 case ERRlock:
728 return -EDEADLK;
729 case ERRfilexists:
730 return -EEXIST;
731 case ERROR_INVALID_PARAMETER:
732 return -EINVAL;
733 case ERROR_DISK_FULL:
734 return -ENOSPC;
735 case ERROR_INVALID_NAME:
736 return -ENOENT;
737 case ERROR_DIR_NOT_EMPTY:
738 return -ENOTEMPTY;
739 case ERROR_NOT_LOCKED:
740 return -ENOLCK;
741 case ERROR_ALREADY_EXISTS:
742 return -EEXIST;
743 default:
744 class = "ERRDOS";
745 goto err_unknown;
747 } else if (errcls == ERRSRV) {
748 switch (error) {
749 /* N.B. This is wrong ... EIO ? */
750 case ERRerror:
751 return -ENFILE;
752 case ERRbadpw:
753 return -EINVAL;
754 case ERRbadtype:
755 case ERRtimeout:
756 return -EIO;
757 case ERRaccess:
758 return -EACCES;
760 * This is a fatal error, as it means the "tree ID"
761 * for this connection is no longer valid. We map
762 * to a special error code and get a new connection.
764 case ERRinvnid:
765 return -EBADSLT;
766 default:
767 class = "ERRSRV";
768 goto err_unknown;
770 } else if (errcls == ERRHRD) {
771 switch (error) {
772 case ERRnowrite:
773 return -EROFS;
774 case ERRbadunit:
775 return -ENODEV;
776 case ERRnotready:
777 return -EUCLEAN;
778 case ERRbadcmd:
779 case ERRdata:
780 return -EIO;
781 case ERRbadreq:
782 return -ERANGE;
783 case ERRbadshare:
784 return -ETXTBSY;
785 case ERRlock:
786 return -EDEADLK;
787 case ERRdiskfull:
788 return -ENOSPC;
789 default:
790 class = "ERRHRD";
791 goto err_unknown;
793 } else if (errcls == ERRCMD) {
794 class = "ERRCMD";
795 } else if (errcls == SUCCESS) {
796 return 0; /* This is the only valid 0 return */
799 err_unknown:
800 printk(KERN_ERR "smb_errno: class %s, code %d from command 0x%x\n",
801 class, error, SMB_CMD(req->rq_header));
802 return -EIO;
805 /* smb_request_ok: We expect the server to be locked. Then we do the
806 request and check the answer completely. When smb_request_ok
807 returns 0, you can be quite sure that everything went well. When
808 the answer is <=0, the returned number is a valid unix errno. */
810 static int
811 smb_request_ok(struct smb_request *req, int command, int wct, int bcc)
813 int result;
815 req->rq_resp_wct = wct;
816 req->rq_resp_bcc = bcc;
818 result = smb_add_request(req);
819 if (result != 0) {
820 DEBUG1("smb_request failed\n");
821 goto out;
824 if (smb_valid_packet(req->rq_header) != 0) {
825 PARANOIA("invalid packet!\n");
826 goto out;
829 result = smb_verify(req->rq_header, command, wct, bcc);
831 out:
832 return result;
836 * This implements the NEWCONN ioctl. It installs the server pid,
837 * sets server->state to CONN_VALID, and wakes up the waiting process.
840 smb_newconn(struct smb_sb_info *server, struct smb_conn_opt *opt)
842 struct file *filp;
843 struct sock *sk;
844 int error;
846 VERBOSE("fd=%d, pid=%d\n", opt->fd, current->pid);
848 smb_lock_server(server);
851 * Make sure we don't already have a valid connection ...
853 error = -EINVAL;
854 if (server->state == CONN_VALID)
855 goto out;
857 error = -EACCES;
858 if (current->uid != server->mnt->mounted_uid &&
859 !capable(CAP_SYS_ADMIN))
860 goto out;
862 error = -EBADF;
863 filp = fget(opt->fd);
864 if (!filp)
865 goto out;
866 if (!smb_valid_socket(filp->f_dentry->d_inode))
867 goto out_putf;
869 server->sock_file = filp;
870 server->conn_pid = current->pid;
871 server->opt = *opt;
872 server->generation += 1;
873 server->state = CONN_VALID;
874 error = 0;
876 if (server->conn_error) {
878 * conn_error is the returncode we originally decided to
879 * drop the old connection on. This message should be positive
880 * and not make people ask questions on why smbfs is printing
881 * error messages ...
883 printk(KERN_INFO "SMB connection re-established (%d)\n",
884 server->conn_error);
885 server->conn_error = 0;
889 * Store the server in sock user_data (Only used by sunrpc)
891 sk = SOCKET_I(filp->f_dentry->d_inode)->sk;
892 sk->user_data = server;
894 /* chain into the data_ready callback */
895 server->data_ready = xchg(&sk->data_ready, smb_data_ready);
897 /* check if we have an old smbmount that uses seconds for the
898 serverzone */
899 if (server->opt.serverzone > 12*60 || server->opt.serverzone < -12*60)
900 server->opt.serverzone /= 60;
902 /* now that we have an established connection we can detect the server
903 type and enable bug workarounds */
904 if (server->opt.protocol < SMB_PROTOCOL_LANMAN2)
905 install_ops(server->ops, &smb_ops_core);
906 else if (server->opt.protocol == SMB_PROTOCOL_LANMAN2)
907 install_ops(server->ops, &smb_ops_os2);
908 else if (server->opt.protocol == SMB_PROTOCOL_NT1 &&
909 (server->opt.max_xmit < 0x1000) &&
910 !(server->opt.capabilities & SMB_CAP_NT_SMBS)) {
911 /* FIXME: can we kill the WIN95 flag now? */
912 server->mnt->flags |= SMB_MOUNT_WIN95;
913 VERBOSE("detected WIN95 server\n");
914 install_ops(server->ops, &smb_ops_win95);
915 } else {
917 * Samba has max_xmit 65535
918 * NT4spX has max_xmit 4536 (or something like that)
919 * win2k has ...
921 VERBOSE("detected NT1 (Samba, NT4/5) server\n");
922 install_ops(server->ops, &smb_ops_winNT);
925 /* FIXME: the win9x code wants to modify these ... (seek/trunc bug) */
926 if (server->mnt->flags & SMB_MOUNT_OLDATTR) {
927 server->ops->getattr = smb_proc_getattr_core;
928 } else if (server->mnt->flags & SMB_MOUNT_DIRATTR) {
929 server->ops->getattr = smb_proc_getattr_ff;
932 /* Decode server capabilities */
933 if (server->opt.capabilities & SMB_CAP_LARGE_FILES) {
934 /* Should be ok to set this now, as no one can access the
935 mount until the connection has been established. */
936 SB_of(server)->s_maxbytes = ~0ULL >> 1;
937 VERBOSE("LFS enabled\n");
939 if (server->opt.capabilities & SMB_CAP_UNICODE) {
940 server->mnt->flags |= SMB_MOUNT_UNICODE;
941 VERBOSE("Unicode enabled\n");
942 } else {
943 server->mnt->flags &= ~SMB_MOUNT_UNICODE;
945 #if 0
946 /* flags we may test for other patches ... */
947 if (server->opt.capabilities & SMB_CAP_LARGE_READX) {
948 VERBOSE("Large reads enabled\n");
950 if (server->opt.capabilities & SMB_CAP_LARGE_WRITEX) {
951 VERBOSE("Large writes enabled\n");
953 #endif
954 if (server->opt.capabilities & SMB_CAP_UNIX) {
955 struct inode *inode;
956 VERBOSE("Using UNIX CIFS extensions\n");
957 install_ops(server->ops, &smb_ops_unix);
958 inode = SB_of(server)->s_root->d_inode;
959 if (inode)
960 inode->i_op = &smb_dir_inode_operations_unix;
963 VERBOSE("protocol=%d, max_xmit=%d, pid=%d capabilities=0x%x\n",
964 server->opt.protocol, server->opt.max_xmit, server->conn_pid,
965 server->opt.capabilities);
967 /* FIXME: this really should be done by smbmount. */
968 if (server->opt.max_xmit > SMB_MAX_PACKET_SIZE) {
969 server->opt.max_xmit = SMB_MAX_PACKET_SIZE;
972 smb_unlock_server(server);
973 smbiod_wake_up();
974 if (server->opt.capabilities & SMB_CAP_UNIX)
975 smb_proc_query_cifsunix(server);
976 return error;
978 out:
979 smb_unlock_server(server);
980 smbiod_wake_up();
981 return error;
983 out_putf:
984 fput(filp);
985 goto out;
988 /* smb_setup_header: We completely set up the packet. You only have to
989 insert the command-specific fields */
991 __u8 *
992 smb_setup_header(struct smb_request *req, __u8 command, __u16 wct, __u16 bcc)
994 __u32 xmit_len = SMB_HEADER_LEN + wct * sizeof(__u16) + bcc + 2;
995 __u8 *p = req->rq_header;
996 struct smb_sb_info *server = req->rq_server;
998 p = smb_encode_smb_length(p, xmit_len - 4);
1000 *p++ = 0xff;
1001 *p++ = 'S';
1002 *p++ = 'M';
1003 *p++ = 'B';
1004 *p++ = command;
1006 memset(p, '\0', 19);
1007 p += 19;
1008 p += 8;
1010 /* FIXME: the request will fail if the 'tid' is changed. This
1011 should perhaps be set just before transmitting ... */
1012 WSET(req->rq_header, smb_tid, server->opt.tid);
1013 WSET(req->rq_header, smb_pid, 1);
1014 WSET(req->rq_header, smb_uid, server->opt.server_uid);
1016 if (server->opt.protocol > SMB_PROTOCOL_CORE) {
1017 int flags = SMB_FLAGS_CASELESS_PATHNAMES;
1018 int flags2 = SMB_FLAGS2_LONG_PATH_COMPONENTS |
1019 SMB_FLAGS2_EXTENDED_ATTRIBUTES; /* EA? not really ... */
1021 *(req->rq_header + smb_flg) = flags;
1022 if (server->mnt->flags & SMB_MOUNT_UNICODE)
1023 flags2 |= SMB_FLAGS2_UNICODE_STRINGS;
1024 WSET(req->rq_header, smb_flg2, flags2);
1026 *p++ = wct; /* wct */
1027 p += 2 * wct;
1028 WSET(p, 0, bcc);
1030 /* Include the header in the data to send */
1031 req->rq_iovlen = 1;
1032 req->rq_iov[0].iov_base = req->rq_header;
1033 req->rq_iov[0].iov_len = xmit_len - bcc;
1035 return req->rq_buffer;
1038 static void
1039 smb_setup_bcc(struct smb_request *req, __u8 *p)
1041 u16 bcc = p - req->rq_buffer;
1042 u8 *pbcc = req->rq_header + SMB_HEADER_LEN + 2*SMB_WCT(req->rq_header);
1044 WSET(pbcc, 0, bcc);
1046 smb_encode_smb_length(req->rq_header, SMB_HEADER_LEN +
1047 2*SMB_WCT(req->rq_header) - 2 + bcc);
1049 /* Include the "bytes" in the data to send */
1050 req->rq_iovlen = 2;
1051 req->rq_iov[1].iov_base = req->rq_buffer;
1052 req->rq_iov[1].iov_len = bcc;
1055 static int
1056 smb_proc_seek(struct smb_sb_info *server, __u16 fileid,
1057 __u16 mode, off_t offset)
1059 int result;
1060 struct smb_request *req;
1062 result = -ENOMEM;
1063 if (! (req = smb_alloc_request(server, 0)))
1064 goto out;
1066 smb_setup_header(req, SMBlseek, 4, 0);
1067 WSET(req->rq_header, smb_vwv0, fileid);
1068 WSET(req->rq_header, smb_vwv1, mode);
1069 DSET(req->rq_header, smb_vwv2, offset);
1070 req->rq_flags |= SMB_REQ_NORETRY;
1072 result = smb_request_ok(req, SMBlseek, 2, 0);
1073 if (result < 0) {
1074 result = 0;
1075 goto out_free;
1078 result = DVAL(req->rq_header, smb_vwv0);
1079 out_free:
1080 smb_rput(req);
1081 out:
1082 return result;
1085 static int
1086 smb_proc_open(struct smb_sb_info *server, struct dentry *dentry, int wish)
1088 struct inode *ino = dentry->d_inode;
1089 struct smb_inode_info *ei = SMB_I(ino);
1090 int mode, read_write = 0x42, read_only = 0x40;
1091 int res;
1092 char *p;
1093 struct smb_request *req;
1096 * Attempt to open r/w, unless there are no write privileges.
1098 mode = read_write;
1099 if (!(ino->i_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1100 mode = read_only;
1101 #if 0
1102 /* FIXME: why is this code not in? below we fix it so that a caller
1103 wanting RO doesn't get RW. smb_revalidate_inode does some
1104 optimization based on access mode. tail -f needs it to be correct.
1106 We must open rw since we don't do the open if called a second time
1107 with different 'wish'. Is that not supported by smb servers? */
1108 if (!(wish & (O_WRONLY | O_RDWR)))
1109 mode = read_only;
1110 #endif
1112 res = -ENOMEM;
1113 if (! (req = smb_alloc_request(server, PAGE_SIZE)))
1114 goto out;
1116 retry:
1117 p = smb_setup_header(req, SMBopen, 2, 0);
1118 WSET(req->rq_header, smb_vwv0, mode);
1119 WSET(req->rq_header, smb_vwv1, aSYSTEM | aHIDDEN | aDIR);
1120 res = smb_simple_encode_path(req, &p, dentry, NULL);
1121 if (res < 0)
1122 goto out_free;
1123 smb_setup_bcc(req, p);
1125 res = smb_request_ok(req, SMBopen, 7, 0);
1126 if (res != 0) {
1127 if (mode == read_write &&
1128 (res == -EACCES || res == -ETXTBSY || res == -EROFS))
1130 VERBOSE("%s/%s R/W failed, error=%d, retrying R/O\n",
1131 DENTRY_PATH(dentry), res);
1132 mode = read_only;
1133 req->rq_flags = 0;
1134 goto retry;
1136 goto out_free;
1138 /* We should now have data in vwv[0..6]. */
1140 ei->fileid = WVAL(req->rq_header, smb_vwv0);
1141 ei->attr = WVAL(req->rq_header, smb_vwv1);
1142 /* smb_vwv2 has mtime */
1143 /* smb_vwv4 has size */
1144 ei->access = (WVAL(req->rq_header, smb_vwv6) & SMB_ACCMASK);
1145 ei->open = server->generation;
1147 out_free:
1148 smb_rput(req);
1149 out:
1150 return res;
1154 * Make sure the file is open, and check that the access
1155 * is compatible with the desired access.
1158 smb_open(struct dentry *dentry, int wish)
1160 struct inode *inode = dentry->d_inode;
1161 int result;
1162 __u16 access;
1164 result = -ENOENT;
1165 if (!inode) {
1166 printk(KERN_ERR "smb_open: no inode for dentry %s/%s\n",
1167 DENTRY_PATH(dentry));
1168 goto out;
1171 if (!smb_is_open(inode)) {
1172 struct smb_sb_info *server = server_from_inode(inode);
1173 result = 0;
1174 if (!smb_is_open(inode))
1175 result = smb_proc_open(server, dentry, wish);
1176 if (result) {
1177 PARANOIA("%s/%s open failed, result=%d\n",
1178 DENTRY_PATH(dentry), result);
1179 goto out;
1182 * A successful open means the path is still valid ...
1184 smb_renew_times(dentry);
1188 * Check whether the access is compatible with the desired mode.
1190 result = 0;
1191 access = SMB_I(inode)->access;
1192 if (access != wish && access != SMB_O_RDWR) {
1193 PARANOIA("%s/%s access denied, access=%x, wish=%x\n",
1194 DENTRY_PATH(dentry), access, wish);
1195 result = -EACCES;
1197 out:
1198 return result;
1201 static int
1202 smb_proc_close(struct smb_sb_info *server, __u16 fileid, __u32 mtime)
1204 struct smb_request *req;
1205 int result = -ENOMEM;
1207 if (! (req = smb_alloc_request(server, 0)))
1208 goto out;
1210 smb_setup_header(req, SMBclose, 3, 0);
1211 WSET(req->rq_header, smb_vwv0, fileid);
1212 DSET(req->rq_header, smb_vwv1, utc2local(server, mtime));
1213 req->rq_flags |= SMB_REQ_NORETRY;
1214 result = smb_request_ok(req, SMBclose, 0, 0);
1216 smb_rput(req);
1217 out:
1218 return result;
1222 * Win NT 4.0 has an apparent bug in that it fails to update the
1223 * modify time when writing to a file. As a workaround, we update
1224 * both modify and access time locally, and post the times to the
1225 * server when closing the file.
1227 static int
1228 smb_proc_close_inode(struct smb_sb_info *server, struct inode * ino)
1230 struct smb_inode_info *ei = SMB_I(ino);
1231 int result = 0;
1232 if (smb_is_open(ino))
1235 * We clear the open flag in advance, in case another
1236 * process observes the value while we block below.
1238 ei->open = 0;
1241 * Kludge alert: SMB timestamps are accurate only to
1242 * two seconds ... round the times to avoid needless
1243 * cache invalidations!
1245 if (ino->i_mtime.tv_sec & 1) {
1246 ino->i_mtime.tv_sec--;
1247 ino->i_mtime.tv_nsec = 0;
1249 if (ino->i_atime.tv_sec & 1) {
1250 ino->i_atime.tv_sec--;
1251 ino->i_atime.tv_nsec = 0;
1254 * If the file is open with write permissions,
1255 * update the time stamps to sync mtime and atime.
1257 if ((server->opt.capabilities & SMB_CAP_UNIX) == 0 &&
1258 (server->opt.protocol >= SMB_PROTOCOL_LANMAN2) &&
1259 !(ei->access == SMB_O_RDONLY))
1261 struct smb_fattr fattr;
1262 smb_get_inode_attr(ino, &fattr);
1263 smb_proc_setattr_ext(server, ino, &fattr);
1266 result = smb_proc_close(server, ei->fileid, ino->i_mtime.tv_sec);
1268 * Force a revalidation after closing ... some servers
1269 * don't post the size until the file has been closed.
1271 if (server->opt.protocol < SMB_PROTOCOL_NT1)
1272 ei->oldmtime = 0;
1273 ei->closed = jiffies;
1275 return result;
1279 smb_close(struct inode *ino)
1281 int result = 0;
1283 if (smb_is_open(ino)) {
1284 struct smb_sb_info *server = server_from_inode(ino);
1285 result = smb_proc_close_inode(server, ino);
1287 return result;
1291 * This is used to close a file following a failed instantiate.
1292 * Since we don't have an inode, we can't use any of the above.
1295 smb_close_fileid(struct dentry *dentry, __u16 fileid)
1297 struct smb_sb_info *server = server_from_dentry(dentry);
1298 int result;
1300 result = smb_proc_close(server, fileid, get_seconds());
1301 return result;
1304 /* In smb_proc_read and smb_proc_write we do not retry, because the
1305 file-id would not be valid after a reconnection. */
1307 static void
1308 smb_proc_read_data(struct smb_request *req)
1310 req->rq_iov[0].iov_base = req->rq_buffer;
1311 req->rq_iov[0].iov_len = 3;
1313 req->rq_iov[1].iov_base = req->rq_page;
1314 req->rq_iov[1].iov_len = req->rq_rsize;
1315 req->rq_iovlen = 2;
1317 req->rq_rlen = smb_len(req->rq_header) + 4 - req->rq_bytes_recvd;
1320 static int
1321 smb_proc_read(struct inode *inode, loff_t offset, int count, char *data)
1323 struct smb_sb_info *server = server_from_inode(inode);
1324 __u16 returned_count, data_len;
1325 unsigned char *buf;
1326 int result;
1327 struct smb_request *req;
1328 u8 rbuf[4];
1330 result = -ENOMEM;
1331 if (! (req = smb_alloc_request(server, 0)))
1332 goto out;
1334 smb_setup_header(req, SMBread, 5, 0);
1335 buf = req->rq_header;
1336 WSET(buf, smb_vwv0, SMB_I(inode)->fileid);
1337 WSET(buf, smb_vwv1, count);
1338 DSET(buf, smb_vwv2, offset);
1339 WSET(buf, smb_vwv4, 0);
1341 req->rq_page = data;
1342 req->rq_rsize = count;
1343 req->rq_callback = smb_proc_read_data;
1344 req->rq_buffer = rbuf;
1345 req->rq_flags |= SMB_REQ_NORETRY | SMB_REQ_STATIC;
1347 result = smb_request_ok(req, SMBread, 5, -1);
1348 if (result < 0)
1349 goto out_free;
1350 returned_count = WVAL(req->rq_header, smb_vwv0);
1352 data_len = WVAL(rbuf, 1);
1354 if (returned_count != data_len) {
1355 printk(KERN_NOTICE "smb_proc_read: returned != data_len\n");
1356 printk(KERN_NOTICE "smb_proc_read: ret_c=%d, data_len=%d\n",
1357 returned_count, data_len);
1359 result = data_len;
1361 out_free:
1362 smb_rput(req);
1363 out:
1364 VERBOSE("ino=%ld, fileid=%d, count=%d, result=%d\n",
1365 inode->i_ino, SMB_I(inode)->fileid, count, result);
1366 return result;
1369 static int
1370 smb_proc_write(struct inode *inode, loff_t offset, int count, const char *data)
1372 struct smb_sb_info *server = server_from_inode(inode);
1373 int result;
1374 u16 fileid = SMB_I(inode)->fileid;
1375 u8 buf[4];
1376 struct smb_request *req;
1378 result = -ENOMEM;
1379 if (! (req = smb_alloc_request(server, 0)))
1380 goto out;
1382 VERBOSE("ino=%ld, fileid=%d, count=%d@%Ld\n",
1383 inode->i_ino, fileid, count, offset);
1385 smb_setup_header(req, SMBwrite, 5, count + 3);
1386 WSET(req->rq_header, smb_vwv0, fileid);
1387 WSET(req->rq_header, smb_vwv1, count);
1388 DSET(req->rq_header, smb_vwv2, offset);
1389 WSET(req->rq_header, smb_vwv4, 0);
1391 buf[0] = 1;
1392 WSET(buf, 1, count); /* yes, again ... */
1393 req->rq_iov[1].iov_base = buf;
1394 req->rq_iov[1].iov_len = 3;
1395 req->rq_iov[2].iov_base = (char *) data;
1396 req->rq_iov[2].iov_len = count;
1397 req->rq_iovlen = 3;
1398 req->rq_flags |= SMB_REQ_NORETRY;
1400 result = smb_request_ok(req, SMBwrite, 1, 0);
1401 if (result >= 0)
1402 result = WVAL(req->rq_header, smb_vwv0);
1404 smb_rput(req);
1405 out:
1406 return result;
1410 * In smb_proc_readX and smb_proc_writeX we do not retry, because the
1411 * file-id would not be valid after a reconnection.
1414 #define SMB_READX_MAX_PAD 64
1415 static void
1416 smb_proc_readX_data(struct smb_request *req)
1418 /* header length, excluding the netbios length (-4) */
1419 int hdrlen = SMB_HEADER_LEN + req->rq_resp_wct*2 - 2;
1420 int data_off = WVAL(req->rq_header, smb_vwv6);
1423 * Some genius made the padding to the data bytes arbitrary.
1424 * So we must first calculate the amount of padding used by the server.
1426 data_off -= hdrlen;
1427 if (data_off > SMB_READX_MAX_PAD) {
1428 PARANOIA("offset is larger than max pad!\n");
1429 PARANOIA("%d > %d\n", data_off, SMB_READX_MAX_PAD);
1430 req->rq_rlen = req->rq_bufsize + 1;
1431 return;
1433 req->rq_iov[0].iov_base = req->rq_buffer;
1434 req->rq_iov[0].iov_len = data_off;
1436 req->rq_iov[1].iov_base = req->rq_page;
1437 req->rq_iov[1].iov_len = req->rq_rsize;
1438 req->rq_iovlen = 2;
1440 req->rq_rlen = smb_len(req->rq_header) + 4 - req->rq_bytes_recvd;
1443 static int
1444 smb_proc_readX(struct inode *inode, loff_t offset, int count, char *data)
1446 struct smb_sb_info *server = server_from_inode(inode);
1447 unsigned char *buf;
1448 int result;
1449 struct smb_request *req;
1450 static char pad[SMB_READX_MAX_PAD];
1452 result = -ENOMEM;
1453 if (! (req = smb_alloc_request(server, 0)))
1454 goto out;
1456 smb_setup_header(req, SMBreadX, 12, 0);
1457 buf = req->rq_header;
1458 WSET(buf, smb_vwv0, 0x00ff);
1459 WSET(buf, smb_vwv1, 0);
1460 WSET(buf, smb_vwv2, SMB_I(inode)->fileid);
1461 DSET(buf, smb_vwv3, (u32)offset); /* low 32 bits */
1462 WSET(buf, smb_vwv5, count);
1463 WSET(buf, smb_vwv6, 0);
1464 DSET(buf, smb_vwv7, 0);
1465 WSET(buf, smb_vwv9, 0);
1466 DSET(buf, smb_vwv10, (u32)(offset >> 32)); /* high 32 bits */
1467 WSET(buf, smb_vwv11, 0);
1469 req->rq_page = data;
1470 req->rq_rsize = count;
1471 req->rq_callback = smb_proc_readX_data;
1472 req->rq_buffer = pad;
1473 req->rq_bufsize = SMB_READX_MAX_PAD;
1474 req->rq_flags |= SMB_REQ_STATIC | SMB_REQ_NORETRY;
1476 result = smb_request_ok(req, SMBreadX, 12, -1);
1477 if (result < 0)
1478 goto out_free;
1479 result = WVAL(req->rq_header, smb_vwv5);
1481 out_free:
1482 smb_rput(req);
1483 out:
1484 VERBOSE("ino=%ld, fileid=%d, count=%d, result=%d\n",
1485 inode->i_ino, SMB_I(inode)->fileid, count, result);
1486 return result;
1489 static int
1490 smb_proc_writeX(struct inode *inode, loff_t offset, int count, const char *data)
1492 struct smb_sb_info *server = server_from_inode(inode);
1493 int result;
1494 u8 *p;
1495 static u8 pad[4];
1496 struct smb_request *req;
1498 result = -ENOMEM;
1499 if (! (req = smb_alloc_request(server, 0)))
1500 goto out;
1502 VERBOSE("ino=%ld, fileid=%d, count=%d@%Ld\n",
1503 inode->i_ino, SMB_I(inode)->fileid, count, offset);
1505 p = smb_setup_header(req, SMBwriteX, 14, count + 1);
1506 WSET(req->rq_header, smb_vwv0, 0x00ff);
1507 WSET(req->rq_header, smb_vwv1, 0);
1508 WSET(req->rq_header, smb_vwv2, SMB_I(inode)->fileid);
1509 DSET(req->rq_header, smb_vwv3, (u32)offset); /* low 32 bits */
1510 DSET(req->rq_header, smb_vwv5, 0);
1511 WSET(req->rq_header, smb_vwv7, 0); /* write mode */
1512 WSET(req->rq_header, smb_vwv8, 0);
1513 WSET(req->rq_header, smb_vwv9, 0);
1514 WSET(req->rq_header, smb_vwv10, count); /* data length */
1515 WSET(req->rq_header, smb_vwv11, smb_vwv12 + 2 + 1);
1516 DSET(req->rq_header, smb_vwv12, (u32)(offset >> 32));
1518 req->rq_iov[1].iov_base = pad;
1519 req->rq_iov[1].iov_len = 1;
1520 req->rq_iov[2].iov_base = (char *) data;
1521 req->rq_iov[2].iov_len = count;
1522 req->rq_iovlen = 3;
1523 req->rq_flags |= SMB_REQ_NORETRY;
1525 result = smb_request_ok(req, SMBwriteX, 6, 0);
1526 if (result >= 0)
1527 result = WVAL(req->rq_header, smb_vwv2);
1529 smb_rput(req);
1530 out:
1531 return result;
1535 smb_proc_create(struct dentry *dentry, __u16 attr, time_t ctime, __u16 *fileid)
1537 struct smb_sb_info *server = server_from_dentry(dentry);
1538 char *p;
1539 int result;
1540 struct smb_request *req;
1542 result = -ENOMEM;
1543 if (! (req = smb_alloc_request(server, PAGE_SIZE)))
1544 goto out;
1546 p = smb_setup_header(req, SMBcreate, 3, 0);
1547 WSET(req->rq_header, smb_vwv0, attr);
1548 DSET(req->rq_header, smb_vwv1, utc2local(server, ctime));
1549 result = smb_simple_encode_path(req, &p, dentry, NULL);
1550 if (result < 0)
1551 goto out_free;
1552 smb_setup_bcc(req, p);
1554 result = smb_request_ok(req, SMBcreate, 1, 0);
1555 if (result < 0)
1556 goto out_free;
1558 *fileid = WVAL(req->rq_header, smb_vwv0);
1559 result = 0;
1561 out_free:
1562 smb_rput(req);
1563 out:
1564 return result;
1568 smb_proc_mv(struct dentry *old_dentry, struct dentry *new_dentry)
1570 struct smb_sb_info *server = server_from_dentry(old_dentry);
1571 char *p;
1572 int result;
1573 struct smb_request *req;
1575 result = -ENOMEM;
1576 if (! (req = smb_alloc_request(server, PAGE_SIZE)))
1577 goto out;
1579 p = smb_setup_header(req, SMBmv, 1, 0);
1580 WSET(req->rq_header, smb_vwv0, aSYSTEM | aHIDDEN | aDIR);
1581 result = smb_simple_encode_path(req, &p, old_dentry, NULL);
1582 if (result < 0)
1583 goto out_free;
1584 result = smb_simple_encode_path(req, &p, new_dentry, NULL);
1585 if (result < 0)
1586 goto out_free;
1587 smb_setup_bcc(req, p);
1589 if ((result = smb_request_ok(req, SMBmv, 0, 0)) < 0)
1590 goto out_free;
1591 result = 0;
1593 out_free:
1594 smb_rput(req);
1595 out:
1596 return result;
1600 * Code common to mkdir and rmdir.
1602 static int
1603 smb_proc_generic_command(struct dentry *dentry, __u8 command)
1605 struct smb_sb_info *server = server_from_dentry(dentry);
1606 char *p;
1607 int result;
1608 struct smb_request *req;
1610 result = -ENOMEM;
1611 if (! (req = smb_alloc_request(server, PAGE_SIZE)))
1612 goto out;
1614 p = smb_setup_header(req, command, 0, 0);
1615 result = smb_simple_encode_path(req, &p, dentry, NULL);
1616 if (result < 0)
1617 goto out_free;
1618 smb_setup_bcc(req, p);
1620 result = smb_request_ok(req, command, 0, 0);
1621 if (result < 0)
1622 goto out_free;
1623 result = 0;
1625 out_free:
1626 smb_rput(req);
1627 out:
1628 return result;
1632 smb_proc_mkdir(struct dentry *dentry)
1634 return smb_proc_generic_command(dentry, SMBmkdir);
1638 smb_proc_rmdir(struct dentry *dentry)
1640 return smb_proc_generic_command(dentry, SMBrmdir);
1643 #if SMBFS_POSIX_UNLINK
1645 * Removes readonly attribute from a file. Used by unlink to give posix
1646 * semantics.
1648 static int
1649 smb_set_rw(struct dentry *dentry,struct smb_sb_info *server)
1651 int result;
1652 struct smb_fattr fattr;
1654 /* FIXME: cifsUE should allow removing a readonly file. */
1656 /* first get current attribute */
1657 smb_init_dirent(server, &fattr);
1658 result = server->ops->getattr(server, dentry, &fattr);
1659 smb_finish_dirent(server, &fattr);
1660 if (result < 0)
1661 return result;
1663 /* if RONLY attribute is set, remove it */
1664 if (fattr.attr & aRONLY) { /* read only attribute is set */
1665 fattr.attr &= ~aRONLY;
1666 result = smb_proc_setattr_core(server, dentry, fattr.attr);
1668 return result;
1670 #endif
1673 smb_proc_unlink(struct dentry *dentry)
1675 struct smb_sb_info *server = server_from_dentry(dentry);
1676 int flag = 0;
1677 char *p;
1678 int result;
1679 struct smb_request *req;
1681 result = -ENOMEM;
1682 if (! (req = smb_alloc_request(server, PAGE_SIZE)))
1683 goto out;
1685 retry:
1686 p = smb_setup_header(req, SMBunlink, 1, 0);
1687 WSET(req->rq_header, smb_vwv0, aSYSTEM | aHIDDEN);
1688 result = smb_simple_encode_path(req, &p, dentry, NULL);
1689 if (result < 0)
1690 goto out_free;
1691 smb_setup_bcc(req, p);
1693 if ((result = smb_request_ok(req, SMBunlink, 0, 0)) < 0) {
1694 #if SMBFS_POSIX_UNLINK
1695 if (result == -EACCES && !flag) {
1696 /* Posix semantics is for the read-only state
1697 of a file to be ignored in unlink(). In the
1698 SMB world a unlink() is refused on a
1699 read-only file. To make things easier for
1700 unix users we try to override the files
1701 permission if the unlink fails with the
1702 right error.
1703 This introduces a race condition that could
1704 lead to a file being written by someone who
1705 shouldn't have access, but as far as I can
1706 tell that is unavoidable */
1708 /* remove RONLY attribute and try again */
1709 result = smb_set_rw(dentry,server);
1710 if (result == 0) {
1711 flag = 1;
1712 req->rq_flags = 0;
1713 goto retry;
1716 #endif
1717 goto out_free;
1719 result = 0;
1721 out_free:
1722 smb_rput(req);
1723 out:
1724 return result;
1728 smb_proc_flush(struct smb_sb_info *server, __u16 fileid)
1730 int result;
1731 struct smb_request *req;
1733 result = -ENOMEM;
1734 if (! (req = smb_alloc_request(server, 0)))
1735 goto out;
1737 smb_setup_header(req, SMBflush, 1, 0);
1738 WSET(req->rq_header, smb_vwv0, fileid);
1739 req->rq_flags |= SMB_REQ_NORETRY;
1740 result = smb_request_ok(req, SMBflush, 0, 0);
1742 smb_rput(req);
1743 out:
1744 return result;
1747 static int
1748 smb_proc_trunc32(struct inode *inode, loff_t length)
1751 * Writing 0bytes is old-SMB magic for truncating files.
1752 * MAX_NON_LFS should prevent this from being called with a too
1753 * large offset.
1755 return smb_proc_write(inode, length, 0, NULL);
1758 static int
1759 smb_proc_trunc64(struct inode *inode, loff_t length)
1761 struct smb_sb_info *server = server_from_inode(inode);
1762 int result;
1763 char *param;
1764 char *data;
1765 struct smb_request *req;
1767 result = -ENOMEM;
1768 if (! (req = smb_alloc_request(server, 14)))
1769 goto out;
1771 param = req->rq_buffer;
1772 data = req->rq_buffer + 6;
1774 /* FIXME: must we also set allocation size? winNT seems to do that */
1775 WSET(param, 0, SMB_I(inode)->fileid);
1776 WSET(param, 2, SMB_SET_FILE_END_OF_FILE_INFO);
1777 WSET(param, 4, 0);
1778 LSET(data, 0, length);
1780 req->rq_trans2_command = TRANSACT2_SETFILEINFO;
1781 req->rq_ldata = 8;
1782 req->rq_data = data;
1783 req->rq_lparm = 6;
1784 req->rq_parm = param;
1785 req->rq_flags |= SMB_REQ_NORETRY;
1786 result = smb_add_request(req);
1787 if (result < 0)
1788 goto out_free;
1790 result = 0;
1791 if (req->rq_rcls != 0)
1792 result = smb_errno(req);
1794 out_free:
1795 smb_rput(req);
1796 out:
1797 return result;
1800 static int
1801 smb_proc_trunc95(struct inode *inode, loff_t length)
1803 struct smb_sb_info *server = server_from_inode(inode);
1804 int result = smb_proc_trunc32(inode, length);
1807 * win9x doesn't appear to update the size immediately.
1808 * It will return the old file size after the truncate,
1809 * confusing smbfs. So we force an update.
1811 * FIXME: is this still necessary?
1813 smb_proc_flush(server, SMB_I(inode)->fileid);
1814 return result;
1817 static void
1818 smb_init_dirent(struct smb_sb_info *server, struct smb_fattr *fattr)
1820 memset(fattr, 0, sizeof(*fattr));
1822 fattr->f_nlink = 1;
1823 fattr->f_uid = server->mnt->uid;
1824 fattr->f_gid = server->mnt->gid;
1825 fattr->f_blksize = SMB_ST_BLKSIZE;
1826 fattr->f_unix = 0;
1829 static void
1830 smb_finish_dirent(struct smb_sb_info *server, struct smb_fattr *fattr)
1832 if (fattr->f_unix)
1833 return;
1835 fattr->f_mode = server->mnt->file_mode;
1836 if (fattr->attr & aDIR) {
1837 fattr->f_mode = server->mnt->dir_mode;
1838 fattr->f_size = SMB_ST_BLKSIZE;
1840 /* Check the read-only flag */
1841 if (fattr->attr & aRONLY)
1842 fattr->f_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
1844 /* How many 512 byte blocks do we need for this file? */
1845 fattr->f_blocks = 0;
1846 if (fattr->f_size != 0)
1847 fattr->f_blocks = 1 + ((fattr->f_size-1) >> 9);
1848 return;
1851 void
1852 smb_init_root_dirent(struct smb_sb_info *server, struct smb_fattr *fattr)
1854 smb_init_dirent(server, fattr);
1855 fattr->attr = aDIR;
1856 fattr->f_ino = 2; /* traditional root inode number */
1857 fattr->f_mtime = CURRENT_TIME;
1858 smb_finish_dirent(server, fattr);
1862 * Decode a dirent for old protocols
1864 * qname is filled with the decoded, and possibly translated, name.
1865 * fattr receives decoded attributes
1867 * Bugs Noted:
1868 * (1) Pathworks servers may pad the name with extra spaces.
1870 static char *
1871 smb_decode_short_dirent(struct smb_sb_info *server, char *p,
1872 struct qstr *qname, struct smb_fattr *fattr,
1873 unsigned char *name_buf)
1875 int len;
1878 * SMB doesn't have a concept of inode numbers ...
1880 smb_init_dirent(server, fattr);
1881 fattr->f_ino = 0; /* FIXME: do we need this? */
1883 p += SMB_STATUS_SIZE; /* reserved (search_status) */
1884 fattr->attr = *p;
1885 fattr->f_mtime.tv_sec = date_dos2unix(server, WVAL(p, 3), WVAL(p, 1));
1886 fattr->f_mtime.tv_nsec = 0;
1887 fattr->f_size = DVAL(p, 5);
1888 fattr->f_ctime = fattr->f_mtime;
1889 fattr->f_atime = fattr->f_mtime;
1890 qname->name = p + 9;
1891 len = strnlen(qname->name, 12);
1894 * Trim trailing blanks for Pathworks servers
1896 while (len > 2 && qname->name[len-1] == ' ')
1897 len--;
1899 smb_finish_dirent(server, fattr);
1901 #if 0
1902 /* FIXME: These only work for ascii chars, and recent smbmount doesn't
1903 allow the flag to be set anyway. It kills const. Remove? */
1904 switch (server->opt.case_handling) {
1905 case SMB_CASE_UPPER:
1906 str_upper(entry->name, len);
1907 break;
1908 case SMB_CASE_LOWER:
1909 str_lower(entry->name, len);
1910 break;
1911 default:
1912 break;
1914 #endif
1916 qname->len = 0;
1917 len = server->ops->convert(name_buf, SMB_MAXNAMELEN,
1918 qname->name, len,
1919 server->remote_nls, server->local_nls);
1920 if (len > 0) {
1921 qname->len = len;
1922 qname->name = name_buf;
1923 DEBUG1("len=%d, name=%.*s\n",qname->len,qname->len,qname->name);
1926 return p + 22;
1930 * This routine is used to read in directory entries from the network.
1931 * Note that it is for short directory name seeks, i.e.: protocol <
1932 * SMB_PROTOCOL_LANMAN2
1934 static int
1935 smb_proc_readdir_short(struct file *filp, void *dirent, filldir_t filldir,
1936 struct smb_cache_control *ctl)
1938 struct dentry *dir = filp->f_dentry;
1939 struct smb_sb_info *server = server_from_dentry(dir);
1940 struct qstr qname;
1941 struct smb_fattr fattr;
1942 char *p;
1943 int result;
1944 int i, first, entries_seen, entries;
1945 int entries_asked = (server->opt.max_xmit - 100) / SMB_DIRINFO_SIZE;
1946 __u16 bcc;
1947 __u16 count;
1948 char status[SMB_STATUS_SIZE];
1949 static struct qstr mask = {
1950 .name = "*.*",
1951 .len = 3,
1953 unsigned char *last_status;
1954 struct smb_request *req;
1955 unsigned char *name_buf;
1957 VERBOSE("%s/%s\n", DENTRY_PATH(dir));
1959 lock_kernel();
1961 result = -ENOMEM;
1962 if (! (name_buf = kmalloc(SMB_MAXNAMELEN, GFP_KERNEL)))
1963 goto out;
1965 first = 1;
1966 entries = 0;
1967 entries_seen = 2; /* implicit . and .. */
1969 result = -ENOMEM;
1970 if (! (req = smb_alloc_request(server, server->opt.max_xmit)))
1971 goto out_name;
1973 while (1) {
1974 p = smb_setup_header(req, SMBsearch, 2, 0);
1975 WSET(req->rq_header, smb_vwv0, entries_asked);
1976 WSET(req->rq_header, smb_vwv1, aDIR);
1977 if (first == 1) {
1978 result = smb_simple_encode_path(req, &p, dir, &mask);
1979 if (result < 0)
1980 goto out_free;
1981 if (p + 3 > (char *)req->rq_buffer + req->rq_bufsize) {
1982 result = -ENAMETOOLONG;
1983 goto out_free;
1985 *p++ = 5;
1986 WSET(p, 0, 0);
1987 p += 2;
1988 first = 0;
1989 } else {
1990 if (p + 5 + SMB_STATUS_SIZE >
1991 (char *)req->rq_buffer + req->rq_bufsize) {
1992 result = -ENAMETOOLONG;
1993 goto out_free;
1996 *p++ = 4;
1997 *p++ = 0;
1998 *p++ = 5;
1999 WSET(p, 0, SMB_STATUS_SIZE);
2000 p += 2;
2001 memcpy(p, status, SMB_STATUS_SIZE);
2002 p += SMB_STATUS_SIZE;
2005 smb_setup_bcc(req, p);
2007 result = smb_request_ok(req, SMBsearch, 1, -1);
2008 if (result < 0) {
2009 if ((req->rq_rcls == ERRDOS) &&
2010 (req->rq_err == ERRnofiles))
2011 break;
2012 goto out_free;
2014 count = WVAL(req->rq_header, smb_vwv0);
2015 if (count <= 0)
2016 break;
2018 result = -EIO;
2019 bcc = smb_bcc(req->rq_header);
2020 if (bcc != count * SMB_DIRINFO_SIZE + 3)
2021 goto out_free;
2022 p = req->rq_buffer + 3;
2025 /* Make sure the response fits in the buffer. Fixed sized
2026 entries means we don't have to check in the decode loop. */
2028 last_status = req->rq_buffer + 3 + (count-1) * SMB_DIRINFO_SIZE;
2030 if (last_status + SMB_DIRINFO_SIZE >=
2031 req->rq_buffer + req->rq_bufsize) {
2032 printk(KERN_ERR "smb_proc_readdir_short: "
2033 "last dir entry outside buffer! "
2034 "%d@%p %d@%p\n", SMB_DIRINFO_SIZE, last_status,
2035 req->rq_bufsize, req->rq_buffer);
2036 goto out_free;
2039 /* Read the last entry into the status field. */
2040 memcpy(status, last_status, SMB_STATUS_SIZE);
2043 /* Now we are ready to parse smb directory entries. */
2045 for (i = 0; i < count; i++) {
2046 p = smb_decode_short_dirent(server, p,
2047 &qname, &fattr, name_buf);
2048 if (qname.len == 0)
2049 continue;
2051 if (entries_seen == 2 && qname.name[0] == '.') {
2052 if (qname.len == 1)
2053 continue;
2054 if (qname.name[1] == '.' && qname.len == 2)
2055 continue;
2057 if (!smb_fill_cache(filp, dirent, filldir, ctl,
2058 &qname, &fattr))
2059 ; /* stop reading? */
2060 entries_seen++;
2063 result = entries;
2065 out_free:
2066 smb_rput(req);
2067 out_name:
2068 kfree(name_buf);
2069 out:
2070 unlock_kernel();
2071 return result;
2074 void smb_decode_unix_basic(struct smb_fattr *fattr, char *p)
2076 /* FIXME: verify nls support. all is sent as utf8? */
2077 __u64 devmajor, devminor;
2079 fattr->f_unix = 1;
2080 fattr->f_mode = 0;
2082 /* FIXME: use the uniqueID from the remote instead? */
2083 /* 0 L file size in bytes */
2084 /* 8 L file size on disk in bytes (block count) */
2085 /* 40 L uid */
2086 /* 48 L gid */
2087 /* 56 W file type */
2088 /* 60 L devmajor */
2089 /* 68 L devminor */
2090 /* 76 L unique ID (inode) */
2091 /* 84 L permissions */
2092 /* 92 L link count */
2094 fattr->f_size = LVAL(p, 0);
2095 fattr->f_blocks = LVAL(p, 8);
2096 fattr->f_ctime = smb_ntutc2unixutc(LVAL(p, 16));
2097 fattr->f_atime = smb_ntutc2unixutc(LVAL(p, 24));
2098 fattr->f_mtime = smb_ntutc2unixutc(LVAL(p, 32));
2099 fattr->f_uid = LVAL(p, 40);
2100 fattr->f_gid = LVAL(p, 48);
2101 fattr->f_mode |= smb_filetype_to_mode(WVAL(p, 56));
2103 if (S_ISBLK(fattr->f_mode) || S_ISCHR(fattr->f_mode)) {
2104 devmajor = LVAL(p, 60);
2105 devminor = LVAL(p, 68);
2106 fattr->f_rdev = ((devmajor & 0xFF) << 8) | (devminor & 0xFF);
2108 fattr->f_mode |= LVAL(p, 84);
2112 * Interpret a long filename structure using the specified info level:
2113 * level 1 for anything below NT1 protocol
2114 * level 260 for NT1 protocol
2116 * qname is filled with the decoded, and possibly translated, name
2117 * fattr receives decoded attributes.
2119 * Bugs Noted:
2120 * (1) Win NT 4.0 appends a null byte to names and counts it in the length!
2122 static char *
2123 smb_decode_long_dirent(struct smb_sb_info *server, char *p, int level,
2124 struct qstr *qname, struct smb_fattr *fattr,
2125 unsigned char *name_buf)
2127 char *result;
2128 unsigned int len = 0;
2129 int n;
2130 __u16 date, time;
2131 int unicode = (server->mnt->flags & SMB_MOUNT_UNICODE);
2134 * SMB doesn't have a concept of inode numbers ...
2136 smb_init_dirent(server, fattr);
2137 fattr->f_ino = 0; /* FIXME: do we need this? */
2139 switch (level) {
2140 case 1:
2141 len = *((unsigned char *) p + 22);
2142 qname->name = p + 23;
2143 result = p + 24 + len;
2145 date = WVAL(p, 0);
2146 time = WVAL(p, 2);
2147 fattr->f_ctime.tv_sec = date_dos2unix(server, date, time);
2148 fattr->f_ctime.tv_nsec = 0;
2150 date = WVAL(p, 4);
2151 time = WVAL(p, 6);
2152 fattr->f_atime.tv_sec = date_dos2unix(server, date, time);
2153 fattr->f_atime.tv_nsec = 0;
2155 date = WVAL(p, 8);
2156 time = WVAL(p, 10);
2157 fattr->f_mtime.tv_sec = date_dos2unix(server, date, time);
2158 fattr->f_mtime.tv_nsec = 0;
2159 fattr->f_size = DVAL(p, 12);
2160 /* ULONG allocation size */
2161 fattr->attr = WVAL(p, 20);
2163 VERBOSE("info 1 at %p, len=%d, name=%.*s\n",
2164 p, len, len, qname->name);
2165 break;
2166 case 260:
2167 result = p + WVAL(p, 0);
2168 len = DVAL(p, 60);
2169 if (len > 255) len = 255;
2170 /* NT4 null terminates, unless we are using unicode ... */
2171 qname->name = p + 94;
2172 if (!unicode && len && qname->name[len-1] == '\0')
2173 len--;
2175 fattr->f_ctime = smb_ntutc2unixutc(LVAL(p, 8));
2176 fattr->f_atime = smb_ntutc2unixutc(LVAL(p, 16));
2177 fattr->f_mtime = smb_ntutc2unixutc(LVAL(p, 24));
2178 /* change time (32) */
2179 fattr->f_size = LVAL(p, 40);
2180 /* alloc size (48) */
2181 fattr->attr = DVAL(p, 56);
2183 VERBOSE("info 260 at %p, len=%d, name=%.*s\n",
2184 p, len, len, qname->name);
2185 break;
2186 case SMB_FIND_FILE_UNIX:
2187 result = p + WVAL(p, 0);
2188 qname->name = p + 108;
2190 len = strlen(qname->name);
2191 /* FIXME: should we check the length?? */
2193 p += 8;
2194 smb_decode_unix_basic(fattr, p);
2195 VERBOSE("info SMB_FIND_FILE_UNIX at %p, len=%d, name=%.*s\n",
2196 p, len, len, qname->name);
2197 break;
2198 default:
2199 PARANOIA("Unknown info level %d\n", level);
2200 result = p + WVAL(p, 0);
2201 goto out;
2204 smb_finish_dirent(server, fattr);
2206 #if 0
2207 /* FIXME: These only work for ascii chars, and recent smbmount doesn't
2208 allow the flag to be set anyway. Remove? */
2209 switch (server->opt.case_handling) {
2210 case SMB_CASE_UPPER:
2211 str_upper(qname->name, len);
2212 break;
2213 case SMB_CASE_LOWER:
2214 str_lower(qname->name, len);
2215 break;
2216 default:
2217 break;
2219 #endif
2221 qname->len = 0;
2222 n = server->ops->convert(name_buf, SMB_MAXNAMELEN,
2223 qname->name, len,
2224 server->remote_nls, server->local_nls);
2225 if (n > 0) {
2226 qname->len = n;
2227 qname->name = name_buf;
2230 out:
2231 return result;
2234 /* findfirst/findnext flags */
2235 #define SMB_CLOSE_AFTER_FIRST (1<<0)
2236 #define SMB_CLOSE_IF_END (1<<1)
2237 #define SMB_REQUIRE_RESUME_KEY (1<<2)
2238 #define SMB_CONTINUE_BIT (1<<3)
2241 * Note: samba-2.0.7 (at least) has a very similar routine, cli_list, in
2242 * source/libsmb/clilist.c. When looking for smb bugs in the readdir code,
2243 * go there for advise.
2245 * Bugs Noted:
2246 * (1) When using Info Level 1 Win NT 4.0 truncates directory listings
2247 * for certain patterns of names and/or lengths. The breakage pattern
2248 * is completely reproducible and can be toggled by the creation of a
2249 * single file. (E.g. echo hi >foo breaks, rm -f foo works.)
2251 static int
2252 smb_proc_readdir_long(struct file *filp, void *dirent, filldir_t filldir,
2253 struct smb_cache_control *ctl)
2255 struct dentry *dir = filp->f_dentry;
2256 struct smb_sb_info *server = server_from_dentry(dir);
2257 struct qstr qname;
2258 struct smb_fattr fattr;
2260 unsigned char *p, *lastname;
2261 char *mask, *param;
2262 __u16 command;
2263 int first, entries_seen;
2265 /* Both NT and OS/2 accept info level 1 (but see note below). */
2266 int info_level = 260;
2267 const int max_matches = 512;
2269 unsigned int ff_searchcount = 0;
2270 unsigned int ff_eos = 0;
2271 unsigned int ff_lastname = 0;
2272 unsigned int ff_dir_handle = 0;
2273 unsigned int loop_count = 0;
2274 unsigned int mask_len, i;
2275 int result;
2276 struct smb_request *req;
2277 unsigned char *name_buf;
2278 static struct qstr star = {
2279 .name = "*",
2280 .len = 1,
2283 lock_kernel();
2286 * We always prefer unix style. Use info level 1 for older
2287 * servers that don't do 260.
2289 if (server->opt.capabilities & SMB_CAP_UNIX)
2290 info_level = SMB_FIND_FILE_UNIX;
2291 else if (server->opt.protocol < SMB_PROTOCOL_NT1)
2292 info_level = 1;
2294 result = -ENOMEM;
2295 if (! (name_buf = kmalloc(SMB_MAXNAMELEN+2, GFP_KERNEL)))
2296 goto out;
2297 if (! (req = smb_alloc_request(server, server->opt.max_xmit)))
2298 goto out_name;
2299 param = req->rq_buffer;
2302 * Encode the initial path
2304 mask = param + 12;
2306 mask_len = smb_encode_path(server, mask, SMB_MAXPATHLEN+1, dir, &star);
2307 if (mask_len < 0) {
2308 result = mask_len;
2309 goto out_free;
2311 mask_len--; /* mask_len is strlen, not #bytes */
2312 first = 1;
2313 VERBOSE("starting mask_len=%d, mask=%s\n", mask_len, mask);
2315 result = 0;
2316 entries_seen = 2;
2317 ff_eos = 0;
2319 while (ff_eos == 0) {
2320 loop_count += 1;
2321 if (loop_count > 10) {
2322 printk(KERN_WARNING "smb_proc_readdir_long: "
2323 "Looping in FIND_NEXT??\n");
2324 result = -EIO;
2325 break;
2328 if (first != 0) {
2329 command = TRANSACT2_FINDFIRST;
2330 WSET(param, 0, aSYSTEM | aHIDDEN | aDIR);
2331 WSET(param, 2, max_matches); /* max count */
2332 WSET(param, 4, SMB_CLOSE_IF_END);
2333 WSET(param, 6, info_level);
2334 DSET(param, 8, 0);
2335 } else {
2336 command = TRANSACT2_FINDNEXT;
2338 VERBOSE("handle=0x%X, lastname=%d, mask=%.*s\n",
2339 ff_dir_handle, ff_lastname, mask_len, mask);
2341 WSET(param, 0, ff_dir_handle); /* search handle */
2342 WSET(param, 2, max_matches); /* max count */
2343 WSET(param, 4, info_level);
2344 DSET(param, 6, 0);
2345 WSET(param, 10, SMB_CONTINUE_BIT|SMB_CLOSE_IF_END);
2348 req->rq_trans2_command = command;
2349 req->rq_ldata = 0;
2350 req->rq_data = NULL;
2351 req->rq_lparm = 12 + mask_len + 1;
2352 req->rq_parm = param;
2353 req->rq_flags = 0;
2354 result = smb_add_request(req);
2355 if (result < 0) {
2356 PARANOIA("error=%d, breaking\n", result);
2357 break;
2360 if (req->rq_rcls == ERRSRV && req->rq_err == ERRerror) {
2361 /* a damn Win95 bug - sometimes it clags if you
2362 ask it too fast */
2363 current->state = TASK_INTERRUPTIBLE;
2364 schedule_timeout(HZ/5);
2365 continue;
2368 if (req->rq_rcls != 0) {
2369 result = smb_errno(req);
2370 PARANOIA("name=%s, result=%d, rcls=%d, err=%d\n",
2371 mask, result, server->rcls, server->err);
2372 break;
2375 /* parse out some important return info */
2376 if (first != 0) {
2377 ff_dir_handle = WVAL(req->rq_parm, 0);
2378 ff_searchcount = WVAL(req->rq_parm, 2);
2379 ff_eos = WVAL(req->rq_parm, 4);
2380 ff_lastname = WVAL(req->rq_parm, 8);
2381 } else {
2382 ff_searchcount = WVAL(req->rq_parm, 0);
2383 ff_eos = WVAL(req->rq_parm, 2);
2384 ff_lastname = WVAL(req->rq_parm, 6);
2387 if (ff_searchcount == 0)
2388 break;
2390 /* Now we are ready to parse smb directory entries. */
2392 /* point to the data bytes */
2393 p = req->rq_data;
2394 for (i = 0; i < ff_searchcount; i++) {
2395 /* make sure we stay within the buffer */
2396 if (p >= req->rq_data + req->rq_ldata) {
2397 printk(KERN_ERR "smb_proc_readdir_long: "
2398 "dirent pointer outside buffer! "
2399 "%p %d@%p\n",
2400 p, req->rq_ldata, req->rq_data);
2401 result = -EIO; /* always a comm. error? */
2402 goto out_free;
2405 p = smb_decode_long_dirent(server, p, info_level,
2406 &qname, &fattr, name_buf);
2408 /* ignore . and .. from the server */
2409 if (entries_seen == 2 && qname.name[0] == '.') {
2410 if (qname.len == 1)
2411 continue;
2412 if (qname.name[1] == '.' && qname.len == 2)
2413 continue;
2416 if (!smb_fill_cache(filp, dirent, filldir, ctl,
2417 &qname, &fattr))
2418 ; /* stop reading? */
2419 entries_seen++;
2422 VERBOSE("received %d entries, eos=%d\n", ff_searchcount,ff_eos);
2425 * We might need the lastname for continuations.
2427 * Note that some servers (win95?) point to the filename and
2428 * others (NT4, Samba using NT1) to the dir entry. We assume
2429 * here that those who do not point to a filename do not need
2430 * this info to continue the listing.
2432 * OS/2 needs this and talks infolevel 1.
2433 * NetApps want lastname with infolevel 260.
2434 * win2k want lastname with infolevel 260, and points to
2435 * the record not to the name.
2436 * Samba+CifsUnixExt doesn't need lastname.
2438 * Both are happy if we return the data they point to. So we do.
2439 * (FIXME: above is not true with win2k)
2441 mask_len = 0;
2442 if (info_level != SMB_FIND_FILE_UNIX &&
2443 ff_lastname > 0 && ff_lastname < req->rq_ldata) {
2444 lastname = req->rq_data + ff_lastname;
2446 switch (info_level) {
2447 case 260:
2448 mask_len = req->rq_ldata - ff_lastname;
2449 break;
2450 case 1:
2451 /* lastname points to a length byte */
2452 mask_len = *lastname++;
2453 if (ff_lastname + 1 + mask_len > req->rq_ldata)
2454 mask_len = req->rq_ldata - ff_lastname - 1;
2455 break;
2459 * Update the mask string for the next message.
2461 if (mask_len < 0)
2462 mask_len = 0;
2463 if (mask_len > 255)
2464 mask_len = 255;
2465 if (mask_len)
2466 strncpy(mask, lastname, mask_len);
2468 mask_len = strnlen(mask, mask_len);
2469 VERBOSE("new mask, len=%d@%d of %d, mask=%.*s\n",
2470 mask_len, ff_lastname, req->rq_ldata, mask_len, mask);
2472 first = 0;
2473 loop_count = 0;
2476 out_free:
2477 smb_rput(req);
2478 out_name:
2479 kfree(name_buf);
2480 out:
2481 unlock_kernel();
2482 return result;
2486 * This version uses the trans2 TRANSACT2_FINDFIRST message
2487 * to get the attribute data.
2489 * Bugs Noted:
2491 static int
2492 smb_proc_getattr_ff(struct smb_sb_info *server, struct dentry *dentry,
2493 struct smb_fattr *fattr)
2495 char *param, *mask;
2496 __u16 date, time;
2497 int mask_len, result;
2498 struct smb_request *req;
2500 result = -ENOMEM;
2501 if (! (req = smb_alloc_request(server, PAGE_SIZE)))
2502 goto out;
2503 param = req->rq_buffer;
2504 mask = param + 12;
2506 mask_len = smb_encode_path(server, mask, SMB_MAXPATHLEN+1, dentry,NULL);
2507 if (mask_len < 0) {
2508 result = mask_len;
2509 goto out_free;
2511 VERBOSE("name=%s, len=%d\n", mask, mask_len);
2512 WSET(param, 0, aSYSTEM | aHIDDEN | aDIR);
2513 WSET(param, 2, 1); /* max count */
2514 WSET(param, 4, 1); /* close after this call */
2515 WSET(param, 6, 1); /* info_level */
2516 DSET(param, 8, 0);
2518 req->rq_trans2_command = TRANSACT2_FINDFIRST;
2519 req->rq_ldata = 0;
2520 req->rq_data = NULL;
2521 req->rq_lparm = 12 + mask_len;
2522 req->rq_parm = param;
2523 req->rq_flags = 0;
2524 result = smb_add_request(req);
2525 if (result < 0)
2526 goto out_free;
2527 if (server->rcls != 0) {
2528 result = smb_errno(req);
2529 #ifdef SMBFS_PARANOIA
2530 if (result != -ENOENT)
2531 PARANOIA("error for %s, rcls=%d, err=%d\n",
2532 mask, req->rq_rcls, req->rq_err);
2533 #endif
2534 goto out_free;
2536 /* Make sure we got enough data ... */
2537 result = -EINVAL;
2538 if (req->rq_ldata < 22 || WVAL(req->rq_parm, 2) != 1) {
2539 PARANOIA("bad result for %s, len=%d, count=%d\n",
2540 mask, req->rq_ldata, WVAL(req->rq_parm, 2));
2541 goto out_free;
2545 * Decode the response into the fattr ...
2547 date = WVAL(req->rq_data, 0);
2548 time = WVAL(req->rq_data, 2);
2549 fattr->f_ctime.tv_sec = date_dos2unix(server, date, time);
2550 fattr->f_ctime.tv_nsec = 0;
2552 date = WVAL(req->rq_data, 4);
2553 time = WVAL(req->rq_data, 6);
2554 fattr->f_atime.tv_sec = date_dos2unix(server, date, time);
2555 fattr->f_atime.tv_nsec = 0;
2557 date = WVAL(req->rq_data, 8);
2558 time = WVAL(req->rq_data, 10);
2559 fattr->f_mtime.tv_sec = date_dos2unix(server, date, time);
2560 fattr->f_mtime.tv_nsec = 0;
2561 VERBOSE("name=%s, date=%x, time=%x, mtime=%ld\n",
2562 mask, date, time, fattr->f_mtime);
2563 fattr->f_size = DVAL(req->rq_data, 12);
2564 /* ULONG allocation size */
2565 fattr->attr = WVAL(req->rq_data, 20);
2566 result = 0;
2568 out_free:
2569 smb_rput(req);
2570 out:
2571 return result;
2574 static int
2575 smb_proc_getattr_core(struct smb_sb_info *server, struct dentry *dir,
2576 struct smb_fattr *fattr)
2578 int result;
2579 char *p;
2580 struct smb_request *req;
2582 result = -ENOMEM;
2583 if (! (req = smb_alloc_request(server, PAGE_SIZE)))
2584 goto out;
2586 p = smb_setup_header(req, SMBgetatr, 0, 0);
2587 result = smb_simple_encode_path(req, &p, dir, NULL);
2588 if (result < 0)
2589 goto out_free;
2590 smb_setup_bcc(req, p);
2592 if ((result = smb_request_ok(req, SMBgetatr, 10, 0)) < 0)
2593 goto out_free;
2594 fattr->attr = WVAL(req->rq_header, smb_vwv0);
2595 fattr->f_mtime.tv_sec = local2utc(server, DVAL(req->rq_header, smb_vwv1));
2596 fattr->f_mtime.tv_nsec = 0;
2597 fattr->f_size = DVAL(req->rq_header, smb_vwv3);
2598 fattr->f_ctime = fattr->f_mtime;
2599 fattr->f_atime = fattr->f_mtime;
2600 #ifdef SMBFS_DEBUG_TIMESTAMP
2601 printk("getattr_core: %s/%s, mtime=%ld\n",
2602 DENTRY_PATH(dir), fattr->f_mtime);
2603 #endif
2604 result = 0;
2606 out_free:
2607 smb_rput(req);
2608 out:
2609 return result;
2613 * Bugs Noted:
2614 * (1) Win 95 swaps the date and time fields in the standard info level.
2616 static int
2617 smb_proc_getattr_trans2(struct smb_sb_info *server, struct dentry *dir,
2618 struct smb_request *req, int infolevel)
2620 char *p, *param;
2621 int result;
2623 param = req->rq_buffer;
2624 WSET(param, 0, infolevel);
2625 DSET(param, 2, 0);
2626 result = smb_encode_path(server, param+6, SMB_MAXPATHLEN+1, dir, NULL);
2627 if (result < 0)
2628 goto out;
2629 p = param + 6 + result;
2631 req->rq_trans2_command = TRANSACT2_QPATHINFO;
2632 req->rq_ldata = 0;
2633 req->rq_data = NULL;
2634 req->rq_lparm = p - param;
2635 req->rq_parm = param;
2636 req->rq_flags = 0;
2637 result = smb_add_request(req);
2638 if (result < 0)
2639 goto out;
2640 if (server->rcls != 0) {
2641 VERBOSE("for %s: result=%d, rcls=%d, err=%d\n",
2642 &param[6], result, req->rq_rcls, req->rq_err);
2643 result = smb_errno(req);
2644 goto out;
2646 result = -ENOENT;
2647 if (req->rq_ldata < 22) {
2648 PARANOIA("not enough data for %s, len=%d\n",
2649 &param[6], req->rq_ldata);
2650 goto out;
2653 result = 0;
2654 out:
2655 return result;
2658 static int
2659 smb_proc_getattr_trans2_std(struct smb_sb_info *server, struct dentry *dir,
2660 struct smb_fattr *attr)
2662 u16 date, time;
2663 int off_date = 0, off_time = 2;
2664 int result;
2665 struct smb_request *req;
2667 result = -ENOMEM;
2668 if (! (req = smb_alloc_request(server, PAGE_SIZE)))
2669 goto out;
2671 result = smb_proc_getattr_trans2(server, dir, req, SMB_INFO_STANDARD);
2672 if (result < 0)
2673 goto out_free;
2676 * Kludge alert: Win 95 swaps the date and time field,
2677 * contrary to the CIFS docs and Win NT practice.
2679 if (server->mnt->flags & SMB_MOUNT_WIN95) {
2680 off_date = 2;
2681 off_time = 0;
2683 date = WVAL(req->rq_data, off_date);
2684 time = WVAL(req->rq_data, off_time);
2685 attr->f_ctime.tv_sec = date_dos2unix(server, date, time);
2686 attr->f_ctime.tv_nsec = 0;
2688 date = WVAL(req->rq_data, 4 + off_date);
2689 time = WVAL(req->rq_data, 4 + off_time);
2690 attr->f_atime.tv_sec = date_dos2unix(server, date, time);
2691 attr->f_atime.tv_nsec = 0;
2693 date = WVAL(req->rq_data, 8 + off_date);
2694 time = WVAL(req->rq_data, 8 + off_time);
2695 attr->f_mtime.tv_sec = date_dos2unix(server, date, time);
2696 attr->f_mtime.tv_nsec = 0;
2697 #ifdef SMBFS_DEBUG_TIMESTAMP
2698 printk(KERN_DEBUG "getattr_trans2: %s/%s, date=%x, time=%x, mtime=%ld\n",
2699 DENTRY_PATH(dir), date, time, attr->f_mtime);
2700 #endif
2701 attr->f_size = DVAL(req->rq_data, 12);
2702 attr->attr = WVAL(req->rq_data, 20);
2704 out_free:
2705 smb_rput(req);
2706 out:
2707 return result;
2710 static int
2711 smb_proc_getattr_trans2_all(struct smb_sb_info *server, struct dentry *dir,
2712 struct smb_fattr *attr)
2714 struct smb_request *req;
2715 int result;
2717 result = -ENOMEM;
2718 if (! (req = smb_alloc_request(server, PAGE_SIZE)))
2719 goto out;
2721 result = smb_proc_getattr_trans2(server, dir, req,
2722 SMB_QUERY_FILE_ALL_INFO);
2723 if (result < 0)
2724 goto out_free;
2726 attr->f_ctime = smb_ntutc2unixutc(LVAL(req->rq_data, 0));
2727 attr->f_atime = smb_ntutc2unixutc(LVAL(req->rq_data, 8));
2728 attr->f_mtime = smb_ntutc2unixutc(LVAL(req->rq_data, 16));
2729 /* change (24) */
2730 attr->attr = WVAL(req->rq_data, 32);
2731 /* pad? (34) */
2732 /* allocated size (40) */
2733 attr->f_size = LVAL(req->rq_data, 48);
2735 out_free:
2736 smb_rput(req);
2737 out:
2738 return result;
2741 static int
2742 smb_proc_getattr_unix(struct smb_sb_info *server, struct dentry *dir,
2743 struct smb_fattr *attr)
2745 struct smb_request *req;
2746 int result;
2748 result = -ENOMEM;
2749 if (! (req = smb_alloc_request(server, PAGE_SIZE)))
2750 goto out;
2752 result = smb_proc_getattr_trans2(server, dir, req,
2753 SMB_QUERY_FILE_UNIX_BASIC);
2754 if (result < 0)
2755 goto out_free;
2757 smb_decode_unix_basic(attr, req->rq_data);
2759 out_free:
2760 smb_rput(req);
2761 out:
2762 return result;
2765 static int
2766 smb_proc_getattr_95(struct smb_sb_info *server, struct dentry *dir,
2767 struct smb_fattr *attr)
2769 struct inode *inode = dir->d_inode;
2770 int result;
2772 /* FIXME: why not use the "all" version? */
2773 result = smb_proc_getattr_trans2_std(server, dir, attr);
2774 if (result < 0)
2775 goto out;
2778 * None of the getattr versions here can make win9x return the right
2779 * filesize if there are changes made to an open file.
2780 * A seek-to-end does return the right size, but we only need to do
2781 * that on files we have written.
2783 if (inode && SMB_I(inode)->flags & SMB_F_LOCALWRITE &&
2784 smb_is_open(inode))
2786 __u16 fileid = SMB_I(inode)->fileid;
2787 attr->f_size = smb_proc_seek(server, fileid, 2, 0);
2790 out:
2791 return result;
2795 smb_proc_getattr(struct dentry *dir, struct smb_fattr *fattr)
2797 struct smb_sb_info *server = server_from_dentry(dir);
2798 int result;
2800 smb_init_dirent(server, fattr);
2801 result = server->ops->getattr(server, dir, fattr);
2802 smb_finish_dirent(server, fattr);
2804 return result;
2809 * Because of bugs in the core protocol, we use this only to set
2810 * attributes. See smb_proc_settime() below for timestamp handling.
2812 * Bugs Noted:
2813 * (1) If mtime is non-zero, both Win 3.1 and Win 95 fail
2814 * with an undocumented error (ERRDOS code 50). Setting
2815 * mtime to 0 allows the attributes to be set.
2816 * (2) The extra parameters following the name string aren't
2817 * in the CIFS docs, but seem to be necessary for operation.
2819 static int
2820 smb_proc_setattr_core(struct smb_sb_info *server, struct dentry *dentry,
2821 __u16 attr)
2823 char *p;
2824 int result;
2825 struct smb_request *req;
2827 result = -ENOMEM;
2828 if (! (req = smb_alloc_request(server, PAGE_SIZE)))
2829 goto out;
2831 p = smb_setup_header(req, SMBsetatr, 8, 0);
2832 WSET(req->rq_header, smb_vwv0, attr);
2833 DSET(req->rq_header, smb_vwv1, 0); /* mtime */
2834 WSET(req->rq_header, smb_vwv3, 0); /* reserved values */
2835 WSET(req->rq_header, smb_vwv4, 0);
2836 WSET(req->rq_header, smb_vwv5, 0);
2837 WSET(req->rq_header, smb_vwv6, 0);
2838 WSET(req->rq_header, smb_vwv7, 0);
2839 result = smb_simple_encode_path(req, &p, dentry, NULL);
2840 if (result < 0)
2841 goto out_free;
2842 if (p + 2 > (char *)req->rq_buffer + req->rq_bufsize) {
2843 result = -ENAMETOOLONG;
2844 goto out_free;
2846 *p++ = 4;
2847 *p++ = 0;
2848 smb_setup_bcc(req, p);
2850 result = smb_request_ok(req, SMBsetatr, 0, 0);
2851 if (result < 0)
2852 goto out_free;
2853 result = 0;
2855 out_free:
2856 smb_rput(req);
2857 out:
2858 return result;
2862 * Because of bugs in the trans2 setattr messages, we must set
2863 * attributes and timestamps separately. The core SMBsetatr
2864 * message seems to be the only reliable way to set attributes.
2867 smb_proc_setattr(struct dentry *dir, struct smb_fattr *fattr)
2869 struct smb_sb_info *server = server_from_dentry(dir);
2870 int result;
2872 VERBOSE("setting %s/%s, open=%d\n",
2873 DENTRY_PATH(dir), smb_is_open(dir->d_inode));
2874 result = smb_proc_setattr_core(server, dir, fattr->attr);
2875 return result;
2879 * Sets the timestamps for an file open with write permissions.
2881 static int
2882 smb_proc_setattr_ext(struct smb_sb_info *server,
2883 struct inode *inode, struct smb_fattr *fattr)
2885 __u16 date, time;
2886 int result;
2887 struct smb_request *req;
2889 result = -ENOMEM;
2890 if (! (req = smb_alloc_request(server, 0)))
2891 goto out;
2893 smb_setup_header(req, SMBsetattrE, 7, 0);
2894 WSET(req->rq_header, smb_vwv0, SMB_I(inode)->fileid);
2895 /* We don't change the creation time */
2896 WSET(req->rq_header, smb_vwv1, 0);
2897 WSET(req->rq_header, smb_vwv2, 0);
2898 date_unix2dos(server, fattr->f_atime.tv_sec, &date, &time);
2899 WSET(req->rq_header, smb_vwv3, date);
2900 WSET(req->rq_header, smb_vwv4, time);
2901 date_unix2dos(server, fattr->f_mtime.tv_sec, &date, &time);
2902 WSET(req->rq_header, smb_vwv5, date);
2903 WSET(req->rq_header, smb_vwv6, time);
2904 #ifdef SMBFS_DEBUG_TIMESTAMP
2905 printk(KERN_DEBUG "smb_proc_setattr_ext: date=%d, time=%d, mtime=%ld\n",
2906 date, time, fattr->f_mtime);
2907 #endif
2909 req->rq_flags |= SMB_REQ_NORETRY;
2910 result = smb_request_ok(req, SMBsetattrE, 0, 0);
2911 if (result < 0)
2912 goto out_free;
2913 result = 0;
2914 out_free:
2915 smb_rput(req);
2916 out:
2917 return result;
2921 * Bugs Noted:
2922 * (1) The TRANSACT2_SETPATHINFO message under Win NT 4.0 doesn't
2923 * set the file's attribute flags.
2925 static int
2926 smb_proc_setattr_trans2(struct smb_sb_info *server,
2927 struct dentry *dir, struct smb_fattr *fattr)
2929 __u16 date, time;
2930 char *p, *param;
2931 int result;
2932 char data[26];
2933 struct smb_request *req;
2935 result = -ENOMEM;
2936 if (! (req = smb_alloc_request(server, PAGE_SIZE)))
2937 goto out;
2938 param = req->rq_buffer;
2940 WSET(param, 0, 1); /* Info level SMB_INFO_STANDARD */
2941 DSET(param, 2, 0);
2942 result = smb_encode_path(server, param+6, SMB_MAXPATHLEN+1, dir, NULL);
2943 if (result < 0)
2944 goto out_free;
2945 p = param + 6 + result;
2947 WSET(data, 0, 0); /* creation time */
2948 WSET(data, 2, 0);
2949 date_unix2dos(server, fattr->f_atime.tv_sec, &date, &time);
2950 WSET(data, 4, date);
2951 WSET(data, 6, time);
2952 date_unix2dos(server, fattr->f_mtime.tv_sec, &date, &time);
2953 WSET(data, 8, date);
2954 WSET(data, 10, time);
2955 #ifdef SMBFS_DEBUG_TIMESTAMP
2956 printk(KERN_DEBUG "setattr_trans2: %s/%s, date=%x, time=%x, mtime=%ld\n",
2957 DENTRY_PATH(dir), date, time, fattr->f_mtime);
2958 #endif
2959 DSET(data, 12, 0); /* size */
2960 DSET(data, 16, 0); /* blksize */
2961 WSET(data, 20, 0); /* attr */
2962 DSET(data, 22, 0); /* ULONG EA size */
2964 req->rq_trans2_command = TRANSACT2_SETPATHINFO;
2965 req->rq_ldata = 26;
2966 req->rq_data = data;
2967 req->rq_lparm = p - param;
2968 req->rq_parm = param;
2969 req->rq_flags = 0;
2970 result = smb_add_request(req);
2971 if (result < 0)
2972 goto out_free;
2973 result = 0;
2974 if (req->rq_rcls != 0)
2975 result = smb_errno(req);
2977 out_free:
2978 smb_rput(req);
2979 out:
2980 return result;
2984 * ATTR_MODE 0x001
2985 * ATTR_UID 0x002
2986 * ATTR_GID 0x004
2987 * ATTR_SIZE 0x008
2988 * ATTR_ATIME 0x010
2989 * ATTR_MTIME 0x020
2990 * ATTR_CTIME 0x040
2991 * ATTR_ATIME_SET 0x080
2992 * ATTR_MTIME_SET 0x100
2993 * ATTR_FORCE 0x200
2994 * ATTR_ATTR_FLAG 0x400
2996 * major/minor should only be set by mknod.
2999 smb_proc_setattr_unix(struct dentry *d, struct iattr *attr,
3000 int major, int minor)
3002 struct smb_sb_info *server = server_from_dentry(d);
3003 u64 nttime;
3004 char *p, *param;
3005 int result;
3006 char data[100];
3007 struct smb_request *req;
3009 result = -ENOMEM;
3010 if (! (req = smb_alloc_request(server, PAGE_SIZE)))
3011 goto out;
3012 param = req->rq_buffer;
3014 DEBUG1("valid flags = 0x%04x\n", attr->ia_valid);
3016 WSET(param, 0, SMB_SET_FILE_UNIX_BASIC);
3017 DSET(param, 2, 0);
3018 result = smb_encode_path(server, param+6, SMB_MAXPATHLEN+1, d, NULL);
3019 if (result < 0)
3020 goto out_free;
3021 p = param + 6 + result;
3023 /* 0 L file size in bytes */
3024 /* 8 L file size on disk in bytes (block count) */
3025 /* 40 L uid */
3026 /* 48 L gid */
3027 /* 56 W file type enum */
3028 /* 60 L devmajor */
3029 /* 68 L devminor */
3030 /* 76 L unique ID (inode) */
3031 /* 84 L permissions */
3032 /* 92 L link count */
3033 LSET(data, 0, SMB_SIZE_NO_CHANGE);
3034 LSET(data, 8, SMB_SIZE_NO_CHANGE);
3035 LSET(data, 16, SMB_TIME_NO_CHANGE);
3036 LSET(data, 24, SMB_TIME_NO_CHANGE);
3037 LSET(data, 32, SMB_TIME_NO_CHANGE);
3038 LSET(data, 40, SMB_UID_NO_CHANGE);
3039 LSET(data, 48, SMB_GID_NO_CHANGE);
3040 LSET(data, 56, smb_filetype_from_mode(attr->ia_mode));
3041 LSET(data, 60, major);
3042 LSET(data, 68, minor);
3043 LSET(data, 76, 0);
3044 LSET(data, 84, SMB_MODE_NO_CHANGE);
3045 LSET(data, 92, 0);
3047 if (attr->ia_valid & ATTR_SIZE) {
3048 LSET(data, 0, attr->ia_size);
3049 LSET(data, 8, 0); /* can't set anyway */
3053 * FIXME: check the conversion function it the correct one
3055 * we can't set ctime but we might as well pass this to the server
3056 * and let it ignore it.
3058 if (attr->ia_valid & ATTR_CTIME) {
3059 nttime = smb_unixutc2ntutc(attr->ia_ctime);
3060 LSET(data, 16, nttime);
3062 if (attr->ia_valid & ATTR_ATIME) {
3063 nttime = smb_unixutc2ntutc(attr->ia_atime);
3064 LSET(data, 24, nttime);
3066 if (attr->ia_valid & ATTR_MTIME) {
3067 nttime = smb_unixutc2ntutc(attr->ia_mtime);
3068 LSET(data, 32, nttime);
3071 if (attr->ia_valid & ATTR_UID) {
3072 LSET(data, 40, attr->ia_uid);
3074 if (attr->ia_valid & ATTR_GID) {
3075 LSET(data, 48, attr->ia_gid);
3078 if (attr->ia_valid & ATTR_MODE) {
3079 LSET(data, 84, attr->ia_mode);
3082 req->rq_trans2_command = TRANSACT2_SETPATHINFO;
3083 req->rq_ldata = 100;
3084 req->rq_data = data;
3085 req->rq_lparm = p - param;
3086 req->rq_parm = param;
3087 req->rq_flags = 0;
3088 result = smb_add_request(req);
3090 out_free:
3091 smb_rput(req);
3092 out:
3093 return result;
3098 * Set the modify and access timestamps for a file.
3100 * Incredibly enough, in all of SMB there is no message to allow
3101 * setting both attributes and timestamps at once.
3103 * Bugs Noted:
3104 * (1) Win 95 doesn't support the TRANSACT2_SETFILEINFO message
3105 * with info level 1 (INFO_STANDARD).
3106 * (2) Win 95 seems not to support setting directory timestamps.
3107 * (3) Under the core protocol apparently the only way to set the
3108 * timestamp is to open and close the file.
3111 smb_proc_settime(struct dentry *dentry, struct smb_fattr *fattr)
3113 struct smb_sb_info *server = server_from_dentry(dentry);
3114 struct inode *inode = dentry->d_inode;
3115 int result;
3117 VERBOSE("setting %s/%s, open=%d\n",
3118 DENTRY_PATH(dentry), smb_is_open(inode));
3120 /* setting the time on a Win95 server fails (tridge) */
3121 if (server->opt.protocol >= SMB_PROTOCOL_LANMAN2 &&
3122 !(server->mnt->flags & SMB_MOUNT_WIN95)) {
3123 if (smb_is_open(inode) && SMB_I(inode)->access != SMB_O_RDONLY)
3124 result = smb_proc_setattr_ext(server, inode, fattr);
3125 else
3126 result = smb_proc_setattr_trans2(server, dentry, fattr);
3127 } else {
3129 * Fail silently on directories ... timestamp can't be set?
3131 result = 0;
3132 if (S_ISREG(inode->i_mode)) {
3134 * Set the mtime by opening and closing the file.
3135 * Note that the file is opened read-only, but this
3136 * still allows us to set the date (tridge)
3138 result = -EACCES;
3139 if (!smb_is_open(inode))
3140 smb_proc_open(server, dentry, SMB_O_RDONLY);
3141 if (smb_is_open(inode)) {
3142 inode->i_mtime = fattr->f_mtime;
3143 result = smb_proc_close_inode(server, inode);
3148 return result;
3152 smb_proc_dskattr(struct super_block *sb, struct statfs *attr)
3154 struct smb_sb_info *server = SMB_SB(sb);
3155 int result;
3156 char *p;
3157 long unit;
3158 struct smb_request *req;
3160 result = -ENOMEM;
3161 if (! (req = smb_alloc_request(server, 0)))
3162 goto out;
3164 smb_setup_header(req, SMBdskattr, 0, 0);
3165 if ((result = smb_request_ok(req, SMBdskattr, 5, 0)) < 0)
3166 goto out_free;
3167 p = SMB_VWV(req->rq_header);
3168 unit = (WVAL(p, 2) * WVAL(p, 4)) >> SMB_ST_BLKSHIFT;
3169 attr->f_blocks = WVAL(p, 0) * unit;
3170 attr->f_bsize = SMB_ST_BLKSIZE;
3171 attr->f_bavail = attr->f_bfree = WVAL(p, 6) * unit;
3172 result = 0;
3174 out_free:
3175 smb_rput(req);
3176 out:
3177 return result;
3181 smb_proc_read_link(struct smb_sb_info *server, struct dentry *d,
3182 char *buffer, int len)
3184 char *p, *param;
3185 int result;
3186 struct smb_request *req;
3188 DEBUG1("readlink of %s/%s\n", DENTRY_PATH(d));
3190 result = -ENOMEM;
3191 if (! (req = smb_alloc_request(server, PAGE_SIZE)))
3192 goto out;
3193 param = req->rq_buffer;
3195 WSET(param, 0, SMB_QUERY_FILE_UNIX_LINK);
3196 DSET(param, 2, 0);
3197 result = smb_encode_path(server, param+6, SMB_MAXPATHLEN+1, d, NULL);
3198 if (result < 0)
3199 goto out_free;
3200 p = param + 6 + result;
3202 req->rq_trans2_command = TRANSACT2_QPATHINFO;
3203 req->rq_ldata = 0;
3204 req->rq_data = NULL;
3205 req->rq_lparm = p - param;
3206 req->rq_parm = param;
3207 req->rq_flags = 0;
3208 result = smb_add_request(req);
3209 if (result < 0)
3210 goto out_free;
3211 DEBUG1("for %s: result=%d, rcls=%d, err=%d\n",
3212 &param[6], result, server->rcls, server->err);
3214 /* copy data up to the \0 or buffer length */
3215 result = len;
3216 if (req->rq_ldata < len)
3217 result = req->rq_ldata;
3218 strncpy(buffer, req->rq_data, result);
3220 out_free:
3221 smb_rput(req);
3222 out:
3223 return result;
3228 * Create a symlink object called dentry which points to oldpath.
3229 * Samba does not permit dangling links but returns a suitable error message.
3232 smb_proc_symlink(struct smb_sb_info *server, struct dentry *d,
3233 const char *oldpath)
3235 char *p, *param;
3236 int result;
3237 struct smb_request *req;
3239 result = -ENOMEM;
3240 if (! (req = smb_alloc_request(server, PAGE_SIZE)))
3241 goto out;
3242 param = req->rq_buffer;
3244 WSET(param, 0, SMB_SET_FILE_UNIX_LINK);
3245 DSET(param, 2, 0);
3246 result = smb_encode_path(server, param + 6, SMB_MAXPATHLEN+1, d, NULL);
3247 if (result < 0)
3248 goto out_free;
3249 p = param + 6 + result;
3251 req->rq_trans2_command = TRANSACT2_SETPATHINFO;
3252 req->rq_ldata = strlen(oldpath) + 1;
3253 req->rq_data = (char *) oldpath;
3254 req->rq_lparm = p - param;
3255 req->rq_parm = param;
3256 req->rq_flags = 0;
3257 result = smb_add_request(req);
3258 if (result < 0)
3259 goto out_free;
3261 DEBUG1("for %s: result=%d, rcls=%d, err=%d\n",
3262 &param[6], result, server->rcls, server->err);
3263 result = 0;
3265 out_free:
3266 smb_rput(req);
3267 out:
3268 return result;
3272 * Create a hard link object called new_dentry which points to dentry.
3275 smb_proc_link(struct smb_sb_info *server, struct dentry *dentry,
3276 struct dentry *new_dentry)
3278 char *p, *param;
3279 int result;
3280 struct smb_request *req;
3282 result = -ENOMEM;
3283 if (! (req = smb_alloc_request(server, PAGE_SIZE)))
3284 goto out;
3285 param = req->rq_buffer;
3287 WSET(param, 0, SMB_SET_FILE_UNIX_HLINK);
3288 DSET(param, 2, 0);
3289 result = smb_encode_path(server, param + 6, SMB_MAXPATHLEN+1,
3290 new_dentry, NULL);
3291 if (result < 0)
3292 goto out_free;
3293 p = param + 6 + result;
3295 /* Grr, pointless separation of parameters and data ... */
3296 req->rq_data = p;
3297 req->rq_ldata = smb_encode_path(server, p, SMB_MAXPATHLEN+1,
3298 dentry, NULL);
3300 req->rq_trans2_command = TRANSACT2_SETPATHINFO;
3301 req->rq_lparm = p - param;
3302 req->rq_parm = param;
3303 req->rq_flags = 0;
3304 result = smb_add_request(req);
3305 if (result < 0)
3306 goto out_free;
3308 DEBUG1("for %s: result=%d, rcls=%d, err=%d\n",
3309 &param[6], result, server->rcls, server->err);
3310 result = 0;
3312 out_free:
3313 smb_rput(req);
3314 out:
3315 return result;
3319 smb_proc_query_cifsunix(struct smb_sb_info *server)
3321 int result;
3322 int major, minor;
3323 u64 caps;
3324 char param[2];
3325 struct smb_request *req;
3327 result = -ENOMEM;
3328 if (! (req = smb_alloc_request(server, 100)))
3329 goto out;
3331 WSET(param, 0, SMB_QUERY_CIFS_UNIX_INFO);
3333 req->rq_trans2_command = TRANSACT2_QFSINFO;
3334 req->rq_ldata = 0;
3335 req->rq_data = NULL;
3336 req->rq_lparm = 2;
3337 req->rq_parm = param;
3338 req->rq_flags = 0;
3339 result = smb_add_request(req);
3340 if (result < 0)
3341 goto out_free;
3343 if (req->rq_ldata < 12) {
3344 PARANOIA("Not enough data\n");
3345 goto out_free;
3347 major = WVAL(req->rq_data, 0);
3348 minor = WVAL(req->rq_data, 2);
3350 DEBUG1("Server implements CIFS Extensions for UNIX systems v%d.%d\n",
3351 major, minor);
3352 /* FIXME: verify that we are ok with this major/minor? */
3354 caps = LVAL(req->rq_data, 4);
3355 DEBUG1("Server capabilities 0x%016llx\n", caps);
3357 out_free:
3358 smb_rput(req);
3359 out:
3360 return result;
3364 static void
3365 install_ops(struct smb_ops *dst, struct smb_ops *src)
3367 memcpy(dst, src, sizeof(void *) * SMB_OPS_NUM_STATIC);
3370 /* < LANMAN2 */
3371 static struct smb_ops smb_ops_core =
3373 .read = smb_proc_read,
3374 .write = smb_proc_write,
3375 .readdir = smb_proc_readdir_short,
3376 .getattr = smb_proc_getattr_core,
3377 .truncate = smb_proc_trunc32,
3380 /* LANMAN2, OS/2, others? */
3381 static struct smb_ops smb_ops_os2 =
3383 .read = smb_proc_read,
3384 .write = smb_proc_write,
3385 .readdir = smb_proc_readdir_long,
3386 .getattr = smb_proc_getattr_trans2_std,
3387 .truncate = smb_proc_trunc32,
3390 /* Win95, and possibly some NetApp versions too */
3391 static struct smb_ops smb_ops_win95 =
3393 .read = smb_proc_read, /* does not support 12word readX */
3394 .write = smb_proc_write,
3395 .readdir = smb_proc_readdir_long,
3396 .getattr = smb_proc_getattr_95,
3397 .truncate = smb_proc_trunc95,
3400 /* Samba, NT4 and NT5 */
3401 static struct smb_ops smb_ops_winNT =
3403 .read = smb_proc_readX,
3404 .write = smb_proc_writeX,
3405 .readdir = smb_proc_readdir_long,
3406 .getattr = smb_proc_getattr_trans2_all,
3407 .truncate = smb_proc_trunc64,
3410 /* Samba w/ unix extensions. Others? */
3411 static struct smb_ops smb_ops_unix =
3413 .read = smb_proc_readX,
3414 .write = smb_proc_writeX,
3415 .readdir = smb_proc_readdir_long,
3416 .getattr = smb_proc_getattr_unix,
3417 /* FIXME: core/ext/time setattr needs to be cleaned up! */
3418 /* .setattr = smb_proc_setattr_unix, */
3419 .truncate = smb_proc_trunc64,