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 */
90 * Mark as invalid, all open files on tree connections since they
91 * were closed when session to server was lost.
94 cifs_mark_open_files_invalid(struct cifs_tcon *tcon)
96 struct cifsFileInfo *open_file = NULL;
97 struct list_head *tmp;
98 struct list_head *tmp1;
100 /* list all files open on tree connection and mark them invalid */
101 spin_lock(&cifs_file_list_lock);
102 list_for_each_safe(tmp, tmp1, &tcon->openFileList) {
103 open_file = list_entry(tmp, struct cifsFileInfo, tlist);
104 open_file->invalidHandle = true;
105 open_file->oplock_break_cancelled = true;
107 spin_unlock(&cifs_file_list_lock);
109 * BB Add call to invalidate_inodes(sb) for all superblocks mounted
114 /* reconnect the socket, tcon, and smb session if needed */
116 cifs_reconnect_tcon(struct cifs_tcon *tcon, int smb_command)
119 struct cifs_ses *ses;
120 struct TCP_Server_Info *server;
121 struct nls_table *nls_codepage;
124 * SMBs NegProt, SessSetup, uLogoff do not have tcon yet so check for
125 * tcp and smb session status done differently for those three - in the
132 server = ses->server;
135 * only tree disconnect, open, and write, (and ulogoff which does not
136 * have tcon) are allowed as we start force umount
138 if (tcon->tidStatus == CifsExiting) {
139 if (smb_command != SMB_COM_WRITE_ANDX &&
140 smb_command != SMB_COM_OPEN_ANDX &&
141 smb_command != SMB_COM_TREE_DISCONNECT) {
142 cFYI(1, "can not send cmd %d while umounting",
149 * Give demultiplex thread up to 10 seconds to reconnect, should be
150 * greater than cifs socket timeout which is 7 seconds
152 while (server->tcpStatus == CifsNeedReconnect) {
153 wait_event_interruptible_timeout(server->response_q,
154 (server->tcpStatus != CifsNeedReconnect), 10 * HZ);
156 /* are we still trying to reconnect? */
157 if (server->tcpStatus != CifsNeedReconnect)
161 * on "soft" mounts we wait once. Hard mounts keep
162 * retrying until process is killed or server comes
166 cFYI(1, "gave up waiting on reconnect in smb_init");
171 if (!ses->need_reconnect && !tcon->need_reconnect)
174 nls_codepage = load_nls_default();
177 * need to prevent multiple threads trying to simultaneously
178 * reconnect the same SMB session
180 mutex_lock(&ses->session_mutex);
181 rc = cifs_negotiate_protocol(0, ses);
182 if (rc == 0 && ses->need_reconnect)
183 rc = cifs_setup_session(0, ses, nls_codepage);
185 /* do we need to reconnect tcon? */
186 if (rc || !tcon->need_reconnect) {
187 mutex_unlock(&ses->session_mutex);
191 cifs_mark_open_files_invalid(tcon);
192 rc = CIFSTCon(0, ses, tcon->treeName, tcon, nls_codepage);
193 mutex_unlock(&ses->session_mutex);
194 cFYI(1, "reconnect tcon rc = %d", rc);
200 * FIXME: check if wsize needs updated due to negotiated smb buffer
203 atomic_inc(&tconInfoReconnectCount);
205 /* tell server Unix caps we support */
206 if (ses->capabilities & CAP_UNIX)
207 reset_cifs_unix_caps(0, tcon, NULL, NULL);
210 * Removed call to reopen open files here. It is safer (and faster) to
211 * reopen files one at a time as needed in read and write.
213 * FIXME: what about file locks? don't we need to reclaim them ASAP?
218 * Check if handle based operation so we know whether we can continue
219 * or not without returning to caller to reset file handle
221 switch (smb_command) {
222 case SMB_COM_READ_ANDX:
223 case SMB_COM_WRITE_ANDX:
225 case SMB_COM_FIND_CLOSE2:
226 case SMB_COM_LOCKING_ANDX:
230 unload_nls(nls_codepage);
234 /* Allocate and return pointer to an SMB request buffer, and set basic
235 SMB information in the SMB header. If the return code is zero, this
236 function must have filled in request_buf pointer */
238 small_smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
243 rc = cifs_reconnect_tcon(tcon, smb_command);
247 *request_buf = cifs_small_buf_get();
248 if (*request_buf == NULL) {
249 /* BB should we add a retry in here if not a writepage? */
253 header_assemble((struct smb_hdr *) *request_buf, smb_command,
257 cifs_stats_inc(&tcon->num_smbs_sent);
263 small_smb_init_no_tc(const int smb_command, const int wct,
264 struct cifs_ses *ses, void **request_buf)
267 struct smb_hdr *buffer;
269 rc = small_smb_init(smb_command, wct, NULL, request_buf);
273 buffer = (struct smb_hdr *)*request_buf;
274 buffer->Mid = get_next_mid(ses->server);
275 if (ses->capabilities & CAP_UNICODE)
276 buffer->Flags2 |= SMBFLG2_UNICODE;
277 if (ses->capabilities & CAP_STATUS32)
278 buffer->Flags2 |= SMBFLG2_ERR_STATUS;
280 /* uid, tid can stay at zero as set in header assemble */
282 /* BB add support for turning on the signing when
283 this function is used after 1st of session setup requests */
288 /* If the return code is zero, this function must fill in request_buf pointer */
290 __smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
291 void **request_buf, void **response_buf)
293 *request_buf = cifs_buf_get();
294 if (*request_buf == NULL) {
295 /* BB should we add a retry in here if not a writepage? */
298 /* Although the original thought was we needed the response buf for */
299 /* potential retries of smb operations it turns out we can determine */
300 /* from the mid flags when the request buffer can be resent without */
301 /* having to use a second distinct buffer for the response */
303 *response_buf = *request_buf;
305 header_assemble((struct smb_hdr *) *request_buf, smb_command, tcon,
309 cifs_stats_inc(&tcon->num_smbs_sent);
314 /* If the return code is zero, this function must fill in request_buf pointer */
316 smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
317 void **request_buf, void **response_buf)
321 rc = cifs_reconnect_tcon(tcon, smb_command);
325 return __smb_init(smb_command, wct, tcon, request_buf, response_buf);
329 smb_init_no_reconnect(int smb_command, int wct, struct cifs_tcon *tcon,
330 void **request_buf, void **response_buf)
332 if (tcon->ses->need_reconnect || tcon->need_reconnect)
335 return __smb_init(smb_command, wct, tcon, request_buf, response_buf);
338 static int validate_t2(struct smb_t2_rsp *pSMB)
340 unsigned int total_size;
342 /* check for plausible wct */
343 if (pSMB->hdr.WordCount < 10)
346 /* check for parm and data offset going beyond end of smb */
347 if (get_unaligned_le16(&pSMB->t2_rsp.ParameterOffset) > 1024 ||
348 get_unaligned_le16(&pSMB->t2_rsp.DataOffset) > 1024)
351 total_size = get_unaligned_le16(&pSMB->t2_rsp.ParameterCount);
352 if (total_size >= 512)
355 /* check that bcc is at least as big as parms + data, and that it is
356 * less than negotiated smb buffer
358 total_size += get_unaligned_le16(&pSMB->t2_rsp.DataCount);
359 if (total_size > get_bcc(&pSMB->hdr) ||
360 total_size >= CIFSMaxBufSize + MAX_CIFS_HDR_SIZE)
365 cifs_dump_mem("Invalid transact2 SMB: ", (char *)pSMB,
366 sizeof(struct smb_t2_rsp) + 16);
371 CIFSSMBNegotiate(const unsigned int xid, struct cifs_ses *ses)
374 NEGOTIATE_RSP *pSMBr;
378 struct TCP_Server_Info *server;
380 unsigned int secFlags;
383 server = ses->server;
388 rc = smb_init(SMB_COM_NEGOTIATE, 0, NULL /* no tcon yet */ ,
389 (void **) &pSMB, (void **) &pSMBr);
393 /* if any of auth flags (ie not sign or seal) are overriden use them */
394 if (ses->overrideSecFlg & (~(CIFSSEC_MUST_SIGN | CIFSSEC_MUST_SEAL)))
395 secFlags = ses->overrideSecFlg; /* BB FIXME fix sign flags? */
396 else /* if override flags set only sign/seal OR them with global auth */
397 secFlags = global_secflags | ses->overrideSecFlg;
399 cFYI(1, "secFlags 0x%x", secFlags);
401 pSMB->hdr.Mid = get_next_mid(server);
402 pSMB->hdr.Flags2 |= (SMBFLG2_UNICODE | SMBFLG2_ERR_STATUS);
404 if ((secFlags & CIFSSEC_MUST_KRB5) == CIFSSEC_MUST_KRB5)
405 pSMB->hdr.Flags2 |= SMBFLG2_EXT_SEC;
406 else if ((secFlags & CIFSSEC_AUTH_MASK) == CIFSSEC_MAY_KRB5) {
407 cFYI(1, "Kerberos only mechanism, enable extended security");
408 pSMB->hdr.Flags2 |= SMBFLG2_EXT_SEC;
409 } else if ((secFlags & CIFSSEC_MUST_NTLMSSP) == CIFSSEC_MUST_NTLMSSP)
410 pSMB->hdr.Flags2 |= SMBFLG2_EXT_SEC;
411 else if ((secFlags & CIFSSEC_AUTH_MASK) == CIFSSEC_MAY_NTLMSSP) {
412 cFYI(1, "NTLMSSP only mechanism, enable extended security");
413 pSMB->hdr.Flags2 |= SMBFLG2_EXT_SEC;
417 for (i = 0; i < CIFS_NUM_PROT; i++) {
418 strncpy(pSMB->DialectsArray+count, protocols[i].name, 16);
419 count += strlen(protocols[i].name) + 1;
420 /* null at end of source and target buffers anyway */
422 inc_rfc1001_len(pSMB, count);
423 pSMB->ByteCount = cpu_to_le16(count);
425 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
426 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
430 server->dialect = le16_to_cpu(pSMBr->DialectIndex);
431 cFYI(1, "Dialect: %d", server->dialect);
432 /* Check wct = 1 error case */
433 if ((pSMBr->hdr.WordCount < 13) || (server->dialect == BAD_PROT)) {
434 /* core returns wct = 1, but we do not ask for core - otherwise
435 small wct just comes when dialect index is -1 indicating we
436 could not negotiate a common dialect */
439 #ifdef CONFIG_CIFS_WEAK_PW_HASH
440 } else if ((pSMBr->hdr.WordCount == 13)
441 && ((server->dialect == LANMAN_PROT)
442 || (server->dialect == LANMAN2_PROT))) {
444 struct lanman_neg_rsp *rsp = (struct lanman_neg_rsp *)pSMBr;
446 if ((secFlags & CIFSSEC_MAY_LANMAN) ||
447 (secFlags & CIFSSEC_MAY_PLNTXT))
448 server->secType = LANMAN;
450 cERROR(1, "mount failed weak security disabled"
451 " in /proc/fs/cifs/SecurityFlags");
455 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
456 server->maxReq = min_t(unsigned int,
457 le16_to_cpu(rsp->MaxMpxCount),
459 set_credits(server, server->maxReq);
460 server->maxBuf = le16_to_cpu(rsp->MaxBufSize);
461 server->max_vcs = le16_to_cpu(rsp->MaxNumberVcs);
462 /* even though we do not use raw we might as well set this
463 accurately, in case we ever find a need for it */
464 if ((le16_to_cpu(rsp->RawMode) & RAW_ENABLE) == RAW_ENABLE) {
465 server->max_rw = 0xFF00;
466 server->capabilities = CAP_MPX_MODE | CAP_RAW_MODE;
468 server->max_rw = 0;/* do not need to use raw anyway */
469 server->capabilities = CAP_MPX_MODE;
471 tmp = (__s16)le16_to_cpu(rsp->ServerTimeZone);
473 /* OS/2 often does not set timezone therefore
474 * we must use server time to calc time zone.
475 * Could deviate slightly from the right zone.
476 * Smallest defined timezone difference is 15 minutes
477 * (i.e. Nepal). Rounding up/down is done to match
480 int val, seconds, remain, result;
481 struct timespec ts, utc;
483 ts = cnvrtDosUnixTm(rsp->SrvTime.Date,
484 rsp->SrvTime.Time, 0);
485 cFYI(1, "SrvTime %d sec since 1970 (utc: %d) diff: %d",
486 (int)ts.tv_sec, (int)utc.tv_sec,
487 (int)(utc.tv_sec - ts.tv_sec));
488 val = (int)(utc.tv_sec - ts.tv_sec);
490 result = (seconds / MIN_TZ_ADJ) * MIN_TZ_ADJ;
491 remain = seconds % MIN_TZ_ADJ;
492 if (remain >= (MIN_TZ_ADJ / 2))
493 result += MIN_TZ_ADJ;
496 server->timeAdj = result;
498 server->timeAdj = (int)tmp;
499 server->timeAdj *= 60; /* also in seconds */
501 cFYI(1, "server->timeAdj: %d seconds", server->timeAdj);
504 /* BB get server time for time conversions and add
505 code to use it and timezone since this is not UTC */
507 if (rsp->EncryptionKeyLength ==
508 cpu_to_le16(CIFS_CRYPTO_KEY_SIZE)) {
509 memcpy(ses->server->cryptkey, rsp->EncryptionKey,
510 CIFS_CRYPTO_KEY_SIZE);
511 } else if (server->sec_mode & SECMODE_PW_ENCRYPT) {
512 rc = -EIO; /* need cryptkey unless plain text */
516 cFYI(1, "LANMAN negotiated");
517 /* we will not end up setting signing flags - as no signing
518 was in LANMAN and server did not return the flags on */
520 #else /* weak security disabled */
521 } else if (pSMBr->hdr.WordCount == 13) {
522 cERROR(1, "mount failed, cifs module not built "
523 "with CIFS_WEAK_PW_HASH support");
525 #endif /* WEAK_PW_HASH */
527 } else if (pSMBr->hdr.WordCount != 17) {
532 /* else wct == 17 NTLM */
533 server->sec_mode = pSMBr->SecurityMode;
534 if ((server->sec_mode & SECMODE_USER) == 0)
535 cFYI(1, "share mode security");
537 if ((server->sec_mode & SECMODE_PW_ENCRYPT) == 0)
538 #ifdef CONFIG_CIFS_WEAK_PW_HASH
539 if ((secFlags & CIFSSEC_MAY_PLNTXT) == 0)
540 #endif /* CIFS_WEAK_PW_HASH */
541 cERROR(1, "Server requests plain text password"
542 " but client support disabled");
544 if ((secFlags & CIFSSEC_MUST_NTLMV2) == CIFSSEC_MUST_NTLMV2)
545 server->secType = NTLMv2;
546 else if (secFlags & CIFSSEC_MAY_NTLM)
547 server->secType = NTLM;
548 else if (secFlags & CIFSSEC_MAY_NTLMV2)
549 server->secType = NTLMv2;
550 else if (secFlags & CIFSSEC_MAY_KRB5)
551 server->secType = Kerberos;
552 else if (secFlags & CIFSSEC_MAY_NTLMSSP)
553 server->secType = RawNTLMSSP;
554 else if (secFlags & CIFSSEC_MAY_LANMAN)
555 server->secType = LANMAN;
558 cERROR(1, "Invalid security type");
561 /* else ... any others ...? */
563 /* one byte, so no need to convert this or EncryptionKeyLen from
565 server->maxReq = min_t(unsigned int, le16_to_cpu(pSMBr->MaxMpxCount),
567 set_credits(server, server->maxReq);
568 /* probably no need to store and check maxvcs */
569 server->maxBuf = le32_to_cpu(pSMBr->MaxBufferSize);
570 server->max_rw = le32_to_cpu(pSMBr->MaxRawSize);
571 cFYI(DBG2, "Max buf = %d", ses->server->maxBuf);
572 server->capabilities = le32_to_cpu(pSMBr->Capabilities);
573 server->timeAdj = (int)(__s16)le16_to_cpu(pSMBr->ServerTimeZone);
574 server->timeAdj *= 60;
575 if (pSMBr->EncryptionKeyLength == CIFS_CRYPTO_KEY_SIZE) {
576 memcpy(ses->server->cryptkey, pSMBr->u.EncryptionKey,
577 CIFS_CRYPTO_KEY_SIZE);
578 } else if ((pSMBr->hdr.Flags2 & SMBFLG2_EXT_SEC ||
579 server->capabilities & CAP_EXTENDED_SECURITY) &&
580 (pSMBr->EncryptionKeyLength == 0)) {
581 /* decode security blob */
582 count = get_bcc(&pSMBr->hdr);
587 spin_lock(&cifs_tcp_ses_lock);
588 if (server->srv_count > 1) {
589 spin_unlock(&cifs_tcp_ses_lock);
590 if (memcmp(server->server_GUID,
591 pSMBr->u.extended_response.
593 cFYI(1, "server UID changed");
594 memcpy(server->server_GUID,
595 pSMBr->u.extended_response.GUID,
599 spin_unlock(&cifs_tcp_ses_lock);
600 memcpy(server->server_GUID,
601 pSMBr->u.extended_response.GUID, 16);
605 server->secType = RawNTLMSSP;
607 rc = decode_negTokenInit(pSMBr->u.extended_response.
608 SecurityBlob, count - 16,
614 if (server->secType == Kerberos) {
615 if (!server->sec_kerberos &&
616 !server->sec_mskerberos)
618 } else if (server->secType == RawNTLMSSP) {
619 if (!server->sec_ntlmssp)
624 } else if (server->sec_mode & SECMODE_PW_ENCRYPT) {
625 rc = -EIO; /* no crypt key only if plain text pwd */
628 server->capabilities &= ~CAP_EXTENDED_SECURITY;
630 #ifdef CONFIG_CIFS_WEAK_PW_HASH
633 if ((secFlags & CIFSSEC_MAY_SIGN) == 0) {
634 /* MUST_SIGN already includes the MAY_SIGN FLAG
635 so if this is zero it means that signing is disabled */
636 cFYI(1, "Signing disabled");
637 if (server->sec_mode & SECMODE_SIGN_REQUIRED) {
638 cERROR(1, "Server requires "
639 "packet signing to be enabled in "
640 "/proc/fs/cifs/SecurityFlags.");
644 ~(SECMODE_SIGN_ENABLED | SECMODE_SIGN_REQUIRED);
645 } else if ((secFlags & CIFSSEC_MUST_SIGN) == CIFSSEC_MUST_SIGN) {
646 /* signing required */
647 cFYI(1, "Must sign - secFlags 0x%x", secFlags);
648 if ((server->sec_mode &
649 (SECMODE_SIGN_ENABLED | SECMODE_SIGN_REQUIRED)) == 0) {
650 cERROR(1, "signing required but server lacks support");
653 server->sec_mode |= SECMODE_SIGN_REQUIRED;
655 /* signing optional ie CIFSSEC_MAY_SIGN */
656 if ((server->sec_mode & SECMODE_SIGN_REQUIRED) == 0)
658 ~(SECMODE_SIGN_ENABLED | SECMODE_SIGN_REQUIRED);
662 cifs_buf_release(pSMB);
664 cFYI(1, "negprot rc %d", rc);
669 CIFSSMBTDis(const unsigned int xid, struct cifs_tcon *tcon)
671 struct smb_hdr *smb_buffer;
674 cFYI(1, "In tree disconnect");
676 /* BB: do we need to check this? These should never be NULL. */
677 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
681 * No need to return error on this operation if tid invalidated and
682 * closed on server already e.g. due to tcp session crashing. Also,
683 * the tcon is no longer on the list, so no need to take lock before
686 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
689 rc = small_smb_init(SMB_COM_TREE_DISCONNECT, 0, tcon,
690 (void **)&smb_buffer);
694 rc = SendReceiveNoRsp(xid, tcon->ses, (char *)smb_buffer, 0);
696 cFYI(1, "Tree disconnect failed %d", rc);
698 /* No need to return error on this operation if tid invalidated and
699 closed on server already e.g. due to tcp session crashing */
707 * This is a no-op for now. We're not really interested in the reply, but
708 * rather in the fact that the server sent one and that server->lstrp
711 * FIXME: maybe we should consider checking that the reply matches request?
714 cifs_echo_callback(struct mid_q_entry *mid)
716 struct TCP_Server_Info *server = mid->callback_data;
718 DeleteMidQEntry(mid);
719 add_credits(server, 1, CIFS_ECHO_OP);
723 CIFSSMBEcho(struct TCP_Server_Info *server)
728 struct smb_rqst rqst = { .rq_iov = &iov,
731 cFYI(1, "In echo request");
733 rc = small_smb_init(SMB_COM_ECHO, 0, NULL, (void **)&smb);
737 /* set up echo request */
738 smb->hdr.Tid = 0xffff;
739 smb->hdr.WordCount = 1;
740 put_unaligned_le16(1, &smb->EchoCount);
741 put_bcc(1, &smb->hdr);
743 inc_rfc1001_len(smb, 3);
745 iov.iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4;
747 rc = cifs_call_async(server, &rqst, NULL, cifs_echo_callback,
748 server, CIFS_ASYNC_OP | CIFS_ECHO_OP);
750 cFYI(1, "Echo request failed: %d", rc);
752 cifs_small_buf_release(smb);
758 CIFSSMBLogoff(const unsigned int xid, struct cifs_ses *ses)
760 LOGOFF_ANDX_REQ *pSMB;
763 cFYI(1, "In SMBLogoff for session disconnect");
766 * BB: do we need to check validity of ses and server? They should
767 * always be valid since we have an active reference. If not, that
768 * should probably be a BUG()
770 if (!ses || !ses->server)
773 mutex_lock(&ses->session_mutex);
774 if (ses->need_reconnect)
775 goto session_already_dead; /* no need to send SMBlogoff if uid
776 already closed due to reconnect */
777 rc = small_smb_init(SMB_COM_LOGOFF_ANDX, 2, NULL, (void **)&pSMB);
779 mutex_unlock(&ses->session_mutex);
783 pSMB->hdr.Mid = get_next_mid(ses->server);
785 if (ses->server->sec_mode &
786 (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
787 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
789 pSMB->hdr.Uid = ses->Suid;
791 pSMB->AndXCommand = 0xFF;
792 rc = SendReceiveNoRsp(xid, ses, (char *) pSMB, 0);
793 session_already_dead:
794 mutex_unlock(&ses->session_mutex);
796 /* if session dead then we do not need to do ulogoff,
797 since server closed smb session, no sense reporting
805 CIFSPOSIXDelFile(const unsigned int xid, struct cifs_tcon *tcon,
806 const char *fileName, __u16 type,
807 const struct nls_table *nls_codepage, int remap)
809 TRANSACTION2_SPI_REQ *pSMB = NULL;
810 TRANSACTION2_SPI_RSP *pSMBr = NULL;
811 struct unlink_psx_rq *pRqD;
814 int bytes_returned = 0;
815 __u16 params, param_offset, offset, byte_count;
817 cFYI(1, "In POSIX delete");
819 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
824 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
826 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
827 PATH_MAX, nls_codepage, remap);
828 name_len++; /* trailing null */
830 } else { /* BB add path length overrun check */
831 name_len = strnlen(fileName, PATH_MAX);
832 name_len++; /* trailing null */
833 strncpy(pSMB->FileName, fileName, name_len);
836 params = 6 + name_len;
837 pSMB->MaxParameterCount = cpu_to_le16(2);
838 pSMB->MaxDataCount = 0; /* BB double check this with jra */
839 pSMB->MaxSetupCount = 0;
844 param_offset = offsetof(struct smb_com_transaction2_spi_req,
845 InformationLevel) - 4;
846 offset = param_offset + params;
848 /* Setup pointer to Request Data (inode type) */
849 pRqD = (struct unlink_psx_rq *)(((char *)&pSMB->hdr.Protocol) + offset);
850 pRqD->type = cpu_to_le16(type);
851 pSMB->ParameterOffset = cpu_to_le16(param_offset);
852 pSMB->DataOffset = cpu_to_le16(offset);
853 pSMB->SetupCount = 1;
855 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
856 byte_count = 3 /* pad */ + params + sizeof(struct unlink_psx_rq);
858 pSMB->DataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
859 pSMB->TotalDataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
860 pSMB->ParameterCount = cpu_to_le16(params);
861 pSMB->TotalParameterCount = pSMB->ParameterCount;
862 pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_UNLINK);
864 inc_rfc1001_len(pSMB, byte_count);
865 pSMB->ByteCount = cpu_to_le16(byte_count);
866 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
867 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
869 cFYI(1, "Posix delete returned %d", rc);
870 cifs_buf_release(pSMB);
872 cifs_stats_inc(&tcon->stats.cifs_stats.num_deletes);
881 CIFSSMBDelFile(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
882 struct cifs_sb_info *cifs_sb)
884 DELETE_FILE_REQ *pSMB = NULL;
885 DELETE_FILE_RSP *pSMBr = NULL;
889 int remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR;
892 rc = smb_init(SMB_COM_DELETE, 1, tcon, (void **) &pSMB,
897 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
898 name_len = cifsConvertToUTF16((__le16 *) pSMB->fileName, name,
899 PATH_MAX, cifs_sb->local_nls,
901 name_len++; /* trailing null */
903 } else { /* BB improve check for buffer overruns BB */
904 name_len = strnlen(name, PATH_MAX);
905 name_len++; /* trailing null */
906 strncpy(pSMB->fileName, name, name_len);
908 pSMB->SearchAttributes =
909 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM);
910 pSMB->BufferFormat = 0x04;
911 inc_rfc1001_len(pSMB, name_len + 1);
912 pSMB->ByteCount = cpu_to_le16(name_len + 1);
913 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
914 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
915 cifs_stats_inc(&tcon->stats.cifs_stats.num_deletes);
917 cFYI(1, "Error in RMFile = %d", rc);
919 cifs_buf_release(pSMB);
927 CIFSSMBRmDir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
928 struct cifs_sb_info *cifs_sb)
930 DELETE_DIRECTORY_REQ *pSMB = NULL;
931 DELETE_DIRECTORY_RSP *pSMBr = NULL;
935 int remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR;
937 cFYI(1, "In CIFSSMBRmDir");
939 rc = smb_init(SMB_COM_DELETE_DIRECTORY, 0, tcon, (void **) &pSMB,
944 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
945 name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, name,
946 PATH_MAX, cifs_sb->local_nls,
948 name_len++; /* trailing null */
950 } else { /* BB improve check for buffer overruns BB */
951 name_len = strnlen(name, PATH_MAX);
952 name_len++; /* trailing null */
953 strncpy(pSMB->DirName, name, name_len);
956 pSMB->BufferFormat = 0x04;
957 inc_rfc1001_len(pSMB, name_len + 1);
958 pSMB->ByteCount = cpu_to_le16(name_len + 1);
959 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
960 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
961 cifs_stats_inc(&tcon->stats.cifs_stats.num_rmdirs);
963 cFYI(1, "Error in RMDir = %d", rc);
965 cifs_buf_release(pSMB);
972 CIFSSMBMkDir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
973 struct cifs_sb_info *cifs_sb)
976 CREATE_DIRECTORY_REQ *pSMB = NULL;
977 CREATE_DIRECTORY_RSP *pSMBr = NULL;
980 int remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR;
982 cFYI(1, "In CIFSSMBMkDir");
984 rc = smb_init(SMB_COM_CREATE_DIRECTORY, 0, tcon, (void **) &pSMB,
989 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
990 name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, name,
991 PATH_MAX, cifs_sb->local_nls,
993 name_len++; /* trailing null */
995 } else { /* BB improve check for buffer overruns BB */
996 name_len = strnlen(name, PATH_MAX);
997 name_len++; /* trailing null */
998 strncpy(pSMB->DirName, name, name_len);
1001 pSMB->BufferFormat = 0x04;
1002 inc_rfc1001_len(pSMB, name_len + 1);
1003 pSMB->ByteCount = cpu_to_le16(name_len + 1);
1004 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1005 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
1006 cifs_stats_inc(&tcon->stats.cifs_stats.num_mkdirs);
1008 cFYI(1, "Error in Mkdir = %d", rc);
1010 cifs_buf_release(pSMB);
1017 CIFSPOSIXCreate(const unsigned int xid, struct cifs_tcon *tcon,
1018 __u32 posix_flags, __u64 mode, __u16 *netfid,
1019 FILE_UNIX_BASIC_INFO *pRetData, __u32 *pOplock,
1020 const char *name, const struct nls_table *nls_codepage,
1023 TRANSACTION2_SPI_REQ *pSMB = NULL;
1024 TRANSACTION2_SPI_RSP *pSMBr = NULL;
1027 int bytes_returned = 0;
1028 __u16 params, param_offset, offset, byte_count, count;
1029 OPEN_PSX_REQ *pdata;
1030 OPEN_PSX_RSP *psx_rsp;
1032 cFYI(1, "In POSIX Create");
1034 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
1039 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1041 cifsConvertToUTF16((__le16 *) pSMB->FileName, name,
1042 PATH_MAX, nls_codepage, remap);
1043 name_len++; /* trailing null */
1045 } else { /* BB improve the check for buffer overruns BB */
1046 name_len = strnlen(name, PATH_MAX);
1047 name_len++; /* trailing null */
1048 strncpy(pSMB->FileName, name, name_len);
1051 params = 6 + name_len;
1052 count = sizeof(OPEN_PSX_REQ);
1053 pSMB->MaxParameterCount = cpu_to_le16(2);
1054 pSMB->MaxDataCount = cpu_to_le16(1000); /* large enough */
1055 pSMB->MaxSetupCount = 0;
1059 pSMB->Reserved2 = 0;
1060 param_offset = offsetof(struct smb_com_transaction2_spi_req,
1061 InformationLevel) - 4;
1062 offset = param_offset + params;
1063 pdata = (OPEN_PSX_REQ *)(((char *)&pSMB->hdr.Protocol) + offset);
1064 pdata->Level = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
1065 pdata->Permissions = cpu_to_le64(mode);
1066 pdata->PosixOpenFlags = cpu_to_le32(posix_flags);
1067 pdata->OpenFlags = cpu_to_le32(*pOplock);
1068 pSMB->ParameterOffset = cpu_to_le16(param_offset);
1069 pSMB->DataOffset = cpu_to_le16(offset);
1070 pSMB->SetupCount = 1;
1071 pSMB->Reserved3 = 0;
1072 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
1073 byte_count = 3 /* pad */ + params + count;
1075 pSMB->DataCount = cpu_to_le16(count);
1076 pSMB->ParameterCount = cpu_to_le16(params);
1077 pSMB->TotalDataCount = pSMB->DataCount;
1078 pSMB->TotalParameterCount = pSMB->ParameterCount;
1079 pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_OPEN);
1080 pSMB->Reserved4 = 0;
1081 inc_rfc1001_len(pSMB, byte_count);
1082 pSMB->ByteCount = cpu_to_le16(byte_count);
1083 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1084 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
1086 cFYI(1, "Posix create returned %d", rc);
1087 goto psx_create_err;
1090 cFYI(1, "copying inode info");
1091 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
1093 if (rc || get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)) {
1094 rc = -EIO; /* bad smb */
1095 goto psx_create_err;
1098 /* copy return information to pRetData */
1099 psx_rsp = (OPEN_PSX_RSP *)((char *) &pSMBr->hdr.Protocol
1100 + le16_to_cpu(pSMBr->t2.DataOffset));
1102 *pOplock = le16_to_cpu(psx_rsp->OplockFlags);
1104 *netfid = psx_rsp->Fid; /* cifs fid stays in le */
1105 /* Let caller know file was created so we can set the mode. */
1106 /* Do we care about the CreateAction in any other cases? */
1107 if (cpu_to_le32(FILE_CREATE) == psx_rsp->CreateAction)
1108 *pOplock |= CIFS_CREATE_ACTION;
1109 /* check to make sure response data is there */
1110 if (psx_rsp->ReturnedLevel != cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC)) {
1111 pRetData->Type = cpu_to_le32(-1); /* unknown */
1112 cFYI(DBG2, "unknown type");
1114 if (get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)
1115 + sizeof(FILE_UNIX_BASIC_INFO)) {
1116 cERROR(1, "Open response data too small");
1117 pRetData->Type = cpu_to_le32(-1);
1118 goto psx_create_err;
1120 memcpy((char *) pRetData,
1121 (char *)psx_rsp + sizeof(OPEN_PSX_RSP),
1122 sizeof(FILE_UNIX_BASIC_INFO));
1126 cifs_buf_release(pSMB);
1128 if (posix_flags & SMB_O_DIRECTORY)
1129 cifs_stats_inc(&tcon->stats.cifs_stats.num_posixmkdirs);
1131 cifs_stats_inc(&tcon->stats.cifs_stats.num_posixopens);
1139 static __u16 convert_disposition(int disposition)
1143 switch (disposition) {
1144 case FILE_SUPERSEDE:
1145 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1148 ofun = SMBOPEN_OAPPEND;
1151 ofun = SMBOPEN_OCREATE;
1154 ofun = SMBOPEN_OCREATE | SMBOPEN_OAPPEND;
1156 case FILE_OVERWRITE:
1157 ofun = SMBOPEN_OTRUNC;
1159 case FILE_OVERWRITE_IF:
1160 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1163 cFYI(1, "unknown disposition %d", disposition);
1164 ofun = SMBOPEN_OAPPEND; /* regular open */
1170 access_flags_to_smbopen_mode(const int access_flags)
1172 int masked_flags = access_flags & (GENERIC_READ | GENERIC_WRITE);
1174 if (masked_flags == GENERIC_READ)
1175 return SMBOPEN_READ;
1176 else if (masked_flags == GENERIC_WRITE)
1177 return SMBOPEN_WRITE;
1179 /* just go for read/write */
1180 return SMBOPEN_READWRITE;
1184 SMBLegacyOpen(const unsigned int xid, struct cifs_tcon *tcon,
1185 const char *fileName, const int openDisposition,
1186 const int access_flags, const int create_options, __u16 *netfid,
1187 int *pOplock, FILE_ALL_INFO *pfile_info,
1188 const struct nls_table *nls_codepage, int remap)
1191 OPENX_REQ *pSMB = NULL;
1192 OPENX_RSP *pSMBr = NULL;
1198 rc = smb_init(SMB_COM_OPEN_ANDX, 15, tcon, (void **) &pSMB,
1203 pSMB->AndXCommand = 0xFF; /* none */
1205 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1206 count = 1; /* account for one byte pad to word boundary */
1208 cifsConvertToUTF16((__le16 *) (pSMB->fileName + 1),
1209 fileName, PATH_MAX, nls_codepage, remap);
1210 name_len++; /* trailing null */
1212 } else { /* BB improve check for buffer overruns BB */
1213 count = 0; /* no pad */
1214 name_len = strnlen(fileName, PATH_MAX);
1215 name_len++; /* trailing null */
1216 strncpy(pSMB->fileName, fileName, name_len);
1218 if (*pOplock & REQ_OPLOCK)
1219 pSMB->OpenFlags = cpu_to_le16(REQ_OPLOCK);
1220 else if (*pOplock & REQ_BATCHOPLOCK)
1221 pSMB->OpenFlags = cpu_to_le16(REQ_BATCHOPLOCK);
1223 pSMB->OpenFlags |= cpu_to_le16(REQ_MORE_INFO);
1224 pSMB->Mode = cpu_to_le16(access_flags_to_smbopen_mode(access_flags));
1225 pSMB->Mode |= cpu_to_le16(0x40); /* deny none */
1226 /* set file as system file if special file such
1227 as fifo and server expecting SFU style and
1228 no Unix extensions */
1230 if (create_options & CREATE_OPTION_SPECIAL)
1231 pSMB->FileAttributes = cpu_to_le16(ATTR_SYSTEM);
1232 else /* BB FIXME BB */
1233 pSMB->FileAttributes = cpu_to_le16(0/*ATTR_NORMAL*/);
1235 if (create_options & CREATE_OPTION_READONLY)
1236 pSMB->FileAttributes |= cpu_to_le16(ATTR_READONLY);
1239 /* pSMB->CreateOptions = cpu_to_le32(create_options &
1240 CREATE_OPTIONS_MASK); */
1241 /* BB FIXME END BB */
1243 pSMB->Sattr = cpu_to_le16(ATTR_HIDDEN | ATTR_SYSTEM | ATTR_DIRECTORY);
1244 pSMB->OpenFunction = cpu_to_le16(convert_disposition(openDisposition));
1246 inc_rfc1001_len(pSMB, count);
1248 pSMB->ByteCount = cpu_to_le16(count);
1249 /* long_op set to 1 to allow for oplock break timeouts */
1250 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1251 (struct smb_hdr *)pSMBr, &bytes_returned, 0);
1252 cifs_stats_inc(&tcon->stats.cifs_stats.num_opens);
1254 cFYI(1, "Error in Open = %d", rc);
1256 /* BB verify if wct == 15 */
1258 /* *pOplock = pSMBr->OplockLevel; */ /* BB take from action field*/
1260 *netfid = pSMBr->Fid; /* cifs fid stays in le */
1261 /* Let caller know file was created so we can set the mode. */
1262 /* Do we care about the CreateAction in any other cases? */
1264 /* if (cpu_to_le32(FILE_CREATE) == pSMBr->CreateAction)
1265 *pOplock |= CIFS_CREATE_ACTION; */
1269 pfile_info->CreationTime = 0; /* BB convert CreateTime*/
1270 pfile_info->LastAccessTime = 0; /* BB fixme */
1271 pfile_info->LastWriteTime = 0; /* BB fixme */
1272 pfile_info->ChangeTime = 0; /* BB fixme */
1273 pfile_info->Attributes =
1274 cpu_to_le32(le16_to_cpu(pSMBr->FileAttributes));
1275 /* the file_info buf is endian converted by caller */
1276 pfile_info->AllocationSize =
1277 cpu_to_le64(le32_to_cpu(pSMBr->EndOfFile));
1278 pfile_info->EndOfFile = pfile_info->AllocationSize;
1279 pfile_info->NumberOfLinks = cpu_to_le32(1);
1280 pfile_info->DeletePending = 0;
1284 cifs_buf_release(pSMB);
1291 CIFSSMBOpen(const unsigned int xid, struct cifs_tcon *tcon,
1292 const char *fileName, const int openDisposition,
1293 const int access_flags, const int create_options, __u16 *netfid,
1294 int *pOplock, FILE_ALL_INFO *pfile_info,
1295 const struct nls_table *nls_codepage, int remap)
1298 OPEN_REQ *pSMB = NULL;
1299 OPEN_RSP *pSMBr = NULL;
1305 rc = smb_init(SMB_COM_NT_CREATE_ANDX, 24, tcon, (void **) &pSMB,
1310 pSMB->AndXCommand = 0xFF; /* none */
1312 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1313 count = 1; /* account for one byte pad to word boundary */
1315 cifsConvertToUTF16((__le16 *) (pSMB->fileName + 1),
1316 fileName, PATH_MAX, nls_codepage, remap);
1317 name_len++; /* trailing null */
1319 pSMB->NameLength = cpu_to_le16(name_len);
1320 } else { /* BB improve check for buffer overruns BB */
1321 count = 0; /* no pad */
1322 name_len = strnlen(fileName, PATH_MAX);
1323 name_len++; /* trailing null */
1324 pSMB->NameLength = cpu_to_le16(name_len);
1325 strncpy(pSMB->fileName, fileName, name_len);
1327 if (*pOplock & REQ_OPLOCK)
1328 pSMB->OpenFlags = cpu_to_le32(REQ_OPLOCK);
1329 else if (*pOplock & REQ_BATCHOPLOCK)
1330 pSMB->OpenFlags = cpu_to_le32(REQ_BATCHOPLOCK);
1331 pSMB->DesiredAccess = cpu_to_le32(access_flags);
1332 pSMB->AllocationSize = 0;
1333 /* set file as system file if special file such
1334 as fifo and server expecting SFU style and
1335 no Unix extensions */
1336 if (create_options & CREATE_OPTION_SPECIAL)
1337 pSMB->FileAttributes = cpu_to_le32(ATTR_SYSTEM);
1339 pSMB->FileAttributes = cpu_to_le32(ATTR_NORMAL);
1341 /* XP does not handle ATTR_POSIX_SEMANTICS */
1342 /* but it helps speed up case sensitive checks for other
1343 servers such as Samba */
1344 if (tcon->ses->capabilities & CAP_UNIX)
1345 pSMB->FileAttributes |= cpu_to_le32(ATTR_POSIX_SEMANTICS);
1347 if (create_options & CREATE_OPTION_READONLY)
1348 pSMB->FileAttributes |= cpu_to_le32(ATTR_READONLY);
1350 pSMB->ShareAccess = cpu_to_le32(FILE_SHARE_ALL);
1351 pSMB->CreateDisposition = cpu_to_le32(openDisposition);
1352 pSMB->CreateOptions = cpu_to_le32(create_options & CREATE_OPTIONS_MASK);
1353 /* BB Expirement with various impersonation levels and verify */
1354 pSMB->ImpersonationLevel = cpu_to_le32(SECURITY_IMPERSONATION);
1355 pSMB->SecurityFlags =
1356 SECURITY_CONTEXT_TRACKING | SECURITY_EFFECTIVE_ONLY;
1359 inc_rfc1001_len(pSMB, count);
1361 pSMB->ByteCount = cpu_to_le16(count);
1362 /* long_op set to 1 to allow for oplock break timeouts */
1363 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1364 (struct smb_hdr *)pSMBr, &bytes_returned, 0);
1365 cifs_stats_inc(&tcon->stats.cifs_stats.num_opens);
1367 cFYI(1, "Error in Open = %d", rc);
1369 *pOplock = pSMBr->OplockLevel; /* 1 byte no need to le_to_cpu */
1370 *netfid = pSMBr->Fid; /* cifs fid stays in le */
1371 /* Let caller know file was created so we can set the mode. */
1372 /* Do we care about the CreateAction in any other cases? */
1373 if (cpu_to_le32(FILE_CREATE) == pSMBr->CreateAction)
1374 *pOplock |= CIFS_CREATE_ACTION;
1376 memcpy((char *)pfile_info, (char *)&pSMBr->CreationTime,
1377 36 /* CreationTime to Attributes */);
1378 /* the file_info buf is endian converted by caller */
1379 pfile_info->AllocationSize = pSMBr->AllocationSize;
1380 pfile_info->EndOfFile = pSMBr->EndOfFile;
1381 pfile_info->NumberOfLinks = cpu_to_le32(1);
1382 pfile_info->DeletePending = 0;
1386 cifs_buf_release(pSMB);
1393 * Discard any remaining data in the current SMB. To do this, we borrow the
1397 cifs_readv_discard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1399 unsigned int rfclen = get_rfc1002_length(server->smallbuf);
1400 int remaining = rfclen + 4 - server->total_read;
1401 struct cifs_readdata *rdata = mid->callback_data;
1403 while (remaining > 0) {
1406 length = cifs_read_from_socket(server, server->bigbuf,
1407 min_t(unsigned int, remaining,
1408 CIFSMaxBufSize + MAX_HEADER_SIZE(server)));
1411 server->total_read += length;
1412 remaining -= length;
1415 dequeue_mid(mid, rdata->result);
1420 cifs_readv_receive(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1423 unsigned int data_offset, data_len;
1424 struct cifs_readdata *rdata = mid->callback_data;
1425 char *buf = server->smallbuf;
1426 unsigned int buflen = get_rfc1002_length(buf) + 4;
1428 cFYI(1, "%s: mid=%llu offset=%llu bytes=%u", __func__,
1429 mid->mid, rdata->offset, rdata->bytes);
1432 * read the rest of READ_RSP header (sans Data array), or whatever we
1433 * can if there's not enough data. At this point, we've read down to
1436 len = min_t(unsigned int, buflen, server->vals->read_rsp_size) -
1437 HEADER_SIZE(server) + 1;
1439 rdata->iov[0].iov_base = buf + HEADER_SIZE(server) - 1;
1440 rdata->iov[0].iov_len = len;
1442 length = cifs_readv_from_socket(server, rdata->iov, 1, len);
1445 server->total_read += length;
1447 /* Was the SMB read successful? */
1448 rdata->result = server->ops->map_error(buf, false);
1449 if (rdata->result != 0) {
1450 cFYI(1, "%s: server returned error %d", __func__,
1452 return cifs_readv_discard(server, mid);
1455 /* Is there enough to get to the rest of the READ_RSP header? */
1456 if (server->total_read < server->vals->read_rsp_size) {
1457 cFYI(1, "%s: server returned short header. got=%u expected=%zu",
1458 __func__, server->total_read,
1459 server->vals->read_rsp_size);
1460 rdata->result = -EIO;
1461 return cifs_readv_discard(server, mid);
1464 data_offset = server->ops->read_data_offset(buf) + 4;
1465 if (data_offset < server->total_read) {
1467 * win2k8 sometimes sends an offset of 0 when the read
1468 * is beyond the EOF. Treat it as if the data starts just after
1471 cFYI(1, "%s: data offset (%u) inside read response header",
1472 __func__, data_offset);
1473 data_offset = server->total_read;
1474 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
1475 /* data_offset is beyond the end of smallbuf */
1476 cFYI(1, "%s: data offset (%u) beyond end of smallbuf",
1477 __func__, data_offset);
1478 rdata->result = -EIO;
1479 return cifs_readv_discard(server, mid);
1482 cFYI(1, "%s: total_read=%u data_offset=%u", __func__,
1483 server->total_read, data_offset);
1485 len = data_offset - server->total_read;
1487 /* read any junk before data into the rest of smallbuf */
1488 rdata->iov[0].iov_base = buf + server->total_read;
1489 rdata->iov[0].iov_len = len;
1490 length = cifs_readv_from_socket(server, rdata->iov, 1, len);
1493 server->total_read += length;
1496 /* set up first iov for signature check */
1497 rdata->iov[0].iov_base = buf;
1498 rdata->iov[0].iov_len = server->total_read;
1500 cFYI(1, "0: iov_base=%p iov_len=%zu",
1501 rdata->iov[0].iov_base, rdata->iov[0].iov_len);
1503 /* how much data is in the response? */
1504 data_len = server->ops->read_data_length(buf);
1505 if (data_offset + data_len > buflen) {
1506 /* data_len is corrupt -- discard frame */
1507 rdata->result = -EIO;
1508 return cifs_readv_discard(server, mid);
1511 length = rdata->read_into_pages(server, rdata, data_len);
1515 server->total_read += length;
1516 rdata->bytes = length;
1518 cFYI(1, "total_read=%u buflen=%u remaining=%u", server->total_read,
1521 /* discard anything left over */
1522 if (server->total_read < buflen)
1523 return cifs_readv_discard(server, mid);
1525 dequeue_mid(mid, false);
1530 cifs_readv_callback(struct mid_q_entry *mid)
1532 struct cifs_readdata *rdata = mid->callback_data;
1533 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
1534 struct TCP_Server_Info *server = tcon->ses->server;
1535 struct smb_rqst rqst = { .rq_iov = rdata->iov,
1536 .rq_nvec = rdata->nr_iov,
1537 .rq_pages = rdata->pages,
1538 .rq_npages = rdata->nr_pages,
1539 .rq_pagesz = rdata->pagesz,
1540 .rq_tailsz = rdata->tailsz };
1542 cFYI(1, "%s: mid=%llu state=%d result=%d bytes=%u", __func__,
1543 mid->mid, mid->mid_state, rdata->result, rdata->bytes);
1545 switch (mid->mid_state) {
1546 case MID_RESPONSE_RECEIVED:
1547 /* result already set, check signature */
1548 if (server->sec_mode &
1549 (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED)) {
1552 rc = cifs_verify_signature(&rqst, server,
1553 mid->sequence_number + 1);
1555 cERROR(1, "SMB signature verification returned "
1558 /* FIXME: should this be counted toward the initiating task? */
1559 task_io_account_read(rdata->bytes);
1560 cifs_stats_bytes_read(tcon, rdata->bytes);
1562 case MID_REQUEST_SUBMITTED:
1563 case MID_RETRY_NEEDED:
1564 rdata->result = -EAGAIN;
1567 rdata->result = -EIO;
1570 queue_work(cifsiod_wq, &rdata->work);
1571 DeleteMidQEntry(mid);
1572 add_credits(server, 1, 0);
1575 /* cifs_async_readv - send an async write, and set up mid to handle result */
1577 cifs_async_readv(struct cifs_readdata *rdata)
1580 READ_REQ *smb = NULL;
1582 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
1583 struct smb_rqst rqst = { .rq_iov = rdata->iov,
1586 cFYI(1, "%s: offset=%llu bytes=%u", __func__,
1587 rdata->offset, rdata->bytes);
1589 if (tcon->ses->capabilities & CAP_LARGE_FILES)
1592 wct = 10; /* old style read */
1593 if ((rdata->offset >> 32) > 0) {
1594 /* can not handle this big offset for old */
1599 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **)&smb);
1603 smb->hdr.Pid = cpu_to_le16((__u16)rdata->pid);
1604 smb->hdr.PidHigh = cpu_to_le16((__u16)(rdata->pid >> 16));
1606 smb->AndXCommand = 0xFF; /* none */
1607 smb->Fid = rdata->cfile->fid.netfid;
1608 smb->OffsetLow = cpu_to_le32(rdata->offset & 0xFFFFFFFF);
1610 smb->OffsetHigh = cpu_to_le32(rdata->offset >> 32);
1612 smb->MaxCount = cpu_to_le16(rdata->bytes & 0xFFFF);
1613 smb->MaxCountHigh = cpu_to_le32(rdata->bytes >> 16);
1617 /* old style read */
1618 struct smb_com_readx_req *smbr =
1619 (struct smb_com_readx_req *)smb;
1620 smbr->ByteCount = 0;
1623 /* 4 for RFC1001 length + 1 for BCC */
1624 rdata->iov[0].iov_base = smb;
1625 rdata->iov[0].iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4;
1627 kref_get(&rdata->refcount);
1628 rc = cifs_call_async(tcon->ses->server, &rqst, cifs_readv_receive,
1629 cifs_readv_callback, rdata, 0);
1632 cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
1634 kref_put(&rdata->refcount, cifs_readdata_release);
1636 cifs_small_buf_release(smb);
1641 CIFSSMBRead(const unsigned int xid, struct cifs_io_parms *io_parms,
1642 unsigned int *nbytes, char **buf, int *pbuf_type)
1645 READ_REQ *pSMB = NULL;
1646 READ_RSP *pSMBr = NULL;
1647 char *pReadData = NULL;
1649 int resp_buf_type = 0;
1651 __u32 pid = io_parms->pid;
1652 __u16 netfid = io_parms->netfid;
1653 __u64 offset = io_parms->offset;
1654 struct cifs_tcon *tcon = io_parms->tcon;
1655 unsigned int count = io_parms->length;
1657 cFYI(1, "Reading %d bytes on fid %d", count, netfid);
1658 if (tcon->ses->capabilities & CAP_LARGE_FILES)
1661 wct = 10; /* old style read */
1662 if ((offset >> 32) > 0) {
1663 /* can not handle this big offset for old */
1669 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **) &pSMB);
1673 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1674 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1676 /* tcon and ses pointer are checked in smb_init */
1677 if (tcon->ses->server == NULL)
1678 return -ECONNABORTED;
1680 pSMB->AndXCommand = 0xFF; /* none */
1682 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
1684 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
1686 pSMB->Remaining = 0;
1687 pSMB->MaxCount = cpu_to_le16(count & 0xFFFF);
1688 pSMB->MaxCountHigh = cpu_to_le32(count >> 16);
1690 pSMB->ByteCount = 0; /* no need to do le conversion since 0 */
1692 /* old style read */
1693 struct smb_com_readx_req *pSMBW =
1694 (struct smb_com_readx_req *)pSMB;
1695 pSMBW->ByteCount = 0;
1698 iov[0].iov_base = (char *)pSMB;
1699 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
1700 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
1701 &resp_buf_type, CIFS_LOG_ERROR);
1702 cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
1703 pSMBr = (READ_RSP *)iov[0].iov_base;
1705 cERROR(1, "Send error in read = %d", rc);
1707 int data_length = le16_to_cpu(pSMBr->DataLengthHigh);
1708 data_length = data_length << 16;
1709 data_length += le16_to_cpu(pSMBr->DataLength);
1710 *nbytes = data_length;
1712 /*check that DataLength would not go beyond end of SMB */
1713 if ((data_length > CIFSMaxBufSize)
1714 || (data_length > count)) {
1715 cFYI(1, "bad length %d for count %d",
1716 data_length, count);
1720 pReadData = (char *) (&pSMBr->hdr.Protocol) +
1721 le16_to_cpu(pSMBr->DataOffset);
1722 /* if (rc = copy_to_user(buf, pReadData, data_length)) {
1723 cERROR(1, "Faulting on read rc = %d",rc);
1725 }*/ /* can not use copy_to_user when using page cache*/
1727 memcpy(*buf, pReadData, data_length);
1731 /* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
1733 if (resp_buf_type == CIFS_SMALL_BUFFER)
1734 cifs_small_buf_release(iov[0].iov_base);
1735 else if (resp_buf_type == CIFS_LARGE_BUFFER)
1736 cifs_buf_release(iov[0].iov_base);
1737 } else if (resp_buf_type != CIFS_NO_BUFFER) {
1738 /* return buffer to caller to free */
1739 *buf = iov[0].iov_base;
1740 if (resp_buf_type == CIFS_SMALL_BUFFER)
1741 *pbuf_type = CIFS_SMALL_BUFFER;
1742 else if (resp_buf_type == CIFS_LARGE_BUFFER)
1743 *pbuf_type = CIFS_LARGE_BUFFER;
1744 } /* else no valid buffer on return - leave as null */
1746 /* Note: On -EAGAIN error only caller can retry on handle based calls
1747 since file handle passed in no longer valid */
1753 CIFSSMBWrite(const unsigned int xid, struct cifs_io_parms *io_parms,
1754 unsigned int *nbytes, const char *buf,
1755 const char __user *ubuf, const int long_op)
1758 WRITE_REQ *pSMB = NULL;
1759 WRITE_RSP *pSMBr = NULL;
1760 int bytes_returned, wct;
1763 __u32 pid = io_parms->pid;
1764 __u16 netfid = io_parms->netfid;
1765 __u64 offset = io_parms->offset;
1766 struct cifs_tcon *tcon = io_parms->tcon;
1767 unsigned int count = io_parms->length;
1771 /* cFYI(1, "write at %lld %d bytes", offset, count);*/
1772 if (tcon->ses == NULL)
1773 return -ECONNABORTED;
1775 if (tcon->ses->capabilities & CAP_LARGE_FILES)
1779 if ((offset >> 32) > 0) {
1780 /* can not handle big offset for old srv */
1785 rc = smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB,
1790 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1791 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1793 /* tcon and ses pointer are checked in smb_init */
1794 if (tcon->ses->server == NULL)
1795 return -ECONNABORTED;
1797 pSMB->AndXCommand = 0xFF; /* none */
1799 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
1801 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
1803 pSMB->Reserved = 0xFFFFFFFF;
1804 pSMB->WriteMode = 0;
1805 pSMB->Remaining = 0;
1807 /* Can increase buffer size if buffer is big enough in some cases ie we
1808 can send more if LARGE_WRITE_X capability returned by the server and if
1809 our buffer is big enough or if we convert to iovecs on socket writes
1810 and eliminate the copy to the CIFS buffer */
1811 if (tcon->ses->capabilities & CAP_LARGE_WRITE_X) {
1812 bytes_sent = min_t(const unsigned int, CIFSMaxBufSize, count);
1814 bytes_sent = (tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE)
1818 if (bytes_sent > count)
1821 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
1823 memcpy(pSMB->Data, buf, bytes_sent);
1825 if (copy_from_user(pSMB->Data, ubuf, bytes_sent)) {
1826 cifs_buf_release(pSMB);
1829 } else if (count != 0) {
1831 cifs_buf_release(pSMB);
1833 } /* else setting file size with write of zero bytes */
1835 byte_count = bytes_sent + 1; /* pad */
1836 else /* wct == 12 */
1837 byte_count = bytes_sent + 5; /* bigger pad, smaller smb hdr */
1839 pSMB->DataLengthLow = cpu_to_le16(bytes_sent & 0xFFFF);
1840 pSMB->DataLengthHigh = cpu_to_le16(bytes_sent >> 16);
1841 inc_rfc1001_len(pSMB, byte_count);
1844 pSMB->ByteCount = cpu_to_le16(byte_count);
1845 else { /* old style write has byte count 4 bytes earlier
1847 struct smb_com_writex_req *pSMBW =
1848 (struct smb_com_writex_req *)pSMB;
1849 pSMBW->ByteCount = cpu_to_le16(byte_count);
1852 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1853 (struct smb_hdr *) pSMBr, &bytes_returned, long_op);
1854 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
1856 cFYI(1, "Send error in write = %d", rc);
1858 *nbytes = le16_to_cpu(pSMBr->CountHigh);
1859 *nbytes = (*nbytes) << 16;
1860 *nbytes += le16_to_cpu(pSMBr->Count);
1863 * Mask off high 16 bits when bytes written as returned by the
1864 * server is greater than bytes requested by the client. Some
1865 * OS/2 servers are known to set incorrect CountHigh values.
1867 if (*nbytes > count)
1871 cifs_buf_release(pSMB);
1873 /* Note: On -EAGAIN error only caller can retry on handle based calls
1874 since file handle passed in no longer valid */
1880 cifs_writedata_release(struct kref *refcount)
1882 struct cifs_writedata *wdata = container_of(refcount,
1883 struct cifs_writedata, refcount);
1886 cifsFileInfo_put(wdata->cfile);
1892 * Write failed with a retryable error. Resend the write request. It's also
1893 * possible that the page was redirtied so re-clean the page.
1896 cifs_writev_requeue(struct cifs_writedata *wdata)
1899 struct inode *inode = wdata->cfile->dentry->d_inode;
1900 struct TCP_Server_Info *server;
1902 for (i = 0; i < wdata->nr_pages; i++) {
1903 lock_page(wdata->pages[i]);
1904 clear_page_dirty_for_io(wdata->pages[i]);
1908 server = tlink_tcon(wdata->cfile->tlink)->ses->server;
1909 rc = server->ops->async_writev(wdata);
1910 } while (rc == -EAGAIN);
1912 for (i = 0; i < wdata->nr_pages; i++) {
1914 SetPageError(wdata->pages[i]);
1915 unlock_page(wdata->pages[i]);
1918 mapping_set_error(inode->i_mapping, rc);
1919 kref_put(&wdata->refcount, cifs_writedata_release);
1923 cifs_writev_complete(struct work_struct *work)
1925 struct cifs_writedata *wdata = container_of(work,
1926 struct cifs_writedata, work);
1927 struct inode *inode = wdata->cfile->dentry->d_inode;
1930 if (wdata->result == 0) {
1931 spin_lock(&inode->i_lock);
1932 cifs_update_eof(CIFS_I(inode), wdata->offset, wdata->bytes);
1933 spin_unlock(&inode->i_lock);
1934 cifs_stats_bytes_written(tlink_tcon(wdata->cfile->tlink),
1936 } else if (wdata->sync_mode == WB_SYNC_ALL && wdata->result == -EAGAIN)
1937 return cifs_writev_requeue(wdata);
1939 for (i = 0; i < wdata->nr_pages; i++) {
1940 struct page *page = wdata->pages[i];
1941 if (wdata->result == -EAGAIN)
1942 __set_page_dirty_nobuffers(page);
1943 else if (wdata->result < 0)
1945 end_page_writeback(page);
1946 page_cache_release(page);
1948 if (wdata->result != -EAGAIN)
1949 mapping_set_error(inode->i_mapping, wdata->result);
1950 kref_put(&wdata->refcount, cifs_writedata_release);
1953 struct cifs_writedata *
1954 cifs_writedata_alloc(unsigned int nr_pages, work_func_t complete)
1956 struct cifs_writedata *wdata;
1958 /* this would overflow */
1959 if (nr_pages == 0) {
1960 cERROR(1, "%s: called with nr_pages == 0!", __func__);
1964 /* writedata + number of page pointers */
1965 wdata = kzalloc(sizeof(*wdata) +
1966 sizeof(struct page *) * (nr_pages - 1), GFP_NOFS);
1967 if (wdata != NULL) {
1968 kref_init(&wdata->refcount);
1969 INIT_LIST_HEAD(&wdata->list);
1970 init_completion(&wdata->done);
1971 INIT_WORK(&wdata->work, complete);
1977 * Check the mid_state and signature on received buffer (if any), and queue the
1978 * workqueue completion task.
1981 cifs_writev_callback(struct mid_q_entry *mid)
1983 struct cifs_writedata *wdata = mid->callback_data;
1984 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
1985 unsigned int written;
1986 WRITE_RSP *smb = (WRITE_RSP *)mid->resp_buf;
1988 switch (mid->mid_state) {
1989 case MID_RESPONSE_RECEIVED:
1990 wdata->result = cifs_check_receive(mid, tcon->ses->server, 0);
1991 if (wdata->result != 0)
1994 written = le16_to_cpu(smb->CountHigh);
1996 written += le16_to_cpu(smb->Count);
1998 * Mask off high 16 bits when bytes written as returned
1999 * by the server is greater than bytes requested by the
2000 * client. OS/2 servers are known to set incorrect
2003 if (written > wdata->bytes)
2006 if (written < wdata->bytes)
2007 wdata->result = -ENOSPC;
2009 wdata->bytes = written;
2011 case MID_REQUEST_SUBMITTED:
2012 case MID_RETRY_NEEDED:
2013 wdata->result = -EAGAIN;
2016 wdata->result = -EIO;
2020 queue_work(cifsiod_wq, &wdata->work);
2021 DeleteMidQEntry(mid);
2022 add_credits(tcon->ses->server, 1, 0);
2025 /* cifs_async_writev - send an async write, and set up mid to handle result */
2027 cifs_async_writev(struct cifs_writedata *wdata)
2030 WRITE_REQ *smb = NULL;
2032 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2034 struct smb_rqst rqst = { };
2036 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
2040 if (wdata->offset >> 32 > 0) {
2041 /* can not handle big offset for old srv */
2046 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **)&smb);
2048 goto async_writev_out;
2050 smb->hdr.Pid = cpu_to_le16((__u16)wdata->pid);
2051 smb->hdr.PidHigh = cpu_to_le16((__u16)(wdata->pid >> 16));
2053 smb->AndXCommand = 0xFF; /* none */
2054 smb->Fid = wdata->cfile->fid.netfid;
2055 smb->OffsetLow = cpu_to_le32(wdata->offset & 0xFFFFFFFF);
2057 smb->OffsetHigh = cpu_to_le32(wdata->offset >> 32);
2058 smb->Reserved = 0xFFFFFFFF;
2063 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
2065 /* 4 for RFC1001 length + 1 for BCC */
2066 iov.iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4 + 1;
2071 rqst.rq_pages = wdata->pages;
2072 rqst.rq_npages = wdata->nr_pages;
2073 rqst.rq_pagesz = wdata->pagesz;
2074 rqst.rq_tailsz = wdata->tailsz;
2076 cFYI(1, "async write at %llu %u bytes", wdata->offset, wdata->bytes);
2078 smb->DataLengthLow = cpu_to_le16(wdata->bytes & 0xFFFF);
2079 smb->DataLengthHigh = cpu_to_le16(wdata->bytes >> 16);
2082 inc_rfc1001_len(&smb->hdr, wdata->bytes + 1);
2083 put_bcc(wdata->bytes + 1, &smb->hdr);
2086 struct smb_com_writex_req *smbw =
2087 (struct smb_com_writex_req *)smb;
2088 inc_rfc1001_len(&smbw->hdr, wdata->bytes + 5);
2089 put_bcc(wdata->bytes + 5, &smbw->hdr);
2090 iov.iov_len += 4; /* pad bigger by four bytes */
2093 kref_get(&wdata->refcount);
2094 rc = cifs_call_async(tcon->ses->server, &rqst, NULL,
2095 cifs_writev_callback, wdata, 0);
2098 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
2100 kref_put(&wdata->refcount, cifs_writedata_release);
2103 cifs_small_buf_release(smb);
2108 CIFSSMBWrite2(const unsigned int xid, struct cifs_io_parms *io_parms,
2109 unsigned int *nbytes, struct kvec *iov, int n_vec)
2112 WRITE_REQ *pSMB = NULL;
2115 int resp_buf_type = 0;
2116 __u32 pid = io_parms->pid;
2117 __u16 netfid = io_parms->netfid;
2118 __u64 offset = io_parms->offset;
2119 struct cifs_tcon *tcon = io_parms->tcon;
2120 unsigned int count = io_parms->length;
2124 cFYI(1, "write2 at %lld %d bytes", (long long)offset, count);
2126 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
2130 if ((offset >> 32) > 0) {
2131 /* can not handle big offset for old srv */
2135 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB);
2139 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
2140 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
2142 /* tcon and ses pointer are checked in smb_init */
2143 if (tcon->ses->server == NULL)
2144 return -ECONNABORTED;
2146 pSMB->AndXCommand = 0xFF; /* none */
2148 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
2150 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
2151 pSMB->Reserved = 0xFFFFFFFF;
2152 pSMB->WriteMode = 0;
2153 pSMB->Remaining = 0;
2156 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
2158 pSMB->DataLengthLow = cpu_to_le16(count & 0xFFFF);
2159 pSMB->DataLengthHigh = cpu_to_le16(count >> 16);
2160 /* header + 1 byte pad */
2161 smb_hdr_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 1;
2163 inc_rfc1001_len(pSMB, count + 1);
2164 else /* wct == 12 */
2165 inc_rfc1001_len(pSMB, count + 5); /* smb data starts later */
2167 pSMB->ByteCount = cpu_to_le16(count + 1);
2168 else /* wct == 12 */ /* bigger pad, smaller smb hdr, keep offset ok */ {
2169 struct smb_com_writex_req *pSMBW =
2170 (struct smb_com_writex_req *)pSMB;
2171 pSMBW->ByteCount = cpu_to_le16(count + 5);
2173 iov[0].iov_base = pSMB;
2175 iov[0].iov_len = smb_hdr_len + 4;
2176 else /* wct == 12 pad bigger by four bytes */
2177 iov[0].iov_len = smb_hdr_len + 8;
2180 rc = SendReceive2(xid, tcon->ses, iov, n_vec + 1, &resp_buf_type, 0);
2181 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
2183 cFYI(1, "Send error Write2 = %d", rc);
2184 } else if (resp_buf_type == 0) {
2185 /* presumably this can not happen, but best to be safe */
2188 WRITE_RSP *pSMBr = (WRITE_RSP *)iov[0].iov_base;
2189 *nbytes = le16_to_cpu(pSMBr->CountHigh);
2190 *nbytes = (*nbytes) << 16;
2191 *nbytes += le16_to_cpu(pSMBr->Count);
2194 * Mask off high 16 bits when bytes written as returned by the
2195 * server is greater than bytes requested by the client. OS/2
2196 * servers are known to set incorrect CountHigh values.
2198 if (*nbytes > count)
2202 /* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
2203 if (resp_buf_type == CIFS_SMALL_BUFFER)
2204 cifs_small_buf_release(iov[0].iov_base);
2205 else if (resp_buf_type == CIFS_LARGE_BUFFER)
2206 cifs_buf_release(iov[0].iov_base);
2208 /* Note: On -EAGAIN error only caller can retry on handle based calls
2209 since file handle passed in no longer valid */
2214 int cifs_lockv(const unsigned int xid, struct cifs_tcon *tcon,
2215 const __u16 netfid, const __u8 lock_type, const __u32 num_unlock,
2216 const __u32 num_lock, LOCKING_ANDX_RANGE *buf)
2219 LOCK_REQ *pSMB = NULL;
2224 cFYI(1, "cifs_lockv num lock %d num unlock %d", num_lock, num_unlock);
2226 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2231 pSMB->NumberOfLocks = cpu_to_le16(num_lock);
2232 pSMB->NumberOfUnlocks = cpu_to_le16(num_unlock);
2233 pSMB->LockType = lock_type;
2234 pSMB->AndXCommand = 0xFF; /* none */
2235 pSMB->Fid = netfid; /* netfid stays le */
2237 count = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2238 inc_rfc1001_len(pSMB, count);
2239 pSMB->ByteCount = cpu_to_le16(count);
2241 iov[0].iov_base = (char *)pSMB;
2242 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4 -
2243 (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2244 iov[1].iov_base = (char *)buf;
2245 iov[1].iov_len = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2247 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
2248 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP);
2250 cFYI(1, "Send error in cifs_lockv = %d", rc);
2256 CIFSSMBLock(const unsigned int xid, struct cifs_tcon *tcon,
2257 const __u16 smb_file_id, const __u32 netpid, const __u64 len,
2258 const __u64 offset, const __u32 numUnlock,
2259 const __u32 numLock, const __u8 lockType,
2260 const bool waitFlag, const __u8 oplock_level)
2263 LOCK_REQ *pSMB = NULL;
2264 /* LOCK_RSP *pSMBr = NULL; */ /* No response data other than rc to parse */
2269 cFYI(1, "CIFSSMBLock timeout %d numLock %d", (int)waitFlag, numLock);
2270 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2275 if (lockType == LOCKING_ANDX_OPLOCK_RELEASE) {
2276 /* no response expected */
2277 flags = CIFS_ASYNC_OP | CIFS_OBREAK_OP;
2279 } else if (waitFlag) {
2280 flags = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
2281 pSMB->Timeout = cpu_to_le32(-1);/* blocking - do not time out */
2286 pSMB->NumberOfLocks = cpu_to_le16(numLock);
2287 pSMB->NumberOfUnlocks = cpu_to_le16(numUnlock);
2288 pSMB->LockType = lockType;
2289 pSMB->OplockLevel = oplock_level;
2290 pSMB->AndXCommand = 0xFF; /* none */
2291 pSMB->Fid = smb_file_id; /* netfid stays le */
2293 if ((numLock != 0) || (numUnlock != 0)) {
2294 pSMB->Locks[0].Pid = cpu_to_le16(netpid);
2295 /* BB where to store pid high? */
2296 pSMB->Locks[0].LengthLow = cpu_to_le32((u32)len);
2297 pSMB->Locks[0].LengthHigh = cpu_to_le32((u32)(len>>32));
2298 pSMB->Locks[0].OffsetLow = cpu_to_le32((u32)offset);
2299 pSMB->Locks[0].OffsetHigh = cpu_to_le32((u32)(offset>>32));
2300 count = sizeof(LOCKING_ANDX_RANGE);
2305 inc_rfc1001_len(pSMB, count);
2306 pSMB->ByteCount = cpu_to_le16(count);
2309 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
2310 (struct smb_hdr *) pSMB, &bytes_returned);
2311 cifs_small_buf_release(pSMB);
2313 rc = SendReceiveNoRsp(xid, tcon->ses, (char *)pSMB, flags);
2314 /* SMB buffer freed by function above */
2316 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
2318 cFYI(1, "Send error in Lock = %d", rc);
2320 /* Note: On -EAGAIN error only caller can retry on handle based calls
2321 since file handle passed in no longer valid */
2326 CIFSSMBPosixLock(const unsigned int xid, struct cifs_tcon *tcon,
2327 const __u16 smb_file_id, const __u32 netpid,
2328 const loff_t start_offset, const __u64 len,
2329 struct file_lock *pLockData, const __u16 lock_type,
2330 const bool waitFlag)
2332 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2333 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
2334 struct cifs_posix_lock *parm_data;
2337 int bytes_returned = 0;
2338 int resp_buf_type = 0;
2339 __u16 params, param_offset, offset, byte_count, count;
2342 cFYI(1, "Posix Lock");
2344 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
2349 pSMBr = (struct smb_com_transaction2_sfi_rsp *)pSMB;
2352 pSMB->MaxSetupCount = 0;
2355 pSMB->Reserved2 = 0;
2356 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2357 offset = param_offset + params;
2359 count = sizeof(struct cifs_posix_lock);
2360 pSMB->MaxParameterCount = cpu_to_le16(2);
2361 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
2362 pSMB->SetupCount = 1;
2363 pSMB->Reserved3 = 0;
2365 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
2367 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2368 byte_count = 3 /* pad */ + params + count;
2369 pSMB->DataCount = cpu_to_le16(count);
2370 pSMB->ParameterCount = cpu_to_le16(params);
2371 pSMB->TotalDataCount = pSMB->DataCount;
2372 pSMB->TotalParameterCount = pSMB->ParameterCount;
2373 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2374 parm_data = (struct cifs_posix_lock *)
2375 (((char *) &pSMB->hdr.Protocol) + offset);
2377 parm_data->lock_type = cpu_to_le16(lock_type);
2379 timeout = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
2380 parm_data->lock_flags = cpu_to_le16(1);
2381 pSMB->Timeout = cpu_to_le32(-1);
2385 parm_data->pid = cpu_to_le32(netpid);
2386 parm_data->start = cpu_to_le64(start_offset);
2387 parm_data->length = cpu_to_le64(len); /* normalize negative numbers */
2389 pSMB->DataOffset = cpu_to_le16(offset);
2390 pSMB->Fid = smb_file_id;
2391 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_LOCK);
2392 pSMB->Reserved4 = 0;
2393 inc_rfc1001_len(pSMB, byte_count);
2394 pSMB->ByteCount = cpu_to_le16(byte_count);
2396 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
2397 (struct smb_hdr *) pSMBr, &bytes_returned);
2399 iov[0].iov_base = (char *)pSMB;
2400 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
2401 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
2402 &resp_buf_type, timeout);
2403 pSMB = NULL; /* request buf already freed by SendReceive2. Do
2404 not try to free it twice below on exit */
2405 pSMBr = (struct smb_com_transaction2_sfi_rsp *)iov[0].iov_base;
2409 cFYI(1, "Send error in Posix Lock = %d", rc);
2410 } else if (pLockData) {
2411 /* lock structure can be returned on get */
2414 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
2416 if (rc || get_bcc(&pSMBr->hdr) < sizeof(*parm_data)) {
2417 rc = -EIO; /* bad smb */
2420 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
2421 data_count = le16_to_cpu(pSMBr->t2.DataCount);
2422 if (data_count < sizeof(struct cifs_posix_lock)) {
2426 parm_data = (struct cifs_posix_lock *)
2427 ((char *)&pSMBr->hdr.Protocol + data_offset);
2428 if (parm_data->lock_type == __constant_cpu_to_le16(CIFS_UNLCK))
2429 pLockData->fl_type = F_UNLCK;
2431 if (parm_data->lock_type ==
2432 __constant_cpu_to_le16(CIFS_RDLCK))
2433 pLockData->fl_type = F_RDLCK;
2434 else if (parm_data->lock_type ==
2435 __constant_cpu_to_le16(CIFS_WRLCK))
2436 pLockData->fl_type = F_WRLCK;
2438 pLockData->fl_start = le64_to_cpu(parm_data->start);
2439 pLockData->fl_end = pLockData->fl_start +
2440 le64_to_cpu(parm_data->length) - 1;
2441 pLockData->fl_pid = le32_to_cpu(parm_data->pid);
2447 cifs_small_buf_release(pSMB);
2449 if (resp_buf_type == CIFS_SMALL_BUFFER)
2450 cifs_small_buf_release(iov[0].iov_base);
2451 else if (resp_buf_type == CIFS_LARGE_BUFFER)
2452 cifs_buf_release(iov[0].iov_base);
2454 /* Note: On -EAGAIN error only caller can retry on handle based calls
2455 since file handle passed in no longer valid */
2462 CIFSSMBClose(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
2465 CLOSE_REQ *pSMB = NULL;
2466 cFYI(1, "In CIFSSMBClose");
2468 /* do not retry on dead session on close */
2469 rc = small_smb_init(SMB_COM_CLOSE, 3, tcon, (void **) &pSMB);
2475 pSMB->FileID = (__u16) smb_file_id;
2476 pSMB->LastWriteTime = 0xFFFFFFFF;
2477 pSMB->ByteCount = 0;
2478 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
2479 cifs_stats_inc(&tcon->stats.cifs_stats.num_closes);
2482 /* EINTR is expected when user ctl-c to kill app */
2483 cERROR(1, "Send error in Close = %d", rc);
2487 /* Since session is dead, file will be closed on server already */
2495 CIFSSMBFlush(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
2498 FLUSH_REQ *pSMB = NULL;
2499 cFYI(1, "In CIFSSMBFlush");
2501 rc = small_smb_init(SMB_COM_FLUSH, 1, tcon, (void **) &pSMB);
2505 pSMB->FileID = (__u16) smb_file_id;
2506 pSMB->ByteCount = 0;
2507 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
2508 cifs_stats_inc(&tcon->stats.cifs_stats.num_flushes);
2510 cERROR(1, "Send error in Flush = %d", rc);
2516 CIFSSMBRename(const unsigned int xid, struct cifs_tcon *tcon,
2517 const char *from_name, const char *to_name,
2518 struct cifs_sb_info *cifs_sb)
2521 RENAME_REQ *pSMB = NULL;
2522 RENAME_RSP *pSMBr = NULL;
2524 int name_len, name_len2;
2526 int remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR;
2528 cFYI(1, "In CIFSSMBRename");
2530 rc = smb_init(SMB_COM_RENAME, 1, tcon, (void **) &pSMB,
2535 pSMB->BufferFormat = 0x04;
2536 pSMB->SearchAttributes =
2537 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
2540 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2541 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2542 from_name, PATH_MAX,
2543 cifs_sb->local_nls, remap);
2544 name_len++; /* trailing null */
2546 pSMB->OldFileName[name_len] = 0x04; /* pad */
2547 /* protocol requires ASCII signature byte on Unicode string */
2548 pSMB->OldFileName[name_len + 1] = 0x00;
2550 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2551 to_name, PATH_MAX, cifs_sb->local_nls,
2553 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2554 name_len2 *= 2; /* convert to bytes */
2555 } else { /* BB improve the check for buffer overruns BB */
2556 name_len = strnlen(from_name, PATH_MAX);
2557 name_len++; /* trailing null */
2558 strncpy(pSMB->OldFileName, from_name, name_len);
2559 name_len2 = strnlen(to_name, PATH_MAX);
2560 name_len2++; /* trailing null */
2561 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2562 strncpy(&pSMB->OldFileName[name_len + 1], to_name, name_len2);
2563 name_len2++; /* trailing null */
2564 name_len2++; /* signature byte */
2567 count = 1 /* 1st signature byte */ + name_len + name_len2;
2568 inc_rfc1001_len(pSMB, count);
2569 pSMB->ByteCount = cpu_to_le16(count);
2571 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2572 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2573 cifs_stats_inc(&tcon->stats.cifs_stats.num_renames);
2575 cFYI(1, "Send error in rename = %d", rc);
2577 cifs_buf_release(pSMB);
2585 int CIFSSMBRenameOpenFile(const unsigned int xid, struct cifs_tcon *pTcon,
2586 int netfid, const char *target_name,
2587 const struct nls_table *nls_codepage, int remap)
2589 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2590 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
2591 struct set_file_rename *rename_info;
2593 char dummy_string[30];
2595 int bytes_returned = 0;
2597 __u16 params, param_offset, offset, count, byte_count;
2599 cFYI(1, "Rename to File by handle");
2600 rc = smb_init(SMB_COM_TRANSACTION2, 15, pTcon, (void **) &pSMB,
2606 pSMB->MaxSetupCount = 0;
2610 pSMB->Reserved2 = 0;
2611 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2612 offset = param_offset + params;
2614 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2615 rename_info = (struct set_file_rename *) data_offset;
2616 pSMB->MaxParameterCount = cpu_to_le16(2);
2617 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
2618 pSMB->SetupCount = 1;
2619 pSMB->Reserved3 = 0;
2620 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2621 byte_count = 3 /* pad */ + params;
2622 pSMB->ParameterCount = cpu_to_le16(params);
2623 pSMB->TotalParameterCount = pSMB->ParameterCount;
2624 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2625 pSMB->DataOffset = cpu_to_le16(offset);
2626 /* construct random name ".cifs_tmp<inodenum><mid>" */
2627 rename_info->overwrite = cpu_to_le32(1);
2628 rename_info->root_fid = 0;
2629 /* unicode only call */
2630 if (target_name == NULL) {
2631 sprintf(dummy_string, "cifs%x", pSMB->hdr.Mid);
2633 cifsConvertToUTF16((__le16 *)rename_info->target_name,
2634 dummy_string, 24, nls_codepage, remap);
2637 cifsConvertToUTF16((__le16 *)rename_info->target_name,
2638 target_name, PATH_MAX, nls_codepage,
2641 rename_info->target_name_len = cpu_to_le32(2 * len_of_str);
2642 count = 12 /* sizeof(struct set_file_rename) */ + (2 * len_of_str);
2643 byte_count += count;
2644 pSMB->DataCount = cpu_to_le16(count);
2645 pSMB->TotalDataCount = pSMB->DataCount;
2647 pSMB->InformationLevel =
2648 cpu_to_le16(SMB_SET_FILE_RENAME_INFORMATION);
2649 pSMB->Reserved4 = 0;
2650 inc_rfc1001_len(pSMB, byte_count);
2651 pSMB->ByteCount = cpu_to_le16(byte_count);
2652 rc = SendReceive(xid, pTcon->ses, (struct smb_hdr *) pSMB,
2653 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2654 cifs_stats_inc(&pTcon->stats.cifs_stats.num_t2renames);
2656 cFYI(1, "Send error in Rename (by file handle) = %d", rc);
2658 cifs_buf_release(pSMB);
2660 /* Note: On -EAGAIN error only caller can retry on handle based calls
2661 since file handle passed in no longer valid */
2667 CIFSSMBCopy(const unsigned int xid, struct cifs_tcon *tcon,
2668 const char *fromName, const __u16 target_tid, const char *toName,
2669 const int flags, const struct nls_table *nls_codepage, int remap)
2672 COPY_REQ *pSMB = NULL;
2673 COPY_RSP *pSMBr = NULL;
2675 int name_len, name_len2;
2678 cFYI(1, "In CIFSSMBCopy");
2680 rc = smb_init(SMB_COM_COPY, 1, tcon, (void **) &pSMB,
2685 pSMB->BufferFormat = 0x04;
2686 pSMB->Tid2 = target_tid;
2688 pSMB->Flags = cpu_to_le16(flags & COPY_TREE);
2690 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2691 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2692 fromName, PATH_MAX, nls_codepage,
2694 name_len++; /* trailing null */
2696 pSMB->OldFileName[name_len] = 0x04; /* pad */
2697 /* protocol requires ASCII signature byte on Unicode string */
2698 pSMB->OldFileName[name_len + 1] = 0x00;
2700 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2701 toName, PATH_MAX, nls_codepage, remap);
2702 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2703 name_len2 *= 2; /* convert to bytes */
2704 } else { /* BB improve the check for buffer overruns BB */
2705 name_len = strnlen(fromName, PATH_MAX);
2706 name_len++; /* trailing null */
2707 strncpy(pSMB->OldFileName, fromName, name_len);
2708 name_len2 = strnlen(toName, PATH_MAX);
2709 name_len2++; /* trailing null */
2710 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2711 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
2712 name_len2++; /* trailing null */
2713 name_len2++; /* signature byte */
2716 count = 1 /* 1st signature byte */ + name_len + name_len2;
2717 inc_rfc1001_len(pSMB, count);
2718 pSMB->ByteCount = cpu_to_le16(count);
2720 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2721 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2723 cFYI(1, "Send error in copy = %d with %d files copied",
2724 rc, le16_to_cpu(pSMBr->CopyCount));
2726 cifs_buf_release(pSMB);
2735 CIFSUnixCreateSymLink(const unsigned int xid, struct cifs_tcon *tcon,
2736 const char *fromName, const char *toName,
2737 const struct nls_table *nls_codepage)
2739 TRANSACTION2_SPI_REQ *pSMB = NULL;
2740 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2743 int name_len_target;
2745 int bytes_returned = 0;
2746 __u16 params, param_offset, offset, byte_count;
2748 cFYI(1, "In Symlink Unix style");
2750 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2755 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2757 cifs_strtoUTF16((__le16 *) pSMB->FileName, fromName,
2758 /* find define for this maxpathcomponent */
2759 PATH_MAX, nls_codepage);
2760 name_len++; /* trailing null */
2763 } else { /* BB improve the check for buffer overruns BB */
2764 name_len = strnlen(fromName, PATH_MAX);
2765 name_len++; /* trailing null */
2766 strncpy(pSMB->FileName, fromName, name_len);
2768 params = 6 + name_len;
2769 pSMB->MaxSetupCount = 0;
2773 pSMB->Reserved2 = 0;
2774 param_offset = offsetof(struct smb_com_transaction2_spi_req,
2775 InformationLevel) - 4;
2776 offset = param_offset + params;
2778 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2779 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2781 cifs_strtoUTF16((__le16 *) data_offset, toName, PATH_MAX
2782 /* find define for this maxpathcomponent */
2784 name_len_target++; /* trailing null */
2785 name_len_target *= 2;
2786 } else { /* BB improve the check for buffer overruns BB */
2787 name_len_target = strnlen(toName, PATH_MAX);
2788 name_len_target++; /* trailing null */
2789 strncpy(data_offset, toName, name_len_target);
2792 pSMB->MaxParameterCount = cpu_to_le16(2);
2793 /* BB find exact max on data count below from sess */
2794 pSMB->MaxDataCount = cpu_to_le16(1000);
2795 pSMB->SetupCount = 1;
2796 pSMB->Reserved3 = 0;
2797 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2798 byte_count = 3 /* pad */ + params + name_len_target;
2799 pSMB->DataCount = cpu_to_le16(name_len_target);
2800 pSMB->ParameterCount = cpu_to_le16(params);
2801 pSMB->TotalDataCount = pSMB->DataCount;
2802 pSMB->TotalParameterCount = pSMB->ParameterCount;
2803 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2804 pSMB->DataOffset = cpu_to_le16(offset);
2805 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_LINK);
2806 pSMB->Reserved4 = 0;
2807 inc_rfc1001_len(pSMB, byte_count);
2808 pSMB->ByteCount = cpu_to_le16(byte_count);
2809 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2810 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2811 cifs_stats_inc(&tcon->stats.cifs_stats.num_symlinks);
2813 cFYI(1, "Send error in SetPathInfo create symlink = %d", rc);
2815 cifs_buf_release(pSMB);
2818 goto createSymLinkRetry;
2824 CIFSUnixCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon,
2825 const char *fromName, const char *toName,
2826 const struct nls_table *nls_codepage, int remap)
2828 TRANSACTION2_SPI_REQ *pSMB = NULL;
2829 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2832 int name_len_target;
2834 int bytes_returned = 0;
2835 __u16 params, param_offset, offset, byte_count;
2837 cFYI(1, "In Create Hard link Unix style");
2838 createHardLinkRetry:
2839 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2844 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2845 name_len = cifsConvertToUTF16((__le16 *) pSMB->FileName, toName,
2846 PATH_MAX, nls_codepage, remap);
2847 name_len++; /* trailing null */
2850 } else { /* BB improve the check for buffer overruns BB */
2851 name_len = strnlen(toName, PATH_MAX);
2852 name_len++; /* trailing null */
2853 strncpy(pSMB->FileName, toName, name_len);
2855 params = 6 + name_len;
2856 pSMB->MaxSetupCount = 0;
2860 pSMB->Reserved2 = 0;
2861 param_offset = offsetof(struct smb_com_transaction2_spi_req,
2862 InformationLevel) - 4;
2863 offset = param_offset + params;
2865 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2866 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2868 cifsConvertToUTF16((__le16 *) data_offset, fromName,
2869 PATH_MAX, nls_codepage, remap);
2870 name_len_target++; /* trailing null */
2871 name_len_target *= 2;
2872 } else { /* BB improve the check for buffer overruns BB */
2873 name_len_target = strnlen(fromName, PATH_MAX);
2874 name_len_target++; /* trailing null */
2875 strncpy(data_offset, fromName, name_len_target);
2878 pSMB->MaxParameterCount = cpu_to_le16(2);
2879 /* BB find exact max on data count below from sess*/
2880 pSMB->MaxDataCount = cpu_to_le16(1000);
2881 pSMB->SetupCount = 1;
2882 pSMB->Reserved3 = 0;
2883 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2884 byte_count = 3 /* pad */ + params + name_len_target;
2885 pSMB->ParameterCount = cpu_to_le16(params);
2886 pSMB->TotalParameterCount = pSMB->ParameterCount;
2887 pSMB->DataCount = cpu_to_le16(name_len_target);
2888 pSMB->TotalDataCount = pSMB->DataCount;
2889 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2890 pSMB->DataOffset = cpu_to_le16(offset);
2891 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_HLINK);
2892 pSMB->Reserved4 = 0;
2893 inc_rfc1001_len(pSMB, byte_count);
2894 pSMB->ByteCount = cpu_to_le16(byte_count);
2895 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2896 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2897 cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
2899 cFYI(1, "Send error in SetPathInfo (hard link) = %d", rc);
2901 cifs_buf_release(pSMB);
2903 goto createHardLinkRetry;
2909 CIFSCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon,
2910 const char *from_name, const char *to_name,
2911 struct cifs_sb_info *cifs_sb)
2914 NT_RENAME_REQ *pSMB = NULL;
2915 RENAME_RSP *pSMBr = NULL;
2917 int name_len, name_len2;
2919 int remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR;
2921 cFYI(1, "In CIFSCreateHardLink");
2922 winCreateHardLinkRetry:
2924 rc = smb_init(SMB_COM_NT_RENAME, 4, tcon, (void **) &pSMB,
2929 pSMB->SearchAttributes =
2930 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
2932 pSMB->Flags = cpu_to_le16(CREATE_HARD_LINK);
2933 pSMB->ClusterCount = 0;
2935 pSMB->BufferFormat = 0x04;
2937 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2939 cifsConvertToUTF16((__le16 *) pSMB->OldFileName, from_name,
2940 PATH_MAX, cifs_sb->local_nls, remap);
2941 name_len++; /* trailing null */
2944 /* protocol specifies ASCII buffer format (0x04) for unicode */
2945 pSMB->OldFileName[name_len] = 0x04;
2946 pSMB->OldFileName[name_len + 1] = 0x00; /* pad */
2948 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2949 to_name, PATH_MAX, cifs_sb->local_nls,
2951 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2952 name_len2 *= 2; /* convert to bytes */
2953 } else { /* BB improve the check for buffer overruns BB */
2954 name_len = strnlen(from_name, PATH_MAX);
2955 name_len++; /* trailing null */
2956 strncpy(pSMB->OldFileName, from_name, name_len);
2957 name_len2 = strnlen(to_name, PATH_MAX);
2958 name_len2++; /* trailing null */
2959 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2960 strncpy(&pSMB->OldFileName[name_len + 1], to_name, name_len2);
2961 name_len2++; /* trailing null */
2962 name_len2++; /* signature byte */
2965 count = 1 /* string type byte */ + name_len + name_len2;
2966 inc_rfc1001_len(pSMB, count);
2967 pSMB->ByteCount = cpu_to_le16(count);
2969 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2970 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2971 cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
2973 cFYI(1, "Send error in hard link (NT rename) = %d", rc);
2975 cifs_buf_release(pSMB);
2977 goto winCreateHardLinkRetry;
2983 CIFSSMBUnixQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon,
2984 const unsigned char *searchName, char **symlinkinfo,
2985 const struct nls_table *nls_codepage)
2987 /* SMB_QUERY_FILE_UNIX_LINK */
2988 TRANSACTION2_QPI_REQ *pSMB = NULL;
2989 TRANSACTION2_QPI_RSP *pSMBr = NULL;
2993 __u16 params, byte_count;
2996 cFYI(1, "In QPathSymLinkInfo (Unix) for path %s", searchName);
2999 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3004 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3006 cifs_strtoUTF16((__le16 *) pSMB->FileName, searchName,
3007 PATH_MAX, nls_codepage);
3008 name_len++; /* trailing null */
3010 } else { /* BB improve the check for buffer overruns BB */
3011 name_len = strnlen(searchName, PATH_MAX);
3012 name_len++; /* trailing null */
3013 strncpy(pSMB->FileName, searchName, name_len);
3016 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3017 pSMB->TotalDataCount = 0;
3018 pSMB->MaxParameterCount = cpu_to_le16(2);
3019 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
3020 pSMB->MaxSetupCount = 0;
3024 pSMB->Reserved2 = 0;
3025 pSMB->ParameterOffset = cpu_to_le16(offsetof(
3026 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
3027 pSMB->DataCount = 0;
3028 pSMB->DataOffset = 0;
3029 pSMB->SetupCount = 1;
3030 pSMB->Reserved3 = 0;
3031 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3032 byte_count = params + 1 /* pad */ ;
3033 pSMB->TotalParameterCount = cpu_to_le16(params);
3034 pSMB->ParameterCount = pSMB->TotalParameterCount;
3035 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_LINK);
3036 pSMB->Reserved4 = 0;
3037 inc_rfc1001_len(pSMB, byte_count);
3038 pSMB->ByteCount = cpu_to_le16(byte_count);
3040 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3041 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3043 cFYI(1, "Send error in QuerySymLinkInfo = %d", rc);
3045 /* decode response */
3047 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
3048 /* BB also check enough total bytes returned */
3049 if (rc || get_bcc(&pSMBr->hdr) < 2)
3053 u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3055 data_start = ((char *) &pSMBr->hdr.Protocol) +
3056 le16_to_cpu(pSMBr->t2.DataOffset);
3058 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3063 /* BB FIXME investigate remapping reserved chars here */
3064 *symlinkinfo = cifs_strndup_from_utf16(data_start,
3065 count, is_unicode, nls_codepage);
3070 cifs_buf_release(pSMB);
3072 goto querySymLinkRetry;
3076 #ifdef CONFIG_CIFS_SYMLINK_EXPERIMENTAL
3078 * Recent Windows versions now create symlinks more frequently
3079 * and they use the "reparse point" mechanism below. We can of course
3080 * do symlinks nicely to Samba and other servers which support the
3081 * CIFS Unix Extensions and we can also do SFU symlinks and "client only"
3082 * "MF" symlinks optionally, but for recent Windows we really need to
3083 * reenable the code below and fix the cifs_symlink callers to handle this.
3084 * In the interim this code has been moved to its own config option so
3085 * it is not compiled in by default until callers fixed up and more tested.
3088 CIFSSMBQueryReparseLinkInfo(const unsigned int xid, struct cifs_tcon *tcon,
3089 const unsigned char *searchName,
3090 char *symlinkinfo, const int buflen, __u16 fid,
3091 const struct nls_table *nls_codepage)
3095 struct smb_com_transaction_ioctl_req *pSMB;
3096 struct smb_com_transaction_ioctl_rsp *pSMBr;
3098 cFYI(1, "In Windows reparse style QueryLink for path %s", searchName);
3099 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3104 pSMB->TotalParameterCount = 0 ;
3105 pSMB->TotalDataCount = 0;
3106 pSMB->MaxParameterCount = cpu_to_le32(2);
3107 /* BB find exact data count max from sess structure BB */
3108 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
3109 pSMB->MaxSetupCount = 4;
3111 pSMB->ParameterOffset = 0;
3112 pSMB->DataCount = 0;
3113 pSMB->DataOffset = 0;
3114 pSMB->SetupCount = 4;
3115 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
3116 pSMB->ParameterCount = pSMB->TotalParameterCount;
3117 pSMB->FunctionCode = cpu_to_le32(FSCTL_GET_REPARSE_POINT);
3118 pSMB->IsFsctl = 1; /* FSCTL */
3119 pSMB->IsRootFlag = 0;
3120 pSMB->Fid = fid; /* file handle always le */
3121 pSMB->ByteCount = 0;
3123 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3124 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3126 cFYI(1, "Send error in QueryReparseLinkInfo = %d", rc);
3127 } else { /* decode response */
3128 __u32 data_offset = le32_to_cpu(pSMBr->DataOffset);
3129 __u32 data_count = le32_to_cpu(pSMBr->DataCount);
3130 if (get_bcc(&pSMBr->hdr) < 2 || data_offset > 512) {
3131 /* BB also check enough total bytes returned */
3132 rc = -EIO; /* bad smb */
3135 if (data_count && (data_count < 2048)) {
3136 char *end_of_smb = 2 /* sizeof byte count */ +
3137 get_bcc(&pSMBr->hdr) + (char *)&pSMBr->ByteCount;
3139 struct reparse_data *reparse_buf =
3140 (struct reparse_data *)
3141 ((char *)&pSMBr->hdr.Protocol
3143 if ((char *)reparse_buf >= end_of_smb) {
3147 if ((reparse_buf->LinkNamesBuf +
3148 reparse_buf->TargetNameOffset +
3149 reparse_buf->TargetNameLen) > end_of_smb) {
3150 cFYI(1, "reparse buf beyond SMB");
3155 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE) {
3156 cifs_from_ucs2(symlinkinfo, (__le16 *)
3157 (reparse_buf->LinkNamesBuf +
3158 reparse_buf->TargetNameOffset),
3160 reparse_buf->TargetNameLen,
3162 } else { /* ASCII names */
3163 strncpy(symlinkinfo,
3164 reparse_buf->LinkNamesBuf +
3165 reparse_buf->TargetNameOffset,
3166 min_t(const int, buflen,
3167 reparse_buf->TargetNameLen));
3171 cFYI(1, "Invalid return data count on "
3172 "get reparse info ioctl");
3174 symlinkinfo[buflen] = 0; /* just in case so the caller
3175 does not go off the end of the buffer */
3176 cFYI(1, "readlink result - %s", symlinkinfo);
3180 cifs_buf_release(pSMB);
3182 /* Note: On -EAGAIN error only caller can retry on handle based calls
3183 since file handle passed in no longer valid */
3187 #endif /* CIFS_SYMLINK_EXPERIMENTAL */ /* BB temporarily unused */
3189 #ifdef CONFIG_CIFS_POSIX
3191 /*Convert an Access Control Entry from wire format to local POSIX xattr format*/
3192 static void cifs_convert_ace(posix_acl_xattr_entry *ace,
3193 struct cifs_posix_ace *cifs_ace)
3195 /* u8 cifs fields do not need le conversion */
3196 ace->e_perm = cpu_to_le16(cifs_ace->cifs_e_perm);
3197 ace->e_tag = cpu_to_le16(cifs_ace->cifs_e_tag);
3198 ace->e_id = cpu_to_le32(le64_to_cpu(cifs_ace->cifs_uid));
3199 /* cFYI(1, "perm %d tag %d id %d",ace->e_perm,ace->e_tag,ace->e_id); */
3204 /* Convert ACL from CIFS POSIX wire format to local Linux POSIX ACL xattr */
3205 static int cifs_copy_posix_acl(char *trgt, char *src, const int buflen,
3206 const int acl_type, const int size_of_data_area)
3211 struct cifs_posix_ace *pACE;
3212 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)src;
3213 posix_acl_xattr_header *local_acl = (posix_acl_xattr_header *)trgt;
3215 if (le16_to_cpu(cifs_acl->version) != CIFS_ACL_VERSION)
3218 if (acl_type & ACL_TYPE_ACCESS) {
3219 count = le16_to_cpu(cifs_acl->access_entry_count);
3220 pACE = &cifs_acl->ace_array[0];
3221 size = sizeof(struct cifs_posix_acl);
3222 size += sizeof(struct cifs_posix_ace) * count;
3223 /* check if we would go beyond end of SMB */
3224 if (size_of_data_area < size) {
3225 cFYI(1, "bad CIFS POSIX ACL size %d vs. %d",
3226 size_of_data_area, size);
3229 } else if (acl_type & ACL_TYPE_DEFAULT) {
3230 count = le16_to_cpu(cifs_acl->access_entry_count);
3231 size = sizeof(struct cifs_posix_acl);
3232 size += sizeof(struct cifs_posix_ace) * count;
3233 /* skip past access ACEs to get to default ACEs */
3234 pACE = &cifs_acl->ace_array[count];
3235 count = le16_to_cpu(cifs_acl->default_entry_count);
3236 size += sizeof(struct cifs_posix_ace) * count;
3237 /* check if we would go beyond end of SMB */
3238 if (size_of_data_area < size)
3245 size = posix_acl_xattr_size(count);
3246 if ((buflen == 0) || (local_acl == NULL)) {
3247 /* used to query ACL EA size */
3248 } else if (size > buflen) {
3250 } else /* buffer big enough */ {
3251 local_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
3252 for (i = 0; i < count ; i++) {
3253 cifs_convert_ace(&local_acl->a_entries[i], pACE);
3260 static __u16 convert_ace_to_cifs_ace(struct cifs_posix_ace *cifs_ace,
3261 const posix_acl_xattr_entry *local_ace)
3263 __u16 rc = 0; /* 0 = ACL converted ok */
3265 cifs_ace->cifs_e_perm = le16_to_cpu(local_ace->e_perm);
3266 cifs_ace->cifs_e_tag = le16_to_cpu(local_ace->e_tag);
3267 /* BB is there a better way to handle the large uid? */
3268 if (local_ace->e_id == cpu_to_le32(-1)) {
3269 /* Probably no need to le convert -1 on any arch but can not hurt */
3270 cifs_ace->cifs_uid = cpu_to_le64(-1);
3272 cifs_ace->cifs_uid = cpu_to_le64(le32_to_cpu(local_ace->e_id));
3273 /*cFYI(1, "perm %d tag %d id %d",ace->e_perm,ace->e_tag,ace->e_id);*/
3277 /* Convert ACL from local Linux POSIX xattr to CIFS POSIX ACL wire format */
3278 static __u16 ACL_to_cifs_posix(char *parm_data, const char *pACL,
3279 const int buflen, const int acl_type)
3282 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)parm_data;
3283 posix_acl_xattr_header *local_acl = (posix_acl_xattr_header *)pACL;
3287 if ((buflen == 0) || (pACL == NULL) || (cifs_acl == NULL))
3290 count = posix_acl_xattr_count((size_t)buflen);
3291 cFYI(1, "setting acl with %d entries from buf of length %d and "
3293 count, buflen, le32_to_cpu(local_acl->a_version));
3294 if (le32_to_cpu(local_acl->a_version) != 2) {
3295 cFYI(1, "unknown POSIX ACL version %d",
3296 le32_to_cpu(local_acl->a_version));
3299 cifs_acl->version = cpu_to_le16(1);
3300 if (acl_type == ACL_TYPE_ACCESS)
3301 cifs_acl->access_entry_count = cpu_to_le16(count);
3302 else if (acl_type == ACL_TYPE_DEFAULT)
3303 cifs_acl->default_entry_count = cpu_to_le16(count);
3305 cFYI(1, "unknown ACL type %d", acl_type);
3308 for (i = 0; i < count; i++) {
3309 rc = convert_ace_to_cifs_ace(&cifs_acl->ace_array[i],
3310 &local_acl->a_entries[i]);
3312 /* ACE not converted */
3317 rc = (__u16)(count * sizeof(struct cifs_posix_ace));
3318 rc += sizeof(struct cifs_posix_acl);
3319 /* BB add check to make sure ACL does not overflow SMB */
3325 CIFSSMBGetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
3326 const unsigned char *searchName,
3327 char *acl_inf, const int buflen, const int acl_type,
3328 const struct nls_table *nls_codepage, int remap)
3330 /* SMB_QUERY_POSIX_ACL */
3331 TRANSACTION2_QPI_REQ *pSMB = NULL;
3332 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3336 __u16 params, byte_count;
3338 cFYI(1, "In GetPosixACL (Unix) for path %s", searchName);
3341 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3346 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3348 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3349 searchName, PATH_MAX, nls_codepage,
3351 name_len++; /* trailing null */
3353 pSMB->FileName[name_len] = 0;
3354 pSMB->FileName[name_len+1] = 0;
3355 } else { /* BB improve the check for buffer overruns BB */
3356 name_len = strnlen(searchName, PATH_MAX);
3357 name_len++; /* trailing null */
3358 strncpy(pSMB->FileName, searchName, name_len);
3361 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3362 pSMB->TotalDataCount = 0;
3363 pSMB->MaxParameterCount = cpu_to_le16(2);
3364 /* BB find exact max data count below from sess structure BB */
3365 pSMB->MaxDataCount = cpu_to_le16(4000);
3366 pSMB->MaxSetupCount = 0;
3370 pSMB->Reserved2 = 0;
3371 pSMB->ParameterOffset = cpu_to_le16(
3372 offsetof(struct smb_com_transaction2_qpi_req,
3373 InformationLevel) - 4);
3374 pSMB->DataCount = 0;
3375 pSMB->DataOffset = 0;
3376 pSMB->SetupCount = 1;
3377 pSMB->Reserved3 = 0;
3378 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3379 byte_count = params + 1 /* pad */ ;
3380 pSMB->TotalParameterCount = cpu_to_le16(params);
3381 pSMB->ParameterCount = pSMB->TotalParameterCount;
3382 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_ACL);
3383 pSMB->Reserved4 = 0;
3384 inc_rfc1001_len(pSMB, byte_count);
3385 pSMB->ByteCount = cpu_to_le16(byte_count);
3387 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3388 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3389 cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
3391 cFYI(1, "Send error in Query POSIX ACL = %d", rc);
3393 /* decode response */
3395 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
3396 /* BB also check enough total bytes returned */
3397 if (rc || get_bcc(&pSMBr->hdr) < 2)
3398 rc = -EIO; /* bad smb */
3400 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3401 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3402 rc = cifs_copy_posix_acl(acl_inf,
3403 (char *)&pSMBr->hdr.Protocol+data_offset,
3404 buflen, acl_type, count);
3407 cifs_buf_release(pSMB);
3414 CIFSSMBSetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
3415 const unsigned char *fileName,
3416 const char *local_acl, const int buflen,
3418 const struct nls_table *nls_codepage, int remap)
3420 struct smb_com_transaction2_spi_req *pSMB = NULL;
3421 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
3425 int bytes_returned = 0;
3426 __u16 params, byte_count, data_count, param_offset, offset;
3428 cFYI(1, "In SetPosixACL (Unix) for path %s", fileName);
3430 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3434 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3436 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
3437 PATH_MAX, nls_codepage, remap);
3438 name_len++; /* trailing null */
3440 } else { /* BB improve the check for buffer overruns BB */
3441 name_len = strnlen(fileName, PATH_MAX);
3442 name_len++; /* trailing null */
3443 strncpy(pSMB->FileName, fileName, name_len);
3445 params = 6 + name_len;
3446 pSMB->MaxParameterCount = cpu_to_le16(2);
3447 /* BB find max SMB size from sess */
3448 pSMB->MaxDataCount = cpu_to_le16(1000);
3449 pSMB->MaxSetupCount = 0;
3453 pSMB->Reserved2 = 0;
3454 param_offset = offsetof(struct smb_com_transaction2_spi_req,
3455 InformationLevel) - 4;
3456 offset = param_offset + params;
3457 parm_data = ((char *) &pSMB->hdr.Protocol) + offset;
3458 pSMB->ParameterOffset = cpu_to_le16(param_offset);
3460 /* convert to on the wire format for POSIX ACL */
3461 data_count = ACL_to_cifs_posix(parm_data, local_acl, buflen, acl_type);
3463 if (data_count == 0) {
3465 goto setACLerrorExit;
3467 pSMB->DataOffset = cpu_to_le16(offset);
3468 pSMB->SetupCount = 1;
3469 pSMB->Reserved3 = 0;
3470 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
3471 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_ACL);
3472 byte_count = 3 /* pad */ + params + data_count;
3473 pSMB->DataCount = cpu_to_le16(data_count);
3474 pSMB->TotalDataCount = pSMB->DataCount;
3475 pSMB->ParameterCount = cpu_to_le16(params);
3476 pSMB->TotalParameterCount = pSMB->ParameterCount;
3477 pSMB->Reserved4 = 0;
3478 inc_rfc1001_len(pSMB, byte_count);
3479 pSMB->ByteCount = cpu_to_le16(byte_count);
3480 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3481 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3483 cFYI(1, "Set POSIX ACL returned %d", rc);
3486 cifs_buf_release(pSMB);
3492 /* BB fix tabs in this function FIXME BB */
3494 CIFSGetExtAttr(const unsigned int xid, struct cifs_tcon *tcon,
3495 const int netfid, __u64 *pExtAttrBits, __u64 *pMask)
3498 struct smb_t2_qfi_req *pSMB = NULL;
3499 struct smb_t2_qfi_rsp *pSMBr = NULL;
3501 __u16 params, byte_count;
3503 cFYI(1, "In GetExtAttr");
3508 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3513 params = 2 /* level */ + 2 /* fid */;
3514 pSMB->t2.TotalDataCount = 0;
3515 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
3516 /* BB find exact max data count below from sess structure BB */
3517 pSMB->t2.MaxDataCount = cpu_to_le16(4000);
3518 pSMB->t2.MaxSetupCount = 0;
3519 pSMB->t2.Reserved = 0;
3521 pSMB->t2.Timeout = 0;
3522 pSMB->t2.Reserved2 = 0;
3523 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
3525 pSMB->t2.DataCount = 0;
3526 pSMB->t2.DataOffset = 0;
3527 pSMB->t2.SetupCount = 1;
3528 pSMB->t2.Reserved3 = 0;
3529 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
3530 byte_count = params + 1 /* pad */ ;
3531 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
3532 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
3533 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_ATTR_FLAGS);
3536 inc_rfc1001_len(pSMB, byte_count);
3537 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
3539 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3540 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3542 cFYI(1, "error %d in GetExtAttr", rc);
3544 /* decode response */
3545 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
3546 /* BB also check enough total bytes returned */
3547 if (rc || get_bcc(&pSMBr->hdr) < 2)
3548 /* If rc should we check for EOPNOSUPP and
3549 disable the srvino flag? or in caller? */
3550 rc = -EIO; /* bad smb */
3552 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3553 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3554 struct file_chattr_info *pfinfo;
3555 /* BB Do we need a cast or hash here ? */
3557 cFYI(1, "Illegal size ret in GetExtAttr");
3561 pfinfo = (struct file_chattr_info *)
3562 (data_offset + (char *) &pSMBr->hdr.Protocol);
3563 *pExtAttrBits = le64_to_cpu(pfinfo->mode);
3564 *pMask = le64_to_cpu(pfinfo->mask);
3568 cifs_buf_release(pSMB);
3570 goto GetExtAttrRetry;
3574 #endif /* CONFIG_POSIX */
3576 #ifdef CONFIG_CIFS_ACL
3578 * Initialize NT TRANSACT SMB into small smb request buffer. This assumes that
3579 * all NT TRANSACTS that we init here have total parm and data under about 400
3580 * bytes (to fit in small cifs buffer size), which is the case so far, it
3581 * easily fits. NB: Setup words themselves and ByteCount MaxSetupCount (size of
3582 * returned setup area) and MaxParameterCount (returned parms size) must be set
3586 smb_init_nttransact(const __u16 sub_command, const int setup_count,
3587 const int parm_len, struct cifs_tcon *tcon,
3592 struct smb_com_ntransact_req *pSMB;
3594 rc = small_smb_init(SMB_COM_NT_TRANSACT, 19 + setup_count, tcon,
3598 *ret_buf = (void *)pSMB;
3600 pSMB->TotalParameterCount = cpu_to_le32(parm_len);
3601 pSMB->TotalDataCount = 0;
3602 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
3603 pSMB->ParameterCount = pSMB->TotalParameterCount;
3604 pSMB->DataCount = pSMB->TotalDataCount;
3605 temp_offset = offsetof(struct smb_com_ntransact_req, Parms) +
3606 (setup_count * 2) - 4 /* for rfc1001 length itself */;
3607 pSMB->ParameterOffset = cpu_to_le32(temp_offset);
3608 pSMB->DataOffset = cpu_to_le32(temp_offset + parm_len);
3609 pSMB->SetupCount = setup_count; /* no need to le convert byte fields */
3610 pSMB->SubCommand = cpu_to_le16(sub_command);
3615 validate_ntransact(char *buf, char **ppparm, char **ppdata,
3616 __u32 *pparmlen, __u32 *pdatalen)
3619 __u32 data_count, data_offset, parm_count, parm_offset;
3620 struct smb_com_ntransact_rsp *pSMBr;
3629 pSMBr = (struct smb_com_ntransact_rsp *)buf;
3631 bcc = get_bcc(&pSMBr->hdr);
3632 end_of_smb = 2 /* sizeof byte count */ + bcc +
3633 (char *)&pSMBr->ByteCount;
3635 data_offset = le32_to_cpu(pSMBr->DataOffset);
3636 data_count = le32_to_cpu(pSMBr->DataCount);
3637 parm_offset = le32_to_cpu(pSMBr->ParameterOffset);
3638 parm_count = le32_to_cpu(pSMBr->ParameterCount);
3640 *ppparm = (char *)&pSMBr->hdr.Protocol + parm_offset;
3641 *ppdata = (char *)&pSMBr->hdr.Protocol + data_offset;
3643 /* should we also check that parm and data areas do not overlap? */
3644 if (*ppparm > end_of_smb) {
3645 cFYI(1, "parms start after end of smb");
3647 } else if (parm_count + *ppparm > end_of_smb) {
3648 cFYI(1, "parm end after end of smb");
3650 } else if (*ppdata > end_of_smb) {
3651 cFYI(1, "data starts after end of smb");
3653 } else if (data_count + *ppdata > end_of_smb) {
3654 cFYI(1, "data %p + count %d (%p) past smb end %p start %p",
3655 *ppdata, data_count, (data_count + *ppdata),
3658 } else if (parm_count + data_count > bcc) {
3659 cFYI(1, "parm count and data count larger than SMB");
3662 *pdatalen = data_count;
3663 *pparmlen = parm_count;
3667 /* Get Security Descriptor (by handle) from remote server for a file or dir */
3669 CIFSSMBGetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
3670 struct cifs_ntsd **acl_inf, __u32 *pbuflen)
3674 QUERY_SEC_DESC_REQ *pSMB;
3677 cFYI(1, "GetCifsACL");
3682 rc = smb_init_nttransact(NT_TRANSACT_QUERY_SECURITY_DESC, 0,
3683 8 /* parm len */, tcon, (void **) &pSMB);
3687 pSMB->MaxParameterCount = cpu_to_le32(4);
3688 /* BB TEST with big acls that might need to be e.g. larger than 16K */
3689 pSMB->MaxSetupCount = 0;
3690 pSMB->Fid = fid; /* file handle always le */
3691 pSMB->AclFlags = cpu_to_le32(CIFS_ACL_OWNER | CIFS_ACL_GROUP |
3693 pSMB->ByteCount = cpu_to_le16(11); /* 3 bytes pad + 8 bytes parm */
3694 inc_rfc1001_len(pSMB, 11);
3695 iov[0].iov_base = (char *)pSMB;
3696 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
3698 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovec */, &buf_type,
3700 cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
3702 cFYI(1, "Send error in QuerySecDesc = %d", rc);
3703 } else { /* decode response */
3707 struct smb_com_ntransact_rsp *pSMBr;
3710 /* validate_nttransact */
3711 rc = validate_ntransact(iov[0].iov_base, (char **)&parm,
3712 &pdata, &parm_len, pbuflen);
3715 pSMBr = (struct smb_com_ntransact_rsp *)iov[0].iov_base;
3717 cFYI(1, "smb %p parm %p data %p", pSMBr, parm, *acl_inf);
3719 if (le32_to_cpu(pSMBr->ParameterCount) != 4) {
3720 rc = -EIO; /* bad smb */
3725 /* BB check that data area is minimum length and as big as acl_len */
3727 acl_len = le32_to_cpu(*parm);
3728 if (acl_len != *pbuflen) {
3729 cERROR(1, "acl length %d does not match %d",
3731 if (*pbuflen > acl_len)
3735 /* check if buffer is big enough for the acl
3736 header followed by the smallest SID */
3737 if ((*pbuflen < sizeof(struct cifs_ntsd) + 8) ||
3738 (*pbuflen >= 64 * 1024)) {
3739 cERROR(1, "bad acl length %d", *pbuflen);
3743 *acl_inf = kmalloc(*pbuflen, GFP_KERNEL);
3744 if (*acl_inf == NULL) {
3748 memcpy(*acl_inf, pdata, *pbuflen);
3752 if (buf_type == CIFS_SMALL_BUFFER)
3753 cifs_small_buf_release(iov[0].iov_base);
3754 else if (buf_type == CIFS_LARGE_BUFFER)
3755 cifs_buf_release(iov[0].iov_base);
3756 /* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
3761 CIFSSMBSetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
3762 struct cifs_ntsd *pntsd, __u32 acllen, int aclflag)
3764 __u16 byte_count, param_count, data_count, param_offset, data_offset;
3766 int bytes_returned = 0;
3767 SET_SEC_DESC_REQ *pSMB = NULL;
3771 rc = smb_init(SMB_COM_NT_TRANSACT, 19, tcon, (void **) &pSMB, &pSMBr);
3775 pSMB->MaxSetupCount = 0;
3779 param_offset = offsetof(struct smb_com_transaction_ssec_req, Fid) - 4;
3780 data_count = acllen;
3781 data_offset = param_offset + param_count;
3782 byte_count = 3 /* pad */ + param_count;
3784 pSMB->DataCount = cpu_to_le32(data_count);
3785 pSMB->TotalDataCount = pSMB->DataCount;
3786 pSMB->MaxParameterCount = cpu_to_le32(4);
3787 pSMB->MaxDataCount = cpu_to_le32(16384);
3788 pSMB->ParameterCount = cpu_to_le32(param_count);
3789 pSMB->ParameterOffset = cpu_to_le32(param_offset);
3790 pSMB->TotalParameterCount = pSMB->ParameterCount;
3791 pSMB->DataOffset = cpu_to_le32(data_offset);
3792 pSMB->SetupCount = 0;
3793 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_SET_SECURITY_DESC);
3794 pSMB->ByteCount = cpu_to_le16(byte_count+data_count);
3796 pSMB->Fid = fid; /* file handle always le */
3797 pSMB->Reserved2 = 0;
3798 pSMB->AclFlags = cpu_to_le32(aclflag);
3800 if (pntsd && acllen) {
3801 memcpy((char *)pSMBr + offsetof(struct smb_hdr, Protocol) +
3802 data_offset, pntsd, acllen);
3803 inc_rfc1001_len(pSMB, byte_count + data_count);
3805 inc_rfc1001_len(pSMB, byte_count);
3807 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3808 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3810 cFYI(1, "SetCIFSACL bytes_returned: %d, rc: %d", bytes_returned, rc);
3812 cFYI(1, "Set CIFS ACL returned %d", rc);
3813 cifs_buf_release(pSMB);
3816 goto setCifsAclRetry;
3821 #endif /* CONFIG_CIFS_ACL */
3823 /* Legacy Query Path Information call for lookup to old servers such
3826 SMBQueryInformation(const unsigned int xid, struct cifs_tcon *tcon,
3827 const char *search_name, FILE_ALL_INFO *data,
3828 const struct nls_table *nls_codepage, int remap)
3830 QUERY_INFORMATION_REQ *pSMB;
3831 QUERY_INFORMATION_RSP *pSMBr;
3836 cFYI(1, "In SMBQPath path %s", search_name);
3838 rc = smb_init(SMB_COM_QUERY_INFORMATION, 0, tcon, (void **) &pSMB,
3843 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3845 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3846 search_name, PATH_MAX, nls_codepage,
3848 name_len++; /* trailing null */
3851 name_len = strnlen(search_name, PATH_MAX);
3852 name_len++; /* trailing null */
3853 strncpy(pSMB->FileName, search_name, name_len);
3855 pSMB->BufferFormat = 0x04;
3856 name_len++; /* account for buffer type byte */
3857 inc_rfc1001_len(pSMB, (__u16)name_len);
3858 pSMB->ByteCount = cpu_to_le16(name_len);
3860 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3861 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3863 cFYI(1, "Send error in QueryInfo = %d", rc);
3866 __u32 time = le32_to_cpu(pSMBr->last_write_time);
3868 /* decode response */
3869 /* BB FIXME - add time zone adjustment BB */
3870 memset(data, 0, sizeof(FILE_ALL_INFO));
3873 /* decode time fields */
3874 data->ChangeTime = cpu_to_le64(cifs_UnixTimeToNT(ts));
3875 data->LastWriteTime = data->ChangeTime;
3876 data->LastAccessTime = 0;
3877 data->AllocationSize =
3878 cpu_to_le64(le32_to_cpu(pSMBr->size));
3879 data->EndOfFile = data->AllocationSize;
3881 cpu_to_le32(le16_to_cpu(pSMBr->attr));
3883 rc = -EIO; /* bad buffer passed in */
3885 cifs_buf_release(pSMB);
3894 CIFSSMBQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
3895 u16 netfid, FILE_ALL_INFO *pFindData)
3897 struct smb_t2_qfi_req *pSMB = NULL;
3898 struct smb_t2_qfi_rsp *pSMBr = NULL;
3901 __u16 params, byte_count;
3904 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3909 params = 2 /* level */ + 2 /* fid */;
3910 pSMB->t2.TotalDataCount = 0;
3911 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
3912 /* BB find exact max data count below from sess structure BB */
3913 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
3914 pSMB->t2.MaxSetupCount = 0;
3915 pSMB->t2.Reserved = 0;
3917 pSMB->t2.Timeout = 0;
3918 pSMB->t2.Reserved2 = 0;
3919 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
3921 pSMB->t2.DataCount = 0;
3922 pSMB->t2.DataOffset = 0;
3923 pSMB->t2.SetupCount = 1;
3924 pSMB->t2.Reserved3 = 0;
3925 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
3926 byte_count = params + 1 /* pad */ ;
3927 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
3928 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
3929 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
3932 inc_rfc1001_len(pSMB, byte_count);
3934 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3935 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3937 cFYI(1, "Send error in QPathInfo = %d", rc);
3938 } else { /* decode response */
3939 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
3941 if (rc) /* BB add auto retry on EOPNOTSUPP? */
3943 else if (get_bcc(&pSMBr->hdr) < 40)
3944 rc = -EIO; /* bad smb */
3945 else if (pFindData) {
3946 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3947 memcpy((char *) pFindData,
3948 (char *) &pSMBr->hdr.Protocol +
3949 data_offset, sizeof(FILE_ALL_INFO));
3953 cifs_buf_release(pSMB);
3955 goto QFileInfoRetry;
3961 CIFSSMBQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
3962 const char *search_name, FILE_ALL_INFO *data,
3963 int legacy /* old style infolevel */,
3964 const struct nls_table *nls_codepage, int remap)
3966 /* level 263 SMB_QUERY_FILE_ALL_INFO */
3967 TRANSACTION2_QPI_REQ *pSMB = NULL;
3968 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3972 __u16 params, byte_count;
3974 /* cFYI(1, "In QPathInfo path %s", search_name); */
3976 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3981 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3983 cifsConvertToUTF16((__le16 *) pSMB->FileName, search_name,
3984 PATH_MAX, nls_codepage, remap);
3985 name_len++; /* trailing null */
3987 } else { /* BB improve the check for buffer overruns BB */
3988 name_len = strnlen(search_name, PATH_MAX);
3989 name_len++; /* trailing null */
3990 strncpy(pSMB->FileName, search_name, name_len);
3993 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
3994 pSMB->TotalDataCount = 0;
3995 pSMB->MaxParameterCount = cpu_to_le16(2);
3996 /* BB find exact max SMB PDU from sess structure BB */
3997 pSMB->MaxDataCount = cpu_to_le16(4000);
3998 pSMB->MaxSetupCount = 0;
4002 pSMB->Reserved2 = 0;
4003 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4004 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
4005 pSMB->DataCount = 0;
4006 pSMB->DataOffset = 0;
4007 pSMB->SetupCount = 1;
4008 pSMB->Reserved3 = 0;
4009 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4010 byte_count = params + 1 /* pad */ ;
4011 pSMB->TotalParameterCount = cpu_to_le16(params);
4012 pSMB->ParameterCount = pSMB->TotalParameterCount;
4014 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_STANDARD);
4016 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
4017 pSMB->Reserved4 = 0;
4018 inc_rfc1001_len(pSMB, byte_count);
4019 pSMB->ByteCount = cpu_to_le16(byte_count);
4021 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4022 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4024 cFYI(1, "Send error in QPathInfo = %d", rc);
4025 } else { /* decode response */
4026 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4028 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4030 else if (!legacy && get_bcc(&pSMBr->hdr) < 40)
4031 rc = -EIO; /* bad smb */
4032 else if (legacy && get_bcc(&pSMBr->hdr) < 24)
4033 rc = -EIO; /* 24 or 26 expected but we do not read
4037 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4040 * On legacy responses we do not read the last field,
4041 * EAsize, fortunately since it varies by subdialect and
4042 * also note it differs on Set vs Get, ie two bytes or 4
4043 * bytes depending but we don't care here.
4046 size = sizeof(FILE_INFO_STANDARD);
4048 size = sizeof(FILE_ALL_INFO);
4049 memcpy((char *) data, (char *) &pSMBr->hdr.Protocol +
4054 cifs_buf_release(pSMB);
4056 goto QPathInfoRetry;
4062 CIFSSMBUnixQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
4063 u16 netfid, FILE_UNIX_BASIC_INFO *pFindData)
4065 struct smb_t2_qfi_req *pSMB = NULL;
4066 struct smb_t2_qfi_rsp *pSMBr = NULL;
4069 __u16 params, byte_count;
4072 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4077 params = 2 /* level */ + 2 /* fid */;
4078 pSMB->t2.TotalDataCount = 0;
4079 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4080 /* BB find exact max data count below from sess structure BB */
4081 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4082 pSMB->t2.MaxSetupCount = 0;
4083 pSMB->t2.Reserved = 0;
4085 pSMB->t2.Timeout = 0;
4086 pSMB->t2.Reserved2 = 0;
4087 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4089 pSMB->t2.DataCount = 0;
4090 pSMB->t2.DataOffset = 0;
4091 pSMB->t2.SetupCount = 1;
4092 pSMB->t2.Reserved3 = 0;
4093 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4094 byte_count = params + 1 /* pad */ ;
4095 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4096 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4097 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4100 inc_rfc1001_len(pSMB, byte_count);
4102 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4103 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4105 cFYI(1, "Send error in QPathInfo = %d", rc);
4106 } else { /* decode response */
4107 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4109 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
4110 cERROR(1, "Malformed FILE_UNIX_BASIC_INFO response. "
4111 "Unix Extensions can be disabled on mount "
4112 "by specifying the nosfu mount option.");
4113 rc = -EIO; /* bad smb */
4115 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4116 memcpy((char *) pFindData,
4117 (char *) &pSMBr->hdr.Protocol +
4119 sizeof(FILE_UNIX_BASIC_INFO));
4123 cifs_buf_release(pSMB);
4125 goto UnixQFileInfoRetry;
4131 CIFSSMBUnixQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
4132 const unsigned char *searchName,
4133 FILE_UNIX_BASIC_INFO *pFindData,
4134 const struct nls_table *nls_codepage, int remap)
4136 /* SMB_QUERY_FILE_UNIX_BASIC */
4137 TRANSACTION2_QPI_REQ *pSMB = NULL;
4138 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4140 int bytes_returned = 0;
4142 __u16 params, byte_count;
4144 cFYI(1, "In QPathInfo (Unix) the path %s", searchName);
4146 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4151 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4153 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4154 PATH_MAX, nls_codepage, remap);
4155 name_len++; /* trailing null */
4157 } else { /* BB improve the check for buffer overruns BB */
4158 name_len = strnlen(searchName, PATH_MAX);
4159 name_len++; /* trailing null */
4160 strncpy(pSMB->FileName, searchName, name_len);
4163 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
4164 pSMB->TotalDataCount = 0;
4165 pSMB->MaxParameterCount = cpu_to_le16(2);
4166 /* BB find exact max SMB PDU from sess structure BB */
4167 pSMB->MaxDataCount = cpu_to_le16(4000);
4168 pSMB->MaxSetupCount = 0;
4172 pSMB->Reserved2 = 0;
4173 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4174 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
4175 pSMB->DataCount = 0;
4176 pSMB->DataOffset = 0;
4177 pSMB->SetupCount = 1;
4178 pSMB->Reserved3 = 0;
4179 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4180 byte_count = params + 1 /* pad */ ;
4181 pSMB->TotalParameterCount = cpu_to_le16(params);
4182 pSMB->ParameterCount = pSMB->TotalParameterCount;
4183 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4184 pSMB->Reserved4 = 0;
4185 inc_rfc1001_len(pSMB, byte_count);
4186 pSMB->ByteCount = cpu_to_le16(byte_count);
4188 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4189 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4191 cFYI(1, "Send error in QPathInfo = %d", rc);
4192 } else { /* decode response */
4193 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4195 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
4196 cERROR(1, "Malformed FILE_UNIX_BASIC_INFO response. "
4197 "Unix Extensions can be disabled on mount "
4198 "by specifying the nosfu mount option.");
4199 rc = -EIO; /* bad smb */
4201 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4202 memcpy((char *) pFindData,
4203 (char *) &pSMBr->hdr.Protocol +
4205 sizeof(FILE_UNIX_BASIC_INFO));
4208 cifs_buf_release(pSMB);
4210 goto UnixQPathInfoRetry;
4215 /* xid, tcon, searchName and codepage are input parms, rest are returned */
4217 CIFSFindFirst(const unsigned int xid, struct cifs_tcon *tcon,
4218 const char *searchName,
4219 const struct nls_table *nls_codepage,
4220 __u16 *pnetfid, __u16 search_flags,
4221 struct cifs_search_info *psrch_inf, int remap, const char dirsep)
4223 /* level 257 SMB_ */
4224 TRANSACTION2_FFIRST_REQ *pSMB = NULL;
4225 TRANSACTION2_FFIRST_RSP *pSMBr = NULL;
4226 T2_FFIRST_RSP_PARMS *parms;
4228 int bytes_returned = 0;
4230 __u16 params, byte_count;
4232 cFYI(1, "In FindFirst for %s", searchName);
4235 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4240 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4242 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4243 PATH_MAX, nls_codepage, remap);
4244 /* We can not add the asterik earlier in case
4245 it got remapped to 0xF03A as if it were part of the
4246 directory name instead of a wildcard */
4248 pSMB->FileName[name_len] = dirsep;
4249 pSMB->FileName[name_len+1] = 0;
4250 pSMB->FileName[name_len+2] = '*';
4251 pSMB->FileName[name_len+3] = 0;
4252 name_len += 4; /* now the trailing null */
4253 pSMB->FileName[name_len] = 0; /* null terminate just in case */
4254 pSMB->FileName[name_len+1] = 0;
4256 } else { /* BB add check for overrun of SMB buf BB */
4257 name_len = strnlen(searchName, PATH_MAX);
4258 /* BB fix here and in unicode clause above ie
4259 if (name_len > buffersize-header)
4260 free buffer exit; BB */
4261 strncpy(pSMB->FileName, searchName, name_len);
4262 pSMB->FileName[name_len] = dirsep;
4263 pSMB->FileName[name_len+1] = '*';
4264 pSMB->FileName[name_len+2] = 0;
4268 params = 12 + name_len /* includes null */ ;
4269 pSMB->TotalDataCount = 0; /* no EAs */
4270 pSMB->MaxParameterCount = cpu_to_le16(10);
4271 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
4272 pSMB->MaxSetupCount = 0;
4276 pSMB->Reserved2 = 0;
4277 byte_count = params + 1 /* pad */ ;
4278 pSMB->TotalParameterCount = cpu_to_le16(params);
4279 pSMB->ParameterCount = pSMB->TotalParameterCount;
4280 pSMB->ParameterOffset = cpu_to_le16(
4281 offsetof(struct smb_com_transaction2_ffirst_req, SearchAttributes)
4283 pSMB->DataCount = 0;
4284 pSMB->DataOffset = 0;
4285 pSMB->SetupCount = 1; /* one byte, no need to make endian neutral */
4286 pSMB->Reserved3 = 0;
4287 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_FIRST);
4288 pSMB->SearchAttributes =
4289 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
4291 pSMB->SearchCount = cpu_to_le16(CIFSMaxBufSize/sizeof(FILE_UNIX_INFO));
4292 pSMB->SearchFlags = cpu_to_le16(search_flags);
4293 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4295 /* BB what should we set StorageType to? Does it matter? BB */
4296 pSMB->SearchStorageType = 0;
4297 inc_rfc1001_len(pSMB, byte_count);
4298 pSMB->ByteCount = cpu_to_le16(byte_count);
4300 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4301 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4302 cifs_stats_inc(&tcon->stats.cifs_stats.num_ffirst);
4304 if (rc) {/* BB add logic to retry regular search if Unix search
4305 rejected unexpectedly by server */
4306 /* BB Add code to handle unsupported level rc */
4307 cFYI(1, "Error in FindFirst = %d", rc);
4309 cifs_buf_release(pSMB);
4311 /* BB eventually could optimize out free and realloc of buf */
4314 goto findFirstRetry;
4315 } else { /* decode response */
4316 /* BB remember to free buffer if error BB */
4317 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4321 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
4322 psrch_inf->unicode = true;
4324 psrch_inf->unicode = false;
4326 psrch_inf->ntwrk_buf_start = (char *)pSMBr;
4327 psrch_inf->smallBuf = 0;
4328 psrch_inf->srch_entries_start =
4329 (char *) &pSMBr->hdr.Protocol +
4330 le16_to_cpu(pSMBr->t2.DataOffset);
4331 parms = (T2_FFIRST_RSP_PARMS *)((char *) &pSMBr->hdr.Protocol +
4332 le16_to_cpu(pSMBr->t2.ParameterOffset));
4334 if (parms->EndofSearch)
4335 psrch_inf->endOfSearch = true;
4337 psrch_inf->endOfSearch = false;
4339 psrch_inf->entries_in_buffer =
4340 le16_to_cpu(parms->SearchCount);
4341 psrch_inf->index_of_last_entry = 2 /* skip . and .. */ +
4342 psrch_inf->entries_in_buffer;
4343 lnoff = le16_to_cpu(parms->LastNameOffset);
4344 if (CIFSMaxBufSize < lnoff) {
4345 cERROR(1, "ignoring corrupt resume name");
4346 psrch_inf->last_entry = NULL;
4350 psrch_inf->last_entry = psrch_inf->srch_entries_start +
4353 *pnetfid = parms->SearchHandle;
4355 cifs_buf_release(pSMB);
4362 int CIFSFindNext(const unsigned int xid, struct cifs_tcon *tcon,
4363 __u16 searchHandle, __u16 search_flags,
4364 struct cifs_search_info *psrch_inf)
4366 TRANSACTION2_FNEXT_REQ *pSMB = NULL;
4367 TRANSACTION2_FNEXT_RSP *pSMBr = NULL;
4368 T2_FNEXT_RSP_PARMS *parms;
4369 char *response_data;
4372 unsigned int name_len;
4373 __u16 params, byte_count;
4375 cFYI(1, "In FindNext");
4377 if (psrch_inf->endOfSearch)
4380 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4385 params = 14; /* includes 2 bytes of null string, converted to LE below*/
4387 pSMB->TotalDataCount = 0; /* no EAs */
4388 pSMB->MaxParameterCount = cpu_to_le16(8);
4389 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
4390 pSMB->MaxSetupCount = 0;
4394 pSMB->Reserved2 = 0;
4395 pSMB->ParameterOffset = cpu_to_le16(
4396 offsetof(struct smb_com_transaction2_fnext_req,SearchHandle) - 4);
4397 pSMB->DataCount = 0;
4398 pSMB->DataOffset = 0;
4399 pSMB->SetupCount = 1;
4400 pSMB->Reserved3 = 0;
4401 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_NEXT);
4402 pSMB->SearchHandle = searchHandle; /* always kept as le */
4404 cpu_to_le16(CIFSMaxBufSize / sizeof(FILE_UNIX_INFO));
4405 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4406 pSMB->ResumeKey = psrch_inf->resume_key;
4407 pSMB->SearchFlags = cpu_to_le16(search_flags);
4409 name_len = psrch_inf->resume_name_len;
4411 if (name_len < PATH_MAX) {
4412 memcpy(pSMB->ResumeFileName, psrch_inf->presume_name, name_len);
4413 byte_count += name_len;
4414 /* 14 byte parm len above enough for 2 byte null terminator */
4415 pSMB->ResumeFileName[name_len] = 0;
4416 pSMB->ResumeFileName[name_len+1] = 0;
4419 goto FNext2_err_exit;
4421 byte_count = params + 1 /* pad */ ;
4422 pSMB->TotalParameterCount = cpu_to_le16(params);
4423 pSMB->ParameterCount = pSMB->TotalParameterCount;
4424 inc_rfc1001_len(pSMB, byte_count);
4425 pSMB->ByteCount = cpu_to_le16(byte_count);
4427 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4428 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4429 cifs_stats_inc(&tcon->stats.cifs_stats.num_fnext);
4432 psrch_inf->endOfSearch = true;
4433 cifs_buf_release(pSMB);
4434 rc = 0; /* search probably was closed at end of search*/
4436 cFYI(1, "FindNext returned = %d", rc);
4437 } else { /* decode response */
4438 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4443 /* BB fixme add lock for file (srch_info) struct here */
4444 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
4445 psrch_inf->unicode = true;
4447 psrch_inf->unicode = false;
4448 response_data = (char *) &pSMBr->hdr.Protocol +
4449 le16_to_cpu(pSMBr->t2.ParameterOffset);
4450 parms = (T2_FNEXT_RSP_PARMS *)response_data;
4451 response_data = (char *)&pSMBr->hdr.Protocol +
4452 le16_to_cpu(pSMBr->t2.DataOffset);
4453 if (psrch_inf->smallBuf)
4454 cifs_small_buf_release(
4455 psrch_inf->ntwrk_buf_start);
4457 cifs_buf_release(psrch_inf->ntwrk_buf_start);
4458 psrch_inf->srch_entries_start = response_data;
4459 psrch_inf->ntwrk_buf_start = (char *)pSMB;
4460 psrch_inf->smallBuf = 0;
4461 if (parms->EndofSearch)
4462 psrch_inf->endOfSearch = true;
4464 psrch_inf->endOfSearch = false;
4465 psrch_inf->entries_in_buffer =
4466 le16_to_cpu(parms->SearchCount);
4467 psrch_inf->index_of_last_entry +=
4468 psrch_inf->entries_in_buffer;
4469 lnoff = le16_to_cpu(parms->LastNameOffset);
4470 if (CIFSMaxBufSize < lnoff) {
4471 cERROR(1, "ignoring corrupt resume name");
4472 psrch_inf->last_entry = NULL;
4475 psrch_inf->last_entry =
4476 psrch_inf->srch_entries_start + lnoff;
4478 /* cFYI(1, "fnxt2 entries in buf %d index_of_last %d",
4479 psrch_inf->entries_in_buffer, psrch_inf->index_of_last_entry); */
4481 /* BB fixme add unlock here */
4486 /* BB On error, should we leave previous search buf (and count and
4487 last entry fields) intact or free the previous one? */
4489 /* Note: On -EAGAIN error only caller can retry on handle based calls
4490 since file handle passed in no longer valid */
4493 cifs_buf_release(pSMB);
4498 CIFSFindClose(const unsigned int xid, struct cifs_tcon *tcon,
4499 const __u16 searchHandle)
4502 FINDCLOSE_REQ *pSMB = NULL;
4504 cFYI(1, "In CIFSSMBFindClose");
4505 rc = small_smb_init(SMB_COM_FIND_CLOSE2, 1, tcon, (void **)&pSMB);
4507 /* no sense returning error if session restarted
4508 as file handle has been closed */
4514 pSMB->FileID = searchHandle;
4515 pSMB->ByteCount = 0;
4516 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
4518 cERROR(1, "Send error in FindClose = %d", rc);
4520 cifs_stats_inc(&tcon->stats.cifs_stats.num_fclose);
4522 /* Since session is dead, search handle closed on server already */
4530 CIFSGetSrvInodeNumber(const unsigned int xid, struct cifs_tcon *tcon,
4531 const char *search_name, __u64 *inode_number,
4532 const struct nls_table *nls_codepage, int remap)
4535 TRANSACTION2_QPI_REQ *pSMB = NULL;
4536 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4537 int name_len, bytes_returned;
4538 __u16 params, byte_count;
4540 cFYI(1, "In GetSrvInodeNum for %s", search_name);
4544 GetInodeNumberRetry:
4545 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4550 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4552 cifsConvertToUTF16((__le16 *) pSMB->FileName,
4553 search_name, PATH_MAX, nls_codepage,
4555 name_len++; /* trailing null */
4557 } else { /* BB improve the check for buffer overruns BB */
4558 name_len = strnlen(search_name, PATH_MAX);
4559 name_len++; /* trailing null */
4560 strncpy(pSMB->FileName, search_name, name_len);
4563 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
4564 pSMB->TotalDataCount = 0;
4565 pSMB->MaxParameterCount = cpu_to_le16(2);
4566 /* BB find exact max data count below from sess structure BB */
4567 pSMB->MaxDataCount = cpu_to_le16(4000);
4568 pSMB->MaxSetupCount = 0;
4572 pSMB->Reserved2 = 0;
4573 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4574 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
4575 pSMB->DataCount = 0;
4576 pSMB->DataOffset = 0;
4577 pSMB->SetupCount = 1;
4578 pSMB->Reserved3 = 0;
4579 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4580 byte_count = params + 1 /* pad */ ;
4581 pSMB->TotalParameterCount = cpu_to_le16(params);
4582 pSMB->ParameterCount = pSMB->TotalParameterCount;
4583 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_INTERNAL_INFO);
4584 pSMB->Reserved4 = 0;
4585 inc_rfc1001_len(pSMB, byte_count);
4586 pSMB->ByteCount = cpu_to_le16(byte_count);
4588 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4589 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4591 cFYI(1, "error %d in QueryInternalInfo", rc);
4593 /* decode response */
4594 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4595 /* BB also check enough total bytes returned */
4596 if (rc || get_bcc(&pSMBr->hdr) < 2)
4597 /* If rc should we check for EOPNOSUPP and
4598 disable the srvino flag? or in caller? */
4599 rc = -EIO; /* bad smb */
4601 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4602 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
4603 struct file_internal_info *pfinfo;
4604 /* BB Do we need a cast or hash here ? */
4606 cFYI(1, "Illegal size ret in QryIntrnlInf");
4608 goto GetInodeNumOut;
4610 pfinfo = (struct file_internal_info *)
4611 (data_offset + (char *) &pSMBr->hdr.Protocol);
4612 *inode_number = le64_to_cpu(pfinfo->UniqueId);
4616 cifs_buf_release(pSMB);
4618 goto GetInodeNumberRetry;
4622 /* parses DFS refferal V3 structure
4623 * caller is responsible for freeing target_nodes
4626 * on failure - errno
4629 parse_DFS_referrals(TRANSACTION2_GET_DFS_REFER_RSP *pSMBr,
4630 unsigned int *num_of_nodes,
4631 struct dfs_info3_param **target_nodes,
4632 const struct nls_table *nls_codepage, int remap,
4633 const char *searchName)
4638 struct dfs_referral_level_3 *ref;
4640 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
4644 *num_of_nodes = le16_to_cpu(pSMBr->NumberOfReferrals);
4646 if (*num_of_nodes < 1) {
4647 cERROR(1, "num_referrals: must be at least > 0,"
4648 "but we get num_referrals = %d", *num_of_nodes);
4650 goto parse_DFS_referrals_exit;
4653 ref = (struct dfs_referral_level_3 *) &(pSMBr->referrals);
4654 if (ref->VersionNumber != cpu_to_le16(3)) {
4655 cERROR(1, "Referrals of V%d version are not supported,"
4656 "should be V3", le16_to_cpu(ref->VersionNumber));
4658 goto parse_DFS_referrals_exit;
4661 /* get the upper boundary of the resp buffer */
4662 data_end = (char *)(&(pSMBr->PathConsumed)) +
4663 le16_to_cpu(pSMBr->t2.DataCount);
4665 cFYI(1, "num_referrals: %d dfs flags: 0x%x ...",
4667 le32_to_cpu(pSMBr->DFSFlags));
4669 *target_nodes = kzalloc(sizeof(struct dfs_info3_param) *
4670 *num_of_nodes, GFP_KERNEL);
4671 if (*target_nodes == NULL) {
4672 cERROR(1, "Failed to allocate buffer for target_nodes");
4674 goto parse_DFS_referrals_exit;
4677 /* collect necessary data from referrals */
4678 for (i = 0; i < *num_of_nodes; i++) {
4681 struct dfs_info3_param *node = (*target_nodes)+i;
4683 node->flags = le32_to_cpu(pSMBr->DFSFlags);
4685 __le16 *tmp = kmalloc(strlen(searchName)*2 + 2,
4689 goto parse_DFS_referrals_exit;
4691 cifsConvertToUTF16((__le16 *) tmp, searchName,
4692 PATH_MAX, nls_codepage, remap);
4693 node->path_consumed = cifs_utf16_bytes(tmp,
4694 le16_to_cpu(pSMBr->PathConsumed),
4698 node->path_consumed = le16_to_cpu(pSMBr->PathConsumed);
4700 node->server_type = le16_to_cpu(ref->ServerType);
4701 node->ref_flag = le16_to_cpu(ref->ReferralEntryFlags);
4704 temp = (char *)ref + le16_to_cpu(ref->DfsPathOffset);
4705 max_len = data_end - temp;
4706 node->path_name = cifs_strndup_from_utf16(temp, max_len,
4707 is_unicode, nls_codepage);
4708 if (!node->path_name) {
4710 goto parse_DFS_referrals_exit;
4713 /* copy link target UNC */
4714 temp = (char *)ref + le16_to_cpu(ref->NetworkAddressOffset);
4715 max_len = data_end - temp;
4716 node->node_name = cifs_strndup_from_utf16(temp, max_len,
4717 is_unicode, nls_codepage);
4718 if (!node->node_name) {
4720 goto parse_DFS_referrals_exit;
4726 parse_DFS_referrals_exit:
4728 free_dfs_info_array(*target_nodes, *num_of_nodes);
4729 *target_nodes = NULL;
4736 CIFSGetDFSRefer(const unsigned int xid, struct cifs_ses *ses,
4737 const char *search_name, struct dfs_info3_param **target_nodes,
4738 unsigned int *num_of_nodes,
4739 const struct nls_table *nls_codepage, int remap)
4741 /* TRANS2_GET_DFS_REFERRAL */
4742 TRANSACTION2_GET_DFS_REFER_REQ *pSMB = NULL;
4743 TRANSACTION2_GET_DFS_REFER_RSP *pSMBr = NULL;
4747 __u16 params, byte_count;
4749 *target_nodes = NULL;
4751 cFYI(1, "In GetDFSRefer the path %s", search_name);
4755 rc = smb_init(SMB_COM_TRANSACTION2, 15, NULL, (void **) &pSMB,
4760 /* server pointer checked in called function,
4761 but should never be null here anyway */
4762 pSMB->hdr.Mid = get_next_mid(ses->server);
4763 pSMB->hdr.Tid = ses->ipc_tid;
4764 pSMB->hdr.Uid = ses->Suid;
4765 if (ses->capabilities & CAP_STATUS32)
4766 pSMB->hdr.Flags2 |= SMBFLG2_ERR_STATUS;
4767 if (ses->capabilities & CAP_DFS)
4768 pSMB->hdr.Flags2 |= SMBFLG2_DFS;
4770 if (ses->capabilities & CAP_UNICODE) {
4771 pSMB->hdr.Flags2 |= SMBFLG2_UNICODE;
4773 cifsConvertToUTF16((__le16 *) pSMB->RequestFileName,
4774 search_name, PATH_MAX, nls_codepage,
4776 name_len++; /* trailing null */
4778 } else { /* BB improve the check for buffer overruns BB */
4779 name_len = strnlen(search_name, PATH_MAX);
4780 name_len++; /* trailing null */
4781 strncpy(pSMB->RequestFileName, search_name, name_len);
4785 if (ses->server->sec_mode &
4786 (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
4787 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
4790 pSMB->hdr.Uid = ses->Suid;
4792 params = 2 /* level */ + name_len /*includes null */ ;
4793 pSMB->TotalDataCount = 0;
4794 pSMB->DataCount = 0;
4795 pSMB->DataOffset = 0;
4796 pSMB->MaxParameterCount = 0;
4797 /* BB find exact max SMB PDU from sess structure BB */
4798 pSMB->MaxDataCount = cpu_to_le16(4000);
4799 pSMB->MaxSetupCount = 0;
4803 pSMB->Reserved2 = 0;
4804 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4805 struct smb_com_transaction2_get_dfs_refer_req, MaxReferralLevel) - 4);
4806 pSMB->SetupCount = 1;
4807 pSMB->Reserved3 = 0;
4808 pSMB->SubCommand = cpu_to_le16(TRANS2_GET_DFS_REFERRAL);
4809 byte_count = params + 3 /* pad */ ;
4810 pSMB->ParameterCount = cpu_to_le16(params);
4811 pSMB->TotalParameterCount = pSMB->ParameterCount;
4812 pSMB->MaxReferralLevel = cpu_to_le16(3);
4813 inc_rfc1001_len(pSMB, byte_count);
4814 pSMB->ByteCount = cpu_to_le16(byte_count);
4816 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
4817 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4819 cFYI(1, "Send error in GetDFSRefer = %d", rc);
4822 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4824 /* BB Also check if enough total bytes returned? */
4825 if (rc || get_bcc(&pSMBr->hdr) < 17) {
4826 rc = -EIO; /* bad smb */
4830 cFYI(1, "Decoding GetDFSRefer response BCC: %d Offset %d",
4831 get_bcc(&pSMBr->hdr),
4832 le16_to_cpu(pSMBr->t2.DataOffset));
4834 /* parse returned result into more usable form */
4835 rc = parse_DFS_referrals(pSMBr, num_of_nodes,
4836 target_nodes, nls_codepage, remap,
4840 cifs_buf_release(pSMB);
4848 /* Query File System Info such as free space to old servers such as Win 9x */
4850 SMBOldQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
4851 struct kstatfs *FSData)
4853 /* level 0x01 SMB_QUERY_FILE_SYSTEM_INFO */
4854 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4855 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4856 FILE_SYSTEM_ALLOC_INFO *response_data;
4858 int bytes_returned = 0;
4859 __u16 params, byte_count;
4861 cFYI(1, "OldQFSInfo");
4863 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4868 params = 2; /* level */
4869 pSMB->TotalDataCount = 0;
4870 pSMB->MaxParameterCount = cpu_to_le16(2);
4871 pSMB->MaxDataCount = cpu_to_le16(1000);
4872 pSMB->MaxSetupCount = 0;
4876 pSMB->Reserved2 = 0;
4877 byte_count = params + 1 /* pad */ ;
4878 pSMB->TotalParameterCount = cpu_to_le16(params);
4879 pSMB->ParameterCount = pSMB->TotalParameterCount;
4880 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4881 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
4882 pSMB->DataCount = 0;
4883 pSMB->DataOffset = 0;
4884 pSMB->SetupCount = 1;
4885 pSMB->Reserved3 = 0;
4886 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
4887 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_ALLOCATION);
4888 inc_rfc1001_len(pSMB, byte_count);
4889 pSMB->ByteCount = cpu_to_le16(byte_count);
4891 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4892 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4894 cFYI(1, "Send error in QFSInfo = %d", rc);
4895 } else { /* decode response */
4896 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4898 if (rc || get_bcc(&pSMBr->hdr) < 18)
4899 rc = -EIO; /* bad smb */
4901 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4902 cFYI(1, "qfsinf resp BCC: %d Offset %d",
4903 get_bcc(&pSMBr->hdr), data_offset);
4905 response_data = (FILE_SYSTEM_ALLOC_INFO *)
4906 (((char *) &pSMBr->hdr.Protocol) + data_offset);
4908 le16_to_cpu(response_data->BytesPerSector) *
4909 le32_to_cpu(response_data->
4910 SectorsPerAllocationUnit);
4912 le32_to_cpu(response_data->TotalAllocationUnits);
4913 FSData->f_bfree = FSData->f_bavail =
4914 le32_to_cpu(response_data->FreeAllocationUnits);
4915 cFYI(1, "Blocks: %lld Free: %lld Block size %ld",
4916 (unsigned long long)FSData->f_blocks,
4917 (unsigned long long)FSData->f_bfree,
4921 cifs_buf_release(pSMB);
4924 goto oldQFSInfoRetry;
4930 CIFSSMBQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
4931 struct kstatfs *FSData)
4933 /* level 0x103 SMB_QUERY_FILE_SYSTEM_INFO */
4934 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4935 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4936 FILE_SYSTEM_INFO *response_data;
4938 int bytes_returned = 0;
4939 __u16 params, byte_count;
4941 cFYI(1, "In QFSInfo");
4943 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4948 params = 2; /* level */
4949 pSMB->TotalDataCount = 0;
4950 pSMB->MaxParameterCount = cpu_to_le16(2);
4951 pSMB->MaxDataCount = cpu_to_le16(1000);
4952 pSMB->MaxSetupCount = 0;
4956 pSMB->Reserved2 = 0;
4957 byte_count = params + 1 /* pad */ ;
4958 pSMB->TotalParameterCount = cpu_to_le16(params);
4959 pSMB->ParameterCount = pSMB->TotalParameterCount;
4960 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4961 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
4962 pSMB->DataCount = 0;
4963 pSMB->DataOffset = 0;
4964 pSMB->SetupCount = 1;
4965 pSMB->Reserved3 = 0;
4966 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
4967 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_SIZE_INFO);
4968 inc_rfc1001_len(pSMB, byte_count);
4969 pSMB->ByteCount = cpu_to_le16(byte_count);
4971 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4972 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4974 cFYI(1, "Send error in QFSInfo = %d", rc);
4975 } else { /* decode response */
4976 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4978 if (rc || get_bcc(&pSMBr->hdr) < 24)
4979 rc = -EIO; /* bad smb */
4981 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4985 *) (((char *) &pSMBr->hdr.Protocol) +
4988 le32_to_cpu(response_data->BytesPerSector) *
4989 le32_to_cpu(response_data->
4990 SectorsPerAllocationUnit);
4992 le64_to_cpu(response_data->TotalAllocationUnits);
4993 FSData->f_bfree = FSData->f_bavail =
4994 le64_to_cpu(response_data->FreeAllocationUnits);
4995 cFYI(1, "Blocks: %lld Free: %lld Block size %ld",
4996 (unsigned long long)FSData->f_blocks,
4997 (unsigned long long)FSData->f_bfree,
5001 cifs_buf_release(pSMB);
5010 CIFSSMBQFSAttributeInfo(const unsigned int xid, struct cifs_tcon *tcon)
5012 /* level 0x105 SMB_QUERY_FILE_SYSTEM_INFO */
5013 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5014 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5015 FILE_SYSTEM_ATTRIBUTE_INFO *response_data;
5017 int bytes_returned = 0;
5018 __u16 params, byte_count;
5020 cFYI(1, "In QFSAttributeInfo");
5022 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5027 params = 2; /* level */
5028 pSMB->TotalDataCount = 0;
5029 pSMB->MaxParameterCount = cpu_to_le16(2);
5030 /* BB find exact max SMB PDU from sess structure BB */
5031 pSMB->MaxDataCount = cpu_to_le16(1000);
5032 pSMB->MaxSetupCount = 0;
5036 pSMB->Reserved2 = 0;
5037 byte_count = params + 1 /* pad */ ;
5038 pSMB->TotalParameterCount = cpu_to_le16(params);
5039 pSMB->ParameterCount = pSMB->TotalParameterCount;
5040 pSMB->ParameterOffset = cpu_to_le16(offsetof(
5041 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
5042 pSMB->DataCount = 0;
5043 pSMB->DataOffset = 0;
5044 pSMB->SetupCount = 1;
5045 pSMB->Reserved3 = 0;
5046 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5047 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_ATTRIBUTE_INFO);
5048 inc_rfc1001_len(pSMB, byte_count);
5049 pSMB->ByteCount = cpu_to_le16(byte_count);
5051 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5052 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5054 cERROR(1, "Send error in QFSAttributeInfo = %d", rc);
5055 } else { /* decode response */
5056 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5058 if (rc || get_bcc(&pSMBr->hdr) < 13) {
5059 /* BB also check if enough bytes returned */
5060 rc = -EIO; /* bad smb */
5062 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5064 (FILE_SYSTEM_ATTRIBUTE_INFO
5065 *) (((char *) &pSMBr->hdr.Protocol) +
5067 memcpy(&tcon->fsAttrInfo, response_data,
5068 sizeof(FILE_SYSTEM_ATTRIBUTE_INFO));
5071 cifs_buf_release(pSMB);
5074 goto QFSAttributeRetry;
5080 CIFSSMBQFSDeviceInfo(const unsigned int xid, struct cifs_tcon *tcon)
5082 /* level 0x104 SMB_QUERY_FILE_SYSTEM_INFO */
5083 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5084 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5085 FILE_SYSTEM_DEVICE_INFO *response_data;
5087 int bytes_returned = 0;
5088 __u16 params, byte_count;
5090 cFYI(1, "In QFSDeviceInfo");
5092 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5097 params = 2; /* level */
5098 pSMB->TotalDataCount = 0;
5099 pSMB->MaxParameterCount = cpu_to_le16(2);
5100 /* BB find exact max SMB PDU from sess structure BB */
5101 pSMB->MaxDataCount = cpu_to_le16(1000);
5102 pSMB->MaxSetupCount = 0;
5106 pSMB->Reserved2 = 0;
5107 byte_count = params + 1 /* pad */ ;
5108 pSMB->TotalParameterCount = cpu_to_le16(params);
5109 pSMB->ParameterCount = pSMB->TotalParameterCount;
5110 pSMB->ParameterOffset = cpu_to_le16(offsetof(
5111 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
5113 pSMB->DataCount = 0;
5114 pSMB->DataOffset = 0;
5115 pSMB->SetupCount = 1;
5116 pSMB->Reserved3 = 0;
5117 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5118 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_DEVICE_INFO);
5119 inc_rfc1001_len(pSMB, byte_count);
5120 pSMB->ByteCount = cpu_to_le16(byte_count);
5122 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5123 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5125 cFYI(1, "Send error in QFSDeviceInfo = %d", rc);
5126 } else { /* decode response */
5127 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5129 if (rc || get_bcc(&pSMBr->hdr) <
5130 sizeof(FILE_SYSTEM_DEVICE_INFO))
5131 rc = -EIO; /* bad smb */
5133 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5135 (FILE_SYSTEM_DEVICE_INFO *)
5136 (((char *) &pSMBr->hdr.Protocol) +
5138 memcpy(&tcon->fsDevInfo, response_data,
5139 sizeof(FILE_SYSTEM_DEVICE_INFO));
5142 cifs_buf_release(pSMB);
5145 goto QFSDeviceRetry;
5151 CIFSSMBQFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon)
5153 /* level 0x200 SMB_QUERY_CIFS_UNIX_INFO */
5154 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5155 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5156 FILE_SYSTEM_UNIX_INFO *response_data;
5158 int bytes_returned = 0;
5159 __u16 params, byte_count;
5161 cFYI(1, "In QFSUnixInfo");
5163 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5164 (void **) &pSMB, (void **) &pSMBr);
5168 params = 2; /* level */
5169 pSMB->TotalDataCount = 0;
5170 pSMB->DataCount = 0;
5171 pSMB->DataOffset = 0;
5172 pSMB->MaxParameterCount = cpu_to_le16(2);
5173 /* BB find exact max SMB PDU from sess structure BB */
5174 pSMB->MaxDataCount = cpu_to_le16(100);
5175 pSMB->MaxSetupCount = 0;
5179 pSMB->Reserved2 = 0;
5180 byte_count = params + 1 /* pad */ ;
5181 pSMB->ParameterCount = cpu_to_le16(params);
5182 pSMB->TotalParameterCount = pSMB->ParameterCount;
5183 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5184 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
5185 pSMB->SetupCount = 1;
5186 pSMB->Reserved3 = 0;
5187 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5188 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_CIFS_UNIX_INFO);
5189 inc_rfc1001_len(pSMB, byte_count);
5190 pSMB->ByteCount = cpu_to_le16(byte_count);
5192 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5193 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5195 cERROR(1, "Send error in QFSUnixInfo = %d", rc);
5196 } else { /* decode response */
5197 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5199 if (rc || get_bcc(&pSMBr->hdr) < 13) {
5200 rc = -EIO; /* bad smb */
5202 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5204 (FILE_SYSTEM_UNIX_INFO
5205 *) (((char *) &pSMBr->hdr.Protocol) +
5207 memcpy(&tcon->fsUnixInfo, response_data,
5208 sizeof(FILE_SYSTEM_UNIX_INFO));
5211 cifs_buf_release(pSMB);
5221 CIFSSMBSetFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon, __u64 cap)
5223 /* level 0x200 SMB_SET_CIFS_UNIX_INFO */
5224 TRANSACTION2_SETFSI_REQ *pSMB = NULL;
5225 TRANSACTION2_SETFSI_RSP *pSMBr = NULL;
5227 int bytes_returned = 0;
5228 __u16 params, param_offset, offset, byte_count;
5230 cFYI(1, "In SETFSUnixInfo");
5232 /* BB switch to small buf init to save memory */
5233 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5234 (void **) &pSMB, (void **) &pSMBr);
5238 params = 4; /* 2 bytes zero followed by info level. */
5239 pSMB->MaxSetupCount = 0;
5243 pSMB->Reserved2 = 0;
5244 param_offset = offsetof(struct smb_com_transaction2_setfsi_req, FileNum)
5246 offset = param_offset + params;
5248 pSMB->MaxParameterCount = cpu_to_le16(4);
5249 /* BB find exact max SMB PDU from sess structure BB */
5250 pSMB->MaxDataCount = cpu_to_le16(100);
5251 pSMB->SetupCount = 1;
5252 pSMB->Reserved3 = 0;
5253 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FS_INFORMATION);
5254 byte_count = 1 /* pad */ + params + 12;
5256 pSMB->DataCount = cpu_to_le16(12);
5257 pSMB->ParameterCount = cpu_to_le16(params);
5258 pSMB->TotalDataCount = pSMB->DataCount;
5259 pSMB->TotalParameterCount = pSMB->ParameterCount;
5260 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5261 pSMB->DataOffset = cpu_to_le16(offset);
5265 pSMB->InformationLevel = cpu_to_le16(SMB_SET_CIFS_UNIX_INFO);
5268 pSMB->ClientUnixMajor = cpu_to_le16(CIFS_UNIX_MAJOR_VERSION);
5269 pSMB->ClientUnixMinor = cpu_to_le16(CIFS_UNIX_MINOR_VERSION);
5270 pSMB->ClientUnixCap = cpu_to_le64(cap);
5272 inc_rfc1001_len(pSMB, byte_count);
5273 pSMB->ByteCount = cpu_to_le16(byte_count);
5275 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5276 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5278 cERROR(1, "Send error in SETFSUnixInfo = %d", rc);
5279 } else { /* decode response */
5280 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5282 rc = -EIO; /* bad smb */
5284 cifs_buf_release(pSMB);
5287 goto SETFSUnixRetry;
5295 CIFSSMBQFSPosixInfo(const unsigned int xid, struct cifs_tcon *tcon,
5296 struct kstatfs *FSData)
5298 /* level 0x201 SMB_QUERY_CIFS_POSIX_INFO */
5299 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5300 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5301 FILE_SYSTEM_POSIX_INFO *response_data;
5303 int bytes_returned = 0;
5304 __u16 params, byte_count;
5306 cFYI(1, "In QFSPosixInfo");
5308 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5313 params = 2; /* level */
5314 pSMB->TotalDataCount = 0;
5315 pSMB->DataCount = 0;
5316 pSMB->DataOffset = 0;
5317 pSMB->MaxParameterCount = cpu_to_le16(2);
5318 /* BB find exact max SMB PDU from sess structure BB */
5319 pSMB->MaxDataCount = cpu_to_le16(100);
5320 pSMB->MaxSetupCount = 0;
5324 pSMB->Reserved2 = 0;
5325 byte_count = params + 1 /* pad */ ;
5326 pSMB->ParameterCount = cpu_to_le16(params);
5327 pSMB->TotalParameterCount = pSMB->ParameterCount;
5328 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5329 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
5330 pSMB->SetupCount = 1;
5331 pSMB->Reserved3 = 0;
5332 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5333 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_FS_INFO);
5334 inc_rfc1001_len(pSMB, byte_count);
5335 pSMB->ByteCount = cpu_to_le16(byte_count);
5337 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5338 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5340 cFYI(1, "Send error in QFSUnixInfo = %d", rc);
5341 } else { /* decode response */
5342 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5344 if (rc || get_bcc(&pSMBr->hdr) < 13) {
5345 rc = -EIO; /* bad smb */
5347 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5349 (FILE_SYSTEM_POSIX_INFO
5350 *) (((char *) &pSMBr->hdr.Protocol) +
5353 le32_to_cpu(response_data->BlockSize);
5355 le64_to_cpu(response_data->TotalBlocks);
5357 le64_to_cpu(response_data->BlocksAvail);
5358 if (response_data->UserBlocksAvail == cpu_to_le64(-1)) {
5359 FSData->f_bavail = FSData->f_bfree;
5362 le64_to_cpu(response_data->UserBlocksAvail);
5364 if (response_data->TotalFileNodes != cpu_to_le64(-1))
5366 le64_to_cpu(response_data->TotalFileNodes);
5367 if (response_data->FreeFileNodes != cpu_to_le64(-1))
5369 le64_to_cpu(response_data->FreeFileNodes);
5372 cifs_buf_release(pSMB);
5382 * We can not use write of zero bytes trick to set file size due to need for
5383 * large file support. Also note that this SetPathInfo is preferred to
5384 * SetFileInfo based method in next routine which is only needed to work around
5385 * a sharing violation bugin Samba which this routine can run into.
5388 CIFSSMBSetEOF(const unsigned int xid, struct cifs_tcon *tcon,
5389 const char *file_name, __u64 size, struct cifs_sb_info *cifs_sb,
5390 bool set_allocation)
5392 struct smb_com_transaction2_spi_req *pSMB = NULL;
5393 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
5394 struct file_end_of_file_info *parm_data;
5397 int bytes_returned = 0;
5398 int remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR;
5400 __u16 params, byte_count, data_count, param_offset, offset;
5402 cFYI(1, "In SetEOF");
5404 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5409 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5411 cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
5412 PATH_MAX, cifs_sb->local_nls, remap);
5413 name_len++; /* trailing null */
5415 } else { /* BB improve the check for buffer overruns BB */
5416 name_len = strnlen(file_name, PATH_MAX);
5417 name_len++; /* trailing null */
5418 strncpy(pSMB->FileName, file_name, name_len);
5420 params = 6 + name_len;
5421 data_count = sizeof(struct file_end_of_file_info);
5422 pSMB->MaxParameterCount = cpu_to_le16(2);
5423 pSMB->MaxDataCount = cpu_to_le16(4100);
5424 pSMB->MaxSetupCount = 0;
5428 pSMB->Reserved2 = 0;
5429 param_offset = offsetof(struct smb_com_transaction2_spi_req,
5430 InformationLevel) - 4;
5431 offset = param_offset + params;
5432 if (set_allocation) {
5433 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5434 pSMB->InformationLevel =
5435 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5437 pSMB->InformationLevel =
5438 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
5439 } else /* Set File Size */ {
5440 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5441 pSMB->InformationLevel =
5442 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
5444 pSMB->InformationLevel =
5445 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
5449 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol) +
5451 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5452 pSMB->DataOffset = cpu_to_le16(offset);
5453 pSMB->SetupCount = 1;
5454 pSMB->Reserved3 = 0;
5455 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5456 byte_count = 3 /* pad */ + params + data_count;
5457 pSMB->DataCount = cpu_to_le16(data_count);
5458 pSMB->TotalDataCount = pSMB->DataCount;
5459 pSMB->ParameterCount = cpu_to_le16(params);
5460 pSMB->TotalParameterCount = pSMB->ParameterCount;
5461 pSMB->Reserved4 = 0;
5462 inc_rfc1001_len(pSMB, byte_count);
5463 parm_data->FileSize = cpu_to_le64(size);
5464 pSMB->ByteCount = cpu_to_le16(byte_count);
5465 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5466 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5468 cFYI(1, "SetPathInfo (file size) returned %d", rc);
5470 cifs_buf_release(pSMB);
5479 CIFSSMBSetFileSize(const unsigned int xid, struct cifs_tcon *tcon,
5480 struct cifsFileInfo *cfile, __u64 size, bool set_allocation)
5482 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5483 struct file_end_of_file_info *parm_data;
5485 __u16 params, param_offset, offset, byte_count, count;
5487 cFYI(1, "SetFileSize (via SetFileInfo) %lld",
5489 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5494 pSMB->hdr.Pid = cpu_to_le16((__u16)cfile->pid);
5495 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(cfile->pid >> 16));
5498 pSMB->MaxSetupCount = 0;
5502 pSMB->Reserved2 = 0;
5503 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5504 offset = param_offset + params;
5506 count = sizeof(struct file_end_of_file_info);
5507 pSMB->MaxParameterCount = cpu_to_le16(2);
5508 /* BB find exact max SMB PDU from sess structure BB */
5509 pSMB->MaxDataCount = cpu_to_le16(1000);
5510 pSMB->SetupCount = 1;
5511 pSMB->Reserved3 = 0;
5512 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5513 byte_count = 3 /* pad */ + params + count;
5514 pSMB->DataCount = cpu_to_le16(count);
5515 pSMB->ParameterCount = cpu_to_le16(params);
5516 pSMB->TotalDataCount = pSMB->DataCount;
5517 pSMB->TotalParameterCount = pSMB->ParameterCount;
5518 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5520 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol)
5522 pSMB->DataOffset = cpu_to_le16(offset);
5523 parm_data->FileSize = cpu_to_le64(size);
5524 pSMB->Fid = cfile->fid.netfid;
5525 if (set_allocation) {
5526 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5527 pSMB->InformationLevel =
5528 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5530 pSMB->InformationLevel =
5531 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
5532 } else /* Set File Size */ {
5533 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5534 pSMB->InformationLevel =
5535 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
5537 pSMB->InformationLevel =
5538 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
5540 pSMB->Reserved4 = 0;
5541 inc_rfc1001_len(pSMB, byte_count);
5542 pSMB->ByteCount = cpu_to_le16(byte_count);
5543 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
5545 cFYI(1, "Send error in SetFileInfo (SetFileSize) = %d", rc);
5548 /* Note: On -EAGAIN error only caller can retry on handle based calls
5549 since file handle passed in no longer valid */
5554 /* Some legacy servers such as NT4 require that the file times be set on
5555 an open handle, rather than by pathname - this is awkward due to
5556 potential access conflicts on the open, but it is unavoidable for these
5557 old servers since the only other choice is to go from 100 nanosecond DCE
5558 time and resort to the original setpathinfo level which takes the ancient
5559 DOS time format with 2 second granularity */
5561 CIFSSMBSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
5562 const FILE_BASIC_INFO *data, __u16 fid, __u32 pid_of_opener)
5564 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5567 __u16 params, param_offset, offset, byte_count, count;
5569 cFYI(1, "Set Times (via SetFileInfo)");
5570 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5575 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5576 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5579 pSMB->MaxSetupCount = 0;
5583 pSMB->Reserved2 = 0;
5584 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5585 offset = param_offset + params;
5587 data_offset = (char *)pSMB +
5588 offsetof(struct smb_hdr, Protocol) + offset;
5590 count = sizeof(FILE_BASIC_INFO);
5591 pSMB->MaxParameterCount = cpu_to_le16(2);
5592 /* BB find max SMB PDU from sess */
5593 pSMB->MaxDataCount = cpu_to_le16(1000);
5594 pSMB->SetupCount = 1;
5595 pSMB->Reserved3 = 0;
5596 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5597 byte_count = 3 /* pad */ + params + count;
5598 pSMB->DataCount = cpu_to_le16(count);
5599 pSMB->ParameterCount = cpu_to_le16(params);
5600 pSMB->TotalDataCount = pSMB->DataCount;
5601 pSMB->TotalParameterCount = pSMB->ParameterCount;
5602 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5603 pSMB->DataOffset = cpu_to_le16(offset);
5605 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5606 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5608 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5609 pSMB->Reserved4 = 0;
5610 inc_rfc1001_len(pSMB, byte_count);
5611 pSMB->ByteCount = cpu_to_le16(byte_count);
5612 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
5613 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
5615 cFYI(1, "Send error in Set Time (SetFileInfo) = %d", rc);
5617 /* Note: On -EAGAIN error only caller can retry on handle based calls
5618 since file handle passed in no longer valid */
5624 CIFSSMBSetFileDisposition(const unsigned int xid, struct cifs_tcon *tcon,
5625 bool delete_file, __u16 fid, __u32 pid_of_opener)
5627 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5630 __u16 params, param_offset, offset, byte_count, count;
5632 cFYI(1, "Set File Disposition (via SetFileInfo)");
5633 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5638 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5639 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5642 pSMB->MaxSetupCount = 0;
5646 pSMB->Reserved2 = 0;
5647 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5648 offset = param_offset + params;
5650 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5653 pSMB->MaxParameterCount = cpu_to_le16(2);
5654 /* BB find max SMB PDU from sess */
5655 pSMB->MaxDataCount = cpu_to_le16(1000);
5656 pSMB->SetupCount = 1;
5657 pSMB->Reserved3 = 0;
5658 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5659 byte_count = 3 /* pad */ + params + count;
5660 pSMB->DataCount = cpu_to_le16(count);
5661 pSMB->ParameterCount = cpu_to_le16(params);
5662 pSMB->TotalDataCount = pSMB->DataCount;
5663 pSMB->TotalParameterCount = pSMB->ParameterCount;
5664 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5665 pSMB->DataOffset = cpu_to_le16(offset);
5667 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_DISPOSITION_INFO);
5668 pSMB->Reserved4 = 0;
5669 inc_rfc1001_len(pSMB, byte_count);
5670 pSMB->ByteCount = cpu_to_le16(byte_count);
5671 *data_offset = delete_file ? 1 : 0;
5672 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
5674 cFYI(1, "Send error in SetFileDisposition = %d", rc);
5680 CIFSSMBSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
5681 const char *fileName, const FILE_BASIC_INFO *data,
5682 const struct nls_table *nls_codepage, int remap)
5684 TRANSACTION2_SPI_REQ *pSMB = NULL;
5685 TRANSACTION2_SPI_RSP *pSMBr = NULL;
5688 int bytes_returned = 0;
5690 __u16 params, param_offset, offset, byte_count, count;
5692 cFYI(1, "In SetTimes");
5695 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5700 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5702 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
5703 PATH_MAX, nls_codepage, remap);
5704 name_len++; /* trailing null */
5706 } else { /* BB improve the check for buffer overruns BB */
5707 name_len = strnlen(fileName, PATH_MAX);
5708 name_len++; /* trailing null */
5709 strncpy(pSMB->FileName, fileName, name_len);
5712 params = 6 + name_len;
5713 count = sizeof(FILE_BASIC_INFO);
5714 pSMB->MaxParameterCount = cpu_to_le16(2);
5715 /* BB find max SMB PDU from sess structure BB */
5716 pSMB->MaxDataCount = cpu_to_le16(1000);
5717 pSMB->MaxSetupCount = 0;
5721 pSMB->Reserved2 = 0;
5722 param_offset = offsetof(struct smb_com_transaction2_spi_req,
5723 InformationLevel) - 4;
5724 offset = param_offset + params;
5725 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5726 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5727 pSMB->DataOffset = cpu_to_le16(offset);
5728 pSMB->SetupCount = 1;
5729 pSMB->Reserved3 = 0;
5730 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5731 byte_count = 3 /* pad */ + params + count;
5733 pSMB->DataCount = cpu_to_le16(count);
5734 pSMB->ParameterCount = cpu_to_le16(params);
5735 pSMB->TotalDataCount = pSMB->DataCount;
5736 pSMB->TotalParameterCount = pSMB->ParameterCount;
5737 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5738 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5740 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5741 pSMB->Reserved4 = 0;
5742 inc_rfc1001_len(pSMB, byte_count);
5743 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
5744 pSMB->ByteCount = cpu_to_le16(byte_count);
5745 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5746 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5748 cFYI(1, "SetPathInfo (times) returned %d", rc);
5750 cifs_buf_release(pSMB);
5758 /* Can not be used to set time stamps yet (due to old DOS time format) */
5759 /* Can be used to set attributes */
5760 #if 0 /* Possibly not needed - since it turns out that strangely NT4 has a bug
5761 handling it anyway and NT4 was what we thought it would be needed for
5762 Do not delete it until we prove whether needed for Win9x though */
5764 CIFSSMBSetAttrLegacy(unsigned int xid, struct cifs_tcon *tcon, char *fileName,
5765 __u16 dos_attrs, const struct nls_table *nls_codepage)
5767 SETATTR_REQ *pSMB = NULL;
5768 SETATTR_RSP *pSMBr = NULL;
5773 cFYI(1, "In SetAttrLegacy");
5776 rc = smb_init(SMB_COM_SETATTR, 8, tcon, (void **) &pSMB,
5781 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5783 ConvertToUTF16((__le16 *) pSMB->fileName, fileName,
5784 PATH_MAX, nls_codepage);
5785 name_len++; /* trailing null */
5787 } else { /* BB improve the check for buffer overruns BB */
5788 name_len = strnlen(fileName, PATH_MAX);
5789 name_len++; /* trailing null */
5790 strncpy(pSMB->fileName, fileName, name_len);
5792 pSMB->attr = cpu_to_le16(dos_attrs);
5793 pSMB->BufferFormat = 0x04;
5794 inc_rfc1001_len(pSMB, name_len + 1);
5795 pSMB->ByteCount = cpu_to_le16(name_len + 1);
5796 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5797 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5799 cFYI(1, "Error in LegacySetAttr = %d", rc);
5801 cifs_buf_release(pSMB);
5804 goto SetAttrLgcyRetry;
5808 #endif /* temporarily unneeded SetAttr legacy function */
5811 cifs_fill_unix_set_info(FILE_UNIX_BASIC_INFO *data_offset,
5812 const struct cifs_unix_set_info_args *args)
5814 u64 mode = args->mode;
5817 * Samba server ignores set of file size to zero due to bugs in some
5818 * older clients, but we should be precise - we use SetFileSize to
5819 * set file size and do not want to truncate file size to zero
5820 * accidentally as happened on one Samba server beta by putting
5821 * zero instead of -1 here
5823 data_offset->EndOfFile = cpu_to_le64(NO_CHANGE_64);
5824 data_offset->NumOfBytes = cpu_to_le64(NO_CHANGE_64);
5825 data_offset->LastStatusChange = cpu_to_le64(args->ctime);
5826 data_offset->LastAccessTime = cpu_to_le64(args->atime);
5827 data_offset->LastModificationTime = cpu_to_le64(args->mtime);
5828 data_offset->Uid = cpu_to_le64(args->uid);
5829 data_offset->Gid = cpu_to_le64(args->gid);
5830 /* better to leave device as zero when it is */
5831 data_offset->DevMajor = cpu_to_le64(MAJOR(args->device));
5832 data_offset->DevMinor = cpu_to_le64(MINOR(args->device));
5833 data_offset->Permissions = cpu_to_le64(mode);
5836 data_offset->Type = cpu_to_le32(UNIX_FILE);
5837 else if (S_ISDIR(mode))
5838 data_offset->Type = cpu_to_le32(UNIX_DIR);
5839 else if (S_ISLNK(mode))
5840 data_offset->Type = cpu_to_le32(UNIX_SYMLINK);
5841 else if (S_ISCHR(mode))
5842 data_offset->Type = cpu_to_le32(UNIX_CHARDEV);
5843 else if (S_ISBLK(mode))
5844 data_offset->Type = cpu_to_le32(UNIX_BLOCKDEV);
5845 else if (S_ISFIFO(mode))
5846 data_offset->Type = cpu_to_le32(UNIX_FIFO);
5847 else if (S_ISSOCK(mode))
5848 data_offset->Type = cpu_to_le32(UNIX_SOCKET);
5852 CIFSSMBUnixSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
5853 const struct cifs_unix_set_info_args *args,
5854 u16 fid, u32 pid_of_opener)
5856 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5859 u16 params, param_offset, offset, byte_count, count;
5861 cFYI(1, "Set Unix Info (via SetFileInfo)");
5862 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5867 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5868 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5871 pSMB->MaxSetupCount = 0;
5875 pSMB->Reserved2 = 0;
5876 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5877 offset = param_offset + params;
5879 data_offset = (char *)pSMB +
5880 offsetof(struct smb_hdr, Protocol) + offset;
5882 count = sizeof(FILE_UNIX_BASIC_INFO);
5884 pSMB->MaxParameterCount = cpu_to_le16(2);
5885 /* BB find max SMB PDU from sess */
5886 pSMB->MaxDataCount = cpu_to_le16(1000);
5887 pSMB->SetupCount = 1;
5888 pSMB->Reserved3 = 0;
5889 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5890 byte_count = 3 /* pad */ + params + count;
5891 pSMB->DataCount = cpu_to_le16(count);
5892 pSMB->ParameterCount = cpu_to_le16(params);
5893 pSMB->TotalDataCount = pSMB->DataCount;
5894 pSMB->TotalParameterCount = pSMB->ParameterCount;
5895 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5896 pSMB->DataOffset = cpu_to_le16(offset);
5898 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
5899 pSMB->Reserved4 = 0;
5900 inc_rfc1001_len(pSMB, byte_count);
5901 pSMB->ByteCount = cpu_to_le16(byte_count);
5903 cifs_fill_unix_set_info((FILE_UNIX_BASIC_INFO *)data_offset, args);
5905 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
5907 cFYI(1, "Send error in Set Time (SetFileInfo) = %d", rc);
5909 /* Note: On -EAGAIN error only caller can retry on handle based calls
5910 since file handle passed in no longer valid */
5916 CIFSSMBUnixSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
5917 const char *file_name,
5918 const struct cifs_unix_set_info_args *args,
5919 const struct nls_table *nls_codepage, int remap)
5921 TRANSACTION2_SPI_REQ *pSMB = NULL;
5922 TRANSACTION2_SPI_RSP *pSMBr = NULL;
5925 int bytes_returned = 0;
5926 FILE_UNIX_BASIC_INFO *data_offset;
5927 __u16 params, param_offset, offset, count, byte_count;
5929 cFYI(1, "In SetUID/GID/Mode");
5931 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5936 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5938 cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
5939 PATH_MAX, nls_codepage, remap);
5940 name_len++; /* trailing null */
5942 } else { /* BB improve the check for buffer overruns BB */
5943 name_len = strnlen(file_name, PATH_MAX);
5944 name_len++; /* trailing null */
5945 strncpy(pSMB->FileName, file_name, name_len);
5948 params = 6 + name_len;
5949 count = sizeof(FILE_UNIX_BASIC_INFO);
5950 pSMB->MaxParameterCount = cpu_to_le16(2);
5951 /* BB find max SMB PDU from sess structure BB */
5952 pSMB->MaxDataCount = cpu_to_le16(1000);
5953 pSMB->MaxSetupCount = 0;
5957 pSMB->Reserved2 = 0;
5958 param_offset = offsetof(struct smb_com_transaction2_spi_req,
5959 InformationLevel) - 4;
5960 offset = param_offset + params;
5962 (FILE_UNIX_BASIC_INFO *) ((char *) &pSMB->hdr.Protocol +
5964 memset(data_offset, 0, count);
5965 pSMB->DataOffset = cpu_to_le16(offset);
5966 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5967 pSMB->SetupCount = 1;
5968 pSMB->Reserved3 = 0;
5969 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5970 byte_count = 3 /* pad */ + params + count;
5971 pSMB->ParameterCount = cpu_to_le16(params);
5972 pSMB->DataCount = cpu_to_le16(count);
5973 pSMB->TotalParameterCount = pSMB->ParameterCount;
5974 pSMB->TotalDataCount = pSMB->DataCount;
5975 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
5976 pSMB->Reserved4 = 0;
5977 inc_rfc1001_len(pSMB, byte_count);
5979 cifs_fill_unix_set_info(data_offset, args);
5981 pSMB->ByteCount = cpu_to_le16(byte_count);
5982 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5983 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5985 cFYI(1, "SetPathInfo (perms) returned %d", rc);
5987 cifs_buf_release(pSMB);
5993 #ifdef CONFIG_CIFS_XATTR
5995 * Do a path-based QUERY_ALL_EAS call and parse the result. This is a common
5996 * function used by listxattr and getxattr type calls. When ea_name is set,
5997 * it looks for that attribute name and stuffs that value into the EAData
5998 * buffer. When ea_name is NULL, it stuffs a list of attribute names into the
5999 * buffer. In both cases, the return value is either the length of the
6000 * resulting data or a negative error code. If EAData is a NULL pointer then
6001 * the data isn't copied to it, but the length is returned.
6004 CIFSSMBQAllEAs(const unsigned int xid, struct cifs_tcon *tcon,
6005 const unsigned char *searchName, const unsigned char *ea_name,
6006 char *EAData, size_t buf_size,
6007 const struct nls_table *nls_codepage, int remap)
6009 /* BB assumes one setup word */
6010 TRANSACTION2_QPI_REQ *pSMB = NULL;
6011 TRANSACTION2_QPI_RSP *pSMBr = NULL;
6015 struct fealist *ea_response_data;
6016 struct fea *temp_fea;
6019 __u16 params, byte_count, data_offset;
6020 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
6022 cFYI(1, "In Query All EAs path %s", searchName);
6024 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6029 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6031 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
6032 PATH_MAX, nls_codepage, remap);
6033 list_len++; /* trailing null */
6035 } else { /* BB improve the check for buffer overruns BB */
6036 list_len = strnlen(searchName, PATH_MAX);
6037 list_len++; /* trailing null */
6038 strncpy(pSMB->FileName, searchName, list_len);
6041 params = 2 /* level */ + 4 /* reserved */ + list_len /* includes NUL */;
6042 pSMB->TotalDataCount = 0;
6043 pSMB->MaxParameterCount = cpu_to_le16(2);
6044 /* BB find exact max SMB PDU from sess structure BB */
6045 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
6046 pSMB->MaxSetupCount = 0;
6050 pSMB->Reserved2 = 0;
6051 pSMB->ParameterOffset = cpu_to_le16(offsetof(
6052 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
6053 pSMB->DataCount = 0;
6054 pSMB->DataOffset = 0;
6055 pSMB->SetupCount = 1;
6056 pSMB->Reserved3 = 0;
6057 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
6058 byte_count = params + 1 /* pad */ ;
6059 pSMB->TotalParameterCount = cpu_to_le16(params);
6060 pSMB->ParameterCount = pSMB->TotalParameterCount;
6061 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_QUERY_ALL_EAS);
6062 pSMB->Reserved4 = 0;
6063 inc_rfc1001_len(pSMB, byte_count);
6064 pSMB->ByteCount = cpu_to_le16(byte_count);
6066 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6067 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
6069 cFYI(1, "Send error in QueryAllEAs = %d", rc);
6074 /* BB also check enough total bytes returned */
6075 /* BB we need to improve the validity checking
6076 of these trans2 responses */
6078 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
6079 if (rc || get_bcc(&pSMBr->hdr) < 4) {
6080 rc = -EIO; /* bad smb */
6084 /* check that length of list is not more than bcc */
6085 /* check that each entry does not go beyond length
6087 /* check that each element of each entry does not
6088 go beyond end of list */
6089 /* validate_trans2_offsets() */
6090 /* BB check if start of smb + data_offset > &bcc+ bcc */
6092 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
6093 ea_response_data = (struct fealist *)
6094 (((char *) &pSMBr->hdr.Protocol) + data_offset);
6096 list_len = le32_to_cpu(ea_response_data->list_len);
6097 cFYI(1, "ea length %d", list_len);
6098 if (list_len <= 8) {
6099 cFYI(1, "empty EA list returned from server");
6103 /* make sure list_len doesn't go past end of SMB */
6104 end_of_smb = (char *)pByteArea(&pSMBr->hdr) + get_bcc(&pSMBr->hdr);
6105 if ((char *)ea_response_data + list_len > end_of_smb) {
6106 cFYI(1, "EA list appears to go beyond SMB");
6111 /* account for ea list len */
6113 temp_fea = ea_response_data->list;
6114 temp_ptr = (char *)temp_fea;
6115 while (list_len > 0) {
6116 unsigned int name_len;
6121 /* make sure we can read name_len and value_len */
6123 cFYI(1, "EA entry goes beyond length of list");
6128 name_len = temp_fea->name_len;
6129 value_len = le16_to_cpu(temp_fea->value_len);
6130 list_len -= name_len + 1 + value_len;
6132 cFYI(1, "EA entry goes beyond length of list");
6138 if (ea_name_len == name_len &&
6139 memcmp(ea_name, temp_ptr, name_len) == 0) {
6140 temp_ptr += name_len + 1;
6144 if ((size_t)value_len > buf_size) {
6148 memcpy(EAData, temp_ptr, value_len);
6152 /* account for prefix user. and trailing null */
6153 rc += (5 + 1 + name_len);
6154 if (rc < (int) buf_size) {
6155 memcpy(EAData, "user.", 5);
6157 memcpy(EAData, temp_ptr, name_len);
6159 /* null terminate name */
6162 } else if (buf_size == 0) {
6163 /* skip copy - calc size only */
6165 /* stop before overrun buffer */
6170 temp_ptr += name_len + 1 + value_len;
6171 temp_fea = (struct fea *)temp_ptr;
6174 /* didn't find the named attribute */
6179 cifs_buf_release(pSMB);
6187 CIFSSMBSetEA(const unsigned int xid, struct cifs_tcon *tcon,
6188 const char *fileName, const char *ea_name, const void *ea_value,
6189 const __u16 ea_value_len, const struct nls_table *nls_codepage,
6192 struct smb_com_transaction2_spi_req *pSMB = NULL;
6193 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
6194 struct fealist *parm_data;
6197 int bytes_returned = 0;
6198 __u16 params, param_offset, byte_count, offset, count;
6200 cFYI(1, "In SetEA");
6202 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6207 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6209 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
6210 PATH_MAX, nls_codepage, remap);
6211 name_len++; /* trailing null */
6213 } else { /* BB improve the check for buffer overruns BB */
6214 name_len = strnlen(fileName, PATH_MAX);
6215 name_len++; /* trailing null */
6216 strncpy(pSMB->FileName, fileName, name_len);
6219 params = 6 + name_len;
6221 /* done calculating parms using name_len of file name,
6222 now use name_len to calculate length of ea name
6223 we are going to create in the inode xattrs */
6224 if (ea_name == NULL)
6227 name_len = strnlen(ea_name, 255);
6229 count = sizeof(*parm_data) + ea_value_len + name_len;
6230 pSMB->MaxParameterCount = cpu_to_le16(2);
6231 /* BB find max SMB PDU from sess */
6232 pSMB->MaxDataCount = cpu_to_le16(1000);
6233 pSMB->MaxSetupCount = 0;
6237 pSMB->Reserved2 = 0;
6238 param_offset = offsetof(struct smb_com_transaction2_spi_req,
6239 InformationLevel) - 4;
6240 offset = param_offset + params;
6241 pSMB->InformationLevel =
6242 cpu_to_le16(SMB_SET_FILE_EA);
6245 (struct fealist *) (((char *) &pSMB->hdr.Protocol) +
6247 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6248 pSMB->DataOffset = cpu_to_le16(offset);
6249 pSMB->SetupCount = 1;
6250 pSMB->Reserved3 = 0;
6251 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6252 byte_count = 3 /* pad */ + params + count;
6253 pSMB->DataCount = cpu_to_le16(count);
6254 parm_data->list_len = cpu_to_le32(count);
6255 parm_data->list[0].EA_flags = 0;
6256 /* we checked above that name len is less than 255 */
6257 parm_data->list[0].name_len = (__u8)name_len;
6258 /* EA names are always ASCII */
6260 strncpy(parm_data->list[0].name, ea_name, name_len);
6261 parm_data->list[0].name[name_len] = 0;
6262 parm_data->list[0].value_len = cpu_to_le16(ea_value_len);
6263 /* caller ensures that ea_value_len is less than 64K but
6264 we need to ensure that it fits within the smb */
6266 /*BB add length check to see if it would fit in
6267 negotiated SMB buffer size BB */
6268 /* if (ea_value_len > buffer_size - 512 (enough for header)) */
6270 memcpy(parm_data->list[0].name+name_len+1,
6271 ea_value, ea_value_len);
6273 pSMB->TotalDataCount = pSMB->DataCount;
6274 pSMB->ParameterCount = cpu_to_le16(params);
6275 pSMB->TotalParameterCount = pSMB->ParameterCount;
6276 pSMB->Reserved4 = 0;
6277 inc_rfc1001_len(pSMB, byte_count);
6278 pSMB->ByteCount = cpu_to_le16(byte_count);
6279 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6280 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
6282 cFYI(1, "SetPathInfo (EA) returned %d", rc);
6284 cifs_buf_release(pSMB);
6293 #ifdef CONFIG_CIFS_DNOTIFY_EXPERIMENTAL /* BB unused temporarily */
6295 * Years ago the kernel added a "dnotify" function for Samba server,
6296 * to allow network clients (such as Windows) to display updated
6297 * lists of files in directory listings automatically when
6298 * files are added by one user when another user has the
6299 * same directory open on their desktop. The Linux cifs kernel
6300 * client hooked into the kernel side of this interface for
6301 * the same reason, but ironically when the VFS moved from
6302 * "dnotify" to "inotify" it became harder to plug in Linux
6303 * network file system clients (the most obvious use case
6304 * for notify interfaces is when multiple users can update
6305 * the contents of the same directory - exactly what network
6306 * file systems can do) although the server (Samba) could
6307 * still use it. For the short term we leave the worker
6308 * function ifdeffed out (below) until inotify is fixed
6309 * in the VFS to make it easier to plug in network file
6310 * system clients. If inotify turns out to be permanently
6311 * incompatible for network fs clients, we could instead simply
6312 * expose this config flag by adding a future cifs (and smb2) notify ioctl.
6314 int CIFSSMBNotify(const unsigned int xid, struct cifs_tcon *tcon,
6315 const int notify_subdirs, const __u16 netfid,
6316 __u32 filter, struct file *pfile, int multishot,
6317 const struct nls_table *nls_codepage)
6320 struct smb_com_transaction_change_notify_req *pSMB = NULL;
6321 struct smb_com_ntransaction_change_notify_rsp *pSMBr = NULL;
6322 struct dir_notify_req *dnotify_req;
6325 cFYI(1, "In CIFSSMBNotify for file handle %d", (int)netfid);
6326 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
6331 pSMB->TotalParameterCount = 0 ;
6332 pSMB->TotalDataCount = 0;
6333 pSMB->MaxParameterCount = cpu_to_le32(2);
6334 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
6335 pSMB->MaxSetupCount = 4;
6337 pSMB->ParameterOffset = 0;
6338 pSMB->DataCount = 0;
6339 pSMB->DataOffset = 0;
6340 pSMB->SetupCount = 4; /* single byte does not need le conversion */
6341 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_NOTIFY_CHANGE);
6342 pSMB->ParameterCount = pSMB->TotalParameterCount;
6344 pSMB->WatchTree = 1; /* one byte - no le conversion needed */
6345 pSMB->Reserved2 = 0;
6346 pSMB->CompletionFilter = cpu_to_le32(filter);
6347 pSMB->Fid = netfid; /* file handle always le */
6348 pSMB->ByteCount = 0;
6350 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6351 (struct smb_hdr *)pSMBr, &bytes_returned,
6354 cFYI(1, "Error in Notify = %d", rc);
6356 /* Add file to outstanding requests */
6357 /* BB change to kmem cache alloc */
6358 dnotify_req = kmalloc(
6359 sizeof(struct dir_notify_req),
6362 dnotify_req->Pid = pSMB->hdr.Pid;
6363 dnotify_req->PidHigh = pSMB->hdr.PidHigh;
6364 dnotify_req->Mid = pSMB->hdr.Mid;
6365 dnotify_req->Tid = pSMB->hdr.Tid;
6366 dnotify_req->Uid = pSMB->hdr.Uid;
6367 dnotify_req->netfid = netfid;
6368 dnotify_req->pfile = pfile;
6369 dnotify_req->filter = filter;
6370 dnotify_req->multishot = multishot;
6371 spin_lock(&GlobalMid_Lock);
6372 list_add_tail(&dnotify_req->lhead,
6373 &GlobalDnotifyReqList);
6374 spin_unlock(&GlobalMid_Lock);
6378 cifs_buf_release(pSMB);
6381 #endif /* was needed for dnotify, and will be needed for inotify when VFS fix */