4 * Copyright (C) International Business Machines Corp., 2002,2006
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 different for reconnection purposes since we never want */
28 /* to reuse a stale file handle and the caller knows the file handle */
31 #include <linux/kernel.h>
32 #include <linux/vfs.h>
33 #include <linux/posix_acl_xattr.h>
34 #include <asm/uaccess.h>
37 #include "cifsproto.h"
38 #include "cifs_unicode.h"
39 #include "cifs_debug.h"
42 #ifdef CONFIG_CIFS_POSIX
47 #ifdef CONFIG_CIFS_WEAK_PW_HASH
48 {LANMAN_PROT, "\2LM1.2X002"},
49 {LANMAN2_PROT, "\2LANMAN2.1"},
50 #endif /* weak password hashing for legacy clients */
51 {CIFS_PROT, "\2NT LM 0.12"},
52 {POSIX_PROT, "\2POSIX 2"},
60 #ifdef CONFIG_CIFS_WEAK_PW_HASH
61 {LANMAN_PROT, "\2LM1.2X002"},
62 {LANMAN2_PROT, "\2LANMAN2.1"},
63 #endif /* weak password hashing for legacy clients */
64 {CIFS_PROT, "\2NT LM 0.12"},
69 /* define the number of elements in the cifs dialect array */
70 #ifdef CONFIG_CIFS_POSIX
71 #ifdef CONFIG_CIFS_WEAK_PW_HASH
72 #define CIFS_NUM_PROT 4
74 #define CIFS_NUM_PROT 2
75 #endif /* CIFS_WEAK_PW_HASH */
77 #ifdef CONFIG_CIFS_WEAK_PW_HASH
78 #define CIFS_NUM_PROT 3
80 #define CIFS_NUM_PROT 1
81 #endif /* CONFIG_CIFS_WEAK_PW_HASH */
82 #endif /* CIFS_POSIX */
85 /* Mark as invalid, all open files on tree connections since they
86 were closed when session to server was lost */
87 static void mark_open_files_invalid(struct cifsTconInfo * pTcon)
89 struct cifsFileInfo *open_file = NULL;
90 struct list_head * tmp;
91 struct list_head * tmp1;
93 /* list all files open on tree connection and mark them invalid */
94 write_lock(&GlobalSMBSeslock);
95 list_for_each_safe(tmp, tmp1, &pTcon->openFileList) {
96 open_file = list_entry(tmp,struct cifsFileInfo, tlist);
98 open_file->invalidHandle = TRUE;
101 write_unlock(&GlobalSMBSeslock);
102 /* BB Add call to invalidate_inodes(sb) for all superblocks mounted
106 /* If the return code is zero, this function must fill in request_buf pointer */
108 small_smb_init(int smb_command, int wct, struct cifsTconInfo *tcon,
109 void **request_buf /* returned */)
113 /* SMBs NegProt, SessSetup, uLogoff do not have tcon yet so
114 check for tcp and smb session status done differently
115 for those three - in the calling routine */
117 if(tcon->tidStatus == CifsExiting) {
118 /* only tree disconnect, open, and write,
119 (and ulogoff which does not have tcon)
120 are allowed as we start force umount */
121 if((smb_command != SMB_COM_WRITE_ANDX) &&
122 (smb_command != SMB_COM_OPEN_ANDX) &&
123 (smb_command != SMB_COM_TREE_DISCONNECT)) {
124 cFYI(1,("can not send cmd %d while umounting",
129 if((tcon->ses) && (tcon->ses->status != CifsExiting) &&
130 (tcon->ses->server)){
131 struct nls_table *nls_codepage;
132 /* Give Demultiplex thread up to 10 seconds to
133 reconnect, should be greater than cifs socket
134 timeout which is 7 seconds */
135 while(tcon->ses->server->tcpStatus == CifsNeedReconnect) {
136 wait_event_interruptible_timeout(tcon->ses->server->response_q,
137 (tcon->ses->server->tcpStatus == CifsGood), 10 * HZ);
138 if(tcon->ses->server->tcpStatus == CifsNeedReconnect) {
139 /* on "soft" mounts we wait once */
140 if((tcon->retry == FALSE) ||
141 (tcon->ses->status == CifsExiting)) {
142 cFYI(1,("gave up waiting on reconnect in smb_init"));
144 } /* else "hard" mount - keep retrying
145 until process is killed or server
146 comes back on-line */
147 } else /* TCP session is reestablished now */
152 nls_codepage = load_nls_default();
153 /* need to prevent multiple threads trying to
154 simultaneously reconnect the same SMB session */
155 down(&tcon->ses->sesSem);
156 if(tcon->ses->status == CifsNeedReconnect)
157 rc = cifs_setup_session(0, tcon->ses,
159 if(!rc && (tcon->tidStatus == CifsNeedReconnect)) {
160 mark_open_files_invalid(tcon);
161 rc = CIFSTCon(0, tcon->ses, tcon->treeName, tcon
163 up(&tcon->ses->sesSem);
164 /* BB FIXME add code to check if wsize needs
165 update due to negotiated smb buffer size
168 atomic_inc(&tconInfoReconnectCount);
170 cFYI(1, ("reconnect tcon rc = %d", rc));
171 /* Removed call to reopen open files here -
172 it is safer (and faster) to reopen files
173 one at a time as needed in read and write */
175 /* Check if handle based operation so we
176 know whether we can continue or not without
177 returning to caller to reset file handle */
178 switch(smb_command) {
179 case SMB_COM_READ_ANDX:
180 case SMB_COM_WRITE_ANDX:
182 case SMB_COM_FIND_CLOSE2:
183 case SMB_COM_LOCKING_ANDX: {
184 unload_nls(nls_codepage);
189 up(&tcon->ses->sesSem);
191 unload_nls(nls_codepage);
200 *request_buf = cifs_small_buf_get();
201 if (*request_buf == NULL) {
202 /* BB should we add a retry in here if not a writepage? */
206 header_assemble((struct smb_hdr *) *request_buf, smb_command, tcon,wct);
209 cifs_stats_inc(&tcon->num_smbs_sent);
215 small_smb_init_no_tc(const int smb_command, const int wct,
216 struct cifsSesInfo *ses, void **request_buf)
219 struct smb_hdr * buffer;
221 rc = small_smb_init(smb_command, wct, NULL, request_buf);
225 buffer = (struct smb_hdr *)*request_buf;
226 buffer->Mid = GetNextMid(ses->server);
227 if (ses->capabilities & CAP_UNICODE)
228 buffer->Flags2 |= SMBFLG2_UNICODE;
229 if (ses->capabilities & CAP_STATUS32)
230 buffer->Flags2 |= SMBFLG2_ERR_STATUS;
232 /* uid, tid can stay at zero as set in header assemble */
234 /* BB add support for turning on the signing when
235 this function is used after 1st of session setup requests */
240 /* If the return code is zero, this function must fill in request_buf pointer */
242 smb_init(int smb_command, int wct, struct cifsTconInfo *tcon,
243 void **request_buf /* returned */ ,
244 void **response_buf /* returned */ )
248 /* SMBs NegProt, SessSetup, uLogoff do not have tcon yet so
249 check for tcp and smb session status done differently
250 for those three - in the calling routine */
252 if(tcon->tidStatus == CifsExiting) {
253 /* only tree disconnect, open, and write,
254 (and ulogoff which does not have tcon)
255 are allowed as we start force umount */
256 if((smb_command != SMB_COM_WRITE_ANDX) &&
257 (smb_command != SMB_COM_OPEN_ANDX) &&
258 (smb_command != SMB_COM_TREE_DISCONNECT)) {
259 cFYI(1,("can not send cmd %d while umounting",
265 if((tcon->ses) && (tcon->ses->status != CifsExiting) &&
266 (tcon->ses->server)){
267 struct nls_table *nls_codepage;
268 /* Give Demultiplex thread up to 10 seconds to
269 reconnect, should be greater than cifs socket
270 timeout which is 7 seconds */
271 while(tcon->ses->server->tcpStatus == CifsNeedReconnect) {
272 wait_event_interruptible_timeout(tcon->ses->server->response_q,
273 (tcon->ses->server->tcpStatus == CifsGood), 10 * HZ);
274 if(tcon->ses->server->tcpStatus ==
276 /* on "soft" mounts we wait once */
277 if((tcon->retry == FALSE) ||
278 (tcon->ses->status == CifsExiting)) {
279 cFYI(1,("gave up waiting on reconnect in smb_init"));
281 } /* else "hard" mount - keep retrying
282 until process is killed or server
284 } else /* TCP session is reestablished now */
289 nls_codepage = load_nls_default();
290 /* need to prevent multiple threads trying to
291 simultaneously reconnect the same SMB session */
292 down(&tcon->ses->sesSem);
293 if(tcon->ses->status == CifsNeedReconnect)
294 rc = cifs_setup_session(0, tcon->ses,
296 if(!rc && (tcon->tidStatus == CifsNeedReconnect)) {
297 mark_open_files_invalid(tcon);
298 rc = CIFSTCon(0, tcon->ses, tcon->treeName,
300 up(&tcon->ses->sesSem);
301 /* BB FIXME add code to check if wsize needs
302 update due to negotiated smb buffer size
305 atomic_inc(&tconInfoReconnectCount);
307 cFYI(1, ("reconnect tcon rc = %d", rc));
308 /* Removed call to reopen open files here -
309 it is safer (and faster) to reopen files
310 one at a time as needed in read and write */
312 /* Check if handle based operation so we
313 know whether we can continue or not without
314 returning to caller to reset file handle */
315 switch(smb_command) {
316 case SMB_COM_READ_ANDX:
317 case SMB_COM_WRITE_ANDX:
319 case SMB_COM_FIND_CLOSE2:
320 case SMB_COM_LOCKING_ANDX: {
321 unload_nls(nls_codepage);
326 up(&tcon->ses->sesSem);
328 unload_nls(nls_codepage);
337 *request_buf = cifs_buf_get();
338 if (*request_buf == NULL) {
339 /* BB should we add a retry in here if not a writepage? */
342 /* Although the original thought was we needed the response buf for */
343 /* potential retries of smb operations it turns out we can determine */
344 /* from the mid flags when the request buffer can be resent without */
345 /* having to use a second distinct buffer for the response */
347 *response_buf = *request_buf;
349 header_assemble((struct smb_hdr *) *request_buf, smb_command, tcon,
353 cifs_stats_inc(&tcon->num_smbs_sent);
358 static int validate_t2(struct smb_t2_rsp * pSMB)
364 /* check for plausible wct, bcc and t2 data and parm sizes */
365 /* check for parm and data offset going beyond end of smb */
366 if(pSMB->hdr.WordCount >= 10) {
367 if((le16_to_cpu(pSMB->t2_rsp.ParameterOffset) <= 1024) &&
368 (le16_to_cpu(pSMB->t2_rsp.DataOffset) <= 1024)) {
369 /* check that bcc is at least as big as parms + data */
370 /* check that bcc is less than negotiated smb buffer */
371 total_size = le16_to_cpu(pSMB->t2_rsp.ParameterCount);
372 if(total_size < 512) {
373 total_size+=le16_to_cpu(pSMB->t2_rsp.DataCount);
374 /* BCC le converted in SendReceive */
375 pBCC = (pSMB->hdr.WordCount * 2) +
376 sizeof(struct smb_hdr) +
378 if((total_size <= (*(u16 *)pBCC)) &&
380 CIFSMaxBufSize+MAX_CIFS_HDR_SIZE)) {
387 cifs_dump_mem("Invalid transact2 SMB: ",(char *)pSMB,
388 sizeof(struct smb_t2_rsp) + 16);
392 CIFSSMBNegotiate(unsigned int xid, struct cifsSesInfo *ses)
395 NEGOTIATE_RSP *pSMBr;
399 struct TCP_Server_Info * server;
401 unsigned int secFlags;
405 server = ses->server;
410 rc = smb_init(SMB_COM_NEGOTIATE, 0, NULL /* no tcon yet */ ,
411 (void **) &pSMB, (void **) &pSMBr);
415 /* if any of auth flags (ie not sign or seal) are overriden use them */
416 if(ses->overrideSecFlg & (~(CIFSSEC_MUST_SIGN | CIFSSEC_MUST_SEAL)))
417 secFlags = ses->overrideSecFlg;
418 else /* if override flags set only sign/seal OR them with global auth */
419 secFlags = extended_security | ses->overrideSecFlg;
421 cFYI(1,("secFlags 0x%x",secFlags));
423 pSMB->hdr.Mid = GetNextMid(server);
424 pSMB->hdr.Flags2 |= SMBFLG2_UNICODE;
425 if((secFlags & CIFSSEC_MUST_KRB5) == CIFSSEC_MUST_KRB5)
426 pSMB->hdr.Flags2 |= SMBFLG2_EXT_SEC;
429 for(i=0;i<CIFS_NUM_PROT;i++) {
430 strncpy(pSMB->DialectsArray+count, protocols[i].name, 16);
431 count += strlen(protocols[i].name) + 1;
432 /* null at end of source and target buffers anyway */
434 pSMB->hdr.smb_buf_length += count;
435 pSMB->ByteCount = cpu_to_le16(count);
437 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
438 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
442 dialect = le16_to_cpu(pSMBr->DialectIndex);
443 cFYI(1,("Dialect: %d", dialect));
444 /* Check wct = 1 error case */
445 if((pSMBr->hdr.WordCount < 13) || (dialect == BAD_PROT)) {
446 /* core returns wct = 1, but we do not ask for core - otherwise
447 small wct just comes when dialect index is -1 indicating we
448 could not negotiate a common dialect */
451 #ifdef CONFIG_CIFS_WEAK_PW_HASH
452 } else if((pSMBr->hdr.WordCount == 13)
453 && ((dialect == LANMAN_PROT)
454 || (dialect == LANMAN2_PROT))) {
456 struct lanman_neg_rsp * rsp = (struct lanman_neg_rsp *)pSMBr;
458 if((secFlags & CIFSSEC_MAY_LANMAN) ||
459 (secFlags & CIFSSEC_MAY_PLNTXT))
460 server->secType = LANMAN;
462 cERROR(1, ("mount failed weak security disabled"
463 " in /proc/fs/cifs/SecurityFlags"));
467 server->secMode = (__u8)le16_to_cpu(rsp->SecurityMode);
468 server->maxReq = le16_to_cpu(rsp->MaxMpxCount);
469 server->maxBuf = min((__u32)le16_to_cpu(rsp->MaxBufSize),
470 (__u32)CIFSMaxBufSize + MAX_CIFS_HDR_SIZE);
471 GETU32(server->sessid) = le32_to_cpu(rsp->SessionKey);
472 /* even though we do not use raw we might as well set this
473 accurately, in case we ever find a need for it */
474 if((le16_to_cpu(rsp->RawMode) & RAW_ENABLE) == RAW_ENABLE) {
475 server->maxRw = 0xFF00;
476 server->capabilities = CAP_MPX_MODE | CAP_RAW_MODE;
478 server->maxRw = 0;/* we do not need to use raw anyway */
479 server->capabilities = CAP_MPX_MODE;
481 tmp = (__s16)le16_to_cpu(rsp->ServerTimeZone);
483 /* OS/2 often does not set timezone therefore
484 * we must use server time to calc time zone.
485 * Could deviate slightly from the right zone.
486 * Smallest defined timezone difference is 15 minutes
487 * (i.e. Nepal). Rounding up/down is done to match
490 int val, seconds, remain, result;
491 struct timespec ts, utc;
493 ts = cnvrtDosUnixTm(le16_to_cpu(rsp->SrvTime.Date),
494 le16_to_cpu(rsp->SrvTime.Time));
495 cFYI(1,("SrvTime: %d sec since 1970 (utc: %d) diff: %d",
496 (int)ts.tv_sec, (int)utc.tv_sec,
497 (int)(utc.tv_sec - ts.tv_sec)));
498 val = (int)(utc.tv_sec - ts.tv_sec);
499 seconds = val < 0 ? -val : val;
500 result = (seconds / MIN_TZ_ADJ) * MIN_TZ_ADJ;
501 remain = seconds % MIN_TZ_ADJ;
502 if(remain >= (MIN_TZ_ADJ / 2))
503 result += MIN_TZ_ADJ;
506 server->timeAdj = result;
508 server->timeAdj = (int)tmp;
509 server->timeAdj *= 60; /* also in seconds */
511 cFYI(1,("server->timeAdj: %d seconds", server->timeAdj));
514 /* BB get server time for time conversions and add
515 code to use it and timezone since this is not UTC */
517 if (rsp->EncryptionKeyLength ==
518 cpu_to_le16(CIFS_CRYPTO_KEY_SIZE)) {
519 memcpy(server->cryptKey, rsp->EncryptionKey,
520 CIFS_CRYPTO_KEY_SIZE);
521 } else if (server->secMode & SECMODE_PW_ENCRYPT) {
522 rc = -EIO; /* need cryptkey unless plain text */
526 cFYI(1,("LANMAN negotiated"));
527 /* we will not end up setting signing flags - as no signing
528 was in LANMAN and server did not return the flags on */
530 #else /* weak security disabled */
531 } else if(pSMBr->hdr.WordCount == 13) {
532 cERROR(1,("mount failed, cifs module not built "
533 "with CIFS_WEAK_PW_HASH support"));
535 #endif /* WEAK_PW_HASH */
537 } else if(pSMBr->hdr.WordCount != 17) {
542 /* else wct == 17 NTLM */
543 server->secMode = pSMBr->SecurityMode;
544 if((server->secMode & SECMODE_USER) == 0)
545 cFYI(1,("share mode security"));
547 if((server->secMode & SECMODE_PW_ENCRYPT) == 0)
548 #ifdef CONFIG_CIFS_WEAK_PW_HASH
549 if ((secFlags & CIFSSEC_MAY_PLNTXT) == 0)
550 #endif /* CIFS_WEAK_PW_HASH */
551 cERROR(1,("Server requests plain text password"
552 " but client support disabled"));
554 if((secFlags & CIFSSEC_MUST_NTLMV2) == CIFSSEC_MUST_NTLMV2)
555 server->secType = NTLMv2;
556 else if(secFlags & CIFSSEC_MAY_NTLM)
557 server->secType = NTLM;
558 else if(secFlags & CIFSSEC_MAY_NTLMV2)
559 server->secType = NTLMv2;
560 /* else krb5 ... any others ... */
562 /* one byte, so no need to convert this or EncryptionKeyLen from
564 server->maxReq = le16_to_cpu(pSMBr->MaxMpxCount);
565 /* probably no need to store and check maxvcs */
566 server->maxBuf = min(le32_to_cpu(pSMBr->MaxBufferSize),
567 (__u32) CIFSMaxBufSize + MAX_CIFS_HDR_SIZE);
568 server->maxRw = le32_to_cpu(pSMBr->MaxRawSize);
569 cFYI(0, ("Max buf = %d", ses->server->maxBuf));
570 GETU32(ses->server->sessid) = le32_to_cpu(pSMBr->SessionKey);
571 server->capabilities = le32_to_cpu(pSMBr->Capabilities);
572 server->timeAdj = (int)(__s16)le16_to_cpu(pSMBr->ServerTimeZone);
573 server->timeAdj *= 60;
574 if (pSMBr->EncryptionKeyLength == CIFS_CRYPTO_KEY_SIZE) {
575 memcpy(server->cryptKey, pSMBr->u.EncryptionKey,
576 CIFS_CRYPTO_KEY_SIZE);
577 } else if ((pSMBr->hdr.Flags2 & SMBFLG2_EXT_SEC)
578 && (pSMBr->EncryptionKeyLength == 0)) {
579 /* decode security blob */
580 } else if (server->secMode & SECMODE_PW_ENCRYPT) {
581 rc = -EIO; /* no crypt key only if plain text pwd */
585 /* BB might be helpful to save off the domain of server here */
587 if ((pSMBr->hdr.Flags2 & SMBFLG2_EXT_SEC) &&
588 (server->capabilities & CAP_EXTENDED_SECURITY)) {
589 count = pSMBr->ByteCount;
592 else if (count == 16) {
593 server->secType = RawNTLMSSP;
594 if (server->socketUseCount.counter > 1) {
595 if (memcmp(server->server_GUID,
596 pSMBr->u.extended_response.
598 cFYI(1, ("server UID changed"));
599 memcpy(server->server_GUID,
600 pSMBr->u.extended_response.GUID,
604 memcpy(server->server_GUID,
605 pSMBr->u.extended_response.GUID, 16);
607 rc = decode_negTokenInit(pSMBr->u.extended_response.
612 /* BB Need to fill struct for sessetup here */
619 server->capabilities &= ~CAP_EXTENDED_SECURITY;
621 #ifdef CONFIG_CIFS_WEAK_PW_HASH
624 if(sign_CIFS_PDUs == FALSE) {
625 if(server->secMode & SECMODE_SIGN_REQUIRED)
626 cERROR(1,("Server requires "
627 "/proc/fs/cifs/PacketSigningEnabled to be on"));
629 ~(SECMODE_SIGN_ENABLED | SECMODE_SIGN_REQUIRED);
630 } else if(sign_CIFS_PDUs == 1) {
631 if((server->secMode & SECMODE_SIGN_REQUIRED) == 0)
633 ~(SECMODE_SIGN_ENABLED | SECMODE_SIGN_REQUIRED);
634 } else if(sign_CIFS_PDUs == 2) {
635 if((server->secMode &
636 (SECMODE_SIGN_ENABLED | SECMODE_SIGN_REQUIRED)) == 0) {
637 cERROR(1,("signing required but server lacks support"));
641 cifs_buf_release(pSMB);
643 cFYI(1,("negprot rc %d",rc));
648 CIFSSMBTDis(const int xid, struct cifsTconInfo *tcon)
650 struct smb_hdr *smb_buffer;
651 struct smb_hdr *smb_buffer_response; /* BB removeme BB */
655 cFYI(1, ("In tree disconnect"));
657 * If last user of the connection and
658 * connection alive - disconnect it
659 * If this is the last connection on the server session disconnect it
660 * (and inside session disconnect we should check if tcp socket needs
661 * to be freed and kernel thread woken up).
664 down(&tcon->tconSem);
668 atomic_dec(&tcon->useCount);
669 if (atomic_read(&tcon->useCount) > 0) {
674 /* No need to return error on this operation if tid invalidated and
675 closed on server already e.g. due to tcp session crashing */
676 if(tcon->tidStatus == CifsNeedReconnect) {
681 if((tcon->ses == NULL) || (tcon->ses->server == NULL)) {
685 rc = small_smb_init(SMB_COM_TREE_DISCONNECT, 0, tcon,
686 (void **)&smb_buffer);
691 smb_buffer_response = smb_buffer; /* BB removeme BB */
693 rc = SendReceive(xid, tcon->ses, smb_buffer, smb_buffer_response,
696 cFYI(1, ("Tree disconnect failed %d", rc));
699 cifs_small_buf_release(smb_buffer);
702 /* No need to return error on this operation if tid invalidated and
703 closed on server already e.g. due to tcp session crashing */
711 CIFSSMBLogoff(const int xid, struct cifsSesInfo *ses)
713 struct smb_hdr *smb_buffer_response;
714 LOGOFF_ANDX_REQ *pSMB;
718 cFYI(1, ("In SMBLogoff for session disconnect"));
724 atomic_dec(&ses->inUse);
725 if (atomic_read(&ses->inUse) > 0) {
729 rc = small_smb_init(SMB_COM_LOGOFF_ANDX, 2, NULL, (void **)&pSMB);
735 smb_buffer_response = (struct smb_hdr *)pSMB; /* BB removeme BB */
738 pSMB->hdr.Mid = GetNextMid(ses->server);
740 if(ses->server->secMode &
741 (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
742 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
745 pSMB->hdr.Uid = ses->Suid;
747 pSMB->AndXCommand = 0xFF;
748 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
749 smb_buffer_response, &length, 0);
751 atomic_dec(&ses->server->socketUseCount);
752 if (atomic_read(&ses->server->socketUseCount) == 0) {
753 spin_lock(&GlobalMid_Lock);
754 ses->server->tcpStatus = CifsExiting;
755 spin_unlock(&GlobalMid_Lock);
760 cifs_small_buf_release(pSMB);
762 /* if session dead then we do not need to do ulogoff,
763 since server closed smb session, no sense reporting
771 CIFSSMBDelFile(const int xid, struct cifsTconInfo *tcon, const char *fileName,
772 const struct nls_table *nls_codepage, int remap)
774 DELETE_FILE_REQ *pSMB = NULL;
775 DELETE_FILE_RSP *pSMBr = NULL;
781 rc = smb_init(SMB_COM_DELETE, 1, tcon, (void **) &pSMB,
786 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
788 cifsConvertToUCS((__le16 *) pSMB->fileName, fileName,
789 PATH_MAX, nls_codepage, remap);
790 name_len++; /* trailing null */
792 } else { /* BB improve check for buffer overruns BB */
793 name_len = strnlen(fileName, PATH_MAX);
794 name_len++; /* trailing null */
795 strncpy(pSMB->fileName, fileName, name_len);
797 pSMB->SearchAttributes =
798 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM);
799 pSMB->BufferFormat = 0x04;
800 pSMB->hdr.smb_buf_length += name_len + 1;
801 pSMB->ByteCount = cpu_to_le16(name_len + 1);
802 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
803 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
804 cifs_stats_inc(&tcon->num_deletes);
806 cFYI(1, ("Error in RMFile = %d", rc));
809 cifs_buf_release(pSMB);
817 CIFSSMBRmDir(const int xid, struct cifsTconInfo *tcon, const char *dirName,
818 const struct nls_table *nls_codepage, int remap)
820 DELETE_DIRECTORY_REQ *pSMB = NULL;
821 DELETE_DIRECTORY_RSP *pSMBr = NULL;
826 cFYI(1, ("In CIFSSMBRmDir"));
828 rc = smb_init(SMB_COM_DELETE_DIRECTORY, 0, tcon, (void **) &pSMB,
833 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
834 name_len = cifsConvertToUCS((__le16 *) pSMB->DirName, dirName,
835 PATH_MAX, nls_codepage, remap);
836 name_len++; /* trailing null */
838 } else { /* BB improve check for buffer overruns BB */
839 name_len = strnlen(dirName, PATH_MAX);
840 name_len++; /* trailing null */
841 strncpy(pSMB->DirName, dirName, name_len);
844 pSMB->BufferFormat = 0x04;
845 pSMB->hdr.smb_buf_length += name_len + 1;
846 pSMB->ByteCount = cpu_to_le16(name_len + 1);
847 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
848 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
849 cifs_stats_inc(&tcon->num_rmdirs);
851 cFYI(1, ("Error in RMDir = %d", rc));
854 cifs_buf_release(pSMB);
861 CIFSSMBMkDir(const int xid, struct cifsTconInfo *tcon,
862 const char *name, const struct nls_table *nls_codepage, int remap)
865 CREATE_DIRECTORY_REQ *pSMB = NULL;
866 CREATE_DIRECTORY_RSP *pSMBr = NULL;
870 cFYI(1, ("In CIFSSMBMkDir"));
872 rc = smb_init(SMB_COM_CREATE_DIRECTORY, 0, tcon, (void **) &pSMB,
877 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
878 name_len = cifsConvertToUCS((__le16 *) pSMB->DirName, name,
879 PATH_MAX, nls_codepage, remap);
880 name_len++; /* trailing null */
882 } else { /* BB improve check for buffer overruns BB */
883 name_len = strnlen(name, PATH_MAX);
884 name_len++; /* trailing null */
885 strncpy(pSMB->DirName, name, name_len);
888 pSMB->BufferFormat = 0x04;
889 pSMB->hdr.smb_buf_length += name_len + 1;
890 pSMB->ByteCount = cpu_to_le16(name_len + 1);
891 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
892 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
893 cifs_stats_inc(&tcon->num_mkdirs);
895 cFYI(1, ("Error in Mkdir = %d", rc));
898 cifs_buf_release(pSMB);
904 static __u16 convert_disposition(int disposition)
908 switch (disposition) {
910 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
913 ofun = SMBOPEN_OAPPEND;
916 ofun = SMBOPEN_OCREATE;
919 ofun = SMBOPEN_OCREATE | SMBOPEN_OAPPEND;
922 ofun = SMBOPEN_OTRUNC;
924 case FILE_OVERWRITE_IF:
925 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
928 cFYI(1,("unknown disposition %d",disposition));
929 ofun = SMBOPEN_OAPPEND; /* regular open */
935 SMBLegacyOpen(const int xid, struct cifsTconInfo *tcon,
936 const char *fileName, const int openDisposition,
937 const int access_flags, const int create_options, __u16 * netfid,
938 int *pOplock, FILE_ALL_INFO * pfile_info,
939 const struct nls_table *nls_codepage, int remap)
942 OPENX_REQ *pSMB = NULL;
943 OPENX_RSP *pSMBr = NULL;
949 rc = smb_init(SMB_COM_OPEN_ANDX, 15, tcon, (void **) &pSMB,
954 pSMB->AndXCommand = 0xFF; /* none */
956 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
957 count = 1; /* account for one byte pad to word boundary */
959 cifsConvertToUCS((__le16 *) (pSMB->fileName + 1),
960 fileName, PATH_MAX, nls_codepage, remap);
961 name_len++; /* trailing null */
963 } else { /* BB improve check for buffer overruns BB */
964 count = 0; /* no pad */
965 name_len = strnlen(fileName, PATH_MAX);
966 name_len++; /* trailing null */
967 strncpy(pSMB->fileName, fileName, name_len);
969 if (*pOplock & REQ_OPLOCK)
970 pSMB->OpenFlags = cpu_to_le16(REQ_OPLOCK);
971 else if (*pOplock & REQ_BATCHOPLOCK) {
972 pSMB->OpenFlags = cpu_to_le16(REQ_BATCHOPLOCK);
974 pSMB->OpenFlags |= cpu_to_le16(REQ_MORE_INFO);
975 /* BB fixme add conversion for access_flags to bits 0 - 2 of mode */
981 pSMB->Mode = cpu_to_le16(2);
982 pSMB->Mode |= cpu_to_le16(0x40); /* deny none */
983 /* set file as system file if special file such
984 as fifo and server expecting SFU style and
985 no Unix extensions */
987 if(create_options & CREATE_OPTION_SPECIAL)
988 pSMB->FileAttributes = cpu_to_le16(ATTR_SYSTEM);
990 pSMB->FileAttributes = cpu_to_le16(0/*ATTR_NORMAL*/); /* BB FIXME */
992 /* if ((omode & S_IWUGO) == 0)
993 pSMB->FileAttributes |= cpu_to_le32(ATTR_READONLY);*/
994 /* Above line causes problems due to vfs splitting create into two
995 pieces - need to set mode after file created not while it is
999 /* pSMB->CreateOptions = cpu_to_le32(create_options & CREATE_OPTIONS_MASK); */
1000 /* BB FIXME END BB */
1002 pSMB->Sattr = cpu_to_le16(ATTR_HIDDEN | ATTR_SYSTEM | ATTR_DIRECTORY);
1003 pSMB->OpenFunction = cpu_to_le16(convert_disposition(openDisposition));
1005 pSMB->hdr.smb_buf_length += count;
1007 pSMB->ByteCount = cpu_to_le16(count);
1008 /* long_op set to 1 to allow for oplock break timeouts */
1009 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1010 (struct smb_hdr *) pSMBr, &bytes_returned, 1);
1011 cifs_stats_inc(&tcon->num_opens);
1013 cFYI(1, ("Error in Open = %d", rc));
1015 /* BB verify if wct == 15 */
1017 /* *pOplock = pSMBr->OplockLevel; */ /* BB take from action field BB */
1019 *netfid = pSMBr->Fid; /* cifs fid stays in le */
1020 /* Let caller know file was created so we can set the mode. */
1021 /* Do we care about the CreateAction in any other cases? */
1023 /* if(cpu_to_le32(FILE_CREATE) == pSMBr->CreateAction)
1024 *pOplock |= CIFS_CREATE_ACTION; */
1028 pfile_info->CreationTime = 0; /* BB convert CreateTime*/
1029 pfile_info->LastAccessTime = 0; /* BB fixme */
1030 pfile_info->LastWriteTime = 0; /* BB fixme */
1031 pfile_info->ChangeTime = 0; /* BB fixme */
1032 pfile_info->Attributes =
1033 cpu_to_le32(le16_to_cpu(pSMBr->FileAttributes));
1034 /* the file_info buf is endian converted by caller */
1035 pfile_info->AllocationSize =
1036 cpu_to_le64(le32_to_cpu(pSMBr->EndOfFile));
1037 pfile_info->EndOfFile = pfile_info->AllocationSize;
1038 pfile_info->NumberOfLinks = cpu_to_le32(1);
1042 cifs_buf_release(pSMB);
1049 CIFSSMBOpen(const int xid, struct cifsTconInfo *tcon,
1050 const char *fileName, const int openDisposition,
1051 const int access_flags, const int create_options, __u16 * netfid,
1052 int *pOplock, FILE_ALL_INFO * pfile_info,
1053 const struct nls_table *nls_codepage, int remap)
1056 OPEN_REQ *pSMB = NULL;
1057 OPEN_RSP *pSMBr = NULL;
1063 rc = smb_init(SMB_COM_NT_CREATE_ANDX, 24, tcon, (void **) &pSMB,
1068 pSMB->AndXCommand = 0xFF; /* none */
1070 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1071 count = 1; /* account for one byte pad to word boundary */
1073 cifsConvertToUCS((__le16 *) (pSMB->fileName + 1),
1074 fileName, PATH_MAX, nls_codepage, remap);
1075 name_len++; /* trailing null */
1077 pSMB->NameLength = cpu_to_le16(name_len);
1078 } else { /* BB improve check for buffer overruns BB */
1079 count = 0; /* no pad */
1080 name_len = strnlen(fileName, PATH_MAX);
1081 name_len++; /* trailing null */
1082 pSMB->NameLength = cpu_to_le16(name_len);
1083 strncpy(pSMB->fileName, fileName, name_len);
1085 if (*pOplock & REQ_OPLOCK)
1086 pSMB->OpenFlags = cpu_to_le32(REQ_OPLOCK);
1087 else if (*pOplock & REQ_BATCHOPLOCK) {
1088 pSMB->OpenFlags = cpu_to_le32(REQ_BATCHOPLOCK);
1090 pSMB->DesiredAccess = cpu_to_le32(access_flags);
1091 pSMB->AllocationSize = 0;
1092 /* set file as system file if special file such
1093 as fifo and server expecting SFU style and
1094 no Unix extensions */
1095 if(create_options & CREATE_OPTION_SPECIAL)
1096 pSMB->FileAttributes = cpu_to_le32(ATTR_SYSTEM);
1098 pSMB->FileAttributes = cpu_to_le32(ATTR_NORMAL);
1099 /* XP does not handle ATTR_POSIX_SEMANTICS */
1100 /* but it helps speed up case sensitive checks for other
1101 servers such as Samba */
1102 if (tcon->ses->capabilities & CAP_UNIX)
1103 pSMB->FileAttributes |= cpu_to_le32(ATTR_POSIX_SEMANTICS);
1105 /* if ((omode & S_IWUGO) == 0)
1106 pSMB->FileAttributes |= cpu_to_le32(ATTR_READONLY);*/
1107 /* Above line causes problems due to vfs splitting create into two
1108 pieces - need to set mode after file created not while it is
1110 pSMB->ShareAccess = cpu_to_le32(FILE_SHARE_ALL);
1111 pSMB->CreateDisposition = cpu_to_le32(openDisposition);
1112 pSMB->CreateOptions = cpu_to_le32(create_options & CREATE_OPTIONS_MASK);
1113 /* BB Expirement with various impersonation levels and verify */
1114 pSMB->ImpersonationLevel = cpu_to_le32(SECURITY_IMPERSONATION);
1115 pSMB->SecurityFlags =
1116 SECURITY_CONTEXT_TRACKING | SECURITY_EFFECTIVE_ONLY;
1119 pSMB->hdr.smb_buf_length += count;
1121 pSMB->ByteCount = cpu_to_le16(count);
1122 /* long_op set to 1 to allow for oplock break timeouts */
1123 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1124 (struct smb_hdr *) pSMBr, &bytes_returned, 1);
1125 cifs_stats_inc(&tcon->num_opens);
1127 cFYI(1, ("Error in Open = %d", rc));
1129 *pOplock = pSMBr->OplockLevel; /* 1 byte no need to le_to_cpu */
1130 *netfid = pSMBr->Fid; /* cifs fid stays in le */
1131 /* Let caller know file was created so we can set the mode. */
1132 /* Do we care about the CreateAction in any other cases? */
1133 if(cpu_to_le32(FILE_CREATE) == pSMBr->CreateAction)
1134 *pOplock |= CIFS_CREATE_ACTION;
1136 memcpy((char *)pfile_info,(char *)&pSMBr->CreationTime,
1137 36 /* CreationTime to Attributes */);
1138 /* the file_info buf is endian converted by caller */
1139 pfile_info->AllocationSize = pSMBr->AllocationSize;
1140 pfile_info->EndOfFile = pSMBr->EndOfFile;
1141 pfile_info->NumberOfLinks = cpu_to_le32(1);
1145 cifs_buf_release(pSMB);
1152 CIFSSMBRead(const int xid, struct cifsTconInfo *tcon,
1153 const int netfid, const unsigned int count,
1154 const __u64 lseek, unsigned int *nbytes, char **buf,
1158 READ_REQ *pSMB = NULL;
1159 READ_RSP *pSMBr = NULL;
1160 char *pReadData = NULL;
1162 int resp_buf_type = 0;
1165 cFYI(1,("Reading %d bytes on fid %d",count,netfid));
1166 if(tcon->ses->capabilities & CAP_LARGE_FILES)
1169 wct = 10; /* old style read */
1172 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **) &pSMB);
1176 /* tcon and ses pointer are checked in smb_init */
1177 if (tcon->ses->server == NULL)
1178 return -ECONNABORTED;
1180 pSMB->AndXCommand = 0xFF; /* none */
1182 pSMB->OffsetLow = cpu_to_le32(lseek & 0xFFFFFFFF);
1184 pSMB->OffsetHigh = cpu_to_le32(lseek >> 32);
1185 else if((lseek >> 32) > 0) /* can not handle this big offset for old */
1188 pSMB->Remaining = 0;
1189 pSMB->MaxCount = cpu_to_le16(count & 0xFFFF);
1190 pSMB->MaxCountHigh = cpu_to_le32(count >> 16);
1192 pSMB->ByteCount = 0; /* no need to do le conversion since 0 */
1194 /* old style read */
1195 struct smb_com_readx_req * pSMBW =
1196 (struct smb_com_readx_req *)pSMB;
1197 pSMBW->ByteCount = 0;
1200 iov[0].iov_base = (char *)pSMB;
1201 iov[0].iov_len = pSMB->hdr.smb_buf_length + 4;
1202 rc = SendReceive2(xid, tcon->ses, iov,
1205 cifs_stats_inc(&tcon->num_reads);
1206 pSMBr = (READ_RSP *)iov[0].iov_base;
1208 cERROR(1, ("Send error in read = %d", rc));
1210 int data_length = le16_to_cpu(pSMBr->DataLengthHigh);
1211 data_length = data_length << 16;
1212 data_length += le16_to_cpu(pSMBr->DataLength);
1213 *nbytes = data_length;
1215 /*check that DataLength would not go beyond end of SMB */
1216 if ((data_length > CIFSMaxBufSize)
1217 || (data_length > count)) {
1218 cFYI(1,("bad length %d for count %d",data_length,count));
1222 pReadData = (char *) (&pSMBr->hdr.Protocol) +
1223 le16_to_cpu(pSMBr->DataOffset);
1224 /* if(rc = copy_to_user(buf, pReadData, data_length)) {
1225 cERROR(1,("Faulting on read rc = %d",rc));
1227 }*/ /* can not use copy_to_user when using page cache*/
1229 memcpy(*buf,pReadData,data_length);
1233 /* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
1235 if(resp_buf_type == CIFS_SMALL_BUFFER)
1236 cifs_small_buf_release(iov[0].iov_base);
1237 else if(resp_buf_type == CIFS_LARGE_BUFFER)
1238 cifs_buf_release(iov[0].iov_base);
1239 } else if(resp_buf_type != CIFS_NO_BUFFER) {
1240 /* return buffer to caller to free */
1241 *buf = iov[0].iov_base;
1242 if(resp_buf_type == CIFS_SMALL_BUFFER)
1243 *pbuf_type = CIFS_SMALL_BUFFER;
1244 else if(resp_buf_type == CIFS_LARGE_BUFFER)
1245 *pbuf_type = CIFS_LARGE_BUFFER;
1246 } /* else no valid buffer on return - leave as null */
1248 /* Note: On -EAGAIN error only caller can retry on handle based calls
1249 since file handle passed in no longer valid */
1255 CIFSSMBWrite(const int xid, struct cifsTconInfo *tcon,
1256 const int netfid, const unsigned int count,
1257 const __u64 offset, unsigned int *nbytes, const char *buf,
1258 const char __user * ubuf, const int long_op)
1261 WRITE_REQ *pSMB = NULL;
1262 WRITE_RSP *pSMBr = NULL;
1263 int bytes_returned, wct;
1267 /* cFYI(1,("write at %lld %d bytes",offset,count));*/
1268 if(tcon->ses == NULL)
1269 return -ECONNABORTED;
1271 if(tcon->ses->capabilities & CAP_LARGE_FILES)
1276 rc = smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB,
1280 /* tcon and ses pointer are checked in smb_init */
1281 if (tcon->ses->server == NULL)
1282 return -ECONNABORTED;
1284 pSMB->AndXCommand = 0xFF; /* none */
1286 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
1288 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
1289 else if((offset >> 32) > 0) /* can not handle this big offset for old */
1292 pSMB->Reserved = 0xFFFFFFFF;
1293 pSMB->WriteMode = 0;
1294 pSMB->Remaining = 0;
1296 /* Can increase buffer size if buffer is big enough in some cases - ie we
1297 can send more if LARGE_WRITE_X capability returned by the server and if
1298 our buffer is big enough or if we convert to iovecs on socket writes
1299 and eliminate the copy to the CIFS buffer */
1300 if(tcon->ses->capabilities & CAP_LARGE_WRITE_X) {
1301 bytes_sent = min_t(const unsigned int, CIFSMaxBufSize, count);
1303 bytes_sent = (tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE)
1307 if (bytes_sent > count)
1310 cpu_to_le16(offsetof(struct smb_com_write_req,Data) - 4);
1312 memcpy(pSMB->Data,buf,bytes_sent);
1314 if(copy_from_user(pSMB->Data,ubuf,bytes_sent)) {
1315 cifs_buf_release(pSMB);
1318 } else if (count != 0) {
1320 cifs_buf_release(pSMB);
1322 } /* else setting file size with write of zero bytes */
1324 byte_count = bytes_sent + 1; /* pad */
1325 else /* wct == 12 */ {
1326 byte_count = bytes_sent + 5; /* bigger pad, smaller smb hdr */
1328 pSMB->DataLengthLow = cpu_to_le16(bytes_sent & 0xFFFF);
1329 pSMB->DataLengthHigh = cpu_to_le16(bytes_sent >> 16);
1330 pSMB->hdr.smb_buf_length += byte_count;
1333 pSMB->ByteCount = cpu_to_le16(byte_count);
1334 else { /* old style write has byte count 4 bytes earlier so 4 bytes pad */
1335 struct smb_com_writex_req * pSMBW =
1336 (struct smb_com_writex_req *)pSMB;
1337 pSMBW->ByteCount = cpu_to_le16(byte_count);
1340 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1341 (struct smb_hdr *) pSMBr, &bytes_returned, long_op);
1342 cifs_stats_inc(&tcon->num_writes);
1344 cFYI(1, ("Send error in write = %d", rc));
1347 *nbytes = le16_to_cpu(pSMBr->CountHigh);
1348 *nbytes = (*nbytes) << 16;
1349 *nbytes += le16_to_cpu(pSMBr->Count);
1352 cifs_buf_release(pSMB);
1354 /* Note: On -EAGAIN error only caller can retry on handle based calls
1355 since file handle passed in no longer valid */
1361 CIFSSMBWrite2(const int xid, struct cifsTconInfo *tcon,
1362 const int netfid, const unsigned int count,
1363 const __u64 offset, unsigned int *nbytes, struct kvec *iov,
1364 int n_vec, const int long_op)
1367 WRITE_REQ *pSMB = NULL;
1370 int resp_buf_type = 0;
1372 cFYI(1,("write2 at %lld %d bytes", (long long)offset, count));
1374 if(tcon->ses->capabilities & CAP_LARGE_FILES)
1378 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB);
1381 /* tcon and ses pointer are checked in smb_init */
1382 if (tcon->ses->server == NULL)
1383 return -ECONNABORTED;
1385 pSMB->AndXCommand = 0xFF; /* none */
1387 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
1389 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
1390 else if((offset >> 32) > 0) /* can not handle this big offset for old */
1392 pSMB->Reserved = 0xFFFFFFFF;
1393 pSMB->WriteMode = 0;
1394 pSMB->Remaining = 0;
1397 cpu_to_le16(offsetof(struct smb_com_write_req,Data) - 4);
1399 pSMB->DataLengthLow = cpu_to_le16(count & 0xFFFF);
1400 pSMB->DataLengthHigh = cpu_to_le16(count >> 16);
1401 smb_hdr_len = pSMB->hdr.smb_buf_length + 1; /* hdr + 1 byte pad */
1403 pSMB->hdr.smb_buf_length += count+1;
1404 else /* wct == 12 */
1405 pSMB->hdr.smb_buf_length += count+5; /* smb data starts later */
1407 pSMB->ByteCount = cpu_to_le16(count + 1);
1408 else /* wct == 12 */ /* bigger pad, smaller smb hdr, keep offset ok */ {
1409 struct smb_com_writex_req * pSMBW =
1410 (struct smb_com_writex_req *)pSMB;
1411 pSMBW->ByteCount = cpu_to_le16(count + 5);
1413 iov[0].iov_base = pSMB;
1415 iov[0].iov_len = smb_hdr_len + 4;
1416 else /* wct == 12 pad bigger by four bytes */
1417 iov[0].iov_len = smb_hdr_len + 8;
1420 rc = SendReceive2(xid, tcon->ses, iov, n_vec + 1, &resp_buf_type,
1422 cifs_stats_inc(&tcon->num_writes);
1424 cFYI(1, ("Send error Write2 = %d", rc));
1426 } else if(resp_buf_type == 0) {
1427 /* presumably this can not happen, but best to be safe */
1431 WRITE_RSP * pSMBr = (WRITE_RSP *)iov[0].iov_base;
1432 *nbytes = le16_to_cpu(pSMBr->CountHigh);
1433 *nbytes = (*nbytes) << 16;
1434 *nbytes += le16_to_cpu(pSMBr->Count);
1437 /* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
1438 if(resp_buf_type == CIFS_SMALL_BUFFER)
1439 cifs_small_buf_release(iov[0].iov_base);
1440 else if(resp_buf_type == CIFS_LARGE_BUFFER)
1441 cifs_buf_release(iov[0].iov_base);
1443 /* Note: On -EAGAIN error only caller can retry on handle based calls
1444 since file handle passed in no longer valid */
1451 CIFSSMBLock(const int xid, struct cifsTconInfo *tcon,
1452 const __u16 smb_file_id, const __u64 len,
1453 const __u64 offset, const __u32 numUnlock,
1454 const __u32 numLock, const __u8 lockType, const int waitFlag)
1457 LOCK_REQ *pSMB = NULL;
1458 LOCK_RSP *pSMBr = NULL;
1463 cFYI(1, ("In CIFSSMBLock - timeout %d numLock %d",waitFlag,numLock));
1464 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
1469 pSMBr = (LOCK_RSP *)pSMB; /* BB removeme BB */
1471 if(lockType == LOCKING_ANDX_OPLOCK_RELEASE) {
1472 timeout = -1; /* no response expected */
1474 } else if (waitFlag == TRUE) {
1475 timeout = 3; /* blocking operation, no timeout */
1476 pSMB->Timeout = cpu_to_le32(-1);/* blocking - do not time out */
1481 pSMB->NumberOfLocks = cpu_to_le16(numLock);
1482 pSMB->NumberOfUnlocks = cpu_to_le16(numUnlock);
1483 pSMB->LockType = lockType;
1484 pSMB->AndXCommand = 0xFF; /* none */
1485 pSMB->Fid = smb_file_id; /* netfid stays le */
1487 if((numLock != 0) || (numUnlock != 0)) {
1488 pSMB->Locks[0].Pid = cpu_to_le16(current->tgid);
1489 /* BB where to store pid high? */
1490 pSMB->Locks[0].LengthLow = cpu_to_le32((u32)len);
1491 pSMB->Locks[0].LengthHigh = cpu_to_le32((u32)(len>>32));
1492 pSMB->Locks[0].OffsetLow = cpu_to_le32((u32)offset);
1493 pSMB->Locks[0].OffsetHigh = cpu_to_le32((u32)(offset>>32));
1494 count = sizeof(LOCKING_ANDX_RANGE);
1499 pSMB->hdr.smb_buf_length += count;
1500 pSMB->ByteCount = cpu_to_le16(count);
1503 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
1504 (struct smb_hdr *) pSMBr, &bytes_returned);
1506 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1507 (struct smb_hdr *) pSMBr, &bytes_returned, timeout);
1509 cifs_stats_inc(&tcon->num_locks);
1511 cFYI(1, ("Send error in Lock = %d", rc));
1513 cifs_small_buf_release(pSMB);
1515 /* Note: On -EAGAIN error only caller can retry on handle based calls
1516 since file handle passed in no longer valid */
1521 CIFSSMBPosixLock(const int xid, struct cifsTconInfo *tcon,
1522 const __u16 smb_file_id, const int get_flag, const __u64 len,
1523 struct file_lock *pLockData, const __u16 lock_type,
1526 struct smb_com_transaction2_sfi_req *pSMB = NULL;
1527 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
1529 struct cifs_posix_lock *parm_data;
1532 int bytes_returned = 0;
1533 __u16 params, param_offset, offset, byte_count, count;
1535 cFYI(1, ("Posix Lock"));
1537 if(pLockData == NULL)
1540 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
1545 pSMBr = (struct smb_com_transaction2_sfi_rsp *)pSMB;
1548 pSMB->MaxSetupCount = 0;
1551 pSMB->Reserved2 = 0;
1552 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
1553 offset = param_offset + params;
1555 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
1557 count = sizeof(struct cifs_posix_lock);
1558 pSMB->MaxParameterCount = cpu_to_le16(2);
1559 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB PDU from sess */
1560 pSMB->SetupCount = 1;
1561 pSMB->Reserved3 = 0;
1563 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
1565 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
1566 byte_count = 3 /* pad */ + params + count;
1567 pSMB->DataCount = cpu_to_le16(count);
1568 pSMB->ParameterCount = cpu_to_le16(params);
1569 pSMB->TotalDataCount = pSMB->DataCount;
1570 pSMB->TotalParameterCount = pSMB->ParameterCount;
1571 pSMB->ParameterOffset = cpu_to_le16(param_offset);
1572 parm_data = (struct cifs_posix_lock *)
1573 (((char *) &pSMB->hdr.Protocol) + offset);
1575 parm_data->lock_type = cpu_to_le16(lock_type);
1577 timeout = 3; /* blocking operation, no timeout */
1578 parm_data->lock_flags = cpu_to_le16(1);
1579 pSMB->Timeout = cpu_to_le32(-1);
1583 parm_data->pid = cpu_to_le32(current->tgid);
1584 parm_data->start = cpu_to_le64(pLockData->fl_start);
1585 parm_data->length = cpu_to_le64(len); /* normalize negative numbers */
1587 pSMB->DataOffset = cpu_to_le16(offset);
1588 pSMB->Fid = smb_file_id;
1589 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_LOCK);
1590 pSMB->Reserved4 = 0;
1591 pSMB->hdr.smb_buf_length += byte_count;
1592 pSMB->ByteCount = cpu_to_le16(byte_count);
1594 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
1595 (struct smb_hdr *) pSMBr, &bytes_returned);
1597 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1598 (struct smb_hdr *) pSMBr, &bytes_returned, timeout);
1602 cFYI(1, ("Send error in Posix Lock = %d", rc));
1603 } else if (get_flag) {
1604 /* lock structure can be returned on get */
1607 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
1609 if (rc || (pSMBr->ByteCount < sizeof(struct cifs_posix_lock))) {
1610 rc = -EIO; /* bad smb */
1613 if(pLockData == NULL) {
1617 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
1618 data_count = le16_to_cpu(pSMBr->t2.DataCount);
1619 if(data_count < sizeof(struct cifs_posix_lock)) {
1623 parm_data = (struct cifs_posix_lock *)
1624 ((char *)&pSMBr->hdr.Protocol + data_offset);
1625 if(parm_data->lock_type == cpu_to_le16(CIFS_UNLCK))
1626 pLockData->fl_type = F_UNLCK;
1631 cifs_small_buf_release(pSMB);
1633 /* Note: On -EAGAIN error only caller can retry on handle based calls
1634 since file handle passed in no longer valid */
1641 CIFSSMBClose(const int xid, struct cifsTconInfo *tcon, int smb_file_id)
1644 CLOSE_REQ *pSMB = NULL;
1645 CLOSE_RSP *pSMBr = NULL;
1647 cFYI(1, ("In CIFSSMBClose"));
1649 /* do not retry on dead session on close */
1650 rc = small_smb_init(SMB_COM_CLOSE, 3, tcon, (void **) &pSMB);
1656 pSMBr = (CLOSE_RSP *)pSMB; /* BB removeme BB */
1658 pSMB->FileID = (__u16) smb_file_id;
1659 pSMB->LastWriteTime = 0xFFFFFFFF;
1660 pSMB->ByteCount = 0;
1661 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1662 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
1663 cifs_stats_inc(&tcon->num_closes);
1666 /* EINTR is expected when user ctl-c to kill app */
1667 cERROR(1, ("Send error in Close = %d", rc));
1671 cifs_small_buf_release(pSMB);
1673 /* Since session is dead, file will be closed on server already */
1681 CIFSSMBRename(const int xid, struct cifsTconInfo *tcon,
1682 const char *fromName, const char *toName,
1683 const struct nls_table *nls_codepage, int remap)
1686 RENAME_REQ *pSMB = NULL;
1687 RENAME_RSP *pSMBr = NULL;
1689 int name_len, name_len2;
1692 cFYI(1, ("In CIFSSMBRename"));
1694 rc = smb_init(SMB_COM_RENAME, 1, tcon, (void **) &pSMB,
1699 pSMB->BufferFormat = 0x04;
1700 pSMB->SearchAttributes =
1701 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
1704 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1706 cifsConvertToUCS((__le16 *) pSMB->OldFileName, fromName,
1707 PATH_MAX, nls_codepage, remap);
1708 name_len++; /* trailing null */
1710 pSMB->OldFileName[name_len] = 0x04; /* pad */
1711 /* protocol requires ASCII signature byte on Unicode string */
1712 pSMB->OldFileName[name_len + 1] = 0x00;
1714 cifsConvertToUCS((__le16 *) &pSMB->OldFileName[name_len + 2],
1715 toName, PATH_MAX, nls_codepage, remap);
1716 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
1717 name_len2 *= 2; /* convert to bytes */
1718 } else { /* BB improve the check for buffer overruns BB */
1719 name_len = strnlen(fromName, PATH_MAX);
1720 name_len++; /* trailing null */
1721 strncpy(pSMB->OldFileName, fromName, name_len);
1722 name_len2 = strnlen(toName, PATH_MAX);
1723 name_len2++; /* trailing null */
1724 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
1725 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
1726 name_len2++; /* trailing null */
1727 name_len2++; /* signature byte */
1730 count = 1 /* 1st signature byte */ + name_len + name_len2;
1731 pSMB->hdr.smb_buf_length += count;
1732 pSMB->ByteCount = cpu_to_le16(count);
1734 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1735 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
1736 cifs_stats_inc(&tcon->num_renames);
1738 cFYI(1, ("Send error in rename = %d", rc));
1741 cifs_buf_release(pSMB);
1749 int CIFSSMBRenameOpenFile(const int xid,struct cifsTconInfo *pTcon,
1750 int netfid, char * target_name,
1751 const struct nls_table * nls_codepage, int remap)
1753 struct smb_com_transaction2_sfi_req *pSMB = NULL;
1754 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
1755 struct set_file_rename * rename_info;
1757 char dummy_string[30];
1759 int bytes_returned = 0;
1761 __u16 params, param_offset, offset, count, byte_count;
1763 cFYI(1, ("Rename to File by handle"));
1764 rc = smb_init(SMB_COM_TRANSACTION2, 15, pTcon, (void **) &pSMB,
1770 pSMB->MaxSetupCount = 0;
1774 pSMB->Reserved2 = 0;
1775 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
1776 offset = param_offset + params;
1778 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
1779 rename_info = (struct set_file_rename *) data_offset;
1780 pSMB->MaxParameterCount = cpu_to_le16(2);
1781 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB PDU from sess */
1782 pSMB->SetupCount = 1;
1783 pSMB->Reserved3 = 0;
1784 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
1785 byte_count = 3 /* pad */ + params;
1786 pSMB->ParameterCount = cpu_to_le16(params);
1787 pSMB->TotalParameterCount = pSMB->ParameterCount;
1788 pSMB->ParameterOffset = cpu_to_le16(param_offset);
1789 pSMB->DataOffset = cpu_to_le16(offset);
1790 /* construct random name ".cifs_tmp<inodenum><mid>" */
1791 rename_info->overwrite = cpu_to_le32(1);
1792 rename_info->root_fid = 0;
1793 /* unicode only call */
1794 if(target_name == NULL) {
1795 sprintf(dummy_string,"cifs%x",pSMB->hdr.Mid);
1796 len_of_str = cifsConvertToUCS((__le16 *)rename_info->target_name,
1797 dummy_string, 24, nls_codepage, remap);
1799 len_of_str = cifsConvertToUCS((__le16 *)rename_info->target_name,
1800 target_name, PATH_MAX, nls_codepage, remap);
1802 rename_info->target_name_len = cpu_to_le32(2 * len_of_str);
1803 count = 12 /* sizeof(struct set_file_rename) */ + (2 * len_of_str) + 2;
1804 byte_count += count;
1805 pSMB->DataCount = cpu_to_le16(count);
1806 pSMB->TotalDataCount = pSMB->DataCount;
1808 pSMB->InformationLevel =
1809 cpu_to_le16(SMB_SET_FILE_RENAME_INFORMATION);
1810 pSMB->Reserved4 = 0;
1811 pSMB->hdr.smb_buf_length += byte_count;
1812 pSMB->ByteCount = cpu_to_le16(byte_count);
1813 rc = SendReceive(xid, pTcon->ses, (struct smb_hdr *) pSMB,
1814 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
1815 cifs_stats_inc(&pTcon->num_t2renames);
1817 cFYI(1,("Send error in Rename (by file handle) = %d", rc));
1820 cifs_buf_release(pSMB);
1822 /* Note: On -EAGAIN error only caller can retry on handle based calls
1823 since file handle passed in no longer valid */
1829 CIFSSMBCopy(const int xid, struct cifsTconInfo *tcon, const char * fromName,
1830 const __u16 target_tid, const char *toName, const int flags,
1831 const struct nls_table *nls_codepage, int remap)
1834 COPY_REQ *pSMB = NULL;
1835 COPY_RSP *pSMBr = NULL;
1837 int name_len, name_len2;
1840 cFYI(1, ("In CIFSSMBCopy"));
1842 rc = smb_init(SMB_COM_COPY, 1, tcon, (void **) &pSMB,
1847 pSMB->BufferFormat = 0x04;
1848 pSMB->Tid2 = target_tid;
1850 pSMB->Flags = cpu_to_le16(flags & COPY_TREE);
1852 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1853 name_len = cifsConvertToUCS((__le16 *) pSMB->OldFileName,
1854 fromName, PATH_MAX, nls_codepage,
1856 name_len++; /* trailing null */
1858 pSMB->OldFileName[name_len] = 0x04; /* pad */
1859 /* protocol requires ASCII signature byte on Unicode string */
1860 pSMB->OldFileName[name_len + 1] = 0x00;
1861 name_len2 = cifsConvertToUCS((__le16 *)&pSMB->OldFileName[name_len + 2],
1862 toName, PATH_MAX, nls_codepage, remap);
1863 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
1864 name_len2 *= 2; /* convert to bytes */
1865 } else { /* BB improve the check for buffer overruns BB */
1866 name_len = strnlen(fromName, PATH_MAX);
1867 name_len++; /* trailing null */
1868 strncpy(pSMB->OldFileName, fromName, name_len);
1869 name_len2 = strnlen(toName, PATH_MAX);
1870 name_len2++; /* trailing null */
1871 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
1872 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
1873 name_len2++; /* trailing null */
1874 name_len2++; /* signature byte */
1877 count = 1 /* 1st signature byte */ + name_len + name_len2;
1878 pSMB->hdr.smb_buf_length += count;
1879 pSMB->ByteCount = cpu_to_le16(count);
1881 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1882 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
1884 cFYI(1, ("Send error in copy = %d with %d files copied",
1885 rc, le16_to_cpu(pSMBr->CopyCount)));
1888 cifs_buf_release(pSMB);
1897 CIFSUnixCreateSymLink(const int xid, struct cifsTconInfo *tcon,
1898 const char *fromName, const char *toName,
1899 const struct nls_table *nls_codepage)
1901 TRANSACTION2_SPI_REQ *pSMB = NULL;
1902 TRANSACTION2_SPI_RSP *pSMBr = NULL;
1905 int name_len_target;
1907 int bytes_returned = 0;
1908 __u16 params, param_offset, offset, byte_count;
1910 cFYI(1, ("In Symlink Unix style"));
1912 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
1917 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1919 cifs_strtoUCS((__le16 *) pSMB->FileName, fromName, PATH_MAX
1920 /* find define for this maxpathcomponent */
1922 name_len++; /* trailing null */
1925 } else { /* BB improve the check for buffer overruns BB */
1926 name_len = strnlen(fromName, PATH_MAX);
1927 name_len++; /* trailing null */
1928 strncpy(pSMB->FileName, fromName, name_len);
1930 params = 6 + name_len;
1931 pSMB->MaxSetupCount = 0;
1935 pSMB->Reserved2 = 0;
1936 param_offset = offsetof(struct smb_com_transaction2_spi_req,
1937 InformationLevel) - 4;
1938 offset = param_offset + params;
1940 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
1941 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1943 cifs_strtoUCS((__le16 *) data_offset, toName, PATH_MAX
1944 /* find define for this maxpathcomponent */
1946 name_len_target++; /* trailing null */
1947 name_len_target *= 2;
1948 } else { /* BB improve the check for buffer overruns BB */
1949 name_len_target = strnlen(toName, PATH_MAX);
1950 name_len_target++; /* trailing null */
1951 strncpy(data_offset, toName, name_len_target);
1954 pSMB->MaxParameterCount = cpu_to_le16(2);
1955 /* BB find exact max on data count below from sess */
1956 pSMB->MaxDataCount = cpu_to_le16(1000);
1957 pSMB->SetupCount = 1;
1958 pSMB->Reserved3 = 0;
1959 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
1960 byte_count = 3 /* pad */ + params + name_len_target;
1961 pSMB->DataCount = cpu_to_le16(name_len_target);
1962 pSMB->ParameterCount = cpu_to_le16(params);
1963 pSMB->TotalDataCount = pSMB->DataCount;
1964 pSMB->TotalParameterCount = pSMB->ParameterCount;
1965 pSMB->ParameterOffset = cpu_to_le16(param_offset);
1966 pSMB->DataOffset = cpu_to_le16(offset);
1967 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_LINK);
1968 pSMB->Reserved4 = 0;
1969 pSMB->hdr.smb_buf_length += byte_count;
1970 pSMB->ByteCount = cpu_to_le16(byte_count);
1971 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1972 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
1973 cifs_stats_inc(&tcon->num_symlinks);
1976 ("Send error in SetPathInfo (create symlink) = %d",
1981 cifs_buf_release(pSMB);
1984 goto createSymLinkRetry;
1990 CIFSUnixCreateHardLink(const int xid, struct cifsTconInfo *tcon,
1991 const char *fromName, const char *toName,
1992 const struct nls_table *nls_codepage, int remap)
1994 TRANSACTION2_SPI_REQ *pSMB = NULL;
1995 TRANSACTION2_SPI_RSP *pSMBr = NULL;
1998 int name_len_target;
2000 int bytes_returned = 0;
2001 __u16 params, param_offset, offset, byte_count;
2003 cFYI(1, ("In Create Hard link Unix style"));
2004 createHardLinkRetry:
2005 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2010 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2011 name_len = cifsConvertToUCS((__le16 *) pSMB->FileName, toName,
2012 PATH_MAX, nls_codepage, remap);
2013 name_len++; /* trailing null */
2016 } else { /* BB improve the check for buffer overruns BB */
2017 name_len = strnlen(toName, PATH_MAX);
2018 name_len++; /* trailing null */
2019 strncpy(pSMB->FileName, toName, name_len);
2021 params = 6 + name_len;
2022 pSMB->MaxSetupCount = 0;
2026 pSMB->Reserved2 = 0;
2027 param_offset = offsetof(struct smb_com_transaction2_spi_req,
2028 InformationLevel) - 4;
2029 offset = param_offset + params;
2031 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2032 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2034 cifsConvertToUCS((__le16 *) data_offset, fromName, PATH_MAX,
2035 nls_codepage, remap);
2036 name_len_target++; /* trailing null */
2037 name_len_target *= 2;
2038 } else { /* BB improve the check for buffer overruns BB */
2039 name_len_target = strnlen(fromName, PATH_MAX);
2040 name_len_target++; /* trailing null */
2041 strncpy(data_offset, fromName, name_len_target);
2044 pSMB->MaxParameterCount = cpu_to_le16(2);
2045 /* BB find exact max on data count below from sess*/
2046 pSMB->MaxDataCount = cpu_to_le16(1000);
2047 pSMB->SetupCount = 1;
2048 pSMB->Reserved3 = 0;
2049 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2050 byte_count = 3 /* pad */ + params + name_len_target;
2051 pSMB->ParameterCount = cpu_to_le16(params);
2052 pSMB->TotalParameterCount = pSMB->ParameterCount;
2053 pSMB->DataCount = cpu_to_le16(name_len_target);
2054 pSMB->TotalDataCount = pSMB->DataCount;
2055 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2056 pSMB->DataOffset = cpu_to_le16(offset);
2057 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_HLINK);
2058 pSMB->Reserved4 = 0;
2059 pSMB->hdr.smb_buf_length += byte_count;
2060 pSMB->ByteCount = cpu_to_le16(byte_count);
2061 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2062 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2063 cifs_stats_inc(&tcon->num_hardlinks);
2065 cFYI(1, ("Send error in SetPathInfo (hard link) = %d", rc));
2068 cifs_buf_release(pSMB);
2070 goto createHardLinkRetry;
2076 CIFSCreateHardLink(const int xid, struct cifsTconInfo *tcon,
2077 const char *fromName, const char *toName,
2078 const struct nls_table *nls_codepage, int remap)
2081 NT_RENAME_REQ *pSMB = NULL;
2082 RENAME_RSP *pSMBr = NULL;
2084 int name_len, name_len2;
2087 cFYI(1, ("In CIFSCreateHardLink"));
2088 winCreateHardLinkRetry:
2090 rc = smb_init(SMB_COM_NT_RENAME, 4, tcon, (void **) &pSMB,
2095 pSMB->SearchAttributes =
2096 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
2098 pSMB->Flags = cpu_to_le16(CREATE_HARD_LINK);
2099 pSMB->ClusterCount = 0;
2101 pSMB->BufferFormat = 0x04;
2103 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2105 cifsConvertToUCS((__le16 *) pSMB->OldFileName, fromName,
2106 PATH_MAX, nls_codepage, remap);
2107 name_len++; /* trailing null */
2109 pSMB->OldFileName[name_len] = 0; /* pad */
2110 pSMB->OldFileName[name_len + 1] = 0x04;
2112 cifsConvertToUCS((__le16 *)&pSMB->OldFileName[name_len + 2],
2113 toName, PATH_MAX, nls_codepage, remap);
2114 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2115 name_len2 *= 2; /* convert to bytes */
2116 } else { /* BB improve the check for buffer overruns BB */
2117 name_len = strnlen(fromName, PATH_MAX);
2118 name_len++; /* trailing null */
2119 strncpy(pSMB->OldFileName, fromName, name_len);
2120 name_len2 = strnlen(toName, PATH_MAX);
2121 name_len2++; /* trailing null */
2122 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2123 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
2124 name_len2++; /* trailing null */
2125 name_len2++; /* signature byte */
2128 count = 1 /* string type byte */ + name_len + name_len2;
2129 pSMB->hdr.smb_buf_length += count;
2130 pSMB->ByteCount = cpu_to_le16(count);
2132 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2133 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2134 cifs_stats_inc(&tcon->num_hardlinks);
2136 cFYI(1, ("Send error in hard link (NT rename) = %d", rc));
2138 cifs_buf_release(pSMB);
2140 goto winCreateHardLinkRetry;
2146 CIFSSMBUnixQuerySymLink(const int xid, struct cifsTconInfo *tcon,
2147 const unsigned char *searchName,
2148 char *symlinkinfo, const int buflen,
2149 const struct nls_table *nls_codepage)
2151 /* SMB_QUERY_FILE_UNIX_LINK */
2152 TRANSACTION2_QPI_REQ *pSMB = NULL;
2153 TRANSACTION2_QPI_RSP *pSMBr = NULL;
2157 __u16 params, byte_count;
2159 cFYI(1, ("In QPathSymLinkInfo (Unix) for path %s", searchName));
2162 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2167 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2169 cifs_strtoUCS((__le16 *) pSMB->FileName, searchName, PATH_MAX
2170 /* find define for this maxpathcomponent */
2172 name_len++; /* trailing null */
2174 } else { /* BB improve the check for buffer overruns BB */
2175 name_len = strnlen(searchName, PATH_MAX);
2176 name_len++; /* trailing null */
2177 strncpy(pSMB->FileName, searchName, name_len);
2180 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
2181 pSMB->TotalDataCount = 0;
2182 pSMB->MaxParameterCount = cpu_to_le16(2);
2183 /* BB find exact max data count below from sess structure BB */
2184 pSMB->MaxDataCount = cpu_to_le16(4000);
2185 pSMB->MaxSetupCount = 0;
2189 pSMB->Reserved2 = 0;
2190 pSMB->ParameterOffset = cpu_to_le16(offsetof(
2191 struct smb_com_transaction2_qpi_req ,InformationLevel) - 4);
2192 pSMB->DataCount = 0;
2193 pSMB->DataOffset = 0;
2194 pSMB->SetupCount = 1;
2195 pSMB->Reserved3 = 0;
2196 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
2197 byte_count = params + 1 /* pad */ ;
2198 pSMB->TotalParameterCount = cpu_to_le16(params);
2199 pSMB->ParameterCount = pSMB->TotalParameterCount;
2200 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_LINK);
2201 pSMB->Reserved4 = 0;
2202 pSMB->hdr.smb_buf_length += byte_count;
2203 pSMB->ByteCount = cpu_to_le16(byte_count);
2205 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2206 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2208 cFYI(1, ("Send error in QuerySymLinkInfo = %d", rc));
2210 /* decode response */
2212 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
2213 if (rc || (pSMBr->ByteCount < 2))
2214 /* BB also check enough total bytes returned */
2215 rc = -EIO; /* bad smb */
2217 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
2218 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
2220 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE) {
2221 name_len = UniStrnlen((wchar_t *) ((char *)
2222 &pSMBr->hdr.Protocol +data_offset),
2223 min_t(const int, buflen,count) / 2);
2224 /* BB FIXME investigate remapping reserved chars here */
2225 cifs_strfromUCS_le(symlinkinfo,
2226 (__le16 *) ((char *)&pSMBr->hdr.Protocol +
2228 name_len, nls_codepage);
2230 strncpy(symlinkinfo,
2231 (char *) &pSMBr->hdr.Protocol +
2233 min_t(const int, buflen, count));
2235 symlinkinfo[buflen] = 0;
2236 /* just in case so calling code does not go off the end of buffer */
2239 cifs_buf_release(pSMB);
2241 goto querySymLinkRetry;
2245 /* Initialize NT TRANSACT SMB into small smb request buffer.
2246 This assumes that all NT TRANSACTS that we init here have
2247 total parm and data under about 400 bytes (to fit in small cifs
2248 buffer size), which is the case so far, it easily fits. NB:
2249 Setup words themselves and ByteCount
2250 MaxSetupCount (size of returned setup area) and
2251 MaxParameterCount (returned parms size) must be set by caller */
2253 smb_init_ntransact(const __u16 sub_command, const int setup_count,
2254 const int parm_len, struct cifsTconInfo *tcon,
2259 struct smb_com_ntransact_req * pSMB;
2261 rc = small_smb_init(SMB_COM_NT_TRANSACT, 19 + setup_count, tcon,
2265 *ret_buf = (void *)pSMB;
2267 pSMB->TotalParameterCount = cpu_to_le32(parm_len);
2268 pSMB->TotalDataCount = 0;
2269 pSMB->MaxDataCount = cpu_to_le32((tcon->ses->server->maxBuf -
2270 MAX_CIFS_HDR_SIZE) & 0xFFFFFF00);
2271 pSMB->ParameterCount = pSMB->TotalParameterCount;
2272 pSMB->DataCount = pSMB->TotalDataCount;
2273 temp_offset = offsetof(struct smb_com_ntransact_req, Parms) +
2274 (setup_count * 2) - 4 /* for rfc1001 length itself */;
2275 pSMB->ParameterOffset = cpu_to_le32(temp_offset);
2276 pSMB->DataOffset = cpu_to_le32(temp_offset + parm_len);
2277 pSMB->SetupCount = setup_count; /* no need to le convert byte fields */
2278 pSMB->SubCommand = cpu_to_le16(sub_command);
2283 validate_ntransact(char * buf, char ** ppparm, char ** ppdata,
2284 int * pdatalen, int * pparmlen)
2287 __u32 data_count, data_offset, parm_count, parm_offset;
2288 struct smb_com_ntransact_rsp * pSMBr;
2293 pSMBr = (struct smb_com_ntransact_rsp *)buf;
2295 /* ByteCount was converted from little endian in SendReceive */
2296 end_of_smb = 2 /* sizeof byte count */ + pSMBr->ByteCount +
2297 (char *)&pSMBr->ByteCount;
2300 data_offset = le32_to_cpu(pSMBr->DataOffset);
2301 data_count = le32_to_cpu(pSMBr->DataCount);
2302 parm_offset = le32_to_cpu(pSMBr->ParameterOffset);
2303 parm_count = le32_to_cpu(pSMBr->ParameterCount);
2305 *ppparm = (char *)&pSMBr->hdr.Protocol + parm_offset;
2306 *ppdata = (char *)&pSMBr->hdr.Protocol + data_offset;
2308 /* should we also check that parm and data areas do not overlap? */
2309 if(*ppparm > end_of_smb) {
2310 cFYI(1,("parms start after end of smb"));
2312 } else if(parm_count + *ppparm > end_of_smb) {
2313 cFYI(1,("parm end after end of smb"));
2315 } else if(*ppdata > end_of_smb) {
2316 cFYI(1,("data starts after end of smb"));
2318 } else if(data_count + *ppdata > end_of_smb) {
2319 cFYI(1,("data %p + count %d (%p) ends after end of smb %p start %p",
2320 *ppdata, data_count, (data_count + *ppdata), end_of_smb, pSMBr)); /* BB FIXME */
2322 } else if(parm_count + data_count > pSMBr->ByteCount) {
2323 cFYI(1,("parm count and data count larger than SMB"));
2330 CIFSSMBQueryReparseLinkInfo(const int xid, struct cifsTconInfo *tcon,
2331 const unsigned char *searchName,
2332 char *symlinkinfo, const int buflen,__u16 fid,
2333 const struct nls_table *nls_codepage)
2338 struct smb_com_transaction_ioctl_req * pSMB;
2339 struct smb_com_transaction_ioctl_rsp * pSMBr;
2341 cFYI(1, ("In Windows reparse style QueryLink for path %s", searchName));
2342 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
2347 pSMB->TotalParameterCount = 0 ;
2348 pSMB->TotalDataCount = 0;
2349 pSMB->MaxParameterCount = cpu_to_le32(2);
2350 /* BB find exact data count max from sess structure BB */
2351 pSMB->MaxDataCount = cpu_to_le32((tcon->ses->server->maxBuf -
2352 MAX_CIFS_HDR_SIZE) & 0xFFFFFF00);
2353 pSMB->MaxSetupCount = 4;
2355 pSMB->ParameterOffset = 0;
2356 pSMB->DataCount = 0;
2357 pSMB->DataOffset = 0;
2358 pSMB->SetupCount = 4;
2359 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
2360 pSMB->ParameterCount = pSMB->TotalParameterCount;
2361 pSMB->FunctionCode = cpu_to_le32(FSCTL_GET_REPARSE_POINT);
2362 pSMB->IsFsctl = 1; /* FSCTL */
2363 pSMB->IsRootFlag = 0;
2364 pSMB->Fid = fid; /* file handle always le */
2365 pSMB->ByteCount = 0;
2367 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2368 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2370 cFYI(1, ("Send error in QueryReparseLinkInfo = %d", rc));
2371 } else { /* decode response */
2372 __u32 data_offset = le32_to_cpu(pSMBr->DataOffset);
2373 __u32 data_count = le32_to_cpu(pSMBr->DataCount);
2374 if ((pSMBr->ByteCount < 2) || (data_offset > 512))
2375 /* BB also check enough total bytes returned */
2376 rc = -EIO; /* bad smb */
2378 if(data_count && (data_count < 2048)) {
2379 char * end_of_smb = 2 /* sizeof byte count */ +
2381 (char *)&pSMBr->ByteCount;
2383 struct reparse_data * reparse_buf = (struct reparse_data *)
2384 ((char *)&pSMBr->hdr.Protocol + data_offset);
2385 if((char*)reparse_buf >= end_of_smb) {
2389 if((reparse_buf->LinkNamesBuf +
2390 reparse_buf->TargetNameOffset +
2391 reparse_buf->TargetNameLen) >
2393 cFYI(1,("reparse buf extended beyond SMB"));
2398 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE) {
2399 name_len = UniStrnlen((wchar_t *)
2400 (reparse_buf->LinkNamesBuf +
2401 reparse_buf->TargetNameOffset),
2402 min(buflen/2, reparse_buf->TargetNameLen / 2));
2403 cifs_strfromUCS_le(symlinkinfo,
2404 (__le16 *) (reparse_buf->LinkNamesBuf +
2405 reparse_buf->TargetNameOffset),
2406 name_len, nls_codepage);
2407 } else { /* ASCII names */
2408 strncpy(symlinkinfo,reparse_buf->LinkNamesBuf +
2409 reparse_buf->TargetNameOffset,
2410 min_t(const int, buflen, reparse_buf->TargetNameLen));
2414 cFYI(1,("Invalid return data count on get reparse info ioctl"));
2416 symlinkinfo[buflen] = 0; /* just in case so the caller
2417 does not go off the end of the buffer */
2418 cFYI(1,("readlink result - %s",symlinkinfo));
2422 cifs_buf_release(pSMB);
2424 /* Note: On -EAGAIN error only caller can retry on handle based calls
2425 since file handle passed in no longer valid */
2430 #ifdef CONFIG_CIFS_POSIX
2432 /*Convert an Access Control Entry from wire format to local POSIX xattr format*/
2433 static void cifs_convert_ace(posix_acl_xattr_entry * ace, struct cifs_posix_ace * cifs_ace)
2435 /* u8 cifs fields do not need le conversion */
2436 ace->e_perm = cpu_to_le16(cifs_ace->cifs_e_perm);
2437 ace->e_tag = cpu_to_le16(cifs_ace->cifs_e_tag);
2438 ace->e_id = cpu_to_le32(le64_to_cpu(cifs_ace->cifs_uid));
2439 /* cFYI(1,("perm %d tag %d id %d",ace->e_perm,ace->e_tag,ace->e_id)); */
2444 /* Convert ACL from CIFS POSIX wire format to local Linux POSIX ACL xattr */
2445 static int cifs_copy_posix_acl(char * trgt,char * src, const int buflen,
2446 const int acl_type,const int size_of_data_area)
2451 struct cifs_posix_ace * pACE;
2452 struct cifs_posix_acl * cifs_acl = (struct cifs_posix_acl *)src;
2453 posix_acl_xattr_header * local_acl = (posix_acl_xattr_header *)trgt;
2455 if (le16_to_cpu(cifs_acl->version) != CIFS_ACL_VERSION)
2458 if(acl_type & ACL_TYPE_ACCESS) {
2459 count = le16_to_cpu(cifs_acl->access_entry_count);
2460 pACE = &cifs_acl->ace_array[0];
2461 size = sizeof(struct cifs_posix_acl);
2462 size += sizeof(struct cifs_posix_ace) * count;
2463 /* check if we would go beyond end of SMB */
2464 if(size_of_data_area < size) {
2465 cFYI(1,("bad CIFS POSIX ACL size %d vs. %d",size_of_data_area,size));
2468 } else if(acl_type & ACL_TYPE_DEFAULT) {
2469 count = le16_to_cpu(cifs_acl->access_entry_count);
2470 size = sizeof(struct cifs_posix_acl);
2471 size += sizeof(struct cifs_posix_ace) * count;
2472 /* skip past access ACEs to get to default ACEs */
2473 pACE = &cifs_acl->ace_array[count];
2474 count = le16_to_cpu(cifs_acl->default_entry_count);
2475 size += sizeof(struct cifs_posix_ace) * count;
2476 /* check if we would go beyond end of SMB */
2477 if(size_of_data_area < size)
2484 size = posix_acl_xattr_size(count);
2485 if((buflen == 0) || (local_acl == NULL)) {
2486 /* used to query ACL EA size */
2487 } else if(size > buflen) {
2489 } else /* buffer big enough */ {
2490 local_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
2491 for(i = 0;i < count ;i++) {
2492 cifs_convert_ace(&local_acl->a_entries[i],pACE);
2499 static __u16 convert_ace_to_cifs_ace(struct cifs_posix_ace * cifs_ace,
2500 const posix_acl_xattr_entry * local_ace)
2502 __u16 rc = 0; /* 0 = ACL converted ok */
2504 cifs_ace->cifs_e_perm = le16_to_cpu(local_ace->e_perm);
2505 cifs_ace->cifs_e_tag = le16_to_cpu(local_ace->e_tag);
2506 /* BB is there a better way to handle the large uid? */
2507 if(local_ace->e_id == cpu_to_le32(-1)) {
2508 /* Probably no need to le convert -1 on any arch but can not hurt */
2509 cifs_ace->cifs_uid = cpu_to_le64(-1);
2511 cifs_ace->cifs_uid = cpu_to_le64(le32_to_cpu(local_ace->e_id));
2512 /*cFYI(1,("perm %d tag %d id %d",ace->e_perm,ace->e_tag,ace->e_id));*/
2516 /* Convert ACL from local Linux POSIX xattr to CIFS POSIX ACL wire format */
2517 static __u16 ACL_to_cifs_posix(char * parm_data,const char * pACL,const int buflen,
2521 struct cifs_posix_acl * cifs_acl = (struct cifs_posix_acl *)parm_data;
2522 posix_acl_xattr_header * local_acl = (posix_acl_xattr_header *)pACL;
2526 if((buflen == 0) || (pACL == NULL) || (cifs_acl == NULL))
2529 count = posix_acl_xattr_count((size_t)buflen);
2530 cFYI(1,("setting acl with %d entries from buf of length %d and version of %d",
2531 count, buflen, le32_to_cpu(local_acl->a_version)));
2532 if(le32_to_cpu(local_acl->a_version) != 2) {
2533 cFYI(1,("unknown POSIX ACL version %d",
2534 le32_to_cpu(local_acl->a_version)));
2537 cifs_acl->version = cpu_to_le16(1);
2538 if(acl_type == ACL_TYPE_ACCESS)
2539 cifs_acl->access_entry_count = cpu_to_le16(count);
2540 else if(acl_type == ACL_TYPE_DEFAULT)
2541 cifs_acl->default_entry_count = cpu_to_le16(count);
2543 cFYI(1,("unknown ACL type %d",acl_type));
2546 for(i=0;i<count;i++) {
2547 rc = convert_ace_to_cifs_ace(&cifs_acl->ace_array[i],
2548 &local_acl->a_entries[i]);
2550 /* ACE not converted */
2555 rc = (__u16)(count * sizeof(struct cifs_posix_ace));
2556 rc += sizeof(struct cifs_posix_acl);
2557 /* BB add check to make sure ACL does not overflow SMB */
2563 CIFSSMBGetPosixACL(const int xid, struct cifsTconInfo *tcon,
2564 const unsigned char *searchName,
2565 char *acl_inf, const int buflen, const int acl_type,
2566 const struct nls_table *nls_codepage, int remap)
2568 /* SMB_QUERY_POSIX_ACL */
2569 TRANSACTION2_QPI_REQ *pSMB = NULL;
2570 TRANSACTION2_QPI_RSP *pSMBr = NULL;
2574 __u16 params, byte_count;
2576 cFYI(1, ("In GetPosixACL (Unix) for path %s", searchName));
2579 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2584 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2586 cifsConvertToUCS((__le16 *) pSMB->FileName, searchName,
2587 PATH_MAX, nls_codepage, remap);
2588 name_len++; /* trailing null */
2590 pSMB->FileName[name_len] = 0;
2591 pSMB->FileName[name_len+1] = 0;
2592 } else { /* BB improve the check for buffer overruns BB */
2593 name_len = strnlen(searchName, PATH_MAX);
2594 name_len++; /* trailing null */
2595 strncpy(pSMB->FileName, searchName, name_len);
2598 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
2599 pSMB->TotalDataCount = 0;
2600 pSMB->MaxParameterCount = cpu_to_le16(2);
2601 /* BB find exact max data count below from sess structure BB */
2602 pSMB->MaxDataCount = cpu_to_le16(4000);
2603 pSMB->MaxSetupCount = 0;
2607 pSMB->Reserved2 = 0;
2608 pSMB->ParameterOffset = cpu_to_le16(
2609 offsetof(struct smb_com_transaction2_qpi_req ,InformationLevel) - 4);
2610 pSMB->DataCount = 0;
2611 pSMB->DataOffset = 0;
2612 pSMB->SetupCount = 1;
2613 pSMB->Reserved3 = 0;
2614 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
2615 byte_count = params + 1 /* pad */ ;
2616 pSMB->TotalParameterCount = cpu_to_le16(params);
2617 pSMB->ParameterCount = pSMB->TotalParameterCount;
2618 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_ACL);
2619 pSMB->Reserved4 = 0;
2620 pSMB->hdr.smb_buf_length += byte_count;
2621 pSMB->ByteCount = cpu_to_le16(byte_count);
2623 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2624 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2625 cifs_stats_inc(&tcon->num_acl_get);
2627 cFYI(1, ("Send error in Query POSIX ACL = %d", rc));
2629 /* decode response */
2631 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
2632 if (rc || (pSMBr->ByteCount < 2))
2633 /* BB also check enough total bytes returned */
2634 rc = -EIO; /* bad smb */
2636 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
2637 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
2638 rc = cifs_copy_posix_acl(acl_inf,
2639 (char *)&pSMBr->hdr.Protocol+data_offset,
2640 buflen,acl_type,count);
2643 cifs_buf_release(pSMB);
2650 CIFSSMBSetPosixACL(const int xid, struct cifsTconInfo *tcon,
2651 const unsigned char *fileName,
2652 const char *local_acl, const int buflen,
2654 const struct nls_table *nls_codepage, int remap)
2656 struct smb_com_transaction2_spi_req *pSMB = NULL;
2657 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
2661 int bytes_returned = 0;
2662 __u16 params, byte_count, data_count, param_offset, offset;
2664 cFYI(1, ("In SetPosixACL (Unix) for path %s", fileName));
2666 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2670 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2672 cifsConvertToUCS((__le16 *) pSMB->FileName, fileName,
2673 PATH_MAX, nls_codepage, remap);
2674 name_len++; /* trailing null */
2676 } else { /* BB improve the check for buffer overruns BB */
2677 name_len = strnlen(fileName, PATH_MAX);
2678 name_len++; /* trailing null */
2679 strncpy(pSMB->FileName, fileName, name_len);
2681 params = 6 + name_len;
2682 pSMB->MaxParameterCount = cpu_to_le16(2);
2683 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB size from sess */
2684 pSMB->MaxSetupCount = 0;
2688 pSMB->Reserved2 = 0;
2689 param_offset = offsetof(struct smb_com_transaction2_spi_req,
2690 InformationLevel) - 4;
2691 offset = param_offset + params;
2692 parm_data = ((char *) &pSMB->hdr.Protocol) + offset;
2693 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2695 /* convert to on the wire format for POSIX ACL */
2696 data_count = ACL_to_cifs_posix(parm_data,local_acl,buflen,acl_type);
2698 if(data_count == 0) {
2700 goto setACLerrorExit;
2702 pSMB->DataOffset = cpu_to_le16(offset);
2703 pSMB->SetupCount = 1;
2704 pSMB->Reserved3 = 0;
2705 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2706 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_ACL);
2707 byte_count = 3 /* pad */ + params + data_count;
2708 pSMB->DataCount = cpu_to_le16(data_count);
2709 pSMB->TotalDataCount = pSMB->DataCount;
2710 pSMB->ParameterCount = cpu_to_le16(params);
2711 pSMB->TotalParameterCount = pSMB->ParameterCount;
2712 pSMB->Reserved4 = 0;
2713 pSMB->hdr.smb_buf_length += byte_count;
2714 pSMB->ByteCount = cpu_to_le16(byte_count);
2715 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2716 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2718 cFYI(1, ("Set POSIX ACL returned %d", rc));
2722 cifs_buf_release(pSMB);
2728 /* BB fix tabs in this function FIXME BB */
2730 CIFSGetExtAttr(const int xid, struct cifsTconInfo *tcon,
2731 const int netfid, __u64 * pExtAttrBits, __u64 *pMask)
2734 struct smb_t2_qfi_req *pSMB = NULL;
2735 struct smb_t2_qfi_rsp *pSMBr = NULL;
2737 __u16 params, byte_count;
2739 cFYI(1,("In GetExtAttr"));
2744 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2749 params = 2 /* level */ +2 /* fid */;
2750 pSMB->t2.TotalDataCount = 0;
2751 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
2752 /* BB find exact max data count below from sess structure BB */
2753 pSMB->t2.MaxDataCount = cpu_to_le16(4000);
2754 pSMB->t2.MaxSetupCount = 0;
2755 pSMB->t2.Reserved = 0;
2757 pSMB->t2.Timeout = 0;
2758 pSMB->t2.Reserved2 = 0;
2759 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
2761 pSMB->t2.DataCount = 0;
2762 pSMB->t2.DataOffset = 0;
2763 pSMB->t2.SetupCount = 1;
2764 pSMB->t2.Reserved3 = 0;
2765 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
2766 byte_count = params + 1 /* pad */ ;
2767 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
2768 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
2769 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_ATTR_FLAGS);
2772 pSMB->hdr.smb_buf_length += byte_count;
2773 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
2775 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2776 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2778 cFYI(1, ("error %d in GetExtAttr", rc));
2780 /* decode response */
2781 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
2782 if (rc || (pSMBr->ByteCount < 2))
2783 /* BB also check enough total bytes returned */
2784 /* If rc should we check for EOPNOSUPP and
2785 disable the srvino flag? or in caller? */
2786 rc = -EIO; /* bad smb */
2788 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
2789 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
2790 struct file_chattr_info * pfinfo;
2791 /* BB Do we need a cast or hash here ? */
2793 cFYI(1, ("Illegal size ret in GetExtAttr"));
2797 pfinfo = (struct file_chattr_info *)
2798 (data_offset + (char *) &pSMBr->hdr.Protocol);
2799 *pExtAttrBits = le64_to_cpu(pfinfo->mode);
2800 *pMask = le64_to_cpu(pfinfo->mask);
2804 cifs_buf_release(pSMB);
2806 goto GetExtAttrRetry;
2811 #endif /* CONFIG_POSIX */
2814 /* security id for everyone */
2815 const static struct cifs_sid sid_everyone =
2816 {1, 1, {0, 0, 0, 0, 0, 0}, {0, 0, 0, 0}};
2818 const static struct cifs_sid sid_user =
2819 {1, 2 , {0, 0, 0, 0, 0, 5}, {32, 545, 0, 0}};
2821 /* Convert CIFS ACL to POSIX form */
2822 static int parse_sec_desc(struct cifs_sid * psec_desc, int acl_len)
2827 /* Get Security Descriptor (by handle) from remote server for a file or dir */
2829 CIFSSMBGetCIFSACL(const int xid, struct cifsTconInfo *tcon, __u16 fid,
2830 /* BB fix up return info */ char *acl_inf, const int buflen,
2831 const int acl_type /* ACCESS/DEFAULT not sure implication */)
2835 QUERY_SEC_DESC_REQ * pSMB;
2838 cFYI(1, ("GetCifsACL"));
2840 rc = smb_init_ntransact(NT_TRANSACT_QUERY_SECURITY_DESC, 0,
2841 8 /* parm len */, tcon, (void **) &pSMB);
2845 pSMB->MaxParameterCount = cpu_to_le32(4);
2846 /* BB TEST with big acls that might need to be e.g. larger than 16K */
2847 pSMB->MaxSetupCount = 0;
2848 pSMB->Fid = fid; /* file handle always le */
2849 pSMB->AclFlags = cpu_to_le32(CIFS_ACL_OWNER | CIFS_ACL_GROUP |
2851 pSMB->ByteCount = cpu_to_le16(11); /* 3 bytes pad + 8 bytes parm */
2852 pSMB->hdr.smb_buf_length += 11;
2853 iov[0].iov_base = (char *)pSMB;
2854 iov[0].iov_len = pSMB->hdr.smb_buf_length + 4;
2856 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovec */, &buf_type, 0);
2857 cifs_stats_inc(&tcon->num_acl_get);
2859 cFYI(1, ("Send error in QuerySecDesc = %d", rc));
2860 } else { /* decode response */
2861 struct cifs_sid * psec_desc;
2866 struct smb_com_ntransact_rsp * pSMBr;
2868 /* validate_nttransact */
2869 rc = validate_ntransact(iov[0].iov_base, (char **)&parm,
2870 (char **)&psec_desc,
2871 &parm_len, &data_len);
2875 pSMBr = (struct smb_com_ntransact_rsp *)iov[0].iov_base;
2877 cERROR(1,("smb %p parm %p data %p",pSMBr,parm,psec_desc)); /* BB removeme BB */
2879 if (le32_to_cpu(pSMBr->ParameterCount) != 4) {
2880 rc = -EIO; /* bad smb */
2884 /* BB check that data area is minimum length and as big as acl_len */
2886 acl_len = le32_to_cpu(*(__le32 *)parm);
2887 /* BB check if(acl_len > bufsize) */
2889 parse_sec_desc(psec_desc, acl_len);
2892 if(buf_type == CIFS_SMALL_BUFFER)
2893 cifs_small_buf_release(iov[0].iov_base);
2894 else if(buf_type == CIFS_LARGE_BUFFER)
2895 cifs_buf_release(iov[0].iov_base);
2896 /* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
2900 /* Legacy Query Path Information call for lookup to old servers such
2902 int SMBQueryInformation(const int xid, struct cifsTconInfo *tcon,
2903 const unsigned char *searchName,
2904 FILE_ALL_INFO * pFinfo,
2905 const struct nls_table *nls_codepage, int remap)
2907 QUERY_INFORMATION_REQ * pSMB;
2908 QUERY_INFORMATION_RSP * pSMBr;
2913 cFYI(1, ("In SMBQPath path %s", searchName));
2915 rc = smb_init(SMB_COM_QUERY_INFORMATION, 0, tcon, (void **) &pSMB,
2920 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2922 cifsConvertToUCS((__le16 *) pSMB->FileName, searchName,
2923 PATH_MAX, nls_codepage, remap);
2924 name_len++; /* trailing null */
2927 name_len = strnlen(searchName, PATH_MAX);
2928 name_len++; /* trailing null */
2929 strncpy(pSMB->FileName, searchName, name_len);
2931 pSMB->BufferFormat = 0x04;
2932 name_len++; /* account for buffer type byte */
2933 pSMB->hdr.smb_buf_length += (__u16) name_len;
2934 pSMB->ByteCount = cpu_to_le16(name_len);
2936 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2937 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2939 cFYI(1, ("Send error in QueryInfo = %d", rc));
2940 } else if (pFinfo) { /* decode response */
2942 __u32 time = le32_to_cpu(pSMBr->last_write_time);
2943 /* BB FIXME - add time zone adjustment BB */
2944 memset(pFinfo, 0, sizeof(FILE_ALL_INFO));
2947 /* decode time fields */
2948 pFinfo->ChangeTime = cpu_to_le64(cifs_UnixTimeToNT(ts));
2949 pFinfo->LastWriteTime = pFinfo->ChangeTime;
2950 pFinfo->LastAccessTime = 0;
2951 pFinfo->AllocationSize =
2952 cpu_to_le64(le32_to_cpu(pSMBr->size));
2953 pFinfo->EndOfFile = pFinfo->AllocationSize;
2954 pFinfo->Attributes =
2955 cpu_to_le32(le16_to_cpu(pSMBr->attr));
2957 rc = -EIO; /* bad buffer passed in */
2959 cifs_buf_release(pSMB);
2971 CIFSSMBQPathInfo(const int xid, struct cifsTconInfo *tcon,
2972 const unsigned char *searchName,
2973 FILE_ALL_INFO * pFindData,
2974 int legacy /* old style infolevel */,
2975 const struct nls_table *nls_codepage, int remap)
2977 /* level 263 SMB_QUERY_FILE_ALL_INFO */
2978 TRANSACTION2_QPI_REQ *pSMB = NULL;
2979 TRANSACTION2_QPI_RSP *pSMBr = NULL;
2983 __u16 params, byte_count;
2985 /* cFYI(1, ("In QPathInfo path %s", searchName)); */
2987 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2992 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2994 cifsConvertToUCS((__le16 *) pSMB->FileName, searchName,
2995 PATH_MAX, nls_codepage, remap);
2996 name_len++; /* trailing null */
2998 } else { /* BB improve the check for buffer overruns BB */
2999 name_len = strnlen(searchName, PATH_MAX);
3000 name_len++; /* trailing null */
3001 strncpy(pSMB->FileName, searchName, name_len);
3004 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */ ;
3005 pSMB->TotalDataCount = 0;
3006 pSMB->MaxParameterCount = cpu_to_le16(2);
3007 pSMB->MaxDataCount = cpu_to_le16(4000); /* BB find exact max SMB PDU from sess structure BB */
3008 pSMB->MaxSetupCount = 0;
3012 pSMB->Reserved2 = 0;
3013 pSMB->ParameterOffset = cpu_to_le16(offsetof(
3014 struct smb_com_transaction2_qpi_req ,InformationLevel) - 4);
3015 pSMB->DataCount = 0;
3016 pSMB->DataOffset = 0;
3017 pSMB->SetupCount = 1;
3018 pSMB->Reserved3 = 0;
3019 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3020 byte_count = params + 1 /* pad */ ;
3021 pSMB->TotalParameterCount = cpu_to_le16(params);
3022 pSMB->ParameterCount = pSMB->TotalParameterCount;
3024 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_STANDARD);
3026 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
3027 pSMB->Reserved4 = 0;
3028 pSMB->hdr.smb_buf_length += byte_count;
3029 pSMB->ByteCount = cpu_to_le16(byte_count);
3031 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3032 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3034 cFYI(1, ("Send error in QPathInfo = %d", rc));
3035 } else { /* decode response */
3036 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
3038 if (rc) /* BB add auto retry on EOPNOTSUPP? */
3040 else if (!legacy && (pSMBr->ByteCount < 40))
3041 rc = -EIO; /* bad smb */
3042 else if(legacy && (pSMBr->ByteCount < 24))
3043 rc = -EIO; /* 24 or 26 expected but we do not read last field */
3044 else if (pFindData){
3046 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3047 if(legacy) /* we do not read the last field, EAsize, fortunately
3048 since it varies by subdialect and on Set vs. Get, is
3049 two bytes or 4 bytes depending but we don't care here */
3050 size = sizeof(FILE_INFO_STANDARD);
3052 size = sizeof(FILE_ALL_INFO);
3053 memcpy((char *) pFindData,
3054 (char *) &pSMBr->hdr.Protocol +
3059 cifs_buf_release(pSMB);
3061 goto QPathInfoRetry;
3067 CIFSSMBUnixQPathInfo(const int xid, struct cifsTconInfo *tcon,
3068 const unsigned char *searchName,
3069 FILE_UNIX_BASIC_INFO * pFindData,
3070 const struct nls_table *nls_codepage, int remap)
3072 /* SMB_QUERY_FILE_UNIX_BASIC */
3073 TRANSACTION2_QPI_REQ *pSMB = NULL;
3074 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3076 int bytes_returned = 0;
3078 __u16 params, byte_count;
3080 cFYI(1, ("In QPathInfo (Unix) the path %s", searchName));
3082 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3087 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3089 cifsConvertToUCS((__le16 *) pSMB->FileName, searchName,
3090 PATH_MAX, nls_codepage, remap);
3091 name_len++; /* trailing null */
3093 } else { /* BB improve the check for buffer overruns BB */
3094 name_len = strnlen(searchName, PATH_MAX);
3095 name_len++; /* trailing null */
3096 strncpy(pSMB->FileName, searchName, name_len);
3099 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */ ;
3100 pSMB->TotalDataCount = 0;
3101 pSMB->MaxParameterCount = cpu_to_le16(2);
3102 /* BB find exact max SMB PDU from sess structure BB */
3103 pSMB->MaxDataCount = cpu_to_le16(4000);
3104 pSMB->MaxSetupCount = 0;
3108 pSMB->Reserved2 = 0;
3109 pSMB->ParameterOffset = cpu_to_le16(offsetof(
3110 struct smb_com_transaction2_qpi_req ,InformationLevel) - 4);
3111 pSMB->DataCount = 0;
3112 pSMB->DataOffset = 0;
3113 pSMB->SetupCount = 1;
3114 pSMB->Reserved3 = 0;
3115 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3116 byte_count = params + 1 /* pad */ ;
3117 pSMB->TotalParameterCount = cpu_to_le16(params);
3118 pSMB->ParameterCount = pSMB->TotalParameterCount;
3119 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
3120 pSMB->Reserved4 = 0;
3121 pSMB->hdr.smb_buf_length += byte_count;
3122 pSMB->ByteCount = cpu_to_le16(byte_count);
3124 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3125 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3127 cFYI(1, ("Send error in QPathInfo = %d", rc));
3128 } else { /* decode response */
3129 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
3131 if (rc || (pSMBr->ByteCount < sizeof(FILE_UNIX_BASIC_INFO))) {
3132 rc = -EIO; /* bad smb */
3134 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3135 memcpy((char *) pFindData,
3136 (char *) &pSMBr->hdr.Protocol +
3138 sizeof (FILE_UNIX_BASIC_INFO));
3141 cifs_buf_release(pSMB);
3143 goto UnixQPathInfoRetry;
3148 #if 0 /* function unused at present */
3149 int CIFSFindSingle(const int xid, struct cifsTconInfo *tcon,
3150 const char *searchName, FILE_ALL_INFO * findData,
3151 const struct nls_table *nls_codepage)
3153 /* level 257 SMB_ */
3154 TRANSACTION2_FFIRST_REQ *pSMB = NULL;
3155 TRANSACTION2_FFIRST_RSP *pSMBr = NULL;
3159 __u16 params, byte_count;
3161 cFYI(1, ("In FindUnique"));
3163 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3168 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3170 cifsConvertToUCS((__le16 *) pSMB->FileName, searchName, PATH_MAX
3171 /* find define for this maxpathcomponent */
3173 name_len++; /* trailing null */
3175 } else { /* BB improve the check for buffer overruns BB */
3176 name_len = strnlen(searchName, PATH_MAX);
3177 name_len++; /* trailing null */
3178 strncpy(pSMB->FileName, searchName, name_len);
3181 params = 12 + name_len /* includes null */ ;
3182 pSMB->TotalDataCount = 0; /* no EAs */
3183 pSMB->MaxParameterCount = cpu_to_le16(2);
3184 pSMB->MaxDataCount = cpu_to_le16(4000); /* BB find exact max SMB PDU from sess structure BB */
3185 pSMB->MaxSetupCount = 0;
3189 pSMB->Reserved2 = 0;
3190 pSMB->ParameterOffset = cpu_to_le16(
3191 offsetof(struct smb_com_transaction2_ffirst_req,InformationLevel) - 4);
3192 pSMB->DataCount = 0;
3193 pSMB->DataOffset = 0;
3194 pSMB->SetupCount = 1; /* one byte, no need to le convert */
3195 pSMB->Reserved3 = 0;
3196 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_FIRST);
3197 byte_count = params + 1 /* pad */ ;
3198 pSMB->TotalParameterCount = cpu_to_le16(params);
3199 pSMB->ParameterCount = pSMB->TotalParameterCount;
3200 pSMB->SearchAttributes =
3201 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
3203 pSMB->SearchCount = cpu_to_le16(16); /* BB increase */
3204 pSMB->SearchFlags = cpu_to_le16(1);
3205 pSMB->InformationLevel = cpu_to_le16(SMB_FIND_FILE_DIRECTORY_INFO);
3206 pSMB->SearchStorageType = 0; /* BB what should we set this to? BB */
3207 pSMB->hdr.smb_buf_length += byte_count;
3208 pSMB->ByteCount = cpu_to_le16(byte_count);
3210 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3211 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3214 cFYI(1, ("Send error in FindFileDirInfo = %d", rc));
3215 } else { /* decode response */
3216 cifs_stats_inc(&tcon->num_ffirst);
3220 cifs_buf_release(pSMB);
3222 goto findUniqueRetry;
3226 #endif /* end unused (temporarily) function */
3228 /* xid, tcon, searchName and codepage are input parms, rest are returned */
3230 CIFSFindFirst(const int xid, struct cifsTconInfo *tcon,
3231 const char *searchName,
3232 const struct nls_table *nls_codepage,
3234 struct cifs_search_info * psrch_inf, int remap, const char dirsep)
3236 /* level 257 SMB_ */
3237 TRANSACTION2_FFIRST_REQ *pSMB = NULL;
3238 TRANSACTION2_FFIRST_RSP *pSMBr = NULL;
3239 T2_FFIRST_RSP_PARMS * parms;
3241 int bytes_returned = 0;
3243 __u16 params, byte_count;
3245 cFYI(1, ("In FindFirst for %s",searchName));
3248 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3253 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3255 cifsConvertToUCS((__le16 *) pSMB->FileName,searchName,
3256 PATH_MAX, nls_codepage, remap);
3257 /* We can not add the asterik earlier in case
3258 it got remapped to 0xF03A as if it were part of the
3259 directory name instead of a wildcard */
3261 pSMB->FileName[name_len] = dirsep;
3262 pSMB->FileName[name_len+1] = 0;
3263 pSMB->FileName[name_len+2] = '*';
3264 pSMB->FileName[name_len+3] = 0;
3265 name_len += 4; /* now the trailing null */
3266 pSMB->FileName[name_len] = 0; /* null terminate just in case */
3267 pSMB->FileName[name_len+1] = 0;
3269 } else { /* BB add check for overrun of SMB buf BB */
3270 name_len = strnlen(searchName, PATH_MAX);
3271 /* BB fix here and in unicode clause above ie
3272 if(name_len > buffersize-header)
3273 free buffer exit; BB */
3274 strncpy(pSMB->FileName, searchName, name_len);
3275 pSMB->FileName[name_len] = dirsep;
3276 pSMB->FileName[name_len+1] = '*';
3277 pSMB->FileName[name_len+2] = 0;
3281 params = 12 + name_len /* includes null */ ;
3282 pSMB->TotalDataCount = 0; /* no EAs */
3283 pSMB->MaxParameterCount = cpu_to_le16(10);
3284 pSMB->MaxDataCount = cpu_to_le16((tcon->ses->server->maxBuf -
3285 MAX_CIFS_HDR_SIZE) & 0xFFFFFF00);
3286 pSMB->MaxSetupCount = 0;
3290 pSMB->Reserved2 = 0;
3291 byte_count = params + 1 /* pad */ ;
3292 pSMB->TotalParameterCount = cpu_to_le16(params);
3293 pSMB->ParameterCount = pSMB->TotalParameterCount;
3294 pSMB->ParameterOffset = cpu_to_le16(
3295 offsetof(struct smb_com_transaction2_ffirst_req, SearchAttributes)
3297 pSMB->DataCount = 0;
3298 pSMB->DataOffset = 0;
3299 pSMB->SetupCount = 1; /* one byte, no need to make endian neutral */
3300 pSMB->Reserved3 = 0;
3301 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_FIRST);
3302 pSMB->SearchAttributes =
3303 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
3305 pSMB->SearchCount= cpu_to_le16(CIFSMaxBufSize/sizeof(FILE_UNIX_INFO));
3306 pSMB->SearchFlags = cpu_to_le16(CIFS_SEARCH_CLOSE_AT_END |
3307 CIFS_SEARCH_RETURN_RESUME);
3308 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
3310 /* BB what should we set StorageType to? Does it matter? BB */
3311 pSMB->SearchStorageType = 0;
3312 pSMB->hdr.smb_buf_length += byte_count;
3313 pSMB->ByteCount = cpu_to_le16(byte_count);
3315 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3316 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3317 cifs_stats_inc(&tcon->num_ffirst);
3319 if (rc) {/* BB add logic to retry regular search if Unix search
3320 rejected unexpectedly by server */
3321 /* BB Add code to handle unsupported level rc */
3322 cFYI(1, ("Error in FindFirst = %d", rc));
3324 cifs_buf_release(pSMB);
3326 /* BB eventually could optimize out free and realloc of buf */
3329 goto findFirstRetry;
3330 } else { /* decode response */
3331 /* BB remember to free buffer if error BB */
3332 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
3334 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3335 psrch_inf->unicode = TRUE;
3337 psrch_inf->unicode = FALSE;
3339 psrch_inf->ntwrk_buf_start = (char *)pSMBr;
3340 psrch_inf->smallBuf = 0;
3341 psrch_inf->srch_entries_start =
3342 (char *) &pSMBr->hdr.Protocol +
3343 le16_to_cpu(pSMBr->t2.DataOffset);
3344 parms = (T2_FFIRST_RSP_PARMS *)((char *) &pSMBr->hdr.Protocol +
3345 le16_to_cpu(pSMBr->t2.ParameterOffset));
3347 if(parms->EndofSearch)
3348 psrch_inf->endOfSearch = TRUE;
3350 psrch_inf->endOfSearch = FALSE;
3352 psrch_inf->entries_in_buffer = le16_to_cpu(parms->SearchCount);
3353 psrch_inf->index_of_last_entry = 2 /* skip . and .. */ +
3354 psrch_inf->entries_in_buffer;
3355 *pnetfid = parms->SearchHandle;
3357 cifs_buf_release(pSMB);
3364 int CIFSFindNext(const int xid, struct cifsTconInfo *tcon,
3365 __u16 searchHandle, struct cifs_search_info * psrch_inf)
3367 TRANSACTION2_FNEXT_REQ *pSMB = NULL;
3368 TRANSACTION2_FNEXT_RSP *pSMBr = NULL;
3369 T2_FNEXT_RSP_PARMS * parms;
3370 char *response_data;
3372 int bytes_returned, name_len;
3373 __u16 params, byte_count;
3375 cFYI(1, ("In FindNext"));
3377 if(psrch_inf->endOfSearch == TRUE)
3380 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3385 params = 14; /* includes 2 bytes of null string, converted to LE below */
3387 pSMB->TotalDataCount = 0; /* no EAs */
3388 pSMB->MaxParameterCount = cpu_to_le16(8);
3389 pSMB->MaxDataCount =
3390 cpu_to_le16((tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE) & 0xFFFFFF00);
3391 pSMB->MaxSetupCount = 0;
3395 pSMB->Reserved2 = 0;
3396 pSMB->ParameterOffset = cpu_to_le16(
3397 offsetof(struct smb_com_transaction2_fnext_req,SearchHandle) - 4);
3398 pSMB->DataCount = 0;
3399 pSMB->DataOffset = 0;
3400 pSMB->SetupCount = 1;
3401 pSMB->Reserved3 = 0;
3402 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_NEXT);
3403 pSMB->SearchHandle = searchHandle; /* always kept as le */
3405 cpu_to_le16(CIFSMaxBufSize / sizeof (FILE_UNIX_INFO));
3406 /* test for Unix extensions */
3407 /* if (tcon->ses->capabilities & CAP_UNIX) {
3408 pSMB->InformationLevel = cpu_to_le16(SMB_FIND_FILE_UNIX);
3409 psrch_inf->info_level = SMB_FIND_FILE_UNIX;
3411 pSMB->InformationLevel =
3412 cpu_to_le16(SMB_FIND_FILE_DIRECTORY_INFO);
3413 psrch_inf->info_level = SMB_FIND_FILE_DIRECTORY_INFO;
3415 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
3416 pSMB->ResumeKey = psrch_inf->resume_key;
3418 cpu_to_le16(CIFS_SEARCH_CLOSE_AT_END | CIFS_SEARCH_RETURN_RESUME);
3420 name_len = psrch_inf->resume_name_len;
3422 if(name_len < PATH_MAX) {
3423 memcpy(pSMB->ResumeFileName, psrch_inf->presume_name, name_len);
3424 byte_count += name_len;
3425 /* 14 byte parm len above enough for 2 byte null terminator */
3426 pSMB->ResumeFileName[name_len] = 0;
3427 pSMB->ResumeFileName[name_len+1] = 0;
3430 goto FNext2_err_exit;
3432 byte_count = params + 1 /* pad */ ;
3433 pSMB->TotalParameterCount = cpu_to_le16(params);
3434 pSMB->ParameterCount = pSMB->TotalParameterCount;
3435 pSMB->hdr.smb_buf_length += byte_count;
3436 pSMB->ByteCount = cpu_to_le16(byte_count);
3438 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3439 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3440 cifs_stats_inc(&tcon->num_fnext);
3443 psrch_inf->endOfSearch = TRUE;
3444 rc = 0; /* search probably was closed at end of search above */
3446 cFYI(1, ("FindNext returned = %d", rc));
3447 } else { /* decode response */
3448 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
3451 /* BB fixme add lock for file (srch_info) struct here */
3452 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3453 psrch_inf->unicode = TRUE;
3455 psrch_inf->unicode = FALSE;
3456 response_data = (char *) &pSMBr->hdr.Protocol +
3457 le16_to_cpu(pSMBr->t2.ParameterOffset);
3458 parms = (T2_FNEXT_RSP_PARMS *)response_data;
3459 response_data = (char *)&pSMBr->hdr.Protocol +
3460 le16_to_cpu(pSMBr->t2.DataOffset);
3461 if(psrch_inf->smallBuf)
3462 cifs_small_buf_release(
3463 psrch_inf->ntwrk_buf_start);
3465 cifs_buf_release(psrch_inf->ntwrk_buf_start);
3466 psrch_inf->srch_entries_start = response_data;
3467 psrch_inf->ntwrk_buf_start = (char *)pSMB;
3468 psrch_inf->smallBuf = 0;
3469 if(parms->EndofSearch)
3470 psrch_inf->endOfSearch = TRUE;
3472 psrch_inf->endOfSearch = FALSE;
3474 psrch_inf->entries_in_buffer = le16_to_cpu(parms->SearchCount);
3475 psrch_inf->index_of_last_entry +=
3476 psrch_inf->entries_in_buffer;
3477 /* cFYI(1,("fnxt2 entries in buf %d index_of_last %d",psrch_inf->entries_in_buffer,psrch_inf->index_of_last_entry)); */
3479 /* BB fixme add unlock here */
3484 /* BB On error, should we leave previous search buf (and count and
3485 last entry fields) intact or free the previous one? */
3487 /* Note: On -EAGAIN error only caller can retry on handle based calls
3488 since file handle passed in no longer valid */
3491 cifs_buf_release(pSMB);
3497 CIFSFindClose(const int xid, struct cifsTconInfo *tcon, const __u16 searchHandle)
3500 FINDCLOSE_REQ *pSMB = NULL;
3501 CLOSE_RSP *pSMBr = NULL; /* BB removeme BB */
3504 cFYI(1, ("In CIFSSMBFindClose"));
3505 rc = small_smb_init(SMB_COM_FIND_CLOSE2, 1, tcon, (void **)&pSMB);
3507 /* no sense returning error if session restarted
3508 as file handle has been closed */
3514 pSMBr = (CLOSE_RSP *)pSMB; /* BB removeme BB */
3515 pSMB->FileID = searchHandle;
3516 pSMB->ByteCount = 0;
3517 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3518 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3520 cERROR(1, ("Send error in FindClose = %d", rc));
3522 cifs_stats_inc(&tcon->num_fclose);
3523 cifs_small_buf_release(pSMB);
3525 /* Since session is dead, search handle closed on server already */
3533 CIFSGetSrvInodeNumber(const int xid, struct cifsTconInfo *tcon,
3534 const unsigned char *searchName,
3535 __u64 * inode_number,
3536 const struct nls_table *nls_codepage, int remap)
3539 TRANSACTION2_QPI_REQ *pSMB = NULL;
3540 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3541 int name_len, bytes_returned;
3542 __u16 params, byte_count;
3544 cFYI(1,("In GetSrvInodeNum for %s",searchName));
3548 GetInodeNumberRetry:
3549 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3555 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3557 cifsConvertToUCS((__le16 *) pSMB->FileName, searchName,
3558 PATH_MAX,nls_codepage, remap);
3559 name_len++; /* trailing null */
3561 } else { /* BB improve the check for buffer overruns BB */
3562 name_len = strnlen(searchName, PATH_MAX);
3563 name_len++; /* trailing null */
3564 strncpy(pSMB->FileName, searchName, name_len);
3567 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3568 pSMB->TotalDataCount = 0;
3569 pSMB->MaxParameterCount = cpu_to_le16(2);
3570 /* BB find exact max data count below from sess structure BB */
3571 pSMB->MaxDataCount = cpu_to_le16(4000);
3572 pSMB->MaxSetupCount = 0;
3576 pSMB->Reserved2 = 0;
3577 pSMB->ParameterOffset = cpu_to_le16(offsetof(
3578 struct smb_com_transaction2_qpi_req ,InformationLevel) - 4);
3579 pSMB->DataCount = 0;
3580 pSMB->DataOffset = 0;
3581 pSMB->SetupCount = 1;
3582 pSMB->Reserved3 = 0;
3583 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3584 byte_count = params + 1 /* pad */ ;
3585 pSMB->TotalParameterCount = cpu_to_le16(params);
3586 pSMB->ParameterCount = pSMB->TotalParameterCount;
3587 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_INTERNAL_INFO);
3588 pSMB->Reserved4 = 0;
3589 pSMB->hdr.smb_buf_length += byte_count;
3590 pSMB->ByteCount = cpu_to_le16(byte_count);
3592 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3593 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3595 cFYI(1, ("error %d in QueryInternalInfo", rc));
3597 /* decode response */
3598 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
3599 if (rc || (pSMBr->ByteCount < 2))
3600 /* BB also check enough total bytes returned */
3601 /* If rc should we check for EOPNOSUPP and
3602 disable the srvino flag? or in caller? */
3603 rc = -EIO; /* bad smb */
3605 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3606 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3607 struct file_internal_info * pfinfo;
3608 /* BB Do we need a cast or hash here ? */
3610 cFYI(1, ("Illegal size ret in QryIntrnlInf"));
3612 goto GetInodeNumOut;
3614 pfinfo = (struct file_internal_info *)
3615 (data_offset + (char *) &pSMBr->hdr.Protocol);
3616 *inode_number = pfinfo->UniqueId;
3620 cifs_buf_release(pSMB);
3622 goto GetInodeNumberRetry;
3627 CIFSGetDFSRefer(const int xid, struct cifsSesInfo *ses,
3628 const unsigned char *searchName,
3629 unsigned char **targetUNCs,
3630 unsigned int *number_of_UNC_in_array,
3631 const struct nls_table *nls_codepage, int remap)
3633 /* TRANS2_GET_DFS_REFERRAL */
3634 TRANSACTION2_GET_DFS_REFER_REQ *pSMB = NULL;
3635 TRANSACTION2_GET_DFS_REFER_RSP *pSMBr = NULL;
3636 struct dfs_referral_level_3 * referrals = NULL;
3642 __u16 params, byte_count;
3643 *number_of_UNC_in_array = 0;
3646 cFYI(1, ("In GetDFSRefer the path %s", searchName));
3650 rc = smb_init(SMB_COM_TRANSACTION2, 15, NULL, (void **) &pSMB,
3655 /* server pointer checked in called function,
3656 but should never be null here anyway */
3657 pSMB->hdr.Mid = GetNextMid(ses->server);
3658 pSMB->hdr.Tid = ses->ipc_tid;
3659 pSMB->hdr.Uid = ses->Suid;
3660 if (ses->capabilities & CAP_STATUS32) {
3661 pSMB->hdr.Flags2 |= SMBFLG2_ERR_STATUS;
3663 if (ses->capabilities & CAP_DFS) {
3664 pSMB->hdr.Flags2 |= SMBFLG2_DFS;
3667 if (ses->capabilities & CAP_UNICODE) {
3668 pSMB->hdr.Flags2 |= SMBFLG2_UNICODE;
3670 cifsConvertToUCS((__le16 *) pSMB->RequestFileName,
3671 searchName, PATH_MAX, nls_codepage, remap);
3672 name_len++; /* trailing null */
3674 } else { /* BB improve the check for buffer overruns BB */
3675 name_len = strnlen(searchName, PATH_MAX);
3676 name_len++; /* trailing null */
3677 strncpy(pSMB->RequestFileName, searchName, name_len);
3681 if(ses->server->secMode &
3682 (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
3683 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
3686 pSMB->hdr.Uid = ses->Suid;
3688 params = 2 /* level */ + name_len /*includes null */ ;
3689 pSMB->TotalDataCount = 0;
3690 pSMB->DataCount = 0;
3691 pSMB->DataOffset = 0;
3692 pSMB->MaxParameterCount = 0;
3693 pSMB->MaxDataCount = cpu_to_le16(4000); /* BB find exact max SMB PDU from sess structure BB */
3694 pSMB->MaxSetupCount = 0;
3698 pSMB->Reserved2 = 0;
3699 pSMB->ParameterOffset = cpu_to_le16(offsetof(
3700 struct smb_com_transaction2_get_dfs_refer_req, MaxReferralLevel) - 4);
3701 pSMB->SetupCount = 1;
3702 pSMB->Reserved3 = 0;
3703 pSMB->SubCommand = cpu_to_le16(TRANS2_GET_DFS_REFERRAL);
3704 byte_count = params + 3 /* pad */ ;
3705 pSMB->ParameterCount = cpu_to_le16(params);
3706 pSMB->TotalParameterCount = pSMB->ParameterCount;
3707 pSMB->MaxReferralLevel = cpu_to_le16(3);
3708 pSMB->hdr.smb_buf_length += byte_count;
3709 pSMB->ByteCount = cpu_to_le16(byte_count);
3711 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
3712 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3714 cFYI(1, ("Send error in GetDFSRefer = %d", rc));
3715 } else { /* decode response */
3716 /* BB Add logic to parse referrals here */
3717 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
3719 if (rc || (pSMBr->ByteCount < 17)) /* BB also check enough total bytes returned */
3720 rc = -EIO; /* bad smb */
3722 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3723 __u16 data_count = le16_to_cpu(pSMBr->t2.DataCount);
3726 ("Decoding GetDFSRefer response. BCC: %d Offset %d",
3727 pSMBr->ByteCount, data_offset));
3729 (struct dfs_referral_level_3 *)
3730 (8 /* sizeof start of data block */ +
3732 (char *) &pSMBr->hdr.Protocol);
3733 cFYI(1,("num_referrals: %d dfs flags: 0x%x ... \nfor referral one refer size: 0x%x srv type: 0x%x refer flags: 0x%x ttl: 0x%x",
3734 le16_to_cpu(pSMBr->NumberOfReferrals),le16_to_cpu(pSMBr->DFSFlags), le16_to_cpu(referrals->ReferralSize),le16_to_cpu(referrals->ServerType),le16_to_cpu(referrals->ReferralFlags),le16_to_cpu(referrals->TimeToLive)));
3735 /* BB This field is actually two bytes in from start of
3736 data block so we could do safety check that DataBlock
3737 begins at address of pSMBr->NumberOfReferrals */
3738 *number_of_UNC_in_array = le16_to_cpu(pSMBr->NumberOfReferrals);
3740 /* BB Fix below so can return more than one referral */
3741 if(*number_of_UNC_in_array > 1)
3742 *number_of_UNC_in_array = 1;
3744 /* get the length of the strings describing refs */
3746 for(i=0;i<*number_of_UNC_in_array;i++) {
3747 /* make sure that DfsPathOffset not past end */
3748 __u16 offset = le16_to_cpu(referrals->DfsPathOffset);
3749 if (offset > data_count) {
3750 /* if invalid referral, stop here and do
3751 not try to copy any more */
3752 *number_of_UNC_in_array = i;
3755 temp = ((char *)referrals) + offset;
3757 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE) {
3758 name_len += UniStrnlen((wchar_t *)temp,data_count);
3760 name_len += strnlen(temp,data_count);
3763 /* BB add check that referral pointer does not fall off end PDU */
3766 /* BB add check for name_len bigger than bcc */
3768 kmalloc(name_len+1+ (*number_of_UNC_in_array),GFP_KERNEL);
3769 if(*targetUNCs == NULL) {
3773 /* copy the ref strings */
3775 (struct dfs_referral_level_3 *)
3776 (8 /* sizeof data hdr */ +
3778 (char *) &pSMBr->hdr.Protocol);
3780 for(i=0;i<*number_of_UNC_in_array;i++) {
3781 temp = ((char *)referrals) + le16_to_cpu(referrals->DfsPathOffset);
3782 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE) {
3783 cifs_strfromUCS_le(*targetUNCs,
3784 (__le16 *) temp, name_len, nls_codepage);
3786 strncpy(*targetUNCs,temp,name_len);
3788 /* BB update target_uncs pointers */
3798 cifs_buf_release(pSMB);
3806 /* Query File System Info such as free space to old servers such as Win 9x */
3808 SMBOldQFSInfo(const int xid, struct cifsTconInfo *tcon, struct kstatfs *FSData)
3810 /* level 0x01 SMB_QUERY_FILE_SYSTEM_INFO */
3811 TRANSACTION2_QFSI_REQ *pSMB = NULL;
3812 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
3813 FILE_SYSTEM_ALLOC_INFO *response_data;
3815 int bytes_returned = 0;
3816 __u16 params, byte_count;
3818 cFYI(1, ("OldQFSInfo"));
3820 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3824 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3829 params = 2; /* level */
3830 pSMB->TotalDataCount = 0;
3831 pSMB->MaxParameterCount = cpu_to_le16(2);
3832 pSMB->MaxDataCount = cpu_to_le16(1000);
3833 pSMB->MaxSetupCount = 0;
3837 pSMB->Reserved2 = 0;
3838 byte_count = params + 1 /* pad */ ;
3839 pSMB->TotalParameterCount = cpu_to_le16(params);
3840 pSMB->ParameterCount = pSMB->TotalParameterCount;
3841 pSMB->ParameterOffset = cpu_to_le16(offsetof(
3842 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
3843 pSMB->DataCount = 0;
3844 pSMB->DataOffset = 0;
3845 pSMB->SetupCount = 1;
3846 pSMB->Reserved3 = 0;
3847 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
3848 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_ALLOCATION);
3849 pSMB->hdr.smb_buf_length += byte_count;
3850 pSMB->ByteCount = cpu_to_le16(byte_count);
3852 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3853 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3855 cFYI(1, ("Send error in QFSInfo = %d", rc));
3856 } else { /* decode response */
3857 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
3859 if (rc || (pSMBr->ByteCount < 18))
3860 rc = -EIO; /* bad smb */
3862 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3863 cFYI(1,("qfsinf resp BCC: %d Offset %d",
3864 pSMBr->ByteCount, data_offset));
3867 (FILE_SYSTEM_ALLOC_INFO *)
3868 (((char *) &pSMBr->hdr.Protocol) + data_offset);
3870 le16_to_cpu(response_data->BytesPerSector) *
3871 le32_to_cpu(response_data->
3872 SectorsPerAllocationUnit);
3874 le32_to_cpu(response_data->TotalAllocationUnits);
3875 FSData->f_bfree = FSData->f_bavail =
3876 le32_to_cpu(response_data->FreeAllocationUnits);
3878 ("Blocks: %lld Free: %lld Block size %ld",
3879 (unsigned long long)FSData->f_blocks,
3880 (unsigned long long)FSData->f_bfree,
3884 cifs_buf_release(pSMB);
3887 goto oldQFSInfoRetry;
3893 CIFSSMBQFSInfo(const int xid, struct cifsTconInfo *tcon, struct kstatfs *FSData)
3895 /* level 0x103 SMB_QUERY_FILE_SYSTEM_INFO */
3896 TRANSACTION2_QFSI_REQ *pSMB = NULL;
3897 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
3898 FILE_SYSTEM_INFO *response_data;
3900 int bytes_returned = 0;
3901 __u16 params, byte_count;
3903 cFYI(1, ("In QFSInfo"));
3905 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3910 params = 2; /* level */
3911 pSMB->TotalDataCount = 0;
3912 pSMB->MaxParameterCount = cpu_to_le16(2);
3913 pSMB->MaxDataCount = cpu_to_le16(1000);
3914 pSMB->MaxSetupCount = 0;
3918 pSMB->Reserved2 = 0;
3919 byte_count = params + 1 /* pad */ ;
3920 pSMB->TotalParameterCount = cpu_to_le16(params);
3921 pSMB->ParameterCount = pSMB->TotalParameterCount;
3922 pSMB->ParameterOffset = cpu_to_le16(offsetof(
3923 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
3924 pSMB->DataCount = 0;
3925 pSMB->DataOffset = 0;
3926 pSMB->SetupCount = 1;
3927 pSMB->Reserved3 = 0;
3928 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
3929 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_SIZE_INFO);
3930 pSMB->hdr.smb_buf_length += byte_count;
3931 pSMB->ByteCount = cpu_to_le16(byte_count);
3933 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3934 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3936 cFYI(1, ("Send error in QFSInfo = %d", rc));
3937 } else { /* decode response */
3938 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
3940 if (rc || (pSMBr->ByteCount < 24))
3941 rc = -EIO; /* bad smb */
3943 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3947 *) (((char *) &pSMBr->hdr.Protocol) +
3950 le32_to_cpu(response_data->BytesPerSector) *
3951 le32_to_cpu(response_data->
3952 SectorsPerAllocationUnit);
3954 le64_to_cpu(response_data->TotalAllocationUnits);
3955 FSData->f_bfree = FSData->f_bavail =
3956 le64_to_cpu(response_data->FreeAllocationUnits);
3958 ("Blocks: %lld Free: %lld Block size %ld",
3959 (unsigned long long)FSData->f_blocks,
3960 (unsigned long long)FSData->f_bfree,
3964 cifs_buf_release(pSMB);
3973 CIFSSMBQFSAttributeInfo(const int xid, struct cifsTconInfo *tcon)
3975 /* level 0x105 SMB_QUERY_FILE_SYSTEM_INFO */
3976 TRANSACTION2_QFSI_REQ *pSMB = NULL;
3977 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
3978 FILE_SYSTEM_ATTRIBUTE_INFO *response_data;
3980 int bytes_returned = 0;
3981 __u16 params, byte_count;
3983 cFYI(1, ("In QFSAttributeInfo"));
3985 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3990 params = 2; /* level */
3991 pSMB->TotalDataCount = 0;
3992 pSMB->MaxParameterCount = cpu_to_le16(2);
3993 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find exact max SMB PDU from sess structure BB */
3994 pSMB->MaxSetupCount = 0;
3998 pSMB->Reserved2 = 0;
3999 byte_count = params + 1 /* pad */ ;
4000 pSMB->TotalParameterCount = cpu_to_le16(params);
4001 pSMB->ParameterCount = pSMB->TotalParameterCount;
4002 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4003 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
4004 pSMB->DataCount = 0;
4005 pSMB->DataOffset = 0;
4006 pSMB->SetupCount = 1;
4007 pSMB->Reserved3 = 0;
4008 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
4009 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_ATTRIBUTE_INFO);
4010 pSMB->hdr.smb_buf_length += byte_count;
4011 pSMB->ByteCount = cpu_to_le16(byte_count);
4013 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4014 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4016 cERROR(1, ("Send error in QFSAttributeInfo = %d", rc));
4017 } else { /* decode response */
4018 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4020 if (rc || (pSMBr->ByteCount < 13)) { /* BB also check enough bytes returned */
4021 rc = -EIO; /* bad smb */
4023 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4025 (FILE_SYSTEM_ATTRIBUTE_INFO
4026 *) (((char *) &pSMBr->hdr.Protocol) +
4028 memcpy(&tcon->fsAttrInfo, response_data,
4029 sizeof (FILE_SYSTEM_ATTRIBUTE_INFO));
4032 cifs_buf_release(pSMB);
4035 goto QFSAttributeRetry;
4041 CIFSSMBQFSDeviceInfo(const int xid, struct cifsTconInfo *tcon)
4043 /* level 0x104 SMB_QUERY_FILE_SYSTEM_INFO */
4044 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4045 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4046 FILE_SYSTEM_DEVICE_INFO *response_data;
4048 int bytes_returned = 0;
4049 __u16 params, byte_count;
4051 cFYI(1, ("In QFSDeviceInfo"));
4053 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4058 params = 2; /* level */
4059 pSMB->TotalDataCount = 0;
4060 pSMB->MaxParameterCount = cpu_to_le16(2);
4061 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find exact max SMB PDU from sess structure BB */
4062 pSMB->MaxSetupCount = 0;
4066 pSMB->Reserved2 = 0;
4067 byte_count = params + 1 /* pad */ ;
4068 pSMB->TotalParameterCount = cpu_to_le16(params);
4069 pSMB->ParameterCount = pSMB->TotalParameterCount;
4070 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4071 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
4073 pSMB->DataCount = 0;
4074 pSMB->DataOffset = 0;
4075 pSMB->SetupCount = 1;
4076 pSMB->Reserved3 = 0;
4077 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
4078 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_DEVICE_INFO);
4079 pSMB->hdr.smb_buf_length += byte_count;
4080 pSMB->ByteCount = cpu_to_le16(byte_count);
4082 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4083 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4085 cFYI(1, ("Send error in QFSDeviceInfo = %d", rc));
4086 } else { /* decode response */
4087 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4089 if (rc || (pSMBr->ByteCount < sizeof (FILE_SYSTEM_DEVICE_INFO)))
4090 rc = -EIO; /* bad smb */
4092 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4094 (FILE_SYSTEM_DEVICE_INFO *)
4095 (((char *) &pSMBr->hdr.Protocol) +
4097 memcpy(&tcon->fsDevInfo, response_data,
4098 sizeof (FILE_SYSTEM_DEVICE_INFO));
4101 cifs_buf_release(pSMB);
4104 goto QFSDeviceRetry;
4110 CIFSSMBQFSUnixInfo(const int xid, struct cifsTconInfo *tcon)
4112 /* level 0x200 SMB_QUERY_CIFS_UNIX_INFO */
4113 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4114 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4115 FILE_SYSTEM_UNIX_INFO *response_data;
4117 int bytes_returned = 0;
4118 __u16 params, byte_count;
4120 cFYI(1, ("In QFSUnixInfo"));
4122 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4127 params = 2; /* level */
4128 pSMB->TotalDataCount = 0;
4129 pSMB->DataCount = 0;
4130 pSMB->DataOffset = 0;
4131 pSMB->MaxParameterCount = cpu_to_le16(2);
4132 pSMB->MaxDataCount = cpu_to_le16(100); /* BB find exact max SMB PDU from sess structure BB */
4133 pSMB->MaxSetupCount = 0;
4137 pSMB->Reserved2 = 0;
4138 byte_count = params + 1 /* pad */ ;
4139 pSMB->ParameterCount = cpu_to_le16(params);
4140 pSMB->TotalParameterCount = pSMB->ParameterCount;
4141 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
4142 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
4143 pSMB->SetupCount = 1;
4144 pSMB->Reserved3 = 0;
4145 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
4146 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_CIFS_UNIX_INFO);
4147 pSMB->hdr.smb_buf_length += byte_count;
4148 pSMB->ByteCount = cpu_to_le16(byte_count);
4150 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4151 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4153 cERROR(1, ("Send error in QFSUnixInfo = %d", rc));
4154 } else { /* decode response */
4155 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4157 if (rc || (pSMBr->ByteCount < 13)) {
4158 rc = -EIO; /* bad smb */
4160 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4162 (FILE_SYSTEM_UNIX_INFO
4163 *) (((char *) &pSMBr->hdr.Protocol) +
4165 memcpy(&tcon->fsUnixInfo, response_data,
4166 sizeof (FILE_SYSTEM_UNIX_INFO));
4169 cifs_buf_release(pSMB);
4179 CIFSSMBSetFSUnixInfo(const int xid, struct cifsTconInfo *tcon, __u64 cap)
4181 /* level 0x200 SMB_SET_CIFS_UNIX_INFO */
4182 TRANSACTION2_SETFSI_REQ *pSMB = NULL;
4183 TRANSACTION2_SETFSI_RSP *pSMBr = NULL;
4185 int bytes_returned = 0;
4186 __u16 params, param_offset, offset, byte_count;
4188 cFYI(1, ("In SETFSUnixInfo"));
4190 /* BB switch to small buf init to save memory */
4191 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4196 params = 4; /* 2 bytes zero followed by info level. */
4197 pSMB->MaxSetupCount = 0;
4201 pSMB->Reserved2 = 0;
4202 param_offset = offsetof(struct smb_com_transaction2_setfsi_req, FileNum) - 4;
4203 offset = param_offset + params;
4205 pSMB->MaxParameterCount = cpu_to_le16(4);
4206 pSMB->MaxDataCount = cpu_to_le16(100); /* BB find exact max SMB PDU from sess structure BB */
4207 pSMB->SetupCount = 1;
4208 pSMB->Reserved3 = 0;
4209 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FS_INFORMATION);
4210 byte_count = 1 /* pad */ + params + 12;
4212 pSMB->DataCount = cpu_to_le16(12);
4213 pSMB->ParameterCount = cpu_to_le16(params);
4214 pSMB->TotalDataCount = pSMB->DataCount;
4215 pSMB->TotalParameterCount = pSMB->ParameterCount;
4216 pSMB->ParameterOffset = cpu_to_le16(param_offset);
4217 pSMB->DataOffset = cpu_to_le16(offset);
4221 pSMB->InformationLevel = cpu_to_le16(SMB_SET_CIFS_UNIX_INFO);
4224 pSMB->ClientUnixMajor = cpu_to_le16(CIFS_UNIX_MAJOR_VERSION);
4225 pSMB->ClientUnixMinor = cpu_to_le16(CIFS_UNIX_MINOR_VERSION);
4226 pSMB->ClientUnixCap = cpu_to_le64(cap);
4228 pSMB->hdr.smb_buf_length += byte_count;
4229 pSMB->ByteCount = cpu_to_le16(byte_count);
4231 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4232 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4234 cERROR(1, ("Send error in SETFSUnixInfo = %d", rc));
4235 } else { /* decode response */
4236 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4238 rc = -EIO; /* bad smb */
4241 cifs_buf_release(pSMB);
4244 goto SETFSUnixRetry;
4252 CIFSSMBQFSPosixInfo(const int xid, struct cifsTconInfo *tcon,
4253 struct kstatfs *FSData)
4255 /* level 0x201 SMB_QUERY_CIFS_POSIX_INFO */
4256 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4257 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4258 FILE_SYSTEM_POSIX_INFO *response_data;
4260 int bytes_returned = 0;
4261 __u16 params, byte_count;
4263 cFYI(1, ("In QFSPosixInfo"));
4265 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4270 params = 2; /* level */
4271 pSMB->TotalDataCount = 0;
4272 pSMB->DataCount = 0;
4273 pSMB->DataOffset = 0;
4274 pSMB->MaxParameterCount = cpu_to_le16(2);
4275 pSMB->MaxDataCount = cpu_to_le16(100); /* BB find exact max SMB PDU from sess structure BB */
4276 pSMB->MaxSetupCount = 0;
4280 pSMB->Reserved2 = 0;
4281 byte_count = params + 1 /* pad */ ;
4282 pSMB->ParameterCount = cpu_to_le16(params);
4283 pSMB->TotalParameterCount = pSMB->ParameterCount;
4284 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
4285 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
4286 pSMB->SetupCount = 1;
4287 pSMB->Reserved3 = 0;
4288 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
4289 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_FS_INFO);
4290 pSMB->hdr.smb_buf_length += byte_count;
4291 pSMB->ByteCount = cpu_to_le16(byte_count);
4293 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4294 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4296 cFYI(1, ("Send error in QFSUnixInfo = %d", rc));
4297 } else { /* decode response */
4298 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4300 if (rc || (pSMBr->ByteCount < 13)) {
4301 rc = -EIO; /* bad smb */
4303 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4305 (FILE_SYSTEM_POSIX_INFO
4306 *) (((char *) &pSMBr->hdr.Protocol) +
4309 le32_to_cpu(response_data->BlockSize);
4311 le64_to_cpu(response_data->TotalBlocks);
4313 le64_to_cpu(response_data->BlocksAvail);
4314 if(response_data->UserBlocksAvail == cpu_to_le64(-1)) {
4315 FSData->f_bavail = FSData->f_bfree;
4318 le64_to_cpu(response_data->UserBlocksAvail);
4320 if(response_data->TotalFileNodes != cpu_to_le64(-1))
4322 le64_to_cpu(response_data->TotalFileNodes);
4323 if(response_data->FreeFileNodes != cpu_to_le64(-1))
4325 le64_to_cpu(response_data->FreeFileNodes);
4328 cifs_buf_release(pSMB);
4337 /* We can not use write of zero bytes trick to
4338 set file size due to need for large file support. Also note that
4339 this SetPathInfo is preferred to SetFileInfo based method in next
4340 routine which is only needed to work around a sharing violation bug
4341 in Samba which this routine can run into */
4344 CIFSSMBSetEOF(const int xid, struct cifsTconInfo *tcon, const char *fileName,
4345 __u64 size, int SetAllocation,
4346 const struct nls_table *nls_codepage, int remap)
4348 struct smb_com_transaction2_spi_req *pSMB = NULL;
4349 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
4350 struct file_end_of_file_info *parm_data;
4353 int bytes_returned = 0;
4354 __u16 params, byte_count, data_count, param_offset, offset;
4356 cFYI(1, ("In SetEOF"));
4358 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4363 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4365 cifsConvertToUCS((__le16 *) pSMB->FileName, fileName,
4366 PATH_MAX, nls_codepage, remap);
4367 name_len++; /* trailing null */
4369 } else { /* BB improve the check for buffer overruns BB */
4370 name_len = strnlen(fileName, PATH_MAX);
4371 name_len++; /* trailing null */
4372 strncpy(pSMB->FileName, fileName, name_len);
4374 params = 6 + name_len;
4375 data_count = sizeof (struct file_end_of_file_info);
4376 pSMB->MaxParameterCount = cpu_to_le16(2);
4377 pSMB->MaxDataCount = cpu_to_le16(4100);
4378 pSMB->MaxSetupCount = 0;
4382 pSMB->Reserved2 = 0;
4383 param_offset = offsetof(struct smb_com_transaction2_spi_req,
4384 InformationLevel) - 4;
4385 offset = param_offset + params;
4387 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
4388 pSMB->InformationLevel =
4389 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
4391 pSMB->InformationLevel =
4392 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
4393 } else /* Set File Size */ {
4394 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
4395 pSMB->InformationLevel =
4396 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
4398 pSMB->InformationLevel =
4399 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
4403 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol) +
4405 pSMB->ParameterOffset = cpu_to_le16(param_offset);
4406 pSMB->DataOffset = cpu_to_le16(offset);
4407 pSMB->SetupCount = 1;
4408 pSMB->Reserved3 = 0;
4409 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
4410 byte_count = 3 /* pad */ + params + data_count;
4411 pSMB->DataCount = cpu_to_le16(data_count);
4412 pSMB->TotalDataCount = pSMB->DataCount;
4413 pSMB->ParameterCount = cpu_to_le16(params);
4414 pSMB->TotalParameterCount = pSMB->ParameterCount;
4415 pSMB->Reserved4 = 0;
4416 pSMB->hdr.smb_buf_length += byte_count;
4417 parm_data->FileSize = cpu_to_le64(size);
4418 pSMB->ByteCount = cpu_to_le16(byte_count);
4419 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4420 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4422 cFYI(1, ("SetPathInfo (file size) returned %d", rc));
4425 cifs_buf_release(pSMB);
4434 CIFSSMBSetFileSize(const int xid, struct cifsTconInfo *tcon, __u64 size,
4435 __u16 fid, __u32 pid_of_opener, int SetAllocation)
4437 struct smb_com_transaction2_sfi_req *pSMB = NULL;
4438 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
4440 struct file_end_of_file_info *parm_data;
4442 int bytes_returned = 0;
4443 __u16 params, param_offset, offset, byte_count, count;
4445 cFYI(1, ("SetFileSize (via SetFileInfo) %lld",
4447 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
4452 pSMBr = (struct smb_com_transaction2_sfi_rsp *)pSMB;
4454 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
4455 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
4458 pSMB->MaxSetupCount = 0;
4462 pSMB->Reserved2 = 0;
4463 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
4464 offset = param_offset + params;
4466 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
4468 count = sizeof(struct file_end_of_file_info);
4469 pSMB->MaxParameterCount = cpu_to_le16(2);
4470 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB PDU from sess */
4471 pSMB->SetupCount = 1;
4472 pSMB->Reserved3 = 0;
4473 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
4474 byte_count = 3 /* pad */ + params + count;
4475 pSMB->DataCount = cpu_to_le16(count);
4476 pSMB->ParameterCount = cpu_to_le16(params);
4477 pSMB->TotalDataCount = pSMB->DataCount;
4478 pSMB->TotalParameterCount = pSMB->ParameterCount;
4479 pSMB->ParameterOffset = cpu_to_le16(param_offset);
4481 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol) +
4483 pSMB->DataOffset = cpu_to_le16(offset);
4484 parm_data->FileSize = cpu_to_le64(size);
4487 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
4488 pSMB->InformationLevel =
4489 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
4491 pSMB->InformationLevel =
4492 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
4493 } else /* Set File Size */ {
4494 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
4495 pSMB->InformationLevel =
4496 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
4498 pSMB->InformationLevel =
4499 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
4501 pSMB->Reserved4 = 0;
4502 pSMB->hdr.smb_buf_length += byte_count;
4503 pSMB->ByteCount = cpu_to_le16(byte_count);
4504 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4505 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4508 ("Send error in SetFileInfo (SetFileSize) = %d",
4513 cifs_small_buf_release(pSMB);
4515 /* Note: On -EAGAIN error only caller can retry on handle based calls
4516 since file handle passed in no longer valid */
4521 /* Some legacy servers such as NT4 require that the file times be set on
4522 an open handle, rather than by pathname - this is awkward due to
4523 potential access conflicts on the open, but it is unavoidable for these
4524 old servers since the only other choice is to go from 100 nanosecond DCE
4525 time and resort to the original setpathinfo level which takes the ancient
4526 DOS time format with 2 second granularity */
4528 CIFSSMBSetFileTimes(const int xid, struct cifsTconInfo *tcon, const FILE_BASIC_INFO * data,
4531 struct smb_com_transaction2_sfi_req *pSMB = NULL;
4532 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
4535 int bytes_returned = 0;
4536 __u16 params, param_offset, offset, byte_count, count;
4538 cFYI(1, ("Set Times (via SetFileInfo)"));
4539 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
4544 pSMBr = (struct smb_com_transaction2_sfi_rsp *)pSMB;
4546 /* At this point there is no need to override the current pid
4547 with the pid of the opener, but that could change if we someday
4548 use an existing handle (rather than opening one on the fly) */
4549 /* pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
4550 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));*/
4553 pSMB->MaxSetupCount = 0;
4557 pSMB->Reserved2 = 0;
4558 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
4559 offset = param_offset + params;
4561 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
4563 count = sizeof (FILE_BASIC_INFO);
4564 pSMB->MaxParameterCount = cpu_to_le16(2);
4565 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB PDU from sess */
4566 pSMB->SetupCount = 1;
4567 pSMB->Reserved3 = 0;
4568 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
4569 byte_count = 3 /* pad */ + params + count;
4570 pSMB->DataCount = cpu_to_le16(count);
4571 pSMB->ParameterCount = cpu_to_le16(params);
4572 pSMB->TotalDataCount = pSMB->DataCount;
4573 pSMB->TotalParameterCount = pSMB->ParameterCount;
4574 pSMB->ParameterOffset = cpu_to_le16(param_offset);
4575 pSMB->DataOffset = cpu_to_le16(offset);
4577 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
4578 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
4580 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
4581 pSMB->Reserved4 = 0;
4582 pSMB->hdr.smb_buf_length += byte_count;
4583 pSMB->ByteCount = cpu_to_le16(byte_count);
4584 memcpy(data_offset,data,sizeof(FILE_BASIC_INFO));
4585 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4586 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4588 cFYI(1,("Send error in Set Time (SetFileInfo) = %d",rc));
4591 cifs_small_buf_release(pSMB);
4593 /* Note: On -EAGAIN error only caller can retry on handle based calls
4594 since file handle passed in no longer valid */
4601 CIFSSMBSetTimes(const int xid, struct cifsTconInfo *tcon, const char *fileName,
4602 const FILE_BASIC_INFO * data,
4603 const struct nls_table *nls_codepage, int remap)
4605 TRANSACTION2_SPI_REQ *pSMB = NULL;
4606 TRANSACTION2_SPI_RSP *pSMBr = NULL;
4609 int bytes_returned = 0;
4611 __u16 params, param_offset, offset, byte_count, count;
4613 cFYI(1, ("In SetTimes"));
4616 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4621 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4623 cifsConvertToUCS((__le16 *) pSMB->FileName, fileName,
4624 PATH_MAX, nls_codepage, remap);
4625 name_len++; /* trailing null */
4627 } else { /* BB improve the check for buffer overruns BB */
4628 name_len = strnlen(fileName, PATH_MAX);
4629 name_len++; /* trailing null */
4630 strncpy(pSMB->FileName, fileName, name_len);
4633 params = 6 + name_len;
4634 count = sizeof (FILE_BASIC_INFO);
4635 pSMB->MaxParameterCount = cpu_to_le16(2);
4636 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find exact max SMB PDU from sess structure BB */
4637 pSMB->MaxSetupCount = 0;
4641 pSMB->Reserved2 = 0;
4642 param_offset = offsetof(struct smb_com_transaction2_spi_req,
4643 InformationLevel) - 4;
4644 offset = param_offset + params;
4645 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
4646 pSMB->ParameterOffset = cpu_to_le16(param_offset);
4647 pSMB->DataOffset = cpu_to_le16(offset);
4648 pSMB->SetupCount = 1;
4649 pSMB->Reserved3 = 0;
4650 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
4651 byte_count = 3 /* pad */ + params + count;
4653 pSMB->DataCount = cpu_to_le16(count);
4654 pSMB->ParameterCount = cpu_to_le16(params);
4655 pSMB->TotalDataCount = pSMB->DataCount;
4656 pSMB->TotalParameterCount = pSMB->ParameterCount;
4657 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
4658 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
4660 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
4661 pSMB->Reserved4 = 0;
4662 pSMB->hdr.smb_buf_length += byte_count;
4663 memcpy(data_offset, data, sizeof (FILE_BASIC_INFO));
4664 pSMB->ByteCount = cpu_to_le16(byte_count);
4665 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4666 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4668 cFYI(1, ("SetPathInfo (times) returned %d", rc));
4671 cifs_buf_release(pSMB);
4679 /* Can not be used to set time stamps yet (due to old DOS time format) */
4680 /* Can be used to set attributes */
4681 #if 0 /* Possibly not needed - since it turns out that strangely NT4 has a bug
4682 handling it anyway and NT4 was what we thought it would be needed for
4683 Do not delete it until we prove whether needed for Win9x though */
4685 CIFSSMBSetAttrLegacy(int xid, struct cifsTconInfo *tcon, char *fileName,
4686 __u16 dos_attrs, const struct nls_table *nls_codepage)
4688 SETATTR_REQ *pSMB = NULL;
4689 SETATTR_RSP *pSMBr = NULL;
4694 cFYI(1, ("In SetAttrLegacy"));
4697 rc = smb_init(SMB_COM_SETATTR, 8, tcon, (void **) &pSMB,
4702 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4704 ConvertToUCS((__le16 *) pSMB->fileName, fileName,
4705 PATH_MAX, nls_codepage);
4706 name_len++; /* trailing null */
4708 } else { /* BB improve the check for buffer overruns BB */
4709 name_len = strnlen(fileName, PATH_MAX);
4710 name_len++; /* trailing null */
4711 strncpy(pSMB->fileName, fileName, name_len);
4713 pSMB->attr = cpu_to_le16(dos_attrs);
4714 pSMB->BufferFormat = 0x04;
4715 pSMB->hdr.smb_buf_length += name_len + 1;
4716 pSMB->ByteCount = cpu_to_le16(name_len + 1);
4717 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4718 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4720 cFYI(1, ("Error in LegacySetAttr = %d", rc));
4723 cifs_buf_release(pSMB);
4726 goto SetAttrLgcyRetry;
4730 #endif /* temporarily unneeded SetAttr legacy function */
4733 CIFSSMBUnixSetPerms(const int xid, struct cifsTconInfo *tcon,
4734 char *fileName, __u64 mode, __u64 uid, __u64 gid,
4735 dev_t device, const struct nls_table *nls_codepage,
4738 TRANSACTION2_SPI_REQ *pSMB = NULL;
4739 TRANSACTION2_SPI_RSP *pSMBr = NULL;
4742 int bytes_returned = 0;
4743 FILE_UNIX_BASIC_INFO *data_offset;
4744 __u16 params, param_offset, offset, count, byte_count;
4746 cFYI(1, ("In SetUID/GID/Mode"));
4748 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4753 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4755 cifsConvertToUCS((__le16 *) pSMB->FileName, fileName,
4756 PATH_MAX, nls_codepage, remap);
4757 name_len++; /* trailing null */
4759 } else { /* BB improve the check for buffer overruns BB */
4760 name_len = strnlen(fileName, PATH_MAX);
4761 name_len++; /* trailing null */
4762 strncpy(pSMB->FileName, fileName, name_len);
4765 params = 6 + name_len;
4766 count = sizeof (FILE_UNIX_BASIC_INFO);
4767 pSMB->MaxParameterCount = cpu_to_le16(2);
4768 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find exact max SMB PDU from sess structure BB */
4769 pSMB->MaxSetupCount = 0;
4773 pSMB->Reserved2 = 0;
4774 param_offset = offsetof(struct smb_com_transaction2_spi_req,
4775 InformationLevel) - 4;
4776 offset = param_offset + params;
4778 (FILE_UNIX_BASIC_INFO *) ((char *) &pSMB->hdr.Protocol +
4780 memset(data_offset, 0, count);
4781 pSMB->DataOffset = cpu_to_le16(offset);
4782 pSMB->ParameterOffset = cpu_to_le16(param_offset);
4783 pSMB->SetupCount = 1;
4784 pSMB->Reserved3 = 0;
4785 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
4786 byte_count = 3 /* pad */ + params + count;
4787 pSMB->ParameterCount = cpu_to_le16(params);
4788 pSMB->DataCount = cpu_to_le16(count);
4789 pSMB->TotalParameterCount = pSMB->ParameterCount;
4790 pSMB->TotalDataCount = pSMB->DataCount;
4791 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
4792 pSMB->Reserved4 = 0;
4793 pSMB->hdr.smb_buf_length += byte_count;
4794 data_offset->Uid = cpu_to_le64(uid);
4795 data_offset->Gid = cpu_to_le64(gid);
4796 /* better to leave device as zero when it is */
4797 data_offset->DevMajor = cpu_to_le64(MAJOR(device));
4798 data_offset->DevMinor = cpu_to_le64(MINOR(device));
4799 data_offset->Permissions = cpu_to_le64(mode);
4802 data_offset->Type = cpu_to_le32(UNIX_FILE);
4803 else if(S_ISDIR(mode))
4804 data_offset->Type = cpu_to_le32(UNIX_DIR);
4805 else if(S_ISLNK(mode))
4806 data_offset->Type = cpu_to_le32(UNIX_SYMLINK);
4807 else if(S_ISCHR(mode))
4808 data_offset->Type = cpu_to_le32(UNIX_CHARDEV);
4809 else if(S_ISBLK(mode))
4810 data_offset->Type = cpu_to_le32(UNIX_BLOCKDEV);
4811 else if(S_ISFIFO(mode))
4812 data_offset->Type = cpu_to_le32(UNIX_FIFO);
4813 else if(S_ISSOCK(mode))
4814 data_offset->Type = cpu_to_le32(UNIX_SOCKET);
4817 pSMB->ByteCount = cpu_to_le16(byte_count);
4818 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4819 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4821 cFYI(1, ("SetPathInfo (perms) returned %d", rc));
4825 cifs_buf_release(pSMB);
4831 int CIFSSMBNotify(const int xid, struct cifsTconInfo *tcon,
4832 const int notify_subdirs, const __u16 netfid,
4833 __u32 filter, struct file * pfile, int multishot,
4834 const struct nls_table *nls_codepage)
4837 struct smb_com_transaction_change_notify_req * pSMB = NULL;
4838 struct smb_com_ntransaction_change_notify_rsp * pSMBr = NULL;
4839 struct dir_notify_req *dnotify_req;
4842 cFYI(1, ("In CIFSSMBNotify for file handle %d",(int)netfid));
4843 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
4848 pSMB->TotalParameterCount = 0 ;
4849 pSMB->TotalDataCount = 0;
4850 pSMB->MaxParameterCount = cpu_to_le32(2);
4851 /* BB find exact data count max from sess structure BB */
4852 pSMB->MaxDataCount = 0; /* same in little endian or be */
4853 /* BB VERIFY verify which is correct for above BB */
4854 pSMB->MaxDataCount = cpu_to_le32((tcon->ses->server->maxBuf -
4855 MAX_CIFS_HDR_SIZE) & 0xFFFFFF00);
4857 pSMB->MaxSetupCount = 4;
4859 pSMB->ParameterOffset = 0;
4860 pSMB->DataCount = 0;
4861 pSMB->DataOffset = 0;
4862 pSMB->SetupCount = 4; /* single byte does not need le conversion */
4863 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_NOTIFY_CHANGE);
4864 pSMB->ParameterCount = pSMB->TotalParameterCount;
4866 pSMB->WatchTree = 1; /* one byte - no le conversion needed */
4867 pSMB->Reserved2 = 0;
4868 pSMB->CompletionFilter = cpu_to_le32(filter);
4869 pSMB->Fid = netfid; /* file handle always le */
4870 pSMB->ByteCount = 0;
4872 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4873 (struct smb_hdr *) pSMBr, &bytes_returned, -1);
4875 cFYI(1, ("Error in Notify = %d", rc));
4877 /* Add file to outstanding requests */
4878 /* BB change to kmem cache alloc */
4879 dnotify_req = kmalloc(
4880 sizeof(struct dir_notify_req),
4883 dnotify_req->Pid = pSMB->hdr.Pid;
4884 dnotify_req->PidHigh = pSMB->hdr.PidHigh;
4885 dnotify_req->Mid = pSMB->hdr.Mid;
4886 dnotify_req->Tid = pSMB->hdr.Tid;
4887 dnotify_req->Uid = pSMB->hdr.Uid;
4888 dnotify_req->netfid = netfid;
4889 dnotify_req->pfile = pfile;
4890 dnotify_req->filter = filter;
4891 dnotify_req->multishot = multishot;
4892 spin_lock(&GlobalMid_Lock);
4893 list_add_tail(&dnotify_req->lhead,
4894 &GlobalDnotifyReqList);
4895 spin_unlock(&GlobalMid_Lock);
4899 cifs_buf_release(pSMB);
4902 #ifdef CONFIG_CIFS_XATTR
4904 CIFSSMBQAllEAs(const int xid, struct cifsTconInfo *tcon,
4905 const unsigned char *searchName,
4906 char * EAData, size_t buf_size,
4907 const struct nls_table *nls_codepage, int remap)
4909 /* BB assumes one setup word */
4910 TRANSACTION2_QPI_REQ *pSMB = NULL;
4911 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4915 struct fea * temp_fea;
4917 __u16 params, byte_count;
4919 cFYI(1, ("In Query All EAs path %s", searchName));
4921 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4926 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4928 cifsConvertToUCS((__le16 *) pSMB->FileName, searchName,
4929 PATH_MAX, nls_codepage, remap);
4930 name_len++; /* trailing null */
4932 } else { /* BB improve the check for buffer overruns BB */
4933 name_len = strnlen(searchName, PATH_MAX);
4934 name_len++; /* trailing null */
4935 strncpy(pSMB->FileName, searchName, name_len);
4938 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */ ;
4939 pSMB->TotalDataCount = 0;
4940 pSMB->MaxParameterCount = cpu_to_le16(2);
4941 pSMB->MaxDataCount = cpu_to_le16(4000); /* BB find exact max SMB PDU from sess structure BB */
4942 pSMB->MaxSetupCount = 0;
4946 pSMB->Reserved2 = 0;
4947 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4948 struct smb_com_transaction2_qpi_req ,InformationLevel) - 4);
4949 pSMB->DataCount = 0;
4950 pSMB->DataOffset = 0;
4951 pSMB->SetupCount = 1;
4952 pSMB->Reserved3 = 0;
4953 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4954 byte_count = params + 1 /* pad */ ;
4955 pSMB->TotalParameterCount = cpu_to_le16(params);
4956 pSMB->ParameterCount = pSMB->TotalParameterCount;
4957 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_QUERY_ALL_EAS);
4958 pSMB->Reserved4 = 0;
4959 pSMB->hdr.smb_buf_length += byte_count;
4960 pSMB->ByteCount = cpu_to_le16(byte_count);
4962 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4963 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4965 cFYI(1, ("Send error in QueryAllEAs = %d", rc));
4966 } else { /* decode response */
4967 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4969 /* BB also check enough total bytes returned */
4970 /* BB we need to improve the validity checking
4971 of these trans2 responses */
4972 if (rc || (pSMBr->ByteCount < 4))
4973 rc = -EIO; /* bad smb */
4974 /* else if (pFindData){
4975 memcpy((char *) pFindData,
4976 (char *) &pSMBr->hdr.Protocol +
4979 /* check that length of list is not more than bcc */
4980 /* check that each entry does not go beyond length
4982 /* check that each element of each entry does not
4983 go beyond end of list */
4984 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4985 struct fealist * ea_response_data;
4987 /* validate_trans2_offsets() */
4988 /* BB to check if(start of smb + data_offset > &bcc+ bcc)*/
4989 ea_response_data = (struct fealist *)
4990 (((char *) &pSMBr->hdr.Protocol) +
4992 name_len = le32_to_cpu(ea_response_data->list_len);
4993 cFYI(1,("ea length %d", name_len));
4995 /* returned EA size zeroed at top of function */
4996 cFYI(1,("empty EA list returned from server"));
4998 /* account for ea list len */
5000 temp_fea = ea_response_data->list;
5001 temp_ptr = (char *)temp_fea;
5002 while(name_len > 0) {
5006 rc += temp_fea->name_len;
5007 /* account for prefix user. and trailing null */
5009 if(rc<(int)buf_size) {
5010 memcpy(EAData,"user.",5);
5012 memcpy(EAData,temp_ptr,temp_fea->name_len);
5013 EAData+=temp_fea->name_len;
5014 /* null terminate name */
5016 EAData = EAData + 1;
5017 } else if(buf_size == 0) {
5018 /* skip copy - calc size only */
5020 /* stop before overrun buffer */
5024 name_len -= temp_fea->name_len;
5025 temp_ptr += temp_fea->name_len;
5026 /* account for trailing null */
5029 value_len = le16_to_cpu(temp_fea->value_len);
5030 name_len -= value_len;
5031 temp_ptr += value_len;
5032 /* BB check that temp_ptr is still within smb BB*/
5033 /* no trailing null to account for in value len */
5034 /* go on to next EA */
5035 temp_fea = (struct fea *)temp_ptr;
5041 cifs_buf_release(pSMB);
5048 ssize_t CIFSSMBQueryEA(const int xid,struct cifsTconInfo * tcon,
5049 const unsigned char * searchName,const unsigned char * ea_name,
5050 unsigned char * ea_value, size_t buf_size,
5051 const struct nls_table *nls_codepage, int remap)
5053 TRANSACTION2_QPI_REQ *pSMB = NULL;
5054 TRANSACTION2_QPI_RSP *pSMBr = NULL;
5058 struct fea * temp_fea;
5060 __u16 params, byte_count;
5062 cFYI(1, ("In Query EA path %s", searchName));
5064 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5069 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5071 cifsConvertToUCS((__le16 *) pSMB->FileName, searchName,
5072 PATH_MAX, nls_codepage, remap);
5073 name_len++; /* trailing null */
5075 } else { /* BB improve the check for buffer overruns BB */
5076 name_len = strnlen(searchName, PATH_MAX);
5077 name_len++; /* trailing null */
5078 strncpy(pSMB->FileName, searchName, name_len);
5081 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */ ;
5082 pSMB->TotalDataCount = 0;
5083 pSMB->MaxParameterCount = cpu_to_le16(2);
5084 pSMB->MaxDataCount = cpu_to_le16(4000); /* BB find exact max SMB PDU from sess structure BB */
5085 pSMB->MaxSetupCount = 0;
5089 pSMB->Reserved2 = 0;
5090 pSMB->ParameterOffset = cpu_to_le16(offsetof(
5091 struct smb_com_transaction2_qpi_req ,InformationLevel) - 4);
5092 pSMB->DataCount = 0;
5093 pSMB->DataOffset = 0;
5094 pSMB->SetupCount = 1;
5095 pSMB->Reserved3 = 0;
5096 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
5097 byte_count = params + 1 /* pad */ ;
5098 pSMB->TotalParameterCount = cpu_to_le16(params);
5099 pSMB->ParameterCount = pSMB->TotalParameterCount;
5100 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_QUERY_ALL_EAS);
5101 pSMB->Reserved4 = 0;
5102 pSMB->hdr.smb_buf_length += byte_count;
5103 pSMB->ByteCount = cpu_to_le16(byte_count);
5105 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5106 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5108 cFYI(1, ("Send error in Query EA = %d", rc));
5109 } else { /* decode response */
5110 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5112 /* BB also check enough total bytes returned */
5113 /* BB we need to improve the validity checking
5114 of these trans2 responses */
5115 if (rc || (pSMBr->ByteCount < 4))
5116 rc = -EIO; /* bad smb */
5117 /* else if (pFindData){
5118 memcpy((char *) pFindData,
5119 (char *) &pSMBr->hdr.Protocol +
5122 /* check that length of list is not more than bcc */
5123 /* check that each entry does not go beyond length
5125 /* check that each element of each entry does not
5126 go beyond end of list */
5127 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5128 struct fealist * ea_response_data;
5130 /* validate_trans2_offsets() */
5131 /* BB to check if(start of smb + data_offset > &bcc+ bcc)*/
5132 ea_response_data = (struct fealist *)
5133 (((char *) &pSMBr->hdr.Protocol) +
5135 name_len = le32_to_cpu(ea_response_data->list_len);
5136 cFYI(1,("ea length %d", name_len));
5138 /* returned EA size zeroed at top of function */
5139 cFYI(1,("empty EA list returned from server"));
5141 /* account for ea list len */
5143 temp_fea = ea_response_data->list;
5144 temp_ptr = (char *)temp_fea;
5145 /* loop through checking if we have a matching
5146 name and then return the associated value */
5147 while(name_len > 0) {
5151 value_len = le16_to_cpu(temp_fea->value_len);
5152 /* BB validate that value_len falls within SMB,
5153 even though maximum for name_len is 255 */
5154 if(memcmp(temp_fea->name,ea_name,
5155 temp_fea->name_len) == 0) {
5158 /* account for prefix user. and trailing null */
5159 if(rc<=(int)buf_size) {
5161 temp_fea->name+temp_fea->name_len+1,
5163 /* ea values, unlike ea names,
5164 are not null terminated */
5165 } else if(buf_size == 0) {
5166 /* skip copy - calc size only */
5168 /* stop before overrun buffer */
5173 name_len -= temp_fea->name_len;
5174 temp_ptr += temp_fea->name_len;
5175 /* account for trailing null */
5178 name_len -= value_len;
5179 temp_ptr += value_len;
5180 /* no trailing null to account for in value len */
5181 /* go on to next EA */
5182 temp_fea = (struct fea *)temp_ptr;
5188 cifs_buf_release(pSMB);
5196 CIFSSMBSetEA(const int xid, struct cifsTconInfo *tcon, const char *fileName,
5197 const char * ea_name, const void * ea_value,
5198 const __u16 ea_value_len, const struct nls_table *nls_codepage,
5201 struct smb_com_transaction2_spi_req *pSMB = NULL;
5202 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
5203 struct fealist *parm_data;
5206 int bytes_returned = 0;
5207 __u16 params, param_offset, byte_count, offset, count;
5209 cFYI(1, ("In SetEA"));
5211 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5216 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5218 cifsConvertToUCS((__le16 *) pSMB->FileName, fileName,
5219 PATH_MAX, nls_codepage, remap);
5220 name_len++; /* trailing null */
5222 } else { /* BB improve the check for buffer overruns BB */
5223 name_len = strnlen(fileName, PATH_MAX);
5224 name_len++; /* trailing null */
5225 strncpy(pSMB->FileName, fileName, name_len);
5228 params = 6 + name_len;
5230 /* done calculating parms using name_len of file name,
5231 now use name_len to calculate length of ea name
5232 we are going to create in the inode xattrs */
5236 name_len = strnlen(ea_name,255);
5238 count = sizeof(*parm_data) + ea_value_len + name_len + 1;
5239 pSMB->MaxParameterCount = cpu_to_le16(2);
5240 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB size from sess */
5241 pSMB->MaxSetupCount = 0;
5245 pSMB->Reserved2 = 0;
5246 param_offset = offsetof(struct smb_com_transaction2_spi_req,
5247 InformationLevel) - 4;
5248 offset = param_offset + params;
5249 pSMB->InformationLevel =
5250 cpu_to_le16(SMB_SET_FILE_EA);
5253 (struct fealist *) (((char *) &pSMB->hdr.Protocol) +
5255 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5256 pSMB->DataOffset = cpu_to_le16(offset);
5257 pSMB->SetupCount = 1;
5258 pSMB->Reserved3 = 0;
5259 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5260 byte_count = 3 /* pad */ + params + count;
5261 pSMB->DataCount = cpu_to_le16(count);
5262 parm_data->list_len = cpu_to_le32(count);
5263 parm_data->list[0].EA_flags = 0;
5264 /* we checked above that name len is less than 255 */
5265 parm_data->list[0].name_len = (__u8)name_len;
5266 /* EA names are always ASCII */
5268 strncpy(parm_data->list[0].name,ea_name,name_len);
5269 parm_data->list[0].name[name_len] = 0;
5270 parm_data->list[0].value_len = cpu_to_le16(ea_value_len);
5271 /* caller ensures that ea_value_len is less than 64K but
5272 we need to ensure that it fits within the smb */
5274 /*BB add length check that it would fit in negotiated SMB buffer size BB */
5275 /* if(ea_value_len > buffer_size - 512 (enough for header)) */
5277 memcpy(parm_data->list[0].name+name_len+1,ea_value,ea_value_len);
5279 pSMB->TotalDataCount = pSMB->DataCount;
5280 pSMB->ParameterCount = cpu_to_le16(params);
5281 pSMB->TotalParameterCount = pSMB->ParameterCount;
5282 pSMB->Reserved4 = 0;
5283 pSMB->hdr.smb_buf_length += byte_count;
5284 pSMB->ByteCount = cpu_to_le16(byte_count);
5285 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5286 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5288 cFYI(1, ("SetPathInfo (EA) returned %d", rc));
5291 cifs_buf_release(pSMB);