4 * Copyright (C) International Business Machines Corp., 2002,2010
5 * Author(s): Steve French (sfrench@us.ibm.com)
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 */
91 * On arches that have high memory, kmap address space is limited. By
92 * serializing the kmap operations on those arches, we ensure that we don't
93 * end up with a bunch of threads in writeback with partially mapped page
94 * arrays, stuck waiting for kmap to come back. That situation prevents
95 * progress and can deadlock.
97 static DEFINE_MUTEX(cifs_kmap_mutex);
102 mutex_lock(&cifs_kmap_mutex);
106 cifs_kmap_unlock(void)
108 mutex_unlock(&cifs_kmap_mutex);
110 #else /* !CONFIG_HIGHMEM */
111 #define cifs_kmap_lock() do { ; } while(0)
112 #define cifs_kmap_unlock() do { ; } while(0)
113 #endif /* CONFIG_HIGHMEM */
115 /* Mark as invalid, all open files on tree connections since they
116 were closed when session to server was lost */
117 static void mark_open_files_invalid(struct cifs_tcon *pTcon)
119 struct cifsFileInfo *open_file = NULL;
120 struct list_head *tmp;
121 struct list_head *tmp1;
123 /* list all files open on tree connection and mark them invalid */
124 spin_lock(&cifs_file_list_lock);
125 list_for_each_safe(tmp, tmp1, &pTcon->openFileList) {
126 open_file = list_entry(tmp, struct cifsFileInfo, tlist);
127 open_file->invalidHandle = true;
128 open_file->oplock_break_cancelled = true;
130 spin_unlock(&cifs_file_list_lock);
131 /* BB Add call to invalidate_inodes(sb) for all superblocks mounted
135 /* reconnect the socket, tcon, and smb session if needed */
137 cifs_reconnect_tcon(struct cifs_tcon *tcon, int smb_command)
140 struct cifs_ses *ses;
141 struct TCP_Server_Info *server;
142 struct nls_table *nls_codepage;
145 * SMBs NegProt, SessSetup, uLogoff do not have tcon yet so check for
146 * tcp and smb session status done differently for those three - in the
153 server = ses->server;
156 * only tree disconnect, open, and write, (and ulogoff which does not
157 * have tcon) are allowed as we start force umount
159 if (tcon->tidStatus == CifsExiting) {
160 if (smb_command != SMB_COM_WRITE_ANDX &&
161 smb_command != SMB_COM_OPEN_ANDX &&
162 smb_command != SMB_COM_TREE_DISCONNECT) {
163 cFYI(1, "can not send cmd %d while umounting",
170 * Give demultiplex thread up to 10 seconds to reconnect, should be
171 * greater than cifs socket timeout which is 7 seconds
173 while (server->tcpStatus == CifsNeedReconnect) {
174 wait_event_interruptible_timeout(server->response_q,
175 (server->tcpStatus != CifsNeedReconnect), 10 * HZ);
177 /* are we still trying to reconnect? */
178 if (server->tcpStatus != CifsNeedReconnect)
182 * on "soft" mounts we wait once. Hard mounts keep
183 * retrying until process is killed or server comes
187 cFYI(1, "gave up waiting on reconnect in smb_init");
192 if (!ses->need_reconnect && !tcon->need_reconnect)
195 nls_codepage = load_nls_default();
198 * need to prevent multiple threads trying to simultaneously
199 * reconnect the same SMB session
201 mutex_lock(&ses->session_mutex);
202 rc = cifs_negotiate_protocol(0, ses);
203 if (rc == 0 && ses->need_reconnect)
204 rc = cifs_setup_session(0, ses, nls_codepage);
206 /* do we need to reconnect tcon? */
207 if (rc || !tcon->need_reconnect) {
208 mutex_unlock(&ses->session_mutex);
212 mark_open_files_invalid(tcon);
213 rc = CIFSTCon(0, ses, tcon->treeName, tcon, nls_codepage);
214 mutex_unlock(&ses->session_mutex);
215 cFYI(1, "reconnect tcon rc = %d", rc);
221 * FIXME: check if wsize needs updated due to negotiated smb buffer
224 atomic_inc(&tconInfoReconnectCount);
226 /* tell server Unix caps we support */
227 if (ses->capabilities & CAP_UNIX)
228 reset_cifs_unix_caps(0, tcon, NULL, NULL);
231 * Removed call to reopen open files here. It is safer (and faster) to
232 * reopen files one at a time as needed in read and write.
234 * FIXME: what about file locks? don't we need to reclaim them ASAP?
239 * Check if handle based operation so we know whether we can continue
240 * or not without returning to caller to reset file handle
242 switch (smb_command) {
243 case SMB_COM_READ_ANDX:
244 case SMB_COM_WRITE_ANDX:
246 case SMB_COM_FIND_CLOSE2:
247 case SMB_COM_LOCKING_ANDX:
251 unload_nls(nls_codepage);
255 /* Allocate and return pointer to an SMB request buffer, and set basic
256 SMB information in the SMB header. If the return code is zero, this
257 function must have filled in request_buf pointer */
259 small_smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
264 rc = cifs_reconnect_tcon(tcon, smb_command);
268 *request_buf = cifs_small_buf_get();
269 if (*request_buf == NULL) {
270 /* BB should we add a retry in here if not a writepage? */
274 header_assemble((struct smb_hdr *) *request_buf, smb_command,
278 cifs_stats_inc(&tcon->num_smbs_sent);
284 small_smb_init_no_tc(const int smb_command, const int wct,
285 struct cifs_ses *ses, void **request_buf)
288 struct smb_hdr *buffer;
290 rc = small_smb_init(smb_command, wct, NULL, request_buf);
294 buffer = (struct smb_hdr *)*request_buf;
295 buffer->Mid = get_next_mid(ses->server);
296 if (ses->capabilities & CAP_UNICODE)
297 buffer->Flags2 |= SMBFLG2_UNICODE;
298 if (ses->capabilities & CAP_STATUS32)
299 buffer->Flags2 |= SMBFLG2_ERR_STATUS;
301 /* uid, tid can stay at zero as set in header assemble */
303 /* BB add support for turning on the signing when
304 this function is used after 1st of session setup requests */
309 /* If the return code is zero, this function must fill in request_buf pointer */
311 __smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
312 void **request_buf, void **response_buf)
314 *request_buf = cifs_buf_get();
315 if (*request_buf == NULL) {
316 /* BB should we add a retry in here if not a writepage? */
319 /* Although the original thought was we needed the response buf for */
320 /* potential retries of smb operations it turns out we can determine */
321 /* from the mid flags when the request buffer can be resent without */
322 /* having to use a second distinct buffer for the response */
324 *response_buf = *request_buf;
326 header_assemble((struct smb_hdr *) *request_buf, smb_command, tcon,
330 cifs_stats_inc(&tcon->num_smbs_sent);
335 /* If the return code is zero, this function must fill in request_buf pointer */
337 smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
338 void **request_buf, void **response_buf)
342 rc = cifs_reconnect_tcon(tcon, smb_command);
346 return __smb_init(smb_command, wct, tcon, request_buf, response_buf);
350 smb_init_no_reconnect(int smb_command, int wct, struct cifs_tcon *tcon,
351 void **request_buf, void **response_buf)
353 if (tcon->ses->need_reconnect || tcon->need_reconnect)
356 return __smb_init(smb_command, wct, tcon, request_buf, response_buf);
359 static int validate_t2(struct smb_t2_rsp *pSMB)
361 unsigned int total_size;
363 /* check for plausible wct */
364 if (pSMB->hdr.WordCount < 10)
367 /* check for parm and data offset going beyond end of smb */
368 if (get_unaligned_le16(&pSMB->t2_rsp.ParameterOffset) > 1024 ||
369 get_unaligned_le16(&pSMB->t2_rsp.DataOffset) > 1024)
372 total_size = get_unaligned_le16(&pSMB->t2_rsp.ParameterCount);
373 if (total_size >= 512)
376 /* check that bcc is at least as big as parms + data, and that it is
377 * less than negotiated smb buffer
379 total_size += get_unaligned_le16(&pSMB->t2_rsp.DataCount);
380 if (total_size > get_bcc(&pSMB->hdr) ||
381 total_size >= CIFSMaxBufSize + MAX_CIFS_HDR_SIZE)
386 cifs_dump_mem("Invalid transact2 SMB: ", (char *)pSMB,
387 sizeof(struct smb_t2_rsp) + 16);
391 static inline void inc_rfc1001_len(void *pSMB, int count)
393 struct smb_hdr *hdr = (struct smb_hdr *)pSMB;
395 be32_add_cpu(&hdr->smb_buf_length, count);
399 CIFSSMBNegotiate(unsigned int xid, struct cifs_ses *ses)
402 NEGOTIATE_RSP *pSMBr;
406 struct TCP_Server_Info *server;
408 unsigned int secFlags;
411 server = ses->server;
416 rc = smb_init(SMB_COM_NEGOTIATE, 0, NULL /* no tcon yet */ ,
417 (void **) &pSMB, (void **) &pSMBr);
421 /* if any of auth flags (ie not sign or seal) are overriden use them */
422 if (ses->overrideSecFlg & (~(CIFSSEC_MUST_SIGN | CIFSSEC_MUST_SEAL)))
423 secFlags = ses->overrideSecFlg; /* BB FIXME fix sign flags? */
424 else /* if override flags set only sign/seal OR them with global auth */
425 secFlags = global_secflags | ses->overrideSecFlg;
427 cFYI(1, "secFlags 0x%x", secFlags);
429 pSMB->hdr.Mid = get_next_mid(server);
430 pSMB->hdr.Flags2 |= (SMBFLG2_UNICODE | SMBFLG2_ERR_STATUS);
432 if ((secFlags & CIFSSEC_MUST_KRB5) == CIFSSEC_MUST_KRB5)
433 pSMB->hdr.Flags2 |= SMBFLG2_EXT_SEC;
434 else if ((secFlags & CIFSSEC_AUTH_MASK) == CIFSSEC_MAY_KRB5) {
435 cFYI(1, "Kerberos only mechanism, enable extended security");
436 pSMB->hdr.Flags2 |= SMBFLG2_EXT_SEC;
437 } else if ((secFlags & CIFSSEC_MUST_NTLMSSP) == CIFSSEC_MUST_NTLMSSP)
438 pSMB->hdr.Flags2 |= SMBFLG2_EXT_SEC;
439 else if ((secFlags & CIFSSEC_AUTH_MASK) == CIFSSEC_MAY_NTLMSSP) {
440 cFYI(1, "NTLMSSP only mechanism, enable extended security");
441 pSMB->hdr.Flags2 |= SMBFLG2_EXT_SEC;
445 for (i = 0; i < CIFS_NUM_PROT; i++) {
446 strncpy(pSMB->DialectsArray+count, protocols[i].name, 16);
447 count += strlen(protocols[i].name) + 1;
448 /* null at end of source and target buffers anyway */
450 inc_rfc1001_len(pSMB, count);
451 pSMB->ByteCount = cpu_to_le16(count);
453 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
454 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
458 server->dialect = le16_to_cpu(pSMBr->DialectIndex);
459 cFYI(1, "Dialect: %d", server->dialect);
460 /* Check wct = 1 error case */
461 if ((pSMBr->hdr.WordCount < 13) || (server->dialect == BAD_PROT)) {
462 /* core returns wct = 1, but we do not ask for core - otherwise
463 small wct just comes when dialect index is -1 indicating we
464 could not negotiate a common dialect */
467 #ifdef CONFIG_CIFS_WEAK_PW_HASH
468 } else if ((pSMBr->hdr.WordCount == 13)
469 && ((server->dialect == LANMAN_PROT)
470 || (server->dialect == LANMAN2_PROT))) {
472 struct lanman_neg_rsp *rsp = (struct lanman_neg_rsp *)pSMBr;
474 if ((secFlags & CIFSSEC_MAY_LANMAN) ||
475 (secFlags & CIFSSEC_MAY_PLNTXT))
476 server->secType = LANMAN;
478 cERROR(1, "mount failed weak security disabled"
479 " in /proc/fs/cifs/SecurityFlags");
483 server->sec_mode = (__u8)le16_to_cpu(rsp->SecurityMode);
484 server->maxReq = min_t(unsigned int,
485 le16_to_cpu(rsp->MaxMpxCount),
487 set_credits(server, server->maxReq);
488 server->maxBuf = le16_to_cpu(rsp->MaxBufSize);
489 server->max_vcs = le16_to_cpu(rsp->MaxNumberVcs);
490 /* even though we do not use raw we might as well set this
491 accurately, in case we ever find a need for it */
492 if ((le16_to_cpu(rsp->RawMode) & RAW_ENABLE) == RAW_ENABLE) {
493 server->max_rw = 0xFF00;
494 server->capabilities = CAP_MPX_MODE | CAP_RAW_MODE;
496 server->max_rw = 0;/* do not need to use raw anyway */
497 server->capabilities = CAP_MPX_MODE;
499 tmp = (__s16)le16_to_cpu(rsp->ServerTimeZone);
501 /* OS/2 often does not set timezone therefore
502 * we must use server time to calc time zone.
503 * Could deviate slightly from the right zone.
504 * Smallest defined timezone difference is 15 minutes
505 * (i.e. Nepal). Rounding up/down is done to match
508 int val, seconds, remain, result;
509 struct timespec ts, utc;
511 ts = cnvrtDosUnixTm(rsp->SrvTime.Date,
512 rsp->SrvTime.Time, 0);
513 cFYI(1, "SrvTime %d sec since 1970 (utc: %d) diff: %d",
514 (int)ts.tv_sec, (int)utc.tv_sec,
515 (int)(utc.tv_sec - ts.tv_sec));
516 val = (int)(utc.tv_sec - ts.tv_sec);
518 result = (seconds / MIN_TZ_ADJ) * MIN_TZ_ADJ;
519 remain = seconds % MIN_TZ_ADJ;
520 if (remain >= (MIN_TZ_ADJ / 2))
521 result += MIN_TZ_ADJ;
524 server->timeAdj = result;
526 server->timeAdj = (int)tmp;
527 server->timeAdj *= 60; /* also in seconds */
529 cFYI(1, "server->timeAdj: %d seconds", server->timeAdj);
532 /* BB get server time for time conversions and add
533 code to use it and timezone since this is not UTC */
535 if (rsp->EncryptionKeyLength ==
536 cpu_to_le16(CIFS_CRYPTO_KEY_SIZE)) {
537 memcpy(ses->server->cryptkey, rsp->EncryptionKey,
538 CIFS_CRYPTO_KEY_SIZE);
539 } else if (server->sec_mode & SECMODE_PW_ENCRYPT) {
540 rc = -EIO; /* need cryptkey unless plain text */
544 cFYI(1, "LANMAN negotiated");
545 /* we will not end up setting signing flags - as no signing
546 was in LANMAN and server did not return the flags on */
548 #else /* weak security disabled */
549 } else if (pSMBr->hdr.WordCount == 13) {
550 cERROR(1, "mount failed, cifs module not built "
551 "with CIFS_WEAK_PW_HASH support");
553 #endif /* WEAK_PW_HASH */
555 } else if (pSMBr->hdr.WordCount != 17) {
560 /* else wct == 17 NTLM */
561 server->sec_mode = pSMBr->SecurityMode;
562 if ((server->sec_mode & SECMODE_USER) == 0)
563 cFYI(1, "share mode security");
565 if ((server->sec_mode & SECMODE_PW_ENCRYPT) == 0)
566 #ifdef CONFIG_CIFS_WEAK_PW_HASH
567 if ((secFlags & CIFSSEC_MAY_PLNTXT) == 0)
568 #endif /* CIFS_WEAK_PW_HASH */
569 cERROR(1, "Server requests plain text password"
570 " but client support disabled");
572 if ((secFlags & CIFSSEC_MUST_NTLMV2) == CIFSSEC_MUST_NTLMV2)
573 server->secType = NTLMv2;
574 else if (secFlags & CIFSSEC_MAY_NTLM)
575 server->secType = NTLM;
576 else if (secFlags & CIFSSEC_MAY_NTLMV2)
577 server->secType = NTLMv2;
578 else if (secFlags & CIFSSEC_MAY_KRB5)
579 server->secType = Kerberos;
580 else if (secFlags & CIFSSEC_MAY_NTLMSSP)
581 server->secType = RawNTLMSSP;
582 else if (secFlags & CIFSSEC_MAY_LANMAN)
583 server->secType = LANMAN;
586 cERROR(1, "Invalid security type");
589 /* else ... any others ...? */
591 /* one byte, so no need to convert this or EncryptionKeyLen from
593 server->maxReq = min_t(unsigned int, le16_to_cpu(pSMBr->MaxMpxCount),
595 set_credits(server, server->maxReq);
596 /* probably no need to store and check maxvcs */
597 server->maxBuf = le32_to_cpu(pSMBr->MaxBufferSize);
598 server->max_rw = le32_to_cpu(pSMBr->MaxRawSize);
599 cFYI(DBG2, "Max buf = %d", ses->server->maxBuf);
600 server->capabilities = le32_to_cpu(pSMBr->Capabilities);
601 server->timeAdj = (int)(__s16)le16_to_cpu(pSMBr->ServerTimeZone);
602 server->timeAdj *= 60;
603 if (pSMBr->EncryptionKeyLength == CIFS_CRYPTO_KEY_SIZE) {
604 memcpy(ses->server->cryptkey, pSMBr->u.EncryptionKey,
605 CIFS_CRYPTO_KEY_SIZE);
606 } else if ((pSMBr->hdr.Flags2 & SMBFLG2_EXT_SEC ||
607 server->capabilities & CAP_EXTENDED_SECURITY) &&
608 (pSMBr->EncryptionKeyLength == 0)) {
609 /* decode security blob */
610 count = get_bcc(&pSMBr->hdr);
615 spin_lock(&cifs_tcp_ses_lock);
616 if (server->srv_count > 1) {
617 spin_unlock(&cifs_tcp_ses_lock);
618 if (memcmp(server->server_GUID,
619 pSMBr->u.extended_response.
621 cFYI(1, "server UID changed");
622 memcpy(server->server_GUID,
623 pSMBr->u.extended_response.GUID,
627 spin_unlock(&cifs_tcp_ses_lock);
628 memcpy(server->server_GUID,
629 pSMBr->u.extended_response.GUID, 16);
633 server->secType = RawNTLMSSP;
635 rc = decode_negTokenInit(pSMBr->u.extended_response.
636 SecurityBlob, count - 16,
642 if (server->secType == Kerberos) {
643 if (!server->sec_kerberos &&
644 !server->sec_mskerberos)
646 } else if (server->secType == RawNTLMSSP) {
647 if (!server->sec_ntlmssp)
652 } else if (server->sec_mode & SECMODE_PW_ENCRYPT) {
653 rc = -EIO; /* no crypt key only if plain text pwd */
656 server->capabilities &= ~CAP_EXTENDED_SECURITY;
658 #ifdef CONFIG_CIFS_WEAK_PW_HASH
661 if ((secFlags & CIFSSEC_MAY_SIGN) == 0) {
662 /* MUST_SIGN already includes the MAY_SIGN FLAG
663 so if this is zero it means that signing is disabled */
664 cFYI(1, "Signing disabled");
665 if (server->sec_mode & SECMODE_SIGN_REQUIRED) {
666 cERROR(1, "Server requires "
667 "packet signing to be enabled in "
668 "/proc/fs/cifs/SecurityFlags.");
672 ~(SECMODE_SIGN_ENABLED | SECMODE_SIGN_REQUIRED);
673 } else if ((secFlags & CIFSSEC_MUST_SIGN) == CIFSSEC_MUST_SIGN) {
674 /* signing required */
675 cFYI(1, "Must sign - secFlags 0x%x", secFlags);
676 if ((server->sec_mode &
677 (SECMODE_SIGN_ENABLED | SECMODE_SIGN_REQUIRED)) == 0) {
678 cERROR(1, "signing required but server lacks support");
681 server->sec_mode |= SECMODE_SIGN_REQUIRED;
683 /* signing optional ie CIFSSEC_MAY_SIGN */
684 if ((server->sec_mode & SECMODE_SIGN_REQUIRED) == 0)
686 ~(SECMODE_SIGN_ENABLED | SECMODE_SIGN_REQUIRED);
690 cifs_buf_release(pSMB);
692 cFYI(1, "negprot rc %d", rc);
697 CIFSSMBTDis(const int xid, struct cifs_tcon *tcon)
699 struct smb_hdr *smb_buffer;
702 cFYI(1, "In tree disconnect");
704 /* BB: do we need to check this? These should never be NULL. */
705 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
709 * No need to return error on this operation if tid invalidated and
710 * closed on server already e.g. due to tcp session crashing. Also,
711 * the tcon is no longer on the list, so no need to take lock before
714 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
717 rc = small_smb_init(SMB_COM_TREE_DISCONNECT, 0, tcon,
718 (void **)&smb_buffer);
722 rc = SendReceiveNoRsp(xid, tcon->ses, (char *)smb_buffer, 0);
724 cFYI(1, "Tree disconnect failed %d", rc);
726 /* No need to return error on this operation if tid invalidated and
727 closed on server already e.g. due to tcp session crashing */
735 * This is a no-op for now. We're not really interested in the reply, but
736 * rather in the fact that the server sent one and that server->lstrp
739 * FIXME: maybe we should consider checking that the reply matches request?
742 cifs_echo_callback(struct mid_q_entry *mid)
744 struct TCP_Server_Info *server = mid->callback_data;
746 DeleteMidQEntry(mid);
747 add_credits(server, 1);
751 CIFSSMBEcho(struct TCP_Server_Info *server)
757 cFYI(1, "In echo request");
759 rc = small_smb_init(SMB_COM_ECHO, 0, NULL, (void **)&smb);
763 /* set up echo request */
764 smb->hdr.Tid = 0xffff;
765 smb->hdr.WordCount = 1;
766 put_unaligned_le16(1, &smb->EchoCount);
767 put_bcc(1, &smb->hdr);
769 inc_rfc1001_len(smb, 3);
771 iov.iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4;
773 rc = cifs_call_async(server, &iov, 1, NULL, cifs_echo_callback,
776 cFYI(1, "Echo request failed: %d", rc);
778 cifs_small_buf_release(smb);
784 CIFSSMBLogoff(const int xid, struct cifs_ses *ses)
786 LOGOFF_ANDX_REQ *pSMB;
789 cFYI(1, "In SMBLogoff for session disconnect");
792 * BB: do we need to check validity of ses and server? They should
793 * always be valid since we have an active reference. If not, that
794 * should probably be a BUG()
796 if (!ses || !ses->server)
799 mutex_lock(&ses->session_mutex);
800 if (ses->need_reconnect)
801 goto session_already_dead; /* no need to send SMBlogoff if uid
802 already closed due to reconnect */
803 rc = small_smb_init(SMB_COM_LOGOFF_ANDX, 2, NULL, (void **)&pSMB);
805 mutex_unlock(&ses->session_mutex);
809 pSMB->hdr.Mid = get_next_mid(ses->server);
811 if (ses->server->sec_mode &
812 (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
813 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
815 pSMB->hdr.Uid = ses->Suid;
817 pSMB->AndXCommand = 0xFF;
818 rc = SendReceiveNoRsp(xid, ses, (char *) pSMB, 0);
819 session_already_dead:
820 mutex_unlock(&ses->session_mutex);
822 /* if session dead then we do not need to do ulogoff,
823 since server closed smb session, no sense reporting
831 CIFSPOSIXDelFile(const int xid, struct cifs_tcon *tcon, const char *fileName,
832 __u16 type, const struct nls_table *nls_codepage, int remap)
834 TRANSACTION2_SPI_REQ *pSMB = NULL;
835 TRANSACTION2_SPI_RSP *pSMBr = NULL;
836 struct unlink_psx_rq *pRqD;
839 int bytes_returned = 0;
840 __u16 params, param_offset, offset, byte_count;
842 cFYI(1, "In POSIX delete");
844 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
849 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
851 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
852 PATH_MAX, nls_codepage, remap);
853 name_len++; /* trailing null */
855 } else { /* BB add path length overrun check */
856 name_len = strnlen(fileName, PATH_MAX);
857 name_len++; /* trailing null */
858 strncpy(pSMB->FileName, fileName, name_len);
861 params = 6 + name_len;
862 pSMB->MaxParameterCount = cpu_to_le16(2);
863 pSMB->MaxDataCount = 0; /* BB double check this with jra */
864 pSMB->MaxSetupCount = 0;
869 param_offset = offsetof(struct smb_com_transaction2_spi_req,
870 InformationLevel) - 4;
871 offset = param_offset + params;
873 /* Setup pointer to Request Data (inode type) */
874 pRqD = (struct unlink_psx_rq *)(((char *)&pSMB->hdr.Protocol) + offset);
875 pRqD->type = cpu_to_le16(type);
876 pSMB->ParameterOffset = cpu_to_le16(param_offset);
877 pSMB->DataOffset = cpu_to_le16(offset);
878 pSMB->SetupCount = 1;
880 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
881 byte_count = 3 /* pad */ + params + sizeof(struct unlink_psx_rq);
883 pSMB->DataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
884 pSMB->TotalDataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
885 pSMB->ParameterCount = cpu_to_le16(params);
886 pSMB->TotalParameterCount = pSMB->ParameterCount;
887 pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_UNLINK);
889 inc_rfc1001_len(pSMB, byte_count);
890 pSMB->ByteCount = cpu_to_le16(byte_count);
891 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
892 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
894 cFYI(1, "Posix delete returned %d", rc);
895 cifs_buf_release(pSMB);
897 cifs_stats_inc(&tcon->num_deletes);
906 CIFSSMBDelFile(const int xid, struct cifs_tcon *tcon, const char *fileName,
907 const struct nls_table *nls_codepage, int remap)
909 DELETE_FILE_REQ *pSMB = NULL;
910 DELETE_FILE_RSP *pSMBr = NULL;
916 rc = smb_init(SMB_COM_DELETE, 1, tcon, (void **) &pSMB,
921 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
923 cifsConvertToUTF16((__le16 *) pSMB->fileName, fileName,
924 PATH_MAX, nls_codepage, remap);
925 name_len++; /* trailing null */
927 } else { /* BB improve check for buffer overruns BB */
928 name_len = strnlen(fileName, PATH_MAX);
929 name_len++; /* trailing null */
930 strncpy(pSMB->fileName, fileName, name_len);
932 pSMB->SearchAttributes =
933 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM);
934 pSMB->BufferFormat = 0x04;
935 inc_rfc1001_len(pSMB, name_len + 1);
936 pSMB->ByteCount = cpu_to_le16(name_len + 1);
937 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
938 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
939 cifs_stats_inc(&tcon->num_deletes);
941 cFYI(1, "Error in RMFile = %d", rc);
943 cifs_buf_release(pSMB);
951 CIFSSMBRmDir(const int xid, struct cifs_tcon *tcon, const char *dirName,
952 const struct nls_table *nls_codepage, int remap)
954 DELETE_DIRECTORY_REQ *pSMB = NULL;
955 DELETE_DIRECTORY_RSP *pSMBr = NULL;
960 cFYI(1, "In CIFSSMBRmDir");
962 rc = smb_init(SMB_COM_DELETE_DIRECTORY, 0, tcon, (void **) &pSMB,
967 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
968 name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, dirName,
969 PATH_MAX, nls_codepage, remap);
970 name_len++; /* trailing null */
972 } else { /* BB improve check for buffer overruns BB */
973 name_len = strnlen(dirName, PATH_MAX);
974 name_len++; /* trailing null */
975 strncpy(pSMB->DirName, dirName, name_len);
978 pSMB->BufferFormat = 0x04;
979 inc_rfc1001_len(pSMB, name_len + 1);
980 pSMB->ByteCount = cpu_to_le16(name_len + 1);
981 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
982 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
983 cifs_stats_inc(&tcon->num_rmdirs);
985 cFYI(1, "Error in RMDir = %d", rc);
987 cifs_buf_release(pSMB);
994 CIFSSMBMkDir(const int xid, struct cifs_tcon *tcon,
995 const char *name, const struct nls_table *nls_codepage, int remap)
998 CREATE_DIRECTORY_REQ *pSMB = NULL;
999 CREATE_DIRECTORY_RSP *pSMBr = NULL;
1003 cFYI(1, "In CIFSSMBMkDir");
1005 rc = smb_init(SMB_COM_CREATE_DIRECTORY, 0, tcon, (void **) &pSMB,
1010 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1011 name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, name,
1012 PATH_MAX, nls_codepage, remap);
1013 name_len++; /* trailing null */
1015 } else { /* BB improve check for buffer overruns BB */
1016 name_len = strnlen(name, PATH_MAX);
1017 name_len++; /* trailing null */
1018 strncpy(pSMB->DirName, name, name_len);
1021 pSMB->BufferFormat = 0x04;
1022 inc_rfc1001_len(pSMB, name_len + 1);
1023 pSMB->ByteCount = cpu_to_le16(name_len + 1);
1024 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1025 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
1026 cifs_stats_inc(&tcon->num_mkdirs);
1028 cFYI(1, "Error in Mkdir = %d", rc);
1030 cifs_buf_release(pSMB);
1037 CIFSPOSIXCreate(const int xid, struct cifs_tcon *tcon, __u32 posix_flags,
1038 __u64 mode, __u16 *netfid, FILE_UNIX_BASIC_INFO *pRetData,
1039 __u32 *pOplock, const char *name,
1040 const struct nls_table *nls_codepage, int remap)
1042 TRANSACTION2_SPI_REQ *pSMB = NULL;
1043 TRANSACTION2_SPI_RSP *pSMBr = NULL;
1046 int bytes_returned = 0;
1047 __u16 params, param_offset, offset, byte_count, count;
1048 OPEN_PSX_REQ *pdata;
1049 OPEN_PSX_RSP *psx_rsp;
1051 cFYI(1, "In POSIX Create");
1053 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
1058 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1060 cifsConvertToUTF16((__le16 *) pSMB->FileName, name,
1061 PATH_MAX, nls_codepage, remap);
1062 name_len++; /* trailing null */
1064 } else { /* BB improve the check for buffer overruns BB */
1065 name_len = strnlen(name, PATH_MAX);
1066 name_len++; /* trailing null */
1067 strncpy(pSMB->FileName, name, name_len);
1070 params = 6 + name_len;
1071 count = sizeof(OPEN_PSX_REQ);
1072 pSMB->MaxParameterCount = cpu_to_le16(2);
1073 pSMB->MaxDataCount = cpu_to_le16(1000); /* large enough */
1074 pSMB->MaxSetupCount = 0;
1078 pSMB->Reserved2 = 0;
1079 param_offset = offsetof(struct smb_com_transaction2_spi_req,
1080 InformationLevel) - 4;
1081 offset = param_offset + params;
1082 pdata = (OPEN_PSX_REQ *)(((char *)&pSMB->hdr.Protocol) + offset);
1083 pdata->Level = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
1084 pdata->Permissions = cpu_to_le64(mode);
1085 pdata->PosixOpenFlags = cpu_to_le32(posix_flags);
1086 pdata->OpenFlags = cpu_to_le32(*pOplock);
1087 pSMB->ParameterOffset = cpu_to_le16(param_offset);
1088 pSMB->DataOffset = cpu_to_le16(offset);
1089 pSMB->SetupCount = 1;
1090 pSMB->Reserved3 = 0;
1091 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
1092 byte_count = 3 /* pad */ + params + count;
1094 pSMB->DataCount = cpu_to_le16(count);
1095 pSMB->ParameterCount = cpu_to_le16(params);
1096 pSMB->TotalDataCount = pSMB->DataCount;
1097 pSMB->TotalParameterCount = pSMB->ParameterCount;
1098 pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_OPEN);
1099 pSMB->Reserved4 = 0;
1100 inc_rfc1001_len(pSMB, byte_count);
1101 pSMB->ByteCount = cpu_to_le16(byte_count);
1102 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1103 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
1105 cFYI(1, "Posix create returned %d", rc);
1106 goto psx_create_err;
1109 cFYI(1, "copying inode info");
1110 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
1112 if (rc || get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)) {
1113 rc = -EIO; /* bad smb */
1114 goto psx_create_err;
1117 /* copy return information to pRetData */
1118 psx_rsp = (OPEN_PSX_RSP *)((char *) &pSMBr->hdr.Protocol
1119 + le16_to_cpu(pSMBr->t2.DataOffset));
1121 *pOplock = le16_to_cpu(psx_rsp->OplockFlags);
1123 *netfid = psx_rsp->Fid; /* cifs fid stays in le */
1124 /* Let caller know file was created so we can set the mode. */
1125 /* Do we care about the CreateAction in any other cases? */
1126 if (cpu_to_le32(FILE_CREATE) == psx_rsp->CreateAction)
1127 *pOplock |= CIFS_CREATE_ACTION;
1128 /* check to make sure response data is there */
1129 if (psx_rsp->ReturnedLevel != cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC)) {
1130 pRetData->Type = cpu_to_le32(-1); /* unknown */
1131 cFYI(DBG2, "unknown type");
1133 if (get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)
1134 + sizeof(FILE_UNIX_BASIC_INFO)) {
1135 cERROR(1, "Open response data too small");
1136 pRetData->Type = cpu_to_le32(-1);
1137 goto psx_create_err;
1139 memcpy((char *) pRetData,
1140 (char *)psx_rsp + sizeof(OPEN_PSX_RSP),
1141 sizeof(FILE_UNIX_BASIC_INFO));
1145 cifs_buf_release(pSMB);
1147 if (posix_flags & SMB_O_DIRECTORY)
1148 cifs_stats_inc(&tcon->num_posixmkdirs);
1150 cifs_stats_inc(&tcon->num_posixopens);
1158 static __u16 convert_disposition(int disposition)
1162 switch (disposition) {
1163 case FILE_SUPERSEDE:
1164 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1167 ofun = SMBOPEN_OAPPEND;
1170 ofun = SMBOPEN_OCREATE;
1173 ofun = SMBOPEN_OCREATE | SMBOPEN_OAPPEND;
1175 case FILE_OVERWRITE:
1176 ofun = SMBOPEN_OTRUNC;
1178 case FILE_OVERWRITE_IF:
1179 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1182 cFYI(1, "unknown disposition %d", disposition);
1183 ofun = SMBOPEN_OAPPEND; /* regular open */
1189 access_flags_to_smbopen_mode(const int access_flags)
1191 int masked_flags = access_flags & (GENERIC_READ | GENERIC_WRITE);
1193 if (masked_flags == GENERIC_READ)
1194 return SMBOPEN_READ;
1195 else if (masked_flags == GENERIC_WRITE)
1196 return SMBOPEN_WRITE;
1198 /* just go for read/write */
1199 return SMBOPEN_READWRITE;
1203 SMBLegacyOpen(const int xid, struct cifs_tcon *tcon,
1204 const char *fileName, const int openDisposition,
1205 const int access_flags, const int create_options, __u16 *netfid,
1206 int *pOplock, FILE_ALL_INFO *pfile_info,
1207 const struct nls_table *nls_codepage, int remap)
1210 OPENX_REQ *pSMB = NULL;
1211 OPENX_RSP *pSMBr = NULL;
1217 rc = smb_init(SMB_COM_OPEN_ANDX, 15, tcon, (void **) &pSMB,
1222 pSMB->AndXCommand = 0xFF; /* none */
1224 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1225 count = 1; /* account for one byte pad to word boundary */
1227 cifsConvertToUTF16((__le16 *) (pSMB->fileName + 1),
1228 fileName, PATH_MAX, nls_codepage, remap);
1229 name_len++; /* trailing null */
1231 } else { /* BB improve check for buffer overruns BB */
1232 count = 0; /* no pad */
1233 name_len = strnlen(fileName, PATH_MAX);
1234 name_len++; /* trailing null */
1235 strncpy(pSMB->fileName, fileName, name_len);
1237 if (*pOplock & REQ_OPLOCK)
1238 pSMB->OpenFlags = cpu_to_le16(REQ_OPLOCK);
1239 else if (*pOplock & REQ_BATCHOPLOCK)
1240 pSMB->OpenFlags = cpu_to_le16(REQ_BATCHOPLOCK);
1242 pSMB->OpenFlags |= cpu_to_le16(REQ_MORE_INFO);
1243 pSMB->Mode = cpu_to_le16(access_flags_to_smbopen_mode(access_flags));
1244 pSMB->Mode |= cpu_to_le16(0x40); /* deny none */
1245 /* set file as system file if special file such
1246 as fifo and server expecting SFU style and
1247 no Unix extensions */
1249 if (create_options & CREATE_OPTION_SPECIAL)
1250 pSMB->FileAttributes = cpu_to_le16(ATTR_SYSTEM);
1251 else /* BB FIXME BB */
1252 pSMB->FileAttributes = cpu_to_le16(0/*ATTR_NORMAL*/);
1254 if (create_options & CREATE_OPTION_READONLY)
1255 pSMB->FileAttributes |= cpu_to_le16(ATTR_READONLY);
1258 /* pSMB->CreateOptions = cpu_to_le32(create_options &
1259 CREATE_OPTIONS_MASK); */
1260 /* BB FIXME END BB */
1262 pSMB->Sattr = cpu_to_le16(ATTR_HIDDEN | ATTR_SYSTEM | ATTR_DIRECTORY);
1263 pSMB->OpenFunction = cpu_to_le16(convert_disposition(openDisposition));
1265 inc_rfc1001_len(pSMB, count);
1267 pSMB->ByteCount = cpu_to_le16(count);
1268 /* long_op set to 1 to allow for oplock break timeouts */
1269 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1270 (struct smb_hdr *)pSMBr, &bytes_returned, 0);
1271 cifs_stats_inc(&tcon->num_opens);
1273 cFYI(1, "Error in Open = %d", rc);
1275 /* BB verify if wct == 15 */
1277 /* *pOplock = pSMBr->OplockLevel; */ /* BB take from action field*/
1279 *netfid = pSMBr->Fid; /* cifs fid stays in le */
1280 /* Let caller know file was created so we can set the mode. */
1281 /* Do we care about the CreateAction in any other cases? */
1283 /* if (cpu_to_le32(FILE_CREATE) == pSMBr->CreateAction)
1284 *pOplock |= CIFS_CREATE_ACTION; */
1288 pfile_info->CreationTime = 0; /* BB convert CreateTime*/
1289 pfile_info->LastAccessTime = 0; /* BB fixme */
1290 pfile_info->LastWriteTime = 0; /* BB fixme */
1291 pfile_info->ChangeTime = 0; /* BB fixme */
1292 pfile_info->Attributes =
1293 cpu_to_le32(le16_to_cpu(pSMBr->FileAttributes));
1294 /* the file_info buf is endian converted by caller */
1295 pfile_info->AllocationSize =
1296 cpu_to_le64(le32_to_cpu(pSMBr->EndOfFile));
1297 pfile_info->EndOfFile = pfile_info->AllocationSize;
1298 pfile_info->NumberOfLinks = cpu_to_le32(1);
1299 pfile_info->DeletePending = 0;
1303 cifs_buf_release(pSMB);
1310 CIFSSMBOpen(const int xid, struct cifs_tcon *tcon,
1311 const char *fileName, const int openDisposition,
1312 const int access_flags, const int create_options, __u16 *netfid,
1313 int *pOplock, FILE_ALL_INFO *pfile_info,
1314 const struct nls_table *nls_codepage, int remap)
1317 OPEN_REQ *pSMB = NULL;
1318 OPEN_RSP *pSMBr = NULL;
1324 rc = smb_init(SMB_COM_NT_CREATE_ANDX, 24, tcon, (void **) &pSMB,
1329 pSMB->AndXCommand = 0xFF; /* none */
1331 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1332 count = 1; /* account for one byte pad to word boundary */
1334 cifsConvertToUTF16((__le16 *) (pSMB->fileName + 1),
1335 fileName, PATH_MAX, nls_codepage, remap);
1336 name_len++; /* trailing null */
1338 pSMB->NameLength = cpu_to_le16(name_len);
1339 } else { /* BB improve check for buffer overruns BB */
1340 count = 0; /* no pad */
1341 name_len = strnlen(fileName, PATH_MAX);
1342 name_len++; /* trailing null */
1343 pSMB->NameLength = cpu_to_le16(name_len);
1344 strncpy(pSMB->fileName, fileName, name_len);
1346 if (*pOplock & REQ_OPLOCK)
1347 pSMB->OpenFlags = cpu_to_le32(REQ_OPLOCK);
1348 else if (*pOplock & REQ_BATCHOPLOCK)
1349 pSMB->OpenFlags = cpu_to_le32(REQ_BATCHOPLOCK);
1350 pSMB->DesiredAccess = cpu_to_le32(access_flags);
1351 pSMB->AllocationSize = 0;
1352 /* set file as system file if special file such
1353 as fifo and server expecting SFU style and
1354 no Unix extensions */
1355 if (create_options & CREATE_OPTION_SPECIAL)
1356 pSMB->FileAttributes = cpu_to_le32(ATTR_SYSTEM);
1358 pSMB->FileAttributes = cpu_to_le32(ATTR_NORMAL);
1360 /* XP does not handle ATTR_POSIX_SEMANTICS */
1361 /* but it helps speed up case sensitive checks for other
1362 servers such as Samba */
1363 if (tcon->ses->capabilities & CAP_UNIX)
1364 pSMB->FileAttributes |= cpu_to_le32(ATTR_POSIX_SEMANTICS);
1366 if (create_options & CREATE_OPTION_READONLY)
1367 pSMB->FileAttributes |= cpu_to_le32(ATTR_READONLY);
1369 pSMB->ShareAccess = cpu_to_le32(FILE_SHARE_ALL);
1370 pSMB->CreateDisposition = cpu_to_le32(openDisposition);
1371 pSMB->CreateOptions = cpu_to_le32(create_options & CREATE_OPTIONS_MASK);
1372 /* BB Expirement with various impersonation levels and verify */
1373 pSMB->ImpersonationLevel = cpu_to_le32(SECURITY_IMPERSONATION);
1374 pSMB->SecurityFlags =
1375 SECURITY_CONTEXT_TRACKING | SECURITY_EFFECTIVE_ONLY;
1378 inc_rfc1001_len(pSMB, count);
1380 pSMB->ByteCount = cpu_to_le16(count);
1381 /* long_op set to 1 to allow for oplock break timeouts */
1382 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1383 (struct smb_hdr *)pSMBr, &bytes_returned, 0);
1384 cifs_stats_inc(&tcon->num_opens);
1386 cFYI(1, "Error in Open = %d", rc);
1388 *pOplock = pSMBr->OplockLevel; /* 1 byte no need to le_to_cpu */
1389 *netfid = pSMBr->Fid; /* cifs fid stays in le */
1390 /* Let caller know file was created so we can set the mode. */
1391 /* Do we care about the CreateAction in any other cases? */
1392 if (cpu_to_le32(FILE_CREATE) == pSMBr->CreateAction)
1393 *pOplock |= CIFS_CREATE_ACTION;
1395 memcpy((char *)pfile_info, (char *)&pSMBr->CreationTime,
1396 36 /* CreationTime to Attributes */);
1397 /* the file_info buf is endian converted by caller */
1398 pfile_info->AllocationSize = pSMBr->AllocationSize;
1399 pfile_info->EndOfFile = pSMBr->EndOfFile;
1400 pfile_info->NumberOfLinks = cpu_to_le32(1);
1401 pfile_info->DeletePending = 0;
1405 cifs_buf_release(pSMB);
1412 * Discard any remaining data in the current SMB. To do this, we borrow the
1416 cifs_readv_discard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1418 unsigned int rfclen = get_rfc1002_length(server->smallbuf);
1419 int remaining = rfclen + 4 - server->total_read;
1420 struct cifs_readdata *rdata = mid->callback_data;
1422 while (remaining > 0) {
1425 length = cifs_read_from_socket(server, server->bigbuf,
1426 min_t(unsigned int, remaining,
1427 CIFSMaxBufSize + MAX_HEADER_SIZE(server)));
1430 server->total_read += length;
1431 remaining -= length;
1434 dequeue_mid(mid, rdata->result);
1439 cifs_readv_receive(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1442 unsigned int data_offset, data_len;
1443 struct cifs_readdata *rdata = mid->callback_data;
1444 char *buf = server->smallbuf;
1445 unsigned int buflen = get_rfc1002_length(buf) + 4;
1447 cFYI(1, "%s: mid=%llu offset=%llu bytes=%u", __func__,
1448 mid->mid, rdata->offset, rdata->bytes);
1451 * read the rest of READ_RSP header (sans Data array), or whatever we
1452 * can if there's not enough data. At this point, we've read down to
1455 len = min_t(unsigned int, buflen, server->vals->read_rsp_size) -
1456 HEADER_SIZE(server) + 1;
1458 rdata->iov[0].iov_base = buf + HEADER_SIZE(server) - 1;
1459 rdata->iov[0].iov_len = len;
1461 length = cifs_readv_from_socket(server, rdata->iov, 1, len);
1464 server->total_read += length;
1466 /* Was the SMB read successful? */
1467 rdata->result = server->ops->map_error(buf, false);
1468 if (rdata->result != 0) {
1469 cFYI(1, "%s: server returned error %d", __func__,
1471 return cifs_readv_discard(server, mid);
1474 /* Is there enough to get to the rest of the READ_RSP header? */
1475 if (server->total_read < server->vals->read_rsp_size) {
1476 cFYI(1, "%s: server returned short header. got=%u expected=%zu",
1477 __func__, server->total_read,
1478 server->vals->read_rsp_size);
1479 rdata->result = -EIO;
1480 return cifs_readv_discard(server, mid);
1483 data_offset = server->ops->read_data_offset(buf) + 4;
1484 if (data_offset < server->total_read) {
1486 * win2k8 sometimes sends an offset of 0 when the read
1487 * is beyond the EOF. Treat it as if the data starts just after
1490 cFYI(1, "%s: data offset (%u) inside read response header",
1491 __func__, data_offset);
1492 data_offset = server->total_read;
1493 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
1494 /* data_offset is beyond the end of smallbuf */
1495 cFYI(1, "%s: data offset (%u) beyond end of smallbuf",
1496 __func__, data_offset);
1497 rdata->result = -EIO;
1498 return cifs_readv_discard(server, mid);
1501 cFYI(1, "%s: total_read=%u data_offset=%u", __func__,
1502 server->total_read, data_offset);
1504 len = data_offset - server->total_read;
1506 /* read any junk before data into the rest of smallbuf */
1507 rdata->iov[0].iov_base = buf + server->total_read;
1508 rdata->iov[0].iov_len = len;
1509 length = cifs_readv_from_socket(server, rdata->iov, 1, len);
1512 server->total_read += length;
1515 /* set up first iov for signature check */
1516 rdata->iov[0].iov_base = buf;
1517 rdata->iov[0].iov_len = server->total_read;
1518 cFYI(1, "0: iov_base=%p iov_len=%zu",
1519 rdata->iov[0].iov_base, rdata->iov[0].iov_len);
1521 /* how much data is in the response? */
1522 data_len = server->ops->read_data_length(buf);
1523 if (data_offset + data_len > buflen) {
1524 /* data_len is corrupt -- discard frame */
1525 rdata->result = -EIO;
1526 return cifs_readv_discard(server, mid);
1529 /* marshal up the page array */
1531 len = rdata->marshal_iov(rdata, data_len);
1535 /* issue the read if we have any iovecs left to fill */
1536 if (rdata->nr_iov > 1) {
1537 length = cifs_readv_from_socket(server, &rdata->iov[1],
1538 rdata->nr_iov - 1, len);
1541 server->total_read += length;
1546 rdata->bytes = length;
1548 cFYI(1, "total_read=%u buflen=%u remaining=%u", server->total_read,
1551 /* discard anything left over */
1552 if (server->total_read < buflen)
1553 return cifs_readv_discard(server, mid);
1555 dequeue_mid(mid, false);
1560 cifs_readv_callback(struct mid_q_entry *mid)
1562 struct cifs_readdata *rdata = mid->callback_data;
1563 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
1564 struct TCP_Server_Info *server = tcon->ses->server;
1566 cFYI(1, "%s: mid=%llu state=%d result=%d bytes=%u", __func__,
1567 mid->mid, mid->mid_state, rdata->result, rdata->bytes);
1569 switch (mid->mid_state) {
1570 case MID_RESPONSE_RECEIVED:
1571 /* result already set, check signature */
1572 if (server->sec_mode &
1573 (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED)) {
1574 if (cifs_verify_signature(rdata->iov, rdata->nr_iov,
1575 server, mid->sequence_number + 1))
1576 cERROR(1, "Unexpected SMB signature");
1578 /* FIXME: should this be counted toward the initiating task? */
1579 task_io_account_read(rdata->bytes);
1580 cifs_stats_bytes_read(tcon, rdata->bytes);
1582 case MID_REQUEST_SUBMITTED:
1583 case MID_RETRY_NEEDED:
1584 rdata->result = -EAGAIN;
1587 rdata->result = -EIO;
1590 queue_work(cifsiod_wq, &rdata->work);
1591 DeleteMidQEntry(mid);
1592 add_credits(server, 1);
1595 /* cifs_async_readv - send an async write, and set up mid to handle result */
1597 cifs_async_readv(struct cifs_readdata *rdata)
1600 READ_REQ *smb = NULL;
1602 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
1604 cFYI(1, "%s: offset=%llu bytes=%u", __func__,
1605 rdata->offset, rdata->bytes);
1607 if (tcon->ses->capabilities & CAP_LARGE_FILES)
1610 wct = 10; /* old style read */
1611 if ((rdata->offset >> 32) > 0) {
1612 /* can not handle this big offset for old */
1617 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **)&smb);
1621 smb->hdr.Pid = cpu_to_le16((__u16)rdata->pid);
1622 smb->hdr.PidHigh = cpu_to_le16((__u16)(rdata->pid >> 16));
1624 smb->AndXCommand = 0xFF; /* none */
1625 smb->Fid = rdata->cfile->netfid;
1626 smb->OffsetLow = cpu_to_le32(rdata->offset & 0xFFFFFFFF);
1628 smb->OffsetHigh = cpu_to_le32(rdata->offset >> 32);
1630 smb->MaxCount = cpu_to_le16(rdata->bytes & 0xFFFF);
1631 smb->MaxCountHigh = cpu_to_le32(rdata->bytes >> 16);
1635 /* old style read */
1636 struct smb_com_readx_req *smbr =
1637 (struct smb_com_readx_req *)smb;
1638 smbr->ByteCount = 0;
1641 /* 4 for RFC1001 length + 1 for BCC */
1642 rdata->iov[0].iov_base = smb;
1643 rdata->iov[0].iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4;
1645 kref_get(&rdata->refcount);
1646 rc = cifs_call_async(tcon->ses->server, rdata->iov, 1,
1647 cifs_readv_receive, cifs_readv_callback,
1651 cifs_stats_inc(&tcon->num_reads);
1653 kref_put(&rdata->refcount, cifs_readdata_release);
1655 cifs_small_buf_release(smb);
1660 CIFSSMBRead(const int xid, struct cifs_io_parms *io_parms, unsigned int *nbytes,
1661 char **buf, int *pbuf_type)
1664 READ_REQ *pSMB = NULL;
1665 READ_RSP *pSMBr = NULL;
1666 char *pReadData = NULL;
1668 int resp_buf_type = 0;
1670 __u32 pid = io_parms->pid;
1671 __u16 netfid = io_parms->netfid;
1672 __u64 offset = io_parms->offset;
1673 struct cifs_tcon *tcon = io_parms->tcon;
1674 unsigned int count = io_parms->length;
1676 cFYI(1, "Reading %d bytes on fid %d", count, netfid);
1677 if (tcon->ses->capabilities & CAP_LARGE_FILES)
1680 wct = 10; /* old style read */
1681 if ((offset >> 32) > 0) {
1682 /* can not handle this big offset for old */
1688 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **) &pSMB);
1692 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1693 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1695 /* tcon and ses pointer are checked in smb_init */
1696 if (tcon->ses->server == NULL)
1697 return -ECONNABORTED;
1699 pSMB->AndXCommand = 0xFF; /* none */
1701 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
1703 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
1705 pSMB->Remaining = 0;
1706 pSMB->MaxCount = cpu_to_le16(count & 0xFFFF);
1707 pSMB->MaxCountHigh = cpu_to_le32(count >> 16);
1709 pSMB->ByteCount = 0; /* no need to do le conversion since 0 */
1711 /* old style read */
1712 struct smb_com_readx_req *pSMBW =
1713 (struct smb_com_readx_req *)pSMB;
1714 pSMBW->ByteCount = 0;
1717 iov[0].iov_base = (char *)pSMB;
1718 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
1719 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
1720 &resp_buf_type, CIFS_LOG_ERROR);
1721 cifs_stats_inc(&tcon->num_reads);
1722 pSMBr = (READ_RSP *)iov[0].iov_base;
1724 cERROR(1, "Send error in read = %d", rc);
1726 int data_length = le16_to_cpu(pSMBr->DataLengthHigh);
1727 data_length = data_length << 16;
1728 data_length += le16_to_cpu(pSMBr->DataLength);
1729 *nbytes = data_length;
1731 /*check that DataLength would not go beyond end of SMB */
1732 if ((data_length > CIFSMaxBufSize)
1733 || (data_length > count)) {
1734 cFYI(1, "bad length %d for count %d",
1735 data_length, count);
1739 pReadData = (char *) (&pSMBr->hdr.Protocol) +
1740 le16_to_cpu(pSMBr->DataOffset);
1741 /* if (rc = copy_to_user(buf, pReadData, data_length)) {
1742 cERROR(1, "Faulting on read rc = %d",rc);
1744 }*/ /* can not use copy_to_user when using page cache*/
1746 memcpy(*buf, pReadData, data_length);
1750 /* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
1752 if (resp_buf_type == CIFS_SMALL_BUFFER)
1753 cifs_small_buf_release(iov[0].iov_base);
1754 else if (resp_buf_type == CIFS_LARGE_BUFFER)
1755 cifs_buf_release(iov[0].iov_base);
1756 } else if (resp_buf_type != CIFS_NO_BUFFER) {
1757 /* return buffer to caller to free */
1758 *buf = iov[0].iov_base;
1759 if (resp_buf_type == CIFS_SMALL_BUFFER)
1760 *pbuf_type = CIFS_SMALL_BUFFER;
1761 else if (resp_buf_type == CIFS_LARGE_BUFFER)
1762 *pbuf_type = CIFS_LARGE_BUFFER;
1763 } /* else no valid buffer on return - leave as null */
1765 /* Note: On -EAGAIN error only caller can retry on handle based calls
1766 since file handle passed in no longer valid */
1772 CIFSSMBWrite(const int xid, struct cifs_io_parms *io_parms,
1773 unsigned int *nbytes, const char *buf,
1774 const char __user *ubuf, const int long_op)
1777 WRITE_REQ *pSMB = NULL;
1778 WRITE_RSP *pSMBr = NULL;
1779 int bytes_returned, wct;
1782 __u32 pid = io_parms->pid;
1783 __u16 netfid = io_parms->netfid;
1784 __u64 offset = io_parms->offset;
1785 struct cifs_tcon *tcon = io_parms->tcon;
1786 unsigned int count = io_parms->length;
1790 /* cFYI(1, "write at %lld %d bytes", offset, count);*/
1791 if (tcon->ses == NULL)
1792 return -ECONNABORTED;
1794 if (tcon->ses->capabilities & CAP_LARGE_FILES)
1798 if ((offset >> 32) > 0) {
1799 /* can not handle big offset for old srv */
1804 rc = smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB,
1809 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1810 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1812 /* tcon and ses pointer are checked in smb_init */
1813 if (tcon->ses->server == NULL)
1814 return -ECONNABORTED;
1816 pSMB->AndXCommand = 0xFF; /* none */
1818 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
1820 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
1822 pSMB->Reserved = 0xFFFFFFFF;
1823 pSMB->WriteMode = 0;
1824 pSMB->Remaining = 0;
1826 /* Can increase buffer size if buffer is big enough in some cases ie we
1827 can send more if LARGE_WRITE_X capability returned by the server and if
1828 our buffer is big enough or if we convert to iovecs on socket writes
1829 and eliminate the copy to the CIFS buffer */
1830 if (tcon->ses->capabilities & CAP_LARGE_WRITE_X) {
1831 bytes_sent = min_t(const unsigned int, CIFSMaxBufSize, count);
1833 bytes_sent = (tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE)
1837 if (bytes_sent > count)
1840 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
1842 memcpy(pSMB->Data, buf, bytes_sent);
1844 if (copy_from_user(pSMB->Data, ubuf, bytes_sent)) {
1845 cifs_buf_release(pSMB);
1848 } else if (count != 0) {
1850 cifs_buf_release(pSMB);
1852 } /* else setting file size with write of zero bytes */
1854 byte_count = bytes_sent + 1; /* pad */
1855 else /* wct == 12 */
1856 byte_count = bytes_sent + 5; /* bigger pad, smaller smb hdr */
1858 pSMB->DataLengthLow = cpu_to_le16(bytes_sent & 0xFFFF);
1859 pSMB->DataLengthHigh = cpu_to_le16(bytes_sent >> 16);
1860 inc_rfc1001_len(pSMB, byte_count);
1863 pSMB->ByteCount = cpu_to_le16(byte_count);
1864 else { /* old style write has byte count 4 bytes earlier
1866 struct smb_com_writex_req *pSMBW =
1867 (struct smb_com_writex_req *)pSMB;
1868 pSMBW->ByteCount = cpu_to_le16(byte_count);
1871 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1872 (struct smb_hdr *) pSMBr, &bytes_returned, long_op);
1873 cifs_stats_inc(&tcon->num_writes);
1875 cFYI(1, "Send error in write = %d", rc);
1877 *nbytes = le16_to_cpu(pSMBr->CountHigh);
1878 *nbytes = (*nbytes) << 16;
1879 *nbytes += le16_to_cpu(pSMBr->Count);
1882 * Mask off high 16 bits when bytes written as returned by the
1883 * server is greater than bytes requested by the client. Some
1884 * OS/2 servers are known to set incorrect CountHigh values.
1886 if (*nbytes > count)
1890 cifs_buf_release(pSMB);
1892 /* Note: On -EAGAIN error only caller can retry on handle based calls
1893 since file handle passed in no longer valid */
1899 cifs_writedata_release(struct kref *refcount)
1901 struct cifs_writedata *wdata = container_of(refcount,
1902 struct cifs_writedata, refcount);
1905 cifsFileInfo_put(wdata->cfile);
1911 * Write failed with a retryable error. Resend the write request. It's also
1912 * possible that the page was redirtied so re-clean the page.
1915 cifs_writev_requeue(struct cifs_writedata *wdata)
1918 struct inode *inode = wdata->cfile->dentry->d_inode;
1920 for (i = 0; i < wdata->nr_pages; i++) {
1921 lock_page(wdata->pages[i]);
1922 clear_page_dirty_for_io(wdata->pages[i]);
1926 rc = cifs_async_writev(wdata);
1927 } while (rc == -EAGAIN);
1929 for (i = 0; i < wdata->nr_pages; i++) {
1931 SetPageError(wdata->pages[i]);
1932 unlock_page(wdata->pages[i]);
1935 mapping_set_error(inode->i_mapping, rc);
1936 kref_put(&wdata->refcount, cifs_writedata_release);
1940 cifs_writev_complete(struct work_struct *work)
1942 struct cifs_writedata *wdata = container_of(work,
1943 struct cifs_writedata, work);
1944 struct inode *inode = wdata->cfile->dentry->d_inode;
1947 if (wdata->result == 0) {
1948 spin_lock(&inode->i_lock);
1949 cifs_update_eof(CIFS_I(inode), wdata->offset, wdata->bytes);
1950 spin_unlock(&inode->i_lock);
1951 cifs_stats_bytes_written(tlink_tcon(wdata->cfile->tlink),
1953 } else if (wdata->sync_mode == WB_SYNC_ALL && wdata->result == -EAGAIN)
1954 return cifs_writev_requeue(wdata);
1956 for (i = 0; i < wdata->nr_pages; i++) {
1957 struct page *page = wdata->pages[i];
1958 if (wdata->result == -EAGAIN)
1959 __set_page_dirty_nobuffers(page);
1960 else if (wdata->result < 0)
1962 end_page_writeback(page);
1963 page_cache_release(page);
1965 if (wdata->result != -EAGAIN)
1966 mapping_set_error(inode->i_mapping, wdata->result);
1967 kref_put(&wdata->refcount, cifs_writedata_release);
1970 struct cifs_writedata *
1971 cifs_writedata_alloc(unsigned int nr_pages, work_func_t complete)
1973 struct cifs_writedata *wdata;
1975 /* this would overflow */
1976 if (nr_pages == 0) {
1977 cERROR(1, "%s: called with nr_pages == 0!", __func__);
1981 /* writedata + number of page pointers */
1982 wdata = kzalloc(sizeof(*wdata) +
1983 sizeof(struct page *) * (nr_pages - 1), GFP_NOFS);
1984 if (wdata != NULL) {
1985 kref_init(&wdata->refcount);
1986 INIT_LIST_HEAD(&wdata->list);
1987 init_completion(&wdata->done);
1988 INIT_WORK(&wdata->work, complete);
1994 * Check the mid_state and signature on received buffer (if any), and queue the
1995 * workqueue completion task.
1998 cifs_writev_callback(struct mid_q_entry *mid)
2000 struct cifs_writedata *wdata = mid->callback_data;
2001 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2002 unsigned int written;
2003 WRITE_RSP *smb = (WRITE_RSP *)mid->resp_buf;
2005 switch (mid->mid_state) {
2006 case MID_RESPONSE_RECEIVED:
2007 wdata->result = cifs_check_receive(mid, tcon->ses->server, 0);
2008 if (wdata->result != 0)
2011 written = le16_to_cpu(smb->CountHigh);
2013 written += le16_to_cpu(smb->Count);
2015 * Mask off high 16 bits when bytes written as returned
2016 * by the server is greater than bytes requested by the
2017 * client. OS/2 servers are known to set incorrect
2020 if (written > wdata->bytes)
2023 if (written < wdata->bytes)
2024 wdata->result = -ENOSPC;
2026 wdata->bytes = written;
2028 case MID_REQUEST_SUBMITTED:
2029 case MID_RETRY_NEEDED:
2030 wdata->result = -EAGAIN;
2033 wdata->result = -EIO;
2037 queue_work(cifsiod_wq, &wdata->work);
2038 DeleteMidQEntry(mid);
2039 add_credits(tcon->ses->server, 1);
2042 /* cifs_async_writev - send an async write, and set up mid to handle result */
2044 cifs_async_writev(struct cifs_writedata *wdata)
2046 int i, rc = -EACCES;
2047 WRITE_REQ *smb = NULL;
2049 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2050 struct kvec *iov = NULL;
2052 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
2056 if (wdata->offset >> 32 > 0) {
2057 /* can not handle big offset for old srv */
2062 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **)&smb);
2064 goto async_writev_out;
2066 /* 1 iov per page + 1 for header */
2067 iov = kzalloc((wdata->nr_pages + 1) * sizeof(*iov), GFP_NOFS);
2070 goto async_writev_out;
2073 smb->hdr.Pid = cpu_to_le16((__u16)wdata->pid);
2074 smb->hdr.PidHigh = cpu_to_le16((__u16)(wdata->pid >> 16));
2076 smb->AndXCommand = 0xFF; /* none */
2077 smb->Fid = wdata->cfile->netfid;
2078 smb->OffsetLow = cpu_to_le32(wdata->offset & 0xFFFFFFFF);
2080 smb->OffsetHigh = cpu_to_le32(wdata->offset >> 32);
2081 smb->Reserved = 0xFFFFFFFF;
2086 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
2088 /* 4 for RFC1001 length + 1 for BCC */
2089 iov[0].iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4 + 1;
2090 iov[0].iov_base = smb;
2093 * This function should marshal up the page array into the kvec
2094 * array, reserving [0] for the header. It should kmap the pages
2095 * and set the iov_len properly for each one. It may also set
2099 wdata->marshal_iov(iov, wdata);
2102 cFYI(1, "async write at %llu %u bytes", wdata->offset, wdata->bytes);
2104 smb->DataLengthLow = cpu_to_le16(wdata->bytes & 0xFFFF);
2105 smb->DataLengthHigh = cpu_to_le16(wdata->bytes >> 16);
2108 inc_rfc1001_len(&smb->hdr, wdata->bytes + 1);
2109 put_bcc(wdata->bytes + 1, &smb->hdr);
2112 struct smb_com_writex_req *smbw =
2113 (struct smb_com_writex_req *)smb;
2114 inc_rfc1001_len(&smbw->hdr, wdata->bytes + 5);
2115 put_bcc(wdata->bytes + 5, &smbw->hdr);
2116 iov[0].iov_len += 4; /* pad bigger by four bytes */
2119 kref_get(&wdata->refcount);
2120 rc = cifs_call_async(tcon->ses->server, iov, wdata->nr_pages + 1,
2121 NULL, cifs_writev_callback, wdata, false);
2124 cifs_stats_inc(&tcon->num_writes);
2126 kref_put(&wdata->refcount, cifs_writedata_release);
2128 /* send is done, unmap pages */
2129 for (i = 0; i < wdata->nr_pages; i++)
2130 kunmap(wdata->pages[i]);
2133 cifs_small_buf_release(smb);
2139 CIFSSMBWrite2(const int xid, struct cifs_io_parms *io_parms,
2140 unsigned int *nbytes, struct kvec *iov, int n_vec,
2144 WRITE_REQ *pSMB = NULL;
2147 int resp_buf_type = 0;
2148 __u32 pid = io_parms->pid;
2149 __u16 netfid = io_parms->netfid;
2150 __u64 offset = io_parms->offset;
2151 struct cifs_tcon *tcon = io_parms->tcon;
2152 unsigned int count = io_parms->length;
2156 cFYI(1, "write2 at %lld %d bytes", (long long)offset, count);
2158 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
2162 if ((offset >> 32) > 0) {
2163 /* can not handle big offset for old srv */
2167 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB);
2171 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
2172 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
2174 /* tcon and ses pointer are checked in smb_init */
2175 if (tcon->ses->server == NULL)
2176 return -ECONNABORTED;
2178 pSMB->AndXCommand = 0xFF; /* none */
2180 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
2182 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
2183 pSMB->Reserved = 0xFFFFFFFF;
2184 pSMB->WriteMode = 0;
2185 pSMB->Remaining = 0;
2188 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
2190 pSMB->DataLengthLow = cpu_to_le16(count & 0xFFFF);
2191 pSMB->DataLengthHigh = cpu_to_le16(count >> 16);
2192 /* header + 1 byte pad */
2193 smb_hdr_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 1;
2195 inc_rfc1001_len(pSMB, count + 1);
2196 else /* wct == 12 */
2197 inc_rfc1001_len(pSMB, count + 5); /* smb data starts later */
2199 pSMB->ByteCount = cpu_to_le16(count + 1);
2200 else /* wct == 12 */ /* bigger pad, smaller smb hdr, keep offset ok */ {
2201 struct smb_com_writex_req *pSMBW =
2202 (struct smb_com_writex_req *)pSMB;
2203 pSMBW->ByteCount = cpu_to_le16(count + 5);
2205 iov[0].iov_base = pSMB;
2207 iov[0].iov_len = smb_hdr_len + 4;
2208 else /* wct == 12 pad bigger by four bytes */
2209 iov[0].iov_len = smb_hdr_len + 8;
2212 rc = SendReceive2(xid, tcon->ses, iov, n_vec + 1, &resp_buf_type,
2214 cifs_stats_inc(&tcon->num_writes);
2216 cFYI(1, "Send error Write2 = %d", rc);
2217 } else if (resp_buf_type == 0) {
2218 /* presumably this can not happen, but best to be safe */
2221 WRITE_RSP *pSMBr = (WRITE_RSP *)iov[0].iov_base;
2222 *nbytes = le16_to_cpu(pSMBr->CountHigh);
2223 *nbytes = (*nbytes) << 16;
2224 *nbytes += le16_to_cpu(pSMBr->Count);
2227 * Mask off high 16 bits when bytes written as returned by the
2228 * server is greater than bytes requested by the client. OS/2
2229 * servers are known to set incorrect CountHigh values.
2231 if (*nbytes > count)
2235 /* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
2236 if (resp_buf_type == CIFS_SMALL_BUFFER)
2237 cifs_small_buf_release(iov[0].iov_base);
2238 else if (resp_buf_type == CIFS_LARGE_BUFFER)
2239 cifs_buf_release(iov[0].iov_base);
2241 /* Note: On -EAGAIN error only caller can retry on handle based calls
2242 since file handle passed in no longer valid */
2247 int cifs_lockv(const int xid, struct cifs_tcon *tcon, const __u16 netfid,
2248 const __u8 lock_type, const __u32 num_unlock,
2249 const __u32 num_lock, LOCKING_ANDX_RANGE *buf)
2252 LOCK_REQ *pSMB = NULL;
2257 cFYI(1, "cifs_lockv num lock %d num unlock %d", num_lock, num_unlock);
2259 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2264 pSMB->NumberOfLocks = cpu_to_le16(num_lock);
2265 pSMB->NumberOfUnlocks = cpu_to_le16(num_unlock);
2266 pSMB->LockType = lock_type;
2267 pSMB->AndXCommand = 0xFF; /* none */
2268 pSMB->Fid = netfid; /* netfid stays le */
2270 count = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2271 inc_rfc1001_len(pSMB, count);
2272 pSMB->ByteCount = cpu_to_le16(count);
2274 iov[0].iov_base = (char *)pSMB;
2275 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4 -
2276 (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2277 iov[1].iov_base = (char *)buf;
2278 iov[1].iov_len = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2280 cifs_stats_inc(&tcon->num_locks);
2281 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP);
2283 cFYI(1, "Send error in cifs_lockv = %d", rc);
2289 CIFSSMBLock(const int xid, struct cifs_tcon *tcon,
2290 const __u16 smb_file_id, const __u32 netpid, const __u64 len,
2291 const __u64 offset, const __u32 numUnlock,
2292 const __u32 numLock, const __u8 lockType,
2293 const bool waitFlag, const __u8 oplock_level)
2296 LOCK_REQ *pSMB = NULL;
2297 /* LOCK_RSP *pSMBr = NULL; */ /* No response data other than rc to parse */
2302 cFYI(1, "CIFSSMBLock timeout %d numLock %d", (int)waitFlag, numLock);
2303 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2308 if (lockType == LOCKING_ANDX_OPLOCK_RELEASE) {
2309 timeout = CIFS_ASYNC_OP; /* no response expected */
2311 } else if (waitFlag) {
2312 timeout = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
2313 pSMB->Timeout = cpu_to_le32(-1);/* blocking - do not time out */
2318 pSMB->NumberOfLocks = cpu_to_le16(numLock);
2319 pSMB->NumberOfUnlocks = cpu_to_le16(numUnlock);
2320 pSMB->LockType = lockType;
2321 pSMB->OplockLevel = oplock_level;
2322 pSMB->AndXCommand = 0xFF; /* none */
2323 pSMB->Fid = smb_file_id; /* netfid stays le */
2325 if ((numLock != 0) || (numUnlock != 0)) {
2326 pSMB->Locks[0].Pid = cpu_to_le16(netpid);
2327 /* BB where to store pid high? */
2328 pSMB->Locks[0].LengthLow = cpu_to_le32((u32)len);
2329 pSMB->Locks[0].LengthHigh = cpu_to_le32((u32)(len>>32));
2330 pSMB->Locks[0].OffsetLow = cpu_to_le32((u32)offset);
2331 pSMB->Locks[0].OffsetHigh = cpu_to_le32((u32)(offset>>32));
2332 count = sizeof(LOCKING_ANDX_RANGE);
2337 inc_rfc1001_len(pSMB, count);
2338 pSMB->ByteCount = cpu_to_le16(count);
2341 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
2342 (struct smb_hdr *) pSMB, &bytes_returned);
2343 cifs_small_buf_release(pSMB);
2345 rc = SendReceiveNoRsp(xid, tcon->ses, (char *)pSMB, timeout);
2346 /* SMB buffer freed by function above */
2348 cifs_stats_inc(&tcon->num_locks);
2350 cFYI(1, "Send error in Lock = %d", rc);
2352 /* Note: On -EAGAIN error only caller can retry on handle based calls
2353 since file handle passed in no longer valid */
2358 CIFSSMBPosixLock(const int xid, struct cifs_tcon *tcon,
2359 const __u16 smb_file_id, const __u32 netpid, const int get_flag,
2360 const __u64 len, struct file_lock *pLockData,
2361 const __u16 lock_type, const bool waitFlag)
2363 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2364 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
2365 struct cifs_posix_lock *parm_data;
2368 int bytes_returned = 0;
2369 int resp_buf_type = 0;
2370 __u16 params, param_offset, offset, byte_count, count;
2373 cFYI(1, "Posix Lock");
2375 if (pLockData == NULL)
2378 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
2383 pSMBr = (struct smb_com_transaction2_sfi_rsp *)pSMB;
2386 pSMB->MaxSetupCount = 0;
2389 pSMB->Reserved2 = 0;
2390 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2391 offset = param_offset + params;
2393 count = sizeof(struct cifs_posix_lock);
2394 pSMB->MaxParameterCount = cpu_to_le16(2);
2395 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
2396 pSMB->SetupCount = 1;
2397 pSMB->Reserved3 = 0;
2399 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
2401 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2402 byte_count = 3 /* pad */ + params + count;
2403 pSMB->DataCount = cpu_to_le16(count);
2404 pSMB->ParameterCount = cpu_to_le16(params);
2405 pSMB->TotalDataCount = pSMB->DataCount;
2406 pSMB->TotalParameterCount = pSMB->ParameterCount;
2407 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2408 parm_data = (struct cifs_posix_lock *)
2409 (((char *) &pSMB->hdr.Protocol) + offset);
2411 parm_data->lock_type = cpu_to_le16(lock_type);
2413 timeout = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
2414 parm_data->lock_flags = cpu_to_le16(1);
2415 pSMB->Timeout = cpu_to_le32(-1);
2419 parm_data->pid = cpu_to_le32(netpid);
2420 parm_data->start = cpu_to_le64(pLockData->fl_start);
2421 parm_data->length = cpu_to_le64(len); /* normalize negative numbers */
2423 pSMB->DataOffset = cpu_to_le16(offset);
2424 pSMB->Fid = smb_file_id;
2425 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_LOCK);
2426 pSMB->Reserved4 = 0;
2427 inc_rfc1001_len(pSMB, byte_count);
2428 pSMB->ByteCount = cpu_to_le16(byte_count);
2430 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
2431 (struct smb_hdr *) pSMBr, &bytes_returned);
2433 iov[0].iov_base = (char *)pSMB;
2434 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
2435 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
2436 &resp_buf_type, timeout);
2437 pSMB = NULL; /* request buf already freed by SendReceive2. Do
2438 not try to free it twice below on exit */
2439 pSMBr = (struct smb_com_transaction2_sfi_rsp *)iov[0].iov_base;
2443 cFYI(1, "Send error in Posix Lock = %d", rc);
2444 } else if (get_flag) {
2445 /* lock structure can be returned on get */
2448 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
2450 if (rc || get_bcc(&pSMBr->hdr) < sizeof(*parm_data)) {
2451 rc = -EIO; /* bad smb */
2454 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
2455 data_count = le16_to_cpu(pSMBr->t2.DataCount);
2456 if (data_count < sizeof(struct cifs_posix_lock)) {
2460 parm_data = (struct cifs_posix_lock *)
2461 ((char *)&pSMBr->hdr.Protocol + data_offset);
2462 if (parm_data->lock_type == __constant_cpu_to_le16(CIFS_UNLCK))
2463 pLockData->fl_type = F_UNLCK;
2465 if (parm_data->lock_type ==
2466 __constant_cpu_to_le16(CIFS_RDLCK))
2467 pLockData->fl_type = F_RDLCK;
2468 else if (parm_data->lock_type ==
2469 __constant_cpu_to_le16(CIFS_WRLCK))
2470 pLockData->fl_type = F_WRLCK;
2472 pLockData->fl_start = le64_to_cpu(parm_data->start);
2473 pLockData->fl_end = pLockData->fl_start +
2474 le64_to_cpu(parm_data->length) - 1;
2475 pLockData->fl_pid = le32_to_cpu(parm_data->pid);
2481 cifs_small_buf_release(pSMB);
2483 if (resp_buf_type == CIFS_SMALL_BUFFER)
2484 cifs_small_buf_release(iov[0].iov_base);
2485 else if (resp_buf_type == CIFS_LARGE_BUFFER)
2486 cifs_buf_release(iov[0].iov_base);
2488 /* Note: On -EAGAIN error only caller can retry on handle based calls
2489 since file handle passed in no longer valid */
2496 CIFSSMBClose(const int xid, struct cifs_tcon *tcon, int smb_file_id)
2499 CLOSE_REQ *pSMB = NULL;
2500 cFYI(1, "In CIFSSMBClose");
2502 /* do not retry on dead session on close */
2503 rc = small_smb_init(SMB_COM_CLOSE, 3, tcon, (void **) &pSMB);
2509 pSMB->FileID = (__u16) smb_file_id;
2510 pSMB->LastWriteTime = 0xFFFFFFFF;
2511 pSMB->ByteCount = 0;
2512 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
2513 cifs_stats_inc(&tcon->num_closes);
2516 /* EINTR is expected when user ctl-c to kill app */
2517 cERROR(1, "Send error in Close = %d", rc);
2521 /* Since session is dead, file will be closed on server already */
2529 CIFSSMBFlush(const int xid, struct cifs_tcon *tcon, int smb_file_id)
2532 FLUSH_REQ *pSMB = NULL;
2533 cFYI(1, "In CIFSSMBFlush");
2535 rc = small_smb_init(SMB_COM_FLUSH, 1, tcon, (void **) &pSMB);
2539 pSMB->FileID = (__u16) smb_file_id;
2540 pSMB->ByteCount = 0;
2541 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
2542 cifs_stats_inc(&tcon->num_flushes);
2544 cERROR(1, "Send error in Flush = %d", rc);
2550 CIFSSMBRename(const int xid, struct cifs_tcon *tcon,
2551 const char *fromName, const char *toName,
2552 const struct nls_table *nls_codepage, int remap)
2555 RENAME_REQ *pSMB = NULL;
2556 RENAME_RSP *pSMBr = NULL;
2558 int name_len, name_len2;
2561 cFYI(1, "In CIFSSMBRename");
2563 rc = smb_init(SMB_COM_RENAME, 1, tcon, (void **) &pSMB,
2568 pSMB->BufferFormat = 0x04;
2569 pSMB->SearchAttributes =
2570 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
2573 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2575 cifsConvertToUTF16((__le16 *) pSMB->OldFileName, fromName,
2576 PATH_MAX, nls_codepage, remap);
2577 name_len++; /* trailing null */
2579 pSMB->OldFileName[name_len] = 0x04; /* pad */
2580 /* protocol requires ASCII signature byte on Unicode string */
2581 pSMB->OldFileName[name_len + 1] = 0x00;
2583 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2584 toName, PATH_MAX, nls_codepage, remap);
2585 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2586 name_len2 *= 2; /* convert to bytes */
2587 } else { /* BB improve the check for buffer overruns BB */
2588 name_len = strnlen(fromName, PATH_MAX);
2589 name_len++; /* trailing null */
2590 strncpy(pSMB->OldFileName, fromName, name_len);
2591 name_len2 = strnlen(toName, PATH_MAX);
2592 name_len2++; /* trailing null */
2593 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2594 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
2595 name_len2++; /* trailing null */
2596 name_len2++; /* signature byte */
2599 count = 1 /* 1st signature byte */ + name_len + name_len2;
2600 inc_rfc1001_len(pSMB, count);
2601 pSMB->ByteCount = cpu_to_le16(count);
2603 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2604 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2605 cifs_stats_inc(&tcon->num_renames);
2607 cFYI(1, "Send error in rename = %d", rc);
2609 cifs_buf_release(pSMB);
2617 int CIFSSMBRenameOpenFile(const int xid, struct cifs_tcon *pTcon,
2618 int netfid, const char *target_name,
2619 const struct nls_table *nls_codepage, int remap)
2621 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2622 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
2623 struct set_file_rename *rename_info;
2625 char dummy_string[30];
2627 int bytes_returned = 0;
2629 __u16 params, param_offset, offset, count, byte_count;
2631 cFYI(1, "Rename to File by handle");
2632 rc = smb_init(SMB_COM_TRANSACTION2, 15, pTcon, (void **) &pSMB,
2638 pSMB->MaxSetupCount = 0;
2642 pSMB->Reserved2 = 0;
2643 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2644 offset = param_offset + params;
2646 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2647 rename_info = (struct set_file_rename *) data_offset;
2648 pSMB->MaxParameterCount = cpu_to_le16(2);
2649 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
2650 pSMB->SetupCount = 1;
2651 pSMB->Reserved3 = 0;
2652 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2653 byte_count = 3 /* pad */ + params;
2654 pSMB->ParameterCount = cpu_to_le16(params);
2655 pSMB->TotalParameterCount = pSMB->ParameterCount;
2656 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2657 pSMB->DataOffset = cpu_to_le16(offset);
2658 /* construct random name ".cifs_tmp<inodenum><mid>" */
2659 rename_info->overwrite = cpu_to_le32(1);
2660 rename_info->root_fid = 0;
2661 /* unicode only call */
2662 if (target_name == NULL) {
2663 sprintf(dummy_string, "cifs%x", pSMB->hdr.Mid);
2665 cifsConvertToUTF16((__le16 *)rename_info->target_name,
2666 dummy_string, 24, nls_codepage, remap);
2669 cifsConvertToUTF16((__le16 *)rename_info->target_name,
2670 target_name, PATH_MAX, nls_codepage,
2673 rename_info->target_name_len = cpu_to_le32(2 * len_of_str);
2674 count = 12 /* sizeof(struct set_file_rename) */ + (2 * len_of_str);
2675 byte_count += count;
2676 pSMB->DataCount = cpu_to_le16(count);
2677 pSMB->TotalDataCount = pSMB->DataCount;
2679 pSMB->InformationLevel =
2680 cpu_to_le16(SMB_SET_FILE_RENAME_INFORMATION);
2681 pSMB->Reserved4 = 0;
2682 inc_rfc1001_len(pSMB, byte_count);
2683 pSMB->ByteCount = cpu_to_le16(byte_count);
2684 rc = SendReceive(xid, pTcon->ses, (struct smb_hdr *) pSMB,
2685 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2686 cifs_stats_inc(&pTcon->num_t2renames);
2688 cFYI(1, "Send error in Rename (by file handle) = %d", rc);
2690 cifs_buf_release(pSMB);
2692 /* Note: On -EAGAIN error only caller can retry on handle based calls
2693 since file handle passed in no longer valid */
2699 CIFSSMBCopy(const int xid, struct cifs_tcon *tcon, const char *fromName,
2700 const __u16 target_tid, const char *toName, const int flags,
2701 const struct nls_table *nls_codepage, int remap)
2704 COPY_REQ *pSMB = NULL;
2705 COPY_RSP *pSMBr = NULL;
2707 int name_len, name_len2;
2710 cFYI(1, "In CIFSSMBCopy");
2712 rc = smb_init(SMB_COM_COPY, 1, tcon, (void **) &pSMB,
2717 pSMB->BufferFormat = 0x04;
2718 pSMB->Tid2 = target_tid;
2720 pSMB->Flags = cpu_to_le16(flags & COPY_TREE);
2722 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2723 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2724 fromName, PATH_MAX, nls_codepage,
2726 name_len++; /* trailing null */
2728 pSMB->OldFileName[name_len] = 0x04; /* pad */
2729 /* protocol requires ASCII signature byte on Unicode string */
2730 pSMB->OldFileName[name_len + 1] = 0x00;
2732 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2733 toName, PATH_MAX, nls_codepage, remap);
2734 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2735 name_len2 *= 2; /* convert to bytes */
2736 } else { /* BB improve the check for buffer overruns BB */
2737 name_len = strnlen(fromName, PATH_MAX);
2738 name_len++; /* trailing null */
2739 strncpy(pSMB->OldFileName, fromName, name_len);
2740 name_len2 = strnlen(toName, PATH_MAX);
2741 name_len2++; /* trailing null */
2742 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2743 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
2744 name_len2++; /* trailing null */
2745 name_len2++; /* signature byte */
2748 count = 1 /* 1st signature byte */ + name_len + name_len2;
2749 inc_rfc1001_len(pSMB, count);
2750 pSMB->ByteCount = cpu_to_le16(count);
2752 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2753 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2755 cFYI(1, "Send error in copy = %d with %d files copied",
2756 rc, le16_to_cpu(pSMBr->CopyCount));
2758 cifs_buf_release(pSMB);
2767 CIFSUnixCreateSymLink(const int xid, struct cifs_tcon *tcon,
2768 const char *fromName, const char *toName,
2769 const struct nls_table *nls_codepage)
2771 TRANSACTION2_SPI_REQ *pSMB = NULL;
2772 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2775 int name_len_target;
2777 int bytes_returned = 0;
2778 __u16 params, param_offset, offset, byte_count;
2780 cFYI(1, "In Symlink Unix style");
2782 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2787 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2789 cifs_strtoUTF16((__le16 *) pSMB->FileName, fromName,
2790 /* find define for this maxpathcomponent */
2791 PATH_MAX, nls_codepage);
2792 name_len++; /* trailing null */
2795 } else { /* BB improve the check for buffer overruns BB */
2796 name_len = strnlen(fromName, PATH_MAX);
2797 name_len++; /* trailing null */
2798 strncpy(pSMB->FileName, fromName, name_len);
2800 params = 6 + name_len;
2801 pSMB->MaxSetupCount = 0;
2805 pSMB->Reserved2 = 0;
2806 param_offset = offsetof(struct smb_com_transaction2_spi_req,
2807 InformationLevel) - 4;
2808 offset = param_offset + params;
2810 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2811 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2813 cifs_strtoUTF16((__le16 *) data_offset, toName, PATH_MAX
2814 /* find define for this maxpathcomponent */
2816 name_len_target++; /* trailing null */
2817 name_len_target *= 2;
2818 } else { /* BB improve the check for buffer overruns BB */
2819 name_len_target = strnlen(toName, PATH_MAX);
2820 name_len_target++; /* trailing null */
2821 strncpy(data_offset, toName, name_len_target);
2824 pSMB->MaxParameterCount = cpu_to_le16(2);
2825 /* BB find exact max on data count below from sess */
2826 pSMB->MaxDataCount = cpu_to_le16(1000);
2827 pSMB->SetupCount = 1;
2828 pSMB->Reserved3 = 0;
2829 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2830 byte_count = 3 /* pad */ + params + name_len_target;
2831 pSMB->DataCount = cpu_to_le16(name_len_target);
2832 pSMB->ParameterCount = cpu_to_le16(params);
2833 pSMB->TotalDataCount = pSMB->DataCount;
2834 pSMB->TotalParameterCount = pSMB->ParameterCount;
2835 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2836 pSMB->DataOffset = cpu_to_le16(offset);
2837 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_LINK);
2838 pSMB->Reserved4 = 0;
2839 inc_rfc1001_len(pSMB, byte_count);
2840 pSMB->ByteCount = cpu_to_le16(byte_count);
2841 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2842 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2843 cifs_stats_inc(&tcon->num_symlinks);
2845 cFYI(1, "Send error in SetPathInfo create symlink = %d", rc);
2847 cifs_buf_release(pSMB);
2850 goto createSymLinkRetry;
2856 CIFSUnixCreateHardLink(const int xid, struct cifs_tcon *tcon,
2857 const char *fromName, const char *toName,
2858 const struct nls_table *nls_codepage, int remap)
2860 TRANSACTION2_SPI_REQ *pSMB = NULL;
2861 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2864 int name_len_target;
2866 int bytes_returned = 0;
2867 __u16 params, param_offset, offset, byte_count;
2869 cFYI(1, "In Create Hard link Unix style");
2870 createHardLinkRetry:
2871 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2876 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2877 name_len = cifsConvertToUTF16((__le16 *) pSMB->FileName, toName,
2878 PATH_MAX, nls_codepage, remap);
2879 name_len++; /* trailing null */
2882 } else { /* BB improve the check for buffer overruns BB */
2883 name_len = strnlen(toName, PATH_MAX);
2884 name_len++; /* trailing null */
2885 strncpy(pSMB->FileName, toName, name_len);
2887 params = 6 + name_len;
2888 pSMB->MaxSetupCount = 0;
2892 pSMB->Reserved2 = 0;
2893 param_offset = offsetof(struct smb_com_transaction2_spi_req,
2894 InformationLevel) - 4;
2895 offset = param_offset + params;
2897 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2898 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2900 cifsConvertToUTF16((__le16 *) data_offset, fromName,
2901 PATH_MAX, nls_codepage, remap);
2902 name_len_target++; /* trailing null */
2903 name_len_target *= 2;
2904 } else { /* BB improve the check for buffer overruns BB */
2905 name_len_target = strnlen(fromName, PATH_MAX);
2906 name_len_target++; /* trailing null */
2907 strncpy(data_offset, fromName, name_len_target);
2910 pSMB->MaxParameterCount = cpu_to_le16(2);
2911 /* BB find exact max on data count below from sess*/
2912 pSMB->MaxDataCount = cpu_to_le16(1000);
2913 pSMB->SetupCount = 1;
2914 pSMB->Reserved3 = 0;
2915 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2916 byte_count = 3 /* pad */ + params + name_len_target;
2917 pSMB->ParameterCount = cpu_to_le16(params);
2918 pSMB->TotalParameterCount = pSMB->ParameterCount;
2919 pSMB->DataCount = cpu_to_le16(name_len_target);
2920 pSMB->TotalDataCount = pSMB->DataCount;
2921 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2922 pSMB->DataOffset = cpu_to_le16(offset);
2923 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_HLINK);
2924 pSMB->Reserved4 = 0;
2925 inc_rfc1001_len(pSMB, byte_count);
2926 pSMB->ByteCount = cpu_to_le16(byte_count);
2927 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2928 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2929 cifs_stats_inc(&tcon->num_hardlinks);
2931 cFYI(1, "Send error in SetPathInfo (hard link) = %d", rc);
2933 cifs_buf_release(pSMB);
2935 goto createHardLinkRetry;
2941 CIFSCreateHardLink(const int xid, struct cifs_tcon *tcon,
2942 const char *fromName, const char *toName,
2943 const struct nls_table *nls_codepage, int remap)
2946 NT_RENAME_REQ *pSMB = NULL;
2947 RENAME_RSP *pSMBr = NULL;
2949 int name_len, name_len2;
2952 cFYI(1, "In CIFSCreateHardLink");
2953 winCreateHardLinkRetry:
2955 rc = smb_init(SMB_COM_NT_RENAME, 4, tcon, (void **) &pSMB,
2960 pSMB->SearchAttributes =
2961 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
2963 pSMB->Flags = cpu_to_le16(CREATE_HARD_LINK);
2964 pSMB->ClusterCount = 0;
2966 pSMB->BufferFormat = 0x04;
2968 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2970 cifsConvertToUTF16((__le16 *) pSMB->OldFileName, fromName,
2971 PATH_MAX, nls_codepage, remap);
2972 name_len++; /* trailing null */
2975 /* protocol specifies ASCII buffer format (0x04) for unicode */
2976 pSMB->OldFileName[name_len] = 0x04;
2977 pSMB->OldFileName[name_len + 1] = 0x00; /* pad */
2979 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2980 toName, PATH_MAX, nls_codepage, remap);
2981 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2982 name_len2 *= 2; /* convert to bytes */
2983 } else { /* BB improve the check for buffer overruns BB */
2984 name_len = strnlen(fromName, PATH_MAX);
2985 name_len++; /* trailing null */
2986 strncpy(pSMB->OldFileName, fromName, name_len);
2987 name_len2 = strnlen(toName, PATH_MAX);
2988 name_len2++; /* trailing null */
2989 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2990 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
2991 name_len2++; /* trailing null */
2992 name_len2++; /* signature byte */
2995 count = 1 /* string type byte */ + name_len + name_len2;
2996 inc_rfc1001_len(pSMB, count);
2997 pSMB->ByteCount = cpu_to_le16(count);
2999 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3000 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3001 cifs_stats_inc(&tcon->num_hardlinks);
3003 cFYI(1, "Send error in hard link (NT rename) = %d", rc);
3005 cifs_buf_release(pSMB);
3007 goto winCreateHardLinkRetry;
3013 CIFSSMBUnixQuerySymLink(const int xid, struct cifs_tcon *tcon,
3014 const unsigned char *searchName, char **symlinkinfo,
3015 const struct nls_table *nls_codepage)
3017 /* SMB_QUERY_FILE_UNIX_LINK */
3018 TRANSACTION2_QPI_REQ *pSMB = NULL;
3019 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3023 __u16 params, byte_count;
3026 cFYI(1, "In QPathSymLinkInfo (Unix) for path %s", searchName);
3029 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3034 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3036 cifs_strtoUTF16((__le16 *) pSMB->FileName, searchName,
3037 PATH_MAX, nls_codepage);
3038 name_len++; /* trailing null */
3040 } else { /* BB improve the check for buffer overruns BB */
3041 name_len = strnlen(searchName, PATH_MAX);
3042 name_len++; /* trailing null */
3043 strncpy(pSMB->FileName, searchName, name_len);
3046 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3047 pSMB->TotalDataCount = 0;
3048 pSMB->MaxParameterCount = cpu_to_le16(2);
3049 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
3050 pSMB->MaxSetupCount = 0;
3054 pSMB->Reserved2 = 0;
3055 pSMB->ParameterOffset = cpu_to_le16(offsetof(
3056 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
3057 pSMB->DataCount = 0;
3058 pSMB->DataOffset = 0;
3059 pSMB->SetupCount = 1;
3060 pSMB->Reserved3 = 0;
3061 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3062 byte_count = params + 1 /* pad */ ;
3063 pSMB->TotalParameterCount = cpu_to_le16(params);
3064 pSMB->ParameterCount = pSMB->TotalParameterCount;
3065 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_LINK);
3066 pSMB->Reserved4 = 0;
3067 inc_rfc1001_len(pSMB, byte_count);
3068 pSMB->ByteCount = cpu_to_le16(byte_count);
3070 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3071 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3073 cFYI(1, "Send error in QuerySymLinkInfo = %d", rc);
3075 /* decode response */
3077 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
3078 /* BB also check enough total bytes returned */
3079 if (rc || get_bcc(&pSMBr->hdr) < 2)
3083 u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3085 data_start = ((char *) &pSMBr->hdr.Protocol) +
3086 le16_to_cpu(pSMBr->t2.DataOffset);
3088 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3093 /* BB FIXME investigate remapping reserved chars here */
3094 *symlinkinfo = cifs_strndup_from_utf16(data_start,
3095 count, is_unicode, nls_codepage);
3100 cifs_buf_release(pSMB);
3102 goto querySymLinkRetry;
3106 #ifdef CONFIG_CIFS_SYMLINK_EXPERIMENTAL
3108 * Recent Windows versions now create symlinks more frequently
3109 * and they use the "reparse point" mechanism below. We can of course
3110 * do symlinks nicely to Samba and other servers which support the
3111 * CIFS Unix Extensions and we can also do SFU symlinks and "client only"
3112 * "MF" symlinks optionally, but for recent Windows we really need to
3113 * reenable the code below and fix the cifs_symlink callers to handle this.
3114 * In the interim this code has been moved to its own config option so
3115 * it is not compiled in by default until callers fixed up and more tested.
3118 CIFSSMBQueryReparseLinkInfo(const int xid, struct cifs_tcon *tcon,
3119 const unsigned char *searchName,
3120 char *symlinkinfo, const int buflen, __u16 fid,
3121 const struct nls_table *nls_codepage)
3125 struct smb_com_transaction_ioctl_req *pSMB;
3126 struct smb_com_transaction_ioctl_rsp *pSMBr;
3128 cFYI(1, "In Windows reparse style QueryLink for path %s", searchName);
3129 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3134 pSMB->TotalParameterCount = 0 ;
3135 pSMB->TotalDataCount = 0;
3136 pSMB->MaxParameterCount = cpu_to_le32(2);
3137 /* BB find exact data count max from sess structure BB */
3138 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
3139 pSMB->MaxSetupCount = 4;
3141 pSMB->ParameterOffset = 0;
3142 pSMB->DataCount = 0;
3143 pSMB->DataOffset = 0;
3144 pSMB->SetupCount = 4;
3145 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
3146 pSMB->ParameterCount = pSMB->TotalParameterCount;
3147 pSMB->FunctionCode = cpu_to_le32(FSCTL_GET_REPARSE_POINT);
3148 pSMB->IsFsctl = 1; /* FSCTL */
3149 pSMB->IsRootFlag = 0;
3150 pSMB->Fid = fid; /* file handle always le */
3151 pSMB->ByteCount = 0;
3153 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3154 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3156 cFYI(1, "Send error in QueryReparseLinkInfo = %d", rc);
3157 } else { /* decode response */
3158 __u32 data_offset = le32_to_cpu(pSMBr->DataOffset);
3159 __u32 data_count = le32_to_cpu(pSMBr->DataCount);
3160 if (get_bcc(&pSMBr->hdr) < 2 || data_offset > 512) {
3161 /* BB also check enough total bytes returned */
3162 rc = -EIO; /* bad smb */
3165 if (data_count && (data_count < 2048)) {
3166 char *end_of_smb = 2 /* sizeof byte count */ +
3167 get_bcc(&pSMBr->hdr) + (char *)&pSMBr->ByteCount;
3169 struct reparse_data *reparse_buf =
3170 (struct reparse_data *)
3171 ((char *)&pSMBr->hdr.Protocol
3173 if ((char *)reparse_buf >= end_of_smb) {
3177 if ((reparse_buf->LinkNamesBuf +
3178 reparse_buf->TargetNameOffset +
3179 reparse_buf->TargetNameLen) > end_of_smb) {
3180 cFYI(1, "reparse buf beyond SMB");
3185 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE) {
3186 cifs_from_ucs2(symlinkinfo, (__le16 *)
3187 (reparse_buf->LinkNamesBuf +
3188 reparse_buf->TargetNameOffset),
3190 reparse_buf->TargetNameLen,
3192 } else { /* ASCII names */
3193 strncpy(symlinkinfo,
3194 reparse_buf->LinkNamesBuf +
3195 reparse_buf->TargetNameOffset,
3196 min_t(const int, buflen,
3197 reparse_buf->TargetNameLen));
3201 cFYI(1, "Invalid return data count on "
3202 "get reparse info ioctl");
3204 symlinkinfo[buflen] = 0; /* just in case so the caller
3205 does not go off the end of the buffer */
3206 cFYI(1, "readlink result - %s", symlinkinfo);
3210 cifs_buf_release(pSMB);
3212 /* Note: On -EAGAIN error only caller can retry on handle based calls
3213 since file handle passed in no longer valid */
3217 #endif /* CIFS_SYMLINK_EXPERIMENTAL */ /* BB temporarily unused */
3219 #ifdef CONFIG_CIFS_POSIX
3221 /*Convert an Access Control Entry from wire format to local POSIX xattr format*/
3222 static void cifs_convert_ace(posix_acl_xattr_entry *ace,
3223 struct cifs_posix_ace *cifs_ace)
3225 /* u8 cifs fields do not need le conversion */
3226 ace->e_perm = cpu_to_le16(cifs_ace->cifs_e_perm);
3227 ace->e_tag = cpu_to_le16(cifs_ace->cifs_e_tag);
3228 ace->e_id = cpu_to_le32(le64_to_cpu(cifs_ace->cifs_uid));
3229 /* cFYI(1, "perm %d tag %d id %d",ace->e_perm,ace->e_tag,ace->e_id); */
3234 /* Convert ACL from CIFS POSIX wire format to local Linux POSIX ACL xattr */
3235 static int cifs_copy_posix_acl(char *trgt, char *src, const int buflen,
3236 const int acl_type, const int size_of_data_area)
3241 struct cifs_posix_ace *pACE;
3242 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)src;
3243 posix_acl_xattr_header *local_acl = (posix_acl_xattr_header *)trgt;
3245 if (le16_to_cpu(cifs_acl->version) != CIFS_ACL_VERSION)
3248 if (acl_type & ACL_TYPE_ACCESS) {
3249 count = le16_to_cpu(cifs_acl->access_entry_count);
3250 pACE = &cifs_acl->ace_array[0];
3251 size = sizeof(struct cifs_posix_acl);
3252 size += sizeof(struct cifs_posix_ace) * count;
3253 /* check if we would go beyond end of SMB */
3254 if (size_of_data_area < size) {
3255 cFYI(1, "bad CIFS POSIX ACL size %d vs. %d",
3256 size_of_data_area, size);
3259 } else if (acl_type & ACL_TYPE_DEFAULT) {
3260 count = le16_to_cpu(cifs_acl->access_entry_count);
3261 size = sizeof(struct cifs_posix_acl);
3262 size += sizeof(struct cifs_posix_ace) * count;
3263 /* skip past access ACEs to get to default ACEs */
3264 pACE = &cifs_acl->ace_array[count];
3265 count = le16_to_cpu(cifs_acl->default_entry_count);
3266 size += sizeof(struct cifs_posix_ace) * count;
3267 /* check if we would go beyond end of SMB */
3268 if (size_of_data_area < size)
3275 size = posix_acl_xattr_size(count);
3276 if ((buflen == 0) || (local_acl == NULL)) {
3277 /* used to query ACL EA size */
3278 } else if (size > buflen) {
3280 } else /* buffer big enough */ {
3281 local_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
3282 for (i = 0; i < count ; i++) {
3283 cifs_convert_ace(&local_acl->a_entries[i], pACE);
3290 static __u16 convert_ace_to_cifs_ace(struct cifs_posix_ace *cifs_ace,
3291 const posix_acl_xattr_entry *local_ace)
3293 __u16 rc = 0; /* 0 = ACL converted ok */
3295 cifs_ace->cifs_e_perm = le16_to_cpu(local_ace->e_perm);
3296 cifs_ace->cifs_e_tag = le16_to_cpu(local_ace->e_tag);
3297 /* BB is there a better way to handle the large uid? */
3298 if (local_ace->e_id == cpu_to_le32(-1)) {
3299 /* Probably no need to le convert -1 on any arch but can not hurt */
3300 cifs_ace->cifs_uid = cpu_to_le64(-1);
3302 cifs_ace->cifs_uid = cpu_to_le64(le32_to_cpu(local_ace->e_id));
3303 /*cFYI(1, "perm %d tag %d id %d",ace->e_perm,ace->e_tag,ace->e_id);*/
3307 /* Convert ACL from local Linux POSIX xattr to CIFS POSIX ACL wire format */
3308 static __u16 ACL_to_cifs_posix(char *parm_data, const char *pACL,
3309 const int buflen, const int acl_type)
3312 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)parm_data;
3313 posix_acl_xattr_header *local_acl = (posix_acl_xattr_header *)pACL;
3317 if ((buflen == 0) || (pACL == NULL) || (cifs_acl == NULL))
3320 count = posix_acl_xattr_count((size_t)buflen);
3321 cFYI(1, "setting acl with %d entries from buf of length %d and "
3323 count, buflen, le32_to_cpu(local_acl->a_version));
3324 if (le32_to_cpu(local_acl->a_version) != 2) {
3325 cFYI(1, "unknown POSIX ACL version %d",
3326 le32_to_cpu(local_acl->a_version));
3329 cifs_acl->version = cpu_to_le16(1);
3330 if (acl_type == ACL_TYPE_ACCESS)
3331 cifs_acl->access_entry_count = cpu_to_le16(count);
3332 else if (acl_type == ACL_TYPE_DEFAULT)
3333 cifs_acl->default_entry_count = cpu_to_le16(count);
3335 cFYI(1, "unknown ACL type %d", acl_type);
3338 for (i = 0; i < count; i++) {
3339 rc = convert_ace_to_cifs_ace(&cifs_acl->ace_array[i],
3340 &local_acl->a_entries[i]);
3342 /* ACE not converted */
3347 rc = (__u16)(count * sizeof(struct cifs_posix_ace));
3348 rc += sizeof(struct cifs_posix_acl);
3349 /* BB add check to make sure ACL does not overflow SMB */
3355 CIFSSMBGetPosixACL(const int xid, struct cifs_tcon *tcon,
3356 const unsigned char *searchName,
3357 char *acl_inf, const int buflen, const int acl_type,
3358 const struct nls_table *nls_codepage, int remap)
3360 /* SMB_QUERY_POSIX_ACL */
3361 TRANSACTION2_QPI_REQ *pSMB = NULL;
3362 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3366 __u16 params, byte_count;
3368 cFYI(1, "In GetPosixACL (Unix) for path %s", searchName);
3371 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3376 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3378 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3379 searchName, PATH_MAX, nls_codepage,
3381 name_len++; /* trailing null */
3383 pSMB->FileName[name_len] = 0;
3384 pSMB->FileName[name_len+1] = 0;
3385 } else { /* BB improve the check for buffer overruns BB */
3386 name_len = strnlen(searchName, PATH_MAX);
3387 name_len++; /* trailing null */
3388 strncpy(pSMB->FileName, searchName, name_len);
3391 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3392 pSMB->TotalDataCount = 0;
3393 pSMB->MaxParameterCount = cpu_to_le16(2);
3394 /* BB find exact max data count below from sess structure BB */
3395 pSMB->MaxDataCount = cpu_to_le16(4000);
3396 pSMB->MaxSetupCount = 0;
3400 pSMB->Reserved2 = 0;
3401 pSMB->ParameterOffset = cpu_to_le16(
3402 offsetof(struct smb_com_transaction2_qpi_req,
3403 InformationLevel) - 4);
3404 pSMB->DataCount = 0;
3405 pSMB->DataOffset = 0;
3406 pSMB->SetupCount = 1;
3407 pSMB->Reserved3 = 0;
3408 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3409 byte_count = params + 1 /* pad */ ;
3410 pSMB->TotalParameterCount = cpu_to_le16(params);
3411 pSMB->ParameterCount = pSMB->TotalParameterCount;
3412 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_ACL);
3413 pSMB->Reserved4 = 0;
3414 inc_rfc1001_len(pSMB, byte_count);
3415 pSMB->ByteCount = cpu_to_le16(byte_count);
3417 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3418 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3419 cifs_stats_inc(&tcon->num_acl_get);
3421 cFYI(1, "Send error in Query POSIX ACL = %d", rc);
3423 /* decode response */
3425 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
3426 /* BB also check enough total bytes returned */
3427 if (rc || get_bcc(&pSMBr->hdr) < 2)
3428 rc = -EIO; /* bad smb */
3430 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3431 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3432 rc = cifs_copy_posix_acl(acl_inf,
3433 (char *)&pSMBr->hdr.Protocol+data_offset,
3434 buflen, acl_type, count);
3437 cifs_buf_release(pSMB);
3444 CIFSSMBSetPosixACL(const int xid, struct cifs_tcon *tcon,
3445 const unsigned char *fileName,
3446 const char *local_acl, const int buflen,
3448 const struct nls_table *nls_codepage, int remap)
3450 struct smb_com_transaction2_spi_req *pSMB = NULL;
3451 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
3455 int bytes_returned = 0;
3456 __u16 params, byte_count, data_count, param_offset, offset;
3458 cFYI(1, "In SetPosixACL (Unix) for path %s", fileName);
3460 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3464 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3466 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
3467 PATH_MAX, nls_codepage, remap);
3468 name_len++; /* trailing null */
3470 } else { /* BB improve the check for buffer overruns BB */
3471 name_len = strnlen(fileName, PATH_MAX);
3472 name_len++; /* trailing null */
3473 strncpy(pSMB->FileName, fileName, name_len);
3475 params = 6 + name_len;
3476 pSMB->MaxParameterCount = cpu_to_le16(2);
3477 /* BB find max SMB size from sess */
3478 pSMB->MaxDataCount = cpu_to_le16(1000);
3479 pSMB->MaxSetupCount = 0;
3483 pSMB->Reserved2 = 0;
3484 param_offset = offsetof(struct smb_com_transaction2_spi_req,
3485 InformationLevel) - 4;
3486 offset = param_offset + params;
3487 parm_data = ((char *) &pSMB->hdr.Protocol) + offset;
3488 pSMB->ParameterOffset = cpu_to_le16(param_offset);
3490 /* convert to on the wire format for POSIX ACL */
3491 data_count = ACL_to_cifs_posix(parm_data, local_acl, buflen, acl_type);
3493 if (data_count == 0) {
3495 goto setACLerrorExit;
3497 pSMB->DataOffset = cpu_to_le16(offset);
3498 pSMB->SetupCount = 1;
3499 pSMB->Reserved3 = 0;
3500 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
3501 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_ACL);
3502 byte_count = 3 /* pad */ + params + data_count;
3503 pSMB->DataCount = cpu_to_le16(data_count);
3504 pSMB->TotalDataCount = pSMB->DataCount;
3505 pSMB->ParameterCount = cpu_to_le16(params);
3506 pSMB->TotalParameterCount = pSMB->ParameterCount;
3507 pSMB->Reserved4 = 0;
3508 inc_rfc1001_len(pSMB, byte_count);
3509 pSMB->ByteCount = cpu_to_le16(byte_count);
3510 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3511 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3513 cFYI(1, "Set POSIX ACL returned %d", rc);
3516 cifs_buf_release(pSMB);
3522 /* BB fix tabs in this function FIXME BB */
3524 CIFSGetExtAttr(const int xid, struct cifs_tcon *tcon,
3525 const int netfid, __u64 *pExtAttrBits, __u64 *pMask)
3528 struct smb_t2_qfi_req *pSMB = NULL;
3529 struct smb_t2_qfi_rsp *pSMBr = NULL;
3531 __u16 params, byte_count;
3533 cFYI(1, "In GetExtAttr");
3538 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3543 params = 2 /* level */ + 2 /* fid */;
3544 pSMB->t2.TotalDataCount = 0;
3545 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
3546 /* BB find exact max data count below from sess structure BB */
3547 pSMB->t2.MaxDataCount = cpu_to_le16(4000);
3548 pSMB->t2.MaxSetupCount = 0;
3549 pSMB->t2.Reserved = 0;
3551 pSMB->t2.Timeout = 0;
3552 pSMB->t2.Reserved2 = 0;
3553 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
3555 pSMB->t2.DataCount = 0;
3556 pSMB->t2.DataOffset = 0;
3557 pSMB->t2.SetupCount = 1;
3558 pSMB->t2.Reserved3 = 0;
3559 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
3560 byte_count = params + 1 /* pad */ ;
3561 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
3562 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
3563 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_ATTR_FLAGS);
3566 inc_rfc1001_len(pSMB, byte_count);
3567 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
3569 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3570 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3572 cFYI(1, "error %d in GetExtAttr", rc);
3574 /* decode response */
3575 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
3576 /* BB also check enough total bytes returned */
3577 if (rc || get_bcc(&pSMBr->hdr) < 2)
3578 /* If rc should we check for EOPNOSUPP and
3579 disable the srvino flag? or in caller? */
3580 rc = -EIO; /* bad smb */
3582 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3583 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3584 struct file_chattr_info *pfinfo;
3585 /* BB Do we need a cast or hash here ? */
3587 cFYI(1, "Illegal size ret in GetExtAttr");
3591 pfinfo = (struct file_chattr_info *)
3592 (data_offset + (char *) &pSMBr->hdr.Protocol);
3593 *pExtAttrBits = le64_to_cpu(pfinfo->mode);
3594 *pMask = le64_to_cpu(pfinfo->mask);
3598 cifs_buf_release(pSMB);
3600 goto GetExtAttrRetry;
3604 #endif /* CONFIG_POSIX */
3606 #ifdef CONFIG_CIFS_ACL
3608 * Initialize NT TRANSACT SMB into small smb request buffer. This assumes that
3609 * all NT TRANSACTS that we init here have total parm and data under about 400
3610 * bytes (to fit in small cifs buffer size), which is the case so far, it
3611 * easily fits. NB: Setup words themselves and ByteCount MaxSetupCount (size of
3612 * returned setup area) and MaxParameterCount (returned parms size) must be set
3616 smb_init_nttransact(const __u16 sub_command, const int setup_count,
3617 const int parm_len, struct cifs_tcon *tcon,
3622 struct smb_com_ntransact_req *pSMB;
3624 rc = small_smb_init(SMB_COM_NT_TRANSACT, 19 + setup_count, tcon,
3628 *ret_buf = (void *)pSMB;
3630 pSMB->TotalParameterCount = cpu_to_le32(parm_len);
3631 pSMB->TotalDataCount = 0;
3632 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
3633 pSMB->ParameterCount = pSMB->TotalParameterCount;
3634 pSMB->DataCount = pSMB->TotalDataCount;
3635 temp_offset = offsetof(struct smb_com_ntransact_req, Parms) +
3636 (setup_count * 2) - 4 /* for rfc1001 length itself */;
3637 pSMB->ParameterOffset = cpu_to_le32(temp_offset);
3638 pSMB->DataOffset = cpu_to_le32(temp_offset + parm_len);
3639 pSMB->SetupCount = setup_count; /* no need to le convert byte fields */
3640 pSMB->SubCommand = cpu_to_le16(sub_command);
3645 validate_ntransact(char *buf, char **ppparm, char **ppdata,
3646 __u32 *pparmlen, __u32 *pdatalen)
3649 __u32 data_count, data_offset, parm_count, parm_offset;
3650 struct smb_com_ntransact_rsp *pSMBr;
3659 pSMBr = (struct smb_com_ntransact_rsp *)buf;
3661 bcc = get_bcc(&pSMBr->hdr);
3662 end_of_smb = 2 /* sizeof byte count */ + bcc +
3663 (char *)&pSMBr->ByteCount;
3665 data_offset = le32_to_cpu(pSMBr->DataOffset);
3666 data_count = le32_to_cpu(pSMBr->DataCount);
3667 parm_offset = le32_to_cpu(pSMBr->ParameterOffset);
3668 parm_count = le32_to_cpu(pSMBr->ParameterCount);
3670 *ppparm = (char *)&pSMBr->hdr.Protocol + parm_offset;
3671 *ppdata = (char *)&pSMBr->hdr.Protocol + data_offset;
3673 /* should we also check that parm and data areas do not overlap? */
3674 if (*ppparm > end_of_smb) {
3675 cFYI(1, "parms start after end of smb");
3677 } else if (parm_count + *ppparm > end_of_smb) {
3678 cFYI(1, "parm end after end of smb");
3680 } else if (*ppdata > end_of_smb) {
3681 cFYI(1, "data starts after end of smb");
3683 } else if (data_count + *ppdata > end_of_smb) {
3684 cFYI(1, "data %p + count %d (%p) past smb end %p start %p",
3685 *ppdata, data_count, (data_count + *ppdata),
3688 } else if (parm_count + data_count > bcc) {
3689 cFYI(1, "parm count and data count larger than SMB");
3692 *pdatalen = data_count;
3693 *pparmlen = parm_count;
3697 /* Get Security Descriptor (by handle) from remote server for a file or dir */
3699 CIFSSMBGetCIFSACL(const int xid, struct cifs_tcon *tcon, __u16 fid,
3700 struct cifs_ntsd **acl_inf, __u32 *pbuflen)
3704 QUERY_SEC_DESC_REQ *pSMB;
3707 cFYI(1, "GetCifsACL");
3712 rc = smb_init_nttransact(NT_TRANSACT_QUERY_SECURITY_DESC, 0,
3713 8 /* parm len */, tcon, (void **) &pSMB);
3717 pSMB->MaxParameterCount = cpu_to_le32(4);
3718 /* BB TEST with big acls that might need to be e.g. larger than 16K */
3719 pSMB->MaxSetupCount = 0;
3720 pSMB->Fid = fid; /* file handle always le */
3721 pSMB->AclFlags = cpu_to_le32(CIFS_ACL_OWNER | CIFS_ACL_GROUP |
3723 pSMB->ByteCount = cpu_to_le16(11); /* 3 bytes pad + 8 bytes parm */
3724 inc_rfc1001_len(pSMB, 11);
3725 iov[0].iov_base = (char *)pSMB;
3726 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
3728 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovec */, &buf_type,
3730 cifs_stats_inc(&tcon->num_acl_get);
3732 cFYI(1, "Send error in QuerySecDesc = %d", rc);
3733 } else { /* decode response */
3737 struct smb_com_ntransact_rsp *pSMBr;
3740 /* validate_nttransact */
3741 rc = validate_ntransact(iov[0].iov_base, (char **)&parm,
3742 &pdata, &parm_len, pbuflen);
3745 pSMBr = (struct smb_com_ntransact_rsp *)iov[0].iov_base;
3747 cFYI(1, "smb %p parm %p data %p", pSMBr, parm, *acl_inf);
3749 if (le32_to_cpu(pSMBr->ParameterCount) != 4) {
3750 rc = -EIO; /* bad smb */
3755 /* BB check that data area is minimum length and as big as acl_len */
3757 acl_len = le32_to_cpu(*parm);
3758 if (acl_len != *pbuflen) {
3759 cERROR(1, "acl length %d does not match %d",
3761 if (*pbuflen > acl_len)
3765 /* check if buffer is big enough for the acl
3766 header followed by the smallest SID */
3767 if ((*pbuflen < sizeof(struct cifs_ntsd) + 8) ||
3768 (*pbuflen >= 64 * 1024)) {
3769 cERROR(1, "bad acl length %d", *pbuflen);
3773 *acl_inf = kmalloc(*pbuflen, GFP_KERNEL);
3774 if (*acl_inf == NULL) {
3778 memcpy(*acl_inf, pdata, *pbuflen);
3782 if (buf_type == CIFS_SMALL_BUFFER)
3783 cifs_small_buf_release(iov[0].iov_base);
3784 else if (buf_type == CIFS_LARGE_BUFFER)
3785 cifs_buf_release(iov[0].iov_base);
3786 /* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
3791 CIFSSMBSetCIFSACL(const int xid, struct cifs_tcon *tcon, __u16 fid,
3792 struct cifs_ntsd *pntsd, __u32 acllen, int aclflag)
3794 __u16 byte_count, param_count, data_count, param_offset, data_offset;
3796 int bytes_returned = 0;
3797 SET_SEC_DESC_REQ *pSMB = NULL;
3801 rc = smb_init(SMB_COM_NT_TRANSACT, 19, tcon, (void **) &pSMB, &pSMBr);
3805 pSMB->MaxSetupCount = 0;
3809 param_offset = offsetof(struct smb_com_transaction_ssec_req, Fid) - 4;
3810 data_count = acllen;
3811 data_offset = param_offset + param_count;
3812 byte_count = 3 /* pad */ + param_count;
3814 pSMB->DataCount = cpu_to_le32(data_count);
3815 pSMB->TotalDataCount = pSMB->DataCount;
3816 pSMB->MaxParameterCount = cpu_to_le32(4);
3817 pSMB->MaxDataCount = cpu_to_le32(16384);
3818 pSMB->ParameterCount = cpu_to_le32(param_count);
3819 pSMB->ParameterOffset = cpu_to_le32(param_offset);
3820 pSMB->TotalParameterCount = pSMB->ParameterCount;
3821 pSMB->DataOffset = cpu_to_le32(data_offset);
3822 pSMB->SetupCount = 0;
3823 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_SET_SECURITY_DESC);
3824 pSMB->ByteCount = cpu_to_le16(byte_count+data_count);
3826 pSMB->Fid = fid; /* file handle always le */
3827 pSMB->Reserved2 = 0;
3828 pSMB->AclFlags = cpu_to_le32(aclflag);
3830 if (pntsd && acllen) {
3831 memcpy((char *)pSMBr + offsetof(struct smb_hdr, Protocol) +
3832 data_offset, pntsd, acllen);
3833 inc_rfc1001_len(pSMB, byte_count + data_count);
3835 inc_rfc1001_len(pSMB, byte_count);
3837 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3838 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3840 cFYI(1, "SetCIFSACL bytes_returned: %d, rc: %d", bytes_returned, rc);
3842 cFYI(1, "Set CIFS ACL returned %d", rc);
3843 cifs_buf_release(pSMB);
3846 goto setCifsAclRetry;
3851 #endif /* CONFIG_CIFS_ACL */
3853 /* Legacy Query Path Information call for lookup to old servers such
3855 int SMBQueryInformation(const int xid, struct cifs_tcon *tcon,
3856 const unsigned char *searchName,
3857 FILE_ALL_INFO *pFinfo,
3858 const struct nls_table *nls_codepage, int remap)
3860 QUERY_INFORMATION_REQ *pSMB;
3861 QUERY_INFORMATION_RSP *pSMBr;
3866 cFYI(1, "In SMBQPath path %s", searchName);
3868 rc = smb_init(SMB_COM_QUERY_INFORMATION, 0, tcon, (void **) &pSMB,
3873 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3875 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3876 searchName, PATH_MAX, nls_codepage,
3878 name_len++; /* trailing null */
3881 name_len = strnlen(searchName, PATH_MAX);
3882 name_len++; /* trailing null */
3883 strncpy(pSMB->FileName, searchName, name_len);
3885 pSMB->BufferFormat = 0x04;
3886 name_len++; /* account for buffer type byte */
3887 inc_rfc1001_len(pSMB, (__u16)name_len);
3888 pSMB->ByteCount = cpu_to_le16(name_len);
3890 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3891 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3893 cFYI(1, "Send error in QueryInfo = %d", rc);
3894 } else if (pFinfo) {
3896 __u32 time = le32_to_cpu(pSMBr->last_write_time);
3898 /* decode response */
3899 /* BB FIXME - add time zone adjustment BB */
3900 memset(pFinfo, 0, sizeof(FILE_ALL_INFO));
3903 /* decode time fields */
3904 pFinfo->ChangeTime = cpu_to_le64(cifs_UnixTimeToNT(ts));
3905 pFinfo->LastWriteTime = pFinfo->ChangeTime;
3906 pFinfo->LastAccessTime = 0;
3907 pFinfo->AllocationSize =
3908 cpu_to_le64(le32_to_cpu(pSMBr->size));
3909 pFinfo->EndOfFile = pFinfo->AllocationSize;
3910 pFinfo->Attributes =
3911 cpu_to_le32(le16_to_cpu(pSMBr->attr));
3913 rc = -EIO; /* bad buffer passed in */
3915 cifs_buf_release(pSMB);
3924 CIFSSMBQFileInfo(const int xid, struct cifs_tcon *tcon,
3925 u16 netfid, FILE_ALL_INFO *pFindData)
3927 struct smb_t2_qfi_req *pSMB = NULL;
3928 struct smb_t2_qfi_rsp *pSMBr = NULL;
3931 __u16 params, byte_count;
3934 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3939 params = 2 /* level */ + 2 /* fid */;
3940 pSMB->t2.TotalDataCount = 0;
3941 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
3942 /* BB find exact max data count below from sess structure BB */
3943 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
3944 pSMB->t2.MaxSetupCount = 0;
3945 pSMB->t2.Reserved = 0;
3947 pSMB->t2.Timeout = 0;
3948 pSMB->t2.Reserved2 = 0;
3949 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
3951 pSMB->t2.DataCount = 0;
3952 pSMB->t2.DataOffset = 0;
3953 pSMB->t2.SetupCount = 1;
3954 pSMB->t2.Reserved3 = 0;
3955 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
3956 byte_count = params + 1 /* pad */ ;
3957 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
3958 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
3959 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
3962 inc_rfc1001_len(pSMB, byte_count);
3964 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3965 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3967 cFYI(1, "Send error in QPathInfo = %d", rc);
3968 } else { /* decode response */
3969 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
3971 if (rc) /* BB add auto retry on EOPNOTSUPP? */
3973 else if (get_bcc(&pSMBr->hdr) < 40)
3974 rc = -EIO; /* bad smb */
3975 else if (pFindData) {
3976 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3977 memcpy((char *) pFindData,
3978 (char *) &pSMBr->hdr.Protocol +
3979 data_offset, sizeof(FILE_ALL_INFO));
3983 cifs_buf_release(pSMB);
3985 goto QFileInfoRetry;
3991 CIFSSMBQPathInfo(const int xid, struct cifs_tcon *tcon,
3992 const unsigned char *searchName,
3993 FILE_ALL_INFO *pFindData,
3994 int legacy /* old style infolevel */,
3995 const struct nls_table *nls_codepage, int remap)
3997 /* level 263 SMB_QUERY_FILE_ALL_INFO */
3998 TRANSACTION2_QPI_REQ *pSMB = NULL;
3999 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4003 __u16 params, byte_count;
4005 /* cFYI(1, "In QPathInfo path %s", searchName); */
4007 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4012 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4014 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4015 PATH_MAX, nls_codepage, remap);
4016 name_len++; /* trailing null */
4018 } else { /* BB improve the check for buffer overruns BB */
4019 name_len = strnlen(searchName, PATH_MAX);
4020 name_len++; /* trailing null */
4021 strncpy(pSMB->FileName, searchName, name_len);
4024 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
4025 pSMB->TotalDataCount = 0;
4026 pSMB->MaxParameterCount = cpu_to_le16(2);
4027 /* BB find exact max SMB PDU from sess structure BB */
4028 pSMB->MaxDataCount = cpu_to_le16(4000);
4029 pSMB->MaxSetupCount = 0;
4033 pSMB->Reserved2 = 0;
4034 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4035 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
4036 pSMB->DataCount = 0;
4037 pSMB->DataOffset = 0;
4038 pSMB->SetupCount = 1;
4039 pSMB->Reserved3 = 0;
4040 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4041 byte_count = params + 1 /* pad */ ;
4042 pSMB->TotalParameterCount = cpu_to_le16(params);
4043 pSMB->ParameterCount = pSMB->TotalParameterCount;
4045 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_STANDARD);
4047 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
4048 pSMB->Reserved4 = 0;
4049 inc_rfc1001_len(pSMB, byte_count);
4050 pSMB->ByteCount = cpu_to_le16(byte_count);
4052 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4053 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4055 cFYI(1, "Send error in QPathInfo = %d", rc);
4056 } else { /* decode response */
4057 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4059 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4061 else if (!legacy && get_bcc(&pSMBr->hdr) < 40)
4062 rc = -EIO; /* bad smb */
4063 else if (legacy && get_bcc(&pSMBr->hdr) < 24)
4064 rc = -EIO; /* 24 or 26 expected but we do not read
4066 else if (pFindData) {
4068 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4070 /* On legacy responses we do not read the last field,
4071 EAsize, fortunately since it varies by subdialect and
4072 also note it differs on Set vs. Get, ie two bytes or 4
4073 bytes depending but we don't care here */
4075 size = sizeof(FILE_INFO_STANDARD);
4077 size = sizeof(FILE_ALL_INFO);
4078 memcpy((char *) pFindData,
4079 (char *) &pSMBr->hdr.Protocol +
4084 cifs_buf_release(pSMB);
4086 goto QPathInfoRetry;
4092 CIFSSMBUnixQFileInfo(const int xid, struct cifs_tcon *tcon,
4093 u16 netfid, FILE_UNIX_BASIC_INFO *pFindData)
4095 struct smb_t2_qfi_req *pSMB = NULL;
4096 struct smb_t2_qfi_rsp *pSMBr = NULL;
4099 __u16 params, byte_count;
4102 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4107 params = 2 /* level */ + 2 /* fid */;
4108 pSMB->t2.TotalDataCount = 0;
4109 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4110 /* BB find exact max data count below from sess structure BB */
4111 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4112 pSMB->t2.MaxSetupCount = 0;
4113 pSMB->t2.Reserved = 0;
4115 pSMB->t2.Timeout = 0;
4116 pSMB->t2.Reserved2 = 0;
4117 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4119 pSMB->t2.DataCount = 0;
4120 pSMB->t2.DataOffset = 0;
4121 pSMB->t2.SetupCount = 1;
4122 pSMB->t2.Reserved3 = 0;
4123 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4124 byte_count = params + 1 /* pad */ ;
4125 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4126 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4127 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4130 inc_rfc1001_len(pSMB, byte_count);
4132 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4133 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4135 cFYI(1, "Send error in QPathInfo = %d", rc);
4136 } else { /* decode response */
4137 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4139 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
4140 cERROR(1, "Malformed FILE_UNIX_BASIC_INFO response.\n"
4141 "Unix Extensions can be disabled on mount "
4142 "by specifying the nosfu mount option.");
4143 rc = -EIO; /* bad smb */
4145 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4146 memcpy((char *) pFindData,
4147 (char *) &pSMBr->hdr.Protocol +
4149 sizeof(FILE_UNIX_BASIC_INFO));
4153 cifs_buf_release(pSMB);
4155 goto UnixQFileInfoRetry;
4161 CIFSSMBUnixQPathInfo(const int xid, struct cifs_tcon *tcon,
4162 const unsigned char *searchName,
4163 FILE_UNIX_BASIC_INFO *pFindData,
4164 const struct nls_table *nls_codepage, int remap)
4166 /* SMB_QUERY_FILE_UNIX_BASIC */
4167 TRANSACTION2_QPI_REQ *pSMB = NULL;
4168 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4170 int bytes_returned = 0;
4172 __u16 params, byte_count;
4174 cFYI(1, "In QPathInfo (Unix) the path %s", searchName);
4176 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4181 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4183 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4184 PATH_MAX, nls_codepage, remap);
4185 name_len++; /* trailing null */
4187 } else { /* BB improve the check for buffer overruns BB */
4188 name_len = strnlen(searchName, PATH_MAX);
4189 name_len++; /* trailing null */
4190 strncpy(pSMB->FileName, searchName, name_len);
4193 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
4194 pSMB->TotalDataCount = 0;
4195 pSMB->MaxParameterCount = cpu_to_le16(2);
4196 /* BB find exact max SMB PDU from sess structure BB */
4197 pSMB->MaxDataCount = cpu_to_le16(4000);
4198 pSMB->MaxSetupCount = 0;
4202 pSMB->Reserved2 = 0;
4203 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4204 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
4205 pSMB->DataCount = 0;
4206 pSMB->DataOffset = 0;
4207 pSMB->SetupCount = 1;
4208 pSMB->Reserved3 = 0;
4209 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4210 byte_count = params + 1 /* pad */ ;
4211 pSMB->TotalParameterCount = cpu_to_le16(params);
4212 pSMB->ParameterCount = pSMB->TotalParameterCount;
4213 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4214 pSMB->Reserved4 = 0;
4215 inc_rfc1001_len(pSMB, byte_count);
4216 pSMB->ByteCount = cpu_to_le16(byte_count);
4218 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4219 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4221 cFYI(1, "Send error in QPathInfo = %d", rc);
4222 } else { /* decode response */
4223 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4225 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
4226 cERROR(1, "Malformed FILE_UNIX_BASIC_INFO response.\n"
4227 "Unix Extensions can be disabled on mount "
4228 "by specifying the nosfu mount option.");
4229 rc = -EIO; /* bad smb */
4231 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4232 memcpy((char *) pFindData,
4233 (char *) &pSMBr->hdr.Protocol +
4235 sizeof(FILE_UNIX_BASIC_INFO));
4238 cifs_buf_release(pSMB);
4240 goto UnixQPathInfoRetry;
4245 /* xid, tcon, searchName and codepage are input parms, rest are returned */
4247 CIFSFindFirst(const int xid, struct cifs_tcon *tcon,
4248 const char *searchName,
4249 const struct nls_table *nls_codepage,
4250 __u16 *pnetfid, __u16 search_flags,
4251 struct cifs_search_info *psrch_inf, int remap, const char dirsep)
4253 /* level 257 SMB_ */
4254 TRANSACTION2_FFIRST_REQ *pSMB = NULL;
4255 TRANSACTION2_FFIRST_RSP *pSMBr = NULL;
4256 T2_FFIRST_RSP_PARMS *parms;
4258 int bytes_returned = 0;
4260 __u16 params, byte_count;
4262 cFYI(1, "In FindFirst for %s", searchName);
4265 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4270 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4272 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4273 PATH_MAX, nls_codepage, remap);
4274 /* We can not add the asterik earlier in case
4275 it got remapped to 0xF03A as if it were part of the
4276 directory name instead of a wildcard */
4278 pSMB->FileName[name_len] = dirsep;
4279 pSMB->FileName[name_len+1] = 0;
4280 pSMB->FileName[name_len+2] = '*';
4281 pSMB->FileName[name_len+3] = 0;
4282 name_len += 4; /* now the trailing null */
4283 pSMB->FileName[name_len] = 0; /* null terminate just in case */
4284 pSMB->FileName[name_len+1] = 0;
4286 } else { /* BB add check for overrun of SMB buf BB */
4287 name_len = strnlen(searchName, PATH_MAX);
4288 /* BB fix here and in unicode clause above ie
4289 if (name_len > buffersize-header)
4290 free buffer exit; BB */
4291 strncpy(pSMB->FileName, searchName, name_len);
4292 pSMB->FileName[name_len] = dirsep;
4293 pSMB->FileName[name_len+1] = '*';
4294 pSMB->FileName[name_len+2] = 0;
4298 params = 12 + name_len /* includes null */ ;
4299 pSMB->TotalDataCount = 0; /* no EAs */
4300 pSMB->MaxParameterCount = cpu_to_le16(10);
4301 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
4302 pSMB->MaxSetupCount = 0;
4306 pSMB->Reserved2 = 0;
4307 byte_count = params + 1 /* pad */ ;
4308 pSMB->TotalParameterCount = cpu_to_le16(params);
4309 pSMB->ParameterCount = pSMB->TotalParameterCount;
4310 pSMB->ParameterOffset = cpu_to_le16(
4311 offsetof(struct smb_com_transaction2_ffirst_req, SearchAttributes)
4313 pSMB->DataCount = 0;
4314 pSMB->DataOffset = 0;
4315 pSMB->SetupCount = 1; /* one byte, no need to make endian neutral */
4316 pSMB->Reserved3 = 0;
4317 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_FIRST);
4318 pSMB->SearchAttributes =
4319 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
4321 pSMB->SearchCount = cpu_to_le16(CIFSMaxBufSize/sizeof(FILE_UNIX_INFO));
4322 pSMB->SearchFlags = cpu_to_le16(search_flags);
4323 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4325 /* BB what should we set StorageType to? Does it matter? BB */
4326 pSMB->SearchStorageType = 0;
4327 inc_rfc1001_len(pSMB, byte_count);
4328 pSMB->ByteCount = cpu_to_le16(byte_count);
4330 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4331 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4332 cifs_stats_inc(&tcon->num_ffirst);
4334 if (rc) {/* BB add logic to retry regular search if Unix search
4335 rejected unexpectedly by server */
4336 /* BB Add code to handle unsupported level rc */
4337 cFYI(1, "Error in FindFirst = %d", rc);
4339 cifs_buf_release(pSMB);
4341 /* BB eventually could optimize out free and realloc of buf */
4344 goto findFirstRetry;
4345 } else { /* decode response */
4346 /* BB remember to free buffer if error BB */
4347 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4351 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
4352 psrch_inf->unicode = true;
4354 psrch_inf->unicode = false;
4356 psrch_inf->ntwrk_buf_start = (char *)pSMBr;
4357 psrch_inf->smallBuf = 0;
4358 psrch_inf->srch_entries_start =
4359 (char *) &pSMBr->hdr.Protocol +
4360 le16_to_cpu(pSMBr->t2.DataOffset);
4361 parms = (T2_FFIRST_RSP_PARMS *)((char *) &pSMBr->hdr.Protocol +
4362 le16_to_cpu(pSMBr->t2.ParameterOffset));
4364 if (parms->EndofSearch)
4365 psrch_inf->endOfSearch = true;
4367 psrch_inf->endOfSearch = false;
4369 psrch_inf->entries_in_buffer =
4370 le16_to_cpu(parms->SearchCount);
4371 psrch_inf->index_of_last_entry = 2 /* skip . and .. */ +
4372 psrch_inf->entries_in_buffer;
4373 lnoff = le16_to_cpu(parms->LastNameOffset);
4374 if (CIFSMaxBufSize < lnoff) {
4375 cERROR(1, "ignoring corrupt resume name");
4376 psrch_inf->last_entry = NULL;
4380 psrch_inf->last_entry = psrch_inf->srch_entries_start +
4383 *pnetfid = parms->SearchHandle;
4385 cifs_buf_release(pSMB);
4392 int CIFSFindNext(const int xid, struct cifs_tcon *tcon, __u16 searchHandle,
4393 __u16 search_flags, struct cifs_search_info *psrch_inf)
4395 TRANSACTION2_FNEXT_REQ *pSMB = NULL;
4396 TRANSACTION2_FNEXT_RSP *pSMBr = NULL;
4397 T2_FNEXT_RSP_PARMS *parms;
4398 char *response_data;
4401 unsigned int name_len;
4402 __u16 params, byte_count;
4404 cFYI(1, "In FindNext");
4406 if (psrch_inf->endOfSearch)
4409 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4414 params = 14; /* includes 2 bytes of null string, converted to LE below*/
4416 pSMB->TotalDataCount = 0; /* no EAs */
4417 pSMB->MaxParameterCount = cpu_to_le16(8);
4418 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
4419 pSMB->MaxSetupCount = 0;
4423 pSMB->Reserved2 = 0;
4424 pSMB->ParameterOffset = cpu_to_le16(
4425 offsetof(struct smb_com_transaction2_fnext_req,SearchHandle) - 4);
4426 pSMB->DataCount = 0;
4427 pSMB->DataOffset = 0;
4428 pSMB->SetupCount = 1;
4429 pSMB->Reserved3 = 0;
4430 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_NEXT);
4431 pSMB->SearchHandle = searchHandle; /* always kept as le */
4433 cpu_to_le16(CIFSMaxBufSize / sizeof(FILE_UNIX_INFO));
4434 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4435 pSMB->ResumeKey = psrch_inf->resume_key;
4436 pSMB->SearchFlags = cpu_to_le16(search_flags);
4438 name_len = psrch_inf->resume_name_len;
4440 if (name_len < PATH_MAX) {
4441 memcpy(pSMB->ResumeFileName, psrch_inf->presume_name, name_len);
4442 byte_count += name_len;
4443 /* 14 byte parm len above enough for 2 byte null terminator */
4444 pSMB->ResumeFileName[name_len] = 0;
4445 pSMB->ResumeFileName[name_len+1] = 0;
4448 goto FNext2_err_exit;
4450 byte_count = params + 1 /* pad */ ;
4451 pSMB->TotalParameterCount = cpu_to_le16(params);
4452 pSMB->ParameterCount = pSMB->TotalParameterCount;
4453 inc_rfc1001_len(pSMB, byte_count);
4454 pSMB->ByteCount = cpu_to_le16(byte_count);
4456 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4457 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4458 cifs_stats_inc(&tcon->num_fnext);
4461 psrch_inf->endOfSearch = true;
4462 cifs_buf_release(pSMB);
4463 rc = 0; /* search probably was closed at end of search*/
4465 cFYI(1, "FindNext returned = %d", rc);
4466 } else { /* decode response */
4467 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4472 /* BB fixme add lock for file (srch_info) struct here */
4473 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
4474 psrch_inf->unicode = true;
4476 psrch_inf->unicode = false;
4477 response_data = (char *) &pSMBr->hdr.Protocol +
4478 le16_to_cpu(pSMBr->t2.ParameterOffset);
4479 parms = (T2_FNEXT_RSP_PARMS *)response_data;
4480 response_data = (char *)&pSMBr->hdr.Protocol +
4481 le16_to_cpu(pSMBr->t2.DataOffset);
4482 if (psrch_inf->smallBuf)
4483 cifs_small_buf_release(
4484 psrch_inf->ntwrk_buf_start);
4486 cifs_buf_release(psrch_inf->ntwrk_buf_start);
4487 psrch_inf->srch_entries_start = response_data;
4488 psrch_inf->ntwrk_buf_start = (char *)pSMB;
4489 psrch_inf->smallBuf = 0;
4490 if (parms->EndofSearch)
4491 psrch_inf->endOfSearch = true;
4493 psrch_inf->endOfSearch = false;
4494 psrch_inf->entries_in_buffer =
4495 le16_to_cpu(parms->SearchCount);
4496 psrch_inf->index_of_last_entry +=
4497 psrch_inf->entries_in_buffer;
4498 lnoff = le16_to_cpu(parms->LastNameOffset);
4499 if (CIFSMaxBufSize < lnoff) {
4500 cERROR(1, "ignoring corrupt resume name");
4501 psrch_inf->last_entry = NULL;
4504 psrch_inf->last_entry =
4505 psrch_inf->srch_entries_start + lnoff;
4507 /* cFYI(1, "fnxt2 entries in buf %d index_of_last %d",
4508 psrch_inf->entries_in_buffer, psrch_inf->index_of_last_entry); */
4510 /* BB fixme add unlock here */
4515 /* BB On error, should we leave previous search buf (and count and
4516 last entry fields) intact or free the previous one? */
4518 /* Note: On -EAGAIN error only caller can retry on handle based calls
4519 since file handle passed in no longer valid */
4522 cifs_buf_release(pSMB);
4527 CIFSFindClose(const int xid, struct cifs_tcon *tcon,
4528 const __u16 searchHandle)
4531 FINDCLOSE_REQ *pSMB = NULL;
4533 cFYI(1, "In CIFSSMBFindClose");
4534 rc = small_smb_init(SMB_COM_FIND_CLOSE2, 1, tcon, (void **)&pSMB);
4536 /* no sense returning error if session restarted
4537 as file handle has been closed */
4543 pSMB->FileID = searchHandle;
4544 pSMB->ByteCount = 0;
4545 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
4547 cERROR(1, "Send error in FindClose = %d", rc);
4549 cifs_stats_inc(&tcon->num_fclose);
4551 /* Since session is dead, search handle closed on server already */
4559 CIFSGetSrvInodeNumber(const int xid, struct cifs_tcon *tcon,
4560 const unsigned char *searchName,
4561 __u64 *inode_number,
4562 const struct nls_table *nls_codepage, int remap)
4565 TRANSACTION2_QPI_REQ *pSMB = NULL;
4566 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4567 int name_len, bytes_returned;
4568 __u16 params, byte_count;
4570 cFYI(1, "In GetSrvInodeNum for %s", searchName);
4574 GetInodeNumberRetry:
4575 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4580 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4582 cifsConvertToUTF16((__le16 *) pSMB->FileName,
4583 searchName, PATH_MAX, nls_codepage,
4585 name_len++; /* trailing null */
4587 } else { /* BB improve the check for buffer overruns BB */
4588 name_len = strnlen(searchName, PATH_MAX);
4589 name_len++; /* trailing null */
4590 strncpy(pSMB->FileName, searchName, name_len);
4593 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
4594 pSMB->TotalDataCount = 0;
4595 pSMB->MaxParameterCount = cpu_to_le16(2);
4596 /* BB find exact max data count below from sess structure BB */
4597 pSMB->MaxDataCount = cpu_to_le16(4000);
4598 pSMB->MaxSetupCount = 0;
4602 pSMB->Reserved2 = 0;
4603 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4604 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
4605 pSMB->DataCount = 0;
4606 pSMB->DataOffset = 0;
4607 pSMB->SetupCount = 1;
4608 pSMB->Reserved3 = 0;
4609 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4610 byte_count = params + 1 /* pad */ ;
4611 pSMB->TotalParameterCount = cpu_to_le16(params);
4612 pSMB->ParameterCount = pSMB->TotalParameterCount;
4613 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_INTERNAL_INFO);
4614 pSMB->Reserved4 = 0;
4615 inc_rfc1001_len(pSMB, byte_count);
4616 pSMB->ByteCount = cpu_to_le16(byte_count);
4618 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4619 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4621 cFYI(1, "error %d in QueryInternalInfo", rc);
4623 /* decode response */
4624 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4625 /* BB also check enough total bytes returned */
4626 if (rc || get_bcc(&pSMBr->hdr) < 2)
4627 /* If rc should we check for EOPNOSUPP and
4628 disable the srvino flag? or in caller? */
4629 rc = -EIO; /* bad smb */
4631 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4632 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
4633 struct file_internal_info *pfinfo;
4634 /* BB Do we need a cast or hash here ? */
4636 cFYI(1, "Illegal size ret in QryIntrnlInf");
4638 goto GetInodeNumOut;
4640 pfinfo = (struct file_internal_info *)
4641 (data_offset + (char *) &pSMBr->hdr.Protocol);
4642 *inode_number = le64_to_cpu(pfinfo->UniqueId);
4646 cifs_buf_release(pSMB);
4648 goto GetInodeNumberRetry;
4652 /* parses DFS refferal V3 structure
4653 * caller is responsible for freeing target_nodes
4656 * on failure - errno
4659 parse_DFS_referrals(TRANSACTION2_GET_DFS_REFER_RSP *pSMBr,
4660 unsigned int *num_of_nodes,
4661 struct dfs_info3_param **target_nodes,
4662 const struct nls_table *nls_codepage, int remap,
4663 const char *searchName)
4668 struct dfs_referral_level_3 *ref;
4670 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
4674 *num_of_nodes = le16_to_cpu(pSMBr->NumberOfReferrals);
4676 if (*num_of_nodes < 1) {
4677 cERROR(1, "num_referrals: must be at least > 0,"
4678 "but we get num_referrals = %d\n", *num_of_nodes);
4680 goto parse_DFS_referrals_exit;
4683 ref = (struct dfs_referral_level_3 *) &(pSMBr->referrals);
4684 if (ref->VersionNumber != cpu_to_le16(3)) {
4685 cERROR(1, "Referrals of V%d version are not supported,"
4686 "should be V3", le16_to_cpu(ref->VersionNumber));
4688 goto parse_DFS_referrals_exit;
4691 /* get the upper boundary of the resp buffer */
4692 data_end = (char *)(&(pSMBr->PathConsumed)) +
4693 le16_to_cpu(pSMBr->t2.DataCount);
4695 cFYI(1, "num_referrals: %d dfs flags: 0x%x ...\n",
4697 le32_to_cpu(pSMBr->DFSFlags));
4699 *target_nodes = kzalloc(sizeof(struct dfs_info3_param) *
4700 *num_of_nodes, GFP_KERNEL);
4701 if (*target_nodes == NULL) {
4702 cERROR(1, "Failed to allocate buffer for target_nodes\n");
4704 goto parse_DFS_referrals_exit;
4707 /* collect necessary data from referrals */
4708 for (i = 0; i < *num_of_nodes; i++) {
4711 struct dfs_info3_param *node = (*target_nodes)+i;
4713 node->flags = le32_to_cpu(pSMBr->DFSFlags);
4715 __le16 *tmp = kmalloc(strlen(searchName)*2 + 2,
4719 goto parse_DFS_referrals_exit;
4721 cifsConvertToUTF16((__le16 *) tmp, searchName,
4722 PATH_MAX, nls_codepage, remap);
4723 node->path_consumed = cifs_utf16_bytes(tmp,
4724 le16_to_cpu(pSMBr->PathConsumed),
4728 node->path_consumed = le16_to_cpu(pSMBr->PathConsumed);
4730 node->server_type = le16_to_cpu(ref->ServerType);
4731 node->ref_flag = le16_to_cpu(ref->ReferralEntryFlags);
4734 temp = (char *)ref + le16_to_cpu(ref->DfsPathOffset);
4735 max_len = data_end - temp;
4736 node->path_name = cifs_strndup_from_utf16(temp, max_len,
4737 is_unicode, nls_codepage);
4738 if (!node->path_name) {
4740 goto parse_DFS_referrals_exit;
4743 /* copy link target UNC */
4744 temp = (char *)ref + le16_to_cpu(ref->NetworkAddressOffset);
4745 max_len = data_end - temp;
4746 node->node_name = cifs_strndup_from_utf16(temp, max_len,
4747 is_unicode, nls_codepage);
4748 if (!node->node_name) {
4750 goto parse_DFS_referrals_exit;
4756 parse_DFS_referrals_exit:
4758 free_dfs_info_array(*target_nodes, *num_of_nodes);
4759 *target_nodes = NULL;
4766 CIFSGetDFSRefer(const int xid, struct cifs_ses *ses,
4767 const unsigned char *searchName,
4768 struct dfs_info3_param **target_nodes,
4769 unsigned int *num_of_nodes,
4770 const struct nls_table *nls_codepage, int remap)
4772 /* TRANS2_GET_DFS_REFERRAL */
4773 TRANSACTION2_GET_DFS_REFER_REQ *pSMB = NULL;
4774 TRANSACTION2_GET_DFS_REFER_RSP *pSMBr = NULL;
4778 __u16 params, byte_count;
4780 *target_nodes = NULL;
4782 cFYI(1, "In GetDFSRefer the path %s", searchName);
4786 rc = smb_init(SMB_COM_TRANSACTION2, 15, NULL, (void **) &pSMB,
4791 /* server pointer checked in called function,
4792 but should never be null here anyway */
4793 pSMB->hdr.Mid = get_next_mid(ses->server);
4794 pSMB->hdr.Tid = ses->ipc_tid;
4795 pSMB->hdr.Uid = ses->Suid;
4796 if (ses->capabilities & CAP_STATUS32)
4797 pSMB->hdr.Flags2 |= SMBFLG2_ERR_STATUS;
4798 if (ses->capabilities & CAP_DFS)
4799 pSMB->hdr.Flags2 |= SMBFLG2_DFS;
4801 if (ses->capabilities & CAP_UNICODE) {
4802 pSMB->hdr.Flags2 |= SMBFLG2_UNICODE;
4804 cifsConvertToUTF16((__le16 *) pSMB->RequestFileName,
4805 searchName, PATH_MAX, nls_codepage,
4807 name_len++; /* trailing null */
4809 } else { /* BB improve the check for buffer overruns BB */
4810 name_len = strnlen(searchName, PATH_MAX);
4811 name_len++; /* trailing null */
4812 strncpy(pSMB->RequestFileName, searchName, name_len);
4816 if (ses->server->sec_mode &
4817 (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
4818 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
4821 pSMB->hdr.Uid = ses->Suid;
4823 params = 2 /* level */ + name_len /*includes null */ ;
4824 pSMB->TotalDataCount = 0;
4825 pSMB->DataCount = 0;
4826 pSMB->DataOffset = 0;
4827 pSMB->MaxParameterCount = 0;
4828 /* BB find exact max SMB PDU from sess structure BB */
4829 pSMB->MaxDataCount = cpu_to_le16(4000);
4830 pSMB->MaxSetupCount = 0;
4834 pSMB->Reserved2 = 0;
4835 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4836 struct smb_com_transaction2_get_dfs_refer_req, MaxReferralLevel) - 4);
4837 pSMB->SetupCount = 1;
4838 pSMB->Reserved3 = 0;
4839 pSMB->SubCommand = cpu_to_le16(TRANS2_GET_DFS_REFERRAL);
4840 byte_count = params + 3 /* pad */ ;
4841 pSMB->ParameterCount = cpu_to_le16(params);
4842 pSMB->TotalParameterCount = pSMB->ParameterCount;
4843 pSMB->MaxReferralLevel = cpu_to_le16(3);
4844 inc_rfc1001_len(pSMB, byte_count);
4845 pSMB->ByteCount = cpu_to_le16(byte_count);
4847 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
4848 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4850 cFYI(1, "Send error in GetDFSRefer = %d", rc);
4853 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4855 /* BB Also check if enough total bytes returned? */
4856 if (rc || get_bcc(&pSMBr->hdr) < 17) {
4857 rc = -EIO; /* bad smb */
4861 cFYI(1, "Decoding GetDFSRefer response BCC: %d Offset %d",
4862 get_bcc(&pSMBr->hdr),
4863 le16_to_cpu(pSMBr->t2.DataOffset));
4865 /* parse returned result into more usable form */
4866 rc = parse_DFS_referrals(pSMBr, num_of_nodes,
4867 target_nodes, nls_codepage, remap,
4871 cifs_buf_release(pSMB);
4879 /* Query File System Info such as free space to old servers such as Win 9x */
4881 SMBOldQFSInfo(const int xid, struct cifs_tcon *tcon, struct kstatfs *FSData)
4883 /* level 0x01 SMB_QUERY_FILE_SYSTEM_INFO */
4884 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4885 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4886 FILE_SYSTEM_ALLOC_INFO *response_data;
4888 int bytes_returned = 0;
4889 __u16 params, byte_count;
4891 cFYI(1, "OldQFSInfo");
4893 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4898 params = 2; /* level */
4899 pSMB->TotalDataCount = 0;
4900 pSMB->MaxParameterCount = cpu_to_le16(2);
4901 pSMB->MaxDataCount = cpu_to_le16(1000);
4902 pSMB->MaxSetupCount = 0;
4906 pSMB->Reserved2 = 0;
4907 byte_count = params + 1 /* pad */ ;
4908 pSMB->TotalParameterCount = cpu_to_le16(params);
4909 pSMB->ParameterCount = pSMB->TotalParameterCount;
4910 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4911 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
4912 pSMB->DataCount = 0;
4913 pSMB->DataOffset = 0;
4914 pSMB->SetupCount = 1;
4915 pSMB->Reserved3 = 0;
4916 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
4917 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_ALLOCATION);
4918 inc_rfc1001_len(pSMB, byte_count);
4919 pSMB->ByteCount = cpu_to_le16(byte_count);
4921 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4922 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4924 cFYI(1, "Send error in QFSInfo = %d", rc);
4925 } else { /* decode response */
4926 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4928 if (rc || get_bcc(&pSMBr->hdr) < 18)
4929 rc = -EIO; /* bad smb */
4931 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4932 cFYI(1, "qfsinf resp BCC: %d Offset %d",
4933 get_bcc(&pSMBr->hdr), data_offset);
4935 response_data = (FILE_SYSTEM_ALLOC_INFO *)
4936 (((char *) &pSMBr->hdr.Protocol) + data_offset);
4938 le16_to_cpu(response_data->BytesPerSector) *
4939 le32_to_cpu(response_data->
4940 SectorsPerAllocationUnit);
4942 le32_to_cpu(response_data->TotalAllocationUnits);
4943 FSData->f_bfree = FSData->f_bavail =
4944 le32_to_cpu(response_data->FreeAllocationUnits);
4945 cFYI(1, "Blocks: %lld Free: %lld Block size %ld",
4946 (unsigned long long)FSData->f_blocks,
4947 (unsigned long long)FSData->f_bfree,
4951 cifs_buf_release(pSMB);
4954 goto oldQFSInfoRetry;
4960 CIFSSMBQFSInfo(const int xid, struct cifs_tcon *tcon, struct kstatfs *FSData)
4962 /* level 0x103 SMB_QUERY_FILE_SYSTEM_INFO */
4963 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4964 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4965 FILE_SYSTEM_INFO *response_data;
4967 int bytes_returned = 0;
4968 __u16 params, byte_count;
4970 cFYI(1, "In QFSInfo");
4972 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4977 params = 2; /* level */
4978 pSMB->TotalDataCount = 0;
4979 pSMB->MaxParameterCount = cpu_to_le16(2);
4980 pSMB->MaxDataCount = cpu_to_le16(1000);
4981 pSMB->MaxSetupCount = 0;
4985 pSMB->Reserved2 = 0;
4986 byte_count = params + 1 /* pad */ ;
4987 pSMB->TotalParameterCount = cpu_to_le16(params);
4988 pSMB->ParameterCount = pSMB->TotalParameterCount;
4989 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4990 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
4991 pSMB->DataCount = 0;
4992 pSMB->DataOffset = 0;
4993 pSMB->SetupCount = 1;
4994 pSMB->Reserved3 = 0;
4995 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
4996 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_SIZE_INFO);
4997 inc_rfc1001_len(pSMB, byte_count);
4998 pSMB->ByteCount = cpu_to_le16(byte_count);
5000 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5001 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5003 cFYI(1, "Send error in QFSInfo = %d", rc);
5004 } else { /* decode response */
5005 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5007 if (rc || get_bcc(&pSMBr->hdr) < 24)
5008 rc = -EIO; /* bad smb */
5010 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5014 *) (((char *) &pSMBr->hdr.Protocol) +
5017 le32_to_cpu(response_data->BytesPerSector) *
5018 le32_to_cpu(response_data->
5019 SectorsPerAllocationUnit);
5021 le64_to_cpu(response_data->TotalAllocationUnits);
5022 FSData->f_bfree = FSData->f_bavail =
5023 le64_to_cpu(response_data->FreeAllocationUnits);
5024 cFYI(1, "Blocks: %lld Free: %lld Block size %ld",
5025 (unsigned long long)FSData->f_blocks,
5026 (unsigned long long)FSData->f_bfree,
5030 cifs_buf_release(pSMB);
5039 CIFSSMBQFSAttributeInfo(const int xid, struct cifs_tcon *tcon)
5041 /* level 0x105 SMB_QUERY_FILE_SYSTEM_INFO */
5042 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5043 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5044 FILE_SYSTEM_ATTRIBUTE_INFO *response_data;
5046 int bytes_returned = 0;
5047 __u16 params, byte_count;
5049 cFYI(1, "In QFSAttributeInfo");
5051 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5056 params = 2; /* level */
5057 pSMB->TotalDataCount = 0;
5058 pSMB->MaxParameterCount = cpu_to_le16(2);
5059 /* BB find exact max SMB PDU from sess structure BB */
5060 pSMB->MaxDataCount = cpu_to_le16(1000);
5061 pSMB->MaxSetupCount = 0;
5065 pSMB->Reserved2 = 0;
5066 byte_count = params + 1 /* pad */ ;
5067 pSMB->TotalParameterCount = cpu_to_le16(params);
5068 pSMB->ParameterCount = pSMB->TotalParameterCount;
5069 pSMB->ParameterOffset = cpu_to_le16(offsetof(
5070 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
5071 pSMB->DataCount = 0;
5072 pSMB->DataOffset = 0;
5073 pSMB->SetupCount = 1;
5074 pSMB->Reserved3 = 0;
5075 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5076 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_ATTRIBUTE_INFO);
5077 inc_rfc1001_len(pSMB, byte_count);
5078 pSMB->ByteCount = cpu_to_le16(byte_count);
5080 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5081 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5083 cERROR(1, "Send error in QFSAttributeInfo = %d", rc);
5084 } else { /* decode response */
5085 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5087 if (rc || get_bcc(&pSMBr->hdr) < 13) {
5088 /* BB also check if enough bytes returned */
5089 rc = -EIO; /* bad smb */
5091 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5093 (FILE_SYSTEM_ATTRIBUTE_INFO
5094 *) (((char *) &pSMBr->hdr.Protocol) +
5096 memcpy(&tcon->fsAttrInfo, response_data,
5097 sizeof(FILE_SYSTEM_ATTRIBUTE_INFO));
5100 cifs_buf_release(pSMB);
5103 goto QFSAttributeRetry;
5109 CIFSSMBQFSDeviceInfo(const int xid, struct cifs_tcon *tcon)
5111 /* level 0x104 SMB_QUERY_FILE_SYSTEM_INFO */
5112 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5113 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5114 FILE_SYSTEM_DEVICE_INFO *response_data;
5116 int bytes_returned = 0;
5117 __u16 params, byte_count;
5119 cFYI(1, "In QFSDeviceInfo");
5121 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5126 params = 2; /* level */
5127 pSMB->TotalDataCount = 0;
5128 pSMB->MaxParameterCount = cpu_to_le16(2);
5129 /* BB find exact max SMB PDU from sess structure BB */
5130 pSMB->MaxDataCount = cpu_to_le16(1000);
5131 pSMB->MaxSetupCount = 0;
5135 pSMB->Reserved2 = 0;
5136 byte_count = params + 1 /* pad */ ;
5137 pSMB->TotalParameterCount = cpu_to_le16(params);
5138 pSMB->ParameterCount = pSMB->TotalParameterCount;
5139 pSMB->ParameterOffset = cpu_to_le16(offsetof(
5140 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
5142 pSMB->DataCount = 0;
5143 pSMB->DataOffset = 0;
5144 pSMB->SetupCount = 1;
5145 pSMB->Reserved3 = 0;
5146 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5147 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_DEVICE_INFO);
5148 inc_rfc1001_len(pSMB, byte_count);
5149 pSMB->ByteCount = cpu_to_le16(byte_count);
5151 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5152 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5154 cFYI(1, "Send error in QFSDeviceInfo = %d", rc);
5155 } else { /* decode response */
5156 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5158 if (rc || get_bcc(&pSMBr->hdr) <
5159 sizeof(FILE_SYSTEM_DEVICE_INFO))
5160 rc = -EIO; /* bad smb */
5162 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5164 (FILE_SYSTEM_DEVICE_INFO *)
5165 (((char *) &pSMBr->hdr.Protocol) +
5167 memcpy(&tcon->fsDevInfo, response_data,
5168 sizeof(FILE_SYSTEM_DEVICE_INFO));
5171 cifs_buf_release(pSMB);
5174 goto QFSDeviceRetry;
5180 CIFSSMBQFSUnixInfo(const int xid, struct cifs_tcon *tcon)
5182 /* level 0x200 SMB_QUERY_CIFS_UNIX_INFO */
5183 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5184 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5185 FILE_SYSTEM_UNIX_INFO *response_data;
5187 int bytes_returned = 0;
5188 __u16 params, byte_count;
5190 cFYI(1, "In QFSUnixInfo");
5192 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5193 (void **) &pSMB, (void **) &pSMBr);
5197 params = 2; /* level */
5198 pSMB->TotalDataCount = 0;
5199 pSMB->DataCount = 0;
5200 pSMB->DataOffset = 0;
5201 pSMB->MaxParameterCount = cpu_to_le16(2);
5202 /* BB find exact max SMB PDU from sess structure BB */
5203 pSMB->MaxDataCount = cpu_to_le16(100);
5204 pSMB->MaxSetupCount = 0;
5208 pSMB->Reserved2 = 0;
5209 byte_count = params + 1 /* pad */ ;
5210 pSMB->ParameterCount = cpu_to_le16(params);
5211 pSMB->TotalParameterCount = pSMB->ParameterCount;
5212 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5213 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
5214 pSMB->SetupCount = 1;
5215 pSMB->Reserved3 = 0;
5216 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5217 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_CIFS_UNIX_INFO);
5218 inc_rfc1001_len(pSMB, byte_count);
5219 pSMB->ByteCount = cpu_to_le16(byte_count);
5221 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5222 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5224 cERROR(1, "Send error in QFSUnixInfo = %d", rc);
5225 } else { /* decode response */
5226 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5228 if (rc || get_bcc(&pSMBr->hdr) < 13) {
5229 rc = -EIO; /* bad smb */
5231 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5233 (FILE_SYSTEM_UNIX_INFO
5234 *) (((char *) &pSMBr->hdr.Protocol) +
5236 memcpy(&tcon->fsUnixInfo, response_data,
5237 sizeof(FILE_SYSTEM_UNIX_INFO));
5240 cifs_buf_release(pSMB);
5250 CIFSSMBSetFSUnixInfo(const int xid, struct cifs_tcon *tcon, __u64 cap)
5252 /* level 0x200 SMB_SET_CIFS_UNIX_INFO */
5253 TRANSACTION2_SETFSI_REQ *pSMB = NULL;
5254 TRANSACTION2_SETFSI_RSP *pSMBr = NULL;
5256 int bytes_returned = 0;
5257 __u16 params, param_offset, offset, byte_count;
5259 cFYI(1, "In SETFSUnixInfo");
5261 /* BB switch to small buf init to save memory */
5262 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5263 (void **) &pSMB, (void **) &pSMBr);
5267 params = 4; /* 2 bytes zero followed by info level. */
5268 pSMB->MaxSetupCount = 0;
5272 pSMB->Reserved2 = 0;
5273 param_offset = offsetof(struct smb_com_transaction2_setfsi_req, FileNum)
5275 offset = param_offset + params;
5277 pSMB->MaxParameterCount = cpu_to_le16(4);
5278 /* BB find exact max SMB PDU from sess structure BB */
5279 pSMB->MaxDataCount = cpu_to_le16(100);
5280 pSMB->SetupCount = 1;
5281 pSMB->Reserved3 = 0;
5282 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FS_INFORMATION);
5283 byte_count = 1 /* pad */ + params + 12;
5285 pSMB->DataCount = cpu_to_le16(12);
5286 pSMB->ParameterCount = cpu_to_le16(params);
5287 pSMB->TotalDataCount = pSMB->DataCount;
5288 pSMB->TotalParameterCount = pSMB->ParameterCount;
5289 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5290 pSMB->DataOffset = cpu_to_le16(offset);
5294 pSMB->InformationLevel = cpu_to_le16(SMB_SET_CIFS_UNIX_INFO);
5297 pSMB->ClientUnixMajor = cpu_to_le16(CIFS_UNIX_MAJOR_VERSION);
5298 pSMB->ClientUnixMinor = cpu_to_le16(CIFS_UNIX_MINOR_VERSION);
5299 pSMB->ClientUnixCap = cpu_to_le64(cap);
5301 inc_rfc1001_len(pSMB, byte_count);
5302 pSMB->ByteCount = cpu_to_le16(byte_count);
5304 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5305 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5307 cERROR(1, "Send error in SETFSUnixInfo = %d", rc);
5308 } else { /* decode response */
5309 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5311 rc = -EIO; /* bad smb */
5313 cifs_buf_release(pSMB);
5316 goto SETFSUnixRetry;
5324 CIFSSMBQFSPosixInfo(const int xid, struct cifs_tcon *tcon,
5325 struct kstatfs *FSData)
5327 /* level 0x201 SMB_QUERY_CIFS_POSIX_INFO */
5328 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5329 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5330 FILE_SYSTEM_POSIX_INFO *response_data;
5332 int bytes_returned = 0;
5333 __u16 params, byte_count;
5335 cFYI(1, "In QFSPosixInfo");
5337 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5342 params = 2; /* level */
5343 pSMB->TotalDataCount = 0;
5344 pSMB->DataCount = 0;
5345 pSMB->DataOffset = 0;
5346 pSMB->MaxParameterCount = cpu_to_le16(2);
5347 /* BB find exact max SMB PDU from sess structure BB */
5348 pSMB->MaxDataCount = cpu_to_le16(100);
5349 pSMB->MaxSetupCount = 0;
5353 pSMB->Reserved2 = 0;
5354 byte_count = params + 1 /* pad */ ;
5355 pSMB->ParameterCount = cpu_to_le16(params);
5356 pSMB->TotalParameterCount = pSMB->ParameterCount;
5357 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5358 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
5359 pSMB->SetupCount = 1;
5360 pSMB->Reserved3 = 0;
5361 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5362 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_FS_INFO);
5363 inc_rfc1001_len(pSMB, byte_count);
5364 pSMB->ByteCount = cpu_to_le16(byte_count);
5366 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5367 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5369 cFYI(1, "Send error in QFSUnixInfo = %d", rc);
5370 } else { /* decode response */
5371 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5373 if (rc || get_bcc(&pSMBr->hdr) < 13) {
5374 rc = -EIO; /* bad smb */
5376 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5378 (FILE_SYSTEM_POSIX_INFO
5379 *) (((char *) &pSMBr->hdr.Protocol) +
5382 le32_to_cpu(response_data->BlockSize);
5384 le64_to_cpu(response_data->TotalBlocks);
5386 le64_to_cpu(response_data->BlocksAvail);
5387 if (response_data->UserBlocksAvail == cpu_to_le64(-1)) {
5388 FSData->f_bavail = FSData->f_bfree;
5391 le64_to_cpu(response_data->UserBlocksAvail);
5393 if (response_data->TotalFileNodes != cpu_to_le64(-1))
5395 le64_to_cpu(response_data->TotalFileNodes);
5396 if (response_data->FreeFileNodes != cpu_to_le64(-1))
5398 le64_to_cpu(response_data->FreeFileNodes);
5401 cifs_buf_release(pSMB);
5410 /* We can not use write of zero bytes trick to
5411 set file size due to need for large file support. Also note that
5412 this SetPathInfo is preferred to SetFileInfo based method in next
5413 routine which is only needed to work around a sharing violation bug
5414 in Samba which this routine can run into */
5417 CIFSSMBSetEOF(const int xid, struct cifs_tcon *tcon, const char *fileName,
5418 __u64 size, bool SetAllocation,
5419 const struct nls_table *nls_codepage, int remap)
5421 struct smb_com_transaction2_spi_req *pSMB = NULL;
5422 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
5423 struct file_end_of_file_info *parm_data;
5426 int bytes_returned = 0;
5427 __u16 params, byte_count, data_count, param_offset, offset;
5429 cFYI(1, "In SetEOF");
5431 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5436 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5438 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
5439 PATH_MAX, nls_codepage, remap);
5440 name_len++; /* trailing null */
5442 } else { /* BB improve the check for buffer overruns BB */
5443 name_len = strnlen(fileName, PATH_MAX);
5444 name_len++; /* trailing null */
5445 strncpy(pSMB->FileName, fileName, name_len);
5447 params = 6 + name_len;
5448 data_count = sizeof(struct file_end_of_file_info);
5449 pSMB->MaxParameterCount = cpu_to_le16(2);
5450 pSMB->MaxDataCount = cpu_to_le16(4100);
5451 pSMB->MaxSetupCount = 0;
5455 pSMB->Reserved2 = 0;
5456 param_offset = offsetof(struct smb_com_transaction2_spi_req,
5457 InformationLevel) - 4;
5458 offset = param_offset + params;
5459 if (SetAllocation) {
5460 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5461 pSMB->InformationLevel =
5462 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5464 pSMB->InformationLevel =
5465 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
5466 } else /* Set File Size */ {
5467 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5468 pSMB->InformationLevel =
5469 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
5471 pSMB->InformationLevel =
5472 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
5476 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol) +
5478 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5479 pSMB->DataOffset = cpu_to_le16(offset);
5480 pSMB->SetupCount = 1;
5481 pSMB->Reserved3 = 0;
5482 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5483 byte_count = 3 /* pad */ + params + data_count;
5484 pSMB->DataCount = cpu_to_le16(data_count);
5485 pSMB->TotalDataCount = pSMB->DataCount;
5486 pSMB->ParameterCount = cpu_to_le16(params);
5487 pSMB->TotalParameterCount = pSMB->ParameterCount;
5488 pSMB->Reserved4 = 0;
5489 inc_rfc1001_len(pSMB, byte_count);
5490 parm_data->FileSize = cpu_to_le64(size);
5491 pSMB->ByteCount = cpu_to_le16(byte_count);
5492 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5493 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5495 cFYI(1, "SetPathInfo (file size) returned %d", rc);
5497 cifs_buf_release(pSMB);
5506 CIFSSMBSetFileSize(const int xid, struct cifs_tcon *tcon, __u64 size,
5507 __u16 fid, __u32 pid_of_opener, bool SetAllocation)
5509 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5510 struct file_end_of_file_info *parm_data;
5512 __u16 params, param_offset, offset, byte_count, count;
5514 cFYI(1, "SetFileSize (via SetFileInfo) %lld",
5516 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5521 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5522 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5525 pSMB->MaxSetupCount = 0;
5529 pSMB->Reserved2 = 0;
5530 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5531 offset = param_offset + params;
5533 count = sizeof(struct file_end_of_file_info);
5534 pSMB->MaxParameterCount = cpu_to_le16(2);
5535 /* BB find exact max SMB PDU from sess structure BB */
5536 pSMB->MaxDataCount = cpu_to_le16(1000);
5537 pSMB->SetupCount = 1;
5538 pSMB->Reserved3 = 0;
5539 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5540 byte_count = 3 /* pad */ + params + count;
5541 pSMB->DataCount = cpu_to_le16(count);
5542 pSMB->ParameterCount = cpu_to_le16(params);
5543 pSMB->TotalDataCount = pSMB->DataCount;
5544 pSMB->TotalParameterCount = pSMB->ParameterCount;
5545 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5547 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol)
5549 pSMB->DataOffset = cpu_to_le16(offset);
5550 parm_data->FileSize = cpu_to_le64(size);
5552 if (SetAllocation) {
5553 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5554 pSMB->InformationLevel =
5555 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5557 pSMB->InformationLevel =
5558 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
5559 } else /* Set File Size */ {
5560 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5561 pSMB->InformationLevel =
5562 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
5564 pSMB->InformationLevel =
5565 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
5567 pSMB->Reserved4 = 0;
5568 inc_rfc1001_len(pSMB, byte_count);
5569 pSMB->ByteCount = cpu_to_le16(byte_count);
5570 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
5572 cFYI(1, "Send error in SetFileInfo (SetFileSize) = %d", rc);
5575 /* Note: On -EAGAIN error only caller can retry on handle based calls
5576 since file handle passed in no longer valid */
5581 /* Some legacy servers such as NT4 require that the file times be set on
5582 an open handle, rather than by pathname - this is awkward due to
5583 potential access conflicts on the open, but it is unavoidable for these
5584 old servers since the only other choice is to go from 100 nanosecond DCE
5585 time and resort to the original setpathinfo level which takes the ancient
5586 DOS time format with 2 second granularity */
5588 CIFSSMBSetFileInfo(const int xid, struct cifs_tcon *tcon,
5589 const FILE_BASIC_INFO *data, __u16 fid, __u32 pid_of_opener)
5591 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5594 __u16 params, param_offset, offset, byte_count, count;
5596 cFYI(1, "Set Times (via SetFileInfo)");
5597 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5602 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5603 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5606 pSMB->MaxSetupCount = 0;
5610 pSMB->Reserved2 = 0;
5611 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5612 offset = param_offset + params;
5614 data_offset = (char *)pSMB +
5615 offsetof(struct smb_hdr, Protocol) + offset;
5617 count = sizeof(FILE_BASIC_INFO);
5618 pSMB->MaxParameterCount = cpu_to_le16(2);
5619 /* BB find max SMB PDU from sess */
5620 pSMB->MaxDataCount = cpu_to_le16(1000);
5621 pSMB->SetupCount = 1;
5622 pSMB->Reserved3 = 0;
5623 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5624 byte_count = 3 /* pad */ + params + count;
5625 pSMB->DataCount = cpu_to_le16(count);
5626 pSMB->ParameterCount = cpu_to_le16(params);
5627 pSMB->TotalDataCount = pSMB->DataCount;
5628 pSMB->TotalParameterCount = pSMB->ParameterCount;
5629 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5630 pSMB->DataOffset = cpu_to_le16(offset);
5632 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5633 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5635 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5636 pSMB->Reserved4 = 0;
5637 inc_rfc1001_len(pSMB, byte_count);
5638 pSMB->ByteCount = cpu_to_le16(byte_count);
5639 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
5640 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
5642 cFYI(1, "Send error in Set Time (SetFileInfo) = %d", rc);
5644 /* Note: On -EAGAIN error only caller can retry on handle based calls
5645 since file handle passed in no longer valid */
5651 CIFSSMBSetFileDisposition(const int xid, struct cifs_tcon *tcon,
5652 bool delete_file, __u16 fid, __u32 pid_of_opener)
5654 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5657 __u16 params, param_offset, offset, byte_count, count;
5659 cFYI(1, "Set File Disposition (via SetFileInfo)");
5660 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5665 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5666 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5669 pSMB->MaxSetupCount = 0;
5673 pSMB->Reserved2 = 0;
5674 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5675 offset = param_offset + params;
5677 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5680 pSMB->MaxParameterCount = cpu_to_le16(2);
5681 /* BB find max SMB PDU from sess */
5682 pSMB->MaxDataCount = cpu_to_le16(1000);
5683 pSMB->SetupCount = 1;
5684 pSMB->Reserved3 = 0;
5685 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5686 byte_count = 3 /* pad */ + params + count;
5687 pSMB->DataCount = cpu_to_le16(count);
5688 pSMB->ParameterCount = cpu_to_le16(params);
5689 pSMB->TotalDataCount = pSMB->DataCount;
5690 pSMB->TotalParameterCount = pSMB->ParameterCount;
5691 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5692 pSMB->DataOffset = cpu_to_le16(offset);
5694 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_DISPOSITION_INFO);
5695 pSMB->Reserved4 = 0;
5696 inc_rfc1001_len(pSMB, byte_count);
5697 pSMB->ByteCount = cpu_to_le16(byte_count);
5698 *data_offset = delete_file ? 1 : 0;
5699 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
5701 cFYI(1, "Send error in SetFileDisposition = %d", rc);
5707 CIFSSMBSetPathInfo(const int xid, struct cifs_tcon *tcon,
5708 const char *fileName, const FILE_BASIC_INFO *data,
5709 const struct nls_table *nls_codepage, int remap)
5711 TRANSACTION2_SPI_REQ *pSMB = NULL;
5712 TRANSACTION2_SPI_RSP *pSMBr = NULL;
5715 int bytes_returned = 0;
5717 __u16 params, param_offset, offset, byte_count, count;
5719 cFYI(1, "In SetTimes");
5722 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5727 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5729 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
5730 PATH_MAX, nls_codepage, remap);
5731 name_len++; /* trailing null */
5733 } else { /* BB improve the check for buffer overruns BB */
5734 name_len = strnlen(fileName, PATH_MAX);
5735 name_len++; /* trailing null */
5736 strncpy(pSMB->FileName, fileName, name_len);
5739 params = 6 + name_len;
5740 count = sizeof(FILE_BASIC_INFO);
5741 pSMB->MaxParameterCount = cpu_to_le16(2);
5742 /* BB find max SMB PDU from sess structure BB */
5743 pSMB->MaxDataCount = cpu_to_le16(1000);
5744 pSMB->MaxSetupCount = 0;
5748 pSMB->Reserved2 = 0;
5749 param_offset = offsetof(struct smb_com_transaction2_spi_req,
5750 InformationLevel) - 4;
5751 offset = param_offset + params;
5752 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5753 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5754 pSMB->DataOffset = cpu_to_le16(offset);
5755 pSMB->SetupCount = 1;
5756 pSMB->Reserved3 = 0;
5757 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5758 byte_count = 3 /* pad */ + params + count;
5760 pSMB->DataCount = cpu_to_le16(count);
5761 pSMB->ParameterCount = cpu_to_le16(params);
5762 pSMB->TotalDataCount = pSMB->DataCount;
5763 pSMB->TotalParameterCount = pSMB->ParameterCount;
5764 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5765 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5767 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5768 pSMB->Reserved4 = 0;
5769 inc_rfc1001_len(pSMB, byte_count);
5770 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
5771 pSMB->ByteCount = cpu_to_le16(byte_count);
5772 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5773 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5775 cFYI(1, "SetPathInfo (times) returned %d", rc);
5777 cifs_buf_release(pSMB);
5785 /* Can not be used to set time stamps yet (due to old DOS time format) */
5786 /* Can be used to set attributes */
5787 #if 0 /* Possibly not needed - since it turns out that strangely NT4 has a bug
5788 handling it anyway and NT4 was what we thought it would be needed for
5789 Do not delete it until we prove whether needed for Win9x though */
5791 CIFSSMBSetAttrLegacy(int xid, struct cifs_tcon *tcon, char *fileName,
5792 __u16 dos_attrs, const struct nls_table *nls_codepage)
5794 SETATTR_REQ *pSMB = NULL;
5795 SETATTR_RSP *pSMBr = NULL;
5800 cFYI(1, "In SetAttrLegacy");
5803 rc = smb_init(SMB_COM_SETATTR, 8, tcon, (void **) &pSMB,
5808 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5810 ConvertToUTF16((__le16 *) pSMB->fileName, fileName,
5811 PATH_MAX, nls_codepage);
5812 name_len++; /* trailing null */
5814 } else { /* BB improve the check for buffer overruns BB */
5815 name_len = strnlen(fileName, PATH_MAX);
5816 name_len++; /* trailing null */
5817 strncpy(pSMB->fileName, fileName, name_len);
5819 pSMB->attr = cpu_to_le16(dos_attrs);
5820 pSMB->BufferFormat = 0x04;
5821 inc_rfc1001_len(pSMB, name_len + 1);
5822 pSMB->ByteCount = cpu_to_le16(name_len + 1);
5823 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5824 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5826 cFYI(1, "Error in LegacySetAttr = %d", rc);
5828 cifs_buf_release(pSMB);
5831 goto SetAttrLgcyRetry;
5835 #endif /* temporarily unneeded SetAttr legacy function */
5838 cifs_fill_unix_set_info(FILE_UNIX_BASIC_INFO *data_offset,
5839 const struct cifs_unix_set_info_args *args)
5841 u64 mode = args->mode;
5844 * Samba server ignores set of file size to zero due to bugs in some
5845 * older clients, but we should be precise - we use SetFileSize to
5846 * set file size and do not want to truncate file size to zero
5847 * accidentally as happened on one Samba server beta by putting
5848 * zero instead of -1 here
5850 data_offset->EndOfFile = cpu_to_le64(NO_CHANGE_64);
5851 data_offset->NumOfBytes = cpu_to_le64(NO_CHANGE_64);
5852 data_offset->LastStatusChange = cpu_to_le64(args->ctime);
5853 data_offset->LastAccessTime = cpu_to_le64(args->atime);
5854 data_offset->LastModificationTime = cpu_to_le64(args->mtime);
5855 data_offset->Uid = cpu_to_le64(args->uid);
5856 data_offset->Gid = cpu_to_le64(args->gid);
5857 /* better to leave device as zero when it is */
5858 data_offset->DevMajor = cpu_to_le64(MAJOR(args->device));
5859 data_offset->DevMinor = cpu_to_le64(MINOR(args->device));
5860 data_offset->Permissions = cpu_to_le64(mode);
5863 data_offset->Type = cpu_to_le32(UNIX_FILE);
5864 else if (S_ISDIR(mode))
5865 data_offset->Type = cpu_to_le32(UNIX_DIR);
5866 else if (S_ISLNK(mode))
5867 data_offset->Type = cpu_to_le32(UNIX_SYMLINK);
5868 else if (S_ISCHR(mode))
5869 data_offset->Type = cpu_to_le32(UNIX_CHARDEV);
5870 else if (S_ISBLK(mode))
5871 data_offset->Type = cpu_to_le32(UNIX_BLOCKDEV);
5872 else if (S_ISFIFO(mode))
5873 data_offset->Type = cpu_to_le32(UNIX_FIFO);
5874 else if (S_ISSOCK(mode))
5875 data_offset->Type = cpu_to_le32(UNIX_SOCKET);
5879 CIFSSMBUnixSetFileInfo(const int xid, struct cifs_tcon *tcon,
5880 const struct cifs_unix_set_info_args *args,
5881 u16 fid, u32 pid_of_opener)
5883 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5886 u16 params, param_offset, offset, byte_count, count;
5888 cFYI(1, "Set Unix Info (via SetFileInfo)");
5889 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5894 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5895 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5898 pSMB->MaxSetupCount = 0;
5902 pSMB->Reserved2 = 0;
5903 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5904 offset = param_offset + params;
5906 data_offset = (char *)pSMB +
5907 offsetof(struct smb_hdr, Protocol) + offset;
5909 count = sizeof(FILE_UNIX_BASIC_INFO);
5911 pSMB->MaxParameterCount = cpu_to_le16(2);
5912 /* BB find max SMB PDU from sess */
5913 pSMB->MaxDataCount = cpu_to_le16(1000);
5914 pSMB->SetupCount = 1;
5915 pSMB->Reserved3 = 0;
5916 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5917 byte_count = 3 /* pad */ + params + count;
5918 pSMB->DataCount = cpu_to_le16(count);
5919 pSMB->ParameterCount = cpu_to_le16(params);
5920 pSMB->TotalDataCount = pSMB->DataCount;
5921 pSMB->TotalParameterCount = pSMB->ParameterCount;
5922 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5923 pSMB->DataOffset = cpu_to_le16(offset);
5925 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
5926 pSMB->Reserved4 = 0;
5927 inc_rfc1001_len(pSMB, byte_count);
5928 pSMB->ByteCount = cpu_to_le16(byte_count);
5930 cifs_fill_unix_set_info((FILE_UNIX_BASIC_INFO *)data_offset, args);
5932 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
5934 cFYI(1, "Send error in Set Time (SetFileInfo) = %d", rc);
5936 /* Note: On -EAGAIN error only caller can retry on handle based calls
5937 since file handle passed in no longer valid */
5943 CIFSSMBUnixSetPathInfo(const int xid, struct cifs_tcon *tcon, char *fileName,
5944 const struct cifs_unix_set_info_args *args,
5945 const struct nls_table *nls_codepage, int remap)
5947 TRANSACTION2_SPI_REQ *pSMB = NULL;
5948 TRANSACTION2_SPI_RSP *pSMBr = NULL;
5951 int bytes_returned = 0;
5952 FILE_UNIX_BASIC_INFO *data_offset;
5953 __u16 params, param_offset, offset, count, byte_count;
5955 cFYI(1, "In SetUID/GID/Mode");
5957 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5962 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5964 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
5965 PATH_MAX, nls_codepage, remap);
5966 name_len++; /* trailing null */
5968 } else { /* BB improve the check for buffer overruns BB */
5969 name_len = strnlen(fileName, PATH_MAX);
5970 name_len++; /* trailing null */
5971 strncpy(pSMB->FileName, fileName, name_len);
5974 params = 6 + name_len;
5975 count = sizeof(FILE_UNIX_BASIC_INFO);
5976 pSMB->MaxParameterCount = cpu_to_le16(2);
5977 /* BB find max SMB PDU from sess structure BB */
5978 pSMB->MaxDataCount = cpu_to_le16(1000);
5979 pSMB->MaxSetupCount = 0;
5983 pSMB->Reserved2 = 0;
5984 param_offset = offsetof(struct smb_com_transaction2_spi_req,
5985 InformationLevel) - 4;
5986 offset = param_offset + params;
5988 (FILE_UNIX_BASIC_INFO *) ((char *) &pSMB->hdr.Protocol +
5990 memset(data_offset, 0, count);
5991 pSMB->DataOffset = cpu_to_le16(offset);
5992 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5993 pSMB->SetupCount = 1;
5994 pSMB->Reserved3 = 0;
5995 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5996 byte_count = 3 /* pad */ + params + count;
5997 pSMB->ParameterCount = cpu_to_le16(params);
5998 pSMB->DataCount = cpu_to_le16(count);
5999 pSMB->TotalParameterCount = pSMB->ParameterCount;
6000 pSMB->TotalDataCount = pSMB->DataCount;
6001 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
6002 pSMB->Reserved4 = 0;
6003 inc_rfc1001_len(pSMB, byte_count);
6005 cifs_fill_unix_set_info(data_offset, args);
6007 pSMB->ByteCount = cpu_to_le16(byte_count);
6008 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6009 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
6011 cFYI(1, "SetPathInfo (perms) returned %d", rc);
6013 cifs_buf_release(pSMB);
6019 #ifdef CONFIG_CIFS_XATTR
6021 * Do a path-based QUERY_ALL_EAS call and parse the result. This is a common
6022 * function used by listxattr and getxattr type calls. When ea_name is set,
6023 * it looks for that attribute name and stuffs that value into the EAData
6024 * buffer. When ea_name is NULL, it stuffs a list of attribute names into the
6025 * buffer. In both cases, the return value is either the length of the
6026 * resulting data or a negative error code. If EAData is a NULL pointer then
6027 * the data isn't copied to it, but the length is returned.
6030 CIFSSMBQAllEAs(const int xid, struct cifs_tcon *tcon,
6031 const unsigned char *searchName, const unsigned char *ea_name,
6032 char *EAData, size_t buf_size,
6033 const struct nls_table *nls_codepage, int remap)
6035 /* BB assumes one setup word */
6036 TRANSACTION2_QPI_REQ *pSMB = NULL;
6037 TRANSACTION2_QPI_RSP *pSMBr = NULL;
6041 struct fealist *ea_response_data;
6042 struct fea *temp_fea;
6045 __u16 params, byte_count, data_offset;
6046 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
6048 cFYI(1, "In Query All EAs path %s", searchName);
6050 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6055 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6057 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
6058 PATH_MAX, nls_codepage, remap);
6059 list_len++; /* trailing null */
6061 } else { /* BB improve the check for buffer overruns BB */
6062 list_len = strnlen(searchName, PATH_MAX);
6063 list_len++; /* trailing null */
6064 strncpy(pSMB->FileName, searchName, list_len);
6067 params = 2 /* level */ + 4 /* reserved */ + list_len /* includes NUL */;
6068 pSMB->TotalDataCount = 0;
6069 pSMB->MaxParameterCount = cpu_to_le16(2);
6070 /* BB find exact max SMB PDU from sess structure BB */
6071 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
6072 pSMB->MaxSetupCount = 0;
6076 pSMB->Reserved2 = 0;
6077 pSMB->ParameterOffset = cpu_to_le16(offsetof(
6078 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
6079 pSMB->DataCount = 0;
6080 pSMB->DataOffset = 0;
6081 pSMB->SetupCount = 1;
6082 pSMB->Reserved3 = 0;
6083 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
6084 byte_count = params + 1 /* pad */ ;
6085 pSMB->TotalParameterCount = cpu_to_le16(params);
6086 pSMB->ParameterCount = pSMB->TotalParameterCount;
6087 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_QUERY_ALL_EAS);
6088 pSMB->Reserved4 = 0;
6089 inc_rfc1001_len(pSMB, byte_count);
6090 pSMB->ByteCount = cpu_to_le16(byte_count);
6092 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6093 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
6095 cFYI(1, "Send error in QueryAllEAs = %d", rc);
6100 /* BB also check enough total bytes returned */
6101 /* BB we need to improve the validity checking
6102 of these trans2 responses */
6104 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
6105 if (rc || get_bcc(&pSMBr->hdr) < 4) {
6106 rc = -EIO; /* bad smb */
6110 /* check that length of list is not more than bcc */
6111 /* check that each entry does not go beyond length
6113 /* check that each element of each entry does not
6114 go beyond end of list */
6115 /* validate_trans2_offsets() */
6116 /* BB check if start of smb + data_offset > &bcc+ bcc */
6118 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
6119 ea_response_data = (struct fealist *)
6120 (((char *) &pSMBr->hdr.Protocol) + data_offset);
6122 list_len = le32_to_cpu(ea_response_data->list_len);
6123 cFYI(1, "ea length %d", list_len);
6124 if (list_len <= 8) {
6125 cFYI(1, "empty EA list returned from server");
6129 /* make sure list_len doesn't go past end of SMB */
6130 end_of_smb = (char *)pByteArea(&pSMBr->hdr) + get_bcc(&pSMBr->hdr);
6131 if ((char *)ea_response_data + list_len > end_of_smb) {
6132 cFYI(1, "EA list appears to go beyond SMB");
6137 /* account for ea list len */
6139 temp_fea = ea_response_data->list;
6140 temp_ptr = (char *)temp_fea;
6141 while (list_len > 0) {
6142 unsigned int name_len;
6147 /* make sure we can read name_len and value_len */
6149 cFYI(1, "EA entry goes beyond length of list");
6154 name_len = temp_fea->name_len;
6155 value_len = le16_to_cpu(temp_fea->value_len);
6156 list_len -= name_len + 1 + value_len;
6158 cFYI(1, "EA entry goes beyond length of list");
6164 if (ea_name_len == name_len &&
6165 memcmp(ea_name, temp_ptr, name_len) == 0) {
6166 temp_ptr += name_len + 1;
6170 if ((size_t)value_len > buf_size) {
6174 memcpy(EAData, temp_ptr, value_len);
6178 /* account for prefix user. and trailing null */
6179 rc += (5 + 1 + name_len);
6180 if (rc < (int) buf_size) {
6181 memcpy(EAData, "user.", 5);
6183 memcpy(EAData, temp_ptr, name_len);
6185 /* null terminate name */
6188 } else if (buf_size == 0) {
6189 /* skip copy - calc size only */
6191 /* stop before overrun buffer */
6196 temp_ptr += name_len + 1 + value_len;
6197 temp_fea = (struct fea *)temp_ptr;
6200 /* didn't find the named attribute */
6205 cifs_buf_release(pSMB);
6213 CIFSSMBSetEA(const int xid, struct cifs_tcon *tcon, const char *fileName,
6214 const char *ea_name, const void *ea_value,
6215 const __u16 ea_value_len, const struct nls_table *nls_codepage,
6218 struct smb_com_transaction2_spi_req *pSMB = NULL;
6219 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
6220 struct fealist *parm_data;
6223 int bytes_returned = 0;
6224 __u16 params, param_offset, byte_count, offset, count;
6226 cFYI(1, "In SetEA");
6228 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6233 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6235 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
6236 PATH_MAX, nls_codepage, remap);
6237 name_len++; /* trailing null */
6239 } else { /* BB improve the check for buffer overruns BB */
6240 name_len = strnlen(fileName, PATH_MAX);
6241 name_len++; /* trailing null */
6242 strncpy(pSMB->FileName, fileName, name_len);
6245 params = 6 + name_len;
6247 /* done calculating parms using name_len of file name,
6248 now use name_len to calculate length of ea name
6249 we are going to create in the inode xattrs */
6250 if (ea_name == NULL)
6253 name_len = strnlen(ea_name, 255);
6255 count = sizeof(*parm_data) + ea_value_len + name_len;
6256 pSMB->MaxParameterCount = cpu_to_le16(2);
6257 /* BB find max SMB PDU from sess */
6258 pSMB->MaxDataCount = cpu_to_le16(1000);
6259 pSMB->MaxSetupCount = 0;
6263 pSMB->Reserved2 = 0;
6264 param_offset = offsetof(struct smb_com_transaction2_spi_req,
6265 InformationLevel) - 4;
6266 offset = param_offset + params;
6267 pSMB->InformationLevel =
6268 cpu_to_le16(SMB_SET_FILE_EA);
6271 (struct fealist *) (((char *) &pSMB->hdr.Protocol) +
6273 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6274 pSMB->DataOffset = cpu_to_le16(offset);
6275 pSMB->SetupCount = 1;
6276 pSMB->Reserved3 = 0;
6277 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6278 byte_count = 3 /* pad */ + params + count;
6279 pSMB->DataCount = cpu_to_le16(count);
6280 parm_data->list_len = cpu_to_le32(count);
6281 parm_data->list[0].EA_flags = 0;
6282 /* we checked above that name len is less than 255 */
6283 parm_data->list[0].name_len = (__u8)name_len;
6284 /* EA names are always ASCII */
6286 strncpy(parm_data->list[0].name, ea_name, name_len);
6287 parm_data->list[0].name[name_len] = 0;
6288 parm_data->list[0].value_len = cpu_to_le16(ea_value_len);
6289 /* caller ensures that ea_value_len is less than 64K but
6290 we need to ensure that it fits within the smb */
6292 /*BB add length check to see if it would fit in
6293 negotiated SMB buffer size BB */
6294 /* if (ea_value_len > buffer_size - 512 (enough for header)) */
6296 memcpy(parm_data->list[0].name+name_len+1,
6297 ea_value, ea_value_len);
6299 pSMB->TotalDataCount = pSMB->DataCount;
6300 pSMB->ParameterCount = cpu_to_le16(params);
6301 pSMB->TotalParameterCount = pSMB->ParameterCount;
6302 pSMB->Reserved4 = 0;
6303 inc_rfc1001_len(pSMB, byte_count);
6304 pSMB->ByteCount = cpu_to_le16(byte_count);
6305 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6306 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
6308 cFYI(1, "SetPathInfo (EA) returned %d", rc);
6310 cifs_buf_release(pSMB);
6319 #ifdef CONFIG_CIFS_DNOTIFY_EXPERIMENTAL /* BB unused temporarily */
6321 * Years ago the kernel added a "dnotify" function for Samba server,
6322 * to allow network clients (such as Windows) to display updated
6323 * lists of files in directory listings automatically when
6324 * files are added by one user when another user has the
6325 * same directory open on their desktop. The Linux cifs kernel
6326 * client hooked into the kernel side of this interface for
6327 * the same reason, but ironically when the VFS moved from
6328 * "dnotify" to "inotify" it became harder to plug in Linux
6329 * network file system clients (the most obvious use case
6330 * for notify interfaces is when multiple users can update
6331 * the contents of the same directory - exactly what network
6332 * file systems can do) although the server (Samba) could
6333 * still use it. For the short term we leave the worker
6334 * function ifdeffed out (below) until inotify is fixed
6335 * in the VFS to make it easier to plug in network file
6336 * system clients. If inotify turns out to be permanently
6337 * incompatible for network fs clients, we could instead simply
6338 * expose this config flag by adding a future cifs (and smb2) notify ioctl.
6340 int CIFSSMBNotify(const int xid, struct cifs_tcon *tcon,
6341 const int notify_subdirs, const __u16 netfid,
6342 __u32 filter, struct file *pfile, int multishot,
6343 const struct nls_table *nls_codepage)
6346 struct smb_com_transaction_change_notify_req *pSMB = NULL;
6347 struct smb_com_ntransaction_change_notify_rsp *pSMBr = NULL;
6348 struct dir_notify_req *dnotify_req;
6351 cFYI(1, "In CIFSSMBNotify for file handle %d", (int)netfid);
6352 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
6357 pSMB->TotalParameterCount = 0 ;
6358 pSMB->TotalDataCount = 0;
6359 pSMB->MaxParameterCount = cpu_to_le32(2);
6360 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
6361 pSMB->MaxSetupCount = 4;
6363 pSMB->ParameterOffset = 0;
6364 pSMB->DataCount = 0;
6365 pSMB->DataOffset = 0;
6366 pSMB->SetupCount = 4; /* single byte does not need le conversion */
6367 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_NOTIFY_CHANGE);
6368 pSMB->ParameterCount = pSMB->TotalParameterCount;
6370 pSMB->WatchTree = 1; /* one byte - no le conversion needed */
6371 pSMB->Reserved2 = 0;
6372 pSMB->CompletionFilter = cpu_to_le32(filter);
6373 pSMB->Fid = netfid; /* file handle always le */
6374 pSMB->ByteCount = 0;
6376 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6377 (struct smb_hdr *)pSMBr, &bytes_returned,
6380 cFYI(1, "Error in Notify = %d", rc);
6382 /* Add file to outstanding requests */
6383 /* BB change to kmem cache alloc */
6384 dnotify_req = kmalloc(
6385 sizeof(struct dir_notify_req),
6388 dnotify_req->Pid = pSMB->hdr.Pid;
6389 dnotify_req->PidHigh = pSMB->hdr.PidHigh;
6390 dnotify_req->Mid = pSMB->hdr.Mid;
6391 dnotify_req->Tid = pSMB->hdr.Tid;
6392 dnotify_req->Uid = pSMB->hdr.Uid;
6393 dnotify_req->netfid = netfid;
6394 dnotify_req->pfile = pfile;
6395 dnotify_req->filter = filter;
6396 dnotify_req->multishot = multishot;
6397 spin_lock(&GlobalMid_Lock);
6398 list_add_tail(&dnotify_req->lhead,
6399 &GlobalDnotifyReqList);
6400 spin_unlock(&GlobalMid_Lock);
6404 cifs_buf_release(pSMB);
6407 #endif /* was needed for dnotify, and will be needed for inotify when VFS fix */