Merge tag 'nds32-for-linus-4.18' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-microblaze.git] / fs / cifs / smb1ops.c
1 /*
2  *  SMB1 (CIFS) version specific operations
3  *
4  *  Copyright (c) 2012, Jeff Layton <jlayton@redhat.com>
5  *
6  *  This library is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License v2 as published
8  *  by the Free Software Foundation.
9  *
10  *  This library is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13  *  the GNU Lesser General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Lesser General Public License
16  *  along with this library; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */
19
20 #include <linux/pagemap.h>
21 #include <linux/vfs.h>
22 #include "cifsglob.h"
23 #include "cifsproto.h"
24 #include "cifs_debug.h"
25 #include "cifspdu.h"
26 #include "cifs_unicode.h"
27
28 /*
29  * An NT cancel request header looks just like the original request except:
30  *
31  * The Command is SMB_COM_NT_CANCEL
32  * The WordCount is zeroed out
33  * The ByteCount is zeroed out
34  *
35  * This function mangles an existing request buffer into a
36  * SMB_COM_NT_CANCEL request and then sends it.
37  */
38 static int
39 send_nt_cancel(struct TCP_Server_Info *server, struct smb_rqst *rqst,
40                struct mid_q_entry *mid)
41 {
42         int rc = 0;
43         struct smb_hdr *in_buf = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
44
45         /* -4 for RFC1001 length and +2 for BCC field */
46         in_buf->smb_buf_length = cpu_to_be32(sizeof(struct smb_hdr) - 4  + 2);
47         in_buf->Command = SMB_COM_NT_CANCEL;
48         in_buf->WordCount = 0;
49         put_bcc(0, in_buf);
50
51         mutex_lock(&server->srv_mutex);
52         rc = cifs_sign_smb(in_buf, server, &mid->sequence_number);
53         if (rc) {
54                 mutex_unlock(&server->srv_mutex);
55                 return rc;
56         }
57
58         /*
59          * The response to this call was already factored into the sequence
60          * number when the call went out, so we must adjust it back downward
61          * after signing here.
62          */
63         --server->sequence_number;
64         rc = smb_send(server, in_buf, be32_to_cpu(in_buf->smb_buf_length));
65         if (rc < 0)
66                 server->sequence_number--;
67
68         mutex_unlock(&server->srv_mutex);
69
70         cifs_dbg(FYI, "issued NT_CANCEL for mid %u, rc = %d\n",
71                  get_mid(in_buf), rc);
72
73         return rc;
74 }
75
76 static bool
77 cifs_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
78 {
79         return ob1->fid.netfid == ob2->fid.netfid;
80 }
81
82 static unsigned int
83 cifs_read_data_offset(char *buf)
84 {
85         READ_RSP *rsp = (READ_RSP *)buf;
86         return le16_to_cpu(rsp->DataOffset);
87 }
88
89 static unsigned int
90 cifs_read_data_length(char *buf, bool in_remaining)
91 {
92         READ_RSP *rsp = (READ_RSP *)buf;
93         /* It's a bug reading remaining data for SMB1 packets */
94         WARN_ON(in_remaining);
95         return (le16_to_cpu(rsp->DataLengthHigh) << 16) +
96                le16_to_cpu(rsp->DataLength);
97 }
98
99 static struct mid_q_entry *
100 cifs_find_mid(struct TCP_Server_Info *server, char *buffer)
101 {
102         struct smb_hdr *buf = (struct smb_hdr *)buffer;
103         struct mid_q_entry *mid;
104
105         spin_lock(&GlobalMid_Lock);
106         list_for_each_entry(mid, &server->pending_mid_q, qhead) {
107                 if (compare_mid(mid->mid, buf) &&
108                     mid->mid_state == MID_REQUEST_SUBMITTED &&
109                     le16_to_cpu(mid->command) == buf->Command) {
110                         kref_get(&mid->refcount);
111                         spin_unlock(&GlobalMid_Lock);
112                         return mid;
113                 }
114         }
115         spin_unlock(&GlobalMid_Lock);
116         return NULL;
117 }
118
119 static void
120 cifs_add_credits(struct TCP_Server_Info *server, const unsigned int add,
121                  const int optype)
122 {
123         spin_lock(&server->req_lock);
124         server->credits += add;
125         server->in_flight--;
126         spin_unlock(&server->req_lock);
127         wake_up(&server->request_q);
128 }
129
130 static void
131 cifs_set_credits(struct TCP_Server_Info *server, const int val)
132 {
133         spin_lock(&server->req_lock);
134         server->credits = val;
135         server->oplocks = val > 1 ? enable_oplocks : false;
136         spin_unlock(&server->req_lock);
137 }
138
139 static int *
140 cifs_get_credits_field(struct TCP_Server_Info *server, const int optype)
141 {
142         return &server->credits;
143 }
144
145 static unsigned int
146 cifs_get_credits(struct mid_q_entry *mid)
147 {
148         return 1;
149 }
150
151 /*
152  * Find a free multiplex id (SMB mid). Otherwise there could be
153  * mid collisions which might cause problems, demultiplexing the
154  * wrong response to this request. Multiplex ids could collide if
155  * one of a series requests takes much longer than the others, or
156  * if a very large number of long lived requests (byte range
157  * locks or FindNotify requests) are pending. No more than
158  * 64K-1 requests can be outstanding at one time. If no
159  * mids are available, return zero. A future optimization
160  * could make the combination of mids and uid the key we use
161  * to demultiplex on (rather than mid alone).
162  * In addition to the above check, the cifs demultiplex
163  * code already used the command code as a secondary
164  * check of the frame and if signing is negotiated the
165  * response would be discarded if the mid were the same
166  * but the signature was wrong. Since the mid is not put in the
167  * pending queue until later (when it is about to be dispatched)
168  * we do have to limit the number of outstanding requests
169  * to somewhat less than 64K-1 although it is hard to imagine
170  * so many threads being in the vfs at one time.
171  */
172 static __u64
173 cifs_get_next_mid(struct TCP_Server_Info *server)
174 {
175         __u64 mid = 0;
176         __u16 last_mid, cur_mid;
177         bool collision;
178
179         spin_lock(&GlobalMid_Lock);
180
181         /* mid is 16 bit only for CIFS/SMB */
182         cur_mid = (__u16)((server->CurrentMid) & 0xffff);
183         /* we do not want to loop forever */
184         last_mid = cur_mid;
185         cur_mid++;
186
187         /*
188          * This nested loop looks more expensive than it is.
189          * In practice the list of pending requests is short,
190          * fewer than 50, and the mids are likely to be unique
191          * on the first pass through the loop unless some request
192          * takes longer than the 64 thousand requests before it
193          * (and it would also have to have been a request that
194          * did not time out).
195          */
196         while (cur_mid != last_mid) {
197                 struct mid_q_entry *mid_entry;
198                 unsigned int num_mids;
199
200                 collision = false;
201                 if (cur_mid == 0)
202                         cur_mid++;
203
204                 num_mids = 0;
205                 list_for_each_entry(mid_entry, &server->pending_mid_q, qhead) {
206                         ++num_mids;
207                         if (mid_entry->mid == cur_mid &&
208                             mid_entry->mid_state == MID_REQUEST_SUBMITTED) {
209                                 /* This mid is in use, try a different one */
210                                 collision = true;
211                                 break;
212                         }
213                 }
214
215                 /*
216                  * if we have more than 32k mids in the list, then something
217                  * is very wrong. Possibly a local user is trying to DoS the
218                  * box by issuing long-running calls and SIGKILL'ing them. If
219                  * we get to 2^16 mids then we're in big trouble as this
220                  * function could loop forever.
221                  *
222                  * Go ahead and assign out the mid in this situation, but force
223                  * an eventual reconnect to clean out the pending_mid_q.
224                  */
225                 if (num_mids > 32768)
226                         server->tcpStatus = CifsNeedReconnect;
227
228                 if (!collision) {
229                         mid = (__u64)cur_mid;
230                         server->CurrentMid = mid;
231                         break;
232                 }
233                 cur_mid++;
234         }
235         spin_unlock(&GlobalMid_Lock);
236         return mid;
237 }
238
239 /*
240         return codes:
241                 0       not a transact2, or all data present
242                 >0      transact2 with that much data missing
243                 -EINVAL invalid transact2
244  */
245 static int
246 check2ndT2(char *buf)
247 {
248         struct smb_hdr *pSMB = (struct smb_hdr *)buf;
249         struct smb_t2_rsp *pSMBt;
250         int remaining;
251         __u16 total_data_size, data_in_this_rsp;
252
253         if (pSMB->Command != SMB_COM_TRANSACTION2)
254                 return 0;
255
256         /* check for plausible wct, bcc and t2 data and parm sizes */
257         /* check for parm and data offset going beyond end of smb */
258         if (pSMB->WordCount != 10) { /* coalesce_t2 depends on this */
259                 cifs_dbg(FYI, "invalid transact2 word count\n");
260                 return -EINVAL;
261         }
262
263         pSMBt = (struct smb_t2_rsp *)pSMB;
264
265         total_data_size = get_unaligned_le16(&pSMBt->t2_rsp.TotalDataCount);
266         data_in_this_rsp = get_unaligned_le16(&pSMBt->t2_rsp.DataCount);
267
268         if (total_data_size == data_in_this_rsp)
269                 return 0;
270         else if (total_data_size < data_in_this_rsp) {
271                 cifs_dbg(FYI, "total data %d smaller than data in frame %d\n",
272                          total_data_size, data_in_this_rsp);
273                 return -EINVAL;
274         }
275
276         remaining = total_data_size - data_in_this_rsp;
277
278         cifs_dbg(FYI, "missing %d bytes from transact2, check next response\n",
279                  remaining);
280         if (total_data_size > CIFSMaxBufSize) {
281                 cifs_dbg(VFS, "TotalDataSize %d is over maximum buffer %d\n",
282                          total_data_size, CIFSMaxBufSize);
283                 return -EINVAL;
284         }
285         return remaining;
286 }
287
288 static int
289 coalesce_t2(char *second_buf, struct smb_hdr *target_hdr)
290 {
291         struct smb_t2_rsp *pSMBs = (struct smb_t2_rsp *)second_buf;
292         struct smb_t2_rsp *pSMBt  = (struct smb_t2_rsp *)target_hdr;
293         char *data_area_of_tgt;
294         char *data_area_of_src;
295         int remaining;
296         unsigned int byte_count, total_in_tgt;
297         __u16 tgt_total_cnt, src_total_cnt, total_in_src;
298
299         src_total_cnt = get_unaligned_le16(&pSMBs->t2_rsp.TotalDataCount);
300         tgt_total_cnt = get_unaligned_le16(&pSMBt->t2_rsp.TotalDataCount);
301
302         if (tgt_total_cnt != src_total_cnt)
303                 cifs_dbg(FYI, "total data count of primary and secondary t2 differ source=%hu target=%hu\n",
304                          src_total_cnt, tgt_total_cnt);
305
306         total_in_tgt = get_unaligned_le16(&pSMBt->t2_rsp.DataCount);
307
308         remaining = tgt_total_cnt - total_in_tgt;
309
310         if (remaining < 0) {
311                 cifs_dbg(FYI, "Server sent too much data. tgt_total_cnt=%hu total_in_tgt=%hu\n",
312                          tgt_total_cnt, total_in_tgt);
313                 return -EPROTO;
314         }
315
316         if (remaining == 0) {
317                 /* nothing to do, ignore */
318                 cifs_dbg(FYI, "no more data remains\n");
319                 return 0;
320         }
321
322         total_in_src = get_unaligned_le16(&pSMBs->t2_rsp.DataCount);
323         if (remaining < total_in_src)
324                 cifs_dbg(FYI, "transact2 2nd response contains too much data\n");
325
326         /* find end of first SMB data area */
327         data_area_of_tgt = (char *)&pSMBt->hdr.Protocol +
328                                 get_unaligned_le16(&pSMBt->t2_rsp.DataOffset);
329
330         /* validate target area */
331         data_area_of_src = (char *)&pSMBs->hdr.Protocol +
332                                 get_unaligned_le16(&pSMBs->t2_rsp.DataOffset);
333
334         data_area_of_tgt += total_in_tgt;
335
336         total_in_tgt += total_in_src;
337         /* is the result too big for the field? */
338         if (total_in_tgt > USHRT_MAX) {
339                 cifs_dbg(FYI, "coalesced DataCount too large (%u)\n",
340                          total_in_tgt);
341                 return -EPROTO;
342         }
343         put_unaligned_le16(total_in_tgt, &pSMBt->t2_rsp.DataCount);
344
345         /* fix up the BCC */
346         byte_count = get_bcc(target_hdr);
347         byte_count += total_in_src;
348         /* is the result too big for the field? */
349         if (byte_count > USHRT_MAX) {
350                 cifs_dbg(FYI, "coalesced BCC too large (%u)\n", byte_count);
351                 return -EPROTO;
352         }
353         put_bcc(byte_count, target_hdr);
354
355         byte_count = be32_to_cpu(target_hdr->smb_buf_length);
356         byte_count += total_in_src;
357         /* don't allow buffer to overflow */
358         if (byte_count > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
359                 cifs_dbg(FYI, "coalesced BCC exceeds buffer size (%u)\n",
360                          byte_count);
361                 return -ENOBUFS;
362         }
363         target_hdr->smb_buf_length = cpu_to_be32(byte_count);
364
365         /* copy second buffer into end of first buffer */
366         memcpy(data_area_of_tgt, data_area_of_src, total_in_src);
367
368         if (remaining != total_in_src) {
369                 /* more responses to go */
370                 cifs_dbg(FYI, "waiting for more secondary responses\n");
371                 return 1;
372         }
373
374         /* we are done */
375         cifs_dbg(FYI, "found the last secondary response\n");
376         return 0;
377 }
378
379 static void
380 cifs_downgrade_oplock(struct TCP_Server_Info *server,
381                         struct cifsInodeInfo *cinode, bool set_level2)
382 {
383         if (set_level2)
384                 cifs_set_oplock_level(cinode, OPLOCK_READ);
385         else
386                 cifs_set_oplock_level(cinode, 0);
387 }
388
389 static bool
390 cifs_check_trans2(struct mid_q_entry *mid, struct TCP_Server_Info *server,
391                   char *buf, int malformed)
392 {
393         if (malformed)
394                 return false;
395         if (check2ndT2(buf) <= 0)
396                 return false;
397         mid->multiRsp = true;
398         if (mid->resp_buf) {
399                 /* merge response - fix up 1st*/
400                 malformed = coalesce_t2(buf, mid->resp_buf);
401                 if (malformed > 0)
402                         return true;
403                 /* All parts received or packet is malformed. */
404                 mid->multiEnd = true;
405                 dequeue_mid(mid, malformed);
406                 return true;
407         }
408         if (!server->large_buf) {
409                 /*FIXME: switch to already allocated largebuf?*/
410                 cifs_dbg(VFS, "1st trans2 resp needs bigbuf\n");
411         } else {
412                 /* Have first buffer */
413                 mid->resp_buf = buf;
414                 mid->large_buf = true;
415                 server->bigbuf = NULL;
416         }
417         return true;
418 }
419
420 static bool
421 cifs_need_neg(struct TCP_Server_Info *server)
422 {
423         return server->maxBuf == 0;
424 }
425
426 static int
427 cifs_negotiate(const unsigned int xid, struct cifs_ses *ses)
428 {
429         int rc;
430         rc = CIFSSMBNegotiate(xid, ses);
431         if (rc == -EAGAIN) {
432                 /* retry only once on 1st time connection */
433                 set_credits(ses->server, 1);
434                 rc = CIFSSMBNegotiate(xid, ses);
435                 if (rc == -EAGAIN)
436                         rc = -EHOSTDOWN;
437         }
438         return rc;
439 }
440
441 static unsigned int
442 cifs_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
443 {
444         __u64 unix_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
445         struct TCP_Server_Info *server = tcon->ses->server;
446         unsigned int wsize;
447
448         /* start with specified wsize, or default */
449         if (volume_info->wsize)
450                 wsize = volume_info->wsize;
451         else if (tcon->unix_ext && (unix_cap & CIFS_UNIX_LARGE_WRITE_CAP))
452                 wsize = CIFS_DEFAULT_IOSIZE;
453         else
454                 wsize = CIFS_DEFAULT_NON_POSIX_WSIZE;
455
456         /* can server support 24-bit write sizes? (via UNIX extensions) */
457         if (!tcon->unix_ext || !(unix_cap & CIFS_UNIX_LARGE_WRITE_CAP))
458                 wsize = min_t(unsigned int, wsize, CIFS_MAX_RFC1002_WSIZE);
459
460         /*
461          * no CAP_LARGE_WRITE_X or is signing enabled without CAP_UNIX set?
462          * Limit it to max buffer offered by the server, minus the size of the
463          * WRITEX header, not including the 4 byte RFC1001 length.
464          */
465         if (!(server->capabilities & CAP_LARGE_WRITE_X) ||
466             (!(server->capabilities & CAP_UNIX) && server->sign))
467                 wsize = min_t(unsigned int, wsize,
468                                 server->maxBuf - sizeof(WRITE_REQ) + 4);
469
470         /* hard limit of CIFS_MAX_WSIZE */
471         wsize = min_t(unsigned int, wsize, CIFS_MAX_WSIZE);
472
473         return wsize;
474 }
475
476 static unsigned int
477 cifs_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
478 {
479         __u64 unix_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
480         struct TCP_Server_Info *server = tcon->ses->server;
481         unsigned int rsize, defsize;
482
483         /*
484          * Set default value...
485          *
486          * HACK alert! Ancient servers have very small buffers. Even though
487          * MS-CIFS indicates that servers are only limited by the client's
488          * bufsize for reads, testing against win98se shows that it throws
489          * INVALID_PARAMETER errors if you try to request too large a read.
490          * OS/2 just sends back short reads.
491          *
492          * If the server doesn't advertise CAP_LARGE_READ_X, then assume that
493          * it can't handle a read request larger than its MaxBufferSize either.
494          */
495         if (tcon->unix_ext && (unix_cap & CIFS_UNIX_LARGE_READ_CAP))
496                 defsize = CIFS_DEFAULT_IOSIZE;
497         else if (server->capabilities & CAP_LARGE_READ_X)
498                 defsize = CIFS_DEFAULT_NON_POSIX_RSIZE;
499         else
500                 defsize = server->maxBuf - sizeof(READ_RSP);
501
502         rsize = volume_info->rsize ? volume_info->rsize : defsize;
503
504         /*
505          * no CAP_LARGE_READ_X? Then MS-CIFS states that we must limit this to
506          * the client's MaxBufferSize.
507          */
508         if (!(server->capabilities & CAP_LARGE_READ_X))
509                 rsize = min_t(unsigned int, CIFSMaxBufSize, rsize);
510
511         /* hard limit of CIFS_MAX_RSIZE */
512         rsize = min_t(unsigned int, rsize, CIFS_MAX_RSIZE);
513
514         return rsize;
515 }
516
517 static void
518 cifs_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
519 {
520         CIFSSMBQFSDeviceInfo(xid, tcon);
521         CIFSSMBQFSAttributeInfo(xid, tcon);
522 }
523
524 static int
525 cifs_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
526                         struct cifs_sb_info *cifs_sb, const char *full_path)
527 {
528         int rc;
529         FILE_ALL_INFO *file_info;
530
531         file_info = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
532         if (file_info == NULL)
533                 return -ENOMEM;
534
535         rc = CIFSSMBQPathInfo(xid, tcon, full_path, file_info,
536                               0 /* not legacy */, cifs_sb->local_nls,
537                               cifs_remap(cifs_sb));
538
539         if (rc == -EOPNOTSUPP || rc == -EINVAL)
540                 rc = SMBQueryInformation(xid, tcon, full_path, file_info,
541                                 cifs_sb->local_nls, cifs_remap(cifs_sb));
542         kfree(file_info);
543         return rc;
544 }
545
546 static int
547 cifs_query_path_info(const unsigned int xid, struct cifs_tcon *tcon,
548                      struct cifs_sb_info *cifs_sb, const char *full_path,
549                      FILE_ALL_INFO *data, bool *adjustTZ, bool *symlink)
550 {
551         int rc;
552
553         *symlink = false;
554
555         /* could do find first instead but this returns more info */
556         rc = CIFSSMBQPathInfo(xid, tcon, full_path, data, 0 /* not legacy */,
557                               cifs_sb->local_nls, cifs_remap(cifs_sb));
558         /*
559          * BB optimize code so we do not make the above call when server claims
560          * no NT SMB support and the above call failed at least once - set flag
561          * in tcon or mount.
562          */
563         if ((rc == -EOPNOTSUPP) || (rc == -EINVAL)) {
564                 rc = SMBQueryInformation(xid, tcon, full_path, data,
565                                          cifs_sb->local_nls,
566                                          cifs_remap(cifs_sb));
567                 *adjustTZ = true;
568         }
569
570         if (!rc && (le32_to_cpu(data->Attributes) & ATTR_REPARSE)) {
571                 int tmprc;
572                 int oplock = 0;
573                 struct cifs_fid fid;
574                 struct cifs_open_parms oparms;
575
576                 oparms.tcon = tcon;
577                 oparms.cifs_sb = cifs_sb;
578                 oparms.desired_access = FILE_READ_ATTRIBUTES;
579                 oparms.create_options = 0;
580                 oparms.disposition = FILE_OPEN;
581                 oparms.path = full_path;
582                 oparms.fid = &fid;
583                 oparms.reconnect = false;
584
585                 /* Need to check if this is a symbolic link or not */
586                 tmprc = CIFS_open(xid, &oparms, &oplock, NULL);
587                 if (tmprc == -EOPNOTSUPP)
588                         *symlink = true;
589                 else if (tmprc == 0)
590                         CIFSSMBClose(xid, tcon, fid.netfid);
591         }
592
593         return rc;
594 }
595
596 static int
597 cifs_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
598                   struct cifs_sb_info *cifs_sb, const char *full_path,
599                   u64 *uniqueid, FILE_ALL_INFO *data)
600 {
601         /*
602          * We can not use the IndexNumber field by default from Windows or
603          * Samba (in ALL_INFO buf) but we can request it explicitly. The SNIA
604          * CIFS spec claims that this value is unique within the scope of a
605          * share, and the windows docs hint that it's actually unique
606          * per-machine.
607          *
608          * There may be higher info levels that work but are there Windows
609          * server or network appliances for which IndexNumber field is not
610          * guaranteed unique?
611          */
612         return CIFSGetSrvInodeNumber(xid, tcon, full_path, uniqueid,
613                                      cifs_sb->local_nls,
614                                      cifs_remap(cifs_sb));
615 }
616
617 static int
618 cifs_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
619                      struct cifs_fid *fid, FILE_ALL_INFO *data)
620 {
621         return CIFSSMBQFileInfo(xid, tcon, fid->netfid, data);
622 }
623
624 static void
625 cifs_clear_stats(struct cifs_tcon *tcon)
626 {
627 #ifdef CONFIG_CIFS_STATS
628         atomic_set(&tcon->stats.cifs_stats.num_writes, 0);
629         atomic_set(&tcon->stats.cifs_stats.num_reads, 0);
630         atomic_set(&tcon->stats.cifs_stats.num_flushes, 0);
631         atomic_set(&tcon->stats.cifs_stats.num_oplock_brks, 0);
632         atomic_set(&tcon->stats.cifs_stats.num_opens, 0);
633         atomic_set(&tcon->stats.cifs_stats.num_posixopens, 0);
634         atomic_set(&tcon->stats.cifs_stats.num_posixmkdirs, 0);
635         atomic_set(&tcon->stats.cifs_stats.num_closes, 0);
636         atomic_set(&tcon->stats.cifs_stats.num_deletes, 0);
637         atomic_set(&tcon->stats.cifs_stats.num_mkdirs, 0);
638         atomic_set(&tcon->stats.cifs_stats.num_rmdirs, 0);
639         atomic_set(&tcon->stats.cifs_stats.num_renames, 0);
640         atomic_set(&tcon->stats.cifs_stats.num_t2renames, 0);
641         atomic_set(&tcon->stats.cifs_stats.num_ffirst, 0);
642         atomic_set(&tcon->stats.cifs_stats.num_fnext, 0);
643         atomic_set(&tcon->stats.cifs_stats.num_fclose, 0);
644         atomic_set(&tcon->stats.cifs_stats.num_hardlinks, 0);
645         atomic_set(&tcon->stats.cifs_stats.num_symlinks, 0);
646         atomic_set(&tcon->stats.cifs_stats.num_locks, 0);
647         atomic_set(&tcon->stats.cifs_stats.num_acl_get, 0);
648         atomic_set(&tcon->stats.cifs_stats.num_acl_set, 0);
649 #endif
650 }
651
652 static void
653 cifs_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
654 {
655 #ifdef CONFIG_CIFS_STATS
656         seq_printf(m, " Oplocks breaks: %d",
657                    atomic_read(&tcon->stats.cifs_stats.num_oplock_brks));
658         seq_printf(m, "\nReads:  %d Bytes: %llu",
659                    atomic_read(&tcon->stats.cifs_stats.num_reads),
660                    (long long)(tcon->bytes_read));
661         seq_printf(m, "\nWrites: %d Bytes: %llu",
662                    atomic_read(&tcon->stats.cifs_stats.num_writes),
663                    (long long)(tcon->bytes_written));
664         seq_printf(m, "\nFlushes: %d",
665                    atomic_read(&tcon->stats.cifs_stats.num_flushes));
666         seq_printf(m, "\nLocks: %d HardLinks: %d Symlinks: %d",
667                    atomic_read(&tcon->stats.cifs_stats.num_locks),
668                    atomic_read(&tcon->stats.cifs_stats.num_hardlinks),
669                    atomic_read(&tcon->stats.cifs_stats.num_symlinks));
670         seq_printf(m, "\nOpens: %d Closes: %d Deletes: %d",
671                    atomic_read(&tcon->stats.cifs_stats.num_opens),
672                    atomic_read(&tcon->stats.cifs_stats.num_closes),
673                    atomic_read(&tcon->stats.cifs_stats.num_deletes));
674         seq_printf(m, "\nPosix Opens: %d Posix Mkdirs: %d",
675                    atomic_read(&tcon->stats.cifs_stats.num_posixopens),
676                    atomic_read(&tcon->stats.cifs_stats.num_posixmkdirs));
677         seq_printf(m, "\nMkdirs: %d Rmdirs: %d",
678                    atomic_read(&tcon->stats.cifs_stats.num_mkdirs),
679                    atomic_read(&tcon->stats.cifs_stats.num_rmdirs));
680         seq_printf(m, "\nRenames: %d T2 Renames %d",
681                    atomic_read(&tcon->stats.cifs_stats.num_renames),
682                    atomic_read(&tcon->stats.cifs_stats.num_t2renames));
683         seq_printf(m, "\nFindFirst: %d FNext %d FClose %d",
684                    atomic_read(&tcon->stats.cifs_stats.num_ffirst),
685                    atomic_read(&tcon->stats.cifs_stats.num_fnext),
686                    atomic_read(&tcon->stats.cifs_stats.num_fclose));
687 #endif
688 }
689
690 static void
691 cifs_mkdir_setinfo(struct inode *inode, const char *full_path,
692                    struct cifs_sb_info *cifs_sb, struct cifs_tcon *tcon,
693                    const unsigned int xid)
694 {
695         FILE_BASIC_INFO info;
696         struct cifsInodeInfo *cifsInode;
697         u32 dosattrs;
698         int rc;
699
700         memset(&info, 0, sizeof(info));
701         cifsInode = CIFS_I(inode);
702         dosattrs = cifsInode->cifsAttrs|ATTR_READONLY;
703         info.Attributes = cpu_to_le32(dosattrs);
704         rc = CIFSSMBSetPathInfo(xid, tcon, full_path, &info, cifs_sb->local_nls,
705                                 cifs_remap(cifs_sb));
706         if (rc == 0)
707                 cifsInode->cifsAttrs = dosattrs;
708 }
709
710 static int
711 cifs_open_file(const unsigned int xid, struct cifs_open_parms *oparms,
712                __u32 *oplock, FILE_ALL_INFO *buf)
713 {
714         if (!(oparms->tcon->ses->capabilities & CAP_NT_SMBS))
715                 return SMBLegacyOpen(xid, oparms->tcon, oparms->path,
716                                      oparms->disposition,
717                                      oparms->desired_access,
718                                      oparms->create_options,
719                                      &oparms->fid->netfid, oplock, buf,
720                                      oparms->cifs_sb->local_nls,
721                                      cifs_remap(oparms->cifs_sb));
722         return CIFS_open(xid, oparms, oplock, buf);
723 }
724
725 static void
726 cifs_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
727 {
728         struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
729         cfile->fid.netfid = fid->netfid;
730         cifs_set_oplock_level(cinode, oplock);
731         cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
732 }
733
734 static void
735 cifs_close_file(const unsigned int xid, struct cifs_tcon *tcon,
736                 struct cifs_fid *fid)
737 {
738         CIFSSMBClose(xid, tcon, fid->netfid);
739 }
740
741 static int
742 cifs_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
743                 struct cifs_fid *fid)
744 {
745         return CIFSSMBFlush(xid, tcon, fid->netfid);
746 }
747
748 static int
749 cifs_sync_read(const unsigned int xid, struct cifs_fid *pfid,
750                struct cifs_io_parms *parms, unsigned int *bytes_read,
751                char **buf, int *buf_type)
752 {
753         parms->netfid = pfid->netfid;
754         return CIFSSMBRead(xid, parms, bytes_read, buf, buf_type);
755 }
756
757 static int
758 cifs_sync_write(const unsigned int xid, struct cifs_fid *pfid,
759                 struct cifs_io_parms *parms, unsigned int *written,
760                 struct kvec *iov, unsigned long nr_segs)
761 {
762
763         parms->netfid = pfid->netfid;
764         return CIFSSMBWrite2(xid, parms, written, iov, nr_segs);
765 }
766
767 static int
768 smb_set_file_info(struct inode *inode, const char *full_path,
769                   FILE_BASIC_INFO *buf, const unsigned int xid)
770 {
771         int oplock = 0;
772         int rc;
773         __u32 netpid;
774         struct cifs_fid fid;
775         struct cifs_open_parms oparms;
776         struct cifsFileInfo *open_file;
777         struct cifsInodeInfo *cinode = CIFS_I(inode);
778         struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
779         struct tcon_link *tlink = NULL;
780         struct cifs_tcon *tcon;
781
782         /* if the file is already open for write, just use that fileid */
783         open_file = find_writable_file(cinode, true);
784         if (open_file) {
785                 fid.netfid = open_file->fid.netfid;
786                 netpid = open_file->pid;
787                 tcon = tlink_tcon(open_file->tlink);
788                 goto set_via_filehandle;
789         }
790
791         tlink = cifs_sb_tlink(cifs_sb);
792         if (IS_ERR(tlink)) {
793                 rc = PTR_ERR(tlink);
794                 tlink = NULL;
795                 goto out;
796         }
797         tcon = tlink_tcon(tlink);
798
799         rc = CIFSSMBSetPathInfo(xid, tcon, full_path, buf, cifs_sb->local_nls,
800                                 cifs_remap(cifs_sb));
801         if (rc == 0) {
802                 cinode->cifsAttrs = le32_to_cpu(buf->Attributes);
803                 goto out;
804         } else if (rc != -EOPNOTSUPP && rc != -EINVAL) {
805                 goto out;
806         }
807
808         oparms.tcon = tcon;
809         oparms.cifs_sb = cifs_sb;
810         oparms.desired_access = SYNCHRONIZE | FILE_WRITE_ATTRIBUTES;
811         oparms.create_options = CREATE_NOT_DIR;
812         oparms.disposition = FILE_OPEN;
813         oparms.path = full_path;
814         oparms.fid = &fid;
815         oparms.reconnect = false;
816
817         cifs_dbg(FYI, "calling SetFileInfo since SetPathInfo for times not supported by this server\n");
818         rc = CIFS_open(xid, &oparms, &oplock, NULL);
819         if (rc != 0) {
820                 if (rc == -EIO)
821                         rc = -EINVAL;
822                 goto out;
823         }
824
825         netpid = current->tgid;
826
827 set_via_filehandle:
828         rc = CIFSSMBSetFileInfo(xid, tcon, buf, fid.netfid, netpid);
829         if (!rc)
830                 cinode->cifsAttrs = le32_to_cpu(buf->Attributes);
831
832         if (open_file == NULL)
833                 CIFSSMBClose(xid, tcon, fid.netfid);
834         else
835                 cifsFileInfo_put(open_file);
836 out:
837         if (tlink != NULL)
838                 cifs_put_tlink(tlink);
839         return rc;
840 }
841
842 static int
843 cifs_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
844                    struct cifsFileInfo *cfile)
845 {
846         return CIFSSMB_set_compression(xid, tcon, cfile->fid.netfid);
847 }
848
849 static int
850 cifs_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
851                      const char *path, struct cifs_sb_info *cifs_sb,
852                      struct cifs_fid *fid, __u16 search_flags,
853                      struct cifs_search_info *srch_inf)
854 {
855         int rc;
856
857         rc = CIFSFindFirst(xid, tcon, path, cifs_sb,
858                            &fid->netfid, search_flags, srch_inf, true);
859         if (rc)
860                 cifs_dbg(FYI, "find first failed=%d\n", rc);
861         return rc;
862 }
863
864 static int
865 cifs_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
866                     struct cifs_fid *fid, __u16 search_flags,
867                     struct cifs_search_info *srch_inf)
868 {
869         return CIFSFindNext(xid, tcon, fid->netfid, search_flags, srch_inf);
870 }
871
872 static int
873 cifs_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
874                struct cifs_fid *fid)
875 {
876         return CIFSFindClose(xid, tcon, fid->netfid);
877 }
878
879 static int
880 cifs_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
881                      struct cifsInodeInfo *cinode)
882 {
883         return CIFSSMBLock(0, tcon, fid->netfid, current->tgid, 0, 0, 0, 0,
884                            LOCKING_ANDX_OPLOCK_RELEASE, false,
885                            CIFS_CACHE_READ(cinode) ? 1 : 0);
886 }
887
888 static int
889 cifs_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
890              struct kstatfs *buf)
891 {
892         int rc = -EOPNOTSUPP;
893
894         buf->f_type = CIFS_MAGIC_NUMBER;
895
896         /*
897          * We could add a second check for a QFS Unix capability bit
898          */
899         if ((tcon->ses->capabilities & CAP_UNIX) &&
900             (CIFS_POSIX_EXTENSIONS & le64_to_cpu(tcon->fsUnixInfo.Capability)))
901                 rc = CIFSSMBQFSPosixInfo(xid, tcon, buf);
902
903         /*
904          * Only need to call the old QFSInfo if failed on newer one,
905          * e.g. by OS/2.
906          **/
907         if (rc && (tcon->ses->capabilities & CAP_NT_SMBS))
908                 rc = CIFSSMBQFSInfo(xid, tcon, buf);
909
910         /*
911          * Some old Windows servers also do not support level 103, retry with
912          * older level one if old server failed the previous call or we
913          * bypassed it because we detected that this was an older LANMAN sess
914          */
915         if (rc)
916                 rc = SMBOldQFSInfo(xid, tcon, buf);
917         return rc;
918 }
919
920 static int
921 cifs_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
922                __u64 length, __u32 type, int lock, int unlock, bool wait)
923 {
924         return CIFSSMBLock(xid, tlink_tcon(cfile->tlink), cfile->fid.netfid,
925                            current->tgid, length, offset, unlock, lock,
926                            (__u8)type, wait, 0);
927 }
928
929 static int
930 cifs_unix_dfs_readlink(const unsigned int xid, struct cifs_tcon *tcon,
931                        const unsigned char *searchName, char **symlinkinfo,
932                        const struct nls_table *nls_codepage)
933 {
934 #ifdef CONFIG_CIFS_DFS_UPCALL
935         int rc;
936         unsigned int num_referrals = 0;
937         struct dfs_info3_param *referrals = NULL;
938
939         rc = get_dfs_path(xid, tcon->ses, searchName, nls_codepage,
940                           &num_referrals, &referrals, 0);
941
942         if (!rc && num_referrals > 0) {
943                 *symlinkinfo = kstrndup(referrals->node_name,
944                                         strlen(referrals->node_name),
945                                         GFP_KERNEL);
946                 if (!*symlinkinfo)
947                         rc = -ENOMEM;
948                 free_dfs_info_array(referrals, num_referrals);
949         }
950         return rc;
951 #else /* No DFS support */
952         return -EREMOTE;
953 #endif
954 }
955
956 static int
957 cifs_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
958                    const char *full_path, char **target_path,
959                    struct cifs_sb_info *cifs_sb)
960 {
961         int rc;
962         int oplock = 0;
963         struct cifs_fid fid;
964         struct cifs_open_parms oparms;
965
966         cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
967
968         /* Check for unix extensions */
969         if (cap_unix(tcon->ses)) {
970                 rc = CIFSSMBUnixQuerySymLink(xid, tcon, full_path, target_path,
971                                              cifs_sb->local_nls,
972                                              cifs_remap(cifs_sb));
973                 if (rc == -EREMOTE)
974                         rc = cifs_unix_dfs_readlink(xid, tcon, full_path,
975                                                     target_path,
976                                                     cifs_sb->local_nls);
977
978                 goto out;
979         }
980
981         oparms.tcon = tcon;
982         oparms.cifs_sb = cifs_sb;
983         oparms.desired_access = FILE_READ_ATTRIBUTES;
984         oparms.create_options = OPEN_REPARSE_POINT;
985         oparms.disposition = FILE_OPEN;
986         oparms.path = full_path;
987         oparms.fid = &fid;
988         oparms.reconnect = false;
989
990         rc = CIFS_open(xid, &oparms, &oplock, NULL);
991         if (rc)
992                 goto out;
993
994         rc = CIFSSMBQuerySymLink(xid, tcon, fid.netfid, target_path,
995                                  cifs_sb->local_nls);
996         if (rc)
997                 goto out_close;
998
999         convert_delimiter(*target_path, '/');
1000 out_close:
1001         CIFSSMBClose(xid, tcon, fid.netfid);
1002 out:
1003         if (!rc)
1004                 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
1005         return rc;
1006 }
1007
1008 static bool
1009 cifs_is_read_op(__u32 oplock)
1010 {
1011         return oplock == OPLOCK_READ;
1012 }
1013
1014 static unsigned int
1015 cifs_wp_retry_size(struct inode *inode)
1016 {
1017         return CIFS_SB(inode->i_sb)->wsize;
1018 }
1019
1020 static bool
1021 cifs_dir_needs_close(struct cifsFileInfo *cfile)
1022 {
1023         return !cfile->srch_inf.endOfSearch && !cfile->invalidHandle;
1024 }
1025
1026 static bool
1027 cifs_can_echo(struct TCP_Server_Info *server)
1028 {
1029         if (server->tcpStatus == CifsGood)
1030                 return true;
1031
1032         return false;
1033 }
1034
1035 struct smb_version_operations smb1_operations = {
1036         .send_cancel = send_nt_cancel,
1037         .compare_fids = cifs_compare_fids,
1038         .setup_request = cifs_setup_request,
1039         .setup_async_request = cifs_setup_async_request,
1040         .check_receive = cifs_check_receive,
1041         .add_credits = cifs_add_credits,
1042         .set_credits = cifs_set_credits,
1043         .get_credits_field = cifs_get_credits_field,
1044         .get_credits = cifs_get_credits,
1045         .wait_mtu_credits = cifs_wait_mtu_credits,
1046         .get_next_mid = cifs_get_next_mid,
1047         .read_data_offset = cifs_read_data_offset,
1048         .read_data_length = cifs_read_data_length,
1049         .map_error = map_smb_to_linux_error,
1050         .find_mid = cifs_find_mid,
1051         .check_message = checkSMB,
1052         .dump_detail = cifs_dump_detail,
1053         .clear_stats = cifs_clear_stats,
1054         .print_stats = cifs_print_stats,
1055         .is_oplock_break = is_valid_oplock_break,
1056         .downgrade_oplock = cifs_downgrade_oplock,
1057         .check_trans2 = cifs_check_trans2,
1058         .need_neg = cifs_need_neg,
1059         .negotiate = cifs_negotiate,
1060         .negotiate_wsize = cifs_negotiate_wsize,
1061         .negotiate_rsize = cifs_negotiate_rsize,
1062         .sess_setup = CIFS_SessSetup,
1063         .logoff = CIFSSMBLogoff,
1064         .tree_connect = CIFSTCon,
1065         .tree_disconnect = CIFSSMBTDis,
1066         .get_dfs_refer = CIFSGetDFSRefer,
1067         .qfs_tcon = cifs_qfs_tcon,
1068         .is_path_accessible = cifs_is_path_accessible,
1069         .can_echo = cifs_can_echo,
1070         .query_path_info = cifs_query_path_info,
1071         .query_file_info = cifs_query_file_info,
1072         .get_srv_inum = cifs_get_srv_inum,
1073         .set_path_size = CIFSSMBSetEOF,
1074         .set_file_size = CIFSSMBSetFileSize,
1075         .set_file_info = smb_set_file_info,
1076         .set_compression = cifs_set_compression,
1077         .echo = CIFSSMBEcho,
1078         .mkdir = CIFSSMBMkDir,
1079         .mkdir_setinfo = cifs_mkdir_setinfo,
1080         .rmdir = CIFSSMBRmDir,
1081         .unlink = CIFSSMBDelFile,
1082         .rename_pending_delete = cifs_rename_pending_delete,
1083         .rename = CIFSSMBRename,
1084         .create_hardlink = CIFSCreateHardLink,
1085         .query_symlink = cifs_query_symlink,
1086         .open = cifs_open_file,
1087         .set_fid = cifs_set_fid,
1088         .close = cifs_close_file,
1089         .flush = cifs_flush_file,
1090         .async_readv = cifs_async_readv,
1091         .async_writev = cifs_async_writev,
1092         .sync_read = cifs_sync_read,
1093         .sync_write = cifs_sync_write,
1094         .query_dir_first = cifs_query_dir_first,
1095         .query_dir_next = cifs_query_dir_next,
1096         .close_dir = cifs_close_dir,
1097         .calc_smb_size = smbCalcSize,
1098         .oplock_response = cifs_oplock_response,
1099         .queryfs = cifs_queryfs,
1100         .mand_lock = cifs_mand_lock,
1101         .mand_unlock_range = cifs_unlock_range,
1102         .push_mand_locks = cifs_push_mandatory_locks,
1103         .query_mf_symlink = cifs_query_mf_symlink,
1104         .create_mf_symlink = cifs_create_mf_symlink,
1105         .is_read_op = cifs_is_read_op,
1106         .wp_retry_size = cifs_wp_retry_size,
1107         .dir_needs_close = cifs_dir_needs_close,
1108         .select_sectype = cifs_select_sectype,
1109 #ifdef CONFIG_CIFS_XATTR
1110         .query_all_EAs = CIFSSMBQAllEAs,
1111         .set_EA = CIFSSMBSetEA,
1112 #endif /* CIFS_XATTR */
1113 #ifdef CONFIG_CIFS_ACL
1114         .get_acl = get_cifs_acl,
1115         .get_acl_by_fid = get_cifs_acl_by_fid,
1116         .set_acl = set_cifs_acl,
1117 #endif /* CIFS_ACL */
1118 };
1119
1120 struct smb_version_values smb1_values = {
1121         .version_string = SMB1_VERSION_STRING,
1122         .large_lock_type = LOCKING_ANDX_LARGE_FILES,
1123         .exclusive_lock_type = 0,
1124         .shared_lock_type = LOCKING_ANDX_SHARED_LOCK,
1125         .unlock_lock_type = 0,
1126         .header_preamble_size = 4,
1127         .header_size = sizeof(struct smb_hdr),
1128         .max_header_size = MAX_CIFS_HDR_SIZE,
1129         .read_rsp_size = sizeof(READ_RSP),
1130         .lock_cmd = cpu_to_le16(SMB_COM_LOCKING_ANDX),
1131         .cap_unix = CAP_UNIX,
1132         .cap_nt_find = CAP_NT_SMBS | CAP_NT_FIND,
1133         .cap_large_files = CAP_LARGE_FILES,
1134         .signing_enabled = SECMODE_SIGN_ENABLED,
1135         .signing_required = SECMODE_SIGN_REQUIRED,
1136 };