blob: 9e430ae9314fd7a8eae349632228d8bd4411a287 [file] [log] [blame]
Thomas Gleixnerb6a3d1b2019-06-03 07:44:47 +02001// SPDX-License-Identifier: GPL-2.0-only
Jeff Layton23db65f2012-05-15 12:20:51 -04002/*
3 * SMB1 (CIFS) version specific operations
4 *
5 * Copyright (c) 2012, Jeff Layton <jlayton@redhat.com>
Jeff Layton23db65f2012-05-15 12:20:51 -04006 */
7
Pavel Shilovsky24985c52012-09-18 16:20:28 -07008#include <linux/pagemap.h>
Pavel Shilovsky76ec5e32012-09-18 16:20:33 -07009#include <linux/vfs.h>
Jeff Layton23db65f2012-05-15 12:20:51 -040010#include "cifsglob.h"
Jeff Layton121b0462012-05-15 12:21:10 -040011#include "cifsproto.h"
12#include "cifs_debug.h"
Pavel Shilovsky106dc532012-02-28 14:23:34 +030013#include "cifspdu.h"
Steve French2baa2682014-09-27 02:19:01 -050014#include "cifs_unicode.h"
Jeff Layton121b0462012-05-15 12:21:10 -040015
16/*
17 * An NT cancel request header looks just like the original request except:
18 *
19 * The Command is SMB_COM_NT_CANCEL
20 * The WordCount is zeroed out
21 * The ByteCount is zeroed out
22 *
23 * This function mangles an existing request buffer into a
24 * SMB_COM_NT_CANCEL request and then sends it.
25 */
26static int
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -080027send_nt_cancel(struct TCP_Server_Info *server, struct smb_rqst *rqst,
Jeff Layton121b0462012-05-15 12:21:10 -040028 struct mid_q_entry *mid)
29{
30 int rc = 0;
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -080031 struct smb_hdr *in_buf = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
Jeff Layton121b0462012-05-15 12:21:10 -040032
33 /* -4 for RFC1001 length and +2 for BCC field */
34 in_buf->smb_buf_length = cpu_to_be32(sizeof(struct smb_hdr) - 4 + 2);
35 in_buf->Command = SMB_COM_NT_CANCEL;
36 in_buf->WordCount = 0;
37 put_bcc(0, in_buf);
38
39 mutex_lock(&server->srv_mutex);
40 rc = cifs_sign_smb(in_buf, server, &mid->sequence_number);
41 if (rc) {
42 mutex_unlock(&server->srv_mutex);
43 return rc;
44 }
Jeff Layton31efee62012-12-27 08:05:03 -050045
46 /*
47 * The response to this call was already factored into the sequence
48 * number when the call went out, so we must adjust it back downward
49 * after signing here.
50 */
51 --server->sequence_number;
Jeff Layton121b0462012-05-15 12:21:10 -040052 rc = smb_send(server, in_buf, be32_to_cpu(in_buf->smb_buf_length));
Jeff Laytonad313cb2013-04-03 10:27:36 -040053 if (rc < 0)
54 server->sequence_number--;
55
Jeff Layton121b0462012-05-15 12:21:10 -040056 mutex_unlock(&server->srv_mutex);
57
Joe Perchesf96637b2013-05-04 22:12:25 -050058 cifs_dbg(FYI, "issued NT_CANCEL for mid %u, rc = %d\n",
Tim Gardner3d378d32013-11-02 12:50:34 -050059 get_mid(in_buf), rc);
Jeff Layton121b0462012-05-15 12:21:10 -040060
61 return rc;
62}
Jeff Layton23db65f2012-05-15 12:20:51 -040063
Pavel Shilovsky55157df2012-02-28 14:04:17 +030064static bool
65cifs_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
66{
Pavel Shilovsky4b4de762012-09-18 16:20:26 -070067 return ob1->fid.netfid == ob2->fid.netfid;
Pavel Shilovsky55157df2012-02-28 14:04:17 +030068}
69
Pavel Shilovskyeb378712012-05-17 13:02:51 +040070static unsigned int
71cifs_read_data_offset(char *buf)
72{
73 READ_RSP *rsp = (READ_RSP *)buf;
74 return le16_to_cpu(rsp->DataOffset);
75}
76
77static unsigned int
Long Li74dcf412017-11-22 17:38:46 -070078cifs_read_data_length(char *buf, bool in_remaining)
Pavel Shilovskyeb378712012-05-17 13:02:51 +040079{
80 READ_RSP *rsp = (READ_RSP *)buf;
Long Li74dcf412017-11-22 17:38:46 -070081 /* It's a bug reading remaining data for SMB1 packets */
82 WARN_ON(in_remaining);
Pavel Shilovskyeb378712012-05-17 13:02:51 +040083 return (le16_to_cpu(rsp->DataLengthHigh) << 16) +
84 le16_to_cpu(rsp->DataLength);
85}
86
Pavel Shilovsky8aa26f32012-05-17 13:25:35 +040087static struct mid_q_entry *
88cifs_find_mid(struct TCP_Server_Info *server, char *buffer)
89{
90 struct smb_hdr *buf = (struct smb_hdr *)buffer;
91 struct mid_q_entry *mid;
92
93 spin_lock(&GlobalMid_Lock);
94 list_for_each_entry(mid, &server->pending_mid_q, qhead) {
Tim Gardner3d378d32013-11-02 12:50:34 -050095 if (compare_mid(mid->mid, buf) &&
Pavel Shilovsky8aa26f32012-05-17 13:25:35 +040096 mid->mid_state == MID_REQUEST_SUBMITTED &&
97 le16_to_cpu(mid->command) == buf->Command) {
Lars Persson696e4202018-06-25 14:05:25 +020098 kref_get(&mid->refcount);
Pavel Shilovsky8aa26f32012-05-17 13:25:35 +040099 spin_unlock(&GlobalMid_Lock);
100 return mid;
101 }
102 }
103 spin_unlock(&GlobalMid_Lock);
104 return NULL;
105}
106
Pavel Shilovsky45275782012-05-17 17:53:29 +0400107static void
Pavel Shilovsky335b7b62019-01-16 11:12:41 -0800108cifs_add_credits(struct TCP_Server_Info *server,
109 const struct cifs_credits *credits, const int optype)
Pavel Shilovsky45275782012-05-17 17:53:29 +0400110{
111 spin_lock(&server->req_lock);
Pavel Shilovsky335b7b62019-01-16 11:12:41 -0800112 server->credits += credits->value;
Pavel Shilovsky45275782012-05-17 17:53:29 +0400113 server->in_flight--;
114 spin_unlock(&server->req_lock);
115 wake_up(&server->request_q);
116}
117
118static void
119cifs_set_credits(struct TCP_Server_Info *server, const int val)
120{
121 spin_lock(&server->req_lock);
122 server->credits = val;
123 server->oplocks = val > 1 ? enable_oplocks : false;
124 spin_unlock(&server->req_lock);
125}
126
127static int *
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400128cifs_get_credits_field(struct TCP_Server_Info *server, const int optype)
Pavel Shilovsky45275782012-05-17 17:53:29 +0400129{
130 return &server->credits;
131}
132
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400133static unsigned int
134cifs_get_credits(struct mid_q_entry *mid)
135{
136 return 1;
137}
138
Pavel Shilovsky88257362012-05-23 14:01:59 +0400139/*
140 * Find a free multiplex id (SMB mid). Otherwise there could be
141 * mid collisions which might cause problems, demultiplexing the
142 * wrong response to this request. Multiplex ids could collide if
143 * one of a series requests takes much longer than the others, or
144 * if a very large number of long lived requests (byte range
145 * locks or FindNotify requests) are pending. No more than
146 * 64K-1 requests can be outstanding at one time. If no
147 * mids are available, return zero. A future optimization
148 * could make the combination of mids and uid the key we use
149 * to demultiplex on (rather than mid alone).
150 * In addition to the above check, the cifs demultiplex
151 * code already used the command code as a secondary
152 * check of the frame and if signing is negotiated the
153 * response would be discarded if the mid were the same
154 * but the signature was wrong. Since the mid is not put in the
155 * pending queue until later (when it is about to be dispatched)
156 * we do have to limit the number of outstanding requests
157 * to somewhat less than 64K-1 although it is hard to imagine
158 * so many threads being in the vfs at one time.
159 */
160static __u64
161cifs_get_next_mid(struct TCP_Server_Info *server)
162{
163 __u64 mid = 0;
164 __u16 last_mid, cur_mid;
165 bool collision;
166
167 spin_lock(&GlobalMid_Lock);
168
169 /* mid is 16 bit only for CIFS/SMB */
170 cur_mid = (__u16)((server->CurrentMid) & 0xffff);
171 /* we do not want to loop forever */
172 last_mid = cur_mid;
173 cur_mid++;
174
175 /*
176 * This nested loop looks more expensive than it is.
177 * In practice the list of pending requests is short,
178 * fewer than 50, and the mids are likely to be unique
179 * on the first pass through the loop unless some request
180 * takes longer than the 64 thousand requests before it
181 * (and it would also have to have been a request that
182 * did not time out).
183 */
184 while (cur_mid != last_mid) {
185 struct mid_q_entry *mid_entry;
186 unsigned int num_mids;
187
188 collision = false;
189 if (cur_mid == 0)
190 cur_mid++;
191
192 num_mids = 0;
193 list_for_each_entry(mid_entry, &server->pending_mid_q, qhead) {
194 ++num_mids;
195 if (mid_entry->mid == cur_mid &&
196 mid_entry->mid_state == MID_REQUEST_SUBMITTED) {
197 /* This mid is in use, try a different one */
198 collision = true;
199 break;
200 }
201 }
202
203 /*
204 * if we have more than 32k mids in the list, then something
205 * is very wrong. Possibly a local user is trying to DoS the
206 * box by issuing long-running calls and SIGKILL'ing them. If
207 * we get to 2^16 mids then we're in big trouble as this
208 * function could loop forever.
209 *
210 * Go ahead and assign out the mid in this situation, but force
211 * an eventual reconnect to clean out the pending_mid_q.
212 */
213 if (num_mids > 32768)
214 server->tcpStatus = CifsNeedReconnect;
215
216 if (!collision) {
217 mid = (__u64)cur_mid;
218 server->CurrentMid = mid;
219 break;
220 }
221 cur_mid++;
222 }
223 spin_unlock(&GlobalMid_Lock);
224 return mid;
225}
226
Pavel Shilovsky316cf942012-05-23 14:31:03 +0400227/*
228 return codes:
229 0 not a transact2, or all data present
230 >0 transact2 with that much data missing
231 -EINVAL invalid transact2
232 */
233static int
234check2ndT2(char *buf)
235{
236 struct smb_hdr *pSMB = (struct smb_hdr *)buf;
237 struct smb_t2_rsp *pSMBt;
238 int remaining;
239 __u16 total_data_size, data_in_this_rsp;
240
241 if (pSMB->Command != SMB_COM_TRANSACTION2)
242 return 0;
243
244 /* check for plausible wct, bcc and t2 data and parm sizes */
245 /* check for parm and data offset going beyond end of smb */
246 if (pSMB->WordCount != 10) { /* coalesce_t2 depends on this */
Joe Perchesf96637b2013-05-04 22:12:25 -0500247 cifs_dbg(FYI, "invalid transact2 word count\n");
Pavel Shilovsky316cf942012-05-23 14:31:03 +0400248 return -EINVAL;
249 }
250
251 pSMBt = (struct smb_t2_rsp *)pSMB;
252
253 total_data_size = get_unaligned_le16(&pSMBt->t2_rsp.TotalDataCount);
254 data_in_this_rsp = get_unaligned_le16(&pSMBt->t2_rsp.DataCount);
255
256 if (total_data_size == data_in_this_rsp)
257 return 0;
258 else if (total_data_size < data_in_this_rsp) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500259 cifs_dbg(FYI, "total data %d smaller than data in frame %d\n",
260 total_data_size, data_in_this_rsp);
Pavel Shilovsky316cf942012-05-23 14:31:03 +0400261 return -EINVAL;
262 }
263
264 remaining = total_data_size - data_in_this_rsp;
265
Joe Perchesf96637b2013-05-04 22:12:25 -0500266 cifs_dbg(FYI, "missing %d bytes from transact2, check next response\n",
267 remaining);
Pavel Shilovsky316cf942012-05-23 14:31:03 +0400268 if (total_data_size > CIFSMaxBufSize) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500269 cifs_dbg(VFS, "TotalDataSize %d is over maximum buffer %d\n",
270 total_data_size, CIFSMaxBufSize);
Pavel Shilovsky316cf942012-05-23 14:31:03 +0400271 return -EINVAL;
272 }
273 return remaining;
274}
275
276static int
277coalesce_t2(char *second_buf, struct smb_hdr *target_hdr)
278{
279 struct smb_t2_rsp *pSMBs = (struct smb_t2_rsp *)second_buf;
280 struct smb_t2_rsp *pSMBt = (struct smb_t2_rsp *)target_hdr;
281 char *data_area_of_tgt;
282 char *data_area_of_src;
283 int remaining;
284 unsigned int byte_count, total_in_tgt;
285 __u16 tgt_total_cnt, src_total_cnt, total_in_src;
286
287 src_total_cnt = get_unaligned_le16(&pSMBs->t2_rsp.TotalDataCount);
288 tgt_total_cnt = get_unaligned_le16(&pSMBt->t2_rsp.TotalDataCount);
289
290 if (tgt_total_cnt != src_total_cnt)
Joe Perchesf96637b2013-05-04 22:12:25 -0500291 cifs_dbg(FYI, "total data count of primary and secondary t2 differ source=%hu target=%hu\n",
292 src_total_cnt, tgt_total_cnt);
Pavel Shilovsky316cf942012-05-23 14:31:03 +0400293
294 total_in_tgt = get_unaligned_le16(&pSMBt->t2_rsp.DataCount);
295
296 remaining = tgt_total_cnt - total_in_tgt;
297
298 if (remaining < 0) {
Louis Taylor259594b2019-02-27 22:25:15 +0000299 cifs_dbg(FYI, "Server sent too much data. tgt_total_cnt=%hu total_in_tgt=%u\n",
Joe Perchesf96637b2013-05-04 22:12:25 -0500300 tgt_total_cnt, total_in_tgt);
Pavel Shilovsky316cf942012-05-23 14:31:03 +0400301 return -EPROTO;
302 }
303
304 if (remaining == 0) {
305 /* nothing to do, ignore */
Joe Perchesf96637b2013-05-04 22:12:25 -0500306 cifs_dbg(FYI, "no more data remains\n");
Pavel Shilovsky316cf942012-05-23 14:31:03 +0400307 return 0;
308 }
309
310 total_in_src = get_unaligned_le16(&pSMBs->t2_rsp.DataCount);
311 if (remaining < total_in_src)
Joe Perchesf96637b2013-05-04 22:12:25 -0500312 cifs_dbg(FYI, "transact2 2nd response contains too much data\n");
Pavel Shilovsky316cf942012-05-23 14:31:03 +0400313
314 /* find end of first SMB data area */
315 data_area_of_tgt = (char *)&pSMBt->hdr.Protocol +
316 get_unaligned_le16(&pSMBt->t2_rsp.DataOffset);
317
318 /* validate target area */
319 data_area_of_src = (char *)&pSMBs->hdr.Protocol +
320 get_unaligned_le16(&pSMBs->t2_rsp.DataOffset);
321
322 data_area_of_tgt += total_in_tgt;
323
324 total_in_tgt += total_in_src;
325 /* is the result too big for the field? */
326 if (total_in_tgt > USHRT_MAX) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500327 cifs_dbg(FYI, "coalesced DataCount too large (%u)\n",
328 total_in_tgt);
Pavel Shilovsky316cf942012-05-23 14:31:03 +0400329 return -EPROTO;
330 }
331 put_unaligned_le16(total_in_tgt, &pSMBt->t2_rsp.DataCount);
332
333 /* fix up the BCC */
334 byte_count = get_bcc(target_hdr);
335 byte_count += total_in_src;
336 /* is the result too big for the field? */
337 if (byte_count > USHRT_MAX) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500338 cifs_dbg(FYI, "coalesced BCC too large (%u)\n", byte_count);
Pavel Shilovsky316cf942012-05-23 14:31:03 +0400339 return -EPROTO;
340 }
341 put_bcc(byte_count, target_hdr);
342
343 byte_count = be32_to_cpu(target_hdr->smb_buf_length);
344 byte_count += total_in_src;
345 /* don't allow buffer to overflow */
346 if (byte_count > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500347 cifs_dbg(FYI, "coalesced BCC exceeds buffer size (%u)\n",
348 byte_count);
Pavel Shilovsky316cf942012-05-23 14:31:03 +0400349 return -ENOBUFS;
350 }
351 target_hdr->smb_buf_length = cpu_to_be32(byte_count);
352
353 /* copy second buffer into end of first buffer */
354 memcpy(data_area_of_tgt, data_area_of_src, total_in_src);
355
356 if (remaining != total_in_src) {
357 /* more responses to go */
Joe Perchesf96637b2013-05-04 22:12:25 -0500358 cifs_dbg(FYI, "waiting for more secondary responses\n");
Pavel Shilovsky316cf942012-05-23 14:31:03 +0400359 return 1;
360 }
361
362 /* we are done */
Joe Perchesf96637b2013-05-04 22:12:25 -0500363 cifs_dbg(FYI, "found the last secondary response\n");
Pavel Shilovsky316cf942012-05-23 14:31:03 +0400364 return 0;
365}
366
Sachin Prabhuc11f1df2014-03-11 16:11:47 +0000367static void
368cifs_downgrade_oplock(struct TCP_Server_Info *server,
369 struct cifsInodeInfo *cinode, bool set_level2)
370{
371 if (set_level2)
372 cifs_set_oplock_level(cinode, OPLOCK_READ);
373 else
374 cifs_set_oplock_level(cinode, 0);
375}
376
Pavel Shilovsky316cf942012-05-23 14:31:03 +0400377static bool
378cifs_check_trans2(struct mid_q_entry *mid, struct TCP_Server_Info *server,
379 char *buf, int malformed)
380{
381 if (malformed)
382 return false;
383 if (check2ndT2(buf) <= 0)
384 return false;
385 mid->multiRsp = true;
386 if (mid->resp_buf) {
387 /* merge response - fix up 1st*/
388 malformed = coalesce_t2(buf, mid->resp_buf);
389 if (malformed > 0)
390 return true;
391 /* All parts received or packet is malformed. */
392 mid->multiEnd = true;
393 dequeue_mid(mid, malformed);
394 return true;
395 }
396 if (!server->large_buf) {
397 /*FIXME: switch to already allocated largebuf?*/
Joe Perchesf96637b2013-05-04 22:12:25 -0500398 cifs_dbg(VFS, "1st trans2 resp needs bigbuf\n");
Pavel Shilovsky316cf942012-05-23 14:31:03 +0400399 } else {
400 /* Have first buffer */
401 mid->resp_buf = buf;
402 mid->large_buf = true;
403 server->bigbuf = NULL;
404 }
405 return true;
406}
407
Pavel Shilovsky286170a2012-05-25 10:43:58 +0400408static bool
409cifs_need_neg(struct TCP_Server_Info *server)
410{
411 return server->maxBuf == 0;
412}
413
414static int
415cifs_negotiate(const unsigned int xid, struct cifs_ses *ses)
416{
417 int rc;
418 rc = CIFSSMBNegotiate(xid, ses);
419 if (rc == -EAGAIN) {
420 /* retry only once on 1st time connection */
421 set_credits(ses->server, 1);
422 rc = CIFSSMBNegotiate(xid, ses);
423 if (rc == -EAGAIN)
424 rc = -EHOSTDOWN;
425 }
426 return rc;
427}
428
Pavel Shilovsky24985c52012-09-18 16:20:28 -0700429static unsigned int
430cifs_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
431{
432 __u64 unix_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
433 struct TCP_Server_Info *server = tcon->ses->server;
434 unsigned int wsize;
435
436 /* start with specified wsize, or default */
437 if (volume_info->wsize)
438 wsize = volume_info->wsize;
439 else if (tcon->unix_ext && (unix_cap & CIFS_UNIX_LARGE_WRITE_CAP))
440 wsize = CIFS_DEFAULT_IOSIZE;
441 else
442 wsize = CIFS_DEFAULT_NON_POSIX_WSIZE;
443
444 /* can server support 24-bit write sizes? (via UNIX extensions) */
445 if (!tcon->unix_ext || !(unix_cap & CIFS_UNIX_LARGE_WRITE_CAP))
446 wsize = min_t(unsigned int, wsize, CIFS_MAX_RFC1002_WSIZE);
447
448 /*
449 * no CAP_LARGE_WRITE_X or is signing enabled without CAP_UNIX set?
450 * Limit it to max buffer offered by the server, minus the size of the
451 * WRITEX header, not including the 4 byte RFC1001 length.
452 */
453 if (!(server->capabilities & CAP_LARGE_WRITE_X) ||
Jeff Layton38d77c52013-05-26 07:01:00 -0400454 (!(server->capabilities & CAP_UNIX) && server->sign))
Pavel Shilovsky24985c52012-09-18 16:20:28 -0700455 wsize = min_t(unsigned int, wsize,
456 server->maxBuf - sizeof(WRITE_REQ) + 4);
457
Pavel Shilovsky24985c52012-09-18 16:20:28 -0700458 /* hard limit of CIFS_MAX_WSIZE */
459 wsize = min_t(unsigned int, wsize, CIFS_MAX_WSIZE);
460
461 return wsize;
462}
463
464static unsigned int
465cifs_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
466{
467 __u64 unix_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
468 struct TCP_Server_Info *server = tcon->ses->server;
469 unsigned int rsize, defsize;
470
471 /*
472 * Set default value...
473 *
474 * HACK alert! Ancient servers have very small buffers. Even though
475 * MS-CIFS indicates that servers are only limited by the client's
476 * bufsize for reads, testing against win98se shows that it throws
477 * INVALID_PARAMETER errors if you try to request too large a read.
478 * OS/2 just sends back short reads.
479 *
480 * If the server doesn't advertise CAP_LARGE_READ_X, then assume that
481 * it can't handle a read request larger than its MaxBufferSize either.
482 */
483 if (tcon->unix_ext && (unix_cap & CIFS_UNIX_LARGE_READ_CAP))
484 defsize = CIFS_DEFAULT_IOSIZE;
485 else if (server->capabilities & CAP_LARGE_READ_X)
486 defsize = CIFS_DEFAULT_NON_POSIX_RSIZE;
487 else
488 defsize = server->maxBuf - sizeof(READ_RSP);
489
490 rsize = volume_info->rsize ? volume_info->rsize : defsize;
491
492 /*
493 * no CAP_LARGE_READ_X? Then MS-CIFS states that we must limit this to
494 * the client's MaxBufferSize.
495 */
496 if (!(server->capabilities & CAP_LARGE_READ_X))
497 rsize = min_t(unsigned int, CIFSMaxBufSize, rsize);
498
Pavel Shilovsky24985c52012-09-18 16:20:28 -0700499 /* hard limit of CIFS_MAX_RSIZE */
500 rsize = min_t(unsigned int, rsize, CIFS_MAX_RSIZE);
501
502 return rsize;
503}
504
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +0400505static void
506cifs_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
507{
508 CIFSSMBQFSDeviceInfo(xid, tcon);
509 CIFSSMBQFSAttributeInfo(xid, tcon);
510}
511
Pavel Shilovsky68889f22012-05-25 14:40:22 +0400512static int
513cifs_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
514 struct cifs_sb_info *cifs_sb, const char *full_path)
515{
516 int rc;
517 FILE_ALL_INFO *file_info;
518
519 file_info = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
520 if (file_info == NULL)
521 return -ENOMEM;
522
523 rc = CIFSSMBQPathInfo(xid, tcon, full_path, file_info,
524 0 /* not legacy */, cifs_sb->local_nls,
Steve French2baa2682014-09-27 02:19:01 -0500525 cifs_remap(cifs_sb));
Pavel Shilovsky68889f22012-05-25 14:40:22 +0400526
527 if (rc == -EOPNOTSUPP || rc == -EINVAL)
528 rc = SMBQueryInformation(xid, tcon, full_path, file_info,
Steve French2baa2682014-09-27 02:19:01 -0500529 cifs_sb->local_nls, cifs_remap(cifs_sb));
Pavel Shilovsky68889f22012-05-25 14:40:22 +0400530 kfree(file_info);
531 return rc;
532}
533
Pavel Shilovsky1208ef12012-05-27 17:34:43 +0400534static int
535cifs_query_path_info(const unsigned int xid, struct cifs_tcon *tcon,
536 struct cifs_sb_info *cifs_sb, const char *full_path,
Pavel Shilovskyeb85d94b2013-10-23 17:49:47 +0400537 FILE_ALL_INFO *data, bool *adjustTZ, bool *symlink)
Pavel Shilovsky1208ef12012-05-27 17:34:43 +0400538{
539 int rc;
540
Pavel Shilovskyeb85d94b2013-10-23 17:49:47 +0400541 *symlink = false;
542
Pavel Shilovsky1208ef12012-05-27 17:34:43 +0400543 /* could do find first instead but this returns more info */
544 rc = CIFSSMBQPathInfo(xid, tcon, full_path, data, 0 /* not legacy */,
Steve French2baa2682014-09-27 02:19:01 -0500545 cifs_sb->local_nls, cifs_remap(cifs_sb));
Pavel Shilovsky1208ef12012-05-27 17:34:43 +0400546 /*
547 * BB optimize code so we do not make the above call when server claims
548 * no NT SMB support and the above call failed at least once - set flag
549 * in tcon or mount.
550 */
551 if ((rc == -EOPNOTSUPP) || (rc == -EINVAL)) {
552 rc = SMBQueryInformation(xid, tcon, full_path, data,
553 cifs_sb->local_nls,
Steve French2baa2682014-09-27 02:19:01 -0500554 cifs_remap(cifs_sb));
Pavel Shilovsky1208ef12012-05-27 17:34:43 +0400555 *adjustTZ = true;
556 }
Pavel Shilovskyeb85d94b2013-10-23 17:49:47 +0400557
558 if (!rc && (le32_to_cpu(data->Attributes) & ATTR_REPARSE)) {
559 int tmprc;
560 int oplock = 0;
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +0400561 struct cifs_fid fid;
562 struct cifs_open_parms oparms;
563
564 oparms.tcon = tcon;
565 oparms.cifs_sb = cifs_sb;
566 oparms.desired_access = FILE_READ_ATTRIBUTES;
567 oparms.create_options = 0;
568 oparms.disposition = FILE_OPEN;
569 oparms.path = full_path;
570 oparms.fid = &fid;
571 oparms.reconnect = false;
Pavel Shilovskyeb85d94b2013-10-23 17:49:47 +0400572
573 /* Need to check if this is a symbolic link or not */
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +0400574 tmprc = CIFS_open(xid, &oparms, &oplock, NULL);
Pavel Shilovskyeb85d94b2013-10-23 17:49:47 +0400575 if (tmprc == -EOPNOTSUPP)
576 *symlink = true;
Steve French19e81572014-09-25 01:26:55 -0500577 else if (tmprc == 0)
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +0400578 CIFSSMBClose(xid, tcon, fid.netfid);
Pavel Shilovskyeb85d94b2013-10-23 17:49:47 +0400579 }
580
Pavel Shilovsky1208ef12012-05-27 17:34:43 +0400581 return rc;
582}
583
584static int
585cifs_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
586 struct cifs_sb_info *cifs_sb, const char *full_path,
587 u64 *uniqueid, FILE_ALL_INFO *data)
588{
589 /*
590 * We can not use the IndexNumber field by default from Windows or
591 * Samba (in ALL_INFO buf) but we can request it explicitly. The SNIA
592 * CIFS spec claims that this value is unique within the scope of a
593 * share, and the windows docs hint that it's actually unique
594 * per-machine.
595 *
596 * There may be higher info levels that work but are there Windows
597 * server or network appliances for which IndexNumber field is not
598 * guaranteed unique?
599 */
600 return CIFSGetSrvInodeNumber(xid, tcon, full_path, uniqueid,
601 cifs_sb->local_nls,
Steve French2baa2682014-09-27 02:19:01 -0500602 cifs_remap(cifs_sb));
Pavel Shilovsky1208ef12012-05-27 17:34:43 +0400603}
604
Pavel Shilovsky4ad65042012-09-18 16:20:26 -0700605static int
606cifs_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
607 struct cifs_fid *fid, FILE_ALL_INFO *data)
608{
609 return CIFSSMBQFileInfo(xid, tcon, fid->netfid, data);
610}
611
Pavel Shilovsky44c58182012-05-28 14:16:31 +0400612static void
613cifs_clear_stats(struct cifs_tcon *tcon)
614{
Pavel Shilovsky44c58182012-05-28 14:16:31 +0400615 atomic_set(&tcon->stats.cifs_stats.num_writes, 0);
616 atomic_set(&tcon->stats.cifs_stats.num_reads, 0);
617 atomic_set(&tcon->stats.cifs_stats.num_flushes, 0);
618 atomic_set(&tcon->stats.cifs_stats.num_oplock_brks, 0);
619 atomic_set(&tcon->stats.cifs_stats.num_opens, 0);
620 atomic_set(&tcon->stats.cifs_stats.num_posixopens, 0);
621 atomic_set(&tcon->stats.cifs_stats.num_posixmkdirs, 0);
622 atomic_set(&tcon->stats.cifs_stats.num_closes, 0);
623 atomic_set(&tcon->stats.cifs_stats.num_deletes, 0);
624 atomic_set(&tcon->stats.cifs_stats.num_mkdirs, 0);
625 atomic_set(&tcon->stats.cifs_stats.num_rmdirs, 0);
626 atomic_set(&tcon->stats.cifs_stats.num_renames, 0);
627 atomic_set(&tcon->stats.cifs_stats.num_t2renames, 0);
628 atomic_set(&tcon->stats.cifs_stats.num_ffirst, 0);
629 atomic_set(&tcon->stats.cifs_stats.num_fnext, 0);
630 atomic_set(&tcon->stats.cifs_stats.num_fclose, 0);
631 atomic_set(&tcon->stats.cifs_stats.num_hardlinks, 0);
632 atomic_set(&tcon->stats.cifs_stats.num_symlinks, 0);
633 atomic_set(&tcon->stats.cifs_stats.num_locks, 0);
634 atomic_set(&tcon->stats.cifs_stats.num_acl_get, 0);
635 atomic_set(&tcon->stats.cifs_stats.num_acl_set, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +0400636}
637
638static void
639cifs_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
640{
Pavel Shilovsky44c58182012-05-28 14:16:31 +0400641 seq_printf(m, " Oplocks breaks: %d",
642 atomic_read(&tcon->stats.cifs_stats.num_oplock_brks));
643 seq_printf(m, "\nReads: %d Bytes: %llu",
644 atomic_read(&tcon->stats.cifs_stats.num_reads),
645 (long long)(tcon->bytes_read));
646 seq_printf(m, "\nWrites: %d Bytes: %llu",
647 atomic_read(&tcon->stats.cifs_stats.num_writes),
648 (long long)(tcon->bytes_written));
649 seq_printf(m, "\nFlushes: %d",
650 atomic_read(&tcon->stats.cifs_stats.num_flushes));
651 seq_printf(m, "\nLocks: %d HardLinks: %d Symlinks: %d",
652 atomic_read(&tcon->stats.cifs_stats.num_locks),
653 atomic_read(&tcon->stats.cifs_stats.num_hardlinks),
654 atomic_read(&tcon->stats.cifs_stats.num_symlinks));
655 seq_printf(m, "\nOpens: %d Closes: %d Deletes: %d",
656 atomic_read(&tcon->stats.cifs_stats.num_opens),
657 atomic_read(&tcon->stats.cifs_stats.num_closes),
658 atomic_read(&tcon->stats.cifs_stats.num_deletes));
659 seq_printf(m, "\nPosix Opens: %d Posix Mkdirs: %d",
660 atomic_read(&tcon->stats.cifs_stats.num_posixopens),
661 atomic_read(&tcon->stats.cifs_stats.num_posixmkdirs));
662 seq_printf(m, "\nMkdirs: %d Rmdirs: %d",
663 atomic_read(&tcon->stats.cifs_stats.num_mkdirs),
664 atomic_read(&tcon->stats.cifs_stats.num_rmdirs));
665 seq_printf(m, "\nRenames: %d T2 Renames %d",
666 atomic_read(&tcon->stats.cifs_stats.num_renames),
667 atomic_read(&tcon->stats.cifs_stats.num_t2renames));
668 seq_printf(m, "\nFindFirst: %d FNext %d FClose %d",
669 atomic_read(&tcon->stats.cifs_stats.num_ffirst),
670 atomic_read(&tcon->stats.cifs_stats.num_fnext),
671 atomic_read(&tcon->stats.cifs_stats.num_fclose));
Pavel Shilovsky44c58182012-05-28 14:16:31 +0400672}
673
Pavel Shilovskyf4367202012-03-17 11:41:12 +0300674static void
675cifs_mkdir_setinfo(struct inode *inode, const char *full_path,
676 struct cifs_sb_info *cifs_sb, struct cifs_tcon *tcon,
677 const unsigned int xid)
678{
679 FILE_BASIC_INFO info;
680 struct cifsInodeInfo *cifsInode;
681 u32 dosattrs;
682 int rc;
683
684 memset(&info, 0, sizeof(info));
685 cifsInode = CIFS_I(inode);
686 dosattrs = cifsInode->cifsAttrs|ATTR_READONLY;
687 info.Attributes = cpu_to_le32(dosattrs);
688 rc = CIFSSMBSetPathInfo(xid, tcon, full_path, &info, cifs_sb->local_nls,
Steve French2baa2682014-09-27 02:19:01 -0500689 cifs_remap(cifs_sb));
Pavel Shilovskyf4367202012-03-17 11:41:12 +0300690 if (rc == 0)
691 cifsInode->cifsAttrs = dosattrs;
692}
693
Pavel Shilovskyfb1214e2012-09-18 16:20:26 -0700694static int
Pavel Shilovsky226730b2013-07-05 12:00:30 +0400695cifs_open_file(const unsigned int xid, struct cifs_open_parms *oparms,
696 __u32 *oplock, FILE_ALL_INFO *buf)
Pavel Shilovskyfb1214e2012-09-18 16:20:26 -0700697{
Pavel Shilovsky226730b2013-07-05 12:00:30 +0400698 if (!(oparms->tcon->ses->capabilities & CAP_NT_SMBS))
699 return SMBLegacyOpen(xid, oparms->tcon, oparms->path,
700 oparms->disposition,
701 oparms->desired_access,
702 oparms->create_options,
703 &oparms->fid->netfid, oplock, buf,
704 oparms->cifs_sb->local_nls,
Steve French2baa2682014-09-27 02:19:01 -0500705 cifs_remap(oparms->cifs_sb));
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +0400706 return CIFS_open(xid, oparms, oplock, buf);
Pavel Shilovskyfb1214e2012-09-18 16:20:26 -0700707}
708
709static void
710cifs_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
711{
David Howells2b0143b2015-03-17 22:25:59 +0000712 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
Pavel Shilovskyfb1214e2012-09-18 16:20:26 -0700713 cfile->fid.netfid = fid->netfid;
714 cifs_set_oplock_level(cinode, oplock);
Pavel Shilovsky18cceb62013-09-05 13:01:06 +0400715 cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
Pavel Shilovskyfb1214e2012-09-18 16:20:26 -0700716}
717
Pavel Shilovsky760ad0c2012-09-25 11:00:07 +0400718static void
Pavel Shilovsky0ff78a22012-09-18 16:20:26 -0700719cifs_close_file(const unsigned int xid, struct cifs_tcon *tcon,
720 struct cifs_fid *fid)
721{
Pavel Shilovsky760ad0c2012-09-25 11:00:07 +0400722 CIFSSMBClose(xid, tcon, fid->netfid);
Pavel Shilovsky0ff78a22012-09-18 16:20:26 -0700723}
724
Pavel Shilovsky1d8c4c02012-09-18 16:20:27 -0700725static int
726cifs_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
727 struct cifs_fid *fid)
728{
729 return CIFSSMBFlush(xid, tcon, fid->netfid);
730}
731
Pavel Shilovskyf9c6e232012-09-18 16:20:29 -0700732static int
Steve Frenchdb8b6312014-09-22 05:13:55 -0500733cifs_sync_read(const unsigned int xid, struct cifs_fid *pfid,
Pavel Shilovskyf9c6e232012-09-18 16:20:29 -0700734 struct cifs_io_parms *parms, unsigned int *bytes_read,
735 char **buf, int *buf_type)
736{
Steve Frenchdb8b6312014-09-22 05:13:55 -0500737 parms->netfid = pfid->netfid;
Pavel Shilovskyf9c6e232012-09-18 16:20:29 -0700738 return CIFSSMBRead(xid, parms, bytes_read, buf, buf_type);
739}
740
Pavel Shilovskyba9ad7252012-09-18 16:20:30 -0700741static int
Steve Frenchdb8b6312014-09-22 05:13:55 -0500742cifs_sync_write(const unsigned int xid, struct cifs_fid *pfid,
Pavel Shilovskyba9ad7252012-09-18 16:20:30 -0700743 struct cifs_io_parms *parms, unsigned int *written,
744 struct kvec *iov, unsigned long nr_segs)
745{
746
Steve Frenchdb8b6312014-09-22 05:13:55 -0500747 parms->netfid = pfid->netfid;
Pavel Shilovskyba9ad7252012-09-18 16:20:30 -0700748 return CIFSSMBWrite2(xid, parms, written, iov, nr_segs);
749}
750
Pavel Shilovsky6bdf6db2012-09-18 16:20:32 -0700751static int
752smb_set_file_info(struct inode *inode, const char *full_path,
753 FILE_BASIC_INFO *buf, const unsigned int xid)
754{
755 int oplock = 0;
756 int rc;
Pavel Shilovsky6bdf6db2012-09-18 16:20:32 -0700757 __u32 netpid;
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +0400758 struct cifs_fid fid;
759 struct cifs_open_parms oparms;
Pavel Shilovsky6bdf6db2012-09-18 16:20:32 -0700760 struct cifsFileInfo *open_file;
761 struct cifsInodeInfo *cinode = CIFS_I(inode);
762 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
763 struct tcon_link *tlink = NULL;
764 struct cifs_tcon *tcon;
Pavel Shilovsky6bdf6db2012-09-18 16:20:32 -0700765
766 /* if the file is already open for write, just use that fileid */
767 open_file = find_writable_file(cinode, true);
768 if (open_file) {
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +0400769 fid.netfid = open_file->fid.netfid;
Pavel Shilovsky6bdf6db2012-09-18 16:20:32 -0700770 netpid = open_file->pid;
771 tcon = tlink_tcon(open_file->tlink);
772 goto set_via_filehandle;
773 }
774
775 tlink = cifs_sb_tlink(cifs_sb);
776 if (IS_ERR(tlink)) {
777 rc = PTR_ERR(tlink);
778 tlink = NULL;
779 goto out;
780 }
781 tcon = tlink_tcon(tlink);
782
Jeff Layton281e2e72013-05-26 07:00:56 -0400783 rc = CIFSSMBSetPathInfo(xid, tcon, full_path, buf, cifs_sb->local_nls,
Steve French2baa2682014-09-27 02:19:01 -0500784 cifs_remap(cifs_sb));
Jeff Layton281e2e72013-05-26 07:00:56 -0400785 if (rc == 0) {
786 cinode->cifsAttrs = le32_to_cpu(buf->Attributes);
787 goto out;
788 } else if (rc != -EOPNOTSUPP && rc != -EINVAL) {
789 goto out;
Pavel Shilovsky6bdf6db2012-09-18 16:20:32 -0700790 }
791
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +0400792 oparms.tcon = tcon;
793 oparms.cifs_sb = cifs_sb;
794 oparms.desired_access = SYNCHRONIZE | FILE_WRITE_ATTRIBUTES;
795 oparms.create_options = CREATE_NOT_DIR;
796 oparms.disposition = FILE_OPEN;
797 oparms.path = full_path;
798 oparms.fid = &fid;
799 oparms.reconnect = false;
Pavel Shilovsky6bdf6db2012-09-18 16:20:32 -0700800
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +0400801 cifs_dbg(FYI, "calling SetFileInfo since SetPathInfo for times not supported by this server\n");
802 rc = CIFS_open(xid, &oparms, &oplock, NULL);
Pavel Shilovsky6bdf6db2012-09-18 16:20:32 -0700803 if (rc != 0) {
804 if (rc == -EIO)
805 rc = -EINVAL;
806 goto out;
807 }
808
809 netpid = current->tgid;
810
811set_via_filehandle:
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +0400812 rc = CIFSSMBSetFileInfo(xid, tcon, buf, fid.netfid, netpid);
Pavel Shilovsky6bdf6db2012-09-18 16:20:32 -0700813 if (!rc)
814 cinode->cifsAttrs = le32_to_cpu(buf->Attributes);
815
816 if (open_file == NULL)
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +0400817 CIFSSMBClose(xid, tcon, fid.netfid);
Pavel Shilovsky6bdf6db2012-09-18 16:20:32 -0700818 else
819 cifsFileInfo_put(open_file);
820out:
821 if (tlink != NULL)
822 cifs_put_tlink(tlink);
823 return rc;
824}
825
Pavel Shilovsky92fc65a2012-09-18 16:20:32 -0700826static int
Steve Frenchc7f508a2013-10-14 15:27:32 -0500827cifs_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
828 struct cifsFileInfo *cfile)
829{
830 return CIFSSMB_set_compression(xid, tcon, cfile->fid.netfid);
831}
832
833static int
Pavel Shilovsky92fc65a2012-09-18 16:20:32 -0700834cifs_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
835 const char *path, struct cifs_sb_info *cifs_sb,
836 struct cifs_fid *fid, __u16 search_flags,
837 struct cifs_search_info *srch_inf)
838{
Pavel Shilovskydcd878382017-06-06 16:58:58 -0700839 int rc;
840
841 rc = CIFSFindFirst(xid, tcon, path, cifs_sb,
842 &fid->netfid, search_flags, srch_inf, true);
843 if (rc)
844 cifs_dbg(FYI, "find first failed=%d\n", rc);
845 return rc;
Pavel Shilovsky92fc65a2012-09-18 16:20:32 -0700846}
847
848static int
849cifs_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
850 struct cifs_fid *fid, __u16 search_flags,
851 struct cifs_search_info *srch_inf)
852{
853 return CIFSFindNext(xid, tcon, fid->netfid, search_flags, srch_inf);
854}
855
856static int
857cifs_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
858 struct cifs_fid *fid)
859{
860 return CIFSFindClose(xid, tcon, fid->netfid);
861}
862
Pavel Shilovsky95a3f2f2012-09-18 16:20:33 -0700863static int
864cifs_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
865 struct cifsInodeInfo *cinode)
866{
867 return CIFSSMBLock(0, tcon, fid->netfid, current->tgid, 0, 0, 0, 0,
868 LOCKING_ANDX_OPLOCK_RELEASE, false,
Pavel Shilovsky18cceb62013-09-05 13:01:06 +0400869 CIFS_CACHE_READ(cinode) ? 1 : 0);
Pavel Shilovsky95a3f2f2012-09-18 16:20:33 -0700870}
871
Pavel Shilovsky76ec5e32012-09-18 16:20:33 -0700872static int
873cifs_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
874 struct kstatfs *buf)
875{
876 int rc = -EOPNOTSUPP;
877
878 buf->f_type = CIFS_MAGIC_NUMBER;
879
880 /*
881 * We could add a second check for a QFS Unix capability bit
882 */
883 if ((tcon->ses->capabilities & CAP_UNIX) &&
884 (CIFS_POSIX_EXTENSIONS & le64_to_cpu(tcon->fsUnixInfo.Capability)))
885 rc = CIFSSMBQFSPosixInfo(xid, tcon, buf);
886
887 /*
888 * Only need to call the old QFSInfo if failed on newer one,
889 * e.g. by OS/2.
890 **/
891 if (rc && (tcon->ses->capabilities & CAP_NT_SMBS))
892 rc = CIFSSMBQFSInfo(xid, tcon, buf);
893
894 /*
895 * Some old Windows servers also do not support level 103, retry with
896 * older level one if old server failed the previous call or we
897 * bypassed it because we detected that this was an older LANMAN sess
898 */
899 if (rc)
900 rc = SMBOldQFSInfo(xid, tcon, buf);
901 return rc;
902}
903
Pavel Shilovskyd39a4f72012-09-19 06:22:43 -0700904static int
905cifs_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
906 __u64 length, __u32 type, int lock, int unlock, bool wait)
907{
908 return CIFSSMBLock(xid, tlink_tcon(cfile->tlink), cfile->fid.netfid,
909 current->tgid, length, offset, unlock, lock,
910 (__u8)type, wait, 0);
911}
912
Pavel Shilovskyd244bf22013-08-14 19:25:22 +0400913static int
Sachin Prabhu924e3fa2013-12-02 16:37:43 +0000914cifs_unix_dfs_readlink(const unsigned int xid, struct cifs_tcon *tcon,
915 const unsigned char *searchName, char **symlinkinfo,
916 const struct nls_table *nls_codepage)
917{
918#ifdef CONFIG_CIFS_DFS_UPCALL
919 int rc;
Paulo Alcantara1c780222018-11-14 16:24:03 -0200920 struct dfs_info3_param referral = {0};
Sachin Prabhu924e3fa2013-12-02 16:37:43 +0000921
Paulo Alcantara1c780222018-11-14 16:24:03 -0200922 rc = get_dfs_path(xid, tcon->ses, searchName, nls_codepage, &referral,
923 0);
Sachin Prabhu924e3fa2013-12-02 16:37:43 +0000924
Paulo Alcantara1c780222018-11-14 16:24:03 -0200925 if (!rc) {
926 *symlinkinfo = kstrndup(referral.node_name,
927 strlen(referral.node_name),
Sachin Prabhu924e3fa2013-12-02 16:37:43 +0000928 GFP_KERNEL);
Paulo Alcantara1c780222018-11-14 16:24:03 -0200929 free_dfs_info_param(&referral);
Sachin Prabhu924e3fa2013-12-02 16:37:43 +0000930 if (!*symlinkinfo)
931 rc = -ENOMEM;
Sachin Prabhu924e3fa2013-12-02 16:37:43 +0000932 }
933 return rc;
934#else /* No DFS support */
935 return -EREMOTE;
936#endif
937}
938
939static int
Pavel Shilovskyd244bf22013-08-14 19:25:22 +0400940cifs_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +1000941 struct cifs_sb_info *cifs_sb, const char *full_path,
942 char **target_path, bool is_reparse_point)
Pavel Shilovskyd244bf22013-08-14 19:25:22 +0400943{
944 int rc;
945 int oplock = 0;
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +0400946 struct cifs_fid fid;
947 struct cifs_open_parms oparms;
Pavel Shilovskyd244bf22013-08-14 19:25:22 +0400948
949 cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
950
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +1000951 if (is_reparse_point) {
952 cifs_dbg(VFS, "reparse points not handled for SMB1 symlinks\n");
953 return -EOPNOTSUPP;
954 }
955
Sachin Prabhu0ecdb4f2013-11-27 13:27:12 +0000956 /* Check for unix extensions */
957 if (cap_unix(tcon->ses)) {
958 rc = CIFSSMBUnixQuerySymLink(xid, tcon, full_path, target_path,
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +0900959 cifs_sb->local_nls,
960 cifs_remap(cifs_sb));
Sachin Prabhu924e3fa2013-12-02 16:37:43 +0000961 if (rc == -EREMOTE)
962 rc = cifs_unix_dfs_readlink(xid, tcon, full_path,
963 target_path,
964 cifs_sb->local_nls);
965
Sachin Prabhu0ecdb4f2013-11-27 13:27:12 +0000966 goto out;
967 }
968
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +0400969 oparms.tcon = tcon;
970 oparms.cifs_sb = cifs_sb;
971 oparms.desired_access = FILE_READ_ATTRIBUTES;
972 oparms.create_options = OPEN_REPARSE_POINT;
973 oparms.disposition = FILE_OPEN;
974 oparms.path = full_path;
975 oparms.fid = &fid;
976 oparms.reconnect = false;
977
978 rc = CIFS_open(xid, &oparms, &oplock, NULL);
Pavel Shilovskyd244bf22013-08-14 19:25:22 +0400979 if (rc)
Sachin Prabhu0ecdb4f2013-11-27 13:27:12 +0000980 goto out;
Pavel Shilovskyd244bf22013-08-14 19:25:22 +0400981
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +0400982 rc = CIFSSMBQuerySymLink(xid, tcon, fid.netfid, target_path,
Pavel Shilovskyd244bf22013-08-14 19:25:22 +0400983 cifs_sb->local_nls);
Sachin Prabhu0ecdb4f2013-11-27 13:27:12 +0000984 if (rc)
985 goto out_close;
Pavel Shilovskyd244bf22013-08-14 19:25:22 +0400986
987 convert_delimiter(*target_path, '/');
Sachin Prabhu0ecdb4f2013-11-27 13:27:12 +0000988out_close:
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +0400989 CIFSSMBClose(xid, tcon, fid.netfid);
Sachin Prabhu0ecdb4f2013-11-27 13:27:12 +0000990out:
991 if (!rc)
992 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
Pavel Shilovskyd244bf22013-08-14 19:25:22 +0400993 return rc;
994}
995
Pavel Shilovsky53ef1012013-09-05 16:11:28 +0400996static bool
997cifs_is_read_op(__u32 oplock)
998{
999 return oplock == OPLOCK_READ;
1000}
1001
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001002static unsigned int
1003cifs_wp_retry_size(struct inode *inode)
1004{
1005 return CIFS_SB(inode->i_sb)->wsize;
1006}
1007
Pavel Shilovsky52755802014-08-18 20:49:57 +04001008static bool
1009cifs_dir_needs_close(struct cifsFileInfo *cfile)
1010{
1011 return !cfile->srch_inf.endOfSearch && !cfile->invalidHandle;
1012}
1013
Sachin Prabhu62a6cfd2017-04-16 20:37:24 +01001014static bool
1015cifs_can_echo(struct TCP_Server_Info *server)
1016{
1017 if (server->tcpStatus == CifsGood)
1018 return true;
1019
1020 return false;
1021}
1022
Aurelien Aptelc847dcc2019-03-14 00:29:17 -05001023static int
1024cifs_make_node(unsigned int xid, struct inode *inode,
1025 struct dentry *dentry, struct cifs_tcon *tcon,
1026 char *full_path, umode_t mode, dev_t dev)
1027{
1028 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1029 struct inode *newinode = NULL;
1030 int rc = -EPERM;
1031 int create_options = CREATE_NOT_DIR | CREATE_OPTION_SPECIAL;
1032 FILE_ALL_INFO *buf = NULL;
1033 struct cifs_io_parms io_parms;
1034 __u32 oplock = 0;
1035 struct cifs_fid fid;
1036 struct cifs_open_parms oparms;
1037 unsigned int bytes_written;
1038 struct win_dev *pdev;
1039 struct kvec iov[2];
1040
1041 if (tcon->unix_ext) {
1042 /*
1043 * SMB1 Unix Extensions: requires server support but
1044 * works with all special files
1045 */
1046 struct cifs_unix_set_info_args args = {
1047 .mode = mode & ~current_umask(),
1048 .ctime = NO_CHANGE_64,
1049 .atime = NO_CHANGE_64,
1050 .mtime = NO_CHANGE_64,
1051 .device = dev,
1052 };
1053 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
1054 args.uid = current_fsuid();
1055 args.gid = current_fsgid();
1056 } else {
1057 args.uid = INVALID_UID; /* no change */
1058 args.gid = INVALID_GID; /* no change */
1059 }
1060 rc = CIFSSMBUnixSetPathInfo(xid, tcon, full_path, &args,
1061 cifs_sb->local_nls,
1062 cifs_remap(cifs_sb));
1063 if (rc)
1064 goto out;
1065
1066 rc = cifs_get_inode_info_unix(&newinode, full_path,
1067 inode->i_sb, xid);
1068
1069 if (rc == 0)
1070 d_instantiate(dentry, newinode);
1071 goto out;
1072 }
1073
1074 /*
1075 * SMB1 SFU emulation: should work with all servers, but only
1076 * support block and char device (no socket & fifo)
1077 */
1078 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL))
1079 goto out;
1080
1081 if (!S_ISCHR(mode) && !S_ISBLK(mode))
1082 goto out;
1083
1084 cifs_dbg(FYI, "sfu compat create special file\n");
1085
1086 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
1087 if (buf == NULL) {
1088 rc = -ENOMEM;
1089 goto out;
1090 }
1091
1092 if (backup_cred(cifs_sb))
1093 create_options |= CREATE_OPEN_BACKUP_INTENT;
1094
1095 oparms.tcon = tcon;
1096 oparms.cifs_sb = cifs_sb;
1097 oparms.desired_access = GENERIC_WRITE;
1098 oparms.create_options = create_options;
1099 oparms.disposition = FILE_CREATE;
1100 oparms.path = full_path;
1101 oparms.fid = &fid;
1102 oparms.reconnect = false;
1103
1104 if (tcon->ses->server->oplocks)
1105 oplock = REQ_OPLOCK;
1106 else
1107 oplock = 0;
1108 rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, buf);
1109 if (rc)
1110 goto out;
1111
1112 /*
1113 * BB Do not bother to decode buf since no local inode yet to put
1114 * timestamps in, but we can reuse it safely.
1115 */
1116
1117 pdev = (struct win_dev *)buf;
1118 io_parms.pid = current->tgid;
1119 io_parms.tcon = tcon;
1120 io_parms.offset = 0;
1121 io_parms.length = sizeof(struct win_dev);
1122 iov[1].iov_base = buf;
1123 iov[1].iov_len = sizeof(struct win_dev);
1124 if (S_ISCHR(mode)) {
1125 memcpy(pdev->type, "IntxCHR", 8);
1126 pdev->major = cpu_to_le64(MAJOR(dev));
1127 pdev->minor = cpu_to_le64(MINOR(dev));
1128 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
1129 &bytes_written, iov, 1);
1130 } else if (S_ISBLK(mode)) {
1131 memcpy(pdev->type, "IntxBLK", 8);
1132 pdev->major = cpu_to_le64(MAJOR(dev));
1133 pdev->minor = cpu_to_le64(MINOR(dev));
1134 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
1135 &bytes_written, iov, 1);
1136 }
1137 tcon->ses->server->ops->close(xid, tcon, &fid);
1138 d_drop(dentry);
1139
1140 /* FIXME: add code here to set EAs */
1141out:
1142 kfree(buf);
1143 return rc;
1144}
1145
1146
1147
Jeff Layton23db65f2012-05-15 12:20:51 -04001148struct smb_version_operations smb1_operations = {
Jeff Layton121b0462012-05-15 12:21:10 -04001149 .send_cancel = send_nt_cancel,
Pavel Shilovsky55157df2012-02-28 14:04:17 +03001150 .compare_fids = cifs_compare_fids,
Pavel Shilovsky082d0642012-05-17 12:18:21 +04001151 .setup_request = cifs_setup_request,
Pavel Shilovsky45740842012-06-01 14:26:18 +04001152 .setup_async_request = cifs_setup_async_request,
Pavel Shilovsky082d0642012-05-17 12:18:21 +04001153 .check_receive = cifs_check_receive,
Pavel Shilovsky45275782012-05-17 17:53:29 +04001154 .add_credits = cifs_add_credits,
1155 .set_credits = cifs_set_credits,
1156 .get_credits_field = cifs_get_credits_field,
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04001157 .get_credits = cifs_get_credits,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04001158 .wait_mtu_credits = cifs_wait_mtu_credits,
Pavel Shilovsky88257362012-05-23 14:01:59 +04001159 .get_next_mid = cifs_get_next_mid,
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001160 .read_data_offset = cifs_read_data_offset,
1161 .read_data_length = cifs_read_data_length,
1162 .map_error = map_smb_to_linux_error,
Pavel Shilovsky8aa26f32012-05-17 13:25:35 +04001163 .find_mid = cifs_find_mid,
1164 .check_message = checkSMB,
1165 .dump_detail = cifs_dump_detail,
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001166 .clear_stats = cifs_clear_stats,
1167 .print_stats = cifs_print_stats,
Pavel Shilovsky8aa26f32012-05-17 13:25:35 +04001168 .is_oplock_break = is_valid_oplock_break,
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00001169 .downgrade_oplock = cifs_downgrade_oplock,
Pavel Shilovsky316cf942012-05-23 14:31:03 +04001170 .check_trans2 = cifs_check_trans2,
Pavel Shilovsky286170a2012-05-25 10:43:58 +04001171 .need_neg = cifs_need_neg,
1172 .negotiate = cifs_negotiate,
Pavel Shilovsky24985c52012-09-18 16:20:28 -07001173 .negotiate_wsize = cifs_negotiate_wsize,
1174 .negotiate_rsize = cifs_negotiate_rsize,
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04001175 .sess_setup = CIFS_SessSetup,
1176 .logoff = CIFSSMBLogoff,
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04001177 .tree_connect = CIFSTCon,
1178 .tree_disconnect = CIFSSMBTDis,
Pavel Shilovskyb669f332012-05-27 20:21:53 +04001179 .get_dfs_refer = CIFSGetDFSRefer,
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04001180 .qfs_tcon = cifs_qfs_tcon,
Pavel Shilovsky68889f22012-05-25 14:40:22 +04001181 .is_path_accessible = cifs_is_path_accessible,
Sachin Prabhu62a6cfd2017-04-16 20:37:24 +01001182 .can_echo = cifs_can_echo,
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04001183 .query_path_info = cifs_query_path_info,
Pavel Shilovsky4ad65042012-09-18 16:20:26 -07001184 .query_file_info = cifs_query_file_info,
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04001185 .get_srv_inum = cifs_get_srv_inum,
Pavel Shilovskyd1433412012-09-18 16:20:31 -07001186 .set_path_size = CIFSSMBSetEOF,
1187 .set_file_size = CIFSSMBSetFileSize,
Pavel Shilovsky6bdf6db2012-09-18 16:20:32 -07001188 .set_file_info = smb_set_file_info,
Steve Frenchc7f508a2013-10-14 15:27:32 -05001189 .set_compression = cifs_set_compression,
Pavel Shilovskyf6d76172012-05-25 14:47:16 +04001190 .echo = CIFSSMBEcho,
Pavel Shilovskyf4367202012-03-17 11:41:12 +03001191 .mkdir = CIFSSMBMkDir,
1192 .mkdir_setinfo = cifs_mkdir_setinfo,
Pavel Shilovskyf958ca52012-07-10 16:14:18 +04001193 .rmdir = CIFSSMBRmDir,
Pavel Shilovskyed6875e2012-09-18 16:20:25 -07001194 .unlink = CIFSSMBDelFile,
1195 .rename_pending_delete = cifs_rename_pending_delete,
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07001196 .rename = CIFSSMBRename,
Steve Frenchd6e906f2012-09-18 16:20:31 -07001197 .create_hardlink = CIFSCreateHardLink,
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04001198 .query_symlink = cifs_query_symlink,
Pavel Shilovskyfb1214e2012-09-18 16:20:26 -07001199 .open = cifs_open_file,
1200 .set_fid = cifs_set_fid,
Pavel Shilovsky0ff78a22012-09-18 16:20:26 -07001201 .close = cifs_close_file,
Pavel Shilovsky1d8c4c02012-09-18 16:20:27 -07001202 .flush = cifs_flush_file,
Pavel Shilovskyfc9c5962012-09-18 16:20:28 -07001203 .async_readv = cifs_async_readv,
Pavel Shilovskyc9de5c82012-09-18 16:20:29 -07001204 .async_writev = cifs_async_writev,
Pavel Shilovskyf9c6e232012-09-18 16:20:29 -07001205 .sync_read = cifs_sync_read,
Pavel Shilovskyba9ad7252012-09-18 16:20:30 -07001206 .sync_write = cifs_sync_write,
Pavel Shilovsky92fc65a2012-09-18 16:20:32 -07001207 .query_dir_first = cifs_query_dir_first,
1208 .query_dir_next = cifs_query_dir_next,
1209 .close_dir = cifs_close_dir,
1210 .calc_smb_size = smbCalcSize,
Pavel Shilovsky95a3f2f2012-09-18 16:20:33 -07001211 .oplock_response = cifs_oplock_response,
Pavel Shilovsky76ec5e32012-09-18 16:20:33 -07001212 .queryfs = cifs_queryfs,
Pavel Shilovskyd39a4f72012-09-19 06:22:43 -07001213 .mand_lock = cifs_mand_lock,
1214 .mand_unlock_range = cifs_unlock_range,
1215 .push_mand_locks = cifs_push_mandatory_locks,
Sachin Prabhub5be1a12013-11-25 17:09:49 +00001216 .query_mf_symlink = cifs_query_mf_symlink,
Sachin Prabhucbb0aba2013-11-25 17:09:52 +00001217 .create_mf_symlink = cifs_create_mf_symlink,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001218 .is_read_op = cifs_is_read_op,
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001219 .wp_retry_size = cifs_wp_retry_size,
Pavel Shilovsky52755802014-08-18 20:49:57 +04001220 .dir_needs_close = cifs_dir_needs_close,
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301221 .select_sectype = cifs_select_sectype,
Steve Frenchd979f3b2014-02-01 23:27:18 -06001222#ifdef CONFIG_CIFS_XATTR
1223 .query_all_EAs = CIFSSMBQAllEAs,
1224 .set_EA = CIFSSMBSetEA,
1225#endif /* CIFS_XATTR */
Steve French83e3bc22014-02-02 23:31:47 -06001226#ifdef CONFIG_CIFS_ACL
1227 .get_acl = get_cifs_acl,
Steve French42eacf92014-02-10 14:08:16 -06001228 .get_acl_by_fid = get_cifs_acl_by_fid,
Steve French83e3bc22014-02-02 23:31:47 -06001229 .set_acl = set_cifs_acl,
1230#endif /* CIFS_ACL */
Aurelien Aptelc847dcc2019-03-14 00:29:17 -05001231 .make_node = cifs_make_node,
Jeff Layton23db65f2012-05-15 12:20:51 -04001232};
1233
1234struct smb_version_values smb1_values = {
1235 .version_string = SMB1_VERSION_STRING,
Pavel Shilovsky106dc532012-02-28 14:23:34 +03001236 .large_lock_type = LOCKING_ANDX_LARGE_FILES,
1237 .exclusive_lock_type = 0,
1238 .shared_lock_type = LOCKING_ANDX_SHARED_LOCK,
1239 .unlock_lock_type = 0,
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11001240 .header_preamble_size = 4,
Pavel Shilovsky1887f602012-05-17 12:45:31 +04001241 .header_size = sizeof(struct smb_hdr),
1242 .max_header_size = MAX_CIFS_HDR_SIZE,
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001243 .read_rsp_size = sizeof(READ_RSP),
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04001244 .lock_cmd = cpu_to_le16(SMB_COM_LOCKING_ANDX),
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04001245 .cap_unix = CAP_UNIX,
1246 .cap_nt_find = CAP_NT_SMBS | CAP_NT_FIND,
1247 .cap_large_files = CAP_LARGE_FILES,
Jeff Layton502858822013-06-27 12:45:00 -04001248 .signing_enabled = SECMODE_SIGN_ENABLED,
1249 .signing_required = SECMODE_SIGN_REQUIRED,
Jeff Layton23db65f2012-05-15 12:20:51 -04001250};