Linux 3.12.28
[linux/fpc-iii.git] / fs / cifs / cifssmb.c
blob5f1f3285479ebc441304f3eb059ba744b110bb1d
1 /*
2 * fs/cifs/cifssmb.c
4 * Copyright (C) International Business Machines Corp., 2002,2010
5 * Author(s): Steve French (sfrench@us.ibm.com)
7 * Contains the routines for constructing the SMB PDUs themselves
9 * This library is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as published
11 * by the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
17 * the GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 /* SMB/CIFS PDU handling routines here - except for leftovers in connect.c */
25 /* These are mostly routines that operate on a pathname, or on a tree id */
26 /* (mounted volume), but there are eight handle based routines which must be */
27 /* treated slightly differently for reconnection purposes since we never */
28 /* want to reuse a stale file handle and only the caller knows the file info */
30 #include <linux/fs.h>
31 #include <linux/kernel.h>
32 #include <linux/vfs.h>
33 #include <linux/slab.h>
34 #include <linux/posix_acl_xattr.h>
35 #include <linux/pagemap.h>
36 #include <linux/swap.h>
37 #include <linux/task_io_accounting_ops.h>
38 #include <asm/uaccess.h>
39 #include "cifspdu.h"
40 #include "cifsglob.h"
41 #include "cifsacl.h"
42 #include "cifsproto.h"
43 #include "cifs_unicode.h"
44 #include "cifs_debug.h"
45 #include "fscache.h"
47 #ifdef CONFIG_CIFS_POSIX
48 static struct {
49 int index;
50 char *name;
51 } protocols[] = {
52 #ifdef CONFIG_CIFS_WEAK_PW_HASH
53 {LANMAN_PROT, "\2LM1.2X002"},
54 {LANMAN2_PROT, "\2LANMAN2.1"},
55 #endif /* weak password hashing for legacy clients */
56 {CIFS_PROT, "\2NT LM 0.12"},
57 {POSIX_PROT, "\2POSIX 2"},
58 {BAD_PROT, "\2"}
60 #else
61 static struct {
62 int index;
63 char *name;
64 } protocols[] = {
65 #ifdef CONFIG_CIFS_WEAK_PW_HASH
66 {LANMAN_PROT, "\2LM1.2X002"},
67 {LANMAN2_PROT, "\2LANMAN2.1"},
68 #endif /* weak password hashing for legacy clients */
69 {CIFS_PROT, "\2NT LM 0.12"},
70 {BAD_PROT, "\2"}
72 #endif
74 /* define the number of elements in the cifs dialect array */
75 #ifdef CONFIG_CIFS_POSIX
76 #ifdef CONFIG_CIFS_WEAK_PW_HASH
77 #define CIFS_NUM_PROT 4
78 #else
79 #define CIFS_NUM_PROT 2
80 #endif /* CIFS_WEAK_PW_HASH */
81 #else /* not posix */
82 #ifdef CONFIG_CIFS_WEAK_PW_HASH
83 #define CIFS_NUM_PROT 3
84 #else
85 #define CIFS_NUM_PROT 1
86 #endif /* CONFIG_CIFS_WEAK_PW_HASH */
87 #endif /* CIFS_POSIX */
90 * Mark as invalid, all open files on tree connections since they
91 * were closed when session to server was lost.
93 void
94 cifs_mark_open_files_invalid(struct cifs_tcon *tcon)
96 struct cifsFileInfo *open_file = NULL;
97 struct list_head *tmp;
98 struct list_head *tmp1;
100 /* list all files open on tree connection and mark them invalid */
101 spin_lock(&cifs_file_list_lock);
102 list_for_each_safe(tmp, tmp1, &tcon->openFileList) {
103 open_file = list_entry(tmp, struct cifsFileInfo, tlist);
104 open_file->invalidHandle = true;
105 open_file->oplock_break_cancelled = true;
107 spin_unlock(&cifs_file_list_lock);
109 * BB Add call to invalidate_inodes(sb) for all superblocks mounted
110 * to this tcon.
114 /* reconnect the socket, tcon, and smb session if needed */
115 static int
116 cifs_reconnect_tcon(struct cifs_tcon *tcon, int smb_command)
118 int rc;
119 struct cifs_ses *ses;
120 struct TCP_Server_Info *server;
121 struct nls_table *nls_codepage;
124 * SMBs NegProt, SessSetup, uLogoff do not have tcon yet so check for
125 * tcp and smb session status done differently for those three - in the
126 * calling routine
128 if (!tcon)
129 return 0;
131 ses = tcon->ses;
132 server = ses->server;
135 * only tree disconnect, open, and write, (and ulogoff which does not
136 * have tcon) are allowed as we start force umount
138 if (tcon->tidStatus == CifsExiting) {
139 if (smb_command != SMB_COM_WRITE_ANDX &&
140 smb_command != SMB_COM_OPEN_ANDX &&
141 smb_command != SMB_COM_TREE_DISCONNECT) {
142 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
143 smb_command);
144 return -ENODEV;
149 * Give demultiplex thread up to 10 seconds to reconnect, should be
150 * greater than cifs socket timeout which is 7 seconds
152 while (server->tcpStatus == CifsNeedReconnect) {
153 wait_event_interruptible_timeout(server->response_q,
154 (server->tcpStatus != CifsNeedReconnect), 10 * HZ);
156 /* are we still trying to reconnect? */
157 if (server->tcpStatus != CifsNeedReconnect)
158 break;
161 * on "soft" mounts we wait once. Hard mounts keep
162 * retrying until process is killed or server comes
163 * back on-line
165 if (!tcon->retry) {
166 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
167 return -EHOSTDOWN;
171 if (!ses->need_reconnect && !tcon->need_reconnect)
172 return 0;
174 nls_codepage = load_nls_default();
177 * need to prevent multiple threads trying to simultaneously
178 * reconnect the same SMB session
180 mutex_lock(&ses->session_mutex);
181 rc = cifs_negotiate_protocol(0, ses);
182 if (rc == 0 && ses->need_reconnect)
183 rc = cifs_setup_session(0, ses, nls_codepage);
185 /* do we need to reconnect tcon? */
186 if (rc || !tcon->need_reconnect) {
187 mutex_unlock(&ses->session_mutex);
188 goto out;
191 cifs_mark_open_files_invalid(tcon);
192 rc = CIFSTCon(0, ses, tcon->treeName, tcon, nls_codepage);
193 mutex_unlock(&ses->session_mutex);
194 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
196 if (rc)
197 goto out;
200 * FIXME: check if wsize needs updated due to negotiated smb buffer
201 * size shrinking
203 atomic_inc(&tconInfoReconnectCount);
205 /* tell server Unix caps we support */
206 if (ses->capabilities & CAP_UNIX)
207 reset_cifs_unix_caps(0, tcon, NULL, NULL);
210 * Removed call to reopen open files here. It is safer (and faster) to
211 * reopen files one at a time as needed in read and write.
213 * FIXME: what about file locks? don't we need to reclaim them ASAP?
216 out:
218 * Check if handle based operation so we know whether we can continue
219 * or not without returning to caller to reset file handle
221 switch (smb_command) {
222 case SMB_COM_READ_ANDX:
223 case SMB_COM_WRITE_ANDX:
224 case SMB_COM_CLOSE:
225 case SMB_COM_FIND_CLOSE2:
226 case SMB_COM_LOCKING_ANDX:
227 rc = -EAGAIN;
230 unload_nls(nls_codepage);
231 return rc;
234 /* Allocate and return pointer to an SMB request buffer, and set basic
235 SMB information in the SMB header. If the return code is zero, this
236 function must have filled in request_buf pointer */
237 static int
238 small_smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
239 void **request_buf)
241 int rc;
243 rc = cifs_reconnect_tcon(tcon, smb_command);
244 if (rc)
245 return rc;
247 *request_buf = cifs_small_buf_get();
248 if (*request_buf == NULL) {
249 /* BB should we add a retry in here if not a writepage? */
250 return -ENOMEM;
253 header_assemble((struct smb_hdr *) *request_buf, smb_command,
254 tcon, wct);
256 if (tcon != NULL)
257 cifs_stats_inc(&tcon->num_smbs_sent);
259 return 0;
263 small_smb_init_no_tc(const int smb_command, const int wct,
264 struct cifs_ses *ses, void **request_buf)
266 int rc;
267 struct smb_hdr *buffer;
269 rc = small_smb_init(smb_command, wct, NULL, request_buf);
270 if (rc)
271 return rc;
273 buffer = (struct smb_hdr *)*request_buf;
274 buffer->Mid = get_next_mid(ses->server);
275 if (ses->capabilities & CAP_UNICODE)
276 buffer->Flags2 |= SMBFLG2_UNICODE;
277 if (ses->capabilities & CAP_STATUS32)
278 buffer->Flags2 |= SMBFLG2_ERR_STATUS;
280 /* uid, tid can stay at zero as set in header assemble */
282 /* BB add support for turning on the signing when
283 this function is used after 1st of session setup requests */
285 return rc;
288 /* If the return code is zero, this function must fill in request_buf pointer */
289 static int
290 __smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
291 void **request_buf, void **response_buf)
293 *request_buf = cifs_buf_get();
294 if (*request_buf == NULL) {
295 /* BB should we add a retry in here if not a writepage? */
296 return -ENOMEM;
298 /* Although the original thought was we needed the response buf for */
299 /* potential retries of smb operations it turns out we can determine */
300 /* from the mid flags when the request buffer can be resent without */
301 /* having to use a second distinct buffer for the response */
302 if (response_buf)
303 *response_buf = *request_buf;
305 header_assemble((struct smb_hdr *) *request_buf, smb_command, tcon,
306 wct);
308 if (tcon != NULL)
309 cifs_stats_inc(&tcon->num_smbs_sent);
311 return 0;
314 /* If the return code is zero, this function must fill in request_buf pointer */
315 static int
316 smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
317 void **request_buf, void **response_buf)
319 int rc;
321 rc = cifs_reconnect_tcon(tcon, smb_command);
322 if (rc)
323 return rc;
325 return __smb_init(smb_command, wct, tcon, request_buf, response_buf);
328 static int
329 smb_init_no_reconnect(int smb_command, int wct, struct cifs_tcon *tcon,
330 void **request_buf, void **response_buf)
332 if (tcon->ses->need_reconnect || tcon->need_reconnect)
333 return -EHOSTDOWN;
335 return __smb_init(smb_command, wct, tcon, request_buf, response_buf);
338 static int validate_t2(struct smb_t2_rsp *pSMB)
340 unsigned int total_size;
342 /* check for plausible wct */
343 if (pSMB->hdr.WordCount < 10)
344 goto vt2_err;
346 /* check for parm and data offset going beyond end of smb */
347 if (get_unaligned_le16(&pSMB->t2_rsp.ParameterOffset) > 1024 ||
348 get_unaligned_le16(&pSMB->t2_rsp.DataOffset) > 1024)
349 goto vt2_err;
351 total_size = get_unaligned_le16(&pSMB->t2_rsp.ParameterCount);
352 if (total_size >= 512)
353 goto vt2_err;
355 /* check that bcc is at least as big as parms + data, and that it is
356 * less than negotiated smb buffer
358 total_size += get_unaligned_le16(&pSMB->t2_rsp.DataCount);
359 if (total_size > get_bcc(&pSMB->hdr) ||
360 total_size >= CIFSMaxBufSize + MAX_CIFS_HDR_SIZE)
361 goto vt2_err;
363 return 0;
364 vt2_err:
365 cifs_dump_mem("Invalid transact2 SMB: ", (char *)pSMB,
366 sizeof(struct smb_t2_rsp) + 16);
367 return -EINVAL;
370 static int
371 decode_ext_sec_blob(struct cifs_ses *ses, NEGOTIATE_RSP *pSMBr)
373 int rc = 0;
374 u16 count;
375 char *guid = pSMBr->u.extended_response.GUID;
376 struct TCP_Server_Info *server = ses->server;
378 count = get_bcc(&pSMBr->hdr);
379 if (count < SMB1_CLIENT_GUID_SIZE)
380 return -EIO;
382 spin_lock(&cifs_tcp_ses_lock);
383 if (server->srv_count > 1) {
384 spin_unlock(&cifs_tcp_ses_lock);
385 if (memcmp(server->server_GUID, guid, SMB1_CLIENT_GUID_SIZE) != 0) {
386 cifs_dbg(FYI, "server UID changed\n");
387 memcpy(server->server_GUID, guid, SMB1_CLIENT_GUID_SIZE);
389 } else {
390 spin_unlock(&cifs_tcp_ses_lock);
391 memcpy(server->server_GUID, guid, SMB1_CLIENT_GUID_SIZE);
394 if (count == SMB1_CLIENT_GUID_SIZE) {
395 server->sec_ntlmssp = true;
396 } else {
397 count -= SMB1_CLIENT_GUID_SIZE;
398 rc = decode_negTokenInit(
399 pSMBr->u.extended_response.SecurityBlob, count, server);
400 if (rc != 1)
401 return -EINVAL;
404 return 0;
408 cifs_enable_signing(struct TCP_Server_Info *server, bool mnt_sign_required)
410 bool srv_sign_required = server->sec_mode & server->vals->signing_required;
411 bool srv_sign_enabled = server->sec_mode & server->vals->signing_enabled;
412 bool mnt_sign_enabled = global_secflags & CIFSSEC_MAY_SIGN;
415 * Is signing required by mnt options? If not then check
416 * global_secflags to see if it is there.
418 if (!mnt_sign_required)
419 mnt_sign_required = ((global_secflags & CIFSSEC_MUST_SIGN) ==
420 CIFSSEC_MUST_SIGN);
423 * If signing is required then it's automatically enabled too,
424 * otherwise, check to see if the secflags allow it.
426 mnt_sign_enabled = mnt_sign_required ? mnt_sign_required :
427 (global_secflags & CIFSSEC_MAY_SIGN);
429 /* If server requires signing, does client allow it? */
430 if (srv_sign_required) {
431 if (!mnt_sign_enabled) {
432 cifs_dbg(VFS, "Server requires signing, but it's disabled in SecurityFlags!");
433 return -ENOTSUPP;
435 server->sign = true;
438 /* If client requires signing, does server allow it? */
439 if (mnt_sign_required) {
440 if (!srv_sign_enabled) {
441 cifs_dbg(VFS, "Server does not support signing!");
442 return -ENOTSUPP;
444 server->sign = true;
447 return 0;
450 #ifdef CONFIG_CIFS_WEAK_PW_HASH
451 static int
452 decode_lanman_negprot_rsp(struct TCP_Server_Info *server, NEGOTIATE_RSP *pSMBr)
454 __s16 tmp;
455 struct lanman_neg_rsp *rsp = (struct lanman_neg_rsp *)pSMBr;
457 if (server->dialect != LANMAN_PROT && server->dialect != LANMAN2_PROT)
458 return -EOPNOTSUPP;
460 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
461 server->maxReq = min_t(unsigned int,
462 le16_to_cpu(rsp->MaxMpxCount),
463 cifs_max_pending);
464 set_credits(server, server->maxReq);
465 server->maxBuf = le16_to_cpu(rsp->MaxBufSize);
466 /* even though we do not use raw we might as well set this
467 accurately, in case we ever find a need for it */
468 if ((le16_to_cpu(rsp->RawMode) & RAW_ENABLE) == RAW_ENABLE) {
469 server->max_rw = 0xFF00;
470 server->capabilities = CAP_MPX_MODE | CAP_RAW_MODE;
471 } else {
472 server->max_rw = 0;/* do not need to use raw anyway */
473 server->capabilities = CAP_MPX_MODE;
475 tmp = (__s16)le16_to_cpu(rsp->ServerTimeZone);
476 if (tmp == -1) {
477 /* OS/2 often does not set timezone therefore
478 * we must use server time to calc time zone.
479 * Could deviate slightly from the right zone.
480 * Smallest defined timezone difference is 15 minutes
481 * (i.e. Nepal). Rounding up/down is done to match
482 * this requirement.
484 int val, seconds, remain, result;
485 struct timespec ts, utc;
486 utc = CURRENT_TIME;
487 ts = cnvrtDosUnixTm(rsp->SrvTime.Date,
488 rsp->SrvTime.Time, 0);
489 cifs_dbg(FYI, "SrvTime %d sec since 1970 (utc: %d) diff: %d\n",
490 (int)ts.tv_sec, (int)utc.tv_sec,
491 (int)(utc.tv_sec - ts.tv_sec));
492 val = (int)(utc.tv_sec - ts.tv_sec);
493 seconds = abs(val);
494 result = (seconds / MIN_TZ_ADJ) * MIN_TZ_ADJ;
495 remain = seconds % MIN_TZ_ADJ;
496 if (remain >= (MIN_TZ_ADJ / 2))
497 result += MIN_TZ_ADJ;
498 if (val < 0)
499 result = -result;
500 server->timeAdj = result;
501 } else {
502 server->timeAdj = (int)tmp;
503 server->timeAdj *= 60; /* also in seconds */
505 cifs_dbg(FYI, "server->timeAdj: %d seconds\n", server->timeAdj);
508 /* BB get server time for time conversions and add
509 code to use it and timezone since this is not UTC */
511 if (rsp->EncryptionKeyLength ==
512 cpu_to_le16(CIFS_CRYPTO_KEY_SIZE)) {
513 memcpy(server->cryptkey, rsp->EncryptionKey,
514 CIFS_CRYPTO_KEY_SIZE);
515 } else if (server->sec_mode & SECMODE_PW_ENCRYPT) {
516 return -EIO; /* need cryptkey unless plain text */
519 cifs_dbg(FYI, "LANMAN negotiated\n");
520 return 0;
522 #else
523 static inline int
524 decode_lanman_negprot_rsp(struct TCP_Server_Info *server, NEGOTIATE_RSP *pSMBr)
526 cifs_dbg(VFS, "mount failed, cifs module not built with CIFS_WEAK_PW_HASH support\n");
527 return -EOPNOTSUPP;
529 #endif
531 static bool
532 should_set_ext_sec_flag(enum securityEnum sectype)
534 switch (sectype) {
535 case RawNTLMSSP:
536 case Kerberos:
537 return true;
538 case Unspecified:
539 if (global_secflags &
540 (CIFSSEC_MAY_KRB5 | CIFSSEC_MAY_NTLMSSP))
541 return true;
542 /* Fallthrough */
543 default:
544 return false;
549 CIFSSMBNegotiate(const unsigned int xid, struct cifs_ses *ses)
551 NEGOTIATE_REQ *pSMB;
552 NEGOTIATE_RSP *pSMBr;
553 int rc = 0;
554 int bytes_returned;
555 int i;
556 struct TCP_Server_Info *server = ses->server;
557 u16 count;
559 if (!server) {
560 WARN(1, "%s: server is NULL!\n", __func__);
561 return -EIO;
564 rc = smb_init(SMB_COM_NEGOTIATE, 0, NULL /* no tcon yet */ ,
565 (void **) &pSMB, (void **) &pSMBr);
566 if (rc)
567 return rc;
569 pSMB->hdr.Mid = get_next_mid(server);
570 pSMB->hdr.Flags2 |= (SMBFLG2_UNICODE | SMBFLG2_ERR_STATUS);
572 if (should_set_ext_sec_flag(ses->sectype)) {
573 cifs_dbg(FYI, "Requesting extended security.");
574 pSMB->hdr.Flags2 |= SMBFLG2_EXT_SEC;
577 count = 0;
578 for (i = 0; i < CIFS_NUM_PROT; i++) {
579 strncpy(pSMB->DialectsArray+count, protocols[i].name, 16);
580 count += strlen(protocols[i].name) + 1;
581 /* null at end of source and target buffers anyway */
583 inc_rfc1001_len(pSMB, count);
584 pSMB->ByteCount = cpu_to_le16(count);
586 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
587 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
588 if (rc != 0)
589 goto neg_err_exit;
591 server->dialect = le16_to_cpu(pSMBr->DialectIndex);
592 cifs_dbg(FYI, "Dialect: %d\n", server->dialect);
593 /* Check wct = 1 error case */
594 if ((pSMBr->hdr.WordCount < 13) || (server->dialect == BAD_PROT)) {
595 /* core returns wct = 1, but we do not ask for core - otherwise
596 small wct just comes when dialect index is -1 indicating we
597 could not negotiate a common dialect */
598 rc = -EOPNOTSUPP;
599 goto neg_err_exit;
600 } else if (pSMBr->hdr.WordCount == 13) {
601 server->negflavor = CIFS_NEGFLAVOR_LANMAN;
602 rc = decode_lanman_negprot_rsp(server, pSMBr);
603 goto signing_check;
604 } else if (pSMBr->hdr.WordCount != 17) {
605 /* unknown wct */
606 rc = -EOPNOTSUPP;
607 goto neg_err_exit;
609 /* else wct == 17, NTLM or better */
611 server->sec_mode = pSMBr->SecurityMode;
612 if ((server->sec_mode & SECMODE_USER) == 0)
613 cifs_dbg(FYI, "share mode security\n");
615 /* one byte, so no need to convert this or EncryptionKeyLen from
616 little endian */
617 server->maxReq = min_t(unsigned int, le16_to_cpu(pSMBr->MaxMpxCount),
618 cifs_max_pending);
619 set_credits(server, server->maxReq);
620 /* probably no need to store and check maxvcs */
621 server->maxBuf = le32_to_cpu(pSMBr->MaxBufferSize);
622 server->max_rw = le32_to_cpu(pSMBr->MaxRawSize);
623 cifs_dbg(NOISY, "Max buf = %d\n", ses->server->maxBuf);
624 server->capabilities = le32_to_cpu(pSMBr->Capabilities);
625 server->timeAdj = (int)(__s16)le16_to_cpu(pSMBr->ServerTimeZone);
626 server->timeAdj *= 60;
628 if (pSMBr->EncryptionKeyLength == CIFS_CRYPTO_KEY_SIZE) {
629 server->negflavor = CIFS_NEGFLAVOR_UNENCAP;
630 memcpy(ses->server->cryptkey, pSMBr->u.EncryptionKey,
631 CIFS_CRYPTO_KEY_SIZE);
632 } else if ((pSMBr->hdr.Flags2 & SMBFLG2_EXT_SEC ||
633 server->capabilities & CAP_EXTENDED_SECURITY) &&
634 (pSMBr->EncryptionKeyLength == 0)) {
635 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
636 rc = decode_ext_sec_blob(ses, pSMBr);
637 } else if (server->sec_mode & SECMODE_PW_ENCRYPT) {
638 rc = -EIO; /* no crypt key only if plain text pwd */
639 } else {
640 server->negflavor = CIFS_NEGFLAVOR_UNENCAP;
641 server->capabilities &= ~CAP_EXTENDED_SECURITY;
644 signing_check:
645 if (!rc)
646 rc = cifs_enable_signing(server, ses->sign);
647 neg_err_exit:
648 cifs_buf_release(pSMB);
650 cifs_dbg(FYI, "negprot rc %d\n", rc);
651 return rc;
655 CIFSSMBTDis(const unsigned int xid, struct cifs_tcon *tcon)
657 struct smb_hdr *smb_buffer;
658 int rc = 0;
660 cifs_dbg(FYI, "In tree disconnect\n");
662 /* BB: do we need to check this? These should never be NULL. */
663 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
664 return -EIO;
667 * No need to return error on this operation if tid invalidated and
668 * closed on server already e.g. due to tcp session crashing. Also,
669 * the tcon is no longer on the list, so no need to take lock before
670 * checking this.
672 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
673 return 0;
675 rc = small_smb_init(SMB_COM_TREE_DISCONNECT, 0, tcon,
676 (void **)&smb_buffer);
677 if (rc)
678 return rc;
680 rc = SendReceiveNoRsp(xid, tcon->ses, (char *)smb_buffer, 0);
681 if (rc)
682 cifs_dbg(FYI, "Tree disconnect failed %d\n", rc);
684 /* No need to return error on this operation if tid invalidated and
685 closed on server already e.g. due to tcp session crashing */
686 if (rc == -EAGAIN)
687 rc = 0;
689 return rc;
693 * This is a no-op for now. We're not really interested in the reply, but
694 * rather in the fact that the server sent one and that server->lstrp
695 * gets updated.
697 * FIXME: maybe we should consider checking that the reply matches request?
699 static void
700 cifs_echo_callback(struct mid_q_entry *mid)
702 struct TCP_Server_Info *server = mid->callback_data;
704 DeleteMidQEntry(mid);
705 add_credits(server, 1, CIFS_ECHO_OP);
709 CIFSSMBEcho(struct TCP_Server_Info *server)
711 ECHO_REQ *smb;
712 int rc = 0;
713 struct kvec iov;
714 struct smb_rqst rqst = { .rq_iov = &iov,
715 .rq_nvec = 1 };
717 cifs_dbg(FYI, "In echo request\n");
719 rc = small_smb_init(SMB_COM_ECHO, 0, NULL, (void **)&smb);
720 if (rc)
721 return rc;
723 /* set up echo request */
724 smb->hdr.Tid = 0xffff;
725 smb->hdr.WordCount = 1;
726 put_unaligned_le16(1, &smb->EchoCount);
727 put_bcc(1, &smb->hdr);
728 smb->Data[0] = 'a';
729 inc_rfc1001_len(smb, 3);
730 iov.iov_base = smb;
731 iov.iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4;
733 rc = cifs_call_async(server, &rqst, NULL, cifs_echo_callback,
734 server, CIFS_ASYNC_OP | CIFS_ECHO_OP);
735 if (rc)
736 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
738 cifs_small_buf_release(smb);
740 return rc;
744 CIFSSMBLogoff(const unsigned int xid, struct cifs_ses *ses)
746 LOGOFF_ANDX_REQ *pSMB;
747 int rc = 0;
749 cifs_dbg(FYI, "In SMBLogoff for session disconnect\n");
752 * BB: do we need to check validity of ses and server? They should
753 * always be valid since we have an active reference. If not, that
754 * should probably be a BUG()
756 if (!ses || !ses->server)
757 return -EIO;
759 mutex_lock(&ses->session_mutex);
760 if (ses->need_reconnect)
761 goto session_already_dead; /* no need to send SMBlogoff if uid
762 already closed due to reconnect */
763 rc = small_smb_init(SMB_COM_LOGOFF_ANDX, 2, NULL, (void **)&pSMB);
764 if (rc) {
765 mutex_unlock(&ses->session_mutex);
766 return rc;
769 pSMB->hdr.Mid = get_next_mid(ses->server);
771 if (ses->server->sign)
772 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
774 pSMB->hdr.Uid = ses->Suid;
776 pSMB->AndXCommand = 0xFF;
777 rc = SendReceiveNoRsp(xid, ses, (char *) pSMB, 0);
778 session_already_dead:
779 mutex_unlock(&ses->session_mutex);
781 /* if session dead then we do not need to do ulogoff,
782 since server closed smb session, no sense reporting
783 error */
784 if (rc == -EAGAIN)
785 rc = 0;
786 return rc;
790 CIFSPOSIXDelFile(const unsigned int xid, struct cifs_tcon *tcon,
791 const char *fileName, __u16 type,
792 const struct nls_table *nls_codepage, int remap)
794 TRANSACTION2_SPI_REQ *pSMB = NULL;
795 TRANSACTION2_SPI_RSP *pSMBr = NULL;
796 struct unlink_psx_rq *pRqD;
797 int name_len;
798 int rc = 0;
799 int bytes_returned = 0;
800 __u16 params, param_offset, offset, byte_count;
802 cifs_dbg(FYI, "In POSIX delete\n");
803 PsxDelete:
804 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
805 (void **) &pSMBr);
806 if (rc)
807 return rc;
809 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
810 name_len =
811 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
812 PATH_MAX, nls_codepage, remap);
813 name_len++; /* trailing null */
814 name_len *= 2;
815 } else { /* BB add path length overrun check */
816 name_len = strnlen(fileName, PATH_MAX);
817 name_len++; /* trailing null */
818 strncpy(pSMB->FileName, fileName, name_len);
821 params = 6 + name_len;
822 pSMB->MaxParameterCount = cpu_to_le16(2);
823 pSMB->MaxDataCount = 0; /* BB double check this with jra */
824 pSMB->MaxSetupCount = 0;
825 pSMB->Reserved = 0;
826 pSMB->Flags = 0;
827 pSMB->Timeout = 0;
828 pSMB->Reserved2 = 0;
829 param_offset = offsetof(struct smb_com_transaction2_spi_req,
830 InformationLevel) - 4;
831 offset = param_offset + params;
833 /* Setup pointer to Request Data (inode type) */
834 pRqD = (struct unlink_psx_rq *)(((char *)&pSMB->hdr.Protocol) + offset);
835 pRqD->type = cpu_to_le16(type);
836 pSMB->ParameterOffset = cpu_to_le16(param_offset);
837 pSMB->DataOffset = cpu_to_le16(offset);
838 pSMB->SetupCount = 1;
839 pSMB->Reserved3 = 0;
840 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
841 byte_count = 3 /* pad */ + params + sizeof(struct unlink_psx_rq);
843 pSMB->DataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
844 pSMB->TotalDataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
845 pSMB->ParameterCount = cpu_to_le16(params);
846 pSMB->TotalParameterCount = pSMB->ParameterCount;
847 pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_UNLINK);
848 pSMB->Reserved4 = 0;
849 inc_rfc1001_len(pSMB, byte_count);
850 pSMB->ByteCount = cpu_to_le16(byte_count);
851 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
852 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
853 if (rc)
854 cifs_dbg(FYI, "Posix delete returned %d\n", rc);
855 cifs_buf_release(pSMB);
857 cifs_stats_inc(&tcon->stats.cifs_stats.num_deletes);
859 if (rc == -EAGAIN)
860 goto PsxDelete;
862 return rc;
866 CIFSSMBDelFile(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
867 struct cifs_sb_info *cifs_sb)
869 DELETE_FILE_REQ *pSMB = NULL;
870 DELETE_FILE_RSP *pSMBr = NULL;
871 int rc = 0;
872 int bytes_returned;
873 int name_len;
874 int remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR;
876 DelFileRetry:
877 rc = smb_init(SMB_COM_DELETE, 1, tcon, (void **) &pSMB,
878 (void **) &pSMBr);
879 if (rc)
880 return rc;
882 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
883 name_len = cifsConvertToUTF16((__le16 *) pSMB->fileName, name,
884 PATH_MAX, cifs_sb->local_nls,
885 remap);
886 name_len++; /* trailing null */
887 name_len *= 2;
888 } else { /* BB improve check for buffer overruns BB */
889 name_len = strnlen(name, PATH_MAX);
890 name_len++; /* trailing null */
891 strncpy(pSMB->fileName, name, name_len);
893 pSMB->SearchAttributes =
894 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM);
895 pSMB->BufferFormat = 0x04;
896 inc_rfc1001_len(pSMB, name_len + 1);
897 pSMB->ByteCount = cpu_to_le16(name_len + 1);
898 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
899 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
900 cifs_stats_inc(&tcon->stats.cifs_stats.num_deletes);
901 if (rc)
902 cifs_dbg(FYI, "Error in RMFile = %d\n", rc);
904 cifs_buf_release(pSMB);
905 if (rc == -EAGAIN)
906 goto DelFileRetry;
908 return rc;
912 CIFSSMBRmDir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
913 struct cifs_sb_info *cifs_sb)
915 DELETE_DIRECTORY_REQ *pSMB = NULL;
916 DELETE_DIRECTORY_RSP *pSMBr = NULL;
917 int rc = 0;
918 int bytes_returned;
919 int name_len;
920 int remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR;
922 cifs_dbg(FYI, "In CIFSSMBRmDir\n");
923 RmDirRetry:
924 rc = smb_init(SMB_COM_DELETE_DIRECTORY, 0, tcon, (void **) &pSMB,
925 (void **) &pSMBr);
926 if (rc)
927 return rc;
929 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
930 name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, name,
931 PATH_MAX, cifs_sb->local_nls,
932 remap);
933 name_len++; /* trailing null */
934 name_len *= 2;
935 } else { /* BB improve check for buffer overruns BB */
936 name_len = strnlen(name, PATH_MAX);
937 name_len++; /* trailing null */
938 strncpy(pSMB->DirName, name, name_len);
941 pSMB->BufferFormat = 0x04;
942 inc_rfc1001_len(pSMB, name_len + 1);
943 pSMB->ByteCount = cpu_to_le16(name_len + 1);
944 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
945 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
946 cifs_stats_inc(&tcon->stats.cifs_stats.num_rmdirs);
947 if (rc)
948 cifs_dbg(FYI, "Error in RMDir = %d\n", rc);
950 cifs_buf_release(pSMB);
951 if (rc == -EAGAIN)
952 goto RmDirRetry;
953 return rc;
957 CIFSSMBMkDir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
958 struct cifs_sb_info *cifs_sb)
960 int rc = 0;
961 CREATE_DIRECTORY_REQ *pSMB = NULL;
962 CREATE_DIRECTORY_RSP *pSMBr = NULL;
963 int bytes_returned;
964 int name_len;
965 int remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR;
967 cifs_dbg(FYI, "In CIFSSMBMkDir\n");
968 MkDirRetry:
969 rc = smb_init(SMB_COM_CREATE_DIRECTORY, 0, tcon, (void **) &pSMB,
970 (void **) &pSMBr);
971 if (rc)
972 return rc;
974 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
975 name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, name,
976 PATH_MAX, cifs_sb->local_nls,
977 remap);
978 name_len++; /* trailing null */
979 name_len *= 2;
980 } else { /* BB improve check for buffer overruns BB */
981 name_len = strnlen(name, PATH_MAX);
982 name_len++; /* trailing null */
983 strncpy(pSMB->DirName, name, name_len);
986 pSMB->BufferFormat = 0x04;
987 inc_rfc1001_len(pSMB, name_len + 1);
988 pSMB->ByteCount = cpu_to_le16(name_len + 1);
989 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
990 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
991 cifs_stats_inc(&tcon->stats.cifs_stats.num_mkdirs);
992 if (rc)
993 cifs_dbg(FYI, "Error in Mkdir = %d\n", rc);
995 cifs_buf_release(pSMB);
996 if (rc == -EAGAIN)
997 goto MkDirRetry;
998 return rc;
1002 CIFSPOSIXCreate(const unsigned int xid, struct cifs_tcon *tcon,
1003 __u32 posix_flags, __u64 mode, __u16 *netfid,
1004 FILE_UNIX_BASIC_INFO *pRetData, __u32 *pOplock,
1005 const char *name, const struct nls_table *nls_codepage,
1006 int remap)
1008 TRANSACTION2_SPI_REQ *pSMB = NULL;
1009 TRANSACTION2_SPI_RSP *pSMBr = NULL;
1010 int name_len;
1011 int rc = 0;
1012 int bytes_returned = 0;
1013 __u16 params, param_offset, offset, byte_count, count;
1014 OPEN_PSX_REQ *pdata;
1015 OPEN_PSX_RSP *psx_rsp;
1017 cifs_dbg(FYI, "In POSIX Create\n");
1018 PsxCreat:
1019 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
1020 (void **) &pSMBr);
1021 if (rc)
1022 return rc;
1024 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1025 name_len =
1026 cifsConvertToUTF16((__le16 *) pSMB->FileName, name,
1027 PATH_MAX, nls_codepage, remap);
1028 name_len++; /* trailing null */
1029 name_len *= 2;
1030 } else { /* BB improve the check for buffer overruns BB */
1031 name_len = strnlen(name, PATH_MAX);
1032 name_len++; /* trailing null */
1033 strncpy(pSMB->FileName, name, name_len);
1036 params = 6 + name_len;
1037 count = sizeof(OPEN_PSX_REQ);
1038 pSMB->MaxParameterCount = cpu_to_le16(2);
1039 pSMB->MaxDataCount = cpu_to_le16(1000); /* large enough */
1040 pSMB->MaxSetupCount = 0;
1041 pSMB->Reserved = 0;
1042 pSMB->Flags = 0;
1043 pSMB->Timeout = 0;
1044 pSMB->Reserved2 = 0;
1045 param_offset = offsetof(struct smb_com_transaction2_spi_req,
1046 InformationLevel) - 4;
1047 offset = param_offset + params;
1048 pdata = (OPEN_PSX_REQ *)(((char *)&pSMB->hdr.Protocol) + offset);
1049 pdata->Level = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
1050 pdata->Permissions = cpu_to_le64(mode);
1051 pdata->PosixOpenFlags = cpu_to_le32(posix_flags);
1052 pdata->OpenFlags = cpu_to_le32(*pOplock);
1053 pSMB->ParameterOffset = cpu_to_le16(param_offset);
1054 pSMB->DataOffset = cpu_to_le16(offset);
1055 pSMB->SetupCount = 1;
1056 pSMB->Reserved3 = 0;
1057 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
1058 byte_count = 3 /* pad */ + params + count;
1060 pSMB->DataCount = cpu_to_le16(count);
1061 pSMB->ParameterCount = cpu_to_le16(params);
1062 pSMB->TotalDataCount = pSMB->DataCount;
1063 pSMB->TotalParameterCount = pSMB->ParameterCount;
1064 pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_OPEN);
1065 pSMB->Reserved4 = 0;
1066 inc_rfc1001_len(pSMB, byte_count);
1067 pSMB->ByteCount = cpu_to_le16(byte_count);
1068 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1069 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
1070 if (rc) {
1071 cifs_dbg(FYI, "Posix create returned %d\n", rc);
1072 goto psx_create_err;
1075 cifs_dbg(FYI, "copying inode info\n");
1076 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
1078 if (rc || get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)) {
1079 rc = -EIO; /* bad smb */
1080 goto psx_create_err;
1083 /* copy return information to pRetData */
1084 psx_rsp = (OPEN_PSX_RSP *)((char *) &pSMBr->hdr.Protocol
1085 + le16_to_cpu(pSMBr->t2.DataOffset));
1087 *pOplock = le16_to_cpu(psx_rsp->OplockFlags);
1088 if (netfid)
1089 *netfid = psx_rsp->Fid; /* cifs fid stays in le */
1090 /* Let caller know file was created so we can set the mode. */
1091 /* Do we care about the CreateAction in any other cases? */
1092 if (cpu_to_le32(FILE_CREATE) == psx_rsp->CreateAction)
1093 *pOplock |= CIFS_CREATE_ACTION;
1094 /* check to make sure response data is there */
1095 if (psx_rsp->ReturnedLevel != cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC)) {
1096 pRetData->Type = cpu_to_le32(-1); /* unknown */
1097 cifs_dbg(NOISY, "unknown type\n");
1098 } else {
1099 if (get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)
1100 + sizeof(FILE_UNIX_BASIC_INFO)) {
1101 cifs_dbg(VFS, "Open response data too small\n");
1102 pRetData->Type = cpu_to_le32(-1);
1103 goto psx_create_err;
1105 memcpy((char *) pRetData,
1106 (char *)psx_rsp + sizeof(OPEN_PSX_RSP),
1107 sizeof(FILE_UNIX_BASIC_INFO));
1110 psx_create_err:
1111 cifs_buf_release(pSMB);
1113 if (posix_flags & SMB_O_DIRECTORY)
1114 cifs_stats_inc(&tcon->stats.cifs_stats.num_posixmkdirs);
1115 else
1116 cifs_stats_inc(&tcon->stats.cifs_stats.num_posixopens);
1118 if (rc == -EAGAIN)
1119 goto PsxCreat;
1121 return rc;
1124 static __u16 convert_disposition(int disposition)
1126 __u16 ofun = 0;
1128 switch (disposition) {
1129 case FILE_SUPERSEDE:
1130 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1131 break;
1132 case FILE_OPEN:
1133 ofun = SMBOPEN_OAPPEND;
1134 break;
1135 case FILE_CREATE:
1136 ofun = SMBOPEN_OCREATE;
1137 break;
1138 case FILE_OPEN_IF:
1139 ofun = SMBOPEN_OCREATE | SMBOPEN_OAPPEND;
1140 break;
1141 case FILE_OVERWRITE:
1142 ofun = SMBOPEN_OTRUNC;
1143 break;
1144 case FILE_OVERWRITE_IF:
1145 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1146 break;
1147 default:
1148 cifs_dbg(FYI, "unknown disposition %d\n", disposition);
1149 ofun = SMBOPEN_OAPPEND; /* regular open */
1151 return ofun;
1154 static int
1155 access_flags_to_smbopen_mode(const int access_flags)
1157 int masked_flags = access_flags & (GENERIC_READ | GENERIC_WRITE);
1159 if (masked_flags == GENERIC_READ)
1160 return SMBOPEN_READ;
1161 else if (masked_flags == GENERIC_WRITE)
1162 return SMBOPEN_WRITE;
1164 /* just go for read/write */
1165 return SMBOPEN_READWRITE;
1169 SMBLegacyOpen(const unsigned int xid, struct cifs_tcon *tcon,
1170 const char *fileName, const int openDisposition,
1171 const int access_flags, const int create_options, __u16 *netfid,
1172 int *pOplock, FILE_ALL_INFO *pfile_info,
1173 const struct nls_table *nls_codepage, int remap)
1175 int rc = -EACCES;
1176 OPENX_REQ *pSMB = NULL;
1177 OPENX_RSP *pSMBr = NULL;
1178 int bytes_returned;
1179 int name_len;
1180 __u16 count;
1182 OldOpenRetry:
1183 rc = smb_init(SMB_COM_OPEN_ANDX, 15, tcon, (void **) &pSMB,
1184 (void **) &pSMBr);
1185 if (rc)
1186 return rc;
1188 pSMB->AndXCommand = 0xFF; /* none */
1190 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1191 count = 1; /* account for one byte pad to word boundary */
1192 name_len =
1193 cifsConvertToUTF16((__le16 *) (pSMB->fileName + 1),
1194 fileName, PATH_MAX, nls_codepage, remap);
1195 name_len++; /* trailing null */
1196 name_len *= 2;
1197 } else { /* BB improve check for buffer overruns BB */
1198 count = 0; /* no pad */
1199 name_len = strnlen(fileName, PATH_MAX);
1200 name_len++; /* trailing null */
1201 strncpy(pSMB->fileName, fileName, name_len);
1203 if (*pOplock & REQ_OPLOCK)
1204 pSMB->OpenFlags = cpu_to_le16(REQ_OPLOCK);
1205 else if (*pOplock & REQ_BATCHOPLOCK)
1206 pSMB->OpenFlags = cpu_to_le16(REQ_BATCHOPLOCK);
1208 pSMB->OpenFlags |= cpu_to_le16(REQ_MORE_INFO);
1209 pSMB->Mode = cpu_to_le16(access_flags_to_smbopen_mode(access_flags));
1210 pSMB->Mode |= cpu_to_le16(0x40); /* deny none */
1211 /* set file as system file if special file such
1212 as fifo and server expecting SFU style and
1213 no Unix extensions */
1215 if (create_options & CREATE_OPTION_SPECIAL)
1216 pSMB->FileAttributes = cpu_to_le16(ATTR_SYSTEM);
1217 else /* BB FIXME BB */
1218 pSMB->FileAttributes = cpu_to_le16(0/*ATTR_NORMAL*/);
1220 if (create_options & CREATE_OPTION_READONLY)
1221 pSMB->FileAttributes |= cpu_to_le16(ATTR_READONLY);
1223 /* BB FIXME BB */
1224 /* pSMB->CreateOptions = cpu_to_le32(create_options &
1225 CREATE_OPTIONS_MASK); */
1226 /* BB FIXME END BB */
1228 pSMB->Sattr = cpu_to_le16(ATTR_HIDDEN | ATTR_SYSTEM | ATTR_DIRECTORY);
1229 pSMB->OpenFunction = cpu_to_le16(convert_disposition(openDisposition));
1230 count += name_len;
1231 inc_rfc1001_len(pSMB, count);
1233 pSMB->ByteCount = cpu_to_le16(count);
1234 /* long_op set to 1 to allow for oplock break timeouts */
1235 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1236 (struct smb_hdr *)pSMBr, &bytes_returned, 0);
1237 cifs_stats_inc(&tcon->stats.cifs_stats.num_opens);
1238 if (rc) {
1239 cifs_dbg(FYI, "Error in Open = %d\n", rc);
1240 } else {
1241 /* BB verify if wct == 15 */
1243 /* *pOplock = pSMBr->OplockLevel; */ /* BB take from action field*/
1245 *netfid = pSMBr->Fid; /* cifs fid stays in le */
1246 /* Let caller know file was created so we can set the mode. */
1247 /* Do we care about the CreateAction in any other cases? */
1248 /* BB FIXME BB */
1249 /* if (cpu_to_le32(FILE_CREATE) == pSMBr->CreateAction)
1250 *pOplock |= CIFS_CREATE_ACTION; */
1251 /* BB FIXME END */
1253 if (pfile_info) {
1254 pfile_info->CreationTime = 0; /* BB convert CreateTime*/
1255 pfile_info->LastAccessTime = 0; /* BB fixme */
1256 pfile_info->LastWriteTime = 0; /* BB fixme */
1257 pfile_info->ChangeTime = 0; /* BB fixme */
1258 pfile_info->Attributes =
1259 cpu_to_le32(le16_to_cpu(pSMBr->FileAttributes));
1260 /* the file_info buf is endian converted by caller */
1261 pfile_info->AllocationSize =
1262 cpu_to_le64(le32_to_cpu(pSMBr->EndOfFile));
1263 pfile_info->EndOfFile = pfile_info->AllocationSize;
1264 pfile_info->NumberOfLinks = cpu_to_le32(1);
1265 pfile_info->DeletePending = 0;
1269 cifs_buf_release(pSMB);
1270 if (rc == -EAGAIN)
1271 goto OldOpenRetry;
1272 return rc;
1276 CIFSSMBOpen(const unsigned int xid, struct cifs_tcon *tcon,
1277 const char *fileName, const int openDisposition,
1278 const int access_flags, const int create_options, __u16 *netfid,
1279 int *pOplock, FILE_ALL_INFO *pfile_info,
1280 const struct nls_table *nls_codepage, int remap)
1282 int rc = -EACCES;
1283 OPEN_REQ *pSMB = NULL;
1284 OPEN_RSP *pSMBr = NULL;
1285 int bytes_returned;
1286 int name_len;
1287 __u16 count;
1289 openRetry:
1290 rc = smb_init(SMB_COM_NT_CREATE_ANDX, 24, tcon, (void **) &pSMB,
1291 (void **) &pSMBr);
1292 if (rc)
1293 return rc;
1295 pSMB->AndXCommand = 0xFF; /* none */
1297 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1298 count = 1; /* account for one byte pad to word boundary */
1299 name_len =
1300 cifsConvertToUTF16((__le16 *) (pSMB->fileName + 1),
1301 fileName, PATH_MAX, nls_codepage, remap);
1302 name_len++; /* trailing null */
1303 name_len *= 2;
1304 pSMB->NameLength = cpu_to_le16(name_len);
1305 } else { /* BB improve check for buffer overruns BB */
1306 count = 0; /* no pad */
1307 name_len = strnlen(fileName, PATH_MAX);
1308 name_len++; /* trailing null */
1309 pSMB->NameLength = cpu_to_le16(name_len);
1310 strncpy(pSMB->fileName, fileName, name_len);
1312 if (*pOplock & REQ_OPLOCK)
1313 pSMB->OpenFlags = cpu_to_le32(REQ_OPLOCK);
1314 else if (*pOplock & REQ_BATCHOPLOCK)
1315 pSMB->OpenFlags = cpu_to_le32(REQ_BATCHOPLOCK);
1316 pSMB->DesiredAccess = cpu_to_le32(access_flags);
1317 pSMB->AllocationSize = 0;
1318 /* set file as system file if special file such
1319 as fifo and server expecting SFU style and
1320 no Unix extensions */
1321 if (create_options & CREATE_OPTION_SPECIAL)
1322 pSMB->FileAttributes = cpu_to_le32(ATTR_SYSTEM);
1323 else
1324 pSMB->FileAttributes = cpu_to_le32(ATTR_NORMAL);
1326 /* XP does not handle ATTR_POSIX_SEMANTICS */
1327 /* but it helps speed up case sensitive checks for other
1328 servers such as Samba */
1329 if (tcon->ses->capabilities & CAP_UNIX)
1330 pSMB->FileAttributes |= cpu_to_le32(ATTR_POSIX_SEMANTICS);
1332 if (create_options & CREATE_OPTION_READONLY)
1333 pSMB->FileAttributes |= cpu_to_le32(ATTR_READONLY);
1335 pSMB->ShareAccess = cpu_to_le32(FILE_SHARE_ALL);
1336 pSMB->CreateDisposition = cpu_to_le32(openDisposition);
1337 pSMB->CreateOptions = cpu_to_le32(create_options & CREATE_OPTIONS_MASK);
1338 /* BB Expirement with various impersonation levels and verify */
1339 pSMB->ImpersonationLevel = cpu_to_le32(SECURITY_IMPERSONATION);
1340 pSMB->SecurityFlags =
1341 SECURITY_CONTEXT_TRACKING | SECURITY_EFFECTIVE_ONLY;
1343 count += name_len;
1344 inc_rfc1001_len(pSMB, count);
1346 pSMB->ByteCount = cpu_to_le16(count);
1347 /* long_op set to 1 to allow for oplock break timeouts */
1348 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1349 (struct smb_hdr *)pSMBr, &bytes_returned, 0);
1350 cifs_stats_inc(&tcon->stats.cifs_stats.num_opens);
1351 if (rc) {
1352 cifs_dbg(FYI, "Error in Open = %d\n", rc);
1353 } else {
1354 *pOplock = pSMBr->OplockLevel; /* 1 byte no need to le_to_cpu */
1355 *netfid = pSMBr->Fid; /* cifs fid stays in le */
1356 /* Let caller know file was created so we can set the mode. */
1357 /* Do we care about the CreateAction in any other cases? */
1358 if (cpu_to_le32(FILE_CREATE) == pSMBr->CreateAction)
1359 *pOplock |= CIFS_CREATE_ACTION;
1360 if (pfile_info) {
1361 memcpy((char *)pfile_info, (char *)&pSMBr->CreationTime,
1362 36 /* CreationTime to Attributes */);
1363 /* the file_info buf is endian converted by caller */
1364 pfile_info->AllocationSize = pSMBr->AllocationSize;
1365 pfile_info->EndOfFile = pSMBr->EndOfFile;
1366 pfile_info->NumberOfLinks = cpu_to_le32(1);
1367 pfile_info->DeletePending = 0;
1371 cifs_buf_release(pSMB);
1372 if (rc == -EAGAIN)
1373 goto openRetry;
1374 return rc;
1378 * Discard any remaining data in the current SMB. To do this, we borrow the
1379 * current bigbuf.
1381 static int
1382 cifs_readv_discard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1384 unsigned int rfclen = get_rfc1002_length(server->smallbuf);
1385 int remaining = rfclen + 4 - server->total_read;
1386 struct cifs_readdata *rdata = mid->callback_data;
1388 while (remaining > 0) {
1389 int length;
1391 length = cifs_read_from_socket(server, server->bigbuf,
1392 min_t(unsigned int, remaining,
1393 CIFSMaxBufSize + MAX_HEADER_SIZE(server)));
1394 if (length < 0)
1395 return length;
1396 server->total_read += length;
1397 remaining -= length;
1400 dequeue_mid(mid, rdata->result);
1401 return 0;
1405 cifs_readv_receive(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1407 int length, len;
1408 unsigned int data_offset, data_len;
1409 struct cifs_readdata *rdata = mid->callback_data;
1410 char *buf = server->smallbuf;
1411 unsigned int buflen = get_rfc1002_length(buf) + 4;
1413 cifs_dbg(FYI, "%s: mid=%llu offset=%llu bytes=%u\n",
1414 __func__, mid->mid, rdata->offset, rdata->bytes);
1417 * read the rest of READ_RSP header (sans Data array), or whatever we
1418 * can if there's not enough data. At this point, we've read down to
1419 * the Mid.
1421 len = min_t(unsigned int, buflen, server->vals->read_rsp_size) -
1422 HEADER_SIZE(server) + 1;
1424 rdata->iov.iov_base = buf + HEADER_SIZE(server) - 1;
1425 rdata->iov.iov_len = len;
1427 length = cifs_readv_from_socket(server, &rdata->iov, 1, len);
1428 if (length < 0)
1429 return length;
1430 server->total_read += length;
1432 /* Was the SMB read successful? */
1433 rdata->result = server->ops->map_error(buf, false);
1434 if (rdata->result != 0) {
1435 cifs_dbg(FYI, "%s: server returned error %d\n",
1436 __func__, rdata->result);
1437 return cifs_readv_discard(server, mid);
1440 /* Is there enough to get to the rest of the READ_RSP header? */
1441 if (server->total_read < server->vals->read_rsp_size) {
1442 cifs_dbg(FYI, "%s: server returned short header. got=%u expected=%zu\n",
1443 __func__, server->total_read,
1444 server->vals->read_rsp_size);
1445 rdata->result = -EIO;
1446 return cifs_readv_discard(server, mid);
1449 data_offset = server->ops->read_data_offset(buf) + 4;
1450 if (data_offset < server->total_read) {
1452 * win2k8 sometimes sends an offset of 0 when the read
1453 * is beyond the EOF. Treat it as if the data starts just after
1454 * the header.
1456 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
1457 __func__, data_offset);
1458 data_offset = server->total_read;
1459 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
1460 /* data_offset is beyond the end of smallbuf */
1461 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
1462 __func__, data_offset);
1463 rdata->result = -EIO;
1464 return cifs_readv_discard(server, mid);
1467 cifs_dbg(FYI, "%s: total_read=%u data_offset=%u\n",
1468 __func__, server->total_read, data_offset);
1470 len = data_offset - server->total_read;
1471 if (len > 0) {
1472 /* read any junk before data into the rest of smallbuf */
1473 rdata->iov.iov_base = buf + server->total_read;
1474 rdata->iov.iov_len = len;
1475 length = cifs_readv_from_socket(server, &rdata->iov, 1, len);
1476 if (length < 0)
1477 return length;
1478 server->total_read += length;
1481 /* set up first iov for signature check */
1482 rdata->iov.iov_base = buf;
1483 rdata->iov.iov_len = server->total_read;
1484 cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
1485 rdata->iov.iov_base, rdata->iov.iov_len);
1487 /* how much data is in the response? */
1488 data_len = server->ops->read_data_length(buf);
1489 if (data_offset + data_len > buflen) {
1490 /* data_len is corrupt -- discard frame */
1491 rdata->result = -EIO;
1492 return cifs_readv_discard(server, mid);
1495 length = rdata->read_into_pages(server, rdata, data_len);
1496 if (length < 0)
1497 return length;
1499 server->total_read += length;
1500 rdata->bytes = length;
1502 cifs_dbg(FYI, "total_read=%u buflen=%u remaining=%u\n",
1503 server->total_read, buflen, data_len);
1505 /* discard anything left over */
1506 if (server->total_read < buflen)
1507 return cifs_readv_discard(server, mid);
1509 dequeue_mid(mid, false);
1510 return length;
1513 static void
1514 cifs_readv_callback(struct mid_q_entry *mid)
1516 struct cifs_readdata *rdata = mid->callback_data;
1517 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
1518 struct TCP_Server_Info *server = tcon->ses->server;
1519 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
1520 .rq_nvec = 1,
1521 .rq_pages = rdata->pages,
1522 .rq_npages = rdata->nr_pages,
1523 .rq_pagesz = rdata->pagesz,
1524 .rq_tailsz = rdata->tailsz };
1526 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
1527 __func__, mid->mid, mid->mid_state, rdata->result,
1528 rdata->bytes);
1530 switch (mid->mid_state) {
1531 case MID_RESPONSE_RECEIVED:
1532 /* result already set, check signature */
1533 if (server->sign) {
1534 int rc = 0;
1536 rc = cifs_verify_signature(&rqst, server,
1537 mid->sequence_number);
1538 if (rc)
1539 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
1540 rc);
1542 /* FIXME: should this be counted toward the initiating task? */
1543 task_io_account_read(rdata->bytes);
1544 cifs_stats_bytes_read(tcon, rdata->bytes);
1545 break;
1546 case MID_REQUEST_SUBMITTED:
1547 case MID_RETRY_NEEDED:
1548 rdata->result = -EAGAIN;
1549 break;
1550 default:
1551 rdata->result = -EIO;
1554 queue_work(cifsiod_wq, &rdata->work);
1555 DeleteMidQEntry(mid);
1556 add_credits(server, 1, 0);
1559 /* cifs_async_readv - send an async write, and set up mid to handle result */
1561 cifs_async_readv(struct cifs_readdata *rdata)
1563 int rc;
1564 READ_REQ *smb = NULL;
1565 int wct;
1566 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
1567 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
1568 .rq_nvec = 1 };
1570 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
1571 __func__, rdata->offset, rdata->bytes);
1573 if (tcon->ses->capabilities & CAP_LARGE_FILES)
1574 wct = 12;
1575 else {
1576 wct = 10; /* old style read */
1577 if ((rdata->offset >> 32) > 0) {
1578 /* can not handle this big offset for old */
1579 return -EIO;
1583 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **)&smb);
1584 if (rc)
1585 return rc;
1587 smb->hdr.Pid = cpu_to_le16((__u16)rdata->pid);
1588 smb->hdr.PidHigh = cpu_to_le16((__u16)(rdata->pid >> 16));
1590 smb->AndXCommand = 0xFF; /* none */
1591 smb->Fid = rdata->cfile->fid.netfid;
1592 smb->OffsetLow = cpu_to_le32(rdata->offset & 0xFFFFFFFF);
1593 if (wct == 12)
1594 smb->OffsetHigh = cpu_to_le32(rdata->offset >> 32);
1595 smb->Remaining = 0;
1596 smb->MaxCount = cpu_to_le16(rdata->bytes & 0xFFFF);
1597 smb->MaxCountHigh = cpu_to_le32(rdata->bytes >> 16);
1598 if (wct == 12)
1599 smb->ByteCount = 0;
1600 else {
1601 /* old style read */
1602 struct smb_com_readx_req *smbr =
1603 (struct smb_com_readx_req *)smb;
1604 smbr->ByteCount = 0;
1607 /* 4 for RFC1001 length + 1 for BCC */
1608 rdata->iov.iov_base = smb;
1609 rdata->iov.iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4;
1611 kref_get(&rdata->refcount);
1612 rc = cifs_call_async(tcon->ses->server, &rqst, cifs_readv_receive,
1613 cifs_readv_callback, rdata, 0);
1615 if (rc == 0)
1616 cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
1617 else
1618 kref_put(&rdata->refcount, cifs_readdata_release);
1620 cifs_small_buf_release(smb);
1621 return rc;
1625 CIFSSMBRead(const unsigned int xid, struct cifs_io_parms *io_parms,
1626 unsigned int *nbytes, char **buf, int *pbuf_type)
1628 int rc = -EACCES;
1629 READ_REQ *pSMB = NULL;
1630 READ_RSP *pSMBr = NULL;
1631 char *pReadData = NULL;
1632 int wct;
1633 int resp_buf_type = 0;
1634 struct kvec iov[1];
1635 __u32 pid = io_parms->pid;
1636 __u16 netfid = io_parms->netfid;
1637 __u64 offset = io_parms->offset;
1638 struct cifs_tcon *tcon = io_parms->tcon;
1639 unsigned int count = io_parms->length;
1641 cifs_dbg(FYI, "Reading %d bytes on fid %d\n", count, netfid);
1642 if (tcon->ses->capabilities & CAP_LARGE_FILES)
1643 wct = 12;
1644 else {
1645 wct = 10; /* old style read */
1646 if ((offset >> 32) > 0) {
1647 /* can not handle this big offset for old */
1648 return -EIO;
1652 *nbytes = 0;
1653 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **) &pSMB);
1654 if (rc)
1655 return rc;
1657 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1658 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1660 /* tcon and ses pointer are checked in smb_init */
1661 if (tcon->ses->server == NULL)
1662 return -ECONNABORTED;
1664 pSMB->AndXCommand = 0xFF; /* none */
1665 pSMB->Fid = netfid;
1666 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
1667 if (wct == 12)
1668 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
1670 pSMB->Remaining = 0;
1671 pSMB->MaxCount = cpu_to_le16(count & 0xFFFF);
1672 pSMB->MaxCountHigh = cpu_to_le32(count >> 16);
1673 if (wct == 12)
1674 pSMB->ByteCount = 0; /* no need to do le conversion since 0 */
1675 else {
1676 /* old style read */
1677 struct smb_com_readx_req *pSMBW =
1678 (struct smb_com_readx_req *)pSMB;
1679 pSMBW->ByteCount = 0;
1682 iov[0].iov_base = (char *)pSMB;
1683 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
1684 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
1685 &resp_buf_type, CIFS_LOG_ERROR);
1686 cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
1687 pSMBr = (READ_RSP *)iov[0].iov_base;
1688 if (rc) {
1689 cifs_dbg(VFS, "Send error in read = %d\n", rc);
1690 } else {
1691 int data_length = le16_to_cpu(pSMBr->DataLengthHigh);
1692 data_length = data_length << 16;
1693 data_length += le16_to_cpu(pSMBr->DataLength);
1694 *nbytes = data_length;
1696 /*check that DataLength would not go beyond end of SMB */
1697 if ((data_length > CIFSMaxBufSize)
1698 || (data_length > count)) {
1699 cifs_dbg(FYI, "bad length %d for count %d\n",
1700 data_length, count);
1701 rc = -EIO;
1702 *nbytes = 0;
1703 } else {
1704 pReadData = (char *) (&pSMBr->hdr.Protocol) +
1705 le16_to_cpu(pSMBr->DataOffset);
1706 /* if (rc = copy_to_user(buf, pReadData, data_length)) {
1707 cifs_dbg(VFS, "Faulting on read rc = %d\n",rc);
1708 rc = -EFAULT;
1709 }*/ /* can not use copy_to_user when using page cache*/
1710 if (*buf)
1711 memcpy(*buf, pReadData, data_length);
1715 /* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
1716 if (*buf) {
1717 if (resp_buf_type == CIFS_SMALL_BUFFER)
1718 cifs_small_buf_release(iov[0].iov_base);
1719 else if (resp_buf_type == CIFS_LARGE_BUFFER)
1720 cifs_buf_release(iov[0].iov_base);
1721 } else if (resp_buf_type != CIFS_NO_BUFFER) {
1722 /* return buffer to caller to free */
1723 *buf = iov[0].iov_base;
1724 if (resp_buf_type == CIFS_SMALL_BUFFER)
1725 *pbuf_type = CIFS_SMALL_BUFFER;
1726 else if (resp_buf_type == CIFS_LARGE_BUFFER)
1727 *pbuf_type = CIFS_LARGE_BUFFER;
1728 } /* else no valid buffer on return - leave as null */
1730 /* Note: On -EAGAIN error only caller can retry on handle based calls
1731 since file handle passed in no longer valid */
1732 return rc;
1737 CIFSSMBWrite(const unsigned int xid, struct cifs_io_parms *io_parms,
1738 unsigned int *nbytes, const char *buf,
1739 const char __user *ubuf, const int long_op)
1741 int rc = -EACCES;
1742 WRITE_REQ *pSMB = NULL;
1743 WRITE_RSP *pSMBr = NULL;
1744 int bytes_returned, wct;
1745 __u32 bytes_sent;
1746 __u16 byte_count;
1747 __u32 pid = io_parms->pid;
1748 __u16 netfid = io_parms->netfid;
1749 __u64 offset = io_parms->offset;
1750 struct cifs_tcon *tcon = io_parms->tcon;
1751 unsigned int count = io_parms->length;
1753 *nbytes = 0;
1755 /* cifs_dbg(FYI, "write at %lld %d bytes\n", offset, count);*/
1756 if (tcon->ses == NULL)
1757 return -ECONNABORTED;
1759 if (tcon->ses->capabilities & CAP_LARGE_FILES)
1760 wct = 14;
1761 else {
1762 wct = 12;
1763 if ((offset >> 32) > 0) {
1764 /* can not handle big offset for old srv */
1765 return -EIO;
1769 rc = smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB,
1770 (void **) &pSMBr);
1771 if (rc)
1772 return rc;
1774 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1775 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1777 /* tcon and ses pointer are checked in smb_init */
1778 if (tcon->ses->server == NULL)
1779 return -ECONNABORTED;
1781 pSMB->AndXCommand = 0xFF; /* none */
1782 pSMB->Fid = netfid;
1783 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
1784 if (wct == 14)
1785 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
1787 pSMB->Reserved = 0xFFFFFFFF;
1788 pSMB->WriteMode = 0;
1789 pSMB->Remaining = 0;
1791 /* Can increase buffer size if buffer is big enough in some cases ie we
1792 can send more if LARGE_WRITE_X capability returned by the server and if
1793 our buffer is big enough or if we convert to iovecs on socket writes
1794 and eliminate the copy to the CIFS buffer */
1795 if (tcon->ses->capabilities & CAP_LARGE_WRITE_X) {
1796 bytes_sent = min_t(const unsigned int, CIFSMaxBufSize, count);
1797 } else {
1798 bytes_sent = (tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE)
1799 & ~0xFF;
1802 if (bytes_sent > count)
1803 bytes_sent = count;
1804 pSMB->DataOffset =
1805 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
1806 if (buf)
1807 memcpy(pSMB->Data, buf, bytes_sent);
1808 else if (ubuf) {
1809 if (copy_from_user(pSMB->Data, ubuf, bytes_sent)) {
1810 cifs_buf_release(pSMB);
1811 return -EFAULT;
1813 } else if (count != 0) {
1814 /* No buffer */
1815 cifs_buf_release(pSMB);
1816 return -EINVAL;
1817 } /* else setting file size with write of zero bytes */
1818 if (wct == 14)
1819 byte_count = bytes_sent + 1; /* pad */
1820 else /* wct == 12 */
1821 byte_count = bytes_sent + 5; /* bigger pad, smaller smb hdr */
1823 pSMB->DataLengthLow = cpu_to_le16(bytes_sent & 0xFFFF);
1824 pSMB->DataLengthHigh = cpu_to_le16(bytes_sent >> 16);
1825 inc_rfc1001_len(pSMB, byte_count);
1827 if (wct == 14)
1828 pSMB->ByteCount = cpu_to_le16(byte_count);
1829 else { /* old style write has byte count 4 bytes earlier
1830 so 4 bytes pad */
1831 struct smb_com_writex_req *pSMBW =
1832 (struct smb_com_writex_req *)pSMB;
1833 pSMBW->ByteCount = cpu_to_le16(byte_count);
1836 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1837 (struct smb_hdr *) pSMBr, &bytes_returned, long_op);
1838 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
1839 if (rc) {
1840 cifs_dbg(FYI, "Send error in write = %d\n", rc);
1841 } else {
1842 *nbytes = le16_to_cpu(pSMBr->CountHigh);
1843 *nbytes = (*nbytes) << 16;
1844 *nbytes += le16_to_cpu(pSMBr->Count);
1847 * Mask off high 16 bits when bytes written as returned by the
1848 * server is greater than bytes requested by the client. Some
1849 * OS/2 servers are known to set incorrect CountHigh values.
1851 if (*nbytes > count)
1852 *nbytes &= 0xFFFF;
1855 cifs_buf_release(pSMB);
1857 /* Note: On -EAGAIN error only caller can retry on handle based calls
1858 since file handle passed in no longer valid */
1860 return rc;
1863 void
1864 cifs_writedata_release(struct kref *refcount)
1866 struct cifs_writedata *wdata = container_of(refcount,
1867 struct cifs_writedata, refcount);
1869 if (wdata->cfile)
1870 cifsFileInfo_put(wdata->cfile);
1872 kfree(wdata);
1876 * Write failed with a retryable error. Resend the write request. It's also
1877 * possible that the page was redirtied so re-clean the page.
1879 static void
1880 cifs_writev_requeue(struct cifs_writedata *wdata)
1882 int i, rc;
1883 struct inode *inode = wdata->cfile->dentry->d_inode;
1884 struct TCP_Server_Info *server;
1886 for (i = 0; i < wdata->nr_pages; i++) {
1887 lock_page(wdata->pages[i]);
1888 clear_page_dirty_for_io(wdata->pages[i]);
1891 do {
1892 server = tlink_tcon(wdata->cfile->tlink)->ses->server;
1893 rc = server->ops->async_writev(wdata);
1894 } while (rc == -EAGAIN);
1896 for (i = 0; i < wdata->nr_pages; i++) {
1897 unlock_page(wdata->pages[i]);
1898 if (rc != 0) {
1899 SetPageError(wdata->pages[i]);
1900 end_page_writeback(wdata->pages[i]);
1901 page_cache_release(wdata->pages[i]);
1905 mapping_set_error(inode->i_mapping, rc);
1906 kref_put(&wdata->refcount, cifs_writedata_release);
1909 void
1910 cifs_writev_complete(struct work_struct *work)
1912 struct cifs_writedata *wdata = container_of(work,
1913 struct cifs_writedata, work);
1914 struct inode *inode = wdata->cfile->dentry->d_inode;
1915 int i = 0;
1917 if (wdata->result == 0) {
1918 spin_lock(&inode->i_lock);
1919 cifs_update_eof(CIFS_I(inode), wdata->offset, wdata->bytes);
1920 spin_unlock(&inode->i_lock);
1921 cifs_stats_bytes_written(tlink_tcon(wdata->cfile->tlink),
1922 wdata->bytes);
1923 } else if (wdata->sync_mode == WB_SYNC_ALL && wdata->result == -EAGAIN)
1924 return cifs_writev_requeue(wdata);
1926 for (i = 0; i < wdata->nr_pages; i++) {
1927 struct page *page = wdata->pages[i];
1928 if (wdata->result == -EAGAIN)
1929 __set_page_dirty_nobuffers(page);
1930 else if (wdata->result < 0)
1931 SetPageError(page);
1932 end_page_writeback(page);
1933 page_cache_release(page);
1935 if (wdata->result != -EAGAIN)
1936 mapping_set_error(inode->i_mapping, wdata->result);
1937 kref_put(&wdata->refcount, cifs_writedata_release);
1940 struct cifs_writedata *
1941 cifs_writedata_alloc(unsigned int nr_pages, work_func_t complete)
1943 struct cifs_writedata *wdata;
1945 /* this would overflow */
1946 if (nr_pages == 0) {
1947 cifs_dbg(VFS, "%s: called with nr_pages == 0!\n", __func__);
1948 return NULL;
1951 /* writedata + number of page pointers */
1952 wdata = kzalloc(sizeof(*wdata) +
1953 sizeof(struct page *) * (nr_pages - 1), GFP_NOFS);
1954 if (wdata != NULL) {
1955 kref_init(&wdata->refcount);
1956 INIT_LIST_HEAD(&wdata->list);
1957 init_completion(&wdata->done);
1958 INIT_WORK(&wdata->work, complete);
1960 return wdata;
1964 * Check the mid_state and signature on received buffer (if any), and queue the
1965 * workqueue completion task.
1967 static void
1968 cifs_writev_callback(struct mid_q_entry *mid)
1970 struct cifs_writedata *wdata = mid->callback_data;
1971 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
1972 unsigned int written;
1973 WRITE_RSP *smb = (WRITE_RSP *)mid->resp_buf;
1975 switch (mid->mid_state) {
1976 case MID_RESPONSE_RECEIVED:
1977 wdata->result = cifs_check_receive(mid, tcon->ses->server, 0);
1978 if (wdata->result != 0)
1979 break;
1981 written = le16_to_cpu(smb->CountHigh);
1982 written <<= 16;
1983 written += le16_to_cpu(smb->Count);
1985 * Mask off high 16 bits when bytes written as returned
1986 * by the server is greater than bytes requested by the
1987 * client. OS/2 servers are known to set incorrect
1988 * CountHigh values.
1990 if (written > wdata->bytes)
1991 written &= 0xFFFF;
1993 if (written < wdata->bytes)
1994 wdata->result = -ENOSPC;
1995 else
1996 wdata->bytes = written;
1997 break;
1998 case MID_REQUEST_SUBMITTED:
1999 case MID_RETRY_NEEDED:
2000 wdata->result = -EAGAIN;
2001 break;
2002 default:
2003 wdata->result = -EIO;
2004 break;
2007 queue_work(cifsiod_wq, &wdata->work);
2008 DeleteMidQEntry(mid);
2009 add_credits(tcon->ses->server, 1, 0);
2012 /* cifs_async_writev - send an async write, and set up mid to handle result */
2014 cifs_async_writev(struct cifs_writedata *wdata)
2016 int rc = -EACCES;
2017 WRITE_REQ *smb = NULL;
2018 int wct;
2019 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2020 struct kvec iov;
2021 struct smb_rqst rqst = { };
2023 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
2024 wct = 14;
2025 } else {
2026 wct = 12;
2027 if (wdata->offset >> 32 > 0) {
2028 /* can not handle big offset for old srv */
2029 return -EIO;
2033 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **)&smb);
2034 if (rc)
2035 goto async_writev_out;
2037 smb->hdr.Pid = cpu_to_le16((__u16)wdata->pid);
2038 smb->hdr.PidHigh = cpu_to_le16((__u16)(wdata->pid >> 16));
2040 smb->AndXCommand = 0xFF; /* none */
2041 smb->Fid = wdata->cfile->fid.netfid;
2042 smb->OffsetLow = cpu_to_le32(wdata->offset & 0xFFFFFFFF);
2043 if (wct == 14)
2044 smb->OffsetHigh = cpu_to_le32(wdata->offset >> 32);
2045 smb->Reserved = 0xFFFFFFFF;
2046 smb->WriteMode = 0;
2047 smb->Remaining = 0;
2049 smb->DataOffset =
2050 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
2052 /* 4 for RFC1001 length + 1 for BCC */
2053 iov.iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4 + 1;
2054 iov.iov_base = smb;
2056 rqst.rq_iov = &iov;
2057 rqst.rq_nvec = 1;
2058 rqst.rq_pages = wdata->pages;
2059 rqst.rq_npages = wdata->nr_pages;
2060 rqst.rq_pagesz = wdata->pagesz;
2061 rqst.rq_tailsz = wdata->tailsz;
2063 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2064 wdata->offset, wdata->bytes);
2066 smb->DataLengthLow = cpu_to_le16(wdata->bytes & 0xFFFF);
2067 smb->DataLengthHigh = cpu_to_le16(wdata->bytes >> 16);
2069 if (wct == 14) {
2070 inc_rfc1001_len(&smb->hdr, wdata->bytes + 1);
2071 put_bcc(wdata->bytes + 1, &smb->hdr);
2072 } else {
2073 /* wct == 12 */
2074 struct smb_com_writex_req *smbw =
2075 (struct smb_com_writex_req *)smb;
2076 inc_rfc1001_len(&smbw->hdr, wdata->bytes + 5);
2077 put_bcc(wdata->bytes + 5, &smbw->hdr);
2078 iov.iov_len += 4; /* pad bigger by four bytes */
2081 kref_get(&wdata->refcount);
2082 rc = cifs_call_async(tcon->ses->server, &rqst, NULL,
2083 cifs_writev_callback, wdata, 0);
2085 if (rc == 0)
2086 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
2087 else
2088 kref_put(&wdata->refcount, cifs_writedata_release);
2090 async_writev_out:
2091 cifs_small_buf_release(smb);
2092 return rc;
2096 CIFSSMBWrite2(const unsigned int xid, struct cifs_io_parms *io_parms,
2097 unsigned int *nbytes, struct kvec *iov, int n_vec)
2099 int rc = -EACCES;
2100 WRITE_REQ *pSMB = NULL;
2101 int wct;
2102 int smb_hdr_len;
2103 int resp_buf_type = 0;
2104 __u32 pid = io_parms->pid;
2105 __u16 netfid = io_parms->netfid;
2106 __u64 offset = io_parms->offset;
2107 struct cifs_tcon *tcon = io_parms->tcon;
2108 unsigned int count = io_parms->length;
2110 *nbytes = 0;
2112 cifs_dbg(FYI, "write2 at %lld %d bytes\n", (long long)offset, count);
2114 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
2115 wct = 14;
2116 } else {
2117 wct = 12;
2118 if ((offset >> 32) > 0) {
2119 /* can not handle big offset for old srv */
2120 return -EIO;
2123 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB);
2124 if (rc)
2125 return rc;
2127 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
2128 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
2130 /* tcon and ses pointer are checked in smb_init */
2131 if (tcon->ses->server == NULL)
2132 return -ECONNABORTED;
2134 pSMB->AndXCommand = 0xFF; /* none */
2135 pSMB->Fid = netfid;
2136 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
2137 if (wct == 14)
2138 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
2139 pSMB->Reserved = 0xFFFFFFFF;
2140 pSMB->WriteMode = 0;
2141 pSMB->Remaining = 0;
2143 pSMB->DataOffset =
2144 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
2146 pSMB->DataLengthLow = cpu_to_le16(count & 0xFFFF);
2147 pSMB->DataLengthHigh = cpu_to_le16(count >> 16);
2148 /* header + 1 byte pad */
2149 smb_hdr_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 1;
2150 if (wct == 14)
2151 inc_rfc1001_len(pSMB, count + 1);
2152 else /* wct == 12 */
2153 inc_rfc1001_len(pSMB, count + 5); /* smb data starts later */
2154 if (wct == 14)
2155 pSMB->ByteCount = cpu_to_le16(count + 1);
2156 else /* wct == 12 */ /* bigger pad, smaller smb hdr, keep offset ok */ {
2157 struct smb_com_writex_req *pSMBW =
2158 (struct smb_com_writex_req *)pSMB;
2159 pSMBW->ByteCount = cpu_to_le16(count + 5);
2161 iov[0].iov_base = pSMB;
2162 if (wct == 14)
2163 iov[0].iov_len = smb_hdr_len + 4;
2164 else /* wct == 12 pad bigger by four bytes */
2165 iov[0].iov_len = smb_hdr_len + 8;
2168 rc = SendReceive2(xid, tcon->ses, iov, n_vec + 1, &resp_buf_type, 0);
2169 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
2170 if (rc) {
2171 cifs_dbg(FYI, "Send error Write2 = %d\n", rc);
2172 } else if (resp_buf_type == 0) {
2173 /* presumably this can not happen, but best to be safe */
2174 rc = -EIO;
2175 } else {
2176 WRITE_RSP *pSMBr = (WRITE_RSP *)iov[0].iov_base;
2177 *nbytes = le16_to_cpu(pSMBr->CountHigh);
2178 *nbytes = (*nbytes) << 16;
2179 *nbytes += le16_to_cpu(pSMBr->Count);
2182 * Mask off high 16 bits when bytes written as returned by the
2183 * server is greater than bytes requested by the client. OS/2
2184 * servers are known to set incorrect CountHigh values.
2186 if (*nbytes > count)
2187 *nbytes &= 0xFFFF;
2190 /* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
2191 if (resp_buf_type == CIFS_SMALL_BUFFER)
2192 cifs_small_buf_release(iov[0].iov_base);
2193 else if (resp_buf_type == CIFS_LARGE_BUFFER)
2194 cifs_buf_release(iov[0].iov_base);
2196 /* Note: On -EAGAIN error only caller can retry on handle based calls
2197 since file handle passed in no longer valid */
2199 return rc;
2202 int cifs_lockv(const unsigned int xid, struct cifs_tcon *tcon,
2203 const __u16 netfid, const __u8 lock_type, const __u32 num_unlock,
2204 const __u32 num_lock, LOCKING_ANDX_RANGE *buf)
2206 int rc = 0;
2207 LOCK_REQ *pSMB = NULL;
2208 struct kvec iov[2];
2209 int resp_buf_type;
2210 __u16 count;
2212 cifs_dbg(FYI, "cifs_lockv num lock %d num unlock %d\n",
2213 num_lock, num_unlock);
2215 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2216 if (rc)
2217 return rc;
2219 pSMB->Timeout = 0;
2220 pSMB->NumberOfLocks = cpu_to_le16(num_lock);
2221 pSMB->NumberOfUnlocks = cpu_to_le16(num_unlock);
2222 pSMB->LockType = lock_type;
2223 pSMB->AndXCommand = 0xFF; /* none */
2224 pSMB->Fid = netfid; /* netfid stays le */
2226 count = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2227 inc_rfc1001_len(pSMB, count);
2228 pSMB->ByteCount = cpu_to_le16(count);
2230 iov[0].iov_base = (char *)pSMB;
2231 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4 -
2232 (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2233 iov[1].iov_base = (char *)buf;
2234 iov[1].iov_len = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2236 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
2237 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP);
2238 if (rc)
2239 cifs_dbg(FYI, "Send error in cifs_lockv = %d\n", rc);
2241 return rc;
2245 CIFSSMBLock(const unsigned int xid, struct cifs_tcon *tcon,
2246 const __u16 smb_file_id, const __u32 netpid, const __u64 len,
2247 const __u64 offset, const __u32 numUnlock,
2248 const __u32 numLock, const __u8 lockType,
2249 const bool waitFlag, const __u8 oplock_level)
2251 int rc = 0;
2252 LOCK_REQ *pSMB = NULL;
2253 /* LOCK_RSP *pSMBr = NULL; */ /* No response data other than rc to parse */
2254 int bytes_returned;
2255 int flags = 0;
2256 __u16 count;
2258 cifs_dbg(FYI, "CIFSSMBLock timeout %d numLock %d\n",
2259 (int)waitFlag, numLock);
2260 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2262 if (rc)
2263 return rc;
2265 if (lockType == LOCKING_ANDX_OPLOCK_RELEASE) {
2266 /* no response expected */
2267 flags = CIFS_ASYNC_OP | CIFS_OBREAK_OP;
2268 pSMB->Timeout = 0;
2269 } else if (waitFlag) {
2270 flags = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
2271 pSMB->Timeout = cpu_to_le32(-1);/* blocking - do not time out */
2272 } else {
2273 pSMB->Timeout = 0;
2276 pSMB->NumberOfLocks = cpu_to_le16(numLock);
2277 pSMB->NumberOfUnlocks = cpu_to_le16(numUnlock);
2278 pSMB->LockType = lockType;
2279 pSMB->OplockLevel = oplock_level;
2280 pSMB->AndXCommand = 0xFF; /* none */
2281 pSMB->Fid = smb_file_id; /* netfid stays le */
2283 if ((numLock != 0) || (numUnlock != 0)) {
2284 pSMB->Locks[0].Pid = cpu_to_le16(netpid);
2285 /* BB where to store pid high? */
2286 pSMB->Locks[0].LengthLow = cpu_to_le32((u32)len);
2287 pSMB->Locks[0].LengthHigh = cpu_to_le32((u32)(len>>32));
2288 pSMB->Locks[0].OffsetLow = cpu_to_le32((u32)offset);
2289 pSMB->Locks[0].OffsetHigh = cpu_to_le32((u32)(offset>>32));
2290 count = sizeof(LOCKING_ANDX_RANGE);
2291 } else {
2292 /* oplock break */
2293 count = 0;
2295 inc_rfc1001_len(pSMB, count);
2296 pSMB->ByteCount = cpu_to_le16(count);
2298 if (waitFlag) {
2299 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
2300 (struct smb_hdr *) pSMB, &bytes_returned);
2301 cifs_small_buf_release(pSMB);
2302 } else {
2303 rc = SendReceiveNoRsp(xid, tcon->ses, (char *)pSMB, flags);
2304 /* SMB buffer freed by function above */
2306 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
2307 if (rc)
2308 cifs_dbg(FYI, "Send error in Lock = %d\n", rc);
2310 /* Note: On -EAGAIN error only caller can retry on handle based calls
2311 since file handle passed in no longer valid */
2312 return rc;
2316 CIFSSMBPosixLock(const unsigned int xid, struct cifs_tcon *tcon,
2317 const __u16 smb_file_id, const __u32 netpid,
2318 const loff_t start_offset, const __u64 len,
2319 struct file_lock *pLockData, const __u16 lock_type,
2320 const bool waitFlag)
2322 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2323 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
2324 struct cifs_posix_lock *parm_data;
2325 int rc = 0;
2326 int timeout = 0;
2327 int bytes_returned = 0;
2328 int resp_buf_type = 0;
2329 __u16 params, param_offset, offset, byte_count, count;
2330 struct kvec iov[1];
2332 cifs_dbg(FYI, "Posix Lock\n");
2334 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
2336 if (rc)
2337 return rc;
2339 pSMBr = (struct smb_com_transaction2_sfi_rsp *)pSMB;
2341 params = 6;
2342 pSMB->MaxSetupCount = 0;
2343 pSMB->Reserved = 0;
2344 pSMB->Flags = 0;
2345 pSMB->Reserved2 = 0;
2346 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2347 offset = param_offset + params;
2349 count = sizeof(struct cifs_posix_lock);
2350 pSMB->MaxParameterCount = cpu_to_le16(2);
2351 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
2352 pSMB->SetupCount = 1;
2353 pSMB->Reserved3 = 0;
2354 if (pLockData)
2355 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
2356 else
2357 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2358 byte_count = 3 /* pad */ + params + count;
2359 pSMB->DataCount = cpu_to_le16(count);
2360 pSMB->ParameterCount = cpu_to_le16(params);
2361 pSMB->TotalDataCount = pSMB->DataCount;
2362 pSMB->TotalParameterCount = pSMB->ParameterCount;
2363 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2364 parm_data = (struct cifs_posix_lock *)
2365 (((char *) &pSMB->hdr.Protocol) + offset);
2367 parm_data->lock_type = cpu_to_le16(lock_type);
2368 if (waitFlag) {
2369 timeout = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
2370 parm_data->lock_flags = cpu_to_le16(1);
2371 pSMB->Timeout = cpu_to_le32(-1);
2372 } else
2373 pSMB->Timeout = 0;
2375 parm_data->pid = cpu_to_le32(netpid);
2376 parm_data->start = cpu_to_le64(start_offset);
2377 parm_data->length = cpu_to_le64(len); /* normalize negative numbers */
2379 pSMB->DataOffset = cpu_to_le16(offset);
2380 pSMB->Fid = smb_file_id;
2381 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_LOCK);
2382 pSMB->Reserved4 = 0;
2383 inc_rfc1001_len(pSMB, byte_count);
2384 pSMB->ByteCount = cpu_to_le16(byte_count);
2385 if (waitFlag) {
2386 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
2387 (struct smb_hdr *) pSMBr, &bytes_returned);
2388 } else {
2389 iov[0].iov_base = (char *)pSMB;
2390 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
2391 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
2392 &resp_buf_type, timeout);
2393 pSMB = NULL; /* request buf already freed by SendReceive2. Do
2394 not try to free it twice below on exit */
2395 pSMBr = (struct smb_com_transaction2_sfi_rsp *)iov[0].iov_base;
2398 if (rc) {
2399 cifs_dbg(FYI, "Send error in Posix Lock = %d\n", rc);
2400 } else if (pLockData) {
2401 /* lock structure can be returned on get */
2402 __u16 data_offset;
2403 __u16 data_count;
2404 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
2406 if (rc || get_bcc(&pSMBr->hdr) < sizeof(*parm_data)) {
2407 rc = -EIO; /* bad smb */
2408 goto plk_err_exit;
2410 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
2411 data_count = le16_to_cpu(pSMBr->t2.DataCount);
2412 if (data_count < sizeof(struct cifs_posix_lock)) {
2413 rc = -EIO;
2414 goto plk_err_exit;
2416 parm_data = (struct cifs_posix_lock *)
2417 ((char *)&pSMBr->hdr.Protocol + data_offset);
2418 if (parm_data->lock_type == __constant_cpu_to_le16(CIFS_UNLCK))
2419 pLockData->fl_type = F_UNLCK;
2420 else {
2421 if (parm_data->lock_type ==
2422 __constant_cpu_to_le16(CIFS_RDLCK))
2423 pLockData->fl_type = F_RDLCK;
2424 else if (parm_data->lock_type ==
2425 __constant_cpu_to_le16(CIFS_WRLCK))
2426 pLockData->fl_type = F_WRLCK;
2428 pLockData->fl_start = le64_to_cpu(parm_data->start);
2429 pLockData->fl_end = pLockData->fl_start +
2430 le64_to_cpu(parm_data->length) - 1;
2431 pLockData->fl_pid = le32_to_cpu(parm_data->pid);
2435 plk_err_exit:
2436 if (pSMB)
2437 cifs_small_buf_release(pSMB);
2439 if (resp_buf_type == CIFS_SMALL_BUFFER)
2440 cifs_small_buf_release(iov[0].iov_base);
2441 else if (resp_buf_type == CIFS_LARGE_BUFFER)
2442 cifs_buf_release(iov[0].iov_base);
2444 /* Note: On -EAGAIN error only caller can retry on handle based calls
2445 since file handle passed in no longer valid */
2447 return rc;
2452 CIFSSMBClose(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
2454 int rc = 0;
2455 CLOSE_REQ *pSMB = NULL;
2456 cifs_dbg(FYI, "In CIFSSMBClose\n");
2458 /* do not retry on dead session on close */
2459 rc = small_smb_init(SMB_COM_CLOSE, 3, tcon, (void **) &pSMB);
2460 if (rc == -EAGAIN)
2461 return 0;
2462 if (rc)
2463 return rc;
2465 pSMB->FileID = (__u16) smb_file_id;
2466 pSMB->LastWriteTime = 0xFFFFFFFF;
2467 pSMB->ByteCount = 0;
2468 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
2469 cifs_stats_inc(&tcon->stats.cifs_stats.num_closes);
2470 if (rc) {
2471 if (rc != -EINTR) {
2472 /* EINTR is expected when user ctl-c to kill app */
2473 cifs_dbg(VFS, "Send error in Close = %d\n", rc);
2477 /* Since session is dead, file will be closed on server already */
2478 if (rc == -EAGAIN)
2479 rc = 0;
2481 return rc;
2485 CIFSSMBFlush(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
2487 int rc = 0;
2488 FLUSH_REQ *pSMB = NULL;
2489 cifs_dbg(FYI, "In CIFSSMBFlush\n");
2491 rc = small_smb_init(SMB_COM_FLUSH, 1, tcon, (void **) &pSMB);
2492 if (rc)
2493 return rc;
2495 pSMB->FileID = (__u16) smb_file_id;
2496 pSMB->ByteCount = 0;
2497 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
2498 cifs_stats_inc(&tcon->stats.cifs_stats.num_flushes);
2499 if (rc)
2500 cifs_dbg(VFS, "Send error in Flush = %d\n", rc);
2502 return rc;
2506 CIFSSMBRename(const unsigned int xid, struct cifs_tcon *tcon,
2507 const char *from_name, const char *to_name,
2508 struct cifs_sb_info *cifs_sb)
2510 int rc = 0;
2511 RENAME_REQ *pSMB = NULL;
2512 RENAME_RSP *pSMBr = NULL;
2513 int bytes_returned;
2514 int name_len, name_len2;
2515 __u16 count;
2516 int remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR;
2518 cifs_dbg(FYI, "In CIFSSMBRename\n");
2519 renameRetry:
2520 rc = smb_init(SMB_COM_RENAME, 1, tcon, (void **) &pSMB,
2521 (void **) &pSMBr);
2522 if (rc)
2523 return rc;
2525 pSMB->BufferFormat = 0x04;
2526 pSMB->SearchAttributes =
2527 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
2528 ATTR_DIRECTORY);
2530 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2531 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2532 from_name, PATH_MAX,
2533 cifs_sb->local_nls, remap);
2534 name_len++; /* trailing null */
2535 name_len *= 2;
2536 pSMB->OldFileName[name_len] = 0x04; /* pad */
2537 /* protocol requires ASCII signature byte on Unicode string */
2538 pSMB->OldFileName[name_len + 1] = 0x00;
2539 name_len2 =
2540 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2541 to_name, PATH_MAX, cifs_sb->local_nls,
2542 remap);
2543 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2544 name_len2 *= 2; /* convert to bytes */
2545 } else { /* BB improve the check for buffer overruns BB */
2546 name_len = strnlen(from_name, PATH_MAX);
2547 name_len++; /* trailing null */
2548 strncpy(pSMB->OldFileName, from_name, name_len);
2549 name_len2 = strnlen(to_name, PATH_MAX);
2550 name_len2++; /* trailing null */
2551 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2552 strncpy(&pSMB->OldFileName[name_len + 1], to_name, name_len2);
2553 name_len2++; /* trailing null */
2554 name_len2++; /* signature byte */
2557 count = 1 /* 1st signature byte */ + name_len + name_len2;
2558 inc_rfc1001_len(pSMB, count);
2559 pSMB->ByteCount = cpu_to_le16(count);
2561 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2562 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2563 cifs_stats_inc(&tcon->stats.cifs_stats.num_renames);
2564 if (rc)
2565 cifs_dbg(FYI, "Send error in rename = %d\n", rc);
2567 cifs_buf_release(pSMB);
2569 if (rc == -EAGAIN)
2570 goto renameRetry;
2572 return rc;
2575 int CIFSSMBRenameOpenFile(const unsigned int xid, struct cifs_tcon *pTcon,
2576 int netfid, const char *target_name,
2577 const struct nls_table *nls_codepage, int remap)
2579 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2580 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
2581 struct set_file_rename *rename_info;
2582 char *data_offset;
2583 char dummy_string[30];
2584 int rc = 0;
2585 int bytes_returned = 0;
2586 int len_of_str;
2587 __u16 params, param_offset, offset, count, byte_count;
2589 cifs_dbg(FYI, "Rename to File by handle\n");
2590 rc = smb_init(SMB_COM_TRANSACTION2, 15, pTcon, (void **) &pSMB,
2591 (void **) &pSMBr);
2592 if (rc)
2593 return rc;
2595 params = 6;
2596 pSMB->MaxSetupCount = 0;
2597 pSMB->Reserved = 0;
2598 pSMB->Flags = 0;
2599 pSMB->Timeout = 0;
2600 pSMB->Reserved2 = 0;
2601 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2602 offset = param_offset + params;
2604 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2605 rename_info = (struct set_file_rename *) data_offset;
2606 pSMB->MaxParameterCount = cpu_to_le16(2);
2607 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
2608 pSMB->SetupCount = 1;
2609 pSMB->Reserved3 = 0;
2610 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2611 byte_count = 3 /* pad */ + params;
2612 pSMB->ParameterCount = cpu_to_le16(params);
2613 pSMB->TotalParameterCount = pSMB->ParameterCount;
2614 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2615 pSMB->DataOffset = cpu_to_le16(offset);
2616 /* construct random name ".cifs_tmp<inodenum><mid>" */
2617 rename_info->overwrite = cpu_to_le32(1);
2618 rename_info->root_fid = 0;
2619 /* unicode only call */
2620 if (target_name == NULL) {
2621 sprintf(dummy_string, "cifs%x", pSMB->hdr.Mid);
2622 len_of_str =
2623 cifsConvertToUTF16((__le16 *)rename_info->target_name,
2624 dummy_string, 24, nls_codepage, remap);
2625 } else {
2626 len_of_str =
2627 cifsConvertToUTF16((__le16 *)rename_info->target_name,
2628 target_name, PATH_MAX, nls_codepage,
2629 remap);
2631 rename_info->target_name_len = cpu_to_le32(2 * len_of_str);
2632 count = 12 /* sizeof(struct set_file_rename) */ + (2 * len_of_str);
2633 byte_count += count;
2634 pSMB->DataCount = cpu_to_le16(count);
2635 pSMB->TotalDataCount = pSMB->DataCount;
2636 pSMB->Fid = netfid;
2637 pSMB->InformationLevel =
2638 cpu_to_le16(SMB_SET_FILE_RENAME_INFORMATION);
2639 pSMB->Reserved4 = 0;
2640 inc_rfc1001_len(pSMB, byte_count);
2641 pSMB->ByteCount = cpu_to_le16(byte_count);
2642 rc = SendReceive(xid, pTcon->ses, (struct smb_hdr *) pSMB,
2643 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2644 cifs_stats_inc(&pTcon->stats.cifs_stats.num_t2renames);
2645 if (rc)
2646 cifs_dbg(FYI, "Send error in Rename (by file handle) = %d\n",
2647 rc);
2649 cifs_buf_release(pSMB);
2651 /* Note: On -EAGAIN error only caller can retry on handle based calls
2652 since file handle passed in no longer valid */
2654 return rc;
2658 CIFSSMBCopy(const unsigned int xid, struct cifs_tcon *tcon,
2659 const char *fromName, const __u16 target_tid, const char *toName,
2660 const int flags, const struct nls_table *nls_codepage, int remap)
2662 int rc = 0;
2663 COPY_REQ *pSMB = NULL;
2664 COPY_RSP *pSMBr = NULL;
2665 int bytes_returned;
2666 int name_len, name_len2;
2667 __u16 count;
2669 cifs_dbg(FYI, "In CIFSSMBCopy\n");
2670 copyRetry:
2671 rc = smb_init(SMB_COM_COPY, 1, tcon, (void **) &pSMB,
2672 (void **) &pSMBr);
2673 if (rc)
2674 return rc;
2676 pSMB->BufferFormat = 0x04;
2677 pSMB->Tid2 = target_tid;
2679 pSMB->Flags = cpu_to_le16(flags & COPY_TREE);
2681 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2682 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2683 fromName, PATH_MAX, nls_codepage,
2684 remap);
2685 name_len++; /* trailing null */
2686 name_len *= 2;
2687 pSMB->OldFileName[name_len] = 0x04; /* pad */
2688 /* protocol requires ASCII signature byte on Unicode string */
2689 pSMB->OldFileName[name_len + 1] = 0x00;
2690 name_len2 =
2691 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2692 toName, PATH_MAX, nls_codepage, remap);
2693 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2694 name_len2 *= 2; /* convert to bytes */
2695 } else { /* BB improve the check for buffer overruns BB */
2696 name_len = strnlen(fromName, PATH_MAX);
2697 name_len++; /* trailing null */
2698 strncpy(pSMB->OldFileName, fromName, name_len);
2699 name_len2 = strnlen(toName, PATH_MAX);
2700 name_len2++; /* trailing null */
2701 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2702 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
2703 name_len2++; /* trailing null */
2704 name_len2++; /* signature byte */
2707 count = 1 /* 1st signature byte */ + name_len + name_len2;
2708 inc_rfc1001_len(pSMB, count);
2709 pSMB->ByteCount = cpu_to_le16(count);
2711 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2712 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2713 if (rc) {
2714 cifs_dbg(FYI, "Send error in copy = %d with %d files copied\n",
2715 rc, le16_to_cpu(pSMBr->CopyCount));
2717 cifs_buf_release(pSMB);
2719 if (rc == -EAGAIN)
2720 goto copyRetry;
2722 return rc;
2726 CIFSUnixCreateSymLink(const unsigned int xid, struct cifs_tcon *tcon,
2727 const char *fromName, const char *toName,
2728 const struct nls_table *nls_codepage)
2730 TRANSACTION2_SPI_REQ *pSMB = NULL;
2731 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2732 char *data_offset;
2733 int name_len;
2734 int name_len_target;
2735 int rc = 0;
2736 int bytes_returned = 0;
2737 __u16 params, param_offset, offset, byte_count;
2739 cifs_dbg(FYI, "In Symlink Unix style\n");
2740 createSymLinkRetry:
2741 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2742 (void **) &pSMBr);
2743 if (rc)
2744 return rc;
2746 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2747 name_len =
2748 cifs_strtoUTF16((__le16 *) pSMB->FileName, fromName,
2749 /* find define for this maxpathcomponent */
2750 PATH_MAX, nls_codepage);
2751 name_len++; /* trailing null */
2752 name_len *= 2;
2754 } else { /* BB improve the check for buffer overruns BB */
2755 name_len = strnlen(fromName, PATH_MAX);
2756 name_len++; /* trailing null */
2757 strncpy(pSMB->FileName, fromName, name_len);
2759 params = 6 + name_len;
2760 pSMB->MaxSetupCount = 0;
2761 pSMB->Reserved = 0;
2762 pSMB->Flags = 0;
2763 pSMB->Timeout = 0;
2764 pSMB->Reserved2 = 0;
2765 param_offset = offsetof(struct smb_com_transaction2_spi_req,
2766 InformationLevel) - 4;
2767 offset = param_offset + params;
2769 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2770 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2771 name_len_target =
2772 cifs_strtoUTF16((__le16 *) data_offset, toName, PATH_MAX
2773 /* find define for this maxpathcomponent */
2774 , nls_codepage);
2775 name_len_target++; /* trailing null */
2776 name_len_target *= 2;
2777 } else { /* BB improve the check for buffer overruns BB */
2778 name_len_target = strnlen(toName, PATH_MAX);
2779 name_len_target++; /* trailing null */
2780 strncpy(data_offset, toName, name_len_target);
2783 pSMB->MaxParameterCount = cpu_to_le16(2);
2784 /* BB find exact max on data count below from sess */
2785 pSMB->MaxDataCount = cpu_to_le16(1000);
2786 pSMB->SetupCount = 1;
2787 pSMB->Reserved3 = 0;
2788 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2789 byte_count = 3 /* pad */ + params + name_len_target;
2790 pSMB->DataCount = cpu_to_le16(name_len_target);
2791 pSMB->ParameterCount = cpu_to_le16(params);
2792 pSMB->TotalDataCount = pSMB->DataCount;
2793 pSMB->TotalParameterCount = pSMB->ParameterCount;
2794 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2795 pSMB->DataOffset = cpu_to_le16(offset);
2796 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_LINK);
2797 pSMB->Reserved4 = 0;
2798 inc_rfc1001_len(pSMB, byte_count);
2799 pSMB->ByteCount = cpu_to_le16(byte_count);
2800 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2801 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2802 cifs_stats_inc(&tcon->stats.cifs_stats.num_symlinks);
2803 if (rc)
2804 cifs_dbg(FYI, "Send error in SetPathInfo create symlink = %d\n",
2805 rc);
2807 cifs_buf_release(pSMB);
2809 if (rc == -EAGAIN)
2810 goto createSymLinkRetry;
2812 return rc;
2816 CIFSUnixCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon,
2817 const char *fromName, const char *toName,
2818 const struct nls_table *nls_codepage, int remap)
2820 TRANSACTION2_SPI_REQ *pSMB = NULL;
2821 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2822 char *data_offset;
2823 int name_len;
2824 int name_len_target;
2825 int rc = 0;
2826 int bytes_returned = 0;
2827 __u16 params, param_offset, offset, byte_count;
2829 cifs_dbg(FYI, "In Create Hard link Unix style\n");
2830 createHardLinkRetry:
2831 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2832 (void **) &pSMBr);
2833 if (rc)
2834 return rc;
2836 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2837 name_len = cifsConvertToUTF16((__le16 *) pSMB->FileName, toName,
2838 PATH_MAX, nls_codepage, remap);
2839 name_len++; /* trailing null */
2840 name_len *= 2;
2842 } else { /* BB improve the check for buffer overruns BB */
2843 name_len = strnlen(toName, PATH_MAX);
2844 name_len++; /* trailing null */
2845 strncpy(pSMB->FileName, toName, name_len);
2847 params = 6 + name_len;
2848 pSMB->MaxSetupCount = 0;
2849 pSMB->Reserved = 0;
2850 pSMB->Flags = 0;
2851 pSMB->Timeout = 0;
2852 pSMB->Reserved2 = 0;
2853 param_offset = offsetof(struct smb_com_transaction2_spi_req,
2854 InformationLevel) - 4;
2855 offset = param_offset + params;
2857 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2858 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2859 name_len_target =
2860 cifsConvertToUTF16((__le16 *) data_offset, fromName,
2861 PATH_MAX, nls_codepage, remap);
2862 name_len_target++; /* trailing null */
2863 name_len_target *= 2;
2864 } else { /* BB improve the check for buffer overruns BB */
2865 name_len_target = strnlen(fromName, PATH_MAX);
2866 name_len_target++; /* trailing null */
2867 strncpy(data_offset, fromName, name_len_target);
2870 pSMB->MaxParameterCount = cpu_to_le16(2);
2871 /* BB find exact max on data count below from sess*/
2872 pSMB->MaxDataCount = cpu_to_le16(1000);
2873 pSMB->SetupCount = 1;
2874 pSMB->Reserved3 = 0;
2875 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2876 byte_count = 3 /* pad */ + params + name_len_target;
2877 pSMB->ParameterCount = cpu_to_le16(params);
2878 pSMB->TotalParameterCount = pSMB->ParameterCount;
2879 pSMB->DataCount = cpu_to_le16(name_len_target);
2880 pSMB->TotalDataCount = pSMB->DataCount;
2881 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2882 pSMB->DataOffset = cpu_to_le16(offset);
2883 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_HLINK);
2884 pSMB->Reserved4 = 0;
2885 inc_rfc1001_len(pSMB, byte_count);
2886 pSMB->ByteCount = cpu_to_le16(byte_count);
2887 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2888 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2889 cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
2890 if (rc)
2891 cifs_dbg(FYI, "Send error in SetPathInfo (hard link) = %d\n",
2892 rc);
2894 cifs_buf_release(pSMB);
2895 if (rc == -EAGAIN)
2896 goto createHardLinkRetry;
2898 return rc;
2902 CIFSCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon,
2903 const char *from_name, const char *to_name,
2904 struct cifs_sb_info *cifs_sb)
2906 int rc = 0;
2907 NT_RENAME_REQ *pSMB = NULL;
2908 RENAME_RSP *pSMBr = NULL;
2909 int bytes_returned;
2910 int name_len, name_len2;
2911 __u16 count;
2912 int remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR;
2914 cifs_dbg(FYI, "In CIFSCreateHardLink\n");
2915 winCreateHardLinkRetry:
2917 rc = smb_init(SMB_COM_NT_RENAME, 4, tcon, (void **) &pSMB,
2918 (void **) &pSMBr);
2919 if (rc)
2920 return rc;
2922 pSMB->SearchAttributes =
2923 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
2924 ATTR_DIRECTORY);
2925 pSMB->Flags = cpu_to_le16(CREATE_HARD_LINK);
2926 pSMB->ClusterCount = 0;
2928 pSMB->BufferFormat = 0x04;
2930 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2931 name_len =
2932 cifsConvertToUTF16((__le16 *) pSMB->OldFileName, from_name,
2933 PATH_MAX, cifs_sb->local_nls, remap);
2934 name_len++; /* trailing null */
2935 name_len *= 2;
2937 /* protocol specifies ASCII buffer format (0x04) for unicode */
2938 pSMB->OldFileName[name_len] = 0x04;
2939 pSMB->OldFileName[name_len + 1] = 0x00; /* pad */
2940 name_len2 =
2941 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2942 to_name, PATH_MAX, cifs_sb->local_nls,
2943 remap);
2944 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2945 name_len2 *= 2; /* convert to bytes */
2946 } else { /* BB improve the check for buffer overruns BB */
2947 name_len = strnlen(from_name, PATH_MAX);
2948 name_len++; /* trailing null */
2949 strncpy(pSMB->OldFileName, from_name, name_len);
2950 name_len2 = strnlen(to_name, PATH_MAX);
2951 name_len2++; /* trailing null */
2952 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2953 strncpy(&pSMB->OldFileName[name_len + 1], to_name, name_len2);
2954 name_len2++; /* trailing null */
2955 name_len2++; /* signature byte */
2958 count = 1 /* string type byte */ + name_len + name_len2;
2959 inc_rfc1001_len(pSMB, count);
2960 pSMB->ByteCount = cpu_to_le16(count);
2962 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2963 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2964 cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
2965 if (rc)
2966 cifs_dbg(FYI, "Send error in hard link (NT rename) = %d\n", rc);
2968 cifs_buf_release(pSMB);
2969 if (rc == -EAGAIN)
2970 goto winCreateHardLinkRetry;
2972 return rc;
2976 CIFSSMBUnixQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon,
2977 const unsigned char *searchName, char **symlinkinfo,
2978 const struct nls_table *nls_codepage)
2980 /* SMB_QUERY_FILE_UNIX_LINK */
2981 TRANSACTION2_QPI_REQ *pSMB = NULL;
2982 TRANSACTION2_QPI_RSP *pSMBr = NULL;
2983 int rc = 0;
2984 int bytes_returned;
2985 int name_len;
2986 __u16 params, byte_count;
2987 char *data_start;
2989 cifs_dbg(FYI, "In QPathSymLinkInfo (Unix) for path %s\n", searchName);
2991 querySymLinkRetry:
2992 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2993 (void **) &pSMBr);
2994 if (rc)
2995 return rc;
2997 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2998 name_len =
2999 cifs_strtoUTF16((__le16 *) pSMB->FileName, searchName,
3000 PATH_MAX, nls_codepage);
3001 name_len++; /* trailing null */
3002 name_len *= 2;
3003 } else { /* BB improve the check for buffer overruns BB */
3004 name_len = strnlen(searchName, PATH_MAX);
3005 name_len++; /* trailing null */
3006 strncpy(pSMB->FileName, searchName, name_len);
3009 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3010 pSMB->TotalDataCount = 0;
3011 pSMB->MaxParameterCount = cpu_to_le16(2);
3012 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
3013 pSMB->MaxSetupCount = 0;
3014 pSMB->Reserved = 0;
3015 pSMB->Flags = 0;
3016 pSMB->Timeout = 0;
3017 pSMB->Reserved2 = 0;
3018 pSMB->ParameterOffset = cpu_to_le16(offsetof(
3019 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
3020 pSMB->DataCount = 0;
3021 pSMB->DataOffset = 0;
3022 pSMB->SetupCount = 1;
3023 pSMB->Reserved3 = 0;
3024 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3025 byte_count = params + 1 /* pad */ ;
3026 pSMB->TotalParameterCount = cpu_to_le16(params);
3027 pSMB->ParameterCount = pSMB->TotalParameterCount;
3028 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_LINK);
3029 pSMB->Reserved4 = 0;
3030 inc_rfc1001_len(pSMB, byte_count);
3031 pSMB->ByteCount = cpu_to_le16(byte_count);
3033 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3034 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3035 if (rc) {
3036 cifs_dbg(FYI, "Send error in QuerySymLinkInfo = %d\n", rc);
3037 } else {
3038 /* decode response */
3040 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
3041 /* BB also check enough total bytes returned */
3042 if (rc || get_bcc(&pSMBr->hdr) < 2)
3043 rc = -EIO;
3044 else {
3045 bool is_unicode;
3046 u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3048 data_start = ((char *) &pSMBr->hdr.Protocol) +
3049 le16_to_cpu(pSMBr->t2.DataOffset);
3051 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3052 is_unicode = true;
3053 else
3054 is_unicode = false;
3056 /* BB FIXME investigate remapping reserved chars here */
3057 *symlinkinfo = cifs_strndup_from_utf16(data_start,
3058 count, is_unicode, nls_codepage);
3059 if (!*symlinkinfo)
3060 rc = -ENOMEM;
3063 cifs_buf_release(pSMB);
3064 if (rc == -EAGAIN)
3065 goto querySymLinkRetry;
3066 return rc;
3070 * Recent Windows versions now create symlinks more frequently
3071 * and they use the "reparse point" mechanism below. We can of course
3072 * do symlinks nicely to Samba and other servers which support the
3073 * CIFS Unix Extensions and we can also do SFU symlinks and "client only"
3074 * "MF" symlinks optionally, but for recent Windows we really need to
3075 * reenable the code below and fix the cifs_symlink callers to handle this.
3076 * In the interim this code has been moved to its own config option so
3077 * it is not compiled in by default until callers fixed up and more tested.
3080 CIFSSMBQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon,
3081 __u16 fid, char **symlinkinfo,
3082 const struct nls_table *nls_codepage)
3084 int rc = 0;
3085 int bytes_returned;
3086 struct smb_com_transaction_ioctl_req *pSMB;
3087 struct smb_com_transaction_ioctl_rsp *pSMBr;
3088 bool is_unicode;
3089 unsigned int sub_len;
3090 char *sub_start;
3091 struct reparse_symlink_data *reparse_buf;
3092 struct reparse_posix_data *posix_buf;
3093 __u32 data_offset, data_count;
3094 char *end_of_smb;
3096 cifs_dbg(FYI, "In Windows reparse style QueryLink for fid %u\n", fid);
3097 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3098 (void **) &pSMBr);
3099 if (rc)
3100 return rc;
3102 pSMB->TotalParameterCount = 0 ;
3103 pSMB->TotalDataCount = 0;
3104 pSMB->MaxParameterCount = cpu_to_le32(2);
3105 /* BB find exact data count max from sess structure BB */
3106 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
3107 pSMB->MaxSetupCount = 4;
3108 pSMB->Reserved = 0;
3109 pSMB->ParameterOffset = 0;
3110 pSMB->DataCount = 0;
3111 pSMB->DataOffset = 0;
3112 pSMB->SetupCount = 4;
3113 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
3114 pSMB->ParameterCount = pSMB->TotalParameterCount;
3115 pSMB->FunctionCode = cpu_to_le32(FSCTL_GET_REPARSE_POINT);
3116 pSMB->IsFsctl = 1; /* FSCTL */
3117 pSMB->IsRootFlag = 0;
3118 pSMB->Fid = fid; /* file handle always le */
3119 pSMB->ByteCount = 0;
3121 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3122 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3123 if (rc) {
3124 cifs_dbg(FYI, "Send error in QueryReparseLinkInfo = %d\n", rc);
3125 goto qreparse_out;
3128 data_offset = le32_to_cpu(pSMBr->DataOffset);
3129 data_count = le32_to_cpu(pSMBr->DataCount);
3130 if (get_bcc(&pSMBr->hdr) < 2 || data_offset > 512) {
3131 /* BB also check enough total bytes returned */
3132 rc = -EIO; /* bad smb */
3133 goto qreparse_out;
3135 if (!data_count || (data_count > 2048)) {
3136 rc = -EIO;
3137 cifs_dbg(FYI, "Invalid return data count on get reparse info ioctl\n");
3138 goto qreparse_out;
3140 end_of_smb = 2 + get_bcc(&pSMBr->hdr) + (char *)&pSMBr->ByteCount;
3141 reparse_buf = (struct reparse_symlink_data *)
3142 ((char *)&pSMBr->hdr.Protocol + data_offset);
3143 if ((char *)reparse_buf >= end_of_smb) {
3144 rc = -EIO;
3145 goto qreparse_out;
3147 if (reparse_buf->ReparseTag == cpu_to_le32(IO_REPARSE_TAG_NFS)) {
3148 cifs_dbg(FYI, "NFS style reparse tag\n");
3149 posix_buf = (struct reparse_posix_data *)reparse_buf;
3151 if (posix_buf->InodeType != cpu_to_le64(NFS_SPECFILE_LNK)) {
3152 cifs_dbg(FYI, "unsupported file type 0x%llx\n",
3153 le64_to_cpu(posix_buf->InodeType));
3154 rc = -EOPNOTSUPP;
3155 goto qreparse_out;
3157 is_unicode = true;
3158 sub_len = le16_to_cpu(reparse_buf->ReparseDataLength);
3159 if (posix_buf->PathBuffer + sub_len > end_of_smb) {
3160 cifs_dbg(FYI, "reparse buf beyond SMB\n");
3161 rc = -EIO;
3162 goto qreparse_out;
3164 *symlinkinfo = cifs_strndup_from_utf16(posix_buf->PathBuffer,
3165 sub_len, is_unicode, nls_codepage);
3166 goto qreparse_out;
3167 } else if (reparse_buf->ReparseTag !=
3168 cpu_to_le32(IO_REPARSE_TAG_SYMLINK)) {
3169 rc = -EOPNOTSUPP;
3170 goto qreparse_out;
3173 /* Reparse tag is NTFS symlink */
3174 sub_start = le16_to_cpu(reparse_buf->SubstituteNameOffset) +
3175 reparse_buf->PathBuffer;
3176 sub_len = le16_to_cpu(reparse_buf->SubstituteNameLength);
3177 if (sub_start + sub_len > end_of_smb) {
3178 cifs_dbg(FYI, "reparse buf beyond SMB\n");
3179 rc = -EIO;
3180 goto qreparse_out;
3182 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3183 is_unicode = true;
3184 else
3185 is_unicode = false;
3187 /* BB FIXME investigate remapping reserved chars here */
3188 *symlinkinfo = cifs_strndup_from_utf16(sub_start, sub_len, is_unicode,
3189 nls_codepage);
3190 if (!*symlinkinfo)
3191 rc = -ENOMEM;
3192 qreparse_out:
3193 cifs_buf_release(pSMB);
3196 * Note: On -EAGAIN error only caller can retry on handle based calls
3197 * since file handle passed in no longer valid.
3199 return rc;
3202 #ifdef CONFIG_CIFS_POSIX
3204 /*Convert an Access Control Entry from wire format to local POSIX xattr format*/
3205 static void cifs_convert_ace(posix_acl_xattr_entry *ace,
3206 struct cifs_posix_ace *cifs_ace)
3208 /* u8 cifs fields do not need le conversion */
3209 ace->e_perm = cpu_to_le16(cifs_ace->cifs_e_perm);
3210 ace->e_tag = cpu_to_le16(cifs_ace->cifs_e_tag);
3211 ace->e_id = cpu_to_le32(le64_to_cpu(cifs_ace->cifs_uid));
3213 cifs_dbg(FYI, "perm %d tag %d id %d\n",
3214 ace->e_perm, ace->e_tag, ace->e_id);
3217 return;
3220 /* Convert ACL from CIFS POSIX wire format to local Linux POSIX ACL xattr */
3221 static int cifs_copy_posix_acl(char *trgt, char *src, const int buflen,
3222 const int acl_type, const int size_of_data_area)
3224 int size = 0;
3225 int i;
3226 __u16 count;
3227 struct cifs_posix_ace *pACE;
3228 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)src;
3229 posix_acl_xattr_header *local_acl = (posix_acl_xattr_header *)trgt;
3231 if (le16_to_cpu(cifs_acl->version) != CIFS_ACL_VERSION)
3232 return -EOPNOTSUPP;
3234 if (acl_type & ACL_TYPE_ACCESS) {
3235 count = le16_to_cpu(cifs_acl->access_entry_count);
3236 pACE = &cifs_acl->ace_array[0];
3237 size = sizeof(struct cifs_posix_acl);
3238 size += sizeof(struct cifs_posix_ace) * count;
3239 /* check if we would go beyond end of SMB */
3240 if (size_of_data_area < size) {
3241 cifs_dbg(FYI, "bad CIFS POSIX ACL size %d vs. %d\n",
3242 size_of_data_area, size);
3243 return -EINVAL;
3245 } else if (acl_type & ACL_TYPE_DEFAULT) {
3246 count = le16_to_cpu(cifs_acl->access_entry_count);
3247 size = sizeof(struct cifs_posix_acl);
3248 size += sizeof(struct cifs_posix_ace) * count;
3249 /* skip past access ACEs to get to default ACEs */
3250 pACE = &cifs_acl->ace_array[count];
3251 count = le16_to_cpu(cifs_acl->default_entry_count);
3252 size += sizeof(struct cifs_posix_ace) * count;
3253 /* check if we would go beyond end of SMB */
3254 if (size_of_data_area < size)
3255 return -EINVAL;
3256 } else {
3257 /* illegal type */
3258 return -EINVAL;
3261 size = posix_acl_xattr_size(count);
3262 if ((buflen == 0) || (local_acl == NULL)) {
3263 /* used to query ACL EA size */
3264 } else if (size > buflen) {
3265 return -ERANGE;
3266 } else /* buffer big enough */ {
3267 local_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
3268 for (i = 0; i < count ; i++) {
3269 cifs_convert_ace(&local_acl->a_entries[i], pACE);
3270 pACE++;
3273 return size;
3276 static __u16 convert_ace_to_cifs_ace(struct cifs_posix_ace *cifs_ace,
3277 const posix_acl_xattr_entry *local_ace)
3279 __u16 rc = 0; /* 0 = ACL converted ok */
3281 cifs_ace->cifs_e_perm = le16_to_cpu(local_ace->e_perm);
3282 cifs_ace->cifs_e_tag = le16_to_cpu(local_ace->e_tag);
3283 /* BB is there a better way to handle the large uid? */
3284 if (local_ace->e_id == cpu_to_le32(-1)) {
3285 /* Probably no need to le convert -1 on any arch but can not hurt */
3286 cifs_ace->cifs_uid = cpu_to_le64(-1);
3287 } else
3288 cifs_ace->cifs_uid = cpu_to_le64(le32_to_cpu(local_ace->e_id));
3290 cifs_dbg(FYI, "perm %d tag %d id %d\n",
3291 ace->e_perm, ace->e_tag, ace->e_id);
3293 return rc;
3296 /* Convert ACL from local Linux POSIX xattr to CIFS POSIX ACL wire format */
3297 static __u16 ACL_to_cifs_posix(char *parm_data, const char *pACL,
3298 const int buflen, const int acl_type)
3300 __u16 rc = 0;
3301 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)parm_data;
3302 posix_acl_xattr_header *local_acl = (posix_acl_xattr_header *)pACL;
3303 int count;
3304 int i;
3306 if ((buflen == 0) || (pACL == NULL) || (cifs_acl == NULL))
3307 return 0;
3309 count = posix_acl_xattr_count((size_t)buflen);
3310 cifs_dbg(FYI, "setting acl with %d entries from buf of length %d and version of %d\n",
3311 count, buflen, le32_to_cpu(local_acl->a_version));
3312 if (le32_to_cpu(local_acl->a_version) != 2) {
3313 cifs_dbg(FYI, "unknown POSIX ACL version %d\n",
3314 le32_to_cpu(local_acl->a_version));
3315 return 0;
3317 cifs_acl->version = cpu_to_le16(1);
3318 if (acl_type == ACL_TYPE_ACCESS) {
3319 cifs_acl->access_entry_count = cpu_to_le16(count);
3320 cifs_acl->default_entry_count = __constant_cpu_to_le16(0xFFFF);
3321 } else if (acl_type == ACL_TYPE_DEFAULT) {
3322 cifs_acl->default_entry_count = cpu_to_le16(count);
3323 cifs_acl->access_entry_count = __constant_cpu_to_le16(0xFFFF);
3324 } else {
3325 cifs_dbg(FYI, "unknown ACL type %d\n", acl_type);
3326 return 0;
3328 for (i = 0; i < count; i++) {
3329 rc = convert_ace_to_cifs_ace(&cifs_acl->ace_array[i],
3330 &local_acl->a_entries[i]);
3331 if (rc != 0) {
3332 /* ACE not converted */
3333 break;
3336 if (rc == 0) {
3337 rc = (__u16)(count * sizeof(struct cifs_posix_ace));
3338 rc += sizeof(struct cifs_posix_acl);
3339 /* BB add check to make sure ACL does not overflow SMB */
3341 return rc;
3345 CIFSSMBGetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
3346 const unsigned char *searchName,
3347 char *acl_inf, const int buflen, const int acl_type,
3348 const struct nls_table *nls_codepage, int remap)
3350 /* SMB_QUERY_POSIX_ACL */
3351 TRANSACTION2_QPI_REQ *pSMB = NULL;
3352 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3353 int rc = 0;
3354 int bytes_returned;
3355 int name_len;
3356 __u16 params, byte_count;
3358 cifs_dbg(FYI, "In GetPosixACL (Unix) for path %s\n", searchName);
3360 queryAclRetry:
3361 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3362 (void **) &pSMBr);
3363 if (rc)
3364 return rc;
3366 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3367 name_len =
3368 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3369 searchName, PATH_MAX, nls_codepage,
3370 remap);
3371 name_len++; /* trailing null */
3372 name_len *= 2;
3373 pSMB->FileName[name_len] = 0;
3374 pSMB->FileName[name_len+1] = 0;
3375 } else { /* BB improve the check for buffer overruns BB */
3376 name_len = strnlen(searchName, PATH_MAX);
3377 name_len++; /* trailing null */
3378 strncpy(pSMB->FileName, searchName, name_len);
3381 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3382 pSMB->TotalDataCount = 0;
3383 pSMB->MaxParameterCount = cpu_to_le16(2);
3384 /* BB find exact max data count below from sess structure BB */
3385 pSMB->MaxDataCount = cpu_to_le16(4000);
3386 pSMB->MaxSetupCount = 0;
3387 pSMB->Reserved = 0;
3388 pSMB->Flags = 0;
3389 pSMB->Timeout = 0;
3390 pSMB->Reserved2 = 0;
3391 pSMB->ParameterOffset = cpu_to_le16(
3392 offsetof(struct smb_com_transaction2_qpi_req,
3393 InformationLevel) - 4);
3394 pSMB->DataCount = 0;
3395 pSMB->DataOffset = 0;
3396 pSMB->SetupCount = 1;
3397 pSMB->Reserved3 = 0;
3398 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3399 byte_count = params + 1 /* pad */ ;
3400 pSMB->TotalParameterCount = cpu_to_le16(params);
3401 pSMB->ParameterCount = pSMB->TotalParameterCount;
3402 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_ACL);
3403 pSMB->Reserved4 = 0;
3404 inc_rfc1001_len(pSMB, byte_count);
3405 pSMB->ByteCount = cpu_to_le16(byte_count);
3407 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3408 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3409 cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
3410 if (rc) {
3411 cifs_dbg(FYI, "Send error in Query POSIX ACL = %d\n", rc);
3412 } else {
3413 /* decode response */
3415 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
3416 /* BB also check enough total bytes returned */
3417 if (rc || get_bcc(&pSMBr->hdr) < 2)
3418 rc = -EIO; /* bad smb */
3419 else {
3420 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3421 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3422 rc = cifs_copy_posix_acl(acl_inf,
3423 (char *)&pSMBr->hdr.Protocol+data_offset,
3424 buflen, acl_type, count);
3427 cifs_buf_release(pSMB);
3428 if (rc == -EAGAIN)
3429 goto queryAclRetry;
3430 return rc;
3434 CIFSSMBSetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
3435 const unsigned char *fileName,
3436 const char *local_acl, const int buflen,
3437 const int acl_type,
3438 const struct nls_table *nls_codepage, int remap)
3440 struct smb_com_transaction2_spi_req *pSMB = NULL;
3441 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
3442 char *parm_data;
3443 int name_len;
3444 int rc = 0;
3445 int bytes_returned = 0;
3446 __u16 params, byte_count, data_count, param_offset, offset;
3448 cifs_dbg(FYI, "In SetPosixACL (Unix) for path %s\n", fileName);
3449 setAclRetry:
3450 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3451 (void **) &pSMBr);
3452 if (rc)
3453 return rc;
3454 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3455 name_len =
3456 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
3457 PATH_MAX, nls_codepage, remap);
3458 name_len++; /* trailing null */
3459 name_len *= 2;
3460 } else { /* BB improve the check for buffer overruns BB */
3461 name_len = strnlen(fileName, PATH_MAX);
3462 name_len++; /* trailing null */
3463 strncpy(pSMB->FileName, fileName, name_len);
3465 params = 6 + name_len;
3466 pSMB->MaxParameterCount = cpu_to_le16(2);
3467 /* BB find max SMB size from sess */
3468 pSMB->MaxDataCount = cpu_to_le16(1000);
3469 pSMB->MaxSetupCount = 0;
3470 pSMB->Reserved = 0;
3471 pSMB->Flags = 0;
3472 pSMB->Timeout = 0;
3473 pSMB->Reserved2 = 0;
3474 param_offset = offsetof(struct smb_com_transaction2_spi_req,
3475 InformationLevel) - 4;
3476 offset = param_offset + params;
3477 parm_data = ((char *) &pSMB->hdr.Protocol) + offset;
3478 pSMB->ParameterOffset = cpu_to_le16(param_offset);
3480 /* convert to on the wire format for POSIX ACL */
3481 data_count = ACL_to_cifs_posix(parm_data, local_acl, buflen, acl_type);
3483 if (data_count == 0) {
3484 rc = -EOPNOTSUPP;
3485 goto setACLerrorExit;
3487 pSMB->DataOffset = cpu_to_le16(offset);
3488 pSMB->SetupCount = 1;
3489 pSMB->Reserved3 = 0;
3490 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
3491 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_ACL);
3492 byte_count = 3 /* pad */ + params + data_count;
3493 pSMB->DataCount = cpu_to_le16(data_count);
3494 pSMB->TotalDataCount = pSMB->DataCount;
3495 pSMB->ParameterCount = cpu_to_le16(params);
3496 pSMB->TotalParameterCount = pSMB->ParameterCount;
3497 pSMB->Reserved4 = 0;
3498 inc_rfc1001_len(pSMB, byte_count);
3499 pSMB->ByteCount = cpu_to_le16(byte_count);
3500 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3501 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3502 if (rc)
3503 cifs_dbg(FYI, "Set POSIX ACL returned %d\n", rc);
3505 setACLerrorExit:
3506 cifs_buf_release(pSMB);
3507 if (rc == -EAGAIN)
3508 goto setAclRetry;
3509 return rc;
3512 /* BB fix tabs in this function FIXME BB */
3514 CIFSGetExtAttr(const unsigned int xid, struct cifs_tcon *tcon,
3515 const int netfid, __u64 *pExtAttrBits, __u64 *pMask)
3517 int rc = 0;
3518 struct smb_t2_qfi_req *pSMB = NULL;
3519 struct smb_t2_qfi_rsp *pSMBr = NULL;
3520 int bytes_returned;
3521 __u16 params, byte_count;
3523 cifs_dbg(FYI, "In GetExtAttr\n");
3524 if (tcon == NULL)
3525 return -ENODEV;
3527 GetExtAttrRetry:
3528 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3529 (void **) &pSMBr);
3530 if (rc)
3531 return rc;
3533 params = 2 /* level */ + 2 /* fid */;
3534 pSMB->t2.TotalDataCount = 0;
3535 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
3536 /* BB find exact max data count below from sess structure BB */
3537 pSMB->t2.MaxDataCount = cpu_to_le16(4000);
3538 pSMB->t2.MaxSetupCount = 0;
3539 pSMB->t2.Reserved = 0;
3540 pSMB->t2.Flags = 0;
3541 pSMB->t2.Timeout = 0;
3542 pSMB->t2.Reserved2 = 0;
3543 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
3544 Fid) - 4);
3545 pSMB->t2.DataCount = 0;
3546 pSMB->t2.DataOffset = 0;
3547 pSMB->t2.SetupCount = 1;
3548 pSMB->t2.Reserved3 = 0;
3549 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
3550 byte_count = params + 1 /* pad */ ;
3551 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
3552 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
3553 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_ATTR_FLAGS);
3554 pSMB->Pad = 0;
3555 pSMB->Fid = netfid;
3556 inc_rfc1001_len(pSMB, byte_count);
3557 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
3559 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3560 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3561 if (rc) {
3562 cifs_dbg(FYI, "error %d in GetExtAttr\n", rc);
3563 } else {
3564 /* decode response */
3565 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
3566 /* BB also check enough total bytes returned */
3567 if (rc || get_bcc(&pSMBr->hdr) < 2)
3568 /* If rc should we check for EOPNOSUPP and
3569 disable the srvino flag? or in caller? */
3570 rc = -EIO; /* bad smb */
3571 else {
3572 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3573 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3574 struct file_chattr_info *pfinfo;
3575 /* BB Do we need a cast or hash here ? */
3576 if (count != 16) {
3577 cifs_dbg(FYI, "Illegal size ret in GetExtAttr\n");
3578 rc = -EIO;
3579 goto GetExtAttrOut;
3581 pfinfo = (struct file_chattr_info *)
3582 (data_offset + (char *) &pSMBr->hdr.Protocol);
3583 *pExtAttrBits = le64_to_cpu(pfinfo->mode);
3584 *pMask = le64_to_cpu(pfinfo->mask);
3587 GetExtAttrOut:
3588 cifs_buf_release(pSMB);
3589 if (rc == -EAGAIN)
3590 goto GetExtAttrRetry;
3591 return rc;
3594 #endif /* CONFIG_POSIX */
3596 #ifdef CONFIG_CIFS_ACL
3598 * Initialize NT TRANSACT SMB into small smb request buffer. This assumes that
3599 * all NT TRANSACTS that we init here have total parm and data under about 400
3600 * bytes (to fit in small cifs buffer size), which is the case so far, it
3601 * easily fits. NB: Setup words themselves and ByteCount MaxSetupCount (size of
3602 * returned setup area) and MaxParameterCount (returned parms size) must be set
3603 * by caller
3605 static int
3606 smb_init_nttransact(const __u16 sub_command, const int setup_count,
3607 const int parm_len, struct cifs_tcon *tcon,
3608 void **ret_buf)
3610 int rc;
3611 __u32 temp_offset;
3612 struct smb_com_ntransact_req *pSMB;
3614 rc = small_smb_init(SMB_COM_NT_TRANSACT, 19 + setup_count, tcon,
3615 (void **)&pSMB);
3616 if (rc)
3617 return rc;
3618 *ret_buf = (void *)pSMB;
3619 pSMB->Reserved = 0;
3620 pSMB->TotalParameterCount = cpu_to_le32(parm_len);
3621 pSMB->TotalDataCount = 0;
3622 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
3623 pSMB->ParameterCount = pSMB->TotalParameterCount;
3624 pSMB->DataCount = pSMB->TotalDataCount;
3625 temp_offset = offsetof(struct smb_com_ntransact_req, Parms) +
3626 (setup_count * 2) - 4 /* for rfc1001 length itself */;
3627 pSMB->ParameterOffset = cpu_to_le32(temp_offset);
3628 pSMB->DataOffset = cpu_to_le32(temp_offset + parm_len);
3629 pSMB->SetupCount = setup_count; /* no need to le convert byte fields */
3630 pSMB->SubCommand = cpu_to_le16(sub_command);
3631 return 0;
3634 static int
3635 validate_ntransact(char *buf, char **ppparm, char **ppdata,
3636 __u32 *pparmlen, __u32 *pdatalen)
3638 char *end_of_smb;
3639 __u32 data_count, data_offset, parm_count, parm_offset;
3640 struct smb_com_ntransact_rsp *pSMBr;
3641 u16 bcc;
3643 *pdatalen = 0;
3644 *pparmlen = 0;
3646 if (buf == NULL)
3647 return -EINVAL;
3649 pSMBr = (struct smb_com_ntransact_rsp *)buf;
3651 bcc = get_bcc(&pSMBr->hdr);
3652 end_of_smb = 2 /* sizeof byte count */ + bcc +
3653 (char *)&pSMBr->ByteCount;
3655 data_offset = le32_to_cpu(pSMBr->DataOffset);
3656 data_count = le32_to_cpu(pSMBr->DataCount);
3657 parm_offset = le32_to_cpu(pSMBr->ParameterOffset);
3658 parm_count = le32_to_cpu(pSMBr->ParameterCount);
3660 *ppparm = (char *)&pSMBr->hdr.Protocol + parm_offset;
3661 *ppdata = (char *)&pSMBr->hdr.Protocol + data_offset;
3663 /* should we also check that parm and data areas do not overlap? */
3664 if (*ppparm > end_of_smb) {
3665 cifs_dbg(FYI, "parms start after end of smb\n");
3666 return -EINVAL;
3667 } else if (parm_count + *ppparm > end_of_smb) {
3668 cifs_dbg(FYI, "parm end after end of smb\n");
3669 return -EINVAL;
3670 } else if (*ppdata > end_of_smb) {
3671 cifs_dbg(FYI, "data starts after end of smb\n");
3672 return -EINVAL;
3673 } else if (data_count + *ppdata > end_of_smb) {
3674 cifs_dbg(FYI, "data %p + count %d (%p) past smb end %p start %p\n",
3675 *ppdata, data_count, (data_count + *ppdata),
3676 end_of_smb, pSMBr);
3677 return -EINVAL;
3678 } else if (parm_count + data_count > bcc) {
3679 cifs_dbg(FYI, "parm count and data count larger than SMB\n");
3680 return -EINVAL;
3682 *pdatalen = data_count;
3683 *pparmlen = parm_count;
3684 return 0;
3687 /* Get Security Descriptor (by handle) from remote server for a file or dir */
3689 CIFSSMBGetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
3690 struct cifs_ntsd **acl_inf, __u32 *pbuflen)
3692 int rc = 0;
3693 int buf_type = 0;
3694 QUERY_SEC_DESC_REQ *pSMB;
3695 struct kvec iov[1];
3697 cifs_dbg(FYI, "GetCifsACL\n");
3699 *pbuflen = 0;
3700 *acl_inf = NULL;
3702 rc = smb_init_nttransact(NT_TRANSACT_QUERY_SECURITY_DESC, 0,
3703 8 /* parm len */, tcon, (void **) &pSMB);
3704 if (rc)
3705 return rc;
3707 pSMB->MaxParameterCount = cpu_to_le32(4);
3708 /* BB TEST with big acls that might need to be e.g. larger than 16K */
3709 pSMB->MaxSetupCount = 0;
3710 pSMB->Fid = fid; /* file handle always le */
3711 pSMB->AclFlags = cpu_to_le32(CIFS_ACL_OWNER | CIFS_ACL_GROUP |
3712 CIFS_ACL_DACL);
3713 pSMB->ByteCount = cpu_to_le16(11); /* 3 bytes pad + 8 bytes parm */
3714 inc_rfc1001_len(pSMB, 11);
3715 iov[0].iov_base = (char *)pSMB;
3716 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
3718 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovec */, &buf_type,
3720 cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
3721 if (rc) {
3722 cifs_dbg(FYI, "Send error in QuerySecDesc = %d\n", rc);
3723 } else { /* decode response */
3724 __le32 *parm;
3725 __u32 parm_len;
3726 __u32 acl_len;
3727 struct smb_com_ntransact_rsp *pSMBr;
3728 char *pdata;
3730 /* validate_nttransact */
3731 rc = validate_ntransact(iov[0].iov_base, (char **)&parm,
3732 &pdata, &parm_len, pbuflen);
3733 if (rc)
3734 goto qsec_out;
3735 pSMBr = (struct smb_com_ntransact_rsp *)iov[0].iov_base;
3737 cifs_dbg(FYI, "smb %p parm %p data %p\n",
3738 pSMBr, parm, *acl_inf);
3740 if (le32_to_cpu(pSMBr->ParameterCount) != 4) {
3741 rc = -EIO; /* bad smb */
3742 *pbuflen = 0;
3743 goto qsec_out;
3746 /* BB check that data area is minimum length and as big as acl_len */
3748 acl_len = le32_to_cpu(*parm);
3749 if (acl_len != *pbuflen) {
3750 cifs_dbg(VFS, "acl length %d does not match %d\n",
3751 acl_len, *pbuflen);
3752 if (*pbuflen > acl_len)
3753 *pbuflen = acl_len;
3756 /* check if buffer is big enough for the acl
3757 header followed by the smallest SID */
3758 if ((*pbuflen < sizeof(struct cifs_ntsd) + 8) ||
3759 (*pbuflen >= 64 * 1024)) {
3760 cifs_dbg(VFS, "bad acl length %d\n", *pbuflen);
3761 rc = -EINVAL;
3762 *pbuflen = 0;
3763 } else {
3764 *acl_inf = kmemdup(pdata, *pbuflen, GFP_KERNEL);
3765 if (*acl_inf == NULL) {
3766 *pbuflen = 0;
3767 rc = -ENOMEM;
3771 qsec_out:
3772 if (buf_type == CIFS_SMALL_BUFFER)
3773 cifs_small_buf_release(iov[0].iov_base);
3774 else if (buf_type == CIFS_LARGE_BUFFER)
3775 cifs_buf_release(iov[0].iov_base);
3776 /* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
3777 return rc;
3781 CIFSSMBSetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
3782 struct cifs_ntsd *pntsd, __u32 acllen, int aclflag)
3784 __u16 byte_count, param_count, data_count, param_offset, data_offset;
3785 int rc = 0;
3786 int bytes_returned = 0;
3787 SET_SEC_DESC_REQ *pSMB = NULL;
3788 void *pSMBr;
3790 setCifsAclRetry:
3791 rc = smb_init(SMB_COM_NT_TRANSACT, 19, tcon, (void **) &pSMB, &pSMBr);
3792 if (rc)
3793 return rc;
3795 pSMB->MaxSetupCount = 0;
3796 pSMB->Reserved = 0;
3798 param_count = 8;
3799 param_offset = offsetof(struct smb_com_transaction_ssec_req, Fid) - 4;
3800 data_count = acllen;
3801 data_offset = param_offset + param_count;
3802 byte_count = 3 /* pad */ + param_count;
3804 pSMB->DataCount = cpu_to_le32(data_count);
3805 pSMB->TotalDataCount = pSMB->DataCount;
3806 pSMB->MaxParameterCount = cpu_to_le32(4);
3807 pSMB->MaxDataCount = cpu_to_le32(16384);
3808 pSMB->ParameterCount = cpu_to_le32(param_count);
3809 pSMB->ParameterOffset = cpu_to_le32(param_offset);
3810 pSMB->TotalParameterCount = pSMB->ParameterCount;
3811 pSMB->DataOffset = cpu_to_le32(data_offset);
3812 pSMB->SetupCount = 0;
3813 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_SET_SECURITY_DESC);
3814 pSMB->ByteCount = cpu_to_le16(byte_count+data_count);
3816 pSMB->Fid = fid; /* file handle always le */
3817 pSMB->Reserved2 = 0;
3818 pSMB->AclFlags = cpu_to_le32(aclflag);
3820 if (pntsd && acllen) {
3821 memcpy((char *)pSMBr + offsetof(struct smb_hdr, Protocol) +
3822 data_offset, pntsd, acllen);
3823 inc_rfc1001_len(pSMB, byte_count + data_count);
3824 } else
3825 inc_rfc1001_len(pSMB, byte_count);
3827 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3828 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3830 cifs_dbg(FYI, "SetCIFSACL bytes_returned: %d, rc: %d\n",
3831 bytes_returned, rc);
3832 if (rc)
3833 cifs_dbg(FYI, "Set CIFS ACL returned %d\n", rc);
3834 cifs_buf_release(pSMB);
3836 if (rc == -EAGAIN)
3837 goto setCifsAclRetry;
3839 return (rc);
3842 #endif /* CONFIG_CIFS_ACL */
3844 /* Legacy Query Path Information call for lookup to old servers such
3845 as Win9x/WinME */
3847 SMBQueryInformation(const unsigned int xid, struct cifs_tcon *tcon,
3848 const char *search_name, FILE_ALL_INFO *data,
3849 const struct nls_table *nls_codepage, int remap)
3851 QUERY_INFORMATION_REQ *pSMB;
3852 QUERY_INFORMATION_RSP *pSMBr;
3853 int rc = 0;
3854 int bytes_returned;
3855 int name_len;
3857 cifs_dbg(FYI, "In SMBQPath path %s\n", search_name);
3858 QInfRetry:
3859 rc = smb_init(SMB_COM_QUERY_INFORMATION, 0, tcon, (void **) &pSMB,
3860 (void **) &pSMBr);
3861 if (rc)
3862 return rc;
3864 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3865 name_len =
3866 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3867 search_name, PATH_MAX, nls_codepage,
3868 remap);
3869 name_len++; /* trailing null */
3870 name_len *= 2;
3871 } else {
3872 name_len = strnlen(search_name, PATH_MAX);
3873 name_len++; /* trailing null */
3874 strncpy(pSMB->FileName, search_name, name_len);
3876 pSMB->BufferFormat = 0x04;
3877 name_len++; /* account for buffer type byte */
3878 inc_rfc1001_len(pSMB, (__u16)name_len);
3879 pSMB->ByteCount = cpu_to_le16(name_len);
3881 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3882 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3883 if (rc) {
3884 cifs_dbg(FYI, "Send error in QueryInfo = %d\n", rc);
3885 } else if (data) {
3886 struct timespec ts;
3887 __u32 time = le32_to_cpu(pSMBr->last_write_time);
3889 /* decode response */
3890 /* BB FIXME - add time zone adjustment BB */
3891 memset(data, 0, sizeof(FILE_ALL_INFO));
3892 ts.tv_nsec = 0;
3893 ts.tv_sec = time;
3894 /* decode time fields */
3895 data->ChangeTime = cpu_to_le64(cifs_UnixTimeToNT(ts));
3896 data->LastWriteTime = data->ChangeTime;
3897 data->LastAccessTime = 0;
3898 data->AllocationSize =
3899 cpu_to_le64(le32_to_cpu(pSMBr->size));
3900 data->EndOfFile = data->AllocationSize;
3901 data->Attributes =
3902 cpu_to_le32(le16_to_cpu(pSMBr->attr));
3903 } else
3904 rc = -EIO; /* bad buffer passed in */
3906 cifs_buf_release(pSMB);
3908 if (rc == -EAGAIN)
3909 goto QInfRetry;
3911 return rc;
3915 CIFSSMBQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
3916 u16 netfid, FILE_ALL_INFO *pFindData)
3918 struct smb_t2_qfi_req *pSMB = NULL;
3919 struct smb_t2_qfi_rsp *pSMBr = NULL;
3920 int rc = 0;
3921 int bytes_returned;
3922 __u16 params, byte_count;
3924 QFileInfoRetry:
3925 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3926 (void **) &pSMBr);
3927 if (rc)
3928 return rc;
3930 params = 2 /* level */ + 2 /* fid */;
3931 pSMB->t2.TotalDataCount = 0;
3932 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
3933 /* BB find exact max data count below from sess structure BB */
3934 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
3935 pSMB->t2.MaxSetupCount = 0;
3936 pSMB->t2.Reserved = 0;
3937 pSMB->t2.Flags = 0;
3938 pSMB->t2.Timeout = 0;
3939 pSMB->t2.Reserved2 = 0;
3940 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
3941 Fid) - 4);
3942 pSMB->t2.DataCount = 0;
3943 pSMB->t2.DataOffset = 0;
3944 pSMB->t2.SetupCount = 1;
3945 pSMB->t2.Reserved3 = 0;
3946 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
3947 byte_count = params + 1 /* pad */ ;
3948 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
3949 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
3950 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
3951 pSMB->Pad = 0;
3952 pSMB->Fid = netfid;
3953 inc_rfc1001_len(pSMB, byte_count);
3954 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
3956 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3957 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3958 if (rc) {
3959 cifs_dbg(FYI, "Send error in QPathInfo = %d\n", rc);
3960 } else { /* decode response */
3961 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
3963 if (rc) /* BB add auto retry on EOPNOTSUPP? */
3964 rc = -EIO;
3965 else if (get_bcc(&pSMBr->hdr) < 40)
3966 rc = -EIO; /* bad smb */
3967 else if (pFindData) {
3968 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3969 memcpy((char *) pFindData,
3970 (char *) &pSMBr->hdr.Protocol +
3971 data_offset, sizeof(FILE_ALL_INFO));
3972 } else
3973 rc = -ENOMEM;
3975 cifs_buf_release(pSMB);
3976 if (rc == -EAGAIN)
3977 goto QFileInfoRetry;
3979 return rc;
3983 CIFSSMBQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
3984 const char *search_name, FILE_ALL_INFO *data,
3985 int legacy /* old style infolevel */,
3986 const struct nls_table *nls_codepage, int remap)
3988 /* level 263 SMB_QUERY_FILE_ALL_INFO */
3989 TRANSACTION2_QPI_REQ *pSMB = NULL;
3990 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3991 int rc = 0;
3992 int bytes_returned;
3993 int name_len;
3994 __u16 params, byte_count;
3996 /* cifs_dbg(FYI, "In QPathInfo path %s\n", search_name); */
3997 QPathInfoRetry:
3998 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3999 (void **) &pSMBr);
4000 if (rc)
4001 return rc;
4003 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4004 name_len =
4005 cifsConvertToUTF16((__le16 *) pSMB->FileName, search_name,
4006 PATH_MAX, nls_codepage, remap);
4007 name_len++; /* trailing null */
4008 name_len *= 2;
4009 } else { /* BB improve the check for buffer overruns BB */
4010 name_len = strnlen(search_name, PATH_MAX);
4011 name_len++; /* trailing null */
4012 strncpy(pSMB->FileName, search_name, name_len);
4015 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
4016 pSMB->TotalDataCount = 0;
4017 pSMB->MaxParameterCount = cpu_to_le16(2);
4018 /* BB find exact max SMB PDU from sess structure BB */
4019 pSMB->MaxDataCount = cpu_to_le16(4000);
4020 pSMB->MaxSetupCount = 0;
4021 pSMB->Reserved = 0;
4022 pSMB->Flags = 0;
4023 pSMB->Timeout = 0;
4024 pSMB->Reserved2 = 0;
4025 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4026 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
4027 pSMB->DataCount = 0;
4028 pSMB->DataOffset = 0;
4029 pSMB->SetupCount = 1;
4030 pSMB->Reserved3 = 0;
4031 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4032 byte_count = params + 1 /* pad */ ;
4033 pSMB->TotalParameterCount = cpu_to_le16(params);
4034 pSMB->ParameterCount = pSMB->TotalParameterCount;
4035 if (legacy)
4036 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_STANDARD);
4037 else
4038 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
4039 pSMB->Reserved4 = 0;
4040 inc_rfc1001_len(pSMB, byte_count);
4041 pSMB->ByteCount = cpu_to_le16(byte_count);
4043 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4044 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4045 if (rc) {
4046 cifs_dbg(FYI, "Send error in QPathInfo = %d\n", rc);
4047 } else { /* decode response */
4048 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4050 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4051 rc = -EIO;
4052 else if (!legacy && get_bcc(&pSMBr->hdr) < 40)
4053 rc = -EIO; /* bad smb */
4054 else if (legacy && get_bcc(&pSMBr->hdr) < 24)
4055 rc = -EIO; /* 24 or 26 expected but we do not read
4056 last field */
4057 else if (data) {
4058 int size;
4059 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4062 * On legacy responses we do not read the last field,
4063 * EAsize, fortunately since it varies by subdialect and
4064 * also note it differs on Set vs Get, ie two bytes or 4
4065 * bytes depending but we don't care here.
4067 if (legacy)
4068 size = sizeof(FILE_INFO_STANDARD);
4069 else
4070 size = sizeof(FILE_ALL_INFO);
4071 memcpy((char *) data, (char *) &pSMBr->hdr.Protocol +
4072 data_offset, size);
4073 } else
4074 rc = -ENOMEM;
4076 cifs_buf_release(pSMB);
4077 if (rc == -EAGAIN)
4078 goto QPathInfoRetry;
4080 return rc;
4084 CIFSSMBUnixQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
4085 u16 netfid, FILE_UNIX_BASIC_INFO *pFindData)
4087 struct smb_t2_qfi_req *pSMB = NULL;
4088 struct smb_t2_qfi_rsp *pSMBr = NULL;
4089 int rc = 0;
4090 int bytes_returned;
4091 __u16 params, byte_count;
4093 UnixQFileInfoRetry:
4094 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4095 (void **) &pSMBr);
4096 if (rc)
4097 return rc;
4099 params = 2 /* level */ + 2 /* fid */;
4100 pSMB->t2.TotalDataCount = 0;
4101 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4102 /* BB find exact max data count below from sess structure BB */
4103 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4104 pSMB->t2.MaxSetupCount = 0;
4105 pSMB->t2.Reserved = 0;
4106 pSMB->t2.Flags = 0;
4107 pSMB->t2.Timeout = 0;
4108 pSMB->t2.Reserved2 = 0;
4109 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4110 Fid) - 4);
4111 pSMB->t2.DataCount = 0;
4112 pSMB->t2.DataOffset = 0;
4113 pSMB->t2.SetupCount = 1;
4114 pSMB->t2.Reserved3 = 0;
4115 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4116 byte_count = params + 1 /* pad */ ;
4117 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4118 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4119 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4120 pSMB->Pad = 0;
4121 pSMB->Fid = netfid;
4122 inc_rfc1001_len(pSMB, byte_count);
4123 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
4125 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4126 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4127 if (rc) {
4128 cifs_dbg(FYI, "Send error in QPathInfo = %d\n", rc);
4129 } else { /* decode response */
4130 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4132 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
4133 cifs_dbg(VFS, "Malformed FILE_UNIX_BASIC_INFO response. Unix Extensions can be disabled on mount by specifying the nosfu mount option.\n");
4134 rc = -EIO; /* bad smb */
4135 } else {
4136 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4137 memcpy((char *) pFindData,
4138 (char *) &pSMBr->hdr.Protocol +
4139 data_offset,
4140 sizeof(FILE_UNIX_BASIC_INFO));
4144 cifs_buf_release(pSMB);
4145 if (rc == -EAGAIN)
4146 goto UnixQFileInfoRetry;
4148 return rc;
4152 CIFSSMBUnixQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
4153 const unsigned char *searchName,
4154 FILE_UNIX_BASIC_INFO *pFindData,
4155 const struct nls_table *nls_codepage, int remap)
4157 /* SMB_QUERY_FILE_UNIX_BASIC */
4158 TRANSACTION2_QPI_REQ *pSMB = NULL;
4159 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4160 int rc = 0;
4161 int bytes_returned = 0;
4162 int name_len;
4163 __u16 params, byte_count;
4165 cifs_dbg(FYI, "In QPathInfo (Unix) the path %s\n", searchName);
4166 UnixQPathInfoRetry:
4167 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4168 (void **) &pSMBr);
4169 if (rc)
4170 return rc;
4172 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4173 name_len =
4174 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4175 PATH_MAX, nls_codepage, remap);
4176 name_len++; /* trailing null */
4177 name_len *= 2;
4178 } else { /* BB improve the check for buffer overruns BB */
4179 name_len = strnlen(searchName, PATH_MAX);
4180 name_len++; /* trailing null */
4181 strncpy(pSMB->FileName, searchName, name_len);
4184 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
4185 pSMB->TotalDataCount = 0;
4186 pSMB->MaxParameterCount = cpu_to_le16(2);
4187 /* BB find exact max SMB PDU from sess structure BB */
4188 pSMB->MaxDataCount = cpu_to_le16(4000);
4189 pSMB->MaxSetupCount = 0;
4190 pSMB->Reserved = 0;
4191 pSMB->Flags = 0;
4192 pSMB->Timeout = 0;
4193 pSMB->Reserved2 = 0;
4194 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4195 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
4196 pSMB->DataCount = 0;
4197 pSMB->DataOffset = 0;
4198 pSMB->SetupCount = 1;
4199 pSMB->Reserved3 = 0;
4200 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4201 byte_count = params + 1 /* pad */ ;
4202 pSMB->TotalParameterCount = cpu_to_le16(params);
4203 pSMB->ParameterCount = pSMB->TotalParameterCount;
4204 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4205 pSMB->Reserved4 = 0;
4206 inc_rfc1001_len(pSMB, byte_count);
4207 pSMB->ByteCount = cpu_to_le16(byte_count);
4209 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4210 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4211 if (rc) {
4212 cifs_dbg(FYI, "Send error in QPathInfo = %d\n", rc);
4213 } else { /* decode response */
4214 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4216 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
4217 cifs_dbg(VFS, "Malformed FILE_UNIX_BASIC_INFO response. Unix Extensions can be disabled on mount by specifying the nosfu mount option.\n");
4218 rc = -EIO; /* bad smb */
4219 } else {
4220 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4221 memcpy((char *) pFindData,
4222 (char *) &pSMBr->hdr.Protocol +
4223 data_offset,
4224 sizeof(FILE_UNIX_BASIC_INFO));
4227 cifs_buf_release(pSMB);
4228 if (rc == -EAGAIN)
4229 goto UnixQPathInfoRetry;
4231 return rc;
4234 /* xid, tcon, searchName and codepage are input parms, rest are returned */
4236 CIFSFindFirst(const unsigned int xid, struct cifs_tcon *tcon,
4237 const char *searchName, struct cifs_sb_info *cifs_sb,
4238 __u16 *pnetfid, __u16 search_flags,
4239 struct cifs_search_info *psrch_inf, bool msearch)
4241 /* level 257 SMB_ */
4242 TRANSACTION2_FFIRST_REQ *pSMB = NULL;
4243 TRANSACTION2_FFIRST_RSP *pSMBr = NULL;
4244 T2_FFIRST_RSP_PARMS *parms;
4245 int rc = 0;
4246 int bytes_returned = 0;
4247 int name_len, remap;
4248 __u16 params, byte_count;
4249 struct nls_table *nls_codepage;
4251 cifs_dbg(FYI, "In FindFirst for %s\n", searchName);
4253 findFirstRetry:
4254 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4255 (void **) &pSMBr);
4256 if (rc)
4257 return rc;
4259 nls_codepage = cifs_sb->local_nls;
4260 remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR;
4262 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4263 name_len =
4264 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4265 PATH_MAX, nls_codepage, remap);
4266 /* We can not add the asterik earlier in case
4267 it got remapped to 0xF03A as if it were part of the
4268 directory name instead of a wildcard */
4269 name_len *= 2;
4270 if (msearch) {
4271 pSMB->FileName[name_len] = CIFS_DIR_SEP(cifs_sb);
4272 pSMB->FileName[name_len+1] = 0;
4273 pSMB->FileName[name_len+2] = '*';
4274 pSMB->FileName[name_len+3] = 0;
4275 name_len += 4; /* now the trailing null */
4276 /* null terminate just in case */
4277 pSMB->FileName[name_len] = 0;
4278 pSMB->FileName[name_len+1] = 0;
4279 name_len += 2;
4281 } else { /* BB add check for overrun of SMB buf BB */
4282 name_len = strnlen(searchName, PATH_MAX);
4283 /* BB fix here and in unicode clause above ie
4284 if (name_len > buffersize-header)
4285 free buffer exit; BB */
4286 strncpy(pSMB->FileName, searchName, name_len);
4287 if (msearch) {
4288 pSMB->FileName[name_len] = CIFS_DIR_SEP(cifs_sb);
4289 pSMB->FileName[name_len+1] = '*';
4290 pSMB->FileName[name_len+2] = 0;
4291 name_len += 3;
4295 params = 12 + name_len /* includes null */ ;
4296 pSMB->TotalDataCount = 0; /* no EAs */
4297 pSMB->MaxParameterCount = cpu_to_le16(10);
4298 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
4299 pSMB->MaxSetupCount = 0;
4300 pSMB->Reserved = 0;
4301 pSMB->Flags = 0;
4302 pSMB->Timeout = 0;
4303 pSMB->Reserved2 = 0;
4304 byte_count = params + 1 /* pad */ ;
4305 pSMB->TotalParameterCount = cpu_to_le16(params);
4306 pSMB->ParameterCount = pSMB->TotalParameterCount;
4307 pSMB->ParameterOffset = cpu_to_le16(
4308 offsetof(struct smb_com_transaction2_ffirst_req, SearchAttributes)
4309 - 4);
4310 pSMB->DataCount = 0;
4311 pSMB->DataOffset = 0;
4312 pSMB->SetupCount = 1; /* one byte, no need to make endian neutral */
4313 pSMB->Reserved3 = 0;
4314 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_FIRST);
4315 pSMB->SearchAttributes =
4316 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
4317 ATTR_DIRECTORY);
4318 pSMB->SearchCount = cpu_to_le16(CIFSMaxBufSize/sizeof(FILE_UNIX_INFO));
4319 pSMB->SearchFlags = cpu_to_le16(search_flags);
4320 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4322 /* BB what should we set StorageType to? Does it matter? BB */
4323 pSMB->SearchStorageType = 0;
4324 inc_rfc1001_len(pSMB, byte_count);
4325 pSMB->ByteCount = cpu_to_le16(byte_count);
4327 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4328 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4329 cifs_stats_inc(&tcon->stats.cifs_stats.num_ffirst);
4331 if (rc) {/* BB add logic to retry regular search if Unix search
4332 rejected unexpectedly by server */
4333 /* BB Add code to handle unsupported level rc */
4334 cifs_dbg(FYI, "Error in FindFirst = %d\n", rc);
4336 cifs_buf_release(pSMB);
4338 /* BB eventually could optimize out free and realloc of buf */
4339 /* for this case */
4340 if (rc == -EAGAIN)
4341 goto findFirstRetry;
4342 } else { /* decode response */
4343 /* BB remember to free buffer if error BB */
4344 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4345 if (rc == 0) {
4346 unsigned int lnoff;
4348 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
4349 psrch_inf->unicode = true;
4350 else
4351 psrch_inf->unicode = false;
4353 psrch_inf->ntwrk_buf_start = (char *)pSMBr;
4354 psrch_inf->smallBuf = 0;
4355 psrch_inf->srch_entries_start =
4356 (char *) &pSMBr->hdr.Protocol +
4357 le16_to_cpu(pSMBr->t2.DataOffset);
4358 parms = (T2_FFIRST_RSP_PARMS *)((char *) &pSMBr->hdr.Protocol +
4359 le16_to_cpu(pSMBr->t2.ParameterOffset));
4361 if (parms->EndofSearch)
4362 psrch_inf->endOfSearch = true;
4363 else
4364 psrch_inf->endOfSearch = false;
4366 psrch_inf->entries_in_buffer =
4367 le16_to_cpu(parms->SearchCount);
4368 psrch_inf->index_of_last_entry = 2 /* skip . and .. */ +
4369 psrch_inf->entries_in_buffer;
4370 lnoff = le16_to_cpu(parms->LastNameOffset);
4371 if (CIFSMaxBufSize < lnoff) {
4372 cifs_dbg(VFS, "ignoring corrupt resume name\n");
4373 psrch_inf->last_entry = NULL;
4374 return rc;
4377 psrch_inf->last_entry = psrch_inf->srch_entries_start +
4378 lnoff;
4380 if (pnetfid)
4381 *pnetfid = parms->SearchHandle;
4382 } else {
4383 cifs_buf_release(pSMB);
4387 return rc;
4390 int CIFSFindNext(const unsigned int xid, struct cifs_tcon *tcon,
4391 __u16 searchHandle, __u16 search_flags,
4392 struct cifs_search_info *psrch_inf)
4394 TRANSACTION2_FNEXT_REQ *pSMB = NULL;
4395 TRANSACTION2_FNEXT_RSP *pSMBr = NULL;
4396 T2_FNEXT_RSP_PARMS *parms;
4397 char *response_data;
4398 int rc = 0;
4399 int bytes_returned;
4400 unsigned int name_len;
4401 __u16 params, byte_count;
4403 cifs_dbg(FYI, "In FindNext\n");
4405 if (psrch_inf->endOfSearch)
4406 return -ENOENT;
4408 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4409 (void **) &pSMBr);
4410 if (rc)
4411 return rc;
4413 params = 14; /* includes 2 bytes of null string, converted to LE below*/
4414 byte_count = 0;
4415 pSMB->TotalDataCount = 0; /* no EAs */
4416 pSMB->MaxParameterCount = cpu_to_le16(8);
4417 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
4418 pSMB->MaxSetupCount = 0;
4419 pSMB->Reserved = 0;
4420 pSMB->Flags = 0;
4421 pSMB->Timeout = 0;
4422 pSMB->Reserved2 = 0;
4423 pSMB->ParameterOffset = cpu_to_le16(
4424 offsetof(struct smb_com_transaction2_fnext_req,SearchHandle) - 4);
4425 pSMB->DataCount = 0;
4426 pSMB->DataOffset = 0;
4427 pSMB->SetupCount = 1;
4428 pSMB->Reserved3 = 0;
4429 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_NEXT);
4430 pSMB->SearchHandle = searchHandle; /* always kept as le */
4431 pSMB->SearchCount =
4432 cpu_to_le16(CIFSMaxBufSize / sizeof(FILE_UNIX_INFO));
4433 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4434 pSMB->ResumeKey = psrch_inf->resume_key;
4435 pSMB->SearchFlags = cpu_to_le16(search_flags);
4437 name_len = psrch_inf->resume_name_len;
4438 params += name_len;
4439 if (name_len < PATH_MAX) {
4440 memcpy(pSMB->ResumeFileName, psrch_inf->presume_name, name_len);
4441 byte_count += name_len;
4442 /* 14 byte parm len above enough for 2 byte null terminator */
4443 pSMB->ResumeFileName[name_len] = 0;
4444 pSMB->ResumeFileName[name_len+1] = 0;
4445 } else {
4446 rc = -EINVAL;
4447 goto FNext2_err_exit;
4449 byte_count = params + 1 /* pad */ ;
4450 pSMB->TotalParameterCount = cpu_to_le16(params);
4451 pSMB->ParameterCount = pSMB->TotalParameterCount;
4452 inc_rfc1001_len(pSMB, byte_count);
4453 pSMB->ByteCount = cpu_to_le16(byte_count);
4455 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4456 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4457 cifs_stats_inc(&tcon->stats.cifs_stats.num_fnext);
4458 if (rc) {
4459 if (rc == -EBADF) {
4460 psrch_inf->endOfSearch = true;
4461 cifs_buf_release(pSMB);
4462 rc = 0; /* search probably was closed at end of search*/
4463 } else
4464 cifs_dbg(FYI, "FindNext returned = %d\n", rc);
4465 } else { /* decode response */
4466 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4468 if (rc == 0) {
4469 unsigned int lnoff;
4471 /* BB fixme add lock for file (srch_info) struct here */
4472 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
4473 psrch_inf->unicode = true;
4474 else
4475 psrch_inf->unicode = false;
4476 response_data = (char *) &pSMBr->hdr.Protocol +
4477 le16_to_cpu(pSMBr->t2.ParameterOffset);
4478 parms = (T2_FNEXT_RSP_PARMS *)response_data;
4479 response_data = (char *)&pSMBr->hdr.Protocol +
4480 le16_to_cpu(pSMBr->t2.DataOffset);
4481 if (psrch_inf->smallBuf)
4482 cifs_small_buf_release(
4483 psrch_inf->ntwrk_buf_start);
4484 else
4485 cifs_buf_release(psrch_inf->ntwrk_buf_start);
4486 psrch_inf->srch_entries_start = response_data;
4487 psrch_inf->ntwrk_buf_start = (char *)pSMB;
4488 psrch_inf->smallBuf = 0;
4489 if (parms->EndofSearch)
4490 psrch_inf->endOfSearch = true;
4491 else
4492 psrch_inf->endOfSearch = false;
4493 psrch_inf->entries_in_buffer =
4494 le16_to_cpu(parms->SearchCount);
4495 psrch_inf->index_of_last_entry +=
4496 psrch_inf->entries_in_buffer;
4497 lnoff = le16_to_cpu(parms->LastNameOffset);
4498 if (CIFSMaxBufSize < lnoff) {
4499 cifs_dbg(VFS, "ignoring corrupt resume name\n");
4500 psrch_inf->last_entry = NULL;
4501 return rc;
4502 } else
4503 psrch_inf->last_entry =
4504 psrch_inf->srch_entries_start + lnoff;
4506 /* cifs_dbg(FYI, "fnxt2 entries in buf %d index_of_last %d\n",
4507 psrch_inf->entries_in_buffer, psrch_inf->index_of_last_entry); */
4509 /* BB fixme add unlock here */
4514 /* BB On error, should we leave previous search buf (and count and
4515 last entry fields) intact or free the previous one? */
4517 /* Note: On -EAGAIN error only caller can retry on handle based calls
4518 since file handle passed in no longer valid */
4519 FNext2_err_exit:
4520 if (rc != 0)
4521 cifs_buf_release(pSMB);
4522 return rc;
4526 CIFSFindClose(const unsigned int xid, struct cifs_tcon *tcon,
4527 const __u16 searchHandle)
4529 int rc = 0;
4530 FINDCLOSE_REQ *pSMB = NULL;
4532 cifs_dbg(FYI, "In CIFSSMBFindClose\n");
4533 rc = small_smb_init(SMB_COM_FIND_CLOSE2, 1, tcon, (void **)&pSMB);
4535 /* no sense returning error if session restarted
4536 as file handle has been closed */
4537 if (rc == -EAGAIN)
4538 return 0;
4539 if (rc)
4540 return rc;
4542 pSMB->FileID = searchHandle;
4543 pSMB->ByteCount = 0;
4544 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
4545 if (rc)
4546 cifs_dbg(VFS, "Send error in FindClose = %d\n", rc);
4548 cifs_stats_inc(&tcon->stats.cifs_stats.num_fclose);
4550 /* Since session is dead, search handle closed on server already */
4551 if (rc == -EAGAIN)
4552 rc = 0;
4554 return rc;
4558 CIFSGetSrvInodeNumber(const unsigned int xid, struct cifs_tcon *tcon,
4559 const char *search_name, __u64 *inode_number,
4560 const struct nls_table *nls_codepage, int remap)
4562 int rc = 0;
4563 TRANSACTION2_QPI_REQ *pSMB = NULL;
4564 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4565 int name_len, bytes_returned;
4566 __u16 params, byte_count;
4568 cifs_dbg(FYI, "In GetSrvInodeNum for %s\n", search_name);
4569 if (tcon == NULL)
4570 return -ENODEV;
4572 GetInodeNumberRetry:
4573 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4574 (void **) &pSMBr);
4575 if (rc)
4576 return rc;
4578 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4579 name_len =
4580 cifsConvertToUTF16((__le16 *) pSMB->FileName,
4581 search_name, PATH_MAX, nls_codepage,
4582 remap);
4583 name_len++; /* trailing null */
4584 name_len *= 2;
4585 } else { /* BB improve the check for buffer overruns BB */
4586 name_len = strnlen(search_name, PATH_MAX);
4587 name_len++; /* trailing null */
4588 strncpy(pSMB->FileName, search_name, name_len);
4591 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
4592 pSMB->TotalDataCount = 0;
4593 pSMB->MaxParameterCount = cpu_to_le16(2);
4594 /* BB find exact max data count below from sess structure BB */
4595 pSMB->MaxDataCount = cpu_to_le16(4000);
4596 pSMB->MaxSetupCount = 0;
4597 pSMB->Reserved = 0;
4598 pSMB->Flags = 0;
4599 pSMB->Timeout = 0;
4600 pSMB->Reserved2 = 0;
4601 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4602 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
4603 pSMB->DataCount = 0;
4604 pSMB->DataOffset = 0;
4605 pSMB->SetupCount = 1;
4606 pSMB->Reserved3 = 0;
4607 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4608 byte_count = params + 1 /* pad */ ;
4609 pSMB->TotalParameterCount = cpu_to_le16(params);
4610 pSMB->ParameterCount = pSMB->TotalParameterCount;
4611 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_INTERNAL_INFO);
4612 pSMB->Reserved4 = 0;
4613 inc_rfc1001_len(pSMB, byte_count);
4614 pSMB->ByteCount = cpu_to_le16(byte_count);
4616 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4617 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4618 if (rc) {
4619 cifs_dbg(FYI, "error %d in QueryInternalInfo\n", rc);
4620 } else {
4621 /* decode response */
4622 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4623 /* BB also check enough total bytes returned */
4624 if (rc || get_bcc(&pSMBr->hdr) < 2)
4625 /* If rc should we check for EOPNOSUPP and
4626 disable the srvino flag? or in caller? */
4627 rc = -EIO; /* bad smb */
4628 else {
4629 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4630 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
4631 struct file_internal_info *pfinfo;
4632 /* BB Do we need a cast or hash here ? */
4633 if (count < 8) {
4634 cifs_dbg(FYI, "Illegal size ret in QryIntrnlInf\n");
4635 rc = -EIO;
4636 goto GetInodeNumOut;
4638 pfinfo = (struct file_internal_info *)
4639 (data_offset + (char *) &pSMBr->hdr.Protocol);
4640 *inode_number = le64_to_cpu(pfinfo->UniqueId);
4643 GetInodeNumOut:
4644 cifs_buf_release(pSMB);
4645 if (rc == -EAGAIN)
4646 goto GetInodeNumberRetry;
4647 return rc;
4650 /* parses DFS refferal V3 structure
4651 * caller is responsible for freeing target_nodes
4652 * returns:
4653 * on success - 0
4654 * on failure - errno
4656 static int
4657 parse_DFS_referrals(TRANSACTION2_GET_DFS_REFER_RSP *pSMBr,
4658 unsigned int *num_of_nodes,
4659 struct dfs_info3_param **target_nodes,
4660 const struct nls_table *nls_codepage, int remap,
4661 const char *searchName)
4663 int i, rc = 0;
4664 char *data_end;
4665 bool is_unicode;
4666 struct dfs_referral_level_3 *ref;
4668 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
4669 is_unicode = true;
4670 else
4671 is_unicode = false;
4672 *num_of_nodes = le16_to_cpu(pSMBr->NumberOfReferrals);
4674 if (*num_of_nodes < 1) {
4675 cifs_dbg(VFS, "num_referrals: must be at least > 0, but we get num_referrals = %d\n",
4676 *num_of_nodes);
4677 rc = -EINVAL;
4678 goto parse_DFS_referrals_exit;
4681 ref = (struct dfs_referral_level_3 *) &(pSMBr->referrals);
4682 if (ref->VersionNumber != cpu_to_le16(3)) {
4683 cifs_dbg(VFS, "Referrals of V%d version are not supported, should be V3\n",
4684 le16_to_cpu(ref->VersionNumber));
4685 rc = -EINVAL;
4686 goto parse_DFS_referrals_exit;
4689 /* get the upper boundary of the resp buffer */
4690 data_end = (char *)(&(pSMBr->PathConsumed)) +
4691 le16_to_cpu(pSMBr->t2.DataCount);
4693 cifs_dbg(FYI, "num_referrals: %d dfs flags: 0x%x ...\n",
4694 *num_of_nodes, le32_to_cpu(pSMBr->DFSFlags));
4696 *target_nodes = kcalloc(*num_of_nodes, sizeof(struct dfs_info3_param),
4697 GFP_KERNEL);
4698 if (*target_nodes == NULL) {
4699 rc = -ENOMEM;
4700 goto parse_DFS_referrals_exit;
4703 /* collect necessary data from referrals */
4704 for (i = 0; i < *num_of_nodes; i++) {
4705 char *temp;
4706 int max_len;
4707 struct dfs_info3_param *node = (*target_nodes)+i;
4709 node->flags = le32_to_cpu(pSMBr->DFSFlags);
4710 if (is_unicode) {
4711 __le16 *tmp = kmalloc(strlen(searchName)*2 + 2,
4712 GFP_KERNEL);
4713 if (tmp == NULL) {
4714 rc = -ENOMEM;
4715 goto parse_DFS_referrals_exit;
4717 cifsConvertToUTF16((__le16 *) tmp, searchName,
4718 PATH_MAX, nls_codepage, remap);
4719 node->path_consumed = cifs_utf16_bytes(tmp,
4720 le16_to_cpu(pSMBr->PathConsumed),
4721 nls_codepage);
4722 kfree(tmp);
4723 } else
4724 node->path_consumed = le16_to_cpu(pSMBr->PathConsumed);
4726 node->server_type = le16_to_cpu(ref->ServerType);
4727 node->ref_flag = le16_to_cpu(ref->ReferralEntryFlags);
4729 /* copy DfsPath */
4730 temp = (char *)ref + le16_to_cpu(ref->DfsPathOffset);
4731 max_len = data_end - temp;
4732 node->path_name = cifs_strndup_from_utf16(temp, max_len,
4733 is_unicode, nls_codepage);
4734 if (!node->path_name) {
4735 rc = -ENOMEM;
4736 goto parse_DFS_referrals_exit;
4739 /* copy link target UNC */
4740 temp = (char *)ref + le16_to_cpu(ref->NetworkAddressOffset);
4741 max_len = data_end - temp;
4742 node->node_name = cifs_strndup_from_utf16(temp, max_len,
4743 is_unicode, nls_codepage);
4744 if (!node->node_name) {
4745 rc = -ENOMEM;
4746 goto parse_DFS_referrals_exit;
4749 ref++;
4752 parse_DFS_referrals_exit:
4753 if (rc) {
4754 free_dfs_info_array(*target_nodes, *num_of_nodes);
4755 *target_nodes = NULL;
4756 *num_of_nodes = 0;
4758 return rc;
4762 CIFSGetDFSRefer(const unsigned int xid, struct cifs_ses *ses,
4763 const char *search_name, struct dfs_info3_param **target_nodes,
4764 unsigned int *num_of_nodes,
4765 const struct nls_table *nls_codepage, int remap)
4767 /* TRANS2_GET_DFS_REFERRAL */
4768 TRANSACTION2_GET_DFS_REFER_REQ *pSMB = NULL;
4769 TRANSACTION2_GET_DFS_REFER_RSP *pSMBr = NULL;
4770 int rc = 0;
4771 int bytes_returned;
4772 int name_len;
4773 __u16 params, byte_count;
4774 *num_of_nodes = 0;
4775 *target_nodes = NULL;
4777 cifs_dbg(FYI, "In GetDFSRefer the path %s\n", search_name);
4778 if (ses == NULL)
4779 return -ENODEV;
4780 getDFSRetry:
4781 rc = smb_init(SMB_COM_TRANSACTION2, 15, NULL, (void **) &pSMB,
4782 (void **) &pSMBr);
4783 if (rc)
4784 return rc;
4786 /* server pointer checked in called function,
4787 but should never be null here anyway */
4788 pSMB->hdr.Mid = get_next_mid(ses->server);
4789 pSMB->hdr.Tid = ses->ipc_tid;
4790 pSMB->hdr.Uid = ses->Suid;
4791 if (ses->capabilities & CAP_STATUS32)
4792 pSMB->hdr.Flags2 |= SMBFLG2_ERR_STATUS;
4793 if (ses->capabilities & CAP_DFS)
4794 pSMB->hdr.Flags2 |= SMBFLG2_DFS;
4796 if (ses->capabilities & CAP_UNICODE) {
4797 pSMB->hdr.Flags2 |= SMBFLG2_UNICODE;
4798 name_len =
4799 cifsConvertToUTF16((__le16 *) pSMB->RequestFileName,
4800 search_name, PATH_MAX, nls_codepage,
4801 remap);
4802 name_len++; /* trailing null */
4803 name_len *= 2;
4804 } else { /* BB improve the check for buffer overruns BB */
4805 name_len = strnlen(search_name, PATH_MAX);
4806 name_len++; /* trailing null */
4807 strncpy(pSMB->RequestFileName, search_name, name_len);
4810 if (ses->server && ses->server->sign)
4811 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
4813 pSMB->hdr.Uid = ses->Suid;
4815 params = 2 /* level */ + name_len /*includes null */ ;
4816 pSMB->TotalDataCount = 0;
4817 pSMB->DataCount = 0;
4818 pSMB->DataOffset = 0;
4819 pSMB->MaxParameterCount = 0;
4820 /* BB find exact max SMB PDU from sess structure BB */
4821 pSMB->MaxDataCount = cpu_to_le16(4000);
4822 pSMB->MaxSetupCount = 0;
4823 pSMB->Reserved = 0;
4824 pSMB->Flags = 0;
4825 pSMB->Timeout = 0;
4826 pSMB->Reserved2 = 0;
4827 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4828 struct smb_com_transaction2_get_dfs_refer_req, MaxReferralLevel) - 4);
4829 pSMB->SetupCount = 1;
4830 pSMB->Reserved3 = 0;
4831 pSMB->SubCommand = cpu_to_le16(TRANS2_GET_DFS_REFERRAL);
4832 byte_count = params + 3 /* pad */ ;
4833 pSMB->ParameterCount = cpu_to_le16(params);
4834 pSMB->TotalParameterCount = pSMB->ParameterCount;
4835 pSMB->MaxReferralLevel = cpu_to_le16(3);
4836 inc_rfc1001_len(pSMB, byte_count);
4837 pSMB->ByteCount = cpu_to_le16(byte_count);
4839 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
4840 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4841 if (rc) {
4842 cifs_dbg(FYI, "Send error in GetDFSRefer = %d\n", rc);
4843 goto GetDFSRefExit;
4845 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4847 /* BB Also check if enough total bytes returned? */
4848 if (rc || get_bcc(&pSMBr->hdr) < 17) {
4849 rc = -EIO; /* bad smb */
4850 goto GetDFSRefExit;
4853 cifs_dbg(FYI, "Decoding GetDFSRefer response BCC: %d Offset %d\n",
4854 get_bcc(&pSMBr->hdr), le16_to_cpu(pSMBr->t2.DataOffset));
4856 /* parse returned result into more usable form */
4857 rc = parse_DFS_referrals(pSMBr, num_of_nodes,
4858 target_nodes, nls_codepage, remap,
4859 search_name);
4861 GetDFSRefExit:
4862 cifs_buf_release(pSMB);
4864 if (rc == -EAGAIN)
4865 goto getDFSRetry;
4867 return rc;
4870 /* Query File System Info such as free space to old servers such as Win 9x */
4872 SMBOldQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
4873 struct kstatfs *FSData)
4875 /* level 0x01 SMB_QUERY_FILE_SYSTEM_INFO */
4876 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4877 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4878 FILE_SYSTEM_ALLOC_INFO *response_data;
4879 int rc = 0;
4880 int bytes_returned = 0;
4881 __u16 params, byte_count;
4883 cifs_dbg(FYI, "OldQFSInfo\n");
4884 oldQFSInfoRetry:
4885 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4886 (void **) &pSMBr);
4887 if (rc)
4888 return rc;
4890 params = 2; /* level */
4891 pSMB->TotalDataCount = 0;
4892 pSMB->MaxParameterCount = cpu_to_le16(2);
4893 pSMB->MaxDataCount = cpu_to_le16(1000);
4894 pSMB->MaxSetupCount = 0;
4895 pSMB->Reserved = 0;
4896 pSMB->Flags = 0;
4897 pSMB->Timeout = 0;
4898 pSMB->Reserved2 = 0;
4899 byte_count = params + 1 /* pad */ ;
4900 pSMB->TotalParameterCount = cpu_to_le16(params);
4901 pSMB->ParameterCount = pSMB->TotalParameterCount;
4902 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4903 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
4904 pSMB->DataCount = 0;
4905 pSMB->DataOffset = 0;
4906 pSMB->SetupCount = 1;
4907 pSMB->Reserved3 = 0;
4908 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
4909 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_ALLOCATION);
4910 inc_rfc1001_len(pSMB, byte_count);
4911 pSMB->ByteCount = cpu_to_le16(byte_count);
4913 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4914 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4915 if (rc) {
4916 cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
4917 } else { /* decode response */
4918 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4920 if (rc || get_bcc(&pSMBr->hdr) < 18)
4921 rc = -EIO; /* bad smb */
4922 else {
4923 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4924 cifs_dbg(FYI, "qfsinf resp BCC: %d Offset %d\n",
4925 get_bcc(&pSMBr->hdr), data_offset);
4927 response_data = (FILE_SYSTEM_ALLOC_INFO *)
4928 (((char *) &pSMBr->hdr.Protocol) + data_offset);
4929 FSData->f_bsize =
4930 le16_to_cpu(response_data->BytesPerSector) *
4931 le32_to_cpu(response_data->
4932 SectorsPerAllocationUnit);
4933 FSData->f_blocks =
4934 le32_to_cpu(response_data->TotalAllocationUnits);
4935 FSData->f_bfree = FSData->f_bavail =
4936 le32_to_cpu(response_data->FreeAllocationUnits);
4937 cifs_dbg(FYI, "Blocks: %lld Free: %lld Block size %ld\n",
4938 (unsigned long long)FSData->f_blocks,
4939 (unsigned long long)FSData->f_bfree,
4940 FSData->f_bsize);
4943 cifs_buf_release(pSMB);
4945 if (rc == -EAGAIN)
4946 goto oldQFSInfoRetry;
4948 return rc;
4952 CIFSSMBQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
4953 struct kstatfs *FSData)
4955 /* level 0x103 SMB_QUERY_FILE_SYSTEM_INFO */
4956 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4957 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4958 FILE_SYSTEM_INFO *response_data;
4959 int rc = 0;
4960 int bytes_returned = 0;
4961 __u16 params, byte_count;
4963 cifs_dbg(FYI, "In QFSInfo\n");
4964 QFSInfoRetry:
4965 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4966 (void **) &pSMBr);
4967 if (rc)
4968 return rc;
4970 params = 2; /* level */
4971 pSMB->TotalDataCount = 0;
4972 pSMB->MaxParameterCount = cpu_to_le16(2);
4973 pSMB->MaxDataCount = cpu_to_le16(1000);
4974 pSMB->MaxSetupCount = 0;
4975 pSMB->Reserved = 0;
4976 pSMB->Flags = 0;
4977 pSMB->Timeout = 0;
4978 pSMB->Reserved2 = 0;
4979 byte_count = params + 1 /* pad */ ;
4980 pSMB->TotalParameterCount = cpu_to_le16(params);
4981 pSMB->ParameterCount = pSMB->TotalParameterCount;
4982 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4983 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
4984 pSMB->DataCount = 0;
4985 pSMB->DataOffset = 0;
4986 pSMB->SetupCount = 1;
4987 pSMB->Reserved3 = 0;
4988 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
4989 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_SIZE_INFO);
4990 inc_rfc1001_len(pSMB, byte_count);
4991 pSMB->ByteCount = cpu_to_le16(byte_count);
4993 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4994 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4995 if (rc) {
4996 cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
4997 } else { /* decode response */
4998 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5000 if (rc || get_bcc(&pSMBr->hdr) < 24)
5001 rc = -EIO; /* bad smb */
5002 else {
5003 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5005 response_data =
5006 (FILE_SYSTEM_INFO
5007 *) (((char *) &pSMBr->hdr.Protocol) +
5008 data_offset);
5009 FSData->f_bsize =
5010 le32_to_cpu(response_data->BytesPerSector) *
5011 le32_to_cpu(response_data->
5012 SectorsPerAllocationUnit);
5013 FSData->f_blocks =
5014 le64_to_cpu(response_data->TotalAllocationUnits);
5015 FSData->f_bfree = FSData->f_bavail =
5016 le64_to_cpu(response_data->FreeAllocationUnits);
5017 cifs_dbg(FYI, "Blocks: %lld Free: %lld Block size %ld\n",
5018 (unsigned long long)FSData->f_blocks,
5019 (unsigned long long)FSData->f_bfree,
5020 FSData->f_bsize);
5023 cifs_buf_release(pSMB);
5025 if (rc == -EAGAIN)
5026 goto QFSInfoRetry;
5028 return rc;
5032 CIFSSMBQFSAttributeInfo(const unsigned int xid, struct cifs_tcon *tcon)
5034 /* level 0x105 SMB_QUERY_FILE_SYSTEM_INFO */
5035 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5036 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5037 FILE_SYSTEM_ATTRIBUTE_INFO *response_data;
5038 int rc = 0;
5039 int bytes_returned = 0;
5040 __u16 params, byte_count;
5042 cifs_dbg(FYI, "In QFSAttributeInfo\n");
5043 QFSAttributeRetry:
5044 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5045 (void **) &pSMBr);
5046 if (rc)
5047 return rc;
5049 params = 2; /* level */
5050 pSMB->TotalDataCount = 0;
5051 pSMB->MaxParameterCount = cpu_to_le16(2);
5052 /* BB find exact max SMB PDU from sess structure BB */
5053 pSMB->MaxDataCount = cpu_to_le16(1000);
5054 pSMB->MaxSetupCount = 0;
5055 pSMB->Reserved = 0;
5056 pSMB->Flags = 0;
5057 pSMB->Timeout = 0;
5058 pSMB->Reserved2 = 0;
5059 byte_count = params + 1 /* pad */ ;
5060 pSMB->TotalParameterCount = cpu_to_le16(params);
5061 pSMB->ParameterCount = pSMB->TotalParameterCount;
5062 pSMB->ParameterOffset = cpu_to_le16(offsetof(
5063 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
5064 pSMB->DataCount = 0;
5065 pSMB->DataOffset = 0;
5066 pSMB->SetupCount = 1;
5067 pSMB->Reserved3 = 0;
5068 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5069 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_ATTRIBUTE_INFO);
5070 inc_rfc1001_len(pSMB, byte_count);
5071 pSMB->ByteCount = cpu_to_le16(byte_count);
5073 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5074 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5075 if (rc) {
5076 cifs_dbg(VFS, "Send error in QFSAttributeInfo = %d\n", rc);
5077 } else { /* decode response */
5078 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5080 if (rc || get_bcc(&pSMBr->hdr) < 13) {
5081 /* BB also check if enough bytes returned */
5082 rc = -EIO; /* bad smb */
5083 } else {
5084 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5085 response_data =
5086 (FILE_SYSTEM_ATTRIBUTE_INFO
5087 *) (((char *) &pSMBr->hdr.Protocol) +
5088 data_offset);
5089 memcpy(&tcon->fsAttrInfo, response_data,
5090 sizeof(FILE_SYSTEM_ATTRIBUTE_INFO));
5093 cifs_buf_release(pSMB);
5095 if (rc == -EAGAIN)
5096 goto QFSAttributeRetry;
5098 return rc;
5102 CIFSSMBQFSDeviceInfo(const unsigned int xid, struct cifs_tcon *tcon)
5104 /* level 0x104 SMB_QUERY_FILE_SYSTEM_INFO */
5105 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5106 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5107 FILE_SYSTEM_DEVICE_INFO *response_data;
5108 int rc = 0;
5109 int bytes_returned = 0;
5110 __u16 params, byte_count;
5112 cifs_dbg(FYI, "In QFSDeviceInfo\n");
5113 QFSDeviceRetry:
5114 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5115 (void **) &pSMBr);
5116 if (rc)
5117 return rc;
5119 params = 2; /* level */
5120 pSMB->TotalDataCount = 0;
5121 pSMB->MaxParameterCount = cpu_to_le16(2);
5122 /* BB find exact max SMB PDU from sess structure BB */
5123 pSMB->MaxDataCount = cpu_to_le16(1000);
5124 pSMB->MaxSetupCount = 0;
5125 pSMB->Reserved = 0;
5126 pSMB->Flags = 0;
5127 pSMB->Timeout = 0;
5128 pSMB->Reserved2 = 0;
5129 byte_count = params + 1 /* pad */ ;
5130 pSMB->TotalParameterCount = cpu_to_le16(params);
5131 pSMB->ParameterCount = pSMB->TotalParameterCount;
5132 pSMB->ParameterOffset = cpu_to_le16(offsetof(
5133 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
5135 pSMB->DataCount = 0;
5136 pSMB->DataOffset = 0;
5137 pSMB->SetupCount = 1;
5138 pSMB->Reserved3 = 0;
5139 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5140 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_DEVICE_INFO);
5141 inc_rfc1001_len(pSMB, byte_count);
5142 pSMB->ByteCount = cpu_to_le16(byte_count);
5144 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5145 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5146 if (rc) {
5147 cifs_dbg(FYI, "Send error in QFSDeviceInfo = %d\n", rc);
5148 } else { /* decode response */
5149 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5151 if (rc || get_bcc(&pSMBr->hdr) <
5152 sizeof(FILE_SYSTEM_DEVICE_INFO))
5153 rc = -EIO; /* bad smb */
5154 else {
5155 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5156 response_data =
5157 (FILE_SYSTEM_DEVICE_INFO *)
5158 (((char *) &pSMBr->hdr.Protocol) +
5159 data_offset);
5160 memcpy(&tcon->fsDevInfo, response_data,
5161 sizeof(FILE_SYSTEM_DEVICE_INFO));
5164 cifs_buf_release(pSMB);
5166 if (rc == -EAGAIN)
5167 goto QFSDeviceRetry;
5169 return rc;
5173 CIFSSMBQFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon)
5175 /* level 0x200 SMB_QUERY_CIFS_UNIX_INFO */
5176 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5177 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5178 FILE_SYSTEM_UNIX_INFO *response_data;
5179 int rc = 0;
5180 int bytes_returned = 0;
5181 __u16 params, byte_count;
5183 cifs_dbg(FYI, "In QFSUnixInfo\n");
5184 QFSUnixRetry:
5185 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5186 (void **) &pSMB, (void **) &pSMBr);
5187 if (rc)
5188 return rc;
5190 params = 2; /* level */
5191 pSMB->TotalDataCount = 0;
5192 pSMB->DataCount = 0;
5193 pSMB->DataOffset = 0;
5194 pSMB->MaxParameterCount = cpu_to_le16(2);
5195 /* BB find exact max SMB PDU from sess structure BB */
5196 pSMB->MaxDataCount = cpu_to_le16(100);
5197 pSMB->MaxSetupCount = 0;
5198 pSMB->Reserved = 0;
5199 pSMB->Flags = 0;
5200 pSMB->Timeout = 0;
5201 pSMB->Reserved2 = 0;
5202 byte_count = params + 1 /* pad */ ;
5203 pSMB->ParameterCount = cpu_to_le16(params);
5204 pSMB->TotalParameterCount = pSMB->ParameterCount;
5205 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5206 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
5207 pSMB->SetupCount = 1;
5208 pSMB->Reserved3 = 0;
5209 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5210 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_CIFS_UNIX_INFO);
5211 inc_rfc1001_len(pSMB, byte_count);
5212 pSMB->ByteCount = cpu_to_le16(byte_count);
5214 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5215 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5216 if (rc) {
5217 cifs_dbg(VFS, "Send error in QFSUnixInfo = %d\n", rc);
5218 } else { /* decode response */
5219 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5221 if (rc || get_bcc(&pSMBr->hdr) < 13) {
5222 rc = -EIO; /* bad smb */
5223 } else {
5224 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5225 response_data =
5226 (FILE_SYSTEM_UNIX_INFO
5227 *) (((char *) &pSMBr->hdr.Protocol) +
5228 data_offset);
5229 memcpy(&tcon->fsUnixInfo, response_data,
5230 sizeof(FILE_SYSTEM_UNIX_INFO));
5233 cifs_buf_release(pSMB);
5235 if (rc == -EAGAIN)
5236 goto QFSUnixRetry;
5239 return rc;
5243 CIFSSMBSetFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon, __u64 cap)
5245 /* level 0x200 SMB_SET_CIFS_UNIX_INFO */
5246 TRANSACTION2_SETFSI_REQ *pSMB = NULL;
5247 TRANSACTION2_SETFSI_RSP *pSMBr = NULL;
5248 int rc = 0;
5249 int bytes_returned = 0;
5250 __u16 params, param_offset, offset, byte_count;
5252 cifs_dbg(FYI, "In SETFSUnixInfo\n");
5253 SETFSUnixRetry:
5254 /* BB switch to small buf init to save memory */
5255 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5256 (void **) &pSMB, (void **) &pSMBr);
5257 if (rc)
5258 return rc;
5260 params = 4; /* 2 bytes zero followed by info level. */
5261 pSMB->MaxSetupCount = 0;
5262 pSMB->Reserved = 0;
5263 pSMB->Flags = 0;
5264 pSMB->Timeout = 0;
5265 pSMB->Reserved2 = 0;
5266 param_offset = offsetof(struct smb_com_transaction2_setfsi_req, FileNum)
5267 - 4;
5268 offset = param_offset + params;
5270 pSMB->MaxParameterCount = cpu_to_le16(4);
5271 /* BB find exact max SMB PDU from sess structure BB */
5272 pSMB->MaxDataCount = cpu_to_le16(100);
5273 pSMB->SetupCount = 1;
5274 pSMB->Reserved3 = 0;
5275 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FS_INFORMATION);
5276 byte_count = 1 /* pad */ + params + 12;
5278 pSMB->DataCount = cpu_to_le16(12);
5279 pSMB->ParameterCount = cpu_to_le16(params);
5280 pSMB->TotalDataCount = pSMB->DataCount;
5281 pSMB->TotalParameterCount = pSMB->ParameterCount;
5282 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5283 pSMB->DataOffset = cpu_to_le16(offset);
5285 /* Params. */
5286 pSMB->FileNum = 0;
5287 pSMB->InformationLevel = cpu_to_le16(SMB_SET_CIFS_UNIX_INFO);
5289 /* Data. */
5290 pSMB->ClientUnixMajor = cpu_to_le16(CIFS_UNIX_MAJOR_VERSION);
5291 pSMB->ClientUnixMinor = cpu_to_le16(CIFS_UNIX_MINOR_VERSION);
5292 pSMB->ClientUnixCap = cpu_to_le64(cap);
5294 inc_rfc1001_len(pSMB, byte_count);
5295 pSMB->ByteCount = cpu_to_le16(byte_count);
5297 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5298 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5299 if (rc) {
5300 cifs_dbg(VFS, "Send error in SETFSUnixInfo = %d\n", rc);
5301 } else { /* decode response */
5302 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5303 if (rc)
5304 rc = -EIO; /* bad smb */
5306 cifs_buf_release(pSMB);
5308 if (rc == -EAGAIN)
5309 goto SETFSUnixRetry;
5311 return rc;
5317 CIFSSMBQFSPosixInfo(const unsigned int xid, struct cifs_tcon *tcon,
5318 struct kstatfs *FSData)
5320 /* level 0x201 SMB_QUERY_CIFS_POSIX_INFO */
5321 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5322 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5323 FILE_SYSTEM_POSIX_INFO *response_data;
5324 int rc = 0;
5325 int bytes_returned = 0;
5326 __u16 params, byte_count;
5328 cifs_dbg(FYI, "In QFSPosixInfo\n");
5329 QFSPosixRetry:
5330 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5331 (void **) &pSMBr);
5332 if (rc)
5333 return rc;
5335 params = 2; /* level */
5336 pSMB->TotalDataCount = 0;
5337 pSMB->DataCount = 0;
5338 pSMB->DataOffset = 0;
5339 pSMB->MaxParameterCount = cpu_to_le16(2);
5340 /* BB find exact max SMB PDU from sess structure BB */
5341 pSMB->MaxDataCount = cpu_to_le16(100);
5342 pSMB->MaxSetupCount = 0;
5343 pSMB->Reserved = 0;
5344 pSMB->Flags = 0;
5345 pSMB->Timeout = 0;
5346 pSMB->Reserved2 = 0;
5347 byte_count = params + 1 /* pad */ ;
5348 pSMB->ParameterCount = cpu_to_le16(params);
5349 pSMB->TotalParameterCount = pSMB->ParameterCount;
5350 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5351 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
5352 pSMB->SetupCount = 1;
5353 pSMB->Reserved3 = 0;
5354 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5355 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_FS_INFO);
5356 inc_rfc1001_len(pSMB, byte_count);
5357 pSMB->ByteCount = cpu_to_le16(byte_count);
5359 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5360 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5361 if (rc) {
5362 cifs_dbg(FYI, "Send error in QFSUnixInfo = %d\n", rc);
5363 } else { /* decode response */
5364 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5366 if (rc || get_bcc(&pSMBr->hdr) < 13) {
5367 rc = -EIO; /* bad smb */
5368 } else {
5369 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5370 response_data =
5371 (FILE_SYSTEM_POSIX_INFO
5372 *) (((char *) &pSMBr->hdr.Protocol) +
5373 data_offset);
5374 FSData->f_bsize =
5375 le32_to_cpu(response_data->BlockSize);
5376 FSData->f_blocks =
5377 le64_to_cpu(response_data->TotalBlocks);
5378 FSData->f_bfree =
5379 le64_to_cpu(response_data->BlocksAvail);
5380 if (response_data->UserBlocksAvail == cpu_to_le64(-1)) {
5381 FSData->f_bavail = FSData->f_bfree;
5382 } else {
5383 FSData->f_bavail =
5384 le64_to_cpu(response_data->UserBlocksAvail);
5386 if (response_data->TotalFileNodes != cpu_to_le64(-1))
5387 FSData->f_files =
5388 le64_to_cpu(response_data->TotalFileNodes);
5389 if (response_data->FreeFileNodes != cpu_to_le64(-1))
5390 FSData->f_ffree =
5391 le64_to_cpu(response_data->FreeFileNodes);
5394 cifs_buf_release(pSMB);
5396 if (rc == -EAGAIN)
5397 goto QFSPosixRetry;
5399 return rc;
5404 * We can not use write of zero bytes trick to set file size due to need for
5405 * large file support. Also note that this SetPathInfo is preferred to
5406 * SetFileInfo based method in next routine which is only needed to work around
5407 * a sharing violation bugin Samba which this routine can run into.
5410 CIFSSMBSetEOF(const unsigned int xid, struct cifs_tcon *tcon,
5411 const char *file_name, __u64 size, struct cifs_sb_info *cifs_sb,
5412 bool set_allocation)
5414 struct smb_com_transaction2_spi_req *pSMB = NULL;
5415 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
5416 struct file_end_of_file_info *parm_data;
5417 int name_len;
5418 int rc = 0;
5419 int bytes_returned = 0;
5420 int remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR;
5422 __u16 params, byte_count, data_count, param_offset, offset;
5424 cifs_dbg(FYI, "In SetEOF\n");
5425 SetEOFRetry:
5426 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5427 (void **) &pSMBr);
5428 if (rc)
5429 return rc;
5431 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5432 name_len =
5433 cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
5434 PATH_MAX, cifs_sb->local_nls, remap);
5435 name_len++; /* trailing null */
5436 name_len *= 2;
5437 } else { /* BB improve the check for buffer overruns BB */
5438 name_len = strnlen(file_name, PATH_MAX);
5439 name_len++; /* trailing null */
5440 strncpy(pSMB->FileName, file_name, name_len);
5442 params = 6 + name_len;
5443 data_count = sizeof(struct file_end_of_file_info);
5444 pSMB->MaxParameterCount = cpu_to_le16(2);
5445 pSMB->MaxDataCount = cpu_to_le16(4100);
5446 pSMB->MaxSetupCount = 0;
5447 pSMB->Reserved = 0;
5448 pSMB->Flags = 0;
5449 pSMB->Timeout = 0;
5450 pSMB->Reserved2 = 0;
5451 param_offset = offsetof(struct smb_com_transaction2_spi_req,
5452 InformationLevel) - 4;
5453 offset = param_offset + params;
5454 if (set_allocation) {
5455 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5456 pSMB->InformationLevel =
5457 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5458 else
5459 pSMB->InformationLevel =
5460 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
5461 } else /* Set File Size */ {
5462 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5463 pSMB->InformationLevel =
5464 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
5465 else
5466 pSMB->InformationLevel =
5467 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
5470 parm_data =
5471 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol) +
5472 offset);
5473 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5474 pSMB->DataOffset = cpu_to_le16(offset);
5475 pSMB->SetupCount = 1;
5476 pSMB->Reserved3 = 0;
5477 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5478 byte_count = 3 /* pad */ + params + data_count;
5479 pSMB->DataCount = cpu_to_le16(data_count);
5480 pSMB->TotalDataCount = pSMB->DataCount;
5481 pSMB->ParameterCount = cpu_to_le16(params);
5482 pSMB->TotalParameterCount = pSMB->ParameterCount;
5483 pSMB->Reserved4 = 0;
5484 inc_rfc1001_len(pSMB, byte_count);
5485 parm_data->FileSize = cpu_to_le64(size);
5486 pSMB->ByteCount = cpu_to_le16(byte_count);
5487 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5488 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5489 if (rc)
5490 cifs_dbg(FYI, "SetPathInfo (file size) returned %d\n", rc);
5492 cifs_buf_release(pSMB);
5494 if (rc == -EAGAIN)
5495 goto SetEOFRetry;
5497 return rc;
5501 CIFSSMBSetFileSize(const unsigned int xid, struct cifs_tcon *tcon,
5502 struct cifsFileInfo *cfile, __u64 size, bool set_allocation)
5504 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5505 struct file_end_of_file_info *parm_data;
5506 int rc = 0;
5507 __u16 params, param_offset, offset, byte_count, count;
5509 cifs_dbg(FYI, "SetFileSize (via SetFileInfo) %lld\n",
5510 (long long)size);
5511 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5513 if (rc)
5514 return rc;
5516 pSMB->hdr.Pid = cpu_to_le16((__u16)cfile->pid);
5517 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(cfile->pid >> 16));
5519 params = 6;
5520 pSMB->MaxSetupCount = 0;
5521 pSMB->Reserved = 0;
5522 pSMB->Flags = 0;
5523 pSMB->Timeout = 0;
5524 pSMB->Reserved2 = 0;
5525 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5526 offset = param_offset + params;
5528 count = sizeof(struct file_end_of_file_info);
5529 pSMB->MaxParameterCount = cpu_to_le16(2);
5530 /* BB find exact max SMB PDU from sess structure BB */
5531 pSMB->MaxDataCount = cpu_to_le16(1000);
5532 pSMB->SetupCount = 1;
5533 pSMB->Reserved3 = 0;
5534 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5535 byte_count = 3 /* pad */ + params + count;
5536 pSMB->DataCount = cpu_to_le16(count);
5537 pSMB->ParameterCount = cpu_to_le16(params);
5538 pSMB->TotalDataCount = pSMB->DataCount;
5539 pSMB->TotalParameterCount = pSMB->ParameterCount;
5540 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5541 parm_data =
5542 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol)
5543 + offset);
5544 pSMB->DataOffset = cpu_to_le16(offset);
5545 parm_data->FileSize = cpu_to_le64(size);
5546 pSMB->Fid = cfile->fid.netfid;
5547 if (set_allocation) {
5548 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5549 pSMB->InformationLevel =
5550 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5551 else
5552 pSMB->InformationLevel =
5553 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
5554 } else /* Set File Size */ {
5555 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5556 pSMB->InformationLevel =
5557 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
5558 else
5559 pSMB->InformationLevel =
5560 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
5562 pSMB->Reserved4 = 0;
5563 inc_rfc1001_len(pSMB, byte_count);
5564 pSMB->ByteCount = cpu_to_le16(byte_count);
5565 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
5566 if (rc) {
5567 cifs_dbg(FYI, "Send error in SetFileInfo (SetFileSize) = %d\n",
5568 rc);
5571 /* Note: On -EAGAIN error only caller can retry on handle based calls
5572 since file handle passed in no longer valid */
5574 return rc;
5577 /* Some legacy servers such as NT4 require that the file times be set on
5578 an open handle, rather than by pathname - this is awkward due to
5579 potential access conflicts on the open, but it is unavoidable for these
5580 old servers since the only other choice is to go from 100 nanosecond DCE
5581 time and resort to the original setpathinfo level which takes the ancient
5582 DOS time format with 2 second granularity */
5584 CIFSSMBSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
5585 const FILE_BASIC_INFO *data, __u16 fid, __u32 pid_of_opener)
5587 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5588 char *data_offset;
5589 int rc = 0;
5590 __u16 params, param_offset, offset, byte_count, count;
5592 cifs_dbg(FYI, "Set Times (via SetFileInfo)\n");
5593 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5595 if (rc)
5596 return rc;
5598 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5599 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5601 params = 6;
5602 pSMB->MaxSetupCount = 0;
5603 pSMB->Reserved = 0;
5604 pSMB->Flags = 0;
5605 pSMB->Timeout = 0;
5606 pSMB->Reserved2 = 0;
5607 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5608 offset = param_offset + params;
5610 data_offset = (char *)pSMB +
5611 offsetof(struct smb_hdr, Protocol) + offset;
5613 count = sizeof(FILE_BASIC_INFO);
5614 pSMB->MaxParameterCount = cpu_to_le16(2);
5615 /* BB find max SMB PDU from sess */
5616 pSMB->MaxDataCount = cpu_to_le16(1000);
5617 pSMB->SetupCount = 1;
5618 pSMB->Reserved3 = 0;
5619 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5620 byte_count = 3 /* pad */ + params + count;
5621 pSMB->DataCount = cpu_to_le16(count);
5622 pSMB->ParameterCount = cpu_to_le16(params);
5623 pSMB->TotalDataCount = pSMB->DataCount;
5624 pSMB->TotalParameterCount = pSMB->ParameterCount;
5625 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5626 pSMB->DataOffset = cpu_to_le16(offset);
5627 pSMB->Fid = fid;
5628 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5629 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5630 else
5631 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5632 pSMB->Reserved4 = 0;
5633 inc_rfc1001_len(pSMB, byte_count);
5634 pSMB->ByteCount = cpu_to_le16(byte_count);
5635 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
5636 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
5637 if (rc)
5638 cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n",
5639 rc);
5641 /* Note: On -EAGAIN error only caller can retry on handle based calls
5642 since file handle passed in no longer valid */
5644 return rc;
5648 CIFSSMBSetFileDisposition(const unsigned int xid, struct cifs_tcon *tcon,
5649 bool delete_file, __u16 fid, __u32 pid_of_opener)
5651 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5652 char *data_offset;
5653 int rc = 0;
5654 __u16 params, param_offset, offset, byte_count, count;
5656 cifs_dbg(FYI, "Set File Disposition (via SetFileInfo)\n");
5657 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5659 if (rc)
5660 return rc;
5662 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5663 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5665 params = 6;
5666 pSMB->MaxSetupCount = 0;
5667 pSMB->Reserved = 0;
5668 pSMB->Flags = 0;
5669 pSMB->Timeout = 0;
5670 pSMB->Reserved2 = 0;
5671 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5672 offset = param_offset + params;
5674 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5676 count = 1;
5677 pSMB->MaxParameterCount = cpu_to_le16(2);
5678 /* BB find max SMB PDU from sess */
5679 pSMB->MaxDataCount = cpu_to_le16(1000);
5680 pSMB->SetupCount = 1;
5681 pSMB->Reserved3 = 0;
5682 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5683 byte_count = 3 /* pad */ + params + count;
5684 pSMB->DataCount = cpu_to_le16(count);
5685 pSMB->ParameterCount = cpu_to_le16(params);
5686 pSMB->TotalDataCount = pSMB->DataCount;
5687 pSMB->TotalParameterCount = pSMB->ParameterCount;
5688 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5689 pSMB->DataOffset = cpu_to_le16(offset);
5690 pSMB->Fid = fid;
5691 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_DISPOSITION_INFO);
5692 pSMB->Reserved4 = 0;
5693 inc_rfc1001_len(pSMB, byte_count);
5694 pSMB->ByteCount = cpu_to_le16(byte_count);
5695 *data_offset = delete_file ? 1 : 0;
5696 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
5697 if (rc)
5698 cifs_dbg(FYI, "Send error in SetFileDisposition = %d\n", rc);
5700 return rc;
5704 CIFSSMBSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
5705 const char *fileName, const FILE_BASIC_INFO *data,
5706 const struct nls_table *nls_codepage, int remap)
5708 TRANSACTION2_SPI_REQ *pSMB = NULL;
5709 TRANSACTION2_SPI_RSP *pSMBr = NULL;
5710 int name_len;
5711 int rc = 0;
5712 int bytes_returned = 0;
5713 char *data_offset;
5714 __u16 params, param_offset, offset, byte_count, count;
5716 cifs_dbg(FYI, "In SetTimes\n");
5718 SetTimesRetry:
5719 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5720 (void **) &pSMBr);
5721 if (rc)
5722 return rc;
5724 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5725 name_len =
5726 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
5727 PATH_MAX, nls_codepage, remap);
5728 name_len++; /* trailing null */
5729 name_len *= 2;
5730 } else { /* BB improve the check for buffer overruns BB */
5731 name_len = strnlen(fileName, PATH_MAX);
5732 name_len++; /* trailing null */
5733 strncpy(pSMB->FileName, fileName, name_len);
5736 params = 6 + name_len;
5737 count = sizeof(FILE_BASIC_INFO);
5738 pSMB->MaxParameterCount = cpu_to_le16(2);
5739 /* BB find max SMB PDU from sess structure BB */
5740 pSMB->MaxDataCount = cpu_to_le16(1000);
5741 pSMB->MaxSetupCount = 0;
5742 pSMB->Reserved = 0;
5743 pSMB->Flags = 0;
5744 pSMB->Timeout = 0;
5745 pSMB->Reserved2 = 0;
5746 param_offset = offsetof(struct smb_com_transaction2_spi_req,
5747 InformationLevel) - 4;
5748 offset = param_offset + params;
5749 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5750 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5751 pSMB->DataOffset = cpu_to_le16(offset);
5752 pSMB->SetupCount = 1;
5753 pSMB->Reserved3 = 0;
5754 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5755 byte_count = 3 /* pad */ + params + count;
5757 pSMB->DataCount = cpu_to_le16(count);
5758 pSMB->ParameterCount = cpu_to_le16(params);
5759 pSMB->TotalDataCount = pSMB->DataCount;
5760 pSMB->TotalParameterCount = pSMB->ParameterCount;
5761 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5762 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5763 else
5764 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5765 pSMB->Reserved4 = 0;
5766 inc_rfc1001_len(pSMB, byte_count);
5767 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
5768 pSMB->ByteCount = cpu_to_le16(byte_count);
5769 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5770 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5771 if (rc)
5772 cifs_dbg(FYI, "SetPathInfo (times) returned %d\n", rc);
5774 cifs_buf_release(pSMB);
5776 if (rc == -EAGAIN)
5777 goto SetTimesRetry;
5779 return rc;
5782 /* Can not be used to set time stamps yet (due to old DOS time format) */
5783 /* Can be used to set attributes */
5784 #if 0 /* Possibly not needed - since it turns out that strangely NT4 has a bug
5785 handling it anyway and NT4 was what we thought it would be needed for
5786 Do not delete it until we prove whether needed for Win9x though */
5788 CIFSSMBSetAttrLegacy(unsigned int xid, struct cifs_tcon *tcon, char *fileName,
5789 __u16 dos_attrs, const struct nls_table *nls_codepage)
5791 SETATTR_REQ *pSMB = NULL;
5792 SETATTR_RSP *pSMBr = NULL;
5793 int rc = 0;
5794 int bytes_returned;
5795 int name_len;
5797 cifs_dbg(FYI, "In SetAttrLegacy\n");
5799 SetAttrLgcyRetry:
5800 rc = smb_init(SMB_COM_SETATTR, 8, tcon, (void **) &pSMB,
5801 (void **) &pSMBr);
5802 if (rc)
5803 return rc;
5805 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5806 name_len =
5807 ConvertToUTF16((__le16 *) pSMB->fileName, fileName,
5808 PATH_MAX, nls_codepage);
5809 name_len++; /* trailing null */
5810 name_len *= 2;
5811 } else { /* BB improve the check for buffer overruns BB */
5812 name_len = strnlen(fileName, PATH_MAX);
5813 name_len++; /* trailing null */
5814 strncpy(pSMB->fileName, fileName, name_len);
5816 pSMB->attr = cpu_to_le16(dos_attrs);
5817 pSMB->BufferFormat = 0x04;
5818 inc_rfc1001_len(pSMB, name_len + 1);
5819 pSMB->ByteCount = cpu_to_le16(name_len + 1);
5820 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5821 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5822 if (rc)
5823 cifs_dbg(FYI, "Error in LegacySetAttr = %d\n", rc);
5825 cifs_buf_release(pSMB);
5827 if (rc == -EAGAIN)
5828 goto SetAttrLgcyRetry;
5830 return rc;
5832 #endif /* temporarily unneeded SetAttr legacy function */
5834 static void
5835 cifs_fill_unix_set_info(FILE_UNIX_BASIC_INFO *data_offset,
5836 const struct cifs_unix_set_info_args *args)
5838 u64 uid = NO_CHANGE_64, gid = NO_CHANGE_64;
5839 u64 mode = args->mode;
5841 if (uid_valid(args->uid))
5842 uid = from_kuid(&init_user_ns, args->uid);
5843 if (gid_valid(args->gid))
5844 gid = from_kgid(&init_user_ns, args->gid);
5847 * Samba server ignores set of file size to zero due to bugs in some
5848 * older clients, but we should be precise - we use SetFileSize to
5849 * set file size and do not want to truncate file size to zero
5850 * accidentally as happened on one Samba server beta by putting
5851 * zero instead of -1 here
5853 data_offset->EndOfFile = cpu_to_le64(NO_CHANGE_64);
5854 data_offset->NumOfBytes = cpu_to_le64(NO_CHANGE_64);
5855 data_offset->LastStatusChange = cpu_to_le64(args->ctime);
5856 data_offset->LastAccessTime = cpu_to_le64(args->atime);
5857 data_offset->LastModificationTime = cpu_to_le64(args->mtime);
5858 data_offset->Uid = cpu_to_le64(uid);
5859 data_offset->Gid = cpu_to_le64(gid);
5860 /* better to leave device as zero when it is */
5861 data_offset->DevMajor = cpu_to_le64(MAJOR(args->device));
5862 data_offset->DevMinor = cpu_to_le64(MINOR(args->device));
5863 data_offset->Permissions = cpu_to_le64(mode);
5865 if (S_ISREG(mode))
5866 data_offset->Type = cpu_to_le32(UNIX_FILE);
5867 else if (S_ISDIR(mode))
5868 data_offset->Type = cpu_to_le32(UNIX_DIR);
5869 else if (S_ISLNK(mode))
5870 data_offset->Type = cpu_to_le32(UNIX_SYMLINK);
5871 else if (S_ISCHR(mode))
5872 data_offset->Type = cpu_to_le32(UNIX_CHARDEV);
5873 else if (S_ISBLK(mode))
5874 data_offset->Type = cpu_to_le32(UNIX_BLOCKDEV);
5875 else if (S_ISFIFO(mode))
5876 data_offset->Type = cpu_to_le32(UNIX_FIFO);
5877 else if (S_ISSOCK(mode))
5878 data_offset->Type = cpu_to_le32(UNIX_SOCKET);
5882 CIFSSMBUnixSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
5883 const struct cifs_unix_set_info_args *args,
5884 u16 fid, u32 pid_of_opener)
5886 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5887 char *data_offset;
5888 int rc = 0;
5889 u16 params, param_offset, offset, byte_count, count;
5891 cifs_dbg(FYI, "Set Unix Info (via SetFileInfo)\n");
5892 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5894 if (rc)
5895 return rc;
5897 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5898 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5900 params = 6;
5901 pSMB->MaxSetupCount = 0;
5902 pSMB->Reserved = 0;
5903 pSMB->Flags = 0;
5904 pSMB->Timeout = 0;
5905 pSMB->Reserved2 = 0;
5906 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5907 offset = param_offset + params;
5909 data_offset = (char *)pSMB +
5910 offsetof(struct smb_hdr, Protocol) + offset;
5912 count = sizeof(FILE_UNIX_BASIC_INFO);
5914 pSMB->MaxParameterCount = cpu_to_le16(2);
5915 /* BB find max SMB PDU from sess */
5916 pSMB->MaxDataCount = cpu_to_le16(1000);
5917 pSMB->SetupCount = 1;
5918 pSMB->Reserved3 = 0;
5919 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5920 byte_count = 3 /* pad */ + params + count;
5921 pSMB->DataCount = cpu_to_le16(count);
5922 pSMB->ParameterCount = cpu_to_le16(params);
5923 pSMB->TotalDataCount = pSMB->DataCount;
5924 pSMB->TotalParameterCount = pSMB->ParameterCount;
5925 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5926 pSMB->DataOffset = cpu_to_le16(offset);
5927 pSMB->Fid = fid;
5928 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
5929 pSMB->Reserved4 = 0;
5930 inc_rfc1001_len(pSMB, byte_count);
5931 pSMB->ByteCount = cpu_to_le16(byte_count);
5933 cifs_fill_unix_set_info((FILE_UNIX_BASIC_INFO *)data_offset, args);
5935 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
5936 if (rc)
5937 cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n",
5938 rc);
5940 /* Note: On -EAGAIN error only caller can retry on handle based calls
5941 since file handle passed in no longer valid */
5943 return rc;
5947 CIFSSMBUnixSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
5948 const char *file_name,
5949 const struct cifs_unix_set_info_args *args,
5950 const struct nls_table *nls_codepage, int remap)
5952 TRANSACTION2_SPI_REQ *pSMB = NULL;
5953 TRANSACTION2_SPI_RSP *pSMBr = NULL;
5954 int name_len;
5955 int rc = 0;
5956 int bytes_returned = 0;
5957 FILE_UNIX_BASIC_INFO *data_offset;
5958 __u16 params, param_offset, offset, count, byte_count;
5960 cifs_dbg(FYI, "In SetUID/GID/Mode\n");
5961 setPermsRetry:
5962 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5963 (void **) &pSMBr);
5964 if (rc)
5965 return rc;
5967 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5968 name_len =
5969 cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
5970 PATH_MAX, nls_codepage, remap);
5971 name_len++; /* trailing null */
5972 name_len *= 2;
5973 } else { /* BB improve the check for buffer overruns BB */
5974 name_len = strnlen(file_name, PATH_MAX);
5975 name_len++; /* trailing null */
5976 strncpy(pSMB->FileName, file_name, name_len);
5979 params = 6 + name_len;
5980 count = sizeof(FILE_UNIX_BASIC_INFO);
5981 pSMB->MaxParameterCount = cpu_to_le16(2);
5982 /* BB find max SMB PDU from sess structure BB */
5983 pSMB->MaxDataCount = cpu_to_le16(1000);
5984 pSMB->MaxSetupCount = 0;
5985 pSMB->Reserved = 0;
5986 pSMB->Flags = 0;
5987 pSMB->Timeout = 0;
5988 pSMB->Reserved2 = 0;
5989 param_offset = offsetof(struct smb_com_transaction2_spi_req,
5990 InformationLevel) - 4;
5991 offset = param_offset + params;
5992 data_offset =
5993 (FILE_UNIX_BASIC_INFO *) ((char *) &pSMB->hdr.Protocol +
5994 offset);
5995 memset(data_offset, 0, count);
5996 pSMB->DataOffset = cpu_to_le16(offset);
5997 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5998 pSMB->SetupCount = 1;
5999 pSMB->Reserved3 = 0;
6000 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6001 byte_count = 3 /* pad */ + params + count;
6002 pSMB->ParameterCount = cpu_to_le16(params);
6003 pSMB->DataCount = cpu_to_le16(count);
6004 pSMB->TotalParameterCount = pSMB->ParameterCount;
6005 pSMB->TotalDataCount = pSMB->DataCount;
6006 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
6007 pSMB->Reserved4 = 0;
6008 inc_rfc1001_len(pSMB, byte_count);
6010 cifs_fill_unix_set_info(data_offset, args);
6012 pSMB->ByteCount = cpu_to_le16(byte_count);
6013 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6014 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
6015 if (rc)
6016 cifs_dbg(FYI, "SetPathInfo (perms) returned %d\n", rc);
6018 cifs_buf_release(pSMB);
6019 if (rc == -EAGAIN)
6020 goto setPermsRetry;
6021 return rc;
6024 #ifdef CONFIG_CIFS_XATTR
6026 * Do a path-based QUERY_ALL_EAS call and parse the result. This is a common
6027 * function used by listxattr and getxattr type calls. When ea_name is set,
6028 * it looks for that attribute name and stuffs that value into the EAData
6029 * buffer. When ea_name is NULL, it stuffs a list of attribute names into the
6030 * buffer. In both cases, the return value is either the length of the
6031 * resulting data or a negative error code. If EAData is a NULL pointer then
6032 * the data isn't copied to it, but the length is returned.
6034 ssize_t
6035 CIFSSMBQAllEAs(const unsigned int xid, struct cifs_tcon *tcon,
6036 const unsigned char *searchName, const unsigned char *ea_name,
6037 char *EAData, size_t buf_size,
6038 const struct nls_table *nls_codepage, int remap)
6040 /* BB assumes one setup word */
6041 TRANSACTION2_QPI_REQ *pSMB = NULL;
6042 TRANSACTION2_QPI_RSP *pSMBr = NULL;
6043 int rc = 0;
6044 int bytes_returned;
6045 int list_len;
6046 struct fealist *ea_response_data;
6047 struct fea *temp_fea;
6048 char *temp_ptr;
6049 char *end_of_smb;
6050 __u16 params, byte_count, data_offset;
6051 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
6053 cifs_dbg(FYI, "In Query All EAs path %s\n", searchName);
6054 QAllEAsRetry:
6055 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6056 (void **) &pSMBr);
6057 if (rc)
6058 return rc;
6060 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6061 list_len =
6062 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
6063 PATH_MAX, nls_codepage, remap);
6064 list_len++; /* trailing null */
6065 list_len *= 2;
6066 } else { /* BB improve the check for buffer overruns BB */
6067 list_len = strnlen(searchName, PATH_MAX);
6068 list_len++; /* trailing null */
6069 strncpy(pSMB->FileName, searchName, list_len);
6072 params = 2 /* level */ + 4 /* reserved */ + list_len /* includes NUL */;
6073 pSMB->TotalDataCount = 0;
6074 pSMB->MaxParameterCount = cpu_to_le16(2);
6075 /* BB find exact max SMB PDU from sess structure BB */
6076 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
6077 pSMB->MaxSetupCount = 0;
6078 pSMB->Reserved = 0;
6079 pSMB->Flags = 0;
6080 pSMB->Timeout = 0;
6081 pSMB->Reserved2 = 0;
6082 pSMB->ParameterOffset = cpu_to_le16(offsetof(
6083 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
6084 pSMB->DataCount = 0;
6085 pSMB->DataOffset = 0;
6086 pSMB->SetupCount = 1;
6087 pSMB->Reserved3 = 0;
6088 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
6089 byte_count = params + 1 /* pad */ ;
6090 pSMB->TotalParameterCount = cpu_to_le16(params);
6091 pSMB->ParameterCount = pSMB->TotalParameterCount;
6092 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_QUERY_ALL_EAS);
6093 pSMB->Reserved4 = 0;
6094 inc_rfc1001_len(pSMB, byte_count);
6095 pSMB->ByteCount = cpu_to_le16(byte_count);
6097 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6098 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
6099 if (rc) {
6100 cifs_dbg(FYI, "Send error in QueryAllEAs = %d\n", rc);
6101 goto QAllEAsOut;
6105 /* BB also check enough total bytes returned */
6106 /* BB we need to improve the validity checking
6107 of these trans2 responses */
6109 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
6110 if (rc || get_bcc(&pSMBr->hdr) < 4) {
6111 rc = -EIO; /* bad smb */
6112 goto QAllEAsOut;
6115 /* check that length of list is not more than bcc */
6116 /* check that each entry does not go beyond length
6117 of list */
6118 /* check that each element of each entry does not
6119 go beyond end of list */
6120 /* validate_trans2_offsets() */
6121 /* BB check if start of smb + data_offset > &bcc+ bcc */
6123 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
6124 ea_response_data = (struct fealist *)
6125 (((char *) &pSMBr->hdr.Protocol) + data_offset);
6127 list_len = le32_to_cpu(ea_response_data->list_len);
6128 cifs_dbg(FYI, "ea length %d\n", list_len);
6129 if (list_len <= 8) {
6130 cifs_dbg(FYI, "empty EA list returned from server\n");
6131 goto QAllEAsOut;
6134 /* make sure list_len doesn't go past end of SMB */
6135 end_of_smb = (char *)pByteArea(&pSMBr->hdr) + get_bcc(&pSMBr->hdr);
6136 if ((char *)ea_response_data + list_len > end_of_smb) {
6137 cifs_dbg(FYI, "EA list appears to go beyond SMB\n");
6138 rc = -EIO;
6139 goto QAllEAsOut;
6142 /* account for ea list len */
6143 list_len -= 4;
6144 temp_fea = ea_response_data->list;
6145 temp_ptr = (char *)temp_fea;
6146 while (list_len > 0) {
6147 unsigned int name_len;
6148 __u16 value_len;
6150 list_len -= 4;
6151 temp_ptr += 4;
6152 /* make sure we can read name_len and value_len */
6153 if (list_len < 0) {
6154 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
6155 rc = -EIO;
6156 goto QAllEAsOut;
6159 name_len = temp_fea->name_len;
6160 value_len = le16_to_cpu(temp_fea->value_len);
6161 list_len -= name_len + 1 + value_len;
6162 if (list_len < 0) {
6163 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
6164 rc = -EIO;
6165 goto QAllEAsOut;
6168 if (ea_name) {
6169 if (ea_name_len == name_len &&
6170 memcmp(ea_name, temp_ptr, name_len) == 0) {
6171 temp_ptr += name_len + 1;
6172 rc = value_len;
6173 if (buf_size == 0)
6174 goto QAllEAsOut;
6175 if ((size_t)value_len > buf_size) {
6176 rc = -ERANGE;
6177 goto QAllEAsOut;
6179 memcpy(EAData, temp_ptr, value_len);
6180 goto QAllEAsOut;
6182 } else {
6183 /* account for prefix user. and trailing null */
6184 rc += (5 + 1 + name_len);
6185 if (rc < (int) buf_size) {
6186 memcpy(EAData, "user.", 5);
6187 EAData += 5;
6188 memcpy(EAData, temp_ptr, name_len);
6189 EAData += name_len;
6190 /* null terminate name */
6191 *EAData = 0;
6192 ++EAData;
6193 } else if (buf_size == 0) {
6194 /* skip copy - calc size only */
6195 } else {
6196 /* stop before overrun buffer */
6197 rc = -ERANGE;
6198 break;
6201 temp_ptr += name_len + 1 + value_len;
6202 temp_fea = (struct fea *)temp_ptr;
6205 /* didn't find the named attribute */
6206 if (ea_name)
6207 rc = -ENODATA;
6209 QAllEAsOut:
6210 cifs_buf_release(pSMB);
6211 if (rc == -EAGAIN)
6212 goto QAllEAsRetry;
6214 return (ssize_t)rc;
6218 CIFSSMBSetEA(const unsigned int xid, struct cifs_tcon *tcon,
6219 const char *fileName, const char *ea_name, const void *ea_value,
6220 const __u16 ea_value_len, const struct nls_table *nls_codepage,
6221 int remap)
6223 struct smb_com_transaction2_spi_req *pSMB = NULL;
6224 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
6225 struct fealist *parm_data;
6226 int name_len;
6227 int rc = 0;
6228 int bytes_returned = 0;
6229 __u16 params, param_offset, byte_count, offset, count;
6231 cifs_dbg(FYI, "In SetEA\n");
6232 SetEARetry:
6233 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6234 (void **) &pSMBr);
6235 if (rc)
6236 return rc;
6238 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6239 name_len =
6240 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
6241 PATH_MAX, nls_codepage, remap);
6242 name_len++; /* trailing null */
6243 name_len *= 2;
6244 } else { /* BB improve the check for buffer overruns BB */
6245 name_len = strnlen(fileName, PATH_MAX);
6246 name_len++; /* trailing null */
6247 strncpy(pSMB->FileName, fileName, name_len);
6250 params = 6 + name_len;
6252 /* done calculating parms using name_len of file name,
6253 now use name_len to calculate length of ea name
6254 we are going to create in the inode xattrs */
6255 if (ea_name == NULL)
6256 name_len = 0;
6257 else
6258 name_len = strnlen(ea_name, 255);
6260 count = sizeof(*parm_data) + ea_value_len + name_len;
6261 pSMB->MaxParameterCount = cpu_to_le16(2);
6262 /* BB find max SMB PDU from sess */
6263 pSMB->MaxDataCount = cpu_to_le16(1000);
6264 pSMB->MaxSetupCount = 0;
6265 pSMB->Reserved = 0;
6266 pSMB->Flags = 0;
6267 pSMB->Timeout = 0;
6268 pSMB->Reserved2 = 0;
6269 param_offset = offsetof(struct smb_com_transaction2_spi_req,
6270 InformationLevel) - 4;
6271 offset = param_offset + params;
6272 pSMB->InformationLevel =
6273 cpu_to_le16(SMB_SET_FILE_EA);
6275 parm_data =
6276 (struct fealist *) (((char *) &pSMB->hdr.Protocol) +
6277 offset);
6278 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6279 pSMB->DataOffset = cpu_to_le16(offset);
6280 pSMB->SetupCount = 1;
6281 pSMB->Reserved3 = 0;
6282 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6283 byte_count = 3 /* pad */ + params + count;
6284 pSMB->DataCount = cpu_to_le16(count);
6285 parm_data->list_len = cpu_to_le32(count);
6286 parm_data->list[0].EA_flags = 0;
6287 /* we checked above that name len is less than 255 */
6288 parm_data->list[0].name_len = (__u8)name_len;
6289 /* EA names are always ASCII */
6290 if (ea_name)
6291 strncpy(parm_data->list[0].name, ea_name, name_len);
6292 parm_data->list[0].name[name_len] = 0;
6293 parm_data->list[0].value_len = cpu_to_le16(ea_value_len);
6294 /* caller ensures that ea_value_len is less than 64K but
6295 we need to ensure that it fits within the smb */
6297 /*BB add length check to see if it would fit in
6298 negotiated SMB buffer size BB */
6299 /* if (ea_value_len > buffer_size - 512 (enough for header)) */
6300 if (ea_value_len)
6301 memcpy(parm_data->list[0].name+name_len+1,
6302 ea_value, ea_value_len);
6304 pSMB->TotalDataCount = pSMB->DataCount;
6305 pSMB->ParameterCount = cpu_to_le16(params);
6306 pSMB->TotalParameterCount = pSMB->ParameterCount;
6307 pSMB->Reserved4 = 0;
6308 inc_rfc1001_len(pSMB, byte_count);
6309 pSMB->ByteCount = cpu_to_le16(byte_count);
6310 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6311 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
6312 if (rc)
6313 cifs_dbg(FYI, "SetPathInfo (EA) returned %d\n", rc);
6315 cifs_buf_release(pSMB);
6317 if (rc == -EAGAIN)
6318 goto SetEARetry;
6320 return rc;
6322 #endif
6324 #ifdef CONFIG_CIFS_DNOTIFY_EXPERIMENTAL /* BB unused temporarily */
6326 * Years ago the kernel added a "dnotify" function for Samba server,
6327 * to allow network clients (such as Windows) to display updated
6328 * lists of files in directory listings automatically when
6329 * files are added by one user when another user has the
6330 * same directory open on their desktop. The Linux cifs kernel
6331 * client hooked into the kernel side of this interface for
6332 * the same reason, but ironically when the VFS moved from
6333 * "dnotify" to "inotify" it became harder to plug in Linux
6334 * network file system clients (the most obvious use case
6335 * for notify interfaces is when multiple users can update
6336 * the contents of the same directory - exactly what network
6337 * file systems can do) although the server (Samba) could
6338 * still use it. For the short term we leave the worker
6339 * function ifdeffed out (below) until inotify is fixed
6340 * in the VFS to make it easier to plug in network file
6341 * system clients. If inotify turns out to be permanently
6342 * incompatible for network fs clients, we could instead simply
6343 * expose this config flag by adding a future cifs (and smb2) notify ioctl.
6345 int CIFSSMBNotify(const unsigned int xid, struct cifs_tcon *tcon,
6346 const int notify_subdirs, const __u16 netfid,
6347 __u32 filter, struct file *pfile, int multishot,
6348 const struct nls_table *nls_codepage)
6350 int rc = 0;
6351 struct smb_com_transaction_change_notify_req *pSMB = NULL;
6352 struct smb_com_ntransaction_change_notify_rsp *pSMBr = NULL;
6353 struct dir_notify_req *dnotify_req;
6354 int bytes_returned;
6356 cifs_dbg(FYI, "In CIFSSMBNotify for file handle %d\n", (int)netfid);
6357 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
6358 (void **) &pSMBr);
6359 if (rc)
6360 return rc;
6362 pSMB->TotalParameterCount = 0 ;
6363 pSMB->TotalDataCount = 0;
6364 pSMB->MaxParameterCount = cpu_to_le32(2);
6365 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
6366 pSMB->MaxSetupCount = 4;
6367 pSMB->Reserved = 0;
6368 pSMB->ParameterOffset = 0;
6369 pSMB->DataCount = 0;
6370 pSMB->DataOffset = 0;
6371 pSMB->SetupCount = 4; /* single byte does not need le conversion */
6372 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_NOTIFY_CHANGE);
6373 pSMB->ParameterCount = pSMB->TotalParameterCount;
6374 if (notify_subdirs)
6375 pSMB->WatchTree = 1; /* one byte - no le conversion needed */
6376 pSMB->Reserved2 = 0;
6377 pSMB->CompletionFilter = cpu_to_le32(filter);
6378 pSMB->Fid = netfid; /* file handle always le */
6379 pSMB->ByteCount = 0;
6381 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6382 (struct smb_hdr *)pSMBr, &bytes_returned,
6383 CIFS_ASYNC_OP);
6384 if (rc) {
6385 cifs_dbg(FYI, "Error in Notify = %d\n", rc);
6386 } else {
6387 /* Add file to outstanding requests */
6388 /* BB change to kmem cache alloc */
6389 dnotify_req = kmalloc(
6390 sizeof(struct dir_notify_req),
6391 GFP_KERNEL);
6392 if (dnotify_req) {
6393 dnotify_req->Pid = pSMB->hdr.Pid;
6394 dnotify_req->PidHigh = pSMB->hdr.PidHigh;
6395 dnotify_req->Mid = pSMB->hdr.Mid;
6396 dnotify_req->Tid = pSMB->hdr.Tid;
6397 dnotify_req->Uid = pSMB->hdr.Uid;
6398 dnotify_req->netfid = netfid;
6399 dnotify_req->pfile = pfile;
6400 dnotify_req->filter = filter;
6401 dnotify_req->multishot = multishot;
6402 spin_lock(&GlobalMid_Lock);
6403 list_add_tail(&dnotify_req->lhead,
6404 &GlobalDnotifyReqList);
6405 spin_unlock(&GlobalMid_Lock);
6406 } else
6407 rc = -ENOMEM;
6409 cifs_buf_release(pSMB);
6410 return rc;
6412 #endif /* was needed for dnotify, and will be needed for inotify when VFS fix */