4 * Copyright (C) International Business Machines Corp., 2002,2010
7 * Contains the routines for constructing the SMB PDUs themselves
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.
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.
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
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 */
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>
42 #include "cifsproto.h"
43 #include "cifs_unicode.h"
44 #include "cifs_debug.h"
47 #ifdef CONFIG_CIFS_POSIX
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"},
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"},
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
79 #define CIFS_NUM_PROT 2
80 #endif /* CIFS_WEAK_PW_HASH */
82 #ifdef CONFIG_CIFS_WEAK_PW_HASH
83 #define CIFS_NUM_PROT 3
85 #define CIFS_NUM_PROT 1
86 #endif /* CONFIG_CIFS_WEAK_PW_HASH */
87 #endif /* CIFS_POSIX */
89 /* Forward declarations */
91 /* Mark as invalid, all open files on tree connections since they
92 were closed when session to server was lost */
93 static void mark_open_files_invalid(struct cifs_tcon *pTcon)
95 struct cifsFileInfo *open_file = NULL;
96 struct list_head *tmp;
97 struct list_head *tmp1;
99 /* list all files open on tree connection and mark them invalid */
100 spin_lock(&cifs_file_list_lock);
101 list_for_each_safe(tmp, tmp1, &pTcon->openFileList) {
102 open_file = list_entry(tmp, struct cifsFileInfo, tlist);
103 open_file->invalidHandle = true;
104 open_file->oplock_break_cancelled = true;
106 spin_unlock(&cifs_file_list_lock);
107 /* BB Add call to invalidate_inodes(sb) for all superblocks mounted
111 /* reconnect the socket, tcon, and smb session if needed */
113 cifs_reconnect_tcon(struct cifs_tcon *tcon, int smb_command)
116 struct cifs_ses *ses;
117 struct TCP_Server_Info *server;
118 struct nls_table *nls_codepage;
121 * SMBs NegProt, SessSetup, uLogoff do not have tcon yet so check for
122 * tcp and smb session status done differently for those three - in the
129 server = ses->server;
132 * only tree disconnect, open, and write, (and ulogoff which does not
133 * have tcon) are allowed as we start force umount
135 if (tcon->tidStatus == CifsExiting) {
136 if (smb_command != SMB_COM_WRITE_ANDX &&
137 smb_command != SMB_COM_OPEN_ANDX &&
138 smb_command != SMB_COM_TREE_DISCONNECT) {
139 cFYI(1, "can not send cmd %d while umounting",
146 * Give demultiplex thread up to 10 seconds to reconnect, should be
147 * greater than cifs socket timeout which is 7 seconds
149 while (server->tcpStatus == CifsNeedReconnect) {
150 wait_event_interruptible_timeout(server->response_q,
151 (server->tcpStatus != CifsNeedReconnect), 10 * HZ);
153 /* are we still trying to reconnect? */
154 if (server->tcpStatus != CifsNeedReconnect)
158 * on "soft" mounts we wait once. Hard mounts keep
159 * retrying until process is killed or server comes
163 cFYI(1, "gave up waiting on reconnect in smb_init");
168 if (!ses->need_reconnect && !tcon->need_reconnect)
171 nls_codepage = load_nls_default();
174 * need to prevent multiple threads trying to simultaneously
175 * reconnect the same SMB session
177 mutex_lock(&ses->session_mutex);
178 rc = cifs_negotiate_protocol(0, ses);
179 if (rc == 0 && ses->need_reconnect)
180 rc = cifs_setup_session(0, ses, nls_codepage);
182 /* do we need to reconnect tcon? */
183 if (rc || !tcon->need_reconnect) {
184 mutex_unlock(&ses->session_mutex);
188 mark_open_files_invalid(tcon);
189 rc = CIFSTCon(0, ses, tcon->treeName, tcon, nls_codepage);
190 mutex_unlock(&ses->session_mutex);
191 cFYI(1, "reconnect tcon rc = %d", rc);
197 * FIXME: check if wsize needs updated due to negotiated smb buffer
200 atomic_inc(&tconInfoReconnectCount);
202 /* tell server Unix caps we support */
203 if (ses->capabilities & CAP_UNIX)
204 reset_cifs_unix_caps(0, tcon, NULL, NULL);
207 * Removed call to reopen open files here. It is safer (and faster) to
208 * reopen files one at a time as needed in read and write.
210 * FIXME: what about file locks? don't we need to reclaim them ASAP?
215 * Check if handle based operation so we know whether we can continue
216 * or not without returning to caller to reset file handle
218 switch (smb_command) {
219 case SMB_COM_READ_ANDX:
220 case SMB_COM_WRITE_ANDX:
222 case SMB_COM_FIND_CLOSE2:
223 case SMB_COM_LOCKING_ANDX:
227 unload_nls(nls_codepage);
231 /* Allocate and return pointer to an SMB request buffer, and set basic
232 SMB information in the SMB header. If the return code is zero, this
233 function must have filled in request_buf pointer */
235 small_smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
240 rc = cifs_reconnect_tcon(tcon, smb_command);
244 *request_buf = cifs_small_buf_get();
245 if (*request_buf == NULL) {
246 /* BB should we add a retry in here if not a writepage? */
250 header_assemble((struct smb_hdr *) *request_buf, smb_command,
254 cifs_stats_inc(&tcon->num_smbs_sent);
260 small_smb_init_no_tc(const int smb_command, const int wct,
261 struct cifs_ses *ses, void **request_buf)
264 struct smb_hdr *buffer;
266 rc = small_smb_init(smb_command, wct, NULL, request_buf);
270 buffer = (struct smb_hdr *)*request_buf;
271 buffer->Mid = GetNextMid(ses->server);
272 if (ses->capabilities & CAP_UNICODE)
273 buffer->Flags2 |= SMBFLG2_UNICODE;
274 if (ses->capabilities & CAP_STATUS32)
275 buffer->Flags2 |= SMBFLG2_ERR_STATUS;
277 /* uid, tid can stay at zero as set in header assemble */
279 /* BB add support for turning on the signing when
280 this function is used after 1st of session setup requests */
285 /* If the return code is zero, this function must fill in request_buf pointer */
287 __smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
288 void **request_buf, void **response_buf)
290 *request_buf = cifs_buf_get();
291 if (*request_buf == NULL) {
292 /* BB should we add a retry in here if not a writepage? */
295 /* Although the original thought was we needed the response buf for */
296 /* potential retries of smb operations it turns out we can determine */
297 /* from the mid flags when the request buffer can be resent without */
298 /* having to use a second distinct buffer for the response */
300 *response_buf = *request_buf;
302 header_assemble((struct smb_hdr *) *request_buf, smb_command, tcon,
306 cifs_stats_inc(&tcon->num_smbs_sent);
311 /* If the return code is zero, this function must fill in request_buf pointer */
313 smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
314 void **request_buf, void **response_buf)
318 rc = cifs_reconnect_tcon(tcon, smb_command);
322 return __smb_init(smb_command, wct, tcon, request_buf, response_buf);
326 smb_init_no_reconnect(int smb_command, int wct, struct cifs_tcon *tcon,
327 void **request_buf, void **response_buf)
329 if (tcon->ses->need_reconnect || tcon->need_reconnect)
332 return __smb_init(smb_command, wct, tcon, request_buf, response_buf);
335 static int validate_t2(struct smb_t2_rsp *pSMB)
337 unsigned int total_size;
339 /* check for plausible wct */
340 if (pSMB->hdr.WordCount < 10)
343 /* check for parm and data offset going beyond end of smb */
344 if (get_unaligned_le16(&pSMB->t2_rsp.ParameterOffset) > 1024 ||
345 get_unaligned_le16(&pSMB->t2_rsp.DataOffset) > 1024)
348 total_size = get_unaligned_le16(&pSMB->t2_rsp.ParameterCount);
349 if (total_size >= 512)
352 /* check that bcc is at least as big as parms + data, and that it is
353 * less than negotiated smb buffer
355 total_size += get_unaligned_le16(&pSMB->t2_rsp.DataCount);
356 if (total_size > get_bcc(&pSMB->hdr) ||
357 total_size >= CIFSMaxBufSize + MAX_CIFS_HDR_SIZE)
362 cifs_dump_mem("Invalid transact2 SMB: ", (char *)pSMB,
363 sizeof(struct smb_t2_rsp) + 16);
367 static inline void inc_rfc1001_len(void *pSMB, int count)
369 struct smb_hdr *hdr = (struct smb_hdr *)pSMB;
371 be32_add_cpu(&hdr->smb_buf_length, count);
375 CIFSSMBNegotiate(unsigned int xid, struct cifs_ses *ses)
378 NEGOTIATE_RSP *pSMBr;
382 struct TCP_Server_Info *server;
384 unsigned int secFlags;
387 server = ses->server;
392 rc = smb_init(SMB_COM_NEGOTIATE, 0, NULL /* no tcon yet */ ,
393 (void **) &pSMB, (void **) &pSMBr);
397 /* if any of auth flags (ie not sign or seal) are overriden use them */
398 if (ses->overrideSecFlg & (~(CIFSSEC_MUST_SIGN | CIFSSEC_MUST_SEAL)))
399 secFlags = ses->overrideSecFlg; /* BB FIXME fix sign flags? */
400 else /* if override flags set only sign/seal OR them with global auth */
401 secFlags = global_secflags | ses->overrideSecFlg;
403 cFYI(1, "secFlags 0x%x", secFlags);
405 pSMB->hdr.Mid = GetNextMid(server);
406 pSMB->hdr.Flags2 |= (SMBFLG2_UNICODE | SMBFLG2_ERR_STATUS);
408 if ((secFlags & CIFSSEC_MUST_KRB5) == CIFSSEC_MUST_KRB5)
409 pSMB->hdr.Flags2 |= SMBFLG2_EXT_SEC;
410 else if ((secFlags & CIFSSEC_AUTH_MASK) == CIFSSEC_MAY_KRB5) {
411 cFYI(1, "Kerberos only mechanism, enable extended security");
412 pSMB->hdr.Flags2 |= SMBFLG2_EXT_SEC;
413 } else if ((secFlags & CIFSSEC_MUST_NTLMSSP) == CIFSSEC_MUST_NTLMSSP)
414 pSMB->hdr.Flags2 |= SMBFLG2_EXT_SEC;
415 else if ((secFlags & CIFSSEC_AUTH_MASK) == CIFSSEC_MAY_NTLMSSP) {
416 cFYI(1, "NTLMSSP only mechanism, enable extended security");
417 pSMB->hdr.Flags2 |= SMBFLG2_EXT_SEC;
421 for (i = 0; i < CIFS_NUM_PROT; i++) {
422 strncpy(pSMB->DialectsArray+count, protocols[i].name, 16);
423 count += strlen(protocols[i].name) + 1;
424 /* null at end of source and target buffers anyway */
426 inc_rfc1001_len(pSMB, count);
427 pSMB->ByteCount = cpu_to_le16(count);
429 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
430 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
434 server->dialect = le16_to_cpu(pSMBr->DialectIndex);
435 cFYI(1, "Dialect: %d", server->dialect);
436 /* Check wct = 1 error case */
437 if ((pSMBr->hdr.WordCount < 13) || (server->dialect == BAD_PROT)) {
438 /* core returns wct = 1, but we do not ask for core - otherwise
439 small wct just comes when dialect index is -1 indicating we
440 could not negotiate a common dialect */
443 #ifdef CONFIG_CIFS_WEAK_PW_HASH
444 } else if ((pSMBr->hdr.WordCount == 13)
445 && ((server->dialect == LANMAN_PROT)
446 || (server->dialect == LANMAN2_PROT))) {
448 struct lanman_neg_rsp *rsp = (struct lanman_neg_rsp *)pSMBr;
450 if ((secFlags & CIFSSEC_MAY_LANMAN) ||
451 (secFlags & CIFSSEC_MAY_PLNTXT))
452 server->secType = LANMAN;
454 cERROR(1, "mount failed weak security disabled"
455 " in /proc/fs/cifs/SecurityFlags");
459 server->sec_mode = (__u8)le16_to_cpu(rsp->SecurityMode);
460 server->maxReq = min_t(unsigned int,
461 le16_to_cpu(rsp->MaxMpxCount),
463 set_credits(server, server->maxReq);
464 server->maxBuf = le16_to_cpu(rsp->MaxBufSize);
465 server->max_vcs = le16_to_cpu(rsp->MaxNumberVcs);
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;
472 server->max_rw = 0;/* do not need to use raw anyway */
473 server->capabilities = CAP_MPX_MODE;
475 tmp = (__s16)le16_to_cpu(rsp->ServerTimeZone);
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
484 int val, seconds, remain, result;
485 struct timespec ts, utc;
487 ts = cnvrtDosUnixTm(rsp->SrvTime.Date,
488 rsp->SrvTime.Time, 0);
489 cFYI(1, "SrvTime %d sec since 1970 (utc: %d) diff: %d",
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);
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;
500 server->timeAdj = result;
502 server->timeAdj = (int)tmp;
503 server->timeAdj *= 60; /* also in seconds */
505 cFYI(1, "server->timeAdj: %d seconds", server->timeAdj);
508 /* BB get server time for time conversions and add
509 code to use it and timezone since this is not UTC */
511 if (rsp->EncryptionKeyLength ==
512 cpu_to_le16(CIFS_CRYPTO_KEY_SIZE)) {
513 memcpy(ses->server->cryptkey, rsp->EncryptionKey,
514 CIFS_CRYPTO_KEY_SIZE);
515 } else if (server->sec_mode & SECMODE_PW_ENCRYPT) {
516 rc = -EIO; /* need cryptkey unless plain text */
520 cFYI(1, "LANMAN negotiated");
521 /* we will not end up setting signing flags - as no signing
522 was in LANMAN and server did not return the flags on */
524 #else /* weak security disabled */
525 } else if (pSMBr->hdr.WordCount == 13) {
526 cERROR(1, "mount failed, cifs module not built "
527 "with CIFS_WEAK_PW_HASH support");
529 #endif /* WEAK_PW_HASH */
531 } else if (pSMBr->hdr.WordCount != 17) {
536 /* else wct == 17 NTLM */
537 server->sec_mode = pSMBr->SecurityMode;
538 if ((server->sec_mode & SECMODE_USER) == 0)
539 cFYI(1, "share mode security");
541 if ((server->sec_mode & SECMODE_PW_ENCRYPT) == 0)
542 #ifdef CONFIG_CIFS_WEAK_PW_HASH
543 if ((secFlags & CIFSSEC_MAY_PLNTXT) == 0)
544 #endif /* CIFS_WEAK_PW_HASH */
545 cERROR(1, "Server requests plain text password"
546 " but client support disabled");
548 if ((secFlags & CIFSSEC_MUST_NTLMV2) == CIFSSEC_MUST_NTLMV2)
549 server->secType = NTLMv2;
550 else if (secFlags & CIFSSEC_MAY_NTLM)
551 server->secType = NTLM;
552 else if (secFlags & CIFSSEC_MAY_NTLMV2)
553 server->secType = NTLMv2;
554 else if (secFlags & CIFSSEC_MAY_KRB5)
555 server->secType = Kerberos;
556 else if (secFlags & CIFSSEC_MAY_NTLMSSP)
557 server->secType = RawNTLMSSP;
558 else if (secFlags & CIFSSEC_MAY_LANMAN)
559 server->secType = LANMAN;
562 cERROR(1, "Invalid security type");
565 /* else ... any others ...? */
567 /* one byte, so no need to convert this or EncryptionKeyLen from
569 server->maxReq = min_t(unsigned int, le16_to_cpu(pSMBr->MaxMpxCount),
571 set_credits(server, server->maxReq);
572 /* probably no need to store and check maxvcs */
573 server->maxBuf = le32_to_cpu(pSMBr->MaxBufferSize);
574 server->max_rw = le32_to_cpu(pSMBr->MaxRawSize);
575 cFYI(DBG2, "Max buf = %d", ses->server->maxBuf);
576 server->capabilities = le32_to_cpu(pSMBr->Capabilities);
577 server->timeAdj = (int)(__s16)le16_to_cpu(pSMBr->ServerTimeZone);
578 server->timeAdj *= 60;
579 if (pSMBr->EncryptionKeyLength == CIFS_CRYPTO_KEY_SIZE) {
580 memcpy(ses->server->cryptkey, pSMBr->u.EncryptionKey,
581 CIFS_CRYPTO_KEY_SIZE);
582 } else if ((pSMBr->hdr.Flags2 & SMBFLG2_EXT_SEC ||
583 server->capabilities & CAP_EXTENDED_SECURITY) &&
584 (pSMBr->EncryptionKeyLength == 0)) {
585 /* decode security blob */
586 count = get_bcc(&pSMBr->hdr);
591 spin_lock(&cifs_tcp_ses_lock);
592 if (server->srv_count > 1) {
593 spin_unlock(&cifs_tcp_ses_lock);
594 if (memcmp(server->server_GUID,
595 pSMBr->u.extended_response.
597 cFYI(1, "server UID changed");
598 memcpy(server->server_GUID,
599 pSMBr->u.extended_response.GUID,
603 spin_unlock(&cifs_tcp_ses_lock);
604 memcpy(server->server_GUID,
605 pSMBr->u.extended_response.GUID, 16);
609 server->secType = RawNTLMSSP;
611 rc = decode_negTokenInit(pSMBr->u.extended_response.
612 SecurityBlob, count - 16,
618 if (server->secType == Kerberos) {
619 if (!server->sec_kerberos &&
620 !server->sec_mskerberos)
622 } else if (server->secType == RawNTLMSSP) {
623 if (!server->sec_ntlmssp)
628 } else if (server->sec_mode & SECMODE_PW_ENCRYPT) {
629 rc = -EIO; /* no crypt key only if plain text pwd */
632 server->capabilities &= ~CAP_EXTENDED_SECURITY;
634 #ifdef CONFIG_CIFS_WEAK_PW_HASH
637 if ((secFlags & CIFSSEC_MAY_SIGN) == 0) {
638 /* MUST_SIGN already includes the MAY_SIGN FLAG
639 so if this is zero it means that signing is disabled */
640 cFYI(1, "Signing disabled");
641 if (server->sec_mode & SECMODE_SIGN_REQUIRED) {
642 cERROR(1, "Server requires "
643 "packet signing to be enabled in "
644 "/proc/fs/cifs/SecurityFlags.");
648 ~(SECMODE_SIGN_ENABLED | SECMODE_SIGN_REQUIRED);
649 } else if ((secFlags & CIFSSEC_MUST_SIGN) == CIFSSEC_MUST_SIGN) {
650 /* signing required */
651 cFYI(1, "Must sign - secFlags 0x%x", secFlags);
652 if ((server->sec_mode &
653 (SECMODE_SIGN_ENABLED | SECMODE_SIGN_REQUIRED)) == 0) {
654 cERROR(1, "signing required but server lacks support");
657 server->sec_mode |= SECMODE_SIGN_REQUIRED;
659 /* signing optional ie CIFSSEC_MAY_SIGN */
660 if ((server->sec_mode & SECMODE_SIGN_REQUIRED) == 0)
662 ~(SECMODE_SIGN_ENABLED | SECMODE_SIGN_REQUIRED);
666 cifs_buf_release(pSMB);
668 cFYI(1, "negprot rc %d", rc);
673 CIFSSMBTDis(const int xid, struct cifs_tcon *tcon)
675 struct smb_hdr *smb_buffer;
678 cFYI(1, "In tree disconnect");
680 /* BB: do we need to check this? These should never be NULL. */
681 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
685 * No need to return error on this operation if tid invalidated and
686 * closed on server already e.g. due to tcp session crashing. Also,
687 * the tcon is no longer on the list, so no need to take lock before
690 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
693 rc = small_smb_init(SMB_COM_TREE_DISCONNECT, 0, tcon,
694 (void **)&smb_buffer);
698 rc = SendReceiveNoRsp(xid, tcon->ses, (char *)smb_buffer, 0);
700 cFYI(1, "Tree disconnect failed %d", rc);
702 /* No need to return error on this operation if tid invalidated and
703 closed on server already e.g. due to tcp session crashing */
711 * This is a no-op for now. We're not really interested in the reply, but
712 * rather in the fact that the server sent one and that server->lstrp
715 * FIXME: maybe we should consider checking that the reply matches request?
718 cifs_echo_callback(struct mid_q_entry *mid)
720 struct TCP_Server_Info *server = mid->callback_data;
722 DeleteMidQEntry(mid);
723 add_credits(server, 1);
727 CIFSSMBEcho(struct TCP_Server_Info *server)
733 cFYI(1, "In echo request");
735 rc = small_smb_init(SMB_COM_ECHO, 0, NULL, (void **)&smb);
739 /* set up echo request */
740 smb->hdr.Tid = 0xffff;
741 smb->hdr.WordCount = 1;
742 put_unaligned_le16(1, &smb->EchoCount);
743 put_bcc(1, &smb->hdr);
745 inc_rfc1001_len(smb, 3);
747 iov.iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4;
749 rc = cifs_call_async(server, &iov, 1, NULL, cifs_echo_callback,
752 cFYI(1, "Echo request failed: %d", rc);
754 cifs_small_buf_release(smb);
760 CIFSSMBLogoff(const int xid, struct cifs_ses *ses)
762 LOGOFF_ANDX_REQ *pSMB;
765 cFYI(1, "In SMBLogoff for session disconnect");
768 * BB: do we need to check validity of ses and server? They should
769 * always be valid since we have an active reference. If not, that
770 * should probably be a BUG()
772 if (!ses || !ses->server)
775 mutex_lock(&ses->session_mutex);
776 if (ses->need_reconnect)
777 goto session_already_dead; /* no need to send SMBlogoff if uid
778 already closed due to reconnect */
779 rc = small_smb_init(SMB_COM_LOGOFF_ANDX, 2, NULL, (void **)&pSMB);
781 mutex_unlock(&ses->session_mutex);
785 pSMB->hdr.Mid = GetNextMid(ses->server);
787 if (ses->server->sec_mode &
788 (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
789 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
791 pSMB->hdr.Uid = ses->Suid;
793 pSMB->AndXCommand = 0xFF;
794 rc = SendReceiveNoRsp(xid, ses, (char *) pSMB, 0);
795 session_already_dead:
796 mutex_unlock(&ses->session_mutex);
798 /* if session dead then we do not need to do ulogoff,
799 since server closed smb session, no sense reporting
807 CIFSPOSIXDelFile(const int xid, struct cifs_tcon *tcon, const char *fileName,
808 __u16 type, const struct nls_table *nls_codepage, int remap)
810 TRANSACTION2_SPI_REQ *pSMB = NULL;
811 TRANSACTION2_SPI_RSP *pSMBr = NULL;
812 struct unlink_psx_rq *pRqD;
815 int bytes_returned = 0;
816 __u16 params, param_offset, offset, byte_count;
818 cFYI(1, "In POSIX delete");
820 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
825 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
827 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
828 PATH_MAX, nls_codepage, remap);
829 name_len++; /* trailing null */
831 } else { /* BB add path length overrun check */
832 name_len = strnlen(fileName, PATH_MAX);
833 name_len++; /* trailing null */
834 strncpy(pSMB->FileName, fileName, name_len);
837 params = 6 + name_len;
838 pSMB->MaxParameterCount = cpu_to_le16(2);
839 pSMB->MaxDataCount = 0; /* BB double check this with jra */
840 pSMB->MaxSetupCount = 0;
845 param_offset = offsetof(struct smb_com_transaction2_spi_req,
846 InformationLevel) - 4;
847 offset = param_offset + params;
849 /* Setup pointer to Request Data (inode type) */
850 pRqD = (struct unlink_psx_rq *)(((char *)&pSMB->hdr.Protocol) + offset);
851 pRqD->type = cpu_to_le16(type);
852 pSMB->ParameterOffset = cpu_to_le16(param_offset);
853 pSMB->DataOffset = cpu_to_le16(offset);
854 pSMB->SetupCount = 1;
856 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
857 byte_count = 3 /* pad */ + params + sizeof(struct unlink_psx_rq);
859 pSMB->DataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
860 pSMB->TotalDataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
861 pSMB->ParameterCount = cpu_to_le16(params);
862 pSMB->TotalParameterCount = pSMB->ParameterCount;
863 pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_UNLINK);
865 inc_rfc1001_len(pSMB, byte_count);
866 pSMB->ByteCount = cpu_to_le16(byte_count);
867 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
868 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
870 cFYI(1, "Posix delete returned %d", rc);
871 cifs_buf_release(pSMB);
873 cifs_stats_inc(&tcon->num_deletes);
882 CIFSSMBDelFile(const int xid, struct cifs_tcon *tcon, const char *fileName,
883 const struct nls_table *nls_codepage, int remap)
885 DELETE_FILE_REQ *pSMB = NULL;
886 DELETE_FILE_RSP *pSMBr = NULL;
892 rc = smb_init(SMB_COM_DELETE, 1, tcon, (void **) &pSMB,
897 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
899 cifsConvertToUTF16((__le16 *) pSMB->fileName, fileName,
900 PATH_MAX, nls_codepage, remap);
901 name_len++; /* trailing null */
903 } else { /* BB improve check for buffer overruns BB */
904 name_len = strnlen(fileName, PATH_MAX);
905 name_len++; /* trailing null */
906 strncpy(pSMB->fileName, fileName, name_len);
908 pSMB->SearchAttributes =
909 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM);
910 pSMB->BufferFormat = 0x04;
911 inc_rfc1001_len(pSMB, name_len + 1);
912 pSMB->ByteCount = cpu_to_le16(name_len + 1);
913 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
914 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
915 cifs_stats_inc(&tcon->num_deletes);
917 cFYI(1, "Error in RMFile = %d", rc);
919 cifs_buf_release(pSMB);
927 CIFSSMBRmDir(const int xid, struct cifs_tcon *tcon, const char *dirName,
928 const struct nls_table *nls_codepage, int remap)
930 DELETE_DIRECTORY_REQ *pSMB = NULL;
931 DELETE_DIRECTORY_RSP *pSMBr = NULL;
936 cFYI(1, "In CIFSSMBRmDir");
938 rc = smb_init(SMB_COM_DELETE_DIRECTORY, 0, tcon, (void **) &pSMB,
943 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
944 name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, dirName,
945 PATH_MAX, nls_codepage, remap);
946 name_len++; /* trailing null */
948 } else { /* BB improve check for buffer overruns BB */
949 name_len = strnlen(dirName, PATH_MAX);
950 name_len++; /* trailing null */
951 strncpy(pSMB->DirName, dirName, name_len);
954 pSMB->BufferFormat = 0x04;
955 inc_rfc1001_len(pSMB, name_len + 1);
956 pSMB->ByteCount = cpu_to_le16(name_len + 1);
957 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
958 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
959 cifs_stats_inc(&tcon->num_rmdirs);
961 cFYI(1, "Error in RMDir = %d", rc);
963 cifs_buf_release(pSMB);
970 CIFSSMBMkDir(const int xid, struct cifs_tcon *tcon,
971 const char *name, const struct nls_table *nls_codepage, int remap)
974 CREATE_DIRECTORY_REQ *pSMB = NULL;
975 CREATE_DIRECTORY_RSP *pSMBr = NULL;
979 cFYI(1, "In CIFSSMBMkDir");
981 rc = smb_init(SMB_COM_CREATE_DIRECTORY, 0, tcon, (void **) &pSMB,
986 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
987 name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, name,
988 PATH_MAX, nls_codepage, remap);
989 name_len++; /* trailing null */
991 } else { /* BB improve check for buffer overruns BB */
992 name_len = strnlen(name, PATH_MAX);
993 name_len++; /* trailing null */
994 strncpy(pSMB->DirName, name, name_len);
997 pSMB->BufferFormat = 0x04;
998 inc_rfc1001_len(pSMB, name_len + 1);
999 pSMB->ByteCount = cpu_to_le16(name_len + 1);
1000 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1001 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
1002 cifs_stats_inc(&tcon->num_mkdirs);
1004 cFYI(1, "Error in Mkdir = %d", rc);
1006 cifs_buf_release(pSMB);
1013 CIFSPOSIXCreate(const int xid, struct cifs_tcon *tcon, __u32 posix_flags,
1014 __u64 mode, __u16 *netfid, FILE_UNIX_BASIC_INFO *pRetData,
1015 __u32 *pOplock, const char *name,
1016 const struct nls_table *nls_codepage, int remap)
1018 TRANSACTION2_SPI_REQ *pSMB = NULL;
1019 TRANSACTION2_SPI_RSP *pSMBr = NULL;
1022 int bytes_returned = 0;
1023 __u16 params, param_offset, offset, byte_count, count;
1024 OPEN_PSX_REQ *pdata;
1025 OPEN_PSX_RSP *psx_rsp;
1027 cFYI(1, "In POSIX Create");
1029 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
1034 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1036 cifsConvertToUTF16((__le16 *) pSMB->FileName, name,
1037 PATH_MAX, nls_codepage, remap);
1038 name_len++; /* trailing null */
1040 } else { /* BB improve the check for buffer overruns BB */
1041 name_len = strnlen(name, PATH_MAX);
1042 name_len++; /* trailing null */
1043 strncpy(pSMB->FileName, name, name_len);
1046 params = 6 + name_len;
1047 count = sizeof(OPEN_PSX_REQ);
1048 pSMB->MaxParameterCount = cpu_to_le16(2);
1049 pSMB->MaxDataCount = cpu_to_le16(1000); /* large enough */
1050 pSMB->MaxSetupCount = 0;
1054 pSMB->Reserved2 = 0;
1055 param_offset = offsetof(struct smb_com_transaction2_spi_req,
1056 InformationLevel) - 4;
1057 offset = param_offset + params;
1058 pdata = (OPEN_PSX_REQ *)(((char *)&pSMB->hdr.Protocol) + offset);
1059 pdata->Level = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
1060 pdata->Permissions = cpu_to_le64(mode);
1061 pdata->PosixOpenFlags = cpu_to_le32(posix_flags);
1062 pdata->OpenFlags = cpu_to_le32(*pOplock);
1063 pSMB->ParameterOffset = cpu_to_le16(param_offset);
1064 pSMB->DataOffset = cpu_to_le16(offset);
1065 pSMB->SetupCount = 1;
1066 pSMB->Reserved3 = 0;
1067 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
1068 byte_count = 3 /* pad */ + params + count;
1070 pSMB->DataCount = cpu_to_le16(count);
1071 pSMB->ParameterCount = cpu_to_le16(params);
1072 pSMB->TotalDataCount = pSMB->DataCount;
1073 pSMB->TotalParameterCount = pSMB->ParameterCount;
1074 pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_OPEN);
1075 pSMB->Reserved4 = 0;
1076 inc_rfc1001_len(pSMB, byte_count);
1077 pSMB->ByteCount = cpu_to_le16(byte_count);
1078 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1079 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
1081 cFYI(1, "Posix create returned %d", rc);
1082 goto psx_create_err;
1085 cFYI(1, "copying inode info");
1086 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
1088 if (rc || get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)) {
1089 rc = -EIO; /* bad smb */
1090 goto psx_create_err;
1093 /* copy return information to pRetData */
1094 psx_rsp = (OPEN_PSX_RSP *)((char *) &pSMBr->hdr.Protocol
1095 + le16_to_cpu(pSMBr->t2.DataOffset));
1097 *pOplock = le16_to_cpu(psx_rsp->OplockFlags);
1099 *netfid = psx_rsp->Fid; /* cifs fid stays in le */
1100 /* Let caller know file was created so we can set the mode. */
1101 /* Do we care about the CreateAction in any other cases? */
1102 if (cpu_to_le32(FILE_CREATE) == psx_rsp->CreateAction)
1103 *pOplock |= CIFS_CREATE_ACTION;
1104 /* check to make sure response data is there */
1105 if (psx_rsp->ReturnedLevel != cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC)) {
1106 pRetData->Type = cpu_to_le32(-1); /* unknown */
1107 cFYI(DBG2, "unknown type");
1109 if (get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)
1110 + sizeof(FILE_UNIX_BASIC_INFO)) {
1111 cERROR(1, "Open response data too small");
1112 pRetData->Type = cpu_to_le32(-1);
1113 goto psx_create_err;
1115 memcpy((char *) pRetData,
1116 (char *)psx_rsp + sizeof(OPEN_PSX_RSP),
1117 sizeof(FILE_UNIX_BASIC_INFO));
1121 cifs_buf_release(pSMB);
1123 if (posix_flags & SMB_O_DIRECTORY)
1124 cifs_stats_inc(&tcon->num_posixmkdirs);
1126 cifs_stats_inc(&tcon->num_posixopens);
1134 static __u16 convert_disposition(int disposition)
1138 switch (disposition) {
1139 case FILE_SUPERSEDE:
1140 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1143 ofun = SMBOPEN_OAPPEND;
1146 ofun = SMBOPEN_OCREATE;
1149 ofun = SMBOPEN_OCREATE | SMBOPEN_OAPPEND;
1151 case FILE_OVERWRITE:
1152 ofun = SMBOPEN_OTRUNC;
1154 case FILE_OVERWRITE_IF:
1155 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1158 cFYI(1, "unknown disposition %d", disposition);
1159 ofun = SMBOPEN_OAPPEND; /* regular open */
1165 access_flags_to_smbopen_mode(const int access_flags)
1167 int masked_flags = access_flags & (GENERIC_READ | GENERIC_WRITE);
1169 if (masked_flags == GENERIC_READ)
1170 return SMBOPEN_READ;
1171 else if (masked_flags == GENERIC_WRITE)
1172 return SMBOPEN_WRITE;
1174 /* just go for read/write */
1175 return SMBOPEN_READWRITE;
1179 SMBLegacyOpen(const int xid, struct cifs_tcon *tcon,
1180 const char *fileName, const int openDisposition,
1181 const int access_flags, const int create_options, __u16 *netfid,
1182 int *pOplock, FILE_ALL_INFO *pfile_info,
1183 const struct nls_table *nls_codepage, int remap)
1186 OPENX_REQ *pSMB = NULL;
1187 OPENX_RSP *pSMBr = NULL;
1193 rc = smb_init(SMB_COM_OPEN_ANDX, 15, tcon, (void **) &pSMB,
1198 pSMB->AndXCommand = 0xFF; /* none */
1200 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1201 count = 1; /* account for one byte pad to word boundary */
1203 cifsConvertToUTF16((__le16 *) (pSMB->fileName + 1),
1204 fileName, PATH_MAX, nls_codepage, remap);
1205 name_len++; /* trailing null */
1207 } else { /* BB improve check for buffer overruns BB */
1208 count = 0; /* no pad */
1209 name_len = strnlen(fileName, PATH_MAX);
1210 name_len++; /* trailing null */
1211 strncpy(pSMB->fileName, fileName, name_len);
1213 if (*pOplock & REQ_OPLOCK)
1214 pSMB->OpenFlags = cpu_to_le16(REQ_OPLOCK);
1215 else if (*pOplock & REQ_BATCHOPLOCK)
1216 pSMB->OpenFlags = cpu_to_le16(REQ_BATCHOPLOCK);
1218 pSMB->OpenFlags |= cpu_to_le16(REQ_MORE_INFO);
1219 pSMB->Mode = cpu_to_le16(access_flags_to_smbopen_mode(access_flags));
1220 pSMB->Mode |= cpu_to_le16(0x40); /* deny none */
1221 /* set file as system file if special file such
1222 as fifo and server expecting SFU style and
1223 no Unix extensions */
1225 if (create_options & CREATE_OPTION_SPECIAL)
1226 pSMB->FileAttributes = cpu_to_le16(ATTR_SYSTEM);
1227 else /* BB FIXME BB */
1228 pSMB->FileAttributes = cpu_to_le16(0/*ATTR_NORMAL*/);
1230 if (create_options & CREATE_OPTION_READONLY)
1231 pSMB->FileAttributes |= cpu_to_le16(ATTR_READONLY);
1234 /* pSMB->CreateOptions = cpu_to_le32(create_options &
1235 CREATE_OPTIONS_MASK); */
1236 /* BB FIXME END BB */
1238 pSMB->Sattr = cpu_to_le16(ATTR_HIDDEN | ATTR_SYSTEM | ATTR_DIRECTORY);
1239 pSMB->OpenFunction = cpu_to_le16(convert_disposition(openDisposition));
1241 inc_rfc1001_len(pSMB, count);
1243 pSMB->ByteCount = cpu_to_le16(count);
1244 /* long_op set to 1 to allow for oplock break timeouts */
1245 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1246 (struct smb_hdr *)pSMBr, &bytes_returned, 0);
1247 cifs_stats_inc(&tcon->num_opens);
1249 cFYI(1, "Error in Open = %d", rc);
1251 /* BB verify if wct == 15 */
1253 /* *pOplock = pSMBr->OplockLevel; */ /* BB take from action field*/
1255 *netfid = pSMBr->Fid; /* cifs fid stays in le */
1256 /* Let caller know file was created so we can set the mode. */
1257 /* Do we care about the CreateAction in any other cases? */
1259 /* if (cpu_to_le32(FILE_CREATE) == pSMBr->CreateAction)
1260 *pOplock |= CIFS_CREATE_ACTION; */
1264 pfile_info->CreationTime = 0; /* BB convert CreateTime*/
1265 pfile_info->LastAccessTime = 0; /* BB fixme */
1266 pfile_info->LastWriteTime = 0; /* BB fixme */
1267 pfile_info->ChangeTime = 0; /* BB fixme */
1268 pfile_info->Attributes =
1269 cpu_to_le32(le16_to_cpu(pSMBr->FileAttributes));
1270 /* the file_info buf is endian converted by caller */
1271 pfile_info->AllocationSize =
1272 cpu_to_le64(le32_to_cpu(pSMBr->EndOfFile));
1273 pfile_info->EndOfFile = pfile_info->AllocationSize;
1274 pfile_info->NumberOfLinks = cpu_to_le32(1);
1275 pfile_info->DeletePending = 0;
1279 cifs_buf_release(pSMB);
1286 CIFSSMBOpen(const int xid, struct cifs_tcon *tcon,
1287 const char *fileName, const int openDisposition,
1288 const int access_flags, const int create_options, __u16 *netfid,
1289 int *pOplock, FILE_ALL_INFO *pfile_info,
1290 const struct nls_table *nls_codepage, int remap)
1293 OPEN_REQ *pSMB = NULL;
1294 OPEN_RSP *pSMBr = NULL;
1300 rc = smb_init(SMB_COM_NT_CREATE_ANDX, 24, tcon, (void **) &pSMB,
1305 pSMB->AndXCommand = 0xFF; /* none */
1307 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1308 count = 1; /* account for one byte pad to word boundary */
1310 cifsConvertToUTF16((__le16 *) (pSMB->fileName + 1),
1311 fileName, PATH_MAX, nls_codepage, remap);
1312 name_len++; /* trailing null */
1314 pSMB->NameLength = cpu_to_le16(name_len);
1315 } else { /* BB improve check for buffer overruns BB */
1316 count = 0; /* no pad */
1317 name_len = strnlen(fileName, PATH_MAX);
1318 name_len++; /* trailing null */
1319 pSMB->NameLength = cpu_to_le16(name_len);
1320 strncpy(pSMB->fileName, fileName, name_len);
1322 if (*pOplock & REQ_OPLOCK)
1323 pSMB->OpenFlags = cpu_to_le32(REQ_OPLOCK);
1324 else if (*pOplock & REQ_BATCHOPLOCK)
1325 pSMB->OpenFlags = cpu_to_le32(REQ_BATCHOPLOCK);
1326 pSMB->DesiredAccess = cpu_to_le32(access_flags);
1327 pSMB->AllocationSize = 0;
1328 /* set file as system file if special file such
1329 as fifo and server expecting SFU style and
1330 no Unix extensions */
1331 if (create_options & CREATE_OPTION_SPECIAL)
1332 pSMB->FileAttributes = cpu_to_le32(ATTR_SYSTEM);
1334 pSMB->FileAttributes = cpu_to_le32(ATTR_NORMAL);
1336 /* XP does not handle ATTR_POSIX_SEMANTICS */
1337 /* but it helps speed up case sensitive checks for other
1338 servers such as Samba */
1339 if (tcon->ses->capabilities & CAP_UNIX)
1340 pSMB->FileAttributes |= cpu_to_le32(ATTR_POSIX_SEMANTICS);
1342 if (create_options & CREATE_OPTION_READONLY)
1343 pSMB->FileAttributes |= cpu_to_le32(ATTR_READONLY);
1345 pSMB->ShareAccess = cpu_to_le32(FILE_SHARE_ALL);
1346 pSMB->CreateDisposition = cpu_to_le32(openDisposition);
1347 pSMB->CreateOptions = cpu_to_le32(create_options & CREATE_OPTIONS_MASK);
1348 /* BB Expirement with various impersonation levels and verify */
1349 pSMB->ImpersonationLevel = cpu_to_le32(SECURITY_IMPERSONATION);
1350 pSMB->SecurityFlags =
1351 SECURITY_CONTEXT_TRACKING | SECURITY_EFFECTIVE_ONLY;
1354 inc_rfc1001_len(pSMB, count);
1356 pSMB->ByteCount = cpu_to_le16(count);
1357 /* long_op set to 1 to allow for oplock break timeouts */
1358 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1359 (struct smb_hdr *)pSMBr, &bytes_returned, 0);
1360 cifs_stats_inc(&tcon->num_opens);
1362 cFYI(1, "Error in Open = %d", rc);
1364 *pOplock = pSMBr->OplockLevel; /* 1 byte no need to le_to_cpu */
1365 *netfid = pSMBr->Fid; /* cifs fid stays in le */
1366 /* Let caller know file was created so we can set the mode. */
1367 /* Do we care about the CreateAction in any other cases? */
1368 if (cpu_to_le32(FILE_CREATE) == pSMBr->CreateAction)
1369 *pOplock |= CIFS_CREATE_ACTION;
1371 memcpy((char *)pfile_info, (char *)&pSMBr->CreationTime,
1372 36 /* CreationTime to Attributes */);
1373 /* the file_info buf is endian converted by caller */
1374 pfile_info->AllocationSize = pSMBr->AllocationSize;
1375 pfile_info->EndOfFile = pSMBr->EndOfFile;
1376 pfile_info->NumberOfLinks = cpu_to_le32(1);
1377 pfile_info->DeletePending = 0;
1381 cifs_buf_release(pSMB);
1388 * Discard any remaining data in the current SMB. To do this, we borrow the
1392 cifs_readv_discard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1394 unsigned int rfclen = get_rfc1002_length(server->smallbuf);
1395 int remaining = rfclen + 4 - server->total_read;
1396 struct cifs_readdata *rdata = mid->callback_data;
1398 while (remaining > 0) {
1401 length = cifs_read_from_socket(server, server->bigbuf,
1402 min_t(unsigned int, remaining,
1403 CIFSMaxBufSize + MAX_HEADER_SIZE(server)));
1406 server->total_read += length;
1407 remaining -= length;
1410 dequeue_mid(mid, rdata->result);
1415 cifs_readv_receive(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1418 unsigned int data_offset, data_len;
1419 struct cifs_readdata *rdata = mid->callback_data;
1420 char *buf = server->smallbuf;
1421 unsigned int buflen = get_rfc1002_length(buf) + 4;
1423 cFYI(1, "%s: mid=%llu offset=%llu bytes=%u", __func__,
1424 mid->mid, rdata->offset, rdata->bytes);
1427 * read the rest of READ_RSP header (sans Data array), or whatever we
1428 * can if there's not enough data. At this point, we've read down to
1431 len = min_t(unsigned int, buflen, server->vals->read_rsp_size) -
1432 HEADER_SIZE(server) + 1;
1434 rdata->iov[0].iov_base = buf + HEADER_SIZE(server) - 1;
1435 rdata->iov[0].iov_len = len;
1437 length = cifs_readv_from_socket(server, rdata->iov, 1, len);
1440 server->total_read += length;
1442 /* Was the SMB read successful? */
1443 rdata->result = server->ops->map_error(buf, false);
1444 if (rdata->result != 0) {
1445 cFYI(1, "%s: server returned error %d", __func__,
1447 return cifs_readv_discard(server, mid);
1450 /* Is there enough to get to the rest of the READ_RSP header? */
1451 if (server->total_read < server->vals->read_rsp_size) {
1452 cFYI(1, "%s: server returned short header. got=%u expected=%zu",
1453 __func__, server->total_read,
1454 server->vals->read_rsp_size);
1455 rdata->result = -EIO;
1456 return cifs_readv_discard(server, mid);
1459 data_offset = server->ops->read_data_offset(buf) + 4;
1460 if (data_offset < server->total_read) {
1462 * win2k8 sometimes sends an offset of 0 when the read
1463 * is beyond the EOF. Treat it as if the data starts just after
1466 cFYI(1, "%s: data offset (%u) inside read response header",
1467 __func__, data_offset);
1468 data_offset = server->total_read;
1469 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
1470 /* data_offset is beyond the end of smallbuf */
1471 cFYI(1, "%s: data offset (%u) beyond end of smallbuf",
1472 __func__, data_offset);
1473 rdata->result = -EIO;
1474 return cifs_readv_discard(server, mid);
1477 cFYI(1, "%s: total_read=%u data_offset=%u", __func__,
1478 server->total_read, data_offset);
1480 len = data_offset - server->total_read;
1482 /* read any junk before data into the rest of smallbuf */
1483 rdata->iov[0].iov_base = buf + server->total_read;
1484 rdata->iov[0].iov_len = len;
1485 length = cifs_readv_from_socket(server, rdata->iov, 1, len);
1488 server->total_read += length;
1491 /* set up first iov for signature check */
1492 rdata->iov[0].iov_base = buf;
1493 rdata->iov[0].iov_len = server->total_read;
1494 cFYI(1, "0: iov_base=%p iov_len=%zu",
1495 rdata->iov[0].iov_base, rdata->iov[0].iov_len);
1497 /* how much data is in the response? */
1498 data_len = server->ops->read_data_length(buf);
1499 if (data_offset + data_len > buflen) {
1500 /* data_len is corrupt -- discard frame */
1501 rdata->result = -EIO;
1502 return cifs_readv_discard(server, mid);
1505 /* marshal up the page array */
1506 len = rdata->marshal_iov(rdata, data_len);
1509 /* issue the read if we have any iovecs left to fill */
1510 if (rdata->nr_iov > 1) {
1511 length = cifs_readv_from_socket(server, &rdata->iov[1],
1512 rdata->nr_iov - 1, len);
1515 server->total_read += length;
1520 rdata->bytes = length;
1522 cFYI(1, "total_read=%u buflen=%u remaining=%u", server->total_read,
1525 /* discard anything left over */
1526 if (server->total_read < buflen)
1527 return cifs_readv_discard(server, mid);
1529 dequeue_mid(mid, false);
1534 cifs_readv_callback(struct mid_q_entry *mid)
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;
1540 cFYI(1, "%s: mid=%llu state=%d result=%d bytes=%u", __func__,
1541 mid->mid, mid->mid_state, rdata->result, rdata->bytes);
1543 switch (mid->mid_state) {
1544 case MID_RESPONSE_RECEIVED:
1545 /* result already set, check signature */
1546 if (server->sec_mode &
1547 (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED)) {
1548 if (cifs_verify_signature(rdata->iov, rdata->nr_iov,
1549 server, mid->sequence_number + 1))
1550 cERROR(1, "Unexpected SMB signature");
1552 /* FIXME: should this be counted toward the initiating task? */
1553 task_io_account_read(rdata->bytes);
1554 cifs_stats_bytes_read(tcon, rdata->bytes);
1556 case MID_REQUEST_SUBMITTED:
1557 case MID_RETRY_NEEDED:
1558 rdata->result = -EAGAIN;
1561 rdata->result = -EIO;
1564 queue_work(cifsiod_wq, &rdata->work);
1565 DeleteMidQEntry(mid);
1566 add_credits(server, 1);
1569 /* cifs_async_readv - send an async write, and set up mid to handle result */
1571 cifs_async_readv(struct cifs_readdata *rdata)
1574 READ_REQ *smb = NULL;
1576 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
1578 cFYI(1, "%s: offset=%llu bytes=%u", __func__,
1579 rdata->offset, rdata->bytes);
1581 if (tcon->ses->capabilities & CAP_LARGE_FILES)
1584 wct = 10; /* old style read */
1585 if ((rdata->offset >> 32) > 0) {
1586 /* can not handle this big offset for old */
1591 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **)&smb);
1595 smb->hdr.Pid = cpu_to_le16((__u16)rdata->pid);
1596 smb->hdr.PidHigh = cpu_to_le16((__u16)(rdata->pid >> 16));
1598 smb->AndXCommand = 0xFF; /* none */
1599 smb->Fid = rdata->cfile->netfid;
1600 smb->OffsetLow = cpu_to_le32(rdata->offset & 0xFFFFFFFF);
1602 smb->OffsetHigh = cpu_to_le32(rdata->offset >> 32);
1604 smb->MaxCount = cpu_to_le16(rdata->bytes & 0xFFFF);
1605 smb->MaxCountHigh = cpu_to_le32(rdata->bytes >> 16);
1609 /* old style read */
1610 struct smb_com_readx_req *smbr =
1611 (struct smb_com_readx_req *)smb;
1612 smbr->ByteCount = 0;
1615 /* 4 for RFC1001 length + 1 for BCC */
1616 rdata->iov[0].iov_base = smb;
1617 rdata->iov[0].iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4;
1619 kref_get(&rdata->refcount);
1620 rc = cifs_call_async(tcon->ses->server, rdata->iov, 1,
1621 cifs_readv_receive, cifs_readv_callback,
1625 cifs_stats_inc(&tcon->num_reads);
1627 kref_put(&rdata->refcount, cifs_readdata_release);
1629 cifs_small_buf_release(smb);
1634 CIFSSMBRead(const int xid, struct cifs_io_parms *io_parms, unsigned int *nbytes,
1635 char **buf, int *pbuf_type)
1638 READ_REQ *pSMB = NULL;
1639 READ_RSP *pSMBr = NULL;
1640 char *pReadData = NULL;
1642 int resp_buf_type = 0;
1644 __u32 pid = io_parms->pid;
1645 __u16 netfid = io_parms->netfid;
1646 __u64 offset = io_parms->offset;
1647 struct cifs_tcon *tcon = io_parms->tcon;
1648 unsigned int count = io_parms->length;
1650 cFYI(1, "Reading %d bytes on fid %d", count, netfid);
1651 if (tcon->ses->capabilities & CAP_LARGE_FILES)
1654 wct = 10; /* old style read */
1655 if ((offset >> 32) > 0) {
1656 /* can not handle this big offset for old */
1662 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **) &pSMB);
1666 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1667 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1669 /* tcon and ses pointer are checked in smb_init */
1670 if (tcon->ses->server == NULL)
1671 return -ECONNABORTED;
1673 pSMB->AndXCommand = 0xFF; /* none */
1675 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
1677 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
1679 pSMB->Remaining = 0;
1680 pSMB->MaxCount = cpu_to_le16(count & 0xFFFF);
1681 pSMB->MaxCountHigh = cpu_to_le32(count >> 16);
1683 pSMB->ByteCount = 0; /* no need to do le conversion since 0 */
1685 /* old style read */
1686 struct smb_com_readx_req *pSMBW =
1687 (struct smb_com_readx_req *)pSMB;
1688 pSMBW->ByteCount = 0;
1691 iov[0].iov_base = (char *)pSMB;
1692 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
1693 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
1694 &resp_buf_type, CIFS_LOG_ERROR);
1695 cifs_stats_inc(&tcon->num_reads);
1696 pSMBr = (READ_RSP *)iov[0].iov_base;
1698 cERROR(1, "Send error in read = %d", rc);
1700 int data_length = le16_to_cpu(pSMBr->DataLengthHigh);
1701 data_length = data_length << 16;
1702 data_length += le16_to_cpu(pSMBr->DataLength);
1703 *nbytes = data_length;
1705 /*check that DataLength would not go beyond end of SMB */
1706 if ((data_length > CIFSMaxBufSize)
1707 || (data_length > count)) {
1708 cFYI(1, "bad length %d for count %d",
1709 data_length, count);
1713 pReadData = (char *) (&pSMBr->hdr.Protocol) +
1714 le16_to_cpu(pSMBr->DataOffset);
1715 /* if (rc = copy_to_user(buf, pReadData, data_length)) {
1716 cERROR(1, "Faulting on read rc = %d",rc);
1718 }*/ /* can not use copy_to_user when using page cache*/
1720 memcpy(*buf, pReadData, data_length);
1724 /* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
1726 if (resp_buf_type == CIFS_SMALL_BUFFER)
1727 cifs_small_buf_release(iov[0].iov_base);
1728 else if (resp_buf_type == CIFS_LARGE_BUFFER)
1729 cifs_buf_release(iov[0].iov_base);
1730 } else if (resp_buf_type != CIFS_NO_BUFFER) {
1731 /* return buffer to caller to free */
1732 *buf = iov[0].iov_base;
1733 if (resp_buf_type == CIFS_SMALL_BUFFER)
1734 *pbuf_type = CIFS_SMALL_BUFFER;
1735 else if (resp_buf_type == CIFS_LARGE_BUFFER)
1736 *pbuf_type = CIFS_LARGE_BUFFER;
1737 } /* else no valid buffer on return - leave as null */
1739 /* Note: On -EAGAIN error only caller can retry on handle based calls
1740 since file handle passed in no longer valid */
1746 CIFSSMBWrite(const int xid, struct cifs_io_parms *io_parms,
1747 unsigned int *nbytes, const char *buf,
1748 const char __user *ubuf, const int long_op)
1751 WRITE_REQ *pSMB = NULL;
1752 WRITE_RSP *pSMBr = NULL;
1753 int bytes_returned, wct;
1756 __u32 pid = io_parms->pid;
1757 __u16 netfid = io_parms->netfid;
1758 __u64 offset = io_parms->offset;
1759 struct cifs_tcon *tcon = io_parms->tcon;
1760 unsigned int count = io_parms->length;
1764 /* cFYI(1, "write at %lld %d bytes", offset, count);*/
1765 if (tcon->ses == NULL)
1766 return -ECONNABORTED;
1768 if (tcon->ses->capabilities & CAP_LARGE_FILES)
1772 if ((offset >> 32) > 0) {
1773 /* can not handle big offset for old srv */
1778 rc = smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB,
1783 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1784 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1786 /* tcon and ses pointer are checked in smb_init */
1787 if (tcon->ses->server == NULL)
1788 return -ECONNABORTED;
1790 pSMB->AndXCommand = 0xFF; /* none */
1792 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
1794 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
1796 pSMB->Reserved = 0xFFFFFFFF;
1797 pSMB->WriteMode = 0;
1798 pSMB->Remaining = 0;
1800 /* Can increase buffer size if buffer is big enough in some cases ie we
1801 can send more if LARGE_WRITE_X capability returned by the server and if
1802 our buffer is big enough or if we convert to iovecs on socket writes
1803 and eliminate the copy to the CIFS buffer */
1804 if (tcon->ses->capabilities & CAP_LARGE_WRITE_X) {
1805 bytes_sent = min_t(const unsigned int, CIFSMaxBufSize, count);
1807 bytes_sent = (tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE)
1811 if (bytes_sent > count)
1814 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
1816 memcpy(pSMB->Data, buf, bytes_sent);
1818 if (copy_from_user(pSMB->Data, ubuf, bytes_sent)) {
1819 cifs_buf_release(pSMB);
1822 } else if (count != 0) {
1824 cifs_buf_release(pSMB);
1826 } /* else setting file size with write of zero bytes */
1828 byte_count = bytes_sent + 1; /* pad */
1829 else /* wct == 12 */
1830 byte_count = bytes_sent + 5; /* bigger pad, smaller smb hdr */
1832 pSMB->DataLengthLow = cpu_to_le16(bytes_sent & 0xFFFF);
1833 pSMB->DataLengthHigh = cpu_to_le16(bytes_sent >> 16);
1834 inc_rfc1001_len(pSMB, byte_count);
1837 pSMB->ByteCount = cpu_to_le16(byte_count);
1838 else { /* old style write has byte count 4 bytes earlier
1840 struct smb_com_writex_req *pSMBW =
1841 (struct smb_com_writex_req *)pSMB;
1842 pSMBW->ByteCount = cpu_to_le16(byte_count);
1845 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1846 (struct smb_hdr *) pSMBr, &bytes_returned, long_op);
1847 cifs_stats_inc(&tcon->num_writes);
1849 cFYI(1, "Send error in write = %d", rc);
1851 *nbytes = le16_to_cpu(pSMBr->CountHigh);
1852 *nbytes = (*nbytes) << 16;
1853 *nbytes += le16_to_cpu(pSMBr->Count);
1856 * Mask off high 16 bits when bytes written as returned by the
1857 * server is greater than bytes requested by the client. Some
1858 * OS/2 servers are known to set incorrect CountHigh values.
1860 if (*nbytes > count)
1864 cifs_buf_release(pSMB);
1866 /* Note: On -EAGAIN error only caller can retry on handle based calls
1867 since file handle passed in no longer valid */
1873 cifs_writedata_release(struct kref *refcount)
1875 struct cifs_writedata *wdata = container_of(refcount,
1876 struct cifs_writedata, refcount);
1879 cifsFileInfo_put(wdata->cfile);
1885 * Write failed with a retryable error. Resend the write request. It's also
1886 * possible that the page was redirtied so re-clean the page.
1889 cifs_writev_requeue(struct cifs_writedata *wdata)
1892 struct inode *inode = wdata->cfile->dentry->d_inode;
1894 for (i = 0; i < wdata->nr_pages; i++) {
1895 lock_page(wdata->pages[i]);
1896 clear_page_dirty_for_io(wdata->pages[i]);
1900 rc = cifs_async_writev(wdata);
1901 } while (rc == -EAGAIN);
1903 for (i = 0; i < wdata->nr_pages; i++) {
1905 SetPageError(wdata->pages[i]);
1906 unlock_page(wdata->pages[i]);
1909 mapping_set_error(inode->i_mapping, rc);
1910 kref_put(&wdata->refcount, cifs_writedata_release);
1914 cifs_writev_complete(struct work_struct *work)
1916 struct cifs_writedata *wdata = container_of(work,
1917 struct cifs_writedata, work);
1918 struct inode *inode = wdata->cfile->dentry->d_inode;
1921 if (wdata->result == 0) {
1922 spin_lock(&inode->i_lock);
1923 cifs_update_eof(CIFS_I(inode), wdata->offset, wdata->bytes);
1924 spin_unlock(&inode->i_lock);
1925 cifs_stats_bytes_written(tlink_tcon(wdata->cfile->tlink),
1927 } else if (wdata->sync_mode == WB_SYNC_ALL && wdata->result == -EAGAIN)
1928 return cifs_writev_requeue(wdata);
1930 for (i = 0; i < wdata->nr_pages; i++) {
1931 struct page *page = wdata->pages[i];
1932 if (wdata->result == -EAGAIN)
1933 __set_page_dirty_nobuffers(page);
1934 else if (wdata->result < 0)
1936 end_page_writeback(page);
1937 page_cache_release(page);
1939 if (wdata->result != -EAGAIN)
1940 mapping_set_error(inode->i_mapping, wdata->result);
1941 kref_put(&wdata->refcount, cifs_writedata_release);
1944 struct cifs_writedata *
1945 cifs_writedata_alloc(unsigned int nr_pages, work_func_t complete)
1947 struct cifs_writedata *wdata;
1949 /* this would overflow */
1950 if (nr_pages == 0) {
1951 cERROR(1, "%s: called with nr_pages == 0!", __func__);
1955 /* writedata + number of page pointers */
1956 wdata = kzalloc(sizeof(*wdata) +
1957 sizeof(struct page *) * (nr_pages - 1), GFP_NOFS);
1958 if (wdata != NULL) {
1959 kref_init(&wdata->refcount);
1960 INIT_LIST_HEAD(&wdata->list);
1961 init_completion(&wdata->done);
1962 INIT_WORK(&wdata->work, complete);
1968 * Check the mid_state and signature on received buffer (if any), and queue the
1969 * workqueue completion task.
1972 cifs_writev_callback(struct mid_q_entry *mid)
1974 struct cifs_writedata *wdata = mid->callback_data;
1975 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
1976 unsigned int written;
1977 WRITE_RSP *smb = (WRITE_RSP *)mid->resp_buf;
1979 switch (mid->mid_state) {
1980 case MID_RESPONSE_RECEIVED:
1981 wdata->result = cifs_check_receive(mid, tcon->ses->server, 0);
1982 if (wdata->result != 0)
1985 written = le16_to_cpu(smb->CountHigh);
1987 written += le16_to_cpu(smb->Count);
1989 * Mask off high 16 bits when bytes written as returned
1990 * by the server is greater than bytes requested by the
1991 * client. OS/2 servers are known to set incorrect
1994 if (written > wdata->bytes)
1997 if (written < wdata->bytes)
1998 wdata->result = -ENOSPC;
2000 wdata->bytes = written;
2002 case MID_REQUEST_SUBMITTED:
2003 case MID_RETRY_NEEDED:
2004 wdata->result = -EAGAIN;
2007 wdata->result = -EIO;
2011 queue_work(cifsiod_wq, &wdata->work);
2012 DeleteMidQEntry(mid);
2013 add_credits(tcon->ses->server, 1);
2016 /* cifs_async_writev - send an async write, and set up mid to handle result */
2018 cifs_async_writev(struct cifs_writedata *wdata)
2020 int i, rc = -EACCES;
2021 WRITE_REQ *smb = NULL;
2023 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2024 struct kvec *iov = NULL;
2026 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
2030 if (wdata->offset >> 32 > 0) {
2031 /* can not handle big offset for old srv */
2036 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **)&smb);
2038 goto async_writev_out;
2040 /* 1 iov per page + 1 for header */
2041 iov = kzalloc((wdata->nr_pages + 1) * sizeof(*iov), GFP_NOFS);
2044 goto async_writev_out;
2047 smb->hdr.Pid = cpu_to_le16((__u16)wdata->pid);
2048 smb->hdr.PidHigh = cpu_to_le16((__u16)(wdata->pid >> 16));
2050 smb->AndXCommand = 0xFF; /* none */
2051 smb->Fid = wdata->cfile->netfid;
2052 smb->OffsetLow = cpu_to_le32(wdata->offset & 0xFFFFFFFF);
2054 smb->OffsetHigh = cpu_to_le32(wdata->offset >> 32);
2055 smb->Reserved = 0xFFFFFFFF;
2060 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
2062 /* 4 for RFC1001 length + 1 for BCC */
2063 iov[0].iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4 + 1;
2064 iov[0].iov_base = smb;
2067 * This function should marshal up the page array into the kvec
2068 * array, reserving [0] for the header. It should kmap the pages
2069 * and set the iov_len properly for each one. It may also set
2072 wdata->marshal_iov(iov, wdata);
2074 cFYI(1, "async write at %llu %u bytes", wdata->offset, wdata->bytes);
2076 smb->DataLengthLow = cpu_to_le16(wdata->bytes & 0xFFFF);
2077 smb->DataLengthHigh = cpu_to_le16(wdata->bytes >> 16);
2080 inc_rfc1001_len(&smb->hdr, wdata->bytes + 1);
2081 put_bcc(wdata->bytes + 1, &smb->hdr);
2084 struct smb_com_writex_req *smbw =
2085 (struct smb_com_writex_req *)smb;
2086 inc_rfc1001_len(&smbw->hdr, wdata->bytes + 5);
2087 put_bcc(wdata->bytes + 5, &smbw->hdr);
2088 iov[0].iov_len += 4; /* pad bigger by four bytes */
2091 kref_get(&wdata->refcount);
2092 rc = cifs_call_async(tcon->ses->server, iov, wdata->nr_pages + 1,
2093 NULL, cifs_writev_callback, wdata, false);
2096 cifs_stats_inc(&tcon->num_writes);
2098 kref_put(&wdata->refcount, cifs_writedata_release);
2100 /* send is done, unmap pages */
2101 for (i = 0; i < wdata->nr_pages; i++)
2102 kunmap(wdata->pages[i]);
2105 cifs_small_buf_release(smb);
2111 CIFSSMBWrite2(const int xid, struct cifs_io_parms *io_parms,
2112 unsigned int *nbytes, struct kvec *iov, int n_vec,
2116 WRITE_REQ *pSMB = NULL;
2119 int resp_buf_type = 0;
2120 __u32 pid = io_parms->pid;
2121 __u16 netfid = io_parms->netfid;
2122 __u64 offset = io_parms->offset;
2123 struct cifs_tcon *tcon = io_parms->tcon;
2124 unsigned int count = io_parms->length;
2128 cFYI(1, "write2 at %lld %d bytes", (long long)offset, count);
2130 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
2134 if ((offset >> 32) > 0) {
2135 /* can not handle big offset for old srv */
2139 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB);
2143 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
2144 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
2146 /* tcon and ses pointer are checked in smb_init */
2147 if (tcon->ses->server == NULL)
2148 return -ECONNABORTED;
2150 pSMB->AndXCommand = 0xFF; /* none */
2152 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
2154 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
2155 pSMB->Reserved = 0xFFFFFFFF;
2156 pSMB->WriteMode = 0;
2157 pSMB->Remaining = 0;
2160 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
2162 pSMB->DataLengthLow = cpu_to_le16(count & 0xFFFF);
2163 pSMB->DataLengthHigh = cpu_to_le16(count >> 16);
2164 /* header + 1 byte pad */
2165 smb_hdr_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 1;
2167 inc_rfc1001_len(pSMB, count + 1);
2168 else /* wct == 12 */
2169 inc_rfc1001_len(pSMB, count + 5); /* smb data starts later */
2171 pSMB->ByteCount = cpu_to_le16(count + 1);
2172 else /* wct == 12 */ /* bigger pad, smaller smb hdr, keep offset ok */ {
2173 struct smb_com_writex_req *pSMBW =
2174 (struct smb_com_writex_req *)pSMB;
2175 pSMBW->ByteCount = cpu_to_le16(count + 5);
2177 iov[0].iov_base = pSMB;
2179 iov[0].iov_len = smb_hdr_len + 4;
2180 else /* wct == 12 pad bigger by four bytes */
2181 iov[0].iov_len = smb_hdr_len + 8;
2184 rc = SendReceive2(xid, tcon->ses, iov, n_vec + 1, &resp_buf_type,
2186 cifs_stats_inc(&tcon->num_writes);
2188 cFYI(1, "Send error Write2 = %d", rc);
2189 } else if (resp_buf_type == 0) {
2190 /* presumably this can not happen, but best to be safe */
2193 WRITE_RSP *pSMBr = (WRITE_RSP *)iov[0].iov_base;
2194 *nbytes = le16_to_cpu(pSMBr->CountHigh);
2195 *nbytes = (*nbytes) << 16;
2196 *nbytes += le16_to_cpu(pSMBr->Count);
2199 * Mask off high 16 bits when bytes written as returned by the
2200 * server is greater than bytes requested by the client. OS/2
2201 * servers are known to set incorrect CountHigh values.
2203 if (*nbytes > count)
2207 /* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
2208 if (resp_buf_type == CIFS_SMALL_BUFFER)
2209 cifs_small_buf_release(iov[0].iov_base);
2210 else if (resp_buf_type == CIFS_LARGE_BUFFER)
2211 cifs_buf_release(iov[0].iov_base);
2213 /* Note: On -EAGAIN error only caller can retry on handle based calls
2214 since file handle passed in no longer valid */
2219 int cifs_lockv(const int xid, struct cifs_tcon *tcon, const __u16 netfid,
2220 const __u8 lock_type, const __u32 num_unlock,
2221 const __u32 num_lock, LOCKING_ANDX_RANGE *buf)
2224 LOCK_REQ *pSMB = NULL;
2229 cFYI(1, "cifs_lockv num lock %d num unlock %d", num_lock, num_unlock);
2231 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2236 pSMB->NumberOfLocks = cpu_to_le16(num_lock);
2237 pSMB->NumberOfUnlocks = cpu_to_le16(num_unlock);
2238 pSMB->LockType = lock_type;
2239 pSMB->AndXCommand = 0xFF; /* none */
2240 pSMB->Fid = netfid; /* netfid stays le */
2242 count = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2243 inc_rfc1001_len(pSMB, count);
2244 pSMB->ByteCount = cpu_to_le16(count);
2246 iov[0].iov_base = (char *)pSMB;
2247 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4 -
2248 (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2249 iov[1].iov_base = (char *)buf;
2250 iov[1].iov_len = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2252 cifs_stats_inc(&tcon->num_locks);
2253 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP);
2255 cFYI(1, "Send error in cifs_lockv = %d", rc);
2261 CIFSSMBLock(const int xid, struct cifs_tcon *tcon,
2262 const __u16 smb_file_id, const __u32 netpid, const __u64 len,
2263 const __u64 offset, const __u32 numUnlock,
2264 const __u32 numLock, const __u8 lockType,
2265 const bool waitFlag, const __u8 oplock_level)
2268 LOCK_REQ *pSMB = NULL;
2269 /* LOCK_RSP *pSMBr = NULL; */ /* No response data other than rc to parse */
2274 cFYI(1, "CIFSSMBLock timeout %d numLock %d", (int)waitFlag, numLock);
2275 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2280 if (lockType == LOCKING_ANDX_OPLOCK_RELEASE) {
2281 timeout = CIFS_ASYNC_OP; /* no response expected */
2283 } else if (waitFlag) {
2284 timeout = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
2285 pSMB->Timeout = cpu_to_le32(-1);/* blocking - do not time out */
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 */
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);
2309 inc_rfc1001_len(pSMB, count);
2310 pSMB->ByteCount = cpu_to_le16(count);
2313 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
2314 (struct smb_hdr *) pSMB, &bytes_returned);
2315 cifs_small_buf_release(pSMB);
2317 rc = SendReceiveNoRsp(xid, tcon->ses, (char *)pSMB, timeout);
2318 /* SMB buffer freed by function above */
2320 cifs_stats_inc(&tcon->num_locks);
2322 cFYI(1, "Send error in Lock = %d", rc);
2324 /* Note: On -EAGAIN error only caller can retry on handle based calls
2325 since file handle passed in no longer valid */
2330 CIFSSMBPosixLock(const int xid, struct cifs_tcon *tcon,
2331 const __u16 smb_file_id, const __u32 netpid, const int get_flag,
2332 const __u64 len, struct file_lock *pLockData,
2333 const __u16 lock_type, const bool waitFlag)
2335 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2336 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
2337 struct cifs_posix_lock *parm_data;
2340 int bytes_returned = 0;
2341 int resp_buf_type = 0;
2342 __u16 params, param_offset, offset, byte_count, count;
2345 cFYI(1, "Posix Lock");
2347 if (pLockData == NULL)
2350 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
2355 pSMBr = (struct smb_com_transaction2_sfi_rsp *)pSMB;
2358 pSMB->MaxSetupCount = 0;
2361 pSMB->Reserved2 = 0;
2362 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2363 offset = param_offset + params;
2365 count = sizeof(struct cifs_posix_lock);
2366 pSMB->MaxParameterCount = cpu_to_le16(2);
2367 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
2368 pSMB->SetupCount = 1;
2369 pSMB->Reserved3 = 0;
2371 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
2373 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2374 byte_count = 3 /* pad */ + params + count;
2375 pSMB->DataCount = cpu_to_le16(count);
2376 pSMB->ParameterCount = cpu_to_le16(params);
2377 pSMB->TotalDataCount = pSMB->DataCount;
2378 pSMB->TotalParameterCount = pSMB->ParameterCount;
2379 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2380 parm_data = (struct cifs_posix_lock *)
2381 (((char *) &pSMB->hdr.Protocol) + offset);
2383 parm_data->lock_type = cpu_to_le16(lock_type);
2385 timeout = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
2386 parm_data->lock_flags = cpu_to_le16(1);
2387 pSMB->Timeout = cpu_to_le32(-1);
2391 parm_data->pid = cpu_to_le32(netpid);
2392 parm_data->start = cpu_to_le64(pLockData->fl_start);
2393 parm_data->length = cpu_to_le64(len); /* normalize negative numbers */
2395 pSMB->DataOffset = cpu_to_le16(offset);
2396 pSMB->Fid = smb_file_id;
2397 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_LOCK);
2398 pSMB->Reserved4 = 0;
2399 inc_rfc1001_len(pSMB, byte_count);
2400 pSMB->ByteCount = cpu_to_le16(byte_count);
2402 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
2403 (struct smb_hdr *) pSMBr, &bytes_returned);
2405 iov[0].iov_base = (char *)pSMB;
2406 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
2407 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
2408 &resp_buf_type, timeout);
2409 pSMB = NULL; /* request buf already freed by SendReceive2. Do
2410 not try to free it twice below on exit */
2411 pSMBr = (struct smb_com_transaction2_sfi_rsp *)iov[0].iov_base;
2415 cFYI(1, "Send error in Posix Lock = %d", rc);
2416 } else if (get_flag) {
2417 /* lock structure can be returned on get */
2420 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
2422 if (rc || get_bcc(&pSMBr->hdr) < sizeof(*parm_data)) {
2423 rc = -EIO; /* bad smb */
2426 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
2427 data_count = le16_to_cpu(pSMBr->t2.DataCount);
2428 if (data_count < sizeof(struct cifs_posix_lock)) {
2432 parm_data = (struct cifs_posix_lock *)
2433 ((char *)&pSMBr->hdr.Protocol + data_offset);
2434 if (parm_data->lock_type == __constant_cpu_to_le16(CIFS_UNLCK))
2435 pLockData->fl_type = F_UNLCK;
2437 if (parm_data->lock_type ==
2438 __constant_cpu_to_le16(CIFS_RDLCK))
2439 pLockData->fl_type = F_RDLCK;
2440 else if (parm_data->lock_type ==
2441 __constant_cpu_to_le16(CIFS_WRLCK))
2442 pLockData->fl_type = F_WRLCK;
2444 pLockData->fl_start = le64_to_cpu(parm_data->start);
2445 pLockData->fl_end = pLockData->fl_start +
2446 le64_to_cpu(parm_data->length) - 1;
2447 pLockData->fl_pid = le32_to_cpu(parm_data->pid);
2453 cifs_small_buf_release(pSMB);
2455 if (resp_buf_type == CIFS_SMALL_BUFFER)
2456 cifs_small_buf_release(iov[0].iov_base);
2457 else if (resp_buf_type == CIFS_LARGE_BUFFER)
2458 cifs_buf_release(iov[0].iov_base);
2460 /* Note: On -EAGAIN error only caller can retry on handle based calls
2461 since file handle passed in no longer valid */
2468 CIFSSMBClose(const int xid, struct cifs_tcon *tcon, int smb_file_id)
2471 CLOSE_REQ *pSMB = NULL;
2472 cFYI(1, "In CIFSSMBClose");
2474 /* do not retry on dead session on close */
2475 rc = small_smb_init(SMB_COM_CLOSE, 3, tcon, (void **) &pSMB);
2481 pSMB->FileID = (__u16) smb_file_id;
2482 pSMB->LastWriteTime = 0xFFFFFFFF;
2483 pSMB->ByteCount = 0;
2484 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
2485 cifs_stats_inc(&tcon->num_closes);
2488 /* EINTR is expected when user ctl-c to kill app */
2489 cERROR(1, "Send error in Close = %d", rc);
2493 /* Since session is dead, file will be closed on server already */
2501 CIFSSMBFlush(const int xid, struct cifs_tcon *tcon, int smb_file_id)
2504 FLUSH_REQ *pSMB = NULL;
2505 cFYI(1, "In CIFSSMBFlush");
2507 rc = small_smb_init(SMB_COM_FLUSH, 1, tcon, (void **) &pSMB);
2511 pSMB->FileID = (__u16) smb_file_id;
2512 pSMB->ByteCount = 0;
2513 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
2514 cifs_stats_inc(&tcon->num_flushes);
2516 cERROR(1, "Send error in Flush = %d", rc);
2522 CIFSSMBRename(const int xid, struct cifs_tcon *tcon,
2523 const char *fromName, const char *toName,
2524 const struct nls_table *nls_codepage, int remap)
2527 RENAME_REQ *pSMB = NULL;
2528 RENAME_RSP *pSMBr = NULL;
2530 int name_len, name_len2;
2533 cFYI(1, "In CIFSSMBRename");
2535 rc = smb_init(SMB_COM_RENAME, 1, tcon, (void **) &pSMB,
2540 pSMB->BufferFormat = 0x04;
2541 pSMB->SearchAttributes =
2542 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
2545 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2547 cifsConvertToUTF16((__le16 *) pSMB->OldFileName, fromName,
2548 PATH_MAX, nls_codepage, remap);
2549 name_len++; /* trailing null */
2551 pSMB->OldFileName[name_len] = 0x04; /* pad */
2552 /* protocol requires ASCII signature byte on Unicode string */
2553 pSMB->OldFileName[name_len + 1] = 0x00;
2555 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2556 toName, PATH_MAX, nls_codepage, 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(fromName, PATH_MAX);
2561 name_len++; /* trailing null */
2562 strncpy(pSMB->OldFileName, fromName, name_len);
2563 name_len2 = strnlen(toName, PATH_MAX);
2564 name_len2++; /* trailing null */
2565 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2566 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
2567 name_len2++; /* trailing null */
2568 name_len2++; /* signature byte */
2571 count = 1 /* 1st signature byte */ + name_len + name_len2;
2572 inc_rfc1001_len(pSMB, count);
2573 pSMB->ByteCount = cpu_to_le16(count);
2575 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2576 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2577 cifs_stats_inc(&tcon->num_renames);
2579 cFYI(1, "Send error in rename = %d", rc);
2581 cifs_buf_release(pSMB);
2589 int CIFSSMBRenameOpenFile(const int xid, struct cifs_tcon *pTcon,
2590 int netfid, const char *target_name,
2591 const struct nls_table *nls_codepage, int remap)
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;
2597 char dummy_string[30];
2599 int bytes_returned = 0;
2601 __u16 params, param_offset, offset, count, byte_count;
2603 cFYI(1, "Rename to File by handle");
2604 rc = smb_init(SMB_COM_TRANSACTION2, 15, pTcon, (void **) &pSMB,
2610 pSMB->MaxSetupCount = 0;
2614 pSMB->Reserved2 = 0;
2615 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2616 offset = param_offset + params;
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);
2637 cifsConvertToUTF16((__le16 *)rename_info->target_name,
2638 dummy_string, 24, nls_codepage, remap);
2641 cifsConvertToUTF16((__le16 *)rename_info->target_name,
2642 target_name, PATH_MAX, nls_codepage,
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;
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->num_t2renames);
2660 cFYI(1, "Send error in Rename (by file handle) = %d", rc);
2662 cifs_buf_release(pSMB);
2664 /* Note: On -EAGAIN error only caller can retry on handle based calls
2665 since file handle passed in no longer valid */
2671 CIFSSMBCopy(const int xid, struct cifs_tcon *tcon, const char *fromName,
2672 const __u16 target_tid, const char *toName, const int flags,
2673 const struct nls_table *nls_codepage, int remap)
2676 COPY_REQ *pSMB = NULL;
2677 COPY_RSP *pSMBr = NULL;
2679 int name_len, name_len2;
2682 cFYI(1, "In CIFSSMBCopy");
2684 rc = smb_init(SMB_COM_COPY, 1, tcon, (void **) &pSMB,
2689 pSMB->BufferFormat = 0x04;
2690 pSMB->Tid2 = target_tid;
2692 pSMB->Flags = cpu_to_le16(flags & COPY_TREE);
2694 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2695 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2696 fromName, PATH_MAX, nls_codepage,
2698 name_len++; /* trailing null */
2700 pSMB->OldFileName[name_len] = 0x04; /* pad */
2701 /* protocol requires ASCII signature byte on Unicode string */
2702 pSMB->OldFileName[name_len + 1] = 0x00;
2704 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2705 toName, PATH_MAX, nls_codepage, remap);
2706 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2707 name_len2 *= 2; /* convert to bytes */
2708 } else { /* BB improve the check for buffer overruns BB */
2709 name_len = strnlen(fromName, PATH_MAX);
2710 name_len++; /* trailing null */
2711 strncpy(pSMB->OldFileName, fromName, name_len);
2712 name_len2 = strnlen(toName, PATH_MAX);
2713 name_len2++; /* trailing null */
2714 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2715 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
2716 name_len2++; /* trailing null */
2717 name_len2++; /* signature byte */
2720 count = 1 /* 1st signature byte */ + name_len + name_len2;
2721 inc_rfc1001_len(pSMB, count);
2722 pSMB->ByteCount = cpu_to_le16(count);
2724 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2725 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2727 cFYI(1, "Send error in copy = %d with %d files copied",
2728 rc, le16_to_cpu(pSMBr->CopyCount));
2730 cifs_buf_release(pSMB);
2739 CIFSUnixCreateSymLink(const int xid, struct cifs_tcon *tcon,
2740 const char *fromName, const char *toName,
2741 const struct nls_table *nls_codepage)
2743 TRANSACTION2_SPI_REQ *pSMB = NULL;
2744 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2747 int name_len_target;
2749 int bytes_returned = 0;
2750 __u16 params, param_offset, offset, byte_count;
2752 cFYI(1, "In Symlink Unix style");
2754 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2759 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2761 cifs_strtoUTF16((__le16 *) pSMB->FileName, fromName,
2762 /* find define for this maxpathcomponent */
2763 PATH_MAX, nls_codepage);
2764 name_len++; /* trailing null */
2767 } else { /* BB improve the check for buffer overruns BB */
2768 name_len = strnlen(fromName, PATH_MAX);
2769 name_len++; /* trailing null */
2770 strncpy(pSMB->FileName, fromName, name_len);
2772 params = 6 + name_len;
2773 pSMB->MaxSetupCount = 0;
2777 pSMB->Reserved2 = 0;
2778 param_offset = offsetof(struct smb_com_transaction2_spi_req,
2779 InformationLevel) - 4;
2780 offset = param_offset + params;
2782 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2783 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2785 cifs_strtoUTF16((__le16 *) data_offset, toName, PATH_MAX
2786 /* find define for this maxpathcomponent */
2788 name_len_target++; /* trailing null */
2789 name_len_target *= 2;
2790 } else { /* BB improve the check for buffer overruns BB */
2791 name_len_target = strnlen(toName, PATH_MAX);
2792 name_len_target++; /* trailing null */
2793 strncpy(data_offset, toName, name_len_target);
2796 pSMB->MaxParameterCount = cpu_to_le16(2);
2797 /* BB find exact max on data count below from sess */
2798 pSMB->MaxDataCount = cpu_to_le16(1000);
2799 pSMB->SetupCount = 1;
2800 pSMB->Reserved3 = 0;
2801 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2802 byte_count = 3 /* pad */ + params + name_len_target;
2803 pSMB->DataCount = cpu_to_le16(name_len_target);
2804 pSMB->ParameterCount = cpu_to_le16(params);
2805 pSMB->TotalDataCount = pSMB->DataCount;
2806 pSMB->TotalParameterCount = pSMB->ParameterCount;
2807 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2808 pSMB->DataOffset = cpu_to_le16(offset);
2809 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_LINK);
2810 pSMB->Reserved4 = 0;
2811 inc_rfc1001_len(pSMB, byte_count);
2812 pSMB->ByteCount = cpu_to_le16(byte_count);
2813 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2814 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2815 cifs_stats_inc(&tcon->num_symlinks);
2817 cFYI(1, "Send error in SetPathInfo create symlink = %d", rc);
2819 cifs_buf_release(pSMB);
2822 goto createSymLinkRetry;
2828 CIFSUnixCreateHardLink(const int xid, struct cifs_tcon *tcon,
2829 const char *fromName, const char *toName,
2830 const struct nls_table *nls_codepage, int remap)
2832 TRANSACTION2_SPI_REQ *pSMB = NULL;
2833 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2836 int name_len_target;
2838 int bytes_returned = 0;
2839 __u16 params, param_offset, offset, byte_count;
2841 cFYI(1, "In Create Hard link Unix style");
2842 createHardLinkRetry:
2843 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2848 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2849 name_len = cifsConvertToUTF16((__le16 *) pSMB->FileName, toName,
2850 PATH_MAX, nls_codepage, remap);
2851 name_len++; /* trailing null */
2854 } else { /* BB improve the check for buffer overruns BB */
2855 name_len = strnlen(toName, PATH_MAX);
2856 name_len++; /* trailing null */
2857 strncpy(pSMB->FileName, toName, name_len);
2859 params = 6 + name_len;
2860 pSMB->MaxSetupCount = 0;
2864 pSMB->Reserved2 = 0;
2865 param_offset = offsetof(struct smb_com_transaction2_spi_req,
2866 InformationLevel) - 4;
2867 offset = param_offset + params;
2869 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2870 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2872 cifsConvertToUTF16((__le16 *) data_offset, fromName,
2873 PATH_MAX, nls_codepage, remap);
2874 name_len_target++; /* trailing null */
2875 name_len_target *= 2;
2876 } else { /* BB improve the check for buffer overruns BB */
2877 name_len_target = strnlen(fromName, PATH_MAX);
2878 name_len_target++; /* trailing null */
2879 strncpy(data_offset, fromName, name_len_target);
2882 pSMB->MaxParameterCount = cpu_to_le16(2);
2883 /* BB find exact max on data count below from sess*/
2884 pSMB->MaxDataCount = cpu_to_le16(1000);
2885 pSMB->SetupCount = 1;
2886 pSMB->Reserved3 = 0;
2887 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2888 byte_count = 3 /* pad */ + params + name_len_target;
2889 pSMB->ParameterCount = cpu_to_le16(params);
2890 pSMB->TotalParameterCount = pSMB->ParameterCount;
2891 pSMB->DataCount = cpu_to_le16(name_len_target);
2892 pSMB->TotalDataCount = pSMB->DataCount;
2893 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2894 pSMB->DataOffset = cpu_to_le16(offset);
2895 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_HLINK);
2896 pSMB->Reserved4 = 0;
2897 inc_rfc1001_len(pSMB, byte_count);
2898 pSMB->ByteCount = cpu_to_le16(byte_count);
2899 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2900 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2901 cifs_stats_inc(&tcon->num_hardlinks);
2903 cFYI(1, "Send error in SetPathInfo (hard link) = %d", rc);
2905 cifs_buf_release(pSMB);
2907 goto createHardLinkRetry;
2913 CIFSCreateHardLink(const int xid, struct cifs_tcon *tcon,
2914 const char *fromName, const char *toName,
2915 const struct nls_table *nls_codepage, int remap)
2918 NT_RENAME_REQ *pSMB = NULL;
2919 RENAME_RSP *pSMBr = NULL;
2921 int name_len, name_len2;
2924 cFYI(1, "In CIFSCreateHardLink");
2925 winCreateHardLinkRetry:
2927 rc = smb_init(SMB_COM_NT_RENAME, 4, tcon, (void **) &pSMB,
2932 pSMB->SearchAttributes =
2933 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
2935 pSMB->Flags = cpu_to_le16(CREATE_HARD_LINK);
2936 pSMB->ClusterCount = 0;
2938 pSMB->BufferFormat = 0x04;
2940 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2942 cifsConvertToUTF16((__le16 *) pSMB->OldFileName, fromName,
2943 PATH_MAX, nls_codepage, remap);
2944 name_len++; /* trailing null */
2947 /* protocol specifies ASCII buffer format (0x04) for unicode */
2948 pSMB->OldFileName[name_len] = 0x04;
2949 pSMB->OldFileName[name_len + 1] = 0x00; /* pad */
2951 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2952 toName, PATH_MAX, nls_codepage, remap);
2953 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2954 name_len2 *= 2; /* convert to bytes */
2955 } else { /* BB improve the check for buffer overruns BB */
2956 name_len = strnlen(fromName, PATH_MAX);
2957 name_len++; /* trailing null */
2958 strncpy(pSMB->OldFileName, fromName, name_len);
2959 name_len2 = strnlen(toName, PATH_MAX);
2960 name_len2++; /* trailing null */
2961 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2962 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
2963 name_len2++; /* trailing null */
2964 name_len2++; /* signature byte */
2967 count = 1 /* string type byte */ + name_len + name_len2;
2968 inc_rfc1001_len(pSMB, count);
2969 pSMB->ByteCount = cpu_to_le16(count);
2971 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2972 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2973 cifs_stats_inc(&tcon->num_hardlinks);
2975 cFYI(1, "Send error in hard link (NT rename) = %d", rc);
2977 cifs_buf_release(pSMB);
2979 goto winCreateHardLinkRetry;
2985 CIFSSMBUnixQuerySymLink(const int xid, struct cifs_tcon *tcon,
2986 const unsigned char *searchName, char **symlinkinfo,
2987 const struct nls_table *nls_codepage)
2989 /* SMB_QUERY_FILE_UNIX_LINK */
2990 TRANSACTION2_QPI_REQ *pSMB = NULL;
2991 TRANSACTION2_QPI_RSP *pSMBr = NULL;
2995 __u16 params, byte_count;
2998 cFYI(1, "In QPathSymLinkInfo (Unix) for path %s", searchName);
3001 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3006 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3008 cifs_strtoUTF16((__le16 *) pSMB->FileName, searchName,
3009 PATH_MAX, nls_codepage);
3010 name_len++; /* trailing null */
3012 } else { /* BB improve the check for buffer overruns BB */
3013 name_len = strnlen(searchName, PATH_MAX);
3014 name_len++; /* trailing null */
3015 strncpy(pSMB->FileName, searchName, name_len);
3018 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3019 pSMB->TotalDataCount = 0;
3020 pSMB->MaxParameterCount = cpu_to_le16(2);
3021 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
3022 pSMB->MaxSetupCount = 0;
3026 pSMB->Reserved2 = 0;
3027 pSMB->ParameterOffset = cpu_to_le16(offsetof(
3028 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
3029 pSMB->DataCount = 0;
3030 pSMB->DataOffset = 0;
3031 pSMB->SetupCount = 1;
3032 pSMB->Reserved3 = 0;
3033 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3034 byte_count = params + 1 /* pad */ ;
3035 pSMB->TotalParameterCount = cpu_to_le16(params);
3036 pSMB->ParameterCount = pSMB->TotalParameterCount;
3037 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_LINK);
3038 pSMB->Reserved4 = 0;
3039 inc_rfc1001_len(pSMB, byte_count);
3040 pSMB->ByteCount = cpu_to_le16(byte_count);
3042 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3043 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3045 cFYI(1, "Send error in QuerySymLinkInfo = %d", rc);
3047 /* decode response */
3049 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
3050 /* BB also check enough total bytes returned */
3051 if (rc || get_bcc(&pSMBr->hdr) < 2)
3055 u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3057 data_start = ((char *) &pSMBr->hdr.Protocol) +
3058 le16_to_cpu(pSMBr->t2.DataOffset);
3060 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3065 /* BB FIXME investigate remapping reserved chars here */
3066 *symlinkinfo = cifs_strndup_from_utf16(data_start,
3067 count, is_unicode, nls_codepage);
3072 cifs_buf_release(pSMB);
3074 goto querySymLinkRetry;
3078 #ifdef CONFIG_CIFS_SYMLINK_EXPERIMENTAL
3080 * Recent Windows versions now create symlinks more frequently
3081 * and they use the "reparse point" mechanism below. We can of course
3082 * do symlinks nicely to Samba and other servers which support the
3083 * CIFS Unix Extensions and we can also do SFU symlinks and "client only"
3084 * "MF" symlinks optionally, but for recent Windows we really need to
3085 * reenable the code below and fix the cifs_symlink callers to handle this.
3086 * In the interim this code has been moved to its own config option so
3087 * it is not compiled in by default until callers fixed up and more tested.
3090 CIFSSMBQueryReparseLinkInfo(const int xid, struct cifs_tcon *tcon,
3091 const unsigned char *searchName,
3092 char *symlinkinfo, const int buflen, __u16 fid,
3093 const struct nls_table *nls_codepage)
3097 struct smb_com_transaction_ioctl_req *pSMB;
3098 struct smb_com_transaction_ioctl_rsp *pSMBr;
3100 cFYI(1, "In Windows reparse style QueryLink for path %s", searchName);
3101 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3106 pSMB->TotalParameterCount = 0 ;
3107 pSMB->TotalDataCount = 0;
3108 pSMB->MaxParameterCount = cpu_to_le32(2);
3109 /* BB find exact data count max from sess structure BB */
3110 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
3111 pSMB->MaxSetupCount = 4;
3113 pSMB->ParameterOffset = 0;
3114 pSMB->DataCount = 0;
3115 pSMB->DataOffset = 0;
3116 pSMB->SetupCount = 4;
3117 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
3118 pSMB->ParameterCount = pSMB->TotalParameterCount;
3119 pSMB->FunctionCode = cpu_to_le32(FSCTL_GET_REPARSE_POINT);
3120 pSMB->IsFsctl = 1; /* FSCTL */
3121 pSMB->IsRootFlag = 0;
3122 pSMB->Fid = fid; /* file handle always le */
3123 pSMB->ByteCount = 0;
3125 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3126 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3128 cFYI(1, "Send error in QueryReparseLinkInfo = %d", rc);
3129 } else { /* decode response */
3130 __u32 data_offset = le32_to_cpu(pSMBr->DataOffset);
3131 __u32 data_count = le32_to_cpu(pSMBr->DataCount);
3132 if (get_bcc(&pSMBr->hdr) < 2 || data_offset > 512) {
3133 /* BB also check enough total bytes returned */
3134 rc = -EIO; /* bad smb */
3137 if (data_count && (data_count < 2048)) {
3138 char *end_of_smb = 2 /* sizeof byte count */ +
3139 get_bcc(&pSMBr->hdr) + (char *)&pSMBr->ByteCount;
3141 struct reparse_data *reparse_buf =
3142 (struct reparse_data *)
3143 ((char *)&pSMBr->hdr.Protocol
3145 if ((char *)reparse_buf >= end_of_smb) {
3149 if ((reparse_buf->LinkNamesBuf +
3150 reparse_buf->TargetNameOffset +
3151 reparse_buf->TargetNameLen) > end_of_smb) {
3152 cFYI(1, "reparse buf beyond SMB");
3157 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE) {
3158 cifs_from_ucs2(symlinkinfo, (__le16 *)
3159 (reparse_buf->LinkNamesBuf +
3160 reparse_buf->TargetNameOffset),
3162 reparse_buf->TargetNameLen,
3164 } else { /* ASCII names */
3165 strncpy(symlinkinfo,
3166 reparse_buf->LinkNamesBuf +
3167 reparse_buf->TargetNameOffset,
3168 min_t(const int, buflen,
3169 reparse_buf->TargetNameLen));
3173 cFYI(1, "Invalid return data count on "
3174 "get reparse info ioctl");
3176 symlinkinfo[buflen] = 0; /* just in case so the caller
3177 does not go off the end of the buffer */
3178 cFYI(1, "readlink result - %s", symlinkinfo);
3182 cifs_buf_release(pSMB);
3184 /* Note: On -EAGAIN error only caller can retry on handle based calls
3185 since file handle passed in no longer valid */
3189 #endif /* CIFS_SYMLINK_EXPERIMENTAL */ /* BB temporarily unused */
3191 #ifdef CONFIG_CIFS_POSIX
3193 /*Convert an Access Control Entry from wire format to local POSIX xattr format*/
3194 static void cifs_convert_ace(posix_acl_xattr_entry *ace,
3195 struct cifs_posix_ace *cifs_ace)
3197 /* u8 cifs fields do not need le conversion */
3198 ace->e_perm = cpu_to_le16(cifs_ace->cifs_e_perm);
3199 ace->e_tag = cpu_to_le16(cifs_ace->cifs_e_tag);
3200 ace->e_id = cpu_to_le32(le64_to_cpu(cifs_ace->cifs_uid));
3201 /* cFYI(1, "perm %d tag %d id %d",ace->e_perm,ace->e_tag,ace->e_id); */
3206 /* Convert ACL from CIFS POSIX wire format to local Linux POSIX ACL xattr */
3207 static int cifs_copy_posix_acl(char *trgt, char *src, const int buflen,
3208 const int acl_type, const int size_of_data_area)
3213 struct cifs_posix_ace *pACE;
3214 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)src;
3215 posix_acl_xattr_header *local_acl = (posix_acl_xattr_header *)trgt;
3217 if (le16_to_cpu(cifs_acl->version) != CIFS_ACL_VERSION)
3220 if (acl_type & ACL_TYPE_ACCESS) {
3221 count = le16_to_cpu(cifs_acl->access_entry_count);
3222 pACE = &cifs_acl->ace_array[0];
3223 size = sizeof(struct cifs_posix_acl);
3224 size += sizeof(struct cifs_posix_ace) * count;
3225 /* check if we would go beyond end of SMB */
3226 if (size_of_data_area < size) {
3227 cFYI(1, "bad CIFS POSIX ACL size %d vs. %d",
3228 size_of_data_area, size);
3231 } else if (acl_type & ACL_TYPE_DEFAULT) {
3232 count = le16_to_cpu(cifs_acl->access_entry_count);
3233 size = sizeof(struct cifs_posix_acl);
3234 size += sizeof(struct cifs_posix_ace) * count;
3235 /* skip past access ACEs to get to default ACEs */
3236 pACE = &cifs_acl->ace_array[count];
3237 count = le16_to_cpu(cifs_acl->default_entry_count);
3238 size += sizeof(struct cifs_posix_ace) * count;
3239 /* check if we would go beyond end of SMB */
3240 if (size_of_data_area < size)
3247 size = posix_acl_xattr_size(count);
3248 if ((buflen == 0) || (local_acl == NULL)) {
3249 /* used to query ACL EA size */
3250 } else if (size > buflen) {
3252 } else /* buffer big enough */ {
3253 local_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
3254 for (i = 0; i < count ; i++) {
3255 cifs_convert_ace(&local_acl->a_entries[i], pACE);
3262 static __u16 convert_ace_to_cifs_ace(struct cifs_posix_ace *cifs_ace,
3263 const posix_acl_xattr_entry *local_ace)
3265 __u16 rc = 0; /* 0 = ACL converted ok */
3267 cifs_ace->cifs_e_perm = le16_to_cpu(local_ace->e_perm);
3268 cifs_ace->cifs_e_tag = le16_to_cpu(local_ace->e_tag);
3269 /* BB is there a better way to handle the large uid? */
3270 if (local_ace->e_id == cpu_to_le32(-1)) {
3271 /* Probably no need to le convert -1 on any arch but can not hurt */
3272 cifs_ace->cifs_uid = cpu_to_le64(-1);
3274 cifs_ace->cifs_uid = cpu_to_le64(le32_to_cpu(local_ace->e_id));
3275 /*cFYI(1, "perm %d tag %d id %d",ace->e_perm,ace->e_tag,ace->e_id);*/
3279 /* Convert ACL from local Linux POSIX xattr to CIFS POSIX ACL wire format */
3280 static __u16 ACL_to_cifs_posix(char *parm_data, const char *pACL,
3281 const int buflen, const int acl_type)
3284 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)parm_data;
3285 posix_acl_xattr_header *local_acl = (posix_acl_xattr_header *)pACL;
3289 if ((buflen == 0) || (pACL == NULL) || (cifs_acl == NULL))
3292 count = posix_acl_xattr_count((size_t)buflen);
3293 cFYI(1, "setting acl with %d entries from buf of length %d and "
3295 count, buflen, le32_to_cpu(local_acl->a_version));
3296 if (le32_to_cpu(local_acl->a_version) != 2) {
3297 cFYI(1, "unknown POSIX ACL version %d",
3298 le32_to_cpu(local_acl->a_version));
3301 cifs_acl->version = cpu_to_le16(1);
3302 if (acl_type == ACL_TYPE_ACCESS)
3303 cifs_acl->access_entry_count = cpu_to_le16(count);
3304 else if (acl_type == ACL_TYPE_DEFAULT)
3305 cifs_acl->default_entry_count = cpu_to_le16(count);
3307 cFYI(1, "unknown ACL type %d", acl_type);
3310 for (i = 0; i < count; i++) {
3311 rc = convert_ace_to_cifs_ace(&cifs_acl->ace_array[i],
3312 &local_acl->a_entries[i]);
3314 /* ACE not converted */
3319 rc = (__u16)(count * sizeof(struct cifs_posix_ace));
3320 rc += sizeof(struct cifs_posix_acl);
3321 /* BB add check to make sure ACL does not overflow SMB */
3327 CIFSSMBGetPosixACL(const int xid, struct cifs_tcon *tcon,
3328 const unsigned char *searchName,
3329 char *acl_inf, const int buflen, const int acl_type,
3330 const struct nls_table *nls_codepage, int remap)
3332 /* SMB_QUERY_POSIX_ACL */
3333 TRANSACTION2_QPI_REQ *pSMB = NULL;
3334 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3338 __u16 params, byte_count;
3340 cFYI(1, "In GetPosixACL (Unix) for path %s", searchName);
3343 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3348 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3350 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3351 searchName, PATH_MAX, nls_codepage,
3353 name_len++; /* trailing null */
3355 pSMB->FileName[name_len] = 0;
3356 pSMB->FileName[name_len+1] = 0;
3357 } else { /* BB improve the check for buffer overruns BB */
3358 name_len = strnlen(searchName, PATH_MAX);
3359 name_len++; /* trailing null */
3360 strncpy(pSMB->FileName, searchName, name_len);
3363 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3364 pSMB->TotalDataCount = 0;
3365 pSMB->MaxParameterCount = cpu_to_le16(2);
3366 /* BB find exact max data count below from sess structure BB */
3367 pSMB->MaxDataCount = cpu_to_le16(4000);
3368 pSMB->MaxSetupCount = 0;
3372 pSMB->Reserved2 = 0;
3373 pSMB->ParameterOffset = cpu_to_le16(
3374 offsetof(struct smb_com_transaction2_qpi_req,
3375 InformationLevel) - 4);
3376 pSMB->DataCount = 0;
3377 pSMB->DataOffset = 0;
3378 pSMB->SetupCount = 1;
3379 pSMB->Reserved3 = 0;
3380 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3381 byte_count = params + 1 /* pad */ ;
3382 pSMB->TotalParameterCount = cpu_to_le16(params);
3383 pSMB->ParameterCount = pSMB->TotalParameterCount;
3384 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_ACL);
3385 pSMB->Reserved4 = 0;
3386 inc_rfc1001_len(pSMB, byte_count);
3387 pSMB->ByteCount = cpu_to_le16(byte_count);
3389 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3390 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3391 cifs_stats_inc(&tcon->num_acl_get);
3393 cFYI(1, "Send error in Query POSIX ACL = %d", rc);
3395 /* decode response */
3397 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
3398 /* BB also check enough total bytes returned */
3399 if (rc || get_bcc(&pSMBr->hdr) < 2)
3400 rc = -EIO; /* bad smb */
3402 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3403 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3404 rc = cifs_copy_posix_acl(acl_inf,
3405 (char *)&pSMBr->hdr.Protocol+data_offset,
3406 buflen, acl_type, count);
3409 cifs_buf_release(pSMB);
3416 CIFSSMBSetPosixACL(const int xid, struct cifs_tcon *tcon,
3417 const unsigned char *fileName,
3418 const char *local_acl, const int buflen,
3420 const struct nls_table *nls_codepage, int remap)
3422 struct smb_com_transaction2_spi_req *pSMB = NULL;
3423 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
3427 int bytes_returned = 0;
3428 __u16 params, byte_count, data_count, param_offset, offset;
3430 cFYI(1, "In SetPosixACL (Unix) for path %s", fileName);
3432 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3436 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3438 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
3439 PATH_MAX, nls_codepage, remap);
3440 name_len++; /* trailing null */
3442 } else { /* BB improve the check for buffer overruns BB */
3443 name_len = strnlen(fileName, PATH_MAX);
3444 name_len++; /* trailing null */
3445 strncpy(pSMB->FileName, fileName, name_len);
3447 params = 6 + name_len;
3448 pSMB->MaxParameterCount = cpu_to_le16(2);
3449 /* BB find max SMB size from sess */
3450 pSMB->MaxDataCount = cpu_to_le16(1000);
3451 pSMB->MaxSetupCount = 0;
3455 pSMB->Reserved2 = 0;
3456 param_offset = offsetof(struct smb_com_transaction2_spi_req,
3457 InformationLevel) - 4;
3458 offset = param_offset + params;
3459 parm_data = ((char *) &pSMB->hdr.Protocol) + offset;
3460 pSMB->ParameterOffset = cpu_to_le16(param_offset);
3462 /* convert to on the wire format for POSIX ACL */
3463 data_count = ACL_to_cifs_posix(parm_data, local_acl, buflen, acl_type);
3465 if (data_count == 0) {
3467 goto setACLerrorExit;
3469 pSMB->DataOffset = cpu_to_le16(offset);
3470 pSMB->SetupCount = 1;
3471 pSMB->Reserved3 = 0;
3472 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
3473 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_ACL);
3474 byte_count = 3 /* pad */ + params + data_count;
3475 pSMB->DataCount = cpu_to_le16(data_count);
3476 pSMB->TotalDataCount = pSMB->DataCount;
3477 pSMB->ParameterCount = cpu_to_le16(params);
3478 pSMB->TotalParameterCount = pSMB->ParameterCount;
3479 pSMB->Reserved4 = 0;
3480 inc_rfc1001_len(pSMB, byte_count);
3481 pSMB->ByteCount = cpu_to_le16(byte_count);
3482 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3483 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3485 cFYI(1, "Set POSIX ACL returned %d", rc);
3488 cifs_buf_release(pSMB);
3494 /* BB fix tabs in this function FIXME BB */
3496 CIFSGetExtAttr(const int xid, struct cifs_tcon *tcon,
3497 const int netfid, __u64 *pExtAttrBits, __u64 *pMask)
3500 struct smb_t2_qfi_req *pSMB = NULL;
3501 struct smb_t2_qfi_rsp *pSMBr = NULL;
3503 __u16 params, byte_count;
3505 cFYI(1, "In GetExtAttr");
3510 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3515 params = 2 /* level */ + 2 /* fid */;
3516 pSMB->t2.TotalDataCount = 0;
3517 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
3518 /* BB find exact max data count below from sess structure BB */
3519 pSMB->t2.MaxDataCount = cpu_to_le16(4000);
3520 pSMB->t2.MaxSetupCount = 0;
3521 pSMB->t2.Reserved = 0;
3523 pSMB->t2.Timeout = 0;
3524 pSMB->t2.Reserved2 = 0;
3525 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
3527 pSMB->t2.DataCount = 0;
3528 pSMB->t2.DataOffset = 0;
3529 pSMB->t2.SetupCount = 1;
3530 pSMB->t2.Reserved3 = 0;
3531 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
3532 byte_count = params + 1 /* pad */ ;
3533 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
3534 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
3535 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_ATTR_FLAGS);
3538 inc_rfc1001_len(pSMB, byte_count);
3539 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
3541 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3542 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3544 cFYI(1, "error %d in GetExtAttr", rc);
3546 /* decode response */
3547 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
3548 /* BB also check enough total bytes returned */
3549 if (rc || get_bcc(&pSMBr->hdr) < 2)
3550 /* If rc should we check for EOPNOSUPP and
3551 disable the srvino flag? or in caller? */
3552 rc = -EIO; /* bad smb */
3554 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3555 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3556 struct file_chattr_info *pfinfo;
3557 /* BB Do we need a cast or hash here ? */
3559 cFYI(1, "Illegal size ret in GetExtAttr");
3563 pfinfo = (struct file_chattr_info *)
3564 (data_offset + (char *) &pSMBr->hdr.Protocol);
3565 *pExtAttrBits = le64_to_cpu(pfinfo->mode);
3566 *pMask = le64_to_cpu(pfinfo->mask);
3570 cifs_buf_release(pSMB);
3572 goto GetExtAttrRetry;
3576 #endif /* CONFIG_POSIX */
3578 #ifdef CONFIG_CIFS_ACL
3580 * Initialize NT TRANSACT SMB into small smb request buffer. This assumes that
3581 * all NT TRANSACTS that we init here have total parm and data under about 400
3582 * bytes (to fit in small cifs buffer size), which is the case so far, it
3583 * easily fits. NB: Setup words themselves and ByteCount MaxSetupCount (size of
3584 * returned setup area) and MaxParameterCount (returned parms size) must be set
3588 smb_init_nttransact(const __u16 sub_command, const int setup_count,
3589 const int parm_len, struct cifs_tcon *tcon,
3594 struct smb_com_ntransact_req *pSMB;
3596 rc = small_smb_init(SMB_COM_NT_TRANSACT, 19 + setup_count, tcon,
3600 *ret_buf = (void *)pSMB;
3602 pSMB->TotalParameterCount = cpu_to_le32(parm_len);
3603 pSMB->TotalDataCount = 0;
3604 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
3605 pSMB->ParameterCount = pSMB->TotalParameterCount;
3606 pSMB->DataCount = pSMB->TotalDataCount;
3607 temp_offset = offsetof(struct smb_com_ntransact_req, Parms) +
3608 (setup_count * 2) - 4 /* for rfc1001 length itself */;
3609 pSMB->ParameterOffset = cpu_to_le32(temp_offset);
3610 pSMB->DataOffset = cpu_to_le32(temp_offset + parm_len);
3611 pSMB->SetupCount = setup_count; /* no need to le convert byte fields */
3612 pSMB->SubCommand = cpu_to_le16(sub_command);
3617 validate_ntransact(char *buf, char **ppparm, char **ppdata,
3618 __u32 *pparmlen, __u32 *pdatalen)
3621 __u32 data_count, data_offset, parm_count, parm_offset;
3622 struct smb_com_ntransact_rsp *pSMBr;
3631 pSMBr = (struct smb_com_ntransact_rsp *)buf;
3633 bcc = get_bcc(&pSMBr->hdr);
3634 end_of_smb = 2 /* sizeof byte count */ + bcc +
3635 (char *)&pSMBr->ByteCount;
3637 data_offset = le32_to_cpu(pSMBr->DataOffset);
3638 data_count = le32_to_cpu(pSMBr->DataCount);
3639 parm_offset = le32_to_cpu(pSMBr->ParameterOffset);
3640 parm_count = le32_to_cpu(pSMBr->ParameterCount);
3642 *ppparm = (char *)&pSMBr->hdr.Protocol + parm_offset;
3643 *ppdata = (char *)&pSMBr->hdr.Protocol + data_offset;
3645 /* should we also check that parm and data areas do not overlap? */
3646 if (*ppparm > end_of_smb) {
3647 cFYI(1, "parms start after end of smb");
3649 } else if (parm_count + *ppparm > end_of_smb) {
3650 cFYI(1, "parm end after end of smb");
3652 } else if (*ppdata > end_of_smb) {
3653 cFYI(1, "data starts after end of smb");
3655 } else if (data_count + *ppdata > end_of_smb) {
3656 cFYI(1, "data %p + count %d (%p) past smb end %p start %p",
3657 *ppdata, data_count, (data_count + *ppdata),
3660 } else if (parm_count + data_count > bcc) {
3661 cFYI(1, "parm count and data count larger than SMB");
3664 *pdatalen = data_count;
3665 *pparmlen = parm_count;
3669 /* Get Security Descriptor (by handle) from remote server for a file or dir */
3671 CIFSSMBGetCIFSACL(const int xid, struct cifs_tcon *tcon, __u16 fid,
3672 struct cifs_ntsd **acl_inf, __u32 *pbuflen)
3676 QUERY_SEC_DESC_REQ *pSMB;
3679 cFYI(1, "GetCifsACL");
3684 rc = smb_init_nttransact(NT_TRANSACT_QUERY_SECURITY_DESC, 0,
3685 8 /* parm len */, tcon, (void **) &pSMB);
3689 pSMB->MaxParameterCount = cpu_to_le32(4);
3690 /* BB TEST with big acls that might need to be e.g. larger than 16K */
3691 pSMB->MaxSetupCount = 0;
3692 pSMB->Fid = fid; /* file handle always le */
3693 pSMB->AclFlags = cpu_to_le32(CIFS_ACL_OWNER | CIFS_ACL_GROUP |
3695 pSMB->ByteCount = cpu_to_le16(11); /* 3 bytes pad + 8 bytes parm */
3696 inc_rfc1001_len(pSMB, 11);
3697 iov[0].iov_base = (char *)pSMB;
3698 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
3700 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovec */, &buf_type,
3702 cifs_stats_inc(&tcon->num_acl_get);
3704 cFYI(1, "Send error in QuerySecDesc = %d", rc);
3705 } else { /* decode response */
3709 struct smb_com_ntransact_rsp *pSMBr;
3712 /* validate_nttransact */
3713 rc = validate_ntransact(iov[0].iov_base, (char **)&parm,
3714 &pdata, &parm_len, pbuflen);
3717 pSMBr = (struct smb_com_ntransact_rsp *)iov[0].iov_base;
3719 cFYI(1, "smb %p parm %p data %p", pSMBr, parm, *acl_inf);
3721 if (le32_to_cpu(pSMBr->ParameterCount) != 4) {
3722 rc = -EIO; /* bad smb */
3727 /* BB check that data area is minimum length and as big as acl_len */
3729 acl_len = le32_to_cpu(*parm);
3730 if (acl_len != *pbuflen) {
3731 cERROR(1, "acl length %d does not match %d",
3733 if (*pbuflen > acl_len)
3737 /* check if buffer is big enough for the acl
3738 header followed by the smallest SID */
3739 if ((*pbuflen < sizeof(struct cifs_ntsd) + 8) ||
3740 (*pbuflen >= 64 * 1024)) {
3741 cERROR(1, "bad acl length %d", *pbuflen);
3745 *acl_inf = kmalloc(*pbuflen, GFP_KERNEL);
3746 if (*acl_inf == NULL) {
3750 memcpy(*acl_inf, pdata, *pbuflen);
3754 if (buf_type == CIFS_SMALL_BUFFER)
3755 cifs_small_buf_release(iov[0].iov_base);
3756 else if (buf_type == CIFS_LARGE_BUFFER)
3757 cifs_buf_release(iov[0].iov_base);
3758 /* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
3763 CIFSSMBSetCIFSACL(const int xid, struct cifs_tcon *tcon, __u16 fid,
3764 struct cifs_ntsd *pntsd, __u32 acllen, int aclflag)
3766 __u16 byte_count, param_count, data_count, param_offset, data_offset;
3768 int bytes_returned = 0;
3769 SET_SEC_DESC_REQ *pSMB = NULL;
3773 rc = smb_init(SMB_COM_NT_TRANSACT, 19, tcon, (void **) &pSMB, &pSMBr);
3777 pSMB->MaxSetupCount = 0;
3781 param_offset = offsetof(struct smb_com_transaction_ssec_req, Fid) - 4;
3782 data_count = acllen;
3783 data_offset = param_offset + param_count;
3784 byte_count = 3 /* pad */ + param_count;
3786 pSMB->DataCount = cpu_to_le32(data_count);
3787 pSMB->TotalDataCount = pSMB->DataCount;
3788 pSMB->MaxParameterCount = cpu_to_le32(4);
3789 pSMB->MaxDataCount = cpu_to_le32(16384);
3790 pSMB->ParameterCount = cpu_to_le32(param_count);
3791 pSMB->ParameterOffset = cpu_to_le32(param_offset);
3792 pSMB->TotalParameterCount = pSMB->ParameterCount;
3793 pSMB->DataOffset = cpu_to_le32(data_offset);
3794 pSMB->SetupCount = 0;
3795 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_SET_SECURITY_DESC);
3796 pSMB->ByteCount = cpu_to_le16(byte_count+data_count);
3798 pSMB->Fid = fid; /* file handle always le */
3799 pSMB->Reserved2 = 0;
3800 pSMB->AclFlags = cpu_to_le32(aclflag);
3802 if (pntsd && acllen) {
3803 memcpy((char *)pSMBr + offsetof(struct smb_hdr, Protocol) +
3804 data_offset, pntsd, acllen);
3805 inc_rfc1001_len(pSMB, byte_count + data_count);
3807 inc_rfc1001_len(pSMB, byte_count);
3809 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3810 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3812 cFYI(1, "SetCIFSACL bytes_returned: %d, rc: %d", bytes_returned, rc);
3814 cFYI(1, "Set CIFS ACL returned %d", rc);
3815 cifs_buf_release(pSMB);
3818 goto setCifsAclRetry;
3823 #endif /* CONFIG_CIFS_ACL */
3825 /* Legacy Query Path Information call for lookup to old servers such
3827 int SMBQueryInformation(const int xid, struct cifs_tcon *tcon,
3828 const unsigned char *searchName,
3829 FILE_ALL_INFO *pFinfo,
3830 const struct nls_table *nls_codepage, int remap)
3832 QUERY_INFORMATION_REQ *pSMB;
3833 QUERY_INFORMATION_RSP *pSMBr;
3838 cFYI(1, "In SMBQPath path %s", searchName);
3840 rc = smb_init(SMB_COM_QUERY_INFORMATION, 0, tcon, (void **) &pSMB,
3845 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3847 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3848 searchName, PATH_MAX, nls_codepage,
3850 name_len++; /* trailing null */
3853 name_len = strnlen(searchName, PATH_MAX);
3854 name_len++; /* trailing null */
3855 strncpy(pSMB->FileName, searchName, name_len);
3857 pSMB->BufferFormat = 0x04;
3858 name_len++; /* account for buffer type byte */
3859 inc_rfc1001_len(pSMB, (__u16)name_len);
3860 pSMB->ByteCount = cpu_to_le16(name_len);
3862 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3863 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3865 cFYI(1, "Send error in QueryInfo = %d", rc);
3866 } else if (pFinfo) {
3868 __u32 time = le32_to_cpu(pSMBr->last_write_time);
3870 /* decode response */
3871 /* BB FIXME - add time zone adjustment BB */
3872 memset(pFinfo, 0, sizeof(FILE_ALL_INFO));
3875 /* decode time fields */
3876 pFinfo->ChangeTime = cpu_to_le64(cifs_UnixTimeToNT(ts));
3877 pFinfo->LastWriteTime = pFinfo->ChangeTime;
3878 pFinfo->LastAccessTime = 0;
3879 pFinfo->AllocationSize =
3880 cpu_to_le64(le32_to_cpu(pSMBr->size));
3881 pFinfo->EndOfFile = pFinfo->AllocationSize;
3882 pFinfo->Attributes =
3883 cpu_to_le32(le16_to_cpu(pSMBr->attr));
3885 rc = -EIO; /* bad buffer passed in */
3887 cifs_buf_release(pSMB);
3896 CIFSSMBQFileInfo(const int xid, struct cifs_tcon *tcon,
3897 u16 netfid, FILE_ALL_INFO *pFindData)
3899 struct smb_t2_qfi_req *pSMB = NULL;
3900 struct smb_t2_qfi_rsp *pSMBr = NULL;
3903 __u16 params, byte_count;
3906 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3911 params = 2 /* level */ + 2 /* fid */;
3912 pSMB->t2.TotalDataCount = 0;
3913 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
3914 /* BB find exact max data count below from sess structure BB */
3915 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
3916 pSMB->t2.MaxSetupCount = 0;
3917 pSMB->t2.Reserved = 0;
3919 pSMB->t2.Timeout = 0;
3920 pSMB->t2.Reserved2 = 0;
3921 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
3923 pSMB->t2.DataCount = 0;
3924 pSMB->t2.DataOffset = 0;
3925 pSMB->t2.SetupCount = 1;
3926 pSMB->t2.Reserved3 = 0;
3927 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
3928 byte_count = params + 1 /* pad */ ;
3929 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
3930 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
3931 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
3934 inc_rfc1001_len(pSMB, byte_count);
3936 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3937 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3939 cFYI(1, "Send error in QPathInfo = %d", rc);
3940 } else { /* decode response */
3941 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
3943 if (rc) /* BB add auto retry on EOPNOTSUPP? */
3945 else if (get_bcc(&pSMBr->hdr) < 40)
3946 rc = -EIO; /* bad smb */
3947 else if (pFindData) {
3948 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3949 memcpy((char *) pFindData,
3950 (char *) &pSMBr->hdr.Protocol +
3951 data_offset, sizeof(FILE_ALL_INFO));
3955 cifs_buf_release(pSMB);
3957 goto QFileInfoRetry;
3963 CIFSSMBQPathInfo(const int xid, struct cifs_tcon *tcon,
3964 const unsigned char *searchName,
3965 FILE_ALL_INFO *pFindData,
3966 int legacy /* old style infolevel */,
3967 const struct nls_table *nls_codepage, int remap)
3969 /* level 263 SMB_QUERY_FILE_ALL_INFO */
3970 TRANSACTION2_QPI_REQ *pSMB = NULL;
3971 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3975 __u16 params, byte_count;
3977 /* cFYI(1, "In QPathInfo path %s", searchName); */
3979 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3984 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3986 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
3987 PATH_MAX, nls_codepage, remap);
3988 name_len++; /* trailing null */
3990 } else { /* BB improve the check for buffer overruns BB */
3991 name_len = strnlen(searchName, PATH_MAX);
3992 name_len++; /* trailing null */
3993 strncpy(pSMB->FileName, searchName, name_len);
3996 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
3997 pSMB->TotalDataCount = 0;
3998 pSMB->MaxParameterCount = cpu_to_le16(2);
3999 /* BB find exact max SMB PDU from sess structure BB */
4000 pSMB->MaxDataCount = cpu_to_le16(4000);
4001 pSMB->MaxSetupCount = 0;
4005 pSMB->Reserved2 = 0;
4006 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4007 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
4008 pSMB->DataCount = 0;
4009 pSMB->DataOffset = 0;
4010 pSMB->SetupCount = 1;
4011 pSMB->Reserved3 = 0;
4012 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4013 byte_count = params + 1 /* pad */ ;
4014 pSMB->TotalParameterCount = cpu_to_le16(params);
4015 pSMB->ParameterCount = pSMB->TotalParameterCount;
4017 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_STANDARD);
4019 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
4020 pSMB->Reserved4 = 0;
4021 inc_rfc1001_len(pSMB, byte_count);
4022 pSMB->ByteCount = cpu_to_le16(byte_count);
4024 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4025 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4027 cFYI(1, "Send error in QPathInfo = %d", rc);
4028 } else { /* decode response */
4029 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4031 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4033 else if (!legacy && get_bcc(&pSMBr->hdr) < 40)
4034 rc = -EIO; /* bad smb */
4035 else if (legacy && get_bcc(&pSMBr->hdr) < 24)
4036 rc = -EIO; /* 24 or 26 expected but we do not read
4038 else if (pFindData) {
4040 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4042 /* On legacy responses we do not read the last field,
4043 EAsize, fortunately since it varies by subdialect and
4044 also note it differs on Set vs. Get, ie two bytes or 4
4045 bytes depending but we don't care here */
4047 size = sizeof(FILE_INFO_STANDARD);
4049 size = sizeof(FILE_ALL_INFO);
4050 memcpy((char *) pFindData,
4051 (char *) &pSMBr->hdr.Protocol +
4056 cifs_buf_release(pSMB);
4058 goto QPathInfoRetry;
4064 CIFSSMBUnixQFileInfo(const int xid, struct cifs_tcon *tcon,
4065 u16 netfid, FILE_UNIX_BASIC_INFO *pFindData)
4067 struct smb_t2_qfi_req *pSMB = NULL;
4068 struct smb_t2_qfi_rsp *pSMBr = NULL;
4071 __u16 params, byte_count;
4074 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4079 params = 2 /* level */ + 2 /* fid */;
4080 pSMB->t2.TotalDataCount = 0;
4081 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4082 /* BB find exact max data count below from sess structure BB */
4083 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4084 pSMB->t2.MaxSetupCount = 0;
4085 pSMB->t2.Reserved = 0;
4087 pSMB->t2.Timeout = 0;
4088 pSMB->t2.Reserved2 = 0;
4089 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4091 pSMB->t2.DataCount = 0;
4092 pSMB->t2.DataOffset = 0;
4093 pSMB->t2.SetupCount = 1;
4094 pSMB->t2.Reserved3 = 0;
4095 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4096 byte_count = params + 1 /* pad */ ;
4097 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4098 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4099 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4102 inc_rfc1001_len(pSMB, byte_count);
4104 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4105 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4107 cFYI(1, "Send error in QPathInfo = %d", rc);
4108 } else { /* decode response */
4109 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4111 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
4112 cERROR(1, "Malformed FILE_UNIX_BASIC_INFO response.\n"
4113 "Unix Extensions can be disabled on mount "
4114 "by specifying the nosfu mount option.");
4115 rc = -EIO; /* bad smb */
4117 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4118 memcpy((char *) pFindData,
4119 (char *) &pSMBr->hdr.Protocol +
4121 sizeof(FILE_UNIX_BASIC_INFO));
4125 cifs_buf_release(pSMB);
4127 goto UnixQFileInfoRetry;
4133 CIFSSMBUnixQPathInfo(const int xid, struct cifs_tcon *tcon,
4134 const unsigned char *searchName,
4135 FILE_UNIX_BASIC_INFO *pFindData,
4136 const struct nls_table *nls_codepage, int remap)
4138 /* SMB_QUERY_FILE_UNIX_BASIC */
4139 TRANSACTION2_QPI_REQ *pSMB = NULL;
4140 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4142 int bytes_returned = 0;
4144 __u16 params, byte_count;
4146 cFYI(1, "In QPathInfo (Unix) the path %s", searchName);
4148 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4153 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4155 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4156 PATH_MAX, nls_codepage, remap);
4157 name_len++; /* trailing null */
4159 } else { /* BB improve the check for buffer overruns BB */
4160 name_len = strnlen(searchName, PATH_MAX);
4161 name_len++; /* trailing null */
4162 strncpy(pSMB->FileName, searchName, name_len);
4165 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
4166 pSMB->TotalDataCount = 0;
4167 pSMB->MaxParameterCount = cpu_to_le16(2);
4168 /* BB find exact max SMB PDU from sess structure BB */
4169 pSMB->MaxDataCount = cpu_to_le16(4000);
4170 pSMB->MaxSetupCount = 0;
4174 pSMB->Reserved2 = 0;
4175 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4176 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
4177 pSMB->DataCount = 0;
4178 pSMB->DataOffset = 0;
4179 pSMB->SetupCount = 1;
4180 pSMB->Reserved3 = 0;
4181 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4182 byte_count = params + 1 /* pad */ ;
4183 pSMB->TotalParameterCount = cpu_to_le16(params);
4184 pSMB->ParameterCount = pSMB->TotalParameterCount;
4185 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4186 pSMB->Reserved4 = 0;
4187 inc_rfc1001_len(pSMB, byte_count);
4188 pSMB->ByteCount = cpu_to_le16(byte_count);
4190 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4191 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4193 cFYI(1, "Send error in QPathInfo = %d", rc);
4194 } else { /* decode response */
4195 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4197 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
4198 cERROR(1, "Malformed FILE_UNIX_BASIC_INFO response.\n"
4199 "Unix Extensions can be disabled on mount "
4200 "by specifying the nosfu mount option.");
4201 rc = -EIO; /* bad smb */
4203 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4204 memcpy((char *) pFindData,
4205 (char *) &pSMBr->hdr.Protocol +
4207 sizeof(FILE_UNIX_BASIC_INFO));
4210 cifs_buf_release(pSMB);
4212 goto UnixQPathInfoRetry;
4217 /* xid, tcon, searchName and codepage are input parms, rest are returned */
4219 CIFSFindFirst(const int xid, struct cifs_tcon *tcon,
4220 const char *searchName,
4221 const struct nls_table *nls_codepage,
4222 __u16 *pnetfid, __u16 search_flags,
4223 struct cifs_search_info *psrch_inf, int remap, const char dirsep)
4225 /* level 257 SMB_ */
4226 TRANSACTION2_FFIRST_REQ *pSMB = NULL;
4227 TRANSACTION2_FFIRST_RSP *pSMBr = NULL;
4228 T2_FFIRST_RSP_PARMS *parms;
4230 int bytes_returned = 0;
4232 __u16 params, byte_count;
4234 cFYI(1, "In FindFirst for %s", searchName);
4237 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4242 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4244 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4245 PATH_MAX, nls_codepage, remap);
4246 /* We can not add the asterik earlier in case
4247 it got remapped to 0xF03A as if it were part of the
4248 directory name instead of a wildcard */
4250 pSMB->FileName[name_len] = dirsep;
4251 pSMB->FileName[name_len+1] = 0;
4252 pSMB->FileName[name_len+2] = '*';
4253 pSMB->FileName[name_len+3] = 0;
4254 name_len += 4; /* now the trailing null */
4255 pSMB->FileName[name_len] = 0; /* null terminate just in case */
4256 pSMB->FileName[name_len+1] = 0;
4258 } else { /* BB add check for overrun of SMB buf BB */
4259 name_len = strnlen(searchName, PATH_MAX);
4260 /* BB fix here and in unicode clause above ie
4261 if (name_len > buffersize-header)
4262 free buffer exit; BB */
4263 strncpy(pSMB->FileName, searchName, name_len);
4264 pSMB->FileName[name_len] = dirsep;
4265 pSMB->FileName[name_len+1] = '*';
4266 pSMB->FileName[name_len+2] = 0;
4270 params = 12 + name_len /* includes null */ ;
4271 pSMB->TotalDataCount = 0; /* no EAs */
4272 pSMB->MaxParameterCount = cpu_to_le16(10);
4273 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
4274 pSMB->MaxSetupCount = 0;
4278 pSMB->Reserved2 = 0;
4279 byte_count = params + 1 /* pad */ ;
4280 pSMB->TotalParameterCount = cpu_to_le16(params);
4281 pSMB->ParameterCount = pSMB->TotalParameterCount;
4282 pSMB->ParameterOffset = cpu_to_le16(
4283 offsetof(struct smb_com_transaction2_ffirst_req, SearchAttributes)
4285 pSMB->DataCount = 0;
4286 pSMB->DataOffset = 0;
4287 pSMB->SetupCount = 1; /* one byte, no need to make endian neutral */
4288 pSMB->Reserved3 = 0;
4289 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_FIRST);
4290 pSMB->SearchAttributes =
4291 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
4293 pSMB->SearchCount = cpu_to_le16(CIFSMaxBufSize/sizeof(FILE_UNIX_INFO));
4294 pSMB->SearchFlags = cpu_to_le16(search_flags);
4295 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4297 /* BB what should we set StorageType to? Does it matter? BB */
4298 pSMB->SearchStorageType = 0;
4299 inc_rfc1001_len(pSMB, byte_count);
4300 pSMB->ByteCount = cpu_to_le16(byte_count);
4302 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4303 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4304 cifs_stats_inc(&tcon->num_ffirst);
4306 if (rc) {/* BB add logic to retry regular search if Unix search
4307 rejected unexpectedly by server */
4308 /* BB Add code to handle unsupported level rc */
4309 cFYI(1, "Error in FindFirst = %d", rc);
4311 cifs_buf_release(pSMB);
4313 /* BB eventually could optimize out free and realloc of buf */
4316 goto findFirstRetry;
4317 } else { /* decode response */
4318 /* BB remember to free buffer if error BB */
4319 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4323 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
4324 psrch_inf->unicode = true;
4326 psrch_inf->unicode = false;
4328 psrch_inf->ntwrk_buf_start = (char *)pSMBr;
4329 psrch_inf->smallBuf = 0;
4330 psrch_inf->srch_entries_start =
4331 (char *) &pSMBr->hdr.Protocol +
4332 le16_to_cpu(pSMBr->t2.DataOffset);
4333 parms = (T2_FFIRST_RSP_PARMS *)((char *) &pSMBr->hdr.Protocol +
4334 le16_to_cpu(pSMBr->t2.ParameterOffset));
4336 if (parms->EndofSearch)
4337 psrch_inf->endOfSearch = true;
4339 psrch_inf->endOfSearch = false;
4341 psrch_inf->entries_in_buffer =
4342 le16_to_cpu(parms->SearchCount);
4343 psrch_inf->index_of_last_entry = 2 /* skip . and .. */ +
4344 psrch_inf->entries_in_buffer;
4345 lnoff = le16_to_cpu(parms->LastNameOffset);
4346 if (CIFSMaxBufSize < lnoff) {
4347 cERROR(1, "ignoring corrupt resume name");
4348 psrch_inf->last_entry = NULL;
4352 psrch_inf->last_entry = psrch_inf->srch_entries_start +
4355 *pnetfid = parms->SearchHandle;
4357 cifs_buf_release(pSMB);
4364 int CIFSFindNext(const int xid, struct cifs_tcon *tcon, __u16 searchHandle,
4365 __u16 search_flags, struct cifs_search_info *psrch_inf)
4367 TRANSACTION2_FNEXT_REQ *pSMB = NULL;
4368 TRANSACTION2_FNEXT_RSP *pSMBr = NULL;
4369 T2_FNEXT_RSP_PARMS *parms;
4370 char *response_data;
4373 unsigned int name_len;
4374 __u16 params, byte_count;
4376 cFYI(1, "In FindNext");
4378 if (psrch_inf->endOfSearch)
4381 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4386 params = 14; /* includes 2 bytes of null string, converted to LE below*/
4388 pSMB->TotalDataCount = 0; /* no EAs */
4389 pSMB->MaxParameterCount = cpu_to_le16(8);
4390 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
4391 pSMB->MaxSetupCount = 0;
4395 pSMB->Reserved2 = 0;
4396 pSMB->ParameterOffset = cpu_to_le16(
4397 offsetof(struct smb_com_transaction2_fnext_req,SearchHandle) - 4);
4398 pSMB->DataCount = 0;
4399 pSMB->DataOffset = 0;
4400 pSMB->SetupCount = 1;
4401 pSMB->Reserved3 = 0;
4402 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_NEXT);
4403 pSMB->SearchHandle = searchHandle; /* always kept as le */
4405 cpu_to_le16(CIFSMaxBufSize / sizeof(FILE_UNIX_INFO));
4406 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4407 pSMB->ResumeKey = psrch_inf->resume_key;
4408 pSMB->SearchFlags = cpu_to_le16(search_flags);
4410 name_len = psrch_inf->resume_name_len;
4412 if (name_len < PATH_MAX) {
4413 memcpy(pSMB->ResumeFileName, psrch_inf->presume_name, name_len);
4414 byte_count += name_len;
4415 /* 14 byte parm len above enough for 2 byte null terminator */
4416 pSMB->ResumeFileName[name_len] = 0;
4417 pSMB->ResumeFileName[name_len+1] = 0;
4420 goto FNext2_err_exit;
4422 byte_count = params + 1 /* pad */ ;
4423 pSMB->TotalParameterCount = cpu_to_le16(params);
4424 pSMB->ParameterCount = pSMB->TotalParameterCount;
4425 inc_rfc1001_len(pSMB, byte_count);
4426 pSMB->ByteCount = cpu_to_le16(byte_count);
4428 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4429 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4430 cifs_stats_inc(&tcon->num_fnext);
4433 psrch_inf->endOfSearch = true;
4434 cifs_buf_release(pSMB);
4435 rc = 0; /* search probably was closed at end of search*/
4437 cFYI(1, "FindNext returned = %d", rc);
4438 } else { /* decode response */
4439 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4444 /* BB fixme add lock for file (srch_info) struct here */
4445 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
4446 psrch_inf->unicode = true;
4448 psrch_inf->unicode = false;
4449 response_data = (char *) &pSMBr->hdr.Protocol +
4450 le16_to_cpu(pSMBr->t2.ParameterOffset);
4451 parms = (T2_FNEXT_RSP_PARMS *)response_data;
4452 response_data = (char *)&pSMBr->hdr.Protocol +
4453 le16_to_cpu(pSMBr->t2.DataOffset);
4454 if (psrch_inf->smallBuf)
4455 cifs_small_buf_release(
4456 psrch_inf->ntwrk_buf_start);
4458 cifs_buf_release(psrch_inf->ntwrk_buf_start);
4459 psrch_inf->srch_entries_start = response_data;
4460 psrch_inf->ntwrk_buf_start = (char *)pSMB;
4461 psrch_inf->smallBuf = 0;
4462 if (parms->EndofSearch)
4463 psrch_inf->endOfSearch = true;
4465 psrch_inf->endOfSearch = false;
4466 psrch_inf->entries_in_buffer =
4467 le16_to_cpu(parms->SearchCount);
4468 psrch_inf->index_of_last_entry +=
4469 psrch_inf->entries_in_buffer;
4470 lnoff = le16_to_cpu(parms->LastNameOffset);
4471 if (CIFSMaxBufSize < lnoff) {
4472 cERROR(1, "ignoring corrupt resume name");
4473 psrch_inf->last_entry = NULL;
4476 psrch_inf->last_entry =
4477 psrch_inf->srch_entries_start + lnoff;
4479 /* cFYI(1, "fnxt2 entries in buf %d index_of_last %d",
4480 psrch_inf->entries_in_buffer, psrch_inf->index_of_last_entry); */
4482 /* BB fixme add unlock here */
4487 /* BB On error, should we leave previous search buf (and count and
4488 last entry fields) intact or free the previous one? */
4490 /* Note: On -EAGAIN error only caller can retry on handle based calls
4491 since file handle passed in no longer valid */
4494 cifs_buf_release(pSMB);
4499 CIFSFindClose(const int xid, struct cifs_tcon *tcon,
4500 const __u16 searchHandle)
4503 FINDCLOSE_REQ *pSMB = NULL;
4505 cFYI(1, "In CIFSSMBFindClose");
4506 rc = small_smb_init(SMB_COM_FIND_CLOSE2, 1, tcon, (void **)&pSMB);
4508 /* no sense returning error if session restarted
4509 as file handle has been closed */
4515 pSMB->FileID = searchHandle;
4516 pSMB->ByteCount = 0;
4517 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
4519 cERROR(1, "Send error in FindClose = %d", rc);
4521 cifs_stats_inc(&tcon->num_fclose);
4523 /* Since session is dead, search handle closed on server already */
4531 CIFSGetSrvInodeNumber(const int xid, struct cifs_tcon *tcon,
4532 const unsigned char *searchName,
4533 __u64 *inode_number,
4534 const struct nls_table *nls_codepage, int remap)
4537 TRANSACTION2_QPI_REQ *pSMB = NULL;
4538 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4539 int name_len, bytes_returned;
4540 __u16 params, byte_count;
4542 cFYI(1, "In GetSrvInodeNum for %s", searchName);
4546 GetInodeNumberRetry:
4547 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4552 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4554 cifsConvertToUTF16((__le16 *) pSMB->FileName,
4555 searchName, PATH_MAX, nls_codepage,
4557 name_len++; /* trailing null */
4559 } else { /* BB improve the check for buffer overruns BB */
4560 name_len = strnlen(searchName, PATH_MAX);
4561 name_len++; /* trailing null */
4562 strncpy(pSMB->FileName, searchName, name_len);
4565 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
4566 pSMB->TotalDataCount = 0;
4567 pSMB->MaxParameterCount = cpu_to_le16(2);
4568 /* BB find exact max data count below from sess structure BB */
4569 pSMB->MaxDataCount = cpu_to_le16(4000);
4570 pSMB->MaxSetupCount = 0;
4574 pSMB->Reserved2 = 0;
4575 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4576 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
4577 pSMB->DataCount = 0;
4578 pSMB->DataOffset = 0;
4579 pSMB->SetupCount = 1;
4580 pSMB->Reserved3 = 0;
4581 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4582 byte_count = params + 1 /* pad */ ;
4583 pSMB->TotalParameterCount = cpu_to_le16(params);
4584 pSMB->ParameterCount = pSMB->TotalParameterCount;
4585 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_INTERNAL_INFO);
4586 pSMB->Reserved4 = 0;
4587 inc_rfc1001_len(pSMB, byte_count);
4588 pSMB->ByteCount = cpu_to_le16(byte_count);
4590 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4591 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4593 cFYI(1, "error %d in QueryInternalInfo", rc);
4595 /* decode response */
4596 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4597 /* BB also check enough total bytes returned */
4598 if (rc || get_bcc(&pSMBr->hdr) < 2)
4599 /* If rc should we check for EOPNOSUPP and
4600 disable the srvino flag? or in caller? */
4601 rc = -EIO; /* bad smb */
4603 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4604 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
4605 struct file_internal_info *pfinfo;
4606 /* BB Do we need a cast or hash here ? */
4608 cFYI(1, "Illegal size ret in QryIntrnlInf");
4610 goto GetInodeNumOut;
4612 pfinfo = (struct file_internal_info *)
4613 (data_offset + (char *) &pSMBr->hdr.Protocol);
4614 *inode_number = le64_to_cpu(pfinfo->UniqueId);
4618 cifs_buf_release(pSMB);
4620 goto GetInodeNumberRetry;
4624 /* parses DFS refferal V3 structure
4625 * caller is responsible for freeing target_nodes
4628 * on failure - errno
4631 parse_DFS_referrals(TRANSACTION2_GET_DFS_REFER_RSP *pSMBr,
4632 unsigned int *num_of_nodes,
4633 struct dfs_info3_param **target_nodes,
4634 const struct nls_table *nls_codepage, int remap,
4635 const char *searchName)
4640 struct dfs_referral_level_3 *ref;
4642 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
4646 *num_of_nodes = le16_to_cpu(pSMBr->NumberOfReferrals);
4648 if (*num_of_nodes < 1) {
4649 cERROR(1, "num_referrals: must be at least > 0,"
4650 "but we get num_referrals = %d\n", *num_of_nodes);
4652 goto parse_DFS_referrals_exit;
4655 ref = (struct dfs_referral_level_3 *) &(pSMBr->referrals);
4656 if (ref->VersionNumber != cpu_to_le16(3)) {
4657 cERROR(1, "Referrals of V%d version are not supported,"
4658 "should be V3", le16_to_cpu(ref->VersionNumber));
4660 goto parse_DFS_referrals_exit;
4663 /* get the upper boundary of the resp buffer */
4664 data_end = (char *)(&(pSMBr->PathConsumed)) +
4665 le16_to_cpu(pSMBr->t2.DataCount);
4667 cFYI(1, "num_referrals: %d dfs flags: 0x%x ...\n",
4669 le32_to_cpu(pSMBr->DFSFlags));
4671 *target_nodes = kzalloc(sizeof(struct dfs_info3_param) *
4672 *num_of_nodes, GFP_KERNEL);
4673 if (*target_nodes == NULL) {
4674 cERROR(1, "Failed to allocate buffer for target_nodes\n");
4676 goto parse_DFS_referrals_exit;
4679 /* collect necessary data from referrals */
4680 for (i = 0; i < *num_of_nodes; i++) {
4683 struct dfs_info3_param *node = (*target_nodes)+i;
4685 node->flags = le32_to_cpu(pSMBr->DFSFlags);
4687 __le16 *tmp = kmalloc(strlen(searchName)*2 + 2,
4691 goto parse_DFS_referrals_exit;
4693 cifsConvertToUTF16((__le16 *) tmp, searchName,
4694 PATH_MAX, nls_codepage, remap);
4695 node->path_consumed = cifs_utf16_bytes(tmp,
4696 le16_to_cpu(pSMBr->PathConsumed),
4700 node->path_consumed = le16_to_cpu(pSMBr->PathConsumed);
4702 node->server_type = le16_to_cpu(ref->ServerType);
4703 node->ref_flag = le16_to_cpu(ref->ReferralEntryFlags);
4706 temp = (char *)ref + le16_to_cpu(ref->DfsPathOffset);
4707 max_len = data_end - temp;
4708 node->path_name = cifs_strndup_from_utf16(temp, max_len,
4709 is_unicode, nls_codepage);
4710 if (!node->path_name) {
4712 goto parse_DFS_referrals_exit;
4715 /* copy link target UNC */
4716 temp = (char *)ref + le16_to_cpu(ref->NetworkAddressOffset);
4717 max_len = data_end - temp;
4718 node->node_name = cifs_strndup_from_utf16(temp, max_len,
4719 is_unicode, nls_codepage);
4720 if (!node->node_name) {
4722 goto parse_DFS_referrals_exit;
4728 parse_DFS_referrals_exit:
4730 free_dfs_info_array(*target_nodes, *num_of_nodes);
4731 *target_nodes = NULL;
4738 CIFSGetDFSRefer(const int xid, struct cifs_ses *ses,
4739 const unsigned char *searchName,
4740 struct dfs_info3_param **target_nodes,
4741 unsigned int *num_of_nodes,
4742 const struct nls_table *nls_codepage, int remap)
4744 /* TRANS2_GET_DFS_REFERRAL */
4745 TRANSACTION2_GET_DFS_REFER_REQ *pSMB = NULL;
4746 TRANSACTION2_GET_DFS_REFER_RSP *pSMBr = NULL;
4750 __u16 params, byte_count;
4752 *target_nodes = NULL;
4754 cFYI(1, "In GetDFSRefer the path %s", searchName);
4758 rc = smb_init(SMB_COM_TRANSACTION2, 15, NULL, (void **) &pSMB,
4763 /* server pointer checked in called function,
4764 but should never be null here anyway */
4765 pSMB->hdr.Mid = GetNextMid(ses->server);
4766 pSMB->hdr.Tid = ses->ipc_tid;
4767 pSMB->hdr.Uid = ses->Suid;
4768 if (ses->capabilities & CAP_STATUS32)
4769 pSMB->hdr.Flags2 |= SMBFLG2_ERR_STATUS;
4770 if (ses->capabilities & CAP_DFS)
4771 pSMB->hdr.Flags2 |= SMBFLG2_DFS;
4773 if (ses->capabilities & CAP_UNICODE) {
4774 pSMB->hdr.Flags2 |= SMBFLG2_UNICODE;
4776 cifsConvertToUTF16((__le16 *) pSMB->RequestFileName,
4777 searchName, PATH_MAX, nls_codepage,
4779 name_len++; /* trailing null */
4781 } else { /* BB improve the check for buffer overruns BB */
4782 name_len = strnlen(searchName, PATH_MAX);
4783 name_len++; /* trailing null */
4784 strncpy(pSMB->RequestFileName, searchName, name_len);
4788 if (ses->server->sec_mode &
4789 (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
4790 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
4793 pSMB->hdr.Uid = ses->Suid;
4795 params = 2 /* level */ + name_len /*includes null */ ;
4796 pSMB->TotalDataCount = 0;
4797 pSMB->DataCount = 0;
4798 pSMB->DataOffset = 0;
4799 pSMB->MaxParameterCount = 0;
4800 /* BB find exact max SMB PDU from sess structure BB */
4801 pSMB->MaxDataCount = cpu_to_le16(4000);
4802 pSMB->MaxSetupCount = 0;
4806 pSMB->Reserved2 = 0;
4807 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4808 struct smb_com_transaction2_get_dfs_refer_req, MaxReferralLevel) - 4);
4809 pSMB->SetupCount = 1;
4810 pSMB->Reserved3 = 0;
4811 pSMB->SubCommand = cpu_to_le16(TRANS2_GET_DFS_REFERRAL);
4812 byte_count = params + 3 /* pad */ ;
4813 pSMB->ParameterCount = cpu_to_le16(params);
4814 pSMB->TotalParameterCount = pSMB->ParameterCount;
4815 pSMB->MaxReferralLevel = cpu_to_le16(3);
4816 inc_rfc1001_len(pSMB, byte_count);
4817 pSMB->ByteCount = cpu_to_le16(byte_count);
4819 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
4820 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4822 cFYI(1, "Send error in GetDFSRefer = %d", rc);
4825 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4827 /* BB Also check if enough total bytes returned? */
4828 if (rc || get_bcc(&pSMBr->hdr) < 17) {
4829 rc = -EIO; /* bad smb */
4833 cFYI(1, "Decoding GetDFSRefer response BCC: %d Offset %d",
4834 get_bcc(&pSMBr->hdr),
4835 le16_to_cpu(pSMBr->t2.DataOffset));
4837 /* parse returned result into more usable form */
4838 rc = parse_DFS_referrals(pSMBr, num_of_nodes,
4839 target_nodes, nls_codepage, remap,
4843 cifs_buf_release(pSMB);
4851 /* Query File System Info such as free space to old servers such as Win 9x */
4853 SMBOldQFSInfo(const int xid, struct cifs_tcon *tcon, struct kstatfs *FSData)
4855 /* level 0x01 SMB_QUERY_FILE_SYSTEM_INFO */
4856 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4857 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4858 FILE_SYSTEM_ALLOC_INFO *response_data;
4860 int bytes_returned = 0;
4861 __u16 params, byte_count;
4863 cFYI(1, "OldQFSInfo");
4865 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4870 params = 2; /* level */
4871 pSMB->TotalDataCount = 0;
4872 pSMB->MaxParameterCount = cpu_to_le16(2);
4873 pSMB->MaxDataCount = cpu_to_le16(1000);
4874 pSMB->MaxSetupCount = 0;
4878 pSMB->Reserved2 = 0;
4879 byte_count = params + 1 /* pad */ ;
4880 pSMB->TotalParameterCount = cpu_to_le16(params);
4881 pSMB->ParameterCount = pSMB->TotalParameterCount;
4882 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4883 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
4884 pSMB->DataCount = 0;
4885 pSMB->DataOffset = 0;
4886 pSMB->SetupCount = 1;
4887 pSMB->Reserved3 = 0;
4888 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
4889 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_ALLOCATION);
4890 inc_rfc1001_len(pSMB, byte_count);
4891 pSMB->ByteCount = cpu_to_le16(byte_count);
4893 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4894 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4896 cFYI(1, "Send error in QFSInfo = %d", rc);
4897 } else { /* decode response */
4898 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4900 if (rc || get_bcc(&pSMBr->hdr) < 18)
4901 rc = -EIO; /* bad smb */
4903 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4904 cFYI(1, "qfsinf resp BCC: %d Offset %d",
4905 get_bcc(&pSMBr->hdr), data_offset);
4907 response_data = (FILE_SYSTEM_ALLOC_INFO *)
4908 (((char *) &pSMBr->hdr.Protocol) + data_offset);
4910 le16_to_cpu(response_data->BytesPerSector) *
4911 le32_to_cpu(response_data->
4912 SectorsPerAllocationUnit);
4914 le32_to_cpu(response_data->TotalAllocationUnits);
4915 FSData->f_bfree = FSData->f_bavail =
4916 le32_to_cpu(response_data->FreeAllocationUnits);
4917 cFYI(1, "Blocks: %lld Free: %lld Block size %ld",
4918 (unsigned long long)FSData->f_blocks,
4919 (unsigned long long)FSData->f_bfree,
4923 cifs_buf_release(pSMB);
4926 goto oldQFSInfoRetry;
4932 CIFSSMBQFSInfo(const int xid, struct cifs_tcon *tcon, struct kstatfs *FSData)
4934 /* level 0x103 SMB_QUERY_FILE_SYSTEM_INFO */
4935 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4936 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4937 FILE_SYSTEM_INFO *response_data;
4939 int bytes_returned = 0;
4940 __u16 params, byte_count;
4942 cFYI(1, "In QFSInfo");
4944 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4949 params = 2; /* level */
4950 pSMB->TotalDataCount = 0;
4951 pSMB->MaxParameterCount = cpu_to_le16(2);
4952 pSMB->MaxDataCount = cpu_to_le16(1000);
4953 pSMB->MaxSetupCount = 0;
4957 pSMB->Reserved2 = 0;
4958 byte_count = params + 1 /* pad */ ;
4959 pSMB->TotalParameterCount = cpu_to_le16(params);
4960 pSMB->ParameterCount = pSMB->TotalParameterCount;
4961 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4962 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
4963 pSMB->DataCount = 0;
4964 pSMB->DataOffset = 0;
4965 pSMB->SetupCount = 1;
4966 pSMB->Reserved3 = 0;
4967 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
4968 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_SIZE_INFO);
4969 inc_rfc1001_len(pSMB, byte_count);
4970 pSMB->ByteCount = cpu_to_le16(byte_count);
4972 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4973 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4975 cFYI(1, "Send error in QFSInfo = %d", rc);
4976 } else { /* decode response */
4977 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4979 if (rc || get_bcc(&pSMBr->hdr) < 24)
4980 rc = -EIO; /* bad smb */
4982 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4986 *) (((char *) &pSMBr->hdr.Protocol) +
4989 le32_to_cpu(response_data->BytesPerSector) *
4990 le32_to_cpu(response_data->
4991 SectorsPerAllocationUnit);
4993 le64_to_cpu(response_data->TotalAllocationUnits);
4994 FSData->f_bfree = FSData->f_bavail =
4995 le64_to_cpu(response_data->FreeAllocationUnits);
4996 cFYI(1, "Blocks: %lld Free: %lld Block size %ld",
4997 (unsigned long long)FSData->f_blocks,
4998 (unsigned long long)FSData->f_bfree,
5002 cifs_buf_release(pSMB);
5011 CIFSSMBQFSAttributeInfo(const int xid, struct cifs_tcon *tcon)
5013 /* level 0x105 SMB_QUERY_FILE_SYSTEM_INFO */
5014 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5015 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5016 FILE_SYSTEM_ATTRIBUTE_INFO *response_data;
5018 int bytes_returned = 0;
5019 __u16 params, byte_count;
5021 cFYI(1, "In QFSAttributeInfo");
5023 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5028 params = 2; /* level */
5029 pSMB->TotalDataCount = 0;
5030 pSMB->MaxParameterCount = cpu_to_le16(2);
5031 /* BB find exact max SMB PDU from sess structure BB */
5032 pSMB->MaxDataCount = cpu_to_le16(1000);
5033 pSMB->MaxSetupCount = 0;
5037 pSMB->Reserved2 = 0;
5038 byte_count = params + 1 /* pad */ ;
5039 pSMB->TotalParameterCount = cpu_to_le16(params);
5040 pSMB->ParameterCount = pSMB->TotalParameterCount;
5041 pSMB->ParameterOffset = cpu_to_le16(offsetof(
5042 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
5043 pSMB->DataCount = 0;
5044 pSMB->DataOffset = 0;
5045 pSMB->SetupCount = 1;
5046 pSMB->Reserved3 = 0;
5047 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5048 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_ATTRIBUTE_INFO);
5049 inc_rfc1001_len(pSMB, byte_count);
5050 pSMB->ByteCount = cpu_to_le16(byte_count);
5052 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5053 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5055 cERROR(1, "Send error in QFSAttributeInfo = %d", rc);
5056 } else { /* decode response */
5057 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5059 if (rc || get_bcc(&pSMBr->hdr) < 13) {
5060 /* BB also check if enough bytes returned */
5061 rc = -EIO; /* bad smb */
5063 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5065 (FILE_SYSTEM_ATTRIBUTE_INFO
5066 *) (((char *) &pSMBr->hdr.Protocol) +
5068 memcpy(&tcon->fsAttrInfo, response_data,
5069 sizeof(FILE_SYSTEM_ATTRIBUTE_INFO));
5072 cifs_buf_release(pSMB);
5075 goto QFSAttributeRetry;
5081 CIFSSMBQFSDeviceInfo(const int xid, struct cifs_tcon *tcon)
5083 /* level 0x104 SMB_QUERY_FILE_SYSTEM_INFO */
5084 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5085 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5086 FILE_SYSTEM_DEVICE_INFO *response_data;
5088 int bytes_returned = 0;
5089 __u16 params, byte_count;
5091 cFYI(1, "In QFSDeviceInfo");
5093 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5098 params = 2; /* level */
5099 pSMB->TotalDataCount = 0;
5100 pSMB->MaxParameterCount = cpu_to_le16(2);
5101 /* BB find exact max SMB PDU from sess structure BB */
5102 pSMB->MaxDataCount = cpu_to_le16(1000);
5103 pSMB->MaxSetupCount = 0;
5107 pSMB->Reserved2 = 0;
5108 byte_count = params + 1 /* pad */ ;
5109 pSMB->TotalParameterCount = cpu_to_le16(params);
5110 pSMB->ParameterCount = pSMB->TotalParameterCount;
5111 pSMB->ParameterOffset = cpu_to_le16(offsetof(
5112 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
5114 pSMB->DataCount = 0;
5115 pSMB->DataOffset = 0;
5116 pSMB->SetupCount = 1;
5117 pSMB->Reserved3 = 0;
5118 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5119 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_DEVICE_INFO);
5120 inc_rfc1001_len(pSMB, byte_count);
5121 pSMB->ByteCount = cpu_to_le16(byte_count);
5123 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5124 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5126 cFYI(1, "Send error in QFSDeviceInfo = %d", rc);
5127 } else { /* decode response */
5128 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5130 if (rc || get_bcc(&pSMBr->hdr) <
5131 sizeof(FILE_SYSTEM_DEVICE_INFO))
5132 rc = -EIO; /* bad smb */
5134 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5136 (FILE_SYSTEM_DEVICE_INFO *)
5137 (((char *) &pSMBr->hdr.Protocol) +
5139 memcpy(&tcon->fsDevInfo, response_data,
5140 sizeof(FILE_SYSTEM_DEVICE_INFO));
5143 cifs_buf_release(pSMB);
5146 goto QFSDeviceRetry;
5152 CIFSSMBQFSUnixInfo(const int xid, struct cifs_tcon *tcon)
5154 /* level 0x200 SMB_QUERY_CIFS_UNIX_INFO */
5155 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5156 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5157 FILE_SYSTEM_UNIX_INFO *response_data;
5159 int bytes_returned = 0;
5160 __u16 params, byte_count;
5162 cFYI(1, "In QFSUnixInfo");
5164 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5165 (void **) &pSMB, (void **) &pSMBr);
5169 params = 2; /* level */
5170 pSMB->TotalDataCount = 0;
5171 pSMB->DataCount = 0;
5172 pSMB->DataOffset = 0;
5173 pSMB->MaxParameterCount = cpu_to_le16(2);
5174 /* BB find exact max SMB PDU from sess structure BB */
5175 pSMB->MaxDataCount = cpu_to_le16(100);
5176 pSMB->MaxSetupCount = 0;
5180 pSMB->Reserved2 = 0;
5181 byte_count = params + 1 /* pad */ ;
5182 pSMB->ParameterCount = cpu_to_le16(params);
5183 pSMB->TotalParameterCount = pSMB->ParameterCount;
5184 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5185 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
5186 pSMB->SetupCount = 1;
5187 pSMB->Reserved3 = 0;
5188 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5189 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_CIFS_UNIX_INFO);
5190 inc_rfc1001_len(pSMB, byte_count);
5191 pSMB->ByteCount = cpu_to_le16(byte_count);
5193 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5194 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5196 cERROR(1, "Send error in QFSUnixInfo = %d", rc);
5197 } else { /* decode response */
5198 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5200 if (rc || get_bcc(&pSMBr->hdr) < 13) {
5201 rc = -EIO; /* bad smb */
5203 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5205 (FILE_SYSTEM_UNIX_INFO
5206 *) (((char *) &pSMBr->hdr.Protocol) +
5208 memcpy(&tcon->fsUnixInfo, response_data,
5209 sizeof(FILE_SYSTEM_UNIX_INFO));
5212 cifs_buf_release(pSMB);
5222 CIFSSMBSetFSUnixInfo(const int xid, struct cifs_tcon *tcon, __u64 cap)
5224 /* level 0x200 SMB_SET_CIFS_UNIX_INFO */
5225 TRANSACTION2_SETFSI_REQ *pSMB = NULL;
5226 TRANSACTION2_SETFSI_RSP *pSMBr = NULL;
5228 int bytes_returned = 0;
5229 __u16 params, param_offset, offset, byte_count;
5231 cFYI(1, "In SETFSUnixInfo");
5233 /* BB switch to small buf init to save memory */
5234 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5235 (void **) &pSMB, (void **) &pSMBr);
5239 params = 4; /* 2 bytes zero followed by info level. */
5240 pSMB->MaxSetupCount = 0;
5244 pSMB->Reserved2 = 0;
5245 param_offset = offsetof(struct smb_com_transaction2_setfsi_req, FileNum)
5247 offset = param_offset + params;
5249 pSMB->MaxParameterCount = cpu_to_le16(4);
5250 /* BB find exact max SMB PDU from sess structure BB */
5251 pSMB->MaxDataCount = cpu_to_le16(100);
5252 pSMB->SetupCount = 1;
5253 pSMB->Reserved3 = 0;
5254 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FS_INFORMATION);
5255 byte_count = 1 /* pad */ + params + 12;
5257 pSMB->DataCount = cpu_to_le16(12);
5258 pSMB->ParameterCount = cpu_to_le16(params);
5259 pSMB->TotalDataCount = pSMB->DataCount;
5260 pSMB->TotalParameterCount = pSMB->ParameterCount;
5261 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5262 pSMB->DataOffset = cpu_to_le16(offset);
5266 pSMB->InformationLevel = cpu_to_le16(SMB_SET_CIFS_UNIX_INFO);
5269 pSMB->ClientUnixMajor = cpu_to_le16(CIFS_UNIX_MAJOR_VERSION);
5270 pSMB->ClientUnixMinor = cpu_to_le16(CIFS_UNIX_MINOR_VERSION);
5271 pSMB->ClientUnixCap = cpu_to_le64(cap);
5273 inc_rfc1001_len(pSMB, byte_count);
5274 pSMB->ByteCount = cpu_to_le16(byte_count);
5276 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5277 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5279 cERROR(1, "Send error in SETFSUnixInfo = %d", rc);
5280 } else { /* decode response */
5281 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5283 rc = -EIO; /* bad smb */
5285 cifs_buf_release(pSMB);
5288 goto SETFSUnixRetry;
5296 CIFSSMBQFSPosixInfo(const int xid, struct cifs_tcon *tcon,
5297 struct kstatfs *FSData)
5299 /* level 0x201 SMB_QUERY_CIFS_POSIX_INFO */
5300 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5301 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5302 FILE_SYSTEM_POSIX_INFO *response_data;
5304 int bytes_returned = 0;
5305 __u16 params, byte_count;
5307 cFYI(1, "In QFSPosixInfo");
5309 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5314 params = 2; /* level */
5315 pSMB->TotalDataCount = 0;
5316 pSMB->DataCount = 0;
5317 pSMB->DataOffset = 0;
5318 pSMB->MaxParameterCount = cpu_to_le16(2);
5319 /* BB find exact max SMB PDU from sess structure BB */
5320 pSMB->MaxDataCount = cpu_to_le16(100);
5321 pSMB->MaxSetupCount = 0;
5325 pSMB->Reserved2 = 0;
5326 byte_count = params + 1 /* pad */ ;
5327 pSMB->ParameterCount = cpu_to_le16(params);
5328 pSMB->TotalParameterCount = pSMB->ParameterCount;
5329 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5330 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
5331 pSMB->SetupCount = 1;
5332 pSMB->Reserved3 = 0;
5333 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5334 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_FS_INFO);
5335 inc_rfc1001_len(pSMB, byte_count);
5336 pSMB->ByteCount = cpu_to_le16(byte_count);
5338 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5339 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5341 cFYI(1, "Send error in QFSUnixInfo = %d", rc);
5342 } else { /* decode response */
5343 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5345 if (rc || get_bcc(&pSMBr->hdr) < 13) {
5346 rc = -EIO; /* bad smb */
5348 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5350 (FILE_SYSTEM_POSIX_INFO
5351 *) (((char *) &pSMBr->hdr.Protocol) +
5354 le32_to_cpu(response_data->BlockSize);
5356 le64_to_cpu(response_data->TotalBlocks);
5358 le64_to_cpu(response_data->BlocksAvail);
5359 if (response_data->UserBlocksAvail == cpu_to_le64(-1)) {
5360 FSData->f_bavail = FSData->f_bfree;
5363 le64_to_cpu(response_data->UserBlocksAvail);
5365 if (response_data->TotalFileNodes != cpu_to_le64(-1))
5367 le64_to_cpu(response_data->TotalFileNodes);
5368 if (response_data->FreeFileNodes != cpu_to_le64(-1))
5370 le64_to_cpu(response_data->FreeFileNodes);
5373 cifs_buf_release(pSMB);
5382 /* We can not use write of zero bytes trick to
5383 set file size due to need for large file support. Also note that
5384 this SetPathInfo is preferred to SetFileInfo based method in next
5385 routine which is only needed to work around a sharing violation bug
5386 in Samba which this routine can run into */
5389 CIFSSMBSetEOF(const int xid, struct cifs_tcon *tcon, const char *fileName,
5390 __u64 size, bool SetAllocation,
5391 const struct nls_table *nls_codepage, int remap)
5393 struct smb_com_transaction2_spi_req *pSMB = NULL;
5394 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
5395 struct file_end_of_file_info *parm_data;
5398 int bytes_returned = 0;
5399 __u16 params, byte_count, data_count, param_offset, offset;
5401 cFYI(1, "In SetEOF");
5403 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5408 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5410 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
5411 PATH_MAX, nls_codepage, remap);
5412 name_len++; /* trailing null */
5414 } else { /* BB improve the check for buffer overruns BB */
5415 name_len = strnlen(fileName, PATH_MAX);
5416 name_len++; /* trailing null */
5417 strncpy(pSMB->FileName, fileName, name_len);
5419 params = 6 + name_len;
5420 data_count = sizeof(struct file_end_of_file_info);
5421 pSMB->MaxParameterCount = cpu_to_le16(2);
5422 pSMB->MaxDataCount = cpu_to_le16(4100);
5423 pSMB->MaxSetupCount = 0;
5427 pSMB->Reserved2 = 0;
5428 param_offset = offsetof(struct smb_com_transaction2_spi_req,
5429 InformationLevel) - 4;
5430 offset = param_offset + params;
5431 if (SetAllocation) {
5432 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5433 pSMB->InformationLevel =
5434 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5436 pSMB->InformationLevel =
5437 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
5438 } else /* Set File Size */ {
5439 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5440 pSMB->InformationLevel =
5441 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
5443 pSMB->InformationLevel =
5444 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
5448 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol) +
5450 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5451 pSMB->DataOffset = cpu_to_le16(offset);
5452 pSMB->SetupCount = 1;
5453 pSMB->Reserved3 = 0;
5454 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5455 byte_count = 3 /* pad */ + params + data_count;
5456 pSMB->DataCount = cpu_to_le16(data_count);
5457 pSMB->TotalDataCount = pSMB->DataCount;
5458 pSMB->ParameterCount = cpu_to_le16(params);
5459 pSMB->TotalParameterCount = pSMB->ParameterCount;
5460 pSMB->Reserved4 = 0;
5461 inc_rfc1001_len(pSMB, byte_count);
5462 parm_data->FileSize = cpu_to_le64(size);
5463 pSMB->ByteCount = cpu_to_le16(byte_count);
5464 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5465 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5467 cFYI(1, "SetPathInfo (file size) returned %d", rc);
5469 cifs_buf_release(pSMB);
5478 CIFSSMBSetFileSize(const int xid, struct cifs_tcon *tcon, __u64 size,
5479 __u16 fid, __u32 pid_of_opener, bool SetAllocation)
5481 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5482 struct file_end_of_file_info *parm_data;
5484 __u16 params, param_offset, offset, byte_count, count;
5486 cFYI(1, "SetFileSize (via SetFileInfo) %lld",
5488 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5493 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5494 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5497 pSMB->MaxSetupCount = 0;
5501 pSMB->Reserved2 = 0;
5502 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5503 offset = param_offset + params;
5505 count = sizeof(struct file_end_of_file_info);
5506 pSMB->MaxParameterCount = cpu_to_le16(2);
5507 /* BB find exact max SMB PDU from sess structure BB */
5508 pSMB->MaxDataCount = cpu_to_le16(1000);
5509 pSMB->SetupCount = 1;
5510 pSMB->Reserved3 = 0;
5511 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5512 byte_count = 3 /* pad */ + params + count;
5513 pSMB->DataCount = cpu_to_le16(count);
5514 pSMB->ParameterCount = cpu_to_le16(params);
5515 pSMB->TotalDataCount = pSMB->DataCount;
5516 pSMB->TotalParameterCount = pSMB->ParameterCount;
5517 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5519 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol)
5521 pSMB->DataOffset = cpu_to_le16(offset);
5522 parm_data->FileSize = cpu_to_le64(size);
5524 if (SetAllocation) {
5525 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5526 pSMB->InformationLevel =
5527 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5529 pSMB->InformationLevel =
5530 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
5531 } else /* Set File Size */ {
5532 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5533 pSMB->InformationLevel =
5534 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
5536 pSMB->InformationLevel =
5537 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
5539 pSMB->Reserved4 = 0;
5540 inc_rfc1001_len(pSMB, byte_count);
5541 pSMB->ByteCount = cpu_to_le16(byte_count);
5542 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
5544 cFYI(1, "Send error in SetFileInfo (SetFileSize) = %d", rc);
5547 /* Note: On -EAGAIN error only caller can retry on handle based calls
5548 since file handle passed in no longer valid */
5553 /* Some legacy servers such as NT4 require that the file times be set on
5554 an open handle, rather than by pathname - this is awkward due to
5555 potential access conflicts on the open, but it is unavoidable for these
5556 old servers since the only other choice is to go from 100 nanosecond DCE
5557 time and resort to the original setpathinfo level which takes the ancient
5558 DOS time format with 2 second granularity */
5560 CIFSSMBSetFileInfo(const int xid, struct cifs_tcon *tcon,
5561 const FILE_BASIC_INFO *data, __u16 fid, __u32 pid_of_opener)
5563 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5566 __u16 params, param_offset, offset, byte_count, count;
5568 cFYI(1, "Set Times (via SetFileInfo)");
5569 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5574 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5575 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5578 pSMB->MaxSetupCount = 0;
5582 pSMB->Reserved2 = 0;
5583 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5584 offset = param_offset + params;
5586 data_offset = (char *)pSMB +
5587 offsetof(struct smb_hdr, Protocol) + offset;
5589 count = sizeof(FILE_BASIC_INFO);
5590 pSMB->MaxParameterCount = cpu_to_le16(2);
5591 /* BB find max SMB PDU from sess */
5592 pSMB->MaxDataCount = cpu_to_le16(1000);
5593 pSMB->SetupCount = 1;
5594 pSMB->Reserved3 = 0;
5595 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5596 byte_count = 3 /* pad */ + params + count;
5597 pSMB->DataCount = cpu_to_le16(count);
5598 pSMB->ParameterCount = cpu_to_le16(params);
5599 pSMB->TotalDataCount = pSMB->DataCount;
5600 pSMB->TotalParameterCount = pSMB->ParameterCount;
5601 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5602 pSMB->DataOffset = cpu_to_le16(offset);
5604 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5605 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5607 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5608 pSMB->Reserved4 = 0;
5609 inc_rfc1001_len(pSMB, byte_count);
5610 pSMB->ByteCount = cpu_to_le16(byte_count);
5611 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
5612 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
5614 cFYI(1, "Send error in Set Time (SetFileInfo) = %d", rc);
5616 /* Note: On -EAGAIN error only caller can retry on handle based calls
5617 since file handle passed in no longer valid */
5623 CIFSSMBSetFileDisposition(const int xid, struct cifs_tcon *tcon,
5624 bool delete_file, __u16 fid, __u32 pid_of_opener)
5626 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5629 __u16 params, param_offset, offset, byte_count, count;
5631 cFYI(1, "Set File Disposition (via SetFileInfo)");
5632 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5637 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5638 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5641 pSMB->MaxSetupCount = 0;
5645 pSMB->Reserved2 = 0;
5646 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5647 offset = param_offset + params;
5649 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5652 pSMB->MaxParameterCount = cpu_to_le16(2);
5653 /* BB find max SMB PDU from sess */
5654 pSMB->MaxDataCount = cpu_to_le16(1000);
5655 pSMB->SetupCount = 1;
5656 pSMB->Reserved3 = 0;
5657 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5658 byte_count = 3 /* pad */ + params + count;
5659 pSMB->DataCount = cpu_to_le16(count);
5660 pSMB->ParameterCount = cpu_to_le16(params);
5661 pSMB->TotalDataCount = pSMB->DataCount;
5662 pSMB->TotalParameterCount = pSMB->ParameterCount;
5663 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5664 pSMB->DataOffset = cpu_to_le16(offset);
5666 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_DISPOSITION_INFO);
5667 pSMB->Reserved4 = 0;
5668 inc_rfc1001_len(pSMB, byte_count);
5669 pSMB->ByteCount = cpu_to_le16(byte_count);
5670 *data_offset = delete_file ? 1 : 0;
5671 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
5673 cFYI(1, "Send error in SetFileDisposition = %d", rc);
5679 CIFSSMBSetPathInfo(const int xid, struct cifs_tcon *tcon,
5680 const char *fileName, const FILE_BASIC_INFO *data,
5681 const struct nls_table *nls_codepage, int remap)
5683 TRANSACTION2_SPI_REQ *pSMB = NULL;
5684 TRANSACTION2_SPI_RSP *pSMBr = NULL;
5687 int bytes_returned = 0;
5689 __u16 params, param_offset, offset, byte_count, count;
5691 cFYI(1, "In SetTimes");
5694 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5699 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5701 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
5702 PATH_MAX, nls_codepage, remap);
5703 name_len++; /* trailing null */
5705 } else { /* BB improve the check for buffer overruns BB */
5706 name_len = strnlen(fileName, PATH_MAX);
5707 name_len++; /* trailing null */
5708 strncpy(pSMB->FileName, fileName, name_len);
5711 params = 6 + name_len;
5712 count = sizeof(FILE_BASIC_INFO);
5713 pSMB->MaxParameterCount = cpu_to_le16(2);
5714 /* BB find max SMB PDU from sess structure BB */
5715 pSMB->MaxDataCount = cpu_to_le16(1000);
5716 pSMB->MaxSetupCount = 0;
5720 pSMB->Reserved2 = 0;
5721 param_offset = offsetof(struct smb_com_transaction2_spi_req,
5722 InformationLevel) - 4;
5723 offset = param_offset + params;
5724 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5725 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5726 pSMB->DataOffset = cpu_to_le16(offset);
5727 pSMB->SetupCount = 1;
5728 pSMB->Reserved3 = 0;
5729 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5730 byte_count = 3 /* pad */ + params + count;
5732 pSMB->DataCount = cpu_to_le16(count);
5733 pSMB->ParameterCount = cpu_to_le16(params);
5734 pSMB->TotalDataCount = pSMB->DataCount;
5735 pSMB->TotalParameterCount = pSMB->ParameterCount;
5736 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5737 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5739 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5740 pSMB->Reserved4 = 0;
5741 inc_rfc1001_len(pSMB, byte_count);
5742 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
5743 pSMB->ByteCount = cpu_to_le16(byte_count);
5744 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5745 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5747 cFYI(1, "SetPathInfo (times) returned %d", rc);
5749 cifs_buf_release(pSMB);
5757 /* Can not be used to set time stamps yet (due to old DOS time format) */
5758 /* Can be used to set attributes */
5759 #if 0 /* Possibly not needed - since it turns out that strangely NT4 has a bug
5760 handling it anyway and NT4 was what we thought it would be needed for
5761 Do not delete it until we prove whether needed for Win9x though */
5763 CIFSSMBSetAttrLegacy(int xid, struct cifs_tcon *tcon, char *fileName,
5764 __u16 dos_attrs, const struct nls_table *nls_codepage)
5766 SETATTR_REQ *pSMB = NULL;
5767 SETATTR_RSP *pSMBr = NULL;
5772 cFYI(1, "In SetAttrLegacy");
5775 rc = smb_init(SMB_COM_SETATTR, 8, tcon, (void **) &pSMB,
5780 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5782 ConvertToUTF16((__le16 *) pSMB->fileName, fileName,
5783 PATH_MAX, nls_codepage);
5784 name_len++; /* trailing null */
5786 } else { /* BB improve the check for buffer overruns BB */
5787 name_len = strnlen(fileName, PATH_MAX);
5788 name_len++; /* trailing null */
5789 strncpy(pSMB->fileName, fileName, name_len);
5791 pSMB->attr = cpu_to_le16(dos_attrs);
5792 pSMB->BufferFormat = 0x04;
5793 inc_rfc1001_len(pSMB, name_len + 1);
5794 pSMB->ByteCount = cpu_to_le16(name_len + 1);
5795 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5796 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5798 cFYI(1, "Error in LegacySetAttr = %d", rc);
5800 cifs_buf_release(pSMB);
5803 goto SetAttrLgcyRetry;
5807 #endif /* temporarily unneeded SetAttr legacy function */
5810 cifs_fill_unix_set_info(FILE_UNIX_BASIC_INFO *data_offset,
5811 const struct cifs_unix_set_info_args *args)
5813 u64 mode = args->mode;
5816 * Samba server ignores set of file size to zero due to bugs in some
5817 * older clients, but we should be precise - we use SetFileSize to
5818 * set file size and do not want to truncate file size to zero
5819 * accidentally as happened on one Samba server beta by putting
5820 * zero instead of -1 here
5822 data_offset->EndOfFile = cpu_to_le64(NO_CHANGE_64);
5823 data_offset->NumOfBytes = cpu_to_le64(NO_CHANGE_64);
5824 data_offset->LastStatusChange = cpu_to_le64(args->ctime);
5825 data_offset->LastAccessTime = cpu_to_le64(args->atime);
5826 data_offset->LastModificationTime = cpu_to_le64(args->mtime);
5827 data_offset->Uid = cpu_to_le64(args->uid);
5828 data_offset->Gid = cpu_to_le64(args->gid);
5829 /* better to leave device as zero when it is */
5830 data_offset->DevMajor = cpu_to_le64(MAJOR(args->device));
5831 data_offset->DevMinor = cpu_to_le64(MINOR(args->device));
5832 data_offset->Permissions = cpu_to_le64(mode);
5835 data_offset->Type = cpu_to_le32(UNIX_FILE);
5836 else if (S_ISDIR(mode))
5837 data_offset->Type = cpu_to_le32(UNIX_DIR);
5838 else if (S_ISLNK(mode))
5839 data_offset->Type = cpu_to_le32(UNIX_SYMLINK);
5840 else if (S_ISCHR(mode))
5841 data_offset->Type = cpu_to_le32(UNIX_CHARDEV);
5842 else if (S_ISBLK(mode))
5843 data_offset->Type = cpu_to_le32(UNIX_BLOCKDEV);
5844 else if (S_ISFIFO(mode))
5845 data_offset->Type = cpu_to_le32(UNIX_FIFO);
5846 else if (S_ISSOCK(mode))
5847 data_offset->Type = cpu_to_le32(UNIX_SOCKET);
5851 CIFSSMBUnixSetFileInfo(const int xid, struct cifs_tcon *tcon,
5852 const struct cifs_unix_set_info_args *args,
5853 u16 fid, u32 pid_of_opener)
5855 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5858 u16 params, param_offset, offset, byte_count, count;
5860 cFYI(1, "Set Unix Info (via SetFileInfo)");
5861 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5866 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5867 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5870 pSMB->MaxSetupCount = 0;
5874 pSMB->Reserved2 = 0;
5875 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5876 offset = param_offset + params;
5878 data_offset = (char *)pSMB +
5879 offsetof(struct smb_hdr, Protocol) + offset;
5881 count = sizeof(FILE_UNIX_BASIC_INFO);
5883 pSMB->MaxParameterCount = cpu_to_le16(2);
5884 /* BB find max SMB PDU from sess */
5885 pSMB->MaxDataCount = cpu_to_le16(1000);
5886 pSMB->SetupCount = 1;
5887 pSMB->Reserved3 = 0;
5888 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5889 byte_count = 3 /* pad */ + params + count;
5890 pSMB->DataCount = cpu_to_le16(count);
5891 pSMB->ParameterCount = cpu_to_le16(params);
5892 pSMB->TotalDataCount = pSMB->DataCount;
5893 pSMB->TotalParameterCount = pSMB->ParameterCount;
5894 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5895 pSMB->DataOffset = cpu_to_le16(offset);
5897 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
5898 pSMB->Reserved4 = 0;
5899 inc_rfc1001_len(pSMB, byte_count);
5900 pSMB->ByteCount = cpu_to_le16(byte_count);
5902 cifs_fill_unix_set_info((FILE_UNIX_BASIC_INFO *)data_offset, args);
5904 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
5906 cFYI(1, "Send error in Set Time (SetFileInfo) = %d", rc);
5908 /* Note: On -EAGAIN error only caller can retry on handle based calls
5909 since file handle passed in no longer valid */
5915 CIFSSMBUnixSetPathInfo(const int xid, struct cifs_tcon *tcon, char *fileName,
5916 const struct cifs_unix_set_info_args *args,
5917 const struct nls_table *nls_codepage, int remap)
5919 TRANSACTION2_SPI_REQ *pSMB = NULL;
5920 TRANSACTION2_SPI_RSP *pSMBr = NULL;
5923 int bytes_returned = 0;
5924 FILE_UNIX_BASIC_INFO *data_offset;
5925 __u16 params, param_offset, offset, count, byte_count;
5927 cFYI(1, "In SetUID/GID/Mode");
5929 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5934 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5936 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
5937 PATH_MAX, nls_codepage, remap);
5938 name_len++; /* trailing null */
5940 } else { /* BB improve the check for buffer overruns BB */
5941 name_len = strnlen(fileName, PATH_MAX);
5942 name_len++; /* trailing null */
5943 strncpy(pSMB->FileName, fileName, name_len);
5946 params = 6 + name_len;
5947 count = sizeof(FILE_UNIX_BASIC_INFO);
5948 pSMB->MaxParameterCount = cpu_to_le16(2);
5949 /* BB find max SMB PDU from sess structure BB */
5950 pSMB->MaxDataCount = cpu_to_le16(1000);
5951 pSMB->MaxSetupCount = 0;
5955 pSMB->Reserved2 = 0;
5956 param_offset = offsetof(struct smb_com_transaction2_spi_req,
5957 InformationLevel) - 4;
5958 offset = param_offset + params;
5960 (FILE_UNIX_BASIC_INFO *) ((char *) &pSMB->hdr.Protocol +
5962 memset(data_offset, 0, count);
5963 pSMB->DataOffset = cpu_to_le16(offset);
5964 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5965 pSMB->SetupCount = 1;
5966 pSMB->Reserved3 = 0;
5967 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5968 byte_count = 3 /* pad */ + params + count;
5969 pSMB->ParameterCount = cpu_to_le16(params);
5970 pSMB->DataCount = cpu_to_le16(count);
5971 pSMB->TotalParameterCount = pSMB->ParameterCount;
5972 pSMB->TotalDataCount = pSMB->DataCount;
5973 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
5974 pSMB->Reserved4 = 0;
5975 inc_rfc1001_len(pSMB, byte_count);
5977 cifs_fill_unix_set_info(data_offset, args);
5979 pSMB->ByteCount = cpu_to_le16(byte_count);
5980 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5981 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5983 cFYI(1, "SetPathInfo (perms) returned %d", rc);
5985 cifs_buf_release(pSMB);
5991 #ifdef CONFIG_CIFS_XATTR
5993 * Do a path-based QUERY_ALL_EAS call and parse the result. This is a common
5994 * function used by listxattr and getxattr type calls. When ea_name is set,
5995 * it looks for that attribute name and stuffs that value into the EAData
5996 * buffer. When ea_name is NULL, it stuffs a list of attribute names into the
5997 * buffer. In both cases, the return value is either the length of the
5998 * resulting data or a negative error code. If EAData is a NULL pointer then
5999 * the data isn't copied to it, but the length is returned.
6002 CIFSSMBQAllEAs(const int xid, struct cifs_tcon *tcon,
6003 const unsigned char *searchName, const unsigned char *ea_name,
6004 char *EAData, size_t buf_size,
6005 const struct nls_table *nls_codepage, int remap)
6007 /* BB assumes one setup word */
6008 TRANSACTION2_QPI_REQ *pSMB = NULL;
6009 TRANSACTION2_QPI_RSP *pSMBr = NULL;
6013 struct fealist *ea_response_data;
6014 struct fea *temp_fea;
6017 __u16 params, byte_count, data_offset;
6018 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
6020 cFYI(1, "In Query All EAs path %s", searchName);
6022 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6027 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6029 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
6030 PATH_MAX, nls_codepage, remap);
6031 list_len++; /* trailing null */
6033 } else { /* BB improve the check for buffer overruns BB */
6034 list_len = strnlen(searchName, PATH_MAX);
6035 list_len++; /* trailing null */
6036 strncpy(pSMB->FileName, searchName, list_len);
6039 params = 2 /* level */ + 4 /* reserved */ + list_len /* includes NUL */;
6040 pSMB->TotalDataCount = 0;
6041 pSMB->MaxParameterCount = cpu_to_le16(2);
6042 /* BB find exact max SMB PDU from sess structure BB */
6043 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
6044 pSMB->MaxSetupCount = 0;
6048 pSMB->Reserved2 = 0;
6049 pSMB->ParameterOffset = cpu_to_le16(offsetof(
6050 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
6051 pSMB->DataCount = 0;
6052 pSMB->DataOffset = 0;
6053 pSMB->SetupCount = 1;
6054 pSMB->Reserved3 = 0;
6055 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
6056 byte_count = params + 1 /* pad */ ;
6057 pSMB->TotalParameterCount = cpu_to_le16(params);
6058 pSMB->ParameterCount = pSMB->TotalParameterCount;
6059 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_QUERY_ALL_EAS);
6060 pSMB->Reserved4 = 0;
6061 inc_rfc1001_len(pSMB, byte_count);
6062 pSMB->ByteCount = cpu_to_le16(byte_count);
6064 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6065 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
6067 cFYI(1, "Send error in QueryAllEAs = %d", rc);
6072 /* BB also check enough total bytes returned */
6073 /* BB we need to improve the validity checking
6074 of these trans2 responses */
6076 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
6077 if (rc || get_bcc(&pSMBr->hdr) < 4) {
6078 rc = -EIO; /* bad smb */
6082 /* check that length of list is not more than bcc */
6083 /* check that each entry does not go beyond length
6085 /* check that each element of each entry does not
6086 go beyond end of list */
6087 /* validate_trans2_offsets() */
6088 /* BB check if start of smb + data_offset > &bcc+ bcc */
6090 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
6091 ea_response_data = (struct fealist *)
6092 (((char *) &pSMBr->hdr.Protocol) + data_offset);
6094 list_len = le32_to_cpu(ea_response_data->list_len);
6095 cFYI(1, "ea length %d", list_len);
6096 if (list_len <= 8) {
6097 cFYI(1, "empty EA list returned from server");
6101 /* make sure list_len doesn't go past end of SMB */
6102 end_of_smb = (char *)pByteArea(&pSMBr->hdr) + get_bcc(&pSMBr->hdr);
6103 if ((char *)ea_response_data + list_len > end_of_smb) {
6104 cFYI(1, "EA list appears to go beyond SMB");
6109 /* account for ea list len */
6111 temp_fea = ea_response_data->list;
6112 temp_ptr = (char *)temp_fea;
6113 while (list_len > 0) {
6114 unsigned int name_len;
6119 /* make sure we can read name_len and value_len */
6121 cFYI(1, "EA entry goes beyond length of list");
6126 name_len = temp_fea->name_len;
6127 value_len = le16_to_cpu(temp_fea->value_len);
6128 list_len -= name_len + 1 + value_len;
6130 cFYI(1, "EA entry goes beyond length of list");
6136 if (ea_name_len == name_len &&
6137 memcmp(ea_name, temp_ptr, name_len) == 0) {
6138 temp_ptr += name_len + 1;
6142 if ((size_t)value_len > buf_size) {
6146 memcpy(EAData, temp_ptr, value_len);
6150 /* account for prefix user. and trailing null */
6151 rc += (5 + 1 + name_len);
6152 if (rc < (int) buf_size) {
6153 memcpy(EAData, "user.", 5);
6155 memcpy(EAData, temp_ptr, name_len);
6157 /* null terminate name */
6160 } else if (buf_size == 0) {
6161 /* skip copy - calc size only */
6163 /* stop before overrun buffer */
6168 temp_ptr += name_len + 1 + value_len;
6169 temp_fea = (struct fea *)temp_ptr;
6172 /* didn't find the named attribute */
6177 cifs_buf_release(pSMB);
6185 CIFSSMBSetEA(const int xid, struct cifs_tcon *tcon, const char *fileName,
6186 const char *ea_name, const void *ea_value,
6187 const __u16 ea_value_len, const struct nls_table *nls_codepage,
6190 struct smb_com_transaction2_spi_req *pSMB = NULL;
6191 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
6192 struct fealist *parm_data;
6195 int bytes_returned = 0;
6196 __u16 params, param_offset, byte_count, offset, count;
6198 cFYI(1, "In SetEA");
6200 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6205 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6207 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
6208 PATH_MAX, nls_codepage, remap);
6209 name_len++; /* trailing null */
6211 } else { /* BB improve the check for buffer overruns BB */
6212 name_len = strnlen(fileName, PATH_MAX);
6213 name_len++; /* trailing null */
6214 strncpy(pSMB->FileName, fileName, name_len);
6217 params = 6 + name_len;
6219 /* done calculating parms using name_len of file name,
6220 now use name_len to calculate length of ea name
6221 we are going to create in the inode xattrs */
6222 if (ea_name == NULL)
6225 name_len = strnlen(ea_name, 255);
6227 count = sizeof(*parm_data) + ea_value_len + name_len;
6228 pSMB->MaxParameterCount = cpu_to_le16(2);
6229 /* BB find max SMB PDU from sess */
6230 pSMB->MaxDataCount = cpu_to_le16(1000);
6231 pSMB->MaxSetupCount = 0;
6235 pSMB->Reserved2 = 0;
6236 param_offset = offsetof(struct smb_com_transaction2_spi_req,
6237 InformationLevel) - 4;
6238 offset = param_offset + params;
6239 pSMB->InformationLevel =
6240 cpu_to_le16(SMB_SET_FILE_EA);
6243 (struct fealist *) (((char *) &pSMB->hdr.Protocol) +
6245 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6246 pSMB->DataOffset = cpu_to_le16(offset);
6247 pSMB->SetupCount = 1;
6248 pSMB->Reserved3 = 0;
6249 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6250 byte_count = 3 /* pad */ + params + count;
6251 pSMB->DataCount = cpu_to_le16(count);
6252 parm_data->list_len = cpu_to_le32(count);
6253 parm_data->list[0].EA_flags = 0;
6254 /* we checked above that name len is less than 255 */
6255 parm_data->list[0].name_len = (__u8)name_len;
6256 /* EA names are always ASCII */
6258 strncpy(parm_data->list[0].name, ea_name, name_len);
6259 parm_data->list[0].name[name_len] = 0;
6260 parm_data->list[0].value_len = cpu_to_le16(ea_value_len);
6261 /* caller ensures that ea_value_len is less than 64K but
6262 we need to ensure that it fits within the smb */
6264 /*BB add length check to see if it would fit in
6265 negotiated SMB buffer size BB */
6266 /* if (ea_value_len > buffer_size - 512 (enough for header)) */
6268 memcpy(parm_data->list[0].name+name_len+1,
6269 ea_value, ea_value_len);
6271 pSMB->TotalDataCount = pSMB->DataCount;
6272 pSMB->ParameterCount = cpu_to_le16(params);
6273 pSMB->TotalParameterCount = pSMB->ParameterCount;
6274 pSMB->Reserved4 = 0;
6275 inc_rfc1001_len(pSMB, byte_count);
6276 pSMB->ByteCount = cpu_to_le16(byte_count);
6277 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6278 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
6280 cFYI(1, "SetPathInfo (EA) returned %d", rc);
6282 cifs_buf_release(pSMB);
6291 #ifdef CONFIG_CIFS_DNOTIFY_EXPERIMENTAL /* BB unused temporarily */
6293 * Years ago the kernel added a "dnotify" function for Samba server,
6294 * to allow network clients (such as Windows) to display updated
6295 * lists of files in directory listings automatically when
6296 * files are added by one user when another user has the
6297 * same directory open on their desktop. The Linux cifs kernel
6298 * client hooked into the kernel side of this interface for
6299 * the same reason, but ironically when the VFS moved from
6300 * "dnotify" to "inotify" it became harder to plug in Linux
6301 * network file system clients (the most obvious use case
6302 * for notify interfaces is when multiple users can update
6303 * the contents of the same directory - exactly what network
6304 * file systems can do) although the server (Samba) could
6305 * still use it. For the short term we leave the worker
6306 * function ifdeffed out (below) until inotify is fixed
6307 * in the VFS to make it easier to plug in network file
6308 * system clients. If inotify turns out to be permanently
6309 * incompatible for network fs clients, we could instead simply
6310 * expose this config flag by adding a future cifs (and smb2) notify ioctl.
6312 int CIFSSMBNotify(const int xid, struct cifs_tcon *tcon,
6313 const int notify_subdirs, const __u16 netfid,
6314 __u32 filter, struct file *pfile, int multishot,
6315 const struct nls_table *nls_codepage)
6318 struct smb_com_transaction_change_notify_req *pSMB = NULL;
6319 struct smb_com_ntransaction_change_notify_rsp *pSMBr = NULL;
6320 struct dir_notify_req *dnotify_req;
6323 cFYI(1, "In CIFSSMBNotify for file handle %d", (int)netfid);
6324 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
6329 pSMB->TotalParameterCount = 0 ;
6330 pSMB->TotalDataCount = 0;
6331 pSMB->MaxParameterCount = cpu_to_le32(2);
6332 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
6333 pSMB->MaxSetupCount = 4;
6335 pSMB->ParameterOffset = 0;
6336 pSMB->DataCount = 0;
6337 pSMB->DataOffset = 0;
6338 pSMB->SetupCount = 4; /* single byte does not need le conversion */
6339 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_NOTIFY_CHANGE);
6340 pSMB->ParameterCount = pSMB->TotalParameterCount;
6342 pSMB->WatchTree = 1; /* one byte - no le conversion needed */
6343 pSMB->Reserved2 = 0;
6344 pSMB->CompletionFilter = cpu_to_le32(filter);
6345 pSMB->Fid = netfid; /* file handle always le */
6346 pSMB->ByteCount = 0;
6348 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6349 (struct smb_hdr *)pSMBr, &bytes_returned,
6352 cFYI(1, "Error in Notify = %d", rc);
6354 /* Add file to outstanding requests */
6355 /* BB change to kmem cache alloc */
6356 dnotify_req = kmalloc(
6357 sizeof(struct dir_notify_req),
6360 dnotify_req->Pid = pSMB->hdr.Pid;
6361 dnotify_req->PidHigh = pSMB->hdr.PidHigh;
6362 dnotify_req->Mid = pSMB->hdr.Mid;
6363 dnotify_req->Tid = pSMB->hdr.Tid;
6364 dnotify_req->Uid = pSMB->hdr.Uid;
6365 dnotify_req->netfid = netfid;
6366 dnotify_req->pfile = pfile;
6367 dnotify_req->filter = filter;
6368 dnotify_req->multishot = multishot;
6369 spin_lock(&GlobalMid_Lock);
6370 list_add_tail(&dnotify_req->lhead,
6371 &GlobalDnotifyReqList);
6372 spin_unlock(&GlobalMid_Lock);
6376 cifs_buf_release(pSMB);
6379 #endif /* was needed for dnotify, and will be needed for inotify when VFS fix */