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