1 // SPDX-License-Identifier: LGPL-2.1
4 * Copyright (C) International Business Machines Corp., 2002,2010
5 * Author(s): Steve French (sfrench@us.ibm.com)
7 * Contains the routines for constructing the SMB PDUs themselves
11 /* SMB/CIFS PDU handling routines here - except for leftovers in connect.c */
12 /* These are mostly routines that operate on a pathname, or on a tree id */
13 /* (mounted volume), but there are eight handle based routines which must be */
14 /* treated slightly differently for reconnection purposes since we never */
15 /* want to reuse a stale file handle and only the caller knows the file info */
18 #include <linux/filelock.h>
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>
31 #include "cifsproto.h"
32 #include "cifs_unicode.h"
33 #include "cifs_debug.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 /* reconnect the socket, tcon, and smb session if needed */
69 cifs_reconnect_tcon(struct cifs_tcon *tcon, int smb_command)
73 struct TCP_Server_Info *server;
74 struct nls_table *nls_codepage = NULL;
77 * SMBs NegProt, SessSetup, uLogoff do not have tcon yet so check for
78 * tcp and smb session status done differently for those three - in the
88 * only tree disconnect, open, and write, (and ulogoff which does not
89 * have tcon) are allowed as we start umount
91 spin_lock(&tcon->tc_lock);
92 if (tcon->status == TID_EXITING) {
93 if (smb_command != SMB_COM_TREE_DISCONNECT) {
94 spin_unlock(&tcon->tc_lock);
95 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
100 spin_unlock(&tcon->tc_lock);
103 rc = cifs_wait_for_server_reconnect(server, tcon->retry);
107 spin_lock(&ses->chan_lock);
108 if (!cifs_chan_needs_reconnect(ses, server) && !tcon->need_reconnect) {
109 spin_unlock(&ses->chan_lock);
112 spin_unlock(&ses->chan_lock);
114 mutex_lock(&ses->session_mutex);
116 * Recheck after acquire mutex. If another thread is negotiating
117 * and the server never sends an answer the socket will be closed
118 * and tcpStatus set to reconnect.
120 spin_lock(&server->srv_lock);
121 if (server->tcpStatus == CifsNeedReconnect) {
122 spin_unlock(&server->srv_lock);
123 mutex_unlock(&ses->session_mutex);
130 spin_unlock(&server->srv_lock);
132 nls_codepage = ses->local_nls;
135 * need to prevent multiple threads trying to simultaneously
136 * reconnect the same SMB session
138 spin_lock(&ses->ses_lock);
139 spin_lock(&ses->chan_lock);
140 if (!cifs_chan_needs_reconnect(ses, server) &&
141 ses->ses_status == SES_GOOD) {
142 spin_unlock(&ses->chan_lock);
143 spin_unlock(&ses->ses_lock);
145 /* this means that we only need to tree connect */
146 if (tcon->need_reconnect)
147 goto skip_sess_setup;
149 mutex_unlock(&ses->session_mutex);
152 spin_unlock(&ses->chan_lock);
153 spin_unlock(&ses->ses_lock);
155 rc = cifs_negotiate_protocol(0, ses, server);
157 rc = cifs_setup_session(0, ses, server, nls_codepage);
159 /* do we need to reconnect tcon? */
160 if (rc || !tcon->need_reconnect) {
161 mutex_unlock(&ses->session_mutex);
166 cifs_mark_open_files_invalid(tcon);
167 rc = cifs_tree_connect(0, tcon, nls_codepage);
168 mutex_unlock(&ses->session_mutex);
169 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
172 pr_warn_once("reconnect tcon failed rc = %d\n", rc);
176 atomic_inc(&tconInfoReconnectCount);
178 /* tell server Unix caps we support */
180 reset_cifs_unix_caps(0, tcon, NULL, NULL);
183 * Removed call to reopen open files here. It is safer (and faster) to
184 * reopen files one at a time as needed in read and write.
186 * FIXME: what about file locks? don't we need to reclaim them ASAP?
191 * Check if handle based operation so we know whether we can continue
192 * or not without returning to caller to reset file handle
194 switch (smb_command) {
195 case SMB_COM_READ_ANDX:
196 case SMB_COM_WRITE_ANDX:
198 case SMB_COM_FIND_CLOSE2:
199 case SMB_COM_LOCKING_ANDX:
206 /* Allocate and return pointer to an SMB request buffer, and set basic
207 SMB information in the SMB header. If the return code is zero, this
208 function must have filled in request_buf pointer */
210 small_smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
215 rc = cifs_reconnect_tcon(tcon, smb_command);
219 *request_buf = cifs_small_buf_get();
220 if (*request_buf == NULL) {
221 /* BB should we add a retry in here if not a writepage? */
225 header_assemble((struct smb_hdr *) *request_buf, smb_command,
229 cifs_stats_inc(&tcon->num_smbs_sent);
235 small_smb_init_no_tc(const int smb_command, const int wct,
236 struct cifs_ses *ses, void **request_buf)
239 struct smb_hdr *buffer;
241 rc = small_smb_init(smb_command, wct, NULL, request_buf);
245 buffer = (struct smb_hdr *)*request_buf;
246 buffer->Mid = get_next_mid(ses->server);
247 if (ses->capabilities & CAP_UNICODE)
248 buffer->Flags2 |= SMBFLG2_UNICODE;
249 if (ses->capabilities & CAP_STATUS32)
250 buffer->Flags2 |= SMBFLG2_ERR_STATUS;
252 /* uid, tid can stay at zero as set in header assemble */
254 /* BB add support for turning on the signing when
255 this function is used after 1st of session setup requests */
260 /* If the return code is zero, this function must fill in request_buf pointer */
262 __smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
263 void **request_buf, void **response_buf)
265 *request_buf = cifs_buf_get();
266 if (*request_buf == NULL) {
267 /* BB should we add a retry in here if not a writepage? */
270 /* Although the original thought was we needed the response buf for */
271 /* potential retries of smb operations it turns out we can determine */
272 /* from the mid flags when the request buffer can be resent without */
273 /* having to use a second distinct buffer for the response */
275 *response_buf = *request_buf;
277 header_assemble((struct smb_hdr *) *request_buf, smb_command, tcon,
281 cifs_stats_inc(&tcon->num_smbs_sent);
286 /* If the return code is zero, this function must fill in request_buf pointer */
288 smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
289 void **request_buf, void **response_buf)
293 rc = cifs_reconnect_tcon(tcon, smb_command);
297 return __smb_init(smb_command, wct, tcon, request_buf, response_buf);
301 smb_init_no_reconnect(int smb_command, int wct, struct cifs_tcon *tcon,
302 void **request_buf, void **response_buf)
304 spin_lock(&tcon->ses->chan_lock);
305 if (cifs_chan_needs_reconnect(tcon->ses, tcon->ses->server) ||
306 tcon->need_reconnect) {
307 spin_unlock(&tcon->ses->chan_lock);
310 spin_unlock(&tcon->ses->chan_lock);
312 return __smb_init(smb_command, wct, tcon, request_buf, response_buf);
315 static int validate_t2(struct smb_t2_rsp *pSMB)
317 unsigned int total_size;
319 /* check for plausible wct */
320 if (pSMB->hdr.WordCount < 10)
323 /* check for parm and data offset going beyond end of smb */
324 if (get_unaligned_le16(&pSMB->t2_rsp.ParameterOffset) > 1024 ||
325 get_unaligned_le16(&pSMB->t2_rsp.DataOffset) > 1024)
328 total_size = get_unaligned_le16(&pSMB->t2_rsp.ParameterCount);
329 if (total_size >= 512)
332 /* check that bcc is at least as big as parms + data, and that it is
333 * less than negotiated smb buffer
335 total_size += get_unaligned_le16(&pSMB->t2_rsp.DataCount);
336 if (total_size > get_bcc(&pSMB->hdr) ||
337 total_size >= CIFSMaxBufSize + MAX_CIFS_HDR_SIZE)
342 cifs_dump_mem("Invalid transact2 SMB: ", (char *)pSMB,
343 sizeof(struct smb_t2_rsp) + 16);
348 decode_ext_sec_blob(struct cifs_ses *ses, NEGOTIATE_RSP *pSMBr)
352 char *guid = pSMBr->u.extended_response.GUID;
353 struct TCP_Server_Info *server = ses->server;
355 count = get_bcc(&pSMBr->hdr);
356 if (count < SMB1_CLIENT_GUID_SIZE)
359 spin_lock(&cifs_tcp_ses_lock);
360 if (server->srv_count > 1) {
361 spin_unlock(&cifs_tcp_ses_lock);
362 if (memcmp(server->server_GUID, guid, SMB1_CLIENT_GUID_SIZE) != 0) {
363 cifs_dbg(FYI, "server UID changed\n");
364 memcpy(server->server_GUID, guid, SMB1_CLIENT_GUID_SIZE);
367 spin_unlock(&cifs_tcp_ses_lock);
368 memcpy(server->server_GUID, guid, SMB1_CLIENT_GUID_SIZE);
371 if (count == SMB1_CLIENT_GUID_SIZE) {
372 server->sec_ntlmssp = true;
374 count -= SMB1_CLIENT_GUID_SIZE;
375 rc = decode_negTokenInit(
376 pSMBr->u.extended_response.SecurityBlob, count, server);
385 should_set_ext_sec_flag(enum securityEnum sectype)
392 if (global_secflags &
393 (CIFSSEC_MAY_KRB5 | CIFSSEC_MAY_NTLMSSP))
402 CIFSSMBNegotiate(const unsigned int xid,
403 struct cifs_ses *ses,
404 struct TCP_Server_Info *server)
407 NEGOTIATE_RSP *pSMBr;
414 WARN(1, "%s: server is NULL!\n", __func__);
418 rc = smb_init(SMB_COM_NEGOTIATE, 0, NULL /* no tcon yet */ ,
419 (void **) &pSMB, (void **) &pSMBr);
423 pSMB->hdr.Mid = get_next_mid(server);
424 pSMB->hdr.Flags2 |= (SMBFLG2_UNICODE | SMBFLG2_ERR_STATUS);
426 if (should_set_ext_sec_flag(ses->sectype)) {
427 cifs_dbg(FYI, "Requesting extended security\n");
428 pSMB->hdr.Flags2 |= SMBFLG2_EXT_SEC;
433 * We know that all the name entries in the protocols array
434 * are short (< 16 bytes anyway) and are NUL terminated.
436 for (i = 0; i < CIFS_NUM_PROT; i++) {
437 size_t len = strlen(protocols[i].name) + 1;
439 memcpy(&pSMB->DialectsArray[count], protocols[i].name, len);
442 inc_rfc1001_len(pSMB, count);
443 pSMB->ByteCount = cpu_to_le16(count);
445 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
446 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
450 server->dialect = le16_to_cpu(pSMBr->DialectIndex);
451 cifs_dbg(FYI, "Dialect: %d\n", server->dialect);
452 /* Check wct = 1 error case */
453 if ((pSMBr->hdr.WordCount <= 13) || (server->dialect == BAD_PROT)) {
454 /* core returns wct = 1, but we do not ask for core - otherwise
455 small wct just comes when dialect index is -1 indicating we
456 could not negotiate a common dialect */
459 } else if (pSMBr->hdr.WordCount != 17) {
464 /* else wct == 17, NTLM or better */
466 server->sec_mode = pSMBr->SecurityMode;
467 if ((server->sec_mode & SECMODE_USER) == 0)
468 cifs_dbg(FYI, "share mode security\n");
470 /* one byte, so no need to convert this or EncryptionKeyLen from
472 server->maxReq = min_t(unsigned int, le16_to_cpu(pSMBr->MaxMpxCount),
474 set_credits(server, server->maxReq);
475 /* probably no need to store and check maxvcs */
476 server->maxBuf = le32_to_cpu(pSMBr->MaxBufferSize);
477 /* set up max_read for readahead check */
478 server->max_read = server->maxBuf;
479 server->max_rw = le32_to_cpu(pSMBr->MaxRawSize);
480 cifs_dbg(NOISY, "Max buf = %d\n", ses->server->maxBuf);
481 server->capabilities = le32_to_cpu(pSMBr->Capabilities);
482 server->timeAdj = (int)(__s16)le16_to_cpu(pSMBr->ServerTimeZone);
483 server->timeAdj *= 60;
485 if (pSMBr->EncryptionKeyLength == CIFS_CRYPTO_KEY_SIZE) {
486 server->negflavor = CIFS_NEGFLAVOR_UNENCAP;
487 memcpy(ses->server->cryptkey, pSMBr->u.EncryptionKey,
488 CIFS_CRYPTO_KEY_SIZE);
489 } else if (pSMBr->hdr.Flags2 & SMBFLG2_EXT_SEC ||
490 server->capabilities & CAP_EXTENDED_SECURITY) {
491 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
492 rc = decode_ext_sec_blob(ses, pSMBr);
493 } else if (server->sec_mode & SECMODE_PW_ENCRYPT) {
494 rc = -EIO; /* no crypt key only if plain text pwd */
496 server->negflavor = CIFS_NEGFLAVOR_UNENCAP;
497 server->capabilities &= ~CAP_EXTENDED_SECURITY;
501 rc = cifs_enable_signing(server, ses->sign);
503 cifs_buf_release(pSMB);
505 cifs_dbg(FYI, "negprot rc %d\n", rc);
510 CIFSSMBTDis(const unsigned int xid, struct cifs_tcon *tcon)
512 struct smb_hdr *smb_buffer;
515 cifs_dbg(FYI, "In tree disconnect\n");
517 /* BB: do we need to check this? These should never be NULL. */
518 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
522 * No need to return error on this operation if tid invalidated and
523 * closed on server already e.g. due to tcp session crashing. Also,
524 * the tcon is no longer on the list, so no need to take lock before
527 spin_lock(&tcon->ses->chan_lock);
528 if ((tcon->need_reconnect) || CIFS_ALL_CHANS_NEED_RECONNECT(tcon->ses)) {
529 spin_unlock(&tcon->ses->chan_lock);
532 spin_unlock(&tcon->ses->chan_lock);
534 rc = small_smb_init(SMB_COM_TREE_DISCONNECT, 0, tcon,
535 (void **)&smb_buffer);
539 rc = SendReceiveNoRsp(xid, tcon->ses, (char *)smb_buffer, 0);
540 cifs_small_buf_release(smb_buffer);
542 cifs_dbg(FYI, "Tree disconnect failed %d\n", rc);
544 /* No need to return error on this operation if tid invalidated and
545 closed on server already e.g. due to tcp session crashing */
553 * This is a no-op for now. We're not really interested in the reply, but
554 * rather in the fact that the server sent one and that server->lstrp
557 * FIXME: maybe we should consider checking that the reply matches request?
560 cifs_echo_callback(struct mid_q_entry *mid)
562 struct TCP_Server_Info *server = mid->callback_data;
563 struct cifs_credits credits = { .value = 1, .instance = 0 };
566 add_credits(server, &credits, CIFS_ECHO_OP);
570 CIFSSMBEcho(struct TCP_Server_Info *server)
575 struct smb_rqst rqst = { .rq_iov = iov,
578 cifs_dbg(FYI, "In echo request\n");
580 rc = small_smb_init(SMB_COM_ECHO, 0, NULL, (void **)&smb);
584 if (server->capabilities & CAP_UNICODE)
585 smb->hdr.Flags2 |= SMBFLG2_UNICODE;
587 /* set up echo request */
588 smb->hdr.Tid = 0xffff;
589 smb->hdr.WordCount = 1;
590 put_unaligned_le16(1, &smb->EchoCount);
591 put_bcc(1, &smb->hdr);
593 inc_rfc1001_len(smb, 3);
596 iov[0].iov_base = smb;
597 iov[1].iov_len = get_rfc1002_length(smb);
598 iov[1].iov_base = (char *)smb + 4;
600 rc = cifs_call_async(server, &rqst, NULL, cifs_echo_callback, NULL,
601 server, CIFS_NON_BLOCKING | CIFS_ECHO_OP, NULL);
603 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
605 cifs_small_buf_release(smb);
611 CIFSSMBLogoff(const unsigned int xid, struct cifs_ses *ses)
613 LOGOFF_ANDX_REQ *pSMB;
616 cifs_dbg(FYI, "In SMBLogoff for session disconnect\n");
619 * BB: do we need to check validity of ses and server? They should
620 * always be valid since we have an active reference. If not, that
621 * should probably be a BUG()
623 if (!ses || !ses->server)
626 mutex_lock(&ses->session_mutex);
627 spin_lock(&ses->chan_lock);
628 if (CIFS_ALL_CHANS_NEED_RECONNECT(ses)) {
629 spin_unlock(&ses->chan_lock);
630 goto session_already_dead; /* no need to send SMBlogoff if uid
631 already closed due to reconnect */
633 spin_unlock(&ses->chan_lock);
635 rc = small_smb_init(SMB_COM_LOGOFF_ANDX, 2, NULL, (void **)&pSMB);
637 mutex_unlock(&ses->session_mutex);
641 pSMB->hdr.Mid = get_next_mid(ses->server);
643 if (ses->server->sign)
644 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
646 pSMB->hdr.Uid = ses->Suid;
648 pSMB->AndXCommand = 0xFF;
649 rc = SendReceiveNoRsp(xid, ses, (char *) pSMB, 0);
650 cifs_small_buf_release(pSMB);
651 session_already_dead:
652 mutex_unlock(&ses->session_mutex);
654 /* if session dead then we do not need to do ulogoff,
655 since server closed smb session, no sense reporting
663 CIFSPOSIXDelFile(const unsigned int xid, struct cifs_tcon *tcon,
664 const char *fileName, __u16 type,
665 const struct nls_table *nls_codepage, int remap)
667 TRANSACTION2_SPI_REQ *pSMB = NULL;
668 TRANSACTION2_SPI_RSP *pSMBr = NULL;
669 struct unlink_psx_rq *pRqD;
672 int bytes_returned = 0;
673 __u16 params, param_offset, offset, byte_count;
675 cifs_dbg(FYI, "In POSIX delete\n");
677 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
682 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
684 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
685 PATH_MAX, nls_codepage, remap);
686 name_len++; /* trailing null */
689 name_len = copy_path_name(pSMB->FileName, fileName);
692 params = 6 + name_len;
693 pSMB->MaxParameterCount = cpu_to_le16(2);
694 pSMB->MaxDataCount = 0; /* BB double check this with jra */
695 pSMB->MaxSetupCount = 0;
700 param_offset = offsetof(struct smb_com_transaction2_spi_req,
701 InformationLevel) - 4;
702 offset = param_offset + params;
704 /* Setup pointer to Request Data (inode type).
705 * Note that SMB offsets are from the beginning of SMB which is 4 bytes
706 * in, after RFC1001 field
708 pRqD = (struct unlink_psx_rq *)((char *)(pSMB) + offset + 4);
709 pRqD->type = cpu_to_le16(type);
710 pSMB->ParameterOffset = cpu_to_le16(param_offset);
711 pSMB->DataOffset = cpu_to_le16(offset);
712 pSMB->SetupCount = 1;
714 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
715 byte_count = 3 /* pad */ + params + sizeof(struct unlink_psx_rq);
717 pSMB->DataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
718 pSMB->TotalDataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
719 pSMB->ParameterCount = cpu_to_le16(params);
720 pSMB->TotalParameterCount = pSMB->ParameterCount;
721 pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_UNLINK);
723 inc_rfc1001_len(pSMB, byte_count);
724 pSMB->ByteCount = cpu_to_le16(byte_count);
725 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
726 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
728 cifs_dbg(FYI, "Posix delete returned %d\n", rc);
729 cifs_buf_release(pSMB);
731 cifs_stats_inc(&tcon->stats.cifs_stats.num_deletes);
740 CIFSSMBDelFile(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
741 struct cifs_sb_info *cifs_sb)
743 DELETE_FILE_REQ *pSMB = NULL;
744 DELETE_FILE_RSP *pSMBr = NULL;
748 int remap = cifs_remap(cifs_sb);
751 rc = smb_init(SMB_COM_DELETE, 1, tcon, (void **) &pSMB,
756 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
757 name_len = cifsConvertToUTF16((__le16 *) pSMB->fileName, name,
758 PATH_MAX, cifs_sb->local_nls,
760 name_len++; /* trailing null */
763 name_len = copy_path_name(pSMB->fileName, name);
765 pSMB->SearchAttributes =
766 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM);
767 pSMB->BufferFormat = 0x04;
768 inc_rfc1001_len(pSMB, name_len + 1);
769 pSMB->ByteCount = cpu_to_le16(name_len + 1);
770 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
771 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
772 cifs_stats_inc(&tcon->stats.cifs_stats.num_deletes);
774 cifs_dbg(FYI, "Error in RMFile = %d\n", rc);
776 cifs_buf_release(pSMB);
784 CIFSSMBRmDir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
785 struct cifs_sb_info *cifs_sb)
787 DELETE_DIRECTORY_REQ *pSMB = NULL;
788 DELETE_DIRECTORY_RSP *pSMBr = NULL;
792 int remap = cifs_remap(cifs_sb);
794 cifs_dbg(FYI, "In CIFSSMBRmDir\n");
796 rc = smb_init(SMB_COM_DELETE_DIRECTORY, 0, tcon, (void **) &pSMB,
801 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
802 name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, name,
803 PATH_MAX, cifs_sb->local_nls,
805 name_len++; /* trailing null */
808 name_len = copy_path_name(pSMB->DirName, name);
811 pSMB->BufferFormat = 0x04;
812 inc_rfc1001_len(pSMB, name_len + 1);
813 pSMB->ByteCount = cpu_to_le16(name_len + 1);
814 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
815 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
816 cifs_stats_inc(&tcon->stats.cifs_stats.num_rmdirs);
818 cifs_dbg(FYI, "Error in RMDir = %d\n", rc);
820 cifs_buf_release(pSMB);
827 CIFSSMBMkDir(const unsigned int xid, struct inode *inode, umode_t mode,
828 struct cifs_tcon *tcon, const char *name,
829 struct cifs_sb_info *cifs_sb)
832 CREATE_DIRECTORY_REQ *pSMB = NULL;
833 CREATE_DIRECTORY_RSP *pSMBr = NULL;
836 int remap = cifs_remap(cifs_sb);
838 cifs_dbg(FYI, "In CIFSSMBMkDir\n");
840 rc = smb_init(SMB_COM_CREATE_DIRECTORY, 0, tcon, (void **) &pSMB,
845 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
846 name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, name,
847 PATH_MAX, cifs_sb->local_nls,
849 name_len++; /* trailing null */
852 name_len = copy_path_name(pSMB->DirName, name);
855 pSMB->BufferFormat = 0x04;
856 inc_rfc1001_len(pSMB, name_len + 1);
857 pSMB->ByteCount = cpu_to_le16(name_len + 1);
858 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
859 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
860 cifs_stats_inc(&tcon->stats.cifs_stats.num_mkdirs);
862 cifs_dbg(FYI, "Error in Mkdir = %d\n", rc);
864 cifs_buf_release(pSMB);
871 CIFSPOSIXCreate(const unsigned int xid, struct cifs_tcon *tcon,
872 __u32 posix_flags, __u64 mode, __u16 *netfid,
873 FILE_UNIX_BASIC_INFO *pRetData, __u32 *pOplock,
874 const char *name, const struct nls_table *nls_codepage,
877 TRANSACTION2_SPI_REQ *pSMB = NULL;
878 TRANSACTION2_SPI_RSP *pSMBr = NULL;
881 int bytes_returned = 0;
882 __u16 params, param_offset, offset, byte_count, count;
884 OPEN_PSX_RSP *psx_rsp;
886 cifs_dbg(FYI, "In POSIX Create\n");
888 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
893 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
895 cifsConvertToUTF16((__le16 *) pSMB->FileName, name,
896 PATH_MAX, nls_codepage, remap);
897 name_len++; /* trailing null */
900 name_len = copy_path_name(pSMB->FileName, name);
903 params = 6 + name_len;
904 count = sizeof(OPEN_PSX_REQ);
905 pSMB->MaxParameterCount = cpu_to_le16(2);
906 pSMB->MaxDataCount = cpu_to_le16(1000); /* large enough */
907 pSMB->MaxSetupCount = 0;
912 param_offset = offsetof(struct smb_com_transaction2_spi_req,
913 InformationLevel) - 4;
914 offset = param_offset + params;
915 /* SMB offsets are from the beginning of SMB which is 4 bytes in, after RFC1001 field */
916 pdata = (OPEN_PSX_REQ *)((char *)(pSMB) + offset + 4);
917 pdata->Level = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
918 pdata->Permissions = cpu_to_le64(mode);
919 pdata->PosixOpenFlags = cpu_to_le32(posix_flags);
920 pdata->OpenFlags = cpu_to_le32(*pOplock);
921 pSMB->ParameterOffset = cpu_to_le16(param_offset);
922 pSMB->DataOffset = cpu_to_le16(offset);
923 pSMB->SetupCount = 1;
925 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
926 byte_count = 3 /* pad */ + params + count;
928 pSMB->DataCount = cpu_to_le16(count);
929 pSMB->ParameterCount = cpu_to_le16(params);
930 pSMB->TotalDataCount = pSMB->DataCount;
931 pSMB->TotalParameterCount = pSMB->ParameterCount;
932 pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_OPEN);
934 inc_rfc1001_len(pSMB, byte_count);
935 pSMB->ByteCount = cpu_to_le16(byte_count);
936 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
937 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
939 cifs_dbg(FYI, "Posix create returned %d\n", rc);
943 cifs_dbg(FYI, "copying inode info\n");
944 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
946 if (rc || get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)) {
947 rc = -EIO; /* bad smb */
951 /* copy return information to pRetData */
952 psx_rsp = (OPEN_PSX_RSP *)((char *) &pSMBr->hdr.Protocol
953 + le16_to_cpu(pSMBr->t2.DataOffset));
955 *pOplock = le16_to_cpu(psx_rsp->OplockFlags);
957 *netfid = psx_rsp->Fid; /* cifs fid stays in le */
958 /* Let caller know file was created so we can set the mode. */
959 /* Do we care about the CreateAction in any other cases? */
960 if (cpu_to_le32(FILE_CREATE) == psx_rsp->CreateAction)
961 *pOplock |= CIFS_CREATE_ACTION;
962 /* check to make sure response data is there */
963 if (psx_rsp->ReturnedLevel != cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC)) {
964 pRetData->Type = cpu_to_le32(-1); /* unknown */
965 cifs_dbg(NOISY, "unknown type\n");
967 if (get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)
968 + sizeof(FILE_UNIX_BASIC_INFO)) {
969 cifs_dbg(VFS, "Open response data too small\n");
970 pRetData->Type = cpu_to_le32(-1);
973 memcpy((char *) pRetData,
974 (char *)psx_rsp + sizeof(OPEN_PSX_RSP),
975 sizeof(FILE_UNIX_BASIC_INFO));
979 cifs_buf_release(pSMB);
981 if (posix_flags & SMB_O_DIRECTORY)
982 cifs_stats_inc(&tcon->stats.cifs_stats.num_posixmkdirs);
984 cifs_stats_inc(&tcon->stats.cifs_stats.num_posixopens);
992 static __u16 convert_disposition(int disposition)
996 switch (disposition) {
998 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1001 ofun = SMBOPEN_OAPPEND;
1004 ofun = SMBOPEN_OCREATE;
1007 ofun = SMBOPEN_OCREATE | SMBOPEN_OAPPEND;
1009 case FILE_OVERWRITE:
1010 ofun = SMBOPEN_OTRUNC;
1012 case FILE_OVERWRITE_IF:
1013 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1016 cifs_dbg(FYI, "unknown disposition %d\n", disposition);
1017 ofun = SMBOPEN_OAPPEND; /* regular open */
1023 access_flags_to_smbopen_mode(const int access_flags)
1025 int masked_flags = access_flags & (GENERIC_READ | GENERIC_WRITE);
1027 if (masked_flags == GENERIC_READ)
1028 return SMBOPEN_READ;
1029 else if (masked_flags == GENERIC_WRITE)
1030 return SMBOPEN_WRITE;
1032 /* just go for read/write */
1033 return SMBOPEN_READWRITE;
1037 SMBLegacyOpen(const unsigned int xid, struct cifs_tcon *tcon,
1038 const char *fileName, const int openDisposition,
1039 const int access_flags, const int create_options, __u16 *netfid,
1040 int *pOplock, FILE_ALL_INFO *pfile_info,
1041 const struct nls_table *nls_codepage, int remap)
1044 OPENX_REQ *pSMB = NULL;
1045 OPENX_RSP *pSMBr = NULL;
1051 rc = smb_init(SMB_COM_OPEN_ANDX, 15, tcon, (void **) &pSMB,
1056 pSMB->AndXCommand = 0xFF; /* none */
1058 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1059 count = 1; /* account for one byte pad to word boundary */
1061 cifsConvertToUTF16((__le16 *) (pSMB->fileName + 1),
1062 fileName, PATH_MAX, nls_codepage, remap);
1063 name_len++; /* trailing null */
1066 count = 0; /* no pad */
1067 name_len = copy_path_name(pSMB->fileName, fileName);
1069 if (*pOplock & REQ_OPLOCK)
1070 pSMB->OpenFlags = cpu_to_le16(REQ_OPLOCK);
1071 else if (*pOplock & REQ_BATCHOPLOCK)
1072 pSMB->OpenFlags = cpu_to_le16(REQ_BATCHOPLOCK);
1074 pSMB->OpenFlags |= cpu_to_le16(REQ_MORE_INFO);
1075 pSMB->Mode = cpu_to_le16(access_flags_to_smbopen_mode(access_flags));
1076 pSMB->Mode |= cpu_to_le16(0x40); /* deny none */
1077 /* set file as system file if special file such
1078 as fifo and server expecting SFU style and
1079 no Unix extensions */
1081 if (create_options & CREATE_OPTION_SPECIAL)
1082 pSMB->FileAttributes = cpu_to_le16(ATTR_SYSTEM);
1083 else /* BB FIXME BB */
1084 pSMB->FileAttributes = cpu_to_le16(0/*ATTR_NORMAL*/);
1086 if (create_options & CREATE_OPTION_READONLY)
1087 pSMB->FileAttributes |= cpu_to_le16(ATTR_READONLY);
1090 /* pSMB->CreateOptions = cpu_to_le32(create_options &
1091 CREATE_OPTIONS_MASK); */
1092 /* BB FIXME END BB */
1094 pSMB->Sattr = cpu_to_le16(ATTR_HIDDEN | ATTR_SYSTEM | ATTR_DIRECTORY);
1095 pSMB->OpenFunction = cpu_to_le16(convert_disposition(openDisposition));
1097 inc_rfc1001_len(pSMB, count);
1099 pSMB->ByteCount = cpu_to_le16(count);
1100 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1101 (struct smb_hdr *)pSMBr, &bytes_returned, 0);
1102 cifs_stats_inc(&tcon->stats.cifs_stats.num_opens);
1104 cifs_dbg(FYI, "Error in Open = %d\n", rc);
1106 /* BB verify if wct == 15 */
1108 /* *pOplock = pSMBr->OplockLevel; */ /* BB take from action field*/
1110 *netfid = pSMBr->Fid; /* cifs fid stays in le */
1111 /* Let caller know file was created so we can set the mode. */
1112 /* Do we care about the CreateAction in any other cases? */
1114 /* if (cpu_to_le32(FILE_CREATE) == pSMBr->CreateAction)
1115 *pOplock |= CIFS_CREATE_ACTION; */
1119 pfile_info->CreationTime = 0; /* BB convert CreateTime*/
1120 pfile_info->LastAccessTime = 0; /* BB fixme */
1121 pfile_info->LastWriteTime = 0; /* BB fixme */
1122 pfile_info->ChangeTime = 0; /* BB fixme */
1123 pfile_info->Attributes =
1124 cpu_to_le32(le16_to_cpu(pSMBr->FileAttributes));
1125 /* the file_info buf is endian converted by caller */
1126 pfile_info->AllocationSize =
1127 cpu_to_le64(le32_to_cpu(pSMBr->EndOfFile));
1128 pfile_info->EndOfFile = pfile_info->AllocationSize;
1129 pfile_info->NumberOfLinks = cpu_to_le32(1);
1130 pfile_info->DeletePending = 0;
1134 cifs_buf_release(pSMB);
1141 CIFS_open(const unsigned int xid, struct cifs_open_parms *oparms, int *oplock,
1145 OPEN_REQ *req = NULL;
1146 OPEN_RSP *rsp = NULL;
1150 struct cifs_sb_info *cifs_sb = oparms->cifs_sb;
1151 struct cifs_tcon *tcon = oparms->tcon;
1152 int remap = cifs_remap(cifs_sb);
1153 const struct nls_table *nls = cifs_sb->local_nls;
1154 int create_options = oparms->create_options;
1155 int desired_access = oparms->desired_access;
1156 int disposition = oparms->disposition;
1157 const char *path = oparms->path;
1160 rc = smb_init(SMB_COM_NT_CREATE_ANDX, 24, tcon, (void **)&req,
1165 /* no commands go after this */
1166 req->AndXCommand = 0xFF;
1168 if (req->hdr.Flags2 & SMBFLG2_UNICODE) {
1169 /* account for one byte pad to word boundary */
1171 name_len = cifsConvertToUTF16((__le16 *)(req->fileName + 1),
1172 path, PATH_MAX, nls, remap);
1176 req->NameLength = cpu_to_le16(name_len);
1178 /* BB improve check for buffer overruns BB */
1181 name_len = copy_path_name(req->fileName, path);
1182 req->NameLength = cpu_to_le16(name_len);
1185 if (*oplock & REQ_OPLOCK)
1186 req->OpenFlags = cpu_to_le32(REQ_OPLOCK);
1187 else if (*oplock & REQ_BATCHOPLOCK)
1188 req->OpenFlags = cpu_to_le32(REQ_BATCHOPLOCK);
1190 req->DesiredAccess = cpu_to_le32(desired_access);
1191 req->AllocationSize = 0;
1194 * Set file as system file if special file such as fifo and server
1195 * expecting SFU style and no Unix extensions.
1197 if (create_options & CREATE_OPTION_SPECIAL)
1198 req->FileAttributes = cpu_to_le32(ATTR_SYSTEM);
1200 req->FileAttributes = cpu_to_le32(ATTR_NORMAL);
1203 * XP does not handle ATTR_POSIX_SEMANTICS but it helps speed up case
1204 * sensitive checks for other servers such as Samba.
1206 if (tcon->ses->capabilities & CAP_UNIX)
1207 req->FileAttributes |= cpu_to_le32(ATTR_POSIX_SEMANTICS);
1209 if (create_options & CREATE_OPTION_READONLY)
1210 req->FileAttributes |= cpu_to_le32(ATTR_READONLY);
1212 req->ShareAccess = cpu_to_le32(FILE_SHARE_ALL);
1213 req->CreateDisposition = cpu_to_le32(disposition);
1214 req->CreateOptions = cpu_to_le32(create_options & CREATE_OPTIONS_MASK);
1216 /* BB Expirement with various impersonation levels and verify */
1217 req->ImpersonationLevel = cpu_to_le32(SECURITY_IMPERSONATION);
1218 req->SecurityFlags = SECURITY_CONTEXT_TRACKING|SECURITY_EFFECTIVE_ONLY;
1221 inc_rfc1001_len(req, count);
1223 req->ByteCount = cpu_to_le16(count);
1224 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *)req,
1225 (struct smb_hdr *)rsp, &bytes_returned, 0);
1226 cifs_stats_inc(&tcon->stats.cifs_stats.num_opens);
1228 cifs_dbg(FYI, "Error in Open = %d\n", rc);
1229 cifs_buf_release(req);
1235 /* 1 byte no need to le_to_cpu */
1236 *oplock = rsp->OplockLevel;
1237 /* cifs fid stays in le */
1238 oparms->fid->netfid = rsp->Fid;
1239 oparms->fid->access = desired_access;
1241 /* Let caller know file was created so we can set the mode. */
1242 /* Do we care about the CreateAction in any other cases? */
1243 if (cpu_to_le32(FILE_CREATE) == rsp->CreateAction)
1244 *oplock |= CIFS_CREATE_ACTION;
1247 /* copy from CreationTime to Attributes */
1248 memcpy((char *)buf, (char *)&rsp->CreationTime, 36);
1249 /* the file_info buf is endian converted by caller */
1250 buf->AllocationSize = rsp->AllocationSize;
1251 buf->EndOfFile = rsp->EndOfFile;
1252 buf->NumberOfLinks = cpu_to_le32(1);
1253 buf->DeletePending = 0;
1256 cifs_buf_release(req);
1261 cifs_readv_callback(struct mid_q_entry *mid)
1263 struct cifs_readdata *rdata = mid->callback_data;
1264 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
1265 struct TCP_Server_Info *server = tcon->ses->server;
1266 struct smb_rqst rqst = { .rq_iov = rdata->iov,
1268 .rq_iter_size = iov_iter_count(&rdata->iter),
1269 .rq_iter = rdata->iter };
1270 struct cifs_credits credits = { .value = 1, .instance = 0 };
1272 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
1273 __func__, mid->mid, mid->mid_state, rdata->result,
1276 switch (mid->mid_state) {
1277 case MID_RESPONSE_RECEIVED:
1278 /* result already set, check signature */
1282 rc = cifs_verify_signature(&rqst, server,
1283 mid->sequence_number);
1285 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
1288 /* FIXME: should this be counted toward the initiating task? */
1289 task_io_account_read(rdata->got_bytes);
1290 cifs_stats_bytes_read(tcon, rdata->got_bytes);
1292 case MID_REQUEST_SUBMITTED:
1293 case MID_RETRY_NEEDED:
1294 rdata->result = -EAGAIN;
1295 if (server->sign && rdata->got_bytes)
1296 /* reset bytes number since we can not check a sign */
1297 rdata->got_bytes = 0;
1298 /* FIXME: should this be counted toward the initiating task? */
1299 task_io_account_read(rdata->got_bytes);
1300 cifs_stats_bytes_read(tcon, rdata->got_bytes);
1303 rdata->result = -EIO;
1306 queue_work(cifsiod_wq, &rdata->work);
1308 add_credits(server, &credits, 0);
1311 /* cifs_async_readv - send an async write, and set up mid to handle result */
1313 cifs_async_readv(struct cifs_readdata *rdata)
1316 READ_REQ *smb = NULL;
1318 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
1319 struct smb_rqst rqst = { .rq_iov = rdata->iov,
1322 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
1323 __func__, rdata->offset, rdata->bytes);
1325 if (tcon->ses->capabilities & CAP_LARGE_FILES)
1328 wct = 10; /* old style read */
1329 if ((rdata->offset >> 32) > 0) {
1330 /* can not handle this big offset for old */
1335 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **)&smb);
1339 smb->hdr.Pid = cpu_to_le16((__u16)rdata->pid);
1340 smb->hdr.PidHigh = cpu_to_le16((__u16)(rdata->pid >> 16));
1342 smb->AndXCommand = 0xFF; /* none */
1343 smb->Fid = rdata->cfile->fid.netfid;
1344 smb->OffsetLow = cpu_to_le32(rdata->offset & 0xFFFFFFFF);
1346 smb->OffsetHigh = cpu_to_le32(rdata->offset >> 32);
1348 smb->MaxCount = cpu_to_le16(rdata->bytes & 0xFFFF);
1349 smb->MaxCountHigh = cpu_to_le32(rdata->bytes >> 16);
1353 /* old style read */
1354 struct smb_com_readx_req *smbr =
1355 (struct smb_com_readx_req *)smb;
1356 smbr->ByteCount = 0;
1359 /* 4 for RFC1001 length + 1 for BCC */
1360 rdata->iov[0].iov_base = smb;
1361 rdata->iov[0].iov_len = 4;
1362 rdata->iov[1].iov_base = (char *)smb + 4;
1363 rdata->iov[1].iov_len = get_rfc1002_length(smb);
1365 kref_get(&rdata->refcount);
1366 rc = cifs_call_async(tcon->ses->server, &rqst, cifs_readv_receive,
1367 cifs_readv_callback, NULL, rdata, 0, NULL);
1370 cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
1372 kref_put(&rdata->refcount, cifs_readdata_release);
1374 cifs_small_buf_release(smb);
1379 CIFSSMBRead(const unsigned int xid, struct cifs_io_parms *io_parms,
1380 unsigned int *nbytes, char **buf, int *pbuf_type)
1383 READ_REQ *pSMB = NULL;
1384 READ_RSP *pSMBr = NULL;
1385 char *pReadData = NULL;
1387 int resp_buf_type = 0;
1389 struct kvec rsp_iov;
1390 __u32 pid = io_parms->pid;
1391 __u16 netfid = io_parms->netfid;
1392 __u64 offset = io_parms->offset;
1393 struct cifs_tcon *tcon = io_parms->tcon;
1394 unsigned int count = io_parms->length;
1396 cifs_dbg(FYI, "Reading %d bytes on fid %d\n", count, netfid);
1397 if (tcon->ses->capabilities & CAP_LARGE_FILES)
1400 wct = 10; /* old style read */
1401 if ((offset >> 32) > 0) {
1402 /* can not handle this big offset for old */
1408 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **) &pSMB);
1412 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1413 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1415 /* tcon and ses pointer are checked in smb_init */
1416 if (tcon->ses->server == NULL)
1417 return -ECONNABORTED;
1419 pSMB->AndXCommand = 0xFF; /* none */
1421 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
1423 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
1425 pSMB->Remaining = 0;
1426 pSMB->MaxCount = cpu_to_le16(count & 0xFFFF);
1427 pSMB->MaxCountHigh = cpu_to_le32(count >> 16);
1429 pSMB->ByteCount = 0; /* no need to do le conversion since 0 */
1431 /* old style read */
1432 struct smb_com_readx_req *pSMBW =
1433 (struct smb_com_readx_req *)pSMB;
1434 pSMBW->ByteCount = 0;
1437 iov[0].iov_base = (char *)pSMB;
1438 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
1439 rc = SendReceive2(xid, tcon->ses, iov, 1, &resp_buf_type,
1440 CIFS_LOG_ERROR, &rsp_iov);
1441 cifs_small_buf_release(pSMB);
1442 cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
1443 pSMBr = (READ_RSP *)rsp_iov.iov_base;
1445 cifs_dbg(VFS, "Send error in read = %d\n", rc);
1447 int data_length = le16_to_cpu(pSMBr->DataLengthHigh);
1448 data_length = data_length << 16;
1449 data_length += le16_to_cpu(pSMBr->DataLength);
1450 *nbytes = data_length;
1452 /*check that DataLength would not go beyond end of SMB */
1453 if ((data_length > CIFSMaxBufSize)
1454 || (data_length > count)) {
1455 cifs_dbg(FYI, "bad length %d for count %d\n",
1456 data_length, count);
1460 pReadData = (char *) (&pSMBr->hdr.Protocol) +
1461 le16_to_cpu(pSMBr->DataOffset);
1462 /* if (rc = copy_to_user(buf, pReadData, data_length)) {
1463 cifs_dbg(VFS, "Faulting on read rc = %d\n",rc);
1465 }*/ /* can not use copy_to_user when using page cache*/
1467 memcpy(*buf, pReadData, data_length);
1472 free_rsp_buf(resp_buf_type, rsp_iov.iov_base);
1473 } else if (resp_buf_type != CIFS_NO_BUFFER) {
1474 /* return buffer to caller to free */
1475 *buf = rsp_iov.iov_base;
1476 if (resp_buf_type == CIFS_SMALL_BUFFER)
1477 *pbuf_type = CIFS_SMALL_BUFFER;
1478 else if (resp_buf_type == CIFS_LARGE_BUFFER)
1479 *pbuf_type = CIFS_LARGE_BUFFER;
1480 } /* else no valid buffer on return - leave as null */
1482 /* Note: On -EAGAIN error only caller can retry on handle based calls
1483 since file handle passed in no longer valid */
1489 CIFSSMBWrite(const unsigned int xid, struct cifs_io_parms *io_parms,
1490 unsigned int *nbytes, const char *buf)
1493 WRITE_REQ *pSMB = NULL;
1494 WRITE_RSP *pSMBr = NULL;
1495 int bytes_returned, wct;
1498 __u32 pid = io_parms->pid;
1499 __u16 netfid = io_parms->netfid;
1500 __u64 offset = io_parms->offset;
1501 struct cifs_tcon *tcon = io_parms->tcon;
1502 unsigned int count = io_parms->length;
1506 /* cifs_dbg(FYI, "write at %lld %d bytes\n", offset, count);*/
1507 if (tcon->ses == NULL)
1508 return -ECONNABORTED;
1510 if (tcon->ses->capabilities & CAP_LARGE_FILES)
1514 if ((offset >> 32) > 0) {
1515 /* can not handle big offset for old srv */
1520 rc = smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB,
1525 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1526 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1528 /* tcon and ses pointer are checked in smb_init */
1529 if (tcon->ses->server == NULL)
1530 return -ECONNABORTED;
1532 pSMB->AndXCommand = 0xFF; /* none */
1534 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
1536 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
1538 pSMB->Reserved = 0xFFFFFFFF;
1539 pSMB->WriteMode = 0;
1540 pSMB->Remaining = 0;
1542 /* Can increase buffer size if buffer is big enough in some cases ie we
1543 can send more if LARGE_WRITE_X capability returned by the server and if
1544 our buffer is big enough or if we convert to iovecs on socket writes
1545 and eliminate the copy to the CIFS buffer */
1546 if (tcon->ses->capabilities & CAP_LARGE_WRITE_X) {
1547 bytes_sent = min_t(const unsigned int, CIFSMaxBufSize, count);
1549 bytes_sent = (tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE)
1553 if (bytes_sent > count)
1556 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
1558 memcpy(pSMB->Data, buf, bytes_sent);
1559 else if (count != 0) {
1561 cifs_buf_release(pSMB);
1563 } /* else setting file size with write of zero bytes */
1565 byte_count = bytes_sent + 1; /* pad */
1566 else /* wct == 12 */
1567 byte_count = bytes_sent + 5; /* bigger pad, smaller smb hdr */
1569 pSMB->DataLengthLow = cpu_to_le16(bytes_sent & 0xFFFF);
1570 pSMB->DataLengthHigh = cpu_to_le16(bytes_sent >> 16);
1571 inc_rfc1001_len(pSMB, byte_count);
1574 pSMB->ByteCount = cpu_to_le16(byte_count);
1575 else { /* old style write has byte count 4 bytes earlier
1577 struct smb_com_writex_req *pSMBW =
1578 (struct smb_com_writex_req *)pSMB;
1579 pSMBW->ByteCount = cpu_to_le16(byte_count);
1582 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1583 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
1584 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
1586 cifs_dbg(FYI, "Send error in write = %d\n", rc);
1588 *nbytes = le16_to_cpu(pSMBr->CountHigh);
1589 *nbytes = (*nbytes) << 16;
1590 *nbytes += le16_to_cpu(pSMBr->Count);
1593 * Mask off high 16 bits when bytes written as returned by the
1594 * server is greater than bytes requested by the client. Some
1595 * OS/2 servers are known to set incorrect CountHigh values.
1597 if (*nbytes > count)
1601 cifs_buf_release(pSMB);
1603 /* Note: On -EAGAIN error only caller can retry on handle based calls
1604 since file handle passed in no longer valid */
1610 * Check the mid_state and signature on received buffer (if any), and queue the
1611 * workqueue completion task.
1614 cifs_writev_callback(struct mid_q_entry *mid)
1616 struct cifs_writedata *wdata = mid->callback_data;
1617 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
1618 unsigned int written;
1619 WRITE_RSP *smb = (WRITE_RSP *)mid->resp_buf;
1620 struct cifs_credits credits = { .value = 1, .instance = 0 };
1622 switch (mid->mid_state) {
1623 case MID_RESPONSE_RECEIVED:
1624 wdata->result = cifs_check_receive(mid, tcon->ses->server, 0);
1625 if (wdata->result != 0)
1628 written = le16_to_cpu(smb->CountHigh);
1630 written += le16_to_cpu(smb->Count);
1632 * Mask off high 16 bits when bytes written as returned
1633 * by the server is greater than bytes requested by the
1634 * client. OS/2 servers are known to set incorrect
1637 if (written > wdata->bytes)
1640 if (written < wdata->bytes)
1641 wdata->result = -ENOSPC;
1643 wdata->bytes = written;
1645 case MID_REQUEST_SUBMITTED:
1646 case MID_RETRY_NEEDED:
1647 wdata->result = -EAGAIN;
1650 wdata->result = -EIO;
1654 queue_work(cifsiod_wq, &wdata->work);
1656 add_credits(tcon->ses->server, &credits, 0);
1659 /* cifs_async_writev - send an async write, and set up mid to handle result */
1661 cifs_async_writev(struct cifs_writedata *wdata,
1662 void (*release)(struct kref *kref))
1665 WRITE_REQ *smb = NULL;
1667 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
1669 struct smb_rqst rqst = { };
1671 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
1675 if (wdata->offset >> 32 > 0) {
1676 /* can not handle big offset for old srv */
1681 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **)&smb);
1683 goto async_writev_out;
1685 smb->hdr.Pid = cpu_to_le16((__u16)wdata->pid);
1686 smb->hdr.PidHigh = cpu_to_le16((__u16)(wdata->pid >> 16));
1688 smb->AndXCommand = 0xFF; /* none */
1689 smb->Fid = wdata->cfile->fid.netfid;
1690 smb->OffsetLow = cpu_to_le32(wdata->offset & 0xFFFFFFFF);
1692 smb->OffsetHigh = cpu_to_le32(wdata->offset >> 32);
1693 smb->Reserved = 0xFFFFFFFF;
1698 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
1700 /* 4 for RFC1001 length + 1 for BCC */
1702 iov[0].iov_base = smb;
1703 iov[1].iov_len = get_rfc1002_length(smb) + 1;
1704 iov[1].iov_base = (char *)smb + 4;
1708 rqst.rq_iter = wdata->iter;
1709 rqst.rq_iter_size = iov_iter_count(&wdata->iter);
1711 cifs_dbg(FYI, "async write at %llu %u bytes\n",
1712 wdata->offset, wdata->bytes);
1714 smb->DataLengthLow = cpu_to_le16(wdata->bytes & 0xFFFF);
1715 smb->DataLengthHigh = cpu_to_le16(wdata->bytes >> 16);
1718 inc_rfc1001_len(&smb->hdr, wdata->bytes + 1);
1719 put_bcc(wdata->bytes + 1, &smb->hdr);
1722 struct smb_com_writex_req *smbw =
1723 (struct smb_com_writex_req *)smb;
1724 inc_rfc1001_len(&smbw->hdr, wdata->bytes + 5);
1725 put_bcc(wdata->bytes + 5, &smbw->hdr);
1726 iov[1].iov_len += 4; /* pad bigger by four bytes */
1729 kref_get(&wdata->refcount);
1730 rc = cifs_call_async(tcon->ses->server, &rqst, NULL,
1731 cifs_writev_callback, NULL, wdata, 0, NULL);
1734 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
1736 kref_put(&wdata->refcount, release);
1739 cifs_small_buf_release(smb);
1744 CIFSSMBWrite2(const unsigned int xid, struct cifs_io_parms *io_parms,
1745 unsigned int *nbytes, struct kvec *iov, int n_vec)
1748 WRITE_REQ *pSMB = NULL;
1751 int resp_buf_type = 0;
1752 __u32 pid = io_parms->pid;
1753 __u16 netfid = io_parms->netfid;
1754 __u64 offset = io_parms->offset;
1755 struct cifs_tcon *tcon = io_parms->tcon;
1756 unsigned int count = io_parms->length;
1757 struct kvec rsp_iov;
1761 cifs_dbg(FYI, "write2 at %lld %d bytes\n", (long long)offset, count);
1763 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
1767 if ((offset >> 32) > 0) {
1768 /* can not handle big offset for old srv */
1772 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB);
1776 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1777 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1779 /* tcon and ses pointer are checked in smb_init */
1780 if (tcon->ses->server == NULL)
1781 return -ECONNABORTED;
1783 pSMB->AndXCommand = 0xFF; /* none */
1785 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
1787 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
1788 pSMB->Reserved = 0xFFFFFFFF;
1789 pSMB->WriteMode = 0;
1790 pSMB->Remaining = 0;
1793 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
1795 pSMB->DataLengthLow = cpu_to_le16(count & 0xFFFF);
1796 pSMB->DataLengthHigh = cpu_to_le16(count >> 16);
1797 /* header + 1 byte pad */
1798 smb_hdr_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 1;
1800 inc_rfc1001_len(pSMB, count + 1);
1801 else /* wct == 12 */
1802 inc_rfc1001_len(pSMB, count + 5); /* smb data starts later */
1804 pSMB->ByteCount = cpu_to_le16(count + 1);
1805 else /* wct == 12 */ /* bigger pad, smaller smb hdr, keep offset ok */ {
1806 struct smb_com_writex_req *pSMBW =
1807 (struct smb_com_writex_req *)pSMB;
1808 pSMBW->ByteCount = cpu_to_le16(count + 5);
1810 iov[0].iov_base = pSMB;
1812 iov[0].iov_len = smb_hdr_len + 4;
1813 else /* wct == 12 pad bigger by four bytes */
1814 iov[0].iov_len = smb_hdr_len + 8;
1816 rc = SendReceive2(xid, tcon->ses, iov, n_vec + 1, &resp_buf_type, 0,
1818 cifs_small_buf_release(pSMB);
1819 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
1821 cifs_dbg(FYI, "Send error Write2 = %d\n", rc);
1822 } else if (resp_buf_type == 0) {
1823 /* presumably this can not happen, but best to be safe */
1826 WRITE_RSP *pSMBr = (WRITE_RSP *)rsp_iov.iov_base;
1827 *nbytes = le16_to_cpu(pSMBr->CountHigh);
1828 *nbytes = (*nbytes) << 16;
1829 *nbytes += le16_to_cpu(pSMBr->Count);
1832 * Mask off high 16 bits when bytes written as returned by the
1833 * server is greater than bytes requested by the client. OS/2
1834 * servers are known to set incorrect CountHigh values.
1836 if (*nbytes > count)
1840 free_rsp_buf(resp_buf_type, rsp_iov.iov_base);
1842 /* Note: On -EAGAIN error only caller can retry on handle based calls
1843 since file handle passed in no longer valid */
1848 int cifs_lockv(const unsigned int xid, struct cifs_tcon *tcon,
1849 const __u16 netfid, const __u8 lock_type, const __u32 num_unlock,
1850 const __u32 num_lock, LOCKING_ANDX_RANGE *buf)
1853 LOCK_REQ *pSMB = NULL;
1855 struct kvec rsp_iov;
1859 cifs_dbg(FYI, "cifs_lockv num lock %d num unlock %d\n",
1860 num_lock, num_unlock);
1862 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
1867 pSMB->NumberOfLocks = cpu_to_le16(num_lock);
1868 pSMB->NumberOfUnlocks = cpu_to_le16(num_unlock);
1869 pSMB->LockType = lock_type;
1870 pSMB->AndXCommand = 0xFF; /* none */
1871 pSMB->Fid = netfid; /* netfid stays le */
1873 count = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
1874 inc_rfc1001_len(pSMB, count);
1875 pSMB->ByteCount = cpu_to_le16(count);
1877 iov[0].iov_base = (char *)pSMB;
1878 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4 -
1879 (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
1880 iov[1].iov_base = (char *)buf;
1881 iov[1].iov_len = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
1883 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
1884 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type,
1885 CIFS_NO_RSP_BUF, &rsp_iov);
1886 cifs_small_buf_release(pSMB);
1888 cifs_dbg(FYI, "Send error in cifs_lockv = %d\n", rc);
1894 CIFSSMBLock(const unsigned int xid, struct cifs_tcon *tcon,
1895 const __u16 smb_file_id, const __u32 netpid, const __u64 len,
1896 const __u64 offset, const __u32 numUnlock,
1897 const __u32 numLock, const __u8 lockType,
1898 const bool waitFlag, const __u8 oplock_level)
1901 LOCK_REQ *pSMB = NULL;
1902 /* LOCK_RSP *pSMBr = NULL; */ /* No response data other than rc to parse */
1907 cifs_dbg(FYI, "CIFSSMBLock timeout %d numLock %d\n",
1908 (int)waitFlag, numLock);
1909 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
1914 if (lockType == LOCKING_ANDX_OPLOCK_RELEASE) {
1915 /* no response expected */
1916 flags = CIFS_NO_SRV_RSP | CIFS_NON_BLOCKING | CIFS_OBREAK_OP;
1918 } else if (waitFlag) {
1919 flags = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
1920 pSMB->Timeout = cpu_to_le32(-1);/* blocking - do not time out */
1925 pSMB->NumberOfLocks = cpu_to_le16(numLock);
1926 pSMB->NumberOfUnlocks = cpu_to_le16(numUnlock);
1927 pSMB->LockType = lockType;
1928 pSMB->OplockLevel = oplock_level;
1929 pSMB->AndXCommand = 0xFF; /* none */
1930 pSMB->Fid = smb_file_id; /* netfid stays le */
1932 if ((numLock != 0) || (numUnlock != 0)) {
1933 pSMB->Locks[0].Pid = cpu_to_le16(netpid);
1934 /* BB where to store pid high? */
1935 pSMB->Locks[0].LengthLow = cpu_to_le32((u32)len);
1936 pSMB->Locks[0].LengthHigh = cpu_to_le32((u32)(len>>32));
1937 pSMB->Locks[0].OffsetLow = cpu_to_le32((u32)offset);
1938 pSMB->Locks[0].OffsetHigh = cpu_to_le32((u32)(offset>>32));
1939 count = sizeof(LOCKING_ANDX_RANGE);
1944 inc_rfc1001_len(pSMB, count);
1945 pSMB->ByteCount = cpu_to_le16(count);
1948 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
1949 (struct smb_hdr *) pSMB, &bytes_returned);
1951 rc = SendReceiveNoRsp(xid, tcon->ses, (char *)pSMB, flags);
1952 cifs_small_buf_release(pSMB);
1953 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
1955 cifs_dbg(FYI, "Send error in Lock = %d\n", rc);
1957 /* Note: On -EAGAIN error only caller can retry on handle based calls
1958 since file handle passed in no longer valid */
1963 CIFSSMBPosixLock(const unsigned int xid, struct cifs_tcon *tcon,
1964 const __u16 smb_file_id, const __u32 netpid,
1965 const loff_t start_offset, const __u64 len,
1966 struct file_lock *pLockData, const __u16 lock_type,
1967 const bool waitFlag)
1969 struct smb_com_transaction2_sfi_req *pSMB = NULL;
1970 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
1971 struct cifs_posix_lock *parm_data;
1974 int bytes_returned = 0;
1975 int resp_buf_type = 0;
1976 __u16 params, param_offset, offset, byte_count, count;
1978 struct kvec rsp_iov;
1980 cifs_dbg(FYI, "Posix Lock\n");
1982 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
1987 pSMBr = (struct smb_com_transaction2_sfi_rsp *)pSMB;
1990 pSMB->MaxSetupCount = 0;
1993 pSMB->Reserved2 = 0;
1994 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
1995 offset = param_offset + params;
1997 count = sizeof(struct cifs_posix_lock);
1998 pSMB->MaxParameterCount = cpu_to_le16(2);
1999 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
2000 pSMB->SetupCount = 1;
2001 pSMB->Reserved3 = 0;
2003 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
2005 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2006 byte_count = 3 /* pad */ + params + count;
2007 pSMB->DataCount = cpu_to_le16(count);
2008 pSMB->ParameterCount = cpu_to_le16(params);
2009 pSMB->TotalDataCount = pSMB->DataCount;
2010 pSMB->TotalParameterCount = pSMB->ParameterCount;
2011 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2012 /* SMB offsets are from the beginning of SMB which is 4 bytes in, after RFC1001 field */
2013 parm_data = (struct cifs_posix_lock *)
2014 (((char *)pSMB) + offset + 4);
2016 parm_data->lock_type = cpu_to_le16(lock_type);
2018 timeout = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
2019 parm_data->lock_flags = cpu_to_le16(1);
2020 pSMB->Timeout = cpu_to_le32(-1);
2024 parm_data->pid = cpu_to_le32(netpid);
2025 parm_data->start = cpu_to_le64(start_offset);
2026 parm_data->length = cpu_to_le64(len); /* normalize negative numbers */
2028 pSMB->DataOffset = cpu_to_le16(offset);
2029 pSMB->Fid = smb_file_id;
2030 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_LOCK);
2031 pSMB->Reserved4 = 0;
2032 inc_rfc1001_len(pSMB, byte_count);
2033 pSMB->ByteCount = cpu_to_le16(byte_count);
2035 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
2036 (struct smb_hdr *) pSMBr, &bytes_returned);
2038 iov[0].iov_base = (char *)pSMB;
2039 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
2040 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
2041 &resp_buf_type, timeout, &rsp_iov);
2042 pSMBr = (struct smb_com_transaction2_sfi_rsp *)rsp_iov.iov_base;
2044 cifs_small_buf_release(pSMB);
2047 cifs_dbg(FYI, "Send error in Posix Lock = %d\n", rc);
2048 } else if (pLockData) {
2049 /* lock structure can be returned on get */
2052 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
2054 if (rc || get_bcc(&pSMBr->hdr) < sizeof(*parm_data)) {
2055 rc = -EIO; /* bad smb */
2058 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
2059 data_count = le16_to_cpu(pSMBr->t2.DataCount);
2060 if (data_count < sizeof(struct cifs_posix_lock)) {
2064 parm_data = (struct cifs_posix_lock *)
2065 ((char *)&pSMBr->hdr.Protocol + data_offset);
2066 if (parm_data->lock_type == cpu_to_le16(CIFS_UNLCK))
2067 pLockData->fl_type = F_UNLCK;
2069 if (parm_data->lock_type ==
2070 cpu_to_le16(CIFS_RDLCK))
2071 pLockData->fl_type = F_RDLCK;
2072 else if (parm_data->lock_type ==
2073 cpu_to_le16(CIFS_WRLCK))
2074 pLockData->fl_type = F_WRLCK;
2076 pLockData->fl_start = le64_to_cpu(parm_data->start);
2077 pLockData->fl_end = pLockData->fl_start +
2078 (le64_to_cpu(parm_data->length) ?
2079 le64_to_cpu(parm_data->length) - 1 : 0);
2080 pLockData->fl_pid = -le32_to_cpu(parm_data->pid);
2085 free_rsp_buf(resp_buf_type, rsp_iov.iov_base);
2087 /* Note: On -EAGAIN error only caller can retry on handle based calls
2088 since file handle passed in no longer valid */
2095 CIFSSMBClose(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
2098 CLOSE_REQ *pSMB = NULL;
2099 cifs_dbg(FYI, "In CIFSSMBClose\n");
2101 /* do not retry on dead session on close */
2102 rc = small_smb_init(SMB_COM_CLOSE, 3, tcon, (void **) &pSMB);
2108 pSMB->FileID = (__u16) smb_file_id;
2109 pSMB->LastWriteTime = 0xFFFFFFFF;
2110 pSMB->ByteCount = 0;
2111 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
2112 cifs_small_buf_release(pSMB);
2113 cifs_stats_inc(&tcon->stats.cifs_stats.num_closes);
2116 /* EINTR is expected when user ctl-c to kill app */
2117 cifs_dbg(VFS, "Send error in Close = %d\n", rc);
2121 /* Since session is dead, file will be closed on server already */
2129 CIFSSMBFlush(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
2132 FLUSH_REQ *pSMB = NULL;
2133 cifs_dbg(FYI, "In CIFSSMBFlush\n");
2135 rc = small_smb_init(SMB_COM_FLUSH, 1, tcon, (void **) &pSMB);
2139 pSMB->FileID = (__u16) smb_file_id;
2140 pSMB->ByteCount = 0;
2141 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
2142 cifs_small_buf_release(pSMB);
2143 cifs_stats_inc(&tcon->stats.cifs_stats.num_flushes);
2145 cifs_dbg(VFS, "Send error in Flush = %d\n", rc);
2151 CIFSSMBRename(const unsigned int xid, struct cifs_tcon *tcon,
2152 const char *from_name, const char *to_name,
2153 struct cifs_sb_info *cifs_sb)
2156 RENAME_REQ *pSMB = NULL;
2157 RENAME_RSP *pSMBr = NULL;
2159 int name_len, name_len2;
2161 int remap = cifs_remap(cifs_sb);
2163 cifs_dbg(FYI, "In CIFSSMBRename\n");
2165 rc = smb_init(SMB_COM_RENAME, 1, tcon, (void **) &pSMB,
2170 pSMB->BufferFormat = 0x04;
2171 pSMB->SearchAttributes =
2172 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
2175 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2176 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2177 from_name, PATH_MAX,
2178 cifs_sb->local_nls, remap);
2179 name_len++; /* trailing null */
2181 pSMB->OldFileName[name_len] = 0x04; /* pad */
2182 /* protocol requires ASCII signature byte on Unicode string */
2183 pSMB->OldFileName[name_len + 1] = 0x00;
2185 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2186 to_name, PATH_MAX, cifs_sb->local_nls,
2188 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2189 name_len2 *= 2; /* convert to bytes */
2191 name_len = copy_path_name(pSMB->OldFileName, from_name);
2192 name_len2 = copy_path_name(pSMB->OldFileName+name_len+1, to_name);
2193 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2194 name_len2++; /* signature byte */
2197 count = 1 /* 1st signature byte */ + name_len + name_len2;
2198 inc_rfc1001_len(pSMB, count);
2199 pSMB->ByteCount = cpu_to_le16(count);
2201 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2202 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2203 cifs_stats_inc(&tcon->stats.cifs_stats.num_renames);
2205 cifs_dbg(FYI, "Send error in rename = %d\n", rc);
2207 cifs_buf_release(pSMB);
2215 int CIFSSMBRenameOpenFile(const unsigned int xid, struct cifs_tcon *pTcon,
2216 int netfid, const char *target_name,
2217 const struct nls_table *nls_codepage, int remap)
2219 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2220 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
2221 struct set_file_rename *rename_info;
2223 char dummy_string[30];
2225 int bytes_returned = 0;
2227 __u16 params, param_offset, offset, count, byte_count;
2229 cifs_dbg(FYI, "Rename to File by handle\n");
2230 rc = smb_init(SMB_COM_TRANSACTION2, 15, pTcon, (void **) &pSMB,
2236 pSMB->MaxSetupCount = 0;
2240 pSMB->Reserved2 = 0;
2241 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2242 offset = param_offset + params;
2244 /* SMB offsets are from the beginning of SMB which is 4 bytes in, after RFC1001 field */
2245 data_offset = (char *)(pSMB) + offset + 4;
2246 rename_info = (struct set_file_rename *) data_offset;
2247 pSMB->MaxParameterCount = cpu_to_le16(2);
2248 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
2249 pSMB->SetupCount = 1;
2250 pSMB->Reserved3 = 0;
2251 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2252 byte_count = 3 /* pad */ + params;
2253 pSMB->ParameterCount = cpu_to_le16(params);
2254 pSMB->TotalParameterCount = pSMB->ParameterCount;
2255 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2256 pSMB->DataOffset = cpu_to_le16(offset);
2257 /* construct random name ".cifs_tmp<inodenum><mid>" */
2258 rename_info->overwrite = cpu_to_le32(1);
2259 rename_info->root_fid = 0;
2260 /* unicode only call */
2261 if (target_name == NULL) {
2262 sprintf(dummy_string, "cifs%x", pSMB->hdr.Mid);
2264 cifsConvertToUTF16((__le16 *)rename_info->target_name,
2265 dummy_string, 24, nls_codepage, remap);
2268 cifsConvertToUTF16((__le16 *)rename_info->target_name,
2269 target_name, PATH_MAX, nls_codepage,
2272 rename_info->target_name_len = cpu_to_le32(2 * len_of_str);
2273 count = sizeof(struct set_file_rename) + (2 * len_of_str);
2274 byte_count += count;
2275 pSMB->DataCount = cpu_to_le16(count);
2276 pSMB->TotalDataCount = pSMB->DataCount;
2278 pSMB->InformationLevel =
2279 cpu_to_le16(SMB_SET_FILE_RENAME_INFORMATION);
2280 pSMB->Reserved4 = 0;
2281 inc_rfc1001_len(pSMB, byte_count);
2282 pSMB->ByteCount = cpu_to_le16(byte_count);
2283 rc = SendReceive(xid, pTcon->ses, (struct smb_hdr *) pSMB,
2284 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2285 cifs_stats_inc(&pTcon->stats.cifs_stats.num_t2renames);
2287 cifs_dbg(FYI, "Send error in Rename (by file handle) = %d\n",
2290 cifs_buf_release(pSMB);
2292 /* Note: On -EAGAIN error only caller can retry on handle based calls
2293 since file handle passed in no longer valid */
2299 CIFSSMBCopy(const unsigned int xid, struct cifs_tcon *tcon,
2300 const char *fromName, const __u16 target_tid, const char *toName,
2301 const int flags, const struct nls_table *nls_codepage, int remap)
2304 COPY_REQ *pSMB = NULL;
2305 COPY_RSP *pSMBr = NULL;
2307 int name_len, name_len2;
2310 cifs_dbg(FYI, "In CIFSSMBCopy\n");
2312 rc = smb_init(SMB_COM_COPY, 1, tcon, (void **) &pSMB,
2317 pSMB->BufferFormat = 0x04;
2318 pSMB->Tid2 = target_tid;
2320 pSMB->Flags = cpu_to_le16(flags & COPY_TREE);
2322 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2323 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2324 fromName, PATH_MAX, nls_codepage,
2326 name_len++; /* trailing null */
2328 pSMB->OldFileName[name_len] = 0x04; /* pad */
2329 /* protocol requires ASCII signature byte on Unicode string */
2330 pSMB->OldFileName[name_len + 1] = 0x00;
2332 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2333 toName, PATH_MAX, nls_codepage, remap);
2334 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2335 name_len2 *= 2; /* convert to bytes */
2337 name_len = copy_path_name(pSMB->OldFileName, fromName);
2338 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2339 name_len2 = copy_path_name(pSMB->OldFileName+name_len+1, toName);
2340 name_len2++; /* signature byte */
2343 count = 1 /* 1st signature byte */ + name_len + name_len2;
2344 inc_rfc1001_len(pSMB, count);
2345 pSMB->ByteCount = cpu_to_le16(count);
2347 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2348 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2350 cifs_dbg(FYI, "Send error in copy = %d with %d files copied\n",
2351 rc, le16_to_cpu(pSMBr->CopyCount));
2353 cifs_buf_release(pSMB);
2362 CIFSUnixCreateSymLink(const unsigned int xid, struct cifs_tcon *tcon,
2363 const char *fromName, const char *toName,
2364 const struct nls_table *nls_codepage, int remap)
2366 TRANSACTION2_SPI_REQ *pSMB = NULL;
2367 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2370 int name_len_target;
2372 int bytes_returned = 0;
2373 __u16 params, param_offset, offset, byte_count;
2375 cifs_dbg(FYI, "In Symlink Unix style\n");
2377 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2382 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2384 cifsConvertToUTF16((__le16 *) pSMB->FileName, fromName,
2385 /* find define for this maxpathcomponent */
2386 PATH_MAX, nls_codepage, remap);
2387 name_len++; /* trailing null */
2391 name_len = copy_path_name(pSMB->FileName, fromName);
2393 params = 6 + name_len;
2394 pSMB->MaxSetupCount = 0;
2398 pSMB->Reserved2 = 0;
2399 param_offset = offsetof(struct smb_com_transaction2_spi_req,
2400 InformationLevel) - 4;
2401 offset = param_offset + params;
2403 /* SMB offsets are from the beginning of SMB which is 4 bytes in, after RFC1001 field */
2404 data_offset = (char *)pSMB + offset + 4;
2405 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2407 cifsConvertToUTF16((__le16 *) data_offset, toName,
2408 /* find define for this maxpathcomponent */
2409 PATH_MAX, nls_codepage, remap);
2410 name_len_target++; /* trailing null */
2411 name_len_target *= 2;
2413 name_len_target = copy_path_name(data_offset, toName);
2416 pSMB->MaxParameterCount = cpu_to_le16(2);
2417 /* BB find exact max on data count below from sess */
2418 pSMB->MaxDataCount = cpu_to_le16(1000);
2419 pSMB->SetupCount = 1;
2420 pSMB->Reserved3 = 0;
2421 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2422 byte_count = 3 /* pad */ + params + name_len_target;
2423 pSMB->DataCount = cpu_to_le16(name_len_target);
2424 pSMB->ParameterCount = cpu_to_le16(params);
2425 pSMB->TotalDataCount = pSMB->DataCount;
2426 pSMB->TotalParameterCount = pSMB->ParameterCount;
2427 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2428 pSMB->DataOffset = cpu_to_le16(offset);
2429 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_LINK);
2430 pSMB->Reserved4 = 0;
2431 inc_rfc1001_len(pSMB, byte_count);
2432 pSMB->ByteCount = cpu_to_le16(byte_count);
2433 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2434 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2435 cifs_stats_inc(&tcon->stats.cifs_stats.num_symlinks);
2437 cifs_dbg(FYI, "Send error in SetPathInfo create symlink = %d\n",
2440 cifs_buf_release(pSMB);
2443 goto createSymLinkRetry;
2449 CIFSUnixCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon,
2450 const char *fromName, const char *toName,
2451 const struct nls_table *nls_codepage, int remap)
2453 TRANSACTION2_SPI_REQ *pSMB = NULL;
2454 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2457 int name_len_target;
2459 int bytes_returned = 0;
2460 __u16 params, param_offset, offset, byte_count;
2462 cifs_dbg(FYI, "In Create Hard link Unix style\n");
2463 createHardLinkRetry:
2464 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2469 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2470 name_len = cifsConvertToUTF16((__le16 *) pSMB->FileName, toName,
2471 PATH_MAX, nls_codepage, remap);
2472 name_len++; /* trailing null */
2476 name_len = copy_path_name(pSMB->FileName, toName);
2478 params = 6 + name_len;
2479 pSMB->MaxSetupCount = 0;
2483 pSMB->Reserved2 = 0;
2484 param_offset = offsetof(struct smb_com_transaction2_spi_req,
2485 InformationLevel) - 4;
2486 offset = param_offset + params;
2488 /* SMB offsets are from the beginning of SMB which is 4 bytes in, after RFC1001 field */
2489 data_offset = (char *)pSMB + offset + 4;
2490 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2492 cifsConvertToUTF16((__le16 *) data_offset, fromName,
2493 PATH_MAX, nls_codepage, remap);
2494 name_len_target++; /* trailing null */
2495 name_len_target *= 2;
2497 name_len_target = copy_path_name(data_offset, fromName);
2500 pSMB->MaxParameterCount = cpu_to_le16(2);
2501 /* BB find exact max on data count below from sess*/
2502 pSMB->MaxDataCount = cpu_to_le16(1000);
2503 pSMB->SetupCount = 1;
2504 pSMB->Reserved3 = 0;
2505 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2506 byte_count = 3 /* pad */ + params + name_len_target;
2507 pSMB->ParameterCount = cpu_to_le16(params);
2508 pSMB->TotalParameterCount = pSMB->ParameterCount;
2509 pSMB->DataCount = cpu_to_le16(name_len_target);
2510 pSMB->TotalDataCount = pSMB->DataCount;
2511 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2512 pSMB->DataOffset = cpu_to_le16(offset);
2513 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_HLINK);
2514 pSMB->Reserved4 = 0;
2515 inc_rfc1001_len(pSMB, byte_count);
2516 pSMB->ByteCount = cpu_to_le16(byte_count);
2517 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2518 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2519 cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
2521 cifs_dbg(FYI, "Send error in SetPathInfo (hard link) = %d\n",
2524 cifs_buf_release(pSMB);
2526 goto createHardLinkRetry;
2532 CIFSCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon,
2533 const char *from_name, const char *to_name,
2534 struct cifs_sb_info *cifs_sb)
2537 NT_RENAME_REQ *pSMB = NULL;
2538 RENAME_RSP *pSMBr = NULL;
2540 int name_len, name_len2;
2542 int remap = cifs_remap(cifs_sb);
2544 cifs_dbg(FYI, "In CIFSCreateHardLink\n");
2545 winCreateHardLinkRetry:
2547 rc = smb_init(SMB_COM_NT_RENAME, 4, tcon, (void **) &pSMB,
2552 pSMB->SearchAttributes =
2553 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
2555 pSMB->Flags = cpu_to_le16(CREATE_HARD_LINK);
2556 pSMB->ClusterCount = 0;
2558 pSMB->BufferFormat = 0x04;
2560 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2562 cifsConvertToUTF16((__le16 *) pSMB->OldFileName, from_name,
2563 PATH_MAX, cifs_sb->local_nls, remap);
2564 name_len++; /* trailing null */
2567 /* protocol specifies ASCII buffer format (0x04) for unicode */
2568 pSMB->OldFileName[name_len] = 0x04;
2569 pSMB->OldFileName[name_len + 1] = 0x00; /* pad */
2571 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2572 to_name, PATH_MAX, cifs_sb->local_nls,
2574 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2575 name_len2 *= 2; /* convert to bytes */
2577 name_len = copy_path_name(pSMB->OldFileName, from_name);
2578 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2579 name_len2 = copy_path_name(pSMB->OldFileName+name_len+1, to_name);
2580 name_len2++; /* signature byte */
2583 count = 1 /* string type byte */ + name_len + name_len2;
2584 inc_rfc1001_len(pSMB, count);
2585 pSMB->ByteCount = cpu_to_le16(count);
2587 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2588 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2589 cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
2591 cifs_dbg(FYI, "Send error in hard link (NT rename) = %d\n", rc);
2593 cifs_buf_release(pSMB);
2595 goto winCreateHardLinkRetry;
2601 CIFSSMBUnixQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon,
2602 const unsigned char *searchName, char **symlinkinfo,
2603 const struct nls_table *nls_codepage, int remap)
2605 /* SMB_QUERY_FILE_UNIX_LINK */
2606 TRANSACTION2_QPI_REQ *pSMB = NULL;
2607 TRANSACTION2_QPI_RSP *pSMBr = NULL;
2611 __u16 params, byte_count;
2614 cifs_dbg(FYI, "In QPathSymLinkInfo (Unix) for path %s\n", searchName);
2617 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2622 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2624 cifsConvertToUTF16((__le16 *) pSMB->FileName,
2625 searchName, PATH_MAX, nls_codepage,
2627 name_len++; /* trailing null */
2630 name_len = copy_path_name(pSMB->FileName, searchName);
2633 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
2634 pSMB->TotalDataCount = 0;
2635 pSMB->MaxParameterCount = cpu_to_le16(2);
2636 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
2637 pSMB->MaxSetupCount = 0;
2641 pSMB->Reserved2 = 0;
2642 pSMB->ParameterOffset = cpu_to_le16(offsetof(
2643 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
2644 pSMB->DataCount = 0;
2645 pSMB->DataOffset = 0;
2646 pSMB->SetupCount = 1;
2647 pSMB->Reserved3 = 0;
2648 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
2649 byte_count = params + 1 /* pad */ ;
2650 pSMB->TotalParameterCount = cpu_to_le16(params);
2651 pSMB->ParameterCount = pSMB->TotalParameterCount;
2652 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_LINK);
2653 pSMB->Reserved4 = 0;
2654 inc_rfc1001_len(pSMB, byte_count);
2655 pSMB->ByteCount = cpu_to_le16(byte_count);
2657 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2658 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2660 cifs_dbg(FYI, "Send error in QuerySymLinkInfo = %d\n", rc);
2662 /* decode response */
2664 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
2665 /* BB also check enough total bytes returned */
2666 if (rc || get_bcc(&pSMBr->hdr) < 2)
2670 u16 count = le16_to_cpu(pSMBr->t2.DataCount);
2672 data_start = ((char *) &pSMBr->hdr.Protocol) +
2673 le16_to_cpu(pSMBr->t2.DataOffset);
2675 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
2680 /* BB FIXME investigate remapping reserved chars here */
2681 *symlinkinfo = cifs_strndup_from_utf16(data_start,
2682 count, is_unicode, nls_codepage);
2687 cifs_buf_release(pSMB);
2689 goto querySymLinkRetry;
2694 * Recent Windows versions now create symlinks more frequently
2695 * and they use the "reparse point" mechanism below. We can of course
2696 * do symlinks nicely to Samba and other servers which support the
2697 * CIFS Unix Extensions and we can also do SFU symlinks and "client only"
2698 * "MF" symlinks optionally, but for recent Windows we really need to
2699 * reenable the code below and fix the cifs_symlink callers to handle this.
2700 * In the interim this code has been moved to its own config option so
2701 * it is not compiled in by default until callers fixed up and more tested.
2704 CIFSSMBQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon,
2705 __u16 fid, char **symlinkinfo,
2706 const struct nls_table *nls_codepage)
2710 struct smb_com_transaction_ioctl_req *pSMB;
2711 struct smb_com_transaction_ioctl_rsp *pSMBr;
2713 unsigned int sub_len;
2715 struct reparse_symlink_data *reparse_buf;
2716 struct reparse_posix_data *posix_buf;
2717 __u32 data_offset, data_count;
2720 cifs_dbg(FYI, "In Windows reparse style QueryLink for fid %u\n", fid);
2721 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
2726 pSMB->TotalParameterCount = 0 ;
2727 pSMB->TotalDataCount = 0;
2728 pSMB->MaxParameterCount = cpu_to_le32(2);
2729 /* BB find exact data count max from sess structure BB */
2730 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
2731 pSMB->MaxSetupCount = 4;
2733 pSMB->ParameterOffset = 0;
2734 pSMB->DataCount = 0;
2735 pSMB->DataOffset = 0;
2736 pSMB->SetupCount = 4;
2737 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
2738 pSMB->ParameterCount = pSMB->TotalParameterCount;
2739 pSMB->FunctionCode = cpu_to_le32(FSCTL_GET_REPARSE_POINT);
2740 pSMB->IsFsctl = 1; /* FSCTL */
2741 pSMB->IsRootFlag = 0;
2742 pSMB->Fid = fid; /* file handle always le */
2743 pSMB->ByteCount = 0;
2745 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2746 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2748 cifs_dbg(FYI, "Send error in QueryReparseLinkInfo = %d\n", rc);
2752 data_offset = le32_to_cpu(pSMBr->DataOffset);
2753 data_count = le32_to_cpu(pSMBr->DataCount);
2754 if (get_bcc(&pSMBr->hdr) < 2 || data_offset > 512) {
2755 /* BB also check enough total bytes returned */
2756 rc = -EIO; /* bad smb */
2759 if (!data_count || (data_count > 2048)) {
2761 cifs_dbg(FYI, "Invalid return data count on get reparse info ioctl\n");
2764 end_of_smb = 2 + get_bcc(&pSMBr->hdr) + (char *)&pSMBr->ByteCount;
2765 reparse_buf = (struct reparse_symlink_data *)
2766 ((char *)&pSMBr->hdr.Protocol + data_offset);
2767 if ((char *)reparse_buf >= end_of_smb) {
2771 if (reparse_buf->ReparseTag == cpu_to_le32(IO_REPARSE_TAG_NFS)) {
2772 cifs_dbg(FYI, "NFS style reparse tag\n");
2773 posix_buf = (struct reparse_posix_data *)reparse_buf;
2775 if (posix_buf->InodeType != cpu_to_le64(NFS_SPECFILE_LNK)) {
2776 cifs_dbg(FYI, "unsupported file type 0x%llx\n",
2777 le64_to_cpu(posix_buf->InodeType));
2782 sub_len = le16_to_cpu(reparse_buf->ReparseDataLength);
2783 if (posix_buf->PathBuffer + sub_len > end_of_smb) {
2784 cifs_dbg(FYI, "reparse buf beyond SMB\n");
2788 *symlinkinfo = cifs_strndup_from_utf16(posix_buf->PathBuffer,
2789 sub_len, is_unicode, nls_codepage);
2791 } else if (reparse_buf->ReparseTag !=
2792 cpu_to_le32(IO_REPARSE_TAG_SYMLINK)) {
2797 /* Reparse tag is NTFS symlink */
2798 sub_start = le16_to_cpu(reparse_buf->SubstituteNameOffset) +
2799 reparse_buf->PathBuffer;
2800 sub_len = le16_to_cpu(reparse_buf->SubstituteNameLength);
2801 if (sub_start + sub_len > end_of_smb) {
2802 cifs_dbg(FYI, "reparse buf beyond SMB\n");
2806 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
2811 /* BB FIXME investigate remapping reserved chars here */
2812 *symlinkinfo = cifs_strndup_from_utf16(sub_start, sub_len, is_unicode,
2817 cifs_buf_release(pSMB);
2820 * Note: On -EAGAIN error only caller can retry on handle based calls
2821 * since file handle passed in no longer valid.
2827 CIFSSMB_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2832 struct smb_com_transaction_compr_ioctl_req *pSMB;
2833 struct smb_com_transaction_ioctl_rsp *pSMBr;
2835 cifs_dbg(FYI, "Set compression for %u\n", fid);
2836 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
2841 pSMB->compression_state = cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
2843 pSMB->TotalParameterCount = 0;
2844 pSMB->TotalDataCount = cpu_to_le32(2);
2845 pSMB->MaxParameterCount = 0;
2846 pSMB->MaxDataCount = 0;
2847 pSMB->MaxSetupCount = 4;
2849 pSMB->ParameterOffset = 0;
2850 pSMB->DataCount = cpu_to_le32(2);
2852 cpu_to_le32(offsetof(struct smb_com_transaction_compr_ioctl_req,
2853 compression_state) - 4); /* 84 */
2854 pSMB->SetupCount = 4;
2855 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
2856 pSMB->ParameterCount = 0;
2857 pSMB->FunctionCode = cpu_to_le32(FSCTL_SET_COMPRESSION);
2858 pSMB->IsFsctl = 1; /* FSCTL */
2859 pSMB->IsRootFlag = 0;
2860 pSMB->Fid = fid; /* file handle always le */
2861 /* 3 byte pad, followed by 2 byte compress state */
2862 pSMB->ByteCount = cpu_to_le16(5);
2863 inc_rfc1001_len(pSMB, 5);
2865 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2866 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2868 cifs_dbg(FYI, "Send error in SetCompression = %d\n", rc);
2870 cifs_buf_release(pSMB);
2873 * Note: On -EAGAIN error only caller can retry on handle based calls
2874 * since file handle passed in no longer valid.
2880 #ifdef CONFIG_CIFS_POSIX
2882 #ifdef CONFIG_FS_POSIX_ACL
2884 * cifs_init_posix_acl - convert ACL from cifs to POSIX ACL format
2885 * @ace: POSIX ACL entry to store converted ACL into
2886 * @cifs_ace: ACL in cifs format
2888 * Convert an Access Control Entry from wire format to local POSIX xattr
2891 * Note that the @cifs_uid member is used to store both {g,u}id_t.
2893 static void cifs_init_posix_acl(struct posix_acl_entry *ace,
2894 struct cifs_posix_ace *cifs_ace)
2896 /* u8 cifs fields do not need le conversion */
2897 ace->e_perm = cifs_ace->cifs_e_perm;
2898 ace->e_tag = cifs_ace->cifs_e_tag;
2900 switch (ace->e_tag) {
2902 ace->e_uid = make_kuid(&init_user_ns,
2903 le64_to_cpu(cifs_ace->cifs_uid));
2906 ace->e_gid = make_kgid(&init_user_ns,
2907 le64_to_cpu(cifs_ace->cifs_uid));
2914 * cifs_to_posix_acl - copy cifs ACL format to POSIX ACL format
2915 * @acl: ACLs returned in POSIX ACL format
2916 * @src: ACLs in cifs format
2917 * @acl_type: type of POSIX ACL requested
2918 * @size_of_data_area: size of SMB we got
2920 * This function converts ACLs from cifs format to POSIX ACL format.
2921 * If @acl is NULL then the size of the buffer required to store POSIX ACLs in
2922 * their uapi format is returned.
2924 static int cifs_to_posix_acl(struct posix_acl **acl, char *src,
2925 const int acl_type, const int size_of_data_area)
2929 struct cifs_posix_ace *pACE;
2930 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)src;
2931 struct posix_acl *kacl = NULL;
2932 struct posix_acl_entry *pa, *pe;
2934 if (le16_to_cpu(cifs_acl->version) != CIFS_ACL_VERSION)
2937 if (acl_type == ACL_TYPE_ACCESS) {
2938 count = le16_to_cpu(cifs_acl->access_entry_count);
2939 pACE = &cifs_acl->ace_array[0];
2940 size = sizeof(struct cifs_posix_acl);
2941 size += sizeof(struct cifs_posix_ace) * count;
2942 /* check if we would go beyond end of SMB */
2943 if (size_of_data_area < size) {
2944 cifs_dbg(FYI, "bad CIFS POSIX ACL size %d vs. %d\n",
2945 size_of_data_area, size);
2948 } else if (acl_type == ACL_TYPE_DEFAULT) {
2949 count = le16_to_cpu(cifs_acl->access_entry_count);
2950 size = sizeof(struct cifs_posix_acl);
2951 size += sizeof(struct cifs_posix_ace) * count;
2952 /* skip past access ACEs to get to default ACEs */
2953 pACE = &cifs_acl->ace_array[count];
2954 count = le16_to_cpu(cifs_acl->default_entry_count);
2955 size += sizeof(struct cifs_posix_ace) * count;
2956 /* check if we would go beyond end of SMB */
2957 if (size_of_data_area < size)
2964 /* Allocate number of POSIX ACLs to store in VFS format. */
2965 kacl = posix_acl_alloc(count, GFP_NOFS);
2969 FOREACH_ACL_ENTRY(pa, kacl, pe) {
2970 cifs_init_posix_acl(pa, pACE);
2979 * cifs_init_ace - convert ACL entry from POSIX ACL to cifs format
2980 * @cifs_ace: the cifs ACL entry to store into
2981 * @local_ace: the POSIX ACL entry to convert
2983 static void cifs_init_ace(struct cifs_posix_ace *cifs_ace,
2984 const struct posix_acl_entry *local_ace)
2986 cifs_ace->cifs_e_perm = local_ace->e_perm;
2987 cifs_ace->cifs_e_tag = local_ace->e_tag;
2989 switch (local_ace->e_tag) {
2991 cifs_ace->cifs_uid =
2992 cpu_to_le64(from_kuid(&init_user_ns, local_ace->e_uid));
2995 cifs_ace->cifs_uid =
2996 cpu_to_le64(from_kgid(&init_user_ns, local_ace->e_gid));
2999 cifs_ace->cifs_uid = cpu_to_le64(-1);
3004 * posix_acl_to_cifs - convert ACLs from POSIX ACL to cifs format
3005 * @parm_data: ACLs in cifs format to conver to
3006 * @acl: ACLs in POSIX ACL format to convert from
3007 * @acl_type: the type of POSIX ACLs stored in @acl
3009 * Return: the number cifs ACL entries after conversion
3011 static __u16 posix_acl_to_cifs(char *parm_data, const struct posix_acl *acl,
3015 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)parm_data;
3016 const struct posix_acl_entry *pa, *pe;
3020 if ((acl == NULL) || (cifs_acl == NULL))
3023 count = acl->a_count;
3024 cifs_dbg(FYI, "setting acl with %d entries\n", count);
3027 * Note that the uapi POSIX ACL version is verified by the VFS and is
3028 * independent of the cifs ACL version. Changing the POSIX ACL version
3029 * is a uapi change and if it's changed we will pass down the POSIX ACL
3030 * version in struct posix_acl from the VFS. For now there's really
3031 * only one that all filesystems know how to deal with.
3033 cifs_acl->version = cpu_to_le16(1);
3034 if (acl_type == ACL_TYPE_ACCESS) {
3035 cifs_acl->access_entry_count = cpu_to_le16(count);
3036 cifs_acl->default_entry_count = cpu_to_le16(0xFFFF);
3037 } else if (acl_type == ACL_TYPE_DEFAULT) {
3038 cifs_acl->default_entry_count = cpu_to_le16(count);
3039 cifs_acl->access_entry_count = cpu_to_le16(0xFFFF);
3041 cifs_dbg(FYI, "unknown ACL type %d\n", acl_type);
3044 FOREACH_ACL_ENTRY(pa, acl, pe) {
3045 cifs_init_ace(&cifs_acl->ace_array[i++], pa);
3048 rc = (__u16)(count * sizeof(struct cifs_posix_ace));
3049 rc += sizeof(struct cifs_posix_acl);
3050 /* BB add check to make sure ACL does not overflow SMB */
3055 int cifs_do_get_acl(const unsigned int xid, struct cifs_tcon *tcon,
3056 const unsigned char *searchName, struct posix_acl **acl,
3057 const int acl_type, const struct nls_table *nls_codepage,
3060 /* SMB_QUERY_POSIX_ACL */
3061 TRANSACTION2_QPI_REQ *pSMB = NULL;
3062 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3066 __u16 params, byte_count;
3068 cifs_dbg(FYI, "In GetPosixACL (Unix) for path %s\n", searchName);
3071 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3076 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3078 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3079 searchName, PATH_MAX, nls_codepage,
3081 name_len++; /* trailing null */
3083 pSMB->FileName[name_len] = 0;
3084 pSMB->FileName[name_len+1] = 0;
3086 name_len = copy_path_name(pSMB->FileName, searchName);
3089 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3090 pSMB->TotalDataCount = 0;
3091 pSMB->MaxParameterCount = cpu_to_le16(2);
3092 /* BB find exact max data count below from sess structure BB */
3093 pSMB->MaxDataCount = cpu_to_le16(4000);
3094 pSMB->MaxSetupCount = 0;
3098 pSMB->Reserved2 = 0;
3099 pSMB->ParameterOffset = cpu_to_le16(
3100 offsetof(struct smb_com_transaction2_qpi_req,
3101 InformationLevel) - 4);
3102 pSMB->DataCount = 0;
3103 pSMB->DataOffset = 0;
3104 pSMB->SetupCount = 1;
3105 pSMB->Reserved3 = 0;
3106 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3107 byte_count = params + 1 /* pad */ ;
3108 pSMB->TotalParameterCount = cpu_to_le16(params);
3109 pSMB->ParameterCount = pSMB->TotalParameterCount;
3110 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_ACL);
3111 pSMB->Reserved4 = 0;
3112 inc_rfc1001_len(pSMB, byte_count);
3113 pSMB->ByteCount = cpu_to_le16(byte_count);
3115 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3116 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3117 cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
3119 cifs_dbg(FYI, "Send error in Query POSIX ACL = %d\n", rc);
3121 /* decode response */
3123 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
3124 /* BB also check enough total bytes returned */
3125 if (rc || get_bcc(&pSMBr->hdr) < 2)
3126 rc = -EIO; /* bad smb */
3128 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3129 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3130 rc = cifs_to_posix_acl(acl,
3131 (char *)&pSMBr->hdr.Protocol+data_offset,
3135 cifs_buf_release(pSMB);
3137 * The else branch after SendReceive() doesn't return EAGAIN so if we
3138 * allocated @acl in cifs_to_posix_acl() we are guaranteed to return
3139 * here and don't leak POSIX ACLs.
3146 int cifs_do_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
3147 const unsigned char *fileName, const struct posix_acl *acl,
3148 const int acl_type, const struct nls_table *nls_codepage,
3151 struct smb_com_transaction2_spi_req *pSMB = NULL;
3152 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
3156 int bytes_returned = 0;
3157 __u16 params, byte_count, data_count, param_offset, offset;
3159 cifs_dbg(FYI, "In SetPosixACL (Unix) for path %s\n", fileName);
3161 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3165 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3167 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
3168 PATH_MAX, nls_codepage, remap);
3169 name_len++; /* trailing null */
3172 name_len = copy_path_name(pSMB->FileName, fileName);
3174 params = 6 + name_len;
3175 pSMB->MaxParameterCount = cpu_to_le16(2);
3176 /* BB find max SMB size from sess */
3177 pSMB->MaxDataCount = cpu_to_le16(1000);
3178 pSMB->MaxSetupCount = 0;
3182 pSMB->Reserved2 = 0;
3183 param_offset = offsetof(struct smb_com_transaction2_spi_req,
3184 InformationLevel) - 4;
3185 offset = param_offset + params;
3186 parm_data = ((char *)pSMB) + sizeof(pSMB->hdr.smb_buf_length) + offset;
3187 pSMB->ParameterOffset = cpu_to_le16(param_offset);
3189 /* convert to on the wire format for POSIX ACL */
3190 data_count = posix_acl_to_cifs(parm_data, acl, acl_type);
3192 if (data_count == 0) {
3194 goto setACLerrorExit;
3196 pSMB->DataOffset = cpu_to_le16(offset);
3197 pSMB->SetupCount = 1;
3198 pSMB->Reserved3 = 0;
3199 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
3200 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_ACL);
3201 byte_count = 3 /* pad */ + params + data_count;
3202 pSMB->DataCount = cpu_to_le16(data_count);
3203 pSMB->TotalDataCount = pSMB->DataCount;
3204 pSMB->ParameterCount = cpu_to_le16(params);
3205 pSMB->TotalParameterCount = pSMB->ParameterCount;
3206 pSMB->Reserved4 = 0;
3207 inc_rfc1001_len(pSMB, byte_count);
3208 pSMB->ByteCount = cpu_to_le16(byte_count);
3209 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3210 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3212 cifs_dbg(FYI, "Set POSIX ACL returned %d\n", rc);
3215 cifs_buf_release(pSMB);
3221 int cifs_do_get_acl(const unsigned int xid, struct cifs_tcon *tcon,
3222 const unsigned char *searchName, struct posix_acl **acl,
3223 const int acl_type, const struct nls_table *nls_codepage,
3229 int cifs_do_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
3230 const unsigned char *fileName, const struct posix_acl *acl,
3231 const int acl_type, const struct nls_table *nls_codepage,
3236 #endif /* CONFIG_FS_POSIX_ACL */
3239 CIFSGetExtAttr(const unsigned int xid, struct cifs_tcon *tcon,
3240 const int netfid, __u64 *pExtAttrBits, __u64 *pMask)
3243 struct smb_t2_qfi_req *pSMB = NULL;
3244 struct smb_t2_qfi_rsp *pSMBr = NULL;
3246 __u16 params, byte_count;
3248 cifs_dbg(FYI, "In GetExtAttr\n");
3253 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3258 params = 2 /* level */ + 2 /* fid */;
3259 pSMB->t2.TotalDataCount = 0;
3260 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
3261 /* BB find exact max data count below from sess structure BB */
3262 pSMB->t2.MaxDataCount = cpu_to_le16(4000);
3263 pSMB->t2.MaxSetupCount = 0;
3264 pSMB->t2.Reserved = 0;
3266 pSMB->t2.Timeout = 0;
3267 pSMB->t2.Reserved2 = 0;
3268 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
3270 pSMB->t2.DataCount = 0;
3271 pSMB->t2.DataOffset = 0;
3272 pSMB->t2.SetupCount = 1;
3273 pSMB->t2.Reserved3 = 0;
3274 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
3275 byte_count = params + 1 /* pad */ ;
3276 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
3277 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
3278 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_ATTR_FLAGS);
3281 inc_rfc1001_len(pSMB, byte_count);
3282 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
3284 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3285 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3287 cifs_dbg(FYI, "error %d in GetExtAttr\n", rc);
3289 /* decode response */
3290 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
3291 /* BB also check enough total bytes returned */
3292 if (rc || get_bcc(&pSMBr->hdr) < 2)
3293 /* If rc should we check for EOPNOSUPP and
3294 disable the srvino flag? or in caller? */
3295 rc = -EIO; /* bad smb */
3297 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3298 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3299 struct file_chattr_info *pfinfo;
3302 cifs_dbg(FYI, "Invalid size ret in GetExtAttr\n");
3306 pfinfo = (struct file_chattr_info *)
3307 (data_offset + (char *) &pSMBr->hdr.Protocol);
3308 *pExtAttrBits = le64_to_cpu(pfinfo->mode);
3309 *pMask = le64_to_cpu(pfinfo->mask);
3313 cifs_buf_release(pSMB);
3315 goto GetExtAttrRetry;
3319 #endif /* CONFIG_POSIX */
3322 * Initialize NT TRANSACT SMB into small smb request buffer. This assumes that
3323 * all NT TRANSACTS that we init here have total parm and data under about 400
3324 * bytes (to fit in small cifs buffer size), which is the case so far, it
3325 * easily fits. NB: Setup words themselves and ByteCount MaxSetupCount (size of
3326 * returned setup area) and MaxParameterCount (returned parms size) must be set
3330 smb_init_nttransact(const __u16 sub_command, const int setup_count,
3331 const int parm_len, struct cifs_tcon *tcon,
3336 struct smb_com_ntransact_req *pSMB;
3338 rc = small_smb_init(SMB_COM_NT_TRANSACT, 19 + setup_count, tcon,
3342 *ret_buf = (void *)pSMB;
3344 pSMB->TotalParameterCount = cpu_to_le32(parm_len);
3345 pSMB->TotalDataCount = 0;
3346 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
3347 pSMB->ParameterCount = pSMB->TotalParameterCount;
3348 pSMB->DataCount = pSMB->TotalDataCount;
3349 temp_offset = offsetof(struct smb_com_ntransact_req, Parms) +
3350 (setup_count * 2) - 4 /* for rfc1001 length itself */;
3351 pSMB->ParameterOffset = cpu_to_le32(temp_offset);
3352 pSMB->DataOffset = cpu_to_le32(temp_offset + parm_len);
3353 pSMB->SetupCount = setup_count; /* no need to le convert byte fields */
3354 pSMB->SubCommand = cpu_to_le16(sub_command);
3359 validate_ntransact(char *buf, char **ppparm, char **ppdata,
3360 __u32 *pparmlen, __u32 *pdatalen)
3363 __u32 data_count, data_offset, parm_count, parm_offset;
3364 struct smb_com_ntransact_rsp *pSMBr;
3373 pSMBr = (struct smb_com_ntransact_rsp *)buf;
3375 bcc = get_bcc(&pSMBr->hdr);
3376 end_of_smb = 2 /* sizeof byte count */ + bcc +
3377 (char *)&pSMBr->ByteCount;
3379 data_offset = le32_to_cpu(pSMBr->DataOffset);
3380 data_count = le32_to_cpu(pSMBr->DataCount);
3381 parm_offset = le32_to_cpu(pSMBr->ParameterOffset);
3382 parm_count = le32_to_cpu(pSMBr->ParameterCount);
3384 *ppparm = (char *)&pSMBr->hdr.Protocol + parm_offset;
3385 *ppdata = (char *)&pSMBr->hdr.Protocol + data_offset;
3387 /* should we also check that parm and data areas do not overlap? */
3388 if (*ppparm > end_of_smb) {
3389 cifs_dbg(FYI, "parms start after end of smb\n");
3391 } else if (parm_count + *ppparm > end_of_smb) {
3392 cifs_dbg(FYI, "parm end after end of smb\n");
3394 } else if (*ppdata > end_of_smb) {
3395 cifs_dbg(FYI, "data starts after end of smb\n");
3397 } else if (data_count + *ppdata > end_of_smb) {
3398 cifs_dbg(FYI, "data %p + count %d (%p) past smb end %p start %p\n",
3399 *ppdata, data_count, (data_count + *ppdata),
3402 } else if (parm_count + data_count > bcc) {
3403 cifs_dbg(FYI, "parm count and data count larger than SMB\n");
3406 *pdatalen = data_count;
3407 *pparmlen = parm_count;
3411 /* Get Security Descriptor (by handle) from remote server for a file or dir */
3413 CIFSSMBGetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
3414 struct cifs_ntsd **acl_inf, __u32 *pbuflen)
3418 QUERY_SEC_DESC_REQ *pSMB;
3420 struct kvec rsp_iov;
3422 cifs_dbg(FYI, "GetCifsACL\n");
3427 rc = smb_init_nttransact(NT_TRANSACT_QUERY_SECURITY_DESC, 0,
3428 8 /* parm len */, tcon, (void **) &pSMB);
3432 pSMB->MaxParameterCount = cpu_to_le32(4);
3433 /* BB TEST with big acls that might need to be e.g. larger than 16K */
3434 pSMB->MaxSetupCount = 0;
3435 pSMB->Fid = fid; /* file handle always le */
3436 pSMB->AclFlags = cpu_to_le32(CIFS_ACL_OWNER | CIFS_ACL_GROUP |
3438 pSMB->ByteCount = cpu_to_le16(11); /* 3 bytes pad + 8 bytes parm */
3439 inc_rfc1001_len(pSMB, 11);
3440 iov[0].iov_base = (char *)pSMB;
3441 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
3443 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovec */, &buf_type,
3445 cifs_small_buf_release(pSMB);
3446 cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
3448 cifs_dbg(FYI, "Send error in QuerySecDesc = %d\n", rc);
3449 } else { /* decode response */
3453 struct smb_com_ntransact_rsp *pSMBr;
3456 /* validate_nttransact */
3457 rc = validate_ntransact(rsp_iov.iov_base, (char **)&parm,
3458 &pdata, &parm_len, pbuflen);
3461 pSMBr = (struct smb_com_ntransact_rsp *)rsp_iov.iov_base;
3463 cifs_dbg(FYI, "smb %p parm %p data %p\n",
3464 pSMBr, parm, *acl_inf);
3466 if (le32_to_cpu(pSMBr->ParameterCount) != 4) {
3467 rc = -EIO; /* bad smb */
3472 /* BB check that data area is minimum length and as big as acl_len */
3474 acl_len = le32_to_cpu(*parm);
3475 if (acl_len != *pbuflen) {
3476 cifs_dbg(VFS, "acl length %d does not match %d\n",
3478 if (*pbuflen > acl_len)
3482 /* check if buffer is big enough for the acl
3483 header followed by the smallest SID */
3484 if ((*pbuflen < sizeof(struct cifs_ntsd) + 8) ||
3485 (*pbuflen >= 64 * 1024)) {
3486 cifs_dbg(VFS, "bad acl length %d\n", *pbuflen);
3490 *acl_inf = kmemdup(pdata, *pbuflen, GFP_KERNEL);
3491 if (*acl_inf == NULL) {
3498 free_rsp_buf(buf_type, rsp_iov.iov_base);
3503 CIFSSMBSetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
3504 struct cifs_ntsd *pntsd, __u32 acllen, int aclflag)
3506 __u16 byte_count, param_count, data_count, param_offset, data_offset;
3508 int bytes_returned = 0;
3509 SET_SEC_DESC_REQ *pSMB = NULL;
3513 rc = smb_init(SMB_COM_NT_TRANSACT, 19, tcon, (void **) &pSMB, &pSMBr);
3517 pSMB->MaxSetupCount = 0;
3521 param_offset = offsetof(struct smb_com_transaction_ssec_req, Fid) - 4;
3522 data_count = acllen;
3523 data_offset = param_offset + param_count;
3524 byte_count = 3 /* pad */ + param_count;
3526 pSMB->DataCount = cpu_to_le32(data_count);
3527 pSMB->TotalDataCount = pSMB->DataCount;
3528 pSMB->MaxParameterCount = cpu_to_le32(4);
3529 pSMB->MaxDataCount = cpu_to_le32(16384);
3530 pSMB->ParameterCount = cpu_to_le32(param_count);
3531 pSMB->ParameterOffset = cpu_to_le32(param_offset);
3532 pSMB->TotalParameterCount = pSMB->ParameterCount;
3533 pSMB->DataOffset = cpu_to_le32(data_offset);
3534 pSMB->SetupCount = 0;
3535 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_SET_SECURITY_DESC);
3536 pSMB->ByteCount = cpu_to_le16(byte_count+data_count);
3538 pSMB->Fid = fid; /* file handle always le */
3539 pSMB->Reserved2 = 0;
3540 pSMB->AclFlags = cpu_to_le32(aclflag);
3542 if (pntsd && acllen) {
3543 memcpy((char *)pSMBr + offsetof(struct smb_hdr, Protocol) +
3544 data_offset, pntsd, acllen);
3545 inc_rfc1001_len(pSMB, byte_count + data_count);
3547 inc_rfc1001_len(pSMB, byte_count);
3549 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3550 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3552 cifs_dbg(FYI, "SetCIFSACL bytes_returned: %d, rc: %d\n",
3553 bytes_returned, rc);
3555 cifs_dbg(FYI, "Set CIFS ACL returned %d\n", rc);
3556 cifs_buf_release(pSMB);
3559 goto setCifsAclRetry;
3565 /* Legacy Query Path Information call for lookup to old servers such
3568 SMBQueryInformation(const unsigned int xid, struct cifs_tcon *tcon,
3569 const char *search_name, FILE_ALL_INFO *data,
3570 const struct nls_table *nls_codepage, int remap)
3572 QUERY_INFORMATION_REQ *pSMB;
3573 QUERY_INFORMATION_RSP *pSMBr;
3578 cifs_dbg(FYI, "In SMBQPath path %s\n", search_name);
3580 rc = smb_init(SMB_COM_QUERY_INFORMATION, 0, tcon, (void **) &pSMB,
3585 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3587 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3588 search_name, PATH_MAX, nls_codepage,
3590 name_len++; /* trailing null */
3593 name_len = copy_path_name(pSMB->FileName, search_name);
3595 pSMB->BufferFormat = 0x04;
3596 name_len++; /* account for buffer type byte */
3597 inc_rfc1001_len(pSMB, (__u16)name_len);
3598 pSMB->ByteCount = cpu_to_le16(name_len);
3600 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3601 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3603 cifs_dbg(FYI, "Send error in QueryInfo = %d\n", rc);
3605 struct timespec64 ts;
3606 __u32 time = le32_to_cpu(pSMBr->last_write_time);
3608 /* decode response */
3609 /* BB FIXME - add time zone adjustment BB */
3610 memset(data, 0, sizeof(FILE_ALL_INFO));
3613 /* decode time fields */
3614 data->ChangeTime = cpu_to_le64(cifs_UnixTimeToNT(ts));
3615 data->LastWriteTime = data->ChangeTime;
3616 data->LastAccessTime = 0;
3617 data->AllocationSize =
3618 cpu_to_le64(le32_to_cpu(pSMBr->size));
3619 data->EndOfFile = data->AllocationSize;
3621 cpu_to_le32(le16_to_cpu(pSMBr->attr));
3623 rc = -EIO; /* bad buffer passed in */
3625 cifs_buf_release(pSMB);
3634 CIFSSMBQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
3635 u16 netfid, FILE_ALL_INFO *pFindData)
3637 struct smb_t2_qfi_req *pSMB = NULL;
3638 struct smb_t2_qfi_rsp *pSMBr = NULL;
3641 __u16 params, byte_count;
3644 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3649 params = 2 /* level */ + 2 /* fid */;
3650 pSMB->t2.TotalDataCount = 0;
3651 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
3652 /* BB find exact max data count below from sess structure BB */
3653 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
3654 pSMB->t2.MaxSetupCount = 0;
3655 pSMB->t2.Reserved = 0;
3657 pSMB->t2.Timeout = 0;
3658 pSMB->t2.Reserved2 = 0;
3659 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
3661 pSMB->t2.DataCount = 0;
3662 pSMB->t2.DataOffset = 0;
3663 pSMB->t2.SetupCount = 1;
3664 pSMB->t2.Reserved3 = 0;
3665 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
3666 byte_count = params + 1 /* pad */ ;
3667 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
3668 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
3669 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
3672 inc_rfc1001_len(pSMB, byte_count);
3673 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
3675 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3676 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3678 cifs_dbg(FYI, "Send error in QFileInfo = %d\n", rc);
3679 } else { /* decode response */
3680 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
3682 if (rc) /* BB add auto retry on EOPNOTSUPP? */
3684 else if (get_bcc(&pSMBr->hdr) < 40)
3685 rc = -EIO; /* bad smb */
3686 else if (pFindData) {
3687 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3688 memcpy((char *) pFindData,
3689 (char *) &pSMBr->hdr.Protocol +
3690 data_offset, sizeof(FILE_ALL_INFO));
3694 cifs_buf_release(pSMB);
3696 goto QFileInfoRetry;
3702 CIFSSMBQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
3703 const char *search_name, FILE_ALL_INFO *data,
3704 int legacy /* old style infolevel */,
3705 const struct nls_table *nls_codepage, int remap)
3707 /* level 263 SMB_QUERY_FILE_ALL_INFO */
3708 TRANSACTION2_QPI_REQ *pSMB = NULL;
3709 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3713 __u16 params, byte_count;
3715 /* cifs_dbg(FYI, "In QPathInfo path %s\n", search_name); */
3717 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3722 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3724 cifsConvertToUTF16((__le16 *) pSMB->FileName, search_name,
3725 PATH_MAX, nls_codepage, remap);
3726 name_len++; /* trailing null */
3729 name_len = copy_path_name(pSMB->FileName, search_name);
3732 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
3733 pSMB->TotalDataCount = 0;
3734 pSMB->MaxParameterCount = cpu_to_le16(2);
3735 /* BB find exact max SMB PDU from sess structure BB */
3736 pSMB->MaxDataCount = cpu_to_le16(4000);
3737 pSMB->MaxSetupCount = 0;
3741 pSMB->Reserved2 = 0;
3742 pSMB->ParameterOffset = cpu_to_le16(offsetof(
3743 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
3744 pSMB->DataCount = 0;
3745 pSMB->DataOffset = 0;
3746 pSMB->SetupCount = 1;
3747 pSMB->Reserved3 = 0;
3748 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3749 byte_count = params + 1 /* pad */ ;
3750 pSMB->TotalParameterCount = cpu_to_le16(params);
3751 pSMB->ParameterCount = pSMB->TotalParameterCount;
3753 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_STANDARD);
3755 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
3756 pSMB->Reserved4 = 0;
3757 inc_rfc1001_len(pSMB, byte_count);
3758 pSMB->ByteCount = cpu_to_le16(byte_count);
3760 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3761 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3763 cifs_dbg(FYI, "Send error in QPathInfo = %d\n", rc);
3764 } else { /* decode response */
3765 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
3767 if (rc) /* BB add auto retry on EOPNOTSUPP? */
3769 else if (!legacy && get_bcc(&pSMBr->hdr) < 40)
3770 rc = -EIO; /* bad smb */
3771 else if (legacy && get_bcc(&pSMBr->hdr) < 24)
3772 rc = -EIO; /* 24 or 26 expected but we do not read
3776 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3779 * On legacy responses we do not read the last field,
3780 * EAsize, fortunately since it varies by subdialect and
3781 * also note it differs on Set vs Get, ie two bytes or 4
3782 * bytes depending but we don't care here.
3785 size = sizeof(FILE_INFO_STANDARD);
3787 size = sizeof(FILE_ALL_INFO);
3788 memcpy((char *) data, (char *) &pSMBr->hdr.Protocol +
3793 cifs_buf_release(pSMB);
3795 goto QPathInfoRetry;
3801 CIFSSMBUnixQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
3802 u16 netfid, FILE_UNIX_BASIC_INFO *pFindData)
3804 struct smb_t2_qfi_req *pSMB = NULL;
3805 struct smb_t2_qfi_rsp *pSMBr = NULL;
3808 __u16 params, byte_count;
3811 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3816 params = 2 /* level */ + 2 /* fid */;
3817 pSMB->t2.TotalDataCount = 0;
3818 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
3819 /* BB find exact max data count below from sess structure BB */
3820 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
3821 pSMB->t2.MaxSetupCount = 0;
3822 pSMB->t2.Reserved = 0;
3824 pSMB->t2.Timeout = 0;
3825 pSMB->t2.Reserved2 = 0;
3826 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
3828 pSMB->t2.DataCount = 0;
3829 pSMB->t2.DataOffset = 0;
3830 pSMB->t2.SetupCount = 1;
3831 pSMB->t2.Reserved3 = 0;
3832 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
3833 byte_count = params + 1 /* pad */ ;
3834 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
3835 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
3836 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
3839 inc_rfc1001_len(pSMB, byte_count);
3840 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
3842 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3843 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3845 cifs_dbg(FYI, "Send error in UnixQFileInfo = %d\n", rc);
3846 } else { /* decode response */
3847 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
3849 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
3850 cifs_dbg(VFS, "Malformed FILE_UNIX_BASIC_INFO response. Unix Extensions can be disabled on mount by specifying the nosfu mount option.\n");
3851 rc = -EIO; /* bad smb */
3853 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3854 memcpy((char *) pFindData,
3855 (char *) &pSMBr->hdr.Protocol +
3857 sizeof(FILE_UNIX_BASIC_INFO));
3861 cifs_buf_release(pSMB);
3863 goto UnixQFileInfoRetry;
3869 CIFSSMBUnixQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
3870 const unsigned char *searchName,
3871 FILE_UNIX_BASIC_INFO *pFindData,
3872 const struct nls_table *nls_codepage, int remap)
3874 /* SMB_QUERY_FILE_UNIX_BASIC */
3875 TRANSACTION2_QPI_REQ *pSMB = NULL;
3876 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3878 int bytes_returned = 0;
3880 __u16 params, byte_count;
3882 cifs_dbg(FYI, "In QPathInfo (Unix) the path %s\n", searchName);
3884 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3889 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3891 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
3892 PATH_MAX, nls_codepage, remap);
3893 name_len++; /* trailing null */
3896 name_len = copy_path_name(pSMB->FileName, searchName);
3899 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
3900 pSMB->TotalDataCount = 0;
3901 pSMB->MaxParameterCount = cpu_to_le16(2);
3902 /* BB find exact max SMB PDU from sess structure BB */
3903 pSMB->MaxDataCount = cpu_to_le16(4000);
3904 pSMB->MaxSetupCount = 0;
3908 pSMB->Reserved2 = 0;
3909 pSMB->ParameterOffset = cpu_to_le16(offsetof(
3910 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
3911 pSMB->DataCount = 0;
3912 pSMB->DataOffset = 0;
3913 pSMB->SetupCount = 1;
3914 pSMB->Reserved3 = 0;
3915 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3916 byte_count = params + 1 /* pad */ ;
3917 pSMB->TotalParameterCount = cpu_to_le16(params);
3918 pSMB->ParameterCount = pSMB->TotalParameterCount;
3919 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
3920 pSMB->Reserved4 = 0;
3921 inc_rfc1001_len(pSMB, byte_count);
3922 pSMB->ByteCount = cpu_to_le16(byte_count);
3924 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3925 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3927 cifs_dbg(FYI, "Send error in UnixQPathInfo = %d\n", rc);
3928 } else { /* decode response */
3929 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
3931 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
3932 cifs_dbg(VFS, "Malformed FILE_UNIX_BASIC_INFO response. Unix Extensions can be disabled on mount by specifying the nosfu mount option.\n");
3933 rc = -EIO; /* bad smb */
3935 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3936 memcpy((char *) pFindData,
3937 (char *) &pSMBr->hdr.Protocol +
3939 sizeof(FILE_UNIX_BASIC_INFO));
3942 cifs_buf_release(pSMB);
3944 goto UnixQPathInfoRetry;
3949 /* xid, tcon, searchName and codepage are input parms, rest are returned */
3951 CIFSFindFirst(const unsigned int xid, struct cifs_tcon *tcon,
3952 const char *searchName, struct cifs_sb_info *cifs_sb,
3953 __u16 *pnetfid, __u16 search_flags,
3954 struct cifs_search_info *psrch_inf, bool msearch)
3956 /* level 257 SMB_ */
3957 TRANSACTION2_FFIRST_REQ *pSMB = NULL;
3958 TRANSACTION2_FFIRST_RSP *pSMBr = NULL;
3959 T2_FFIRST_RSP_PARMS *parms;
3960 struct nls_table *nls_codepage;
3962 __u16 params, byte_count;
3963 int bytes_returned = 0;
3964 int name_len, remap;
3967 cifs_dbg(FYI, "In FindFirst for %s\n", searchName);
3970 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3975 nls_codepage = cifs_sb->local_nls;
3976 remap = cifs_remap(cifs_sb);
3978 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3980 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
3981 PATH_MAX, nls_codepage, remap);
3982 /* We can not add the asterik earlier in case
3983 it got remapped to 0xF03A as if it were part of the
3984 directory name instead of a wildcard */
3987 pSMB->FileName[name_len] = CIFS_DIR_SEP(cifs_sb);
3988 pSMB->FileName[name_len+1] = 0;
3989 pSMB->FileName[name_len+2] = '*';
3990 pSMB->FileName[name_len+3] = 0;
3991 name_len += 4; /* now the trailing null */
3992 /* null terminate just in case */
3993 pSMB->FileName[name_len] = 0;
3994 pSMB->FileName[name_len+1] = 0;
3998 name_len = copy_path_name(pSMB->FileName, searchName);
4000 if (WARN_ON_ONCE(name_len > PATH_MAX-2))
4001 name_len = PATH_MAX-2;
4002 /* overwrite nul byte */
4003 pSMB->FileName[name_len-1] = CIFS_DIR_SEP(cifs_sb);
4004 pSMB->FileName[name_len] = '*';
4005 pSMB->FileName[name_len+1] = 0;
4010 params = 12 + name_len /* includes null */ ;
4011 pSMB->TotalDataCount = 0; /* no EAs */
4012 pSMB->MaxParameterCount = cpu_to_le16(10);
4013 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
4014 pSMB->MaxSetupCount = 0;
4018 pSMB->Reserved2 = 0;
4019 byte_count = params + 1 /* pad */ ;
4020 pSMB->TotalParameterCount = cpu_to_le16(params);
4021 pSMB->ParameterCount = pSMB->TotalParameterCount;
4022 pSMB->ParameterOffset = cpu_to_le16(
4023 offsetof(struct smb_com_transaction2_ffirst_req, SearchAttributes)
4025 pSMB->DataCount = 0;
4026 pSMB->DataOffset = 0;
4027 pSMB->SetupCount = 1; /* one byte, no need to make endian neutral */
4028 pSMB->Reserved3 = 0;
4029 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_FIRST);
4030 pSMB->SearchAttributes =
4031 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
4033 pSMB->SearchCount = cpu_to_le16(CIFSMaxBufSize/sizeof(FILE_UNIX_INFO));
4034 pSMB->SearchFlags = cpu_to_le16(search_flags);
4035 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4037 /* BB what should we set StorageType to? Does it matter? BB */
4038 pSMB->SearchStorageType = 0;
4039 inc_rfc1001_len(pSMB, byte_count);
4040 pSMB->ByteCount = cpu_to_le16(byte_count);
4042 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4043 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4044 cifs_stats_inc(&tcon->stats.cifs_stats.num_ffirst);
4048 * BB: add logic to retry regular search if Unix search rejected
4049 * unexpectedly by server.
4051 /* BB: add code to handle unsupported level rc */
4052 cifs_dbg(FYI, "Error in FindFirst = %d\n", rc);
4053 cifs_buf_release(pSMB);
4055 * BB: eventually could optimize out free and realloc of buf for
4059 goto findFirstRetry;
4062 /* decode response */
4063 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4065 cifs_buf_release(pSMB);
4069 psrch_inf->unicode = !!(pSMBr->hdr.Flags2 & SMBFLG2_UNICODE);
4070 psrch_inf->ntwrk_buf_start = (char *)pSMBr;
4071 psrch_inf->smallBuf = false;
4072 psrch_inf->srch_entries_start = (char *)&pSMBr->hdr.Protocol +
4073 le16_to_cpu(pSMBr->t2.DataOffset);
4075 parms = (T2_FFIRST_RSP_PARMS *)((char *)&pSMBr->hdr.Protocol +
4076 le16_to_cpu(pSMBr->t2.ParameterOffset));
4077 psrch_inf->endOfSearch = !!parms->EndofSearch;
4079 psrch_inf->entries_in_buffer = le16_to_cpu(parms->SearchCount);
4080 psrch_inf->index_of_last_entry = 2 /* skip . and .. */ +
4081 psrch_inf->entries_in_buffer;
4082 lnoff = le16_to_cpu(parms->LastNameOffset);
4083 if (CIFSMaxBufSize < lnoff) {
4084 cifs_dbg(VFS, "ignoring corrupt resume name\n");
4085 psrch_inf->last_entry = NULL;
4087 psrch_inf->last_entry = psrch_inf->srch_entries_start + lnoff;
4089 *pnetfid = parms->SearchHandle;
4094 int CIFSFindNext(const unsigned int xid, struct cifs_tcon *tcon,
4095 __u16 searchHandle, __u16 search_flags,
4096 struct cifs_search_info *psrch_inf)
4098 TRANSACTION2_FNEXT_REQ *pSMB = NULL;
4099 TRANSACTION2_FNEXT_RSP *pSMBr = NULL;
4100 T2_FNEXT_RSP_PARMS *parms;
4101 unsigned int name_len;
4103 __u16 params, byte_count;
4104 char *response_data;
4108 cifs_dbg(FYI, "In FindNext\n");
4110 if (psrch_inf->endOfSearch)
4113 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4118 params = 14; /* includes 2 bytes of null string, converted to LE below*/
4120 pSMB->TotalDataCount = 0; /* no EAs */
4121 pSMB->MaxParameterCount = cpu_to_le16(8);
4122 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
4123 pSMB->MaxSetupCount = 0;
4127 pSMB->Reserved2 = 0;
4128 pSMB->ParameterOffset = cpu_to_le16(
4129 offsetof(struct smb_com_transaction2_fnext_req,SearchHandle) - 4);
4130 pSMB->DataCount = 0;
4131 pSMB->DataOffset = 0;
4132 pSMB->SetupCount = 1;
4133 pSMB->Reserved3 = 0;
4134 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_NEXT);
4135 pSMB->SearchHandle = searchHandle; /* always kept as le */
4137 cpu_to_le16(CIFSMaxBufSize / sizeof(FILE_UNIX_INFO));
4138 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4139 pSMB->ResumeKey = psrch_inf->resume_key;
4140 pSMB->SearchFlags = cpu_to_le16(search_flags);
4142 name_len = psrch_inf->resume_name_len;
4144 if (name_len < PATH_MAX) {
4145 memcpy(pSMB->ResumeFileName, psrch_inf->presume_name, name_len);
4146 byte_count += name_len;
4147 /* 14 byte parm len above enough for 2 byte null terminator */
4148 pSMB->ResumeFileName[name_len] = 0;
4149 pSMB->ResumeFileName[name_len+1] = 0;
4151 cifs_buf_release(pSMB);
4154 byte_count = params + 1 /* pad */ ;
4155 pSMB->TotalParameterCount = cpu_to_le16(params);
4156 pSMB->ParameterCount = pSMB->TotalParameterCount;
4157 inc_rfc1001_len(pSMB, byte_count);
4158 pSMB->ByteCount = cpu_to_le16(byte_count);
4160 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4161 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4162 cifs_stats_inc(&tcon->stats.cifs_stats.num_fnext);
4165 cifs_buf_release(pSMB);
4167 psrch_inf->endOfSearch = true;
4168 rc = 0; /* search probably was closed at end of search*/
4170 cifs_dbg(FYI, "FindNext returned = %d\n", rc);
4175 /* decode response */
4176 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4178 cifs_buf_release(pSMB);
4181 /* BB fixme add lock for file (srch_info) struct here */
4182 psrch_inf->unicode = !!(pSMBr->hdr.Flags2 & SMBFLG2_UNICODE);
4183 response_data = (char *)&pSMBr->hdr.Protocol +
4184 le16_to_cpu(pSMBr->t2.ParameterOffset);
4185 parms = (T2_FNEXT_RSP_PARMS *)response_data;
4186 response_data = (char *)&pSMBr->hdr.Protocol +
4187 le16_to_cpu(pSMBr->t2.DataOffset);
4189 if (psrch_inf->smallBuf)
4190 cifs_small_buf_release(psrch_inf->ntwrk_buf_start);
4192 cifs_buf_release(psrch_inf->ntwrk_buf_start);
4194 psrch_inf->srch_entries_start = response_data;
4195 psrch_inf->ntwrk_buf_start = (char *)pSMB;
4196 psrch_inf->smallBuf = false;
4197 psrch_inf->endOfSearch = !!parms->EndofSearch;
4198 psrch_inf->entries_in_buffer = le16_to_cpu(parms->SearchCount);
4199 psrch_inf->index_of_last_entry += psrch_inf->entries_in_buffer;
4200 lnoff = le16_to_cpu(parms->LastNameOffset);
4201 if (CIFSMaxBufSize < lnoff) {
4202 cifs_dbg(VFS, "ignoring corrupt resume name\n");
4203 psrch_inf->last_entry = NULL;
4205 psrch_inf->last_entry =
4206 psrch_inf->srch_entries_start + lnoff;
4208 /* BB fixme add unlock here */
4211 * BB: On error, should we leave previous search buf
4212 * (and count and last entry fields) intact or free the previous one?
4214 * Note: On -EAGAIN error only caller can retry on handle based calls
4215 * since file handle passed in no longer valid.
4221 CIFSFindClose(const unsigned int xid, struct cifs_tcon *tcon,
4222 const __u16 searchHandle)
4225 FINDCLOSE_REQ *pSMB = NULL;
4227 cifs_dbg(FYI, "In CIFSSMBFindClose\n");
4228 rc = small_smb_init(SMB_COM_FIND_CLOSE2, 1, tcon, (void **)&pSMB);
4230 /* no sense returning error if session restarted
4231 as file handle has been closed */
4237 pSMB->FileID = searchHandle;
4238 pSMB->ByteCount = 0;
4239 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
4240 cifs_small_buf_release(pSMB);
4242 cifs_dbg(VFS, "Send error in FindClose = %d\n", rc);
4244 cifs_stats_inc(&tcon->stats.cifs_stats.num_fclose);
4246 /* Since session is dead, search handle closed on server already */
4254 CIFSGetSrvInodeNumber(const unsigned int xid, struct cifs_tcon *tcon,
4255 const char *search_name, __u64 *inode_number,
4256 const struct nls_table *nls_codepage, int remap)
4259 TRANSACTION2_QPI_REQ *pSMB = NULL;
4260 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4261 int name_len, bytes_returned;
4262 __u16 params, byte_count;
4264 cifs_dbg(FYI, "In GetSrvInodeNum for %s\n", search_name);
4268 GetInodeNumberRetry:
4269 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4274 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4276 cifsConvertToUTF16((__le16 *) pSMB->FileName,
4277 search_name, PATH_MAX, nls_codepage,
4279 name_len++; /* trailing null */
4282 name_len = copy_path_name(pSMB->FileName, search_name);
4285 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
4286 pSMB->TotalDataCount = 0;
4287 pSMB->MaxParameterCount = cpu_to_le16(2);
4288 /* BB find exact max data count below from sess structure BB */
4289 pSMB->MaxDataCount = cpu_to_le16(4000);
4290 pSMB->MaxSetupCount = 0;
4294 pSMB->Reserved2 = 0;
4295 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4296 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
4297 pSMB->DataCount = 0;
4298 pSMB->DataOffset = 0;
4299 pSMB->SetupCount = 1;
4300 pSMB->Reserved3 = 0;
4301 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4302 byte_count = params + 1 /* pad */ ;
4303 pSMB->TotalParameterCount = cpu_to_le16(params);
4304 pSMB->ParameterCount = pSMB->TotalParameterCount;
4305 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_INTERNAL_INFO);
4306 pSMB->Reserved4 = 0;
4307 inc_rfc1001_len(pSMB, byte_count);
4308 pSMB->ByteCount = cpu_to_le16(byte_count);
4310 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4311 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4313 cifs_dbg(FYI, "error %d in QueryInternalInfo\n", rc);
4315 /* decode response */
4316 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4317 /* BB also check enough total bytes returned */
4318 if (rc || get_bcc(&pSMBr->hdr) < 2)
4319 /* If rc should we check for EOPNOSUPP and
4320 disable the srvino flag? or in caller? */
4321 rc = -EIO; /* bad smb */
4323 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4324 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
4325 struct file_internal_info *pfinfo;
4326 /* BB Do we need a cast or hash here ? */
4328 cifs_dbg(FYI, "Invalid size ret in QryIntrnlInf\n");
4330 goto GetInodeNumOut;
4332 pfinfo = (struct file_internal_info *)
4333 (data_offset + (char *) &pSMBr->hdr.Protocol);
4334 *inode_number = le64_to_cpu(pfinfo->UniqueId);
4338 cifs_buf_release(pSMB);
4340 goto GetInodeNumberRetry;
4345 CIFSGetDFSRefer(const unsigned int xid, struct cifs_ses *ses,
4346 const char *search_name, struct dfs_info3_param **target_nodes,
4347 unsigned int *num_of_nodes,
4348 const struct nls_table *nls_codepage, int remap)
4350 /* TRANS2_GET_DFS_REFERRAL */
4351 TRANSACTION2_GET_DFS_REFER_REQ *pSMB = NULL;
4352 TRANSACTION2_GET_DFS_REFER_RSP *pSMBr = NULL;
4356 __u16 params, byte_count;
4358 *target_nodes = NULL;
4360 cifs_dbg(FYI, "In GetDFSRefer the path %s\n", search_name);
4361 if (ses == NULL || ses->tcon_ipc == NULL)
4366 * Use smb_init_no_reconnect() instead of smb_init() as
4367 * CIFSGetDFSRefer() may be called from cifs_reconnect_tcon() and thus
4368 * causing an infinite recursion.
4370 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, ses->tcon_ipc,
4371 (void **)&pSMB, (void **)&pSMBr);
4375 /* server pointer checked in called function,
4376 but should never be null here anyway */
4377 pSMB->hdr.Mid = get_next_mid(ses->server);
4378 pSMB->hdr.Tid = ses->tcon_ipc->tid;
4379 pSMB->hdr.Uid = ses->Suid;
4380 if (ses->capabilities & CAP_STATUS32)
4381 pSMB->hdr.Flags2 |= SMBFLG2_ERR_STATUS;
4382 if (ses->capabilities & CAP_DFS)
4383 pSMB->hdr.Flags2 |= SMBFLG2_DFS;
4385 if (ses->capabilities & CAP_UNICODE) {
4386 pSMB->hdr.Flags2 |= SMBFLG2_UNICODE;
4388 cifsConvertToUTF16((__le16 *) pSMB->RequestFileName,
4389 search_name, PATH_MAX, nls_codepage,
4391 name_len++; /* trailing null */
4393 } else { /* BB improve the check for buffer overruns BB */
4394 name_len = copy_path_name(pSMB->RequestFileName, search_name);
4397 if (ses->server->sign)
4398 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
4400 pSMB->hdr.Uid = ses->Suid;
4402 params = 2 /* level */ + name_len /*includes null */ ;
4403 pSMB->TotalDataCount = 0;
4404 pSMB->DataCount = 0;
4405 pSMB->DataOffset = 0;
4406 pSMB->MaxParameterCount = 0;
4407 /* BB find exact max SMB PDU from sess structure BB */
4408 pSMB->MaxDataCount = cpu_to_le16(4000);
4409 pSMB->MaxSetupCount = 0;
4413 pSMB->Reserved2 = 0;
4414 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4415 struct smb_com_transaction2_get_dfs_refer_req, MaxReferralLevel) - 4);
4416 pSMB->SetupCount = 1;
4417 pSMB->Reserved3 = 0;
4418 pSMB->SubCommand = cpu_to_le16(TRANS2_GET_DFS_REFERRAL);
4419 byte_count = params + 3 /* pad */ ;
4420 pSMB->ParameterCount = cpu_to_le16(params);
4421 pSMB->TotalParameterCount = pSMB->ParameterCount;
4422 pSMB->MaxReferralLevel = cpu_to_le16(3);
4423 inc_rfc1001_len(pSMB, byte_count);
4424 pSMB->ByteCount = cpu_to_le16(byte_count);
4426 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
4427 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4429 cifs_dbg(FYI, "Send error in GetDFSRefer = %d\n", rc);
4432 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4434 /* BB Also check if enough total bytes returned? */
4435 if (rc || get_bcc(&pSMBr->hdr) < 17) {
4436 rc = -EIO; /* bad smb */
4440 cifs_dbg(FYI, "Decoding GetDFSRefer response BCC: %d Offset %d\n",
4441 get_bcc(&pSMBr->hdr), le16_to_cpu(pSMBr->t2.DataOffset));
4443 /* parse returned result into more usable form */
4444 rc = parse_dfs_referrals(&pSMBr->dfs_data,
4445 le16_to_cpu(pSMBr->t2.DataCount),
4446 num_of_nodes, target_nodes, nls_codepage,
4448 (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE) != 0);
4451 cifs_buf_release(pSMB);
4459 /* Query File System Info such as free space to old servers such as Win 9x */
4461 SMBOldQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
4462 struct kstatfs *FSData)
4464 /* level 0x01 SMB_QUERY_FILE_SYSTEM_INFO */
4465 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4466 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4467 FILE_SYSTEM_ALLOC_INFO *response_data;
4469 int bytes_returned = 0;
4470 __u16 params, byte_count;
4472 cifs_dbg(FYI, "OldQFSInfo\n");
4474 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4479 params = 2; /* level */
4480 pSMB->TotalDataCount = 0;
4481 pSMB->MaxParameterCount = cpu_to_le16(2);
4482 pSMB->MaxDataCount = cpu_to_le16(1000);
4483 pSMB->MaxSetupCount = 0;
4487 pSMB->Reserved2 = 0;
4488 byte_count = params + 1 /* pad */ ;
4489 pSMB->TotalParameterCount = cpu_to_le16(params);
4490 pSMB->ParameterCount = pSMB->TotalParameterCount;
4491 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4492 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
4493 pSMB->DataCount = 0;
4494 pSMB->DataOffset = 0;
4495 pSMB->SetupCount = 1;
4496 pSMB->Reserved3 = 0;
4497 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
4498 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_ALLOCATION);
4499 inc_rfc1001_len(pSMB, byte_count);
4500 pSMB->ByteCount = cpu_to_le16(byte_count);
4502 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4503 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4505 cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
4506 } else { /* decode response */
4507 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4509 if (rc || get_bcc(&pSMBr->hdr) < 18)
4510 rc = -EIO; /* bad smb */
4512 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4513 cifs_dbg(FYI, "qfsinf resp BCC: %d Offset %d\n",
4514 get_bcc(&pSMBr->hdr), data_offset);
4516 response_data = (FILE_SYSTEM_ALLOC_INFO *)
4517 (((char *) &pSMBr->hdr.Protocol) + data_offset);
4519 le16_to_cpu(response_data->BytesPerSector) *
4520 le32_to_cpu(response_data->
4521 SectorsPerAllocationUnit);
4523 * much prefer larger but if server doesn't report
4524 * a valid size than 4K is a reasonable minimum
4526 if (FSData->f_bsize < 512)
4527 FSData->f_bsize = 4096;
4530 le32_to_cpu(response_data->TotalAllocationUnits);
4531 FSData->f_bfree = FSData->f_bavail =
4532 le32_to_cpu(response_data->FreeAllocationUnits);
4533 cifs_dbg(FYI, "Blocks: %lld Free: %lld Block size %ld\n",
4534 (unsigned long long)FSData->f_blocks,
4535 (unsigned long long)FSData->f_bfree,
4539 cifs_buf_release(pSMB);
4542 goto oldQFSInfoRetry;
4548 CIFSSMBQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
4549 struct kstatfs *FSData)
4551 /* level 0x103 SMB_QUERY_FILE_SYSTEM_INFO */
4552 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4553 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4554 FILE_SYSTEM_INFO *response_data;
4556 int bytes_returned = 0;
4557 __u16 params, byte_count;
4559 cifs_dbg(FYI, "In QFSInfo\n");
4561 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4566 params = 2; /* level */
4567 pSMB->TotalDataCount = 0;
4568 pSMB->MaxParameterCount = cpu_to_le16(2);
4569 pSMB->MaxDataCount = cpu_to_le16(1000);
4570 pSMB->MaxSetupCount = 0;
4574 pSMB->Reserved2 = 0;
4575 byte_count = params + 1 /* pad */ ;
4576 pSMB->TotalParameterCount = cpu_to_le16(params);
4577 pSMB->ParameterCount = pSMB->TotalParameterCount;
4578 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4579 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
4580 pSMB->DataCount = 0;
4581 pSMB->DataOffset = 0;
4582 pSMB->SetupCount = 1;
4583 pSMB->Reserved3 = 0;
4584 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
4585 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_SIZE_INFO);
4586 inc_rfc1001_len(pSMB, byte_count);
4587 pSMB->ByteCount = cpu_to_le16(byte_count);
4589 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4590 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4592 cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
4593 } else { /* decode response */
4594 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4596 if (rc || get_bcc(&pSMBr->hdr) < 24)
4597 rc = -EIO; /* bad smb */
4599 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4603 *) (((char *) &pSMBr->hdr.Protocol) +
4606 le32_to_cpu(response_data->BytesPerSector) *
4607 le32_to_cpu(response_data->
4608 SectorsPerAllocationUnit);
4610 * much prefer larger but if server doesn't report
4611 * a valid size than 4K is a reasonable minimum
4613 if (FSData->f_bsize < 512)
4614 FSData->f_bsize = 4096;
4617 le64_to_cpu(response_data->TotalAllocationUnits);
4618 FSData->f_bfree = FSData->f_bavail =
4619 le64_to_cpu(response_data->FreeAllocationUnits);
4620 cifs_dbg(FYI, "Blocks: %lld Free: %lld Block size %ld\n",
4621 (unsigned long long)FSData->f_blocks,
4622 (unsigned long long)FSData->f_bfree,
4626 cifs_buf_release(pSMB);
4635 CIFSSMBQFSAttributeInfo(const unsigned int xid, struct cifs_tcon *tcon)
4637 /* level 0x105 SMB_QUERY_FILE_SYSTEM_INFO */
4638 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4639 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4640 FILE_SYSTEM_ATTRIBUTE_INFO *response_data;
4642 int bytes_returned = 0;
4643 __u16 params, byte_count;
4645 cifs_dbg(FYI, "In QFSAttributeInfo\n");
4647 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4652 params = 2; /* level */
4653 pSMB->TotalDataCount = 0;
4654 pSMB->MaxParameterCount = cpu_to_le16(2);
4655 /* BB find exact max SMB PDU from sess structure BB */
4656 pSMB->MaxDataCount = cpu_to_le16(1000);
4657 pSMB->MaxSetupCount = 0;
4661 pSMB->Reserved2 = 0;
4662 byte_count = params + 1 /* pad */ ;
4663 pSMB->TotalParameterCount = cpu_to_le16(params);
4664 pSMB->ParameterCount = pSMB->TotalParameterCount;
4665 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4666 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
4667 pSMB->DataCount = 0;
4668 pSMB->DataOffset = 0;
4669 pSMB->SetupCount = 1;
4670 pSMB->Reserved3 = 0;
4671 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
4672 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_ATTRIBUTE_INFO);
4673 inc_rfc1001_len(pSMB, byte_count);
4674 pSMB->ByteCount = cpu_to_le16(byte_count);
4676 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4677 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4679 cifs_dbg(VFS, "Send error in QFSAttributeInfo = %d\n", rc);
4680 } else { /* decode response */
4681 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4683 if (rc || get_bcc(&pSMBr->hdr) < 13) {
4684 /* BB also check if enough bytes returned */
4685 rc = -EIO; /* bad smb */
4687 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4689 (FILE_SYSTEM_ATTRIBUTE_INFO
4690 *) (((char *) &pSMBr->hdr.Protocol) +
4692 memcpy(&tcon->fsAttrInfo, response_data,
4693 sizeof(FILE_SYSTEM_ATTRIBUTE_INFO));
4696 cifs_buf_release(pSMB);
4699 goto QFSAttributeRetry;
4705 CIFSSMBQFSDeviceInfo(const unsigned int xid, struct cifs_tcon *tcon)
4707 /* level 0x104 SMB_QUERY_FILE_SYSTEM_INFO */
4708 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4709 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4710 FILE_SYSTEM_DEVICE_INFO *response_data;
4712 int bytes_returned = 0;
4713 __u16 params, byte_count;
4715 cifs_dbg(FYI, "In QFSDeviceInfo\n");
4717 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4722 params = 2; /* level */
4723 pSMB->TotalDataCount = 0;
4724 pSMB->MaxParameterCount = cpu_to_le16(2);
4725 /* BB find exact max SMB PDU from sess structure BB */
4726 pSMB->MaxDataCount = cpu_to_le16(1000);
4727 pSMB->MaxSetupCount = 0;
4731 pSMB->Reserved2 = 0;
4732 byte_count = params + 1 /* pad */ ;
4733 pSMB->TotalParameterCount = cpu_to_le16(params);
4734 pSMB->ParameterCount = pSMB->TotalParameterCount;
4735 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4736 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
4738 pSMB->DataCount = 0;
4739 pSMB->DataOffset = 0;
4740 pSMB->SetupCount = 1;
4741 pSMB->Reserved3 = 0;
4742 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
4743 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_DEVICE_INFO);
4744 inc_rfc1001_len(pSMB, byte_count);
4745 pSMB->ByteCount = cpu_to_le16(byte_count);
4747 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4748 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4750 cifs_dbg(FYI, "Send error in QFSDeviceInfo = %d\n", rc);
4751 } else { /* decode response */
4752 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4754 if (rc || get_bcc(&pSMBr->hdr) <
4755 sizeof(FILE_SYSTEM_DEVICE_INFO))
4756 rc = -EIO; /* bad smb */
4758 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4760 (FILE_SYSTEM_DEVICE_INFO *)
4761 (((char *) &pSMBr->hdr.Protocol) +
4763 memcpy(&tcon->fsDevInfo, response_data,
4764 sizeof(FILE_SYSTEM_DEVICE_INFO));
4767 cifs_buf_release(pSMB);
4770 goto QFSDeviceRetry;
4776 CIFSSMBQFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon)
4778 /* level 0x200 SMB_QUERY_CIFS_UNIX_INFO */
4779 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4780 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4781 FILE_SYSTEM_UNIX_INFO *response_data;
4783 int bytes_returned = 0;
4784 __u16 params, byte_count;
4786 cifs_dbg(FYI, "In QFSUnixInfo\n");
4788 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
4789 (void **) &pSMB, (void **) &pSMBr);
4793 params = 2; /* level */
4794 pSMB->TotalDataCount = 0;
4795 pSMB->DataCount = 0;
4796 pSMB->DataOffset = 0;
4797 pSMB->MaxParameterCount = cpu_to_le16(2);
4798 /* BB find exact max SMB PDU from sess structure BB */
4799 pSMB->MaxDataCount = cpu_to_le16(100);
4800 pSMB->MaxSetupCount = 0;
4804 pSMB->Reserved2 = 0;
4805 byte_count = params + 1 /* pad */ ;
4806 pSMB->ParameterCount = cpu_to_le16(params);
4807 pSMB->TotalParameterCount = pSMB->ParameterCount;
4808 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
4809 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
4810 pSMB->SetupCount = 1;
4811 pSMB->Reserved3 = 0;
4812 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
4813 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_CIFS_UNIX_INFO);
4814 inc_rfc1001_len(pSMB, byte_count);
4815 pSMB->ByteCount = cpu_to_le16(byte_count);
4817 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4818 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4820 cifs_dbg(VFS, "Send error in QFSUnixInfo = %d\n", rc);
4821 } else { /* decode response */
4822 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4824 if (rc || get_bcc(&pSMBr->hdr) < 13) {
4825 rc = -EIO; /* bad smb */
4827 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4829 (FILE_SYSTEM_UNIX_INFO
4830 *) (((char *) &pSMBr->hdr.Protocol) +
4832 memcpy(&tcon->fsUnixInfo, response_data,
4833 sizeof(FILE_SYSTEM_UNIX_INFO));
4836 cifs_buf_release(pSMB);
4846 CIFSSMBSetFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon, __u64 cap)
4848 /* level 0x200 SMB_SET_CIFS_UNIX_INFO */
4849 TRANSACTION2_SETFSI_REQ *pSMB = NULL;
4850 TRANSACTION2_SETFSI_RSP *pSMBr = NULL;
4852 int bytes_returned = 0;
4853 __u16 params, param_offset, offset, byte_count;
4855 cifs_dbg(FYI, "In SETFSUnixInfo\n");
4857 /* BB switch to small buf init to save memory */
4858 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
4859 (void **) &pSMB, (void **) &pSMBr);
4863 params = 4; /* 2 bytes zero followed by info level. */
4864 pSMB->MaxSetupCount = 0;
4868 pSMB->Reserved2 = 0;
4869 param_offset = offsetof(struct smb_com_transaction2_setfsi_req, FileNum)
4871 offset = param_offset + params;
4873 pSMB->MaxParameterCount = cpu_to_le16(4);
4874 /* BB find exact max SMB PDU from sess structure BB */
4875 pSMB->MaxDataCount = cpu_to_le16(100);
4876 pSMB->SetupCount = 1;
4877 pSMB->Reserved3 = 0;
4878 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FS_INFORMATION);
4879 byte_count = 1 /* pad */ + params + 12;
4881 pSMB->DataCount = cpu_to_le16(12);
4882 pSMB->ParameterCount = cpu_to_le16(params);
4883 pSMB->TotalDataCount = pSMB->DataCount;
4884 pSMB->TotalParameterCount = pSMB->ParameterCount;
4885 pSMB->ParameterOffset = cpu_to_le16(param_offset);
4886 pSMB->DataOffset = cpu_to_le16(offset);
4890 pSMB->InformationLevel = cpu_to_le16(SMB_SET_CIFS_UNIX_INFO);
4893 pSMB->ClientUnixMajor = cpu_to_le16(CIFS_UNIX_MAJOR_VERSION);
4894 pSMB->ClientUnixMinor = cpu_to_le16(CIFS_UNIX_MINOR_VERSION);
4895 pSMB->ClientUnixCap = cpu_to_le64(cap);
4897 inc_rfc1001_len(pSMB, byte_count);
4898 pSMB->ByteCount = cpu_to_le16(byte_count);
4900 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4901 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4903 cifs_dbg(VFS, "Send error in SETFSUnixInfo = %d\n", rc);
4904 } else { /* decode response */
4905 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4907 rc = -EIO; /* bad smb */
4909 cifs_buf_release(pSMB);
4912 goto SETFSUnixRetry;
4920 CIFSSMBQFSPosixInfo(const unsigned int xid, struct cifs_tcon *tcon,
4921 struct kstatfs *FSData)
4923 /* level 0x201 SMB_QUERY_CIFS_POSIX_INFO */
4924 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4925 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4926 FILE_SYSTEM_POSIX_INFO *response_data;
4928 int bytes_returned = 0;
4929 __u16 params, byte_count;
4931 cifs_dbg(FYI, "In QFSPosixInfo\n");
4933 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4938 params = 2; /* level */
4939 pSMB->TotalDataCount = 0;
4940 pSMB->DataCount = 0;
4941 pSMB->DataOffset = 0;
4942 pSMB->MaxParameterCount = cpu_to_le16(2);
4943 /* BB find exact max SMB PDU from sess structure BB */
4944 pSMB->MaxDataCount = cpu_to_le16(100);
4945 pSMB->MaxSetupCount = 0;
4949 pSMB->Reserved2 = 0;
4950 byte_count = params + 1 /* pad */ ;
4951 pSMB->ParameterCount = cpu_to_le16(params);
4952 pSMB->TotalParameterCount = pSMB->ParameterCount;
4953 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
4954 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
4955 pSMB->SetupCount = 1;
4956 pSMB->Reserved3 = 0;
4957 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
4958 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_FS_INFO);
4959 inc_rfc1001_len(pSMB, byte_count);
4960 pSMB->ByteCount = cpu_to_le16(byte_count);
4962 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4963 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4965 cifs_dbg(FYI, "Send error in QFSUnixInfo = %d\n", rc);
4966 } else { /* decode response */
4967 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4969 if (rc || get_bcc(&pSMBr->hdr) < 13) {
4970 rc = -EIO; /* bad smb */
4972 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4974 (FILE_SYSTEM_POSIX_INFO
4975 *) (((char *) &pSMBr->hdr.Protocol) +
4978 le32_to_cpu(response_data->BlockSize);
4980 * much prefer larger but if server doesn't report
4981 * a valid size than 4K is a reasonable minimum
4983 if (FSData->f_bsize < 512)
4984 FSData->f_bsize = 4096;
4987 le64_to_cpu(response_data->TotalBlocks);
4989 le64_to_cpu(response_data->BlocksAvail);
4990 if (response_data->UserBlocksAvail == cpu_to_le64(-1)) {
4991 FSData->f_bavail = FSData->f_bfree;
4994 le64_to_cpu(response_data->UserBlocksAvail);
4996 if (response_data->TotalFileNodes != cpu_to_le64(-1))
4998 le64_to_cpu(response_data->TotalFileNodes);
4999 if (response_data->FreeFileNodes != cpu_to_le64(-1))
5001 le64_to_cpu(response_data->FreeFileNodes);
5004 cifs_buf_release(pSMB);
5014 * We can not use write of zero bytes trick to set file size due to need for
5015 * large file support. Also note that this SetPathInfo is preferred to
5016 * SetFileInfo based method in next routine which is only needed to work around
5017 * a sharing violation bugin Samba which this routine can run into.
5020 CIFSSMBSetEOF(const unsigned int xid, struct cifs_tcon *tcon,
5021 const char *file_name, __u64 size, struct cifs_sb_info *cifs_sb,
5022 bool set_allocation)
5024 struct smb_com_transaction2_spi_req *pSMB = NULL;
5025 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
5026 struct file_end_of_file_info *parm_data;
5029 int bytes_returned = 0;
5030 int remap = cifs_remap(cifs_sb);
5032 __u16 params, byte_count, data_count, param_offset, offset;
5034 cifs_dbg(FYI, "In SetEOF\n");
5036 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5041 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5043 cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
5044 PATH_MAX, cifs_sb->local_nls, remap);
5045 name_len++; /* trailing null */
5048 name_len = copy_path_name(pSMB->FileName, file_name);
5050 params = 6 + name_len;
5051 data_count = sizeof(struct file_end_of_file_info);
5052 pSMB->MaxParameterCount = cpu_to_le16(2);
5053 pSMB->MaxDataCount = cpu_to_le16(4100);
5054 pSMB->MaxSetupCount = 0;
5058 pSMB->Reserved2 = 0;
5059 param_offset = offsetof(struct smb_com_transaction2_spi_req,
5060 InformationLevel) - 4;
5061 offset = param_offset + params;
5062 if (set_allocation) {
5063 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5064 pSMB->InformationLevel =
5065 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5067 pSMB->InformationLevel =
5068 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
5069 } else /* Set File Size */ {
5070 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5071 pSMB->InformationLevel =
5072 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
5074 pSMB->InformationLevel =
5075 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
5079 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol) +
5081 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5082 pSMB->DataOffset = cpu_to_le16(offset);
5083 pSMB->SetupCount = 1;
5084 pSMB->Reserved3 = 0;
5085 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5086 byte_count = 3 /* pad */ + params + data_count;
5087 pSMB->DataCount = cpu_to_le16(data_count);
5088 pSMB->TotalDataCount = pSMB->DataCount;
5089 pSMB->ParameterCount = cpu_to_le16(params);
5090 pSMB->TotalParameterCount = pSMB->ParameterCount;
5091 pSMB->Reserved4 = 0;
5092 inc_rfc1001_len(pSMB, byte_count);
5093 parm_data->FileSize = cpu_to_le64(size);
5094 pSMB->ByteCount = cpu_to_le16(byte_count);
5095 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5096 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5098 cifs_dbg(FYI, "SetPathInfo (file size) returned %d\n", rc);
5100 cifs_buf_release(pSMB);
5109 CIFSSMBSetFileSize(const unsigned int xid, struct cifs_tcon *tcon,
5110 struct cifsFileInfo *cfile, __u64 size, bool set_allocation)
5112 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5113 struct file_end_of_file_info *parm_data;
5115 __u16 params, param_offset, offset, byte_count, count;
5117 cifs_dbg(FYI, "SetFileSize (via SetFileInfo) %lld\n",
5119 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5124 pSMB->hdr.Pid = cpu_to_le16((__u16)cfile->pid);
5125 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(cfile->pid >> 16));
5128 pSMB->MaxSetupCount = 0;
5132 pSMB->Reserved2 = 0;
5133 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5134 offset = param_offset + params;
5136 count = sizeof(struct file_end_of_file_info);
5137 pSMB->MaxParameterCount = cpu_to_le16(2);
5138 /* BB find exact max SMB PDU from sess structure BB */
5139 pSMB->MaxDataCount = cpu_to_le16(1000);
5140 pSMB->SetupCount = 1;
5141 pSMB->Reserved3 = 0;
5142 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5143 byte_count = 3 /* pad */ + params + count;
5144 pSMB->DataCount = cpu_to_le16(count);
5145 pSMB->ParameterCount = cpu_to_le16(params);
5146 pSMB->TotalDataCount = pSMB->DataCount;
5147 pSMB->TotalParameterCount = pSMB->ParameterCount;
5148 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5149 /* SMB offsets are from the beginning of SMB which is 4 bytes in, after RFC1001 field */
5151 (struct file_end_of_file_info *)(((char *)pSMB) + offset + 4);
5152 pSMB->DataOffset = cpu_to_le16(offset);
5153 parm_data->FileSize = cpu_to_le64(size);
5154 pSMB->Fid = cfile->fid.netfid;
5155 if (set_allocation) {
5156 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5157 pSMB->InformationLevel =
5158 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5160 pSMB->InformationLevel =
5161 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
5162 } else /* Set File Size */ {
5163 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5164 pSMB->InformationLevel =
5165 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
5167 pSMB->InformationLevel =
5168 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
5170 pSMB->Reserved4 = 0;
5171 inc_rfc1001_len(pSMB, byte_count);
5172 pSMB->ByteCount = cpu_to_le16(byte_count);
5173 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
5174 cifs_small_buf_release(pSMB);
5176 cifs_dbg(FYI, "Send error in SetFileInfo (SetFileSize) = %d\n",
5180 /* Note: On -EAGAIN error only caller can retry on handle based calls
5181 since file handle passed in no longer valid */
5186 /* Some legacy servers such as NT4 require that the file times be set on
5187 an open handle, rather than by pathname - this is awkward due to
5188 potential access conflicts on the open, but it is unavoidable for these
5189 old servers since the only other choice is to go from 100 nanosecond DCE
5190 time and resort to the original setpathinfo level which takes the ancient
5191 DOS time format with 2 second granularity */
5193 CIFSSMBSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
5194 const FILE_BASIC_INFO *data, __u16 fid, __u32 pid_of_opener)
5196 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5199 __u16 params, param_offset, offset, byte_count, count;
5201 cifs_dbg(FYI, "Set Times (via SetFileInfo)\n");
5202 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5207 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5208 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5211 pSMB->MaxSetupCount = 0;
5215 pSMB->Reserved2 = 0;
5216 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5217 offset = param_offset + params;
5219 data_offset = (char *)pSMB +
5220 offsetof(struct smb_hdr, Protocol) + offset;
5222 count = sizeof(FILE_BASIC_INFO);
5223 pSMB->MaxParameterCount = cpu_to_le16(2);
5224 /* BB find max SMB PDU from sess */
5225 pSMB->MaxDataCount = cpu_to_le16(1000);
5226 pSMB->SetupCount = 1;
5227 pSMB->Reserved3 = 0;
5228 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5229 byte_count = 3 /* pad */ + params + count;
5230 pSMB->DataCount = cpu_to_le16(count);
5231 pSMB->ParameterCount = cpu_to_le16(params);
5232 pSMB->TotalDataCount = pSMB->DataCount;
5233 pSMB->TotalParameterCount = pSMB->ParameterCount;
5234 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5235 pSMB->DataOffset = cpu_to_le16(offset);
5237 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5238 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5240 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5241 pSMB->Reserved4 = 0;
5242 inc_rfc1001_len(pSMB, byte_count);
5243 pSMB->ByteCount = cpu_to_le16(byte_count);
5244 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
5245 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
5246 cifs_small_buf_release(pSMB);
5248 cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n",
5251 /* Note: On -EAGAIN error only caller can retry on handle based calls
5252 since file handle passed in no longer valid */
5258 CIFSSMBSetFileDisposition(const unsigned int xid, struct cifs_tcon *tcon,
5259 bool delete_file, __u16 fid, __u32 pid_of_opener)
5261 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5264 __u16 params, param_offset, offset, byte_count, count;
5266 cifs_dbg(FYI, "Set File Disposition (via SetFileInfo)\n");
5267 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5272 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5273 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5276 pSMB->MaxSetupCount = 0;
5280 pSMB->Reserved2 = 0;
5281 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5282 offset = param_offset + params;
5284 /* SMB offsets are from the beginning of SMB which is 4 bytes in, after RFC1001 field */
5285 data_offset = (char *)(pSMB) + offset + 4;
5288 pSMB->MaxParameterCount = cpu_to_le16(2);
5289 /* BB find max SMB PDU from sess */
5290 pSMB->MaxDataCount = cpu_to_le16(1000);
5291 pSMB->SetupCount = 1;
5292 pSMB->Reserved3 = 0;
5293 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5294 byte_count = 3 /* pad */ + params + count;
5295 pSMB->DataCount = cpu_to_le16(count);
5296 pSMB->ParameterCount = cpu_to_le16(params);
5297 pSMB->TotalDataCount = pSMB->DataCount;
5298 pSMB->TotalParameterCount = pSMB->ParameterCount;
5299 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5300 pSMB->DataOffset = cpu_to_le16(offset);
5302 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_DISPOSITION_INFO);
5303 pSMB->Reserved4 = 0;
5304 inc_rfc1001_len(pSMB, byte_count);
5305 pSMB->ByteCount = cpu_to_le16(byte_count);
5306 *data_offset = delete_file ? 1 : 0;
5307 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
5308 cifs_small_buf_release(pSMB);
5310 cifs_dbg(FYI, "Send error in SetFileDisposition = %d\n", rc);
5316 CIFSSMBSetPathInfoFB(const unsigned int xid, struct cifs_tcon *tcon,
5317 const char *fileName, const FILE_BASIC_INFO *data,
5318 const struct nls_table *nls_codepage,
5319 struct cifs_sb_info *cifs_sb)
5322 struct cifs_open_parms oparms;
5323 struct cifs_fid fid;
5326 oparms = (struct cifs_open_parms) {
5329 .desired_access = GENERIC_WRITE,
5330 .create_options = cifs_create_options(cifs_sb, 0),
5331 .disposition = FILE_OPEN,
5336 rc = CIFS_open(xid, &oparms, &oplock, NULL);
5340 rc = CIFSSMBSetFileInfo(xid, tcon, data, fid.netfid, current->tgid);
5341 CIFSSMBClose(xid, tcon, fid.netfid);
5348 CIFSSMBSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
5349 const char *fileName, const FILE_BASIC_INFO *data,
5350 const struct nls_table *nls_codepage,
5351 struct cifs_sb_info *cifs_sb)
5353 TRANSACTION2_SPI_REQ *pSMB = NULL;
5354 TRANSACTION2_SPI_RSP *pSMBr = NULL;
5357 int bytes_returned = 0;
5359 __u16 params, param_offset, offset, byte_count, count;
5360 int remap = cifs_remap(cifs_sb);
5362 cifs_dbg(FYI, "In SetTimes\n");
5365 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5370 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5372 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
5373 PATH_MAX, nls_codepage, remap);
5374 name_len++; /* trailing null */
5377 name_len = copy_path_name(pSMB->FileName, fileName);
5380 params = 6 + name_len;
5381 count = sizeof(FILE_BASIC_INFO);
5382 pSMB->MaxParameterCount = cpu_to_le16(2);
5383 /* BB find max SMB PDU from sess structure BB */
5384 pSMB->MaxDataCount = cpu_to_le16(1000);
5385 pSMB->MaxSetupCount = 0;
5389 pSMB->Reserved2 = 0;
5390 param_offset = offsetof(struct smb_com_transaction2_spi_req,
5391 InformationLevel) - 4;
5392 offset = param_offset + params;
5393 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5394 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5395 pSMB->DataOffset = cpu_to_le16(offset);
5396 pSMB->SetupCount = 1;
5397 pSMB->Reserved3 = 0;
5398 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5399 byte_count = 3 /* pad */ + params + count;
5401 pSMB->DataCount = cpu_to_le16(count);
5402 pSMB->ParameterCount = cpu_to_le16(params);
5403 pSMB->TotalDataCount = pSMB->DataCount;
5404 pSMB->TotalParameterCount = pSMB->ParameterCount;
5405 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5406 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5408 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5409 pSMB->Reserved4 = 0;
5410 inc_rfc1001_len(pSMB, byte_count);
5411 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
5412 pSMB->ByteCount = cpu_to_le16(byte_count);
5413 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5414 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5416 cifs_dbg(FYI, "SetPathInfo (times) returned %d\n", rc);
5418 cifs_buf_release(pSMB);
5423 if (rc == -EOPNOTSUPP)
5424 return CIFSSMBSetPathInfoFB(xid, tcon, fileName, data,
5425 nls_codepage, cifs_sb);
5431 cifs_fill_unix_set_info(FILE_UNIX_BASIC_INFO *data_offset,
5432 const struct cifs_unix_set_info_args *args)
5434 u64 uid = NO_CHANGE_64, gid = NO_CHANGE_64;
5435 u64 mode = args->mode;
5437 if (uid_valid(args->uid))
5438 uid = from_kuid(&init_user_ns, args->uid);
5439 if (gid_valid(args->gid))
5440 gid = from_kgid(&init_user_ns, args->gid);
5443 * Samba server ignores set of file size to zero due to bugs in some
5444 * older clients, but we should be precise - we use SetFileSize to
5445 * set file size and do not want to truncate file size to zero
5446 * accidentally as happened on one Samba server beta by putting
5447 * zero instead of -1 here
5449 data_offset->EndOfFile = cpu_to_le64(NO_CHANGE_64);
5450 data_offset->NumOfBytes = cpu_to_le64(NO_CHANGE_64);
5451 data_offset->LastStatusChange = cpu_to_le64(args->ctime);
5452 data_offset->LastAccessTime = cpu_to_le64(args->atime);
5453 data_offset->LastModificationTime = cpu_to_le64(args->mtime);
5454 data_offset->Uid = cpu_to_le64(uid);
5455 data_offset->Gid = cpu_to_le64(gid);
5456 /* better to leave device as zero when it is */
5457 data_offset->DevMajor = cpu_to_le64(MAJOR(args->device));
5458 data_offset->DevMinor = cpu_to_le64(MINOR(args->device));
5459 data_offset->Permissions = cpu_to_le64(mode);
5462 data_offset->Type = cpu_to_le32(UNIX_FILE);
5463 else if (S_ISDIR(mode))
5464 data_offset->Type = cpu_to_le32(UNIX_DIR);
5465 else if (S_ISLNK(mode))
5466 data_offset->Type = cpu_to_le32(UNIX_SYMLINK);
5467 else if (S_ISCHR(mode))
5468 data_offset->Type = cpu_to_le32(UNIX_CHARDEV);
5469 else if (S_ISBLK(mode))
5470 data_offset->Type = cpu_to_le32(UNIX_BLOCKDEV);
5471 else if (S_ISFIFO(mode))
5472 data_offset->Type = cpu_to_le32(UNIX_FIFO);
5473 else if (S_ISSOCK(mode))
5474 data_offset->Type = cpu_to_le32(UNIX_SOCKET);
5478 CIFSSMBUnixSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
5479 const struct cifs_unix_set_info_args *args,
5480 u16 fid, u32 pid_of_opener)
5482 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5485 u16 params, param_offset, offset, byte_count, count;
5487 cifs_dbg(FYI, "Set Unix Info (via SetFileInfo)\n");
5488 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5493 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5494 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5497 pSMB->MaxSetupCount = 0;
5501 pSMB->Reserved2 = 0;
5502 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5503 offset = param_offset + params;
5505 data_offset = (char *)pSMB +
5506 offsetof(struct smb_hdr, Protocol) + offset;
5508 count = sizeof(FILE_UNIX_BASIC_INFO);
5510 pSMB->MaxParameterCount = cpu_to_le16(2);
5511 /* BB find max SMB PDU from sess */
5512 pSMB->MaxDataCount = cpu_to_le16(1000);
5513 pSMB->SetupCount = 1;
5514 pSMB->Reserved3 = 0;
5515 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5516 byte_count = 3 /* pad */ + params + count;
5517 pSMB->DataCount = cpu_to_le16(count);
5518 pSMB->ParameterCount = cpu_to_le16(params);
5519 pSMB->TotalDataCount = pSMB->DataCount;
5520 pSMB->TotalParameterCount = pSMB->ParameterCount;
5521 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5522 pSMB->DataOffset = cpu_to_le16(offset);
5524 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
5525 pSMB->Reserved4 = 0;
5526 inc_rfc1001_len(pSMB, byte_count);
5527 pSMB->ByteCount = cpu_to_le16(byte_count);
5529 cifs_fill_unix_set_info((FILE_UNIX_BASIC_INFO *)data_offset, args);
5531 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
5532 cifs_small_buf_release(pSMB);
5534 cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n",
5537 /* Note: On -EAGAIN error only caller can retry on handle based calls
5538 since file handle passed in no longer valid */
5544 CIFSSMBUnixSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
5545 const char *file_name,
5546 const struct cifs_unix_set_info_args *args,
5547 const struct nls_table *nls_codepage, int remap)
5549 TRANSACTION2_SPI_REQ *pSMB = NULL;
5550 TRANSACTION2_SPI_RSP *pSMBr = NULL;
5553 int bytes_returned = 0;
5554 FILE_UNIX_BASIC_INFO *data_offset;
5555 __u16 params, param_offset, offset, count, byte_count;
5557 cifs_dbg(FYI, "In SetUID/GID/Mode\n");
5559 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5564 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5566 cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
5567 PATH_MAX, nls_codepage, remap);
5568 name_len++; /* trailing null */
5571 name_len = copy_path_name(pSMB->FileName, file_name);
5574 params = 6 + name_len;
5575 count = sizeof(FILE_UNIX_BASIC_INFO);
5576 pSMB->MaxParameterCount = cpu_to_le16(2);
5577 /* BB find max SMB PDU from sess structure BB */
5578 pSMB->MaxDataCount = cpu_to_le16(1000);
5579 pSMB->MaxSetupCount = 0;
5583 pSMB->Reserved2 = 0;
5584 param_offset = offsetof(struct smb_com_transaction2_spi_req,
5585 InformationLevel) - 4;
5586 offset = param_offset + params;
5587 /* SMB offsets are from the beginning of SMB which is 4 bytes in, after RFC1001 field */
5588 data_offset = (FILE_UNIX_BASIC_INFO *)((char *) pSMB + offset + 4);
5589 memset(data_offset, 0, count);
5590 pSMB->DataOffset = cpu_to_le16(offset);
5591 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5592 pSMB->SetupCount = 1;
5593 pSMB->Reserved3 = 0;
5594 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5595 byte_count = 3 /* pad */ + params + count;
5596 pSMB->ParameterCount = cpu_to_le16(params);
5597 pSMB->DataCount = cpu_to_le16(count);
5598 pSMB->TotalParameterCount = pSMB->ParameterCount;
5599 pSMB->TotalDataCount = pSMB->DataCount;
5600 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
5601 pSMB->Reserved4 = 0;
5602 inc_rfc1001_len(pSMB, byte_count);
5604 cifs_fill_unix_set_info(data_offset, args);
5606 pSMB->ByteCount = cpu_to_le16(byte_count);
5607 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5608 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5610 cifs_dbg(FYI, "SetPathInfo (perms) returned %d\n", rc);
5612 cifs_buf_release(pSMB);
5618 #ifdef CONFIG_CIFS_XATTR
5620 * Do a path-based QUERY_ALL_EAS call and parse the result. This is a common
5621 * function used by listxattr and getxattr type calls. When ea_name is set,
5622 * it looks for that attribute name and stuffs that value into the EAData
5623 * buffer. When ea_name is NULL, it stuffs a list of attribute names into the
5624 * buffer. In both cases, the return value is either the length of the
5625 * resulting data or a negative error code. If EAData is a NULL pointer then
5626 * the data isn't copied to it, but the length is returned.
5629 CIFSSMBQAllEAs(const unsigned int xid, struct cifs_tcon *tcon,
5630 const unsigned char *searchName, const unsigned char *ea_name,
5631 char *EAData, size_t buf_size,
5632 struct cifs_sb_info *cifs_sb)
5634 /* BB assumes one setup word */
5635 TRANSACTION2_QPI_REQ *pSMB = NULL;
5636 TRANSACTION2_QPI_RSP *pSMBr = NULL;
5637 int remap = cifs_remap(cifs_sb);
5638 struct nls_table *nls_codepage = cifs_sb->local_nls;
5642 struct fealist *ea_response_data;
5643 struct fea *temp_fea;
5646 __u16 params, byte_count, data_offset;
5647 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
5649 cifs_dbg(FYI, "In Query All EAs path %s\n", searchName);
5651 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5656 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5658 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
5659 PATH_MAX, nls_codepage, remap);
5660 list_len++; /* trailing null */
5663 list_len = copy_path_name(pSMB->FileName, searchName);
5666 params = 2 /* level */ + 4 /* reserved */ + list_len /* includes NUL */;
5667 pSMB->TotalDataCount = 0;
5668 pSMB->MaxParameterCount = cpu_to_le16(2);
5669 /* BB find exact max SMB PDU from sess structure BB */
5670 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
5671 pSMB->MaxSetupCount = 0;
5675 pSMB->Reserved2 = 0;
5676 pSMB->ParameterOffset = cpu_to_le16(offsetof(
5677 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
5678 pSMB->DataCount = 0;
5679 pSMB->DataOffset = 0;
5680 pSMB->SetupCount = 1;
5681 pSMB->Reserved3 = 0;
5682 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
5683 byte_count = params + 1 /* pad */ ;
5684 pSMB->TotalParameterCount = cpu_to_le16(params);
5685 pSMB->ParameterCount = pSMB->TotalParameterCount;
5686 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_QUERY_ALL_EAS);
5687 pSMB->Reserved4 = 0;
5688 inc_rfc1001_len(pSMB, byte_count);
5689 pSMB->ByteCount = cpu_to_le16(byte_count);
5691 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5692 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5694 cifs_dbg(FYI, "Send error in QueryAllEAs = %d\n", rc);
5699 /* BB also check enough total bytes returned */
5700 /* BB we need to improve the validity checking
5701 of these trans2 responses */
5703 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5704 if (rc || get_bcc(&pSMBr->hdr) < 4) {
5705 rc = -EIO; /* bad smb */
5709 /* check that length of list is not more than bcc */
5710 /* check that each entry does not go beyond length
5712 /* check that each element of each entry does not
5713 go beyond end of list */
5714 /* validate_trans2_offsets() */
5715 /* BB check if start of smb + data_offset > &bcc+ bcc */
5717 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5718 ea_response_data = (struct fealist *)
5719 (((char *) &pSMBr->hdr.Protocol) + data_offset);
5721 list_len = le32_to_cpu(ea_response_data->list_len);
5722 cifs_dbg(FYI, "ea length %d\n", list_len);
5723 if (list_len <= 8) {
5724 cifs_dbg(FYI, "empty EA list returned from server\n");
5725 /* didn't find the named attribute */
5731 /* make sure list_len doesn't go past end of SMB */
5732 end_of_smb = (char *)pByteArea(&pSMBr->hdr) + get_bcc(&pSMBr->hdr);
5733 if ((char *)ea_response_data + list_len > end_of_smb) {
5734 cifs_dbg(FYI, "EA list appears to go beyond SMB\n");
5739 /* account for ea list len */
5741 temp_fea = &ea_response_data->list;
5742 temp_ptr = (char *)temp_fea;
5743 while (list_len > 0) {
5744 unsigned int name_len;
5749 /* make sure we can read name_len and value_len */
5751 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
5756 name_len = temp_fea->name_len;
5757 value_len = le16_to_cpu(temp_fea->value_len);
5758 list_len -= name_len + 1 + value_len;
5760 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
5766 if (ea_name_len == name_len &&
5767 memcmp(ea_name, temp_ptr, name_len) == 0) {
5768 temp_ptr += name_len + 1;
5772 if ((size_t)value_len > buf_size) {
5776 memcpy(EAData, temp_ptr, value_len);
5780 /* account for prefix user. and trailing null */
5781 rc += (5 + 1 + name_len);
5782 if (rc < (int) buf_size) {
5783 memcpy(EAData, "user.", 5);
5785 memcpy(EAData, temp_ptr, name_len);
5787 /* null terminate name */
5790 } else if (buf_size == 0) {
5791 /* skip copy - calc size only */
5793 /* stop before overrun buffer */
5798 temp_ptr += name_len + 1 + value_len;
5799 temp_fea = (struct fea *)temp_ptr;
5802 /* didn't find the named attribute */
5807 cifs_buf_release(pSMB);
5815 CIFSSMBSetEA(const unsigned int xid, struct cifs_tcon *tcon,
5816 const char *fileName, const char *ea_name, const void *ea_value,
5817 const __u16 ea_value_len, const struct nls_table *nls_codepage,
5818 struct cifs_sb_info *cifs_sb)
5820 struct smb_com_transaction2_spi_req *pSMB = NULL;
5821 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
5822 struct fealist *parm_data;
5825 int bytes_returned = 0;
5826 __u16 params, param_offset, byte_count, offset, count;
5827 int remap = cifs_remap(cifs_sb);
5829 cifs_dbg(FYI, "In SetEA\n");
5831 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5836 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5838 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
5839 PATH_MAX, nls_codepage, remap);
5840 name_len++; /* trailing null */
5843 name_len = copy_path_name(pSMB->FileName, fileName);
5846 params = 6 + name_len;
5848 /* done calculating parms using name_len of file name,
5849 now use name_len to calculate length of ea name
5850 we are going to create in the inode xattrs */
5851 if (ea_name == NULL)
5854 name_len = strnlen(ea_name, 255);
5856 count = sizeof(*parm_data) + 1 + ea_value_len + name_len;
5857 pSMB->MaxParameterCount = cpu_to_le16(2);
5858 /* BB find max SMB PDU from sess */
5859 pSMB->MaxDataCount = cpu_to_le16(1000);
5860 pSMB->MaxSetupCount = 0;
5864 pSMB->Reserved2 = 0;
5865 param_offset = offsetof(struct smb_com_transaction2_spi_req,
5866 InformationLevel) - 4;
5867 offset = param_offset + params;
5868 pSMB->InformationLevel =
5869 cpu_to_le16(SMB_SET_FILE_EA);
5871 parm_data = (void *)pSMB + offsetof(struct smb_hdr, Protocol) + offset;
5872 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5873 pSMB->DataOffset = cpu_to_le16(offset);
5874 pSMB->SetupCount = 1;
5875 pSMB->Reserved3 = 0;
5876 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5877 byte_count = 3 /* pad */ + params + count;
5878 pSMB->DataCount = cpu_to_le16(count);
5879 parm_data->list_len = cpu_to_le32(count);
5880 parm_data->list.EA_flags = 0;
5881 /* we checked above that name len is less than 255 */
5882 parm_data->list.name_len = (__u8)name_len;
5883 /* EA names are always ASCII */
5885 strncpy(parm_data->list.name, ea_name, name_len);
5886 parm_data->list.name[name_len] = '\0';
5887 parm_data->list.value_len = cpu_to_le16(ea_value_len);
5888 /* caller ensures that ea_value_len is less than 64K but
5889 we need to ensure that it fits within the smb */
5891 /*BB add length check to see if it would fit in
5892 negotiated SMB buffer size BB */
5893 /* if (ea_value_len > buffer_size - 512 (enough for header)) */
5895 memcpy(parm_data->list.name + name_len + 1,
5896 ea_value, ea_value_len);
5898 pSMB->TotalDataCount = pSMB->DataCount;
5899 pSMB->ParameterCount = cpu_to_le16(params);
5900 pSMB->TotalParameterCount = pSMB->ParameterCount;
5901 pSMB->Reserved4 = 0;
5902 inc_rfc1001_len(pSMB, byte_count);
5903 pSMB->ByteCount = cpu_to_le16(byte_count);
5904 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5905 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5907 cifs_dbg(FYI, "SetPathInfo (EA) returned %d\n", rc);
5909 cifs_buf_release(pSMB);