1 // SPDX-License-Identifier: GPL-2.0-only
3 * SMB1 (CIFS) version specific operations
5 * Copyright (c) 2012, Jeff Layton <jlayton@redhat.com>
8 #include <linux/pagemap.h>
11 #include "cifsproto.h"
12 #include "cifs_debug.h"
14 #include "cifs_unicode.h"
17 * An NT cancel request header looks just like the original request except:
19 * The Command is SMB_COM_NT_CANCEL
20 * The WordCount is zeroed out
21 * The ByteCount is zeroed out
23 * This function mangles an existing request buffer into a
24 * SMB_COM_NT_CANCEL request and then sends it.
27 send_nt_cancel(struct TCP_Server_Info
*server
, struct smb_rqst
*rqst
,
28 struct mid_q_entry
*mid
)
31 struct smb_hdr
*in_buf
= (struct smb_hdr
*)rqst
->rq_iov
[0].iov_base
;
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;
39 mutex_lock(&server
->srv_mutex
);
40 rc
= cifs_sign_smb(in_buf
, server
, &mid
->sequence_number
);
42 mutex_unlock(&server
->srv_mutex
);
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
51 --server
->sequence_number
;
52 rc
= smb_send(server
, in_buf
, be32_to_cpu(in_buf
->smb_buf_length
));
54 server
->sequence_number
--;
56 mutex_unlock(&server
->srv_mutex
);
58 cifs_dbg(FYI
, "issued NT_CANCEL for mid %u, rc = %d\n",
65 cifs_compare_fids(struct cifsFileInfo
*ob1
, struct cifsFileInfo
*ob2
)
67 return ob1
->fid
.netfid
== ob2
->fid
.netfid
;
71 cifs_read_data_offset(char *buf
)
73 READ_RSP
*rsp
= (READ_RSP
*)buf
;
74 return le16_to_cpu(rsp
->DataOffset
);
78 cifs_read_data_length(char *buf
, bool in_remaining
)
80 READ_RSP
*rsp
= (READ_RSP
*)buf
;
81 /* It's a bug reading remaining data for SMB1 packets */
82 WARN_ON(in_remaining
);
83 return (le16_to_cpu(rsp
->DataLengthHigh
) << 16) +
84 le16_to_cpu(rsp
->DataLength
);
87 static struct mid_q_entry
*
88 cifs_find_mid(struct TCP_Server_Info
*server
, char *buffer
)
90 struct smb_hdr
*buf
= (struct smb_hdr
*)buffer
;
91 struct mid_q_entry
*mid
;
93 spin_lock(&GlobalMid_Lock
);
94 list_for_each_entry(mid
, &server
->pending_mid_q
, qhead
) {
95 if (compare_mid(mid
->mid
, buf
) &&
96 mid
->mid_state
== MID_REQUEST_SUBMITTED
&&
97 le16_to_cpu(mid
->command
) == buf
->Command
) {
98 kref_get(&mid
->refcount
);
99 spin_unlock(&GlobalMid_Lock
);
103 spin_unlock(&GlobalMid_Lock
);
108 cifs_add_credits(struct TCP_Server_Info
*server
,
109 const struct cifs_credits
*credits
, const int optype
)
111 spin_lock(&server
->req_lock
);
112 server
->credits
+= credits
->value
;
114 spin_unlock(&server
->req_lock
);
115 wake_up(&server
->request_q
);
119 cifs_set_credits(struct TCP_Server_Info
*server
, const int val
)
121 spin_lock(&server
->req_lock
);
122 server
->credits
= val
;
123 server
->oplocks
= val
> 1 ? enable_oplocks
: false;
124 spin_unlock(&server
->req_lock
);
128 cifs_get_credits_field(struct TCP_Server_Info
*server
, const int optype
)
130 return &server
->credits
;
134 cifs_get_credits(struct mid_q_entry
*mid
)
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.
161 cifs_get_next_mid(struct TCP_Server_Info
*server
)
164 __u16 last_mid
, cur_mid
;
167 spin_lock(&GlobalMid_Lock
);
169 /* mid is 16 bit only for CIFS/SMB */
170 cur_mid
= (__u16
)((server
->CurrentMid
) & 0xffff);
171 /* we do not want to loop forever */
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
184 while (cur_mid
!= last_mid
) {
185 struct mid_q_entry
*mid_entry
;
186 unsigned int num_mids
;
193 list_for_each_entry(mid_entry
, &server
->pending_mid_q
, qhead
) {
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 */
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.
210 * Go ahead and assign out the mid in this situation, but force
211 * an eventual reconnect to clean out the pending_mid_q.
213 if (num_mids
> 32768)
214 server
->tcpStatus
= CifsNeedReconnect
;
217 mid
= (__u64
)cur_mid
;
218 server
->CurrentMid
= mid
;
223 spin_unlock(&GlobalMid_Lock
);
229 0 not a transact2, or all data present
230 >0 transact2 with that much data missing
231 -EINVAL invalid transact2
234 check2ndT2(char *buf
)
236 struct smb_hdr
*pSMB
= (struct smb_hdr
*)buf
;
237 struct smb_t2_rsp
*pSMBt
;
239 __u16 total_data_size
, data_in_this_rsp
;
241 if (pSMB
->Command
!= SMB_COM_TRANSACTION2
)
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 */
247 cifs_dbg(FYI
, "invalid transact2 word count\n");
251 pSMBt
= (struct smb_t2_rsp
*)pSMB
;
253 total_data_size
= get_unaligned_le16(&pSMBt
->t2_rsp
.TotalDataCount
);
254 data_in_this_rsp
= get_unaligned_le16(&pSMBt
->t2_rsp
.DataCount
);
256 if (total_data_size
== data_in_this_rsp
)
258 else if (total_data_size
< data_in_this_rsp
) {
259 cifs_dbg(FYI
, "total data %d smaller than data in frame %d\n",
260 total_data_size
, data_in_this_rsp
);
264 remaining
= total_data_size
- data_in_this_rsp
;
266 cifs_dbg(FYI
, "missing %d bytes from transact2, check next response\n",
268 if (total_data_size
> CIFSMaxBufSize
) {
269 cifs_dbg(VFS
, "TotalDataSize %d is over maximum buffer %d\n",
270 total_data_size
, CIFSMaxBufSize
);
277 coalesce_t2(char *second_buf
, struct smb_hdr
*target_hdr
)
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
;
284 unsigned int byte_count
, total_in_tgt
;
285 __u16 tgt_total_cnt
, src_total_cnt
, total_in_src
;
287 src_total_cnt
= get_unaligned_le16(&pSMBs
->t2_rsp
.TotalDataCount
);
288 tgt_total_cnt
= get_unaligned_le16(&pSMBt
->t2_rsp
.TotalDataCount
);
290 if (tgt_total_cnt
!= src_total_cnt
)
291 cifs_dbg(FYI
, "total data count of primary and secondary t2 differ source=%hu target=%hu\n",
292 src_total_cnt
, tgt_total_cnt
);
294 total_in_tgt
= get_unaligned_le16(&pSMBt
->t2_rsp
.DataCount
);
296 remaining
= tgt_total_cnt
- total_in_tgt
;
299 cifs_dbg(FYI
, "Server sent too much data. tgt_total_cnt=%hu total_in_tgt=%u\n",
300 tgt_total_cnt
, total_in_tgt
);
304 if (remaining
== 0) {
305 /* nothing to do, ignore */
306 cifs_dbg(FYI
, "no more data remains\n");
310 total_in_src
= get_unaligned_le16(&pSMBs
->t2_rsp
.DataCount
);
311 if (remaining
< total_in_src
)
312 cifs_dbg(FYI
, "transact2 2nd response contains too much data\n");
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
);
318 /* validate target area */
319 data_area_of_src
= (char *)&pSMBs
->hdr
.Protocol
+
320 get_unaligned_le16(&pSMBs
->t2_rsp
.DataOffset
);
322 data_area_of_tgt
+= total_in_tgt
;
324 total_in_tgt
+= total_in_src
;
325 /* is the result too big for the field? */
326 if (total_in_tgt
> USHRT_MAX
) {
327 cifs_dbg(FYI
, "coalesced DataCount too large (%u)\n",
331 put_unaligned_le16(total_in_tgt
, &pSMBt
->t2_rsp
.DataCount
);
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
) {
338 cifs_dbg(FYI
, "coalesced BCC too large (%u)\n", byte_count
);
341 put_bcc(byte_count
, target_hdr
);
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) {
347 cifs_dbg(FYI
, "coalesced BCC exceeds buffer size (%u)\n",
351 target_hdr
->smb_buf_length
= cpu_to_be32(byte_count
);
353 /* copy second buffer into end of first buffer */
354 memcpy(data_area_of_tgt
, data_area_of_src
, total_in_src
);
356 if (remaining
!= total_in_src
) {
357 /* more responses to go */
358 cifs_dbg(FYI
, "waiting for more secondary responses\n");
363 cifs_dbg(FYI
, "found the last secondary response\n");
368 cifs_downgrade_oplock(struct TCP_Server_Info
*server
,
369 struct cifsInodeInfo
*cinode
, bool set_level2
)
372 cifs_set_oplock_level(cinode
, OPLOCK_READ
);
374 cifs_set_oplock_level(cinode
, 0);
378 cifs_check_trans2(struct mid_q_entry
*mid
, struct TCP_Server_Info
*server
,
379 char *buf
, int malformed
)
383 if (check2ndT2(buf
) <= 0)
385 mid
->multiRsp
= true;
387 /* merge response - fix up 1st*/
388 malformed
= coalesce_t2(buf
, mid
->resp_buf
);
391 /* All parts received or packet is malformed. */
392 mid
->multiEnd
= true;
393 dequeue_mid(mid
, malformed
);
396 if (!server
->large_buf
) {
397 /*FIXME: switch to already allocated largebuf?*/
398 cifs_dbg(VFS
, "1st trans2 resp needs bigbuf\n");
400 /* Have first buffer */
402 mid
->large_buf
= true;
403 server
->bigbuf
= NULL
;
409 cifs_need_neg(struct TCP_Server_Info
*server
)
411 return server
->maxBuf
== 0;
415 cifs_negotiate(const unsigned int xid
, struct cifs_ses
*ses
)
418 rc
= CIFSSMBNegotiate(xid
, ses
);
420 /* retry only once on 1st time connection */
421 set_credits(ses
->server
, 1);
422 rc
= CIFSSMBNegotiate(xid
, ses
);
430 cifs_negotiate_wsize(struct cifs_tcon
*tcon
, struct smb_vol
*volume_info
)
432 __u64 unix_cap
= le64_to_cpu(tcon
->fsUnixInfo
.Capability
);
433 struct TCP_Server_Info
*server
= tcon
->ses
->server
;
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
;
442 wsize
= CIFS_DEFAULT_NON_POSIX_WSIZE
;
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
);
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.
453 if (!(server
->capabilities
& CAP_LARGE_WRITE_X
) ||
454 (!(server
->capabilities
& CAP_UNIX
) && server
->sign
))
455 wsize
= min_t(unsigned int, wsize
,
456 server
->maxBuf
- sizeof(WRITE_REQ
) + 4);
458 /* hard limit of CIFS_MAX_WSIZE */
459 wsize
= min_t(unsigned int, wsize
, CIFS_MAX_WSIZE
);
465 cifs_negotiate_rsize(struct cifs_tcon
*tcon
, struct smb_vol
*volume_info
)
467 __u64 unix_cap
= le64_to_cpu(tcon
->fsUnixInfo
.Capability
);
468 struct TCP_Server_Info
*server
= tcon
->ses
->server
;
469 unsigned int rsize
, defsize
;
472 * Set default value...
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.
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.
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
;
488 defsize
= server
->maxBuf
- sizeof(READ_RSP
);
490 rsize
= volume_info
->rsize
? volume_info
->rsize
: defsize
;
493 * no CAP_LARGE_READ_X? Then MS-CIFS states that we must limit this to
494 * the client's MaxBufferSize.
496 if (!(server
->capabilities
& CAP_LARGE_READ_X
))
497 rsize
= min_t(unsigned int, CIFSMaxBufSize
, rsize
);
499 /* hard limit of CIFS_MAX_RSIZE */
500 rsize
= min_t(unsigned int, rsize
, CIFS_MAX_RSIZE
);
506 cifs_qfs_tcon(const unsigned int xid
, struct cifs_tcon
*tcon
)
508 CIFSSMBQFSDeviceInfo(xid
, tcon
);
509 CIFSSMBQFSAttributeInfo(xid
, tcon
);
513 cifs_is_path_accessible(const unsigned int xid
, struct cifs_tcon
*tcon
,
514 struct cifs_sb_info
*cifs_sb
, const char *full_path
)
517 FILE_ALL_INFO
*file_info
;
519 file_info
= kmalloc(sizeof(FILE_ALL_INFO
), GFP_KERNEL
);
520 if (file_info
== NULL
)
523 rc
= CIFSSMBQPathInfo(xid
, tcon
, full_path
, file_info
,
524 0 /* not legacy */, cifs_sb
->local_nls
,
525 cifs_remap(cifs_sb
));
527 if (rc
== -EOPNOTSUPP
|| rc
== -EINVAL
)
528 rc
= SMBQueryInformation(xid
, tcon
, full_path
, file_info
,
529 cifs_sb
->local_nls
, cifs_remap(cifs_sb
));
535 cifs_query_path_info(const unsigned int xid
, struct cifs_tcon
*tcon
,
536 struct cifs_sb_info
*cifs_sb
, const char *full_path
,
537 FILE_ALL_INFO
*data
, bool *adjustTZ
, bool *symlink
)
543 /* could do find first instead but this returns more info */
544 rc
= CIFSSMBQPathInfo(xid
, tcon
, full_path
, data
, 0 /* not legacy */,
545 cifs_sb
->local_nls
, cifs_remap(cifs_sb
));
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
551 if ((rc
== -EOPNOTSUPP
) || (rc
== -EINVAL
)) {
552 rc
= SMBQueryInformation(xid
, tcon
, full_path
, data
,
554 cifs_remap(cifs_sb
));
558 if (!rc
&& (le32_to_cpu(data
->Attributes
) & ATTR_REPARSE
)) {
562 struct cifs_open_parms oparms
;
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
;
571 oparms
.reconnect
= false;
573 /* Need to check if this is a symbolic link or not */
574 tmprc
= CIFS_open(xid
, &oparms
, &oplock
, NULL
);
575 if (tmprc
== -EOPNOTSUPP
)
578 CIFSSMBClose(xid
, tcon
, fid
.netfid
);
585 cifs_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
)
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
596 * There may be higher info levels that work but are there Windows
597 * server or network appliances for which IndexNumber field is not
600 return CIFSGetSrvInodeNumber(xid
, tcon
, full_path
, uniqueid
,
602 cifs_remap(cifs_sb
));
606 cifs_query_file_info(const unsigned int xid
, struct cifs_tcon
*tcon
,
607 struct cifs_fid
*fid
, FILE_ALL_INFO
*data
)
609 return CIFSSMBQFileInfo(xid
, tcon
, fid
->netfid
, data
);
613 cifs_clear_stats(struct cifs_tcon
*tcon
)
615 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);
639 cifs_print_stats(struct seq_file
*m
, struct cifs_tcon
*tcon
)
641 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
));
675 cifs_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
)
679 FILE_BASIC_INFO info
;
680 struct cifsInodeInfo
*cifsInode
;
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
,
689 cifs_remap(cifs_sb
));
691 cifsInode
->cifsAttrs
= dosattrs
;
695 cifs_open_file(const unsigned int xid
, struct cifs_open_parms
*oparms
,
696 __u32
*oplock
, FILE_ALL_INFO
*buf
)
698 if (!(oparms
->tcon
->ses
->capabilities
& CAP_NT_SMBS
))
699 return SMBLegacyOpen(xid
, oparms
->tcon
, oparms
->path
,
701 oparms
->desired_access
,
702 oparms
->create_options
,
703 &oparms
->fid
->netfid
, oplock
, buf
,
704 oparms
->cifs_sb
->local_nls
,
705 cifs_remap(oparms
->cifs_sb
));
706 return CIFS_open(xid
, oparms
, oplock
, buf
);
710 cifs_set_fid(struct cifsFileInfo
*cfile
, struct cifs_fid
*fid
, __u32 oplock
)
712 struct cifsInodeInfo
*cinode
= CIFS_I(d_inode(cfile
->dentry
));
713 cfile
->fid
.netfid
= fid
->netfid
;
714 cifs_set_oplock_level(cinode
, oplock
);
715 cinode
->can_cache_brlcks
= CIFS_CACHE_WRITE(cinode
);
719 cifs_close_file(const unsigned int xid
, struct cifs_tcon
*tcon
,
720 struct cifs_fid
*fid
)
722 CIFSSMBClose(xid
, tcon
, fid
->netfid
);
726 cifs_flush_file(const unsigned int xid
, struct cifs_tcon
*tcon
,
727 struct cifs_fid
*fid
)
729 return CIFSSMBFlush(xid
, tcon
, fid
->netfid
);
733 cifs_sync_read(const unsigned int xid
, struct cifs_fid
*pfid
,
734 struct cifs_io_parms
*parms
, unsigned int *bytes_read
,
735 char **buf
, int *buf_type
)
737 parms
->netfid
= pfid
->netfid
;
738 return CIFSSMBRead(xid
, parms
, bytes_read
, buf
, buf_type
);
742 cifs_sync_write(const unsigned int xid
, struct cifs_fid
*pfid
,
743 struct cifs_io_parms
*parms
, unsigned int *written
,
744 struct kvec
*iov
, unsigned long nr_segs
)
747 parms
->netfid
= pfid
->netfid
;
748 return CIFSSMBWrite2(xid
, parms
, written
, iov
, nr_segs
);
752 smb_set_file_info(struct inode
*inode
, const char *full_path
,
753 FILE_BASIC_INFO
*buf
, const unsigned int xid
)
759 struct cifs_open_parms oparms
;
760 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
;
766 /* if the file is already open for write, just use that fileid */
767 open_file
= find_writable_file(cinode
, true);
769 fid
.netfid
= open_file
->fid
.netfid
;
770 netpid
= open_file
->pid
;
771 tcon
= tlink_tcon(open_file
->tlink
);
772 goto set_via_filehandle
;
775 tlink
= cifs_sb_tlink(cifs_sb
);
781 tcon
= tlink_tcon(tlink
);
783 rc
= CIFSSMBSetPathInfo(xid
, tcon
, full_path
, buf
, cifs_sb
->local_nls
,
784 cifs_remap(cifs_sb
));
786 cinode
->cifsAttrs
= le32_to_cpu(buf
->Attributes
);
788 } else if (rc
!= -EOPNOTSUPP
&& rc
!= -EINVAL
) {
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
;
799 oparms
.reconnect
= false;
801 cifs_dbg(FYI
, "calling SetFileInfo since SetPathInfo for times not supported by this server\n");
802 rc
= CIFS_open(xid
, &oparms
, &oplock
, NULL
);
809 netpid
= current
->tgid
;
812 rc
= CIFSSMBSetFileInfo(xid
, tcon
, buf
, fid
.netfid
, netpid
);
814 cinode
->cifsAttrs
= le32_to_cpu(buf
->Attributes
);
816 if (open_file
== NULL
)
817 CIFSSMBClose(xid
, tcon
, fid
.netfid
);
819 cifsFileInfo_put(open_file
);
822 cifs_put_tlink(tlink
);
827 cifs_set_compression(const unsigned int xid
, struct cifs_tcon
*tcon
,
828 struct cifsFileInfo
*cfile
)
830 return CIFSSMB_set_compression(xid
, tcon
, cfile
->fid
.netfid
);
834 cifs_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
)
841 rc
= CIFSFindFirst(xid
, tcon
, path
, cifs_sb
,
842 &fid
->netfid
, search_flags
, srch_inf
, true);
844 cifs_dbg(FYI
, "find first failed=%d\n", rc
);
849 cifs_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
)
853 return CIFSFindNext(xid
, tcon
, fid
->netfid
, search_flags
, srch_inf
);
857 cifs_close_dir(const unsigned int xid
, struct cifs_tcon
*tcon
,
858 struct cifs_fid
*fid
)
860 return CIFSFindClose(xid
, tcon
, fid
->netfid
);
864 cifs_oplock_response(struct cifs_tcon
*tcon
, struct cifs_fid
*fid
,
865 struct cifsInodeInfo
*cinode
)
867 return CIFSSMBLock(0, tcon
, fid
->netfid
, current
->tgid
, 0, 0, 0, 0,
868 LOCKING_ANDX_OPLOCK_RELEASE
, false,
869 CIFS_CACHE_READ(cinode
) ? 1 : 0);
873 cifs_queryfs(const unsigned int xid
, struct cifs_tcon
*tcon
,
876 int rc
= -EOPNOTSUPP
;
878 buf
->f_type
= CIFS_MAGIC_NUMBER
;
881 * We could add a second check for a QFS Unix capability bit
883 if ((tcon
->ses
->capabilities
& CAP_UNIX
) &&
884 (CIFS_POSIX_EXTENSIONS
& le64_to_cpu(tcon
->fsUnixInfo
.Capability
)))
885 rc
= CIFSSMBQFSPosixInfo(xid
, tcon
, buf
);
888 * Only need to call the old QFSInfo if failed on newer one,
891 if (rc
&& (tcon
->ses
->capabilities
& CAP_NT_SMBS
))
892 rc
= CIFSSMBQFSInfo(xid
, tcon
, buf
);
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
900 rc
= SMBOldQFSInfo(xid
, tcon
, buf
);
905 cifs_mand_lock(const unsigned int xid
, struct cifsFileInfo
*cfile
, __u64 offset
,
906 __u64 length
, __u32 type
, int lock
, int unlock
, bool wait
)
908 return CIFSSMBLock(xid
, tlink_tcon(cfile
->tlink
), cfile
->fid
.netfid
,
909 current
->tgid
, length
, offset
, unlock
, lock
,
910 (__u8
)type
, wait
, 0);
914 cifs_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
)
918 #ifdef CONFIG_CIFS_DFS_UPCALL
920 struct dfs_info3_param referral
= {0};
922 rc
= get_dfs_path(xid
, tcon
->ses
, searchName
, nls_codepage
, &referral
,
926 *symlinkinfo
= kstrndup(referral
.node_name
,
927 strlen(referral
.node_name
),
929 free_dfs_info_param(&referral
);
934 #else /* No DFS support */
940 cifs_query_symlink(const unsigned int xid
, struct cifs_tcon
*tcon
,
941 struct cifs_sb_info
*cifs_sb
, const char *full_path
,
942 char **target_path
, bool is_reparse_point
)
947 struct cifs_open_parms oparms
;
949 cifs_dbg(FYI
, "%s: path: %s\n", __func__
, full_path
);
951 if (is_reparse_point
) {
952 cifs_dbg(VFS
, "reparse points not handled for SMB1 symlinks\n");
956 /* Check for unix extensions */
957 if (cap_unix(tcon
->ses
)) {
958 rc
= CIFSSMBUnixQuerySymLink(xid
, tcon
, full_path
, target_path
,
960 cifs_remap(cifs_sb
));
962 rc
= cifs_unix_dfs_readlink(xid
, tcon
, full_path
,
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
;
976 oparms
.reconnect
= false;
978 rc
= CIFS_open(xid
, &oparms
, &oplock
, NULL
);
982 rc
= CIFSSMBQuerySymLink(xid
, tcon
, fid
.netfid
, target_path
,
987 convert_delimiter(*target_path
, '/');
989 CIFSSMBClose(xid
, tcon
, fid
.netfid
);
992 cifs_dbg(FYI
, "%s: target path: %s\n", __func__
, *target_path
);
997 cifs_is_read_op(__u32 oplock
)
999 return oplock
== OPLOCK_READ
;
1003 cifs_wp_retry_size(struct inode
*inode
)
1005 return CIFS_SB(inode
->i_sb
)->wsize
;
1009 cifs_dir_needs_close(struct cifsFileInfo
*cfile
)
1011 return !cfile
->srch_inf
.endOfSearch
&& !cfile
->invalidHandle
;
1015 cifs_can_echo(struct TCP_Server_Info
*server
)
1017 if (server
->tcpStatus
== CifsGood
)
1024 cifs_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
)
1028 struct cifs_sb_info
*cifs_sb
= CIFS_SB(inode
->i_sb
);
1029 struct inode
*newinode
= NULL
;
1031 int create_options
= CREATE_NOT_DIR
| CREATE_OPTION_SPECIAL
;
1032 FILE_ALL_INFO
*buf
= NULL
;
1033 struct cifs_io_parms io_parms
;
1035 struct cifs_fid fid
;
1036 struct cifs_open_parms oparms
;
1037 unsigned int bytes_written
;
1038 struct win_dev
*pdev
;
1041 if (tcon
->unix_ext
) {
1043 * SMB1 Unix Extensions: requires server support but
1044 * works with all special files
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
,
1053 if (cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_SET_UID
) {
1054 args
.uid
= current_fsuid();
1055 args
.gid
= current_fsgid();
1057 args
.uid
= INVALID_UID
; /* no change */
1058 args
.gid
= INVALID_GID
; /* no change */
1060 rc
= CIFSSMBUnixSetPathInfo(xid
, tcon
, full_path
, &args
,
1062 cifs_remap(cifs_sb
));
1066 rc
= cifs_get_inode_info_unix(&newinode
, full_path
,
1070 d_instantiate(dentry
, newinode
);
1075 * SMB1 SFU emulation: should work with all servers, but only
1076 * support block and char device (no socket & fifo)
1078 if (!(cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_UNX_EMUL
))
1081 if (!S_ISCHR(mode
) && !S_ISBLK(mode
))
1084 cifs_dbg(FYI
, "sfu compat create special file\n");
1086 buf
= kmalloc(sizeof(FILE_ALL_INFO
), GFP_KERNEL
);
1092 if (backup_cred(cifs_sb
))
1093 create_options
|= CREATE_OPEN_BACKUP_INTENT
;
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
;
1102 oparms
.reconnect
= false;
1104 if (tcon
->ses
->server
->oplocks
)
1105 oplock
= REQ_OPLOCK
;
1108 rc
= tcon
->ses
->server
->ops
->open(xid
, &oparms
, &oplock
, buf
);
1113 * BB Do not bother to decode buf since no local inode yet to put
1114 * timestamps in, but we can reuse it safely.
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);
1137 tcon
->ses
->server
->ops
->close(xid
, tcon
, &fid
);
1140 /* FIXME: add code here to set EAs */
1148 struct smb_version_operations smb1_operations
= {
1149 .send_cancel
= send_nt_cancel
,
1150 .compare_fids
= cifs_compare_fids
,
1151 .setup_request
= cifs_setup_request
,
1152 .setup_async_request
= cifs_setup_async_request
,
1153 .check_receive
= cifs_check_receive
,
1154 .add_credits
= cifs_add_credits
,
1155 .set_credits
= cifs_set_credits
,
1156 .get_credits_field
= cifs_get_credits_field
,
1157 .get_credits
= cifs_get_credits
,
1158 .wait_mtu_credits
= cifs_wait_mtu_credits
,
1159 .get_next_mid
= cifs_get_next_mid
,
1160 .read_data_offset
= cifs_read_data_offset
,
1161 .read_data_length
= cifs_read_data_length
,
1162 .map_error
= map_smb_to_linux_error
,
1163 .find_mid
= cifs_find_mid
,
1164 .check_message
= checkSMB
,
1165 .dump_detail
= cifs_dump_detail
,
1166 .clear_stats
= cifs_clear_stats
,
1167 .print_stats
= cifs_print_stats
,
1168 .is_oplock_break
= is_valid_oplock_break
,
1169 .downgrade_oplock
= cifs_downgrade_oplock
,
1170 .check_trans2
= cifs_check_trans2
,
1171 .need_neg
= cifs_need_neg
,
1172 .negotiate
= cifs_negotiate
,
1173 .negotiate_wsize
= cifs_negotiate_wsize
,
1174 .negotiate_rsize
= cifs_negotiate_rsize
,
1175 .sess_setup
= CIFS_SessSetup
,
1176 .logoff
= CIFSSMBLogoff
,
1177 .tree_connect
= CIFSTCon
,
1178 .tree_disconnect
= CIFSSMBTDis
,
1179 .get_dfs_refer
= CIFSGetDFSRefer
,
1180 .qfs_tcon
= cifs_qfs_tcon
,
1181 .is_path_accessible
= cifs_is_path_accessible
,
1182 .can_echo
= cifs_can_echo
,
1183 .query_path_info
= cifs_query_path_info
,
1184 .query_file_info
= cifs_query_file_info
,
1185 .get_srv_inum
= cifs_get_srv_inum
,
1186 .set_path_size
= CIFSSMBSetEOF
,
1187 .set_file_size
= CIFSSMBSetFileSize
,
1188 .set_file_info
= smb_set_file_info
,
1189 .set_compression
= cifs_set_compression
,
1190 .echo
= CIFSSMBEcho
,
1191 .mkdir
= CIFSSMBMkDir
,
1192 .mkdir_setinfo
= cifs_mkdir_setinfo
,
1193 .rmdir
= CIFSSMBRmDir
,
1194 .unlink
= CIFSSMBDelFile
,
1195 .rename_pending_delete
= cifs_rename_pending_delete
,
1196 .rename
= CIFSSMBRename
,
1197 .create_hardlink
= CIFSCreateHardLink
,
1198 .query_symlink
= cifs_query_symlink
,
1199 .open
= cifs_open_file
,
1200 .set_fid
= cifs_set_fid
,
1201 .close
= cifs_close_file
,
1202 .flush
= cifs_flush_file
,
1203 .async_readv
= cifs_async_readv
,
1204 .async_writev
= cifs_async_writev
,
1205 .sync_read
= cifs_sync_read
,
1206 .sync_write
= cifs_sync_write
,
1207 .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
,
1211 .oplock_response
= cifs_oplock_response
,
1212 .queryfs
= cifs_queryfs
,
1213 .mand_lock
= cifs_mand_lock
,
1214 .mand_unlock_range
= cifs_unlock_range
,
1215 .push_mand_locks
= cifs_push_mandatory_locks
,
1216 .query_mf_symlink
= cifs_query_mf_symlink
,
1217 .create_mf_symlink
= cifs_create_mf_symlink
,
1218 .is_read_op
= cifs_is_read_op
,
1219 .wp_retry_size
= cifs_wp_retry_size
,
1220 .dir_needs_close
= cifs_dir_needs_close
,
1221 .select_sectype
= cifs_select_sectype
,
1222 #ifdef CONFIG_CIFS_XATTR
1223 .query_all_EAs
= CIFSSMBQAllEAs
,
1224 .set_EA
= CIFSSMBSetEA
,
1225 #endif /* CIFS_XATTR */
1226 #ifdef CONFIG_CIFS_ACL
1227 .get_acl
= get_cifs_acl
,
1228 .get_acl_by_fid
= get_cifs_acl_by_fid
,
1229 .set_acl
= set_cifs_acl
,
1230 #endif /* CIFS_ACL */
1231 .make_node
= cifs_make_node
,
1234 struct smb_version_values smb1_values
= {
1235 .version_string
= SMB1_VERSION_STRING
,
1236 .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,
1240 .header_preamble_size
= 4,
1241 .header_size
= sizeof(struct smb_hdr
),
1242 .max_header_size
= MAX_CIFS_HDR_SIZE
,
1243 .read_rsp_size
= sizeof(READ_RSP
),
1244 .lock_cmd
= cpu_to_le16(SMB_COM_LOCKING_ANDX
),
1245 .cap_unix
= CAP_UNIX
,
1246 .cap_nt_find
= CAP_NT_SMBS
| CAP_NT_FIND
,
1247 .cap_large_files
= CAP_LARGE_FILES
,
1248 .signing_enabled
= SECMODE_SIGN_ENABLED
,
1249 .signing_required
= SECMODE_SIGN_REQUIRED
,