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