Commit | Line | Data |
---|---|---|
b6a3d1b7 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
23db65f5 JL |
2 | /* |
3 | * SMB1 (CIFS) version specific operations | |
4 | * | |
5 | * Copyright (c) 2012, Jeff Layton <jlayton@redhat.com> | |
23db65f5 JL |
6 | */ |
7 | ||
24985c53 | 8 | #include <linux/pagemap.h> |
76ec5e33 | 9 | #include <linux/vfs.h> |
23db65f5 | 10 | #include "cifsglob.h" |
121b046a JL |
11 | #include "cifsproto.h" |
12 | #include "cifs_debug.h" | |
106dc538 | 13 | #include "cifspdu.h" |
2baa2682 | 14 | #include "cifs_unicode.h" |
121b046a JL |
15 | |
16 | /* | |
17 | * An NT cancel request header looks just like the original request except: | |
18 | * | |
19 | * The Command is SMB_COM_NT_CANCEL | |
20 | * The WordCount is zeroed out | |
21 | * The ByteCount is zeroed out | |
22 | * | |
23 | * This function mangles an existing request buffer into a | |
24 | * SMB_COM_NT_CANCEL request and then sends it. | |
25 | */ | |
26 | static int | |
fb2036d8 | 27 | send_nt_cancel(struct TCP_Server_Info *server, struct smb_rqst *rqst, |
121b046a JL |
28 | struct mid_q_entry *mid) |
29 | { | |
30 | int rc = 0; | |
fb2036d8 | 31 | struct smb_hdr *in_buf = (struct smb_hdr *)rqst->rq_iov[0].iov_base; |
121b046a JL |
32 | |
33 | /* -4 for RFC1001 length and +2 for BCC field */ | |
34 | in_buf->smb_buf_length = cpu_to_be32(sizeof(struct smb_hdr) - 4 + 2); | |
35 | in_buf->Command = SMB_COM_NT_CANCEL; | |
36 | in_buf->WordCount = 0; | |
37 | put_bcc(0, in_buf); | |
38 | ||
39 | mutex_lock(&server->srv_mutex); | |
40 | rc = cifs_sign_smb(in_buf, server, &mid->sequence_number); | |
41 | if (rc) { | |
42 | mutex_unlock(&server->srv_mutex); | |
43 | return rc; | |
44 | } | |
31efee60 JL |
45 | |
46 | /* | |
47 | * The response to this call was already factored into the sequence | |
48 | * number when the call went out, so we must adjust it back downward | |
49 | * after signing here. | |
50 | */ | |
51 | --server->sequence_number; | |
121b046a | 52 | rc = smb_send(server, in_buf, be32_to_cpu(in_buf->smb_buf_length)); |
ad313cb8 JL |
53 | if (rc < 0) |
54 | server->sequence_number--; | |
55 | ||
121b046a JL |
56 | mutex_unlock(&server->srv_mutex); |
57 | ||
f96637be | 58 | cifs_dbg(FYI, "issued NT_CANCEL for mid %u, rc = %d\n", |
3d378d3f | 59 | get_mid(in_buf), rc); |
121b046a JL |
60 | |
61 | return rc; | |
62 | } | |
23db65f5 | 63 | |
55157dfb PS |
64 | static bool |
65 | cifs_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2) | |
66 | { | |
4b4de76e | 67 | return ob1->fid.netfid == ob2->fid.netfid; |
55157dfb PS |
68 | } |
69 | ||
eb378711 PS |
70 | static unsigned int |
71 | cifs_read_data_offset(char *buf) | |
72 | { | |
73 | READ_RSP *rsp = (READ_RSP *)buf; | |
74 | return le16_to_cpu(rsp->DataOffset); | |
75 | } | |
76 | ||
77 | static unsigned int | |
74dcf418 | 78 | cifs_read_data_length(char *buf, bool in_remaining) |
eb378711 PS |
79 | { |
80 | READ_RSP *rsp = (READ_RSP *)buf; | |
74dcf418 LL |
81 | /* It's a bug reading remaining data for SMB1 packets */ |
82 | WARN_ON(in_remaining); | |
eb378711 PS |
83 | return (le16_to_cpu(rsp->DataLengthHigh) << 16) + |
84 | le16_to_cpu(rsp->DataLength); | |
85 | } | |
86 | ||
8aa26f3e PS |
87 | static struct mid_q_entry * |
88 | cifs_find_mid(struct TCP_Server_Info *server, char *buffer) | |
89 | { | |
90 | struct smb_hdr *buf = (struct smb_hdr *)buffer; | |
91 | struct mid_q_entry *mid; | |
92 | ||
93 | spin_lock(&GlobalMid_Lock); | |
94 | list_for_each_entry(mid, &server->pending_mid_q, qhead) { | |
3d378d3f | 95 | if (compare_mid(mid->mid, buf) && |
8aa26f3e PS |
96 | mid->mid_state == MID_REQUEST_SUBMITTED && |
97 | le16_to_cpu(mid->command) == buf->Command) { | |
696e420b | 98 | kref_get(&mid->refcount); |
8aa26f3e PS |
99 | spin_unlock(&GlobalMid_Lock); |
100 | return mid; | |
101 | } | |
102 | } | |
103 | spin_unlock(&GlobalMid_Lock); | |
104 | return NULL; | |
105 | } | |
106 | ||
45275789 | 107 | static void |
335b7b62 PS |
108 | cifs_add_credits(struct TCP_Server_Info *server, |
109 | const struct cifs_credits *credits, const int optype) | |
45275789 PS |
110 | { |
111 | spin_lock(&server->req_lock); | |
335b7b62 | 112 | server->credits += credits->value; |
45275789 PS |
113 | server->in_flight--; |
114 | spin_unlock(&server->req_lock); | |
115 | wake_up(&server->request_q); | |
116 | } | |
117 | ||
118 | static void | |
119 | cifs_set_credits(struct TCP_Server_Info *server, const int val) | |
120 | { | |
121 | spin_lock(&server->req_lock); | |
122 | server->credits = val; | |
123 | server->oplocks = val > 1 ? enable_oplocks : false; | |
124 | spin_unlock(&server->req_lock); | |
125 | } | |
126 | ||
127 | static int * | |
a891f0f8 | 128 | cifs_get_credits_field(struct TCP_Server_Info *server, const int optype) |
45275789 PS |
129 | { |
130 | return &server->credits; | |
131 | } | |
132 | ||
a891f0f8 PS |
133 | static unsigned int |
134 | cifs_get_credits(struct mid_q_entry *mid) | |
135 | { | |
136 | return 1; | |
137 | } | |
138 | ||
88257360 PS |
139 | /* |
140 | * Find a free multiplex id (SMB mid). Otherwise there could be | |
141 | * mid collisions which might cause problems, demultiplexing the | |
142 | * wrong response to this request. Multiplex ids could collide if | |
143 | * one of a series requests takes much longer than the others, or | |
144 | * if a very large number of long lived requests (byte range | |
145 | * locks or FindNotify requests) are pending. No more than | |
146 | * 64K-1 requests can be outstanding at one time. If no | |
147 | * mids are available, return zero. A future optimization | |
148 | * could make the combination of mids and uid the key we use | |
149 | * to demultiplex on (rather than mid alone). | |
150 | * In addition to the above check, the cifs demultiplex | |
151 | * code already used the command code as a secondary | |
152 | * check of the frame and if signing is negotiated the | |
153 | * response would be discarded if the mid were the same | |
154 | * but the signature was wrong. Since the mid is not put in the | |
155 | * pending queue until later (when it is about to be dispatched) | |
156 | * we do have to limit the number of outstanding requests | |
157 | * to somewhat less than 64K-1 although it is hard to imagine | |
158 | * so many threads being in the vfs at one time. | |
159 | */ | |
160 | static __u64 | |
161 | cifs_get_next_mid(struct TCP_Server_Info *server) | |
162 | { | |
163 | __u64 mid = 0; | |
164 | __u16 last_mid, cur_mid; | |
165 | bool collision; | |
166 | ||
167 | spin_lock(&GlobalMid_Lock); | |
168 | ||
169 | /* mid is 16 bit only for CIFS/SMB */ | |
170 | cur_mid = (__u16)((server->CurrentMid) & 0xffff); | |
171 | /* we do not want to loop forever */ | |
172 | last_mid = cur_mid; | |
173 | cur_mid++; | |
174 | ||
175 | /* | |
176 | * This nested loop looks more expensive than it is. | |
177 | * In practice the list of pending requests is short, | |
178 | * fewer than 50, and the mids are likely to be unique | |
179 | * on the first pass through the loop unless some request | |
180 | * takes longer than the 64 thousand requests before it | |
181 | * (and it would also have to have been a request that | |
182 | * did not time out). | |
183 | */ | |
184 | while (cur_mid != last_mid) { | |
185 | struct mid_q_entry *mid_entry; | |
186 | unsigned int num_mids; | |
187 | ||
188 | collision = false; | |
189 | if (cur_mid == 0) | |
190 | cur_mid++; | |
191 | ||
192 | num_mids = 0; | |
193 | list_for_each_entry(mid_entry, &server->pending_mid_q, qhead) { | |
194 | ++num_mids; | |
195 | if (mid_entry->mid == cur_mid && | |
196 | mid_entry->mid_state == MID_REQUEST_SUBMITTED) { | |
197 | /* This mid is in use, try a different one */ | |
198 | collision = true; | |
199 | break; | |
200 | } | |
201 | } | |
202 | ||
203 | /* | |
204 | * if we have more than 32k mids in the list, then something | |
205 | * is very wrong. Possibly a local user is trying to DoS the | |
206 | * box by issuing long-running calls and SIGKILL'ing them. If | |
207 | * we get to 2^16 mids then we're in big trouble as this | |
208 | * function could loop forever. | |
209 | * | |
210 | * Go ahead and assign out the mid in this situation, but force | |
211 | * an eventual reconnect to clean out the pending_mid_q. | |
212 | */ | |
213 | if (num_mids > 32768) | |
214 | server->tcpStatus = CifsNeedReconnect; | |
215 | ||
216 | if (!collision) { | |
217 | mid = (__u64)cur_mid; | |
218 | server->CurrentMid = mid; | |
219 | break; | |
220 | } | |
221 | cur_mid++; | |
222 | } | |
223 | spin_unlock(&GlobalMid_Lock); | |
224 | return mid; | |
225 | } | |
226 | ||
316cf94a PS |
227 | /* |
228 | return codes: | |
229 | 0 not a transact2, or all data present | |
230 | >0 transact2 with that much data missing | |
231 | -EINVAL invalid transact2 | |
232 | */ | |
233 | static int | |
234 | check2ndT2(char *buf) | |
235 | { | |
236 | struct smb_hdr *pSMB = (struct smb_hdr *)buf; | |
237 | struct smb_t2_rsp *pSMBt; | |
238 | int remaining; | |
239 | __u16 total_data_size, data_in_this_rsp; | |
240 | ||
241 | if (pSMB->Command != SMB_COM_TRANSACTION2) | |
242 | return 0; | |
243 | ||
244 | /* check for plausible wct, bcc and t2 data and parm sizes */ | |
245 | /* check for parm and data offset going beyond end of smb */ | |
246 | if (pSMB->WordCount != 10) { /* coalesce_t2 depends on this */ | |
f96637be | 247 | cifs_dbg(FYI, "invalid transact2 word count\n"); |
316cf94a PS |
248 | return -EINVAL; |
249 | } | |
250 | ||
251 | pSMBt = (struct smb_t2_rsp *)pSMB; | |
252 | ||
253 | total_data_size = get_unaligned_le16(&pSMBt->t2_rsp.TotalDataCount); | |
254 | data_in_this_rsp = get_unaligned_le16(&pSMBt->t2_rsp.DataCount); | |
255 | ||
256 | if (total_data_size == data_in_this_rsp) | |
257 | return 0; | |
258 | else if (total_data_size < data_in_this_rsp) { | |
f96637be JP |
259 | cifs_dbg(FYI, "total data %d smaller than data in frame %d\n", |
260 | total_data_size, data_in_this_rsp); | |
316cf94a PS |
261 | return -EINVAL; |
262 | } | |
263 | ||
264 | remaining = total_data_size - data_in_this_rsp; | |
265 | ||
f96637be JP |
266 | cifs_dbg(FYI, "missing %d bytes from transact2, check next response\n", |
267 | remaining); | |
316cf94a | 268 | if (total_data_size > CIFSMaxBufSize) { |
f96637be JP |
269 | cifs_dbg(VFS, "TotalDataSize %d is over maximum buffer %d\n", |
270 | total_data_size, CIFSMaxBufSize); | |
316cf94a PS |
271 | return -EINVAL; |
272 | } | |
273 | return remaining; | |
274 | } | |
275 | ||
276 | static int | |
277 | coalesce_t2(char *second_buf, struct smb_hdr *target_hdr) | |
278 | { | |
279 | struct smb_t2_rsp *pSMBs = (struct smb_t2_rsp *)second_buf; | |
280 | struct smb_t2_rsp *pSMBt = (struct smb_t2_rsp *)target_hdr; | |
281 | char *data_area_of_tgt; | |
282 | char *data_area_of_src; | |
283 | int remaining; | |
284 | unsigned int byte_count, total_in_tgt; | |
285 | __u16 tgt_total_cnt, src_total_cnt, total_in_src; | |
286 | ||
287 | src_total_cnt = get_unaligned_le16(&pSMBs->t2_rsp.TotalDataCount); | |
288 | tgt_total_cnt = get_unaligned_le16(&pSMBt->t2_rsp.TotalDataCount); | |
289 | ||
290 | if (tgt_total_cnt != src_total_cnt) | |
f96637be JP |
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); | |
316cf94a PS |
293 | |
294 | total_in_tgt = get_unaligned_le16(&pSMBt->t2_rsp.DataCount); | |
295 | ||
296 | remaining = tgt_total_cnt - total_in_tgt; | |
297 | ||
298 | if (remaining < 0) { | |
259594be | 299 | cifs_dbg(FYI, "Server sent too much data. tgt_total_cnt=%hu total_in_tgt=%u\n", |
f96637be | 300 | tgt_total_cnt, total_in_tgt); |
316cf94a PS |
301 | return -EPROTO; |
302 | } | |
303 | ||
304 | if (remaining == 0) { | |
305 | /* nothing to do, ignore */ | |
f96637be | 306 | cifs_dbg(FYI, "no more data remains\n"); |
316cf94a PS |
307 | return 0; |
308 | } | |
309 | ||
310 | total_in_src = get_unaligned_le16(&pSMBs->t2_rsp.DataCount); | |
311 | if (remaining < total_in_src) | |
f96637be | 312 | cifs_dbg(FYI, "transact2 2nd response contains too much data\n"); |
316cf94a PS |
313 | |
314 | /* find end of first SMB data area */ | |
315 | data_area_of_tgt = (char *)&pSMBt->hdr.Protocol + | |
316 | get_unaligned_le16(&pSMBt->t2_rsp.DataOffset); | |
317 | ||
318 | /* validate target area */ | |
319 | data_area_of_src = (char *)&pSMBs->hdr.Protocol + | |
320 | get_unaligned_le16(&pSMBs->t2_rsp.DataOffset); | |
321 | ||
322 | data_area_of_tgt += total_in_tgt; | |
323 | ||
324 | total_in_tgt += total_in_src; | |
325 | /* is the result too big for the field? */ | |
326 | if (total_in_tgt > USHRT_MAX) { | |
f96637be JP |
327 | cifs_dbg(FYI, "coalesced DataCount too large (%u)\n", |
328 | total_in_tgt); | |
316cf94a PS |
329 | return -EPROTO; |
330 | } | |
331 | put_unaligned_le16(total_in_tgt, &pSMBt->t2_rsp.DataCount); | |
332 | ||
333 | /* fix up the BCC */ | |
334 | byte_count = get_bcc(target_hdr); | |
335 | byte_count += total_in_src; | |
336 | /* is the result too big for the field? */ | |
337 | if (byte_count > USHRT_MAX) { | |
f96637be | 338 | cifs_dbg(FYI, "coalesced BCC too large (%u)\n", byte_count); |
316cf94a PS |
339 | return -EPROTO; |
340 | } | |
341 | put_bcc(byte_count, target_hdr); | |
342 | ||
343 | byte_count = be32_to_cpu(target_hdr->smb_buf_length); | |
344 | byte_count += total_in_src; | |
345 | /* don't allow buffer to overflow */ | |
346 | if (byte_count > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) { | |
f96637be JP |
347 | cifs_dbg(FYI, "coalesced BCC exceeds buffer size (%u)\n", |
348 | byte_count); | |
316cf94a PS |
349 | return -ENOBUFS; |
350 | } | |
351 | target_hdr->smb_buf_length = cpu_to_be32(byte_count); | |
352 | ||
353 | /* copy second buffer into end of first buffer */ | |
354 | memcpy(data_area_of_tgt, data_area_of_src, total_in_src); | |
355 | ||
356 | if (remaining != total_in_src) { | |
357 | /* more responses to go */ | |
f96637be | 358 | cifs_dbg(FYI, "waiting for more secondary responses\n"); |
316cf94a PS |
359 | return 1; |
360 | } | |
361 | ||
362 | /* we are done */ | |
f96637be | 363 | cifs_dbg(FYI, "found the last secondary response\n"); |
316cf94a PS |
364 | return 0; |
365 | } | |
366 | ||
c11f1df5 SP |
367 | static void |
368 | cifs_downgrade_oplock(struct TCP_Server_Info *server, | |
369 | struct cifsInodeInfo *cinode, bool set_level2) | |
370 | { | |
371 | if (set_level2) | |
372 | cifs_set_oplock_level(cinode, OPLOCK_READ); | |
373 | else | |
374 | cifs_set_oplock_level(cinode, 0); | |
375 | } | |
376 | ||
316cf94a PS |
377 | static bool |
378 | cifs_check_trans2(struct mid_q_entry *mid, struct TCP_Server_Info *server, | |
379 | char *buf, int malformed) | |
380 | { | |
381 | if (malformed) | |
382 | return false; | |
383 | if (check2ndT2(buf) <= 0) | |
384 | return false; | |
385 | mid->multiRsp = true; | |
386 | if (mid->resp_buf) { | |
387 | /* merge response - fix up 1st*/ | |
388 | malformed = coalesce_t2(buf, mid->resp_buf); | |
389 | if (malformed > 0) | |
390 | return true; | |
391 | /* All parts received or packet is malformed. */ | |
392 | mid->multiEnd = true; | |
393 | dequeue_mid(mid, malformed); | |
394 | return true; | |
395 | } | |
396 | if (!server->large_buf) { | |
397 | /*FIXME: switch to already allocated largebuf?*/ | |
f96637be | 398 | cifs_dbg(VFS, "1st trans2 resp needs bigbuf\n"); |
316cf94a PS |
399 | } else { |
400 | /* Have first buffer */ | |
401 | mid->resp_buf = buf; | |
402 | mid->large_buf = true; | |
403 | server->bigbuf = NULL; | |
404 | } | |
405 | return true; | |
406 | } | |
407 | ||
286170aa PS |
408 | static bool |
409 | cifs_need_neg(struct TCP_Server_Info *server) | |
410 | { | |
411 | return server->maxBuf == 0; | |
412 | } | |
413 | ||
414 | static int | |
415 | cifs_negotiate(const unsigned int xid, struct cifs_ses *ses) | |
416 | { | |
417 | int rc; | |
418 | rc = CIFSSMBNegotiate(xid, ses); | |
419 | if (rc == -EAGAIN) { | |
420 | /* retry only once on 1st time connection */ | |
421 | set_credits(ses->server, 1); | |
422 | rc = CIFSSMBNegotiate(xid, ses); | |
423 | if (rc == -EAGAIN) | |
424 | rc = -EHOSTDOWN; | |
425 | } | |
426 | return rc; | |
427 | } | |
428 | ||
24985c53 PS |
429 | static unsigned int |
430 | cifs_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info) | |
431 | { | |
432 | __u64 unix_cap = le64_to_cpu(tcon->fsUnixInfo.Capability); | |
433 | struct TCP_Server_Info *server = tcon->ses->server; | |
434 | unsigned int wsize; | |
435 | ||
436 | /* start with specified wsize, or default */ | |
437 | if (volume_info->wsize) | |
438 | wsize = volume_info->wsize; | |
439 | else if (tcon->unix_ext && (unix_cap & CIFS_UNIX_LARGE_WRITE_CAP)) | |
440 | wsize = CIFS_DEFAULT_IOSIZE; | |
441 | else | |
442 | wsize = CIFS_DEFAULT_NON_POSIX_WSIZE; | |
443 | ||
444 | /* can server support 24-bit write sizes? (via UNIX extensions) */ | |
445 | if (!tcon->unix_ext || !(unix_cap & CIFS_UNIX_LARGE_WRITE_CAP)) | |
446 | wsize = min_t(unsigned int, wsize, CIFS_MAX_RFC1002_WSIZE); | |
447 | ||
448 | /* | |
449 | * no CAP_LARGE_WRITE_X or is signing enabled without CAP_UNIX set? | |
450 | * Limit it to max buffer offered by the server, minus the size of the | |
451 | * WRITEX header, not including the 4 byte RFC1001 length. | |
452 | */ | |
453 | if (!(server->capabilities & CAP_LARGE_WRITE_X) || | |
38d77c50 | 454 | (!(server->capabilities & CAP_UNIX) && server->sign)) |
24985c53 PS |
455 | wsize = min_t(unsigned int, wsize, |
456 | server->maxBuf - sizeof(WRITE_REQ) + 4); | |
457 | ||
24985c53 PS |
458 | /* hard limit of CIFS_MAX_WSIZE */ |
459 | wsize = min_t(unsigned int, wsize, CIFS_MAX_WSIZE); | |
460 | ||
461 | return wsize; | |
462 | } | |
463 | ||
464 | static unsigned int | |
465 | cifs_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info) | |
466 | { | |
467 | __u64 unix_cap = le64_to_cpu(tcon->fsUnixInfo.Capability); | |
468 | struct TCP_Server_Info *server = tcon->ses->server; | |
469 | unsigned int rsize, defsize; | |
470 | ||
471 | /* | |
472 | * Set default value... | |
473 | * | |
474 | * HACK alert! Ancient servers have very small buffers. Even though | |
475 | * MS-CIFS indicates that servers are only limited by the client's | |
476 | * bufsize for reads, testing against win98se shows that it throws | |
477 | * INVALID_PARAMETER errors if you try to request too large a read. | |
478 | * OS/2 just sends back short reads. | |
479 | * | |
480 | * If the server doesn't advertise CAP_LARGE_READ_X, then assume that | |
481 | * it can't handle a read request larger than its MaxBufferSize either. | |
482 | */ | |
483 | if (tcon->unix_ext && (unix_cap & CIFS_UNIX_LARGE_READ_CAP)) | |
484 | defsize = CIFS_DEFAULT_IOSIZE; | |
485 | else if (server->capabilities & CAP_LARGE_READ_X) | |
486 | defsize = CIFS_DEFAULT_NON_POSIX_RSIZE; | |
487 | else | |
488 | defsize = server->maxBuf - sizeof(READ_RSP); | |
489 | ||
490 | rsize = volume_info->rsize ? volume_info->rsize : defsize; | |
491 | ||
492 | /* | |
493 | * no CAP_LARGE_READ_X? Then MS-CIFS states that we must limit this to | |
494 | * the client's MaxBufferSize. | |
495 | */ | |
496 | if (!(server->capabilities & CAP_LARGE_READ_X)) | |
497 | rsize = min_t(unsigned int, CIFSMaxBufSize, rsize); | |
498 | ||
24985c53 PS |
499 | /* hard limit of CIFS_MAX_RSIZE */ |
500 | rsize = min_t(unsigned int, rsize, CIFS_MAX_RSIZE); | |
501 | ||
502 | return rsize; | |
503 | } | |
504 | ||
af4281dc PS |
505 | static void |
506 | cifs_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon) | |
507 | { | |
508 | CIFSSMBQFSDeviceInfo(xid, tcon); | |
509 | CIFSSMBQFSAttributeInfo(xid, tcon); | |
510 | } | |
511 | ||
68889f26 PS |
512 | static int |
513 | cifs_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon, | |
514 | struct cifs_sb_info *cifs_sb, const char *full_path) | |
515 | { | |
516 | int rc; | |
517 | FILE_ALL_INFO *file_info; | |
518 | ||
519 | file_info = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL); | |
520 | if (file_info == NULL) | |
521 | return -ENOMEM; | |
522 | ||
523 | rc = CIFSSMBQPathInfo(xid, tcon, full_path, file_info, | |
524 | 0 /* not legacy */, cifs_sb->local_nls, | |
2baa2682 | 525 | cifs_remap(cifs_sb)); |
68889f26 PS |
526 | |
527 | if (rc == -EOPNOTSUPP || rc == -EINVAL) | |
528 | rc = SMBQueryInformation(xid, tcon, full_path, file_info, | |
2baa2682 | 529 | cifs_sb->local_nls, cifs_remap(cifs_sb)); |
68889f26 PS |
530 | kfree(file_info); |
531 | return rc; | |
532 | } | |
533 | ||
1208ef1f PS |
534 | static int |
535 | cifs_query_path_info(const unsigned int xid, struct cifs_tcon *tcon, | |
536 | struct cifs_sb_info *cifs_sb, const char *full_path, | |
eb85d94b | 537 | FILE_ALL_INFO *data, bool *adjustTZ, bool *symlink) |
1208ef1f PS |
538 | { |
539 | int rc; | |
540 | ||
eb85d94b PS |
541 | *symlink = false; |
542 | ||
1208ef1f PS |
543 | /* could do find first instead but this returns more info */ |
544 | rc = CIFSSMBQPathInfo(xid, tcon, full_path, data, 0 /* not legacy */, | |
2baa2682 | 545 | cifs_sb->local_nls, cifs_remap(cifs_sb)); |
1208ef1f PS |
546 | /* |
547 | * BB optimize code so we do not make the above call when server claims | |
548 | * no NT SMB support and the above call failed at least once - set flag | |
549 | * in tcon or mount. | |
550 | */ | |
551 | if ((rc == -EOPNOTSUPP) || (rc == -EINVAL)) { | |
552 | rc = SMBQueryInformation(xid, tcon, full_path, data, | |
553 | cifs_sb->local_nls, | |
2baa2682 | 554 | cifs_remap(cifs_sb)); |
1208ef1f PS |
555 | *adjustTZ = true; |
556 | } | |
eb85d94b PS |
557 | |
558 | if (!rc && (le32_to_cpu(data->Attributes) & ATTR_REPARSE)) { | |
559 | int tmprc; | |
560 | int oplock = 0; | |
d81b8a40 PS |
561 | struct cifs_fid fid; |
562 | struct cifs_open_parms oparms; | |
563 | ||
564 | oparms.tcon = tcon; | |
565 | oparms.cifs_sb = cifs_sb; | |
566 | oparms.desired_access = FILE_READ_ATTRIBUTES; | |
567 | oparms.create_options = 0; | |
568 | oparms.disposition = FILE_OPEN; | |
569 | oparms.path = full_path; | |
570 | oparms.fid = &fid; | |
571 | oparms.reconnect = false; | |
eb85d94b PS |
572 | |
573 | /* Need to check if this is a symbolic link or not */ | |
d81b8a40 | 574 | tmprc = CIFS_open(xid, &oparms, &oplock, NULL); |
eb85d94b PS |
575 | if (tmprc == -EOPNOTSUPP) |
576 | *symlink = true; | |
19e81573 | 577 | else if (tmprc == 0) |
d81b8a40 | 578 | CIFSSMBClose(xid, tcon, fid.netfid); |
eb85d94b PS |
579 | } |
580 | ||
1208ef1f PS |
581 | return rc; |
582 | } | |
583 | ||
584 | static int | |
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) | |
588 | { | |
589 | /* | |
590 | * We can not use the IndexNumber field by default from Windows or | |
591 | * Samba (in ALL_INFO buf) but we can request it explicitly. The SNIA | |
592 | * CIFS spec claims that this value is unique within the scope of a | |
593 | * share, and the windows docs hint that it's actually unique | |
594 | * per-machine. | |
595 | * | |
596 | * There may be higher info levels that work but are there Windows | |
597 | * server or network appliances for which IndexNumber field is not | |
598 | * guaranteed unique? | |
599 | */ | |
600 | return CIFSGetSrvInodeNumber(xid, tcon, full_path, uniqueid, | |
601 | cifs_sb->local_nls, | |
2baa2682 | 602 | cifs_remap(cifs_sb)); |
1208ef1f PS |
603 | } |
604 | ||
4ad65044 PS |
605 | static int |
606 | cifs_query_file_info(const unsigned int xid, struct cifs_tcon *tcon, | |
607 | struct cifs_fid *fid, FILE_ALL_INFO *data) | |
608 | { | |
609 | return CIFSSMBQFileInfo(xid, tcon, fid->netfid, data); | |
610 | } | |
611 | ||
44c58186 PS |
612 | static void |
613 | cifs_clear_stats(struct cifs_tcon *tcon) | |
614 | { | |
44c58186 PS |
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); | |
44c58186 PS |
636 | } |
637 | ||
638 | static void | |
639 | cifs_print_stats(struct seq_file *m, struct cifs_tcon *tcon) | |
640 | { | |
44c58186 PS |
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)); | |
44c58186 PS |
672 | } |
673 | ||
f436720e PS |
674 | static void |
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) | |
678 | { | |
679 | FILE_BASIC_INFO info; | |
680 | struct cifsInodeInfo *cifsInode; | |
681 | u32 dosattrs; | |
682 | int rc; | |
683 | ||
684 | memset(&info, 0, sizeof(info)); | |
685 | cifsInode = CIFS_I(inode); | |
686 | dosattrs = cifsInode->cifsAttrs|ATTR_READONLY; | |
687 | info.Attributes = cpu_to_le32(dosattrs); | |
688 | rc = CIFSSMBSetPathInfo(xid, tcon, full_path, &info, cifs_sb->local_nls, | |
2baa2682 | 689 | cifs_remap(cifs_sb)); |
f436720e PS |
690 | if (rc == 0) |
691 | cifsInode->cifsAttrs = dosattrs; | |
692 | } | |
693 | ||
fb1214e4 | 694 | static int |
226730b4 PS |
695 | cifs_open_file(const unsigned int xid, struct cifs_open_parms *oparms, |
696 | __u32 *oplock, FILE_ALL_INFO *buf) | |
fb1214e4 | 697 | { |
226730b4 PS |
698 | if (!(oparms->tcon->ses->capabilities & CAP_NT_SMBS)) |
699 | return SMBLegacyOpen(xid, oparms->tcon, oparms->path, | |
700 | oparms->disposition, | |
701 | oparms->desired_access, | |
702 | oparms->create_options, | |
703 | &oparms->fid->netfid, oplock, buf, | |
704 | oparms->cifs_sb->local_nls, | |
2baa2682 | 705 | cifs_remap(oparms->cifs_sb)); |
d81b8a40 | 706 | return CIFS_open(xid, oparms, oplock, buf); |
fb1214e4 PS |
707 | } |
708 | ||
709 | static void | |
710 | cifs_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock) | |
711 | { | |
2b0143b5 | 712 | struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry)); |
fb1214e4 PS |
713 | cfile->fid.netfid = fid->netfid; |
714 | cifs_set_oplock_level(cinode, oplock); | |
18cceb6a | 715 | cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode); |
fb1214e4 PS |
716 | } |
717 | ||
760ad0ca | 718 | static void |
0ff78a22 PS |
719 | cifs_close_file(const unsigned int xid, struct cifs_tcon *tcon, |
720 | struct cifs_fid *fid) | |
721 | { | |
760ad0ca | 722 | CIFSSMBClose(xid, tcon, fid->netfid); |
0ff78a22 PS |
723 | } |
724 | ||
1d8c4c00 PS |
725 | static int |
726 | cifs_flush_file(const unsigned int xid, struct cifs_tcon *tcon, | |
727 | struct cifs_fid *fid) | |
728 | { | |
729 | return CIFSSMBFlush(xid, tcon, fid->netfid); | |
730 | } | |
731 | ||
f9c6e234 | 732 | static int |
db8b631d | 733 | cifs_sync_read(const unsigned int xid, struct cifs_fid *pfid, |
f9c6e234 PS |
734 | struct cifs_io_parms *parms, unsigned int *bytes_read, |
735 | char **buf, int *buf_type) | |
736 | { | |
db8b631d | 737 | parms->netfid = pfid->netfid; |
f9c6e234 PS |
738 | return CIFSSMBRead(xid, parms, bytes_read, buf, buf_type); |
739 | } | |
740 | ||
ba9ad725 | 741 | static int |
db8b631d | 742 | cifs_sync_write(const unsigned int xid, struct cifs_fid *pfid, |
ba9ad725 PS |
743 | struct cifs_io_parms *parms, unsigned int *written, |
744 | struct kvec *iov, unsigned long nr_segs) | |
745 | { | |
746 | ||
db8b631d | 747 | parms->netfid = pfid->netfid; |
ba9ad725 PS |
748 | return CIFSSMBWrite2(xid, parms, written, iov, nr_segs); |
749 | } | |
750 | ||
6bdf6dbd PS |
751 | static int |
752 | smb_set_file_info(struct inode *inode, const char *full_path, | |
753 | FILE_BASIC_INFO *buf, const unsigned int xid) | |
754 | { | |
755 | int oplock = 0; | |
756 | int rc; | |
6bdf6dbd | 757 | __u32 netpid; |
d81b8a40 PS |
758 | struct cifs_fid fid; |
759 | struct cifs_open_parms oparms; | |
6bdf6dbd PS |
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; | |
6bdf6dbd PS |
765 | |
766 | /* if the file is already open for write, just use that fileid */ | |
767 | open_file = find_writable_file(cinode, true); | |
768 | if (open_file) { | |
d81b8a40 | 769 | fid.netfid = open_file->fid.netfid; |
6bdf6dbd PS |
770 | netpid = open_file->pid; |
771 | tcon = tlink_tcon(open_file->tlink); | |
772 | goto set_via_filehandle; | |
773 | } | |
774 | ||
775 | tlink = cifs_sb_tlink(cifs_sb); | |
776 | if (IS_ERR(tlink)) { | |
777 | rc = PTR_ERR(tlink); | |
778 | tlink = NULL; | |
779 | goto out; | |
780 | } | |
781 | tcon = tlink_tcon(tlink); | |
782 | ||
281e2e7d | 783 | rc = CIFSSMBSetPathInfo(xid, tcon, full_path, buf, cifs_sb->local_nls, |
2baa2682 | 784 | cifs_remap(cifs_sb)); |
281e2e7d JL |
785 | if (rc == 0) { |
786 | cinode->cifsAttrs = le32_to_cpu(buf->Attributes); | |
787 | goto out; | |
788 | } else if (rc != -EOPNOTSUPP && rc != -EINVAL) { | |
789 | goto out; | |
6bdf6dbd PS |
790 | } |
791 | ||
d81b8a40 PS |
792 | oparms.tcon = tcon; |
793 | oparms.cifs_sb = cifs_sb; | |
794 | oparms.desired_access = SYNCHRONIZE | FILE_WRITE_ATTRIBUTES; | |
795 | oparms.create_options = CREATE_NOT_DIR; | |
796 | oparms.disposition = FILE_OPEN; | |
797 | oparms.path = full_path; | |
798 | oparms.fid = &fid; | |
799 | oparms.reconnect = false; | |
6bdf6dbd | 800 | |
d81b8a40 PS |
801 | cifs_dbg(FYI, "calling SetFileInfo since SetPathInfo for times not supported by this server\n"); |
802 | rc = CIFS_open(xid, &oparms, &oplock, NULL); | |
6bdf6dbd PS |
803 | if (rc != 0) { |
804 | if (rc == -EIO) | |
805 | rc = -EINVAL; | |
806 | goto out; | |
807 | } | |
808 | ||
809 | netpid = current->tgid; | |
810 | ||
811 | set_via_filehandle: | |
d81b8a40 | 812 | rc = CIFSSMBSetFileInfo(xid, tcon, buf, fid.netfid, netpid); |
6bdf6dbd PS |
813 | if (!rc) |
814 | cinode->cifsAttrs = le32_to_cpu(buf->Attributes); | |
815 | ||
816 | if (open_file == NULL) | |
d81b8a40 | 817 | CIFSSMBClose(xid, tcon, fid.netfid); |
6bdf6dbd PS |
818 | else |
819 | cifsFileInfo_put(open_file); | |
820 | out: | |
821 | if (tlink != NULL) | |
822 | cifs_put_tlink(tlink); | |
823 | return rc; | |
824 | } | |
825 | ||
c7f508a9 SF |
826 | static int |
827 | cifs_set_compression(const unsigned int xid, struct cifs_tcon *tcon, | |
828 | struct cifsFileInfo *cfile) | |
829 | { | |
830 | return CIFSSMB_set_compression(xid, tcon, cfile->fid.netfid); | |
831 | } | |
832 | ||
92fc65a7 PS |
833 | static int |
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) | |
838 | { | |
dcd87838 PS |
839 | int rc; |
840 | ||
841 | rc = CIFSFindFirst(xid, tcon, path, cifs_sb, | |
842 | &fid->netfid, search_flags, srch_inf, true); | |
843 | if (rc) | |
844 | cifs_dbg(FYI, "find first failed=%d\n", rc); | |
845 | return rc; | |
92fc65a7 PS |
846 | } |
847 | ||
848 | static int | |
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) | |
852 | { | |
853 | return CIFSFindNext(xid, tcon, fid->netfid, search_flags, srch_inf); | |
854 | } | |
855 | ||
856 | static int | |
857 | cifs_close_dir(const unsigned int xid, struct cifs_tcon *tcon, | |
858 | struct cifs_fid *fid) | |
859 | { | |
860 | return CIFSFindClose(xid, tcon, fid->netfid); | |
861 | } | |
862 | ||
95a3f2f3 PS |
863 | static int |
864 | cifs_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid, | |
865 | struct cifsInodeInfo *cinode) | |
866 | { | |
867 | return CIFSSMBLock(0, tcon, fid->netfid, current->tgid, 0, 0, 0, 0, | |
868 | LOCKING_ANDX_OPLOCK_RELEASE, false, | |
18cceb6a | 869 | CIFS_CACHE_READ(cinode) ? 1 : 0); |
95a3f2f3 PS |
870 | } |
871 | ||
76ec5e33 PS |
872 | static int |
873 | cifs_queryfs(const unsigned int xid, struct cifs_tcon *tcon, | |
874 | struct kstatfs *buf) | |
875 | { | |
876 | int rc = -EOPNOTSUPP; | |
877 | ||
878 | buf->f_type = CIFS_MAGIC_NUMBER; | |
879 | ||
880 | /* | |
881 | * We could add a second check for a QFS Unix capability bit | |
882 | */ | |
883 | if ((tcon->ses->capabilities & CAP_UNIX) && | |
884 | (CIFS_POSIX_EXTENSIONS & le64_to_cpu(tcon->fsUnixInfo.Capability))) | |
885 | rc = CIFSSMBQFSPosixInfo(xid, tcon, buf); | |
886 | ||
887 | /* | |
888 | * Only need to call the old QFSInfo if failed on newer one, | |
889 | * e.g. by OS/2. | |
890 | **/ | |
891 | if (rc && (tcon->ses->capabilities & CAP_NT_SMBS)) | |
892 | rc = CIFSSMBQFSInfo(xid, tcon, buf); | |
893 | ||
894 | /* | |
895 | * Some old Windows servers also do not support level 103, retry with | |
896 | * older level one if old server failed the previous call or we | |
897 | * bypassed it because we detected that this was an older LANMAN sess | |
898 | */ | |
899 | if (rc) | |
900 | rc = SMBOldQFSInfo(xid, tcon, buf); | |
901 | return rc; | |
902 | } | |
903 | ||
d39a4f71 PS |
904 | static int |
905 | cifs_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset, | |
906 | __u64 length, __u32 type, int lock, int unlock, bool wait) | |
907 | { | |
908 | return CIFSSMBLock(xid, tlink_tcon(cfile->tlink), cfile->fid.netfid, | |
909 | current->tgid, length, offset, unlock, lock, | |
910 | (__u8)type, wait, 0); | |
911 | } | |
912 | ||
924e3fa4 SP |
913 | static int |
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) | |
917 | { | |
918 | #ifdef CONFIG_CIFS_DFS_UPCALL | |
919 | int rc; | |
1c780228 | 920 | struct dfs_info3_param referral = {0}; |
924e3fa4 | 921 | |
1c780228 PA |
922 | rc = get_dfs_path(xid, tcon->ses, searchName, nls_codepage, &referral, |
923 | 0); | |
924e3fa4 | 924 | |
1c780228 PA |
925 | if (!rc) { |
926 | *symlinkinfo = kstrndup(referral.node_name, | |
927 | strlen(referral.node_name), | |
924e3fa4 | 928 | GFP_KERNEL); |
1c780228 | 929 | free_dfs_info_param(&referral); |
924e3fa4 SP |
930 | if (!*symlinkinfo) |
931 | rc = -ENOMEM; | |
924e3fa4 SP |
932 | } |
933 | return rc; | |
934 | #else /* No DFS support */ | |
935 | return -EREMOTE; | |
936 | #endif | |
937 | } | |
938 | ||
d244bf2d PS |
939 | static int |
940 | cifs_query_symlink(const unsigned int xid, struct cifs_tcon *tcon, | |
ebaf546a RS |
941 | struct cifs_sb_info *cifs_sb, const char *full_path, |
942 | char **target_path, bool is_reparse_point) | |
d244bf2d PS |
943 | { |
944 | int rc; | |
945 | int oplock = 0; | |
d81b8a40 PS |
946 | struct cifs_fid fid; |
947 | struct cifs_open_parms oparms; | |
d244bf2d PS |
948 | |
949 | cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path); | |
950 | ||
ebaf546a RS |
951 | if (is_reparse_point) { |
952 | cifs_dbg(VFS, "reparse points not handled for SMB1 symlinks\n"); | |
953 | return -EOPNOTSUPP; | |
954 | } | |
955 | ||
0ecdb4f5 SP |
956 | /* Check for unix extensions */ |
957 | if (cap_unix(tcon->ses)) { | |
958 | rc = CIFSSMBUnixQuerySymLink(xid, tcon, full_path, target_path, | |
bc8ebdc4 NA |
959 | cifs_sb->local_nls, |
960 | cifs_remap(cifs_sb)); | |
924e3fa4 SP |
961 | if (rc == -EREMOTE) |
962 | rc = cifs_unix_dfs_readlink(xid, tcon, full_path, | |
963 | target_path, | |
964 | cifs_sb->local_nls); | |
965 | ||
0ecdb4f5 SP |
966 | goto out; |
967 | } | |
968 | ||
d81b8a40 PS |
969 | oparms.tcon = tcon; |
970 | oparms.cifs_sb = cifs_sb; | |
971 | oparms.desired_access = FILE_READ_ATTRIBUTES; | |
972 | oparms.create_options = OPEN_REPARSE_POINT; | |
973 | oparms.disposition = FILE_OPEN; | |
974 | oparms.path = full_path; | |
975 | oparms.fid = &fid; | |
976 | oparms.reconnect = false; | |
977 | ||
978 | rc = CIFS_open(xid, &oparms, &oplock, NULL); | |
d244bf2d | 979 | if (rc) |
0ecdb4f5 | 980 | goto out; |
d244bf2d | 981 | |
d81b8a40 | 982 | rc = CIFSSMBQuerySymLink(xid, tcon, fid.netfid, target_path, |
d244bf2d | 983 | cifs_sb->local_nls); |
0ecdb4f5 SP |
984 | if (rc) |
985 | goto out_close; | |
d244bf2d PS |
986 | |
987 | convert_delimiter(*target_path, '/'); | |
0ecdb4f5 | 988 | out_close: |
d81b8a40 | 989 | CIFSSMBClose(xid, tcon, fid.netfid); |
0ecdb4f5 SP |
990 | out: |
991 | if (!rc) | |
992 | cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path); | |
d244bf2d PS |
993 | return rc; |
994 | } | |
995 | ||
53ef1016 PS |
996 | static bool |
997 | cifs_is_read_op(__u32 oplock) | |
998 | { | |
999 | return oplock == OPLOCK_READ; | |
1000 | } | |
1001 | ||
7f6c5008 PS |
1002 | static unsigned int |
1003 | cifs_wp_retry_size(struct inode *inode) | |
1004 | { | |
1005 | return CIFS_SB(inode->i_sb)->wsize; | |
1006 | } | |
1007 | ||
52755808 PS |
1008 | static bool |
1009 | cifs_dir_needs_close(struct cifsFileInfo *cfile) | |
1010 | { | |
1011 | return !cfile->srch_inf.endOfSearch && !cfile->invalidHandle; | |
1012 | } | |
1013 | ||
62a6cfdd SP |
1014 | static bool |
1015 | cifs_can_echo(struct TCP_Server_Info *server) | |
1016 | { | |
1017 | if (server->tcpStatus == CifsGood) | |
1018 | return true; | |
1019 | ||
1020 | return false; | |
1021 | } | |
1022 | ||
c847dccf AA |
1023 | static int |
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) | |
1027 | { | |
1028 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); | |
1029 | struct inode *newinode = NULL; | |
1030 | int rc = -EPERM; | |
1031 | int create_options = CREATE_NOT_DIR | CREATE_OPTION_SPECIAL; | |
1032 | FILE_ALL_INFO *buf = NULL; | |
1033 | struct cifs_io_parms io_parms; | |
1034 | __u32 oplock = 0; | |
1035 | struct cifs_fid fid; | |
1036 | struct cifs_open_parms oparms; | |
1037 | unsigned int bytes_written; | |
1038 | struct win_dev *pdev; | |
1039 | struct kvec iov[2]; | |
1040 | ||
1041 | if (tcon->unix_ext) { | |
1042 | /* | |
1043 | * SMB1 Unix Extensions: requires server support but | |
1044 | * works with all special files | |
1045 | */ | |
1046 | struct cifs_unix_set_info_args args = { | |
1047 | .mode = mode & ~current_umask(), | |
1048 | .ctime = NO_CHANGE_64, | |
1049 | .atime = NO_CHANGE_64, | |
1050 | .mtime = NO_CHANGE_64, | |
1051 | .device = dev, | |
1052 | }; | |
1053 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) { | |
1054 | args.uid = current_fsuid(); | |
1055 | args.gid = current_fsgid(); | |
1056 | } else { | |
1057 | args.uid = INVALID_UID; /* no change */ | |
1058 | args.gid = INVALID_GID; /* no change */ | |
1059 | } | |
1060 | rc = CIFSSMBUnixSetPathInfo(xid, tcon, full_path, &args, | |
1061 | cifs_sb->local_nls, | |
1062 | cifs_remap(cifs_sb)); | |
1063 | if (rc) | |
1064 | goto out; | |
1065 | ||
1066 | rc = cifs_get_inode_info_unix(&newinode, full_path, | |
1067 | inode->i_sb, xid); | |
1068 | ||
1069 | if (rc == 0) | |
1070 | d_instantiate(dentry, newinode); | |
1071 | goto out; | |
1072 | } | |
1073 | ||
1074 | /* | |
1075 | * SMB1 SFU emulation: should work with all servers, but only | |
1076 | * support block and char device (no socket & fifo) | |
1077 | */ | |
1078 | if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)) | |
1079 | goto out; | |
1080 | ||
1081 | if (!S_ISCHR(mode) && !S_ISBLK(mode)) | |
1082 | goto out; | |
1083 | ||
1084 | cifs_dbg(FYI, "sfu compat create special file\n"); | |
1085 | ||
1086 | buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL); | |
1087 | if (buf == NULL) { | |
1088 | rc = -ENOMEM; | |
1089 | goto out; | |
1090 | } | |
1091 | ||
1092 | if (backup_cred(cifs_sb)) | |
1093 | create_options |= CREATE_OPEN_BACKUP_INTENT; | |
1094 | ||
1095 | oparms.tcon = tcon; | |
1096 | oparms.cifs_sb = cifs_sb; | |
1097 | oparms.desired_access = GENERIC_WRITE; | |
1098 | oparms.create_options = create_options; | |
1099 | oparms.disposition = FILE_CREATE; | |
1100 | oparms.path = full_path; | |
1101 | oparms.fid = &fid; | |
1102 | oparms.reconnect = false; | |
1103 | ||
1104 | if (tcon->ses->server->oplocks) | |
1105 | oplock = REQ_OPLOCK; | |
1106 | else | |
1107 | oplock = 0; | |
1108 | rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, buf); | |
1109 | if (rc) | |
1110 | goto out; | |
1111 | ||
1112 | /* | |
1113 | * BB Do not bother to decode buf since no local inode yet to put | |
1114 | * timestamps in, but we can reuse it safely. | |
1115 | */ | |
1116 | ||
1117 | pdev = (struct win_dev *)buf; | |
1118 | io_parms.pid = current->tgid; | |
1119 | io_parms.tcon = tcon; | |
1120 | io_parms.offset = 0; | |
1121 | io_parms.length = sizeof(struct win_dev); | |
1122 | iov[1].iov_base = buf; | |
1123 | iov[1].iov_len = sizeof(struct win_dev); | |
1124 | if (S_ISCHR(mode)) { | |
1125 | memcpy(pdev->type, "IntxCHR", 8); | |
1126 | pdev->major = cpu_to_le64(MAJOR(dev)); | |
1127 | pdev->minor = cpu_to_le64(MINOR(dev)); | |
1128 | rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms, | |
1129 | &bytes_written, iov, 1); | |
1130 | } else if (S_ISBLK(mode)) { | |
1131 | memcpy(pdev->type, "IntxBLK", 8); | |
1132 | pdev->major = cpu_to_le64(MAJOR(dev)); | |
1133 | pdev->minor = cpu_to_le64(MINOR(dev)); | |
1134 | rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms, | |
1135 | &bytes_written, iov, 1); | |
1136 | } | |
1137 | tcon->ses->server->ops->close(xid, tcon, &fid); | |
1138 | d_drop(dentry); | |
1139 | ||
1140 | /* FIXME: add code here to set EAs */ | |
1141 | out: | |
1142 | kfree(buf); | |
1143 | return rc; | |
1144 | } | |
1145 | ||
1146 | ||
1147 | ||
23db65f5 | 1148 | struct smb_version_operations smb1_operations = { |
121b046a | 1149 | .send_cancel = send_nt_cancel, |
55157dfb | 1150 | .compare_fids = cifs_compare_fids, |
082d0642 | 1151 | .setup_request = cifs_setup_request, |
45740847 | 1152 | .setup_async_request = cifs_setup_async_request, |
082d0642 | 1153 | .check_receive = cifs_check_receive, |
45275789 PS |
1154 | .add_credits = cifs_add_credits, |
1155 | .set_credits = cifs_set_credits, | |
1156 | .get_credits_field = cifs_get_credits_field, | |
a891f0f8 | 1157 | .get_credits = cifs_get_credits, |
cb7e9eab | 1158 | .wait_mtu_credits = cifs_wait_mtu_credits, |
88257360 | 1159 | .get_next_mid = cifs_get_next_mid, |
eb378711 PS |
1160 | .read_data_offset = cifs_read_data_offset, |
1161 | .read_data_length = cifs_read_data_length, | |
1162 | .map_error = map_smb_to_linux_error, | |
8aa26f3e PS |
1163 | .find_mid = cifs_find_mid, |
1164 | .check_message = checkSMB, | |
1165 | .dump_detail = cifs_dump_detail, | |
44c58186 PS |
1166 | .clear_stats = cifs_clear_stats, |
1167 | .print_stats = cifs_print_stats, | |
8aa26f3e | 1168 | .is_oplock_break = is_valid_oplock_break, |
c11f1df5 | 1169 | .downgrade_oplock = cifs_downgrade_oplock, |
316cf94a | 1170 | .check_trans2 = cifs_check_trans2, |
286170aa PS |
1171 | .need_neg = cifs_need_neg, |
1172 | .negotiate = cifs_negotiate, | |
24985c53 PS |
1173 | .negotiate_wsize = cifs_negotiate_wsize, |
1174 | .negotiate_rsize = cifs_negotiate_rsize, | |
58c45c58 PS |
1175 | .sess_setup = CIFS_SessSetup, |
1176 | .logoff = CIFSSMBLogoff, | |
2e6e02ab PS |
1177 | .tree_connect = CIFSTCon, |
1178 | .tree_disconnect = CIFSSMBTDis, | |
b669f33c | 1179 | .get_dfs_refer = CIFSGetDFSRefer, |
af4281dc | 1180 | .qfs_tcon = cifs_qfs_tcon, |
68889f26 | 1181 | .is_path_accessible = cifs_is_path_accessible, |
62a6cfdd | 1182 | .can_echo = cifs_can_echo, |
1208ef1f | 1183 | .query_path_info = cifs_query_path_info, |
4ad65044 | 1184 | .query_file_info = cifs_query_file_info, |
1208ef1f | 1185 | .get_srv_inum = cifs_get_srv_inum, |
d1433418 PS |
1186 | .set_path_size = CIFSSMBSetEOF, |
1187 | .set_file_size = CIFSSMBSetFileSize, | |
6bdf6dbd | 1188 | .set_file_info = smb_set_file_info, |
c7f508a9 | 1189 | .set_compression = cifs_set_compression, |
f6d76178 | 1190 | .echo = CIFSSMBEcho, |
f436720e PS |
1191 | .mkdir = CIFSSMBMkDir, |
1192 | .mkdir_setinfo = cifs_mkdir_setinfo, | |
f958ca5d | 1193 | .rmdir = CIFSSMBRmDir, |
ed6875e0 PS |
1194 | .unlink = CIFSSMBDelFile, |
1195 | .rename_pending_delete = cifs_rename_pending_delete, | |
8ceb9843 | 1196 | .rename = CIFSSMBRename, |
d6e906f1 | 1197 | .create_hardlink = CIFSCreateHardLink, |
d244bf2d | 1198 | .query_symlink = cifs_query_symlink, |
fb1214e4 PS |
1199 | .open = cifs_open_file, |
1200 | .set_fid = cifs_set_fid, | |
0ff78a22 | 1201 | .close = cifs_close_file, |
1d8c4c00 | 1202 | .flush = cifs_flush_file, |
fc9c5966 | 1203 | .async_readv = cifs_async_readv, |
c9de5c80 | 1204 | .async_writev = cifs_async_writev, |
f9c6e234 | 1205 | .sync_read = cifs_sync_read, |
ba9ad725 | 1206 | .sync_write = cifs_sync_write, |
92fc65a7 PS |
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, | |
95a3f2f3 | 1211 | .oplock_response = cifs_oplock_response, |
76ec5e33 | 1212 | .queryfs = cifs_queryfs, |
d39a4f71 PS |
1213 | .mand_lock = cifs_mand_lock, |
1214 | .mand_unlock_range = cifs_unlock_range, | |
1215 | .push_mand_locks = cifs_push_mandatory_locks, | |
b5be1a1c | 1216 | .query_mf_symlink = cifs_query_mf_symlink, |
cbb0aba6 | 1217 | .create_mf_symlink = cifs_create_mf_symlink, |
53ef1016 | 1218 | .is_read_op = cifs_is_read_op, |
7f6c5008 | 1219 | .wp_retry_size = cifs_wp_retry_size, |
52755808 | 1220 | .dir_needs_close = cifs_dir_needs_close, |
ef65aaed | 1221 | .select_sectype = cifs_select_sectype, |
d979f3b0 SF |
1222 | #ifdef CONFIG_CIFS_XATTR |
1223 | .query_all_EAs = CIFSSMBQAllEAs, | |
1224 | .set_EA = CIFSSMBSetEA, | |
1225 | #endif /* CIFS_XATTR */ | |
83e3bc23 | 1226 | .get_acl = get_cifs_acl, |
42eacf9e | 1227 | .get_acl_by_fid = get_cifs_acl_by_fid, |
83e3bc23 | 1228 | .set_acl = set_cifs_acl, |
c847dccf | 1229 | .make_node = cifs_make_node, |
23db65f5 JL |
1230 | }; |
1231 | ||
1232 | struct smb_version_values smb1_values = { | |
1233 | .version_string = SMB1_VERSION_STRING, | |
43cdae88 | 1234 | .protocol_id = SMB10_PROT_ID, |
106dc538 PS |
1235 | .large_lock_type = LOCKING_ANDX_LARGE_FILES, |
1236 | .exclusive_lock_type = 0, | |
1237 | .shared_lock_type = LOCKING_ANDX_SHARED_LOCK, | |
1238 | .unlock_lock_type = 0, | |
93012bf9 | 1239 | .header_preamble_size = 4, |
1887f601 PS |
1240 | .header_size = sizeof(struct smb_hdr), |
1241 | .max_header_size = MAX_CIFS_HDR_SIZE, | |
eb378711 | 1242 | .read_rsp_size = sizeof(READ_RSP), |
2dc7e1c0 | 1243 | .lock_cmd = cpu_to_le16(SMB_COM_LOCKING_ANDX), |
29e20f9c PS |
1244 | .cap_unix = CAP_UNIX, |
1245 | .cap_nt_find = CAP_NT_SMBS | CAP_NT_FIND, | |
1246 | .cap_large_files = CAP_LARGE_FILES, | |
50285882 JL |
1247 | .signing_enabled = SECMODE_SIGN_ENABLED, |
1248 | .signing_required = SECMODE_SIGN_REQUIRED, | |
23db65f5 | 1249 | }; |