1 // SPDX-License-Identifier: LGPL-2.1
5 * Copyright (C) International Business Machines Corp., 2002,2010
8 * Contains the routines for constructing the SMB PDUs themselves
12 /* SMB/CIFS PDU handling routines here - except for leftovers in connect.c */
13 /* These are mostly routines that operate on a pathname, or on a tree id */
14 /* (mounted volume), but there are eight handle based routines which must be */
15 /* treated slightly differently for reconnection purposes since we never */
16 /* want to reuse a stale file handle and only the caller knows the file info */
19 #include <linux/kernel.h>
20 #include <linux/vfs.h>
21 #include <linux/slab.h>
22 #include <linux/posix_acl_xattr.h>
23 #include <linux/pagemap.h>
24 #include <linux/swap.h>
25 #include <linux/task_io_accounting_ops.h>
26 #include <linux/uaccess.h>
30 #include "cifsproto.h"
31 #include "cifs_unicode.h"
32 #include "cifs_debug.h"
33 #include "smb2proto.h"
35 #include "smbdirect.h"
36 #ifdef CONFIG_CIFS_DFS_UPCALL
37 #include "dfs_cache.h"
40 #ifdef CONFIG_CIFS_POSIX
45 {CIFS_PROT, "\2NT LM 0.12"},
46 {POSIX_PROT, "\2POSIX 2"},
54 {CIFS_PROT, "\2NT LM 0.12"},
59 /* define the number of elements in the cifs dialect array */
60 #ifdef CONFIG_CIFS_POSIX
61 #define CIFS_NUM_PROT 2
63 #define CIFS_NUM_PROT 1
64 #endif /* CIFS_POSIX */
67 * Mark as invalid, all open files on tree connections since they
68 * were closed when session to server was lost.
71 cifs_mark_open_files_invalid(struct cifs_tcon *tcon)
73 struct cifsFileInfo *open_file = NULL;
74 struct list_head *tmp;
75 struct list_head *tmp1;
77 /* list all files open on tree connection and mark them invalid */
78 spin_lock(&tcon->open_file_lock);
79 list_for_each_safe(tmp, tmp1, &tcon->openFileList) {
80 open_file = list_entry(tmp, struct cifsFileInfo, tlist);
81 open_file->invalidHandle = true;
82 open_file->oplock_break_cancelled = true;
84 spin_unlock(&tcon->open_file_lock);
86 mutex_lock(&tcon->crfid.fid_mutex);
87 tcon->crfid.is_valid = false;
88 /* cached handle is not valid, so SMB2_CLOSE won't be sent below */
89 close_cached_dir_lease_locked(&tcon->crfid);
90 memset(tcon->crfid.fid, 0, sizeof(struct cifs_fid));
91 mutex_unlock(&tcon->crfid.fid_mutex);
94 * BB Add call to invalidate_inodes(sb) for all superblocks mounted
99 /* reconnect the socket, tcon, and smb session if needed */
101 cifs_reconnect_tcon(struct cifs_tcon *tcon, int smb_command)
104 struct cifs_ses *ses;
105 struct TCP_Server_Info *server;
106 struct nls_table *nls_codepage;
110 * SMBs NegProt, SessSetup, uLogoff do not have tcon yet so check for
111 * tcp and smb session status done differently for those three - in the
118 server = ses->server;
121 * only tree disconnect, open, and write, (and ulogoff which does not
122 * have tcon) are allowed as we start force umount
124 if (tcon->tidStatus == CifsExiting) {
125 if (smb_command != SMB_COM_WRITE_ANDX &&
126 smb_command != SMB_COM_OPEN_ANDX &&
127 smb_command != SMB_COM_TREE_DISCONNECT) {
128 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
134 retries = server->nr_targets;
137 * Give demultiplex thread up to 10 seconds to each target available for
138 * reconnect -- should be greater than cifs socket timeout which is 7
141 while (server->tcpStatus == CifsNeedReconnect) {
142 rc = wait_event_interruptible_timeout(server->response_q,
143 (server->tcpStatus != CifsNeedReconnect),
146 cifs_dbg(FYI, "%s: aborting reconnect due to a received signal by the process\n",
151 /* are we still trying to reconnect? */
152 if (server->tcpStatus != CifsNeedReconnect)
155 if (retries && --retries)
159 * on "soft" mounts we wait once. Hard mounts keep
160 * retrying until process is killed or server comes
164 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
167 retries = server->nr_targets;
170 if (!ses->need_reconnect && !tcon->need_reconnect)
173 nls_codepage = load_nls_default();
176 * need to prevent multiple threads trying to simultaneously
177 * reconnect the same SMB session
179 mutex_lock(&ses->session_mutex);
182 * Recheck after acquire mutex. If another thread is negotiating
183 * and the server never sends an answer the socket will be closed
184 * and tcpStatus set to reconnect.
186 if (server->tcpStatus == CifsNeedReconnect) {
188 mutex_unlock(&ses->session_mutex);
192 rc = cifs_negotiate_protocol(0, ses);
193 if (rc == 0 && ses->need_reconnect)
194 rc = cifs_setup_session(0, ses, nls_codepage);
196 /* do we need to reconnect tcon? */
197 if (rc || !tcon->need_reconnect) {
198 mutex_unlock(&ses->session_mutex);
202 cifs_mark_open_files_invalid(tcon);
203 rc = cifs_tree_connect(0, tcon, nls_codepage);
204 mutex_unlock(&ses->session_mutex);
205 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
208 pr_warn_once("reconnect tcon failed rc = %d\n", rc);
212 atomic_inc(&tconInfoReconnectCount);
214 /* tell server Unix caps we support */
216 reset_cifs_unix_caps(0, tcon, NULL, NULL);
219 * Removed call to reopen open files here. It is safer (and faster) to
220 * reopen files one at a time as needed in read and write.
222 * FIXME: what about file locks? don't we need to reclaim them ASAP?
227 * Check if handle based operation so we know whether we can continue
228 * or not without returning to caller to reset file handle
230 switch (smb_command) {
231 case SMB_COM_READ_ANDX:
232 case SMB_COM_WRITE_ANDX:
234 case SMB_COM_FIND_CLOSE2:
235 case SMB_COM_LOCKING_ANDX:
239 unload_nls(nls_codepage);
243 /* Allocate and return pointer to an SMB request buffer, and set basic
244 SMB information in the SMB header. If the return code is zero, this
245 function must have filled in request_buf pointer */
247 small_smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
252 rc = cifs_reconnect_tcon(tcon, smb_command);
256 *request_buf = cifs_small_buf_get();
257 if (*request_buf == NULL) {
258 /* BB should we add a retry in here if not a writepage? */
262 header_assemble((struct smb_hdr *) *request_buf, smb_command,
266 cifs_stats_inc(&tcon->num_smbs_sent);
272 small_smb_init_no_tc(const int smb_command, const int wct,
273 struct cifs_ses *ses, void **request_buf)
276 struct smb_hdr *buffer;
278 rc = small_smb_init(smb_command, wct, NULL, request_buf);
282 buffer = (struct smb_hdr *)*request_buf;
283 buffer->Mid = get_next_mid(ses->server);
284 if (ses->capabilities & CAP_UNICODE)
285 buffer->Flags2 |= SMBFLG2_UNICODE;
286 if (ses->capabilities & CAP_STATUS32)
287 buffer->Flags2 |= SMBFLG2_ERR_STATUS;
289 /* uid, tid can stay at zero as set in header assemble */
291 /* BB add support for turning on the signing when
292 this function is used after 1st of session setup requests */
297 /* If the return code is zero, this function must fill in request_buf pointer */
299 __smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
300 void **request_buf, void **response_buf)
302 *request_buf = cifs_buf_get();
303 if (*request_buf == NULL) {
304 /* BB should we add a retry in here if not a writepage? */
307 /* Although the original thought was we needed the response buf for */
308 /* potential retries of smb operations it turns out we can determine */
309 /* from the mid flags when the request buffer can be resent without */
310 /* having to use a second distinct buffer for the response */
312 *response_buf = *request_buf;
314 header_assemble((struct smb_hdr *) *request_buf, smb_command, tcon,
318 cifs_stats_inc(&tcon->num_smbs_sent);
323 /* If the return code is zero, this function must fill in request_buf pointer */
325 smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
326 void **request_buf, void **response_buf)
330 rc = cifs_reconnect_tcon(tcon, smb_command);
334 return __smb_init(smb_command, wct, tcon, request_buf, response_buf);
338 smb_init_no_reconnect(int smb_command, int wct, struct cifs_tcon *tcon,
339 void **request_buf, void **response_buf)
341 if (tcon->ses->need_reconnect || tcon->need_reconnect)
344 return __smb_init(smb_command, wct, tcon, request_buf, response_buf);
347 static int validate_t2(struct smb_t2_rsp *pSMB)
349 unsigned int total_size;
351 /* check for plausible wct */
352 if (pSMB->hdr.WordCount < 10)
355 /* check for parm and data offset going beyond end of smb */
356 if (get_unaligned_le16(&pSMB->t2_rsp.ParameterOffset) > 1024 ||
357 get_unaligned_le16(&pSMB->t2_rsp.DataOffset) > 1024)
360 total_size = get_unaligned_le16(&pSMB->t2_rsp.ParameterCount);
361 if (total_size >= 512)
364 /* check that bcc is at least as big as parms + data, and that it is
365 * less than negotiated smb buffer
367 total_size += get_unaligned_le16(&pSMB->t2_rsp.DataCount);
368 if (total_size > get_bcc(&pSMB->hdr) ||
369 total_size >= CIFSMaxBufSize + MAX_CIFS_HDR_SIZE)
374 cifs_dump_mem("Invalid transact2 SMB: ", (char *)pSMB,
375 sizeof(struct smb_t2_rsp) + 16);
380 decode_ext_sec_blob(struct cifs_ses *ses, NEGOTIATE_RSP *pSMBr)
384 char *guid = pSMBr->u.extended_response.GUID;
385 struct TCP_Server_Info *server = ses->server;
387 count = get_bcc(&pSMBr->hdr);
388 if (count < SMB1_CLIENT_GUID_SIZE)
391 spin_lock(&cifs_tcp_ses_lock);
392 if (server->srv_count > 1) {
393 spin_unlock(&cifs_tcp_ses_lock);
394 if (memcmp(server->server_GUID, guid, SMB1_CLIENT_GUID_SIZE) != 0) {
395 cifs_dbg(FYI, "server UID changed\n");
396 memcpy(server->server_GUID, guid, SMB1_CLIENT_GUID_SIZE);
399 spin_unlock(&cifs_tcp_ses_lock);
400 memcpy(server->server_GUID, guid, SMB1_CLIENT_GUID_SIZE);
403 if (count == SMB1_CLIENT_GUID_SIZE) {
404 server->sec_ntlmssp = true;
406 count -= SMB1_CLIENT_GUID_SIZE;
407 rc = decode_negTokenInit(
408 pSMBr->u.extended_response.SecurityBlob, count, server);
417 cifs_enable_signing(struct TCP_Server_Info *server, bool mnt_sign_required)
419 bool srv_sign_required = server->sec_mode & server->vals->signing_required;
420 bool srv_sign_enabled = server->sec_mode & server->vals->signing_enabled;
421 bool mnt_sign_enabled = global_secflags & CIFSSEC_MAY_SIGN;
424 * Is signing required by mnt options? If not then check
425 * global_secflags to see if it is there.
427 if (!mnt_sign_required)
428 mnt_sign_required = ((global_secflags & CIFSSEC_MUST_SIGN) ==
432 * If signing is required then it's automatically enabled too,
433 * otherwise, check to see if the secflags allow it.
435 mnt_sign_enabled = mnt_sign_required ? mnt_sign_required :
436 (global_secflags & CIFSSEC_MAY_SIGN);
438 /* If server requires signing, does client allow it? */
439 if (srv_sign_required) {
440 if (!mnt_sign_enabled) {
441 cifs_dbg(VFS, "Server requires signing, but it's disabled in SecurityFlags!\n");
447 /* If client requires signing, does server allow it? */
448 if (mnt_sign_required) {
449 if (!srv_sign_enabled) {
450 cifs_dbg(VFS, "Server does not support signing!\n");
456 if (cifs_rdma_enabled(server) && server->sign)
457 cifs_dbg(VFS, "Signing is enabled, and RDMA read/write will be disabled\n");
463 should_set_ext_sec_flag(enum securityEnum sectype)
470 if (global_secflags &
471 (CIFSSEC_MAY_KRB5 | CIFSSEC_MAY_NTLMSSP))
480 CIFSSMBNegotiate(const unsigned int xid, struct cifs_ses *ses)
483 NEGOTIATE_RSP *pSMBr;
487 struct TCP_Server_Info *server = ses->server;
491 WARN(1, "%s: server is NULL!\n", __func__);
495 rc = smb_init(SMB_COM_NEGOTIATE, 0, NULL /* no tcon yet */ ,
496 (void **) &pSMB, (void **) &pSMBr);
500 pSMB->hdr.Mid = get_next_mid(server);
501 pSMB->hdr.Flags2 |= (SMBFLG2_UNICODE | SMBFLG2_ERR_STATUS);
503 if (should_set_ext_sec_flag(ses->sectype)) {
504 cifs_dbg(FYI, "Requesting extended security\n");
505 pSMB->hdr.Flags2 |= SMBFLG2_EXT_SEC;
510 * We know that all the name entries in the protocols array
511 * are short (< 16 bytes anyway) and are NUL terminated.
513 for (i = 0; i < CIFS_NUM_PROT; i++) {
514 size_t len = strlen(protocols[i].name) + 1;
516 memcpy(pSMB->DialectsArray+count, protocols[i].name, len);
519 inc_rfc1001_len(pSMB, count);
520 pSMB->ByteCount = cpu_to_le16(count);
522 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
523 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
527 server->dialect = le16_to_cpu(pSMBr->DialectIndex);
528 cifs_dbg(FYI, "Dialect: %d\n", server->dialect);
529 /* Check wct = 1 error case */
530 if ((pSMBr->hdr.WordCount <= 13) || (server->dialect == BAD_PROT)) {
531 /* core returns wct = 1, but we do not ask for core - otherwise
532 small wct just comes when dialect index is -1 indicating we
533 could not negotiate a common dialect */
536 } else if (pSMBr->hdr.WordCount != 17) {
541 /* else wct == 17, NTLM or better */
543 server->sec_mode = pSMBr->SecurityMode;
544 if ((server->sec_mode & SECMODE_USER) == 0)
545 cifs_dbg(FYI, "share mode security\n");
547 /* one byte, so no need to convert this or EncryptionKeyLen from
549 server->maxReq = min_t(unsigned int, le16_to_cpu(pSMBr->MaxMpxCount),
551 set_credits(server, server->maxReq);
552 /* probably no need to store and check maxvcs */
553 server->maxBuf = le32_to_cpu(pSMBr->MaxBufferSize);
554 /* set up max_read for readpages check */
555 server->max_read = server->maxBuf;
556 server->max_rw = le32_to_cpu(pSMBr->MaxRawSize);
557 cifs_dbg(NOISY, "Max buf = %d\n", ses->server->maxBuf);
558 server->capabilities = le32_to_cpu(pSMBr->Capabilities);
559 server->timeAdj = (int)(__s16)le16_to_cpu(pSMBr->ServerTimeZone);
560 server->timeAdj *= 60;
562 if (pSMBr->EncryptionKeyLength == CIFS_CRYPTO_KEY_SIZE) {
563 server->negflavor = CIFS_NEGFLAVOR_UNENCAP;
564 memcpy(ses->server->cryptkey, pSMBr->u.EncryptionKey,
565 CIFS_CRYPTO_KEY_SIZE);
566 } else if (pSMBr->hdr.Flags2 & SMBFLG2_EXT_SEC ||
567 server->capabilities & CAP_EXTENDED_SECURITY) {
568 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
569 rc = decode_ext_sec_blob(ses, pSMBr);
570 } else if (server->sec_mode & SECMODE_PW_ENCRYPT) {
571 rc = -EIO; /* no crypt key only if plain text pwd */
573 server->negflavor = CIFS_NEGFLAVOR_UNENCAP;
574 server->capabilities &= ~CAP_EXTENDED_SECURITY;
578 rc = cifs_enable_signing(server, ses->sign);
580 cifs_buf_release(pSMB);
582 cifs_dbg(FYI, "negprot rc %d\n", rc);
587 CIFSSMBTDis(const unsigned int xid, struct cifs_tcon *tcon)
589 struct smb_hdr *smb_buffer;
592 cifs_dbg(FYI, "In tree disconnect\n");
594 /* BB: do we need to check this? These should never be NULL. */
595 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
599 * No need to return error on this operation if tid invalidated and
600 * closed on server already e.g. due to tcp session crashing. Also,
601 * the tcon is no longer on the list, so no need to take lock before
604 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
607 rc = small_smb_init(SMB_COM_TREE_DISCONNECT, 0, tcon,
608 (void **)&smb_buffer);
612 rc = SendReceiveNoRsp(xid, tcon->ses, (char *)smb_buffer, 0);
613 cifs_small_buf_release(smb_buffer);
615 cifs_dbg(FYI, "Tree disconnect failed %d\n", rc);
617 /* No need to return error on this operation if tid invalidated and
618 closed on server already e.g. due to tcp session crashing */
626 * This is a no-op for now. We're not really interested in the reply, but
627 * rather in the fact that the server sent one and that server->lstrp
630 * FIXME: maybe we should consider checking that the reply matches request?
633 cifs_echo_callback(struct mid_q_entry *mid)
635 struct TCP_Server_Info *server = mid->callback_data;
636 struct cifs_credits credits = { .value = 1, .instance = 0 };
638 DeleteMidQEntry(mid);
639 add_credits(server, &credits, CIFS_ECHO_OP);
643 CIFSSMBEcho(struct TCP_Server_Info *server)
648 struct smb_rqst rqst = { .rq_iov = iov,
651 cifs_dbg(FYI, "In echo request\n");
653 rc = small_smb_init(SMB_COM_ECHO, 0, NULL, (void **)&smb);
657 if (server->capabilities & CAP_UNICODE)
658 smb->hdr.Flags2 |= SMBFLG2_UNICODE;
660 /* set up echo request */
661 smb->hdr.Tid = 0xffff;
662 smb->hdr.WordCount = 1;
663 put_unaligned_le16(1, &smb->EchoCount);
664 put_bcc(1, &smb->hdr);
666 inc_rfc1001_len(smb, 3);
669 iov[0].iov_base = smb;
670 iov[1].iov_len = get_rfc1002_length(smb);
671 iov[1].iov_base = (char *)smb + 4;
673 rc = cifs_call_async(server, &rqst, NULL, cifs_echo_callback, NULL,
674 server, CIFS_NON_BLOCKING | CIFS_ECHO_OP, NULL);
676 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
678 cifs_small_buf_release(smb);
684 CIFSSMBLogoff(const unsigned int xid, struct cifs_ses *ses)
686 LOGOFF_ANDX_REQ *pSMB;
689 cifs_dbg(FYI, "In SMBLogoff for session disconnect\n");
692 * BB: do we need to check validity of ses and server? They should
693 * always be valid since we have an active reference. If not, that
694 * should probably be a BUG()
696 if (!ses || !ses->server)
699 mutex_lock(&ses->session_mutex);
700 if (ses->need_reconnect)
701 goto session_already_dead; /* no need to send SMBlogoff if uid
702 already closed due to reconnect */
703 rc = small_smb_init(SMB_COM_LOGOFF_ANDX, 2, NULL, (void **)&pSMB);
705 mutex_unlock(&ses->session_mutex);
709 pSMB->hdr.Mid = get_next_mid(ses->server);
711 if (ses->server->sign)
712 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
714 pSMB->hdr.Uid = ses->Suid;
716 pSMB->AndXCommand = 0xFF;
717 rc = SendReceiveNoRsp(xid, ses, (char *) pSMB, 0);
718 cifs_small_buf_release(pSMB);
719 session_already_dead:
720 mutex_unlock(&ses->session_mutex);
722 /* if session dead then we do not need to do ulogoff,
723 since server closed smb session, no sense reporting
731 CIFSPOSIXDelFile(const unsigned int xid, struct cifs_tcon *tcon,
732 const char *fileName, __u16 type,
733 const struct nls_table *nls_codepage, int remap)
735 TRANSACTION2_SPI_REQ *pSMB = NULL;
736 TRANSACTION2_SPI_RSP *pSMBr = NULL;
737 struct unlink_psx_rq *pRqD;
740 int bytes_returned = 0;
741 __u16 params, param_offset, offset, byte_count;
743 cifs_dbg(FYI, "In POSIX delete\n");
745 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
750 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
752 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
753 PATH_MAX, nls_codepage, remap);
754 name_len++; /* trailing null */
757 name_len = copy_path_name(pSMB->FileName, fileName);
760 params = 6 + name_len;
761 pSMB->MaxParameterCount = cpu_to_le16(2);
762 pSMB->MaxDataCount = 0; /* BB double check this with jra */
763 pSMB->MaxSetupCount = 0;
768 param_offset = offsetof(struct smb_com_transaction2_spi_req,
769 InformationLevel) - 4;
770 offset = param_offset + params;
772 /* Setup pointer to Request Data (inode type).
773 * Note that SMB offsets are from the beginning of SMB which is 4 bytes
774 * in, after RFC1001 field
776 pRqD = (struct unlink_psx_rq *)((char *)(pSMB) + offset + 4);
777 pRqD->type = cpu_to_le16(type);
778 pSMB->ParameterOffset = cpu_to_le16(param_offset);
779 pSMB->DataOffset = cpu_to_le16(offset);
780 pSMB->SetupCount = 1;
782 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
783 byte_count = 3 /* pad */ + params + sizeof(struct unlink_psx_rq);
785 pSMB->DataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
786 pSMB->TotalDataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
787 pSMB->ParameterCount = cpu_to_le16(params);
788 pSMB->TotalParameterCount = pSMB->ParameterCount;
789 pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_UNLINK);
791 inc_rfc1001_len(pSMB, byte_count);
792 pSMB->ByteCount = cpu_to_le16(byte_count);
793 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
794 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
796 cifs_dbg(FYI, "Posix delete returned %d\n", rc);
797 cifs_buf_release(pSMB);
799 cifs_stats_inc(&tcon->stats.cifs_stats.num_deletes);
808 CIFSSMBDelFile(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
809 struct cifs_sb_info *cifs_sb)
811 DELETE_FILE_REQ *pSMB = NULL;
812 DELETE_FILE_RSP *pSMBr = NULL;
816 int remap = cifs_remap(cifs_sb);
819 rc = smb_init(SMB_COM_DELETE, 1, tcon, (void **) &pSMB,
824 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
825 name_len = cifsConvertToUTF16((__le16 *) pSMB->fileName, name,
826 PATH_MAX, cifs_sb->local_nls,
828 name_len++; /* trailing null */
831 name_len = copy_path_name(pSMB->fileName, name);
833 pSMB->SearchAttributes =
834 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM);
835 pSMB->BufferFormat = 0x04;
836 inc_rfc1001_len(pSMB, name_len + 1);
837 pSMB->ByteCount = cpu_to_le16(name_len + 1);
838 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
839 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
840 cifs_stats_inc(&tcon->stats.cifs_stats.num_deletes);
842 cifs_dbg(FYI, "Error in RMFile = %d\n", rc);
844 cifs_buf_release(pSMB);
852 CIFSSMBRmDir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
853 struct cifs_sb_info *cifs_sb)
855 DELETE_DIRECTORY_REQ *pSMB = NULL;
856 DELETE_DIRECTORY_RSP *pSMBr = NULL;
860 int remap = cifs_remap(cifs_sb);
862 cifs_dbg(FYI, "In CIFSSMBRmDir\n");
864 rc = smb_init(SMB_COM_DELETE_DIRECTORY, 0, tcon, (void **) &pSMB,
869 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
870 name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, name,
871 PATH_MAX, cifs_sb->local_nls,
873 name_len++; /* trailing null */
876 name_len = copy_path_name(pSMB->DirName, name);
879 pSMB->BufferFormat = 0x04;
880 inc_rfc1001_len(pSMB, name_len + 1);
881 pSMB->ByteCount = cpu_to_le16(name_len + 1);
882 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
883 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
884 cifs_stats_inc(&tcon->stats.cifs_stats.num_rmdirs);
886 cifs_dbg(FYI, "Error in RMDir = %d\n", rc);
888 cifs_buf_release(pSMB);
895 CIFSSMBMkDir(const unsigned int xid, struct inode *inode, umode_t mode,
896 struct cifs_tcon *tcon, const char *name,
897 struct cifs_sb_info *cifs_sb)
900 CREATE_DIRECTORY_REQ *pSMB = NULL;
901 CREATE_DIRECTORY_RSP *pSMBr = NULL;
904 int remap = cifs_remap(cifs_sb);
906 cifs_dbg(FYI, "In CIFSSMBMkDir\n");
908 rc = smb_init(SMB_COM_CREATE_DIRECTORY, 0, tcon, (void **) &pSMB,
913 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
914 name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, name,
915 PATH_MAX, cifs_sb->local_nls,
917 name_len++; /* trailing null */
920 name_len = copy_path_name(pSMB->DirName, name);
923 pSMB->BufferFormat = 0x04;
924 inc_rfc1001_len(pSMB, name_len + 1);
925 pSMB->ByteCount = cpu_to_le16(name_len + 1);
926 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
927 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
928 cifs_stats_inc(&tcon->stats.cifs_stats.num_mkdirs);
930 cifs_dbg(FYI, "Error in Mkdir = %d\n", rc);
932 cifs_buf_release(pSMB);
939 CIFSPOSIXCreate(const unsigned int xid, struct cifs_tcon *tcon,
940 __u32 posix_flags, __u64 mode, __u16 *netfid,
941 FILE_UNIX_BASIC_INFO *pRetData, __u32 *pOplock,
942 const char *name, const struct nls_table *nls_codepage,
945 TRANSACTION2_SPI_REQ *pSMB = NULL;
946 TRANSACTION2_SPI_RSP *pSMBr = NULL;
949 int bytes_returned = 0;
950 __u16 params, param_offset, offset, byte_count, count;
952 OPEN_PSX_RSP *psx_rsp;
954 cifs_dbg(FYI, "In POSIX Create\n");
956 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
961 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
963 cifsConvertToUTF16((__le16 *) pSMB->FileName, name,
964 PATH_MAX, nls_codepage, remap);
965 name_len++; /* trailing null */
968 name_len = copy_path_name(pSMB->FileName, name);
971 params = 6 + name_len;
972 count = sizeof(OPEN_PSX_REQ);
973 pSMB->MaxParameterCount = cpu_to_le16(2);
974 pSMB->MaxDataCount = cpu_to_le16(1000); /* large enough */
975 pSMB->MaxSetupCount = 0;
980 param_offset = offsetof(struct smb_com_transaction2_spi_req,
981 InformationLevel) - 4;
982 offset = param_offset + params;
983 /* SMB offsets are from the beginning of SMB which is 4 bytes in, after RFC1001 field */
984 pdata = (OPEN_PSX_REQ *)((char *)(pSMB) + offset + 4);
985 pdata->Level = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
986 pdata->Permissions = cpu_to_le64(mode);
987 pdata->PosixOpenFlags = cpu_to_le32(posix_flags);
988 pdata->OpenFlags = cpu_to_le32(*pOplock);
989 pSMB->ParameterOffset = cpu_to_le16(param_offset);
990 pSMB->DataOffset = cpu_to_le16(offset);
991 pSMB->SetupCount = 1;
993 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
994 byte_count = 3 /* pad */ + params + count;
996 pSMB->DataCount = cpu_to_le16(count);
997 pSMB->ParameterCount = cpu_to_le16(params);
998 pSMB->TotalDataCount = pSMB->DataCount;
999 pSMB->TotalParameterCount = pSMB->ParameterCount;
1000 pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_OPEN);
1001 pSMB->Reserved4 = 0;
1002 inc_rfc1001_len(pSMB, byte_count);
1003 pSMB->ByteCount = cpu_to_le16(byte_count);
1004 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1005 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
1007 cifs_dbg(FYI, "Posix create returned %d\n", rc);
1008 goto psx_create_err;
1011 cifs_dbg(FYI, "copying inode info\n");
1012 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
1014 if (rc || get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)) {
1015 rc = -EIO; /* bad smb */
1016 goto psx_create_err;
1019 /* copy return information to pRetData */
1020 psx_rsp = (OPEN_PSX_RSP *)((char *) &pSMBr->hdr.Protocol
1021 + le16_to_cpu(pSMBr->t2.DataOffset));
1023 *pOplock = le16_to_cpu(psx_rsp->OplockFlags);
1025 *netfid = psx_rsp->Fid; /* cifs fid stays in le */
1026 /* Let caller know file was created so we can set the mode. */
1027 /* Do we care about the CreateAction in any other cases? */
1028 if (cpu_to_le32(FILE_CREATE) == psx_rsp->CreateAction)
1029 *pOplock |= CIFS_CREATE_ACTION;
1030 /* check to make sure response data is there */
1031 if (psx_rsp->ReturnedLevel != cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC)) {
1032 pRetData->Type = cpu_to_le32(-1); /* unknown */
1033 cifs_dbg(NOISY, "unknown type\n");
1035 if (get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)
1036 + sizeof(FILE_UNIX_BASIC_INFO)) {
1037 cifs_dbg(VFS, "Open response data too small\n");
1038 pRetData->Type = cpu_to_le32(-1);
1039 goto psx_create_err;
1041 memcpy((char *) pRetData,
1042 (char *)psx_rsp + sizeof(OPEN_PSX_RSP),
1043 sizeof(FILE_UNIX_BASIC_INFO));
1047 cifs_buf_release(pSMB);
1049 if (posix_flags & SMB_O_DIRECTORY)
1050 cifs_stats_inc(&tcon->stats.cifs_stats.num_posixmkdirs);
1052 cifs_stats_inc(&tcon->stats.cifs_stats.num_posixopens);
1060 static __u16 convert_disposition(int disposition)
1064 switch (disposition) {
1065 case FILE_SUPERSEDE:
1066 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1069 ofun = SMBOPEN_OAPPEND;
1072 ofun = SMBOPEN_OCREATE;
1075 ofun = SMBOPEN_OCREATE | SMBOPEN_OAPPEND;
1077 case FILE_OVERWRITE:
1078 ofun = SMBOPEN_OTRUNC;
1080 case FILE_OVERWRITE_IF:
1081 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1084 cifs_dbg(FYI, "unknown disposition %d\n", disposition);
1085 ofun = SMBOPEN_OAPPEND; /* regular open */
1091 access_flags_to_smbopen_mode(const int access_flags)
1093 int masked_flags = access_flags & (GENERIC_READ | GENERIC_WRITE);
1095 if (masked_flags == GENERIC_READ)
1096 return SMBOPEN_READ;
1097 else if (masked_flags == GENERIC_WRITE)
1098 return SMBOPEN_WRITE;
1100 /* just go for read/write */
1101 return SMBOPEN_READWRITE;
1105 SMBLegacyOpen(const unsigned int xid, struct cifs_tcon *tcon,
1106 const char *fileName, const int openDisposition,
1107 const int access_flags, const int create_options, __u16 *netfid,
1108 int *pOplock, FILE_ALL_INFO *pfile_info,
1109 const struct nls_table *nls_codepage, int remap)
1112 OPENX_REQ *pSMB = NULL;
1113 OPENX_RSP *pSMBr = NULL;
1119 rc = smb_init(SMB_COM_OPEN_ANDX, 15, tcon, (void **) &pSMB,
1124 pSMB->AndXCommand = 0xFF; /* none */
1126 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1127 count = 1; /* account for one byte pad to word boundary */
1129 cifsConvertToUTF16((__le16 *) (pSMB->fileName + 1),
1130 fileName, PATH_MAX, nls_codepage, remap);
1131 name_len++; /* trailing null */
1134 count = 0; /* no pad */
1135 name_len = copy_path_name(pSMB->fileName, fileName);
1137 if (*pOplock & REQ_OPLOCK)
1138 pSMB->OpenFlags = cpu_to_le16(REQ_OPLOCK);
1139 else if (*pOplock & REQ_BATCHOPLOCK)
1140 pSMB->OpenFlags = cpu_to_le16(REQ_BATCHOPLOCK);
1142 pSMB->OpenFlags |= cpu_to_le16(REQ_MORE_INFO);
1143 pSMB->Mode = cpu_to_le16(access_flags_to_smbopen_mode(access_flags));
1144 pSMB->Mode |= cpu_to_le16(0x40); /* deny none */
1145 /* set file as system file if special file such
1146 as fifo and server expecting SFU style and
1147 no Unix extensions */
1149 if (create_options & CREATE_OPTION_SPECIAL)
1150 pSMB->FileAttributes = cpu_to_le16(ATTR_SYSTEM);
1151 else /* BB FIXME BB */
1152 pSMB->FileAttributes = cpu_to_le16(0/*ATTR_NORMAL*/);
1154 if (create_options & CREATE_OPTION_READONLY)
1155 pSMB->FileAttributes |= cpu_to_le16(ATTR_READONLY);
1158 /* pSMB->CreateOptions = cpu_to_le32(create_options &
1159 CREATE_OPTIONS_MASK); */
1160 /* BB FIXME END BB */
1162 pSMB->Sattr = cpu_to_le16(ATTR_HIDDEN | ATTR_SYSTEM | ATTR_DIRECTORY);
1163 pSMB->OpenFunction = cpu_to_le16(convert_disposition(openDisposition));
1165 inc_rfc1001_len(pSMB, count);
1167 pSMB->ByteCount = cpu_to_le16(count);
1168 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1169 (struct smb_hdr *)pSMBr, &bytes_returned, 0);
1170 cifs_stats_inc(&tcon->stats.cifs_stats.num_opens);
1172 cifs_dbg(FYI, "Error in Open = %d\n", rc);
1174 /* BB verify if wct == 15 */
1176 /* *pOplock = pSMBr->OplockLevel; */ /* BB take from action field*/
1178 *netfid = pSMBr->Fid; /* cifs fid stays in le */
1179 /* Let caller know file was created so we can set the mode. */
1180 /* Do we care about the CreateAction in any other cases? */
1182 /* if (cpu_to_le32(FILE_CREATE) == pSMBr->CreateAction)
1183 *pOplock |= CIFS_CREATE_ACTION; */
1187 pfile_info->CreationTime = 0; /* BB convert CreateTime*/
1188 pfile_info->LastAccessTime = 0; /* BB fixme */
1189 pfile_info->LastWriteTime = 0; /* BB fixme */
1190 pfile_info->ChangeTime = 0; /* BB fixme */
1191 pfile_info->Attributes =
1192 cpu_to_le32(le16_to_cpu(pSMBr->FileAttributes));
1193 /* the file_info buf is endian converted by caller */
1194 pfile_info->AllocationSize =
1195 cpu_to_le64(le32_to_cpu(pSMBr->EndOfFile));
1196 pfile_info->EndOfFile = pfile_info->AllocationSize;
1197 pfile_info->NumberOfLinks = cpu_to_le32(1);
1198 pfile_info->DeletePending = 0;
1202 cifs_buf_release(pSMB);
1209 CIFS_open(const unsigned int xid, struct cifs_open_parms *oparms, int *oplock,
1213 OPEN_REQ *req = NULL;
1214 OPEN_RSP *rsp = NULL;
1218 struct cifs_sb_info *cifs_sb = oparms->cifs_sb;
1219 struct cifs_tcon *tcon = oparms->tcon;
1220 int remap = cifs_remap(cifs_sb);
1221 const struct nls_table *nls = cifs_sb->local_nls;
1222 int create_options = oparms->create_options;
1223 int desired_access = oparms->desired_access;
1224 int disposition = oparms->disposition;
1225 const char *path = oparms->path;
1228 rc = smb_init(SMB_COM_NT_CREATE_ANDX, 24, tcon, (void **)&req,
1233 /* no commands go after this */
1234 req->AndXCommand = 0xFF;
1236 if (req->hdr.Flags2 & SMBFLG2_UNICODE) {
1237 /* account for one byte pad to word boundary */
1239 name_len = cifsConvertToUTF16((__le16 *)(req->fileName + 1),
1240 path, PATH_MAX, nls, remap);
1244 req->NameLength = cpu_to_le16(name_len);
1246 /* BB improve check for buffer overruns BB */
1249 name_len = copy_path_name(req->fileName, path);
1250 req->NameLength = cpu_to_le16(name_len);
1253 if (*oplock & REQ_OPLOCK)
1254 req->OpenFlags = cpu_to_le32(REQ_OPLOCK);
1255 else if (*oplock & REQ_BATCHOPLOCK)
1256 req->OpenFlags = cpu_to_le32(REQ_BATCHOPLOCK);
1258 req->DesiredAccess = cpu_to_le32(desired_access);
1259 req->AllocationSize = 0;
1262 * Set file as system file if special file such as fifo and server
1263 * expecting SFU style and no Unix extensions.
1265 if (create_options & CREATE_OPTION_SPECIAL)
1266 req->FileAttributes = cpu_to_le32(ATTR_SYSTEM);
1268 req->FileAttributes = cpu_to_le32(ATTR_NORMAL);
1271 * XP does not handle ATTR_POSIX_SEMANTICS but it helps speed up case
1272 * sensitive checks for other servers such as Samba.
1274 if (tcon->ses->capabilities & CAP_UNIX)
1275 req->FileAttributes |= cpu_to_le32(ATTR_POSIX_SEMANTICS);
1277 if (create_options & CREATE_OPTION_READONLY)
1278 req->FileAttributes |= cpu_to_le32(ATTR_READONLY);
1280 req->ShareAccess = cpu_to_le32(FILE_SHARE_ALL);
1281 req->CreateDisposition = cpu_to_le32(disposition);
1282 req->CreateOptions = cpu_to_le32(create_options & CREATE_OPTIONS_MASK);
1284 /* BB Expirement with various impersonation levels and verify */
1285 req->ImpersonationLevel = cpu_to_le32(SECURITY_IMPERSONATION);
1286 req->SecurityFlags = SECURITY_CONTEXT_TRACKING|SECURITY_EFFECTIVE_ONLY;
1289 inc_rfc1001_len(req, count);
1291 req->ByteCount = cpu_to_le16(count);
1292 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *)req,
1293 (struct smb_hdr *)rsp, &bytes_returned, 0);
1294 cifs_stats_inc(&tcon->stats.cifs_stats.num_opens);
1296 cifs_dbg(FYI, "Error in Open = %d\n", rc);
1297 cifs_buf_release(req);
1303 /* 1 byte no need to le_to_cpu */
1304 *oplock = rsp->OplockLevel;
1305 /* cifs fid stays in le */
1306 oparms->fid->netfid = rsp->Fid;
1307 oparms->fid->access = desired_access;
1309 /* Let caller know file was created so we can set the mode. */
1310 /* Do we care about the CreateAction in any other cases? */
1311 if (cpu_to_le32(FILE_CREATE) == rsp->CreateAction)
1312 *oplock |= CIFS_CREATE_ACTION;
1315 /* copy from CreationTime to Attributes */
1316 memcpy((char *)buf, (char *)&rsp->CreationTime, 36);
1317 /* the file_info buf is endian converted by caller */
1318 buf->AllocationSize = rsp->AllocationSize;
1319 buf->EndOfFile = rsp->EndOfFile;
1320 buf->NumberOfLinks = cpu_to_le32(1);
1321 buf->DeletePending = 0;
1324 cifs_buf_release(req);
1329 * Discard any remaining data in the current SMB. To do this, we borrow the
1333 cifs_discard_remaining_data(struct TCP_Server_Info *server)
1335 unsigned int rfclen = server->pdu_size;
1336 int remaining = rfclen + server->vals->header_preamble_size -
1339 while (remaining > 0) {
1342 length = cifs_discard_from_socket(server,
1343 min_t(size_t, remaining,
1344 CIFSMaxBufSize + MAX_HEADER_SIZE(server)));
1347 server->total_read += length;
1348 remaining -= length;
1355 __cifs_readv_discard(struct TCP_Server_Info *server, struct mid_q_entry *mid,
1360 length = cifs_discard_remaining_data(server);
1361 dequeue_mid(mid, malformed);
1362 mid->resp_buf = server->smallbuf;
1363 server->smallbuf = NULL;
1368 cifs_readv_discard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1370 struct cifs_readdata *rdata = mid->callback_data;
1372 return __cifs_readv_discard(server, mid, rdata->result);
1376 cifs_readv_receive(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1379 unsigned int data_offset, data_len;
1380 struct cifs_readdata *rdata = mid->callback_data;
1381 char *buf = server->smallbuf;
1382 unsigned int buflen = server->pdu_size +
1383 server->vals->header_preamble_size;
1384 bool use_rdma_mr = false;
1386 cifs_dbg(FYI, "%s: mid=%llu offset=%llu bytes=%u\n",
1387 __func__, mid->mid, rdata->offset, rdata->bytes);
1390 * read the rest of READ_RSP header (sans Data array), or whatever we
1391 * can if there's not enough data. At this point, we've read down to
1394 len = min_t(unsigned int, buflen, server->vals->read_rsp_size) -
1395 HEADER_SIZE(server) + 1;
1397 length = cifs_read_from_socket(server,
1398 buf + HEADER_SIZE(server) - 1, len);
1401 server->total_read += length;
1403 if (server->ops->is_session_expired &&
1404 server->ops->is_session_expired(buf)) {
1405 cifs_reconnect(server);
1409 if (server->ops->is_status_pending &&
1410 server->ops->is_status_pending(buf, server)) {
1411 cifs_discard_remaining_data(server);
1415 /* set up first two iov for signature check and to get credits */
1416 rdata->iov[0].iov_base = buf;
1417 rdata->iov[0].iov_len = server->vals->header_preamble_size;
1418 rdata->iov[1].iov_base = buf + server->vals->header_preamble_size;
1419 rdata->iov[1].iov_len =
1420 server->total_read - server->vals->header_preamble_size;
1421 cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
1422 rdata->iov[0].iov_base, rdata->iov[0].iov_len);
1423 cifs_dbg(FYI, "1: iov_base=%p iov_len=%zu\n",
1424 rdata->iov[1].iov_base, rdata->iov[1].iov_len);
1426 /* Was the SMB read successful? */
1427 rdata->result = server->ops->map_error(buf, false);
1428 if (rdata->result != 0) {
1429 cifs_dbg(FYI, "%s: server returned error %d\n",
1430 __func__, rdata->result);
1431 /* normal error on read response */
1432 return __cifs_readv_discard(server, mid, false);
1435 /* Is there enough to get to the rest of the READ_RSP header? */
1436 if (server->total_read < server->vals->read_rsp_size) {
1437 cifs_dbg(FYI, "%s: server returned short header. got=%u expected=%zu\n",
1438 __func__, server->total_read,
1439 server->vals->read_rsp_size);
1440 rdata->result = -EIO;
1441 return cifs_readv_discard(server, mid);
1444 data_offset = server->ops->read_data_offset(buf) +
1445 server->vals->header_preamble_size;
1446 if (data_offset < server->total_read) {
1448 * win2k8 sometimes sends an offset of 0 when the read
1449 * is beyond the EOF. Treat it as if the data starts just after
1452 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
1453 __func__, data_offset);
1454 data_offset = server->total_read;
1455 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
1456 /* data_offset is beyond the end of smallbuf */
1457 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
1458 __func__, data_offset);
1459 rdata->result = -EIO;
1460 return cifs_readv_discard(server, mid);
1463 cifs_dbg(FYI, "%s: total_read=%u data_offset=%u\n",
1464 __func__, server->total_read, data_offset);
1466 len = data_offset - server->total_read;
1468 /* read any junk before data into the rest of smallbuf */
1469 length = cifs_read_from_socket(server,
1470 buf + server->total_read, len);
1473 server->total_read += length;
1476 /* how much data is in the response? */
1477 #ifdef CONFIG_CIFS_SMB_DIRECT
1478 use_rdma_mr = rdata->mr;
1480 data_len = server->ops->read_data_length(buf, use_rdma_mr);
1481 if (!use_rdma_mr && (data_offset + data_len > buflen)) {
1482 /* data_len is corrupt -- discard frame */
1483 rdata->result = -EIO;
1484 return cifs_readv_discard(server, mid);
1487 length = rdata->read_into_pages(server, rdata, data_len);
1491 server->total_read += length;
1493 cifs_dbg(FYI, "total_read=%u buflen=%u remaining=%u\n",
1494 server->total_read, buflen, data_len);
1496 /* discard anything left over */
1497 if (server->total_read < buflen)
1498 return cifs_readv_discard(server, mid);
1500 dequeue_mid(mid, false);
1501 mid->resp_buf = server->smallbuf;
1502 server->smallbuf = NULL;
1507 cifs_readv_callback(struct mid_q_entry *mid)
1509 struct cifs_readdata *rdata = mid->callback_data;
1510 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
1511 struct TCP_Server_Info *server = tcon->ses->server;
1512 struct smb_rqst rqst = { .rq_iov = rdata->iov,
1514 .rq_pages = rdata->pages,
1515 .rq_offset = rdata->page_offset,
1516 .rq_npages = rdata->nr_pages,
1517 .rq_pagesz = rdata->pagesz,
1518 .rq_tailsz = rdata->tailsz };
1519 struct cifs_credits credits = { .value = 1, .instance = 0 };
1521 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
1522 __func__, mid->mid, mid->mid_state, rdata->result,
1525 switch (mid->mid_state) {
1526 case MID_RESPONSE_RECEIVED:
1527 /* result already set, check signature */
1531 rc = cifs_verify_signature(&rqst, server,
1532 mid->sequence_number);
1534 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
1537 /* FIXME: should this be counted toward the initiating task? */
1538 task_io_account_read(rdata->got_bytes);
1539 cifs_stats_bytes_read(tcon, rdata->got_bytes);
1541 case MID_REQUEST_SUBMITTED:
1542 case MID_RETRY_NEEDED:
1543 rdata->result = -EAGAIN;
1544 if (server->sign && rdata->got_bytes)
1545 /* reset bytes number since we can not check a sign */
1546 rdata->got_bytes = 0;
1547 /* FIXME: should this be counted toward the initiating task? */
1548 task_io_account_read(rdata->got_bytes);
1549 cifs_stats_bytes_read(tcon, rdata->got_bytes);
1552 rdata->result = -EIO;
1555 queue_work(cifsiod_wq, &rdata->work);
1556 DeleteMidQEntry(mid);
1557 add_credits(server, &credits, 0);
1560 /* cifs_async_readv - send an async write, and set up mid to handle result */
1562 cifs_async_readv(struct cifs_readdata *rdata)
1565 READ_REQ *smb = NULL;
1567 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
1568 struct smb_rqst rqst = { .rq_iov = rdata->iov,
1571 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
1572 __func__, rdata->offset, rdata->bytes);
1574 if (tcon->ses->capabilities & CAP_LARGE_FILES)
1577 wct = 10; /* old style read */
1578 if ((rdata->offset >> 32) > 0) {
1579 /* can not handle this big offset for old */
1584 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **)&smb);
1588 smb->hdr.Pid = cpu_to_le16((__u16)rdata->pid);
1589 smb->hdr.PidHigh = cpu_to_le16((__u16)(rdata->pid >> 16));
1591 smb->AndXCommand = 0xFF; /* none */
1592 smb->Fid = rdata->cfile->fid.netfid;
1593 smb->OffsetLow = cpu_to_le32(rdata->offset & 0xFFFFFFFF);
1595 smb->OffsetHigh = cpu_to_le32(rdata->offset >> 32);
1597 smb->MaxCount = cpu_to_le16(rdata->bytes & 0xFFFF);
1598 smb->MaxCountHigh = cpu_to_le32(rdata->bytes >> 16);
1602 /* old style read */
1603 struct smb_com_readx_req *smbr =
1604 (struct smb_com_readx_req *)smb;
1605 smbr->ByteCount = 0;
1608 /* 4 for RFC1001 length + 1 for BCC */
1609 rdata->iov[0].iov_base = smb;
1610 rdata->iov[0].iov_len = 4;
1611 rdata->iov[1].iov_base = (char *)smb + 4;
1612 rdata->iov[1].iov_len = get_rfc1002_length(smb);
1614 kref_get(&rdata->refcount);
1615 rc = cifs_call_async(tcon->ses->server, &rqst, cifs_readv_receive,
1616 cifs_readv_callback, NULL, rdata, 0, NULL);
1619 cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
1621 kref_put(&rdata->refcount, cifs_readdata_release);
1623 cifs_small_buf_release(smb);
1628 CIFSSMBRead(const unsigned int xid, struct cifs_io_parms *io_parms,
1629 unsigned int *nbytes, char **buf, int *pbuf_type)
1632 READ_REQ *pSMB = NULL;
1633 READ_RSP *pSMBr = NULL;
1634 char *pReadData = NULL;
1636 int resp_buf_type = 0;
1638 struct kvec rsp_iov;
1639 __u32 pid = io_parms->pid;
1640 __u16 netfid = io_parms->netfid;
1641 __u64 offset = io_parms->offset;
1642 struct cifs_tcon *tcon = io_parms->tcon;
1643 unsigned int count = io_parms->length;
1645 cifs_dbg(FYI, "Reading %d bytes on fid %d\n", count, netfid);
1646 if (tcon->ses->capabilities & CAP_LARGE_FILES)
1649 wct = 10; /* old style read */
1650 if ((offset >> 32) > 0) {
1651 /* can not handle this big offset for old */
1657 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **) &pSMB);
1661 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1662 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1664 /* tcon and ses pointer are checked in smb_init */
1665 if (tcon->ses->server == NULL)
1666 return -ECONNABORTED;
1668 pSMB->AndXCommand = 0xFF; /* none */
1670 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
1672 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
1674 pSMB->Remaining = 0;
1675 pSMB->MaxCount = cpu_to_le16(count & 0xFFFF);
1676 pSMB->MaxCountHigh = cpu_to_le32(count >> 16);
1678 pSMB->ByteCount = 0; /* no need to do le conversion since 0 */
1680 /* old style read */
1681 struct smb_com_readx_req *pSMBW =
1682 (struct smb_com_readx_req *)pSMB;
1683 pSMBW->ByteCount = 0;
1686 iov[0].iov_base = (char *)pSMB;
1687 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
1688 rc = SendReceive2(xid, tcon->ses, iov, 1, &resp_buf_type,
1689 CIFS_LOG_ERROR, &rsp_iov);
1690 cifs_small_buf_release(pSMB);
1691 cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
1692 pSMBr = (READ_RSP *)rsp_iov.iov_base;
1694 cifs_dbg(VFS, "Send error in read = %d\n", rc);
1696 int data_length = le16_to_cpu(pSMBr->DataLengthHigh);
1697 data_length = data_length << 16;
1698 data_length += le16_to_cpu(pSMBr->DataLength);
1699 *nbytes = data_length;
1701 /*check that DataLength would not go beyond end of SMB */
1702 if ((data_length > CIFSMaxBufSize)
1703 || (data_length > count)) {
1704 cifs_dbg(FYI, "bad length %d for count %d\n",
1705 data_length, count);
1709 pReadData = (char *) (&pSMBr->hdr.Protocol) +
1710 le16_to_cpu(pSMBr->DataOffset);
1711 /* if (rc = copy_to_user(buf, pReadData, data_length)) {
1712 cifs_dbg(VFS, "Faulting on read rc = %d\n",rc);
1714 }*/ /* can not use copy_to_user when using page cache*/
1716 memcpy(*buf, pReadData, data_length);
1721 free_rsp_buf(resp_buf_type, rsp_iov.iov_base);
1722 } else if (resp_buf_type != CIFS_NO_BUFFER) {
1723 /* return buffer to caller to free */
1724 *buf = rsp_iov.iov_base;
1725 if (resp_buf_type == CIFS_SMALL_BUFFER)
1726 *pbuf_type = CIFS_SMALL_BUFFER;
1727 else if (resp_buf_type == CIFS_LARGE_BUFFER)
1728 *pbuf_type = CIFS_LARGE_BUFFER;
1729 } /* else no valid buffer on return - leave as null */
1731 /* Note: On -EAGAIN error only caller can retry on handle based calls
1732 since file handle passed in no longer valid */
1738 CIFSSMBWrite(const unsigned int xid, struct cifs_io_parms *io_parms,
1739 unsigned int *nbytes, const char *buf)
1742 WRITE_REQ *pSMB = NULL;
1743 WRITE_RSP *pSMBr = NULL;
1744 int bytes_returned, wct;
1747 __u32 pid = io_parms->pid;
1748 __u16 netfid = io_parms->netfid;
1749 __u64 offset = io_parms->offset;
1750 struct cifs_tcon *tcon = io_parms->tcon;
1751 unsigned int count = io_parms->length;
1755 /* cifs_dbg(FYI, "write at %lld %d bytes\n", offset, count);*/
1756 if (tcon->ses == NULL)
1757 return -ECONNABORTED;
1759 if (tcon->ses->capabilities & CAP_LARGE_FILES)
1763 if ((offset >> 32) > 0) {
1764 /* can not handle big offset for old srv */
1769 rc = smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB,
1774 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1775 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1777 /* tcon and ses pointer are checked in smb_init */
1778 if (tcon->ses->server == NULL)
1779 return -ECONNABORTED;
1781 pSMB->AndXCommand = 0xFF; /* none */
1783 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
1785 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
1787 pSMB->Reserved = 0xFFFFFFFF;
1788 pSMB->WriteMode = 0;
1789 pSMB->Remaining = 0;
1791 /* Can increase buffer size if buffer is big enough in some cases ie we
1792 can send more if LARGE_WRITE_X capability returned by the server and if
1793 our buffer is big enough or if we convert to iovecs on socket writes
1794 and eliminate the copy to the CIFS buffer */
1795 if (tcon->ses->capabilities & CAP_LARGE_WRITE_X) {
1796 bytes_sent = min_t(const unsigned int, CIFSMaxBufSize, count);
1798 bytes_sent = (tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE)
1802 if (bytes_sent > count)
1805 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
1807 memcpy(pSMB->Data, buf, bytes_sent);
1808 else if (count != 0) {
1810 cifs_buf_release(pSMB);
1812 } /* else setting file size with write of zero bytes */
1814 byte_count = bytes_sent + 1; /* pad */
1815 else /* wct == 12 */
1816 byte_count = bytes_sent + 5; /* bigger pad, smaller smb hdr */
1818 pSMB->DataLengthLow = cpu_to_le16(bytes_sent & 0xFFFF);
1819 pSMB->DataLengthHigh = cpu_to_le16(bytes_sent >> 16);
1820 inc_rfc1001_len(pSMB, byte_count);
1823 pSMB->ByteCount = cpu_to_le16(byte_count);
1824 else { /* old style write has byte count 4 bytes earlier
1826 struct smb_com_writex_req *pSMBW =
1827 (struct smb_com_writex_req *)pSMB;
1828 pSMBW->ByteCount = cpu_to_le16(byte_count);
1831 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1832 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
1833 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
1835 cifs_dbg(FYI, "Send error in write = %d\n", rc);
1837 *nbytes = le16_to_cpu(pSMBr->CountHigh);
1838 *nbytes = (*nbytes) << 16;
1839 *nbytes += le16_to_cpu(pSMBr->Count);
1842 * Mask off high 16 bits when bytes written as returned by the
1843 * server is greater than bytes requested by the client. Some
1844 * OS/2 servers are known to set incorrect CountHigh values.
1846 if (*nbytes > count)
1850 cifs_buf_release(pSMB);
1852 /* Note: On -EAGAIN error only caller can retry on handle based calls
1853 since file handle passed in no longer valid */
1859 cifs_writedata_release(struct kref *refcount)
1861 struct cifs_writedata *wdata = container_of(refcount,
1862 struct cifs_writedata, refcount);
1863 #ifdef CONFIG_CIFS_SMB_DIRECT
1865 smbd_deregister_mr(wdata->mr);
1871 cifsFileInfo_put(wdata->cfile);
1873 kvfree(wdata->pages);
1878 * Write failed with a retryable error. Resend the write request. It's also
1879 * possible that the page was redirtied so re-clean the page.
1882 cifs_writev_requeue(struct cifs_writedata *wdata)
1885 struct inode *inode = d_inode(wdata->cfile->dentry);
1886 struct TCP_Server_Info *server;
1887 unsigned int rest_len;
1889 server = tlink_tcon(wdata->cfile->tlink)->ses->server;
1891 rest_len = wdata->bytes;
1893 struct cifs_writedata *wdata2;
1894 unsigned int j, nr_pages, wsize, tailsz, cur_len;
1896 wsize = server->ops->wp_retry_size(inode);
1897 if (wsize < rest_len) {
1898 nr_pages = wsize / PAGE_SIZE;
1903 cur_len = nr_pages * PAGE_SIZE;
1906 nr_pages = DIV_ROUND_UP(rest_len, PAGE_SIZE);
1908 tailsz = rest_len - (nr_pages - 1) * PAGE_SIZE;
1911 wdata2 = cifs_writedata_alloc(nr_pages, cifs_writev_complete);
1917 for (j = 0; j < nr_pages; j++) {
1918 wdata2->pages[j] = wdata->pages[i + j];
1919 lock_page(wdata2->pages[j]);
1920 clear_page_dirty_for_io(wdata2->pages[j]);
1923 wdata2->sync_mode = wdata->sync_mode;
1924 wdata2->nr_pages = nr_pages;
1925 wdata2->offset = page_offset(wdata2->pages[0]);
1926 wdata2->pagesz = PAGE_SIZE;
1927 wdata2->tailsz = tailsz;
1928 wdata2->bytes = cur_len;
1930 rc = cifs_get_writable_file(CIFS_I(inode), FIND_WR_ANY,
1932 if (!wdata2->cfile) {
1933 cifs_dbg(VFS, "No writable handle to retry writepages rc=%d\n",
1935 if (!is_retryable_error(rc))
1938 wdata2->pid = wdata2->cfile->pid;
1939 rc = server->ops->async_writev(wdata2,
1940 cifs_writedata_release);
1943 for (j = 0; j < nr_pages; j++) {
1944 unlock_page(wdata2->pages[j]);
1945 if (rc != 0 && !is_retryable_error(rc)) {
1946 SetPageError(wdata2->pages[j]);
1947 end_page_writeback(wdata2->pages[j]);
1948 put_page(wdata2->pages[j]);
1952 kref_put(&wdata2->refcount, cifs_writedata_release);
1954 if (is_retryable_error(rc))
1960 rest_len -= cur_len;
1962 } while (i < wdata->nr_pages);
1964 /* cleanup remaining pages from the original wdata */
1965 for (; i < wdata->nr_pages; i++) {
1966 SetPageError(wdata->pages[i]);
1967 end_page_writeback(wdata->pages[i]);
1968 put_page(wdata->pages[i]);
1971 if (rc != 0 && !is_retryable_error(rc))
1972 mapping_set_error(inode->i_mapping, rc);
1973 kref_put(&wdata->refcount, cifs_writedata_release);
1977 cifs_writev_complete(struct work_struct *work)
1979 struct cifs_writedata *wdata = container_of(work,
1980 struct cifs_writedata, work);
1981 struct inode *inode = d_inode(wdata->cfile->dentry);
1984 if (wdata->result == 0) {
1985 spin_lock(&inode->i_lock);
1986 cifs_update_eof(CIFS_I(inode), wdata->offset, wdata->bytes);
1987 spin_unlock(&inode->i_lock);
1988 cifs_stats_bytes_written(tlink_tcon(wdata->cfile->tlink),
1990 } else if (wdata->sync_mode == WB_SYNC_ALL && wdata->result == -EAGAIN)
1991 return cifs_writev_requeue(wdata);
1993 for (i = 0; i < wdata->nr_pages; i++) {
1994 struct page *page = wdata->pages[i];
1995 if (wdata->result == -EAGAIN)
1996 __set_page_dirty_nobuffers(page);
1997 else if (wdata->result < 0)
1999 end_page_writeback(page);
2000 cifs_readpage_to_fscache(inode, page);
2003 if (wdata->result != -EAGAIN)
2004 mapping_set_error(inode->i_mapping, wdata->result);
2005 kref_put(&wdata->refcount, cifs_writedata_release);
2008 struct cifs_writedata *
2009 cifs_writedata_alloc(unsigned int nr_pages, work_func_t complete)
2011 struct page **pages =
2012 kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
2014 return cifs_writedata_direct_alloc(pages, complete);
2019 struct cifs_writedata *
2020 cifs_writedata_direct_alloc(struct page **pages, work_func_t complete)
2022 struct cifs_writedata *wdata;
2024 wdata = kzalloc(sizeof(*wdata), GFP_NOFS);
2025 if (wdata != NULL) {
2026 wdata->pages = pages;
2027 kref_init(&wdata->refcount);
2028 INIT_LIST_HEAD(&wdata->list);
2029 init_completion(&wdata->done);
2030 INIT_WORK(&wdata->work, complete);
2036 * Check the mid_state and signature on received buffer (if any), and queue the
2037 * workqueue completion task.
2040 cifs_writev_callback(struct mid_q_entry *mid)
2042 struct cifs_writedata *wdata = mid->callback_data;
2043 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2044 unsigned int written;
2045 WRITE_RSP *smb = (WRITE_RSP *)mid->resp_buf;
2046 struct cifs_credits credits = { .value = 1, .instance = 0 };
2048 switch (mid->mid_state) {
2049 case MID_RESPONSE_RECEIVED:
2050 wdata->result = cifs_check_receive(mid, tcon->ses->server, 0);
2051 if (wdata->result != 0)
2054 written = le16_to_cpu(smb->CountHigh);
2056 written += le16_to_cpu(smb->Count);
2058 * Mask off high 16 bits when bytes written as returned
2059 * by the server is greater than bytes requested by the
2060 * client. OS/2 servers are known to set incorrect
2063 if (written > wdata->bytes)
2066 if (written < wdata->bytes)
2067 wdata->result = -ENOSPC;
2069 wdata->bytes = written;
2071 case MID_REQUEST_SUBMITTED:
2072 case MID_RETRY_NEEDED:
2073 wdata->result = -EAGAIN;
2076 wdata->result = -EIO;
2080 queue_work(cifsiod_wq, &wdata->work);
2081 DeleteMidQEntry(mid);
2082 add_credits(tcon->ses->server, &credits, 0);
2085 /* cifs_async_writev - send an async write, and set up mid to handle result */
2087 cifs_async_writev(struct cifs_writedata *wdata,
2088 void (*release)(struct kref *kref))
2091 WRITE_REQ *smb = NULL;
2093 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2095 struct smb_rqst rqst = { };
2097 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
2101 if (wdata->offset >> 32 > 0) {
2102 /* can not handle big offset for old srv */
2107 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **)&smb);
2109 goto async_writev_out;
2111 smb->hdr.Pid = cpu_to_le16((__u16)wdata->pid);
2112 smb->hdr.PidHigh = cpu_to_le16((__u16)(wdata->pid >> 16));
2114 smb->AndXCommand = 0xFF; /* none */
2115 smb->Fid = wdata->cfile->fid.netfid;
2116 smb->OffsetLow = cpu_to_le32(wdata->offset & 0xFFFFFFFF);
2118 smb->OffsetHigh = cpu_to_le32(wdata->offset >> 32);
2119 smb->Reserved = 0xFFFFFFFF;
2124 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
2126 /* 4 for RFC1001 length + 1 for BCC */
2128 iov[0].iov_base = smb;
2129 iov[1].iov_len = get_rfc1002_length(smb) + 1;
2130 iov[1].iov_base = (char *)smb + 4;
2134 rqst.rq_pages = wdata->pages;
2135 rqst.rq_offset = wdata->page_offset;
2136 rqst.rq_npages = wdata->nr_pages;
2137 rqst.rq_pagesz = wdata->pagesz;
2138 rqst.rq_tailsz = wdata->tailsz;
2140 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2141 wdata->offset, wdata->bytes);
2143 smb->DataLengthLow = cpu_to_le16(wdata->bytes & 0xFFFF);
2144 smb->DataLengthHigh = cpu_to_le16(wdata->bytes >> 16);
2147 inc_rfc1001_len(&smb->hdr, wdata->bytes + 1);
2148 put_bcc(wdata->bytes + 1, &smb->hdr);
2151 struct smb_com_writex_req *smbw =
2152 (struct smb_com_writex_req *)smb;
2153 inc_rfc1001_len(&smbw->hdr, wdata->bytes + 5);
2154 put_bcc(wdata->bytes + 5, &smbw->hdr);
2155 iov[1].iov_len += 4; /* pad bigger by four bytes */
2158 kref_get(&wdata->refcount);
2159 rc = cifs_call_async(tcon->ses->server, &rqst, NULL,
2160 cifs_writev_callback, NULL, wdata, 0, NULL);
2163 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
2165 kref_put(&wdata->refcount, release);
2168 cifs_small_buf_release(smb);
2173 CIFSSMBWrite2(const unsigned int xid, struct cifs_io_parms *io_parms,
2174 unsigned int *nbytes, struct kvec *iov, int n_vec)
2177 WRITE_REQ *pSMB = NULL;
2180 int resp_buf_type = 0;
2181 __u32 pid = io_parms->pid;
2182 __u16 netfid = io_parms->netfid;
2183 __u64 offset = io_parms->offset;
2184 struct cifs_tcon *tcon = io_parms->tcon;
2185 unsigned int count = io_parms->length;
2186 struct kvec rsp_iov;
2190 cifs_dbg(FYI, "write2 at %lld %d bytes\n", (long long)offset, count);
2192 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
2196 if ((offset >> 32) > 0) {
2197 /* can not handle big offset for old srv */
2201 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB);
2205 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
2206 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
2208 /* tcon and ses pointer are checked in smb_init */
2209 if (tcon->ses->server == NULL)
2210 return -ECONNABORTED;
2212 pSMB->AndXCommand = 0xFF; /* none */
2214 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
2216 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
2217 pSMB->Reserved = 0xFFFFFFFF;
2218 pSMB->WriteMode = 0;
2219 pSMB->Remaining = 0;
2222 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
2224 pSMB->DataLengthLow = cpu_to_le16(count & 0xFFFF);
2225 pSMB->DataLengthHigh = cpu_to_le16(count >> 16);
2226 /* header + 1 byte pad */
2227 smb_hdr_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 1;
2229 inc_rfc1001_len(pSMB, count + 1);
2230 else /* wct == 12 */
2231 inc_rfc1001_len(pSMB, count + 5); /* smb data starts later */
2233 pSMB->ByteCount = cpu_to_le16(count + 1);
2234 else /* wct == 12 */ /* bigger pad, smaller smb hdr, keep offset ok */ {
2235 struct smb_com_writex_req *pSMBW =
2236 (struct smb_com_writex_req *)pSMB;
2237 pSMBW->ByteCount = cpu_to_le16(count + 5);
2239 iov[0].iov_base = pSMB;
2241 iov[0].iov_len = smb_hdr_len + 4;
2242 else /* wct == 12 pad bigger by four bytes */
2243 iov[0].iov_len = smb_hdr_len + 8;
2245 rc = SendReceive2(xid, tcon->ses, iov, n_vec + 1, &resp_buf_type, 0,
2247 cifs_small_buf_release(pSMB);
2248 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
2250 cifs_dbg(FYI, "Send error Write2 = %d\n", rc);
2251 } else if (resp_buf_type == 0) {
2252 /* presumably this can not happen, but best to be safe */
2255 WRITE_RSP *pSMBr = (WRITE_RSP *)rsp_iov.iov_base;
2256 *nbytes = le16_to_cpu(pSMBr->CountHigh);
2257 *nbytes = (*nbytes) << 16;
2258 *nbytes += le16_to_cpu(pSMBr->Count);
2261 * Mask off high 16 bits when bytes written as returned by the
2262 * server is greater than bytes requested by the client. OS/2
2263 * servers are known to set incorrect CountHigh values.
2265 if (*nbytes > count)
2269 free_rsp_buf(resp_buf_type, rsp_iov.iov_base);
2271 /* Note: On -EAGAIN error only caller can retry on handle based calls
2272 since file handle passed in no longer valid */
2277 int cifs_lockv(const unsigned int xid, struct cifs_tcon *tcon,
2278 const __u16 netfid, const __u8 lock_type, const __u32 num_unlock,
2279 const __u32 num_lock, LOCKING_ANDX_RANGE *buf)
2282 LOCK_REQ *pSMB = NULL;
2284 struct kvec rsp_iov;
2288 cifs_dbg(FYI, "cifs_lockv num lock %d num unlock %d\n",
2289 num_lock, num_unlock);
2291 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2296 pSMB->NumberOfLocks = cpu_to_le16(num_lock);
2297 pSMB->NumberOfUnlocks = cpu_to_le16(num_unlock);
2298 pSMB->LockType = lock_type;
2299 pSMB->AndXCommand = 0xFF; /* none */
2300 pSMB->Fid = netfid; /* netfid stays le */
2302 count = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2303 inc_rfc1001_len(pSMB, count);
2304 pSMB->ByteCount = cpu_to_le16(count);
2306 iov[0].iov_base = (char *)pSMB;
2307 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4 -
2308 (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2309 iov[1].iov_base = (char *)buf;
2310 iov[1].iov_len = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2312 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
2313 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type,
2314 CIFS_NO_RSP_BUF, &rsp_iov);
2315 cifs_small_buf_release(pSMB);
2317 cifs_dbg(FYI, "Send error in cifs_lockv = %d\n", rc);
2323 CIFSSMBLock(const unsigned int xid, struct cifs_tcon *tcon,
2324 const __u16 smb_file_id, const __u32 netpid, const __u64 len,
2325 const __u64 offset, const __u32 numUnlock,
2326 const __u32 numLock, const __u8 lockType,
2327 const bool waitFlag, const __u8 oplock_level)
2330 LOCK_REQ *pSMB = NULL;
2331 /* LOCK_RSP *pSMBr = NULL; */ /* No response data other than rc to parse */
2336 cifs_dbg(FYI, "CIFSSMBLock timeout %d numLock %d\n",
2337 (int)waitFlag, numLock);
2338 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2343 if (lockType == LOCKING_ANDX_OPLOCK_RELEASE) {
2344 /* no response expected */
2345 flags = CIFS_NO_SRV_RSP | CIFS_NON_BLOCKING | CIFS_OBREAK_OP;
2347 } else if (waitFlag) {
2348 flags = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
2349 pSMB->Timeout = cpu_to_le32(-1);/* blocking - do not time out */
2354 pSMB->NumberOfLocks = cpu_to_le16(numLock);
2355 pSMB->NumberOfUnlocks = cpu_to_le16(numUnlock);
2356 pSMB->LockType = lockType;
2357 pSMB->OplockLevel = oplock_level;
2358 pSMB->AndXCommand = 0xFF; /* none */
2359 pSMB->Fid = smb_file_id; /* netfid stays le */
2361 if ((numLock != 0) || (numUnlock != 0)) {
2362 pSMB->Locks[0].Pid = cpu_to_le16(netpid);
2363 /* BB where to store pid high? */
2364 pSMB->Locks[0].LengthLow = cpu_to_le32((u32)len);
2365 pSMB->Locks[0].LengthHigh = cpu_to_le32((u32)(len>>32));
2366 pSMB->Locks[0].OffsetLow = cpu_to_le32((u32)offset);
2367 pSMB->Locks[0].OffsetHigh = cpu_to_le32((u32)(offset>>32));
2368 count = sizeof(LOCKING_ANDX_RANGE);
2373 inc_rfc1001_len(pSMB, count);
2374 pSMB->ByteCount = cpu_to_le16(count);
2377 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
2378 (struct smb_hdr *) pSMB, &bytes_returned);
2380 rc = SendReceiveNoRsp(xid, tcon->ses, (char *)pSMB, flags);
2381 cifs_small_buf_release(pSMB);
2382 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
2384 cifs_dbg(FYI, "Send error in Lock = %d\n", rc);
2386 /* Note: On -EAGAIN error only caller can retry on handle based calls
2387 since file handle passed in no longer valid */
2392 CIFSSMBPosixLock(const unsigned int xid, struct cifs_tcon *tcon,
2393 const __u16 smb_file_id, const __u32 netpid,
2394 const loff_t start_offset, const __u64 len,
2395 struct file_lock *pLockData, const __u16 lock_type,
2396 const bool waitFlag)
2398 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2399 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
2400 struct cifs_posix_lock *parm_data;
2403 int bytes_returned = 0;
2404 int resp_buf_type = 0;
2405 __u16 params, param_offset, offset, byte_count, count;
2407 struct kvec rsp_iov;
2409 cifs_dbg(FYI, "Posix Lock\n");
2411 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
2416 pSMBr = (struct smb_com_transaction2_sfi_rsp *)pSMB;
2419 pSMB->MaxSetupCount = 0;
2422 pSMB->Reserved2 = 0;
2423 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2424 offset = param_offset + params;
2426 count = sizeof(struct cifs_posix_lock);
2427 pSMB->MaxParameterCount = cpu_to_le16(2);
2428 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
2429 pSMB->SetupCount = 1;
2430 pSMB->Reserved3 = 0;
2432 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
2434 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2435 byte_count = 3 /* pad */ + params + count;
2436 pSMB->DataCount = cpu_to_le16(count);
2437 pSMB->ParameterCount = cpu_to_le16(params);
2438 pSMB->TotalDataCount = pSMB->DataCount;
2439 pSMB->TotalParameterCount = pSMB->ParameterCount;
2440 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2441 /* SMB offsets are from the beginning of SMB which is 4 bytes in, after RFC1001 field */
2442 parm_data = (struct cifs_posix_lock *)
2443 (((char *)pSMB) + offset + 4);
2445 parm_data->lock_type = cpu_to_le16(lock_type);
2447 timeout = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
2448 parm_data->lock_flags = cpu_to_le16(1);
2449 pSMB->Timeout = cpu_to_le32(-1);
2453 parm_data->pid = cpu_to_le32(netpid);
2454 parm_data->start = cpu_to_le64(start_offset);
2455 parm_data->length = cpu_to_le64(len); /* normalize negative numbers */
2457 pSMB->DataOffset = cpu_to_le16(offset);
2458 pSMB->Fid = smb_file_id;
2459 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_LOCK);
2460 pSMB->Reserved4 = 0;
2461 inc_rfc1001_len(pSMB, byte_count);
2462 pSMB->ByteCount = cpu_to_le16(byte_count);
2464 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
2465 (struct smb_hdr *) pSMBr, &bytes_returned);
2467 iov[0].iov_base = (char *)pSMB;
2468 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
2469 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
2470 &resp_buf_type, timeout, &rsp_iov);
2471 pSMBr = (struct smb_com_transaction2_sfi_rsp *)rsp_iov.iov_base;
2473 cifs_small_buf_release(pSMB);
2476 cifs_dbg(FYI, "Send error in Posix Lock = %d\n", rc);
2477 } else if (pLockData) {
2478 /* lock structure can be returned on get */
2481 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
2483 if (rc || get_bcc(&pSMBr->hdr) < sizeof(*parm_data)) {
2484 rc = -EIO; /* bad smb */
2487 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
2488 data_count = le16_to_cpu(pSMBr->t2.DataCount);
2489 if (data_count < sizeof(struct cifs_posix_lock)) {
2493 parm_data = (struct cifs_posix_lock *)
2494 ((char *)&pSMBr->hdr.Protocol + data_offset);
2495 if (parm_data->lock_type == cpu_to_le16(CIFS_UNLCK))
2496 pLockData->fl_type = F_UNLCK;
2498 if (parm_data->lock_type ==
2499 cpu_to_le16(CIFS_RDLCK))
2500 pLockData->fl_type = F_RDLCK;
2501 else if (parm_data->lock_type ==
2502 cpu_to_le16(CIFS_WRLCK))
2503 pLockData->fl_type = F_WRLCK;
2505 pLockData->fl_start = le64_to_cpu(parm_data->start);
2506 pLockData->fl_end = pLockData->fl_start +
2507 le64_to_cpu(parm_data->length) - 1;
2508 pLockData->fl_pid = -le32_to_cpu(parm_data->pid);
2513 free_rsp_buf(resp_buf_type, rsp_iov.iov_base);
2515 /* Note: On -EAGAIN error only caller can retry on handle based calls
2516 since file handle passed in no longer valid */
2523 CIFSSMBClose(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
2526 CLOSE_REQ *pSMB = NULL;
2527 cifs_dbg(FYI, "In CIFSSMBClose\n");
2529 /* do not retry on dead session on close */
2530 rc = small_smb_init(SMB_COM_CLOSE, 3, tcon, (void **) &pSMB);
2536 pSMB->FileID = (__u16) smb_file_id;
2537 pSMB->LastWriteTime = 0xFFFFFFFF;
2538 pSMB->ByteCount = 0;
2539 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
2540 cifs_small_buf_release(pSMB);
2541 cifs_stats_inc(&tcon->stats.cifs_stats.num_closes);
2544 /* EINTR is expected when user ctl-c to kill app */
2545 cifs_dbg(VFS, "Send error in Close = %d\n", rc);
2549 /* Since session is dead, file will be closed on server already */
2557 CIFSSMBFlush(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
2560 FLUSH_REQ *pSMB = NULL;
2561 cifs_dbg(FYI, "In CIFSSMBFlush\n");
2563 rc = small_smb_init(SMB_COM_FLUSH, 1, tcon, (void **) &pSMB);
2567 pSMB->FileID = (__u16) smb_file_id;
2568 pSMB->ByteCount = 0;
2569 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
2570 cifs_small_buf_release(pSMB);
2571 cifs_stats_inc(&tcon->stats.cifs_stats.num_flushes);
2573 cifs_dbg(VFS, "Send error in Flush = %d\n", rc);
2579 CIFSSMBRename(const unsigned int xid, struct cifs_tcon *tcon,
2580 const char *from_name, const char *to_name,
2581 struct cifs_sb_info *cifs_sb)
2584 RENAME_REQ *pSMB = NULL;
2585 RENAME_RSP *pSMBr = NULL;
2587 int name_len, name_len2;
2589 int remap = cifs_remap(cifs_sb);
2591 cifs_dbg(FYI, "In CIFSSMBRename\n");
2593 rc = smb_init(SMB_COM_RENAME, 1, tcon, (void **) &pSMB,
2598 pSMB->BufferFormat = 0x04;
2599 pSMB->SearchAttributes =
2600 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
2603 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2604 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2605 from_name, PATH_MAX,
2606 cifs_sb->local_nls, remap);
2607 name_len++; /* trailing null */
2609 pSMB->OldFileName[name_len] = 0x04; /* pad */
2610 /* protocol requires ASCII signature byte on Unicode string */
2611 pSMB->OldFileName[name_len + 1] = 0x00;
2613 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2614 to_name, PATH_MAX, cifs_sb->local_nls,
2616 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2617 name_len2 *= 2; /* convert to bytes */
2619 name_len = copy_path_name(pSMB->OldFileName, from_name);
2620 name_len2 = copy_path_name(pSMB->OldFileName+name_len+1, to_name);
2621 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2622 name_len2++; /* signature byte */
2625 count = 1 /* 1st signature byte */ + name_len + name_len2;
2626 inc_rfc1001_len(pSMB, count);
2627 pSMB->ByteCount = cpu_to_le16(count);
2629 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2630 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2631 cifs_stats_inc(&tcon->stats.cifs_stats.num_renames);
2633 cifs_dbg(FYI, "Send error in rename = %d\n", rc);
2635 cifs_buf_release(pSMB);
2643 int CIFSSMBRenameOpenFile(const unsigned int xid, struct cifs_tcon *pTcon,
2644 int netfid, const char *target_name,
2645 const struct nls_table *nls_codepage, int remap)
2647 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2648 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
2649 struct set_file_rename *rename_info;
2651 char dummy_string[30];
2653 int bytes_returned = 0;
2655 __u16 params, param_offset, offset, count, byte_count;
2657 cifs_dbg(FYI, "Rename to File by handle\n");
2658 rc = smb_init(SMB_COM_TRANSACTION2, 15, pTcon, (void **) &pSMB,
2664 pSMB->MaxSetupCount = 0;
2668 pSMB->Reserved2 = 0;
2669 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2670 offset = param_offset + params;
2672 /* SMB offsets are from the beginning of SMB which is 4 bytes in, after RFC1001 field */
2673 data_offset = (char *)(pSMB) + offset + 4;
2674 rename_info = (struct set_file_rename *) data_offset;
2675 pSMB->MaxParameterCount = cpu_to_le16(2);
2676 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
2677 pSMB->SetupCount = 1;
2678 pSMB->Reserved3 = 0;
2679 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2680 byte_count = 3 /* pad */ + params;
2681 pSMB->ParameterCount = cpu_to_le16(params);
2682 pSMB->TotalParameterCount = pSMB->ParameterCount;
2683 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2684 pSMB->DataOffset = cpu_to_le16(offset);
2685 /* construct random name ".cifs_tmp<inodenum><mid>" */
2686 rename_info->overwrite = cpu_to_le32(1);
2687 rename_info->root_fid = 0;
2688 /* unicode only call */
2689 if (target_name == NULL) {
2690 sprintf(dummy_string, "cifs%x", pSMB->hdr.Mid);
2692 cifsConvertToUTF16((__le16 *)rename_info->target_name,
2693 dummy_string, 24, nls_codepage, remap);
2696 cifsConvertToUTF16((__le16 *)rename_info->target_name,
2697 target_name, PATH_MAX, nls_codepage,
2700 rename_info->target_name_len = cpu_to_le32(2 * len_of_str);
2701 count = 12 /* sizeof(struct set_file_rename) */ + (2 * len_of_str);
2702 byte_count += count;
2703 pSMB->DataCount = cpu_to_le16(count);
2704 pSMB->TotalDataCount = pSMB->DataCount;
2706 pSMB->InformationLevel =
2707 cpu_to_le16(SMB_SET_FILE_RENAME_INFORMATION);
2708 pSMB->Reserved4 = 0;
2709 inc_rfc1001_len(pSMB, byte_count);
2710 pSMB->ByteCount = cpu_to_le16(byte_count);
2711 rc = SendReceive(xid, pTcon->ses, (struct smb_hdr *) pSMB,
2712 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2713 cifs_stats_inc(&pTcon->stats.cifs_stats.num_t2renames);
2715 cifs_dbg(FYI, "Send error in Rename (by file handle) = %d\n",
2718 cifs_buf_release(pSMB);
2720 /* Note: On -EAGAIN error only caller can retry on handle based calls
2721 since file handle passed in no longer valid */
2727 CIFSSMBCopy(const unsigned int xid, struct cifs_tcon *tcon,
2728 const char *fromName, const __u16 target_tid, const char *toName,
2729 const int flags, const struct nls_table *nls_codepage, int remap)
2732 COPY_REQ *pSMB = NULL;
2733 COPY_RSP *pSMBr = NULL;
2735 int name_len, name_len2;
2738 cifs_dbg(FYI, "In CIFSSMBCopy\n");
2740 rc = smb_init(SMB_COM_COPY, 1, tcon, (void **) &pSMB,
2745 pSMB->BufferFormat = 0x04;
2746 pSMB->Tid2 = target_tid;
2748 pSMB->Flags = cpu_to_le16(flags & COPY_TREE);
2750 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2751 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2752 fromName, PATH_MAX, nls_codepage,
2754 name_len++; /* trailing null */
2756 pSMB->OldFileName[name_len] = 0x04; /* pad */
2757 /* protocol requires ASCII signature byte on Unicode string */
2758 pSMB->OldFileName[name_len + 1] = 0x00;
2760 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2761 toName, PATH_MAX, nls_codepage, remap);
2762 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2763 name_len2 *= 2; /* convert to bytes */
2765 name_len = copy_path_name(pSMB->OldFileName, fromName);
2766 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2767 name_len2 = copy_path_name(pSMB->OldFileName+name_len+1, toName);
2768 name_len2++; /* signature byte */
2771 count = 1 /* 1st signature byte */ + name_len + name_len2;
2772 inc_rfc1001_len(pSMB, count);
2773 pSMB->ByteCount = cpu_to_le16(count);
2775 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2776 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2778 cifs_dbg(FYI, "Send error in copy = %d with %d files copied\n",
2779 rc, le16_to_cpu(pSMBr->CopyCount));
2781 cifs_buf_release(pSMB);
2790 CIFSUnixCreateSymLink(const unsigned int xid, struct cifs_tcon *tcon,
2791 const char *fromName, const char *toName,
2792 const struct nls_table *nls_codepage, int remap)
2794 TRANSACTION2_SPI_REQ *pSMB = NULL;
2795 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2798 int name_len_target;
2800 int bytes_returned = 0;
2801 __u16 params, param_offset, offset, byte_count;
2803 cifs_dbg(FYI, "In Symlink Unix style\n");
2805 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2810 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2812 cifsConvertToUTF16((__le16 *) pSMB->FileName, fromName,
2813 /* find define for this maxpathcomponent */
2814 PATH_MAX, nls_codepage, remap);
2815 name_len++; /* trailing null */
2819 name_len = copy_path_name(pSMB->FileName, fromName);
2821 params = 6 + name_len;
2822 pSMB->MaxSetupCount = 0;
2826 pSMB->Reserved2 = 0;
2827 param_offset = offsetof(struct smb_com_transaction2_spi_req,
2828 InformationLevel) - 4;
2829 offset = param_offset + params;
2831 /* SMB offsets are from the beginning of SMB which is 4 bytes in, after RFC1001 field */
2832 data_offset = (char *)pSMB + offset + 4;
2833 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2835 cifsConvertToUTF16((__le16 *) data_offset, toName,
2836 /* find define for this maxpathcomponent */
2837 PATH_MAX, nls_codepage, remap);
2838 name_len_target++; /* trailing null */
2839 name_len_target *= 2;
2841 name_len_target = copy_path_name(data_offset, toName);
2844 pSMB->MaxParameterCount = cpu_to_le16(2);
2845 /* BB find exact max on data count below from sess */
2846 pSMB->MaxDataCount = cpu_to_le16(1000);
2847 pSMB->SetupCount = 1;
2848 pSMB->Reserved3 = 0;
2849 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2850 byte_count = 3 /* pad */ + params + name_len_target;
2851 pSMB->DataCount = cpu_to_le16(name_len_target);
2852 pSMB->ParameterCount = cpu_to_le16(params);
2853 pSMB->TotalDataCount = pSMB->DataCount;
2854 pSMB->TotalParameterCount = pSMB->ParameterCount;
2855 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2856 pSMB->DataOffset = cpu_to_le16(offset);
2857 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_LINK);
2858 pSMB->Reserved4 = 0;
2859 inc_rfc1001_len(pSMB, byte_count);
2860 pSMB->ByteCount = cpu_to_le16(byte_count);
2861 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2862 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2863 cifs_stats_inc(&tcon->stats.cifs_stats.num_symlinks);
2865 cifs_dbg(FYI, "Send error in SetPathInfo create symlink = %d\n",
2868 cifs_buf_release(pSMB);
2871 goto createSymLinkRetry;
2877 CIFSUnixCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon,
2878 const char *fromName, const char *toName,
2879 const struct nls_table *nls_codepage, int remap)
2881 TRANSACTION2_SPI_REQ *pSMB = NULL;
2882 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2885 int name_len_target;
2887 int bytes_returned = 0;
2888 __u16 params, param_offset, offset, byte_count;
2890 cifs_dbg(FYI, "In Create Hard link Unix style\n");
2891 createHardLinkRetry:
2892 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2897 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2898 name_len = cifsConvertToUTF16((__le16 *) pSMB->FileName, toName,
2899 PATH_MAX, nls_codepage, remap);
2900 name_len++; /* trailing null */
2904 name_len = copy_path_name(pSMB->FileName, toName);
2906 params = 6 + name_len;
2907 pSMB->MaxSetupCount = 0;
2911 pSMB->Reserved2 = 0;
2912 param_offset = offsetof(struct smb_com_transaction2_spi_req,
2913 InformationLevel) - 4;
2914 offset = param_offset + params;
2916 /* SMB offsets are from the beginning of SMB which is 4 bytes in, after RFC1001 field */
2917 data_offset = (char *)pSMB + offset + 4;
2918 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2920 cifsConvertToUTF16((__le16 *) data_offset, fromName,
2921 PATH_MAX, nls_codepage, remap);
2922 name_len_target++; /* trailing null */
2923 name_len_target *= 2;
2925 name_len_target = copy_path_name(data_offset, fromName);
2928 pSMB->MaxParameterCount = cpu_to_le16(2);
2929 /* BB find exact max on data count below from sess*/
2930 pSMB->MaxDataCount = cpu_to_le16(1000);
2931 pSMB->SetupCount = 1;
2932 pSMB->Reserved3 = 0;
2933 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2934 byte_count = 3 /* pad */ + params + name_len_target;
2935 pSMB->ParameterCount = cpu_to_le16(params);
2936 pSMB->TotalParameterCount = pSMB->ParameterCount;
2937 pSMB->DataCount = cpu_to_le16(name_len_target);
2938 pSMB->TotalDataCount = pSMB->DataCount;
2939 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2940 pSMB->DataOffset = cpu_to_le16(offset);
2941 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_HLINK);
2942 pSMB->Reserved4 = 0;
2943 inc_rfc1001_len(pSMB, byte_count);
2944 pSMB->ByteCount = cpu_to_le16(byte_count);
2945 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2946 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2947 cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
2949 cifs_dbg(FYI, "Send error in SetPathInfo (hard link) = %d\n",
2952 cifs_buf_release(pSMB);
2954 goto createHardLinkRetry;
2960 CIFSCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon,
2961 const char *from_name, const char *to_name,
2962 struct cifs_sb_info *cifs_sb)
2965 NT_RENAME_REQ *pSMB = NULL;
2966 RENAME_RSP *pSMBr = NULL;
2968 int name_len, name_len2;
2970 int remap = cifs_remap(cifs_sb);
2972 cifs_dbg(FYI, "In CIFSCreateHardLink\n");
2973 winCreateHardLinkRetry:
2975 rc = smb_init(SMB_COM_NT_RENAME, 4, tcon, (void **) &pSMB,
2980 pSMB->SearchAttributes =
2981 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
2983 pSMB->Flags = cpu_to_le16(CREATE_HARD_LINK);
2984 pSMB->ClusterCount = 0;
2986 pSMB->BufferFormat = 0x04;
2988 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2990 cifsConvertToUTF16((__le16 *) pSMB->OldFileName, from_name,
2991 PATH_MAX, cifs_sb->local_nls, remap);
2992 name_len++; /* trailing null */
2995 /* protocol specifies ASCII buffer format (0x04) for unicode */
2996 pSMB->OldFileName[name_len] = 0x04;
2997 pSMB->OldFileName[name_len + 1] = 0x00; /* pad */
2999 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
3000 to_name, PATH_MAX, cifs_sb->local_nls,
3002 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
3003 name_len2 *= 2; /* convert to bytes */
3005 name_len = copy_path_name(pSMB->OldFileName, from_name);
3006 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
3007 name_len2 = copy_path_name(pSMB->OldFileName+name_len+1, to_name);
3008 name_len2++; /* signature byte */
3011 count = 1 /* string type byte */ + name_len + name_len2;
3012 inc_rfc1001_len(pSMB, count);
3013 pSMB->ByteCount = cpu_to_le16(count);
3015 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3016 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3017 cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
3019 cifs_dbg(FYI, "Send error in hard link (NT rename) = %d\n", rc);
3021 cifs_buf_release(pSMB);
3023 goto winCreateHardLinkRetry;
3029 CIFSSMBUnixQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon,
3030 const unsigned char *searchName, char **symlinkinfo,
3031 const struct nls_table *nls_codepage, int remap)
3033 /* SMB_QUERY_FILE_UNIX_LINK */
3034 TRANSACTION2_QPI_REQ *pSMB = NULL;
3035 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3039 __u16 params, byte_count;
3042 cifs_dbg(FYI, "In QPathSymLinkInfo (Unix) for path %s\n", searchName);
3045 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3050 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3052 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3053 searchName, PATH_MAX, nls_codepage,
3055 name_len++; /* trailing null */
3058 name_len = copy_path_name(pSMB->FileName, searchName);
3061 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3062 pSMB->TotalDataCount = 0;
3063 pSMB->MaxParameterCount = cpu_to_le16(2);
3064 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
3065 pSMB->MaxSetupCount = 0;
3069 pSMB->Reserved2 = 0;
3070 pSMB->ParameterOffset = cpu_to_le16(offsetof(
3071 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
3072 pSMB->DataCount = 0;
3073 pSMB->DataOffset = 0;
3074 pSMB->SetupCount = 1;
3075 pSMB->Reserved3 = 0;
3076 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3077 byte_count = params + 1 /* pad */ ;
3078 pSMB->TotalParameterCount = cpu_to_le16(params);
3079 pSMB->ParameterCount = pSMB->TotalParameterCount;
3080 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_LINK);
3081 pSMB->Reserved4 = 0;
3082 inc_rfc1001_len(pSMB, byte_count);
3083 pSMB->ByteCount = cpu_to_le16(byte_count);
3085 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3086 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3088 cifs_dbg(FYI, "Send error in QuerySymLinkInfo = %d\n", rc);
3090 /* decode response */
3092 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
3093 /* BB also check enough total bytes returned */
3094 if (rc || get_bcc(&pSMBr->hdr) < 2)
3098 u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3100 data_start = ((char *) &pSMBr->hdr.Protocol) +
3101 le16_to_cpu(pSMBr->t2.DataOffset);
3103 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3108 /* BB FIXME investigate remapping reserved chars here */
3109 *symlinkinfo = cifs_strndup_from_utf16(data_start,
3110 count, is_unicode, nls_codepage);
3115 cifs_buf_release(pSMB);
3117 goto querySymLinkRetry;
3122 * Recent Windows versions now create symlinks more frequently
3123 * and they use the "reparse point" mechanism below. We can of course
3124 * do symlinks nicely to Samba and other servers which support the
3125 * CIFS Unix Extensions and we can also do SFU symlinks and "client only"
3126 * "MF" symlinks optionally, but for recent Windows we really need to
3127 * reenable the code below and fix the cifs_symlink callers to handle this.
3128 * In the interim this code has been moved to its own config option so
3129 * it is not compiled in by default until callers fixed up and more tested.
3132 CIFSSMBQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon,
3133 __u16 fid, char **symlinkinfo,
3134 const struct nls_table *nls_codepage)
3138 struct smb_com_transaction_ioctl_req *pSMB;
3139 struct smb_com_transaction_ioctl_rsp *pSMBr;
3141 unsigned int sub_len;
3143 struct reparse_symlink_data *reparse_buf;
3144 struct reparse_posix_data *posix_buf;
3145 __u32 data_offset, data_count;
3148 cifs_dbg(FYI, "In Windows reparse style QueryLink for fid %u\n", fid);
3149 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3154 pSMB->TotalParameterCount = 0 ;
3155 pSMB->TotalDataCount = 0;
3156 pSMB->MaxParameterCount = cpu_to_le32(2);
3157 /* BB find exact data count max from sess structure BB */
3158 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
3159 pSMB->MaxSetupCount = 4;
3161 pSMB->ParameterOffset = 0;
3162 pSMB->DataCount = 0;
3163 pSMB->DataOffset = 0;
3164 pSMB->SetupCount = 4;
3165 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
3166 pSMB->ParameterCount = pSMB->TotalParameterCount;
3167 pSMB->FunctionCode = cpu_to_le32(FSCTL_GET_REPARSE_POINT);
3168 pSMB->IsFsctl = 1; /* FSCTL */
3169 pSMB->IsRootFlag = 0;
3170 pSMB->Fid = fid; /* file handle always le */
3171 pSMB->ByteCount = 0;
3173 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3174 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3176 cifs_dbg(FYI, "Send error in QueryReparseLinkInfo = %d\n", rc);
3180 data_offset = le32_to_cpu(pSMBr->DataOffset);
3181 data_count = le32_to_cpu(pSMBr->DataCount);
3182 if (get_bcc(&pSMBr->hdr) < 2 || data_offset > 512) {
3183 /* BB also check enough total bytes returned */
3184 rc = -EIO; /* bad smb */
3187 if (!data_count || (data_count > 2048)) {
3189 cifs_dbg(FYI, "Invalid return data count on get reparse info ioctl\n");
3192 end_of_smb = 2 + get_bcc(&pSMBr->hdr) + (char *)&pSMBr->ByteCount;
3193 reparse_buf = (struct reparse_symlink_data *)
3194 ((char *)&pSMBr->hdr.Protocol + data_offset);
3195 if ((char *)reparse_buf >= end_of_smb) {
3199 if (reparse_buf->ReparseTag == cpu_to_le32(IO_REPARSE_TAG_NFS)) {
3200 cifs_dbg(FYI, "NFS style reparse tag\n");
3201 posix_buf = (struct reparse_posix_data *)reparse_buf;
3203 if (posix_buf->InodeType != cpu_to_le64(NFS_SPECFILE_LNK)) {
3204 cifs_dbg(FYI, "unsupported file type 0x%llx\n",
3205 le64_to_cpu(posix_buf->InodeType));
3210 sub_len = le16_to_cpu(reparse_buf->ReparseDataLength);
3211 if (posix_buf->PathBuffer + sub_len > end_of_smb) {
3212 cifs_dbg(FYI, "reparse buf beyond SMB\n");
3216 *symlinkinfo = cifs_strndup_from_utf16(posix_buf->PathBuffer,
3217 sub_len, is_unicode, nls_codepage);
3219 } else if (reparse_buf->ReparseTag !=
3220 cpu_to_le32(IO_REPARSE_TAG_SYMLINK)) {
3225 /* Reparse tag is NTFS symlink */
3226 sub_start = le16_to_cpu(reparse_buf->SubstituteNameOffset) +
3227 reparse_buf->PathBuffer;
3228 sub_len = le16_to_cpu(reparse_buf->SubstituteNameLength);
3229 if (sub_start + sub_len > end_of_smb) {
3230 cifs_dbg(FYI, "reparse buf beyond SMB\n");
3234 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3239 /* BB FIXME investigate remapping reserved chars here */
3240 *symlinkinfo = cifs_strndup_from_utf16(sub_start, sub_len, is_unicode,
3245 cifs_buf_release(pSMB);
3248 * Note: On -EAGAIN error only caller can retry on handle based calls
3249 * since file handle passed in no longer valid.
3255 CIFSSMB_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
3260 struct smb_com_transaction_compr_ioctl_req *pSMB;
3261 struct smb_com_transaction_ioctl_rsp *pSMBr;
3263 cifs_dbg(FYI, "Set compression for %u\n", fid);
3264 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3269 pSMB->compression_state = cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
3271 pSMB->TotalParameterCount = 0;
3272 pSMB->TotalDataCount = cpu_to_le32(2);
3273 pSMB->MaxParameterCount = 0;
3274 pSMB->MaxDataCount = 0;
3275 pSMB->MaxSetupCount = 4;
3277 pSMB->ParameterOffset = 0;
3278 pSMB->DataCount = cpu_to_le32(2);
3280 cpu_to_le32(offsetof(struct smb_com_transaction_compr_ioctl_req,
3281 compression_state) - 4); /* 84 */
3282 pSMB->SetupCount = 4;
3283 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
3284 pSMB->ParameterCount = 0;
3285 pSMB->FunctionCode = cpu_to_le32(FSCTL_SET_COMPRESSION);
3286 pSMB->IsFsctl = 1; /* FSCTL */
3287 pSMB->IsRootFlag = 0;
3288 pSMB->Fid = fid; /* file handle always le */
3289 /* 3 byte pad, followed by 2 byte compress state */
3290 pSMB->ByteCount = cpu_to_le16(5);
3291 inc_rfc1001_len(pSMB, 5);
3293 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3294 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3296 cifs_dbg(FYI, "Send error in SetCompression = %d\n", rc);
3298 cifs_buf_release(pSMB);
3301 * Note: On -EAGAIN error only caller can retry on handle based calls
3302 * since file handle passed in no longer valid.
3308 #ifdef CONFIG_CIFS_POSIX
3310 /*Convert an Access Control Entry from wire format to local POSIX xattr format*/
3311 static void cifs_convert_ace(struct posix_acl_xattr_entry *ace,
3312 struct cifs_posix_ace *cifs_ace)
3314 /* u8 cifs fields do not need le conversion */
3315 ace->e_perm = cpu_to_le16(cifs_ace->cifs_e_perm);
3316 ace->e_tag = cpu_to_le16(cifs_ace->cifs_e_tag);
3317 ace->e_id = cpu_to_le32(le64_to_cpu(cifs_ace->cifs_uid));
3319 cifs_dbg(FYI, "perm %d tag %d id %d\n",
3320 ace->e_perm, ace->e_tag, ace->e_id);
3326 /* Convert ACL from CIFS POSIX wire format to local Linux POSIX ACL xattr */
3327 static int cifs_copy_posix_acl(char *trgt, char *src, const int buflen,
3328 const int acl_type, const int size_of_data_area)
3333 struct cifs_posix_ace *pACE;
3334 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)src;
3335 struct posix_acl_xattr_header *local_acl = (void *)trgt;
3337 if (le16_to_cpu(cifs_acl->version) != CIFS_ACL_VERSION)
3340 if (acl_type == ACL_TYPE_ACCESS) {
3341 count = le16_to_cpu(cifs_acl->access_entry_count);
3342 pACE = &cifs_acl->ace_array[0];
3343 size = sizeof(struct cifs_posix_acl);
3344 size += sizeof(struct cifs_posix_ace) * count;
3345 /* check if we would go beyond end of SMB */
3346 if (size_of_data_area < size) {
3347 cifs_dbg(FYI, "bad CIFS POSIX ACL size %d vs. %d\n",
3348 size_of_data_area, size);
3351 } else if (acl_type == ACL_TYPE_DEFAULT) {
3352 count = le16_to_cpu(cifs_acl->access_entry_count);
3353 size = sizeof(struct cifs_posix_acl);
3354 size += sizeof(struct cifs_posix_ace) * count;
3355 /* skip past access ACEs to get to default ACEs */
3356 pACE = &cifs_acl->ace_array[count];
3357 count = le16_to_cpu(cifs_acl->default_entry_count);
3358 size += sizeof(struct cifs_posix_ace) * count;
3359 /* check if we would go beyond end of SMB */
3360 if (size_of_data_area < size)
3367 size = posix_acl_xattr_size(count);
3368 if ((buflen == 0) || (local_acl == NULL)) {
3369 /* used to query ACL EA size */
3370 } else if (size > buflen) {
3372 } else /* buffer big enough */ {
3373 struct posix_acl_xattr_entry *ace = (void *)(local_acl + 1);
3375 local_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
3376 for (i = 0; i < count ; i++) {
3377 cifs_convert_ace(&ace[i], pACE);
3384 static void convert_ace_to_cifs_ace(struct cifs_posix_ace *cifs_ace,
3385 const struct posix_acl_xattr_entry *local_ace)
3387 cifs_ace->cifs_e_perm = le16_to_cpu(local_ace->e_perm);
3388 cifs_ace->cifs_e_tag = le16_to_cpu(local_ace->e_tag);
3389 /* BB is there a better way to handle the large uid? */
3390 if (local_ace->e_id == cpu_to_le32(-1)) {
3391 /* Probably no need to le convert -1 on any arch but can not hurt */
3392 cifs_ace->cifs_uid = cpu_to_le64(-1);
3394 cifs_ace->cifs_uid = cpu_to_le64(le32_to_cpu(local_ace->e_id));
3396 cifs_dbg(FYI, "perm %d tag %d id %d\n",
3397 ace->e_perm, ace->e_tag, ace->e_id);
3401 /* Convert ACL from local Linux POSIX xattr to CIFS POSIX ACL wire format */
3402 static __u16 ACL_to_cifs_posix(char *parm_data, const char *pACL,
3403 const int buflen, const int acl_type)
3406 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)parm_data;
3407 struct posix_acl_xattr_header *local_acl = (void *)pACL;
3408 struct posix_acl_xattr_entry *ace = (void *)(local_acl + 1);
3412 if ((buflen == 0) || (pACL == NULL) || (cifs_acl == NULL))
3415 count = posix_acl_xattr_count((size_t)buflen);
3416 cifs_dbg(FYI, "setting acl with %d entries from buf of length %d and version of %d\n",
3417 count, buflen, le32_to_cpu(local_acl->a_version));
3418 if (le32_to_cpu(local_acl->a_version) != 2) {
3419 cifs_dbg(FYI, "unknown POSIX ACL version %d\n",
3420 le32_to_cpu(local_acl->a_version));
3423 cifs_acl->version = cpu_to_le16(1);
3424 if (acl_type == ACL_TYPE_ACCESS) {
3425 cifs_acl->access_entry_count = cpu_to_le16(count);
3426 cifs_acl->default_entry_count = cpu_to_le16(0xFFFF);
3427 } else if (acl_type == ACL_TYPE_DEFAULT) {
3428 cifs_acl->default_entry_count = cpu_to_le16(count);
3429 cifs_acl->access_entry_count = cpu_to_le16(0xFFFF);
3431 cifs_dbg(FYI, "unknown ACL type %d\n", acl_type);
3434 for (i = 0; i < count; i++)
3435 convert_ace_to_cifs_ace(&cifs_acl->ace_array[i], &ace[i]);
3437 rc = (__u16)(count * sizeof(struct cifs_posix_ace));
3438 rc += sizeof(struct cifs_posix_acl);
3439 /* BB add check to make sure ACL does not overflow SMB */
3445 CIFSSMBGetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
3446 const unsigned char *searchName,
3447 char *acl_inf, const int buflen, const int acl_type,
3448 const struct nls_table *nls_codepage, int remap)
3450 /* SMB_QUERY_POSIX_ACL */
3451 TRANSACTION2_QPI_REQ *pSMB = NULL;
3452 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3456 __u16 params, byte_count;
3458 cifs_dbg(FYI, "In GetPosixACL (Unix) for path %s\n", searchName);
3461 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3466 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3468 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3469 searchName, PATH_MAX, nls_codepage,
3471 name_len++; /* trailing null */
3473 pSMB->FileName[name_len] = 0;
3474 pSMB->FileName[name_len+1] = 0;
3476 name_len = copy_path_name(pSMB->FileName, searchName);
3479 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3480 pSMB->TotalDataCount = 0;
3481 pSMB->MaxParameterCount = cpu_to_le16(2);
3482 /* BB find exact max data count below from sess structure BB */
3483 pSMB->MaxDataCount = cpu_to_le16(4000);
3484 pSMB->MaxSetupCount = 0;
3488 pSMB->Reserved2 = 0;
3489 pSMB->ParameterOffset = cpu_to_le16(
3490 offsetof(struct smb_com_transaction2_qpi_req,
3491 InformationLevel) - 4);
3492 pSMB->DataCount = 0;
3493 pSMB->DataOffset = 0;
3494 pSMB->SetupCount = 1;
3495 pSMB->Reserved3 = 0;
3496 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3497 byte_count = params + 1 /* pad */ ;
3498 pSMB->TotalParameterCount = cpu_to_le16(params);
3499 pSMB->ParameterCount = pSMB->TotalParameterCount;
3500 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_ACL);
3501 pSMB->Reserved4 = 0;
3502 inc_rfc1001_len(pSMB, byte_count);
3503 pSMB->ByteCount = cpu_to_le16(byte_count);
3505 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3506 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3507 cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
3509 cifs_dbg(FYI, "Send error in Query POSIX ACL = %d\n", rc);
3511 /* decode response */
3513 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
3514 /* BB also check enough total bytes returned */
3515 if (rc || get_bcc(&pSMBr->hdr) < 2)
3516 rc = -EIO; /* bad smb */
3518 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3519 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3520 rc = cifs_copy_posix_acl(acl_inf,
3521 (char *)&pSMBr->hdr.Protocol+data_offset,
3522 buflen, acl_type, count);
3525 cifs_buf_release(pSMB);
3532 CIFSSMBSetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
3533 const unsigned char *fileName,
3534 const char *local_acl, const int buflen,
3536 const struct nls_table *nls_codepage, int remap)
3538 struct smb_com_transaction2_spi_req *pSMB = NULL;
3539 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
3543 int bytes_returned = 0;
3544 __u16 params, byte_count, data_count, param_offset, offset;
3546 cifs_dbg(FYI, "In SetPosixACL (Unix) for path %s\n", fileName);
3548 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3552 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3554 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
3555 PATH_MAX, nls_codepage, remap);
3556 name_len++; /* trailing null */
3559 name_len = copy_path_name(pSMB->FileName, fileName);
3561 params = 6 + name_len;
3562 pSMB->MaxParameterCount = cpu_to_le16(2);
3563 /* BB find max SMB size from sess */
3564 pSMB->MaxDataCount = cpu_to_le16(1000);
3565 pSMB->MaxSetupCount = 0;
3569 pSMB->Reserved2 = 0;
3570 param_offset = offsetof(struct smb_com_transaction2_spi_req,
3571 InformationLevel) - 4;
3572 offset = param_offset + params;
3573 parm_data = ((char *) &pSMB->hdr.Protocol) + offset;
3574 pSMB->ParameterOffset = cpu_to_le16(param_offset);
3576 /* convert to on the wire format for POSIX ACL */
3577 data_count = ACL_to_cifs_posix(parm_data, local_acl, buflen, acl_type);
3579 if (data_count == 0) {
3581 goto setACLerrorExit;
3583 pSMB->DataOffset = cpu_to_le16(offset);
3584 pSMB->SetupCount = 1;
3585 pSMB->Reserved3 = 0;
3586 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
3587 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_ACL);
3588 byte_count = 3 /* pad */ + params + data_count;
3589 pSMB->DataCount = cpu_to_le16(data_count);
3590 pSMB->TotalDataCount = pSMB->DataCount;
3591 pSMB->ParameterCount = cpu_to_le16(params);
3592 pSMB->TotalParameterCount = pSMB->ParameterCount;
3593 pSMB->Reserved4 = 0;
3594 inc_rfc1001_len(pSMB, byte_count);
3595 pSMB->ByteCount = cpu_to_le16(byte_count);
3596 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3597 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3599 cifs_dbg(FYI, "Set POSIX ACL returned %d\n", rc);
3602 cifs_buf_release(pSMB);
3608 /* BB fix tabs in this function FIXME BB */
3610 CIFSGetExtAttr(const unsigned int xid, struct cifs_tcon *tcon,
3611 const int netfid, __u64 *pExtAttrBits, __u64 *pMask)
3614 struct smb_t2_qfi_req *pSMB = NULL;
3615 struct smb_t2_qfi_rsp *pSMBr = NULL;
3617 __u16 params, byte_count;
3619 cifs_dbg(FYI, "In GetExtAttr\n");
3624 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3629 params = 2 /* level */ + 2 /* fid */;
3630 pSMB->t2.TotalDataCount = 0;
3631 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
3632 /* BB find exact max data count below from sess structure BB */
3633 pSMB->t2.MaxDataCount = cpu_to_le16(4000);
3634 pSMB->t2.MaxSetupCount = 0;
3635 pSMB->t2.Reserved = 0;
3637 pSMB->t2.Timeout = 0;
3638 pSMB->t2.Reserved2 = 0;
3639 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
3641 pSMB->t2.DataCount = 0;
3642 pSMB->t2.DataOffset = 0;
3643 pSMB->t2.SetupCount = 1;
3644 pSMB->t2.Reserved3 = 0;
3645 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
3646 byte_count = params + 1 /* pad */ ;
3647 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
3648 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
3649 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_ATTR_FLAGS);
3652 inc_rfc1001_len(pSMB, byte_count);
3653 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
3655 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3656 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3658 cifs_dbg(FYI, "error %d in GetExtAttr\n", rc);
3660 /* decode response */
3661 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
3662 /* BB also check enough total bytes returned */
3663 if (rc || get_bcc(&pSMBr->hdr) < 2)
3664 /* If rc should we check for EOPNOSUPP and
3665 disable the srvino flag? or in caller? */
3666 rc = -EIO; /* bad smb */
3668 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3669 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3670 struct file_chattr_info *pfinfo;
3671 /* BB Do we need a cast or hash here ? */
3673 cifs_dbg(FYI, "Invalid size ret in GetExtAttr\n");
3677 pfinfo = (struct file_chattr_info *)
3678 (data_offset + (char *) &pSMBr->hdr.Protocol);
3679 *pExtAttrBits = le64_to_cpu(pfinfo->mode);
3680 *pMask = le64_to_cpu(pfinfo->mask);
3684 cifs_buf_release(pSMB);
3686 goto GetExtAttrRetry;
3690 #endif /* CONFIG_POSIX */
3693 * Initialize NT TRANSACT SMB into small smb request buffer. This assumes that
3694 * all NT TRANSACTS that we init here have total parm and data under about 400
3695 * bytes (to fit in small cifs buffer size), which is the case so far, it
3696 * easily fits. NB: Setup words themselves and ByteCount MaxSetupCount (size of
3697 * returned setup area) and MaxParameterCount (returned parms size) must be set
3701 smb_init_nttransact(const __u16 sub_command, const int setup_count,
3702 const int parm_len, struct cifs_tcon *tcon,
3707 struct smb_com_ntransact_req *pSMB;
3709 rc = small_smb_init(SMB_COM_NT_TRANSACT, 19 + setup_count, tcon,
3713 *ret_buf = (void *)pSMB;
3715 pSMB->TotalParameterCount = cpu_to_le32(parm_len);
3716 pSMB->TotalDataCount = 0;
3717 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
3718 pSMB->ParameterCount = pSMB->TotalParameterCount;
3719 pSMB->DataCount = pSMB->TotalDataCount;
3720 temp_offset = offsetof(struct smb_com_ntransact_req, Parms) +
3721 (setup_count * 2) - 4 /* for rfc1001 length itself */;
3722 pSMB->ParameterOffset = cpu_to_le32(temp_offset);
3723 pSMB->DataOffset = cpu_to_le32(temp_offset + parm_len);
3724 pSMB->SetupCount = setup_count; /* no need to le convert byte fields */
3725 pSMB->SubCommand = cpu_to_le16(sub_command);
3730 validate_ntransact(char *buf, char **ppparm, char **ppdata,
3731 __u32 *pparmlen, __u32 *pdatalen)
3734 __u32 data_count, data_offset, parm_count, parm_offset;
3735 struct smb_com_ntransact_rsp *pSMBr;
3744 pSMBr = (struct smb_com_ntransact_rsp *)buf;
3746 bcc = get_bcc(&pSMBr->hdr);
3747 end_of_smb = 2 /* sizeof byte count */ + bcc +
3748 (char *)&pSMBr->ByteCount;
3750 data_offset = le32_to_cpu(pSMBr->DataOffset);
3751 data_count = le32_to_cpu(pSMBr->DataCount);
3752 parm_offset = le32_to_cpu(pSMBr->ParameterOffset);
3753 parm_count = le32_to_cpu(pSMBr->ParameterCount);
3755 *ppparm = (char *)&pSMBr->hdr.Protocol + parm_offset;
3756 *ppdata = (char *)&pSMBr->hdr.Protocol + data_offset;
3758 /* should we also check that parm and data areas do not overlap? */
3759 if (*ppparm > end_of_smb) {
3760 cifs_dbg(FYI, "parms start after end of smb\n");
3762 } else if (parm_count + *ppparm > end_of_smb) {
3763 cifs_dbg(FYI, "parm end after end of smb\n");
3765 } else if (*ppdata > end_of_smb) {
3766 cifs_dbg(FYI, "data starts after end of smb\n");
3768 } else if (data_count + *ppdata > end_of_smb) {
3769 cifs_dbg(FYI, "data %p + count %d (%p) past smb end %p start %p\n",
3770 *ppdata, data_count, (data_count + *ppdata),
3773 } else if (parm_count + data_count > bcc) {
3774 cifs_dbg(FYI, "parm count and data count larger than SMB\n");
3777 *pdatalen = data_count;
3778 *pparmlen = parm_count;
3782 /* Get Security Descriptor (by handle) from remote server for a file or dir */
3784 CIFSSMBGetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
3785 struct cifs_ntsd **acl_inf, __u32 *pbuflen)
3789 QUERY_SEC_DESC_REQ *pSMB;
3791 struct kvec rsp_iov;
3793 cifs_dbg(FYI, "GetCifsACL\n");
3798 rc = smb_init_nttransact(NT_TRANSACT_QUERY_SECURITY_DESC, 0,
3799 8 /* parm len */, tcon, (void **) &pSMB);
3803 pSMB->MaxParameterCount = cpu_to_le32(4);
3804 /* BB TEST with big acls that might need to be e.g. larger than 16K */
3805 pSMB->MaxSetupCount = 0;
3806 pSMB->Fid = fid; /* file handle always le */
3807 pSMB->AclFlags = cpu_to_le32(CIFS_ACL_OWNER | CIFS_ACL_GROUP |
3809 pSMB->ByteCount = cpu_to_le16(11); /* 3 bytes pad + 8 bytes parm */
3810 inc_rfc1001_len(pSMB, 11);
3811 iov[0].iov_base = (char *)pSMB;
3812 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
3814 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovec */, &buf_type,
3816 cifs_small_buf_release(pSMB);
3817 cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
3819 cifs_dbg(FYI, "Send error in QuerySecDesc = %d\n", rc);
3820 } else { /* decode response */
3824 struct smb_com_ntransact_rsp *pSMBr;
3827 /* validate_nttransact */
3828 rc = validate_ntransact(rsp_iov.iov_base, (char **)&parm,
3829 &pdata, &parm_len, pbuflen);
3832 pSMBr = (struct smb_com_ntransact_rsp *)rsp_iov.iov_base;
3834 cifs_dbg(FYI, "smb %p parm %p data %p\n",
3835 pSMBr, parm, *acl_inf);
3837 if (le32_to_cpu(pSMBr->ParameterCount) != 4) {
3838 rc = -EIO; /* bad smb */
3843 /* BB check that data area is minimum length and as big as acl_len */
3845 acl_len = le32_to_cpu(*parm);
3846 if (acl_len != *pbuflen) {
3847 cifs_dbg(VFS, "acl length %d does not match %d\n",
3849 if (*pbuflen > acl_len)
3853 /* check if buffer is big enough for the acl
3854 header followed by the smallest SID */
3855 if ((*pbuflen < sizeof(struct cifs_ntsd) + 8) ||
3856 (*pbuflen >= 64 * 1024)) {
3857 cifs_dbg(VFS, "bad acl length %d\n", *pbuflen);
3861 *acl_inf = kmemdup(pdata, *pbuflen, GFP_KERNEL);
3862 if (*acl_inf == NULL) {
3869 free_rsp_buf(buf_type, rsp_iov.iov_base);
3874 CIFSSMBSetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
3875 struct cifs_ntsd *pntsd, __u32 acllen, int aclflag)
3877 __u16 byte_count, param_count, data_count, param_offset, data_offset;
3879 int bytes_returned = 0;
3880 SET_SEC_DESC_REQ *pSMB = NULL;
3884 rc = smb_init(SMB_COM_NT_TRANSACT, 19, tcon, (void **) &pSMB, &pSMBr);
3888 pSMB->MaxSetupCount = 0;
3892 param_offset = offsetof(struct smb_com_transaction_ssec_req, Fid) - 4;
3893 data_count = acllen;
3894 data_offset = param_offset + param_count;
3895 byte_count = 3 /* pad */ + param_count;
3897 pSMB->DataCount = cpu_to_le32(data_count);
3898 pSMB->TotalDataCount = pSMB->DataCount;
3899 pSMB->MaxParameterCount = cpu_to_le32(4);
3900 pSMB->MaxDataCount = cpu_to_le32(16384);
3901 pSMB->ParameterCount = cpu_to_le32(param_count);
3902 pSMB->ParameterOffset = cpu_to_le32(param_offset);
3903 pSMB->TotalParameterCount = pSMB->ParameterCount;
3904 pSMB->DataOffset = cpu_to_le32(data_offset);
3905 pSMB->SetupCount = 0;
3906 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_SET_SECURITY_DESC);
3907 pSMB->ByteCount = cpu_to_le16(byte_count+data_count);
3909 pSMB->Fid = fid; /* file handle always le */
3910 pSMB->Reserved2 = 0;
3911 pSMB->AclFlags = cpu_to_le32(aclflag);
3913 if (pntsd && acllen) {
3914 memcpy((char *)pSMBr + offsetof(struct smb_hdr, Protocol) +
3915 data_offset, pntsd, acllen);
3916 inc_rfc1001_len(pSMB, byte_count + data_count);
3918 inc_rfc1001_len(pSMB, byte_count);
3920 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3921 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3923 cifs_dbg(FYI, "SetCIFSACL bytes_returned: %d, rc: %d\n",
3924 bytes_returned, rc);
3926 cifs_dbg(FYI, "Set CIFS ACL returned %d\n", rc);
3927 cifs_buf_release(pSMB);
3930 goto setCifsAclRetry;
3936 /* Legacy Query Path Information call for lookup to old servers such
3939 SMBQueryInformation(const unsigned int xid, struct cifs_tcon *tcon,
3940 const char *search_name, FILE_ALL_INFO *data,
3941 const struct nls_table *nls_codepage, int remap)
3943 QUERY_INFORMATION_REQ *pSMB;
3944 QUERY_INFORMATION_RSP *pSMBr;
3949 cifs_dbg(FYI, "In SMBQPath path %s\n", search_name);
3951 rc = smb_init(SMB_COM_QUERY_INFORMATION, 0, tcon, (void **) &pSMB,
3956 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3958 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3959 search_name, PATH_MAX, nls_codepage,
3961 name_len++; /* trailing null */
3964 name_len = copy_path_name(pSMB->FileName, search_name);
3966 pSMB->BufferFormat = 0x04;
3967 name_len++; /* account for buffer type byte */
3968 inc_rfc1001_len(pSMB, (__u16)name_len);
3969 pSMB->ByteCount = cpu_to_le16(name_len);
3971 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3972 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3974 cifs_dbg(FYI, "Send error in QueryInfo = %d\n", rc);
3976 struct timespec64 ts;
3977 __u32 time = le32_to_cpu(pSMBr->last_write_time);
3979 /* decode response */
3980 /* BB FIXME - add time zone adjustment BB */
3981 memset(data, 0, sizeof(FILE_ALL_INFO));
3984 /* decode time fields */
3985 data->ChangeTime = cpu_to_le64(cifs_UnixTimeToNT(ts));
3986 data->LastWriteTime = data->ChangeTime;
3987 data->LastAccessTime = 0;
3988 data->AllocationSize =
3989 cpu_to_le64(le32_to_cpu(pSMBr->size));
3990 data->EndOfFile = data->AllocationSize;
3992 cpu_to_le32(le16_to_cpu(pSMBr->attr));
3994 rc = -EIO; /* bad buffer passed in */
3996 cifs_buf_release(pSMB);
4005 CIFSSMBQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
4006 u16 netfid, FILE_ALL_INFO *pFindData)
4008 struct smb_t2_qfi_req *pSMB = NULL;
4009 struct smb_t2_qfi_rsp *pSMBr = NULL;
4012 __u16 params, byte_count;
4015 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4020 params = 2 /* level */ + 2 /* fid */;
4021 pSMB->t2.TotalDataCount = 0;
4022 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4023 /* BB find exact max data count below from sess structure BB */
4024 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4025 pSMB->t2.MaxSetupCount = 0;
4026 pSMB->t2.Reserved = 0;
4028 pSMB->t2.Timeout = 0;
4029 pSMB->t2.Reserved2 = 0;
4030 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4032 pSMB->t2.DataCount = 0;
4033 pSMB->t2.DataOffset = 0;
4034 pSMB->t2.SetupCount = 1;
4035 pSMB->t2.Reserved3 = 0;
4036 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4037 byte_count = params + 1 /* pad */ ;
4038 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4039 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4040 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
4043 inc_rfc1001_len(pSMB, byte_count);
4044 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
4046 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4047 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4049 cifs_dbg(FYI, "Send error in QFileInfo = %d\n", rc);
4050 } else { /* decode response */
4051 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4053 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4055 else if (get_bcc(&pSMBr->hdr) < 40)
4056 rc = -EIO; /* bad smb */
4057 else if (pFindData) {
4058 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4059 memcpy((char *) pFindData,
4060 (char *) &pSMBr->hdr.Protocol +
4061 data_offset, sizeof(FILE_ALL_INFO));
4065 cifs_buf_release(pSMB);
4067 goto QFileInfoRetry;
4073 CIFSSMBQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
4074 const char *search_name, FILE_ALL_INFO *data,
4075 int legacy /* old style infolevel */,
4076 const struct nls_table *nls_codepage, int remap)
4078 /* level 263 SMB_QUERY_FILE_ALL_INFO */
4079 TRANSACTION2_QPI_REQ *pSMB = NULL;
4080 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4084 __u16 params, byte_count;
4086 /* cifs_dbg(FYI, "In QPathInfo path %s\n", search_name); */
4088 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4093 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4095 cifsConvertToUTF16((__le16 *) pSMB->FileName, search_name,
4096 PATH_MAX, nls_codepage, remap);
4097 name_len++; /* trailing null */
4100 name_len = copy_path_name(pSMB->FileName, search_name);
4103 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
4104 pSMB->TotalDataCount = 0;
4105 pSMB->MaxParameterCount = cpu_to_le16(2);
4106 /* BB find exact max SMB PDU from sess structure BB */
4107 pSMB->MaxDataCount = cpu_to_le16(4000);
4108 pSMB->MaxSetupCount = 0;
4112 pSMB->Reserved2 = 0;
4113 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4114 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
4115 pSMB->DataCount = 0;
4116 pSMB->DataOffset = 0;
4117 pSMB->SetupCount = 1;
4118 pSMB->Reserved3 = 0;
4119 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4120 byte_count = params + 1 /* pad */ ;
4121 pSMB->TotalParameterCount = cpu_to_le16(params);
4122 pSMB->ParameterCount = pSMB->TotalParameterCount;
4124 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_STANDARD);
4126 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
4127 pSMB->Reserved4 = 0;
4128 inc_rfc1001_len(pSMB, byte_count);
4129 pSMB->ByteCount = cpu_to_le16(byte_count);
4131 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4132 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4134 cifs_dbg(FYI, "Send error in QPathInfo = %d\n", rc);
4135 } else { /* decode response */
4136 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4138 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4140 else if (!legacy && get_bcc(&pSMBr->hdr) < 40)
4141 rc = -EIO; /* bad smb */
4142 else if (legacy && get_bcc(&pSMBr->hdr) < 24)
4143 rc = -EIO; /* 24 or 26 expected but we do not read
4147 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4150 * On legacy responses we do not read the last field,
4151 * EAsize, fortunately since it varies by subdialect and
4152 * also note it differs on Set vs Get, ie two bytes or 4
4153 * bytes depending but we don't care here.
4156 size = sizeof(FILE_INFO_STANDARD);
4158 size = sizeof(FILE_ALL_INFO);
4159 memcpy((char *) data, (char *) &pSMBr->hdr.Protocol +
4164 cifs_buf_release(pSMB);
4166 goto QPathInfoRetry;
4172 CIFSSMBUnixQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
4173 u16 netfid, FILE_UNIX_BASIC_INFO *pFindData)
4175 struct smb_t2_qfi_req *pSMB = NULL;
4176 struct smb_t2_qfi_rsp *pSMBr = NULL;
4179 __u16 params, byte_count;
4182 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4187 params = 2 /* level */ + 2 /* fid */;
4188 pSMB->t2.TotalDataCount = 0;
4189 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4190 /* BB find exact max data count below from sess structure BB */
4191 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4192 pSMB->t2.MaxSetupCount = 0;
4193 pSMB->t2.Reserved = 0;
4195 pSMB->t2.Timeout = 0;
4196 pSMB->t2.Reserved2 = 0;
4197 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4199 pSMB->t2.DataCount = 0;
4200 pSMB->t2.DataOffset = 0;
4201 pSMB->t2.SetupCount = 1;
4202 pSMB->t2.Reserved3 = 0;
4203 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4204 byte_count = params + 1 /* pad */ ;
4205 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4206 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4207 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4210 inc_rfc1001_len(pSMB, byte_count);
4211 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
4213 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4214 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4216 cifs_dbg(FYI, "Send error in UnixQFileInfo = %d\n", rc);
4217 } else { /* decode response */
4218 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4220 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
4221 cifs_dbg(VFS, "Malformed FILE_UNIX_BASIC_INFO response. Unix Extensions can be disabled on mount by specifying the nosfu mount option.\n");
4222 rc = -EIO; /* bad smb */
4224 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4225 memcpy((char *) pFindData,
4226 (char *) &pSMBr->hdr.Protocol +
4228 sizeof(FILE_UNIX_BASIC_INFO));
4232 cifs_buf_release(pSMB);
4234 goto UnixQFileInfoRetry;
4240 CIFSSMBUnixQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
4241 const unsigned char *searchName,
4242 FILE_UNIX_BASIC_INFO *pFindData,
4243 const struct nls_table *nls_codepage, int remap)
4245 /* SMB_QUERY_FILE_UNIX_BASIC */
4246 TRANSACTION2_QPI_REQ *pSMB = NULL;
4247 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4249 int bytes_returned = 0;
4251 __u16 params, byte_count;
4253 cifs_dbg(FYI, "In QPathInfo (Unix) the path %s\n", searchName);
4255 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4260 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4262 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4263 PATH_MAX, nls_codepage, remap);
4264 name_len++; /* trailing null */
4267 name_len = copy_path_name(pSMB->FileName, searchName);
4270 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
4271 pSMB->TotalDataCount = 0;
4272 pSMB->MaxParameterCount = cpu_to_le16(2);
4273 /* BB find exact max SMB PDU from sess structure BB */
4274 pSMB->MaxDataCount = cpu_to_le16(4000);
4275 pSMB->MaxSetupCount = 0;
4279 pSMB->Reserved2 = 0;
4280 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4281 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
4282 pSMB->DataCount = 0;
4283 pSMB->DataOffset = 0;
4284 pSMB->SetupCount = 1;
4285 pSMB->Reserved3 = 0;
4286 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4287 byte_count = params + 1 /* pad */ ;
4288 pSMB->TotalParameterCount = cpu_to_le16(params);
4289 pSMB->ParameterCount = pSMB->TotalParameterCount;
4290 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4291 pSMB->Reserved4 = 0;
4292 inc_rfc1001_len(pSMB, byte_count);
4293 pSMB->ByteCount = cpu_to_le16(byte_count);
4295 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4296 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4298 cifs_dbg(FYI, "Send error in UnixQPathInfo = %d\n", rc);
4299 } else { /* decode response */
4300 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4302 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
4303 cifs_dbg(VFS, "Malformed FILE_UNIX_BASIC_INFO response. Unix Extensions can be disabled on mount by specifying the nosfu mount option.\n");
4304 rc = -EIO; /* bad smb */
4306 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4307 memcpy((char *) pFindData,
4308 (char *) &pSMBr->hdr.Protocol +
4310 sizeof(FILE_UNIX_BASIC_INFO));
4313 cifs_buf_release(pSMB);
4315 goto UnixQPathInfoRetry;
4320 /* xid, tcon, searchName and codepage are input parms, rest are returned */
4322 CIFSFindFirst(const unsigned int xid, struct cifs_tcon *tcon,
4323 const char *searchName, struct cifs_sb_info *cifs_sb,
4324 __u16 *pnetfid, __u16 search_flags,
4325 struct cifs_search_info *psrch_inf, bool msearch)
4327 /* level 257 SMB_ */
4328 TRANSACTION2_FFIRST_REQ *pSMB = NULL;
4329 TRANSACTION2_FFIRST_RSP *pSMBr = NULL;
4330 T2_FFIRST_RSP_PARMS *parms;
4332 int bytes_returned = 0;
4333 int name_len, remap;
4334 __u16 params, byte_count;
4335 struct nls_table *nls_codepage;
4337 cifs_dbg(FYI, "In FindFirst for %s\n", searchName);
4340 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4345 nls_codepage = cifs_sb->local_nls;
4346 remap = cifs_remap(cifs_sb);
4348 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4350 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4351 PATH_MAX, nls_codepage, remap);
4352 /* We can not add the asterik earlier in case
4353 it got remapped to 0xF03A as if it were part of the
4354 directory name instead of a wildcard */
4357 pSMB->FileName[name_len] = CIFS_DIR_SEP(cifs_sb);
4358 pSMB->FileName[name_len+1] = 0;
4359 pSMB->FileName[name_len+2] = '*';
4360 pSMB->FileName[name_len+3] = 0;
4361 name_len += 4; /* now the trailing null */
4362 /* null terminate just in case */
4363 pSMB->FileName[name_len] = 0;
4364 pSMB->FileName[name_len+1] = 0;
4368 name_len = copy_path_name(pSMB->FileName, searchName);
4370 if (WARN_ON_ONCE(name_len > PATH_MAX-2))
4371 name_len = PATH_MAX-2;
4372 /* overwrite nul byte */
4373 pSMB->FileName[name_len-1] = CIFS_DIR_SEP(cifs_sb);
4374 pSMB->FileName[name_len] = '*';
4375 pSMB->FileName[name_len+1] = 0;
4380 params = 12 + name_len /* includes null */ ;
4381 pSMB->TotalDataCount = 0; /* no EAs */
4382 pSMB->MaxParameterCount = cpu_to_le16(10);
4383 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
4384 pSMB->MaxSetupCount = 0;
4388 pSMB->Reserved2 = 0;
4389 byte_count = params + 1 /* pad */ ;
4390 pSMB->TotalParameterCount = cpu_to_le16(params);
4391 pSMB->ParameterCount = pSMB->TotalParameterCount;
4392 pSMB->ParameterOffset = cpu_to_le16(
4393 offsetof(struct smb_com_transaction2_ffirst_req, SearchAttributes)
4395 pSMB->DataCount = 0;
4396 pSMB->DataOffset = 0;
4397 pSMB->SetupCount = 1; /* one byte, no need to make endian neutral */
4398 pSMB->Reserved3 = 0;
4399 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_FIRST);
4400 pSMB->SearchAttributes =
4401 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
4403 pSMB->SearchCount = cpu_to_le16(CIFSMaxBufSize/sizeof(FILE_UNIX_INFO));
4404 pSMB->SearchFlags = cpu_to_le16(search_flags);
4405 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4407 /* BB what should we set StorageType to? Does it matter? BB */
4408 pSMB->SearchStorageType = 0;
4409 inc_rfc1001_len(pSMB, byte_count);
4410 pSMB->ByteCount = cpu_to_le16(byte_count);
4412 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4413 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4414 cifs_stats_inc(&tcon->stats.cifs_stats.num_ffirst);
4416 if (rc) {/* BB add logic to retry regular search if Unix search
4417 rejected unexpectedly by server */
4418 /* BB Add code to handle unsupported level rc */
4419 cifs_dbg(FYI, "Error in FindFirst = %d\n", rc);
4421 cifs_buf_release(pSMB);
4423 /* BB eventually could optimize out free and realloc of buf */
4426 goto findFirstRetry;
4427 } else { /* decode response */
4428 /* BB remember to free buffer if error BB */
4429 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4433 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
4434 psrch_inf->unicode = true;
4436 psrch_inf->unicode = false;
4438 psrch_inf->ntwrk_buf_start = (char *)pSMBr;
4439 psrch_inf->smallBuf = false;
4440 psrch_inf->srch_entries_start =
4441 (char *) &pSMBr->hdr.Protocol +
4442 le16_to_cpu(pSMBr->t2.DataOffset);
4443 parms = (T2_FFIRST_RSP_PARMS *)((char *) &pSMBr->hdr.Protocol +
4444 le16_to_cpu(pSMBr->t2.ParameterOffset));
4446 if (parms->EndofSearch)
4447 psrch_inf->endOfSearch = true;
4449 psrch_inf->endOfSearch = false;
4451 psrch_inf->entries_in_buffer =
4452 le16_to_cpu(parms->SearchCount);
4453 psrch_inf->index_of_last_entry = 2 /* skip . and .. */ +
4454 psrch_inf->entries_in_buffer;
4455 lnoff = le16_to_cpu(parms->LastNameOffset);
4456 if (CIFSMaxBufSize < lnoff) {
4457 cifs_dbg(VFS, "ignoring corrupt resume name\n");
4458 psrch_inf->last_entry = NULL;
4462 psrch_inf->last_entry = psrch_inf->srch_entries_start +
4466 *pnetfid = parms->SearchHandle;
4468 cifs_buf_release(pSMB);
4475 int CIFSFindNext(const unsigned int xid, struct cifs_tcon *tcon,
4476 __u16 searchHandle, __u16 search_flags,
4477 struct cifs_search_info *psrch_inf)
4479 TRANSACTION2_FNEXT_REQ *pSMB = NULL;
4480 TRANSACTION2_FNEXT_RSP *pSMBr = NULL;
4481 T2_FNEXT_RSP_PARMS *parms;
4482 char *response_data;
4485 unsigned int name_len;
4486 __u16 params, byte_count;
4488 cifs_dbg(FYI, "In FindNext\n");
4490 if (psrch_inf->endOfSearch)
4493 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4498 params = 14; /* includes 2 bytes of null string, converted to LE below*/
4500 pSMB->TotalDataCount = 0; /* no EAs */
4501 pSMB->MaxParameterCount = cpu_to_le16(8);
4502 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
4503 pSMB->MaxSetupCount = 0;
4507 pSMB->Reserved2 = 0;
4508 pSMB->ParameterOffset = cpu_to_le16(
4509 offsetof(struct smb_com_transaction2_fnext_req,SearchHandle) - 4);
4510 pSMB->DataCount = 0;
4511 pSMB->DataOffset = 0;
4512 pSMB->SetupCount = 1;
4513 pSMB->Reserved3 = 0;
4514 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_NEXT);
4515 pSMB->SearchHandle = searchHandle; /* always kept as le */
4517 cpu_to_le16(CIFSMaxBufSize / sizeof(FILE_UNIX_INFO));
4518 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4519 pSMB->ResumeKey = psrch_inf->resume_key;
4520 pSMB->SearchFlags = cpu_to_le16(search_flags);
4522 name_len = psrch_inf->resume_name_len;
4524 if (name_len < PATH_MAX) {
4525 memcpy(pSMB->ResumeFileName, psrch_inf->presume_name, name_len);
4526 byte_count += name_len;
4527 /* 14 byte parm len above enough for 2 byte null terminator */
4528 pSMB->ResumeFileName[name_len] = 0;
4529 pSMB->ResumeFileName[name_len+1] = 0;
4532 goto FNext2_err_exit;
4534 byte_count = params + 1 /* pad */ ;
4535 pSMB->TotalParameterCount = cpu_to_le16(params);
4536 pSMB->ParameterCount = pSMB->TotalParameterCount;
4537 inc_rfc1001_len(pSMB, byte_count);
4538 pSMB->ByteCount = cpu_to_le16(byte_count);
4540 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4541 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4542 cifs_stats_inc(&tcon->stats.cifs_stats.num_fnext);
4545 psrch_inf->endOfSearch = true;
4546 cifs_buf_release(pSMB);
4547 rc = 0; /* search probably was closed at end of search*/
4549 cifs_dbg(FYI, "FindNext returned = %d\n", rc);
4550 } else { /* decode response */
4551 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4556 /* BB fixme add lock for file (srch_info) struct here */
4557 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
4558 psrch_inf->unicode = true;
4560 psrch_inf->unicode = false;
4561 response_data = (char *) &pSMBr->hdr.Protocol +
4562 le16_to_cpu(pSMBr->t2.ParameterOffset);
4563 parms = (T2_FNEXT_RSP_PARMS *)response_data;
4564 response_data = (char *)&pSMBr->hdr.Protocol +
4565 le16_to_cpu(pSMBr->t2.DataOffset);
4566 if (psrch_inf->smallBuf)
4567 cifs_small_buf_release(
4568 psrch_inf->ntwrk_buf_start);
4570 cifs_buf_release(psrch_inf->ntwrk_buf_start);
4571 psrch_inf->srch_entries_start = response_data;
4572 psrch_inf->ntwrk_buf_start = (char *)pSMB;
4573 psrch_inf->smallBuf = false;
4574 if (parms->EndofSearch)
4575 psrch_inf->endOfSearch = true;
4577 psrch_inf->endOfSearch = false;
4578 psrch_inf->entries_in_buffer =
4579 le16_to_cpu(parms->SearchCount);
4580 psrch_inf->index_of_last_entry +=
4581 psrch_inf->entries_in_buffer;
4582 lnoff = le16_to_cpu(parms->LastNameOffset);
4583 if (CIFSMaxBufSize < lnoff) {
4584 cifs_dbg(VFS, "ignoring corrupt resume name\n");
4585 psrch_inf->last_entry = NULL;
4588 psrch_inf->last_entry =
4589 psrch_inf->srch_entries_start + lnoff;
4591 /* cifs_dbg(FYI, "fnxt2 entries in buf %d index_of_last %d\n",
4592 psrch_inf->entries_in_buffer, psrch_inf->index_of_last_entry); */
4594 /* BB fixme add unlock here */
4599 /* BB On error, should we leave previous search buf (and count and
4600 last entry fields) intact or free the previous one? */
4602 /* Note: On -EAGAIN error only caller can retry on handle based calls
4603 since file handle passed in no longer valid */
4606 cifs_buf_release(pSMB);
4611 CIFSFindClose(const unsigned int xid, struct cifs_tcon *tcon,
4612 const __u16 searchHandle)
4615 FINDCLOSE_REQ *pSMB = NULL;
4617 cifs_dbg(FYI, "In CIFSSMBFindClose\n");
4618 rc = small_smb_init(SMB_COM_FIND_CLOSE2, 1, tcon, (void **)&pSMB);
4620 /* no sense returning error if session restarted
4621 as file handle has been closed */
4627 pSMB->FileID = searchHandle;
4628 pSMB->ByteCount = 0;
4629 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
4630 cifs_small_buf_release(pSMB);
4632 cifs_dbg(VFS, "Send error in FindClose = %d\n", rc);
4634 cifs_stats_inc(&tcon->stats.cifs_stats.num_fclose);
4636 /* Since session is dead, search handle closed on server already */
4644 CIFSGetSrvInodeNumber(const unsigned int xid, struct cifs_tcon *tcon,
4645 const char *search_name, __u64 *inode_number,
4646 const struct nls_table *nls_codepage, int remap)
4649 TRANSACTION2_QPI_REQ *pSMB = NULL;
4650 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4651 int name_len, bytes_returned;
4652 __u16 params, byte_count;
4654 cifs_dbg(FYI, "In GetSrvInodeNum for %s\n", search_name);
4658 GetInodeNumberRetry:
4659 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4664 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4666 cifsConvertToUTF16((__le16 *) pSMB->FileName,
4667 search_name, PATH_MAX, nls_codepage,
4669 name_len++; /* trailing null */
4672 name_len = copy_path_name(pSMB->FileName, search_name);
4675 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
4676 pSMB->TotalDataCount = 0;
4677 pSMB->MaxParameterCount = cpu_to_le16(2);
4678 /* BB find exact max data count below from sess structure BB */
4679 pSMB->MaxDataCount = cpu_to_le16(4000);
4680 pSMB->MaxSetupCount = 0;
4684 pSMB->Reserved2 = 0;
4685 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4686 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
4687 pSMB->DataCount = 0;
4688 pSMB->DataOffset = 0;
4689 pSMB->SetupCount = 1;
4690 pSMB->Reserved3 = 0;
4691 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4692 byte_count = params + 1 /* pad */ ;
4693 pSMB->TotalParameterCount = cpu_to_le16(params);
4694 pSMB->ParameterCount = pSMB->TotalParameterCount;
4695 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_INTERNAL_INFO);
4696 pSMB->Reserved4 = 0;
4697 inc_rfc1001_len(pSMB, byte_count);
4698 pSMB->ByteCount = cpu_to_le16(byte_count);
4700 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4701 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4703 cifs_dbg(FYI, "error %d in QueryInternalInfo\n", rc);
4705 /* decode response */
4706 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4707 /* BB also check enough total bytes returned */
4708 if (rc || get_bcc(&pSMBr->hdr) < 2)
4709 /* If rc should we check for EOPNOSUPP and
4710 disable the srvino flag? or in caller? */
4711 rc = -EIO; /* bad smb */
4713 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4714 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
4715 struct file_internal_info *pfinfo;
4716 /* BB Do we need a cast or hash here ? */
4718 cifs_dbg(FYI, "Invalid size ret in QryIntrnlInf\n");
4720 goto GetInodeNumOut;
4722 pfinfo = (struct file_internal_info *)
4723 (data_offset + (char *) &pSMBr->hdr.Protocol);
4724 *inode_number = le64_to_cpu(pfinfo->UniqueId);
4728 cifs_buf_release(pSMB);
4730 goto GetInodeNumberRetry;
4735 CIFSGetDFSRefer(const unsigned int xid, struct cifs_ses *ses,
4736 const char *search_name, struct dfs_info3_param **target_nodes,
4737 unsigned int *num_of_nodes,
4738 const struct nls_table *nls_codepage, int remap)
4740 /* TRANS2_GET_DFS_REFERRAL */
4741 TRANSACTION2_GET_DFS_REFER_REQ *pSMB = NULL;
4742 TRANSACTION2_GET_DFS_REFER_RSP *pSMBr = NULL;
4746 __u16 params, byte_count;
4748 *target_nodes = NULL;
4750 cifs_dbg(FYI, "In GetDFSRefer the path %s\n", search_name);
4751 if (ses == NULL || ses->tcon_ipc == NULL)
4755 rc = smb_init(SMB_COM_TRANSACTION2, 15, ses->tcon_ipc, (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->tcon_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 = copy_path_name(pSMB->RequestFileName, search_name);
4782 if (ses->server->sign)
4783 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
4785 pSMB->hdr.Uid = ses->Suid;
4787 params = 2 /* level */ + name_len /*includes null */ ;
4788 pSMB->TotalDataCount = 0;
4789 pSMB->DataCount = 0;
4790 pSMB->DataOffset = 0;
4791 pSMB->MaxParameterCount = 0;
4792 /* BB find exact max SMB PDU from sess structure BB */
4793 pSMB->MaxDataCount = cpu_to_le16(4000);
4794 pSMB->MaxSetupCount = 0;
4798 pSMB->Reserved2 = 0;
4799 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4800 struct smb_com_transaction2_get_dfs_refer_req, MaxReferralLevel) - 4);
4801 pSMB->SetupCount = 1;
4802 pSMB->Reserved3 = 0;
4803 pSMB->SubCommand = cpu_to_le16(TRANS2_GET_DFS_REFERRAL);
4804 byte_count = params + 3 /* pad */ ;
4805 pSMB->ParameterCount = cpu_to_le16(params);
4806 pSMB->TotalParameterCount = pSMB->ParameterCount;
4807 pSMB->MaxReferralLevel = cpu_to_le16(3);
4808 inc_rfc1001_len(pSMB, byte_count);
4809 pSMB->ByteCount = cpu_to_le16(byte_count);
4811 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
4812 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4814 cifs_dbg(FYI, "Send error in GetDFSRefer = %d\n", rc);
4817 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4819 /* BB Also check if enough total bytes returned? */
4820 if (rc || get_bcc(&pSMBr->hdr) < 17) {
4821 rc = -EIO; /* bad smb */
4825 cifs_dbg(FYI, "Decoding GetDFSRefer response BCC: %d Offset %d\n",
4826 get_bcc(&pSMBr->hdr), le16_to_cpu(pSMBr->t2.DataOffset));
4828 /* parse returned result into more usable form */
4829 rc = parse_dfs_referrals(&pSMBr->dfs_data,
4830 le16_to_cpu(pSMBr->t2.DataCount),
4831 num_of_nodes, target_nodes, nls_codepage,
4833 (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE) != 0);
4836 cifs_buf_release(pSMB);
4844 /* Query File System Info such as free space to old servers such as Win 9x */
4846 SMBOldQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
4847 struct kstatfs *FSData)
4849 /* level 0x01 SMB_QUERY_FILE_SYSTEM_INFO */
4850 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4851 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4852 FILE_SYSTEM_ALLOC_INFO *response_data;
4854 int bytes_returned = 0;
4855 __u16 params, byte_count;
4857 cifs_dbg(FYI, "OldQFSInfo\n");
4859 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4864 params = 2; /* level */
4865 pSMB->TotalDataCount = 0;
4866 pSMB->MaxParameterCount = cpu_to_le16(2);
4867 pSMB->MaxDataCount = cpu_to_le16(1000);
4868 pSMB->MaxSetupCount = 0;
4872 pSMB->Reserved2 = 0;
4873 byte_count = params + 1 /* pad */ ;
4874 pSMB->TotalParameterCount = cpu_to_le16(params);
4875 pSMB->ParameterCount = pSMB->TotalParameterCount;
4876 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4877 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
4878 pSMB->DataCount = 0;
4879 pSMB->DataOffset = 0;
4880 pSMB->SetupCount = 1;
4881 pSMB->Reserved3 = 0;
4882 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
4883 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_ALLOCATION);
4884 inc_rfc1001_len(pSMB, byte_count);
4885 pSMB->ByteCount = cpu_to_le16(byte_count);
4887 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4888 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4890 cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
4891 } else { /* decode response */
4892 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4894 if (rc || get_bcc(&pSMBr->hdr) < 18)
4895 rc = -EIO; /* bad smb */
4897 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4898 cifs_dbg(FYI, "qfsinf resp BCC: %d Offset %d\n",
4899 get_bcc(&pSMBr->hdr), data_offset);
4901 response_data = (FILE_SYSTEM_ALLOC_INFO *)
4902 (((char *) &pSMBr->hdr.Protocol) + data_offset);
4904 le16_to_cpu(response_data->BytesPerSector) *
4905 le32_to_cpu(response_data->
4906 SectorsPerAllocationUnit);
4908 * much prefer larger but if server doesn't report
4909 * a valid size than 4K is a reasonable minimum
4911 if (FSData->f_bsize < 512)
4912 FSData->f_bsize = 4096;
4915 le32_to_cpu(response_data->TotalAllocationUnits);
4916 FSData->f_bfree = FSData->f_bavail =
4917 le32_to_cpu(response_data->FreeAllocationUnits);
4918 cifs_dbg(FYI, "Blocks: %lld Free: %lld Block size %ld\n",
4919 (unsigned long long)FSData->f_blocks,
4920 (unsigned long long)FSData->f_bfree,
4924 cifs_buf_release(pSMB);
4927 goto oldQFSInfoRetry;
4933 CIFSSMBQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
4934 struct kstatfs *FSData)
4936 /* level 0x103 SMB_QUERY_FILE_SYSTEM_INFO */
4937 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4938 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4939 FILE_SYSTEM_INFO *response_data;
4941 int bytes_returned = 0;
4942 __u16 params, byte_count;
4944 cifs_dbg(FYI, "In QFSInfo\n");
4946 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4951 params = 2; /* level */
4952 pSMB->TotalDataCount = 0;
4953 pSMB->MaxParameterCount = cpu_to_le16(2);
4954 pSMB->MaxDataCount = cpu_to_le16(1000);
4955 pSMB->MaxSetupCount = 0;
4959 pSMB->Reserved2 = 0;
4960 byte_count = params + 1 /* pad */ ;
4961 pSMB->TotalParameterCount = cpu_to_le16(params);
4962 pSMB->ParameterCount = pSMB->TotalParameterCount;
4963 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4964 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
4965 pSMB->DataCount = 0;
4966 pSMB->DataOffset = 0;
4967 pSMB->SetupCount = 1;
4968 pSMB->Reserved3 = 0;
4969 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
4970 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_SIZE_INFO);
4971 inc_rfc1001_len(pSMB, byte_count);
4972 pSMB->ByteCount = cpu_to_le16(byte_count);
4974 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4975 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4977 cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
4978 } else { /* decode response */
4979 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4981 if (rc || get_bcc(&pSMBr->hdr) < 24)
4982 rc = -EIO; /* bad smb */
4984 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4988 *) (((char *) &pSMBr->hdr.Protocol) +
4991 le32_to_cpu(response_data->BytesPerSector) *
4992 le32_to_cpu(response_data->
4993 SectorsPerAllocationUnit);
4995 * much prefer larger but if server doesn't report
4996 * a valid size than 4K is a reasonable minimum
4998 if (FSData->f_bsize < 512)
4999 FSData->f_bsize = 4096;
5002 le64_to_cpu(response_data->TotalAllocationUnits);
5003 FSData->f_bfree = FSData->f_bavail =
5004 le64_to_cpu(response_data->FreeAllocationUnits);
5005 cifs_dbg(FYI, "Blocks: %lld Free: %lld Block size %ld\n",
5006 (unsigned long long)FSData->f_blocks,
5007 (unsigned long long)FSData->f_bfree,
5011 cifs_buf_release(pSMB);
5020 CIFSSMBQFSAttributeInfo(const unsigned int xid, struct cifs_tcon *tcon)
5022 /* level 0x105 SMB_QUERY_FILE_SYSTEM_INFO */
5023 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5024 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5025 FILE_SYSTEM_ATTRIBUTE_INFO *response_data;
5027 int bytes_returned = 0;
5028 __u16 params, byte_count;
5030 cifs_dbg(FYI, "In QFSAttributeInfo\n");
5032 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5037 params = 2; /* level */
5038 pSMB->TotalDataCount = 0;
5039 pSMB->MaxParameterCount = cpu_to_le16(2);
5040 /* BB find exact max SMB PDU from sess structure BB */
5041 pSMB->MaxDataCount = cpu_to_le16(1000);
5042 pSMB->MaxSetupCount = 0;
5046 pSMB->Reserved2 = 0;
5047 byte_count = params + 1 /* pad */ ;
5048 pSMB->TotalParameterCount = cpu_to_le16(params);
5049 pSMB->ParameterCount = pSMB->TotalParameterCount;
5050 pSMB->ParameterOffset = cpu_to_le16(offsetof(
5051 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
5052 pSMB->DataCount = 0;
5053 pSMB->DataOffset = 0;
5054 pSMB->SetupCount = 1;
5055 pSMB->Reserved3 = 0;
5056 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5057 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_ATTRIBUTE_INFO);
5058 inc_rfc1001_len(pSMB, byte_count);
5059 pSMB->ByteCount = cpu_to_le16(byte_count);
5061 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5062 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5064 cifs_dbg(VFS, "Send error in QFSAttributeInfo = %d\n", rc);
5065 } else { /* decode response */
5066 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5068 if (rc || get_bcc(&pSMBr->hdr) < 13) {
5069 /* BB also check if enough bytes returned */
5070 rc = -EIO; /* bad smb */
5072 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5074 (FILE_SYSTEM_ATTRIBUTE_INFO
5075 *) (((char *) &pSMBr->hdr.Protocol) +
5077 memcpy(&tcon->fsAttrInfo, response_data,
5078 sizeof(FILE_SYSTEM_ATTRIBUTE_INFO));
5081 cifs_buf_release(pSMB);
5084 goto QFSAttributeRetry;
5090 CIFSSMBQFSDeviceInfo(const unsigned int xid, struct cifs_tcon *tcon)
5092 /* level 0x104 SMB_QUERY_FILE_SYSTEM_INFO */
5093 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5094 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5095 FILE_SYSTEM_DEVICE_INFO *response_data;
5097 int bytes_returned = 0;
5098 __u16 params, byte_count;
5100 cifs_dbg(FYI, "In QFSDeviceInfo\n");
5102 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5107 params = 2; /* level */
5108 pSMB->TotalDataCount = 0;
5109 pSMB->MaxParameterCount = cpu_to_le16(2);
5110 /* BB find exact max SMB PDU from sess structure BB */
5111 pSMB->MaxDataCount = cpu_to_le16(1000);
5112 pSMB->MaxSetupCount = 0;
5116 pSMB->Reserved2 = 0;
5117 byte_count = params + 1 /* pad */ ;
5118 pSMB->TotalParameterCount = cpu_to_le16(params);
5119 pSMB->ParameterCount = pSMB->TotalParameterCount;
5120 pSMB->ParameterOffset = cpu_to_le16(offsetof(
5121 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
5123 pSMB->DataCount = 0;
5124 pSMB->DataOffset = 0;
5125 pSMB->SetupCount = 1;
5126 pSMB->Reserved3 = 0;
5127 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5128 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_DEVICE_INFO);
5129 inc_rfc1001_len(pSMB, byte_count);
5130 pSMB->ByteCount = cpu_to_le16(byte_count);
5132 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5133 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5135 cifs_dbg(FYI, "Send error in QFSDeviceInfo = %d\n", rc);
5136 } else { /* decode response */
5137 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5139 if (rc || get_bcc(&pSMBr->hdr) <
5140 sizeof(FILE_SYSTEM_DEVICE_INFO))
5141 rc = -EIO; /* bad smb */
5143 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5145 (FILE_SYSTEM_DEVICE_INFO *)
5146 (((char *) &pSMBr->hdr.Protocol) +
5148 memcpy(&tcon->fsDevInfo, response_data,
5149 sizeof(FILE_SYSTEM_DEVICE_INFO));
5152 cifs_buf_release(pSMB);
5155 goto QFSDeviceRetry;
5161 CIFSSMBQFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon)
5163 /* level 0x200 SMB_QUERY_CIFS_UNIX_INFO */
5164 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5165 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5166 FILE_SYSTEM_UNIX_INFO *response_data;
5168 int bytes_returned = 0;
5169 __u16 params, byte_count;
5171 cifs_dbg(FYI, "In QFSUnixInfo\n");
5173 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5174 (void **) &pSMB, (void **) &pSMBr);
5178 params = 2; /* level */
5179 pSMB->TotalDataCount = 0;
5180 pSMB->DataCount = 0;
5181 pSMB->DataOffset = 0;
5182 pSMB->MaxParameterCount = cpu_to_le16(2);
5183 /* BB find exact max SMB PDU from sess structure BB */
5184 pSMB->MaxDataCount = cpu_to_le16(100);
5185 pSMB->MaxSetupCount = 0;
5189 pSMB->Reserved2 = 0;
5190 byte_count = params + 1 /* pad */ ;
5191 pSMB->ParameterCount = cpu_to_le16(params);
5192 pSMB->TotalParameterCount = pSMB->ParameterCount;
5193 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5194 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
5195 pSMB->SetupCount = 1;
5196 pSMB->Reserved3 = 0;
5197 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5198 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_CIFS_UNIX_INFO);
5199 inc_rfc1001_len(pSMB, byte_count);
5200 pSMB->ByteCount = cpu_to_le16(byte_count);
5202 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5203 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5205 cifs_dbg(VFS, "Send error in QFSUnixInfo = %d\n", rc);
5206 } else { /* decode response */
5207 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5209 if (rc || get_bcc(&pSMBr->hdr) < 13) {
5210 rc = -EIO; /* bad smb */
5212 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5214 (FILE_SYSTEM_UNIX_INFO
5215 *) (((char *) &pSMBr->hdr.Protocol) +
5217 memcpy(&tcon->fsUnixInfo, response_data,
5218 sizeof(FILE_SYSTEM_UNIX_INFO));
5221 cifs_buf_release(pSMB);
5231 CIFSSMBSetFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon, __u64 cap)
5233 /* level 0x200 SMB_SET_CIFS_UNIX_INFO */
5234 TRANSACTION2_SETFSI_REQ *pSMB = NULL;
5235 TRANSACTION2_SETFSI_RSP *pSMBr = NULL;
5237 int bytes_returned = 0;
5238 __u16 params, param_offset, offset, byte_count;
5240 cifs_dbg(FYI, "In SETFSUnixInfo\n");
5242 /* BB switch to small buf init to save memory */
5243 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5244 (void **) &pSMB, (void **) &pSMBr);
5248 params = 4; /* 2 bytes zero followed by info level. */
5249 pSMB->MaxSetupCount = 0;
5253 pSMB->Reserved2 = 0;
5254 param_offset = offsetof(struct smb_com_transaction2_setfsi_req, FileNum)
5256 offset = param_offset + params;
5258 pSMB->MaxParameterCount = cpu_to_le16(4);
5259 /* BB find exact max SMB PDU from sess structure BB */
5260 pSMB->MaxDataCount = cpu_to_le16(100);
5261 pSMB->SetupCount = 1;
5262 pSMB->Reserved3 = 0;
5263 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FS_INFORMATION);
5264 byte_count = 1 /* pad */ + params + 12;
5266 pSMB->DataCount = cpu_to_le16(12);
5267 pSMB->ParameterCount = cpu_to_le16(params);
5268 pSMB->TotalDataCount = pSMB->DataCount;
5269 pSMB->TotalParameterCount = pSMB->ParameterCount;
5270 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5271 pSMB->DataOffset = cpu_to_le16(offset);
5275 pSMB->InformationLevel = cpu_to_le16(SMB_SET_CIFS_UNIX_INFO);
5278 pSMB->ClientUnixMajor = cpu_to_le16(CIFS_UNIX_MAJOR_VERSION);
5279 pSMB->ClientUnixMinor = cpu_to_le16(CIFS_UNIX_MINOR_VERSION);
5280 pSMB->ClientUnixCap = cpu_to_le64(cap);
5282 inc_rfc1001_len(pSMB, byte_count);
5283 pSMB->ByteCount = cpu_to_le16(byte_count);
5285 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5286 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5288 cifs_dbg(VFS, "Send error in SETFSUnixInfo = %d\n", rc);
5289 } else { /* decode response */
5290 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5292 rc = -EIO; /* bad smb */
5294 cifs_buf_release(pSMB);
5297 goto SETFSUnixRetry;
5305 CIFSSMBQFSPosixInfo(const unsigned int xid, struct cifs_tcon *tcon,
5306 struct kstatfs *FSData)
5308 /* level 0x201 SMB_QUERY_CIFS_POSIX_INFO */
5309 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5310 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5311 FILE_SYSTEM_POSIX_INFO *response_data;
5313 int bytes_returned = 0;
5314 __u16 params, byte_count;
5316 cifs_dbg(FYI, "In QFSPosixInfo\n");
5318 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5323 params = 2; /* level */
5324 pSMB->TotalDataCount = 0;
5325 pSMB->DataCount = 0;
5326 pSMB->DataOffset = 0;
5327 pSMB->MaxParameterCount = cpu_to_le16(2);
5328 /* BB find exact max SMB PDU from sess structure BB */
5329 pSMB->MaxDataCount = cpu_to_le16(100);
5330 pSMB->MaxSetupCount = 0;
5334 pSMB->Reserved2 = 0;
5335 byte_count = params + 1 /* pad */ ;
5336 pSMB->ParameterCount = cpu_to_le16(params);
5337 pSMB->TotalParameterCount = pSMB->ParameterCount;
5338 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5339 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
5340 pSMB->SetupCount = 1;
5341 pSMB->Reserved3 = 0;
5342 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5343 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_FS_INFO);
5344 inc_rfc1001_len(pSMB, byte_count);
5345 pSMB->ByteCount = cpu_to_le16(byte_count);
5347 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5348 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5350 cifs_dbg(FYI, "Send error in QFSUnixInfo = %d\n", rc);
5351 } else { /* decode response */
5352 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5354 if (rc || get_bcc(&pSMBr->hdr) < 13) {
5355 rc = -EIO; /* bad smb */
5357 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5359 (FILE_SYSTEM_POSIX_INFO
5360 *) (((char *) &pSMBr->hdr.Protocol) +
5363 le32_to_cpu(response_data->BlockSize);
5365 * much prefer larger but if server doesn't report
5366 * a valid size than 4K is a reasonable minimum
5368 if (FSData->f_bsize < 512)
5369 FSData->f_bsize = 4096;
5372 le64_to_cpu(response_data->TotalBlocks);
5374 le64_to_cpu(response_data->BlocksAvail);
5375 if (response_data->UserBlocksAvail == cpu_to_le64(-1)) {
5376 FSData->f_bavail = FSData->f_bfree;
5379 le64_to_cpu(response_data->UserBlocksAvail);
5381 if (response_data->TotalFileNodes != cpu_to_le64(-1))
5383 le64_to_cpu(response_data->TotalFileNodes);
5384 if (response_data->FreeFileNodes != cpu_to_le64(-1))
5386 le64_to_cpu(response_data->FreeFileNodes);
5389 cifs_buf_release(pSMB);
5399 * We can not use write of zero bytes trick to set file size due to need for
5400 * large file support. Also note that this SetPathInfo is preferred to
5401 * SetFileInfo based method in next routine which is only needed to work around
5402 * a sharing violation bugin Samba which this routine can run into.
5405 CIFSSMBSetEOF(const unsigned int xid, struct cifs_tcon *tcon,
5406 const char *file_name, __u64 size, struct cifs_sb_info *cifs_sb,
5407 bool set_allocation)
5409 struct smb_com_transaction2_spi_req *pSMB = NULL;
5410 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
5411 struct file_end_of_file_info *parm_data;
5414 int bytes_returned = 0;
5415 int remap = cifs_remap(cifs_sb);
5417 __u16 params, byte_count, data_count, param_offset, offset;
5419 cifs_dbg(FYI, "In SetEOF\n");
5421 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5426 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5428 cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
5429 PATH_MAX, cifs_sb->local_nls, remap);
5430 name_len++; /* trailing null */
5433 name_len = copy_path_name(pSMB->FileName, file_name);
5435 params = 6 + name_len;
5436 data_count = sizeof(struct file_end_of_file_info);
5437 pSMB->MaxParameterCount = cpu_to_le16(2);
5438 pSMB->MaxDataCount = cpu_to_le16(4100);
5439 pSMB->MaxSetupCount = 0;
5443 pSMB->Reserved2 = 0;
5444 param_offset = offsetof(struct smb_com_transaction2_spi_req,
5445 InformationLevel) - 4;
5446 offset = param_offset + params;
5447 if (set_allocation) {
5448 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5449 pSMB->InformationLevel =
5450 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5452 pSMB->InformationLevel =
5453 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
5454 } else /* Set File Size */ {
5455 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5456 pSMB->InformationLevel =
5457 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
5459 pSMB->InformationLevel =
5460 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
5464 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol) +
5466 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5467 pSMB->DataOffset = cpu_to_le16(offset);
5468 pSMB->SetupCount = 1;
5469 pSMB->Reserved3 = 0;
5470 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5471 byte_count = 3 /* pad */ + params + data_count;
5472 pSMB->DataCount = cpu_to_le16(data_count);
5473 pSMB->TotalDataCount = pSMB->DataCount;
5474 pSMB->ParameterCount = cpu_to_le16(params);
5475 pSMB->TotalParameterCount = pSMB->ParameterCount;
5476 pSMB->Reserved4 = 0;
5477 inc_rfc1001_len(pSMB, byte_count);
5478 parm_data->FileSize = cpu_to_le64(size);
5479 pSMB->ByteCount = cpu_to_le16(byte_count);
5480 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5481 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5483 cifs_dbg(FYI, "SetPathInfo (file size) returned %d\n", rc);
5485 cifs_buf_release(pSMB);
5494 CIFSSMBSetFileSize(const unsigned int xid, struct cifs_tcon *tcon,
5495 struct cifsFileInfo *cfile, __u64 size, bool set_allocation)
5497 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5498 struct file_end_of_file_info *parm_data;
5500 __u16 params, param_offset, offset, byte_count, count;
5502 cifs_dbg(FYI, "SetFileSize (via SetFileInfo) %lld\n",
5504 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5509 pSMB->hdr.Pid = cpu_to_le16((__u16)cfile->pid);
5510 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(cfile->pid >> 16));
5513 pSMB->MaxSetupCount = 0;
5517 pSMB->Reserved2 = 0;
5518 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5519 offset = param_offset + params;
5521 count = sizeof(struct file_end_of_file_info);
5522 pSMB->MaxParameterCount = cpu_to_le16(2);
5523 /* BB find exact max SMB PDU from sess structure BB */
5524 pSMB->MaxDataCount = cpu_to_le16(1000);
5525 pSMB->SetupCount = 1;
5526 pSMB->Reserved3 = 0;
5527 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5528 byte_count = 3 /* pad */ + params + count;
5529 pSMB->DataCount = cpu_to_le16(count);
5530 pSMB->ParameterCount = cpu_to_le16(params);
5531 pSMB->TotalDataCount = pSMB->DataCount;
5532 pSMB->TotalParameterCount = pSMB->ParameterCount;
5533 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5534 /* SMB offsets are from the beginning of SMB which is 4 bytes in, after RFC1001 field */
5536 (struct file_end_of_file_info *)(((char *)pSMB) + offset + 4);
5537 pSMB->DataOffset = cpu_to_le16(offset);
5538 parm_data->FileSize = cpu_to_le64(size);
5539 pSMB->Fid = cfile->fid.netfid;
5540 if (set_allocation) {
5541 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5542 pSMB->InformationLevel =
5543 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5545 pSMB->InformationLevel =
5546 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
5547 } else /* Set File Size */ {
5548 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5549 pSMB->InformationLevel =
5550 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
5552 pSMB->InformationLevel =
5553 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
5555 pSMB->Reserved4 = 0;
5556 inc_rfc1001_len(pSMB, byte_count);
5557 pSMB->ByteCount = cpu_to_le16(byte_count);
5558 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
5559 cifs_small_buf_release(pSMB);
5561 cifs_dbg(FYI, "Send error in SetFileInfo (SetFileSize) = %d\n",
5565 /* Note: On -EAGAIN error only caller can retry on handle based calls
5566 since file handle passed in no longer valid */
5571 /* Some legacy servers such as NT4 require that the file times be set on
5572 an open handle, rather than by pathname - this is awkward due to
5573 potential access conflicts on the open, but it is unavoidable for these
5574 old servers since the only other choice is to go from 100 nanosecond DCE
5575 time and resort to the original setpathinfo level which takes the ancient
5576 DOS time format with 2 second granularity */
5578 CIFSSMBSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
5579 const FILE_BASIC_INFO *data, __u16 fid, __u32 pid_of_opener)
5581 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5584 __u16 params, param_offset, offset, byte_count, count;
5586 cifs_dbg(FYI, "Set Times (via SetFileInfo)\n");
5587 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5592 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5593 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5596 pSMB->MaxSetupCount = 0;
5600 pSMB->Reserved2 = 0;
5601 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5602 offset = param_offset + params;
5604 data_offset = (char *)pSMB +
5605 offsetof(struct smb_hdr, Protocol) + offset;
5607 count = sizeof(FILE_BASIC_INFO);
5608 pSMB->MaxParameterCount = cpu_to_le16(2);
5609 /* BB find max SMB PDU from sess */
5610 pSMB->MaxDataCount = cpu_to_le16(1000);
5611 pSMB->SetupCount = 1;
5612 pSMB->Reserved3 = 0;
5613 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5614 byte_count = 3 /* pad */ + params + count;
5615 pSMB->DataCount = cpu_to_le16(count);
5616 pSMB->ParameterCount = cpu_to_le16(params);
5617 pSMB->TotalDataCount = pSMB->DataCount;
5618 pSMB->TotalParameterCount = pSMB->ParameterCount;
5619 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5620 pSMB->DataOffset = cpu_to_le16(offset);
5622 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5623 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5625 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5626 pSMB->Reserved4 = 0;
5627 inc_rfc1001_len(pSMB, byte_count);
5628 pSMB->ByteCount = cpu_to_le16(byte_count);
5629 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
5630 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
5631 cifs_small_buf_release(pSMB);
5633 cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n",
5636 /* Note: On -EAGAIN error only caller can retry on handle based calls
5637 since file handle passed in no longer valid */
5643 CIFSSMBSetFileDisposition(const unsigned int xid, struct cifs_tcon *tcon,
5644 bool delete_file, __u16 fid, __u32 pid_of_opener)
5646 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5649 __u16 params, param_offset, offset, byte_count, count;
5651 cifs_dbg(FYI, "Set File Disposition (via SetFileInfo)\n");
5652 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5657 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5658 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5661 pSMB->MaxSetupCount = 0;
5665 pSMB->Reserved2 = 0;
5666 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5667 offset = param_offset + params;
5669 /* SMB offsets are from the beginning of SMB which is 4 bytes in, after RFC1001 field */
5670 data_offset = (char *)(pSMB) + offset + 4;
5673 pSMB->MaxParameterCount = cpu_to_le16(2);
5674 /* BB find max SMB PDU from sess */
5675 pSMB->MaxDataCount = cpu_to_le16(1000);
5676 pSMB->SetupCount = 1;
5677 pSMB->Reserved3 = 0;
5678 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5679 byte_count = 3 /* pad */ + params + count;
5680 pSMB->DataCount = cpu_to_le16(count);
5681 pSMB->ParameterCount = cpu_to_le16(params);
5682 pSMB->TotalDataCount = pSMB->DataCount;
5683 pSMB->TotalParameterCount = pSMB->ParameterCount;
5684 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5685 pSMB->DataOffset = cpu_to_le16(offset);
5687 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_DISPOSITION_INFO);
5688 pSMB->Reserved4 = 0;
5689 inc_rfc1001_len(pSMB, byte_count);
5690 pSMB->ByteCount = cpu_to_le16(byte_count);
5691 *data_offset = delete_file ? 1 : 0;
5692 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
5693 cifs_small_buf_release(pSMB);
5695 cifs_dbg(FYI, "Send error in SetFileDisposition = %d\n", rc);
5701 CIFSSMBSetPathInfoFB(const unsigned int xid, struct cifs_tcon *tcon,
5702 const char *fileName, const FILE_BASIC_INFO *data,
5703 const struct nls_table *nls_codepage,
5704 struct cifs_sb_info *cifs_sb)
5707 struct cifs_open_parms oparms;
5708 struct cifs_fid fid;
5712 oparms.cifs_sb = cifs_sb;
5713 oparms.desired_access = GENERIC_WRITE;
5714 oparms.create_options = cifs_create_options(cifs_sb, 0);
5715 oparms.disposition = FILE_OPEN;
5716 oparms.path = fileName;
5718 oparms.reconnect = false;
5720 rc = CIFS_open(xid, &oparms, &oplock, NULL);
5724 rc = CIFSSMBSetFileInfo(xid, tcon, data, fid.netfid, current->tgid);
5725 CIFSSMBClose(xid, tcon, fid.netfid);
5732 CIFSSMBSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
5733 const char *fileName, const FILE_BASIC_INFO *data,
5734 const struct nls_table *nls_codepage,
5735 struct cifs_sb_info *cifs_sb)
5737 TRANSACTION2_SPI_REQ *pSMB = NULL;
5738 TRANSACTION2_SPI_RSP *pSMBr = NULL;
5741 int bytes_returned = 0;
5743 __u16 params, param_offset, offset, byte_count, count;
5744 int remap = cifs_remap(cifs_sb);
5746 cifs_dbg(FYI, "In SetTimes\n");
5749 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5754 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5756 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
5757 PATH_MAX, nls_codepage, remap);
5758 name_len++; /* trailing null */
5761 name_len = copy_path_name(pSMB->FileName, fileName);
5764 params = 6 + name_len;
5765 count = sizeof(FILE_BASIC_INFO);
5766 pSMB->MaxParameterCount = cpu_to_le16(2);
5767 /* BB find max SMB PDU from sess structure BB */
5768 pSMB->MaxDataCount = cpu_to_le16(1000);
5769 pSMB->MaxSetupCount = 0;
5773 pSMB->Reserved2 = 0;
5774 param_offset = offsetof(struct smb_com_transaction2_spi_req,
5775 InformationLevel) - 4;
5776 offset = param_offset + params;
5777 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5778 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5779 pSMB->DataOffset = cpu_to_le16(offset);
5780 pSMB->SetupCount = 1;
5781 pSMB->Reserved3 = 0;
5782 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5783 byte_count = 3 /* pad */ + params + count;
5785 pSMB->DataCount = cpu_to_le16(count);
5786 pSMB->ParameterCount = cpu_to_le16(params);
5787 pSMB->TotalDataCount = pSMB->DataCount;
5788 pSMB->TotalParameterCount = pSMB->ParameterCount;
5789 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5790 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5792 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5793 pSMB->Reserved4 = 0;
5794 inc_rfc1001_len(pSMB, byte_count);
5795 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
5796 pSMB->ByteCount = cpu_to_le16(byte_count);
5797 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5798 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5800 cifs_dbg(FYI, "SetPathInfo (times) returned %d\n", rc);
5802 cifs_buf_release(pSMB);
5807 if (rc == -EOPNOTSUPP)
5808 return CIFSSMBSetPathInfoFB(xid, tcon, fileName, data,
5809 nls_codepage, cifs_sb);
5815 cifs_fill_unix_set_info(FILE_UNIX_BASIC_INFO *data_offset,
5816 const struct cifs_unix_set_info_args *args)
5818 u64 uid = NO_CHANGE_64, gid = NO_CHANGE_64;
5819 u64 mode = args->mode;
5821 if (uid_valid(args->uid))
5822 uid = from_kuid(&init_user_ns, args->uid);
5823 if (gid_valid(args->gid))
5824 gid = from_kgid(&init_user_ns, args->gid);
5827 * Samba server ignores set of file size to zero due to bugs in some
5828 * older clients, but we should be precise - we use SetFileSize to
5829 * set file size and do not want to truncate file size to zero
5830 * accidentally as happened on one Samba server beta by putting
5831 * zero instead of -1 here
5833 data_offset->EndOfFile = cpu_to_le64(NO_CHANGE_64);
5834 data_offset->NumOfBytes = cpu_to_le64(NO_CHANGE_64);
5835 data_offset->LastStatusChange = cpu_to_le64(args->ctime);
5836 data_offset->LastAccessTime = cpu_to_le64(args->atime);
5837 data_offset->LastModificationTime = cpu_to_le64(args->mtime);
5838 data_offset->Uid = cpu_to_le64(uid);
5839 data_offset->Gid = cpu_to_le64(gid);
5840 /* better to leave device as zero when it is */
5841 data_offset->DevMajor = cpu_to_le64(MAJOR(args->device));
5842 data_offset->DevMinor = cpu_to_le64(MINOR(args->device));
5843 data_offset->Permissions = cpu_to_le64(mode);
5846 data_offset->Type = cpu_to_le32(UNIX_FILE);
5847 else if (S_ISDIR(mode))
5848 data_offset->Type = cpu_to_le32(UNIX_DIR);
5849 else if (S_ISLNK(mode))
5850 data_offset->Type = cpu_to_le32(UNIX_SYMLINK);
5851 else if (S_ISCHR(mode))
5852 data_offset->Type = cpu_to_le32(UNIX_CHARDEV);
5853 else if (S_ISBLK(mode))
5854 data_offset->Type = cpu_to_le32(UNIX_BLOCKDEV);
5855 else if (S_ISFIFO(mode))
5856 data_offset->Type = cpu_to_le32(UNIX_FIFO);
5857 else if (S_ISSOCK(mode))
5858 data_offset->Type = cpu_to_le32(UNIX_SOCKET);
5862 CIFSSMBUnixSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
5863 const struct cifs_unix_set_info_args *args,
5864 u16 fid, u32 pid_of_opener)
5866 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5869 u16 params, param_offset, offset, byte_count, count;
5871 cifs_dbg(FYI, "Set Unix Info (via SetFileInfo)\n");
5872 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5877 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5878 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5881 pSMB->MaxSetupCount = 0;
5885 pSMB->Reserved2 = 0;
5886 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5887 offset = param_offset + params;
5889 data_offset = (char *)pSMB +
5890 offsetof(struct smb_hdr, Protocol) + offset;
5892 count = sizeof(FILE_UNIX_BASIC_INFO);
5894 pSMB->MaxParameterCount = cpu_to_le16(2);
5895 /* BB find max SMB PDU from sess */
5896 pSMB->MaxDataCount = cpu_to_le16(1000);
5897 pSMB->SetupCount = 1;
5898 pSMB->Reserved3 = 0;
5899 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5900 byte_count = 3 /* pad */ + params + count;
5901 pSMB->DataCount = cpu_to_le16(count);
5902 pSMB->ParameterCount = cpu_to_le16(params);
5903 pSMB->TotalDataCount = pSMB->DataCount;
5904 pSMB->TotalParameterCount = pSMB->ParameterCount;
5905 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5906 pSMB->DataOffset = cpu_to_le16(offset);
5908 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
5909 pSMB->Reserved4 = 0;
5910 inc_rfc1001_len(pSMB, byte_count);
5911 pSMB->ByteCount = cpu_to_le16(byte_count);
5913 cifs_fill_unix_set_info((FILE_UNIX_BASIC_INFO *)data_offset, args);
5915 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
5916 cifs_small_buf_release(pSMB);
5918 cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n",
5921 /* Note: On -EAGAIN error only caller can retry on handle based calls
5922 since file handle passed in no longer valid */
5928 CIFSSMBUnixSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
5929 const char *file_name,
5930 const struct cifs_unix_set_info_args *args,
5931 const struct nls_table *nls_codepage, int remap)
5933 TRANSACTION2_SPI_REQ *pSMB = NULL;
5934 TRANSACTION2_SPI_RSP *pSMBr = NULL;
5937 int bytes_returned = 0;
5938 FILE_UNIX_BASIC_INFO *data_offset;
5939 __u16 params, param_offset, offset, count, byte_count;
5941 cifs_dbg(FYI, "In SetUID/GID/Mode\n");
5943 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5948 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5950 cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
5951 PATH_MAX, nls_codepage, remap);
5952 name_len++; /* trailing null */
5955 name_len = copy_path_name(pSMB->FileName, file_name);
5958 params = 6 + name_len;
5959 count = sizeof(FILE_UNIX_BASIC_INFO);
5960 pSMB->MaxParameterCount = cpu_to_le16(2);
5961 /* BB find max SMB PDU from sess structure BB */
5962 pSMB->MaxDataCount = cpu_to_le16(1000);
5963 pSMB->MaxSetupCount = 0;
5967 pSMB->Reserved2 = 0;
5968 param_offset = offsetof(struct smb_com_transaction2_spi_req,
5969 InformationLevel) - 4;
5970 offset = param_offset + params;
5971 /* SMB offsets are from the beginning of SMB which is 4 bytes in, after RFC1001 field */
5972 data_offset = (FILE_UNIX_BASIC_INFO *)((char *) pSMB + offset + 4);
5973 memset(data_offset, 0, count);
5974 pSMB->DataOffset = cpu_to_le16(offset);
5975 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5976 pSMB->SetupCount = 1;
5977 pSMB->Reserved3 = 0;
5978 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5979 byte_count = 3 /* pad */ + params + count;
5980 pSMB->ParameterCount = cpu_to_le16(params);
5981 pSMB->DataCount = cpu_to_le16(count);
5982 pSMB->TotalParameterCount = pSMB->ParameterCount;
5983 pSMB->TotalDataCount = pSMB->DataCount;
5984 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
5985 pSMB->Reserved4 = 0;
5986 inc_rfc1001_len(pSMB, byte_count);
5988 cifs_fill_unix_set_info(data_offset, args);
5990 pSMB->ByteCount = cpu_to_le16(byte_count);
5991 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5992 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5994 cifs_dbg(FYI, "SetPathInfo (perms) returned %d\n", rc);
5996 cifs_buf_release(pSMB);
6002 #ifdef CONFIG_CIFS_XATTR
6004 * Do a path-based QUERY_ALL_EAS call and parse the result. This is a common
6005 * function used by listxattr and getxattr type calls. When ea_name is set,
6006 * it looks for that attribute name and stuffs that value into the EAData
6007 * buffer. When ea_name is NULL, it stuffs a list of attribute names into the
6008 * buffer. In both cases, the return value is either the length of the
6009 * resulting data or a negative error code. If EAData is a NULL pointer then
6010 * the data isn't copied to it, but the length is returned.
6013 CIFSSMBQAllEAs(const unsigned int xid, struct cifs_tcon *tcon,
6014 const unsigned char *searchName, const unsigned char *ea_name,
6015 char *EAData, size_t buf_size,
6016 struct cifs_sb_info *cifs_sb)
6018 /* BB assumes one setup word */
6019 TRANSACTION2_QPI_REQ *pSMB = NULL;
6020 TRANSACTION2_QPI_RSP *pSMBr = NULL;
6021 int remap = cifs_remap(cifs_sb);
6022 struct nls_table *nls_codepage = cifs_sb->local_nls;
6026 struct fealist *ea_response_data;
6027 struct fea *temp_fea;
6030 __u16 params, byte_count, data_offset;
6031 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
6033 cifs_dbg(FYI, "In Query All EAs path %s\n", searchName);
6035 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6040 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6042 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
6043 PATH_MAX, nls_codepage, remap);
6044 list_len++; /* trailing null */
6047 list_len = copy_path_name(pSMB->FileName, searchName);
6050 params = 2 /* level */ + 4 /* reserved */ + list_len /* includes NUL */;
6051 pSMB->TotalDataCount = 0;
6052 pSMB->MaxParameterCount = cpu_to_le16(2);
6053 /* BB find exact max SMB PDU from sess structure BB */
6054 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
6055 pSMB->MaxSetupCount = 0;
6059 pSMB->Reserved2 = 0;
6060 pSMB->ParameterOffset = cpu_to_le16(offsetof(
6061 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
6062 pSMB->DataCount = 0;
6063 pSMB->DataOffset = 0;
6064 pSMB->SetupCount = 1;
6065 pSMB->Reserved3 = 0;
6066 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
6067 byte_count = params + 1 /* pad */ ;
6068 pSMB->TotalParameterCount = cpu_to_le16(params);
6069 pSMB->ParameterCount = pSMB->TotalParameterCount;
6070 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_QUERY_ALL_EAS);
6071 pSMB->Reserved4 = 0;
6072 inc_rfc1001_len(pSMB, byte_count);
6073 pSMB->ByteCount = cpu_to_le16(byte_count);
6075 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6076 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
6078 cifs_dbg(FYI, "Send error in QueryAllEAs = %d\n", rc);
6083 /* BB also check enough total bytes returned */
6084 /* BB we need to improve the validity checking
6085 of these trans2 responses */
6087 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
6088 if (rc || get_bcc(&pSMBr->hdr) < 4) {
6089 rc = -EIO; /* bad smb */
6093 /* check that length of list is not more than bcc */
6094 /* check that each entry does not go beyond length
6096 /* check that each element of each entry does not
6097 go beyond end of list */
6098 /* validate_trans2_offsets() */
6099 /* BB check if start of smb + data_offset > &bcc+ bcc */
6101 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
6102 ea_response_data = (struct fealist *)
6103 (((char *) &pSMBr->hdr.Protocol) + data_offset);
6105 list_len = le32_to_cpu(ea_response_data->list_len);
6106 cifs_dbg(FYI, "ea length %d\n", list_len);
6107 if (list_len <= 8) {
6108 cifs_dbg(FYI, "empty EA list returned from server\n");
6109 /* didn't find the named attribute */
6115 /* make sure list_len doesn't go past end of SMB */
6116 end_of_smb = (char *)pByteArea(&pSMBr->hdr) + get_bcc(&pSMBr->hdr);
6117 if ((char *)ea_response_data + list_len > end_of_smb) {
6118 cifs_dbg(FYI, "EA list appears to go beyond SMB\n");
6123 /* account for ea list len */
6125 temp_fea = ea_response_data->list;
6126 temp_ptr = (char *)temp_fea;
6127 while (list_len > 0) {
6128 unsigned int name_len;
6133 /* make sure we can read name_len and value_len */
6135 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
6140 name_len = temp_fea->name_len;
6141 value_len = le16_to_cpu(temp_fea->value_len);
6142 list_len -= name_len + 1 + value_len;
6144 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
6150 if (ea_name_len == name_len &&
6151 memcmp(ea_name, temp_ptr, name_len) == 0) {
6152 temp_ptr += name_len + 1;
6156 if ((size_t)value_len > buf_size) {
6160 memcpy(EAData, temp_ptr, value_len);
6164 /* account for prefix user. and trailing null */
6165 rc += (5 + 1 + name_len);
6166 if (rc < (int) buf_size) {
6167 memcpy(EAData, "user.", 5);
6169 memcpy(EAData, temp_ptr, name_len);
6171 /* null terminate name */
6174 } else if (buf_size == 0) {
6175 /* skip copy - calc size only */
6177 /* stop before overrun buffer */
6182 temp_ptr += name_len + 1 + value_len;
6183 temp_fea = (struct fea *)temp_ptr;
6186 /* didn't find the named attribute */
6191 cifs_buf_release(pSMB);
6199 CIFSSMBSetEA(const unsigned int xid, struct cifs_tcon *tcon,
6200 const char *fileName, const char *ea_name, const void *ea_value,
6201 const __u16 ea_value_len, const struct nls_table *nls_codepage,
6202 struct cifs_sb_info *cifs_sb)
6204 struct smb_com_transaction2_spi_req *pSMB = NULL;
6205 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
6206 struct fealist *parm_data;
6209 int bytes_returned = 0;
6210 __u16 params, param_offset, byte_count, offset, count;
6211 int remap = cifs_remap(cifs_sb);
6213 cifs_dbg(FYI, "In SetEA\n");
6215 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6220 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6222 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
6223 PATH_MAX, nls_codepage, remap);
6224 name_len++; /* trailing null */
6227 name_len = copy_path_name(pSMB->FileName, fileName);
6230 params = 6 + name_len;
6232 /* done calculating parms using name_len of file name,
6233 now use name_len to calculate length of ea name
6234 we are going to create in the inode xattrs */
6235 if (ea_name == NULL)
6238 name_len = strnlen(ea_name, 255);
6240 count = sizeof(*parm_data) + ea_value_len + name_len;
6241 pSMB->MaxParameterCount = cpu_to_le16(2);
6242 /* BB find max SMB PDU from sess */
6243 pSMB->MaxDataCount = cpu_to_le16(1000);
6244 pSMB->MaxSetupCount = 0;
6248 pSMB->Reserved2 = 0;
6249 param_offset = offsetof(struct smb_com_transaction2_spi_req,
6250 InformationLevel) - 4;
6251 offset = param_offset + params;
6252 pSMB->InformationLevel =
6253 cpu_to_le16(SMB_SET_FILE_EA);
6255 parm_data = (void *)pSMB + offsetof(struct smb_hdr, Protocol) + offset;
6256 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6257 pSMB->DataOffset = cpu_to_le16(offset);
6258 pSMB->SetupCount = 1;
6259 pSMB->Reserved3 = 0;
6260 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6261 byte_count = 3 /* pad */ + params + count;
6262 pSMB->DataCount = cpu_to_le16(count);
6263 parm_data->list_len = cpu_to_le32(count);
6264 parm_data->list[0].EA_flags = 0;
6265 /* we checked above that name len is less than 255 */
6266 parm_data->list[0].name_len = (__u8)name_len;
6267 /* EA names are always ASCII */
6269 strncpy(parm_data->list[0].name, ea_name, name_len);
6270 parm_data->list[0].name[name_len] = 0;
6271 parm_data->list[0].value_len = cpu_to_le16(ea_value_len);
6272 /* caller ensures that ea_value_len is less than 64K but
6273 we need to ensure that it fits within the smb */
6275 /*BB add length check to see if it would fit in
6276 negotiated SMB buffer size BB */
6277 /* if (ea_value_len > buffer_size - 512 (enough for header)) */
6279 memcpy(parm_data->list[0].name+name_len+1,
6280 ea_value, ea_value_len);
6282 pSMB->TotalDataCount = pSMB->DataCount;
6283 pSMB->ParameterCount = cpu_to_le16(params);
6284 pSMB->TotalParameterCount = pSMB->ParameterCount;
6285 pSMB->Reserved4 = 0;
6286 inc_rfc1001_len(pSMB, byte_count);
6287 pSMB->ByteCount = cpu_to_le16(byte_count);
6288 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6289 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
6291 cifs_dbg(FYI, "SetPathInfo (EA) returned %d\n", rc);
6293 cifs_buf_release(pSMB);