1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include <linux/pagemap.h>
21#include <linux/vfs.h>
22#include "cifsglob.h"
23#include "cifsproto.h"
24#include "cifs_debug.h"
25#include "cifspdu.h"
26#include "cifs_unicode.h"
27
28
29
30
31
32
33
34
35
36
37
38static int
39send_nt_cancel(struct TCP_Server_Info *server, struct smb_rqst *rqst,
40 struct mid_q_entry *mid)
41{
42 int rc = 0;
43 struct smb_hdr *in_buf = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
44
45
46 in_buf->smb_buf_length = cpu_to_be32(sizeof(struct smb_hdr) - 4 + 2);
47 in_buf->Command = SMB_COM_NT_CANCEL;
48 in_buf->WordCount = 0;
49 put_bcc(0, in_buf);
50
51 mutex_lock(&server->srv_mutex);
52 rc = cifs_sign_smb(in_buf, server, &mid->sequence_number);
53 if (rc) {
54 mutex_unlock(&server->srv_mutex);
55 return rc;
56 }
57
58
59
60
61
62
63 --server->sequence_number;
64 rc = smb_send(server, in_buf, be32_to_cpu(in_buf->smb_buf_length));
65 if (rc < 0)
66 server->sequence_number--;
67
68 mutex_unlock(&server->srv_mutex);
69
70 cifs_dbg(FYI, "issued NT_CANCEL for mid %u, rc = %d\n",
71 get_mid(in_buf), rc);
72
73 return rc;
74}
75
76static bool
77cifs_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
78{
79 return ob1->fid.netfid == ob2->fid.netfid;
80}
81
82static unsigned int
83cifs_read_data_offset(char *buf)
84{
85 READ_RSP *rsp = (READ_RSP *)buf;
86 return le16_to_cpu(rsp->DataOffset);
87}
88
89static unsigned int
90cifs_read_data_length(char *buf, bool in_remaining)
91{
92 READ_RSP *rsp = (READ_RSP *)buf;
93
94 WARN_ON(in_remaining);
95 return (le16_to_cpu(rsp->DataLengthHigh) << 16) +
96 le16_to_cpu(rsp->DataLength);
97}
98
99static struct mid_q_entry *
100cifs_find_mid(struct TCP_Server_Info *server, char *buffer)
101{
102 struct smb_hdr *buf = (struct smb_hdr *)buffer;
103 struct mid_q_entry *mid;
104
105 spin_lock(&GlobalMid_Lock);
106 list_for_each_entry(mid, &server->pending_mid_q, qhead) {
107 if (compare_mid(mid->mid, buf) &&
108 mid->mid_state == MID_REQUEST_SUBMITTED &&
109 le16_to_cpu(mid->command) == buf->Command) {
110 kref_get(&mid->refcount);
111 spin_unlock(&GlobalMid_Lock);
112 return mid;
113 }
114 }
115 spin_unlock(&GlobalMid_Lock);
116 return NULL;
117}
118
119static void
120cifs_add_credits(struct TCP_Server_Info *server,
121 const struct cifs_credits *credits, const int optype)
122{
123 spin_lock(&server->req_lock);
124 server->credits += credits->value;
125 server->in_flight--;
126 spin_unlock(&server->req_lock);
127 wake_up(&server->request_q);
128}
129
130static void
131cifs_set_credits(struct TCP_Server_Info *server, const int val)
132{
133 spin_lock(&server->req_lock);
134 server->credits = val;
135 server->oplocks = val > 1 ? enable_oplocks : false;
136 spin_unlock(&server->req_lock);
137}
138
139static int *
140cifs_get_credits_field(struct TCP_Server_Info *server, const int optype)
141{
142 return &server->credits;
143}
144
145static unsigned int
146cifs_get_credits(struct mid_q_entry *mid)
147{
148 return 1;
149}
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172static __u64
173cifs_get_next_mid(struct TCP_Server_Info *server)
174{
175 __u64 mid = 0;
176 __u16 last_mid, cur_mid;
177 bool collision;
178
179 spin_lock(&GlobalMid_Lock);
180
181
182 cur_mid = (__u16)((server->CurrentMid) & 0xffff);
183
184 last_mid = cur_mid;
185 cur_mid++;
186
187 if (cur_mid == 0xffff)
188 cur_mid++;
189
190
191
192
193
194
195
196
197
198
199 while (cur_mid != last_mid) {
200 struct mid_q_entry *mid_entry;
201 unsigned int num_mids;
202
203 collision = false;
204 if (cur_mid == 0)
205 cur_mid++;
206
207 num_mids = 0;
208 list_for_each_entry(mid_entry, &server->pending_mid_q, qhead) {
209 ++num_mids;
210 if (mid_entry->mid == cur_mid &&
211 mid_entry->mid_state == MID_REQUEST_SUBMITTED) {
212
213 collision = true;
214 break;
215 }
216 }
217
218
219
220
221
222
223
224
225
226
227
228 if (num_mids > 32768)
229 server->tcpStatus = CifsNeedReconnect;
230
231 if (!collision) {
232 mid = (__u64)cur_mid;
233 server->CurrentMid = mid;
234 break;
235 }
236 cur_mid++;
237 }
238 spin_unlock(&GlobalMid_Lock);
239 return mid;
240}
241
242
243
244
245
246
247
248static int
249check2ndT2(char *buf)
250{
251 struct smb_hdr *pSMB = (struct smb_hdr *)buf;
252 struct smb_t2_rsp *pSMBt;
253 int remaining;
254 __u16 total_data_size, data_in_this_rsp;
255
256 if (pSMB->Command != SMB_COM_TRANSACTION2)
257 return 0;
258
259
260
261 if (pSMB->WordCount != 10) {
262 cifs_dbg(FYI, "Invalid transact2 word count\n");
263 return -EINVAL;
264 }
265
266 pSMBt = (struct smb_t2_rsp *)pSMB;
267
268 total_data_size = get_unaligned_le16(&pSMBt->t2_rsp.TotalDataCount);
269 data_in_this_rsp = get_unaligned_le16(&pSMBt->t2_rsp.DataCount);
270
271 if (total_data_size == data_in_this_rsp)
272 return 0;
273 else if (total_data_size < data_in_this_rsp) {
274 cifs_dbg(FYI, "total data %d smaller than data in frame %d\n",
275 total_data_size, data_in_this_rsp);
276 return -EINVAL;
277 }
278
279 remaining = total_data_size - data_in_this_rsp;
280
281 cifs_dbg(FYI, "missing %d bytes from transact2, check next response\n",
282 remaining);
283 if (total_data_size > CIFSMaxBufSize) {
284 cifs_dbg(VFS, "TotalDataSize %d is over maximum buffer %d\n",
285 total_data_size, CIFSMaxBufSize);
286 return -EINVAL;
287 }
288 return remaining;
289}
290
291static int
292coalesce_t2(char *second_buf, struct smb_hdr *target_hdr)
293{
294 struct smb_t2_rsp *pSMBs = (struct smb_t2_rsp *)second_buf;
295 struct smb_t2_rsp *pSMBt = (struct smb_t2_rsp *)target_hdr;
296 char *data_area_of_tgt;
297 char *data_area_of_src;
298 int remaining;
299 unsigned int byte_count, total_in_tgt;
300 __u16 tgt_total_cnt, src_total_cnt, total_in_src;
301
302 src_total_cnt = get_unaligned_le16(&pSMBs->t2_rsp.TotalDataCount);
303 tgt_total_cnt = get_unaligned_le16(&pSMBt->t2_rsp.TotalDataCount);
304
305 if (tgt_total_cnt != src_total_cnt)
306 cifs_dbg(FYI, "total data count of primary and secondary t2 differ source=%hu target=%hu\n",
307 src_total_cnt, tgt_total_cnt);
308
309 total_in_tgt = get_unaligned_le16(&pSMBt->t2_rsp.DataCount);
310
311 remaining = tgt_total_cnt - total_in_tgt;
312
313 if (remaining < 0) {
314 cifs_dbg(FYI, "Server sent too much data. tgt_total_cnt=%hu total_in_tgt=%u\n",
315 tgt_total_cnt, total_in_tgt);
316 return -EPROTO;
317 }
318
319 if (remaining == 0) {
320
321 cifs_dbg(FYI, "no more data remains\n");
322 return 0;
323 }
324
325 total_in_src = get_unaligned_le16(&pSMBs->t2_rsp.DataCount);
326 if (remaining < total_in_src)
327 cifs_dbg(FYI, "transact2 2nd response contains too much data\n");
328
329
330 data_area_of_tgt = (char *)&pSMBt->hdr.Protocol +
331 get_unaligned_le16(&pSMBt->t2_rsp.DataOffset);
332
333
334 data_area_of_src = (char *)&pSMBs->hdr.Protocol +
335 get_unaligned_le16(&pSMBs->t2_rsp.DataOffset);
336
337 data_area_of_tgt += total_in_tgt;
338
339 total_in_tgt += total_in_src;
340
341 if (total_in_tgt > USHRT_MAX) {
342 cifs_dbg(FYI, "coalesced DataCount too large (%u)\n",
343 total_in_tgt);
344 return -EPROTO;
345 }
346 put_unaligned_le16(total_in_tgt, &pSMBt->t2_rsp.DataCount);
347
348
349 byte_count = get_bcc(target_hdr);
350 byte_count += total_in_src;
351
352 if (byte_count > USHRT_MAX) {
353 cifs_dbg(FYI, "coalesced BCC too large (%u)\n", byte_count);
354 return -EPROTO;
355 }
356 put_bcc(byte_count, target_hdr);
357
358 byte_count = be32_to_cpu(target_hdr->smb_buf_length);
359 byte_count += total_in_src;
360
361 if (byte_count > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
362 cifs_dbg(FYI, "coalesced BCC exceeds buffer size (%u)\n",
363 byte_count);
364 return -ENOBUFS;
365 }
366 target_hdr->smb_buf_length = cpu_to_be32(byte_count);
367
368
369 memcpy(data_area_of_tgt, data_area_of_src, total_in_src);
370
371 if (remaining != total_in_src) {
372
373 cifs_dbg(FYI, "waiting for more secondary responses\n");
374 return 1;
375 }
376
377
378 cifs_dbg(FYI, "found the last secondary response\n");
379 return 0;
380}
381
382static void
383cifs_downgrade_oplock(struct TCP_Server_Info *server,
384 struct cifsInodeInfo *cinode, __u32 oplock,
385 unsigned int epoch, bool *purge_cache)
386{
387 cifs_set_oplock_level(cinode, oplock);
388}
389
390static bool
391cifs_check_trans2(struct mid_q_entry *mid, struct TCP_Server_Info *server,
392 char *buf, int malformed)
393{
394 if (malformed)
395 return false;
396 if (check2ndT2(buf) <= 0)
397 return false;
398 mid->multiRsp = true;
399 if (mid->resp_buf) {
400
401 malformed = coalesce_t2(buf, mid->resp_buf);
402 if (malformed > 0)
403 return true;
404
405 mid->multiEnd = true;
406 dequeue_mid(mid, malformed);
407 return true;
408 }
409 if (!server->large_buf) {
410
411 cifs_dbg(VFS, "1st trans2 resp needs bigbuf\n");
412 } else {
413
414 mid->resp_buf = buf;
415 mid->large_buf = true;
416 server->bigbuf = NULL;
417 }
418 return true;
419}
420
421static bool
422cifs_need_neg(struct TCP_Server_Info *server)
423{
424 return server->maxBuf == 0;
425}
426
427static int
428cifs_negotiate(const unsigned int xid, struct cifs_ses *ses)
429{
430 int rc;
431 rc = CIFSSMBNegotiate(xid, ses);
432 if (rc == -EAGAIN) {
433
434 set_credits(ses->server, 1);
435 rc = CIFSSMBNegotiate(xid, ses);
436 if (rc == -EAGAIN)
437 rc = -EHOSTDOWN;
438 }
439 return rc;
440}
441
442static unsigned int
443cifs_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
444{
445 __u64 unix_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
446 struct TCP_Server_Info *server = tcon->ses->server;
447 unsigned int wsize;
448
449
450 if (volume_info->wsize)
451 wsize = volume_info->wsize;
452 else if (tcon->unix_ext && (unix_cap & CIFS_UNIX_LARGE_WRITE_CAP))
453 wsize = CIFS_DEFAULT_IOSIZE;
454 else
455 wsize = CIFS_DEFAULT_NON_POSIX_WSIZE;
456
457
458 if (!tcon->unix_ext || !(unix_cap & CIFS_UNIX_LARGE_WRITE_CAP))
459 wsize = min_t(unsigned int, wsize, CIFS_MAX_RFC1002_WSIZE);
460
461
462
463
464
465
466 if (!(server->capabilities & CAP_LARGE_WRITE_X) ||
467 (!(server->capabilities & CAP_UNIX) && server->sign))
468 wsize = min_t(unsigned int, wsize,
469 server->maxBuf - sizeof(WRITE_REQ) + 4);
470
471
472 wsize = min_t(unsigned int, wsize, CIFS_MAX_WSIZE);
473
474 return wsize;
475}
476
477static unsigned int
478cifs_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
479{
480 __u64 unix_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
481 struct TCP_Server_Info *server = tcon->ses->server;
482 unsigned int rsize, defsize;
483
484
485
486
487
488
489
490
491
492
493
494
495
496 if (tcon->unix_ext && (unix_cap & CIFS_UNIX_LARGE_READ_CAP))
497 defsize = CIFS_DEFAULT_IOSIZE;
498 else if (server->capabilities & CAP_LARGE_READ_X)
499 defsize = CIFS_DEFAULT_NON_POSIX_RSIZE;
500 else
501 defsize = server->maxBuf - sizeof(READ_RSP);
502
503 rsize = volume_info->rsize ? volume_info->rsize : defsize;
504
505
506
507
508
509 if (!(server->capabilities & CAP_LARGE_READ_X))
510 rsize = min_t(unsigned int, CIFSMaxBufSize, rsize);
511
512
513 rsize = min_t(unsigned int, rsize, CIFS_MAX_RSIZE);
514
515 return rsize;
516}
517
518static void
519cifs_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon,
520 struct cifs_sb_info *cifs_sb)
521{
522 CIFSSMBQFSDeviceInfo(xid, tcon);
523 CIFSSMBQFSAttributeInfo(xid, tcon);
524}
525
526static int
527cifs_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
528 struct cifs_sb_info *cifs_sb, const char *full_path)
529{
530 int rc;
531 FILE_ALL_INFO *file_info;
532
533 file_info = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
534 if (file_info == NULL)
535 return -ENOMEM;
536
537 rc = CIFSSMBQPathInfo(xid, tcon, full_path, file_info,
538 0 , cifs_sb->local_nls,
539 cifs_remap(cifs_sb));
540
541 if (rc == -EOPNOTSUPP || rc == -EINVAL)
542 rc = SMBQueryInformation(xid, tcon, full_path, file_info,
543 cifs_sb->local_nls, cifs_remap(cifs_sb));
544 kfree(file_info);
545 return rc;
546}
547
548static int
549cifs_query_path_info(const unsigned int xid, struct cifs_tcon *tcon,
550 struct cifs_sb_info *cifs_sb, const char *full_path,
551 FILE_ALL_INFO *data, bool *adjustTZ, bool *symlink)
552{
553 int rc;
554
555 *symlink = false;
556
557
558 rc = CIFSSMBQPathInfo(xid, tcon, full_path, data, 0 ,
559 cifs_sb->local_nls, cifs_remap(cifs_sb));
560
561
562
563
564
565 if ((rc == -EOPNOTSUPP) || (rc == -EINVAL)) {
566 rc = SMBQueryInformation(xid, tcon, full_path, data,
567 cifs_sb->local_nls,
568 cifs_remap(cifs_sb));
569 *adjustTZ = true;
570 }
571
572 if (!rc && (le32_to_cpu(data->Attributes) & ATTR_REPARSE)) {
573 int tmprc;
574 int oplock = 0;
575 struct cifs_fid fid;
576 struct cifs_open_parms oparms;
577
578 oparms.tcon = tcon;
579 oparms.cifs_sb = cifs_sb;
580 oparms.desired_access = FILE_READ_ATTRIBUTES;
581 oparms.create_options = cifs_create_options(cifs_sb, 0);
582 oparms.disposition = FILE_OPEN;
583 oparms.path = full_path;
584 oparms.fid = &fid;
585 oparms.reconnect = false;
586
587
588 tmprc = CIFS_open(xid, &oparms, &oplock, NULL);
589 if (tmprc == -EOPNOTSUPP)
590 *symlink = true;
591 else if (tmprc == 0)
592 CIFSSMBClose(xid, tcon, fid.netfid);
593 }
594
595 return rc;
596}
597
598static int
599cifs_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
600 struct cifs_sb_info *cifs_sb, const char *full_path,
601 u64 *uniqueid, FILE_ALL_INFO *data)
602{
603
604
605
606
607
608
609
610
611
612
613
614 return CIFSGetSrvInodeNumber(xid, tcon, full_path, uniqueid,
615 cifs_sb->local_nls,
616 cifs_remap(cifs_sb));
617}
618
619static int
620cifs_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
621 struct cifs_fid *fid, FILE_ALL_INFO *data)
622{
623 return CIFSSMBQFileInfo(xid, tcon, fid->netfid, data);
624}
625
626static void
627cifs_clear_stats(struct cifs_tcon *tcon)
628{
629 atomic_set(&tcon->stats.cifs_stats.num_writes, 0);
630 atomic_set(&tcon->stats.cifs_stats.num_reads, 0);
631 atomic_set(&tcon->stats.cifs_stats.num_flushes, 0);
632 atomic_set(&tcon->stats.cifs_stats.num_oplock_brks, 0);
633 atomic_set(&tcon->stats.cifs_stats.num_opens, 0);
634 atomic_set(&tcon->stats.cifs_stats.num_posixopens, 0);
635 atomic_set(&tcon->stats.cifs_stats.num_posixmkdirs, 0);
636 atomic_set(&tcon->stats.cifs_stats.num_closes, 0);
637 atomic_set(&tcon->stats.cifs_stats.num_deletes, 0);
638 atomic_set(&tcon->stats.cifs_stats.num_mkdirs, 0);
639 atomic_set(&tcon->stats.cifs_stats.num_rmdirs, 0);
640 atomic_set(&tcon->stats.cifs_stats.num_renames, 0);
641 atomic_set(&tcon->stats.cifs_stats.num_t2renames, 0);
642 atomic_set(&tcon->stats.cifs_stats.num_ffirst, 0);
643 atomic_set(&tcon->stats.cifs_stats.num_fnext, 0);
644 atomic_set(&tcon->stats.cifs_stats.num_fclose, 0);
645 atomic_set(&tcon->stats.cifs_stats.num_hardlinks, 0);
646 atomic_set(&tcon->stats.cifs_stats.num_symlinks, 0);
647 atomic_set(&tcon->stats.cifs_stats.num_locks, 0);
648 atomic_set(&tcon->stats.cifs_stats.num_acl_get, 0);
649 atomic_set(&tcon->stats.cifs_stats.num_acl_set, 0);
650}
651
652static void
653cifs_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
654{
655 seq_printf(m, " Oplocks breaks: %d",
656 atomic_read(&tcon->stats.cifs_stats.num_oplock_brks));
657 seq_printf(m, "\nReads: %d Bytes: %llu",
658 atomic_read(&tcon->stats.cifs_stats.num_reads),
659 (long long)(tcon->bytes_read));
660 seq_printf(m, "\nWrites: %d Bytes: %llu",
661 atomic_read(&tcon->stats.cifs_stats.num_writes),
662 (long long)(tcon->bytes_written));
663 seq_printf(m, "\nFlushes: %d",
664 atomic_read(&tcon->stats.cifs_stats.num_flushes));
665 seq_printf(m, "\nLocks: %d HardLinks: %d Symlinks: %d",
666 atomic_read(&tcon->stats.cifs_stats.num_locks),
667 atomic_read(&tcon->stats.cifs_stats.num_hardlinks),
668 atomic_read(&tcon->stats.cifs_stats.num_symlinks));
669 seq_printf(m, "\nOpens: %d Closes: %d Deletes: %d",
670 atomic_read(&tcon->stats.cifs_stats.num_opens),
671 atomic_read(&tcon->stats.cifs_stats.num_closes),
672 atomic_read(&tcon->stats.cifs_stats.num_deletes));
673 seq_printf(m, "\nPosix Opens: %d Posix Mkdirs: %d",
674 atomic_read(&tcon->stats.cifs_stats.num_posixopens),
675 atomic_read(&tcon->stats.cifs_stats.num_posixmkdirs));
676 seq_printf(m, "\nMkdirs: %d Rmdirs: %d",
677 atomic_read(&tcon->stats.cifs_stats.num_mkdirs),
678 atomic_read(&tcon->stats.cifs_stats.num_rmdirs));
679 seq_printf(m, "\nRenames: %d T2 Renames %d",
680 atomic_read(&tcon->stats.cifs_stats.num_renames),
681 atomic_read(&tcon->stats.cifs_stats.num_t2renames));
682 seq_printf(m, "\nFindFirst: %d FNext %d FClose %d",
683 atomic_read(&tcon->stats.cifs_stats.num_ffirst),
684 atomic_read(&tcon->stats.cifs_stats.num_fnext),
685 atomic_read(&tcon->stats.cifs_stats.num_fclose));
686}
687
688static void
689cifs_mkdir_setinfo(struct inode *inode, const char *full_path,
690 struct cifs_sb_info *cifs_sb, struct cifs_tcon *tcon,
691 const unsigned int xid)
692{
693 FILE_BASIC_INFO info;
694 struct cifsInodeInfo *cifsInode;
695 u32 dosattrs;
696 int rc;
697
698 memset(&info, 0, sizeof(info));
699 cifsInode = CIFS_I(inode);
700 dosattrs = cifsInode->cifsAttrs|ATTR_READONLY;
701 info.Attributes = cpu_to_le32(dosattrs);
702 rc = CIFSSMBSetPathInfo(xid, tcon, full_path, &info, cifs_sb->local_nls,
703 cifs_sb);
704 if (rc == 0)
705 cifsInode->cifsAttrs = dosattrs;
706}
707
708static int
709cifs_open_file(const unsigned int xid, struct cifs_open_parms *oparms,
710 __u32 *oplock, FILE_ALL_INFO *buf)
711{
712 if (!(oparms->tcon->ses->capabilities & CAP_NT_SMBS))
713 return SMBLegacyOpen(xid, oparms->tcon, oparms->path,
714 oparms->disposition,
715 oparms->desired_access,
716 oparms->create_options,
717 &oparms->fid->netfid, oplock, buf,
718 oparms->cifs_sb->local_nls,
719 cifs_remap(oparms->cifs_sb));
720 return CIFS_open(xid, oparms, oplock, buf);
721}
722
723static void
724cifs_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
725{
726 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
727 cfile->fid.netfid = fid->netfid;
728 cifs_set_oplock_level(cinode, oplock);
729 cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
730}
731
732static void
733cifs_close_file(const unsigned int xid, struct cifs_tcon *tcon,
734 struct cifs_fid *fid)
735{
736 CIFSSMBClose(xid, tcon, fid->netfid);
737}
738
739static int
740cifs_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
741 struct cifs_fid *fid)
742{
743 return CIFSSMBFlush(xid, tcon, fid->netfid);
744}
745
746static int
747cifs_sync_read(const unsigned int xid, struct cifs_fid *pfid,
748 struct cifs_io_parms *parms, unsigned int *bytes_read,
749 char **buf, int *buf_type)
750{
751 parms->netfid = pfid->netfid;
752 return CIFSSMBRead(xid, parms, bytes_read, buf, buf_type);
753}
754
755static int
756cifs_sync_write(const unsigned int xid, struct cifs_fid *pfid,
757 struct cifs_io_parms *parms, unsigned int *written,
758 struct kvec *iov, unsigned long nr_segs)
759{
760
761 parms->netfid = pfid->netfid;
762 return CIFSSMBWrite2(xid, parms, written, iov, nr_segs);
763}
764
765static int
766smb_set_file_info(struct inode *inode, const char *full_path,
767 FILE_BASIC_INFO *buf, const unsigned int xid)
768{
769 int oplock = 0;
770 int rc;
771 __u32 netpid;
772 struct cifs_fid fid;
773 struct cifs_open_parms oparms;
774 struct cifsFileInfo *open_file;
775 struct cifsInodeInfo *cinode = CIFS_I(inode);
776 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
777 struct tcon_link *tlink = NULL;
778 struct cifs_tcon *tcon;
779
780
781 open_file = find_writable_file(cinode, FIND_WR_FSUID_ONLY);
782 if (open_file) {
783 fid.netfid = open_file->fid.netfid;
784 netpid = open_file->pid;
785 tcon = tlink_tcon(open_file->tlink);
786 goto set_via_filehandle;
787 }
788
789 tlink = cifs_sb_tlink(cifs_sb);
790 if (IS_ERR(tlink)) {
791 rc = PTR_ERR(tlink);
792 tlink = NULL;
793 goto out;
794 }
795 tcon = tlink_tcon(tlink);
796
797 rc = CIFSSMBSetPathInfo(xid, tcon, full_path, buf, cifs_sb->local_nls,
798 cifs_sb);
799 if (rc == 0) {
800 cinode->cifsAttrs = le32_to_cpu(buf->Attributes);
801 goto out;
802 } else if (rc != -EOPNOTSUPP && rc != -EINVAL) {
803 goto out;
804 }
805
806 oparms.tcon = tcon;
807 oparms.cifs_sb = cifs_sb;
808 oparms.desired_access = SYNCHRONIZE | FILE_WRITE_ATTRIBUTES;
809 oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR);
810 oparms.disposition = FILE_OPEN;
811 oparms.path = full_path;
812 oparms.fid = &fid;
813 oparms.reconnect = false;
814
815 cifs_dbg(FYI, "calling SetFileInfo since SetPathInfo for times not supported by this server\n");
816 rc = CIFS_open(xid, &oparms, &oplock, NULL);
817 if (rc != 0) {
818 if (rc == -EIO)
819 rc = -EINVAL;
820 goto out;
821 }
822
823 netpid = current->tgid;
824
825set_via_filehandle:
826 rc = CIFSSMBSetFileInfo(xid, tcon, buf, fid.netfid, netpid);
827 if (!rc)
828 cinode->cifsAttrs = le32_to_cpu(buf->Attributes);
829
830 if (open_file == NULL)
831 CIFSSMBClose(xid, tcon, fid.netfid);
832 else
833 cifsFileInfo_put(open_file);
834out:
835 if (tlink != NULL)
836 cifs_put_tlink(tlink);
837 return rc;
838}
839
840static int
841cifs_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
842 struct cifsFileInfo *cfile)
843{
844 return CIFSSMB_set_compression(xid, tcon, cfile->fid.netfid);
845}
846
847static int
848cifs_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
849 const char *path, struct cifs_sb_info *cifs_sb,
850 struct cifs_fid *fid, __u16 search_flags,
851 struct cifs_search_info *srch_inf)
852{
853 int rc;
854
855 rc = CIFSFindFirst(xid, tcon, path, cifs_sb,
856 &fid->netfid, search_flags, srch_inf, true);
857 if (rc)
858 cifs_dbg(FYI, "find first failed=%d\n", rc);
859 return rc;
860}
861
862static int
863cifs_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
864 struct cifs_fid *fid, __u16 search_flags,
865 struct cifs_search_info *srch_inf)
866{
867 return CIFSFindNext(xid, tcon, fid->netfid, search_flags, srch_inf);
868}
869
870static int
871cifs_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
872 struct cifs_fid *fid)
873{
874 return CIFSFindClose(xid, tcon, fid->netfid);
875}
876
877static int
878cifs_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
879 struct cifsInodeInfo *cinode)
880{
881 return CIFSSMBLock(0, tcon, fid->netfid, current->tgid, 0, 0, 0, 0,
882 LOCKING_ANDX_OPLOCK_RELEASE, false,
883 CIFS_CACHE_READ(cinode) ? 1 : 0);
884}
885
886static int
887cifs_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
888 struct cifs_sb_info *cifs_sb, struct kstatfs *buf)
889{
890 int rc = -EOPNOTSUPP;
891
892 buf->f_type = CIFS_MAGIC_NUMBER;
893
894
895
896
897 if ((tcon->ses->capabilities & CAP_UNIX) &&
898 (CIFS_POSIX_EXTENSIONS & le64_to_cpu(tcon->fsUnixInfo.Capability)))
899 rc = CIFSSMBQFSPosixInfo(xid, tcon, buf);
900
901
902
903
904
905 if (rc && (tcon->ses->capabilities & CAP_NT_SMBS))
906 rc = CIFSSMBQFSInfo(xid, tcon, buf);
907
908
909
910
911
912
913 if (rc)
914 rc = SMBOldQFSInfo(xid, tcon, buf);
915 return rc;
916}
917
918static int
919cifs_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
920 __u64 length, __u32 type, int lock, int unlock, bool wait)
921{
922 return CIFSSMBLock(xid, tlink_tcon(cfile->tlink), cfile->fid.netfid,
923 current->tgid, length, offset, unlock, lock,
924 (__u8)type, wait, 0);
925}
926
927static int
928cifs_unix_dfs_readlink(const unsigned int xid, struct cifs_tcon *tcon,
929 const unsigned char *searchName, char **symlinkinfo,
930 const struct nls_table *nls_codepage)
931{
932#ifdef CONFIG_CIFS_DFS_UPCALL
933 int rc;
934 struct dfs_info3_param referral = {0};
935
936 rc = get_dfs_path(xid, tcon->ses, searchName, nls_codepage, &referral,
937 0);
938
939 if (!rc) {
940 *symlinkinfo = kstrndup(referral.node_name,
941 strlen(referral.node_name),
942 GFP_KERNEL);
943 free_dfs_info_param(&referral);
944 if (!*symlinkinfo)
945 rc = -ENOMEM;
946 }
947 return rc;
948#else
949 return -EREMOTE;
950#endif
951}
952
953static int
954cifs_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
955 struct cifs_sb_info *cifs_sb, const char *full_path,
956 char **target_path, bool is_reparse_point)
957{
958 int rc;
959 int oplock = 0;
960 struct cifs_fid fid;
961 struct cifs_open_parms oparms;
962
963 cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
964
965 if (is_reparse_point) {
966 cifs_dbg(VFS, "reparse points not handled for SMB1 symlinks\n");
967 return -EOPNOTSUPP;
968 }
969
970
971 if (cap_unix(tcon->ses)) {
972 rc = CIFSSMBUnixQuerySymLink(xid, tcon, full_path, target_path,
973 cifs_sb->local_nls,
974 cifs_remap(cifs_sb));
975 if (rc == -EREMOTE)
976 rc = cifs_unix_dfs_readlink(xid, tcon, full_path,
977 target_path,
978 cifs_sb->local_nls);
979
980 goto out;
981 }
982
983 oparms.tcon = tcon;
984 oparms.cifs_sb = cifs_sb;
985 oparms.desired_access = FILE_READ_ATTRIBUTES;
986 oparms.create_options = cifs_create_options(cifs_sb,
987 OPEN_REPARSE_POINT);
988 oparms.disposition = FILE_OPEN;
989 oparms.path = full_path;
990 oparms.fid = &fid;
991 oparms.reconnect = false;
992
993 rc = CIFS_open(xid, &oparms, &oplock, NULL);
994 if (rc)
995 goto out;
996
997 rc = CIFSSMBQuerySymLink(xid, tcon, fid.netfid, target_path,
998 cifs_sb->local_nls);
999 if (rc)
1000 goto out_close;
1001
1002 convert_delimiter(*target_path, '/');
1003out_close:
1004 CIFSSMBClose(xid, tcon, fid.netfid);
1005out:
1006 if (!rc)
1007 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
1008 return rc;
1009}
1010
1011static bool
1012cifs_is_read_op(__u32 oplock)
1013{
1014 return oplock == OPLOCK_READ;
1015}
1016
1017static unsigned int
1018cifs_wp_retry_size(struct inode *inode)
1019{
1020 return CIFS_SB(inode->i_sb)->wsize;
1021}
1022
1023static bool
1024cifs_dir_needs_close(struct cifsFileInfo *cfile)
1025{
1026 return !cfile->srch_inf.endOfSearch && !cfile->invalidHandle;
1027}
1028
1029static bool
1030cifs_can_echo(struct TCP_Server_Info *server)
1031{
1032 if (server->tcpStatus == CifsGood)
1033 return true;
1034
1035 return false;
1036}
1037
1038static int
1039cifs_make_node(unsigned int xid, struct inode *inode,
1040 struct dentry *dentry, struct cifs_tcon *tcon,
1041 char *full_path, umode_t mode, dev_t dev)
1042{
1043 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1044 struct inode *newinode = NULL;
1045 int rc = -EPERM;
1046 FILE_ALL_INFO *buf = NULL;
1047 struct cifs_io_parms io_parms;
1048 __u32 oplock = 0;
1049 struct cifs_fid fid;
1050 struct cifs_open_parms oparms;
1051 unsigned int bytes_written;
1052 struct win_dev *pdev;
1053 struct kvec iov[2];
1054
1055 if (tcon->unix_ext) {
1056
1057
1058
1059
1060 struct cifs_unix_set_info_args args = {
1061 .mode = mode & ~current_umask(),
1062 .ctime = NO_CHANGE_64,
1063 .atime = NO_CHANGE_64,
1064 .mtime = NO_CHANGE_64,
1065 .device = dev,
1066 };
1067 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
1068 args.uid = current_fsuid();
1069 args.gid = current_fsgid();
1070 } else {
1071 args.uid = INVALID_UID;
1072 args.gid = INVALID_GID;
1073 }
1074 rc = CIFSSMBUnixSetPathInfo(xid, tcon, full_path, &args,
1075 cifs_sb->local_nls,
1076 cifs_remap(cifs_sb));
1077 if (rc)
1078 goto out;
1079
1080 rc = cifs_get_inode_info_unix(&newinode, full_path,
1081 inode->i_sb, xid);
1082
1083 if (rc == 0)
1084 d_instantiate(dentry, newinode);
1085 goto out;
1086 }
1087
1088
1089
1090
1091
1092 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL))
1093 goto out;
1094
1095 if (!S_ISCHR(mode) && !S_ISBLK(mode))
1096 goto out;
1097
1098 cifs_dbg(FYI, "sfu compat create special file\n");
1099
1100 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
1101 if (buf == NULL) {
1102 rc = -ENOMEM;
1103 goto out;
1104 }
1105
1106 oparms.tcon = tcon;
1107 oparms.cifs_sb = cifs_sb;
1108 oparms.desired_access = GENERIC_WRITE;
1109 oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR |
1110 CREATE_OPTION_SPECIAL);
1111 oparms.disposition = FILE_CREATE;
1112 oparms.path = full_path;
1113 oparms.fid = &fid;
1114 oparms.reconnect = false;
1115
1116 if (tcon->ses->server->oplocks)
1117 oplock = REQ_OPLOCK;
1118 else
1119 oplock = 0;
1120 rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, buf);
1121 if (rc)
1122 goto out;
1123
1124
1125
1126
1127
1128
1129 pdev = (struct win_dev *)buf;
1130 io_parms.pid = current->tgid;
1131 io_parms.tcon = tcon;
1132 io_parms.offset = 0;
1133 io_parms.length = sizeof(struct win_dev);
1134 iov[1].iov_base = buf;
1135 iov[1].iov_len = sizeof(struct win_dev);
1136 if (S_ISCHR(mode)) {
1137 memcpy(pdev->type, "IntxCHR", 8);
1138 pdev->major = cpu_to_le64(MAJOR(dev));
1139 pdev->minor = cpu_to_le64(MINOR(dev));
1140 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
1141 &bytes_written, iov, 1);
1142 } else if (S_ISBLK(mode)) {
1143 memcpy(pdev->type, "IntxBLK", 8);
1144 pdev->major = cpu_to_le64(MAJOR(dev));
1145 pdev->minor = cpu_to_le64(MINOR(dev));
1146 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
1147 &bytes_written, iov, 1);
1148 }
1149 tcon->ses->server->ops->close(xid, tcon, &fid);
1150 d_drop(dentry);
1151
1152
1153out:
1154 kfree(buf);
1155 return rc;
1156}
1157
1158
1159
1160struct smb_version_operations smb1_operations = {
1161 .send_cancel = send_nt_cancel,
1162 .compare_fids = cifs_compare_fids,
1163 .setup_request = cifs_setup_request,
1164 .setup_async_request = cifs_setup_async_request,
1165 .check_receive = cifs_check_receive,
1166 .add_credits = cifs_add_credits,
1167 .set_credits = cifs_set_credits,
1168 .get_credits_field = cifs_get_credits_field,
1169 .get_credits = cifs_get_credits,
1170 .wait_mtu_credits = cifs_wait_mtu_credits,
1171 .get_next_mid = cifs_get_next_mid,
1172 .read_data_offset = cifs_read_data_offset,
1173 .read_data_length = cifs_read_data_length,
1174 .map_error = map_smb_to_linux_error,
1175 .find_mid = cifs_find_mid,
1176 .check_message = checkSMB,
1177 .dump_detail = cifs_dump_detail,
1178 .clear_stats = cifs_clear_stats,
1179 .print_stats = cifs_print_stats,
1180 .is_oplock_break = is_valid_oplock_break,
1181 .downgrade_oplock = cifs_downgrade_oplock,
1182 .check_trans2 = cifs_check_trans2,
1183 .need_neg = cifs_need_neg,
1184 .negotiate = cifs_negotiate,
1185 .negotiate_wsize = cifs_negotiate_wsize,
1186 .negotiate_rsize = cifs_negotiate_rsize,
1187 .sess_setup = CIFS_SessSetup,
1188 .logoff = CIFSSMBLogoff,
1189 .tree_connect = CIFSTCon,
1190 .tree_disconnect = CIFSSMBTDis,
1191 .get_dfs_refer = CIFSGetDFSRefer,
1192 .qfs_tcon = cifs_qfs_tcon,
1193 .is_path_accessible = cifs_is_path_accessible,
1194 .can_echo = cifs_can_echo,
1195 .query_path_info = cifs_query_path_info,
1196 .query_file_info = cifs_query_file_info,
1197 .get_srv_inum = cifs_get_srv_inum,
1198 .set_path_size = CIFSSMBSetEOF,
1199 .set_file_size = CIFSSMBSetFileSize,
1200 .set_file_info = smb_set_file_info,
1201 .set_compression = cifs_set_compression,
1202 .echo = CIFSSMBEcho,
1203 .mkdir = CIFSSMBMkDir,
1204 .mkdir_setinfo = cifs_mkdir_setinfo,
1205 .rmdir = CIFSSMBRmDir,
1206 .unlink = CIFSSMBDelFile,
1207 .rename_pending_delete = cifs_rename_pending_delete,
1208 .rename = CIFSSMBRename,
1209 .create_hardlink = CIFSCreateHardLink,
1210 .query_symlink = cifs_query_symlink,
1211 .open = cifs_open_file,
1212 .set_fid = cifs_set_fid,
1213 .close = cifs_close_file,
1214 .flush = cifs_flush_file,
1215 .async_readv = cifs_async_readv,
1216 .async_writev = cifs_async_writev,
1217 .sync_read = cifs_sync_read,
1218 .sync_write = cifs_sync_write,
1219 .query_dir_first = cifs_query_dir_first,
1220 .query_dir_next = cifs_query_dir_next,
1221 .close_dir = cifs_close_dir,
1222 .calc_smb_size = smbCalcSize,
1223 .oplock_response = cifs_oplock_response,
1224 .queryfs = cifs_queryfs,
1225 .mand_lock = cifs_mand_lock,
1226 .mand_unlock_range = cifs_unlock_range,
1227 .push_mand_locks = cifs_push_mandatory_locks,
1228 .query_mf_symlink = cifs_query_mf_symlink,
1229 .create_mf_symlink = cifs_create_mf_symlink,
1230 .is_read_op = cifs_is_read_op,
1231 .wp_retry_size = cifs_wp_retry_size,
1232 .dir_needs_close = cifs_dir_needs_close,
1233 .select_sectype = cifs_select_sectype,
1234#ifdef CONFIG_CIFS_XATTR
1235 .query_all_EAs = CIFSSMBQAllEAs,
1236 .set_EA = CIFSSMBSetEA,
1237#endif
1238 .get_acl = get_cifs_acl,
1239 .get_acl_by_fid = get_cifs_acl_by_fid,
1240 .set_acl = set_cifs_acl,
1241 .make_node = cifs_make_node,
1242};
1243
1244struct smb_version_values smb1_values = {
1245 .version_string = SMB1_VERSION_STRING,
1246 .protocol_id = SMB10_PROT_ID,
1247 .large_lock_type = LOCKING_ANDX_LARGE_FILES,
1248 .exclusive_lock_type = 0,
1249 .shared_lock_type = LOCKING_ANDX_SHARED_LOCK,
1250 .unlock_lock_type = 0,
1251 .header_preamble_size = 4,
1252 .header_size = sizeof(struct smb_hdr),
1253 .max_header_size = MAX_CIFS_HDR_SIZE,
1254 .read_rsp_size = sizeof(READ_RSP),
1255 .lock_cmd = cpu_to_le16(SMB_COM_LOCKING_ANDX),
1256 .cap_unix = CAP_UNIX,
1257 .cap_nt_find = CAP_NT_SMBS | CAP_NT_FIND,
1258 .cap_large_files = CAP_LARGE_FILES,
1259 .signing_enabled = SECMODE_SIGN_ENABLED,
1260 .signing_required = SECMODE_SIGN_REQUIRED,
1261};
1262