]> Git Repo - linux.git/blame - fs/cifs/cifssmb.c
Merge tag 'hsi-for-5.1' of git://git.kernel.org/pub/scm/linux/kernel/git/sre/linux-hsi
[linux.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 ([email protected])
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) {
15bc77f9 142 snprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$",
08744015 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
15bc77f9 175 snprintf(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
PS
1607 if (server->ops->is_status_pending &&
1608 server->ops->is_status_pending(buf, server, 0)) {
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) {
2128 cifs_dbg(VFS, "No writable handles for inode\n");
2129 rc = -EBADF;
2130 break;
2131 }
2132 wdata2->pid = wdata2->cfile->pid;
2133 rc = server->ops->async_writev(wdata2, cifs_writedata_release);
2134
2135 for (j = 0; j < nr_pages; j++) {
2136 unlock_page(wdata2->pages[j]);
9a66396f 2137 if (rc != 0 && !is_retryable_error(rc)) {
7f6c5008
PS
2138 SetPageError(wdata2->pages[j]);
2139 end_page_writeback(wdata2->pages[j]);
09cbfeaf 2140 put_page(wdata2->pages[j]);
7f6c5008
PS
2141 }
2142 }
2143
2144 if (rc) {
2145 kref_put(&wdata2->refcount, cifs_writedata_release);
9a66396f 2146 if (is_retryable_error(rc))
7f6c5008
PS
2147 continue;
2148 break;
2149 }
2150
2151 rest_len -= cur_len;
2152 i += nr_pages;
2153 } while (i < wdata->nr_pages);
c28c89fc 2154
9a66396f
PS
2155 if (rc != 0 && !is_retryable_error(rc))
2156 mapping_set_error(inode->i_mapping, rc);
c28c89fc
JL
2157 kref_put(&wdata->refcount, cifs_writedata_release);
2158}
2159
c2e87640 2160void
c28c89fc
JL
2161cifs_writev_complete(struct work_struct *work)
2162{
2163 struct cifs_writedata *wdata = container_of(work,
2164 struct cifs_writedata, work);
2b0143b5 2165 struct inode *inode = d_inode(wdata->cfile->dentry);
c28c89fc
JL
2166 int i = 0;
2167
2168 if (wdata->result == 0) {
597b027f 2169 spin_lock(&inode->i_lock);
c28c89fc 2170 cifs_update_eof(CIFS_I(inode), wdata->offset, wdata->bytes);
597b027f 2171 spin_unlock(&inode->i_lock);
c28c89fc
JL
2172 cifs_stats_bytes_written(tlink_tcon(wdata->cfile->tlink),
2173 wdata->bytes);
2174 } else if (wdata->sync_mode == WB_SYNC_ALL && wdata->result == -EAGAIN)
2175 return cifs_writev_requeue(wdata);
2176
2177 for (i = 0; i < wdata->nr_pages; i++) {
2178 struct page *page = wdata->pages[i];
2179 if (wdata->result == -EAGAIN)
2180 __set_page_dirty_nobuffers(page);
2181 else if (wdata->result < 0)
2182 SetPageError(page);
2183 end_page_writeback(page);
09cbfeaf 2184 put_page(page);
c28c89fc
JL
2185 }
2186 if (wdata->result != -EAGAIN)
2187 mapping_set_error(inode->i_mapping, wdata->result);
2188 kref_put(&wdata->refcount, cifs_writedata_release);
2189}
2190
2191struct cifs_writedata *
c2e87640 2192cifs_writedata_alloc(unsigned int nr_pages, work_func_t complete)
8e7360f6
LL
2193{
2194 struct page **pages =
6396bb22 2195 kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
8e7360f6
LL
2196 if (pages)
2197 return cifs_writedata_direct_alloc(pages, complete);
2198
2199 return NULL;
2200}
2201
2202struct cifs_writedata *
2203cifs_writedata_direct_alloc(struct page **pages, work_func_t complete)
c28c89fc
JL
2204{
2205 struct cifs_writedata *wdata;
2206
8e7360f6 2207 wdata = kzalloc(sizeof(*wdata), GFP_NOFS);
c28c89fc 2208 if (wdata != NULL) {
8e7360f6 2209 wdata->pages = pages;
c28c89fc 2210 kref_init(&wdata->refcount);
da82f7e7
JL
2211 INIT_LIST_HEAD(&wdata->list);
2212 init_completion(&wdata->done);
2213 INIT_WORK(&wdata->work, complete);
c28c89fc
JL
2214 }
2215 return wdata;
2216}
2217
2218/*
7c9421e1 2219 * Check the mid_state and signature on received buffer (if any), and queue the
c28c89fc
JL
2220 * workqueue completion task.
2221 */
2222static void
2223cifs_writev_callback(struct mid_q_entry *mid)
2224{
2225 struct cifs_writedata *wdata = mid->callback_data;
96daf2b0 2226 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
c28c89fc
JL
2227 unsigned int written;
2228 WRITE_RSP *smb = (WRITE_RSP *)mid->resp_buf;
2229
7c9421e1 2230 switch (mid->mid_state) {
c28c89fc
JL
2231 case MID_RESPONSE_RECEIVED:
2232 wdata->result = cifs_check_receive(mid, tcon->ses->server, 0);
2233 if (wdata->result != 0)
2234 break;
2235
2236 written = le16_to_cpu(smb->CountHigh);
2237 written <<= 16;
2238 written += le16_to_cpu(smb->Count);
2239 /*
2240 * Mask off high 16 bits when bytes written as returned
2241 * by the server is greater than bytes requested by the
2242 * client. OS/2 servers are known to set incorrect
2243 * CountHigh values.
2244 */
2245 if (written > wdata->bytes)
2246 written &= 0xFFFF;
2247
2248 if (written < wdata->bytes)
2249 wdata->result = -ENOSPC;
2250 else
2251 wdata->bytes = written;
2252 break;
2253 case MID_REQUEST_SUBMITTED:
2254 case MID_RETRY_NEEDED:
2255 wdata->result = -EAGAIN;
2256 break;
2257 default:
2258 wdata->result = -EIO;
2259 break;
2260 }
2261
da472fc8 2262 queue_work(cifsiod_wq, &wdata->work);
c28c89fc 2263 DeleteMidQEntry(mid);
a891f0f8 2264 add_credits(tcon->ses->server, 1, 0);
c28c89fc
JL
2265}
2266
2267/* cifs_async_writev - send an async write, and set up mid to handle result */
2268int
4a5c80d7
SF
2269cifs_async_writev(struct cifs_writedata *wdata,
2270 void (*release)(struct kref *kref))
c28c89fc 2271{
eddb079d 2272 int rc = -EACCES;
c28c89fc
JL
2273 WRITE_REQ *smb = NULL;
2274 int wct;
96daf2b0 2275 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
738f9de5 2276 struct kvec iov[2];
fec344e3 2277 struct smb_rqst rqst = { };
c28c89fc
JL
2278
2279 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
2280 wct = 14;
2281 } else {
2282 wct = 12;
2283 if (wdata->offset >> 32 > 0) {
2284 /* can not handle big offset for old srv */
2285 return -EIO;
2286 }
2287 }
2288
2289 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **)&smb);
2290 if (rc)
2291 goto async_writev_out;
2292
fe5f5d2e
JL
2293 smb->hdr.Pid = cpu_to_le16((__u16)wdata->pid);
2294 smb->hdr.PidHigh = cpu_to_le16((__u16)(wdata->pid >> 16));
fa2989f4 2295
c28c89fc 2296 smb->AndXCommand = 0xFF; /* none */
4b4de76e 2297 smb->Fid = wdata->cfile->fid.netfid;
c28c89fc
JL
2298 smb->OffsetLow = cpu_to_le32(wdata->offset & 0xFFFFFFFF);
2299 if (wct == 14)
2300 smb->OffsetHigh = cpu_to_le32(wdata->offset >> 32);
2301 smb->Reserved = 0xFFFFFFFF;
2302 smb->WriteMode = 0;
2303 smb->Remaining = 0;
2304
2305 smb->DataOffset =
2306 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
2307
2308 /* 4 for RFC1001 length + 1 for BCC */
738f9de5
PS
2309 iov[0].iov_len = 4;
2310 iov[0].iov_base = smb;
2311 iov[1].iov_len = get_rfc1002_length(smb) + 1;
2312 iov[1].iov_base = (char *)smb + 4;
c28c89fc 2313
738f9de5
PS
2314 rqst.rq_iov = iov;
2315 rqst.rq_nvec = 2;
eddb079d 2316 rqst.rq_pages = wdata->pages;
6d3adb23 2317 rqst.rq_offset = wdata->page_offset;
eddb079d
JL
2318 rqst.rq_npages = wdata->nr_pages;
2319 rqst.rq_pagesz = wdata->pagesz;
2320 rqst.rq_tailsz = wdata->tailsz;
c28c89fc 2321
f96637be
JP
2322 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2323 wdata->offset, wdata->bytes);
c28c89fc
JL
2324
2325 smb->DataLengthLow = cpu_to_le16(wdata->bytes & 0xFFFF);
2326 smb->DataLengthHigh = cpu_to_le16(wdata->bytes >> 16);
2327
2328 if (wct == 14) {
2329 inc_rfc1001_len(&smb->hdr, wdata->bytes + 1);
2330 put_bcc(wdata->bytes + 1, &smb->hdr);
2331 } else {
2332 /* wct == 12 */
2333 struct smb_com_writex_req *smbw =
2334 (struct smb_com_writex_req *)smb;
2335 inc_rfc1001_len(&smbw->hdr, wdata->bytes + 5);
2336 put_bcc(wdata->bytes + 5, &smbw->hdr);
738f9de5 2337 iov[1].iov_len += 4; /* pad bigger by four bytes */
c28c89fc
JL
2338 }
2339
2340 kref_get(&wdata->refcount);
fec344e3 2341 rc = cifs_call_async(tcon->ses->server, &rqst, NULL,
9b7c18a2 2342 cifs_writev_callback, NULL, wdata, 0);
c28c89fc
JL
2343
2344 if (rc == 0)
44c58186 2345 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
c28c89fc 2346 else
4a5c80d7 2347 kref_put(&wdata->refcount, release);
c28c89fc 2348
c28c89fc
JL
2349async_writev_out:
2350 cifs_small_buf_release(smb);
c28c89fc
JL
2351 return rc;
2352}
2353
d6e04ae6 2354int
6d5786a3 2355CIFSSMBWrite2(const unsigned int xid, struct cifs_io_parms *io_parms,
ba9ad725 2356 unsigned int *nbytes, struct kvec *iov, int n_vec)
1da177e4
LT
2357{
2358 int rc = -EACCES;
2359 WRITE_REQ *pSMB = NULL;
ec637e3f 2360 int wct;
d6e04ae6 2361 int smb_hdr_len;
ec637e3f 2362 int resp_buf_type = 0;
fa2989f4
PS
2363 __u32 pid = io_parms->pid;
2364 __u16 netfid = io_parms->netfid;
2365 __u64 offset = io_parms->offset;
96daf2b0 2366 struct cifs_tcon *tcon = io_parms->tcon;
fa2989f4 2367 unsigned int count = io_parms->length;
da502f7d 2368 struct kvec rsp_iov;
1da177e4 2369
fbec9ab9
JL
2370 *nbytes = 0;
2371
f96637be 2372 cifs_dbg(FYI, "write2 at %lld %d bytes\n", (long long)offset, count);
ff7feac9 2373
4c3130ef 2374 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
8cc64c6e 2375 wct = 14;
4c3130ef 2376 } else {
8cc64c6e 2377 wct = 12;
4c3130ef
SF
2378 if ((offset >> 32) > 0) {
2379 /* can not handle big offset for old srv */
2380 return -EIO;
2381 }
2382 }
8cc64c6e 2383 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB);
1da177e4
LT
2384 if (rc)
2385 return rc;
fa2989f4
PS
2386
2387 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
2388 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
2389
1da177e4
LT
2390 /* tcon and ses pointer are checked in smb_init */
2391 if (tcon->ses->server == NULL)
2392 return -ECONNABORTED;
2393
d6e04ae6 2394 pSMB->AndXCommand = 0xFF; /* none */
1da177e4
LT
2395 pSMB->Fid = netfid;
2396 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
790fe579 2397 if (wct == 14)
8cc64c6e 2398 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
1da177e4
LT
2399 pSMB->Reserved = 0xFFFFFFFF;
2400 pSMB->WriteMode = 0;
2401 pSMB->Remaining = 0;
d6e04ae6 2402
1da177e4 2403 pSMB->DataOffset =
50c2f753 2404 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
1da177e4 2405
3e84469d
SF
2406 pSMB->DataLengthLow = cpu_to_le16(count & 0xFFFF);
2407 pSMB->DataLengthHigh = cpu_to_le16(count >> 16);
be8e3b00
SF
2408 /* header + 1 byte pad */
2409 smb_hdr_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 1;
790fe579 2410 if (wct == 14)
be8e3b00 2411 inc_rfc1001_len(pSMB, count + 1);
8cc64c6e 2412 else /* wct == 12 */
be8e3b00 2413 inc_rfc1001_len(pSMB, count + 5); /* smb data starts later */
790fe579 2414 if (wct == 14)
8cc64c6e
SF
2415 pSMB->ByteCount = cpu_to_le16(count + 1);
2416 else /* wct == 12 */ /* bigger pad, smaller smb hdr, keep offset ok */ {
50c2f753 2417 struct smb_com_writex_req *pSMBW =
8cc64c6e
SF
2418 (struct smb_com_writex_req *)pSMB;
2419 pSMBW->ByteCount = cpu_to_le16(count + 5);
2420 }
3e84469d 2421 iov[0].iov_base = pSMB;
790fe579 2422 if (wct == 14)
ec637e3f
SF
2423 iov[0].iov_len = smb_hdr_len + 4;
2424 else /* wct == 12 pad bigger by four bytes */
2425 iov[0].iov_len = smb_hdr_len + 8;
50c2f753 2426
da502f7d
PS
2427 rc = SendReceive2(xid, tcon->ses, iov, n_vec + 1, &resp_buf_type, 0,
2428 &rsp_iov);
2429 cifs_small_buf_release(pSMB);
44c58186 2430 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
1da177e4 2431 if (rc) {
f96637be 2432 cifs_dbg(FYI, "Send error Write2 = %d\n", rc);
790fe579 2433 } else if (resp_buf_type == 0) {
ec637e3f
SF
2434 /* presumably this can not happen, but best to be safe */
2435 rc = -EIO;
d6e04ae6 2436 } else {
da502f7d 2437 WRITE_RSP *pSMBr = (WRITE_RSP *)rsp_iov.iov_base;
d6e04ae6
SF
2438 *nbytes = le16_to_cpu(pSMBr->CountHigh);
2439 *nbytes = (*nbytes) << 16;
2440 *nbytes += le16_to_cpu(pSMBr->Count);
6513a81e
SJ
2441
2442 /*
2443 * Mask off high 16 bits when bytes written as returned by the
2444 * server is greater than bytes requested by the client. OS/2
2445 * servers are known to set incorrect CountHigh values.
2446 */
2447 if (*nbytes > count)
2448 *nbytes &= 0xFFFF;
50c2f753 2449 }
1da177e4 2450
da502f7d 2451 free_rsp_buf(resp_buf_type, rsp_iov.iov_base);
1da177e4 2452
50c2f753 2453 /* Note: On -EAGAIN error only caller can retry on handle based calls
1da177e4
LT
2454 since file handle passed in no longer valid */
2455
2456 return rc;
2457}
d6e04ae6 2458
6d5786a3
PS
2459int cifs_lockv(const unsigned int xid, struct cifs_tcon *tcon,
2460 const __u16 netfid, const __u8 lock_type, const __u32 num_unlock,
9ee305b7
PS
2461 const __u32 num_lock, LOCKING_ANDX_RANGE *buf)
2462{
2463 int rc = 0;
2464 LOCK_REQ *pSMB = NULL;
2465 struct kvec iov[2];
da502f7d 2466 struct kvec rsp_iov;
9ee305b7
PS
2467 int resp_buf_type;
2468 __u16 count;
2469
f96637be
JP
2470 cifs_dbg(FYI, "cifs_lockv num lock %d num unlock %d\n",
2471 num_lock, num_unlock);
9ee305b7
PS
2472
2473 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2474 if (rc)
2475 return rc;
2476
2477 pSMB->Timeout = 0;
2478 pSMB->NumberOfLocks = cpu_to_le16(num_lock);
2479 pSMB->NumberOfUnlocks = cpu_to_le16(num_unlock);
2480 pSMB->LockType = lock_type;
2481 pSMB->AndXCommand = 0xFF; /* none */
2482 pSMB->Fid = netfid; /* netfid stays le */
2483
2484 count = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2485 inc_rfc1001_len(pSMB, count);
2486 pSMB->ByteCount = cpu_to_le16(count);
2487
2488 iov[0].iov_base = (char *)pSMB;
2489 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4 -
2490 (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2491 iov[1].iov_base = (char *)buf;
2492 iov[1].iov_len = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2493
44c58186 2494 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
da502f7d
PS
2495 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP,
2496 &rsp_iov);
2497 cifs_small_buf_release(pSMB);
9ee305b7 2498 if (rc)
f96637be 2499 cifs_dbg(FYI, "Send error in cifs_lockv = %d\n", rc);
9ee305b7
PS
2500
2501 return rc;
2502}
d6e04ae6 2503
1da177e4 2504int
6d5786a3 2505CIFSSMBLock(const unsigned int xid, struct cifs_tcon *tcon,
03776f45 2506 const __u16 smb_file_id, const __u32 netpid, const __u64 len,
1da177e4 2507 const __u64 offset, const __u32 numUnlock,
12fed00d
PS
2508 const __u32 numLock, const __u8 lockType,
2509 const bool waitFlag, const __u8 oplock_level)
1da177e4
LT
2510{
2511 int rc = 0;
2512 LOCK_REQ *pSMB = NULL;
aaa9bbe0 2513/* LOCK_RSP *pSMBr = NULL; */ /* No response data other than rc to parse */
1da177e4 2514 int bytes_returned;
a891f0f8 2515 int flags = 0;
1da177e4
LT
2516 __u16 count;
2517
f96637be
JP
2518 cifs_dbg(FYI, "CIFSSMBLock timeout %d numLock %d\n",
2519 (int)waitFlag, numLock);
46810cbf
SF
2520 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2521
1da177e4
LT
2522 if (rc)
2523 return rc;
2524
790fe579 2525 if (lockType == LOCKING_ANDX_OPLOCK_RELEASE) {
a891f0f8
PS
2526 /* no response expected */
2527 flags = CIFS_ASYNC_OP | CIFS_OBREAK_OP;
1da177e4 2528 pSMB->Timeout = 0;
4b18f2a9 2529 } else if (waitFlag) {
a891f0f8 2530 flags = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
1da177e4
LT
2531 pSMB->Timeout = cpu_to_le32(-1);/* blocking - do not time out */
2532 } else {
2533 pSMB->Timeout = 0;
2534 }
2535
2536 pSMB->NumberOfLocks = cpu_to_le16(numLock);
2537 pSMB->NumberOfUnlocks = cpu_to_le16(numUnlock);
2538 pSMB->LockType = lockType;
12fed00d 2539 pSMB->OplockLevel = oplock_level;
1da177e4
LT
2540 pSMB->AndXCommand = 0xFF; /* none */
2541 pSMB->Fid = smb_file_id; /* netfid stays le */
2542
790fe579 2543 if ((numLock != 0) || (numUnlock != 0)) {
03776f45 2544 pSMB->Locks[0].Pid = cpu_to_le16(netpid);
1da177e4
LT
2545 /* BB where to store pid high? */
2546 pSMB->Locks[0].LengthLow = cpu_to_le32((u32)len);
2547 pSMB->Locks[0].LengthHigh = cpu_to_le32((u32)(len>>32));
2548 pSMB->Locks[0].OffsetLow = cpu_to_le32((u32)offset);
2549 pSMB->Locks[0].OffsetHigh = cpu_to_le32((u32)(offset>>32));
2550 count = sizeof(LOCKING_ANDX_RANGE);
2551 } else {
2552 /* oplock break */
2553 count = 0;
2554 }
be8e3b00 2555 inc_rfc1001_len(pSMB, count);
1da177e4
LT
2556 pSMB->ByteCount = cpu_to_le16(count);
2557
da502f7d 2558 if (waitFlag)
7ee1af76 2559 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
aaa9bbe0 2560 (struct smb_hdr *) pSMB, &bytes_returned);
da502f7d 2561 else
a891f0f8 2562 rc = SendReceiveNoRsp(xid, tcon->ses, (char *)pSMB, flags);
da502f7d 2563 cifs_small_buf_release(pSMB);
44c58186 2564 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
ad7a2926 2565 if (rc)
f96637be 2566 cifs_dbg(FYI, "Send error in Lock = %d\n", rc);
1da177e4 2567
50c2f753 2568 /* Note: On -EAGAIN error only caller can retry on handle based calls
1da177e4
LT
2569 since file handle passed in no longer valid */
2570 return rc;
2571}
2572
08547b03 2573int
6d5786a3 2574CIFSSMBPosixLock(const unsigned int xid, struct cifs_tcon *tcon,
c5fd363d
JL
2575 const __u16 smb_file_id, const __u32 netpid,
2576 const loff_t start_offset, const __u64 len,
2577 struct file_lock *pLockData, const __u16 lock_type,
2578 const bool waitFlag)
08547b03
SF
2579{
2580 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2581 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
08547b03
SF
2582 struct cifs_posix_lock *parm_data;
2583 int rc = 0;
3a5ff61c 2584 int timeout = 0;
08547b03 2585 int bytes_returned = 0;
133672ef 2586 int resp_buf_type = 0;
08547b03 2587 __u16 params, param_offset, offset, byte_count, count;
133672ef 2588 struct kvec iov[1];
da502f7d 2589 struct kvec rsp_iov;
08547b03 2590
f96637be 2591 cifs_dbg(FYI, "Posix Lock\n");
fc94cdb9 2592
08547b03
SF
2593 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
2594
2595 if (rc)
2596 return rc;
2597
2598 pSMBr = (struct smb_com_transaction2_sfi_rsp *)pSMB;
2599
50c2f753 2600 params = 6;
08547b03
SF
2601 pSMB->MaxSetupCount = 0;
2602 pSMB->Reserved = 0;
2603 pSMB->Flags = 0;
08547b03
SF
2604 pSMB->Reserved2 = 0;
2605 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2606 offset = param_offset + params;
2607
08547b03
SF
2608 count = sizeof(struct cifs_posix_lock);
2609 pSMB->MaxParameterCount = cpu_to_le16(2);
ad7a2926 2610 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
08547b03
SF
2611 pSMB->SetupCount = 1;
2612 pSMB->Reserved3 = 0;
c5fd363d 2613 if (pLockData)
08547b03
SF
2614 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
2615 else
2616 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2617 byte_count = 3 /* pad */ + params + count;
2618 pSMB->DataCount = cpu_to_le16(count);
2619 pSMB->ParameterCount = cpu_to_le16(params);
2620 pSMB->TotalDataCount = pSMB->DataCount;
2621 pSMB->TotalParameterCount = pSMB->ParameterCount;
2622 pSMB->ParameterOffset = cpu_to_le16(param_offset);
50c2f753 2623 parm_data = (struct cifs_posix_lock *)
08547b03
SF
2624 (((char *) &pSMB->hdr.Protocol) + offset);
2625
2626 parm_data->lock_type = cpu_to_le16(lock_type);
790fe579 2627 if (waitFlag) {
133672ef 2628 timeout = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
cec6815a 2629 parm_data->lock_flags = cpu_to_le16(1);
3a5ff61c
SF
2630 pSMB->Timeout = cpu_to_le32(-1);
2631 } else
2632 pSMB->Timeout = 0;
2633
4f6bcec9 2634 parm_data->pid = cpu_to_le32(netpid);
c5fd363d 2635 parm_data->start = cpu_to_le64(start_offset);
cec6815a 2636 parm_data->length = cpu_to_le64(len); /* normalize negative numbers */
08547b03
SF
2637
2638 pSMB->DataOffset = cpu_to_le16(offset);
f26282c9 2639 pSMB->Fid = smb_file_id;
08547b03
SF
2640 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_LOCK);
2641 pSMB->Reserved4 = 0;
be8e3b00 2642 inc_rfc1001_len(pSMB, byte_count);
08547b03 2643 pSMB->ByteCount = cpu_to_le16(byte_count);
7ee1af76
JA
2644 if (waitFlag) {
2645 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
2646 (struct smb_hdr *) pSMBr, &bytes_returned);
2647 } else {
133672ef 2648 iov[0].iov_base = (char *)pSMB;
be8e3b00 2649 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
133672ef 2650 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
da502f7d
PS
2651 &resp_buf_type, timeout, &rsp_iov);
2652 pSMBr = (struct smb_com_transaction2_sfi_rsp *)rsp_iov.iov_base;
7ee1af76 2653 }
da502f7d 2654 cifs_small_buf_release(pSMB);
7ee1af76 2655
08547b03 2656 if (rc) {
f96637be 2657 cifs_dbg(FYI, "Send error in Posix Lock = %d\n", rc);
c5fd363d 2658 } else if (pLockData) {
fc94cdb9
SF
2659 /* lock structure can be returned on get */
2660 __u16 data_offset;
2661 __u16 data_count;
2662 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
2663
820a803f 2664 if (rc || get_bcc(&pSMBr->hdr) < sizeof(*parm_data)) {
fc94cdb9
SF
2665 rc = -EIO; /* bad smb */
2666 goto plk_err_exit;
2667 }
fc94cdb9
SF
2668 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
2669 data_count = le16_to_cpu(pSMBr->t2.DataCount);
790fe579 2670 if (data_count < sizeof(struct cifs_posix_lock)) {
fc94cdb9
SF
2671 rc = -EIO;
2672 goto plk_err_exit;
2673 }
2674 parm_data = (struct cifs_posix_lock *)
2675 ((char *)&pSMBr->hdr.Protocol + data_offset);
bc09d141 2676 if (parm_data->lock_type == cpu_to_le16(CIFS_UNLCK))
fc94cdb9 2677 pLockData->fl_type = F_UNLCK;
f05337c6
PS
2678 else {
2679 if (parm_data->lock_type ==
bc09d141 2680 cpu_to_le16(CIFS_RDLCK))
f05337c6
PS
2681 pLockData->fl_type = F_RDLCK;
2682 else if (parm_data->lock_type ==
bc09d141 2683 cpu_to_le16(CIFS_WRLCK))
f05337c6
PS
2684 pLockData->fl_type = F_WRLCK;
2685
5443d130
SF
2686 pLockData->fl_start = le64_to_cpu(parm_data->start);
2687 pLockData->fl_end = pLockData->fl_start +
2688 le64_to_cpu(parm_data->length) - 1;
9d5b86ac 2689 pLockData->fl_pid = -le32_to_cpu(parm_data->pid);
f05337c6 2690 }
08547b03 2691 }
50c2f753 2692
fc94cdb9 2693plk_err_exit:
da502f7d 2694 free_rsp_buf(resp_buf_type, rsp_iov.iov_base);
133672ef 2695
08547b03
SF
2696 /* Note: On -EAGAIN error only caller can retry on handle based calls
2697 since file handle passed in no longer valid */
2698
2699 return rc;
2700}
2701
2702
1da177e4 2703int
6d5786a3 2704CIFSSMBClose(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
1da177e4
LT
2705{
2706 int rc = 0;
2707 CLOSE_REQ *pSMB = NULL;
f96637be 2708 cifs_dbg(FYI, "In CIFSSMBClose\n");
1da177e4
LT
2709
2710/* do not retry on dead session on close */
2711 rc = small_smb_init(SMB_COM_CLOSE, 3, tcon, (void **) &pSMB);
790fe579 2712 if (rc == -EAGAIN)
1da177e4
LT
2713 return 0;
2714 if (rc)
2715 return rc;
2716
1da177e4 2717 pSMB->FileID = (__u16) smb_file_id;
b815f1e5 2718 pSMB->LastWriteTime = 0xFFFFFFFF;
1da177e4 2719 pSMB->ByteCount = 0;
792af7b0 2720 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
da502f7d 2721 cifs_small_buf_release(pSMB);
44c58186 2722 cifs_stats_inc(&tcon->stats.cifs_stats.num_closes);
1da177e4 2723 if (rc) {
790fe579 2724 if (rc != -EINTR) {
1da177e4 2725 /* EINTR is expected when user ctl-c to kill app */
f96637be 2726 cifs_dbg(VFS, "Send error in Close = %d\n", rc);
1da177e4
LT
2727 }
2728 }
2729
1da177e4 2730 /* Since session is dead, file will be closed on server already */
790fe579 2731 if (rc == -EAGAIN)
1da177e4
LT
2732 rc = 0;
2733
2734 return rc;
2735}
2736
b298f223 2737int
6d5786a3 2738CIFSSMBFlush(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
b298f223
SF
2739{
2740 int rc = 0;
2741 FLUSH_REQ *pSMB = NULL;
f96637be 2742 cifs_dbg(FYI, "In CIFSSMBFlush\n");
b298f223
SF
2743
2744 rc = small_smb_init(SMB_COM_FLUSH, 1, tcon, (void **) &pSMB);
2745 if (rc)
2746 return rc;
2747
2748 pSMB->FileID = (__u16) smb_file_id;
2749 pSMB->ByteCount = 0;
792af7b0 2750 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
da502f7d 2751 cifs_small_buf_release(pSMB);
44c58186 2752 cifs_stats_inc(&tcon->stats.cifs_stats.num_flushes);
b298f223 2753 if (rc)
f96637be 2754 cifs_dbg(VFS, "Send error in Flush = %d\n", rc);
b298f223
SF
2755
2756 return rc;
2757}
2758
1da177e4 2759int
6d5786a3 2760CIFSSMBRename(const unsigned int xid, struct cifs_tcon *tcon,
8ceb9843
PS
2761 const char *from_name, const char *to_name,
2762 struct cifs_sb_info *cifs_sb)
1da177e4
LT
2763{
2764 int rc = 0;
2765 RENAME_REQ *pSMB = NULL;
2766 RENAME_RSP *pSMBr = NULL;
2767 int bytes_returned;
2768 int name_len, name_len2;
2769 __u16 count;
2baa2682 2770 int remap = cifs_remap(cifs_sb);
1da177e4 2771
f96637be 2772 cifs_dbg(FYI, "In CIFSSMBRename\n");
1da177e4
LT
2773renameRetry:
2774 rc = smb_init(SMB_COM_RENAME, 1, tcon, (void **) &pSMB,
2775 (void **) &pSMBr);
2776 if (rc)
2777 return rc;
2778
2779 pSMB->BufferFormat = 0x04;
2780 pSMB->SearchAttributes =
2781 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
2782 ATTR_DIRECTORY);
2783
2784 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
8ceb9843
PS
2785 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2786 from_name, PATH_MAX,
2787 cifs_sb->local_nls, remap);
1da177e4
LT
2788 name_len++; /* trailing null */
2789 name_len *= 2;
2790 pSMB->OldFileName[name_len] = 0x04; /* pad */
2791 /* protocol requires ASCII signature byte on Unicode string */
2792 pSMB->OldFileName[name_len + 1] = 0x00;
2793 name_len2 =
acbbb76a 2794 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
8ceb9843
PS
2795 to_name, PATH_MAX, cifs_sb->local_nls,
2796 remap);
1da177e4
LT
2797 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2798 name_len2 *= 2; /* convert to bytes */
50c2f753 2799 } else { /* BB improve the check for buffer overruns BB */
8ceb9843 2800 name_len = strnlen(from_name, PATH_MAX);
1da177e4 2801 name_len++; /* trailing null */
8ceb9843
PS
2802 strncpy(pSMB->OldFileName, from_name, name_len);
2803 name_len2 = strnlen(to_name, PATH_MAX);
1da177e4
LT
2804 name_len2++; /* trailing null */
2805 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
8ceb9843 2806 strncpy(&pSMB->OldFileName[name_len + 1], to_name, name_len2);
1da177e4
LT
2807 name_len2++; /* trailing null */
2808 name_len2++; /* signature byte */
2809 }
2810
2811 count = 1 /* 1st signature byte */ + name_len + name_len2;
be8e3b00 2812 inc_rfc1001_len(pSMB, count);
1da177e4
LT
2813 pSMB->ByteCount = cpu_to_le16(count);
2814
2815 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2816 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
44c58186 2817 cifs_stats_inc(&tcon->stats.cifs_stats.num_renames);
ad7a2926 2818 if (rc)
f96637be 2819 cifs_dbg(FYI, "Send error in rename = %d\n", rc);
1da177e4 2820
1da177e4
LT
2821 cifs_buf_release(pSMB);
2822
2823 if (rc == -EAGAIN)
2824 goto renameRetry;
2825
2826 return rc;
2827}
2828
6d5786a3 2829int CIFSSMBRenameOpenFile(const unsigned int xid, struct cifs_tcon *pTcon,
391e5755 2830 int netfid, const char *target_name,
50c2f753 2831 const struct nls_table *nls_codepage, int remap)
1da177e4
LT
2832{
2833 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2834 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
50c2f753 2835 struct set_file_rename *rename_info;
1da177e4
LT
2836 char *data_offset;
2837 char dummy_string[30];
2838 int rc = 0;
2839 int bytes_returned = 0;
2840 int len_of_str;
2841 __u16 params, param_offset, offset, count, byte_count;
2842
f96637be 2843 cifs_dbg(FYI, "Rename to File by handle\n");
1da177e4
LT
2844 rc = smb_init(SMB_COM_TRANSACTION2, 15, pTcon, (void **) &pSMB,
2845 (void **) &pSMBr);
2846 if (rc)
2847 return rc;
2848
2849 params = 6;
2850 pSMB->MaxSetupCount = 0;
2851 pSMB->Reserved = 0;
2852 pSMB->Flags = 0;
2853 pSMB->Timeout = 0;
2854 pSMB->Reserved2 = 0;
2855 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2856 offset = param_offset + params;
2857
2858 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2859 rename_info = (struct set_file_rename *) data_offset;
2860 pSMB->MaxParameterCount = cpu_to_le16(2);
ad7a2926 2861 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
1da177e4
LT
2862 pSMB->SetupCount = 1;
2863 pSMB->Reserved3 = 0;
2864 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2865 byte_count = 3 /* pad */ + params;
2866 pSMB->ParameterCount = cpu_to_le16(params);
2867 pSMB->TotalParameterCount = pSMB->ParameterCount;
2868 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2869 pSMB->DataOffset = cpu_to_le16(offset);
2870 /* construct random name ".cifs_tmp<inodenum><mid>" */
2871 rename_info->overwrite = cpu_to_le32(1);
2872 rename_info->root_fid = 0;
2873 /* unicode only call */
790fe579 2874 if (target_name == NULL) {
50c2f753 2875 sprintf(dummy_string, "cifs%x", pSMB->hdr.Mid);
acbbb76a
SF
2876 len_of_str =
2877 cifsConvertToUTF16((__le16 *)rename_info->target_name,
737b758c 2878 dummy_string, 24, nls_codepage, remap);
1da177e4 2879 } else {
acbbb76a
SF
2880 len_of_str =
2881 cifsConvertToUTF16((__le16 *)rename_info->target_name,
50c2f753
SF
2882 target_name, PATH_MAX, nls_codepage,
2883 remap);
1da177e4
LT
2884 }
2885 rename_info->target_name_len = cpu_to_le32(2 * len_of_str);
391e5755 2886 count = 12 /* sizeof(struct set_file_rename) */ + (2 * len_of_str);
1da177e4
LT
2887 byte_count += count;
2888 pSMB->DataCount = cpu_to_le16(count);
2889 pSMB->TotalDataCount = pSMB->DataCount;
2890 pSMB->Fid = netfid;
2891 pSMB->InformationLevel =
2892 cpu_to_le16(SMB_SET_FILE_RENAME_INFORMATION);
2893 pSMB->Reserved4 = 0;
be8e3b00 2894 inc_rfc1001_len(pSMB, byte_count);
1da177e4
LT
2895 pSMB->ByteCount = cpu_to_le16(byte_count);
2896 rc = SendReceive(xid, pTcon->ses, (struct smb_hdr *) pSMB,
50c2f753 2897 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
44c58186 2898 cifs_stats_inc(&pTcon->stats.cifs_stats.num_t2renames);
ad7a2926 2899 if (rc)
f96637be
JP
2900 cifs_dbg(FYI, "Send error in Rename (by file handle) = %d\n",
2901 rc);
a5a2b489 2902
1da177e4
LT
2903 cifs_buf_release(pSMB);
2904
2905 /* Note: On -EAGAIN error only caller can retry on handle based calls
2906 since file handle passed in no longer valid */
2907
2908 return rc;
2909}
2910
2911int
6d5786a3
PS
2912CIFSSMBCopy(const unsigned int xid, struct cifs_tcon *tcon,
2913 const char *fromName, const __u16 target_tid, const char *toName,
2914 const int flags, const struct nls_table *nls_codepage, int remap)
1da177e4
LT
2915{
2916 int rc = 0;
2917 COPY_REQ *pSMB = NULL;
2918 COPY_RSP *pSMBr = NULL;
2919 int bytes_returned;
2920 int name_len, name_len2;
2921 __u16 count;
2922
f96637be 2923 cifs_dbg(FYI, "In CIFSSMBCopy\n");
1da177e4
LT
2924copyRetry:
2925 rc = smb_init(SMB_COM_COPY, 1, tcon, (void **) &pSMB,
2926 (void **) &pSMBr);
2927 if (rc)
2928 return rc;
2929
2930 pSMB->BufferFormat = 0x04;
2931 pSMB->Tid2 = target_tid;
2932
2933 pSMB->Flags = cpu_to_le16(flags & COPY_TREE);
2934
2935 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
acbbb76a
SF
2936 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2937 fromName, PATH_MAX, nls_codepage,
2938 remap);
1da177e4
LT
2939 name_len++; /* trailing null */
2940 name_len *= 2;
2941 pSMB->OldFileName[name_len] = 0x04; /* pad */
2942 /* protocol requires ASCII signature byte on Unicode string */
2943 pSMB->OldFileName[name_len + 1] = 0x00;
50c2f753 2944 name_len2 =
acbbb76a
SF
2945 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2946 toName, PATH_MAX, nls_codepage, remap);
1da177e4
LT
2947 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2948 name_len2 *= 2; /* convert to bytes */
50c2f753 2949 } else { /* BB improve the check for buffer overruns BB */
1da177e4
LT
2950 name_len = strnlen(fromName, PATH_MAX);
2951 name_len++; /* trailing null */
2952 strncpy(pSMB->OldFileName, fromName, name_len);
2953 name_len2 = strnlen(toName, PATH_MAX);
2954 name_len2++; /* trailing null */
2955 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2956 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
2957 name_len2++; /* trailing null */
2958 name_len2++; /* signature byte */
2959 }
2960
2961 count = 1 /* 1st signature byte */ + name_len + name_len2;
be8e3b00 2962 inc_rfc1001_len(pSMB, count);
1da177e4
LT
2963 pSMB->ByteCount = cpu_to_le16(count);
2964
2965 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2966 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2967 if (rc) {
f96637be
JP
2968 cifs_dbg(FYI, "Send error in copy = %d with %d files copied\n",
2969 rc, le16_to_cpu(pSMBr->CopyCount));
1da177e4 2970 }
0d817bc0 2971 cifs_buf_release(pSMB);
1da177e4
LT
2972
2973 if (rc == -EAGAIN)
2974 goto copyRetry;
2975
2976 return rc;
2977}
2978
2979int
6d5786a3 2980CIFSUnixCreateSymLink(const unsigned int xid, struct cifs_tcon *tcon,
1da177e4 2981 const char *fromName, const char *toName,
bc8ebdc4 2982 const struct nls_table *nls_codepage, int remap)
1da177e4
LT
2983{
2984 TRANSACTION2_SPI_REQ *pSMB = NULL;
2985 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2986 char *data_offset;
2987 int name_len;
2988 int name_len_target;
2989 int rc = 0;
2990 int bytes_returned = 0;
2991 __u16 params, param_offset, offset, byte_count;
2992
f96637be 2993 cifs_dbg(FYI, "In Symlink Unix style\n");
1da177e4
LT
2994createSymLinkRetry:
2995 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2996 (void **) &pSMBr);
2997 if (rc)
2998 return rc;
2999
3000 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3001 name_len =
bc8ebdc4
NA
3002 cifsConvertToUTF16((__le16 *) pSMB->FileName, fromName,
3003 /* find define for this maxpathcomponent */
3004 PATH_MAX, nls_codepage, remap);
1da177e4
LT
3005 name_len++; /* trailing null */
3006 name_len *= 2;
3007
50c2f753 3008 } else { /* BB improve the check for buffer overruns BB */
1da177e4
LT
3009 name_len = strnlen(fromName, PATH_MAX);
3010 name_len++; /* trailing null */
3011 strncpy(pSMB->FileName, fromName, name_len);
3012 }
3013 params = 6 + name_len;
3014 pSMB->MaxSetupCount = 0;
3015 pSMB->Reserved = 0;
3016 pSMB->Flags = 0;
3017 pSMB->Timeout = 0;
3018 pSMB->Reserved2 = 0;
3019 param_offset = offsetof(struct smb_com_transaction2_spi_req,
50c2f753 3020 InformationLevel) - 4;
1da177e4
LT
3021 offset = param_offset + params;
3022
3023 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
3024 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3025 name_len_target =
bc8ebdc4
NA
3026 cifsConvertToUTF16((__le16 *) data_offset, toName,
3027 /* find define for this maxpathcomponent */
3028 PATH_MAX, nls_codepage, remap);
1da177e4
LT
3029 name_len_target++; /* trailing null */
3030 name_len_target *= 2;
50c2f753 3031 } else { /* BB improve the check for buffer overruns BB */
1da177e4
LT
3032 name_len_target = strnlen(toName, PATH_MAX);
3033 name_len_target++; /* trailing null */
3034 strncpy(data_offset, toName, name_len_target);
3035 }
3036
3037 pSMB->MaxParameterCount = cpu_to_le16(2);
3038 /* BB find exact max on data count below from sess */
3039 pSMB->MaxDataCount = cpu_to_le16(1000);
3040 pSMB->SetupCount = 1;
3041 pSMB->Reserved3 = 0;
3042 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
3043 byte_count = 3 /* pad */ + params + name_len_target;
3044 pSMB->DataCount = cpu_to_le16(name_len_target);
3045 pSMB->ParameterCount = cpu_to_le16(params);
3046 pSMB->TotalDataCount = pSMB->DataCount;
3047 pSMB->TotalParameterCount = pSMB->ParameterCount;
3048 pSMB->ParameterOffset = cpu_to_le16(param_offset);
3049 pSMB->DataOffset = cpu_to_le16(offset);
3050 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_LINK);
3051 pSMB->Reserved4 = 0;
be8e3b00 3052 inc_rfc1001_len(pSMB, byte_count);
1da177e4
LT
3053 pSMB->ByteCount = cpu_to_le16(byte_count);
3054 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3055 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
44c58186 3056 cifs_stats_inc(&tcon->stats.cifs_stats.num_symlinks);
ad7a2926 3057 if (rc)
f96637be
JP
3058 cifs_dbg(FYI, "Send error in SetPathInfo create symlink = %d\n",
3059 rc);
1da177e4 3060
0d817bc0 3061 cifs_buf_release(pSMB);
1da177e4
LT
3062
3063 if (rc == -EAGAIN)
3064 goto createSymLinkRetry;
3065
3066 return rc;
3067}
3068
3069int
6d5786a3 3070CIFSUnixCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon,
1da177e4 3071 const char *fromName, const char *toName,
737b758c 3072 const struct nls_table *nls_codepage, int remap)
1da177e4
LT
3073{
3074 TRANSACTION2_SPI_REQ *pSMB = NULL;
3075 TRANSACTION2_SPI_RSP *pSMBr = NULL;
3076 char *data_offset;
3077 int name_len;
3078 int name_len_target;
3079 int rc = 0;
3080 int bytes_returned = 0;
3081 __u16 params, param_offset, offset, byte_count;
3082
f96637be 3083 cifs_dbg(FYI, "In Create Hard link Unix style\n");
1da177e4
LT
3084createHardLinkRetry:
3085 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3086 (void **) &pSMBr);
3087 if (rc)
3088 return rc;
3089
3090 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
acbbb76a
SF
3091 name_len = cifsConvertToUTF16((__le16 *) pSMB->FileName, toName,
3092 PATH_MAX, nls_codepage, remap);
1da177e4
LT
3093 name_len++; /* trailing null */
3094 name_len *= 2;
3095
50c2f753 3096 } else { /* BB improve the check for buffer overruns BB */
1da177e4
LT
3097 name_len = strnlen(toName, PATH_MAX);
3098 name_len++; /* trailing null */
3099 strncpy(pSMB->FileName, toName, name_len);
3100 }
3101 params = 6 + name_len;
3102 pSMB->MaxSetupCount = 0;
3103 pSMB->Reserved = 0;
3104 pSMB->Flags = 0;
3105 pSMB->Timeout = 0;
3106 pSMB->Reserved2 = 0;
3107 param_offset = offsetof(struct smb_com_transaction2_spi_req,
50c2f753 3108 InformationLevel) - 4;
1da177e4
LT
3109 offset = param_offset + params;
3110
3111 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
3112 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3113 name_len_target =
acbbb76a
SF
3114 cifsConvertToUTF16((__le16 *) data_offset, fromName,
3115 PATH_MAX, nls_codepage, remap);
1da177e4
LT
3116 name_len_target++; /* trailing null */
3117 name_len_target *= 2;
50c2f753 3118 } else { /* BB improve the check for buffer overruns BB */
1da177e4
LT
3119 name_len_target = strnlen(fromName, PATH_MAX);
3120 name_len_target++; /* trailing null */
3121 strncpy(data_offset, fromName, name_len_target);
3122 }
3123
3124 pSMB->MaxParameterCount = cpu_to_le16(2);
3125 /* BB find exact max on data count below from sess*/
3126 pSMB->MaxDataCount = cpu_to_le16(1000);
3127 pSMB->SetupCount = 1;
3128 pSMB->Reserved3 = 0;
3129 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
3130 byte_count = 3 /* pad */ + params + name_len_target;
3131 pSMB->ParameterCount = cpu_to_le16(params);
3132 pSMB->TotalParameterCount = pSMB->ParameterCount;
3133 pSMB->DataCount = cpu_to_le16(name_len_target);
3134 pSMB->TotalDataCount = pSMB->DataCount;
3135 pSMB->ParameterOffset = cpu_to_le16(param_offset);
3136 pSMB->DataOffset = cpu_to_le16(offset);
3137 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_HLINK);
3138 pSMB->Reserved4 = 0;
be8e3b00 3139 inc_rfc1001_len(pSMB, byte_count);
1da177e4
LT
3140 pSMB->ByteCount = cpu_to_le16(byte_count);
3141 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3142 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
44c58186 3143 cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
ad7a2926 3144 if (rc)
f96637be
JP
3145 cifs_dbg(FYI, "Send error in SetPathInfo (hard link) = %d\n",
3146 rc);
1da177e4
LT
3147
3148 cifs_buf_release(pSMB);
3149 if (rc == -EAGAIN)
3150 goto createHardLinkRetry;
3151
3152 return rc;
3153}
3154
3155int
6d5786a3 3156CIFSCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon,
d6e906f1
SF
3157 const char *from_name, const char *to_name,
3158 struct cifs_sb_info *cifs_sb)
1da177e4
LT
3159{
3160 int rc = 0;
3161 NT_RENAME_REQ *pSMB = NULL;
3162 RENAME_RSP *pSMBr = NULL;
3163 int bytes_returned;
3164 int name_len, name_len2;
3165 __u16 count;
2baa2682 3166 int remap = cifs_remap(cifs_sb);
1da177e4 3167
f96637be 3168 cifs_dbg(FYI, "In CIFSCreateHardLink\n");
1da177e4
LT
3169winCreateHardLinkRetry:
3170
3171 rc = smb_init(SMB_COM_NT_RENAME, 4, tcon, (void **) &pSMB,
3172 (void **) &pSMBr);
3173 if (rc)
3174 return rc;
3175
3176 pSMB->SearchAttributes =
3177 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
3178 ATTR_DIRECTORY);
3179 pSMB->Flags = cpu_to_le16(CREATE_HARD_LINK);
3180 pSMB->ClusterCount = 0;
3181
3182 pSMB->BufferFormat = 0x04;
3183
3184 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3185 name_len =
d6e906f1
SF
3186 cifsConvertToUTF16((__le16 *) pSMB->OldFileName, from_name,
3187 PATH_MAX, cifs_sb->local_nls, remap);
1da177e4
LT
3188 name_len++; /* trailing null */
3189 name_len *= 2;
fcc7c09d
JL
3190
3191 /* protocol specifies ASCII buffer format (0x04) for unicode */
3192 pSMB->OldFileName[name_len] = 0x04;
3193 pSMB->OldFileName[name_len + 1] = 0x00; /* pad */
1da177e4 3194 name_len2 =
acbbb76a 3195 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
d6e906f1
SF
3196 to_name, PATH_MAX, cifs_sb->local_nls,
3197 remap);
1da177e4
LT
3198 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
3199 name_len2 *= 2; /* convert to bytes */
50c2f753 3200 } else { /* BB improve the check for buffer overruns BB */
d6e906f1 3201 name_len = strnlen(from_name, PATH_MAX);
1da177e4 3202 name_len++; /* trailing null */
d6e906f1
SF
3203 strncpy(pSMB->OldFileName, from_name, name_len);
3204 name_len2 = strnlen(to_name, PATH_MAX);
1da177e4
LT
3205 name_len2++; /* trailing null */
3206 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
d6e906f1 3207 strncpy(&pSMB->OldFileName[name_len + 1], to_name, name_len2);
1da177e4
LT
3208 name_len2++; /* trailing null */
3209 name_len2++; /* signature byte */
3210 }
3211
3212 count = 1 /* string type byte */ + name_len + name_len2;
be8e3b00 3213 inc_rfc1001_len(pSMB, count);
1da177e4
LT
3214 pSMB->ByteCount = cpu_to_le16(count);
3215
3216 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3217 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
44c58186 3218 cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
ad7a2926 3219 if (rc)
f96637be 3220 cifs_dbg(FYI, "Send error in hard link (NT rename) = %d\n", rc);
ad7a2926 3221
1da177e4
LT
3222 cifs_buf_release(pSMB);
3223 if (rc == -EAGAIN)
3224 goto winCreateHardLinkRetry;
3225
3226 return rc;
3227}
3228
3229int
6d5786a3 3230CIFSSMBUnixQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon,
460b9696 3231 const unsigned char *searchName, char **symlinkinfo,
bc8ebdc4 3232 const struct nls_table *nls_codepage, int remap)
1da177e4
LT
3233{
3234/* SMB_QUERY_FILE_UNIX_LINK */
3235 TRANSACTION2_QPI_REQ *pSMB = NULL;
3236 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3237 int rc = 0;
3238 int bytes_returned;
3239 int name_len;
3240 __u16 params, byte_count;
460b9696 3241 char *data_start;
1da177e4 3242
f96637be 3243 cifs_dbg(FYI, "In QPathSymLinkInfo (Unix) for path %s\n", searchName);
1da177e4
LT
3244
3245querySymLinkRetry:
3246 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3247 (void **) &pSMBr);
3248 if (rc)
3249 return rc;
3250
3251 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3252 name_len =
bc8ebdc4
NA
3253 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3254 searchName, PATH_MAX, nls_codepage,
3255 remap);
1da177e4
LT
3256 name_len++; /* trailing null */
3257 name_len *= 2;
50c2f753 3258 } else { /* BB improve the check for buffer overruns BB */
1da177e4
LT
3259 name_len = strnlen(searchName, PATH_MAX);
3260 name_len++; /* trailing null */
3261 strncpy(pSMB->FileName, searchName, name_len);
3262 }
3263
3264 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3265 pSMB->TotalDataCount = 0;
3266 pSMB->MaxParameterCount = cpu_to_le16(2);
46a7574c 3267 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
1da177e4
LT
3268 pSMB->MaxSetupCount = 0;
3269 pSMB->Reserved = 0;
3270 pSMB->Flags = 0;
3271 pSMB->Timeout = 0;
3272 pSMB->Reserved2 = 0;
3273 pSMB->ParameterOffset = cpu_to_le16(offsetof(
50c2f753 3274 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
1da177e4
LT
3275 pSMB->DataCount = 0;
3276 pSMB->DataOffset = 0;
3277 pSMB->SetupCount = 1;
3278 pSMB->Reserved3 = 0;
3279 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3280 byte_count = params + 1 /* pad */ ;
3281 pSMB->TotalParameterCount = cpu_to_le16(params);
3282 pSMB->ParameterCount = pSMB->TotalParameterCount;
3283 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_LINK);
3284 pSMB->Reserved4 = 0;
be8e3b00 3285 inc_rfc1001_len(pSMB, byte_count);
1da177e4
LT
3286 pSMB->ByteCount = cpu_to_le16(byte_count);
3287
3288 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3289 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3290 if (rc) {
f96637be 3291 cifs_dbg(FYI, "Send error in QuerySymLinkInfo = %d\n", rc);
1da177e4
LT
3292 } else {
3293 /* decode response */
3294
3295 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
1da177e4 3296 /* BB also check enough total bytes returned */
820a803f 3297 if (rc || get_bcc(&pSMBr->hdr) < 2)
460b9696 3298 rc = -EIO;
1da177e4 3299 else {
0e0d2cf3 3300 bool is_unicode;
460b9696
JL
3301 u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3302
3303 data_start = ((char *) &pSMBr->hdr.Protocol) +
3304 le16_to_cpu(pSMBr->t2.DataOffset);
1da177e4 3305
0e0d2cf3
SF
3306 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3307 is_unicode = true;
3308 else
3309 is_unicode = false;
3310
737b758c 3311 /* BB FIXME investigate remapping reserved chars here */
acbbb76a
SF
3312 *symlinkinfo = cifs_strndup_from_utf16(data_start,
3313 count, is_unicode, nls_codepage);
8b6427a2 3314 if (!*symlinkinfo)
460b9696 3315 rc = -ENOMEM;
1da177e4
LT
3316 }
3317 }
3318 cifs_buf_release(pSMB);
3319 if (rc == -EAGAIN)
3320 goto querySymLinkRetry;
3321 return rc;
3322}
3323
c52a9554
SF
3324/*
3325 * Recent Windows versions now create symlinks more frequently
3326 * and they use the "reparse point" mechanism below. We can of course
3327 * do symlinks nicely to Samba and other servers which support the
3328 * CIFS Unix Extensions and we can also do SFU symlinks and "client only"
3329 * "MF" symlinks optionally, but for recent Windows we really need to
3330 * reenable the code below and fix the cifs_symlink callers to handle this.
3331 * In the interim this code has been moved to its own config option so
3332 * it is not compiled in by default until callers fixed up and more tested.
3333 */
1da177e4 3334int
d244bf2d
PS
3335CIFSSMBQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon,
3336 __u16 fid, char **symlinkinfo,
3337 const struct nls_table *nls_codepage)
1da177e4
LT
3338{
3339 int rc = 0;
3340 int bytes_returned;
50c2f753
SF
3341 struct smb_com_transaction_ioctl_req *pSMB;
3342 struct smb_com_transaction_ioctl_rsp *pSMBr;
d244bf2d
PS
3343 bool is_unicode;
3344 unsigned int sub_len;
3345 char *sub_start;
c31f3307
SF
3346 struct reparse_symlink_data *reparse_buf;
3347 struct reparse_posix_data *posix_buf;
d244bf2d
PS
3348 __u32 data_offset, data_count;
3349 char *end_of_smb;
3350
3351 cifs_dbg(FYI, "In Windows reparse style QueryLink for fid %u\n", fid);
1da177e4
LT
3352 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3353 (void **) &pSMBr);
3354 if (rc)
3355 return rc;
3356
3357 pSMB->TotalParameterCount = 0 ;
3358 pSMB->TotalDataCount = 0;
3359 pSMB->MaxParameterCount = cpu_to_le32(2);
3360 /* BB find exact data count max from sess structure BB */
c974befa 3361 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
1da177e4
LT
3362 pSMB->MaxSetupCount = 4;
3363 pSMB->Reserved = 0;
3364 pSMB->ParameterOffset = 0;
3365 pSMB->DataCount = 0;
3366 pSMB->DataOffset = 0;
3367 pSMB->SetupCount = 4;
3368 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
3369 pSMB->ParameterCount = pSMB->TotalParameterCount;
3370 pSMB->FunctionCode = cpu_to_le32(FSCTL_GET_REPARSE_POINT);
3371 pSMB->IsFsctl = 1; /* FSCTL */
3372 pSMB->IsRootFlag = 0;
3373 pSMB->Fid = fid; /* file handle always le */
3374 pSMB->ByteCount = 0;
3375
3376 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3377 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3378 if (rc) {
f96637be 3379 cifs_dbg(FYI, "Send error in QueryReparseLinkInfo = %d\n", rc);
d244bf2d
PS
3380 goto qreparse_out;
3381 }
3382
3383 data_offset = le32_to_cpu(pSMBr->DataOffset);
3384 data_count = le32_to_cpu(pSMBr->DataCount);
3385 if (get_bcc(&pSMBr->hdr) < 2 || data_offset > 512) {
3386 /* BB also check enough total bytes returned */
3387 rc = -EIO; /* bad smb */
3388 goto qreparse_out;
3389 }
3390 if (!data_count || (data_count > 2048)) {
3391 rc = -EIO;
3392 cifs_dbg(FYI, "Invalid return data count on get reparse info ioctl\n");
3393 goto qreparse_out;
3394 }
3395 end_of_smb = 2 + get_bcc(&pSMBr->hdr) + (char *)&pSMBr->ByteCount;
c31f3307 3396 reparse_buf = (struct reparse_symlink_data *)
d244bf2d
PS
3397 ((char *)&pSMBr->hdr.Protocol + data_offset);
3398 if ((char *)reparse_buf >= end_of_smb) {
3399 rc = -EIO;
3400 goto qreparse_out;
1da177e4 3401 }
c31f3307
SF
3402 if (reparse_buf->ReparseTag == cpu_to_le32(IO_REPARSE_TAG_NFS)) {
3403 cifs_dbg(FYI, "NFS style reparse tag\n");
3404 posix_buf = (struct reparse_posix_data *)reparse_buf;
3405
3406 if (posix_buf->InodeType != cpu_to_le64(NFS_SPECFILE_LNK)) {
3407 cifs_dbg(FYI, "unsupported file type 0x%llx\n",
3408 le64_to_cpu(posix_buf->InodeType));
3409 rc = -EOPNOTSUPP;
3410 goto qreparse_out;
3411 }
3412 is_unicode = true;
3413 sub_len = le16_to_cpu(reparse_buf->ReparseDataLength);
3414 if (posix_buf->PathBuffer + sub_len > end_of_smb) {
3415 cifs_dbg(FYI, "reparse buf beyond SMB\n");
3416 rc = -EIO;
3417 goto qreparse_out;
3418 }
3419 *symlinkinfo = cifs_strndup_from_utf16(posix_buf->PathBuffer,
3420 sub_len, is_unicode, nls_codepage);
3421 goto qreparse_out;
3422 } else if (reparse_buf->ReparseTag !=
3423 cpu_to_le32(IO_REPARSE_TAG_SYMLINK)) {
3424 rc = -EOPNOTSUPP;
3425 goto qreparse_out;
3426 }
3427
3428 /* Reparse tag is NTFS symlink */
3429 sub_start = le16_to_cpu(reparse_buf->SubstituteNameOffset) +
3430 reparse_buf->PathBuffer;
3431 sub_len = le16_to_cpu(reparse_buf->SubstituteNameLength);
3432 if (sub_start + sub_len > end_of_smb) {
d244bf2d
PS
3433 cifs_dbg(FYI, "reparse buf beyond SMB\n");
3434 rc = -EIO;
3435 goto qreparse_out;
3436 }
d244bf2d
PS
3437 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3438 is_unicode = true;
3439 else
3440 is_unicode = false;
989c7e51 3441
d244bf2d
PS
3442 /* BB FIXME investigate remapping reserved chars here */
3443 *symlinkinfo = cifs_strndup_from_utf16(sub_start, sub_len, is_unicode,
3444 nls_codepage);
3445 if (!*symlinkinfo)
3446 rc = -ENOMEM;
1da177e4 3447qreparse_out:
4a6d87f1 3448 cifs_buf_release(pSMB);
1da177e4 3449
d244bf2d
PS
3450 /*
3451 * Note: On -EAGAIN error only caller can retry on handle based calls
3452 * since file handle passed in no longer valid.
3453 */
1da177e4
LT
3454 return rc;
3455}
3456
c7f508a9
SF
3457int
3458CIFSSMB_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
3459 __u16 fid)
3460{
3461 int rc = 0;
3462 int bytes_returned;
3463 struct smb_com_transaction_compr_ioctl_req *pSMB;
3464 struct smb_com_transaction_ioctl_rsp *pSMBr;
3465
3466 cifs_dbg(FYI, "Set compression for %u\n", fid);
3467 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3468 (void **) &pSMBr);
3469 if (rc)
3470 return rc;
3471
3472 pSMB->compression_state = cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
3473
3474 pSMB->TotalParameterCount = 0;
bc09d141 3475 pSMB->TotalDataCount = cpu_to_le32(2);
c7f508a9
SF
3476 pSMB->MaxParameterCount = 0;
3477 pSMB->MaxDataCount = 0;
3478 pSMB->MaxSetupCount = 4;
3479 pSMB->Reserved = 0;
3480 pSMB->ParameterOffset = 0;
bc09d141 3481 pSMB->DataCount = cpu_to_le32(2);
c7f508a9
SF
3482 pSMB->DataOffset =
3483 cpu_to_le32(offsetof(struct smb_com_transaction_compr_ioctl_req,
3484 compression_state) - 4); /* 84 */
3485 pSMB->SetupCount = 4;
bc09d141 3486 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
c7f508a9 3487 pSMB->ParameterCount = 0;
bc09d141 3488 pSMB->FunctionCode = cpu_to_le32(FSCTL_SET_COMPRESSION);
c7f508a9
SF
3489 pSMB->IsFsctl = 1; /* FSCTL */
3490 pSMB->IsRootFlag = 0;
3491 pSMB->Fid = fid; /* file handle always le */
3492 /* 3 byte pad, followed by 2 byte compress state */
bc09d141 3493 pSMB->ByteCount = cpu_to_le16(5);
c7f508a9
SF
3494 inc_rfc1001_len(pSMB, 5);
3495
3496 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3497 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3498 if (rc)
3499 cifs_dbg(FYI, "Send error in SetCompression = %d\n", rc);
3500
3501 cifs_buf_release(pSMB);
3502
3503 /*
3504 * Note: On -EAGAIN error only caller can retry on handle based calls
3505 * since file handle passed in no longer valid.
3506 */
3507 return rc;
3508}
3509
3510
1da177e4
LT
3511#ifdef CONFIG_CIFS_POSIX
3512
3513/*Convert an Access Control Entry from wire format to local POSIX xattr format*/
2211d5ba 3514static void cifs_convert_ace(struct posix_acl_xattr_entry *ace,
50c2f753 3515 struct cifs_posix_ace *cifs_ace)
1da177e4
LT
3516{
3517 /* u8 cifs fields do not need le conversion */
ff7feac9
SF
3518 ace->e_perm = cpu_to_le16(cifs_ace->cifs_e_perm);
3519 ace->e_tag = cpu_to_le16(cifs_ace->cifs_e_tag);
3520 ace->e_id = cpu_to_le32(le64_to_cpu(cifs_ace->cifs_uid));
f96637be
JP
3521/*
3522 cifs_dbg(FYI, "perm %d tag %d id %d\n",
3523 ace->e_perm, ace->e_tag, ace->e_id);
3524*/
1da177e4
LT
3525
3526 return;
3527}
3528
3529/* Convert ACL from CIFS POSIX wire format to local Linux POSIX ACL xattr */
50c2f753
SF
3530static int cifs_copy_posix_acl(char *trgt, char *src, const int buflen,
3531 const int acl_type, const int size_of_data_area)
1da177e4
LT
3532{
3533 int size = 0;
3534 int i;
3535 __u16 count;
50c2f753
SF
3536 struct cifs_posix_ace *pACE;
3537 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)src;
2211d5ba 3538 struct posix_acl_xattr_header *local_acl = (void *)trgt;
1da177e4
LT
3539
3540 if (le16_to_cpu(cifs_acl->version) != CIFS_ACL_VERSION)
3541 return -EOPNOTSUPP;
3542
45987e00 3543 if (acl_type == ACL_TYPE_ACCESS) {
1da177e4
LT
3544 count = le16_to_cpu(cifs_acl->access_entry_count);
3545 pACE = &cifs_acl->ace_array[0];
3546 size = sizeof(struct cifs_posix_acl);
3547 size += sizeof(struct cifs_posix_ace) * count;
3548 /* check if we would go beyond end of SMB */
790fe579 3549 if (size_of_data_area < size) {
f96637be
JP
3550 cifs_dbg(FYI, "bad CIFS POSIX ACL size %d vs. %d\n",
3551 size_of_data_area, size);
1da177e4
LT
3552 return -EINVAL;
3553 }
45987e00 3554 } else if (acl_type == ACL_TYPE_DEFAULT) {
1da177e4
LT
3555 count = le16_to_cpu(cifs_acl->access_entry_count);
3556 size = sizeof(struct cifs_posix_acl);
3557 size += sizeof(struct cifs_posix_ace) * count;
3558/* skip past access ACEs to get to default ACEs */
3559 pACE = &cifs_acl->ace_array[count];
3560 count = le16_to_cpu(cifs_acl->default_entry_count);
3561 size += sizeof(struct cifs_posix_ace) * count;
3562 /* check if we would go beyond end of SMB */
790fe579 3563 if (size_of_data_area < size)
1da177e4
LT
3564 return -EINVAL;
3565 } else {
3566 /* illegal type */
3567 return -EINVAL;
3568 }
3569
3570 size = posix_acl_xattr_size(count);
790fe579 3571 if ((buflen == 0) || (local_acl == NULL)) {
50c2f753 3572 /* used to query ACL EA size */
790fe579 3573 } else if (size > buflen) {
1da177e4
LT
3574 return -ERANGE;
3575 } else /* buffer big enough */ {
2211d5ba
AG
3576 struct posix_acl_xattr_entry *ace = (void *)(local_acl + 1);
3577
ff7feac9 3578 local_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
50c2f753 3579 for (i = 0; i < count ; i++) {
2211d5ba 3580 cifs_convert_ace(&ace[i], pACE);
50c2f753 3581 pACE++;
1da177e4
LT
3582 }
3583 }
3584 return size;
3585}
3586
50c2f753 3587static __u16 convert_ace_to_cifs_ace(struct cifs_posix_ace *cifs_ace,
2211d5ba 3588 const struct posix_acl_xattr_entry *local_ace)
1da177e4
LT
3589{
3590 __u16 rc = 0; /* 0 = ACL converted ok */
3591
ff7feac9
SF
3592 cifs_ace->cifs_e_perm = le16_to_cpu(local_ace->e_perm);
3593 cifs_ace->cifs_e_tag = le16_to_cpu(local_ace->e_tag);
1da177e4 3594 /* BB is there a better way to handle the large uid? */
790fe579 3595 if (local_ace->e_id == cpu_to_le32(-1)) {
1da177e4
LT
3596 /* Probably no need to le convert -1 on any arch but can not hurt */
3597 cifs_ace->cifs_uid = cpu_to_le64(-1);
50c2f753 3598 } else
ff7feac9 3599 cifs_ace->cifs_uid = cpu_to_le64(le32_to_cpu(local_ace->e_id));
f96637be
JP
3600/*
3601 cifs_dbg(FYI, "perm %d tag %d id %d\n",
3602 ace->e_perm, ace->e_tag, ace->e_id);
3603*/
1da177e4
LT
3604 return rc;
3605}
3606
3607/* Convert ACL from local Linux POSIX xattr to CIFS POSIX ACL wire format */
50c2f753
SF
3608static __u16 ACL_to_cifs_posix(char *parm_data, const char *pACL,
3609 const int buflen, const int acl_type)
1da177e4
LT
3610{
3611 __u16 rc = 0;
50c2f753 3612 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)parm_data;
2211d5ba 3613 struct posix_acl_xattr_header *local_acl = (void *)pACL;
ae9ebe7c 3614 struct posix_acl_xattr_entry *ace = (void *)(local_acl + 1);
1da177e4
LT
3615 int count;
3616 int i;
3617
790fe579 3618 if ((buflen == 0) || (pACL == NULL) || (cifs_acl == NULL))
1da177e4
LT
3619 return 0;
3620
3621 count = posix_acl_xattr_count((size_t)buflen);
f96637be
JP
3622 cifs_dbg(FYI, "setting acl with %d entries from buf of length %d and version of %d\n",
3623 count, buflen, le32_to_cpu(local_acl->a_version));
790fe579 3624 if (le32_to_cpu(local_acl->a_version) != 2) {
f96637be
JP
3625 cifs_dbg(FYI, "unknown POSIX ACL version %d\n",
3626 le32_to_cpu(local_acl->a_version));
1da177e4
LT
3627 return 0;
3628 }
3629 cifs_acl->version = cpu_to_le16(1);
b1d93356 3630 if (acl_type == ACL_TYPE_ACCESS) {
ff7feac9 3631 cifs_acl->access_entry_count = cpu_to_le16(count);
bc09d141 3632 cifs_acl->default_entry_count = cpu_to_le16(0xFFFF);
b1d93356 3633 } else if (acl_type == ACL_TYPE_DEFAULT) {
ff7feac9 3634 cifs_acl->default_entry_count = cpu_to_le16(count);
bc09d141 3635 cifs_acl->access_entry_count = cpu_to_le16(0xFFFF);
b1d93356 3636 } else {
f96637be 3637 cifs_dbg(FYI, "unknown ACL type %d\n", acl_type);
1da177e4
LT
3638 return 0;
3639 }
50c2f753 3640 for (i = 0; i < count; i++) {
ae9ebe7c 3641 rc = convert_ace_to_cifs_ace(&cifs_acl->ace_array[i], &ace[i]);
790fe579 3642 if (rc != 0) {
1da177e4
LT
3643 /* ACE not converted */
3644 break;
3645 }
3646 }
790fe579 3647 if (rc == 0) {
1da177e4
LT
3648 rc = (__u16)(count * sizeof(struct cifs_posix_ace));
3649 rc += sizeof(struct cifs_posix_acl);
3650 /* BB add check to make sure ACL does not overflow SMB */
3651 }
3652 return rc;
3653}
3654
3655int
6d5786a3 3656CIFSSMBGetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
50c2f753
SF
3657 const unsigned char *searchName,
3658 char *acl_inf, const int buflen, const int acl_type,
3659 const struct nls_table *nls_codepage, int remap)
1da177e4
LT
3660{
3661/* SMB_QUERY_POSIX_ACL */
3662 TRANSACTION2_QPI_REQ *pSMB = NULL;
3663 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3664 int rc = 0;
3665 int bytes_returned;
3666 int name_len;
3667 __u16 params, byte_count;
50c2f753 3668
f96637be 3669 cifs_dbg(FYI, "In GetPosixACL (Unix) for path %s\n", searchName);
1da177e4
LT
3670
3671queryAclRetry:
3672 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3673 (void **) &pSMBr);
3674 if (rc)
3675 return rc;
50c2f753 3676
1da177e4
LT
3677 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3678 name_len =
acbbb76a
SF
3679 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3680 searchName, PATH_MAX, nls_codepage,
3681 remap);
1da177e4
LT
3682 name_len++; /* trailing null */
3683 name_len *= 2;
3684 pSMB->FileName[name_len] = 0;
3685 pSMB->FileName[name_len+1] = 0;
50c2f753 3686 } else { /* BB improve the check for buffer overruns BB */
1da177e4
LT
3687 name_len = strnlen(searchName, PATH_MAX);
3688 name_len++; /* trailing null */
3689 strncpy(pSMB->FileName, searchName, name_len);
3690 }
3691
3692 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3693 pSMB->TotalDataCount = 0;
3694 pSMB->MaxParameterCount = cpu_to_le16(2);
50c2f753 3695 /* BB find exact max data count below from sess structure BB */
1da177e4
LT
3696 pSMB->MaxDataCount = cpu_to_le16(4000);
3697 pSMB->MaxSetupCount = 0;
3698 pSMB->Reserved = 0;
3699 pSMB->Flags = 0;
3700 pSMB->Timeout = 0;
3701 pSMB->Reserved2 = 0;
3702 pSMB->ParameterOffset = cpu_to_le16(
50c2f753
SF
3703 offsetof(struct smb_com_transaction2_qpi_req,
3704 InformationLevel) - 4);
1da177e4
LT
3705 pSMB->DataCount = 0;
3706 pSMB->DataOffset = 0;
3707 pSMB->SetupCount = 1;
3708 pSMB->Reserved3 = 0;
3709 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3710 byte_count = params + 1 /* pad */ ;
3711 pSMB->TotalParameterCount = cpu_to_le16(params);
3712 pSMB->ParameterCount = pSMB->TotalParameterCount;
3713 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_ACL);
3714 pSMB->Reserved4 = 0;
be8e3b00 3715 inc_rfc1001_len(pSMB, byte_count);
1da177e4
LT
3716 pSMB->ByteCount = cpu_to_le16(byte_count);
3717
3718 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3719 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
44c58186 3720 cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
1da177e4 3721 if (rc) {
f96637be 3722 cifs_dbg(FYI, "Send error in Query POSIX ACL = %d\n", rc);
1da177e4
LT
3723 } else {
3724 /* decode response */
50c2f753 3725
1da177e4 3726 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
1da177e4 3727 /* BB also check enough total bytes returned */
820a803f 3728 if (rc || get_bcc(&pSMBr->hdr) < 2)
1da177e4
LT
3729 rc = -EIO; /* bad smb */
3730 else {
3731 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3732 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3733 rc = cifs_copy_posix_acl(acl_inf,
3734 (char *)&pSMBr->hdr.Protocol+data_offset,
50c2f753 3735 buflen, acl_type, count);
1da177e4
LT
3736 }
3737 }
3738 cifs_buf_release(pSMB);
3739 if (rc == -EAGAIN)
3740 goto queryAclRetry;
3741 return rc;
3742}
3743
3744int
6d5786a3 3745CIFSSMBSetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
50c2f753
SF
3746 const unsigned char *fileName,
3747 const char *local_acl, const int buflen,
3748 const int acl_type,
3749 const struct nls_table *nls_codepage, int remap)
1da177e4
LT
3750{
3751 struct smb_com_transaction2_spi_req *pSMB = NULL;
3752 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
3753 char *parm_data;
3754 int name_len;
3755 int rc = 0;
3756 int bytes_returned = 0;
3757 __u16 params, byte_count, data_count, param_offset, offset;
3758
f96637be 3759 cifs_dbg(FYI, "In SetPosixACL (Unix) for path %s\n", fileName);
1da177e4
LT
3760setAclRetry:
3761 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
50c2f753 3762 (void **) &pSMBr);
1da177e4
LT
3763 if (rc)
3764 return rc;
3765 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3766 name_len =
acbbb76a
SF
3767 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
3768 PATH_MAX, nls_codepage, remap);
1da177e4
LT
3769 name_len++; /* trailing null */
3770 name_len *= 2;
50c2f753 3771 } else { /* BB improve the check for buffer overruns BB */
1da177e4
LT
3772 name_len = strnlen(fileName, PATH_MAX);
3773 name_len++; /* trailing null */
3774 strncpy(pSMB->FileName, fileName, name_len);
3775 }
3776 params = 6 + name_len;
3777 pSMB->MaxParameterCount = cpu_to_le16(2);
582d21e5
SF
3778 /* BB find max SMB size from sess */
3779 pSMB->MaxDataCount = cpu_to_le16(1000);
1da177e4
LT
3780 pSMB->MaxSetupCount = 0;
3781 pSMB->Reserved = 0;
3782 pSMB->Flags = 0;
3783 pSMB->Timeout = 0;
3784 pSMB->Reserved2 = 0;
3785 param_offset = offsetof(struct smb_com_transaction2_spi_req,
50c2f753 3786 InformationLevel) - 4;
1da177e4
LT
3787 offset = param_offset + params;
3788 parm_data = ((char *) &pSMB->hdr.Protocol) + offset;
3789 pSMB->ParameterOffset = cpu_to_le16(param_offset);
3790
3791 /* convert to on the wire format for POSIX ACL */
50c2f753 3792 data_count = ACL_to_cifs_posix(parm_data, local_acl, buflen, acl_type);
1da177e4 3793
790fe579 3794 if (data_count == 0) {
1da177e4
LT
3795 rc = -EOPNOTSUPP;
3796 goto setACLerrorExit;
3797 }
3798 pSMB->DataOffset = cpu_to_le16(offset);
3799 pSMB->SetupCount = 1;
3800 pSMB->Reserved3 = 0;
3801 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
3802 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_ACL);
3803 byte_count = 3 /* pad */ + params + data_count;
3804 pSMB->DataCount = cpu_to_le16(data_count);
3805 pSMB->TotalDataCount = pSMB->DataCount;
3806 pSMB->ParameterCount = cpu_to_le16(params);
3807 pSMB->TotalParameterCount = pSMB->ParameterCount;
3808 pSMB->Reserved4 = 0;
be8e3b00 3809 inc_rfc1001_len(pSMB, byte_count);
1da177e4
LT
3810 pSMB->ByteCount = cpu_to_le16(byte_count);
3811 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
50c2f753 3812 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
ad7a2926 3813 if (rc)
f96637be 3814 cifs_dbg(FYI, "Set POSIX ACL returned %d\n", rc);
1da177e4
LT
3815
3816setACLerrorExit:
3817 cifs_buf_release(pSMB);
3818 if (rc == -EAGAIN)
3819 goto setAclRetry;
3820 return rc;
3821}
3822
f654bac2
SF
3823/* BB fix tabs in this function FIXME BB */
3824int
6d5786a3 3825CIFSGetExtAttr(const unsigned int xid, struct cifs_tcon *tcon,
ad7a2926 3826 const int netfid, __u64 *pExtAttrBits, __u64 *pMask)
f654bac2 3827{
50c2f753
SF
3828 int rc = 0;
3829 struct smb_t2_qfi_req *pSMB = NULL;
3830 struct smb_t2_qfi_rsp *pSMBr = NULL;
3831 int bytes_returned;
3832 __u16 params, byte_count;
f654bac2 3833
f96637be 3834 cifs_dbg(FYI, "In GetExtAttr\n");
790fe579
SF
3835 if (tcon == NULL)
3836 return -ENODEV;
f654bac2
SF
3837
3838GetExtAttrRetry:
790fe579
SF
3839 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3840 (void **) &pSMBr);
3841 if (rc)
3842 return rc;
f654bac2 3843
ad7a2926 3844 params = 2 /* level */ + 2 /* fid */;
790fe579
SF
3845 pSMB->t2.TotalDataCount = 0;
3846 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
3847 /* BB find exact max data count below from sess structure BB */
3848 pSMB->t2.MaxDataCount = cpu_to_le16(4000);
3849 pSMB->t2.MaxSetupCount = 0;
3850 pSMB->t2.Reserved = 0;
3851 pSMB->t2.Flags = 0;
3852 pSMB->t2.Timeout = 0;
3853 pSMB->t2.Reserved2 = 0;
3854 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
3855 Fid) - 4);
3856 pSMB->t2.DataCount = 0;
3857 pSMB->t2.DataOffset = 0;
3858 pSMB->t2.SetupCount = 1;
3859 pSMB->t2.Reserved3 = 0;
3860 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
3861 byte_count = params + 1 /* pad */ ;
3862 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
3863 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
3864 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_ATTR_FLAGS);
3865 pSMB->Pad = 0;
f654bac2 3866 pSMB->Fid = netfid;
be8e3b00 3867 inc_rfc1001_len(pSMB, byte_count);
790fe579
SF
3868 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
3869
3870 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3871 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3872 if (rc) {
f96637be 3873 cifs_dbg(FYI, "error %d in GetExtAttr\n", rc);
790fe579
SF
3874 } else {
3875 /* decode response */
3876 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
790fe579 3877 /* BB also check enough total bytes returned */
820a803f 3878 if (rc || get_bcc(&pSMBr->hdr) < 2)
790fe579
SF
3879 /* If rc should we check for EOPNOSUPP and
3880 disable the srvino flag? or in caller? */
3881 rc = -EIO; /* bad smb */
3882 else {
3883 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3884 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3885 struct file_chattr_info *pfinfo;
3886 /* BB Do we need a cast or hash here ? */
3887 if (count != 16) {
f96637be 3888 cifs_dbg(FYI, "Illegal size ret in GetExtAttr\n");
790fe579
SF
3889 rc = -EIO;
3890 goto GetExtAttrOut;
3891 }
3892 pfinfo = (struct file_chattr_info *)
3893 (data_offset + (char *) &pSMBr->hdr.Protocol);
3894 *pExtAttrBits = le64_to_cpu(pfinfo->mode);
f654bac2 3895 *pMask = le64_to_cpu(pfinfo->mask);
790fe579
SF
3896 }
3897 }
f654bac2 3898GetExtAttrOut:
790fe579
SF
3899 cifs_buf_release(pSMB);
3900 if (rc == -EAGAIN)
3901 goto GetExtAttrRetry;
3902 return rc;
f654bac2
SF
3903}
3904
f654bac2 3905#endif /* CONFIG_POSIX */
1da177e4 3906
79df1bae
JL
3907#ifdef CONFIG_CIFS_ACL
3908/*
3909 * Initialize NT TRANSACT SMB into small smb request buffer. This assumes that
3910 * all NT TRANSACTS that we init here have total parm and data under about 400
3911 * bytes (to fit in small cifs buffer size), which is the case so far, it
3912 * easily fits. NB: Setup words themselves and ByteCount MaxSetupCount (size of
3913 * returned setup area) and MaxParameterCount (returned parms size) must be set
3914 * by caller
3915 */
3916static int
3917smb_init_nttransact(const __u16 sub_command, const int setup_count,
96daf2b0 3918 const int parm_len, struct cifs_tcon *tcon,
79df1bae
JL
3919 void **ret_buf)
3920{
3921 int rc;
3922 __u32 temp_offset;
3923 struct smb_com_ntransact_req *pSMB;
3924
3925 rc = small_smb_init(SMB_COM_NT_TRANSACT, 19 + setup_count, tcon,
3926 (void **)&pSMB);
3927 if (rc)
3928 return rc;
3929 *ret_buf = (void *)pSMB;
3930 pSMB->Reserved = 0;
3931 pSMB->TotalParameterCount = cpu_to_le32(parm_len);
3932 pSMB->TotalDataCount = 0;
c974befa 3933 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
79df1bae
JL
3934 pSMB->ParameterCount = pSMB->TotalParameterCount;
3935 pSMB->DataCount = pSMB->TotalDataCount;
3936 temp_offset = offsetof(struct smb_com_ntransact_req, Parms) +
3937 (setup_count * 2) - 4 /* for rfc1001 length itself */;
3938 pSMB->ParameterOffset = cpu_to_le32(temp_offset);
3939 pSMB->DataOffset = cpu_to_le32(temp_offset + parm_len);
3940 pSMB->SetupCount = setup_count; /* no need to le convert byte fields */
3941 pSMB->SubCommand = cpu_to_le16(sub_command);
3942 return 0;
3943}
3944
3945static int
3946validate_ntransact(char *buf, char **ppparm, char **ppdata,
3947 __u32 *pparmlen, __u32 *pdatalen)
3948{
3949 char *end_of_smb;
3950 __u32 data_count, data_offset, parm_count, parm_offset;
3951 struct smb_com_ntransact_rsp *pSMBr;
820a803f 3952 u16 bcc;
79df1bae
JL
3953
3954 *pdatalen = 0;
3955 *pparmlen = 0;
3956
3957 if (buf == NULL)
3958 return -EINVAL;
3959
3960 pSMBr = (struct smb_com_ntransact_rsp *)buf;
3961
820a803f
JL
3962 bcc = get_bcc(&pSMBr->hdr);
3963 end_of_smb = 2 /* sizeof byte count */ + bcc +
79df1bae
JL
3964 (char *)&pSMBr->ByteCount;
3965
3966 data_offset = le32_to_cpu(pSMBr->DataOffset);
3967 data_count = le32_to_cpu(pSMBr->DataCount);
3968 parm_offset = le32_to_cpu(pSMBr->ParameterOffset);
3969 parm_count = le32_to_cpu(pSMBr->ParameterCount);
3970
3971 *ppparm = (char *)&pSMBr->hdr.Protocol + parm_offset;
3972 *ppdata = (char *)&pSMBr->hdr.Protocol + data_offset;
3973
3974 /* should we also check that parm and data areas do not overlap? */
3975 if (*ppparm > end_of_smb) {
f96637be 3976 cifs_dbg(FYI, "parms start after end of smb\n");
79df1bae
JL
3977 return -EINVAL;
3978 } else if (parm_count + *ppparm > end_of_smb) {
f96637be 3979 cifs_dbg(FYI, "parm end after end of smb\n");
79df1bae
JL
3980 return -EINVAL;
3981 } else if (*ppdata > end_of_smb) {
f96637be 3982 cifs_dbg(FYI, "data starts after end of smb\n");
79df1bae
JL
3983 return -EINVAL;
3984 } else if (data_count + *ppdata > end_of_smb) {
f96637be
JP
3985 cifs_dbg(FYI, "data %p + count %d (%p) past smb end %p start %p\n",
3986 *ppdata, data_count, (data_count + *ppdata),
3987 end_of_smb, pSMBr);
79df1bae 3988 return -EINVAL;
820a803f 3989 } else if (parm_count + data_count > bcc) {
f96637be 3990 cifs_dbg(FYI, "parm count and data count larger than SMB\n");
79df1bae
JL
3991 return -EINVAL;
3992 }
3993 *pdatalen = data_count;
3994 *pparmlen = parm_count;
3995 return 0;
3996}
3997
0a4b92c0
SF
3998/* Get Security Descriptor (by handle) from remote server for a file or dir */
3999int
6d5786a3 4000CIFSSMBGetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
630f3f0c 4001 struct cifs_ntsd **acl_inf, __u32 *pbuflen)
0a4b92c0
SF
4002{
4003 int rc = 0;
4004 int buf_type = 0;
ad7a2926 4005 QUERY_SEC_DESC_REQ *pSMB;
0a4b92c0 4006 struct kvec iov[1];
da502f7d 4007 struct kvec rsp_iov;
0a4b92c0 4008
f96637be 4009 cifs_dbg(FYI, "GetCifsACL\n");
0a4b92c0 4010
630f3f0c
SF
4011 *pbuflen = 0;
4012 *acl_inf = NULL;
4013
b9c7a2bb 4014 rc = smb_init_nttransact(NT_TRANSACT_QUERY_SECURITY_DESC, 0,
0a4b92c0
SF
4015 8 /* parm len */, tcon, (void **) &pSMB);
4016 if (rc)
4017 return rc;
4018
4019 pSMB->MaxParameterCount = cpu_to_le32(4);
4020 /* BB TEST with big acls that might need to be e.g. larger than 16K */
4021 pSMB->MaxSetupCount = 0;
4022 pSMB->Fid = fid; /* file handle always le */
4023 pSMB->AclFlags = cpu_to_le32(CIFS_ACL_OWNER | CIFS_ACL_GROUP |
4024 CIFS_ACL_DACL);
4025 pSMB->ByteCount = cpu_to_le16(11); /* 3 bytes pad + 8 bytes parm */
be8e3b00 4026 inc_rfc1001_len(pSMB, 11);
0a4b92c0 4027 iov[0].iov_base = (char *)pSMB;
be8e3b00 4028 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
0a4b92c0 4029
a761ac57 4030 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovec */, &buf_type,
da502f7d
PS
4031 0, &rsp_iov);
4032 cifs_small_buf_release(pSMB);
44c58186 4033 cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
0a4b92c0 4034 if (rc) {
f96637be 4035 cifs_dbg(FYI, "Send error in QuerySecDesc = %d\n", rc);
0a4b92c0 4036 } else { /* decode response */
ad7a2926 4037 __le32 *parm;
630f3f0c
SF
4038 __u32 parm_len;
4039 __u32 acl_len;
50c2f753 4040 struct smb_com_ntransact_rsp *pSMBr;
630f3f0c 4041 char *pdata;
0a4b92c0
SF
4042
4043/* validate_nttransact */
da502f7d 4044 rc = validate_ntransact(rsp_iov.iov_base, (char **)&parm,
630f3f0c 4045 &pdata, &parm_len, pbuflen);
790fe579 4046 if (rc)
0a4b92c0 4047 goto qsec_out;
da502f7d 4048 pSMBr = (struct smb_com_ntransact_rsp *)rsp_iov.iov_base;
0a4b92c0 4049
f96637be
JP
4050 cifs_dbg(FYI, "smb %p parm %p data %p\n",
4051 pSMBr, parm, *acl_inf);
0a4b92c0
SF
4052
4053 if (le32_to_cpu(pSMBr->ParameterCount) != 4) {
4054 rc = -EIO; /* bad smb */
630f3f0c 4055 *pbuflen = 0;
0a4b92c0
SF
4056 goto qsec_out;
4057 }
4058
4059/* BB check that data area is minimum length and as big as acl_len */
4060
af6f4612 4061 acl_len = le32_to_cpu(*parm);
630f3f0c 4062 if (acl_len != *pbuflen) {
f96637be
JP
4063 cifs_dbg(VFS, "acl length %d does not match %d\n",
4064 acl_len, *pbuflen);
630f3f0c
SF
4065 if (*pbuflen > acl_len)
4066 *pbuflen = acl_len;
4067 }
0a4b92c0 4068
630f3f0c
SF
4069 /* check if buffer is big enough for the acl
4070 header followed by the smallest SID */
4071 if ((*pbuflen < sizeof(struct cifs_ntsd) + 8) ||
4072 (*pbuflen >= 64 * 1024)) {
f96637be 4073 cifs_dbg(VFS, "bad acl length %d\n", *pbuflen);
630f3f0c
SF
4074 rc = -EINVAL;
4075 *pbuflen = 0;
4076 } else {
f7f7c185 4077 *acl_inf = kmemdup(pdata, *pbuflen, GFP_KERNEL);
630f3f0c
SF
4078 if (*acl_inf == NULL) {
4079 *pbuflen = 0;
4080 rc = -ENOMEM;
4081 }
630f3f0c 4082 }
0a4b92c0
SF
4083 }
4084qsec_out:
da502f7d 4085 free_rsp_buf(buf_type, rsp_iov.iov_base);
0a4b92c0
SF
4086 return rc;
4087}
97837582
SF
4088
4089int
6d5786a3 4090CIFSSMBSetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
a5ff3769 4091 struct cifs_ntsd *pntsd, __u32 acllen, int aclflag)
97837582
SF
4092{
4093 __u16 byte_count, param_count, data_count, param_offset, data_offset;
4094 int rc = 0;
4095 int bytes_returned = 0;
4096 SET_SEC_DESC_REQ *pSMB = NULL;
b2a3ad9c 4097 void *pSMBr;
97837582
SF
4098
4099setCifsAclRetry:
b2a3ad9c 4100 rc = smb_init(SMB_COM_NT_TRANSACT, 19, tcon, (void **) &pSMB, &pSMBr);
97837582 4101 if (rc)
b2a3ad9c 4102 return rc;
97837582
SF
4103
4104 pSMB->MaxSetupCount = 0;
4105 pSMB->Reserved = 0;
4106
4107 param_count = 8;
4108 param_offset = offsetof(struct smb_com_transaction_ssec_req, Fid) - 4;
4109 data_count = acllen;
4110 data_offset = param_offset + param_count;
4111 byte_count = 3 /* pad */ + param_count;
4112
4113 pSMB->DataCount = cpu_to_le32(data_count);
4114 pSMB->TotalDataCount = pSMB->DataCount;
4115 pSMB->MaxParameterCount = cpu_to_le32(4);
4116 pSMB->MaxDataCount = cpu_to_le32(16384);
4117 pSMB->ParameterCount = cpu_to_le32(param_count);
4118 pSMB->ParameterOffset = cpu_to_le32(param_offset);
4119 pSMB->TotalParameterCount = pSMB->ParameterCount;
4120 pSMB->DataOffset = cpu_to_le32(data_offset);
4121 pSMB->SetupCount = 0;
4122 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_SET_SECURITY_DESC);
4123 pSMB->ByteCount = cpu_to_le16(byte_count+data_count);
4124
4125 pSMB->Fid = fid; /* file handle always le */
4126 pSMB->Reserved2 = 0;
a5ff3769 4127 pSMB->AclFlags = cpu_to_le32(aclflag);
97837582
SF
4128
4129 if (pntsd && acllen) {
b2a3ad9c
JL
4130 memcpy((char *)pSMBr + offsetof(struct smb_hdr, Protocol) +
4131 data_offset, pntsd, acllen);
be8e3b00 4132 inc_rfc1001_len(pSMB, byte_count + data_count);
97837582 4133 } else
be8e3b00 4134 inc_rfc1001_len(pSMB, byte_count);
97837582
SF
4135
4136 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4137 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4138
f96637be
JP
4139 cifs_dbg(FYI, "SetCIFSACL bytes_returned: %d, rc: %d\n",
4140 bytes_returned, rc);
97837582 4141 if (rc)
f96637be 4142 cifs_dbg(FYI, "Set CIFS ACL returned %d\n", rc);
97837582
SF
4143 cifs_buf_release(pSMB);
4144
4145 if (rc == -EAGAIN)
4146 goto setCifsAclRetry;
4147
4148 return (rc);
4149}
4150
79df1bae 4151#endif /* CONFIG_CIFS_ACL */
0a4b92c0 4152
6b8edfe0
SF
4153/* Legacy Query Path Information call for lookup to old servers such
4154 as Win9x/WinME */
68889f26
PS
4155int
4156SMBQueryInformation(const unsigned int xid, struct cifs_tcon *tcon,
4157 const char *search_name, FILE_ALL_INFO *data,
4158 const struct nls_table *nls_codepage, int remap)
6b8edfe0 4159{
ad7a2926
SF
4160 QUERY_INFORMATION_REQ *pSMB;
4161 QUERY_INFORMATION_RSP *pSMBr;
6b8edfe0
SF
4162 int rc = 0;
4163 int bytes_returned;
4164 int name_len;
4165
f96637be 4166 cifs_dbg(FYI, "In SMBQPath path %s\n", search_name);
6b8edfe0
SF
4167QInfRetry:
4168 rc = smb_init(SMB_COM_QUERY_INFORMATION, 0, tcon, (void **) &pSMB,
50c2f753 4169 (void **) &pSMBr);
6b8edfe0
SF
4170 if (rc)
4171 return rc;
4172
4173 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4174 name_len =
acbbb76a 4175 cifsConvertToUTF16((__le16 *) pSMB->FileName,
68889f26 4176 search_name, PATH_MAX, nls_codepage,
acbbb76a 4177 remap);
6b8edfe0
SF
4178 name_len++; /* trailing null */
4179 name_len *= 2;
50c2f753 4180 } else {
68889f26 4181 name_len = strnlen(search_name, PATH_MAX);
6b8edfe0 4182 name_len++; /* trailing null */
68889f26 4183 strncpy(pSMB->FileName, search_name, name_len);
6b8edfe0
SF
4184 }
4185 pSMB->BufferFormat = 0x04;
50c2f753 4186 name_len++; /* account for buffer type byte */
be8e3b00 4187 inc_rfc1001_len(pSMB, (__u16)name_len);
6b8edfe0
SF
4188 pSMB->ByteCount = cpu_to_le16(name_len);
4189
4190 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
50c2f753 4191 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
6b8edfe0 4192 if (rc) {
f96637be 4193 cifs_dbg(FYI, "Send error in QueryInfo = %d\n", rc);
68889f26 4194 } else if (data) {
95390201 4195 struct timespec64 ts;
1bd5bbcb 4196 __u32 time = le32_to_cpu(pSMBr->last_write_time);
ad7a2926
SF
4197
4198 /* decode response */
1bd5bbcb 4199 /* BB FIXME - add time zone adjustment BB */
68889f26 4200 memset(data, 0, sizeof(FILE_ALL_INFO));
1bd5bbcb
SF
4201 ts.tv_nsec = 0;
4202 ts.tv_sec = time;
4203 /* decode time fields */
68889f26
PS
4204 data->ChangeTime = cpu_to_le64(cifs_UnixTimeToNT(ts));
4205 data->LastWriteTime = data->ChangeTime;
4206 data->LastAccessTime = 0;
4207 data->AllocationSize =
70ca734a 4208 cpu_to_le64(le32_to_cpu(pSMBr->size));
68889f26
PS
4209 data->EndOfFile = data->AllocationSize;
4210 data->Attributes =
70ca734a 4211 cpu_to_le32(le16_to_cpu(pSMBr->attr));
6b8edfe0
SF
4212 } else
4213 rc = -EIO; /* bad buffer passed in */
4214
4215 cifs_buf_release(pSMB);
4216
4217 if (rc == -EAGAIN)
4218 goto QInfRetry;
4219
4220 return rc;
4221}
4222
bcd5357f 4223int
6d5786a3 4224CIFSSMBQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
bcd5357f
JL
4225 u16 netfid, FILE_ALL_INFO *pFindData)
4226{
4227 struct smb_t2_qfi_req *pSMB = NULL;
4228 struct smb_t2_qfi_rsp *pSMBr = NULL;
4229 int rc = 0;
4230 int bytes_returned;
4231 __u16 params, byte_count;
4232
4233QFileInfoRetry:
4234 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4235 (void **) &pSMBr);
4236 if (rc)
4237 return rc;
4238
4239 params = 2 /* level */ + 2 /* fid */;
4240 pSMB->t2.TotalDataCount = 0;
4241 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4242 /* BB find exact max data count below from sess structure BB */
4243 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4244 pSMB->t2.MaxSetupCount = 0;
4245 pSMB->t2.Reserved = 0;
4246 pSMB->t2.Flags = 0;
4247 pSMB->t2.Timeout = 0;
4248 pSMB->t2.Reserved2 = 0;
4249 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4250 Fid) - 4);
4251 pSMB->t2.DataCount = 0;
4252 pSMB->t2.DataOffset = 0;
4253 pSMB->t2.SetupCount = 1;
4254 pSMB->t2.Reserved3 = 0;
4255 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4256 byte_count = params + 1 /* pad */ ;
4257 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4258 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4259 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
4260 pSMB->Pad = 0;
4261 pSMB->Fid = netfid;
be8e3b00 4262 inc_rfc1001_len(pSMB, byte_count);
7ac0febb 4263 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
6b8edfe0 4264
bcd5357f
JL
4265 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4266 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4267 if (rc) {
ebcc943c 4268 cifs_dbg(FYI, "Send error in QFileInfo = %d", rc);
bcd5357f
JL
4269 } else { /* decode response */
4270 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
6b8edfe0 4271
bcd5357f
JL
4272 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4273 rc = -EIO;
820a803f 4274 else if (get_bcc(&pSMBr->hdr) < 40)
bcd5357f
JL
4275 rc = -EIO; /* bad smb */
4276 else if (pFindData) {
4277 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4278 memcpy((char *) pFindData,
4279 (char *) &pSMBr->hdr.Protocol +
4280 data_offset, sizeof(FILE_ALL_INFO));
4281 } else
4282 rc = -ENOMEM;
4283 }
4284 cifs_buf_release(pSMB);
4285 if (rc == -EAGAIN)
4286 goto QFileInfoRetry;
6b8edfe0 4287
bcd5357f
JL
4288 return rc;
4289}
6b8edfe0 4290
1da177e4 4291int
6d5786a3 4292CIFSSMBQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
68889f26 4293 const char *search_name, FILE_ALL_INFO *data,
acf1a1b1 4294 int legacy /* old style infolevel */,
737b758c 4295 const struct nls_table *nls_codepage, int remap)
1da177e4 4296{
68889f26 4297 /* level 263 SMB_QUERY_FILE_ALL_INFO */
1da177e4
LT
4298 TRANSACTION2_QPI_REQ *pSMB = NULL;
4299 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4300 int rc = 0;
4301 int bytes_returned;
4302 int name_len;
4303 __u16 params, byte_count;
4304
f96637be 4305 /* cifs_dbg(FYI, "In QPathInfo path %s\n", search_name); */
1da177e4
LT
4306QPathInfoRetry:
4307 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4308 (void **) &pSMBr);
4309 if (rc)
4310 return rc;
4311
4312 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4313 name_len =
68889f26 4314 cifsConvertToUTF16((__le16 *) pSMB->FileName, search_name,
acbbb76a 4315 PATH_MAX, nls_codepage, remap);
1da177e4
LT
4316 name_len++; /* trailing null */
4317 name_len *= 2;
50c2f753 4318 } else { /* BB improve the check for buffer overruns BB */
68889f26 4319 name_len = strnlen(search_name, PATH_MAX);
1da177e4 4320 name_len++; /* trailing null */
68889f26 4321 strncpy(pSMB->FileName, search_name, name_len);
1da177e4
LT
4322 }
4323
50c2f753 4324 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
1da177e4
LT
4325 pSMB->TotalDataCount = 0;
4326 pSMB->MaxParameterCount = cpu_to_le16(2);
582d21e5
SF
4327 /* BB find exact max SMB PDU from sess structure BB */
4328 pSMB->MaxDataCount = cpu_to_le16(4000);
1da177e4
LT
4329 pSMB->MaxSetupCount = 0;
4330 pSMB->Reserved = 0;
4331 pSMB->Flags = 0;
4332 pSMB->Timeout = 0;
4333 pSMB->Reserved2 = 0;
4334 pSMB->ParameterOffset = cpu_to_le16(offsetof(
50c2f753 4335 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
1da177e4
LT
4336 pSMB->DataCount = 0;
4337 pSMB->DataOffset = 0;
4338 pSMB->SetupCount = 1;
4339 pSMB->Reserved3 = 0;
4340 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4341 byte_count = params + 1 /* pad */ ;
4342 pSMB->TotalParameterCount = cpu_to_le16(params);
4343 pSMB->ParameterCount = pSMB->TotalParameterCount;
790fe579 4344 if (legacy)
acf1a1b1
SF
4345 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_STANDARD);
4346 else
4347 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
1da177e4 4348 pSMB->Reserved4 = 0;
be8e3b00 4349 inc_rfc1001_len(pSMB, byte_count);
1da177e4
LT
4350 pSMB->ByteCount = cpu_to_le16(byte_count);
4351
4352 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4353 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4354 if (rc) {
f96637be 4355 cifs_dbg(FYI, "Send error in QPathInfo = %d\n", rc);
1da177e4
LT
4356 } else { /* decode response */
4357 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4358
acf1a1b1
SF
4359 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4360 rc = -EIO;
820a803f 4361 else if (!legacy && get_bcc(&pSMBr->hdr) < 40)
1da177e4 4362 rc = -EIO; /* bad smb */
820a803f 4363 else if (legacy && get_bcc(&pSMBr->hdr) < 24)
50c2f753
SF
4364 rc = -EIO; /* 24 or 26 expected but we do not read
4365 last field */
68889f26 4366 else if (data) {
acf1a1b1 4367 int size;
1da177e4 4368 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
ad7a2926 4369
68889f26
PS
4370 /*
4371 * On legacy responses we do not read the last field,
4372 * EAsize, fortunately since it varies by subdialect and
4373 * also note it differs on Set vs Get, ie two bytes or 4
4374 * bytes depending but we don't care here.
4375 */
ad7a2926 4376 if (legacy)
acf1a1b1
SF
4377 size = sizeof(FILE_INFO_STANDARD);
4378 else
4379 size = sizeof(FILE_ALL_INFO);
68889f26 4380 memcpy((char *) data, (char *) &pSMBr->hdr.Protocol +
acf1a1b1 4381 data_offset, size);
1da177e4
LT
4382 } else
4383 rc = -ENOMEM;
4384 }
4385 cifs_buf_release(pSMB);
4386 if (rc == -EAGAIN)
4387 goto QPathInfoRetry;
4388
4389 return rc;
4390}
4391
c8634fd3 4392int
6d5786a3 4393CIFSSMBUnixQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
c8634fd3
JL
4394 u16 netfid, FILE_UNIX_BASIC_INFO *pFindData)
4395{
4396 struct smb_t2_qfi_req *pSMB = NULL;
4397 struct smb_t2_qfi_rsp *pSMBr = NULL;
4398 int rc = 0;
4399 int bytes_returned;
4400 __u16 params, byte_count;
4401
4402UnixQFileInfoRetry:
4403 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4404 (void **) &pSMBr);
4405 if (rc)
4406 return rc;
4407
4408 params = 2 /* level */ + 2 /* fid */;
4409 pSMB->t2.TotalDataCount = 0;
4410 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4411 /* BB find exact max data count below from sess structure BB */
4412 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4413 pSMB->t2.MaxSetupCount = 0;
4414 pSMB->t2.Reserved = 0;
4415 pSMB->t2.Flags = 0;
4416 pSMB->t2.Timeout = 0;
4417 pSMB->t2.Reserved2 = 0;
4418 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4419 Fid) - 4);
4420 pSMB->t2.DataCount = 0;
4421 pSMB->t2.DataOffset = 0;
4422 pSMB->t2.SetupCount = 1;
4423 pSMB->t2.Reserved3 = 0;
4424 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4425 byte_count = params + 1 /* pad */ ;
4426 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4427 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4428 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4429 pSMB->Pad = 0;
4430 pSMB->Fid = netfid;
be8e3b00 4431 inc_rfc1001_len(pSMB, byte_count);
7ac0febb 4432 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
c8634fd3
JL
4433
4434 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4435 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4436 if (rc) {
ebcc943c 4437 cifs_dbg(FYI, "Send error in UnixQFileInfo = %d", rc);
c8634fd3
JL
4438 } else { /* decode response */
4439 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4440
820a803f 4441 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
f96637be 4442 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
4443 rc = -EIO; /* bad smb */
4444 } else {
4445 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4446 memcpy((char *) pFindData,
4447 (char *) &pSMBr->hdr.Protocol +
4448 data_offset,
4449 sizeof(FILE_UNIX_BASIC_INFO));
4450 }
4451 }
4452
4453 cifs_buf_release(pSMB);
4454 if (rc == -EAGAIN)
4455 goto UnixQFileInfoRetry;
4456
4457 return rc;
4458}
4459
1da177e4 4460int
6d5786a3 4461CIFSSMBUnixQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
1da177e4 4462 const unsigned char *searchName,
582d21e5 4463 FILE_UNIX_BASIC_INFO *pFindData,
737b758c 4464 const struct nls_table *nls_codepage, int remap)
1da177e4
LT
4465{
4466/* SMB_QUERY_FILE_UNIX_BASIC */
4467 TRANSACTION2_QPI_REQ *pSMB = NULL;
4468 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4469 int rc = 0;
4470 int bytes_returned = 0;
4471 int name_len;
4472 __u16 params, byte_count;
4473
f96637be 4474 cifs_dbg(FYI, "In QPathInfo (Unix) the path %s\n", searchName);
1da177e4
LT
4475UnixQPathInfoRetry:
4476 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4477 (void **) &pSMBr);
4478 if (rc)
4479 return rc;
4480
4481 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4482 name_len =
acbbb76a
SF
4483 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4484 PATH_MAX, nls_codepage, remap);
1da177e4
LT
4485 name_len++; /* trailing null */
4486 name_len *= 2;
50c2f753 4487 } else { /* BB improve the check for buffer overruns BB */
1da177e4
LT
4488 name_len = strnlen(searchName, PATH_MAX);
4489 name_len++; /* trailing null */
4490 strncpy(pSMB->FileName, searchName, name_len);
4491 }
4492
50c2f753 4493 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
1da177e4
LT
4494 pSMB->TotalDataCount = 0;
4495 pSMB->MaxParameterCount = cpu_to_le16(2);
4496 /* BB find exact max SMB PDU from sess structure BB */
50c2f753 4497 pSMB->MaxDataCount = cpu_to_le16(4000);
1da177e4
LT
4498 pSMB->MaxSetupCount = 0;
4499 pSMB->Reserved = 0;
4500 pSMB->Flags = 0;
4501 pSMB->Timeout = 0;
4502 pSMB->Reserved2 = 0;
4503 pSMB->ParameterOffset = cpu_to_le16(offsetof(
50c2f753 4504 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
1da177e4
LT
4505 pSMB->DataCount = 0;
4506 pSMB->DataOffset = 0;
4507 pSMB->SetupCount = 1;
4508 pSMB->Reserved3 = 0;
4509 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4510 byte_count = params + 1 /* pad */ ;
4511 pSMB->TotalParameterCount = cpu_to_le16(params);
4512 pSMB->ParameterCount = pSMB->TotalParameterCount;
4513 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4514 pSMB->Reserved4 = 0;
be8e3b00 4515 inc_rfc1001_len(pSMB, byte_count);
1da177e4
LT
4516 pSMB->ByteCount = cpu_to_le16(byte_count);
4517
4518 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4519 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4520 if (rc) {
ebcc943c 4521 cifs_dbg(FYI, "Send error in UnixQPathInfo = %d", rc);
1da177e4
LT
4522 } else { /* decode response */
4523 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4524
820a803f 4525 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
f96637be 4526 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
4527 rc = -EIO; /* bad smb */
4528 } else {
4529 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4530 memcpy((char *) pFindData,
4531 (char *) &pSMBr->hdr.Protocol +
4532 data_offset,
630f3f0c 4533 sizeof(FILE_UNIX_BASIC_INFO));
1da177e4
LT
4534 }
4535 }
4536 cifs_buf_release(pSMB);
4537 if (rc == -EAGAIN)
4538 goto UnixQPathInfoRetry;
4539
4540 return rc;
4541}
4542
1da177e4
LT
4543/* xid, tcon, searchName and codepage are input parms, rest are returned */
4544int
6d5786a3 4545CIFSFindFirst(const unsigned int xid, struct cifs_tcon *tcon,
c052e2b4 4546 const char *searchName, struct cifs_sb_info *cifs_sb,
2608bee7 4547 __u16 *pnetfid, __u16 search_flags,
c052e2b4 4548 struct cifs_search_info *psrch_inf, bool msearch)
1da177e4
LT
4549{
4550/* level 257 SMB_ */
4551 TRANSACTION2_FFIRST_REQ *pSMB = NULL;
4552 TRANSACTION2_FFIRST_RSP *pSMBr = NULL;
ad7a2926 4553 T2_FFIRST_RSP_PARMS *parms;
1da177e4
LT
4554 int rc = 0;
4555 int bytes_returned = 0;
c052e2b4 4556 int name_len, remap;
1da177e4 4557 __u16 params, byte_count;
c052e2b4 4558 struct nls_table *nls_codepage;
1da177e4 4559
f96637be 4560 cifs_dbg(FYI, "In FindFirst for %s\n", searchName);
1da177e4
LT
4561
4562findFirstRetry:
4563 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4564 (void **) &pSMBr);
4565 if (rc)
4566 return rc;
4567
c052e2b4 4568 nls_codepage = cifs_sb->local_nls;
2baa2682 4569 remap = cifs_remap(cifs_sb);
c052e2b4 4570
1da177e4
LT
4571 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4572 name_len =
acbbb76a
SF
4573 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4574 PATH_MAX, nls_codepage, remap);
737b758c
SF
4575 /* We can not add the asterik earlier in case
4576 it got remapped to 0xF03A as if it were part of the
4577 directory name instead of a wildcard */
1da177e4 4578 name_len *= 2;
c052e2b4
SP
4579 if (msearch) {
4580 pSMB->FileName[name_len] = CIFS_DIR_SEP(cifs_sb);
4581 pSMB->FileName[name_len+1] = 0;
4582 pSMB->FileName[name_len+2] = '*';
4583 pSMB->FileName[name_len+3] = 0;
4584 name_len += 4; /* now the trailing null */
4585 /* null terminate just in case */
4586 pSMB->FileName[name_len] = 0;
4587 pSMB->FileName[name_len+1] = 0;
4588 name_len += 2;
4589 }
1da177e4
LT
4590 } else { /* BB add check for overrun of SMB buf BB */
4591 name_len = strnlen(searchName, PATH_MAX);
1da177e4 4592/* BB fix here and in unicode clause above ie
790fe579 4593 if (name_len > buffersize-header)
1da177e4
LT
4594 free buffer exit; BB */
4595 strncpy(pSMB->FileName, searchName, name_len);
c052e2b4
SP
4596 if (msearch) {
4597 pSMB->FileName[name_len] = CIFS_DIR_SEP(cifs_sb);
4598 pSMB->FileName[name_len+1] = '*';
4599 pSMB->FileName[name_len+2] = 0;
4600 name_len += 3;
4601 }
1da177e4
LT
4602 }
4603
4604 params = 12 + name_len /* includes null */ ;
4605 pSMB->TotalDataCount = 0; /* no EAs */
4606 pSMB->MaxParameterCount = cpu_to_le16(10);
c974befa 4607 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
1da177e4
LT
4608 pSMB->MaxSetupCount = 0;
4609 pSMB->Reserved = 0;
4610 pSMB->Flags = 0;
4611 pSMB->Timeout = 0;
4612 pSMB->Reserved2 = 0;
4613 byte_count = params + 1 /* pad */ ;
4614 pSMB->TotalParameterCount = cpu_to_le16(params);
4615 pSMB->ParameterCount = pSMB->TotalParameterCount;
4616 pSMB->ParameterOffset = cpu_to_le16(
88274815
SF
4617 offsetof(struct smb_com_transaction2_ffirst_req, SearchAttributes)
4618 - 4);
1da177e4
LT
4619 pSMB->DataCount = 0;
4620 pSMB->DataOffset = 0;
4621 pSMB->SetupCount = 1; /* one byte, no need to make endian neutral */
4622 pSMB->Reserved3 = 0;
4623 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_FIRST);
4624 pSMB->SearchAttributes =
4625 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
4626 ATTR_DIRECTORY);
50c2f753 4627 pSMB->SearchCount = cpu_to_le16(CIFSMaxBufSize/sizeof(FILE_UNIX_INFO));
2608bee7 4628 pSMB->SearchFlags = cpu_to_le16(search_flags);
1da177e4
LT
4629 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4630
4631 /* BB what should we set StorageType to? Does it matter? BB */
4632 pSMB->SearchStorageType = 0;
be8e3b00 4633 inc_rfc1001_len(pSMB, byte_count);
1da177e4
LT
4634 pSMB->ByteCount = cpu_to_le16(byte_count);
4635
4636 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4637 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
44c58186 4638 cifs_stats_inc(&tcon->stats.cifs_stats.num_ffirst);
1da177e4 4639
88274815
SF
4640 if (rc) {/* BB add logic to retry regular search if Unix search
4641 rejected unexpectedly by server */
1da177e4 4642 /* BB Add code to handle unsupported level rc */
f96637be 4643 cifs_dbg(FYI, "Error in FindFirst = %d\n", rc);
1982c344 4644
88274815 4645 cifs_buf_release(pSMB);
1da177e4
LT
4646
4647 /* BB eventually could optimize out free and realloc of buf */
4648 /* for this case */
4649 if (rc == -EAGAIN)
4650 goto findFirstRetry;
4651 } else { /* decode response */
4652 /* BB remember to free buffer if error BB */
4653 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
790fe579 4654 if (rc == 0) {
b77d753c
SF
4655 unsigned int lnoff;
4656
1da177e4 4657 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
4b18f2a9 4658 psrch_inf->unicode = true;
1da177e4 4659 else
4b18f2a9 4660 psrch_inf->unicode = false;
1da177e4
LT
4661
4662 psrch_inf->ntwrk_buf_start = (char *)pSMBr;
d47d7c1a 4663 psrch_inf->smallBuf = 0;
50c2f753
SF
4664 psrch_inf->srch_entries_start =
4665 (char *) &pSMBr->hdr.Protocol +
1da177e4 4666 le16_to_cpu(pSMBr->t2.DataOffset);
1da177e4
LT
4667 parms = (T2_FFIRST_RSP_PARMS *)((char *) &pSMBr->hdr.Protocol +
4668 le16_to_cpu(pSMBr->t2.ParameterOffset));
4669
790fe579 4670 if (parms->EndofSearch)
4b18f2a9 4671 psrch_inf->endOfSearch = true;
1da177e4 4672 else
4b18f2a9 4673 psrch_inf->endOfSearch = false;
1da177e4 4674
50c2f753
SF
4675 psrch_inf->entries_in_buffer =
4676 le16_to_cpu(parms->SearchCount);
60808233 4677 psrch_inf->index_of_last_entry = 2 /* skip . and .. */ +
1da177e4 4678 psrch_inf->entries_in_buffer;
b77d753c 4679 lnoff = le16_to_cpu(parms->LastNameOffset);
c974befa 4680 if (CIFSMaxBufSize < lnoff) {
f96637be 4681 cifs_dbg(VFS, "ignoring corrupt resume name\n");
b77d753c
SF
4682 psrch_inf->last_entry = NULL;
4683 return rc;
4684 }
4685
0752f152 4686 psrch_inf->last_entry = psrch_inf->srch_entries_start +
b77d753c
SF
4687 lnoff;
4688
c052e2b4
SP
4689 if (pnetfid)
4690 *pnetfid = parms->SearchHandle;
1da177e4
LT
4691 } else {
4692 cifs_buf_release(pSMB);
4693 }
4694 }
4695
4696 return rc;
4697}
4698
6d5786a3
PS
4699int CIFSFindNext(const unsigned int xid, struct cifs_tcon *tcon,
4700 __u16 searchHandle, __u16 search_flags,
4701 struct cifs_search_info *psrch_inf)
1da177e4
LT
4702{
4703 TRANSACTION2_FNEXT_REQ *pSMB = NULL;
4704 TRANSACTION2_FNEXT_RSP *pSMBr = NULL;
ad7a2926 4705 T2_FNEXT_RSP_PARMS *parms;
1da177e4
LT
4706 char *response_data;
4707 int rc = 0;
9438fabb
JL
4708 int bytes_returned;
4709 unsigned int name_len;
1da177e4
LT
4710 __u16 params, byte_count;
4711
f96637be 4712 cifs_dbg(FYI, "In FindNext\n");
1da177e4 4713
4b18f2a9 4714 if (psrch_inf->endOfSearch)
1da177e4
LT
4715 return -ENOENT;
4716
4717 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4718 (void **) &pSMBr);
4719 if (rc)
4720 return rc;
4721
50c2f753 4722 params = 14; /* includes 2 bytes of null string, converted to LE below*/
1da177e4
LT
4723 byte_count = 0;
4724 pSMB->TotalDataCount = 0; /* no EAs */
4725 pSMB->MaxParameterCount = cpu_to_le16(8);
c974befa 4726 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
1da177e4
LT
4727 pSMB->MaxSetupCount = 0;
4728 pSMB->Reserved = 0;
4729 pSMB->Flags = 0;
4730 pSMB->Timeout = 0;
4731 pSMB->Reserved2 = 0;
4732 pSMB->ParameterOffset = cpu_to_le16(
4733 offsetof(struct smb_com_transaction2_fnext_req,SearchHandle) - 4);
4734 pSMB->DataCount = 0;
4735 pSMB->DataOffset = 0;
4736 pSMB->SetupCount = 1;
4737 pSMB->Reserved3 = 0;
4738 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_NEXT);
4739 pSMB->SearchHandle = searchHandle; /* always kept as le */
4740 pSMB->SearchCount =
630f3f0c 4741 cpu_to_le16(CIFSMaxBufSize / sizeof(FILE_UNIX_INFO));
1da177e4
LT
4742 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4743 pSMB->ResumeKey = psrch_inf->resume_key;
2608bee7 4744 pSMB->SearchFlags = cpu_to_le16(search_flags);
1da177e4
LT
4745
4746 name_len = psrch_inf->resume_name_len;
4747 params += name_len;
790fe579 4748 if (name_len < PATH_MAX) {
1da177e4
LT
4749 memcpy(pSMB->ResumeFileName, psrch_inf->presume_name, name_len);
4750 byte_count += name_len;
ef6724e3
SF
4751 /* 14 byte parm len above enough for 2 byte null terminator */
4752 pSMB->ResumeFileName[name_len] = 0;
4753 pSMB->ResumeFileName[name_len+1] = 0;
1da177e4
LT
4754 } else {
4755 rc = -EINVAL;
4756 goto FNext2_err_exit;
4757 }
4758 byte_count = params + 1 /* pad */ ;
4759 pSMB->TotalParameterCount = cpu_to_le16(params);
4760 pSMB->ParameterCount = pSMB->TotalParameterCount;
be8e3b00 4761 inc_rfc1001_len(pSMB, byte_count);
1da177e4 4762 pSMB->ByteCount = cpu_to_le16(byte_count);
50c2f753 4763
1da177e4
LT
4764 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4765 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
44c58186 4766 cifs_stats_inc(&tcon->stats.cifs_stats.num_fnext);
1da177e4
LT
4767 if (rc) {
4768 if (rc == -EBADF) {
4b18f2a9 4769 psrch_inf->endOfSearch = true;
6353450a 4770 cifs_buf_release(pSMB);
50c2f753 4771 rc = 0; /* search probably was closed at end of search*/
1da177e4 4772 } else
f96637be 4773 cifs_dbg(FYI, "FindNext returned = %d\n", rc);
1da177e4
LT
4774 } else { /* decode response */
4775 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
50c2f753 4776
790fe579 4777 if (rc == 0) {
b77d753c
SF
4778 unsigned int lnoff;
4779
1da177e4
LT
4780 /* BB fixme add lock for file (srch_info) struct here */
4781 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
4b18f2a9 4782 psrch_inf->unicode = true;
1da177e4 4783 else
4b18f2a9 4784 psrch_inf->unicode = false;
1da177e4
LT
4785 response_data = (char *) &pSMBr->hdr.Protocol +
4786 le16_to_cpu(pSMBr->t2.ParameterOffset);
4787 parms = (T2_FNEXT_RSP_PARMS *)response_data;
4788 response_data = (char *)&pSMBr->hdr.Protocol +
4789 le16_to_cpu(pSMBr->t2.DataOffset);
790fe579 4790 if (psrch_inf->smallBuf)
d47d7c1a
SF
4791 cifs_small_buf_release(
4792 psrch_inf->ntwrk_buf_start);
4793 else
4794 cifs_buf_release(psrch_inf->ntwrk_buf_start);
1da177e4
LT
4795 psrch_inf->srch_entries_start = response_data;
4796 psrch_inf->ntwrk_buf_start = (char *)pSMB;
d47d7c1a 4797 psrch_inf->smallBuf = 0;
790fe579 4798 if (parms->EndofSearch)
4b18f2a9 4799 psrch_inf->endOfSearch = true;
1da177e4 4800 else
4b18f2a9 4801 psrch_inf->endOfSearch = false;
50c2f753
SF
4802 psrch_inf->entries_in_buffer =
4803 le16_to_cpu(parms->SearchCount);
1da177e4
LT
4804 psrch_inf->index_of_last_entry +=
4805 psrch_inf->entries_in_buffer;
b77d753c 4806 lnoff = le16_to_cpu(parms->LastNameOffset);
c974befa 4807 if (CIFSMaxBufSize < lnoff) {
f96637be 4808 cifs_dbg(VFS, "ignoring corrupt resume name\n");
b77d753c
SF
4809 psrch_inf->last_entry = NULL;
4810 return rc;
4811 } else
4812 psrch_inf->last_entry =
4813 psrch_inf->srch_entries_start + lnoff;
4814
f96637be
JP
4815/* cifs_dbg(FYI, "fnxt2 entries in buf %d index_of_last %d\n",
4816 psrch_inf->entries_in_buffer, psrch_inf->index_of_last_entry); */
1da177e4
LT
4817
4818 /* BB fixme add unlock here */
4819 }
4820
4821 }
4822
4823 /* BB On error, should we leave previous search buf (and count and
4824 last entry fields) intact or free the previous one? */
4825
4826 /* Note: On -EAGAIN error only caller can retry on handle based calls
4827 since file handle passed in no longer valid */
4828FNext2_err_exit:
4829 if (rc != 0)
4830 cifs_buf_release(pSMB);
1da177e4
LT
4831 return rc;
4832}
4833
4834int
6d5786a3 4835CIFSFindClose(const unsigned int xid, struct cifs_tcon *tcon,
50c2f753 4836 const __u16 searchHandle)
1da177e4
LT
4837{
4838 int rc = 0;
4839 FINDCLOSE_REQ *pSMB = NULL;
1da177e4 4840
f96637be 4841 cifs_dbg(FYI, "In CIFSSMBFindClose\n");
1da177e4
LT
4842 rc = small_smb_init(SMB_COM_FIND_CLOSE2, 1, tcon, (void **)&pSMB);
4843
4844 /* no sense returning error if session restarted
4845 as file handle has been closed */
790fe579 4846 if (rc == -EAGAIN)
1da177e4
LT
4847 return 0;
4848 if (rc)
4849 return rc;
4850
1da177e4
LT
4851 pSMB->FileID = searchHandle;
4852 pSMB->ByteCount = 0;
792af7b0 4853 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
da502f7d 4854 cifs_small_buf_release(pSMB);
ad7a2926 4855 if (rc)
f96637be 4856 cifs_dbg(VFS, "Send error in FindClose = %d\n", rc);
ad7a2926 4857
44c58186 4858 cifs_stats_inc(&tcon->stats.cifs_stats.num_fclose);
1da177e4
LT
4859
4860 /* Since session is dead, search handle closed on server already */
4861 if (rc == -EAGAIN)
4862 rc = 0;
4863
4864 return rc;
4865}
4866
1da177e4 4867int
6d5786a3 4868CIFSGetSrvInodeNumber(const unsigned int xid, struct cifs_tcon *tcon,
1208ef1f 4869 const char *search_name, __u64 *inode_number,
50c2f753 4870 const struct nls_table *nls_codepage, int remap)
1da177e4
LT
4871{
4872 int rc = 0;
4873 TRANSACTION2_QPI_REQ *pSMB = NULL;
4874 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4875 int name_len, bytes_returned;
4876 __u16 params, byte_count;
4877
f96637be 4878 cifs_dbg(FYI, "In GetSrvInodeNum for %s\n", search_name);
790fe579 4879 if (tcon == NULL)
50c2f753 4880 return -ENODEV;
1da177e4
LT
4881
4882GetInodeNumberRetry:
4883 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
50c2f753 4884 (void **) &pSMBr);
1da177e4
LT
4885 if (rc)
4886 return rc;
4887
1da177e4
LT
4888 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4889 name_len =
acbbb76a 4890 cifsConvertToUTF16((__le16 *) pSMB->FileName,
1208ef1f 4891 search_name, PATH_MAX, nls_codepage,
acbbb76a 4892 remap);
1da177e4
LT
4893 name_len++; /* trailing null */
4894 name_len *= 2;
50c2f753 4895 } else { /* BB improve the check for buffer overruns BB */
1208ef1f 4896 name_len = strnlen(search_name, PATH_MAX);
1da177e4 4897 name_len++; /* trailing null */
1208ef1f 4898 strncpy(pSMB->FileName, search_name, name_len);
1da177e4
LT
4899 }
4900
4901 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
4902 pSMB->TotalDataCount = 0;
4903 pSMB->MaxParameterCount = cpu_to_le16(2);
4904 /* BB find exact max data count below from sess structure BB */
4905 pSMB->MaxDataCount = cpu_to_le16(4000);
4906 pSMB->MaxSetupCount = 0;
4907 pSMB->Reserved = 0;
4908 pSMB->Flags = 0;
4909 pSMB->Timeout = 0;
4910 pSMB->Reserved2 = 0;
4911 pSMB->ParameterOffset = cpu_to_le16(offsetof(
50c2f753 4912 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
1da177e4
LT
4913 pSMB->DataCount = 0;
4914 pSMB->DataOffset = 0;
4915 pSMB->SetupCount = 1;
4916 pSMB->Reserved3 = 0;
4917 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4918 byte_count = params + 1 /* pad */ ;
4919 pSMB->TotalParameterCount = cpu_to_le16(params);
4920 pSMB->ParameterCount = pSMB->TotalParameterCount;
4921 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_INTERNAL_INFO);
4922 pSMB->Reserved4 = 0;
be8e3b00 4923 inc_rfc1001_len(pSMB, byte_count);
1da177e4
LT
4924 pSMB->ByteCount = cpu_to_le16(byte_count);
4925
4926 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4927 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4928 if (rc) {
f96637be 4929 cifs_dbg(FYI, "error %d in QueryInternalInfo\n", rc);
1da177e4
LT
4930 } else {
4931 /* decode response */
4932 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
1da177e4 4933 /* BB also check enough total bytes returned */
820a803f 4934 if (rc || get_bcc(&pSMBr->hdr) < 2)
1da177e4
LT
4935 /* If rc should we check for EOPNOSUPP and
4936 disable the srvino flag? or in caller? */
4937 rc = -EIO; /* bad smb */
50c2f753 4938 else {
1da177e4
LT
4939 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4940 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
50c2f753 4941 struct file_internal_info *pfinfo;
1da177e4 4942 /* BB Do we need a cast or hash here ? */
790fe579 4943 if (count < 8) {
f96637be 4944 cifs_dbg(FYI, "Illegal size ret in QryIntrnlInf\n");
1da177e4
LT
4945 rc = -EIO;
4946 goto GetInodeNumOut;
4947 }
4948 pfinfo = (struct file_internal_info *)
4949 (data_offset + (char *) &pSMBr->hdr.Protocol);
85a6dac5 4950 *inode_number = le64_to_cpu(pfinfo->UniqueId);
1da177e4
LT
4951 }
4952 }
4953GetInodeNumOut:
4954 cifs_buf_release(pSMB);
4955 if (rc == -EAGAIN)
4956 goto GetInodeNumberRetry;
4957 return rc;
4958}
1da177e4
LT
4959
4960int
6d5786a3 4961CIFSGetDFSRefer(const unsigned int xid, struct cifs_ses *ses,
b669f33c 4962 const char *search_name, struct dfs_info3_param **target_nodes,
c2cf07d5 4963 unsigned int *num_of_nodes,
737b758c 4964 const struct nls_table *nls_codepage, int remap)
1da177e4
LT
4965{
4966/* TRANS2_GET_DFS_REFERRAL */
4967 TRANSACTION2_GET_DFS_REFER_REQ *pSMB = NULL;
4968 TRANSACTION2_GET_DFS_REFER_RSP *pSMBr = NULL;
1da177e4
LT
4969 int rc = 0;
4970 int bytes_returned;
4971 int name_len;
1da177e4 4972 __u16 params, byte_count;
c2cf07d5
SF
4973 *num_of_nodes = 0;
4974 *target_nodes = NULL;
1da177e4 4975
f96637be 4976 cifs_dbg(FYI, "In GetDFSRefer the path %s\n", search_name);
b327a717 4977 if (ses == NULL || ses->tcon_ipc == NULL)
1da177e4 4978 return -ENODEV;
b327a717 4979
1da177e4 4980getDFSRetry:
b327a717 4981 rc = smb_init(SMB_COM_TRANSACTION2, 15, ses->tcon_ipc, (void **) &pSMB,
1da177e4
LT
4982 (void **) &pSMBr);
4983 if (rc)
4984 return rc;
50c2f753
SF
4985
4986 /* server pointer checked in called function,
1982c344 4987 but should never be null here anyway */
88257360 4988 pSMB->hdr.Mid = get_next_mid(ses->server);
b327a717 4989 pSMB->hdr.Tid = ses->tcon_ipc->tid;
1da177e4 4990 pSMB->hdr.Uid = ses->Suid;
26f57364 4991 if (ses->capabilities & CAP_STATUS32)
1da177e4 4992 pSMB->hdr.Flags2 |= SMBFLG2_ERR_STATUS;
26f57364 4993 if (ses->capabilities & CAP_DFS)
1da177e4 4994 pSMB->hdr.Flags2 |= SMBFLG2_DFS;
1da177e4
LT
4995
4996 if (ses->capabilities & CAP_UNICODE) {
4997 pSMB->hdr.Flags2 |= SMBFLG2_UNICODE;
4998 name_len =
acbbb76a 4999 cifsConvertToUTF16((__le16 *) pSMB->RequestFileName,
b669f33c 5000 search_name, PATH_MAX, nls_codepage,
acbbb76a 5001 remap);
1da177e4
LT
5002 name_len++; /* trailing null */
5003 name_len *= 2;
50c2f753 5004 } else { /* BB improve the check for buffer overruns BB */
b669f33c 5005 name_len = strnlen(search_name, PATH_MAX);
1da177e4 5006 name_len++; /* trailing null */
b669f33c 5007 strncpy(pSMB->RequestFileName, search_name, name_len);
1da177e4
LT
5008 }
5009
65c3b205 5010 if (ses->server->sign)
38d77c50 5011 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
1a4e15a0 5012
50c2f753 5013 pSMB->hdr.Uid = ses->Suid;
1a4e15a0 5014
1da177e4
LT
5015 params = 2 /* level */ + name_len /*includes null */ ;
5016 pSMB->TotalDataCount = 0;
5017 pSMB->DataCount = 0;
5018 pSMB->DataOffset = 0;
5019 pSMB->MaxParameterCount = 0;
582d21e5
SF
5020 /* BB find exact max SMB PDU from sess structure BB */
5021 pSMB->MaxDataCount = cpu_to_le16(4000);
1da177e4
LT
5022 pSMB->MaxSetupCount = 0;
5023 pSMB->Reserved = 0;
5024 pSMB->Flags = 0;
5025 pSMB->Timeout = 0;
5026 pSMB->Reserved2 = 0;
5027 pSMB->ParameterOffset = cpu_to_le16(offsetof(
50c2f753 5028 struct smb_com_transaction2_get_dfs_refer_req, MaxReferralLevel) - 4);
1da177e4
LT
5029 pSMB->SetupCount = 1;
5030 pSMB->Reserved3 = 0;
5031 pSMB->SubCommand = cpu_to_le16(TRANS2_GET_DFS_REFERRAL);
5032 byte_count = params + 3 /* pad */ ;
5033 pSMB->ParameterCount = cpu_to_le16(params);
5034 pSMB->TotalParameterCount = pSMB->ParameterCount;
5035 pSMB->MaxReferralLevel = cpu_to_le16(3);
be8e3b00 5036 inc_rfc1001_len(pSMB, byte_count);
1da177e4
LT
5037 pSMB->ByteCount = cpu_to_le16(byte_count);
5038
5039 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
5040 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5041 if (rc) {
f96637be 5042 cifs_dbg(FYI, "Send error in GetDFSRefer = %d\n", rc);
c2cf07d5
SF
5043 goto GetDFSRefExit;
5044 }
5045 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
1da177e4 5046
c2cf07d5 5047 /* BB Also check if enough total bytes returned? */
820a803f 5048 if (rc || get_bcc(&pSMBr->hdr) < 17) {
c2cf07d5 5049 rc = -EIO; /* bad smb */
fec4585f
IM
5050 goto GetDFSRefExit;
5051 }
c2cf07d5 5052
f96637be
JP
5053 cifs_dbg(FYI, "Decoding GetDFSRefer response BCC: %d Offset %d\n",
5054 get_bcc(&pSMBr->hdr), le16_to_cpu(pSMBr->t2.DataOffset));
1da177e4 5055
fec4585f 5056 /* parse returned result into more usable form */
4ecce920
AA
5057 rc = parse_dfs_referrals(&pSMBr->dfs_data,
5058 le16_to_cpu(pSMBr->t2.DataCount),
5059 num_of_nodes, target_nodes, nls_codepage,
5060 remap, search_name,
284316dd 5061 (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE) != 0);
c2cf07d5 5062
1da177e4 5063GetDFSRefExit:
0d817bc0 5064 cifs_buf_release(pSMB);
1da177e4
LT
5065
5066 if (rc == -EAGAIN)
5067 goto getDFSRetry;
5068
5069 return rc;
5070}
5071
20962438
SF
5072/* Query File System Info such as free space to old servers such as Win 9x */
5073int
6d5786a3
PS
5074SMBOldQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
5075 struct kstatfs *FSData)
20962438
SF
5076{
5077/* level 0x01 SMB_QUERY_FILE_SYSTEM_INFO */
5078 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5079 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5080 FILE_SYSTEM_ALLOC_INFO *response_data;
5081 int rc = 0;
5082 int bytes_returned = 0;
5083 __u16 params, byte_count;
5084
f96637be 5085 cifs_dbg(FYI, "OldQFSInfo\n");
20962438
SF
5086oldQFSInfoRetry:
5087 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5088 (void **) &pSMBr);
5089 if (rc)
5090 return rc;
20962438
SF
5091
5092 params = 2; /* level */
5093 pSMB->TotalDataCount = 0;
5094 pSMB->MaxParameterCount = cpu_to_le16(2);
5095 pSMB->MaxDataCount = cpu_to_le16(1000);
5096 pSMB->MaxSetupCount = 0;
5097 pSMB->Reserved = 0;
5098 pSMB->Flags = 0;
5099 pSMB->Timeout = 0;
5100 pSMB->Reserved2 = 0;
5101 byte_count = params + 1 /* pad */ ;
5102 pSMB->TotalParameterCount = cpu_to_le16(params);
5103 pSMB->ParameterCount = pSMB->TotalParameterCount;
5104 pSMB->ParameterOffset = cpu_to_le16(offsetof(
5105 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
5106 pSMB->DataCount = 0;
5107 pSMB->DataOffset = 0;
5108 pSMB->SetupCount = 1;
5109 pSMB->Reserved3 = 0;
5110 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5111 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_ALLOCATION);
be8e3b00 5112 inc_rfc1001_len(pSMB, byte_count);
20962438
SF
5113 pSMB->ByteCount = cpu_to_le16(byte_count);
5114
5115 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5116 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5117 if (rc) {
f96637be 5118 cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
20962438
SF
5119 } else { /* decode response */
5120 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5121
820a803f 5122 if (rc || get_bcc(&pSMBr->hdr) < 18)
20962438
SF
5123 rc = -EIO; /* bad smb */
5124 else {
5125 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
f96637be 5126 cifs_dbg(FYI, "qfsinf resp BCC: %d Offset %d\n",
820a803f 5127 get_bcc(&pSMBr->hdr), data_offset);
20962438 5128
50c2f753 5129 response_data = (FILE_SYSTEM_ALLOC_INFO *)
20962438
SF
5130 (((char *) &pSMBr->hdr.Protocol) + data_offset);
5131 FSData->f_bsize =
5132 le16_to_cpu(response_data->BytesPerSector) *
5133 le32_to_cpu(response_data->
5134 SectorsPerAllocationUnit);
5a519bea
SF
5135 /*
5136 * much prefer larger but if server doesn't report
5137 * a valid size than 4K is a reasonable minimum
5138 */
5139 if (FSData->f_bsize < 512)
5140 FSData->f_bsize = 4096;
5141
20962438 5142 FSData->f_blocks =
50c2f753 5143 le32_to_cpu(response_data->TotalAllocationUnits);
20962438
SF
5144 FSData->f_bfree = FSData->f_bavail =
5145 le32_to_cpu(response_data->FreeAllocationUnits);
f96637be
JP
5146 cifs_dbg(FYI, "Blocks: %lld Free: %lld Block size %ld\n",
5147 (unsigned long long)FSData->f_blocks,
5148 (unsigned long long)FSData->f_bfree,
5149 FSData->f_bsize);
20962438
SF
5150 }
5151 }
5152 cifs_buf_release(pSMB);
5153
5154 if (rc == -EAGAIN)
5155 goto oldQFSInfoRetry;
5156
5157 return rc;
5158}
5159
1da177e4 5160int
6d5786a3
PS
5161CIFSSMBQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
5162 struct kstatfs *FSData)
1da177e4
LT
5163{
5164/* level 0x103 SMB_QUERY_FILE_SYSTEM_INFO */
5165 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5166 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5167 FILE_SYSTEM_INFO *response_data;
5168 int rc = 0;
5169 int bytes_returned = 0;
5170 __u16 params, byte_count;
5171
f96637be 5172 cifs_dbg(FYI, "In QFSInfo\n");
1da177e4
LT
5173QFSInfoRetry:
5174 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5175 (void **) &pSMBr);
5176 if (rc)
5177 return rc;
5178
5179 params = 2; /* level */
5180 pSMB->TotalDataCount = 0;
5181 pSMB->MaxParameterCount = cpu_to_le16(2);
20962438 5182 pSMB->MaxDataCount = cpu_to_le16(1000);
1da177e4
LT
5183 pSMB->MaxSetupCount = 0;
5184 pSMB->Reserved = 0;
5185 pSMB->Flags = 0;
5186 pSMB->Timeout = 0;
5187 pSMB->Reserved2 = 0;
5188 byte_count = params + 1 /* pad */ ;
5189 pSMB->TotalParameterCount = cpu_to_le16(params);
5190 pSMB->ParameterCount = pSMB->TotalParameterCount;
5191 pSMB->ParameterOffset = cpu_to_le16(offsetof(
50c2f753 5192 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
1da177e4
LT
5193 pSMB->DataCount = 0;
5194 pSMB->DataOffset = 0;
5195 pSMB->SetupCount = 1;
5196 pSMB->Reserved3 = 0;
5197 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5198 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_SIZE_INFO);
be8e3b00 5199 inc_rfc1001_len(pSMB, byte_count);
1da177e4
LT
5200 pSMB->ByteCount = cpu_to_le16(byte_count);
5201
5202 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5203 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5204 if (rc) {
f96637be 5205 cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
1da177e4 5206 } else { /* decode response */
50c2f753 5207 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
1da177e4 5208
820a803f 5209 if (rc || get_bcc(&pSMBr->hdr) < 24)
1da177e4
LT
5210 rc = -EIO; /* bad smb */
5211 else {
5212 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
1da177e4
LT
5213
5214 response_data =
5215 (FILE_SYSTEM_INFO
5216 *) (((char *) &pSMBr->hdr.Protocol) +
5217 data_offset);
5218 FSData->f_bsize =
5219 le32_to_cpu(response_data->BytesPerSector) *
5220 le32_to_cpu(response_data->
5221 SectorsPerAllocationUnit);
5a519bea
SF
5222 /*
5223 * much prefer larger but if server doesn't report
5224 * a valid size than 4K is a reasonable minimum
5225 */
5226 if (FSData->f_bsize < 512)
5227 FSData->f_bsize = 4096;
5228
1da177e4
LT
5229 FSData->f_blocks =
5230 le64_to_cpu(response_data->TotalAllocationUnits);
5231 FSData->f_bfree = FSData->f_bavail =
5232 le64_to_cpu(response_data->FreeAllocationUnits);
f96637be
JP
5233 cifs_dbg(FYI, "Blocks: %lld Free: %lld Block size %ld\n",
5234 (unsigned long long)FSData->f_blocks,
5235 (unsigned long long)FSData->f_bfree,
5236 FSData->f_bsize);
1da177e4
LT
5237 }
5238 }
5239 cifs_buf_release(pSMB);
5240
5241 if (rc == -EAGAIN)
5242 goto QFSInfoRetry;
5243
5244 return rc;
5245}
5246
5247int
6d5786a3 5248CIFSSMBQFSAttributeInfo(const unsigned int xid, struct cifs_tcon *tcon)
1da177e4
LT
5249{
5250/* level 0x105 SMB_QUERY_FILE_SYSTEM_INFO */
5251 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5252 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5253 FILE_SYSTEM_ATTRIBUTE_INFO *response_data;
5254 int rc = 0;
5255 int bytes_returned = 0;
5256 __u16 params, byte_count;
5257
f96637be 5258 cifs_dbg(FYI, "In QFSAttributeInfo\n");
1da177e4
LT
5259QFSAttributeRetry:
5260 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5261 (void **) &pSMBr);
5262 if (rc)
5263 return rc;
5264
5265 params = 2; /* level */
5266 pSMB->TotalDataCount = 0;
5267 pSMB->MaxParameterCount = cpu_to_le16(2);
582d21e5
SF
5268 /* BB find exact max SMB PDU from sess structure BB */
5269 pSMB->MaxDataCount = cpu_to_le16(1000);
1da177e4
LT
5270 pSMB->MaxSetupCount = 0;
5271 pSMB->Reserved = 0;
5272 pSMB->Flags = 0;
5273 pSMB->Timeout = 0;
5274 pSMB->Reserved2 = 0;
5275 byte_count = params + 1 /* pad */ ;
5276 pSMB->TotalParameterCount = cpu_to_le16(params);
5277 pSMB->ParameterCount = pSMB->TotalParameterCount;
5278 pSMB->ParameterOffset = cpu_to_le16(offsetof(
50c2f753 5279 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
1da177e4
LT
5280 pSMB->DataCount = 0;
5281 pSMB->DataOffset = 0;
5282 pSMB->SetupCount = 1;
5283 pSMB->Reserved3 = 0;
5284 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5285 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_ATTRIBUTE_INFO);
be8e3b00 5286 inc_rfc1001_len(pSMB, byte_count);
1da177e4
LT
5287 pSMB->ByteCount = cpu_to_le16(byte_count);
5288
5289 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5290 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5291 if (rc) {
f96637be 5292 cifs_dbg(VFS, "Send error in QFSAttributeInfo = %d\n", rc);
1da177e4
LT
5293 } else { /* decode response */
5294 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5295
820a803f 5296 if (rc || get_bcc(&pSMBr->hdr) < 13) {
50c2f753 5297 /* BB also check if enough bytes returned */
1da177e4
LT
5298 rc = -EIO; /* bad smb */
5299 } else {
5300 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5301 response_data =
5302 (FILE_SYSTEM_ATTRIBUTE_INFO
5303 *) (((char *) &pSMBr->hdr.Protocol) +
5304 data_offset);
5305 memcpy(&tcon->fsAttrInfo, response_data,
26f57364 5306 sizeof(FILE_SYSTEM_ATTRIBUTE_INFO));
1da177e4
LT
5307 }
5308 }
5309 cifs_buf_release(pSMB);
5310
5311 if (rc == -EAGAIN)
5312 goto QFSAttributeRetry;
5313
5314 return rc;
5315}
5316
5317int
6d5786a3 5318CIFSSMBQFSDeviceInfo(const unsigned int xid, struct cifs_tcon *tcon)
1da177e4
LT
5319{
5320/* level 0x104 SMB_QUERY_FILE_SYSTEM_INFO */
5321 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5322 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5323 FILE_SYSTEM_DEVICE_INFO *response_data;
5324 int rc = 0;
5325 int bytes_returned = 0;
5326 __u16 params, byte_count;
5327
f96637be 5328 cifs_dbg(FYI, "In QFSDeviceInfo\n");
1da177e4
LT
5329QFSDeviceRetry:
5330 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5331 (void **) &pSMBr);
5332 if (rc)
5333 return rc;
5334
5335 params = 2; /* level */
5336 pSMB->TotalDataCount = 0;
5337 pSMB->MaxParameterCount = cpu_to_le16(2);
582d21e5
SF
5338 /* BB find exact max SMB PDU from sess structure BB */
5339 pSMB->MaxDataCount = cpu_to_le16(1000);
1da177e4
LT
5340 pSMB->MaxSetupCount = 0;
5341 pSMB->Reserved = 0;
5342 pSMB->Flags = 0;
5343 pSMB->Timeout = 0;
5344 pSMB->Reserved2 = 0;
5345 byte_count = params + 1 /* pad */ ;
5346 pSMB->TotalParameterCount = cpu_to_le16(params);
5347 pSMB->ParameterCount = pSMB->TotalParameterCount;
5348 pSMB->ParameterOffset = cpu_to_le16(offsetof(
50c2f753 5349 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
1da177e4
LT
5350
5351 pSMB->DataCount = 0;
5352 pSMB->DataOffset = 0;
5353 pSMB->SetupCount = 1;
5354 pSMB->Reserved3 = 0;
5355 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5356 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_DEVICE_INFO);
be8e3b00 5357 inc_rfc1001_len(pSMB, byte_count);
1da177e4
LT
5358 pSMB->ByteCount = cpu_to_le16(byte_count);
5359
5360 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5361 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5362 if (rc) {
f96637be 5363 cifs_dbg(FYI, "Send error in QFSDeviceInfo = %d\n", rc);
1da177e4
LT
5364 } else { /* decode response */
5365 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5366
820a803f
JL
5367 if (rc || get_bcc(&pSMBr->hdr) <
5368 sizeof(FILE_SYSTEM_DEVICE_INFO))
1da177e4
LT
5369 rc = -EIO; /* bad smb */
5370 else {
5371 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5372 response_data =
737b758c
SF
5373 (FILE_SYSTEM_DEVICE_INFO *)
5374 (((char *) &pSMBr->hdr.Protocol) +
1da177e4
LT
5375 data_offset);
5376 memcpy(&tcon->fsDevInfo, response_data,
26f57364 5377 sizeof(FILE_SYSTEM_DEVICE_INFO));
1da177e4
LT
5378 }
5379 }
5380 cifs_buf_release(pSMB);
5381
5382 if (rc == -EAGAIN)
5383 goto QFSDeviceRetry;
5384
5385 return rc;
5386}
5387
5388int
6d5786a3 5389CIFSSMBQFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon)
1da177e4
LT
5390{
5391/* level 0x200 SMB_QUERY_CIFS_UNIX_INFO */
5392 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5393 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5394 FILE_SYSTEM_UNIX_INFO *response_data;
5395 int rc = 0;
5396 int bytes_returned = 0;
5397 __u16 params, byte_count;
5398
f96637be 5399 cifs_dbg(FYI, "In QFSUnixInfo\n");
1da177e4 5400QFSUnixRetry:
f569599a
JL
5401 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5402 (void **) &pSMB, (void **) &pSMBr);
1da177e4
LT
5403 if (rc)
5404 return rc;
5405
5406 params = 2; /* level */
5407 pSMB->TotalDataCount = 0;
5408 pSMB->DataCount = 0;
5409 pSMB->DataOffset = 0;
5410 pSMB->MaxParameterCount = cpu_to_le16(2);
582d21e5
SF
5411 /* BB find exact max SMB PDU from sess structure BB */
5412 pSMB->MaxDataCount = cpu_to_le16(100);
1da177e4
LT
5413 pSMB->MaxSetupCount = 0;
5414 pSMB->Reserved = 0;
5415 pSMB->Flags = 0;
5416 pSMB->Timeout = 0;
5417 pSMB->Reserved2 = 0;
5418 byte_count = params + 1 /* pad */ ;
5419 pSMB->ParameterCount = cpu_to_le16(params);
5420 pSMB->TotalParameterCount = pSMB->ParameterCount;
50c2f753
SF
5421 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5422 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
1da177e4
LT
5423 pSMB->SetupCount = 1;
5424 pSMB->Reserved3 = 0;
5425 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5426 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_CIFS_UNIX_INFO);
be8e3b00 5427 inc_rfc1001_len(pSMB, byte_count);
1da177e4
LT
5428 pSMB->ByteCount = cpu_to_le16(byte_count);
5429
5430 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5431 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5432 if (rc) {
f96637be 5433 cifs_dbg(VFS, "Send error in QFSUnixInfo = %d\n", rc);
1da177e4
LT
5434 } else { /* decode response */
5435 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5436
820a803f 5437 if (rc || get_bcc(&pSMBr->hdr) < 13) {
1da177e4
LT
5438 rc = -EIO; /* bad smb */
5439 } else {
5440 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5441 response_data =
5442 (FILE_SYSTEM_UNIX_INFO
5443 *) (((char *) &pSMBr->hdr.Protocol) +
5444 data_offset);
5445 memcpy(&tcon->fsUnixInfo, response_data,
26f57364 5446 sizeof(FILE_SYSTEM_UNIX_INFO));
1da177e4
LT
5447 }
5448 }
5449 cifs_buf_release(pSMB);
5450
5451 if (rc == -EAGAIN)
5452 goto QFSUnixRetry;
5453
5454
5455 return rc;
5456}
5457
ac67055e 5458int
6d5786a3 5459CIFSSMBSetFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon, __u64 cap)
ac67055e
JA
5460{
5461/* level 0x200 SMB_SET_CIFS_UNIX_INFO */
5462 TRANSACTION2_SETFSI_REQ *pSMB = NULL;
5463 TRANSACTION2_SETFSI_RSP *pSMBr = NULL;
5464 int rc = 0;
5465 int bytes_returned = 0;
5466 __u16 params, param_offset, offset, byte_count;
5467
f96637be 5468 cifs_dbg(FYI, "In SETFSUnixInfo\n");
ac67055e 5469SETFSUnixRetry:
f26282c9 5470 /* BB switch to small buf init to save memory */
f569599a
JL
5471 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5472 (void **) &pSMB, (void **) &pSMBr);
ac67055e
JA
5473 if (rc)
5474 return rc;
5475
5476 params = 4; /* 2 bytes zero followed by info level. */
5477 pSMB->MaxSetupCount = 0;
5478 pSMB->Reserved = 0;
5479 pSMB->Flags = 0;
5480 pSMB->Timeout = 0;
5481 pSMB->Reserved2 = 0;
50c2f753
SF
5482 param_offset = offsetof(struct smb_com_transaction2_setfsi_req, FileNum)
5483 - 4;
ac67055e
JA
5484 offset = param_offset + params;
5485
5486 pSMB->MaxParameterCount = cpu_to_le16(4);
582d21e5
SF
5487 /* BB find exact max SMB PDU from sess structure BB */
5488 pSMB->MaxDataCount = cpu_to_le16(100);
ac67055e
JA
5489 pSMB->SetupCount = 1;
5490 pSMB->Reserved3 = 0;
5491 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FS_INFORMATION);
5492 byte_count = 1 /* pad */ + params + 12;
5493
5494 pSMB->DataCount = cpu_to_le16(12);
5495 pSMB->ParameterCount = cpu_to_le16(params);
5496 pSMB->TotalDataCount = pSMB->DataCount;
5497 pSMB->TotalParameterCount = pSMB->ParameterCount;
5498 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5499 pSMB->DataOffset = cpu_to_le16(offset);
5500
5501 /* Params. */
5502 pSMB->FileNum = 0;
5503 pSMB->InformationLevel = cpu_to_le16(SMB_SET_CIFS_UNIX_INFO);
5504
5505 /* Data. */
5506 pSMB->ClientUnixMajor = cpu_to_le16(CIFS_UNIX_MAJOR_VERSION);
5507 pSMB->ClientUnixMinor = cpu_to_le16(CIFS_UNIX_MINOR_VERSION);
5508 pSMB->ClientUnixCap = cpu_to_le64(cap);
5509
be8e3b00 5510 inc_rfc1001_len(pSMB, byte_count);
ac67055e
JA
5511 pSMB->ByteCount = cpu_to_le16(byte_count);
5512
5513 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5514 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5515 if (rc) {
f96637be 5516 cifs_dbg(VFS, "Send error in SETFSUnixInfo = %d\n", rc);
ac67055e
JA
5517 } else { /* decode response */
5518 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
ad7a2926 5519 if (rc)
ac67055e 5520 rc = -EIO; /* bad smb */
ac67055e
JA
5521 }
5522 cifs_buf_release(pSMB);
5523
5524 if (rc == -EAGAIN)
5525 goto SETFSUnixRetry;
5526
5527 return rc;
5528}
5529
5530
1da177e4
LT
5531
5532int
6d5786a3 5533CIFSSMBQFSPosixInfo(const unsigned int xid, struct cifs_tcon *tcon,
737b758c 5534 struct kstatfs *FSData)
1da177e4
LT
5535{
5536/* level 0x201 SMB_QUERY_CIFS_POSIX_INFO */
5537 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5538 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5539 FILE_SYSTEM_POSIX_INFO *response_data;
5540 int rc = 0;
5541 int bytes_returned = 0;
5542 __u16 params, byte_count;
5543
f96637be 5544 cifs_dbg(FYI, "In QFSPosixInfo\n");
1da177e4
LT
5545QFSPosixRetry:
5546 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5547 (void **) &pSMBr);
5548 if (rc)
5549 return rc;
5550
5551 params = 2; /* level */
5552 pSMB->TotalDataCount = 0;
5553 pSMB->DataCount = 0;
5554 pSMB->DataOffset = 0;
5555 pSMB->MaxParameterCount = cpu_to_le16(2);
582d21e5
SF
5556 /* BB find exact max SMB PDU from sess structure BB */
5557 pSMB->MaxDataCount = cpu_to_le16(100);
1da177e4
LT
5558 pSMB->MaxSetupCount = 0;
5559 pSMB->Reserved = 0;
5560 pSMB->Flags = 0;
5561 pSMB->Timeout = 0;
5562 pSMB->Reserved2 = 0;
5563 byte_count = params + 1 /* pad */ ;
5564 pSMB->ParameterCount = cpu_to_le16(params);
5565 pSMB->TotalParameterCount = pSMB->ParameterCount;
50c2f753
SF
5566 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5567 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
1da177e4
LT
5568 pSMB->SetupCount = 1;
5569 pSMB->Reserved3 = 0;
5570 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5571 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_FS_INFO);
be8e3b00 5572 inc_rfc1001_len(pSMB, byte_count);
1da177e4
LT
5573 pSMB->ByteCount = cpu_to_le16(byte_count);
5574
5575 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5576 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5577 if (rc) {
f96637be 5578 cifs_dbg(FYI, "Send error in QFSUnixInfo = %d\n", rc);
1da177e4
LT
5579 } else { /* decode response */
5580 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5581
820a803f 5582 if (rc || get_bcc(&pSMBr->hdr) < 13) {
1da177e4
LT
5583 rc = -EIO; /* bad smb */
5584 } else {
5585 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5586 response_data =
5587 (FILE_SYSTEM_POSIX_INFO
5588 *) (((char *) &pSMBr->hdr.Protocol) +
5589 data_offset);
5590 FSData->f_bsize =
5591 le32_to_cpu(response_data->BlockSize);
5a519bea
SF
5592 /*
5593 * much prefer larger but if server doesn't report
5594 * a valid size than 4K is a reasonable minimum
5595 */
5596 if (FSData->f_bsize < 512)
5597 FSData->f_bsize = 4096;
5598
1da177e4
LT
5599 FSData->f_blocks =
5600 le64_to_cpu(response_data->TotalBlocks);
5601 FSData->f_bfree =
5602 le64_to_cpu(response_data->BlocksAvail);
790fe579 5603 if (response_data->UserBlocksAvail == cpu_to_le64(-1)) {
1da177e4
LT
5604 FSData->f_bavail = FSData->f_bfree;
5605 } else {
5606 FSData->f_bavail =
50c2f753 5607 le64_to_cpu(response_data->UserBlocksAvail);
1da177e4 5608 }
790fe579 5609 if (response_data->TotalFileNodes != cpu_to_le64(-1))
1da177e4 5610 FSData->f_files =
50c2f753 5611 le64_to_cpu(response_data->TotalFileNodes);
790fe579 5612 if (response_data->FreeFileNodes != cpu_to_le64(-1))
1da177e4 5613 FSData->f_ffree =
50c2f753 5614 le64_to_cpu(response_data->FreeFileNodes);
1da177e4
LT
5615 }
5616 }
5617 cifs_buf_release(pSMB);
5618
5619 if (rc == -EAGAIN)
5620 goto QFSPosixRetry;
5621
5622 return rc;
5623}
5624
5625
d1433418
PS
5626/*
5627 * We can not use write of zero bytes trick to set file size due to need for
5628 * large file support. Also note that this SetPathInfo is preferred to
5629 * SetFileInfo based method in next routine which is only needed to work around
5630 * a sharing violation bugin Samba which this routine can run into.
5631 */
1da177e4 5632int
6d5786a3 5633CIFSSMBSetEOF(const unsigned int xid, struct cifs_tcon *tcon,
d1433418
PS
5634 const char *file_name, __u64 size, struct cifs_sb_info *cifs_sb,
5635 bool set_allocation)
1da177e4
LT
5636{
5637 struct smb_com_transaction2_spi_req *pSMB = NULL;
5638 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
5639 struct file_end_of_file_info *parm_data;
5640 int name_len;
5641 int rc = 0;
5642 int bytes_returned = 0;
2baa2682 5643 int remap = cifs_remap(cifs_sb);
d1433418 5644
1da177e4
LT
5645 __u16 params, byte_count, data_count, param_offset, offset;
5646
f96637be 5647 cifs_dbg(FYI, "In SetEOF\n");
1da177e4
LT
5648SetEOFRetry:
5649 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5650 (void **) &pSMBr);
5651 if (rc)
5652 return rc;
5653
5654 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5655 name_len =
d1433418
PS
5656 cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
5657 PATH_MAX, cifs_sb->local_nls, remap);
1da177e4
LT
5658 name_len++; /* trailing null */
5659 name_len *= 2;
3e87d803 5660 } else { /* BB improve the check for buffer overruns BB */
d1433418 5661 name_len = strnlen(file_name, PATH_MAX);
1da177e4 5662 name_len++; /* trailing null */
d1433418 5663 strncpy(pSMB->FileName, file_name, name_len);
1da177e4
LT
5664 }
5665 params = 6 + name_len;
26f57364 5666 data_count = sizeof(struct file_end_of_file_info);
1da177e4 5667 pSMB->MaxParameterCount = cpu_to_le16(2);
3e87d803 5668 pSMB->MaxDataCount = cpu_to_le16(4100);
1da177e4
LT
5669 pSMB->MaxSetupCount = 0;
5670 pSMB->Reserved = 0;
5671 pSMB->Flags = 0;
5672 pSMB->Timeout = 0;
5673 pSMB->Reserved2 = 0;
5674 param_offset = offsetof(struct smb_com_transaction2_spi_req,
50c2f753 5675 InformationLevel) - 4;
1da177e4 5676 offset = param_offset + params;
d1433418 5677 if (set_allocation) {
50c2f753
SF
5678 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5679 pSMB->InformationLevel =
5680 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5681 else
5682 pSMB->InformationLevel =
5683 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
5684 } else /* Set File Size */ {
1da177e4
LT
5685 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5686 pSMB->InformationLevel =
50c2f753 5687 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
1da177e4
LT
5688 else
5689 pSMB->InformationLevel =
50c2f753 5690 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
1da177e4
LT
5691 }
5692
5693 parm_data =
5694 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol) +
5695 offset);
5696 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5697 pSMB->DataOffset = cpu_to_le16(offset);
5698 pSMB->SetupCount = 1;
5699 pSMB->Reserved3 = 0;
5700 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5701 byte_count = 3 /* pad */ + params + data_count;
5702 pSMB->DataCount = cpu_to_le16(data_count);
5703 pSMB->TotalDataCount = pSMB->DataCount;
5704 pSMB->ParameterCount = cpu_to_le16(params);
5705 pSMB->TotalParameterCount = pSMB->ParameterCount;
5706 pSMB->Reserved4 = 0;
be8e3b00 5707 inc_rfc1001_len(pSMB, byte_count);
1da177e4
LT
5708 parm_data->FileSize = cpu_to_le64(size);
5709 pSMB->ByteCount = cpu_to_le16(byte_count);
5710 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5711 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
ad7a2926 5712 if (rc)
f96637be 5713 cifs_dbg(FYI, "SetPathInfo (file size) returned %d\n", rc);
1da177e4
LT
5714
5715 cifs_buf_release(pSMB);
5716
5717 if (rc == -EAGAIN)
5718 goto SetEOFRetry;
5719
5720 return rc;
5721}
5722
5723int
d1433418
PS
5724CIFSSMBSetFileSize(const unsigned int xid, struct cifs_tcon *tcon,
5725 struct cifsFileInfo *cfile, __u64 size, bool set_allocation)
1da177e4
LT
5726{
5727 struct smb_com_transaction2_sfi_req *pSMB = NULL;
1da177e4
LT
5728 struct file_end_of_file_info *parm_data;
5729 int rc = 0;
1da177e4
LT
5730 __u16 params, param_offset, offset, byte_count, count;
5731
f96637be
JP
5732 cifs_dbg(FYI, "SetFileSize (via SetFileInfo) %lld\n",
5733 (long long)size);
cd63499c
SF
5734 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5735
1da177e4
LT
5736 if (rc)
5737 return rc;
5738
d1433418
PS
5739 pSMB->hdr.Pid = cpu_to_le16((__u16)cfile->pid);
5740 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(cfile->pid >> 16));
50c2f753 5741
1da177e4
LT
5742 params = 6;
5743 pSMB->MaxSetupCount = 0;
5744 pSMB->Reserved = 0;
5745 pSMB->Flags = 0;
5746 pSMB->Timeout = 0;
5747 pSMB->Reserved2 = 0;
5748 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5749 offset = param_offset + params;
5750
1da177e4
LT
5751 count = sizeof(struct file_end_of_file_info);
5752 pSMB->MaxParameterCount = cpu_to_le16(2);
582d21e5
SF
5753 /* BB find exact max SMB PDU from sess structure BB */
5754 pSMB->MaxDataCount = cpu_to_le16(1000);
1da177e4
LT
5755 pSMB->SetupCount = 1;
5756 pSMB->Reserved3 = 0;
5757 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5758 byte_count = 3 /* pad */ + params + count;
5759 pSMB->DataCount = cpu_to_le16(count);
5760 pSMB->ParameterCount = cpu_to_le16(params);
5761 pSMB->TotalDataCount = pSMB->DataCount;
5762 pSMB->TotalParameterCount = pSMB->ParameterCount;
5763 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5764 parm_data =
50c2f753
SF
5765 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol)
5766 + offset);
1da177e4
LT
5767 pSMB->DataOffset = cpu_to_le16(offset);
5768 parm_data->FileSize = cpu_to_le64(size);
d1433418
PS
5769 pSMB->Fid = cfile->fid.netfid;
5770 if (set_allocation) {
1da177e4
LT
5771 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5772 pSMB->InformationLevel =
5773 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5774 else
5775 pSMB->InformationLevel =
5776 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
50c2f753 5777 } else /* Set File Size */ {
1da177e4
LT
5778 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5779 pSMB->InformationLevel =
50c2f753 5780 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
1da177e4
LT
5781 else
5782 pSMB->InformationLevel =
50c2f753 5783 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
1da177e4
LT
5784 }
5785 pSMB->Reserved4 = 0;
be8e3b00 5786 inc_rfc1001_len(pSMB, byte_count);
1da177e4 5787 pSMB->ByteCount = cpu_to_le16(byte_count);
792af7b0 5788 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
da502f7d 5789 cifs_small_buf_release(pSMB);
1da177e4 5790 if (rc) {
f96637be
JP
5791 cifs_dbg(FYI, "Send error in SetFileInfo (SetFileSize) = %d\n",
5792 rc);
1da177e4
LT
5793 }
5794
50c2f753 5795 /* Note: On -EAGAIN error only caller can retry on handle based calls
1da177e4
LT
5796 since file handle passed in no longer valid */
5797
5798 return rc;
5799}
5800
50c2f753 5801/* Some legacy servers such as NT4 require that the file times be set on
1da177e4
LT
5802 an open handle, rather than by pathname - this is awkward due to
5803 potential access conflicts on the open, but it is unavoidable for these
5804 old servers since the only other choice is to go from 100 nanosecond DCE
5805 time and resort to the original setpathinfo level which takes the ancient
5806 DOS time format with 2 second granularity */
5807int
6d5786a3 5808CIFSSMBSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
2dd2dfa0 5809 const FILE_BASIC_INFO *data, __u16 fid, __u32 pid_of_opener)
1da177e4
LT
5810{
5811 struct smb_com_transaction2_sfi_req *pSMB = NULL;
1da177e4
LT
5812 char *data_offset;
5813 int rc = 0;
1da177e4
LT
5814 __u16 params, param_offset, offset, byte_count, count;
5815
f96637be 5816 cifs_dbg(FYI, "Set Times (via SetFileInfo)\n");
cd63499c
SF
5817 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5818
1da177e4
LT
5819 if (rc)
5820 return rc;
5821
2dd2dfa0
JL
5822 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5823 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
50c2f753 5824
1da177e4
LT
5825 params = 6;
5826 pSMB->MaxSetupCount = 0;
5827 pSMB->Reserved = 0;
5828 pSMB->Flags = 0;
5829 pSMB->Timeout = 0;
5830 pSMB->Reserved2 = 0;
5831 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5832 offset = param_offset + params;
5833
b2a3ad9c
JL
5834 data_offset = (char *)pSMB +
5835 offsetof(struct smb_hdr, Protocol) + offset;
1da177e4 5836
26f57364 5837 count = sizeof(FILE_BASIC_INFO);
1da177e4 5838 pSMB->MaxParameterCount = cpu_to_le16(2);
582d21e5
SF
5839 /* BB find max SMB PDU from sess */
5840 pSMB->MaxDataCount = cpu_to_le16(1000);
1da177e4
LT
5841 pSMB->SetupCount = 1;
5842 pSMB->Reserved3 = 0;
5843 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5844 byte_count = 3 /* pad */ + params + count;
5845 pSMB->DataCount = cpu_to_le16(count);
5846 pSMB->ParameterCount = cpu_to_le16(params);
5847 pSMB->TotalDataCount = pSMB->DataCount;
5848 pSMB->TotalParameterCount = pSMB->ParameterCount;
5849 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5850 pSMB->DataOffset = cpu_to_le16(offset);
5851 pSMB->Fid = fid;
5852 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5853 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5854 else
5855 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5856 pSMB->Reserved4 = 0;
be8e3b00 5857 inc_rfc1001_len(pSMB, byte_count);
1da177e4 5858 pSMB->ByteCount = cpu_to_le16(byte_count);
50c2f753 5859 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
792af7b0 5860 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
da502f7d 5861 cifs_small_buf_release(pSMB);
ad7a2926 5862 if (rc)
f96637be
JP
5863 cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n",
5864 rc);
1da177e4 5865
50c2f753 5866 /* Note: On -EAGAIN error only caller can retry on handle based calls
1da177e4
LT
5867 since file handle passed in no longer valid */
5868
5869 return rc;
5870}
5871
6d22f098 5872int
6d5786a3 5873CIFSSMBSetFileDisposition(const unsigned int xid, struct cifs_tcon *tcon,
6d22f098
JL
5874 bool delete_file, __u16 fid, __u32 pid_of_opener)
5875{
5876 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5877 char *data_offset;
5878 int rc = 0;
5879 __u16 params, param_offset, offset, byte_count, count;
5880
f96637be 5881 cifs_dbg(FYI, "Set File Disposition (via SetFileInfo)\n");
6d22f098
JL
5882 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5883
5884 if (rc)
5885 return rc;
5886
5887 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5888 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5889
5890 params = 6;
5891 pSMB->MaxSetupCount = 0;
5892 pSMB->Reserved = 0;
5893 pSMB->Flags = 0;
5894 pSMB->Timeout = 0;
5895 pSMB->Reserved2 = 0;
5896 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5897 offset = param_offset + params;
5898
5899 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5900
5901 count = 1;
5902 pSMB->MaxParameterCount = cpu_to_le16(2);
5903 /* BB find max SMB PDU from sess */
5904 pSMB->MaxDataCount = cpu_to_le16(1000);
5905 pSMB->SetupCount = 1;
5906 pSMB->Reserved3 = 0;
5907 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5908 byte_count = 3 /* pad */ + params + count;
5909 pSMB->DataCount = cpu_to_le16(count);
5910 pSMB->ParameterCount = cpu_to_le16(params);
5911 pSMB->TotalDataCount = pSMB->DataCount;
5912 pSMB->TotalParameterCount = pSMB->ParameterCount;
5913 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5914 pSMB->DataOffset = cpu_to_le16(offset);
5915 pSMB->Fid = fid;
5916 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_DISPOSITION_INFO);
5917 pSMB->Reserved4 = 0;
be8e3b00 5918 inc_rfc1001_len(pSMB, byte_count);
6d22f098
JL
5919 pSMB->ByteCount = cpu_to_le16(byte_count);
5920 *data_offset = delete_file ? 1 : 0;
792af7b0 5921 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
da502f7d 5922 cifs_small_buf_release(pSMB);
6d22f098 5923 if (rc)
f96637be 5924 cifs_dbg(FYI, "Send error in SetFileDisposition = %d\n", rc);
6d22f098
JL
5925
5926 return rc;
5927}
1da177e4
LT
5928
5929int
6d5786a3 5930CIFSSMBSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
6fc000e5
JL
5931 const char *fileName, const FILE_BASIC_INFO *data,
5932 const struct nls_table *nls_codepage, int remap)
1da177e4
LT
5933{
5934 TRANSACTION2_SPI_REQ *pSMB = NULL;
5935 TRANSACTION2_SPI_RSP *pSMBr = NULL;
5936 int name_len;
5937 int rc = 0;
5938 int bytes_returned = 0;
5939 char *data_offset;
5940 __u16 params, param_offset, offset, byte_count, count;
5941
f96637be 5942 cifs_dbg(FYI, "In SetTimes\n");
1da177e4
LT
5943
5944SetTimesRetry:
5945 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5946 (void **) &pSMBr);
5947 if (rc)
5948 return rc;
5949
5950 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5951 name_len =
acbbb76a
SF
5952 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
5953 PATH_MAX, nls_codepage, remap);
1da177e4
LT
5954 name_len++; /* trailing null */
5955 name_len *= 2;
50c2f753 5956 } else { /* BB improve the check for buffer overruns BB */
1da177e4
LT
5957 name_len = strnlen(fileName, PATH_MAX);
5958 name_len++; /* trailing null */
5959 strncpy(pSMB->FileName, fileName, name_len);
5960 }
5961
5962 params = 6 + name_len;
26f57364 5963 count = sizeof(FILE_BASIC_INFO);
1da177e4 5964 pSMB->MaxParameterCount = cpu_to_le16(2);
582d21e5
SF
5965 /* BB find max SMB PDU from sess structure BB */
5966 pSMB->MaxDataCount = cpu_to_le16(1000);
1da177e4
LT
5967 pSMB->MaxSetupCount = 0;
5968 pSMB->Reserved = 0;
5969 pSMB->Flags = 0;
5970 pSMB->Timeout = 0;
5971 pSMB->Reserved2 = 0;
5972 param_offset = offsetof(struct smb_com_transaction2_spi_req,
50c2f753 5973 InformationLevel) - 4;
1da177e4
LT
5974 offset = param_offset + params;
5975 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5976 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5977 pSMB->DataOffset = cpu_to_le16(offset);
5978 pSMB->SetupCount = 1;
5979 pSMB->Reserved3 = 0;
5980 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5981 byte_count = 3 /* pad */ + params + count;
5982
5983 pSMB->DataCount = cpu_to_le16(count);
5984 pSMB->ParameterCount = cpu_to_le16(params);
5985 pSMB->TotalDataCount = pSMB->DataCount;
5986 pSMB->TotalParameterCount = pSMB->ParameterCount;
5987 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5988 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5989 else
5990 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5991 pSMB->Reserved4 = 0;
be8e3b00 5992 inc_rfc1001_len(pSMB, byte_count);
26f57364 5993 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
1da177e4
LT
5994 pSMB->ByteCount = cpu_to_le16(byte_count);
5995 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5996 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
ad7a2926 5997 if (rc)
f96637be 5998 cifs_dbg(FYI, "SetPathInfo (times) returned %d\n", rc);
1da177e4
LT
5999
6000 cifs_buf_release(pSMB);
6001
6002 if (rc == -EAGAIN)
6003 goto SetTimesRetry;
6004
6005 return rc;
6006}
6007
6008/* Can not be used to set time stamps yet (due to old DOS time format) */
6009/* Can be used to set attributes */
6010#if 0 /* Possibly not needed - since it turns out that strangely NT4 has a bug
6011 handling it anyway and NT4 was what we thought it would be needed for
6012 Do not delete it until we prove whether needed for Win9x though */
6013int
6d5786a3 6014CIFSSMBSetAttrLegacy(unsigned int xid, struct cifs_tcon *tcon, char *fileName,
1da177e4
LT
6015 __u16 dos_attrs, const struct nls_table *nls_codepage)
6016{
6017 SETATTR_REQ *pSMB = NULL;
6018 SETATTR_RSP *pSMBr = NULL;
6019 int rc = 0;
6020 int bytes_returned;
6021 int name_len;
6022
f96637be 6023 cifs_dbg(FYI, "In SetAttrLegacy\n");
1da177e4
LT
6024
6025SetAttrLgcyRetry:
6026 rc = smb_init(SMB_COM_SETATTR, 8, tcon, (void **) &pSMB,
6027 (void **) &pSMBr);
6028 if (rc)
6029 return rc;
6030
6031 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6032 name_len =
acbbb76a
SF
6033 ConvertToUTF16((__le16 *) pSMB->fileName, fileName,
6034 PATH_MAX, nls_codepage);
1da177e4
LT
6035 name_len++; /* trailing null */
6036 name_len *= 2;
50c2f753 6037 } else { /* BB improve the check for buffer overruns BB */
1da177e4
LT
6038 name_len = strnlen(fileName, PATH_MAX);
6039 name_len++; /* trailing null */
6040 strncpy(pSMB->fileName, fileName, name_len);
6041 }
6042 pSMB->attr = cpu_to_le16(dos_attrs);
6043 pSMB->BufferFormat = 0x04;
be8e3b00 6044 inc_rfc1001_len(pSMB, name_len + 1);
1da177e4
LT
6045 pSMB->ByteCount = cpu_to_le16(name_len + 1);
6046 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6047 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
ad7a2926 6048 if (rc)
f96637be 6049 cifs_dbg(FYI, "Error in LegacySetAttr = %d\n", rc);
1da177e4
LT
6050
6051 cifs_buf_release(pSMB);
6052
6053 if (rc == -EAGAIN)
6054 goto SetAttrLgcyRetry;
6055
6056 return rc;
6057}
6058#endif /* temporarily unneeded SetAttr legacy function */
6059
654cf14a
JL
6060static void
6061cifs_fill_unix_set_info(FILE_UNIX_BASIC_INFO *data_offset,
6062 const struct cifs_unix_set_info_args *args)
6063{
49418b2c 6064 u64 uid = NO_CHANGE_64, gid = NO_CHANGE_64;
654cf14a
JL
6065 u64 mode = args->mode;
6066
49418b2c
EB
6067 if (uid_valid(args->uid))
6068 uid = from_kuid(&init_user_ns, args->uid);
6069 if (gid_valid(args->gid))
6070 gid = from_kgid(&init_user_ns, args->gid);
6071
654cf14a
JL
6072 /*
6073 * Samba server ignores set of file size to zero due to bugs in some
6074 * older clients, but we should be precise - we use SetFileSize to
6075 * set file size and do not want to truncate file size to zero
25985edc 6076 * accidentally as happened on one Samba server beta by putting
654cf14a
JL
6077 * zero instead of -1 here
6078 */
6079 data_offset->EndOfFile = cpu_to_le64(NO_CHANGE_64);
6080 data_offset->NumOfBytes = cpu_to_le64(NO_CHANGE_64);
6081 data_offset->LastStatusChange = cpu_to_le64(args->ctime);
6082 data_offset->LastAccessTime = cpu_to_le64(args->atime);
6083 data_offset->LastModificationTime = cpu_to_le64(args->mtime);
49418b2c
EB
6084 data_offset->Uid = cpu_to_le64(uid);
6085 data_offset->Gid = cpu_to_le64(gid);
654cf14a
JL
6086 /* better to leave device as zero when it is */
6087 data_offset->DevMajor = cpu_to_le64(MAJOR(args->device));
6088 data_offset->DevMinor = cpu_to_le64(MINOR(args->device));
6089 data_offset->Permissions = cpu_to_le64(mode);
6090
6091 if (S_ISREG(mode))
6092 data_offset->Type = cpu_to_le32(UNIX_FILE);
6093 else if (S_ISDIR(mode))
6094 data_offset->Type = cpu_to_le32(UNIX_DIR);
6095 else if (S_ISLNK(mode))
6096 data_offset->Type = cpu_to_le32(UNIX_SYMLINK);
6097 else if (S_ISCHR(mode))
6098 data_offset->Type = cpu_to_le32(UNIX_CHARDEV);
6099 else if (S_ISBLK(mode))
6100 data_offset->Type = cpu_to_le32(UNIX_BLOCKDEV);
6101 else if (S_ISFIFO(mode))
6102 data_offset->Type = cpu_to_le32(UNIX_FIFO);
6103 else if (S_ISSOCK(mode))
6104 data_offset->Type = cpu_to_le32(UNIX_SOCKET);
6105}
6106
3bbeeb3c 6107int
6d5786a3 6108CIFSSMBUnixSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
3bbeeb3c
JL
6109 const struct cifs_unix_set_info_args *args,
6110 u16 fid, u32 pid_of_opener)
6111{
6112 struct smb_com_transaction2_sfi_req *pSMB = NULL;
b2a3ad9c 6113 char *data_offset;
3bbeeb3c
JL
6114 int rc = 0;
6115 u16 params, param_offset, offset, byte_count, count;
6116
f96637be 6117 cifs_dbg(FYI, "Set Unix Info (via SetFileInfo)\n");
3bbeeb3c
JL
6118 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
6119
6120 if (rc)
6121 return rc;
6122
6123 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
6124 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
6125
6126 params = 6;
6127 pSMB->MaxSetupCount = 0;
6128 pSMB->Reserved = 0;
6129 pSMB->Flags = 0;
6130 pSMB->Timeout = 0;
6131 pSMB->Reserved2 = 0;
6132 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
6133 offset = param_offset + params;
6134
b2a3ad9c
JL
6135 data_offset = (char *)pSMB +
6136 offsetof(struct smb_hdr, Protocol) + offset;
6137
3bbeeb3c
JL
6138 count = sizeof(FILE_UNIX_BASIC_INFO);
6139
6140 pSMB->MaxParameterCount = cpu_to_le16(2);
6141 /* BB find max SMB PDU from sess */
6142 pSMB->MaxDataCount = cpu_to_le16(1000);
6143 pSMB->SetupCount = 1;
6144 pSMB->Reserved3 = 0;
6145 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
6146 byte_count = 3 /* pad */ + params + count;
6147 pSMB->DataCount = cpu_to_le16(count);
6148 pSMB->ParameterCount = cpu_to_le16(params);
6149 pSMB->TotalDataCount = pSMB->DataCount;
6150 pSMB->TotalParameterCount = pSMB->ParameterCount;
6151 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6152 pSMB->DataOffset = cpu_to_le16(offset);
6153 pSMB->Fid = fid;
6154 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
6155 pSMB->Reserved4 = 0;
be8e3b00 6156 inc_rfc1001_len(pSMB, byte_count);
3bbeeb3c
JL
6157 pSMB->ByteCount = cpu_to_le16(byte_count);
6158
b2a3ad9c 6159 cifs_fill_unix_set_info((FILE_UNIX_BASIC_INFO *)data_offset, args);
3bbeeb3c 6160
792af7b0 6161 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
da502f7d 6162 cifs_small_buf_release(pSMB);
3bbeeb3c 6163 if (rc)
f96637be
JP
6164 cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n",
6165 rc);
3bbeeb3c
JL
6166
6167 /* Note: On -EAGAIN error only caller can retry on handle based calls
6168 since file handle passed in no longer valid */
6169
6170 return rc;
6171}
6172
1da177e4 6173int
6d5786a3 6174CIFSSMBUnixSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
ff691e96 6175 const char *file_name,
01ea95e3
JL
6176 const struct cifs_unix_set_info_args *args,
6177 const struct nls_table *nls_codepage, int remap)
1da177e4
LT
6178{
6179 TRANSACTION2_SPI_REQ *pSMB = NULL;
6180 TRANSACTION2_SPI_RSP *pSMBr = NULL;
6181 int name_len;
6182 int rc = 0;
6183 int bytes_returned = 0;
6184 FILE_UNIX_BASIC_INFO *data_offset;
6185 __u16 params, param_offset, offset, count, byte_count;
6186
f96637be 6187 cifs_dbg(FYI, "In SetUID/GID/Mode\n");
1da177e4
LT
6188setPermsRetry:
6189 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6190 (void **) &pSMBr);
6191 if (rc)
6192 return rc;
6193
6194 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6195 name_len =
ff691e96 6196 cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
acbbb76a 6197 PATH_MAX, nls_codepage, remap);
1da177e4
LT
6198 name_len++; /* trailing null */
6199 name_len *= 2;
3e87d803 6200 } else { /* BB improve the check for buffer overruns BB */
ff691e96 6201 name_len = strnlen(file_name, PATH_MAX);
1da177e4 6202 name_len++; /* trailing null */
ff691e96 6203 strncpy(pSMB->FileName, file_name, name_len);
1da177e4
LT
6204 }
6205
6206 params = 6 + name_len;
26f57364 6207 count = sizeof(FILE_UNIX_BASIC_INFO);
1da177e4 6208 pSMB->MaxParameterCount = cpu_to_le16(2);
582d21e5
SF
6209 /* BB find max SMB PDU from sess structure BB */
6210 pSMB->MaxDataCount = cpu_to_le16(1000);
1da177e4
LT
6211 pSMB->MaxSetupCount = 0;
6212 pSMB->Reserved = 0;
6213 pSMB->Flags = 0;
6214 pSMB->Timeout = 0;
6215 pSMB->Reserved2 = 0;
6216 param_offset = offsetof(struct smb_com_transaction2_spi_req,
50c2f753 6217 InformationLevel) - 4;
1da177e4
LT
6218 offset = param_offset + params;
6219 data_offset =
6220 (FILE_UNIX_BASIC_INFO *) ((char *) &pSMB->hdr.Protocol +
6221 offset);
6222 memset(data_offset, 0, count);
6223 pSMB->DataOffset = cpu_to_le16(offset);
6224 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6225 pSMB->SetupCount = 1;
6226 pSMB->Reserved3 = 0;
6227 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6228 byte_count = 3 /* pad */ + params + count;
6229 pSMB->ParameterCount = cpu_to_le16(params);
6230 pSMB->DataCount = cpu_to_le16(count);
6231 pSMB->TotalParameterCount = pSMB->ParameterCount;
6232 pSMB->TotalDataCount = pSMB->DataCount;
6233 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
6234 pSMB->Reserved4 = 0;
be8e3b00 6235 inc_rfc1001_len(pSMB, byte_count);
1da177e4 6236
654cf14a 6237 cifs_fill_unix_set_info(data_offset, args);
1da177e4
LT
6238
6239 pSMB->ByteCount = cpu_to_le16(byte_count);
6240 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6241 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
ad7a2926 6242 if (rc)
f96637be 6243 cifs_dbg(FYI, "SetPathInfo (perms) returned %d\n", rc);
1da177e4 6244
0d817bc0 6245 cifs_buf_release(pSMB);
1da177e4
LT
6246 if (rc == -EAGAIN)
6247 goto setPermsRetry;
6248 return rc;
6249}
6250
1da177e4 6251#ifdef CONFIG_CIFS_XATTR
31c0519f
JL
6252/*
6253 * Do a path-based QUERY_ALL_EAS call and parse the result. This is a common
6254 * function used by listxattr and getxattr type calls. When ea_name is set,
6255 * it looks for that attribute name and stuffs that value into the EAData
6256 * buffer. When ea_name is NULL, it stuffs a list of attribute names into the
6257 * buffer. In both cases, the return value is either the length of the
6258 * resulting data or a negative error code. If EAData is a NULL pointer then
6259 * the data isn't copied to it, but the length is returned.
6260 */
1da177e4 6261ssize_t
6d5786a3 6262CIFSSMBQAllEAs(const unsigned int xid, struct cifs_tcon *tcon,
31c0519f
JL
6263 const unsigned char *searchName, const unsigned char *ea_name,
6264 char *EAData, size_t buf_size,
67b4c889 6265 struct cifs_sb_info *cifs_sb)
1da177e4
LT
6266{
6267 /* BB assumes one setup word */
6268 TRANSACTION2_QPI_REQ *pSMB = NULL;
6269 TRANSACTION2_QPI_RSP *pSMBr = NULL;
67b4c889
SF
6270 int remap = cifs_remap(cifs_sb);
6271 struct nls_table *nls_codepage = cifs_sb->local_nls;
1da177e4
LT
6272 int rc = 0;
6273 int bytes_returned;
6e462b9f 6274 int list_len;
f0d3868b 6275 struct fealist *ea_response_data;
50c2f753
SF
6276 struct fea *temp_fea;
6277 char *temp_ptr;
0cd126b5 6278 char *end_of_smb;
f0d3868b 6279 __u16 params, byte_count, data_offset;
5980fc96 6280 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
1da177e4 6281
f96637be 6282 cifs_dbg(FYI, "In Query All EAs path %s\n", searchName);
1da177e4
LT
6283QAllEAsRetry:
6284 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6285 (void **) &pSMBr);
6286 if (rc)
6287 return rc;
6288
6289 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6e462b9f 6290 list_len =
acbbb76a
SF
6291 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
6292 PATH_MAX, nls_codepage, remap);
6e462b9f
JL
6293 list_len++; /* trailing null */
6294 list_len *= 2;
1da177e4 6295 } else { /* BB improve the check for buffer overruns BB */
6e462b9f
JL
6296 list_len = strnlen(searchName, PATH_MAX);
6297 list_len++; /* trailing null */
6298 strncpy(pSMB->FileName, searchName, list_len);
1da177e4
LT
6299 }
6300
6e462b9f 6301 params = 2 /* level */ + 4 /* reserved */ + list_len /* includes NUL */;
1da177e4
LT
6302 pSMB->TotalDataCount = 0;
6303 pSMB->MaxParameterCount = cpu_to_le16(2);
582d21e5 6304 /* BB find exact max SMB PDU from sess structure BB */
e529614a 6305 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
1da177e4
LT
6306 pSMB->MaxSetupCount = 0;
6307 pSMB->Reserved = 0;
6308 pSMB->Flags = 0;
6309 pSMB->Timeout = 0;
6310 pSMB->Reserved2 = 0;
6311 pSMB->ParameterOffset = cpu_to_le16(offsetof(
50c2f753 6312 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
1da177e4
LT
6313 pSMB->DataCount = 0;
6314 pSMB->DataOffset = 0;
6315 pSMB->SetupCount = 1;
6316 pSMB->Reserved3 = 0;
6317 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
6318 byte_count = params + 1 /* pad */ ;
6319 pSMB->TotalParameterCount = cpu_to_le16(params);
6320 pSMB->ParameterCount = pSMB->TotalParameterCount;
6321 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_QUERY_ALL_EAS);
6322 pSMB->Reserved4 = 0;
be8e3b00 6323 inc_rfc1001_len(pSMB, byte_count);
1da177e4
LT
6324 pSMB->ByteCount = cpu_to_le16(byte_count);
6325
6326 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6327 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
6328 if (rc) {
f96637be 6329 cifs_dbg(FYI, "Send error in QueryAllEAs = %d\n", rc);
f0d3868b
JL
6330 goto QAllEAsOut;
6331 }
1da177e4 6332
f0d3868b
JL
6333
6334 /* BB also check enough total bytes returned */
6335 /* BB we need to improve the validity checking
6336 of these trans2 responses */
6337
6338 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
820a803f 6339 if (rc || get_bcc(&pSMBr->hdr) < 4) {
f0d3868b
JL
6340 rc = -EIO; /* bad smb */
6341 goto QAllEAsOut;
6342 }
6343
6344 /* check that length of list is not more than bcc */
6345 /* check that each entry does not go beyond length
6346 of list */
6347 /* check that each element of each entry does not
6348 go beyond end of list */
6349 /* validate_trans2_offsets() */
6350 /* BB check if start of smb + data_offset > &bcc+ bcc */
6351
6352 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
6353 ea_response_data = (struct fealist *)
6354 (((char *) &pSMBr->hdr.Protocol) + data_offset);
6355
6e462b9f 6356 list_len = le32_to_cpu(ea_response_data->list_len);
f96637be 6357 cifs_dbg(FYI, "ea length %d\n", list_len);
6e462b9f 6358 if (list_len <= 8) {
f96637be 6359 cifs_dbg(FYI, "empty EA list returned from server\n");
60977fcc
SF
6360 /* didn't find the named attribute */
6361 if (ea_name)
6362 rc = -ENODATA;
f0d3868b
JL
6363 goto QAllEAsOut;
6364 }
6365
0cd126b5 6366 /* make sure list_len doesn't go past end of SMB */
690c522f 6367 end_of_smb = (char *)pByteArea(&pSMBr->hdr) + get_bcc(&pSMBr->hdr);
0cd126b5 6368 if ((char *)ea_response_data + list_len > end_of_smb) {
f96637be 6369 cifs_dbg(FYI, "EA list appears to go beyond SMB\n");
0cd126b5
JL
6370 rc = -EIO;
6371 goto QAllEAsOut;
6372 }
6373
f0d3868b 6374 /* account for ea list len */
6e462b9f 6375 list_len -= 4;
f0d3868b
JL
6376 temp_fea = ea_response_data->list;
6377 temp_ptr = (char *)temp_fea;
6e462b9f 6378 while (list_len > 0) {
122ca007 6379 unsigned int name_len;
f0d3868b 6380 __u16 value_len;
0cd126b5 6381
6e462b9f 6382 list_len -= 4;
f0d3868b 6383 temp_ptr += 4;
0cd126b5
JL
6384 /* make sure we can read name_len and value_len */
6385 if (list_len < 0) {
f96637be 6386 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
0cd126b5
JL
6387 rc = -EIO;
6388 goto QAllEAsOut;
6389 }
6390
6391 name_len = temp_fea->name_len;
6392 value_len = le16_to_cpu(temp_fea->value_len);
6393 list_len -= name_len + 1 + 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
31c0519f 6400 if (ea_name) {
91d065c4 6401 if (ea_name_len == name_len &&
ac423446 6402 memcmp(ea_name, temp_ptr, name_len) == 0) {
31c0519f
JL
6403 temp_ptr += name_len + 1;
6404 rc = value_len;
6405 if (buf_size == 0)
6406 goto QAllEAsOut;
6407 if ((size_t)value_len > buf_size) {
6408 rc = -ERANGE;
6409 goto QAllEAsOut;
6410 }
6411 memcpy(EAData, temp_ptr, value_len);
6412 goto QAllEAsOut;
6413 }
f0d3868b 6414 } else {
31c0519f
JL
6415 /* account for prefix user. and trailing null */
6416 rc += (5 + 1 + name_len);
6417 if (rc < (int) buf_size) {
6418 memcpy(EAData, "user.", 5);
6419 EAData += 5;
6420 memcpy(EAData, temp_ptr, name_len);
6421 EAData += name_len;
6422 /* null terminate name */
6423 *EAData = 0;
6424 ++EAData;
6425 } else if (buf_size == 0) {
6426 /* skip copy - calc size only */
6427 } else {
6428 /* stop before overrun buffer */
6429 rc = -ERANGE;
6430 break;
6431 }
1da177e4 6432 }
0cd126b5 6433 temp_ptr += name_len + 1 + value_len;
f0d3868b 6434 temp_fea = (struct fea *)temp_ptr;
1da177e4 6435 }
f0d3868b 6436
31c0519f
JL
6437 /* didn't find the named attribute */
6438 if (ea_name)
6439 rc = -ENODATA;
6440
f0d3868b 6441QAllEAsOut:
0d817bc0 6442 cifs_buf_release(pSMB);
1da177e4
LT
6443 if (rc == -EAGAIN)
6444 goto QAllEAsRetry;
6445
6446 return (ssize_t)rc;
6447}
6448
1da177e4 6449int
6d5786a3
PS
6450CIFSSMBSetEA(const unsigned int xid, struct cifs_tcon *tcon,
6451 const char *fileName, const char *ea_name, const void *ea_value,
50c2f753 6452 const __u16 ea_value_len, const struct nls_table *nls_codepage,
5517554e 6453 struct cifs_sb_info *cifs_sb)
1da177e4
LT
6454{
6455 struct smb_com_transaction2_spi_req *pSMB = NULL;
6456 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
6457 struct fealist *parm_data;
6458 int name_len;
6459 int rc = 0;
6460 int bytes_returned = 0;
6461 __u16 params, param_offset, byte_count, offset, count;
5517554e 6462 int remap = cifs_remap(cifs_sb);
1da177e4 6463
f96637be 6464 cifs_dbg(FYI, "In SetEA\n");
1da177e4
LT
6465SetEARetry:
6466 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6467 (void **) &pSMBr);
6468 if (rc)
6469 return rc;
6470
6471 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6472 name_len =
acbbb76a
SF
6473 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
6474 PATH_MAX, nls_codepage, remap);
1da177e4
LT
6475 name_len++; /* trailing null */
6476 name_len *= 2;
50c2f753 6477 } else { /* BB improve the check for buffer overruns BB */
1da177e4
LT
6478 name_len = strnlen(fileName, PATH_MAX);
6479 name_len++; /* trailing null */
6480 strncpy(pSMB->FileName, fileName, name_len);
6481 }
6482
6483 params = 6 + name_len;
6484
6485 /* done calculating parms using name_len of file name,
6486 now use name_len to calculate length of ea name
6487 we are going to create in the inode xattrs */
790fe579 6488 if (ea_name == NULL)
1da177e4
LT
6489 name_len = 0;
6490 else
50c2f753 6491 name_len = strnlen(ea_name, 255);
1da177e4 6492
dae5dbdb 6493 count = sizeof(*parm_data) + ea_value_len + name_len;
1da177e4 6494 pSMB->MaxParameterCount = cpu_to_le16(2);
582d21e5
SF
6495 /* BB find max SMB PDU from sess */
6496 pSMB->MaxDataCount = cpu_to_le16(1000);
1da177e4
LT
6497 pSMB->MaxSetupCount = 0;
6498 pSMB->Reserved = 0;
6499 pSMB->Flags = 0;
6500 pSMB->Timeout = 0;
6501 pSMB->Reserved2 = 0;
6502 param_offset = offsetof(struct smb_com_transaction2_spi_req,
50c2f753 6503 InformationLevel) - 4;
1da177e4
LT
6504 offset = param_offset + params;
6505 pSMB->InformationLevel =
6506 cpu_to_le16(SMB_SET_FILE_EA);
6507
ade7db99 6508 parm_data = (void *)pSMB + offsetof(struct smb_hdr, Protocol) + offset;
1da177e4
LT
6509 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6510 pSMB->DataOffset = cpu_to_le16(offset);
6511 pSMB->SetupCount = 1;
6512 pSMB->Reserved3 = 0;
6513 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6514 byte_count = 3 /* pad */ + params + count;
6515 pSMB->DataCount = cpu_to_le16(count);
6516 parm_data->list_len = cpu_to_le32(count);
6517 parm_data->list[0].EA_flags = 0;
6518 /* we checked above that name len is less than 255 */
53b3531b 6519 parm_data->list[0].name_len = (__u8)name_len;
1da177e4 6520 /* EA names are always ASCII */
790fe579 6521 if (ea_name)
50c2f753 6522 strncpy(parm_data->list[0].name, ea_name, name_len);
1da177e4
LT
6523 parm_data->list[0].name[name_len] = 0;
6524 parm_data->list[0].value_len = cpu_to_le16(ea_value_len);
6525 /* caller ensures that ea_value_len is less than 64K but
6526 we need to ensure that it fits within the smb */
6527
50c2f753
SF
6528 /*BB add length check to see if it would fit in
6529 negotiated SMB buffer size BB */
790fe579
SF
6530 /* if (ea_value_len > buffer_size - 512 (enough for header)) */
6531 if (ea_value_len)
50c2f753
SF
6532 memcpy(parm_data->list[0].name+name_len+1,
6533 ea_value, ea_value_len);
1da177e4
LT
6534
6535 pSMB->TotalDataCount = pSMB->DataCount;
6536 pSMB->ParameterCount = cpu_to_le16(params);
6537 pSMB->TotalParameterCount = pSMB->ParameterCount;
6538 pSMB->Reserved4 = 0;
be8e3b00 6539 inc_rfc1001_len(pSMB, byte_count);
1da177e4
LT
6540 pSMB->ByteCount = cpu_to_le16(byte_count);
6541 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6542 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
ad7a2926 6543 if (rc)
f96637be 6544 cifs_dbg(FYI, "SetPathInfo (EA) returned %d\n", rc);
1da177e4
LT
6545
6546 cifs_buf_release(pSMB);
6547
6548 if (rc == -EAGAIN)
6549 goto SetEARetry;
6550
6551 return rc;
6552}
1da177e4 6553#endif
0eff0e26
SF
6554
6555#ifdef CONFIG_CIFS_DNOTIFY_EXPERIMENTAL /* BB unused temporarily */
6556/*
6557 * Years ago the kernel added a "dnotify" function for Samba server,
6558 * to allow network clients (such as Windows) to display updated
6559 * lists of files in directory listings automatically when
6560 * files are added by one user when another user has the
6561 * same directory open on their desktop. The Linux cifs kernel
6562 * client hooked into the kernel side of this interface for
6563 * the same reason, but ironically when the VFS moved from
6564 * "dnotify" to "inotify" it became harder to plug in Linux
6565 * network file system clients (the most obvious use case
6566 * for notify interfaces is when multiple users can update
6567 * the contents of the same directory - exactly what network
6568 * file systems can do) although the server (Samba) could
6569 * still use it. For the short term we leave the worker
6570 * function ifdeffed out (below) until inotify is fixed
6571 * in the VFS to make it easier to plug in network file
6572 * system clients. If inotify turns out to be permanently
6573 * incompatible for network fs clients, we could instead simply
6574 * expose this config flag by adding a future cifs (and smb2) notify ioctl.
6575 */
6d5786a3 6576int CIFSSMBNotify(const unsigned int xid, struct cifs_tcon *tcon,
0eff0e26
SF
6577 const int notify_subdirs, const __u16 netfid,
6578 __u32 filter, struct file *pfile, int multishot,
6579 const struct nls_table *nls_codepage)
6580{
6581 int rc = 0;
6582 struct smb_com_transaction_change_notify_req *pSMB = NULL;
6583 struct smb_com_ntransaction_change_notify_rsp *pSMBr = NULL;
6584 struct dir_notify_req *dnotify_req;
6585 int bytes_returned;
6586
f96637be 6587 cifs_dbg(FYI, "In CIFSSMBNotify for file handle %d\n", (int)netfid);
0eff0e26
SF
6588 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
6589 (void **) &pSMBr);
6590 if (rc)
6591 return rc;
6592
6593 pSMB->TotalParameterCount = 0 ;
6594 pSMB->TotalDataCount = 0;
6595 pSMB->MaxParameterCount = cpu_to_le32(2);
c974befa 6596 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
0eff0e26
SF
6597 pSMB->MaxSetupCount = 4;
6598 pSMB->Reserved = 0;
6599 pSMB->ParameterOffset = 0;
6600 pSMB->DataCount = 0;
6601 pSMB->DataOffset = 0;
6602 pSMB->SetupCount = 4; /* single byte does not need le conversion */
6603 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_NOTIFY_CHANGE);
6604 pSMB->ParameterCount = pSMB->TotalParameterCount;
6605 if (notify_subdirs)
6606 pSMB->WatchTree = 1; /* one byte - no le conversion needed */
6607 pSMB->Reserved2 = 0;
6608 pSMB->CompletionFilter = cpu_to_le32(filter);
6609 pSMB->Fid = netfid; /* file handle always le */
6610 pSMB->ByteCount = 0;
6611
6612 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6613 (struct smb_hdr *)pSMBr, &bytes_returned,
6614 CIFS_ASYNC_OP);
6615 if (rc) {
f96637be 6616 cifs_dbg(FYI, "Error in Notify = %d\n", rc);
0eff0e26
SF
6617 } else {
6618 /* Add file to outstanding requests */
6619 /* BB change to kmem cache alloc */
6620 dnotify_req = kmalloc(
6621 sizeof(struct dir_notify_req),
6622 GFP_KERNEL);
6623 if (dnotify_req) {
6624 dnotify_req->Pid = pSMB->hdr.Pid;
6625 dnotify_req->PidHigh = pSMB->hdr.PidHigh;
6626 dnotify_req->Mid = pSMB->hdr.Mid;
6627 dnotify_req->Tid = pSMB->hdr.Tid;
6628 dnotify_req->Uid = pSMB->hdr.Uid;
6629 dnotify_req->netfid = netfid;
6630 dnotify_req->pfile = pfile;
6631 dnotify_req->filter = filter;
6632 dnotify_req->multishot = multishot;
6633 spin_lock(&GlobalMid_Lock);
6634 list_add_tail(&dnotify_req->lhead,
6635 &GlobalDnotifyReqList);
6636 spin_unlock(&GlobalMid_Lock);
6637 } else
6638 rc = -ENOMEM;
6639 }
6640 cifs_buf_release(pSMB);
6641 return rc;
6642}
6643#endif /* was needed for dnotify, and will be needed for inotify when VFS fix */
This page took 1.825242 seconds and 4 git commands to generate.