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