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.
10 #include <linux/types.h>
11 #include <linux/errno.h>
12 #include <linux/slab.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>
21 #include <linux/smb_fs.h>
22 #include <linux/smbno.h>
23 #include <linux/smb_mount.h>
25 #include <asm/string.h>
26 #include <asm/div64.h>
28 #include "smb_debug.h"
32 /* Features. Undefine if they cause problems, this should perhaps be a
34 #define SMBFS_POSIX_UNLINK 1
36 #define SMB_VWV(packet) ((packet) + SMB_HEADER_LEN)
37 #define SMB_CMD(packet) (*(packet+8))
38 #define SMB_WCT(packet) (*(packet+SMB_HEADER_LEN - 1))
39 #define SMB_BCC(packet) smb_bcc(packet)
40 #define SMB_BUF(packet) ((packet) + SMB_HEADER_LEN + SMB_WCT(packet) * 2 + 2)
42 #define SMB_DIRINFO_SIZE 43
43 #define SMB_STATUS_SIZE 21
45 #define SMB_ST_BLKSIZE (PAGE_SIZE)
46 #define SMB_ST_BLKSHIFT (PAGE_SHIFT)
48 static struct smb_ops smb_ops_core;
49 static struct smb_ops smb_ops_os2;
50 static struct smb_ops smb_ops_win95;
51 static struct smb_ops smb_ops_winNT;
52 static struct smb_ops smb_ops_unix;
55 smb_init_dirent(struct smb_sb_info *server, struct smb_fattr *fattr);
57 smb_finish_dirent(struct smb_sb_info *server, struct smb_fattr *fattr);
59 smb_proc_getattr_core(struct smb_sb_info *server, struct dentry *dir,
60 struct smb_fattr *fattr);
62 smb_proc_getattr_ff(struct smb_sb_info *server, struct dentry *dir,
63 struct smb_fattr *fattr);
65 smb_proc_setattr_ext(struct smb_sb_info *, struct inode *,
68 smb_proc_setattr_core(struct smb_sb_info *server, struct dentry *dentry,
71 install_ops(struct smb_ops *dst, struct smb_ops *src);
73 smb_proc_query_cifsunix(struct smb_sb_info *server);
77 str_upper(char *name, int len)
81 if (*name >= 'a' && *name <= 'z')
89 str_lower(char *name, int len)
93 if (*name >= 'A' && *name <= 'Z')
100 /* reverse a string inline. This is used by the dircache walking routines */
101 static void reverse_string(char *buf, int len)
104 char *end = buf+len-1;
113 /* no conversion, just a wrapper for memcpy. */
114 static int convert_memcpy(unsigned char *output, int olen,
115 const unsigned char *input, int ilen,
116 struct nls_table *nls_from,
117 struct nls_table *nls_to)
120 return -ENAMETOOLONG;
121 memcpy(output, input, ilen);
125 static inline int write_char(unsigned char ch, char *output, int olen)
128 return -ENAMETOOLONG;
129 sprintf(output, ":x%02x", ch);
133 static inline int write_unichar(wchar_t ch, char *output, int olen)
136 return -ENAMETOOLONG;
137 sprintf(output, ":%04x", ch);
141 /* convert from one "codepage" to another (possibly being utf8). */
142 static int convert_cp(unsigned char *output, int olen,
143 const unsigned char *input, int ilen,
144 struct nls_table *nls_from,
145 struct nls_table *nls_to)
152 /* convert by changing to unicode and back to the new cp */
153 n = nls_from->char2uni((unsigned char *)input, ilen, &ch);
156 n = write_char(*input++, output, olen);
168 n = nls_to->uni2char(ch, output, olen);
170 n = write_unichar(ch, output, olen);
183 static int setcodepage(struct nls_table **p, char *name)
185 struct nls_table *nls;
187 if (!name || !*name) {
189 } else if ( (nls = load_nls(name)) == NULL) {
190 printk (KERN_ERR "smbfs: failed to load nls '%s'\n", name);
194 /* if already set, unload the previous one. */
202 /* Handles all changes to codepage settings. */
203 int smb_setcodepage(struct smb_sb_info *server, struct smb_nls_codepage *cp)
207 smb_lock_server(server);
209 /* Don't load any nls_* at all, if no remote is requested */
210 if (!*cp->remote_name)
213 n = setcodepage(&server->local_nls, cp->local_name);
216 n = setcodepage(&server->remote_nls, cp->remote_name);
218 setcodepage(&server->local_nls, NULL);
221 if (server->local_nls != NULL && server->remote_nls != NULL)
222 server->ops->convert = convert_cp;
224 server->ops->convert = convert_memcpy;
226 smb_unlock_server(server);
231 /*****************************************************************************/
233 /* Encoding/Decoding section */
235 /*****************************************************************************/
238 smb_encode_smb_length(__u8 * p, __u32 len)
242 *(p+2) = (len & 0xFF00) >> 8;
243 *(p+3) = (len & 0xFF);
252 * smb_build_path: build the path to entry and name storing it in buf.
253 * The path returned will have the trailing '\0'.
255 static int smb_build_path(struct smb_sb_info *server, char * buf, int maxlen,
256 struct dentry * entry, struct qstr * name)
262 return -ENAMETOOLONG;
264 if (maxlen > SMB_MAXPATHLEN + 1)
265 maxlen = SMB_MAXPATHLEN + 1;
268 goto test_name_and_out;
271 * If IS_ROOT, we have to do no walking at all.
273 if (IS_ROOT(entry) && !name) {
280 * Build the path string walking the tree backward from end to ROOT
281 * and store it in reversed order [see reverse_string()]
283 while (!IS_ROOT(entry)) {
285 return -ENAMETOOLONG;
287 len = server->ops->convert(path, maxlen-2,
288 entry->d_name.name, entry->d_name.len,
289 server->local_nls, server->remote_nls);
292 reverse_string(path, len);
297 entry = entry->d_parent;
301 reverse_string(buf, path-buf);
303 /* maxlen is at least 1 */
307 return -ENAMETOOLONG;
309 len = server->ops->convert(path, maxlen-2,
310 name->name, name->len,
311 server->local_nls, server->remote_nls);
317 /* maxlen is at least 1 */
322 static int smb_encode_path(struct smb_sb_info *server, char *buf, int maxlen,
323 struct dentry *dir, struct qstr *name)
327 result = smb_build_path(server, buf, maxlen, dir, name);
330 if (server->opt.protocol <= SMB_PROTOCOL_COREPLUS)
331 str_upper(buf, result);
336 static int smb_simple_encode_path(struct smb_sb_info *server, char **p,
337 struct dentry * entry, struct qstr * name)
341 int maxlen = ((char *)server->packet + server->packet_size) - s;
344 return -ENAMETOOLONG;
346 res = smb_encode_path(server, s, maxlen-1, entry, name);
353 /* The following are taken directly from msdos-fs */
355 /* Linear day numbers of the respective 1sts in non-leap years. */
358 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 0, 0, 0, 0};
359 /* JanFebMarApr May Jun Jul Aug Sep Oct Nov Dec */
363 utc2local(struct smb_sb_info *server, time_t time)
365 return time - server->opt.serverzone*60;
369 local2utc(struct smb_sb_info *server, time_t time)
371 return time + server->opt.serverzone*60;
374 /* Convert a MS-DOS time/date pair to a UNIX date (seconds since 1 1 70). */
377 date_dos2unix(struct smb_sb_info *server, __u16 date, __u16 time)
382 /* first subtract and mask after that... Otherwise, if
383 date == 0, bad things happen */
384 month = ((date >> 5) - 1) & 15;
386 secs = (time & 31) * 2 + 60 * ((time >> 5) & 63) + (time >> 11) * 3600 + 86400 *
387 ((date & 31) - 1 + day_n[month] + (year / 4) + year * 365 - ((year & 3) == 0 &&
388 month < 2 ? 1 : 0) + 3653);
389 /* days since 1.1.70 plus 80's leap day */
390 return local2utc(server, secs);
394 /* Convert linear UNIX date to a MS-DOS time/date pair. */
397 date_unix2dos(struct smb_sb_info *server,
398 int unix_date, __u16 *date, __u16 *time)
400 int day, year, nl_day, month;
402 unix_date = utc2local(server, unix_date);
403 if (unix_date < 315532800)
404 unix_date = 315532800;
406 *time = (unix_date % 60) / 2 +
407 (((unix_date / 60) % 60) << 5) +
408 (((unix_date / 3600) % 24) << 11);
410 day = unix_date / 86400 - 3652;
412 if ((year + 3) / 4 + 365 * year > day)
414 day -= (year + 3) / 4 + 365 * year;
415 if (day == 59 && !(year & 3)) {
419 nl_day = (year & 3) || day <= 59 ? day : day - 1;
420 for (month = 0; month < 12; month++)
421 if (day_n[month] > nl_day)
424 *date = nl_day - day_n[month - 1] + 1 + (month << 5) + (year << 9);
427 /* The following are taken from fs/ntfs/util.c */
429 #define NTFS_TIME_OFFSET ((u64)(369*365 + 89) * 24 * 3600 * 10000000)
432 * Convert the NT UTC (based 1601-01-01, in hundred nanosecond units)
433 * into Unix UTC (based 1970-01-01, in seconds).
436 smb_ntutc2unixutc(u64 ntutc)
438 /* FIXME: what about the timezone difference? */
439 /* Subtract the NTFS time offset, then convert to 1s intervals. */
440 u64 t = ntutc - NTFS_TIME_OFFSET;
446 smb_unixutc2ntutc(time_t t)
448 /* Note: timezone conversion is probably wrong. */
449 return ((u64)t) * 10000000 + NTFS_TIME_OFFSET;
452 #define MAX_FILE_MODE 6
453 static mode_t file_mode[] = {
454 S_IFREG, S_IFDIR, S_IFLNK, S_IFCHR, S_IFBLK, S_IFIFO, S_IFSOCK
457 static int smb_filetype_to_mode(u32 filetype)
459 if (filetype > MAX_FILE_MODE) {
460 PARANOIA("Filetype out of range: %d\n", filetype);
463 return file_mode[filetype];
466 static u32 smb_filetype_from_mode(int mode)
469 return UNIX_TYPE_FILE;
471 return UNIX_TYPE_DIR;
473 return UNIX_TYPE_SYMLINK;
475 return UNIX_TYPE_CHARDEV;
477 return UNIX_TYPE_BLKDEV;
479 return UNIX_TYPE_FIFO;
481 return UNIX_TYPE_SOCKET;
482 return UNIX_TYPE_UNKNOWN;
486 /*****************************************************************************/
488 /* Support section. */
490 /*****************************************************************************/
495 return ((*(p+1) & 0x1) << 16L) | (*(p+2) << 8L) | *(p+3);
499 smb_bcc(__u8 * packet)
501 int pos = SMB_HEADER_LEN + SMB_WCT(packet) * sizeof(__u16);
502 return WVAL(packet, pos);
505 /* smb_valid_packet: We check if packet fulfills the basic
506 requirements of a smb packet */
509 smb_valid_packet(__u8 * packet)
511 return (packet[4] == 0xff
515 && (smb_len(packet) + 4 == SMB_HEADER_LEN
516 + SMB_WCT(packet) * 2 + SMB_BCC(packet)));
519 /* smb_verify: We check if we got the answer we expected, and if we
520 got enough data. If bcc == -1, we don't care. */
523 smb_verify(__u8 * packet, int command, int wct, int bcc)
525 if (SMB_CMD(packet) != command)
527 if (SMB_WCT(packet) < wct)
529 if (bcc != -1 && SMB_BCC(packet) < bcc)
534 printk(KERN_ERR "smb_verify: command=%x, SMB_CMD=%x??\n",
535 command, SMB_CMD(packet));
538 printk(KERN_ERR "smb_verify: command=%x, wct=%d, SMB_WCT=%d??\n",
539 command, wct, SMB_WCT(packet));
542 printk(KERN_ERR "smb_verify: command=%x, bcc=%d, SMB_BCC=%d??\n",
543 command, bcc, SMB_BCC(packet));
549 * Returns the maximum read or write size for the "payload". Making all of the
550 * packet fit within the negotiated max_xmit size.
552 * N.B. Since this value is usually computed before locking the server,
553 * the server's packet size must never be decreased!
556 smb_get_xmitsize(struct smb_sb_info *server, int overhead)
558 return server->opt.max_xmit - overhead;
562 * Calculate the maximum read size
565 smb_get_rsize(struct smb_sb_info *server)
567 /* readX has 12 parameters, read has 5 */
568 int overhead = SMB_HEADER_LEN + 12 * sizeof(__u16) + 2 + 1 + 2;
569 int size = smb_get_xmitsize(server, overhead);
571 VERBOSE("packet=%d, xmit=%d, size=%d\n",
572 server->packet_size, server->opt.max_xmit, size);
578 * Calculate the maximum write size
581 smb_get_wsize(struct smb_sb_info *server)
583 /* writeX has 14 parameters, write has 5 */
584 int overhead = SMB_HEADER_LEN + 14 * sizeof(__u16) + 2 + 1 + 2;
585 int size = smb_get_xmitsize(server, overhead);
587 VERBOSE("packet=%d, xmit=%d, size=%d\n",
588 server->packet_size, server->opt.max_xmit, size);
594 * Convert SMB error codes to -E... errno values.
597 smb_errno(struct smb_sb_info *server)
599 int errcls = server->rcls;
600 int error = server->err;
601 char *class = "Unknown";
603 VERBOSE("errcls %d code %d from command 0x%x\n",
604 errcls, error, SMB_CMD(server->packet));
606 if (errcls == ERRDOS) {
646 case ERROR_INVALID_PARAMETER:
648 case ERROR_DISK_FULL:
650 case ERROR_INVALID_NAME:
652 case ERROR_DIR_NOT_EMPTY:
654 case ERROR_NOT_LOCKED:
656 case ERROR_ALREADY_EXISTS:
662 } else if (errcls == ERRSRV) {
664 /* N.B. This is wrong ... EIO ? */
674 * This is a fatal error, as it means the "tree ID"
675 * for this connection is no longer valid. We map
676 * to a special error code and get a new connection.
684 } else if (errcls == ERRHRD) {
707 } else if (errcls == ERRCMD) {
709 } else if (errcls == SUCCESS) {
710 return 0; /* This is the only valid 0 return */
714 printk(KERN_ERR "smb_errno: class %s, code %d from command 0x%x\n",
715 class, error, SMB_CMD(server->packet));
720 * smb_retry: This function should be called when smb_request_ok has
721 * indicated an error. If the error was indicated because the
722 * connection was killed, we try to reconnect. If smb_retry returns 0,
723 * the error was indicated for another reason, so a retry would not be
725 * N.B. The server must be locked for this call.
728 smb_retry(struct smb_sb_info *server)
730 pid_t pid = server->conn_pid;
731 int error, result = 0;
733 if (server->state == CONN_VALID || server->state == CONN_RETRYING)
736 smb_close_socket(server);
739 printk(KERN_ERR "smb_retry: no connection process\n");
740 server->state = CONN_RETRIED;
745 * Change state so that only one retry per server will be started.
747 server->state = CONN_RETRYING;
750 * Note: use the "priv" flag, as a user process may need to reconnect.
752 error = kill_proc(pid, SIGUSR1, 1);
754 /* FIXME: this is fatal */
755 printk(KERN_ERR "smb_retry: signal failed, error=%d\n", error);
758 VERBOSE("signalled pid %d, waiting for new connection\n", pid);
761 * Wait for the new connection.
763 smb_unlock_server(server);
764 interruptible_sleep_on_timeout(&server->wait, server->mnt->timeo*HZ);
765 smb_lock_server(server);
766 if (signal_pending(current))
767 printk(KERN_INFO "smb_retry: caught signal\n");
770 * Check for a valid connection.
772 if (server->state == CONN_VALID) {
773 /* This should be changed to VERBOSE, except many smbfs
774 problems is with the userspace daemon not reconnecting. */
775 PARANOIA("successful, new pid=%d, generation=%d\n",
776 server->conn_pid, server->generation);
778 } else if (server->state == CONN_RETRYING) {
779 /* allow further attempts later */
780 server->state = CONN_RETRIED;
787 /* smb_request_ok: We expect the server to be locked. Then we do the
788 request and check the answer completely. When smb_request_ok
789 returns 0, you can be quite sure that everything went well. When
790 the answer is <=0, the returned number is a valid unix errno. */
793 smb_request_ok(struct smb_sb_info *s, int command, int wct, int bcc)
800 /* Make sure we have a connection */
801 if (s->state != CONN_VALID) {
806 if (smb_request(s) < 0) {
807 DEBUG1("smb_request failed\n");
810 if (smb_valid_packet(s->packet) != 0) {
811 PARANOIA("invalid packet!\n");
816 * Check for server errors.
819 result = smb_errno(s);
821 printk(KERN_DEBUG "smb_request_ok: rcls=%d, err=%d mapped to 0\n",
824 * Exit now even if the error was squashed ...
825 * packet verify will fail anyway.
829 result = smb_verify(s->packet, command, wct, bcc);
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)
845 VERBOSE("fd=%d, pid=%d\n", opt->fd, current->pid);
847 smb_lock_server(server);
850 * Make sure we don't already have a valid connection ...
853 if (server->state == CONN_VALID)
857 if (current->uid != server->mnt->mounted_uid &&
858 !capable(CAP_SYS_ADMIN))
862 filp = fget(opt->fd);
865 if (!smb_valid_socket(filp->f_dentry->d_inode))
868 server->sock_file = filp;
869 server->conn_pid = current->pid;
870 smb_catch_keepalive(server);
872 server->generation += 1;
873 server->state = CONN_VALID;
876 /* check if we have an old smbmount that uses seconds for the
878 if (server->opt.serverzone > 12*60 || server->opt.serverzone < -12*60)
879 server->opt.serverzone /= 60;
881 /* now that we have an established connection we can detect the server
882 type and enable bug workarounds */
883 if (server->opt.protocol < SMB_PROTOCOL_LANMAN2)
884 install_ops(server->ops, &smb_ops_core);
885 else if (server->opt.protocol == SMB_PROTOCOL_LANMAN2)
886 install_ops(server->ops, &smb_ops_os2);
887 else if (server->opt.protocol == SMB_PROTOCOL_NT1 &&
888 (server->opt.max_xmit < 0x1000) &&
889 !(server->opt.capabilities & SMB_CAP_NT_SMBS)) {
890 /* FIXME: can we kill the WIN95 flag now? */
891 server->mnt->flags |= SMB_MOUNT_WIN95;
892 VERBOSE("detected WIN95 server\n");
893 install_ops(server->ops, &smb_ops_win95);
896 * Samba has max_xmit 65535
897 * NT4spX has max_xmit 4536 (or something like that)
900 VERBOSE("detected NT1 (Samba, NT4/5) server\n");
901 install_ops(server->ops, &smb_ops_winNT);
904 /* FIXME: the win9x code wants to modify these ... (seek/trunc bug) */
905 if (server->mnt->flags & SMB_MOUNT_OLDATTR) {
906 server->ops->getattr = smb_proc_getattr_core;
907 } else if (server->mnt->flags & SMB_MOUNT_DIRATTR) {
908 server->ops->getattr = smb_proc_getattr_ff;
911 /* Decode server capabilities */
912 if (server->opt.capabilities & SMB_CAP_LARGE_FILES) {
913 /* Should be ok to set this now, as no one can access the
914 mount until the connection has been established. */
915 SB_of(server)->s_maxbytes = ~0ULL >> 1;
916 VERBOSE("LFS enabled\n");
918 #ifndef CONFIG_SMB_UNIX
919 server->opt.capabilities &= ~SMB_CAP_UNIX;
921 if (server->opt.capabilities & SMB_CAP_UNIX) {
923 VERBOSE("Using UNIX CIFS extensions\n");
924 install_ops(server->ops, &smb_ops_unix);
925 inode = SB_of(server)->s_root->d_inode;
927 inode->i_op = &smb_dir_inode_operations_unix;
930 VERBOSE("protocol=%d, max_xmit=%d, pid=%d capabilities=0x%x\n",
931 server->opt.protocol, server->opt.max_xmit, server->conn_pid,
932 server->opt.capabilities);
934 /* Make sure we can fit a message of the negotiated size in our
936 if (server->opt.max_xmit > server->packet_size) {
937 int len = smb_round_length(server->opt.max_xmit);
938 char *buf = smb_vmalloc(len);
941 smb_vfree(server->packet);
942 server->packet = buf;
943 server->packet_size = len;
945 /* else continue with the too small buffer? */
946 PARANOIA("Failed to allocate new packet buffer: "
947 "max_xmit=%d, packet_size=%d\n",
948 server->opt.max_xmit, server->packet_size);
949 server->opt.max_xmit = server->packet_size;
953 if (server->opt.capabilities & SMB_CAP_UNIX)
954 smb_proc_query_cifsunix(server);
957 smb_unlock_server(server);
967 smb_wakeup(struct smb_sb_info *server)
969 wake_up_interruptible(&server->wait);
973 /* smb_setup_header: We completely set up the packet. You only have to
974 insert the command-specific fields */
977 smb_setup_header(struct smb_sb_info * server, __u8 command, __u16 wct, __u16 bcc)
979 __u32 xmit_len = SMB_HEADER_LEN + wct * sizeof(__u16) + bcc + 2;
980 __u8 *p = server->packet;
981 __u8 *buf = server->packet;
983 if (xmit_len > server->packet_size)
984 printk(KERN_DEBUG "smb_setup_header: "
985 "Aieee, xmit len > packet! len=%d, size=%d\n",
986 xmit_len, server->packet_size);
988 p = smb_encode_smb_length(p, xmit_len - 4);
1000 WSET(buf, smb_tid, server->opt.tid);
1001 WSET(buf, smb_pid, 1);
1002 WSET(buf, smb_uid, server->opt.server_uid);
1003 WSET(buf, smb_mid, 1);
1005 if (server->opt.protocol > SMB_PROTOCOL_CORE)
1007 *(buf+smb_flg) = 0x8;
1008 WSET(buf, smb_flg2, 0x3);
1010 *p++ = wct; /* wct */
1017 smb_setup_bcc(struct smb_sb_info *server, __u8 * p)
1019 __u8 *packet = server->packet;
1020 __u8 *pbcc = packet + SMB_HEADER_LEN + 2 * SMB_WCT(packet);
1021 __u16 bcc = p - (pbcc + 2);
1024 smb_encode_smb_length(packet,
1025 SMB_HEADER_LEN + 2 * SMB_WCT(packet) - 2 + bcc);
1029 * Called with the server locked
1032 smb_proc_seek(struct smb_sb_info *server, __u16 fileid,
1033 __u16 mode, off_t offset)
1037 smb_setup_header(server, SMBlseek, 4, 0);
1038 WSET(server->packet, smb_vwv0, fileid);
1039 WSET(server->packet, smb_vwv1, mode);
1040 DSET(server->packet, smb_vwv2, offset);
1042 result = smb_request_ok(server, SMBlseek, 2, 0);
1048 result = DVAL(server->packet, smb_vwv0);
1054 * We're called with the server locked, and we leave it that way.
1057 smb_proc_open(struct smb_sb_info *server, struct dentry *dentry, int wish)
1059 struct inode *ino = dentry->d_inode;
1060 int mode, read_write = 0x42, read_only = 0x40;
1065 * Attempt to open r/w, unless there are no write privileges.
1068 if (!(ino->i_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1071 /* FIXME: why is this code not in? below we fix it so that a caller
1072 wanting RO doesn't get RW. smb_revalidate_inode does some
1073 optimization based on access mode. tail -f needs it to be correct.
1075 We must open rw since we don't do the open if called a second time
1076 with different 'wish'. Is that not supported by smb servers? */
1077 if (!(wish & (O_WRONLY | O_RDWR)))
1082 p = smb_setup_header(server, SMBopen, 2, 0);
1083 WSET(server->packet, smb_vwv0, mode);
1084 WSET(server->packet, smb_vwv1, aSYSTEM | aHIDDEN | aDIR);
1085 res = smb_simple_encode_path(server, &p, dentry, NULL);
1088 smb_setup_bcc(server, p);
1090 res = smb_request_ok(server, SMBopen, 7, 0);
1092 if (smb_retry(server))
1095 if (mode == read_write &&
1096 (res == -EACCES || res == -ETXTBSY || res == -EROFS))
1098 VERBOSE("%s/%s R/W failed, error=%d, retrying R/O\n",
1099 DENTRY_PATH(dentry), res);
1105 /* We should now have data in vwv[0..6]. */
1107 ino->u.smbfs_i.fileid = WVAL(server->packet, smb_vwv0);
1108 ino->u.smbfs_i.attr = WVAL(server->packet, smb_vwv1);
1109 /* smb_vwv2 has mtime */
1110 /* smb_vwv4 has size */
1111 ino->u.smbfs_i.access = (WVAL(server->packet, smb_vwv6) & SMB_ACCMASK);
1112 ino->u.smbfs_i.open = server->generation;
1119 * Make sure the file is open, and check that the access
1120 * is compatible with the desired access.
1123 smb_open(struct dentry *dentry, int wish)
1125 struct inode *inode = dentry->d_inode;
1130 printk(KERN_ERR "smb_open: no inode for dentry %s/%s\n",
1131 DENTRY_PATH(dentry));
1135 if (!smb_is_open(inode)) {
1136 struct smb_sb_info *server = server_from_inode(inode);
1137 smb_lock_server(server);
1139 if (!smb_is_open(inode))
1140 result = smb_proc_open(server, dentry, wish);
1141 smb_unlock_server(server);
1143 PARANOIA("%s/%s open failed, result=%d\n",
1144 DENTRY_PATH(dentry), result);
1148 * A successful open means the path is still valid ...
1150 smb_renew_times(dentry);
1154 * Check whether the access is compatible with the desired mode.
1157 if (inode->u.smbfs_i.access != wish &&
1158 inode->u.smbfs_i.access != SMB_O_RDWR)
1160 PARANOIA("%s/%s access denied, access=%x, wish=%x\n",
1161 DENTRY_PATH(dentry), inode->u.smbfs_i.access, wish);
1168 /* We're called with the server locked */
1171 smb_proc_close(struct smb_sb_info *server, __u16 fileid, __u32 mtime)
1173 smb_setup_header(server, SMBclose, 3, 0);
1174 WSET(server->packet, smb_vwv0, fileid);
1175 DSET(server->packet, smb_vwv1, utc2local(server, mtime));
1176 return smb_request_ok(server, SMBclose, 0, 0);
1180 * Called with the server locked.
1182 * Win NT 4.0 has an apparent bug in that it fails to update the
1183 * modify time when writing to a file. As a workaround, we update
1184 * both modify and access time locally, and post the times to the
1185 * server when closing the file.
1188 smb_proc_close_inode(struct smb_sb_info *server, struct inode * ino)
1191 if (smb_is_open(ino))
1194 * We clear the open flag in advance, in case another
1195 * process observes the value while we block below.
1197 ino->u.smbfs_i.open = 0;
1200 * Kludge alert: SMB timestamps are accurate only to
1201 * two seconds ... round the times to avoid needless
1202 * cache invalidations!
1204 if (ino->i_mtime & 1)
1206 if (ino->i_atime & 1)
1209 * If the file is open with write permissions,
1210 * update the time stamps to sync mtime and atime.
1212 if ((server->opt.capabilities & SMB_CAP_UNIX) == 0 &&
1213 (server->opt.protocol >= SMB_PROTOCOL_LANMAN2) &&
1214 !(ino->u.smbfs_i.access == SMB_O_RDONLY))
1216 struct smb_fattr fattr;
1217 smb_get_inode_attr(ino, &fattr);
1218 smb_proc_setattr_ext(server, ino, &fattr);
1221 result = smb_proc_close(server, ino->u.smbfs_i.fileid,
1224 * Force a revalidation after closing ... some servers
1225 * don't post the size until the file has been closed.
1227 if (server->opt.protocol < SMB_PROTOCOL_NT1)
1228 ino->u.smbfs_i.oldmtime = 0;
1229 ino->u.smbfs_i.closed = jiffies;
1235 smb_close(struct inode *ino)
1239 if (smb_is_open(ino)) {
1240 struct smb_sb_info *server = server_from_inode(ino);
1241 smb_lock_server(server);
1242 result = smb_proc_close_inode(server, ino);
1243 smb_unlock_server(server);
1249 * This is used to close a file following a failed instantiate.
1250 * Since we don't have an inode, we can't use any of the above.
1253 smb_close_fileid(struct dentry *dentry, __u16 fileid)
1255 struct smb_sb_info *server = server_from_dentry(dentry);
1258 smb_lock_server(server);
1259 result = smb_proc_close(server, fileid, CURRENT_TIME);
1260 smb_unlock_server(server);
1264 /* In smb_proc_read and smb_proc_write we do not retry, because the
1265 file-id would not be valid after a reconnection. */
1268 smb_proc_read(struct inode *inode, loff_t offset, int count, char *data)
1270 struct smb_sb_info *server = server_from_inode(inode);
1271 __u16 returned_count, data_len;
1275 smb_lock_server(server);
1276 smb_setup_header(server, SMBread, 5, 0);
1277 buf = server->packet;
1278 WSET(buf, smb_vwv0, inode->u.smbfs_i.fileid);
1279 WSET(buf, smb_vwv1, count);
1280 DSET(buf, smb_vwv2, offset);
1281 WSET(buf, smb_vwv4, 0);
1283 result = smb_request_ok(server, SMBread, 5, -1);
1286 returned_count = WVAL(server->packet, smb_vwv0);
1288 buf = SMB_BUF(server->packet);
1289 data_len = WVAL(buf, 1);
1291 /* we can NOT simply trust the data_len given by the server ... */
1292 if (data_len > count ||
1293 (buf+3 - server->packet) + data_len > server->packet_size) {
1294 printk(KERN_ERR "smb_proc_read: invalid data length/offset!! "
1295 "%d > %d || (%p - %p) + %d > %d\n",
1297 buf+3, server->packet, data_len, server->packet_size);
1302 memcpy(data, buf+3, data_len);
1304 if (returned_count != data_len) {
1305 printk(KERN_NOTICE "smb_proc_read: returned != data_len\n");
1306 printk(KERN_NOTICE "smb_proc_read: ret_c=%d, data_len=%d\n",
1307 returned_count, data_len);
1312 VERBOSE("ino=%ld, fileid=%d, count=%d, result=%d\n",
1313 inode->i_ino, inode->u.smbfs_i.fileid, count, result);
1314 smb_unlock_server(server);
1319 smb_proc_write(struct inode *inode, loff_t offset, int count, const char *data)
1321 struct smb_sb_info *server = server_from_inode(inode);
1324 __u16 fileid = inode->u.smbfs_i.fileid;
1326 VERBOSE("ino=%ld, fileid=%d, count=%d@%Ld, packet_size=%d\n",
1327 inode->i_ino, inode->u.smbfs_i.fileid, count, offset,
1328 server->packet_size);
1330 smb_lock_server(server);
1331 p = smb_setup_header(server, SMBwrite, 5, count + 3);
1332 WSET(server->packet, smb_vwv0, fileid);
1333 WSET(server->packet, smb_vwv1, count);
1334 DSET(server->packet, smb_vwv2, offset);
1335 WSET(server->packet, smb_vwv4, 0);
1339 memcpy(p+2, data, count);
1341 result = smb_request_ok(server, SMBwrite, 1, 0);
1343 result = WVAL(server->packet, smb_vwv0);
1345 smb_unlock_server(server);
1350 * In smb_proc_readX and smb_proc_writeX we do not retry, because the
1351 * file-id would not be valid after a reconnection.
1354 smb_proc_readX(struct inode *inode, loff_t offset, int count, char *data)
1356 struct smb_sb_info *server = server_from_inode(inode);
1357 u16 data_len, data_off;
1361 smb_lock_server(server);
1362 smb_setup_header(server, SMBreadX, 12, 0);
1363 buf = server->packet;
1364 WSET(buf, smb_vwv0, 0x00ff);
1365 WSET(buf, smb_vwv1, 0);
1366 WSET(buf, smb_vwv2, inode->u.smbfs_i.fileid);
1367 DSET(buf, smb_vwv3, (u32)offset); /* low 32 bits */
1368 WSET(buf, smb_vwv5, count);
1369 WSET(buf, smb_vwv6, 0);
1370 DSET(buf, smb_vwv7, 0);
1371 WSET(buf, smb_vwv9, 0);
1372 DSET(buf, smb_vwv10, (u32)(offset >> 32)); /* high 32 bits */
1373 WSET(buf, smb_vwv11, 0);
1375 result = smb_request_ok(server, SMBreadX, 12, -1);
1378 data_len = WVAL(server->packet, smb_vwv5);
1379 data_off = WVAL(server->packet, smb_vwv6);
1380 buf = smb_base(server->packet) + data_off;
1382 /* we can NOT simply trust the info given by the server ... */
1383 if (data_len > count ||
1384 (buf - server->packet) + data_len > server->packet_size) {
1385 printk(KERN_ERR "smb_proc_readX: invalid data length/offset!! "
1386 "%d > %d || (%p - %p) + %d > %d\n",
1388 buf, server->packet, data_len, server->packet_size);
1393 memcpy(data, buf, data_len);
1397 smb_unlock_server(server);
1398 VERBOSE("ino=%ld, fileid=%d, count=%d, result=%d\n",
1399 inode->i_ino, inode->u.smbfs_i.fileid, count, result);
1404 smb_proc_writeX(struct inode *inode, loff_t offset, int count, const char *data)
1406 struct smb_sb_info *server = server_from_inode(inode);
1410 VERBOSE("ino=%ld, fileid=%d, count=%d@%Ld, packet_size=%d\n",
1411 inode->i_ino, inode->u.smbfs_i.fileid, count, offset,
1412 server->packet_size);
1414 smb_lock_server(server);
1415 p = smb_setup_header(server, SMBwriteX, 14, count + 2);
1416 WSET(server->packet, smb_vwv0, 0x00ff);
1417 WSET(server->packet, smb_vwv1, 0);
1418 WSET(server->packet, smb_vwv2, inode->u.smbfs_i.fileid);
1419 DSET(server->packet, smb_vwv3, (u32)offset); /* low 32 bits */
1420 DSET(server->packet, smb_vwv5, 0);
1421 WSET(server->packet, smb_vwv7, 0); /* write mode */
1422 WSET(server->packet, smb_vwv8, 0);
1423 WSET(server->packet, smb_vwv9, 0);
1424 WSET(server->packet, smb_vwv10, count); /* data length */
1425 WSET(server->packet, smb_vwv11, p + 2 - smb_base(server->packet));
1426 DSET(server->packet, smb_vwv12, (u32)(offset >> 32));
1427 *p++ = 0; /* FIXME: pad to short or long ... change +2 above also */
1429 memcpy(p, data, count);
1431 result = smb_request_ok(server, SMBwriteX, 6, 0);
1433 result = WVAL(server->packet, smb_vwv2);
1435 smb_unlock_server(server);
1440 smb_proc_create(struct dentry *dentry, __u16 attr, time_t ctime, __u16 *fileid)
1442 struct smb_sb_info *server = server_from_dentry(dentry);
1446 smb_lock_server(server);
1449 p = smb_setup_header(server, SMBcreate, 3, 0);
1450 WSET(server->packet, smb_vwv0, attr);
1451 DSET(server->packet, smb_vwv1, utc2local(server, ctime));
1452 result = smb_simple_encode_path(server, &p, dentry, NULL);
1455 smb_setup_bcc(server, p);
1457 result = smb_request_ok(server, SMBcreate, 1, 0);
1459 if (smb_retry(server))
1463 *fileid = WVAL(server->packet, smb_vwv0);
1467 smb_unlock_server(server);
1472 smb_proc_mv(struct dentry *old_dentry, struct dentry *new_dentry)
1474 struct smb_sb_info *server = server_from_dentry(old_dentry);
1478 smb_lock_server(server);
1481 p = smb_setup_header(server, SMBmv, 1, 0);
1482 WSET(server->packet, smb_vwv0, aSYSTEM | aHIDDEN | aDIR);
1483 result = smb_simple_encode_path(server, &p, old_dentry, NULL);
1486 result = smb_simple_encode_path(server, &p, new_dentry, NULL);
1489 smb_setup_bcc(server, p);
1491 if ((result = smb_request_ok(server, SMBmv, 0, 0)) < 0) {
1492 if (smb_retry(server))
1498 smb_unlock_server(server);
1503 * Code common to mkdir and rmdir.
1506 smb_proc_generic_command(struct dentry *dentry, __u8 command)
1508 struct smb_sb_info *server = server_from_dentry(dentry);
1512 smb_lock_server(server);
1515 p = smb_setup_header(server, command, 0, 0);
1516 result = smb_simple_encode_path(server, &p, dentry, NULL);
1519 smb_setup_bcc(server, p);
1521 result = smb_request_ok(server, command, 0, 0);
1523 if (smb_retry(server))
1529 smb_unlock_server(server);
1534 smb_proc_mkdir(struct dentry *dentry)
1536 return smb_proc_generic_command(dentry, SMBmkdir);
1540 smb_proc_rmdir(struct dentry *dentry)
1542 return smb_proc_generic_command(dentry, SMBrmdir);
1545 #if SMBFS_POSIX_UNLINK
1547 * Removes readonly attribute from a file. Used by unlink to give posix
1549 * Note: called with the server locked.
1552 smb_set_rw(struct dentry *dentry,struct smb_sb_info *server)
1555 struct smb_fattr fattr;
1557 /* first get current attribute */
1558 smb_init_dirent(server, &fattr);
1559 result = server->ops->getattr(server, dentry, &fattr);
1560 smb_finish_dirent(server, &fattr);
1564 /* if RONLY attribute is set, remove it */
1565 if (fattr.attr & aRONLY) { /* read only attribute is set */
1566 fattr.attr &= ~aRONLY;
1567 result = smb_proc_setattr_core(server, dentry, fattr.attr);
1574 smb_proc_unlink(struct dentry *dentry)
1576 struct smb_sb_info *server = server_from_dentry(dentry);
1581 smb_lock_server(server);
1584 p = smb_setup_header(server, SMBunlink, 1, 0);
1585 WSET(server->packet, smb_vwv0, aSYSTEM | aHIDDEN);
1586 result = smb_simple_encode_path(server, &p, dentry, NULL);
1589 smb_setup_bcc(server, p);
1591 if ((result = smb_request_ok(server, SMBunlink, 0, 0)) < 0) {
1592 #if SMBFS_POSIX_UNLINK
1593 if (result == -EACCES && !flag) {
1594 /* Posix semantics is for the read-only state
1595 of a file to be ignored in unlink(). In the
1596 SMB world a unlink() is refused on a
1597 read-only file. To make things easier for
1598 unix users we try to override the files
1599 permission if the unlink fails with the
1601 This introduces a race condition that could
1602 lead to a file being written by someone who
1603 shouldn't have access, but as far as I can
1604 tell that is unavoidable */
1606 /* remove RONLY attribute and try again */
1607 result = smb_set_rw(dentry,server);
1614 if (smb_retry(server))
1620 smb_unlock_server(server);
1625 * Called with the server locked
1628 smb_proc_flush(struct smb_sb_info *server, __u16 fileid)
1630 smb_setup_header(server, SMBflush, 1, 0);
1631 WSET(server->packet, smb_vwv0, fileid);
1632 return smb_request_ok(server, SMBflush, 0, 0);
1636 smb_proc_trunc32(struct inode *inode, loff_t length)
1639 * Writing 0bytes is old-SMB magic for truncating files.
1640 * MAX_NON_LFS should prevent this from being called with a too
1643 return smb_proc_write(inode, length, 0, NULL);
1647 smb_proc_trunc64(struct inode *inode, loff_t length)
1649 struct smb_sb_info *server = server_from_inode(inode);
1652 char *param = server->temp_buf;
1654 unsigned char *resp_data = NULL;
1655 unsigned char *resp_param = NULL;
1656 int resp_data_len = 0;
1657 int resp_param_len = 0;
1659 smb_lock_server(server);
1660 command = TRANSACT2_SETFILEINFO;
1662 /* FIXME: must we also set allocation size? winNT seems to do that */
1664 WSET(param, 0, inode->u.smbfs_i.fileid);
1665 WSET(param, 2, SMB_SET_FILE_END_OF_FILE_INFO);
1667 LSET(data, 0, length);
1668 result = smb_trans2_request(server, command,
1670 &resp_data_len, &resp_data,
1671 &resp_param_len, &resp_param);
1673 if (smb_retry(server))
1678 if (server->rcls != 0)
1679 result = smb_errno(server);
1682 smb_unlock_server(server);
1687 smb_proc_trunc95(struct inode *inode, loff_t length)
1689 struct smb_sb_info *server = server_from_inode(inode);
1690 int result = smb_proc_trunc32(inode, length);
1693 * win9x doesn't appear to update the size immediately.
1694 * It will return the old file size after the truncate,
1695 * confusing smbfs. So we force an update.
1697 * FIXME: is this still necessary?
1699 smb_lock_server(server);
1700 smb_proc_flush(server, inode->u.smbfs_i.fileid);
1701 smb_unlock_server(server);
1706 smb_init_dirent(struct smb_sb_info *server, struct smb_fattr *fattr)
1708 memset(fattr, 0, sizeof(*fattr));
1711 fattr->f_uid = server->mnt->uid;
1712 fattr->f_gid = server->mnt->gid;
1713 fattr->f_blksize = SMB_ST_BLKSIZE;
1718 smb_finish_dirent(struct smb_sb_info *server, struct smb_fattr *fattr)
1723 fattr->f_mode = server->mnt->file_mode;
1724 if (fattr->attr & aDIR) {
1725 fattr->f_mode = server->mnt->dir_mode;
1726 fattr->f_size = SMB_ST_BLKSIZE;
1728 /* Check the read-only flag */
1729 if (fattr->attr & aRONLY)
1730 fattr->f_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
1732 /* How many 512 byte blocks do we need for this file? */
1733 fattr->f_blocks = 0;
1734 if (fattr->f_size != 0)
1735 fattr->f_blocks = 1 + ((fattr->f_size-1) >> 9);
1740 smb_init_root_dirent(struct smb_sb_info *server, struct smb_fattr *fattr)
1742 smb_init_dirent(server, fattr);
1744 fattr->f_ino = 2; /* traditional root inode number */
1745 fattr->f_mtime = CURRENT_TIME;
1746 smb_finish_dirent(server, fattr);
1750 * Decode a dirent for old protocols
1752 * qname is filled with the decoded, and possibly translated, name.
1753 * fattr receives decoded attributes
1756 * (1) Pathworks servers may pad the name with extra spaces.
1759 smb_decode_short_dirent(struct smb_sb_info *server, char *p,
1760 struct qstr *qname, struct smb_fattr *fattr)
1765 * SMB doesn't have a concept of inode numbers ...
1767 smb_init_dirent(server, fattr);
1768 fattr->f_ino = 0; /* FIXME: do we need this? */
1770 p += SMB_STATUS_SIZE; /* reserved (search_status) */
1772 fattr->f_mtime = date_dos2unix(server, WVAL(p, 3), WVAL(p, 1));
1773 fattr->f_size = DVAL(p, 5);
1774 fattr->f_ctime = fattr->f_mtime;
1775 fattr->f_atime = fattr->f_mtime;
1776 qname->name = p + 9;
1777 len = strnlen(qname->name, 12);
1780 * Trim trailing blanks for Pathworks servers
1782 while (len > 2 && qname->name[len-1] == ' ')
1786 smb_finish_dirent(server, fattr);
1789 /* FIXME: These only work for ascii chars, and recent smbmount doesn't
1790 allow the flag to be set anyway. It kills const. Remove? */
1791 switch (server->opt.case_handling) {
1792 case SMB_CASE_UPPER:
1793 str_upper(entry->name, len);
1795 case SMB_CASE_LOWER:
1796 str_lower(entry->name, len);
1804 len = server->ops->convert(server->name_buf, SMB_MAXNAMELEN,
1806 server->remote_nls, server->local_nls);
1809 qname->name = server->name_buf;
1810 DEBUG1("len=%d, name=%.*s\n",qname->len,qname->len,qname->name);
1817 * This routine is used to read in directory entries from the network.
1818 * Note that it is for short directory name seeks, i.e.: protocol <
1819 * SMB_PROTOCOL_LANMAN2
1822 smb_proc_readdir_short(struct file *filp, void *dirent, filldir_t filldir,
1823 struct smb_cache_control *ctl)
1825 struct dentry *dir = filp->f_dentry;
1826 struct smb_sb_info *server = server_from_dentry(dir);
1828 struct smb_fattr fattr;
1831 int i, first, entries_seen, entries;
1832 int entries_asked = (server->opt.max_xmit - 100) / SMB_DIRINFO_SIZE;
1835 char status[SMB_STATUS_SIZE];
1836 static struct qstr mask = { "*.*", 3, 0 };
1837 unsigned char *last_status;
1839 VERBOSE("%s/%s\n", DENTRY_PATH(dir));
1841 smb_lock_server(server);
1845 entries_seen = 2; /* implicit . and .. */
1848 p = smb_setup_header(server, SMBsearch, 2, 0);
1849 WSET(server->packet, smb_vwv0, entries_asked);
1850 WSET(server->packet, smb_vwv1, aDIR);
1852 result = smb_simple_encode_path(server, &p, dir, &mask);
1855 if (p + 3 > (char*)server->packet+server->packet_size) {
1856 result = -ENAMETOOLONG;
1864 if (p + 5 + SMB_STATUS_SIZE >
1865 (char*)server->packet + server->packet_size) {
1866 result = -ENAMETOOLONG;
1873 WSET(p, 0, SMB_STATUS_SIZE);
1875 memcpy(p, status, SMB_STATUS_SIZE);
1876 p += SMB_STATUS_SIZE;
1879 smb_setup_bcc(server, p);
1881 result = smb_request_ok(server, SMBsearch, 1, -1);
1883 if ((server->rcls == ERRDOS) &&
1884 (server->err == ERRnofiles))
1886 if (smb_retry(server)) {
1887 ctl->idx = -1; /* retry */
1892 p = SMB_VWV(server->packet);
1899 if (bcc != count * SMB_DIRINFO_SIZE + 3)
1904 /* Make sure the response fits in the buffer. Fixed sized
1905 entries means we don't have to check in the decode loop. */
1907 last_status = SMB_BUF(server->packet) + 3 + (count - 1) *
1910 if (last_status + SMB_DIRINFO_SIZE >=
1911 server->packet + server->packet_size) {
1912 printk(KERN_ERR "smb_proc_readdir_short: "
1913 "last dir entry outside buffer! "
1914 "%d@%p %d@%p\n", SMB_DIRINFO_SIZE, last_status,
1915 server->packet_size, server->packet);
1919 /* Read the last entry into the status field. */
1920 memcpy(status, last_status, SMB_STATUS_SIZE);
1923 /* Now we are ready to parse smb directory entries. */
1925 for (i = 0; i < count; i++) {
1926 p = smb_decode_short_dirent(server, p,
1930 if (entries_seen == 2 && qname.name[0] == '.') {
1933 if (qname.name[1] == '.' && qname.len == 2)
1936 if (!smb_fill_cache(filp, dirent, filldir, ctl,
1938 ; /* stop reading? */
1945 smb_unlock_server(server);
1949 void smb_decode_unix_basic(struct smb_fattr *fattr, struct smb_sb_info *server, char *p)
1951 /* FIXME: verify nls support. all is sent as utf8? */
1956 /* FIXME: use the uniqueID from the remote instead? */
1957 /* 0 L file size in bytes */
1958 /* 8 L file size on disk in bytes (block count) */
1961 /* 56 W file type */
1964 /* 76 L unique ID (inode) */
1965 /* 84 L permissions */
1966 /* 92 L link count */
1968 fattr->f_size = LVAL(p, 0);
1969 fattr->f_blocks = LVAL(p, 8);
1970 fattr->f_ctime = smb_ntutc2unixutc(LVAL(p, 16));
1971 fattr->f_atime = smb_ntutc2unixutc(LVAL(p, 24));
1972 fattr->f_mtime = smb_ntutc2unixutc(LVAL(p, 32));
1974 if (server->mnt->flags & SMB_MOUNT_UID)
1975 fattr->f_uid = server->mnt->uid;
1977 fattr->f_uid = LVAL(p, 40);
1979 if (server->mnt->flags & SMB_MOUNT_GID)
1980 fattr->f_gid = server->mnt->gid;
1982 fattr->f_gid = LVAL(p, 48);
1984 fattr->f_mode |= smb_filetype_to_mode(WVAL(p, 56));
1986 if (S_ISBLK(fattr->f_mode) || S_ISCHR(fattr->f_mode)) {
1987 __u64 major = LVAL(p, 60);
1988 __u64 minor = LVAL(p, 68);
1990 fattr->f_rdev = MKDEV(major & 0xffffffff, minor & 0xffffffff);
1993 fattr->f_mode |= LVAL(p, 84);
1995 if ( (server->mnt->flags & SMB_MOUNT_DMODE) &&
1996 (S_ISDIR(fattr->f_mode)) )
1997 fattr->f_mode = (server->mnt->dir_mode & S_IRWXUGO) | S_IFDIR;
1998 else if ( (server->mnt->flags & SMB_MOUNT_FMODE) &&
1999 !(S_ISDIR(fattr->f_mode)) )
2000 fattr->f_mode = (server->mnt->file_mode & S_IRWXUGO) |
2001 (fattr->f_mode & S_IFMT);
2006 * Interpret a long filename structure using the specified info level:
2007 * level 1 for anything below NT1 protocol
2008 * level 260 for NT1 protocol
2010 * qname is filled with the decoded, and possibly translated, name
2011 * fattr receives decoded attributes.
2014 * (1) Win NT 4.0 appends a null byte to names and counts it in the length!
2017 smb_decode_long_dirent(struct smb_sb_info *server, char *p, int level,
2018 struct qstr *qname, struct smb_fattr *fattr)
2021 unsigned int len = 0;
2026 * SMB doesn't have a concept of inode numbers ...
2028 smb_init_dirent(server, fattr);
2029 fattr->f_ino = 0; /* FIXME: do we need this? */
2033 len = *((unsigned char *) p + 22);
2034 qname->name = p + 23;
2035 result = p + 24 + len;
2039 fattr->f_ctime = date_dos2unix(server, date, time);
2043 fattr->f_atime = date_dos2unix(server, date, time);
2047 fattr->f_mtime = date_dos2unix(server, date, time);
2048 fattr->f_size = DVAL(p, 12);
2049 /* ULONG allocation size */
2050 fattr->attr = WVAL(p, 20);
2052 VERBOSE("info 1 at %p, len=%d, name=%.*s\n",
2053 p, len, len, qname->name);
2056 result = p + WVAL(p, 0);
2058 if (len > 255) len = 255;
2059 /* NT4 null terminates */
2060 qname->name = p + 94;
2061 if (len && qname->name[len-1] == '\0')
2064 fattr->f_ctime = smb_ntutc2unixutc(LVAL(p, 8));
2065 fattr->f_atime = smb_ntutc2unixutc(LVAL(p, 16));
2066 fattr->f_mtime = smb_ntutc2unixutc(LVAL(p, 24));
2067 /* change time (32) */
2068 fattr->f_size = LVAL(p, 40);
2069 /* alloc size (48) */
2070 fattr->attr = DVAL(p, 56);
2072 VERBOSE("info 260 at %p, len=%d, name=%.*s\n",
2073 p, len, len, qname->name);
2075 case SMB_FIND_FILE_UNIX:
2076 result = p + WVAL(p, 0);
2077 qname->name = p + 108;
2079 len = strlen(qname->name);
2080 /* FIXME: should we check the length?? */
2083 smb_decode_unix_basic(fattr, server, p);
2084 VERBOSE("info SMB_FIND_FILE_UNIX at %p, len=%d, name=%.*s\n",
2085 p, len, len, qname->name);
2088 PARANOIA("Unknown info level %d\n", level);
2089 result = p + WVAL(p, 0);
2093 smb_finish_dirent(server, fattr);
2096 /* FIXME: These only work for ascii chars, and recent smbmount doesn't
2097 allow the flag to be set anyway. Remove? */
2098 switch (server->opt.case_handling) {
2099 case SMB_CASE_UPPER:
2100 str_upper(qname->name, len);
2102 case SMB_CASE_LOWER:
2103 str_lower(qname->name, len);
2111 n = server->ops->convert(server->name_buf, SMB_MAXNAMELEN,
2113 server->remote_nls, server->local_nls);
2116 qname->name = server->name_buf;
2123 /* findfirst/findnext flags */
2124 #define SMB_CLOSE_AFTER_FIRST (1<<0)
2125 #define SMB_CLOSE_IF_END (1<<1)
2126 #define SMB_REQUIRE_RESUME_KEY (1<<2)
2127 #define SMB_CONTINUE_BIT (1<<3)
2130 * Note: samba-2.0.7 (at least) has a very similar routine, cli_list, in
2131 * source/libsmb/clilist.c. When looking for smb bugs in the readdir code,
2132 * go there for advise.
2135 * (1) When using Info Level 1 Win NT 4.0 truncates directory listings
2136 * for certain patterns of names and/or lengths. The breakage pattern
2137 * is completely reproducible and can be toggled by the creation of a
2138 * single file. (E.g. echo hi >foo breaks, rm -f foo works.)
2141 smb_proc_readdir_long(struct file *filp, void *dirent, filldir_t filldir,
2142 struct smb_cache_control *ctl)
2144 struct dentry *dir = filp->f_dentry;
2145 struct smb_sb_info *server = server_from_dentry(dir);
2147 struct smb_fattr fattr;
2149 unsigned char *p, *lastname;
2150 char *mask, *param = server->temp_buf;
2152 int first, entries_seen;
2154 /* Both NT and OS/2 accept info level 1 (but see note below). */
2155 int info_level = 260;
2156 const int max_matches = 512;
2158 unsigned char *resp_data = NULL;
2159 unsigned char *resp_param = NULL;
2160 int resp_data_len = 0;
2161 int resp_param_len = 0;
2162 int ff_searchcount = 0;
2164 int ff_lastname = 0;
2165 int ff_dir_handle = 0;
2167 int mask_len, i, result;
2168 static struct qstr star = { "*", 1, 0 };
2171 * use info level 1 for older servers that don't do 260
2173 if (server->opt.capabilities & SMB_CAP_UNIX)
2174 info_level = SMB_FIND_FILE_UNIX;
2175 else if (server->opt.protocol < SMB_PROTOCOL_NT1)
2178 smb_lock_server(server);
2181 * Encode the initial path
2185 mask_len = smb_encode_path(server, mask, SMB_MAXPATHLEN+1, dir, &star);
2190 mask_len--; /* mask_len is strlen, not #bytes */
2192 VERBOSE("starting mask_len=%d, mask=%s\n", mask_len, mask);
2198 while (ff_eos == 0) {
2200 if (loop_count > 10) {
2201 printk(KERN_WARNING "smb_proc_readdir_long: "
2202 "Looping in FIND_NEXT??\n");
2208 command = TRANSACT2_FINDFIRST;
2209 WSET(param, 0, aSYSTEM | aHIDDEN | aDIR);
2210 WSET(param, 2, max_matches); /* max count */
2211 WSET(param, 4, SMB_CLOSE_IF_END);
2212 WSET(param, 6, info_level);
2215 command = TRANSACT2_FINDNEXT;
2217 VERBOSE("handle=0x%X, lastname=%d, mask=%s\n",
2218 ff_dir_handle, ff_lastname, mask);
2220 WSET(param, 0, ff_dir_handle); /* search handle */
2221 WSET(param, 2, max_matches); /* max count */
2222 WSET(param, 4, info_level);
2224 WSET(param, 10, SMB_CONTINUE_BIT|SMB_CLOSE_IF_END);
2227 result = smb_trans2_request(server, command,
2228 0, NULL, 12 + mask_len + 1, param,
2229 &resp_data_len, &resp_data,
2230 &resp_param_len, &resp_param);
2233 if (smb_retry(server)) {
2234 PARANOIA("error=%d, retrying\n", result);
2235 ctl->idx = -1; /* retry */
2239 PARANOIA("error=%d, breaking\n", result);
2243 if (server->rcls == ERRSRV && server->err == ERRerror) {
2244 /* a damn Win95 bug - sometimes it clags if you
2246 current->state = TASK_INTERRUPTIBLE;
2247 schedule_timeout(HZ/5);
2251 if (server->rcls != 0) {
2252 result = smb_errno(server);
2253 PARANOIA("name=%s, result=%d, rcls=%d, err=%d\n",
2254 mask, result, server->rcls, server->err);
2258 /* parse out some important return info */
2260 ff_dir_handle = WVAL(resp_param, 0);
2261 ff_searchcount = WVAL(resp_param, 2);
2262 ff_eos = WVAL(resp_param, 4);
2263 ff_lastname = WVAL(resp_param, 8);
2265 ff_searchcount = WVAL(resp_param, 0);
2266 ff_eos = WVAL(resp_param, 2);
2267 ff_lastname = WVAL(resp_param, 6);
2270 if (ff_searchcount == 0)
2274 * We might need the lastname for continuations.
2276 * Note that some servers (win95?) point to the filename and
2277 * others (NT4, Samba using NT1) to the dir entry. We assume
2278 * here that those who do not point to a filename do not need
2279 * this info to continue the listing.
2281 * OS/2 needs this and talks infolevel 1
2282 * NetApps want lastname with infolevel 260
2283 * win2k want lastname with infolevel 260, and points to
2284 * the record not to the name.
2285 * Samba+CifsUnixExt doesn't need lastname.
2287 * All are happy if we return the data they point to. So we do.
2290 if (info_level != SMB_FIND_FILE_UNIX &&
2291 ff_lastname > 0 && ff_lastname < resp_data_len) {
2292 lastname = resp_data + ff_lastname;
2294 switch (info_level) {
2296 mask_len = resp_data_len - ff_lastname;
2299 /* lastname points to a length byte */
2300 mask_len = *lastname++;
2301 if (ff_lastname + 1 + mask_len > resp_data_len)
2302 mask_len = resp_data_len - ff_lastname - 1;
2307 * Update the mask string for the next message.
2314 strncpy(mask, lastname, mask_len);
2316 mask_len = strnlen(mask, mask_len);
2317 VERBOSE("new mask, len=%d@%d of %d, mask=%.*s\n",
2318 mask_len, ff_lastname, resp_data_len, mask_len, mask);
2320 /* Now we are ready to parse smb directory entries. */
2322 /* point to the data bytes */
2324 for (i = 0; i < ff_searchcount; i++) {
2325 /* make sure we stay within the buffer */
2326 if (p >= resp_data + resp_data_len) {
2327 printk(KERN_ERR "smb_proc_readdir_long: "
2328 "dirent pointer outside buffer! "
2330 p, resp_data_len, resp_data,
2331 server->packet_size, server->packet);
2332 result = -EIO; /* always a comm. error? */
2336 p = smb_decode_long_dirent(server, p, info_level,
2341 /* ignore . and .. from the server */
2342 if (entries_seen == 2 && qname.name[0] == '.') {
2345 if (qname.name[1] == '.' && qname.len == 2)
2349 if (!smb_fill_cache(filp, dirent, filldir, ctl,
2351 ; /* stop reading? */
2355 VERBOSE("received %d entries, eos=%d\n", ff_searchcount,ff_eos);
2362 smb_unlock_server(server);
2367 * This version uses the trans2 TRANSACT2_FINDFIRST message
2368 * to get the attribute data.
2369 * Note: called with the server locked.
2374 smb_proc_getattr_ff(struct smb_sb_info *server, struct dentry *dentry,
2375 struct smb_fattr *fattr)
2377 char *param = server->temp_buf, *mask = param + 12;
2379 unsigned char *resp_data = NULL;
2380 unsigned char *resp_param = NULL;
2381 int resp_data_len = 0;
2382 int resp_param_len = 0;
2383 int mask_len, result;
2386 mask_len = smb_encode_path(server, mask, SMB_MAXPATHLEN+1, dentry, NULL);
2391 VERBOSE("name=%s, len=%d\n", mask, mask_len);
2392 WSET(param, 0, aSYSTEM | aHIDDEN | aDIR);
2393 WSET(param, 2, 1); /* max count */
2394 WSET(param, 4, 1); /* close after this call */
2395 WSET(param, 6, 1); /* info_level */
2398 result = smb_trans2_request(server, TRANSACT2_FINDFIRST,
2399 0, NULL, 12 + mask_len, param,
2400 &resp_data_len, &resp_data,
2401 &resp_param_len, &resp_param);
2404 if (smb_retry(server))
2408 if (server->rcls != 0)
2410 result = smb_errno(server);
2411 #ifdef SMBFS_PARANOIA
2412 if (result != -ENOENT)
2413 PARANOIA("error for %s, rcls=%d, err=%d\n",
2414 mask, server->rcls, server->err);
2418 /* Make sure we got enough data ... */
2420 if (resp_data_len < 22 || WVAL(resp_param, 2) != 1)
2422 PARANOIA("bad result for %s, len=%d, count=%d\n",
2423 mask, resp_data_len, WVAL(resp_param, 2));
2428 * Decode the response into the fattr ...
2430 date = WVAL(resp_data, 0);
2431 time = WVAL(resp_data, 2);
2432 fattr->f_ctime = date_dos2unix(server, date, time);
2434 date = WVAL(resp_data, 4);
2435 time = WVAL(resp_data, 6);
2436 fattr->f_atime = date_dos2unix(server, date, time);
2438 date = WVAL(resp_data, 8);
2439 time = WVAL(resp_data, 10);
2440 fattr->f_mtime = date_dos2unix(server, date, time);
2441 VERBOSE("name=%s, date=%x, time=%x, mtime=%ld\n",
2442 mask, date, time, fattr->f_mtime);
2443 fattr->f_size = DVAL(resp_data, 12);
2444 /* ULONG allocation size */
2445 fattr->attr = WVAL(resp_data, 20);
2453 * Note: called with the server locked.
2456 smb_proc_getattr_core(struct smb_sb_info *server, struct dentry *dir,
2457 struct smb_fattr *fattr)
2463 p = smb_setup_header(server, SMBgetatr, 0, 0);
2464 result = smb_simple_encode_path(server, &p, dir, NULL);
2467 smb_setup_bcc(server, p);
2469 if ((result = smb_request_ok(server, SMBgetatr, 10, 0)) < 0)
2471 if (smb_retry(server))
2475 fattr->attr = WVAL(server->packet, smb_vwv0);
2476 fattr->f_mtime = local2utc(server, DVAL(server->packet, smb_vwv1));
2477 fattr->f_size = DVAL(server->packet, smb_vwv3);
2478 fattr->f_ctime = fattr->f_mtime;
2479 fattr->f_atime = fattr->f_mtime;
2480 #ifdef SMBFS_DEBUG_TIMESTAMP
2481 printk("getattr_core: %s/%s, mtime=%ld\n",
2482 DENTRY_PATH(dir), fattr->f_mtime);
2491 * Note: called with the server locked.
2494 smb_proc_getattr_trans2_all(struct smb_sb_info *server, struct dentry *dir,
2495 struct smb_fattr *attr)
2497 char *p, *param = server->temp_buf;
2498 unsigned char *resp_data = NULL;
2499 unsigned char *resp_param = NULL;
2500 int resp_data_len = 0;
2501 int resp_param_len = 0;
2505 WSET(param, 0, SMB_QUERY_FILE_ALL_INFO);
2507 result = smb_encode_path(server, param+6, SMB_MAXPATHLEN+1, dir, NULL);
2510 p = param + 6 + result;
2512 result = smb_trans2_request(server, TRANSACT2_QPATHINFO,
2513 0, NULL, p - param, param,
2514 &resp_data_len, &resp_data,
2515 &resp_param_len, &resp_param);
2517 if (smb_retry(server))
2521 if (server->rcls != 0) {
2522 VERBOSE("for %s: result=%d, rcls=%d, err=%d\n",
2523 ¶m[6], result, server->rcls, server->err);
2524 result = smb_errno(server);
2529 if (resp_data_len < 56) {
2530 PARANOIA("not enough data for %s, len=%d\n",
2531 ¶m[6], resp_data_len);
2535 attr->f_ctime = smb_ntutc2unixutc(LVAL(resp_data, 0));
2536 attr->f_atime = smb_ntutc2unixutc(LVAL(resp_data, 8));
2537 attr->f_mtime = smb_ntutc2unixutc(LVAL(resp_data, 16));
2539 attr->attr = WVAL(resp_data, 32);
2541 /* allocated size (40) */
2542 attr->f_size = LVAL(resp_data, 48);
2550 * Note: called with the server locked.
2553 * (1) Win 95 swaps the date and time fields in the standard info level.
2556 smb_proc_getattr_trans2_std(struct smb_sb_info *server, struct dentry *dir,
2557 struct smb_fattr *attr)
2559 char *p, *param = server->temp_buf;
2561 int off_date = 0, off_time = 2;
2562 unsigned char *resp_data = NULL;
2563 unsigned char *resp_param = NULL;
2564 int resp_data_len = 0;
2565 int resp_param_len = 0;
2569 WSET(param, 0, SMB_INFO_STANDARD);
2571 result = smb_encode_path(server, param+6, SMB_MAXPATHLEN+1, dir, NULL);
2574 p = param + 6 + result;
2576 result = smb_trans2_request(server, TRANSACT2_QPATHINFO,
2577 0, NULL, p - param, param,
2578 &resp_data_len, &resp_data,
2579 &resp_param_len, &resp_param);
2582 if (smb_retry(server))
2586 if (server->rcls != 0)
2588 VERBOSE("for %s: result=%d, rcls=%d, err=%d\n",
2589 ¶m[6], result, server->rcls, server->err);
2590 result = smb_errno(server);
2595 if (resp_data_len < 22)
2597 PARANOIA("not enough data for %s, len=%d\n",
2598 ¶m[6], resp_data_len);
2603 * Kludge alert: Win 95 swaps the date and time field,
2604 * contrary to the CIFS docs and Win NT practice.
2606 if (server->mnt->flags & SMB_MOUNT_WIN95) {
2610 date = WVAL(resp_data, off_date);
2611 time = WVAL(resp_data, off_time);
2612 attr->f_ctime = date_dos2unix(server, date, time);
2614 date = WVAL(resp_data, 4 + off_date);
2615 time = WVAL(resp_data, 4 + off_time);
2616 attr->f_atime = date_dos2unix(server, date, time);
2618 date = WVAL(resp_data, 8 + off_date);
2619 time = WVAL(resp_data, 8 + off_time);
2620 attr->f_mtime = date_dos2unix(server, date, time);
2621 #ifdef SMBFS_DEBUG_TIMESTAMP
2622 printk(KERN_DEBUG "getattr_trans2: %s/%s, date=%x, time=%x, mtime=%ld\n",
2623 DENTRY_PATH(dir), date, time, attr->f_mtime);
2625 attr->f_size = DVAL(resp_data, 12);
2626 attr->attr = WVAL(resp_data, 20);
2634 smb_proc_getattr_95(struct smb_sb_info *server, struct dentry *dir,
2635 struct smb_fattr *attr)
2637 struct inode *inode = dir->d_inode;
2641 result = smb_proc_getattr_trans2_std(server, dir, attr);
2643 if (server->rcls == ERRSRV && server->err == ERRerror) {
2644 /* a damn Win95 bug - sometimes it clags if you
2646 current->state = TASK_INTERRUPTIBLE;
2647 schedule_timeout(HZ/5);
2654 * None of the other getattr versions here can make win9x
2655 * return the right filesize if there are changes made to an
2656 * open file. A seek-to-end does return the right size, but
2657 * we only need to do that on files we have written.
2659 if (inode && inode->u.smbfs_i.flags & SMB_F_LOCALWRITE &&
2662 __u16 fileid = inode->u.smbfs_i.fileid;
2663 attr->f_size = smb_proc_seek(server, fileid, 2, 0);
2671 * Note: called with the server locked.
2674 smb_proc_getattr_unix(struct smb_sb_info *server, struct dentry *dir,
2675 struct smb_fattr *attr)
2677 char *p, *param = server->temp_buf;
2678 unsigned char *resp_data = NULL;
2679 unsigned char *resp_param = NULL;
2680 int resp_data_len = 0;
2681 int resp_param_len = 0;
2685 WSET(param, 0, SMB_QUERY_FILE_UNIX_BASIC);
2687 result = smb_encode_path(server, param+6, SMB_MAXPATHLEN+1, dir, NULL);
2690 p = param + 6 + result;
2692 result = smb_trans2_request(server, TRANSACT2_QPATHINFO,
2693 0, NULL, p - param, param,
2694 &resp_data_len, &resp_data,
2695 &resp_param_len, &resp_param);
2697 if (smb_retry(server))
2701 if (server->rcls != 0) {
2702 VERBOSE("for %s: result=%d, rcls=%d, err=%d\n",
2703 ¶m[6], result, server->rcls, server->err);
2704 result = smb_errno(server);
2708 smb_decode_unix_basic(attr, server, resp_data);
2716 smb_proc_getattr_null(struct smb_sb_info *server, struct dentry *dir,
2717 struct smb_fattr *attr)
2723 smb_proc_getattr(struct dentry *dir, struct smb_fattr *fattr)
2725 struct smb_sb_info *server = server_from_dentry(dir);
2728 smb_lock_server(server);
2729 smb_init_dirent(server, fattr);
2730 result = server->ops->getattr(server, dir, fattr);
2731 smb_finish_dirent(server, fattr);
2732 smb_unlock_server(server);
2738 * Called with the server locked. Because of bugs in the
2739 * core protocol, we use this only to set attributes. See
2740 * smb_proc_settime() below for timestamp handling.
2743 * (1) If mtime is non-zero, both Win 3.1 and Win 95 fail
2744 * with an undocumented error (ERRDOS code 50). Setting
2745 * mtime to 0 allows the attributes to be set.
2746 * (2) The extra parameters following the name string aren't
2747 * in the CIFS docs, but seem to be necessary for operation.
2750 smb_proc_setattr_core(struct smb_sb_info *server, struct dentry *dentry,
2757 p = smb_setup_header(server, SMBsetatr, 8, 0);
2758 WSET(server->packet, smb_vwv0, attr);
2759 DSET(server->packet, smb_vwv1, 0); /* mtime */
2760 WSET(server->packet, smb_vwv3, 0); /* reserved values */
2761 WSET(server->packet, smb_vwv4, 0);
2762 WSET(server->packet, smb_vwv5, 0);
2763 WSET(server->packet, smb_vwv6, 0);
2764 WSET(server->packet, smb_vwv7, 0);
2765 result = smb_simple_encode_path(server, &p, dentry, NULL);
2768 if (p + 2 > (char *)server->packet + server->packet_size) {
2769 result = -ENAMETOOLONG;
2774 smb_setup_bcc(server, p);
2776 result = smb_request_ok(server, SMBsetatr, 0, 0);
2778 if (smb_retry(server))
2788 * Because of bugs in the trans2 setattr messages, we must set
2789 * attributes and timestamps separately. The core SMBsetatr
2790 * message seems to be the only reliable way to set attributes.
2793 smb_proc_setattr(struct dentry *dir, struct smb_fattr *fattr)
2795 struct smb_sb_info *server = server_from_dentry(dir);
2798 VERBOSE("setting %s/%s, open=%d\n",
2799 DENTRY_PATH(dir), smb_is_open(dir->d_inode));
2800 smb_lock_server(server);
2801 result = smb_proc_setattr_core(server, dir, fattr->attr);
2802 smb_unlock_server(server);
2807 * Called with the server locked. Sets the timestamps for an
2808 * file open with write permissions.
2811 smb_proc_setattr_ext(struct smb_sb_info *server,
2812 struct inode *inode, struct smb_fattr *fattr)
2818 smb_setup_header(server, SMBsetattrE, 7, 0);
2819 WSET(server->packet, smb_vwv0, inode->u.smbfs_i.fileid);
2820 /* We don't change the creation time */
2821 WSET(server->packet, smb_vwv1, 0);
2822 WSET(server->packet, smb_vwv2, 0);
2823 date_unix2dos(server, fattr->f_atime, &date, &time);
2824 WSET(server->packet, smb_vwv3, date);
2825 WSET(server->packet, smb_vwv4, time);
2826 date_unix2dos(server, fattr->f_mtime, &date, &time);
2827 WSET(server->packet, smb_vwv5, date);
2828 WSET(server->packet, smb_vwv6, time);
2829 #ifdef SMBFS_DEBUG_TIMESTAMP
2830 printk(KERN_DEBUG "smb_proc_setattr_ext: date=%d, time=%d, mtime=%ld\n",
2831 date, time, fattr->f_mtime);
2834 result = smb_request_ok(server, SMBsetattrE, 0, 0);
2836 if (smb_retry(server))
2846 * Note: called with the server locked.
2849 * (1) The TRANSACT2_SETPATHINFO message under Win NT 4.0 doesn't
2850 * set the file's attribute flags.
2853 smb_proc_setattr_trans2(struct smb_sb_info *server,
2854 struct dentry *dir, struct smb_fattr *fattr)
2857 char *p, *param = server->temp_buf;
2858 unsigned char *resp_data = NULL;
2859 unsigned char *resp_param = NULL;
2860 int resp_data_len = 0;
2861 int resp_param_len = 0;
2866 WSET(param, 0, 1); /* Info level SMB_INFO_STANDARD */
2868 result = smb_encode_path(server, param+6, SMB_MAXPATHLEN+1, dir, NULL);
2871 p = param + 6 + result;
2873 WSET(data, 0, 0); /* creation time */
2875 date_unix2dos(server, fattr->f_atime, &date, &time);
2876 WSET(data, 4, date);
2877 WSET(data, 6, time);
2878 date_unix2dos(server, fattr->f_mtime, &date, &time);
2879 WSET(data, 8, date);
2880 WSET(data, 10, time);
2881 #ifdef SMBFS_DEBUG_TIMESTAMP
2882 printk(KERN_DEBUG "setattr_trans2: %s/%s, date=%x, time=%x, mtime=%ld\n",
2883 DENTRY_PATH(dir), date, time, fattr->f_mtime);
2885 DSET(data, 12, 0); /* size */
2886 DSET(data, 16, 0); /* blksize */
2887 WSET(data, 20, 0); /* attr */
2888 DSET(data, 22, 0); /* ULONG EA size */
2890 result = smb_trans2_request(server, TRANSACT2_SETPATHINFO,
2891 26, data, p - param, param,
2892 &resp_data_len, &resp_data,
2893 &resp_param_len, &resp_param);
2896 if (smb_retry(server))
2901 if (server->rcls != 0)
2902 result = smb_errno(server);
2916 * ATTR_ATIME_SET 0x080
2917 * ATTR_MTIME_SET 0x100
2919 * ATTR_ATTR_FLAG 0x400
2921 * major/minor should only be set by mknod.
2924 smb_proc_setattr_unix(struct dentry *dentry, struct iattr *attr,
2925 unsigned int major, unsigned int minor)
2927 struct smb_sb_info *server = server_from_dentry(dentry);
2930 unsigned char *resp_data = NULL;
2931 unsigned char *resp_param = NULL;
2932 int resp_data_len = 0;
2933 int resp_param_len = 0;
2937 smb_lock_server(server);
2938 param = server->temp_buf;
2940 DEBUG1("valid flags = 0x%04x\n", attr->ia_valid);
2943 WSET(param, 0, SMB_SET_FILE_UNIX_BASIC);
2945 result = smb_encode_path(server, param + 6, SMB_MAXPATHLEN+1, dentry, NULL);
2948 p = param + 6 + result;
2950 /* 0 L file size in bytes */
2951 /* 8 L file size on disk in bytes (block count) */
2954 /* 56 W file type enum */
2957 /* 76 L unique ID (inode) */
2958 /* 84 L permissions */
2959 /* 92 L link count */
2960 LSET(data, 0, SMB_SIZE_NO_CHANGE);
2961 LSET(data, 8, SMB_SIZE_NO_CHANGE);
2962 LSET(data, 16, SMB_TIME_NO_CHANGE);
2963 LSET(data, 24, SMB_TIME_NO_CHANGE);
2964 LSET(data, 32, SMB_TIME_NO_CHANGE);
2965 LSET(data, 40, SMB_UID_NO_CHANGE);
2966 LSET(data, 48, SMB_GID_NO_CHANGE);
2967 DSET(data, 56, smb_filetype_from_mode(attr->ia_mode));
2968 LSET(data, 60, major);
2969 LSET(data, 68, minor);
2971 LSET(data, 84, SMB_MODE_NO_CHANGE);
2974 if (attr->ia_valid & ATTR_SIZE) {
2975 LSET(data, 0, attr->ia_size);
2976 LSET(data, 8, 0); /* can't set anyway */
2980 * FIXME: check the conversion function it the correct one
2982 * we can't set ctime but we might as well pass this to the server
2983 * and let it ignore it.
2985 if (attr->ia_valid & ATTR_CTIME) {
2986 nttime = smb_unixutc2ntutc(attr->ia_ctime);
2987 LSET(data, 16, nttime);
2989 if (attr->ia_valid & ATTR_ATIME) {
2990 nttime = smb_unixutc2ntutc(attr->ia_atime);
2991 LSET(data, 24, nttime);
2993 if (attr->ia_valid & ATTR_MTIME) {
2994 nttime = smb_unixutc2ntutc(attr->ia_mtime);
2995 LSET(data, 32, nttime);
2998 if (attr->ia_valid & ATTR_UID) {
2999 LSET(data, 40, attr->ia_uid);
3001 if (attr->ia_valid & ATTR_GID) {
3002 LSET(data, 48, attr->ia_gid);
3005 if (attr->ia_valid & ATTR_MODE) {
3006 LSET(data, 84, attr->ia_mode);
3009 result = smb_trans2_request(server, TRANSACT2_SETPATHINFO,
3010 sizeof(data), data, p - param, param,
3011 &resp_data_len, &resp_data,
3012 &resp_param_len, &resp_param);
3015 if (smb_retry(server))
3020 if (server->rcls != 0)
3021 result = smb_errno(server);
3024 smb_unlock_server(server);
3029 * Set the modify and access timestamps for a file.
3031 * Incredibly enough, in all of SMB there is no message to allow
3032 * setting both attributes and timestamps at once.
3035 * (1) Win 95 doesn't support the TRANSACT2_SETFILEINFO message
3036 * with info level 1 (INFO_STANDARD).
3037 * (2) Win 95 seems not to support setting directory timestamps.
3038 * (3) Under the core protocol apparently the only way to set the
3039 * timestamp is to open and close the file.
3042 smb_proc_settime(struct dentry *dentry, struct smb_fattr *fattr)
3044 struct smb_sb_info *server = server_from_dentry(dentry);
3045 struct inode *inode = dentry->d_inode;
3048 VERBOSE("setting %s/%s, open=%d\n",
3049 DENTRY_PATH(dentry), smb_is_open(inode));
3051 smb_lock_server(server);
3052 /* setting the time on a Win95 server fails (tridge) */
3053 if (server->opt.protocol >= SMB_PROTOCOL_LANMAN2 &&
3054 !(server->mnt->flags & SMB_MOUNT_WIN95)) {
3055 if (smb_is_open(inode) &&
3056 inode->u.smbfs_i.access != SMB_O_RDONLY)
3057 result = smb_proc_setattr_ext(server, inode, fattr);
3059 result = smb_proc_setattr_trans2(server, dentry, fattr);
3062 * Fail silently on directories ... timestamp can't be set?
3065 if (S_ISREG(inode->i_mode)) {
3067 * Set the mtime by opening and closing the file.
3068 * Note that the file is opened read-only, but this
3069 * still allows us to set the date (tridge)
3072 if (!smb_is_open(inode))
3073 smb_proc_open(server, dentry, SMB_O_RDONLY);
3074 if (smb_is_open(inode)) {
3075 inode->i_mtime = fattr->f_mtime;
3076 result = smb_proc_close_inode(server, inode);
3081 smb_unlock_server(server);
3086 smb_proc_dskattr(struct super_block *sb, struct statfs *attr)
3088 struct smb_sb_info *server = &(sb->u.smbfs_sb);
3093 smb_lock_server(server);
3096 smb_setup_header(server, SMBdskattr, 0, 0);
3098 if ((result = smb_request_ok(server, SMBdskattr, 5, 0)) < 0) {
3099 if (smb_retry(server))
3103 p = SMB_VWV(server->packet);
3104 unit = (WVAL(p, 2) * WVAL(p, 4)) >> SMB_ST_BLKSHIFT;
3105 attr->f_blocks = WVAL(p, 0) * unit;
3106 attr->f_bsize = SMB_ST_BLKSIZE;
3107 attr->f_bavail = attr->f_bfree = WVAL(p, 6) * unit;
3111 smb_unlock_server(server);
3116 smb_proc_read_link(struct smb_sb_info *server, struct dentry *dentry,
3117 char *buffer, int len)
3119 char *p, *param = server->temp_buf;
3120 unsigned char *resp_data = NULL;
3121 unsigned char *resp_param = NULL;
3122 int resp_data_len = 0;
3123 int resp_param_len = 0;
3126 DEBUG1("readlink of %s/%s\n", DENTRY_PATH(dentry));
3128 smb_lock_server(server);
3130 WSET(param, 0, SMB_QUERY_FILE_UNIX_LINK);
3132 result = smb_encode_path(server, param + 6, SMB_MAXPATHLEN+1, dentry, NULL);
3135 p = param + 6 + result;
3137 result = smb_trans2_request(server, TRANSACT2_QPATHINFO,
3138 0, NULL, p - param, param,
3139 &resp_data_len, &resp_data,
3140 &resp_param_len, &resp_param);
3141 DEBUG1("for %s: result=%d, rcls=%d, err=%d\n",
3142 ¶m[6], result, server->rcls, server->err);
3144 if (smb_retry(server))
3148 if (server->rcls != 0) {
3149 VERBOSE("for %s: result=%d, rcls=%d, err=%d\n",
3150 ¶m[6], result, server->rcls, server->err);
3151 result = smb_errno(server);
3155 /* copy data up to the \0 or buffer length */
3157 if (resp_data_len < len)
3158 result = resp_data_len;
3159 strncpy(buffer, resp_data, result);
3162 smb_unlock_server(server);
3168 * Create a symlink object called dentry which points to oldpath.
3169 * Samba does not permit dangling links but returns a suitable error message.
3172 smb_proc_symlink(struct smb_sb_info *server, struct dentry *dentry,
3173 const char *oldpath)
3175 char *p, *param = server->temp_buf;
3176 unsigned char *resp_data = NULL;
3177 unsigned char *resp_param = NULL;
3178 int resp_data_len = 0;
3179 int resp_param_len = 0;
3182 smb_lock_server(server);
3184 WSET(param, 0, SMB_SET_FILE_UNIX_LINK);
3186 result = smb_encode_path(server, param + 6, SMB_MAXPATHLEN+1, dentry, NULL);
3189 p = param + 6 + result;
3191 result = smb_trans2_request(server, TRANSACT2_SETPATHINFO,
3192 strlen(oldpath) + 1,
3193 (char *)oldpath, p - param, param,
3194 &resp_data_len, &resp_data,
3195 &resp_param_len, &resp_param);
3196 DEBUG1("for %s: result=%d, rcls=%d, err=%d\n",
3197 ¶m[6], result, server->rcls, server->err);
3199 if (smb_retry(server))
3203 if (server->rcls != 0) {
3204 VERBOSE("for %s: result=%d, rcls=%d, err=%d\n",
3205 ¶m[6], result, server->rcls, server->err);
3206 result = smb_errno(server);
3213 smb_unlock_server(server);
3218 * Create a hard link object called new_dentry which points to dentry.
3221 smb_proc_link(struct smb_sb_info *server, struct dentry *dentry,
3222 struct dentry *new_dentry)
3224 char *p, *param = server->temp_buf;
3225 unsigned char *resp_data = NULL;
3226 unsigned char *resp_param = NULL;
3227 int resp_data_len = 0;
3228 int resp_param_len = 0;
3231 char newpath[SMB_MAXPATHLEN+1]; /* FIXME: ugh! */
3233 smb_lock_server(server);
3235 newpathlen = smb_encode_path(server, newpath, SMB_MAXPATHLEN+1, dentry, NULL);
3237 DEBUG1("newpathlen = %d newpath=\"%s\"\n", newpathlen, newpath);
3239 WSET(param, 0, SMB_SET_FILE_UNIX_HLINK);
3241 result = smb_encode_path(server, param + 6, SMB_MAXPATHLEN+1, new_dentry, NULL);
3244 p = param + 6 + result;
3245 DEBUG1("pathlen = %d oldpath=\"%s\"\n", result, param + 6);
3248 result = smb_trans2_request(server, TRANSACT2_SETPATHINFO,
3250 (char *)newpath, p - param, param,
3251 &resp_data_len, &resp_data,
3252 &resp_param_len, &resp_param);
3253 DEBUG1("for %s: result=%d, rcls=%d, err=%d\n",
3254 ¶m[6], result, server->rcls, server->err);
3256 if (smb_retry(server))
3260 if (server->rcls != 0) {
3261 VERBOSE("for %s: result=%d, rcls=%d, err=%d\n",
3262 ¶m[6], result, server->rcls, server->err);
3263 result = smb_errno(server);
3270 smb_unlock_server(server);
3275 * We are called with the server locked
3278 smb_proc_query_cifsunix(struct smb_sb_info *server)
3280 char *param = server->temp_buf;
3281 unsigned char *resp_data = NULL;
3282 unsigned char *resp_param = NULL;
3283 int resp_data_len = 0;
3284 int resp_param_len = 0;
3290 WSET(param, 0, SMB_QUERY_CIFS_UNIX_INFO);
3291 result = smb_trans2_request(server, TRANSACT2_QFSINFO,
3293 &resp_data_len, &resp_data,
3294 &resp_param_len, &resp_param);
3296 if (smb_retry(server))
3301 if (resp_data_len < 12) {
3302 PARANOIA("Not enough data\n");
3306 major = WVAL(resp_data, 0);
3307 minor = WVAL(resp_data, 2);
3308 VERBOSE("Server implements CIFS Extensions for UNIX systems v%d.%d\n",
3310 /* FIXME: verify that we are ok with this major/minor? */
3312 caps = LVAL(resp_data, 4);
3313 DEBUG1("Server capabilities 0x%016llx\n", caps);
3321 install_ops(struct smb_ops *dst, struct smb_ops *src)
3323 memcpy(dst, src, sizeof(void *) * SMB_OPS_NUM_STATIC);
3327 static struct smb_ops smb_ops_core =
3329 .read = smb_proc_read,
3330 .write = smb_proc_write,
3331 .readdir = smb_proc_readdir_short,
3332 .getattr = smb_proc_getattr_core,
3333 .truncate = smb_proc_trunc32,
3336 /* LANMAN2, OS/2, others? */
3337 static struct smb_ops smb_ops_os2 =
3339 .read = smb_proc_read,
3340 .write = smb_proc_write,
3341 .readdir = smb_proc_readdir_long,
3342 .getattr = smb_proc_getattr_trans2_std,
3343 .truncate = smb_proc_trunc32,
3346 /* Win95, and possibly some NetApp versions too */
3347 static struct smb_ops smb_ops_win95 =
3349 .read = smb_proc_read, /* does not support 12word readX */
3350 .write = smb_proc_write,
3351 .readdir = smb_proc_readdir_long,
3352 .getattr = smb_proc_getattr_95,
3353 .truncate = smb_proc_trunc95,
3356 /* Samba, NT4 and NT5 */
3357 static struct smb_ops smb_ops_winNT =
3359 .read = smb_proc_readX,
3360 .write = smb_proc_writeX,
3361 .readdir = smb_proc_readdir_long,
3362 .getattr = smb_proc_getattr_trans2_all,
3363 .truncate = smb_proc_trunc64,
3366 /* Samba w/ unix extensions. Others? */
3367 static struct smb_ops smb_ops_unix =
3369 .read = smb_proc_readX,
3370 .write = smb_proc_writeX,
3371 .readdir = smb_proc_readdir_long,
3372 .getattr = smb_proc_getattr_unix,
3373 .truncate = smb_proc_trunc64,
3376 /* Place holder until real ops are in place */
3377 static struct smb_ops smb_ops_null =
3379 .getattr = smb_proc_getattr_null,
3382 void smb_install_null_ops(struct smb_ops *ops)
3384 install_ops(ops, &smb_ops_null);