Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * fs/cifs/cifssmb.c |
| 3 | * |
Steve French | f19159d | 2010-04-21 04:12:10 +0000 | [diff] [blame] | 4 | * Copyright (C) International Business Machines Corp., 2002,2010 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * Author(s): Steve French (sfrench@us.ibm.com) |
| 6 | * |
| 7 | * Contains the routines for constructing the SMB PDUs themselves |
| 8 | * |
| 9 | * This library is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU Lesser General Public License as published |
| 11 | * by the Free Software Foundation; either version 2.1 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This library is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 17 | * the GNU Lesser General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU Lesser General Public License |
| 20 | * along with this library; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | /* SMB/CIFS PDU handling routines here - except for leftovers in connect.c */ |
| 25 | /* These are mostly routines that operate on a pathname, or on a tree id */ |
| 26 | /* (mounted volume), but there are eight handle based routines which must be */ |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 27 | /* treated slightly differently for reconnection purposes since we never */ |
| 28 | /* want to reuse a stale file handle and only the caller knows the file info */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
| 30 | #include <linux/fs.h> |
| 31 | #include <linux/kernel.h> |
| 32 | #include <linux/vfs.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 33 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include <linux/posix_acl_xattr.h> |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 35 | #include <linux/pagemap.h> |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 36 | #include <linux/swap.h> |
| 37 | #include <linux/task_io_accounting_ops.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <asm/uaccess.h> |
| 39 | #include "cifspdu.h" |
| 40 | #include "cifsglob.h" |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 41 | #include "cifsacl.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include "cifsproto.h" |
| 43 | #include "cifs_unicode.h" |
| 44 | #include "cifs_debug.h" |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 45 | #include "fscache.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
| 47 | #ifdef CONFIG_CIFS_POSIX |
| 48 | static struct { |
| 49 | int index; |
| 50 | char *name; |
| 51 | } protocols[] = { |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 52 | #ifdef CONFIG_CIFS_WEAK_PW_HASH |
| 53 | {LANMAN_PROT, "\2LM1.2X002"}, |
Steve French | 9ac00b7 | 2006-09-30 04:13:17 +0000 | [diff] [blame] | 54 | {LANMAN2_PROT, "\2LANMAN2.1"}, |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 55 | #endif /* weak password hashing for legacy clients */ |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 56 | {CIFS_PROT, "\2NT LM 0.12"}, |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 57 | {POSIX_PROT, "\2POSIX 2"}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | {BAD_PROT, "\2"} |
| 59 | }; |
| 60 | #else |
| 61 | static struct { |
| 62 | int index; |
| 63 | char *name; |
| 64 | } protocols[] = { |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 65 | #ifdef CONFIG_CIFS_WEAK_PW_HASH |
| 66 | {LANMAN_PROT, "\2LM1.2X002"}, |
Steve French | 18f75ca | 2006-10-01 03:13:01 +0000 | [diff] [blame] | 67 | {LANMAN2_PROT, "\2LANMAN2.1"}, |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 68 | #endif /* weak password hashing for legacy clients */ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 69 | {CIFS_PROT, "\2NT LM 0.12"}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | {BAD_PROT, "\2"} |
| 71 | }; |
| 72 | #endif |
| 73 | |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 74 | /* define the number of elements in the cifs dialect array */ |
| 75 | #ifdef CONFIG_CIFS_POSIX |
| 76 | #ifdef CONFIG_CIFS_WEAK_PW_HASH |
Steve French | 9ac00b7 | 2006-09-30 04:13:17 +0000 | [diff] [blame] | 77 | #define CIFS_NUM_PROT 4 |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 78 | #else |
| 79 | #define CIFS_NUM_PROT 2 |
| 80 | #endif /* CIFS_WEAK_PW_HASH */ |
| 81 | #else /* not posix */ |
| 82 | #ifdef CONFIG_CIFS_WEAK_PW_HASH |
Steve French | 9ac00b7 | 2006-09-30 04:13:17 +0000 | [diff] [blame] | 83 | #define CIFS_NUM_PROT 3 |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 84 | #else |
| 85 | #define CIFS_NUM_PROT 1 |
| 86 | #endif /* CONFIG_CIFS_WEAK_PW_HASH */ |
| 87 | #endif /* CIFS_POSIX */ |
| 88 | |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 89 | /* |
| 90 | * Mark as invalid, all open files on tree connections since they |
| 91 | * were closed when session to server was lost. |
| 92 | */ |
| 93 | void |
| 94 | cifs_mark_open_files_invalid(struct cifs_tcon *tcon) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | { |
| 96 | struct cifsFileInfo *open_file = NULL; |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 97 | struct list_head *tmp; |
| 98 | struct list_head *tmp1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 100 | /* list all files open on tree connection and mark them invalid */ |
Jeff Layton | 4477288 | 2010-10-15 15:34:03 -0400 | [diff] [blame] | 101 | spin_lock(&cifs_file_list_lock); |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 102 | list_for_each_safe(tmp, tmp1, &tcon->openFileList) { |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 103 | open_file = list_entry(tmp, struct cifsFileInfo, tlist); |
Steve French | ad8b15f | 2008-08-08 21:10:16 +0000 | [diff] [blame] | 104 | open_file->invalidHandle = true; |
Jeff Layton | 3bc303c | 2009-09-21 06:47:50 -0400 | [diff] [blame] | 105 | open_file->oplock_break_cancelled = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | } |
Jeff Layton | 4477288 | 2010-10-15 15:34:03 -0400 | [diff] [blame] | 107 | spin_unlock(&cifs_file_list_lock); |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 108 | /* |
| 109 | * BB Add call to invalidate_inodes(sb) for all superblocks mounted |
| 110 | * to this tcon. |
| 111 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Jeff Layton | 9162ab2 | 2009-09-03 12:07:17 -0400 | [diff] [blame] | 114 | /* reconnect the socket, tcon, and smb session if needed */ |
| 115 | static int |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 116 | cifs_reconnect_tcon(struct cifs_tcon *tcon, int smb_command) |
Jeff Layton | 9162ab2 | 2009-09-03 12:07:17 -0400 | [diff] [blame] | 117 | { |
Jeff Layton | c4a5534 | 2011-07-28 12:40:36 -0400 | [diff] [blame] | 118 | int rc; |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 119 | struct cifs_ses *ses; |
Jeff Layton | 9162ab2 | 2009-09-03 12:07:17 -0400 | [diff] [blame] | 120 | struct TCP_Server_Info *server; |
| 121 | struct nls_table *nls_codepage; |
| 122 | |
| 123 | /* |
| 124 | * SMBs NegProt, SessSetup, uLogoff do not have tcon yet so check for |
| 125 | * tcp and smb session status done differently for those three - in the |
| 126 | * calling routine |
| 127 | */ |
| 128 | if (!tcon) |
| 129 | return 0; |
| 130 | |
| 131 | ses = tcon->ses; |
| 132 | server = ses->server; |
| 133 | |
| 134 | /* |
| 135 | * only tree disconnect, open, and write, (and ulogoff which does not |
| 136 | * have tcon) are allowed as we start force umount |
| 137 | */ |
| 138 | if (tcon->tidStatus == CifsExiting) { |
| 139 | if (smb_command != SMB_COM_WRITE_ANDX && |
| 140 | smb_command != SMB_COM_OPEN_ANDX && |
| 141 | smb_command != SMB_COM_TREE_DISCONNECT) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 142 | cifs_dbg(FYI, "can not send cmd %d while umounting\n", |
| 143 | smb_command); |
Jeff Layton | 9162ab2 | 2009-09-03 12:07:17 -0400 | [diff] [blame] | 144 | return -ENODEV; |
| 145 | } |
| 146 | } |
| 147 | |
Jeff Layton | 9162ab2 | 2009-09-03 12:07:17 -0400 | [diff] [blame] | 148 | /* |
| 149 | * Give demultiplex thread up to 10 seconds to reconnect, should be |
| 150 | * greater than cifs socket timeout which is 7 seconds |
| 151 | */ |
| 152 | while (server->tcpStatus == CifsNeedReconnect) { |
| 153 | wait_event_interruptible_timeout(server->response_q, |
Steve French | fd88ce9 | 2011-04-12 01:01:14 +0000 | [diff] [blame] | 154 | (server->tcpStatus != CifsNeedReconnect), 10 * HZ); |
Jeff Layton | 9162ab2 | 2009-09-03 12:07:17 -0400 | [diff] [blame] | 155 | |
Steve French | fd88ce9 | 2011-04-12 01:01:14 +0000 | [diff] [blame] | 156 | /* are we still trying to reconnect? */ |
Jeff Layton | 9162ab2 | 2009-09-03 12:07:17 -0400 | [diff] [blame] | 157 | if (server->tcpStatus != CifsNeedReconnect) |
| 158 | break; |
| 159 | |
| 160 | /* |
| 161 | * on "soft" mounts we wait once. Hard mounts keep |
| 162 | * retrying until process is killed or server comes |
| 163 | * back on-line |
| 164 | */ |
Jeff Layton | d402539 | 2011-02-07 08:54:35 -0500 | [diff] [blame] | 165 | if (!tcon->retry) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 166 | cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n"); |
Jeff Layton | 9162ab2 | 2009-09-03 12:07:17 -0400 | [diff] [blame] | 167 | return -EHOSTDOWN; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | if (!ses->need_reconnect && !tcon->need_reconnect) |
| 172 | return 0; |
| 173 | |
| 174 | nls_codepage = load_nls_default(); |
| 175 | |
| 176 | /* |
| 177 | * need to prevent multiple threads trying to simultaneously |
| 178 | * reconnect the same SMB session |
| 179 | */ |
Steve French | d7b619c | 2010-02-25 05:36:46 +0000 | [diff] [blame] | 180 | mutex_lock(&ses->session_mutex); |
Jeff Layton | 198b568 | 2010-04-24 07:57:48 -0400 | [diff] [blame] | 181 | rc = cifs_negotiate_protocol(0, ses); |
| 182 | if (rc == 0 && ses->need_reconnect) |
Jeff Layton | 9162ab2 | 2009-09-03 12:07:17 -0400 | [diff] [blame] | 183 | rc = cifs_setup_session(0, ses, nls_codepage); |
| 184 | |
| 185 | /* do we need to reconnect tcon? */ |
| 186 | if (rc || !tcon->need_reconnect) { |
Steve French | d7b619c | 2010-02-25 05:36:46 +0000 | [diff] [blame] | 187 | mutex_unlock(&ses->session_mutex); |
Jeff Layton | 9162ab2 | 2009-09-03 12:07:17 -0400 | [diff] [blame] | 188 | goto out; |
| 189 | } |
| 190 | |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 191 | cifs_mark_open_files_invalid(tcon); |
Jeff Layton | 9162ab2 | 2009-09-03 12:07:17 -0400 | [diff] [blame] | 192 | rc = CIFSTCon(0, ses, tcon->treeName, tcon, nls_codepage); |
Steve French | d7b619c | 2010-02-25 05:36:46 +0000 | [diff] [blame] | 193 | mutex_unlock(&ses->session_mutex); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 194 | cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc); |
Jeff Layton | 9162ab2 | 2009-09-03 12:07:17 -0400 | [diff] [blame] | 195 | |
| 196 | if (rc) |
| 197 | goto out; |
| 198 | |
| 199 | /* |
| 200 | * FIXME: check if wsize needs updated due to negotiated smb buffer |
| 201 | * size shrinking |
| 202 | */ |
| 203 | atomic_inc(&tconInfoReconnectCount); |
| 204 | |
| 205 | /* tell server Unix caps we support */ |
| 206 | if (ses->capabilities & CAP_UNIX) |
| 207 | reset_cifs_unix_caps(0, tcon, NULL, NULL); |
| 208 | |
| 209 | /* |
| 210 | * Removed call to reopen open files here. It is safer (and faster) to |
| 211 | * reopen files one at a time as needed in read and write. |
| 212 | * |
| 213 | * FIXME: what about file locks? don't we need to reclaim them ASAP? |
| 214 | */ |
| 215 | |
| 216 | out: |
| 217 | /* |
| 218 | * Check if handle based operation so we know whether we can continue |
| 219 | * or not without returning to caller to reset file handle |
| 220 | */ |
| 221 | switch (smb_command) { |
| 222 | case SMB_COM_READ_ANDX: |
| 223 | case SMB_COM_WRITE_ANDX: |
| 224 | case SMB_COM_CLOSE: |
| 225 | case SMB_COM_FIND_CLOSE2: |
| 226 | case SMB_COM_LOCKING_ANDX: |
| 227 | rc = -EAGAIN; |
| 228 | } |
| 229 | |
| 230 | unload_nls(nls_codepage); |
| 231 | return rc; |
| 232 | } |
| 233 | |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 234 | /* Allocate and return pointer to an SMB request buffer, and set basic |
| 235 | SMB information in the SMB header. If the return code is zero, this |
| 236 | function must have filled in request_buf pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | static int |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 238 | small_smb_init(int smb_command, int wct, struct cifs_tcon *tcon, |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 239 | void **request_buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | { |
Jeff Layton | f569599 | 2010-09-29 15:27:08 -0400 | [diff] [blame] | 241 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | |
Jeff Layton | 9162ab2 | 2009-09-03 12:07:17 -0400 | [diff] [blame] | 243 | rc = cifs_reconnect_tcon(tcon, smb_command); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 244 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | return rc; |
| 246 | |
| 247 | *request_buf = cifs_small_buf_get(); |
| 248 | if (*request_buf == NULL) { |
| 249 | /* BB should we add a retry in here if not a writepage? */ |
| 250 | return -ENOMEM; |
| 251 | } |
| 252 | |
Steve French | 63135e0 | 2007-07-17 17:34:02 +0000 | [diff] [blame] | 253 | header_assemble((struct smb_hdr *) *request_buf, smb_command, |
Steve French | c18c842 | 2007-07-18 23:21:09 +0000 | [diff] [blame] | 254 | tcon, wct); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 256 | if (tcon != NULL) |
| 257 | cifs_stats_inc(&tcon->num_smbs_sent); |
Steve French | a454434 | 2005-08-24 13:59:35 -0700 | [diff] [blame] | 258 | |
Jeff Layton | f569599 | 2010-09-29 15:27:08 -0400 | [diff] [blame] | 259 | return 0; |
Steve French | 5815449d | 2006-02-14 01:36:20 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Steve French | 12b3b8f | 2006-02-09 21:12:47 +0000 | [diff] [blame] | 262 | int |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 263 | small_smb_init_no_tc(const int smb_command, const int wct, |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 264 | struct cifs_ses *ses, void **request_buf) |
Steve French | 12b3b8f | 2006-02-09 21:12:47 +0000 | [diff] [blame] | 265 | { |
| 266 | int rc; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 267 | struct smb_hdr *buffer; |
Steve French | 12b3b8f | 2006-02-09 21:12:47 +0000 | [diff] [blame] | 268 | |
Steve French | 5815449d | 2006-02-14 01:36:20 +0000 | [diff] [blame] | 269 | rc = small_smb_init(smb_command, wct, NULL, request_buf); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 270 | if (rc) |
Steve French | 12b3b8f | 2006-02-09 21:12:47 +0000 | [diff] [blame] | 271 | return rc; |
| 272 | |
Steve French | 04fdabe | 2006-02-10 05:52:50 +0000 | [diff] [blame] | 273 | buffer = (struct smb_hdr *)*request_buf; |
Pavel Shilovsky | 8825736 | 2012-05-23 14:01:59 +0400 | [diff] [blame] | 274 | buffer->Mid = get_next_mid(ses->server); |
Steve French | 12b3b8f | 2006-02-09 21:12:47 +0000 | [diff] [blame] | 275 | if (ses->capabilities & CAP_UNICODE) |
| 276 | buffer->Flags2 |= SMBFLG2_UNICODE; |
Steve French | 04fdabe | 2006-02-10 05:52:50 +0000 | [diff] [blame] | 277 | if (ses->capabilities & CAP_STATUS32) |
Steve French | 12b3b8f | 2006-02-09 21:12:47 +0000 | [diff] [blame] | 278 | buffer->Flags2 |= SMBFLG2_ERR_STATUS; |
| 279 | |
| 280 | /* uid, tid can stay at zero as set in header assemble */ |
| 281 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 282 | /* BB add support for turning on the signing when |
Steve French | 12b3b8f | 2006-02-09 21:12:47 +0000 | [diff] [blame] | 283 | this function is used after 1st of session setup requests */ |
| 284 | |
| 285 | return rc; |
| 286 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | |
| 288 | /* If the return code is zero, this function must fill in request_buf pointer */ |
| 289 | static int |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 290 | __smb_init(int smb_command, int wct, struct cifs_tcon *tcon, |
Jeff Layton | f569599 | 2010-09-29 15:27:08 -0400 | [diff] [blame] | 291 | void **request_buf, void **response_buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | *request_buf = cifs_buf_get(); |
| 294 | if (*request_buf == NULL) { |
| 295 | /* BB should we add a retry in here if not a writepage? */ |
| 296 | return -ENOMEM; |
| 297 | } |
| 298 | /* Although the original thought was we needed the response buf for */ |
| 299 | /* potential retries of smb operations it turns out we can determine */ |
| 300 | /* from the mid flags when the request buffer can be resent without */ |
| 301 | /* having to use a second distinct buffer for the response */ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 302 | if (response_buf) |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 303 | *response_buf = *request_buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | |
| 305 | header_assemble((struct smb_hdr *) *request_buf, smb_command, tcon, |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 306 | wct); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 308 | if (tcon != NULL) |
| 309 | cifs_stats_inc(&tcon->num_smbs_sent); |
Steve French | a454434 | 2005-08-24 13:59:35 -0700 | [diff] [blame] | 310 | |
Jeff Layton | f569599 | 2010-09-29 15:27:08 -0400 | [diff] [blame] | 311 | return 0; |
| 312 | } |
| 313 | |
| 314 | /* If the return code is zero, this function must fill in request_buf pointer */ |
| 315 | static int |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 316 | smb_init(int smb_command, int wct, struct cifs_tcon *tcon, |
Jeff Layton | f569599 | 2010-09-29 15:27:08 -0400 | [diff] [blame] | 317 | void **request_buf, void **response_buf) |
| 318 | { |
| 319 | int rc; |
| 320 | |
| 321 | rc = cifs_reconnect_tcon(tcon, smb_command); |
| 322 | if (rc) |
| 323 | return rc; |
| 324 | |
| 325 | return __smb_init(smb_command, wct, tcon, request_buf, response_buf); |
| 326 | } |
| 327 | |
| 328 | static int |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 329 | smb_init_no_reconnect(int smb_command, int wct, struct cifs_tcon *tcon, |
Jeff Layton | f569599 | 2010-09-29 15:27:08 -0400 | [diff] [blame] | 330 | void **request_buf, void **response_buf) |
| 331 | { |
| 332 | if (tcon->ses->need_reconnect || tcon->need_reconnect) |
| 333 | return -EHOSTDOWN; |
| 334 | |
| 335 | return __smb_init(smb_command, wct, tcon, request_buf, response_buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | } |
| 337 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 338 | static int validate_t2(struct smb_t2_rsp *pSMB) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | { |
Jeff Layton | 12df83c | 2011-01-20 13:36:51 -0500 | [diff] [blame] | 340 | unsigned int total_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | |
Jeff Layton | 12df83c | 2011-01-20 13:36:51 -0500 | [diff] [blame] | 342 | /* check for plausible wct */ |
| 343 | if (pSMB->hdr.WordCount < 10) |
| 344 | goto vt2_err; |
| 345 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | /* check for parm and data offset going beyond end of smb */ |
Jeff Layton | 12df83c | 2011-01-20 13:36:51 -0500 | [diff] [blame] | 347 | if (get_unaligned_le16(&pSMB->t2_rsp.ParameterOffset) > 1024 || |
| 348 | get_unaligned_le16(&pSMB->t2_rsp.DataOffset) > 1024) |
| 349 | goto vt2_err; |
| 350 | |
Jeff Layton | 12df83c | 2011-01-20 13:36:51 -0500 | [diff] [blame] | 351 | total_size = get_unaligned_le16(&pSMB->t2_rsp.ParameterCount); |
| 352 | if (total_size >= 512) |
| 353 | goto vt2_err; |
| 354 | |
Jeff Layton | fd5707e | 2011-03-31 17:22:07 -0400 | [diff] [blame] | 355 | /* check that bcc is at least as big as parms + data, and that it is |
| 356 | * less than negotiated smb buffer |
| 357 | */ |
Jeff Layton | 12df83c | 2011-01-20 13:36:51 -0500 | [diff] [blame] | 358 | total_size += get_unaligned_le16(&pSMB->t2_rsp.DataCount); |
| 359 | if (total_size > get_bcc(&pSMB->hdr) || |
| 360 | total_size >= CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) |
| 361 | goto vt2_err; |
| 362 | |
| 363 | return 0; |
| 364 | vt2_err: |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 365 | cifs_dump_mem("Invalid transact2 SMB: ", (char *)pSMB, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | sizeof(struct smb_t2_rsp) + 16); |
Jeff Layton | 12df83c | 2011-01-20 13:36:51 -0500 | [diff] [blame] | 367 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | } |
Jeff Layton | 690c522 | 2011-01-20 13:36:51 -0500 | [diff] [blame] | 369 | |
Jeff Layton | 31d9e2b | 2013-05-26 07:00:57 -0400 | [diff] [blame] | 370 | static int |
Jeff Layton | 3f61822 | 2013-06-12 19:52:14 -0500 | [diff] [blame] | 371 | decode_ext_sec_blob(struct cifs_ses *ses, NEGOTIATE_RSP *pSMBr) |
Jeff Layton | 31d9e2b | 2013-05-26 07:00:57 -0400 | [diff] [blame] | 372 | { |
| 373 | int rc = 0; |
| 374 | u16 count; |
| 375 | char *guid = pSMBr->u.extended_response.GUID; |
Jeff Layton | 3f61822 | 2013-06-12 19:52:14 -0500 | [diff] [blame] | 376 | struct TCP_Server_Info *server = ses->server; |
Jeff Layton | 31d9e2b | 2013-05-26 07:00:57 -0400 | [diff] [blame] | 377 | |
| 378 | count = get_bcc(&pSMBr->hdr); |
| 379 | if (count < SMB1_CLIENT_GUID_SIZE) |
| 380 | return -EIO; |
| 381 | |
| 382 | spin_lock(&cifs_tcp_ses_lock); |
| 383 | if (server->srv_count > 1) { |
| 384 | spin_unlock(&cifs_tcp_ses_lock); |
| 385 | if (memcmp(server->server_GUID, guid, SMB1_CLIENT_GUID_SIZE) != 0) { |
| 386 | cifs_dbg(FYI, "server UID changed\n"); |
| 387 | memcpy(server->server_GUID, guid, SMB1_CLIENT_GUID_SIZE); |
| 388 | } |
| 389 | } else { |
| 390 | spin_unlock(&cifs_tcp_ses_lock); |
| 391 | memcpy(server->server_GUID, guid, SMB1_CLIENT_GUID_SIZE); |
| 392 | } |
| 393 | |
| 394 | if (count == SMB1_CLIENT_GUID_SIZE) { |
Jeff Layton | 3f61822 | 2013-06-12 19:52:14 -0500 | [diff] [blame] | 395 | server->sec_ntlmssp = true; |
Jeff Layton | 31d9e2b | 2013-05-26 07:00:57 -0400 | [diff] [blame] | 396 | } else { |
| 397 | count -= SMB1_CLIENT_GUID_SIZE; |
| 398 | rc = decode_negTokenInit( |
| 399 | pSMBr->u.extended_response.SecurityBlob, count, server); |
| 400 | if (rc != 1) |
| 401 | return -EINVAL; |
Jeff Layton | 31d9e2b | 2013-05-26 07:00:57 -0400 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | return 0; |
| 405 | } |
| 406 | |
Jeff Layton | 9ddec56 | 2013-05-26 07:00:58 -0400 | [diff] [blame] | 407 | int |
Jeff Layton | 38d77c5 | 2013-05-26 07:01:00 -0400 | [diff] [blame] | 408 | cifs_enable_signing(struct TCP_Server_Info *server, bool mnt_sign_required) |
Jeff Layton | 9ddec56 | 2013-05-26 07:00:58 -0400 | [diff] [blame] | 409 | { |
Jeff Layton | 5028588 | 2013-06-27 12:45:00 -0400 | [diff] [blame] | 410 | bool srv_sign_required = server->sec_mode & server->vals->signing_required; |
| 411 | bool srv_sign_enabled = server->sec_mode & server->vals->signing_enabled; |
Jeff Layton | 38d77c5 | 2013-05-26 07:01:00 -0400 | [diff] [blame] | 412 | bool mnt_sign_enabled = global_secflags & CIFSSEC_MAY_SIGN; |
| 413 | |
| 414 | /* |
| 415 | * Is signing required by mnt options? If not then check |
| 416 | * global_secflags to see if it is there. |
| 417 | */ |
| 418 | if (!mnt_sign_required) |
| 419 | mnt_sign_required = ((global_secflags & CIFSSEC_MUST_SIGN) == |
| 420 | CIFSSEC_MUST_SIGN); |
| 421 | |
| 422 | /* |
| 423 | * If signing is required then it's automatically enabled too, |
| 424 | * otherwise, check to see if the secflags allow it. |
| 425 | */ |
| 426 | mnt_sign_enabled = mnt_sign_required ? mnt_sign_required : |
| 427 | (global_secflags & CIFSSEC_MAY_SIGN); |
| 428 | |
| 429 | /* If server requires signing, does client allow it? */ |
| 430 | if (srv_sign_required) { |
| 431 | if (!mnt_sign_enabled) { |
| 432 | cifs_dbg(VFS, "Server requires signing, but it's disabled in SecurityFlags!"); |
| 433 | return -ENOTSUPP; |
Jeff Layton | 9ddec56 | 2013-05-26 07:00:58 -0400 | [diff] [blame] | 434 | } |
Jeff Layton | 38d77c5 | 2013-05-26 07:01:00 -0400 | [diff] [blame] | 435 | server->sign = true; |
| 436 | } |
| 437 | |
| 438 | /* If client requires signing, does server allow it? */ |
| 439 | if (mnt_sign_required) { |
| 440 | if (!srv_sign_enabled) { |
| 441 | cifs_dbg(VFS, "Server does not support signing!"); |
| 442 | return -ENOTSUPP; |
| 443 | } |
| 444 | server->sign = true; |
Jeff Layton | 9ddec56 | 2013-05-26 07:00:58 -0400 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | return 0; |
| 448 | } |
| 449 | |
Jeff Layton | 2190eca | 2013-05-26 07:00:57 -0400 | [diff] [blame] | 450 | #ifdef CONFIG_CIFS_WEAK_PW_HASH |
| 451 | static int |
Jeff Layton | 3f61822 | 2013-06-12 19:52:14 -0500 | [diff] [blame] | 452 | decode_lanman_negprot_rsp(struct TCP_Server_Info *server, NEGOTIATE_RSP *pSMBr) |
Jeff Layton | 2190eca | 2013-05-26 07:00:57 -0400 | [diff] [blame] | 453 | { |
| 454 | __s16 tmp; |
| 455 | struct lanman_neg_rsp *rsp = (struct lanman_neg_rsp *)pSMBr; |
| 456 | |
| 457 | if (server->dialect != LANMAN_PROT && server->dialect != LANMAN2_PROT) |
| 458 | return -EOPNOTSUPP; |
| 459 | |
Jeff Layton | 2190eca | 2013-05-26 07:00:57 -0400 | [diff] [blame] | 460 | server->sec_mode = le16_to_cpu(rsp->SecurityMode); |
| 461 | server->maxReq = min_t(unsigned int, |
| 462 | le16_to_cpu(rsp->MaxMpxCount), |
| 463 | cifs_max_pending); |
| 464 | set_credits(server, server->maxReq); |
| 465 | server->maxBuf = le16_to_cpu(rsp->MaxBufSize); |
Jeff Layton | 2190eca | 2013-05-26 07:00:57 -0400 | [diff] [blame] | 466 | /* even though we do not use raw we might as well set this |
| 467 | accurately, in case we ever find a need for it */ |
| 468 | if ((le16_to_cpu(rsp->RawMode) & RAW_ENABLE) == RAW_ENABLE) { |
| 469 | server->max_rw = 0xFF00; |
| 470 | server->capabilities = CAP_MPX_MODE | CAP_RAW_MODE; |
| 471 | } else { |
| 472 | server->max_rw = 0;/* do not need to use raw anyway */ |
| 473 | server->capabilities = CAP_MPX_MODE; |
| 474 | } |
| 475 | tmp = (__s16)le16_to_cpu(rsp->ServerTimeZone); |
| 476 | if (tmp == -1) { |
| 477 | /* OS/2 often does not set timezone therefore |
| 478 | * we must use server time to calc time zone. |
| 479 | * Could deviate slightly from the right zone. |
| 480 | * Smallest defined timezone difference is 15 minutes |
| 481 | * (i.e. Nepal). Rounding up/down is done to match |
| 482 | * this requirement. |
| 483 | */ |
| 484 | int val, seconds, remain, result; |
| 485 | struct timespec ts, utc; |
| 486 | utc = CURRENT_TIME; |
| 487 | ts = cnvrtDosUnixTm(rsp->SrvTime.Date, |
| 488 | rsp->SrvTime.Time, 0); |
| 489 | cifs_dbg(FYI, "SrvTime %d sec since 1970 (utc: %d) diff: %d\n", |
| 490 | (int)ts.tv_sec, (int)utc.tv_sec, |
| 491 | (int)(utc.tv_sec - ts.tv_sec)); |
| 492 | val = (int)(utc.tv_sec - ts.tv_sec); |
| 493 | seconds = abs(val); |
| 494 | result = (seconds / MIN_TZ_ADJ) * MIN_TZ_ADJ; |
| 495 | remain = seconds % MIN_TZ_ADJ; |
| 496 | if (remain >= (MIN_TZ_ADJ / 2)) |
| 497 | result += MIN_TZ_ADJ; |
| 498 | if (val < 0) |
| 499 | result = -result; |
| 500 | server->timeAdj = result; |
| 501 | } else { |
| 502 | server->timeAdj = (int)tmp; |
| 503 | server->timeAdj *= 60; /* also in seconds */ |
| 504 | } |
| 505 | cifs_dbg(FYI, "server->timeAdj: %d seconds\n", server->timeAdj); |
| 506 | |
| 507 | |
| 508 | /* BB get server time for time conversions and add |
| 509 | code to use it and timezone since this is not UTC */ |
| 510 | |
| 511 | if (rsp->EncryptionKeyLength == |
| 512 | cpu_to_le16(CIFS_CRYPTO_KEY_SIZE)) { |
| 513 | memcpy(server->cryptkey, rsp->EncryptionKey, |
| 514 | CIFS_CRYPTO_KEY_SIZE); |
| 515 | } else if (server->sec_mode & SECMODE_PW_ENCRYPT) { |
| 516 | return -EIO; /* need cryptkey unless plain text */ |
| 517 | } |
| 518 | |
| 519 | cifs_dbg(FYI, "LANMAN negotiated\n"); |
| 520 | return 0; |
| 521 | } |
| 522 | #else |
| 523 | static inline int |
Jeff Layton | 3f61822 | 2013-06-12 19:52:14 -0500 | [diff] [blame] | 524 | decode_lanman_negprot_rsp(struct TCP_Server_Info *server, NEGOTIATE_RSP *pSMBr) |
Jeff Layton | 2190eca | 2013-05-26 07:00:57 -0400 | [diff] [blame] | 525 | { |
| 526 | cifs_dbg(VFS, "mount failed, cifs module not built with CIFS_WEAK_PW_HASH support\n"); |
| 527 | return -EOPNOTSUPP; |
| 528 | } |
| 529 | #endif |
| 530 | |
Jeff Layton | 9193400 | 2013-05-26 07:00:58 -0400 | [diff] [blame] | 531 | static bool |
Jeff Layton | 3f61822 | 2013-06-12 19:52:14 -0500 | [diff] [blame] | 532 | should_set_ext_sec_flag(enum securityEnum sectype) |
Jeff Layton | 9193400 | 2013-05-26 07:00:58 -0400 | [diff] [blame] | 533 | { |
Jeff Layton | 3f61822 | 2013-06-12 19:52:14 -0500 | [diff] [blame] | 534 | switch (sectype) { |
| 535 | case RawNTLMSSP: |
| 536 | case Kerberos: |
Jeff Layton | 9193400 | 2013-05-26 07:00:58 -0400 | [diff] [blame] | 537 | return true; |
Jeff Layton | 3f61822 | 2013-06-12 19:52:14 -0500 | [diff] [blame] | 538 | case Unspecified: |
| 539 | if (global_secflags & |
| 540 | (CIFSSEC_MAY_KRB5 | CIFSSEC_MAY_NTLMSSP)) |
| 541 | return true; |
| 542 | /* Fallthrough */ |
| 543 | default: |
| 544 | return false; |
| 545 | } |
Jeff Layton | 9193400 | 2013-05-26 07:00:58 -0400 | [diff] [blame] | 546 | } |
| 547 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | int |
Pavel Shilovsky | 286170a | 2012-05-25 10:43:58 +0400 | [diff] [blame] | 549 | CIFSSMBNegotiate(const unsigned int xid, struct cifs_ses *ses) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | { |
| 551 | NEGOTIATE_REQ *pSMB; |
| 552 | NEGOTIATE_RSP *pSMBr; |
| 553 | int rc = 0; |
| 554 | int bytes_returned; |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 555 | int i; |
Jeff Layton | 3534b85 | 2013-05-24 07:41:01 -0400 | [diff] [blame] | 556 | struct TCP_Server_Info *server = ses->server; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | u16 count; |
| 558 | |
Jeff Layton | 3534b85 | 2013-05-24 07:41:01 -0400 | [diff] [blame] | 559 | if (!server) { |
| 560 | WARN(1, "%s: server is NULL!\n", __func__); |
| 561 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | } |
Jeff Layton | 3534b85 | 2013-05-24 07:41:01 -0400 | [diff] [blame] | 563 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | rc = smb_init(SMB_COM_NEGOTIATE, 0, NULL /* no tcon yet */ , |
| 565 | (void **) &pSMB, (void **) &pSMBr); |
| 566 | if (rc) |
| 567 | return rc; |
Steve French | 750d115 | 2006-06-27 06:28:30 +0000 | [diff] [blame] | 568 | |
Pavel Shilovsky | 8825736 | 2012-05-23 14:01:59 +0400 | [diff] [blame] | 569 | pSMB->hdr.Mid = get_next_mid(server); |
Yehuda Sadeh Weinraub | 100c1dd | 2007-06-05 21:31:16 +0000 | [diff] [blame] | 570 | pSMB->hdr.Flags2 |= (SMBFLG2_UNICODE | SMBFLG2_ERR_STATUS); |
Steve French | a013689 | 2007-10-04 20:05:09 +0000 | [diff] [blame] | 571 | |
Jeff Layton | 3f61822 | 2013-06-12 19:52:14 -0500 | [diff] [blame] | 572 | if (should_set_ext_sec_flag(ses->sectype)) { |
Jeff Layton | 9193400 | 2013-05-26 07:00:58 -0400 | [diff] [blame] | 573 | cifs_dbg(FYI, "Requesting extended security."); |
Steve French | ac68392 | 2009-05-06 04:16:04 +0000 | [diff] [blame] | 574 | pSMB->hdr.Flags2 |= SMBFLG2_EXT_SEC; |
| 575 | } |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 576 | |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 577 | count = 0; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 578 | for (i = 0; i < CIFS_NUM_PROT; i++) { |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 579 | strncpy(pSMB->DialectsArray+count, protocols[i].name, 16); |
| 580 | count += strlen(protocols[i].name) + 1; |
| 581 | /* null at end of source and target buffers anyway */ |
| 582 | } |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 583 | inc_rfc1001_len(pSMB, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | pSMB->ByteCount = cpu_to_le16(count); |
| 585 | |
| 586 | rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB, |
| 587 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 588 | if (rc != 0) |
Steve French | 254e55e | 2006-06-04 05:53:15 +0000 | [diff] [blame] | 589 | goto neg_err_exit; |
| 590 | |
Jeff Layton | 9bf67e5 | 2010-04-24 07:57:46 -0400 | [diff] [blame] | 591 | server->dialect = le16_to_cpu(pSMBr->DialectIndex); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 592 | cifs_dbg(FYI, "Dialect: %d\n", server->dialect); |
Steve French | 254e55e | 2006-06-04 05:53:15 +0000 | [diff] [blame] | 593 | /* Check wct = 1 error case */ |
Jeff Layton | 9bf67e5 | 2010-04-24 07:57:46 -0400 | [diff] [blame] | 594 | if ((pSMBr->hdr.WordCount < 13) || (server->dialect == BAD_PROT)) { |
Steve French | 254e55e | 2006-06-04 05:53:15 +0000 | [diff] [blame] | 595 | /* core returns wct = 1, but we do not ask for core - otherwise |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 596 | small wct just comes when dialect index is -1 indicating we |
Steve French | 254e55e | 2006-06-04 05:53:15 +0000 | [diff] [blame] | 597 | could not negotiate a common dialect */ |
| 598 | rc = -EOPNOTSUPP; |
| 599 | goto neg_err_exit; |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 600 | } else if (pSMBr->hdr.WordCount == 13) { |
Jeff Layton | e598d1d8 | 2013-05-26 07:00:59 -0400 | [diff] [blame] | 601 | server->negflavor = CIFS_NEGFLAVOR_LANMAN; |
Jeff Layton | 3f61822 | 2013-06-12 19:52:14 -0500 | [diff] [blame] | 602 | rc = decode_lanman_negprot_rsp(server, pSMBr); |
Jeff Layton | 9ddec56 | 2013-05-26 07:00:58 -0400 | [diff] [blame] | 603 | goto signing_check; |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 604 | } else if (pSMBr->hdr.WordCount != 17) { |
Steve French | 254e55e | 2006-06-04 05:53:15 +0000 | [diff] [blame] | 605 | /* unknown wct */ |
| 606 | rc = -EOPNOTSUPP; |
| 607 | goto neg_err_exit; |
| 608 | } |
Jeff Layton | 2190eca | 2013-05-26 07:00:57 -0400 | [diff] [blame] | 609 | /* else wct == 17, NTLM or better */ |
| 610 | |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 611 | server->sec_mode = pSMBr->SecurityMode; |
| 612 | if ((server->sec_mode & SECMODE_USER) == 0) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 613 | cifs_dbg(FYI, "share mode security\n"); |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 614 | |
Steve French | 254e55e | 2006-06-04 05:53:15 +0000 | [diff] [blame] | 615 | /* one byte, so no need to convert this or EncryptionKeyLen from |
| 616 | little endian */ |
Pavel Shilovsky | 10b9b98 | 2012-03-20 12:55:09 +0300 | [diff] [blame] | 617 | server->maxReq = min_t(unsigned int, le16_to_cpu(pSMBr->MaxMpxCount), |
| 618 | cifs_max_pending); |
Pavel Shilovsky | 4527578 | 2012-05-17 17:53:29 +0400 | [diff] [blame] | 619 | set_credits(server, server->maxReq); |
Steve French | 254e55e | 2006-06-04 05:53:15 +0000 | [diff] [blame] | 620 | /* probably no need to store and check maxvcs */ |
Jeff Layton | c974bef | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 621 | server->maxBuf = le32_to_cpu(pSMBr->MaxBufferSize); |
Steve French | eca6acf | 2009-02-20 05:43:09 +0000 | [diff] [blame] | 622 | server->max_rw = le32_to_cpu(pSMBr->MaxRawSize); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 623 | cifs_dbg(NOISY, "Max buf = %d\n", ses->server->maxBuf); |
Steve French | 254e55e | 2006-06-04 05:53:15 +0000 | [diff] [blame] | 624 | server->capabilities = le32_to_cpu(pSMBr->Capabilities); |
Steve French | b815f1e5 | 2006-10-02 05:53:29 +0000 | [diff] [blame] | 625 | server->timeAdj = (int)(__s16)le16_to_cpu(pSMBr->ServerTimeZone); |
| 626 | server->timeAdj *= 60; |
Jeff Layton | 31d9e2b | 2013-05-26 07:00:57 -0400 | [diff] [blame] | 627 | |
Jeff Layton | e598d1d8 | 2013-05-26 07:00:59 -0400 | [diff] [blame] | 628 | if (pSMBr->EncryptionKeyLength == CIFS_CRYPTO_KEY_SIZE) { |
| 629 | server->negflavor = CIFS_NEGFLAVOR_UNENCAP; |
Shirish Pargaonkar | d3ba50b | 2010-10-27 15:20:36 -0500 | [diff] [blame] | 630 | memcpy(ses->server->cryptkey, pSMBr->u.EncryptionKey, |
Steve French | 254e55e | 2006-06-04 05:53:15 +0000 | [diff] [blame] | 631 | CIFS_CRYPTO_KEY_SIZE); |
Jeff Layton | e598d1d8 | 2013-05-26 07:00:59 -0400 | [diff] [blame] | 632 | } else if ((pSMBr->hdr.Flags2 & SMBFLG2_EXT_SEC || |
Steve French | 07cc6cf | 2011-05-27 04:12:29 +0000 | [diff] [blame] | 633 | server->capabilities & CAP_EXTENDED_SECURITY) && |
Jeff Layton | e598d1d8 | 2013-05-26 07:00:59 -0400 | [diff] [blame] | 634 | (pSMBr->EncryptionKeyLength == 0)) { |
| 635 | server->negflavor = CIFS_NEGFLAVOR_EXTENDED; |
Jeff Layton | 3f61822 | 2013-06-12 19:52:14 -0500 | [diff] [blame] | 636 | rc = decode_ext_sec_blob(ses, pSMBr); |
Jeff Layton | e598d1d8 | 2013-05-26 07:00:59 -0400 | [diff] [blame] | 637 | } else if (server->sec_mode & SECMODE_PW_ENCRYPT) { |
Steve French | 07cc6cf | 2011-05-27 04:12:29 +0000 | [diff] [blame] | 638 | rc = -EIO; /* no crypt key only if plain text pwd */ |
Jeff Layton | e598d1d8 | 2013-05-26 07:00:59 -0400 | [diff] [blame] | 639 | } else { |
| 640 | server->negflavor = CIFS_NEGFLAVOR_UNENCAP; |
Steve French | 254e55e | 2006-06-04 05:53:15 +0000 | [diff] [blame] | 641 | server->capabilities &= ~CAP_EXTENDED_SECURITY; |
Jeff Layton | e598d1d8 | 2013-05-26 07:00:59 -0400 | [diff] [blame] | 642 | } |
Steve French | 254e55e | 2006-06-04 05:53:15 +0000 | [diff] [blame] | 643 | |
| 644 | signing_check: |
Jeff Layton | 9ddec56 | 2013-05-26 07:00:58 -0400 | [diff] [blame] | 645 | if (!rc) |
Jeff Layton | 38d77c5 | 2013-05-26 07:01:00 -0400 | [diff] [blame] | 646 | rc = cifs_enable_signing(server, ses->sign); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 647 | neg_err_exit: |
Steve French | 4a6d87f | 2005-08-13 08:15:54 -0700 | [diff] [blame] | 648 | cifs_buf_release(pSMB); |
Steve French | 254e55e | 2006-06-04 05:53:15 +0000 | [diff] [blame] | 649 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 650 | cifs_dbg(FYI, "negprot rc %d\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | return rc; |
| 652 | } |
| 653 | |
| 654 | int |
Pavel Shilovsky | 2e6e02a | 2012-05-25 11:11:39 +0400 | [diff] [blame] | 655 | CIFSSMBTDis(const unsigned int xid, struct cifs_tcon *tcon) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | { |
| 657 | struct smb_hdr *smb_buffer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | int rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 660 | cifs_dbg(FYI, "In tree disconnect\n"); |
Jeff Layton | f1987b4 | 2008-11-15 11:12:47 -0500 | [diff] [blame] | 661 | |
| 662 | /* BB: do we need to check this? These should never be NULL. */ |
| 663 | if ((tcon->ses == NULL) || (tcon->ses->server == NULL)) |
| 664 | return -EIO; |
| 665 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | /* |
Jeff Layton | f1987b4 | 2008-11-15 11:12:47 -0500 | [diff] [blame] | 667 | * No need to return error on this operation if tid invalidated and |
| 668 | * closed on server already e.g. due to tcp session crashing. Also, |
| 669 | * the tcon is no longer on the list, so no need to take lock before |
| 670 | * checking this. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | */ |
Steve French | 268875b | 2009-06-25 00:29:21 +0000 | [diff] [blame] | 672 | if ((tcon->need_reconnect) || (tcon->ses->need_reconnect)) |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 673 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 675 | rc = small_smb_init(SMB_COM_TREE_DISCONNECT, 0, tcon, |
Steve French | 09d1db5 | 2005-04-28 22:41:08 -0700 | [diff] [blame] | 676 | (void **)&smb_buffer); |
Jeff Layton | f1987b4 | 2008-11-15 11:12:47 -0500 | [diff] [blame] | 677 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | return rc; |
Steve French | 133672e | 2007-11-13 22:41:37 +0000 | [diff] [blame] | 679 | |
Pavel Shilovsky | 792af7b | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 680 | rc = SendReceiveNoRsp(xid, tcon->ses, (char *)smb_buffer, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 682 | cifs_dbg(FYI, "Tree disconnect failed %d\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 684 | /* No need to return error on this operation if tid invalidated and |
Jeff Layton | f1987b4 | 2008-11-15 11:12:47 -0500 | [diff] [blame] | 685 | closed on server already e.g. due to tcp session crashing */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | if (rc == -EAGAIN) |
| 687 | rc = 0; |
| 688 | |
| 689 | return rc; |
| 690 | } |
| 691 | |
Jeff Layton | 766fdbb | 2011-01-11 07:24:21 -0500 | [diff] [blame] | 692 | /* |
| 693 | * This is a no-op for now. We're not really interested in the reply, but |
| 694 | * rather in the fact that the server sent one and that server->lstrp |
| 695 | * gets updated. |
| 696 | * |
| 697 | * FIXME: maybe we should consider checking that the reply matches request? |
| 698 | */ |
| 699 | static void |
| 700 | cifs_echo_callback(struct mid_q_entry *mid) |
| 701 | { |
| 702 | struct TCP_Server_Info *server = mid->callback_data; |
| 703 | |
| 704 | DeleteMidQEntry(mid); |
Pavel Shilovsky | a891f0f | 2012-05-23 16:14:34 +0400 | [diff] [blame] | 705 | add_credits(server, 1, CIFS_ECHO_OP); |
Jeff Layton | 766fdbb | 2011-01-11 07:24:21 -0500 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | int |
| 709 | CIFSSMBEcho(struct TCP_Server_Info *server) |
| 710 | { |
| 711 | ECHO_REQ *smb; |
| 712 | int rc = 0; |
Jeff Layton | fcc31cb | 2011-05-19 16:22:53 -0400 | [diff] [blame] | 713 | struct kvec iov; |
Jeff Layton | fec344e | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 714 | struct smb_rqst rqst = { .rq_iov = &iov, |
| 715 | .rq_nvec = 1 }; |
Jeff Layton | 766fdbb | 2011-01-11 07:24:21 -0500 | [diff] [blame] | 716 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 717 | cifs_dbg(FYI, "In echo request\n"); |
Jeff Layton | 766fdbb | 2011-01-11 07:24:21 -0500 | [diff] [blame] | 718 | |
| 719 | rc = small_smb_init(SMB_COM_ECHO, 0, NULL, (void **)&smb); |
| 720 | if (rc) |
| 721 | return rc; |
| 722 | |
| 723 | /* set up echo request */ |
Steve French | 5443d13 | 2011-03-13 05:08:25 +0000 | [diff] [blame] | 724 | smb->hdr.Tid = 0xffff; |
Jeff Layton | 99d86c8 | 2011-01-20 21:19:25 -0500 | [diff] [blame] | 725 | smb->hdr.WordCount = 1; |
| 726 | put_unaligned_le16(1, &smb->EchoCount); |
Jeff Layton | 820a803 | 2011-05-04 08:05:26 -0400 | [diff] [blame] | 727 | put_bcc(1, &smb->hdr); |
Jeff Layton | 766fdbb | 2011-01-11 07:24:21 -0500 | [diff] [blame] | 728 | smb->Data[0] = 'a'; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 729 | inc_rfc1001_len(smb, 3); |
Jeff Layton | fcc31cb | 2011-05-19 16:22:53 -0400 | [diff] [blame] | 730 | iov.iov_base = smb; |
| 731 | iov.iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4; |
Jeff Layton | 766fdbb | 2011-01-11 07:24:21 -0500 | [diff] [blame] | 732 | |
Jeff Layton | fec344e | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 733 | rc = cifs_call_async(server, &rqst, NULL, cifs_echo_callback, |
Pavel Shilovsky | a891f0f | 2012-05-23 16:14:34 +0400 | [diff] [blame] | 734 | server, CIFS_ASYNC_OP | CIFS_ECHO_OP); |
Jeff Layton | 766fdbb | 2011-01-11 07:24:21 -0500 | [diff] [blame] | 735 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 736 | cifs_dbg(FYI, "Echo request failed: %d\n", rc); |
Jeff Layton | 766fdbb | 2011-01-11 07:24:21 -0500 | [diff] [blame] | 737 | |
| 738 | cifs_small_buf_release(smb); |
| 739 | |
| 740 | return rc; |
| 741 | } |
| 742 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | int |
Pavel Shilovsky | 58c45c5 | 2012-05-25 10:54:49 +0400 | [diff] [blame] | 744 | CIFSSMBLogoff(const unsigned int xid, struct cifs_ses *ses) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | LOGOFF_ANDX_REQ *pSMB; |
| 747 | int rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 749 | cifs_dbg(FYI, "In SMBLogoff for session disconnect\n"); |
Jeff Layton | 14fbf50 | 2008-11-14 13:53:46 -0500 | [diff] [blame] | 750 | |
| 751 | /* |
| 752 | * BB: do we need to check validity of ses and server? They should |
| 753 | * always be valid since we have an active reference. If not, that |
| 754 | * should probably be a BUG() |
| 755 | */ |
| 756 | if (!ses || !ses->server) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | return -EIO; |
| 758 | |
Steve French | d7b619c | 2010-02-25 05:36:46 +0000 | [diff] [blame] | 759 | mutex_lock(&ses->session_mutex); |
Steve French | 3b79521 | 2008-11-13 19:45:32 +0000 | [diff] [blame] | 760 | if (ses->need_reconnect) |
| 761 | goto session_already_dead; /* no need to send SMBlogoff if uid |
| 762 | already closed due to reconnect */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | rc = small_smb_init(SMB_COM_LOGOFF_ANDX, 2, NULL, (void **)&pSMB); |
| 764 | if (rc) { |
Steve French | d7b619c | 2010-02-25 05:36:46 +0000 | [diff] [blame] | 765 | mutex_unlock(&ses->session_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | return rc; |
| 767 | } |
| 768 | |
Pavel Shilovsky | 8825736 | 2012-05-23 14:01:59 +0400 | [diff] [blame] | 769 | pSMB->hdr.Mid = get_next_mid(ses->server); |
Steve French | 1982c34 | 2005-08-17 12:38:22 -0700 | [diff] [blame] | 770 | |
Jeff Layton | 38d77c5 | 2013-05-26 07:01:00 -0400 | [diff] [blame] | 771 | if (ses->server->sign) |
| 772 | pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | |
| 774 | pSMB->hdr.Uid = ses->Suid; |
| 775 | |
| 776 | pSMB->AndXCommand = 0xFF; |
Pavel Shilovsky | 792af7b | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 777 | rc = SendReceiveNoRsp(xid, ses, (char *) pSMB, 0); |
Steve French | 3b79521 | 2008-11-13 19:45:32 +0000 | [diff] [blame] | 778 | session_already_dead: |
Steve French | d7b619c | 2010-02-25 05:36:46 +0000 | [diff] [blame] | 779 | mutex_unlock(&ses->session_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | |
| 781 | /* if session dead then we do not need to do ulogoff, |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 782 | since server closed smb session, no sense reporting |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | error */ |
| 784 | if (rc == -EAGAIN) |
| 785 | rc = 0; |
| 786 | return rc; |
| 787 | } |
| 788 | |
| 789 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 790 | CIFSPOSIXDelFile(const unsigned int xid, struct cifs_tcon *tcon, |
| 791 | const char *fileName, __u16 type, |
| 792 | const struct nls_table *nls_codepage, int remap) |
Steve French | 2d785a5 | 2007-07-15 01:48:57 +0000 | [diff] [blame] | 793 | { |
| 794 | TRANSACTION2_SPI_REQ *pSMB = NULL; |
| 795 | TRANSACTION2_SPI_RSP *pSMBr = NULL; |
| 796 | struct unlink_psx_rq *pRqD; |
| 797 | int name_len; |
| 798 | int rc = 0; |
| 799 | int bytes_returned = 0; |
| 800 | __u16 params, param_offset, offset, byte_count; |
| 801 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 802 | cifs_dbg(FYI, "In POSIX delete\n"); |
Steve French | 2d785a5 | 2007-07-15 01:48:57 +0000 | [diff] [blame] | 803 | PsxDelete: |
| 804 | rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB, |
| 805 | (void **) &pSMBr); |
| 806 | if (rc) |
| 807 | return rc; |
| 808 | |
| 809 | if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) { |
| 810 | name_len = |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 811 | cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName, |
| 812 | PATH_MAX, nls_codepage, remap); |
Steve French | 2d785a5 | 2007-07-15 01:48:57 +0000 | [diff] [blame] | 813 | name_len++; /* trailing null */ |
| 814 | name_len *= 2; |
| 815 | } else { /* BB add path length overrun check */ |
| 816 | name_len = strnlen(fileName, PATH_MAX); |
| 817 | name_len++; /* trailing null */ |
| 818 | strncpy(pSMB->FileName, fileName, name_len); |
| 819 | } |
| 820 | |
| 821 | params = 6 + name_len; |
| 822 | pSMB->MaxParameterCount = cpu_to_le16(2); |
| 823 | pSMB->MaxDataCount = 0; /* BB double check this with jra */ |
| 824 | pSMB->MaxSetupCount = 0; |
| 825 | pSMB->Reserved = 0; |
| 826 | pSMB->Flags = 0; |
| 827 | pSMB->Timeout = 0; |
| 828 | pSMB->Reserved2 = 0; |
| 829 | param_offset = offsetof(struct smb_com_transaction2_spi_req, |
| 830 | InformationLevel) - 4; |
| 831 | offset = param_offset + params; |
| 832 | |
| 833 | /* Setup pointer to Request Data (inode type) */ |
| 834 | pRqD = (struct unlink_psx_rq *)(((char *)&pSMB->hdr.Protocol) + offset); |
| 835 | pRqD->type = cpu_to_le16(type); |
| 836 | pSMB->ParameterOffset = cpu_to_le16(param_offset); |
| 837 | pSMB->DataOffset = cpu_to_le16(offset); |
| 838 | pSMB->SetupCount = 1; |
| 839 | pSMB->Reserved3 = 0; |
| 840 | pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION); |
| 841 | byte_count = 3 /* pad */ + params + sizeof(struct unlink_psx_rq); |
| 842 | |
| 843 | pSMB->DataCount = cpu_to_le16(sizeof(struct unlink_psx_rq)); |
| 844 | pSMB->TotalDataCount = cpu_to_le16(sizeof(struct unlink_psx_rq)); |
| 845 | pSMB->ParameterCount = cpu_to_le16(params); |
| 846 | pSMB->TotalParameterCount = pSMB->ParameterCount; |
| 847 | pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_UNLINK); |
| 848 | pSMB->Reserved4 = 0; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 849 | inc_rfc1001_len(pSMB, byte_count); |
Steve French | 2d785a5 | 2007-07-15 01:48:57 +0000 | [diff] [blame] | 850 | pSMB->ByteCount = cpu_to_le16(byte_count); |
| 851 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 852 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 853 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 854 | cifs_dbg(FYI, "Posix delete returned %d\n", rc); |
Steve French | 2d785a5 | 2007-07-15 01:48:57 +0000 | [diff] [blame] | 855 | cifs_buf_release(pSMB); |
| 856 | |
Pavel Shilovsky | 44c5818 | 2012-05-28 14:16:31 +0400 | [diff] [blame] | 857 | cifs_stats_inc(&tcon->stats.cifs_stats.num_deletes); |
Steve French | 2d785a5 | 2007-07-15 01:48:57 +0000 | [diff] [blame] | 858 | |
| 859 | if (rc == -EAGAIN) |
| 860 | goto PsxDelete; |
| 861 | |
| 862 | return rc; |
| 863 | } |
| 864 | |
| 865 | int |
Pavel Shilovsky | ed6875e | 2012-09-18 16:20:25 -0700 | [diff] [blame] | 866 | CIFSSMBDelFile(const unsigned int xid, struct cifs_tcon *tcon, const char *name, |
| 867 | struct cifs_sb_info *cifs_sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | { |
| 869 | DELETE_FILE_REQ *pSMB = NULL; |
| 870 | DELETE_FILE_RSP *pSMBr = NULL; |
| 871 | int rc = 0; |
| 872 | int bytes_returned; |
| 873 | int name_len; |
Pavel Shilovsky | ed6875e | 2012-09-18 16:20:25 -0700 | [diff] [blame] | 874 | int remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | |
| 876 | DelFileRetry: |
| 877 | rc = smb_init(SMB_COM_DELETE, 1, tcon, (void **) &pSMB, |
| 878 | (void **) &pSMBr); |
| 879 | if (rc) |
| 880 | return rc; |
| 881 | |
| 882 | if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) { |
Pavel Shilovsky | ed6875e | 2012-09-18 16:20:25 -0700 | [diff] [blame] | 883 | name_len = cifsConvertToUTF16((__le16 *) pSMB->fileName, name, |
| 884 | PATH_MAX, cifs_sb->local_nls, |
| 885 | remap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | name_len++; /* trailing null */ |
| 887 | name_len *= 2; |
Steve French | 09d1db5 | 2005-04-28 22:41:08 -0700 | [diff] [blame] | 888 | } else { /* BB improve check for buffer overruns BB */ |
Pavel Shilovsky | ed6875e | 2012-09-18 16:20:25 -0700 | [diff] [blame] | 889 | name_len = strnlen(name, PATH_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | name_len++; /* trailing null */ |
Pavel Shilovsky | ed6875e | 2012-09-18 16:20:25 -0700 | [diff] [blame] | 891 | strncpy(pSMB->fileName, name, name_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | } |
| 893 | pSMB->SearchAttributes = |
| 894 | cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM); |
| 895 | pSMB->BufferFormat = 0x04; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 896 | inc_rfc1001_len(pSMB, name_len + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | pSMB->ByteCount = cpu_to_le16(name_len + 1); |
| 898 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 899 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
Pavel Shilovsky | 44c5818 | 2012-05-28 14:16:31 +0400 | [diff] [blame] | 900 | cifs_stats_inc(&tcon->stats.cifs_stats.num_deletes); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 901 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 902 | cifs_dbg(FYI, "Error in RMFile = %d\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | |
| 904 | cifs_buf_release(pSMB); |
| 905 | if (rc == -EAGAIN) |
| 906 | goto DelFileRetry; |
| 907 | |
| 908 | return rc; |
| 909 | } |
| 910 | |
| 911 | int |
Pavel Shilovsky | f958ca5 | 2012-07-10 16:14:18 +0400 | [diff] [blame] | 912 | CIFSSMBRmDir(const unsigned int xid, struct cifs_tcon *tcon, const char *name, |
| 913 | struct cifs_sb_info *cifs_sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | { |
| 915 | DELETE_DIRECTORY_REQ *pSMB = NULL; |
| 916 | DELETE_DIRECTORY_RSP *pSMBr = NULL; |
| 917 | int rc = 0; |
| 918 | int bytes_returned; |
| 919 | int name_len; |
Pavel Shilovsky | f958ca5 | 2012-07-10 16:14:18 +0400 | [diff] [blame] | 920 | int remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 922 | cifs_dbg(FYI, "In CIFSSMBRmDir\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | RmDirRetry: |
| 924 | rc = smb_init(SMB_COM_DELETE_DIRECTORY, 0, tcon, (void **) &pSMB, |
| 925 | (void **) &pSMBr); |
| 926 | if (rc) |
| 927 | return rc; |
| 928 | |
| 929 | if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) { |
Pavel Shilovsky | f958ca5 | 2012-07-10 16:14:18 +0400 | [diff] [blame] | 930 | name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, name, |
| 931 | PATH_MAX, cifs_sb->local_nls, |
| 932 | remap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | name_len++; /* trailing null */ |
| 934 | name_len *= 2; |
Steve French | 09d1db5 | 2005-04-28 22:41:08 -0700 | [diff] [blame] | 935 | } else { /* BB improve check for buffer overruns BB */ |
Pavel Shilovsky | f958ca5 | 2012-07-10 16:14:18 +0400 | [diff] [blame] | 936 | name_len = strnlen(name, PATH_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | name_len++; /* trailing null */ |
Pavel Shilovsky | f958ca5 | 2012-07-10 16:14:18 +0400 | [diff] [blame] | 938 | strncpy(pSMB->DirName, name, name_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | pSMB->BufferFormat = 0x04; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 942 | inc_rfc1001_len(pSMB, name_len + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | pSMB->ByteCount = cpu_to_le16(name_len + 1); |
| 944 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 945 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
Pavel Shilovsky | 44c5818 | 2012-05-28 14:16:31 +0400 | [diff] [blame] | 946 | cifs_stats_inc(&tcon->stats.cifs_stats.num_rmdirs); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 947 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 948 | cifs_dbg(FYI, "Error in RMDir = %d\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | |
| 950 | cifs_buf_release(pSMB); |
| 951 | if (rc == -EAGAIN) |
| 952 | goto RmDirRetry; |
| 953 | return rc; |
| 954 | } |
| 955 | |
| 956 | int |
Pavel Shilovsky | f436720 | 2012-03-17 11:41:12 +0300 | [diff] [blame] | 957 | CIFSSMBMkDir(const unsigned int xid, struct cifs_tcon *tcon, const char *name, |
| 958 | struct cifs_sb_info *cifs_sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | { |
| 960 | int rc = 0; |
| 961 | CREATE_DIRECTORY_REQ *pSMB = NULL; |
| 962 | CREATE_DIRECTORY_RSP *pSMBr = NULL; |
| 963 | int bytes_returned; |
| 964 | int name_len; |
Pavel Shilovsky | f436720 | 2012-03-17 11:41:12 +0300 | [diff] [blame] | 965 | int remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 967 | cifs_dbg(FYI, "In CIFSSMBMkDir\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | MkDirRetry: |
| 969 | rc = smb_init(SMB_COM_CREATE_DIRECTORY, 0, tcon, (void **) &pSMB, |
| 970 | (void **) &pSMBr); |
| 971 | if (rc) |
| 972 | return rc; |
| 973 | |
| 974 | if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) { |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 975 | name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, name, |
Pavel Shilovsky | f436720 | 2012-03-17 11:41:12 +0300 | [diff] [blame] | 976 | PATH_MAX, cifs_sb->local_nls, |
| 977 | remap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | name_len++; /* trailing null */ |
| 979 | name_len *= 2; |
Steve French | 09d1db5 | 2005-04-28 22:41:08 -0700 | [diff] [blame] | 980 | } else { /* BB improve check for buffer overruns BB */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | name_len = strnlen(name, PATH_MAX); |
| 982 | name_len++; /* trailing null */ |
| 983 | strncpy(pSMB->DirName, name, name_len); |
| 984 | } |
| 985 | |
| 986 | pSMB->BufferFormat = 0x04; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 987 | inc_rfc1001_len(pSMB, name_len + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | pSMB->ByteCount = cpu_to_le16(name_len + 1); |
| 989 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 990 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
Pavel Shilovsky | 44c5818 | 2012-05-28 14:16:31 +0400 | [diff] [blame] | 991 | cifs_stats_inc(&tcon->stats.cifs_stats.num_mkdirs); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 992 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 993 | cifs_dbg(FYI, "Error in Mkdir = %d\n", rc); |
Steve French | a5a2b48 | 2005-08-20 21:42:53 -0700 | [diff] [blame] | 994 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | cifs_buf_release(pSMB); |
| 996 | if (rc == -EAGAIN) |
| 997 | goto MkDirRetry; |
| 998 | return rc; |
| 999 | } |
| 1000 | |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1001 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1002 | CIFSPOSIXCreate(const unsigned int xid, struct cifs_tcon *tcon, |
| 1003 | __u32 posix_flags, __u64 mode, __u16 *netfid, |
| 1004 | FILE_UNIX_BASIC_INFO *pRetData, __u32 *pOplock, |
| 1005 | const char *name, const struct nls_table *nls_codepage, |
| 1006 | int remap) |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1007 | { |
| 1008 | TRANSACTION2_SPI_REQ *pSMB = NULL; |
| 1009 | TRANSACTION2_SPI_RSP *pSMBr = NULL; |
| 1010 | int name_len; |
| 1011 | int rc = 0; |
| 1012 | int bytes_returned = 0; |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1013 | __u16 params, param_offset, offset, byte_count, count; |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 1014 | OPEN_PSX_REQ *pdata; |
| 1015 | OPEN_PSX_RSP *psx_rsp; |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1016 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1017 | cifs_dbg(FYI, "In POSIX Create\n"); |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1018 | PsxCreat: |
| 1019 | rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB, |
| 1020 | (void **) &pSMBr); |
| 1021 | if (rc) |
| 1022 | return rc; |
| 1023 | |
| 1024 | if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) { |
| 1025 | name_len = |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 1026 | cifsConvertToUTF16((__le16 *) pSMB->FileName, name, |
| 1027 | PATH_MAX, nls_codepage, remap); |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1028 | name_len++; /* trailing null */ |
| 1029 | name_len *= 2; |
| 1030 | } else { /* BB improve the check for buffer overruns BB */ |
| 1031 | name_len = strnlen(name, PATH_MAX); |
| 1032 | name_len++; /* trailing null */ |
| 1033 | strncpy(pSMB->FileName, name, name_len); |
| 1034 | } |
| 1035 | |
| 1036 | params = 6 + name_len; |
| 1037 | count = sizeof(OPEN_PSX_REQ); |
| 1038 | pSMB->MaxParameterCount = cpu_to_le16(2); |
| 1039 | pSMB->MaxDataCount = cpu_to_le16(1000); /* large enough */ |
| 1040 | pSMB->MaxSetupCount = 0; |
| 1041 | pSMB->Reserved = 0; |
| 1042 | pSMB->Flags = 0; |
| 1043 | pSMB->Timeout = 0; |
| 1044 | pSMB->Reserved2 = 0; |
| 1045 | param_offset = offsetof(struct smb_com_transaction2_spi_req, |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 1046 | InformationLevel) - 4; |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1047 | offset = param_offset + params; |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1048 | pdata = (OPEN_PSX_REQ *)(((char *)&pSMB->hdr.Protocol) + offset); |
Cyril Gorcunov | 8f2376a | 2007-10-14 17:58:43 +0000 | [diff] [blame] | 1049 | pdata->Level = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC); |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1050 | pdata->Permissions = cpu_to_le64(mode); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 1051 | pdata->PosixOpenFlags = cpu_to_le32(posix_flags); |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1052 | pdata->OpenFlags = cpu_to_le32(*pOplock); |
| 1053 | pSMB->ParameterOffset = cpu_to_le16(param_offset); |
| 1054 | pSMB->DataOffset = cpu_to_le16(offset); |
| 1055 | pSMB->SetupCount = 1; |
| 1056 | pSMB->Reserved3 = 0; |
| 1057 | pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION); |
| 1058 | byte_count = 3 /* pad */ + params + count; |
| 1059 | |
| 1060 | pSMB->DataCount = cpu_to_le16(count); |
| 1061 | pSMB->ParameterCount = cpu_to_le16(params); |
| 1062 | pSMB->TotalDataCount = pSMB->DataCount; |
| 1063 | pSMB->TotalParameterCount = pSMB->ParameterCount; |
| 1064 | pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_OPEN); |
| 1065 | pSMB->Reserved4 = 0; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 1066 | inc_rfc1001_len(pSMB, byte_count); |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1067 | pSMB->ByteCount = cpu_to_le16(byte_count); |
| 1068 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 1069 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
| 1070 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1071 | cifs_dbg(FYI, "Posix create returned %d\n", rc); |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1072 | goto psx_create_err; |
| 1073 | } |
| 1074 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1075 | cifs_dbg(FYI, "copying inode info\n"); |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1076 | rc = validate_t2((struct smb_t2_rsp *)pSMBr); |
| 1077 | |
Jeff Layton | 820a803 | 2011-05-04 08:05:26 -0400 | [diff] [blame] | 1078 | if (rc || get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)) { |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1079 | rc = -EIO; /* bad smb */ |
| 1080 | goto psx_create_err; |
| 1081 | } |
| 1082 | |
| 1083 | /* copy return information to pRetData */ |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 1084 | psx_rsp = (OPEN_PSX_RSP *)((char *) &pSMBr->hdr.Protocol |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1085 | + le16_to_cpu(pSMBr->t2.DataOffset)); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 1086 | |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1087 | *pOplock = le16_to_cpu(psx_rsp->OplockFlags); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1088 | if (netfid) |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1089 | *netfid = psx_rsp->Fid; /* cifs fid stays in le */ |
| 1090 | /* Let caller know file was created so we can set the mode. */ |
| 1091 | /* Do we care about the CreateAction in any other cases? */ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1092 | if (cpu_to_le32(FILE_CREATE) == psx_rsp->CreateAction) |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1093 | *pOplock |= CIFS_CREATE_ACTION; |
| 1094 | /* check to make sure response data is there */ |
Cyril Gorcunov | 8f2376a | 2007-10-14 17:58:43 +0000 | [diff] [blame] | 1095 | if (psx_rsp->ReturnedLevel != cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC)) { |
| 1096 | pRetData->Type = cpu_to_le32(-1); /* unknown */ |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1097 | cifs_dbg(NOISY, "unknown type\n"); |
Steve French | cbac3cb | 2007-04-25 11:46:06 +0000 | [diff] [blame] | 1098 | } else { |
Jeff Layton | 820a803 | 2011-05-04 08:05:26 -0400 | [diff] [blame] | 1099 | if (get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP) |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1100 | + sizeof(FILE_UNIX_BASIC_INFO)) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1101 | cifs_dbg(VFS, "Open response data too small\n"); |
Cyril Gorcunov | 8f2376a | 2007-10-14 17:58:43 +0000 | [diff] [blame] | 1102 | pRetData->Type = cpu_to_le32(-1); |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1103 | goto psx_create_err; |
| 1104 | } |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 1105 | memcpy((char *) pRetData, |
Steve French | cbac3cb | 2007-04-25 11:46:06 +0000 | [diff] [blame] | 1106 | (char *)psx_rsp + sizeof(OPEN_PSX_RSP), |
Steve French | 26f5736 | 2007-08-30 22:09:15 +0000 | [diff] [blame] | 1107 | sizeof(FILE_UNIX_BASIC_INFO)); |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1108 | } |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1109 | |
| 1110 | psx_create_err: |
| 1111 | cifs_buf_release(pSMB); |
| 1112 | |
Steve French | 65bc98b | 2009-07-10 15:27:25 +0000 | [diff] [blame] | 1113 | if (posix_flags & SMB_O_DIRECTORY) |
Pavel Shilovsky | 44c5818 | 2012-05-28 14:16:31 +0400 | [diff] [blame] | 1114 | cifs_stats_inc(&tcon->stats.cifs_stats.num_posixmkdirs); |
Steve French | 65bc98b | 2009-07-10 15:27:25 +0000 | [diff] [blame] | 1115 | else |
Pavel Shilovsky | 44c5818 | 2012-05-28 14:16:31 +0400 | [diff] [blame] | 1116 | cifs_stats_inc(&tcon->stats.cifs_stats.num_posixopens); |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1117 | |
| 1118 | if (rc == -EAGAIN) |
| 1119 | goto PsxCreat; |
| 1120 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 1121 | return rc; |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1122 | } |
| 1123 | |
Steve French | a9d02ad | 2005-08-24 23:06:05 -0700 | [diff] [blame] | 1124 | static __u16 convert_disposition(int disposition) |
| 1125 | { |
| 1126 | __u16 ofun = 0; |
| 1127 | |
| 1128 | switch (disposition) { |
| 1129 | case FILE_SUPERSEDE: |
| 1130 | ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC; |
| 1131 | break; |
| 1132 | case FILE_OPEN: |
| 1133 | ofun = SMBOPEN_OAPPEND; |
| 1134 | break; |
| 1135 | case FILE_CREATE: |
| 1136 | ofun = SMBOPEN_OCREATE; |
| 1137 | break; |
| 1138 | case FILE_OPEN_IF: |
| 1139 | ofun = SMBOPEN_OCREATE | SMBOPEN_OAPPEND; |
| 1140 | break; |
| 1141 | case FILE_OVERWRITE: |
| 1142 | ofun = SMBOPEN_OTRUNC; |
| 1143 | break; |
| 1144 | case FILE_OVERWRITE_IF: |
| 1145 | ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC; |
| 1146 | break; |
| 1147 | default: |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1148 | cifs_dbg(FYI, "unknown disposition %d\n", disposition); |
Steve French | a9d02ad | 2005-08-24 23:06:05 -0700 | [diff] [blame] | 1149 | ofun = SMBOPEN_OAPPEND; /* regular open */ |
| 1150 | } |
| 1151 | return ofun; |
| 1152 | } |
| 1153 | |
Jeff Layton | 35fc37d | 2008-05-14 10:22:03 -0700 | [diff] [blame] | 1154 | static int |
| 1155 | access_flags_to_smbopen_mode(const int access_flags) |
| 1156 | { |
| 1157 | int masked_flags = access_flags & (GENERIC_READ | GENERIC_WRITE); |
| 1158 | |
| 1159 | if (masked_flags == GENERIC_READ) |
| 1160 | return SMBOPEN_READ; |
| 1161 | else if (masked_flags == GENERIC_WRITE) |
| 1162 | return SMBOPEN_WRITE; |
| 1163 | |
| 1164 | /* just go for read/write */ |
| 1165 | return SMBOPEN_READWRITE; |
| 1166 | } |
| 1167 | |
Steve French | a9d02ad | 2005-08-24 23:06:05 -0700 | [diff] [blame] | 1168 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1169 | SMBLegacyOpen(const unsigned int xid, struct cifs_tcon *tcon, |
Steve French | a9d02ad | 2005-08-24 23:06:05 -0700 | [diff] [blame] | 1170 | const char *fileName, const int openDisposition, |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 1171 | const int access_flags, const int create_options, __u16 *netfid, |
| 1172 | int *pOplock, FILE_ALL_INFO *pfile_info, |
Steve French | a9d02ad | 2005-08-24 23:06:05 -0700 | [diff] [blame] | 1173 | const struct nls_table *nls_codepage, int remap) |
| 1174 | { |
| 1175 | int rc = -EACCES; |
| 1176 | OPENX_REQ *pSMB = NULL; |
| 1177 | OPENX_RSP *pSMBr = NULL; |
| 1178 | int bytes_returned; |
| 1179 | int name_len; |
| 1180 | __u16 count; |
| 1181 | |
| 1182 | OldOpenRetry: |
| 1183 | rc = smb_init(SMB_COM_OPEN_ANDX, 15, tcon, (void **) &pSMB, |
| 1184 | (void **) &pSMBr); |
| 1185 | if (rc) |
| 1186 | return rc; |
| 1187 | |
| 1188 | pSMB->AndXCommand = 0xFF; /* none */ |
| 1189 | |
| 1190 | if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) { |
| 1191 | count = 1; /* account for one byte pad to word boundary */ |
| 1192 | name_len = |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 1193 | cifsConvertToUTF16((__le16 *) (pSMB->fileName + 1), |
| 1194 | fileName, PATH_MAX, nls_codepage, remap); |
Steve French | a9d02ad | 2005-08-24 23:06:05 -0700 | [diff] [blame] | 1195 | name_len++; /* trailing null */ |
| 1196 | name_len *= 2; |
| 1197 | } else { /* BB improve check for buffer overruns BB */ |
| 1198 | count = 0; /* no pad */ |
| 1199 | name_len = strnlen(fileName, PATH_MAX); |
| 1200 | name_len++; /* trailing null */ |
| 1201 | strncpy(pSMB->fileName, fileName, name_len); |
| 1202 | } |
| 1203 | if (*pOplock & REQ_OPLOCK) |
| 1204 | pSMB->OpenFlags = cpu_to_le16(REQ_OPLOCK); |
Steve French | 26f5736 | 2007-08-30 22:09:15 +0000 | [diff] [blame] | 1205 | else if (*pOplock & REQ_BATCHOPLOCK) |
Steve French | a9d02ad | 2005-08-24 23:06:05 -0700 | [diff] [blame] | 1206 | pSMB->OpenFlags = cpu_to_le16(REQ_BATCHOPLOCK); |
Steve French | 26f5736 | 2007-08-30 22:09:15 +0000 | [diff] [blame] | 1207 | |
Steve French | a9d02ad | 2005-08-24 23:06:05 -0700 | [diff] [blame] | 1208 | pSMB->OpenFlags |= cpu_to_le16(REQ_MORE_INFO); |
Jeff Layton | 35fc37d | 2008-05-14 10:22:03 -0700 | [diff] [blame] | 1209 | pSMB->Mode = cpu_to_le16(access_flags_to_smbopen_mode(access_flags)); |
Steve French | a9d02ad | 2005-08-24 23:06:05 -0700 | [diff] [blame] | 1210 | pSMB->Mode |= cpu_to_le16(0x40); /* deny none */ |
| 1211 | /* set file as system file if special file such |
| 1212 | as fifo and server expecting SFU style and |
| 1213 | no Unix extensions */ |
| 1214 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1215 | if (create_options & CREATE_OPTION_SPECIAL) |
| 1216 | pSMB->FileAttributes = cpu_to_le16(ATTR_SYSTEM); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 1217 | else /* BB FIXME BB */ |
| 1218 | pSMB->FileAttributes = cpu_to_le16(0/*ATTR_NORMAL*/); |
Steve French | a9d02ad | 2005-08-24 23:06:05 -0700 | [diff] [blame] | 1219 | |
Jeff Layton | 67750fb | 2008-05-09 22:28:02 +0000 | [diff] [blame] | 1220 | if (create_options & CREATE_OPTION_READONLY) |
| 1221 | pSMB->FileAttributes |= cpu_to_le16(ATTR_READONLY); |
Steve French | a9d02ad | 2005-08-24 23:06:05 -0700 | [diff] [blame] | 1222 | |
| 1223 | /* BB FIXME BB */ |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 1224 | /* pSMB->CreateOptions = cpu_to_le32(create_options & |
| 1225 | CREATE_OPTIONS_MASK); */ |
Steve French | a9d02ad | 2005-08-24 23:06:05 -0700 | [diff] [blame] | 1226 | /* BB FIXME END BB */ |
Steve French | 3e87d80 | 2005-09-18 20:49:21 -0700 | [diff] [blame] | 1227 | |
| 1228 | pSMB->Sattr = cpu_to_le16(ATTR_HIDDEN | ATTR_SYSTEM | ATTR_DIRECTORY); |
Steve French | 70ca734 | 2005-09-22 16:32:06 -0700 | [diff] [blame] | 1229 | pSMB->OpenFunction = cpu_to_le16(convert_disposition(openDisposition)); |
Steve French | a9d02ad | 2005-08-24 23:06:05 -0700 | [diff] [blame] | 1230 | count += name_len; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 1231 | inc_rfc1001_len(pSMB, count); |
Steve French | a9d02ad | 2005-08-24 23:06:05 -0700 | [diff] [blame] | 1232 | |
| 1233 | pSMB->ByteCount = cpu_to_le16(count); |
| 1234 | /* long_op set to 1 to allow for oplock break timeouts */ |
| 1235 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
Jeff Layton | 7749981 | 2011-01-11 07:24:23 -0500 | [diff] [blame] | 1236 | (struct smb_hdr *)pSMBr, &bytes_returned, 0); |
Pavel Shilovsky | 44c5818 | 2012-05-28 14:16:31 +0400 | [diff] [blame] | 1237 | cifs_stats_inc(&tcon->stats.cifs_stats.num_opens); |
Steve French | a9d02ad | 2005-08-24 23:06:05 -0700 | [diff] [blame] | 1238 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1239 | cifs_dbg(FYI, "Error in Open = %d\n", rc); |
Steve French | a9d02ad | 2005-08-24 23:06:05 -0700 | [diff] [blame] | 1240 | } else { |
| 1241 | /* BB verify if wct == 15 */ |
| 1242 | |
Steve French | 582d21e | 2008-05-13 04:54:12 +0000 | [diff] [blame] | 1243 | /* *pOplock = pSMBr->OplockLevel; */ /* BB take from action field*/ |
Steve French | a9d02ad | 2005-08-24 23:06:05 -0700 | [diff] [blame] | 1244 | |
| 1245 | *netfid = pSMBr->Fid; /* cifs fid stays in le */ |
| 1246 | /* Let caller know file was created so we can set the mode. */ |
| 1247 | /* Do we care about the CreateAction in any other cases? */ |
| 1248 | /* BB FIXME BB */ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1249 | /* if (cpu_to_le32(FILE_CREATE) == pSMBr->CreateAction) |
Steve French | a9d02ad | 2005-08-24 23:06:05 -0700 | [diff] [blame] | 1250 | *pOplock |= CIFS_CREATE_ACTION; */ |
| 1251 | /* BB FIXME END */ |
| 1252 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1253 | if (pfile_info) { |
Steve French | a9d02ad | 2005-08-24 23:06:05 -0700 | [diff] [blame] | 1254 | pfile_info->CreationTime = 0; /* BB convert CreateTime*/ |
| 1255 | pfile_info->LastAccessTime = 0; /* BB fixme */ |
| 1256 | pfile_info->LastWriteTime = 0; /* BB fixme */ |
| 1257 | pfile_info->ChangeTime = 0; /* BB fixme */ |
Steve French | 70ca734 | 2005-09-22 16:32:06 -0700 | [diff] [blame] | 1258 | pfile_info->Attributes = |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 1259 | cpu_to_le32(le16_to_cpu(pSMBr->FileAttributes)); |
Steve French | a9d02ad | 2005-08-24 23:06:05 -0700 | [diff] [blame] | 1260 | /* the file_info buf is endian converted by caller */ |
Steve French | 70ca734 | 2005-09-22 16:32:06 -0700 | [diff] [blame] | 1261 | pfile_info->AllocationSize = |
| 1262 | cpu_to_le64(le32_to_cpu(pSMBr->EndOfFile)); |
| 1263 | pfile_info->EndOfFile = pfile_info->AllocationSize; |
Steve French | a9d02ad | 2005-08-24 23:06:05 -0700 | [diff] [blame] | 1264 | pfile_info->NumberOfLinks = cpu_to_le32(1); |
Jeff Layton | 9a8165f | 2008-10-17 21:03:20 -0400 | [diff] [blame] | 1265 | pfile_info->DeletePending = 0; |
Steve French | a9d02ad | 2005-08-24 23:06:05 -0700 | [diff] [blame] | 1266 | } |
| 1267 | } |
| 1268 | |
| 1269 | cifs_buf_release(pSMB); |
| 1270 | if (rc == -EAGAIN) |
| 1271 | goto OldOpenRetry; |
| 1272 | return rc; |
| 1273 | } |
| 1274 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | int |
Pavel Shilovsky | d81b8a4 | 2014-01-16 15:53:36 +0400 | [diff] [blame] | 1276 | CIFS_open(const unsigned int xid, struct cifs_open_parms *oparms, int *oplock, |
| 1277 | FILE_ALL_INFO *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1278 | { |
| 1279 | int rc = -EACCES; |
Pavel Shilovsky | 9bf4fa0 | 2014-01-16 15:53:33 +0400 | [diff] [blame] | 1280 | OPEN_REQ *req = NULL; |
| 1281 | OPEN_RSP *rsp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1282 | int bytes_returned; |
| 1283 | int name_len; |
| 1284 | __u16 count; |
Pavel Shilovsky | d81b8a4 | 2014-01-16 15:53:36 +0400 | [diff] [blame] | 1285 | struct cifs_sb_info *cifs_sb = oparms->cifs_sb; |
| 1286 | struct cifs_tcon *tcon = oparms->tcon; |
| 1287 | int remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR; |
| 1288 | const struct nls_table *nls = cifs_sb->local_nls; |
| 1289 | int create_options = oparms->create_options; |
| 1290 | int desired_access = oparms->desired_access; |
| 1291 | int disposition = oparms->disposition; |
| 1292 | const char *path = oparms->path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1293 | |
| 1294 | openRetry: |
Pavel Shilovsky | 9bf4fa0 | 2014-01-16 15:53:33 +0400 | [diff] [blame] | 1295 | rc = smb_init(SMB_COM_NT_CREATE_ANDX, 24, tcon, (void **)&req, |
| 1296 | (void **)&rsp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1297 | if (rc) |
| 1298 | return rc; |
| 1299 | |
Pavel Shilovsky | 9bf4fa0 | 2014-01-16 15:53:33 +0400 | [diff] [blame] | 1300 | /* no commands go after this */ |
| 1301 | req->AndXCommand = 0xFF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | |
Pavel Shilovsky | 9bf4fa0 | 2014-01-16 15:53:33 +0400 | [diff] [blame] | 1303 | if (req->hdr.Flags2 & SMBFLG2_UNICODE) { |
| 1304 | /* account for one byte pad to word boundary */ |
| 1305 | count = 1; |
| 1306 | name_len = cifsConvertToUTF16((__le16 *)(req->fileName + 1), |
| 1307 | path, PATH_MAX, nls, remap); |
| 1308 | /* trailing null */ |
| 1309 | name_len++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1310 | name_len *= 2; |
Pavel Shilovsky | 9bf4fa0 | 2014-01-16 15:53:33 +0400 | [diff] [blame] | 1311 | req->NameLength = cpu_to_le16(name_len); |
| 1312 | } else { |
| 1313 | /* BB improve check for buffer overruns BB */ |
| 1314 | /* no pad */ |
| 1315 | count = 0; |
| 1316 | name_len = strnlen(path, PATH_MAX); |
| 1317 | /* trailing null */ |
| 1318 | name_len++; |
| 1319 | req->NameLength = cpu_to_le16(name_len); |
| 1320 | strncpy(req->fileName, path, name_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1321 | } |
Jeff Layton | 67750fb | 2008-05-09 22:28:02 +0000 | [diff] [blame] | 1322 | |
Pavel Shilovsky | 9bf4fa0 | 2014-01-16 15:53:33 +0400 | [diff] [blame] | 1323 | if (*oplock & REQ_OPLOCK) |
| 1324 | req->OpenFlags = cpu_to_le32(REQ_OPLOCK); |
| 1325 | else if (*oplock & REQ_BATCHOPLOCK) |
| 1326 | req->OpenFlags = cpu_to_le32(REQ_BATCHOPLOCK); |
| 1327 | |
| 1328 | req->DesiredAccess = cpu_to_le32(desired_access); |
| 1329 | req->AllocationSize = 0; |
| 1330 | |
| 1331 | /* |
| 1332 | * Set file as system file if special file such as fifo and server |
| 1333 | * expecting SFU style and no Unix extensions. |
| 1334 | */ |
| 1335 | if (create_options & CREATE_OPTION_SPECIAL) |
| 1336 | req->FileAttributes = cpu_to_le32(ATTR_SYSTEM); |
| 1337 | else |
| 1338 | req->FileAttributes = cpu_to_le32(ATTR_NORMAL); |
| 1339 | |
| 1340 | /* |
| 1341 | * XP does not handle ATTR_POSIX_SEMANTICS but it helps speed up case |
| 1342 | * sensitive checks for other servers such as Samba. |
| 1343 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1344 | if (tcon->ses->capabilities & CAP_UNIX) |
Pavel Shilovsky | 9bf4fa0 | 2014-01-16 15:53:33 +0400 | [diff] [blame] | 1345 | req->FileAttributes |= cpu_to_le32(ATTR_POSIX_SEMANTICS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | |
Jeff Layton | 67750fb | 2008-05-09 22:28:02 +0000 | [diff] [blame] | 1347 | if (create_options & CREATE_OPTION_READONLY) |
Pavel Shilovsky | 9bf4fa0 | 2014-01-16 15:53:33 +0400 | [diff] [blame] | 1348 | req->FileAttributes |= cpu_to_le32(ATTR_READONLY); |
Jeff Layton | 67750fb | 2008-05-09 22:28:02 +0000 | [diff] [blame] | 1349 | |
Pavel Shilovsky | 9bf4fa0 | 2014-01-16 15:53:33 +0400 | [diff] [blame] | 1350 | req->ShareAccess = cpu_to_le32(FILE_SHARE_ALL); |
| 1351 | req->CreateDisposition = cpu_to_le32(disposition); |
| 1352 | req->CreateOptions = cpu_to_le32(create_options & CREATE_OPTIONS_MASK); |
| 1353 | |
Steve French | 09d1db5 | 2005-04-28 22:41:08 -0700 | [diff] [blame] | 1354 | /* BB Expirement with various impersonation levels and verify */ |
Pavel Shilovsky | 9bf4fa0 | 2014-01-16 15:53:33 +0400 | [diff] [blame] | 1355 | req->ImpersonationLevel = cpu_to_le32(SECURITY_IMPERSONATION); |
| 1356 | req->SecurityFlags = SECURITY_CONTEXT_TRACKING|SECURITY_EFFECTIVE_ONLY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1357 | |
| 1358 | count += name_len; |
Pavel Shilovsky | 9bf4fa0 | 2014-01-16 15:53:33 +0400 | [diff] [blame] | 1359 | inc_rfc1001_len(req, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | |
Pavel Shilovsky | 9bf4fa0 | 2014-01-16 15:53:33 +0400 | [diff] [blame] | 1361 | req->ByteCount = cpu_to_le16(count); |
| 1362 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *)req, |
| 1363 | (struct smb_hdr *)rsp, &bytes_returned, 0); |
Pavel Shilovsky | 44c5818 | 2012-05-28 14:16:31 +0400 | [diff] [blame] | 1364 | cifs_stats_inc(&tcon->stats.cifs_stats.num_opens); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1366 | cifs_dbg(FYI, "Error in Open = %d\n", rc); |
Pavel Shilovsky | 9bf4fa0 | 2014-01-16 15:53:33 +0400 | [diff] [blame] | 1367 | cifs_buf_release(req); |
| 1368 | if (rc == -EAGAIN) |
| 1369 | goto openRetry; |
| 1370 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | } |
Steve French | a5a2b48 | 2005-08-20 21:42:53 -0700 | [diff] [blame] | 1372 | |
Pavel Shilovsky | 9bf4fa0 | 2014-01-16 15:53:33 +0400 | [diff] [blame] | 1373 | /* 1 byte no need to le_to_cpu */ |
| 1374 | *oplock = rsp->OplockLevel; |
| 1375 | /* cifs fid stays in le */ |
Pavel Shilovsky | d81b8a4 | 2014-01-16 15:53:36 +0400 | [diff] [blame] | 1376 | oparms->fid->netfid = rsp->Fid; |
Pavel Shilovsky | 9bf4fa0 | 2014-01-16 15:53:33 +0400 | [diff] [blame] | 1377 | |
| 1378 | /* Let caller know file was created so we can set the mode. */ |
| 1379 | /* Do we care about the CreateAction in any other cases? */ |
| 1380 | if (cpu_to_le32(FILE_CREATE) == rsp->CreateAction) |
| 1381 | *oplock |= CIFS_CREATE_ACTION; |
| 1382 | |
| 1383 | if (buf) { |
| 1384 | /* copy from CreationTime to Attributes */ |
| 1385 | memcpy((char *)buf, (char *)&rsp->CreationTime, 36); |
| 1386 | /* the file_info buf is endian converted by caller */ |
| 1387 | buf->AllocationSize = rsp->AllocationSize; |
| 1388 | buf->EndOfFile = rsp->EndOfFile; |
| 1389 | buf->NumberOfLinks = cpu_to_le32(1); |
| 1390 | buf->DeletePending = 0; |
| 1391 | } |
| 1392 | |
| 1393 | cifs_buf_release(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1394 | return rc; |
| 1395 | } |
| 1396 | |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1397 | /* |
| 1398 | * Discard any remaining data in the current SMB. To do this, we borrow the |
| 1399 | * current bigbuf. |
| 1400 | */ |
| 1401 | static int |
| 1402 | cifs_readv_discard(struct TCP_Server_Info *server, struct mid_q_entry *mid) |
| 1403 | { |
Pavel Shilovsky | 5ffef7b | 2012-03-23 14:28:03 -0400 | [diff] [blame] | 1404 | unsigned int rfclen = get_rfc1002_length(server->smallbuf); |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1405 | int remaining = rfclen + 4 - server->total_read; |
| 1406 | struct cifs_readdata *rdata = mid->callback_data; |
| 1407 | |
| 1408 | while (remaining > 0) { |
| 1409 | int length; |
| 1410 | |
| 1411 | length = cifs_read_from_socket(server, server->bigbuf, |
| 1412 | min_t(unsigned int, remaining, |
Pavel Shilovsky | 1887f60 | 2012-05-17 12:45:31 +0400 | [diff] [blame] | 1413 | CIFSMaxBufSize + MAX_HEADER_SIZE(server))); |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1414 | if (length < 0) |
| 1415 | return length; |
| 1416 | server->total_read += length; |
| 1417 | remaining -= length; |
| 1418 | } |
| 1419 | |
| 1420 | dequeue_mid(mid, rdata->result); |
| 1421 | return 0; |
| 1422 | } |
| 1423 | |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 1424 | int |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1425 | cifs_readv_receive(struct TCP_Server_Info *server, struct mid_q_entry *mid) |
| 1426 | { |
| 1427 | int length, len; |
Jeff Layton | 8d5ce4d | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 1428 | unsigned int data_offset, data_len; |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1429 | struct cifs_readdata *rdata = mid->callback_data; |
Pavel Shilovsky | 5ffef7b | 2012-03-23 14:28:03 -0400 | [diff] [blame] | 1430 | char *buf = server->smallbuf; |
| 1431 | unsigned int buflen = get_rfc1002_length(buf) + 4; |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1432 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1433 | cifs_dbg(FYI, "%s: mid=%llu offset=%llu bytes=%u\n", |
| 1434 | __func__, mid->mid, rdata->offset, rdata->bytes); |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1435 | |
| 1436 | /* |
| 1437 | * read the rest of READ_RSP header (sans Data array), or whatever we |
| 1438 | * can if there's not enough data. At this point, we've read down to |
| 1439 | * the Mid. |
| 1440 | */ |
Pavel Shilovsky | eb37871 | 2012-05-17 13:02:51 +0400 | [diff] [blame] | 1441 | len = min_t(unsigned int, buflen, server->vals->read_rsp_size) - |
Pavel Shilovsky | 1887f60 | 2012-05-17 12:45:31 +0400 | [diff] [blame] | 1442 | HEADER_SIZE(server) + 1; |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1443 | |
Jeff Layton | 5819575 | 2012-09-19 06:22:34 -0700 | [diff] [blame] | 1444 | rdata->iov.iov_base = buf + HEADER_SIZE(server) - 1; |
| 1445 | rdata->iov.iov_len = len; |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1446 | |
Jeff Layton | 5819575 | 2012-09-19 06:22:34 -0700 | [diff] [blame] | 1447 | length = cifs_readv_from_socket(server, &rdata->iov, 1, len); |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1448 | if (length < 0) |
| 1449 | return length; |
| 1450 | server->total_read += length; |
| 1451 | |
| 1452 | /* Was the SMB read successful? */ |
Pavel Shilovsky | eb37871 | 2012-05-17 13:02:51 +0400 | [diff] [blame] | 1453 | rdata->result = server->ops->map_error(buf, false); |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1454 | if (rdata->result != 0) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1455 | cifs_dbg(FYI, "%s: server returned error %d\n", |
| 1456 | __func__, rdata->result); |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1457 | return cifs_readv_discard(server, mid); |
| 1458 | } |
| 1459 | |
| 1460 | /* Is there enough to get to the rest of the READ_RSP header? */ |
Pavel Shilovsky | eb37871 | 2012-05-17 13:02:51 +0400 | [diff] [blame] | 1461 | if (server->total_read < server->vals->read_rsp_size) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1462 | cifs_dbg(FYI, "%s: server returned short header. got=%u expected=%zu\n", |
| 1463 | __func__, server->total_read, |
| 1464 | server->vals->read_rsp_size); |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1465 | rdata->result = -EIO; |
| 1466 | return cifs_readv_discard(server, mid); |
| 1467 | } |
| 1468 | |
Pavel Shilovsky | eb37871 | 2012-05-17 13:02:51 +0400 | [diff] [blame] | 1469 | data_offset = server->ops->read_data_offset(buf) + 4; |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1470 | if (data_offset < server->total_read) { |
| 1471 | /* |
| 1472 | * win2k8 sometimes sends an offset of 0 when the read |
| 1473 | * is beyond the EOF. Treat it as if the data starts just after |
| 1474 | * the header. |
| 1475 | */ |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1476 | cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n", |
| 1477 | __func__, data_offset); |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1478 | data_offset = server->total_read; |
| 1479 | } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) { |
| 1480 | /* data_offset is beyond the end of smallbuf */ |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1481 | cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n", |
| 1482 | __func__, data_offset); |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1483 | rdata->result = -EIO; |
| 1484 | return cifs_readv_discard(server, mid); |
| 1485 | } |
| 1486 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1487 | cifs_dbg(FYI, "%s: total_read=%u data_offset=%u\n", |
| 1488 | __func__, server->total_read, data_offset); |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1489 | |
| 1490 | len = data_offset - server->total_read; |
| 1491 | if (len > 0) { |
| 1492 | /* read any junk before data into the rest of smallbuf */ |
Jeff Layton | 5819575 | 2012-09-19 06:22:34 -0700 | [diff] [blame] | 1493 | rdata->iov.iov_base = buf + server->total_read; |
| 1494 | rdata->iov.iov_len = len; |
| 1495 | length = cifs_readv_from_socket(server, &rdata->iov, 1, len); |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1496 | if (length < 0) |
| 1497 | return length; |
| 1498 | server->total_read += length; |
| 1499 | } |
| 1500 | |
| 1501 | /* set up first iov for signature check */ |
Jeff Layton | 5819575 | 2012-09-19 06:22:34 -0700 | [diff] [blame] | 1502 | rdata->iov.iov_base = buf; |
| 1503 | rdata->iov.iov_len = server->total_read; |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1504 | cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n", |
| 1505 | rdata->iov.iov_base, rdata->iov.iov_len); |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1506 | |
| 1507 | /* how much data is in the response? */ |
Pavel Shilovsky | eb37871 | 2012-05-17 13:02:51 +0400 | [diff] [blame] | 1508 | data_len = server->ops->read_data_length(buf); |
Pavel Shilovsky | 5ffef7b | 2012-03-23 14:28:03 -0400 | [diff] [blame] | 1509 | if (data_offset + data_len > buflen) { |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1510 | /* data_len is corrupt -- discard frame */ |
| 1511 | rdata->result = -EIO; |
| 1512 | return cifs_readv_discard(server, mid); |
| 1513 | } |
| 1514 | |
Jeff Layton | 8321fec | 2012-09-19 06:22:32 -0700 | [diff] [blame] | 1515 | length = rdata->read_into_pages(server, rdata, data_len); |
| 1516 | if (length < 0) |
| 1517 | return length; |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1518 | |
Jeff Layton | 8321fec | 2012-09-19 06:22:32 -0700 | [diff] [blame] | 1519 | server->total_read += length; |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1520 | rdata->bytes = length; |
| 1521 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1522 | cifs_dbg(FYI, "total_read=%u buflen=%u remaining=%u\n", |
| 1523 | server->total_read, buflen, data_len); |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1524 | |
| 1525 | /* discard anything left over */ |
Pavel Shilovsky | 5ffef7b | 2012-03-23 14:28:03 -0400 | [diff] [blame] | 1526 | if (server->total_read < buflen) |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1527 | return cifs_readv_discard(server, mid); |
| 1528 | |
| 1529 | dequeue_mid(mid, false); |
| 1530 | return length; |
| 1531 | } |
| 1532 | |
| 1533 | static void |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1534 | cifs_readv_callback(struct mid_q_entry *mid) |
| 1535 | { |
| 1536 | struct cifs_readdata *rdata = mid->callback_data; |
| 1537 | struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink); |
| 1538 | struct TCP_Server_Info *server = tcon->ses->server; |
Jeff Layton | 5819575 | 2012-09-19 06:22:34 -0700 | [diff] [blame] | 1539 | struct smb_rqst rqst = { .rq_iov = &rdata->iov, |
| 1540 | .rq_nvec = 1, |
Jeff Layton | 8321fec | 2012-09-19 06:22:32 -0700 | [diff] [blame] | 1541 | .rq_pages = rdata->pages, |
| 1542 | .rq_npages = rdata->nr_pages, |
| 1543 | .rq_pagesz = rdata->pagesz, |
| 1544 | .rq_tailsz = rdata->tailsz }; |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1545 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1546 | cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n", |
| 1547 | __func__, mid->mid, mid->mid_state, rdata->result, |
| 1548 | rdata->bytes); |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1549 | |
Pavel Shilovsky | 7c9421e | 2012-03-23 14:28:03 -0400 | [diff] [blame] | 1550 | switch (mid->mid_state) { |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1551 | case MID_RESPONSE_RECEIVED: |
| 1552 | /* result already set, check signature */ |
Jeff Layton | 38d77c5 | 2013-05-26 07:01:00 -0400 | [diff] [blame] | 1553 | if (server->sign) { |
Steve French | 985e4ff0 | 2012-08-03 09:42:45 -0500 | [diff] [blame] | 1554 | int rc = 0; |
| 1555 | |
Jeff Layton | bf5ea0e | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 1556 | rc = cifs_verify_signature(&rqst, server, |
Jeff Layton | 0124cc4 | 2013-04-03 11:55:03 -0400 | [diff] [blame] | 1557 | mid->sequence_number); |
Steve French | 985e4ff0 | 2012-08-03 09:42:45 -0500 | [diff] [blame] | 1558 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1559 | cifs_dbg(VFS, "SMB signature verification returned error = %d\n", |
| 1560 | rc); |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1561 | } |
| 1562 | /* FIXME: should this be counted toward the initiating task? */ |
| 1563 | task_io_account_read(rdata->bytes); |
| 1564 | cifs_stats_bytes_read(tcon, rdata->bytes); |
| 1565 | break; |
| 1566 | case MID_REQUEST_SUBMITTED: |
| 1567 | case MID_RETRY_NEEDED: |
| 1568 | rdata->result = -EAGAIN; |
| 1569 | break; |
| 1570 | default: |
| 1571 | rdata->result = -EIO; |
| 1572 | } |
| 1573 | |
Jeff Layton | da472fc | 2012-03-23 14:40:53 -0400 | [diff] [blame] | 1574 | queue_work(cifsiod_wq, &rdata->work); |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1575 | DeleteMidQEntry(mid); |
Pavel Shilovsky | a891f0f | 2012-05-23 16:14:34 +0400 | [diff] [blame] | 1576 | add_credits(server, 1, 0); |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1577 | } |
| 1578 | |
| 1579 | /* cifs_async_readv - send an async write, and set up mid to handle result */ |
| 1580 | int |
| 1581 | cifs_async_readv(struct cifs_readdata *rdata) |
| 1582 | { |
| 1583 | int rc; |
| 1584 | READ_REQ *smb = NULL; |
| 1585 | int wct; |
| 1586 | struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink); |
Jeff Layton | 5819575 | 2012-09-19 06:22:34 -0700 | [diff] [blame] | 1587 | struct smb_rqst rqst = { .rq_iov = &rdata->iov, |
Jeff Layton | fec344e | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 1588 | .rq_nvec = 1 }; |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1589 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1590 | cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n", |
| 1591 | __func__, rdata->offset, rdata->bytes); |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1592 | |
| 1593 | if (tcon->ses->capabilities & CAP_LARGE_FILES) |
| 1594 | wct = 12; |
| 1595 | else { |
| 1596 | wct = 10; /* old style read */ |
| 1597 | if ((rdata->offset >> 32) > 0) { |
| 1598 | /* can not handle this big offset for old */ |
| 1599 | return -EIO; |
| 1600 | } |
| 1601 | } |
| 1602 | |
| 1603 | rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **)&smb); |
| 1604 | if (rc) |
| 1605 | return rc; |
| 1606 | |
| 1607 | smb->hdr.Pid = cpu_to_le16((__u16)rdata->pid); |
| 1608 | smb->hdr.PidHigh = cpu_to_le16((__u16)(rdata->pid >> 16)); |
| 1609 | |
| 1610 | smb->AndXCommand = 0xFF; /* none */ |
Pavel Shilovsky | 4b4de76 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 1611 | smb->Fid = rdata->cfile->fid.netfid; |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1612 | smb->OffsetLow = cpu_to_le32(rdata->offset & 0xFFFFFFFF); |
| 1613 | if (wct == 12) |
| 1614 | smb->OffsetHigh = cpu_to_le32(rdata->offset >> 32); |
| 1615 | smb->Remaining = 0; |
| 1616 | smb->MaxCount = cpu_to_le16(rdata->bytes & 0xFFFF); |
| 1617 | smb->MaxCountHigh = cpu_to_le32(rdata->bytes >> 16); |
| 1618 | if (wct == 12) |
| 1619 | smb->ByteCount = 0; |
| 1620 | else { |
| 1621 | /* old style read */ |
| 1622 | struct smb_com_readx_req *smbr = |
| 1623 | (struct smb_com_readx_req *)smb; |
| 1624 | smbr->ByteCount = 0; |
| 1625 | } |
| 1626 | |
| 1627 | /* 4 for RFC1001 length + 1 for BCC */ |
Jeff Layton | 5819575 | 2012-09-19 06:22:34 -0700 | [diff] [blame] | 1628 | rdata->iov.iov_base = smb; |
| 1629 | rdata->iov.iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4; |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1630 | |
Jeff Layton | 6993f74 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 1631 | kref_get(&rdata->refcount); |
Jeff Layton | fec344e | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 1632 | rc = cifs_call_async(tcon->ses->server, &rqst, cifs_readv_receive, |
| 1633 | cifs_readv_callback, rdata, 0); |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1634 | |
| 1635 | if (rc == 0) |
Pavel Shilovsky | 44c5818 | 2012-05-28 14:16:31 +0400 | [diff] [blame] | 1636 | cifs_stats_inc(&tcon->stats.cifs_stats.num_reads); |
Jeff Layton | 6993f74 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 1637 | else |
| 1638 | kref_put(&rdata->refcount, cifs_readdata_release); |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1639 | |
| 1640 | cifs_small_buf_release(smb); |
| 1641 | return rc; |
| 1642 | } |
| 1643 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1644 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1645 | CIFSSMBRead(const unsigned int xid, struct cifs_io_parms *io_parms, |
| 1646 | unsigned int *nbytes, char **buf, int *pbuf_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1647 | { |
| 1648 | int rc = -EACCES; |
| 1649 | READ_REQ *pSMB = NULL; |
| 1650 | READ_RSP *pSMBr = NULL; |
| 1651 | char *pReadData = NULL; |
Steve French | bfa0d75 | 2005-08-31 21:50:37 -0700 | [diff] [blame] | 1652 | int wct; |
Steve French | ec637e3 | 2005-12-12 20:53:18 -0800 | [diff] [blame] | 1653 | int resp_buf_type = 0; |
| 1654 | struct kvec iov[1]; |
Pavel Shilovsky | d4ffff1 | 2011-05-26 06:02:00 +0000 | [diff] [blame] | 1655 | __u32 pid = io_parms->pid; |
| 1656 | __u16 netfid = io_parms->netfid; |
| 1657 | __u64 offset = io_parms->offset; |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 1658 | struct cifs_tcon *tcon = io_parms->tcon; |
Pavel Shilovsky | d4ffff1 | 2011-05-26 06:02:00 +0000 | [diff] [blame] | 1659 | unsigned int count = io_parms->length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1660 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1661 | cifs_dbg(FYI, "Reading %d bytes on fid %d\n", count, netfid); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1662 | if (tcon->ses->capabilities & CAP_LARGE_FILES) |
Steve French | bfa0d75 | 2005-08-31 21:50:37 -0700 | [diff] [blame] | 1663 | wct = 12; |
Steve French | 4c3130e | 2008-12-09 00:28:16 +0000 | [diff] [blame] | 1664 | else { |
Steve French | bfa0d75 | 2005-08-31 21:50:37 -0700 | [diff] [blame] | 1665 | wct = 10; /* old style read */ |
Pavel Shilovsky | d4ffff1 | 2011-05-26 06:02:00 +0000 | [diff] [blame] | 1666 | if ((offset >> 32) > 0) { |
Steve French | 4c3130e | 2008-12-09 00:28:16 +0000 | [diff] [blame] | 1667 | /* can not handle this big offset for old */ |
| 1668 | return -EIO; |
| 1669 | } |
| 1670 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1671 | |
| 1672 | *nbytes = 0; |
Steve French | ec637e3 | 2005-12-12 20:53:18 -0800 | [diff] [blame] | 1673 | rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **) &pSMB); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1674 | if (rc) |
| 1675 | return rc; |
| 1676 | |
Pavel Shilovsky | d4ffff1 | 2011-05-26 06:02:00 +0000 | [diff] [blame] | 1677 | pSMB->hdr.Pid = cpu_to_le16((__u16)pid); |
| 1678 | pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16)); |
| 1679 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1680 | /* tcon and ses pointer are checked in smb_init */ |
| 1681 | if (tcon->ses->server == NULL) |
| 1682 | return -ECONNABORTED; |
| 1683 | |
Steve French | ec637e3 | 2005-12-12 20:53:18 -0800 | [diff] [blame] | 1684 | pSMB->AndXCommand = 0xFF; /* none */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1685 | pSMB->Fid = netfid; |
Pavel Shilovsky | d4ffff1 | 2011-05-26 06:02:00 +0000 | [diff] [blame] | 1686 | pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1687 | if (wct == 12) |
Pavel Shilovsky | d4ffff1 | 2011-05-26 06:02:00 +0000 | [diff] [blame] | 1688 | pSMB->OffsetHigh = cpu_to_le32(offset >> 32); |
Steve French | bfa0d75 | 2005-08-31 21:50:37 -0700 | [diff] [blame] | 1689 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1690 | pSMB->Remaining = 0; |
| 1691 | pSMB->MaxCount = cpu_to_le16(count & 0xFFFF); |
| 1692 | pSMB->MaxCountHigh = cpu_to_le32(count >> 16); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1693 | if (wct == 12) |
Steve French | bfa0d75 | 2005-08-31 21:50:37 -0700 | [diff] [blame] | 1694 | pSMB->ByteCount = 0; /* no need to do le conversion since 0 */ |
| 1695 | else { |
| 1696 | /* old style read */ |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 1697 | struct smb_com_readx_req *pSMBW = |
Steve French | bfa0d75 | 2005-08-31 21:50:37 -0700 | [diff] [blame] | 1698 | (struct smb_com_readx_req *)pSMB; |
Steve French | ec637e3 | 2005-12-12 20:53:18 -0800 | [diff] [blame] | 1699 | pSMBW->ByteCount = 0; |
Steve French | bfa0d75 | 2005-08-31 21:50:37 -0700 | [diff] [blame] | 1700 | } |
Steve French | ec637e3 | 2005-12-12 20:53:18 -0800 | [diff] [blame] | 1701 | |
| 1702 | iov[0].iov_base = (char *)pSMB; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 1703 | iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4; |
Steve French | a761ac5 | 2007-10-18 21:45:27 +0000 | [diff] [blame] | 1704 | rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */, |
Jeff Layton | 7749981 | 2011-01-11 07:24:23 -0500 | [diff] [blame] | 1705 | &resp_buf_type, CIFS_LOG_ERROR); |
Pavel Shilovsky | 44c5818 | 2012-05-28 14:16:31 +0400 | [diff] [blame] | 1706 | cifs_stats_inc(&tcon->stats.cifs_stats.num_reads); |
Steve French | ec637e3 | 2005-12-12 20:53:18 -0800 | [diff] [blame] | 1707 | pSMBr = (READ_RSP *)iov[0].iov_base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1708 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1709 | cifs_dbg(VFS, "Send error in read = %d\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1710 | } else { |
| 1711 | int data_length = le16_to_cpu(pSMBr->DataLengthHigh); |
| 1712 | data_length = data_length << 16; |
| 1713 | data_length += le16_to_cpu(pSMBr->DataLength); |
| 1714 | *nbytes = data_length; |
| 1715 | |
| 1716 | /*check that DataLength would not go beyond end of SMB */ |
Steve French | ec637e3 | 2005-12-12 20:53:18 -0800 | [diff] [blame] | 1717 | if ((data_length > CIFSMaxBufSize) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1718 | || (data_length > count)) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1719 | cifs_dbg(FYI, "bad length %d for count %d\n", |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 1720 | data_length, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1721 | rc = -EIO; |
| 1722 | *nbytes = 0; |
| 1723 | } else { |
Steve French | ec637e3 | 2005-12-12 20:53:18 -0800 | [diff] [blame] | 1724 | pReadData = (char *) (&pSMBr->hdr.Protocol) + |
Steve French | 26f5736 | 2007-08-30 22:09:15 +0000 | [diff] [blame] | 1725 | le16_to_cpu(pSMBr->DataOffset); |
| 1726 | /* if (rc = copy_to_user(buf, pReadData, data_length)) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1727 | cifs_dbg(VFS, "Faulting on read rc = %d\n",rc); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 1728 | rc = -EFAULT; |
Steve French | 26f5736 | 2007-08-30 22:09:15 +0000 | [diff] [blame] | 1729 | }*/ /* can not use copy_to_user when using page cache*/ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1730 | if (*buf) |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 1731 | memcpy(*buf, pReadData, data_length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1732 | } |
| 1733 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1734 | |
Steve French | 4b8f930 | 2006-02-26 16:41:18 +0000 | [diff] [blame] | 1735 | /* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1736 | if (*buf) { |
| 1737 | if (resp_buf_type == CIFS_SMALL_BUFFER) |
Steve French | ec637e3 | 2005-12-12 20:53:18 -0800 | [diff] [blame] | 1738 | cifs_small_buf_release(iov[0].iov_base); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1739 | else if (resp_buf_type == CIFS_LARGE_BUFFER) |
Steve French | ec637e3 | 2005-12-12 20:53:18 -0800 | [diff] [blame] | 1740 | cifs_buf_release(iov[0].iov_base); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1741 | } else if (resp_buf_type != CIFS_NO_BUFFER) { |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 1742 | /* return buffer to caller to free */ |
| 1743 | *buf = iov[0].iov_base; |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1744 | if (resp_buf_type == CIFS_SMALL_BUFFER) |
Steve French | ec637e3 | 2005-12-12 20:53:18 -0800 | [diff] [blame] | 1745 | *pbuf_type = CIFS_SMALL_BUFFER; |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1746 | else if (resp_buf_type == CIFS_LARGE_BUFFER) |
Steve French | ec637e3 | 2005-12-12 20:53:18 -0800 | [diff] [blame] | 1747 | *pbuf_type = CIFS_LARGE_BUFFER; |
Steve French | 6cec2ae | 2006-02-22 17:31:52 -0600 | [diff] [blame] | 1748 | } /* else no valid buffer on return - leave as null */ |
Steve French | ec637e3 | 2005-12-12 20:53:18 -0800 | [diff] [blame] | 1749 | |
| 1750 | /* Note: On -EAGAIN error only caller can retry on handle based calls |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1751 | since file handle passed in no longer valid */ |
| 1752 | return rc; |
| 1753 | } |
| 1754 | |
Steve French | ec637e3 | 2005-12-12 20:53:18 -0800 | [diff] [blame] | 1755 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1756 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1757 | CIFSSMBWrite(const unsigned int xid, struct cifs_io_parms *io_parms, |
Pavel Shilovsky | fa2989f | 2011-05-26 10:01:59 +0400 | [diff] [blame] | 1758 | unsigned int *nbytes, const char *buf, |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 1759 | const char __user *ubuf, const int long_op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1760 | { |
| 1761 | int rc = -EACCES; |
| 1762 | WRITE_REQ *pSMB = NULL; |
| 1763 | WRITE_RSP *pSMBr = NULL; |
Steve French | 1c95518 | 2005-08-30 20:58:07 -0700 | [diff] [blame] | 1764 | int bytes_returned, wct; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1765 | __u32 bytes_sent; |
| 1766 | __u16 byte_count; |
Pavel Shilovsky | fa2989f | 2011-05-26 10:01:59 +0400 | [diff] [blame] | 1767 | __u32 pid = io_parms->pid; |
| 1768 | __u16 netfid = io_parms->netfid; |
| 1769 | __u64 offset = io_parms->offset; |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 1770 | struct cifs_tcon *tcon = io_parms->tcon; |
Pavel Shilovsky | fa2989f | 2011-05-26 10:01:59 +0400 | [diff] [blame] | 1771 | unsigned int count = io_parms->length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1772 | |
Steve French | a24e2d7 | 2010-04-03 17:20:21 +0000 | [diff] [blame] | 1773 | *nbytes = 0; |
| 1774 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1775 | /* cifs_dbg(FYI, "write at %lld %d bytes\n", offset, count);*/ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1776 | if (tcon->ses == NULL) |
Steve French | 1c95518 | 2005-08-30 20:58:07 -0700 | [diff] [blame] | 1777 | return -ECONNABORTED; |
| 1778 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1779 | if (tcon->ses->capabilities & CAP_LARGE_FILES) |
Steve French | 1c95518 | 2005-08-30 20:58:07 -0700 | [diff] [blame] | 1780 | wct = 14; |
Steve French | 4c3130e | 2008-12-09 00:28:16 +0000 | [diff] [blame] | 1781 | else { |
Steve French | 1c95518 | 2005-08-30 20:58:07 -0700 | [diff] [blame] | 1782 | wct = 12; |
Steve French | 4c3130e | 2008-12-09 00:28:16 +0000 | [diff] [blame] | 1783 | if ((offset >> 32) > 0) { |
| 1784 | /* can not handle big offset for old srv */ |
| 1785 | return -EIO; |
| 1786 | } |
| 1787 | } |
Steve French | 1c95518 | 2005-08-30 20:58:07 -0700 | [diff] [blame] | 1788 | |
| 1789 | rc = smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1790 | (void **) &pSMBr); |
| 1791 | if (rc) |
| 1792 | return rc; |
Pavel Shilovsky | fa2989f | 2011-05-26 10:01:59 +0400 | [diff] [blame] | 1793 | |
| 1794 | pSMB->hdr.Pid = cpu_to_le16((__u16)pid); |
| 1795 | pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16)); |
| 1796 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1797 | /* tcon and ses pointer are checked in smb_init */ |
| 1798 | if (tcon->ses->server == NULL) |
| 1799 | return -ECONNABORTED; |
| 1800 | |
| 1801 | pSMB->AndXCommand = 0xFF; /* none */ |
| 1802 | pSMB->Fid = netfid; |
| 1803 | pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1804 | if (wct == 14) |
Steve French | 1c95518 | 2005-08-30 20:58:07 -0700 | [diff] [blame] | 1805 | pSMB->OffsetHigh = cpu_to_le32(offset >> 32); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 1806 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1807 | pSMB->Reserved = 0xFFFFFFFF; |
| 1808 | pSMB->WriteMode = 0; |
| 1809 | pSMB->Remaining = 0; |
| 1810 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 1811 | /* Can increase buffer size if buffer is big enough in some cases ie we |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1812 | can send more if LARGE_WRITE_X capability returned by the server and if |
| 1813 | our buffer is big enough or if we convert to iovecs on socket writes |
| 1814 | and eliminate the copy to the CIFS buffer */ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1815 | if (tcon->ses->capabilities & CAP_LARGE_WRITE_X) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1816 | bytes_sent = min_t(const unsigned int, CIFSMaxBufSize, count); |
| 1817 | } else { |
| 1818 | bytes_sent = (tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE) |
| 1819 | & ~0xFF; |
| 1820 | } |
| 1821 | |
| 1822 | if (bytes_sent > count) |
| 1823 | bytes_sent = count; |
| 1824 | pSMB->DataOffset = |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 1825 | cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1826 | if (buf) |
Steve French | 61e7480 | 2008-12-03 00:57:54 +0000 | [diff] [blame] | 1827 | memcpy(pSMB->Data, buf, bytes_sent); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1828 | else if (ubuf) { |
| 1829 | if (copy_from_user(pSMB->Data, ubuf, bytes_sent)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1830 | cifs_buf_release(pSMB); |
| 1831 | return -EFAULT; |
| 1832 | } |
Steve French | e30dcf3 | 2005-09-20 20:49:16 -0700 | [diff] [blame] | 1833 | } else if (count != 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1834 | /* No buffer */ |
| 1835 | cifs_buf_release(pSMB); |
| 1836 | return -EINVAL; |
Steve French | e30dcf3 | 2005-09-20 20:49:16 -0700 | [diff] [blame] | 1837 | } /* else setting file size with write of zero bytes */ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1838 | if (wct == 14) |
Steve French | e30dcf3 | 2005-09-20 20:49:16 -0700 | [diff] [blame] | 1839 | byte_count = bytes_sent + 1; /* pad */ |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 1840 | else /* wct == 12 */ |
Steve French | e30dcf3 | 2005-09-20 20:49:16 -0700 | [diff] [blame] | 1841 | byte_count = bytes_sent + 5; /* bigger pad, smaller smb hdr */ |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 1842 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1843 | pSMB->DataLengthLow = cpu_to_le16(bytes_sent & 0xFFFF); |
| 1844 | pSMB->DataLengthHigh = cpu_to_le16(bytes_sent >> 16); |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 1845 | inc_rfc1001_len(pSMB, byte_count); |
Steve French | 1c95518 | 2005-08-30 20:58:07 -0700 | [diff] [blame] | 1846 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1847 | if (wct == 14) |
Steve French | 1c95518 | 2005-08-30 20:58:07 -0700 | [diff] [blame] | 1848 | pSMB->ByteCount = cpu_to_le16(byte_count); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 1849 | else { /* old style write has byte count 4 bytes earlier |
| 1850 | so 4 bytes pad */ |
| 1851 | struct smb_com_writex_req *pSMBW = |
Steve French | 1c95518 | 2005-08-30 20:58:07 -0700 | [diff] [blame] | 1852 | (struct smb_com_writex_req *)pSMB; |
| 1853 | pSMBW->ByteCount = cpu_to_le16(byte_count); |
| 1854 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1855 | |
| 1856 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 1857 | (struct smb_hdr *) pSMBr, &bytes_returned, long_op); |
Pavel Shilovsky | 44c5818 | 2012-05-28 14:16:31 +0400 | [diff] [blame] | 1858 | cifs_stats_inc(&tcon->stats.cifs_stats.num_writes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1859 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1860 | cifs_dbg(FYI, "Send error in write = %d\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1861 | } else { |
| 1862 | *nbytes = le16_to_cpu(pSMBr->CountHigh); |
| 1863 | *nbytes = (*nbytes) << 16; |
| 1864 | *nbytes += le16_to_cpu(pSMBr->Count); |
Suresh Jayaraman | 6513a81 | 2010-03-31 12:00:03 +0530 | [diff] [blame] | 1865 | |
| 1866 | /* |
| 1867 | * Mask off high 16 bits when bytes written as returned by the |
| 1868 | * server is greater than bytes requested by the client. Some |
| 1869 | * OS/2 servers are known to set incorrect CountHigh values. |
| 1870 | */ |
| 1871 | if (*nbytes > count) |
| 1872 | *nbytes &= 0xFFFF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1873 | } |
| 1874 | |
| 1875 | cifs_buf_release(pSMB); |
| 1876 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 1877 | /* Note: On -EAGAIN error only caller can retry on handle based calls |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1878 | since file handle passed in no longer valid */ |
| 1879 | |
| 1880 | return rc; |
| 1881 | } |
| 1882 | |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 1883 | void |
| 1884 | cifs_writedata_release(struct kref *refcount) |
| 1885 | { |
| 1886 | struct cifs_writedata *wdata = container_of(refcount, |
| 1887 | struct cifs_writedata, refcount); |
| 1888 | |
| 1889 | if (wdata->cfile) |
| 1890 | cifsFileInfo_put(wdata->cfile); |
| 1891 | |
| 1892 | kfree(wdata); |
| 1893 | } |
| 1894 | |
| 1895 | /* |
| 1896 | * Write failed with a retryable error. Resend the write request. It's also |
| 1897 | * possible that the page was redirtied so re-clean the page. |
| 1898 | */ |
| 1899 | static void |
| 1900 | cifs_writev_requeue(struct cifs_writedata *wdata) |
| 1901 | { |
| 1902 | int i, rc; |
| 1903 | struct inode *inode = wdata->cfile->dentry->d_inode; |
Pavel Shilovsky | c9de5c8 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 1904 | struct TCP_Server_Info *server; |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 1905 | |
| 1906 | for (i = 0; i < wdata->nr_pages; i++) { |
| 1907 | lock_page(wdata->pages[i]); |
| 1908 | clear_page_dirty_for_io(wdata->pages[i]); |
| 1909 | } |
| 1910 | |
| 1911 | do { |
Pavel Shilovsky | c9de5c8 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 1912 | server = tlink_tcon(wdata->cfile->tlink)->ses->server; |
Steve French | 4a5c80d | 2014-02-07 20:45:12 -0600 | [diff] [blame] | 1913 | rc = server->ops->async_writev(wdata, cifs_writedata_release); |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 1914 | } while (rc == -EAGAIN); |
| 1915 | |
| 1916 | for (i = 0; i < wdata->nr_pages; i++) { |
Jeff Layton | 94e1800 | 2013-03-04 15:18:25 -0500 | [diff] [blame] | 1917 | unlock_page(wdata->pages[i]); |
Ouyang Maochun | c51bb0e | 2013-02-18 09:54:52 -0600 | [diff] [blame] | 1918 | if (rc != 0) { |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 1919 | SetPageError(wdata->pages[i]); |
Ouyang Maochun | c51bb0e | 2013-02-18 09:54:52 -0600 | [diff] [blame] | 1920 | end_page_writeback(wdata->pages[i]); |
| 1921 | page_cache_release(wdata->pages[i]); |
| 1922 | } |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 1923 | } |
| 1924 | |
| 1925 | mapping_set_error(inode->i_mapping, rc); |
| 1926 | kref_put(&wdata->refcount, cifs_writedata_release); |
| 1927 | } |
| 1928 | |
Jeff Layton | c2e8764 | 2012-03-23 14:40:55 -0400 | [diff] [blame] | 1929 | void |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 1930 | cifs_writev_complete(struct work_struct *work) |
| 1931 | { |
| 1932 | struct cifs_writedata *wdata = container_of(work, |
| 1933 | struct cifs_writedata, work); |
| 1934 | struct inode *inode = wdata->cfile->dentry->d_inode; |
| 1935 | int i = 0; |
| 1936 | |
| 1937 | if (wdata->result == 0) { |
Jeff Layton | 597b027 | 2012-03-23 14:40:56 -0400 | [diff] [blame] | 1938 | spin_lock(&inode->i_lock); |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 1939 | cifs_update_eof(CIFS_I(inode), wdata->offset, wdata->bytes); |
Jeff Layton | 597b027 | 2012-03-23 14:40:56 -0400 | [diff] [blame] | 1940 | spin_unlock(&inode->i_lock); |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 1941 | cifs_stats_bytes_written(tlink_tcon(wdata->cfile->tlink), |
| 1942 | wdata->bytes); |
| 1943 | } else if (wdata->sync_mode == WB_SYNC_ALL && wdata->result == -EAGAIN) |
| 1944 | return cifs_writev_requeue(wdata); |
| 1945 | |
| 1946 | for (i = 0; i < wdata->nr_pages; i++) { |
| 1947 | struct page *page = wdata->pages[i]; |
| 1948 | if (wdata->result == -EAGAIN) |
| 1949 | __set_page_dirty_nobuffers(page); |
| 1950 | else if (wdata->result < 0) |
| 1951 | SetPageError(page); |
| 1952 | end_page_writeback(page); |
| 1953 | page_cache_release(page); |
| 1954 | } |
| 1955 | if (wdata->result != -EAGAIN) |
| 1956 | mapping_set_error(inode->i_mapping, wdata->result); |
| 1957 | kref_put(&wdata->refcount, cifs_writedata_release); |
| 1958 | } |
| 1959 | |
| 1960 | struct cifs_writedata * |
Jeff Layton | c2e8764 | 2012-03-23 14:40:55 -0400 | [diff] [blame] | 1961 | cifs_writedata_alloc(unsigned int nr_pages, work_func_t complete) |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 1962 | { |
| 1963 | struct cifs_writedata *wdata; |
| 1964 | |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 1965 | /* writedata + number of page pointers */ |
| 1966 | wdata = kzalloc(sizeof(*wdata) + |
Jeff Layton | 26c8f0d | 2014-02-07 11:04:04 -0500 | [diff] [blame] | 1967 | sizeof(struct page *) * nr_pages, GFP_NOFS); |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 1968 | if (wdata != NULL) { |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 1969 | kref_init(&wdata->refcount); |
Jeff Layton | da82f7e | 2012-03-23 14:40:56 -0400 | [diff] [blame] | 1970 | INIT_LIST_HEAD(&wdata->list); |
| 1971 | init_completion(&wdata->done); |
| 1972 | INIT_WORK(&wdata->work, complete); |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 1973 | } |
| 1974 | return wdata; |
| 1975 | } |
| 1976 | |
| 1977 | /* |
Pavel Shilovsky | 7c9421e | 2012-03-23 14:28:03 -0400 | [diff] [blame] | 1978 | * Check the mid_state and signature on received buffer (if any), and queue the |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 1979 | * workqueue completion task. |
| 1980 | */ |
| 1981 | static void |
| 1982 | cifs_writev_callback(struct mid_q_entry *mid) |
| 1983 | { |
| 1984 | struct cifs_writedata *wdata = mid->callback_data; |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 1985 | struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink); |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 1986 | unsigned int written; |
| 1987 | WRITE_RSP *smb = (WRITE_RSP *)mid->resp_buf; |
| 1988 | |
Pavel Shilovsky | 7c9421e | 2012-03-23 14:28:03 -0400 | [diff] [blame] | 1989 | switch (mid->mid_state) { |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 1990 | case MID_RESPONSE_RECEIVED: |
| 1991 | wdata->result = cifs_check_receive(mid, tcon->ses->server, 0); |
| 1992 | if (wdata->result != 0) |
| 1993 | break; |
| 1994 | |
| 1995 | written = le16_to_cpu(smb->CountHigh); |
| 1996 | written <<= 16; |
| 1997 | written += le16_to_cpu(smb->Count); |
| 1998 | /* |
| 1999 | * Mask off high 16 bits when bytes written as returned |
| 2000 | * by the server is greater than bytes requested by the |
| 2001 | * client. OS/2 servers are known to set incorrect |
| 2002 | * CountHigh values. |
| 2003 | */ |
| 2004 | if (written > wdata->bytes) |
| 2005 | written &= 0xFFFF; |
| 2006 | |
| 2007 | if (written < wdata->bytes) |
| 2008 | wdata->result = -ENOSPC; |
| 2009 | else |
| 2010 | wdata->bytes = written; |
| 2011 | break; |
| 2012 | case MID_REQUEST_SUBMITTED: |
| 2013 | case MID_RETRY_NEEDED: |
| 2014 | wdata->result = -EAGAIN; |
| 2015 | break; |
| 2016 | default: |
| 2017 | wdata->result = -EIO; |
| 2018 | break; |
| 2019 | } |
| 2020 | |
Jeff Layton | da472fc | 2012-03-23 14:40:53 -0400 | [diff] [blame] | 2021 | queue_work(cifsiod_wq, &wdata->work); |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 2022 | DeleteMidQEntry(mid); |
Pavel Shilovsky | a891f0f | 2012-05-23 16:14:34 +0400 | [diff] [blame] | 2023 | add_credits(tcon->ses->server, 1, 0); |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 2024 | } |
| 2025 | |
| 2026 | /* cifs_async_writev - send an async write, and set up mid to handle result */ |
| 2027 | int |
Steve French | 4a5c80d | 2014-02-07 20:45:12 -0600 | [diff] [blame] | 2028 | cifs_async_writev(struct cifs_writedata *wdata, |
| 2029 | void (*release)(struct kref *kref)) |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 2030 | { |
Jeff Layton | eddb079 | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 2031 | int rc = -EACCES; |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 2032 | WRITE_REQ *smb = NULL; |
| 2033 | int wct; |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 2034 | struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink); |
Jeff Layton | eddb079 | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 2035 | struct kvec iov; |
Jeff Layton | fec344e | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 2036 | struct smb_rqst rqst = { }; |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 2037 | |
| 2038 | if (tcon->ses->capabilities & CAP_LARGE_FILES) { |
| 2039 | wct = 14; |
| 2040 | } else { |
| 2041 | wct = 12; |
| 2042 | if (wdata->offset >> 32 > 0) { |
| 2043 | /* can not handle big offset for old srv */ |
| 2044 | return -EIO; |
| 2045 | } |
| 2046 | } |
| 2047 | |
| 2048 | rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **)&smb); |
| 2049 | if (rc) |
| 2050 | goto async_writev_out; |
| 2051 | |
Jeff Layton | fe5f5d2 | 2012-03-23 14:40:55 -0400 | [diff] [blame] | 2052 | smb->hdr.Pid = cpu_to_le16((__u16)wdata->pid); |
| 2053 | smb->hdr.PidHigh = cpu_to_le16((__u16)(wdata->pid >> 16)); |
Pavel Shilovsky | fa2989f | 2011-05-26 10:01:59 +0400 | [diff] [blame] | 2054 | |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 2055 | smb->AndXCommand = 0xFF; /* none */ |
Pavel Shilovsky | 4b4de76 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 2056 | smb->Fid = wdata->cfile->fid.netfid; |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 2057 | smb->OffsetLow = cpu_to_le32(wdata->offset & 0xFFFFFFFF); |
| 2058 | if (wct == 14) |
| 2059 | smb->OffsetHigh = cpu_to_le32(wdata->offset >> 32); |
| 2060 | smb->Reserved = 0xFFFFFFFF; |
| 2061 | smb->WriteMode = 0; |
| 2062 | smb->Remaining = 0; |
| 2063 | |
| 2064 | smb->DataOffset = |
| 2065 | cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4); |
| 2066 | |
| 2067 | /* 4 for RFC1001 length + 1 for BCC */ |
Jeff Layton | eddb079 | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 2068 | iov.iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4 + 1; |
| 2069 | iov.iov_base = smb; |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 2070 | |
Jeff Layton | eddb079 | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 2071 | rqst.rq_iov = &iov; |
| 2072 | rqst.rq_nvec = 1; |
| 2073 | rqst.rq_pages = wdata->pages; |
| 2074 | rqst.rq_npages = wdata->nr_pages; |
| 2075 | rqst.rq_pagesz = wdata->pagesz; |
| 2076 | rqst.rq_tailsz = wdata->tailsz; |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 2077 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2078 | cifs_dbg(FYI, "async write at %llu %u bytes\n", |
| 2079 | wdata->offset, wdata->bytes); |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 2080 | |
| 2081 | smb->DataLengthLow = cpu_to_le16(wdata->bytes & 0xFFFF); |
| 2082 | smb->DataLengthHigh = cpu_to_le16(wdata->bytes >> 16); |
| 2083 | |
| 2084 | if (wct == 14) { |
| 2085 | inc_rfc1001_len(&smb->hdr, wdata->bytes + 1); |
| 2086 | put_bcc(wdata->bytes + 1, &smb->hdr); |
| 2087 | } else { |
| 2088 | /* wct == 12 */ |
| 2089 | struct smb_com_writex_req *smbw = |
| 2090 | (struct smb_com_writex_req *)smb; |
| 2091 | inc_rfc1001_len(&smbw->hdr, wdata->bytes + 5); |
| 2092 | put_bcc(wdata->bytes + 5, &smbw->hdr); |
Jeff Layton | eddb079 | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 2093 | iov.iov_len += 4; /* pad bigger by four bytes */ |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 2094 | } |
| 2095 | |
| 2096 | kref_get(&wdata->refcount); |
Jeff Layton | fec344e | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 2097 | rc = cifs_call_async(tcon->ses->server, &rqst, NULL, |
| 2098 | cifs_writev_callback, wdata, 0); |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 2099 | |
| 2100 | if (rc == 0) |
Pavel Shilovsky | 44c5818 | 2012-05-28 14:16:31 +0400 | [diff] [blame] | 2101 | cifs_stats_inc(&tcon->stats.cifs_stats.num_writes); |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 2102 | else |
Steve French | 4a5c80d | 2014-02-07 20:45:12 -0600 | [diff] [blame] | 2103 | kref_put(&wdata->refcount, release); |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 2104 | |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 2105 | async_writev_out: |
| 2106 | cifs_small_buf_release(smb); |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 2107 | return rc; |
| 2108 | } |
| 2109 | |
Steve French | d6e04ae | 2005-06-13 13:24:43 -0500 | [diff] [blame] | 2110 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2111 | CIFSSMBWrite2(const unsigned int xid, struct cifs_io_parms *io_parms, |
Pavel Shilovsky | ba9ad725 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 2112 | unsigned int *nbytes, struct kvec *iov, int n_vec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2113 | { |
| 2114 | int rc = -EACCES; |
| 2115 | WRITE_REQ *pSMB = NULL; |
Steve French | ec637e3 | 2005-12-12 20:53:18 -0800 | [diff] [blame] | 2116 | int wct; |
Steve French | d6e04ae | 2005-06-13 13:24:43 -0500 | [diff] [blame] | 2117 | int smb_hdr_len; |
Steve French | ec637e3 | 2005-12-12 20:53:18 -0800 | [diff] [blame] | 2118 | int resp_buf_type = 0; |
Pavel Shilovsky | fa2989f | 2011-05-26 10:01:59 +0400 | [diff] [blame] | 2119 | __u32 pid = io_parms->pid; |
| 2120 | __u16 netfid = io_parms->netfid; |
| 2121 | __u64 offset = io_parms->offset; |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 2122 | struct cifs_tcon *tcon = io_parms->tcon; |
Pavel Shilovsky | fa2989f | 2011-05-26 10:01:59 +0400 | [diff] [blame] | 2123 | unsigned int count = io_parms->length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2124 | |
Jeff Layton | fbec9ab | 2009-04-03 13:44:00 -0400 | [diff] [blame] | 2125 | *nbytes = 0; |
| 2126 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2127 | cifs_dbg(FYI, "write2 at %lld %d bytes\n", (long long)offset, count); |
Steve French | ff7feac | 2005-11-15 16:45:16 -0800 | [diff] [blame] | 2128 | |
Steve French | 4c3130e | 2008-12-09 00:28:16 +0000 | [diff] [blame] | 2129 | if (tcon->ses->capabilities & CAP_LARGE_FILES) { |
Steve French | 8cc64c6 | 2005-10-03 13:49:43 -0700 | [diff] [blame] | 2130 | wct = 14; |
Steve French | 4c3130e | 2008-12-09 00:28:16 +0000 | [diff] [blame] | 2131 | } else { |
Steve French | 8cc64c6 | 2005-10-03 13:49:43 -0700 | [diff] [blame] | 2132 | wct = 12; |
Steve French | 4c3130e | 2008-12-09 00:28:16 +0000 | [diff] [blame] | 2133 | if ((offset >> 32) > 0) { |
| 2134 | /* can not handle big offset for old srv */ |
| 2135 | return -EIO; |
| 2136 | } |
| 2137 | } |
Steve French | 8cc64c6 | 2005-10-03 13:49:43 -0700 | [diff] [blame] | 2138 | rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2139 | if (rc) |
| 2140 | return rc; |
Pavel Shilovsky | fa2989f | 2011-05-26 10:01:59 +0400 | [diff] [blame] | 2141 | |
| 2142 | pSMB->hdr.Pid = cpu_to_le16((__u16)pid); |
| 2143 | pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16)); |
| 2144 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2145 | /* tcon and ses pointer are checked in smb_init */ |
| 2146 | if (tcon->ses->server == NULL) |
| 2147 | return -ECONNABORTED; |
| 2148 | |
Steve French | d6e04ae | 2005-06-13 13:24:43 -0500 | [diff] [blame] | 2149 | pSMB->AndXCommand = 0xFF; /* none */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2150 | pSMB->Fid = netfid; |
| 2151 | pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 2152 | if (wct == 14) |
Steve French | 8cc64c6 | 2005-10-03 13:49:43 -0700 | [diff] [blame] | 2153 | pSMB->OffsetHigh = cpu_to_le32(offset >> 32); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2154 | pSMB->Reserved = 0xFFFFFFFF; |
| 2155 | pSMB->WriteMode = 0; |
| 2156 | pSMB->Remaining = 0; |
Steve French | d6e04ae | 2005-06-13 13:24:43 -0500 | [diff] [blame] | 2157 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2158 | pSMB->DataOffset = |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2159 | cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2160 | |
Steve French | 3e84469 | 2005-10-03 13:37:24 -0700 | [diff] [blame] | 2161 | pSMB->DataLengthLow = cpu_to_le16(count & 0xFFFF); |
| 2162 | pSMB->DataLengthHigh = cpu_to_le16(count >> 16); |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 2163 | /* header + 1 byte pad */ |
| 2164 | smb_hdr_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 1; |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 2165 | if (wct == 14) |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 2166 | inc_rfc1001_len(pSMB, count + 1); |
Steve French | 8cc64c6 | 2005-10-03 13:49:43 -0700 | [diff] [blame] | 2167 | else /* wct == 12 */ |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 2168 | inc_rfc1001_len(pSMB, count + 5); /* smb data starts later */ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 2169 | if (wct == 14) |
Steve French | 8cc64c6 | 2005-10-03 13:49:43 -0700 | [diff] [blame] | 2170 | pSMB->ByteCount = cpu_to_le16(count + 1); |
| 2171 | else /* wct == 12 */ /* bigger pad, smaller smb hdr, keep offset ok */ { |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2172 | struct smb_com_writex_req *pSMBW = |
Steve French | 8cc64c6 | 2005-10-03 13:49:43 -0700 | [diff] [blame] | 2173 | (struct smb_com_writex_req *)pSMB; |
| 2174 | pSMBW->ByteCount = cpu_to_le16(count + 5); |
| 2175 | } |
Steve French | 3e84469 | 2005-10-03 13:37:24 -0700 | [diff] [blame] | 2176 | iov[0].iov_base = pSMB; |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 2177 | if (wct == 14) |
Steve French | ec637e3 | 2005-12-12 20:53:18 -0800 | [diff] [blame] | 2178 | iov[0].iov_len = smb_hdr_len + 4; |
| 2179 | else /* wct == 12 pad bigger by four bytes */ |
| 2180 | iov[0].iov_len = smb_hdr_len + 8; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2181 | |
Steve French | 3e84469 | 2005-10-03 13:37:24 -0700 | [diff] [blame] | 2182 | |
Pavel Shilovsky | ba9ad725 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 2183 | rc = SendReceive2(xid, tcon->ses, iov, n_vec + 1, &resp_buf_type, 0); |
Pavel Shilovsky | 44c5818 | 2012-05-28 14:16:31 +0400 | [diff] [blame] | 2184 | cifs_stats_inc(&tcon->stats.cifs_stats.num_writes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2185 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2186 | cifs_dbg(FYI, "Send error Write2 = %d\n", rc); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 2187 | } else if (resp_buf_type == 0) { |
Steve French | ec637e3 | 2005-12-12 20:53:18 -0800 | [diff] [blame] | 2188 | /* presumably this can not happen, but best to be safe */ |
| 2189 | rc = -EIO; |
Steve French | d6e04ae | 2005-06-13 13:24:43 -0500 | [diff] [blame] | 2190 | } else { |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 2191 | WRITE_RSP *pSMBr = (WRITE_RSP *)iov[0].iov_base; |
Steve French | d6e04ae | 2005-06-13 13:24:43 -0500 | [diff] [blame] | 2192 | *nbytes = le16_to_cpu(pSMBr->CountHigh); |
| 2193 | *nbytes = (*nbytes) << 16; |
| 2194 | *nbytes += le16_to_cpu(pSMBr->Count); |
Suresh Jayaraman | 6513a81 | 2010-03-31 12:00:03 +0530 | [diff] [blame] | 2195 | |
| 2196 | /* |
| 2197 | * Mask off high 16 bits when bytes written as returned by the |
| 2198 | * server is greater than bytes requested by the client. OS/2 |
| 2199 | * servers are known to set incorrect CountHigh values. |
| 2200 | */ |
| 2201 | if (*nbytes > count) |
| 2202 | *nbytes &= 0xFFFF; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2203 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2204 | |
Steve French | 4b8f930 | 2006-02-26 16:41:18 +0000 | [diff] [blame] | 2205 | /* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 2206 | if (resp_buf_type == CIFS_SMALL_BUFFER) |
Steve French | ec637e3 | 2005-12-12 20:53:18 -0800 | [diff] [blame] | 2207 | cifs_small_buf_release(iov[0].iov_base); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 2208 | else if (resp_buf_type == CIFS_LARGE_BUFFER) |
Steve French | ec637e3 | 2005-12-12 20:53:18 -0800 | [diff] [blame] | 2209 | cifs_buf_release(iov[0].iov_base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2210 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2211 | /* Note: On -EAGAIN error only caller can retry on handle based calls |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2212 | since file handle passed in no longer valid */ |
| 2213 | |
| 2214 | return rc; |
| 2215 | } |
Steve French | d6e04ae | 2005-06-13 13:24:43 -0500 | [diff] [blame] | 2216 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2217 | int cifs_lockv(const unsigned int xid, struct cifs_tcon *tcon, |
| 2218 | const __u16 netfid, const __u8 lock_type, const __u32 num_unlock, |
Pavel Shilovsky | 9ee305b | 2011-10-22 15:33:31 +0400 | [diff] [blame] | 2219 | const __u32 num_lock, LOCKING_ANDX_RANGE *buf) |
| 2220 | { |
| 2221 | int rc = 0; |
| 2222 | LOCK_REQ *pSMB = NULL; |
| 2223 | struct kvec iov[2]; |
| 2224 | int resp_buf_type; |
| 2225 | __u16 count; |
| 2226 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2227 | cifs_dbg(FYI, "cifs_lockv num lock %d num unlock %d\n", |
| 2228 | num_lock, num_unlock); |
Pavel Shilovsky | 9ee305b | 2011-10-22 15:33:31 +0400 | [diff] [blame] | 2229 | |
| 2230 | rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB); |
| 2231 | if (rc) |
| 2232 | return rc; |
| 2233 | |
| 2234 | pSMB->Timeout = 0; |
| 2235 | pSMB->NumberOfLocks = cpu_to_le16(num_lock); |
| 2236 | pSMB->NumberOfUnlocks = cpu_to_le16(num_unlock); |
| 2237 | pSMB->LockType = lock_type; |
| 2238 | pSMB->AndXCommand = 0xFF; /* none */ |
| 2239 | pSMB->Fid = netfid; /* netfid stays le */ |
| 2240 | |
| 2241 | count = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE); |
| 2242 | inc_rfc1001_len(pSMB, count); |
| 2243 | pSMB->ByteCount = cpu_to_le16(count); |
| 2244 | |
| 2245 | iov[0].iov_base = (char *)pSMB; |
| 2246 | iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4 - |
| 2247 | (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE); |
| 2248 | iov[1].iov_base = (char *)buf; |
| 2249 | iov[1].iov_len = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE); |
| 2250 | |
Pavel Shilovsky | 44c5818 | 2012-05-28 14:16:31 +0400 | [diff] [blame] | 2251 | cifs_stats_inc(&tcon->stats.cifs_stats.num_locks); |
Pavel Shilovsky | 9ee305b | 2011-10-22 15:33:31 +0400 | [diff] [blame] | 2252 | rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP); |
| 2253 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2254 | cifs_dbg(FYI, "Send error in cifs_lockv = %d\n", rc); |
Pavel Shilovsky | 9ee305b | 2011-10-22 15:33:31 +0400 | [diff] [blame] | 2255 | |
| 2256 | return rc; |
| 2257 | } |
Steve French | d6e04ae | 2005-06-13 13:24:43 -0500 | [diff] [blame] | 2258 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2259 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2260 | CIFSSMBLock(const unsigned int xid, struct cifs_tcon *tcon, |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 2261 | const __u16 smb_file_id, const __u32 netpid, const __u64 len, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2262 | const __u64 offset, const __u32 numUnlock, |
Pavel Shilovsky | 12fed00 | 2011-01-17 20:15:44 +0300 | [diff] [blame] | 2263 | const __u32 numLock, const __u8 lockType, |
| 2264 | const bool waitFlag, const __u8 oplock_level) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2265 | { |
| 2266 | int rc = 0; |
| 2267 | LOCK_REQ *pSMB = NULL; |
Steve French | aaa9bbe | 2008-05-23 17:38:32 +0000 | [diff] [blame] | 2268 | /* LOCK_RSP *pSMBr = NULL; */ /* No response data other than rc to parse */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2269 | int bytes_returned; |
Pavel Shilovsky | a891f0f | 2012-05-23 16:14:34 +0400 | [diff] [blame] | 2270 | int flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2271 | __u16 count; |
| 2272 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2273 | cifs_dbg(FYI, "CIFSSMBLock timeout %d numLock %d\n", |
| 2274 | (int)waitFlag, numLock); |
Steve French | 46810cb | 2005-04-28 22:41:09 -0700 | [diff] [blame] | 2275 | rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB); |
| 2276 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2277 | if (rc) |
| 2278 | return rc; |
| 2279 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 2280 | if (lockType == LOCKING_ANDX_OPLOCK_RELEASE) { |
Pavel Shilovsky | a891f0f | 2012-05-23 16:14:34 +0400 | [diff] [blame] | 2281 | /* no response expected */ |
| 2282 | flags = CIFS_ASYNC_OP | CIFS_OBREAK_OP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2283 | pSMB->Timeout = 0; |
Steve French | 4b18f2a | 2008-04-29 00:06:05 +0000 | [diff] [blame] | 2284 | } else if (waitFlag) { |
Pavel Shilovsky | a891f0f | 2012-05-23 16:14:34 +0400 | [diff] [blame] | 2285 | flags = CIFS_BLOCKING_OP; /* blocking operation, no timeout */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2286 | pSMB->Timeout = cpu_to_le32(-1);/* blocking - do not time out */ |
| 2287 | } else { |
| 2288 | pSMB->Timeout = 0; |
| 2289 | } |
| 2290 | |
| 2291 | pSMB->NumberOfLocks = cpu_to_le16(numLock); |
| 2292 | pSMB->NumberOfUnlocks = cpu_to_le16(numUnlock); |
| 2293 | pSMB->LockType = lockType; |
Pavel Shilovsky | 12fed00 | 2011-01-17 20:15:44 +0300 | [diff] [blame] | 2294 | pSMB->OplockLevel = oplock_level; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2295 | pSMB->AndXCommand = 0xFF; /* none */ |
| 2296 | pSMB->Fid = smb_file_id; /* netfid stays le */ |
| 2297 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 2298 | if ((numLock != 0) || (numUnlock != 0)) { |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 2299 | pSMB->Locks[0].Pid = cpu_to_le16(netpid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2300 | /* BB where to store pid high? */ |
| 2301 | pSMB->Locks[0].LengthLow = cpu_to_le32((u32)len); |
| 2302 | pSMB->Locks[0].LengthHigh = cpu_to_le32((u32)(len>>32)); |
| 2303 | pSMB->Locks[0].OffsetLow = cpu_to_le32((u32)offset); |
| 2304 | pSMB->Locks[0].OffsetHigh = cpu_to_le32((u32)(offset>>32)); |
| 2305 | count = sizeof(LOCKING_ANDX_RANGE); |
| 2306 | } else { |
| 2307 | /* oplock break */ |
| 2308 | count = 0; |
| 2309 | } |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 2310 | inc_rfc1001_len(pSMB, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2311 | pSMB->ByteCount = cpu_to_le16(count); |
| 2312 | |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 2313 | if (waitFlag) { |
| 2314 | rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB, |
Steve French | aaa9bbe | 2008-05-23 17:38:32 +0000 | [diff] [blame] | 2315 | (struct smb_hdr *) pSMB, &bytes_returned); |
Steve French | 133672e | 2007-11-13 22:41:37 +0000 | [diff] [blame] | 2316 | cifs_small_buf_release(pSMB); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 2317 | } else { |
Pavel Shilovsky | a891f0f | 2012-05-23 16:14:34 +0400 | [diff] [blame] | 2318 | rc = SendReceiveNoRsp(xid, tcon->ses, (char *)pSMB, flags); |
Steve French | 133672e | 2007-11-13 22:41:37 +0000 | [diff] [blame] | 2319 | /* SMB buffer freed by function above */ |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 2320 | } |
Pavel Shilovsky | 44c5818 | 2012-05-28 14:16:31 +0400 | [diff] [blame] | 2321 | cifs_stats_inc(&tcon->stats.cifs_stats.num_locks); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 2322 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2323 | cifs_dbg(FYI, "Send error in Lock = %d\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2324 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2325 | /* Note: On -EAGAIN error only caller can retry on handle based calls |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2326 | since file handle passed in no longer valid */ |
| 2327 | return rc; |
| 2328 | } |
| 2329 | |
| 2330 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2331 | CIFSSMBPosixLock(const unsigned int xid, struct cifs_tcon *tcon, |
Jeff Layton | c5fd363 | 2012-07-23 13:28:37 -0400 | [diff] [blame] | 2332 | const __u16 smb_file_id, const __u32 netpid, |
| 2333 | const loff_t start_offset, const __u64 len, |
| 2334 | struct file_lock *pLockData, const __u16 lock_type, |
| 2335 | const bool waitFlag) |
Steve French | 08547b0 | 2006-02-28 22:39:25 +0000 | [diff] [blame] | 2336 | { |
| 2337 | struct smb_com_transaction2_sfi_req *pSMB = NULL; |
| 2338 | struct smb_com_transaction2_sfi_rsp *pSMBr = NULL; |
Steve French | 08547b0 | 2006-02-28 22:39:25 +0000 | [diff] [blame] | 2339 | struct cifs_posix_lock *parm_data; |
| 2340 | int rc = 0; |
Steve French | 3a5ff61 | 2006-07-14 22:37:11 +0000 | [diff] [blame] | 2341 | int timeout = 0; |
Steve French | 08547b0 | 2006-02-28 22:39:25 +0000 | [diff] [blame] | 2342 | int bytes_returned = 0; |
Steve French | 133672e | 2007-11-13 22:41:37 +0000 | [diff] [blame] | 2343 | int resp_buf_type = 0; |
Steve French | 08547b0 | 2006-02-28 22:39:25 +0000 | [diff] [blame] | 2344 | __u16 params, param_offset, offset, byte_count, count; |
Steve French | 133672e | 2007-11-13 22:41:37 +0000 | [diff] [blame] | 2345 | struct kvec iov[1]; |
Steve French | 08547b0 | 2006-02-28 22:39:25 +0000 | [diff] [blame] | 2346 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2347 | cifs_dbg(FYI, "Posix Lock\n"); |
Steve French | fc94cdb | 2006-05-30 18:03:32 +0000 | [diff] [blame] | 2348 | |
Steve French | 08547b0 | 2006-02-28 22:39:25 +0000 | [diff] [blame] | 2349 | rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB); |
| 2350 | |
| 2351 | if (rc) |
| 2352 | return rc; |
| 2353 | |
| 2354 | pSMBr = (struct smb_com_transaction2_sfi_rsp *)pSMB; |
| 2355 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2356 | params = 6; |
Steve French | 08547b0 | 2006-02-28 22:39:25 +0000 | [diff] [blame] | 2357 | pSMB->MaxSetupCount = 0; |
| 2358 | pSMB->Reserved = 0; |
| 2359 | pSMB->Flags = 0; |
Steve French | 08547b0 | 2006-02-28 22:39:25 +0000 | [diff] [blame] | 2360 | pSMB->Reserved2 = 0; |
| 2361 | param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4; |
| 2362 | offset = param_offset + params; |
| 2363 | |
Steve French | 08547b0 | 2006-02-28 22:39:25 +0000 | [diff] [blame] | 2364 | count = sizeof(struct cifs_posix_lock); |
| 2365 | pSMB->MaxParameterCount = cpu_to_le16(2); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 2366 | pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */ |
Steve French | 08547b0 | 2006-02-28 22:39:25 +0000 | [diff] [blame] | 2367 | pSMB->SetupCount = 1; |
| 2368 | pSMB->Reserved3 = 0; |
Jeff Layton | c5fd363 | 2012-07-23 13:28:37 -0400 | [diff] [blame] | 2369 | if (pLockData) |
Steve French | 08547b0 | 2006-02-28 22:39:25 +0000 | [diff] [blame] | 2370 | pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION); |
| 2371 | else |
| 2372 | pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION); |
| 2373 | byte_count = 3 /* pad */ + params + count; |
| 2374 | pSMB->DataCount = cpu_to_le16(count); |
| 2375 | pSMB->ParameterCount = cpu_to_le16(params); |
| 2376 | pSMB->TotalDataCount = pSMB->DataCount; |
| 2377 | pSMB->TotalParameterCount = pSMB->ParameterCount; |
| 2378 | pSMB->ParameterOffset = cpu_to_le16(param_offset); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2379 | parm_data = (struct cifs_posix_lock *) |
Steve French | 08547b0 | 2006-02-28 22:39:25 +0000 | [diff] [blame] | 2380 | (((char *) &pSMB->hdr.Protocol) + offset); |
| 2381 | |
| 2382 | parm_data->lock_type = cpu_to_le16(lock_type); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 2383 | if (waitFlag) { |
Steve French | 133672e | 2007-11-13 22:41:37 +0000 | [diff] [blame] | 2384 | timeout = CIFS_BLOCKING_OP; /* blocking operation, no timeout */ |
Steve French | cec6815a | 2006-05-30 18:07:17 +0000 | [diff] [blame] | 2385 | parm_data->lock_flags = cpu_to_le16(1); |
Steve French | 3a5ff61 | 2006-07-14 22:37:11 +0000 | [diff] [blame] | 2386 | pSMB->Timeout = cpu_to_le32(-1); |
| 2387 | } else |
| 2388 | pSMB->Timeout = 0; |
| 2389 | |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 2390 | parm_data->pid = cpu_to_le32(netpid); |
Jeff Layton | c5fd363 | 2012-07-23 13:28:37 -0400 | [diff] [blame] | 2391 | parm_data->start = cpu_to_le64(start_offset); |
Steve French | cec6815a | 2006-05-30 18:07:17 +0000 | [diff] [blame] | 2392 | parm_data->length = cpu_to_le64(len); /* normalize negative numbers */ |
Steve French | 08547b0 | 2006-02-28 22:39:25 +0000 | [diff] [blame] | 2393 | |
| 2394 | pSMB->DataOffset = cpu_to_le16(offset); |
Steve French | f26282c | 2006-03-01 09:17:37 +0000 | [diff] [blame] | 2395 | pSMB->Fid = smb_file_id; |
Steve French | 08547b0 | 2006-02-28 22:39:25 +0000 | [diff] [blame] | 2396 | pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_LOCK); |
| 2397 | pSMB->Reserved4 = 0; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 2398 | inc_rfc1001_len(pSMB, byte_count); |
Steve French | 08547b0 | 2006-02-28 22:39:25 +0000 | [diff] [blame] | 2399 | pSMB->ByteCount = cpu_to_le16(byte_count); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 2400 | if (waitFlag) { |
| 2401 | rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB, |
| 2402 | (struct smb_hdr *) pSMBr, &bytes_returned); |
| 2403 | } else { |
Steve French | 133672e | 2007-11-13 22:41:37 +0000 | [diff] [blame] | 2404 | iov[0].iov_base = (char *)pSMB; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 2405 | iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4; |
Steve French | 133672e | 2007-11-13 22:41:37 +0000 | [diff] [blame] | 2406 | rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */, |
| 2407 | &resp_buf_type, timeout); |
| 2408 | pSMB = NULL; /* request buf already freed by SendReceive2. Do |
| 2409 | not try to free it twice below on exit */ |
| 2410 | pSMBr = (struct smb_com_transaction2_sfi_rsp *)iov[0].iov_base; |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 2411 | } |
| 2412 | |
Steve French | 08547b0 | 2006-02-28 22:39:25 +0000 | [diff] [blame] | 2413 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2414 | cifs_dbg(FYI, "Send error in Posix Lock = %d\n", rc); |
Jeff Layton | c5fd363 | 2012-07-23 13:28:37 -0400 | [diff] [blame] | 2415 | } else if (pLockData) { |
Steve French | fc94cdb | 2006-05-30 18:03:32 +0000 | [diff] [blame] | 2416 | /* lock structure can be returned on get */ |
| 2417 | __u16 data_offset; |
| 2418 | __u16 data_count; |
| 2419 | rc = validate_t2((struct smb_t2_rsp *)pSMBr); |
Steve French | 08547b0 | 2006-02-28 22:39:25 +0000 | [diff] [blame] | 2420 | |
Jeff Layton | 820a803 | 2011-05-04 08:05:26 -0400 | [diff] [blame] | 2421 | if (rc || get_bcc(&pSMBr->hdr) < sizeof(*parm_data)) { |
Steve French | fc94cdb | 2006-05-30 18:03:32 +0000 | [diff] [blame] | 2422 | rc = -EIO; /* bad smb */ |
| 2423 | goto plk_err_exit; |
| 2424 | } |
Steve French | fc94cdb | 2006-05-30 18:03:32 +0000 | [diff] [blame] | 2425 | data_offset = le16_to_cpu(pSMBr->t2.DataOffset); |
| 2426 | data_count = le16_to_cpu(pSMBr->t2.DataCount); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 2427 | if (data_count < sizeof(struct cifs_posix_lock)) { |
Steve French | fc94cdb | 2006-05-30 18:03:32 +0000 | [diff] [blame] | 2428 | rc = -EIO; |
| 2429 | goto plk_err_exit; |
| 2430 | } |
| 2431 | parm_data = (struct cifs_posix_lock *) |
| 2432 | ((char *)&pSMBr->hdr.Protocol + data_offset); |
Pavel Shilovsky | f05337c | 2010-04-05 09:59:14 +0400 | [diff] [blame] | 2433 | if (parm_data->lock_type == __constant_cpu_to_le16(CIFS_UNLCK)) |
Steve French | fc94cdb | 2006-05-30 18:03:32 +0000 | [diff] [blame] | 2434 | pLockData->fl_type = F_UNLCK; |
Pavel Shilovsky | f05337c | 2010-04-05 09:59:14 +0400 | [diff] [blame] | 2435 | else { |
| 2436 | if (parm_data->lock_type == |
| 2437 | __constant_cpu_to_le16(CIFS_RDLCK)) |
| 2438 | pLockData->fl_type = F_RDLCK; |
| 2439 | else if (parm_data->lock_type == |
| 2440 | __constant_cpu_to_le16(CIFS_WRLCK)) |
| 2441 | pLockData->fl_type = F_WRLCK; |
| 2442 | |
Steve French | 5443d13 | 2011-03-13 05:08:25 +0000 | [diff] [blame] | 2443 | pLockData->fl_start = le64_to_cpu(parm_data->start); |
| 2444 | pLockData->fl_end = pLockData->fl_start + |
| 2445 | le64_to_cpu(parm_data->length) - 1; |
| 2446 | pLockData->fl_pid = le32_to_cpu(parm_data->pid); |
Pavel Shilovsky | f05337c | 2010-04-05 09:59:14 +0400 | [diff] [blame] | 2447 | } |
Steve French | fc94cdb | 2006-05-30 18:03:32 +0000 | [diff] [blame] | 2448 | } |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2449 | |
Steve French | fc94cdb | 2006-05-30 18:03:32 +0000 | [diff] [blame] | 2450 | plk_err_exit: |
Steve French | 08547b0 | 2006-02-28 22:39:25 +0000 | [diff] [blame] | 2451 | if (pSMB) |
| 2452 | cifs_small_buf_release(pSMB); |
| 2453 | |
Steve French | 133672e | 2007-11-13 22:41:37 +0000 | [diff] [blame] | 2454 | if (resp_buf_type == CIFS_SMALL_BUFFER) |
| 2455 | cifs_small_buf_release(iov[0].iov_base); |
| 2456 | else if (resp_buf_type == CIFS_LARGE_BUFFER) |
| 2457 | cifs_buf_release(iov[0].iov_base); |
| 2458 | |
Steve French | 08547b0 | 2006-02-28 22:39:25 +0000 | [diff] [blame] | 2459 | /* Note: On -EAGAIN error only caller can retry on handle based calls |
| 2460 | since file handle passed in no longer valid */ |
| 2461 | |
| 2462 | return rc; |
| 2463 | } |
| 2464 | |
| 2465 | |
| 2466 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2467 | CIFSSMBClose(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2468 | { |
| 2469 | int rc = 0; |
| 2470 | CLOSE_REQ *pSMB = NULL; |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2471 | cifs_dbg(FYI, "In CIFSSMBClose\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2472 | |
| 2473 | /* do not retry on dead session on close */ |
| 2474 | rc = small_smb_init(SMB_COM_CLOSE, 3, tcon, (void **) &pSMB); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 2475 | if (rc == -EAGAIN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2476 | return 0; |
| 2477 | if (rc) |
| 2478 | return rc; |
| 2479 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2480 | pSMB->FileID = (__u16) smb_file_id; |
Steve French | b815f1e5 | 2006-10-02 05:53:29 +0000 | [diff] [blame] | 2481 | pSMB->LastWriteTime = 0xFFFFFFFF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2482 | pSMB->ByteCount = 0; |
Pavel Shilovsky | 792af7b | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 2483 | rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0); |
Pavel Shilovsky | 44c5818 | 2012-05-28 14:16:31 +0400 | [diff] [blame] | 2484 | cifs_stats_inc(&tcon->stats.cifs_stats.num_closes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2485 | if (rc) { |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 2486 | if (rc != -EINTR) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2487 | /* EINTR is expected when user ctl-c to kill app */ |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2488 | cifs_dbg(VFS, "Send error in Close = %d\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2489 | } |
| 2490 | } |
| 2491 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2492 | /* Since session is dead, file will be closed on server already */ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 2493 | if (rc == -EAGAIN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2494 | rc = 0; |
| 2495 | |
| 2496 | return rc; |
| 2497 | } |
| 2498 | |
| 2499 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2500 | CIFSSMBFlush(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id) |
Steve French | b298f22 | 2009-02-21 21:17:43 +0000 | [diff] [blame] | 2501 | { |
| 2502 | int rc = 0; |
| 2503 | FLUSH_REQ *pSMB = NULL; |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2504 | cifs_dbg(FYI, "In CIFSSMBFlush\n"); |
Steve French | b298f22 | 2009-02-21 21:17:43 +0000 | [diff] [blame] | 2505 | |
| 2506 | rc = small_smb_init(SMB_COM_FLUSH, 1, tcon, (void **) &pSMB); |
| 2507 | if (rc) |
| 2508 | return rc; |
| 2509 | |
| 2510 | pSMB->FileID = (__u16) smb_file_id; |
| 2511 | pSMB->ByteCount = 0; |
Pavel Shilovsky | 792af7b | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 2512 | rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0); |
Pavel Shilovsky | 44c5818 | 2012-05-28 14:16:31 +0400 | [diff] [blame] | 2513 | cifs_stats_inc(&tcon->stats.cifs_stats.num_flushes); |
Steve French | b298f22 | 2009-02-21 21:17:43 +0000 | [diff] [blame] | 2514 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2515 | cifs_dbg(VFS, "Send error in Flush = %d\n", rc); |
Steve French | b298f22 | 2009-02-21 21:17:43 +0000 | [diff] [blame] | 2516 | |
| 2517 | return rc; |
| 2518 | } |
| 2519 | |
| 2520 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2521 | CIFSSMBRename(const unsigned int xid, struct cifs_tcon *tcon, |
Pavel Shilovsky | 8ceb984 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 2522 | const char *from_name, const char *to_name, |
| 2523 | struct cifs_sb_info *cifs_sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2524 | { |
| 2525 | int rc = 0; |
| 2526 | RENAME_REQ *pSMB = NULL; |
| 2527 | RENAME_RSP *pSMBr = NULL; |
| 2528 | int bytes_returned; |
| 2529 | int name_len, name_len2; |
| 2530 | __u16 count; |
Pavel Shilovsky | 8ceb984 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 2531 | int remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2532 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2533 | cifs_dbg(FYI, "In CIFSSMBRename\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2534 | renameRetry: |
| 2535 | rc = smb_init(SMB_COM_RENAME, 1, tcon, (void **) &pSMB, |
| 2536 | (void **) &pSMBr); |
| 2537 | if (rc) |
| 2538 | return rc; |
| 2539 | |
| 2540 | pSMB->BufferFormat = 0x04; |
| 2541 | pSMB->SearchAttributes = |
| 2542 | cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM | |
| 2543 | ATTR_DIRECTORY); |
| 2544 | |
| 2545 | if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) { |
Pavel Shilovsky | 8ceb984 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 2546 | name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName, |
| 2547 | from_name, PATH_MAX, |
| 2548 | cifs_sb->local_nls, remap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2549 | name_len++; /* trailing null */ |
| 2550 | name_len *= 2; |
| 2551 | pSMB->OldFileName[name_len] = 0x04; /* pad */ |
| 2552 | /* protocol requires ASCII signature byte on Unicode string */ |
| 2553 | pSMB->OldFileName[name_len + 1] = 0x00; |
| 2554 | name_len2 = |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 2555 | cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2], |
Pavel Shilovsky | 8ceb984 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 2556 | to_name, PATH_MAX, cifs_sb->local_nls, |
| 2557 | remap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2558 | name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ; |
| 2559 | name_len2 *= 2; /* convert to bytes */ |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2560 | } else { /* BB improve the check for buffer overruns BB */ |
Pavel Shilovsky | 8ceb984 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 2561 | name_len = strnlen(from_name, PATH_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2562 | name_len++; /* trailing null */ |
Pavel Shilovsky | 8ceb984 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 2563 | strncpy(pSMB->OldFileName, from_name, name_len); |
| 2564 | name_len2 = strnlen(to_name, PATH_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2565 | name_len2++; /* trailing null */ |
| 2566 | pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */ |
Pavel Shilovsky | 8ceb984 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 2567 | strncpy(&pSMB->OldFileName[name_len + 1], to_name, name_len2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2568 | name_len2++; /* trailing null */ |
| 2569 | name_len2++; /* signature byte */ |
| 2570 | } |
| 2571 | |
| 2572 | count = 1 /* 1st signature byte */ + name_len + name_len2; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 2573 | inc_rfc1001_len(pSMB, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2574 | pSMB->ByteCount = cpu_to_le16(count); |
| 2575 | |
| 2576 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 2577 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
Pavel Shilovsky | 44c5818 | 2012-05-28 14:16:31 +0400 | [diff] [blame] | 2578 | cifs_stats_inc(&tcon->stats.cifs_stats.num_renames); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 2579 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2580 | cifs_dbg(FYI, "Send error in rename = %d\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2581 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2582 | cifs_buf_release(pSMB); |
| 2583 | |
| 2584 | if (rc == -EAGAIN) |
| 2585 | goto renameRetry; |
| 2586 | |
| 2587 | return rc; |
| 2588 | } |
| 2589 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2590 | int CIFSSMBRenameOpenFile(const unsigned int xid, struct cifs_tcon *pTcon, |
Jeff Layton | 391e575 | 2008-09-24 11:32:59 -0400 | [diff] [blame] | 2591 | int netfid, const char *target_name, |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2592 | const struct nls_table *nls_codepage, int remap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2593 | { |
| 2594 | struct smb_com_transaction2_sfi_req *pSMB = NULL; |
| 2595 | struct smb_com_transaction2_sfi_rsp *pSMBr = NULL; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2596 | struct set_file_rename *rename_info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2597 | char *data_offset; |
| 2598 | char dummy_string[30]; |
| 2599 | int rc = 0; |
| 2600 | int bytes_returned = 0; |
| 2601 | int len_of_str; |
| 2602 | __u16 params, param_offset, offset, count, byte_count; |
| 2603 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2604 | cifs_dbg(FYI, "Rename to File by handle\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2605 | rc = smb_init(SMB_COM_TRANSACTION2, 15, pTcon, (void **) &pSMB, |
| 2606 | (void **) &pSMBr); |
| 2607 | if (rc) |
| 2608 | return rc; |
| 2609 | |
| 2610 | params = 6; |
| 2611 | pSMB->MaxSetupCount = 0; |
| 2612 | pSMB->Reserved = 0; |
| 2613 | pSMB->Flags = 0; |
| 2614 | pSMB->Timeout = 0; |
| 2615 | pSMB->Reserved2 = 0; |
| 2616 | param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4; |
| 2617 | offset = param_offset + params; |
| 2618 | |
| 2619 | data_offset = (char *) (&pSMB->hdr.Protocol) + offset; |
| 2620 | rename_info = (struct set_file_rename *) data_offset; |
| 2621 | pSMB->MaxParameterCount = cpu_to_le16(2); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 2622 | pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2623 | pSMB->SetupCount = 1; |
| 2624 | pSMB->Reserved3 = 0; |
| 2625 | pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION); |
| 2626 | byte_count = 3 /* pad */ + params; |
| 2627 | pSMB->ParameterCount = cpu_to_le16(params); |
| 2628 | pSMB->TotalParameterCount = pSMB->ParameterCount; |
| 2629 | pSMB->ParameterOffset = cpu_to_le16(param_offset); |
| 2630 | pSMB->DataOffset = cpu_to_le16(offset); |
| 2631 | /* construct random name ".cifs_tmp<inodenum><mid>" */ |
| 2632 | rename_info->overwrite = cpu_to_le32(1); |
| 2633 | rename_info->root_fid = 0; |
| 2634 | /* unicode only call */ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 2635 | if (target_name == NULL) { |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2636 | sprintf(dummy_string, "cifs%x", pSMB->hdr.Mid); |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 2637 | len_of_str = |
| 2638 | cifsConvertToUTF16((__le16 *)rename_info->target_name, |
Steve French | 737b758 | 2005-04-28 22:41:06 -0700 | [diff] [blame] | 2639 | dummy_string, 24, nls_codepage, remap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2640 | } else { |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 2641 | len_of_str = |
| 2642 | cifsConvertToUTF16((__le16 *)rename_info->target_name, |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2643 | target_name, PATH_MAX, nls_codepage, |
| 2644 | remap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2645 | } |
| 2646 | rename_info->target_name_len = cpu_to_le32(2 * len_of_str); |
Jeff Layton | 391e575 | 2008-09-24 11:32:59 -0400 | [diff] [blame] | 2647 | count = 12 /* sizeof(struct set_file_rename) */ + (2 * len_of_str); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2648 | byte_count += count; |
| 2649 | pSMB->DataCount = cpu_to_le16(count); |
| 2650 | pSMB->TotalDataCount = pSMB->DataCount; |
| 2651 | pSMB->Fid = netfid; |
| 2652 | pSMB->InformationLevel = |
| 2653 | cpu_to_le16(SMB_SET_FILE_RENAME_INFORMATION); |
| 2654 | pSMB->Reserved4 = 0; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 2655 | inc_rfc1001_len(pSMB, byte_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2656 | pSMB->ByteCount = cpu_to_le16(byte_count); |
| 2657 | rc = SendReceive(xid, pTcon->ses, (struct smb_hdr *) pSMB, |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2658 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
Pavel Shilovsky | 44c5818 | 2012-05-28 14:16:31 +0400 | [diff] [blame] | 2659 | cifs_stats_inc(&pTcon->stats.cifs_stats.num_t2renames); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 2660 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2661 | cifs_dbg(FYI, "Send error in Rename (by file handle) = %d\n", |
| 2662 | rc); |
Steve French | a5a2b48 | 2005-08-20 21:42:53 -0700 | [diff] [blame] | 2663 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2664 | cifs_buf_release(pSMB); |
| 2665 | |
| 2666 | /* Note: On -EAGAIN error only caller can retry on handle based calls |
| 2667 | since file handle passed in no longer valid */ |
| 2668 | |
| 2669 | return rc; |
| 2670 | } |
| 2671 | |
| 2672 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2673 | CIFSSMBCopy(const unsigned int xid, struct cifs_tcon *tcon, |
| 2674 | const char *fromName, const __u16 target_tid, const char *toName, |
| 2675 | const int flags, const struct nls_table *nls_codepage, int remap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2676 | { |
| 2677 | int rc = 0; |
| 2678 | COPY_REQ *pSMB = NULL; |
| 2679 | COPY_RSP *pSMBr = NULL; |
| 2680 | int bytes_returned; |
| 2681 | int name_len, name_len2; |
| 2682 | __u16 count; |
| 2683 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2684 | cifs_dbg(FYI, "In CIFSSMBCopy\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2685 | copyRetry: |
| 2686 | rc = smb_init(SMB_COM_COPY, 1, tcon, (void **) &pSMB, |
| 2687 | (void **) &pSMBr); |
| 2688 | if (rc) |
| 2689 | return rc; |
| 2690 | |
| 2691 | pSMB->BufferFormat = 0x04; |
| 2692 | pSMB->Tid2 = target_tid; |
| 2693 | |
| 2694 | pSMB->Flags = cpu_to_le16(flags & COPY_TREE); |
| 2695 | |
| 2696 | if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) { |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 2697 | name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName, |
| 2698 | fromName, PATH_MAX, nls_codepage, |
| 2699 | remap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2700 | name_len++; /* trailing null */ |
| 2701 | name_len *= 2; |
| 2702 | pSMB->OldFileName[name_len] = 0x04; /* pad */ |
| 2703 | /* protocol requires ASCII signature byte on Unicode string */ |
| 2704 | pSMB->OldFileName[name_len + 1] = 0x00; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2705 | name_len2 = |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 2706 | cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2], |
| 2707 | toName, PATH_MAX, nls_codepage, remap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2708 | name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ; |
| 2709 | name_len2 *= 2; /* convert to bytes */ |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2710 | } else { /* BB improve the check for buffer overruns BB */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2711 | name_len = strnlen(fromName, PATH_MAX); |
| 2712 | name_len++; /* trailing null */ |
| 2713 | strncpy(pSMB->OldFileName, fromName, name_len); |
| 2714 | name_len2 = strnlen(toName, PATH_MAX); |
| 2715 | name_len2++; /* trailing null */ |
| 2716 | pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */ |
| 2717 | strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2); |
| 2718 | name_len2++; /* trailing null */ |
| 2719 | name_len2++; /* signature byte */ |
| 2720 | } |
| 2721 | |
| 2722 | count = 1 /* 1st signature byte */ + name_len + name_len2; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 2723 | inc_rfc1001_len(pSMB, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2724 | pSMB->ByteCount = cpu_to_le16(count); |
| 2725 | |
| 2726 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 2727 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
| 2728 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2729 | cifs_dbg(FYI, "Send error in copy = %d with %d files copied\n", |
| 2730 | rc, le16_to_cpu(pSMBr->CopyCount)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2731 | } |
Steve French | 0d817bc | 2008-05-22 02:02:03 +0000 | [diff] [blame] | 2732 | cifs_buf_release(pSMB); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2733 | |
| 2734 | if (rc == -EAGAIN) |
| 2735 | goto copyRetry; |
| 2736 | |
| 2737 | return rc; |
| 2738 | } |
| 2739 | |
| 2740 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2741 | CIFSUnixCreateSymLink(const unsigned int xid, struct cifs_tcon *tcon, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2742 | const char *fromName, const char *toName, |
| 2743 | const struct nls_table *nls_codepage) |
| 2744 | { |
| 2745 | TRANSACTION2_SPI_REQ *pSMB = NULL; |
| 2746 | TRANSACTION2_SPI_RSP *pSMBr = NULL; |
| 2747 | char *data_offset; |
| 2748 | int name_len; |
| 2749 | int name_len_target; |
| 2750 | int rc = 0; |
| 2751 | int bytes_returned = 0; |
| 2752 | __u16 params, param_offset, offset, byte_count; |
| 2753 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2754 | cifs_dbg(FYI, "In Symlink Unix style\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2755 | createSymLinkRetry: |
| 2756 | rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB, |
| 2757 | (void **) &pSMBr); |
| 2758 | if (rc) |
| 2759 | return rc; |
| 2760 | |
| 2761 | if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) { |
| 2762 | name_len = |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 2763 | cifs_strtoUTF16((__le16 *) pSMB->FileName, fromName, |
| 2764 | /* find define for this maxpathcomponent */ |
| 2765 | PATH_MAX, nls_codepage); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2766 | name_len++; /* trailing null */ |
| 2767 | name_len *= 2; |
| 2768 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2769 | } else { /* BB improve the check for buffer overruns BB */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2770 | name_len = strnlen(fromName, PATH_MAX); |
| 2771 | name_len++; /* trailing null */ |
| 2772 | strncpy(pSMB->FileName, fromName, name_len); |
| 2773 | } |
| 2774 | params = 6 + name_len; |
| 2775 | pSMB->MaxSetupCount = 0; |
| 2776 | pSMB->Reserved = 0; |
| 2777 | pSMB->Flags = 0; |
| 2778 | pSMB->Timeout = 0; |
| 2779 | pSMB->Reserved2 = 0; |
| 2780 | param_offset = offsetof(struct smb_com_transaction2_spi_req, |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2781 | InformationLevel) - 4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2782 | offset = param_offset + params; |
| 2783 | |
| 2784 | data_offset = (char *) (&pSMB->hdr.Protocol) + offset; |
| 2785 | if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) { |
| 2786 | name_len_target = |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 2787 | cifs_strtoUTF16((__le16 *) data_offset, toName, PATH_MAX |
| 2788 | /* find define for this maxpathcomponent */ |
| 2789 | , nls_codepage); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2790 | name_len_target++; /* trailing null */ |
| 2791 | name_len_target *= 2; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2792 | } else { /* BB improve the check for buffer overruns BB */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2793 | name_len_target = strnlen(toName, PATH_MAX); |
| 2794 | name_len_target++; /* trailing null */ |
| 2795 | strncpy(data_offset, toName, name_len_target); |
| 2796 | } |
| 2797 | |
| 2798 | pSMB->MaxParameterCount = cpu_to_le16(2); |
| 2799 | /* BB find exact max on data count below from sess */ |
| 2800 | pSMB->MaxDataCount = cpu_to_le16(1000); |
| 2801 | pSMB->SetupCount = 1; |
| 2802 | pSMB->Reserved3 = 0; |
| 2803 | pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION); |
| 2804 | byte_count = 3 /* pad */ + params + name_len_target; |
| 2805 | pSMB->DataCount = cpu_to_le16(name_len_target); |
| 2806 | pSMB->ParameterCount = cpu_to_le16(params); |
| 2807 | pSMB->TotalDataCount = pSMB->DataCount; |
| 2808 | pSMB->TotalParameterCount = pSMB->ParameterCount; |
| 2809 | pSMB->ParameterOffset = cpu_to_le16(param_offset); |
| 2810 | pSMB->DataOffset = cpu_to_le16(offset); |
| 2811 | pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_LINK); |
| 2812 | pSMB->Reserved4 = 0; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 2813 | inc_rfc1001_len(pSMB, byte_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2814 | pSMB->ByteCount = cpu_to_le16(byte_count); |
| 2815 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 2816 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
Pavel Shilovsky | 44c5818 | 2012-05-28 14:16:31 +0400 | [diff] [blame] | 2817 | cifs_stats_inc(&tcon->stats.cifs_stats.num_symlinks); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 2818 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2819 | cifs_dbg(FYI, "Send error in SetPathInfo create symlink = %d\n", |
| 2820 | rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2821 | |
Steve French | 0d817bc | 2008-05-22 02:02:03 +0000 | [diff] [blame] | 2822 | cifs_buf_release(pSMB); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2823 | |
| 2824 | if (rc == -EAGAIN) |
| 2825 | goto createSymLinkRetry; |
| 2826 | |
| 2827 | return rc; |
| 2828 | } |
| 2829 | |
| 2830 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2831 | CIFSUnixCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2832 | const char *fromName, const char *toName, |
Steve French | 737b758 | 2005-04-28 22:41:06 -0700 | [diff] [blame] | 2833 | const struct nls_table *nls_codepage, int remap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2834 | { |
| 2835 | TRANSACTION2_SPI_REQ *pSMB = NULL; |
| 2836 | TRANSACTION2_SPI_RSP *pSMBr = NULL; |
| 2837 | char *data_offset; |
| 2838 | int name_len; |
| 2839 | int name_len_target; |
| 2840 | int rc = 0; |
| 2841 | int bytes_returned = 0; |
| 2842 | __u16 params, param_offset, offset, byte_count; |
| 2843 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2844 | cifs_dbg(FYI, "In Create Hard link Unix style\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2845 | createHardLinkRetry: |
| 2846 | rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB, |
| 2847 | (void **) &pSMBr); |
| 2848 | if (rc) |
| 2849 | return rc; |
| 2850 | |
| 2851 | if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) { |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 2852 | name_len = cifsConvertToUTF16((__le16 *) pSMB->FileName, toName, |
| 2853 | PATH_MAX, nls_codepage, remap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2854 | name_len++; /* trailing null */ |
| 2855 | name_len *= 2; |
| 2856 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2857 | } else { /* BB improve the check for buffer overruns BB */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2858 | name_len = strnlen(toName, PATH_MAX); |
| 2859 | name_len++; /* trailing null */ |
| 2860 | strncpy(pSMB->FileName, toName, name_len); |
| 2861 | } |
| 2862 | params = 6 + name_len; |
| 2863 | pSMB->MaxSetupCount = 0; |
| 2864 | pSMB->Reserved = 0; |
| 2865 | pSMB->Flags = 0; |
| 2866 | pSMB->Timeout = 0; |
| 2867 | pSMB->Reserved2 = 0; |
| 2868 | param_offset = offsetof(struct smb_com_transaction2_spi_req, |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2869 | InformationLevel) - 4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2870 | offset = param_offset + params; |
| 2871 | |
| 2872 | data_offset = (char *) (&pSMB->hdr.Protocol) + offset; |
| 2873 | if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) { |
| 2874 | name_len_target = |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 2875 | cifsConvertToUTF16((__le16 *) data_offset, fromName, |
| 2876 | PATH_MAX, nls_codepage, remap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2877 | name_len_target++; /* trailing null */ |
| 2878 | name_len_target *= 2; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2879 | } else { /* BB improve the check for buffer overruns BB */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2880 | name_len_target = strnlen(fromName, PATH_MAX); |
| 2881 | name_len_target++; /* trailing null */ |
| 2882 | strncpy(data_offset, fromName, name_len_target); |
| 2883 | } |
| 2884 | |
| 2885 | pSMB->MaxParameterCount = cpu_to_le16(2); |
| 2886 | /* BB find exact max on data count below from sess*/ |
| 2887 | pSMB->MaxDataCount = cpu_to_le16(1000); |
| 2888 | pSMB->SetupCount = 1; |
| 2889 | pSMB->Reserved3 = 0; |
| 2890 | pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION); |
| 2891 | byte_count = 3 /* pad */ + params + name_len_target; |
| 2892 | pSMB->ParameterCount = cpu_to_le16(params); |
| 2893 | pSMB->TotalParameterCount = pSMB->ParameterCount; |
| 2894 | pSMB->DataCount = cpu_to_le16(name_len_target); |
| 2895 | pSMB->TotalDataCount = pSMB->DataCount; |
| 2896 | pSMB->ParameterOffset = cpu_to_le16(param_offset); |
| 2897 | pSMB->DataOffset = cpu_to_le16(offset); |
| 2898 | pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_HLINK); |
| 2899 | pSMB->Reserved4 = 0; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 2900 | inc_rfc1001_len(pSMB, byte_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2901 | pSMB->ByteCount = cpu_to_le16(byte_count); |
| 2902 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 2903 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
Pavel Shilovsky | 44c5818 | 2012-05-28 14:16:31 +0400 | [diff] [blame] | 2904 | cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 2905 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2906 | cifs_dbg(FYI, "Send error in SetPathInfo (hard link) = %d\n", |
| 2907 | rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2908 | |
| 2909 | cifs_buf_release(pSMB); |
| 2910 | if (rc == -EAGAIN) |
| 2911 | goto createHardLinkRetry; |
| 2912 | |
| 2913 | return rc; |
| 2914 | } |
| 2915 | |
| 2916 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2917 | CIFSCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon, |
Steve French | d6e906f | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 2918 | const char *from_name, const char *to_name, |
| 2919 | struct cifs_sb_info *cifs_sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2920 | { |
| 2921 | int rc = 0; |
| 2922 | NT_RENAME_REQ *pSMB = NULL; |
| 2923 | RENAME_RSP *pSMBr = NULL; |
| 2924 | int bytes_returned; |
| 2925 | int name_len, name_len2; |
| 2926 | __u16 count; |
Steve French | d6e906f | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 2927 | int remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2928 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2929 | cifs_dbg(FYI, "In CIFSCreateHardLink\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2930 | winCreateHardLinkRetry: |
| 2931 | |
| 2932 | rc = smb_init(SMB_COM_NT_RENAME, 4, tcon, (void **) &pSMB, |
| 2933 | (void **) &pSMBr); |
| 2934 | if (rc) |
| 2935 | return rc; |
| 2936 | |
| 2937 | pSMB->SearchAttributes = |
| 2938 | cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM | |
| 2939 | ATTR_DIRECTORY); |
| 2940 | pSMB->Flags = cpu_to_le16(CREATE_HARD_LINK); |
| 2941 | pSMB->ClusterCount = 0; |
| 2942 | |
| 2943 | pSMB->BufferFormat = 0x04; |
| 2944 | |
| 2945 | if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) { |
| 2946 | name_len = |
Steve French | d6e906f | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 2947 | cifsConvertToUTF16((__le16 *) pSMB->OldFileName, from_name, |
| 2948 | PATH_MAX, cifs_sb->local_nls, remap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2949 | name_len++; /* trailing null */ |
| 2950 | name_len *= 2; |
Jeff Layton | fcc7c09 | 2009-02-28 12:59:03 -0500 | [diff] [blame] | 2951 | |
| 2952 | /* protocol specifies ASCII buffer format (0x04) for unicode */ |
| 2953 | pSMB->OldFileName[name_len] = 0x04; |
| 2954 | pSMB->OldFileName[name_len + 1] = 0x00; /* pad */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2955 | name_len2 = |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 2956 | cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2], |
Steve French | d6e906f | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 2957 | to_name, PATH_MAX, cifs_sb->local_nls, |
| 2958 | remap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2959 | name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ; |
| 2960 | name_len2 *= 2; /* convert to bytes */ |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2961 | } else { /* BB improve the check for buffer overruns BB */ |
Steve French | d6e906f | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 2962 | name_len = strnlen(from_name, PATH_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2963 | name_len++; /* trailing null */ |
Steve French | d6e906f | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 2964 | strncpy(pSMB->OldFileName, from_name, name_len); |
| 2965 | name_len2 = strnlen(to_name, PATH_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2966 | name_len2++; /* trailing null */ |
| 2967 | pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */ |
Steve French | d6e906f | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 2968 | strncpy(&pSMB->OldFileName[name_len + 1], to_name, name_len2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2969 | name_len2++; /* trailing null */ |
| 2970 | name_len2++; /* signature byte */ |
| 2971 | } |
| 2972 | |
| 2973 | count = 1 /* string type byte */ + name_len + name_len2; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 2974 | inc_rfc1001_len(pSMB, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2975 | pSMB->ByteCount = cpu_to_le16(count); |
| 2976 | |
| 2977 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 2978 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
Pavel Shilovsky | 44c5818 | 2012-05-28 14:16:31 +0400 | [diff] [blame] | 2979 | cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 2980 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2981 | cifs_dbg(FYI, "Send error in hard link (NT rename) = %d\n", rc); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 2982 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2983 | cifs_buf_release(pSMB); |
| 2984 | if (rc == -EAGAIN) |
| 2985 | goto winCreateHardLinkRetry; |
| 2986 | |
| 2987 | return rc; |
| 2988 | } |
| 2989 | |
| 2990 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2991 | CIFSSMBUnixQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon, |
Jeff Layton | 460b969 | 2009-04-30 07:17:56 -0400 | [diff] [blame] | 2992 | const unsigned char *searchName, char **symlinkinfo, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2993 | const struct nls_table *nls_codepage) |
| 2994 | { |
| 2995 | /* SMB_QUERY_FILE_UNIX_LINK */ |
| 2996 | TRANSACTION2_QPI_REQ *pSMB = NULL; |
| 2997 | TRANSACTION2_QPI_RSP *pSMBr = NULL; |
| 2998 | int rc = 0; |
| 2999 | int bytes_returned; |
| 3000 | int name_len; |
| 3001 | __u16 params, byte_count; |
Jeff Layton | 460b969 | 2009-04-30 07:17:56 -0400 | [diff] [blame] | 3002 | char *data_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3003 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3004 | cifs_dbg(FYI, "In QPathSymLinkInfo (Unix) for path %s\n", searchName); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3005 | |
| 3006 | querySymLinkRetry: |
| 3007 | rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB, |
| 3008 | (void **) &pSMBr); |
| 3009 | if (rc) |
| 3010 | return rc; |
| 3011 | |
| 3012 | if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) { |
| 3013 | name_len = |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 3014 | cifs_strtoUTF16((__le16 *) pSMB->FileName, searchName, |
| 3015 | PATH_MAX, nls_codepage); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3016 | name_len++; /* trailing null */ |
| 3017 | name_len *= 2; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3018 | } else { /* BB improve the check for buffer overruns BB */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3019 | name_len = strnlen(searchName, PATH_MAX); |
| 3020 | name_len++; /* trailing null */ |
| 3021 | strncpy(pSMB->FileName, searchName, name_len); |
| 3022 | } |
| 3023 | |
| 3024 | params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ; |
| 3025 | pSMB->TotalDataCount = 0; |
| 3026 | pSMB->MaxParameterCount = cpu_to_le16(2); |
Jeff Layton | 46a7574 | 2009-05-24 18:45:17 -0400 | [diff] [blame] | 3027 | pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3028 | pSMB->MaxSetupCount = 0; |
| 3029 | pSMB->Reserved = 0; |
| 3030 | pSMB->Flags = 0; |
| 3031 | pSMB->Timeout = 0; |
| 3032 | pSMB->Reserved2 = 0; |
| 3033 | pSMB->ParameterOffset = cpu_to_le16(offsetof( |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3034 | struct smb_com_transaction2_qpi_req, InformationLevel) - 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3035 | pSMB->DataCount = 0; |
| 3036 | pSMB->DataOffset = 0; |
| 3037 | pSMB->SetupCount = 1; |
| 3038 | pSMB->Reserved3 = 0; |
| 3039 | pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION); |
| 3040 | byte_count = params + 1 /* pad */ ; |
| 3041 | pSMB->TotalParameterCount = cpu_to_le16(params); |
| 3042 | pSMB->ParameterCount = pSMB->TotalParameterCount; |
| 3043 | pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_LINK); |
| 3044 | pSMB->Reserved4 = 0; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 3045 | inc_rfc1001_len(pSMB, byte_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3046 | pSMB->ByteCount = cpu_to_le16(byte_count); |
| 3047 | |
| 3048 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 3049 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
| 3050 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3051 | cifs_dbg(FYI, "Send error in QuerySymLinkInfo = %d\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3052 | } else { |
| 3053 | /* decode response */ |
| 3054 | |
| 3055 | rc = validate_t2((struct smb_t2_rsp *)pSMBr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3056 | /* BB also check enough total bytes returned */ |
Jeff Layton | 820a803 | 2011-05-04 08:05:26 -0400 | [diff] [blame] | 3057 | if (rc || get_bcc(&pSMBr->hdr) < 2) |
Jeff Layton | 460b969 | 2009-04-30 07:17:56 -0400 | [diff] [blame] | 3058 | rc = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3059 | else { |
Steve French | 0e0d2cf | 2009-05-01 05:27:32 +0000 | [diff] [blame] | 3060 | bool is_unicode; |
Jeff Layton | 460b969 | 2009-04-30 07:17:56 -0400 | [diff] [blame] | 3061 | u16 count = le16_to_cpu(pSMBr->t2.DataCount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3062 | |
Jeff Layton | 460b969 | 2009-04-30 07:17:56 -0400 | [diff] [blame] | 3063 | data_start = ((char *) &pSMBr->hdr.Protocol) + |
| 3064 | le16_to_cpu(pSMBr->t2.DataOffset); |
| 3065 | |
Steve French | 0e0d2cf | 2009-05-01 05:27:32 +0000 | [diff] [blame] | 3066 | if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE) |
| 3067 | is_unicode = true; |
| 3068 | else |
| 3069 | is_unicode = false; |
| 3070 | |
Steve French | 737b758 | 2005-04-28 22:41:06 -0700 | [diff] [blame] | 3071 | /* BB FIXME investigate remapping reserved chars here */ |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 3072 | *symlinkinfo = cifs_strndup_from_utf16(data_start, |
| 3073 | count, is_unicode, nls_codepage); |
Jeff Layton | 8b6427a | 2009-05-19 09:57:03 -0400 | [diff] [blame] | 3074 | if (!*symlinkinfo) |
Jeff Layton | 460b969 | 2009-04-30 07:17:56 -0400 | [diff] [blame] | 3075 | rc = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3076 | } |
| 3077 | } |
| 3078 | cifs_buf_release(pSMB); |
| 3079 | if (rc == -EAGAIN) |
| 3080 | goto querySymLinkRetry; |
| 3081 | return rc; |
| 3082 | } |
| 3083 | |
Steve French | c52a955 | 2011-02-24 06:16:22 +0000 | [diff] [blame] | 3084 | /* |
| 3085 | * Recent Windows versions now create symlinks more frequently |
| 3086 | * and they use the "reparse point" mechanism below. We can of course |
| 3087 | * do symlinks nicely to Samba and other servers which support the |
| 3088 | * CIFS Unix Extensions and we can also do SFU symlinks and "client only" |
| 3089 | * "MF" symlinks optionally, but for recent Windows we really need to |
| 3090 | * reenable the code below and fix the cifs_symlink callers to handle this. |
| 3091 | * In the interim this code has been moved to its own config option so |
| 3092 | * it is not compiled in by default until callers fixed up and more tested. |
| 3093 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3094 | int |
Pavel Shilovsky | d244bf2 | 2013-08-14 19:25:22 +0400 | [diff] [blame] | 3095 | CIFSSMBQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon, |
| 3096 | __u16 fid, char **symlinkinfo, |
| 3097 | const struct nls_table *nls_codepage) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3098 | { |
| 3099 | int rc = 0; |
| 3100 | int bytes_returned; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3101 | struct smb_com_transaction_ioctl_req *pSMB; |
| 3102 | struct smb_com_transaction_ioctl_rsp *pSMBr; |
Pavel Shilovsky | d244bf2 | 2013-08-14 19:25:22 +0400 | [diff] [blame] | 3103 | bool is_unicode; |
| 3104 | unsigned int sub_len; |
| 3105 | char *sub_start; |
Steve French | c31f330 | 2013-09-28 18:24:12 -0500 | [diff] [blame] | 3106 | struct reparse_symlink_data *reparse_buf; |
| 3107 | struct reparse_posix_data *posix_buf; |
Pavel Shilovsky | d244bf2 | 2013-08-14 19:25:22 +0400 | [diff] [blame] | 3108 | __u32 data_offset, data_count; |
| 3109 | char *end_of_smb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3110 | |
Pavel Shilovsky | d244bf2 | 2013-08-14 19:25:22 +0400 | [diff] [blame] | 3111 | cifs_dbg(FYI, "In Windows reparse style QueryLink for fid %u\n", fid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3112 | rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB, |
| 3113 | (void **) &pSMBr); |
| 3114 | if (rc) |
| 3115 | return rc; |
| 3116 | |
| 3117 | pSMB->TotalParameterCount = 0 ; |
| 3118 | pSMB->TotalDataCount = 0; |
| 3119 | pSMB->MaxParameterCount = cpu_to_le32(2); |
| 3120 | /* BB find exact data count max from sess structure BB */ |
Jeff Layton | c974bef | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 3121 | pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3122 | pSMB->MaxSetupCount = 4; |
| 3123 | pSMB->Reserved = 0; |
| 3124 | pSMB->ParameterOffset = 0; |
| 3125 | pSMB->DataCount = 0; |
| 3126 | pSMB->DataOffset = 0; |
| 3127 | pSMB->SetupCount = 4; |
| 3128 | pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL); |
| 3129 | pSMB->ParameterCount = pSMB->TotalParameterCount; |
| 3130 | pSMB->FunctionCode = cpu_to_le32(FSCTL_GET_REPARSE_POINT); |
| 3131 | pSMB->IsFsctl = 1; /* FSCTL */ |
| 3132 | pSMB->IsRootFlag = 0; |
| 3133 | pSMB->Fid = fid; /* file handle always le */ |
| 3134 | pSMB->ByteCount = 0; |
| 3135 | |
| 3136 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 3137 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
| 3138 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3139 | cifs_dbg(FYI, "Send error in QueryReparseLinkInfo = %d\n", rc); |
Pavel Shilovsky | d244bf2 | 2013-08-14 19:25:22 +0400 | [diff] [blame] | 3140 | goto qreparse_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3141 | } |
Steve French | 989c7e5 | 2009-05-02 05:32:20 +0000 | [diff] [blame] | 3142 | |
Pavel Shilovsky | d244bf2 | 2013-08-14 19:25:22 +0400 | [diff] [blame] | 3143 | data_offset = le32_to_cpu(pSMBr->DataOffset); |
| 3144 | data_count = le32_to_cpu(pSMBr->DataCount); |
| 3145 | if (get_bcc(&pSMBr->hdr) < 2 || data_offset > 512) { |
| 3146 | /* BB also check enough total bytes returned */ |
| 3147 | rc = -EIO; /* bad smb */ |
| 3148 | goto qreparse_out; |
| 3149 | } |
| 3150 | if (!data_count || (data_count > 2048)) { |
| 3151 | rc = -EIO; |
| 3152 | cifs_dbg(FYI, "Invalid return data count on get reparse info ioctl\n"); |
| 3153 | goto qreparse_out; |
| 3154 | } |
| 3155 | end_of_smb = 2 + get_bcc(&pSMBr->hdr) + (char *)&pSMBr->ByteCount; |
Steve French | c31f330 | 2013-09-28 18:24:12 -0500 | [diff] [blame] | 3156 | reparse_buf = (struct reparse_symlink_data *) |
Pavel Shilovsky | d244bf2 | 2013-08-14 19:25:22 +0400 | [diff] [blame] | 3157 | ((char *)&pSMBr->hdr.Protocol + data_offset); |
| 3158 | if ((char *)reparse_buf >= end_of_smb) { |
| 3159 | rc = -EIO; |
| 3160 | goto qreparse_out; |
| 3161 | } |
Steve French | c31f330 | 2013-09-28 18:24:12 -0500 | [diff] [blame] | 3162 | if (reparse_buf->ReparseTag == cpu_to_le32(IO_REPARSE_TAG_NFS)) { |
| 3163 | cifs_dbg(FYI, "NFS style reparse tag\n"); |
| 3164 | posix_buf = (struct reparse_posix_data *)reparse_buf; |
| 3165 | |
| 3166 | if (posix_buf->InodeType != cpu_to_le64(NFS_SPECFILE_LNK)) { |
| 3167 | cifs_dbg(FYI, "unsupported file type 0x%llx\n", |
| 3168 | le64_to_cpu(posix_buf->InodeType)); |
| 3169 | rc = -EOPNOTSUPP; |
| 3170 | goto qreparse_out; |
| 3171 | } |
| 3172 | is_unicode = true; |
| 3173 | sub_len = le16_to_cpu(reparse_buf->ReparseDataLength); |
| 3174 | if (posix_buf->PathBuffer + sub_len > end_of_smb) { |
| 3175 | cifs_dbg(FYI, "reparse buf beyond SMB\n"); |
| 3176 | rc = -EIO; |
| 3177 | goto qreparse_out; |
| 3178 | } |
| 3179 | *symlinkinfo = cifs_strndup_from_utf16(posix_buf->PathBuffer, |
| 3180 | sub_len, is_unicode, nls_codepage); |
| 3181 | goto qreparse_out; |
| 3182 | } else if (reparse_buf->ReparseTag != |
| 3183 | cpu_to_le32(IO_REPARSE_TAG_SYMLINK)) { |
| 3184 | rc = -EOPNOTSUPP; |
| 3185 | goto qreparse_out; |
| 3186 | } |
| 3187 | |
| 3188 | /* Reparse tag is NTFS symlink */ |
| 3189 | sub_start = le16_to_cpu(reparse_buf->SubstituteNameOffset) + |
| 3190 | reparse_buf->PathBuffer; |
| 3191 | sub_len = le16_to_cpu(reparse_buf->SubstituteNameLength); |
| 3192 | if (sub_start + sub_len > end_of_smb) { |
Pavel Shilovsky | d244bf2 | 2013-08-14 19:25:22 +0400 | [diff] [blame] | 3193 | cifs_dbg(FYI, "reparse buf beyond SMB\n"); |
| 3194 | rc = -EIO; |
| 3195 | goto qreparse_out; |
| 3196 | } |
Pavel Shilovsky | d244bf2 | 2013-08-14 19:25:22 +0400 | [diff] [blame] | 3197 | if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE) |
| 3198 | is_unicode = true; |
| 3199 | else |
| 3200 | is_unicode = false; |
| 3201 | |
| 3202 | /* BB FIXME investigate remapping reserved chars here */ |
| 3203 | *symlinkinfo = cifs_strndup_from_utf16(sub_start, sub_len, is_unicode, |
| 3204 | nls_codepage); |
| 3205 | if (!*symlinkinfo) |
| 3206 | rc = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3207 | qreparse_out: |
Steve French | 4a6d87f | 2005-08-13 08:15:54 -0700 | [diff] [blame] | 3208 | cifs_buf_release(pSMB); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3209 | |
Pavel Shilovsky | d244bf2 | 2013-08-14 19:25:22 +0400 | [diff] [blame] | 3210 | /* |
| 3211 | * Note: On -EAGAIN error only caller can retry on handle based calls |
| 3212 | * since file handle passed in no longer valid. |
| 3213 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3214 | return rc; |
| 3215 | } |
| 3216 | |
Steve French | c7f508a | 2013-10-14 15:27:32 -0500 | [diff] [blame] | 3217 | int |
| 3218 | CIFSSMB_set_compression(const unsigned int xid, struct cifs_tcon *tcon, |
| 3219 | __u16 fid) |
| 3220 | { |
| 3221 | int rc = 0; |
| 3222 | int bytes_returned; |
| 3223 | struct smb_com_transaction_compr_ioctl_req *pSMB; |
| 3224 | struct smb_com_transaction_ioctl_rsp *pSMBr; |
| 3225 | |
| 3226 | cifs_dbg(FYI, "Set compression for %u\n", fid); |
| 3227 | rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB, |
| 3228 | (void **) &pSMBr); |
| 3229 | if (rc) |
| 3230 | return rc; |
| 3231 | |
| 3232 | pSMB->compression_state = cpu_to_le16(COMPRESSION_FORMAT_DEFAULT); |
| 3233 | |
| 3234 | pSMB->TotalParameterCount = 0; |
| 3235 | pSMB->TotalDataCount = __constant_cpu_to_le32(2); |
| 3236 | pSMB->MaxParameterCount = 0; |
| 3237 | pSMB->MaxDataCount = 0; |
| 3238 | pSMB->MaxSetupCount = 4; |
| 3239 | pSMB->Reserved = 0; |
| 3240 | pSMB->ParameterOffset = 0; |
| 3241 | pSMB->DataCount = __constant_cpu_to_le32(2); |
| 3242 | pSMB->DataOffset = |
| 3243 | cpu_to_le32(offsetof(struct smb_com_transaction_compr_ioctl_req, |
| 3244 | compression_state) - 4); /* 84 */ |
| 3245 | pSMB->SetupCount = 4; |
| 3246 | pSMB->SubCommand = __constant_cpu_to_le16(NT_TRANSACT_IOCTL); |
| 3247 | pSMB->ParameterCount = 0; |
| 3248 | pSMB->FunctionCode = __constant_cpu_to_le32(FSCTL_SET_COMPRESSION); |
| 3249 | pSMB->IsFsctl = 1; /* FSCTL */ |
| 3250 | pSMB->IsRootFlag = 0; |
| 3251 | pSMB->Fid = fid; /* file handle always le */ |
| 3252 | /* 3 byte pad, followed by 2 byte compress state */ |
| 3253 | pSMB->ByteCount = __constant_cpu_to_le16(5); |
| 3254 | inc_rfc1001_len(pSMB, 5); |
| 3255 | |
| 3256 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 3257 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
| 3258 | if (rc) |
| 3259 | cifs_dbg(FYI, "Send error in SetCompression = %d\n", rc); |
| 3260 | |
| 3261 | cifs_buf_release(pSMB); |
| 3262 | |
| 3263 | /* |
| 3264 | * Note: On -EAGAIN error only caller can retry on handle based calls |
| 3265 | * since file handle passed in no longer valid. |
| 3266 | */ |
| 3267 | return rc; |
| 3268 | } |
| 3269 | |
| 3270 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3271 | #ifdef CONFIG_CIFS_POSIX |
| 3272 | |
| 3273 | /*Convert an Access Control Entry from wire format to local POSIX xattr format*/ |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3274 | static void cifs_convert_ace(posix_acl_xattr_entry *ace, |
| 3275 | struct cifs_posix_ace *cifs_ace) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3276 | { |
| 3277 | /* u8 cifs fields do not need le conversion */ |
Steve French | ff7feac | 2005-11-15 16:45:16 -0800 | [diff] [blame] | 3278 | ace->e_perm = cpu_to_le16(cifs_ace->cifs_e_perm); |
| 3279 | ace->e_tag = cpu_to_le16(cifs_ace->cifs_e_tag); |
| 3280 | ace->e_id = cpu_to_le32(le64_to_cpu(cifs_ace->cifs_uid)); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3281 | /* |
| 3282 | cifs_dbg(FYI, "perm %d tag %d id %d\n", |
| 3283 | ace->e_perm, ace->e_tag, ace->e_id); |
| 3284 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3285 | |
| 3286 | return; |
| 3287 | } |
| 3288 | |
| 3289 | /* Convert ACL from CIFS POSIX wire format to local Linux POSIX ACL xattr */ |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3290 | static int cifs_copy_posix_acl(char *trgt, char *src, const int buflen, |
| 3291 | const int acl_type, const int size_of_data_area) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3292 | { |
| 3293 | int size = 0; |
| 3294 | int i; |
| 3295 | __u16 count; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3296 | struct cifs_posix_ace *pACE; |
| 3297 | struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)src; |
| 3298 | posix_acl_xattr_header *local_acl = (posix_acl_xattr_header *)trgt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3299 | |
| 3300 | if (le16_to_cpu(cifs_acl->version) != CIFS_ACL_VERSION) |
| 3301 | return -EOPNOTSUPP; |
| 3302 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 3303 | if (acl_type & ACL_TYPE_ACCESS) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3304 | count = le16_to_cpu(cifs_acl->access_entry_count); |
| 3305 | pACE = &cifs_acl->ace_array[0]; |
| 3306 | size = sizeof(struct cifs_posix_acl); |
| 3307 | size += sizeof(struct cifs_posix_ace) * count; |
| 3308 | /* check if we would go beyond end of SMB */ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 3309 | if (size_of_data_area < size) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3310 | cifs_dbg(FYI, "bad CIFS POSIX ACL size %d vs. %d\n", |
| 3311 | size_of_data_area, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3312 | return -EINVAL; |
| 3313 | } |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 3314 | } else if (acl_type & ACL_TYPE_DEFAULT) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3315 | count = le16_to_cpu(cifs_acl->access_entry_count); |
| 3316 | size = sizeof(struct cifs_posix_acl); |
| 3317 | size += sizeof(struct cifs_posix_ace) * count; |
| 3318 | /* skip past access ACEs to get to default ACEs */ |
| 3319 | pACE = &cifs_acl->ace_array[count]; |
| 3320 | count = le16_to_cpu(cifs_acl->default_entry_count); |
| 3321 | size += sizeof(struct cifs_posix_ace) * count; |
| 3322 | /* check if we would go beyond end of SMB */ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 3323 | if (size_of_data_area < size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3324 | return -EINVAL; |
| 3325 | } else { |
| 3326 | /* illegal type */ |
| 3327 | return -EINVAL; |
| 3328 | } |
| 3329 | |
| 3330 | size = posix_acl_xattr_size(count); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 3331 | if ((buflen == 0) || (local_acl == NULL)) { |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3332 | /* used to query ACL EA size */ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 3333 | } else if (size > buflen) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3334 | return -ERANGE; |
| 3335 | } else /* buffer big enough */ { |
Steve French | ff7feac | 2005-11-15 16:45:16 -0800 | [diff] [blame] | 3336 | local_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3337 | for (i = 0; i < count ; i++) { |
| 3338 | cifs_convert_ace(&local_acl->a_entries[i], pACE); |
| 3339 | pACE++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3340 | } |
| 3341 | } |
| 3342 | return size; |
| 3343 | } |
| 3344 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3345 | static __u16 convert_ace_to_cifs_ace(struct cifs_posix_ace *cifs_ace, |
| 3346 | const posix_acl_xattr_entry *local_ace) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3347 | { |
| 3348 | __u16 rc = 0; /* 0 = ACL converted ok */ |
| 3349 | |
Steve French | ff7feac | 2005-11-15 16:45:16 -0800 | [diff] [blame] | 3350 | cifs_ace->cifs_e_perm = le16_to_cpu(local_ace->e_perm); |
| 3351 | cifs_ace->cifs_e_tag = le16_to_cpu(local_ace->e_tag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3352 | /* BB is there a better way to handle the large uid? */ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 3353 | if (local_ace->e_id == cpu_to_le32(-1)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3354 | /* Probably no need to le convert -1 on any arch but can not hurt */ |
| 3355 | cifs_ace->cifs_uid = cpu_to_le64(-1); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3356 | } else |
Steve French | ff7feac | 2005-11-15 16:45:16 -0800 | [diff] [blame] | 3357 | cifs_ace->cifs_uid = cpu_to_le64(le32_to_cpu(local_ace->e_id)); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3358 | /* |
| 3359 | cifs_dbg(FYI, "perm %d tag %d id %d\n", |
| 3360 | ace->e_perm, ace->e_tag, ace->e_id); |
| 3361 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3362 | return rc; |
| 3363 | } |
| 3364 | |
| 3365 | /* Convert ACL from local Linux POSIX xattr to CIFS POSIX ACL wire format */ |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3366 | static __u16 ACL_to_cifs_posix(char *parm_data, const char *pACL, |
| 3367 | const int buflen, const int acl_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3368 | { |
| 3369 | __u16 rc = 0; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3370 | struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)parm_data; |
| 3371 | posix_acl_xattr_header *local_acl = (posix_acl_xattr_header *)pACL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3372 | int count; |
| 3373 | int i; |
| 3374 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 3375 | if ((buflen == 0) || (pACL == NULL) || (cifs_acl == NULL)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3376 | return 0; |
| 3377 | |
| 3378 | count = posix_acl_xattr_count((size_t)buflen); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3379 | cifs_dbg(FYI, "setting acl with %d entries from buf of length %d and version of %d\n", |
| 3380 | count, buflen, le32_to_cpu(local_acl->a_version)); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 3381 | if (le32_to_cpu(local_acl->a_version) != 2) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3382 | cifs_dbg(FYI, "unknown POSIX ACL version %d\n", |
| 3383 | le32_to_cpu(local_acl->a_version)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3384 | return 0; |
| 3385 | } |
| 3386 | cifs_acl->version = cpu_to_le16(1); |
Steve French | b1d9335 | 2013-11-15 20:41:32 -0600 | [diff] [blame] | 3387 | if (acl_type == ACL_TYPE_ACCESS) { |
Steve French | ff7feac | 2005-11-15 16:45:16 -0800 | [diff] [blame] | 3388 | cifs_acl->access_entry_count = cpu_to_le16(count); |
Steve French | b1d9335 | 2013-11-15 20:41:32 -0600 | [diff] [blame] | 3389 | cifs_acl->default_entry_count = __constant_cpu_to_le16(0xFFFF); |
| 3390 | } else if (acl_type == ACL_TYPE_DEFAULT) { |
Steve French | ff7feac | 2005-11-15 16:45:16 -0800 | [diff] [blame] | 3391 | cifs_acl->default_entry_count = cpu_to_le16(count); |
Steve French | b1d9335 | 2013-11-15 20:41:32 -0600 | [diff] [blame] | 3392 | cifs_acl->access_entry_count = __constant_cpu_to_le16(0xFFFF); |
| 3393 | } else { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3394 | cifs_dbg(FYI, "unknown ACL type %d\n", acl_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3395 | return 0; |
| 3396 | } |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3397 | for (i = 0; i < count; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3398 | rc = convert_ace_to_cifs_ace(&cifs_acl->ace_array[i], |
| 3399 | &local_acl->a_entries[i]); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 3400 | if (rc != 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3401 | /* ACE not converted */ |
| 3402 | break; |
| 3403 | } |
| 3404 | } |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 3405 | if (rc == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3406 | rc = (__u16)(count * sizeof(struct cifs_posix_ace)); |
| 3407 | rc += sizeof(struct cifs_posix_acl); |
| 3408 | /* BB add check to make sure ACL does not overflow SMB */ |
| 3409 | } |
| 3410 | return rc; |
| 3411 | } |
| 3412 | |
| 3413 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 3414 | CIFSSMBGetPosixACL(const unsigned int xid, struct cifs_tcon *tcon, |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3415 | const unsigned char *searchName, |
| 3416 | char *acl_inf, const int buflen, const int acl_type, |
| 3417 | const struct nls_table *nls_codepage, int remap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3418 | { |
| 3419 | /* SMB_QUERY_POSIX_ACL */ |
| 3420 | TRANSACTION2_QPI_REQ *pSMB = NULL; |
| 3421 | TRANSACTION2_QPI_RSP *pSMBr = NULL; |
| 3422 | int rc = 0; |
| 3423 | int bytes_returned; |
| 3424 | int name_len; |
| 3425 | __u16 params, byte_count; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3426 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3427 | cifs_dbg(FYI, "In GetPosixACL (Unix) for path %s\n", searchName); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3428 | |
| 3429 | queryAclRetry: |
| 3430 | rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB, |
| 3431 | (void **) &pSMBr); |
| 3432 | if (rc) |
| 3433 | return rc; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3434 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3435 | if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) { |
| 3436 | name_len = |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 3437 | cifsConvertToUTF16((__le16 *) pSMB->FileName, |
| 3438 | searchName, PATH_MAX, nls_codepage, |
| 3439 | remap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3440 | name_len++; /* trailing null */ |
| 3441 | name_len *= 2; |
| 3442 | pSMB->FileName[name_len] = 0; |
| 3443 | pSMB->FileName[name_len+1] = 0; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3444 | } else { /* BB improve the check for buffer overruns BB */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3445 | name_len = strnlen(searchName, PATH_MAX); |
| 3446 | name_len++; /* trailing null */ |
| 3447 | strncpy(pSMB->FileName, searchName, name_len); |
| 3448 | } |
| 3449 | |
| 3450 | params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ; |
| 3451 | pSMB->TotalDataCount = 0; |
| 3452 | pSMB->MaxParameterCount = cpu_to_le16(2); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3453 | /* BB find exact max data count below from sess structure BB */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3454 | pSMB->MaxDataCount = cpu_to_le16(4000); |
| 3455 | pSMB->MaxSetupCount = 0; |
| 3456 | pSMB->Reserved = 0; |
| 3457 | pSMB->Flags = 0; |
| 3458 | pSMB->Timeout = 0; |
| 3459 | pSMB->Reserved2 = 0; |
| 3460 | pSMB->ParameterOffset = cpu_to_le16( |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3461 | offsetof(struct smb_com_transaction2_qpi_req, |
| 3462 | InformationLevel) - 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3463 | pSMB->DataCount = 0; |
| 3464 | pSMB->DataOffset = 0; |
| 3465 | pSMB->SetupCount = 1; |
| 3466 | pSMB->Reserved3 = 0; |
| 3467 | pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION); |
| 3468 | byte_count = params + 1 /* pad */ ; |
| 3469 | pSMB->TotalParameterCount = cpu_to_le16(params); |
| 3470 | pSMB->ParameterCount = pSMB->TotalParameterCount; |
| 3471 | pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_ACL); |
| 3472 | pSMB->Reserved4 = 0; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 3473 | inc_rfc1001_len(pSMB, byte_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3474 | pSMB->ByteCount = cpu_to_le16(byte_count); |
| 3475 | |
| 3476 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 3477 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
Pavel Shilovsky | 44c5818 | 2012-05-28 14:16:31 +0400 | [diff] [blame] | 3478 | cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3479 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3480 | cifs_dbg(FYI, "Send error in Query POSIX ACL = %d\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3481 | } else { |
| 3482 | /* decode response */ |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3483 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3484 | rc = validate_t2((struct smb_t2_rsp *)pSMBr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3485 | /* BB also check enough total bytes returned */ |
Jeff Layton | 820a803 | 2011-05-04 08:05:26 -0400 | [diff] [blame] | 3486 | if (rc || get_bcc(&pSMBr->hdr) < 2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3487 | rc = -EIO; /* bad smb */ |
| 3488 | else { |
| 3489 | __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset); |
| 3490 | __u16 count = le16_to_cpu(pSMBr->t2.DataCount); |
| 3491 | rc = cifs_copy_posix_acl(acl_inf, |
| 3492 | (char *)&pSMBr->hdr.Protocol+data_offset, |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3493 | buflen, acl_type, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3494 | } |
| 3495 | } |
| 3496 | cifs_buf_release(pSMB); |
| 3497 | if (rc == -EAGAIN) |
| 3498 | goto queryAclRetry; |
| 3499 | return rc; |
| 3500 | } |
| 3501 | |
| 3502 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 3503 | CIFSSMBSetPosixACL(const unsigned int xid, struct cifs_tcon *tcon, |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3504 | const unsigned char *fileName, |
| 3505 | const char *local_acl, const int buflen, |
| 3506 | const int acl_type, |
| 3507 | const struct nls_table *nls_codepage, int remap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3508 | { |
| 3509 | struct smb_com_transaction2_spi_req *pSMB = NULL; |
| 3510 | struct smb_com_transaction2_spi_rsp *pSMBr = NULL; |
| 3511 | char *parm_data; |
| 3512 | int name_len; |
| 3513 | int rc = 0; |
| 3514 | int bytes_returned = 0; |
| 3515 | __u16 params, byte_count, data_count, param_offset, offset; |
| 3516 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3517 | cifs_dbg(FYI, "In SetPosixACL (Unix) for path %s\n", fileName); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3518 | setAclRetry: |
| 3519 | rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB, |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3520 | (void **) &pSMBr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3521 | if (rc) |
| 3522 | return rc; |
| 3523 | if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) { |
| 3524 | name_len = |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 3525 | cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName, |
| 3526 | PATH_MAX, nls_codepage, remap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3527 | name_len++; /* trailing null */ |
| 3528 | name_len *= 2; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3529 | } else { /* BB improve the check for buffer overruns BB */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3530 | name_len = strnlen(fileName, PATH_MAX); |
| 3531 | name_len++; /* trailing null */ |
| 3532 | strncpy(pSMB->FileName, fileName, name_len); |
| 3533 | } |
| 3534 | params = 6 + name_len; |
| 3535 | pSMB->MaxParameterCount = cpu_to_le16(2); |
Steve French | 582d21e | 2008-05-13 04:54:12 +0000 | [diff] [blame] | 3536 | /* BB find max SMB size from sess */ |
| 3537 | pSMB->MaxDataCount = cpu_to_le16(1000); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3538 | pSMB->MaxSetupCount = 0; |
| 3539 | pSMB->Reserved = 0; |
| 3540 | pSMB->Flags = 0; |
| 3541 | pSMB->Timeout = 0; |
| 3542 | pSMB->Reserved2 = 0; |
| 3543 | param_offset = offsetof(struct smb_com_transaction2_spi_req, |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3544 | InformationLevel) - 4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3545 | offset = param_offset + params; |
| 3546 | parm_data = ((char *) &pSMB->hdr.Protocol) + offset; |
| 3547 | pSMB->ParameterOffset = cpu_to_le16(param_offset); |
| 3548 | |
| 3549 | /* convert to on the wire format for POSIX ACL */ |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3550 | data_count = ACL_to_cifs_posix(parm_data, local_acl, buflen, acl_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3551 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 3552 | if (data_count == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3553 | rc = -EOPNOTSUPP; |
| 3554 | goto setACLerrorExit; |
| 3555 | } |
| 3556 | pSMB->DataOffset = cpu_to_le16(offset); |
| 3557 | pSMB->SetupCount = 1; |
| 3558 | pSMB->Reserved3 = 0; |
| 3559 | pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION); |
| 3560 | pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_ACL); |
| 3561 | byte_count = 3 /* pad */ + params + data_count; |
| 3562 | pSMB->DataCount = cpu_to_le16(data_count); |
| 3563 | pSMB->TotalDataCount = pSMB->DataCount; |
| 3564 | pSMB->ParameterCount = cpu_to_le16(params); |
| 3565 | pSMB->TotalParameterCount = pSMB->ParameterCount; |
| 3566 | pSMB->Reserved4 = 0; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 3567 | inc_rfc1001_len(pSMB, byte_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3568 | pSMB->ByteCount = cpu_to_le16(byte_count); |
| 3569 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3570 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 3571 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3572 | cifs_dbg(FYI, "Set POSIX ACL returned %d\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3573 | |
| 3574 | setACLerrorExit: |
| 3575 | cifs_buf_release(pSMB); |
| 3576 | if (rc == -EAGAIN) |
| 3577 | goto setAclRetry; |
| 3578 | return rc; |
| 3579 | } |
| 3580 | |
Steve French | f654bac | 2005-04-28 22:41:04 -0700 | [diff] [blame] | 3581 | /* BB fix tabs in this function FIXME BB */ |
| 3582 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 3583 | CIFSGetExtAttr(const unsigned int xid, struct cifs_tcon *tcon, |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 3584 | const int netfid, __u64 *pExtAttrBits, __u64 *pMask) |
Steve French | f654bac | 2005-04-28 22:41:04 -0700 | [diff] [blame] | 3585 | { |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3586 | int rc = 0; |
| 3587 | struct smb_t2_qfi_req *pSMB = NULL; |
| 3588 | struct smb_t2_qfi_rsp *pSMBr = NULL; |
| 3589 | int bytes_returned; |
| 3590 | __u16 params, byte_count; |
Steve French | f654bac | 2005-04-28 22:41:04 -0700 | [diff] [blame] | 3591 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3592 | cifs_dbg(FYI, "In GetExtAttr\n"); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 3593 | if (tcon == NULL) |
| 3594 | return -ENODEV; |
Steve French | f654bac | 2005-04-28 22:41:04 -0700 | [diff] [blame] | 3595 | |
| 3596 | GetExtAttrRetry: |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 3597 | rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB, |
| 3598 | (void **) &pSMBr); |
| 3599 | if (rc) |
| 3600 | return rc; |
Steve French | f654bac | 2005-04-28 22:41:04 -0700 | [diff] [blame] | 3601 | |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 3602 | params = 2 /* level */ + 2 /* fid */; |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 3603 | pSMB->t2.TotalDataCount = 0; |
| 3604 | pSMB->t2.MaxParameterCount = cpu_to_le16(4); |
| 3605 | /* BB find exact max data count below from sess structure BB */ |
| 3606 | pSMB->t2.MaxDataCount = cpu_to_le16(4000); |
| 3607 | pSMB->t2.MaxSetupCount = 0; |
| 3608 | pSMB->t2.Reserved = 0; |
| 3609 | pSMB->t2.Flags = 0; |
| 3610 | pSMB->t2.Timeout = 0; |
| 3611 | pSMB->t2.Reserved2 = 0; |
| 3612 | pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req, |
| 3613 | Fid) - 4); |
| 3614 | pSMB->t2.DataCount = 0; |
| 3615 | pSMB->t2.DataOffset = 0; |
| 3616 | pSMB->t2.SetupCount = 1; |
| 3617 | pSMB->t2.Reserved3 = 0; |
| 3618 | pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION); |
| 3619 | byte_count = params + 1 /* pad */ ; |
| 3620 | pSMB->t2.TotalParameterCount = cpu_to_le16(params); |
| 3621 | pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount; |
| 3622 | pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_ATTR_FLAGS); |
| 3623 | pSMB->Pad = 0; |
Steve French | f654bac | 2005-04-28 22:41:04 -0700 | [diff] [blame] | 3624 | pSMB->Fid = netfid; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 3625 | inc_rfc1001_len(pSMB, byte_count); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 3626 | pSMB->t2.ByteCount = cpu_to_le16(byte_count); |
Steve French | f654bac | 2005-04-28 22:41:04 -0700 | [diff] [blame] | 3627 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 3628 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 3629 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
| 3630 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3631 | cifs_dbg(FYI, "error %d in GetExtAttr\n", rc); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 3632 | } else { |
| 3633 | /* decode response */ |
| 3634 | rc = validate_t2((struct smb_t2_rsp *)pSMBr); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 3635 | /* BB also check enough total bytes returned */ |
Jeff Layton | 820a803 | 2011-05-04 08:05:26 -0400 | [diff] [blame] | 3636 | if (rc || get_bcc(&pSMBr->hdr) < 2) |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 3637 | /* If rc should we check for EOPNOSUPP and |
| 3638 | disable the srvino flag? or in caller? */ |
| 3639 | rc = -EIO; /* bad smb */ |
| 3640 | else { |
| 3641 | __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset); |
| 3642 | __u16 count = le16_to_cpu(pSMBr->t2.DataCount); |
| 3643 | struct file_chattr_info *pfinfo; |
| 3644 | /* BB Do we need a cast or hash here ? */ |
| 3645 | if (count != 16) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3646 | cifs_dbg(FYI, "Illegal size ret in GetExtAttr\n"); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 3647 | rc = -EIO; |
| 3648 | goto GetExtAttrOut; |
| 3649 | } |
| 3650 | pfinfo = (struct file_chattr_info *) |
| 3651 | (data_offset + (char *) &pSMBr->hdr.Protocol); |
| 3652 | *pExtAttrBits = le64_to_cpu(pfinfo->mode); |
Steve French | f654bac | 2005-04-28 22:41:04 -0700 | [diff] [blame] | 3653 | *pMask = le64_to_cpu(pfinfo->mask); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 3654 | } |
| 3655 | } |
Steve French | f654bac | 2005-04-28 22:41:04 -0700 | [diff] [blame] | 3656 | GetExtAttrOut: |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 3657 | cifs_buf_release(pSMB); |
| 3658 | if (rc == -EAGAIN) |
| 3659 | goto GetExtAttrRetry; |
| 3660 | return rc; |
Steve French | f654bac | 2005-04-28 22:41:04 -0700 | [diff] [blame] | 3661 | } |
| 3662 | |
Steve French | f654bac | 2005-04-28 22:41:04 -0700 | [diff] [blame] | 3663 | #endif /* CONFIG_POSIX */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3664 | |
Jeff Layton | 79df1ba | 2010-12-06 12:52:08 -0500 | [diff] [blame] | 3665 | #ifdef CONFIG_CIFS_ACL |
| 3666 | /* |
| 3667 | * Initialize NT TRANSACT SMB into small smb request buffer. This assumes that |
| 3668 | * all NT TRANSACTS that we init here have total parm and data under about 400 |
| 3669 | * bytes (to fit in small cifs buffer size), which is the case so far, it |
| 3670 | * easily fits. NB: Setup words themselves and ByteCount MaxSetupCount (size of |
| 3671 | * returned setup area) and MaxParameterCount (returned parms size) must be set |
| 3672 | * by caller |
| 3673 | */ |
| 3674 | static int |
| 3675 | smb_init_nttransact(const __u16 sub_command, const int setup_count, |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 3676 | const int parm_len, struct cifs_tcon *tcon, |
Jeff Layton | 79df1ba | 2010-12-06 12:52:08 -0500 | [diff] [blame] | 3677 | void **ret_buf) |
| 3678 | { |
| 3679 | int rc; |
| 3680 | __u32 temp_offset; |
| 3681 | struct smb_com_ntransact_req *pSMB; |
| 3682 | |
| 3683 | rc = small_smb_init(SMB_COM_NT_TRANSACT, 19 + setup_count, tcon, |
| 3684 | (void **)&pSMB); |
| 3685 | if (rc) |
| 3686 | return rc; |
| 3687 | *ret_buf = (void *)pSMB; |
| 3688 | pSMB->Reserved = 0; |
| 3689 | pSMB->TotalParameterCount = cpu_to_le32(parm_len); |
| 3690 | pSMB->TotalDataCount = 0; |
Jeff Layton | c974bef | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 3691 | pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00); |
Jeff Layton | 79df1ba | 2010-12-06 12:52:08 -0500 | [diff] [blame] | 3692 | pSMB->ParameterCount = pSMB->TotalParameterCount; |
| 3693 | pSMB->DataCount = pSMB->TotalDataCount; |
| 3694 | temp_offset = offsetof(struct smb_com_ntransact_req, Parms) + |
| 3695 | (setup_count * 2) - 4 /* for rfc1001 length itself */; |
| 3696 | pSMB->ParameterOffset = cpu_to_le32(temp_offset); |
| 3697 | pSMB->DataOffset = cpu_to_le32(temp_offset + parm_len); |
| 3698 | pSMB->SetupCount = setup_count; /* no need to le convert byte fields */ |
| 3699 | pSMB->SubCommand = cpu_to_le16(sub_command); |
| 3700 | return 0; |
| 3701 | } |
| 3702 | |
| 3703 | static int |
| 3704 | validate_ntransact(char *buf, char **ppparm, char **ppdata, |
| 3705 | __u32 *pparmlen, __u32 *pdatalen) |
| 3706 | { |
| 3707 | char *end_of_smb; |
| 3708 | __u32 data_count, data_offset, parm_count, parm_offset; |
| 3709 | struct smb_com_ntransact_rsp *pSMBr; |
Jeff Layton | 820a803 | 2011-05-04 08:05:26 -0400 | [diff] [blame] | 3710 | u16 bcc; |
Jeff Layton | 79df1ba | 2010-12-06 12:52:08 -0500 | [diff] [blame] | 3711 | |
| 3712 | *pdatalen = 0; |
| 3713 | *pparmlen = 0; |
| 3714 | |
| 3715 | if (buf == NULL) |
| 3716 | return -EINVAL; |
| 3717 | |
| 3718 | pSMBr = (struct smb_com_ntransact_rsp *)buf; |
| 3719 | |
Jeff Layton | 820a803 | 2011-05-04 08:05:26 -0400 | [diff] [blame] | 3720 | bcc = get_bcc(&pSMBr->hdr); |
| 3721 | end_of_smb = 2 /* sizeof byte count */ + bcc + |
Jeff Layton | 79df1ba | 2010-12-06 12:52:08 -0500 | [diff] [blame] | 3722 | (char *)&pSMBr->ByteCount; |
| 3723 | |
| 3724 | data_offset = le32_to_cpu(pSMBr->DataOffset); |
| 3725 | data_count = le32_to_cpu(pSMBr->DataCount); |
| 3726 | parm_offset = le32_to_cpu(pSMBr->ParameterOffset); |
| 3727 | parm_count = le32_to_cpu(pSMBr->ParameterCount); |
| 3728 | |
| 3729 | *ppparm = (char *)&pSMBr->hdr.Protocol + parm_offset; |
| 3730 | *ppdata = (char *)&pSMBr->hdr.Protocol + data_offset; |
| 3731 | |
| 3732 | /* should we also check that parm and data areas do not overlap? */ |
| 3733 | if (*ppparm > end_of_smb) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3734 | cifs_dbg(FYI, "parms start after end of smb\n"); |
Jeff Layton | 79df1ba | 2010-12-06 12:52:08 -0500 | [diff] [blame] | 3735 | return -EINVAL; |
| 3736 | } else if (parm_count + *ppparm > end_of_smb) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3737 | cifs_dbg(FYI, "parm end after end of smb\n"); |
Jeff Layton | 79df1ba | 2010-12-06 12:52:08 -0500 | [diff] [blame] | 3738 | return -EINVAL; |
| 3739 | } else if (*ppdata > end_of_smb) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3740 | cifs_dbg(FYI, "data starts after end of smb\n"); |
Jeff Layton | 79df1ba | 2010-12-06 12:52:08 -0500 | [diff] [blame] | 3741 | return -EINVAL; |
| 3742 | } else if (data_count + *ppdata > end_of_smb) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3743 | cifs_dbg(FYI, "data %p + count %d (%p) past smb end %p start %p\n", |
| 3744 | *ppdata, data_count, (data_count + *ppdata), |
| 3745 | end_of_smb, pSMBr); |
Jeff Layton | 79df1ba | 2010-12-06 12:52:08 -0500 | [diff] [blame] | 3746 | return -EINVAL; |
Jeff Layton | 820a803 | 2011-05-04 08:05:26 -0400 | [diff] [blame] | 3747 | } else if (parm_count + data_count > bcc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3748 | cifs_dbg(FYI, "parm count and data count larger than SMB\n"); |
Jeff Layton | 79df1ba | 2010-12-06 12:52:08 -0500 | [diff] [blame] | 3749 | return -EINVAL; |
| 3750 | } |
| 3751 | *pdatalen = data_count; |
| 3752 | *pparmlen = parm_count; |
| 3753 | return 0; |
| 3754 | } |
| 3755 | |
Steve French | 0a4b92c | 2006-01-12 15:44:21 -0800 | [diff] [blame] | 3756 | /* Get Security Descriptor (by handle) from remote server for a file or dir */ |
| 3757 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 3758 | CIFSSMBGetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid, |
Steve French | 630f3f0c | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 3759 | struct cifs_ntsd **acl_inf, __u32 *pbuflen) |
Steve French | 0a4b92c | 2006-01-12 15:44:21 -0800 | [diff] [blame] | 3760 | { |
| 3761 | int rc = 0; |
| 3762 | int buf_type = 0; |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 3763 | QUERY_SEC_DESC_REQ *pSMB; |
Steve French | 0a4b92c | 2006-01-12 15:44:21 -0800 | [diff] [blame] | 3764 | struct kvec iov[1]; |
| 3765 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3766 | cifs_dbg(FYI, "GetCifsACL\n"); |
Steve French | 0a4b92c | 2006-01-12 15:44:21 -0800 | [diff] [blame] | 3767 | |
Steve French | 630f3f0c | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 3768 | *pbuflen = 0; |
| 3769 | *acl_inf = NULL; |
| 3770 | |
Steve French | b9c7a2b | 2007-10-26 23:40:20 +0000 | [diff] [blame] | 3771 | rc = smb_init_nttransact(NT_TRANSACT_QUERY_SECURITY_DESC, 0, |
Steve French | 0a4b92c | 2006-01-12 15:44:21 -0800 | [diff] [blame] | 3772 | 8 /* parm len */, tcon, (void **) &pSMB); |
| 3773 | if (rc) |
| 3774 | return rc; |
| 3775 | |
| 3776 | pSMB->MaxParameterCount = cpu_to_le32(4); |
| 3777 | /* BB TEST with big acls that might need to be e.g. larger than 16K */ |
| 3778 | pSMB->MaxSetupCount = 0; |
| 3779 | pSMB->Fid = fid; /* file handle always le */ |
| 3780 | pSMB->AclFlags = cpu_to_le32(CIFS_ACL_OWNER | CIFS_ACL_GROUP | |
| 3781 | CIFS_ACL_DACL); |
| 3782 | pSMB->ByteCount = cpu_to_le16(11); /* 3 bytes pad + 8 bytes parm */ |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 3783 | inc_rfc1001_len(pSMB, 11); |
Steve French | 0a4b92c | 2006-01-12 15:44:21 -0800 | [diff] [blame] | 3784 | iov[0].iov_base = (char *)pSMB; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 3785 | iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4; |
Steve French | 0a4b92c | 2006-01-12 15:44:21 -0800 | [diff] [blame] | 3786 | |
Steve French | a761ac5 | 2007-10-18 21:45:27 +0000 | [diff] [blame] | 3787 | rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovec */, &buf_type, |
Jeff Layton | 7749981 | 2011-01-11 07:24:23 -0500 | [diff] [blame] | 3788 | 0); |
Pavel Shilovsky | 44c5818 | 2012-05-28 14:16:31 +0400 | [diff] [blame] | 3789 | cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get); |
Steve French | 0a4b92c | 2006-01-12 15:44:21 -0800 | [diff] [blame] | 3790 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3791 | cifs_dbg(FYI, "Send error in QuerySecDesc = %d\n", rc); |
Steve French | 0a4b92c | 2006-01-12 15:44:21 -0800 | [diff] [blame] | 3792 | } else { /* decode response */ |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 3793 | __le32 *parm; |
Steve French | 630f3f0c | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 3794 | __u32 parm_len; |
| 3795 | __u32 acl_len; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3796 | struct smb_com_ntransact_rsp *pSMBr; |
Steve French | 630f3f0c | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 3797 | char *pdata; |
Steve French | 0a4b92c | 2006-01-12 15:44:21 -0800 | [diff] [blame] | 3798 | |
| 3799 | /* validate_nttransact */ |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3800 | rc = validate_ntransact(iov[0].iov_base, (char **)&parm, |
Steve French | 630f3f0c | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 3801 | &pdata, &parm_len, pbuflen); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 3802 | if (rc) |
Steve French | 0a4b92c | 2006-01-12 15:44:21 -0800 | [diff] [blame] | 3803 | goto qsec_out; |
| 3804 | pSMBr = (struct smb_com_ntransact_rsp *)iov[0].iov_base; |
| 3805 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3806 | cifs_dbg(FYI, "smb %p parm %p data %p\n", |
| 3807 | pSMBr, parm, *acl_inf); |
Steve French | 0a4b92c | 2006-01-12 15:44:21 -0800 | [diff] [blame] | 3808 | |
| 3809 | if (le32_to_cpu(pSMBr->ParameterCount) != 4) { |
| 3810 | rc = -EIO; /* bad smb */ |
Steve French | 630f3f0c | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 3811 | *pbuflen = 0; |
Steve French | 0a4b92c | 2006-01-12 15:44:21 -0800 | [diff] [blame] | 3812 | goto qsec_out; |
| 3813 | } |
| 3814 | |
| 3815 | /* BB check that data area is minimum length and as big as acl_len */ |
| 3816 | |
Steve French | af6f461 | 2007-10-16 18:40:37 +0000 | [diff] [blame] | 3817 | acl_len = le32_to_cpu(*parm); |
Steve French | 630f3f0c | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 3818 | if (acl_len != *pbuflen) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3819 | cifs_dbg(VFS, "acl length %d does not match %d\n", |
| 3820 | acl_len, *pbuflen); |
Steve French | 630f3f0c | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 3821 | if (*pbuflen > acl_len) |
| 3822 | *pbuflen = acl_len; |
| 3823 | } |
Steve French | 0a4b92c | 2006-01-12 15:44:21 -0800 | [diff] [blame] | 3824 | |
Steve French | 630f3f0c | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 3825 | /* check if buffer is big enough for the acl |
| 3826 | header followed by the smallest SID */ |
| 3827 | if ((*pbuflen < sizeof(struct cifs_ntsd) + 8) || |
| 3828 | (*pbuflen >= 64 * 1024)) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3829 | cifs_dbg(VFS, "bad acl length %d\n", *pbuflen); |
Steve French | 630f3f0c | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 3830 | rc = -EINVAL; |
| 3831 | *pbuflen = 0; |
| 3832 | } else { |
Silviu-Mihai Popescu | f7f7c18 | 2013-03-11 18:22:32 +0200 | [diff] [blame] | 3833 | *acl_inf = kmemdup(pdata, *pbuflen, GFP_KERNEL); |
Steve French | 630f3f0c | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 3834 | if (*acl_inf == NULL) { |
| 3835 | *pbuflen = 0; |
| 3836 | rc = -ENOMEM; |
| 3837 | } |
Steve French | 630f3f0c | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 3838 | } |
Steve French | 0a4b92c | 2006-01-12 15:44:21 -0800 | [diff] [blame] | 3839 | } |
| 3840 | qsec_out: |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 3841 | if (buf_type == CIFS_SMALL_BUFFER) |
Steve French | 0a4b92c | 2006-01-12 15:44:21 -0800 | [diff] [blame] | 3842 | cifs_small_buf_release(iov[0].iov_base); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 3843 | else if (buf_type == CIFS_LARGE_BUFFER) |
Steve French | 0a4b92c | 2006-01-12 15:44:21 -0800 | [diff] [blame] | 3844 | cifs_buf_release(iov[0].iov_base); |
Steve French | 4b8f930 | 2006-02-26 16:41:18 +0000 | [diff] [blame] | 3845 | /* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */ |
Steve French | 0a4b92c | 2006-01-12 15:44:21 -0800 | [diff] [blame] | 3846 | return rc; |
| 3847 | } |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 3848 | |
| 3849 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 3850 | CIFSSMBSetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid, |
Shirish Pargaonkar | a5ff376 | 2011-10-13 10:26:03 -0500 | [diff] [blame] | 3851 | struct cifs_ntsd *pntsd, __u32 acllen, int aclflag) |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 3852 | { |
| 3853 | __u16 byte_count, param_count, data_count, param_offset, data_offset; |
| 3854 | int rc = 0; |
| 3855 | int bytes_returned = 0; |
| 3856 | SET_SEC_DESC_REQ *pSMB = NULL; |
Jeff Layton | b2a3ad9 | 2012-03-26 09:55:29 -0400 | [diff] [blame] | 3857 | void *pSMBr; |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 3858 | |
| 3859 | setCifsAclRetry: |
Jeff Layton | b2a3ad9 | 2012-03-26 09:55:29 -0400 | [diff] [blame] | 3860 | rc = smb_init(SMB_COM_NT_TRANSACT, 19, tcon, (void **) &pSMB, &pSMBr); |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 3861 | if (rc) |
Jeff Layton | b2a3ad9 | 2012-03-26 09:55:29 -0400 | [diff] [blame] | 3862 | return rc; |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 3863 | |
| 3864 | pSMB->MaxSetupCount = 0; |
| 3865 | pSMB->Reserved = 0; |
| 3866 | |
| 3867 | param_count = 8; |
| 3868 | param_offset = offsetof(struct smb_com_transaction_ssec_req, Fid) - 4; |
| 3869 | data_count = acllen; |
| 3870 | data_offset = param_offset + param_count; |
| 3871 | byte_count = 3 /* pad */ + param_count; |
| 3872 | |
| 3873 | pSMB->DataCount = cpu_to_le32(data_count); |
| 3874 | pSMB->TotalDataCount = pSMB->DataCount; |
| 3875 | pSMB->MaxParameterCount = cpu_to_le32(4); |
| 3876 | pSMB->MaxDataCount = cpu_to_le32(16384); |
| 3877 | pSMB->ParameterCount = cpu_to_le32(param_count); |
| 3878 | pSMB->ParameterOffset = cpu_to_le32(param_offset); |
| 3879 | pSMB->TotalParameterCount = pSMB->ParameterCount; |
| 3880 | pSMB->DataOffset = cpu_to_le32(data_offset); |
| 3881 | pSMB->SetupCount = 0; |
| 3882 | pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_SET_SECURITY_DESC); |
| 3883 | pSMB->ByteCount = cpu_to_le16(byte_count+data_count); |
| 3884 | |
| 3885 | pSMB->Fid = fid; /* file handle always le */ |
| 3886 | pSMB->Reserved2 = 0; |
Shirish Pargaonkar | a5ff376 | 2011-10-13 10:26:03 -0500 | [diff] [blame] | 3887 | pSMB->AclFlags = cpu_to_le32(aclflag); |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 3888 | |
| 3889 | if (pntsd && acllen) { |
Jeff Layton | b2a3ad9 | 2012-03-26 09:55:29 -0400 | [diff] [blame] | 3890 | memcpy((char *)pSMBr + offsetof(struct smb_hdr, Protocol) + |
| 3891 | data_offset, pntsd, acllen); |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 3892 | inc_rfc1001_len(pSMB, byte_count + data_count); |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 3893 | } else |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 3894 | inc_rfc1001_len(pSMB, byte_count); |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 3895 | |
| 3896 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 3897 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
| 3898 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3899 | cifs_dbg(FYI, "SetCIFSACL bytes_returned: %d, rc: %d\n", |
| 3900 | bytes_returned, rc); |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 3901 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3902 | cifs_dbg(FYI, "Set CIFS ACL returned %d\n", rc); |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 3903 | cifs_buf_release(pSMB); |
| 3904 | |
| 3905 | if (rc == -EAGAIN) |
| 3906 | goto setCifsAclRetry; |
| 3907 | |
| 3908 | return (rc); |
| 3909 | } |
| 3910 | |
Jeff Layton | 79df1ba | 2010-12-06 12:52:08 -0500 | [diff] [blame] | 3911 | #endif /* CONFIG_CIFS_ACL */ |
Steve French | 0a4b92c | 2006-01-12 15:44:21 -0800 | [diff] [blame] | 3912 | |
Steve French | 6b8edfe | 2005-08-23 20:26:03 -0700 | [diff] [blame] | 3913 | /* Legacy Query Path Information call for lookup to old servers such |
| 3914 | as Win9x/WinME */ |
Pavel Shilovsky | 68889f2 | 2012-05-25 14:40:22 +0400 | [diff] [blame] | 3915 | int |
| 3916 | SMBQueryInformation(const unsigned int xid, struct cifs_tcon *tcon, |
| 3917 | const char *search_name, FILE_ALL_INFO *data, |
| 3918 | const struct nls_table *nls_codepage, int remap) |
Steve French | 6b8edfe | 2005-08-23 20:26:03 -0700 | [diff] [blame] | 3919 | { |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 3920 | QUERY_INFORMATION_REQ *pSMB; |
| 3921 | QUERY_INFORMATION_RSP *pSMBr; |
Steve French | 6b8edfe | 2005-08-23 20:26:03 -0700 | [diff] [blame] | 3922 | int rc = 0; |
| 3923 | int bytes_returned; |
| 3924 | int name_len; |
| 3925 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3926 | cifs_dbg(FYI, "In SMBQPath path %s\n", search_name); |
Steve French | 6b8edfe | 2005-08-23 20:26:03 -0700 | [diff] [blame] | 3927 | QInfRetry: |
| 3928 | rc = smb_init(SMB_COM_QUERY_INFORMATION, 0, tcon, (void **) &pSMB, |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3929 | (void **) &pSMBr); |
Steve French | 6b8edfe | 2005-08-23 20:26:03 -0700 | [diff] [blame] | 3930 | if (rc) |
| 3931 | return rc; |
| 3932 | |
| 3933 | if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) { |
| 3934 | name_len = |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 3935 | cifsConvertToUTF16((__le16 *) pSMB->FileName, |
Pavel Shilovsky | 68889f2 | 2012-05-25 14:40:22 +0400 | [diff] [blame] | 3936 | search_name, PATH_MAX, nls_codepage, |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 3937 | remap); |
Steve French | 6b8edfe | 2005-08-23 20:26:03 -0700 | [diff] [blame] | 3938 | name_len++; /* trailing null */ |
| 3939 | name_len *= 2; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3940 | } else { |
Pavel Shilovsky | 68889f2 | 2012-05-25 14:40:22 +0400 | [diff] [blame] | 3941 | name_len = strnlen(search_name, PATH_MAX); |
Steve French | 6b8edfe | 2005-08-23 20:26:03 -0700 | [diff] [blame] | 3942 | name_len++; /* trailing null */ |
Pavel Shilovsky | 68889f2 | 2012-05-25 14:40:22 +0400 | [diff] [blame] | 3943 | strncpy(pSMB->FileName, search_name, name_len); |
Steve French | 6b8edfe | 2005-08-23 20:26:03 -0700 | [diff] [blame] | 3944 | } |
| 3945 | pSMB->BufferFormat = 0x04; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3946 | name_len++; /* account for buffer type byte */ |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 3947 | inc_rfc1001_len(pSMB, (__u16)name_len); |
Steve French | 6b8edfe | 2005-08-23 20:26:03 -0700 | [diff] [blame] | 3948 | pSMB->ByteCount = cpu_to_le16(name_len); |
| 3949 | |
| 3950 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3951 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
Steve French | 6b8edfe | 2005-08-23 20:26:03 -0700 | [diff] [blame] | 3952 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3953 | cifs_dbg(FYI, "Send error in QueryInfo = %d\n", rc); |
Pavel Shilovsky | 68889f2 | 2012-05-25 14:40:22 +0400 | [diff] [blame] | 3954 | } else if (data) { |
Steve French | 1bd5bbc | 2006-09-28 03:35:57 +0000 | [diff] [blame] | 3955 | struct timespec ts; |
| 3956 | __u32 time = le32_to_cpu(pSMBr->last_write_time); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 3957 | |
| 3958 | /* decode response */ |
Steve French | 1bd5bbc | 2006-09-28 03:35:57 +0000 | [diff] [blame] | 3959 | /* BB FIXME - add time zone adjustment BB */ |
Pavel Shilovsky | 68889f2 | 2012-05-25 14:40:22 +0400 | [diff] [blame] | 3960 | memset(data, 0, sizeof(FILE_ALL_INFO)); |
Steve French | 1bd5bbc | 2006-09-28 03:35:57 +0000 | [diff] [blame] | 3961 | ts.tv_nsec = 0; |
| 3962 | ts.tv_sec = time; |
| 3963 | /* decode time fields */ |
Pavel Shilovsky | 68889f2 | 2012-05-25 14:40:22 +0400 | [diff] [blame] | 3964 | data->ChangeTime = cpu_to_le64(cifs_UnixTimeToNT(ts)); |
| 3965 | data->LastWriteTime = data->ChangeTime; |
| 3966 | data->LastAccessTime = 0; |
| 3967 | data->AllocationSize = |
Steve French | 70ca734 | 2005-09-22 16:32:06 -0700 | [diff] [blame] | 3968 | cpu_to_le64(le32_to_cpu(pSMBr->size)); |
Pavel Shilovsky | 68889f2 | 2012-05-25 14:40:22 +0400 | [diff] [blame] | 3969 | data->EndOfFile = data->AllocationSize; |
| 3970 | data->Attributes = |
Steve French | 70ca734 | 2005-09-22 16:32:06 -0700 | [diff] [blame] | 3971 | cpu_to_le32(le16_to_cpu(pSMBr->attr)); |
Steve French | 6b8edfe | 2005-08-23 20:26:03 -0700 | [diff] [blame] | 3972 | } else |
| 3973 | rc = -EIO; /* bad buffer passed in */ |
| 3974 | |
| 3975 | cifs_buf_release(pSMB); |
| 3976 | |
| 3977 | if (rc == -EAGAIN) |
| 3978 | goto QInfRetry; |
| 3979 | |
| 3980 | return rc; |
| 3981 | } |
| 3982 | |
Jeff Layton | bcd5357 | 2010-02-12 07:44:16 -0500 | [diff] [blame] | 3983 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 3984 | CIFSSMBQFileInfo(const unsigned int xid, struct cifs_tcon *tcon, |
Jeff Layton | bcd5357 | 2010-02-12 07:44:16 -0500 | [diff] [blame] | 3985 | u16 netfid, FILE_ALL_INFO *pFindData) |
| 3986 | { |
| 3987 | struct smb_t2_qfi_req *pSMB = NULL; |
| 3988 | struct smb_t2_qfi_rsp *pSMBr = NULL; |
| 3989 | int rc = 0; |
| 3990 | int bytes_returned; |
| 3991 | __u16 params, byte_count; |
Steve French | 6b8edfe | 2005-08-23 20:26:03 -0700 | [diff] [blame] | 3992 | |
Jeff Layton | bcd5357 | 2010-02-12 07:44:16 -0500 | [diff] [blame] | 3993 | QFileInfoRetry: |
| 3994 | rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB, |
| 3995 | (void **) &pSMBr); |
| 3996 | if (rc) |
| 3997 | return rc; |
Steve French | 6b8edfe | 2005-08-23 20:26:03 -0700 | [diff] [blame] | 3998 | |
Jeff Layton | bcd5357 | 2010-02-12 07:44:16 -0500 | [diff] [blame] | 3999 | params = 2 /* level */ + 2 /* fid */; |
| 4000 | pSMB->t2.TotalDataCount = 0; |
| 4001 | pSMB->t2.MaxParameterCount = cpu_to_le16(4); |
| 4002 | /* BB find exact max data count below from sess structure BB */ |
| 4003 | pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize); |
| 4004 | pSMB->t2.MaxSetupCount = 0; |
| 4005 | pSMB->t2.Reserved = 0; |
| 4006 | pSMB->t2.Flags = 0; |
| 4007 | pSMB->t2.Timeout = 0; |
| 4008 | pSMB->t2.Reserved2 = 0; |
| 4009 | pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req, |
| 4010 | Fid) - 4); |
| 4011 | pSMB->t2.DataCount = 0; |
| 4012 | pSMB->t2.DataOffset = 0; |
| 4013 | pSMB->t2.SetupCount = 1; |
| 4014 | pSMB->t2.Reserved3 = 0; |
| 4015 | pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION); |
| 4016 | byte_count = params + 1 /* pad */ ; |
| 4017 | pSMB->t2.TotalParameterCount = cpu_to_le16(params); |
| 4018 | pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount; |
| 4019 | pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO); |
| 4020 | pSMB->Pad = 0; |
| 4021 | pSMB->Fid = netfid; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 4022 | inc_rfc1001_len(pSMB, byte_count); |
David Disseldorp | 7ac0feb | 2013-06-28 11:47:33 +0200 | [diff] [blame] | 4023 | pSMB->t2.ByteCount = cpu_to_le16(byte_count); |
Jeff Layton | bcd5357 | 2010-02-12 07:44:16 -0500 | [diff] [blame] | 4024 | |
| 4025 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 4026 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
| 4027 | if (rc) { |
Steve French | ebcc943 | 2013-12-09 09:18:09 -0600 | [diff] [blame] | 4028 | cifs_dbg(FYI, "Send error in QFileInfo = %d", rc); |
Jeff Layton | bcd5357 | 2010-02-12 07:44:16 -0500 | [diff] [blame] | 4029 | } else { /* decode response */ |
| 4030 | rc = validate_t2((struct smb_t2_rsp *)pSMBr); |
| 4031 | |
| 4032 | if (rc) /* BB add auto retry on EOPNOTSUPP? */ |
| 4033 | rc = -EIO; |
Jeff Layton | 820a803 | 2011-05-04 08:05:26 -0400 | [diff] [blame] | 4034 | else if (get_bcc(&pSMBr->hdr) < 40) |
Jeff Layton | bcd5357 | 2010-02-12 07:44:16 -0500 | [diff] [blame] | 4035 | rc = -EIO; /* bad smb */ |
| 4036 | else if (pFindData) { |
| 4037 | __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset); |
| 4038 | memcpy((char *) pFindData, |
| 4039 | (char *) &pSMBr->hdr.Protocol + |
| 4040 | data_offset, sizeof(FILE_ALL_INFO)); |
| 4041 | } else |
| 4042 | rc = -ENOMEM; |
| 4043 | } |
| 4044 | cifs_buf_release(pSMB); |
| 4045 | if (rc == -EAGAIN) |
| 4046 | goto QFileInfoRetry; |
| 4047 | |
| 4048 | return rc; |
| 4049 | } |
Steve French | 6b8edfe | 2005-08-23 20:26:03 -0700 | [diff] [blame] | 4050 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4051 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 4052 | CIFSSMBQPathInfo(const unsigned int xid, struct cifs_tcon *tcon, |
Pavel Shilovsky | 68889f2 | 2012-05-25 14:40:22 +0400 | [diff] [blame] | 4053 | const char *search_name, FILE_ALL_INFO *data, |
Steve French | acf1a1b | 2006-10-12 03:28:28 +0000 | [diff] [blame] | 4054 | int legacy /* old style infolevel */, |
Steve French | 737b758 | 2005-04-28 22:41:06 -0700 | [diff] [blame] | 4055 | const struct nls_table *nls_codepage, int remap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4056 | { |
Pavel Shilovsky | 68889f2 | 2012-05-25 14:40:22 +0400 | [diff] [blame] | 4057 | /* level 263 SMB_QUERY_FILE_ALL_INFO */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4058 | TRANSACTION2_QPI_REQ *pSMB = NULL; |
| 4059 | TRANSACTION2_QPI_RSP *pSMBr = NULL; |
| 4060 | int rc = 0; |
| 4061 | int bytes_returned; |
| 4062 | int name_len; |
| 4063 | __u16 params, byte_count; |
| 4064 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4065 | /* cifs_dbg(FYI, "In QPathInfo path %s\n", search_name); */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4066 | QPathInfoRetry: |
| 4067 | rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB, |
| 4068 | (void **) &pSMBr); |
| 4069 | if (rc) |
| 4070 | return rc; |
| 4071 | |
| 4072 | if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) { |
| 4073 | name_len = |
Pavel Shilovsky | 68889f2 | 2012-05-25 14:40:22 +0400 | [diff] [blame] | 4074 | cifsConvertToUTF16((__le16 *) pSMB->FileName, search_name, |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 4075 | PATH_MAX, nls_codepage, remap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4076 | name_len++; /* trailing null */ |
| 4077 | name_len *= 2; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4078 | } else { /* BB improve the check for buffer overruns BB */ |
Pavel Shilovsky | 68889f2 | 2012-05-25 14:40:22 +0400 | [diff] [blame] | 4079 | name_len = strnlen(search_name, PATH_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4080 | name_len++; /* trailing null */ |
Pavel Shilovsky | 68889f2 | 2012-05-25 14:40:22 +0400 | [diff] [blame] | 4081 | strncpy(pSMB->FileName, search_name, name_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4082 | } |
| 4083 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4084 | params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4085 | pSMB->TotalDataCount = 0; |
| 4086 | pSMB->MaxParameterCount = cpu_to_le16(2); |
Steve French | 582d21e | 2008-05-13 04:54:12 +0000 | [diff] [blame] | 4087 | /* BB find exact max SMB PDU from sess structure BB */ |
| 4088 | pSMB->MaxDataCount = cpu_to_le16(4000); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4089 | pSMB->MaxSetupCount = 0; |
| 4090 | pSMB->Reserved = 0; |
| 4091 | pSMB->Flags = 0; |
| 4092 | pSMB->Timeout = 0; |
| 4093 | pSMB->Reserved2 = 0; |
| 4094 | pSMB->ParameterOffset = cpu_to_le16(offsetof( |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4095 | struct smb_com_transaction2_qpi_req, InformationLevel) - 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4096 | pSMB->DataCount = 0; |
| 4097 | pSMB->DataOffset = 0; |
| 4098 | pSMB->SetupCount = 1; |
| 4099 | pSMB->Reserved3 = 0; |
| 4100 | pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION); |
| 4101 | byte_count = params + 1 /* pad */ ; |
| 4102 | pSMB->TotalParameterCount = cpu_to_le16(params); |
| 4103 | pSMB->ParameterCount = pSMB->TotalParameterCount; |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 4104 | if (legacy) |
Steve French | acf1a1b | 2006-10-12 03:28:28 +0000 | [diff] [blame] | 4105 | pSMB->InformationLevel = cpu_to_le16(SMB_INFO_STANDARD); |
| 4106 | else |
| 4107 | pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4108 | pSMB->Reserved4 = 0; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 4109 | inc_rfc1001_len(pSMB, byte_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4110 | pSMB->ByteCount = cpu_to_le16(byte_count); |
| 4111 | |
| 4112 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 4113 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
| 4114 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4115 | cifs_dbg(FYI, "Send error in QPathInfo = %d\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4116 | } else { /* decode response */ |
| 4117 | rc = validate_t2((struct smb_t2_rsp *)pSMBr); |
| 4118 | |
Steve French | acf1a1b | 2006-10-12 03:28:28 +0000 | [diff] [blame] | 4119 | if (rc) /* BB add auto retry on EOPNOTSUPP? */ |
| 4120 | rc = -EIO; |
Jeff Layton | 820a803 | 2011-05-04 08:05:26 -0400 | [diff] [blame] | 4121 | else if (!legacy && get_bcc(&pSMBr->hdr) < 40) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4122 | rc = -EIO; /* bad smb */ |
Jeff Layton | 820a803 | 2011-05-04 08:05:26 -0400 | [diff] [blame] | 4123 | else if (legacy && get_bcc(&pSMBr->hdr) < 24) |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4124 | rc = -EIO; /* 24 or 26 expected but we do not read |
| 4125 | last field */ |
Pavel Shilovsky | 68889f2 | 2012-05-25 14:40:22 +0400 | [diff] [blame] | 4126 | else if (data) { |
Steve French | acf1a1b | 2006-10-12 03:28:28 +0000 | [diff] [blame] | 4127 | int size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4128 | __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 4129 | |
Pavel Shilovsky | 68889f2 | 2012-05-25 14:40:22 +0400 | [diff] [blame] | 4130 | /* |
| 4131 | * On legacy responses we do not read the last field, |
| 4132 | * EAsize, fortunately since it varies by subdialect and |
| 4133 | * also note it differs on Set vs Get, ie two bytes or 4 |
| 4134 | * bytes depending but we don't care here. |
| 4135 | */ |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 4136 | if (legacy) |
Steve French | acf1a1b | 2006-10-12 03:28:28 +0000 | [diff] [blame] | 4137 | size = sizeof(FILE_INFO_STANDARD); |
| 4138 | else |
| 4139 | size = sizeof(FILE_ALL_INFO); |
Pavel Shilovsky | 68889f2 | 2012-05-25 14:40:22 +0400 | [diff] [blame] | 4140 | memcpy((char *) data, (char *) &pSMBr->hdr.Protocol + |
Steve French | acf1a1b | 2006-10-12 03:28:28 +0000 | [diff] [blame] | 4141 | data_offset, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4142 | } else |
| 4143 | rc = -ENOMEM; |
| 4144 | } |
| 4145 | cifs_buf_release(pSMB); |
| 4146 | if (rc == -EAGAIN) |
| 4147 | goto QPathInfoRetry; |
| 4148 | |
| 4149 | return rc; |
| 4150 | } |
| 4151 | |
| 4152 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 4153 | CIFSSMBUnixQFileInfo(const unsigned int xid, struct cifs_tcon *tcon, |
Jeff Layton | c8634fd | 2010-02-12 07:44:17 -0500 | [diff] [blame] | 4154 | u16 netfid, FILE_UNIX_BASIC_INFO *pFindData) |
| 4155 | { |
| 4156 | struct smb_t2_qfi_req *pSMB = NULL; |
| 4157 | struct smb_t2_qfi_rsp *pSMBr = NULL; |
| 4158 | int rc = 0; |
| 4159 | int bytes_returned; |
| 4160 | __u16 params, byte_count; |
| 4161 | |
| 4162 | UnixQFileInfoRetry: |
| 4163 | rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB, |
| 4164 | (void **) &pSMBr); |
| 4165 | if (rc) |
| 4166 | return rc; |
| 4167 | |
| 4168 | params = 2 /* level */ + 2 /* fid */; |
| 4169 | pSMB->t2.TotalDataCount = 0; |
| 4170 | pSMB->t2.MaxParameterCount = cpu_to_le16(4); |
| 4171 | /* BB find exact max data count below from sess structure BB */ |
| 4172 | pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize); |
| 4173 | pSMB->t2.MaxSetupCount = 0; |
| 4174 | pSMB->t2.Reserved = 0; |
| 4175 | pSMB->t2.Flags = 0; |
| 4176 | pSMB->t2.Timeout = 0; |
| 4177 | pSMB->t2.Reserved2 = 0; |
| 4178 | pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req, |
| 4179 | Fid) - 4); |
| 4180 | pSMB->t2.DataCount = 0; |
| 4181 | pSMB->t2.DataOffset = 0; |
| 4182 | pSMB->t2.SetupCount = 1; |
| 4183 | pSMB->t2.Reserved3 = 0; |
| 4184 | pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION); |
| 4185 | byte_count = params + 1 /* pad */ ; |
| 4186 | pSMB->t2.TotalParameterCount = cpu_to_le16(params); |
| 4187 | pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount; |
| 4188 | pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC); |
| 4189 | pSMB->Pad = 0; |
| 4190 | pSMB->Fid = netfid; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 4191 | inc_rfc1001_len(pSMB, byte_count); |
David Disseldorp | 7ac0feb | 2013-06-28 11:47:33 +0200 | [diff] [blame] | 4192 | pSMB->t2.ByteCount = cpu_to_le16(byte_count); |
Jeff Layton | c8634fd | 2010-02-12 07:44:17 -0500 | [diff] [blame] | 4193 | |
| 4194 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 4195 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
| 4196 | if (rc) { |
Steve French | ebcc943 | 2013-12-09 09:18:09 -0600 | [diff] [blame] | 4197 | cifs_dbg(FYI, "Send error in UnixQFileInfo = %d", rc); |
Jeff Layton | c8634fd | 2010-02-12 07:44:17 -0500 | [diff] [blame] | 4198 | } else { /* decode response */ |
| 4199 | rc = validate_t2((struct smb_t2_rsp *)pSMBr); |
| 4200 | |
Jeff Layton | 820a803 | 2011-05-04 08:05:26 -0400 | [diff] [blame] | 4201 | if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4202 | cifs_dbg(VFS, "Malformed FILE_UNIX_BASIC_INFO response. Unix Extensions can be disabled on mount by specifying the nosfu mount option.\n"); |
Jeff Layton | c8634fd | 2010-02-12 07:44:17 -0500 | [diff] [blame] | 4203 | rc = -EIO; /* bad smb */ |
| 4204 | } else { |
| 4205 | __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset); |
| 4206 | memcpy((char *) pFindData, |
| 4207 | (char *) &pSMBr->hdr.Protocol + |
| 4208 | data_offset, |
| 4209 | sizeof(FILE_UNIX_BASIC_INFO)); |
| 4210 | } |
| 4211 | } |
| 4212 | |
| 4213 | cifs_buf_release(pSMB); |
| 4214 | if (rc == -EAGAIN) |
| 4215 | goto UnixQFileInfoRetry; |
| 4216 | |
| 4217 | return rc; |
| 4218 | } |
| 4219 | |
| 4220 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 4221 | CIFSSMBUnixQPathInfo(const unsigned int xid, struct cifs_tcon *tcon, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4222 | const unsigned char *searchName, |
Steve French | 582d21e | 2008-05-13 04:54:12 +0000 | [diff] [blame] | 4223 | FILE_UNIX_BASIC_INFO *pFindData, |
Steve French | 737b758 | 2005-04-28 22:41:06 -0700 | [diff] [blame] | 4224 | const struct nls_table *nls_codepage, int remap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4225 | { |
| 4226 | /* SMB_QUERY_FILE_UNIX_BASIC */ |
| 4227 | TRANSACTION2_QPI_REQ *pSMB = NULL; |
| 4228 | TRANSACTION2_QPI_RSP *pSMBr = NULL; |
| 4229 | int rc = 0; |
| 4230 | int bytes_returned = 0; |
| 4231 | int name_len; |
| 4232 | __u16 params, byte_count; |
| 4233 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4234 | cifs_dbg(FYI, "In QPathInfo (Unix) the path %s\n", searchName); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4235 | UnixQPathInfoRetry: |
| 4236 | rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB, |
| 4237 | (void **) &pSMBr); |
| 4238 | if (rc) |
| 4239 | return rc; |
| 4240 | |
| 4241 | if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) { |
| 4242 | name_len = |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 4243 | cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName, |
| 4244 | PATH_MAX, nls_codepage, remap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4245 | name_len++; /* trailing null */ |
| 4246 | name_len *= 2; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4247 | } else { /* BB improve the check for buffer overruns BB */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4248 | name_len = strnlen(searchName, PATH_MAX); |
| 4249 | name_len++; /* trailing null */ |
| 4250 | strncpy(pSMB->FileName, searchName, name_len); |
| 4251 | } |
| 4252 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4253 | params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4254 | pSMB->TotalDataCount = 0; |
| 4255 | pSMB->MaxParameterCount = cpu_to_le16(2); |
| 4256 | /* BB find exact max SMB PDU from sess structure BB */ |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4257 | pSMB->MaxDataCount = cpu_to_le16(4000); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4258 | pSMB->MaxSetupCount = 0; |
| 4259 | pSMB->Reserved = 0; |
| 4260 | pSMB->Flags = 0; |
| 4261 | pSMB->Timeout = 0; |
| 4262 | pSMB->Reserved2 = 0; |
| 4263 | pSMB->ParameterOffset = cpu_to_le16(offsetof( |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4264 | struct smb_com_transaction2_qpi_req, InformationLevel) - 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4265 | pSMB->DataCount = 0; |
| 4266 | pSMB->DataOffset = 0; |
| 4267 | pSMB->SetupCount = 1; |
| 4268 | pSMB->Reserved3 = 0; |
| 4269 | pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION); |
| 4270 | byte_count = params + 1 /* pad */ ; |
| 4271 | pSMB->TotalParameterCount = cpu_to_le16(params); |
| 4272 | pSMB->ParameterCount = pSMB->TotalParameterCount; |
| 4273 | pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC); |
| 4274 | pSMB->Reserved4 = 0; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 4275 | inc_rfc1001_len(pSMB, byte_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4276 | pSMB->ByteCount = cpu_to_le16(byte_count); |
| 4277 | |
| 4278 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 4279 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
| 4280 | if (rc) { |
Steve French | ebcc943 | 2013-12-09 09:18:09 -0600 | [diff] [blame] | 4281 | cifs_dbg(FYI, "Send error in UnixQPathInfo = %d", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4282 | } else { /* decode response */ |
| 4283 | rc = validate_t2((struct smb_t2_rsp *)pSMBr); |
| 4284 | |
Jeff Layton | 820a803 | 2011-05-04 08:05:26 -0400 | [diff] [blame] | 4285 | if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4286 | cifs_dbg(VFS, "Malformed FILE_UNIX_BASIC_INFO response. Unix Extensions can be disabled on mount by specifying the nosfu mount option.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4287 | rc = -EIO; /* bad smb */ |
| 4288 | } else { |
| 4289 | __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset); |
| 4290 | memcpy((char *) pFindData, |
| 4291 | (char *) &pSMBr->hdr.Protocol + |
| 4292 | data_offset, |
Steve French | 630f3f0c | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 4293 | sizeof(FILE_UNIX_BASIC_INFO)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4294 | } |
| 4295 | } |
| 4296 | cifs_buf_release(pSMB); |
| 4297 | if (rc == -EAGAIN) |
| 4298 | goto UnixQPathInfoRetry; |
| 4299 | |
| 4300 | return rc; |
| 4301 | } |
| 4302 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4303 | /* xid, tcon, searchName and codepage are input parms, rest are returned */ |
| 4304 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 4305 | CIFSFindFirst(const unsigned int xid, struct cifs_tcon *tcon, |
Shirish Pargaonkar | c052e2b | 2012-09-28 12:21:14 -0500 | [diff] [blame] | 4306 | const char *searchName, struct cifs_sb_info *cifs_sb, |
Shirish Pargaonkar | 2608bee | 2012-05-15 10:19:16 -0500 | [diff] [blame] | 4307 | __u16 *pnetfid, __u16 search_flags, |
Shirish Pargaonkar | c052e2b | 2012-09-28 12:21:14 -0500 | [diff] [blame] | 4308 | struct cifs_search_info *psrch_inf, bool msearch) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4309 | { |
| 4310 | /* level 257 SMB_ */ |
| 4311 | TRANSACTION2_FFIRST_REQ *pSMB = NULL; |
| 4312 | TRANSACTION2_FFIRST_RSP *pSMBr = NULL; |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 4313 | T2_FFIRST_RSP_PARMS *parms; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4314 | int rc = 0; |
| 4315 | int bytes_returned = 0; |
Shirish Pargaonkar | c052e2b | 2012-09-28 12:21:14 -0500 | [diff] [blame] | 4316 | int name_len, remap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4317 | __u16 params, byte_count; |
Shirish Pargaonkar | c052e2b | 2012-09-28 12:21:14 -0500 | [diff] [blame] | 4318 | struct nls_table *nls_codepage; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4319 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4320 | cifs_dbg(FYI, "In FindFirst for %s\n", searchName); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4321 | |
| 4322 | findFirstRetry: |
| 4323 | rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB, |
| 4324 | (void **) &pSMBr); |
| 4325 | if (rc) |
| 4326 | return rc; |
| 4327 | |
Shirish Pargaonkar | c052e2b | 2012-09-28 12:21:14 -0500 | [diff] [blame] | 4328 | nls_codepage = cifs_sb->local_nls; |
| 4329 | remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR; |
| 4330 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4331 | if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) { |
| 4332 | name_len = |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 4333 | cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName, |
| 4334 | PATH_MAX, nls_codepage, remap); |
Steve French | 737b758 | 2005-04-28 22:41:06 -0700 | [diff] [blame] | 4335 | /* We can not add the asterik earlier in case |
| 4336 | it got remapped to 0xF03A as if it were part of the |
| 4337 | directory name instead of a wildcard */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4338 | name_len *= 2; |
Shirish Pargaonkar | c052e2b | 2012-09-28 12:21:14 -0500 | [diff] [blame] | 4339 | if (msearch) { |
| 4340 | pSMB->FileName[name_len] = CIFS_DIR_SEP(cifs_sb); |
| 4341 | pSMB->FileName[name_len+1] = 0; |
| 4342 | pSMB->FileName[name_len+2] = '*'; |
| 4343 | pSMB->FileName[name_len+3] = 0; |
| 4344 | name_len += 4; /* now the trailing null */ |
| 4345 | /* null terminate just in case */ |
| 4346 | pSMB->FileName[name_len] = 0; |
| 4347 | pSMB->FileName[name_len+1] = 0; |
| 4348 | name_len += 2; |
| 4349 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4350 | } else { /* BB add check for overrun of SMB buf BB */ |
| 4351 | name_len = strnlen(searchName, PATH_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4352 | /* BB fix here and in unicode clause above ie |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 4353 | if (name_len > buffersize-header) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4354 | free buffer exit; BB */ |
| 4355 | strncpy(pSMB->FileName, searchName, name_len); |
Shirish Pargaonkar | c052e2b | 2012-09-28 12:21:14 -0500 | [diff] [blame] | 4356 | if (msearch) { |
| 4357 | pSMB->FileName[name_len] = CIFS_DIR_SEP(cifs_sb); |
| 4358 | pSMB->FileName[name_len+1] = '*'; |
| 4359 | pSMB->FileName[name_len+2] = 0; |
| 4360 | name_len += 3; |
| 4361 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4362 | } |
| 4363 | |
| 4364 | params = 12 + name_len /* includes null */ ; |
| 4365 | pSMB->TotalDataCount = 0; /* no EAs */ |
| 4366 | pSMB->MaxParameterCount = cpu_to_le16(10); |
Jeff Layton | c974bef | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 4367 | pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4368 | pSMB->MaxSetupCount = 0; |
| 4369 | pSMB->Reserved = 0; |
| 4370 | pSMB->Flags = 0; |
| 4371 | pSMB->Timeout = 0; |
| 4372 | pSMB->Reserved2 = 0; |
| 4373 | byte_count = params + 1 /* pad */ ; |
| 4374 | pSMB->TotalParameterCount = cpu_to_le16(params); |
| 4375 | pSMB->ParameterCount = pSMB->TotalParameterCount; |
| 4376 | pSMB->ParameterOffset = cpu_to_le16( |
Steve French | 8827481 | 2006-03-09 22:21:45 +0000 | [diff] [blame] | 4377 | offsetof(struct smb_com_transaction2_ffirst_req, SearchAttributes) |
| 4378 | - 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4379 | pSMB->DataCount = 0; |
| 4380 | pSMB->DataOffset = 0; |
| 4381 | pSMB->SetupCount = 1; /* one byte, no need to make endian neutral */ |
| 4382 | pSMB->Reserved3 = 0; |
| 4383 | pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_FIRST); |
| 4384 | pSMB->SearchAttributes = |
| 4385 | cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM | |
| 4386 | ATTR_DIRECTORY); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4387 | pSMB->SearchCount = cpu_to_le16(CIFSMaxBufSize/sizeof(FILE_UNIX_INFO)); |
Shirish Pargaonkar | 2608bee | 2012-05-15 10:19:16 -0500 | [diff] [blame] | 4388 | pSMB->SearchFlags = cpu_to_le16(search_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4389 | pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level); |
| 4390 | |
| 4391 | /* BB what should we set StorageType to? Does it matter? BB */ |
| 4392 | pSMB->SearchStorageType = 0; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 4393 | inc_rfc1001_len(pSMB, byte_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4394 | pSMB->ByteCount = cpu_to_le16(byte_count); |
| 4395 | |
| 4396 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 4397 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
Pavel Shilovsky | 44c5818 | 2012-05-28 14:16:31 +0400 | [diff] [blame] | 4398 | cifs_stats_inc(&tcon->stats.cifs_stats.num_ffirst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4399 | |
Steve French | 8827481 | 2006-03-09 22:21:45 +0000 | [diff] [blame] | 4400 | if (rc) {/* BB add logic to retry regular search if Unix search |
| 4401 | rejected unexpectedly by server */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4402 | /* BB Add code to handle unsupported level rc */ |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4403 | cifs_dbg(FYI, "Error in FindFirst = %d\n", rc); |
Steve French | 1982c34 | 2005-08-17 12:38:22 -0700 | [diff] [blame] | 4404 | |
Steve French | 8827481 | 2006-03-09 22:21:45 +0000 | [diff] [blame] | 4405 | cifs_buf_release(pSMB); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4406 | |
| 4407 | /* BB eventually could optimize out free and realloc of buf */ |
| 4408 | /* for this case */ |
| 4409 | if (rc == -EAGAIN) |
| 4410 | goto findFirstRetry; |
| 4411 | } else { /* decode response */ |
| 4412 | /* BB remember to free buffer if error BB */ |
| 4413 | rc = validate_t2((struct smb_t2_rsp *)pSMBr); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 4414 | if (rc == 0) { |
Steve French | b77d753 | 2008-10-08 19:13:46 +0000 | [diff] [blame] | 4415 | unsigned int lnoff; |
| 4416 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4417 | if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE) |
Steve French | 4b18f2a | 2008-04-29 00:06:05 +0000 | [diff] [blame] | 4418 | psrch_inf->unicode = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4419 | else |
Steve French | 4b18f2a | 2008-04-29 00:06:05 +0000 | [diff] [blame] | 4420 | psrch_inf->unicode = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4421 | |
| 4422 | psrch_inf->ntwrk_buf_start = (char *)pSMBr; |
Steve French | d47d7c1 | 2006-02-28 03:45:48 +0000 | [diff] [blame] | 4423 | psrch_inf->smallBuf = 0; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4424 | psrch_inf->srch_entries_start = |
| 4425 | (char *) &pSMBr->hdr.Protocol + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4426 | le16_to_cpu(pSMBr->t2.DataOffset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4427 | parms = (T2_FFIRST_RSP_PARMS *)((char *) &pSMBr->hdr.Protocol + |
| 4428 | le16_to_cpu(pSMBr->t2.ParameterOffset)); |
| 4429 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 4430 | if (parms->EndofSearch) |
Steve French | 4b18f2a | 2008-04-29 00:06:05 +0000 | [diff] [blame] | 4431 | psrch_inf->endOfSearch = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4432 | else |
Steve French | 4b18f2a | 2008-04-29 00:06:05 +0000 | [diff] [blame] | 4433 | psrch_inf->endOfSearch = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4434 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4435 | psrch_inf->entries_in_buffer = |
| 4436 | le16_to_cpu(parms->SearchCount); |
Steve French | 6080823 | 2006-04-22 15:53:05 +0000 | [diff] [blame] | 4437 | psrch_inf->index_of_last_entry = 2 /* skip . and .. */ + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4438 | psrch_inf->entries_in_buffer; |
Steve French | b77d753 | 2008-10-08 19:13:46 +0000 | [diff] [blame] | 4439 | lnoff = le16_to_cpu(parms->LastNameOffset); |
Jeff Layton | c974bef | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 4440 | if (CIFSMaxBufSize < lnoff) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4441 | cifs_dbg(VFS, "ignoring corrupt resume name\n"); |
Steve French | b77d753 | 2008-10-08 19:13:46 +0000 | [diff] [blame] | 4442 | psrch_inf->last_entry = NULL; |
| 4443 | return rc; |
| 4444 | } |
| 4445 | |
Steve French | 0752f15 | 2008-10-07 20:03:33 +0000 | [diff] [blame] | 4446 | psrch_inf->last_entry = psrch_inf->srch_entries_start + |
Steve French | b77d753 | 2008-10-08 19:13:46 +0000 | [diff] [blame] | 4447 | lnoff; |
| 4448 | |
Shirish Pargaonkar | c052e2b | 2012-09-28 12:21:14 -0500 | [diff] [blame] | 4449 | if (pnetfid) |
| 4450 | *pnetfid = parms->SearchHandle; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4451 | } else { |
| 4452 | cifs_buf_release(pSMB); |
| 4453 | } |
| 4454 | } |
| 4455 | |
| 4456 | return rc; |
| 4457 | } |
| 4458 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 4459 | int CIFSFindNext(const unsigned int xid, struct cifs_tcon *tcon, |
| 4460 | __u16 searchHandle, __u16 search_flags, |
| 4461 | struct cifs_search_info *psrch_inf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4462 | { |
| 4463 | TRANSACTION2_FNEXT_REQ *pSMB = NULL; |
| 4464 | TRANSACTION2_FNEXT_RSP *pSMBr = NULL; |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 4465 | T2_FNEXT_RSP_PARMS *parms; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4466 | char *response_data; |
| 4467 | int rc = 0; |
Jeff Layton | 9438fab | 2011-08-23 07:21:28 -0400 | [diff] [blame] | 4468 | int bytes_returned; |
| 4469 | unsigned int name_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4470 | __u16 params, byte_count; |
| 4471 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4472 | cifs_dbg(FYI, "In FindNext\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4473 | |
Steve French | 4b18f2a | 2008-04-29 00:06:05 +0000 | [diff] [blame] | 4474 | if (psrch_inf->endOfSearch) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4475 | return -ENOENT; |
| 4476 | |
| 4477 | rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB, |
| 4478 | (void **) &pSMBr); |
| 4479 | if (rc) |
| 4480 | return rc; |
| 4481 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4482 | params = 14; /* includes 2 bytes of null string, converted to LE below*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4483 | byte_count = 0; |
| 4484 | pSMB->TotalDataCount = 0; /* no EAs */ |
| 4485 | pSMB->MaxParameterCount = cpu_to_le16(8); |
Jeff Layton | c974bef | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 4486 | pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4487 | pSMB->MaxSetupCount = 0; |
| 4488 | pSMB->Reserved = 0; |
| 4489 | pSMB->Flags = 0; |
| 4490 | pSMB->Timeout = 0; |
| 4491 | pSMB->Reserved2 = 0; |
| 4492 | pSMB->ParameterOffset = cpu_to_le16( |
| 4493 | offsetof(struct smb_com_transaction2_fnext_req,SearchHandle) - 4); |
| 4494 | pSMB->DataCount = 0; |
| 4495 | pSMB->DataOffset = 0; |
| 4496 | pSMB->SetupCount = 1; |
| 4497 | pSMB->Reserved3 = 0; |
| 4498 | pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_NEXT); |
| 4499 | pSMB->SearchHandle = searchHandle; /* always kept as le */ |
| 4500 | pSMB->SearchCount = |
Steve French | 630f3f0c | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 4501 | cpu_to_le16(CIFSMaxBufSize / sizeof(FILE_UNIX_INFO)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4502 | pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level); |
| 4503 | pSMB->ResumeKey = psrch_inf->resume_key; |
Shirish Pargaonkar | 2608bee | 2012-05-15 10:19:16 -0500 | [diff] [blame] | 4504 | pSMB->SearchFlags = cpu_to_le16(search_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4505 | |
| 4506 | name_len = psrch_inf->resume_name_len; |
| 4507 | params += name_len; |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 4508 | if (name_len < PATH_MAX) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4509 | memcpy(pSMB->ResumeFileName, psrch_inf->presume_name, name_len); |
| 4510 | byte_count += name_len; |
Steve French | ef6724e | 2005-08-02 21:31:05 -0700 | [diff] [blame] | 4511 | /* 14 byte parm len above enough for 2 byte null terminator */ |
| 4512 | pSMB->ResumeFileName[name_len] = 0; |
| 4513 | pSMB->ResumeFileName[name_len+1] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4514 | } else { |
| 4515 | rc = -EINVAL; |
| 4516 | goto FNext2_err_exit; |
| 4517 | } |
| 4518 | byte_count = params + 1 /* pad */ ; |
| 4519 | pSMB->TotalParameterCount = cpu_to_le16(params); |
| 4520 | pSMB->ParameterCount = pSMB->TotalParameterCount; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 4521 | inc_rfc1001_len(pSMB, byte_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4522 | pSMB->ByteCount = cpu_to_le16(byte_count); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4523 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4524 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 4525 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
Pavel Shilovsky | 44c5818 | 2012-05-28 14:16:31 +0400 | [diff] [blame] | 4526 | cifs_stats_inc(&tcon->stats.cifs_stats.num_fnext); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4527 | if (rc) { |
| 4528 | if (rc == -EBADF) { |
Steve French | 4b18f2a | 2008-04-29 00:06:05 +0000 | [diff] [blame] | 4529 | psrch_inf->endOfSearch = true; |
Jeff Layton | 6353450 | 2008-05-12 19:56:05 -0700 | [diff] [blame] | 4530 | cifs_buf_release(pSMB); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4531 | rc = 0; /* search probably was closed at end of search*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4532 | } else |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4533 | cifs_dbg(FYI, "FindNext returned = %d\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4534 | } else { /* decode response */ |
| 4535 | rc = validate_t2((struct smb_t2_rsp *)pSMBr); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4536 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 4537 | if (rc == 0) { |
Steve French | b77d753 | 2008-10-08 19:13:46 +0000 | [diff] [blame] | 4538 | unsigned int lnoff; |
| 4539 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4540 | /* BB fixme add lock for file (srch_info) struct here */ |
| 4541 | if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE) |
Steve French | 4b18f2a | 2008-04-29 00:06:05 +0000 | [diff] [blame] | 4542 | psrch_inf->unicode = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4543 | else |
Steve French | 4b18f2a | 2008-04-29 00:06:05 +0000 | [diff] [blame] | 4544 | psrch_inf->unicode = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4545 | response_data = (char *) &pSMBr->hdr.Protocol + |
| 4546 | le16_to_cpu(pSMBr->t2.ParameterOffset); |
| 4547 | parms = (T2_FNEXT_RSP_PARMS *)response_data; |
| 4548 | response_data = (char *)&pSMBr->hdr.Protocol + |
| 4549 | le16_to_cpu(pSMBr->t2.DataOffset); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 4550 | if (psrch_inf->smallBuf) |
Steve French | d47d7c1 | 2006-02-28 03:45:48 +0000 | [diff] [blame] | 4551 | cifs_small_buf_release( |
| 4552 | psrch_inf->ntwrk_buf_start); |
| 4553 | else |
| 4554 | cifs_buf_release(psrch_inf->ntwrk_buf_start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4555 | psrch_inf->srch_entries_start = response_data; |
| 4556 | psrch_inf->ntwrk_buf_start = (char *)pSMB; |
Steve French | d47d7c1 | 2006-02-28 03:45:48 +0000 | [diff] [blame] | 4557 | psrch_inf->smallBuf = 0; |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 4558 | if (parms->EndofSearch) |
Steve French | 4b18f2a | 2008-04-29 00:06:05 +0000 | [diff] [blame] | 4559 | psrch_inf->endOfSearch = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4560 | else |
Steve French | 4b18f2a | 2008-04-29 00:06:05 +0000 | [diff] [blame] | 4561 | psrch_inf->endOfSearch = false; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4562 | psrch_inf->entries_in_buffer = |
| 4563 | le16_to_cpu(parms->SearchCount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4564 | psrch_inf->index_of_last_entry += |
| 4565 | psrch_inf->entries_in_buffer; |
Steve French | b77d753 | 2008-10-08 19:13:46 +0000 | [diff] [blame] | 4566 | lnoff = le16_to_cpu(parms->LastNameOffset); |
Jeff Layton | c974bef | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 4567 | if (CIFSMaxBufSize < lnoff) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4568 | cifs_dbg(VFS, "ignoring corrupt resume name\n"); |
Steve French | b77d753 | 2008-10-08 19:13:46 +0000 | [diff] [blame] | 4569 | psrch_inf->last_entry = NULL; |
| 4570 | return rc; |
| 4571 | } else |
| 4572 | psrch_inf->last_entry = |
| 4573 | psrch_inf->srch_entries_start + lnoff; |
| 4574 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4575 | /* cifs_dbg(FYI, "fnxt2 entries in buf %d index_of_last %d\n", |
| 4576 | psrch_inf->entries_in_buffer, psrch_inf->index_of_last_entry); */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4577 | |
| 4578 | /* BB fixme add unlock here */ |
| 4579 | } |
| 4580 | |
| 4581 | } |
| 4582 | |
| 4583 | /* BB On error, should we leave previous search buf (and count and |
| 4584 | last entry fields) intact or free the previous one? */ |
| 4585 | |
| 4586 | /* Note: On -EAGAIN error only caller can retry on handle based calls |
| 4587 | since file handle passed in no longer valid */ |
| 4588 | FNext2_err_exit: |
| 4589 | if (rc != 0) |
| 4590 | cifs_buf_release(pSMB); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4591 | return rc; |
| 4592 | } |
| 4593 | |
| 4594 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 4595 | CIFSFindClose(const unsigned int xid, struct cifs_tcon *tcon, |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4596 | const __u16 searchHandle) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4597 | { |
| 4598 | int rc = 0; |
| 4599 | FINDCLOSE_REQ *pSMB = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4600 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4601 | cifs_dbg(FYI, "In CIFSSMBFindClose\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4602 | rc = small_smb_init(SMB_COM_FIND_CLOSE2, 1, tcon, (void **)&pSMB); |
| 4603 | |
| 4604 | /* no sense returning error if session restarted |
| 4605 | as file handle has been closed */ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 4606 | if (rc == -EAGAIN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4607 | return 0; |
| 4608 | if (rc) |
| 4609 | return rc; |
| 4610 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4611 | pSMB->FileID = searchHandle; |
| 4612 | pSMB->ByteCount = 0; |
Pavel Shilovsky | 792af7b | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 4613 | rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 4614 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4615 | cifs_dbg(VFS, "Send error in FindClose = %d\n", rc); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 4616 | |
Pavel Shilovsky | 44c5818 | 2012-05-28 14:16:31 +0400 | [diff] [blame] | 4617 | cifs_stats_inc(&tcon->stats.cifs_stats.num_fclose); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4618 | |
| 4619 | /* Since session is dead, search handle closed on server already */ |
| 4620 | if (rc == -EAGAIN) |
| 4621 | rc = 0; |
| 4622 | |
| 4623 | return rc; |
| 4624 | } |
| 4625 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4626 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 4627 | CIFSGetSrvInodeNumber(const unsigned int xid, struct cifs_tcon *tcon, |
Pavel Shilovsky | 1208ef1 | 2012-05-27 17:34:43 +0400 | [diff] [blame] | 4628 | const char *search_name, __u64 *inode_number, |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4629 | const struct nls_table *nls_codepage, int remap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4630 | { |
| 4631 | int rc = 0; |
| 4632 | TRANSACTION2_QPI_REQ *pSMB = NULL; |
| 4633 | TRANSACTION2_QPI_RSP *pSMBr = NULL; |
| 4634 | int name_len, bytes_returned; |
| 4635 | __u16 params, byte_count; |
| 4636 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4637 | cifs_dbg(FYI, "In GetSrvInodeNum for %s\n", search_name); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 4638 | if (tcon == NULL) |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4639 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4640 | |
| 4641 | GetInodeNumberRetry: |
| 4642 | rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB, |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4643 | (void **) &pSMBr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4644 | if (rc) |
| 4645 | return rc; |
| 4646 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4647 | if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) { |
| 4648 | name_len = |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 4649 | cifsConvertToUTF16((__le16 *) pSMB->FileName, |
Pavel Shilovsky | 1208ef1 | 2012-05-27 17:34:43 +0400 | [diff] [blame] | 4650 | search_name, PATH_MAX, nls_codepage, |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 4651 | remap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4652 | name_len++; /* trailing null */ |
| 4653 | name_len *= 2; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4654 | } else { /* BB improve the check for buffer overruns BB */ |
Pavel Shilovsky | 1208ef1 | 2012-05-27 17:34:43 +0400 | [diff] [blame] | 4655 | name_len = strnlen(search_name, PATH_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4656 | name_len++; /* trailing null */ |
Pavel Shilovsky | 1208ef1 | 2012-05-27 17:34:43 +0400 | [diff] [blame] | 4657 | strncpy(pSMB->FileName, search_name, name_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4658 | } |
| 4659 | |
| 4660 | params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ; |
| 4661 | pSMB->TotalDataCount = 0; |
| 4662 | pSMB->MaxParameterCount = cpu_to_le16(2); |
| 4663 | /* BB find exact max data count below from sess structure BB */ |
| 4664 | pSMB->MaxDataCount = cpu_to_le16(4000); |
| 4665 | pSMB->MaxSetupCount = 0; |
| 4666 | pSMB->Reserved = 0; |
| 4667 | pSMB->Flags = 0; |
| 4668 | pSMB->Timeout = 0; |
| 4669 | pSMB->Reserved2 = 0; |
| 4670 | pSMB->ParameterOffset = cpu_to_le16(offsetof( |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4671 | struct smb_com_transaction2_qpi_req, InformationLevel) - 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4672 | pSMB->DataCount = 0; |
| 4673 | pSMB->DataOffset = 0; |
| 4674 | pSMB->SetupCount = 1; |
| 4675 | pSMB->Reserved3 = 0; |
| 4676 | pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION); |
| 4677 | byte_count = params + 1 /* pad */ ; |
| 4678 | pSMB->TotalParameterCount = cpu_to_le16(params); |
| 4679 | pSMB->ParameterCount = pSMB->TotalParameterCount; |
| 4680 | pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_INTERNAL_INFO); |
| 4681 | pSMB->Reserved4 = 0; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 4682 | inc_rfc1001_len(pSMB, byte_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4683 | pSMB->ByteCount = cpu_to_le16(byte_count); |
| 4684 | |
| 4685 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 4686 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
| 4687 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4688 | cifs_dbg(FYI, "error %d in QueryInternalInfo\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4689 | } else { |
| 4690 | /* decode response */ |
| 4691 | rc = validate_t2((struct smb_t2_rsp *)pSMBr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4692 | /* BB also check enough total bytes returned */ |
Jeff Layton | 820a803 | 2011-05-04 08:05:26 -0400 | [diff] [blame] | 4693 | if (rc || get_bcc(&pSMBr->hdr) < 2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4694 | /* If rc should we check for EOPNOSUPP and |
| 4695 | disable the srvino flag? or in caller? */ |
| 4696 | rc = -EIO; /* bad smb */ |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4697 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4698 | __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset); |
| 4699 | __u16 count = le16_to_cpu(pSMBr->t2.DataCount); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4700 | struct file_internal_info *pfinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4701 | /* BB Do we need a cast or hash here ? */ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 4702 | if (count < 8) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4703 | cifs_dbg(FYI, "Illegal size ret in QryIntrnlInf\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4704 | rc = -EIO; |
| 4705 | goto GetInodeNumOut; |
| 4706 | } |
| 4707 | pfinfo = (struct file_internal_info *) |
| 4708 | (data_offset + (char *) &pSMBr->hdr.Protocol); |
Steve French | 85a6dac | 2009-04-01 05:22:00 +0000 | [diff] [blame] | 4709 | *inode_number = le64_to_cpu(pfinfo->UniqueId); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4710 | } |
| 4711 | } |
| 4712 | GetInodeNumOut: |
| 4713 | cifs_buf_release(pSMB); |
| 4714 | if (rc == -EAGAIN) |
| 4715 | goto GetInodeNumberRetry; |
| 4716 | return rc; |
| 4717 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4718 | |
Igor Mammedov | fec4585 | 2008-05-16 13:06:30 +0400 | [diff] [blame] | 4719 | /* parses DFS refferal V3 structure |
| 4720 | * caller is responsible for freeing target_nodes |
| 4721 | * returns: |
| 4722 | * on success - 0 |
| 4723 | * on failure - errno |
| 4724 | */ |
| 4725 | static int |
Steve French | a1fe78f | 2008-05-16 18:48:38 +0000 | [diff] [blame] | 4726 | parse_DFS_referrals(TRANSACTION2_GET_DFS_REFER_RSP *pSMBr, |
Igor Mammedov | fec4585 | 2008-05-16 13:06:30 +0400 | [diff] [blame] | 4727 | unsigned int *num_of_nodes, |
| 4728 | struct dfs_info3_param **target_nodes, |
Igor Mammedov | 2c55608 | 2008-10-23 13:58:42 +0400 | [diff] [blame] | 4729 | const struct nls_table *nls_codepage, int remap, |
| 4730 | const char *searchName) |
Igor Mammedov | fec4585 | 2008-05-16 13:06:30 +0400 | [diff] [blame] | 4731 | { |
| 4732 | int i, rc = 0; |
| 4733 | char *data_end; |
| 4734 | bool is_unicode; |
| 4735 | struct dfs_referral_level_3 *ref; |
| 4736 | |
Harvey Harrison | 5ca33c6 | 2008-07-23 17:45:58 -0700 | [diff] [blame] | 4737 | if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE) |
| 4738 | is_unicode = true; |
| 4739 | else |
| 4740 | is_unicode = false; |
Igor Mammedov | fec4585 | 2008-05-16 13:06:30 +0400 | [diff] [blame] | 4741 | *num_of_nodes = le16_to_cpu(pSMBr->NumberOfReferrals); |
| 4742 | |
| 4743 | if (*num_of_nodes < 1) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4744 | cifs_dbg(VFS, "num_referrals: must be at least > 0, but we get num_referrals = %d\n", |
| 4745 | *num_of_nodes); |
Igor Mammedov | fec4585 | 2008-05-16 13:06:30 +0400 | [diff] [blame] | 4746 | rc = -EINVAL; |
Steve French | a1fe78f | 2008-05-16 18:48:38 +0000 | [diff] [blame] | 4747 | goto parse_DFS_referrals_exit; |
Igor Mammedov | fec4585 | 2008-05-16 13:06:30 +0400 | [diff] [blame] | 4748 | } |
| 4749 | |
| 4750 | ref = (struct dfs_referral_level_3 *) &(pSMBr->referrals); |
Al Viro | 1d92cfd | 2008-06-02 10:59:02 +0100 | [diff] [blame] | 4751 | if (ref->VersionNumber != cpu_to_le16(3)) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4752 | cifs_dbg(VFS, "Referrals of V%d version are not supported, should be V3\n", |
| 4753 | le16_to_cpu(ref->VersionNumber)); |
Igor Mammedov | fec4585 | 2008-05-16 13:06:30 +0400 | [diff] [blame] | 4754 | rc = -EINVAL; |
Steve French | a1fe78f | 2008-05-16 18:48:38 +0000 | [diff] [blame] | 4755 | goto parse_DFS_referrals_exit; |
Igor Mammedov | fec4585 | 2008-05-16 13:06:30 +0400 | [diff] [blame] | 4756 | } |
| 4757 | |
| 4758 | /* get the upper boundary of the resp buffer */ |
| 4759 | data_end = (char *)(&(pSMBr->PathConsumed)) + |
| 4760 | le16_to_cpu(pSMBr->t2.DataCount); |
| 4761 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4762 | cifs_dbg(FYI, "num_referrals: %d dfs flags: 0x%x ...\n", |
| 4763 | *num_of_nodes, le32_to_cpu(pSMBr->DFSFlags)); |
Igor Mammedov | fec4585 | 2008-05-16 13:06:30 +0400 | [diff] [blame] | 4764 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4765 | *target_nodes = kcalloc(*num_of_nodes, sizeof(struct dfs_info3_param), |
| 4766 | GFP_KERNEL); |
Igor Mammedov | fec4585 | 2008-05-16 13:06:30 +0400 | [diff] [blame] | 4767 | if (*target_nodes == NULL) { |
Igor Mammedov | fec4585 | 2008-05-16 13:06:30 +0400 | [diff] [blame] | 4768 | rc = -ENOMEM; |
Steve French | a1fe78f | 2008-05-16 18:48:38 +0000 | [diff] [blame] | 4769 | goto parse_DFS_referrals_exit; |
Igor Mammedov | fec4585 | 2008-05-16 13:06:30 +0400 | [diff] [blame] | 4770 | } |
| 4771 | |
Daniel Mack | 3ad2f3f | 2010-02-03 08:01:28 +0800 | [diff] [blame] | 4772 | /* collect necessary data from referrals */ |
Igor Mammedov | fec4585 | 2008-05-16 13:06:30 +0400 | [diff] [blame] | 4773 | for (i = 0; i < *num_of_nodes; i++) { |
| 4774 | char *temp; |
| 4775 | int max_len; |
| 4776 | struct dfs_info3_param *node = (*target_nodes)+i; |
| 4777 | |
Steve French | 0e0d2cf | 2009-05-01 05:27:32 +0000 | [diff] [blame] | 4778 | node->flags = le32_to_cpu(pSMBr->DFSFlags); |
Igor Mammedov | 2c55608 | 2008-10-23 13:58:42 +0400 | [diff] [blame] | 4779 | if (is_unicode) { |
Jeff Layton | 331c313 | 2008-12-17 06:31:53 -0500 | [diff] [blame] | 4780 | __le16 *tmp = kmalloc(strlen(searchName)*2 + 2, |
| 4781 | GFP_KERNEL); |
Steve French | 2920ee2 | 2009-08-31 15:27:26 +0000 | [diff] [blame] | 4782 | if (tmp == NULL) { |
| 4783 | rc = -ENOMEM; |
| 4784 | goto parse_DFS_referrals_exit; |
| 4785 | } |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 4786 | cifsConvertToUTF16((__le16 *) tmp, searchName, |
| 4787 | PATH_MAX, nls_codepage, remap); |
| 4788 | node->path_consumed = cifs_utf16_bytes(tmp, |
Jeff Layton | 69f801f | 2009-04-30 06:46:32 -0400 | [diff] [blame] | 4789 | le16_to_cpu(pSMBr->PathConsumed), |
Igor Mammedov | 2c55608 | 2008-10-23 13:58:42 +0400 | [diff] [blame] | 4790 | nls_codepage); |
| 4791 | kfree(tmp); |
| 4792 | } else |
| 4793 | node->path_consumed = le16_to_cpu(pSMBr->PathConsumed); |
| 4794 | |
Igor Mammedov | fec4585 | 2008-05-16 13:06:30 +0400 | [diff] [blame] | 4795 | node->server_type = le16_to_cpu(ref->ServerType); |
| 4796 | node->ref_flag = le16_to_cpu(ref->ReferralEntryFlags); |
| 4797 | |
| 4798 | /* copy DfsPath */ |
| 4799 | temp = (char *)ref + le16_to_cpu(ref->DfsPathOffset); |
| 4800 | max_len = data_end - temp; |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 4801 | node->path_name = cifs_strndup_from_utf16(temp, max_len, |
| 4802 | is_unicode, nls_codepage); |
Jeff Layton | d8e2f53 | 2009-05-14 07:46:59 -0400 | [diff] [blame] | 4803 | if (!node->path_name) { |
| 4804 | rc = -ENOMEM; |
Steve French | a1fe78f | 2008-05-16 18:48:38 +0000 | [diff] [blame] | 4805 | goto parse_DFS_referrals_exit; |
Jeff Layton | 066ce68 | 2009-04-30 07:16:14 -0400 | [diff] [blame] | 4806 | } |
Igor Mammedov | fec4585 | 2008-05-16 13:06:30 +0400 | [diff] [blame] | 4807 | |
| 4808 | /* copy link target UNC */ |
| 4809 | temp = (char *)ref + le16_to_cpu(ref->NetworkAddressOffset); |
| 4810 | max_len = data_end - temp; |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 4811 | node->node_name = cifs_strndup_from_utf16(temp, max_len, |
| 4812 | is_unicode, nls_codepage); |
Stefan Metzmacher | d8f2799 | 2012-05-04 00:19:28 +0200 | [diff] [blame] | 4813 | if (!node->node_name) { |
Jeff Layton | d8e2f53 | 2009-05-14 07:46:59 -0400 | [diff] [blame] | 4814 | rc = -ENOMEM; |
Stefan Metzmacher | d8f2799 | 2012-05-04 00:19:28 +0200 | [diff] [blame] | 4815 | goto parse_DFS_referrals_exit; |
| 4816 | } |
| 4817 | |
| 4818 | ref++; |
Igor Mammedov | fec4585 | 2008-05-16 13:06:30 +0400 | [diff] [blame] | 4819 | } |
| 4820 | |
Steve French | a1fe78f | 2008-05-16 18:48:38 +0000 | [diff] [blame] | 4821 | parse_DFS_referrals_exit: |
Igor Mammedov | fec4585 | 2008-05-16 13:06:30 +0400 | [diff] [blame] | 4822 | if (rc) { |
| 4823 | free_dfs_info_array(*target_nodes, *num_of_nodes); |
| 4824 | *target_nodes = NULL; |
| 4825 | *num_of_nodes = 0; |
| 4826 | } |
| 4827 | return rc; |
| 4828 | } |
| 4829 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4830 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 4831 | CIFSGetDFSRefer(const unsigned int xid, struct cifs_ses *ses, |
Pavel Shilovsky | b669f33 | 2012-05-27 20:21:53 +0400 | [diff] [blame] | 4832 | const char *search_name, struct dfs_info3_param **target_nodes, |
Steve French | c2cf07d | 2008-05-15 06:20:02 +0000 | [diff] [blame] | 4833 | unsigned int *num_of_nodes, |
Steve French | 737b758 | 2005-04-28 22:41:06 -0700 | [diff] [blame] | 4834 | const struct nls_table *nls_codepage, int remap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4835 | { |
| 4836 | /* TRANS2_GET_DFS_REFERRAL */ |
| 4837 | TRANSACTION2_GET_DFS_REFER_REQ *pSMB = NULL; |
| 4838 | TRANSACTION2_GET_DFS_REFER_RSP *pSMBr = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4839 | int rc = 0; |
| 4840 | int bytes_returned; |
| 4841 | int name_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4842 | __u16 params, byte_count; |
Steve French | c2cf07d | 2008-05-15 06:20:02 +0000 | [diff] [blame] | 4843 | *num_of_nodes = 0; |
| 4844 | *target_nodes = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4845 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4846 | cifs_dbg(FYI, "In GetDFSRefer the path %s\n", search_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4847 | if (ses == NULL) |
| 4848 | return -ENODEV; |
| 4849 | getDFSRetry: |
| 4850 | rc = smb_init(SMB_COM_TRANSACTION2, 15, NULL, (void **) &pSMB, |
| 4851 | (void **) &pSMBr); |
| 4852 | if (rc) |
| 4853 | return rc; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4854 | |
| 4855 | /* server pointer checked in called function, |
Steve French | 1982c34 | 2005-08-17 12:38:22 -0700 | [diff] [blame] | 4856 | but should never be null here anyway */ |
Pavel Shilovsky | 8825736 | 2012-05-23 14:01:59 +0400 | [diff] [blame] | 4857 | pSMB->hdr.Mid = get_next_mid(ses->server); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4858 | pSMB->hdr.Tid = ses->ipc_tid; |
| 4859 | pSMB->hdr.Uid = ses->Suid; |
Steve French | 26f5736 | 2007-08-30 22:09:15 +0000 | [diff] [blame] | 4860 | if (ses->capabilities & CAP_STATUS32) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4861 | pSMB->hdr.Flags2 |= SMBFLG2_ERR_STATUS; |
Steve French | 26f5736 | 2007-08-30 22:09:15 +0000 | [diff] [blame] | 4862 | if (ses->capabilities & CAP_DFS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4863 | pSMB->hdr.Flags2 |= SMBFLG2_DFS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4864 | |
| 4865 | if (ses->capabilities & CAP_UNICODE) { |
| 4866 | pSMB->hdr.Flags2 |= SMBFLG2_UNICODE; |
| 4867 | name_len = |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 4868 | cifsConvertToUTF16((__le16 *) pSMB->RequestFileName, |
Pavel Shilovsky | b669f33 | 2012-05-27 20:21:53 +0400 | [diff] [blame] | 4869 | search_name, PATH_MAX, nls_codepage, |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 4870 | remap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4871 | name_len++; /* trailing null */ |
| 4872 | name_len *= 2; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4873 | } else { /* BB improve the check for buffer overruns BB */ |
Pavel Shilovsky | b669f33 | 2012-05-27 20:21:53 +0400 | [diff] [blame] | 4874 | name_len = strnlen(search_name, PATH_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4875 | name_len++; /* trailing null */ |
Pavel Shilovsky | b669f33 | 2012-05-27 20:21:53 +0400 | [diff] [blame] | 4876 | strncpy(pSMB->RequestFileName, search_name, name_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4877 | } |
| 4878 | |
Jeff Layton | 38d77c5 | 2013-05-26 07:01:00 -0400 | [diff] [blame] | 4879 | if (ses->server && ses->server->sign) |
| 4880 | pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE; |
Steve French | 1a4e15a | 2006-10-12 21:33:51 +0000 | [diff] [blame] | 4881 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4882 | pSMB->hdr.Uid = ses->Suid; |
Steve French | 1a4e15a | 2006-10-12 21:33:51 +0000 | [diff] [blame] | 4883 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4884 | params = 2 /* level */ + name_len /*includes null */ ; |
| 4885 | pSMB->TotalDataCount = 0; |
| 4886 | pSMB->DataCount = 0; |
| 4887 | pSMB->DataOffset = 0; |
| 4888 | pSMB->MaxParameterCount = 0; |
Steve French | 582d21e | 2008-05-13 04:54:12 +0000 | [diff] [blame] | 4889 | /* BB find exact max SMB PDU from sess structure BB */ |
| 4890 | pSMB->MaxDataCount = cpu_to_le16(4000); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4891 | pSMB->MaxSetupCount = 0; |
| 4892 | pSMB->Reserved = 0; |
| 4893 | pSMB->Flags = 0; |
| 4894 | pSMB->Timeout = 0; |
| 4895 | pSMB->Reserved2 = 0; |
| 4896 | pSMB->ParameterOffset = cpu_to_le16(offsetof( |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4897 | struct smb_com_transaction2_get_dfs_refer_req, MaxReferralLevel) - 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4898 | pSMB->SetupCount = 1; |
| 4899 | pSMB->Reserved3 = 0; |
| 4900 | pSMB->SubCommand = cpu_to_le16(TRANS2_GET_DFS_REFERRAL); |
| 4901 | byte_count = params + 3 /* pad */ ; |
| 4902 | pSMB->ParameterCount = cpu_to_le16(params); |
| 4903 | pSMB->TotalParameterCount = pSMB->ParameterCount; |
| 4904 | pSMB->MaxReferralLevel = cpu_to_le16(3); |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 4905 | inc_rfc1001_len(pSMB, byte_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4906 | pSMB->ByteCount = cpu_to_le16(byte_count); |
| 4907 | |
| 4908 | rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB, |
| 4909 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
| 4910 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4911 | cifs_dbg(FYI, "Send error in GetDFSRefer = %d\n", rc); |
Steve French | c2cf07d | 2008-05-15 06:20:02 +0000 | [diff] [blame] | 4912 | goto GetDFSRefExit; |
| 4913 | } |
| 4914 | rc = validate_t2((struct smb_t2_rsp *)pSMBr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4915 | |
Steve French | c2cf07d | 2008-05-15 06:20:02 +0000 | [diff] [blame] | 4916 | /* BB Also check if enough total bytes returned? */ |
Jeff Layton | 820a803 | 2011-05-04 08:05:26 -0400 | [diff] [blame] | 4917 | if (rc || get_bcc(&pSMBr->hdr) < 17) { |
Steve French | c2cf07d | 2008-05-15 06:20:02 +0000 | [diff] [blame] | 4918 | rc = -EIO; /* bad smb */ |
Igor Mammedov | fec4585 | 2008-05-16 13:06:30 +0400 | [diff] [blame] | 4919 | goto GetDFSRefExit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4920 | } |
Igor Mammedov | fec4585 | 2008-05-16 13:06:30 +0400 | [diff] [blame] | 4921 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4922 | cifs_dbg(FYI, "Decoding GetDFSRefer response BCC: %d Offset %d\n", |
| 4923 | get_bcc(&pSMBr->hdr), le16_to_cpu(pSMBr->t2.DataOffset)); |
Igor Mammedov | fec4585 | 2008-05-16 13:06:30 +0400 | [diff] [blame] | 4924 | |
| 4925 | /* parse returned result into more usable form */ |
Steve French | a1fe78f | 2008-05-16 18:48:38 +0000 | [diff] [blame] | 4926 | rc = parse_DFS_referrals(pSMBr, num_of_nodes, |
Igor Mammedov | 2c55608 | 2008-10-23 13:58:42 +0400 | [diff] [blame] | 4927 | target_nodes, nls_codepage, remap, |
Pavel Shilovsky | b669f33 | 2012-05-27 20:21:53 +0400 | [diff] [blame] | 4928 | search_name); |
Igor Mammedov | fec4585 | 2008-05-16 13:06:30 +0400 | [diff] [blame] | 4929 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4930 | GetDFSRefExit: |
Steve French | 0d817bc | 2008-05-22 02:02:03 +0000 | [diff] [blame] | 4931 | cifs_buf_release(pSMB); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4932 | |
| 4933 | if (rc == -EAGAIN) |
| 4934 | goto getDFSRetry; |
| 4935 | |
| 4936 | return rc; |
| 4937 | } |
| 4938 | |
Steve French | 2096243 | 2005-09-21 22:05:57 -0700 | [diff] [blame] | 4939 | /* Query File System Info such as free space to old servers such as Win 9x */ |
| 4940 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 4941 | SMBOldQFSInfo(const unsigned int xid, struct cifs_tcon *tcon, |
| 4942 | struct kstatfs *FSData) |
Steve French | 2096243 | 2005-09-21 22:05:57 -0700 | [diff] [blame] | 4943 | { |
| 4944 | /* level 0x01 SMB_QUERY_FILE_SYSTEM_INFO */ |
| 4945 | TRANSACTION2_QFSI_REQ *pSMB = NULL; |
| 4946 | TRANSACTION2_QFSI_RSP *pSMBr = NULL; |
| 4947 | FILE_SYSTEM_ALLOC_INFO *response_data; |
| 4948 | int rc = 0; |
| 4949 | int bytes_returned = 0; |
| 4950 | __u16 params, byte_count; |
| 4951 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4952 | cifs_dbg(FYI, "OldQFSInfo\n"); |
Steve French | 2096243 | 2005-09-21 22:05:57 -0700 | [diff] [blame] | 4953 | oldQFSInfoRetry: |
| 4954 | rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB, |
| 4955 | (void **) &pSMBr); |
| 4956 | if (rc) |
| 4957 | return rc; |
Steve French | 2096243 | 2005-09-21 22:05:57 -0700 | [diff] [blame] | 4958 | |
| 4959 | params = 2; /* level */ |
| 4960 | pSMB->TotalDataCount = 0; |
| 4961 | pSMB->MaxParameterCount = cpu_to_le16(2); |
| 4962 | pSMB->MaxDataCount = cpu_to_le16(1000); |
| 4963 | pSMB->MaxSetupCount = 0; |
| 4964 | pSMB->Reserved = 0; |
| 4965 | pSMB->Flags = 0; |
| 4966 | pSMB->Timeout = 0; |
| 4967 | pSMB->Reserved2 = 0; |
| 4968 | byte_count = params + 1 /* pad */ ; |
| 4969 | pSMB->TotalParameterCount = cpu_to_le16(params); |
| 4970 | pSMB->ParameterCount = pSMB->TotalParameterCount; |
| 4971 | pSMB->ParameterOffset = cpu_to_le16(offsetof( |
| 4972 | struct smb_com_transaction2_qfsi_req, InformationLevel) - 4); |
| 4973 | pSMB->DataCount = 0; |
| 4974 | pSMB->DataOffset = 0; |
| 4975 | pSMB->SetupCount = 1; |
| 4976 | pSMB->Reserved3 = 0; |
| 4977 | pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION); |
| 4978 | pSMB->InformationLevel = cpu_to_le16(SMB_INFO_ALLOCATION); |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 4979 | inc_rfc1001_len(pSMB, byte_count); |
Steve French | 2096243 | 2005-09-21 22:05:57 -0700 | [diff] [blame] | 4980 | pSMB->ByteCount = cpu_to_le16(byte_count); |
| 4981 | |
| 4982 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 4983 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
| 4984 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4985 | cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc); |
Steve French | 2096243 | 2005-09-21 22:05:57 -0700 | [diff] [blame] | 4986 | } else { /* decode response */ |
| 4987 | rc = validate_t2((struct smb_t2_rsp *)pSMBr); |
| 4988 | |
Jeff Layton | 820a803 | 2011-05-04 08:05:26 -0400 | [diff] [blame] | 4989 | if (rc || get_bcc(&pSMBr->hdr) < 18) |
Steve French | 2096243 | 2005-09-21 22:05:57 -0700 | [diff] [blame] | 4990 | rc = -EIO; /* bad smb */ |
| 4991 | else { |
| 4992 | __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4993 | cifs_dbg(FYI, "qfsinf resp BCC: %d Offset %d\n", |
Jeff Layton | 820a803 | 2011-05-04 08:05:26 -0400 | [diff] [blame] | 4994 | get_bcc(&pSMBr->hdr), data_offset); |
Steve French | 2096243 | 2005-09-21 22:05:57 -0700 | [diff] [blame] | 4995 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4996 | response_data = (FILE_SYSTEM_ALLOC_INFO *) |
Steve French | 2096243 | 2005-09-21 22:05:57 -0700 | [diff] [blame] | 4997 | (((char *) &pSMBr->hdr.Protocol) + data_offset); |
| 4998 | FSData->f_bsize = |
| 4999 | le16_to_cpu(response_data->BytesPerSector) * |
| 5000 | le32_to_cpu(response_data-> |
| 5001 | SectorsPerAllocationUnit); |
| 5002 | FSData->f_blocks = |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 5003 | le32_to_cpu(response_data->TotalAllocationUnits); |
Steve French | 2096243 | 2005-09-21 22:05:57 -0700 | [diff] [blame] | 5004 | FSData->f_bfree = FSData->f_bavail = |
| 5005 | le32_to_cpu(response_data->FreeAllocationUnits); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5006 | cifs_dbg(FYI, "Blocks: %lld Free: %lld Block size %ld\n", |
| 5007 | (unsigned long long)FSData->f_blocks, |
| 5008 | (unsigned long long)FSData->f_bfree, |
| 5009 | FSData->f_bsize); |
Steve French | 2096243 | 2005-09-21 22:05:57 -0700 | [diff] [blame] | 5010 | } |
| 5011 | } |
| 5012 | cifs_buf_release(pSMB); |
| 5013 | |
| 5014 | if (rc == -EAGAIN) |
| 5015 | goto oldQFSInfoRetry; |
| 5016 | |
| 5017 | return rc; |
| 5018 | } |
| 5019 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5020 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 5021 | CIFSSMBQFSInfo(const unsigned int xid, struct cifs_tcon *tcon, |
| 5022 | struct kstatfs *FSData) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5023 | { |
| 5024 | /* level 0x103 SMB_QUERY_FILE_SYSTEM_INFO */ |
| 5025 | TRANSACTION2_QFSI_REQ *pSMB = NULL; |
| 5026 | TRANSACTION2_QFSI_RSP *pSMBr = NULL; |
| 5027 | FILE_SYSTEM_INFO *response_data; |
| 5028 | int rc = 0; |
| 5029 | int bytes_returned = 0; |
| 5030 | __u16 params, byte_count; |
| 5031 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5032 | cifs_dbg(FYI, "In QFSInfo\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5033 | QFSInfoRetry: |
| 5034 | rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB, |
| 5035 | (void **) &pSMBr); |
| 5036 | if (rc) |
| 5037 | return rc; |
| 5038 | |
| 5039 | params = 2; /* level */ |
| 5040 | pSMB->TotalDataCount = 0; |
| 5041 | pSMB->MaxParameterCount = cpu_to_le16(2); |
Steve French | 2096243 | 2005-09-21 22:05:57 -0700 | [diff] [blame] | 5042 | pSMB->MaxDataCount = cpu_to_le16(1000); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5043 | pSMB->MaxSetupCount = 0; |
| 5044 | pSMB->Reserved = 0; |
| 5045 | pSMB->Flags = 0; |
| 5046 | pSMB->Timeout = 0; |
| 5047 | pSMB->Reserved2 = 0; |
| 5048 | byte_count = params + 1 /* pad */ ; |
| 5049 | pSMB->TotalParameterCount = cpu_to_le16(params); |
| 5050 | pSMB->ParameterCount = pSMB->TotalParameterCount; |
| 5051 | pSMB->ParameterOffset = cpu_to_le16(offsetof( |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 5052 | struct smb_com_transaction2_qfsi_req, InformationLevel) - 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5053 | pSMB->DataCount = 0; |
| 5054 | pSMB->DataOffset = 0; |
| 5055 | pSMB->SetupCount = 1; |
| 5056 | pSMB->Reserved3 = 0; |
| 5057 | pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION); |
| 5058 | pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_SIZE_INFO); |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 5059 | inc_rfc1001_len(pSMB, byte_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5060 | pSMB->ByteCount = cpu_to_le16(byte_count); |
| 5061 | |
| 5062 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 5063 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
| 5064 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5065 | cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5066 | } else { /* decode response */ |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 5067 | rc = validate_t2((struct smb_t2_rsp *)pSMBr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5068 | |
Jeff Layton | 820a803 | 2011-05-04 08:05:26 -0400 | [diff] [blame] | 5069 | if (rc || get_bcc(&pSMBr->hdr) < 24) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5070 | rc = -EIO; /* bad smb */ |
| 5071 | else { |
| 5072 | __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5073 | |
| 5074 | response_data = |
| 5075 | (FILE_SYSTEM_INFO |
| 5076 | *) (((char *) &pSMBr->hdr.Protocol) + |
| 5077 | data_offset); |
| 5078 | FSData->f_bsize = |
| 5079 | le32_to_cpu(response_data->BytesPerSector) * |
| 5080 | le32_to_cpu(response_data-> |
| 5081 | SectorsPerAllocationUnit); |
| 5082 | FSData->f_blocks = |
| 5083 | le64_to_cpu(response_data->TotalAllocationUnits); |
| 5084 | FSData->f_bfree = FSData->f_bavail = |
| 5085 | le64_to_cpu(response_data->FreeAllocationUnits); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5086 | cifs_dbg(FYI, "Blocks: %lld Free: %lld Block size %ld\n", |
| 5087 | (unsigned long long)FSData->f_blocks, |
| 5088 | (unsigned long long)FSData->f_bfree, |
| 5089 | FSData->f_bsize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5090 | } |
| 5091 | } |
| 5092 | cifs_buf_release(pSMB); |
| 5093 | |
| 5094 | if (rc == -EAGAIN) |
| 5095 | goto QFSInfoRetry; |
| 5096 | |
| 5097 | return rc; |
| 5098 | } |
| 5099 | |
| 5100 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 5101 | CIFSSMBQFSAttributeInfo(const unsigned int xid, struct cifs_tcon *tcon) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5102 | { |
| 5103 | /* level 0x105 SMB_QUERY_FILE_SYSTEM_INFO */ |
| 5104 | TRANSACTION2_QFSI_REQ *pSMB = NULL; |
| 5105 | TRANSACTION2_QFSI_RSP *pSMBr = NULL; |
| 5106 | FILE_SYSTEM_ATTRIBUTE_INFO *response_data; |
| 5107 | int rc = 0; |
| 5108 | int bytes_returned = 0; |
| 5109 | __u16 params, byte_count; |
| 5110 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5111 | cifs_dbg(FYI, "In QFSAttributeInfo\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5112 | QFSAttributeRetry: |
| 5113 | rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB, |
| 5114 | (void **) &pSMBr); |
| 5115 | if (rc) |
| 5116 | return rc; |
| 5117 | |
| 5118 | params = 2; /* level */ |
| 5119 | pSMB->TotalDataCount = 0; |
| 5120 | pSMB->MaxParameterCount = cpu_to_le16(2); |
Steve French | 582d21e | 2008-05-13 04:54:12 +0000 | [diff] [blame] | 5121 | /* BB find exact max SMB PDU from sess structure BB */ |
| 5122 | pSMB->MaxDataCount = cpu_to_le16(1000); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5123 | pSMB->MaxSetupCount = 0; |
| 5124 | pSMB->Reserved = 0; |
| 5125 | pSMB->Flags = 0; |
| 5126 | pSMB->Timeout = 0; |
| 5127 | pSMB->Reserved2 = 0; |
| 5128 | byte_count = params + 1 /* pad */ ; |
| 5129 | pSMB->TotalParameterCount = cpu_to_le16(params); |
| 5130 | pSMB->ParameterCount = pSMB->TotalParameterCount; |
| 5131 | pSMB->ParameterOffset = cpu_to_le16(offsetof( |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 5132 | struct smb_com_transaction2_qfsi_req, InformationLevel) - 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5133 | pSMB->DataCount = 0; |
| 5134 | pSMB->DataOffset = 0; |
| 5135 | pSMB->SetupCount = 1; |
| 5136 | pSMB->Reserved3 = 0; |
| 5137 | pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION); |
| 5138 | pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_ATTRIBUTE_INFO); |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 5139 | inc_rfc1001_len(pSMB, byte_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5140 | pSMB->ByteCount = cpu_to_le16(byte_count); |
| 5141 | |
| 5142 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 5143 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
| 5144 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5145 | cifs_dbg(VFS, "Send error in QFSAttributeInfo = %d\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5146 | } else { /* decode response */ |
| 5147 | rc = validate_t2((struct smb_t2_rsp *)pSMBr); |
| 5148 | |
Jeff Layton | 820a803 | 2011-05-04 08:05:26 -0400 | [diff] [blame] | 5149 | if (rc || get_bcc(&pSMBr->hdr) < 13) { |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 5150 | /* BB also check if enough bytes returned */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5151 | rc = -EIO; /* bad smb */ |
| 5152 | } else { |
| 5153 | __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset); |
| 5154 | response_data = |
| 5155 | (FILE_SYSTEM_ATTRIBUTE_INFO |
| 5156 | *) (((char *) &pSMBr->hdr.Protocol) + |
| 5157 | data_offset); |
| 5158 | memcpy(&tcon->fsAttrInfo, response_data, |
Steve French | 26f5736 | 2007-08-30 22:09:15 +0000 | [diff] [blame] | 5159 | sizeof(FILE_SYSTEM_ATTRIBUTE_INFO)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5160 | } |
| 5161 | } |
| 5162 | cifs_buf_release(pSMB); |
| 5163 | |
| 5164 | if (rc == -EAGAIN) |
| 5165 | goto QFSAttributeRetry; |
| 5166 | |
| 5167 | return rc; |
| 5168 | } |
| 5169 | |
| 5170 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 5171 | CIFSSMBQFSDeviceInfo(const unsigned int xid, struct cifs_tcon *tcon) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5172 | { |
| 5173 | /* level 0x104 SMB_QUERY_FILE_SYSTEM_INFO */ |
| 5174 | TRANSACTION2_QFSI_REQ *pSMB = NULL; |
| 5175 | TRANSACTION2_QFSI_RSP *pSMBr = NULL; |
| 5176 | FILE_SYSTEM_DEVICE_INFO *response_data; |
| 5177 | int rc = 0; |
| 5178 | int bytes_returned = 0; |
| 5179 | __u16 params, byte_count; |
| 5180 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5181 | cifs_dbg(FYI, "In QFSDeviceInfo\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5182 | QFSDeviceRetry: |
| 5183 | rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB, |
| 5184 | (void **) &pSMBr); |
| 5185 | if (rc) |
| 5186 | return rc; |
| 5187 | |
| 5188 | params = 2; /* level */ |
| 5189 | pSMB->TotalDataCount = 0; |
| 5190 | pSMB->MaxParameterCount = cpu_to_le16(2); |
Steve French | 582d21e | 2008-05-13 04:54:12 +0000 | [diff] [blame] | 5191 | /* BB find exact max SMB PDU from sess structure BB */ |
| 5192 | pSMB->MaxDataCount = cpu_to_le16(1000); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5193 | pSMB->MaxSetupCount = 0; |
| 5194 | pSMB->Reserved = 0; |
| 5195 | pSMB->Flags = 0; |
| 5196 | pSMB->Timeout = 0; |
| 5197 | pSMB->Reserved2 = 0; |
| 5198 | byte_count = params + 1 /* pad */ ; |
| 5199 | pSMB->TotalParameterCount = cpu_to_le16(params); |
| 5200 | pSMB->ParameterCount = pSMB->TotalParameterCount; |
| 5201 | pSMB->ParameterOffset = cpu_to_le16(offsetof( |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 5202 | struct smb_com_transaction2_qfsi_req, InformationLevel) - 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5203 | |
| 5204 | pSMB->DataCount = 0; |
| 5205 | pSMB->DataOffset = 0; |
| 5206 | pSMB->SetupCount = 1; |
| 5207 | pSMB->Reserved3 = 0; |
| 5208 | pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION); |
| 5209 | pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_DEVICE_INFO); |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 5210 | inc_rfc1001_len(pSMB, byte_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5211 | pSMB->ByteCount = cpu_to_le16(byte_count); |
| 5212 | |
| 5213 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 5214 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
| 5215 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5216 | cifs_dbg(FYI, "Send error in QFSDeviceInfo = %d\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5217 | } else { /* decode response */ |
| 5218 | rc = validate_t2((struct smb_t2_rsp *)pSMBr); |
| 5219 | |
Jeff Layton | 820a803 | 2011-05-04 08:05:26 -0400 | [diff] [blame] | 5220 | if (rc || get_bcc(&pSMBr->hdr) < |
| 5221 | sizeof(FILE_SYSTEM_DEVICE_INFO)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5222 | rc = -EIO; /* bad smb */ |
| 5223 | else { |
| 5224 | __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset); |
| 5225 | response_data = |
Steve French | 737b758 | 2005-04-28 22:41:06 -0700 | [diff] [blame] | 5226 | (FILE_SYSTEM_DEVICE_INFO *) |
| 5227 | (((char *) &pSMBr->hdr.Protocol) + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5228 | data_offset); |
| 5229 | memcpy(&tcon->fsDevInfo, response_data, |
Steve French | 26f5736 | 2007-08-30 22:09:15 +0000 | [diff] [blame] | 5230 | sizeof(FILE_SYSTEM_DEVICE_INFO)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5231 | } |
| 5232 | } |
| 5233 | cifs_buf_release(pSMB); |
| 5234 | |
| 5235 | if (rc == -EAGAIN) |
| 5236 | goto QFSDeviceRetry; |
| 5237 | |
| 5238 | return rc; |
| 5239 | } |
| 5240 | |
| 5241 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 5242 | CIFSSMBQFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5243 | { |
| 5244 | /* level 0x200 SMB_QUERY_CIFS_UNIX_INFO */ |
| 5245 | TRANSACTION2_QFSI_REQ *pSMB = NULL; |
| 5246 | TRANSACTION2_QFSI_RSP *pSMBr = NULL; |
| 5247 | FILE_SYSTEM_UNIX_INFO *response_data; |
| 5248 | int rc = 0; |
| 5249 | int bytes_returned = 0; |
| 5250 | __u16 params, byte_count; |
| 5251 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5252 | cifs_dbg(FYI, "In QFSUnixInfo\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5253 | QFSUnixRetry: |
Jeff Layton | f569599 | 2010-09-29 15:27:08 -0400 | [diff] [blame] | 5254 | rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon, |
| 5255 | (void **) &pSMB, (void **) &pSMBr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5256 | if (rc) |
| 5257 | return rc; |
| 5258 | |
| 5259 | params = 2; /* level */ |
| 5260 | pSMB->TotalDataCount = 0; |
| 5261 | pSMB->DataCount = 0; |
| 5262 | pSMB->DataOffset = 0; |
| 5263 | pSMB->MaxParameterCount = cpu_to_le16(2); |
Steve French | 582d21e | 2008-05-13 04:54:12 +0000 | [diff] [blame] | 5264 | /* BB find exact max SMB PDU from sess structure BB */ |
| 5265 | pSMB->MaxDataCount = cpu_to_le16(100); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5266 | pSMB->MaxSetupCount = 0; |
| 5267 | pSMB->Reserved = 0; |
| 5268 | pSMB->Flags = 0; |
| 5269 | pSMB->Timeout = 0; |
| 5270 | pSMB->Reserved2 = 0; |
| 5271 | byte_count = params + 1 /* pad */ ; |
| 5272 | pSMB->ParameterCount = cpu_to_le16(params); |
| 5273 | pSMB->TotalParameterCount = pSMB->ParameterCount; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 5274 | pSMB->ParameterOffset = cpu_to_le16(offsetof(struct |
| 5275 | smb_com_transaction2_qfsi_req, InformationLevel) - 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5276 | pSMB->SetupCount = 1; |
| 5277 | pSMB->Reserved3 = 0; |
| 5278 | pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION); |
| 5279 | pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_CIFS_UNIX_INFO); |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 5280 | inc_rfc1001_len(pSMB, byte_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5281 | pSMB->ByteCount = cpu_to_le16(byte_count); |
| 5282 | |
| 5283 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 5284 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
| 5285 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5286 | cifs_dbg(VFS, "Send error in QFSUnixInfo = %d\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5287 | } else { /* decode response */ |
| 5288 | rc = validate_t2((struct smb_t2_rsp *)pSMBr); |
| 5289 | |
Jeff Layton | 820a803 | 2011-05-04 08:05:26 -0400 | [diff] [blame] | 5290 | if (rc || get_bcc(&pSMBr->hdr) < 13) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5291 | rc = -EIO; /* bad smb */ |
| 5292 | } else { |
| 5293 | __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset); |
| 5294 | response_data = |
| 5295 | (FILE_SYSTEM_UNIX_INFO |
| 5296 | *) (((char *) &pSMBr->hdr.Protocol) + |
| 5297 | data_offset); |
| 5298 | memcpy(&tcon->fsUnixInfo, response_data, |
Steve French | 26f5736 | 2007-08-30 22:09:15 +0000 | [diff] [blame] | 5299 | sizeof(FILE_SYSTEM_UNIX_INFO)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5300 | } |
| 5301 | } |
| 5302 | cifs_buf_release(pSMB); |
| 5303 | |
| 5304 | if (rc == -EAGAIN) |
| 5305 | goto QFSUnixRetry; |
| 5306 | |
| 5307 | |
| 5308 | return rc; |
| 5309 | } |
| 5310 | |
Jeremy Allison | ac67055 | 2005-06-22 17:26:35 -0700 | [diff] [blame] | 5311 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 5312 | CIFSSMBSetFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon, __u64 cap) |
Jeremy Allison | ac67055 | 2005-06-22 17:26:35 -0700 | [diff] [blame] | 5313 | { |
| 5314 | /* level 0x200 SMB_SET_CIFS_UNIX_INFO */ |
| 5315 | TRANSACTION2_SETFSI_REQ *pSMB = NULL; |
| 5316 | TRANSACTION2_SETFSI_RSP *pSMBr = NULL; |
| 5317 | int rc = 0; |
| 5318 | int bytes_returned = 0; |
| 5319 | __u16 params, param_offset, offset, byte_count; |
| 5320 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5321 | cifs_dbg(FYI, "In SETFSUnixInfo\n"); |
Jeremy Allison | ac67055 | 2005-06-22 17:26:35 -0700 | [diff] [blame] | 5322 | SETFSUnixRetry: |
Steve French | f26282c | 2006-03-01 09:17:37 +0000 | [diff] [blame] | 5323 | /* BB switch to small buf init to save memory */ |
Jeff Layton | f569599 | 2010-09-29 15:27:08 -0400 | [diff] [blame] | 5324 | rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon, |
| 5325 | (void **) &pSMB, (void **) &pSMBr); |
Jeremy Allison | ac67055 | 2005-06-22 17:26:35 -0700 | [diff] [blame] | 5326 | if (rc) |
| 5327 | return rc; |
| 5328 | |
| 5329 | params = 4; /* 2 bytes zero followed by info level. */ |
| 5330 | pSMB->MaxSetupCount = 0; |
| 5331 | pSMB->Reserved = 0; |
| 5332 | pSMB->Flags = 0; |
| 5333 | pSMB->Timeout = 0; |
| 5334 | pSMB->Reserved2 = 0; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 5335 | param_offset = offsetof(struct smb_com_transaction2_setfsi_req, FileNum) |
| 5336 | - 4; |
Jeremy Allison | ac67055 | 2005-06-22 17:26:35 -0700 | [diff] [blame] | 5337 | offset = param_offset + params; |
| 5338 | |
| 5339 | pSMB->MaxParameterCount = cpu_to_le16(4); |
Steve French | 582d21e | 2008-05-13 04:54:12 +0000 | [diff] [blame] | 5340 | /* BB find exact max SMB PDU from sess structure BB */ |
| 5341 | pSMB->MaxDataCount = cpu_to_le16(100); |
Jeremy Allison | ac67055 | 2005-06-22 17:26:35 -0700 | [diff] [blame] | 5342 | pSMB->SetupCount = 1; |
| 5343 | pSMB->Reserved3 = 0; |
| 5344 | pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FS_INFORMATION); |
| 5345 | byte_count = 1 /* pad */ + params + 12; |
| 5346 | |
| 5347 | pSMB->DataCount = cpu_to_le16(12); |
| 5348 | pSMB->ParameterCount = cpu_to_le16(params); |
| 5349 | pSMB->TotalDataCount = pSMB->DataCount; |
| 5350 | pSMB->TotalParameterCount = pSMB->ParameterCount; |
| 5351 | pSMB->ParameterOffset = cpu_to_le16(param_offset); |
| 5352 | pSMB->DataOffset = cpu_to_le16(offset); |
| 5353 | |
| 5354 | /* Params. */ |
| 5355 | pSMB->FileNum = 0; |
| 5356 | pSMB->InformationLevel = cpu_to_le16(SMB_SET_CIFS_UNIX_INFO); |
| 5357 | |
| 5358 | /* Data. */ |
| 5359 | pSMB->ClientUnixMajor = cpu_to_le16(CIFS_UNIX_MAJOR_VERSION); |
| 5360 | pSMB->ClientUnixMinor = cpu_to_le16(CIFS_UNIX_MINOR_VERSION); |
| 5361 | pSMB->ClientUnixCap = cpu_to_le64(cap); |
| 5362 | |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 5363 | inc_rfc1001_len(pSMB, byte_count); |
Jeremy Allison | ac67055 | 2005-06-22 17:26:35 -0700 | [diff] [blame] | 5364 | pSMB->ByteCount = cpu_to_le16(byte_count); |
| 5365 | |
| 5366 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 5367 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
| 5368 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5369 | cifs_dbg(VFS, "Send error in SETFSUnixInfo = %d\n", rc); |
Jeremy Allison | ac67055 | 2005-06-22 17:26:35 -0700 | [diff] [blame] | 5370 | } else { /* decode response */ |
| 5371 | rc = validate_t2((struct smb_t2_rsp *)pSMBr); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 5372 | if (rc) |
Jeremy Allison | ac67055 | 2005-06-22 17:26:35 -0700 | [diff] [blame] | 5373 | rc = -EIO; /* bad smb */ |
Jeremy Allison | ac67055 | 2005-06-22 17:26:35 -0700 | [diff] [blame] | 5374 | } |
| 5375 | cifs_buf_release(pSMB); |
| 5376 | |
| 5377 | if (rc == -EAGAIN) |
| 5378 | goto SETFSUnixRetry; |
| 5379 | |
| 5380 | return rc; |
| 5381 | } |
| 5382 | |
| 5383 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5384 | |
| 5385 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 5386 | CIFSSMBQFSPosixInfo(const unsigned int xid, struct cifs_tcon *tcon, |
Steve French | 737b758 | 2005-04-28 22:41:06 -0700 | [diff] [blame] | 5387 | struct kstatfs *FSData) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5388 | { |
| 5389 | /* level 0x201 SMB_QUERY_CIFS_POSIX_INFO */ |
| 5390 | TRANSACTION2_QFSI_REQ *pSMB = NULL; |
| 5391 | TRANSACTION2_QFSI_RSP *pSMBr = NULL; |
| 5392 | FILE_SYSTEM_POSIX_INFO *response_data; |
| 5393 | int rc = 0; |
| 5394 | int bytes_returned = 0; |
| 5395 | __u16 params, byte_count; |
| 5396 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5397 | cifs_dbg(FYI, "In QFSPosixInfo\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5398 | QFSPosixRetry: |
| 5399 | rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB, |
| 5400 | (void **) &pSMBr); |
| 5401 | if (rc) |
| 5402 | return rc; |
| 5403 | |
| 5404 | params = 2; /* level */ |
| 5405 | pSMB->TotalDataCount = 0; |
| 5406 | pSMB->DataCount = 0; |
| 5407 | pSMB->DataOffset = 0; |
| 5408 | pSMB->MaxParameterCount = cpu_to_le16(2); |
Steve French | 582d21e | 2008-05-13 04:54:12 +0000 | [diff] [blame] | 5409 | /* BB find exact max SMB PDU from sess structure BB */ |
| 5410 | pSMB->MaxDataCount = cpu_to_le16(100); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5411 | pSMB->MaxSetupCount = 0; |
| 5412 | pSMB->Reserved = 0; |
| 5413 | pSMB->Flags = 0; |
| 5414 | pSMB->Timeout = 0; |
| 5415 | pSMB->Reserved2 = 0; |
| 5416 | byte_count = params + 1 /* pad */ ; |
| 5417 | pSMB->ParameterCount = cpu_to_le16(params); |
| 5418 | pSMB->TotalParameterCount = pSMB->ParameterCount; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 5419 | pSMB->ParameterOffset = cpu_to_le16(offsetof(struct |
| 5420 | smb_com_transaction2_qfsi_req, InformationLevel) - 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5421 | pSMB->SetupCount = 1; |
| 5422 | pSMB->Reserved3 = 0; |
| 5423 | pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION); |
| 5424 | pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_FS_INFO); |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 5425 | inc_rfc1001_len(pSMB, byte_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5426 | pSMB->ByteCount = cpu_to_le16(byte_count); |
| 5427 | |
| 5428 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 5429 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
| 5430 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5431 | cifs_dbg(FYI, "Send error in QFSUnixInfo = %d\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5432 | } else { /* decode response */ |
| 5433 | rc = validate_t2((struct smb_t2_rsp *)pSMBr); |
| 5434 | |
Jeff Layton | 820a803 | 2011-05-04 08:05:26 -0400 | [diff] [blame] | 5435 | if (rc || get_bcc(&pSMBr->hdr) < 13) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5436 | rc = -EIO; /* bad smb */ |
| 5437 | } else { |
| 5438 | __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset); |
| 5439 | response_data = |
| 5440 | (FILE_SYSTEM_POSIX_INFO |
| 5441 | *) (((char *) &pSMBr->hdr.Protocol) + |
| 5442 | data_offset); |
| 5443 | FSData->f_bsize = |
| 5444 | le32_to_cpu(response_data->BlockSize); |
| 5445 | FSData->f_blocks = |
| 5446 | le64_to_cpu(response_data->TotalBlocks); |
| 5447 | FSData->f_bfree = |
| 5448 | le64_to_cpu(response_data->BlocksAvail); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 5449 | if (response_data->UserBlocksAvail == cpu_to_le64(-1)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5450 | FSData->f_bavail = FSData->f_bfree; |
| 5451 | } else { |
| 5452 | FSData->f_bavail = |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 5453 | le64_to_cpu(response_data->UserBlocksAvail); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5454 | } |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 5455 | if (response_data->TotalFileNodes != cpu_to_le64(-1)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5456 | FSData->f_files = |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 5457 | le64_to_cpu(response_data->TotalFileNodes); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 5458 | if (response_data->FreeFileNodes != cpu_to_le64(-1)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5459 | FSData->f_ffree = |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 5460 | le64_to_cpu(response_data->FreeFileNodes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5461 | } |
| 5462 | } |
| 5463 | cifs_buf_release(pSMB); |
| 5464 | |
| 5465 | if (rc == -EAGAIN) |
| 5466 | goto QFSPosixRetry; |
| 5467 | |
| 5468 | return rc; |
| 5469 | } |
| 5470 | |
| 5471 | |
Pavel Shilovsky | d143341 | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 5472 | /* |
| 5473 | * We can not use write of zero bytes trick to set file size due to need for |
| 5474 | * large file support. Also note that this SetPathInfo is preferred to |
| 5475 | * SetFileInfo based method in next routine which is only needed to work around |
| 5476 | * a sharing violation bugin Samba which this routine can run into. |
| 5477 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5478 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 5479 | CIFSSMBSetEOF(const unsigned int xid, struct cifs_tcon *tcon, |
Pavel Shilovsky | d143341 | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 5480 | const char *file_name, __u64 size, struct cifs_sb_info *cifs_sb, |
| 5481 | bool set_allocation) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5482 | { |
| 5483 | struct smb_com_transaction2_spi_req *pSMB = NULL; |
| 5484 | struct smb_com_transaction2_spi_rsp *pSMBr = NULL; |
| 5485 | struct file_end_of_file_info *parm_data; |
| 5486 | int name_len; |
| 5487 | int rc = 0; |
| 5488 | int bytes_returned = 0; |
Pavel Shilovsky | d143341 | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 5489 | int remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR; |
| 5490 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5491 | __u16 params, byte_count, data_count, param_offset, offset; |
| 5492 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5493 | cifs_dbg(FYI, "In SetEOF\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5494 | SetEOFRetry: |
| 5495 | rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB, |
| 5496 | (void **) &pSMBr); |
| 5497 | if (rc) |
| 5498 | return rc; |
| 5499 | |
| 5500 | if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) { |
| 5501 | name_len = |
Pavel Shilovsky | d143341 | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 5502 | cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name, |
| 5503 | PATH_MAX, cifs_sb->local_nls, remap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5504 | name_len++; /* trailing null */ |
| 5505 | name_len *= 2; |
Steve French | 3e87d80 | 2005-09-18 20:49:21 -0700 | [diff] [blame] | 5506 | } else { /* BB improve the check for buffer overruns BB */ |
Pavel Shilovsky | d143341 | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 5507 | name_len = strnlen(file_name, PATH_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5508 | name_len++; /* trailing null */ |
Pavel Shilovsky | d143341 | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 5509 | strncpy(pSMB->FileName, file_name, name_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5510 | } |
| 5511 | params = 6 + name_len; |
Steve French | 26f5736 | 2007-08-30 22:09:15 +0000 | [diff] [blame] | 5512 | data_count = sizeof(struct file_end_of_file_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5513 | pSMB->MaxParameterCount = cpu_to_le16(2); |
Steve French | 3e87d80 | 2005-09-18 20:49:21 -0700 | [diff] [blame] | 5514 | pSMB->MaxDataCount = cpu_to_le16(4100); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5515 | pSMB->MaxSetupCount = 0; |
| 5516 | pSMB->Reserved = 0; |
| 5517 | pSMB->Flags = 0; |
| 5518 | pSMB->Timeout = 0; |
| 5519 | pSMB->Reserved2 = 0; |
| 5520 | param_offset = offsetof(struct smb_com_transaction2_spi_req, |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 5521 | InformationLevel) - 4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5522 | offset = param_offset + params; |
Pavel Shilovsky | d143341 | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 5523 | if (set_allocation) { |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 5524 | if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU) |
| 5525 | pSMB->InformationLevel = |
| 5526 | cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2); |
| 5527 | else |
| 5528 | pSMB->InformationLevel = |
| 5529 | cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO); |
| 5530 | } else /* Set File Size */ { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5531 | if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU) |
| 5532 | pSMB->InformationLevel = |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 5533 | cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5534 | else |
| 5535 | pSMB->InformationLevel = |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 5536 | cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5537 | } |
| 5538 | |
| 5539 | parm_data = |
| 5540 | (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol) + |
| 5541 | offset); |
| 5542 | pSMB->ParameterOffset = cpu_to_le16(param_offset); |
| 5543 | pSMB->DataOffset = cpu_to_le16(offset); |
| 5544 | pSMB->SetupCount = 1; |
| 5545 | pSMB->Reserved3 = 0; |
| 5546 | pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION); |
| 5547 | byte_count = 3 /* pad */ + params + data_count; |
| 5548 | pSMB->DataCount = cpu_to_le16(data_count); |
| 5549 | pSMB->TotalDataCount = pSMB->DataCount; |
| 5550 | pSMB->ParameterCount = cpu_to_le16(params); |
| 5551 | pSMB->TotalParameterCount = pSMB->ParameterCount; |
| 5552 | pSMB->Reserved4 = 0; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 5553 | inc_rfc1001_len(pSMB, byte_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5554 | parm_data->FileSize = cpu_to_le64(size); |
| 5555 | pSMB->ByteCount = cpu_to_le16(byte_count); |
| 5556 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 5557 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 5558 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5559 | cifs_dbg(FYI, "SetPathInfo (file size) returned %d\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5560 | |
| 5561 | cifs_buf_release(pSMB); |
| 5562 | |
| 5563 | if (rc == -EAGAIN) |
| 5564 | goto SetEOFRetry; |
| 5565 | |
| 5566 | return rc; |
| 5567 | } |
| 5568 | |
| 5569 | int |
Pavel Shilovsky | d143341 | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 5570 | CIFSSMBSetFileSize(const unsigned int xid, struct cifs_tcon *tcon, |
| 5571 | struct cifsFileInfo *cfile, __u64 size, bool set_allocation) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5572 | { |
| 5573 | struct smb_com_transaction2_sfi_req *pSMB = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5574 | struct file_end_of_file_info *parm_data; |
| 5575 | int rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5576 | __u16 params, param_offset, offset, byte_count, count; |
| 5577 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5578 | cifs_dbg(FYI, "SetFileSize (via SetFileInfo) %lld\n", |
| 5579 | (long long)size); |
Steve French | cd63499 | 2005-04-28 22:41:10 -0700 | [diff] [blame] | 5580 | rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB); |
| 5581 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5582 | if (rc) |
| 5583 | return rc; |
| 5584 | |
Pavel Shilovsky | d143341 | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 5585 | pSMB->hdr.Pid = cpu_to_le16((__u16)cfile->pid); |
| 5586 | pSMB->hdr.PidHigh = cpu_to_le16((__u16)(cfile->pid >> 16)); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 5587 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5588 | params = 6; |
| 5589 | pSMB->MaxSetupCount = 0; |
| 5590 | pSMB->Reserved = 0; |
| 5591 | pSMB->Flags = 0; |
| 5592 | pSMB->Timeout = 0; |
| 5593 | pSMB->Reserved2 = 0; |
| 5594 | param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4; |
| 5595 | offset = param_offset + params; |
| 5596 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5597 | count = sizeof(struct file_end_of_file_info); |
| 5598 | pSMB->MaxParameterCount = cpu_to_le16(2); |
Steve French | 582d21e | 2008-05-13 04:54:12 +0000 | [diff] [blame] | 5599 | /* BB find exact max SMB PDU from sess structure BB */ |
| 5600 | pSMB->MaxDataCount = cpu_to_le16(1000); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5601 | pSMB->SetupCount = 1; |
| 5602 | pSMB->Reserved3 = 0; |
| 5603 | pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION); |
| 5604 | byte_count = 3 /* pad */ + params + count; |
| 5605 | pSMB->DataCount = cpu_to_le16(count); |
| 5606 | pSMB->ParameterCount = cpu_to_le16(params); |
| 5607 | pSMB->TotalDataCount = pSMB->DataCount; |
| 5608 | pSMB->TotalParameterCount = pSMB->ParameterCount; |
| 5609 | pSMB->ParameterOffset = cpu_to_le16(param_offset); |
| 5610 | parm_data = |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 5611 | (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol) |
| 5612 | + offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5613 | pSMB->DataOffset = cpu_to_le16(offset); |
| 5614 | parm_data->FileSize = cpu_to_le64(size); |
Pavel Shilovsky | d143341 | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 5615 | pSMB->Fid = cfile->fid.netfid; |
| 5616 | if (set_allocation) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5617 | if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU) |
| 5618 | pSMB->InformationLevel = |
| 5619 | cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2); |
| 5620 | else |
| 5621 | pSMB->InformationLevel = |
| 5622 | cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 5623 | } else /* Set File Size */ { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5624 | if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU) |
| 5625 | pSMB->InformationLevel = |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 5626 | cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5627 | else |
| 5628 | pSMB->InformationLevel = |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 5629 | cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5630 | } |
| 5631 | pSMB->Reserved4 = 0; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 5632 | inc_rfc1001_len(pSMB, byte_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5633 | pSMB->ByteCount = cpu_to_le16(byte_count); |
Pavel Shilovsky | 792af7b | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 5634 | rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5635 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5636 | cifs_dbg(FYI, "Send error in SetFileInfo (SetFileSize) = %d\n", |
| 5637 | rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5638 | } |
| 5639 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 5640 | /* Note: On -EAGAIN error only caller can retry on handle based calls |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5641 | since file handle passed in no longer valid */ |
| 5642 | |
| 5643 | return rc; |
| 5644 | } |
| 5645 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 5646 | /* Some legacy servers such as NT4 require that the file times be set on |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5647 | an open handle, rather than by pathname - this is awkward due to |
| 5648 | potential access conflicts on the open, but it is unavoidable for these |
| 5649 | old servers since the only other choice is to go from 100 nanosecond DCE |
| 5650 | time and resort to the original setpathinfo level which takes the ancient |
| 5651 | DOS time format with 2 second granularity */ |
| 5652 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 5653 | CIFSSMBSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon, |
Jeff Layton | 2dd2dfa | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 5654 | const FILE_BASIC_INFO *data, __u16 fid, __u32 pid_of_opener) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5655 | { |
| 5656 | struct smb_com_transaction2_sfi_req *pSMB = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5657 | char *data_offset; |
| 5658 | int rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5659 | __u16 params, param_offset, offset, byte_count, count; |
| 5660 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5661 | cifs_dbg(FYI, "Set Times (via SetFileInfo)\n"); |
Steve French | cd63499 | 2005-04-28 22:41:10 -0700 | [diff] [blame] | 5662 | rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB); |
| 5663 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5664 | if (rc) |
| 5665 | return rc; |
| 5666 | |
Jeff Layton | 2dd2dfa | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 5667 | pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener); |
| 5668 | pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16)); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 5669 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5670 | params = 6; |
| 5671 | pSMB->MaxSetupCount = 0; |
| 5672 | pSMB->Reserved = 0; |
| 5673 | pSMB->Flags = 0; |
| 5674 | pSMB->Timeout = 0; |
| 5675 | pSMB->Reserved2 = 0; |
| 5676 | param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4; |
| 5677 | offset = param_offset + params; |
| 5678 | |
Jeff Layton | b2a3ad9 | 2012-03-26 09:55:29 -0400 | [diff] [blame] | 5679 | data_offset = (char *)pSMB + |
| 5680 | offsetof(struct smb_hdr, Protocol) + offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5681 | |
Steve French | 26f5736 | 2007-08-30 22:09:15 +0000 | [diff] [blame] | 5682 | count = sizeof(FILE_BASIC_INFO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5683 | pSMB->MaxParameterCount = cpu_to_le16(2); |
Steve French | 582d21e | 2008-05-13 04:54:12 +0000 | [diff] [blame] | 5684 | /* BB find max SMB PDU from sess */ |
| 5685 | pSMB->MaxDataCount = cpu_to_le16(1000); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5686 | pSMB->SetupCount = 1; |
| 5687 | pSMB->Reserved3 = 0; |
| 5688 | pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION); |
| 5689 | byte_count = 3 /* pad */ + params + count; |
| 5690 | pSMB->DataCount = cpu_to_le16(count); |
| 5691 | pSMB->ParameterCount = cpu_to_le16(params); |
| 5692 | pSMB->TotalDataCount = pSMB->DataCount; |
| 5693 | pSMB->TotalParameterCount = pSMB->ParameterCount; |
| 5694 | pSMB->ParameterOffset = cpu_to_le16(param_offset); |
| 5695 | pSMB->DataOffset = cpu_to_le16(offset); |
| 5696 | pSMB->Fid = fid; |
| 5697 | if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU) |
| 5698 | pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2); |
| 5699 | else |
| 5700 | pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO); |
| 5701 | pSMB->Reserved4 = 0; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 5702 | inc_rfc1001_len(pSMB, byte_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5703 | pSMB->ByteCount = cpu_to_le16(byte_count); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 5704 | memcpy(data_offset, data, sizeof(FILE_BASIC_INFO)); |
Pavel Shilovsky | 792af7b | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 5705 | rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 5706 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5707 | cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n", |
| 5708 | rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5709 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 5710 | /* Note: On -EAGAIN error only caller can retry on handle based calls |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5711 | since file handle passed in no longer valid */ |
| 5712 | |
| 5713 | return rc; |
| 5714 | } |
| 5715 | |
Jeff Layton | 6d22f09 | 2008-09-23 11:48:35 -0400 | [diff] [blame] | 5716 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 5717 | CIFSSMBSetFileDisposition(const unsigned int xid, struct cifs_tcon *tcon, |
Jeff Layton | 6d22f09 | 2008-09-23 11:48:35 -0400 | [diff] [blame] | 5718 | bool delete_file, __u16 fid, __u32 pid_of_opener) |
| 5719 | { |
| 5720 | struct smb_com_transaction2_sfi_req *pSMB = NULL; |
| 5721 | char *data_offset; |
| 5722 | int rc = 0; |
| 5723 | __u16 params, param_offset, offset, byte_count, count; |
| 5724 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5725 | cifs_dbg(FYI, "Set File Disposition (via SetFileInfo)\n"); |
Jeff Layton | 6d22f09 | 2008-09-23 11:48:35 -0400 | [diff] [blame] | 5726 | rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB); |
| 5727 | |
| 5728 | if (rc) |
| 5729 | return rc; |
| 5730 | |
| 5731 | pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener); |
| 5732 | pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16)); |
| 5733 | |
| 5734 | params = 6; |
| 5735 | pSMB->MaxSetupCount = 0; |
| 5736 | pSMB->Reserved = 0; |
| 5737 | pSMB->Flags = 0; |
| 5738 | pSMB->Timeout = 0; |
| 5739 | pSMB->Reserved2 = 0; |
| 5740 | param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4; |
| 5741 | offset = param_offset + params; |
| 5742 | |
| 5743 | data_offset = (char *) (&pSMB->hdr.Protocol) + offset; |
| 5744 | |
| 5745 | count = 1; |
| 5746 | pSMB->MaxParameterCount = cpu_to_le16(2); |
| 5747 | /* BB find max SMB PDU from sess */ |
| 5748 | pSMB->MaxDataCount = cpu_to_le16(1000); |
| 5749 | pSMB->SetupCount = 1; |
| 5750 | pSMB->Reserved3 = 0; |
| 5751 | pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION); |
| 5752 | byte_count = 3 /* pad */ + params + count; |
| 5753 | pSMB->DataCount = cpu_to_le16(count); |
| 5754 | pSMB->ParameterCount = cpu_to_le16(params); |
| 5755 | pSMB->TotalDataCount = pSMB->DataCount; |
| 5756 | pSMB->TotalParameterCount = pSMB->ParameterCount; |
| 5757 | pSMB->ParameterOffset = cpu_to_le16(param_offset); |
| 5758 | pSMB->DataOffset = cpu_to_le16(offset); |
| 5759 | pSMB->Fid = fid; |
| 5760 | pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_DISPOSITION_INFO); |
| 5761 | pSMB->Reserved4 = 0; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 5762 | inc_rfc1001_len(pSMB, byte_count); |
Jeff Layton | 6d22f09 | 2008-09-23 11:48:35 -0400 | [diff] [blame] | 5763 | pSMB->ByteCount = cpu_to_le16(byte_count); |
| 5764 | *data_offset = delete_file ? 1 : 0; |
Pavel Shilovsky | 792af7b | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 5765 | rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0); |
Jeff Layton | 6d22f09 | 2008-09-23 11:48:35 -0400 | [diff] [blame] | 5766 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5767 | cifs_dbg(FYI, "Send error in SetFileDisposition = %d\n", rc); |
Jeff Layton | 6d22f09 | 2008-09-23 11:48:35 -0400 | [diff] [blame] | 5768 | |
| 5769 | return rc; |
| 5770 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5771 | |
| 5772 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 5773 | CIFSSMBSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon, |
Jeff Layton | 6fc000e | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 5774 | const char *fileName, const FILE_BASIC_INFO *data, |
| 5775 | const struct nls_table *nls_codepage, int remap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5776 | { |
| 5777 | TRANSACTION2_SPI_REQ *pSMB = NULL; |
| 5778 | TRANSACTION2_SPI_RSP *pSMBr = NULL; |
| 5779 | int name_len; |
| 5780 | int rc = 0; |
| 5781 | int bytes_returned = 0; |
| 5782 | char *data_offset; |
| 5783 | __u16 params, param_offset, offset, byte_count, count; |
| 5784 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5785 | cifs_dbg(FYI, "In SetTimes\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5786 | |
| 5787 | SetTimesRetry: |
| 5788 | rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB, |
| 5789 | (void **) &pSMBr); |
| 5790 | if (rc) |
| 5791 | return rc; |
| 5792 | |
| 5793 | if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) { |
| 5794 | name_len = |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 5795 | cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName, |
| 5796 | PATH_MAX, nls_codepage, remap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5797 | name_len++; /* trailing null */ |
| 5798 | name_len *= 2; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 5799 | } else { /* BB improve the check for buffer overruns BB */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5800 | name_len = strnlen(fileName, PATH_MAX); |
| 5801 | name_len++; /* trailing null */ |
| 5802 | strncpy(pSMB->FileName, fileName, name_len); |
| 5803 | } |
| 5804 | |
| 5805 | params = 6 + name_len; |
Steve French | 26f5736 | 2007-08-30 22:09:15 +0000 | [diff] [blame] | 5806 | count = sizeof(FILE_BASIC_INFO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5807 | pSMB->MaxParameterCount = cpu_to_le16(2); |
Steve French | 582d21e | 2008-05-13 04:54:12 +0000 | [diff] [blame] | 5808 | /* BB find max SMB PDU from sess structure BB */ |
| 5809 | pSMB->MaxDataCount = cpu_to_le16(1000); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5810 | pSMB->MaxSetupCount = 0; |
| 5811 | pSMB->Reserved = 0; |
| 5812 | pSMB->Flags = 0; |
| 5813 | pSMB->Timeout = 0; |
| 5814 | pSMB->Reserved2 = 0; |
| 5815 | param_offset = offsetof(struct smb_com_transaction2_spi_req, |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 5816 | InformationLevel) - 4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5817 | offset = param_offset + params; |
| 5818 | data_offset = (char *) (&pSMB->hdr.Protocol) + offset; |
| 5819 | pSMB->ParameterOffset = cpu_to_le16(param_offset); |
| 5820 | pSMB->DataOffset = cpu_to_le16(offset); |
| 5821 | pSMB->SetupCount = 1; |
| 5822 | pSMB->Reserved3 = 0; |
| 5823 | pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION); |
| 5824 | byte_count = 3 /* pad */ + params + count; |
| 5825 | |
| 5826 | pSMB->DataCount = cpu_to_le16(count); |
| 5827 | pSMB->ParameterCount = cpu_to_le16(params); |
| 5828 | pSMB->TotalDataCount = pSMB->DataCount; |
| 5829 | pSMB->TotalParameterCount = pSMB->ParameterCount; |
| 5830 | if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU) |
| 5831 | pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2); |
| 5832 | else |
| 5833 | pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO); |
| 5834 | pSMB->Reserved4 = 0; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 5835 | inc_rfc1001_len(pSMB, byte_count); |
Steve French | 26f5736 | 2007-08-30 22:09:15 +0000 | [diff] [blame] | 5836 | memcpy(data_offset, data, sizeof(FILE_BASIC_INFO)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5837 | pSMB->ByteCount = cpu_to_le16(byte_count); |
| 5838 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 5839 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 5840 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5841 | cifs_dbg(FYI, "SetPathInfo (times) returned %d\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5842 | |
| 5843 | cifs_buf_release(pSMB); |
| 5844 | |
| 5845 | if (rc == -EAGAIN) |
| 5846 | goto SetTimesRetry; |
| 5847 | |
| 5848 | return rc; |
| 5849 | } |
| 5850 | |
| 5851 | /* Can not be used to set time stamps yet (due to old DOS time format) */ |
| 5852 | /* Can be used to set attributes */ |
| 5853 | #if 0 /* Possibly not needed - since it turns out that strangely NT4 has a bug |
| 5854 | handling it anyway and NT4 was what we thought it would be needed for |
| 5855 | Do not delete it until we prove whether needed for Win9x though */ |
| 5856 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 5857 | CIFSSMBSetAttrLegacy(unsigned int xid, struct cifs_tcon *tcon, char *fileName, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5858 | __u16 dos_attrs, const struct nls_table *nls_codepage) |
| 5859 | { |
| 5860 | SETATTR_REQ *pSMB = NULL; |
| 5861 | SETATTR_RSP *pSMBr = NULL; |
| 5862 | int rc = 0; |
| 5863 | int bytes_returned; |
| 5864 | int name_len; |
| 5865 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5866 | cifs_dbg(FYI, "In SetAttrLegacy\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5867 | |
| 5868 | SetAttrLgcyRetry: |
| 5869 | rc = smb_init(SMB_COM_SETATTR, 8, tcon, (void **) &pSMB, |
| 5870 | (void **) &pSMBr); |
| 5871 | if (rc) |
| 5872 | return rc; |
| 5873 | |
| 5874 | if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) { |
| 5875 | name_len = |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 5876 | ConvertToUTF16((__le16 *) pSMB->fileName, fileName, |
| 5877 | PATH_MAX, nls_codepage); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5878 | name_len++; /* trailing null */ |
| 5879 | name_len *= 2; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 5880 | } else { /* BB improve the check for buffer overruns BB */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5881 | name_len = strnlen(fileName, PATH_MAX); |
| 5882 | name_len++; /* trailing null */ |
| 5883 | strncpy(pSMB->fileName, fileName, name_len); |
| 5884 | } |
| 5885 | pSMB->attr = cpu_to_le16(dos_attrs); |
| 5886 | pSMB->BufferFormat = 0x04; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 5887 | inc_rfc1001_len(pSMB, name_len + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5888 | pSMB->ByteCount = cpu_to_le16(name_len + 1); |
| 5889 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 5890 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 5891 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5892 | cifs_dbg(FYI, "Error in LegacySetAttr = %d\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5893 | |
| 5894 | cifs_buf_release(pSMB); |
| 5895 | |
| 5896 | if (rc == -EAGAIN) |
| 5897 | goto SetAttrLgcyRetry; |
| 5898 | |
| 5899 | return rc; |
| 5900 | } |
| 5901 | #endif /* temporarily unneeded SetAttr legacy function */ |
| 5902 | |
Jeff Layton | 654cf14 | 2009-07-09 20:02:49 -0400 | [diff] [blame] | 5903 | static void |
| 5904 | cifs_fill_unix_set_info(FILE_UNIX_BASIC_INFO *data_offset, |
| 5905 | const struct cifs_unix_set_info_args *args) |
| 5906 | { |
Eric W. Biederman | 49418b2 | 2013-02-06 00:57:56 -0800 | [diff] [blame] | 5907 | u64 uid = NO_CHANGE_64, gid = NO_CHANGE_64; |
Jeff Layton | 654cf14 | 2009-07-09 20:02:49 -0400 | [diff] [blame] | 5908 | u64 mode = args->mode; |
| 5909 | |
Eric W. Biederman | 49418b2 | 2013-02-06 00:57:56 -0800 | [diff] [blame] | 5910 | if (uid_valid(args->uid)) |
| 5911 | uid = from_kuid(&init_user_ns, args->uid); |
| 5912 | if (gid_valid(args->gid)) |
| 5913 | gid = from_kgid(&init_user_ns, args->gid); |
| 5914 | |
Jeff Layton | 654cf14 | 2009-07-09 20:02:49 -0400 | [diff] [blame] | 5915 | /* |
| 5916 | * Samba server ignores set of file size to zero due to bugs in some |
| 5917 | * older clients, but we should be precise - we use SetFileSize to |
| 5918 | * set file size and do not want to truncate file size to zero |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 5919 | * accidentally as happened on one Samba server beta by putting |
Jeff Layton | 654cf14 | 2009-07-09 20:02:49 -0400 | [diff] [blame] | 5920 | * zero instead of -1 here |
| 5921 | */ |
| 5922 | data_offset->EndOfFile = cpu_to_le64(NO_CHANGE_64); |
| 5923 | data_offset->NumOfBytes = cpu_to_le64(NO_CHANGE_64); |
| 5924 | data_offset->LastStatusChange = cpu_to_le64(args->ctime); |
| 5925 | data_offset->LastAccessTime = cpu_to_le64(args->atime); |
| 5926 | data_offset->LastModificationTime = cpu_to_le64(args->mtime); |
Eric W. Biederman | 49418b2 | 2013-02-06 00:57:56 -0800 | [diff] [blame] | 5927 | data_offset->Uid = cpu_to_le64(uid); |
| 5928 | data_offset->Gid = cpu_to_le64(gid); |
Jeff Layton | 654cf14 | 2009-07-09 20:02:49 -0400 | [diff] [blame] | 5929 | /* better to leave device as zero when it is */ |
| 5930 | data_offset->DevMajor = cpu_to_le64(MAJOR(args->device)); |
| 5931 | data_offset->DevMinor = cpu_to_le64(MINOR(args->device)); |
| 5932 | data_offset->Permissions = cpu_to_le64(mode); |
| 5933 | |
| 5934 | if (S_ISREG(mode)) |
| 5935 | data_offset->Type = cpu_to_le32(UNIX_FILE); |
| 5936 | else if (S_ISDIR(mode)) |
| 5937 | data_offset->Type = cpu_to_le32(UNIX_DIR); |
| 5938 | else if (S_ISLNK(mode)) |
| 5939 | data_offset->Type = cpu_to_le32(UNIX_SYMLINK); |
| 5940 | else if (S_ISCHR(mode)) |
| 5941 | data_offset->Type = cpu_to_le32(UNIX_CHARDEV); |
| 5942 | else if (S_ISBLK(mode)) |
| 5943 | data_offset->Type = cpu_to_le32(UNIX_BLOCKDEV); |
| 5944 | else if (S_ISFIFO(mode)) |
| 5945 | data_offset->Type = cpu_to_le32(UNIX_FIFO); |
| 5946 | else if (S_ISSOCK(mode)) |
| 5947 | data_offset->Type = cpu_to_le32(UNIX_SOCKET); |
| 5948 | } |
| 5949 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5950 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 5951 | CIFSSMBUnixSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon, |
Jeff Layton | 3bbeeb3 | 2009-07-09 20:02:50 -0400 | [diff] [blame] | 5952 | const struct cifs_unix_set_info_args *args, |
| 5953 | u16 fid, u32 pid_of_opener) |
| 5954 | { |
| 5955 | struct smb_com_transaction2_sfi_req *pSMB = NULL; |
Jeff Layton | b2a3ad9 | 2012-03-26 09:55:29 -0400 | [diff] [blame] | 5956 | char *data_offset; |
Jeff Layton | 3bbeeb3 | 2009-07-09 20:02:50 -0400 | [diff] [blame] | 5957 | int rc = 0; |
| 5958 | u16 params, param_offset, offset, byte_count, count; |
| 5959 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5960 | cifs_dbg(FYI, "Set Unix Info (via SetFileInfo)\n"); |
Jeff Layton | 3bbeeb3 | 2009-07-09 20:02:50 -0400 | [diff] [blame] | 5961 | rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB); |
| 5962 | |
| 5963 | if (rc) |
| 5964 | return rc; |
| 5965 | |
| 5966 | pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener); |
| 5967 | pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16)); |
| 5968 | |
| 5969 | params = 6; |
| 5970 | pSMB->MaxSetupCount = 0; |
| 5971 | pSMB->Reserved = 0; |
| 5972 | pSMB->Flags = 0; |
| 5973 | pSMB->Timeout = 0; |
| 5974 | pSMB->Reserved2 = 0; |
| 5975 | param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4; |
| 5976 | offset = param_offset + params; |
| 5977 | |
Jeff Layton | b2a3ad9 | 2012-03-26 09:55:29 -0400 | [diff] [blame] | 5978 | data_offset = (char *)pSMB + |
| 5979 | offsetof(struct smb_hdr, Protocol) + offset; |
| 5980 | |
Jeff Layton | 3bbeeb3 | 2009-07-09 20:02:50 -0400 | [diff] [blame] | 5981 | count = sizeof(FILE_UNIX_BASIC_INFO); |
| 5982 | |
| 5983 | pSMB->MaxParameterCount = cpu_to_le16(2); |
| 5984 | /* BB find max SMB PDU from sess */ |
| 5985 | pSMB->MaxDataCount = cpu_to_le16(1000); |
| 5986 | pSMB->SetupCount = 1; |
| 5987 | pSMB->Reserved3 = 0; |
| 5988 | pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION); |
| 5989 | byte_count = 3 /* pad */ + params + count; |
| 5990 | pSMB->DataCount = cpu_to_le16(count); |
| 5991 | pSMB->ParameterCount = cpu_to_le16(params); |
| 5992 | pSMB->TotalDataCount = pSMB->DataCount; |
| 5993 | pSMB->TotalParameterCount = pSMB->ParameterCount; |
| 5994 | pSMB->ParameterOffset = cpu_to_le16(param_offset); |
| 5995 | pSMB->DataOffset = cpu_to_le16(offset); |
| 5996 | pSMB->Fid = fid; |
| 5997 | pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC); |
| 5998 | pSMB->Reserved4 = 0; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 5999 | inc_rfc1001_len(pSMB, byte_count); |
Jeff Layton | 3bbeeb3 | 2009-07-09 20:02:50 -0400 | [diff] [blame] | 6000 | pSMB->ByteCount = cpu_to_le16(byte_count); |
| 6001 | |
Jeff Layton | b2a3ad9 | 2012-03-26 09:55:29 -0400 | [diff] [blame] | 6002 | cifs_fill_unix_set_info((FILE_UNIX_BASIC_INFO *)data_offset, args); |
Jeff Layton | 3bbeeb3 | 2009-07-09 20:02:50 -0400 | [diff] [blame] | 6003 | |
Pavel Shilovsky | 792af7b | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 6004 | rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0); |
Jeff Layton | 3bbeeb3 | 2009-07-09 20:02:50 -0400 | [diff] [blame] | 6005 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 6006 | cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n", |
| 6007 | rc); |
Jeff Layton | 3bbeeb3 | 2009-07-09 20:02:50 -0400 | [diff] [blame] | 6008 | |
| 6009 | /* Note: On -EAGAIN error only caller can retry on handle based calls |
| 6010 | since file handle passed in no longer valid */ |
| 6011 | |
| 6012 | return rc; |
| 6013 | } |
| 6014 | |
| 6015 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 6016 | CIFSSMBUnixSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon, |
Pavel Shilovsky | ff691e9 | 2012-07-13 14:04:46 +0400 | [diff] [blame] | 6017 | const char *file_name, |
Jeff Layton | 01ea95e | 2009-07-09 20:02:49 -0400 | [diff] [blame] | 6018 | const struct cifs_unix_set_info_args *args, |
| 6019 | const struct nls_table *nls_codepage, int remap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6020 | { |
| 6021 | TRANSACTION2_SPI_REQ *pSMB = NULL; |
| 6022 | TRANSACTION2_SPI_RSP *pSMBr = NULL; |
| 6023 | int name_len; |
| 6024 | int rc = 0; |
| 6025 | int bytes_returned = 0; |
| 6026 | FILE_UNIX_BASIC_INFO *data_offset; |
| 6027 | __u16 params, param_offset, offset, count, byte_count; |
| 6028 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 6029 | cifs_dbg(FYI, "In SetUID/GID/Mode\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6030 | setPermsRetry: |
| 6031 | rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB, |
| 6032 | (void **) &pSMBr); |
| 6033 | if (rc) |
| 6034 | return rc; |
| 6035 | |
| 6036 | if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) { |
| 6037 | name_len = |
Pavel Shilovsky | ff691e9 | 2012-07-13 14:04:46 +0400 | [diff] [blame] | 6038 | cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name, |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 6039 | PATH_MAX, nls_codepage, remap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6040 | name_len++; /* trailing null */ |
| 6041 | name_len *= 2; |
Steve French | 3e87d80 | 2005-09-18 20:49:21 -0700 | [diff] [blame] | 6042 | } else { /* BB improve the check for buffer overruns BB */ |
Pavel Shilovsky | ff691e9 | 2012-07-13 14:04:46 +0400 | [diff] [blame] | 6043 | name_len = strnlen(file_name, PATH_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6044 | name_len++; /* trailing null */ |
Pavel Shilovsky | ff691e9 | 2012-07-13 14:04:46 +0400 | [diff] [blame] | 6045 | strncpy(pSMB->FileName, file_name, name_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6046 | } |
| 6047 | |
| 6048 | params = 6 + name_len; |
Steve French | 26f5736 | 2007-08-30 22:09:15 +0000 | [diff] [blame] | 6049 | count = sizeof(FILE_UNIX_BASIC_INFO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6050 | pSMB->MaxParameterCount = cpu_to_le16(2); |
Steve French | 582d21e | 2008-05-13 04:54:12 +0000 | [diff] [blame] | 6051 | /* BB find max SMB PDU from sess structure BB */ |
| 6052 | pSMB->MaxDataCount = cpu_to_le16(1000); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6053 | pSMB->MaxSetupCount = 0; |
| 6054 | pSMB->Reserved = 0; |
| 6055 | pSMB->Flags = 0; |
| 6056 | pSMB->Timeout = 0; |
| 6057 | pSMB->Reserved2 = 0; |
| 6058 | param_offset = offsetof(struct smb_com_transaction2_spi_req, |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 6059 | InformationLevel) - 4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6060 | offset = param_offset + params; |
| 6061 | data_offset = |
| 6062 | (FILE_UNIX_BASIC_INFO *) ((char *) &pSMB->hdr.Protocol + |
| 6063 | offset); |
| 6064 | memset(data_offset, 0, count); |
| 6065 | pSMB->DataOffset = cpu_to_le16(offset); |
| 6066 | pSMB->ParameterOffset = cpu_to_le16(param_offset); |
| 6067 | pSMB->SetupCount = 1; |
| 6068 | pSMB->Reserved3 = 0; |
| 6069 | pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION); |
| 6070 | byte_count = 3 /* pad */ + params + count; |
| 6071 | pSMB->ParameterCount = cpu_to_le16(params); |
| 6072 | pSMB->DataCount = cpu_to_le16(count); |
| 6073 | pSMB->TotalParameterCount = pSMB->ParameterCount; |
| 6074 | pSMB->TotalDataCount = pSMB->DataCount; |
| 6075 | pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC); |
| 6076 | pSMB->Reserved4 = 0; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 6077 | inc_rfc1001_len(pSMB, byte_count); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 6078 | |
Jeff Layton | 654cf14 | 2009-07-09 20:02:49 -0400 | [diff] [blame] | 6079 | cifs_fill_unix_set_info(data_offset, args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6080 | |
| 6081 | pSMB->ByteCount = cpu_to_le16(byte_count); |
| 6082 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 6083 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 6084 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 6085 | cifs_dbg(FYI, "SetPathInfo (perms) returned %d\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6086 | |
Steve French | 0d817bc | 2008-05-22 02:02:03 +0000 | [diff] [blame] | 6087 | cifs_buf_release(pSMB); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6088 | if (rc == -EAGAIN) |
| 6089 | goto setPermsRetry; |
| 6090 | return rc; |
| 6091 | } |
| 6092 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6093 | #ifdef CONFIG_CIFS_XATTR |
Jeff Layton | 31c0519 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6094 | /* |
| 6095 | * Do a path-based QUERY_ALL_EAS call and parse the result. This is a common |
| 6096 | * function used by listxattr and getxattr type calls. When ea_name is set, |
| 6097 | * it looks for that attribute name and stuffs that value into the EAData |
| 6098 | * buffer. When ea_name is NULL, it stuffs a list of attribute names into the |
| 6099 | * buffer. In both cases, the return value is either the length of the |
| 6100 | * resulting data or a negative error code. If EAData is a NULL pointer then |
| 6101 | * the data isn't copied to it, but the length is returned. |
| 6102 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6103 | ssize_t |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 6104 | CIFSSMBQAllEAs(const unsigned int xid, struct cifs_tcon *tcon, |
Jeff Layton | 31c0519 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6105 | const unsigned char *searchName, const unsigned char *ea_name, |
| 6106 | char *EAData, size_t buf_size, |
| 6107 | const struct nls_table *nls_codepage, int remap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6108 | { |
| 6109 | /* BB assumes one setup word */ |
| 6110 | TRANSACTION2_QPI_REQ *pSMB = NULL; |
| 6111 | TRANSACTION2_QPI_RSP *pSMBr = NULL; |
| 6112 | int rc = 0; |
| 6113 | int bytes_returned; |
Jeff Layton | 6e462b9 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6114 | int list_len; |
Jeff Layton | f0d3868 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6115 | struct fealist *ea_response_data; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 6116 | struct fea *temp_fea; |
| 6117 | char *temp_ptr; |
Jeff Layton | 0cd126b | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6118 | char *end_of_smb; |
Jeff Layton | f0d3868 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6119 | __u16 params, byte_count, data_offset; |
Jeff Layton | 5980fc9 | 2011-07-28 12:48:26 -0400 | [diff] [blame] | 6120 | unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6121 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 6122 | cifs_dbg(FYI, "In Query All EAs path %s\n", searchName); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6123 | QAllEAsRetry: |
| 6124 | rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB, |
| 6125 | (void **) &pSMBr); |
| 6126 | if (rc) |
| 6127 | return rc; |
| 6128 | |
| 6129 | if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) { |
Jeff Layton | 6e462b9 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6130 | list_len = |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 6131 | cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName, |
| 6132 | PATH_MAX, nls_codepage, remap); |
Jeff Layton | 6e462b9 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6133 | list_len++; /* trailing null */ |
| 6134 | list_len *= 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6135 | } else { /* BB improve the check for buffer overruns BB */ |
Jeff Layton | 6e462b9 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6136 | list_len = strnlen(searchName, PATH_MAX); |
| 6137 | list_len++; /* trailing null */ |
| 6138 | strncpy(pSMB->FileName, searchName, list_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6139 | } |
| 6140 | |
Jeff Layton | 6e462b9 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6141 | params = 2 /* level */ + 4 /* reserved */ + list_len /* includes NUL */; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6142 | pSMB->TotalDataCount = 0; |
| 6143 | pSMB->MaxParameterCount = cpu_to_le16(2); |
Steve French | 582d21e | 2008-05-13 04:54:12 +0000 | [diff] [blame] | 6144 | /* BB find exact max SMB PDU from sess structure BB */ |
Jeff Layton | e529614 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6145 | pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6146 | pSMB->MaxSetupCount = 0; |
| 6147 | pSMB->Reserved = 0; |
| 6148 | pSMB->Flags = 0; |
| 6149 | pSMB->Timeout = 0; |
| 6150 | pSMB->Reserved2 = 0; |
| 6151 | pSMB->ParameterOffset = cpu_to_le16(offsetof( |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 6152 | struct smb_com_transaction2_qpi_req, InformationLevel) - 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6153 | pSMB->DataCount = 0; |
| 6154 | pSMB->DataOffset = 0; |
| 6155 | pSMB->SetupCount = 1; |
| 6156 | pSMB->Reserved3 = 0; |
| 6157 | pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION); |
| 6158 | byte_count = params + 1 /* pad */ ; |
| 6159 | pSMB->TotalParameterCount = cpu_to_le16(params); |
| 6160 | pSMB->ParameterCount = pSMB->TotalParameterCount; |
| 6161 | pSMB->InformationLevel = cpu_to_le16(SMB_INFO_QUERY_ALL_EAS); |
| 6162 | pSMB->Reserved4 = 0; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 6163 | inc_rfc1001_len(pSMB, byte_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6164 | pSMB->ByteCount = cpu_to_le16(byte_count); |
| 6165 | |
| 6166 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 6167 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
| 6168 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 6169 | cifs_dbg(FYI, "Send error in QueryAllEAs = %d\n", rc); |
Jeff Layton | f0d3868 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6170 | goto QAllEAsOut; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6171 | } |
Jeff Layton | f0d3868 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6172 | |
| 6173 | |
| 6174 | /* BB also check enough total bytes returned */ |
| 6175 | /* BB we need to improve the validity checking |
| 6176 | of these trans2 responses */ |
| 6177 | |
| 6178 | rc = validate_t2((struct smb_t2_rsp *)pSMBr); |
Jeff Layton | 820a803 | 2011-05-04 08:05:26 -0400 | [diff] [blame] | 6179 | if (rc || get_bcc(&pSMBr->hdr) < 4) { |
Jeff Layton | f0d3868 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6180 | rc = -EIO; /* bad smb */ |
| 6181 | goto QAllEAsOut; |
| 6182 | } |
| 6183 | |
| 6184 | /* check that length of list is not more than bcc */ |
| 6185 | /* check that each entry does not go beyond length |
| 6186 | of list */ |
| 6187 | /* check that each element of each entry does not |
| 6188 | go beyond end of list */ |
| 6189 | /* validate_trans2_offsets() */ |
| 6190 | /* BB check if start of smb + data_offset > &bcc+ bcc */ |
| 6191 | |
| 6192 | data_offset = le16_to_cpu(pSMBr->t2.DataOffset); |
| 6193 | ea_response_data = (struct fealist *) |
| 6194 | (((char *) &pSMBr->hdr.Protocol) + data_offset); |
| 6195 | |
Jeff Layton | 6e462b9 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6196 | list_len = le32_to_cpu(ea_response_data->list_len); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 6197 | cifs_dbg(FYI, "ea length %d\n", list_len); |
Jeff Layton | 6e462b9 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6198 | if (list_len <= 8) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 6199 | cifs_dbg(FYI, "empty EA list returned from server\n"); |
Jeff Layton | f0d3868 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6200 | goto QAllEAsOut; |
| 6201 | } |
| 6202 | |
Jeff Layton | 0cd126b | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6203 | /* make sure list_len doesn't go past end of SMB */ |
Jeff Layton | 690c522 | 2011-01-20 13:36:51 -0500 | [diff] [blame] | 6204 | end_of_smb = (char *)pByteArea(&pSMBr->hdr) + get_bcc(&pSMBr->hdr); |
Jeff Layton | 0cd126b | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6205 | if ((char *)ea_response_data + list_len > end_of_smb) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 6206 | cifs_dbg(FYI, "EA list appears to go beyond SMB\n"); |
Jeff Layton | 0cd126b | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6207 | rc = -EIO; |
| 6208 | goto QAllEAsOut; |
| 6209 | } |
| 6210 | |
Jeff Layton | f0d3868 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6211 | /* account for ea list len */ |
Jeff Layton | 6e462b9 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6212 | list_len -= 4; |
Jeff Layton | f0d3868 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6213 | temp_fea = ea_response_data->list; |
| 6214 | temp_ptr = (char *)temp_fea; |
Jeff Layton | 6e462b9 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6215 | while (list_len > 0) { |
Steve French | 122ca00 | 2010-02-24 21:56:48 +0000 | [diff] [blame] | 6216 | unsigned int name_len; |
Jeff Layton | f0d3868 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6217 | __u16 value_len; |
Jeff Layton | 0cd126b | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6218 | |
Jeff Layton | 6e462b9 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6219 | list_len -= 4; |
Jeff Layton | f0d3868 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6220 | temp_ptr += 4; |
Jeff Layton | 0cd126b | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6221 | /* make sure we can read name_len and value_len */ |
| 6222 | if (list_len < 0) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 6223 | cifs_dbg(FYI, "EA entry goes beyond length of list\n"); |
Jeff Layton | 0cd126b | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6224 | rc = -EIO; |
| 6225 | goto QAllEAsOut; |
| 6226 | } |
| 6227 | |
| 6228 | name_len = temp_fea->name_len; |
| 6229 | value_len = le16_to_cpu(temp_fea->value_len); |
| 6230 | list_len -= name_len + 1 + value_len; |
| 6231 | if (list_len < 0) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 6232 | cifs_dbg(FYI, "EA entry goes beyond length of list\n"); |
Jeff Layton | 0cd126b | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6233 | rc = -EIO; |
| 6234 | goto QAllEAsOut; |
| 6235 | } |
| 6236 | |
Jeff Layton | 31c0519 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6237 | if (ea_name) { |
Jeff Layton | 91d065c | 2011-07-26 18:23:47 -0400 | [diff] [blame] | 6238 | if (ea_name_len == name_len && |
Jeff Layton | ac42344 | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 6239 | memcmp(ea_name, temp_ptr, name_len) == 0) { |
Jeff Layton | 31c0519 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6240 | temp_ptr += name_len + 1; |
| 6241 | rc = value_len; |
| 6242 | if (buf_size == 0) |
| 6243 | goto QAllEAsOut; |
| 6244 | if ((size_t)value_len > buf_size) { |
| 6245 | rc = -ERANGE; |
| 6246 | goto QAllEAsOut; |
| 6247 | } |
| 6248 | memcpy(EAData, temp_ptr, value_len); |
| 6249 | goto QAllEAsOut; |
| 6250 | } |
Jeff Layton | f0d3868 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6251 | } else { |
Jeff Layton | 31c0519 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6252 | /* account for prefix user. and trailing null */ |
| 6253 | rc += (5 + 1 + name_len); |
| 6254 | if (rc < (int) buf_size) { |
| 6255 | memcpy(EAData, "user.", 5); |
| 6256 | EAData += 5; |
| 6257 | memcpy(EAData, temp_ptr, name_len); |
| 6258 | EAData += name_len; |
| 6259 | /* null terminate name */ |
| 6260 | *EAData = 0; |
| 6261 | ++EAData; |
| 6262 | } else if (buf_size == 0) { |
| 6263 | /* skip copy - calc size only */ |
| 6264 | } else { |
| 6265 | /* stop before overrun buffer */ |
| 6266 | rc = -ERANGE; |
| 6267 | break; |
| 6268 | } |
Jeff Layton | f0d3868 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6269 | } |
Jeff Layton | 0cd126b | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6270 | temp_ptr += name_len + 1 + value_len; |
Jeff Layton | f0d3868 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6271 | temp_fea = (struct fea *)temp_ptr; |
| 6272 | } |
| 6273 | |
Jeff Layton | 31c0519 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6274 | /* didn't find the named attribute */ |
| 6275 | if (ea_name) |
| 6276 | rc = -ENODATA; |
| 6277 | |
Jeff Layton | f0d3868 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6278 | QAllEAsOut: |
Steve French | 0d817bc | 2008-05-22 02:02:03 +0000 | [diff] [blame] | 6279 | cifs_buf_release(pSMB); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6280 | if (rc == -EAGAIN) |
| 6281 | goto QAllEAsRetry; |
| 6282 | |
| 6283 | return (ssize_t)rc; |
| 6284 | } |
| 6285 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6286 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 6287 | CIFSSMBSetEA(const unsigned int xid, struct cifs_tcon *tcon, |
| 6288 | const char *fileName, const char *ea_name, const void *ea_value, |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 6289 | const __u16 ea_value_len, const struct nls_table *nls_codepage, |
| 6290 | int remap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6291 | { |
| 6292 | struct smb_com_transaction2_spi_req *pSMB = NULL; |
| 6293 | struct smb_com_transaction2_spi_rsp *pSMBr = NULL; |
| 6294 | struct fealist *parm_data; |
| 6295 | int name_len; |
| 6296 | int rc = 0; |
| 6297 | int bytes_returned = 0; |
| 6298 | __u16 params, param_offset, byte_count, offset, count; |
| 6299 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 6300 | cifs_dbg(FYI, "In SetEA\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6301 | SetEARetry: |
| 6302 | rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB, |
| 6303 | (void **) &pSMBr); |
| 6304 | if (rc) |
| 6305 | return rc; |
| 6306 | |
| 6307 | if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) { |
| 6308 | name_len = |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 6309 | cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName, |
| 6310 | PATH_MAX, nls_codepage, remap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6311 | name_len++; /* trailing null */ |
| 6312 | name_len *= 2; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 6313 | } else { /* BB improve the check for buffer overruns BB */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6314 | name_len = strnlen(fileName, PATH_MAX); |
| 6315 | name_len++; /* trailing null */ |
| 6316 | strncpy(pSMB->FileName, fileName, name_len); |
| 6317 | } |
| 6318 | |
| 6319 | params = 6 + name_len; |
| 6320 | |
| 6321 | /* done calculating parms using name_len of file name, |
| 6322 | now use name_len to calculate length of ea name |
| 6323 | we are going to create in the inode xattrs */ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 6324 | if (ea_name == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6325 | name_len = 0; |
| 6326 | else |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 6327 | name_len = strnlen(ea_name, 255); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6328 | |
Steve French | dae5dbdb | 2007-12-30 23:49:57 +0000 | [diff] [blame] | 6329 | count = sizeof(*parm_data) + ea_value_len + name_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6330 | pSMB->MaxParameterCount = cpu_to_le16(2); |
Steve French | 582d21e | 2008-05-13 04:54:12 +0000 | [diff] [blame] | 6331 | /* BB find max SMB PDU from sess */ |
| 6332 | pSMB->MaxDataCount = cpu_to_le16(1000); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6333 | pSMB->MaxSetupCount = 0; |
| 6334 | pSMB->Reserved = 0; |
| 6335 | pSMB->Flags = 0; |
| 6336 | pSMB->Timeout = 0; |
| 6337 | pSMB->Reserved2 = 0; |
| 6338 | param_offset = offsetof(struct smb_com_transaction2_spi_req, |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 6339 | InformationLevel) - 4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6340 | offset = param_offset + params; |
| 6341 | pSMB->InformationLevel = |
| 6342 | cpu_to_le16(SMB_SET_FILE_EA); |
| 6343 | |
| 6344 | parm_data = |
| 6345 | (struct fealist *) (((char *) &pSMB->hdr.Protocol) + |
| 6346 | offset); |
| 6347 | pSMB->ParameterOffset = cpu_to_le16(param_offset); |
| 6348 | pSMB->DataOffset = cpu_to_le16(offset); |
| 6349 | pSMB->SetupCount = 1; |
| 6350 | pSMB->Reserved3 = 0; |
| 6351 | pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION); |
| 6352 | byte_count = 3 /* pad */ + params + count; |
| 6353 | pSMB->DataCount = cpu_to_le16(count); |
| 6354 | parm_data->list_len = cpu_to_le32(count); |
| 6355 | parm_data->list[0].EA_flags = 0; |
| 6356 | /* we checked above that name len is less than 255 */ |
Alexey Dobriyan | 53b3531 | 2006-03-24 03:16:13 -0800 | [diff] [blame] | 6357 | parm_data->list[0].name_len = (__u8)name_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6358 | /* EA names are always ASCII */ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 6359 | if (ea_name) |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 6360 | strncpy(parm_data->list[0].name, ea_name, name_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6361 | parm_data->list[0].name[name_len] = 0; |
| 6362 | parm_data->list[0].value_len = cpu_to_le16(ea_value_len); |
| 6363 | /* caller ensures that ea_value_len is less than 64K but |
| 6364 | we need to ensure that it fits within the smb */ |
| 6365 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 6366 | /*BB add length check to see if it would fit in |
| 6367 | negotiated SMB buffer size BB */ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 6368 | /* if (ea_value_len > buffer_size - 512 (enough for header)) */ |
| 6369 | if (ea_value_len) |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 6370 | memcpy(parm_data->list[0].name+name_len+1, |
| 6371 | ea_value, ea_value_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6372 | |
| 6373 | pSMB->TotalDataCount = pSMB->DataCount; |
| 6374 | pSMB->ParameterCount = cpu_to_le16(params); |
| 6375 | pSMB->TotalParameterCount = pSMB->ParameterCount; |
| 6376 | pSMB->Reserved4 = 0; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 6377 | inc_rfc1001_len(pSMB, byte_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6378 | pSMB->ByteCount = cpu_to_le16(byte_count); |
| 6379 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 6380 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 6381 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 6382 | cifs_dbg(FYI, "SetPathInfo (EA) returned %d\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6383 | |
| 6384 | cifs_buf_release(pSMB); |
| 6385 | |
| 6386 | if (rc == -EAGAIN) |
| 6387 | goto SetEARetry; |
| 6388 | |
| 6389 | return rc; |
| 6390 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6391 | #endif |
Steve French | 0eff0e2 | 2011-02-24 05:39:23 +0000 | [diff] [blame] | 6392 | |
| 6393 | #ifdef CONFIG_CIFS_DNOTIFY_EXPERIMENTAL /* BB unused temporarily */ |
| 6394 | /* |
| 6395 | * Years ago the kernel added a "dnotify" function for Samba server, |
| 6396 | * to allow network clients (such as Windows) to display updated |
| 6397 | * lists of files in directory listings automatically when |
| 6398 | * files are added by one user when another user has the |
| 6399 | * same directory open on their desktop. The Linux cifs kernel |
| 6400 | * client hooked into the kernel side of this interface for |
| 6401 | * the same reason, but ironically when the VFS moved from |
| 6402 | * "dnotify" to "inotify" it became harder to plug in Linux |
| 6403 | * network file system clients (the most obvious use case |
| 6404 | * for notify interfaces is when multiple users can update |
| 6405 | * the contents of the same directory - exactly what network |
| 6406 | * file systems can do) although the server (Samba) could |
| 6407 | * still use it. For the short term we leave the worker |
| 6408 | * function ifdeffed out (below) until inotify is fixed |
| 6409 | * in the VFS to make it easier to plug in network file |
| 6410 | * system clients. If inotify turns out to be permanently |
| 6411 | * incompatible for network fs clients, we could instead simply |
| 6412 | * expose this config flag by adding a future cifs (and smb2) notify ioctl. |
| 6413 | */ |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 6414 | int CIFSSMBNotify(const unsigned int xid, struct cifs_tcon *tcon, |
Steve French | 0eff0e2 | 2011-02-24 05:39:23 +0000 | [diff] [blame] | 6415 | const int notify_subdirs, const __u16 netfid, |
| 6416 | __u32 filter, struct file *pfile, int multishot, |
| 6417 | const struct nls_table *nls_codepage) |
| 6418 | { |
| 6419 | int rc = 0; |
| 6420 | struct smb_com_transaction_change_notify_req *pSMB = NULL; |
| 6421 | struct smb_com_ntransaction_change_notify_rsp *pSMBr = NULL; |
| 6422 | struct dir_notify_req *dnotify_req; |
| 6423 | int bytes_returned; |
| 6424 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 6425 | cifs_dbg(FYI, "In CIFSSMBNotify for file handle %d\n", (int)netfid); |
Steve French | 0eff0e2 | 2011-02-24 05:39:23 +0000 | [diff] [blame] | 6426 | rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB, |
| 6427 | (void **) &pSMBr); |
| 6428 | if (rc) |
| 6429 | return rc; |
| 6430 | |
| 6431 | pSMB->TotalParameterCount = 0 ; |
| 6432 | pSMB->TotalDataCount = 0; |
| 6433 | pSMB->MaxParameterCount = cpu_to_le32(2); |
Jeff Layton | c974bef | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 6434 | pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00); |
Steve French | 0eff0e2 | 2011-02-24 05:39:23 +0000 | [diff] [blame] | 6435 | pSMB->MaxSetupCount = 4; |
| 6436 | pSMB->Reserved = 0; |
| 6437 | pSMB->ParameterOffset = 0; |
| 6438 | pSMB->DataCount = 0; |
| 6439 | pSMB->DataOffset = 0; |
| 6440 | pSMB->SetupCount = 4; /* single byte does not need le conversion */ |
| 6441 | pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_NOTIFY_CHANGE); |
| 6442 | pSMB->ParameterCount = pSMB->TotalParameterCount; |
| 6443 | if (notify_subdirs) |
| 6444 | pSMB->WatchTree = 1; /* one byte - no le conversion needed */ |
| 6445 | pSMB->Reserved2 = 0; |
| 6446 | pSMB->CompletionFilter = cpu_to_le32(filter); |
| 6447 | pSMB->Fid = netfid; /* file handle always le */ |
| 6448 | pSMB->ByteCount = 0; |
| 6449 | |
| 6450 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 6451 | (struct smb_hdr *)pSMBr, &bytes_returned, |
| 6452 | CIFS_ASYNC_OP); |
| 6453 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 6454 | cifs_dbg(FYI, "Error in Notify = %d\n", rc); |
Steve French | 0eff0e2 | 2011-02-24 05:39:23 +0000 | [diff] [blame] | 6455 | } else { |
| 6456 | /* Add file to outstanding requests */ |
| 6457 | /* BB change to kmem cache alloc */ |
| 6458 | dnotify_req = kmalloc( |
| 6459 | sizeof(struct dir_notify_req), |
| 6460 | GFP_KERNEL); |
| 6461 | if (dnotify_req) { |
| 6462 | dnotify_req->Pid = pSMB->hdr.Pid; |
| 6463 | dnotify_req->PidHigh = pSMB->hdr.PidHigh; |
| 6464 | dnotify_req->Mid = pSMB->hdr.Mid; |
| 6465 | dnotify_req->Tid = pSMB->hdr.Tid; |
| 6466 | dnotify_req->Uid = pSMB->hdr.Uid; |
| 6467 | dnotify_req->netfid = netfid; |
| 6468 | dnotify_req->pfile = pfile; |
| 6469 | dnotify_req->filter = filter; |
| 6470 | dnotify_req->multishot = multishot; |
| 6471 | spin_lock(&GlobalMid_Lock); |
| 6472 | list_add_tail(&dnotify_req->lhead, |
| 6473 | &GlobalDnotifyReqList); |
| 6474 | spin_unlock(&GlobalMid_Lock); |
| 6475 | } else |
| 6476 | rc = -ENOMEM; |
| 6477 | } |
| 6478 | cifs_buf_release(pSMB); |
| 6479 | return rc; |
| 6480 | } |
| 6481 | #endif /* was needed for dnotify, and will be needed for inotify when VFS fix */ |