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