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 | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 38 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 */ |
Steve French | 3afca26 | 2016-09-22 18:58:16 -0500 | [diff] [blame] | 101 | spin_lock(&tcon->open_file_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 | } |
Steve French | 3afca26 | 2016-09-22 18:58:16 -0500 | [diff] [blame] | 107 | spin_unlock(&tcon->open_file_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 | |
Jeff Layton | 9162ab2 | 2009-09-03 12:07:17 -0400 | [diff] [blame] | 199 | atomic_inc(&tconInfoReconnectCount); |
| 200 | |
| 201 | /* tell server Unix caps we support */ |
| 202 | if (ses->capabilities & CAP_UNIX) |
| 203 | reset_cifs_unix_caps(0, tcon, NULL, NULL); |
| 204 | |
| 205 | /* |
| 206 | * Removed call to reopen open files here. It is safer (and faster) to |
| 207 | * reopen files one at a time as needed in read and write. |
| 208 | * |
| 209 | * FIXME: what about file locks? don't we need to reclaim them ASAP? |
| 210 | */ |
| 211 | |
| 212 | out: |
| 213 | /* |
| 214 | * Check if handle based operation so we know whether we can continue |
| 215 | * or not without returning to caller to reset file handle |
| 216 | */ |
| 217 | switch (smb_command) { |
| 218 | case SMB_COM_READ_ANDX: |
| 219 | case SMB_COM_WRITE_ANDX: |
| 220 | case SMB_COM_CLOSE: |
| 221 | case SMB_COM_FIND_CLOSE2: |
| 222 | case SMB_COM_LOCKING_ANDX: |
| 223 | rc = -EAGAIN; |
| 224 | } |
| 225 | |
| 226 | unload_nls(nls_codepage); |
| 227 | return rc; |
| 228 | } |
| 229 | |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 230 | /* Allocate and return pointer to an SMB request buffer, and set basic |
| 231 | SMB information in the SMB header. If the return code is zero, this |
| 232 | function must have filled in request_buf pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | static int |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 234 | small_smb_init(int smb_command, int wct, struct cifs_tcon *tcon, |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 235 | void **request_buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | { |
Jeff Layton | f569599 | 2010-09-29 15:27:08 -0400 | [diff] [blame] | 237 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | |
Jeff Layton | 9162ab2 | 2009-09-03 12:07:17 -0400 | [diff] [blame] | 239 | rc = cifs_reconnect_tcon(tcon, smb_command); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 240 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | return rc; |
| 242 | |
| 243 | *request_buf = cifs_small_buf_get(); |
| 244 | if (*request_buf == NULL) { |
| 245 | /* BB should we add a retry in here if not a writepage? */ |
| 246 | return -ENOMEM; |
| 247 | } |
| 248 | |
Steve French | 63135e0 | 2007-07-17 17:34:02 +0000 | [diff] [blame] | 249 | header_assemble((struct smb_hdr *) *request_buf, smb_command, |
Steve French | c18c842 | 2007-07-18 23:21:09 +0000 | [diff] [blame] | 250 | tcon, wct); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 252 | if (tcon != NULL) |
| 253 | cifs_stats_inc(&tcon->num_smbs_sent); |
Steve French | a454434 | 2005-08-24 13:59:35 -0700 | [diff] [blame] | 254 | |
Jeff Layton | f569599 | 2010-09-29 15:27:08 -0400 | [diff] [blame] | 255 | return 0; |
Steve French | 5815449d | 2006-02-14 01:36:20 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Steve French | 12b3b8f | 2006-02-09 21:12:47 +0000 | [diff] [blame] | 258 | int |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 259 | small_smb_init_no_tc(const int smb_command, const int wct, |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 260 | struct cifs_ses *ses, void **request_buf) |
Steve French | 12b3b8f | 2006-02-09 21:12:47 +0000 | [diff] [blame] | 261 | { |
| 262 | int rc; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 263 | struct smb_hdr *buffer; |
Steve French | 12b3b8f | 2006-02-09 21:12:47 +0000 | [diff] [blame] | 264 | |
Steve French | 5815449d | 2006-02-14 01:36:20 +0000 | [diff] [blame] | 265 | rc = small_smb_init(smb_command, wct, NULL, request_buf); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 266 | if (rc) |
Steve French | 12b3b8f | 2006-02-09 21:12:47 +0000 | [diff] [blame] | 267 | return rc; |
| 268 | |
Steve French | 04fdabe | 2006-02-10 05:52:50 +0000 | [diff] [blame] | 269 | buffer = (struct smb_hdr *)*request_buf; |
Pavel Shilovsky | 8825736 | 2012-05-23 14:01:59 +0400 | [diff] [blame] | 270 | buffer->Mid = get_next_mid(ses->server); |
Steve French | 12b3b8f | 2006-02-09 21:12:47 +0000 | [diff] [blame] | 271 | if (ses->capabilities & CAP_UNICODE) |
| 272 | buffer->Flags2 |= SMBFLG2_UNICODE; |
Steve French | 04fdabe | 2006-02-10 05:52:50 +0000 | [diff] [blame] | 273 | if (ses->capabilities & CAP_STATUS32) |
Steve French | 12b3b8f | 2006-02-09 21:12:47 +0000 | [diff] [blame] | 274 | buffer->Flags2 |= SMBFLG2_ERR_STATUS; |
| 275 | |
| 276 | /* uid, tid can stay at zero as set in header assemble */ |
| 277 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 278 | /* BB add support for turning on the signing when |
Steve French | 12b3b8f | 2006-02-09 21:12:47 +0000 | [diff] [blame] | 279 | this function is used after 1st of session setup requests */ |
| 280 | |
| 281 | return rc; |
| 282 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | |
| 284 | /* If the return code is zero, this function must fill in request_buf pointer */ |
| 285 | static int |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 286 | __smb_init(int smb_command, int wct, struct cifs_tcon *tcon, |
Jeff Layton | f569599 | 2010-09-29 15:27:08 -0400 | [diff] [blame] | 287 | void **request_buf, void **response_buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | *request_buf = cifs_buf_get(); |
| 290 | if (*request_buf == NULL) { |
| 291 | /* BB should we add a retry in here if not a writepage? */ |
| 292 | return -ENOMEM; |
| 293 | } |
| 294 | /* Although the original thought was we needed the response buf for */ |
| 295 | /* potential retries of smb operations it turns out we can determine */ |
| 296 | /* from the mid flags when the request buffer can be resent without */ |
| 297 | /* having to use a second distinct buffer for the response */ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 298 | if (response_buf) |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 299 | *response_buf = *request_buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | |
| 301 | header_assemble((struct smb_hdr *) *request_buf, smb_command, tcon, |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 302 | wct); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 304 | if (tcon != NULL) |
| 305 | cifs_stats_inc(&tcon->num_smbs_sent); |
Steve French | a454434 | 2005-08-24 13:59:35 -0700 | [diff] [blame] | 306 | |
Jeff Layton | f569599 | 2010-09-29 15:27:08 -0400 | [diff] [blame] | 307 | return 0; |
| 308 | } |
| 309 | |
| 310 | /* If the return code is zero, this function must fill in request_buf pointer */ |
| 311 | static int |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 312 | smb_init(int smb_command, int wct, struct cifs_tcon *tcon, |
Jeff Layton | f569599 | 2010-09-29 15:27:08 -0400 | [diff] [blame] | 313 | void **request_buf, void **response_buf) |
| 314 | { |
| 315 | int rc; |
| 316 | |
| 317 | rc = cifs_reconnect_tcon(tcon, smb_command); |
| 318 | if (rc) |
| 319 | return rc; |
| 320 | |
| 321 | return __smb_init(smb_command, wct, tcon, request_buf, response_buf); |
| 322 | } |
| 323 | |
| 324 | static int |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 325 | 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] | 326 | void **request_buf, void **response_buf) |
| 327 | { |
| 328 | if (tcon->ses->need_reconnect || tcon->need_reconnect) |
| 329 | return -EHOSTDOWN; |
| 330 | |
| 331 | return __smb_init(smb_command, wct, tcon, request_buf, response_buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 334 | static int validate_t2(struct smb_t2_rsp *pSMB) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | { |
Jeff Layton | 12df83c | 2011-01-20 13:36:51 -0500 | [diff] [blame] | 336 | unsigned int total_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | |
Jeff Layton | 12df83c | 2011-01-20 13:36:51 -0500 | [diff] [blame] | 338 | /* check for plausible wct */ |
| 339 | if (pSMB->hdr.WordCount < 10) |
| 340 | goto vt2_err; |
| 341 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | /* check for parm and data offset going beyond end of smb */ |
Jeff Layton | 12df83c | 2011-01-20 13:36:51 -0500 | [diff] [blame] | 343 | if (get_unaligned_le16(&pSMB->t2_rsp.ParameterOffset) > 1024 || |
| 344 | get_unaligned_le16(&pSMB->t2_rsp.DataOffset) > 1024) |
| 345 | goto vt2_err; |
| 346 | |
Jeff Layton | 12df83c | 2011-01-20 13:36:51 -0500 | [diff] [blame] | 347 | total_size = get_unaligned_le16(&pSMB->t2_rsp.ParameterCount); |
| 348 | if (total_size >= 512) |
| 349 | goto vt2_err; |
| 350 | |
Jeff Layton | fd5707e | 2011-03-31 17:22:07 -0400 | [diff] [blame] | 351 | /* check that bcc is at least as big as parms + data, and that it is |
| 352 | * less than negotiated smb buffer |
| 353 | */ |
Jeff Layton | 12df83c | 2011-01-20 13:36:51 -0500 | [diff] [blame] | 354 | total_size += get_unaligned_le16(&pSMB->t2_rsp.DataCount); |
| 355 | if (total_size > get_bcc(&pSMB->hdr) || |
| 356 | total_size >= CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) |
| 357 | goto vt2_err; |
| 358 | |
| 359 | return 0; |
| 360 | vt2_err: |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 361 | cifs_dump_mem("Invalid transact2 SMB: ", (char *)pSMB, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | sizeof(struct smb_t2_rsp) + 16); |
Jeff Layton | 12df83c | 2011-01-20 13:36:51 -0500 | [diff] [blame] | 363 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | } |
Jeff Layton | 690c522 | 2011-01-20 13:36:51 -0500 | [diff] [blame] | 365 | |
Jeff Layton | 31d9e2b | 2013-05-26 07:00:57 -0400 | [diff] [blame] | 366 | static int |
Jeff Layton | 3f61822 | 2013-06-12 19:52:14 -0500 | [diff] [blame] | 367 | decode_ext_sec_blob(struct cifs_ses *ses, NEGOTIATE_RSP *pSMBr) |
Jeff Layton | 31d9e2b | 2013-05-26 07:00:57 -0400 | [diff] [blame] | 368 | { |
| 369 | int rc = 0; |
| 370 | u16 count; |
| 371 | char *guid = pSMBr->u.extended_response.GUID; |
Jeff Layton | 3f61822 | 2013-06-12 19:52:14 -0500 | [diff] [blame] | 372 | struct TCP_Server_Info *server = ses->server; |
Jeff Layton | 31d9e2b | 2013-05-26 07:00:57 -0400 | [diff] [blame] | 373 | |
| 374 | count = get_bcc(&pSMBr->hdr); |
| 375 | if (count < SMB1_CLIENT_GUID_SIZE) |
| 376 | return -EIO; |
| 377 | |
| 378 | spin_lock(&cifs_tcp_ses_lock); |
| 379 | if (server->srv_count > 1) { |
| 380 | spin_unlock(&cifs_tcp_ses_lock); |
| 381 | if (memcmp(server->server_GUID, guid, SMB1_CLIENT_GUID_SIZE) != 0) { |
| 382 | cifs_dbg(FYI, "server UID changed\n"); |
| 383 | memcpy(server->server_GUID, guid, SMB1_CLIENT_GUID_SIZE); |
| 384 | } |
| 385 | } else { |
| 386 | spin_unlock(&cifs_tcp_ses_lock); |
| 387 | memcpy(server->server_GUID, guid, SMB1_CLIENT_GUID_SIZE); |
| 388 | } |
| 389 | |
| 390 | if (count == SMB1_CLIENT_GUID_SIZE) { |
Jeff Layton | 3f61822 | 2013-06-12 19:52:14 -0500 | [diff] [blame] | 391 | server->sec_ntlmssp = true; |
Jeff Layton | 31d9e2b | 2013-05-26 07:00:57 -0400 | [diff] [blame] | 392 | } else { |
| 393 | count -= SMB1_CLIENT_GUID_SIZE; |
| 394 | rc = decode_negTokenInit( |
| 395 | pSMBr->u.extended_response.SecurityBlob, count, server); |
| 396 | if (rc != 1) |
| 397 | return -EINVAL; |
Jeff Layton | 31d9e2b | 2013-05-26 07:00:57 -0400 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | return 0; |
| 401 | } |
| 402 | |
Jeff Layton | 9ddec56 | 2013-05-26 07:00:58 -0400 | [diff] [blame] | 403 | int |
Jeff Layton | 38d77c5 | 2013-05-26 07:01:00 -0400 | [diff] [blame] | 404 | cifs_enable_signing(struct TCP_Server_Info *server, bool mnt_sign_required) |
Jeff Layton | 9ddec56 | 2013-05-26 07:00:58 -0400 | [diff] [blame] | 405 | { |
Jeff Layton | 5028588 | 2013-06-27 12:45:00 -0400 | [diff] [blame] | 406 | bool srv_sign_required = server->sec_mode & server->vals->signing_required; |
| 407 | bool srv_sign_enabled = server->sec_mode & server->vals->signing_enabled; |
Jeff Layton | 38d77c5 | 2013-05-26 07:01:00 -0400 | [diff] [blame] | 408 | bool mnt_sign_enabled = global_secflags & CIFSSEC_MAY_SIGN; |
| 409 | |
| 410 | /* |
| 411 | * Is signing required by mnt options? If not then check |
| 412 | * global_secflags to see if it is there. |
| 413 | */ |
| 414 | if (!mnt_sign_required) |
| 415 | mnt_sign_required = ((global_secflags & CIFSSEC_MUST_SIGN) == |
| 416 | CIFSSEC_MUST_SIGN); |
| 417 | |
| 418 | /* |
| 419 | * If signing is required then it's automatically enabled too, |
| 420 | * otherwise, check to see if the secflags allow it. |
| 421 | */ |
| 422 | mnt_sign_enabled = mnt_sign_required ? mnt_sign_required : |
| 423 | (global_secflags & CIFSSEC_MAY_SIGN); |
| 424 | |
| 425 | /* If server requires signing, does client allow it? */ |
| 426 | if (srv_sign_required) { |
| 427 | if (!mnt_sign_enabled) { |
| 428 | cifs_dbg(VFS, "Server requires signing, but it's disabled in SecurityFlags!"); |
| 429 | return -ENOTSUPP; |
Jeff Layton | 9ddec56 | 2013-05-26 07:00:58 -0400 | [diff] [blame] | 430 | } |
Jeff Layton | 38d77c5 | 2013-05-26 07:01:00 -0400 | [diff] [blame] | 431 | server->sign = true; |
| 432 | } |
| 433 | |
| 434 | /* If client requires signing, does server allow it? */ |
| 435 | if (mnt_sign_required) { |
| 436 | if (!srv_sign_enabled) { |
| 437 | cifs_dbg(VFS, "Server does not support signing!"); |
| 438 | return -ENOTSUPP; |
| 439 | } |
| 440 | server->sign = true; |
Jeff Layton | 9ddec56 | 2013-05-26 07:00:58 -0400 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | return 0; |
| 444 | } |
| 445 | |
Jeff Layton | 2190eca | 2013-05-26 07:00:57 -0400 | [diff] [blame] | 446 | #ifdef CONFIG_CIFS_WEAK_PW_HASH |
| 447 | static int |
Jeff Layton | 3f61822 | 2013-06-12 19:52:14 -0500 | [diff] [blame] | 448 | decode_lanman_negprot_rsp(struct TCP_Server_Info *server, NEGOTIATE_RSP *pSMBr) |
Jeff Layton | 2190eca | 2013-05-26 07:00:57 -0400 | [diff] [blame] | 449 | { |
| 450 | __s16 tmp; |
| 451 | struct lanman_neg_rsp *rsp = (struct lanman_neg_rsp *)pSMBr; |
| 452 | |
| 453 | if (server->dialect != LANMAN_PROT && server->dialect != LANMAN2_PROT) |
| 454 | return -EOPNOTSUPP; |
| 455 | |
Jeff Layton | 2190eca | 2013-05-26 07:00:57 -0400 | [diff] [blame] | 456 | server->sec_mode = le16_to_cpu(rsp->SecurityMode); |
| 457 | server->maxReq = min_t(unsigned int, |
| 458 | le16_to_cpu(rsp->MaxMpxCount), |
| 459 | cifs_max_pending); |
| 460 | set_credits(server, server->maxReq); |
| 461 | server->maxBuf = le16_to_cpu(rsp->MaxBufSize); |
Jeff Layton | 2190eca | 2013-05-26 07:00:57 -0400 | [diff] [blame] | 462 | /* even though we do not use raw we might as well set this |
| 463 | accurately, in case we ever find a need for it */ |
| 464 | if ((le16_to_cpu(rsp->RawMode) & RAW_ENABLE) == RAW_ENABLE) { |
| 465 | server->max_rw = 0xFF00; |
| 466 | server->capabilities = CAP_MPX_MODE | CAP_RAW_MODE; |
| 467 | } else { |
| 468 | server->max_rw = 0;/* do not need to use raw anyway */ |
| 469 | server->capabilities = CAP_MPX_MODE; |
| 470 | } |
| 471 | tmp = (__s16)le16_to_cpu(rsp->ServerTimeZone); |
| 472 | if (tmp == -1) { |
| 473 | /* OS/2 often does not set timezone therefore |
| 474 | * we must use server time to calc time zone. |
| 475 | * Could deviate slightly from the right zone. |
| 476 | * Smallest defined timezone difference is 15 minutes |
| 477 | * (i.e. Nepal). Rounding up/down is done to match |
| 478 | * this requirement. |
| 479 | */ |
| 480 | int val, seconds, remain, result; |
| 481 | struct timespec ts, utc; |
| 482 | utc = CURRENT_TIME; |
| 483 | ts = cnvrtDosUnixTm(rsp->SrvTime.Date, |
| 484 | rsp->SrvTime.Time, 0); |
| 485 | cifs_dbg(FYI, "SrvTime %d sec since 1970 (utc: %d) diff: %d\n", |
| 486 | (int)ts.tv_sec, (int)utc.tv_sec, |
| 487 | (int)(utc.tv_sec - ts.tv_sec)); |
| 488 | val = (int)(utc.tv_sec - ts.tv_sec); |
| 489 | seconds = abs(val); |
| 490 | result = (seconds / MIN_TZ_ADJ) * MIN_TZ_ADJ; |
| 491 | remain = seconds % MIN_TZ_ADJ; |
| 492 | if (remain >= (MIN_TZ_ADJ / 2)) |
| 493 | result += MIN_TZ_ADJ; |
| 494 | if (val < 0) |
| 495 | result = -result; |
| 496 | server->timeAdj = result; |
| 497 | } else { |
| 498 | server->timeAdj = (int)tmp; |
| 499 | server->timeAdj *= 60; /* also in seconds */ |
| 500 | } |
| 501 | cifs_dbg(FYI, "server->timeAdj: %d seconds\n", server->timeAdj); |
| 502 | |
| 503 | |
| 504 | /* BB get server time for time conversions and add |
| 505 | code to use it and timezone since this is not UTC */ |
| 506 | |
| 507 | if (rsp->EncryptionKeyLength == |
| 508 | cpu_to_le16(CIFS_CRYPTO_KEY_SIZE)) { |
| 509 | memcpy(server->cryptkey, rsp->EncryptionKey, |
| 510 | CIFS_CRYPTO_KEY_SIZE); |
| 511 | } else if (server->sec_mode & SECMODE_PW_ENCRYPT) { |
| 512 | return -EIO; /* need cryptkey unless plain text */ |
| 513 | } |
| 514 | |
| 515 | cifs_dbg(FYI, "LANMAN negotiated\n"); |
| 516 | return 0; |
| 517 | } |
| 518 | #else |
| 519 | static inline int |
Jeff Layton | 3f61822 | 2013-06-12 19:52:14 -0500 | [diff] [blame] | 520 | decode_lanman_negprot_rsp(struct TCP_Server_Info *server, NEGOTIATE_RSP *pSMBr) |
Jeff Layton | 2190eca | 2013-05-26 07:00:57 -0400 | [diff] [blame] | 521 | { |
| 522 | cifs_dbg(VFS, "mount failed, cifs module not built with CIFS_WEAK_PW_HASH support\n"); |
| 523 | return -EOPNOTSUPP; |
| 524 | } |
| 525 | #endif |
| 526 | |
Jeff Layton | 9193400 | 2013-05-26 07:00:58 -0400 | [diff] [blame] | 527 | static bool |
Jeff Layton | 3f61822 | 2013-06-12 19:52:14 -0500 | [diff] [blame] | 528 | should_set_ext_sec_flag(enum securityEnum sectype) |
Jeff Layton | 9193400 | 2013-05-26 07:00:58 -0400 | [diff] [blame] | 529 | { |
Jeff Layton | 3f61822 | 2013-06-12 19:52:14 -0500 | [diff] [blame] | 530 | switch (sectype) { |
| 531 | case RawNTLMSSP: |
| 532 | case Kerberos: |
Jeff Layton | 9193400 | 2013-05-26 07:00:58 -0400 | [diff] [blame] | 533 | return true; |
Jeff Layton | 3f61822 | 2013-06-12 19:52:14 -0500 | [diff] [blame] | 534 | case Unspecified: |
| 535 | if (global_secflags & |
| 536 | (CIFSSEC_MAY_KRB5 | CIFSSEC_MAY_NTLMSSP)) |
| 537 | return true; |
| 538 | /* Fallthrough */ |
| 539 | default: |
| 540 | return false; |
| 541 | } |
Jeff Layton | 9193400 | 2013-05-26 07:00:58 -0400 | [diff] [blame] | 542 | } |
| 543 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | int |
Pavel Shilovsky | 286170a | 2012-05-25 10:43:58 +0400 | [diff] [blame] | 545 | CIFSSMBNegotiate(const unsigned int xid, struct cifs_ses *ses) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | { |
| 547 | NEGOTIATE_REQ *pSMB; |
| 548 | NEGOTIATE_RSP *pSMBr; |
| 549 | int rc = 0; |
| 550 | int bytes_returned; |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 551 | int i; |
Jeff Layton | 3534b85 | 2013-05-24 07:41:01 -0400 | [diff] [blame] | 552 | struct TCP_Server_Info *server = ses->server; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | u16 count; |
| 554 | |
Jeff Layton | 3534b85 | 2013-05-24 07:41:01 -0400 | [diff] [blame] | 555 | if (!server) { |
| 556 | WARN(1, "%s: server is NULL!\n", __func__); |
| 557 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | } |
Jeff Layton | 3534b85 | 2013-05-24 07:41:01 -0400 | [diff] [blame] | 559 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | rc = smb_init(SMB_COM_NEGOTIATE, 0, NULL /* no tcon yet */ , |
| 561 | (void **) &pSMB, (void **) &pSMBr); |
| 562 | if (rc) |
| 563 | return rc; |
Steve French | 750d115 | 2006-06-27 06:28:30 +0000 | [diff] [blame] | 564 | |
Pavel Shilovsky | 8825736 | 2012-05-23 14:01:59 +0400 | [diff] [blame] | 565 | pSMB->hdr.Mid = get_next_mid(server); |
Yehuda Sadeh Weinraub | 100c1dd | 2007-06-05 21:31:16 +0000 | [diff] [blame] | 566 | pSMB->hdr.Flags2 |= (SMBFLG2_UNICODE | SMBFLG2_ERR_STATUS); |
Steve French | a013689 | 2007-10-04 20:05:09 +0000 | [diff] [blame] | 567 | |
Jeff Layton | 3f61822 | 2013-06-12 19:52:14 -0500 | [diff] [blame] | 568 | if (should_set_ext_sec_flag(ses->sectype)) { |
Jeff Layton | 9193400 | 2013-05-26 07:00:58 -0400 | [diff] [blame] | 569 | cifs_dbg(FYI, "Requesting extended security."); |
Steve French | ac68392 | 2009-05-06 04:16:04 +0000 | [diff] [blame] | 570 | pSMB->hdr.Flags2 |= SMBFLG2_EXT_SEC; |
| 571 | } |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 572 | |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 573 | count = 0; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 574 | for (i = 0; i < CIFS_NUM_PROT; i++) { |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 575 | strncpy(pSMB->DialectsArray+count, protocols[i].name, 16); |
| 576 | count += strlen(protocols[i].name) + 1; |
| 577 | /* null at end of source and target buffers anyway */ |
| 578 | } |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 579 | inc_rfc1001_len(pSMB, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | pSMB->ByteCount = cpu_to_le16(count); |
| 581 | |
| 582 | rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB, |
| 583 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 584 | if (rc != 0) |
Steve French | 254e55e | 2006-06-04 05:53:15 +0000 | [diff] [blame] | 585 | goto neg_err_exit; |
| 586 | |
Jeff Layton | 9bf67e5 | 2010-04-24 07:57:46 -0400 | [diff] [blame] | 587 | server->dialect = le16_to_cpu(pSMBr->DialectIndex); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 588 | cifs_dbg(FYI, "Dialect: %d\n", server->dialect); |
Steve French | 254e55e | 2006-06-04 05:53:15 +0000 | [diff] [blame] | 589 | /* Check wct = 1 error case */ |
Jeff Layton | 9bf67e5 | 2010-04-24 07:57:46 -0400 | [diff] [blame] | 590 | if ((pSMBr->hdr.WordCount < 13) || (server->dialect == BAD_PROT)) { |
Steve French | 254e55e | 2006-06-04 05:53:15 +0000 | [diff] [blame] | 591 | /* core returns wct = 1, but we do not ask for core - otherwise |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 592 | small wct just comes when dialect index is -1 indicating we |
Steve French | 254e55e | 2006-06-04 05:53:15 +0000 | [diff] [blame] | 593 | could not negotiate a common dialect */ |
| 594 | rc = -EOPNOTSUPP; |
| 595 | goto neg_err_exit; |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 596 | } else if (pSMBr->hdr.WordCount == 13) { |
Jeff Layton | e598d1d8 | 2013-05-26 07:00:59 -0400 | [diff] [blame] | 597 | server->negflavor = CIFS_NEGFLAVOR_LANMAN; |
Jeff Layton | 3f61822 | 2013-06-12 19:52:14 -0500 | [diff] [blame] | 598 | rc = decode_lanman_negprot_rsp(server, pSMBr); |
Jeff Layton | 9ddec56 | 2013-05-26 07:00:58 -0400 | [diff] [blame] | 599 | goto signing_check; |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 600 | } else if (pSMBr->hdr.WordCount != 17) { |
Steve French | 254e55e | 2006-06-04 05:53:15 +0000 | [diff] [blame] | 601 | /* unknown wct */ |
| 602 | rc = -EOPNOTSUPP; |
| 603 | goto neg_err_exit; |
| 604 | } |
Jeff Layton | 2190eca | 2013-05-26 07:00:57 -0400 | [diff] [blame] | 605 | /* else wct == 17, NTLM or better */ |
| 606 | |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 607 | server->sec_mode = pSMBr->SecurityMode; |
| 608 | if ((server->sec_mode & SECMODE_USER) == 0) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 609 | cifs_dbg(FYI, "share mode security\n"); |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 610 | |
Steve French | 254e55e | 2006-06-04 05:53:15 +0000 | [diff] [blame] | 611 | /* one byte, so no need to convert this or EncryptionKeyLen from |
| 612 | little endian */ |
Pavel Shilovsky | 10b9b98 | 2012-03-20 12:55:09 +0300 | [diff] [blame] | 613 | server->maxReq = min_t(unsigned int, le16_to_cpu(pSMBr->MaxMpxCount), |
| 614 | cifs_max_pending); |
Pavel Shilovsky | 4527578 | 2012-05-17 17:53:29 +0400 | [diff] [blame] | 615 | set_credits(server, server->maxReq); |
Steve French | 254e55e | 2006-06-04 05:53:15 +0000 | [diff] [blame] | 616 | /* probably no need to store and check maxvcs */ |
Jeff Layton | c974bef | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 617 | server->maxBuf = le32_to_cpu(pSMBr->MaxBufferSize); |
Steve French | eca6acf | 2009-02-20 05:43:09 +0000 | [diff] [blame] | 618 | server->max_rw = le32_to_cpu(pSMBr->MaxRawSize); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 619 | cifs_dbg(NOISY, "Max buf = %d\n", ses->server->maxBuf); |
Steve French | 254e55e | 2006-06-04 05:53:15 +0000 | [diff] [blame] | 620 | server->capabilities = le32_to_cpu(pSMBr->Capabilities); |
Steve French | b815f1e5 | 2006-10-02 05:53:29 +0000 | [diff] [blame] | 621 | server->timeAdj = (int)(__s16)le16_to_cpu(pSMBr->ServerTimeZone); |
| 622 | server->timeAdj *= 60; |
Jeff Layton | 31d9e2b | 2013-05-26 07:00:57 -0400 | [diff] [blame] | 623 | |
Jeff Layton | e598d1d8 | 2013-05-26 07:00:59 -0400 | [diff] [blame] | 624 | if (pSMBr->EncryptionKeyLength == CIFS_CRYPTO_KEY_SIZE) { |
| 625 | server->negflavor = CIFS_NEGFLAVOR_UNENCAP; |
Shirish Pargaonkar | d3ba50b | 2010-10-27 15:20:36 -0500 | [diff] [blame] | 626 | memcpy(ses->server->cryptkey, pSMBr->u.EncryptionKey, |
Steve French | 254e55e | 2006-06-04 05:53:15 +0000 | [diff] [blame] | 627 | CIFS_CRYPTO_KEY_SIZE); |
Noel Power | f291095 | 2015-05-27 09:22:10 +0100 | [diff] [blame] | 628 | } else if (pSMBr->hdr.Flags2 & SMBFLG2_EXT_SEC || |
| 629 | server->capabilities & CAP_EXTENDED_SECURITY) { |
Jeff Layton | e598d1d8 | 2013-05-26 07:00:59 -0400 | [diff] [blame] | 630 | server->negflavor = CIFS_NEGFLAVOR_EXTENDED; |
Jeff Layton | 3f61822 | 2013-06-12 19:52:14 -0500 | [diff] [blame] | 631 | rc = decode_ext_sec_blob(ses, pSMBr); |
Jeff Layton | e598d1d8 | 2013-05-26 07:00:59 -0400 | [diff] [blame] | 632 | } else if (server->sec_mode & SECMODE_PW_ENCRYPT) { |
Steve French | 07cc6cf | 2011-05-27 04:12:29 +0000 | [diff] [blame] | 633 | rc = -EIO; /* no crypt key only if plain text pwd */ |
Jeff Layton | e598d1d8 | 2013-05-26 07:00:59 -0400 | [diff] [blame] | 634 | } else { |
| 635 | server->negflavor = CIFS_NEGFLAVOR_UNENCAP; |
Steve French | 254e55e | 2006-06-04 05:53:15 +0000 | [diff] [blame] | 636 | server->capabilities &= ~CAP_EXTENDED_SECURITY; |
Jeff Layton | e598d1d8 | 2013-05-26 07:00:59 -0400 | [diff] [blame] | 637 | } |
Steve French | 254e55e | 2006-06-04 05:53:15 +0000 | [diff] [blame] | 638 | |
| 639 | signing_check: |
Jeff Layton | 9ddec56 | 2013-05-26 07:00:58 -0400 | [diff] [blame] | 640 | if (!rc) |
Jeff Layton | 38d77c5 | 2013-05-26 07:01:00 -0400 | [diff] [blame] | 641 | rc = cifs_enable_signing(server, ses->sign); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 642 | neg_err_exit: |
Steve French | 4a6d87f | 2005-08-13 08:15:54 -0700 | [diff] [blame] | 643 | cifs_buf_release(pSMB); |
Steve French | 254e55e | 2006-06-04 05:53:15 +0000 | [diff] [blame] | 644 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 645 | cifs_dbg(FYI, "negprot rc %d\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | return rc; |
| 647 | } |
| 648 | |
| 649 | int |
Pavel Shilovsky | 2e6e02a | 2012-05-25 11:11:39 +0400 | [diff] [blame] | 650 | CIFSSMBTDis(const unsigned int xid, struct cifs_tcon *tcon) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | { |
| 652 | struct smb_hdr *smb_buffer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | int rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 655 | cifs_dbg(FYI, "In tree disconnect\n"); |
Jeff Layton | f1987b4 | 2008-11-15 11:12:47 -0500 | [diff] [blame] | 656 | |
| 657 | /* BB: do we need to check this? These should never be NULL. */ |
| 658 | if ((tcon->ses == NULL) || (tcon->ses->server == NULL)) |
| 659 | return -EIO; |
| 660 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | /* |
Jeff Layton | f1987b4 | 2008-11-15 11:12:47 -0500 | [diff] [blame] | 662 | * No need to return error on this operation if tid invalidated and |
| 663 | * closed on server already e.g. due to tcp session crashing. Also, |
| 664 | * the tcon is no longer on the list, so no need to take lock before |
| 665 | * checking this. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | */ |
Steve French | 268875b | 2009-06-25 00:29:21 +0000 | [diff] [blame] | 667 | if ((tcon->need_reconnect) || (tcon->ses->need_reconnect)) |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 668 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 670 | rc = small_smb_init(SMB_COM_TREE_DISCONNECT, 0, tcon, |
Steve French | 09d1db5 | 2005-04-28 22:41:08 -0700 | [diff] [blame] | 671 | (void **)&smb_buffer); |
Jeff Layton | f1987b4 | 2008-11-15 11:12:47 -0500 | [diff] [blame] | 672 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | return rc; |
Steve French | 133672e | 2007-11-13 22:41:37 +0000 | [diff] [blame] | 674 | |
Pavel Shilovsky | 792af7b | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 675 | rc = SendReceiveNoRsp(xid, tcon->ses, (char *)smb_buffer, 0); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 676 | cifs_small_buf_release(smb_buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 678 | cifs_dbg(FYI, "Tree disconnect failed %d\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 680 | /* No need to return error on this operation if tid invalidated and |
Jeff Layton | f1987b4 | 2008-11-15 11:12:47 -0500 | [diff] [blame] | 681 | closed on server already e.g. due to tcp session crashing */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | if (rc == -EAGAIN) |
| 683 | rc = 0; |
| 684 | |
| 685 | return rc; |
| 686 | } |
| 687 | |
Jeff Layton | 766fdbb | 2011-01-11 07:24:21 -0500 | [diff] [blame] | 688 | /* |
| 689 | * This is a no-op for now. We're not really interested in the reply, but |
| 690 | * rather in the fact that the server sent one and that server->lstrp |
| 691 | * gets updated. |
| 692 | * |
| 693 | * FIXME: maybe we should consider checking that the reply matches request? |
| 694 | */ |
| 695 | static void |
| 696 | cifs_echo_callback(struct mid_q_entry *mid) |
| 697 | { |
| 698 | struct TCP_Server_Info *server = mid->callback_data; |
| 699 | |
Christopher Oo | 5fb4e28 | 2015-06-25 16:10:48 -0700 | [diff] [blame] | 700 | mutex_lock(&server->srv_mutex); |
Jeff Layton | 766fdbb | 2011-01-11 07:24:21 -0500 | [diff] [blame] | 701 | DeleteMidQEntry(mid); |
Christopher Oo | 5fb4e28 | 2015-06-25 16:10:48 -0700 | [diff] [blame] | 702 | mutex_unlock(&server->srv_mutex); |
Pavel Shilovsky | a891f0f | 2012-05-23 16:14:34 +0400 | [diff] [blame] | 703 | add_credits(server, 1, CIFS_ECHO_OP); |
Jeff Layton | 766fdbb | 2011-01-11 07:24:21 -0500 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | int |
| 707 | CIFSSMBEcho(struct TCP_Server_Info *server) |
| 708 | { |
| 709 | ECHO_REQ *smb; |
| 710 | int rc = 0; |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 711 | struct kvec iov[2]; |
| 712 | struct smb_rqst rqst = { .rq_iov = iov, |
| 713 | .rq_nvec = 2 }; |
Jeff Layton | 766fdbb | 2011-01-11 07:24:21 -0500 | [diff] [blame] | 714 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 715 | cifs_dbg(FYI, "In echo request\n"); |
Jeff Layton | 766fdbb | 2011-01-11 07:24:21 -0500 | [diff] [blame] | 716 | |
| 717 | rc = small_smb_init(SMB_COM_ECHO, 0, NULL, (void **)&smb); |
| 718 | if (rc) |
| 719 | return rc; |
| 720 | |
| 721 | /* set up echo request */ |
Steve French | 5443d13 | 2011-03-13 05:08:25 +0000 | [diff] [blame] | 722 | smb->hdr.Tid = 0xffff; |
Jeff Layton | 99d86c8f | 2011-01-20 21:19:25 -0500 | [diff] [blame] | 723 | smb->hdr.WordCount = 1; |
| 724 | put_unaligned_le16(1, &smb->EchoCount); |
Jeff Layton | 820a803 | 2011-05-04 08:05:26 -0400 | [diff] [blame] | 725 | put_bcc(1, &smb->hdr); |
Jeff Layton | 766fdbb | 2011-01-11 07:24:21 -0500 | [diff] [blame] | 726 | smb->Data[0] = 'a'; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 727 | inc_rfc1001_len(smb, 3); |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 728 | |
| 729 | iov[0].iov_len = 4; |
| 730 | iov[0].iov_base = smb; |
| 731 | iov[1].iov_len = get_rfc1002_length(smb); |
| 732 | iov[1].iov_base = (char *)smb + 4; |
Jeff Layton | 766fdbb | 2011-01-11 07:24:21 -0500 | [diff] [blame] | 733 | |
Pavel Shilovsky | 9b7c18a | 2016-11-16 14:06:17 -0800 | [diff] [blame] | 734 | rc = cifs_call_async(server, &rqst, NULL, cifs_echo_callback, NULL, |
Pavel Shilovsky | a891f0f | 2012-05-23 16:14:34 +0400 | [diff] [blame] | 735 | server, CIFS_ASYNC_OP | CIFS_ECHO_OP); |
Jeff Layton | 766fdbb | 2011-01-11 07:24:21 -0500 | [diff] [blame] | 736 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 737 | cifs_dbg(FYI, "Echo request failed: %d\n", rc); |
Jeff Layton | 766fdbb | 2011-01-11 07:24:21 -0500 | [diff] [blame] | 738 | |
| 739 | cifs_small_buf_release(smb); |
| 740 | |
| 741 | return rc; |
| 742 | } |
| 743 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | int |
Pavel Shilovsky | 58c45c5 | 2012-05-25 10:54:49 +0400 | [diff] [blame] | 745 | CIFSSMBLogoff(const unsigned int xid, struct cifs_ses *ses) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | LOGOFF_ANDX_REQ *pSMB; |
| 748 | int rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 750 | cifs_dbg(FYI, "In SMBLogoff for session disconnect\n"); |
Jeff Layton | 14fbf50 | 2008-11-14 13:53:46 -0500 | [diff] [blame] | 751 | |
| 752 | /* |
| 753 | * BB: do we need to check validity of ses and server? They should |
| 754 | * always be valid since we have an active reference. If not, that |
| 755 | * should probably be a BUG() |
| 756 | */ |
| 757 | if (!ses || !ses->server) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | return -EIO; |
| 759 | |
Steve French | d7b619c | 2010-02-25 05:36:46 +0000 | [diff] [blame] | 760 | mutex_lock(&ses->session_mutex); |
Steve French | 3b79521 | 2008-11-13 19:45:32 +0000 | [diff] [blame] | 761 | if (ses->need_reconnect) |
| 762 | goto session_already_dead; /* no need to send SMBlogoff if uid |
| 763 | already closed due to reconnect */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | rc = small_smb_init(SMB_COM_LOGOFF_ANDX, 2, NULL, (void **)&pSMB); |
| 765 | if (rc) { |
Steve French | d7b619c | 2010-02-25 05:36:46 +0000 | [diff] [blame] | 766 | mutex_unlock(&ses->session_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | return rc; |
| 768 | } |
| 769 | |
Pavel Shilovsky | 8825736 | 2012-05-23 14:01:59 +0400 | [diff] [blame] | 770 | pSMB->hdr.Mid = get_next_mid(ses->server); |
Steve French | 1982c34 | 2005-08-17 12:38:22 -0700 | [diff] [blame] | 771 | |
Jeff Layton | 38d77c5 | 2013-05-26 07:01:00 -0400 | [diff] [blame] | 772 | if (ses->server->sign) |
| 773 | pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | |
| 775 | pSMB->hdr.Uid = ses->Suid; |
| 776 | |
| 777 | pSMB->AndXCommand = 0xFF; |
Pavel Shilovsky | 792af7b | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 778 | rc = SendReceiveNoRsp(xid, ses, (char *) pSMB, 0); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 779 | cifs_small_buf_release(pSMB); |
Steve French | 3b79521 | 2008-11-13 19:45:32 +0000 | [diff] [blame] | 780 | session_already_dead: |
Steve French | d7b619c | 2010-02-25 05:36:46 +0000 | [diff] [blame] | 781 | mutex_unlock(&ses->session_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | |
| 783 | /* if session dead then we do not need to do ulogoff, |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 784 | since server closed smb session, no sense reporting |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | error */ |
| 786 | if (rc == -EAGAIN) |
| 787 | rc = 0; |
| 788 | return rc; |
| 789 | } |
| 790 | |
| 791 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 792 | CIFSPOSIXDelFile(const unsigned int xid, struct cifs_tcon *tcon, |
| 793 | const char *fileName, __u16 type, |
| 794 | const struct nls_table *nls_codepage, int remap) |
Steve French | 2d785a5 | 2007-07-15 01:48:57 +0000 | [diff] [blame] | 795 | { |
| 796 | TRANSACTION2_SPI_REQ *pSMB = NULL; |
| 797 | TRANSACTION2_SPI_RSP *pSMBr = NULL; |
| 798 | struct unlink_psx_rq *pRqD; |
| 799 | int name_len; |
| 800 | int rc = 0; |
| 801 | int bytes_returned = 0; |
| 802 | __u16 params, param_offset, offset, byte_count; |
| 803 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 804 | cifs_dbg(FYI, "In POSIX delete\n"); |
Steve French | 2d785a5 | 2007-07-15 01:48:57 +0000 | [diff] [blame] | 805 | PsxDelete: |
| 806 | rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB, |
| 807 | (void **) &pSMBr); |
| 808 | if (rc) |
| 809 | return rc; |
| 810 | |
| 811 | if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) { |
| 812 | name_len = |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 813 | cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName, |
| 814 | PATH_MAX, nls_codepage, remap); |
Steve French | 2d785a5 | 2007-07-15 01:48:57 +0000 | [diff] [blame] | 815 | name_len++; /* trailing null */ |
| 816 | name_len *= 2; |
| 817 | } else { /* BB add path length overrun check */ |
| 818 | name_len = strnlen(fileName, PATH_MAX); |
| 819 | name_len++; /* trailing null */ |
| 820 | strncpy(pSMB->FileName, fileName, name_len); |
| 821 | } |
| 822 | |
| 823 | params = 6 + name_len; |
| 824 | pSMB->MaxParameterCount = cpu_to_le16(2); |
| 825 | pSMB->MaxDataCount = 0; /* BB double check this with jra */ |
| 826 | pSMB->MaxSetupCount = 0; |
| 827 | pSMB->Reserved = 0; |
| 828 | pSMB->Flags = 0; |
| 829 | pSMB->Timeout = 0; |
| 830 | pSMB->Reserved2 = 0; |
| 831 | param_offset = offsetof(struct smb_com_transaction2_spi_req, |
| 832 | InformationLevel) - 4; |
| 833 | offset = param_offset + params; |
| 834 | |
| 835 | /* Setup pointer to Request Data (inode type) */ |
| 836 | pRqD = (struct unlink_psx_rq *)(((char *)&pSMB->hdr.Protocol) + offset); |
| 837 | pRqD->type = cpu_to_le16(type); |
| 838 | pSMB->ParameterOffset = cpu_to_le16(param_offset); |
| 839 | pSMB->DataOffset = cpu_to_le16(offset); |
| 840 | pSMB->SetupCount = 1; |
| 841 | pSMB->Reserved3 = 0; |
| 842 | pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION); |
| 843 | byte_count = 3 /* pad */ + params + sizeof(struct unlink_psx_rq); |
| 844 | |
| 845 | pSMB->DataCount = cpu_to_le16(sizeof(struct unlink_psx_rq)); |
| 846 | pSMB->TotalDataCount = cpu_to_le16(sizeof(struct unlink_psx_rq)); |
| 847 | pSMB->ParameterCount = cpu_to_le16(params); |
| 848 | pSMB->TotalParameterCount = pSMB->ParameterCount; |
| 849 | pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_UNLINK); |
| 850 | pSMB->Reserved4 = 0; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 851 | inc_rfc1001_len(pSMB, byte_count); |
Steve French | 2d785a5 | 2007-07-15 01:48:57 +0000 | [diff] [blame] | 852 | pSMB->ByteCount = cpu_to_le16(byte_count); |
| 853 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 854 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 855 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 856 | cifs_dbg(FYI, "Posix delete returned %d\n", rc); |
Steve French | 2d785a5 | 2007-07-15 01:48:57 +0000 | [diff] [blame] | 857 | cifs_buf_release(pSMB); |
| 858 | |
Pavel Shilovsky | 44c5818 | 2012-05-28 14:16:31 +0400 | [diff] [blame] | 859 | cifs_stats_inc(&tcon->stats.cifs_stats.num_deletes); |
Steve French | 2d785a5 | 2007-07-15 01:48:57 +0000 | [diff] [blame] | 860 | |
| 861 | if (rc == -EAGAIN) |
| 862 | goto PsxDelete; |
| 863 | |
| 864 | return rc; |
| 865 | } |
| 866 | |
| 867 | int |
Pavel Shilovsky | ed6875e | 2012-09-18 16:20:25 -0700 | [diff] [blame] | 868 | CIFSSMBDelFile(const unsigned int xid, struct cifs_tcon *tcon, const char *name, |
| 869 | struct cifs_sb_info *cifs_sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | { |
| 871 | DELETE_FILE_REQ *pSMB = NULL; |
| 872 | DELETE_FILE_RSP *pSMBr = NULL; |
| 873 | int rc = 0; |
| 874 | int bytes_returned; |
| 875 | int name_len; |
Steve French | 2baa268 | 2014-09-27 02:19:01 -0500 | [diff] [blame] | 876 | int remap = cifs_remap(cifs_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | |
| 878 | DelFileRetry: |
| 879 | rc = smb_init(SMB_COM_DELETE, 1, tcon, (void **) &pSMB, |
| 880 | (void **) &pSMBr); |
| 881 | if (rc) |
| 882 | return rc; |
| 883 | |
| 884 | if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) { |
Pavel Shilovsky | ed6875e | 2012-09-18 16:20:25 -0700 | [diff] [blame] | 885 | name_len = cifsConvertToUTF16((__le16 *) pSMB->fileName, name, |
| 886 | PATH_MAX, cifs_sb->local_nls, |
| 887 | remap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | name_len++; /* trailing null */ |
| 889 | name_len *= 2; |
Steve French | 09d1db5 | 2005-04-28 22:41:08 -0700 | [diff] [blame] | 890 | } else { /* BB improve check for buffer overruns BB */ |
Pavel Shilovsky | ed6875e | 2012-09-18 16:20:25 -0700 | [diff] [blame] | 891 | name_len = strnlen(name, PATH_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | name_len++; /* trailing null */ |
Pavel Shilovsky | ed6875e | 2012-09-18 16:20:25 -0700 | [diff] [blame] | 893 | strncpy(pSMB->fileName, name, name_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | } |
| 895 | pSMB->SearchAttributes = |
| 896 | cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM); |
| 897 | pSMB->BufferFormat = 0x04; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 898 | inc_rfc1001_len(pSMB, name_len + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | pSMB->ByteCount = cpu_to_le16(name_len + 1); |
| 900 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 901 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
Pavel Shilovsky | 44c5818 | 2012-05-28 14:16:31 +0400 | [diff] [blame] | 902 | cifs_stats_inc(&tcon->stats.cifs_stats.num_deletes); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 903 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 904 | cifs_dbg(FYI, "Error in RMFile = %d\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | |
| 906 | cifs_buf_release(pSMB); |
| 907 | if (rc == -EAGAIN) |
| 908 | goto DelFileRetry; |
| 909 | |
| 910 | return rc; |
| 911 | } |
| 912 | |
| 913 | int |
Pavel Shilovsky | f958ca5 | 2012-07-10 16:14:18 +0400 | [diff] [blame] | 914 | CIFSSMBRmDir(const unsigned int xid, struct cifs_tcon *tcon, const char *name, |
| 915 | struct cifs_sb_info *cifs_sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | { |
| 917 | DELETE_DIRECTORY_REQ *pSMB = NULL; |
| 918 | DELETE_DIRECTORY_RSP *pSMBr = NULL; |
| 919 | int rc = 0; |
| 920 | int bytes_returned; |
| 921 | int name_len; |
Steve French | 2baa268 | 2014-09-27 02:19:01 -0500 | [diff] [blame] | 922 | int remap = cifs_remap(cifs_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 924 | cifs_dbg(FYI, "In CIFSSMBRmDir\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | RmDirRetry: |
| 926 | rc = smb_init(SMB_COM_DELETE_DIRECTORY, 0, tcon, (void **) &pSMB, |
| 927 | (void **) &pSMBr); |
| 928 | if (rc) |
| 929 | return rc; |
| 930 | |
| 931 | if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) { |
Pavel Shilovsky | f958ca5 | 2012-07-10 16:14:18 +0400 | [diff] [blame] | 932 | name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, name, |
| 933 | PATH_MAX, cifs_sb->local_nls, |
| 934 | remap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | name_len++; /* trailing null */ |
| 936 | name_len *= 2; |
Steve French | 09d1db5 | 2005-04-28 22:41:08 -0700 | [diff] [blame] | 937 | } else { /* BB improve check for buffer overruns BB */ |
Pavel Shilovsky | f958ca5 | 2012-07-10 16:14:18 +0400 | [diff] [blame] | 938 | name_len = strnlen(name, PATH_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | name_len++; /* trailing null */ |
Pavel Shilovsky | f958ca5 | 2012-07-10 16:14:18 +0400 | [diff] [blame] | 940 | strncpy(pSMB->DirName, name, name_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | pSMB->BufferFormat = 0x04; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 944 | inc_rfc1001_len(pSMB, name_len + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | pSMB->ByteCount = cpu_to_le16(name_len + 1); |
| 946 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 947 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
Pavel Shilovsky | 44c5818 | 2012-05-28 14:16:31 +0400 | [diff] [blame] | 948 | cifs_stats_inc(&tcon->stats.cifs_stats.num_rmdirs); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 949 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 950 | cifs_dbg(FYI, "Error in RMDir = %d\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | |
| 952 | cifs_buf_release(pSMB); |
| 953 | if (rc == -EAGAIN) |
| 954 | goto RmDirRetry; |
| 955 | return rc; |
| 956 | } |
| 957 | |
| 958 | int |
Pavel Shilovsky | f436720 | 2012-03-17 11:41:12 +0300 | [diff] [blame] | 959 | CIFSSMBMkDir(const unsigned int xid, struct cifs_tcon *tcon, const char *name, |
| 960 | struct cifs_sb_info *cifs_sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | { |
| 962 | int rc = 0; |
| 963 | CREATE_DIRECTORY_REQ *pSMB = NULL; |
| 964 | CREATE_DIRECTORY_RSP *pSMBr = NULL; |
| 965 | int bytes_returned; |
| 966 | int name_len; |
Steve French | 2baa268 | 2014-09-27 02:19:01 -0500 | [diff] [blame] | 967 | int remap = cifs_remap(cifs_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 969 | cifs_dbg(FYI, "In CIFSSMBMkDir\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | MkDirRetry: |
| 971 | rc = smb_init(SMB_COM_CREATE_DIRECTORY, 0, tcon, (void **) &pSMB, |
| 972 | (void **) &pSMBr); |
| 973 | if (rc) |
| 974 | return rc; |
| 975 | |
| 976 | if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) { |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 977 | name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, name, |
Pavel Shilovsky | f436720 | 2012-03-17 11:41:12 +0300 | [diff] [blame] | 978 | PATH_MAX, cifs_sb->local_nls, |
| 979 | remap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | name_len++; /* trailing null */ |
| 981 | name_len *= 2; |
Steve French | 09d1db5 | 2005-04-28 22:41:08 -0700 | [diff] [blame] | 982 | } else { /* BB improve check for buffer overruns BB */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | name_len = strnlen(name, PATH_MAX); |
| 984 | name_len++; /* trailing null */ |
| 985 | strncpy(pSMB->DirName, name, name_len); |
| 986 | } |
| 987 | |
| 988 | pSMB->BufferFormat = 0x04; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 989 | inc_rfc1001_len(pSMB, name_len + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | pSMB->ByteCount = cpu_to_le16(name_len + 1); |
| 991 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 992 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
Pavel Shilovsky | 44c5818 | 2012-05-28 14:16:31 +0400 | [diff] [blame] | 993 | cifs_stats_inc(&tcon->stats.cifs_stats.num_mkdirs); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 994 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 995 | cifs_dbg(FYI, "Error in Mkdir = %d\n", rc); |
Steve French | a5a2b48 | 2005-08-20 21:42:53 -0700 | [diff] [blame] | 996 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | cifs_buf_release(pSMB); |
| 998 | if (rc == -EAGAIN) |
| 999 | goto MkDirRetry; |
| 1000 | return rc; |
| 1001 | } |
| 1002 | |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1003 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1004 | CIFSPOSIXCreate(const unsigned int xid, struct cifs_tcon *tcon, |
| 1005 | __u32 posix_flags, __u64 mode, __u16 *netfid, |
| 1006 | FILE_UNIX_BASIC_INFO *pRetData, __u32 *pOplock, |
| 1007 | const char *name, const struct nls_table *nls_codepage, |
| 1008 | int remap) |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1009 | { |
| 1010 | TRANSACTION2_SPI_REQ *pSMB = NULL; |
| 1011 | TRANSACTION2_SPI_RSP *pSMBr = NULL; |
| 1012 | int name_len; |
| 1013 | int rc = 0; |
| 1014 | int bytes_returned = 0; |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1015 | __u16 params, param_offset, offset, byte_count, count; |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 1016 | OPEN_PSX_REQ *pdata; |
| 1017 | OPEN_PSX_RSP *psx_rsp; |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1018 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1019 | cifs_dbg(FYI, "In POSIX Create\n"); |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1020 | PsxCreat: |
| 1021 | rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB, |
| 1022 | (void **) &pSMBr); |
| 1023 | if (rc) |
| 1024 | return rc; |
| 1025 | |
| 1026 | if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) { |
| 1027 | name_len = |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 1028 | cifsConvertToUTF16((__le16 *) pSMB->FileName, name, |
| 1029 | PATH_MAX, nls_codepage, remap); |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1030 | name_len++; /* trailing null */ |
| 1031 | name_len *= 2; |
| 1032 | } else { /* BB improve the check for buffer overruns BB */ |
| 1033 | name_len = strnlen(name, PATH_MAX); |
| 1034 | name_len++; /* trailing null */ |
| 1035 | strncpy(pSMB->FileName, name, name_len); |
| 1036 | } |
| 1037 | |
| 1038 | params = 6 + name_len; |
| 1039 | count = sizeof(OPEN_PSX_REQ); |
| 1040 | pSMB->MaxParameterCount = cpu_to_le16(2); |
| 1041 | pSMB->MaxDataCount = cpu_to_le16(1000); /* large enough */ |
| 1042 | pSMB->MaxSetupCount = 0; |
| 1043 | pSMB->Reserved = 0; |
| 1044 | pSMB->Flags = 0; |
| 1045 | pSMB->Timeout = 0; |
| 1046 | pSMB->Reserved2 = 0; |
| 1047 | param_offset = offsetof(struct smb_com_transaction2_spi_req, |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 1048 | InformationLevel) - 4; |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1049 | offset = param_offset + params; |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1050 | pdata = (OPEN_PSX_REQ *)(((char *)&pSMB->hdr.Protocol) + offset); |
Cyril Gorcunov | 8f2376a | 2007-10-14 17:58:43 +0000 | [diff] [blame] | 1051 | pdata->Level = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC); |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1052 | pdata->Permissions = cpu_to_le64(mode); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 1053 | pdata->PosixOpenFlags = cpu_to_le32(posix_flags); |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1054 | pdata->OpenFlags = cpu_to_le32(*pOplock); |
| 1055 | pSMB->ParameterOffset = cpu_to_le16(param_offset); |
| 1056 | pSMB->DataOffset = cpu_to_le16(offset); |
| 1057 | pSMB->SetupCount = 1; |
| 1058 | pSMB->Reserved3 = 0; |
| 1059 | pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION); |
| 1060 | byte_count = 3 /* pad */ + params + count; |
| 1061 | |
| 1062 | pSMB->DataCount = cpu_to_le16(count); |
| 1063 | pSMB->ParameterCount = cpu_to_le16(params); |
| 1064 | pSMB->TotalDataCount = pSMB->DataCount; |
| 1065 | pSMB->TotalParameterCount = pSMB->ParameterCount; |
| 1066 | pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_OPEN); |
| 1067 | pSMB->Reserved4 = 0; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 1068 | inc_rfc1001_len(pSMB, byte_count); |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1069 | pSMB->ByteCount = cpu_to_le16(byte_count); |
| 1070 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 1071 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
| 1072 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1073 | cifs_dbg(FYI, "Posix create returned %d\n", rc); |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1074 | goto psx_create_err; |
| 1075 | } |
| 1076 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1077 | cifs_dbg(FYI, "copying inode info\n"); |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1078 | rc = validate_t2((struct smb_t2_rsp *)pSMBr); |
| 1079 | |
Jeff Layton | 820a803 | 2011-05-04 08:05:26 -0400 | [diff] [blame] | 1080 | if (rc || get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)) { |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1081 | rc = -EIO; /* bad smb */ |
| 1082 | goto psx_create_err; |
| 1083 | } |
| 1084 | |
| 1085 | /* copy return information to pRetData */ |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 1086 | psx_rsp = (OPEN_PSX_RSP *)((char *) &pSMBr->hdr.Protocol |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1087 | + le16_to_cpu(pSMBr->t2.DataOffset)); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 1088 | |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1089 | *pOplock = le16_to_cpu(psx_rsp->OplockFlags); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1090 | if (netfid) |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1091 | *netfid = psx_rsp->Fid; /* cifs fid stays in le */ |
| 1092 | /* Let caller know file was created so we can set the mode. */ |
| 1093 | /* Do we care about the CreateAction in any other cases? */ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1094 | if (cpu_to_le32(FILE_CREATE) == psx_rsp->CreateAction) |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1095 | *pOplock |= CIFS_CREATE_ACTION; |
| 1096 | /* check to make sure response data is there */ |
Cyril Gorcunov | 8f2376a | 2007-10-14 17:58:43 +0000 | [diff] [blame] | 1097 | if (psx_rsp->ReturnedLevel != cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC)) { |
| 1098 | pRetData->Type = cpu_to_le32(-1); /* unknown */ |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1099 | cifs_dbg(NOISY, "unknown type\n"); |
Steve French | cbac3cb | 2007-04-25 11:46:06 +0000 | [diff] [blame] | 1100 | } else { |
Jeff Layton | 820a803 | 2011-05-04 08:05:26 -0400 | [diff] [blame] | 1101 | if (get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP) |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1102 | + sizeof(FILE_UNIX_BASIC_INFO)) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1103 | cifs_dbg(VFS, "Open response data too small\n"); |
Cyril Gorcunov | 8f2376a | 2007-10-14 17:58:43 +0000 | [diff] [blame] | 1104 | pRetData->Type = cpu_to_le32(-1); |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1105 | goto psx_create_err; |
| 1106 | } |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 1107 | memcpy((char *) pRetData, |
Steve French | cbac3cb | 2007-04-25 11:46:06 +0000 | [diff] [blame] | 1108 | (char *)psx_rsp + sizeof(OPEN_PSX_RSP), |
Steve French | 26f5736 | 2007-08-30 22:09:15 +0000 | [diff] [blame] | 1109 | sizeof(FILE_UNIX_BASIC_INFO)); |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1110 | } |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1111 | |
| 1112 | psx_create_err: |
| 1113 | cifs_buf_release(pSMB); |
| 1114 | |
Steve French | 65bc98b | 2009-07-10 15:27:25 +0000 | [diff] [blame] | 1115 | if (posix_flags & SMB_O_DIRECTORY) |
Pavel Shilovsky | 44c5818 | 2012-05-28 14:16:31 +0400 | [diff] [blame] | 1116 | cifs_stats_inc(&tcon->stats.cifs_stats.num_posixmkdirs); |
Steve French | 65bc98b | 2009-07-10 15:27:25 +0000 | [diff] [blame] | 1117 | else |
Pavel Shilovsky | 44c5818 | 2012-05-28 14:16:31 +0400 | [diff] [blame] | 1118 | cifs_stats_inc(&tcon->stats.cifs_stats.num_posixopens); |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1119 | |
| 1120 | if (rc == -EAGAIN) |
| 1121 | goto PsxCreat; |
| 1122 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 1123 | return rc; |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1124 | } |
| 1125 | |
Steve French | a9d02ad | 2005-08-24 23:06:05 -0700 | [diff] [blame] | 1126 | static __u16 convert_disposition(int disposition) |
| 1127 | { |
| 1128 | __u16 ofun = 0; |
| 1129 | |
| 1130 | switch (disposition) { |
| 1131 | case FILE_SUPERSEDE: |
| 1132 | ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC; |
| 1133 | break; |
| 1134 | case FILE_OPEN: |
| 1135 | ofun = SMBOPEN_OAPPEND; |
| 1136 | break; |
| 1137 | case FILE_CREATE: |
| 1138 | ofun = SMBOPEN_OCREATE; |
| 1139 | break; |
| 1140 | case FILE_OPEN_IF: |
| 1141 | ofun = SMBOPEN_OCREATE | SMBOPEN_OAPPEND; |
| 1142 | break; |
| 1143 | case FILE_OVERWRITE: |
| 1144 | ofun = SMBOPEN_OTRUNC; |
| 1145 | break; |
| 1146 | case FILE_OVERWRITE_IF: |
| 1147 | ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC; |
| 1148 | break; |
| 1149 | default: |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1150 | cifs_dbg(FYI, "unknown disposition %d\n", disposition); |
Steve French | a9d02ad | 2005-08-24 23:06:05 -0700 | [diff] [blame] | 1151 | ofun = SMBOPEN_OAPPEND; /* regular open */ |
| 1152 | } |
| 1153 | return ofun; |
| 1154 | } |
| 1155 | |
Jeff Layton | 35fc37d | 2008-05-14 10:22:03 -0700 | [diff] [blame] | 1156 | static int |
| 1157 | access_flags_to_smbopen_mode(const int access_flags) |
| 1158 | { |
| 1159 | int masked_flags = access_flags & (GENERIC_READ | GENERIC_WRITE); |
| 1160 | |
| 1161 | if (masked_flags == GENERIC_READ) |
| 1162 | return SMBOPEN_READ; |
| 1163 | else if (masked_flags == GENERIC_WRITE) |
| 1164 | return SMBOPEN_WRITE; |
| 1165 | |
| 1166 | /* just go for read/write */ |
| 1167 | return SMBOPEN_READWRITE; |
| 1168 | } |
| 1169 | |
Steve French | a9d02ad | 2005-08-24 23:06:05 -0700 | [diff] [blame] | 1170 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1171 | SMBLegacyOpen(const unsigned int xid, struct cifs_tcon *tcon, |
Steve French | a9d02ad | 2005-08-24 23:06:05 -0700 | [diff] [blame] | 1172 | const char *fileName, const int openDisposition, |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 1173 | const int access_flags, const int create_options, __u16 *netfid, |
| 1174 | int *pOplock, FILE_ALL_INFO *pfile_info, |
Steve French | a9d02ad | 2005-08-24 23:06:05 -0700 | [diff] [blame] | 1175 | const struct nls_table *nls_codepage, int remap) |
| 1176 | { |
| 1177 | int rc = -EACCES; |
| 1178 | OPENX_REQ *pSMB = NULL; |
| 1179 | OPENX_RSP *pSMBr = NULL; |
| 1180 | int bytes_returned; |
| 1181 | int name_len; |
| 1182 | __u16 count; |
| 1183 | |
| 1184 | OldOpenRetry: |
| 1185 | rc = smb_init(SMB_COM_OPEN_ANDX, 15, tcon, (void **) &pSMB, |
| 1186 | (void **) &pSMBr); |
| 1187 | if (rc) |
| 1188 | return rc; |
| 1189 | |
| 1190 | pSMB->AndXCommand = 0xFF; /* none */ |
| 1191 | |
| 1192 | if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) { |
| 1193 | count = 1; /* account for one byte pad to word boundary */ |
| 1194 | name_len = |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 1195 | cifsConvertToUTF16((__le16 *) (pSMB->fileName + 1), |
| 1196 | fileName, PATH_MAX, nls_codepage, remap); |
Steve French | a9d02ad | 2005-08-24 23:06:05 -0700 | [diff] [blame] | 1197 | name_len++; /* trailing null */ |
| 1198 | name_len *= 2; |
| 1199 | } else { /* BB improve check for buffer overruns BB */ |
| 1200 | count = 0; /* no pad */ |
| 1201 | name_len = strnlen(fileName, PATH_MAX); |
| 1202 | name_len++; /* trailing null */ |
| 1203 | strncpy(pSMB->fileName, fileName, name_len); |
| 1204 | } |
| 1205 | if (*pOplock & REQ_OPLOCK) |
| 1206 | pSMB->OpenFlags = cpu_to_le16(REQ_OPLOCK); |
Steve French | 26f5736 | 2007-08-30 22:09:15 +0000 | [diff] [blame] | 1207 | else if (*pOplock & REQ_BATCHOPLOCK) |
Steve French | a9d02ad | 2005-08-24 23:06:05 -0700 | [diff] [blame] | 1208 | pSMB->OpenFlags = cpu_to_le16(REQ_BATCHOPLOCK); |
Steve French | 26f5736 | 2007-08-30 22:09:15 +0000 | [diff] [blame] | 1209 | |
Steve French | a9d02ad | 2005-08-24 23:06:05 -0700 | [diff] [blame] | 1210 | pSMB->OpenFlags |= cpu_to_le16(REQ_MORE_INFO); |
Jeff Layton | 35fc37d | 2008-05-14 10:22:03 -0700 | [diff] [blame] | 1211 | pSMB->Mode = cpu_to_le16(access_flags_to_smbopen_mode(access_flags)); |
Steve French | a9d02ad | 2005-08-24 23:06:05 -0700 | [diff] [blame] | 1212 | pSMB->Mode |= cpu_to_le16(0x40); /* deny none */ |
| 1213 | /* set file as system file if special file such |
| 1214 | as fifo and server expecting SFU style and |
| 1215 | no Unix extensions */ |
| 1216 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1217 | if (create_options & CREATE_OPTION_SPECIAL) |
| 1218 | pSMB->FileAttributes = cpu_to_le16(ATTR_SYSTEM); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 1219 | else /* BB FIXME BB */ |
| 1220 | pSMB->FileAttributes = cpu_to_le16(0/*ATTR_NORMAL*/); |
Steve French | a9d02ad | 2005-08-24 23:06:05 -0700 | [diff] [blame] | 1221 | |
Jeff Layton | 67750fb | 2008-05-09 22:28:02 +0000 | [diff] [blame] | 1222 | if (create_options & CREATE_OPTION_READONLY) |
| 1223 | pSMB->FileAttributes |= cpu_to_le16(ATTR_READONLY); |
Steve French | a9d02ad | 2005-08-24 23:06:05 -0700 | [diff] [blame] | 1224 | |
| 1225 | /* BB FIXME BB */ |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 1226 | /* pSMB->CreateOptions = cpu_to_le32(create_options & |
| 1227 | CREATE_OPTIONS_MASK); */ |
Steve French | a9d02ad | 2005-08-24 23:06:05 -0700 | [diff] [blame] | 1228 | /* BB FIXME END BB */ |
Steve French | 3e87d80 | 2005-09-18 20:49:21 -0700 | [diff] [blame] | 1229 | |
| 1230 | pSMB->Sattr = cpu_to_le16(ATTR_HIDDEN | ATTR_SYSTEM | ATTR_DIRECTORY); |
Steve French | 70ca734 | 2005-09-22 16:32:06 -0700 | [diff] [blame] | 1231 | pSMB->OpenFunction = cpu_to_le16(convert_disposition(openDisposition)); |
Steve French | a9d02ad | 2005-08-24 23:06:05 -0700 | [diff] [blame] | 1232 | count += name_len; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 1233 | inc_rfc1001_len(pSMB, count); |
Steve French | a9d02ad | 2005-08-24 23:06:05 -0700 | [diff] [blame] | 1234 | |
| 1235 | pSMB->ByteCount = cpu_to_le16(count); |
Steve French | a9d02ad | 2005-08-24 23:06:05 -0700 | [diff] [blame] | 1236 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
Jeff Layton | 7749981 | 2011-01-11 07:24:23 -0500 | [diff] [blame] | 1237 | (struct smb_hdr *)pSMBr, &bytes_returned, 0); |
Pavel Shilovsky | 44c5818 | 2012-05-28 14:16:31 +0400 | [diff] [blame] | 1238 | cifs_stats_inc(&tcon->stats.cifs_stats.num_opens); |
Steve French | a9d02ad | 2005-08-24 23:06:05 -0700 | [diff] [blame] | 1239 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1240 | cifs_dbg(FYI, "Error in Open = %d\n", rc); |
Steve French | a9d02ad | 2005-08-24 23:06:05 -0700 | [diff] [blame] | 1241 | } else { |
| 1242 | /* BB verify if wct == 15 */ |
| 1243 | |
Steve French | 582d21e | 2008-05-13 04:54:12 +0000 | [diff] [blame] | 1244 | /* *pOplock = pSMBr->OplockLevel; */ /* BB take from action field*/ |
Steve French | a9d02ad | 2005-08-24 23:06:05 -0700 | [diff] [blame] | 1245 | |
| 1246 | *netfid = pSMBr->Fid; /* cifs fid stays in le */ |
| 1247 | /* Let caller know file was created so we can set the mode. */ |
| 1248 | /* Do we care about the CreateAction in any other cases? */ |
| 1249 | /* BB FIXME BB */ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1250 | /* if (cpu_to_le32(FILE_CREATE) == pSMBr->CreateAction) |
Steve French | a9d02ad | 2005-08-24 23:06:05 -0700 | [diff] [blame] | 1251 | *pOplock |= CIFS_CREATE_ACTION; */ |
| 1252 | /* BB FIXME END */ |
| 1253 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1254 | if (pfile_info) { |
Steve French | a9d02ad | 2005-08-24 23:06:05 -0700 | [diff] [blame] | 1255 | pfile_info->CreationTime = 0; /* BB convert CreateTime*/ |
| 1256 | pfile_info->LastAccessTime = 0; /* BB fixme */ |
| 1257 | pfile_info->LastWriteTime = 0; /* BB fixme */ |
| 1258 | pfile_info->ChangeTime = 0; /* BB fixme */ |
Steve French | 70ca734 | 2005-09-22 16:32:06 -0700 | [diff] [blame] | 1259 | pfile_info->Attributes = |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 1260 | cpu_to_le32(le16_to_cpu(pSMBr->FileAttributes)); |
Steve French | a9d02ad | 2005-08-24 23:06:05 -0700 | [diff] [blame] | 1261 | /* the file_info buf is endian converted by caller */ |
Steve French | 70ca734 | 2005-09-22 16:32:06 -0700 | [diff] [blame] | 1262 | pfile_info->AllocationSize = |
| 1263 | cpu_to_le64(le32_to_cpu(pSMBr->EndOfFile)); |
| 1264 | pfile_info->EndOfFile = pfile_info->AllocationSize; |
Steve French | a9d02ad | 2005-08-24 23:06:05 -0700 | [diff] [blame] | 1265 | pfile_info->NumberOfLinks = cpu_to_le32(1); |
Jeff Layton | 9a8165f | 2008-10-17 21:03:20 -0400 | [diff] [blame] | 1266 | pfile_info->DeletePending = 0; |
Steve French | a9d02ad | 2005-08-24 23:06:05 -0700 | [diff] [blame] | 1267 | } |
| 1268 | } |
| 1269 | |
| 1270 | cifs_buf_release(pSMB); |
| 1271 | if (rc == -EAGAIN) |
| 1272 | goto OldOpenRetry; |
| 1273 | return rc; |
| 1274 | } |
| 1275 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1276 | int |
Pavel Shilovsky | d81b8a4 | 2014-01-16 15:53:36 +0400 | [diff] [blame] | 1277 | CIFS_open(const unsigned int xid, struct cifs_open_parms *oparms, int *oplock, |
| 1278 | FILE_ALL_INFO *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1279 | { |
| 1280 | int rc = -EACCES; |
Pavel Shilovsky | 9bf4fa0 | 2014-01-16 15:53:33 +0400 | [diff] [blame] | 1281 | OPEN_REQ *req = NULL; |
| 1282 | OPEN_RSP *rsp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | int bytes_returned; |
| 1284 | int name_len; |
| 1285 | __u16 count; |
Pavel Shilovsky | d81b8a4 | 2014-01-16 15:53:36 +0400 | [diff] [blame] | 1286 | struct cifs_sb_info *cifs_sb = oparms->cifs_sb; |
| 1287 | struct cifs_tcon *tcon = oparms->tcon; |
Steve French | 2baa268 | 2014-09-27 02:19:01 -0500 | [diff] [blame] | 1288 | int remap = cifs_remap(cifs_sb); |
Pavel Shilovsky | d81b8a4 | 2014-01-16 15:53:36 +0400 | [diff] [blame] | 1289 | const struct nls_table *nls = cifs_sb->local_nls; |
| 1290 | int create_options = oparms->create_options; |
| 1291 | int desired_access = oparms->desired_access; |
| 1292 | int disposition = oparms->disposition; |
| 1293 | const char *path = oparms->path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1294 | |
| 1295 | openRetry: |
Pavel Shilovsky | 9bf4fa0 | 2014-01-16 15:53:33 +0400 | [diff] [blame] | 1296 | rc = smb_init(SMB_COM_NT_CREATE_ANDX, 24, tcon, (void **)&req, |
| 1297 | (void **)&rsp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1298 | if (rc) |
| 1299 | return rc; |
| 1300 | |
Pavel Shilovsky | 9bf4fa0 | 2014-01-16 15:53:33 +0400 | [diff] [blame] | 1301 | /* no commands go after this */ |
| 1302 | req->AndXCommand = 0xFF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1303 | |
Pavel Shilovsky | 9bf4fa0 | 2014-01-16 15:53:33 +0400 | [diff] [blame] | 1304 | if (req->hdr.Flags2 & SMBFLG2_UNICODE) { |
| 1305 | /* account for one byte pad to word boundary */ |
| 1306 | count = 1; |
| 1307 | name_len = cifsConvertToUTF16((__le16 *)(req->fileName + 1), |
| 1308 | path, PATH_MAX, nls, remap); |
| 1309 | /* trailing null */ |
| 1310 | name_len++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1311 | name_len *= 2; |
Pavel Shilovsky | 9bf4fa0 | 2014-01-16 15:53:33 +0400 | [diff] [blame] | 1312 | req->NameLength = cpu_to_le16(name_len); |
| 1313 | } else { |
| 1314 | /* BB improve check for buffer overruns BB */ |
| 1315 | /* no pad */ |
| 1316 | count = 0; |
| 1317 | name_len = strnlen(path, PATH_MAX); |
| 1318 | /* trailing null */ |
| 1319 | name_len++; |
| 1320 | req->NameLength = cpu_to_le16(name_len); |
| 1321 | strncpy(req->fileName, path, name_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | } |
Jeff Layton | 67750fb | 2008-05-09 22:28:02 +0000 | [diff] [blame] | 1323 | |
Pavel Shilovsky | 9bf4fa0 | 2014-01-16 15:53:33 +0400 | [diff] [blame] | 1324 | if (*oplock & REQ_OPLOCK) |
| 1325 | req->OpenFlags = cpu_to_le32(REQ_OPLOCK); |
| 1326 | else if (*oplock & REQ_BATCHOPLOCK) |
| 1327 | req->OpenFlags = cpu_to_le32(REQ_BATCHOPLOCK); |
| 1328 | |
| 1329 | req->DesiredAccess = cpu_to_le32(desired_access); |
| 1330 | req->AllocationSize = 0; |
| 1331 | |
| 1332 | /* |
| 1333 | * Set file as system file if special file such as fifo and server |
| 1334 | * expecting SFU style and no Unix extensions. |
| 1335 | */ |
| 1336 | if (create_options & CREATE_OPTION_SPECIAL) |
| 1337 | req->FileAttributes = cpu_to_le32(ATTR_SYSTEM); |
| 1338 | else |
| 1339 | req->FileAttributes = cpu_to_le32(ATTR_NORMAL); |
| 1340 | |
| 1341 | /* |
| 1342 | * XP does not handle ATTR_POSIX_SEMANTICS but it helps speed up case |
| 1343 | * sensitive checks for other servers such as Samba. |
| 1344 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1345 | if (tcon->ses->capabilities & CAP_UNIX) |
Pavel Shilovsky | 9bf4fa0 | 2014-01-16 15:53:33 +0400 | [diff] [blame] | 1346 | req->FileAttributes |= cpu_to_le32(ATTR_POSIX_SEMANTICS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | |
Jeff Layton | 67750fb | 2008-05-09 22:28:02 +0000 | [diff] [blame] | 1348 | if (create_options & CREATE_OPTION_READONLY) |
Pavel Shilovsky | 9bf4fa0 | 2014-01-16 15:53:33 +0400 | [diff] [blame] | 1349 | req->FileAttributes |= cpu_to_le32(ATTR_READONLY); |
Jeff Layton | 67750fb | 2008-05-09 22:28:02 +0000 | [diff] [blame] | 1350 | |
Pavel Shilovsky | 9bf4fa0 | 2014-01-16 15:53:33 +0400 | [diff] [blame] | 1351 | req->ShareAccess = cpu_to_le32(FILE_SHARE_ALL); |
| 1352 | req->CreateDisposition = cpu_to_le32(disposition); |
| 1353 | req->CreateOptions = cpu_to_le32(create_options & CREATE_OPTIONS_MASK); |
| 1354 | |
Steve French | 09d1db5 | 2005-04-28 22:41:08 -0700 | [diff] [blame] | 1355 | /* BB Expirement with various impersonation levels and verify */ |
Pavel Shilovsky | 9bf4fa0 | 2014-01-16 15:53:33 +0400 | [diff] [blame] | 1356 | req->ImpersonationLevel = cpu_to_le32(SECURITY_IMPERSONATION); |
| 1357 | req->SecurityFlags = SECURITY_CONTEXT_TRACKING|SECURITY_EFFECTIVE_ONLY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1358 | |
| 1359 | count += name_len; |
Pavel Shilovsky | 9bf4fa0 | 2014-01-16 15:53:33 +0400 | [diff] [blame] | 1360 | inc_rfc1001_len(req, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1361 | |
Pavel Shilovsky | 9bf4fa0 | 2014-01-16 15:53:33 +0400 | [diff] [blame] | 1362 | req->ByteCount = cpu_to_le16(count); |
| 1363 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *)req, |
| 1364 | (struct smb_hdr *)rsp, &bytes_returned, 0); |
Pavel Shilovsky | 44c5818 | 2012-05-28 14:16:31 +0400 | [diff] [blame] | 1365 | cifs_stats_inc(&tcon->stats.cifs_stats.num_opens); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1367 | cifs_dbg(FYI, "Error in Open = %d\n", rc); |
Pavel Shilovsky | 9bf4fa0 | 2014-01-16 15:53:33 +0400 | [diff] [blame] | 1368 | cifs_buf_release(req); |
| 1369 | if (rc == -EAGAIN) |
| 1370 | goto openRetry; |
| 1371 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1372 | } |
Steve French | a5a2b48 | 2005-08-20 21:42:53 -0700 | [diff] [blame] | 1373 | |
Pavel Shilovsky | 9bf4fa0 | 2014-01-16 15:53:33 +0400 | [diff] [blame] | 1374 | /* 1 byte no need to le_to_cpu */ |
| 1375 | *oplock = rsp->OplockLevel; |
| 1376 | /* cifs fid stays in le */ |
Pavel Shilovsky | d81b8a4 | 2014-01-16 15:53:36 +0400 | [diff] [blame] | 1377 | oparms->fid->netfid = rsp->Fid; |
Pavel Shilovsky | 9bf4fa0 | 2014-01-16 15:53:33 +0400 | [diff] [blame] | 1378 | |
| 1379 | /* Let caller know file was created so we can set the mode. */ |
| 1380 | /* Do we care about the CreateAction in any other cases? */ |
| 1381 | if (cpu_to_le32(FILE_CREATE) == rsp->CreateAction) |
| 1382 | *oplock |= CIFS_CREATE_ACTION; |
| 1383 | |
| 1384 | if (buf) { |
| 1385 | /* copy from CreationTime to Attributes */ |
| 1386 | memcpy((char *)buf, (char *)&rsp->CreationTime, 36); |
| 1387 | /* the file_info buf is endian converted by caller */ |
| 1388 | buf->AllocationSize = rsp->AllocationSize; |
| 1389 | buf->EndOfFile = rsp->EndOfFile; |
| 1390 | buf->NumberOfLinks = cpu_to_le32(1); |
| 1391 | buf->DeletePending = 0; |
| 1392 | } |
| 1393 | |
| 1394 | cifs_buf_release(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | return rc; |
| 1396 | } |
| 1397 | |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1398 | /* |
| 1399 | * Discard any remaining data in the current SMB. To do this, we borrow the |
| 1400 | * current bigbuf. |
| 1401 | */ |
Pavel Shilovsky | c42a6ab | 2016-11-17 16:20:23 -0800 | [diff] [blame] | 1402 | int |
| 1403 | cifs_discard_remaining_data(struct TCP_Server_Info *server) |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1404 | { |
Pavel Shilovsky | 5ffef7b | 2012-03-23 14:28:03 -0400 | [diff] [blame] | 1405 | unsigned int rfclen = get_rfc1002_length(server->smallbuf); |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1406 | int remaining = rfclen + 4 - server->total_read; |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 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 | |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1420 | return 0; |
| 1421 | } |
| 1422 | |
Pavel Shilovsky | 6cc3b24 | 2016-02-27 11:58:18 +0300 | [diff] [blame] | 1423 | static int |
| 1424 | cifs_readv_discard(struct TCP_Server_Info *server, struct mid_q_entry *mid) |
| 1425 | { |
| 1426 | int length; |
| 1427 | struct cifs_readdata *rdata = mid->callback_data; |
| 1428 | |
Pavel Shilovsky | c42a6ab | 2016-11-17 16:20:23 -0800 | [diff] [blame] | 1429 | length = cifs_discard_remaining_data(server); |
Pavel Shilovsky | 6cc3b24 | 2016-02-27 11:58:18 +0300 | [diff] [blame] | 1430 | dequeue_mid(mid, rdata->result); |
| 1431 | return length; |
| 1432 | } |
| 1433 | |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 1434 | int |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1435 | cifs_readv_receive(struct TCP_Server_Info *server, struct mid_q_entry *mid) |
| 1436 | { |
| 1437 | int length, len; |
Jeff Layton | 8d5ce4d | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 1438 | unsigned int data_offset, data_len; |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1439 | struct cifs_readdata *rdata = mid->callback_data; |
Pavel Shilovsky | 5ffef7b | 2012-03-23 14:28:03 -0400 | [diff] [blame] | 1440 | char *buf = server->smallbuf; |
| 1441 | unsigned int buflen = get_rfc1002_length(buf) + 4; |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1442 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1443 | cifs_dbg(FYI, "%s: mid=%llu offset=%llu bytes=%u\n", |
| 1444 | __func__, mid->mid, rdata->offset, rdata->bytes); |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1445 | |
| 1446 | /* |
| 1447 | * read the rest of READ_RSP header (sans Data array), or whatever we |
| 1448 | * can if there's not enough data. At this point, we've read down to |
| 1449 | * the Mid. |
| 1450 | */ |
Pavel Shilovsky | eb37871 | 2012-05-17 13:02:51 +0400 | [diff] [blame] | 1451 | len = min_t(unsigned int, buflen, server->vals->read_rsp_size) - |
Pavel Shilovsky | 1887f60 | 2012-05-17 12:45:31 +0400 | [diff] [blame] | 1452 | HEADER_SIZE(server) + 1; |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1453 | |
Al Viro | a613730 | 2016-01-09 19:37:16 -0500 | [diff] [blame] | 1454 | length = cifs_read_from_socket(server, |
| 1455 | buf + HEADER_SIZE(server) - 1, len); |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1456 | if (length < 0) |
| 1457 | return length; |
| 1458 | server->total_read += length; |
| 1459 | |
Pavel Shilovsky | 6cc3b24 | 2016-02-27 11:58:18 +0300 | [diff] [blame] | 1460 | if (server->ops->is_status_pending && |
| 1461 | server->ops->is_status_pending(buf, server, 0)) { |
Pavel Shilovsky | c42a6ab | 2016-11-17 16:20:23 -0800 | [diff] [blame] | 1462 | cifs_discard_remaining_data(server); |
Pavel Shilovsky | 6cc3b24 | 2016-02-27 11:58:18 +0300 | [diff] [blame] | 1463 | return -1; |
| 1464 | } |
| 1465 | |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1466 | /* Was the SMB read successful? */ |
Pavel Shilovsky | eb37871 | 2012-05-17 13:02:51 +0400 | [diff] [blame] | 1467 | rdata->result = server->ops->map_error(buf, false); |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1468 | if (rdata->result != 0) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1469 | cifs_dbg(FYI, "%s: server returned error %d\n", |
| 1470 | __func__, rdata->result); |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1471 | return cifs_readv_discard(server, mid); |
| 1472 | } |
| 1473 | |
| 1474 | /* 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] | 1475 | if (server->total_read < server->vals->read_rsp_size) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1476 | cifs_dbg(FYI, "%s: server returned short header. got=%u expected=%zu\n", |
| 1477 | __func__, server->total_read, |
| 1478 | server->vals->read_rsp_size); |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1479 | rdata->result = -EIO; |
| 1480 | return cifs_readv_discard(server, mid); |
| 1481 | } |
| 1482 | |
Pavel Shilovsky | eb37871 | 2012-05-17 13:02:51 +0400 | [diff] [blame] | 1483 | data_offset = server->ops->read_data_offset(buf) + 4; |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1484 | if (data_offset < server->total_read) { |
| 1485 | /* |
| 1486 | * win2k8 sometimes sends an offset of 0 when the read |
| 1487 | * is beyond the EOF. Treat it as if the data starts just after |
| 1488 | * the header. |
| 1489 | */ |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1490 | cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n", |
| 1491 | __func__, data_offset); |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1492 | data_offset = server->total_read; |
| 1493 | } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) { |
| 1494 | /* data_offset is beyond the end of smallbuf */ |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1495 | cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n", |
| 1496 | __func__, data_offset); |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1497 | rdata->result = -EIO; |
| 1498 | return cifs_readv_discard(server, mid); |
| 1499 | } |
| 1500 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1501 | cifs_dbg(FYI, "%s: total_read=%u data_offset=%u\n", |
| 1502 | __func__, server->total_read, data_offset); |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1503 | |
| 1504 | len = data_offset - server->total_read; |
| 1505 | if (len > 0) { |
| 1506 | /* read any junk before data into the rest of smallbuf */ |
Al Viro | a613730 | 2016-01-09 19:37:16 -0500 | [diff] [blame] | 1507 | length = cifs_read_from_socket(server, |
| 1508 | buf + server->total_read, len); |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1509 | if (length < 0) |
| 1510 | return length; |
| 1511 | server->total_read += length; |
| 1512 | } |
| 1513 | |
| 1514 | /* set up first iov for signature check */ |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 1515 | rdata->iov[0].iov_base = buf; |
| 1516 | rdata->iov[0].iov_len = 4; |
| 1517 | rdata->iov[1].iov_base = buf + 4; |
| 1518 | rdata->iov[1].iov_len = server->total_read - 4; |
| 1519 | cifs_dbg(FYI, "0: iov_base=%p iov_len=%u\n", |
| 1520 | rdata->iov[0].iov_base, server->total_read); |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1521 | |
| 1522 | /* how much data is in the response? */ |
Pavel Shilovsky | eb37871 | 2012-05-17 13:02:51 +0400 | [diff] [blame] | 1523 | data_len = server->ops->read_data_length(buf); |
Pavel Shilovsky | 5ffef7b | 2012-03-23 14:28:03 -0400 | [diff] [blame] | 1524 | if (data_offset + data_len > buflen) { |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1525 | /* data_len is corrupt -- discard frame */ |
| 1526 | rdata->result = -EIO; |
| 1527 | return cifs_readv_discard(server, mid); |
| 1528 | } |
| 1529 | |
Jeff Layton | 8321fec | 2012-09-19 06:22:32 -0700 | [diff] [blame] | 1530 | length = rdata->read_into_pages(server, rdata, data_len); |
| 1531 | if (length < 0) |
| 1532 | return length; |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1533 | |
Jeff Layton | 8321fec | 2012-09-19 06:22:32 -0700 | [diff] [blame] | 1534 | server->total_read += length; |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1535 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1536 | cifs_dbg(FYI, "total_read=%u buflen=%u remaining=%u\n", |
| 1537 | server->total_read, buflen, data_len); |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1538 | |
| 1539 | /* discard anything left over */ |
Pavel Shilovsky | 5ffef7b | 2012-03-23 14:28:03 -0400 | [diff] [blame] | 1540 | if (server->total_read < buflen) |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1541 | return cifs_readv_discard(server, mid); |
| 1542 | |
| 1543 | dequeue_mid(mid, false); |
| 1544 | return length; |
| 1545 | } |
| 1546 | |
| 1547 | static void |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1548 | cifs_readv_callback(struct mid_q_entry *mid) |
| 1549 | { |
| 1550 | struct cifs_readdata *rdata = mid->callback_data; |
| 1551 | struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink); |
| 1552 | struct TCP_Server_Info *server = tcon->ses->server; |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 1553 | struct smb_rqst rqst = { .rq_iov = rdata->iov, |
| 1554 | .rq_nvec = 2, |
Jeff Layton | 8321fec | 2012-09-19 06:22:32 -0700 | [diff] [blame] | 1555 | .rq_pages = rdata->pages, |
| 1556 | .rq_npages = rdata->nr_pages, |
| 1557 | .rq_pagesz = rdata->pagesz, |
| 1558 | .rq_tailsz = rdata->tailsz }; |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1559 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1560 | cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n", |
| 1561 | __func__, mid->mid, mid->mid_state, rdata->result, |
| 1562 | rdata->bytes); |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1563 | |
Pavel Shilovsky | 7c9421e | 2012-03-23 14:28:03 -0400 | [diff] [blame] | 1564 | switch (mid->mid_state) { |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1565 | case MID_RESPONSE_RECEIVED: |
| 1566 | /* result already set, check signature */ |
Jeff Layton | 38d77c5 | 2013-05-26 07:01:00 -0400 | [diff] [blame] | 1567 | if (server->sign) { |
Steve French | 985e4ff0 | 2012-08-03 09:42:45 -0500 | [diff] [blame] | 1568 | int rc = 0; |
| 1569 | |
Jeff Layton | bf5ea0e | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 1570 | rc = cifs_verify_signature(&rqst, server, |
Jeff Layton | 0124cc4 | 2013-04-03 11:55:03 -0400 | [diff] [blame] | 1571 | mid->sequence_number); |
Steve French | 985e4ff0 | 2012-08-03 09:42:45 -0500 | [diff] [blame] | 1572 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1573 | cifs_dbg(VFS, "SMB signature verification returned error = %d\n", |
| 1574 | rc); |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1575 | } |
| 1576 | /* FIXME: should this be counted toward the initiating task? */ |
Pavel Shilovsky | 34a54d6 | 2014-07-10 10:03:29 +0400 | [diff] [blame] | 1577 | task_io_account_read(rdata->got_bytes); |
| 1578 | cifs_stats_bytes_read(tcon, rdata->got_bytes); |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1579 | break; |
| 1580 | case MID_REQUEST_SUBMITTED: |
| 1581 | case MID_RETRY_NEEDED: |
| 1582 | rdata->result = -EAGAIN; |
Pavel Shilovsky | d913ed1 | 2014-07-10 11:31:48 +0400 | [diff] [blame] | 1583 | if (server->sign && rdata->got_bytes) |
| 1584 | /* reset bytes number since we can not check a sign */ |
| 1585 | rdata->got_bytes = 0; |
| 1586 | /* FIXME: should this be counted toward the initiating task? */ |
| 1587 | task_io_account_read(rdata->got_bytes); |
| 1588 | cifs_stats_bytes_read(tcon, rdata->got_bytes); |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1589 | break; |
| 1590 | default: |
| 1591 | rdata->result = -EIO; |
| 1592 | } |
| 1593 | |
Jeff Layton | da472fc | 2012-03-23 14:40:53 -0400 | [diff] [blame] | 1594 | queue_work(cifsiod_wq, &rdata->work); |
Christopher Oo | 5fb4e28 | 2015-06-25 16:10:48 -0700 | [diff] [blame] | 1595 | mutex_lock(&server->srv_mutex); |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1596 | DeleteMidQEntry(mid); |
Christopher Oo | 5fb4e28 | 2015-06-25 16:10:48 -0700 | [diff] [blame] | 1597 | mutex_unlock(&server->srv_mutex); |
Pavel Shilovsky | a891f0f | 2012-05-23 16:14:34 +0400 | [diff] [blame] | 1598 | add_credits(server, 1, 0); |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1599 | } |
| 1600 | |
| 1601 | /* cifs_async_readv - send an async write, and set up mid to handle result */ |
| 1602 | int |
| 1603 | cifs_async_readv(struct cifs_readdata *rdata) |
| 1604 | { |
| 1605 | int rc; |
| 1606 | READ_REQ *smb = NULL; |
| 1607 | int wct; |
| 1608 | struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink); |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 1609 | struct smb_rqst rqst = { .rq_iov = rdata->iov, |
| 1610 | .rq_nvec = 2 }; |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1611 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1612 | cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n", |
| 1613 | __func__, rdata->offset, rdata->bytes); |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1614 | |
| 1615 | if (tcon->ses->capabilities & CAP_LARGE_FILES) |
| 1616 | wct = 12; |
| 1617 | else { |
| 1618 | wct = 10; /* old style read */ |
| 1619 | if ((rdata->offset >> 32) > 0) { |
| 1620 | /* can not handle this big offset for old */ |
| 1621 | return -EIO; |
| 1622 | } |
| 1623 | } |
| 1624 | |
| 1625 | rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **)&smb); |
| 1626 | if (rc) |
| 1627 | return rc; |
| 1628 | |
| 1629 | smb->hdr.Pid = cpu_to_le16((__u16)rdata->pid); |
| 1630 | smb->hdr.PidHigh = cpu_to_le16((__u16)(rdata->pid >> 16)); |
| 1631 | |
| 1632 | smb->AndXCommand = 0xFF; /* none */ |
Pavel Shilovsky | 4b4de76 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 1633 | smb->Fid = rdata->cfile->fid.netfid; |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1634 | smb->OffsetLow = cpu_to_le32(rdata->offset & 0xFFFFFFFF); |
| 1635 | if (wct == 12) |
| 1636 | smb->OffsetHigh = cpu_to_le32(rdata->offset >> 32); |
| 1637 | smb->Remaining = 0; |
| 1638 | smb->MaxCount = cpu_to_le16(rdata->bytes & 0xFFFF); |
| 1639 | smb->MaxCountHigh = cpu_to_le32(rdata->bytes >> 16); |
| 1640 | if (wct == 12) |
| 1641 | smb->ByteCount = 0; |
| 1642 | else { |
| 1643 | /* old style read */ |
| 1644 | struct smb_com_readx_req *smbr = |
| 1645 | (struct smb_com_readx_req *)smb; |
| 1646 | smbr->ByteCount = 0; |
| 1647 | } |
| 1648 | |
| 1649 | /* 4 for RFC1001 length + 1 for BCC */ |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 1650 | rdata->iov[0].iov_base = smb; |
| 1651 | rdata->iov[0].iov_len = 4; |
| 1652 | rdata->iov[1].iov_base = (char *)smb + 4; |
| 1653 | rdata->iov[1].iov_len = get_rfc1002_length(smb); |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1654 | |
Jeff Layton | 6993f74 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 1655 | kref_get(&rdata->refcount); |
Jeff Layton | fec344e | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 1656 | rc = cifs_call_async(tcon->ses->server, &rqst, cifs_readv_receive, |
Pavel Shilovsky | 9b7c18a | 2016-11-16 14:06:17 -0800 | [diff] [blame] | 1657 | cifs_readv_callback, NULL, rdata, 0); |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1658 | |
| 1659 | if (rc == 0) |
Pavel Shilovsky | 44c5818 | 2012-05-28 14:16:31 +0400 | [diff] [blame] | 1660 | cifs_stats_inc(&tcon->stats.cifs_stats.num_reads); |
Jeff Layton | 6993f74 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 1661 | else |
| 1662 | kref_put(&rdata->refcount, cifs_readdata_release); |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1663 | |
| 1664 | cifs_small_buf_release(smb); |
| 1665 | return rc; |
| 1666 | } |
| 1667 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1668 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1669 | CIFSSMBRead(const unsigned int xid, struct cifs_io_parms *io_parms, |
| 1670 | unsigned int *nbytes, char **buf, int *pbuf_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1671 | { |
| 1672 | int rc = -EACCES; |
| 1673 | READ_REQ *pSMB = NULL; |
| 1674 | READ_RSP *pSMBr = NULL; |
| 1675 | char *pReadData = NULL; |
Steve French | bfa0d75 | 2005-08-31 21:50:37 -0700 | [diff] [blame] | 1676 | int wct; |
Steve French | ec637e3 | 2005-12-12 20:53:18 -0800 | [diff] [blame] | 1677 | int resp_buf_type = 0; |
| 1678 | struct kvec iov[1]; |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 1679 | struct kvec rsp_iov; |
Pavel Shilovsky | d4ffff1 | 2011-05-26 06:02:00 +0000 | [diff] [blame] | 1680 | __u32 pid = io_parms->pid; |
| 1681 | __u16 netfid = io_parms->netfid; |
| 1682 | __u64 offset = io_parms->offset; |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 1683 | struct cifs_tcon *tcon = io_parms->tcon; |
Pavel Shilovsky | d4ffff1 | 2011-05-26 06:02:00 +0000 | [diff] [blame] | 1684 | unsigned int count = io_parms->length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1685 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1686 | cifs_dbg(FYI, "Reading %d bytes on fid %d\n", count, netfid); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1687 | if (tcon->ses->capabilities & CAP_LARGE_FILES) |
Steve French | bfa0d75 | 2005-08-31 21:50:37 -0700 | [diff] [blame] | 1688 | wct = 12; |
Steve French | 4c3130e | 2008-12-09 00:28:16 +0000 | [diff] [blame] | 1689 | else { |
Steve French | bfa0d75 | 2005-08-31 21:50:37 -0700 | [diff] [blame] | 1690 | wct = 10; /* old style read */ |
Pavel Shilovsky | d4ffff1 | 2011-05-26 06:02:00 +0000 | [diff] [blame] | 1691 | if ((offset >> 32) > 0) { |
Steve French | 4c3130e | 2008-12-09 00:28:16 +0000 | [diff] [blame] | 1692 | /* can not handle this big offset for old */ |
| 1693 | return -EIO; |
| 1694 | } |
| 1695 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1696 | |
| 1697 | *nbytes = 0; |
Steve French | ec637e3 | 2005-12-12 20:53:18 -0800 | [diff] [blame] | 1698 | rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **) &pSMB); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1699 | if (rc) |
| 1700 | return rc; |
| 1701 | |
Pavel Shilovsky | d4ffff1 | 2011-05-26 06:02:00 +0000 | [diff] [blame] | 1702 | pSMB->hdr.Pid = cpu_to_le16((__u16)pid); |
| 1703 | pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16)); |
| 1704 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1705 | /* tcon and ses pointer are checked in smb_init */ |
| 1706 | if (tcon->ses->server == NULL) |
| 1707 | return -ECONNABORTED; |
| 1708 | |
Steve French | ec637e3 | 2005-12-12 20:53:18 -0800 | [diff] [blame] | 1709 | pSMB->AndXCommand = 0xFF; /* none */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1710 | pSMB->Fid = netfid; |
Pavel Shilovsky | d4ffff1 | 2011-05-26 06:02:00 +0000 | [diff] [blame] | 1711 | pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1712 | if (wct == 12) |
Pavel Shilovsky | d4ffff1 | 2011-05-26 06:02:00 +0000 | [diff] [blame] | 1713 | pSMB->OffsetHigh = cpu_to_le32(offset >> 32); |
Steve French | bfa0d75 | 2005-08-31 21:50:37 -0700 | [diff] [blame] | 1714 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1715 | pSMB->Remaining = 0; |
| 1716 | pSMB->MaxCount = cpu_to_le16(count & 0xFFFF); |
| 1717 | pSMB->MaxCountHigh = cpu_to_le32(count >> 16); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1718 | if (wct == 12) |
Steve French | bfa0d75 | 2005-08-31 21:50:37 -0700 | [diff] [blame] | 1719 | pSMB->ByteCount = 0; /* no need to do le conversion since 0 */ |
| 1720 | else { |
| 1721 | /* old style read */ |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 1722 | struct smb_com_readx_req *pSMBW = |
Steve French | bfa0d75 | 2005-08-31 21:50:37 -0700 | [diff] [blame] | 1723 | (struct smb_com_readx_req *)pSMB; |
Steve French | ec637e3 | 2005-12-12 20:53:18 -0800 | [diff] [blame] | 1724 | pSMBW->ByteCount = 0; |
Steve French | bfa0d75 | 2005-08-31 21:50:37 -0700 | [diff] [blame] | 1725 | } |
Steve French | ec637e3 | 2005-12-12 20:53:18 -0800 | [diff] [blame] | 1726 | |
| 1727 | iov[0].iov_base = (char *)pSMB; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 1728 | iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4; |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 1729 | rc = SendReceive2(xid, tcon->ses, iov, 1, &resp_buf_type, |
| 1730 | CIFS_LOG_ERROR, &rsp_iov); |
| 1731 | cifs_small_buf_release(pSMB); |
Pavel Shilovsky | 44c5818 | 2012-05-28 14:16:31 +0400 | [diff] [blame] | 1732 | cifs_stats_inc(&tcon->stats.cifs_stats.num_reads); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 1733 | pSMBr = (READ_RSP *)rsp_iov.iov_base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1734 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1735 | cifs_dbg(VFS, "Send error in read = %d\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1736 | } else { |
| 1737 | int data_length = le16_to_cpu(pSMBr->DataLengthHigh); |
| 1738 | data_length = data_length << 16; |
| 1739 | data_length += le16_to_cpu(pSMBr->DataLength); |
| 1740 | *nbytes = data_length; |
| 1741 | |
| 1742 | /*check that DataLength would not go beyond end of SMB */ |
Steve French | ec637e3 | 2005-12-12 20:53:18 -0800 | [diff] [blame] | 1743 | if ((data_length > CIFSMaxBufSize) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1744 | || (data_length > count)) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1745 | cifs_dbg(FYI, "bad length %d for count %d\n", |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 1746 | data_length, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1747 | rc = -EIO; |
| 1748 | *nbytes = 0; |
| 1749 | } else { |
Steve French | ec637e3 | 2005-12-12 20:53:18 -0800 | [diff] [blame] | 1750 | pReadData = (char *) (&pSMBr->hdr.Protocol) + |
Steve French | 26f5736 | 2007-08-30 22:09:15 +0000 | [diff] [blame] | 1751 | le16_to_cpu(pSMBr->DataOffset); |
| 1752 | /* if (rc = copy_to_user(buf, pReadData, data_length)) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1753 | cifs_dbg(VFS, "Faulting on read rc = %d\n",rc); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 1754 | rc = -EFAULT; |
Steve French | 26f5736 | 2007-08-30 22:09:15 +0000 | [diff] [blame] | 1755 | }*/ /* can not use copy_to_user when using page cache*/ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1756 | if (*buf) |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 1757 | memcpy(*buf, pReadData, data_length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1758 | } |
| 1759 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1760 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1761 | if (*buf) { |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 1762 | free_rsp_buf(resp_buf_type, rsp_iov.iov_base); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1763 | } else if (resp_buf_type != CIFS_NO_BUFFER) { |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 1764 | /* return buffer to caller to free */ |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 1765 | *buf = rsp_iov.iov_base; |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1766 | if (resp_buf_type == CIFS_SMALL_BUFFER) |
Steve French | ec637e3 | 2005-12-12 20:53:18 -0800 | [diff] [blame] | 1767 | *pbuf_type = CIFS_SMALL_BUFFER; |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1768 | else if (resp_buf_type == CIFS_LARGE_BUFFER) |
Steve French | ec637e3 | 2005-12-12 20:53:18 -0800 | [diff] [blame] | 1769 | *pbuf_type = CIFS_LARGE_BUFFER; |
Steve French | 6cec2ae | 2006-02-22 17:31:52 -0600 | [diff] [blame] | 1770 | } /* else no valid buffer on return - leave as null */ |
Steve French | ec637e3 | 2005-12-12 20:53:18 -0800 | [diff] [blame] | 1771 | |
| 1772 | /* Note: On -EAGAIN error only caller can retry on handle based calls |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1773 | since file handle passed in no longer valid */ |
| 1774 | return rc; |
| 1775 | } |
| 1776 | |
Steve French | ec637e3 | 2005-12-12 20:53:18 -0800 | [diff] [blame] | 1777 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1778 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1779 | CIFSSMBWrite(const unsigned int xid, struct cifs_io_parms *io_parms, |
Al Viro | dbbab32 | 2016-09-05 17:53:43 -0400 | [diff] [blame] | 1780 | unsigned int *nbytes, const char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1781 | { |
| 1782 | int rc = -EACCES; |
| 1783 | WRITE_REQ *pSMB = NULL; |
| 1784 | WRITE_RSP *pSMBr = NULL; |
Steve French | 1c95518 | 2005-08-30 20:58:07 -0700 | [diff] [blame] | 1785 | int bytes_returned, wct; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1786 | __u32 bytes_sent; |
| 1787 | __u16 byte_count; |
Pavel Shilovsky | fa2989f | 2011-05-26 10:01:59 +0400 | [diff] [blame] | 1788 | __u32 pid = io_parms->pid; |
| 1789 | __u16 netfid = io_parms->netfid; |
| 1790 | __u64 offset = io_parms->offset; |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 1791 | struct cifs_tcon *tcon = io_parms->tcon; |
Pavel Shilovsky | fa2989f | 2011-05-26 10:01:59 +0400 | [diff] [blame] | 1792 | unsigned int count = io_parms->length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1793 | |
Steve French | a24e2d7 | 2010-04-03 17:20:21 +0000 | [diff] [blame] | 1794 | *nbytes = 0; |
| 1795 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1796 | /* cifs_dbg(FYI, "write at %lld %d bytes\n", offset, count);*/ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1797 | if (tcon->ses == NULL) |
Steve French | 1c95518 | 2005-08-30 20:58:07 -0700 | [diff] [blame] | 1798 | return -ECONNABORTED; |
| 1799 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1800 | if (tcon->ses->capabilities & CAP_LARGE_FILES) |
Steve French | 1c95518 | 2005-08-30 20:58:07 -0700 | [diff] [blame] | 1801 | wct = 14; |
Steve French | 4c3130e | 2008-12-09 00:28:16 +0000 | [diff] [blame] | 1802 | else { |
Steve French | 1c95518 | 2005-08-30 20:58:07 -0700 | [diff] [blame] | 1803 | wct = 12; |
Steve French | 4c3130e | 2008-12-09 00:28:16 +0000 | [diff] [blame] | 1804 | if ((offset >> 32) > 0) { |
| 1805 | /* can not handle big offset for old srv */ |
| 1806 | return -EIO; |
| 1807 | } |
| 1808 | } |
Steve French | 1c95518 | 2005-08-30 20:58:07 -0700 | [diff] [blame] | 1809 | |
| 1810 | rc = smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1811 | (void **) &pSMBr); |
| 1812 | if (rc) |
| 1813 | return rc; |
Pavel Shilovsky | fa2989f | 2011-05-26 10:01:59 +0400 | [diff] [blame] | 1814 | |
| 1815 | pSMB->hdr.Pid = cpu_to_le16((__u16)pid); |
| 1816 | pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16)); |
| 1817 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1818 | /* tcon and ses pointer are checked in smb_init */ |
| 1819 | if (tcon->ses->server == NULL) |
| 1820 | return -ECONNABORTED; |
| 1821 | |
| 1822 | pSMB->AndXCommand = 0xFF; /* none */ |
| 1823 | pSMB->Fid = netfid; |
| 1824 | pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1825 | if (wct == 14) |
Steve French | 1c95518 | 2005-08-30 20:58:07 -0700 | [diff] [blame] | 1826 | pSMB->OffsetHigh = cpu_to_le32(offset >> 32); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 1827 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1828 | pSMB->Reserved = 0xFFFFFFFF; |
| 1829 | pSMB->WriteMode = 0; |
| 1830 | pSMB->Remaining = 0; |
| 1831 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 1832 | /* 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] | 1833 | can send more if LARGE_WRITE_X capability returned by the server and if |
| 1834 | our buffer is big enough or if we convert to iovecs on socket writes |
| 1835 | and eliminate the copy to the CIFS buffer */ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1836 | if (tcon->ses->capabilities & CAP_LARGE_WRITE_X) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1837 | bytes_sent = min_t(const unsigned int, CIFSMaxBufSize, count); |
| 1838 | } else { |
| 1839 | bytes_sent = (tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE) |
| 1840 | & ~0xFF; |
| 1841 | } |
| 1842 | |
| 1843 | if (bytes_sent > count) |
| 1844 | bytes_sent = count; |
| 1845 | pSMB->DataOffset = |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 1846 | cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1847 | if (buf) |
Steve French | 61e7480 | 2008-12-03 00:57:54 +0000 | [diff] [blame] | 1848 | memcpy(pSMB->Data, buf, bytes_sent); |
Al Viro | dbbab32 | 2016-09-05 17:53:43 -0400 | [diff] [blame] | 1849 | else if (count != 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1850 | /* No buffer */ |
| 1851 | cifs_buf_release(pSMB); |
| 1852 | return -EINVAL; |
Steve French | e30dcf3 | 2005-09-20 20:49:16 -0700 | [diff] [blame] | 1853 | } /* else setting file size with write of zero bytes */ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1854 | if (wct == 14) |
Steve French | e30dcf3 | 2005-09-20 20:49:16 -0700 | [diff] [blame] | 1855 | byte_count = bytes_sent + 1; /* pad */ |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 1856 | else /* wct == 12 */ |
Steve French | e30dcf3 | 2005-09-20 20:49:16 -0700 | [diff] [blame] | 1857 | byte_count = bytes_sent + 5; /* bigger pad, smaller smb hdr */ |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 1858 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1859 | pSMB->DataLengthLow = cpu_to_le16(bytes_sent & 0xFFFF); |
| 1860 | pSMB->DataLengthHigh = cpu_to_le16(bytes_sent >> 16); |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 1861 | inc_rfc1001_len(pSMB, byte_count); |
Steve French | 1c95518 | 2005-08-30 20:58:07 -0700 | [diff] [blame] | 1862 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1863 | if (wct == 14) |
Steve French | 1c95518 | 2005-08-30 20:58:07 -0700 | [diff] [blame] | 1864 | pSMB->ByteCount = cpu_to_le16(byte_count); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 1865 | else { /* old style write has byte count 4 bytes earlier |
| 1866 | so 4 bytes pad */ |
| 1867 | struct smb_com_writex_req *pSMBW = |
Steve French | 1c95518 | 2005-08-30 20:58:07 -0700 | [diff] [blame] | 1868 | (struct smb_com_writex_req *)pSMB; |
| 1869 | pSMBW->ByteCount = cpu_to_le16(byte_count); |
| 1870 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1871 | |
| 1872 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
Al Viro | dbbab32 | 2016-09-05 17:53:43 -0400 | [diff] [blame] | 1873 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
Pavel Shilovsky | 44c5818 | 2012-05-28 14:16:31 +0400 | [diff] [blame] | 1874 | cifs_stats_inc(&tcon->stats.cifs_stats.num_writes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1875 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1876 | cifs_dbg(FYI, "Send error in write = %d\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1877 | } else { |
| 1878 | *nbytes = le16_to_cpu(pSMBr->CountHigh); |
| 1879 | *nbytes = (*nbytes) << 16; |
| 1880 | *nbytes += le16_to_cpu(pSMBr->Count); |
Suresh Jayaraman | 6513a81 | 2010-03-31 12:00:03 +0530 | [diff] [blame] | 1881 | |
| 1882 | /* |
| 1883 | * Mask off high 16 bits when bytes written as returned by the |
| 1884 | * server is greater than bytes requested by the client. Some |
| 1885 | * OS/2 servers are known to set incorrect CountHigh values. |
| 1886 | */ |
| 1887 | if (*nbytes > count) |
| 1888 | *nbytes &= 0xFFFF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1889 | } |
| 1890 | |
| 1891 | cifs_buf_release(pSMB); |
| 1892 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 1893 | /* Note: On -EAGAIN error only caller can retry on handle based calls |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1894 | since file handle passed in no longer valid */ |
| 1895 | |
| 1896 | return rc; |
| 1897 | } |
| 1898 | |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 1899 | void |
| 1900 | cifs_writedata_release(struct kref *refcount) |
| 1901 | { |
| 1902 | struct cifs_writedata *wdata = container_of(refcount, |
| 1903 | struct cifs_writedata, refcount); |
| 1904 | |
| 1905 | if (wdata->cfile) |
| 1906 | cifsFileInfo_put(wdata->cfile); |
| 1907 | |
| 1908 | kfree(wdata); |
| 1909 | } |
| 1910 | |
| 1911 | /* |
| 1912 | * Write failed with a retryable error. Resend the write request. It's also |
| 1913 | * possible that the page was redirtied so re-clean the page. |
| 1914 | */ |
| 1915 | static void |
| 1916 | cifs_writev_requeue(struct cifs_writedata *wdata) |
| 1917 | { |
Pavel Shilovsky | 7f6c500 | 2014-06-22 11:03:22 +0400 | [diff] [blame] | 1918 | int i, rc = 0; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1919 | struct inode *inode = d_inode(wdata->cfile->dentry); |
Pavel Shilovsky | c9de5c8 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 1920 | struct TCP_Server_Info *server; |
Pavel Shilovsky | 7f6c500 | 2014-06-22 11:03:22 +0400 | [diff] [blame] | 1921 | unsigned int rest_len; |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 1922 | |
Pavel Shilovsky | 7f6c500 | 2014-06-22 11:03:22 +0400 | [diff] [blame] | 1923 | server = tlink_tcon(wdata->cfile->tlink)->ses->server; |
| 1924 | i = 0; |
| 1925 | rest_len = wdata->bytes; |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 1926 | do { |
Pavel Shilovsky | 7f6c500 | 2014-06-22 11:03:22 +0400 | [diff] [blame] | 1927 | struct cifs_writedata *wdata2; |
| 1928 | unsigned int j, nr_pages, wsize, tailsz, cur_len; |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 1929 | |
Pavel Shilovsky | 7f6c500 | 2014-06-22 11:03:22 +0400 | [diff] [blame] | 1930 | wsize = server->ops->wp_retry_size(inode); |
| 1931 | if (wsize < rest_len) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1932 | nr_pages = wsize / PAGE_SIZE; |
Pavel Shilovsky | 7f6c500 | 2014-06-22 11:03:22 +0400 | [diff] [blame] | 1933 | if (!nr_pages) { |
| 1934 | rc = -ENOTSUPP; |
| 1935 | break; |
| 1936 | } |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1937 | cur_len = nr_pages * PAGE_SIZE; |
| 1938 | tailsz = PAGE_SIZE; |
Pavel Shilovsky | 7f6c500 | 2014-06-22 11:03:22 +0400 | [diff] [blame] | 1939 | } else { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1940 | nr_pages = DIV_ROUND_UP(rest_len, PAGE_SIZE); |
Pavel Shilovsky | 7f6c500 | 2014-06-22 11:03:22 +0400 | [diff] [blame] | 1941 | cur_len = rest_len; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1942 | tailsz = rest_len - (nr_pages - 1) * PAGE_SIZE; |
Ouyang Maochun | c51bb0e | 2013-02-18 09:54:52 -0600 | [diff] [blame] | 1943 | } |
Pavel Shilovsky | 7f6c500 | 2014-06-22 11:03:22 +0400 | [diff] [blame] | 1944 | |
| 1945 | wdata2 = cifs_writedata_alloc(nr_pages, cifs_writev_complete); |
| 1946 | if (!wdata2) { |
| 1947 | rc = -ENOMEM; |
| 1948 | break; |
| 1949 | } |
| 1950 | |
| 1951 | for (j = 0; j < nr_pages; j++) { |
| 1952 | wdata2->pages[j] = wdata->pages[i + j]; |
| 1953 | lock_page(wdata2->pages[j]); |
| 1954 | clear_page_dirty_for_io(wdata2->pages[j]); |
| 1955 | } |
| 1956 | |
| 1957 | wdata2->sync_mode = wdata->sync_mode; |
| 1958 | wdata2->nr_pages = nr_pages; |
| 1959 | wdata2->offset = page_offset(wdata2->pages[0]); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1960 | wdata2->pagesz = PAGE_SIZE; |
Pavel Shilovsky | 7f6c500 | 2014-06-22 11:03:22 +0400 | [diff] [blame] | 1961 | wdata2->tailsz = tailsz; |
| 1962 | wdata2->bytes = cur_len; |
| 1963 | |
| 1964 | wdata2->cfile = find_writable_file(CIFS_I(inode), false); |
| 1965 | if (!wdata2->cfile) { |
| 1966 | cifs_dbg(VFS, "No writable handles for inode\n"); |
| 1967 | rc = -EBADF; |
| 1968 | break; |
| 1969 | } |
| 1970 | wdata2->pid = wdata2->cfile->pid; |
| 1971 | rc = server->ops->async_writev(wdata2, cifs_writedata_release); |
| 1972 | |
| 1973 | for (j = 0; j < nr_pages; j++) { |
| 1974 | unlock_page(wdata2->pages[j]); |
| 1975 | if (rc != 0 && rc != -EAGAIN) { |
| 1976 | SetPageError(wdata2->pages[j]); |
| 1977 | end_page_writeback(wdata2->pages[j]); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1978 | put_page(wdata2->pages[j]); |
Pavel Shilovsky | 7f6c500 | 2014-06-22 11:03:22 +0400 | [diff] [blame] | 1979 | } |
| 1980 | } |
| 1981 | |
| 1982 | if (rc) { |
| 1983 | kref_put(&wdata2->refcount, cifs_writedata_release); |
| 1984 | if (rc == -EAGAIN) |
| 1985 | continue; |
| 1986 | break; |
| 1987 | } |
| 1988 | |
| 1989 | rest_len -= cur_len; |
| 1990 | i += nr_pages; |
| 1991 | } while (i < wdata->nr_pages); |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 1992 | |
| 1993 | mapping_set_error(inode->i_mapping, rc); |
| 1994 | kref_put(&wdata->refcount, cifs_writedata_release); |
| 1995 | } |
| 1996 | |
Jeff Layton | c2e8764 | 2012-03-23 14:40:55 -0400 | [diff] [blame] | 1997 | void |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 1998 | cifs_writev_complete(struct work_struct *work) |
| 1999 | { |
| 2000 | struct cifs_writedata *wdata = container_of(work, |
| 2001 | struct cifs_writedata, work); |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 2002 | struct inode *inode = d_inode(wdata->cfile->dentry); |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 2003 | int i = 0; |
| 2004 | |
| 2005 | if (wdata->result == 0) { |
Jeff Layton | 597b027 | 2012-03-23 14:40:56 -0400 | [diff] [blame] | 2006 | spin_lock(&inode->i_lock); |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 2007 | cifs_update_eof(CIFS_I(inode), wdata->offset, wdata->bytes); |
Jeff Layton | 597b027 | 2012-03-23 14:40:56 -0400 | [diff] [blame] | 2008 | spin_unlock(&inode->i_lock); |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 2009 | cifs_stats_bytes_written(tlink_tcon(wdata->cfile->tlink), |
| 2010 | wdata->bytes); |
| 2011 | } else if (wdata->sync_mode == WB_SYNC_ALL && wdata->result == -EAGAIN) |
| 2012 | return cifs_writev_requeue(wdata); |
| 2013 | |
| 2014 | for (i = 0; i < wdata->nr_pages; i++) { |
| 2015 | struct page *page = wdata->pages[i]; |
| 2016 | if (wdata->result == -EAGAIN) |
| 2017 | __set_page_dirty_nobuffers(page); |
| 2018 | else if (wdata->result < 0) |
| 2019 | SetPageError(page); |
| 2020 | end_page_writeback(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2021 | put_page(page); |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 2022 | } |
| 2023 | if (wdata->result != -EAGAIN) |
| 2024 | mapping_set_error(inode->i_mapping, wdata->result); |
| 2025 | kref_put(&wdata->refcount, cifs_writedata_release); |
| 2026 | } |
| 2027 | |
| 2028 | struct cifs_writedata * |
Jeff Layton | c2e8764 | 2012-03-23 14:40:55 -0400 | [diff] [blame] | 2029 | cifs_writedata_alloc(unsigned int nr_pages, work_func_t complete) |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 2030 | { |
| 2031 | struct cifs_writedata *wdata; |
| 2032 | |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 2033 | /* writedata + number of page pointers */ |
| 2034 | wdata = kzalloc(sizeof(*wdata) + |
Jeff Layton | 26c8f0d | 2014-02-07 11:04:04 -0500 | [diff] [blame] | 2035 | sizeof(struct page *) * nr_pages, GFP_NOFS); |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 2036 | if (wdata != NULL) { |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 2037 | kref_init(&wdata->refcount); |
Jeff Layton | da82f7e | 2012-03-23 14:40:56 -0400 | [diff] [blame] | 2038 | INIT_LIST_HEAD(&wdata->list); |
| 2039 | init_completion(&wdata->done); |
| 2040 | INIT_WORK(&wdata->work, complete); |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 2041 | } |
| 2042 | return wdata; |
| 2043 | } |
| 2044 | |
| 2045 | /* |
Pavel Shilovsky | 7c9421e | 2012-03-23 14:28:03 -0400 | [diff] [blame] | 2046 | * 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] | 2047 | * workqueue completion task. |
| 2048 | */ |
| 2049 | static void |
| 2050 | cifs_writev_callback(struct mid_q_entry *mid) |
| 2051 | { |
| 2052 | struct cifs_writedata *wdata = mid->callback_data; |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 2053 | struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink); |
Christopher Oo | 5fb4e28 | 2015-06-25 16:10:48 -0700 | [diff] [blame] | 2054 | struct TCP_Server_Info *server = tcon->ses->server; |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 2055 | unsigned int written; |
| 2056 | WRITE_RSP *smb = (WRITE_RSP *)mid->resp_buf; |
| 2057 | |
Pavel Shilovsky | 7c9421e | 2012-03-23 14:28:03 -0400 | [diff] [blame] | 2058 | switch (mid->mid_state) { |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 2059 | case MID_RESPONSE_RECEIVED: |
| 2060 | wdata->result = cifs_check_receive(mid, tcon->ses->server, 0); |
| 2061 | if (wdata->result != 0) |
| 2062 | break; |
| 2063 | |
| 2064 | written = le16_to_cpu(smb->CountHigh); |
| 2065 | written <<= 16; |
| 2066 | written += le16_to_cpu(smb->Count); |
| 2067 | /* |
| 2068 | * Mask off high 16 bits when bytes written as returned |
| 2069 | * by the server is greater than bytes requested by the |
| 2070 | * client. OS/2 servers are known to set incorrect |
| 2071 | * CountHigh values. |
| 2072 | */ |
| 2073 | if (written > wdata->bytes) |
| 2074 | written &= 0xFFFF; |
| 2075 | |
| 2076 | if (written < wdata->bytes) |
| 2077 | wdata->result = -ENOSPC; |
| 2078 | else |
| 2079 | wdata->bytes = written; |
| 2080 | break; |
| 2081 | case MID_REQUEST_SUBMITTED: |
| 2082 | case MID_RETRY_NEEDED: |
| 2083 | wdata->result = -EAGAIN; |
| 2084 | break; |
| 2085 | default: |
| 2086 | wdata->result = -EIO; |
| 2087 | break; |
| 2088 | } |
| 2089 | |
Jeff Layton | da472fc | 2012-03-23 14:40:53 -0400 | [diff] [blame] | 2090 | queue_work(cifsiod_wq, &wdata->work); |
Christopher Oo | 5fb4e28 | 2015-06-25 16:10:48 -0700 | [diff] [blame] | 2091 | mutex_lock(&server->srv_mutex); |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 2092 | DeleteMidQEntry(mid); |
Christopher Oo | 5fb4e28 | 2015-06-25 16:10:48 -0700 | [diff] [blame] | 2093 | mutex_unlock(&server->srv_mutex); |
Pavel Shilovsky | a891f0f | 2012-05-23 16:14:34 +0400 | [diff] [blame] | 2094 | add_credits(tcon->ses->server, 1, 0); |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 2095 | } |
| 2096 | |
| 2097 | /* cifs_async_writev - send an async write, and set up mid to handle result */ |
| 2098 | int |
Steve French | 4a5c80d | 2014-02-07 20:45:12 -0600 | [diff] [blame] | 2099 | cifs_async_writev(struct cifs_writedata *wdata, |
| 2100 | void (*release)(struct kref *kref)) |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 2101 | { |
Jeff Layton | eddb079 | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 2102 | int rc = -EACCES; |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 2103 | WRITE_REQ *smb = NULL; |
| 2104 | int wct; |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 2105 | struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink); |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 2106 | struct kvec iov[2]; |
Jeff Layton | fec344e | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 2107 | struct smb_rqst rqst = { }; |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 2108 | |
| 2109 | if (tcon->ses->capabilities & CAP_LARGE_FILES) { |
| 2110 | wct = 14; |
| 2111 | } else { |
| 2112 | wct = 12; |
| 2113 | if (wdata->offset >> 32 > 0) { |
| 2114 | /* can not handle big offset for old srv */ |
| 2115 | return -EIO; |
| 2116 | } |
| 2117 | } |
| 2118 | |
| 2119 | rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **)&smb); |
| 2120 | if (rc) |
| 2121 | goto async_writev_out; |
| 2122 | |
Jeff Layton | fe5f5d2 | 2012-03-23 14:40:55 -0400 | [diff] [blame] | 2123 | smb->hdr.Pid = cpu_to_le16((__u16)wdata->pid); |
| 2124 | smb->hdr.PidHigh = cpu_to_le16((__u16)(wdata->pid >> 16)); |
Pavel Shilovsky | fa2989f | 2011-05-26 10:01:59 +0400 | [diff] [blame] | 2125 | |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 2126 | smb->AndXCommand = 0xFF; /* none */ |
Pavel Shilovsky | 4b4de76 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 2127 | smb->Fid = wdata->cfile->fid.netfid; |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 2128 | smb->OffsetLow = cpu_to_le32(wdata->offset & 0xFFFFFFFF); |
| 2129 | if (wct == 14) |
| 2130 | smb->OffsetHigh = cpu_to_le32(wdata->offset >> 32); |
| 2131 | smb->Reserved = 0xFFFFFFFF; |
| 2132 | smb->WriteMode = 0; |
| 2133 | smb->Remaining = 0; |
| 2134 | |
| 2135 | smb->DataOffset = |
| 2136 | cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4); |
| 2137 | |
| 2138 | /* 4 for RFC1001 length + 1 for BCC */ |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 2139 | iov[0].iov_len = 4; |
| 2140 | iov[0].iov_base = smb; |
| 2141 | iov[1].iov_len = get_rfc1002_length(smb) + 1; |
| 2142 | iov[1].iov_base = (char *)smb + 4; |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 2143 | |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 2144 | rqst.rq_iov = iov; |
| 2145 | rqst.rq_nvec = 2; |
Jeff Layton | eddb079 | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 2146 | rqst.rq_pages = wdata->pages; |
| 2147 | rqst.rq_npages = wdata->nr_pages; |
| 2148 | rqst.rq_pagesz = wdata->pagesz; |
| 2149 | rqst.rq_tailsz = wdata->tailsz; |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 2150 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2151 | cifs_dbg(FYI, "async write at %llu %u bytes\n", |
| 2152 | wdata->offset, wdata->bytes); |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 2153 | |
| 2154 | smb->DataLengthLow = cpu_to_le16(wdata->bytes & 0xFFFF); |
| 2155 | smb->DataLengthHigh = cpu_to_le16(wdata->bytes >> 16); |
| 2156 | |
| 2157 | if (wct == 14) { |
| 2158 | inc_rfc1001_len(&smb->hdr, wdata->bytes + 1); |
| 2159 | put_bcc(wdata->bytes + 1, &smb->hdr); |
| 2160 | } else { |
| 2161 | /* wct == 12 */ |
| 2162 | struct smb_com_writex_req *smbw = |
| 2163 | (struct smb_com_writex_req *)smb; |
| 2164 | inc_rfc1001_len(&smbw->hdr, wdata->bytes + 5); |
| 2165 | put_bcc(wdata->bytes + 5, &smbw->hdr); |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 2166 | iov[1].iov_len += 4; /* pad bigger by four bytes */ |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 2167 | } |
| 2168 | |
| 2169 | kref_get(&wdata->refcount); |
Jeff Layton | fec344e | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 2170 | rc = cifs_call_async(tcon->ses->server, &rqst, NULL, |
Pavel Shilovsky | 9b7c18a | 2016-11-16 14:06:17 -0800 | [diff] [blame] | 2171 | cifs_writev_callback, NULL, wdata, 0); |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 2172 | |
| 2173 | if (rc == 0) |
Pavel Shilovsky | 44c5818 | 2012-05-28 14:16:31 +0400 | [diff] [blame] | 2174 | cifs_stats_inc(&tcon->stats.cifs_stats.num_writes); |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 2175 | else |
Steve French | 4a5c80d | 2014-02-07 20:45:12 -0600 | [diff] [blame] | 2176 | kref_put(&wdata->refcount, release); |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 2177 | |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 2178 | async_writev_out: |
| 2179 | cifs_small_buf_release(smb); |
Jeff Layton | c28c89f | 2011-05-19 16:22:56 -0400 | [diff] [blame] | 2180 | return rc; |
| 2181 | } |
| 2182 | |
Steve French | d6e04ae | 2005-06-13 13:24:43 -0500 | [diff] [blame] | 2183 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2184 | CIFSSMBWrite2(const unsigned int xid, struct cifs_io_parms *io_parms, |
Pavel Shilovsky | ba9ad725 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 2185 | unsigned int *nbytes, struct kvec *iov, int n_vec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2186 | { |
| 2187 | int rc = -EACCES; |
| 2188 | WRITE_REQ *pSMB = NULL; |
Steve French | ec637e3 | 2005-12-12 20:53:18 -0800 | [diff] [blame] | 2189 | int wct; |
Steve French | d6e04ae | 2005-06-13 13:24:43 -0500 | [diff] [blame] | 2190 | int smb_hdr_len; |
Steve French | ec637e3 | 2005-12-12 20:53:18 -0800 | [diff] [blame] | 2191 | int resp_buf_type = 0; |
Pavel Shilovsky | fa2989f | 2011-05-26 10:01:59 +0400 | [diff] [blame] | 2192 | __u32 pid = io_parms->pid; |
| 2193 | __u16 netfid = io_parms->netfid; |
| 2194 | __u64 offset = io_parms->offset; |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 2195 | struct cifs_tcon *tcon = io_parms->tcon; |
Pavel Shilovsky | fa2989f | 2011-05-26 10:01:59 +0400 | [diff] [blame] | 2196 | unsigned int count = io_parms->length; |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 2197 | struct kvec rsp_iov; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2198 | |
Jeff Layton | fbec9ab | 2009-04-03 13:44:00 -0400 | [diff] [blame] | 2199 | *nbytes = 0; |
| 2200 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2201 | 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] | 2202 | |
Steve French | 4c3130e | 2008-12-09 00:28:16 +0000 | [diff] [blame] | 2203 | if (tcon->ses->capabilities & CAP_LARGE_FILES) { |
Steve French | 8cc64c6 | 2005-10-03 13:49:43 -0700 | [diff] [blame] | 2204 | wct = 14; |
Steve French | 4c3130e | 2008-12-09 00:28:16 +0000 | [diff] [blame] | 2205 | } else { |
Steve French | 8cc64c6 | 2005-10-03 13:49:43 -0700 | [diff] [blame] | 2206 | wct = 12; |
Steve French | 4c3130e | 2008-12-09 00:28:16 +0000 | [diff] [blame] | 2207 | if ((offset >> 32) > 0) { |
| 2208 | /* can not handle big offset for old srv */ |
| 2209 | return -EIO; |
| 2210 | } |
| 2211 | } |
Steve French | 8cc64c6 | 2005-10-03 13:49:43 -0700 | [diff] [blame] | 2212 | rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2213 | if (rc) |
| 2214 | return rc; |
Pavel Shilovsky | fa2989f | 2011-05-26 10:01:59 +0400 | [diff] [blame] | 2215 | |
| 2216 | pSMB->hdr.Pid = cpu_to_le16((__u16)pid); |
| 2217 | pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16)); |
| 2218 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2219 | /* tcon and ses pointer are checked in smb_init */ |
| 2220 | if (tcon->ses->server == NULL) |
| 2221 | return -ECONNABORTED; |
| 2222 | |
Steve French | d6e04ae | 2005-06-13 13:24:43 -0500 | [diff] [blame] | 2223 | pSMB->AndXCommand = 0xFF; /* none */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2224 | pSMB->Fid = netfid; |
| 2225 | pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 2226 | if (wct == 14) |
Steve French | 8cc64c6 | 2005-10-03 13:49:43 -0700 | [diff] [blame] | 2227 | pSMB->OffsetHigh = cpu_to_le32(offset >> 32); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2228 | pSMB->Reserved = 0xFFFFFFFF; |
| 2229 | pSMB->WriteMode = 0; |
| 2230 | pSMB->Remaining = 0; |
Steve French | d6e04ae | 2005-06-13 13:24:43 -0500 | [diff] [blame] | 2231 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2232 | pSMB->DataOffset = |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2233 | cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2234 | |
Steve French | 3e84469 | 2005-10-03 13:37:24 -0700 | [diff] [blame] | 2235 | pSMB->DataLengthLow = cpu_to_le16(count & 0xFFFF); |
| 2236 | pSMB->DataLengthHigh = cpu_to_le16(count >> 16); |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 2237 | /* header + 1 byte pad */ |
| 2238 | smb_hdr_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 1; |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 2239 | if (wct == 14) |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 2240 | inc_rfc1001_len(pSMB, count + 1); |
Steve French | 8cc64c6 | 2005-10-03 13:49:43 -0700 | [diff] [blame] | 2241 | else /* wct == 12 */ |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 2242 | inc_rfc1001_len(pSMB, count + 5); /* smb data starts later */ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 2243 | if (wct == 14) |
Steve French | 8cc64c6 | 2005-10-03 13:49:43 -0700 | [diff] [blame] | 2244 | pSMB->ByteCount = cpu_to_le16(count + 1); |
| 2245 | else /* wct == 12 */ /* bigger pad, smaller smb hdr, keep offset ok */ { |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2246 | struct smb_com_writex_req *pSMBW = |
Steve French | 8cc64c6 | 2005-10-03 13:49:43 -0700 | [diff] [blame] | 2247 | (struct smb_com_writex_req *)pSMB; |
| 2248 | pSMBW->ByteCount = cpu_to_le16(count + 5); |
| 2249 | } |
Steve French | 3e84469 | 2005-10-03 13:37:24 -0700 | [diff] [blame] | 2250 | iov[0].iov_base = pSMB; |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 2251 | if (wct == 14) |
Steve French | ec637e3 | 2005-12-12 20:53:18 -0800 | [diff] [blame] | 2252 | iov[0].iov_len = smb_hdr_len + 4; |
| 2253 | else /* wct == 12 pad bigger by four bytes */ |
| 2254 | iov[0].iov_len = smb_hdr_len + 8; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2255 | |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 2256 | rc = SendReceive2(xid, tcon->ses, iov, n_vec + 1, &resp_buf_type, 0, |
| 2257 | &rsp_iov); |
| 2258 | cifs_small_buf_release(pSMB); |
Pavel Shilovsky | 44c5818 | 2012-05-28 14:16:31 +0400 | [diff] [blame] | 2259 | cifs_stats_inc(&tcon->stats.cifs_stats.num_writes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2260 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2261 | cifs_dbg(FYI, "Send error Write2 = %d\n", rc); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 2262 | } else if (resp_buf_type == 0) { |
Steve French | ec637e3 | 2005-12-12 20:53:18 -0800 | [diff] [blame] | 2263 | /* presumably this can not happen, but best to be safe */ |
| 2264 | rc = -EIO; |
Steve French | d6e04ae | 2005-06-13 13:24:43 -0500 | [diff] [blame] | 2265 | } else { |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 2266 | WRITE_RSP *pSMBr = (WRITE_RSP *)rsp_iov.iov_base; |
Steve French | d6e04ae | 2005-06-13 13:24:43 -0500 | [diff] [blame] | 2267 | *nbytes = le16_to_cpu(pSMBr->CountHigh); |
| 2268 | *nbytes = (*nbytes) << 16; |
| 2269 | *nbytes += le16_to_cpu(pSMBr->Count); |
Suresh Jayaraman | 6513a81 | 2010-03-31 12:00:03 +0530 | [diff] [blame] | 2270 | |
| 2271 | /* |
| 2272 | * Mask off high 16 bits when bytes written as returned by the |
| 2273 | * server is greater than bytes requested by the client. OS/2 |
| 2274 | * servers are known to set incorrect CountHigh values. |
| 2275 | */ |
| 2276 | if (*nbytes > count) |
| 2277 | *nbytes &= 0xFFFF; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2278 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2279 | |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 2280 | free_rsp_buf(resp_buf_type, rsp_iov.iov_base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2281 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2282 | /* Note: On -EAGAIN error only caller can retry on handle based calls |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2283 | since file handle passed in no longer valid */ |
| 2284 | |
| 2285 | return rc; |
| 2286 | } |
Steve French | d6e04ae | 2005-06-13 13:24:43 -0500 | [diff] [blame] | 2287 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2288 | int cifs_lockv(const unsigned int xid, struct cifs_tcon *tcon, |
| 2289 | const __u16 netfid, const __u8 lock_type, const __u32 num_unlock, |
Pavel Shilovsky | 9ee305b | 2011-10-22 15:33:31 +0400 | [diff] [blame] | 2290 | const __u32 num_lock, LOCKING_ANDX_RANGE *buf) |
| 2291 | { |
| 2292 | int rc = 0; |
| 2293 | LOCK_REQ *pSMB = NULL; |
| 2294 | struct kvec iov[2]; |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 2295 | struct kvec rsp_iov; |
Pavel Shilovsky | 9ee305b | 2011-10-22 15:33:31 +0400 | [diff] [blame] | 2296 | int resp_buf_type; |
| 2297 | __u16 count; |
| 2298 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2299 | cifs_dbg(FYI, "cifs_lockv num lock %d num unlock %d\n", |
| 2300 | num_lock, num_unlock); |
Pavel Shilovsky | 9ee305b | 2011-10-22 15:33:31 +0400 | [diff] [blame] | 2301 | |
| 2302 | rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB); |
| 2303 | if (rc) |
| 2304 | return rc; |
| 2305 | |
| 2306 | pSMB->Timeout = 0; |
| 2307 | pSMB->NumberOfLocks = cpu_to_le16(num_lock); |
| 2308 | pSMB->NumberOfUnlocks = cpu_to_le16(num_unlock); |
| 2309 | pSMB->LockType = lock_type; |
| 2310 | pSMB->AndXCommand = 0xFF; /* none */ |
| 2311 | pSMB->Fid = netfid; /* netfid stays le */ |
| 2312 | |
| 2313 | count = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE); |
| 2314 | inc_rfc1001_len(pSMB, count); |
| 2315 | pSMB->ByteCount = cpu_to_le16(count); |
| 2316 | |
| 2317 | iov[0].iov_base = (char *)pSMB; |
| 2318 | iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4 - |
| 2319 | (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE); |
| 2320 | iov[1].iov_base = (char *)buf; |
| 2321 | iov[1].iov_len = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE); |
| 2322 | |
Pavel Shilovsky | 44c5818 | 2012-05-28 14:16:31 +0400 | [diff] [blame] | 2323 | cifs_stats_inc(&tcon->stats.cifs_stats.num_locks); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 2324 | rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP, |
| 2325 | &rsp_iov); |
| 2326 | cifs_small_buf_release(pSMB); |
Pavel Shilovsky | 9ee305b | 2011-10-22 15:33:31 +0400 | [diff] [blame] | 2327 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2328 | cifs_dbg(FYI, "Send error in cifs_lockv = %d\n", rc); |
Pavel Shilovsky | 9ee305b | 2011-10-22 15:33:31 +0400 | [diff] [blame] | 2329 | |
| 2330 | return rc; |
| 2331 | } |
Steve French | d6e04ae | 2005-06-13 13:24:43 -0500 | [diff] [blame] | 2332 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2333 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2334 | CIFSSMBLock(const unsigned int xid, struct cifs_tcon *tcon, |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 2335 | const __u16 smb_file_id, const __u32 netpid, const __u64 len, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2336 | const __u64 offset, const __u32 numUnlock, |
Pavel Shilovsky | 12fed00 | 2011-01-17 20:15:44 +0300 | [diff] [blame] | 2337 | const __u32 numLock, const __u8 lockType, |
| 2338 | const bool waitFlag, const __u8 oplock_level) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2339 | { |
| 2340 | int rc = 0; |
| 2341 | LOCK_REQ *pSMB = NULL; |
Steve French | aaa9bbe | 2008-05-23 17:38:32 +0000 | [diff] [blame] | 2342 | /* LOCK_RSP *pSMBr = NULL; */ /* No response data other than rc to parse */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2343 | int bytes_returned; |
Pavel Shilovsky | a891f0f | 2012-05-23 16:14:34 +0400 | [diff] [blame] | 2344 | int flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2345 | __u16 count; |
| 2346 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2347 | cifs_dbg(FYI, "CIFSSMBLock timeout %d numLock %d\n", |
| 2348 | (int)waitFlag, numLock); |
Steve French | 46810cb | 2005-04-28 22:41:09 -0700 | [diff] [blame] | 2349 | rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB); |
| 2350 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2351 | if (rc) |
| 2352 | return rc; |
| 2353 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 2354 | if (lockType == LOCKING_ANDX_OPLOCK_RELEASE) { |
Pavel Shilovsky | a891f0f | 2012-05-23 16:14:34 +0400 | [diff] [blame] | 2355 | /* no response expected */ |
| 2356 | flags = CIFS_ASYNC_OP | CIFS_OBREAK_OP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2357 | pSMB->Timeout = 0; |
Steve French | 4b18f2a | 2008-04-29 00:06:05 +0000 | [diff] [blame] | 2358 | } else if (waitFlag) { |
Pavel Shilovsky | a891f0f | 2012-05-23 16:14:34 +0400 | [diff] [blame] | 2359 | flags = CIFS_BLOCKING_OP; /* blocking operation, no timeout */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2360 | pSMB->Timeout = cpu_to_le32(-1);/* blocking - do not time out */ |
| 2361 | } else { |
| 2362 | pSMB->Timeout = 0; |
| 2363 | } |
| 2364 | |
| 2365 | pSMB->NumberOfLocks = cpu_to_le16(numLock); |
| 2366 | pSMB->NumberOfUnlocks = cpu_to_le16(numUnlock); |
| 2367 | pSMB->LockType = lockType; |
Pavel Shilovsky | 12fed00 | 2011-01-17 20:15:44 +0300 | [diff] [blame] | 2368 | pSMB->OplockLevel = oplock_level; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2369 | pSMB->AndXCommand = 0xFF; /* none */ |
| 2370 | pSMB->Fid = smb_file_id; /* netfid stays le */ |
| 2371 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 2372 | if ((numLock != 0) || (numUnlock != 0)) { |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 2373 | pSMB->Locks[0].Pid = cpu_to_le16(netpid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2374 | /* BB where to store pid high? */ |
| 2375 | pSMB->Locks[0].LengthLow = cpu_to_le32((u32)len); |
| 2376 | pSMB->Locks[0].LengthHigh = cpu_to_le32((u32)(len>>32)); |
| 2377 | pSMB->Locks[0].OffsetLow = cpu_to_le32((u32)offset); |
| 2378 | pSMB->Locks[0].OffsetHigh = cpu_to_le32((u32)(offset>>32)); |
| 2379 | count = sizeof(LOCKING_ANDX_RANGE); |
| 2380 | } else { |
| 2381 | /* oplock break */ |
| 2382 | count = 0; |
| 2383 | } |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 2384 | inc_rfc1001_len(pSMB, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2385 | pSMB->ByteCount = cpu_to_le16(count); |
| 2386 | |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 2387 | if (waitFlag) |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 2388 | rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB, |
Steve French | aaa9bbe | 2008-05-23 17:38:32 +0000 | [diff] [blame] | 2389 | (struct smb_hdr *) pSMB, &bytes_returned); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 2390 | else |
Pavel Shilovsky | a891f0f | 2012-05-23 16:14:34 +0400 | [diff] [blame] | 2391 | rc = SendReceiveNoRsp(xid, tcon->ses, (char *)pSMB, flags); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 2392 | cifs_small_buf_release(pSMB); |
Pavel Shilovsky | 44c5818 | 2012-05-28 14:16:31 +0400 | [diff] [blame] | 2393 | cifs_stats_inc(&tcon->stats.cifs_stats.num_locks); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 2394 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2395 | cifs_dbg(FYI, "Send error in Lock = %d\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2396 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2397 | /* Note: On -EAGAIN error only caller can retry on handle based calls |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2398 | since file handle passed in no longer valid */ |
| 2399 | return rc; |
| 2400 | } |
| 2401 | |
| 2402 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2403 | CIFSSMBPosixLock(const unsigned int xid, struct cifs_tcon *tcon, |
Jeff Layton | c5fd363 | 2012-07-23 13:28:37 -0400 | [diff] [blame] | 2404 | const __u16 smb_file_id, const __u32 netpid, |
| 2405 | const loff_t start_offset, const __u64 len, |
| 2406 | struct file_lock *pLockData, const __u16 lock_type, |
| 2407 | const bool waitFlag) |
Steve French | 08547b0 | 2006-02-28 22:39:25 +0000 | [diff] [blame] | 2408 | { |
| 2409 | struct smb_com_transaction2_sfi_req *pSMB = NULL; |
| 2410 | struct smb_com_transaction2_sfi_rsp *pSMBr = NULL; |
Steve French | 08547b0 | 2006-02-28 22:39:25 +0000 | [diff] [blame] | 2411 | struct cifs_posix_lock *parm_data; |
| 2412 | int rc = 0; |
Steve French | 3a5ff61 | 2006-07-14 22:37:11 +0000 | [diff] [blame] | 2413 | int timeout = 0; |
Steve French | 08547b0 | 2006-02-28 22:39:25 +0000 | [diff] [blame] | 2414 | int bytes_returned = 0; |
Steve French | 133672e | 2007-11-13 22:41:37 +0000 | [diff] [blame] | 2415 | int resp_buf_type = 0; |
Steve French | 08547b0 | 2006-02-28 22:39:25 +0000 | [diff] [blame] | 2416 | __u16 params, param_offset, offset, byte_count, count; |
Steve French | 133672e | 2007-11-13 22:41:37 +0000 | [diff] [blame] | 2417 | struct kvec iov[1]; |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 2418 | struct kvec rsp_iov; |
Steve French | 08547b0 | 2006-02-28 22:39:25 +0000 | [diff] [blame] | 2419 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2420 | cifs_dbg(FYI, "Posix Lock\n"); |
Steve French | fc94cdb | 2006-05-30 18:03:32 +0000 | [diff] [blame] | 2421 | |
Steve French | 08547b0 | 2006-02-28 22:39:25 +0000 | [diff] [blame] | 2422 | rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB); |
| 2423 | |
| 2424 | if (rc) |
| 2425 | return rc; |
| 2426 | |
| 2427 | pSMBr = (struct smb_com_transaction2_sfi_rsp *)pSMB; |
| 2428 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2429 | params = 6; |
Steve French | 08547b0 | 2006-02-28 22:39:25 +0000 | [diff] [blame] | 2430 | pSMB->MaxSetupCount = 0; |
| 2431 | pSMB->Reserved = 0; |
| 2432 | pSMB->Flags = 0; |
Steve French | 08547b0 | 2006-02-28 22:39:25 +0000 | [diff] [blame] | 2433 | pSMB->Reserved2 = 0; |
| 2434 | param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4; |
| 2435 | offset = param_offset + params; |
| 2436 | |
Steve French | 08547b0 | 2006-02-28 22:39:25 +0000 | [diff] [blame] | 2437 | count = sizeof(struct cifs_posix_lock); |
| 2438 | pSMB->MaxParameterCount = cpu_to_le16(2); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 2439 | pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */ |
Steve French | 08547b0 | 2006-02-28 22:39:25 +0000 | [diff] [blame] | 2440 | pSMB->SetupCount = 1; |
| 2441 | pSMB->Reserved3 = 0; |
Jeff Layton | c5fd363 | 2012-07-23 13:28:37 -0400 | [diff] [blame] | 2442 | if (pLockData) |
Steve French | 08547b0 | 2006-02-28 22:39:25 +0000 | [diff] [blame] | 2443 | pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION); |
| 2444 | else |
| 2445 | pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION); |
| 2446 | byte_count = 3 /* pad */ + params + count; |
| 2447 | pSMB->DataCount = cpu_to_le16(count); |
| 2448 | pSMB->ParameterCount = cpu_to_le16(params); |
| 2449 | pSMB->TotalDataCount = pSMB->DataCount; |
| 2450 | pSMB->TotalParameterCount = pSMB->ParameterCount; |
| 2451 | pSMB->ParameterOffset = cpu_to_le16(param_offset); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2452 | parm_data = (struct cifs_posix_lock *) |
Steve French | 08547b0 | 2006-02-28 22:39:25 +0000 | [diff] [blame] | 2453 | (((char *) &pSMB->hdr.Protocol) + offset); |
| 2454 | |
| 2455 | parm_data->lock_type = cpu_to_le16(lock_type); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 2456 | if (waitFlag) { |
Steve French | 133672e | 2007-11-13 22:41:37 +0000 | [diff] [blame] | 2457 | timeout = CIFS_BLOCKING_OP; /* blocking operation, no timeout */ |
Steve French | cec6815a | 2006-05-30 18:07:17 +0000 | [diff] [blame] | 2458 | parm_data->lock_flags = cpu_to_le16(1); |
Steve French | 3a5ff61 | 2006-07-14 22:37:11 +0000 | [diff] [blame] | 2459 | pSMB->Timeout = cpu_to_le32(-1); |
| 2460 | } else |
| 2461 | pSMB->Timeout = 0; |
| 2462 | |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 2463 | parm_data->pid = cpu_to_le32(netpid); |
Jeff Layton | c5fd363 | 2012-07-23 13:28:37 -0400 | [diff] [blame] | 2464 | parm_data->start = cpu_to_le64(start_offset); |
Steve French | cec6815a | 2006-05-30 18:07:17 +0000 | [diff] [blame] | 2465 | parm_data->length = cpu_to_le64(len); /* normalize negative numbers */ |
Steve French | 08547b0 | 2006-02-28 22:39:25 +0000 | [diff] [blame] | 2466 | |
| 2467 | pSMB->DataOffset = cpu_to_le16(offset); |
Steve French | f26282c | 2006-03-01 09:17:37 +0000 | [diff] [blame] | 2468 | pSMB->Fid = smb_file_id; |
Steve French | 08547b0 | 2006-02-28 22:39:25 +0000 | [diff] [blame] | 2469 | pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_LOCK); |
| 2470 | pSMB->Reserved4 = 0; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 2471 | inc_rfc1001_len(pSMB, byte_count); |
Steve French | 08547b0 | 2006-02-28 22:39:25 +0000 | [diff] [blame] | 2472 | pSMB->ByteCount = cpu_to_le16(byte_count); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 2473 | if (waitFlag) { |
| 2474 | rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB, |
| 2475 | (struct smb_hdr *) pSMBr, &bytes_returned); |
| 2476 | } else { |
Steve French | 133672e | 2007-11-13 22:41:37 +0000 | [diff] [blame] | 2477 | iov[0].iov_base = (char *)pSMB; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 2478 | 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] | 2479 | rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */, |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 2480 | &resp_buf_type, timeout, &rsp_iov); |
| 2481 | pSMBr = (struct smb_com_transaction2_sfi_rsp *)rsp_iov.iov_base; |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 2482 | } |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 2483 | cifs_small_buf_release(pSMB); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 2484 | |
Steve French | 08547b0 | 2006-02-28 22:39:25 +0000 | [diff] [blame] | 2485 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2486 | cifs_dbg(FYI, "Send error in Posix Lock = %d\n", rc); |
Jeff Layton | c5fd363 | 2012-07-23 13:28:37 -0400 | [diff] [blame] | 2487 | } else if (pLockData) { |
Steve French | fc94cdb | 2006-05-30 18:03:32 +0000 | [diff] [blame] | 2488 | /* lock structure can be returned on get */ |
| 2489 | __u16 data_offset; |
| 2490 | __u16 data_count; |
| 2491 | rc = validate_t2((struct smb_t2_rsp *)pSMBr); |
Steve French | 08547b0 | 2006-02-28 22:39:25 +0000 | [diff] [blame] | 2492 | |
Jeff Layton | 820a803 | 2011-05-04 08:05:26 -0400 | [diff] [blame] | 2493 | if (rc || get_bcc(&pSMBr->hdr) < sizeof(*parm_data)) { |
Steve French | fc94cdb | 2006-05-30 18:03:32 +0000 | [diff] [blame] | 2494 | rc = -EIO; /* bad smb */ |
| 2495 | goto plk_err_exit; |
| 2496 | } |
Steve French | fc94cdb | 2006-05-30 18:03:32 +0000 | [diff] [blame] | 2497 | data_offset = le16_to_cpu(pSMBr->t2.DataOffset); |
| 2498 | data_count = le16_to_cpu(pSMBr->t2.DataCount); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 2499 | if (data_count < sizeof(struct cifs_posix_lock)) { |
Steve French | fc94cdb | 2006-05-30 18:03:32 +0000 | [diff] [blame] | 2500 | rc = -EIO; |
| 2501 | goto plk_err_exit; |
| 2502 | } |
| 2503 | parm_data = (struct cifs_posix_lock *) |
| 2504 | ((char *)&pSMBr->hdr.Protocol + data_offset); |
Fabian Frederick | bc09d14 | 2014-12-10 15:41:15 -0800 | [diff] [blame] | 2505 | if (parm_data->lock_type == cpu_to_le16(CIFS_UNLCK)) |
Steve French | fc94cdb | 2006-05-30 18:03:32 +0000 | [diff] [blame] | 2506 | pLockData->fl_type = F_UNLCK; |
Pavel Shilovsky | f05337c | 2010-04-05 09:59:14 +0400 | [diff] [blame] | 2507 | else { |
| 2508 | if (parm_data->lock_type == |
Fabian Frederick | bc09d14 | 2014-12-10 15:41:15 -0800 | [diff] [blame] | 2509 | cpu_to_le16(CIFS_RDLCK)) |
Pavel Shilovsky | f05337c | 2010-04-05 09:59:14 +0400 | [diff] [blame] | 2510 | pLockData->fl_type = F_RDLCK; |
| 2511 | else if (parm_data->lock_type == |
Fabian Frederick | bc09d14 | 2014-12-10 15:41:15 -0800 | [diff] [blame] | 2512 | cpu_to_le16(CIFS_WRLCK)) |
Pavel Shilovsky | f05337c | 2010-04-05 09:59:14 +0400 | [diff] [blame] | 2513 | pLockData->fl_type = F_WRLCK; |
| 2514 | |
Steve French | 5443d13 | 2011-03-13 05:08:25 +0000 | [diff] [blame] | 2515 | pLockData->fl_start = le64_to_cpu(parm_data->start); |
| 2516 | pLockData->fl_end = pLockData->fl_start + |
| 2517 | le64_to_cpu(parm_data->length) - 1; |
| 2518 | pLockData->fl_pid = le32_to_cpu(parm_data->pid); |
Pavel Shilovsky | f05337c | 2010-04-05 09:59:14 +0400 | [diff] [blame] | 2519 | } |
Steve French | fc94cdb | 2006-05-30 18:03:32 +0000 | [diff] [blame] | 2520 | } |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2521 | |
Steve French | fc94cdb | 2006-05-30 18:03:32 +0000 | [diff] [blame] | 2522 | plk_err_exit: |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 2523 | free_rsp_buf(resp_buf_type, rsp_iov.iov_base); |
Steve French | 133672e | 2007-11-13 22:41:37 +0000 | [diff] [blame] | 2524 | |
Steve French | 08547b0 | 2006-02-28 22:39:25 +0000 | [diff] [blame] | 2525 | /* Note: On -EAGAIN error only caller can retry on handle based calls |
| 2526 | since file handle passed in no longer valid */ |
| 2527 | |
| 2528 | return rc; |
| 2529 | } |
| 2530 | |
| 2531 | |
| 2532 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2533 | 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] | 2534 | { |
| 2535 | int rc = 0; |
| 2536 | CLOSE_REQ *pSMB = NULL; |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2537 | cifs_dbg(FYI, "In CIFSSMBClose\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2538 | |
| 2539 | /* do not retry on dead session on close */ |
| 2540 | rc = small_smb_init(SMB_COM_CLOSE, 3, tcon, (void **) &pSMB); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 2541 | if (rc == -EAGAIN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2542 | return 0; |
| 2543 | if (rc) |
| 2544 | return rc; |
| 2545 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2546 | pSMB->FileID = (__u16) smb_file_id; |
Steve French | b815f1e5 | 2006-10-02 05:53:29 +0000 | [diff] [blame] | 2547 | pSMB->LastWriteTime = 0xFFFFFFFF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2548 | pSMB->ByteCount = 0; |
Pavel Shilovsky | 792af7b | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 2549 | rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 2550 | cifs_small_buf_release(pSMB); |
Pavel Shilovsky | 44c5818 | 2012-05-28 14:16:31 +0400 | [diff] [blame] | 2551 | cifs_stats_inc(&tcon->stats.cifs_stats.num_closes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2552 | if (rc) { |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 2553 | if (rc != -EINTR) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2554 | /* EINTR is expected when user ctl-c to kill app */ |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2555 | cifs_dbg(VFS, "Send error in Close = %d\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2556 | } |
| 2557 | } |
| 2558 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2559 | /* Since session is dead, file will be closed on server already */ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 2560 | if (rc == -EAGAIN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2561 | rc = 0; |
| 2562 | |
| 2563 | return rc; |
| 2564 | } |
| 2565 | |
| 2566 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2567 | 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] | 2568 | { |
| 2569 | int rc = 0; |
| 2570 | FLUSH_REQ *pSMB = NULL; |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2571 | cifs_dbg(FYI, "In CIFSSMBFlush\n"); |
Steve French | b298f22 | 2009-02-21 21:17:43 +0000 | [diff] [blame] | 2572 | |
| 2573 | rc = small_smb_init(SMB_COM_FLUSH, 1, tcon, (void **) &pSMB); |
| 2574 | if (rc) |
| 2575 | return rc; |
| 2576 | |
| 2577 | pSMB->FileID = (__u16) smb_file_id; |
| 2578 | pSMB->ByteCount = 0; |
Pavel Shilovsky | 792af7b | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 2579 | rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 2580 | cifs_small_buf_release(pSMB); |
Pavel Shilovsky | 44c5818 | 2012-05-28 14:16:31 +0400 | [diff] [blame] | 2581 | cifs_stats_inc(&tcon->stats.cifs_stats.num_flushes); |
Steve French | b298f22 | 2009-02-21 21:17:43 +0000 | [diff] [blame] | 2582 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2583 | cifs_dbg(VFS, "Send error in Flush = %d\n", rc); |
Steve French | b298f22 | 2009-02-21 21:17:43 +0000 | [diff] [blame] | 2584 | |
| 2585 | return rc; |
| 2586 | } |
| 2587 | |
| 2588 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2589 | CIFSSMBRename(const unsigned int xid, struct cifs_tcon *tcon, |
Pavel Shilovsky | 8ceb984 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 2590 | const char *from_name, const char *to_name, |
| 2591 | struct cifs_sb_info *cifs_sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2592 | { |
| 2593 | int rc = 0; |
| 2594 | RENAME_REQ *pSMB = NULL; |
| 2595 | RENAME_RSP *pSMBr = NULL; |
| 2596 | int bytes_returned; |
| 2597 | int name_len, name_len2; |
| 2598 | __u16 count; |
Steve French | 2baa268 | 2014-09-27 02:19:01 -0500 | [diff] [blame] | 2599 | int remap = cifs_remap(cifs_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2600 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2601 | cifs_dbg(FYI, "In CIFSSMBRename\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2602 | renameRetry: |
| 2603 | rc = smb_init(SMB_COM_RENAME, 1, tcon, (void **) &pSMB, |
| 2604 | (void **) &pSMBr); |
| 2605 | if (rc) |
| 2606 | return rc; |
| 2607 | |
| 2608 | pSMB->BufferFormat = 0x04; |
| 2609 | pSMB->SearchAttributes = |
| 2610 | cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM | |
| 2611 | ATTR_DIRECTORY); |
| 2612 | |
| 2613 | if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) { |
Pavel Shilovsky | 8ceb984 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 2614 | name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName, |
| 2615 | from_name, PATH_MAX, |
| 2616 | cifs_sb->local_nls, remap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2617 | name_len++; /* trailing null */ |
| 2618 | name_len *= 2; |
| 2619 | pSMB->OldFileName[name_len] = 0x04; /* pad */ |
| 2620 | /* protocol requires ASCII signature byte on Unicode string */ |
| 2621 | pSMB->OldFileName[name_len + 1] = 0x00; |
| 2622 | name_len2 = |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 2623 | cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2], |
Pavel Shilovsky | 8ceb984 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 2624 | to_name, PATH_MAX, cifs_sb->local_nls, |
| 2625 | remap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2626 | name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ; |
| 2627 | name_len2 *= 2; /* convert to bytes */ |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2628 | } else { /* BB improve the check for buffer overruns BB */ |
Pavel Shilovsky | 8ceb984 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 2629 | name_len = strnlen(from_name, PATH_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2630 | name_len++; /* trailing null */ |
Pavel Shilovsky | 8ceb984 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 2631 | strncpy(pSMB->OldFileName, from_name, name_len); |
| 2632 | name_len2 = strnlen(to_name, PATH_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2633 | name_len2++; /* trailing null */ |
| 2634 | pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */ |
Pavel Shilovsky | 8ceb984 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 2635 | strncpy(&pSMB->OldFileName[name_len + 1], to_name, name_len2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2636 | name_len2++; /* trailing null */ |
| 2637 | name_len2++; /* signature byte */ |
| 2638 | } |
| 2639 | |
| 2640 | count = 1 /* 1st signature byte */ + name_len + name_len2; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 2641 | inc_rfc1001_len(pSMB, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2642 | pSMB->ByteCount = cpu_to_le16(count); |
| 2643 | |
| 2644 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 2645 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
Pavel Shilovsky | 44c5818 | 2012-05-28 14:16:31 +0400 | [diff] [blame] | 2646 | cifs_stats_inc(&tcon->stats.cifs_stats.num_renames); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 2647 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2648 | cifs_dbg(FYI, "Send error in rename = %d\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2649 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2650 | cifs_buf_release(pSMB); |
| 2651 | |
| 2652 | if (rc == -EAGAIN) |
| 2653 | goto renameRetry; |
| 2654 | |
| 2655 | return rc; |
| 2656 | } |
| 2657 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2658 | int CIFSSMBRenameOpenFile(const unsigned int xid, struct cifs_tcon *pTcon, |
Jeff Layton | 391e575 | 2008-09-24 11:32:59 -0400 | [diff] [blame] | 2659 | int netfid, const char *target_name, |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2660 | const struct nls_table *nls_codepage, int remap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2661 | { |
| 2662 | struct smb_com_transaction2_sfi_req *pSMB = NULL; |
| 2663 | struct smb_com_transaction2_sfi_rsp *pSMBr = NULL; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2664 | struct set_file_rename *rename_info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2665 | char *data_offset; |
| 2666 | char dummy_string[30]; |
| 2667 | int rc = 0; |
| 2668 | int bytes_returned = 0; |
| 2669 | int len_of_str; |
| 2670 | __u16 params, param_offset, offset, count, byte_count; |
| 2671 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2672 | cifs_dbg(FYI, "Rename to File by handle\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2673 | rc = smb_init(SMB_COM_TRANSACTION2, 15, pTcon, (void **) &pSMB, |
| 2674 | (void **) &pSMBr); |
| 2675 | if (rc) |
| 2676 | return rc; |
| 2677 | |
| 2678 | params = 6; |
| 2679 | pSMB->MaxSetupCount = 0; |
| 2680 | pSMB->Reserved = 0; |
| 2681 | pSMB->Flags = 0; |
| 2682 | pSMB->Timeout = 0; |
| 2683 | pSMB->Reserved2 = 0; |
| 2684 | param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4; |
| 2685 | offset = param_offset + params; |
| 2686 | |
| 2687 | data_offset = (char *) (&pSMB->hdr.Protocol) + offset; |
| 2688 | rename_info = (struct set_file_rename *) data_offset; |
| 2689 | pSMB->MaxParameterCount = cpu_to_le16(2); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 2690 | pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2691 | pSMB->SetupCount = 1; |
| 2692 | pSMB->Reserved3 = 0; |
| 2693 | pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION); |
| 2694 | byte_count = 3 /* pad */ + params; |
| 2695 | pSMB->ParameterCount = cpu_to_le16(params); |
| 2696 | pSMB->TotalParameterCount = pSMB->ParameterCount; |
| 2697 | pSMB->ParameterOffset = cpu_to_le16(param_offset); |
| 2698 | pSMB->DataOffset = cpu_to_le16(offset); |
| 2699 | /* construct random name ".cifs_tmp<inodenum><mid>" */ |
| 2700 | rename_info->overwrite = cpu_to_le32(1); |
| 2701 | rename_info->root_fid = 0; |
| 2702 | /* unicode only call */ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 2703 | if (target_name == NULL) { |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2704 | sprintf(dummy_string, "cifs%x", pSMB->hdr.Mid); |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 2705 | len_of_str = |
| 2706 | cifsConvertToUTF16((__le16 *)rename_info->target_name, |
Steve French | 737b758 | 2005-04-28 22:41:06 -0700 | [diff] [blame] | 2707 | dummy_string, 24, nls_codepage, remap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2708 | } else { |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 2709 | len_of_str = |
| 2710 | cifsConvertToUTF16((__le16 *)rename_info->target_name, |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2711 | target_name, PATH_MAX, nls_codepage, |
| 2712 | remap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2713 | } |
| 2714 | rename_info->target_name_len = cpu_to_le32(2 * len_of_str); |
Jeff Layton | 391e575 | 2008-09-24 11:32:59 -0400 | [diff] [blame] | 2715 | count = 12 /* sizeof(struct set_file_rename) */ + (2 * len_of_str); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2716 | byte_count += count; |
| 2717 | pSMB->DataCount = cpu_to_le16(count); |
| 2718 | pSMB->TotalDataCount = pSMB->DataCount; |
| 2719 | pSMB->Fid = netfid; |
| 2720 | pSMB->InformationLevel = |
| 2721 | cpu_to_le16(SMB_SET_FILE_RENAME_INFORMATION); |
| 2722 | pSMB->Reserved4 = 0; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 2723 | inc_rfc1001_len(pSMB, byte_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2724 | pSMB->ByteCount = cpu_to_le16(byte_count); |
| 2725 | rc = SendReceive(xid, pTcon->ses, (struct smb_hdr *) pSMB, |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2726 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
Pavel Shilovsky | 44c5818 | 2012-05-28 14:16:31 +0400 | [diff] [blame] | 2727 | cifs_stats_inc(&pTcon->stats.cifs_stats.num_t2renames); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 2728 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2729 | cifs_dbg(FYI, "Send error in Rename (by file handle) = %d\n", |
| 2730 | rc); |
Steve French | a5a2b48 | 2005-08-20 21:42:53 -0700 | [diff] [blame] | 2731 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2732 | cifs_buf_release(pSMB); |
| 2733 | |
| 2734 | /* Note: On -EAGAIN error only caller can retry on handle based calls |
| 2735 | since file handle passed in no longer valid */ |
| 2736 | |
| 2737 | return rc; |
| 2738 | } |
| 2739 | |
| 2740 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2741 | CIFSSMBCopy(const unsigned int xid, struct cifs_tcon *tcon, |
| 2742 | const char *fromName, const __u16 target_tid, const char *toName, |
| 2743 | const int flags, const struct nls_table *nls_codepage, int remap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2744 | { |
| 2745 | int rc = 0; |
| 2746 | COPY_REQ *pSMB = NULL; |
| 2747 | COPY_RSP *pSMBr = NULL; |
| 2748 | int bytes_returned; |
| 2749 | int name_len, name_len2; |
| 2750 | __u16 count; |
| 2751 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2752 | cifs_dbg(FYI, "In CIFSSMBCopy\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2753 | copyRetry: |
| 2754 | rc = smb_init(SMB_COM_COPY, 1, tcon, (void **) &pSMB, |
| 2755 | (void **) &pSMBr); |
| 2756 | if (rc) |
| 2757 | return rc; |
| 2758 | |
| 2759 | pSMB->BufferFormat = 0x04; |
| 2760 | pSMB->Tid2 = target_tid; |
| 2761 | |
| 2762 | pSMB->Flags = cpu_to_le16(flags & COPY_TREE); |
| 2763 | |
| 2764 | if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) { |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 2765 | name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName, |
| 2766 | fromName, PATH_MAX, nls_codepage, |
| 2767 | remap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2768 | name_len++; /* trailing null */ |
| 2769 | name_len *= 2; |
| 2770 | pSMB->OldFileName[name_len] = 0x04; /* pad */ |
| 2771 | /* protocol requires ASCII signature byte on Unicode string */ |
| 2772 | pSMB->OldFileName[name_len + 1] = 0x00; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2773 | name_len2 = |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 2774 | cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2], |
| 2775 | toName, PATH_MAX, nls_codepage, remap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2776 | name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ; |
| 2777 | name_len2 *= 2; /* convert to bytes */ |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2778 | } else { /* BB improve the check for buffer overruns BB */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2779 | name_len = strnlen(fromName, PATH_MAX); |
| 2780 | name_len++; /* trailing null */ |
| 2781 | strncpy(pSMB->OldFileName, fromName, name_len); |
| 2782 | name_len2 = strnlen(toName, PATH_MAX); |
| 2783 | name_len2++; /* trailing null */ |
| 2784 | pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */ |
| 2785 | strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2); |
| 2786 | name_len2++; /* trailing null */ |
| 2787 | name_len2++; /* signature byte */ |
| 2788 | } |
| 2789 | |
| 2790 | count = 1 /* 1st signature byte */ + name_len + name_len2; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 2791 | inc_rfc1001_len(pSMB, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2792 | pSMB->ByteCount = cpu_to_le16(count); |
| 2793 | |
| 2794 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 2795 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
| 2796 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2797 | cifs_dbg(FYI, "Send error in copy = %d with %d files copied\n", |
| 2798 | rc, le16_to_cpu(pSMBr->CopyCount)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2799 | } |
Steve French | 0d817bc | 2008-05-22 02:02:03 +0000 | [diff] [blame] | 2800 | cifs_buf_release(pSMB); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2801 | |
| 2802 | if (rc == -EAGAIN) |
| 2803 | goto copyRetry; |
| 2804 | |
| 2805 | return rc; |
| 2806 | } |
| 2807 | |
| 2808 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2809 | CIFSUnixCreateSymLink(const unsigned int xid, struct cifs_tcon *tcon, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2810 | const char *fromName, const char *toName, |
Nakajima Akira | bc8ebdc4 | 2015-02-13 15:35:58 +0900 | [diff] [blame] | 2811 | const struct nls_table *nls_codepage, int remap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2812 | { |
| 2813 | TRANSACTION2_SPI_REQ *pSMB = NULL; |
| 2814 | TRANSACTION2_SPI_RSP *pSMBr = NULL; |
| 2815 | char *data_offset; |
| 2816 | int name_len; |
| 2817 | int name_len_target; |
| 2818 | int rc = 0; |
| 2819 | int bytes_returned = 0; |
| 2820 | __u16 params, param_offset, offset, byte_count; |
| 2821 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2822 | cifs_dbg(FYI, "In Symlink Unix style\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2823 | createSymLinkRetry: |
| 2824 | rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB, |
| 2825 | (void **) &pSMBr); |
| 2826 | if (rc) |
| 2827 | return rc; |
| 2828 | |
| 2829 | if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) { |
| 2830 | name_len = |
Nakajima Akira | bc8ebdc4 | 2015-02-13 15:35:58 +0900 | [diff] [blame] | 2831 | cifsConvertToUTF16((__le16 *) pSMB->FileName, fromName, |
| 2832 | /* find define for this maxpathcomponent */ |
| 2833 | PATH_MAX, nls_codepage, remap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2834 | name_len++; /* trailing null */ |
| 2835 | name_len *= 2; |
| 2836 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2837 | } else { /* BB improve the check for buffer overruns BB */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2838 | name_len = strnlen(fromName, PATH_MAX); |
| 2839 | name_len++; /* trailing null */ |
| 2840 | strncpy(pSMB->FileName, fromName, name_len); |
| 2841 | } |
| 2842 | params = 6 + name_len; |
| 2843 | pSMB->MaxSetupCount = 0; |
| 2844 | pSMB->Reserved = 0; |
| 2845 | pSMB->Flags = 0; |
| 2846 | pSMB->Timeout = 0; |
| 2847 | pSMB->Reserved2 = 0; |
| 2848 | param_offset = offsetof(struct smb_com_transaction2_spi_req, |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2849 | InformationLevel) - 4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2850 | offset = param_offset + params; |
| 2851 | |
| 2852 | data_offset = (char *) (&pSMB->hdr.Protocol) + offset; |
| 2853 | if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) { |
| 2854 | name_len_target = |
Nakajima Akira | bc8ebdc4 | 2015-02-13 15:35:58 +0900 | [diff] [blame] | 2855 | cifsConvertToUTF16((__le16 *) data_offset, toName, |
| 2856 | /* find define for this maxpathcomponent */ |
| 2857 | PATH_MAX, nls_codepage, remap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2858 | name_len_target++; /* trailing null */ |
| 2859 | name_len_target *= 2; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2860 | } else { /* BB improve the check for buffer overruns BB */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2861 | name_len_target = strnlen(toName, PATH_MAX); |
| 2862 | name_len_target++; /* trailing null */ |
| 2863 | strncpy(data_offset, toName, name_len_target); |
| 2864 | } |
| 2865 | |
| 2866 | pSMB->MaxParameterCount = cpu_to_le16(2); |
| 2867 | /* BB find exact max on data count below from sess */ |
| 2868 | pSMB->MaxDataCount = cpu_to_le16(1000); |
| 2869 | pSMB->SetupCount = 1; |
| 2870 | pSMB->Reserved3 = 0; |
| 2871 | pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION); |
| 2872 | byte_count = 3 /* pad */ + params + name_len_target; |
| 2873 | pSMB->DataCount = cpu_to_le16(name_len_target); |
| 2874 | pSMB->ParameterCount = cpu_to_le16(params); |
| 2875 | pSMB->TotalDataCount = pSMB->DataCount; |
| 2876 | pSMB->TotalParameterCount = pSMB->ParameterCount; |
| 2877 | pSMB->ParameterOffset = cpu_to_le16(param_offset); |
| 2878 | pSMB->DataOffset = cpu_to_le16(offset); |
| 2879 | pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_LINK); |
| 2880 | pSMB->Reserved4 = 0; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 2881 | inc_rfc1001_len(pSMB, byte_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2882 | pSMB->ByteCount = cpu_to_le16(byte_count); |
| 2883 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 2884 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
Pavel Shilovsky | 44c5818 | 2012-05-28 14:16:31 +0400 | [diff] [blame] | 2885 | cifs_stats_inc(&tcon->stats.cifs_stats.num_symlinks); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 2886 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2887 | cifs_dbg(FYI, "Send error in SetPathInfo create symlink = %d\n", |
| 2888 | rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2889 | |
Steve French | 0d817bc | 2008-05-22 02:02:03 +0000 | [diff] [blame] | 2890 | cifs_buf_release(pSMB); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2891 | |
| 2892 | if (rc == -EAGAIN) |
| 2893 | goto createSymLinkRetry; |
| 2894 | |
| 2895 | return rc; |
| 2896 | } |
| 2897 | |
| 2898 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2899 | CIFSUnixCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2900 | const char *fromName, const char *toName, |
Steve French | 737b758 | 2005-04-28 22:41:06 -0700 | [diff] [blame] | 2901 | const struct nls_table *nls_codepage, int remap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2902 | { |
| 2903 | TRANSACTION2_SPI_REQ *pSMB = NULL; |
| 2904 | TRANSACTION2_SPI_RSP *pSMBr = NULL; |
| 2905 | char *data_offset; |
| 2906 | int name_len; |
| 2907 | int name_len_target; |
| 2908 | int rc = 0; |
| 2909 | int bytes_returned = 0; |
| 2910 | __u16 params, param_offset, offset, byte_count; |
| 2911 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2912 | cifs_dbg(FYI, "In Create Hard link Unix style\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2913 | createHardLinkRetry: |
| 2914 | rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB, |
| 2915 | (void **) &pSMBr); |
| 2916 | if (rc) |
| 2917 | return rc; |
| 2918 | |
| 2919 | if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) { |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 2920 | name_len = cifsConvertToUTF16((__le16 *) pSMB->FileName, toName, |
| 2921 | PATH_MAX, nls_codepage, remap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2922 | name_len++; /* trailing null */ |
| 2923 | name_len *= 2; |
| 2924 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2925 | } else { /* BB improve the check for buffer overruns BB */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2926 | name_len = strnlen(toName, PATH_MAX); |
| 2927 | name_len++; /* trailing null */ |
| 2928 | strncpy(pSMB->FileName, toName, name_len); |
| 2929 | } |
| 2930 | params = 6 + name_len; |
| 2931 | pSMB->MaxSetupCount = 0; |
| 2932 | pSMB->Reserved = 0; |
| 2933 | pSMB->Flags = 0; |
| 2934 | pSMB->Timeout = 0; |
| 2935 | pSMB->Reserved2 = 0; |
| 2936 | param_offset = offsetof(struct smb_com_transaction2_spi_req, |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2937 | InformationLevel) - 4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2938 | offset = param_offset + params; |
| 2939 | |
| 2940 | data_offset = (char *) (&pSMB->hdr.Protocol) + offset; |
| 2941 | if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) { |
| 2942 | name_len_target = |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 2943 | cifsConvertToUTF16((__le16 *) data_offset, fromName, |
| 2944 | PATH_MAX, nls_codepage, remap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2945 | name_len_target++; /* trailing null */ |
| 2946 | name_len_target *= 2; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2947 | } else { /* BB improve the check for buffer overruns BB */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2948 | name_len_target = strnlen(fromName, PATH_MAX); |
| 2949 | name_len_target++; /* trailing null */ |
| 2950 | strncpy(data_offset, fromName, name_len_target); |
| 2951 | } |
| 2952 | |
| 2953 | pSMB->MaxParameterCount = cpu_to_le16(2); |
| 2954 | /* BB find exact max on data count below from sess*/ |
| 2955 | pSMB->MaxDataCount = cpu_to_le16(1000); |
| 2956 | pSMB->SetupCount = 1; |
| 2957 | pSMB->Reserved3 = 0; |
| 2958 | pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION); |
| 2959 | byte_count = 3 /* pad */ + params + name_len_target; |
| 2960 | pSMB->ParameterCount = cpu_to_le16(params); |
| 2961 | pSMB->TotalParameterCount = pSMB->ParameterCount; |
| 2962 | pSMB->DataCount = cpu_to_le16(name_len_target); |
| 2963 | pSMB->TotalDataCount = pSMB->DataCount; |
| 2964 | pSMB->ParameterOffset = cpu_to_le16(param_offset); |
| 2965 | pSMB->DataOffset = cpu_to_le16(offset); |
| 2966 | pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_HLINK); |
| 2967 | pSMB->Reserved4 = 0; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 2968 | inc_rfc1001_len(pSMB, byte_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2969 | pSMB->ByteCount = cpu_to_le16(byte_count); |
| 2970 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 2971 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
Pavel Shilovsky | 44c5818 | 2012-05-28 14:16:31 +0400 | [diff] [blame] | 2972 | cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 2973 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2974 | cifs_dbg(FYI, "Send error in SetPathInfo (hard link) = %d\n", |
| 2975 | rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2976 | |
| 2977 | cifs_buf_release(pSMB); |
| 2978 | if (rc == -EAGAIN) |
| 2979 | goto createHardLinkRetry; |
| 2980 | |
| 2981 | return rc; |
| 2982 | } |
| 2983 | |
| 2984 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2985 | CIFSCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon, |
Steve French | d6e906f | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 2986 | const char *from_name, const char *to_name, |
| 2987 | struct cifs_sb_info *cifs_sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2988 | { |
| 2989 | int rc = 0; |
| 2990 | NT_RENAME_REQ *pSMB = NULL; |
| 2991 | RENAME_RSP *pSMBr = NULL; |
| 2992 | int bytes_returned; |
| 2993 | int name_len, name_len2; |
| 2994 | __u16 count; |
Steve French | 2baa268 | 2014-09-27 02:19:01 -0500 | [diff] [blame] | 2995 | int remap = cifs_remap(cifs_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2996 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2997 | cifs_dbg(FYI, "In CIFSCreateHardLink\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2998 | winCreateHardLinkRetry: |
| 2999 | |
| 3000 | rc = smb_init(SMB_COM_NT_RENAME, 4, tcon, (void **) &pSMB, |
| 3001 | (void **) &pSMBr); |
| 3002 | if (rc) |
| 3003 | return rc; |
| 3004 | |
| 3005 | pSMB->SearchAttributes = |
| 3006 | cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM | |
| 3007 | ATTR_DIRECTORY); |
| 3008 | pSMB->Flags = cpu_to_le16(CREATE_HARD_LINK); |
| 3009 | pSMB->ClusterCount = 0; |
| 3010 | |
| 3011 | pSMB->BufferFormat = 0x04; |
| 3012 | |
| 3013 | if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) { |
| 3014 | name_len = |
Steve French | d6e906f | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 3015 | cifsConvertToUTF16((__le16 *) pSMB->OldFileName, from_name, |
| 3016 | PATH_MAX, cifs_sb->local_nls, remap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3017 | name_len++; /* trailing null */ |
| 3018 | name_len *= 2; |
Jeff Layton | fcc7c09 | 2009-02-28 12:59:03 -0500 | [diff] [blame] | 3019 | |
| 3020 | /* protocol specifies ASCII buffer format (0x04) for unicode */ |
| 3021 | pSMB->OldFileName[name_len] = 0x04; |
| 3022 | pSMB->OldFileName[name_len + 1] = 0x00; /* pad */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3023 | name_len2 = |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 3024 | cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2], |
Steve French | d6e906f | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 3025 | to_name, PATH_MAX, cifs_sb->local_nls, |
| 3026 | remap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3027 | name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ; |
| 3028 | name_len2 *= 2; /* convert to bytes */ |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3029 | } else { /* BB improve the check for buffer overruns BB */ |
Steve French | d6e906f | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 3030 | name_len = strnlen(from_name, PATH_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3031 | name_len++; /* trailing null */ |
Steve French | d6e906f | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 3032 | strncpy(pSMB->OldFileName, from_name, name_len); |
| 3033 | name_len2 = strnlen(to_name, PATH_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3034 | name_len2++; /* trailing null */ |
| 3035 | pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */ |
Steve French | d6e906f | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 3036 | strncpy(&pSMB->OldFileName[name_len + 1], to_name, name_len2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3037 | name_len2++; /* trailing null */ |
| 3038 | name_len2++; /* signature byte */ |
| 3039 | } |
| 3040 | |
| 3041 | count = 1 /* string type byte */ + name_len + name_len2; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 3042 | inc_rfc1001_len(pSMB, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3043 | pSMB->ByteCount = cpu_to_le16(count); |
| 3044 | |
| 3045 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 3046 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
Pavel Shilovsky | 44c5818 | 2012-05-28 14:16:31 +0400 | [diff] [blame] | 3047 | cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 3048 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3049 | 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] | 3050 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3051 | cifs_buf_release(pSMB); |
| 3052 | if (rc == -EAGAIN) |
| 3053 | goto winCreateHardLinkRetry; |
| 3054 | |
| 3055 | return rc; |
| 3056 | } |
| 3057 | |
| 3058 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 3059 | CIFSSMBUnixQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon, |
Jeff Layton | 460b969 | 2009-04-30 07:17:56 -0400 | [diff] [blame] | 3060 | const unsigned char *searchName, char **symlinkinfo, |
Nakajima Akira | bc8ebdc4 | 2015-02-13 15:35:58 +0900 | [diff] [blame] | 3061 | const struct nls_table *nls_codepage, int remap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3062 | { |
| 3063 | /* SMB_QUERY_FILE_UNIX_LINK */ |
| 3064 | TRANSACTION2_QPI_REQ *pSMB = NULL; |
| 3065 | TRANSACTION2_QPI_RSP *pSMBr = NULL; |
| 3066 | int rc = 0; |
| 3067 | int bytes_returned; |
| 3068 | int name_len; |
| 3069 | __u16 params, byte_count; |
Jeff Layton | 460b969 | 2009-04-30 07:17:56 -0400 | [diff] [blame] | 3070 | char *data_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3071 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3072 | cifs_dbg(FYI, "In QPathSymLinkInfo (Unix) for path %s\n", searchName); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3073 | |
| 3074 | querySymLinkRetry: |
| 3075 | rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB, |
| 3076 | (void **) &pSMBr); |
| 3077 | if (rc) |
| 3078 | return rc; |
| 3079 | |
| 3080 | if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) { |
| 3081 | name_len = |
Nakajima Akira | bc8ebdc4 | 2015-02-13 15:35:58 +0900 | [diff] [blame] | 3082 | cifsConvertToUTF16((__le16 *) pSMB->FileName, |
| 3083 | searchName, PATH_MAX, nls_codepage, |
| 3084 | remap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3085 | name_len++; /* trailing null */ |
| 3086 | name_len *= 2; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3087 | } else { /* BB improve the check for buffer overruns BB */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3088 | name_len = strnlen(searchName, PATH_MAX); |
| 3089 | name_len++; /* trailing null */ |
| 3090 | strncpy(pSMB->FileName, searchName, name_len); |
| 3091 | } |
| 3092 | |
| 3093 | params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ; |
| 3094 | pSMB->TotalDataCount = 0; |
| 3095 | pSMB->MaxParameterCount = cpu_to_le16(2); |
Jeff Layton | 46a7574 | 2009-05-24 18:45:17 -0400 | [diff] [blame] | 3096 | pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3097 | pSMB->MaxSetupCount = 0; |
| 3098 | pSMB->Reserved = 0; |
| 3099 | pSMB->Flags = 0; |
| 3100 | pSMB->Timeout = 0; |
| 3101 | pSMB->Reserved2 = 0; |
| 3102 | pSMB->ParameterOffset = cpu_to_le16(offsetof( |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3103 | struct smb_com_transaction2_qpi_req, InformationLevel) - 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3104 | pSMB->DataCount = 0; |
| 3105 | pSMB->DataOffset = 0; |
| 3106 | pSMB->SetupCount = 1; |
| 3107 | pSMB->Reserved3 = 0; |
| 3108 | pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION); |
| 3109 | byte_count = params + 1 /* pad */ ; |
| 3110 | pSMB->TotalParameterCount = cpu_to_le16(params); |
| 3111 | pSMB->ParameterCount = pSMB->TotalParameterCount; |
| 3112 | pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_LINK); |
| 3113 | pSMB->Reserved4 = 0; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 3114 | inc_rfc1001_len(pSMB, byte_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3115 | pSMB->ByteCount = cpu_to_le16(byte_count); |
| 3116 | |
| 3117 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 3118 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
| 3119 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3120 | cifs_dbg(FYI, "Send error in QuerySymLinkInfo = %d\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3121 | } else { |
| 3122 | /* decode response */ |
| 3123 | |
| 3124 | rc = validate_t2((struct smb_t2_rsp *)pSMBr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3125 | /* BB also check enough total bytes returned */ |
Jeff Layton | 820a803 | 2011-05-04 08:05:26 -0400 | [diff] [blame] | 3126 | if (rc || get_bcc(&pSMBr->hdr) < 2) |
Jeff Layton | 460b969 | 2009-04-30 07:17:56 -0400 | [diff] [blame] | 3127 | rc = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3128 | else { |
Steve French | 0e0d2cf | 2009-05-01 05:27:32 +0000 | [diff] [blame] | 3129 | bool is_unicode; |
Jeff Layton | 460b969 | 2009-04-30 07:17:56 -0400 | [diff] [blame] | 3130 | u16 count = le16_to_cpu(pSMBr->t2.DataCount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3131 | |
Jeff Layton | 460b969 | 2009-04-30 07:17:56 -0400 | [diff] [blame] | 3132 | data_start = ((char *) &pSMBr->hdr.Protocol) + |
| 3133 | le16_to_cpu(pSMBr->t2.DataOffset); |
| 3134 | |
Steve French | 0e0d2cf | 2009-05-01 05:27:32 +0000 | [diff] [blame] | 3135 | if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE) |
| 3136 | is_unicode = true; |
| 3137 | else |
| 3138 | is_unicode = false; |
| 3139 | |
Steve French | 737b758 | 2005-04-28 22:41:06 -0700 | [diff] [blame] | 3140 | /* BB FIXME investigate remapping reserved chars here */ |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 3141 | *symlinkinfo = cifs_strndup_from_utf16(data_start, |
| 3142 | count, is_unicode, nls_codepage); |
Jeff Layton | 8b6427a | 2009-05-19 09:57:03 -0400 | [diff] [blame] | 3143 | if (!*symlinkinfo) |
Jeff Layton | 460b969 | 2009-04-30 07:17:56 -0400 | [diff] [blame] | 3144 | rc = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3145 | } |
| 3146 | } |
| 3147 | cifs_buf_release(pSMB); |
| 3148 | if (rc == -EAGAIN) |
| 3149 | goto querySymLinkRetry; |
| 3150 | return rc; |
| 3151 | } |
| 3152 | |
Steve French | c52a955 | 2011-02-24 06:16:22 +0000 | [diff] [blame] | 3153 | /* |
| 3154 | * Recent Windows versions now create symlinks more frequently |
| 3155 | * and they use the "reparse point" mechanism below. We can of course |
| 3156 | * do symlinks nicely to Samba and other servers which support the |
| 3157 | * CIFS Unix Extensions and we can also do SFU symlinks and "client only" |
| 3158 | * "MF" symlinks optionally, but for recent Windows we really need to |
| 3159 | * reenable the code below and fix the cifs_symlink callers to handle this. |
| 3160 | * In the interim this code has been moved to its own config option so |
| 3161 | * it is not compiled in by default until callers fixed up and more tested. |
| 3162 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3163 | int |
Pavel Shilovsky | d244bf2 | 2013-08-14 19:25:22 +0400 | [diff] [blame] | 3164 | CIFSSMBQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon, |
| 3165 | __u16 fid, char **symlinkinfo, |
| 3166 | const struct nls_table *nls_codepage) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3167 | { |
| 3168 | int rc = 0; |
| 3169 | int bytes_returned; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3170 | struct smb_com_transaction_ioctl_req *pSMB; |
| 3171 | struct smb_com_transaction_ioctl_rsp *pSMBr; |
Pavel Shilovsky | d244bf2 | 2013-08-14 19:25:22 +0400 | [diff] [blame] | 3172 | bool is_unicode; |
| 3173 | unsigned int sub_len; |
| 3174 | char *sub_start; |
Steve French | c31f330 | 2013-09-28 18:24:12 -0500 | [diff] [blame] | 3175 | struct reparse_symlink_data *reparse_buf; |
| 3176 | struct reparse_posix_data *posix_buf; |
Pavel Shilovsky | d244bf2 | 2013-08-14 19:25:22 +0400 | [diff] [blame] | 3177 | __u32 data_offset, data_count; |
| 3178 | char *end_of_smb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3179 | |
Pavel Shilovsky | d244bf2 | 2013-08-14 19:25:22 +0400 | [diff] [blame] | 3180 | 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] | 3181 | rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB, |
| 3182 | (void **) &pSMBr); |
| 3183 | if (rc) |
| 3184 | return rc; |
| 3185 | |
| 3186 | pSMB->TotalParameterCount = 0 ; |
| 3187 | pSMB->TotalDataCount = 0; |
| 3188 | pSMB->MaxParameterCount = cpu_to_le32(2); |
| 3189 | /* BB find exact data count max from sess structure BB */ |
Jeff Layton | c974bef | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 3190 | pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3191 | pSMB->MaxSetupCount = 4; |
| 3192 | pSMB->Reserved = 0; |
| 3193 | pSMB->ParameterOffset = 0; |
| 3194 | pSMB->DataCount = 0; |
| 3195 | pSMB->DataOffset = 0; |
| 3196 | pSMB->SetupCount = 4; |
| 3197 | pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL); |
| 3198 | pSMB->ParameterCount = pSMB->TotalParameterCount; |
| 3199 | pSMB->FunctionCode = cpu_to_le32(FSCTL_GET_REPARSE_POINT); |
| 3200 | pSMB->IsFsctl = 1; /* FSCTL */ |
| 3201 | pSMB->IsRootFlag = 0; |
| 3202 | pSMB->Fid = fid; /* file handle always le */ |
| 3203 | pSMB->ByteCount = 0; |
| 3204 | |
| 3205 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 3206 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
| 3207 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3208 | cifs_dbg(FYI, "Send error in QueryReparseLinkInfo = %d\n", rc); |
Pavel Shilovsky | d244bf2 | 2013-08-14 19:25:22 +0400 | [diff] [blame] | 3209 | goto qreparse_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3210 | } |
Steve French | 989c7e5 | 2009-05-02 05:32:20 +0000 | [diff] [blame] | 3211 | |
Pavel Shilovsky | d244bf2 | 2013-08-14 19:25:22 +0400 | [diff] [blame] | 3212 | data_offset = le32_to_cpu(pSMBr->DataOffset); |
| 3213 | data_count = le32_to_cpu(pSMBr->DataCount); |
| 3214 | if (get_bcc(&pSMBr->hdr) < 2 || data_offset > 512) { |
| 3215 | /* BB also check enough total bytes returned */ |
| 3216 | rc = -EIO; /* bad smb */ |
| 3217 | goto qreparse_out; |
| 3218 | } |
| 3219 | if (!data_count || (data_count > 2048)) { |
| 3220 | rc = -EIO; |
| 3221 | cifs_dbg(FYI, "Invalid return data count on get reparse info ioctl\n"); |
| 3222 | goto qreparse_out; |
| 3223 | } |
| 3224 | end_of_smb = 2 + get_bcc(&pSMBr->hdr) + (char *)&pSMBr->ByteCount; |
Steve French | c31f330 | 2013-09-28 18:24:12 -0500 | [diff] [blame] | 3225 | reparse_buf = (struct reparse_symlink_data *) |
Pavel Shilovsky | d244bf2 | 2013-08-14 19:25:22 +0400 | [diff] [blame] | 3226 | ((char *)&pSMBr->hdr.Protocol + data_offset); |
| 3227 | if ((char *)reparse_buf >= end_of_smb) { |
| 3228 | rc = -EIO; |
| 3229 | goto qreparse_out; |
| 3230 | } |
Steve French | c31f330 | 2013-09-28 18:24:12 -0500 | [diff] [blame] | 3231 | if (reparse_buf->ReparseTag == cpu_to_le32(IO_REPARSE_TAG_NFS)) { |
| 3232 | cifs_dbg(FYI, "NFS style reparse tag\n"); |
| 3233 | posix_buf = (struct reparse_posix_data *)reparse_buf; |
| 3234 | |
| 3235 | if (posix_buf->InodeType != cpu_to_le64(NFS_SPECFILE_LNK)) { |
| 3236 | cifs_dbg(FYI, "unsupported file type 0x%llx\n", |
| 3237 | le64_to_cpu(posix_buf->InodeType)); |
| 3238 | rc = -EOPNOTSUPP; |
| 3239 | goto qreparse_out; |
| 3240 | } |
| 3241 | is_unicode = true; |
| 3242 | sub_len = le16_to_cpu(reparse_buf->ReparseDataLength); |
| 3243 | if (posix_buf->PathBuffer + sub_len > end_of_smb) { |
| 3244 | cifs_dbg(FYI, "reparse buf beyond SMB\n"); |
| 3245 | rc = -EIO; |
| 3246 | goto qreparse_out; |
| 3247 | } |
| 3248 | *symlinkinfo = cifs_strndup_from_utf16(posix_buf->PathBuffer, |
| 3249 | sub_len, is_unicode, nls_codepage); |
| 3250 | goto qreparse_out; |
| 3251 | } else if (reparse_buf->ReparseTag != |
| 3252 | cpu_to_le32(IO_REPARSE_TAG_SYMLINK)) { |
| 3253 | rc = -EOPNOTSUPP; |
| 3254 | goto qreparse_out; |
| 3255 | } |
| 3256 | |
| 3257 | /* Reparse tag is NTFS symlink */ |
| 3258 | sub_start = le16_to_cpu(reparse_buf->SubstituteNameOffset) + |
| 3259 | reparse_buf->PathBuffer; |
| 3260 | sub_len = le16_to_cpu(reparse_buf->SubstituteNameLength); |
| 3261 | if (sub_start + sub_len > end_of_smb) { |
Pavel Shilovsky | d244bf2 | 2013-08-14 19:25:22 +0400 | [diff] [blame] | 3262 | cifs_dbg(FYI, "reparse buf beyond SMB\n"); |
| 3263 | rc = -EIO; |
| 3264 | goto qreparse_out; |
| 3265 | } |
Pavel Shilovsky | d244bf2 | 2013-08-14 19:25:22 +0400 | [diff] [blame] | 3266 | if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE) |
| 3267 | is_unicode = true; |
| 3268 | else |
| 3269 | is_unicode = false; |
| 3270 | |
| 3271 | /* BB FIXME investigate remapping reserved chars here */ |
| 3272 | *symlinkinfo = cifs_strndup_from_utf16(sub_start, sub_len, is_unicode, |
| 3273 | nls_codepage); |
| 3274 | if (!*symlinkinfo) |
| 3275 | rc = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3276 | qreparse_out: |
Steve French | 4a6d87f | 2005-08-13 08:15:54 -0700 | [diff] [blame] | 3277 | cifs_buf_release(pSMB); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3278 | |
Pavel Shilovsky | d244bf2 | 2013-08-14 19:25:22 +0400 | [diff] [blame] | 3279 | /* |
| 3280 | * Note: On -EAGAIN error only caller can retry on handle based calls |
| 3281 | * since file handle passed in no longer valid. |
| 3282 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3283 | return rc; |
| 3284 | } |
| 3285 | |
Steve French | c7f508a | 2013-10-14 15:27:32 -0500 | [diff] [blame] | 3286 | int |
| 3287 | CIFSSMB_set_compression(const unsigned int xid, struct cifs_tcon *tcon, |
| 3288 | __u16 fid) |
| 3289 | { |
| 3290 | int rc = 0; |
| 3291 | int bytes_returned; |
| 3292 | struct smb_com_transaction_compr_ioctl_req *pSMB; |
| 3293 | struct smb_com_transaction_ioctl_rsp *pSMBr; |
| 3294 | |
| 3295 | cifs_dbg(FYI, "Set compression for %u\n", fid); |
| 3296 | rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB, |
| 3297 | (void **) &pSMBr); |
| 3298 | if (rc) |
| 3299 | return rc; |
| 3300 | |
| 3301 | pSMB->compression_state = cpu_to_le16(COMPRESSION_FORMAT_DEFAULT); |
| 3302 | |
| 3303 | pSMB->TotalParameterCount = 0; |
Fabian Frederick | bc09d14 | 2014-12-10 15:41:15 -0800 | [diff] [blame] | 3304 | pSMB->TotalDataCount = cpu_to_le32(2); |
Steve French | c7f508a | 2013-10-14 15:27:32 -0500 | [diff] [blame] | 3305 | pSMB->MaxParameterCount = 0; |
| 3306 | pSMB->MaxDataCount = 0; |
| 3307 | pSMB->MaxSetupCount = 4; |
| 3308 | pSMB->Reserved = 0; |
| 3309 | pSMB->ParameterOffset = 0; |
Fabian Frederick | bc09d14 | 2014-12-10 15:41:15 -0800 | [diff] [blame] | 3310 | pSMB->DataCount = cpu_to_le32(2); |
Steve French | c7f508a | 2013-10-14 15:27:32 -0500 | [diff] [blame] | 3311 | pSMB->DataOffset = |
| 3312 | cpu_to_le32(offsetof(struct smb_com_transaction_compr_ioctl_req, |
| 3313 | compression_state) - 4); /* 84 */ |
| 3314 | pSMB->SetupCount = 4; |
Fabian Frederick | bc09d14 | 2014-12-10 15:41:15 -0800 | [diff] [blame] | 3315 | pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL); |
Steve French | c7f508a | 2013-10-14 15:27:32 -0500 | [diff] [blame] | 3316 | pSMB->ParameterCount = 0; |
Fabian Frederick | bc09d14 | 2014-12-10 15:41:15 -0800 | [diff] [blame] | 3317 | pSMB->FunctionCode = cpu_to_le32(FSCTL_SET_COMPRESSION); |
Steve French | c7f508a | 2013-10-14 15:27:32 -0500 | [diff] [blame] | 3318 | pSMB->IsFsctl = 1; /* FSCTL */ |
| 3319 | pSMB->IsRootFlag = 0; |
| 3320 | pSMB->Fid = fid; /* file handle always le */ |
| 3321 | /* 3 byte pad, followed by 2 byte compress state */ |
Fabian Frederick | bc09d14 | 2014-12-10 15:41:15 -0800 | [diff] [blame] | 3322 | pSMB->ByteCount = cpu_to_le16(5); |
Steve French | c7f508a | 2013-10-14 15:27:32 -0500 | [diff] [blame] | 3323 | inc_rfc1001_len(pSMB, 5); |
| 3324 | |
| 3325 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 3326 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
| 3327 | if (rc) |
| 3328 | cifs_dbg(FYI, "Send error in SetCompression = %d\n", rc); |
| 3329 | |
| 3330 | cifs_buf_release(pSMB); |
| 3331 | |
| 3332 | /* |
| 3333 | * Note: On -EAGAIN error only caller can retry on handle based calls |
| 3334 | * since file handle passed in no longer valid. |
| 3335 | */ |
| 3336 | return rc; |
| 3337 | } |
| 3338 | |
| 3339 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3340 | #ifdef CONFIG_CIFS_POSIX |
| 3341 | |
| 3342 | /*Convert an Access Control Entry from wire format to local POSIX xattr format*/ |
Andreas Gruenbacher | 2211d5b | 2016-09-27 13:03:22 +0200 | [diff] [blame] | 3343 | static void cifs_convert_ace(struct posix_acl_xattr_entry *ace, |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3344 | struct cifs_posix_ace *cifs_ace) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3345 | { |
| 3346 | /* u8 cifs fields do not need le conversion */ |
Steve French | ff7feac | 2005-11-15 16:45:16 -0800 | [diff] [blame] | 3347 | ace->e_perm = cpu_to_le16(cifs_ace->cifs_e_perm); |
| 3348 | ace->e_tag = cpu_to_le16(cifs_ace->cifs_e_tag); |
| 3349 | 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] | 3350 | /* |
| 3351 | cifs_dbg(FYI, "perm %d tag %d id %d\n", |
| 3352 | ace->e_perm, ace->e_tag, ace->e_id); |
| 3353 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3354 | |
| 3355 | return; |
| 3356 | } |
| 3357 | |
| 3358 | /* 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] | 3359 | static int cifs_copy_posix_acl(char *trgt, char *src, const int buflen, |
| 3360 | const int acl_type, const int size_of_data_area) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3361 | { |
| 3362 | int size = 0; |
| 3363 | int i; |
| 3364 | __u16 count; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3365 | struct cifs_posix_ace *pACE; |
| 3366 | struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)src; |
Andreas Gruenbacher | 2211d5b | 2016-09-27 13:03:22 +0200 | [diff] [blame] | 3367 | struct posix_acl_xattr_header *local_acl = (void *)trgt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3368 | |
| 3369 | if (le16_to_cpu(cifs_acl->version) != CIFS_ACL_VERSION) |
| 3370 | return -EOPNOTSUPP; |
| 3371 | |
Andreas Gruenbacher | 45987e0 | 2016-04-14 00:30:14 +0200 | [diff] [blame] | 3372 | if (acl_type == ACL_TYPE_ACCESS) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3373 | count = le16_to_cpu(cifs_acl->access_entry_count); |
| 3374 | pACE = &cifs_acl->ace_array[0]; |
| 3375 | size = sizeof(struct cifs_posix_acl); |
| 3376 | size += sizeof(struct cifs_posix_ace) * count; |
| 3377 | /* check if we would go beyond end of SMB */ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 3378 | if (size_of_data_area < size) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3379 | cifs_dbg(FYI, "bad CIFS POSIX ACL size %d vs. %d\n", |
| 3380 | size_of_data_area, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3381 | return -EINVAL; |
| 3382 | } |
Andreas Gruenbacher | 45987e0 | 2016-04-14 00:30:14 +0200 | [diff] [blame] | 3383 | } else if (acl_type == ACL_TYPE_DEFAULT) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3384 | count = le16_to_cpu(cifs_acl->access_entry_count); |
| 3385 | size = sizeof(struct cifs_posix_acl); |
| 3386 | size += sizeof(struct cifs_posix_ace) * count; |
| 3387 | /* skip past access ACEs to get to default ACEs */ |
| 3388 | pACE = &cifs_acl->ace_array[count]; |
| 3389 | count = le16_to_cpu(cifs_acl->default_entry_count); |
| 3390 | size += sizeof(struct cifs_posix_ace) * count; |
| 3391 | /* check if we would go beyond end of SMB */ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 3392 | if (size_of_data_area < size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3393 | return -EINVAL; |
| 3394 | } else { |
| 3395 | /* illegal type */ |
| 3396 | return -EINVAL; |
| 3397 | } |
| 3398 | |
| 3399 | size = posix_acl_xattr_size(count); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 3400 | if ((buflen == 0) || (local_acl == NULL)) { |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3401 | /* used to query ACL EA size */ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 3402 | } else if (size > buflen) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3403 | return -ERANGE; |
| 3404 | } else /* buffer big enough */ { |
Andreas Gruenbacher | 2211d5b | 2016-09-27 13:03:22 +0200 | [diff] [blame] | 3405 | struct posix_acl_xattr_entry *ace = (void *)(local_acl + 1); |
| 3406 | |
Steve French | ff7feac | 2005-11-15 16:45:16 -0800 | [diff] [blame] | 3407 | local_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3408 | for (i = 0; i < count ; i++) { |
Andreas Gruenbacher | 2211d5b | 2016-09-27 13:03:22 +0200 | [diff] [blame] | 3409 | cifs_convert_ace(&ace[i], pACE); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3410 | pACE++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3411 | } |
| 3412 | } |
| 3413 | return size; |
| 3414 | } |
| 3415 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3416 | static __u16 convert_ace_to_cifs_ace(struct cifs_posix_ace *cifs_ace, |
Andreas Gruenbacher | 2211d5b | 2016-09-27 13:03:22 +0200 | [diff] [blame] | 3417 | const struct posix_acl_xattr_entry *local_ace) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3418 | { |
| 3419 | __u16 rc = 0; /* 0 = ACL converted ok */ |
| 3420 | |
Steve French | ff7feac | 2005-11-15 16:45:16 -0800 | [diff] [blame] | 3421 | cifs_ace->cifs_e_perm = le16_to_cpu(local_ace->e_perm); |
| 3422 | cifs_ace->cifs_e_tag = le16_to_cpu(local_ace->e_tag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3423 | /* BB is there a better way to handle the large uid? */ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 3424 | if (local_ace->e_id == cpu_to_le32(-1)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3425 | /* Probably no need to le convert -1 on any arch but can not hurt */ |
| 3426 | cifs_ace->cifs_uid = cpu_to_le64(-1); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3427 | } else |
Steve French | ff7feac | 2005-11-15 16:45:16 -0800 | [diff] [blame] | 3428 | 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] | 3429 | /* |
| 3430 | cifs_dbg(FYI, "perm %d tag %d id %d\n", |
| 3431 | ace->e_perm, ace->e_tag, ace->e_id); |
| 3432 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3433 | return rc; |
| 3434 | } |
| 3435 | |
| 3436 | /* 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] | 3437 | static __u16 ACL_to_cifs_posix(char *parm_data, const char *pACL, |
| 3438 | const int buflen, const int acl_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3439 | { |
| 3440 | __u16 rc = 0; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3441 | struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)parm_data; |
Andreas Gruenbacher | 2211d5b | 2016-09-27 13:03:22 +0200 | [diff] [blame] | 3442 | struct posix_acl_xattr_header *local_acl = (void *)pACL; |
Eryu Guan | ae9ebe7 | 2016-10-24 20:46:40 +0800 | [diff] [blame] | 3443 | struct posix_acl_xattr_entry *ace = (void *)(local_acl + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3444 | int count; |
| 3445 | int i; |
| 3446 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 3447 | if ((buflen == 0) || (pACL == NULL) || (cifs_acl == NULL)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3448 | return 0; |
| 3449 | |
| 3450 | count = posix_acl_xattr_count((size_t)buflen); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3451 | cifs_dbg(FYI, "setting acl with %d entries from buf of length %d and version of %d\n", |
| 3452 | count, buflen, le32_to_cpu(local_acl->a_version)); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 3453 | if (le32_to_cpu(local_acl->a_version) != 2) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3454 | cifs_dbg(FYI, "unknown POSIX ACL version %d\n", |
| 3455 | le32_to_cpu(local_acl->a_version)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3456 | return 0; |
| 3457 | } |
| 3458 | cifs_acl->version = cpu_to_le16(1); |
Steve French | b1d9335 | 2013-11-15 20:41:32 -0600 | [diff] [blame] | 3459 | if (acl_type == ACL_TYPE_ACCESS) { |
Steve French | ff7feac | 2005-11-15 16:45:16 -0800 | [diff] [blame] | 3460 | cifs_acl->access_entry_count = cpu_to_le16(count); |
Fabian Frederick | bc09d14 | 2014-12-10 15:41:15 -0800 | [diff] [blame] | 3461 | cifs_acl->default_entry_count = cpu_to_le16(0xFFFF); |
Steve French | b1d9335 | 2013-11-15 20:41:32 -0600 | [diff] [blame] | 3462 | } else if (acl_type == ACL_TYPE_DEFAULT) { |
Steve French | ff7feac | 2005-11-15 16:45:16 -0800 | [diff] [blame] | 3463 | cifs_acl->default_entry_count = cpu_to_le16(count); |
Fabian Frederick | bc09d14 | 2014-12-10 15:41:15 -0800 | [diff] [blame] | 3464 | cifs_acl->access_entry_count = cpu_to_le16(0xFFFF); |
Steve French | b1d9335 | 2013-11-15 20:41:32 -0600 | [diff] [blame] | 3465 | } else { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3466 | cifs_dbg(FYI, "unknown ACL type %d\n", acl_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3467 | return 0; |
| 3468 | } |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3469 | for (i = 0; i < count; i++) { |
Eryu Guan | ae9ebe7 | 2016-10-24 20:46:40 +0800 | [diff] [blame] | 3470 | rc = convert_ace_to_cifs_ace(&cifs_acl->ace_array[i], &ace[i]); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 3471 | if (rc != 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3472 | /* ACE not converted */ |
| 3473 | break; |
| 3474 | } |
| 3475 | } |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 3476 | if (rc == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3477 | rc = (__u16)(count * sizeof(struct cifs_posix_ace)); |
| 3478 | rc += sizeof(struct cifs_posix_acl); |
| 3479 | /* BB add check to make sure ACL does not overflow SMB */ |
| 3480 | } |
| 3481 | return rc; |
| 3482 | } |
| 3483 | |
| 3484 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 3485 | CIFSSMBGetPosixACL(const unsigned int xid, struct cifs_tcon *tcon, |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3486 | const unsigned char *searchName, |
| 3487 | char *acl_inf, const int buflen, const int acl_type, |
| 3488 | const struct nls_table *nls_codepage, int remap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3489 | { |
| 3490 | /* SMB_QUERY_POSIX_ACL */ |
| 3491 | TRANSACTION2_QPI_REQ *pSMB = NULL; |
| 3492 | TRANSACTION2_QPI_RSP *pSMBr = NULL; |
| 3493 | int rc = 0; |
| 3494 | int bytes_returned; |
| 3495 | int name_len; |
| 3496 | __u16 params, byte_count; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3497 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3498 | cifs_dbg(FYI, "In GetPosixACL (Unix) for path %s\n", searchName); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3499 | |
| 3500 | queryAclRetry: |
| 3501 | rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB, |
| 3502 | (void **) &pSMBr); |
| 3503 | if (rc) |
| 3504 | return rc; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3505 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3506 | if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) { |
| 3507 | name_len = |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 3508 | cifsConvertToUTF16((__le16 *) pSMB->FileName, |
| 3509 | searchName, PATH_MAX, nls_codepage, |
| 3510 | remap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3511 | name_len++; /* trailing null */ |
| 3512 | name_len *= 2; |
| 3513 | pSMB->FileName[name_len] = 0; |
| 3514 | pSMB->FileName[name_len+1] = 0; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3515 | } else { /* BB improve the check for buffer overruns BB */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3516 | name_len = strnlen(searchName, PATH_MAX); |
| 3517 | name_len++; /* trailing null */ |
| 3518 | strncpy(pSMB->FileName, searchName, name_len); |
| 3519 | } |
| 3520 | |
| 3521 | params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ; |
| 3522 | pSMB->TotalDataCount = 0; |
| 3523 | pSMB->MaxParameterCount = cpu_to_le16(2); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3524 | /* BB find exact max data count below from sess structure BB */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3525 | pSMB->MaxDataCount = cpu_to_le16(4000); |
| 3526 | pSMB->MaxSetupCount = 0; |
| 3527 | pSMB->Reserved = 0; |
| 3528 | pSMB->Flags = 0; |
| 3529 | pSMB->Timeout = 0; |
| 3530 | pSMB->Reserved2 = 0; |
| 3531 | pSMB->ParameterOffset = cpu_to_le16( |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3532 | offsetof(struct smb_com_transaction2_qpi_req, |
| 3533 | InformationLevel) - 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3534 | pSMB->DataCount = 0; |
| 3535 | pSMB->DataOffset = 0; |
| 3536 | pSMB->SetupCount = 1; |
| 3537 | pSMB->Reserved3 = 0; |
| 3538 | pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION); |
| 3539 | byte_count = params + 1 /* pad */ ; |
| 3540 | pSMB->TotalParameterCount = cpu_to_le16(params); |
| 3541 | pSMB->ParameterCount = pSMB->TotalParameterCount; |
| 3542 | pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_ACL); |
| 3543 | pSMB->Reserved4 = 0; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 3544 | inc_rfc1001_len(pSMB, byte_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3545 | pSMB->ByteCount = cpu_to_le16(byte_count); |
| 3546 | |
| 3547 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 3548 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
Pavel Shilovsky | 44c5818 | 2012-05-28 14:16:31 +0400 | [diff] [blame] | 3549 | cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3550 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3551 | cifs_dbg(FYI, "Send error in Query POSIX ACL = %d\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3552 | } else { |
| 3553 | /* decode response */ |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3554 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3555 | rc = validate_t2((struct smb_t2_rsp *)pSMBr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3556 | /* BB also check enough total bytes returned */ |
Jeff Layton | 820a803 | 2011-05-04 08:05:26 -0400 | [diff] [blame] | 3557 | if (rc || get_bcc(&pSMBr->hdr) < 2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3558 | rc = -EIO; /* bad smb */ |
| 3559 | else { |
| 3560 | __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset); |
| 3561 | __u16 count = le16_to_cpu(pSMBr->t2.DataCount); |
| 3562 | rc = cifs_copy_posix_acl(acl_inf, |
| 3563 | (char *)&pSMBr->hdr.Protocol+data_offset, |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3564 | buflen, acl_type, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3565 | } |
| 3566 | } |
| 3567 | cifs_buf_release(pSMB); |
| 3568 | if (rc == -EAGAIN) |
| 3569 | goto queryAclRetry; |
| 3570 | return rc; |
| 3571 | } |
| 3572 | |
| 3573 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 3574 | CIFSSMBSetPosixACL(const unsigned int xid, struct cifs_tcon *tcon, |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3575 | const unsigned char *fileName, |
| 3576 | const char *local_acl, const int buflen, |
| 3577 | const int acl_type, |
| 3578 | const struct nls_table *nls_codepage, int remap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3579 | { |
| 3580 | struct smb_com_transaction2_spi_req *pSMB = NULL; |
| 3581 | struct smb_com_transaction2_spi_rsp *pSMBr = NULL; |
| 3582 | char *parm_data; |
| 3583 | int name_len; |
| 3584 | int rc = 0; |
| 3585 | int bytes_returned = 0; |
| 3586 | __u16 params, byte_count, data_count, param_offset, offset; |
| 3587 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3588 | cifs_dbg(FYI, "In SetPosixACL (Unix) for path %s\n", fileName); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3589 | setAclRetry: |
| 3590 | rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB, |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3591 | (void **) &pSMBr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3592 | if (rc) |
| 3593 | return rc; |
| 3594 | if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) { |
| 3595 | name_len = |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 3596 | cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName, |
| 3597 | PATH_MAX, nls_codepage, remap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3598 | name_len++; /* trailing null */ |
| 3599 | name_len *= 2; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3600 | } else { /* BB improve the check for buffer overruns BB */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3601 | name_len = strnlen(fileName, PATH_MAX); |
| 3602 | name_len++; /* trailing null */ |
| 3603 | strncpy(pSMB->FileName, fileName, name_len); |
| 3604 | } |
| 3605 | params = 6 + name_len; |
| 3606 | pSMB->MaxParameterCount = cpu_to_le16(2); |
Steve French | 582d21e | 2008-05-13 04:54:12 +0000 | [diff] [blame] | 3607 | /* BB find max SMB size from sess */ |
| 3608 | pSMB->MaxDataCount = cpu_to_le16(1000); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3609 | pSMB->MaxSetupCount = 0; |
| 3610 | pSMB->Reserved = 0; |
| 3611 | pSMB->Flags = 0; |
| 3612 | pSMB->Timeout = 0; |
| 3613 | pSMB->Reserved2 = 0; |
| 3614 | param_offset = offsetof(struct smb_com_transaction2_spi_req, |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3615 | InformationLevel) - 4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3616 | offset = param_offset + params; |
| 3617 | parm_data = ((char *) &pSMB->hdr.Protocol) + offset; |
| 3618 | pSMB->ParameterOffset = cpu_to_le16(param_offset); |
| 3619 | |
| 3620 | /* convert to on the wire format for POSIX ACL */ |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3621 | 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] | 3622 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 3623 | if (data_count == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3624 | rc = -EOPNOTSUPP; |
| 3625 | goto setACLerrorExit; |
| 3626 | } |
| 3627 | pSMB->DataOffset = cpu_to_le16(offset); |
| 3628 | pSMB->SetupCount = 1; |
| 3629 | pSMB->Reserved3 = 0; |
| 3630 | pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION); |
| 3631 | pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_ACL); |
| 3632 | byte_count = 3 /* pad */ + params + data_count; |
| 3633 | pSMB->DataCount = cpu_to_le16(data_count); |
| 3634 | pSMB->TotalDataCount = pSMB->DataCount; |
| 3635 | pSMB->ParameterCount = cpu_to_le16(params); |
| 3636 | pSMB->TotalParameterCount = pSMB->ParameterCount; |
| 3637 | pSMB->Reserved4 = 0; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 3638 | inc_rfc1001_len(pSMB, byte_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3639 | pSMB->ByteCount = cpu_to_le16(byte_count); |
| 3640 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3641 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 3642 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3643 | cifs_dbg(FYI, "Set POSIX ACL returned %d\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3644 | |
| 3645 | setACLerrorExit: |
| 3646 | cifs_buf_release(pSMB); |
| 3647 | if (rc == -EAGAIN) |
| 3648 | goto setAclRetry; |
| 3649 | return rc; |
| 3650 | } |
| 3651 | |
Steve French | f654bac | 2005-04-28 22:41:04 -0700 | [diff] [blame] | 3652 | /* BB fix tabs in this function FIXME BB */ |
| 3653 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 3654 | CIFSGetExtAttr(const unsigned int xid, struct cifs_tcon *tcon, |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 3655 | const int netfid, __u64 *pExtAttrBits, __u64 *pMask) |
Steve French | f654bac | 2005-04-28 22:41:04 -0700 | [diff] [blame] | 3656 | { |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3657 | int rc = 0; |
| 3658 | struct smb_t2_qfi_req *pSMB = NULL; |
| 3659 | struct smb_t2_qfi_rsp *pSMBr = NULL; |
| 3660 | int bytes_returned; |
| 3661 | __u16 params, byte_count; |
Steve French | f654bac | 2005-04-28 22:41:04 -0700 | [diff] [blame] | 3662 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3663 | cifs_dbg(FYI, "In GetExtAttr\n"); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 3664 | if (tcon == NULL) |
| 3665 | return -ENODEV; |
Steve French | f654bac | 2005-04-28 22:41:04 -0700 | [diff] [blame] | 3666 | |
| 3667 | GetExtAttrRetry: |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 3668 | rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB, |
| 3669 | (void **) &pSMBr); |
| 3670 | if (rc) |
| 3671 | return rc; |
Steve French | f654bac | 2005-04-28 22:41:04 -0700 | [diff] [blame] | 3672 | |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 3673 | params = 2 /* level */ + 2 /* fid */; |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 3674 | pSMB->t2.TotalDataCount = 0; |
| 3675 | pSMB->t2.MaxParameterCount = cpu_to_le16(4); |
| 3676 | /* BB find exact max data count below from sess structure BB */ |
| 3677 | pSMB->t2.MaxDataCount = cpu_to_le16(4000); |
| 3678 | pSMB->t2.MaxSetupCount = 0; |
| 3679 | pSMB->t2.Reserved = 0; |
| 3680 | pSMB->t2.Flags = 0; |
| 3681 | pSMB->t2.Timeout = 0; |
| 3682 | pSMB->t2.Reserved2 = 0; |
| 3683 | pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req, |
| 3684 | Fid) - 4); |
| 3685 | pSMB->t2.DataCount = 0; |
| 3686 | pSMB->t2.DataOffset = 0; |
| 3687 | pSMB->t2.SetupCount = 1; |
| 3688 | pSMB->t2.Reserved3 = 0; |
| 3689 | pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION); |
| 3690 | byte_count = params + 1 /* pad */ ; |
| 3691 | pSMB->t2.TotalParameterCount = cpu_to_le16(params); |
| 3692 | pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount; |
| 3693 | pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_ATTR_FLAGS); |
| 3694 | pSMB->Pad = 0; |
Steve French | f654bac | 2005-04-28 22:41:04 -0700 | [diff] [blame] | 3695 | pSMB->Fid = netfid; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 3696 | inc_rfc1001_len(pSMB, byte_count); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 3697 | pSMB->t2.ByteCount = cpu_to_le16(byte_count); |
Steve French | f654bac | 2005-04-28 22:41:04 -0700 | [diff] [blame] | 3698 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 3699 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 3700 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
| 3701 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3702 | cifs_dbg(FYI, "error %d in GetExtAttr\n", rc); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 3703 | } else { |
| 3704 | /* decode response */ |
| 3705 | rc = validate_t2((struct smb_t2_rsp *)pSMBr); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 3706 | /* BB also check enough total bytes returned */ |
Jeff Layton | 820a803 | 2011-05-04 08:05:26 -0400 | [diff] [blame] | 3707 | if (rc || get_bcc(&pSMBr->hdr) < 2) |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 3708 | /* If rc should we check for EOPNOSUPP and |
| 3709 | disable the srvino flag? or in caller? */ |
| 3710 | rc = -EIO; /* bad smb */ |
| 3711 | else { |
| 3712 | __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset); |
| 3713 | __u16 count = le16_to_cpu(pSMBr->t2.DataCount); |
| 3714 | struct file_chattr_info *pfinfo; |
| 3715 | /* BB Do we need a cast or hash here ? */ |
| 3716 | if (count != 16) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3717 | cifs_dbg(FYI, "Illegal size ret in GetExtAttr\n"); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 3718 | rc = -EIO; |
| 3719 | goto GetExtAttrOut; |
| 3720 | } |
| 3721 | pfinfo = (struct file_chattr_info *) |
| 3722 | (data_offset + (char *) &pSMBr->hdr.Protocol); |
| 3723 | *pExtAttrBits = le64_to_cpu(pfinfo->mode); |
Steve French | f654bac | 2005-04-28 22:41:04 -0700 | [diff] [blame] | 3724 | *pMask = le64_to_cpu(pfinfo->mask); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 3725 | } |
| 3726 | } |
Steve French | f654bac | 2005-04-28 22:41:04 -0700 | [diff] [blame] | 3727 | GetExtAttrOut: |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 3728 | cifs_buf_release(pSMB); |
| 3729 | if (rc == -EAGAIN) |
| 3730 | goto GetExtAttrRetry; |
| 3731 | return rc; |
Steve French | f654bac | 2005-04-28 22:41:04 -0700 | [diff] [blame] | 3732 | } |
| 3733 | |
Steve French | f654bac | 2005-04-28 22:41:04 -0700 | [diff] [blame] | 3734 | #endif /* CONFIG_POSIX */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3735 | |
Jeff Layton | 79df1ba | 2010-12-06 12:52:08 -0500 | [diff] [blame] | 3736 | #ifdef CONFIG_CIFS_ACL |
| 3737 | /* |
| 3738 | * Initialize NT TRANSACT SMB into small smb request buffer. This assumes that |
| 3739 | * all NT TRANSACTS that we init here have total parm and data under about 400 |
| 3740 | * bytes (to fit in small cifs buffer size), which is the case so far, it |
| 3741 | * easily fits. NB: Setup words themselves and ByteCount MaxSetupCount (size of |
| 3742 | * returned setup area) and MaxParameterCount (returned parms size) must be set |
| 3743 | * by caller |
| 3744 | */ |
| 3745 | static int |
| 3746 | smb_init_nttransact(const __u16 sub_command, const int setup_count, |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 3747 | const int parm_len, struct cifs_tcon *tcon, |
Jeff Layton | 79df1ba | 2010-12-06 12:52:08 -0500 | [diff] [blame] | 3748 | void **ret_buf) |
| 3749 | { |
| 3750 | int rc; |
| 3751 | __u32 temp_offset; |
| 3752 | struct smb_com_ntransact_req *pSMB; |
| 3753 | |
| 3754 | rc = small_smb_init(SMB_COM_NT_TRANSACT, 19 + setup_count, tcon, |
| 3755 | (void **)&pSMB); |
| 3756 | if (rc) |
| 3757 | return rc; |
| 3758 | *ret_buf = (void *)pSMB; |
| 3759 | pSMB->Reserved = 0; |
| 3760 | pSMB->TotalParameterCount = cpu_to_le32(parm_len); |
| 3761 | pSMB->TotalDataCount = 0; |
Jeff Layton | c974bef | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 3762 | pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00); |
Jeff Layton | 79df1ba | 2010-12-06 12:52:08 -0500 | [diff] [blame] | 3763 | pSMB->ParameterCount = pSMB->TotalParameterCount; |
| 3764 | pSMB->DataCount = pSMB->TotalDataCount; |
| 3765 | temp_offset = offsetof(struct smb_com_ntransact_req, Parms) + |
| 3766 | (setup_count * 2) - 4 /* for rfc1001 length itself */; |
| 3767 | pSMB->ParameterOffset = cpu_to_le32(temp_offset); |
| 3768 | pSMB->DataOffset = cpu_to_le32(temp_offset + parm_len); |
| 3769 | pSMB->SetupCount = setup_count; /* no need to le convert byte fields */ |
| 3770 | pSMB->SubCommand = cpu_to_le16(sub_command); |
| 3771 | return 0; |
| 3772 | } |
| 3773 | |
| 3774 | static int |
| 3775 | validate_ntransact(char *buf, char **ppparm, char **ppdata, |
| 3776 | __u32 *pparmlen, __u32 *pdatalen) |
| 3777 | { |
| 3778 | char *end_of_smb; |
| 3779 | __u32 data_count, data_offset, parm_count, parm_offset; |
| 3780 | struct smb_com_ntransact_rsp *pSMBr; |
Jeff Layton | 820a803 | 2011-05-04 08:05:26 -0400 | [diff] [blame] | 3781 | u16 bcc; |
Jeff Layton | 79df1ba | 2010-12-06 12:52:08 -0500 | [diff] [blame] | 3782 | |
| 3783 | *pdatalen = 0; |
| 3784 | *pparmlen = 0; |
| 3785 | |
| 3786 | if (buf == NULL) |
| 3787 | return -EINVAL; |
| 3788 | |
| 3789 | pSMBr = (struct smb_com_ntransact_rsp *)buf; |
| 3790 | |
Jeff Layton | 820a803 | 2011-05-04 08:05:26 -0400 | [diff] [blame] | 3791 | bcc = get_bcc(&pSMBr->hdr); |
| 3792 | end_of_smb = 2 /* sizeof byte count */ + bcc + |
Jeff Layton | 79df1ba | 2010-12-06 12:52:08 -0500 | [diff] [blame] | 3793 | (char *)&pSMBr->ByteCount; |
| 3794 | |
| 3795 | data_offset = le32_to_cpu(pSMBr->DataOffset); |
| 3796 | data_count = le32_to_cpu(pSMBr->DataCount); |
| 3797 | parm_offset = le32_to_cpu(pSMBr->ParameterOffset); |
| 3798 | parm_count = le32_to_cpu(pSMBr->ParameterCount); |
| 3799 | |
| 3800 | *ppparm = (char *)&pSMBr->hdr.Protocol + parm_offset; |
| 3801 | *ppdata = (char *)&pSMBr->hdr.Protocol + data_offset; |
| 3802 | |
| 3803 | /* should we also check that parm and data areas do not overlap? */ |
| 3804 | if (*ppparm > end_of_smb) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3805 | cifs_dbg(FYI, "parms start after end of smb\n"); |
Jeff Layton | 79df1ba | 2010-12-06 12:52:08 -0500 | [diff] [blame] | 3806 | return -EINVAL; |
| 3807 | } else if (parm_count + *ppparm > end_of_smb) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3808 | cifs_dbg(FYI, "parm end after end of smb\n"); |
Jeff Layton | 79df1ba | 2010-12-06 12:52:08 -0500 | [diff] [blame] | 3809 | return -EINVAL; |
| 3810 | } else if (*ppdata > end_of_smb) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3811 | cifs_dbg(FYI, "data starts after end of smb\n"); |
Jeff Layton | 79df1ba | 2010-12-06 12:52:08 -0500 | [diff] [blame] | 3812 | return -EINVAL; |
| 3813 | } else if (data_count + *ppdata > end_of_smb) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3814 | cifs_dbg(FYI, "data %p + count %d (%p) past smb end %p start %p\n", |
| 3815 | *ppdata, data_count, (data_count + *ppdata), |
| 3816 | end_of_smb, pSMBr); |
Jeff Layton | 79df1ba | 2010-12-06 12:52:08 -0500 | [diff] [blame] | 3817 | return -EINVAL; |
Jeff Layton | 820a803 | 2011-05-04 08:05:26 -0400 | [diff] [blame] | 3818 | } else if (parm_count + data_count > bcc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3819 | cifs_dbg(FYI, "parm count and data count larger than SMB\n"); |
Jeff Layton | 79df1ba | 2010-12-06 12:52:08 -0500 | [diff] [blame] | 3820 | return -EINVAL; |
| 3821 | } |
| 3822 | *pdatalen = data_count; |
| 3823 | *pparmlen = parm_count; |
| 3824 | return 0; |
| 3825 | } |
| 3826 | |
Steve French | 0a4b92c | 2006-01-12 15:44:21 -0800 | [diff] [blame] | 3827 | /* Get Security Descriptor (by handle) from remote server for a file or dir */ |
| 3828 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 3829 | CIFSSMBGetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid, |
Steve French | 630f3f0c | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 3830 | struct cifs_ntsd **acl_inf, __u32 *pbuflen) |
Steve French | 0a4b92c | 2006-01-12 15:44:21 -0800 | [diff] [blame] | 3831 | { |
| 3832 | int rc = 0; |
| 3833 | int buf_type = 0; |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 3834 | QUERY_SEC_DESC_REQ *pSMB; |
Steve French | 0a4b92c | 2006-01-12 15:44:21 -0800 | [diff] [blame] | 3835 | struct kvec iov[1]; |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 3836 | struct kvec rsp_iov; |
Steve French | 0a4b92c | 2006-01-12 15:44:21 -0800 | [diff] [blame] | 3837 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3838 | cifs_dbg(FYI, "GetCifsACL\n"); |
Steve French | 0a4b92c | 2006-01-12 15:44:21 -0800 | [diff] [blame] | 3839 | |
Steve French | 630f3f0c | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 3840 | *pbuflen = 0; |
| 3841 | *acl_inf = NULL; |
| 3842 | |
Steve French | b9c7a2b | 2007-10-26 23:40:20 +0000 | [diff] [blame] | 3843 | rc = smb_init_nttransact(NT_TRANSACT_QUERY_SECURITY_DESC, 0, |
Steve French | 0a4b92c | 2006-01-12 15:44:21 -0800 | [diff] [blame] | 3844 | 8 /* parm len */, tcon, (void **) &pSMB); |
| 3845 | if (rc) |
| 3846 | return rc; |
| 3847 | |
| 3848 | pSMB->MaxParameterCount = cpu_to_le32(4); |
| 3849 | /* BB TEST with big acls that might need to be e.g. larger than 16K */ |
| 3850 | pSMB->MaxSetupCount = 0; |
| 3851 | pSMB->Fid = fid; /* file handle always le */ |
| 3852 | pSMB->AclFlags = cpu_to_le32(CIFS_ACL_OWNER | CIFS_ACL_GROUP | |
| 3853 | CIFS_ACL_DACL); |
| 3854 | pSMB->ByteCount = cpu_to_le16(11); /* 3 bytes pad + 8 bytes parm */ |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 3855 | inc_rfc1001_len(pSMB, 11); |
Steve French | 0a4b92c | 2006-01-12 15:44:21 -0800 | [diff] [blame] | 3856 | iov[0].iov_base = (char *)pSMB; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 3857 | 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] | 3858 | |
Steve French | a761ac5 | 2007-10-18 21:45:27 +0000 | [diff] [blame] | 3859 | rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovec */, &buf_type, |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 3860 | 0, &rsp_iov); |
| 3861 | cifs_small_buf_release(pSMB); |
Pavel Shilovsky | 44c5818 | 2012-05-28 14:16:31 +0400 | [diff] [blame] | 3862 | cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get); |
Steve French | 0a4b92c | 2006-01-12 15:44:21 -0800 | [diff] [blame] | 3863 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3864 | cifs_dbg(FYI, "Send error in QuerySecDesc = %d\n", rc); |
Steve French | 0a4b92c | 2006-01-12 15:44:21 -0800 | [diff] [blame] | 3865 | } else { /* decode response */ |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 3866 | __le32 *parm; |
Steve French | 630f3f0c | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 3867 | __u32 parm_len; |
| 3868 | __u32 acl_len; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3869 | struct smb_com_ntransact_rsp *pSMBr; |
Steve French | 630f3f0c | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 3870 | char *pdata; |
Steve French | 0a4b92c | 2006-01-12 15:44:21 -0800 | [diff] [blame] | 3871 | |
| 3872 | /* validate_nttransact */ |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 3873 | rc = validate_ntransact(rsp_iov.iov_base, (char **)&parm, |
Steve French | 630f3f0c | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 3874 | &pdata, &parm_len, pbuflen); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 3875 | if (rc) |
Steve French | 0a4b92c | 2006-01-12 15:44:21 -0800 | [diff] [blame] | 3876 | goto qsec_out; |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 3877 | pSMBr = (struct smb_com_ntransact_rsp *)rsp_iov.iov_base; |
Steve French | 0a4b92c | 2006-01-12 15:44:21 -0800 | [diff] [blame] | 3878 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3879 | cifs_dbg(FYI, "smb %p parm %p data %p\n", |
| 3880 | pSMBr, parm, *acl_inf); |
Steve French | 0a4b92c | 2006-01-12 15:44:21 -0800 | [diff] [blame] | 3881 | |
| 3882 | if (le32_to_cpu(pSMBr->ParameterCount) != 4) { |
| 3883 | rc = -EIO; /* bad smb */ |
Steve French | 630f3f0c | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 3884 | *pbuflen = 0; |
Steve French | 0a4b92c | 2006-01-12 15:44:21 -0800 | [diff] [blame] | 3885 | goto qsec_out; |
| 3886 | } |
| 3887 | |
| 3888 | /* BB check that data area is minimum length and as big as acl_len */ |
| 3889 | |
Steve French | af6f461 | 2007-10-16 18:40:37 +0000 | [diff] [blame] | 3890 | acl_len = le32_to_cpu(*parm); |
Steve French | 630f3f0c | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 3891 | if (acl_len != *pbuflen) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3892 | cifs_dbg(VFS, "acl length %d does not match %d\n", |
| 3893 | acl_len, *pbuflen); |
Steve French | 630f3f0c | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 3894 | if (*pbuflen > acl_len) |
| 3895 | *pbuflen = acl_len; |
| 3896 | } |
Steve French | 0a4b92c | 2006-01-12 15:44:21 -0800 | [diff] [blame] | 3897 | |
Steve French | 630f3f0c | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 3898 | /* check if buffer is big enough for the acl |
| 3899 | header followed by the smallest SID */ |
| 3900 | if ((*pbuflen < sizeof(struct cifs_ntsd) + 8) || |
| 3901 | (*pbuflen >= 64 * 1024)) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3902 | cifs_dbg(VFS, "bad acl length %d\n", *pbuflen); |
Steve French | 630f3f0c | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 3903 | rc = -EINVAL; |
| 3904 | *pbuflen = 0; |
| 3905 | } else { |
Silviu-Mihai Popescu | f7f7c18 | 2013-03-11 18:22:32 +0200 | [diff] [blame] | 3906 | *acl_inf = kmemdup(pdata, *pbuflen, GFP_KERNEL); |
Steve French | 630f3f0c | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 3907 | if (*acl_inf == NULL) { |
| 3908 | *pbuflen = 0; |
| 3909 | rc = -ENOMEM; |
| 3910 | } |
Steve French | 630f3f0c | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 3911 | } |
Steve French | 0a4b92c | 2006-01-12 15:44:21 -0800 | [diff] [blame] | 3912 | } |
| 3913 | qsec_out: |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 3914 | free_rsp_buf(buf_type, rsp_iov.iov_base); |
Steve French | 0a4b92c | 2006-01-12 15:44:21 -0800 | [diff] [blame] | 3915 | return rc; |
| 3916 | } |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 3917 | |
| 3918 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 3919 | CIFSSMBSetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid, |
Shirish Pargaonkar | a5ff376 | 2011-10-13 10:26:03 -0500 | [diff] [blame] | 3920 | struct cifs_ntsd *pntsd, __u32 acllen, int aclflag) |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 3921 | { |
| 3922 | __u16 byte_count, param_count, data_count, param_offset, data_offset; |
| 3923 | int rc = 0; |
| 3924 | int bytes_returned = 0; |
| 3925 | SET_SEC_DESC_REQ *pSMB = NULL; |
Jeff Layton | b2a3ad9 | 2012-03-26 09:55:29 -0400 | [diff] [blame] | 3926 | void *pSMBr; |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 3927 | |
| 3928 | setCifsAclRetry: |
Jeff Layton | b2a3ad9 | 2012-03-26 09:55:29 -0400 | [diff] [blame] | 3929 | rc = smb_init(SMB_COM_NT_TRANSACT, 19, tcon, (void **) &pSMB, &pSMBr); |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 3930 | if (rc) |
Jeff Layton | b2a3ad9 | 2012-03-26 09:55:29 -0400 | [diff] [blame] | 3931 | return rc; |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 3932 | |
| 3933 | pSMB->MaxSetupCount = 0; |
| 3934 | pSMB->Reserved = 0; |
| 3935 | |
| 3936 | param_count = 8; |
| 3937 | param_offset = offsetof(struct smb_com_transaction_ssec_req, Fid) - 4; |
| 3938 | data_count = acllen; |
| 3939 | data_offset = param_offset + param_count; |
| 3940 | byte_count = 3 /* pad */ + param_count; |
| 3941 | |
| 3942 | pSMB->DataCount = cpu_to_le32(data_count); |
| 3943 | pSMB->TotalDataCount = pSMB->DataCount; |
| 3944 | pSMB->MaxParameterCount = cpu_to_le32(4); |
| 3945 | pSMB->MaxDataCount = cpu_to_le32(16384); |
| 3946 | pSMB->ParameterCount = cpu_to_le32(param_count); |
| 3947 | pSMB->ParameterOffset = cpu_to_le32(param_offset); |
| 3948 | pSMB->TotalParameterCount = pSMB->ParameterCount; |
| 3949 | pSMB->DataOffset = cpu_to_le32(data_offset); |
| 3950 | pSMB->SetupCount = 0; |
| 3951 | pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_SET_SECURITY_DESC); |
| 3952 | pSMB->ByteCount = cpu_to_le16(byte_count+data_count); |
| 3953 | |
| 3954 | pSMB->Fid = fid; /* file handle always le */ |
| 3955 | pSMB->Reserved2 = 0; |
Shirish Pargaonkar | a5ff376 | 2011-10-13 10:26:03 -0500 | [diff] [blame] | 3956 | pSMB->AclFlags = cpu_to_le32(aclflag); |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 3957 | |
| 3958 | if (pntsd && acllen) { |
Jeff Layton | b2a3ad9 | 2012-03-26 09:55:29 -0400 | [diff] [blame] | 3959 | memcpy((char *)pSMBr + offsetof(struct smb_hdr, Protocol) + |
| 3960 | data_offset, pntsd, acllen); |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 3961 | inc_rfc1001_len(pSMB, byte_count + data_count); |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 3962 | } else |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 3963 | inc_rfc1001_len(pSMB, byte_count); |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 3964 | |
| 3965 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 3966 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
| 3967 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3968 | cifs_dbg(FYI, "SetCIFSACL bytes_returned: %d, rc: %d\n", |
| 3969 | bytes_returned, rc); |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 3970 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3971 | cifs_dbg(FYI, "Set CIFS ACL returned %d\n", rc); |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 3972 | cifs_buf_release(pSMB); |
| 3973 | |
| 3974 | if (rc == -EAGAIN) |
| 3975 | goto setCifsAclRetry; |
| 3976 | |
| 3977 | return (rc); |
| 3978 | } |
| 3979 | |
Jeff Layton | 79df1ba | 2010-12-06 12:52:08 -0500 | [diff] [blame] | 3980 | #endif /* CONFIG_CIFS_ACL */ |
Steve French | 0a4b92c | 2006-01-12 15:44:21 -0800 | [diff] [blame] | 3981 | |
Steve French | 6b8edfe | 2005-08-23 20:26:03 -0700 | [diff] [blame] | 3982 | /* Legacy Query Path Information call for lookup to old servers such |
| 3983 | as Win9x/WinME */ |
Pavel Shilovsky | 68889f2 | 2012-05-25 14:40:22 +0400 | [diff] [blame] | 3984 | int |
| 3985 | SMBQueryInformation(const unsigned int xid, struct cifs_tcon *tcon, |
| 3986 | const char *search_name, FILE_ALL_INFO *data, |
| 3987 | const struct nls_table *nls_codepage, int remap) |
Steve French | 6b8edfe | 2005-08-23 20:26:03 -0700 | [diff] [blame] | 3988 | { |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 3989 | QUERY_INFORMATION_REQ *pSMB; |
| 3990 | QUERY_INFORMATION_RSP *pSMBr; |
Steve French | 6b8edfe | 2005-08-23 20:26:03 -0700 | [diff] [blame] | 3991 | int rc = 0; |
| 3992 | int bytes_returned; |
| 3993 | int name_len; |
| 3994 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3995 | cifs_dbg(FYI, "In SMBQPath path %s\n", search_name); |
Steve French | 6b8edfe | 2005-08-23 20:26:03 -0700 | [diff] [blame] | 3996 | QInfRetry: |
| 3997 | rc = smb_init(SMB_COM_QUERY_INFORMATION, 0, tcon, (void **) &pSMB, |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3998 | (void **) &pSMBr); |
Steve French | 6b8edfe | 2005-08-23 20:26:03 -0700 | [diff] [blame] | 3999 | if (rc) |
| 4000 | return rc; |
| 4001 | |
| 4002 | if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) { |
| 4003 | name_len = |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 4004 | cifsConvertToUTF16((__le16 *) pSMB->FileName, |
Pavel Shilovsky | 68889f2 | 2012-05-25 14:40:22 +0400 | [diff] [blame] | 4005 | search_name, PATH_MAX, nls_codepage, |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 4006 | remap); |
Steve French | 6b8edfe | 2005-08-23 20:26:03 -0700 | [diff] [blame] | 4007 | name_len++; /* trailing null */ |
| 4008 | name_len *= 2; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4009 | } else { |
Pavel Shilovsky | 68889f2 | 2012-05-25 14:40:22 +0400 | [diff] [blame] | 4010 | name_len = strnlen(search_name, PATH_MAX); |
Steve French | 6b8edfe | 2005-08-23 20:26:03 -0700 | [diff] [blame] | 4011 | name_len++; /* trailing null */ |
Pavel Shilovsky | 68889f2 | 2012-05-25 14:40:22 +0400 | [diff] [blame] | 4012 | strncpy(pSMB->FileName, search_name, name_len); |
Steve French | 6b8edfe | 2005-08-23 20:26:03 -0700 | [diff] [blame] | 4013 | } |
| 4014 | pSMB->BufferFormat = 0x04; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4015 | name_len++; /* account for buffer type byte */ |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 4016 | inc_rfc1001_len(pSMB, (__u16)name_len); |
Steve French | 6b8edfe | 2005-08-23 20:26:03 -0700 | [diff] [blame] | 4017 | pSMB->ByteCount = cpu_to_le16(name_len); |
| 4018 | |
| 4019 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4020 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
Steve French | 6b8edfe | 2005-08-23 20:26:03 -0700 | [diff] [blame] | 4021 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4022 | cifs_dbg(FYI, "Send error in QueryInfo = %d\n", rc); |
Pavel Shilovsky | 68889f2 | 2012-05-25 14:40:22 +0400 | [diff] [blame] | 4023 | } else if (data) { |
Steve French | 1bd5bbc | 2006-09-28 03:35:57 +0000 | [diff] [blame] | 4024 | struct timespec ts; |
| 4025 | __u32 time = le32_to_cpu(pSMBr->last_write_time); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 4026 | |
| 4027 | /* decode response */ |
Steve French | 1bd5bbc | 2006-09-28 03:35:57 +0000 | [diff] [blame] | 4028 | /* BB FIXME - add time zone adjustment BB */ |
Pavel Shilovsky | 68889f2 | 2012-05-25 14:40:22 +0400 | [diff] [blame] | 4029 | memset(data, 0, sizeof(FILE_ALL_INFO)); |
Steve French | 1bd5bbc | 2006-09-28 03:35:57 +0000 | [diff] [blame] | 4030 | ts.tv_nsec = 0; |
| 4031 | ts.tv_sec = time; |
| 4032 | /* decode time fields */ |
Pavel Shilovsky | 68889f2 | 2012-05-25 14:40:22 +0400 | [diff] [blame] | 4033 | data->ChangeTime = cpu_to_le64(cifs_UnixTimeToNT(ts)); |
| 4034 | data->LastWriteTime = data->ChangeTime; |
| 4035 | data->LastAccessTime = 0; |
| 4036 | data->AllocationSize = |
Steve French | 70ca734 | 2005-09-22 16:32:06 -0700 | [diff] [blame] | 4037 | cpu_to_le64(le32_to_cpu(pSMBr->size)); |
Pavel Shilovsky | 68889f2 | 2012-05-25 14:40:22 +0400 | [diff] [blame] | 4038 | data->EndOfFile = data->AllocationSize; |
| 4039 | data->Attributes = |
Steve French | 70ca734 | 2005-09-22 16:32:06 -0700 | [diff] [blame] | 4040 | cpu_to_le32(le16_to_cpu(pSMBr->attr)); |
Steve French | 6b8edfe | 2005-08-23 20:26:03 -0700 | [diff] [blame] | 4041 | } else |
| 4042 | rc = -EIO; /* bad buffer passed in */ |
| 4043 | |
| 4044 | cifs_buf_release(pSMB); |
| 4045 | |
| 4046 | if (rc == -EAGAIN) |
| 4047 | goto QInfRetry; |
| 4048 | |
| 4049 | return rc; |
| 4050 | } |
| 4051 | |
Jeff Layton | bcd5357 | 2010-02-12 07:44:16 -0500 | [diff] [blame] | 4052 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 4053 | CIFSSMBQFileInfo(const unsigned int xid, struct cifs_tcon *tcon, |
Jeff Layton | bcd5357 | 2010-02-12 07:44:16 -0500 | [diff] [blame] | 4054 | u16 netfid, FILE_ALL_INFO *pFindData) |
| 4055 | { |
| 4056 | struct smb_t2_qfi_req *pSMB = NULL; |
| 4057 | struct smb_t2_qfi_rsp *pSMBr = NULL; |
| 4058 | int rc = 0; |
| 4059 | int bytes_returned; |
| 4060 | __u16 params, byte_count; |
Steve French | 6b8edfe | 2005-08-23 20:26:03 -0700 | [diff] [blame] | 4061 | |
Jeff Layton | bcd5357 | 2010-02-12 07:44:16 -0500 | [diff] [blame] | 4062 | QFileInfoRetry: |
| 4063 | rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB, |
| 4064 | (void **) &pSMBr); |
| 4065 | if (rc) |
| 4066 | return rc; |
Steve French | 6b8edfe | 2005-08-23 20:26:03 -0700 | [diff] [blame] | 4067 | |
Jeff Layton | bcd5357 | 2010-02-12 07:44:16 -0500 | [diff] [blame] | 4068 | params = 2 /* level */ + 2 /* fid */; |
| 4069 | pSMB->t2.TotalDataCount = 0; |
| 4070 | pSMB->t2.MaxParameterCount = cpu_to_le16(4); |
| 4071 | /* BB find exact max data count below from sess structure BB */ |
| 4072 | pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize); |
| 4073 | pSMB->t2.MaxSetupCount = 0; |
| 4074 | pSMB->t2.Reserved = 0; |
| 4075 | pSMB->t2.Flags = 0; |
| 4076 | pSMB->t2.Timeout = 0; |
| 4077 | pSMB->t2.Reserved2 = 0; |
| 4078 | pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req, |
| 4079 | Fid) - 4); |
| 4080 | pSMB->t2.DataCount = 0; |
| 4081 | pSMB->t2.DataOffset = 0; |
| 4082 | pSMB->t2.SetupCount = 1; |
| 4083 | pSMB->t2.Reserved3 = 0; |
| 4084 | pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION); |
| 4085 | byte_count = params + 1 /* pad */ ; |
| 4086 | pSMB->t2.TotalParameterCount = cpu_to_le16(params); |
| 4087 | pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount; |
| 4088 | pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO); |
| 4089 | pSMB->Pad = 0; |
| 4090 | pSMB->Fid = netfid; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 4091 | inc_rfc1001_len(pSMB, byte_count); |
David Disseldorp | 7ac0feb | 2013-06-28 11:47:33 +0200 | [diff] [blame] | 4092 | pSMB->t2.ByteCount = cpu_to_le16(byte_count); |
Jeff Layton | bcd5357 | 2010-02-12 07:44:16 -0500 | [diff] [blame] | 4093 | |
| 4094 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 4095 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
| 4096 | if (rc) { |
Steve French | ebcc943 | 2013-12-09 09:18:09 -0600 | [diff] [blame] | 4097 | cifs_dbg(FYI, "Send error in QFileInfo = %d", rc); |
Jeff Layton | bcd5357 | 2010-02-12 07:44:16 -0500 | [diff] [blame] | 4098 | } else { /* decode response */ |
| 4099 | rc = validate_t2((struct smb_t2_rsp *)pSMBr); |
| 4100 | |
| 4101 | if (rc) /* BB add auto retry on EOPNOTSUPP? */ |
| 4102 | rc = -EIO; |
Jeff Layton | 820a803 | 2011-05-04 08:05:26 -0400 | [diff] [blame] | 4103 | else if (get_bcc(&pSMBr->hdr) < 40) |
Jeff Layton | bcd5357 | 2010-02-12 07:44:16 -0500 | [diff] [blame] | 4104 | rc = -EIO; /* bad smb */ |
| 4105 | else if (pFindData) { |
| 4106 | __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset); |
| 4107 | memcpy((char *) pFindData, |
| 4108 | (char *) &pSMBr->hdr.Protocol + |
| 4109 | data_offset, sizeof(FILE_ALL_INFO)); |
| 4110 | } else |
| 4111 | rc = -ENOMEM; |
| 4112 | } |
| 4113 | cifs_buf_release(pSMB); |
| 4114 | if (rc == -EAGAIN) |
| 4115 | goto QFileInfoRetry; |
| 4116 | |
| 4117 | return rc; |
| 4118 | } |
Steve French | 6b8edfe | 2005-08-23 20:26:03 -0700 | [diff] [blame] | 4119 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4120 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 4121 | CIFSSMBQPathInfo(const unsigned int xid, struct cifs_tcon *tcon, |
Pavel Shilovsky | 68889f2 | 2012-05-25 14:40:22 +0400 | [diff] [blame] | 4122 | const char *search_name, FILE_ALL_INFO *data, |
Steve French | acf1a1b | 2006-10-12 03:28:28 +0000 | [diff] [blame] | 4123 | int legacy /* old style infolevel */, |
Steve French | 737b758 | 2005-04-28 22:41:06 -0700 | [diff] [blame] | 4124 | const struct nls_table *nls_codepage, int remap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4125 | { |
Pavel Shilovsky | 68889f2 | 2012-05-25 14:40:22 +0400 | [diff] [blame] | 4126 | /* level 263 SMB_QUERY_FILE_ALL_INFO */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4127 | TRANSACTION2_QPI_REQ *pSMB = NULL; |
| 4128 | TRANSACTION2_QPI_RSP *pSMBr = NULL; |
| 4129 | int rc = 0; |
| 4130 | int bytes_returned; |
| 4131 | int name_len; |
| 4132 | __u16 params, byte_count; |
| 4133 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4134 | /* cifs_dbg(FYI, "In QPathInfo path %s\n", search_name); */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4135 | QPathInfoRetry: |
| 4136 | rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB, |
| 4137 | (void **) &pSMBr); |
| 4138 | if (rc) |
| 4139 | return rc; |
| 4140 | |
| 4141 | if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) { |
| 4142 | name_len = |
Pavel Shilovsky | 68889f2 | 2012-05-25 14:40:22 +0400 | [diff] [blame] | 4143 | cifsConvertToUTF16((__le16 *) pSMB->FileName, search_name, |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 4144 | PATH_MAX, nls_codepage, remap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4145 | name_len++; /* trailing null */ |
| 4146 | name_len *= 2; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4147 | } else { /* BB improve the check for buffer overruns BB */ |
Pavel Shilovsky | 68889f2 | 2012-05-25 14:40:22 +0400 | [diff] [blame] | 4148 | name_len = strnlen(search_name, PATH_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4149 | name_len++; /* trailing null */ |
Pavel Shilovsky | 68889f2 | 2012-05-25 14:40:22 +0400 | [diff] [blame] | 4150 | strncpy(pSMB->FileName, search_name, name_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4151 | } |
| 4152 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4153 | params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4154 | pSMB->TotalDataCount = 0; |
| 4155 | pSMB->MaxParameterCount = cpu_to_le16(2); |
Steve French | 582d21e | 2008-05-13 04:54:12 +0000 | [diff] [blame] | 4156 | /* BB find exact max SMB PDU from sess structure BB */ |
| 4157 | pSMB->MaxDataCount = cpu_to_le16(4000); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4158 | pSMB->MaxSetupCount = 0; |
| 4159 | pSMB->Reserved = 0; |
| 4160 | pSMB->Flags = 0; |
| 4161 | pSMB->Timeout = 0; |
| 4162 | pSMB->Reserved2 = 0; |
| 4163 | pSMB->ParameterOffset = cpu_to_le16(offsetof( |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4164 | struct smb_com_transaction2_qpi_req, InformationLevel) - 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4165 | pSMB->DataCount = 0; |
| 4166 | pSMB->DataOffset = 0; |
| 4167 | pSMB->SetupCount = 1; |
| 4168 | pSMB->Reserved3 = 0; |
| 4169 | pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION); |
| 4170 | byte_count = params + 1 /* pad */ ; |
| 4171 | pSMB->TotalParameterCount = cpu_to_le16(params); |
| 4172 | pSMB->ParameterCount = pSMB->TotalParameterCount; |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 4173 | if (legacy) |
Steve French | acf1a1b | 2006-10-12 03:28:28 +0000 | [diff] [blame] | 4174 | pSMB->InformationLevel = cpu_to_le16(SMB_INFO_STANDARD); |
| 4175 | else |
| 4176 | pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4177 | pSMB->Reserved4 = 0; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 4178 | inc_rfc1001_len(pSMB, byte_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4179 | pSMB->ByteCount = cpu_to_le16(byte_count); |
| 4180 | |
| 4181 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 4182 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
| 4183 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4184 | cifs_dbg(FYI, "Send error in QPathInfo = %d\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4185 | } else { /* decode response */ |
| 4186 | rc = validate_t2((struct smb_t2_rsp *)pSMBr); |
| 4187 | |
Steve French | acf1a1b | 2006-10-12 03:28:28 +0000 | [diff] [blame] | 4188 | if (rc) /* BB add auto retry on EOPNOTSUPP? */ |
| 4189 | rc = -EIO; |
Jeff Layton | 820a803 | 2011-05-04 08:05:26 -0400 | [diff] [blame] | 4190 | else if (!legacy && get_bcc(&pSMBr->hdr) < 40) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4191 | rc = -EIO; /* bad smb */ |
Jeff Layton | 820a803 | 2011-05-04 08:05:26 -0400 | [diff] [blame] | 4192 | else if (legacy && get_bcc(&pSMBr->hdr) < 24) |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4193 | rc = -EIO; /* 24 or 26 expected but we do not read |
| 4194 | last field */ |
Pavel Shilovsky | 68889f2 | 2012-05-25 14:40:22 +0400 | [diff] [blame] | 4195 | else if (data) { |
Steve French | acf1a1b | 2006-10-12 03:28:28 +0000 | [diff] [blame] | 4196 | int size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4197 | __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 4198 | |
Pavel Shilovsky | 68889f2 | 2012-05-25 14:40:22 +0400 | [diff] [blame] | 4199 | /* |
| 4200 | * On legacy responses we do not read the last field, |
| 4201 | * EAsize, fortunately since it varies by subdialect and |
| 4202 | * also note it differs on Set vs Get, ie two bytes or 4 |
| 4203 | * bytes depending but we don't care here. |
| 4204 | */ |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 4205 | if (legacy) |
Steve French | acf1a1b | 2006-10-12 03:28:28 +0000 | [diff] [blame] | 4206 | size = sizeof(FILE_INFO_STANDARD); |
| 4207 | else |
| 4208 | size = sizeof(FILE_ALL_INFO); |
Pavel Shilovsky | 68889f2 | 2012-05-25 14:40:22 +0400 | [diff] [blame] | 4209 | memcpy((char *) data, (char *) &pSMBr->hdr.Protocol + |
Steve French | acf1a1b | 2006-10-12 03:28:28 +0000 | [diff] [blame] | 4210 | data_offset, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4211 | } else |
| 4212 | rc = -ENOMEM; |
| 4213 | } |
| 4214 | cifs_buf_release(pSMB); |
| 4215 | if (rc == -EAGAIN) |
| 4216 | goto QPathInfoRetry; |
| 4217 | |
| 4218 | return rc; |
| 4219 | } |
| 4220 | |
| 4221 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 4222 | CIFSSMBUnixQFileInfo(const unsigned int xid, struct cifs_tcon *tcon, |
Jeff Layton | c8634fd | 2010-02-12 07:44:17 -0500 | [diff] [blame] | 4223 | u16 netfid, FILE_UNIX_BASIC_INFO *pFindData) |
| 4224 | { |
| 4225 | struct smb_t2_qfi_req *pSMB = NULL; |
| 4226 | struct smb_t2_qfi_rsp *pSMBr = NULL; |
| 4227 | int rc = 0; |
| 4228 | int bytes_returned; |
| 4229 | __u16 params, byte_count; |
| 4230 | |
| 4231 | UnixQFileInfoRetry: |
| 4232 | rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB, |
| 4233 | (void **) &pSMBr); |
| 4234 | if (rc) |
| 4235 | return rc; |
| 4236 | |
| 4237 | params = 2 /* level */ + 2 /* fid */; |
| 4238 | pSMB->t2.TotalDataCount = 0; |
| 4239 | pSMB->t2.MaxParameterCount = cpu_to_le16(4); |
| 4240 | /* BB find exact max data count below from sess structure BB */ |
| 4241 | pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize); |
| 4242 | pSMB->t2.MaxSetupCount = 0; |
| 4243 | pSMB->t2.Reserved = 0; |
| 4244 | pSMB->t2.Flags = 0; |
| 4245 | pSMB->t2.Timeout = 0; |
| 4246 | pSMB->t2.Reserved2 = 0; |
| 4247 | pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req, |
| 4248 | Fid) - 4); |
| 4249 | pSMB->t2.DataCount = 0; |
| 4250 | pSMB->t2.DataOffset = 0; |
| 4251 | pSMB->t2.SetupCount = 1; |
| 4252 | pSMB->t2.Reserved3 = 0; |
| 4253 | pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION); |
| 4254 | byte_count = params + 1 /* pad */ ; |
| 4255 | pSMB->t2.TotalParameterCount = cpu_to_le16(params); |
| 4256 | pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount; |
| 4257 | pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC); |
| 4258 | pSMB->Pad = 0; |
| 4259 | pSMB->Fid = netfid; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 4260 | inc_rfc1001_len(pSMB, byte_count); |
David Disseldorp | 7ac0feb | 2013-06-28 11:47:33 +0200 | [diff] [blame] | 4261 | pSMB->t2.ByteCount = cpu_to_le16(byte_count); |
Jeff Layton | c8634fd | 2010-02-12 07:44:17 -0500 | [diff] [blame] | 4262 | |
| 4263 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 4264 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
| 4265 | if (rc) { |
Steve French | ebcc943 | 2013-12-09 09:18:09 -0600 | [diff] [blame] | 4266 | cifs_dbg(FYI, "Send error in UnixQFileInfo = %d", rc); |
Jeff Layton | c8634fd | 2010-02-12 07:44:17 -0500 | [diff] [blame] | 4267 | } else { /* decode response */ |
| 4268 | rc = validate_t2((struct smb_t2_rsp *)pSMBr); |
| 4269 | |
Jeff Layton | 820a803 | 2011-05-04 08:05:26 -0400 | [diff] [blame] | 4270 | if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4271 | 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] | 4272 | rc = -EIO; /* bad smb */ |
| 4273 | } else { |
| 4274 | __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset); |
| 4275 | memcpy((char *) pFindData, |
| 4276 | (char *) &pSMBr->hdr.Protocol + |
| 4277 | data_offset, |
| 4278 | sizeof(FILE_UNIX_BASIC_INFO)); |
| 4279 | } |
| 4280 | } |
| 4281 | |
| 4282 | cifs_buf_release(pSMB); |
| 4283 | if (rc == -EAGAIN) |
| 4284 | goto UnixQFileInfoRetry; |
| 4285 | |
| 4286 | return rc; |
| 4287 | } |
| 4288 | |
| 4289 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 4290 | CIFSSMBUnixQPathInfo(const unsigned int xid, struct cifs_tcon *tcon, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4291 | const unsigned char *searchName, |
Steve French | 582d21e | 2008-05-13 04:54:12 +0000 | [diff] [blame] | 4292 | FILE_UNIX_BASIC_INFO *pFindData, |
Steve French | 737b758 | 2005-04-28 22:41:06 -0700 | [diff] [blame] | 4293 | const struct nls_table *nls_codepage, int remap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4294 | { |
| 4295 | /* SMB_QUERY_FILE_UNIX_BASIC */ |
| 4296 | TRANSACTION2_QPI_REQ *pSMB = NULL; |
| 4297 | TRANSACTION2_QPI_RSP *pSMBr = NULL; |
| 4298 | int rc = 0; |
| 4299 | int bytes_returned = 0; |
| 4300 | int name_len; |
| 4301 | __u16 params, byte_count; |
| 4302 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4303 | cifs_dbg(FYI, "In QPathInfo (Unix) the path %s\n", searchName); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4304 | UnixQPathInfoRetry: |
| 4305 | rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB, |
| 4306 | (void **) &pSMBr); |
| 4307 | if (rc) |
| 4308 | return rc; |
| 4309 | |
| 4310 | if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) { |
| 4311 | name_len = |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 4312 | cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName, |
| 4313 | PATH_MAX, nls_codepage, remap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4314 | name_len++; /* trailing null */ |
| 4315 | name_len *= 2; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4316 | } else { /* BB improve the check for buffer overruns BB */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4317 | name_len = strnlen(searchName, PATH_MAX); |
| 4318 | name_len++; /* trailing null */ |
| 4319 | strncpy(pSMB->FileName, searchName, name_len); |
| 4320 | } |
| 4321 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4322 | params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4323 | pSMB->TotalDataCount = 0; |
| 4324 | pSMB->MaxParameterCount = cpu_to_le16(2); |
| 4325 | /* BB find exact max SMB PDU from sess structure BB */ |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4326 | pSMB->MaxDataCount = cpu_to_le16(4000); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4327 | pSMB->MaxSetupCount = 0; |
| 4328 | pSMB->Reserved = 0; |
| 4329 | pSMB->Flags = 0; |
| 4330 | pSMB->Timeout = 0; |
| 4331 | pSMB->Reserved2 = 0; |
| 4332 | pSMB->ParameterOffset = cpu_to_le16(offsetof( |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4333 | struct smb_com_transaction2_qpi_req, InformationLevel) - 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4334 | pSMB->DataCount = 0; |
| 4335 | pSMB->DataOffset = 0; |
| 4336 | pSMB->SetupCount = 1; |
| 4337 | pSMB->Reserved3 = 0; |
| 4338 | pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION); |
| 4339 | byte_count = params + 1 /* pad */ ; |
| 4340 | pSMB->TotalParameterCount = cpu_to_le16(params); |
| 4341 | pSMB->ParameterCount = pSMB->TotalParameterCount; |
| 4342 | pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC); |
| 4343 | pSMB->Reserved4 = 0; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 4344 | inc_rfc1001_len(pSMB, byte_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4345 | pSMB->ByteCount = cpu_to_le16(byte_count); |
| 4346 | |
| 4347 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 4348 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
| 4349 | if (rc) { |
Steve French | ebcc943 | 2013-12-09 09:18:09 -0600 | [diff] [blame] | 4350 | cifs_dbg(FYI, "Send error in UnixQPathInfo = %d", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4351 | } else { /* decode response */ |
| 4352 | rc = validate_t2((struct smb_t2_rsp *)pSMBr); |
| 4353 | |
Jeff Layton | 820a803 | 2011-05-04 08:05:26 -0400 | [diff] [blame] | 4354 | if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4355 | 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] | 4356 | rc = -EIO; /* bad smb */ |
| 4357 | } else { |
| 4358 | __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset); |
| 4359 | memcpy((char *) pFindData, |
| 4360 | (char *) &pSMBr->hdr.Protocol + |
| 4361 | data_offset, |
Steve French | 630f3f0c | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 4362 | sizeof(FILE_UNIX_BASIC_INFO)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4363 | } |
| 4364 | } |
| 4365 | cifs_buf_release(pSMB); |
| 4366 | if (rc == -EAGAIN) |
| 4367 | goto UnixQPathInfoRetry; |
| 4368 | |
| 4369 | return rc; |
| 4370 | } |
| 4371 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4372 | /* xid, tcon, searchName and codepage are input parms, rest are returned */ |
| 4373 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 4374 | CIFSFindFirst(const unsigned int xid, struct cifs_tcon *tcon, |
Shirish Pargaonkar | c052e2b | 2012-09-28 12:21:14 -0500 | [diff] [blame] | 4375 | const char *searchName, struct cifs_sb_info *cifs_sb, |
Shirish Pargaonkar | 2608bee | 2012-05-15 10:19:16 -0500 | [diff] [blame] | 4376 | __u16 *pnetfid, __u16 search_flags, |
Shirish Pargaonkar | c052e2b | 2012-09-28 12:21:14 -0500 | [diff] [blame] | 4377 | struct cifs_search_info *psrch_inf, bool msearch) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4378 | { |
| 4379 | /* level 257 SMB_ */ |
| 4380 | TRANSACTION2_FFIRST_REQ *pSMB = NULL; |
| 4381 | TRANSACTION2_FFIRST_RSP *pSMBr = NULL; |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 4382 | T2_FFIRST_RSP_PARMS *parms; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4383 | int rc = 0; |
| 4384 | int bytes_returned = 0; |
Shirish Pargaonkar | c052e2b | 2012-09-28 12:21:14 -0500 | [diff] [blame] | 4385 | int name_len, remap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4386 | __u16 params, byte_count; |
Shirish Pargaonkar | c052e2b | 2012-09-28 12:21:14 -0500 | [diff] [blame] | 4387 | struct nls_table *nls_codepage; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4388 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4389 | cifs_dbg(FYI, "In FindFirst for %s\n", searchName); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4390 | |
| 4391 | findFirstRetry: |
| 4392 | rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB, |
| 4393 | (void **) &pSMBr); |
| 4394 | if (rc) |
| 4395 | return rc; |
| 4396 | |
Shirish Pargaonkar | c052e2b | 2012-09-28 12:21:14 -0500 | [diff] [blame] | 4397 | nls_codepage = cifs_sb->local_nls; |
Steve French | 2baa268 | 2014-09-27 02:19:01 -0500 | [diff] [blame] | 4398 | remap = cifs_remap(cifs_sb); |
Shirish Pargaonkar | c052e2b | 2012-09-28 12:21:14 -0500 | [diff] [blame] | 4399 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4400 | if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) { |
| 4401 | name_len = |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 4402 | cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName, |
| 4403 | PATH_MAX, nls_codepage, remap); |
Steve French | 737b758 | 2005-04-28 22:41:06 -0700 | [diff] [blame] | 4404 | /* We can not add the asterik earlier in case |
| 4405 | it got remapped to 0xF03A as if it were part of the |
| 4406 | directory name instead of a wildcard */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4407 | name_len *= 2; |
Shirish Pargaonkar | c052e2b | 2012-09-28 12:21:14 -0500 | [diff] [blame] | 4408 | if (msearch) { |
| 4409 | pSMB->FileName[name_len] = CIFS_DIR_SEP(cifs_sb); |
| 4410 | pSMB->FileName[name_len+1] = 0; |
| 4411 | pSMB->FileName[name_len+2] = '*'; |
| 4412 | pSMB->FileName[name_len+3] = 0; |
| 4413 | name_len += 4; /* now the trailing null */ |
| 4414 | /* null terminate just in case */ |
| 4415 | pSMB->FileName[name_len] = 0; |
| 4416 | pSMB->FileName[name_len+1] = 0; |
| 4417 | name_len += 2; |
| 4418 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4419 | } else { /* BB add check for overrun of SMB buf BB */ |
| 4420 | name_len = strnlen(searchName, PATH_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4421 | /* BB fix here and in unicode clause above ie |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 4422 | if (name_len > buffersize-header) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4423 | free buffer exit; BB */ |
| 4424 | strncpy(pSMB->FileName, searchName, name_len); |
Shirish Pargaonkar | c052e2b | 2012-09-28 12:21:14 -0500 | [diff] [blame] | 4425 | if (msearch) { |
| 4426 | pSMB->FileName[name_len] = CIFS_DIR_SEP(cifs_sb); |
| 4427 | pSMB->FileName[name_len+1] = '*'; |
| 4428 | pSMB->FileName[name_len+2] = 0; |
| 4429 | name_len += 3; |
| 4430 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4431 | } |
| 4432 | |
| 4433 | params = 12 + name_len /* includes null */ ; |
| 4434 | pSMB->TotalDataCount = 0; /* no EAs */ |
| 4435 | pSMB->MaxParameterCount = cpu_to_le16(10); |
Jeff Layton | c974bef | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 4436 | pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4437 | pSMB->MaxSetupCount = 0; |
| 4438 | pSMB->Reserved = 0; |
| 4439 | pSMB->Flags = 0; |
| 4440 | pSMB->Timeout = 0; |
| 4441 | pSMB->Reserved2 = 0; |
| 4442 | byte_count = params + 1 /* pad */ ; |
| 4443 | pSMB->TotalParameterCount = cpu_to_le16(params); |
| 4444 | pSMB->ParameterCount = pSMB->TotalParameterCount; |
| 4445 | pSMB->ParameterOffset = cpu_to_le16( |
Steve French | 8827481 | 2006-03-09 22:21:45 +0000 | [diff] [blame] | 4446 | offsetof(struct smb_com_transaction2_ffirst_req, SearchAttributes) |
| 4447 | - 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4448 | pSMB->DataCount = 0; |
| 4449 | pSMB->DataOffset = 0; |
| 4450 | pSMB->SetupCount = 1; /* one byte, no need to make endian neutral */ |
| 4451 | pSMB->Reserved3 = 0; |
| 4452 | pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_FIRST); |
| 4453 | pSMB->SearchAttributes = |
| 4454 | cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM | |
| 4455 | ATTR_DIRECTORY); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4456 | pSMB->SearchCount = cpu_to_le16(CIFSMaxBufSize/sizeof(FILE_UNIX_INFO)); |
Shirish Pargaonkar | 2608bee | 2012-05-15 10:19:16 -0500 | [diff] [blame] | 4457 | pSMB->SearchFlags = cpu_to_le16(search_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4458 | pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level); |
| 4459 | |
| 4460 | /* BB what should we set StorageType to? Does it matter? BB */ |
| 4461 | pSMB->SearchStorageType = 0; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 4462 | inc_rfc1001_len(pSMB, byte_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4463 | pSMB->ByteCount = cpu_to_le16(byte_count); |
| 4464 | |
| 4465 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 4466 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
Pavel Shilovsky | 44c5818 | 2012-05-28 14:16:31 +0400 | [diff] [blame] | 4467 | cifs_stats_inc(&tcon->stats.cifs_stats.num_ffirst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4468 | |
Steve French | 8827481 | 2006-03-09 22:21:45 +0000 | [diff] [blame] | 4469 | if (rc) {/* BB add logic to retry regular search if Unix search |
| 4470 | rejected unexpectedly by server */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4471 | /* BB Add code to handle unsupported level rc */ |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4472 | cifs_dbg(FYI, "Error in FindFirst = %d\n", rc); |
Steve French | 1982c34 | 2005-08-17 12:38:22 -0700 | [diff] [blame] | 4473 | |
Steve French | 8827481 | 2006-03-09 22:21:45 +0000 | [diff] [blame] | 4474 | cifs_buf_release(pSMB); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4475 | |
| 4476 | /* BB eventually could optimize out free and realloc of buf */ |
| 4477 | /* for this case */ |
| 4478 | if (rc == -EAGAIN) |
| 4479 | goto findFirstRetry; |
| 4480 | } else { /* decode response */ |
| 4481 | /* BB remember to free buffer if error BB */ |
| 4482 | rc = validate_t2((struct smb_t2_rsp *)pSMBr); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 4483 | if (rc == 0) { |
Steve French | b77d753 | 2008-10-08 19:13:46 +0000 | [diff] [blame] | 4484 | unsigned int lnoff; |
| 4485 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4486 | if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE) |
Steve French | 4b18f2a | 2008-04-29 00:06:05 +0000 | [diff] [blame] | 4487 | psrch_inf->unicode = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4488 | else |
Steve French | 4b18f2a | 2008-04-29 00:06:05 +0000 | [diff] [blame] | 4489 | psrch_inf->unicode = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4490 | |
| 4491 | psrch_inf->ntwrk_buf_start = (char *)pSMBr; |
Steve French | d47d7c1 | 2006-02-28 03:45:48 +0000 | [diff] [blame] | 4492 | psrch_inf->smallBuf = 0; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4493 | psrch_inf->srch_entries_start = |
| 4494 | (char *) &pSMBr->hdr.Protocol + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4495 | le16_to_cpu(pSMBr->t2.DataOffset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4496 | parms = (T2_FFIRST_RSP_PARMS *)((char *) &pSMBr->hdr.Protocol + |
| 4497 | le16_to_cpu(pSMBr->t2.ParameterOffset)); |
| 4498 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 4499 | if (parms->EndofSearch) |
Steve French | 4b18f2a | 2008-04-29 00:06:05 +0000 | [diff] [blame] | 4500 | psrch_inf->endOfSearch = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4501 | else |
Steve French | 4b18f2a | 2008-04-29 00:06:05 +0000 | [diff] [blame] | 4502 | psrch_inf->endOfSearch = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4503 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4504 | psrch_inf->entries_in_buffer = |
| 4505 | le16_to_cpu(parms->SearchCount); |
Steve French | 6080823 | 2006-04-22 15:53:05 +0000 | [diff] [blame] | 4506 | psrch_inf->index_of_last_entry = 2 /* skip . and .. */ + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4507 | psrch_inf->entries_in_buffer; |
Steve French | b77d753 | 2008-10-08 19:13:46 +0000 | [diff] [blame] | 4508 | lnoff = le16_to_cpu(parms->LastNameOffset); |
Jeff Layton | c974bef | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 4509 | if (CIFSMaxBufSize < lnoff) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4510 | cifs_dbg(VFS, "ignoring corrupt resume name\n"); |
Steve French | b77d753 | 2008-10-08 19:13:46 +0000 | [diff] [blame] | 4511 | psrch_inf->last_entry = NULL; |
| 4512 | return rc; |
| 4513 | } |
| 4514 | |
Steve French | 0752f15 | 2008-10-07 20:03:33 +0000 | [diff] [blame] | 4515 | psrch_inf->last_entry = psrch_inf->srch_entries_start + |
Steve French | b77d753 | 2008-10-08 19:13:46 +0000 | [diff] [blame] | 4516 | lnoff; |
| 4517 | |
Shirish Pargaonkar | c052e2b | 2012-09-28 12:21:14 -0500 | [diff] [blame] | 4518 | if (pnetfid) |
| 4519 | *pnetfid = parms->SearchHandle; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4520 | } else { |
| 4521 | cifs_buf_release(pSMB); |
| 4522 | } |
| 4523 | } |
| 4524 | |
| 4525 | return rc; |
| 4526 | } |
| 4527 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 4528 | int CIFSFindNext(const unsigned int xid, struct cifs_tcon *tcon, |
| 4529 | __u16 searchHandle, __u16 search_flags, |
| 4530 | struct cifs_search_info *psrch_inf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4531 | { |
| 4532 | TRANSACTION2_FNEXT_REQ *pSMB = NULL; |
| 4533 | TRANSACTION2_FNEXT_RSP *pSMBr = NULL; |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 4534 | T2_FNEXT_RSP_PARMS *parms; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4535 | char *response_data; |
| 4536 | int rc = 0; |
Jeff Layton | 9438fab | 2011-08-23 07:21:28 -0400 | [diff] [blame] | 4537 | int bytes_returned; |
| 4538 | unsigned int name_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4539 | __u16 params, byte_count; |
| 4540 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4541 | cifs_dbg(FYI, "In FindNext\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4542 | |
Steve French | 4b18f2a | 2008-04-29 00:06:05 +0000 | [diff] [blame] | 4543 | if (psrch_inf->endOfSearch) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4544 | return -ENOENT; |
| 4545 | |
| 4546 | rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB, |
| 4547 | (void **) &pSMBr); |
| 4548 | if (rc) |
| 4549 | return rc; |
| 4550 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4551 | params = 14; /* includes 2 bytes of null string, converted to LE below*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4552 | byte_count = 0; |
| 4553 | pSMB->TotalDataCount = 0; /* no EAs */ |
| 4554 | pSMB->MaxParameterCount = cpu_to_le16(8); |
Jeff Layton | c974bef | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 4555 | pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4556 | pSMB->MaxSetupCount = 0; |
| 4557 | pSMB->Reserved = 0; |
| 4558 | pSMB->Flags = 0; |
| 4559 | pSMB->Timeout = 0; |
| 4560 | pSMB->Reserved2 = 0; |
| 4561 | pSMB->ParameterOffset = cpu_to_le16( |
| 4562 | offsetof(struct smb_com_transaction2_fnext_req,SearchHandle) - 4); |
| 4563 | pSMB->DataCount = 0; |
| 4564 | pSMB->DataOffset = 0; |
| 4565 | pSMB->SetupCount = 1; |
| 4566 | pSMB->Reserved3 = 0; |
| 4567 | pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_NEXT); |
| 4568 | pSMB->SearchHandle = searchHandle; /* always kept as le */ |
| 4569 | pSMB->SearchCount = |
Steve French | 630f3f0c | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 4570 | cpu_to_le16(CIFSMaxBufSize / sizeof(FILE_UNIX_INFO)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4571 | pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level); |
| 4572 | pSMB->ResumeKey = psrch_inf->resume_key; |
Shirish Pargaonkar | 2608bee | 2012-05-15 10:19:16 -0500 | [diff] [blame] | 4573 | pSMB->SearchFlags = cpu_to_le16(search_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4574 | |
| 4575 | name_len = psrch_inf->resume_name_len; |
| 4576 | params += name_len; |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 4577 | if (name_len < PATH_MAX) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4578 | memcpy(pSMB->ResumeFileName, psrch_inf->presume_name, name_len); |
| 4579 | byte_count += name_len; |
Steve French | ef6724e | 2005-08-02 21:31:05 -0700 | [diff] [blame] | 4580 | /* 14 byte parm len above enough for 2 byte null terminator */ |
| 4581 | pSMB->ResumeFileName[name_len] = 0; |
| 4582 | pSMB->ResumeFileName[name_len+1] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4583 | } else { |
| 4584 | rc = -EINVAL; |
| 4585 | goto FNext2_err_exit; |
| 4586 | } |
| 4587 | byte_count = params + 1 /* pad */ ; |
| 4588 | pSMB->TotalParameterCount = cpu_to_le16(params); |
| 4589 | pSMB->ParameterCount = pSMB->TotalParameterCount; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 4590 | inc_rfc1001_len(pSMB, byte_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4591 | pSMB->ByteCount = cpu_to_le16(byte_count); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4592 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4593 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 4594 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
Pavel Shilovsky | 44c5818 | 2012-05-28 14:16:31 +0400 | [diff] [blame] | 4595 | cifs_stats_inc(&tcon->stats.cifs_stats.num_fnext); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4596 | if (rc) { |
| 4597 | if (rc == -EBADF) { |
Steve French | 4b18f2a | 2008-04-29 00:06:05 +0000 | [diff] [blame] | 4598 | psrch_inf->endOfSearch = true; |
Jeff Layton | 6353450 | 2008-05-12 19:56:05 -0700 | [diff] [blame] | 4599 | cifs_buf_release(pSMB); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4600 | rc = 0; /* search probably was closed at end of search*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4601 | } else |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4602 | cifs_dbg(FYI, "FindNext returned = %d\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4603 | } else { /* decode response */ |
| 4604 | rc = validate_t2((struct smb_t2_rsp *)pSMBr); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4605 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 4606 | if (rc == 0) { |
Steve French | b77d753 | 2008-10-08 19:13:46 +0000 | [diff] [blame] | 4607 | unsigned int lnoff; |
| 4608 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4609 | /* BB fixme add lock for file (srch_info) struct here */ |
| 4610 | if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE) |
Steve French | 4b18f2a | 2008-04-29 00:06:05 +0000 | [diff] [blame] | 4611 | psrch_inf->unicode = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4612 | else |
Steve French | 4b18f2a | 2008-04-29 00:06:05 +0000 | [diff] [blame] | 4613 | psrch_inf->unicode = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4614 | response_data = (char *) &pSMBr->hdr.Protocol + |
| 4615 | le16_to_cpu(pSMBr->t2.ParameterOffset); |
| 4616 | parms = (T2_FNEXT_RSP_PARMS *)response_data; |
| 4617 | response_data = (char *)&pSMBr->hdr.Protocol + |
| 4618 | le16_to_cpu(pSMBr->t2.DataOffset); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 4619 | if (psrch_inf->smallBuf) |
Steve French | d47d7c1 | 2006-02-28 03:45:48 +0000 | [diff] [blame] | 4620 | cifs_small_buf_release( |
| 4621 | psrch_inf->ntwrk_buf_start); |
| 4622 | else |
| 4623 | cifs_buf_release(psrch_inf->ntwrk_buf_start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4624 | psrch_inf->srch_entries_start = response_data; |
| 4625 | psrch_inf->ntwrk_buf_start = (char *)pSMB; |
Steve French | d47d7c1 | 2006-02-28 03:45:48 +0000 | [diff] [blame] | 4626 | psrch_inf->smallBuf = 0; |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 4627 | if (parms->EndofSearch) |
Steve French | 4b18f2a | 2008-04-29 00:06:05 +0000 | [diff] [blame] | 4628 | psrch_inf->endOfSearch = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4629 | else |
Steve French | 4b18f2a | 2008-04-29 00:06:05 +0000 | [diff] [blame] | 4630 | psrch_inf->endOfSearch = false; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4631 | psrch_inf->entries_in_buffer = |
| 4632 | le16_to_cpu(parms->SearchCount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4633 | psrch_inf->index_of_last_entry += |
| 4634 | psrch_inf->entries_in_buffer; |
Steve French | b77d753 | 2008-10-08 19:13:46 +0000 | [diff] [blame] | 4635 | lnoff = le16_to_cpu(parms->LastNameOffset); |
Jeff Layton | c974bef | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 4636 | if (CIFSMaxBufSize < lnoff) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4637 | cifs_dbg(VFS, "ignoring corrupt resume name\n"); |
Steve French | b77d753 | 2008-10-08 19:13:46 +0000 | [diff] [blame] | 4638 | psrch_inf->last_entry = NULL; |
| 4639 | return rc; |
| 4640 | } else |
| 4641 | psrch_inf->last_entry = |
| 4642 | psrch_inf->srch_entries_start + lnoff; |
| 4643 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4644 | /* cifs_dbg(FYI, "fnxt2 entries in buf %d index_of_last %d\n", |
| 4645 | psrch_inf->entries_in_buffer, psrch_inf->index_of_last_entry); */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4646 | |
| 4647 | /* BB fixme add unlock here */ |
| 4648 | } |
| 4649 | |
| 4650 | } |
| 4651 | |
| 4652 | /* BB On error, should we leave previous search buf (and count and |
| 4653 | last entry fields) intact or free the previous one? */ |
| 4654 | |
| 4655 | /* Note: On -EAGAIN error only caller can retry on handle based calls |
| 4656 | since file handle passed in no longer valid */ |
| 4657 | FNext2_err_exit: |
| 4658 | if (rc != 0) |
| 4659 | cifs_buf_release(pSMB); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4660 | return rc; |
| 4661 | } |
| 4662 | |
| 4663 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 4664 | CIFSFindClose(const unsigned int xid, struct cifs_tcon *tcon, |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4665 | const __u16 searchHandle) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4666 | { |
| 4667 | int rc = 0; |
| 4668 | FINDCLOSE_REQ *pSMB = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4669 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4670 | cifs_dbg(FYI, "In CIFSSMBFindClose\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4671 | rc = small_smb_init(SMB_COM_FIND_CLOSE2, 1, tcon, (void **)&pSMB); |
| 4672 | |
| 4673 | /* no sense returning error if session restarted |
| 4674 | as file handle has been closed */ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 4675 | if (rc == -EAGAIN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4676 | return 0; |
| 4677 | if (rc) |
| 4678 | return rc; |
| 4679 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4680 | pSMB->FileID = searchHandle; |
| 4681 | pSMB->ByteCount = 0; |
Pavel Shilovsky | 792af7b | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 4682 | rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 4683 | cifs_small_buf_release(pSMB); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 4684 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4685 | cifs_dbg(VFS, "Send error in FindClose = %d\n", rc); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 4686 | |
Pavel Shilovsky | 44c5818 | 2012-05-28 14:16:31 +0400 | [diff] [blame] | 4687 | cifs_stats_inc(&tcon->stats.cifs_stats.num_fclose); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4688 | |
| 4689 | /* Since session is dead, search handle closed on server already */ |
| 4690 | if (rc == -EAGAIN) |
| 4691 | rc = 0; |
| 4692 | |
| 4693 | return rc; |
| 4694 | } |
| 4695 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4696 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 4697 | CIFSGetSrvInodeNumber(const unsigned int xid, struct cifs_tcon *tcon, |
Pavel Shilovsky | 1208ef1 | 2012-05-27 17:34:43 +0400 | [diff] [blame] | 4698 | const char *search_name, __u64 *inode_number, |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4699 | const struct nls_table *nls_codepage, int remap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4700 | { |
| 4701 | int rc = 0; |
| 4702 | TRANSACTION2_QPI_REQ *pSMB = NULL; |
| 4703 | TRANSACTION2_QPI_RSP *pSMBr = NULL; |
| 4704 | int name_len, bytes_returned; |
| 4705 | __u16 params, byte_count; |
| 4706 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4707 | cifs_dbg(FYI, "In GetSrvInodeNum for %s\n", search_name); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 4708 | if (tcon == NULL) |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4709 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4710 | |
| 4711 | GetInodeNumberRetry: |
| 4712 | rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB, |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4713 | (void **) &pSMBr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4714 | if (rc) |
| 4715 | return rc; |
| 4716 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4717 | if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) { |
| 4718 | name_len = |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 4719 | cifsConvertToUTF16((__le16 *) pSMB->FileName, |
Pavel Shilovsky | 1208ef1 | 2012-05-27 17:34:43 +0400 | [diff] [blame] | 4720 | search_name, PATH_MAX, nls_codepage, |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 4721 | remap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4722 | name_len++; /* trailing null */ |
| 4723 | name_len *= 2; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4724 | } else { /* BB improve the check for buffer overruns BB */ |
Pavel Shilovsky | 1208ef1 | 2012-05-27 17:34:43 +0400 | [diff] [blame] | 4725 | name_len = strnlen(search_name, PATH_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4726 | name_len++; /* trailing null */ |
Pavel Shilovsky | 1208ef1 | 2012-05-27 17:34:43 +0400 | [diff] [blame] | 4727 | strncpy(pSMB->FileName, search_name, name_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4728 | } |
| 4729 | |
| 4730 | params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ; |
| 4731 | pSMB->TotalDataCount = 0; |
| 4732 | pSMB->MaxParameterCount = cpu_to_le16(2); |
| 4733 | /* BB find exact max data count below from sess structure BB */ |
| 4734 | pSMB->MaxDataCount = cpu_to_le16(4000); |
| 4735 | pSMB->MaxSetupCount = 0; |
| 4736 | pSMB->Reserved = 0; |
| 4737 | pSMB->Flags = 0; |
| 4738 | pSMB->Timeout = 0; |
| 4739 | pSMB->Reserved2 = 0; |
| 4740 | pSMB->ParameterOffset = cpu_to_le16(offsetof( |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4741 | struct smb_com_transaction2_qpi_req, InformationLevel) - 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4742 | pSMB->DataCount = 0; |
| 4743 | pSMB->DataOffset = 0; |
| 4744 | pSMB->SetupCount = 1; |
| 4745 | pSMB->Reserved3 = 0; |
| 4746 | pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION); |
| 4747 | byte_count = params + 1 /* pad */ ; |
| 4748 | pSMB->TotalParameterCount = cpu_to_le16(params); |
| 4749 | pSMB->ParameterCount = pSMB->TotalParameterCount; |
| 4750 | pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_INTERNAL_INFO); |
| 4751 | pSMB->Reserved4 = 0; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 4752 | inc_rfc1001_len(pSMB, byte_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4753 | pSMB->ByteCount = cpu_to_le16(byte_count); |
| 4754 | |
| 4755 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 4756 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
| 4757 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4758 | cifs_dbg(FYI, "error %d in QueryInternalInfo\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4759 | } else { |
| 4760 | /* decode response */ |
| 4761 | rc = validate_t2((struct smb_t2_rsp *)pSMBr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4762 | /* BB also check enough total bytes returned */ |
Jeff Layton | 820a803 | 2011-05-04 08:05:26 -0400 | [diff] [blame] | 4763 | if (rc || get_bcc(&pSMBr->hdr) < 2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4764 | /* If rc should we check for EOPNOSUPP and |
| 4765 | disable the srvino flag? or in caller? */ |
| 4766 | rc = -EIO; /* bad smb */ |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4767 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4768 | __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset); |
| 4769 | __u16 count = le16_to_cpu(pSMBr->t2.DataCount); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4770 | struct file_internal_info *pfinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4771 | /* BB Do we need a cast or hash here ? */ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 4772 | if (count < 8) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4773 | cifs_dbg(FYI, "Illegal size ret in QryIntrnlInf\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4774 | rc = -EIO; |
| 4775 | goto GetInodeNumOut; |
| 4776 | } |
| 4777 | pfinfo = (struct file_internal_info *) |
| 4778 | (data_offset + (char *) &pSMBr->hdr.Protocol); |
Steve French | 85a6dac | 2009-04-01 05:22:00 +0000 | [diff] [blame] | 4779 | *inode_number = le64_to_cpu(pfinfo->UniqueId); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4780 | } |
| 4781 | } |
| 4782 | GetInodeNumOut: |
| 4783 | cifs_buf_release(pSMB); |
| 4784 | if (rc == -EAGAIN) |
| 4785 | goto GetInodeNumberRetry; |
| 4786 | return rc; |
| 4787 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4788 | |
| 4789 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 4790 | CIFSGetDFSRefer(const unsigned int xid, struct cifs_ses *ses, |
Pavel Shilovsky | b669f33 | 2012-05-27 20:21:53 +0400 | [diff] [blame] | 4791 | const char *search_name, struct dfs_info3_param **target_nodes, |
Steve French | c2cf07d | 2008-05-15 06:20:02 +0000 | [diff] [blame] | 4792 | unsigned int *num_of_nodes, |
Steve French | 737b758 | 2005-04-28 22:41:06 -0700 | [diff] [blame] | 4793 | const struct nls_table *nls_codepage, int remap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4794 | { |
| 4795 | /* TRANS2_GET_DFS_REFERRAL */ |
| 4796 | TRANSACTION2_GET_DFS_REFER_REQ *pSMB = NULL; |
| 4797 | TRANSACTION2_GET_DFS_REFER_RSP *pSMBr = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4798 | int rc = 0; |
| 4799 | int bytes_returned; |
| 4800 | int name_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4801 | __u16 params, byte_count; |
Steve French | c2cf07d | 2008-05-15 06:20:02 +0000 | [diff] [blame] | 4802 | *num_of_nodes = 0; |
| 4803 | *target_nodes = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4804 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4805 | cifs_dbg(FYI, "In GetDFSRefer the path %s\n", search_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4806 | if (ses == NULL) |
| 4807 | return -ENODEV; |
| 4808 | getDFSRetry: |
| 4809 | rc = smb_init(SMB_COM_TRANSACTION2, 15, NULL, (void **) &pSMB, |
| 4810 | (void **) &pSMBr); |
| 4811 | if (rc) |
| 4812 | return rc; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4813 | |
| 4814 | /* server pointer checked in called function, |
Steve French | 1982c34 | 2005-08-17 12:38:22 -0700 | [diff] [blame] | 4815 | but should never be null here anyway */ |
Pavel Shilovsky | 8825736 | 2012-05-23 14:01:59 +0400 | [diff] [blame] | 4816 | pSMB->hdr.Mid = get_next_mid(ses->server); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4817 | pSMB->hdr.Tid = ses->ipc_tid; |
| 4818 | pSMB->hdr.Uid = ses->Suid; |
Steve French | 26f5736 | 2007-08-30 22:09:15 +0000 | [diff] [blame] | 4819 | if (ses->capabilities & CAP_STATUS32) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4820 | pSMB->hdr.Flags2 |= SMBFLG2_ERR_STATUS; |
Steve French | 26f5736 | 2007-08-30 22:09:15 +0000 | [diff] [blame] | 4821 | if (ses->capabilities & CAP_DFS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4822 | pSMB->hdr.Flags2 |= SMBFLG2_DFS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4823 | |
| 4824 | if (ses->capabilities & CAP_UNICODE) { |
| 4825 | pSMB->hdr.Flags2 |= SMBFLG2_UNICODE; |
| 4826 | name_len = |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 4827 | cifsConvertToUTF16((__le16 *) pSMB->RequestFileName, |
Pavel Shilovsky | b669f33 | 2012-05-27 20:21:53 +0400 | [diff] [blame] | 4828 | search_name, PATH_MAX, nls_codepage, |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 4829 | remap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4830 | name_len++; /* trailing null */ |
| 4831 | name_len *= 2; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4832 | } else { /* BB improve the check for buffer overruns BB */ |
Pavel Shilovsky | b669f33 | 2012-05-27 20:21:53 +0400 | [diff] [blame] | 4833 | name_len = strnlen(search_name, PATH_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4834 | name_len++; /* trailing null */ |
Pavel Shilovsky | b669f33 | 2012-05-27 20:21:53 +0400 | [diff] [blame] | 4835 | strncpy(pSMB->RequestFileName, search_name, name_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4836 | } |
| 4837 | |
Dan Carpenter | 65c3b20 | 2015-04-30 17:30:24 +0300 | [diff] [blame] | 4838 | if (ses->server->sign) |
Jeff Layton | 38d77c5 | 2013-05-26 07:01:00 -0400 | [diff] [blame] | 4839 | pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE; |
Steve French | 1a4e15a | 2006-10-12 21:33:51 +0000 | [diff] [blame] | 4840 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4841 | pSMB->hdr.Uid = ses->Suid; |
Steve French | 1a4e15a | 2006-10-12 21:33:51 +0000 | [diff] [blame] | 4842 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4843 | params = 2 /* level */ + name_len /*includes null */ ; |
| 4844 | pSMB->TotalDataCount = 0; |
| 4845 | pSMB->DataCount = 0; |
| 4846 | pSMB->DataOffset = 0; |
| 4847 | pSMB->MaxParameterCount = 0; |
Steve French | 582d21e | 2008-05-13 04:54:12 +0000 | [diff] [blame] | 4848 | /* BB find exact max SMB PDU from sess structure BB */ |
| 4849 | pSMB->MaxDataCount = cpu_to_le16(4000); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4850 | pSMB->MaxSetupCount = 0; |
| 4851 | pSMB->Reserved = 0; |
| 4852 | pSMB->Flags = 0; |
| 4853 | pSMB->Timeout = 0; |
| 4854 | pSMB->Reserved2 = 0; |
| 4855 | pSMB->ParameterOffset = cpu_to_le16(offsetof( |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4856 | struct smb_com_transaction2_get_dfs_refer_req, MaxReferralLevel) - 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4857 | pSMB->SetupCount = 1; |
| 4858 | pSMB->Reserved3 = 0; |
| 4859 | pSMB->SubCommand = cpu_to_le16(TRANS2_GET_DFS_REFERRAL); |
| 4860 | byte_count = params + 3 /* pad */ ; |
| 4861 | pSMB->ParameterCount = cpu_to_le16(params); |
| 4862 | pSMB->TotalParameterCount = pSMB->ParameterCount; |
| 4863 | pSMB->MaxReferralLevel = cpu_to_le16(3); |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 4864 | inc_rfc1001_len(pSMB, byte_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4865 | pSMB->ByteCount = cpu_to_le16(byte_count); |
| 4866 | |
| 4867 | rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB, |
| 4868 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
| 4869 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4870 | cifs_dbg(FYI, "Send error in GetDFSRefer = %d\n", rc); |
Steve French | c2cf07d | 2008-05-15 06:20:02 +0000 | [diff] [blame] | 4871 | goto GetDFSRefExit; |
| 4872 | } |
| 4873 | rc = validate_t2((struct smb_t2_rsp *)pSMBr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4874 | |
Steve French | c2cf07d | 2008-05-15 06:20:02 +0000 | [diff] [blame] | 4875 | /* BB Also check if enough total bytes returned? */ |
Jeff Layton | 820a803 | 2011-05-04 08:05:26 -0400 | [diff] [blame] | 4876 | if (rc || get_bcc(&pSMBr->hdr) < 17) { |
Steve French | c2cf07d | 2008-05-15 06:20:02 +0000 | [diff] [blame] | 4877 | rc = -EIO; /* bad smb */ |
Igor Mammedov | fec4585 | 2008-05-16 13:06:30 +0400 | [diff] [blame] | 4878 | goto GetDFSRefExit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4879 | } |
Igor Mammedov | fec4585 | 2008-05-16 13:06:30 +0400 | [diff] [blame] | 4880 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4881 | cifs_dbg(FYI, "Decoding GetDFSRefer response BCC: %d Offset %d\n", |
| 4882 | get_bcc(&pSMBr->hdr), le16_to_cpu(pSMBr->t2.DataOffset)); |
Igor Mammedov | fec4585 | 2008-05-16 13:06:30 +0400 | [diff] [blame] | 4883 | |
| 4884 | /* parse returned result into more usable form */ |
Aurelien Aptel | 4ecce92 | 2017-02-13 16:03:47 +0100 | [diff] [blame^] | 4885 | rc = parse_dfs_referrals(&pSMBr->dfs_data, |
| 4886 | le16_to_cpu(pSMBr->t2.DataCount), |
| 4887 | num_of_nodes, target_nodes, nls_codepage, |
| 4888 | remap, search_name, |
| 4889 | pSMBr->hdr.Flags2 & SMBFLG2_UNICODE); |
Igor Mammedov | fec4585 | 2008-05-16 13:06:30 +0400 | [diff] [blame] | 4890 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4891 | GetDFSRefExit: |
Steve French | 0d817bc | 2008-05-22 02:02:03 +0000 | [diff] [blame] | 4892 | cifs_buf_release(pSMB); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4893 | |
| 4894 | if (rc == -EAGAIN) |
| 4895 | goto getDFSRetry; |
| 4896 | |
| 4897 | return rc; |
| 4898 | } |
| 4899 | |
Steve French | 2096243 | 2005-09-21 22:05:57 -0700 | [diff] [blame] | 4900 | /* Query File System Info such as free space to old servers such as Win 9x */ |
| 4901 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 4902 | SMBOldQFSInfo(const unsigned int xid, struct cifs_tcon *tcon, |
| 4903 | struct kstatfs *FSData) |
Steve French | 2096243 | 2005-09-21 22:05:57 -0700 | [diff] [blame] | 4904 | { |
| 4905 | /* level 0x01 SMB_QUERY_FILE_SYSTEM_INFO */ |
| 4906 | TRANSACTION2_QFSI_REQ *pSMB = NULL; |
| 4907 | TRANSACTION2_QFSI_RSP *pSMBr = NULL; |
| 4908 | FILE_SYSTEM_ALLOC_INFO *response_data; |
| 4909 | int rc = 0; |
| 4910 | int bytes_returned = 0; |
| 4911 | __u16 params, byte_count; |
| 4912 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4913 | cifs_dbg(FYI, "OldQFSInfo\n"); |
Steve French | 2096243 | 2005-09-21 22:05:57 -0700 | [diff] [blame] | 4914 | oldQFSInfoRetry: |
| 4915 | rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB, |
| 4916 | (void **) &pSMBr); |
| 4917 | if (rc) |
| 4918 | return rc; |
Steve French | 2096243 | 2005-09-21 22:05:57 -0700 | [diff] [blame] | 4919 | |
| 4920 | params = 2; /* level */ |
| 4921 | pSMB->TotalDataCount = 0; |
| 4922 | pSMB->MaxParameterCount = cpu_to_le16(2); |
| 4923 | pSMB->MaxDataCount = cpu_to_le16(1000); |
| 4924 | pSMB->MaxSetupCount = 0; |
| 4925 | pSMB->Reserved = 0; |
| 4926 | pSMB->Flags = 0; |
| 4927 | pSMB->Timeout = 0; |
| 4928 | pSMB->Reserved2 = 0; |
| 4929 | byte_count = params + 1 /* pad */ ; |
| 4930 | pSMB->TotalParameterCount = cpu_to_le16(params); |
| 4931 | pSMB->ParameterCount = pSMB->TotalParameterCount; |
| 4932 | pSMB->ParameterOffset = cpu_to_le16(offsetof( |
| 4933 | struct smb_com_transaction2_qfsi_req, InformationLevel) - 4); |
| 4934 | pSMB->DataCount = 0; |
| 4935 | pSMB->DataOffset = 0; |
| 4936 | pSMB->SetupCount = 1; |
| 4937 | pSMB->Reserved3 = 0; |
| 4938 | pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION); |
| 4939 | pSMB->InformationLevel = cpu_to_le16(SMB_INFO_ALLOCATION); |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 4940 | inc_rfc1001_len(pSMB, byte_count); |
Steve French | 2096243 | 2005-09-21 22:05:57 -0700 | [diff] [blame] | 4941 | pSMB->ByteCount = cpu_to_le16(byte_count); |
| 4942 | |
| 4943 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 4944 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
| 4945 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4946 | cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc); |
Steve French | 2096243 | 2005-09-21 22:05:57 -0700 | [diff] [blame] | 4947 | } else { /* decode response */ |
| 4948 | rc = validate_t2((struct smb_t2_rsp *)pSMBr); |
| 4949 | |
Jeff Layton | 820a803 | 2011-05-04 08:05:26 -0400 | [diff] [blame] | 4950 | if (rc || get_bcc(&pSMBr->hdr) < 18) |
Steve French | 2096243 | 2005-09-21 22:05:57 -0700 | [diff] [blame] | 4951 | rc = -EIO; /* bad smb */ |
| 4952 | else { |
| 4953 | __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4954 | cifs_dbg(FYI, "qfsinf resp BCC: %d Offset %d\n", |
Jeff Layton | 820a803 | 2011-05-04 08:05:26 -0400 | [diff] [blame] | 4955 | get_bcc(&pSMBr->hdr), data_offset); |
Steve French | 2096243 | 2005-09-21 22:05:57 -0700 | [diff] [blame] | 4956 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4957 | response_data = (FILE_SYSTEM_ALLOC_INFO *) |
Steve French | 2096243 | 2005-09-21 22:05:57 -0700 | [diff] [blame] | 4958 | (((char *) &pSMBr->hdr.Protocol) + data_offset); |
| 4959 | FSData->f_bsize = |
| 4960 | le16_to_cpu(response_data->BytesPerSector) * |
| 4961 | le32_to_cpu(response_data-> |
| 4962 | SectorsPerAllocationUnit); |
| 4963 | FSData->f_blocks = |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 4964 | le32_to_cpu(response_data->TotalAllocationUnits); |
Steve French | 2096243 | 2005-09-21 22:05:57 -0700 | [diff] [blame] | 4965 | FSData->f_bfree = FSData->f_bavail = |
| 4966 | le32_to_cpu(response_data->FreeAllocationUnits); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4967 | cifs_dbg(FYI, "Blocks: %lld Free: %lld Block size %ld\n", |
| 4968 | (unsigned long long)FSData->f_blocks, |
| 4969 | (unsigned long long)FSData->f_bfree, |
| 4970 | FSData->f_bsize); |
Steve French | 2096243 | 2005-09-21 22:05:57 -0700 | [diff] [blame] | 4971 | } |
| 4972 | } |
| 4973 | cifs_buf_release(pSMB); |
| 4974 | |
| 4975 | if (rc == -EAGAIN) |
| 4976 | goto oldQFSInfoRetry; |
| 4977 | |
| 4978 | return rc; |
| 4979 | } |
| 4980 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4981 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 4982 | CIFSSMBQFSInfo(const unsigned int xid, struct cifs_tcon *tcon, |
| 4983 | struct kstatfs *FSData) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4984 | { |
| 4985 | /* level 0x103 SMB_QUERY_FILE_SYSTEM_INFO */ |
| 4986 | TRANSACTION2_QFSI_REQ *pSMB = NULL; |
| 4987 | TRANSACTION2_QFSI_RSP *pSMBr = NULL; |
| 4988 | FILE_SYSTEM_INFO *response_data; |
| 4989 | int rc = 0; |
| 4990 | int bytes_returned = 0; |
| 4991 | __u16 params, byte_count; |
| 4992 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4993 | cifs_dbg(FYI, "In QFSInfo\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4994 | QFSInfoRetry: |
| 4995 | rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB, |
| 4996 | (void **) &pSMBr); |
| 4997 | if (rc) |
| 4998 | return rc; |
| 4999 | |
| 5000 | params = 2; /* level */ |
| 5001 | pSMB->TotalDataCount = 0; |
| 5002 | pSMB->MaxParameterCount = cpu_to_le16(2); |
Steve French | 2096243 | 2005-09-21 22:05:57 -0700 | [diff] [blame] | 5003 | pSMB->MaxDataCount = cpu_to_le16(1000); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5004 | pSMB->MaxSetupCount = 0; |
| 5005 | pSMB->Reserved = 0; |
| 5006 | pSMB->Flags = 0; |
| 5007 | pSMB->Timeout = 0; |
| 5008 | pSMB->Reserved2 = 0; |
| 5009 | byte_count = params + 1 /* pad */ ; |
| 5010 | pSMB->TotalParameterCount = cpu_to_le16(params); |
| 5011 | pSMB->ParameterCount = pSMB->TotalParameterCount; |
| 5012 | pSMB->ParameterOffset = cpu_to_le16(offsetof( |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 5013 | struct smb_com_transaction2_qfsi_req, InformationLevel) - 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5014 | pSMB->DataCount = 0; |
| 5015 | pSMB->DataOffset = 0; |
| 5016 | pSMB->SetupCount = 1; |
| 5017 | pSMB->Reserved3 = 0; |
| 5018 | pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION); |
| 5019 | pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_SIZE_INFO); |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 5020 | inc_rfc1001_len(pSMB, byte_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5021 | pSMB->ByteCount = cpu_to_le16(byte_count); |
| 5022 | |
| 5023 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 5024 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
| 5025 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5026 | cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5027 | } else { /* decode response */ |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 5028 | rc = validate_t2((struct smb_t2_rsp *)pSMBr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5029 | |
Jeff Layton | 820a803 | 2011-05-04 08:05:26 -0400 | [diff] [blame] | 5030 | if (rc || get_bcc(&pSMBr->hdr) < 24) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5031 | rc = -EIO; /* bad smb */ |
| 5032 | else { |
| 5033 | __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5034 | |
| 5035 | response_data = |
| 5036 | (FILE_SYSTEM_INFO |
| 5037 | *) (((char *) &pSMBr->hdr.Protocol) + |
| 5038 | data_offset); |
| 5039 | FSData->f_bsize = |
| 5040 | le32_to_cpu(response_data->BytesPerSector) * |
| 5041 | le32_to_cpu(response_data-> |
| 5042 | SectorsPerAllocationUnit); |
| 5043 | FSData->f_blocks = |
| 5044 | le64_to_cpu(response_data->TotalAllocationUnits); |
| 5045 | FSData->f_bfree = FSData->f_bavail = |
| 5046 | le64_to_cpu(response_data->FreeAllocationUnits); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5047 | cifs_dbg(FYI, "Blocks: %lld Free: %lld Block size %ld\n", |
| 5048 | (unsigned long long)FSData->f_blocks, |
| 5049 | (unsigned long long)FSData->f_bfree, |
| 5050 | FSData->f_bsize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5051 | } |
| 5052 | } |
| 5053 | cifs_buf_release(pSMB); |
| 5054 | |
| 5055 | if (rc == -EAGAIN) |
| 5056 | goto QFSInfoRetry; |
| 5057 | |
| 5058 | return rc; |
| 5059 | } |
| 5060 | |
| 5061 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 5062 | CIFSSMBQFSAttributeInfo(const unsigned int xid, struct cifs_tcon *tcon) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5063 | { |
| 5064 | /* level 0x105 SMB_QUERY_FILE_SYSTEM_INFO */ |
| 5065 | TRANSACTION2_QFSI_REQ *pSMB = NULL; |
| 5066 | TRANSACTION2_QFSI_RSP *pSMBr = NULL; |
| 5067 | FILE_SYSTEM_ATTRIBUTE_INFO *response_data; |
| 5068 | int rc = 0; |
| 5069 | int bytes_returned = 0; |
| 5070 | __u16 params, byte_count; |
| 5071 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5072 | cifs_dbg(FYI, "In QFSAttributeInfo\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5073 | QFSAttributeRetry: |
| 5074 | rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB, |
| 5075 | (void **) &pSMBr); |
| 5076 | if (rc) |
| 5077 | return rc; |
| 5078 | |
| 5079 | params = 2; /* level */ |
| 5080 | pSMB->TotalDataCount = 0; |
| 5081 | pSMB->MaxParameterCount = cpu_to_le16(2); |
Steve French | 582d21e | 2008-05-13 04:54:12 +0000 | [diff] [blame] | 5082 | /* BB find exact max SMB PDU from sess structure BB */ |
| 5083 | pSMB->MaxDataCount = cpu_to_le16(1000); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5084 | pSMB->MaxSetupCount = 0; |
| 5085 | pSMB->Reserved = 0; |
| 5086 | pSMB->Flags = 0; |
| 5087 | pSMB->Timeout = 0; |
| 5088 | pSMB->Reserved2 = 0; |
| 5089 | byte_count = params + 1 /* pad */ ; |
| 5090 | pSMB->TotalParameterCount = cpu_to_le16(params); |
| 5091 | pSMB->ParameterCount = pSMB->TotalParameterCount; |
| 5092 | pSMB->ParameterOffset = cpu_to_le16(offsetof( |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 5093 | struct smb_com_transaction2_qfsi_req, InformationLevel) - 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5094 | pSMB->DataCount = 0; |
| 5095 | pSMB->DataOffset = 0; |
| 5096 | pSMB->SetupCount = 1; |
| 5097 | pSMB->Reserved3 = 0; |
| 5098 | pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION); |
| 5099 | pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_ATTRIBUTE_INFO); |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 5100 | inc_rfc1001_len(pSMB, byte_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5101 | pSMB->ByteCount = cpu_to_le16(byte_count); |
| 5102 | |
| 5103 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 5104 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
| 5105 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5106 | cifs_dbg(VFS, "Send error in QFSAttributeInfo = %d\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5107 | } else { /* decode response */ |
| 5108 | rc = validate_t2((struct smb_t2_rsp *)pSMBr); |
| 5109 | |
Jeff Layton | 820a803 | 2011-05-04 08:05:26 -0400 | [diff] [blame] | 5110 | if (rc || get_bcc(&pSMBr->hdr) < 13) { |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 5111 | /* BB also check if enough bytes returned */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5112 | rc = -EIO; /* bad smb */ |
| 5113 | } else { |
| 5114 | __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset); |
| 5115 | response_data = |
| 5116 | (FILE_SYSTEM_ATTRIBUTE_INFO |
| 5117 | *) (((char *) &pSMBr->hdr.Protocol) + |
| 5118 | data_offset); |
| 5119 | memcpy(&tcon->fsAttrInfo, response_data, |
Steve French | 26f5736 | 2007-08-30 22:09:15 +0000 | [diff] [blame] | 5120 | sizeof(FILE_SYSTEM_ATTRIBUTE_INFO)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5121 | } |
| 5122 | } |
| 5123 | cifs_buf_release(pSMB); |
| 5124 | |
| 5125 | if (rc == -EAGAIN) |
| 5126 | goto QFSAttributeRetry; |
| 5127 | |
| 5128 | return rc; |
| 5129 | } |
| 5130 | |
| 5131 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 5132 | CIFSSMBQFSDeviceInfo(const unsigned int xid, struct cifs_tcon *tcon) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5133 | { |
| 5134 | /* level 0x104 SMB_QUERY_FILE_SYSTEM_INFO */ |
| 5135 | TRANSACTION2_QFSI_REQ *pSMB = NULL; |
| 5136 | TRANSACTION2_QFSI_RSP *pSMBr = NULL; |
| 5137 | FILE_SYSTEM_DEVICE_INFO *response_data; |
| 5138 | int rc = 0; |
| 5139 | int bytes_returned = 0; |
| 5140 | __u16 params, byte_count; |
| 5141 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5142 | cifs_dbg(FYI, "In QFSDeviceInfo\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5143 | QFSDeviceRetry: |
| 5144 | rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB, |
| 5145 | (void **) &pSMBr); |
| 5146 | if (rc) |
| 5147 | return rc; |
| 5148 | |
| 5149 | params = 2; /* level */ |
| 5150 | pSMB->TotalDataCount = 0; |
| 5151 | pSMB->MaxParameterCount = cpu_to_le16(2); |
Steve French | 582d21e | 2008-05-13 04:54:12 +0000 | [diff] [blame] | 5152 | /* BB find exact max SMB PDU from sess structure BB */ |
| 5153 | pSMB->MaxDataCount = cpu_to_le16(1000); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5154 | pSMB->MaxSetupCount = 0; |
| 5155 | pSMB->Reserved = 0; |
| 5156 | pSMB->Flags = 0; |
| 5157 | pSMB->Timeout = 0; |
| 5158 | pSMB->Reserved2 = 0; |
| 5159 | byte_count = params + 1 /* pad */ ; |
| 5160 | pSMB->TotalParameterCount = cpu_to_le16(params); |
| 5161 | pSMB->ParameterCount = pSMB->TotalParameterCount; |
| 5162 | pSMB->ParameterOffset = cpu_to_le16(offsetof( |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 5163 | struct smb_com_transaction2_qfsi_req, InformationLevel) - 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5164 | |
| 5165 | pSMB->DataCount = 0; |
| 5166 | pSMB->DataOffset = 0; |
| 5167 | pSMB->SetupCount = 1; |
| 5168 | pSMB->Reserved3 = 0; |
| 5169 | pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION); |
| 5170 | pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_DEVICE_INFO); |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 5171 | inc_rfc1001_len(pSMB, byte_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5172 | pSMB->ByteCount = cpu_to_le16(byte_count); |
| 5173 | |
| 5174 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 5175 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
| 5176 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5177 | cifs_dbg(FYI, "Send error in QFSDeviceInfo = %d\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5178 | } else { /* decode response */ |
| 5179 | rc = validate_t2((struct smb_t2_rsp *)pSMBr); |
| 5180 | |
Jeff Layton | 820a803 | 2011-05-04 08:05:26 -0400 | [diff] [blame] | 5181 | if (rc || get_bcc(&pSMBr->hdr) < |
| 5182 | sizeof(FILE_SYSTEM_DEVICE_INFO)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5183 | rc = -EIO; /* bad smb */ |
| 5184 | else { |
| 5185 | __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset); |
| 5186 | response_data = |
Steve French | 737b758 | 2005-04-28 22:41:06 -0700 | [diff] [blame] | 5187 | (FILE_SYSTEM_DEVICE_INFO *) |
| 5188 | (((char *) &pSMBr->hdr.Protocol) + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5189 | data_offset); |
| 5190 | memcpy(&tcon->fsDevInfo, response_data, |
Steve French | 26f5736 | 2007-08-30 22:09:15 +0000 | [diff] [blame] | 5191 | sizeof(FILE_SYSTEM_DEVICE_INFO)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5192 | } |
| 5193 | } |
| 5194 | cifs_buf_release(pSMB); |
| 5195 | |
| 5196 | if (rc == -EAGAIN) |
| 5197 | goto QFSDeviceRetry; |
| 5198 | |
| 5199 | return rc; |
| 5200 | } |
| 5201 | |
| 5202 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 5203 | CIFSSMBQFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5204 | { |
| 5205 | /* level 0x200 SMB_QUERY_CIFS_UNIX_INFO */ |
| 5206 | TRANSACTION2_QFSI_REQ *pSMB = NULL; |
| 5207 | TRANSACTION2_QFSI_RSP *pSMBr = NULL; |
| 5208 | FILE_SYSTEM_UNIX_INFO *response_data; |
| 5209 | int rc = 0; |
| 5210 | int bytes_returned = 0; |
| 5211 | __u16 params, byte_count; |
| 5212 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5213 | cifs_dbg(FYI, "In QFSUnixInfo\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5214 | QFSUnixRetry: |
Jeff Layton | f569599 | 2010-09-29 15:27:08 -0400 | [diff] [blame] | 5215 | rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon, |
| 5216 | (void **) &pSMB, (void **) &pSMBr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5217 | if (rc) |
| 5218 | return rc; |
| 5219 | |
| 5220 | params = 2; /* level */ |
| 5221 | pSMB->TotalDataCount = 0; |
| 5222 | pSMB->DataCount = 0; |
| 5223 | pSMB->DataOffset = 0; |
| 5224 | pSMB->MaxParameterCount = cpu_to_le16(2); |
Steve French | 582d21e | 2008-05-13 04:54:12 +0000 | [diff] [blame] | 5225 | /* BB find exact max SMB PDU from sess structure BB */ |
| 5226 | pSMB->MaxDataCount = cpu_to_le16(100); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5227 | pSMB->MaxSetupCount = 0; |
| 5228 | pSMB->Reserved = 0; |
| 5229 | pSMB->Flags = 0; |
| 5230 | pSMB->Timeout = 0; |
| 5231 | pSMB->Reserved2 = 0; |
| 5232 | byte_count = params + 1 /* pad */ ; |
| 5233 | pSMB->ParameterCount = cpu_to_le16(params); |
| 5234 | pSMB->TotalParameterCount = pSMB->ParameterCount; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 5235 | pSMB->ParameterOffset = cpu_to_le16(offsetof(struct |
| 5236 | smb_com_transaction2_qfsi_req, InformationLevel) - 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5237 | pSMB->SetupCount = 1; |
| 5238 | pSMB->Reserved3 = 0; |
| 5239 | pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION); |
| 5240 | pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_CIFS_UNIX_INFO); |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 5241 | inc_rfc1001_len(pSMB, byte_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5242 | pSMB->ByteCount = cpu_to_le16(byte_count); |
| 5243 | |
| 5244 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 5245 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
| 5246 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5247 | cifs_dbg(VFS, "Send error in QFSUnixInfo = %d\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5248 | } else { /* decode response */ |
| 5249 | rc = validate_t2((struct smb_t2_rsp *)pSMBr); |
| 5250 | |
Jeff Layton | 820a803 | 2011-05-04 08:05:26 -0400 | [diff] [blame] | 5251 | if (rc || get_bcc(&pSMBr->hdr) < 13) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5252 | rc = -EIO; /* bad smb */ |
| 5253 | } else { |
| 5254 | __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset); |
| 5255 | response_data = |
| 5256 | (FILE_SYSTEM_UNIX_INFO |
| 5257 | *) (((char *) &pSMBr->hdr.Protocol) + |
| 5258 | data_offset); |
| 5259 | memcpy(&tcon->fsUnixInfo, response_data, |
Steve French | 26f5736 | 2007-08-30 22:09:15 +0000 | [diff] [blame] | 5260 | sizeof(FILE_SYSTEM_UNIX_INFO)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5261 | } |
| 5262 | } |
| 5263 | cifs_buf_release(pSMB); |
| 5264 | |
| 5265 | if (rc == -EAGAIN) |
| 5266 | goto QFSUnixRetry; |
| 5267 | |
| 5268 | |
| 5269 | return rc; |
| 5270 | } |
| 5271 | |
Jeremy Allison | ac67055 | 2005-06-22 17:26:35 -0700 | [diff] [blame] | 5272 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 5273 | CIFSSMBSetFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon, __u64 cap) |
Jeremy Allison | ac67055 | 2005-06-22 17:26:35 -0700 | [diff] [blame] | 5274 | { |
| 5275 | /* level 0x200 SMB_SET_CIFS_UNIX_INFO */ |
| 5276 | TRANSACTION2_SETFSI_REQ *pSMB = NULL; |
| 5277 | TRANSACTION2_SETFSI_RSP *pSMBr = NULL; |
| 5278 | int rc = 0; |
| 5279 | int bytes_returned = 0; |
| 5280 | __u16 params, param_offset, offset, byte_count; |
| 5281 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5282 | cifs_dbg(FYI, "In SETFSUnixInfo\n"); |
Jeremy Allison | ac67055 | 2005-06-22 17:26:35 -0700 | [diff] [blame] | 5283 | SETFSUnixRetry: |
Steve French | f26282c | 2006-03-01 09:17:37 +0000 | [diff] [blame] | 5284 | /* BB switch to small buf init to save memory */ |
Jeff Layton | f569599 | 2010-09-29 15:27:08 -0400 | [diff] [blame] | 5285 | rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon, |
| 5286 | (void **) &pSMB, (void **) &pSMBr); |
Jeremy Allison | ac67055 | 2005-06-22 17:26:35 -0700 | [diff] [blame] | 5287 | if (rc) |
| 5288 | return rc; |
| 5289 | |
| 5290 | params = 4; /* 2 bytes zero followed by info level. */ |
| 5291 | pSMB->MaxSetupCount = 0; |
| 5292 | pSMB->Reserved = 0; |
| 5293 | pSMB->Flags = 0; |
| 5294 | pSMB->Timeout = 0; |
| 5295 | pSMB->Reserved2 = 0; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 5296 | param_offset = offsetof(struct smb_com_transaction2_setfsi_req, FileNum) |
| 5297 | - 4; |
Jeremy Allison | ac67055 | 2005-06-22 17:26:35 -0700 | [diff] [blame] | 5298 | offset = param_offset + params; |
| 5299 | |
| 5300 | pSMB->MaxParameterCount = cpu_to_le16(4); |
Steve French | 582d21e | 2008-05-13 04:54:12 +0000 | [diff] [blame] | 5301 | /* BB find exact max SMB PDU from sess structure BB */ |
| 5302 | pSMB->MaxDataCount = cpu_to_le16(100); |
Jeremy Allison | ac67055 | 2005-06-22 17:26:35 -0700 | [diff] [blame] | 5303 | pSMB->SetupCount = 1; |
| 5304 | pSMB->Reserved3 = 0; |
| 5305 | pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FS_INFORMATION); |
| 5306 | byte_count = 1 /* pad */ + params + 12; |
| 5307 | |
| 5308 | pSMB->DataCount = cpu_to_le16(12); |
| 5309 | pSMB->ParameterCount = cpu_to_le16(params); |
| 5310 | pSMB->TotalDataCount = pSMB->DataCount; |
| 5311 | pSMB->TotalParameterCount = pSMB->ParameterCount; |
| 5312 | pSMB->ParameterOffset = cpu_to_le16(param_offset); |
| 5313 | pSMB->DataOffset = cpu_to_le16(offset); |
| 5314 | |
| 5315 | /* Params. */ |
| 5316 | pSMB->FileNum = 0; |
| 5317 | pSMB->InformationLevel = cpu_to_le16(SMB_SET_CIFS_UNIX_INFO); |
| 5318 | |
| 5319 | /* Data. */ |
| 5320 | pSMB->ClientUnixMajor = cpu_to_le16(CIFS_UNIX_MAJOR_VERSION); |
| 5321 | pSMB->ClientUnixMinor = cpu_to_le16(CIFS_UNIX_MINOR_VERSION); |
| 5322 | pSMB->ClientUnixCap = cpu_to_le64(cap); |
| 5323 | |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 5324 | inc_rfc1001_len(pSMB, byte_count); |
Jeremy Allison | ac67055 | 2005-06-22 17:26:35 -0700 | [diff] [blame] | 5325 | pSMB->ByteCount = cpu_to_le16(byte_count); |
| 5326 | |
| 5327 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 5328 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
| 5329 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5330 | cifs_dbg(VFS, "Send error in SETFSUnixInfo = %d\n", rc); |
Jeremy Allison | ac67055 | 2005-06-22 17:26:35 -0700 | [diff] [blame] | 5331 | } else { /* decode response */ |
| 5332 | rc = validate_t2((struct smb_t2_rsp *)pSMBr); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 5333 | if (rc) |
Jeremy Allison | ac67055 | 2005-06-22 17:26:35 -0700 | [diff] [blame] | 5334 | rc = -EIO; /* bad smb */ |
Jeremy Allison | ac67055 | 2005-06-22 17:26:35 -0700 | [diff] [blame] | 5335 | } |
| 5336 | cifs_buf_release(pSMB); |
| 5337 | |
| 5338 | if (rc == -EAGAIN) |
| 5339 | goto SETFSUnixRetry; |
| 5340 | |
| 5341 | return rc; |
| 5342 | } |
| 5343 | |
| 5344 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5345 | |
| 5346 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 5347 | CIFSSMBQFSPosixInfo(const unsigned int xid, struct cifs_tcon *tcon, |
Steve French | 737b758 | 2005-04-28 22:41:06 -0700 | [diff] [blame] | 5348 | struct kstatfs *FSData) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5349 | { |
| 5350 | /* level 0x201 SMB_QUERY_CIFS_POSIX_INFO */ |
| 5351 | TRANSACTION2_QFSI_REQ *pSMB = NULL; |
| 5352 | TRANSACTION2_QFSI_RSP *pSMBr = NULL; |
| 5353 | FILE_SYSTEM_POSIX_INFO *response_data; |
| 5354 | int rc = 0; |
| 5355 | int bytes_returned = 0; |
| 5356 | __u16 params, byte_count; |
| 5357 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5358 | cifs_dbg(FYI, "In QFSPosixInfo\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5359 | QFSPosixRetry: |
| 5360 | rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB, |
| 5361 | (void **) &pSMBr); |
| 5362 | if (rc) |
| 5363 | return rc; |
| 5364 | |
| 5365 | params = 2; /* level */ |
| 5366 | pSMB->TotalDataCount = 0; |
| 5367 | pSMB->DataCount = 0; |
| 5368 | pSMB->DataOffset = 0; |
| 5369 | pSMB->MaxParameterCount = cpu_to_le16(2); |
Steve French | 582d21e | 2008-05-13 04:54:12 +0000 | [diff] [blame] | 5370 | /* BB find exact max SMB PDU from sess structure BB */ |
| 5371 | pSMB->MaxDataCount = cpu_to_le16(100); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5372 | pSMB->MaxSetupCount = 0; |
| 5373 | pSMB->Reserved = 0; |
| 5374 | pSMB->Flags = 0; |
| 5375 | pSMB->Timeout = 0; |
| 5376 | pSMB->Reserved2 = 0; |
| 5377 | byte_count = params + 1 /* pad */ ; |
| 5378 | pSMB->ParameterCount = cpu_to_le16(params); |
| 5379 | pSMB->TotalParameterCount = pSMB->ParameterCount; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 5380 | pSMB->ParameterOffset = cpu_to_le16(offsetof(struct |
| 5381 | smb_com_transaction2_qfsi_req, InformationLevel) - 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5382 | pSMB->SetupCount = 1; |
| 5383 | pSMB->Reserved3 = 0; |
| 5384 | pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION); |
| 5385 | pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_FS_INFO); |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 5386 | inc_rfc1001_len(pSMB, byte_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5387 | pSMB->ByteCount = cpu_to_le16(byte_count); |
| 5388 | |
| 5389 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 5390 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
| 5391 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5392 | cifs_dbg(FYI, "Send error in QFSUnixInfo = %d\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5393 | } else { /* decode response */ |
| 5394 | rc = validate_t2((struct smb_t2_rsp *)pSMBr); |
| 5395 | |
Jeff Layton | 820a803 | 2011-05-04 08:05:26 -0400 | [diff] [blame] | 5396 | if (rc || get_bcc(&pSMBr->hdr) < 13) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5397 | rc = -EIO; /* bad smb */ |
| 5398 | } else { |
| 5399 | __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset); |
| 5400 | response_data = |
| 5401 | (FILE_SYSTEM_POSIX_INFO |
| 5402 | *) (((char *) &pSMBr->hdr.Protocol) + |
| 5403 | data_offset); |
| 5404 | FSData->f_bsize = |
| 5405 | le32_to_cpu(response_data->BlockSize); |
| 5406 | FSData->f_blocks = |
| 5407 | le64_to_cpu(response_data->TotalBlocks); |
| 5408 | FSData->f_bfree = |
| 5409 | le64_to_cpu(response_data->BlocksAvail); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 5410 | if (response_data->UserBlocksAvail == cpu_to_le64(-1)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5411 | FSData->f_bavail = FSData->f_bfree; |
| 5412 | } else { |
| 5413 | FSData->f_bavail = |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 5414 | le64_to_cpu(response_data->UserBlocksAvail); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5415 | } |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 5416 | if (response_data->TotalFileNodes != cpu_to_le64(-1)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5417 | FSData->f_files = |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 5418 | le64_to_cpu(response_data->TotalFileNodes); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 5419 | if (response_data->FreeFileNodes != cpu_to_le64(-1)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5420 | FSData->f_ffree = |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 5421 | le64_to_cpu(response_data->FreeFileNodes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5422 | } |
| 5423 | } |
| 5424 | cifs_buf_release(pSMB); |
| 5425 | |
| 5426 | if (rc == -EAGAIN) |
| 5427 | goto QFSPosixRetry; |
| 5428 | |
| 5429 | return rc; |
| 5430 | } |
| 5431 | |
| 5432 | |
Pavel Shilovsky | d143341 | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 5433 | /* |
| 5434 | * We can not use write of zero bytes trick to set file size due to need for |
| 5435 | * large file support. Also note that this SetPathInfo is preferred to |
| 5436 | * SetFileInfo based method in next routine which is only needed to work around |
| 5437 | * a sharing violation bugin Samba which this routine can run into. |
| 5438 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5439 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 5440 | CIFSSMBSetEOF(const unsigned int xid, struct cifs_tcon *tcon, |
Pavel Shilovsky | d143341 | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 5441 | const char *file_name, __u64 size, struct cifs_sb_info *cifs_sb, |
| 5442 | bool set_allocation) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5443 | { |
| 5444 | struct smb_com_transaction2_spi_req *pSMB = NULL; |
| 5445 | struct smb_com_transaction2_spi_rsp *pSMBr = NULL; |
| 5446 | struct file_end_of_file_info *parm_data; |
| 5447 | int name_len; |
| 5448 | int rc = 0; |
| 5449 | int bytes_returned = 0; |
Steve French | 2baa268 | 2014-09-27 02:19:01 -0500 | [diff] [blame] | 5450 | int remap = cifs_remap(cifs_sb); |
Pavel Shilovsky | d143341 | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 5451 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5452 | __u16 params, byte_count, data_count, param_offset, offset; |
| 5453 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5454 | cifs_dbg(FYI, "In SetEOF\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5455 | SetEOFRetry: |
| 5456 | rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB, |
| 5457 | (void **) &pSMBr); |
| 5458 | if (rc) |
| 5459 | return rc; |
| 5460 | |
| 5461 | if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) { |
| 5462 | name_len = |
Pavel Shilovsky | d143341 | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 5463 | cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name, |
| 5464 | PATH_MAX, cifs_sb->local_nls, remap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5465 | name_len++; /* trailing null */ |
| 5466 | name_len *= 2; |
Steve French | 3e87d80 | 2005-09-18 20:49:21 -0700 | [diff] [blame] | 5467 | } else { /* BB improve the check for buffer overruns BB */ |
Pavel Shilovsky | d143341 | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 5468 | name_len = strnlen(file_name, PATH_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5469 | name_len++; /* trailing null */ |
Pavel Shilovsky | d143341 | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 5470 | strncpy(pSMB->FileName, file_name, name_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5471 | } |
| 5472 | params = 6 + name_len; |
Steve French | 26f5736 | 2007-08-30 22:09:15 +0000 | [diff] [blame] | 5473 | data_count = sizeof(struct file_end_of_file_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5474 | pSMB->MaxParameterCount = cpu_to_le16(2); |
Steve French | 3e87d80 | 2005-09-18 20:49:21 -0700 | [diff] [blame] | 5475 | pSMB->MaxDataCount = cpu_to_le16(4100); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5476 | pSMB->MaxSetupCount = 0; |
| 5477 | pSMB->Reserved = 0; |
| 5478 | pSMB->Flags = 0; |
| 5479 | pSMB->Timeout = 0; |
| 5480 | pSMB->Reserved2 = 0; |
| 5481 | param_offset = offsetof(struct smb_com_transaction2_spi_req, |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 5482 | InformationLevel) - 4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5483 | offset = param_offset + params; |
Pavel Shilovsky | d143341 | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 5484 | if (set_allocation) { |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 5485 | if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU) |
| 5486 | pSMB->InformationLevel = |
| 5487 | cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2); |
| 5488 | else |
| 5489 | pSMB->InformationLevel = |
| 5490 | cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO); |
| 5491 | } else /* Set File Size */ { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5492 | if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU) |
| 5493 | pSMB->InformationLevel = |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 5494 | cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5495 | else |
| 5496 | pSMB->InformationLevel = |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 5497 | cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5498 | } |
| 5499 | |
| 5500 | parm_data = |
| 5501 | (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol) + |
| 5502 | offset); |
| 5503 | pSMB->ParameterOffset = cpu_to_le16(param_offset); |
| 5504 | pSMB->DataOffset = cpu_to_le16(offset); |
| 5505 | pSMB->SetupCount = 1; |
| 5506 | pSMB->Reserved3 = 0; |
| 5507 | pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION); |
| 5508 | byte_count = 3 /* pad */ + params + data_count; |
| 5509 | pSMB->DataCount = cpu_to_le16(data_count); |
| 5510 | pSMB->TotalDataCount = pSMB->DataCount; |
| 5511 | pSMB->ParameterCount = cpu_to_le16(params); |
| 5512 | pSMB->TotalParameterCount = pSMB->ParameterCount; |
| 5513 | pSMB->Reserved4 = 0; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 5514 | inc_rfc1001_len(pSMB, byte_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5515 | parm_data->FileSize = cpu_to_le64(size); |
| 5516 | pSMB->ByteCount = cpu_to_le16(byte_count); |
| 5517 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 5518 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 5519 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5520 | cifs_dbg(FYI, "SetPathInfo (file size) returned %d\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5521 | |
| 5522 | cifs_buf_release(pSMB); |
| 5523 | |
| 5524 | if (rc == -EAGAIN) |
| 5525 | goto SetEOFRetry; |
| 5526 | |
| 5527 | return rc; |
| 5528 | } |
| 5529 | |
| 5530 | int |
Pavel Shilovsky | d143341 | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 5531 | CIFSSMBSetFileSize(const unsigned int xid, struct cifs_tcon *tcon, |
| 5532 | struct cifsFileInfo *cfile, __u64 size, bool set_allocation) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5533 | { |
| 5534 | struct smb_com_transaction2_sfi_req *pSMB = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5535 | struct file_end_of_file_info *parm_data; |
| 5536 | int rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5537 | __u16 params, param_offset, offset, byte_count, count; |
| 5538 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5539 | cifs_dbg(FYI, "SetFileSize (via SetFileInfo) %lld\n", |
| 5540 | (long long)size); |
Steve French | cd63499 | 2005-04-28 22:41:10 -0700 | [diff] [blame] | 5541 | rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB); |
| 5542 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5543 | if (rc) |
| 5544 | return rc; |
| 5545 | |
Pavel Shilovsky | d143341 | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 5546 | pSMB->hdr.Pid = cpu_to_le16((__u16)cfile->pid); |
| 5547 | pSMB->hdr.PidHigh = cpu_to_le16((__u16)(cfile->pid >> 16)); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 5548 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5549 | params = 6; |
| 5550 | pSMB->MaxSetupCount = 0; |
| 5551 | pSMB->Reserved = 0; |
| 5552 | pSMB->Flags = 0; |
| 5553 | pSMB->Timeout = 0; |
| 5554 | pSMB->Reserved2 = 0; |
| 5555 | param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4; |
| 5556 | offset = param_offset + params; |
| 5557 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5558 | count = sizeof(struct file_end_of_file_info); |
| 5559 | pSMB->MaxParameterCount = cpu_to_le16(2); |
Steve French | 582d21e | 2008-05-13 04:54:12 +0000 | [diff] [blame] | 5560 | /* BB find exact max SMB PDU from sess structure BB */ |
| 5561 | pSMB->MaxDataCount = cpu_to_le16(1000); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5562 | pSMB->SetupCount = 1; |
| 5563 | pSMB->Reserved3 = 0; |
| 5564 | pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION); |
| 5565 | byte_count = 3 /* pad */ + params + count; |
| 5566 | pSMB->DataCount = cpu_to_le16(count); |
| 5567 | pSMB->ParameterCount = cpu_to_le16(params); |
| 5568 | pSMB->TotalDataCount = pSMB->DataCount; |
| 5569 | pSMB->TotalParameterCount = pSMB->ParameterCount; |
| 5570 | pSMB->ParameterOffset = cpu_to_le16(param_offset); |
| 5571 | parm_data = |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 5572 | (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol) |
| 5573 | + offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5574 | pSMB->DataOffset = cpu_to_le16(offset); |
| 5575 | parm_data->FileSize = cpu_to_le64(size); |
Pavel Shilovsky | d143341 | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 5576 | pSMB->Fid = cfile->fid.netfid; |
| 5577 | if (set_allocation) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5578 | if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU) |
| 5579 | pSMB->InformationLevel = |
| 5580 | cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2); |
| 5581 | else |
| 5582 | pSMB->InformationLevel = |
| 5583 | cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 5584 | } else /* Set File Size */ { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5585 | if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU) |
| 5586 | pSMB->InformationLevel = |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 5587 | cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5588 | else |
| 5589 | pSMB->InformationLevel = |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 5590 | cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5591 | } |
| 5592 | pSMB->Reserved4 = 0; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 5593 | inc_rfc1001_len(pSMB, byte_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5594 | pSMB->ByteCount = cpu_to_le16(byte_count); |
Pavel Shilovsky | 792af7b | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 5595 | rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 5596 | cifs_small_buf_release(pSMB); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5597 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5598 | cifs_dbg(FYI, "Send error in SetFileInfo (SetFileSize) = %d\n", |
| 5599 | rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5600 | } |
| 5601 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 5602 | /* Note: On -EAGAIN error only caller can retry on handle based calls |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5603 | since file handle passed in no longer valid */ |
| 5604 | |
| 5605 | return rc; |
| 5606 | } |
| 5607 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 5608 | /* 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] | 5609 | an open handle, rather than by pathname - this is awkward due to |
| 5610 | potential access conflicts on the open, but it is unavoidable for these |
| 5611 | old servers since the only other choice is to go from 100 nanosecond DCE |
| 5612 | time and resort to the original setpathinfo level which takes the ancient |
| 5613 | DOS time format with 2 second granularity */ |
| 5614 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 5615 | CIFSSMBSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon, |
Jeff Layton | 2dd2dfa | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 5616 | const FILE_BASIC_INFO *data, __u16 fid, __u32 pid_of_opener) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5617 | { |
| 5618 | struct smb_com_transaction2_sfi_req *pSMB = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5619 | char *data_offset; |
| 5620 | int rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5621 | __u16 params, param_offset, offset, byte_count, count; |
| 5622 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5623 | cifs_dbg(FYI, "Set Times (via SetFileInfo)\n"); |
Steve French | cd63499 | 2005-04-28 22:41:10 -0700 | [diff] [blame] | 5624 | rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB); |
| 5625 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5626 | if (rc) |
| 5627 | return rc; |
| 5628 | |
Jeff Layton | 2dd2dfa | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 5629 | pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener); |
| 5630 | pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16)); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 5631 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5632 | params = 6; |
| 5633 | pSMB->MaxSetupCount = 0; |
| 5634 | pSMB->Reserved = 0; |
| 5635 | pSMB->Flags = 0; |
| 5636 | pSMB->Timeout = 0; |
| 5637 | pSMB->Reserved2 = 0; |
| 5638 | param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4; |
| 5639 | offset = param_offset + params; |
| 5640 | |
Jeff Layton | b2a3ad9 | 2012-03-26 09:55:29 -0400 | [diff] [blame] | 5641 | data_offset = (char *)pSMB + |
| 5642 | offsetof(struct smb_hdr, Protocol) + offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5643 | |
Steve French | 26f5736 | 2007-08-30 22:09:15 +0000 | [diff] [blame] | 5644 | count = sizeof(FILE_BASIC_INFO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5645 | pSMB->MaxParameterCount = cpu_to_le16(2); |
Steve French | 582d21e | 2008-05-13 04:54:12 +0000 | [diff] [blame] | 5646 | /* BB find max SMB PDU from sess */ |
| 5647 | pSMB->MaxDataCount = cpu_to_le16(1000); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5648 | pSMB->SetupCount = 1; |
| 5649 | pSMB->Reserved3 = 0; |
| 5650 | pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION); |
| 5651 | byte_count = 3 /* pad */ + params + count; |
| 5652 | pSMB->DataCount = cpu_to_le16(count); |
| 5653 | pSMB->ParameterCount = cpu_to_le16(params); |
| 5654 | pSMB->TotalDataCount = pSMB->DataCount; |
| 5655 | pSMB->TotalParameterCount = pSMB->ParameterCount; |
| 5656 | pSMB->ParameterOffset = cpu_to_le16(param_offset); |
| 5657 | pSMB->DataOffset = cpu_to_le16(offset); |
| 5658 | pSMB->Fid = fid; |
| 5659 | if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU) |
| 5660 | pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2); |
| 5661 | else |
| 5662 | pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO); |
| 5663 | pSMB->Reserved4 = 0; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 5664 | inc_rfc1001_len(pSMB, byte_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5665 | pSMB->ByteCount = cpu_to_le16(byte_count); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 5666 | memcpy(data_offset, data, sizeof(FILE_BASIC_INFO)); |
Pavel Shilovsky | 792af7b | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 5667 | rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 5668 | cifs_small_buf_release(pSMB); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 5669 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5670 | cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n", |
| 5671 | rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5672 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 5673 | /* Note: On -EAGAIN error only caller can retry on handle based calls |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5674 | since file handle passed in no longer valid */ |
| 5675 | |
| 5676 | return rc; |
| 5677 | } |
| 5678 | |
Jeff Layton | 6d22f09 | 2008-09-23 11:48:35 -0400 | [diff] [blame] | 5679 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 5680 | CIFSSMBSetFileDisposition(const unsigned int xid, struct cifs_tcon *tcon, |
Jeff Layton | 6d22f09 | 2008-09-23 11:48:35 -0400 | [diff] [blame] | 5681 | bool delete_file, __u16 fid, __u32 pid_of_opener) |
| 5682 | { |
| 5683 | struct smb_com_transaction2_sfi_req *pSMB = NULL; |
| 5684 | char *data_offset; |
| 5685 | int rc = 0; |
| 5686 | __u16 params, param_offset, offset, byte_count, count; |
| 5687 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5688 | cifs_dbg(FYI, "Set File Disposition (via SetFileInfo)\n"); |
Jeff Layton | 6d22f09 | 2008-09-23 11:48:35 -0400 | [diff] [blame] | 5689 | rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB); |
| 5690 | |
| 5691 | if (rc) |
| 5692 | return rc; |
| 5693 | |
| 5694 | pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener); |
| 5695 | pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16)); |
| 5696 | |
| 5697 | params = 6; |
| 5698 | pSMB->MaxSetupCount = 0; |
| 5699 | pSMB->Reserved = 0; |
| 5700 | pSMB->Flags = 0; |
| 5701 | pSMB->Timeout = 0; |
| 5702 | pSMB->Reserved2 = 0; |
| 5703 | param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4; |
| 5704 | offset = param_offset + params; |
| 5705 | |
| 5706 | data_offset = (char *) (&pSMB->hdr.Protocol) + offset; |
| 5707 | |
| 5708 | count = 1; |
| 5709 | pSMB->MaxParameterCount = cpu_to_le16(2); |
| 5710 | /* BB find max SMB PDU from sess */ |
| 5711 | pSMB->MaxDataCount = cpu_to_le16(1000); |
| 5712 | pSMB->SetupCount = 1; |
| 5713 | pSMB->Reserved3 = 0; |
| 5714 | pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION); |
| 5715 | byte_count = 3 /* pad */ + params + count; |
| 5716 | pSMB->DataCount = cpu_to_le16(count); |
| 5717 | pSMB->ParameterCount = cpu_to_le16(params); |
| 5718 | pSMB->TotalDataCount = pSMB->DataCount; |
| 5719 | pSMB->TotalParameterCount = pSMB->ParameterCount; |
| 5720 | pSMB->ParameterOffset = cpu_to_le16(param_offset); |
| 5721 | pSMB->DataOffset = cpu_to_le16(offset); |
| 5722 | pSMB->Fid = fid; |
| 5723 | pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_DISPOSITION_INFO); |
| 5724 | pSMB->Reserved4 = 0; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 5725 | inc_rfc1001_len(pSMB, byte_count); |
Jeff Layton | 6d22f09 | 2008-09-23 11:48:35 -0400 | [diff] [blame] | 5726 | pSMB->ByteCount = cpu_to_le16(byte_count); |
| 5727 | *data_offset = delete_file ? 1 : 0; |
Pavel Shilovsky | 792af7b | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 5728 | rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 5729 | cifs_small_buf_release(pSMB); |
Jeff Layton | 6d22f09 | 2008-09-23 11:48:35 -0400 | [diff] [blame] | 5730 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5731 | cifs_dbg(FYI, "Send error in SetFileDisposition = %d\n", rc); |
Jeff Layton | 6d22f09 | 2008-09-23 11:48:35 -0400 | [diff] [blame] | 5732 | |
| 5733 | return rc; |
| 5734 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5735 | |
| 5736 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 5737 | CIFSSMBSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon, |
Jeff Layton | 6fc000e | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 5738 | const char *fileName, const FILE_BASIC_INFO *data, |
| 5739 | const struct nls_table *nls_codepage, int remap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5740 | { |
| 5741 | TRANSACTION2_SPI_REQ *pSMB = NULL; |
| 5742 | TRANSACTION2_SPI_RSP *pSMBr = NULL; |
| 5743 | int name_len; |
| 5744 | int rc = 0; |
| 5745 | int bytes_returned = 0; |
| 5746 | char *data_offset; |
| 5747 | __u16 params, param_offset, offset, byte_count, count; |
| 5748 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5749 | cifs_dbg(FYI, "In SetTimes\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5750 | |
| 5751 | SetTimesRetry: |
| 5752 | rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB, |
| 5753 | (void **) &pSMBr); |
| 5754 | if (rc) |
| 5755 | return rc; |
| 5756 | |
| 5757 | if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) { |
| 5758 | name_len = |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 5759 | cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName, |
| 5760 | PATH_MAX, nls_codepage, remap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5761 | name_len++; /* trailing null */ |
| 5762 | name_len *= 2; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 5763 | } else { /* BB improve the check for buffer overruns BB */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5764 | name_len = strnlen(fileName, PATH_MAX); |
| 5765 | name_len++; /* trailing null */ |
| 5766 | strncpy(pSMB->FileName, fileName, name_len); |
| 5767 | } |
| 5768 | |
| 5769 | params = 6 + name_len; |
Steve French | 26f5736 | 2007-08-30 22:09:15 +0000 | [diff] [blame] | 5770 | count = sizeof(FILE_BASIC_INFO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5771 | pSMB->MaxParameterCount = cpu_to_le16(2); |
Steve French | 582d21e | 2008-05-13 04:54:12 +0000 | [diff] [blame] | 5772 | /* BB find max SMB PDU from sess structure BB */ |
| 5773 | pSMB->MaxDataCount = cpu_to_le16(1000); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5774 | pSMB->MaxSetupCount = 0; |
| 5775 | pSMB->Reserved = 0; |
| 5776 | pSMB->Flags = 0; |
| 5777 | pSMB->Timeout = 0; |
| 5778 | pSMB->Reserved2 = 0; |
| 5779 | param_offset = offsetof(struct smb_com_transaction2_spi_req, |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 5780 | InformationLevel) - 4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5781 | offset = param_offset + params; |
| 5782 | data_offset = (char *) (&pSMB->hdr.Protocol) + offset; |
| 5783 | pSMB->ParameterOffset = cpu_to_le16(param_offset); |
| 5784 | pSMB->DataOffset = cpu_to_le16(offset); |
| 5785 | pSMB->SetupCount = 1; |
| 5786 | pSMB->Reserved3 = 0; |
| 5787 | pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION); |
| 5788 | byte_count = 3 /* pad */ + params + count; |
| 5789 | |
| 5790 | pSMB->DataCount = cpu_to_le16(count); |
| 5791 | pSMB->ParameterCount = cpu_to_le16(params); |
| 5792 | pSMB->TotalDataCount = pSMB->DataCount; |
| 5793 | pSMB->TotalParameterCount = pSMB->ParameterCount; |
| 5794 | if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU) |
| 5795 | pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2); |
| 5796 | else |
| 5797 | pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO); |
| 5798 | pSMB->Reserved4 = 0; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 5799 | inc_rfc1001_len(pSMB, byte_count); |
Steve French | 26f5736 | 2007-08-30 22:09:15 +0000 | [diff] [blame] | 5800 | memcpy(data_offset, data, sizeof(FILE_BASIC_INFO)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5801 | pSMB->ByteCount = cpu_to_le16(byte_count); |
| 5802 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 5803 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 5804 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5805 | cifs_dbg(FYI, "SetPathInfo (times) returned %d\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5806 | |
| 5807 | cifs_buf_release(pSMB); |
| 5808 | |
| 5809 | if (rc == -EAGAIN) |
| 5810 | goto SetTimesRetry; |
| 5811 | |
| 5812 | return rc; |
| 5813 | } |
| 5814 | |
| 5815 | /* Can not be used to set time stamps yet (due to old DOS time format) */ |
| 5816 | /* Can be used to set attributes */ |
| 5817 | #if 0 /* Possibly not needed - since it turns out that strangely NT4 has a bug |
| 5818 | handling it anyway and NT4 was what we thought it would be needed for |
| 5819 | Do not delete it until we prove whether needed for Win9x though */ |
| 5820 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 5821 | CIFSSMBSetAttrLegacy(unsigned int xid, struct cifs_tcon *tcon, char *fileName, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5822 | __u16 dos_attrs, const struct nls_table *nls_codepage) |
| 5823 | { |
| 5824 | SETATTR_REQ *pSMB = NULL; |
| 5825 | SETATTR_RSP *pSMBr = NULL; |
| 5826 | int rc = 0; |
| 5827 | int bytes_returned; |
| 5828 | int name_len; |
| 5829 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5830 | cifs_dbg(FYI, "In SetAttrLegacy\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5831 | |
| 5832 | SetAttrLgcyRetry: |
| 5833 | rc = smb_init(SMB_COM_SETATTR, 8, tcon, (void **) &pSMB, |
| 5834 | (void **) &pSMBr); |
| 5835 | if (rc) |
| 5836 | return rc; |
| 5837 | |
| 5838 | if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) { |
| 5839 | name_len = |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 5840 | ConvertToUTF16((__le16 *) pSMB->fileName, fileName, |
| 5841 | PATH_MAX, nls_codepage); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5842 | name_len++; /* trailing null */ |
| 5843 | name_len *= 2; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 5844 | } else { /* BB improve the check for buffer overruns BB */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5845 | name_len = strnlen(fileName, PATH_MAX); |
| 5846 | name_len++; /* trailing null */ |
| 5847 | strncpy(pSMB->fileName, fileName, name_len); |
| 5848 | } |
| 5849 | pSMB->attr = cpu_to_le16(dos_attrs); |
| 5850 | pSMB->BufferFormat = 0x04; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 5851 | inc_rfc1001_len(pSMB, name_len + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5852 | pSMB->ByteCount = cpu_to_le16(name_len + 1); |
| 5853 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 5854 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 5855 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5856 | cifs_dbg(FYI, "Error in LegacySetAttr = %d\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5857 | |
| 5858 | cifs_buf_release(pSMB); |
| 5859 | |
| 5860 | if (rc == -EAGAIN) |
| 5861 | goto SetAttrLgcyRetry; |
| 5862 | |
| 5863 | return rc; |
| 5864 | } |
| 5865 | #endif /* temporarily unneeded SetAttr legacy function */ |
| 5866 | |
Jeff Layton | 654cf14 | 2009-07-09 20:02:49 -0400 | [diff] [blame] | 5867 | static void |
| 5868 | cifs_fill_unix_set_info(FILE_UNIX_BASIC_INFO *data_offset, |
| 5869 | const struct cifs_unix_set_info_args *args) |
| 5870 | { |
Eric W. Biederman | 49418b2 | 2013-02-06 00:57:56 -0800 | [diff] [blame] | 5871 | u64 uid = NO_CHANGE_64, gid = NO_CHANGE_64; |
Jeff Layton | 654cf14 | 2009-07-09 20:02:49 -0400 | [diff] [blame] | 5872 | u64 mode = args->mode; |
| 5873 | |
Eric W. Biederman | 49418b2 | 2013-02-06 00:57:56 -0800 | [diff] [blame] | 5874 | if (uid_valid(args->uid)) |
| 5875 | uid = from_kuid(&init_user_ns, args->uid); |
| 5876 | if (gid_valid(args->gid)) |
| 5877 | gid = from_kgid(&init_user_ns, args->gid); |
| 5878 | |
Jeff Layton | 654cf14 | 2009-07-09 20:02:49 -0400 | [diff] [blame] | 5879 | /* |
| 5880 | * Samba server ignores set of file size to zero due to bugs in some |
| 5881 | * older clients, but we should be precise - we use SetFileSize to |
| 5882 | * 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] | 5883 | * accidentally as happened on one Samba server beta by putting |
Jeff Layton | 654cf14 | 2009-07-09 20:02:49 -0400 | [diff] [blame] | 5884 | * zero instead of -1 here |
| 5885 | */ |
| 5886 | data_offset->EndOfFile = cpu_to_le64(NO_CHANGE_64); |
| 5887 | data_offset->NumOfBytes = cpu_to_le64(NO_CHANGE_64); |
| 5888 | data_offset->LastStatusChange = cpu_to_le64(args->ctime); |
| 5889 | data_offset->LastAccessTime = cpu_to_le64(args->atime); |
| 5890 | data_offset->LastModificationTime = cpu_to_le64(args->mtime); |
Eric W. Biederman | 49418b2 | 2013-02-06 00:57:56 -0800 | [diff] [blame] | 5891 | data_offset->Uid = cpu_to_le64(uid); |
| 5892 | data_offset->Gid = cpu_to_le64(gid); |
Jeff Layton | 654cf14 | 2009-07-09 20:02:49 -0400 | [diff] [blame] | 5893 | /* better to leave device as zero when it is */ |
| 5894 | data_offset->DevMajor = cpu_to_le64(MAJOR(args->device)); |
| 5895 | data_offset->DevMinor = cpu_to_le64(MINOR(args->device)); |
| 5896 | data_offset->Permissions = cpu_to_le64(mode); |
| 5897 | |
| 5898 | if (S_ISREG(mode)) |
| 5899 | data_offset->Type = cpu_to_le32(UNIX_FILE); |
| 5900 | else if (S_ISDIR(mode)) |
| 5901 | data_offset->Type = cpu_to_le32(UNIX_DIR); |
| 5902 | else if (S_ISLNK(mode)) |
| 5903 | data_offset->Type = cpu_to_le32(UNIX_SYMLINK); |
| 5904 | else if (S_ISCHR(mode)) |
| 5905 | data_offset->Type = cpu_to_le32(UNIX_CHARDEV); |
| 5906 | else if (S_ISBLK(mode)) |
| 5907 | data_offset->Type = cpu_to_le32(UNIX_BLOCKDEV); |
| 5908 | else if (S_ISFIFO(mode)) |
| 5909 | data_offset->Type = cpu_to_le32(UNIX_FIFO); |
| 5910 | else if (S_ISSOCK(mode)) |
| 5911 | data_offset->Type = cpu_to_le32(UNIX_SOCKET); |
| 5912 | } |
| 5913 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5914 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 5915 | CIFSSMBUnixSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon, |
Jeff Layton | 3bbeeb3 | 2009-07-09 20:02:50 -0400 | [diff] [blame] | 5916 | const struct cifs_unix_set_info_args *args, |
| 5917 | u16 fid, u32 pid_of_opener) |
| 5918 | { |
| 5919 | struct smb_com_transaction2_sfi_req *pSMB = NULL; |
Jeff Layton | b2a3ad9 | 2012-03-26 09:55:29 -0400 | [diff] [blame] | 5920 | char *data_offset; |
Jeff Layton | 3bbeeb3 | 2009-07-09 20:02:50 -0400 | [diff] [blame] | 5921 | int rc = 0; |
| 5922 | u16 params, param_offset, offset, byte_count, count; |
| 5923 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5924 | cifs_dbg(FYI, "Set Unix Info (via SetFileInfo)\n"); |
Jeff Layton | 3bbeeb3 | 2009-07-09 20:02:50 -0400 | [diff] [blame] | 5925 | rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB); |
| 5926 | |
| 5927 | if (rc) |
| 5928 | return rc; |
| 5929 | |
| 5930 | pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener); |
| 5931 | pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16)); |
| 5932 | |
| 5933 | params = 6; |
| 5934 | pSMB->MaxSetupCount = 0; |
| 5935 | pSMB->Reserved = 0; |
| 5936 | pSMB->Flags = 0; |
| 5937 | pSMB->Timeout = 0; |
| 5938 | pSMB->Reserved2 = 0; |
| 5939 | param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4; |
| 5940 | offset = param_offset + params; |
| 5941 | |
Jeff Layton | b2a3ad9 | 2012-03-26 09:55:29 -0400 | [diff] [blame] | 5942 | data_offset = (char *)pSMB + |
| 5943 | offsetof(struct smb_hdr, Protocol) + offset; |
| 5944 | |
Jeff Layton | 3bbeeb3 | 2009-07-09 20:02:50 -0400 | [diff] [blame] | 5945 | count = sizeof(FILE_UNIX_BASIC_INFO); |
| 5946 | |
| 5947 | pSMB->MaxParameterCount = cpu_to_le16(2); |
| 5948 | /* BB find max SMB PDU from sess */ |
| 5949 | pSMB->MaxDataCount = cpu_to_le16(1000); |
| 5950 | pSMB->SetupCount = 1; |
| 5951 | pSMB->Reserved3 = 0; |
| 5952 | pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION); |
| 5953 | byte_count = 3 /* pad */ + params + count; |
| 5954 | pSMB->DataCount = cpu_to_le16(count); |
| 5955 | pSMB->ParameterCount = cpu_to_le16(params); |
| 5956 | pSMB->TotalDataCount = pSMB->DataCount; |
| 5957 | pSMB->TotalParameterCount = pSMB->ParameterCount; |
| 5958 | pSMB->ParameterOffset = cpu_to_le16(param_offset); |
| 5959 | pSMB->DataOffset = cpu_to_le16(offset); |
| 5960 | pSMB->Fid = fid; |
| 5961 | pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC); |
| 5962 | pSMB->Reserved4 = 0; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 5963 | inc_rfc1001_len(pSMB, byte_count); |
Jeff Layton | 3bbeeb3 | 2009-07-09 20:02:50 -0400 | [diff] [blame] | 5964 | pSMB->ByteCount = cpu_to_le16(byte_count); |
| 5965 | |
Jeff Layton | b2a3ad9 | 2012-03-26 09:55:29 -0400 | [diff] [blame] | 5966 | cifs_fill_unix_set_info((FILE_UNIX_BASIC_INFO *)data_offset, args); |
Jeff Layton | 3bbeeb3 | 2009-07-09 20:02:50 -0400 | [diff] [blame] | 5967 | |
Pavel Shilovsky | 792af7b | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 5968 | rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 5969 | cifs_small_buf_release(pSMB); |
Jeff Layton | 3bbeeb3 | 2009-07-09 20:02:50 -0400 | [diff] [blame] | 5970 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5971 | cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n", |
| 5972 | rc); |
Jeff Layton | 3bbeeb3 | 2009-07-09 20:02:50 -0400 | [diff] [blame] | 5973 | |
| 5974 | /* Note: On -EAGAIN error only caller can retry on handle based calls |
| 5975 | since file handle passed in no longer valid */ |
| 5976 | |
| 5977 | return rc; |
| 5978 | } |
| 5979 | |
| 5980 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 5981 | CIFSSMBUnixSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon, |
Pavel Shilovsky | ff691e9 | 2012-07-13 14:04:46 +0400 | [diff] [blame] | 5982 | const char *file_name, |
Jeff Layton | 01ea95e | 2009-07-09 20:02:49 -0400 | [diff] [blame] | 5983 | const struct cifs_unix_set_info_args *args, |
| 5984 | const struct nls_table *nls_codepage, int remap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5985 | { |
| 5986 | TRANSACTION2_SPI_REQ *pSMB = NULL; |
| 5987 | TRANSACTION2_SPI_RSP *pSMBr = NULL; |
| 5988 | int name_len; |
| 5989 | int rc = 0; |
| 5990 | int bytes_returned = 0; |
| 5991 | FILE_UNIX_BASIC_INFO *data_offset; |
| 5992 | __u16 params, param_offset, offset, count, byte_count; |
| 5993 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5994 | cifs_dbg(FYI, "In SetUID/GID/Mode\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5995 | setPermsRetry: |
| 5996 | rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB, |
| 5997 | (void **) &pSMBr); |
| 5998 | if (rc) |
| 5999 | return rc; |
| 6000 | |
| 6001 | if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) { |
| 6002 | name_len = |
Pavel Shilovsky | ff691e9 | 2012-07-13 14:04:46 +0400 | [diff] [blame] | 6003 | cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name, |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 6004 | PATH_MAX, nls_codepage, remap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6005 | name_len++; /* trailing null */ |
| 6006 | name_len *= 2; |
Steve French | 3e87d80 | 2005-09-18 20:49:21 -0700 | [diff] [blame] | 6007 | } else { /* BB improve the check for buffer overruns BB */ |
Pavel Shilovsky | ff691e9 | 2012-07-13 14:04:46 +0400 | [diff] [blame] | 6008 | name_len = strnlen(file_name, PATH_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6009 | name_len++; /* trailing null */ |
Pavel Shilovsky | ff691e9 | 2012-07-13 14:04:46 +0400 | [diff] [blame] | 6010 | strncpy(pSMB->FileName, file_name, name_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6011 | } |
| 6012 | |
| 6013 | params = 6 + name_len; |
Steve French | 26f5736 | 2007-08-30 22:09:15 +0000 | [diff] [blame] | 6014 | count = sizeof(FILE_UNIX_BASIC_INFO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6015 | pSMB->MaxParameterCount = cpu_to_le16(2); |
Steve French | 582d21e | 2008-05-13 04:54:12 +0000 | [diff] [blame] | 6016 | /* BB find max SMB PDU from sess structure BB */ |
| 6017 | pSMB->MaxDataCount = cpu_to_le16(1000); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6018 | pSMB->MaxSetupCount = 0; |
| 6019 | pSMB->Reserved = 0; |
| 6020 | pSMB->Flags = 0; |
| 6021 | pSMB->Timeout = 0; |
| 6022 | pSMB->Reserved2 = 0; |
| 6023 | param_offset = offsetof(struct smb_com_transaction2_spi_req, |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 6024 | InformationLevel) - 4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6025 | offset = param_offset + params; |
| 6026 | data_offset = |
| 6027 | (FILE_UNIX_BASIC_INFO *) ((char *) &pSMB->hdr.Protocol + |
| 6028 | offset); |
| 6029 | memset(data_offset, 0, count); |
| 6030 | pSMB->DataOffset = cpu_to_le16(offset); |
| 6031 | pSMB->ParameterOffset = cpu_to_le16(param_offset); |
| 6032 | pSMB->SetupCount = 1; |
| 6033 | pSMB->Reserved3 = 0; |
| 6034 | pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION); |
| 6035 | byte_count = 3 /* pad */ + params + count; |
| 6036 | pSMB->ParameterCount = cpu_to_le16(params); |
| 6037 | pSMB->DataCount = cpu_to_le16(count); |
| 6038 | pSMB->TotalParameterCount = pSMB->ParameterCount; |
| 6039 | pSMB->TotalDataCount = pSMB->DataCount; |
| 6040 | pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC); |
| 6041 | pSMB->Reserved4 = 0; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 6042 | inc_rfc1001_len(pSMB, byte_count); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 6043 | |
Jeff Layton | 654cf14 | 2009-07-09 20:02:49 -0400 | [diff] [blame] | 6044 | cifs_fill_unix_set_info(data_offset, args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6045 | |
| 6046 | pSMB->ByteCount = cpu_to_le16(byte_count); |
| 6047 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 6048 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 6049 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 6050 | cifs_dbg(FYI, "SetPathInfo (perms) returned %d\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6051 | |
Steve French | 0d817bc | 2008-05-22 02:02:03 +0000 | [diff] [blame] | 6052 | cifs_buf_release(pSMB); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6053 | if (rc == -EAGAIN) |
| 6054 | goto setPermsRetry; |
| 6055 | return rc; |
| 6056 | } |
| 6057 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6058 | #ifdef CONFIG_CIFS_XATTR |
Jeff Layton | 31c0519 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6059 | /* |
| 6060 | * Do a path-based QUERY_ALL_EAS call and parse the result. This is a common |
| 6061 | * function used by listxattr and getxattr type calls. When ea_name is set, |
| 6062 | * it looks for that attribute name and stuffs that value into the EAData |
| 6063 | * buffer. When ea_name is NULL, it stuffs a list of attribute names into the |
| 6064 | * buffer. In both cases, the return value is either the length of the |
| 6065 | * resulting data or a negative error code. If EAData is a NULL pointer then |
| 6066 | * the data isn't copied to it, but the length is returned. |
| 6067 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6068 | ssize_t |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 6069 | CIFSSMBQAllEAs(const unsigned int xid, struct cifs_tcon *tcon, |
Jeff Layton | 31c0519 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6070 | const unsigned char *searchName, const unsigned char *ea_name, |
| 6071 | char *EAData, size_t buf_size, |
| 6072 | const struct nls_table *nls_codepage, int remap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6073 | { |
| 6074 | /* BB assumes one setup word */ |
| 6075 | TRANSACTION2_QPI_REQ *pSMB = NULL; |
| 6076 | TRANSACTION2_QPI_RSP *pSMBr = NULL; |
| 6077 | int rc = 0; |
| 6078 | int bytes_returned; |
Jeff Layton | 6e462b9 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6079 | int list_len; |
Jeff Layton | f0d3868 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6080 | struct fealist *ea_response_data; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 6081 | struct fea *temp_fea; |
| 6082 | char *temp_ptr; |
Jeff Layton | 0cd126b | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6083 | char *end_of_smb; |
Jeff Layton | f0d3868 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6084 | __u16 params, byte_count, data_offset; |
Jeff Layton | 5980fc9 | 2011-07-28 12:48:26 -0400 | [diff] [blame] | 6085 | unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6086 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 6087 | cifs_dbg(FYI, "In Query All EAs path %s\n", searchName); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6088 | QAllEAsRetry: |
| 6089 | rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB, |
| 6090 | (void **) &pSMBr); |
| 6091 | if (rc) |
| 6092 | return rc; |
| 6093 | |
| 6094 | if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) { |
Jeff Layton | 6e462b9 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6095 | list_len = |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 6096 | cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName, |
| 6097 | PATH_MAX, nls_codepage, remap); |
Jeff Layton | 6e462b9 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6098 | list_len++; /* trailing null */ |
| 6099 | list_len *= 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6100 | } else { /* BB improve the check for buffer overruns BB */ |
Jeff Layton | 6e462b9 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6101 | list_len = strnlen(searchName, PATH_MAX); |
| 6102 | list_len++; /* trailing null */ |
| 6103 | strncpy(pSMB->FileName, searchName, list_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6104 | } |
| 6105 | |
Jeff Layton | 6e462b9 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6106 | params = 2 /* level */ + 4 /* reserved */ + list_len /* includes NUL */; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6107 | pSMB->TotalDataCount = 0; |
| 6108 | pSMB->MaxParameterCount = cpu_to_le16(2); |
Steve French | 582d21e | 2008-05-13 04:54:12 +0000 | [diff] [blame] | 6109 | /* BB find exact max SMB PDU from sess structure BB */ |
Jeff Layton | e529614 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6110 | pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6111 | pSMB->MaxSetupCount = 0; |
| 6112 | pSMB->Reserved = 0; |
| 6113 | pSMB->Flags = 0; |
| 6114 | pSMB->Timeout = 0; |
| 6115 | pSMB->Reserved2 = 0; |
| 6116 | pSMB->ParameterOffset = cpu_to_le16(offsetof( |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 6117 | struct smb_com_transaction2_qpi_req, InformationLevel) - 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6118 | pSMB->DataCount = 0; |
| 6119 | pSMB->DataOffset = 0; |
| 6120 | pSMB->SetupCount = 1; |
| 6121 | pSMB->Reserved3 = 0; |
| 6122 | pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION); |
| 6123 | byte_count = params + 1 /* pad */ ; |
| 6124 | pSMB->TotalParameterCount = cpu_to_le16(params); |
| 6125 | pSMB->ParameterCount = pSMB->TotalParameterCount; |
| 6126 | pSMB->InformationLevel = cpu_to_le16(SMB_INFO_QUERY_ALL_EAS); |
| 6127 | pSMB->Reserved4 = 0; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 6128 | inc_rfc1001_len(pSMB, byte_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6129 | pSMB->ByteCount = cpu_to_le16(byte_count); |
| 6130 | |
| 6131 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 6132 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
| 6133 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 6134 | cifs_dbg(FYI, "Send error in QueryAllEAs = %d\n", rc); |
Jeff Layton | f0d3868 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6135 | goto QAllEAsOut; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6136 | } |
Jeff Layton | f0d3868 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6137 | |
| 6138 | |
| 6139 | /* BB also check enough total bytes returned */ |
| 6140 | /* BB we need to improve the validity checking |
| 6141 | of these trans2 responses */ |
| 6142 | |
| 6143 | rc = validate_t2((struct smb_t2_rsp *)pSMBr); |
Jeff Layton | 820a803 | 2011-05-04 08:05:26 -0400 | [diff] [blame] | 6144 | if (rc || get_bcc(&pSMBr->hdr) < 4) { |
Jeff Layton | f0d3868 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6145 | rc = -EIO; /* bad smb */ |
| 6146 | goto QAllEAsOut; |
| 6147 | } |
| 6148 | |
| 6149 | /* check that length of list is not more than bcc */ |
| 6150 | /* check that each entry does not go beyond length |
| 6151 | of list */ |
| 6152 | /* check that each element of each entry does not |
| 6153 | go beyond end of list */ |
| 6154 | /* validate_trans2_offsets() */ |
| 6155 | /* BB check if start of smb + data_offset > &bcc+ bcc */ |
| 6156 | |
| 6157 | data_offset = le16_to_cpu(pSMBr->t2.DataOffset); |
| 6158 | ea_response_data = (struct fealist *) |
| 6159 | (((char *) &pSMBr->hdr.Protocol) + data_offset); |
| 6160 | |
Jeff Layton | 6e462b9 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6161 | list_len = le32_to_cpu(ea_response_data->list_len); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 6162 | cifs_dbg(FYI, "ea length %d\n", list_len); |
Jeff Layton | 6e462b9 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6163 | if (list_len <= 8) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 6164 | cifs_dbg(FYI, "empty EA list returned from server\n"); |
Steve French | 60977fc | 2014-03-25 19:46:36 -0500 | [diff] [blame] | 6165 | /* didn't find the named attribute */ |
| 6166 | if (ea_name) |
| 6167 | rc = -ENODATA; |
Jeff Layton | f0d3868 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6168 | goto QAllEAsOut; |
| 6169 | } |
| 6170 | |
Jeff Layton | 0cd126b | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6171 | /* make sure list_len doesn't go past end of SMB */ |
Jeff Layton | 690c522 | 2011-01-20 13:36:51 -0500 | [diff] [blame] | 6172 | end_of_smb = (char *)pByteArea(&pSMBr->hdr) + get_bcc(&pSMBr->hdr); |
Jeff Layton | 0cd126b | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6173 | if ((char *)ea_response_data + list_len > end_of_smb) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 6174 | cifs_dbg(FYI, "EA list appears to go beyond SMB\n"); |
Jeff Layton | 0cd126b | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6175 | rc = -EIO; |
| 6176 | goto QAllEAsOut; |
| 6177 | } |
| 6178 | |
Jeff Layton | f0d3868 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6179 | /* account for ea list len */ |
Jeff Layton | 6e462b9 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6180 | list_len -= 4; |
Jeff Layton | f0d3868 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6181 | temp_fea = ea_response_data->list; |
| 6182 | temp_ptr = (char *)temp_fea; |
Jeff Layton | 6e462b9 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6183 | while (list_len > 0) { |
Steve French | 122ca00 | 2010-02-24 21:56:48 +0000 | [diff] [blame] | 6184 | unsigned int name_len; |
Jeff Layton | f0d3868 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6185 | __u16 value_len; |
Jeff Layton | 0cd126b | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6186 | |
Jeff Layton | 6e462b9 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6187 | list_len -= 4; |
Jeff Layton | f0d3868 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6188 | temp_ptr += 4; |
Jeff Layton | 0cd126b | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6189 | /* make sure we can read name_len and value_len */ |
| 6190 | if (list_len < 0) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 6191 | cifs_dbg(FYI, "EA entry goes beyond length of list\n"); |
Jeff Layton | 0cd126b | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6192 | rc = -EIO; |
| 6193 | goto QAllEAsOut; |
| 6194 | } |
| 6195 | |
| 6196 | name_len = temp_fea->name_len; |
| 6197 | value_len = le16_to_cpu(temp_fea->value_len); |
| 6198 | list_len -= name_len + 1 + value_len; |
| 6199 | if (list_len < 0) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 6200 | cifs_dbg(FYI, "EA entry goes beyond length of list\n"); |
Jeff Layton | 0cd126b | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6201 | rc = -EIO; |
| 6202 | goto QAllEAsOut; |
| 6203 | } |
| 6204 | |
Jeff Layton | 31c0519 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6205 | if (ea_name) { |
Jeff Layton | 91d065c | 2011-07-26 18:23:47 -0400 | [diff] [blame] | 6206 | if (ea_name_len == name_len && |
Jeff Layton | ac42344 | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 6207 | memcmp(ea_name, temp_ptr, name_len) == 0) { |
Jeff Layton | 31c0519 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6208 | temp_ptr += name_len + 1; |
| 6209 | rc = value_len; |
| 6210 | if (buf_size == 0) |
| 6211 | goto QAllEAsOut; |
| 6212 | if ((size_t)value_len > buf_size) { |
| 6213 | rc = -ERANGE; |
| 6214 | goto QAllEAsOut; |
| 6215 | } |
| 6216 | memcpy(EAData, temp_ptr, value_len); |
| 6217 | goto QAllEAsOut; |
| 6218 | } |
Jeff Layton | f0d3868 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6219 | } else { |
Jeff Layton | 31c0519 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6220 | /* account for prefix user. and trailing null */ |
| 6221 | rc += (5 + 1 + name_len); |
| 6222 | if (rc < (int) buf_size) { |
| 6223 | memcpy(EAData, "user.", 5); |
| 6224 | EAData += 5; |
| 6225 | memcpy(EAData, temp_ptr, name_len); |
| 6226 | EAData += name_len; |
| 6227 | /* null terminate name */ |
| 6228 | *EAData = 0; |
| 6229 | ++EAData; |
| 6230 | } else if (buf_size == 0) { |
| 6231 | /* skip copy - calc size only */ |
| 6232 | } else { |
| 6233 | /* stop before overrun buffer */ |
| 6234 | rc = -ERANGE; |
| 6235 | break; |
| 6236 | } |
Jeff Layton | f0d3868 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6237 | } |
Jeff Layton | 0cd126b | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6238 | temp_ptr += name_len + 1 + value_len; |
Jeff Layton | f0d3868 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6239 | temp_fea = (struct fea *)temp_ptr; |
| 6240 | } |
| 6241 | |
Jeff Layton | 31c0519 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6242 | /* didn't find the named attribute */ |
| 6243 | if (ea_name) |
| 6244 | rc = -ENODATA; |
| 6245 | |
Jeff Layton | f0d3868 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 6246 | QAllEAsOut: |
Steve French | 0d817bc | 2008-05-22 02:02:03 +0000 | [diff] [blame] | 6247 | cifs_buf_release(pSMB); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6248 | if (rc == -EAGAIN) |
| 6249 | goto QAllEAsRetry; |
| 6250 | |
| 6251 | return (ssize_t)rc; |
| 6252 | } |
| 6253 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6254 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 6255 | CIFSSMBSetEA(const unsigned int xid, struct cifs_tcon *tcon, |
| 6256 | const char *fileName, const char *ea_name, const void *ea_value, |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 6257 | const __u16 ea_value_len, const struct nls_table *nls_codepage, |
| 6258 | int remap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6259 | { |
| 6260 | struct smb_com_transaction2_spi_req *pSMB = NULL; |
| 6261 | struct smb_com_transaction2_spi_rsp *pSMBr = NULL; |
| 6262 | struct fealist *parm_data; |
| 6263 | int name_len; |
| 6264 | int rc = 0; |
| 6265 | int bytes_returned = 0; |
| 6266 | __u16 params, param_offset, byte_count, offset, count; |
| 6267 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 6268 | cifs_dbg(FYI, "In SetEA\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6269 | SetEARetry: |
| 6270 | rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB, |
| 6271 | (void **) &pSMBr); |
| 6272 | if (rc) |
| 6273 | return rc; |
| 6274 | |
| 6275 | if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) { |
| 6276 | name_len = |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 6277 | cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName, |
| 6278 | PATH_MAX, nls_codepage, remap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6279 | name_len++; /* trailing null */ |
| 6280 | name_len *= 2; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 6281 | } else { /* BB improve the check for buffer overruns BB */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6282 | name_len = strnlen(fileName, PATH_MAX); |
| 6283 | name_len++; /* trailing null */ |
| 6284 | strncpy(pSMB->FileName, fileName, name_len); |
| 6285 | } |
| 6286 | |
| 6287 | params = 6 + name_len; |
| 6288 | |
| 6289 | /* done calculating parms using name_len of file name, |
| 6290 | now use name_len to calculate length of ea name |
| 6291 | we are going to create in the inode xattrs */ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 6292 | if (ea_name == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6293 | name_len = 0; |
| 6294 | else |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 6295 | name_len = strnlen(ea_name, 255); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6296 | |
Steve French | dae5dbd | 2007-12-30 23:49:57 +0000 | [diff] [blame] | 6297 | count = sizeof(*parm_data) + ea_value_len + name_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6298 | pSMB->MaxParameterCount = cpu_to_le16(2); |
Steve French | 582d21e | 2008-05-13 04:54:12 +0000 | [diff] [blame] | 6299 | /* BB find max SMB PDU from sess */ |
| 6300 | pSMB->MaxDataCount = cpu_to_le16(1000); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6301 | pSMB->MaxSetupCount = 0; |
| 6302 | pSMB->Reserved = 0; |
| 6303 | pSMB->Flags = 0; |
| 6304 | pSMB->Timeout = 0; |
| 6305 | pSMB->Reserved2 = 0; |
| 6306 | param_offset = offsetof(struct smb_com_transaction2_spi_req, |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 6307 | InformationLevel) - 4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6308 | offset = param_offset + params; |
| 6309 | pSMB->InformationLevel = |
| 6310 | cpu_to_le16(SMB_SET_FILE_EA); |
| 6311 | |
| 6312 | parm_data = |
| 6313 | (struct fealist *) (((char *) &pSMB->hdr.Protocol) + |
| 6314 | offset); |
| 6315 | pSMB->ParameterOffset = cpu_to_le16(param_offset); |
| 6316 | pSMB->DataOffset = cpu_to_le16(offset); |
| 6317 | pSMB->SetupCount = 1; |
| 6318 | pSMB->Reserved3 = 0; |
| 6319 | pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION); |
| 6320 | byte_count = 3 /* pad */ + params + count; |
| 6321 | pSMB->DataCount = cpu_to_le16(count); |
| 6322 | parm_data->list_len = cpu_to_le32(count); |
| 6323 | parm_data->list[0].EA_flags = 0; |
| 6324 | /* we checked above that name len is less than 255 */ |
Alexey Dobriyan | 53b3531 | 2006-03-24 03:16:13 -0800 | [diff] [blame] | 6325 | parm_data->list[0].name_len = (__u8)name_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6326 | /* EA names are always ASCII */ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 6327 | if (ea_name) |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 6328 | strncpy(parm_data->list[0].name, ea_name, name_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6329 | parm_data->list[0].name[name_len] = 0; |
| 6330 | parm_data->list[0].value_len = cpu_to_le16(ea_value_len); |
| 6331 | /* caller ensures that ea_value_len is less than 64K but |
| 6332 | we need to ensure that it fits within the smb */ |
| 6333 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 6334 | /*BB add length check to see if it would fit in |
| 6335 | negotiated SMB buffer size BB */ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 6336 | /* if (ea_value_len > buffer_size - 512 (enough for header)) */ |
| 6337 | if (ea_value_len) |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 6338 | memcpy(parm_data->list[0].name+name_len+1, |
| 6339 | ea_value, ea_value_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6340 | |
| 6341 | pSMB->TotalDataCount = pSMB->DataCount; |
| 6342 | pSMB->ParameterCount = cpu_to_le16(params); |
| 6343 | pSMB->TotalParameterCount = pSMB->ParameterCount; |
| 6344 | pSMB->Reserved4 = 0; |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 6345 | inc_rfc1001_len(pSMB, byte_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6346 | pSMB->ByteCount = cpu_to_le16(byte_count); |
| 6347 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 6348 | (struct smb_hdr *) pSMBr, &bytes_returned, 0); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 6349 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 6350 | cifs_dbg(FYI, "SetPathInfo (EA) returned %d\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6351 | |
| 6352 | cifs_buf_release(pSMB); |
| 6353 | |
| 6354 | if (rc == -EAGAIN) |
| 6355 | goto SetEARetry; |
| 6356 | |
| 6357 | return rc; |
| 6358 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6359 | #endif |
Steve French | 0eff0e2 | 2011-02-24 05:39:23 +0000 | [diff] [blame] | 6360 | |
| 6361 | #ifdef CONFIG_CIFS_DNOTIFY_EXPERIMENTAL /* BB unused temporarily */ |
| 6362 | /* |
| 6363 | * Years ago the kernel added a "dnotify" function for Samba server, |
| 6364 | * to allow network clients (such as Windows) to display updated |
| 6365 | * lists of files in directory listings automatically when |
| 6366 | * files are added by one user when another user has the |
| 6367 | * same directory open on their desktop. The Linux cifs kernel |
| 6368 | * client hooked into the kernel side of this interface for |
| 6369 | * the same reason, but ironically when the VFS moved from |
| 6370 | * "dnotify" to "inotify" it became harder to plug in Linux |
| 6371 | * network file system clients (the most obvious use case |
| 6372 | * for notify interfaces is when multiple users can update |
| 6373 | * the contents of the same directory - exactly what network |
| 6374 | * file systems can do) although the server (Samba) could |
| 6375 | * still use it. For the short term we leave the worker |
| 6376 | * function ifdeffed out (below) until inotify is fixed |
| 6377 | * in the VFS to make it easier to plug in network file |
| 6378 | * system clients. If inotify turns out to be permanently |
| 6379 | * incompatible for network fs clients, we could instead simply |
| 6380 | * expose this config flag by adding a future cifs (and smb2) notify ioctl. |
| 6381 | */ |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 6382 | int CIFSSMBNotify(const unsigned int xid, struct cifs_tcon *tcon, |
Steve French | 0eff0e2 | 2011-02-24 05:39:23 +0000 | [diff] [blame] | 6383 | const int notify_subdirs, const __u16 netfid, |
| 6384 | __u32 filter, struct file *pfile, int multishot, |
| 6385 | const struct nls_table *nls_codepage) |
| 6386 | { |
| 6387 | int rc = 0; |
| 6388 | struct smb_com_transaction_change_notify_req *pSMB = NULL; |
| 6389 | struct smb_com_ntransaction_change_notify_rsp *pSMBr = NULL; |
| 6390 | struct dir_notify_req *dnotify_req; |
| 6391 | int bytes_returned; |
| 6392 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 6393 | cifs_dbg(FYI, "In CIFSSMBNotify for file handle %d\n", (int)netfid); |
Steve French | 0eff0e2 | 2011-02-24 05:39:23 +0000 | [diff] [blame] | 6394 | rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB, |
| 6395 | (void **) &pSMBr); |
| 6396 | if (rc) |
| 6397 | return rc; |
| 6398 | |
| 6399 | pSMB->TotalParameterCount = 0 ; |
| 6400 | pSMB->TotalDataCount = 0; |
| 6401 | pSMB->MaxParameterCount = cpu_to_le32(2); |
Jeff Layton | c974bef | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 6402 | pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00); |
Steve French | 0eff0e2 | 2011-02-24 05:39:23 +0000 | [diff] [blame] | 6403 | pSMB->MaxSetupCount = 4; |
| 6404 | pSMB->Reserved = 0; |
| 6405 | pSMB->ParameterOffset = 0; |
| 6406 | pSMB->DataCount = 0; |
| 6407 | pSMB->DataOffset = 0; |
| 6408 | pSMB->SetupCount = 4; /* single byte does not need le conversion */ |
| 6409 | pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_NOTIFY_CHANGE); |
| 6410 | pSMB->ParameterCount = pSMB->TotalParameterCount; |
| 6411 | if (notify_subdirs) |
| 6412 | pSMB->WatchTree = 1; /* one byte - no le conversion needed */ |
| 6413 | pSMB->Reserved2 = 0; |
| 6414 | pSMB->CompletionFilter = cpu_to_le32(filter); |
| 6415 | pSMB->Fid = netfid; /* file handle always le */ |
| 6416 | pSMB->ByteCount = 0; |
| 6417 | |
| 6418 | rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, |
| 6419 | (struct smb_hdr *)pSMBr, &bytes_returned, |
| 6420 | CIFS_ASYNC_OP); |
| 6421 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 6422 | cifs_dbg(FYI, "Error in Notify = %d\n", rc); |
Steve French | 0eff0e2 | 2011-02-24 05:39:23 +0000 | [diff] [blame] | 6423 | } else { |
| 6424 | /* Add file to outstanding requests */ |
| 6425 | /* BB change to kmem cache alloc */ |
| 6426 | dnotify_req = kmalloc( |
| 6427 | sizeof(struct dir_notify_req), |
| 6428 | GFP_KERNEL); |
| 6429 | if (dnotify_req) { |
| 6430 | dnotify_req->Pid = pSMB->hdr.Pid; |
| 6431 | dnotify_req->PidHigh = pSMB->hdr.PidHigh; |
| 6432 | dnotify_req->Mid = pSMB->hdr.Mid; |
| 6433 | dnotify_req->Tid = pSMB->hdr.Tid; |
| 6434 | dnotify_req->Uid = pSMB->hdr.Uid; |
| 6435 | dnotify_req->netfid = netfid; |
| 6436 | dnotify_req->pfile = pfile; |
| 6437 | dnotify_req->filter = filter; |
| 6438 | dnotify_req->multishot = multishot; |
| 6439 | spin_lock(&GlobalMid_Lock); |
| 6440 | list_add_tail(&dnotify_req->lhead, |
| 6441 | &GlobalDnotifyReqList); |
| 6442 | spin_unlock(&GlobalMid_Lock); |
| 6443 | } else |
| 6444 | rc = -ENOMEM; |
| 6445 | } |
| 6446 | cifs_buf_release(pSMB); |
| 6447 | return rc; |
| 6448 | } |
| 6449 | #endif /* was needed for dnotify, and will be needed for inotify when VFS fix */ |