diff options
Diffstat (limited to 'fs/smbfs/proc.c')
-rw-r--r-- | fs/smbfs/proc.c | 3509 |
1 files changed, 3509 insertions, 0 deletions
diff --git a/fs/smbfs/proc.c b/fs/smbfs/proc.c new file mode 100644 index 00000000000..220babe91ef --- /dev/null +++ b/fs/smbfs/proc.c @@ -0,0 +1,3509 @@ +/* + * proc.c + * + * Copyright (C) 1995, 1996 by Paal-Kr. Engstad and Volker Lendecke + * Copyright (C) 1997 by Volker Lendecke + * + * Please add a note about your changes to smbfs in the ChangeLog file. + */ + +#include <linux/types.h> +#include <linux/errno.h> +#include <linux/slab.h> +#include <linux/fs.h> +#include <linux/file.h> +#include <linux/stat.h> +#include <linux/fcntl.h> +#include <linux/dcache.h> +#include <linux/dirent.h> +#include <linux/nls.h> +#include <linux/smp_lock.h> +#include <linux/net.h> +#include <linux/vfs.h> +#include <linux/smb_fs.h> +#include <linux/smbno.h> +#include <linux/smb_mount.h> + +#include <net/sock.h> + +#include <asm/string.h> +#include <asm/div64.h> + +#include "smb_debug.h" +#include "proto.h" +#include "request.h" + + +/* Features. Undefine if they cause problems, this should perhaps be a + config option. */ +#define SMBFS_POSIX_UNLINK 1 + +/* Allow smb_retry to be interrupted. */ +#define SMB_RETRY_INTR + +#define SMB_VWV(packet) ((packet) + SMB_HEADER_LEN) +#define SMB_CMD(packet) (*(packet+8)) +#define SMB_WCT(packet) (*(packet+SMB_HEADER_LEN - 1)) + +#define SMB_DIRINFO_SIZE 43 +#define SMB_STATUS_SIZE 21 + +#define SMB_ST_BLKSIZE (PAGE_SIZE) +#define SMB_ST_BLKSHIFT (PAGE_SHIFT) + +static struct smb_ops smb_ops_core; +static struct smb_ops smb_ops_os2; +static struct smb_ops smb_ops_win95; +static struct smb_ops smb_ops_winNT; +static struct smb_ops smb_ops_unix; +static struct smb_ops smb_ops_null; + +static void +smb_init_dirent(struct smb_sb_info *server, struct smb_fattr *fattr); +static void +smb_finish_dirent(struct smb_sb_info *server, struct smb_fattr *fattr); +static int +smb_proc_getattr_core(struct smb_sb_info *server, struct dentry *dir, + struct smb_fattr *fattr); +static int +smb_proc_getattr_ff(struct smb_sb_info *server, struct dentry *dentry, + struct smb_fattr *fattr); +static int +smb_proc_setattr_core(struct smb_sb_info *server, struct dentry *dentry, + u16 attr); +static int +smb_proc_setattr_ext(struct smb_sb_info *server, + struct inode *inode, struct smb_fattr *fattr); +static int +smb_proc_query_cifsunix(struct smb_sb_info *server); +static void +install_ops(struct smb_ops *dst, struct smb_ops *src); + + +static void +str_upper(char *name, int len) +{ + while (len--) + { + if (*name >= 'a' && *name <= 'z') + *name -= ('a' - 'A'); + name++; + } +} + +#if 0 +static void +str_lower(char *name, int len) +{ + while (len--) + { + if (*name >= 'A' && *name <= 'Z') + *name += ('a' - 'A'); + name++; + } +} +#endif + +/* reverse a string inline. This is used by the dircache walking routines */ +static void reverse_string(char *buf, int len) +{ + char c; + char *end = buf+len-1; + + while(buf < end) { + c = *buf; + *(buf++) = *end; + *(end--) = c; + } +} + +/* no conversion, just a wrapper for memcpy. */ +static int convert_memcpy(unsigned char *output, int olen, + const unsigned char *input, int ilen, + struct nls_table *nls_from, + struct nls_table *nls_to) +{ + if (olen < ilen) + return -ENAMETOOLONG; + memcpy(output, input, ilen); + return ilen; +} + +static inline int write_char(unsigned char ch, char *output, int olen) +{ + if (olen < 4) + return -ENAMETOOLONG; + sprintf(output, ":x%02x", ch); + return 4; +} + +static inline int write_unichar(wchar_t ch, char *output, int olen) +{ + if (olen < 5) + return -ENAMETOOLONG; + sprintf(output, ":%04x", ch); + return 5; +} + +/* convert from one "codepage" to another (possibly being utf8). */ +static int convert_cp(unsigned char *output, int olen, + const unsigned char *input, int ilen, + struct nls_table *nls_from, + struct nls_table *nls_to) +{ + int len = 0; + int n; + wchar_t ch; + + while (ilen > 0) { + /* convert by changing to unicode and back to the new cp */ + n = nls_from->char2uni(input, ilen, &ch); + if (n == -EINVAL) { + ilen--; + n = write_char(*input++, output, olen); + if (n < 0) + goto fail; + output += n; + olen -= n; + len += n; + continue; + } else if (n < 0) + goto fail; + input += n; + ilen -= n; + + n = nls_to->uni2char(ch, output, olen); + if (n == -EINVAL) + n = write_unichar(ch, output, olen); + if (n < 0) + goto fail; + output += n; + olen -= n; + + len += n; + } + return len; +fail: + return n; +} + +/* ----------------------------------------------------------- */ + +/* + * nls_unicode + * + * This encodes/decodes little endian unicode format + */ + +static int uni2char(wchar_t uni, unsigned char *out, int boundlen) +{ + if (boundlen < 2) + return -EINVAL; + *out++ = uni & 0xff; + *out++ = uni >> 8; + return 2; +} + +static int char2uni(const unsigned char *rawstring, int boundlen, wchar_t *uni) +{ + if (boundlen < 2) + return -EINVAL; + *uni = (rawstring[1] << 8) | rawstring[0]; + return 2; +} + +static struct nls_table unicode_table = { + .charset = "unicode", + .uni2char = uni2char, + .char2uni = char2uni, +}; + +/* ----------------------------------------------------------- */ + +static int setcodepage(struct nls_table **p, char *name) +{ + struct nls_table *nls; + + if (!name || !*name) { + nls = NULL; + } else if ( (nls = load_nls(name)) == NULL) { + printk (KERN_ERR "smbfs: failed to load nls '%s'\n", name); + return -EINVAL; + } + + /* if already set, unload the previous one. */ + if (*p && *p != &unicode_table) + unload_nls(*p); + *p = nls; + + return 0; +} + +/* Handles all changes to codepage settings. */ +int smb_setcodepage(struct smb_sb_info *server, struct smb_nls_codepage *cp) +{ + int n = 0; + + smb_lock_server(server); + + /* Don't load any nls_* at all, if no remote is requested */ + if (!*cp->remote_name) + goto out; + + /* local */ + n = setcodepage(&server->local_nls, cp->local_name); + if (n != 0) + goto out; + + /* remote */ + if (!strcmp(cp->remote_name, "unicode")) { + server->remote_nls = &unicode_table; + } else { + n = setcodepage(&server->remote_nls, cp->remote_name); + if (n != 0) + setcodepage(&server->local_nls, NULL); + } + +out: + if (server->local_nls != NULL && server->remote_nls != NULL) + server->ops->convert = convert_cp; + else + server->ops->convert = convert_memcpy; + + smb_unlock_server(server); + return n; +} + + +/*****************************************************************************/ +/* */ +/* Encoding/Decoding section */ +/* */ +/*****************************************************************************/ + +static __u8 * +smb_encode_smb_length(__u8 * p, __u32 len) +{ + *p = 0; + *(p+1) = 0; + *(p+2) = (len & 0xFF00) >> 8; + *(p+3) = (len & 0xFF); + if (len > 0xFFFF) + { + *(p+1) = 1; + } + return p + 4; +} + +/* + * smb_build_path: build the path to entry and name storing it in buf. + * The path returned will have the trailing '\0'. + */ +static int smb_build_path(struct smb_sb_info *server, unsigned char *buf, + int maxlen, + struct dentry *entry, struct qstr *name) +{ + unsigned char *path = buf; + int len; + int unicode = (server->mnt->flags & SMB_MOUNT_UNICODE) != 0; + + if (maxlen < (2<<unicode)) + return -ENAMETOOLONG; + + if (maxlen > SMB_MAXPATHLEN + 1) + maxlen = SMB_MAXPATHLEN + 1; + + if (entry == NULL) + goto test_name_and_out; + + /* + * If IS_ROOT, we have to do no walking at all. + */ + if (IS_ROOT(entry) && !name) { + *path++ = '\\'; + if (unicode) *path++ = '\0'; + *path++ = '\0'; + if (unicode) *path++ = '\0'; + return path-buf; + } + + /* + * Build the path string walking the tree backward from end to ROOT + * and store it in reversed order [see reverse_string()] + */ + dget(entry); + spin_lock(&entry->d_lock); + while (!IS_ROOT(entry)) { + struct dentry *parent; + + if (maxlen < (3<<unicode)) { + spin_unlock(&entry->d_lock); + dput(entry); + return -ENAMETOOLONG; + } + + len = server->ops->convert(path, maxlen-2, + entry->d_name.name, entry->d_name.len, + server->local_nls, server->remote_nls); + if (len < 0) { + spin_unlock(&entry->d_lock); + dput(entry); + return len; + } + reverse_string(path, len); + path += len; + if (unicode) { + /* Note: reverse order */ + *path++ = '\0'; + maxlen--; + } + *path++ = '\\'; + maxlen -= len+1; + + parent = entry->d_parent; + dget(parent); + spin_unlock(&entry->d_lock); + dput(entry); + entry = parent; + spin_lock(&entry->d_lock); + } + spin_unlock(&entry->d_lock); + dput(entry); + reverse_string(buf, path-buf); + + /* maxlen has space for at least one char */ +test_name_and_out: + if (name) { + if (maxlen < (3<<unicode)) + return -ENAMETOOLONG; + *path++ = '\\'; + if (unicode) { + *path++ = '\0'; + maxlen--; + } + len = server->ops->convert(path, maxlen-2, + name->name, name->len, + server->local_nls, server->remote_nls); + if (len < 0) + return len; + path += len; + maxlen -= len+1; + } + /* maxlen has space for at least one char */ + *path++ = '\0'; + if (unicode) *path++ = '\0'; + return path-buf; +} + +static int smb_encode_path(struct smb_sb_info *server, char *buf, int maxlen, + struct dentry *dir, struct qstr *name) +{ + int result; + + result = smb_build_path(server, buf, maxlen, dir, name); + if (result < 0) + goto out; + if (server->opt.protocol <= SMB_PROTOCOL_COREPLUS) + str_upper(buf, result); +out: + return result; +} + +/* encode_path for non-trans2 request SMBs */ +static int smb_simple_encode_path(struct smb_request *req, char **p, + struct dentry * entry, struct qstr * name) +{ + struct smb_sb_info *server = req->rq_server; + char *s = *p; + int res; + int maxlen = ((char *)req->rq_buffer + req->rq_bufsize) - s; + int unicode = (server->mnt->flags & SMB_MOUNT_UNICODE); + + if (!maxlen) + return -ENAMETOOLONG; + *s++ = 4; /* ASCII data format */ + + /* + * SMB Unicode strings must be 16bit aligned relative the start of the + * packet. If they are not they must be padded with 0. + */ + if (unicode) { + int align = s - (char *)req->rq_buffer; + if (!(align & 1)) { + *s++ = '\0'; + maxlen--; + } + } + + res = smb_encode_path(server, s, maxlen-1, entry, name); + if (res < 0) + return res; + *p = s + res; + return 0; +} + +/* The following are taken directly from msdos-fs */ + +/* Linear day numbers of the respective 1sts in non-leap years. */ + +static int day_n[] = +{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 0, 0, 0, 0}; + /* JanFebMarApr May Jun Jul Aug Sep Oct Nov Dec */ + + +static time_t +utc2local(struct smb_sb_info *server, time_t time) +{ + return time - server->opt.serverzone*60; +} + +static time_t +local2utc(struct smb_sb_info *server, time_t time) +{ + return time + server->opt.serverzone*60; +} + +/* Convert a MS-DOS time/date pair to a UNIX date (seconds since 1 1 70). */ + +static time_t +date_dos2unix(struct smb_sb_info *server, __u16 date, __u16 time) +{ + int month, year; + time_t secs; + + /* first subtract and mask after that... Otherwise, if + date == 0, bad things happen */ + month = ((date >> 5) - 1) & 15; + year = date >> 9; + secs = (time & 31) * 2 + 60 * ((time >> 5) & 63) + (time >> 11) * 3600 + 86400 * + ((date & 31) - 1 + day_n[month] + (year / 4) + year * 365 - ((year & 3) == 0 && + month < 2 ? 1 : 0) + 3653); + /* days since 1.1.70 plus 80's leap day */ + return local2utc(server, secs); +} + + +/* Convert linear UNIX date to a MS-DOS time/date pair. */ + +static void +date_unix2dos(struct smb_sb_info *server, + int unix_date, __u16 *date, __u16 *time) +{ + int day, year, nl_day, month; + + unix_date = utc2local(server, unix_date); + if (unix_date < 315532800) + unix_date = 315532800; + + *time = (unix_date % 60) / 2 + + (((unix_date / 60) % 60) << 5) + + (((unix_date / 3600) % 24) << 11); + + day = unix_date / 86400 - 3652; + year = day / 365; + if ((year + 3) / 4 + 365 * year > day) + year--; + day -= (year + 3) / 4 + 365 * year; + if (day == 59 && !(year & 3)) { + nl_day = day; + month = 2; + } else { + nl_day = (year & 3) || day <= 59 ? day : day - 1; + for (month = 0; month < 12; month++) + if (day_n[month] > nl_day) + break; + } + *date = nl_day - day_n[month - 1] + 1 + (month << 5) + (year << 9); +} + +/* The following are taken from fs/ntfs/util.c */ + +#define NTFS_TIME_OFFSET ((u64)(369*365 + 89) * 24 * 3600 * 10000000) + +/* + * Convert the NT UTC (based 1601-01-01, in hundred nanosecond units) + * into Unix UTC (based 1970-01-01, in seconds). + */ +static struct timespec +smb_ntutc2unixutc(u64 ntutc) +{ + struct timespec ts; + /* FIXME: what about the timezone difference? */ + /* Subtract the NTFS time offset, then convert to 1s intervals. */ + u64 t = ntutc - NTFS_TIME_OFFSET; + ts.tv_nsec = do_div(t, 10000000) * 100; + ts.tv_sec = t; + return ts; +} + +/* Convert the Unix UTC into NT time */ +static u64 +smb_unixutc2ntutc(struct timespec ts) +{ + /* Note: timezone conversion is probably wrong. */ + /* return ((u64)utc2local(server, t)) * 10000000 + NTFS_TIME_OFFSET; */ + return ((u64)ts.tv_sec) * 10000000 + ts.tv_nsec/100 + NTFS_TIME_OFFSET; +} + +#define MAX_FILE_MODE 6 +static mode_t file_mode[] = { + S_IFREG, S_IFDIR, S_IFLNK, S_IFCHR, S_IFBLK, S_IFIFO, S_IFSOCK +}; + +static int smb_filetype_to_mode(u32 filetype) +{ + if (filetype > MAX_FILE_MODE) { + PARANOIA("Filetype out of range: %d\n", filetype); + return S_IFREG; + } + return file_mode[filetype]; +} + +static u32 smb_filetype_from_mode(int mode) +{ + if (S_ISREG(mode)) + return UNIX_TYPE_FILE; + if (S_ISDIR(mode)) + return UNIX_TYPE_DIR; + if (S_ISLNK(mode)) + return UNIX_TYPE_SYMLINK; + if (S_ISCHR(mode)) + return UNIX_TYPE_CHARDEV; + if (S_ISBLK(mode)) + return UNIX_TYPE_BLKDEV; + if (S_ISFIFO(mode)) + return UNIX_TYPE_FIFO; + if (S_ISSOCK(mode)) + return UNIX_TYPE_SOCKET; + return UNIX_TYPE_UNKNOWN; +} + + +/*****************************************************************************/ +/* */ +/* Support section. */ +/* */ +/*****************************************************************************/ + +__u32 +smb_len(__u8 * p) +{ + return ((*(p+1) & 0x1) << 16L) | (*(p+2) << 8L) | *(p+3); +} + +static __u16 +smb_bcc(__u8 * packet) +{ + int pos = SMB_HEADER_LEN + SMB_WCT(packet) * sizeof(__u16); + return WVAL(packet, pos); +} + +/* smb_valid_packet: We check if packet fulfills the basic + requirements of a smb packet */ + +static int +smb_valid_packet(__u8 * packet) +{ + return (packet[4] == 0xff + && packet[5] == 'S' + && packet[6] == 'M' + && packet[7] == 'B' + && (smb_len(packet) + 4 == SMB_HEADER_LEN + + SMB_WCT(packet) * 2 + smb_bcc(packet))); +} + +/* smb_verify: We check if we got the answer we expected, and if we + got enough data. If bcc == -1, we don't care. */ + +static int +smb_verify(__u8 * packet, int command, int wct, int bcc) +{ + if (SMB_CMD(packet) != command) + goto bad_command; + if (SMB_WCT(packet) < wct) + goto bad_wct; + if (bcc != -1 && smb_bcc(packet) < bcc) + goto bad_bcc; + return 0; + +bad_command: + printk(KERN_ERR "smb_verify: command=%x, SMB_CMD=%x??\n", + command, SMB_CMD(packet)); + goto fail; +bad_wct: + printk(KERN_ERR "smb_verify: command=%x, wct=%d, SMB_WCT=%d??\n", + command, wct, SMB_WCT(packet)); + goto fail; +bad_bcc: + printk(KERN_ERR "smb_verify: command=%x, bcc=%d, SMB_BCC=%d??\n", + command, bcc, smb_bcc(packet)); +fail: + return -EIO; +} + +/* + * Returns the maximum read or write size for the "payload". Making all of the + * packet fit within the negotiated max_xmit size. + * + * N.B. Since this value is usually computed before locking the server, + * the server's packet size must never be decreased! + */ +static inline int +smb_get_xmitsize(struct smb_sb_info *server, int overhead) +{ + return server->opt.max_xmit - overhead; +} + +/* + * Calculate the maximum read size + */ +int +smb_get_rsize(struct smb_sb_info *server) +{ + /* readX has 12 parameters, read has 5 */ + int overhead = SMB_HEADER_LEN + 12 * sizeof(__u16) + 2 + 1 + 2; + int size = smb_get_xmitsize(server, overhead); + + VERBOSE("xmit=%d, size=%d\n", server->opt.max_xmit, size); + + return size; +} + +/* + * Calculate the maximum write size + */ +int +smb_get_wsize(struct smb_sb_info *server) +{ + /* writeX has 14 parameters, write has 5 */ + int overhead = SMB_HEADER_LEN + 14 * sizeof(__u16) + 2 + 1 + 2; + int size = smb_get_xmitsize(server, overhead); + + VERBOSE("xmit=%d, size=%d\n", server->opt.max_xmit, size); + + return size; +} + +/* + * Convert SMB error codes to -E... errno values. + */ +int +smb_errno(struct smb_request *req) +{ + int errcls = req->rq_rcls; + int error = req->rq_err; + char *class = "Unknown"; + + VERBOSE("errcls %d code %d from command 0x%x\n", + errcls, error, SMB_CMD(req->rq_header)); + + if (errcls == ERRDOS) { + switch (error) { + case ERRbadfunc: + return -EINVAL; + case ERRbadfile: + case ERRbadpath: + return -ENOENT; + case ERRnofids: + return -EMFILE; + case ERRnoaccess: + return -EACCES; + case ERRbadfid: + return -EBADF; + case ERRbadmcb: + return -EREMOTEIO; + case ERRnomem: + return -ENOMEM; + case ERRbadmem: + return -EFAULT; + case ERRbadenv: + case ERRbadformat: + return -EREMOTEIO; + case ERRbadaccess: + return -EACCES; + case ERRbaddata: + return -E2BIG; + case ERRbaddrive: + return -ENXIO; + case ERRremcd: + return -EREMOTEIO; + case ERRdiffdevice: + return -EXDEV; + case ERRnofiles: + return -ENOENT; + case ERRbadshare: + return -ETXTBSY; + case ERRlock: + return -EDEADLK; + case ERRfilexists: + return -EEXIST; + case ERROR_INVALID_PARAMETER: + return -EINVAL; + case ERROR_DISK_FULL: + return -ENOSPC; + case ERROR_INVALID_NAME: + return -ENOENT; + case ERROR_DIR_NOT_EMPTY: + return -ENOTEMPTY; + case ERROR_NOT_LOCKED: + return -ENOLCK; + case ERROR_ALREADY_EXISTS: + return -EEXIST; + default: + class = "ERRDOS"; + goto err_unknown; + } + } else if (errcls == ERRSRV) { + switch (error) { + /* N.B. This is wrong ... EIO ? */ + case ERRerror: + return -ENFILE; + case ERRbadpw: + return -EINVAL; + case ERRbadtype: + case ERRtimeout: + return -EIO; + case ERRaccess: + return -EACCES; + /* + * This is a fatal error, as it means the "tree ID" + * for this connection is no longer valid. We map + * to a special error code and get a new connection. + */ + case ERRinvnid: + return -EBADSLT; + default: + class = "ERRSRV"; + goto err_unknown; + } + } else if (errcls == ERRHRD) { + switch (error) { + case ERRnowrite: + return -EROFS; + case ERRbadunit: + return -ENODEV; + case ERRnotready: + return -EUCLEAN; + case ERRbadcmd: + case ERRdata: + return -EIO; + case ERRbadreq: + return -ERANGE; + case ERRbadshare: + return -ETXTBSY; + case ERRlock: + return -EDEADLK; + case ERRdiskfull: + return -ENOSPC; + default: + class = "ERRHRD"; + goto err_unknown; + } + } else if (errcls == ERRCMD) { + class = "ERRCMD"; + } else if (errcls == SUCCESS) { + return 0; /* This is the only valid 0 return */ + } + +err_unknown: + printk(KERN_ERR "smb_errno: class %s, code %d from command 0x%x\n", + class, error, SMB_CMD(req->rq_header)); + return -EIO; +} + +/* smb_request_ok: We expect the server to be locked. Then we do the + request and check the answer completely. When smb_request_ok + returns 0, you can be quite sure that everything went well. When + the answer is <=0, the returned number is a valid unix errno. */ + +static int +smb_request_ok(struct smb_request *req, int command, int wct, int bcc) +{ + int result; + + req->rq_resp_wct = wct; + req->rq_resp_bcc = bcc; + + result = smb_add_request(req); + if (result != 0) { + DEBUG1("smb_request failed\n"); + goto out; + } + + if (smb_valid_packet(req->rq_header) != 0) { + PARANOIA("invalid packet!\n"); + goto out; + } + + result = smb_verify(req->rq_header, command, wct, bcc); + +out: + return result; +} + +/* + * This implements the NEWCONN ioctl. It installs the server pid, + * sets server->state to CONN_VALID, and wakes up the waiting process. + */ +int +smb_newconn(struct smb_sb_info *server, struct smb_conn_opt *opt) +{ + struct file *filp; + struct sock *sk; + int error; + + VERBOSE("fd=%d, pid=%d\n", opt->fd, current->pid); + + smb_lock_server(server); + + /* + * Make sure we don't already have a valid connection ... + */ + error = -EINVAL; + if (server->state == CONN_VALID) + goto out; + + error = -EACCES; + if (current->uid != server->mnt->mounted_uid && + !capable(CAP_SYS_ADMIN)) + goto out; + + error = -EBADF; + filp = fget(opt->fd); + if (!filp) + goto out; + if (!smb_valid_socket(filp->f_dentry->d_inode)) + goto out_putf; + + server->sock_file = filp; + server->conn_pid = current->pid; + server->opt = *opt; + server->generation += 1; + server->state = CONN_VALID; + error = 0; + + if (server->conn_error) { + /* + * conn_error is the returncode we originally decided to + * drop the old connection on. This message should be positive + * and not make people ask questions on why smbfs is printing + * error messages ... + */ + printk(KERN_INFO "SMB connection re-established (%d)\n", + server->conn_error); + server->conn_error = 0; + } + + /* + * Store the server in sock user_data (Only used by sunrpc) + */ + sk = SOCKET_I(filp->f_dentry->d_inode)->sk; + sk->sk_user_data = server; + + /* chain into the data_ready callback */ + server->data_ready = xchg(&sk->sk_data_ready, smb_data_ready); + + /* check if we have an old smbmount that uses seconds for the + serverzone */ + if (server->opt.serverzone > 12*60 || server->opt.serverzone < -12*60) + server->opt.serverzone /= 60; + + /* now that we have an established connection we can detect the server + type and enable bug workarounds */ + if (server->opt.protocol < SMB_PROTOCOL_LANMAN2) + install_ops(server->ops, &smb_ops_core); + else if (server->opt.protocol == SMB_PROTOCOL_LANMAN2) + install_ops(server->ops, &smb_ops_os2); + else if (server->opt.protocol == SMB_PROTOCOL_NT1 && + (server->opt.max_xmit < 0x1000) && + !(server->opt.capabilities & SMB_CAP_NT_SMBS)) { + /* FIXME: can we kill the WIN95 flag now? */ + server->mnt->flags |= SMB_MOUNT_WIN95; + VERBOSE("detected WIN95 server\n"); + install_ops(server->ops, &smb_ops_win95); + } else { + /* + * Samba has max_xmit 65535 + * NT4spX has max_xmit 4536 (or something like that) + * win2k has ... + */ + VERBOSE("detected NT1 (Samba, NT4/5) server\n"); + install_ops(server->ops, &smb_ops_winNT); + } + + /* FIXME: the win9x code wants to modify these ... (seek/trunc bug) */ + if (server->mnt->flags & SMB_MOUNT_OLDATTR) { + server->ops->getattr = smb_proc_getattr_core; + } else if (server->mnt->flags & SMB_MOUNT_DIRATTR) { + server->ops->getattr = smb_proc_getattr_ff; + } + + /* Decode server capabilities */ + if (server->opt.capabilities & SMB_CAP_LARGE_FILES) { + /* Should be ok to set this now, as no one can access the + mount until the connection has been established. */ + SB_of(server)->s_maxbytes = ~0ULL >> 1; + VERBOSE("LFS enabled\n"); + } + if (server->opt.capabilities & SMB_CAP_UNICODE) { + server->mnt->flags |= SMB_MOUNT_UNICODE; + VERBOSE("Unicode enabled\n"); + } else { + server->mnt->flags &= ~SMB_MOUNT_UNICODE; + } +#if 0 + /* flags we may test for other patches ... */ + if (server->opt.capabilities & SMB_CAP_LARGE_READX) { + VERBOSE("Large reads enabled\n"); + } + if (server->opt.capabilities & SMB_CAP_LARGE_WRITEX) { + VERBOSE("Large writes enabled\n"); + } +#endif + if (server->opt.capabilities & SMB_CAP_UNIX) { + struct inode *inode; + VERBOSE("Using UNIX CIFS extensions\n"); + install_ops(server->ops, &smb_ops_unix); + inode = SB_of(server)->s_root->d_inode; + if (inode) + inode->i_op = &smb_dir_inode_operations_unix; + } + + VERBOSE("protocol=%d, max_xmit=%d, pid=%d capabilities=0x%x\n", + server->opt.protocol, server->opt.max_xmit, server->conn_pid, + server->opt.capabilities); + + /* FIXME: this really should be done by smbmount. */ + if (server->opt.max_xmit > SMB_MAX_PACKET_SIZE) { + server->opt.max_xmit = SMB_MAX_PACKET_SIZE; + } + + smb_unlock_server(server); + smbiod_wake_up(); + if (server->opt.capabilities & SMB_CAP_UNIX) + smb_proc_query_cifsunix(server); + + server->conn_complete++; + wake_up_interruptible_all(&server->conn_wq); + return error; + +out: + smb_unlock_server(server); + smbiod_wake_up(); + return error; + +out_putf: + fput(filp); + goto out; +} + +/* smb_setup_header: We completely set up the packet. You only have to + insert the command-specific fields */ + +__u8 * +smb_setup_header(struct smb_request *req, __u8 command, __u16 wct, __u16 bcc) +{ + __u32 xmit_len = SMB_HEADER_LEN + wct * sizeof(__u16) + bcc + 2; + __u8 *p = req->rq_header; + struct smb_sb_info *server = req->rq_server; + + p = smb_encode_smb_length(p, xmit_len - 4); + + *p++ = 0xff; + *p++ = 'S'; + *p++ = 'M'; + *p++ = 'B'; + *p++ = command; + + memset(p, '\0', 19); + p += 19; + p += 8; + + if (server->opt.protocol > SMB_PROTOCOL_CORE) { + int flags = SMB_FLAGS_CASELESS_PATHNAMES; + int flags2 = SMB_FLAGS2_LONG_PATH_COMPONENTS | + SMB_FLAGS2_EXTENDED_ATTRIBUTES; /* EA? not really ... */ + + *(req->rq_header + smb_flg) = flags; + if (server->mnt->flags & SMB_MOUNT_UNICODE) + flags2 |= SMB_FLAGS2_UNICODE_STRINGS; + WSET(req->rq_header, smb_flg2, flags2); + } + *p++ = wct; /* wct */ + p += 2 * wct; + WSET(p, 0, bcc); + + /* Include the header in the data to send */ + req->rq_iovlen = 1; + req->rq_iov[0].iov_base = req->rq_header; + req->rq_iov[0].iov_len = xmit_len - bcc; + + return req->rq_buffer; +} + +static void +smb_setup_bcc(struct smb_request *req, __u8 *p) +{ + u16 bcc = p - req->rq_buffer; + u8 *pbcc = req->rq_header + SMB_HEADER_LEN + 2*SMB_WCT(req->rq_header); + + WSET(pbcc, 0, bcc); + + smb_encode_smb_length(req->rq_header, SMB_HEADER_LEN + + 2*SMB_WCT(req->rq_header) - 2 + bcc); + + /* Include the "bytes" in the data to send */ + req->rq_iovlen = 2; + req->rq_iov[1].iov_base = req->rq_buffer; + req->rq_iov[1].iov_len = bcc; +} + +static int +smb_proc_seek(struct smb_sb_info *server, __u16 fileid, + __u16 mode, off_t offset) +{ + int result; + struct smb_request *req; + + result = -ENOMEM; + if (! (req = smb_alloc_request(server, 0))) + goto out; + + smb_setup_header(req, SMBlseek, 4, 0); + WSET(req->rq_header, smb_vwv0, fileid); + WSET(req->rq_header, smb_vwv1, mode); + DSET(req->rq_header, smb_vwv2, offset); + req->rq_flags |= SMB_REQ_NORETRY; + + result = smb_request_ok(req, SMBlseek, 2, 0); + if (result < 0) { + result = 0; + goto out_free; + } + + result = DVAL(req->rq_header, smb_vwv0); +out_free: + smb_rput(req); +out: + return result; +} + +static int +smb_proc_open(struct smb_sb_info *server, struct dentry *dentry, int wish) +{ + struct inode *ino = dentry->d_inode; + struct smb_inode_info *ei = SMB_I(ino); + int mode, read_write = 0x42, read_only = 0x40; + int res; + char *p; + struct smb_request *req; + + /* + * Attempt to open r/w, unless there are no write privileges. + */ + mode = read_write; + if (!(ino->i_mode & (S_IWUSR | S_IWGRP | S_IWOTH))) + mode = read_only; +#if 0 + /* FIXME: why is this code not in? below we fix it so that a caller + wanting RO doesn't get RW. smb_revalidate_inode does some + optimization based on access mode. tail -f needs it to be correct. + + We must open rw since we don't do the open if called a second time + with different 'wish'. Is that not supported by smb servers? */ + if (!(wish & (O_WRONLY | O_RDWR))) + mode = read_only; +#endif + + res = -ENOMEM; + if (! (req = smb_alloc_request(server, PAGE_SIZE))) + goto out; + + retry: + p = smb_setup_header(req, SMBopen, 2, 0); + WSET(req->rq_header, smb_vwv0, mode); + WSET(req->rq_header, smb_vwv1, aSYSTEM | aHIDDEN | aDIR); + res = smb_simple_encode_path(req, &p, dentry, NULL); + if (res < 0) + goto out_free; + smb_setup_bcc(req, p); + + res = smb_request_ok(req, SMBopen, 7, 0); + if (res != 0) { + if (mode == read_write && + (res == -EACCES || res == -ETXTBSY || res == -EROFS)) + { + VERBOSE("%s/%s R/W failed, error=%d, retrying R/O\n", + DENTRY_PATH(dentry), res); + mode = read_only; + req->rq_flags = 0; + goto retry; + } + goto out_free; + } + /* We should now have data in vwv[0..6]. */ + + ei->fileid = WVAL(req->rq_header, smb_vwv0); + ei->attr = WVAL(req->rq_header, smb_vwv1); + /* smb_vwv2 has mtime */ + /* smb_vwv4 has size */ + ei->access = (WVAL(req->rq_header, smb_vwv6) & SMB_ACCMASK); + ei->open = server->generation; + +out_free: + smb_rput(req); +out: + return res; +} + +/* + * Make sure the file is open, and check that the access + * is compatible with the desired access. + */ +int +smb_open(struct dentry *dentry, int wish) +{ + struct inode *inode = dentry->d_inode; + int result; + __u16 access; + + result = -ENOENT; + if (!inode) { + printk(KERN_ERR "smb_open: no inode for dentry %s/%s\n", + DENTRY_PATH(dentry)); + goto out; + } + + if (!smb_is_open(inode)) { + struct smb_sb_info *server = server_from_inode(inode); + result = 0; + if (!smb_is_open(inode)) + result = smb_proc_open(server, dentry, wish); + if (result) + goto out; + /* + * A successful open means the path is still valid ... + */ + smb_renew_times(dentry); + } + + /* + * Check whether the access is compatible with the desired mode. + */ + result = 0; + access = SMB_I(inode)->access; + if (access != wish && access != SMB_O_RDWR) { + PARANOIA("%s/%s access denied, access=%x, wish=%x\n", + DENTRY_PATH(dentry), access, wish); + result = -EACCES; + } +out: + return result; +} + +static int +smb_proc_close(struct smb_sb_info *server, __u16 fileid, __u32 mtime) +{ + struct smb_request *req; + int result = -ENOMEM; + + if (! (req = smb_alloc_request(server, 0))) + goto out; + + smb_setup_header(req, SMBclose, 3, 0); + WSET(req->rq_header, smb_vwv0, fileid); + DSET(req->rq_header, smb_vwv1, utc2local(server, mtime)); + req->rq_flags |= SMB_REQ_NORETRY; + result = smb_request_ok(req, SMBclose, 0, 0); + + smb_rput(req); +out: + return result; +} + +/* + * Win NT 4.0 has an apparent bug in that it fails to update the + * modify time when writing to a file. As a workaround, we update + * both modify and access time locally, and post the times to the + * server when closing the file. + */ +static int +smb_proc_close_inode(struct smb_sb_info *server, struct inode * ino) +{ + struct smb_inode_info *ei = SMB_I(ino); + int result = 0; + if (smb_is_open(ino)) + { + /* + * We clear the open flag in advance, in case another + * process observes the value while we block below. + */ + ei->open = 0; + + /* + * Kludge alert: SMB timestamps are accurate only to + * two seconds ... round the times to avoid needless + * cache invalidations! + */ + if (ino->i_mtime.tv_sec & 1) { + ino->i_mtime.tv_sec--; + ino->i_mtime.tv_nsec = 0; + } + if (ino->i_atime.tv_sec & 1) { + ino->i_atim |