4 * Copyright (C) International Business Machines Corp., 2002,2010
5 * Author(s): Steve French (sfrench@us.ibm.com)
7 * Contains the routines for constructing the SMB PDUs themselves
9 * This library is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as published
11 * by the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
17 * the GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 /* SMB/CIFS PDU handling routines here - except for leftovers in connect.c */
25 /* These are mostly routines that operate on a pathname, or on a tree id */
26 /* (mounted volume), but there are eight handle based routines which must be */
27 /* treated slightly differently for reconnection purposes since we never */
28 /* want to reuse a stale file handle and only the caller knows the file info */
31 #include <linux/kernel.h>
32 #include <linux/vfs.h>
33 #include <linux/slab.h>
34 #include <linux/posix_acl_xattr.h>
35 #include <linux/pagemap.h>
36 #include <linux/swap.h>
37 #include <linux/task_io_accounting_ops.h>
38 #include <linux/uaccess.h>
42 #include "cifsproto.h"
43 #include "cifs_unicode.h"
44 #include "cifs_debug.h"
47 #ifdef CONFIG_CIFS_POSIX
52 #ifdef CONFIG_CIFS_WEAK_PW_HASH
53 {LANMAN_PROT, "\2LM1.2X002"},
54 {LANMAN2_PROT, "\2LANMAN2.1"},
55 #endif /* weak password hashing for legacy clients */
56 {CIFS_PROT, "\2NT LM 0.12"},
57 {POSIX_PROT, "\2POSIX 2"},
65 #ifdef CONFIG_CIFS_WEAK_PW_HASH
66 {LANMAN_PROT, "\2LM1.2X002"},
67 {LANMAN2_PROT, "\2LANMAN2.1"},
68 #endif /* weak password hashing for legacy clients */
69 {CIFS_PROT, "\2NT LM 0.12"},
74 /* define the number of elements in the cifs dialect array */
75 #ifdef CONFIG_CIFS_POSIX
76 #ifdef CONFIG_CIFS_WEAK_PW_HASH
77 #define CIFS_NUM_PROT 4
79 #define CIFS_NUM_PROT 2
80 #endif /* CIFS_WEAK_PW_HASH */
82 #ifdef CONFIG_CIFS_WEAK_PW_HASH
83 #define CIFS_NUM_PROT 3
85 #define CIFS_NUM_PROT 1
86 #endif /* CONFIG_CIFS_WEAK_PW_HASH */
87 #endif /* CIFS_POSIX */
90 * Mark as invalid, all open files on tree connections since they
91 * were closed when session to server was lost.
94 cifs_mark_open_files_invalid(struct cifs_tcon *tcon)
96 struct cifsFileInfo *open_file = NULL;
97 struct list_head *tmp;
98 struct list_head *tmp1;
100 /* list all files open on tree connection and mark them invalid */
101 spin_lock(&tcon->open_file_lock);
102 list_for_each_safe(tmp, tmp1, &tcon->openFileList) {
103 open_file = list_entry(tmp, struct cifsFileInfo, tlist);
104 open_file->invalidHandle = true;
105 open_file->oplock_break_cancelled = true;
107 spin_unlock(&tcon->open_file_lock);
109 * BB Add call to invalidate_inodes(sb) for all superblocks mounted
114 /* reconnect the socket, tcon, and smb session if needed */
116 cifs_reconnect_tcon(struct cifs_tcon *tcon, int smb_command)
119 struct cifs_ses *ses;
120 struct TCP_Server_Info *server;
121 struct nls_table *nls_codepage;
124 * SMBs NegProt, SessSetup, uLogoff do not have tcon yet so check for
125 * tcp and smb session status done differently for those three - in the
132 server = ses->server;
135 * only tree disconnect, open, and write, (and ulogoff which does not
136 * have tcon) are allowed as we start force umount
138 if (tcon->tidStatus == CifsExiting) {
139 if (smb_command != SMB_COM_WRITE_ANDX &&
140 smb_command != SMB_COM_OPEN_ANDX &&
141 smb_command != SMB_COM_TREE_DISCONNECT) {
142 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
149 * Give demultiplex thread up to 10 seconds to reconnect, should be
150 * greater than cifs socket timeout which is 7 seconds
152 while (server->tcpStatus == CifsNeedReconnect) {
153 wait_event_interruptible_timeout(server->response_q,
154 (server->tcpStatus != CifsNeedReconnect), 10 * HZ);
156 /* are we still trying to reconnect? */
157 if (server->tcpStatus != CifsNeedReconnect)
161 * on "soft" mounts we wait once. Hard mounts keep
162 * retrying until process is killed or server comes
166 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
171 if (!ses->need_reconnect && !tcon->need_reconnect)
174 nls_codepage = load_nls_default();
177 * need to prevent multiple threads trying to simultaneously
178 * reconnect the same SMB session
180 mutex_lock(&ses->session_mutex);
181 rc = cifs_negotiate_protocol(0, ses);
182 if (rc == 0 && ses->need_reconnect)
183 rc = cifs_setup_session(0, ses, nls_codepage);
185 /* do we need to reconnect tcon? */
186 if (rc || !tcon->need_reconnect) {
187 mutex_unlock(&ses->session_mutex);
191 cifs_mark_open_files_invalid(tcon);
192 rc = CIFSTCon(0, ses, tcon->treeName, tcon, nls_codepage);
193 mutex_unlock(&ses->session_mutex);
194 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
199 atomic_inc(&tconInfoReconnectCount);
201 /* tell server Unix caps we support */
202 if (ses->capabilities & CAP_UNIX)
203 reset_cifs_unix_caps(0, tcon, NULL, NULL);
206 * Removed call to reopen open files here. It is safer (and faster) to
207 * reopen files one at a time as needed in read and write.
209 * FIXME: what about file locks? don't we need to reclaim them ASAP?
214 * Check if handle based operation so we know whether we can continue
215 * or not without returning to caller to reset file handle
217 switch (smb_command) {
218 case SMB_COM_READ_ANDX:
219 case SMB_COM_WRITE_ANDX:
221 case SMB_COM_FIND_CLOSE2:
222 case SMB_COM_LOCKING_ANDX:
226 unload_nls(nls_codepage);
230 /* Allocate and return pointer to an SMB request buffer, and set basic
231 SMB information in the SMB header. If the return code is zero, this
232 function must have filled in request_buf pointer */
234 small_smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
239 rc = cifs_reconnect_tcon(tcon, smb_command);
243 *request_buf = cifs_small_buf_get();
244 if (*request_buf == NULL) {
245 /* BB should we add a retry in here if not a writepage? */
249 header_assemble((struct smb_hdr *) *request_buf, smb_command,
253 cifs_stats_inc(&tcon->num_smbs_sent);
259 small_smb_init_no_tc(const int smb_command, const int wct,
260 struct cifs_ses *ses, void **request_buf)
263 struct smb_hdr *buffer;
265 rc = small_smb_init(smb_command, wct, NULL, request_buf);
269 buffer = (struct smb_hdr *)*request_buf;
270 buffer->Mid = get_next_mid(ses->server);
271 if (ses->capabilities & CAP_UNICODE)
272 buffer->Flags2 |= SMBFLG2_UNICODE;
273 if (ses->capabilities & CAP_STATUS32)
274 buffer->Flags2 |= SMBFLG2_ERR_STATUS;
276 /* uid, tid can stay at zero as set in header assemble */
278 /* BB add support for turning on the signing when
279 this function is used after 1st of session setup requests */
284 /* If the return code is zero, this function must fill in request_buf pointer */
286 __smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
287 void **request_buf, void **response_buf)
289 *request_buf = cifs_buf_get();
290 if (*request_buf == NULL) {
291 /* BB should we add a retry in here if not a writepage? */
294 /* Although the original thought was we needed the response buf for */
295 /* potential retries of smb operations it turns out we can determine */
296 /* from the mid flags when the request buffer can be resent without */
297 /* having to use a second distinct buffer for the response */
299 *response_buf = *request_buf;
301 header_assemble((struct smb_hdr *) *request_buf, smb_command, tcon,
305 cifs_stats_inc(&tcon->num_smbs_sent);
310 /* If the return code is zero, this function must fill in request_buf pointer */
312 smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
313 void **request_buf, void **response_buf)
317 rc = cifs_reconnect_tcon(tcon, smb_command);
321 return __smb_init(smb_command, wct, tcon, request_buf, response_buf);
325 smb_init_no_reconnect(int smb_command, int wct, struct cifs_tcon *tcon,
326 void **request_buf, void **response_buf)
328 if (tcon->ses->need_reconnect || tcon->need_reconnect)
331 return __smb_init(smb_command, wct, tcon, request_buf, response_buf);
334 static int validate_t2(struct smb_t2_rsp *pSMB)
336 unsigned int total_size;
338 /* check for plausible wct */
339 if (pSMB->hdr.WordCount < 10)
342 /* check for parm and data offset going beyond end of smb */
343 if (get_unaligned_le16(&pSMB->t2_rsp.ParameterOffset) > 1024 ||
344 get_unaligned_le16(&pSMB->t2_rsp.DataOffset) > 1024)
347 total_size = get_unaligned_le16(&pSMB->t2_rsp.ParameterCount);
348 if (total_size >= 512)
351 /* check that bcc is at least as big as parms + data, and that it is
352 * less than negotiated smb buffer
354 total_size += get_unaligned_le16(&pSMB->t2_rsp.DataCount);
355 if (total_size > get_bcc(&pSMB->hdr) ||
356 total_size >= CIFSMaxBufSize + MAX_CIFS_HDR_SIZE)
361 cifs_dump_mem("Invalid transact2 SMB: ", (char *)pSMB,
362 sizeof(struct smb_t2_rsp) + 16);
367 decode_ext_sec_blob(struct cifs_ses *ses, NEGOTIATE_RSP *pSMBr)
371 char *guid = pSMBr->u.extended_response.GUID;
372 struct TCP_Server_Info *server = ses->server;
374 count = get_bcc(&pSMBr->hdr);
375 if (count < SMB1_CLIENT_GUID_SIZE)
378 spin_lock(&cifs_tcp_ses_lock);
379 if (server->srv_count > 1) {
380 spin_unlock(&cifs_tcp_ses_lock);
381 if (memcmp(server->server_GUID, guid, SMB1_CLIENT_GUID_SIZE) != 0) {
382 cifs_dbg(FYI, "server UID changed\n");
383 memcpy(server->server_GUID, guid, SMB1_CLIENT_GUID_SIZE);
386 spin_unlock(&cifs_tcp_ses_lock);
387 memcpy(server->server_GUID, guid, SMB1_CLIENT_GUID_SIZE);
390 if (count == SMB1_CLIENT_GUID_SIZE) {
391 server->sec_ntlmssp = true;
393 count -= SMB1_CLIENT_GUID_SIZE;
394 rc = decode_negTokenInit(
395 pSMBr->u.extended_response.SecurityBlob, count, server);
404 cifs_enable_signing(struct TCP_Server_Info *server, bool mnt_sign_required)
406 bool srv_sign_required = server->sec_mode & server->vals->signing_required;
407 bool srv_sign_enabled = server->sec_mode & server->vals->signing_enabled;
408 bool mnt_sign_enabled = global_secflags & CIFSSEC_MAY_SIGN;
411 * Is signing required by mnt options? If not then check
412 * global_secflags to see if it is there.
414 if (!mnt_sign_required)
415 mnt_sign_required = ((global_secflags & CIFSSEC_MUST_SIGN) ==
419 * If signing is required then it's automatically enabled too,
420 * otherwise, check to see if the secflags allow it.
422 mnt_sign_enabled = mnt_sign_required ? mnt_sign_required :
423 (global_secflags & CIFSSEC_MAY_SIGN);
425 /* If server requires signing, does client allow it? */
426 if (srv_sign_required) {
427 if (!mnt_sign_enabled) {
428 cifs_dbg(VFS, "Server requires signing, but it's disabled in SecurityFlags!");
434 /* If client requires signing, does server allow it? */
435 if (mnt_sign_required) {
436 if (!srv_sign_enabled) {
437 cifs_dbg(VFS, "Server does not support signing!");
446 #ifdef CONFIG_CIFS_WEAK_PW_HASH
448 decode_lanman_negprot_rsp(struct TCP_Server_Info *server, NEGOTIATE_RSP *pSMBr)
451 struct lanman_neg_rsp *rsp = (struct lanman_neg_rsp *)pSMBr;
453 if (server->dialect != LANMAN_PROT && server->dialect != LANMAN2_PROT)
456 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
457 server->maxReq = min_t(unsigned int,
458 le16_to_cpu(rsp->MaxMpxCount),
460 set_credits(server, server->maxReq);
461 server->maxBuf = le16_to_cpu(rsp->MaxBufSize);
462 /* even though we do not use raw we might as well set this
463 accurately, in case we ever find a need for it */
464 if ((le16_to_cpu(rsp->RawMode) & RAW_ENABLE) == RAW_ENABLE) {
465 server->max_rw = 0xFF00;
466 server->capabilities = CAP_MPX_MODE | CAP_RAW_MODE;
468 server->max_rw = 0;/* do not need to use raw anyway */
469 server->capabilities = CAP_MPX_MODE;
471 tmp = (__s16)le16_to_cpu(rsp->ServerTimeZone);
473 /* OS/2 often does not set timezone therefore
474 * we must use server time to calc time zone.
475 * Could deviate slightly from the right zone.
476 * Smallest defined timezone difference is 15 minutes
477 * (i.e. Nepal). Rounding up/down is done to match
480 int val, seconds, remain, result;
481 struct timespec ts, utc;
483 ts = cnvrtDosUnixTm(rsp->SrvTime.Date,
484 rsp->SrvTime.Time, 0);
485 cifs_dbg(FYI, "SrvTime %d sec since 1970 (utc: %d) diff: %d\n",
486 (int)ts.tv_sec, (int)utc.tv_sec,
487 (int)(utc.tv_sec - ts.tv_sec));
488 val = (int)(utc.tv_sec - ts.tv_sec);
490 result = (seconds / MIN_TZ_ADJ) * MIN_TZ_ADJ;
491 remain = seconds % MIN_TZ_ADJ;
492 if (remain >= (MIN_TZ_ADJ / 2))
493 result += MIN_TZ_ADJ;
496 server->timeAdj = result;
498 server->timeAdj = (int)tmp;
499 server->timeAdj *= 60; /* also in seconds */
501 cifs_dbg(FYI, "server->timeAdj: %d seconds\n", server->timeAdj);
504 /* BB get server time for time conversions and add
505 code to use it and timezone since this is not UTC */
507 if (rsp->EncryptionKeyLength ==
508 cpu_to_le16(CIFS_CRYPTO_KEY_SIZE)) {
509 memcpy(server->cryptkey, rsp->EncryptionKey,
510 CIFS_CRYPTO_KEY_SIZE);
511 } else if (server->sec_mode & SECMODE_PW_ENCRYPT) {
512 return -EIO; /* need cryptkey unless plain text */
515 cifs_dbg(FYI, "LANMAN negotiated\n");
520 decode_lanman_negprot_rsp(struct TCP_Server_Info *server, NEGOTIATE_RSP *pSMBr)
522 cifs_dbg(VFS, "mount failed, cifs module not built with CIFS_WEAK_PW_HASH support\n");
528 should_set_ext_sec_flag(enum securityEnum sectype)
535 if (global_secflags &
536 (CIFSSEC_MAY_KRB5 | CIFSSEC_MAY_NTLMSSP))
545 CIFSSMBNegotiate(const unsigned int xid, struct cifs_ses *ses)
548 NEGOTIATE_RSP *pSMBr;
552 struct TCP_Server_Info *server = ses->server;
556 WARN(1, "%s: server is NULL!\n", __func__);
560 rc = smb_init(SMB_COM_NEGOTIATE, 0, NULL /* no tcon yet */ ,
561 (void **) &pSMB, (void **) &pSMBr);
565 pSMB->hdr.Mid = get_next_mid(server);
566 pSMB->hdr.Flags2 |= (SMBFLG2_UNICODE | SMBFLG2_ERR_STATUS);
568 if (should_set_ext_sec_flag(ses->sectype)) {
569 cifs_dbg(FYI, "Requesting extended security.");
570 pSMB->hdr.Flags2 |= SMBFLG2_EXT_SEC;
574 for (i = 0; i < CIFS_NUM_PROT; i++) {
575 strncpy(pSMB->DialectsArray+count, protocols[i].name, 16);
576 count += strlen(protocols[i].name) + 1;
577 /* null at end of source and target buffers anyway */
579 inc_rfc1001_len(pSMB, count);
580 pSMB->ByteCount = cpu_to_le16(count);
582 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
583 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
587 server->dialect = le16_to_cpu(pSMBr->DialectIndex);
588 cifs_dbg(FYI, "Dialect: %d\n", server->dialect);
589 /* Check wct = 1 error case */
590 if ((pSMBr->hdr.WordCount < 13) || (server->dialect == BAD_PROT)) {
591 /* core returns wct = 1, but we do not ask for core - otherwise
592 small wct just comes when dialect index is -1 indicating we
593 could not negotiate a common dialect */
596 } else if (pSMBr->hdr.WordCount == 13) {
597 server->negflavor = CIFS_NEGFLAVOR_LANMAN;
598 rc = decode_lanman_negprot_rsp(server, pSMBr);
600 } else if (pSMBr->hdr.WordCount != 17) {
605 /* else wct == 17, NTLM or better */
607 server->sec_mode = pSMBr->SecurityMode;
608 if ((server->sec_mode & SECMODE_USER) == 0)
609 cifs_dbg(FYI, "share mode security\n");
611 /* one byte, so no need to convert this or EncryptionKeyLen from
613 server->maxReq = min_t(unsigned int, le16_to_cpu(pSMBr->MaxMpxCount),
615 set_credits(server, server->maxReq);
616 /* probably no need to store and check maxvcs */
617 server->maxBuf = le32_to_cpu(pSMBr->MaxBufferSize);
618 server->max_rw = le32_to_cpu(pSMBr->MaxRawSize);
619 cifs_dbg(NOISY, "Max buf = %d\n", ses->server->maxBuf);
620 server->capabilities = le32_to_cpu(pSMBr->Capabilities);
621 server->timeAdj = (int)(__s16)le16_to_cpu(pSMBr->ServerTimeZone);
622 server->timeAdj *= 60;
624 if (pSMBr->EncryptionKeyLength == CIFS_CRYPTO_KEY_SIZE) {
625 server->negflavor = CIFS_NEGFLAVOR_UNENCAP;
626 memcpy(ses->server->cryptkey, pSMBr->u.EncryptionKey,
627 CIFS_CRYPTO_KEY_SIZE);
628 } else if (pSMBr->hdr.Flags2 & SMBFLG2_EXT_SEC ||
629 server->capabilities & CAP_EXTENDED_SECURITY) {
630 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
631 rc = decode_ext_sec_blob(ses, pSMBr);
632 } else if (server->sec_mode & SECMODE_PW_ENCRYPT) {
633 rc = -EIO; /* no crypt key only if plain text pwd */
635 server->negflavor = CIFS_NEGFLAVOR_UNENCAP;
636 server->capabilities &= ~CAP_EXTENDED_SECURITY;
641 rc = cifs_enable_signing(server, ses->sign);
643 cifs_buf_release(pSMB);
645 cifs_dbg(FYI, "negprot rc %d\n", rc);
650 CIFSSMBTDis(const unsigned int xid, struct cifs_tcon *tcon)
652 struct smb_hdr *smb_buffer;
655 cifs_dbg(FYI, "In tree disconnect\n");
657 /* BB: do we need to check this? These should never be NULL. */
658 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
662 * No need to return error on this operation if tid invalidated and
663 * closed on server already e.g. due to tcp session crashing. Also,
664 * the tcon is no longer on the list, so no need to take lock before
667 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
670 rc = small_smb_init(SMB_COM_TREE_DISCONNECT, 0, tcon,
671 (void **)&smb_buffer);
675 rc = SendReceiveNoRsp(xid, tcon->ses, (char *)smb_buffer, 0);
676 cifs_small_buf_release(smb_buffer);
678 cifs_dbg(FYI, "Tree disconnect failed %d\n", rc);
680 /* No need to return error on this operation if tid invalidated and
681 closed on server already e.g. due to tcp session crashing */
689 * This is a no-op for now. We're not really interested in the reply, but
690 * rather in the fact that the server sent one and that server->lstrp
693 * FIXME: maybe we should consider checking that the reply matches request?
696 cifs_echo_callback(struct mid_q_entry *mid)
698 struct TCP_Server_Info *server = mid->callback_data;
700 mutex_lock(&server->srv_mutex);
701 DeleteMidQEntry(mid);
702 mutex_unlock(&server->srv_mutex);
703 add_credits(server, 1, CIFS_ECHO_OP);
707 CIFSSMBEcho(struct TCP_Server_Info *server)
712 struct smb_rqst rqst = { .rq_iov = iov,
715 cifs_dbg(FYI, "In echo request\n");
717 rc = small_smb_init(SMB_COM_ECHO, 0, NULL, (void **)&smb);
721 /* set up echo request */
722 smb->hdr.Tid = 0xffff;
723 smb->hdr.WordCount = 1;
724 put_unaligned_le16(1, &smb->EchoCount);
725 put_bcc(1, &smb->hdr);
727 inc_rfc1001_len(smb, 3);
730 iov[0].iov_base = smb;
731 iov[1].iov_len = get_rfc1002_length(smb);
732 iov[1].iov_base = (char *)smb + 4;
734 rc = cifs_call_async(server, &rqst, NULL, cifs_echo_callback, NULL,
735 server, CIFS_ASYNC_OP | CIFS_ECHO_OP);
737 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
739 cifs_small_buf_release(smb);
745 CIFSSMBLogoff(const unsigned int xid, struct cifs_ses *ses)
747 LOGOFF_ANDX_REQ *pSMB;
750 cifs_dbg(FYI, "In SMBLogoff for session disconnect\n");
753 * BB: do we need to check validity of ses and server? They should
754 * always be valid since we have an active reference. If not, that
755 * should probably be a BUG()
757 if (!ses || !ses->server)
760 mutex_lock(&ses->session_mutex);
761 if (ses->need_reconnect)
762 goto session_already_dead; /* no need to send SMBlogoff if uid
763 already closed due to reconnect */
764 rc = small_smb_init(SMB_COM_LOGOFF_ANDX, 2, NULL, (void **)&pSMB);
766 mutex_unlock(&ses->session_mutex);
770 pSMB->hdr.Mid = get_next_mid(ses->server);
772 if (ses->server->sign)
773 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
775 pSMB->hdr.Uid = ses->Suid;
777 pSMB->AndXCommand = 0xFF;
778 rc = SendReceiveNoRsp(xid, ses, (char *) pSMB, 0);
779 cifs_small_buf_release(pSMB);
780 session_already_dead:
781 mutex_unlock(&ses->session_mutex);
783 /* if session dead then we do not need to do ulogoff,
784 since server closed smb session, no sense reporting
792 CIFSPOSIXDelFile(const unsigned int xid, struct cifs_tcon *tcon,
793 const char *fileName, __u16 type,
794 const struct nls_table *nls_codepage, int remap)
796 TRANSACTION2_SPI_REQ *pSMB = NULL;
797 TRANSACTION2_SPI_RSP *pSMBr = NULL;
798 struct unlink_psx_rq *pRqD;
801 int bytes_returned = 0;
802 __u16 params, param_offset, offset, byte_count;
804 cifs_dbg(FYI, "In POSIX delete\n");
806 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
811 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
813 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
814 PATH_MAX, nls_codepage, remap);
815 name_len++; /* trailing null */
817 } else { /* BB add path length overrun check */
818 name_len = strnlen(fileName, PATH_MAX);
819 name_len++; /* trailing null */
820 strncpy(pSMB->FileName, fileName, name_len);
823 params = 6 + name_len;
824 pSMB->MaxParameterCount = cpu_to_le16(2);
825 pSMB->MaxDataCount = 0; /* BB double check this with jra */
826 pSMB->MaxSetupCount = 0;
831 param_offset = offsetof(struct smb_com_transaction2_spi_req,
832 InformationLevel) - 4;
833 offset = param_offset + params;
835 /* Setup pointer to Request Data (inode type) */
836 pRqD = (struct unlink_psx_rq *)(((char *)&pSMB->hdr.Protocol) + offset);
837 pRqD->type = cpu_to_le16(type);
838 pSMB->ParameterOffset = cpu_to_le16(param_offset);
839 pSMB->DataOffset = cpu_to_le16(offset);
840 pSMB->SetupCount = 1;
842 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
843 byte_count = 3 /* pad */ + params + sizeof(struct unlink_psx_rq);
845 pSMB->DataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
846 pSMB->TotalDataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
847 pSMB->ParameterCount = cpu_to_le16(params);
848 pSMB->TotalParameterCount = pSMB->ParameterCount;
849 pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_UNLINK);
851 inc_rfc1001_len(pSMB, byte_count);
852 pSMB->ByteCount = cpu_to_le16(byte_count);
853 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
854 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
856 cifs_dbg(FYI, "Posix delete returned %d\n", rc);
857 cifs_buf_release(pSMB);
859 cifs_stats_inc(&tcon->stats.cifs_stats.num_deletes);
868 CIFSSMBDelFile(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
869 struct cifs_sb_info *cifs_sb)
871 DELETE_FILE_REQ *pSMB = NULL;
872 DELETE_FILE_RSP *pSMBr = NULL;
876 int remap = cifs_remap(cifs_sb);
879 rc = smb_init(SMB_COM_DELETE, 1, tcon, (void **) &pSMB,
884 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
885 name_len = cifsConvertToUTF16((__le16 *) pSMB->fileName, name,
886 PATH_MAX, cifs_sb->local_nls,
888 name_len++; /* trailing null */
890 } else { /* BB improve check for buffer overruns BB */
891 name_len = strnlen(name, PATH_MAX);
892 name_len++; /* trailing null */
893 strncpy(pSMB->fileName, name, name_len);
895 pSMB->SearchAttributes =
896 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM);
897 pSMB->BufferFormat = 0x04;
898 inc_rfc1001_len(pSMB, name_len + 1);
899 pSMB->ByteCount = cpu_to_le16(name_len + 1);
900 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
901 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
902 cifs_stats_inc(&tcon->stats.cifs_stats.num_deletes);
904 cifs_dbg(FYI, "Error in RMFile = %d\n", rc);
906 cifs_buf_release(pSMB);
914 CIFSSMBRmDir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
915 struct cifs_sb_info *cifs_sb)
917 DELETE_DIRECTORY_REQ *pSMB = NULL;
918 DELETE_DIRECTORY_RSP *pSMBr = NULL;
922 int remap = cifs_remap(cifs_sb);
924 cifs_dbg(FYI, "In CIFSSMBRmDir\n");
926 rc = smb_init(SMB_COM_DELETE_DIRECTORY, 0, tcon, (void **) &pSMB,
931 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
932 name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, name,
933 PATH_MAX, cifs_sb->local_nls,
935 name_len++; /* trailing null */
937 } else { /* BB improve check for buffer overruns BB */
938 name_len = strnlen(name, PATH_MAX);
939 name_len++; /* trailing null */
940 strncpy(pSMB->DirName, name, name_len);
943 pSMB->BufferFormat = 0x04;
944 inc_rfc1001_len(pSMB, name_len + 1);
945 pSMB->ByteCount = cpu_to_le16(name_len + 1);
946 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
947 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
948 cifs_stats_inc(&tcon->stats.cifs_stats.num_rmdirs);
950 cifs_dbg(FYI, "Error in RMDir = %d\n", rc);
952 cifs_buf_release(pSMB);
959 CIFSSMBMkDir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
960 struct cifs_sb_info *cifs_sb)
963 CREATE_DIRECTORY_REQ *pSMB = NULL;
964 CREATE_DIRECTORY_RSP *pSMBr = NULL;
967 int remap = cifs_remap(cifs_sb);
969 cifs_dbg(FYI, "In CIFSSMBMkDir\n");
971 rc = smb_init(SMB_COM_CREATE_DIRECTORY, 0, tcon, (void **) &pSMB,
976 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
977 name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, name,
978 PATH_MAX, cifs_sb->local_nls,
980 name_len++; /* trailing null */
982 } else { /* BB improve check for buffer overruns BB */
983 name_len = strnlen(name, PATH_MAX);
984 name_len++; /* trailing null */
985 strncpy(pSMB->DirName, name, name_len);
988 pSMB->BufferFormat = 0x04;
989 inc_rfc1001_len(pSMB, name_len + 1);
990 pSMB->ByteCount = cpu_to_le16(name_len + 1);
991 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
992 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
993 cifs_stats_inc(&tcon->stats.cifs_stats.num_mkdirs);
995 cifs_dbg(FYI, "Error in Mkdir = %d\n", rc);
997 cifs_buf_release(pSMB);
1004 CIFSPOSIXCreate(const unsigned int xid, struct cifs_tcon *tcon,
1005 __u32 posix_flags, __u64 mode, __u16 *netfid,
1006 FILE_UNIX_BASIC_INFO *pRetData, __u32 *pOplock,
1007 const char *name, const struct nls_table *nls_codepage,
1010 TRANSACTION2_SPI_REQ *pSMB = NULL;
1011 TRANSACTION2_SPI_RSP *pSMBr = NULL;
1014 int bytes_returned = 0;
1015 __u16 params, param_offset, offset, byte_count, count;
1016 OPEN_PSX_REQ *pdata;
1017 OPEN_PSX_RSP *psx_rsp;
1019 cifs_dbg(FYI, "In POSIX Create\n");
1021 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
1026 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1028 cifsConvertToUTF16((__le16 *) pSMB->FileName, name,
1029 PATH_MAX, nls_codepage, remap);
1030 name_len++; /* trailing null */
1032 } else { /* BB improve the check for buffer overruns BB */
1033 name_len = strnlen(name, PATH_MAX);
1034 name_len++; /* trailing null */
1035 strncpy(pSMB->FileName, name, name_len);
1038 params = 6 + name_len;
1039 count = sizeof(OPEN_PSX_REQ);
1040 pSMB->MaxParameterCount = cpu_to_le16(2);
1041 pSMB->MaxDataCount = cpu_to_le16(1000); /* large enough */
1042 pSMB->MaxSetupCount = 0;
1046 pSMB->Reserved2 = 0;
1047 param_offset = offsetof(struct smb_com_transaction2_spi_req,
1048 InformationLevel) - 4;
1049 offset = param_offset + params;
1050 pdata = (OPEN_PSX_REQ *)(((char *)&pSMB->hdr.Protocol) + offset);
1051 pdata->Level = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
1052 pdata->Permissions = cpu_to_le64(mode);
1053 pdata->PosixOpenFlags = cpu_to_le32(posix_flags);
1054 pdata->OpenFlags = cpu_to_le32(*pOplock);
1055 pSMB->ParameterOffset = cpu_to_le16(param_offset);
1056 pSMB->DataOffset = cpu_to_le16(offset);
1057 pSMB->SetupCount = 1;
1058 pSMB->Reserved3 = 0;
1059 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
1060 byte_count = 3 /* pad */ + params + count;
1062 pSMB->DataCount = cpu_to_le16(count);
1063 pSMB->ParameterCount = cpu_to_le16(params);
1064 pSMB->TotalDataCount = pSMB->DataCount;
1065 pSMB->TotalParameterCount = pSMB->ParameterCount;
1066 pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_OPEN);
1067 pSMB->Reserved4 = 0;
1068 inc_rfc1001_len(pSMB, byte_count);
1069 pSMB->ByteCount = cpu_to_le16(byte_count);
1070 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1071 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
1073 cifs_dbg(FYI, "Posix create returned %d\n", rc);
1074 goto psx_create_err;
1077 cifs_dbg(FYI, "copying inode info\n");
1078 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
1080 if (rc || get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)) {
1081 rc = -EIO; /* bad smb */
1082 goto psx_create_err;
1085 /* copy return information to pRetData */
1086 psx_rsp = (OPEN_PSX_RSP *)((char *) &pSMBr->hdr.Protocol
1087 + le16_to_cpu(pSMBr->t2.DataOffset));
1089 *pOplock = le16_to_cpu(psx_rsp->OplockFlags);
1091 *netfid = psx_rsp->Fid; /* cifs fid stays in le */
1092 /* Let caller know file was created so we can set the mode. */
1093 /* Do we care about the CreateAction in any other cases? */
1094 if (cpu_to_le32(FILE_CREATE) == psx_rsp->CreateAction)
1095 *pOplock |= CIFS_CREATE_ACTION;
1096 /* check to make sure response data is there */
1097 if (psx_rsp->ReturnedLevel != cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC)) {
1098 pRetData->Type = cpu_to_le32(-1); /* unknown */
1099 cifs_dbg(NOISY, "unknown type\n");
1101 if (get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)
1102 + sizeof(FILE_UNIX_BASIC_INFO)) {
1103 cifs_dbg(VFS, "Open response data too small\n");
1104 pRetData->Type = cpu_to_le32(-1);
1105 goto psx_create_err;
1107 memcpy((char *) pRetData,
1108 (char *)psx_rsp + sizeof(OPEN_PSX_RSP),
1109 sizeof(FILE_UNIX_BASIC_INFO));
1113 cifs_buf_release(pSMB);
1115 if (posix_flags & SMB_O_DIRECTORY)
1116 cifs_stats_inc(&tcon->stats.cifs_stats.num_posixmkdirs);
1118 cifs_stats_inc(&tcon->stats.cifs_stats.num_posixopens);
1126 static __u16 convert_disposition(int disposition)
1130 switch (disposition) {
1131 case FILE_SUPERSEDE:
1132 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1135 ofun = SMBOPEN_OAPPEND;
1138 ofun = SMBOPEN_OCREATE;
1141 ofun = SMBOPEN_OCREATE | SMBOPEN_OAPPEND;
1143 case FILE_OVERWRITE:
1144 ofun = SMBOPEN_OTRUNC;
1146 case FILE_OVERWRITE_IF:
1147 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1150 cifs_dbg(FYI, "unknown disposition %d\n", disposition);
1151 ofun = SMBOPEN_OAPPEND; /* regular open */
1157 access_flags_to_smbopen_mode(const int access_flags)
1159 int masked_flags = access_flags & (GENERIC_READ | GENERIC_WRITE);
1161 if (masked_flags == GENERIC_READ)
1162 return SMBOPEN_READ;
1163 else if (masked_flags == GENERIC_WRITE)
1164 return SMBOPEN_WRITE;
1166 /* just go for read/write */
1167 return SMBOPEN_READWRITE;
1171 SMBLegacyOpen(const unsigned int xid, struct cifs_tcon *tcon,
1172 const char *fileName, const int openDisposition,
1173 const int access_flags, const int create_options, __u16 *netfid,
1174 int *pOplock, FILE_ALL_INFO *pfile_info,
1175 const struct nls_table *nls_codepage, int remap)
1178 OPENX_REQ *pSMB = NULL;
1179 OPENX_RSP *pSMBr = NULL;
1185 rc = smb_init(SMB_COM_OPEN_ANDX, 15, tcon, (void **) &pSMB,
1190 pSMB->AndXCommand = 0xFF; /* none */
1192 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1193 count = 1; /* account for one byte pad to word boundary */
1195 cifsConvertToUTF16((__le16 *) (pSMB->fileName + 1),
1196 fileName, PATH_MAX, nls_codepage, remap);
1197 name_len++; /* trailing null */
1199 } else { /* BB improve check for buffer overruns BB */
1200 count = 0; /* no pad */
1201 name_len = strnlen(fileName, PATH_MAX);
1202 name_len++; /* trailing null */
1203 strncpy(pSMB->fileName, fileName, name_len);
1205 if (*pOplock & REQ_OPLOCK)
1206 pSMB->OpenFlags = cpu_to_le16(REQ_OPLOCK);
1207 else if (*pOplock & REQ_BATCHOPLOCK)
1208 pSMB->OpenFlags = cpu_to_le16(REQ_BATCHOPLOCK);
1210 pSMB->OpenFlags |= cpu_to_le16(REQ_MORE_INFO);
1211 pSMB->Mode = cpu_to_le16(access_flags_to_smbopen_mode(access_flags));
1212 pSMB->Mode |= cpu_to_le16(0x40); /* deny none */
1213 /* set file as system file if special file such
1214 as fifo and server expecting SFU style and
1215 no Unix extensions */
1217 if (create_options & CREATE_OPTION_SPECIAL)
1218 pSMB->FileAttributes = cpu_to_le16(ATTR_SYSTEM);
1219 else /* BB FIXME BB */
1220 pSMB->FileAttributes = cpu_to_le16(0/*ATTR_NORMAL*/);
1222 if (create_options & CREATE_OPTION_READONLY)
1223 pSMB->FileAttributes |= cpu_to_le16(ATTR_READONLY);
1226 /* pSMB->CreateOptions = cpu_to_le32(create_options &
1227 CREATE_OPTIONS_MASK); */
1228 /* BB FIXME END BB */
1230 pSMB->Sattr = cpu_to_le16(ATTR_HIDDEN | ATTR_SYSTEM | ATTR_DIRECTORY);
1231 pSMB->OpenFunction = cpu_to_le16(convert_disposition(openDisposition));
1233 inc_rfc1001_len(pSMB, count);
1235 pSMB->ByteCount = cpu_to_le16(count);
1236 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1237 (struct smb_hdr *)pSMBr, &bytes_returned, 0);
1238 cifs_stats_inc(&tcon->stats.cifs_stats.num_opens);
1240 cifs_dbg(FYI, "Error in Open = %d\n", rc);
1242 /* BB verify if wct == 15 */
1244 /* *pOplock = pSMBr->OplockLevel; */ /* BB take from action field*/
1246 *netfid = pSMBr->Fid; /* cifs fid stays in le */
1247 /* Let caller know file was created so we can set the mode. */
1248 /* Do we care about the CreateAction in any other cases? */
1250 /* if (cpu_to_le32(FILE_CREATE) == pSMBr->CreateAction)
1251 *pOplock |= CIFS_CREATE_ACTION; */
1255 pfile_info->CreationTime = 0; /* BB convert CreateTime*/
1256 pfile_info->LastAccessTime = 0; /* BB fixme */
1257 pfile_info->LastWriteTime = 0; /* BB fixme */
1258 pfile_info->ChangeTime = 0; /* BB fixme */
1259 pfile_info->Attributes =
1260 cpu_to_le32(le16_to_cpu(pSMBr->FileAttributes));
1261 /* the file_info buf is endian converted by caller */
1262 pfile_info->AllocationSize =
1263 cpu_to_le64(le32_to_cpu(pSMBr->EndOfFile));
1264 pfile_info->EndOfFile = pfile_info->AllocationSize;
1265 pfile_info->NumberOfLinks = cpu_to_le32(1);
1266 pfile_info->DeletePending = 0;
1270 cifs_buf_release(pSMB);
1277 CIFS_open(const unsigned int xid, struct cifs_open_parms *oparms, int *oplock,
1281 OPEN_REQ *req = NULL;
1282 OPEN_RSP *rsp = NULL;
1286 struct cifs_sb_info *cifs_sb = oparms->cifs_sb;
1287 struct cifs_tcon *tcon = oparms->tcon;
1288 int remap = cifs_remap(cifs_sb);
1289 const struct nls_table *nls = cifs_sb->local_nls;
1290 int create_options = oparms->create_options;
1291 int desired_access = oparms->desired_access;
1292 int disposition = oparms->disposition;
1293 const char *path = oparms->path;
1296 rc = smb_init(SMB_COM_NT_CREATE_ANDX, 24, tcon, (void **)&req,
1301 /* no commands go after this */
1302 req->AndXCommand = 0xFF;
1304 if (req->hdr.Flags2 & SMBFLG2_UNICODE) {
1305 /* account for one byte pad to word boundary */
1307 name_len = cifsConvertToUTF16((__le16 *)(req->fileName + 1),
1308 path, PATH_MAX, nls, remap);
1312 req->NameLength = cpu_to_le16(name_len);
1314 /* BB improve check for buffer overruns BB */
1317 name_len = strnlen(path, PATH_MAX);
1320 req->NameLength = cpu_to_le16(name_len);
1321 strncpy(req->fileName, path, name_len);
1324 if (*oplock & REQ_OPLOCK)
1325 req->OpenFlags = cpu_to_le32(REQ_OPLOCK);
1326 else if (*oplock & REQ_BATCHOPLOCK)
1327 req->OpenFlags = cpu_to_le32(REQ_BATCHOPLOCK);
1329 req->DesiredAccess = cpu_to_le32(desired_access);
1330 req->AllocationSize = 0;
1333 * Set file as system file if special file such as fifo and server
1334 * expecting SFU style and no Unix extensions.
1336 if (create_options & CREATE_OPTION_SPECIAL)
1337 req->FileAttributes = cpu_to_le32(ATTR_SYSTEM);
1339 req->FileAttributes = cpu_to_le32(ATTR_NORMAL);
1342 * XP does not handle ATTR_POSIX_SEMANTICS but it helps speed up case
1343 * sensitive checks for other servers such as Samba.
1345 if (tcon->ses->capabilities & CAP_UNIX)
1346 req->FileAttributes |= cpu_to_le32(ATTR_POSIX_SEMANTICS);
1348 if (create_options & CREATE_OPTION_READONLY)
1349 req->FileAttributes |= cpu_to_le32(ATTR_READONLY);
1351 req->ShareAccess = cpu_to_le32(FILE_SHARE_ALL);
1352 req->CreateDisposition = cpu_to_le32(disposition);
1353 req->CreateOptions = cpu_to_le32(create_options & CREATE_OPTIONS_MASK);
1355 /* BB Expirement with various impersonation levels and verify */
1356 req->ImpersonationLevel = cpu_to_le32(SECURITY_IMPERSONATION);
1357 req->SecurityFlags = SECURITY_CONTEXT_TRACKING|SECURITY_EFFECTIVE_ONLY;
1360 inc_rfc1001_len(req, count);
1362 req->ByteCount = cpu_to_le16(count);
1363 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *)req,
1364 (struct smb_hdr *)rsp, &bytes_returned, 0);
1365 cifs_stats_inc(&tcon->stats.cifs_stats.num_opens);
1367 cifs_dbg(FYI, "Error in Open = %d\n", rc);
1368 cifs_buf_release(req);
1374 /* 1 byte no need to le_to_cpu */
1375 *oplock = rsp->OplockLevel;
1376 /* cifs fid stays in le */
1377 oparms->fid->netfid = rsp->Fid;
1379 /* Let caller know file was created so we can set the mode. */
1380 /* Do we care about the CreateAction in any other cases? */
1381 if (cpu_to_le32(FILE_CREATE) == rsp->CreateAction)
1382 *oplock |= CIFS_CREATE_ACTION;
1385 /* copy from CreationTime to Attributes */
1386 memcpy((char *)buf, (char *)&rsp->CreationTime, 36);
1387 /* the file_info buf is endian converted by caller */
1388 buf->AllocationSize = rsp->AllocationSize;
1389 buf->EndOfFile = rsp->EndOfFile;
1390 buf->NumberOfLinks = cpu_to_le32(1);
1391 buf->DeletePending = 0;
1394 cifs_buf_release(req);
1399 * Discard any remaining data in the current SMB. To do this, we borrow the
1403 cifs_discard_remaining_data(struct TCP_Server_Info *server)
1405 unsigned int rfclen = get_rfc1002_length(server->smallbuf);
1406 int remaining = rfclen + 4 - server->total_read;
1408 while (remaining > 0) {
1411 length = cifs_read_from_socket(server, server->bigbuf,
1412 min_t(unsigned int, remaining,
1413 CIFSMaxBufSize + MAX_HEADER_SIZE(server)));
1416 server->total_read += length;
1417 remaining -= length;
1424 cifs_readv_discard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1427 struct cifs_readdata *rdata = mid->callback_data;
1429 length = cifs_discard_remaining_data(server);
1430 dequeue_mid(mid, rdata->result);
1431 mid->resp_buf = server->smallbuf;
1432 server->smallbuf = NULL;
1437 cifs_readv_receive(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1440 unsigned int data_offset, data_len;
1441 struct cifs_readdata *rdata = mid->callback_data;
1442 char *buf = server->smallbuf;
1443 unsigned int buflen = get_rfc1002_length(buf) + 4;
1445 cifs_dbg(FYI, "%s: mid=%llu offset=%llu bytes=%u\n",
1446 __func__, mid->mid, rdata->offset, rdata->bytes);
1449 * read the rest of READ_RSP header (sans Data array), or whatever we
1450 * can if there's not enough data. At this point, we've read down to
1453 len = min_t(unsigned int, buflen, server->vals->read_rsp_size) -
1454 HEADER_SIZE(server) + 1;
1456 length = cifs_read_from_socket(server,
1457 buf + HEADER_SIZE(server) - 1, len);
1460 server->total_read += length;
1462 if (server->ops->is_status_pending &&
1463 server->ops->is_status_pending(buf, server, 0)) {
1464 cifs_discard_remaining_data(server);
1468 /* Was the SMB read successful? */
1469 rdata->result = server->ops->map_error(buf, false);
1470 if (rdata->result != 0) {
1471 cifs_dbg(FYI, "%s: server returned error %d\n",
1472 __func__, rdata->result);
1473 return cifs_readv_discard(server, mid);
1476 /* Is there enough to get to the rest of the READ_RSP header? */
1477 if (server->total_read < server->vals->read_rsp_size) {
1478 cifs_dbg(FYI, "%s: server returned short header. got=%u expected=%zu\n",
1479 __func__, server->total_read,
1480 server->vals->read_rsp_size);
1481 rdata->result = -EIO;
1482 return cifs_readv_discard(server, mid);
1485 data_offset = server->ops->read_data_offset(buf) + 4;
1486 if (data_offset < server->total_read) {
1488 * win2k8 sometimes sends an offset of 0 when the read
1489 * is beyond the EOF. Treat it as if the data starts just after
1492 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
1493 __func__, data_offset);
1494 data_offset = server->total_read;
1495 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
1496 /* data_offset is beyond the end of smallbuf */
1497 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
1498 __func__, data_offset);
1499 rdata->result = -EIO;
1500 return cifs_readv_discard(server, mid);
1503 cifs_dbg(FYI, "%s: total_read=%u data_offset=%u\n",
1504 __func__, server->total_read, data_offset);
1506 len = data_offset - server->total_read;
1508 /* read any junk before data into the rest of smallbuf */
1509 length = cifs_read_from_socket(server,
1510 buf + server->total_read, len);
1513 server->total_read += length;
1516 /* set up first iov for signature check */
1517 rdata->iov[0].iov_base = buf;
1518 rdata->iov[0].iov_len = 4;
1519 rdata->iov[1].iov_base = buf + 4;
1520 rdata->iov[1].iov_len = server->total_read - 4;
1521 cifs_dbg(FYI, "0: iov_base=%p iov_len=%u\n",
1522 rdata->iov[0].iov_base, server->total_read);
1524 /* how much data is in the response? */
1525 data_len = server->ops->read_data_length(buf);
1526 if (data_offset + data_len > buflen) {
1527 /* data_len is corrupt -- discard frame */
1528 rdata->result = -EIO;
1529 return cifs_readv_discard(server, mid);
1532 length = rdata->read_into_pages(server, rdata, data_len);
1536 server->total_read += length;
1538 cifs_dbg(FYI, "total_read=%u buflen=%u remaining=%u\n",
1539 server->total_read, buflen, data_len);
1541 /* discard anything left over */
1542 if (server->total_read < buflen)
1543 return cifs_readv_discard(server, mid);
1545 dequeue_mid(mid, false);
1546 mid->resp_buf = server->smallbuf;
1547 server->smallbuf = NULL;
1552 cifs_readv_callback(struct mid_q_entry *mid)
1554 struct cifs_readdata *rdata = mid->callback_data;
1555 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
1556 struct TCP_Server_Info *server = tcon->ses->server;
1557 struct smb_rqst rqst = { .rq_iov = rdata->iov,
1559 .rq_pages = rdata->pages,
1560 .rq_npages = rdata->nr_pages,
1561 .rq_pagesz = rdata->pagesz,
1562 .rq_tailsz = rdata->tailsz };
1564 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
1565 __func__, mid->mid, mid->mid_state, rdata->result,
1568 switch (mid->mid_state) {
1569 case MID_RESPONSE_RECEIVED:
1570 /* result already set, check signature */
1574 rc = cifs_verify_signature(&rqst, server,
1575 mid->sequence_number);
1577 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
1580 /* FIXME: should this be counted toward the initiating task? */
1581 task_io_account_read(rdata->got_bytes);
1582 cifs_stats_bytes_read(tcon, rdata->got_bytes);
1584 case MID_REQUEST_SUBMITTED:
1585 case MID_RETRY_NEEDED:
1586 rdata->result = -EAGAIN;
1587 if (server->sign && rdata->got_bytes)
1588 /* reset bytes number since we can not check a sign */
1589 rdata->got_bytes = 0;
1590 /* FIXME: should this be counted toward the initiating task? */
1591 task_io_account_read(rdata->got_bytes);
1592 cifs_stats_bytes_read(tcon, rdata->got_bytes);
1595 rdata->result = -EIO;
1598 queue_work(cifsiod_wq, &rdata->work);
1599 mutex_lock(&server->srv_mutex);
1600 DeleteMidQEntry(mid);
1601 mutex_unlock(&server->srv_mutex);
1602 add_credits(server, 1, 0);
1605 /* cifs_async_readv - send an async write, and set up mid to handle result */
1607 cifs_async_readv(struct cifs_readdata *rdata)
1610 READ_REQ *smb = NULL;
1612 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
1613 struct smb_rqst rqst = { .rq_iov = rdata->iov,
1616 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
1617 __func__, rdata->offset, rdata->bytes);
1619 if (tcon->ses->capabilities & CAP_LARGE_FILES)
1622 wct = 10; /* old style read */
1623 if ((rdata->offset >> 32) > 0) {
1624 /* can not handle this big offset for old */
1629 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **)&smb);
1633 smb->hdr.Pid = cpu_to_le16((__u16)rdata->pid);
1634 smb->hdr.PidHigh = cpu_to_le16((__u16)(rdata->pid >> 16));
1636 smb->AndXCommand = 0xFF; /* none */
1637 smb->Fid = rdata->cfile->fid.netfid;
1638 smb->OffsetLow = cpu_to_le32(rdata->offset & 0xFFFFFFFF);
1640 smb->OffsetHigh = cpu_to_le32(rdata->offset >> 32);
1642 smb->MaxCount = cpu_to_le16(rdata->bytes & 0xFFFF);
1643 smb->MaxCountHigh = cpu_to_le32(rdata->bytes >> 16);
1647 /* old style read */
1648 struct smb_com_readx_req *smbr =
1649 (struct smb_com_readx_req *)smb;
1650 smbr->ByteCount = 0;
1653 /* 4 for RFC1001 length + 1 for BCC */
1654 rdata->iov[0].iov_base = smb;
1655 rdata->iov[0].iov_len = 4;
1656 rdata->iov[1].iov_base = (char *)smb + 4;
1657 rdata->iov[1].iov_len = get_rfc1002_length(smb);
1659 kref_get(&rdata->refcount);
1660 rc = cifs_call_async(tcon->ses->server, &rqst, cifs_readv_receive,
1661 cifs_readv_callback, NULL, rdata, 0);
1664 cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
1666 kref_put(&rdata->refcount, cifs_readdata_release);
1668 cifs_small_buf_release(smb);
1673 CIFSSMBRead(const unsigned int xid, struct cifs_io_parms *io_parms,
1674 unsigned int *nbytes, char **buf, int *pbuf_type)
1677 READ_REQ *pSMB = NULL;
1678 READ_RSP *pSMBr = NULL;
1679 char *pReadData = NULL;
1681 int resp_buf_type = 0;
1683 struct kvec rsp_iov;
1684 __u32 pid = io_parms->pid;
1685 __u16 netfid = io_parms->netfid;
1686 __u64 offset = io_parms->offset;
1687 struct cifs_tcon *tcon = io_parms->tcon;
1688 unsigned int count = io_parms->length;
1690 cifs_dbg(FYI, "Reading %d bytes on fid %d\n", count, netfid);
1691 if (tcon->ses->capabilities & CAP_LARGE_FILES)
1694 wct = 10; /* old style read */
1695 if ((offset >> 32) > 0) {
1696 /* can not handle this big offset for old */
1702 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **) &pSMB);
1706 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1707 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1709 /* tcon and ses pointer are checked in smb_init */
1710 if (tcon->ses->server == NULL)
1711 return -ECONNABORTED;
1713 pSMB->AndXCommand = 0xFF; /* none */
1715 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
1717 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
1719 pSMB->Remaining = 0;
1720 pSMB->MaxCount = cpu_to_le16(count & 0xFFFF);
1721 pSMB->MaxCountHigh = cpu_to_le32(count >> 16);
1723 pSMB->ByteCount = 0; /* no need to do le conversion since 0 */
1725 /* old style read */
1726 struct smb_com_readx_req *pSMBW =
1727 (struct smb_com_readx_req *)pSMB;
1728 pSMBW->ByteCount = 0;
1731 iov[0].iov_base = (char *)pSMB;
1732 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
1733 rc = SendReceive2(xid, tcon->ses, iov, 1, &resp_buf_type,
1734 CIFS_LOG_ERROR, &rsp_iov);
1735 cifs_small_buf_release(pSMB);
1736 cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
1737 pSMBr = (READ_RSP *)rsp_iov.iov_base;
1739 cifs_dbg(VFS, "Send error in read = %d\n", rc);
1741 int data_length = le16_to_cpu(pSMBr->DataLengthHigh);
1742 data_length = data_length << 16;
1743 data_length += le16_to_cpu(pSMBr->DataLength);
1744 *nbytes = data_length;
1746 /*check that DataLength would not go beyond end of SMB */
1747 if ((data_length > CIFSMaxBufSize)
1748 || (data_length > count)) {
1749 cifs_dbg(FYI, "bad length %d for count %d\n",
1750 data_length, count);
1754 pReadData = (char *) (&pSMBr->hdr.Protocol) +
1755 le16_to_cpu(pSMBr->DataOffset);
1756 /* if (rc = copy_to_user(buf, pReadData, data_length)) {
1757 cifs_dbg(VFS, "Faulting on read rc = %d\n",rc);
1759 }*/ /* can not use copy_to_user when using page cache*/
1761 memcpy(*buf, pReadData, data_length);
1766 free_rsp_buf(resp_buf_type, rsp_iov.iov_base);
1767 } else if (resp_buf_type != CIFS_NO_BUFFER) {
1768 /* return buffer to caller to free */
1769 *buf = rsp_iov.iov_base;
1770 if (resp_buf_type == CIFS_SMALL_BUFFER)
1771 *pbuf_type = CIFS_SMALL_BUFFER;
1772 else if (resp_buf_type == CIFS_LARGE_BUFFER)
1773 *pbuf_type = CIFS_LARGE_BUFFER;
1774 } /* else no valid buffer on return - leave as null */
1776 /* Note: On -EAGAIN error only caller can retry on handle based calls
1777 since file handle passed in no longer valid */
1783 CIFSSMBWrite(const unsigned int xid, struct cifs_io_parms *io_parms,
1784 unsigned int *nbytes, const char *buf)
1787 WRITE_REQ *pSMB = NULL;
1788 WRITE_RSP *pSMBr = NULL;
1789 int bytes_returned, wct;
1792 __u32 pid = io_parms->pid;
1793 __u16 netfid = io_parms->netfid;
1794 __u64 offset = io_parms->offset;
1795 struct cifs_tcon *tcon = io_parms->tcon;
1796 unsigned int count = io_parms->length;
1800 /* cifs_dbg(FYI, "write at %lld %d bytes\n", offset, count);*/
1801 if (tcon->ses == NULL)
1802 return -ECONNABORTED;
1804 if (tcon->ses->capabilities & CAP_LARGE_FILES)
1808 if ((offset >> 32) > 0) {
1809 /* can not handle big offset for old srv */
1814 rc = smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB,
1819 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1820 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1822 /* tcon and ses pointer are checked in smb_init */
1823 if (tcon->ses->server == NULL)
1824 return -ECONNABORTED;
1826 pSMB->AndXCommand = 0xFF; /* none */
1828 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
1830 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
1832 pSMB->Reserved = 0xFFFFFFFF;
1833 pSMB->WriteMode = 0;
1834 pSMB->Remaining = 0;
1836 /* Can increase buffer size if buffer is big enough in some cases ie we
1837 can send more if LARGE_WRITE_X capability returned by the server and if
1838 our buffer is big enough or if we convert to iovecs on socket writes
1839 and eliminate the copy to the CIFS buffer */
1840 if (tcon->ses->capabilities & CAP_LARGE_WRITE_X) {
1841 bytes_sent = min_t(const unsigned int, CIFSMaxBufSize, count);
1843 bytes_sent = (tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE)
1847 if (bytes_sent > count)
1850 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
1852 memcpy(pSMB->Data, buf, bytes_sent);
1853 else if (count != 0) {
1855 cifs_buf_release(pSMB);
1857 } /* else setting file size with write of zero bytes */
1859 byte_count = bytes_sent + 1; /* pad */
1860 else /* wct == 12 */
1861 byte_count = bytes_sent + 5; /* bigger pad, smaller smb hdr */
1863 pSMB->DataLengthLow = cpu_to_le16(bytes_sent & 0xFFFF);
1864 pSMB->DataLengthHigh = cpu_to_le16(bytes_sent >> 16);
1865 inc_rfc1001_len(pSMB, byte_count);
1868 pSMB->ByteCount = cpu_to_le16(byte_count);
1869 else { /* old style write has byte count 4 bytes earlier
1871 struct smb_com_writex_req *pSMBW =
1872 (struct smb_com_writex_req *)pSMB;
1873 pSMBW->ByteCount = cpu_to_le16(byte_count);
1876 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1877 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
1878 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
1880 cifs_dbg(FYI, "Send error in write = %d\n", rc);
1882 *nbytes = le16_to_cpu(pSMBr->CountHigh);
1883 *nbytes = (*nbytes) << 16;
1884 *nbytes += le16_to_cpu(pSMBr->Count);
1887 * Mask off high 16 bits when bytes written as returned by the
1888 * server is greater than bytes requested by the client. Some
1889 * OS/2 servers are known to set incorrect CountHigh values.
1891 if (*nbytes > count)
1895 cifs_buf_release(pSMB);
1897 /* Note: On -EAGAIN error only caller can retry on handle based calls
1898 since file handle passed in no longer valid */
1904 cifs_writedata_release(struct kref *refcount)
1906 struct cifs_writedata *wdata = container_of(refcount,
1907 struct cifs_writedata, refcount);
1910 cifsFileInfo_put(wdata->cfile);
1916 * Write failed with a retryable error. Resend the write request. It's also
1917 * possible that the page was redirtied so re-clean the page.
1920 cifs_writev_requeue(struct cifs_writedata *wdata)
1923 struct inode *inode = d_inode(wdata->cfile->dentry);
1924 struct TCP_Server_Info *server;
1925 unsigned int rest_len;
1927 server = tlink_tcon(wdata->cfile->tlink)->ses->server;
1929 rest_len = wdata->bytes;
1931 struct cifs_writedata *wdata2;
1932 unsigned int j, nr_pages, wsize, tailsz, cur_len;
1934 wsize = server->ops->wp_retry_size(inode);
1935 if (wsize < rest_len) {
1936 nr_pages = wsize / PAGE_SIZE;
1941 cur_len = nr_pages * PAGE_SIZE;
1944 nr_pages = DIV_ROUND_UP(rest_len, PAGE_SIZE);
1946 tailsz = rest_len - (nr_pages - 1) * PAGE_SIZE;
1949 wdata2 = cifs_writedata_alloc(nr_pages, cifs_writev_complete);
1955 for (j = 0; j < nr_pages; j++) {
1956 wdata2->pages[j] = wdata->pages[i + j];
1957 lock_page(wdata2->pages[j]);
1958 clear_page_dirty_for_io(wdata2->pages[j]);
1961 wdata2->sync_mode = wdata->sync_mode;
1962 wdata2->nr_pages = nr_pages;
1963 wdata2->offset = page_offset(wdata2->pages[0]);
1964 wdata2->pagesz = PAGE_SIZE;
1965 wdata2->tailsz = tailsz;
1966 wdata2->bytes = cur_len;
1968 wdata2->cfile = find_writable_file(CIFS_I(inode), false);
1969 if (!wdata2->cfile) {
1970 cifs_dbg(VFS, "No writable handles for inode\n");
1974 wdata2->pid = wdata2->cfile->pid;
1975 rc = server->ops->async_writev(wdata2, cifs_writedata_release);
1977 for (j = 0; j < nr_pages; j++) {
1978 unlock_page(wdata2->pages[j]);
1979 if (rc != 0 && rc != -EAGAIN) {
1980 SetPageError(wdata2->pages[j]);
1981 end_page_writeback(wdata2->pages[j]);
1982 put_page(wdata2->pages[j]);
1987 kref_put(&wdata2->refcount, cifs_writedata_release);
1993 rest_len -= cur_len;
1995 } while (i < wdata->nr_pages);
1997 mapping_set_error(inode->i_mapping, rc);
1998 kref_put(&wdata->refcount, cifs_writedata_release);
2002 cifs_writev_complete(struct work_struct *work)
2004 struct cifs_writedata *wdata = container_of(work,
2005 struct cifs_writedata, work);
2006 struct inode *inode = d_inode(wdata->cfile->dentry);
2009 if (wdata->result == 0) {
2010 spin_lock(&inode->i_lock);
2011 cifs_update_eof(CIFS_I(inode), wdata->offset, wdata->bytes);
2012 spin_unlock(&inode->i_lock);
2013 cifs_stats_bytes_written(tlink_tcon(wdata->cfile->tlink),
2015 } else if (wdata->sync_mode == WB_SYNC_ALL && wdata->result == -EAGAIN)
2016 return cifs_writev_requeue(wdata);
2018 for (i = 0; i < wdata->nr_pages; i++) {
2019 struct page *page = wdata->pages[i];
2020 if (wdata->result == -EAGAIN)
2021 __set_page_dirty_nobuffers(page);
2022 else if (wdata->result < 0)
2024 end_page_writeback(page);
2027 if (wdata->result != -EAGAIN)
2028 mapping_set_error(inode->i_mapping, wdata->result);
2029 kref_put(&wdata->refcount, cifs_writedata_release);
2032 struct cifs_writedata *
2033 cifs_writedata_alloc(unsigned int nr_pages, work_func_t complete)
2035 struct cifs_writedata *wdata;
2037 /* writedata + number of page pointers */
2038 wdata = kzalloc(sizeof(*wdata) +
2039 sizeof(struct page *) * nr_pages, GFP_NOFS);
2040 if (wdata != NULL) {
2041 kref_init(&wdata->refcount);
2042 INIT_LIST_HEAD(&wdata->list);
2043 init_completion(&wdata->done);
2044 INIT_WORK(&wdata->work, complete);
2050 * Check the mid_state and signature on received buffer (if any), and queue the
2051 * workqueue completion task.
2054 cifs_writev_callback(struct mid_q_entry *mid)
2056 struct cifs_writedata *wdata = mid->callback_data;
2057 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2058 struct TCP_Server_Info *server = tcon->ses->server;
2059 unsigned int written;
2060 WRITE_RSP *smb = (WRITE_RSP *)mid->resp_buf;
2062 switch (mid->mid_state) {
2063 case MID_RESPONSE_RECEIVED:
2064 wdata->result = cifs_check_receive(mid, tcon->ses->server, 0);
2065 if (wdata->result != 0)
2068 written = le16_to_cpu(smb->CountHigh);
2070 written += le16_to_cpu(smb->Count);
2072 * Mask off high 16 bits when bytes written as returned
2073 * by the server is greater than bytes requested by the
2074 * client. OS/2 servers are known to set incorrect
2077 if (written > wdata->bytes)
2080 if (written < wdata->bytes)
2081 wdata->result = -ENOSPC;
2083 wdata->bytes = written;
2085 case MID_REQUEST_SUBMITTED:
2086 case MID_RETRY_NEEDED:
2087 wdata->result = -EAGAIN;
2090 wdata->result = -EIO;
2094 queue_work(cifsiod_wq, &wdata->work);
2095 mutex_lock(&server->srv_mutex);
2096 DeleteMidQEntry(mid);
2097 mutex_unlock(&server->srv_mutex);
2098 add_credits(tcon->ses->server, 1, 0);
2101 /* cifs_async_writev - send an async write, and set up mid to handle result */
2103 cifs_async_writev(struct cifs_writedata *wdata,
2104 void (*release)(struct kref *kref))
2107 WRITE_REQ *smb = NULL;
2109 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2111 struct smb_rqst rqst = { };
2113 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
2117 if (wdata->offset >> 32 > 0) {
2118 /* can not handle big offset for old srv */
2123 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **)&smb);
2125 goto async_writev_out;
2127 smb->hdr.Pid = cpu_to_le16((__u16)wdata->pid);
2128 smb->hdr.PidHigh = cpu_to_le16((__u16)(wdata->pid >> 16));
2130 smb->AndXCommand = 0xFF; /* none */
2131 smb->Fid = wdata->cfile->fid.netfid;
2132 smb->OffsetLow = cpu_to_le32(wdata->offset & 0xFFFFFFFF);
2134 smb->OffsetHigh = cpu_to_le32(wdata->offset >> 32);
2135 smb->Reserved = 0xFFFFFFFF;
2140 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
2142 /* 4 for RFC1001 length + 1 for BCC */
2144 iov[0].iov_base = smb;
2145 iov[1].iov_len = get_rfc1002_length(smb) + 1;
2146 iov[1].iov_base = (char *)smb + 4;
2150 rqst.rq_pages = wdata->pages;
2151 rqst.rq_npages = wdata->nr_pages;
2152 rqst.rq_pagesz = wdata->pagesz;
2153 rqst.rq_tailsz = wdata->tailsz;
2155 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2156 wdata->offset, wdata->bytes);
2158 smb->DataLengthLow = cpu_to_le16(wdata->bytes & 0xFFFF);
2159 smb->DataLengthHigh = cpu_to_le16(wdata->bytes >> 16);
2162 inc_rfc1001_len(&smb->hdr, wdata->bytes + 1);
2163 put_bcc(wdata->bytes + 1, &smb->hdr);
2166 struct smb_com_writex_req *smbw =
2167 (struct smb_com_writex_req *)smb;
2168 inc_rfc1001_len(&smbw->hdr, wdata->bytes + 5);
2169 put_bcc(wdata->bytes + 5, &smbw->hdr);
2170 iov[1].iov_len += 4; /* pad bigger by four bytes */
2173 kref_get(&wdata->refcount);
2174 rc = cifs_call_async(tcon->ses->server, &rqst, NULL,
2175 cifs_writev_callback, NULL, wdata, 0);
2178 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
2180 kref_put(&wdata->refcount, release);
2183 cifs_small_buf_release(smb);
2188 CIFSSMBWrite2(const unsigned int xid, struct cifs_io_parms *io_parms,
2189 unsigned int *nbytes, struct kvec *iov, int n_vec)
2192 WRITE_REQ *pSMB = NULL;
2195 int resp_buf_type = 0;
2196 __u32 pid = io_parms->pid;
2197 __u16 netfid = io_parms->netfid;
2198 __u64 offset = io_parms->offset;
2199 struct cifs_tcon *tcon = io_parms->tcon;
2200 unsigned int count = io_parms->length;
2201 struct kvec rsp_iov;
2205 cifs_dbg(FYI, "write2 at %lld %d bytes\n", (long long)offset, count);
2207 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
2211 if ((offset >> 32) > 0) {
2212 /* can not handle big offset for old srv */
2216 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB);
2220 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
2221 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
2223 /* tcon and ses pointer are checked in smb_init */
2224 if (tcon->ses->server == NULL)
2225 return -ECONNABORTED;
2227 pSMB->AndXCommand = 0xFF; /* none */
2229 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
2231 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
2232 pSMB->Reserved = 0xFFFFFFFF;
2233 pSMB->WriteMode = 0;
2234 pSMB->Remaining = 0;
2237 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
2239 pSMB->DataLengthLow = cpu_to_le16(count & 0xFFFF);
2240 pSMB->DataLengthHigh = cpu_to_le16(count >> 16);
2241 /* header + 1 byte pad */
2242 smb_hdr_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 1;
2244 inc_rfc1001_len(pSMB, count + 1);
2245 else /* wct == 12 */
2246 inc_rfc1001_len(pSMB, count + 5); /* smb data starts later */
2248 pSMB->ByteCount = cpu_to_le16(count + 1);
2249 else /* wct == 12 */ /* bigger pad, smaller smb hdr, keep offset ok */ {
2250 struct smb_com_writex_req *pSMBW =
2251 (struct smb_com_writex_req *)pSMB;
2252 pSMBW->ByteCount = cpu_to_le16(count + 5);
2254 iov[0].iov_base = pSMB;
2256 iov[0].iov_len = smb_hdr_len + 4;
2257 else /* wct == 12 pad bigger by four bytes */
2258 iov[0].iov_len = smb_hdr_len + 8;
2260 rc = SendReceive2(xid, tcon->ses, iov, n_vec + 1, &resp_buf_type, 0,
2262 cifs_small_buf_release(pSMB);
2263 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
2265 cifs_dbg(FYI, "Send error Write2 = %d\n", rc);
2266 } else if (resp_buf_type == 0) {
2267 /* presumably this can not happen, but best to be safe */
2270 WRITE_RSP *pSMBr = (WRITE_RSP *)rsp_iov.iov_base;
2271 *nbytes = le16_to_cpu(pSMBr->CountHigh);
2272 *nbytes = (*nbytes) << 16;
2273 *nbytes += le16_to_cpu(pSMBr->Count);
2276 * Mask off high 16 bits when bytes written as returned by the
2277 * server is greater than bytes requested by the client. OS/2
2278 * servers are known to set incorrect CountHigh values.
2280 if (*nbytes > count)
2284 free_rsp_buf(resp_buf_type, rsp_iov.iov_base);
2286 /* Note: On -EAGAIN error only caller can retry on handle based calls
2287 since file handle passed in no longer valid */
2292 int cifs_lockv(const unsigned int xid, struct cifs_tcon *tcon,
2293 const __u16 netfid, const __u8 lock_type, const __u32 num_unlock,
2294 const __u32 num_lock, LOCKING_ANDX_RANGE *buf)
2297 LOCK_REQ *pSMB = NULL;
2299 struct kvec rsp_iov;
2303 cifs_dbg(FYI, "cifs_lockv num lock %d num unlock %d\n",
2304 num_lock, num_unlock);
2306 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2311 pSMB->NumberOfLocks = cpu_to_le16(num_lock);
2312 pSMB->NumberOfUnlocks = cpu_to_le16(num_unlock);
2313 pSMB->LockType = lock_type;
2314 pSMB->AndXCommand = 0xFF; /* none */
2315 pSMB->Fid = netfid; /* netfid stays le */
2317 count = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2318 inc_rfc1001_len(pSMB, count);
2319 pSMB->ByteCount = cpu_to_le16(count);
2321 iov[0].iov_base = (char *)pSMB;
2322 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4 -
2323 (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2324 iov[1].iov_base = (char *)buf;
2325 iov[1].iov_len = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2327 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
2328 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP,
2330 cifs_small_buf_release(pSMB);
2332 cifs_dbg(FYI, "Send error in cifs_lockv = %d\n", rc);
2338 CIFSSMBLock(const unsigned int xid, struct cifs_tcon *tcon,
2339 const __u16 smb_file_id, const __u32 netpid, const __u64 len,
2340 const __u64 offset, const __u32 numUnlock,
2341 const __u32 numLock, const __u8 lockType,
2342 const bool waitFlag, const __u8 oplock_level)
2345 LOCK_REQ *pSMB = NULL;
2346 /* LOCK_RSP *pSMBr = NULL; */ /* No response data other than rc to parse */
2351 cifs_dbg(FYI, "CIFSSMBLock timeout %d numLock %d\n",
2352 (int)waitFlag, numLock);
2353 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2358 if (lockType == LOCKING_ANDX_OPLOCK_RELEASE) {
2359 /* no response expected */
2360 flags = CIFS_ASYNC_OP | CIFS_OBREAK_OP;
2362 } else if (waitFlag) {
2363 flags = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
2364 pSMB->Timeout = cpu_to_le32(-1);/* blocking - do not time out */
2369 pSMB->NumberOfLocks = cpu_to_le16(numLock);
2370 pSMB->NumberOfUnlocks = cpu_to_le16(numUnlock);
2371 pSMB->LockType = lockType;
2372 pSMB->OplockLevel = oplock_level;
2373 pSMB->AndXCommand = 0xFF; /* none */
2374 pSMB->Fid = smb_file_id; /* netfid stays le */
2376 if ((numLock != 0) || (numUnlock != 0)) {
2377 pSMB->Locks[0].Pid = cpu_to_le16(netpid);
2378 /* BB where to store pid high? */
2379 pSMB->Locks[0].LengthLow = cpu_to_le32((u32)len);
2380 pSMB->Locks[0].LengthHigh = cpu_to_le32((u32)(len>>32));
2381 pSMB->Locks[0].OffsetLow = cpu_to_le32((u32)offset);
2382 pSMB->Locks[0].OffsetHigh = cpu_to_le32((u32)(offset>>32));
2383 count = sizeof(LOCKING_ANDX_RANGE);
2388 inc_rfc1001_len(pSMB, count);
2389 pSMB->ByteCount = cpu_to_le16(count);
2392 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
2393 (struct smb_hdr *) pSMB, &bytes_returned);
2395 rc = SendReceiveNoRsp(xid, tcon->ses, (char *)pSMB, flags);
2396 cifs_small_buf_release(pSMB);
2397 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
2399 cifs_dbg(FYI, "Send error in Lock = %d\n", rc);
2401 /* Note: On -EAGAIN error only caller can retry on handle based calls
2402 since file handle passed in no longer valid */
2407 CIFSSMBPosixLock(const unsigned int xid, struct cifs_tcon *tcon,
2408 const __u16 smb_file_id, const __u32 netpid,
2409 const loff_t start_offset, const __u64 len,
2410 struct file_lock *pLockData, const __u16 lock_type,
2411 const bool waitFlag)
2413 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2414 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
2415 struct cifs_posix_lock *parm_data;
2418 int bytes_returned = 0;
2419 int resp_buf_type = 0;
2420 __u16 params, param_offset, offset, byte_count, count;
2422 struct kvec rsp_iov;
2424 cifs_dbg(FYI, "Posix Lock\n");
2426 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
2431 pSMBr = (struct smb_com_transaction2_sfi_rsp *)pSMB;
2434 pSMB->MaxSetupCount = 0;
2437 pSMB->Reserved2 = 0;
2438 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2439 offset = param_offset + params;
2441 count = sizeof(struct cifs_posix_lock);
2442 pSMB->MaxParameterCount = cpu_to_le16(2);
2443 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
2444 pSMB->SetupCount = 1;
2445 pSMB->Reserved3 = 0;
2447 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
2449 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2450 byte_count = 3 /* pad */ + params + count;
2451 pSMB->DataCount = cpu_to_le16(count);
2452 pSMB->ParameterCount = cpu_to_le16(params);
2453 pSMB->TotalDataCount = pSMB->DataCount;
2454 pSMB->TotalParameterCount = pSMB->ParameterCount;
2455 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2456 parm_data = (struct cifs_posix_lock *)
2457 (((char *) &pSMB->hdr.Protocol) + offset);
2459 parm_data->lock_type = cpu_to_le16(lock_type);
2461 timeout = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
2462 parm_data->lock_flags = cpu_to_le16(1);
2463 pSMB->Timeout = cpu_to_le32(-1);
2467 parm_data->pid = cpu_to_le32(netpid);
2468 parm_data->start = cpu_to_le64(start_offset);
2469 parm_data->length = cpu_to_le64(len); /* normalize negative numbers */
2471 pSMB->DataOffset = cpu_to_le16(offset);
2472 pSMB->Fid = smb_file_id;
2473 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_LOCK);
2474 pSMB->Reserved4 = 0;
2475 inc_rfc1001_len(pSMB, byte_count);
2476 pSMB->ByteCount = cpu_to_le16(byte_count);
2478 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
2479 (struct smb_hdr *) pSMBr, &bytes_returned);
2481 iov[0].iov_base = (char *)pSMB;
2482 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
2483 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
2484 &resp_buf_type, timeout, &rsp_iov);
2485 pSMBr = (struct smb_com_transaction2_sfi_rsp *)rsp_iov.iov_base;
2487 cifs_small_buf_release(pSMB);
2490 cifs_dbg(FYI, "Send error in Posix Lock = %d\n", rc);
2491 } else if (pLockData) {
2492 /* lock structure can be returned on get */
2495 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
2497 if (rc || get_bcc(&pSMBr->hdr) < sizeof(*parm_data)) {
2498 rc = -EIO; /* bad smb */
2501 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
2502 data_count = le16_to_cpu(pSMBr->t2.DataCount);
2503 if (data_count < sizeof(struct cifs_posix_lock)) {
2507 parm_data = (struct cifs_posix_lock *)
2508 ((char *)&pSMBr->hdr.Protocol + data_offset);
2509 if (parm_data->lock_type == cpu_to_le16(CIFS_UNLCK))
2510 pLockData->fl_type = F_UNLCK;
2512 if (parm_data->lock_type ==
2513 cpu_to_le16(CIFS_RDLCK))
2514 pLockData->fl_type = F_RDLCK;
2515 else if (parm_data->lock_type ==
2516 cpu_to_le16(CIFS_WRLCK))
2517 pLockData->fl_type = F_WRLCK;
2519 pLockData->fl_start = le64_to_cpu(parm_data->start);
2520 pLockData->fl_end = pLockData->fl_start +
2521 le64_to_cpu(parm_data->length) - 1;
2522 pLockData->fl_pid = le32_to_cpu(parm_data->pid);
2527 free_rsp_buf(resp_buf_type, rsp_iov.iov_base);
2529 /* Note: On -EAGAIN error only caller can retry on handle based calls
2530 since file handle passed in no longer valid */
2537 CIFSSMBClose(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
2540 CLOSE_REQ *pSMB = NULL;
2541 cifs_dbg(FYI, "In CIFSSMBClose\n");
2543 /* do not retry on dead session on close */
2544 rc = small_smb_init(SMB_COM_CLOSE, 3, tcon, (void **) &pSMB);
2550 pSMB->FileID = (__u16) smb_file_id;
2551 pSMB->LastWriteTime = 0xFFFFFFFF;
2552 pSMB->ByteCount = 0;
2553 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
2554 cifs_small_buf_release(pSMB);
2555 cifs_stats_inc(&tcon->stats.cifs_stats.num_closes);
2558 /* EINTR is expected when user ctl-c to kill app */
2559 cifs_dbg(VFS, "Send error in Close = %d\n", rc);
2563 /* Since session is dead, file will be closed on server already */
2571 CIFSSMBFlush(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
2574 FLUSH_REQ *pSMB = NULL;
2575 cifs_dbg(FYI, "In CIFSSMBFlush\n");
2577 rc = small_smb_init(SMB_COM_FLUSH, 1, tcon, (void **) &pSMB);
2581 pSMB->FileID = (__u16) smb_file_id;
2582 pSMB->ByteCount = 0;
2583 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
2584 cifs_small_buf_release(pSMB);
2585 cifs_stats_inc(&tcon->stats.cifs_stats.num_flushes);
2587 cifs_dbg(VFS, "Send error in Flush = %d\n", rc);
2593 CIFSSMBRename(const unsigned int xid, struct cifs_tcon *tcon,
2594 const char *from_name, const char *to_name,
2595 struct cifs_sb_info *cifs_sb)
2598 RENAME_REQ *pSMB = NULL;
2599 RENAME_RSP *pSMBr = NULL;
2601 int name_len, name_len2;
2603 int remap = cifs_remap(cifs_sb);
2605 cifs_dbg(FYI, "In CIFSSMBRename\n");
2607 rc = smb_init(SMB_COM_RENAME, 1, tcon, (void **) &pSMB,
2612 pSMB->BufferFormat = 0x04;
2613 pSMB->SearchAttributes =
2614 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
2617 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2618 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2619 from_name, PATH_MAX,
2620 cifs_sb->local_nls, remap);
2621 name_len++; /* trailing null */
2623 pSMB->OldFileName[name_len] = 0x04; /* pad */
2624 /* protocol requires ASCII signature byte on Unicode string */
2625 pSMB->OldFileName[name_len + 1] = 0x00;
2627 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2628 to_name, PATH_MAX, cifs_sb->local_nls,
2630 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2631 name_len2 *= 2; /* convert to bytes */
2632 } else { /* BB improve the check for buffer overruns BB */
2633 name_len = strnlen(from_name, PATH_MAX);
2634 name_len++; /* trailing null */
2635 strncpy(pSMB->OldFileName, from_name, name_len);
2636 name_len2 = strnlen(to_name, PATH_MAX);
2637 name_len2++; /* trailing null */
2638 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2639 strncpy(&pSMB->OldFileName[name_len + 1], to_name, name_len2);
2640 name_len2++; /* trailing null */
2641 name_len2++; /* signature byte */
2644 count = 1 /* 1st signature byte */ + name_len + name_len2;
2645 inc_rfc1001_len(pSMB, count);
2646 pSMB->ByteCount = cpu_to_le16(count);
2648 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2649 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2650 cifs_stats_inc(&tcon->stats.cifs_stats.num_renames);
2652 cifs_dbg(FYI, "Send error in rename = %d\n", rc);
2654 cifs_buf_release(pSMB);
2662 int CIFSSMBRenameOpenFile(const unsigned int xid, struct cifs_tcon *pTcon,
2663 int netfid, const char *target_name,
2664 const struct nls_table *nls_codepage, int remap)
2666 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2667 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
2668 struct set_file_rename *rename_info;
2670 char dummy_string[30];
2672 int bytes_returned = 0;
2674 __u16 params, param_offset, offset, count, byte_count;
2676 cifs_dbg(FYI, "Rename to File by handle\n");
2677 rc = smb_init(SMB_COM_TRANSACTION2, 15, pTcon, (void **) &pSMB,
2683 pSMB->MaxSetupCount = 0;
2687 pSMB->Reserved2 = 0;
2688 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2689 offset = param_offset + params;
2691 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2692 rename_info = (struct set_file_rename *) data_offset;
2693 pSMB->MaxParameterCount = cpu_to_le16(2);
2694 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
2695 pSMB->SetupCount = 1;
2696 pSMB->Reserved3 = 0;
2697 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2698 byte_count = 3 /* pad */ + params;
2699 pSMB->ParameterCount = cpu_to_le16(params);
2700 pSMB->TotalParameterCount = pSMB->ParameterCount;
2701 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2702 pSMB->DataOffset = cpu_to_le16(offset);
2703 /* construct random name ".cifs_tmp<inodenum><mid>" */
2704 rename_info->overwrite = cpu_to_le32(1);
2705 rename_info->root_fid = 0;
2706 /* unicode only call */
2707 if (target_name == NULL) {
2708 sprintf(dummy_string, "cifs%x", pSMB->hdr.Mid);
2710 cifsConvertToUTF16((__le16 *)rename_info->target_name,
2711 dummy_string, 24, nls_codepage, remap);
2714 cifsConvertToUTF16((__le16 *)rename_info->target_name,
2715 target_name, PATH_MAX, nls_codepage,
2718 rename_info->target_name_len = cpu_to_le32(2 * len_of_str);
2719 count = 12 /* sizeof(struct set_file_rename) */ + (2 * len_of_str);
2720 byte_count += count;
2721 pSMB->DataCount = cpu_to_le16(count);
2722 pSMB->TotalDataCount = pSMB->DataCount;
2724 pSMB->InformationLevel =
2725 cpu_to_le16(SMB_SET_FILE_RENAME_INFORMATION);
2726 pSMB->Reserved4 = 0;
2727 inc_rfc1001_len(pSMB, byte_count);
2728 pSMB->ByteCount = cpu_to_le16(byte_count);
2729 rc = SendReceive(xid, pTcon->ses, (struct smb_hdr *) pSMB,
2730 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2731 cifs_stats_inc(&pTcon->stats.cifs_stats.num_t2renames);
2733 cifs_dbg(FYI, "Send error in Rename (by file handle) = %d\n",
2736 cifs_buf_release(pSMB);
2738 /* Note: On -EAGAIN error only caller can retry on handle based calls
2739 since file handle passed in no longer valid */
2745 CIFSSMBCopy(const unsigned int xid, struct cifs_tcon *tcon,
2746 const char *fromName, const __u16 target_tid, const char *toName,
2747 const int flags, const struct nls_table *nls_codepage, int remap)
2750 COPY_REQ *pSMB = NULL;
2751 COPY_RSP *pSMBr = NULL;
2753 int name_len, name_len2;
2756 cifs_dbg(FYI, "In CIFSSMBCopy\n");
2758 rc = smb_init(SMB_COM_COPY, 1, tcon, (void **) &pSMB,
2763 pSMB->BufferFormat = 0x04;
2764 pSMB->Tid2 = target_tid;
2766 pSMB->Flags = cpu_to_le16(flags & COPY_TREE);
2768 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2769 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2770 fromName, PATH_MAX, nls_codepage,
2772 name_len++; /* trailing null */
2774 pSMB->OldFileName[name_len] = 0x04; /* pad */
2775 /* protocol requires ASCII signature byte on Unicode string */
2776 pSMB->OldFileName[name_len + 1] = 0x00;
2778 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2779 toName, PATH_MAX, nls_codepage, remap);
2780 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2781 name_len2 *= 2; /* convert to bytes */
2782 } else { /* BB improve the check for buffer overruns BB */
2783 name_len = strnlen(fromName, PATH_MAX);
2784 name_len++; /* trailing null */
2785 strncpy(pSMB->OldFileName, fromName, name_len);
2786 name_len2 = strnlen(toName, PATH_MAX);
2787 name_len2++; /* trailing null */
2788 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2789 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
2790 name_len2++; /* trailing null */
2791 name_len2++; /* signature byte */
2794 count = 1 /* 1st signature byte */ + name_len + name_len2;
2795 inc_rfc1001_len(pSMB, count);
2796 pSMB->ByteCount = cpu_to_le16(count);
2798 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2799 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2801 cifs_dbg(FYI, "Send error in copy = %d with %d files copied\n",
2802 rc, le16_to_cpu(pSMBr->CopyCount));
2804 cifs_buf_release(pSMB);
2813 CIFSUnixCreateSymLink(const unsigned int xid, struct cifs_tcon *tcon,
2814 const char *fromName, const char *toName,
2815 const struct nls_table *nls_codepage, int remap)
2817 TRANSACTION2_SPI_REQ *pSMB = NULL;
2818 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2821 int name_len_target;
2823 int bytes_returned = 0;
2824 __u16 params, param_offset, offset, byte_count;
2826 cifs_dbg(FYI, "In Symlink Unix style\n");
2828 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2833 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2835 cifsConvertToUTF16((__le16 *) pSMB->FileName, fromName,
2836 /* find define for this maxpathcomponent */
2837 PATH_MAX, nls_codepage, remap);
2838 name_len++; /* trailing null */
2841 } else { /* BB improve the check for buffer overruns BB */
2842 name_len = strnlen(fromName, PATH_MAX);
2843 name_len++; /* trailing null */
2844 strncpy(pSMB->FileName, fromName, name_len);
2846 params = 6 + name_len;
2847 pSMB->MaxSetupCount = 0;
2851 pSMB->Reserved2 = 0;
2852 param_offset = offsetof(struct smb_com_transaction2_spi_req,
2853 InformationLevel) - 4;
2854 offset = param_offset + params;
2856 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2857 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2859 cifsConvertToUTF16((__le16 *) data_offset, toName,
2860 /* find define for this maxpathcomponent */
2861 PATH_MAX, nls_codepage, remap);
2862 name_len_target++; /* trailing null */
2863 name_len_target *= 2;
2864 } else { /* BB improve the check for buffer overruns BB */
2865 name_len_target = strnlen(toName, PATH_MAX);
2866 name_len_target++; /* trailing null */
2867 strncpy(data_offset, toName, name_len_target);
2870 pSMB->MaxParameterCount = cpu_to_le16(2);
2871 /* BB find exact max on data count below from sess */
2872 pSMB->MaxDataCount = cpu_to_le16(1000);
2873 pSMB->SetupCount = 1;
2874 pSMB->Reserved3 = 0;
2875 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2876 byte_count = 3 /* pad */ + params + name_len_target;
2877 pSMB->DataCount = cpu_to_le16(name_len_target);
2878 pSMB->ParameterCount = cpu_to_le16(params);
2879 pSMB->TotalDataCount = pSMB->DataCount;
2880 pSMB->TotalParameterCount = pSMB->ParameterCount;
2881 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2882 pSMB->DataOffset = cpu_to_le16(offset);
2883 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_LINK);
2884 pSMB->Reserved4 = 0;
2885 inc_rfc1001_len(pSMB, byte_count);
2886 pSMB->ByteCount = cpu_to_le16(byte_count);
2887 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2888 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2889 cifs_stats_inc(&tcon->stats.cifs_stats.num_symlinks);
2891 cifs_dbg(FYI, "Send error in SetPathInfo create symlink = %d\n",
2894 cifs_buf_release(pSMB);
2897 goto createSymLinkRetry;
2903 CIFSUnixCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon,
2904 const char *fromName, const char *toName,
2905 const struct nls_table *nls_codepage, int remap)
2907 TRANSACTION2_SPI_REQ *pSMB = NULL;
2908 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2911 int name_len_target;
2913 int bytes_returned = 0;
2914 __u16 params, param_offset, offset, byte_count;
2916 cifs_dbg(FYI, "In Create Hard link Unix style\n");
2917 createHardLinkRetry:
2918 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2923 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2924 name_len = cifsConvertToUTF16((__le16 *) pSMB->FileName, toName,
2925 PATH_MAX, nls_codepage, remap);
2926 name_len++; /* trailing null */
2929 } else { /* BB improve the check for buffer overruns BB */
2930 name_len = strnlen(toName, PATH_MAX);
2931 name_len++; /* trailing null */
2932 strncpy(pSMB->FileName, toName, name_len);
2934 params = 6 + name_len;
2935 pSMB->MaxSetupCount = 0;
2939 pSMB->Reserved2 = 0;
2940 param_offset = offsetof(struct smb_com_transaction2_spi_req,
2941 InformationLevel) - 4;
2942 offset = param_offset + params;
2944 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2945 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2947 cifsConvertToUTF16((__le16 *) data_offset, fromName,
2948 PATH_MAX, nls_codepage, remap);
2949 name_len_target++; /* trailing null */
2950 name_len_target *= 2;
2951 } else { /* BB improve the check for buffer overruns BB */
2952 name_len_target = strnlen(fromName, PATH_MAX);
2953 name_len_target++; /* trailing null */
2954 strncpy(data_offset, fromName, name_len_target);
2957 pSMB->MaxParameterCount = cpu_to_le16(2);
2958 /* BB find exact max on data count below from sess*/
2959 pSMB->MaxDataCount = cpu_to_le16(1000);
2960 pSMB->SetupCount = 1;
2961 pSMB->Reserved3 = 0;
2962 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2963 byte_count = 3 /* pad */ + params + name_len_target;
2964 pSMB->ParameterCount = cpu_to_le16(params);
2965 pSMB->TotalParameterCount = pSMB->ParameterCount;
2966 pSMB->DataCount = cpu_to_le16(name_len_target);
2967 pSMB->TotalDataCount = pSMB->DataCount;
2968 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2969 pSMB->DataOffset = cpu_to_le16(offset);
2970 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_HLINK);
2971 pSMB->Reserved4 = 0;
2972 inc_rfc1001_len(pSMB, byte_count);
2973 pSMB->ByteCount = cpu_to_le16(byte_count);
2974 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2975 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2976 cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
2978 cifs_dbg(FYI, "Send error in SetPathInfo (hard link) = %d\n",
2981 cifs_buf_release(pSMB);
2983 goto createHardLinkRetry;
2989 CIFSCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon,
2990 const char *from_name, const char *to_name,
2991 struct cifs_sb_info *cifs_sb)
2994 NT_RENAME_REQ *pSMB = NULL;
2995 RENAME_RSP *pSMBr = NULL;
2997 int name_len, name_len2;
2999 int remap = cifs_remap(cifs_sb);
3001 cifs_dbg(FYI, "In CIFSCreateHardLink\n");
3002 winCreateHardLinkRetry:
3004 rc = smb_init(SMB_COM_NT_RENAME, 4, tcon, (void **) &pSMB,
3009 pSMB->SearchAttributes =
3010 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
3012 pSMB->Flags = cpu_to_le16(CREATE_HARD_LINK);
3013 pSMB->ClusterCount = 0;
3015 pSMB->BufferFormat = 0x04;
3017 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3019 cifsConvertToUTF16((__le16 *) pSMB->OldFileName, from_name,
3020 PATH_MAX, cifs_sb->local_nls, remap);
3021 name_len++; /* trailing null */
3024 /* protocol specifies ASCII buffer format (0x04) for unicode */
3025 pSMB->OldFileName[name_len] = 0x04;
3026 pSMB->OldFileName[name_len + 1] = 0x00; /* pad */
3028 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
3029 to_name, PATH_MAX, cifs_sb->local_nls,
3031 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
3032 name_len2 *= 2; /* convert to bytes */
3033 } else { /* BB improve the check for buffer overruns BB */
3034 name_len = strnlen(from_name, PATH_MAX);
3035 name_len++; /* trailing null */
3036 strncpy(pSMB->OldFileName, from_name, name_len);
3037 name_len2 = strnlen(to_name, PATH_MAX);
3038 name_len2++; /* trailing null */
3039 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
3040 strncpy(&pSMB->OldFileName[name_len + 1], to_name, name_len2);
3041 name_len2++; /* trailing null */
3042 name_len2++; /* signature byte */
3045 count = 1 /* string type byte */ + name_len + name_len2;
3046 inc_rfc1001_len(pSMB, count);
3047 pSMB->ByteCount = cpu_to_le16(count);
3049 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3050 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3051 cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
3053 cifs_dbg(FYI, "Send error in hard link (NT rename) = %d\n", rc);
3055 cifs_buf_release(pSMB);
3057 goto winCreateHardLinkRetry;
3063 CIFSSMBUnixQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon,
3064 const unsigned char *searchName, char **symlinkinfo,
3065 const struct nls_table *nls_codepage, int remap)
3067 /* SMB_QUERY_FILE_UNIX_LINK */
3068 TRANSACTION2_QPI_REQ *pSMB = NULL;
3069 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3073 __u16 params, byte_count;
3076 cifs_dbg(FYI, "In QPathSymLinkInfo (Unix) for path %s\n", searchName);
3079 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3084 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3086 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3087 searchName, PATH_MAX, nls_codepage,
3089 name_len++; /* trailing null */
3091 } else { /* BB improve the check for buffer overruns BB */
3092 name_len = strnlen(searchName, PATH_MAX);
3093 name_len++; /* trailing null */
3094 strncpy(pSMB->FileName, searchName, name_len);
3097 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3098 pSMB->TotalDataCount = 0;
3099 pSMB->MaxParameterCount = cpu_to_le16(2);
3100 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
3101 pSMB->MaxSetupCount = 0;
3105 pSMB->Reserved2 = 0;
3106 pSMB->ParameterOffset = cpu_to_le16(offsetof(
3107 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
3108 pSMB->DataCount = 0;
3109 pSMB->DataOffset = 0;
3110 pSMB->SetupCount = 1;
3111 pSMB->Reserved3 = 0;
3112 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3113 byte_count = params + 1 /* pad */ ;
3114 pSMB->TotalParameterCount = cpu_to_le16(params);
3115 pSMB->ParameterCount = pSMB->TotalParameterCount;
3116 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_LINK);
3117 pSMB->Reserved4 = 0;
3118 inc_rfc1001_len(pSMB, byte_count);
3119 pSMB->ByteCount = cpu_to_le16(byte_count);
3121 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3122 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3124 cifs_dbg(FYI, "Send error in QuerySymLinkInfo = %d\n", rc);
3126 /* decode response */
3128 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
3129 /* BB also check enough total bytes returned */
3130 if (rc || get_bcc(&pSMBr->hdr) < 2)
3134 u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3136 data_start = ((char *) &pSMBr->hdr.Protocol) +
3137 le16_to_cpu(pSMBr->t2.DataOffset);
3139 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3144 /* BB FIXME investigate remapping reserved chars here */
3145 *symlinkinfo = cifs_strndup_from_utf16(data_start,
3146 count, is_unicode, nls_codepage);
3151 cifs_buf_release(pSMB);
3153 goto querySymLinkRetry;
3158 * Recent Windows versions now create symlinks more frequently
3159 * and they use the "reparse point" mechanism below. We can of course
3160 * do symlinks nicely to Samba and other servers which support the
3161 * CIFS Unix Extensions and we can also do SFU symlinks and "client only"
3162 * "MF" symlinks optionally, but for recent Windows we really need to
3163 * reenable the code below and fix the cifs_symlink callers to handle this.
3164 * In the interim this code has been moved to its own config option so
3165 * it is not compiled in by default until callers fixed up and more tested.
3168 CIFSSMBQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon,
3169 __u16 fid, char **symlinkinfo,
3170 const struct nls_table *nls_codepage)
3174 struct smb_com_transaction_ioctl_req *pSMB;
3175 struct smb_com_transaction_ioctl_rsp *pSMBr;
3177 unsigned int sub_len;
3179 struct reparse_symlink_data *reparse_buf;
3180 struct reparse_posix_data *posix_buf;
3181 __u32 data_offset, data_count;
3184 cifs_dbg(FYI, "In Windows reparse style QueryLink for fid %u\n", fid);
3185 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3190 pSMB->TotalParameterCount = 0 ;
3191 pSMB->TotalDataCount = 0;
3192 pSMB->MaxParameterCount = cpu_to_le32(2);
3193 /* BB find exact data count max from sess structure BB */
3194 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
3195 pSMB->MaxSetupCount = 4;
3197 pSMB->ParameterOffset = 0;
3198 pSMB->DataCount = 0;
3199 pSMB->DataOffset = 0;
3200 pSMB->SetupCount = 4;
3201 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
3202 pSMB->ParameterCount = pSMB->TotalParameterCount;
3203 pSMB->FunctionCode = cpu_to_le32(FSCTL_GET_REPARSE_POINT);
3204 pSMB->IsFsctl = 1; /* FSCTL */
3205 pSMB->IsRootFlag = 0;
3206 pSMB->Fid = fid; /* file handle always le */
3207 pSMB->ByteCount = 0;
3209 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3210 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3212 cifs_dbg(FYI, "Send error in QueryReparseLinkInfo = %d\n", rc);
3216 data_offset = le32_to_cpu(pSMBr->DataOffset);
3217 data_count = le32_to_cpu(pSMBr->DataCount);
3218 if (get_bcc(&pSMBr->hdr) < 2 || data_offset > 512) {
3219 /* BB also check enough total bytes returned */
3220 rc = -EIO; /* bad smb */
3223 if (!data_count || (data_count > 2048)) {
3225 cifs_dbg(FYI, "Invalid return data count on get reparse info ioctl\n");
3228 end_of_smb = 2 + get_bcc(&pSMBr->hdr) + (char *)&pSMBr->ByteCount;
3229 reparse_buf = (struct reparse_symlink_data *)
3230 ((char *)&pSMBr->hdr.Protocol + data_offset);
3231 if ((char *)reparse_buf >= end_of_smb) {
3235 if (reparse_buf->ReparseTag == cpu_to_le32(IO_REPARSE_TAG_NFS)) {
3236 cifs_dbg(FYI, "NFS style reparse tag\n");
3237 posix_buf = (struct reparse_posix_data *)reparse_buf;
3239 if (posix_buf->InodeType != cpu_to_le64(NFS_SPECFILE_LNK)) {
3240 cifs_dbg(FYI, "unsupported file type 0x%llx\n",
3241 le64_to_cpu(posix_buf->InodeType));
3246 sub_len = le16_to_cpu(reparse_buf->ReparseDataLength);
3247 if (posix_buf->PathBuffer + sub_len > end_of_smb) {
3248 cifs_dbg(FYI, "reparse buf beyond SMB\n");
3252 *symlinkinfo = cifs_strndup_from_utf16(posix_buf->PathBuffer,
3253 sub_len, is_unicode, nls_codepage);
3255 } else if (reparse_buf->ReparseTag !=
3256 cpu_to_le32(IO_REPARSE_TAG_SYMLINK)) {
3261 /* Reparse tag is NTFS symlink */
3262 sub_start = le16_to_cpu(reparse_buf->SubstituteNameOffset) +
3263 reparse_buf->PathBuffer;
3264 sub_len = le16_to_cpu(reparse_buf->SubstituteNameLength);
3265 if (sub_start + sub_len > end_of_smb) {
3266 cifs_dbg(FYI, "reparse buf beyond SMB\n");
3270 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3275 /* BB FIXME investigate remapping reserved chars here */
3276 *symlinkinfo = cifs_strndup_from_utf16(sub_start, sub_len, is_unicode,
3281 cifs_buf_release(pSMB);
3284 * Note: On -EAGAIN error only caller can retry on handle based calls
3285 * since file handle passed in no longer valid.
3291 CIFSSMB_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
3296 struct smb_com_transaction_compr_ioctl_req *pSMB;
3297 struct smb_com_transaction_ioctl_rsp *pSMBr;
3299 cifs_dbg(FYI, "Set compression for %u\n", fid);
3300 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3305 pSMB->compression_state = cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
3307 pSMB->TotalParameterCount = 0;
3308 pSMB->TotalDataCount = cpu_to_le32(2);
3309 pSMB->MaxParameterCount = 0;
3310 pSMB->MaxDataCount = 0;
3311 pSMB->MaxSetupCount = 4;
3313 pSMB->ParameterOffset = 0;
3314 pSMB->DataCount = cpu_to_le32(2);
3316 cpu_to_le32(offsetof(struct smb_com_transaction_compr_ioctl_req,
3317 compression_state) - 4); /* 84 */
3318 pSMB->SetupCount = 4;
3319 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
3320 pSMB->ParameterCount = 0;
3321 pSMB->FunctionCode = cpu_to_le32(FSCTL_SET_COMPRESSION);
3322 pSMB->IsFsctl = 1; /* FSCTL */
3323 pSMB->IsRootFlag = 0;
3324 pSMB->Fid = fid; /* file handle always le */
3325 /* 3 byte pad, followed by 2 byte compress state */
3326 pSMB->ByteCount = cpu_to_le16(5);
3327 inc_rfc1001_len(pSMB, 5);
3329 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3330 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3332 cifs_dbg(FYI, "Send error in SetCompression = %d\n", rc);
3334 cifs_buf_release(pSMB);
3337 * Note: On -EAGAIN error only caller can retry on handle based calls
3338 * since file handle passed in no longer valid.
3344 #ifdef CONFIG_CIFS_POSIX
3346 /*Convert an Access Control Entry from wire format to local POSIX xattr format*/
3347 static void cifs_convert_ace(struct posix_acl_xattr_entry *ace,
3348 struct cifs_posix_ace *cifs_ace)
3350 /* u8 cifs fields do not need le conversion */
3351 ace->e_perm = cpu_to_le16(cifs_ace->cifs_e_perm);
3352 ace->e_tag = cpu_to_le16(cifs_ace->cifs_e_tag);
3353 ace->e_id = cpu_to_le32(le64_to_cpu(cifs_ace->cifs_uid));
3355 cifs_dbg(FYI, "perm %d tag %d id %d\n",
3356 ace->e_perm, ace->e_tag, ace->e_id);
3362 /* Convert ACL from CIFS POSIX wire format to local Linux POSIX ACL xattr */
3363 static int cifs_copy_posix_acl(char *trgt, char *src, const int buflen,
3364 const int acl_type, const int size_of_data_area)
3369 struct cifs_posix_ace *pACE;
3370 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)src;
3371 struct posix_acl_xattr_header *local_acl = (void *)trgt;
3373 if (le16_to_cpu(cifs_acl->version) != CIFS_ACL_VERSION)
3376 if (acl_type == ACL_TYPE_ACCESS) {
3377 count = le16_to_cpu(cifs_acl->access_entry_count);
3378 pACE = &cifs_acl->ace_array[0];
3379 size = sizeof(struct cifs_posix_acl);
3380 size += sizeof(struct cifs_posix_ace) * count;
3381 /* check if we would go beyond end of SMB */
3382 if (size_of_data_area < size) {
3383 cifs_dbg(FYI, "bad CIFS POSIX ACL size %d vs. %d\n",
3384 size_of_data_area, size);
3387 } else if (acl_type == ACL_TYPE_DEFAULT) {
3388 count = le16_to_cpu(cifs_acl->access_entry_count);
3389 size = sizeof(struct cifs_posix_acl);
3390 size += sizeof(struct cifs_posix_ace) * count;
3391 /* skip past access ACEs to get to default ACEs */
3392 pACE = &cifs_acl->ace_array[count];
3393 count = le16_to_cpu(cifs_acl->default_entry_count);
3394 size += sizeof(struct cifs_posix_ace) * count;
3395 /* check if we would go beyond end of SMB */
3396 if (size_of_data_area < size)
3403 size = posix_acl_xattr_size(count);
3404 if ((buflen == 0) || (local_acl == NULL)) {
3405 /* used to query ACL EA size */
3406 } else if (size > buflen) {
3408 } else /* buffer big enough */ {
3409 struct posix_acl_xattr_entry *ace = (void *)(local_acl + 1);
3411 local_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
3412 for (i = 0; i < count ; i++) {
3413 cifs_convert_ace(&ace[i], pACE);
3420 static __u16 convert_ace_to_cifs_ace(struct cifs_posix_ace *cifs_ace,
3421 const struct posix_acl_xattr_entry *local_ace)
3423 __u16 rc = 0; /* 0 = ACL converted ok */
3425 cifs_ace->cifs_e_perm = le16_to_cpu(local_ace->e_perm);
3426 cifs_ace->cifs_e_tag = le16_to_cpu(local_ace->e_tag);
3427 /* BB is there a better way to handle the large uid? */
3428 if (local_ace->e_id == cpu_to_le32(-1)) {
3429 /* Probably no need to le convert -1 on any arch but can not hurt */
3430 cifs_ace->cifs_uid = cpu_to_le64(-1);
3432 cifs_ace->cifs_uid = cpu_to_le64(le32_to_cpu(local_ace->e_id));
3434 cifs_dbg(FYI, "perm %d tag %d id %d\n",
3435 ace->e_perm, ace->e_tag, ace->e_id);
3440 /* Convert ACL from local Linux POSIX xattr to CIFS POSIX ACL wire format */
3441 static __u16 ACL_to_cifs_posix(char *parm_data, const char *pACL,
3442 const int buflen, const int acl_type)
3445 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)parm_data;
3446 struct posix_acl_xattr_header *local_acl = (void *)pACL;
3447 struct posix_acl_xattr_entry *ace = (void *)(local_acl + 1);
3451 if ((buflen == 0) || (pACL == NULL) || (cifs_acl == NULL))
3454 count = posix_acl_xattr_count((size_t)buflen);
3455 cifs_dbg(FYI, "setting acl with %d entries from buf of length %d and version of %d\n",
3456 count, buflen, le32_to_cpu(local_acl->a_version));
3457 if (le32_to_cpu(local_acl->a_version) != 2) {
3458 cifs_dbg(FYI, "unknown POSIX ACL version %d\n",
3459 le32_to_cpu(local_acl->a_version));
3462 cifs_acl->version = cpu_to_le16(1);
3463 if (acl_type == ACL_TYPE_ACCESS) {
3464 cifs_acl->access_entry_count = cpu_to_le16(count);
3465 cifs_acl->default_entry_count = cpu_to_le16(0xFFFF);
3466 } else if (acl_type == ACL_TYPE_DEFAULT) {
3467 cifs_acl->default_entry_count = cpu_to_le16(count);
3468 cifs_acl->access_entry_count = cpu_to_le16(0xFFFF);
3470 cifs_dbg(FYI, "unknown ACL type %d\n", acl_type);
3473 for (i = 0; i < count; i++) {
3474 rc = convert_ace_to_cifs_ace(&cifs_acl->ace_array[i], &ace[i]);
3476 /* ACE not converted */
3481 rc = (__u16)(count * sizeof(struct cifs_posix_ace));
3482 rc += sizeof(struct cifs_posix_acl);
3483 /* BB add check to make sure ACL does not overflow SMB */
3489 CIFSSMBGetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
3490 const unsigned char *searchName,
3491 char *acl_inf, const int buflen, const int acl_type,
3492 const struct nls_table *nls_codepage, int remap)
3494 /* SMB_QUERY_POSIX_ACL */
3495 TRANSACTION2_QPI_REQ *pSMB = NULL;
3496 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3500 __u16 params, byte_count;
3502 cifs_dbg(FYI, "In GetPosixACL (Unix) for path %s\n", searchName);
3505 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3510 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3512 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3513 searchName, PATH_MAX, nls_codepage,
3515 name_len++; /* trailing null */
3517 pSMB->FileName[name_len] = 0;
3518 pSMB->FileName[name_len+1] = 0;
3519 } else { /* BB improve the check for buffer overruns BB */
3520 name_len = strnlen(searchName, PATH_MAX);
3521 name_len++; /* trailing null */
3522 strncpy(pSMB->FileName, searchName, name_len);
3525 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3526 pSMB->TotalDataCount = 0;
3527 pSMB->MaxParameterCount = cpu_to_le16(2);
3528 /* BB find exact max data count below from sess structure BB */
3529 pSMB->MaxDataCount = cpu_to_le16(4000);
3530 pSMB->MaxSetupCount = 0;
3534 pSMB->Reserved2 = 0;
3535 pSMB->ParameterOffset = cpu_to_le16(
3536 offsetof(struct smb_com_transaction2_qpi_req,
3537 InformationLevel) - 4);
3538 pSMB->DataCount = 0;
3539 pSMB->DataOffset = 0;
3540 pSMB->SetupCount = 1;
3541 pSMB->Reserved3 = 0;
3542 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3543 byte_count = params + 1 /* pad */ ;
3544 pSMB->TotalParameterCount = cpu_to_le16(params);
3545 pSMB->ParameterCount = pSMB->TotalParameterCount;
3546 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_ACL);
3547 pSMB->Reserved4 = 0;
3548 inc_rfc1001_len(pSMB, byte_count);
3549 pSMB->ByteCount = cpu_to_le16(byte_count);
3551 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3552 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3553 cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
3555 cifs_dbg(FYI, "Send error in Query POSIX ACL = %d\n", rc);
3557 /* decode response */
3559 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
3560 /* BB also check enough total bytes returned */
3561 if (rc || get_bcc(&pSMBr->hdr) < 2)
3562 rc = -EIO; /* bad smb */
3564 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3565 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3566 rc = cifs_copy_posix_acl(acl_inf,
3567 (char *)&pSMBr->hdr.Protocol+data_offset,
3568 buflen, acl_type, count);
3571 cifs_buf_release(pSMB);
3578 CIFSSMBSetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
3579 const unsigned char *fileName,
3580 const char *local_acl, const int buflen,
3582 const struct nls_table *nls_codepage, int remap)
3584 struct smb_com_transaction2_spi_req *pSMB = NULL;
3585 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
3589 int bytes_returned = 0;
3590 __u16 params, byte_count, data_count, param_offset, offset;
3592 cifs_dbg(FYI, "In SetPosixACL (Unix) for path %s\n", fileName);
3594 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3598 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3600 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
3601 PATH_MAX, nls_codepage, remap);
3602 name_len++; /* trailing null */
3604 } else { /* BB improve the check for buffer overruns BB */
3605 name_len = strnlen(fileName, PATH_MAX);
3606 name_len++; /* trailing null */
3607 strncpy(pSMB->FileName, fileName, name_len);
3609 params = 6 + name_len;
3610 pSMB->MaxParameterCount = cpu_to_le16(2);
3611 /* BB find max SMB size from sess */
3612 pSMB->MaxDataCount = cpu_to_le16(1000);
3613 pSMB->MaxSetupCount = 0;
3617 pSMB->Reserved2 = 0;
3618 param_offset = offsetof(struct smb_com_transaction2_spi_req,
3619 InformationLevel) - 4;
3620 offset = param_offset + params;
3621 parm_data = ((char *) &pSMB->hdr.Protocol) + offset;
3622 pSMB->ParameterOffset = cpu_to_le16(param_offset);
3624 /* convert to on the wire format for POSIX ACL */
3625 data_count = ACL_to_cifs_posix(parm_data, local_acl, buflen, acl_type);
3627 if (data_count == 0) {
3629 goto setACLerrorExit;
3631 pSMB->DataOffset = cpu_to_le16(offset);
3632 pSMB->SetupCount = 1;
3633 pSMB->Reserved3 = 0;
3634 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
3635 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_ACL);
3636 byte_count = 3 /* pad */ + params + data_count;
3637 pSMB->DataCount = cpu_to_le16(data_count);
3638 pSMB->TotalDataCount = pSMB->DataCount;
3639 pSMB->ParameterCount = cpu_to_le16(params);
3640 pSMB->TotalParameterCount = pSMB->ParameterCount;
3641 pSMB->Reserved4 = 0;
3642 inc_rfc1001_len(pSMB, byte_count);
3643 pSMB->ByteCount = cpu_to_le16(byte_count);
3644 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3645 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3647 cifs_dbg(FYI, "Set POSIX ACL returned %d\n", rc);
3650 cifs_buf_release(pSMB);
3656 /* BB fix tabs in this function FIXME BB */
3658 CIFSGetExtAttr(const unsigned int xid, struct cifs_tcon *tcon,
3659 const int netfid, __u64 *pExtAttrBits, __u64 *pMask)
3662 struct smb_t2_qfi_req *pSMB = NULL;
3663 struct smb_t2_qfi_rsp *pSMBr = NULL;
3665 __u16 params, byte_count;
3667 cifs_dbg(FYI, "In GetExtAttr\n");
3672 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3677 params = 2 /* level */ + 2 /* fid */;
3678 pSMB->t2.TotalDataCount = 0;
3679 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
3680 /* BB find exact max data count below from sess structure BB */
3681 pSMB->t2.MaxDataCount = cpu_to_le16(4000);
3682 pSMB->t2.MaxSetupCount = 0;
3683 pSMB->t2.Reserved = 0;
3685 pSMB->t2.Timeout = 0;
3686 pSMB->t2.Reserved2 = 0;
3687 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
3689 pSMB->t2.DataCount = 0;
3690 pSMB->t2.DataOffset = 0;
3691 pSMB->t2.SetupCount = 1;
3692 pSMB->t2.Reserved3 = 0;
3693 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
3694 byte_count = params + 1 /* pad */ ;
3695 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
3696 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
3697 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_ATTR_FLAGS);
3700 inc_rfc1001_len(pSMB, byte_count);
3701 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
3703 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3704 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3706 cifs_dbg(FYI, "error %d in GetExtAttr\n", rc);
3708 /* decode response */
3709 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
3710 /* BB also check enough total bytes returned */
3711 if (rc || get_bcc(&pSMBr->hdr) < 2)
3712 /* If rc should we check for EOPNOSUPP and
3713 disable the srvino flag? or in caller? */
3714 rc = -EIO; /* bad smb */
3716 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3717 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3718 struct file_chattr_info *pfinfo;
3719 /* BB Do we need a cast or hash here ? */
3721 cifs_dbg(FYI, "Illegal size ret in GetExtAttr\n");
3725 pfinfo = (struct file_chattr_info *)
3726 (data_offset + (char *) &pSMBr->hdr.Protocol);
3727 *pExtAttrBits = le64_to_cpu(pfinfo->mode);
3728 *pMask = le64_to_cpu(pfinfo->mask);
3732 cifs_buf_release(pSMB);
3734 goto GetExtAttrRetry;
3738 #endif /* CONFIG_POSIX */
3740 #ifdef CONFIG_CIFS_ACL
3742 * Initialize NT TRANSACT SMB into small smb request buffer. This assumes that
3743 * all NT TRANSACTS that we init here have total parm and data under about 400
3744 * bytes (to fit in small cifs buffer size), which is the case so far, it
3745 * easily fits. NB: Setup words themselves and ByteCount MaxSetupCount (size of
3746 * returned setup area) and MaxParameterCount (returned parms size) must be set
3750 smb_init_nttransact(const __u16 sub_command, const int setup_count,
3751 const int parm_len, struct cifs_tcon *tcon,
3756 struct smb_com_ntransact_req *pSMB;
3758 rc = small_smb_init(SMB_COM_NT_TRANSACT, 19 + setup_count, tcon,
3762 *ret_buf = (void *)pSMB;
3764 pSMB->TotalParameterCount = cpu_to_le32(parm_len);
3765 pSMB->TotalDataCount = 0;
3766 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
3767 pSMB->ParameterCount = pSMB->TotalParameterCount;
3768 pSMB->DataCount = pSMB->TotalDataCount;
3769 temp_offset = offsetof(struct smb_com_ntransact_req, Parms) +
3770 (setup_count * 2) - 4 /* for rfc1001 length itself */;
3771 pSMB->ParameterOffset = cpu_to_le32(temp_offset);
3772 pSMB->DataOffset = cpu_to_le32(temp_offset + parm_len);
3773 pSMB->SetupCount = setup_count; /* no need to le convert byte fields */
3774 pSMB->SubCommand = cpu_to_le16(sub_command);
3779 validate_ntransact(char *buf, char **ppparm, char **ppdata,
3780 __u32 *pparmlen, __u32 *pdatalen)
3783 __u32 data_count, data_offset, parm_count, parm_offset;
3784 struct smb_com_ntransact_rsp *pSMBr;
3793 pSMBr = (struct smb_com_ntransact_rsp *)buf;
3795 bcc = get_bcc(&pSMBr->hdr);
3796 end_of_smb = 2 /* sizeof byte count */ + bcc +
3797 (char *)&pSMBr->ByteCount;
3799 data_offset = le32_to_cpu(pSMBr->DataOffset);
3800 data_count = le32_to_cpu(pSMBr->DataCount);
3801 parm_offset = le32_to_cpu(pSMBr->ParameterOffset);
3802 parm_count = le32_to_cpu(pSMBr->ParameterCount);
3804 *ppparm = (char *)&pSMBr->hdr.Protocol + parm_offset;
3805 *ppdata = (char *)&pSMBr->hdr.Protocol + data_offset;
3807 /* should we also check that parm and data areas do not overlap? */
3808 if (*ppparm > end_of_smb) {
3809 cifs_dbg(FYI, "parms start after end of smb\n");
3811 } else if (parm_count + *ppparm > end_of_smb) {
3812 cifs_dbg(FYI, "parm end after end of smb\n");
3814 } else if (*ppdata > end_of_smb) {
3815 cifs_dbg(FYI, "data starts after end of smb\n");
3817 } else if (data_count + *ppdata > end_of_smb) {
3818 cifs_dbg(FYI, "data %p + count %d (%p) past smb end %p start %p\n",
3819 *ppdata, data_count, (data_count + *ppdata),
3822 } else if (parm_count + data_count > bcc) {
3823 cifs_dbg(FYI, "parm count and data count larger than SMB\n");
3826 *pdatalen = data_count;
3827 *pparmlen = parm_count;
3831 /* Get Security Descriptor (by handle) from remote server for a file or dir */
3833 CIFSSMBGetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
3834 struct cifs_ntsd **acl_inf, __u32 *pbuflen)
3838 QUERY_SEC_DESC_REQ *pSMB;
3840 struct kvec rsp_iov;
3842 cifs_dbg(FYI, "GetCifsACL\n");
3847 rc = smb_init_nttransact(NT_TRANSACT_QUERY_SECURITY_DESC, 0,
3848 8 /* parm len */, tcon, (void **) &pSMB);
3852 pSMB->MaxParameterCount = cpu_to_le32(4);
3853 /* BB TEST with big acls that might need to be e.g. larger than 16K */
3854 pSMB->MaxSetupCount = 0;
3855 pSMB->Fid = fid; /* file handle always le */
3856 pSMB->AclFlags = cpu_to_le32(CIFS_ACL_OWNER | CIFS_ACL_GROUP |
3858 pSMB->ByteCount = cpu_to_le16(11); /* 3 bytes pad + 8 bytes parm */
3859 inc_rfc1001_len(pSMB, 11);
3860 iov[0].iov_base = (char *)pSMB;
3861 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
3863 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovec */, &buf_type,
3865 cifs_small_buf_release(pSMB);
3866 cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
3868 cifs_dbg(FYI, "Send error in QuerySecDesc = %d\n", rc);
3869 } else { /* decode response */
3873 struct smb_com_ntransact_rsp *pSMBr;
3876 /* validate_nttransact */
3877 rc = validate_ntransact(rsp_iov.iov_base, (char **)&parm,
3878 &pdata, &parm_len, pbuflen);
3881 pSMBr = (struct smb_com_ntransact_rsp *)rsp_iov.iov_base;
3883 cifs_dbg(FYI, "smb %p parm %p data %p\n",
3884 pSMBr, parm, *acl_inf);
3886 if (le32_to_cpu(pSMBr->ParameterCount) != 4) {
3887 rc = -EIO; /* bad smb */
3892 /* BB check that data area is minimum length and as big as acl_len */
3894 acl_len = le32_to_cpu(*parm);
3895 if (acl_len != *pbuflen) {
3896 cifs_dbg(VFS, "acl length %d does not match %d\n",
3898 if (*pbuflen > acl_len)
3902 /* check if buffer is big enough for the acl
3903 header followed by the smallest SID */
3904 if ((*pbuflen < sizeof(struct cifs_ntsd) + 8) ||
3905 (*pbuflen >= 64 * 1024)) {
3906 cifs_dbg(VFS, "bad acl length %d\n", *pbuflen);
3910 *acl_inf = kmemdup(pdata, *pbuflen, GFP_KERNEL);
3911 if (*acl_inf == NULL) {
3918 free_rsp_buf(buf_type, rsp_iov.iov_base);
3923 CIFSSMBSetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
3924 struct cifs_ntsd *pntsd, __u32 acllen, int aclflag)
3926 __u16 byte_count, param_count, data_count, param_offset, data_offset;
3928 int bytes_returned = 0;
3929 SET_SEC_DESC_REQ *pSMB = NULL;
3933 rc = smb_init(SMB_COM_NT_TRANSACT, 19, tcon, (void **) &pSMB, &pSMBr);
3937 pSMB->MaxSetupCount = 0;
3941 param_offset = offsetof(struct smb_com_transaction_ssec_req, Fid) - 4;
3942 data_count = acllen;
3943 data_offset = param_offset + param_count;
3944 byte_count = 3 /* pad */ + param_count;
3946 pSMB->DataCount = cpu_to_le32(data_count);
3947 pSMB->TotalDataCount = pSMB->DataCount;
3948 pSMB->MaxParameterCount = cpu_to_le32(4);
3949 pSMB->MaxDataCount = cpu_to_le32(16384);
3950 pSMB->ParameterCount = cpu_to_le32(param_count);
3951 pSMB->ParameterOffset = cpu_to_le32(param_offset);
3952 pSMB->TotalParameterCount = pSMB->ParameterCount;
3953 pSMB->DataOffset = cpu_to_le32(data_offset);
3954 pSMB->SetupCount = 0;
3955 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_SET_SECURITY_DESC);
3956 pSMB->ByteCount = cpu_to_le16(byte_count+data_count);
3958 pSMB->Fid = fid; /* file handle always le */
3959 pSMB->Reserved2 = 0;
3960 pSMB->AclFlags = cpu_to_le32(aclflag);
3962 if (pntsd && acllen) {
3963 memcpy((char *)pSMBr + offsetof(struct smb_hdr, Protocol) +
3964 data_offset, pntsd, acllen);
3965 inc_rfc1001_len(pSMB, byte_count + data_count);
3967 inc_rfc1001_len(pSMB, byte_count);
3969 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3970 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3972 cifs_dbg(FYI, "SetCIFSACL bytes_returned: %d, rc: %d\n",
3973 bytes_returned, rc);
3975 cifs_dbg(FYI, "Set CIFS ACL returned %d\n", rc);
3976 cifs_buf_release(pSMB);
3979 goto setCifsAclRetry;
3984 #endif /* CONFIG_CIFS_ACL */
3986 /* Legacy Query Path Information call for lookup to old servers such
3989 SMBQueryInformation(const unsigned int xid, struct cifs_tcon *tcon,
3990 const char *search_name, FILE_ALL_INFO *data,
3991 const struct nls_table *nls_codepage, int remap)
3993 QUERY_INFORMATION_REQ *pSMB;
3994 QUERY_INFORMATION_RSP *pSMBr;
3999 cifs_dbg(FYI, "In SMBQPath path %s\n", search_name);
4001 rc = smb_init(SMB_COM_QUERY_INFORMATION, 0, tcon, (void **) &pSMB,
4006 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4008 cifsConvertToUTF16((__le16 *) pSMB->FileName,
4009 search_name, PATH_MAX, nls_codepage,
4011 name_len++; /* trailing null */
4014 name_len = strnlen(search_name, PATH_MAX);
4015 name_len++; /* trailing null */
4016 strncpy(pSMB->FileName, search_name, name_len);
4018 pSMB->BufferFormat = 0x04;
4019 name_len++; /* account for buffer type byte */
4020 inc_rfc1001_len(pSMB, (__u16)name_len);
4021 pSMB->ByteCount = cpu_to_le16(name_len);
4023 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4024 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4026 cifs_dbg(FYI, "Send error in QueryInfo = %d\n", rc);
4029 __u32 time = le32_to_cpu(pSMBr->last_write_time);
4031 /* decode response */
4032 /* BB FIXME - add time zone adjustment BB */
4033 memset(data, 0, sizeof(FILE_ALL_INFO));
4036 /* decode time fields */
4037 data->ChangeTime = cpu_to_le64(cifs_UnixTimeToNT(ts));
4038 data->LastWriteTime = data->ChangeTime;
4039 data->LastAccessTime = 0;
4040 data->AllocationSize =
4041 cpu_to_le64(le32_to_cpu(pSMBr->size));
4042 data->EndOfFile = data->AllocationSize;
4044 cpu_to_le32(le16_to_cpu(pSMBr->attr));
4046 rc = -EIO; /* bad buffer passed in */
4048 cifs_buf_release(pSMB);
4057 CIFSSMBQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
4058 u16 netfid, FILE_ALL_INFO *pFindData)
4060 struct smb_t2_qfi_req *pSMB = NULL;
4061 struct smb_t2_qfi_rsp *pSMBr = NULL;
4064 __u16 params, byte_count;
4067 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4072 params = 2 /* level */ + 2 /* fid */;
4073 pSMB->t2.TotalDataCount = 0;
4074 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4075 /* BB find exact max data count below from sess structure BB */
4076 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4077 pSMB->t2.MaxSetupCount = 0;
4078 pSMB->t2.Reserved = 0;
4080 pSMB->t2.Timeout = 0;
4081 pSMB->t2.Reserved2 = 0;
4082 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4084 pSMB->t2.DataCount = 0;
4085 pSMB->t2.DataOffset = 0;
4086 pSMB->t2.SetupCount = 1;
4087 pSMB->t2.Reserved3 = 0;
4088 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4089 byte_count = params + 1 /* pad */ ;
4090 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4091 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4092 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
4095 inc_rfc1001_len(pSMB, byte_count);
4096 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
4098 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4099 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4101 cifs_dbg(FYI, "Send error in QFileInfo = %d", rc);
4102 } else { /* decode response */
4103 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4105 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4107 else if (get_bcc(&pSMBr->hdr) < 40)
4108 rc = -EIO; /* bad smb */
4109 else if (pFindData) {
4110 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4111 memcpy((char *) pFindData,
4112 (char *) &pSMBr->hdr.Protocol +
4113 data_offset, sizeof(FILE_ALL_INFO));
4117 cifs_buf_release(pSMB);
4119 goto QFileInfoRetry;
4125 CIFSSMBQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
4126 const char *search_name, FILE_ALL_INFO *data,
4127 int legacy /* old style infolevel */,
4128 const struct nls_table *nls_codepage, int remap)
4130 /* level 263 SMB_QUERY_FILE_ALL_INFO */
4131 TRANSACTION2_QPI_REQ *pSMB = NULL;
4132 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4136 __u16 params, byte_count;
4138 /* cifs_dbg(FYI, "In QPathInfo path %s\n", search_name); */
4140 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4145 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4147 cifsConvertToUTF16((__le16 *) pSMB->FileName, search_name,
4148 PATH_MAX, nls_codepage, remap);
4149 name_len++; /* trailing null */
4151 } else { /* BB improve the check for buffer overruns BB */
4152 name_len = strnlen(search_name, PATH_MAX);
4153 name_len++; /* trailing null */
4154 strncpy(pSMB->FileName, search_name, name_len);
4157 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
4158 pSMB->TotalDataCount = 0;
4159 pSMB->MaxParameterCount = cpu_to_le16(2);
4160 /* BB find exact max SMB PDU from sess structure BB */
4161 pSMB->MaxDataCount = cpu_to_le16(4000);
4162 pSMB->MaxSetupCount = 0;
4166 pSMB->Reserved2 = 0;
4167 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4168 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
4169 pSMB->DataCount = 0;
4170 pSMB->DataOffset = 0;
4171 pSMB->SetupCount = 1;
4172 pSMB->Reserved3 = 0;
4173 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4174 byte_count = params + 1 /* pad */ ;
4175 pSMB->TotalParameterCount = cpu_to_le16(params);
4176 pSMB->ParameterCount = pSMB->TotalParameterCount;
4178 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_STANDARD);
4180 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
4181 pSMB->Reserved4 = 0;
4182 inc_rfc1001_len(pSMB, byte_count);
4183 pSMB->ByteCount = cpu_to_le16(byte_count);
4185 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4186 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4188 cifs_dbg(FYI, "Send error in QPathInfo = %d\n", rc);
4189 } else { /* decode response */
4190 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4192 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4194 else if (!legacy && get_bcc(&pSMBr->hdr) < 40)
4195 rc = -EIO; /* bad smb */
4196 else if (legacy && get_bcc(&pSMBr->hdr) < 24)
4197 rc = -EIO; /* 24 or 26 expected but we do not read
4201 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4204 * On legacy responses we do not read the last field,
4205 * EAsize, fortunately since it varies by subdialect and
4206 * also note it differs on Set vs Get, ie two bytes or 4
4207 * bytes depending but we don't care here.
4210 size = sizeof(FILE_INFO_STANDARD);
4212 size = sizeof(FILE_ALL_INFO);
4213 memcpy((char *) data, (char *) &pSMBr->hdr.Protocol +
4218 cifs_buf_release(pSMB);
4220 goto QPathInfoRetry;
4226 CIFSSMBUnixQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
4227 u16 netfid, FILE_UNIX_BASIC_INFO *pFindData)
4229 struct smb_t2_qfi_req *pSMB = NULL;
4230 struct smb_t2_qfi_rsp *pSMBr = NULL;
4233 __u16 params, byte_count;
4236 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4241 params = 2 /* level */ + 2 /* fid */;
4242 pSMB->t2.TotalDataCount = 0;
4243 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4244 /* BB find exact max data count below from sess structure BB */
4245 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4246 pSMB->t2.MaxSetupCount = 0;
4247 pSMB->t2.Reserved = 0;
4249 pSMB->t2.Timeout = 0;
4250 pSMB->t2.Reserved2 = 0;
4251 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4253 pSMB->t2.DataCount = 0;
4254 pSMB->t2.DataOffset = 0;
4255 pSMB->t2.SetupCount = 1;
4256 pSMB->t2.Reserved3 = 0;
4257 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4258 byte_count = params + 1 /* pad */ ;
4259 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4260 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4261 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4264 inc_rfc1001_len(pSMB, byte_count);
4265 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
4267 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4268 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4270 cifs_dbg(FYI, "Send error in UnixQFileInfo = %d", rc);
4271 } else { /* decode response */
4272 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4274 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
4275 cifs_dbg(VFS, "Malformed FILE_UNIX_BASIC_INFO response. Unix Extensions can be disabled on mount by specifying the nosfu mount option.\n");
4276 rc = -EIO; /* bad smb */
4278 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4279 memcpy((char *) pFindData,
4280 (char *) &pSMBr->hdr.Protocol +
4282 sizeof(FILE_UNIX_BASIC_INFO));
4286 cifs_buf_release(pSMB);
4288 goto UnixQFileInfoRetry;
4294 CIFSSMBUnixQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
4295 const unsigned char *searchName,
4296 FILE_UNIX_BASIC_INFO *pFindData,
4297 const struct nls_table *nls_codepage, int remap)
4299 /* SMB_QUERY_FILE_UNIX_BASIC */
4300 TRANSACTION2_QPI_REQ *pSMB = NULL;
4301 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4303 int bytes_returned = 0;
4305 __u16 params, byte_count;
4307 cifs_dbg(FYI, "In QPathInfo (Unix) the path %s\n", searchName);
4309 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4314 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4316 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4317 PATH_MAX, nls_codepage, remap);
4318 name_len++; /* trailing null */
4320 } else { /* BB improve the check for buffer overruns BB */
4321 name_len = strnlen(searchName, PATH_MAX);
4322 name_len++; /* trailing null */
4323 strncpy(pSMB->FileName, searchName, name_len);
4326 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
4327 pSMB->TotalDataCount = 0;
4328 pSMB->MaxParameterCount = cpu_to_le16(2);
4329 /* BB find exact max SMB PDU from sess structure BB */
4330 pSMB->MaxDataCount = cpu_to_le16(4000);
4331 pSMB->MaxSetupCount = 0;
4335 pSMB->Reserved2 = 0;
4336 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4337 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
4338 pSMB->DataCount = 0;
4339 pSMB->DataOffset = 0;
4340 pSMB->SetupCount = 1;
4341 pSMB->Reserved3 = 0;
4342 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4343 byte_count = params + 1 /* pad */ ;
4344 pSMB->TotalParameterCount = cpu_to_le16(params);
4345 pSMB->ParameterCount = pSMB->TotalParameterCount;
4346 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4347 pSMB->Reserved4 = 0;
4348 inc_rfc1001_len(pSMB, byte_count);
4349 pSMB->ByteCount = cpu_to_le16(byte_count);
4351 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4352 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4354 cifs_dbg(FYI, "Send error in UnixQPathInfo = %d", rc);
4355 } else { /* decode response */
4356 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4358 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
4359 cifs_dbg(VFS, "Malformed FILE_UNIX_BASIC_INFO response. Unix Extensions can be disabled on mount by specifying the nosfu mount option.\n");
4360 rc = -EIO; /* bad smb */
4362 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4363 memcpy((char *) pFindData,
4364 (char *) &pSMBr->hdr.Protocol +
4366 sizeof(FILE_UNIX_BASIC_INFO));
4369 cifs_buf_release(pSMB);
4371 goto UnixQPathInfoRetry;
4376 /* xid, tcon, searchName and codepage are input parms, rest are returned */
4378 CIFSFindFirst(const unsigned int xid, struct cifs_tcon *tcon,
4379 const char *searchName, struct cifs_sb_info *cifs_sb,
4380 __u16 *pnetfid, __u16 search_flags,
4381 struct cifs_search_info *psrch_inf, bool msearch)
4383 /* level 257 SMB_ */
4384 TRANSACTION2_FFIRST_REQ *pSMB = NULL;
4385 TRANSACTION2_FFIRST_RSP *pSMBr = NULL;
4386 T2_FFIRST_RSP_PARMS *parms;
4388 int bytes_returned = 0;
4389 int name_len, remap;
4390 __u16 params, byte_count;
4391 struct nls_table *nls_codepage;
4393 cifs_dbg(FYI, "In FindFirst for %s\n", searchName);
4396 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4401 nls_codepage = cifs_sb->local_nls;
4402 remap = cifs_remap(cifs_sb);
4404 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4406 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4407 PATH_MAX, nls_codepage, remap);
4408 /* We can not add the asterik earlier in case
4409 it got remapped to 0xF03A as if it were part of the
4410 directory name instead of a wildcard */
4413 pSMB->FileName[name_len] = CIFS_DIR_SEP(cifs_sb);
4414 pSMB->FileName[name_len+1] = 0;
4415 pSMB->FileName[name_len+2] = '*';
4416 pSMB->FileName[name_len+3] = 0;
4417 name_len += 4; /* now the trailing null */
4418 /* null terminate just in case */
4419 pSMB->FileName[name_len] = 0;
4420 pSMB->FileName[name_len+1] = 0;
4423 } else { /* BB add check for overrun of SMB buf BB */
4424 name_len = strnlen(searchName, PATH_MAX);
4425 /* BB fix here and in unicode clause above ie
4426 if (name_len > buffersize-header)
4427 free buffer exit; BB */
4428 strncpy(pSMB->FileName, searchName, name_len);
4430 pSMB->FileName[name_len] = CIFS_DIR_SEP(cifs_sb);
4431 pSMB->FileName[name_len+1] = '*';
4432 pSMB->FileName[name_len+2] = 0;
4437 params = 12 + name_len /* includes null */ ;
4438 pSMB->TotalDataCount = 0; /* no EAs */
4439 pSMB->MaxParameterCount = cpu_to_le16(10);
4440 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
4441 pSMB->MaxSetupCount = 0;
4445 pSMB->Reserved2 = 0;
4446 byte_count = params + 1 /* pad */ ;
4447 pSMB->TotalParameterCount = cpu_to_le16(params);
4448 pSMB->ParameterCount = pSMB->TotalParameterCount;
4449 pSMB->ParameterOffset = cpu_to_le16(
4450 offsetof(struct smb_com_transaction2_ffirst_req, SearchAttributes)
4452 pSMB->DataCount = 0;
4453 pSMB->DataOffset = 0;
4454 pSMB->SetupCount = 1; /* one byte, no need to make endian neutral */
4455 pSMB->Reserved3 = 0;
4456 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_FIRST);
4457 pSMB->SearchAttributes =
4458 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
4460 pSMB->SearchCount = cpu_to_le16(CIFSMaxBufSize/sizeof(FILE_UNIX_INFO));
4461 pSMB->SearchFlags = cpu_to_le16(search_flags);
4462 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4464 /* BB what should we set StorageType to? Does it matter? BB */
4465 pSMB->SearchStorageType = 0;
4466 inc_rfc1001_len(pSMB, byte_count);
4467 pSMB->ByteCount = cpu_to_le16(byte_count);
4469 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4470 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4471 cifs_stats_inc(&tcon->stats.cifs_stats.num_ffirst);
4473 if (rc) {/* BB add logic to retry regular search if Unix search
4474 rejected unexpectedly by server */
4475 /* BB Add code to handle unsupported level rc */
4476 cifs_dbg(FYI, "Error in FindFirst = %d\n", rc);
4478 cifs_buf_release(pSMB);
4480 /* BB eventually could optimize out free and realloc of buf */
4483 goto findFirstRetry;
4484 } else { /* decode response */
4485 /* BB remember to free buffer if error BB */
4486 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4490 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
4491 psrch_inf->unicode = true;
4493 psrch_inf->unicode = false;
4495 psrch_inf->ntwrk_buf_start = (char *)pSMBr;
4496 psrch_inf->smallBuf = 0;
4497 psrch_inf->srch_entries_start =
4498 (char *) &pSMBr->hdr.Protocol +
4499 le16_to_cpu(pSMBr->t2.DataOffset);
4500 parms = (T2_FFIRST_RSP_PARMS *)((char *) &pSMBr->hdr.Protocol +
4501 le16_to_cpu(pSMBr->t2.ParameterOffset));
4503 if (parms->EndofSearch)
4504 psrch_inf->endOfSearch = true;
4506 psrch_inf->endOfSearch = false;
4508 psrch_inf->entries_in_buffer =
4509 le16_to_cpu(parms->SearchCount);
4510 psrch_inf->index_of_last_entry = 2 /* skip . and .. */ +
4511 psrch_inf->entries_in_buffer;
4512 lnoff = le16_to_cpu(parms->LastNameOffset);
4513 if (CIFSMaxBufSize < lnoff) {
4514 cifs_dbg(VFS, "ignoring corrupt resume name\n");
4515 psrch_inf->last_entry = NULL;
4519 psrch_inf->last_entry = psrch_inf->srch_entries_start +
4523 *pnetfid = parms->SearchHandle;
4525 cifs_buf_release(pSMB);
4532 int CIFSFindNext(const unsigned int xid, struct cifs_tcon *tcon,
4533 __u16 searchHandle, __u16 search_flags,
4534 struct cifs_search_info *psrch_inf)
4536 TRANSACTION2_FNEXT_REQ *pSMB = NULL;
4537 TRANSACTION2_FNEXT_RSP *pSMBr = NULL;
4538 T2_FNEXT_RSP_PARMS *parms;
4539 char *response_data;
4542 unsigned int name_len;
4543 __u16 params, byte_count;
4545 cifs_dbg(FYI, "In FindNext\n");
4547 if (psrch_inf->endOfSearch)
4550 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4555 params = 14; /* includes 2 bytes of null string, converted to LE below*/
4557 pSMB->TotalDataCount = 0; /* no EAs */
4558 pSMB->MaxParameterCount = cpu_to_le16(8);
4559 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
4560 pSMB->MaxSetupCount = 0;
4564 pSMB->Reserved2 = 0;
4565 pSMB->ParameterOffset = cpu_to_le16(
4566 offsetof(struct smb_com_transaction2_fnext_req,SearchHandle) - 4);
4567 pSMB->DataCount = 0;
4568 pSMB->DataOffset = 0;
4569 pSMB->SetupCount = 1;
4570 pSMB->Reserved3 = 0;
4571 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_NEXT);
4572 pSMB->SearchHandle = searchHandle; /* always kept as le */
4574 cpu_to_le16(CIFSMaxBufSize / sizeof(FILE_UNIX_INFO));
4575 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4576 pSMB->ResumeKey = psrch_inf->resume_key;
4577 pSMB->SearchFlags = cpu_to_le16(search_flags);
4579 name_len = psrch_inf->resume_name_len;
4581 if (name_len < PATH_MAX) {
4582 memcpy(pSMB->ResumeFileName, psrch_inf->presume_name, name_len);
4583 byte_count += name_len;
4584 /* 14 byte parm len above enough for 2 byte null terminator */
4585 pSMB->ResumeFileName[name_len] = 0;
4586 pSMB->ResumeFileName[name_len+1] = 0;
4589 goto FNext2_err_exit;
4591 byte_count = params + 1 /* pad */ ;
4592 pSMB->TotalParameterCount = cpu_to_le16(params);
4593 pSMB->ParameterCount = pSMB->TotalParameterCount;
4594 inc_rfc1001_len(pSMB, byte_count);
4595 pSMB->ByteCount = cpu_to_le16(byte_count);
4597 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4598 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4599 cifs_stats_inc(&tcon->stats.cifs_stats.num_fnext);
4602 psrch_inf->endOfSearch = true;
4603 cifs_buf_release(pSMB);
4604 rc = 0; /* search probably was closed at end of search*/
4606 cifs_dbg(FYI, "FindNext returned = %d\n", rc);
4607 } else { /* decode response */
4608 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4613 /* BB fixme add lock for file (srch_info) struct here */
4614 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
4615 psrch_inf->unicode = true;
4617 psrch_inf->unicode = false;
4618 response_data = (char *) &pSMBr->hdr.Protocol +
4619 le16_to_cpu(pSMBr->t2.ParameterOffset);
4620 parms = (T2_FNEXT_RSP_PARMS *)response_data;
4621 response_data = (char *)&pSMBr->hdr.Protocol +
4622 le16_to_cpu(pSMBr->t2.DataOffset);
4623 if (psrch_inf->smallBuf)
4624 cifs_small_buf_release(
4625 psrch_inf->ntwrk_buf_start);
4627 cifs_buf_release(psrch_inf->ntwrk_buf_start);
4628 psrch_inf->srch_entries_start = response_data;
4629 psrch_inf->ntwrk_buf_start = (char *)pSMB;
4630 psrch_inf->smallBuf = 0;
4631 if (parms->EndofSearch)
4632 psrch_inf->endOfSearch = true;
4634 psrch_inf->endOfSearch = false;
4635 psrch_inf->entries_in_buffer =
4636 le16_to_cpu(parms->SearchCount);
4637 psrch_inf->index_of_last_entry +=
4638 psrch_inf->entries_in_buffer;
4639 lnoff = le16_to_cpu(parms->LastNameOffset);
4640 if (CIFSMaxBufSize < lnoff) {
4641 cifs_dbg(VFS, "ignoring corrupt resume name\n");
4642 psrch_inf->last_entry = NULL;
4645 psrch_inf->last_entry =
4646 psrch_inf->srch_entries_start + lnoff;
4648 /* cifs_dbg(FYI, "fnxt2 entries in buf %d index_of_last %d\n",
4649 psrch_inf->entries_in_buffer, psrch_inf->index_of_last_entry); */
4651 /* BB fixme add unlock here */
4656 /* BB On error, should we leave previous search buf (and count and
4657 last entry fields) intact or free the previous one? */
4659 /* Note: On -EAGAIN error only caller can retry on handle based calls
4660 since file handle passed in no longer valid */
4663 cifs_buf_release(pSMB);
4668 CIFSFindClose(const unsigned int xid, struct cifs_tcon *tcon,
4669 const __u16 searchHandle)
4672 FINDCLOSE_REQ *pSMB = NULL;
4674 cifs_dbg(FYI, "In CIFSSMBFindClose\n");
4675 rc = small_smb_init(SMB_COM_FIND_CLOSE2, 1, tcon, (void **)&pSMB);
4677 /* no sense returning error if session restarted
4678 as file handle has been closed */
4684 pSMB->FileID = searchHandle;
4685 pSMB->ByteCount = 0;
4686 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
4687 cifs_small_buf_release(pSMB);
4689 cifs_dbg(VFS, "Send error in FindClose = %d\n", rc);
4691 cifs_stats_inc(&tcon->stats.cifs_stats.num_fclose);
4693 /* Since session is dead, search handle closed on server already */
4701 CIFSGetSrvInodeNumber(const unsigned int xid, struct cifs_tcon *tcon,
4702 const char *search_name, __u64 *inode_number,
4703 const struct nls_table *nls_codepage, int remap)
4706 TRANSACTION2_QPI_REQ *pSMB = NULL;
4707 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4708 int name_len, bytes_returned;
4709 __u16 params, byte_count;
4711 cifs_dbg(FYI, "In GetSrvInodeNum for %s\n", search_name);
4715 GetInodeNumberRetry:
4716 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4721 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4723 cifsConvertToUTF16((__le16 *) pSMB->FileName,
4724 search_name, PATH_MAX, nls_codepage,
4726 name_len++; /* trailing null */
4728 } else { /* BB improve the check for buffer overruns BB */
4729 name_len = strnlen(search_name, PATH_MAX);
4730 name_len++; /* trailing null */
4731 strncpy(pSMB->FileName, search_name, name_len);
4734 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
4735 pSMB->TotalDataCount = 0;
4736 pSMB->MaxParameterCount = cpu_to_le16(2);
4737 /* BB find exact max data count below from sess structure BB */
4738 pSMB->MaxDataCount = cpu_to_le16(4000);
4739 pSMB->MaxSetupCount = 0;
4743 pSMB->Reserved2 = 0;
4744 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4745 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
4746 pSMB->DataCount = 0;
4747 pSMB->DataOffset = 0;
4748 pSMB->SetupCount = 1;
4749 pSMB->Reserved3 = 0;
4750 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4751 byte_count = params + 1 /* pad */ ;
4752 pSMB->TotalParameterCount = cpu_to_le16(params);
4753 pSMB->ParameterCount = pSMB->TotalParameterCount;
4754 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_INTERNAL_INFO);
4755 pSMB->Reserved4 = 0;
4756 inc_rfc1001_len(pSMB, byte_count);
4757 pSMB->ByteCount = cpu_to_le16(byte_count);
4759 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4760 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4762 cifs_dbg(FYI, "error %d in QueryInternalInfo\n", rc);
4764 /* decode response */
4765 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4766 /* BB also check enough total bytes returned */
4767 if (rc || get_bcc(&pSMBr->hdr) < 2)
4768 /* If rc should we check for EOPNOSUPP and
4769 disable the srvino flag? or in caller? */
4770 rc = -EIO; /* bad smb */
4772 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4773 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
4774 struct file_internal_info *pfinfo;
4775 /* BB Do we need a cast or hash here ? */
4777 cifs_dbg(FYI, "Illegal size ret in QryIntrnlInf\n");
4779 goto GetInodeNumOut;
4781 pfinfo = (struct file_internal_info *)
4782 (data_offset + (char *) &pSMBr->hdr.Protocol);
4783 *inode_number = le64_to_cpu(pfinfo->UniqueId);
4787 cifs_buf_release(pSMB);
4789 goto GetInodeNumberRetry;
4794 CIFSGetDFSRefer(const unsigned int xid, struct cifs_ses *ses,
4795 const char *search_name, struct dfs_info3_param **target_nodes,
4796 unsigned int *num_of_nodes,
4797 const struct nls_table *nls_codepage, int remap)
4799 /* TRANS2_GET_DFS_REFERRAL */
4800 TRANSACTION2_GET_DFS_REFER_REQ *pSMB = NULL;
4801 TRANSACTION2_GET_DFS_REFER_RSP *pSMBr = NULL;
4805 __u16 params, byte_count;
4807 *target_nodes = NULL;
4809 cifs_dbg(FYI, "In GetDFSRefer the path %s\n", search_name);
4813 rc = smb_init(SMB_COM_TRANSACTION2, 15, NULL, (void **) &pSMB,
4818 /* server pointer checked in called function,
4819 but should never be null here anyway */
4820 pSMB->hdr.Mid = get_next_mid(ses->server);
4821 pSMB->hdr.Tid = ses->ipc_tid;
4822 pSMB->hdr.Uid = ses->Suid;
4823 if (ses->capabilities & CAP_STATUS32)
4824 pSMB->hdr.Flags2 |= SMBFLG2_ERR_STATUS;
4825 if (ses->capabilities & CAP_DFS)
4826 pSMB->hdr.Flags2 |= SMBFLG2_DFS;
4828 if (ses->capabilities & CAP_UNICODE) {
4829 pSMB->hdr.Flags2 |= SMBFLG2_UNICODE;
4831 cifsConvertToUTF16((__le16 *) pSMB->RequestFileName,
4832 search_name, PATH_MAX, nls_codepage,
4834 name_len++; /* trailing null */
4836 } else { /* BB improve the check for buffer overruns BB */
4837 name_len = strnlen(search_name, PATH_MAX);
4838 name_len++; /* trailing null */
4839 strncpy(pSMB->RequestFileName, search_name, name_len);
4842 if (ses->server->sign)
4843 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
4845 pSMB->hdr.Uid = ses->Suid;
4847 params = 2 /* level */ + name_len /*includes null */ ;
4848 pSMB->TotalDataCount = 0;
4849 pSMB->DataCount = 0;
4850 pSMB->DataOffset = 0;
4851 pSMB->MaxParameterCount = 0;
4852 /* BB find exact max SMB PDU from sess structure BB */
4853 pSMB->MaxDataCount = cpu_to_le16(4000);
4854 pSMB->MaxSetupCount = 0;
4858 pSMB->Reserved2 = 0;
4859 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4860 struct smb_com_transaction2_get_dfs_refer_req, MaxReferralLevel) - 4);
4861 pSMB->SetupCount = 1;
4862 pSMB->Reserved3 = 0;
4863 pSMB->SubCommand = cpu_to_le16(TRANS2_GET_DFS_REFERRAL);
4864 byte_count = params + 3 /* pad */ ;
4865 pSMB->ParameterCount = cpu_to_le16(params);
4866 pSMB->TotalParameterCount = pSMB->ParameterCount;
4867 pSMB->MaxReferralLevel = cpu_to_le16(3);
4868 inc_rfc1001_len(pSMB, byte_count);
4869 pSMB->ByteCount = cpu_to_le16(byte_count);
4871 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
4872 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4874 cifs_dbg(FYI, "Send error in GetDFSRefer = %d\n", rc);
4877 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4879 /* BB Also check if enough total bytes returned? */
4880 if (rc || get_bcc(&pSMBr->hdr) < 17) {
4881 rc = -EIO; /* bad smb */
4885 cifs_dbg(FYI, "Decoding GetDFSRefer response BCC: %d Offset %d\n",
4886 get_bcc(&pSMBr->hdr), le16_to_cpu(pSMBr->t2.DataOffset));
4888 /* parse returned result into more usable form */
4889 rc = parse_dfs_referrals(&pSMBr->dfs_data,
4890 le16_to_cpu(pSMBr->t2.DataCount),
4891 num_of_nodes, target_nodes, nls_codepage,
4893 (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE) != 0);
4896 cifs_buf_release(pSMB);
4904 /* Query File System Info such as free space to old servers such as Win 9x */
4906 SMBOldQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
4907 struct kstatfs *FSData)
4909 /* level 0x01 SMB_QUERY_FILE_SYSTEM_INFO */
4910 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4911 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4912 FILE_SYSTEM_ALLOC_INFO *response_data;
4914 int bytes_returned = 0;
4915 __u16 params, byte_count;
4917 cifs_dbg(FYI, "OldQFSInfo\n");
4919 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4924 params = 2; /* level */
4925 pSMB->TotalDataCount = 0;
4926 pSMB->MaxParameterCount = cpu_to_le16(2);
4927 pSMB->MaxDataCount = cpu_to_le16(1000);
4928 pSMB->MaxSetupCount = 0;
4932 pSMB->Reserved2 = 0;
4933 byte_count = params + 1 /* pad */ ;
4934 pSMB->TotalParameterCount = cpu_to_le16(params);
4935 pSMB->ParameterCount = pSMB->TotalParameterCount;
4936 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4937 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
4938 pSMB->DataCount = 0;
4939 pSMB->DataOffset = 0;
4940 pSMB->SetupCount = 1;
4941 pSMB->Reserved3 = 0;
4942 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
4943 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_ALLOCATION);
4944 inc_rfc1001_len(pSMB, byte_count);
4945 pSMB->ByteCount = cpu_to_le16(byte_count);
4947 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4948 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4950 cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
4951 } else { /* decode response */
4952 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4954 if (rc || get_bcc(&pSMBr->hdr) < 18)
4955 rc = -EIO; /* bad smb */
4957 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4958 cifs_dbg(FYI, "qfsinf resp BCC: %d Offset %d\n",
4959 get_bcc(&pSMBr->hdr), data_offset);
4961 response_data = (FILE_SYSTEM_ALLOC_INFO *)
4962 (((char *) &pSMBr->hdr.Protocol) + data_offset);
4964 le16_to_cpu(response_data->BytesPerSector) *
4965 le32_to_cpu(response_data->
4966 SectorsPerAllocationUnit);
4968 le32_to_cpu(response_data->TotalAllocationUnits);
4969 FSData->f_bfree = FSData->f_bavail =
4970 le32_to_cpu(response_data->FreeAllocationUnits);
4971 cifs_dbg(FYI, "Blocks: %lld Free: %lld Block size %ld\n",
4972 (unsigned long long)FSData->f_blocks,
4973 (unsigned long long)FSData->f_bfree,
4977 cifs_buf_release(pSMB);
4980 goto oldQFSInfoRetry;
4986 CIFSSMBQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
4987 struct kstatfs *FSData)
4989 /* level 0x103 SMB_QUERY_FILE_SYSTEM_INFO */
4990 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4991 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4992 FILE_SYSTEM_INFO *response_data;
4994 int bytes_returned = 0;
4995 __u16 params, byte_count;
4997 cifs_dbg(FYI, "In QFSInfo\n");
4999 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5004 params = 2; /* level */
5005 pSMB->TotalDataCount = 0;
5006 pSMB->MaxParameterCount = cpu_to_le16(2);
5007 pSMB->MaxDataCount = cpu_to_le16(1000);
5008 pSMB->MaxSetupCount = 0;
5012 pSMB->Reserved2 = 0;
5013 byte_count = params + 1 /* pad */ ;
5014 pSMB->TotalParameterCount = cpu_to_le16(params);
5015 pSMB->ParameterCount = pSMB->TotalParameterCount;
5016 pSMB->ParameterOffset = cpu_to_le16(offsetof(
5017 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
5018 pSMB->DataCount = 0;
5019 pSMB->DataOffset = 0;
5020 pSMB->SetupCount = 1;
5021 pSMB->Reserved3 = 0;
5022 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5023 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_SIZE_INFO);
5024 inc_rfc1001_len(pSMB, byte_count);
5025 pSMB->ByteCount = cpu_to_le16(byte_count);
5027 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5028 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5030 cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
5031 } else { /* decode response */
5032 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5034 if (rc || get_bcc(&pSMBr->hdr) < 24)
5035 rc = -EIO; /* bad smb */
5037 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5041 *) (((char *) &pSMBr->hdr.Protocol) +
5044 le32_to_cpu(response_data->BytesPerSector) *
5045 le32_to_cpu(response_data->
5046 SectorsPerAllocationUnit);
5048 le64_to_cpu(response_data->TotalAllocationUnits);
5049 FSData->f_bfree = FSData->f_bavail =
5050 le64_to_cpu(response_data->FreeAllocationUnits);
5051 cifs_dbg(FYI, "Blocks: %lld Free: %lld Block size %ld\n",
5052 (unsigned long long)FSData->f_blocks,
5053 (unsigned long long)FSData->f_bfree,
5057 cifs_buf_release(pSMB);
5066 CIFSSMBQFSAttributeInfo(const unsigned int xid, struct cifs_tcon *tcon)
5068 /* level 0x105 SMB_QUERY_FILE_SYSTEM_INFO */
5069 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5070 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5071 FILE_SYSTEM_ATTRIBUTE_INFO *response_data;
5073 int bytes_returned = 0;
5074 __u16 params, byte_count;
5076 cifs_dbg(FYI, "In QFSAttributeInfo\n");
5078 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5083 params = 2; /* level */
5084 pSMB->TotalDataCount = 0;
5085 pSMB->MaxParameterCount = cpu_to_le16(2);
5086 /* BB find exact max SMB PDU from sess structure BB */
5087 pSMB->MaxDataCount = cpu_to_le16(1000);
5088 pSMB->MaxSetupCount = 0;
5092 pSMB->Reserved2 = 0;
5093 byte_count = params + 1 /* pad */ ;
5094 pSMB->TotalParameterCount = cpu_to_le16(params);
5095 pSMB->ParameterCount = pSMB->TotalParameterCount;
5096 pSMB->ParameterOffset = cpu_to_le16(offsetof(
5097 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
5098 pSMB->DataCount = 0;
5099 pSMB->DataOffset = 0;
5100 pSMB->SetupCount = 1;
5101 pSMB->Reserved3 = 0;
5102 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5103 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_ATTRIBUTE_INFO);
5104 inc_rfc1001_len(pSMB, byte_count);
5105 pSMB->ByteCount = cpu_to_le16(byte_count);
5107 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5108 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5110 cifs_dbg(VFS, "Send error in QFSAttributeInfo = %d\n", rc);
5111 } else { /* decode response */
5112 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5114 if (rc || get_bcc(&pSMBr->hdr) < 13) {
5115 /* BB also check if enough bytes returned */
5116 rc = -EIO; /* bad smb */
5118 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5120 (FILE_SYSTEM_ATTRIBUTE_INFO
5121 *) (((char *) &pSMBr->hdr.Protocol) +
5123 memcpy(&tcon->fsAttrInfo, response_data,
5124 sizeof(FILE_SYSTEM_ATTRIBUTE_INFO));
5127 cifs_buf_release(pSMB);
5130 goto QFSAttributeRetry;
5136 CIFSSMBQFSDeviceInfo(const unsigned int xid, struct cifs_tcon *tcon)
5138 /* level 0x104 SMB_QUERY_FILE_SYSTEM_INFO */
5139 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5140 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5141 FILE_SYSTEM_DEVICE_INFO *response_data;
5143 int bytes_returned = 0;
5144 __u16 params, byte_count;
5146 cifs_dbg(FYI, "In QFSDeviceInfo\n");
5148 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5153 params = 2; /* level */
5154 pSMB->TotalDataCount = 0;
5155 pSMB->MaxParameterCount = cpu_to_le16(2);
5156 /* BB find exact max SMB PDU from sess structure BB */
5157 pSMB->MaxDataCount = cpu_to_le16(1000);
5158 pSMB->MaxSetupCount = 0;
5162 pSMB->Reserved2 = 0;
5163 byte_count = params + 1 /* pad */ ;
5164 pSMB->TotalParameterCount = cpu_to_le16(params);
5165 pSMB->ParameterCount = pSMB->TotalParameterCount;
5166 pSMB->ParameterOffset = cpu_to_le16(offsetof(
5167 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
5169 pSMB->DataCount = 0;
5170 pSMB->DataOffset = 0;
5171 pSMB->SetupCount = 1;
5172 pSMB->Reserved3 = 0;
5173 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5174 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_DEVICE_INFO);
5175 inc_rfc1001_len(pSMB, byte_count);
5176 pSMB->ByteCount = cpu_to_le16(byte_count);
5178 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5179 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5181 cifs_dbg(FYI, "Send error in QFSDeviceInfo = %d\n", rc);
5182 } else { /* decode response */
5183 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5185 if (rc || get_bcc(&pSMBr->hdr) <
5186 sizeof(FILE_SYSTEM_DEVICE_INFO))
5187 rc = -EIO; /* bad smb */
5189 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5191 (FILE_SYSTEM_DEVICE_INFO *)
5192 (((char *) &pSMBr->hdr.Protocol) +
5194 memcpy(&tcon->fsDevInfo, response_data,
5195 sizeof(FILE_SYSTEM_DEVICE_INFO));
5198 cifs_buf_release(pSMB);
5201 goto QFSDeviceRetry;
5207 CIFSSMBQFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon)
5209 /* level 0x200 SMB_QUERY_CIFS_UNIX_INFO */
5210 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5211 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5212 FILE_SYSTEM_UNIX_INFO *response_data;
5214 int bytes_returned = 0;
5215 __u16 params, byte_count;
5217 cifs_dbg(FYI, "In QFSUnixInfo\n");
5219 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5220 (void **) &pSMB, (void **) &pSMBr);
5224 params = 2; /* level */
5225 pSMB->TotalDataCount = 0;
5226 pSMB->DataCount = 0;
5227 pSMB->DataOffset = 0;
5228 pSMB->MaxParameterCount = cpu_to_le16(2);
5229 /* BB find exact max SMB PDU from sess structure BB */
5230 pSMB->MaxDataCount = cpu_to_le16(100);
5231 pSMB->MaxSetupCount = 0;
5235 pSMB->Reserved2 = 0;
5236 byte_count = params + 1 /* pad */ ;
5237 pSMB->ParameterCount = cpu_to_le16(params);
5238 pSMB->TotalParameterCount = pSMB->ParameterCount;
5239 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5240 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
5241 pSMB->SetupCount = 1;
5242 pSMB->Reserved3 = 0;
5243 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5244 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_CIFS_UNIX_INFO);
5245 inc_rfc1001_len(pSMB, byte_count);
5246 pSMB->ByteCount = cpu_to_le16(byte_count);
5248 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5249 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5251 cifs_dbg(VFS, "Send error in QFSUnixInfo = %d\n", rc);
5252 } else { /* decode response */
5253 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5255 if (rc || get_bcc(&pSMBr->hdr) < 13) {
5256 rc = -EIO; /* bad smb */
5258 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5260 (FILE_SYSTEM_UNIX_INFO
5261 *) (((char *) &pSMBr->hdr.Protocol) +
5263 memcpy(&tcon->fsUnixInfo, response_data,
5264 sizeof(FILE_SYSTEM_UNIX_INFO));
5267 cifs_buf_release(pSMB);
5277 CIFSSMBSetFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon, __u64 cap)
5279 /* level 0x200 SMB_SET_CIFS_UNIX_INFO */
5280 TRANSACTION2_SETFSI_REQ *pSMB = NULL;
5281 TRANSACTION2_SETFSI_RSP *pSMBr = NULL;
5283 int bytes_returned = 0;
5284 __u16 params, param_offset, offset, byte_count;
5286 cifs_dbg(FYI, "In SETFSUnixInfo\n");
5288 /* BB switch to small buf init to save memory */
5289 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5290 (void **) &pSMB, (void **) &pSMBr);
5294 params = 4; /* 2 bytes zero followed by info level. */
5295 pSMB->MaxSetupCount = 0;
5299 pSMB->Reserved2 = 0;
5300 param_offset = offsetof(struct smb_com_transaction2_setfsi_req, FileNum)
5302 offset = param_offset + params;
5304 pSMB->MaxParameterCount = cpu_to_le16(4);
5305 /* BB find exact max SMB PDU from sess structure BB */
5306 pSMB->MaxDataCount = cpu_to_le16(100);
5307 pSMB->SetupCount = 1;
5308 pSMB->Reserved3 = 0;
5309 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FS_INFORMATION);
5310 byte_count = 1 /* pad */ + params + 12;
5312 pSMB->DataCount = cpu_to_le16(12);
5313 pSMB->ParameterCount = cpu_to_le16(params);
5314 pSMB->TotalDataCount = pSMB->DataCount;
5315 pSMB->TotalParameterCount = pSMB->ParameterCount;
5316 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5317 pSMB->DataOffset = cpu_to_le16(offset);
5321 pSMB->InformationLevel = cpu_to_le16(SMB_SET_CIFS_UNIX_INFO);
5324 pSMB->ClientUnixMajor = cpu_to_le16(CIFS_UNIX_MAJOR_VERSION);
5325 pSMB->ClientUnixMinor = cpu_to_le16(CIFS_UNIX_MINOR_VERSION);
5326 pSMB->ClientUnixCap = cpu_to_le64(cap);
5328 inc_rfc1001_len(pSMB, byte_count);
5329 pSMB->ByteCount = cpu_to_le16(byte_count);
5331 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5332 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5334 cifs_dbg(VFS, "Send error in SETFSUnixInfo = %d\n", rc);
5335 } else { /* decode response */
5336 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5338 rc = -EIO; /* bad smb */
5340 cifs_buf_release(pSMB);
5343 goto SETFSUnixRetry;
5351 CIFSSMBQFSPosixInfo(const unsigned int xid, struct cifs_tcon *tcon,
5352 struct kstatfs *FSData)
5354 /* level 0x201 SMB_QUERY_CIFS_POSIX_INFO */
5355 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5356 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5357 FILE_SYSTEM_POSIX_INFO *response_data;
5359 int bytes_returned = 0;
5360 __u16 params, byte_count;
5362 cifs_dbg(FYI, "In QFSPosixInfo\n");
5364 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5369 params = 2; /* level */
5370 pSMB->TotalDataCount = 0;
5371 pSMB->DataCount = 0;
5372 pSMB->DataOffset = 0;
5373 pSMB->MaxParameterCount = cpu_to_le16(2);
5374 /* BB find exact max SMB PDU from sess structure BB */
5375 pSMB->MaxDataCount = cpu_to_le16(100);
5376 pSMB->MaxSetupCount = 0;
5380 pSMB->Reserved2 = 0;
5381 byte_count = params + 1 /* pad */ ;
5382 pSMB->ParameterCount = cpu_to_le16(params);
5383 pSMB->TotalParameterCount = pSMB->ParameterCount;
5384 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5385 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
5386 pSMB->SetupCount = 1;
5387 pSMB->Reserved3 = 0;
5388 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5389 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_FS_INFO);
5390 inc_rfc1001_len(pSMB, byte_count);
5391 pSMB->ByteCount = cpu_to_le16(byte_count);
5393 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5394 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5396 cifs_dbg(FYI, "Send error in QFSUnixInfo = %d\n", rc);
5397 } else { /* decode response */
5398 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5400 if (rc || get_bcc(&pSMBr->hdr) < 13) {
5401 rc = -EIO; /* bad smb */
5403 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5405 (FILE_SYSTEM_POSIX_INFO
5406 *) (((char *) &pSMBr->hdr.Protocol) +
5409 le32_to_cpu(response_data->BlockSize);
5411 le64_to_cpu(response_data->TotalBlocks);
5413 le64_to_cpu(response_data->BlocksAvail);
5414 if (response_data->UserBlocksAvail == cpu_to_le64(-1)) {
5415 FSData->f_bavail = FSData->f_bfree;
5418 le64_to_cpu(response_data->UserBlocksAvail);
5420 if (response_data->TotalFileNodes != cpu_to_le64(-1))
5422 le64_to_cpu(response_data->TotalFileNodes);
5423 if (response_data->FreeFileNodes != cpu_to_le64(-1))
5425 le64_to_cpu(response_data->FreeFileNodes);
5428 cifs_buf_release(pSMB);
5438 * We can not use write of zero bytes trick to set file size due to need for
5439 * large file support. Also note that this SetPathInfo is preferred to
5440 * SetFileInfo based method in next routine which is only needed to work around
5441 * a sharing violation bugin Samba which this routine can run into.
5444 CIFSSMBSetEOF(const unsigned int xid, struct cifs_tcon *tcon,
5445 const char *file_name, __u64 size, struct cifs_sb_info *cifs_sb,
5446 bool set_allocation)
5448 struct smb_com_transaction2_spi_req *pSMB = NULL;
5449 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
5450 struct file_end_of_file_info *parm_data;
5453 int bytes_returned = 0;
5454 int remap = cifs_remap(cifs_sb);
5456 __u16 params, byte_count, data_count, param_offset, offset;
5458 cifs_dbg(FYI, "In SetEOF\n");
5460 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5465 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5467 cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
5468 PATH_MAX, cifs_sb->local_nls, remap);
5469 name_len++; /* trailing null */
5471 } else { /* BB improve the check for buffer overruns BB */
5472 name_len = strnlen(file_name, PATH_MAX);
5473 name_len++; /* trailing null */
5474 strncpy(pSMB->FileName, file_name, name_len);
5476 params = 6 + name_len;
5477 data_count = sizeof(struct file_end_of_file_info);
5478 pSMB->MaxParameterCount = cpu_to_le16(2);
5479 pSMB->MaxDataCount = cpu_to_le16(4100);
5480 pSMB->MaxSetupCount = 0;
5484 pSMB->Reserved2 = 0;
5485 param_offset = offsetof(struct smb_com_transaction2_spi_req,
5486 InformationLevel) - 4;
5487 offset = param_offset + params;
5488 if (set_allocation) {
5489 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5490 pSMB->InformationLevel =
5491 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5493 pSMB->InformationLevel =
5494 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
5495 } else /* Set File Size */ {
5496 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5497 pSMB->InformationLevel =
5498 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
5500 pSMB->InformationLevel =
5501 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
5505 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol) +
5507 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5508 pSMB->DataOffset = cpu_to_le16(offset);
5509 pSMB->SetupCount = 1;
5510 pSMB->Reserved3 = 0;
5511 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5512 byte_count = 3 /* pad */ + params + data_count;
5513 pSMB->DataCount = cpu_to_le16(data_count);
5514 pSMB->TotalDataCount = pSMB->DataCount;
5515 pSMB->ParameterCount = cpu_to_le16(params);
5516 pSMB->TotalParameterCount = pSMB->ParameterCount;
5517 pSMB->Reserved4 = 0;
5518 inc_rfc1001_len(pSMB, byte_count);
5519 parm_data->FileSize = cpu_to_le64(size);
5520 pSMB->ByteCount = cpu_to_le16(byte_count);
5521 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5522 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5524 cifs_dbg(FYI, "SetPathInfo (file size) returned %d\n", rc);
5526 cifs_buf_release(pSMB);
5535 CIFSSMBSetFileSize(const unsigned int xid, struct cifs_tcon *tcon,
5536 struct cifsFileInfo *cfile, __u64 size, bool set_allocation)
5538 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5539 struct file_end_of_file_info *parm_data;
5541 __u16 params, param_offset, offset, byte_count, count;
5543 cifs_dbg(FYI, "SetFileSize (via SetFileInfo) %lld\n",
5545 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5550 pSMB->hdr.Pid = cpu_to_le16((__u16)cfile->pid);
5551 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(cfile->pid >> 16));
5554 pSMB->MaxSetupCount = 0;
5558 pSMB->Reserved2 = 0;
5559 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5560 offset = param_offset + params;
5562 count = sizeof(struct file_end_of_file_info);
5563 pSMB->MaxParameterCount = cpu_to_le16(2);
5564 /* BB find exact max SMB PDU from sess structure BB */
5565 pSMB->MaxDataCount = cpu_to_le16(1000);
5566 pSMB->SetupCount = 1;
5567 pSMB->Reserved3 = 0;
5568 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5569 byte_count = 3 /* pad */ + params + count;
5570 pSMB->DataCount = cpu_to_le16(count);
5571 pSMB->ParameterCount = cpu_to_le16(params);
5572 pSMB->TotalDataCount = pSMB->DataCount;
5573 pSMB->TotalParameterCount = pSMB->ParameterCount;
5574 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5576 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol)
5578 pSMB->DataOffset = cpu_to_le16(offset);
5579 parm_data->FileSize = cpu_to_le64(size);
5580 pSMB->Fid = cfile->fid.netfid;
5581 if (set_allocation) {
5582 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5583 pSMB->InformationLevel =
5584 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5586 pSMB->InformationLevel =
5587 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
5588 } else /* Set File Size */ {
5589 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5590 pSMB->InformationLevel =
5591 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
5593 pSMB->InformationLevel =
5594 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
5596 pSMB->Reserved4 = 0;
5597 inc_rfc1001_len(pSMB, byte_count);
5598 pSMB->ByteCount = cpu_to_le16(byte_count);
5599 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
5600 cifs_small_buf_release(pSMB);
5602 cifs_dbg(FYI, "Send error in SetFileInfo (SetFileSize) = %d\n",
5606 /* Note: On -EAGAIN error only caller can retry on handle based calls
5607 since file handle passed in no longer valid */
5612 /* Some legacy servers such as NT4 require that the file times be set on
5613 an open handle, rather than by pathname - this is awkward due to
5614 potential access conflicts on the open, but it is unavoidable for these
5615 old servers since the only other choice is to go from 100 nanosecond DCE
5616 time and resort to the original setpathinfo level which takes the ancient
5617 DOS time format with 2 second granularity */
5619 CIFSSMBSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
5620 const FILE_BASIC_INFO *data, __u16 fid, __u32 pid_of_opener)
5622 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5625 __u16 params, param_offset, offset, byte_count, count;
5627 cifs_dbg(FYI, "Set Times (via SetFileInfo)\n");
5628 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5633 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5634 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5637 pSMB->MaxSetupCount = 0;
5641 pSMB->Reserved2 = 0;
5642 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5643 offset = param_offset + params;
5645 data_offset = (char *)pSMB +
5646 offsetof(struct smb_hdr, Protocol) + offset;
5648 count = sizeof(FILE_BASIC_INFO);
5649 pSMB->MaxParameterCount = cpu_to_le16(2);
5650 /* BB find max SMB PDU from sess */
5651 pSMB->MaxDataCount = cpu_to_le16(1000);
5652 pSMB->SetupCount = 1;
5653 pSMB->Reserved3 = 0;
5654 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5655 byte_count = 3 /* pad */ + params + count;
5656 pSMB->DataCount = cpu_to_le16(count);
5657 pSMB->ParameterCount = cpu_to_le16(params);
5658 pSMB->TotalDataCount = pSMB->DataCount;
5659 pSMB->TotalParameterCount = pSMB->ParameterCount;
5660 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5661 pSMB->DataOffset = cpu_to_le16(offset);
5663 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5664 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5666 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5667 pSMB->Reserved4 = 0;
5668 inc_rfc1001_len(pSMB, byte_count);
5669 pSMB->ByteCount = cpu_to_le16(byte_count);
5670 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
5671 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
5672 cifs_small_buf_release(pSMB);
5674 cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n",
5677 /* Note: On -EAGAIN error only caller can retry on handle based calls
5678 since file handle passed in no longer valid */
5684 CIFSSMBSetFileDisposition(const unsigned int xid, struct cifs_tcon *tcon,
5685 bool delete_file, __u16 fid, __u32 pid_of_opener)
5687 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5690 __u16 params, param_offset, offset, byte_count, count;
5692 cifs_dbg(FYI, "Set File Disposition (via SetFileInfo)\n");
5693 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5698 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5699 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5702 pSMB->MaxSetupCount = 0;
5706 pSMB->Reserved2 = 0;
5707 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5708 offset = param_offset + params;
5710 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5713 pSMB->MaxParameterCount = cpu_to_le16(2);
5714 /* BB find max SMB PDU from sess */
5715 pSMB->MaxDataCount = cpu_to_le16(1000);
5716 pSMB->SetupCount = 1;
5717 pSMB->Reserved3 = 0;
5718 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5719 byte_count = 3 /* pad */ + params + count;
5720 pSMB->DataCount = cpu_to_le16(count);
5721 pSMB->ParameterCount = cpu_to_le16(params);
5722 pSMB->TotalDataCount = pSMB->DataCount;
5723 pSMB->TotalParameterCount = pSMB->ParameterCount;
5724 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5725 pSMB->DataOffset = cpu_to_le16(offset);
5727 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_DISPOSITION_INFO);
5728 pSMB->Reserved4 = 0;
5729 inc_rfc1001_len(pSMB, byte_count);
5730 pSMB->ByteCount = cpu_to_le16(byte_count);
5731 *data_offset = delete_file ? 1 : 0;
5732 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
5733 cifs_small_buf_release(pSMB);
5735 cifs_dbg(FYI, "Send error in SetFileDisposition = %d\n", rc);
5741 CIFSSMBSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
5742 const char *fileName, const FILE_BASIC_INFO *data,
5743 const struct nls_table *nls_codepage, int remap)
5745 TRANSACTION2_SPI_REQ *pSMB = NULL;
5746 TRANSACTION2_SPI_RSP *pSMBr = NULL;
5749 int bytes_returned = 0;
5751 __u16 params, param_offset, offset, byte_count, count;
5753 cifs_dbg(FYI, "In SetTimes\n");
5756 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5761 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5763 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
5764 PATH_MAX, nls_codepage, remap);
5765 name_len++; /* trailing null */
5767 } else { /* BB improve the check for buffer overruns BB */
5768 name_len = strnlen(fileName, PATH_MAX);
5769 name_len++; /* trailing null */
5770 strncpy(pSMB->FileName, fileName, name_len);
5773 params = 6 + name_len;
5774 count = sizeof(FILE_BASIC_INFO);
5775 pSMB->MaxParameterCount = cpu_to_le16(2);
5776 /* BB find max SMB PDU from sess structure BB */
5777 pSMB->MaxDataCount = cpu_to_le16(1000);
5778 pSMB->MaxSetupCount = 0;
5782 pSMB->Reserved2 = 0;
5783 param_offset = offsetof(struct smb_com_transaction2_spi_req,
5784 InformationLevel) - 4;
5785 offset = param_offset + params;
5786 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5787 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5788 pSMB->DataOffset = cpu_to_le16(offset);
5789 pSMB->SetupCount = 1;
5790 pSMB->Reserved3 = 0;
5791 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5792 byte_count = 3 /* pad */ + params + count;
5794 pSMB->DataCount = cpu_to_le16(count);
5795 pSMB->ParameterCount = cpu_to_le16(params);
5796 pSMB->TotalDataCount = pSMB->DataCount;
5797 pSMB->TotalParameterCount = pSMB->ParameterCount;
5798 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5799 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5801 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5802 pSMB->Reserved4 = 0;
5803 inc_rfc1001_len(pSMB, byte_count);
5804 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
5805 pSMB->ByteCount = cpu_to_le16(byte_count);
5806 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5807 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5809 cifs_dbg(FYI, "SetPathInfo (times) returned %d\n", rc);
5811 cifs_buf_release(pSMB);
5819 /* Can not be used to set time stamps yet (due to old DOS time format) */
5820 /* Can be used to set attributes */
5821 #if 0 /* Possibly not needed - since it turns out that strangely NT4 has a bug
5822 handling it anyway and NT4 was what we thought it would be needed for
5823 Do not delete it until we prove whether needed for Win9x though */
5825 CIFSSMBSetAttrLegacy(unsigned int xid, struct cifs_tcon *tcon, char *fileName,
5826 __u16 dos_attrs, const struct nls_table *nls_codepage)
5828 SETATTR_REQ *pSMB = NULL;
5829 SETATTR_RSP *pSMBr = NULL;
5834 cifs_dbg(FYI, "In SetAttrLegacy\n");
5837 rc = smb_init(SMB_COM_SETATTR, 8, tcon, (void **) &pSMB,
5842 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5844 ConvertToUTF16((__le16 *) pSMB->fileName, fileName,
5845 PATH_MAX, nls_codepage);
5846 name_len++; /* trailing null */
5848 } else { /* BB improve the check for buffer overruns BB */
5849 name_len = strnlen(fileName, PATH_MAX);
5850 name_len++; /* trailing null */
5851 strncpy(pSMB->fileName, fileName, name_len);
5853 pSMB->attr = cpu_to_le16(dos_attrs);
5854 pSMB->BufferFormat = 0x04;
5855 inc_rfc1001_len(pSMB, name_len + 1);
5856 pSMB->ByteCount = cpu_to_le16(name_len + 1);
5857 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5858 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5860 cifs_dbg(FYI, "Error in LegacySetAttr = %d\n", rc);
5862 cifs_buf_release(pSMB);
5865 goto SetAttrLgcyRetry;
5869 #endif /* temporarily unneeded SetAttr legacy function */
5872 cifs_fill_unix_set_info(FILE_UNIX_BASIC_INFO *data_offset,
5873 const struct cifs_unix_set_info_args *args)
5875 u64 uid = NO_CHANGE_64, gid = NO_CHANGE_64;
5876 u64 mode = args->mode;
5878 if (uid_valid(args->uid))
5879 uid = from_kuid(&init_user_ns, args->uid);
5880 if (gid_valid(args->gid))
5881 gid = from_kgid(&init_user_ns, args->gid);
5884 * Samba server ignores set of file size to zero due to bugs in some
5885 * older clients, but we should be precise - we use SetFileSize to
5886 * set file size and do not want to truncate file size to zero
5887 * accidentally as happened on one Samba server beta by putting
5888 * zero instead of -1 here
5890 data_offset->EndOfFile = cpu_to_le64(NO_CHANGE_64);
5891 data_offset->NumOfBytes = cpu_to_le64(NO_CHANGE_64);
5892 data_offset->LastStatusChange = cpu_to_le64(args->ctime);
5893 data_offset->LastAccessTime = cpu_to_le64(args->atime);
5894 data_offset->LastModificationTime = cpu_to_le64(args->mtime);
5895 data_offset->Uid = cpu_to_le64(uid);
5896 data_offset->Gid = cpu_to_le64(gid);
5897 /* better to leave device as zero when it is */
5898 data_offset->DevMajor = cpu_to_le64(MAJOR(args->device));
5899 data_offset->DevMinor = cpu_to_le64(MINOR(args->device));
5900 data_offset->Permissions = cpu_to_le64(mode);
5903 data_offset->Type = cpu_to_le32(UNIX_FILE);
5904 else if (S_ISDIR(mode))
5905 data_offset->Type = cpu_to_le32(UNIX_DIR);
5906 else if (S_ISLNK(mode))
5907 data_offset->Type = cpu_to_le32(UNIX_SYMLINK);
5908 else if (S_ISCHR(mode))
5909 data_offset->Type = cpu_to_le32(UNIX_CHARDEV);
5910 else if (S_ISBLK(mode))
5911 data_offset->Type = cpu_to_le32(UNIX_BLOCKDEV);
5912 else if (S_ISFIFO(mode))
5913 data_offset->Type = cpu_to_le32(UNIX_FIFO);
5914 else if (S_ISSOCK(mode))
5915 data_offset->Type = cpu_to_le32(UNIX_SOCKET);
5919 CIFSSMBUnixSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
5920 const struct cifs_unix_set_info_args *args,
5921 u16 fid, u32 pid_of_opener)
5923 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5926 u16 params, param_offset, offset, byte_count, count;
5928 cifs_dbg(FYI, "Set Unix Info (via SetFileInfo)\n");
5929 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5934 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5935 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5938 pSMB->MaxSetupCount = 0;
5942 pSMB->Reserved2 = 0;
5943 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5944 offset = param_offset + params;
5946 data_offset = (char *)pSMB +
5947 offsetof(struct smb_hdr, Protocol) + offset;
5949 count = sizeof(FILE_UNIX_BASIC_INFO);
5951 pSMB->MaxParameterCount = cpu_to_le16(2);
5952 /* BB find max SMB PDU from sess */
5953 pSMB->MaxDataCount = cpu_to_le16(1000);
5954 pSMB->SetupCount = 1;
5955 pSMB->Reserved3 = 0;
5956 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5957 byte_count = 3 /* pad */ + params + count;
5958 pSMB->DataCount = cpu_to_le16(count);
5959 pSMB->ParameterCount = cpu_to_le16(params);
5960 pSMB->TotalDataCount = pSMB->DataCount;
5961 pSMB->TotalParameterCount = pSMB->ParameterCount;
5962 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5963 pSMB->DataOffset = cpu_to_le16(offset);
5965 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
5966 pSMB->Reserved4 = 0;
5967 inc_rfc1001_len(pSMB, byte_count);
5968 pSMB->ByteCount = cpu_to_le16(byte_count);
5970 cifs_fill_unix_set_info((FILE_UNIX_BASIC_INFO *)data_offset, args);
5972 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
5973 cifs_small_buf_release(pSMB);
5975 cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n",
5978 /* Note: On -EAGAIN error only caller can retry on handle based calls
5979 since file handle passed in no longer valid */
5985 CIFSSMBUnixSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
5986 const char *file_name,
5987 const struct cifs_unix_set_info_args *args,
5988 const struct nls_table *nls_codepage, int remap)
5990 TRANSACTION2_SPI_REQ *pSMB = NULL;
5991 TRANSACTION2_SPI_RSP *pSMBr = NULL;
5994 int bytes_returned = 0;
5995 FILE_UNIX_BASIC_INFO *data_offset;
5996 __u16 params, param_offset, offset, count, byte_count;
5998 cifs_dbg(FYI, "In SetUID/GID/Mode\n");
6000 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6005 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6007 cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
6008 PATH_MAX, nls_codepage, remap);
6009 name_len++; /* trailing null */
6011 } else { /* BB improve the check for buffer overruns BB */
6012 name_len = strnlen(file_name, PATH_MAX);
6013 name_len++; /* trailing null */
6014 strncpy(pSMB->FileName, file_name, name_len);
6017 params = 6 + name_len;
6018 count = sizeof(FILE_UNIX_BASIC_INFO);
6019 pSMB->MaxParameterCount = cpu_to_le16(2);
6020 /* BB find max SMB PDU from sess structure BB */
6021 pSMB->MaxDataCount = cpu_to_le16(1000);
6022 pSMB->MaxSetupCount = 0;
6026 pSMB->Reserved2 = 0;
6027 param_offset = offsetof(struct smb_com_transaction2_spi_req,
6028 InformationLevel) - 4;
6029 offset = param_offset + params;
6031 (FILE_UNIX_BASIC_INFO *) ((char *) &pSMB->hdr.Protocol +
6033 memset(data_offset, 0, count);
6034 pSMB->DataOffset = cpu_to_le16(offset);
6035 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6036 pSMB->SetupCount = 1;
6037 pSMB->Reserved3 = 0;
6038 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6039 byte_count = 3 /* pad */ + params + count;
6040 pSMB->ParameterCount = cpu_to_le16(params);
6041 pSMB->DataCount = cpu_to_le16(count);
6042 pSMB->TotalParameterCount = pSMB->ParameterCount;
6043 pSMB->TotalDataCount = pSMB->DataCount;
6044 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
6045 pSMB->Reserved4 = 0;
6046 inc_rfc1001_len(pSMB, byte_count);
6048 cifs_fill_unix_set_info(data_offset, args);
6050 pSMB->ByteCount = cpu_to_le16(byte_count);
6051 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6052 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
6054 cifs_dbg(FYI, "SetPathInfo (perms) returned %d\n", rc);
6056 cifs_buf_release(pSMB);
6062 #ifdef CONFIG_CIFS_XATTR
6064 * Do a path-based QUERY_ALL_EAS call and parse the result. This is a common
6065 * function used by listxattr and getxattr type calls. When ea_name is set,
6066 * it looks for that attribute name and stuffs that value into the EAData
6067 * buffer. When ea_name is NULL, it stuffs a list of attribute names into the
6068 * buffer. In both cases, the return value is either the length of the
6069 * resulting data or a negative error code. If EAData is a NULL pointer then
6070 * the data isn't copied to it, but the length is returned.
6073 CIFSSMBQAllEAs(const unsigned int xid, struct cifs_tcon *tcon,
6074 const unsigned char *searchName, const unsigned char *ea_name,
6075 char *EAData, size_t buf_size,
6076 const struct nls_table *nls_codepage, int remap)
6078 /* BB assumes one setup word */
6079 TRANSACTION2_QPI_REQ *pSMB = NULL;
6080 TRANSACTION2_QPI_RSP *pSMBr = NULL;
6084 struct fealist *ea_response_data;
6085 struct fea *temp_fea;
6088 __u16 params, byte_count, data_offset;
6089 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
6091 cifs_dbg(FYI, "In Query All EAs path %s\n", searchName);
6093 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6098 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6100 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
6101 PATH_MAX, nls_codepage, remap);
6102 list_len++; /* trailing null */
6104 } else { /* BB improve the check for buffer overruns BB */
6105 list_len = strnlen(searchName, PATH_MAX);
6106 list_len++; /* trailing null */
6107 strncpy(pSMB->FileName, searchName, list_len);
6110 params = 2 /* level */ + 4 /* reserved */ + list_len /* includes NUL */;
6111 pSMB->TotalDataCount = 0;
6112 pSMB->MaxParameterCount = cpu_to_le16(2);
6113 /* BB find exact max SMB PDU from sess structure BB */
6114 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
6115 pSMB->MaxSetupCount = 0;
6119 pSMB->Reserved2 = 0;
6120 pSMB->ParameterOffset = cpu_to_le16(offsetof(
6121 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
6122 pSMB->DataCount = 0;
6123 pSMB->DataOffset = 0;
6124 pSMB->SetupCount = 1;
6125 pSMB->Reserved3 = 0;
6126 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
6127 byte_count = params + 1 /* pad */ ;
6128 pSMB->TotalParameterCount = cpu_to_le16(params);
6129 pSMB->ParameterCount = pSMB->TotalParameterCount;
6130 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_QUERY_ALL_EAS);
6131 pSMB->Reserved4 = 0;
6132 inc_rfc1001_len(pSMB, byte_count);
6133 pSMB->ByteCount = cpu_to_le16(byte_count);
6135 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6136 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
6138 cifs_dbg(FYI, "Send error in QueryAllEAs = %d\n", rc);
6143 /* BB also check enough total bytes returned */
6144 /* BB we need to improve the validity checking
6145 of these trans2 responses */
6147 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
6148 if (rc || get_bcc(&pSMBr->hdr) < 4) {
6149 rc = -EIO; /* bad smb */
6153 /* check that length of list is not more than bcc */
6154 /* check that each entry does not go beyond length
6156 /* check that each element of each entry does not
6157 go beyond end of list */
6158 /* validate_trans2_offsets() */
6159 /* BB check if start of smb + data_offset > &bcc+ bcc */
6161 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
6162 ea_response_data = (struct fealist *)
6163 (((char *) &pSMBr->hdr.Protocol) + data_offset);
6165 list_len = le32_to_cpu(ea_response_data->list_len);
6166 cifs_dbg(FYI, "ea length %d\n", list_len);
6167 if (list_len <= 8) {
6168 cifs_dbg(FYI, "empty EA list returned from server\n");
6169 /* didn't find the named attribute */
6175 /* make sure list_len doesn't go past end of SMB */
6176 end_of_smb = (char *)pByteArea(&pSMBr->hdr) + get_bcc(&pSMBr->hdr);
6177 if ((char *)ea_response_data + list_len > end_of_smb) {
6178 cifs_dbg(FYI, "EA list appears to go beyond SMB\n");
6183 /* account for ea list len */
6185 temp_fea = ea_response_data->list;
6186 temp_ptr = (char *)temp_fea;
6187 while (list_len > 0) {
6188 unsigned int name_len;
6193 /* make sure we can read name_len and value_len */
6195 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
6200 name_len = temp_fea->name_len;
6201 value_len = le16_to_cpu(temp_fea->value_len);
6202 list_len -= name_len + 1 + value_len;
6204 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
6210 if (ea_name_len == name_len &&
6211 memcmp(ea_name, temp_ptr, name_len) == 0) {
6212 temp_ptr += name_len + 1;
6216 if ((size_t)value_len > buf_size) {
6220 memcpy(EAData, temp_ptr, value_len);
6224 /* account for prefix user. and trailing null */
6225 rc += (5 + 1 + name_len);
6226 if (rc < (int) buf_size) {
6227 memcpy(EAData, "user.", 5);
6229 memcpy(EAData, temp_ptr, name_len);
6231 /* null terminate name */
6234 } else if (buf_size == 0) {
6235 /* skip copy - calc size only */
6237 /* stop before overrun buffer */
6242 temp_ptr += name_len + 1 + value_len;
6243 temp_fea = (struct fea *)temp_ptr;
6246 /* didn't find the named attribute */
6251 cifs_buf_release(pSMB);
6259 CIFSSMBSetEA(const unsigned int xid, struct cifs_tcon *tcon,
6260 const char *fileName, const char *ea_name, const void *ea_value,
6261 const __u16 ea_value_len, const struct nls_table *nls_codepage,
6264 struct smb_com_transaction2_spi_req *pSMB = NULL;
6265 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
6266 struct fealist *parm_data;
6269 int bytes_returned = 0;
6270 __u16 params, param_offset, byte_count, offset, count;
6272 cifs_dbg(FYI, "In SetEA\n");
6274 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6279 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6281 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
6282 PATH_MAX, nls_codepage, remap);
6283 name_len++; /* trailing null */
6285 } else { /* BB improve the check for buffer overruns BB */
6286 name_len = strnlen(fileName, PATH_MAX);
6287 name_len++; /* trailing null */
6288 strncpy(pSMB->FileName, fileName, name_len);
6291 params = 6 + name_len;
6293 /* done calculating parms using name_len of file name,
6294 now use name_len to calculate length of ea name
6295 we are going to create in the inode xattrs */
6296 if (ea_name == NULL)
6299 name_len = strnlen(ea_name, 255);
6301 count = sizeof(*parm_data) + ea_value_len + name_len;
6302 pSMB->MaxParameterCount = cpu_to_le16(2);
6303 /* BB find max SMB PDU from sess */
6304 pSMB->MaxDataCount = cpu_to_le16(1000);
6305 pSMB->MaxSetupCount = 0;
6309 pSMB->Reserved2 = 0;
6310 param_offset = offsetof(struct smb_com_transaction2_spi_req,
6311 InformationLevel) - 4;
6312 offset = param_offset + params;
6313 pSMB->InformationLevel =
6314 cpu_to_le16(SMB_SET_FILE_EA);
6317 (struct fealist *) (((char *) &pSMB->hdr.Protocol) +
6319 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6320 pSMB->DataOffset = cpu_to_le16(offset);
6321 pSMB->SetupCount = 1;
6322 pSMB->Reserved3 = 0;
6323 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6324 byte_count = 3 /* pad */ + params + count;
6325 pSMB->DataCount = cpu_to_le16(count);
6326 parm_data->list_len = cpu_to_le32(count);
6327 parm_data->list[0].EA_flags = 0;
6328 /* we checked above that name len is less than 255 */
6329 parm_data->list[0].name_len = (__u8)name_len;
6330 /* EA names are always ASCII */
6332 strncpy(parm_data->list[0].name, ea_name, name_len);
6333 parm_data->list[0].name[name_len] = 0;
6334 parm_data->list[0].value_len = cpu_to_le16(ea_value_len);
6335 /* caller ensures that ea_value_len is less than 64K but
6336 we need to ensure that it fits within the smb */
6338 /*BB add length check to see if it would fit in
6339 negotiated SMB buffer size BB */
6340 /* if (ea_value_len > buffer_size - 512 (enough for header)) */
6342 memcpy(parm_data->list[0].name+name_len+1,
6343 ea_value, ea_value_len);
6345 pSMB->TotalDataCount = pSMB->DataCount;
6346 pSMB->ParameterCount = cpu_to_le16(params);
6347 pSMB->TotalParameterCount = pSMB->ParameterCount;
6348 pSMB->Reserved4 = 0;
6349 inc_rfc1001_len(pSMB, byte_count);
6350 pSMB->ByteCount = cpu_to_le16(byte_count);
6351 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6352 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
6354 cifs_dbg(FYI, "SetPathInfo (EA) returned %d\n", rc);
6356 cifs_buf_release(pSMB);
6365 #ifdef CONFIG_CIFS_DNOTIFY_EXPERIMENTAL /* BB unused temporarily */
6367 * Years ago the kernel added a "dnotify" function for Samba server,
6368 * to allow network clients (such as Windows) to display updated
6369 * lists of files in directory listings automatically when
6370 * files are added by one user when another user has the
6371 * same directory open on their desktop. The Linux cifs kernel
6372 * client hooked into the kernel side of this interface for
6373 * the same reason, but ironically when the VFS moved from
6374 * "dnotify" to "inotify" it became harder to plug in Linux
6375 * network file system clients (the most obvious use case
6376 * for notify interfaces is when multiple users can update
6377 * the contents of the same directory - exactly what network
6378 * file systems can do) although the server (Samba) could
6379 * still use it. For the short term we leave the worker
6380 * function ifdeffed out (below) until inotify is fixed
6381 * in the VFS to make it easier to plug in network file
6382 * system clients. If inotify turns out to be permanently
6383 * incompatible for network fs clients, we could instead simply
6384 * expose this config flag by adding a future cifs (and smb2) notify ioctl.
6386 int CIFSSMBNotify(const unsigned int xid, struct cifs_tcon *tcon,
6387 const int notify_subdirs, const __u16 netfid,
6388 __u32 filter, struct file *pfile, int multishot,
6389 const struct nls_table *nls_codepage)
6392 struct smb_com_transaction_change_notify_req *pSMB = NULL;
6393 struct smb_com_ntransaction_change_notify_rsp *pSMBr = NULL;
6394 struct dir_notify_req *dnotify_req;
6397 cifs_dbg(FYI, "In CIFSSMBNotify for file handle %d\n", (int)netfid);
6398 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
6403 pSMB->TotalParameterCount = 0 ;
6404 pSMB->TotalDataCount = 0;
6405 pSMB->MaxParameterCount = cpu_to_le32(2);
6406 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
6407 pSMB->MaxSetupCount = 4;
6409 pSMB->ParameterOffset = 0;
6410 pSMB->DataCount = 0;
6411 pSMB->DataOffset = 0;
6412 pSMB->SetupCount = 4; /* single byte does not need le conversion */
6413 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_NOTIFY_CHANGE);
6414 pSMB->ParameterCount = pSMB->TotalParameterCount;
6416 pSMB->WatchTree = 1; /* one byte - no le conversion needed */
6417 pSMB->Reserved2 = 0;
6418 pSMB->CompletionFilter = cpu_to_le32(filter);
6419 pSMB->Fid = netfid; /* file handle always le */
6420 pSMB->ByteCount = 0;
6422 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6423 (struct smb_hdr *)pSMBr, &bytes_returned,
6426 cifs_dbg(FYI, "Error in Notify = %d\n", rc);
6428 /* Add file to outstanding requests */
6429 /* BB change to kmem cache alloc */
6430 dnotify_req = kmalloc(
6431 sizeof(struct dir_notify_req),
6434 dnotify_req->Pid = pSMB->hdr.Pid;
6435 dnotify_req->PidHigh = pSMB->hdr.PidHigh;
6436 dnotify_req->Mid = pSMB->hdr.Mid;
6437 dnotify_req->Tid = pSMB->hdr.Tid;
6438 dnotify_req->Uid = pSMB->hdr.Uid;
6439 dnotify_req->netfid = netfid;
6440 dnotify_req->pfile = pfile;
6441 dnotify_req->filter = filter;
6442 dnotify_req->multishot = multishot;
6443 spin_lock(&GlobalMid_Lock);
6444 list_add_tail(&dnotify_req->lhead,
6445 &GlobalDnotifyReqList);
6446 spin_unlock(&GlobalMid_Lock);
6450 cifs_buf_release(pSMB);
6453 #endif /* was needed for dnotify, and will be needed for inotify when VFS fix */