CIFS: Count SMB3 credits for malformed pending responses
[linux-2.6-block.git] / fs / cifs / cifssmb.c
CommitLineData
1da177e4
LT
1/*
2 * fs/cifs/cifssmb.c
3 *
f19159dc 4 * Copyright (C) International Business Machines Corp., 2002,2010
1da177e4
LT
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 */
2dd29d31
SF
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 */
1da177e4
LT
29
30#include <linux/fs.h>
31#include <linux/kernel.h>
32#include <linux/vfs.h>
5a0e3ad6 33#include <linux/slab.h>
1da177e4 34#include <linux/posix_acl_xattr.h>
c28c89fc 35#include <linux/pagemap.h>
e28bc5b1
JL
36#include <linux/swap.h>
37#include <linux/task_io_accounting_ops.h>
7c0f6ba6 38#include <linux/uaccess.h>
1da177e4
LT
39#include "cifspdu.h"
40#include "cifsglob.h"
d0d66c44 41#include "cifsacl.h"
1da177e4
LT
42#include "cifsproto.h"
43#include "cifs_unicode.h"
44#include "cifs_debug.h"
e28bc5b1 45#include "fscache.h"
db223a59 46#include "smbdirect.h"
08744015
PA
47#ifdef CONFIG_CIFS_DFS_UPCALL
48#include "dfs_cache.h"
49#endif
1da177e4
LT
50
51#ifdef CONFIG_CIFS_POSIX
52static struct {
53 int index;
54 char *name;
55} protocols[] = {
3979877e
SF
56#ifdef CONFIG_CIFS_WEAK_PW_HASH
57 {LANMAN_PROT, "\2LM1.2X002"},
9ac00b7d 58 {LANMAN2_PROT, "\2LANMAN2.1"},
3979877e 59#endif /* weak password hashing for legacy clients */
50c2f753 60 {CIFS_PROT, "\2NT LM 0.12"},
3979877e 61 {POSIX_PROT, "\2POSIX 2"},
1da177e4
LT
62 {BAD_PROT, "\2"}
63};
64#else
65static struct {
66 int index;
67 char *name;
68} protocols[] = {
3979877e
SF
69#ifdef CONFIG_CIFS_WEAK_PW_HASH
70 {LANMAN_PROT, "\2LM1.2X002"},
18f75ca0 71 {LANMAN2_PROT, "\2LANMAN2.1"},
3979877e 72#endif /* weak password hashing for legacy clients */
790fe579 73 {CIFS_PROT, "\2NT LM 0.12"},
1da177e4
LT
74 {BAD_PROT, "\2"}
75};
76#endif
77
3979877e
SF
78/* define the number of elements in the cifs dialect array */
79#ifdef CONFIG_CIFS_POSIX
80#ifdef CONFIG_CIFS_WEAK_PW_HASH
9ac00b7d 81#define CIFS_NUM_PROT 4
3979877e
SF
82#else
83#define CIFS_NUM_PROT 2
84#endif /* CIFS_WEAK_PW_HASH */
85#else /* not posix */
86#ifdef CONFIG_CIFS_WEAK_PW_HASH
9ac00b7d 87#define CIFS_NUM_PROT 3
3979877e
SF
88#else
89#define CIFS_NUM_PROT 1
90#endif /* CONFIG_CIFS_WEAK_PW_HASH */
91#endif /* CIFS_POSIX */
92
aa24d1e9
PS
93/*
94 * Mark as invalid, all open files on tree connections since they
95 * were closed when session to server was lost.
96 */
97void
98cifs_mark_open_files_invalid(struct cifs_tcon *tcon)
1da177e4
LT
99{
100 struct cifsFileInfo *open_file = NULL;
790fe579
SF
101 struct list_head *tmp;
102 struct list_head *tmp1;
1da177e4 103
aa24d1e9 104 /* list all files open on tree connection and mark them invalid */
3afca265 105 spin_lock(&tcon->open_file_lock);
aa24d1e9 106 list_for_each_safe(tmp, tmp1, &tcon->openFileList) {
790fe579 107 open_file = list_entry(tmp, struct cifsFileInfo, tlist);
ad8b15f0 108 open_file->invalidHandle = true;
3bc303c2 109 open_file->oplock_break_cancelled = true;
1da177e4 110 }
3afca265 111 spin_unlock(&tcon->open_file_lock);
3d4ef9a1 112
a93864d9
RS
113 mutex_lock(&tcon->crfid.fid_mutex);
114 tcon->crfid.is_valid = false;
115 memset(tcon->crfid.fid, 0, sizeof(struct cifs_fid));
116 mutex_unlock(&tcon->crfid.fid_mutex);
3d4ef9a1 117
aa24d1e9
PS
118 /*
119 * BB Add call to invalidate_inodes(sb) for all superblocks mounted
120 * to this tcon.
121 */
1da177e4
LT
122}
123
08744015
PA
124#ifdef CONFIG_CIFS_DFS_UPCALL
125static int __cifs_reconnect_tcon(const struct nls_table *nlsc,
126 struct cifs_tcon *tcon)
127{
128 int rc;
129 struct dfs_cache_tgt_list tl;
130 struct dfs_cache_tgt_iterator *it = NULL;
15bc77f9 131 char *tree;
08744015
PA
132 const char *tcp_host;
133 size_t tcp_host_len;
134 const char *dfs_host;
135 size_t dfs_host_len;
136
15bc77f9
AA
137 tree = kzalloc(MAX_TREE_SIZE, GFP_KERNEL);
138 if (!tree)
139 return -ENOMEM;
140
08744015 141 if (tcon->ipc) {
74ea5f98
RS
142 scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$",
143 tcon->ses->server->hostname);
15bc77f9
AA
144 rc = CIFSTCon(0, tcon->ses, tree, tcon, nlsc);
145 goto out;
08744015
PA
146 }
147
15bc77f9
AA
148 if (!tcon->dfs_path) {
149 rc = CIFSTCon(0, tcon->ses, tcon->treeName, tcon, nlsc);
150 goto out;
151 }
08744015
PA
152
153 rc = dfs_cache_noreq_find(tcon->dfs_path + 1, NULL, &tl);
154 if (rc)
15bc77f9 155 goto out;
08744015
PA
156
157 extract_unc_hostname(tcon->ses->server->hostname, &tcp_host,
158 &tcp_host_len);
159
160 for (it = dfs_cache_get_tgt_iterator(&tl); it;
161 it = dfs_cache_get_next_tgt(&tl, it)) {
162 const char *tgt = dfs_cache_get_tgt_name(it);
163
164 extract_unc_hostname(tgt, &dfs_host, &dfs_host_len);
165
166 if (dfs_host_len != tcp_host_len
167 || strncasecmp(dfs_host, tcp_host, dfs_host_len) != 0) {
168 cifs_dbg(FYI, "%s: skipping %.*s, doesn't match %.*s",
169 __func__,
170 (int)dfs_host_len, dfs_host,
171 (int)tcp_host_len, tcp_host);
172 continue;
173 }
174
74ea5f98 175 scnprintf(tree, MAX_TREE_SIZE, "\\%s", tgt);
08744015
PA
176
177 rc = CIFSTCon(0, tcon->ses, tree, tcon, nlsc);
178 if (!rc)
179 break;
180 if (rc == -EREMOTE)
181 break;
182 }
183
184 if (!rc) {
185 if (it)
186 rc = dfs_cache_noreq_update_tgthint(tcon->dfs_path + 1,
187 it);
188 else
189 rc = -ENOENT;
190 }
191 dfs_cache_free_tgts(&tl);
15bc77f9
AA
192out:
193 kfree(tree);
08744015
PA
194 return rc;
195}
196#else
197static inline int __cifs_reconnect_tcon(const struct nls_table *nlsc,
198 struct cifs_tcon *tcon)
199{
200 return CIFSTCon(0, tcon->ses, tcon->treeName, tcon, nlsc);
201}
202#endif
203
9162ab20
JL
204/* reconnect the socket, tcon, and smb session if needed */
205static int
96daf2b0 206cifs_reconnect_tcon(struct cifs_tcon *tcon, int smb_command)
9162ab20 207{
c4a5534a 208 int rc;
96daf2b0 209 struct cifs_ses *ses;
9162ab20
JL
210 struct TCP_Server_Info *server;
211 struct nls_table *nls_codepage;
08744015 212 int retries;
9162ab20
JL
213
214 /*
215 * SMBs NegProt, SessSetup, uLogoff do not have tcon yet so check for
216 * tcp and smb session status done differently for those three - in the
217 * calling routine
218 */
219 if (!tcon)
220 return 0;
221
222 ses = tcon->ses;
223 server = ses->server;
224
225 /*
226 * only tree disconnect, open, and write, (and ulogoff which does not
227 * have tcon) are allowed as we start force umount
228 */
229 if (tcon->tidStatus == CifsExiting) {
230 if (smb_command != SMB_COM_WRITE_ANDX &&
231 smb_command != SMB_COM_OPEN_ANDX &&
232 smb_command != SMB_COM_TREE_DISCONNECT) {
f96637be
JP
233 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
234 smb_command);
9162ab20
JL
235 return -ENODEV;
236 }
237 }
238
08744015
PA
239 retries = server->nr_targets;
240
9162ab20 241 /*
08744015
PA
242 * Give demultiplex thread up to 10 seconds to each target available for
243 * reconnect -- should be greater than cifs socket timeout which is 7
244 * seconds.
9162ab20
JL
245 */
246 while (server->tcpStatus == CifsNeedReconnect) {
7ffbe655
PA
247 rc = wait_event_interruptible_timeout(server->response_q,
248 (server->tcpStatus != CifsNeedReconnect),
249 10 * HZ);
250 if (rc < 0) {
251 cifs_dbg(FYI, "%s: aborting reconnect due to a received"
252 " signal by the process\n", __func__);
253 return -ERESTARTSYS;
254 }
9162ab20 255
fd88ce93 256 /* are we still trying to reconnect? */
9162ab20
JL
257 if (server->tcpStatus != CifsNeedReconnect)
258 break;
259
08744015
PA
260 if (--retries)
261 continue;
262
9162ab20
JL
263 /*
264 * on "soft" mounts we wait once. Hard mounts keep
265 * retrying until process is killed or server comes
266 * back on-line
267 */
d402539b 268 if (!tcon->retry) {
f96637be 269 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
9162ab20
JL
270 return -EHOSTDOWN;
271 }
08744015 272 retries = server->nr_targets;
9162ab20
JL
273 }
274
275 if (!ses->need_reconnect && !tcon->need_reconnect)
276 return 0;
277
278 nls_codepage = load_nls_default();
279
280 /*
281 * need to prevent multiple threads trying to simultaneously
282 * reconnect the same SMB session
283 */
d7b619cf 284 mutex_lock(&ses->session_mutex);
76e75270
SC
285
286 /*
287 * Recheck after acquire mutex. If another thread is negotiating
288 * and the server never sends an answer the socket will be closed
289 * and tcpStatus set to reconnect.
290 */
291 if (server->tcpStatus == CifsNeedReconnect) {
292 rc = -EHOSTDOWN;
293 mutex_unlock(&ses->session_mutex);
294 goto out;
295 }
296
198b5682
JL
297 rc = cifs_negotiate_protocol(0, ses);
298 if (rc == 0 && ses->need_reconnect)
9162ab20
JL
299 rc = cifs_setup_session(0, ses, nls_codepage);
300
301 /* do we need to reconnect tcon? */
302 if (rc || !tcon->need_reconnect) {
d7b619cf 303 mutex_unlock(&ses->session_mutex);
9162ab20
JL
304 goto out;
305 }
306
aa24d1e9 307 cifs_mark_open_files_invalid(tcon);
08744015 308 rc = __cifs_reconnect_tcon(nls_codepage, tcon);
d7b619cf 309 mutex_unlock(&ses->session_mutex);
f96637be 310 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
9162ab20 311
c318e6c2
SF
312 if (rc) {
313 printk_once(KERN_WARNING "reconnect tcon failed rc = %d\n", rc);
9162ab20 314 goto out;
c318e6c2 315 }
9162ab20 316
9162ab20
JL
317 atomic_inc(&tconInfoReconnectCount);
318
319 /* tell server Unix caps we support */
320 if (ses->capabilities & CAP_UNIX)
321 reset_cifs_unix_caps(0, tcon, NULL, NULL);
322
323 /*
324 * Removed call to reopen open files here. It is safer (and faster) to
325 * reopen files one at a time as needed in read and write.
326 *
327 * FIXME: what about file locks? don't we need to reclaim them ASAP?
328 */
329
330out:
331 /*
332 * Check if handle based operation so we know whether we can continue
333 * or not without returning to caller to reset file handle
334 */
335 switch (smb_command) {
336 case SMB_COM_READ_ANDX:
337 case SMB_COM_WRITE_ANDX:
338 case SMB_COM_CLOSE:
339 case SMB_COM_FIND_CLOSE2:
340 case SMB_COM_LOCKING_ANDX:
341 rc = -EAGAIN;
342 }
343
344 unload_nls(nls_codepage);
345 return rc;
346}
347
ad7a2926
SF
348/* Allocate and return pointer to an SMB request buffer, and set basic
349 SMB information in the SMB header. If the return code is zero, this
350 function must have filled in request_buf pointer */
1da177e4 351static int
96daf2b0 352small_smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
ad7a2926 353 void **request_buf)
1da177e4 354{
f569599a 355 int rc;
1da177e4 356
9162ab20 357 rc = cifs_reconnect_tcon(tcon, smb_command);
790fe579 358 if (rc)
1da177e4
LT
359 return rc;
360
361 *request_buf = cifs_small_buf_get();
362 if (*request_buf == NULL) {
363 /* BB should we add a retry in here if not a writepage? */
364 return -ENOMEM;
365 }
366
63135e08 367 header_assemble((struct smb_hdr *) *request_buf, smb_command,
c18c842b 368 tcon, wct);
1da177e4 369
790fe579
SF
370 if (tcon != NULL)
371 cifs_stats_inc(&tcon->num_smbs_sent);
a4544347 372
f569599a 373 return 0;
5815449d
SF
374}
375
12b3b8ff 376int
50c2f753 377small_smb_init_no_tc(const int smb_command, const int wct,
96daf2b0 378 struct cifs_ses *ses, void **request_buf)
12b3b8ff
SF
379{
380 int rc;
50c2f753 381 struct smb_hdr *buffer;
12b3b8ff 382
5815449d 383 rc = small_smb_init(smb_command, wct, NULL, request_buf);
790fe579 384 if (rc)
12b3b8ff
SF
385 return rc;
386
04fdabe1 387 buffer = (struct smb_hdr *)*request_buf;
88257360 388 buffer->Mid = get_next_mid(ses->server);
12b3b8ff
SF
389 if (ses->capabilities & CAP_UNICODE)
390 buffer->Flags2 |= SMBFLG2_UNICODE;
04fdabe1 391 if (ses->capabilities & CAP_STATUS32)
12b3b8ff
SF
392 buffer->Flags2 |= SMBFLG2_ERR_STATUS;
393
394 /* uid, tid can stay at zero as set in header assemble */
395
50c2f753 396 /* BB add support for turning on the signing when
12b3b8ff
SF
397 this function is used after 1st of session setup requests */
398
399 return rc;
400}
1da177e4
LT
401
402/* If the return code is zero, this function must fill in request_buf pointer */
403static int
96daf2b0 404__smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
f569599a 405 void **request_buf, void **response_buf)
1da177e4 406{
1da177e4
LT
407 *request_buf = cifs_buf_get();
408 if (*request_buf == NULL) {
409 /* BB should we add a retry in here if not a writepage? */
410 return -ENOMEM;
411 }
412 /* Although the original thought was we needed the response buf for */
413 /* potential retries of smb operations it turns out we can determine */
414 /* from the mid flags when the request buffer can be resent without */
415 /* having to use a second distinct buffer for the response */
790fe579 416 if (response_buf)
50c2f753 417 *response_buf = *request_buf;
1da177e4
LT
418
419 header_assemble((struct smb_hdr *) *request_buf, smb_command, tcon,
ad7a2926 420 wct);
1da177e4 421
790fe579
SF
422 if (tcon != NULL)
423 cifs_stats_inc(&tcon->num_smbs_sent);
a4544347 424
f569599a
JL
425 return 0;
426}
427
428/* If the return code is zero, this function must fill in request_buf pointer */
429static int
96daf2b0 430smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
f569599a
JL
431 void **request_buf, void **response_buf)
432{
433 int rc;
434
435 rc = cifs_reconnect_tcon(tcon, smb_command);
436 if (rc)
437 return rc;
438
439 return __smb_init(smb_command, wct, tcon, request_buf, response_buf);
440}
441
442static int
96daf2b0 443smb_init_no_reconnect(int smb_command, int wct, struct cifs_tcon *tcon,
f569599a
JL
444 void **request_buf, void **response_buf)
445{
446 if (tcon->ses->need_reconnect || tcon->need_reconnect)
447 return -EHOSTDOWN;
448
449 return __smb_init(smb_command, wct, tcon, request_buf, response_buf);
1da177e4
LT
450}
451
50c2f753 452static int validate_t2(struct smb_t2_rsp *pSMB)
1da177e4 453{
12df83c9
JL
454 unsigned int total_size;
455
456 /* check for plausible wct */
457 if (pSMB->hdr.WordCount < 10)
458 goto vt2_err;
1da177e4 459
1da177e4 460 /* check for parm and data offset going beyond end of smb */
12df83c9
JL
461 if (get_unaligned_le16(&pSMB->t2_rsp.ParameterOffset) > 1024 ||
462 get_unaligned_le16(&pSMB->t2_rsp.DataOffset) > 1024)
463 goto vt2_err;
464
12df83c9
JL
465 total_size = get_unaligned_le16(&pSMB->t2_rsp.ParameterCount);
466 if (total_size >= 512)
467 goto vt2_err;
468
fd5707e1
JL
469 /* check that bcc is at least as big as parms + data, and that it is
470 * less than negotiated smb buffer
471 */
12df83c9
JL
472 total_size += get_unaligned_le16(&pSMB->t2_rsp.DataCount);
473 if (total_size > get_bcc(&pSMB->hdr) ||
474 total_size >= CIFSMaxBufSize + MAX_CIFS_HDR_SIZE)
475 goto vt2_err;
476
477 return 0;
478vt2_err:
50c2f753 479 cifs_dump_mem("Invalid transact2 SMB: ", (char *)pSMB,
1da177e4 480 sizeof(struct smb_t2_rsp) + 16);
12df83c9 481 return -EINVAL;
1da177e4 482}
690c522f 483
31d9e2bd 484static int
3f618223 485decode_ext_sec_blob(struct cifs_ses *ses, NEGOTIATE_RSP *pSMBr)
31d9e2bd
JL
486{
487 int rc = 0;
488 u16 count;
489 char *guid = pSMBr->u.extended_response.GUID;
3f618223 490 struct TCP_Server_Info *server = ses->server;
31d9e2bd
JL
491
492 count = get_bcc(&pSMBr->hdr);
493 if (count < SMB1_CLIENT_GUID_SIZE)
494 return -EIO;
495
496 spin_lock(&cifs_tcp_ses_lock);
497 if (server->srv_count > 1) {
498 spin_unlock(&cifs_tcp_ses_lock);
499 if (memcmp(server->server_GUID, guid, SMB1_CLIENT_GUID_SIZE) != 0) {
500 cifs_dbg(FYI, "server UID changed\n");
501 memcpy(server->server_GUID, guid, SMB1_CLIENT_GUID_SIZE);
502 }
503 } else {
504 spin_unlock(&cifs_tcp_ses_lock);
505 memcpy(server->server_GUID, guid, SMB1_CLIENT_GUID_SIZE);
506 }
507
508 if (count == SMB1_CLIENT_GUID_SIZE) {
3f618223 509 server->sec_ntlmssp = true;
31d9e2bd
JL
510 } else {
511 count -= SMB1_CLIENT_GUID_SIZE;
512 rc = decode_negTokenInit(
513 pSMBr->u.extended_response.SecurityBlob, count, server);
514 if (rc != 1)
515 return -EINVAL;
31d9e2bd
JL
516 }
517
518 return 0;
519}
520
9ddec561 521int
38d77c50 522cifs_enable_signing(struct TCP_Server_Info *server, bool mnt_sign_required)
9ddec561 523{
50285882
JL
524 bool srv_sign_required = server->sec_mode & server->vals->signing_required;
525 bool srv_sign_enabled = server->sec_mode & server->vals->signing_enabled;
38d77c50
JL
526 bool mnt_sign_enabled = global_secflags & CIFSSEC_MAY_SIGN;
527
528 /*
529 * Is signing required by mnt options? If not then check
530 * global_secflags to see if it is there.
531 */
532 if (!mnt_sign_required)
533 mnt_sign_required = ((global_secflags & CIFSSEC_MUST_SIGN) ==
534 CIFSSEC_MUST_SIGN);
535
536 /*
537 * If signing is required then it's automatically enabled too,
538 * otherwise, check to see if the secflags allow it.
539 */
540 mnt_sign_enabled = mnt_sign_required ? mnt_sign_required :
541 (global_secflags & CIFSSEC_MAY_SIGN);
542
543 /* If server requires signing, does client allow it? */
544 if (srv_sign_required) {
545 if (!mnt_sign_enabled) {
546 cifs_dbg(VFS, "Server requires signing, but it's disabled in SecurityFlags!");
547 return -ENOTSUPP;
9ddec561 548 }
38d77c50
JL
549 server->sign = true;
550 }
551
552 /* If client requires signing, does server allow it? */
553 if (mnt_sign_required) {
554 if (!srv_sign_enabled) {
555 cifs_dbg(VFS, "Server does not support signing!");
556 return -ENOTSUPP;
557 }
558 server->sign = true;
9ddec561
JL
559 }
560
bb4c0419
LL
561 if (cifs_rdma_enabled(server) && server->sign)
562 cifs_dbg(VFS, "Signing is enabled, and RDMA read/write will be disabled");
563
9ddec561
JL
564 return 0;
565}
566
2190eca1
JL
567#ifdef CONFIG_CIFS_WEAK_PW_HASH
568static int
3f618223 569decode_lanman_negprot_rsp(struct TCP_Server_Info *server, NEGOTIATE_RSP *pSMBr)
2190eca1
JL
570{
571 __s16 tmp;
572 struct lanman_neg_rsp *rsp = (struct lanman_neg_rsp *)pSMBr;
573
574 if (server->dialect != LANMAN_PROT && server->dialect != LANMAN2_PROT)
575 return -EOPNOTSUPP;
576
2190eca1
JL
577 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
578 server->maxReq = min_t(unsigned int,
579 le16_to_cpu(rsp->MaxMpxCount),
580 cifs_max_pending);
581 set_credits(server, server->maxReq);
582 server->maxBuf = le16_to_cpu(rsp->MaxBufSize);
2190eca1
JL
583 /* even though we do not use raw we might as well set this
584 accurately, in case we ever find a need for it */
585 if ((le16_to_cpu(rsp->RawMode) & RAW_ENABLE) == RAW_ENABLE) {
586 server->max_rw = 0xFF00;
587 server->capabilities = CAP_MPX_MODE | CAP_RAW_MODE;
588 } else {
589 server->max_rw = 0;/* do not need to use raw anyway */
590 server->capabilities = CAP_MPX_MODE;
591 }
592 tmp = (__s16)le16_to_cpu(rsp->ServerTimeZone);
593 if (tmp == -1) {
594 /* OS/2 often does not set timezone therefore
595 * we must use server time to calc time zone.
596 * Could deviate slightly from the right zone.
597 * Smallest defined timezone difference is 15 minutes
598 * (i.e. Nepal). Rounding up/down is done to match
599 * this requirement.
600 */
601 int val, seconds, remain, result;
95390201
AB
602 struct timespec64 ts;
603 time64_t utc = ktime_get_real_seconds();
2190eca1
JL
604 ts = cnvrtDosUnixTm(rsp->SrvTime.Date,
605 rsp->SrvTime.Time, 0);
95390201
AB
606 cifs_dbg(FYI, "SrvTime %lld sec since 1970 (utc: %lld) diff: %lld\n",
607 ts.tv_sec, utc,
608 utc - ts.tv_sec);
e37fea58 609 val = (int)(utc - ts.tv_sec);
2190eca1
JL
610 seconds = abs(val);
611 result = (seconds / MIN_TZ_ADJ) * MIN_TZ_ADJ;
612 remain = seconds % MIN_TZ_ADJ;
613 if (remain >= (MIN_TZ_ADJ / 2))
614 result += MIN_TZ_ADJ;
615 if (val < 0)
616 result = -result;
617 server->timeAdj = result;
618 } else {
619 server->timeAdj = (int)tmp;
620 server->timeAdj *= 60; /* also in seconds */
621 }
622 cifs_dbg(FYI, "server->timeAdj: %d seconds\n", server->timeAdj);
623
624
625 /* BB get server time for time conversions and add
626 code to use it and timezone since this is not UTC */
627
628 if (rsp->EncryptionKeyLength ==
629 cpu_to_le16(CIFS_CRYPTO_KEY_SIZE)) {
630 memcpy(server->cryptkey, rsp->EncryptionKey,
631 CIFS_CRYPTO_KEY_SIZE);
632 } else if (server->sec_mode & SECMODE_PW_ENCRYPT) {
633 return -EIO; /* need cryptkey unless plain text */
634 }
635
636 cifs_dbg(FYI, "LANMAN negotiated\n");
637 return 0;
638}
639#else
640static inline int
3f618223 641decode_lanman_negprot_rsp(struct TCP_Server_Info *server, NEGOTIATE_RSP *pSMBr)
2190eca1
JL
642{
643 cifs_dbg(VFS, "mount failed, cifs module not built with CIFS_WEAK_PW_HASH support\n");
644 return -EOPNOTSUPP;
645}
646#endif
647
9193400b 648static bool
3f618223 649should_set_ext_sec_flag(enum securityEnum sectype)
9193400b 650{
3f618223
JL
651 switch (sectype) {
652 case RawNTLMSSP:
653 case Kerberos:
9193400b 654 return true;
3f618223
JL
655 case Unspecified:
656 if (global_secflags &
657 (CIFSSEC_MAY_KRB5 | CIFSSEC_MAY_NTLMSSP))
658 return true;
659 /* Fallthrough */
660 default:
661 return false;
662 }
9193400b
JL
663}
664
1da177e4 665int
286170aa 666CIFSSMBNegotiate(const unsigned int xid, struct cifs_ses *ses)
1da177e4
LT
667{
668 NEGOTIATE_REQ *pSMB;
669 NEGOTIATE_RSP *pSMBr;
670 int rc = 0;
671 int bytes_returned;
3979877e 672 int i;
3534b850 673 struct TCP_Server_Info *server = ses->server;
1da177e4
LT
674 u16 count;
675
3534b850
JL
676 if (!server) {
677 WARN(1, "%s: server is NULL!\n", __func__);
678 return -EIO;
1da177e4 679 }
3534b850 680
1da177e4
LT
681 rc = smb_init(SMB_COM_NEGOTIATE, 0, NULL /* no tcon yet */ ,
682 (void **) &pSMB, (void **) &pSMBr);
683 if (rc)
684 return rc;
750d1151 685
88257360 686 pSMB->hdr.Mid = get_next_mid(server);
100c1ddc 687 pSMB->hdr.Flags2 |= (SMBFLG2_UNICODE | SMBFLG2_ERR_STATUS);
a013689d 688
3f618223 689 if (should_set_ext_sec_flag(ses->sectype)) {
9193400b 690 cifs_dbg(FYI, "Requesting extended security.");
ac683924
SF
691 pSMB->hdr.Flags2 |= SMBFLG2_EXT_SEC;
692 }
50c2f753 693
3979877e 694 count = 0;
bcfb84a9
SR
695 /*
696 * We know that all the name entries in the protocols array
697 * are short (< 16 bytes anyway) and are NUL terminated.
698 */
50c2f753 699 for (i = 0; i < CIFS_NUM_PROT; i++) {
bcfb84a9
SR
700 size_t len = strlen(protocols[i].name) + 1;
701
702 memcpy(pSMB->DialectsArray+count, protocols[i].name, len);
703 count += len;
3979877e 704 }
be8e3b00 705 inc_rfc1001_len(pSMB, count);
1da177e4
LT
706 pSMB->ByteCount = cpu_to_le16(count);
707
708 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
709 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
50c2f753 710 if (rc != 0)
254e55ed
SF
711 goto neg_err_exit;
712
9bf67e51 713 server->dialect = le16_to_cpu(pSMBr->DialectIndex);
f96637be 714 cifs_dbg(FYI, "Dialect: %d\n", server->dialect);
254e55ed 715 /* Check wct = 1 error case */
9bf67e51 716 if ((pSMBr->hdr.WordCount < 13) || (server->dialect == BAD_PROT)) {
254e55ed 717 /* core returns wct = 1, but we do not ask for core - otherwise
50c2f753 718 small wct just comes when dialect index is -1 indicating we
254e55ed
SF
719 could not negotiate a common dialect */
720 rc = -EOPNOTSUPP;
721 goto neg_err_exit;
790fe579 722 } else if (pSMBr->hdr.WordCount == 13) {
e598d1d8 723 server->negflavor = CIFS_NEGFLAVOR_LANMAN;
3f618223 724 rc = decode_lanman_negprot_rsp(server, pSMBr);
9ddec561 725 goto signing_check;
790fe579 726 } else if (pSMBr->hdr.WordCount != 17) {
254e55ed
SF
727 /* unknown wct */
728 rc = -EOPNOTSUPP;
729 goto neg_err_exit;
730 }
2190eca1
JL
731 /* else wct == 17, NTLM or better */
732
96daf2b0
SF
733 server->sec_mode = pSMBr->SecurityMode;
734 if ((server->sec_mode & SECMODE_USER) == 0)
f96637be 735 cifs_dbg(FYI, "share mode security\n");
bdc4bf6e 736
254e55ed
SF
737 /* one byte, so no need to convert this or EncryptionKeyLen from
738 little endian */
10b9b98e
PS
739 server->maxReq = min_t(unsigned int, le16_to_cpu(pSMBr->MaxMpxCount),
740 cifs_max_pending);
45275789 741 set_credits(server, server->maxReq);
254e55ed 742 /* probably no need to store and check maxvcs */
c974befa 743 server->maxBuf = le32_to_cpu(pSMBr->MaxBufferSize);
eca6acf9 744 server->max_rw = le32_to_cpu(pSMBr->MaxRawSize);
f96637be 745 cifs_dbg(NOISY, "Max buf = %d\n", ses->server->maxBuf);
254e55ed 746 server->capabilities = le32_to_cpu(pSMBr->Capabilities);
b815f1e5
SF
747 server->timeAdj = (int)(__s16)le16_to_cpu(pSMBr->ServerTimeZone);
748 server->timeAdj *= 60;
31d9e2bd 749
e598d1d8
JL
750 if (pSMBr->EncryptionKeyLength == CIFS_CRYPTO_KEY_SIZE) {
751 server->negflavor = CIFS_NEGFLAVOR_UNENCAP;
d3ba50b1 752 memcpy(ses->server->cryptkey, pSMBr->u.EncryptionKey,
254e55ed 753 CIFS_CRYPTO_KEY_SIZE);
f291095f
NP
754 } else if (pSMBr->hdr.Flags2 & SMBFLG2_EXT_SEC ||
755 server->capabilities & CAP_EXTENDED_SECURITY) {
e598d1d8 756 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
3f618223 757 rc = decode_ext_sec_blob(ses, pSMBr);
e598d1d8 758 } else if (server->sec_mode & SECMODE_PW_ENCRYPT) {
07cc6cf9 759 rc = -EIO; /* no crypt key only if plain text pwd */
e598d1d8
JL
760 } else {
761 server->negflavor = CIFS_NEGFLAVOR_UNENCAP;
254e55ed 762 server->capabilities &= ~CAP_EXTENDED_SECURITY;
e598d1d8 763 }
254e55ed
SF
764
765signing_check:
9ddec561 766 if (!rc)
38d77c50 767 rc = cifs_enable_signing(server, ses->sign);
50c2f753 768neg_err_exit:
4a6d87f1 769 cifs_buf_release(pSMB);
254e55ed 770
f96637be 771 cifs_dbg(FYI, "negprot rc %d\n", rc);
1da177e4
LT
772 return rc;
773}
774
775int
2e6e02ab 776CIFSSMBTDis(const unsigned int xid, struct cifs_tcon *tcon)
1da177e4
LT
777{
778 struct smb_hdr *smb_buffer;
1da177e4 779 int rc = 0;
1da177e4 780
f96637be 781 cifs_dbg(FYI, "In tree disconnect\n");
1da177e4 782
f1987b44
JL
783 /* BB: do we need to check this? These should never be NULL. */
784 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
785 return -EIO;
1da177e4 786
f1987b44
JL
787 /*
788 * No need to return error on this operation if tid invalidated and
789 * closed on server already e.g. due to tcp session crashing. Also,
790 * the tcon is no longer on the list, so no need to take lock before
791 * checking this.
792 */
268875b9 793 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
50c2f753 794 return 0;
1da177e4 795
50c2f753 796 rc = small_smb_init(SMB_COM_TREE_DISCONNECT, 0, tcon,
09d1db5c 797 (void **)&smb_buffer);
f1987b44 798 if (rc)
1da177e4 799 return rc;
133672ef 800
792af7b0 801 rc = SendReceiveNoRsp(xid, tcon->ses, (char *)smb_buffer, 0);
da502f7d 802 cifs_small_buf_release(smb_buffer);
1da177e4 803 if (rc)
f96637be 804 cifs_dbg(FYI, "Tree disconnect failed %d\n", rc);
1da177e4 805
50c2f753 806 /* No need to return error on this operation if tid invalidated and
f1987b44 807 closed on server already e.g. due to tcp session crashing */
1da177e4
LT
808 if (rc == -EAGAIN)
809 rc = 0;
810
811 return rc;
812}
813
766fdbb5
JL
814/*
815 * This is a no-op for now. We're not really interested in the reply, but
816 * rather in the fact that the server sent one and that server->lstrp
817 * gets updated.
818 *
819 * FIXME: maybe we should consider checking that the reply matches request?
820 */
821static void
822cifs_echo_callback(struct mid_q_entry *mid)
823{
824 struct TCP_Server_Info *server = mid->callback_data;
825
826 DeleteMidQEntry(mid);
a891f0f8 827 add_credits(server, 1, CIFS_ECHO_OP);
766fdbb5
JL
828}
829
830int
831CIFSSMBEcho(struct TCP_Server_Info *server)
832{
833 ECHO_REQ *smb;
834 int rc = 0;
738f9de5
PS
835 struct kvec iov[2];
836 struct smb_rqst rqst = { .rq_iov = iov,
837 .rq_nvec = 2 };
766fdbb5 838
f96637be 839 cifs_dbg(FYI, "In echo request\n");
766fdbb5
JL
840
841 rc = small_smb_init(SMB_COM_ECHO, 0, NULL, (void **)&smb);
842 if (rc)
843 return rc;
844
26c9cb66
SF
845 if (server->capabilities & CAP_UNICODE)
846 smb->hdr.Flags2 |= SMBFLG2_UNICODE;
847
766fdbb5 848 /* set up echo request */
5443d130 849 smb->hdr.Tid = 0xffff;
99d86c8f
JL
850 smb->hdr.WordCount = 1;
851 put_unaligned_le16(1, &smb->EchoCount);
820a803f 852 put_bcc(1, &smb->hdr);
766fdbb5 853 smb->Data[0] = 'a';
be8e3b00 854 inc_rfc1001_len(smb, 3);
738f9de5
PS
855
856 iov[0].iov_len = 4;
857 iov[0].iov_base = smb;
858 iov[1].iov_len = get_rfc1002_length(smb);
859 iov[1].iov_base = (char *)smb + 4;
766fdbb5 860
9b7c18a2 861 rc = cifs_call_async(server, &rqst, NULL, cifs_echo_callback, NULL,
a891f0f8 862 server, CIFS_ASYNC_OP | CIFS_ECHO_OP);
766fdbb5 863 if (rc)
f96637be 864 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
766fdbb5
JL
865
866 cifs_small_buf_release(smb);
867
868 return rc;
869}
870
1da177e4 871int
58c45c58 872CIFSSMBLogoff(const unsigned int xid, struct cifs_ses *ses)
1da177e4 873{
1da177e4
LT
874 LOGOFF_ANDX_REQ *pSMB;
875 int rc = 0;
1da177e4 876
f96637be 877 cifs_dbg(FYI, "In SMBLogoff for session disconnect\n");
3b795210 878
14fbf50d
JL
879 /*
880 * BB: do we need to check validity of ses and server? They should
881 * always be valid since we have an active reference. If not, that
882 * should probably be a BUG()
883 */
884 if (!ses || !ses->server)
3b795210
SF
885 return -EIO;
886
d7b619cf 887 mutex_lock(&ses->session_mutex);
3b795210
SF
888 if (ses->need_reconnect)
889 goto session_already_dead; /* no need to send SMBlogoff if uid
890 already closed due to reconnect */
1da177e4
LT
891 rc = small_smb_init(SMB_COM_LOGOFF_ANDX, 2, NULL, (void **)&pSMB);
892 if (rc) {
d7b619cf 893 mutex_unlock(&ses->session_mutex);
1da177e4
LT
894 return rc;
895 }
896
88257360 897 pSMB->hdr.Mid = get_next_mid(ses->server);
1982c344 898
38d77c50
JL
899 if (ses->server->sign)
900 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
1da177e4
LT
901
902 pSMB->hdr.Uid = ses->Suid;
903
904 pSMB->AndXCommand = 0xFF;
792af7b0 905 rc = SendReceiveNoRsp(xid, ses, (char *) pSMB, 0);
da502f7d 906 cifs_small_buf_release(pSMB);
3b795210 907session_already_dead:
d7b619cf 908 mutex_unlock(&ses->session_mutex);
1da177e4
LT
909
910 /* if session dead then we do not need to do ulogoff,
50c2f753 911 since server closed smb session, no sense reporting
1da177e4
LT
912 error */
913 if (rc == -EAGAIN)
914 rc = 0;
915 return rc;
916}
917
2d785a50 918int
6d5786a3
PS
919CIFSPOSIXDelFile(const unsigned int xid, struct cifs_tcon *tcon,
920 const char *fileName, __u16 type,
921 const struct nls_table *nls_codepage, int remap)
2d785a50
SF
922{
923 TRANSACTION2_SPI_REQ *pSMB = NULL;
924 TRANSACTION2_SPI_RSP *pSMBr = NULL;
925 struct unlink_psx_rq *pRqD;
926 int name_len;
927 int rc = 0;
928 int bytes_returned = 0;
929 __u16 params, param_offset, offset, byte_count;
930
f96637be 931 cifs_dbg(FYI, "In POSIX delete\n");
2d785a50
SF
932PsxDelete:
933 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
934 (void **) &pSMBr);
935 if (rc)
936 return rc;
937
938 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
939 name_len =
acbbb76a
SF
940 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
941 PATH_MAX, nls_codepage, remap);
2d785a50
SF
942 name_len++; /* trailing null */
943 name_len *= 2;
944 } else { /* BB add path length overrun check */
945 name_len = strnlen(fileName, PATH_MAX);
946 name_len++; /* trailing null */
947 strncpy(pSMB->FileName, fileName, name_len);
948 }
949
950 params = 6 + name_len;
951 pSMB->MaxParameterCount = cpu_to_le16(2);
952 pSMB->MaxDataCount = 0; /* BB double check this with jra */
953 pSMB->MaxSetupCount = 0;
954 pSMB->Reserved = 0;
955 pSMB->Flags = 0;
956 pSMB->Timeout = 0;
957 pSMB->Reserved2 = 0;
958 param_offset = offsetof(struct smb_com_transaction2_spi_req,
959 InformationLevel) - 4;
960 offset = param_offset + params;
961
962 /* Setup pointer to Request Data (inode type) */
963 pRqD = (struct unlink_psx_rq *)(((char *)&pSMB->hdr.Protocol) + offset);
964 pRqD->type = cpu_to_le16(type);
965 pSMB->ParameterOffset = cpu_to_le16(param_offset);
966 pSMB->DataOffset = cpu_to_le16(offset);
967 pSMB->SetupCount = 1;
968 pSMB->Reserved3 = 0;
969 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
970 byte_count = 3 /* pad */ + params + sizeof(struct unlink_psx_rq);
971
972 pSMB->DataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
973 pSMB->TotalDataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
974 pSMB->ParameterCount = cpu_to_le16(params);
975 pSMB->TotalParameterCount = pSMB->ParameterCount;
976 pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_UNLINK);
977 pSMB->Reserved4 = 0;
be8e3b00 978 inc_rfc1001_len(pSMB, byte_count);
2d785a50
SF
979 pSMB->ByteCount = cpu_to_le16(byte_count);
980 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
981 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
ad7a2926 982 if (rc)
f96637be 983 cifs_dbg(FYI, "Posix delete returned %d\n", rc);
2d785a50
SF
984 cifs_buf_release(pSMB);
985
44c58186 986 cifs_stats_inc(&tcon->stats.cifs_stats.num_deletes);
2d785a50
SF
987
988 if (rc == -EAGAIN)
989 goto PsxDelete;
990
991 return rc;
992}
993
1da177e4 994int
ed6875e0
PS
995CIFSSMBDelFile(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
996 struct cifs_sb_info *cifs_sb)
1da177e4
LT
997{
998 DELETE_FILE_REQ *pSMB = NULL;
999 DELETE_FILE_RSP *pSMBr = NULL;
1000 int rc = 0;
1001 int bytes_returned;
1002 int name_len;
2baa2682 1003 int remap = cifs_remap(cifs_sb);
1da177e4
LT
1004
1005DelFileRetry:
1006 rc = smb_init(SMB_COM_DELETE, 1, tcon, (void **) &pSMB,
1007 (void **) &pSMBr);
1008 if (rc)
1009 return rc;
1010
1011 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
ed6875e0
PS
1012 name_len = cifsConvertToUTF16((__le16 *) pSMB->fileName, name,
1013 PATH_MAX, cifs_sb->local_nls,
1014 remap);
1da177e4
LT
1015 name_len++; /* trailing null */
1016 name_len *= 2;
09d1db5c 1017 } else { /* BB improve check for buffer overruns BB */
ed6875e0 1018 name_len = strnlen(name, PATH_MAX);
1da177e4 1019 name_len++; /* trailing null */
ed6875e0 1020 strncpy(pSMB->fileName, name, name_len);
1da177e4
LT
1021 }
1022 pSMB->SearchAttributes =
1023 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM);
1024 pSMB->BufferFormat = 0x04;
be8e3b00 1025 inc_rfc1001_len(pSMB, name_len + 1);
1da177e4
LT
1026 pSMB->ByteCount = cpu_to_le16(name_len + 1);
1027 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1028 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
44c58186 1029 cifs_stats_inc(&tcon->stats.cifs_stats.num_deletes);
ad7a2926 1030 if (rc)
f96637be 1031 cifs_dbg(FYI, "Error in RMFile = %d\n", rc);
1da177e4
LT
1032
1033 cifs_buf_release(pSMB);
1034 if (rc == -EAGAIN)
1035 goto DelFileRetry;
1036
1037 return rc;
1038}
1039
1040int
f958ca5d
PS
1041CIFSSMBRmDir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
1042 struct cifs_sb_info *cifs_sb)
1da177e4
LT
1043{
1044 DELETE_DIRECTORY_REQ *pSMB = NULL;
1045 DELETE_DIRECTORY_RSP *pSMBr = NULL;
1046 int rc = 0;
1047 int bytes_returned;
1048 int name_len;
2baa2682 1049 int remap = cifs_remap(cifs_sb);
1da177e4 1050
f96637be 1051 cifs_dbg(FYI, "In CIFSSMBRmDir\n");
1da177e4
LT
1052RmDirRetry:
1053 rc = smb_init(SMB_COM_DELETE_DIRECTORY, 0, tcon, (void **) &pSMB,
1054 (void **) &pSMBr);
1055 if (rc)
1056 return rc;
1057
1058 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
f958ca5d
PS
1059 name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, name,
1060 PATH_MAX, cifs_sb->local_nls,
1061 remap);
1da177e4
LT
1062 name_len++; /* trailing null */
1063 name_len *= 2;
09d1db5c 1064 } else { /* BB improve check for buffer overruns BB */
f958ca5d 1065 name_len = strnlen(name, PATH_MAX);
1da177e4 1066 name_len++; /* trailing null */
f958ca5d 1067 strncpy(pSMB->DirName, name, name_len);
1da177e4
LT
1068 }
1069
1070 pSMB->BufferFormat = 0x04;
be8e3b00 1071 inc_rfc1001_len(pSMB, name_len + 1);
1da177e4
LT
1072 pSMB->ByteCount = cpu_to_le16(name_len + 1);
1073 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1074 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
44c58186 1075 cifs_stats_inc(&tcon->stats.cifs_stats.num_rmdirs);
ad7a2926 1076 if (rc)
f96637be 1077 cifs_dbg(FYI, "Error in RMDir = %d\n", rc);
1da177e4
LT
1078
1079 cifs_buf_release(pSMB);
1080 if (rc == -EAGAIN)
1081 goto RmDirRetry;
1082 return rc;
1083}
1084
1085int
f436720e
PS
1086CIFSSMBMkDir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
1087 struct cifs_sb_info *cifs_sb)
1da177e4
LT
1088{
1089 int rc = 0;
1090 CREATE_DIRECTORY_REQ *pSMB = NULL;
1091 CREATE_DIRECTORY_RSP *pSMBr = NULL;
1092 int bytes_returned;
1093 int name_len;
2baa2682 1094 int remap = cifs_remap(cifs_sb);
1da177e4 1095
f96637be 1096 cifs_dbg(FYI, "In CIFSSMBMkDir\n");
1da177e4
LT
1097MkDirRetry:
1098 rc = smb_init(SMB_COM_CREATE_DIRECTORY, 0, tcon, (void **) &pSMB,
1099 (void **) &pSMBr);
1100 if (rc)
1101 return rc;
1102
1103 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
acbbb76a 1104 name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, name,
f436720e
PS
1105 PATH_MAX, cifs_sb->local_nls,
1106 remap);
1da177e4
LT
1107 name_len++; /* trailing null */
1108 name_len *= 2;
09d1db5c 1109 } else { /* BB improve check for buffer overruns BB */
1da177e4
LT
1110 name_len = strnlen(name, PATH_MAX);
1111 name_len++; /* trailing null */
1112 strncpy(pSMB->DirName, name, name_len);
1113 }
1114
1115 pSMB->BufferFormat = 0x04;
be8e3b00 1116 inc_rfc1001_len(pSMB, name_len + 1);
1da177e4
LT
1117 pSMB->ByteCount = cpu_to_le16(name_len + 1);
1118 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1119 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
44c58186 1120 cifs_stats_inc(&tcon->stats.cifs_stats.num_mkdirs);
ad7a2926 1121 if (rc)
f96637be 1122 cifs_dbg(FYI, "Error in Mkdir = %d\n", rc);
a5a2b489 1123
1da177e4
LT
1124 cifs_buf_release(pSMB);
1125 if (rc == -EAGAIN)
1126 goto MkDirRetry;
1127 return rc;
1128}
1129
2dd29d31 1130int
6d5786a3
PS
1131CIFSPOSIXCreate(const unsigned int xid, struct cifs_tcon *tcon,
1132 __u32 posix_flags, __u64 mode, __u16 *netfid,
1133 FILE_UNIX_BASIC_INFO *pRetData, __u32 *pOplock,
1134 const char *name, const struct nls_table *nls_codepage,
1135 int remap)
2dd29d31
SF
1136{
1137 TRANSACTION2_SPI_REQ *pSMB = NULL;
1138 TRANSACTION2_SPI_RSP *pSMBr = NULL;
1139 int name_len;
1140 int rc = 0;
1141 int bytes_returned = 0;
2dd29d31 1142 __u16 params, param_offset, offset, byte_count, count;
ad7a2926
SF
1143 OPEN_PSX_REQ *pdata;
1144 OPEN_PSX_RSP *psx_rsp;
2dd29d31 1145
f96637be 1146 cifs_dbg(FYI, "In POSIX Create\n");
2dd29d31
SF
1147PsxCreat:
1148 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
1149 (void **) &pSMBr);
1150 if (rc)
1151 return rc;
1152
1153 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1154 name_len =
acbbb76a
SF
1155 cifsConvertToUTF16((__le16 *) pSMB->FileName, name,
1156 PATH_MAX, nls_codepage, remap);
2dd29d31
SF
1157 name_len++; /* trailing null */
1158 name_len *= 2;
1159 } else { /* BB improve the check for buffer overruns BB */
1160 name_len = strnlen(name, PATH_MAX);
1161 name_len++; /* trailing null */
1162 strncpy(pSMB->FileName, name, name_len);
1163 }
1164
1165 params = 6 + name_len;
1166 count = sizeof(OPEN_PSX_REQ);
1167 pSMB->MaxParameterCount = cpu_to_le16(2);
1168 pSMB->MaxDataCount = cpu_to_le16(1000); /* large enough */
1169 pSMB->MaxSetupCount = 0;
1170 pSMB->Reserved = 0;
1171 pSMB->Flags = 0;
1172 pSMB->Timeout = 0;
1173 pSMB->Reserved2 = 0;
1174 param_offset = offsetof(struct smb_com_transaction2_spi_req,
50c2f753 1175 InformationLevel) - 4;
2dd29d31 1176 offset = param_offset + params;
2dd29d31 1177 pdata = (OPEN_PSX_REQ *)(((char *)&pSMB->hdr.Protocol) + offset);
8f2376ad 1178 pdata->Level = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
2dd29d31 1179 pdata->Permissions = cpu_to_le64(mode);
50c2f753 1180 pdata->PosixOpenFlags = cpu_to_le32(posix_flags);
2dd29d31
SF
1181 pdata->OpenFlags = cpu_to_le32(*pOplock);
1182 pSMB->ParameterOffset = cpu_to_le16(param_offset);
1183 pSMB->DataOffset = cpu_to_le16(offset);
1184 pSMB->SetupCount = 1;
1185 pSMB->Reserved3 = 0;
1186 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
1187 byte_count = 3 /* pad */ + params + count;
1188
1189 pSMB->DataCount = cpu_to_le16(count);
1190 pSMB->ParameterCount = cpu_to_le16(params);
1191 pSMB->TotalDataCount = pSMB->DataCount;
1192 pSMB->TotalParameterCount = pSMB->ParameterCount;
1193 pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_OPEN);
1194 pSMB->Reserved4 = 0;
be8e3b00 1195 inc_rfc1001_len(pSMB, byte_count);
2dd29d31
SF
1196 pSMB->ByteCount = cpu_to_le16(byte_count);
1197 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1198 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
1199 if (rc) {
f96637be 1200 cifs_dbg(FYI, "Posix create returned %d\n", rc);
2dd29d31
SF
1201 goto psx_create_err;
1202 }
1203
f96637be 1204 cifs_dbg(FYI, "copying inode info\n");
2dd29d31
SF
1205 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
1206
820a803f 1207 if (rc || get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)) {
2dd29d31
SF
1208 rc = -EIO; /* bad smb */
1209 goto psx_create_err;
1210 }
1211
1212 /* copy return information to pRetData */
50c2f753 1213 psx_rsp = (OPEN_PSX_RSP *)((char *) &pSMBr->hdr.Protocol
2dd29d31 1214 + le16_to_cpu(pSMBr->t2.DataOffset));
50c2f753 1215
2dd29d31 1216 *pOplock = le16_to_cpu(psx_rsp->OplockFlags);
790fe579 1217 if (netfid)
2dd29d31
SF
1218 *netfid = psx_rsp->Fid; /* cifs fid stays in le */
1219 /* Let caller know file was created so we can set the mode. */
1220 /* Do we care about the CreateAction in any other cases? */
790fe579 1221 if (cpu_to_le32(FILE_CREATE) == psx_rsp->CreateAction)
2dd29d31
SF
1222 *pOplock |= CIFS_CREATE_ACTION;
1223 /* check to make sure response data is there */
8f2376ad
CG
1224 if (psx_rsp->ReturnedLevel != cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC)) {
1225 pRetData->Type = cpu_to_le32(-1); /* unknown */
f96637be 1226 cifs_dbg(NOISY, "unknown type\n");
cbac3cba 1227 } else {
820a803f 1228 if (get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)
2dd29d31 1229 + sizeof(FILE_UNIX_BASIC_INFO)) {
f96637be 1230 cifs_dbg(VFS, "Open response data too small\n");
8f2376ad 1231 pRetData->Type = cpu_to_le32(-1);
2dd29d31
SF
1232 goto psx_create_err;
1233 }
50c2f753 1234 memcpy((char *) pRetData,
cbac3cba 1235 (char *)psx_rsp + sizeof(OPEN_PSX_RSP),
26f57364 1236 sizeof(FILE_UNIX_BASIC_INFO));
2dd29d31 1237 }
2dd29d31
SF
1238
1239psx_create_err:
1240 cifs_buf_release(pSMB);
1241
65bc98b0 1242 if (posix_flags & SMB_O_DIRECTORY)
44c58186 1243 cifs_stats_inc(&tcon->stats.cifs_stats.num_posixmkdirs);
65bc98b0 1244 else
44c58186 1245 cifs_stats_inc(&tcon->stats.cifs_stats.num_posixopens);
2dd29d31
SF
1246
1247 if (rc == -EAGAIN)
1248 goto PsxCreat;
1249
50c2f753 1250 return rc;
2dd29d31
SF
1251}
1252
a9d02ad4
SF
1253static __u16 convert_disposition(int disposition)
1254{
1255 __u16 ofun = 0;
1256
1257 switch (disposition) {
1258 case FILE_SUPERSEDE:
1259 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1260 break;
1261 case FILE_OPEN:
1262 ofun = SMBOPEN_OAPPEND;
1263 break;
1264 case FILE_CREATE:
1265 ofun = SMBOPEN_OCREATE;
1266 break;
1267 case FILE_OPEN_IF:
1268 ofun = SMBOPEN_OCREATE | SMBOPEN_OAPPEND;
1269 break;
1270 case FILE_OVERWRITE:
1271 ofun = SMBOPEN_OTRUNC;
1272 break;
1273 case FILE_OVERWRITE_IF:
1274 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1275 break;
1276 default:
f96637be 1277 cifs_dbg(FYI, "unknown disposition %d\n", disposition);
a9d02ad4
SF
1278 ofun = SMBOPEN_OAPPEND; /* regular open */
1279 }
1280 return ofun;
1281}
1282
35fc37d5
JL
1283static int
1284access_flags_to_smbopen_mode(const int access_flags)
1285{
1286 int masked_flags = access_flags & (GENERIC_READ | GENERIC_WRITE);
1287
1288 if (masked_flags == GENERIC_READ)
1289 return SMBOPEN_READ;
1290 else if (masked_flags == GENERIC_WRITE)
1291 return SMBOPEN_WRITE;
1292
1293 /* just go for read/write */
1294 return SMBOPEN_READWRITE;
1295}
1296
a9d02ad4 1297int
6d5786a3 1298SMBLegacyOpen(const unsigned int xid, struct cifs_tcon *tcon,
a9d02ad4 1299 const char *fileName, const int openDisposition,
ad7a2926
SF
1300 const int access_flags, const int create_options, __u16 *netfid,
1301 int *pOplock, FILE_ALL_INFO *pfile_info,
a9d02ad4
SF
1302 const struct nls_table *nls_codepage, int remap)
1303{
1304 int rc = -EACCES;
1305 OPENX_REQ *pSMB = NULL;
1306 OPENX_RSP *pSMBr = NULL;
1307 int bytes_returned;
1308 int name_len;
1309 __u16 count;
1310
1311OldOpenRetry:
1312 rc = smb_init(SMB_COM_OPEN_ANDX, 15, tcon, (void **) &pSMB,
1313 (void **) &pSMBr);
1314 if (rc)
1315 return rc;
1316
1317 pSMB->AndXCommand = 0xFF; /* none */
1318
1319 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1320 count = 1; /* account for one byte pad to word boundary */
1321 name_len =
acbbb76a
SF
1322 cifsConvertToUTF16((__le16 *) (pSMB->fileName + 1),
1323 fileName, PATH_MAX, nls_codepage, remap);
a9d02ad4
SF
1324 name_len++; /* trailing null */
1325 name_len *= 2;
1326 } else { /* BB improve check for buffer overruns BB */
1327 count = 0; /* no pad */
1328 name_len = strnlen(fileName, PATH_MAX);
1329 name_len++; /* trailing null */
1330 strncpy(pSMB->fileName, fileName, name_len);
1331 }
1332 if (*pOplock & REQ_OPLOCK)
1333 pSMB->OpenFlags = cpu_to_le16(REQ_OPLOCK);
26f57364 1334 else if (*pOplock & REQ_BATCHOPLOCK)
a9d02ad4 1335 pSMB->OpenFlags = cpu_to_le16(REQ_BATCHOPLOCK);
26f57364 1336
a9d02ad4 1337 pSMB->OpenFlags |= cpu_to_le16(REQ_MORE_INFO);
35fc37d5 1338 pSMB->Mode = cpu_to_le16(access_flags_to_smbopen_mode(access_flags));
a9d02ad4
SF
1339 pSMB->Mode |= cpu_to_le16(0x40); /* deny none */
1340 /* set file as system file if special file such
1341 as fifo and server expecting SFU style and
1342 no Unix extensions */
1343
790fe579
SF
1344 if (create_options & CREATE_OPTION_SPECIAL)
1345 pSMB->FileAttributes = cpu_to_le16(ATTR_SYSTEM);
ad7a2926
SF
1346 else /* BB FIXME BB */
1347 pSMB->FileAttributes = cpu_to_le16(0/*ATTR_NORMAL*/);
a9d02ad4 1348
67750fb9
JL
1349 if (create_options & CREATE_OPTION_READONLY)
1350 pSMB->FileAttributes |= cpu_to_le16(ATTR_READONLY);
a9d02ad4
SF
1351
1352 /* BB FIXME BB */
50c2f753
SF
1353/* pSMB->CreateOptions = cpu_to_le32(create_options &
1354 CREATE_OPTIONS_MASK); */
a9d02ad4 1355 /* BB FIXME END BB */
3e87d803
SF
1356
1357 pSMB->Sattr = cpu_to_le16(ATTR_HIDDEN | ATTR_SYSTEM | ATTR_DIRECTORY);
70ca734a 1358 pSMB->OpenFunction = cpu_to_le16(convert_disposition(openDisposition));
a9d02ad4 1359 count += name_len;
be8e3b00 1360 inc_rfc1001_len(pSMB, count);
a9d02ad4
SF
1361
1362 pSMB->ByteCount = cpu_to_le16(count);
a9d02ad4 1363 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
7749981e 1364 (struct smb_hdr *)pSMBr, &bytes_returned, 0);
44c58186 1365 cifs_stats_inc(&tcon->stats.cifs_stats.num_opens);
a9d02ad4 1366 if (rc) {
f96637be 1367 cifs_dbg(FYI, "Error in Open = %d\n", rc);
a9d02ad4
SF
1368 } else {
1369 /* BB verify if wct == 15 */
1370
582d21e5 1371/* *pOplock = pSMBr->OplockLevel; */ /* BB take from action field*/
a9d02ad4
SF
1372
1373 *netfid = pSMBr->Fid; /* cifs fid stays in le */
1374 /* Let caller know file was created so we can set the mode. */
1375 /* Do we care about the CreateAction in any other cases? */
1376 /* BB FIXME BB */
790fe579 1377/* if (cpu_to_le32(FILE_CREATE) == pSMBr->CreateAction)
a9d02ad4
SF
1378 *pOplock |= CIFS_CREATE_ACTION; */
1379 /* BB FIXME END */
1380
790fe579 1381 if (pfile_info) {
a9d02ad4
SF
1382 pfile_info->CreationTime = 0; /* BB convert CreateTime*/
1383 pfile_info->LastAccessTime = 0; /* BB fixme */
1384 pfile_info->LastWriteTime = 0; /* BB fixme */
1385 pfile_info->ChangeTime = 0; /* BB fixme */
70ca734a 1386 pfile_info->Attributes =
50c2f753 1387 cpu_to_le32(le16_to_cpu(pSMBr->FileAttributes));
a9d02ad4 1388 /* the file_info buf is endian converted by caller */
70ca734a
SF
1389 pfile_info->AllocationSize =
1390 cpu_to_le64(le32_to_cpu(pSMBr->EndOfFile));
1391 pfile_info->EndOfFile = pfile_info->AllocationSize;
a9d02ad4 1392 pfile_info->NumberOfLinks = cpu_to_le32(1);
9a8165fc 1393 pfile_info->DeletePending = 0;
a9d02ad4
SF
1394 }
1395 }
1396
1397 cifs_buf_release(pSMB);
1398 if (rc == -EAGAIN)
1399 goto OldOpenRetry;
1400 return rc;
1401}
1402
1da177e4 1403int
d81b8a40
PS
1404CIFS_open(const unsigned int xid, struct cifs_open_parms *oparms, int *oplock,
1405 FILE_ALL_INFO *buf)
1da177e4
LT
1406{
1407 int rc = -EACCES;
9bf4fa01
PS
1408 OPEN_REQ *req = NULL;
1409 OPEN_RSP *rsp = NULL;
1da177e4
LT
1410 int bytes_returned;
1411 int name_len;
1412 __u16 count;
d81b8a40
PS
1413 struct cifs_sb_info *cifs_sb = oparms->cifs_sb;
1414 struct cifs_tcon *tcon = oparms->tcon;
2baa2682 1415 int remap = cifs_remap(cifs_sb);
d81b8a40
PS
1416 const struct nls_table *nls = cifs_sb->local_nls;
1417 int create_options = oparms->create_options;
1418 int desired_access = oparms->desired_access;
1419 int disposition = oparms->disposition;
1420 const char *path = oparms->path;
1da177e4
LT
1421
1422openRetry:
9bf4fa01
PS
1423 rc = smb_init(SMB_COM_NT_CREATE_ANDX, 24, tcon, (void **)&req,
1424 (void **)&rsp);
1da177e4
LT
1425 if (rc)
1426 return rc;
1427
9bf4fa01
PS
1428 /* no commands go after this */
1429 req->AndXCommand = 0xFF;
1da177e4 1430
9bf4fa01
PS
1431 if (req->hdr.Flags2 & SMBFLG2_UNICODE) {
1432 /* account for one byte pad to word boundary */
1433 count = 1;
1434 name_len = cifsConvertToUTF16((__le16 *)(req->fileName + 1),
1435 path, PATH_MAX, nls, remap);
1436 /* trailing null */
1437 name_len++;
1da177e4 1438 name_len *= 2;
9bf4fa01
PS
1439 req->NameLength = cpu_to_le16(name_len);
1440 } else {
1441 /* BB improve check for buffer overruns BB */
1442 /* no pad */
1443 count = 0;
1444 name_len = strnlen(path, PATH_MAX);
1445 /* trailing null */
1446 name_len++;
1447 req->NameLength = cpu_to_le16(name_len);
1448 strncpy(req->fileName, path, name_len);
1da177e4 1449 }
9bf4fa01
PS
1450
1451 if (*oplock & REQ_OPLOCK)
1452 req->OpenFlags = cpu_to_le32(REQ_OPLOCK);
1453 else if (*oplock & REQ_BATCHOPLOCK)
1454 req->OpenFlags = cpu_to_le32(REQ_BATCHOPLOCK);
1455
1456 req->DesiredAccess = cpu_to_le32(desired_access);
1457 req->AllocationSize = 0;
1458
1459 /*
1460 * Set file as system file if special file such as fifo and server
1461 * expecting SFU style and no Unix extensions.
1462 */
790fe579 1463 if (create_options & CREATE_OPTION_SPECIAL)
9bf4fa01 1464 req->FileAttributes = cpu_to_le32(ATTR_SYSTEM);
eda3c029 1465 else
9bf4fa01 1466 req->FileAttributes = cpu_to_le32(ATTR_NORMAL);
67750fb9 1467
9bf4fa01
PS
1468 /*
1469 * XP does not handle ATTR_POSIX_SEMANTICS but it helps speed up case
1470 * sensitive checks for other servers such as Samba.
1471 */
1da177e4 1472 if (tcon->ses->capabilities & CAP_UNIX)
9bf4fa01 1473 req->FileAttributes |= cpu_to_le32(ATTR_POSIX_SEMANTICS);
1da177e4 1474
67750fb9 1475 if (create_options & CREATE_OPTION_READONLY)
9bf4fa01
PS
1476 req->FileAttributes |= cpu_to_le32(ATTR_READONLY);
1477
1478 req->ShareAccess = cpu_to_le32(FILE_SHARE_ALL);
1479 req->CreateDisposition = cpu_to_le32(disposition);
1480 req->CreateOptions = cpu_to_le32(create_options & CREATE_OPTIONS_MASK);
67750fb9 1481
09d1db5c 1482 /* BB Expirement with various impersonation levels and verify */
9bf4fa01
PS
1483 req->ImpersonationLevel = cpu_to_le32(SECURITY_IMPERSONATION);
1484 req->SecurityFlags = SECURITY_CONTEXT_TRACKING|SECURITY_EFFECTIVE_ONLY;
1da177e4
LT
1485
1486 count += name_len;
9bf4fa01 1487 inc_rfc1001_len(req, count);
1da177e4 1488
9bf4fa01
PS
1489 req->ByteCount = cpu_to_le16(count);
1490 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *)req,
1491 (struct smb_hdr *)rsp, &bytes_returned, 0);
44c58186 1492 cifs_stats_inc(&tcon->stats.cifs_stats.num_opens);
1da177e4 1493 if (rc) {
f96637be 1494 cifs_dbg(FYI, "Error in Open = %d\n", rc);
9bf4fa01
PS
1495 cifs_buf_release(req);
1496 if (rc == -EAGAIN)
1497 goto openRetry;
1498 return rc;
1da177e4 1499 }
a5a2b489 1500
9bf4fa01
PS
1501 /* 1 byte no need to le_to_cpu */
1502 *oplock = rsp->OplockLevel;
1503 /* cifs fid stays in le */
d81b8a40 1504 oparms->fid->netfid = rsp->Fid;
9bf4fa01
PS
1505
1506 /* Let caller know file was created so we can set the mode. */
1507 /* Do we care about the CreateAction in any other cases? */
1508 if (cpu_to_le32(FILE_CREATE) == rsp->CreateAction)
1509 *oplock |= CIFS_CREATE_ACTION;
1510
1511 if (buf) {
1512 /* copy from CreationTime to Attributes */
1513 memcpy((char *)buf, (char *)&rsp->CreationTime, 36);
1514 /* the file_info buf is endian converted by caller */
1515 buf->AllocationSize = rsp->AllocationSize;
1516 buf->EndOfFile = rsp->EndOfFile;
1517 buf->NumberOfLinks = cpu_to_le32(1);
1518 buf->DeletePending = 0;
1519 }
1520
1521 cifs_buf_release(req);
1da177e4
LT
1522 return rc;
1523}
1524
e28bc5b1
JL
1525/*
1526 * Discard any remaining data in the current SMB. To do this, we borrow the
1527 * current bigbuf.
1528 */
c42a6abe 1529int
350be257 1530cifs_discard_remaining_data(struct TCP_Server_Info *server)
e28bc5b1 1531{
05432e29
RS
1532 unsigned int rfclen = server->pdu_size;
1533 int remaining = rfclen + server->vals->header_preamble_size -
1534 server->total_read;
e28bc5b1
JL
1535
1536 while (remaining > 0) {
1537 int length;
1538
1539 length = cifs_read_from_socket(server, server->bigbuf,
1540 min_t(unsigned int, remaining,
1887f601 1541 CIFSMaxBufSize + MAX_HEADER_SIZE(server)));
e28bc5b1
JL
1542 if (length < 0)
1543 return length;
1544 server->total_read += length;
1545 remaining -= length;
1546 }
1547
e28bc5b1
JL
1548 return 0;
1549}
1550
6cc3b242 1551static int
8004c78c
PS
1552__cifs_readv_discard(struct TCP_Server_Info *server, struct mid_q_entry *mid,
1553 bool malformed)
6cc3b242
PS
1554{
1555 int length;
6cc3b242 1556
350be257 1557 length = cifs_discard_remaining_data(server);
8004c78c 1558 dequeue_mid(mid, malformed);
350be257
PS
1559 mid->resp_buf = server->smallbuf;
1560 server->smallbuf = NULL;
6cc3b242
PS
1561 return length;
1562}
1563
8004c78c
PS
1564static int
1565cifs_readv_discard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1566{
1567 struct cifs_readdata *rdata = mid->callback_data;
1568
1569 return __cifs_readv_discard(server, mid, rdata->result);
1570}
1571
09a4707e 1572int
e28bc5b1
JL
1573cifs_readv_receive(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1574{
1575 int length, len;
8d5ce4d2 1576 unsigned int data_offset, data_len;
e28bc5b1 1577 struct cifs_readdata *rdata = mid->callback_data;
5ffef7bf 1578 char *buf = server->smallbuf;
2e96467d 1579 unsigned int buflen = server->pdu_size +
93012bf9 1580 server->vals->header_preamble_size;
74dcf418 1581 bool use_rdma_mr = false;
e28bc5b1 1582
f96637be
JP
1583 cifs_dbg(FYI, "%s: mid=%llu offset=%llu bytes=%u\n",
1584 __func__, mid->mid, rdata->offset, rdata->bytes);
e28bc5b1
JL
1585
1586 /*
1587 * read the rest of READ_RSP header (sans Data array), or whatever we
1588 * can if there's not enough data. At this point, we've read down to
1589 * the Mid.
1590 */
eb378711 1591 len = min_t(unsigned int, buflen, server->vals->read_rsp_size) -
1887f601 1592 HEADER_SIZE(server) + 1;
e28bc5b1 1593
a6137305
AV
1594 length = cifs_read_from_socket(server,
1595 buf + HEADER_SIZE(server) - 1, len);
e28bc5b1
JL
1596 if (length < 0)
1597 return length;
1598 server->total_read += length;
1599
511c54a2
PS
1600 if (server->ops->is_session_expired &&
1601 server->ops->is_session_expired(buf)) {
1602 cifs_reconnect(server);
1603 wake_up(&server->response_q);
1604 return -1;
1605 }
1606
6cc3b242 1607 if (server->ops->is_status_pending &&
66265f13 1608 server->ops->is_status_pending(buf, server)) {
350be257 1609 cifs_discard_remaining_data(server);
6cc3b242
PS
1610 return -1;
1611 }
1612
8004c78c
PS
1613 /* set up first two iov for signature check and to get credits */
1614 rdata->iov[0].iov_base = buf;
1615 rdata->iov[0].iov_len = 4;
1616 rdata->iov[1].iov_base = buf + 4;
1617 rdata->iov[1].iov_len = server->total_read - 4;
1618 cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
1619 rdata->iov[0].iov_base, rdata->iov[0].iov_len);
1620 cifs_dbg(FYI, "1: iov_base=%p iov_len=%zu\n",
1621 rdata->iov[1].iov_base, rdata->iov[1].iov_len);
1622
e28bc5b1 1623 /* Was the SMB read successful? */
eb378711 1624 rdata->result = server->ops->map_error(buf, false);
e28bc5b1 1625 if (rdata->result != 0) {
f96637be
JP
1626 cifs_dbg(FYI, "%s: server returned error %d\n",
1627 __func__, rdata->result);
8004c78c
PS
1628 /* normal error on read response */
1629 return __cifs_readv_discard(server, mid, false);
e28bc5b1
JL
1630 }
1631
1632 /* Is there enough to get to the rest of the READ_RSP header? */
eb378711 1633 if (server->total_read < server->vals->read_rsp_size) {
f96637be
JP
1634 cifs_dbg(FYI, "%s: server returned short header. got=%u expected=%zu\n",
1635 __func__, server->total_read,
1636 server->vals->read_rsp_size);
e28bc5b1
JL
1637 rdata->result = -EIO;
1638 return cifs_readv_discard(server, mid);
1639 }
1640
93012bf9
RS
1641 data_offset = server->ops->read_data_offset(buf) +
1642 server->vals->header_preamble_size;
e28bc5b1
JL
1643 if (data_offset < server->total_read) {
1644 /*
1645 * win2k8 sometimes sends an offset of 0 when the read
1646 * is beyond the EOF. Treat it as if the data starts just after
1647 * the header.
1648 */
f96637be
JP
1649 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
1650 __func__, data_offset);
e28bc5b1
JL
1651 data_offset = server->total_read;
1652 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
1653 /* data_offset is beyond the end of smallbuf */
f96637be
JP
1654 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
1655 __func__, data_offset);
e28bc5b1
JL
1656 rdata->result = -EIO;
1657 return cifs_readv_discard(server, mid);
1658 }
1659
f96637be
JP
1660 cifs_dbg(FYI, "%s: total_read=%u data_offset=%u\n",
1661 __func__, server->total_read, data_offset);
e28bc5b1
JL
1662
1663 len = data_offset - server->total_read;
1664 if (len > 0) {
1665 /* read any junk before data into the rest of smallbuf */
a6137305
AV
1666 length = cifs_read_from_socket(server,
1667 buf + server->total_read, len);
e28bc5b1
JL
1668 if (length < 0)
1669 return length;
1670 server->total_read += length;
1671 }
1672
e28bc5b1 1673 /* how much data is in the response? */
74dcf418
LL
1674#ifdef CONFIG_CIFS_SMB_DIRECT
1675 use_rdma_mr = rdata->mr;
1676#endif
1677 data_len = server->ops->read_data_length(buf, use_rdma_mr);
1678 if (!use_rdma_mr && (data_offset + data_len > buflen)) {
e28bc5b1
JL
1679 /* data_len is corrupt -- discard frame */
1680 rdata->result = -EIO;
1681 return cifs_readv_discard(server, mid);
1682 }
1683
8321fec4
JL
1684 length = rdata->read_into_pages(server, rdata, data_len);
1685 if (length < 0)
1686 return length;
e28bc5b1 1687
8321fec4 1688 server->total_read += length;
e28bc5b1 1689
f96637be
JP
1690 cifs_dbg(FYI, "total_read=%u buflen=%u remaining=%u\n",
1691 server->total_read, buflen, data_len);
e28bc5b1
JL
1692
1693 /* discard anything left over */
5ffef7bf 1694 if (server->total_read < buflen)
e28bc5b1
JL
1695 return cifs_readv_discard(server, mid);
1696
1697 dequeue_mid(mid, false);
350be257
PS
1698 mid->resp_buf = server->smallbuf;
1699 server->smallbuf = NULL;
e28bc5b1
JL
1700 return length;
1701}
1702
e28bc5b1
JL
1703static void
1704cifs_readv_callback(struct mid_q_entry *mid)
1705{
1706 struct cifs_readdata *rdata = mid->callback_data;
1707 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
1708 struct TCP_Server_Info *server = tcon->ses->server;
738f9de5
PS
1709 struct smb_rqst rqst = { .rq_iov = rdata->iov,
1710 .rq_nvec = 2,
8321fec4 1711 .rq_pages = rdata->pages,
6d3adb23 1712 .rq_offset = rdata->page_offset,
8321fec4
JL
1713 .rq_npages = rdata->nr_pages,
1714 .rq_pagesz = rdata->pagesz,
1715 .rq_tailsz = rdata->tailsz };
e28bc5b1 1716
f96637be
JP
1717 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
1718 __func__, mid->mid, mid->mid_state, rdata->result,
1719 rdata->bytes);
e28bc5b1 1720
7c9421e1 1721 switch (mid->mid_state) {
e28bc5b1
JL
1722 case MID_RESPONSE_RECEIVED:
1723 /* result already set, check signature */
38d77c50 1724 if (server->sign) {
985e4ff0
SF
1725 int rc = 0;
1726
bf5ea0e2 1727 rc = cifs_verify_signature(&rqst, server,
0124cc45 1728 mid->sequence_number);
985e4ff0 1729 if (rc)
f96637be
JP
1730 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
1731 rc);
e28bc5b1
JL
1732 }
1733 /* FIXME: should this be counted toward the initiating task? */
34a54d61
PS
1734 task_io_account_read(rdata->got_bytes);
1735 cifs_stats_bytes_read(tcon, rdata->got_bytes);
e28bc5b1
JL
1736 break;
1737 case MID_REQUEST_SUBMITTED:
1738 case MID_RETRY_NEEDED:
1739 rdata->result = -EAGAIN;
d913ed17
PS
1740 if (server->sign && rdata->got_bytes)
1741 /* reset bytes number since we can not check a sign */
1742 rdata->got_bytes = 0;
1743 /* FIXME: should this be counted toward the initiating task? */
1744 task_io_account_read(rdata->got_bytes);
1745 cifs_stats_bytes_read(tcon, rdata->got_bytes);
e28bc5b1
JL
1746 break;
1747 default:
1748 rdata->result = -EIO;
1749 }
1750
da472fc8 1751 queue_work(cifsiod_wq, &rdata->work);
e28bc5b1 1752 DeleteMidQEntry(mid);
a891f0f8 1753 add_credits(server, 1, 0);
e28bc5b1
JL
1754}
1755
1756/* cifs_async_readv - send an async write, and set up mid to handle result */
1757int
1758cifs_async_readv(struct cifs_readdata *rdata)
1759{
1760 int rc;
1761 READ_REQ *smb = NULL;
1762 int wct;
1763 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
738f9de5
PS
1764 struct smb_rqst rqst = { .rq_iov = rdata->iov,
1765 .rq_nvec = 2 };
e28bc5b1 1766
f96637be
JP
1767 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
1768 __func__, rdata->offset, rdata->bytes);
e28bc5b1
JL
1769
1770 if (tcon->ses->capabilities & CAP_LARGE_FILES)
1771 wct = 12;
1772 else {
1773 wct = 10; /* old style read */
1774 if ((rdata->offset >> 32) > 0) {
1775 /* can not handle this big offset for old */
1776 return -EIO;
1777 }
1778 }
1779
1780 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **)&smb);
1781 if (rc)
1782 return rc;
1783
1784 smb->hdr.Pid = cpu_to_le16((__u16)rdata->pid);
1785 smb->hdr.PidHigh = cpu_to_le16((__u16)(rdata->pid >> 16));
1786
1787 smb->AndXCommand = 0xFF; /* none */
4b4de76e 1788 smb->Fid = rdata->cfile->fid.netfid;
e28bc5b1
JL
1789 smb->OffsetLow = cpu_to_le32(rdata->offset & 0xFFFFFFFF);
1790 if (wct == 12)
1791 smb->OffsetHigh = cpu_to_le32(rdata->offset >> 32);
1792 smb->Remaining = 0;
1793 smb->MaxCount = cpu_to_le16(rdata->bytes & 0xFFFF);
1794 smb->MaxCountHigh = cpu_to_le32(rdata->bytes >> 16);
1795 if (wct == 12)
1796 smb->ByteCount = 0;
1797 else {
1798 /* old style read */
1799 struct smb_com_readx_req *smbr =
1800 (struct smb_com_readx_req *)smb;
1801 smbr->ByteCount = 0;
1802 }
1803
1804 /* 4 for RFC1001 length + 1 for BCC */
738f9de5
PS
1805 rdata->iov[0].iov_base = smb;
1806 rdata->iov[0].iov_len = 4;
1807 rdata->iov[1].iov_base = (char *)smb + 4;
1808 rdata->iov[1].iov_len = get_rfc1002_length(smb);
e28bc5b1 1809
6993f74a 1810 kref_get(&rdata->refcount);
fec344e3 1811 rc = cifs_call_async(tcon->ses->server, &rqst, cifs_readv_receive,
9b7c18a2 1812 cifs_readv_callback, NULL, rdata, 0);
e28bc5b1
JL
1813
1814 if (rc == 0)
44c58186 1815 cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
6993f74a
JL
1816 else
1817 kref_put(&rdata->refcount, cifs_readdata_release);
e28bc5b1
JL
1818
1819 cifs_small_buf_release(smb);
1820 return rc;
1821}
1822
1da177e4 1823int
6d5786a3
PS
1824CIFSSMBRead(const unsigned int xid, struct cifs_io_parms *io_parms,
1825 unsigned int *nbytes, char **buf, int *pbuf_type)
1da177e4
LT
1826{
1827 int rc = -EACCES;
1828 READ_REQ *pSMB = NULL;
1829 READ_RSP *pSMBr = NULL;
1830 char *pReadData = NULL;
bfa0d75a 1831 int wct;
ec637e3f
SF
1832 int resp_buf_type = 0;
1833 struct kvec iov[1];
da502f7d 1834 struct kvec rsp_iov;
d4ffff1f
PS
1835 __u32 pid = io_parms->pid;
1836 __u16 netfid = io_parms->netfid;
1837 __u64 offset = io_parms->offset;
96daf2b0 1838 struct cifs_tcon *tcon = io_parms->tcon;
d4ffff1f 1839 unsigned int count = io_parms->length;
1da177e4 1840
f96637be 1841 cifs_dbg(FYI, "Reading %d bytes on fid %d\n", count, netfid);
790fe579 1842 if (tcon->ses->capabilities & CAP_LARGE_FILES)
bfa0d75a 1843 wct = 12;
4c3130ef 1844 else {
bfa0d75a 1845 wct = 10; /* old style read */
d4ffff1f 1846 if ((offset >> 32) > 0) {
4c3130ef
SF
1847 /* can not handle this big offset for old */
1848 return -EIO;
1849 }
1850 }
1da177e4
LT
1851
1852 *nbytes = 0;
ec637e3f 1853 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **) &pSMB);
1da177e4
LT
1854 if (rc)
1855 return rc;
1856
d4ffff1f
PS
1857 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1858 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1859
1da177e4
LT
1860 /* tcon and ses pointer are checked in smb_init */
1861 if (tcon->ses->server == NULL)
1862 return -ECONNABORTED;
1863
ec637e3f 1864 pSMB->AndXCommand = 0xFF; /* none */
1da177e4 1865 pSMB->Fid = netfid;
d4ffff1f 1866 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
790fe579 1867 if (wct == 12)
d4ffff1f 1868 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
bfa0d75a 1869
1da177e4
LT
1870 pSMB->Remaining = 0;
1871 pSMB->MaxCount = cpu_to_le16(count & 0xFFFF);
1872 pSMB->MaxCountHigh = cpu_to_le32(count >> 16);
790fe579 1873 if (wct == 12)
bfa0d75a
SF
1874 pSMB->ByteCount = 0; /* no need to do le conversion since 0 */
1875 else {
1876 /* old style read */
50c2f753 1877 struct smb_com_readx_req *pSMBW =
bfa0d75a 1878 (struct smb_com_readx_req *)pSMB;
ec637e3f 1879 pSMBW->ByteCount = 0;
bfa0d75a 1880 }
ec637e3f
SF
1881
1882 iov[0].iov_base = (char *)pSMB;
be8e3b00 1883 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
da502f7d
PS
1884 rc = SendReceive2(xid, tcon->ses, iov, 1, &resp_buf_type,
1885 CIFS_LOG_ERROR, &rsp_iov);
1886 cifs_small_buf_release(pSMB);
44c58186 1887 cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
da502f7d 1888 pSMBr = (READ_RSP *)rsp_iov.iov_base;
1da177e4 1889 if (rc) {
f96637be 1890 cifs_dbg(VFS, "Send error in read = %d\n", rc);
1da177e4
LT
1891 } else {
1892 int data_length = le16_to_cpu(pSMBr->DataLengthHigh);
1893 data_length = data_length << 16;
1894 data_length += le16_to_cpu(pSMBr->DataLength);
1895 *nbytes = data_length;
1896
1897 /*check that DataLength would not go beyond end of SMB */
ec637e3f 1898 if ((data_length > CIFSMaxBufSize)
1da177e4 1899 || (data_length > count)) {
f96637be 1900 cifs_dbg(FYI, "bad length %d for count %d\n",
b6b38f70 1901 data_length, count);
1da177e4
LT
1902 rc = -EIO;
1903 *nbytes = 0;
1904 } else {
ec637e3f 1905 pReadData = (char *) (&pSMBr->hdr.Protocol) +
26f57364
SF
1906 le16_to_cpu(pSMBr->DataOffset);
1907/* if (rc = copy_to_user(buf, pReadData, data_length)) {
f96637be 1908 cifs_dbg(VFS, "Faulting on read rc = %d\n",rc);
50c2f753 1909 rc = -EFAULT;
26f57364 1910 }*/ /* can not use copy_to_user when using page cache*/
790fe579 1911 if (*buf)
50c2f753 1912 memcpy(*buf, pReadData, data_length);
1da177e4
LT
1913 }
1914 }
1da177e4 1915
790fe579 1916 if (*buf) {
da502f7d 1917 free_rsp_buf(resp_buf_type, rsp_iov.iov_base);
790fe579 1918 } else if (resp_buf_type != CIFS_NO_BUFFER) {
50c2f753 1919 /* return buffer to caller to free */
da502f7d 1920 *buf = rsp_iov.iov_base;
790fe579 1921 if (resp_buf_type == CIFS_SMALL_BUFFER)
ec637e3f 1922 *pbuf_type = CIFS_SMALL_BUFFER;
790fe579 1923 else if (resp_buf_type == CIFS_LARGE_BUFFER)
ec637e3f 1924 *pbuf_type = CIFS_LARGE_BUFFER;
6cec2aed 1925 } /* else no valid buffer on return - leave as null */
ec637e3f
SF
1926
1927 /* Note: On -EAGAIN error only caller can retry on handle based calls
1da177e4
LT
1928 since file handle passed in no longer valid */
1929 return rc;
1930}
1931
ec637e3f 1932
1da177e4 1933int
6d5786a3 1934CIFSSMBWrite(const unsigned int xid, struct cifs_io_parms *io_parms,
dbbab325 1935 unsigned int *nbytes, const char *buf)
1da177e4
LT
1936{
1937 int rc = -EACCES;
1938 WRITE_REQ *pSMB = NULL;
1939 WRITE_RSP *pSMBr = NULL;
1c955187 1940 int bytes_returned, wct;
1da177e4
LT
1941 __u32 bytes_sent;
1942 __u16 byte_count;
fa2989f4
PS
1943 __u32 pid = io_parms->pid;
1944 __u16 netfid = io_parms->netfid;
1945 __u64 offset = io_parms->offset;
96daf2b0 1946 struct cifs_tcon *tcon = io_parms->tcon;
fa2989f4 1947 unsigned int count = io_parms->length;
1da177e4 1948
a24e2d7d
SF
1949 *nbytes = 0;
1950
f96637be 1951 /* cifs_dbg(FYI, "write at %lld %d bytes\n", offset, count);*/
790fe579 1952 if (tcon->ses == NULL)
1c955187
SF
1953 return -ECONNABORTED;
1954
790fe579 1955 if (tcon->ses->capabilities & CAP_LARGE_FILES)
1c955187 1956 wct = 14;
4c3130ef 1957 else {
1c955187 1958 wct = 12;
4c3130ef
SF
1959 if ((offset >> 32) > 0) {
1960 /* can not handle big offset for old srv */
1961 return -EIO;
1962 }
1963 }
1c955187
SF
1964
1965 rc = smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB,
1da177e4
LT
1966 (void **) &pSMBr);
1967 if (rc)
1968 return rc;
fa2989f4
PS
1969
1970 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1971 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1972
1da177e4
LT
1973 /* tcon and ses pointer are checked in smb_init */
1974 if (tcon->ses->server == NULL)
1975 return -ECONNABORTED;
1976
1977 pSMB->AndXCommand = 0xFF; /* none */
1978 pSMB->Fid = netfid;
1979 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
790fe579 1980 if (wct == 14)
1c955187 1981 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
50c2f753 1982
1da177e4
LT
1983 pSMB->Reserved = 0xFFFFFFFF;
1984 pSMB->WriteMode = 0;
1985 pSMB->Remaining = 0;
1986
50c2f753 1987 /* Can increase buffer size if buffer is big enough in some cases ie we
1da177e4
LT
1988 can send more if LARGE_WRITE_X capability returned by the server and if
1989 our buffer is big enough or if we convert to iovecs on socket writes
1990 and eliminate the copy to the CIFS buffer */
790fe579 1991 if (tcon->ses->capabilities & CAP_LARGE_WRITE_X) {
1da177e4
LT
1992 bytes_sent = min_t(const unsigned int, CIFSMaxBufSize, count);
1993 } else {
1994 bytes_sent = (tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE)
1995 & ~0xFF;
1996 }
1997
1998 if (bytes_sent > count)
1999 bytes_sent = count;
2000 pSMB->DataOffset =
50c2f753 2001 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
790fe579 2002 if (buf)
61e74801 2003 memcpy(pSMB->Data, buf, bytes_sent);
dbbab325 2004 else if (count != 0) {
1da177e4
LT
2005 /* No buffer */
2006 cifs_buf_release(pSMB);
2007 return -EINVAL;
e30dcf3a 2008 } /* else setting file size with write of zero bytes */
790fe579 2009 if (wct == 14)
e30dcf3a 2010 byte_count = bytes_sent + 1; /* pad */
ad7a2926 2011 else /* wct == 12 */
e30dcf3a 2012 byte_count = bytes_sent + 5; /* bigger pad, smaller smb hdr */
ad7a2926 2013
1da177e4
LT
2014 pSMB->DataLengthLow = cpu_to_le16(bytes_sent & 0xFFFF);
2015 pSMB->DataLengthHigh = cpu_to_le16(bytes_sent >> 16);
be8e3b00 2016 inc_rfc1001_len(pSMB, byte_count);
1c955187 2017
790fe579 2018 if (wct == 14)
1c955187 2019 pSMB->ByteCount = cpu_to_le16(byte_count);
50c2f753
SF
2020 else { /* old style write has byte count 4 bytes earlier
2021 so 4 bytes pad */
2022 struct smb_com_writex_req *pSMBW =
1c955187
SF
2023 (struct smb_com_writex_req *)pSMB;
2024 pSMBW->ByteCount = cpu_to_le16(byte_count);
2025 }
1da177e4
LT
2026
2027 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
dbbab325 2028 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
44c58186 2029 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
1da177e4 2030 if (rc) {
f96637be 2031 cifs_dbg(FYI, "Send error in write = %d\n", rc);
1da177e4
LT
2032 } else {
2033 *nbytes = le16_to_cpu(pSMBr->CountHigh);
2034 *nbytes = (*nbytes) << 16;
2035 *nbytes += le16_to_cpu(pSMBr->Count);
6513a81e
SJ
2036
2037 /*
2038 * Mask off high 16 bits when bytes written as returned by the
2039 * server is greater than bytes requested by the client. Some
2040 * OS/2 servers are known to set incorrect CountHigh values.
2041 */
2042 if (*nbytes > count)
2043 *nbytes &= 0xFFFF;
1da177e4
LT
2044 }
2045
2046 cifs_buf_release(pSMB);
2047
50c2f753 2048 /* Note: On -EAGAIN error only caller can retry on handle based calls
1da177e4
LT
2049 since file handle passed in no longer valid */
2050
2051 return rc;
2052}
2053
c28c89fc
JL
2054void
2055cifs_writedata_release(struct kref *refcount)
2056{
2057 struct cifs_writedata *wdata = container_of(refcount,
2058 struct cifs_writedata, refcount);
db223a59
LL
2059#ifdef CONFIG_CIFS_SMB_DIRECT
2060 if (wdata->mr) {
2061 smbd_deregister_mr(wdata->mr);
2062 wdata->mr = NULL;
2063 }
2064#endif
c28c89fc
JL
2065
2066 if (wdata->cfile)
2067 cifsFileInfo_put(wdata->cfile);
2068
8e7360f6 2069 kvfree(wdata->pages);
c28c89fc
JL
2070 kfree(wdata);
2071}
2072
2073/*
2074 * Write failed with a retryable error. Resend the write request. It's also
2075 * possible that the page was redirtied so re-clean the page.
2076 */
2077static void
2078cifs_writev_requeue(struct cifs_writedata *wdata)
2079{
7f6c5008 2080 int i, rc = 0;
2b0143b5 2081 struct inode *inode = d_inode(wdata->cfile->dentry);
c9de5c80 2082 struct TCP_Server_Info *server;
7f6c5008 2083 unsigned int rest_len;
c28c89fc 2084
7f6c5008
PS
2085 server = tlink_tcon(wdata->cfile->tlink)->ses->server;
2086 i = 0;
2087 rest_len = wdata->bytes;
c28c89fc 2088 do {
7f6c5008
PS
2089 struct cifs_writedata *wdata2;
2090 unsigned int j, nr_pages, wsize, tailsz, cur_len;
2091
2092 wsize = server->ops->wp_retry_size(inode);
2093 if (wsize < rest_len) {
09cbfeaf 2094 nr_pages = wsize / PAGE_SIZE;
7f6c5008
PS
2095 if (!nr_pages) {
2096 rc = -ENOTSUPP;
2097 break;
2098 }
09cbfeaf
KS
2099 cur_len = nr_pages * PAGE_SIZE;
2100 tailsz = PAGE_SIZE;
7f6c5008 2101 } else {
09cbfeaf 2102 nr_pages = DIV_ROUND_UP(rest_len, PAGE_SIZE);
7f6c5008 2103 cur_len = rest_len;
09cbfeaf 2104 tailsz = rest_len - (nr_pages - 1) * PAGE_SIZE;
7f6c5008 2105 }
c28c89fc 2106
7f6c5008
PS
2107 wdata2 = cifs_writedata_alloc(nr_pages, cifs_writev_complete);
2108 if (!wdata2) {
2109 rc = -ENOMEM;
2110 break;
c51bb0ea 2111 }
7f6c5008
PS
2112
2113 for (j = 0; j < nr_pages; j++) {
2114 wdata2->pages[j] = wdata->pages[i + j];
2115 lock_page(wdata2->pages[j]);
2116 clear_page_dirty_for_io(wdata2->pages[j]);
2117 }
2118
2119 wdata2->sync_mode = wdata->sync_mode;
2120 wdata2->nr_pages = nr_pages;
2121 wdata2->offset = page_offset(wdata2->pages[0]);
09cbfeaf 2122 wdata2->pagesz = PAGE_SIZE;
7f6c5008
PS
2123 wdata2->tailsz = tailsz;
2124 wdata2->bytes = cur_len;
2125
2126 wdata2->cfile = find_writable_file(CIFS_I(inode), false);
2127 if (!wdata2->cfile) {
165df9a0 2128 cifs_dbg(VFS, "No writable handle to retry writepages\n");
7f6c5008 2129 rc = -EBADF;
165df9a0
PS
2130 } else {
2131 wdata2->pid = wdata2->cfile->pid;
2132 rc = server->ops->async_writev(wdata2,
2133 cifs_writedata_release);
7f6c5008 2134 }
7f6c5008
PS
2135
2136 for (j = 0; j < nr_pages; j++) {
2137 unlock_page(wdata2->pages[j]);
9a66396f 2138 if (rc != 0 && !is_retryable_error(rc)) {
7f6c5008
PS
2139 SetPageError(wdata2->pages[j]);
2140 end_page_writeback(wdata2->pages[j]);
09cbfeaf 2141 put_page(wdata2->pages[j]);
7f6c5008
PS
2142 }
2143 }
2144
2145 if (rc) {
2146 kref_put(&wdata2->refcount, cifs_writedata_release);
9a66396f 2147 if (is_retryable_error(rc))
7f6c5008 2148 continue;
165df9a0 2149 i += nr_pages;
7f6c5008
PS
2150 break;
2151 }
2152
2153 rest_len -= cur_len;
2154 i += nr_pages;
2155 } while (i < wdata->nr_pages);
c28c89fc 2156
165df9a0
PS
2157 /* cleanup remaining pages from the original wdata */
2158 for (; i < wdata->nr_pages; i++) {
2159 SetPageError(wdata->pages[i]);
2160 end_page_writeback(wdata->pages[i]);
2161 put_page(wdata->pages[i]);
2162 }
2163
9a66396f
PS
2164 if (rc != 0 && !is_retryable_error(rc))
2165 mapping_set_error(inode->i_mapping, rc);
c28c89fc
JL
2166 kref_put(&wdata->refcount, cifs_writedata_release);
2167}
2168
c2e87640 2169void
c28c89fc
JL
2170cifs_writev_complete(struct work_struct *work)
2171{
2172 struct cifs_writedata *wdata = container_of(work,
2173 struct cifs_writedata, work);
2b0143b5 2174 struct inode *inode = d_inode(wdata->cfile->dentry);
c28c89fc
JL
2175 int i = 0;
2176
2177 if (wdata->result == 0) {
597b027f 2178 spin_lock(&inode->i_lock);
c28c89fc 2179 cifs_update_eof(CIFS_I(inode), wdata->offset, wdata->bytes);
597b027f 2180 spin_unlock(&inode->i_lock);
c28c89fc
JL
2181 cifs_stats_bytes_written(tlink_tcon(wdata->cfile->tlink),
2182 wdata->bytes);
2183 } else if (wdata->sync_mode == WB_SYNC_ALL && wdata->result == -EAGAIN)
2184 return cifs_writev_requeue(wdata);
2185
2186 for (i = 0; i < wdata->nr_pages; i++) {
2187 struct page *page = wdata->pages[i];
2188 if (wdata->result == -EAGAIN)
2189 __set_page_dirty_nobuffers(page);
2190 else if (wdata->result < 0)
2191 SetPageError(page);
2192 end_page_writeback(page);
09cbfeaf 2193 put_page(page);
c28c89fc
JL
2194 }
2195 if (wdata->result != -EAGAIN)
2196 mapping_set_error(inode->i_mapping, wdata->result);
2197 kref_put(&wdata->refcount, cifs_writedata_release);
2198}
2199
2200struct cifs_writedata *
c2e87640 2201cifs_writedata_alloc(unsigned int nr_pages, work_func_t complete)
8e7360f6
LL
2202{
2203 struct page **pages =
6396bb22 2204 kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
8e7360f6
LL
2205 if (pages)
2206 return cifs_writedata_direct_alloc(pages, complete);
2207
2208 return NULL;
2209}
2210
2211struct cifs_writedata *
2212cifs_writedata_direct_alloc(struct page **pages, work_func_t complete)
c28c89fc
JL
2213{
2214 struct cifs_writedata *wdata;
2215
8e7360f6 2216 wdata = kzalloc(sizeof(*wdata), GFP_NOFS);
c28c89fc 2217 if (wdata != NULL) {
8e7360f6 2218 wdata->pages = pages;
c28c89fc 2219 kref_init(&wdata->refcount);
da82f7e7
JL
2220 INIT_LIST_HEAD(&wdata->list);
2221 init_completion(&wdata->done);
2222 INIT_WORK(&wdata->work, complete);
c28c89fc
JL
2223 }
2224 return wdata;
2225}
2226
2227/*
7c9421e1 2228 * Check the mid_state and signature on received buffer (if any), and queue the
c28c89fc
JL
2229 * workqueue completion task.
2230 */
2231static void
2232cifs_writev_callback(struct mid_q_entry *mid)
2233{
2234 struct cifs_writedata *wdata = mid->callback_data;
96daf2b0 2235 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
c28c89fc
JL
2236 unsigned int written;
2237 WRITE_RSP *smb = (WRITE_RSP *)mid->resp_buf;
2238
7c9421e1 2239 switch (mid->mid_state) {
c28c89fc
JL
2240 case MID_RESPONSE_RECEIVED:
2241 wdata->result = cifs_check_receive(mid, tcon->ses->server, 0);
2242 if (wdata->result != 0)
2243 break;
2244
2245 written = le16_to_cpu(smb->CountHigh);
2246 written <<= 16;
2247 written += le16_to_cpu(smb->Count);
2248 /*
2249 * Mask off high 16 bits when bytes written as returned
2250 * by the server is greater than bytes requested by the
2251 * client. OS/2 servers are known to set incorrect
2252 * CountHigh values.
2253 */
2254 if (written > wdata->bytes)
2255 written &= 0xFFFF;
2256
2257 if (written < wdata->bytes)
2258 wdata->result = -ENOSPC;
2259 else
2260 wdata->bytes = written;
2261 break;
2262 case MID_REQUEST_SUBMITTED:
2263 case MID_RETRY_NEEDED:
2264 wdata->result = -EAGAIN;
2265 break;
2266 default:
2267 wdata->result = -EIO;
2268 break;
2269 }
2270
da472fc8 2271 queue_work(cifsiod_wq, &wdata->work);
c28c89fc 2272 DeleteMidQEntry(mid);
a891f0f8 2273 add_credits(tcon->ses->server, 1, 0);
c28c89fc
JL
2274}
2275
2276/* cifs_async_writev - send an async write, and set up mid to handle result */
2277int
4a5c80d7
SF
2278cifs_async_writev(struct cifs_writedata *wdata,
2279 void (*release)(struct kref *kref))
c28c89fc 2280{
eddb079d 2281 int rc = -EACCES;
c28c89fc
JL
2282 WRITE_REQ *smb = NULL;
2283 int wct;
96daf2b0 2284 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
738f9de5 2285 struct kvec iov[2];
fec344e3 2286 struct smb_rqst rqst = { };
c28c89fc
JL
2287
2288 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
2289 wct = 14;
2290 } else {
2291 wct = 12;
2292 if (wdata->offset >> 32 > 0) {
2293 /* can not handle big offset for old srv */
2294 return -EIO;
2295 }
2296 }
2297
2298 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **)&smb);
2299 if (rc)
2300 goto async_writev_out;
2301
fe5f5d2e
JL
2302 smb->hdr.Pid = cpu_to_le16((__u16)wdata->pid);
2303 smb->hdr.PidHigh = cpu_to_le16((__u16)(wdata->pid >> 16));
fa2989f4 2304
c28c89fc 2305 smb->AndXCommand = 0xFF; /* none */
4b4de76e 2306 smb->Fid = wdata->cfile->fid.netfid;
c28c89fc
JL
2307 smb->OffsetLow = cpu_to_le32(wdata->offset & 0xFFFFFFFF);
2308 if (wct == 14)
2309 smb->OffsetHigh = cpu_to_le32(wdata->offset >> 32);
2310 smb->Reserved = 0xFFFFFFFF;
2311 smb->WriteMode = 0;
2312 smb->Remaining = 0;
2313
2314 smb->DataOffset =
2315 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
2316
2317 /* 4 for RFC1001 length + 1 for BCC */
738f9de5
PS
2318 iov[0].iov_len = 4;
2319 iov[0].iov_base = smb;
2320 iov[1].iov_len = get_rfc1002_length(smb) + 1;
2321 iov[1].iov_base = (char *)smb + 4;
c28c89fc 2322
738f9de5
PS
2323 rqst.rq_iov = iov;
2324 rqst.rq_nvec = 2;
eddb079d 2325 rqst.rq_pages = wdata->pages;
6d3adb23 2326 rqst.rq_offset = wdata->page_offset;
eddb079d
JL
2327 rqst.rq_npages = wdata->nr_pages;
2328 rqst.rq_pagesz = wdata->pagesz;
2329 rqst.rq_tailsz = wdata->tailsz;
c28c89fc 2330
f96637be
JP
2331 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2332 wdata->offset, wdata->bytes);
c28c89fc
JL
2333
2334 smb->DataLengthLow = cpu_to_le16(wdata->bytes & 0xFFFF);
2335 smb->DataLengthHigh = cpu_to_le16(wdata->bytes >> 16);
2336
2337 if (wct == 14) {
2338 inc_rfc1001_len(&smb->hdr, wdata->bytes + 1);
2339 put_bcc(wdata->bytes + 1, &smb->hdr);
2340 } else {
2341 /* wct == 12 */
2342 struct smb_com_writex_req *smbw =
2343 (struct smb_com_writex_req *)smb;
2344 inc_rfc1001_len(&smbw->hdr, wdata->bytes + 5);
2345 put_bcc(wdata->bytes + 5, &smbw->hdr);
738f9de5 2346 iov[1].iov_len += 4; /* pad bigger by four bytes */
c28c89fc
JL
2347 }
2348
2349 kref_get(&wdata->refcount);
fec344e3 2350 rc = cifs_call_async(tcon->ses->server, &rqst, NULL,
9b7c18a2 2351 cifs_writev_callback, NULL, wdata, 0);
c28c89fc
JL
2352
2353 if (rc == 0)
44c58186 2354 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
c28c89fc 2355 else
4a5c80d7 2356 kref_put(&wdata->refcount, release);
c28c89fc 2357
c28c89fc
JL
2358async_writev_out:
2359 cifs_small_buf_release(smb);
c28c89fc
JL
2360 return rc;
2361}
2362
d6e04ae6 2363int
6d5786a3 2364CIFSSMBWrite2(const unsigned int xid, struct cifs_io_parms *io_parms,
ba9ad725 2365 unsigned int *nbytes, struct kvec *iov, int n_vec)
1da177e4
LT
2366{
2367 int rc = -EACCES;
2368 WRITE_REQ *pSMB = NULL;
ec637e3f 2369 int wct;
d6e04ae6 2370 int smb_hdr_len;
ec637e3f 2371 int resp_buf_type = 0;
fa2989f4
PS
2372 __u32 pid = io_parms->pid;
2373 __u16 netfid = io_parms->netfid;
2374 __u64 offset = io_parms->offset;
96daf2b0 2375 struct cifs_tcon *tcon = io_parms->tcon;
fa2989f4 2376 unsigned int count = io_parms->length;
da502f7d 2377 struct kvec rsp_iov;
1da177e4 2378
fbec9ab9
JL
2379 *nbytes = 0;
2380
f96637be 2381 cifs_dbg(FYI, "write2 at %lld %d bytes\n", (long long)offset, count);
ff7feac9 2382
4c3130ef 2383 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
8cc64c6e 2384 wct = 14;
4c3130ef 2385 } else {
8cc64c6e 2386 wct = 12;
4c3130ef
SF
2387 if ((offset >> 32) > 0) {
2388 /* can not handle big offset for old srv */
2389 return -EIO;
2390 }
2391 }
8cc64c6e 2392 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB);
1da177e4
LT
2393 if (rc)
2394 return rc;
fa2989f4
PS
2395
2396 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
2397 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
2398
1da177e4
LT
2399 /* tcon and ses pointer are checked in smb_init */
2400 if (tcon->ses->server == NULL)
2401 return -ECONNABORTED;
2402
d6e04ae6 2403 pSMB->AndXCommand = 0xFF; /* none */
1da177e4
LT
2404 pSMB->Fid = netfid;
2405 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
790fe579 2406 if (wct == 14)
8cc64c6e 2407 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
1da177e4
LT
2408 pSMB->Reserved = 0xFFFFFFFF;
2409 pSMB->WriteMode = 0;
2410 pSMB->Remaining = 0;
d6e04ae6 2411
1da177e4 2412 pSMB->DataOffset =
50c2f753 2413 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
1da177e4 2414
3e84469d
SF
2415 pSMB->DataLengthLow = cpu_to_le16(count & 0xFFFF);
2416 pSMB->DataLengthHigh = cpu_to_le16(count >> 16);
be8e3b00
SF
2417 /* header + 1 byte pad */
2418 smb_hdr_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 1;
790fe579 2419 if (wct == 14)
be8e3b00 2420 inc_rfc1001_len(pSMB, count + 1);
8cc64c6e 2421 else /* wct == 12 */
be8e3b00 2422 inc_rfc1001_len(pSMB, count + 5); /* smb data starts later */
790fe579 2423 if (wct == 14)
8cc64c6e
SF
2424 pSMB->ByteCount = cpu_to_le16(count + 1);
2425 else /* wct == 12 */ /* bigger pad, smaller smb hdr, keep offset ok */ {
50c2f753 2426 struct smb_com_writex_req *pSMBW =
8cc64c6e
SF
2427 (struct smb_com_writex_req *)pSMB;
2428 pSMBW->ByteCount = cpu_to_le16(count + 5);
2429 }
3e84469d 2430 iov[0].iov_base = pSMB;
790fe579 2431 if (wct == 14)
ec637e3f
SF
2432 iov[0].iov_len = smb_hdr_len + 4;
2433 else /* wct == 12 pad bigger by four bytes */
2434 iov[0].iov_len = smb_hdr_len + 8;
50c2f753 2435
da502f7d
PS
2436 rc = SendReceive2(xid, tcon->ses, iov, n_vec + 1, &resp_buf_type, 0,
2437 &rsp_iov);
2438 cifs_small_buf_release(pSMB);
44c58186 2439 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
1da177e4 2440 if (rc) {
f96637be 2441 cifs_dbg(FYI, "Send error Write2 = %d\n", rc);
790fe579 2442 } else if (resp_buf_type == 0) {
ec637e3f
SF
2443 /* presumably this can not happen, but best to be safe */
2444 rc = -EIO;
d6e04ae6 2445 } else {
da502f7d 2446 WRITE_RSP *pSMBr = (WRITE_RSP *)rsp_iov.iov_base;
d6e04ae6
SF
2447 *nbytes = le16_to_cpu(pSMBr->CountHigh);
2448 *nbytes = (*nbytes) << 16;
2449 *nbytes += le16_to_cpu(pSMBr->Count);
6513a81e
SJ
2450
2451 /*
2452 * Mask off high 16 bits when bytes written as returned by the
2453 * server is greater than bytes requested by the client. OS/2
2454 * servers are known to set incorrect CountHigh values.
2455 */
2456 if (*nbytes > count)
2457 *nbytes &= 0xFFFF;
50c2f753 2458 }
1da177e4 2459
da502f7d 2460 free_rsp_buf(resp_buf_type, rsp_iov.iov_base);
1da177e4 2461
50c2f753 2462 /* Note: On -EAGAIN error only caller can retry on handle based calls
1da177e4
LT
2463 since file handle passed in no longer valid */
2464
2465 return rc;
2466}
d6e04ae6 2467
6d5786a3
PS
2468int cifs_lockv(const unsigned int xid, struct cifs_tcon *tcon,
2469 const __u16 netfid, const __u8 lock_type, const __u32 num_unlock,
9ee305b7
PS
2470 const __u32 num_lock, LOCKING_ANDX_RANGE *buf)
2471{
2472 int rc = 0;
2473 LOCK_REQ *pSMB = NULL;
2474 struct kvec iov[2];
da502f7d 2475 struct kvec rsp_iov;
9ee305b7
PS
2476 int resp_buf_type;
2477 __u16 count;
2478
f96637be
JP
2479 cifs_dbg(FYI, "cifs_lockv num lock %d num unlock %d\n",
2480 num_lock, num_unlock);
9ee305b7
PS
2481
2482 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2483 if (rc)
2484 return rc;
2485
2486 pSMB->Timeout = 0;
2487 pSMB->NumberOfLocks = cpu_to_le16(num_lock);
2488 pSMB->NumberOfUnlocks = cpu_to_le16(num_unlock);
2489 pSMB->LockType = lock_type;
2490 pSMB->AndXCommand = 0xFF; /* none */
2491 pSMB->Fid = netfid; /* netfid stays le */
2492
2493 count = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2494 inc_rfc1001_len(pSMB, count);
2495 pSMB->ByteCount = cpu_to_le16(count);
2496
2497 iov[0].iov_base = (char *)pSMB;
2498 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4 -
2499 (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2500 iov[1].iov_base = (char *)buf;
2501 iov[1].iov_len = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2502
44c58186 2503 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
da502f7d
PS
2504 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP,
2505 &rsp_iov);
2506 cifs_small_buf_release(pSMB);
9ee305b7 2507 if (rc)
f96637be 2508 cifs_dbg(FYI, "Send error in cifs_lockv = %d\n", rc);
9ee305b7
PS
2509
2510 return rc;
2511}
d6e04ae6 2512
1da177e4 2513int
6d5786a3 2514CIFSSMBLock(const unsigned int xid, struct cifs_tcon *tcon,
03776f45 2515 const __u16 smb_file_id, const __u32 netpid, const __u64 len,
1da177e4 2516 const __u64 offset, const __u32 numUnlock,
12fed00d
PS
2517 const __u32 numLock, const __u8 lockType,
2518 const bool waitFlag, const __u8 oplock_level)
1da177e4
LT
2519{
2520 int rc = 0;
2521 LOCK_REQ *pSMB = NULL;
aaa9bbe0 2522/* LOCK_RSP *pSMBr = NULL; */ /* No response data other than rc to parse */
1da177e4 2523 int bytes_returned;
a891f0f8 2524 int flags = 0;
1da177e4
LT
2525 __u16 count;
2526
f96637be
JP
2527 cifs_dbg(FYI, "CIFSSMBLock timeout %d numLock %d\n",
2528 (int)waitFlag, numLock);
46810cbf
SF
2529 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2530
1da177e4
LT
2531 if (rc)
2532 return rc;
2533
790fe579 2534 if (lockType == LOCKING_ANDX_OPLOCK_RELEASE) {
a891f0f8
PS
2535 /* no response expected */
2536 flags = CIFS_ASYNC_OP | CIFS_OBREAK_OP;
1da177e4 2537 pSMB->Timeout = 0;
4b18f2a9 2538 } else if (waitFlag) {
a891f0f8 2539 flags = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
1da177e4
LT
2540 pSMB->Timeout = cpu_to_le32(-1);/* blocking - do not time out */
2541 } else {
2542 pSMB->Timeout = 0;
2543 }
2544
2545 pSMB->NumberOfLocks = cpu_to_le16(numLock);
2546 pSMB->NumberOfUnlocks = cpu_to_le16(numUnlock);
2547 pSMB->LockType = lockType;
12fed00d 2548 pSMB->OplockLevel = oplock_level;
1da177e4
LT
2549 pSMB->AndXCommand = 0xFF; /* none */
2550 pSMB->Fid = smb_file_id; /* netfid stays le */
2551
790fe579 2552 if ((numLock != 0) || (numUnlock != 0)) {
03776f45 2553 pSMB->Locks[0].Pid = cpu_to_le16(netpid);
1da177e4
LT
2554 /* BB where to store pid high? */
2555 pSMB->Locks[0].LengthLow = cpu_to_le32((u32)len);
2556 pSMB->Locks[0].LengthHigh = cpu_to_le32((u32)(len>>32));
2557 pSMB->Locks[0].OffsetLow = cpu_to_le32((u32)offset);
2558 pSMB->Locks[0].OffsetHigh = cpu_to_le32((u32)(offset>>32));
2559 count = sizeof(LOCKING_ANDX_RANGE);
2560 } else {
2561 /* oplock break */
2562 count = 0;
2563 }
be8e3b00 2564 inc_rfc1001_len(pSMB, count);
1da177e4
LT
2565 pSMB->ByteCount = cpu_to_le16(count);
2566
da502f7d 2567 if (waitFlag)
7ee1af76 2568 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
aaa9bbe0 2569 (struct smb_hdr *) pSMB, &bytes_returned);
da502f7d 2570 else
a891f0f8 2571 rc = SendReceiveNoRsp(xid, tcon->ses, (char *)pSMB, flags);
da502f7d 2572 cifs_small_buf_release(pSMB);
44c58186 2573 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
ad7a2926 2574 if (rc)
f96637be 2575 cifs_dbg(FYI, "Send error in Lock = %d\n", rc);
1da177e4 2576
50c2f753 2577 /* Note: On -EAGAIN error only caller can retry on handle based calls
1da177e4
LT
2578 since file handle passed in no longer valid */
2579 return rc;
2580}
2581
08547b03 2582int
6d5786a3 2583CIFSSMBPosixLock(const unsigned int xid, struct cifs_tcon *tcon,
c5fd363d
JL
2584 const __u16 smb_file_id, const __u32 netpid,
2585 const loff_t start_offset, const __u64 len,
2586 struct file_lock *pLockData, const __u16 lock_type,
2587 const bool waitFlag)
08547b03
SF
2588{
2589 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2590 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
08547b03
SF
2591 struct cifs_posix_lock *parm_data;
2592 int rc = 0;
3a5ff61c 2593 int timeout = 0;
08547b03 2594 int bytes_returned = 0;
133672ef 2595 int resp_buf_type = 0;
08547b03 2596 __u16 params, param_offset, offset, byte_count, count;
133672ef 2597 struct kvec iov[1];
da502f7d 2598 struct kvec rsp_iov;
08547b03 2599
f96637be 2600 cifs_dbg(FYI, "Posix Lock\n");
fc94cdb9 2601
08547b03
SF
2602 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
2603
2604 if (rc)
2605 return rc;
2606
2607 pSMBr = (struct smb_com_transaction2_sfi_rsp *)pSMB;
2608
50c2f753 2609 params = 6;
08547b03
SF
2610 pSMB->MaxSetupCount = 0;
2611 pSMB->Reserved = 0;
2612 pSMB->Flags = 0;
08547b03
SF
2613 pSMB->Reserved2 = 0;
2614 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2615 offset = param_offset + params;
2616
08547b03
SF
2617 count = sizeof(struct cifs_posix_lock);
2618 pSMB->MaxParameterCount = cpu_to_le16(2);
ad7a2926 2619 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
08547b03
SF
2620 pSMB->SetupCount = 1;
2621 pSMB->Reserved3 = 0;
c5fd363d 2622 if (pLockData)
08547b03
SF
2623 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
2624 else
2625 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2626 byte_count = 3 /* pad */ + params + count;
2627 pSMB->DataCount = cpu_to_le16(count);
2628 pSMB->ParameterCount = cpu_to_le16(params);
2629 pSMB->TotalDataCount = pSMB->DataCount;
2630 pSMB->TotalParameterCount = pSMB->ParameterCount;
2631 pSMB->ParameterOffset = cpu_to_le16(param_offset);
50c2f753 2632 parm_data = (struct cifs_posix_lock *)
08547b03
SF
2633 (((char *) &pSMB->hdr.Protocol) + offset);
2634
2635 parm_data->lock_type = cpu_to_le16(lock_type);
790fe579 2636 if (waitFlag) {
133672ef 2637 timeout = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
cec6815a 2638 parm_data->lock_flags = cpu_to_le16(1);
3a5ff61c
SF
2639 pSMB->Timeout = cpu_to_le32(-1);
2640 } else
2641 pSMB->Timeout = 0;
2642
4f6bcec9 2643 parm_data->pid = cpu_to_le32(netpid);
c5fd363d 2644 parm_data->start = cpu_to_le64(start_offset);
cec6815a 2645 parm_data->length = cpu_to_le64(len); /* normalize negative numbers */
08547b03
SF
2646
2647 pSMB->DataOffset = cpu_to_le16(offset);
f26282c9 2648 pSMB->Fid = smb_file_id;
08547b03
SF
2649 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_LOCK);
2650 pSMB->Reserved4 = 0;
be8e3b00 2651 inc_rfc1001_len(pSMB, byte_count);
08547b03 2652 pSMB->ByteCount = cpu_to_le16(byte_count);
7ee1af76
JA
2653 if (waitFlag) {
2654 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
2655 (struct smb_hdr *) pSMBr, &bytes_returned);
2656 } else {
133672ef 2657 iov[0].iov_base = (char *)pSMB;
be8e3b00 2658 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
133672ef 2659 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
da502f7d
PS
2660 &resp_buf_type, timeout, &rsp_iov);
2661 pSMBr = (struct smb_com_transaction2_sfi_rsp *)rsp_iov.iov_base;
7ee1af76 2662 }
da502f7d 2663 cifs_small_buf_release(pSMB);
7ee1af76 2664
08547b03 2665 if (rc) {
f96637be 2666 cifs_dbg(FYI, "Send error in Posix Lock = %d\n", rc);
c5fd363d 2667 } else if (pLockData) {
fc94cdb9
SF
2668 /* lock structure can be returned on get */
2669 __u16 data_offset;
2670 __u16 data_count;
2671 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
2672
820a803f 2673 if (rc || get_bcc(&pSMBr->hdr) < sizeof(*parm_data)) {
fc94cdb9
SF
2674 rc = -EIO; /* bad smb */
2675 goto plk_err_exit;
2676 }
fc94cdb9
SF
2677 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
2678 data_count = le16_to_cpu(pSMBr->t2.DataCount);
790fe579 2679 if (data_count < sizeof(struct cifs_posix_lock)) {
fc94cdb9
SF
2680 rc = -EIO;
2681 goto plk_err_exit;
2682 }
2683 parm_data = (struct cifs_posix_lock *)
2684 ((char *)&pSMBr->hdr.Protocol + data_offset);
bc09d141 2685 if (parm_data->lock_type == cpu_to_le16(CIFS_UNLCK))
fc94cdb9 2686 pLockData->fl_type = F_UNLCK;
f05337c6
PS
2687 else {
2688 if (parm_data->lock_type ==
bc09d141 2689 cpu_to_le16(CIFS_RDLCK))
f05337c6
PS
2690 pLockData->fl_type = F_RDLCK;
2691 else if (parm_data->lock_type ==
bc09d141 2692 cpu_to_le16(CIFS_WRLCK))
f05337c6
PS
2693 pLockData->fl_type = F_WRLCK;
2694
5443d130
SF
2695 pLockData->fl_start = le64_to_cpu(parm_data->start);
2696 pLockData->fl_end = pLockData->fl_start +
2697 le64_to_cpu(parm_data->length) - 1;
9d5b86ac 2698 pLockData->fl_pid = -le32_to_cpu(parm_data->pid);
f05337c6 2699 }
08547b03 2700 }
50c2f753 2701
fc94cdb9 2702plk_err_exit:
da502f7d 2703 free_rsp_buf(resp_buf_type, rsp_iov.iov_base);
133672ef 2704
08547b03
SF
2705 /* Note: On -EAGAIN error only caller can retry on handle based calls
2706 since file handle passed in no longer valid */
2707
2708 return rc;
2709}
2710
2711
1da177e4 2712int
6d5786a3 2713CIFSSMBClose(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
1da177e4
LT
2714{
2715 int rc = 0;
2716 CLOSE_REQ *pSMB = NULL;
f96637be 2717 cifs_dbg(FYI, "In CIFSSMBClose\n");
1da177e4
LT
2718
2719/* do not retry on dead session on close */
2720 rc = small_smb_init(SMB_COM_CLOSE, 3, tcon, (void **) &pSMB);
790fe579 2721 if (rc == -EAGAIN)
1da177e4
LT
2722 return 0;
2723 if (rc)
2724 return rc;
2725
1da177e4 2726 pSMB->FileID = (__u16) smb_file_id;
b815f1e5 2727 pSMB->LastWriteTime = 0xFFFFFFFF;
1da177e4 2728 pSMB->ByteCount = 0;
792af7b0 2729 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
da502f7d 2730 cifs_small_buf_release(pSMB);
44c58186 2731 cifs_stats_inc(&tcon->stats.cifs_stats.num_closes);
1da177e4 2732 if (rc) {
790fe579 2733 if (rc != -EINTR) {
1da177e4 2734 /* EINTR is expected when user ctl-c to kill app */
f96637be 2735 cifs_dbg(VFS, "Send error in Close = %d\n", rc);
1da177e4
LT
2736 }
2737 }
2738
1da177e4 2739 /* Since session is dead, file will be closed on server already */
790fe579 2740 if (rc == -EAGAIN)
1da177e4
LT
2741 rc = 0;
2742
2743 return rc;
2744}
2745
b298f223 2746int
6d5786a3 2747CIFSSMBFlush(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
b298f223
SF
2748{
2749 int rc = 0;
2750 FLUSH_REQ *pSMB = NULL;
f96637be 2751 cifs_dbg(FYI, "In CIFSSMBFlush\n");
b298f223
SF
2752
2753 rc = small_smb_init(SMB_COM_FLUSH, 1, tcon, (void **) &pSMB);
2754 if (rc)
2755 return rc;
2756
2757 pSMB->FileID = (__u16) smb_file_id;
2758 pSMB->ByteCount = 0;
792af7b0 2759 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
da502f7d 2760 cifs_small_buf_release(pSMB);
44c58186 2761 cifs_stats_inc(&tcon->stats.cifs_stats.num_flushes);
b298f223 2762 if (rc)
f96637be 2763 cifs_dbg(VFS, "Send error in Flush = %d\n", rc);
b298f223
SF
2764
2765 return rc;
2766}
2767
1da177e4 2768int
6d5786a3 2769CIFSSMBRename(const unsigned int xid, struct cifs_tcon *tcon,
8ceb9843
PS
2770 const char *from_name, const char *to_name,
2771 struct cifs_sb_info *cifs_sb)
1da177e4
LT
2772{
2773 int rc = 0;
2774 RENAME_REQ *pSMB = NULL;
2775 RENAME_RSP *pSMBr = NULL;
2776 int bytes_returned;
2777 int name_len, name_len2;
2778 __u16 count;
2baa2682 2779 int remap = cifs_remap(cifs_sb);
1da177e4 2780
f96637be 2781 cifs_dbg(FYI, "In CIFSSMBRename\n");
1da177e4
LT
2782renameRetry:
2783 rc = smb_init(SMB_COM_RENAME, 1, tcon, (void **) &pSMB,
2784 (void **) &pSMBr);
2785 if (rc)
2786 return rc;
2787
2788 pSMB->BufferFormat = 0x04;
2789 pSMB->SearchAttributes =
2790 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
2791 ATTR_DIRECTORY);
2792
2793 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
8ceb9843
PS
2794 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2795 from_name, PATH_MAX,
2796 cifs_sb->local_nls, remap);
1da177e4
LT
2797 name_len++; /* trailing null */
2798 name_len *= 2;
2799 pSMB->OldFileName[name_len] = 0x04; /* pad */
2800 /* protocol requires ASCII signature byte on Unicode string */
2801 pSMB->OldFileName[name_len + 1] = 0x00;
2802 name_len2 =
acbbb76a 2803 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
8ceb9843
PS
2804 to_name, PATH_MAX, cifs_sb->local_nls,
2805 remap);
1da177e4
LT
2806 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2807 name_len2 *= 2; /* convert to bytes */
50c2f753 2808 } else { /* BB improve the check for buffer overruns BB */
8ceb9843 2809 name_len = strnlen(from_name, PATH_MAX);
1da177e4 2810 name_len++; /* trailing null */
8ceb9843
PS
2811 strncpy(pSMB->OldFileName, from_name, name_len);
2812 name_len2 = strnlen(to_name, PATH_MAX);
1da177e4
LT
2813 name_len2++; /* trailing null */
2814 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
8ceb9843 2815 strncpy(&pSMB->OldFileName[name_len + 1], to_name, name_len2);
1da177e4
LT
2816 name_len2++; /* trailing null */
2817 name_len2++; /* signature byte */
2818 }
2819
2820 count = 1 /* 1st signature byte */ + name_len + name_len2;
be8e3b00 2821 inc_rfc1001_len(pSMB, count);
1da177e4
LT
2822 pSMB->ByteCount = cpu_to_le16(count);
2823
2824 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2825 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
44c58186 2826 cifs_stats_inc(&tcon->stats.cifs_stats.num_renames);
ad7a2926 2827 if (rc)
f96637be 2828 cifs_dbg(FYI, "Send error in rename = %d\n", rc);
1da177e4 2829
1da177e4
LT
2830 cifs_buf_release(pSMB);
2831
2832 if (rc == -EAGAIN)
2833 goto renameRetry;
2834
2835 return rc;
2836}
2837
6d5786a3 2838int CIFSSMBRenameOpenFile(const unsigned int xid, struct cifs_tcon *pTcon,
391e5755 2839 int netfid, const char *target_name,
50c2f753 2840 const struct nls_table *nls_codepage, int remap)
1da177e4
LT
2841{
2842 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2843 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
50c2f753 2844 struct set_file_rename *rename_info;
1da177e4
LT
2845 char *data_offset;
2846 char dummy_string[30];
2847 int rc = 0;
2848 int bytes_returned = 0;
2849 int len_of_str;
2850 __u16 params, param_offset, offset, count, byte_count;
2851
f96637be 2852 cifs_dbg(FYI, "Rename to File by handle\n");
1da177e4
LT
2853 rc = smb_init(SMB_COM_TRANSACTION2, 15, pTcon, (void **) &pSMB,
2854 (void **) &pSMBr);
2855 if (rc)
2856 return rc;
2857
2858 params = 6;
2859 pSMB->MaxSetupCount = 0;
2860 pSMB->Reserved = 0;
2861 pSMB->Flags = 0;
2862 pSMB->Timeout = 0;
2863 pSMB->Reserved2 = 0;
2864 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2865 offset = param_offset + params;
2866
2867 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2868 rename_info = (struct set_file_rename *) data_offset;
2869 pSMB->MaxParameterCount = cpu_to_le16(2);
ad7a2926 2870 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
1da177e4
LT
2871 pSMB->SetupCount = 1;
2872 pSMB->Reserved3 = 0;
2873 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2874 byte_count = 3 /* pad */ + params;
2875 pSMB->ParameterCount = cpu_to_le16(params);
2876 pSMB->TotalParameterCount = pSMB->ParameterCount;
2877 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2878 pSMB->DataOffset = cpu_to_le16(offset);
2879 /* construct random name ".cifs_tmp<inodenum><mid>" */
2880 rename_info->overwrite = cpu_to_le32(1);
2881 rename_info->root_fid = 0;
2882 /* unicode only call */
790fe579 2883 if (target_name == NULL) {
50c2f753 2884 sprintf(dummy_string, "cifs%x", pSMB->hdr.Mid);
acbbb76a
SF
2885 len_of_str =
2886 cifsConvertToUTF16((__le16 *)rename_info->target_name,
737b758c 2887 dummy_string, 24, nls_codepage, remap);
1da177e4 2888 } else {
acbbb76a
SF
2889 len_of_str =
2890 cifsConvertToUTF16((__le16 *)rename_info->target_name,
50c2f753
SF
2891 target_name, PATH_MAX, nls_codepage,
2892 remap);
1da177e4
LT
2893 }
2894 rename_info->target_name_len = cpu_to_le32(2 * len_of_str);
391e5755 2895 count = 12 /* sizeof(struct set_file_rename) */ + (2 * len_of_str);
1da177e4
LT
2896 byte_count += count;
2897 pSMB->DataCount = cpu_to_le16(count);
2898 pSMB->TotalDataCount = pSMB->DataCount;
2899 pSMB->Fid = netfid;
2900 pSMB->InformationLevel =
2901 cpu_to_le16(SMB_SET_FILE_RENAME_INFORMATION);
2902 pSMB->Reserved4 = 0;
be8e3b00 2903 inc_rfc1001_len(pSMB, byte_count);
1da177e4
LT
2904 pSMB->ByteCount = cpu_to_le16(byte_count);
2905 rc = SendReceive(xid, pTcon->ses, (struct smb_hdr *) pSMB,
50c2f753 2906 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
44c58186 2907 cifs_stats_inc(&pTcon->stats.cifs_stats.num_t2renames);
ad7a2926 2908 if (rc)
f96637be
JP
2909 cifs_dbg(FYI, "Send error in Rename (by file handle) = %d\n",
2910 rc);
a5a2b489 2911
1da177e4
LT
2912 cifs_buf_release(pSMB);
2913
2914 /* Note: On -EAGAIN error only caller can retry on handle based calls
2915 since file handle passed in no longer valid */
2916
2917 return rc;
2918}
2919
2920int
6d5786a3
PS
2921CIFSSMBCopy(const unsigned int xid, struct cifs_tcon *tcon,
2922 const char *fromName, const __u16 target_tid, const char *toName,
2923 const int flags, const struct nls_table *nls_codepage, int remap)
1da177e4
LT
2924{
2925 int rc = 0;
2926 COPY_REQ *pSMB = NULL;
2927 COPY_RSP *pSMBr = NULL;
2928 int bytes_returned;
2929 int name_len, name_len2;
2930 __u16 count;
2931
f96637be 2932 cifs_dbg(FYI, "In CIFSSMBCopy\n");
1da177e4
LT
2933copyRetry:
2934 rc = smb_init(SMB_COM_COPY, 1, tcon, (void **) &pSMB,
2935 (void **) &pSMBr);
2936 if (rc)
2937 return rc;
2938
2939 pSMB->BufferFormat = 0x04;
2940 pSMB->Tid2 = target_tid;
2941
2942 pSMB->Flags = cpu_to_le16(flags & COPY_TREE);
2943
2944 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
acbbb76a
SF
2945 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2946 fromName, PATH_MAX, nls_codepage,
2947 remap);
1da177e4
LT
2948 name_len++; /* trailing null */
2949 name_len *= 2;
2950 pSMB->OldFileName[name_len] = 0x04; /* pad */
2951 /* protocol requires ASCII signature byte on Unicode string */
2952 pSMB->OldFileName[name_len + 1] = 0x00;
50c2f753 2953 name_len2 =
acbbb76a
SF
2954 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2955 toName, PATH_MAX, nls_codepage, remap);
1da177e4
LT
2956 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2957 name_len2 *= 2; /* convert to bytes */
50c2f753 2958 } else { /* BB improve the check for buffer overruns BB */
1da177e4
LT
2959 name_len = strnlen(fromName, PATH_MAX);
2960 name_len++; /* trailing null */
2961 strncpy(pSMB->OldFileName, fromName, name_len);
2962 name_len2 = strnlen(toName, PATH_MAX);
2963 name_len2++; /* trailing null */
2964 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2965 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
2966 name_len2++; /* trailing null */
2967 name_len2++; /* signature byte */
2968 }
2969
2970 count = 1 /* 1st signature byte */ + name_len + name_len2;
be8e3b00 2971 inc_rfc1001_len(pSMB, count);
1da177e4
LT
2972 pSMB->ByteCount = cpu_to_le16(count);
2973
2974 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2975 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2976 if (rc) {
f96637be
JP
2977 cifs_dbg(FYI, "Send error in copy = %d with %d files copied\n",
2978 rc, le16_to_cpu(pSMBr->CopyCount));
1da177e4 2979 }
0d817bc0 2980 cifs_buf_release(pSMB);
1da177e4
LT
2981
2982 if (rc == -EAGAIN)
2983 goto copyRetry;
2984
2985 return rc;
2986}
2987
2988int
6d5786a3 2989CIFSUnixCreateSymLink(const unsigned int xid, struct cifs_tcon *tcon,
1da177e4 2990 const char *fromName, const char *toName,
bc8ebdc4 2991 const struct nls_table *nls_codepage, int remap)
1da177e4
LT
2992{
2993 TRANSACTION2_SPI_REQ *pSMB = NULL;
2994 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2995 char *data_offset;
2996 int name_len;
2997 int name_len_target;
2998 int rc = 0;
2999 int bytes_returned = 0;
3000 __u16 params, param_offset, offset, byte_count;
3001
f96637be 3002 cifs_dbg(FYI, "In Symlink Unix style\n");
1da177e4
LT
3003createSymLinkRetry:
3004 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3005 (void **) &pSMBr);
3006 if (rc)
3007 return rc;
3008
3009 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3010 name_len =
bc8ebdc4
NA
3011 cifsConvertToUTF16((__le16 *) pSMB->FileName, fromName,
3012 /* find define for this maxpathcomponent */
3013 PATH_MAX, nls_codepage, remap);
1da177e4
LT
3014 name_len++; /* trailing null */
3015 name_len *= 2;
3016
50c2f753 3017 } else { /* BB improve the check for buffer overruns BB */
1da177e4
LT
3018 name_len = strnlen(fromName, PATH_MAX);
3019 name_len++; /* trailing null */
3020 strncpy(pSMB->FileName, fromName, name_len);
3021 }
3022 params = 6 + name_len;
3023 pSMB->MaxSetupCount = 0;
3024 pSMB->Reserved = 0;
3025 pSMB->Flags = 0;
3026 pSMB->Timeout = 0;
3027 pSMB->Reserved2 = 0;
3028 param_offset = offsetof(struct smb_com_transaction2_spi_req,
50c2f753 3029 InformationLevel) - 4;
1da177e4
LT
3030 offset = param_offset + params;
3031
3032 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
3033 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3034 name_len_target =
bc8ebdc4
NA
3035 cifsConvertToUTF16((__le16 *) data_offset, toName,
3036 /* find define for this maxpathcomponent */
3037 PATH_MAX, nls_codepage, remap);
1da177e4
LT
3038 name_len_target++; /* trailing null */
3039 name_len_target *= 2;
50c2f753 3040 } else { /* BB improve the check for buffer overruns BB */
1da177e4
LT
3041 name_len_target = strnlen(toName, PATH_MAX);
3042 name_len_target++; /* trailing null */
3043 strncpy(data_offset, toName, name_len_target);
3044 }
3045
3046 pSMB->MaxParameterCount = cpu_to_le16(2);
3047 /* BB find exact max on data count below from sess */
3048 pSMB->MaxDataCount = cpu_to_le16(1000);
3049 pSMB->SetupCount = 1;
3050 pSMB->Reserved3 = 0;
3051 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
3052 byte_count = 3 /* pad */ + params + name_len_target;
3053 pSMB->DataCount = cpu_to_le16(name_len_target);
3054 pSMB->ParameterCount = cpu_to_le16(params);
3055 pSMB->TotalDataCount = pSMB->DataCount;
3056 pSMB->TotalParameterCount = pSMB->ParameterCount;
3057 pSMB->ParameterOffset = cpu_to_le16(param_offset);
3058 pSMB->DataOffset = cpu_to_le16(offset);
3059 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_LINK);
3060 pSMB->Reserved4 = 0;
be8e3b00 3061 inc_rfc1001_len(pSMB, byte_count);
1da177e4
LT
3062 pSMB->ByteCount = cpu_to_le16(byte_count);
3063 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3064 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
44c58186 3065 cifs_stats_inc(&tcon->stats.cifs_stats.num_symlinks);
ad7a2926 3066 if (rc)
f96637be
JP
3067 cifs_dbg(FYI, "Send error in SetPathInfo create symlink = %d\n",
3068 rc);
1da177e4 3069
0d817bc0 3070 cifs_buf_release(pSMB);
1da177e4
LT
3071
3072 if (rc == -EAGAIN)
3073 goto createSymLinkRetry;
3074
3075 return rc;
3076}
3077
3078int
6d5786a3 3079CIFSUnixCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon,
1da177e4 3080 const char *fromName, const char *toName,
737b758c 3081 const struct nls_table *nls_codepage, int remap)
1da177e4
LT
3082{
3083 TRANSACTION2_SPI_REQ *pSMB = NULL;
3084 TRANSACTION2_SPI_RSP *pSMBr = NULL;
3085 char *data_offset;
3086 int name_len;
3087 int name_len_target;
3088 int rc = 0;
3089 int bytes_returned = 0;
3090 __u16 params, param_offset, offset, byte_count;
3091
f96637be 3092 cifs_dbg(FYI, "In Create Hard link Unix style\n");
1da177e4
LT
3093createHardLinkRetry:
3094 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3095 (void **) &pSMBr);
3096 if (rc)
3097 return rc;
3098
3099 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
acbbb76a
SF
3100 name_len = cifsConvertToUTF16((__le16 *) pSMB->FileName, toName,
3101 PATH_MAX, nls_codepage, remap);
1da177e4
LT
3102 name_len++; /* trailing null */
3103 name_len *= 2;
3104
50c2f753 3105 } else { /* BB improve the check for buffer overruns BB */
1da177e4
LT
3106 name_len = strnlen(toName, PATH_MAX);
3107 name_len++; /* trailing null */
3108 strncpy(pSMB->FileName, toName, name_len);
3109 }
3110 params = 6 + name_len;
3111 pSMB->MaxSetupCount = 0;
3112 pSMB->Reserved = 0;
3113 pSMB->Flags = 0;
3114 pSMB->Timeout = 0;
3115 pSMB->Reserved2 = 0;
3116 param_offset = offsetof(struct smb_com_transaction2_spi_req,
50c2f753 3117 InformationLevel) - 4;
1da177e4
LT
3118 offset = param_offset + params;
3119
3120 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
3121 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3122 name_len_target =
acbbb76a
SF
3123 cifsConvertToUTF16((__le16 *) data_offset, fromName,
3124 PATH_MAX, nls_codepage, remap);
1da177e4
LT
3125 name_len_target++; /* trailing null */
3126 name_len_target *= 2;
50c2f753 3127 } else { /* BB improve the check for buffer overruns BB */
1da177e4
LT
3128 name_len_target = strnlen(fromName, PATH_MAX);
3129 name_len_target++; /* trailing null */
3130 strncpy(data_offset, fromName, name_len_target);
3131 }
3132
3133 pSMB->MaxParameterCount = cpu_to_le16(2);
3134 /* BB find exact max on data count below from sess*/
3135 pSMB->MaxDataCount = cpu_to_le16(1000);
3136 pSMB->SetupCount = 1;
3137 pSMB->Reserved3 = 0;
3138 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
3139 byte_count = 3 /* pad */ + params + name_len_target;
3140 pSMB->ParameterCount = cpu_to_le16(params);
3141 pSMB->TotalParameterCount = pSMB->ParameterCount;
3142 pSMB->DataCount = cpu_to_le16(name_len_target);
3143 pSMB->TotalDataCount = pSMB->DataCount;
3144 pSMB->ParameterOffset = cpu_to_le16(param_offset);
3145 pSMB->DataOffset = cpu_to_le16(offset);
3146 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_HLINK);
3147 pSMB->Reserved4 = 0;
be8e3b00 3148 inc_rfc1001_len(pSMB, byte_count);
1da177e4
LT
3149 pSMB->ByteCount = cpu_to_le16(byte_count);
3150 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3151 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
44c58186 3152 cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
ad7a2926 3153 if (rc)
f96637be
JP
3154 cifs_dbg(FYI, "Send error in SetPathInfo (hard link) = %d\n",
3155 rc);
1da177e4
LT
3156
3157 cifs_buf_release(pSMB);
3158 if (rc == -EAGAIN)
3159 goto createHardLinkRetry;
3160
3161 return rc;
3162}
3163
3164int
6d5786a3 3165CIFSCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon,
d6e906f1
SF
3166 const char *from_name, const char *to_name,
3167 struct cifs_sb_info *cifs_sb)
1da177e4
LT
3168{
3169 int rc = 0;
3170 NT_RENAME_REQ *pSMB = NULL;
3171 RENAME_RSP *pSMBr = NULL;
3172 int bytes_returned;
3173 int name_len, name_len2;
3174 __u16 count;
2baa2682 3175 int remap = cifs_remap(cifs_sb);
1da177e4 3176
f96637be 3177 cifs_dbg(FYI, "In CIFSCreateHardLink\n");
1da177e4
LT
3178winCreateHardLinkRetry:
3179
3180 rc = smb_init(SMB_COM_NT_RENAME, 4, tcon, (void **) &pSMB,
3181 (void **) &pSMBr);
3182 if (rc)
3183 return rc;
3184
3185 pSMB->SearchAttributes =
3186 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
3187 ATTR_DIRECTORY);
3188 pSMB->Flags = cpu_to_le16(CREATE_HARD_LINK);
3189 pSMB->ClusterCount = 0;
3190
3191 pSMB->BufferFormat = 0x04;
3192
3193 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3194 name_len =
d6e906f1
SF
3195 cifsConvertToUTF16((__le16 *) pSMB->OldFileName, from_name,
3196 PATH_MAX, cifs_sb->local_nls, remap);
1da177e4
LT
3197 name_len++; /* trailing null */
3198 name_len *= 2;
fcc7c09d
JL
3199
3200 /* protocol specifies ASCII buffer format (0x04) for unicode */
3201 pSMB->OldFileName[name_len] = 0x04;
3202 pSMB->OldFileName[name_len + 1] = 0x00; /* pad */
1da177e4 3203 name_len2 =
acbbb76a 3204 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
d6e906f1
SF
3205 to_name, PATH_MAX, cifs_sb->local_nls,
3206 remap);
1da177e4
LT
3207 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
3208 name_len2 *= 2; /* convert to bytes */
50c2f753 3209 } else { /* BB improve the check for buffer overruns BB */
d6e906f1 3210 name_len = strnlen(from_name, PATH_MAX);
1da177e4 3211 name_len++; /* trailing null */
d6e906f1
SF
3212 strncpy(pSMB->OldFileName, from_name, name_len);
3213 name_len2 = strnlen(to_name, PATH_MAX);
1da177e4
LT
3214 name_len2++; /* trailing null */
3215 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
d6e906f1 3216 strncpy(&pSMB->OldFileName[name_len + 1], to_name, name_len2);
1da177e4
LT
3217 name_len2++; /* trailing null */
3218 name_len2++; /* signature byte */
3219 }
3220
3221 count = 1 /* string type byte */ + name_len + name_len2;
be8e3b00 3222 inc_rfc1001_len(pSMB, count);
1da177e4
LT
3223 pSMB->ByteCount = cpu_to_le16(count);
3224
3225 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3226 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
44c58186 3227 cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
ad7a2926 3228 if (rc)
f96637be 3229 cifs_dbg(FYI, "Send error in hard link (NT rename) = %d\n", rc);
ad7a2926 3230
1da177e4
LT
3231 cifs_buf_release(pSMB);
3232 if (rc == -EAGAIN)
3233 goto winCreateHardLinkRetry;
3234
3235 return rc;
3236}
3237
3238int
6d5786a3 3239CIFSSMBUnixQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon,
460b9696 3240 const unsigned char *searchName, char **symlinkinfo,
bc8ebdc4 3241 const struct nls_table *nls_codepage, int remap)
1da177e4
LT
3242{
3243/* SMB_QUERY_FILE_UNIX_LINK */
3244 TRANSACTION2_QPI_REQ *pSMB = NULL;
3245 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3246 int rc = 0;
3247 int bytes_returned;
3248 int name_len;
3249 __u16 params, byte_count;
460b9696 3250 char *data_start;
1da177e4 3251
f96637be 3252 cifs_dbg(FYI, "In QPathSymLinkInfo (Unix) for path %s\n", searchName);
1da177e4
LT
3253
3254querySymLinkRetry:
3255 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3256 (void **) &pSMBr);
3257 if (rc)
3258 return rc;
3259
3260 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3261 name_len =
bc8ebdc4
NA
3262 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3263 searchName, PATH_MAX, nls_codepage,
3264 remap);
1da177e4
LT
3265 name_len++; /* trailing null */
3266 name_len *= 2;
50c2f753 3267 } else { /* BB improve the check for buffer overruns BB */
1da177e4
LT
3268 name_len = strnlen(searchName, PATH_MAX);
3269 name_len++; /* trailing null */
3270 strncpy(pSMB->FileName, searchName, name_len);
3271 }
3272
3273 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3274 pSMB->TotalDataCount = 0;
3275 pSMB->MaxParameterCount = cpu_to_le16(2);
46a7574c 3276 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
1da177e4
LT
3277 pSMB->MaxSetupCount = 0;
3278 pSMB->Reserved = 0;
3279 pSMB->Flags = 0;
3280 pSMB->Timeout = 0;
3281 pSMB->Reserved2 = 0;
3282 pSMB->ParameterOffset = cpu_to_le16(offsetof(
50c2f753 3283 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
1da177e4
LT
3284 pSMB->DataCount = 0;
3285 pSMB->DataOffset = 0;
3286 pSMB->SetupCount = 1;
3287 pSMB->Reserved3 = 0;
3288 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3289 byte_count = params + 1 /* pad */ ;
3290 pSMB->TotalParameterCount = cpu_to_le16(params);
3291 pSMB->ParameterCount = pSMB->TotalParameterCount;
3292 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_LINK);
3293 pSMB->Reserved4 = 0;
be8e3b00 3294 inc_rfc1001_len(pSMB, byte_count);
1da177e4
LT
3295 pSMB->ByteCount = cpu_to_le16(byte_count);
3296
3297 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3298 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3299 if (rc) {
f96637be 3300 cifs_dbg(FYI, "Send error in QuerySymLinkInfo = %d\n", rc);
1da177e4
LT
3301 } else {
3302 /* decode response */
3303
3304 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
1da177e4 3305 /* BB also check enough total bytes returned */
820a803f 3306 if (rc || get_bcc(&pSMBr->hdr) < 2)
460b9696 3307 rc = -EIO;
1da177e4 3308 else {
0e0d2cf3 3309 bool is_unicode;
460b9696
JL
3310 u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3311
3312 data_start = ((char *) &pSMBr->hdr.Protocol) +
3313 le16_to_cpu(pSMBr->t2.DataOffset);
1da177e4 3314
0e0d2cf3
SF
3315 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3316 is_unicode = true;
3317 else
3318 is_unicode = false;
3319
737b758c 3320 /* BB FIXME investigate remapping reserved chars here */
acbbb76a
SF
3321 *symlinkinfo = cifs_strndup_from_utf16(data_start,
3322 count, is_unicode, nls_codepage);
8b6427a2 3323 if (!*symlinkinfo)
460b9696 3324 rc = -ENOMEM;
1da177e4
LT
3325 }
3326 }
3327 cifs_buf_release(pSMB);
3328 if (rc == -EAGAIN)
3329 goto querySymLinkRetry;
3330 return rc;
3331}
3332
c52a9554
SF
3333/*
3334 * Recent Windows versions now create symlinks more frequently
3335 * and they use the "reparse point" mechanism below. We can of course
3336 * do symlinks nicely to Samba and other servers which support the
3337 * CIFS Unix Extensions and we can also do SFU symlinks and "client only"
3338 * "MF" symlinks optionally, but for recent Windows we really need to
3339 * reenable the code below and fix the cifs_symlink callers to handle this.
3340 * In the interim this code has been moved to its own config option so
3341 * it is not compiled in by default until callers fixed up and more tested.
3342 */
1da177e4 3343int
d244bf2d
PS
3344CIFSSMBQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon,
3345 __u16 fid, char **symlinkinfo,
3346 const struct nls_table *nls_codepage)
1da177e4
LT
3347{
3348 int rc = 0;
3349 int bytes_returned;
50c2f753
SF
3350 struct smb_com_transaction_ioctl_req *pSMB;
3351 struct smb_com_transaction_ioctl_rsp *pSMBr;
d244bf2d
PS
3352 bool is_unicode;
3353 unsigned int sub_len;
3354 char *sub_start;
c31f3307
SF
3355 struct reparse_symlink_data *reparse_buf;
3356 struct reparse_posix_data *posix_buf;
d244bf2d
PS
3357 __u32 data_offset, data_count;
3358 char *end_of_smb;
3359
3360 cifs_dbg(FYI, "In Windows reparse style QueryLink for fid %u\n", fid);
1da177e4
LT
3361 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3362 (void **) &pSMBr);
3363 if (rc)
3364 return rc;
3365
3366 pSMB->TotalParameterCount = 0 ;
3367 pSMB->TotalDataCount = 0;
3368 pSMB->MaxParameterCount = cpu_to_le32(2);
3369 /* BB find exact data count max from sess structure BB */
c974befa 3370 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
1da177e4
LT
3371 pSMB->MaxSetupCount = 4;
3372 pSMB->Reserved = 0;
3373 pSMB->ParameterOffset = 0;
3374 pSMB->DataCount = 0;
3375 pSMB->DataOffset = 0;
3376 pSMB->SetupCount = 4;
3377 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
3378 pSMB->ParameterCount = pSMB->TotalParameterCount;
3379 pSMB->FunctionCode = cpu_to_le32(FSCTL_GET_REPARSE_POINT);
3380 pSMB->IsFsctl = 1; /* FSCTL */
3381 pSMB->IsRootFlag = 0;
3382 pSMB->Fid = fid; /* file handle always le */
3383 pSMB->ByteCount = 0;
3384
3385 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3386 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3387 if (rc) {
f96637be 3388 cifs_dbg(FYI, "Send error in QueryReparseLinkInfo = %d\n", rc);
d244bf2d
PS
3389 goto qreparse_out;
3390 }
3391
3392 data_offset = le32_to_cpu(pSMBr->DataOffset);
3393 data_count = le32_to_cpu(pSMBr->DataCount);
3394 if (get_bcc(&pSMBr->hdr) < 2 || data_offset > 512) {
3395 /* BB also check enough total bytes returned */
3396 rc = -EIO; /* bad smb */
3397 goto qreparse_out;
3398 }
3399 if (!data_count || (data_count > 2048)) {
3400 rc = -EIO;
3401 cifs_dbg(FYI, "Invalid return data count on get reparse info ioctl\n");
3402 goto qreparse_out;
3403 }
3404 end_of_smb = 2 + get_bcc(&pSMBr->hdr) + (char *)&pSMBr->ByteCount;
c31f3307 3405 reparse_buf = (struct reparse_symlink_data *)
d244bf2d
PS
3406 ((char *)&pSMBr->hdr.Protocol + data_offset);
3407 if ((char *)reparse_buf >= end_of_smb) {
3408 rc = -EIO;
3409 goto qreparse_out;
1da177e4 3410 }
c31f3307
SF
3411 if (reparse_buf->ReparseTag == cpu_to_le32(IO_REPARSE_TAG_NFS)) {
3412 cifs_dbg(FYI, "NFS style reparse tag\n");
3413 posix_buf = (struct reparse_posix_data *)reparse_buf;
3414
3415 if (posix_buf->InodeType != cpu_to_le64(NFS_SPECFILE_LNK)) {
3416 cifs_dbg(FYI, "unsupported file type 0x%llx\n",
3417 le64_to_cpu(posix_buf->InodeType));
3418 rc = -EOPNOTSUPP;
3419 goto qreparse_out;
3420 }
3421 is_unicode = true;
3422 sub_len = le16_to_cpu(reparse_buf->ReparseDataLength);
3423 if (posix_buf->PathBuffer + sub_len > end_of_smb) {
3424 cifs_dbg(FYI, "reparse buf beyond SMB\n");
3425 rc = -EIO;
3426 goto qreparse_out;
3427 }
3428 *symlinkinfo = cifs_strndup_from_utf16(posix_buf->PathBuffer,
3429 sub_len, is_unicode, nls_codepage);
3430 goto qreparse_out;
3431 } else if (reparse_buf->ReparseTag !=
3432 cpu_to_le32(IO_REPARSE_TAG_SYMLINK)) {
3433 rc = -EOPNOTSUPP;
3434 goto qreparse_out;
3435 }
3436
3437 /* Reparse tag is NTFS symlink */
3438 sub_start = le16_to_cpu(reparse_buf->SubstituteNameOffset) +
3439 reparse_buf->PathBuffer;
3440 sub_len = le16_to_cpu(reparse_buf->SubstituteNameLength);
3441 if (sub_start + sub_len > end_of_smb) {
d244bf2d
PS
3442 cifs_dbg(FYI, "reparse buf beyond SMB\n");
3443 rc = -EIO;
3444 goto qreparse_out;
3445 }
d244bf2d
PS
3446 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3447 is_unicode = true;
3448 else
3449 is_unicode = false;
989c7e51 3450
d244bf2d
PS
3451 /* BB FIXME investigate remapping reserved chars here */
3452 *symlinkinfo = cifs_strndup_from_utf16(sub_start, sub_len, is_unicode,
3453 nls_codepage);
3454 if (!*symlinkinfo)
3455 rc = -ENOMEM;
1da177e4 3456qreparse_out:
4a6d87f1 3457 cifs_buf_release(pSMB);
1da177e4 3458
d244bf2d
PS
3459 /*
3460 * Note: On -EAGAIN error only caller can retry on handle based calls
3461 * since file handle passed in no longer valid.
3462 */
1da177e4
LT
3463 return rc;
3464}
3465
c7f508a9
SF
3466int
3467CIFSSMB_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
3468 __u16 fid)
3469{
3470 int rc = 0;
3471 int bytes_returned;
3472 struct smb_com_transaction_compr_ioctl_req *pSMB;
3473 struct smb_com_transaction_ioctl_rsp *pSMBr;
3474
3475 cifs_dbg(FYI, "Set compression for %u\n", fid);
3476 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3477 (void **) &pSMBr);
3478 if (rc)
3479 return rc;
3480
3481 pSMB->compression_state = cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
3482
3483 pSMB->TotalParameterCount = 0;
bc09d141 3484 pSMB->TotalDataCount = cpu_to_le32(2);
c7f508a9
SF
3485 pSMB->MaxParameterCount = 0;
3486 pSMB->MaxDataCount = 0;
3487 pSMB->MaxSetupCount = 4;
3488 pSMB->Reserved = 0;
3489 pSMB->ParameterOffset = 0;
bc09d141 3490 pSMB->DataCount = cpu_to_le32(2);
c7f508a9
SF
3491 pSMB->DataOffset =
3492 cpu_to_le32(offsetof(struct smb_com_transaction_compr_ioctl_req,
3493 compression_state) - 4); /* 84 */
3494 pSMB->SetupCount = 4;
bc09d141 3495 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
c7f508a9 3496 pSMB->ParameterCount = 0;
bc09d141 3497 pSMB->FunctionCode = cpu_to_le32(FSCTL_SET_COMPRESSION);
c7f508a9
SF
3498 pSMB->IsFsctl = 1; /* FSCTL */
3499 pSMB->IsRootFlag = 0;
3500 pSMB->Fid = fid; /* file handle always le */
3501 /* 3 byte pad, followed by 2 byte compress state */
bc09d141 3502 pSMB->ByteCount = cpu_to_le16(5);
c7f508a9
SF
3503 inc_rfc1001_len(pSMB, 5);
3504
3505 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3506 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3507 if (rc)
3508 cifs_dbg(FYI, "Send error in SetCompression = %d\n", rc);
3509
3510 cifs_buf_release(pSMB);
3511
3512 /*
3513 * Note: On -EAGAIN error only caller can retry on handle based calls
3514 * since file handle passed in no longer valid.
3515 */
3516 return rc;
3517}
3518
3519
1da177e4
LT
3520#ifdef CONFIG_CIFS_POSIX
3521
3522/*Convert an Access Control Entry from wire format to local POSIX xattr format*/
2211d5ba 3523static void cifs_convert_ace(struct posix_acl_xattr_entry *ace,
50c2f753 3524 struct cifs_posix_ace *cifs_ace)
1da177e4
LT
3525{
3526 /* u8 cifs fields do not need le conversion */
ff7feac9
SF
3527 ace->e_perm = cpu_to_le16(cifs_ace->cifs_e_perm);
3528 ace->e_tag = cpu_to_le16(cifs_ace->cifs_e_tag);
3529 ace->e_id = cpu_to_le32(le64_to_cpu(cifs_ace->cifs_uid));
f96637be
JP
3530/*
3531 cifs_dbg(FYI, "perm %d tag %d id %d\n",
3532 ace->e_perm, ace->e_tag, ace->e_id);
3533*/
1da177e4
LT
3534
3535 return;
3536}
3537
3538/* Convert ACL from CIFS POSIX wire format to local Linux POSIX ACL xattr */
50c2f753
SF
3539static int cifs_copy_posix_acl(char *trgt, char *src, const int buflen,
3540 const int acl_type, const int size_of_data_area)
1da177e4
LT
3541{
3542 int size = 0;
3543 int i;
3544 __u16 count;
50c2f753
SF
3545 struct cifs_posix_ace *pACE;
3546 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)src;
2211d5ba 3547 struct posix_acl_xattr_header *local_acl = (void *)trgt;
1da177e4
LT
3548
3549 if (le16_to_cpu(cifs_acl->version) != CIFS_ACL_VERSION)
3550 return -EOPNOTSUPP;
3551
45987e00 3552 if (acl_type == ACL_TYPE_ACCESS) {
1da177e4
LT
3553 count = le16_to_cpu(cifs_acl->access_entry_count);
3554 pACE = &cifs_acl->ace_array[0];
3555 size = sizeof(struct cifs_posix_acl);
3556 size += sizeof(struct cifs_posix_ace) * count;
3557 /* check if we would go beyond end of SMB */
790fe579 3558 if (size_of_data_area < size) {
f96637be
JP
3559 cifs_dbg(FYI, "bad CIFS POSIX ACL size %d vs. %d\n",
3560 size_of_data_area, size);
1da177e4
LT
3561 return -EINVAL;
3562 }
45987e00 3563 } else if (acl_type == ACL_TYPE_DEFAULT) {
1da177e4
LT
3564 count = le16_to_cpu(cifs_acl->access_entry_count);
3565 size = sizeof(struct cifs_posix_acl);
3566 size += sizeof(struct cifs_posix_ace) * count;
3567/* skip past access ACEs to get to default ACEs */
3568 pACE = &cifs_acl->ace_array[count];
3569 count = le16_to_cpu(cifs_acl->default_entry_count);
3570 size += sizeof(struct cifs_posix_ace) * count;
3571 /* check if we would go beyond end of SMB */
790fe579 3572 if (size_of_data_area < size)
1da177e4
LT
3573 return -EINVAL;
3574 } else {
3575 /* illegal type */
3576 return -EINVAL;
3577 }
3578
3579 size = posix_acl_xattr_size(count);
790fe579 3580 if ((buflen == 0) || (local_acl == NULL)) {
50c2f753 3581 /* used to query ACL EA size */
790fe579 3582 } else if (size > buflen) {
1da177e4
LT
3583 return -ERANGE;
3584 } else /* buffer big enough */ {
2211d5ba
AG
3585 struct posix_acl_xattr_entry *ace = (void *)(local_acl + 1);
3586
ff7feac9 3587 local_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
50c2f753 3588 for (i = 0; i < count ; i++) {
2211d5ba 3589 cifs_convert_ace(&ace[i], pACE);
50c2f753 3590 pACE++;
1da177e4
LT
3591 }
3592 }
3593 return size;
3594}
3595
50c2f753 3596static __u16 convert_ace_to_cifs_ace(struct cifs_posix_ace *cifs_ace,
2211d5ba 3597 const struct posix_acl_xattr_entry *local_ace)
1da177e4
LT
3598{
3599 __u16 rc = 0; /* 0 = ACL converted ok */
3600
ff7feac9
SF
3601 cifs_ace->cifs_e_perm = le16_to_cpu(local_ace->e_perm);
3602 cifs_ace->cifs_e_tag = le16_to_cpu(local_ace->e_tag);
1da177e4 3603 /* BB is there a better way to handle the large uid? */
790fe579 3604 if (local_ace->e_id == cpu_to_le32(-1)) {
1da177e4
LT
3605 /* Probably no need to le convert -1 on any arch but can not hurt */
3606 cifs_ace->cifs_uid = cpu_to_le64(-1);
50c2f753 3607 } else
ff7feac9 3608 cifs_ace->cifs_uid = cpu_to_le64(le32_to_cpu(local_ace->e_id));
f96637be
JP
3609/*
3610 cifs_dbg(FYI, "perm %d tag %d id %d\n",
3611 ace->e_perm, ace->e_tag, ace->e_id);
3612*/
1da177e4
LT
3613 return rc;
3614}
3615
3616/* Convert ACL from local Linux POSIX xattr to CIFS POSIX ACL wire format */
50c2f753
SF
3617static __u16 ACL_to_cifs_posix(char *parm_data, const char *pACL,
3618 const int buflen, const int acl_type)
1da177e4
LT
3619{
3620 __u16 rc = 0;
50c2f753 3621 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)parm_data;
2211d5ba 3622 struct posix_acl_xattr_header *local_acl = (void *)pACL;
ae9ebe7c 3623 struct posix_acl_xattr_entry *ace = (void *)(local_acl + 1);
1da177e4
LT
3624 int count;
3625 int i;
3626
790fe579 3627 if ((buflen == 0) || (pACL == NULL) || (cifs_acl == NULL))
1da177e4
LT
3628 return 0;
3629
3630 count = posix_acl_xattr_count((size_t)buflen);
f96637be
JP
3631 cifs_dbg(FYI, "setting acl with %d entries from buf of length %d and version of %d\n",
3632 count, buflen, le32_to_cpu(local_acl->a_version));
790fe579 3633 if (le32_to_cpu(local_acl->a_version) != 2) {
f96637be
JP
3634 cifs_dbg(FYI, "unknown POSIX ACL version %d\n",
3635 le32_to_cpu(local_acl->a_version));
1da177e4
LT
3636 return 0;
3637 }
3638 cifs_acl->version = cpu_to_le16(1);
b1d93356 3639 if (acl_type == ACL_TYPE_ACCESS) {
ff7feac9 3640 cifs_acl->access_entry_count = cpu_to_le16(count);
bc09d141 3641 cifs_acl->default_entry_count = cpu_to_le16(0xFFFF);
b1d93356 3642 } else if (acl_type == ACL_TYPE_DEFAULT) {
ff7feac9 3643 cifs_acl->default_entry_count = cpu_to_le16(count);
bc09d141 3644 cifs_acl->access_entry_count = cpu_to_le16(0xFFFF);
b1d93356 3645 } else {
f96637be 3646 cifs_dbg(FYI, "unknown ACL type %d\n", acl_type);
1da177e4
LT
3647 return 0;
3648 }
50c2f753 3649 for (i = 0; i < count; i++) {
ae9ebe7c 3650 rc = convert_ace_to_cifs_ace(&cifs_acl->ace_array[i], &ace[i]);
790fe579 3651 if (rc != 0) {
1da177e4
LT
3652 /* ACE not converted */
3653 break;
3654 }
3655 }
790fe579 3656 if (rc == 0) {
1da177e4
LT
3657 rc = (__u16)(count * sizeof(struct cifs_posix_ace));
3658 rc += sizeof(struct cifs_posix_acl);
3659 /* BB add check to make sure ACL does not overflow SMB */
3660 }
3661 return rc;
3662}
3663
3664int
6d5786a3 3665CIFSSMBGetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
50c2f753
SF
3666 const unsigned char *searchName,
3667 char *acl_inf, const int buflen, const int acl_type,
3668 const struct nls_table *nls_codepage, int remap)
1da177e4
LT
3669{
3670/* SMB_QUERY_POSIX_ACL */
3671 TRANSACTION2_QPI_REQ *pSMB = NULL;
3672 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3673 int rc = 0;
3674 int bytes_returned;
3675 int name_len;
3676 __u16 params, byte_count;
50c2f753 3677
f96637be 3678 cifs_dbg(FYI, "In GetPosixACL (Unix) for path %s\n", searchName);
1da177e4
LT
3679
3680queryAclRetry:
3681 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3682 (void **) &pSMBr);
3683 if (rc)
3684 return rc;
50c2f753 3685
1da177e4
LT
3686 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3687 name_len =
acbbb76a
SF
3688 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3689 searchName, PATH_MAX, nls_codepage,
3690 remap);
1da177e4
LT
3691 name_len++; /* trailing null */
3692 name_len *= 2;
3693 pSMB->FileName[name_len] = 0;
3694 pSMB->FileName[name_len+1] = 0;
50c2f753 3695 } else { /* BB improve the check for buffer overruns BB */
1da177e4
LT
3696 name_len = strnlen(searchName, PATH_MAX);
3697 name_len++; /* trailing null */
3698 strncpy(pSMB->FileName, searchName, name_len);
3699 }
3700
3701 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3702 pSMB->TotalDataCount = 0;
3703 pSMB->MaxParameterCount = cpu_to_le16(2);
50c2f753 3704 /* BB find exact max data count below from sess structure BB */
1da177e4
LT
3705 pSMB->MaxDataCount = cpu_to_le16(4000);
3706 pSMB->MaxSetupCount = 0;
3707 pSMB->Reserved = 0;
3708 pSMB->Flags = 0;
3709 pSMB->Timeout = 0;
3710 pSMB->Reserved2 = 0;
3711 pSMB->ParameterOffset = cpu_to_le16(
50c2f753
SF
3712 offsetof(struct smb_com_transaction2_qpi_req,
3713 InformationLevel) - 4);
1da177e4
LT
3714 pSMB->DataCount = 0;
3715 pSMB->DataOffset = 0;
3716 pSMB->SetupCount = 1;
3717 pSMB->Reserved3 = 0;
3718 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3719 byte_count = params + 1 /* pad */ ;
3720 pSMB->TotalParameterCount = cpu_to_le16(params);
3721 pSMB->ParameterCount = pSMB->TotalParameterCount;
3722 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_ACL);
3723 pSMB->Reserved4 = 0;
be8e3b00 3724 inc_rfc1001_len(pSMB, byte_count);
1da177e4
LT
3725 pSMB->ByteCount = cpu_to_le16(byte_count);
3726
3727 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3728 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
44c58186 3729 cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
1da177e4 3730 if (rc) {
f96637be 3731 cifs_dbg(FYI, "Send error in Query POSIX ACL = %d\n", rc);
1da177e4
LT
3732 } else {
3733 /* decode response */
50c2f753 3734
1da177e4 3735 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
1da177e4 3736 /* BB also check enough total bytes returned */
820a803f 3737 if (rc || get_bcc(&pSMBr->hdr) < 2)
1da177e4
LT
3738 rc = -EIO; /* bad smb */
3739 else {
3740 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3741 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3742 rc = cifs_copy_posix_acl(acl_inf,
3743 (char *)&pSMBr->hdr.Protocol+data_offset,
50c2f753 3744 buflen, acl_type, count);
1da177e4
LT
3745 }
3746 }
3747 cifs_buf_release(pSMB);
3748 if (rc == -EAGAIN)
3749 goto queryAclRetry;
3750 return rc;
3751}
3752
3753int
6d5786a3 3754CIFSSMBSetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
50c2f753
SF
3755 const unsigned char *fileName,
3756 const char *local_acl, const int buflen,
3757 const int acl_type,
3758 const struct nls_table *nls_codepage, int remap)
1da177e4
LT
3759{
3760 struct smb_com_transaction2_spi_req *pSMB = NULL;
3761 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
3762 char *parm_data;
3763 int name_len;
3764 int rc = 0;
3765 int bytes_returned = 0;
3766 __u16 params, byte_count, data_count, param_offset, offset;
3767
f96637be 3768 cifs_dbg(FYI, "In SetPosixACL (Unix) for path %s\n", fileName);
1da177e4
LT
3769setAclRetry:
3770 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
50c2f753 3771 (void **) &pSMBr);
1da177e4
LT
3772 if (rc)
3773 return rc;
3774 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3775 name_len =
acbbb76a
SF
3776 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
3777 PATH_MAX, nls_codepage, remap);
1da177e4
LT
3778 name_len++; /* trailing null */
3779 name_len *= 2;
50c2f753 3780 } else { /* BB improve the check for buffer overruns BB */
1da177e4
LT
3781 name_len = strnlen(fileName, PATH_MAX);
3782 name_len++; /* trailing null */
3783 strncpy(pSMB->FileName, fileName, name_len);
3784 }
3785 params = 6 + name_len;
3786 pSMB->MaxParameterCount = cpu_to_le16(2);
582d21e5
SF
3787 /* BB find max SMB size from sess */
3788 pSMB->MaxDataCount = cpu_to_le16(1000);
1da177e4
LT
3789 pSMB->MaxSetupCount = 0;
3790 pSMB->Reserved = 0;
3791 pSMB->Flags = 0;
3792 pSMB->Timeout = 0;
3793 pSMB->Reserved2 = 0;
3794 param_offset = offsetof(struct smb_com_transaction2_spi_req,
50c2f753 3795 InformationLevel) - 4;
1da177e4
LT
3796 offset = param_offset + params;
3797 parm_data = ((char *) &pSMB->hdr.Protocol) + offset;
3798 pSMB->ParameterOffset = cpu_to_le16(param_offset);
3799
3800 /* convert to on the wire format for POSIX ACL */
50c2f753 3801 data_count = ACL_to_cifs_posix(parm_data, local_acl, buflen, acl_type);
1da177e4 3802
790fe579 3803 if (data_count == 0) {
1da177e4
LT
3804 rc = -EOPNOTSUPP;
3805 goto setACLerrorExit;
3806 }
3807 pSMB->DataOffset = cpu_to_le16(offset);
3808 pSMB->SetupCount = 1;
3809 pSMB->Reserved3 = 0;
3810 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
3811 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_ACL);
3812 byte_count = 3 /* pad */ + params + data_count;
3813 pSMB->DataCount = cpu_to_le16(data_count);
3814 pSMB->TotalDataCount = pSMB->DataCount;
3815 pSMB->ParameterCount = cpu_to_le16(params);
3816 pSMB->TotalParameterCount = pSMB->ParameterCount;
3817 pSMB->Reserved4 = 0;
be8e3b00 3818 inc_rfc1001_len(pSMB, byte_count);
1da177e4
LT
3819 pSMB->ByteCount = cpu_to_le16(byte_count);
3820 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
50c2f753 3821 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
ad7a2926 3822 if (rc)
f96637be 3823 cifs_dbg(FYI, "Set POSIX ACL returned %d\n", rc);
1da177e4
LT
3824
3825setACLerrorExit:
3826 cifs_buf_release(pSMB);
3827 if (rc == -EAGAIN)
3828 goto setAclRetry;
3829 return rc;
3830}
3831
f654bac2
SF
3832/* BB fix tabs in this function FIXME BB */
3833int
6d5786a3 3834CIFSGetExtAttr(const unsigned int xid, struct cifs_tcon *tcon,
ad7a2926 3835 const int netfid, __u64 *pExtAttrBits, __u64 *pMask)
f654bac2 3836{
50c2f753
SF
3837 int rc = 0;
3838 struct smb_t2_qfi_req *pSMB = NULL;
3839 struct smb_t2_qfi_rsp *pSMBr = NULL;
3840 int bytes_returned;
3841 __u16 params, byte_count;
f654bac2 3842
f96637be 3843 cifs_dbg(FYI, "In GetExtAttr\n");
790fe579
SF
3844 if (tcon == NULL)
3845 return -ENODEV;
f654bac2
SF
3846
3847GetExtAttrRetry:
790fe579
SF
3848 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3849 (void **) &pSMBr);
3850 if (rc)
3851 return rc;
f654bac2 3852
ad7a2926 3853 params = 2 /* level */ + 2 /* fid */;
790fe579
SF
3854 pSMB->t2.TotalDataCount = 0;
3855 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
3856 /* BB find exact max data count below from sess structure BB */
3857 pSMB->t2.MaxDataCount = cpu_to_le16(4000);
3858 pSMB->t2.MaxSetupCount = 0;
3859 pSMB->t2.Reserved = 0;
3860 pSMB->t2.Flags = 0;
3861 pSMB->t2.Timeout = 0;
3862 pSMB->t2.Reserved2 = 0;
3863 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
3864 Fid) - 4);
3865 pSMB->t2.DataCount = 0;
3866 pSMB->t2.DataOffset = 0;
3867 pSMB->t2.SetupCount = 1;
3868 pSMB->t2.Reserved3 = 0;
3869 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
3870 byte_count = params + 1 /* pad */ ;
3871 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
3872 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
3873 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_ATTR_FLAGS);
3874 pSMB->Pad = 0;
f654bac2 3875 pSMB->Fid = netfid;
be8e3b00 3876 inc_rfc1001_len(pSMB, byte_count);
790fe579
SF
3877 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
3878
3879 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3880 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3881 if (rc) {
f96637be 3882 cifs_dbg(FYI, "error %d in GetExtAttr\n", rc);
790fe579
SF
3883 } else {
3884 /* decode response */
3885 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
790fe579 3886 /* BB also check enough total bytes returned */
820a803f 3887 if (rc || get_bcc(&pSMBr->hdr) < 2)
790fe579
SF
3888 /* If rc should we check for EOPNOSUPP and
3889 disable the srvino flag? or in caller? */
3890 rc = -EIO; /* bad smb */
3891 else {
3892 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3893 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3894 struct file_chattr_info *pfinfo;
3895 /* BB Do we need a cast or hash here ? */
3896 if (count != 16) {
f96637be 3897 cifs_dbg(FYI, "Illegal size ret in GetExtAttr\n");
790fe579
SF
3898 rc = -EIO;
3899 goto GetExtAttrOut;
3900 }
3901 pfinfo = (struct file_chattr_info *)
3902 (data_offset + (char *) &pSMBr->hdr.Protocol);
3903 *pExtAttrBits = le64_to_cpu(pfinfo->mode);
f654bac2 3904 *pMask = le64_to_cpu(pfinfo->mask);
790fe579
SF
3905 }
3906 }
f654bac2 3907GetExtAttrOut:
790fe579
SF
3908 cifs_buf_release(pSMB);
3909 if (rc == -EAGAIN)
3910 goto GetExtAttrRetry;
3911 return rc;
f654bac2
SF
3912}
3913
f654bac2 3914#endif /* CONFIG_POSIX */
1da177e4 3915
79df1bae
JL
3916#ifdef CONFIG_CIFS_ACL
3917/*
3918 * Initialize NT TRANSACT SMB into small smb request buffer. This assumes that
3919 * all NT TRANSACTS that we init here have total parm and data under about 400
3920 * bytes (to fit in small cifs buffer size), which is the case so far, it
3921 * easily fits. NB: Setup words themselves and ByteCount MaxSetupCount (size of
3922 * returned setup area) and MaxParameterCount (returned parms size) must be set
3923 * by caller
3924 */
3925static int
3926smb_init_nttransact(const __u16 sub_command, const int setup_count,
96daf2b0 3927 const int parm_len, struct cifs_tcon *tcon,
79df1bae
JL
3928 void **ret_buf)
3929{
3930 int rc;
3931 __u32 temp_offset;
3932 struct smb_com_ntransact_req *pSMB;
3933
3934 rc = small_smb_init(SMB_COM_NT_TRANSACT, 19 + setup_count, tcon,
3935 (void **)&pSMB);
3936 if (rc)
3937 return rc;
3938 *ret_buf = (void *)pSMB;
3939 pSMB->Reserved = 0;
3940 pSMB->TotalParameterCount = cpu_to_le32(parm_len);
3941 pSMB->TotalDataCount = 0;
c974befa 3942 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
79df1bae
JL
3943 pSMB->ParameterCount = pSMB->TotalParameterCount;
3944 pSMB->DataCount = pSMB->TotalDataCount;
3945 temp_offset = offsetof(struct smb_com_ntransact_req, Parms) +
3946 (setup_count * 2) - 4 /* for rfc1001 length itself */;
3947 pSMB->ParameterOffset = cpu_to_le32(temp_offset);
3948 pSMB->DataOffset = cpu_to_le32(temp_offset + parm_len);
3949 pSMB->SetupCount = setup_count; /* no need to le convert byte fields */
3950 pSMB->SubCommand = cpu_to_le16(sub_command);
3951 return 0;
3952}
3953
3954static int
3955validate_ntransact(char *buf, char **ppparm, char **ppdata,
3956 __u32 *pparmlen, __u32 *pdatalen)
3957{
3958 char *end_of_smb;
3959 __u32 data_count, data_offset, parm_count, parm_offset;
3960 struct smb_com_ntransact_rsp *pSMBr;
820a803f 3961 u16 bcc;
79df1bae
JL
3962
3963 *pdatalen = 0;
3964 *pparmlen = 0;
3965
3966 if (buf == NULL)
3967 return -EINVAL;
3968
3969 pSMBr = (struct smb_com_ntransact_rsp *)buf;
3970
820a803f
JL
3971 bcc = get_bcc(&pSMBr->hdr);
3972 end_of_smb = 2 /* sizeof byte count */ + bcc +
79df1bae
JL
3973 (char *)&pSMBr->ByteCount;
3974
3975 data_offset = le32_to_cpu(pSMBr->DataOffset);
3976 data_count = le32_to_cpu(pSMBr->DataCount);
3977 parm_offset = le32_to_cpu(pSMBr->ParameterOffset);
3978 parm_count = le32_to_cpu(pSMBr->ParameterCount);
3979
3980 *ppparm = (char *)&pSMBr->hdr.Protocol + parm_offset;
3981 *ppdata = (char *)&pSMBr->hdr.Protocol + data_offset;
3982
3983 /* should we also check that parm and data areas do not overlap? */
3984 if (*ppparm > end_of_smb) {
f96637be 3985 cifs_dbg(FYI, "parms start after end of smb\n");
79df1bae
JL
3986 return -EINVAL;
3987 } else if (parm_count + *ppparm > end_of_smb) {
f96637be 3988 cifs_dbg(FYI, "parm end after end of smb\n");
79df1bae
JL
3989 return -EINVAL;
3990 } else if (*ppdata > end_of_smb) {
f96637be 3991 cifs_dbg(FYI, "data starts after end of smb\n");
79df1bae
JL
3992 return -EINVAL;
3993 } else if (data_count + *ppdata > end_of_smb) {
f96637be
JP
3994 cifs_dbg(FYI, "data %p + count %d (%p) past smb end %p start %p\n",
3995 *ppdata, data_count, (data_count + *ppdata),
3996 end_of_smb, pSMBr);
79df1bae 3997 return -EINVAL;
820a803f 3998 } else if (parm_count + data_count > bcc) {
f96637be 3999 cifs_dbg(FYI, "parm count and data count larger than SMB\n");
79df1bae
JL
4000 return -EINVAL;
4001 }
4002 *pdatalen = data_count;
4003 *pparmlen = parm_count;
4004 return 0;
4005}
4006
0a4b92c0
SF
4007/* Get Security Descriptor (by handle) from remote server for a file or dir */
4008int
6d5786a3 4009CIFSSMBGetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
630f3f0c 4010 struct cifs_ntsd **acl_inf, __u32 *pbuflen)
0a4b92c0
SF
4011{
4012 int rc = 0;
4013 int buf_type = 0;
ad7a2926 4014 QUERY_SEC_DESC_REQ *pSMB;
0a4b92c0 4015 struct kvec iov[1];
da502f7d 4016 struct kvec rsp_iov;
0a4b92c0 4017
f96637be 4018 cifs_dbg(FYI, "GetCifsACL\n");
0a4b92c0 4019
630f3f0c
SF
4020 *pbuflen = 0;
4021 *acl_inf = NULL;
4022
b9c7a2bb 4023 rc = smb_init_nttransact(NT_TRANSACT_QUERY_SECURITY_DESC, 0,
0a4b92c0
SF
4024 8 /* parm len */, tcon, (void **) &pSMB);
4025 if (rc)
4026 return rc;
4027
4028 pSMB->MaxParameterCount = cpu_to_le32(4);
4029 /* BB TEST with big acls that might need to be e.g. larger than 16K */
4030 pSMB->MaxSetupCount = 0;
4031 pSMB->Fid = fid; /* file handle always le */
4032 pSMB->AclFlags = cpu_to_le32(CIFS_ACL_OWNER | CIFS_ACL_GROUP |
4033 CIFS_ACL_DACL);
4034 pSMB->ByteCount = cpu_to_le16(11); /* 3 bytes pad + 8 bytes parm */
be8e3b00 4035 inc_rfc1001_len(pSMB, 11);
0a4b92c0 4036 iov[0].iov_base = (char *)pSMB;
be8e3b00 4037 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
0a4b92c0 4038
a761ac57 4039 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovec */, &buf_type,
da502f7d
PS
4040 0, &rsp_iov);
4041 cifs_small_buf_release(pSMB);
44c58186 4042 cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
0a4b92c0 4043 if (rc) {
f96637be 4044 cifs_dbg(FYI, "Send error in QuerySecDesc = %d\n", rc);
0a4b92c0 4045 } else { /* decode response */
ad7a2926 4046 __le32 *parm;
630f3f0c
SF
4047 __u32 parm_len;
4048 __u32 acl_len;
50c2f753 4049 struct smb_com_ntransact_rsp *pSMBr;
630f3f0c 4050 char *pdata;
0a4b92c0
SF
4051
4052/* validate_nttransact */
da502f7d 4053 rc = validate_ntransact(rsp_iov.iov_base, (char **)&parm,
630f3f0c 4054 &pdata, &parm_len, pbuflen);
790fe579 4055 if (rc)
0a4b92c0 4056 goto qsec_out;
da502f7d 4057 pSMBr = (struct smb_com_ntransact_rsp *)rsp_iov.iov_base;
0a4b92c0 4058
f96637be
JP
4059 cifs_dbg(FYI, "smb %p parm %p data %p\n",
4060 pSMBr, parm, *acl_inf);
0a4b92c0
SF
4061
4062 if (le32_to_cpu(pSMBr->ParameterCount) != 4) {
4063 rc = -EIO; /* bad smb */
630f3f0c 4064 *pbuflen = 0;
0a4b92c0
SF
4065 goto qsec_out;
4066 }
4067
4068/* BB check that data area is minimum length and as big as acl_len */
4069
af6f4612 4070 acl_len = le32_to_cpu(*parm);
630f3f0c 4071 if (acl_len != *pbuflen) {
f96637be
JP
4072 cifs_dbg(VFS, "acl length %d does not match %d\n",
4073 acl_len, *pbuflen);
630f3f0c
SF
4074 if (*pbuflen > acl_len)
4075 *pbuflen = acl_len;
4076 }
0a4b92c0 4077
630f3f0c
SF
4078 /* check if buffer is big enough for the acl
4079 header followed by the smallest SID */
4080 if ((*pbuflen < sizeof(struct cifs_ntsd) + 8) ||
4081 (*pbuflen >= 64 * 1024)) {
f96637be 4082 cifs_dbg(VFS, "bad acl length %d\n", *pbuflen);
630f3f0c
SF
4083 rc = -EINVAL;
4084 *pbuflen = 0;
4085 } else {
f7f7c185 4086 *acl_inf = kmemdup(pdata, *pbuflen, GFP_KERNEL);
630f3f0c
SF
4087 if (*acl_inf == NULL) {
4088 *pbuflen = 0;
4089 rc = -ENOMEM;
4090 }
630f3f0c 4091 }
0a4b92c0
SF
4092 }
4093qsec_out:
da502f7d 4094 free_rsp_buf(buf_type, rsp_iov.iov_base);
0a4b92c0
SF
4095 return rc;
4096}
97837582
SF
4097
4098int
6d5786a3 4099CIFSSMBSetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
a5ff3769 4100 struct cifs_ntsd *pntsd, __u32 acllen, int aclflag)
97837582
SF
4101{
4102 __u16 byte_count, param_count, data_count, param_offset, data_offset;
4103 int rc = 0;
4104 int bytes_returned = 0;
4105 SET_SEC_DESC_REQ *pSMB = NULL;
b2a3ad9c 4106 void *pSMBr;
97837582
SF
4107
4108setCifsAclRetry:
b2a3ad9c 4109 rc = smb_init(SMB_COM_NT_TRANSACT, 19, tcon, (void **) &pSMB, &pSMBr);
97837582 4110 if (rc)
b2a3ad9c 4111 return rc;
97837582
SF
4112
4113 pSMB->MaxSetupCount = 0;
4114 pSMB->Reserved = 0;
4115
4116 param_count = 8;
4117 param_offset = offsetof(struct smb_com_transaction_ssec_req, Fid) - 4;
4118 data_count = acllen;
4119 data_offset = param_offset + param_count;
4120 byte_count = 3 /* pad */ + param_count;
4121
4122 pSMB->DataCount = cpu_to_le32(data_count);
4123 pSMB->TotalDataCount = pSMB->DataCount;
4124 pSMB->MaxParameterCount = cpu_to_le32(4);
4125 pSMB->MaxDataCount = cpu_to_le32(16384);
4126 pSMB->ParameterCount = cpu_to_le32(param_count);
4127 pSMB->ParameterOffset = cpu_to_le32(param_offset);
4128 pSMB->TotalParameterCount = pSMB->ParameterCount;
4129 pSMB->DataOffset = cpu_to_le32(data_offset);
4130 pSMB->SetupCount = 0;
4131 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_SET_SECURITY_DESC);
4132 pSMB->ByteCount = cpu_to_le16(byte_count+data_count);
4133
4134 pSMB->Fid = fid; /* file handle always le */
4135 pSMB->Reserved2 = 0;
a5ff3769 4136 pSMB->AclFlags = cpu_to_le32(aclflag);
97837582
SF
4137
4138 if (pntsd && acllen) {
b2a3ad9c
JL
4139 memcpy((char *)pSMBr + offsetof(struct smb_hdr, Protocol) +
4140 data_offset, pntsd, acllen);
be8e3b00 4141 inc_rfc1001_len(pSMB, byte_count + data_count);
97837582 4142 } else
be8e3b00 4143 inc_rfc1001_len(pSMB, byte_count);
97837582
SF
4144
4145 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4146 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4147
f96637be
JP
4148 cifs_dbg(FYI, "SetCIFSACL bytes_returned: %d, rc: %d\n",
4149 bytes_returned, rc);
97837582 4150 if (rc)
f96637be 4151 cifs_dbg(FYI, "Set CIFS ACL returned %d\n", rc);
97837582
SF
4152 cifs_buf_release(pSMB);
4153
4154 if (rc == -EAGAIN)
4155 goto setCifsAclRetry;
4156
4157 return (rc);
4158}
4159
79df1bae 4160#endif /* CONFIG_CIFS_ACL */
0a4b92c0 4161
6b8edfe0
SF
4162/* Legacy Query Path Information call for lookup to old servers such
4163 as Win9x/WinME */
68889f26
PS
4164int
4165SMBQueryInformation(const unsigned int xid, struct cifs_tcon *tcon,
4166 const char *search_name, FILE_ALL_INFO *data,
4167 const struct nls_table *nls_codepage, int remap)
6b8edfe0 4168{
ad7a2926
SF
4169 QUERY_INFORMATION_REQ *pSMB;
4170 QUERY_INFORMATION_RSP *pSMBr;
6b8edfe0
SF
4171 int rc = 0;
4172 int bytes_returned;
4173 int name_len;
4174
f96637be 4175 cifs_dbg(FYI, "In SMBQPath path %s\n", search_name);
6b8edfe0
SF
4176QInfRetry:
4177 rc = smb_init(SMB_COM_QUERY_INFORMATION, 0, tcon, (void **) &pSMB,
50c2f753 4178 (void **) &pSMBr);
6b8edfe0
SF
4179 if (rc)
4180 return rc;
4181
4182 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4183 name_len =
acbbb76a 4184 cifsConvertToUTF16((__le16 *) pSMB->FileName,
68889f26 4185 search_name, PATH_MAX, nls_codepage,
acbbb76a 4186 remap);
6b8edfe0
SF
4187 name_len++; /* trailing null */
4188 name_len *= 2;
50c2f753 4189 } else {
68889f26 4190 name_len = strnlen(search_name, PATH_MAX);
6b8edfe0 4191 name_len++; /* trailing null */
68889f26 4192 strncpy(pSMB->FileName, search_name, name_len);
6b8edfe0
SF
4193 }
4194 pSMB->BufferFormat = 0x04;
50c2f753 4195 name_len++; /* account for buffer type byte */
be8e3b00 4196 inc_rfc1001_len(pSMB, (__u16)name_len);
6b8edfe0
SF
4197 pSMB->ByteCount = cpu_to_le16(name_len);
4198
4199 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
50c2f753 4200 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
6b8edfe0 4201 if (rc) {
f96637be 4202 cifs_dbg(FYI, "Send error in QueryInfo = %d\n", rc);
68889f26 4203 } else if (data) {
95390201 4204 struct timespec64 ts;
1bd5bbcb 4205 __u32 time = le32_to_cpu(pSMBr->last_write_time);
ad7a2926
SF
4206
4207 /* decode response */
1bd5bbcb 4208 /* BB FIXME - add time zone adjustment BB */
68889f26 4209 memset(data, 0, sizeof(FILE_ALL_INFO));
1bd5bbcb
SF
4210 ts.tv_nsec = 0;
4211 ts.tv_sec = time;
4212 /* decode time fields */
68889f26
PS
4213 data->ChangeTime = cpu_to_le64(cifs_UnixTimeToNT(ts));
4214 data->LastWriteTime = data->ChangeTime;
4215 data->LastAccessTime = 0;
4216 data->AllocationSize =
70ca734a 4217 cpu_to_le64(le32_to_cpu(pSMBr->size));
68889f26
PS
4218 data->EndOfFile = data->AllocationSize;
4219 data->Attributes =
70ca734a 4220 cpu_to_le32(le16_to_cpu(pSMBr->attr));
6b8edfe0
SF
4221 } else
4222 rc = -EIO; /* bad buffer passed in */
4223
4224 cifs_buf_release(pSMB);
4225
4226 if (rc == -EAGAIN)
4227 goto QInfRetry;
4228
4229 return rc;
4230}
4231
bcd5357f 4232int
6d5786a3 4233CIFSSMBQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
bcd5357f
JL
4234 u16 netfid, FILE_ALL_INFO *pFindData)
4235{
4236 struct smb_t2_qfi_req *pSMB = NULL;
4237 struct smb_t2_qfi_rsp *pSMBr = NULL;
4238 int rc = 0;
4239 int bytes_returned;
4240 __u16 params, byte_count;
4241
4242QFileInfoRetry:
4243 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4244 (void **) &pSMBr);
4245 if (rc)
4246 return rc;
4247
4248 params = 2 /* level */ + 2 /* fid */;
4249 pSMB->t2.TotalDataCount = 0;
4250 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4251 /* BB find exact max data count below from sess structure BB */
4252 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4253 pSMB->t2.MaxSetupCount = 0;
4254 pSMB->t2.Reserved = 0;
4255 pSMB->t2.Flags = 0;
4256 pSMB->t2.Timeout = 0;
4257 pSMB->t2.Reserved2 = 0;
4258 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4259 Fid) - 4);
4260 pSMB->t2.DataCount = 0;
4261 pSMB->t2.DataOffset = 0;
4262 pSMB->t2.SetupCount = 1;
4263 pSMB->t2.Reserved3 = 0;
4264 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4265 byte_count = params + 1 /* pad */ ;
4266 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4267 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4268 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
4269 pSMB->Pad = 0;
4270 pSMB->Fid = netfid;
be8e3b00 4271 inc_rfc1001_len(pSMB, byte_count);
7ac0febb 4272 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
6b8edfe0 4273
bcd5357f
JL
4274 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4275 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4276 if (rc) {
ebcc943c 4277 cifs_dbg(FYI, "Send error in QFileInfo = %d", rc);
bcd5357f
JL
4278 } else { /* decode response */
4279 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
6b8edfe0 4280
bcd5357f
JL
4281 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4282 rc = -EIO;
820a803f 4283 else if (get_bcc(&pSMBr->hdr) < 40)
bcd5357f
JL
4284 rc = -EIO; /* bad smb */
4285 else if (pFindData) {
4286 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4287 memcpy((char *) pFindData,
4288 (char *) &pSMBr->hdr.Protocol +
4289 data_offset, sizeof(FILE_ALL_INFO));
4290 } else
4291 rc = -ENOMEM;
4292 }
4293 cifs_buf_release(pSMB);
4294 if (rc == -EAGAIN)
4295 goto QFileInfoRetry;
6b8edfe0 4296
bcd5357f
JL
4297 return rc;
4298}
6b8edfe0 4299
1da177e4 4300int
6d5786a3 4301CIFSSMBQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
68889f26 4302 const char *search_name, FILE_ALL_INFO *data,
acf1a1b1 4303 int legacy /* old style infolevel */,
737b758c 4304 const struct nls_table *nls_codepage, int remap)
1da177e4 4305{
68889f26 4306 /* level 263 SMB_QUERY_FILE_ALL_INFO */
1da177e4
LT
4307 TRANSACTION2_QPI_REQ *pSMB = NULL;
4308 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4309 int rc = 0;
4310 int bytes_returned;
4311 int name_len;
4312 __u16 params, byte_count;
4313
f96637be 4314 /* cifs_dbg(FYI, "In QPathInfo path %s\n", search_name); */
1da177e4
LT
4315QPathInfoRetry:
4316 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4317 (void **) &pSMBr);
4318 if (rc)
4319 return rc;
4320
4321 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4322 name_len =
68889f26 4323 cifsConvertToUTF16((__le16 *) pSMB->FileName, search_name,
acbbb76a 4324 PATH_MAX, nls_codepage, remap);
1da177e4
LT
4325 name_len++; /* trailing null */
4326 name_len *= 2;
50c2f753 4327 } else { /* BB improve the check for buffer overruns BB */
68889f26 4328 name_len = strnlen(search_name, PATH_MAX);
1da177e4 4329 name_len++; /* trailing null */
68889f26 4330 strncpy(pSMB->FileName, search_name, name_len);
1da177e4
LT
4331 }
4332
50c2f753 4333 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
1da177e4
LT
4334 pSMB->TotalDataCount = 0;
4335 pSMB->MaxParameterCount = cpu_to_le16(2);
582d21e5
SF
4336 /* BB find exact max SMB PDU from sess structure BB */
4337 pSMB->MaxDataCount = cpu_to_le16(4000);
1da177e4
LT
4338 pSMB->MaxSetupCount = 0;
4339 pSMB->Reserved = 0;
4340 pSMB->Flags = 0;
4341 pSMB->Timeout = 0;
4342 pSMB->Reserved2 = 0;
4343 pSMB->ParameterOffset = cpu_to_le16(offsetof(
50c2f753 4344 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
1da177e4
LT
4345 pSMB->DataCount = 0;
4346 pSMB->DataOffset = 0;
4347 pSMB->SetupCount = 1;
4348 pSMB->Reserved3 = 0;
4349 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4350 byte_count = params + 1 /* pad */ ;
4351 pSMB->TotalParameterCount = cpu_to_le16(params);
4352 pSMB->ParameterCount = pSMB->TotalParameterCount;
790fe579 4353 if (legacy)
acf1a1b1
SF
4354 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_STANDARD);
4355 else
4356 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
1da177e4 4357 pSMB->Reserved4 = 0;
be8e3b00 4358 inc_rfc1001_len(pSMB, byte_count);
1da177e4
LT
4359 pSMB->ByteCount = cpu_to_le16(byte_count);
4360
4361 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4362 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4363 if (rc) {
f96637be 4364 cifs_dbg(FYI, "Send error in QPathInfo = %d\n", rc);
1da177e4
LT
4365 } else { /* decode response */
4366 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4367
acf1a1b1
SF
4368 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4369 rc = -EIO;
820a803f 4370 else if (!legacy && get_bcc(&pSMBr->hdr) < 40)
1da177e4 4371 rc = -EIO; /* bad smb */
820a803f 4372 else if (legacy && get_bcc(&pSMBr->hdr) < 24)
50c2f753
SF
4373 rc = -EIO; /* 24 or 26 expected but we do not read
4374 last field */
68889f26 4375 else if (data) {
acf1a1b1 4376 int size;
1da177e4 4377 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
ad7a2926 4378
68889f26
PS
4379 /*
4380 * On legacy responses we do not read the last field,
4381 * EAsize, fortunately since it varies by subdialect and
4382 * also note it differs on Set vs Get, ie two bytes or 4
4383 * bytes depending but we don't care here.
4384 */
ad7a2926 4385 if (legacy)
acf1a1b1
SF
4386 size = sizeof(FILE_INFO_STANDARD);
4387 else
4388 size = sizeof(FILE_ALL_INFO);
68889f26 4389 memcpy((char *) data, (char *) &pSMBr->hdr.Protocol +
acf1a1b1 4390 data_offset, size);
1da177e4
LT
4391 } else
4392 rc = -ENOMEM;
4393 }
4394 cifs_buf_release(pSMB);
4395 if (rc == -EAGAIN)
4396 goto QPathInfoRetry;
4397
4398 return rc;
4399}
4400
c8634fd3 4401int
6d5786a3 4402CIFSSMBUnixQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
c8634fd3
JL
4403 u16 netfid, FILE_UNIX_BASIC_INFO *pFindData)
4404{
4405 struct smb_t2_qfi_req *pSMB = NULL;
4406 struct smb_t2_qfi_rsp *pSMBr = NULL;
4407 int rc = 0;
4408 int bytes_returned;
4409 __u16 params, byte_count;
4410
4411UnixQFileInfoRetry:
4412 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4413 (void **) &pSMBr);
4414 if (rc)
4415 return rc;
4416
4417 params = 2 /* level */ + 2 /* fid */;
4418 pSMB->t2.TotalDataCount = 0;
4419 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4420 /* BB find exact max data count below from sess structure BB */
4421 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4422 pSMB->t2.MaxSetupCount = 0;
4423 pSMB->t2.Reserved = 0;
4424 pSMB->t2.Flags = 0;
4425 pSMB->t2.Timeout = 0;
4426 pSMB->t2.Reserved2 = 0;
4427 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4428 Fid) - 4);
4429 pSMB->t2.DataCount = 0;
4430 pSMB->t2.DataOffset = 0;
4431 pSMB->t2.SetupCount = 1;
4432 pSMB->t2.Reserved3 = 0;
4433 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4434 byte_count = params + 1 /* pad */ ;
4435 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4436 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4437 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4438 pSMB->Pad = 0;
4439 pSMB->Fid = netfid;
be8e3b00 4440 inc_rfc1001_len(pSMB, byte_count);
7ac0febb 4441 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
c8634fd3
JL
4442
4443 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4444 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4445 if (rc) {
ebcc943c 4446 cifs_dbg(FYI, "Send error in UnixQFileInfo = %d", rc);
c8634fd3
JL
4447 } else { /* decode response */
4448 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4449
820a803f 4450 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
f96637be 4451 cifs_dbg(VFS, "Malformed FILE_UNIX_BASIC_INFO response. Unix Extensions can be disabled on mount by specifying the nosfu mount option.\n");
c8634fd3
JL
4452 rc = -EIO; /* bad smb */
4453 } else {
4454 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4455 memcpy((char *) pFindData,
4456 (char *) &pSMBr->hdr.Protocol +
4457 data_offset,
4458 sizeof(FILE_UNIX_BASIC_INFO));
4459 }
4460 }
4461
4462 cifs_buf_release(pSMB);
4463 if (rc == -EAGAIN)
4464 goto UnixQFileInfoRetry;
4465
4466 return rc;
4467}
4468
1da177e4 4469int
6d5786a3 4470CIFSSMBUnixQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
1da177e4 4471 const unsigned char *searchName,
582d21e5 4472 FILE_UNIX_BASIC_INFO *pFindData,
737b758c 4473 const struct nls_table *nls_codepage, int remap)
1da177e4
LT
4474{
4475/* SMB_QUERY_FILE_UNIX_BASIC */
4476 TRANSACTION2_QPI_REQ *pSMB = NULL;
4477 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4478 int rc = 0;
4479 int bytes_returned = 0;
4480 int name_len;
4481 __u16 params, byte_count;
4482
f96637be 4483 cifs_dbg(FYI, "In QPathInfo (Unix) the path %s\n", searchName);
1da177e4
LT
4484UnixQPathInfoRetry:
4485 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4486 (void **) &pSMBr);
4487 if (rc)
4488 return rc;
4489
4490 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4491 name_len =
acbbb76a
SF
4492 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4493 PATH_MAX, nls_codepage, remap);
1da177e4
LT
4494 name_len++; /* trailing null */
4495 name_len *= 2;
50c2f753 4496 } else { /* BB improve the check for buffer overruns BB */
1da177e4
LT
4497 name_len = strnlen(searchName, PATH_MAX);
4498 name_len++; /* trailing null */
4499 strncpy(pSMB->FileName, searchName, name_len);
4500 }
4501
50c2f753 4502 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
1da177e4
LT
4503 pSMB->TotalDataCount = 0;
4504 pSMB->MaxParameterCount = cpu_to_le16(2);
4505 /* BB find exact max SMB PDU from sess structure BB */
50c2f753 4506 pSMB->MaxDataCount = cpu_to_le16(4000);
1da177e4
LT
4507 pSMB->MaxSetupCount = 0;
4508 pSMB->Reserved = 0;
4509 pSMB->Flags = 0;
4510 pSMB->Timeout = 0;
4511 pSMB->Reserved2 = 0;
4512 pSMB->ParameterOffset = cpu_to_le16(offsetof(
50c2f753 4513 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
1da177e4
LT
4514 pSMB->DataCount = 0;
4515 pSMB->DataOffset = 0;
4516 pSMB->SetupCount = 1;
4517 pSMB->Reserved3 = 0;
4518 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4519 byte_count = params + 1 /* pad */ ;
4520 pSMB->TotalParameterCount = cpu_to_le16(params);
4521 pSMB->ParameterCount = pSMB->TotalParameterCount;
4522 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4523 pSMB->Reserved4 = 0;
be8e3b00 4524 inc_rfc1001_len(pSMB, byte_count);
1da177e4
LT
4525 pSMB->ByteCount = cpu_to_le16(byte_count);
4526
4527 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4528 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4529 if (rc) {
ebcc943c 4530 cifs_dbg(FYI, "Send error in UnixQPathInfo = %d", rc);
1da177e4
LT
4531 } else { /* decode response */
4532 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4533
820a803f 4534 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
f96637be 4535 cifs_dbg(VFS, "Malformed FILE_UNIX_BASIC_INFO response. Unix Extensions can be disabled on mount by specifying the nosfu mount option.\n");
1da177e4
LT
4536 rc = -EIO; /* bad smb */
4537 } else {
4538 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4539 memcpy((char *) pFindData,
4540 (char *) &pSMBr->hdr.Protocol +
4541 data_offset,
630f3f0c 4542 sizeof(FILE_UNIX_BASIC_INFO));
1da177e4
LT
4543 }
4544 }
4545 cifs_buf_release(pSMB);
4546 if (rc == -EAGAIN)
4547 goto UnixQPathInfoRetry;
4548
4549 return rc;
4550}
4551
1da177e4
LT
4552/* xid, tcon, searchName and codepage are input parms, rest are returned */
4553int
6d5786a3 4554CIFSFindFirst(const unsigned int xid, struct cifs_tcon *tcon,
c052e2b4 4555 const char *searchName, struct cifs_sb_info *cifs_sb,
2608bee7 4556 __u16 *pnetfid, __u16 search_flags,
c052e2b4 4557 struct cifs_search_info *psrch_inf, bool msearch)
1da177e4
LT
4558{
4559/* level 257 SMB_ */
4560 TRANSACTION2_FFIRST_REQ *pSMB = NULL;
4561 TRANSACTION2_FFIRST_RSP *pSMBr = NULL;
ad7a2926 4562 T2_FFIRST_RSP_PARMS *parms;
1da177e4
LT
4563 int rc = 0;
4564 int bytes_returned = 0;
c052e2b4 4565 int name_len, remap;
1da177e4 4566 __u16 params, byte_count;
c052e2b4 4567 struct nls_table *nls_codepage;
1da177e4 4568
f96637be 4569 cifs_dbg(FYI, "In FindFirst for %s\n", searchName);
1da177e4
LT
4570
4571findFirstRetry:
4572 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4573 (void **) &pSMBr);
4574 if (rc)
4575 return rc;
4576
c052e2b4 4577 nls_codepage = cifs_sb->local_nls;
2baa2682 4578 remap = cifs_remap(cifs_sb);
c052e2b4 4579
1da177e4
LT
4580 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4581 name_len =
acbbb76a
SF
4582 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4583 PATH_MAX, nls_codepage, remap);
737b758c
SF
4584 /* We can not add the asterik earlier in case
4585 it got remapped to 0xF03A as if it were part of the
4586 directory name instead of a wildcard */
1da177e4 4587 name_len *= 2;
c052e2b4
SP
4588 if (msearch) {
4589 pSMB->FileName[name_len] = CIFS_DIR_SEP(cifs_sb);
4590 pSMB->FileName[name_len+1] = 0;
4591 pSMB->FileName[name_len+2] = '*';
4592 pSMB->FileName[name_len+3] = 0;
4593 name_len += 4; /* now the trailing null */
4594 /* null terminate just in case */
4595 pSMB->FileName[name_len] = 0;
4596 pSMB->FileName[name_len+1] = 0;
4597 name_len += 2;
4598 }
1da177e4
LT
4599 } else { /* BB add check for overrun of SMB buf BB */
4600 name_len = strnlen(searchName, PATH_MAX);
1da177e4 4601/* BB fix here and in unicode clause above ie
790fe579 4602 if (name_len > buffersize-header)
1da177e4
LT
4603 free buffer exit; BB */
4604 strncpy(pSMB->FileName, searchName, name_len);
c052e2b4
SP
4605 if (msearch) {
4606 pSMB->FileName[name_len] = CIFS_DIR_SEP(cifs_sb);
4607 pSMB->FileName[name_len+1] = '*';
4608 pSMB->FileName[name_len+2] = 0;
4609 name_len += 3;
4610 }
1da177e4
LT
4611 }
4612
4613 params = 12 + name_len /* includes null */ ;
4614 pSMB->TotalDataCount = 0; /* no EAs */
4615 pSMB->MaxParameterCount = cpu_to_le16(10);
c974befa 4616 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
1da177e4
LT
4617 pSMB->MaxSetupCount = 0;
4618 pSMB->Reserved = 0;
4619 pSMB->Flags = 0;
4620 pSMB->Timeout = 0;
4621 pSMB->Reserved2 = 0;
4622 byte_count = params + 1 /* pad */ ;
4623 pSMB->TotalParameterCount = cpu_to_le16(params);
4624 pSMB->ParameterCount = pSMB->TotalParameterCount;
4625 pSMB->ParameterOffset = cpu_to_le16(
88274815
SF
4626 offsetof(struct smb_com_transaction2_ffirst_req, SearchAttributes)
4627 - 4);
1da177e4
LT
4628 pSMB->DataCount = 0;
4629 pSMB->DataOffset = 0;
4630 pSMB->SetupCount = 1; /* one byte, no need to make endian neutral */
4631 pSMB->Reserved3 = 0;
4632 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_FIRST);
4633 pSMB->SearchAttributes =
4634 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
4635 ATTR_DIRECTORY);
50c2f753 4636 pSMB->SearchCount = cpu_to_le16(CIFSMaxBufSize/sizeof(FILE_UNIX_INFO));
2608bee7 4637 pSMB->SearchFlags = cpu_to_le16(search_flags);
1da177e4
LT
4638 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4639
4640 /* BB what should we set StorageType to? Does it matter? BB */
4641 pSMB->SearchStorageType = 0;
be8e3b00 4642 inc_rfc1001_len(pSMB, byte_count);
1da177e4
LT
4643 pSMB->ByteCount = cpu_to_le16(byte_count);
4644
4645 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4646 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
44c58186 4647 cifs_stats_inc(&tcon->stats.cifs_stats.num_ffirst);
1da177e4 4648
88274815
SF
4649 if (rc) {/* BB add logic to retry regular search if Unix search
4650 rejected unexpectedly by server */
1da177e4 4651 /* BB Add code to handle unsupported level rc */
f96637be 4652 cifs_dbg(FYI, "Error in FindFirst = %d\n", rc);
1982c344 4653
88274815 4654 cifs_buf_release(pSMB);
1da177e4
LT
4655
4656 /* BB eventually could optimize out free and realloc of buf */
4657 /* for this case */
4658 if (rc == -EAGAIN)
4659 goto findFirstRetry;
4660 } else { /* decode response */
4661 /* BB remember to free buffer if error BB */
4662 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
790fe579 4663 if (rc == 0) {
b77d753c
SF
4664 unsigned int lnoff;
4665
1da177e4 4666 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
4b18f2a9 4667 psrch_inf->unicode = true;
1da177e4 4668 else
4b18f2a9 4669 psrch_inf->unicode = false;
1da177e4
LT
4670
4671 psrch_inf->ntwrk_buf_start = (char *)pSMBr;
d47d7c1a 4672 psrch_inf->smallBuf = 0;
50c2f753
SF
4673 psrch_inf->srch_entries_start =
4674 (char *) &pSMBr->hdr.Protocol +
1da177e4 4675 le16_to_cpu(pSMBr->t2.DataOffset);
1da177e4
LT
4676 parms = (T2_FFIRST_RSP_PARMS *)((char *) &pSMBr->hdr.Protocol +
4677 le16_to_cpu(pSMBr->t2.ParameterOffset));
4678
790fe579 4679 if (parms->EndofSearch)
4b18f2a9 4680 psrch_inf->endOfSearch = true;
1da177e4 4681 else
4b18f2a9 4682 psrch_inf->endOfSearch = false;
1da177e4 4683
50c2f753
SF
4684 psrch_inf->entries_in_buffer =
4685 le16_to_cpu(parms->SearchCount);
60808233 4686 psrch_inf->index_of_last_entry = 2 /* skip . and .. */ +
1da177e4 4687 psrch_inf->entries_in_buffer;
b77d753c 4688 lnoff = le16_to_cpu(parms->LastNameOffset);
c974befa 4689 if (CIFSMaxBufSize < lnoff) {
f96637be 4690 cifs_dbg(VFS, "ignoring corrupt resume name\n");
b77d753c
SF
4691 psrch_inf->last_entry = NULL;
4692 return rc;
4693 }
4694
0752f152 4695 psrch_inf->last_entry = psrch_inf->srch_entries_start +
b77d753c
SF
4696 lnoff;
4697
c052e2b4
SP
4698 if (pnetfid)
4699 *pnetfid = parms->SearchHandle;
1da177e4
LT
4700 } else {
4701 cifs_buf_release(pSMB);
4702 }
4703 }
4704
4705 return rc;
4706}
4707
6d5786a3
PS
4708int CIFSFindNext(const unsigned int xid, struct cifs_tcon *tcon,
4709 __u16 searchHandle, __u16 search_flags,
4710 struct cifs_search_info *psrch_inf)
1da177e4
LT
4711{
4712 TRANSACTION2_FNEXT_REQ *pSMB = NULL;
4713 TRANSACTION2_FNEXT_RSP *pSMBr = NULL;
ad7a2926 4714 T2_FNEXT_RSP_PARMS *parms;
1da177e4
LT
4715 char *response_data;
4716 int rc = 0;
9438fabb
JL
4717 int bytes_returned;
4718 unsigned int name_len;
1da177e4
LT
4719 __u16 params, byte_count;
4720
f96637be 4721 cifs_dbg(FYI, "In FindNext\n");
1da177e4 4722
4b18f2a9 4723 if (psrch_inf->endOfSearch)
1da177e4
LT
4724 return -ENOENT;
4725
4726 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4727 (void **) &pSMBr);
4728 if (rc)
4729 return rc;
4730
50c2f753 4731 params = 14; /* includes 2 bytes of null string, converted to LE below*/
1da177e4
LT
4732 byte_count = 0;
4733 pSMB->TotalDataCount = 0; /* no EAs */
4734 pSMB->MaxParameterCount = cpu_to_le16(8);
c974befa 4735 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
1da177e4
LT
4736 pSMB->MaxSetupCount = 0;
4737 pSMB->Reserved = 0;
4738 pSMB->Flags = 0;
4739 pSMB->Timeout = 0;
4740 pSMB->Reserved2 = 0;
4741 pSMB->ParameterOffset = cpu_to_le16(
4742 offsetof(struct smb_com_transaction2_fnext_req,SearchHandle) - 4);
4743 pSMB->DataCount = 0;
4744 pSMB->DataOffset = 0;
4745 pSMB->SetupCount = 1;
4746 pSMB->Reserved3 = 0;
4747 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_NEXT);
4748 pSMB->SearchHandle = searchHandle; /* always kept as le */
4749 pSMB->SearchCount =
630f3f0c 4750 cpu_to_le16(CIFSMaxBufSize / sizeof(FILE_UNIX_INFO));
1da177e4
LT
4751 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4752 pSMB->ResumeKey = psrch_inf->resume_key;
2608bee7 4753 pSMB->SearchFlags = cpu_to_le16(search_flags);
1da177e4
LT
4754
4755 name_len = psrch_inf->resume_name_len;
4756 params += name_len;
790fe579 4757 if (name_len < PATH_MAX) {
1da177e4
LT
4758 memcpy(pSMB->ResumeFileName, psrch_inf->presume_name, name_len);
4759 byte_count += name_len;
ef6724e3
SF
4760 /* 14 byte parm len above enough for 2 byte null terminator */
4761 pSMB->ResumeFileName[name_len] = 0;
4762 pSMB->ResumeFileName[name_len+1] = 0;
1da177e4
LT
4763 } else {
4764 rc = -EINVAL;
4765 goto FNext2_err_exit;
4766 }
4767 byte_count = params + 1 /* pad */ ;
4768 pSMB->TotalParameterCount = cpu_to_le16(params);
4769 pSMB->ParameterCount = pSMB->TotalParameterCount;
be8e3b00 4770 inc_rfc1001_len(pSMB, byte_count);
1da177e4 4771 pSMB->ByteCount = cpu_to_le16(byte_count);
50c2f753 4772
1da177e4
LT
4773 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4774 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
44c58186 4775 cifs_stats_inc(&tcon->stats.cifs_stats.num_fnext);
1da177e4
LT
4776 if (rc) {
4777 if (rc == -EBADF) {
4b18f2a9 4778 psrch_inf->endOfSearch = true;
6353450a 4779 cifs_buf_release(pSMB);
50c2f753 4780 rc = 0; /* search probably was closed at end of search*/
1da177e4 4781 } else
f96637be 4782 cifs_dbg(FYI, "FindNext returned = %d\n", rc);
1da177e4
LT
4783 } else { /* decode response */
4784 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
50c2f753 4785
790fe579 4786 if (rc == 0) {
b77d753c
SF
4787 unsigned int lnoff;
4788
1da177e4
LT
4789 /* BB fixme add lock for file (srch_info) struct here */
4790 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
4b18f2a9 4791 psrch_inf->unicode = true;
1da177e4 4792 else
4b18f2a9 4793 psrch_inf->unicode = false;
1da177e4
LT
4794 response_data = (char *) &pSMBr->hdr.Protocol +
4795 le16_to_cpu(pSMBr->t2.ParameterOffset);
4796 parms = (T2_FNEXT_RSP_PARMS *)response_data;
4797 response_data = (char *)&pSMBr->hdr.Protocol +
4798 le16_to_cpu(pSMBr->t2.DataOffset);
790fe579 4799 if (psrch_inf->smallBuf)
d47d7c1a
SF
4800 cifs_small_buf_release(
4801 psrch_inf->ntwrk_buf_start);
4802 else
4803 cifs_buf_release(psrch_inf->ntwrk_buf_start);
1da177e4
LT
4804 psrch_inf->srch_entries_start = response_data;
4805 psrch_inf->ntwrk_buf_start = (char *)pSMB;
d47d7c1a 4806 psrch_inf->smallBuf = 0;
790fe579 4807 if (parms->EndofSearch)
4b18f2a9 4808 psrch_inf->endOfSearch = true;
1da177e4 4809 else
4b18f2a9 4810 psrch_inf->endOfSearch = false;
50c2f753
SF
4811 psrch_inf->entries_in_buffer =
4812 le16_to_cpu(parms->SearchCount);
1da177e4
LT
4813 psrch_inf->index_of_last_entry +=
4814 psrch_inf->entries_in_buffer;
b77d753c 4815 lnoff = le16_to_cpu(parms->LastNameOffset);
c974befa 4816 if (CIFSMaxBufSize < lnoff) {
f96637be 4817 cifs_dbg(VFS, "ignoring corrupt resume name\n");
b77d753c
SF
4818 psrch_inf->last_entry = NULL;
4819 return rc;
4820 } else
4821 psrch_inf->last_entry =
4822 psrch_inf->srch_entries_start + lnoff;
4823
f96637be
JP
4824/* cifs_dbg(FYI, "fnxt2 entries in buf %d index_of_last %d\n",
4825 psrch_inf->entries_in_buffer, psrch_inf->index_of_last_entry); */
1da177e4
LT
4826
4827 /* BB fixme add unlock here */
4828 }
4829
4830 }
4831
4832 /* BB On error, should we leave previous search buf (and count and
4833 last entry fields) intact or free the previous one? */
4834
4835 /* Note: On -EAGAIN error only caller can retry on handle based calls
4836 since file handle passed in no longer valid */
4837FNext2_err_exit:
4838 if (rc != 0)
4839 cifs_buf_release(pSMB);
1da177e4
LT
4840 return rc;
4841}
4842
4843int
6d5786a3 4844CIFSFindClose(const unsigned int xid, struct cifs_tcon *tcon,
50c2f753 4845 const __u16 searchHandle)
1da177e4
LT
4846{
4847 int rc = 0;
4848 FINDCLOSE_REQ *pSMB = NULL;
1da177e4 4849
f96637be 4850 cifs_dbg(FYI, "In CIFSSMBFindClose\n");
1da177e4
LT
4851 rc = small_smb_init(SMB_COM_FIND_CLOSE2, 1, tcon, (void **)&pSMB);
4852
4853 /* no sense returning error if session restarted
4854 as file handle has been closed */
790fe579 4855 if (rc == -EAGAIN)
1da177e4
LT
4856 return 0;
4857 if (rc)
4858 return rc;
4859
1da177e4
LT
4860 pSMB->FileID = searchHandle;
4861 pSMB->ByteCount = 0;
792af7b0 4862 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
da502f7d 4863 cifs_small_buf_release(pSMB);
ad7a2926 4864 if (rc)
f96637be 4865 cifs_dbg(VFS, "Send error in FindClose = %d\n", rc);
ad7a2926 4866
44c58186 4867 cifs_stats_inc(&tcon->stats.cifs_stats.num_fclose);
1da177e4
LT
4868
4869 /* Since session is dead, search handle closed on server already */
4870 if (rc == -EAGAIN)
4871 rc = 0;
4872
4873 return rc;
4874}
4875
1da177e4 4876int
6d5786a3 4877CIFSGetSrvInodeNumber(const unsigned int xid, struct cifs_tcon *tcon,
1208ef1f 4878 const char *search_name, __u64 *inode_number,
50c2f753 4879 const struct nls_table *nls_codepage, int remap)
1da177e4
LT
4880{
4881 int rc = 0;
4882 TRANSACTION2_QPI_REQ *pSMB = NULL;
4883 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4884 int name_len, bytes_returned;
4885 __u16 params, byte_count;
4886
f96637be 4887 cifs_dbg(FYI, "In GetSrvInodeNum for %s\n", search_name);
790fe579 4888 if (tcon == NULL)
50c2f753 4889 return -ENODEV;
1da177e4
LT
4890
4891GetInodeNumberRetry:
4892 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
50c2f753 4893 (void **) &pSMBr);
1da177e4
LT
4894 if (rc)
4895 return rc;
4896
1da177e4
LT
4897 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4898 name_len =
acbbb76a 4899 cifsConvertToUTF16((__le16 *) pSMB->FileName,
1208ef1f 4900 search_name, PATH_MAX, nls_codepage,
acbbb76a 4901 remap);
1da177e4
LT
4902 name_len++; /* trailing null */
4903 name_len *= 2;
50c2f753 4904 } else { /* BB improve the check for buffer overruns BB */
1208ef1f 4905 name_len = strnlen(search_name, PATH_MAX);
1da177e4 4906 name_len++; /* trailing null */
1208ef1f 4907 strncpy(pSMB->FileName, search_name, name_len);
1da177e4
LT
4908 }
4909
4910 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
4911 pSMB->TotalDataCount = 0;
4912 pSMB->MaxParameterCount = cpu_to_le16(2);
4913 /* BB find exact max data count below from sess structure BB */
4914 pSMB->MaxDataCount = cpu_to_le16(4000);
4915 pSMB->MaxSetupCount = 0;
4916 pSMB->Reserved = 0;
4917 pSMB->Flags = 0;
4918 pSMB->Timeout = 0;
4919 pSMB->Reserved2 = 0;
4920 pSMB->ParameterOffset = cpu_to_le16(offsetof(
50c2f753 4921 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
1da177e4
LT
4922 pSMB->DataCount = 0;
4923 pSMB->DataOffset = 0;
4924 pSMB->SetupCount = 1;
4925 pSMB->Reserved3 = 0;
4926 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4927 byte_count = params + 1 /* pad */ ;
4928 pSMB->TotalParameterCount = cpu_to_le16(params);
4929 pSMB->ParameterCount = pSMB->TotalParameterCount;
4930 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_INTERNAL_INFO);
4931 pSMB->Reserved4 = 0;
be8e3b00 4932 inc_rfc1001_len(pSMB, byte_count);
1da177e4
LT
4933 pSMB->ByteCount = cpu_to_le16(byte_count);
4934
4935 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4936 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4937 if (rc) {
f96637be 4938 cifs_dbg(FYI, "error %d in QueryInternalInfo\n", rc);
1da177e4
LT
4939 } else {
4940 /* decode response */
4941 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
1da177e4 4942 /* BB also check enough total bytes returned */
820a803f 4943 if (rc || get_bcc(&pSMBr->hdr) < 2)
1da177e4
LT
4944 /* If rc should we check for EOPNOSUPP and
4945 disable the srvino flag? or in caller? */
4946 rc = -EIO; /* bad smb */
50c2f753 4947 else {
1da177e4
LT
4948 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4949 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
50c2f753 4950 struct file_internal_info *pfinfo;
1da177e4 4951 /* BB Do we need a cast or hash here ? */
790fe579 4952 if (count < 8) {
f96637be 4953 cifs_dbg(FYI, "Illegal size ret in QryIntrnlInf\n");
1da177e4
LT
4954 rc = -EIO;
4955 goto GetInodeNumOut;
4956 }
4957 pfinfo = (struct file_internal_info *)
4958 (data_offset + (char *) &pSMBr->hdr.Protocol);
85a6dac5 4959 *inode_number = le64_to_cpu(pfinfo->UniqueId);
1da177e4
LT
4960 }
4961 }
4962GetInodeNumOut:
4963 cifs_buf_release(pSMB);
4964 if (rc == -EAGAIN)
4965 goto GetInodeNumberRetry;
4966 return rc;
4967}
1da177e4
LT
4968
4969int
6d5786a3 4970CIFSGetDFSRefer(const unsigned int xid, struct cifs_ses *ses,
b669f33c 4971 const char *search_name, struct dfs_info3_param **target_nodes,
c2cf07d5 4972 unsigned int *num_of_nodes,
737b758c 4973 const struct nls_table *nls_codepage, int remap)
1da177e4
LT
4974{
4975/* TRANS2_GET_DFS_REFERRAL */
4976 TRANSACTION2_GET_DFS_REFER_REQ *pSMB = NULL;
4977 TRANSACTION2_GET_DFS_REFER_RSP *pSMBr = NULL;
1da177e4
LT
4978 int rc = 0;
4979 int bytes_returned;
4980 int name_len;
1da177e4 4981 __u16 params, byte_count;
c2cf07d5
SF
4982 *num_of_nodes = 0;
4983 *target_nodes = NULL;
1da177e4 4984
f96637be 4985 cifs_dbg(FYI, "In GetDFSRefer the path %s\n", search_name);
b327a717 4986 if (ses == NULL || ses->tcon_ipc == NULL)
1da177e4 4987 return -ENODEV;
b327a717 4988
1da177e4 4989getDFSRetry:
b327a717 4990 rc = smb_init(SMB_COM_TRANSACTION2, 15, ses->tcon_ipc, (void **) &pSMB,
1da177e4
LT
4991 (void **) &pSMBr);
4992 if (rc)
4993 return rc;
50c2f753
SF
4994
4995 /* server pointer checked in called function,
1982c344 4996 but should never be null here anyway */
88257360 4997 pSMB->hdr.Mid = get_next_mid(ses->server);
b327a717 4998 pSMB->hdr.Tid = ses->tcon_ipc->tid;
1da177e4 4999 pSMB->hdr.Uid = ses->Suid;
26f57364 5000 if (ses->capabilities & CAP_STATUS32)
1da177e4 5001 pSMB->hdr.Flags2 |= SMBFLG2_ERR_STATUS;
26f57364 5002 if (ses->capabilities & CAP_DFS)
1da177e4 5003 pSMB->hdr.Flags2 |= SMBFLG2_DFS;
1da177e4
LT
5004
5005 if (ses->capabilities & CAP_UNICODE) {
5006 pSMB->hdr.Flags2 |= SMBFLG2_UNICODE;
5007 name_len =
acbbb76a 5008 cifsConvertToUTF16((__le16 *) pSMB->RequestFileName,
b669f33c 5009 search_name, PATH_MAX, nls_codepage,
acbbb76a 5010 remap);
1da177e4
LT
5011 name_len++; /* trailing null */
5012 name_len *= 2;
50c2f753 5013 } else { /* BB improve the check for buffer overruns BB */
b669f33c 5014 name_len = strnlen(search_name, PATH_MAX);
1da177e4 5015 name_len++; /* trailing null */
b669f33c 5016 strncpy(pSMB->RequestFileName, search_name, name_len);
1da177e4
LT
5017 }
5018
65c3b205 5019 if (ses->server->sign)
38d77c50 5020 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
1a4e15a0 5021
50c2f753 5022 pSMB->hdr.Uid = ses->Suid;
1a4e15a0 5023
1da177e4
LT
5024 params = 2 /* level */ + name_len /*includes null */ ;
5025 pSMB->TotalDataCount = 0;
5026 pSMB->DataCount = 0;
5027 pSMB->DataOffset = 0;
5028 pSMB->MaxParameterCount = 0;
582d21e5
SF
5029 /* BB find exact max SMB PDU from sess structure BB */
5030 pSMB->MaxDataCount = cpu_to_le16(4000);
1da177e4
LT
5031 pSMB->MaxSetupCount = 0;
5032 pSMB->Reserved = 0;
5033 pSMB->Flags = 0;
5034 pSMB->Timeout = 0;
5035 pSMB->Reserved2 = 0;
5036 pSMB->ParameterOffset = cpu_to_le16(offsetof(
50c2f753 5037 struct smb_com_transaction2_get_dfs_refer_req, MaxReferralLevel) - 4);
1da177e4
LT
5038 pSMB->SetupCount = 1;
5039 pSMB->Reserved3 = 0;
5040 pSMB->SubCommand = cpu_to_le16(TRANS2_GET_DFS_REFERRAL);
5041 byte_count = params + 3 /* pad */ ;
5042 pSMB->ParameterCount = cpu_to_le16(params);
5043 pSMB->TotalParameterCount = pSMB->ParameterCount;
5044 pSMB->MaxReferralLevel = cpu_to_le16(3);
be8e3b00 5045 inc_rfc1001_len(pSMB, byte_count);
1da177e4
LT
5046 pSMB->ByteCount = cpu_to_le16(byte_count);
5047
5048 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
5049 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5050 if (rc) {
f96637be 5051 cifs_dbg(FYI, "Send error in GetDFSRefer = %d\n", rc);
c2cf07d5
SF
5052 goto GetDFSRefExit;
5053 }
5054 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
1da177e4 5055
c2cf07d5 5056 /* BB Also check if enough total bytes returned? */
820a803f 5057 if (rc || get_bcc(&pSMBr->hdr) < 17) {
c2cf07d5 5058 rc = -EIO; /* bad smb */
fec4585f
IM
5059 goto GetDFSRefExit;
5060 }
c2cf07d5 5061
f96637be
JP
5062 cifs_dbg(FYI, "Decoding GetDFSRefer response BCC: %d Offset %d\n",
5063 get_bcc(&pSMBr->hdr), le16_to_cpu(pSMBr->t2.DataOffset));
1da177e4 5064
fec4585f 5065 /* parse returned result into more usable form */
4ecce920
AA
5066 rc = parse_dfs_referrals(&pSMBr->dfs_data,
5067 le16_to_cpu(pSMBr->t2.DataCount),
5068 num_of_nodes, target_nodes, nls_codepage,
5069 remap, search_name,
284316dd 5070 (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE) != 0);
c2cf07d5 5071
1da177e4 5072GetDFSRefExit:
0d817bc0 5073 cifs_buf_release(pSMB);
1da177e4
LT
5074
5075 if (rc == -EAGAIN)
5076 goto getDFSRetry;
5077
5078 return rc;
5079}
5080
20962438
SF
5081/* Query File System Info such as free space to old servers such as Win 9x */
5082int
6d5786a3
PS
5083SMBOldQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
5084 struct kstatfs *FSData)
20962438
SF
5085{
5086/* level 0x01 SMB_QUERY_FILE_SYSTEM_INFO */
5087 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5088 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5089 FILE_SYSTEM_ALLOC_INFO *response_data;
5090 int rc = 0;
5091 int bytes_returned = 0;
5092 __u16 params, byte_count;
5093
f96637be 5094 cifs_dbg(FYI, "OldQFSInfo\n");
20962438
SF
5095oldQFSInfoRetry:
5096 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5097 (void **) &pSMBr);
5098 if (rc)
5099 return rc;
20962438
SF
5100
5101 params = 2; /* level */
5102 pSMB->TotalDataCount = 0;
5103 pSMB->MaxParameterCount = cpu_to_le16(2);
5104 pSMB->MaxDataCount = cpu_to_le16(1000);
5105 pSMB->MaxSetupCount = 0;
5106 pSMB->Reserved = 0;
5107 pSMB->Flags = 0;
5108 pSMB->Timeout = 0;
5109 pSMB->Reserved2 = 0;
5110 byte_count = params + 1 /* pad */ ;
5111 pSMB->TotalParameterCount = cpu_to_le16(params);
5112 pSMB->ParameterCount = pSMB->TotalParameterCount;
5113 pSMB->ParameterOffset = cpu_to_le16(offsetof(
5114 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
5115 pSMB->DataCount = 0;
5116 pSMB->DataOffset = 0;
5117 pSMB->SetupCount = 1;
5118 pSMB->Reserved3 = 0;
5119 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5120 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_ALLOCATION);
be8e3b00 5121 inc_rfc1001_len(pSMB, byte_count);
20962438
SF
5122 pSMB->ByteCount = cpu_to_le16(byte_count);
5123
5124 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5125 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5126 if (rc) {
f96637be 5127 cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
20962438
SF
5128 } else { /* decode response */
5129 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5130
820a803f 5131 if (rc || get_bcc(&pSMBr->hdr) < 18)
20962438
SF
5132 rc = -EIO; /* bad smb */
5133 else {
5134 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
f96637be 5135 cifs_dbg(FYI, "qfsinf resp BCC: %d Offset %d\n",
820a803f 5136 get_bcc(&pSMBr->hdr), data_offset);
20962438 5137
50c2f753 5138 response_data = (FILE_SYSTEM_ALLOC_INFO *)
20962438
SF
5139 (((char *) &pSMBr->hdr.Protocol) + data_offset);
5140 FSData->f_bsize =
5141 le16_to_cpu(response_data->BytesPerSector) *
5142 le32_to_cpu(response_data->
5143 SectorsPerAllocationUnit);
5a519bea
SF
5144 /*
5145 * much prefer larger but if server doesn't report
5146 * a valid size than 4K is a reasonable minimum
5147 */
5148 if (FSData->f_bsize < 512)
5149 FSData->f_bsize = 4096;
5150
20962438 5151 FSData->f_blocks =
50c2f753 5152 le32_to_cpu(response_data->TotalAllocationUnits);
20962438
SF
5153 FSData->f_bfree = FSData->f_bavail =
5154 le32_to_cpu(response_data->FreeAllocationUnits);
f96637be
JP
5155 cifs_dbg(FYI, "Blocks: %lld Free: %lld Block size %ld\n",
5156 (unsigned long long)FSData->f_blocks,
5157 (unsigned long long)FSData->f_bfree,
5158 FSData->f_bsize);
20962438
SF
5159 }
5160 }
5161 cifs_buf_release(pSMB);
5162
5163 if (rc == -EAGAIN)
5164 goto oldQFSInfoRetry;
5165
5166 return rc;
5167}
5168
1da177e4 5169int
6d5786a3
PS
5170CIFSSMBQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
5171 struct kstatfs *FSData)
1da177e4
LT
5172{
5173/* level 0x103 SMB_QUERY_FILE_SYSTEM_INFO */
5174 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5175 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5176 FILE_SYSTEM_INFO *response_data;
5177 int rc = 0;
5178 int bytes_returned = 0;
5179 __u16 params, byte_count;
5180
f96637be 5181 cifs_dbg(FYI, "In QFSInfo\n");
1da177e4
LT
5182QFSInfoRetry:
5183 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5184 (void **) &pSMBr);
5185 if (rc)
5186 return rc;
5187
5188 params = 2; /* level */
5189 pSMB->TotalDataCount = 0;
5190 pSMB->MaxParameterCount = cpu_to_le16(2);
20962438 5191 pSMB->MaxDataCount = cpu_to_le16(1000);
1da177e4
LT
5192 pSMB->MaxSetupCount = 0;
5193 pSMB->Reserved = 0;
5194 pSMB->Flags = 0;
5195 pSMB->Timeout = 0;
5196 pSMB->Reserved2 = 0;
5197 byte_count = params + 1 /* pad */ ;
5198 pSMB->TotalParameterCount = cpu_to_le16(params);
5199 pSMB->ParameterCount = pSMB->TotalParameterCount;
5200 pSMB->ParameterOffset = cpu_to_le16(offsetof(
50c2f753 5201 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
1da177e4
LT
5202 pSMB->DataCount = 0;
5203 pSMB->DataOffset = 0;
5204 pSMB->SetupCount = 1;
5205 pSMB->Reserved3 = 0;
5206 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5207 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_SIZE_INFO);
be8e3b00 5208 inc_rfc1001_len(pSMB, byte_count);
1da177e4
LT
5209 pSMB->ByteCount = cpu_to_le16(byte_count);
5210
5211 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5212 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5213 if (rc) {
f96637be 5214 cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
1da177e4 5215 } else { /* decode response */
50c2f753 5216 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
1da177e4 5217
820a803f 5218 if (rc || get_bcc(&pSMBr->hdr) < 24)
1da177e4
LT
5219 rc = -EIO; /* bad smb */
5220 else {
5221 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
1da177e4
LT
5222
5223 response_data =
5224 (FILE_SYSTEM_INFO
5225 *) (((char *) &pSMBr->hdr.Protocol) +
5226 data_offset);
5227 FSData->f_bsize =
5228 le32_to_cpu(response_data->BytesPerSector) *
5229 le32_to_cpu(response_data->
5230 SectorsPerAllocationUnit);
5a519bea
SF
5231 /*
5232 * much prefer larger but if server doesn't report
5233 * a valid size than 4K is a reasonable minimum
5234 */
5235 if (FSData->f_bsize < 512)
5236 FSData->f_bsize = 4096;
5237
1da177e4
LT
5238 FSData->f_blocks =
5239 le64_to_cpu(response_data->TotalAllocationUnits);
5240 FSData->f_bfree = FSData->f_bavail =
5241 le64_to_cpu(response_data->FreeAllocationUnits);
f96637be
JP
5242 cifs_dbg(FYI, "Blocks: %lld Free: %lld Block size %ld\n",
5243 (unsigned long long)FSData->f_blocks,
5244 (unsigned long long)FSData->f_bfree,
5245 FSData->f_bsize);
1da177e4
LT
5246 }
5247 }
5248 cifs_buf_release(pSMB);
5249
5250 if (rc == -EAGAIN)
5251 goto QFSInfoRetry;
5252
5253 return rc;
5254}
5255
5256int
6d5786a3 5257CIFSSMBQFSAttributeInfo(const unsigned int xid, struct cifs_tcon *tcon)
1da177e4
LT
5258{
5259/* level 0x105 SMB_QUERY_FILE_SYSTEM_INFO */
5260 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5261 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5262 FILE_SYSTEM_ATTRIBUTE_INFO *response_data;
5263 int rc = 0;
5264 int bytes_returned = 0;
5265 __u16 params, byte_count;
5266
f96637be 5267 cifs_dbg(FYI, "In QFSAttributeInfo\n");
1da177e4
LT
5268QFSAttributeRetry:
5269 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5270 (void **) &pSMBr);
5271 if (rc)
5272 return rc;
5273
5274 params = 2; /* level */
5275 pSMB->TotalDataCount = 0;
5276 pSMB->MaxParameterCount = cpu_to_le16(2);
582d21e5
SF
5277 /* BB find exact max SMB PDU from sess structure BB */
5278 pSMB->MaxDataCount = cpu_to_le16(1000);
1da177e4
LT
5279 pSMB->MaxSetupCount = 0;
5280 pSMB->Reserved = 0;
5281 pSMB->Flags = 0;
5282 pSMB->Timeout = 0;
5283 pSMB->Reserved2 = 0;
5284 byte_count = params + 1 /* pad */ ;
5285 pSMB->TotalParameterCount = cpu_to_le16(params);
5286 pSMB->ParameterCount = pSMB->TotalParameterCount;
5287 pSMB->ParameterOffset = cpu_to_le16(offsetof(
50c2f753 5288 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
1da177e4
LT
5289 pSMB->DataCount = 0;
5290 pSMB->DataOffset = 0;
5291 pSMB->SetupCount = 1;
5292 pSMB->Reserved3 = 0;
5293 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5294 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_ATTRIBUTE_INFO);
be8e3b00 5295 inc_rfc1001_len(pSMB, byte_count);
1da177e4
LT
5296 pSMB->ByteCount = cpu_to_le16(byte_count);
5297
5298 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5299 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5300 if (rc) {
f96637be 5301 cifs_dbg(VFS, "Send error in QFSAttributeInfo = %d\n", rc);
1da177e4
LT
5302 } else { /* decode response */
5303 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5304
820a803f 5305 if (rc || get_bcc(&pSMBr->hdr) < 13) {
50c2f753 5306 /* BB also check if enough bytes returned */
1da177e4
LT
5307 rc = -EIO; /* bad smb */
5308 } else {
5309 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5310 response_data =
5311 (FILE_SYSTEM_ATTRIBUTE_INFO
5312 *) (((char *) &pSMBr->hdr.Protocol) +
5313 data_offset);
5314 memcpy(&tcon->fsAttrInfo, response_data,
26f57364 5315 sizeof(FILE_SYSTEM_ATTRIBUTE_INFO));
1da177e4
LT
5316 }
5317 }
5318 cifs_buf_release(pSMB);
5319
5320 if (rc == -EAGAIN)
5321 goto QFSAttributeRetry;
5322
5323 return rc;
5324}
5325
5326int
6d5786a3 5327CIFSSMBQFSDeviceInfo(const unsigned int xid, struct cifs_tcon *tcon)
1da177e4
LT
5328{
5329/* level 0x104 SMB_QUERY_FILE_SYSTEM_INFO */
5330 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5331 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5332 FILE_SYSTEM_DEVICE_INFO *response_data;
5333 int rc = 0;
5334 int bytes_returned = 0;
5335 __u16 params, byte_count;
5336
f96637be 5337 cifs_dbg(FYI, "In QFSDeviceInfo\n");
1da177e4
LT
5338QFSDeviceRetry:
5339 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5340 (void **) &pSMBr);
5341 if (rc)
5342 return rc;
5343
5344 params = 2; /* level */
5345 pSMB->TotalDataCount = 0;
5346 pSMB->MaxParameterCount = cpu_to_le16(2);
582d21e5
SF
5347 /* BB find exact max SMB PDU from sess structure BB */
5348 pSMB->MaxDataCount = cpu_to_le16(1000);
1da177e4
LT
5349 pSMB->MaxSetupCount = 0;
5350 pSMB->Reserved = 0;
5351 pSMB->Flags = 0;
5352 pSMB->Timeout = 0;
5353 pSMB->Reserved2 = 0;
5354 byte_count = params + 1 /* pad */ ;
5355 pSMB->TotalParameterCount = cpu_to_le16(params);
5356 pSMB->ParameterCount = pSMB->TotalParameterCount;
5357 pSMB->ParameterOffset = cpu_to_le16(offsetof(
50c2f753 5358 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
1da177e4
LT
5359
5360 pSMB->DataCount = 0;
5361 pSMB->DataOffset = 0;
5362 pSMB->SetupCount = 1;
5363 pSMB->Reserved3 = 0;
5364 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5365 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_DEVICE_INFO);
be8e3b00 5366 inc_rfc1001_len(pSMB, byte_count);
1da177e4
LT
5367 pSMB->ByteCount = cpu_to_le16(byte_count);
5368
5369 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5370 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5371 if (rc) {
f96637be 5372 cifs_dbg(FYI, "Send error in QFSDeviceInfo = %d\n", rc);
1da177e4
LT
5373 } else { /* decode response */
5374 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5375
820a803f
JL
5376 if (rc || get_bcc(&pSMBr->hdr) <
5377 sizeof(FILE_SYSTEM_DEVICE_INFO))
1da177e4
LT
5378 rc = -EIO; /* bad smb */
5379 else {
5380 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5381 response_data =
737b758c
SF
5382 (FILE_SYSTEM_DEVICE_INFO *)
5383 (((char *) &pSMBr->hdr.Protocol) +
1da177e4
LT
5384 data_offset);
5385 memcpy(&tcon->fsDevInfo, response_data,
26f57364 5386 sizeof(FILE_SYSTEM_DEVICE_INFO));
1da177e4
LT
5387 }
5388 }
5389 cifs_buf_release(pSMB);
5390
5391 if (rc == -EAGAIN)
5392 goto QFSDeviceRetry;
5393
5394 return rc;
5395}
5396
5397int
6d5786a3 5398CIFSSMBQFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon)
1da177e4
LT
5399{
5400/* level 0x200 SMB_QUERY_CIFS_UNIX_INFO */
5401 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5402 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5403 FILE_SYSTEM_UNIX_INFO *response_data;
5404 int rc = 0;
5405 int bytes_returned = 0;
5406 __u16 params, byte_count;
5407
f96637be 5408 cifs_dbg(FYI, "In QFSUnixInfo\n");
1da177e4 5409QFSUnixRetry:
f569599a
JL
5410 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5411 (void **) &pSMB, (void **) &pSMBr);
1da177e4
LT
5412 if (rc)
5413 return rc;
5414
5415 params = 2; /* level */
5416 pSMB->TotalDataCount = 0;
5417 pSMB->DataCount = 0;
5418 pSMB->DataOffset = 0;
5419 pSMB->MaxParameterCount = cpu_to_le16(2);
582d21e5
SF
5420 /* BB find exact max SMB PDU from sess structure BB */
5421 pSMB->MaxDataCount = cpu_to_le16(100);
1da177e4
LT
5422 pSMB->MaxSetupCount = 0;
5423 pSMB->Reserved = 0;
5424 pSMB->Flags = 0;
5425 pSMB->Timeout = 0;
5426 pSMB->Reserved2 = 0;
5427 byte_count = params + 1 /* pad */ ;
5428 pSMB->ParameterCount = cpu_to_le16(params);
5429 pSMB->TotalParameterCount = pSMB->ParameterCount;
50c2f753
SF
5430 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5431 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
1da177e4
LT
5432 pSMB->SetupCount = 1;
5433 pSMB->Reserved3 = 0;
5434 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5435 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_CIFS_UNIX_INFO);
be8e3b00 5436 inc_rfc1001_len(pSMB, byte_count);
1da177e4
LT
5437 pSMB->ByteCount = cpu_to_le16(byte_count);
5438
5439 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5440 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5441 if (rc) {
f96637be 5442 cifs_dbg(VFS, "Send error in QFSUnixInfo = %d\n", rc);
1da177e4
LT
5443 } else { /* decode response */
5444 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5445
820a803f 5446 if (rc || get_bcc(&pSMBr->hdr) < 13) {
1da177e4
LT
5447 rc = -EIO; /* bad smb */
5448 } else {
5449 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5450 response_data =
5451 (FILE_SYSTEM_UNIX_INFO
5452 *) (((char *) &pSMBr->hdr.Protocol) +
5453 data_offset);
5454 memcpy(&tcon->fsUnixInfo, response_data,
26f57364 5455 sizeof(FILE_SYSTEM_UNIX_INFO));
1da177e4
LT
5456 }
5457 }
5458 cifs_buf_release(pSMB);
5459
5460 if (rc == -EAGAIN)
5461 goto QFSUnixRetry;
5462
5463
5464 return rc;
5465}
5466
ac67055e 5467int
6d5786a3 5468CIFSSMBSetFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon, __u64 cap)
ac67055e
JA
5469{
5470/* level 0x200 SMB_SET_CIFS_UNIX_INFO */
5471 TRANSACTION2_SETFSI_REQ *pSMB = NULL;
5472 TRANSACTION2_SETFSI_RSP *pSMBr = NULL;
5473 int rc = 0;
5474 int bytes_returned = 0;
5475 __u16 params, param_offset, offset, byte_count;
5476
f96637be 5477 cifs_dbg(FYI, "In SETFSUnixInfo\n");
ac67055e 5478SETFSUnixRetry:
f26282c9 5479 /* BB switch to small buf init to save memory */
f569599a
JL
5480 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5481 (void **) &pSMB, (void **) &pSMBr);
ac67055e
JA
5482 if (rc)
5483 return rc;
5484
5485 params = 4; /* 2 bytes zero followed by info level. */
5486 pSMB->MaxSetupCount = 0;
5487 pSMB->Reserved = 0;
5488 pSMB->Flags = 0;
5489 pSMB->Timeout = 0;
5490 pSMB->Reserved2 = 0;
50c2f753
SF
5491 param_offset = offsetof(struct smb_com_transaction2_setfsi_req, FileNum)
5492 - 4;
ac67055e
JA
5493 offset = param_offset + params;
5494
5495 pSMB->MaxParameterCount = cpu_to_le16(4);
582d21e5
SF
5496 /* BB find exact max SMB PDU from sess structure BB */
5497 pSMB->MaxDataCount = cpu_to_le16(100);
ac67055e
JA
5498 pSMB->SetupCount = 1;
5499 pSMB->Reserved3 = 0;
5500 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FS_INFORMATION);
5501 byte_count = 1 /* pad */ + params + 12;
5502
5503 pSMB->DataCount = cpu_to_le16(12);
5504 pSMB->ParameterCount = cpu_to_le16(params);
5505 pSMB->TotalDataCount = pSMB->DataCount;
5506 pSMB->TotalParameterCount = pSMB->ParameterCount;
5507 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5508 pSMB->DataOffset = cpu_to_le16(offset);
5509
5510 /* Params. */
5511 pSMB->FileNum = 0;
5512 pSMB->InformationLevel = cpu_to_le16(SMB_SET_CIFS_UNIX_INFO);
5513
5514 /* Data. */
5515 pSMB->ClientUnixMajor = cpu_to_le16(CIFS_UNIX_MAJOR_VERSION);
5516 pSMB->ClientUnixMinor = cpu_to_le16(CIFS_UNIX_MINOR_VERSION);
5517 pSMB->ClientUnixCap = cpu_to_le64(cap);
5518
be8e3b00 5519 inc_rfc1001_len(pSMB, byte_count);
ac67055e
JA
5520 pSMB->ByteCount = cpu_to_le16(byte_count);
5521
5522 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5523 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5524 if (rc) {
f96637be 5525 cifs_dbg(VFS, "Send error in SETFSUnixInfo = %d\n", rc);
ac67055e
JA
5526 } else { /* decode response */
5527 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
ad7a2926 5528 if (rc)
ac67055e 5529 rc = -EIO; /* bad smb */
ac67055e
JA
5530 }
5531 cifs_buf_release(pSMB);
5532
5533 if (rc == -EAGAIN)
5534 goto SETFSUnixRetry;
5535
5536 return rc;
5537}
5538
5539
1da177e4
LT
5540
5541int
6d5786a3 5542CIFSSMBQFSPosixInfo(const unsigned int xid, struct cifs_tcon *tcon,
737b758c 5543 struct kstatfs *FSData)
1da177e4
LT
5544{
5545/* level 0x201 SMB_QUERY_CIFS_POSIX_INFO */
5546 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5547 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5548 FILE_SYSTEM_POSIX_INFO *response_data;
5549 int rc = 0;
5550 int bytes_returned = 0;
5551 __u16 params, byte_count;
5552
f96637be 5553 cifs_dbg(FYI, "In QFSPosixInfo\n");
1da177e4
LT
5554QFSPosixRetry:
5555 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5556 (void **) &pSMBr);
5557 if (rc)
5558 return rc;
5559
5560 params = 2; /* level */
5561 pSMB->TotalDataCount = 0;
5562 pSMB->DataCount = 0;
5563 pSMB->DataOffset = 0;
5564 pSMB->MaxParameterCount = cpu_to_le16(2);
582d21e5
SF
5565 /* BB find exact max SMB PDU from sess structure BB */
5566 pSMB->MaxDataCount = cpu_to_le16(100);
1da177e4
LT
5567 pSMB->MaxSetupCount = 0;
5568 pSMB->Reserved = 0;
5569 pSMB->Flags = 0;
5570 pSMB->Timeout = 0;
5571 pSMB->Reserved2 = 0;
5572 byte_count = params + 1 /* pad */ ;
5573 pSMB->ParameterCount = cpu_to_le16(params);
5574 pSMB->TotalParameterCount = pSMB->ParameterCount;
50c2f753
SF
5575 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5576 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
1da177e4
LT
5577 pSMB->SetupCount = 1;
5578 pSMB->Reserved3 = 0;
5579 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5580 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_FS_INFO);
be8e3b00 5581 inc_rfc1001_len(pSMB, byte_count);
1da177e4
LT
5582 pSMB->ByteCount = cpu_to_le16(byte_count);
5583
5584 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5585 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5586 if (rc) {
f96637be 5587 cifs_dbg(FYI, "Send error in QFSUnixInfo = %d\n", rc);
1da177e4
LT
5588 } else { /* decode response */
5589 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5590
820a803f 5591 if (rc || get_bcc(&pSMBr->hdr) < 13) {
1da177e4
LT
5592 rc = -EIO; /* bad smb */
5593 } else {
5594 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5595 response_data =
5596 (FILE_SYSTEM_POSIX_INFO
5597 *) (((char *) &pSMBr->hdr.Protocol) +
5598 data_offset);
5599 FSData->f_bsize =
5600 le32_to_cpu(response_data->BlockSize);
5a519bea
SF
5601 /*
5602 * much prefer larger but if server doesn't report
5603 * a valid size than 4K is a reasonable minimum
5604 */
5605 if (FSData->f_bsize < 512)
5606 FSData->f_bsize = 4096;
5607
1da177e4
LT
5608 FSData->f_blocks =
5609 le64_to_cpu(response_data->TotalBlocks);
5610 FSData->f_bfree =
5611 le64_to_cpu(response_data->BlocksAvail);
790fe579 5612 if (response_data->UserBlocksAvail == cpu_to_le64(-1)) {
1da177e4
LT
5613 FSData->f_bavail = FSData->f_bfree;
5614 } else {
5615 FSData->f_bavail =
50c2f753 5616 le64_to_cpu(response_data->UserBlocksAvail);
1da177e4 5617 }
790fe579 5618 if (response_data->TotalFileNodes != cpu_to_le64(-1))
1da177e4 5619 FSData->f_files =
50c2f753 5620 le64_to_cpu(response_data->TotalFileNodes);
790fe579 5621 if (response_data->FreeFileNodes != cpu_to_le64(-1))
1da177e4 5622 FSData->f_ffree =
50c2f753 5623 le64_to_cpu(response_data->FreeFileNodes);
1da177e4
LT
5624 }
5625 }
5626 cifs_buf_release(pSMB);
5627
5628 if (rc == -EAGAIN)
5629 goto QFSPosixRetry;
5630
5631 return rc;
5632}
5633
5634
d1433418
PS
5635/*
5636 * We can not use write of zero bytes trick to set file size due to need for
5637 * large file support. Also note that this SetPathInfo is preferred to
5638 * SetFileInfo based method in next routine which is only needed to work around
5639 * a sharing violation bugin Samba which this routine can run into.
5640 */
1da177e4 5641int
6d5786a3 5642CIFSSMBSetEOF(const unsigned int xid, struct cifs_tcon *tcon,
d1433418
PS
5643 const char *file_name, __u64 size, struct cifs_sb_info *cifs_sb,
5644 bool set_allocation)
1da177e4
LT
5645{
5646 struct smb_com_transaction2_spi_req *pSMB = NULL;
5647 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
5648 struct file_end_of_file_info *parm_data;
5649 int name_len;
5650 int rc = 0;
5651 int bytes_returned = 0;
2baa2682 5652 int remap = cifs_remap(cifs_sb);
d1433418 5653
1da177e4
LT
5654 __u16 params, byte_count, data_count, param_offset, offset;
5655
f96637be 5656 cifs_dbg(FYI, "In SetEOF\n");
1da177e4
LT
5657SetEOFRetry:
5658 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5659 (void **) &pSMBr);
5660 if (rc)
5661 return rc;
5662
5663 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5664 name_len =
d1433418
PS
5665 cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
5666 PATH_MAX, cifs_sb->local_nls, remap);
1da177e4
LT
5667 name_len++; /* trailing null */
5668 name_len *= 2;
3e87d803 5669 } else { /* BB improve the check for buffer overruns BB */
d1433418 5670 name_len = strnlen(file_name, PATH_MAX);
1da177e4 5671 name_len++; /* trailing null */
d1433418 5672 strncpy(pSMB->FileName, file_name, name_len);
1da177e4
LT
5673 }
5674 params = 6 + name_len;
26f57364 5675 data_count = sizeof(struct file_end_of_file_info);
1da177e4 5676 pSMB->MaxParameterCount = cpu_to_le16(2);
3e87d803 5677 pSMB->MaxDataCount = cpu_to_le16(4100);
1da177e4
LT
5678 pSMB->MaxSetupCount = 0;
5679 pSMB->Reserved = 0;
5680 pSMB->Flags = 0;
5681 pSMB->Timeout = 0;
5682 pSMB->Reserved2 = 0;
5683 param_offset = offsetof(struct smb_com_transaction2_spi_req,
50c2f753 5684 InformationLevel) - 4;
1da177e4 5685 offset = param_offset + params;
d1433418 5686 if (set_allocation) {
50c2f753
SF
5687 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5688 pSMB->InformationLevel =
5689 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5690 else
5691 pSMB->InformationLevel =
5692 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
5693 } else /* Set File Size */ {
1da177e4
LT
5694 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5695 pSMB->InformationLevel =
50c2f753 5696 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
1da177e4
LT
5697 else
5698 pSMB->InformationLevel =
50c2f753 5699 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
1da177e4
LT
5700 }
5701
5702 parm_data =
5703 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol) +
5704 offset);
5705 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5706 pSMB->DataOffset = cpu_to_le16(offset);
5707 pSMB->SetupCount = 1;
5708 pSMB->Reserved3 = 0;
5709 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5710 byte_count = 3 /* pad */ + params + data_count;
5711 pSMB->DataCount = cpu_to_le16(data_count);
5712 pSMB->TotalDataCount = pSMB->DataCount;
5713 pSMB->ParameterCount = cpu_to_le16(params);
5714 pSMB->TotalParameterCount = pSMB->ParameterCount;
5715 pSMB->Reserved4 = 0;
be8e3b00 5716 inc_rfc1001_len(pSMB, byte_count);
1da177e4
LT
5717 parm_data->FileSize = cpu_to_le64(size);
5718 pSMB->ByteCount = cpu_to_le16(byte_count);
5719 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5720 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
ad7a2926 5721 if (rc)
f96637be 5722 cifs_dbg(FYI, "SetPathInfo (file size) returned %d\n", rc);
1da177e4
LT
5723
5724 cifs_buf_release(pSMB);
5725
5726 if (rc == -EAGAIN)
5727 goto SetEOFRetry;
5728
5729 return rc;
5730}
5731
5732int
d1433418
PS
5733CIFSSMBSetFileSize(const unsigned int xid, struct cifs_tcon *tcon,
5734 struct cifsFileInfo *cfile, __u64 size, bool set_allocation)
1da177e4
LT
5735{
5736 struct smb_com_transaction2_sfi_req *pSMB = NULL;
1da177e4
LT
5737 struct file_end_of_file_info *parm_data;
5738 int rc = 0;
1da177e4
LT
5739 __u16 params, param_offset, offset, byte_count, count;
5740
f96637be
JP
5741 cifs_dbg(FYI, "SetFileSize (via SetFileInfo) %lld\n",
5742 (long long)size);
cd63499c
SF
5743 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5744
1da177e4
LT
5745 if (rc)
5746 return rc;
5747
d1433418
PS
5748 pSMB->hdr.Pid = cpu_to_le16((__u16)cfile->pid);
5749 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(cfile->pid >> 16));
50c2f753 5750
1da177e4
LT
5751 params = 6;
5752 pSMB->MaxSetupCount = 0;
5753 pSMB->Reserved = 0;
5754 pSMB->Flags = 0;
5755 pSMB->Timeout = 0;
5756 pSMB->Reserved2 = 0;
5757 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5758 offset = param_offset + params;
5759
1da177e4
LT
5760 count = sizeof(struct file_end_of_file_info);
5761 pSMB->MaxParameterCount = cpu_to_le16(2);
582d21e5
SF
5762 /* BB find exact max SMB PDU from sess structure BB */
5763 pSMB->MaxDataCount = cpu_to_le16(1000);
1da177e4
LT
5764 pSMB->SetupCount = 1;
5765 pSMB->Reserved3 = 0;
5766 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5767 byte_count = 3 /* pad */ + params + count;
5768 pSMB->DataCount = cpu_to_le16(count);
5769 pSMB->ParameterCount = cpu_to_le16(params);
5770 pSMB->TotalDataCount = pSMB->DataCount;
5771 pSMB->TotalParameterCount = pSMB->ParameterCount;
5772 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5773 parm_data =
50c2f753
SF
5774 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol)
5775 + offset);
1da177e4
LT
5776 pSMB->DataOffset = cpu_to_le16(offset);
5777 parm_data->FileSize = cpu_to_le64(size);
d1433418
PS
5778 pSMB->Fid = cfile->fid.netfid;
5779 if (set_allocation) {
1da177e4
LT
5780 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5781 pSMB->InformationLevel =
5782 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5783 else
5784 pSMB->InformationLevel =
5785 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
50c2f753 5786 } else /* Set File Size */ {
1da177e4
LT
5787 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5788 pSMB->InformationLevel =
50c2f753 5789 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
1da177e4
LT
5790 else
5791 pSMB->InformationLevel =
50c2f753 5792 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
1da177e4
LT
5793 }
5794 pSMB->Reserved4 = 0;
be8e3b00 5795 inc_rfc1001_len(pSMB, byte_count);
1da177e4 5796 pSMB->ByteCount = cpu_to_le16(byte_count);
792af7b0 5797 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
da502f7d 5798 cifs_small_buf_release(pSMB);
1da177e4 5799 if (rc) {
f96637be
JP
5800 cifs_dbg(FYI, "Send error in SetFileInfo (SetFileSize) = %d\n",
5801 rc);
1da177e4
LT
5802 }
5803
50c2f753 5804 /* Note: On -EAGAIN error only caller can retry on handle based calls
1da177e4
LT
5805 since file handle passed in no longer valid */
5806
5807 return rc;
5808}
5809
50c2f753 5810/* Some legacy servers such as NT4 require that the file times be set on
1da177e4
LT
5811 an open handle, rather than by pathname - this is awkward due to
5812 potential access conflicts on the open, but it is unavoidable for these
5813 old servers since the only other choice is to go from 100 nanosecond DCE
5814 time and resort to the original setpathinfo level which takes the ancient
5815 DOS time format with 2 second granularity */
5816int
6d5786a3 5817CIFSSMBSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
2dd2dfa0 5818 const FILE_BASIC_INFO *data, __u16 fid, __u32 pid_of_opener)
1da177e4
LT
5819{
5820 struct smb_com_transaction2_sfi_req *pSMB = NULL;
1da177e4
LT
5821 char *data_offset;
5822 int rc = 0;
1da177e4
LT
5823 __u16 params, param_offset, offset, byte_count, count;
5824
f96637be 5825 cifs_dbg(FYI, "Set Times (via SetFileInfo)\n");
cd63499c
SF
5826 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5827
1da177e4
LT
5828 if (rc)
5829 return rc;
5830
2dd2dfa0
JL
5831 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5832 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
50c2f753 5833
1da177e4
LT
5834 params = 6;
5835 pSMB->MaxSetupCount = 0;
5836 pSMB->Reserved = 0;
5837 pSMB->Flags = 0;
5838 pSMB->Timeout = 0;
5839 pSMB->Reserved2 = 0;
5840 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5841 offset = param_offset + params;
5842
b2a3ad9c
JL
5843 data_offset = (char *)pSMB +
5844 offsetof(struct smb_hdr, Protocol) + offset;
1da177e4 5845
26f57364 5846 count = sizeof(FILE_BASIC_INFO);
1da177e4 5847 pSMB->MaxParameterCount = cpu_to_le16(2);
582d21e5
SF
5848 /* BB find max SMB PDU from sess */
5849 pSMB->MaxDataCount = cpu_to_le16(1000);
1da177e4
LT
5850 pSMB->SetupCount = 1;
5851 pSMB->Reserved3 = 0;
5852 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5853 byte_count = 3 /* pad */ + params + count;
5854 pSMB->DataCount = cpu_to_le16(count);
5855 pSMB->ParameterCount = cpu_to_le16(params);
5856 pSMB->TotalDataCount = pSMB->DataCount;
5857 pSMB->TotalParameterCount = pSMB->ParameterCount;
5858 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5859 pSMB->DataOffset = cpu_to_le16(offset);
5860 pSMB->Fid = fid;
5861 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5862 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5863 else
5864 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5865 pSMB->Reserved4 = 0;
be8e3b00 5866 inc_rfc1001_len(pSMB, byte_count);
1da177e4 5867 pSMB->ByteCount = cpu_to_le16(byte_count);
50c2f753 5868 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
792af7b0 5869 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
da502f7d 5870 cifs_small_buf_release(pSMB);
ad7a2926 5871 if (rc)
f96637be
JP
5872 cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n",
5873 rc);
1da177e4 5874
50c2f753 5875 /* Note: On -EAGAIN error only caller can retry on handle based calls
1da177e4
LT
5876 since file handle passed in no longer valid */
5877
5878 return rc;
5879}
5880
6d22f098 5881int
6d5786a3 5882CIFSSMBSetFileDisposition(const unsigned int xid, struct cifs_tcon *tcon,
6d22f098
JL
5883 bool delete_file, __u16 fid, __u32 pid_of_opener)
5884{
5885 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5886 char *data_offset;
5887 int rc = 0;
5888 __u16 params, param_offset, offset, byte_count, count;
5889
f96637be 5890 cifs_dbg(FYI, "Set File Disposition (via SetFileInfo)\n");
6d22f098
JL
5891 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5892
5893 if (rc)
5894 return rc;
5895
5896 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5897 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5898
5899 params = 6;
5900 pSMB->MaxSetupCount = 0;
5901 pSMB->Reserved = 0;
5902 pSMB->Flags = 0;
5903 pSMB->Timeout = 0;
5904 pSMB->Reserved2 = 0;
5905 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5906 offset = param_offset + params;
5907
5908 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5909
5910 count = 1;
5911 pSMB->MaxParameterCount = cpu_to_le16(2);
5912 /* BB find max SMB PDU from sess */
5913 pSMB->MaxDataCount = cpu_to_le16(1000);
5914 pSMB->SetupCount = 1;
5915 pSMB->Reserved3 = 0;
5916 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5917 byte_count = 3 /* pad */ + params + count;
5918 pSMB->DataCount = cpu_to_le16(count);
5919 pSMB->ParameterCount = cpu_to_le16(params);
5920 pSMB->TotalDataCount = pSMB->DataCount;
5921 pSMB->TotalParameterCount = pSMB->ParameterCount;
5922 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5923 pSMB->DataOffset = cpu_to_le16(offset);
5924 pSMB->Fid = fid;
5925 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_DISPOSITION_INFO);
5926 pSMB->Reserved4 = 0;
be8e3b00 5927 inc_rfc1001_len(pSMB, byte_count);
6d22f098
JL
5928 pSMB->ByteCount = cpu_to_le16(byte_count);
5929 *data_offset = delete_file ? 1 : 0;
792af7b0 5930 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
da502f7d 5931 cifs_small_buf_release(pSMB);
6d22f098 5932 if (rc)
f96637be 5933 cifs_dbg(FYI, "Send error in SetFileDisposition = %d\n", rc);
6d22f098
JL
5934
5935 return rc;
5936}
1da177e4
LT
5937
5938int
6d5786a3 5939CIFSSMBSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
6fc000e5
JL
5940 const char *fileName, const FILE_BASIC_INFO *data,
5941 const struct nls_table *nls_codepage, int remap)
1da177e4
LT
5942{
5943 TRANSACTION2_SPI_REQ *pSMB = NULL;
5944 TRANSACTION2_SPI_RSP *pSMBr = NULL;
5945 int name_len;
5946 int rc = 0;
5947 int bytes_returned = 0;
5948 char *data_offset;
5949 __u16 params, param_offset, offset, byte_count, count;
5950
f96637be 5951 cifs_dbg(FYI, "In SetTimes\n");
1da177e4
LT
5952
5953SetTimesRetry:
5954 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5955 (void **) &pSMBr);
5956 if (rc)
5957 return rc;
5958
5959 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5960 name_len =
acbbb76a
SF
5961 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
5962 PATH_MAX, nls_codepage, remap);
1da177e4
LT
5963 name_len++; /* trailing null */
5964 name_len *= 2;
50c2f753 5965 } else { /* BB improve the check for buffer overruns BB */
1da177e4
LT
5966 name_len = strnlen(fileName, PATH_MAX);
5967 name_len++; /* trailing null */
5968 strncpy(pSMB->FileName, fileName, name_len);
5969 }
5970
5971 params = 6 + name_len;
26f57364 5972 count = sizeof(FILE_BASIC_INFO);
1da177e4 5973 pSMB->MaxParameterCount = cpu_to_le16(2);
582d21e5
SF
5974 /* BB find max SMB PDU from sess structure BB */
5975 pSMB->MaxDataCount = cpu_to_le16(1000);
1da177e4
LT
5976 pSMB->MaxSetupCount = 0;
5977 pSMB->Reserved = 0;
5978 pSMB->Flags = 0;
5979 pSMB->Timeout = 0;
5980 pSMB->Reserved2 = 0;
5981 param_offset = offsetof(struct smb_com_transaction2_spi_req,
50c2f753 5982 InformationLevel) - 4;
1da177e4
LT
5983 offset = param_offset + params;
5984 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5985 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5986 pSMB->DataOffset = cpu_to_le16(offset);
5987 pSMB->SetupCount = 1;
5988 pSMB->Reserved3 = 0;
5989 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5990 byte_count = 3 /* pad */ + params + count;
5991
5992 pSMB->DataCount = cpu_to_le16(count);
5993 pSMB->ParameterCount = cpu_to_le16(params);
5994 pSMB->TotalDataCount = pSMB->DataCount;
5995 pSMB->TotalParameterCount = pSMB->ParameterCount;
5996 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5997 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5998 else
5999 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
6000 pSMB->Reserved4 = 0;
be8e3b00 6001 inc_rfc1001_len(pSMB, byte_count);
26f57364 6002 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
1da177e4
LT
6003 pSMB->ByteCount = cpu_to_le16(byte_count);
6004 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6005 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
ad7a2926 6006 if (rc)
f96637be 6007 cifs_dbg(FYI, "SetPathInfo (times) returned %d\n", rc);
1da177e4
LT
6008
6009 cifs_buf_release(pSMB);
6010
6011 if (rc == -EAGAIN)
6012 goto SetTimesRetry;
6013
6014 return rc;
6015}
6016
6017/* Can not be used to set time stamps yet (due to old DOS time format) */
6018/* Can be used to set attributes */
6019#if 0 /* Possibly not needed - since it turns out that strangely NT4 has a bug
6020 handling it anyway and NT4 was what we thought it would be needed for
6021 Do not delete it until we prove whether needed for Win9x though */
6022int
6d5786a3 6023CIFSSMBSetAttrLegacy(unsigned int xid, struct cifs_tcon *tcon, char *fileName,
1da177e4
LT
6024 __u16 dos_attrs, const struct nls_table *nls_codepage)
6025{
6026 SETATTR_REQ *pSMB = NULL;
6027 SETATTR_RSP *pSMBr = NULL;
6028 int rc = 0;
6029 int bytes_returned;
6030 int name_len;
6031
f96637be 6032 cifs_dbg(FYI, "In SetAttrLegacy\n");
1da177e4
LT
6033
6034SetAttrLgcyRetry:
6035 rc = smb_init(SMB_COM_SETATTR, 8, tcon, (void **) &pSMB,
6036 (void **) &pSMBr);
6037 if (rc)
6038 return rc;
6039
6040 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6041 name_len =
acbbb76a
SF
6042 ConvertToUTF16((__le16 *) pSMB->fileName, fileName,
6043 PATH_MAX, nls_codepage);
1da177e4
LT
6044 name_len++; /* trailing null */
6045 name_len *= 2;
50c2f753 6046 } else { /* BB improve the check for buffer overruns BB */
1da177e4
LT
6047 name_len = strnlen(fileName, PATH_MAX);
6048 name_len++; /* trailing null */
6049 strncpy(pSMB->fileName, fileName, name_len);
6050 }
6051 pSMB->attr = cpu_to_le16(dos_attrs);
6052 pSMB->BufferFormat = 0x04;
be8e3b00 6053 inc_rfc1001_len(pSMB, name_len + 1);
1da177e4
LT
6054 pSMB->ByteCount = cpu_to_le16(name_len + 1);
6055 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6056 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
ad7a2926 6057 if (rc)
f96637be 6058 cifs_dbg(FYI, "Error in LegacySetAttr = %d\n", rc);
1da177e4
LT
6059
6060 cifs_buf_release(pSMB);
6061
6062 if (rc == -EAGAIN)
6063 goto SetAttrLgcyRetry;
6064
6065 return rc;
6066}
6067#endif /* temporarily unneeded SetAttr legacy function */
6068
654cf14a
JL
6069static void
6070cifs_fill_unix_set_info(FILE_UNIX_BASIC_INFO *data_offset,
6071 const struct cifs_unix_set_info_args *args)
6072{
49418b2c 6073 u64 uid = NO_CHANGE_64, gid = NO_CHANGE_64;
654cf14a
JL
6074 u64 mode = args->mode;
6075
49418b2c
EB
6076 if (uid_valid(args->uid))
6077 uid = from_kuid(&init_user_ns, args->uid);
6078 if (gid_valid(args->gid))
6079 gid = from_kgid(&init_user_ns, args->gid);
6080
654cf14a
JL
6081 /*
6082 * Samba server ignores set of file size to zero due to bugs in some
6083 * older clients, but we should be precise - we use SetFileSize to
6084 * set file size and do not want to truncate file size to zero
25985edc 6085 * accidentally as happened on one Samba server beta by putting
654cf14a
JL
6086 * zero instead of -1 here
6087 */
6088 data_offset->EndOfFile = cpu_to_le64(NO_CHANGE_64);
6089 data_offset->NumOfBytes = cpu_to_le64(NO_CHANGE_64);
6090 data_offset->LastStatusChange = cpu_to_le64(args->ctime);
6091 data_offset->LastAccessTime = cpu_to_le64(args->atime);
6092 data_offset->LastModificationTime = cpu_to_le64(args->mtime);
49418b2c
EB
6093 data_offset->Uid = cpu_to_le64(uid);
6094 data_offset->Gid = cpu_to_le64(gid);
654cf14a
JL
6095 /* better to leave device as zero when it is */
6096 data_offset->DevMajor = cpu_to_le64(MAJOR(args->device));
6097 data_offset->DevMinor = cpu_to_le64(MINOR(args->device));
6098 data_offset->Permissions = cpu_to_le64(mode);
6099
6100 if (S_ISREG(mode))
6101 data_offset->Type = cpu_to_le32(UNIX_FILE);
6102 else if (S_ISDIR(mode))
6103 data_offset->Type = cpu_to_le32(UNIX_DIR);
6104 else if (S_ISLNK(mode))
6105 data_offset->Type = cpu_to_le32(UNIX_SYMLINK);
6106 else if (S_ISCHR(mode))
6107 data_offset->Type = cpu_to_le32(UNIX_CHARDEV);
6108 else if (S_ISBLK(mode))
6109 data_offset->Type = cpu_to_le32(UNIX_BLOCKDEV);
6110 else if (S_ISFIFO(mode))
6111 data_offset->Type = cpu_to_le32(UNIX_FIFO);
6112 else if (S_ISSOCK(mode))
6113 data_offset->Type = cpu_to_le32(UNIX_SOCKET);
6114}
6115
3bbeeb3c 6116int
6d5786a3 6117CIFSSMBUnixSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
3bbeeb3c
JL
6118 const struct cifs_unix_set_info_args *args,
6119 u16 fid, u32 pid_of_opener)
6120{
6121 struct smb_com_transaction2_sfi_req *pSMB = NULL;
b2a3ad9c 6122 char *data_offset;
3bbeeb3c
JL
6123 int rc = 0;
6124 u16 params, param_offset, offset, byte_count, count;
6125
f96637be 6126 cifs_dbg(FYI, "Set Unix Info (via SetFileInfo)\n");
3bbeeb3c
JL
6127 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
6128
6129 if (rc)
6130 return rc;
6131
6132 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
6133 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
6134
6135 params = 6;
6136 pSMB->MaxSetupCount = 0;
6137 pSMB->Reserved = 0;
6138 pSMB->Flags = 0;
6139 pSMB->Timeout = 0;
6140 pSMB->Reserved2 = 0;
6141 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
6142 offset = param_offset + params;
6143
b2a3ad9c
JL
6144 data_offset = (char *)pSMB +
6145 offsetof(struct smb_hdr, Protocol) + offset;
6146
3bbeeb3c
JL
6147 count = sizeof(FILE_UNIX_BASIC_INFO);
6148
6149 pSMB->MaxParameterCount = cpu_to_le16(2);
6150 /* BB find max SMB PDU from sess */
6151 pSMB->MaxDataCount = cpu_to_le16(1000);
6152 pSMB->SetupCount = 1;
6153 pSMB->Reserved3 = 0;
6154 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
6155 byte_count = 3 /* pad */ + params + count;
6156 pSMB->DataCount = cpu_to_le16(count);
6157 pSMB->ParameterCount = cpu_to_le16(params);
6158 pSMB->TotalDataCount = pSMB->DataCount;
6159 pSMB->TotalParameterCount = pSMB->ParameterCount;
6160 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6161 pSMB->DataOffset = cpu_to_le16(offset);
6162 pSMB->Fid = fid;
6163 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
6164 pSMB->Reserved4 = 0;
be8e3b00 6165 inc_rfc1001_len(pSMB, byte_count);
3bbeeb3c
JL
6166 pSMB->ByteCount = cpu_to_le16(byte_count);
6167
b2a3ad9c 6168 cifs_fill_unix_set_info((FILE_UNIX_BASIC_INFO *)data_offset, args);
3bbeeb3c 6169
792af7b0 6170 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
da502f7d 6171 cifs_small_buf_release(pSMB);
3bbeeb3c 6172 if (rc)
f96637be
JP
6173 cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n",
6174 rc);
3bbeeb3c
JL
6175
6176 /* Note: On -EAGAIN error only caller can retry on handle based calls
6177 since file handle passed in no longer valid */
6178
6179 return rc;
6180}
6181
1da177e4 6182int
6d5786a3 6183CIFSSMBUnixSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
ff691e96 6184 const char *file_name,
01ea95e3
JL
6185 const struct cifs_unix_set_info_args *args,
6186 const struct nls_table *nls_codepage, int remap)
1da177e4
LT
6187{
6188 TRANSACTION2_SPI_REQ *pSMB = NULL;
6189 TRANSACTION2_SPI_RSP *pSMBr = NULL;
6190 int name_len;
6191 int rc = 0;
6192 int bytes_returned = 0;
6193 FILE_UNIX_BASIC_INFO *data_offset;
6194 __u16 params, param_offset, offset, count, byte_count;
6195
f96637be 6196 cifs_dbg(FYI, "In SetUID/GID/Mode\n");
1da177e4
LT
6197setPermsRetry:
6198 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6199 (void **) &pSMBr);
6200 if (rc)
6201 return rc;
6202
6203 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6204 name_len =
ff691e96 6205 cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
acbbb76a 6206 PATH_MAX, nls_codepage, remap);
1da177e4
LT
6207 name_len++; /* trailing null */
6208 name_len *= 2;
3e87d803 6209 } else { /* BB improve the check for buffer overruns BB */
ff691e96 6210 name_len = strnlen(file_name, PATH_MAX);
1da177e4 6211 name_len++; /* trailing null */
ff691e96 6212 strncpy(pSMB->FileName, file_name, name_len);
1da177e4
LT
6213 }
6214
6215 params = 6 + name_len;
26f57364 6216 count = sizeof(FILE_UNIX_BASIC_INFO);
1da177e4 6217 pSMB->MaxParameterCount = cpu_to_le16(2);
582d21e5
SF
6218 /* BB find max SMB PDU from sess structure BB */
6219 pSMB->MaxDataCount = cpu_to_le16(1000);
1da177e4
LT
6220 pSMB->MaxSetupCount = 0;
6221 pSMB->Reserved = 0;
6222 pSMB->Flags = 0;
6223 pSMB->Timeout = 0;
6224 pSMB->Reserved2 = 0;
6225 param_offset = offsetof(struct smb_com_transaction2_spi_req,
50c2f753 6226 InformationLevel) - 4;
1da177e4
LT
6227 offset = param_offset + params;
6228 data_offset =
6229 (FILE_UNIX_BASIC_INFO *) ((char *) &pSMB->hdr.Protocol +
6230 offset);
6231 memset(data_offset, 0, count);
6232 pSMB->DataOffset = cpu_to_le16(offset);
6233 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6234 pSMB->SetupCount = 1;
6235 pSMB->Reserved3 = 0;
6236 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6237 byte_count = 3 /* pad */ + params + count;
6238 pSMB->ParameterCount = cpu_to_le16(params);
6239 pSMB->DataCount = cpu_to_le16(count);
6240 pSMB->TotalParameterCount = pSMB->ParameterCount;
6241 pSMB->TotalDataCount = pSMB->DataCount;
6242 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
6243 pSMB->Reserved4 = 0;
be8e3b00 6244 inc_rfc1001_len(pSMB, byte_count);
1da177e4 6245
654cf14a 6246 cifs_fill_unix_set_info(data_offset, args);
1da177e4
LT
6247
6248 pSMB->ByteCount = cpu_to_le16(byte_count);
6249 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6250 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
ad7a2926 6251 if (rc)
f96637be 6252 cifs_dbg(FYI, "SetPathInfo (perms) returned %d\n", rc);
1da177e4 6253
0d817bc0 6254 cifs_buf_release(pSMB);
1da177e4
LT
6255 if (rc == -EAGAIN)
6256 goto setPermsRetry;
6257 return rc;
6258}
6259
1da177e4 6260#ifdef CONFIG_CIFS_XATTR
31c0519f
JL
6261/*
6262 * Do a path-based QUERY_ALL_EAS call and parse the result. This is a common
6263 * function used by listxattr and getxattr type calls. When ea_name is set,
6264 * it looks for that attribute name and stuffs that value into the EAData
6265 * buffer. When ea_name is NULL, it stuffs a list of attribute names into the
6266 * buffer. In both cases, the return value is either the length of the
6267 * resulting data or a negative error code. If EAData is a NULL pointer then
6268 * the data isn't copied to it, but the length is returned.
6269 */
1da177e4 6270ssize_t
6d5786a3 6271CIFSSMBQAllEAs(const unsigned int xid, struct cifs_tcon *tcon,
31c0519f
JL
6272 const unsigned char *searchName, const unsigned char *ea_name,
6273 char *EAData, size_t buf_size,
67b4c889 6274 struct cifs_sb_info *cifs_sb)
1da177e4
LT
6275{
6276 /* BB assumes one setup word */
6277 TRANSACTION2_QPI_REQ *pSMB = NULL;
6278 TRANSACTION2_QPI_RSP *pSMBr = NULL;
67b4c889
SF
6279 int remap = cifs_remap(cifs_sb);
6280 struct nls_table *nls_codepage = cifs_sb->local_nls;
1da177e4
LT
6281 int rc = 0;
6282 int bytes_returned;
6e462b9f 6283 int list_len;
f0d3868b 6284 struct fealist *ea_response_data;
50c2f753
SF
6285 struct fea *temp_fea;
6286 char *temp_ptr;
0cd126b5 6287 char *end_of_smb;
f0d3868b 6288 __u16 params, byte_count, data_offset;
5980fc96 6289 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
1da177e4 6290
f96637be 6291 cifs_dbg(FYI, "In Query All EAs path %s\n", searchName);
1da177e4
LT
6292QAllEAsRetry:
6293 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6294 (void **) &pSMBr);
6295 if (rc)
6296 return rc;
6297
6298 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6e462b9f 6299 list_len =
acbbb76a
SF
6300 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
6301 PATH_MAX, nls_codepage, remap);
6e462b9f
JL
6302 list_len++; /* trailing null */
6303 list_len *= 2;
1da177e4 6304 } else { /* BB improve the check for buffer overruns BB */
6e462b9f
JL
6305 list_len = strnlen(searchName, PATH_MAX);
6306 list_len++; /* trailing null */
6307 strncpy(pSMB->FileName, searchName, list_len);
1da177e4
LT
6308 }
6309
6e462b9f 6310 params = 2 /* level */ + 4 /* reserved */ + list_len /* includes NUL */;
1da177e4
LT
6311 pSMB->TotalDataCount = 0;
6312 pSMB->MaxParameterCount = cpu_to_le16(2);
582d21e5 6313 /* BB find exact max SMB PDU from sess structure BB */
e529614a 6314 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
1da177e4
LT
6315 pSMB->MaxSetupCount = 0;
6316 pSMB->Reserved = 0;
6317 pSMB->Flags = 0;
6318 pSMB->Timeout = 0;
6319 pSMB->Reserved2 = 0;
6320 pSMB->ParameterOffset = cpu_to_le16(offsetof(
50c2f753 6321 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
1da177e4
LT
6322 pSMB->DataCount = 0;
6323 pSMB->DataOffset = 0;
6324 pSMB->SetupCount = 1;
6325 pSMB->Reserved3 = 0;
6326 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
6327 byte_count = params + 1 /* pad */ ;
6328 pSMB->TotalParameterCount = cpu_to_le16(params);
6329 pSMB->ParameterCount = pSMB->TotalParameterCount;
6330 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_QUERY_ALL_EAS);
6331 pSMB->Reserved4 = 0;
be8e3b00 6332 inc_rfc1001_len(pSMB, byte_count);
1da177e4
LT
6333 pSMB->ByteCount = cpu_to_le16(byte_count);
6334
6335 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6336 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
6337 if (rc) {
f96637be 6338 cifs_dbg(FYI, "Send error in QueryAllEAs = %d\n", rc);
f0d3868b
JL
6339 goto QAllEAsOut;
6340 }
1da177e4 6341
f0d3868b
JL
6342
6343 /* BB also check enough total bytes returned */
6344 /* BB we need to improve the validity checking
6345 of these trans2 responses */
6346
6347 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
820a803f 6348 if (rc || get_bcc(&pSMBr->hdr) < 4) {
f0d3868b
JL
6349 rc = -EIO; /* bad smb */
6350 goto QAllEAsOut;
6351 }
6352
6353 /* check that length of list is not more than bcc */
6354 /* check that each entry does not go beyond length
6355 of list */
6356 /* check that each element of each entry does not
6357 go beyond end of list */
6358 /* validate_trans2_offsets() */
6359 /* BB check if start of smb + data_offset > &bcc+ bcc */
6360
6361 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
6362 ea_response_data = (struct fealist *)
6363 (((char *) &pSMBr->hdr.Protocol) + data_offset);
6364
6e462b9f 6365 list_len = le32_to_cpu(ea_response_data->list_len);
f96637be 6366 cifs_dbg(FYI, "ea length %d\n", list_len);
6e462b9f 6367 if (list_len <= 8) {
f96637be 6368 cifs_dbg(FYI, "empty EA list returned from server\n");
60977fcc
SF
6369 /* didn't find the named attribute */
6370 if (ea_name)
6371 rc = -ENODATA;
f0d3868b
JL
6372 goto QAllEAsOut;
6373 }
6374
0cd126b5 6375 /* make sure list_len doesn't go past end of SMB */
690c522f 6376 end_of_smb = (char *)pByteArea(&pSMBr->hdr) + get_bcc(&pSMBr->hdr);
0cd126b5 6377 if ((char *)ea_response_data + list_len > end_of_smb) {
f96637be 6378 cifs_dbg(FYI, "EA list appears to go beyond SMB\n");
0cd126b5
JL
6379 rc = -EIO;
6380 goto QAllEAsOut;
6381 }
6382
f0d3868b 6383 /* account for ea list len */
6e462b9f 6384 list_len -= 4;
f0d3868b
JL
6385 temp_fea = ea_response_data->list;
6386 temp_ptr = (char *)temp_fea;
6e462b9f 6387 while (list_len > 0) {
122ca007 6388 unsigned int name_len;
f0d3868b 6389 __u16 value_len;
0cd126b5 6390
6e462b9f 6391 list_len -= 4;
f0d3868b 6392 temp_ptr += 4;
0cd126b5
JL
6393 /* make sure we can read name_len and value_len */
6394 if (list_len < 0) {
f96637be 6395 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
0cd126b5
JL
6396 rc = -EIO;
6397 goto QAllEAsOut;
6398 }
6399
6400 name_len = temp_fea->name_len;
6401 value_len = le16_to_cpu(temp_fea->value_len);
6402 list_len -= name_len + 1 + value_len;
6403 if (list_len < 0) {
f96637be 6404 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
0cd126b5
JL
6405 rc = -EIO;
6406 goto QAllEAsOut;
6407 }
6408
31c0519f 6409 if (ea_name) {
91d065c4 6410 if (ea_name_len == name_len &&
ac423446 6411 memcmp(ea_name, temp_ptr, name_len) == 0) {
31c0519f
JL
6412 temp_ptr += name_len + 1;
6413 rc = value_len;
6414 if (buf_size == 0)
6415 goto QAllEAsOut;
6416 if ((size_t)value_len > buf_size) {
6417 rc = -ERANGE;
6418 goto QAllEAsOut;
6419 }
6420 memcpy(EAData, temp_ptr, value_len);
6421 goto QAllEAsOut;
6422 }
f0d3868b 6423 } else {
31c0519f
JL
6424 /* account for prefix user. and trailing null */
6425 rc += (5 + 1 + name_len);
6426 if (rc < (int) buf_size) {
6427 memcpy(EAData, "user.", 5);
6428 EAData += 5;
6429 memcpy(EAData, temp_ptr, name_len);
6430 EAData += name_len;
6431 /* null terminate name */
6432 *EAData = 0;
6433 ++EAData;
6434 } else if (buf_size == 0) {
6435 /* skip copy - calc size only */
6436 } else {
6437 /* stop before overrun buffer */
6438 rc = -ERANGE;
6439 break;
6440 }
1da177e4 6441 }
0cd126b5 6442 temp_ptr += name_len + 1 + value_len;
f0d3868b 6443 temp_fea = (struct fea *)temp_ptr;
1da177e4 6444 }
f0d3868b 6445
31c0519f
JL
6446 /* didn't find the named attribute */
6447 if (ea_name)
6448 rc = -ENODATA;
6449
f0d3868b 6450QAllEAsOut:
0d817bc0 6451 cifs_buf_release(pSMB);
1da177e4
LT
6452 if (rc == -EAGAIN)
6453 goto QAllEAsRetry;
6454
6455 return (ssize_t)rc;
6456}
6457
1da177e4 6458int
6d5786a3
PS
6459CIFSSMBSetEA(const unsigned int xid, struct cifs_tcon *tcon,
6460 const char *fileName, const char *ea_name, const void *ea_value,
50c2f753 6461 const __u16 ea_value_len, const struct nls_table *nls_codepage,
5517554e 6462 struct cifs_sb_info *cifs_sb)
1da177e4
LT
6463{
6464 struct smb_com_transaction2_spi_req *pSMB = NULL;
6465 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
6466 struct fealist *parm_data;
6467 int name_len;
6468 int rc = 0;
6469 int bytes_returned = 0;
6470 __u16 params, param_offset, byte_count, offset, count;
5517554e 6471 int remap = cifs_remap(cifs_sb);
1da177e4 6472
f96637be 6473 cifs_dbg(FYI, "In SetEA\n");
1da177e4
LT
6474SetEARetry:
6475 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6476 (void **) &pSMBr);
6477 if (rc)
6478 return rc;
6479
6480 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6481 name_len =
acbbb76a
SF
6482 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
6483 PATH_MAX, nls_codepage, remap);
1da177e4
LT
6484 name_len++; /* trailing null */
6485 name_len *= 2;
50c2f753 6486 } else { /* BB improve the check for buffer overruns BB */
1da177e4
LT
6487 name_len = strnlen(fileName, PATH_MAX);
6488 name_len++; /* trailing null */
6489 strncpy(pSMB->FileName, fileName, name_len);
6490 }
6491
6492 params = 6 + name_len;
6493
6494 /* done calculating parms using name_len of file name,
6495 now use name_len to calculate length of ea name
6496 we are going to create in the inode xattrs */
790fe579 6497 if (ea_name == NULL)
1da177e4
LT
6498 name_len = 0;
6499 else
50c2f753 6500 name_len = strnlen(ea_name, 255);
1da177e4 6501
dae5dbdb 6502 count = sizeof(*parm_data) + ea_value_len + name_len;
1da177e4 6503 pSMB->MaxParameterCount = cpu_to_le16(2);
582d21e5
SF
6504 /* BB find max SMB PDU from sess */
6505 pSMB->MaxDataCount = cpu_to_le16(1000);
1da177e4
LT
6506 pSMB->MaxSetupCount = 0;
6507 pSMB->Reserved = 0;
6508 pSMB->Flags = 0;
6509 pSMB->Timeout = 0;
6510 pSMB->Reserved2 = 0;
6511 param_offset = offsetof(struct smb_com_transaction2_spi_req,
50c2f753 6512 InformationLevel) - 4;
1da177e4
LT
6513 offset = param_offset + params;
6514 pSMB->InformationLevel =
6515 cpu_to_le16(SMB_SET_FILE_EA);
6516
ade7db99 6517 parm_data = (void *)pSMB + offsetof(struct smb_hdr, Protocol) + offset;
1da177e4
LT
6518 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6519 pSMB->DataOffset = cpu_to_le16(offset);
6520 pSMB->SetupCount = 1;
6521 pSMB->Reserved3 = 0;
6522 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6523 byte_count = 3 /* pad */ + params + count;
6524 pSMB->DataCount = cpu_to_le16(count);
6525 parm_data->list_len = cpu_to_le32(count);
6526 parm_data->list[0].EA_flags = 0;
6527 /* we checked above that name len is less than 255 */
53b3531b 6528 parm_data->list[0].name_len = (__u8)name_len;
1da177e4 6529 /* EA names are always ASCII */
790fe579 6530 if (ea_name)
50c2f753 6531 strncpy(parm_data->list[0].name, ea_name, name_len);
1da177e4
LT
6532 parm_data->list[0].name[name_len] = 0;
6533 parm_data->list[0].value_len = cpu_to_le16(ea_value_len);
6534 /* caller ensures that ea_value_len is less than 64K but
6535 we need to ensure that it fits within the smb */
6536
50c2f753
SF
6537 /*BB add length check to see if it would fit in
6538 negotiated SMB buffer size BB */
790fe579
SF
6539 /* if (ea_value_len > buffer_size - 512 (enough for header)) */
6540 if (ea_value_len)
50c2f753
SF
6541 memcpy(parm_data->list[0].name+name_len+1,
6542 ea_value, ea_value_len);
1da177e4
LT
6543
6544 pSMB->TotalDataCount = pSMB->DataCount;
6545 pSMB->ParameterCount = cpu_to_le16(params);
6546 pSMB->TotalParameterCount = pSMB->ParameterCount;
6547 pSMB->Reserved4 = 0;
be8e3b00 6548 inc_rfc1001_len(pSMB, byte_count);
1da177e4
LT
6549 pSMB->ByteCount = cpu_to_le16(byte_count);
6550 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6551 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
ad7a2926 6552 if (rc)
f96637be 6553 cifs_dbg(FYI, "SetPathInfo (EA) returned %d\n", rc);
1da177e4
LT
6554
6555 cifs_buf_release(pSMB);
6556
6557 if (rc == -EAGAIN)
6558 goto SetEARetry;
6559
6560 return rc;
6561}
1da177e4 6562#endif
0eff0e26
SF
6563
6564#ifdef CONFIG_CIFS_DNOTIFY_EXPERIMENTAL /* BB unused temporarily */
6565/*
6566 * Years ago the kernel added a "dnotify" function for Samba server,
6567 * to allow network clients (such as Windows) to display updated
6568 * lists of files in directory listings automatically when
6569 * files are added by one user when another user has the
6570 * same directory open on their desktop. The Linux cifs kernel
6571 * client hooked into the kernel side of this interface for
6572 * the same reason, but ironically when the VFS moved from
6573 * "dnotify" to "inotify" it became harder to plug in Linux
6574 * network file system clients (the most obvious use case
6575 * for notify interfaces is when multiple users can update
6576 * the contents of the same directory - exactly what network
6577 * file systems can do) although the server (Samba) could
6578 * still use it. For the short term we leave the worker
6579 * function ifdeffed out (below) until inotify is fixed
6580 * in the VFS to make it easier to plug in network file
6581 * system clients. If inotify turns out to be permanently
6582 * incompatible for network fs clients, we could instead simply
6583 * expose this config flag by adding a future cifs (and smb2) notify ioctl.
6584 */
6d5786a3 6585int CIFSSMBNotify(const unsigned int xid, struct cifs_tcon *tcon,
0eff0e26
SF
6586 const int notify_subdirs, const __u16 netfid,
6587 __u32 filter, struct file *pfile, int multishot,
6588 const struct nls_table *nls_codepage)
6589{
6590 int rc = 0;
6591 struct smb_com_transaction_change_notify_req *pSMB = NULL;
6592 struct smb_com_ntransaction_change_notify_rsp *pSMBr = NULL;
6593 struct dir_notify_req *dnotify_req;
6594 int bytes_returned;
6595
f96637be 6596 cifs_dbg(FYI, "In CIFSSMBNotify for file handle %d\n", (int)netfid);
0eff0e26
SF
6597 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
6598 (void **) &pSMBr);
6599 if (rc)
6600 return rc;
6601
6602 pSMB->TotalParameterCount = 0 ;
6603 pSMB->TotalDataCount = 0;
6604 pSMB->MaxParameterCount = cpu_to_le32(2);
c974befa 6605 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
0eff0e26
SF
6606 pSMB->MaxSetupCount = 4;
6607 pSMB->Reserved = 0;
6608 pSMB->ParameterOffset = 0;
6609 pSMB->DataCount = 0;
6610 pSMB->DataOffset = 0;
6611 pSMB->SetupCount = 4; /* single byte does not need le conversion */
6612 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_NOTIFY_CHANGE);
6613 pSMB->ParameterCount = pSMB->TotalParameterCount;
6614 if (notify_subdirs)
6615 pSMB->WatchTree = 1; /* one byte - no le conversion needed */
6616 pSMB->Reserved2 = 0;
6617 pSMB->CompletionFilter = cpu_to_le32(filter);
6618 pSMB->Fid = netfid; /* file handle always le */
6619 pSMB->ByteCount = 0;
6620
6621 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6622 (struct smb_hdr *)pSMBr, &bytes_returned,
6623 CIFS_ASYNC_OP);
6624 if (rc) {
f96637be 6625 cifs_dbg(FYI, "Error in Notify = %d\n", rc);
0eff0e26
SF
6626 } else {
6627 /* Add file to outstanding requests */
6628 /* BB change to kmem cache alloc */
6629 dnotify_req = kmalloc(
6630 sizeof(struct dir_notify_req),
6631 GFP_KERNEL);
6632 if (dnotify_req) {
6633 dnotify_req->Pid = pSMB->hdr.Pid;
6634 dnotify_req->PidHigh = pSMB->hdr.PidHigh;
6635 dnotify_req->Mid = pSMB->hdr.Mid;
6636 dnotify_req->Tid = pSMB->hdr.Tid;
6637 dnotify_req->Uid = pSMB->hdr.Uid;
6638 dnotify_req->netfid = netfid;
6639 dnotify_req->pfile = pfile;
6640 dnotify_req->filter = filter;
6641 dnotify_req->multishot = multishot;
6642 spin_lock(&GlobalMid_Lock);
6643 list_add_tail(&dnotify_req->lhead,
6644 &GlobalDnotifyReqList);
6645 spin_unlock(&GlobalMid_Lock);
6646 } else
6647 rc = -ENOMEM;
6648 }
6649 cifs_buf_release(pSMB);
6650 return rc;
6651}
6652#endif /* was needed for dnotify, and will be needed for inotify when VFS fix */