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