1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * SMB1 (CIFS) version specific operations
4 *
5 * Copyright (c) 2012, Jeff Layton <jlayton@redhat.com>
6 */
7
8 #include <linux/pagemap.h>
9 #include <linux/vfs.h>
10 #include <uapi/linux/magic.h>
11 #include "cifsglob.h"
12 #include "cifsproto.h"
13 #include "cifs_debug.h"
14 #include "cifspdu.h"
15 #include "cifs_unicode.h"
16 #include "fs_context.h"
17 #include "nterr.h"
18 #include "smberr.h"
19 #include "reparse.h"
20
21 /*
22 * An NT cancel request header looks just like the original request except:
23 *
24 * The Command is SMB_COM_NT_CANCEL
25 * The WordCount is zeroed out
26 * The ByteCount is zeroed out
27 *
28 * This function mangles an existing request buffer into a
29 * SMB_COM_NT_CANCEL request and then sends it.
30 */
31 static int
send_nt_cancel(struct TCP_Server_Info * server,struct smb_rqst * rqst,struct mid_q_entry * mid)32 send_nt_cancel(struct TCP_Server_Info *server, struct smb_rqst *rqst,
33 struct mid_q_entry *mid)
34 {
35 int rc = 0;
36 struct smb_hdr *in_buf = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
37
38 /* -4 for RFC1001 length and +2 for BCC field */
39 in_buf->smb_buf_length = cpu_to_be32(sizeof(struct smb_hdr) - 4 + 2);
40 in_buf->Command = SMB_COM_NT_CANCEL;
41 in_buf->WordCount = 0;
42 put_bcc(0, in_buf);
43
44 cifs_server_lock(server);
45 rc = cifs_sign_smb(in_buf, server, &mid->sequence_number);
46 if (rc) {
47 cifs_server_unlock(server);
48 return rc;
49 }
50
51 /*
52 * The response to this call was already factored into the sequence
53 * number when the call went out, so we must adjust it back downward
54 * after signing here.
55 */
56 --server->sequence_number;
57 rc = smb_send(server, in_buf, be32_to_cpu(in_buf->smb_buf_length));
58 if (rc < 0)
59 server->sequence_number--;
60
61 cifs_server_unlock(server);
62
63 cifs_dbg(FYI, "issued NT_CANCEL for mid %u, rc = %d\n",
64 get_mid(in_buf), rc);
65
66 return rc;
67 }
68
69 static bool
cifs_compare_fids(struct cifsFileInfo * ob1,struct cifsFileInfo * ob2)70 cifs_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
71 {
72 return ob1->fid.netfid == ob2->fid.netfid;
73 }
74
75 static unsigned int
cifs_read_data_offset(char * buf)76 cifs_read_data_offset(char *buf)
77 {
78 READ_RSP *rsp = (READ_RSP *)buf;
79 return le16_to_cpu(rsp->DataOffset);
80 }
81
82 static unsigned int
cifs_read_data_length(char * buf,bool in_remaining)83 cifs_read_data_length(char *buf, bool in_remaining)
84 {
85 READ_RSP *rsp = (READ_RSP *)buf;
86 /* It's a bug reading remaining data for SMB1 packets */
87 WARN_ON(in_remaining);
88 return (le16_to_cpu(rsp->DataLengthHigh) << 16) +
89 le16_to_cpu(rsp->DataLength);
90 }
91
92 static struct mid_q_entry *
cifs_find_mid(struct TCP_Server_Info * server,char * buffer)93 cifs_find_mid(struct TCP_Server_Info *server, char *buffer)
94 {
95 struct smb_hdr *buf = (struct smb_hdr *)buffer;
96 struct mid_q_entry *mid;
97
98 spin_lock(&server->mid_queue_lock);
99 list_for_each_entry(mid, &server->pending_mid_q, qhead) {
100 if (compare_mid(mid->mid, buf) &&
101 mid->mid_state == MID_REQUEST_SUBMITTED &&
102 le16_to_cpu(mid->command) == buf->Command) {
103 kref_get(&mid->refcount);
104 spin_unlock(&server->mid_queue_lock);
105 return mid;
106 }
107 }
108 spin_unlock(&server->mid_queue_lock);
109 return NULL;
110 }
111
112 static void
cifs_add_credits(struct TCP_Server_Info * server,struct cifs_credits * credits,const int optype)113 cifs_add_credits(struct TCP_Server_Info *server,
114 struct cifs_credits *credits, const int optype)
115 {
116 spin_lock(&server->req_lock);
117 server->credits += credits->value;
118 server->in_flight--;
119 spin_unlock(&server->req_lock);
120 wake_up(&server->request_q);
121 }
122
123 static void
cifs_set_credits(struct TCP_Server_Info * server,const int val)124 cifs_set_credits(struct TCP_Server_Info *server, const int val)
125 {
126 spin_lock(&server->req_lock);
127 server->credits = val;
128 server->oplocks = val > 1 ? enable_oplocks : false;
129 spin_unlock(&server->req_lock);
130 }
131
132 static int *
cifs_get_credits_field(struct TCP_Server_Info * server,const int optype)133 cifs_get_credits_field(struct TCP_Server_Info *server, const int optype)
134 {
135 return &server->credits;
136 }
137
138 static unsigned int
cifs_get_credits(struct mid_q_entry * mid)139 cifs_get_credits(struct mid_q_entry *mid)
140 {
141 return 1;
142 }
143
144 /*
145 * Find a free multiplex id (SMB mid). Otherwise there could be
146 * mid collisions which might cause problems, demultiplexing the
147 * wrong response to this request. Multiplex ids could collide if
148 * one of a series requests takes much longer than the others, or
149 * if a very large number of long lived requests (byte range
150 * locks or FindNotify requests) are pending. No more than
151 * 64K-1 requests can be outstanding at one time. If no
152 * mids are available, return zero. A future optimization
153 * could make the combination of mids and uid the key we use
154 * to demultiplex on (rather than mid alone).
155 * In addition to the above check, the cifs demultiplex
156 * code already used the command code as a secondary
157 * check of the frame and if signing is negotiated the
158 * response would be discarded if the mid were the same
159 * but the signature was wrong. Since the mid is not put in the
160 * pending queue until later (when it is about to be dispatched)
161 * we do have to limit the number of outstanding requests
162 * to somewhat less than 64K-1 although it is hard to imagine
163 * so many threads being in the vfs at one time.
164 */
165 static __u64
cifs_get_next_mid(struct TCP_Server_Info * server)166 cifs_get_next_mid(struct TCP_Server_Info *server)
167 {
168 __u64 mid = 0;
169 __u16 last_mid, cur_mid;
170 bool collision, reconnect = false;
171
172 spin_lock(&server->mid_counter_lock);
173 /* mid is 16 bit only for CIFS/SMB */
174 cur_mid = (__u16)((server->current_mid) & 0xffff);
175 /* we do not want to loop forever */
176 last_mid = cur_mid;
177 cur_mid++;
178 /* avoid 0xFFFF MID */
179 if (cur_mid == 0xffff)
180 cur_mid++;
181
182 /*
183 * This nested loop looks more expensive than it is.
184 * In practice the list of pending requests is short,
185 * fewer than 50, and the mids are likely to be unique
186 * on the first pass through the loop unless some request
187 * takes longer than the 64 thousand requests before it
188 * (and it would also have to have been a request that
189 * did not time out).
190 */
191 while (cur_mid != last_mid) {
192 struct mid_q_entry *mid_entry;
193 unsigned int num_mids;
194
195 collision = false;
196 if (cur_mid == 0)
197 cur_mid++;
198
199 num_mids = 0;
200 spin_lock(&server->mid_queue_lock);
201 list_for_each_entry(mid_entry, &server->pending_mid_q, qhead) {
202 ++num_mids;
203 if (mid_entry->mid == cur_mid &&
204 mid_entry->mid_state == MID_REQUEST_SUBMITTED) {
205 /* This mid is in use, try a different one */
206 collision = true;
207 break;
208 }
209 }
210 spin_unlock(&server->mid_queue_lock);
211
212 /*
213 * if we have more than 32k mids in the list, then something
214 * is very wrong. Possibly a local user is trying to DoS the
215 * box by issuing long-running calls and SIGKILL'ing them. If
216 * we get to 2^16 mids then we're in big trouble as this
217 * function could loop forever.
218 *
219 * Go ahead and assign out the mid in this situation, but force
220 * an eventual reconnect to clean out the pending_mid_q.
221 */
222 if (num_mids > 32768)
223 reconnect = true;
224
225 if (!collision) {
226 mid = (__u64)cur_mid;
227 server->current_mid = mid;
228 break;
229 }
230 cur_mid++;
231 }
232 spin_unlock(&server->mid_counter_lock);
233
234 if (reconnect) {
235 cifs_signal_cifsd_for_reconnect(server, false);
236 }
237
238 return mid;
239 }
240
241 /*
242 return codes:
243 0 not a transact2, or all data present
244 >0 transact2 with that much data missing
245 -EINVAL invalid transact2
246 */
247 static int
check2ndT2(char * buf)248 check2ndT2(char *buf)
249 {
250 struct smb_hdr *pSMB = (struct smb_hdr *)buf;
251 struct smb_t2_rsp *pSMBt;
252 int remaining;
253 __u16 total_data_size, data_in_this_rsp;
254
255 if (pSMB->Command != SMB_COM_TRANSACTION2)
256 return 0;
257
258 /* check for plausible wct, bcc and t2 data and parm sizes */
259 /* check for parm and data offset going beyond end of smb */
260 if (pSMB->WordCount != 10) { /* coalesce_t2 depends on this */
261 cifs_dbg(FYI, "Invalid transact2 word count\n");
262 return -EINVAL;
263 }
264
265 pSMBt = (struct smb_t2_rsp *)pSMB;
266
267 total_data_size = get_unaligned_le16(&pSMBt->t2_rsp.TotalDataCount);
268 data_in_this_rsp = get_unaligned_le16(&pSMBt->t2_rsp.DataCount);
269
270 if (total_data_size == data_in_this_rsp)
271 return 0;
272 else if (total_data_size < data_in_this_rsp) {
273 cifs_dbg(FYI, "total data %d smaller than data in frame %d\n",
274 total_data_size, data_in_this_rsp);
275 return -EINVAL;
276 }
277
278 remaining = total_data_size - data_in_this_rsp;
279
280 cifs_dbg(FYI, "missing %d bytes from transact2, check next response\n",
281 remaining);
282 if (total_data_size > CIFSMaxBufSize) {
283 cifs_dbg(VFS, "TotalDataSize %d is over maximum buffer %d\n",
284 total_data_size, CIFSMaxBufSize);
285 return -EINVAL;
286 }
287 return remaining;
288 }
289
290 static int
coalesce_t2(char * second_buf,struct smb_hdr * target_hdr)291 coalesce_t2(char *second_buf, struct smb_hdr *target_hdr)
292 {
293 struct smb_t2_rsp *pSMBs = (struct smb_t2_rsp *)second_buf;
294 struct smb_t2_rsp *pSMBt = (struct smb_t2_rsp *)target_hdr;
295 char *data_area_of_tgt;
296 char *data_area_of_src;
297 int remaining;
298 unsigned int byte_count, total_in_tgt;
299 __u16 tgt_total_cnt, src_total_cnt, total_in_src;
300
301 src_total_cnt = get_unaligned_le16(&pSMBs->t2_rsp.TotalDataCount);
302 tgt_total_cnt = get_unaligned_le16(&pSMBt->t2_rsp.TotalDataCount);
303
304 if (tgt_total_cnt != src_total_cnt)
305 cifs_dbg(FYI, "total data count of primary and secondary t2 differ source=%hu target=%hu\n",
306 src_total_cnt, tgt_total_cnt);
307
308 total_in_tgt = get_unaligned_le16(&pSMBt->t2_rsp.DataCount);
309
310 remaining = tgt_total_cnt - total_in_tgt;
311
312 if (remaining < 0) {
313 cifs_dbg(FYI, "Server sent too much data. tgt_total_cnt=%hu total_in_tgt=%u\n",
314 tgt_total_cnt, total_in_tgt);
315 return -EPROTO;
316 }
317
318 if (remaining == 0) {
319 /* nothing to do, ignore */
320 cifs_dbg(FYI, "no more data remains\n");
321 return 0;
322 }
323
324 total_in_src = get_unaligned_le16(&pSMBs->t2_rsp.DataCount);
325 if (remaining < total_in_src)
326 cifs_dbg(FYI, "transact2 2nd response contains too much data\n");
327
328 /* find end of first SMB data area */
329 data_area_of_tgt = (char *)&pSMBt->hdr.Protocol +
330 get_unaligned_le16(&pSMBt->t2_rsp.DataOffset);
331
332 /* validate target area */
333 data_area_of_src = (char *)&pSMBs->hdr.Protocol +
334 get_unaligned_le16(&pSMBs->t2_rsp.DataOffset);
335
336 data_area_of_tgt += total_in_tgt;
337
338 total_in_tgt += total_in_src;
339 /* is the result too big for the field? */
340 if (total_in_tgt > USHRT_MAX) {
341 cifs_dbg(FYI, "coalesced DataCount too large (%u)\n",
342 total_in_tgt);
343 return -EPROTO;
344 }
345 put_unaligned_le16(total_in_tgt, &pSMBt->t2_rsp.DataCount);
346
347 /* fix up the BCC */
348 byte_count = get_bcc(target_hdr);
349 byte_count += total_in_src;
350 /* is the result too big for the field? */
351 if (byte_count > USHRT_MAX) {
352 cifs_dbg(FYI, "coalesced BCC too large (%u)\n", byte_count);
353 return -EPROTO;
354 }
355 put_bcc(byte_count, target_hdr);
356
357 byte_count = be32_to_cpu(target_hdr->smb_buf_length);
358 byte_count += total_in_src;
359 /* don't allow buffer to overflow */
360 if (byte_count > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
361 cifs_dbg(FYI, "coalesced BCC exceeds buffer size (%u)\n",
362 byte_count);
363 return -ENOBUFS;
364 }
365 target_hdr->smb_buf_length = cpu_to_be32(byte_count);
366
367 /* copy second buffer into end of first buffer */
368 memcpy(data_area_of_tgt, data_area_of_src, total_in_src);
369
370 if (remaining != total_in_src) {
371 /* more responses to go */
372 cifs_dbg(FYI, "waiting for more secondary responses\n");
373 return 1;
374 }
375
376 /* we are done */
377 cifs_dbg(FYI, "found the last secondary response\n");
378 return 0;
379 }
380
381 static void
cifs_downgrade_oplock(struct TCP_Server_Info * server,struct cifsInodeInfo * cinode,__u32 oplock,__u16 epoch,bool * purge_cache)382 cifs_downgrade_oplock(struct TCP_Server_Info *server,
383 struct cifsInodeInfo *cinode, __u32 oplock,
384 __u16 epoch, bool *purge_cache)
385 {
386 cifs_set_oplock_level(cinode, oplock);
387 }
388
389 static bool
cifs_check_trans2(struct mid_q_entry * mid,struct TCP_Server_Info * server,char * buf,int malformed)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
cifs_need_neg(struct TCP_Server_Info * server)421 cifs_need_neg(struct TCP_Server_Info *server)
422 {
423 return server->maxBuf == 0;
424 }
425
426 static int
cifs_negotiate(const unsigned int xid,struct cifs_ses * ses,struct TCP_Server_Info * server)427 cifs_negotiate(const unsigned int xid,
428 struct cifs_ses *ses,
429 struct TCP_Server_Info *server)
430 {
431 int rc;
432 rc = CIFSSMBNegotiate(xid, ses, server);
433 return rc;
434 }
435
436 static unsigned int
smb1_negotiate_wsize(struct cifs_tcon * tcon,struct smb3_fs_context * ctx)437 smb1_negotiate_wsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
438 {
439 __u64 unix_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
440 struct TCP_Server_Info *server = tcon->ses->server;
441 unsigned int wsize;
442
443 /* start with specified wsize, or default */
444 if (ctx->got_wsize)
445 wsize = ctx->vol_wsize;
446 else if (tcon->unix_ext && (unix_cap & CIFS_UNIX_LARGE_WRITE_CAP))
447 wsize = CIFS_DEFAULT_IOSIZE;
448 else
449 wsize = CIFS_DEFAULT_NON_POSIX_WSIZE;
450
451 /* can server support 24-bit write sizes? (via UNIX extensions) */
452 if (!tcon->unix_ext || !(unix_cap & CIFS_UNIX_LARGE_WRITE_CAP))
453 wsize = min_t(unsigned int, wsize, CIFS_MAX_RFC1002_WSIZE);
454
455 /*
456 * no CAP_LARGE_WRITE_X or is signing enabled without CAP_UNIX set?
457 * Limit it to max buffer offered by the server, minus the size of the
458 * WRITEX header, not including the 4 byte RFC1001 length.
459 */
460 if (!(server->capabilities & CAP_LARGE_WRITE_X) ||
461 (!(server->capabilities & CAP_UNIX) && server->sign))
462 wsize = min_t(unsigned int, wsize,
463 server->maxBuf - sizeof(WRITE_REQ) + 4);
464
465 /* hard limit of CIFS_MAX_WSIZE */
466 wsize = min_t(unsigned int, wsize, CIFS_MAX_WSIZE);
467
468 return wsize;
469 }
470
471 static unsigned int
smb1_negotiate_rsize(struct cifs_tcon * tcon,struct smb3_fs_context * ctx)472 smb1_negotiate_rsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
473 {
474 __u64 unix_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
475 struct TCP_Server_Info *server = tcon->ses->server;
476 unsigned int rsize, defsize;
477
478 /*
479 * Set default value...
480 *
481 * HACK alert! Ancient servers have very small buffers. Even though
482 * MS-CIFS indicates that servers are only limited by the client's
483 * bufsize for reads, testing against win98se shows that it throws
484 * INVALID_PARAMETER errors if you try to request too large a read.
485 * OS/2 just sends back short reads.
486 *
487 * If the server doesn't advertise CAP_LARGE_READ_X, then assume that
488 * it can't handle a read request larger than its MaxBufferSize either.
489 */
490 if (tcon->unix_ext && (unix_cap & CIFS_UNIX_LARGE_READ_CAP))
491 defsize = CIFS_DEFAULT_IOSIZE;
492 else if (server->capabilities & CAP_LARGE_READ_X)
493 defsize = CIFS_DEFAULT_NON_POSIX_RSIZE;
494 else
495 defsize = server->maxBuf - sizeof(READ_RSP);
496
497 rsize = ctx->got_rsize ? ctx->vol_rsize : defsize;
498
499 /*
500 * no CAP_LARGE_READ_X? Then MS-CIFS states that we must limit this to
501 * the client's MaxBufferSize.
502 */
503 if (!(server->capabilities & CAP_LARGE_READ_X))
504 rsize = min_t(unsigned int, CIFSMaxBufSize, rsize);
505
506 /* hard limit of CIFS_MAX_RSIZE */
507 rsize = min_t(unsigned int, rsize, CIFS_MAX_RSIZE);
508
509 return rsize;
510 }
511
512 static void
cifs_qfs_tcon(const unsigned int xid,struct cifs_tcon * tcon,struct cifs_sb_info * cifs_sb)513 cifs_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon,
514 struct cifs_sb_info *cifs_sb)
515 {
516 CIFSSMBQFSDeviceInfo(xid, tcon);
517 CIFSSMBQFSAttributeInfo(xid, tcon);
518 }
519
520 static int
cifs_is_path_accessible(const unsigned int xid,struct cifs_tcon * tcon,struct cifs_sb_info * cifs_sb,const char * full_path)521 cifs_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
522 struct cifs_sb_info *cifs_sb, const char *full_path)
523 {
524 int rc;
525 FILE_ALL_INFO *file_info;
526
527 file_info = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
528 if (file_info == NULL)
529 return -ENOMEM;
530
531 rc = CIFSSMBQPathInfo(xid, tcon, full_path, file_info,
532 0 /* not legacy */, cifs_sb->local_nls,
533 cifs_remap(cifs_sb));
534
535 if (rc == -EOPNOTSUPP || rc == -EINVAL)
536 rc = SMBQueryInformation(xid, tcon, full_path, file_info,
537 cifs_sb->local_nls, cifs_remap(cifs_sb));
538 kfree(file_info);
539 return rc;
540 }
541
cifs_query_path_info(const unsigned int xid,struct cifs_tcon * tcon,struct cifs_sb_info * cifs_sb,const char * full_path,struct cifs_open_info_data * data)542 static int cifs_query_path_info(const unsigned int xid,
543 struct cifs_tcon *tcon,
544 struct cifs_sb_info *cifs_sb,
545 const char *full_path,
546 struct cifs_open_info_data *data)
547 {
548 int rc = -EOPNOTSUPP;
549 FILE_ALL_INFO fi = {};
550 struct cifs_search_info search_info = {};
551 bool non_unicode_wildcard = false;
552
553 data->reparse_point = false;
554 data->adjust_tz = false;
555
556 /*
557 * First try CIFSSMBQPathInfo() function which returns more info
558 * (NumberOfLinks) than CIFSFindFirst() fallback function.
559 * Some servers like Win9x do not support SMB_QUERY_FILE_ALL_INFO over
560 * TRANS2_QUERY_PATH_INFORMATION, but supports it with filehandle over
561 * TRANS2_QUERY_FILE_INFORMATION (function CIFSSMBQFileInfo(). But SMB
562 * Open command on non-NT servers works only for files, does not work
563 * for directories. And moreover Win9x SMB server returns bogus data in
564 * SMB_QUERY_FILE_ALL_INFO Attributes field. So for non-NT servers,
565 * do not even use CIFSSMBQPathInfo() or CIFSSMBQFileInfo() function.
566 */
567 if (tcon->ses->capabilities & CAP_NT_SMBS)
568 rc = CIFSSMBQPathInfo(xid, tcon, full_path, &fi, 0 /* not legacy */,
569 cifs_sb->local_nls, cifs_remap(cifs_sb));
570
571 /*
572 * Non-UNICODE variant of fallback functions below expands wildcards,
573 * so they cannot be used for querying paths with wildcard characters.
574 */
575 if (rc && !(tcon->ses->capabilities & CAP_UNICODE) && strpbrk(full_path, "*?\"><"))
576 non_unicode_wildcard = true;
577
578 /*
579 * Then fallback to CIFSFindFirst() which works also with non-NT servers
580 * but does not does not provide NumberOfLinks.
581 */
582 if ((rc == -EOPNOTSUPP || rc == -EINVAL) &&
583 !non_unicode_wildcard) {
584 if (!(tcon->ses->capabilities & tcon->ses->server->vals->cap_nt_find))
585 search_info.info_level = SMB_FIND_FILE_INFO_STANDARD;
586 else
587 search_info.info_level = SMB_FIND_FILE_FULL_DIRECTORY_INFO;
588 rc = CIFSFindFirst(xid, tcon, full_path, cifs_sb, NULL,
589 CIFS_SEARCH_CLOSE_ALWAYS | CIFS_SEARCH_CLOSE_AT_END,
590 &search_info, false);
591 if (rc == 0) {
592 if (!(tcon->ses->capabilities & tcon->ses->server->vals->cap_nt_find)) {
593 FIND_FILE_STANDARD_INFO *di;
594 int offset = tcon->ses->server->timeAdj;
595
596 di = (FIND_FILE_STANDARD_INFO *)search_info.srch_entries_start;
597 fi.CreationTime = cpu_to_le64(cifs_UnixTimeToNT(cnvrtDosUnixTm(
598 di->CreationDate, di->CreationTime, offset)));
599 fi.LastAccessTime = cpu_to_le64(cifs_UnixTimeToNT(cnvrtDosUnixTm(
600 di->LastAccessDate, di->LastAccessTime, offset)));
601 fi.LastWriteTime = cpu_to_le64(cifs_UnixTimeToNT(cnvrtDosUnixTm(
602 di->LastWriteDate, di->LastWriteTime, offset)));
603 fi.ChangeTime = fi.LastWriteTime;
604 fi.Attributes = cpu_to_le32(le16_to_cpu(di->Attributes));
605 fi.AllocationSize = cpu_to_le64(le32_to_cpu(di->AllocationSize));
606 fi.EndOfFile = cpu_to_le64(le32_to_cpu(di->DataSize));
607 } else {
608 FILE_FULL_DIRECTORY_INFO *di;
609
610 di = (FILE_FULL_DIRECTORY_INFO *)search_info.srch_entries_start;
611 fi.CreationTime = di->CreationTime;
612 fi.LastAccessTime = di->LastAccessTime;
613 fi.LastWriteTime = di->LastWriteTime;
614 fi.ChangeTime = di->ChangeTime;
615 fi.Attributes = di->ExtFileAttributes;
616 fi.AllocationSize = di->AllocationSize;
617 fi.EndOfFile = di->EndOfFile;
618 fi.EASize = di->EaSize;
619 }
620 fi.NumberOfLinks = cpu_to_le32(1);
621 fi.DeletePending = 0;
622 fi.Directory = !!(le32_to_cpu(fi.Attributes) & ATTR_DIRECTORY);
623 cifs_buf_release(search_info.ntwrk_buf_start);
624 } else if (!full_path[0]) {
625 /*
626 * CIFSFindFirst() does not work on root path if the
627 * root path was exported on the server from the top
628 * level path (drive letter).
629 */
630 rc = -EOPNOTSUPP;
631 }
632 }
633
634 /*
635 * If everything failed then fallback to the legacy SMB command
636 * SMB_COM_QUERY_INFORMATION which works with all servers, but
637 * provide just few information.
638 */
639 if ((rc == -EOPNOTSUPP || rc == -EINVAL) && !non_unicode_wildcard) {
640 rc = SMBQueryInformation(xid, tcon, full_path, &fi, cifs_sb->local_nls,
641 cifs_remap(cifs_sb));
642 data->adjust_tz = true;
643 } else if ((rc == -EOPNOTSUPP || rc == -EINVAL) && non_unicode_wildcard) {
644 /* Path with non-UNICODE wildcard character cannot exist. */
645 rc = -ENOENT;
646 }
647
648 if (!rc) {
649 move_cifs_info_to_smb2(&data->fi, &fi);
650 data->reparse_point = le32_to_cpu(fi.Attributes) & ATTR_REPARSE;
651 }
652
653 #ifdef CONFIG_CIFS_XATTR
654 /*
655 * For non-symlink WSL reparse points it is required to fetch
656 * EA $LXMOD which contains in its S_DT part the mandatory file type.
657 */
658 if (!rc && data->reparse_point) {
659 struct smb2_file_full_ea_info *ea;
660 u32 next = 0;
661
662 ea = (struct smb2_file_full_ea_info *)data->wsl.eas;
663 do {
664 ea = (void *)((u8 *)ea + next);
665 next = le32_to_cpu(ea->next_entry_offset);
666 } while (next);
667 if (le16_to_cpu(ea->ea_value_length)) {
668 ea->next_entry_offset = cpu_to_le32(ALIGN(sizeof(*ea) +
669 ea->ea_name_length + 1 +
670 le16_to_cpu(ea->ea_value_length), 4));
671 ea = (void *)((u8 *)ea + le32_to_cpu(ea->next_entry_offset));
672 }
673
674 rc = CIFSSMBQAllEAs(xid, tcon, full_path, SMB2_WSL_XATTR_MODE,
675 &ea->ea_data[SMB2_WSL_XATTR_NAME_LEN + 1],
676 SMB2_WSL_XATTR_MODE_SIZE, cifs_sb);
677 if (rc == SMB2_WSL_XATTR_MODE_SIZE) {
678 ea->next_entry_offset = cpu_to_le32(0);
679 ea->flags = 0;
680 ea->ea_name_length = SMB2_WSL_XATTR_NAME_LEN;
681 ea->ea_value_length = cpu_to_le16(SMB2_WSL_XATTR_MODE_SIZE);
682 memcpy(&ea->ea_data[0], SMB2_WSL_XATTR_MODE, SMB2_WSL_XATTR_NAME_LEN + 1);
683 data->wsl.eas_len += ALIGN(sizeof(*ea) + SMB2_WSL_XATTR_NAME_LEN + 1 +
684 SMB2_WSL_XATTR_MODE_SIZE, 4);
685 rc = 0;
686 } else if (rc >= 0) {
687 /* It is an error if EA $LXMOD has wrong size. */
688 rc = -EINVAL;
689 } else {
690 /*
691 * In all other cases ignore error if fetching
692 * of EA $LXMOD failed. It is needed only for
693 * non-symlink WSL reparse points and wsl_to_fattr()
694 * handle the case when EA is missing.
695 */
696 rc = 0;
697 }
698 }
699
700 /*
701 * For WSL CHR and BLK reparse points it is required to fetch
702 * EA $LXDEV which contains major and minor device numbers.
703 */
704 if (!rc && data->reparse_point) {
705 struct smb2_file_full_ea_info *ea;
706 u32 next = 0;
707
708 ea = (struct smb2_file_full_ea_info *)data->wsl.eas;
709 do {
710 ea = (void *)((u8 *)ea + next);
711 next = le32_to_cpu(ea->next_entry_offset);
712 } while (next);
713 if (le16_to_cpu(ea->ea_value_length)) {
714 ea->next_entry_offset = cpu_to_le32(ALIGN(sizeof(*ea) +
715 ea->ea_name_length + 1 +
716 le16_to_cpu(ea->ea_value_length), 4));
717 ea = (void *)((u8 *)ea + le32_to_cpu(ea->next_entry_offset));
718 }
719
720 rc = CIFSSMBQAllEAs(xid, tcon, full_path, SMB2_WSL_XATTR_DEV,
721 &ea->ea_data[SMB2_WSL_XATTR_NAME_LEN + 1],
722 SMB2_WSL_XATTR_DEV_SIZE, cifs_sb);
723 if (rc == SMB2_WSL_XATTR_DEV_SIZE) {
724 ea->next_entry_offset = cpu_to_le32(0);
725 ea->flags = 0;
726 ea->ea_name_length = SMB2_WSL_XATTR_NAME_LEN;
727 ea->ea_value_length = cpu_to_le16(SMB2_WSL_XATTR_DEV_SIZE);
728 memcpy(&ea->ea_data[0], SMB2_WSL_XATTR_DEV, SMB2_WSL_XATTR_NAME_LEN + 1);
729 data->wsl.eas_len += ALIGN(sizeof(*ea) + SMB2_WSL_XATTR_NAME_LEN + 1 +
730 SMB2_WSL_XATTR_MODE_SIZE, 4);
731 rc = 0;
732 } else if (rc >= 0) {
733 /* It is an error if EA $LXDEV has wrong size. */
734 rc = -EINVAL;
735 } else {
736 /*
737 * In all other cases ignore error if fetching
738 * of EA $LXDEV failed. It is needed only for
739 * WSL CHR and BLK reparse points and wsl_to_fattr()
740 * handle the case when EA is missing.
741 */
742 rc = 0;
743 }
744 }
745 #endif
746
747 return rc;
748 }
749
cifs_get_srv_inum(const unsigned int xid,struct cifs_tcon * tcon,struct cifs_sb_info * cifs_sb,const char * full_path,u64 * uniqueid,struct cifs_open_info_data * unused)750 static int cifs_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
751 struct cifs_sb_info *cifs_sb, const char *full_path,
752 u64 *uniqueid, struct cifs_open_info_data *unused)
753 {
754 /*
755 * We can not use the IndexNumber field by default from Windows or
756 * Samba (in ALL_INFO buf) but we can request it explicitly. The SNIA
757 * CIFS spec claims that this value is unique within the scope of a
758 * share, and the windows docs hint that it's actually unique
759 * per-machine.
760 *
761 * There may be higher info levels that work but are there Windows
762 * server or network appliances for which IndexNumber field is not
763 * guaranteed unique?
764 *
765 * CIFSGetSrvInodeNumber() uses SMB_QUERY_FILE_INTERNAL_INFO
766 * which is SMB PASSTHROUGH level therefore check for capability.
767 * Note that this function can be called with tcon == NULL.
768 */
769 if (tcon && !(tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU))
770 return -EOPNOTSUPP;
771 return CIFSGetSrvInodeNumber(xid, tcon, full_path, uniqueid,
772 cifs_sb->local_nls,
773 cifs_remap(cifs_sb));
774 }
775
cifs_query_file_info(const unsigned int xid,struct cifs_tcon * tcon,struct cifsFileInfo * cfile,struct cifs_open_info_data * data)776 static int cifs_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
777 struct cifsFileInfo *cfile, struct cifs_open_info_data *data)
778 {
779 int rc;
780 FILE_ALL_INFO fi = {};
781
782 /*
783 * CIFSSMBQFileInfo() for non-NT servers returns bogus data in
784 * Attributes fields. So do not use this command for non-NT servers.
785 */
786 if (!(tcon->ses->capabilities & CAP_NT_SMBS))
787 return -EOPNOTSUPP;
788
789 if (cfile->symlink_target) {
790 data->symlink_target = kstrdup(cfile->symlink_target, GFP_KERNEL);
791 if (!data->symlink_target)
792 return -ENOMEM;
793 }
794
795 rc = CIFSSMBQFileInfo(xid, tcon, cfile->fid.netfid, &fi);
796 if (!rc)
797 move_cifs_info_to_smb2(&data->fi, &fi);
798 return rc;
799 }
800
801 static void
cifs_clear_stats(struct cifs_tcon * tcon)802 cifs_clear_stats(struct cifs_tcon *tcon)
803 {
804 atomic_set(&tcon->stats.cifs_stats.num_writes, 0);
805 atomic_set(&tcon->stats.cifs_stats.num_reads, 0);
806 atomic_set(&tcon->stats.cifs_stats.num_flushes, 0);
807 atomic_set(&tcon->stats.cifs_stats.num_oplock_brks, 0);
808 atomic_set(&tcon->stats.cifs_stats.num_opens, 0);
809 atomic_set(&tcon->stats.cifs_stats.num_posixopens, 0);
810 atomic_set(&tcon->stats.cifs_stats.num_posixmkdirs, 0);
811 atomic_set(&tcon->stats.cifs_stats.num_closes, 0);
812 atomic_set(&tcon->stats.cifs_stats.num_deletes, 0);
813 atomic_set(&tcon->stats.cifs_stats.num_mkdirs, 0);
814 atomic_set(&tcon->stats.cifs_stats.num_rmdirs, 0);
815 atomic_set(&tcon->stats.cifs_stats.num_renames, 0);
816 atomic_set(&tcon->stats.cifs_stats.num_t2renames, 0);
817 atomic_set(&tcon->stats.cifs_stats.num_ffirst, 0);
818 atomic_set(&tcon->stats.cifs_stats.num_fnext, 0);
819 atomic_set(&tcon->stats.cifs_stats.num_fclose, 0);
820 atomic_set(&tcon->stats.cifs_stats.num_hardlinks, 0);
821 atomic_set(&tcon->stats.cifs_stats.num_symlinks, 0);
822 atomic_set(&tcon->stats.cifs_stats.num_locks, 0);
823 atomic_set(&tcon->stats.cifs_stats.num_acl_get, 0);
824 atomic_set(&tcon->stats.cifs_stats.num_acl_set, 0);
825 }
826
827 static void
cifs_print_stats(struct seq_file * m,struct cifs_tcon * tcon)828 cifs_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
829 {
830 seq_printf(m, " Oplocks breaks: %d",
831 atomic_read(&tcon->stats.cifs_stats.num_oplock_brks));
832 seq_printf(m, "\nReads: %d Bytes: %llu",
833 atomic_read(&tcon->stats.cifs_stats.num_reads),
834 (long long)(tcon->bytes_read));
835 seq_printf(m, "\nWrites: %d Bytes: %llu",
836 atomic_read(&tcon->stats.cifs_stats.num_writes),
837 (long long)(tcon->bytes_written));
838 seq_printf(m, "\nFlushes: %d",
839 atomic_read(&tcon->stats.cifs_stats.num_flushes));
840 seq_printf(m, "\nLocks: %d HardLinks: %d Symlinks: %d",
841 atomic_read(&tcon->stats.cifs_stats.num_locks),
842 atomic_read(&tcon->stats.cifs_stats.num_hardlinks),
843 atomic_read(&tcon->stats.cifs_stats.num_symlinks));
844 seq_printf(m, "\nOpens: %d Closes: %d Deletes: %d",
845 atomic_read(&tcon->stats.cifs_stats.num_opens),
846 atomic_read(&tcon->stats.cifs_stats.num_closes),
847 atomic_read(&tcon->stats.cifs_stats.num_deletes));
848 seq_printf(m, "\nPosix Opens: %d Posix Mkdirs: %d",
849 atomic_read(&tcon->stats.cifs_stats.num_posixopens),
850 atomic_read(&tcon->stats.cifs_stats.num_posixmkdirs));
851 seq_printf(m, "\nMkdirs: %d Rmdirs: %d",
852 atomic_read(&tcon->stats.cifs_stats.num_mkdirs),
853 atomic_read(&tcon->stats.cifs_stats.num_rmdirs));
854 seq_printf(m, "\nRenames: %d T2 Renames %d",
855 atomic_read(&tcon->stats.cifs_stats.num_renames),
856 atomic_read(&tcon->stats.cifs_stats.num_t2renames));
857 seq_printf(m, "\nFindFirst: %d FNext %d FClose %d",
858 atomic_read(&tcon->stats.cifs_stats.num_ffirst),
859 atomic_read(&tcon->stats.cifs_stats.num_fnext),
860 atomic_read(&tcon->stats.cifs_stats.num_fclose));
861 }
862
863 static void
cifs_mkdir_setinfo(struct inode * inode,const char * full_path,struct cifs_sb_info * cifs_sb,struct cifs_tcon * tcon,const unsigned int xid)864 cifs_mkdir_setinfo(struct inode *inode, const char *full_path,
865 struct cifs_sb_info *cifs_sb, struct cifs_tcon *tcon,
866 const unsigned int xid)
867 {
868 FILE_BASIC_INFO info;
869 struct cifsInodeInfo *cifsInode;
870 u32 dosattrs;
871 int rc;
872
873 memset(&info, 0, sizeof(info));
874 cifsInode = CIFS_I(inode);
875 dosattrs = cifsInode->cifsAttrs|ATTR_READONLY;
876 info.Attributes = cpu_to_le32(dosattrs);
877 rc = CIFSSMBSetPathInfo(xid, tcon, full_path, &info, cifs_sb->local_nls,
878 cifs_sb);
879 if (rc == -EOPNOTSUPP || rc == -EINVAL)
880 rc = SMBSetInformation(xid, tcon, full_path,
881 info.Attributes,
882 0 /* do not change write time */,
883 cifs_sb->local_nls, cifs_sb);
884 if (rc == 0)
885 cifsInode->cifsAttrs = dosattrs;
886 }
887
cifs_open_file(const unsigned int xid,struct cifs_open_parms * oparms,__u32 * oplock,void * buf)888 static int cifs_open_file(const unsigned int xid, struct cifs_open_parms *oparms, __u32 *oplock,
889 void *buf)
890 {
891 struct cifs_open_info_data *data = buf;
892 FILE_ALL_INFO fi = {};
893 int rc;
894
895 if (!(oparms->tcon->ses->capabilities & CAP_NT_SMBS))
896 rc = SMBLegacyOpen(xid, oparms->tcon, oparms->path,
897 oparms->disposition,
898 oparms->desired_access,
899 oparms->create_options,
900 &oparms->fid->netfid, oplock, &fi,
901 oparms->cifs_sb->local_nls,
902 cifs_remap(oparms->cifs_sb));
903 else
904 rc = CIFS_open(xid, oparms, oplock, &fi);
905
906 if (!rc && data)
907 move_cifs_info_to_smb2(&data->fi, &fi);
908
909 return rc;
910 }
911
912 static void
cifs_set_fid(struct cifsFileInfo * cfile,struct cifs_fid * fid,__u32 oplock)913 cifs_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
914 {
915 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
916 cfile->fid.netfid = fid->netfid;
917 cifs_set_oplock_level(cinode, oplock);
918 cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
919 }
920
921 static int
cifs_close_file(const unsigned int xid,struct cifs_tcon * tcon,struct cifs_fid * fid)922 cifs_close_file(const unsigned int xid, struct cifs_tcon *tcon,
923 struct cifs_fid *fid)
924 {
925 return CIFSSMBClose(xid, tcon, fid->netfid);
926 }
927
928 static int
cifs_flush_file(const unsigned int xid,struct cifs_tcon * tcon,struct cifs_fid * fid)929 cifs_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
930 struct cifs_fid *fid)
931 {
932 return CIFSSMBFlush(xid, tcon, fid->netfid);
933 }
934
935 static int
cifs_sync_read(const unsigned int xid,struct cifs_fid * pfid,struct cifs_io_parms * parms,unsigned int * bytes_read,char ** buf,int * buf_type)936 cifs_sync_read(const unsigned int xid, struct cifs_fid *pfid,
937 struct cifs_io_parms *parms, unsigned int *bytes_read,
938 char **buf, int *buf_type)
939 {
940 parms->netfid = pfid->netfid;
941 return CIFSSMBRead(xid, parms, bytes_read, buf, buf_type);
942 }
943
944 static int
cifs_sync_write(const unsigned int xid,struct cifs_fid * pfid,struct cifs_io_parms * parms,unsigned int * written,struct kvec * iov,unsigned long nr_segs)945 cifs_sync_write(const unsigned int xid, struct cifs_fid *pfid,
946 struct cifs_io_parms *parms, unsigned int *written,
947 struct kvec *iov, unsigned long nr_segs)
948 {
949
950 parms->netfid = pfid->netfid;
951 return CIFSSMBWrite2(xid, parms, written, iov, nr_segs);
952 }
953
954 static int
smb_set_file_info(struct inode * inode,const char * full_path,FILE_BASIC_INFO * buf,const unsigned int xid)955 smb_set_file_info(struct inode *inode, const char *full_path,
956 FILE_BASIC_INFO *buf, const unsigned int xid)
957 {
958 int oplock = 0;
959 int rc;
960 __u32 netpid;
961 struct cifs_fid fid;
962 struct cifs_open_parms oparms;
963 struct cifsFileInfo *open_file;
964 FILE_BASIC_INFO new_buf;
965 struct cifs_open_info_data query_data;
966 __le64 write_time = buf->LastWriteTime;
967 struct cifsInodeInfo *cinode = CIFS_I(inode);
968 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
969 struct tcon_link *tlink = NULL;
970 struct cifs_tcon *tcon;
971
972 /* if the file is already open for write, just use that fileid */
973 open_file = find_writable_file(cinode, FIND_WR_FSUID_ONLY);
974
975 if (open_file) {
976 fid.netfid = open_file->fid.netfid;
977 netpid = open_file->pid;
978 tcon = tlink_tcon(open_file->tlink);
979 } else {
980 tlink = cifs_sb_tlink(cifs_sb);
981 if (IS_ERR(tlink)) {
982 rc = PTR_ERR(tlink);
983 tlink = NULL;
984 goto out;
985 }
986 tcon = tlink_tcon(tlink);
987 }
988
989 /*
990 * Non-NT servers interprets zero time value in SMB_SET_FILE_BASIC_INFO
991 * over TRANS2_SET_FILE_INFORMATION as a valid time value. NT servers
992 * interprets zero time value as do not change existing value on server.
993 * API of ->set_file_info() callback expects that zero time value has
994 * the NT meaning - do not change. Therefore if server is non-NT and
995 * some time values in "buf" are zero, then fetch missing time values.
996 */
997 if (!(tcon->ses->capabilities & CAP_NT_SMBS) &&
998 (!buf->CreationTime || !buf->LastAccessTime ||
999 !buf->LastWriteTime || !buf->ChangeTime)) {
1000 rc = cifs_query_path_info(xid, tcon, cifs_sb, full_path, &query_data);
1001 if (rc) {
1002 if (open_file) {
1003 cifsFileInfo_put(open_file);
1004 open_file = NULL;
1005 }
1006 goto out;
1007 }
1008 /*
1009 * Original write_time from buf->LastWriteTime is preserved
1010 * as SMBSetInformation() interprets zero as do not change.
1011 */
1012 new_buf = *buf;
1013 buf = &new_buf;
1014 if (!buf->CreationTime)
1015 buf->CreationTime = query_data.fi.CreationTime;
1016 if (!buf->LastAccessTime)
1017 buf->LastAccessTime = query_data.fi.LastAccessTime;
1018 if (!buf->LastWriteTime)
1019 buf->LastWriteTime = query_data.fi.LastWriteTime;
1020 if (!buf->ChangeTime)
1021 buf->ChangeTime = query_data.fi.ChangeTime;
1022 }
1023
1024 if (open_file)
1025 goto set_via_filehandle;
1026
1027 rc = CIFSSMBSetPathInfo(xid, tcon, full_path, buf, cifs_sb->local_nls,
1028 cifs_sb);
1029 if (rc == 0) {
1030 cinode->cifsAttrs = le32_to_cpu(buf->Attributes);
1031 goto out;
1032 } else if (rc != -EOPNOTSUPP && rc != -EINVAL) {
1033 goto out;
1034 }
1035
1036 oparms = (struct cifs_open_parms) {
1037 .tcon = tcon,
1038 .cifs_sb = cifs_sb,
1039 .desired_access = SYNCHRONIZE | FILE_WRITE_ATTRIBUTES,
1040 .create_options = cifs_create_options(cifs_sb, 0),
1041 .disposition = FILE_OPEN,
1042 .path = full_path,
1043 .fid = &fid,
1044 };
1045
1046 if (S_ISDIR(inode->i_mode) && !(tcon->ses->capabilities & CAP_NT_SMBS)) {
1047 /* Opening directory path is not possible on non-NT servers. */
1048 rc = -EOPNOTSUPP;
1049 } else {
1050 /*
1051 * Use cifs_open_file() instead of CIFS_open() as the
1052 * cifs_open_file() selects the correct function which
1053 * works also on non-NT servers.
1054 */
1055 rc = cifs_open_file(xid, &oparms, &oplock, NULL);
1056 /*
1057 * Opening path for writing on non-NT servers is not
1058 * possible when the read-only attribute is already set.
1059 * Non-NT server in this case returns -EACCES. For those
1060 * servers the only possible way how to clear the read-only
1061 * bit is via SMB_COM_SETATTR command.
1062 */
1063 if (rc == -EACCES &&
1064 (cinode->cifsAttrs & ATTR_READONLY) &&
1065 le32_to_cpu(buf->Attributes) != 0 && /* 0 = do not change attrs */
1066 !(le32_to_cpu(buf->Attributes) & ATTR_READONLY) &&
1067 !(tcon->ses->capabilities & CAP_NT_SMBS))
1068 rc = -EOPNOTSUPP;
1069 }
1070
1071 /* Fallback to SMB_COM_SETATTR command when absolutely needed. */
1072 if (rc == -EOPNOTSUPP) {
1073 cifs_dbg(FYI, "calling SetInformation since SetPathInfo for attrs/times not supported by this server\n");
1074 rc = SMBSetInformation(xid, tcon, full_path,
1075 buf->Attributes != 0 ? buf->Attributes : cpu_to_le32(cinode->cifsAttrs),
1076 write_time,
1077 cifs_sb->local_nls, cifs_sb);
1078 if (rc == 0)
1079 cinode->cifsAttrs = le32_to_cpu(buf->Attributes);
1080 else
1081 rc = -EACCES;
1082 goto out;
1083 }
1084
1085 if (rc != 0) {
1086 if (rc == -EIO)
1087 rc = -EINVAL;
1088 goto out;
1089 }
1090
1091 netpid = current->tgid;
1092 cifs_dbg(FYI, "calling SetFileInfo since SetPathInfo for attrs/times not supported by this server\n");
1093
1094 set_via_filehandle:
1095 rc = CIFSSMBSetFileInfo(xid, tcon, buf, fid.netfid, netpid);
1096 if (!rc)
1097 cinode->cifsAttrs = le32_to_cpu(buf->Attributes);
1098
1099 if (open_file == NULL)
1100 CIFSSMBClose(xid, tcon, fid.netfid);
1101 else
1102 cifsFileInfo_put(open_file);
1103
1104 /*
1105 * Setting the read-only bit is not honored on non-NT servers when done
1106 * via open-semantics. So for setting it, use SMB_COM_SETATTR command.
1107 * This command works only after the file is closed, so use it only when
1108 * operation was called without the filehandle.
1109 */
1110 if (open_file == NULL &&
1111 !(tcon->ses->capabilities & CAP_NT_SMBS) &&
1112 le32_to_cpu(buf->Attributes) & ATTR_READONLY) {
1113 SMBSetInformation(xid, tcon, full_path,
1114 buf->Attributes,
1115 0 /* do not change write time */,
1116 cifs_sb->local_nls, cifs_sb);
1117 }
1118 out:
1119 if (tlink != NULL)
1120 cifs_put_tlink(tlink);
1121 return rc;
1122 }
1123
1124 static int
cifs_set_compression(const unsigned int xid,struct cifs_tcon * tcon,struct cifsFileInfo * cfile)1125 cifs_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1126 struct cifsFileInfo *cfile)
1127 {
1128 return CIFSSMB_set_compression(xid, tcon, cfile->fid.netfid);
1129 }
1130
1131 static int
cifs_query_dir_first(const unsigned int xid,struct cifs_tcon * tcon,const char * path,struct cifs_sb_info * cifs_sb,struct cifs_fid * fid,__u16 search_flags,struct cifs_search_info * srch_inf)1132 cifs_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
1133 const char *path, struct cifs_sb_info *cifs_sb,
1134 struct cifs_fid *fid, __u16 search_flags,
1135 struct cifs_search_info *srch_inf)
1136 {
1137 int rc;
1138
1139 rc = CIFSFindFirst(xid, tcon, path, cifs_sb,
1140 &fid->netfid, search_flags, srch_inf, true);
1141 if (rc)
1142 cifs_dbg(FYI, "find first failed=%d\n", rc);
1143 return rc;
1144 }
1145
1146 static int
cifs_query_dir_next(const unsigned int xid,struct cifs_tcon * tcon,struct cifs_fid * fid,__u16 search_flags,struct cifs_search_info * srch_inf)1147 cifs_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
1148 struct cifs_fid *fid, __u16 search_flags,
1149 struct cifs_search_info *srch_inf)
1150 {
1151 return CIFSFindNext(xid, tcon, fid->netfid, search_flags, srch_inf);
1152 }
1153
1154 static int
cifs_close_dir(const unsigned int xid,struct cifs_tcon * tcon,struct cifs_fid * fid)1155 cifs_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
1156 struct cifs_fid *fid)
1157 {
1158 return CIFSFindClose(xid, tcon, fid->netfid);
1159 }
1160
1161 static int
cifs_oplock_response(struct cifs_tcon * tcon,__u64 persistent_fid,__u64 volatile_fid,__u16 net_fid,struct cifsInodeInfo * cinode)1162 cifs_oplock_response(struct cifs_tcon *tcon, __u64 persistent_fid,
1163 __u64 volatile_fid, __u16 net_fid, struct cifsInodeInfo *cinode)
1164 {
1165 return CIFSSMBLock(0, tcon, net_fid, current->tgid, 0, 0, 0, 0,
1166 LOCKING_ANDX_OPLOCK_RELEASE, false, CIFS_CACHE_READ(cinode) ? 1 : 0);
1167 }
1168
1169 static int
cifs_queryfs(const unsigned int xid,struct cifs_tcon * tcon,const char * path,struct cifs_sb_info * cifs_sb,struct kstatfs * buf)1170 cifs_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
1171 const char *path, struct cifs_sb_info *cifs_sb, struct kstatfs *buf)
1172 {
1173 int rc = -EOPNOTSUPP;
1174
1175 buf->f_type = CIFS_SUPER_MAGIC;
1176
1177 /*
1178 * We could add a second check for a QFS Unix capability bit
1179 */
1180 if ((tcon->ses->capabilities & CAP_UNIX) &&
1181 (CIFS_POSIX_EXTENSIONS & le64_to_cpu(tcon->fsUnixInfo.Capability)))
1182 rc = CIFSSMBQFSPosixInfo(xid, tcon, buf);
1183
1184 /*
1185 * Only need to call the old QFSInfo if failed on newer one,
1186 * e.g. by OS/2.
1187 **/
1188 if (rc && (tcon->ses->capabilities & CAP_NT_SMBS))
1189 rc = CIFSSMBQFSInfo(xid, tcon, buf);
1190
1191 /*
1192 * Some old Windows servers also do not support level 103, retry with
1193 * older level one if old server failed the previous call or we
1194 * bypassed it because we detected that this was an older LANMAN sess
1195 */
1196 if (rc)
1197 rc = SMBOldQFSInfo(xid, tcon, buf);
1198 return rc;
1199 }
1200
1201 static int
cifs_mand_lock(const unsigned int xid,struct cifsFileInfo * cfile,__u64 offset,__u64 length,__u32 type,int lock,int unlock,bool wait)1202 cifs_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
1203 __u64 length, __u32 type, int lock, int unlock, bool wait)
1204 {
1205 return CIFSSMBLock(xid, tlink_tcon(cfile->tlink), cfile->fid.netfid,
1206 current->tgid, length, offset, unlock, lock,
1207 (__u8)type, wait, 0);
1208 }
1209
1210 static int
cifs_unix_dfs_readlink(const unsigned int xid,struct cifs_tcon * tcon,const unsigned char * searchName,char ** symlinkinfo,const struct nls_table * nls_codepage)1211 cifs_unix_dfs_readlink(const unsigned int xid, struct cifs_tcon *tcon,
1212 const unsigned char *searchName, char **symlinkinfo,
1213 const struct nls_table *nls_codepage)
1214 {
1215 #ifdef CONFIG_CIFS_DFS_UPCALL
1216 int rc;
1217 struct dfs_info3_param referral = {0};
1218
1219 rc = get_dfs_path(xid, tcon->ses, searchName, nls_codepage, &referral,
1220 0);
1221
1222 if (!rc) {
1223 *symlinkinfo = kstrdup(referral.node_name, GFP_KERNEL);
1224 free_dfs_info_param(&referral);
1225 if (!*symlinkinfo)
1226 rc = -ENOMEM;
1227 }
1228 return rc;
1229 #else /* No DFS support */
1230 return -EREMOTE;
1231 #endif
1232 }
1233
cifs_query_symlink(const unsigned int xid,struct cifs_tcon * tcon,struct cifs_sb_info * cifs_sb,const char * full_path,char ** target_path)1234 static int cifs_query_symlink(const unsigned int xid,
1235 struct cifs_tcon *tcon,
1236 struct cifs_sb_info *cifs_sb,
1237 const char *full_path,
1238 char **target_path)
1239 {
1240 int rc;
1241
1242 cifs_tcon_dbg(FYI, "%s: path=%s\n", __func__, full_path);
1243
1244 if (!cap_unix(tcon->ses))
1245 return -EOPNOTSUPP;
1246
1247 rc = CIFSSMBUnixQuerySymLink(xid, tcon, full_path, target_path,
1248 cifs_sb->local_nls, cifs_remap(cifs_sb));
1249 if (rc == -EREMOTE)
1250 rc = cifs_unix_dfs_readlink(xid, tcon, full_path,
1251 target_path, cifs_sb->local_nls);
1252 return rc;
1253 }
1254
cifs_get_reparse_point_buffer(const struct kvec * rsp_iov,u32 * plen)1255 static struct reparse_data_buffer *cifs_get_reparse_point_buffer(const struct kvec *rsp_iov,
1256 u32 *plen)
1257 {
1258 TRANSACT_IOCTL_RSP *io = rsp_iov->iov_base;
1259 *plen = le16_to_cpu(io->ByteCount);
1260 return (struct reparse_data_buffer *)((__u8 *)&io->hdr.Protocol +
1261 le32_to_cpu(io->DataOffset));
1262 }
1263
1264 static bool
cifs_is_read_op(__u32 oplock)1265 cifs_is_read_op(__u32 oplock)
1266 {
1267 return oplock == OPLOCK_READ;
1268 }
1269
1270 static unsigned int
cifs_wp_retry_size(struct inode * inode)1271 cifs_wp_retry_size(struct inode *inode)
1272 {
1273 return CIFS_SB(inode->i_sb)->ctx->wsize;
1274 }
1275
1276 static bool
cifs_dir_needs_close(struct cifsFileInfo * cfile)1277 cifs_dir_needs_close(struct cifsFileInfo *cfile)
1278 {
1279 return !cfile->srch_inf.endOfSearch && !cfile->invalidHandle;
1280 }
1281
1282 static bool
cifs_can_echo(struct TCP_Server_Info * server)1283 cifs_can_echo(struct TCP_Server_Info *server)
1284 {
1285 if (server->tcpStatus == CifsGood)
1286 return true;
1287
1288 return false;
1289 }
1290
1291 static int
cifs_make_node(unsigned int xid,struct inode * inode,struct dentry * dentry,struct cifs_tcon * tcon,const char * full_path,umode_t mode,dev_t dev)1292 cifs_make_node(unsigned int xid, struct inode *inode,
1293 struct dentry *dentry, struct cifs_tcon *tcon,
1294 const char *full_path, umode_t mode, dev_t dev)
1295 {
1296 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1297 struct inode *newinode = NULL;
1298 int rc;
1299
1300 if (tcon->unix_ext) {
1301 /*
1302 * SMB1 Unix Extensions: requires server support but
1303 * works with all special files
1304 */
1305 struct cifs_unix_set_info_args args = {
1306 .mode = mode & ~current_umask(),
1307 .ctime = NO_CHANGE_64,
1308 .atime = NO_CHANGE_64,
1309 .mtime = NO_CHANGE_64,
1310 .device = dev,
1311 };
1312 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
1313 args.uid = current_fsuid();
1314 args.gid = current_fsgid();
1315 } else {
1316 args.uid = INVALID_UID; /* no change */
1317 args.gid = INVALID_GID; /* no change */
1318 }
1319 rc = CIFSSMBUnixSetPathInfo(xid, tcon, full_path, &args,
1320 cifs_sb->local_nls,
1321 cifs_remap(cifs_sb));
1322 if (rc)
1323 return rc;
1324
1325 rc = cifs_get_inode_info_unix(&newinode, full_path,
1326 inode->i_sb, xid);
1327
1328 if (rc == 0)
1329 d_instantiate(dentry, newinode);
1330 return rc;
1331 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
1332 /*
1333 * Check if mounted with mount parm 'sfu' mount parm.
1334 * SFU emulation should work with all servers
1335 * and was used by default in earlier versions of Windows.
1336 */
1337 return cifs_sfu_make_node(xid, inode, dentry, tcon,
1338 full_path, mode, dev);
1339 } else if (CIFS_REPARSE_SUPPORT(tcon)) {
1340 /*
1341 * mknod via reparse points requires server support for
1342 * storing reparse points, which is available since
1343 * Windows 2000, but was not widely used until release
1344 * of Windows Server 2012 by the Windows NFS server.
1345 */
1346 return mknod_reparse(xid, inode, dentry, tcon,
1347 full_path, mode, dev);
1348 } else {
1349 return -EOPNOTSUPP;
1350 }
1351 }
1352
1353 static bool
cifs_is_network_name_deleted(char * buf,struct TCP_Server_Info * server)1354 cifs_is_network_name_deleted(char *buf, struct TCP_Server_Info *server)
1355 {
1356 struct smb_hdr *shdr = (struct smb_hdr *)buf;
1357 struct TCP_Server_Info *pserver;
1358 struct cifs_ses *ses;
1359 struct cifs_tcon *tcon;
1360
1361 if (shdr->Flags2 & SMBFLG2_ERR_STATUS) {
1362 if (shdr->Status.CifsError != cpu_to_le32(NT_STATUS_NETWORK_NAME_DELETED))
1363 return false;
1364 } else {
1365 if (shdr->Status.DosError.ErrorClass != ERRSRV ||
1366 shdr->Status.DosError.Error != cpu_to_le16(ERRinvtid))
1367 return false;
1368 }
1369
1370 /* If server is a channel, select the primary channel */
1371 pserver = SERVER_IS_CHAN(server) ? server->primary_server : server;
1372
1373 spin_lock(&cifs_tcp_ses_lock);
1374 list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) {
1375 if (cifs_ses_exiting(ses))
1376 continue;
1377 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
1378 if (tcon->tid == shdr->Tid) {
1379 spin_lock(&tcon->tc_lock);
1380 tcon->need_reconnect = true;
1381 spin_unlock(&tcon->tc_lock);
1382 spin_unlock(&cifs_tcp_ses_lock);
1383 pr_warn_once("Server share %s deleted.\n",
1384 tcon->tree_name);
1385 return true;
1386 }
1387 }
1388 }
1389 spin_unlock(&cifs_tcp_ses_lock);
1390
1391 return false;
1392 }
1393
1394 struct smb_version_operations smb1_operations = {
1395 .send_cancel = send_nt_cancel,
1396 .compare_fids = cifs_compare_fids,
1397 .setup_request = cifs_setup_request,
1398 .setup_async_request = cifs_setup_async_request,
1399 .check_receive = cifs_check_receive,
1400 .add_credits = cifs_add_credits,
1401 .set_credits = cifs_set_credits,
1402 .get_credits_field = cifs_get_credits_field,
1403 .get_credits = cifs_get_credits,
1404 .wait_mtu_credits = cifs_wait_mtu_credits,
1405 .get_next_mid = cifs_get_next_mid,
1406 .read_data_offset = cifs_read_data_offset,
1407 .read_data_length = cifs_read_data_length,
1408 .map_error = map_smb_to_linux_error,
1409 .find_mid = cifs_find_mid,
1410 .check_message = checkSMB,
1411 .dump_detail = cifs_dump_detail,
1412 .clear_stats = cifs_clear_stats,
1413 .print_stats = cifs_print_stats,
1414 .is_oplock_break = is_valid_oplock_break,
1415 .downgrade_oplock = cifs_downgrade_oplock,
1416 .check_trans2 = cifs_check_trans2,
1417 .need_neg = cifs_need_neg,
1418 .negotiate = cifs_negotiate,
1419 .negotiate_wsize = smb1_negotiate_wsize,
1420 .negotiate_rsize = smb1_negotiate_rsize,
1421 .sess_setup = CIFS_SessSetup,
1422 .logoff = CIFSSMBLogoff,
1423 .tree_connect = CIFSTCon,
1424 .tree_disconnect = CIFSSMBTDis,
1425 .get_dfs_refer = CIFSGetDFSRefer,
1426 .qfs_tcon = cifs_qfs_tcon,
1427 .is_path_accessible = cifs_is_path_accessible,
1428 .can_echo = cifs_can_echo,
1429 .query_path_info = cifs_query_path_info,
1430 .query_reparse_point = cifs_query_reparse_point,
1431 .query_file_info = cifs_query_file_info,
1432 .get_srv_inum = cifs_get_srv_inum,
1433 .set_path_size = CIFSSMBSetEOF,
1434 .set_file_size = CIFSSMBSetFileSize,
1435 .set_file_info = smb_set_file_info,
1436 .set_compression = cifs_set_compression,
1437 .echo = CIFSSMBEcho,
1438 .mkdir = CIFSSMBMkDir,
1439 .mkdir_setinfo = cifs_mkdir_setinfo,
1440 .rmdir = CIFSSMBRmDir,
1441 .unlink = CIFSSMBDelFile,
1442 .rename_pending_delete = cifs_rename_pending_delete,
1443 .rename = CIFSSMBRename,
1444 .create_hardlink = CIFSCreateHardLink,
1445 .query_symlink = cifs_query_symlink,
1446 .get_reparse_point_buffer = cifs_get_reparse_point_buffer,
1447 .create_reparse_inode = cifs_create_reparse_inode,
1448 .open = cifs_open_file,
1449 .set_fid = cifs_set_fid,
1450 .close = cifs_close_file,
1451 .flush = cifs_flush_file,
1452 .async_readv = cifs_async_readv,
1453 .async_writev = cifs_async_writev,
1454 .sync_read = cifs_sync_read,
1455 .sync_write = cifs_sync_write,
1456 .query_dir_first = cifs_query_dir_first,
1457 .query_dir_next = cifs_query_dir_next,
1458 .close_dir = cifs_close_dir,
1459 .calc_smb_size = smbCalcSize,
1460 .oplock_response = cifs_oplock_response,
1461 .queryfs = cifs_queryfs,
1462 .mand_lock = cifs_mand_lock,
1463 .mand_unlock_range = cifs_unlock_range,
1464 .push_mand_locks = cifs_push_mandatory_locks,
1465 .query_mf_symlink = cifs_query_mf_symlink,
1466 .create_mf_symlink = cifs_create_mf_symlink,
1467 .is_read_op = cifs_is_read_op,
1468 .wp_retry_size = cifs_wp_retry_size,
1469 .dir_needs_close = cifs_dir_needs_close,
1470 .select_sectype = cifs_select_sectype,
1471 #ifdef CONFIG_CIFS_XATTR
1472 .query_all_EAs = CIFSSMBQAllEAs,
1473 .set_EA = CIFSSMBSetEA,
1474 #endif /* CIFS_XATTR */
1475 .get_acl = get_cifs_acl,
1476 .get_acl_by_fid = get_cifs_acl_by_fid,
1477 .set_acl = set_cifs_acl,
1478 .make_node = cifs_make_node,
1479 .is_network_name_deleted = cifs_is_network_name_deleted,
1480 };
1481
1482 struct smb_version_values smb1_values = {
1483 .version_string = SMB1_VERSION_STRING,
1484 .protocol_id = SMB10_PROT_ID,
1485 .large_lock_type = LOCKING_ANDX_LARGE_FILES,
1486 .exclusive_lock_type = 0,
1487 .shared_lock_type = LOCKING_ANDX_SHARED_LOCK,
1488 .unlock_lock_type = 0,
1489 .header_preamble_size = 4,
1490 .header_size = sizeof(struct smb_hdr),
1491 .max_header_size = MAX_CIFS_HDR_SIZE,
1492 .read_rsp_size = sizeof(READ_RSP),
1493 .lock_cmd = cpu_to_le16(SMB_COM_LOCKING_ANDX),
1494 .cap_unix = CAP_UNIX,
1495 .cap_nt_find = CAP_NT_SMBS | CAP_NT_FIND,
1496 .cap_large_files = CAP_LARGE_FILES,
1497 .cap_unicode = CAP_UNICODE,
1498 .signing_enabled = SECMODE_SIGN_ENABLED,
1499 .signing_required = SECMODE_SIGN_REQUIRED,
1500 };
1501