]> git.ipfire.org Git - thirdparty/linux.git/blob - fs/cifs/misc.c
gpu: host1x: Use SMMU on Tegra124 and Tegra210
[thirdparty/linux.git] / fs / cifs / misc.c
1 /*
2 * fs/cifs/misc.c
3 *
4 * Copyright (C) International Business Machines Corp., 2002,2008
5 * Author(s): Steve French (sfrench@us.ibm.com)
6 *
7 * This library is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published
9 * by the Free Software Foundation; either version 2.1 of the License, or
10 * (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
15 * the GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22 #include <linux/slab.h>
23 #include <linux/ctype.h>
24 #include <linux/mempool.h>
25 #include <linux/vmalloc.h>
26 #include "cifspdu.h"
27 #include "cifsglob.h"
28 #include "cifsproto.h"
29 #include "cifs_debug.h"
30 #include "smberr.h"
31 #include "nterr.h"
32 #include "cifs_unicode.h"
33 #include "smb2pdu.h"
34 #include "cifsfs.h"
35
36 extern mempool_t *cifs_sm_req_poolp;
37 extern mempool_t *cifs_req_poolp;
38
39 /* The xid serves as a useful identifier for each incoming vfs request,
40 in a similar way to the mid which is useful to track each sent smb,
41 and CurrentXid can also provide a running counter (although it
42 will eventually wrap past zero) of the total vfs operations handled
43 since the cifs fs was mounted */
44
45 unsigned int
46 _get_xid(void)
47 {
48 unsigned int xid;
49
50 spin_lock(&GlobalMid_Lock);
51 GlobalTotalActiveXid++;
52
53 /* keep high water mark for number of simultaneous ops in filesystem */
54 if (GlobalTotalActiveXid > GlobalMaxActiveXid)
55 GlobalMaxActiveXid = GlobalTotalActiveXid;
56 if (GlobalTotalActiveXid > 65000)
57 cifs_dbg(FYI, "warning: more than 65000 requests active\n");
58 xid = GlobalCurrentXid++;
59 spin_unlock(&GlobalMid_Lock);
60 return xid;
61 }
62
63 void
64 _free_xid(unsigned int xid)
65 {
66 spin_lock(&GlobalMid_Lock);
67 /* if (GlobalTotalActiveXid == 0)
68 BUG(); */
69 GlobalTotalActiveXid--;
70 spin_unlock(&GlobalMid_Lock);
71 }
72
73 struct cifs_ses *
74 sesInfoAlloc(void)
75 {
76 struct cifs_ses *ret_buf;
77
78 ret_buf = kzalloc(sizeof(struct cifs_ses), GFP_KERNEL);
79 if (ret_buf) {
80 atomic_inc(&sesInfoAllocCount);
81 ret_buf->status = CifsNew;
82 ++ret_buf->ses_count;
83 INIT_LIST_HEAD(&ret_buf->smb_ses_list);
84 INIT_LIST_HEAD(&ret_buf->tcon_list);
85 mutex_init(&ret_buf->session_mutex);
86 spin_lock_init(&ret_buf->iface_lock);
87 }
88 return ret_buf;
89 }
90
91 void
92 sesInfoFree(struct cifs_ses *buf_to_free)
93 {
94 if (buf_to_free == NULL) {
95 cifs_dbg(FYI, "Null buffer passed to sesInfoFree\n");
96 return;
97 }
98
99 atomic_dec(&sesInfoAllocCount);
100 kfree(buf_to_free->serverOS);
101 kfree(buf_to_free->serverDomain);
102 kfree(buf_to_free->serverNOS);
103 kzfree(buf_to_free->password);
104 kfree(buf_to_free->user_name);
105 kfree(buf_to_free->domainName);
106 kzfree(buf_to_free->auth_key.response);
107 kfree(buf_to_free->iface_list);
108 kzfree(buf_to_free);
109 }
110
111 struct cifs_tcon *
112 tconInfoAlloc(void)
113 {
114 struct cifs_tcon *ret_buf;
115
116 ret_buf = kzalloc(sizeof(*ret_buf), GFP_KERNEL);
117 if (!ret_buf)
118 return NULL;
119 ret_buf->crfid.fid = kzalloc(sizeof(*ret_buf->crfid.fid), GFP_KERNEL);
120 if (!ret_buf->crfid.fid) {
121 kfree(ret_buf);
122 return NULL;
123 }
124
125 atomic_inc(&tconInfoAllocCount);
126 ret_buf->tidStatus = CifsNew;
127 ++ret_buf->tc_count;
128 INIT_LIST_HEAD(&ret_buf->openFileList);
129 INIT_LIST_HEAD(&ret_buf->tcon_list);
130 spin_lock_init(&ret_buf->open_file_lock);
131 mutex_init(&ret_buf->crfid.fid_mutex);
132 spin_lock_init(&ret_buf->stat_lock);
133 atomic_set(&ret_buf->num_local_opens, 0);
134 atomic_set(&ret_buf->num_remote_opens, 0);
135
136 return ret_buf;
137 }
138
139 void
140 tconInfoFree(struct cifs_tcon *buf_to_free)
141 {
142 if (buf_to_free == NULL) {
143 cifs_dbg(FYI, "Null buffer passed to tconInfoFree\n");
144 return;
145 }
146 atomic_dec(&tconInfoAllocCount);
147 kfree(buf_to_free->nativeFileSystem);
148 kzfree(buf_to_free->password);
149 kfree(buf_to_free->crfid.fid);
150 #ifdef CONFIG_CIFS_DFS_UPCALL
151 kfree(buf_to_free->dfs_path);
152 #endif
153 kfree(buf_to_free);
154 }
155
156 struct smb_hdr *
157 cifs_buf_get(void)
158 {
159 struct smb_hdr *ret_buf = NULL;
160 /*
161 * SMB2 header is bigger than CIFS one - no problems to clean some
162 * more bytes for CIFS.
163 */
164 size_t buf_size = sizeof(struct smb2_sync_hdr);
165
166 /*
167 * We could use negotiated size instead of max_msgsize -
168 * but it may be more efficient to always alloc same size
169 * albeit slightly larger than necessary and maxbuffersize
170 * defaults to this and can not be bigger.
171 */
172 ret_buf = mempool_alloc(cifs_req_poolp, GFP_NOFS);
173
174 /* clear the first few header bytes */
175 /* for most paths, more is cleared in header_assemble */
176 memset(ret_buf, 0, buf_size + 3);
177 atomic_inc(&bufAllocCount);
178 #ifdef CONFIG_CIFS_STATS2
179 atomic_inc(&totBufAllocCount);
180 #endif /* CONFIG_CIFS_STATS2 */
181
182 return ret_buf;
183 }
184
185 void
186 cifs_buf_release(void *buf_to_free)
187 {
188 if (buf_to_free == NULL) {
189 /* cifs_dbg(FYI, "Null buffer passed to cifs_buf_release\n");*/
190 return;
191 }
192 mempool_free(buf_to_free, cifs_req_poolp);
193
194 atomic_dec(&bufAllocCount);
195 return;
196 }
197
198 struct smb_hdr *
199 cifs_small_buf_get(void)
200 {
201 struct smb_hdr *ret_buf = NULL;
202
203 /* We could use negotiated size instead of max_msgsize -
204 but it may be more efficient to always alloc same size
205 albeit slightly larger than necessary and maxbuffersize
206 defaults to this and can not be bigger */
207 ret_buf = mempool_alloc(cifs_sm_req_poolp, GFP_NOFS);
208 /* No need to clear memory here, cleared in header assemble */
209 /* memset(ret_buf, 0, sizeof(struct smb_hdr) + 27);*/
210 atomic_inc(&smBufAllocCount);
211 #ifdef CONFIG_CIFS_STATS2
212 atomic_inc(&totSmBufAllocCount);
213 #endif /* CONFIG_CIFS_STATS2 */
214
215 return ret_buf;
216 }
217
218 void
219 cifs_small_buf_release(void *buf_to_free)
220 {
221
222 if (buf_to_free == NULL) {
223 cifs_dbg(FYI, "Null buffer passed to cifs_small_buf_release\n");
224 return;
225 }
226 mempool_free(buf_to_free, cifs_sm_req_poolp);
227
228 atomic_dec(&smBufAllocCount);
229 return;
230 }
231
232 void
233 free_rsp_buf(int resp_buftype, void *rsp)
234 {
235 if (resp_buftype == CIFS_SMALL_BUFFER)
236 cifs_small_buf_release(rsp);
237 else if (resp_buftype == CIFS_LARGE_BUFFER)
238 cifs_buf_release(rsp);
239 }
240
241 /* NB: MID can not be set if treeCon not passed in, in that
242 case it is responsbility of caller to set the mid */
243 void
244 header_assemble(struct smb_hdr *buffer, char smb_command /* command */ ,
245 const struct cifs_tcon *treeCon, int word_count
246 /* length of fixed section (word count) in two byte units */)
247 {
248 char *temp = (char *) buffer;
249
250 memset(temp, 0, 256); /* bigger than MAX_CIFS_HDR_SIZE */
251
252 buffer->smb_buf_length = cpu_to_be32(
253 (2 * word_count) + sizeof(struct smb_hdr) -
254 4 /* RFC 1001 length field does not count */ +
255 2 /* for bcc field itself */) ;
256
257 buffer->Protocol[0] = 0xFF;
258 buffer->Protocol[1] = 'S';
259 buffer->Protocol[2] = 'M';
260 buffer->Protocol[3] = 'B';
261 buffer->Command = smb_command;
262 buffer->Flags = 0x00; /* case sensitive */
263 buffer->Flags2 = SMBFLG2_KNOWS_LONG_NAMES;
264 buffer->Pid = cpu_to_le16((__u16)current->tgid);
265 buffer->PidHigh = cpu_to_le16((__u16)(current->tgid >> 16));
266 if (treeCon) {
267 buffer->Tid = treeCon->tid;
268 if (treeCon->ses) {
269 if (treeCon->ses->capabilities & CAP_UNICODE)
270 buffer->Flags2 |= SMBFLG2_UNICODE;
271 if (treeCon->ses->capabilities & CAP_STATUS32)
272 buffer->Flags2 |= SMBFLG2_ERR_STATUS;
273
274 /* Uid is not converted */
275 buffer->Uid = treeCon->ses->Suid;
276 buffer->Mid = get_next_mid(treeCon->ses->server);
277 }
278 if (treeCon->Flags & SMB_SHARE_IS_IN_DFS)
279 buffer->Flags2 |= SMBFLG2_DFS;
280 if (treeCon->nocase)
281 buffer->Flags |= SMBFLG_CASELESS;
282 if ((treeCon->ses) && (treeCon->ses->server))
283 if (treeCon->ses->server->sign)
284 buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
285 }
286
287 /* endian conversion of flags is now done just before sending */
288 buffer->WordCount = (char) word_count;
289 return;
290 }
291
292 static int
293 check_smb_hdr(struct smb_hdr *smb)
294 {
295 /* does it have the right SMB "signature" ? */
296 if (*(__le32 *) smb->Protocol != cpu_to_le32(0x424d53ff)) {
297 cifs_dbg(VFS, "Bad protocol string signature header 0x%x\n",
298 *(unsigned int *)smb->Protocol);
299 return 1;
300 }
301
302 /* if it's a response then accept */
303 if (smb->Flags & SMBFLG_RESPONSE)
304 return 0;
305
306 /* only one valid case where server sends us request */
307 if (smb->Command == SMB_COM_LOCKING_ANDX)
308 return 0;
309
310 cifs_dbg(VFS, "Server sent request, not response. mid=%u\n",
311 get_mid(smb));
312 return 1;
313 }
314
315 int
316 checkSMB(char *buf, unsigned int total_read, struct TCP_Server_Info *server)
317 {
318 struct smb_hdr *smb = (struct smb_hdr *)buf;
319 __u32 rfclen = be32_to_cpu(smb->smb_buf_length);
320 __u32 clc_len; /* calculated length */
321 cifs_dbg(FYI, "checkSMB Length: 0x%x, smb_buf_length: 0x%x\n",
322 total_read, rfclen);
323
324 /* is this frame too small to even get to a BCC? */
325 if (total_read < 2 + sizeof(struct smb_hdr)) {
326 if ((total_read >= sizeof(struct smb_hdr) - 1)
327 && (smb->Status.CifsError != 0)) {
328 /* it's an error return */
329 smb->WordCount = 0;
330 /* some error cases do not return wct and bcc */
331 return 0;
332 } else if ((total_read == sizeof(struct smb_hdr) + 1) &&
333 (smb->WordCount == 0)) {
334 char *tmp = (char *)smb;
335 /* Need to work around a bug in two servers here */
336 /* First, check if the part of bcc they sent was zero */
337 if (tmp[sizeof(struct smb_hdr)] == 0) {
338 /* some servers return only half of bcc
339 * on simple responses (wct, bcc both zero)
340 * in particular have seen this on
341 * ulogoffX and FindClose. This leaves
342 * one byte of bcc potentially unitialized
343 */
344 /* zero rest of bcc */
345 tmp[sizeof(struct smb_hdr)+1] = 0;
346 return 0;
347 }
348 cifs_dbg(VFS, "rcvd invalid byte count (bcc)\n");
349 } else {
350 cifs_dbg(VFS, "Length less than smb header size\n");
351 }
352 return -EIO;
353 }
354
355 /* otherwise, there is enough to get to the BCC */
356 if (check_smb_hdr(smb))
357 return -EIO;
358 clc_len = smbCalcSize(smb, server);
359
360 if (4 + rfclen != total_read) {
361 cifs_dbg(VFS, "Length read does not match RFC1001 length %d\n",
362 rfclen);
363 return -EIO;
364 }
365
366 if (4 + rfclen != clc_len) {
367 __u16 mid = get_mid(smb);
368 /* check if bcc wrapped around for large read responses */
369 if ((rfclen > 64 * 1024) && (rfclen > clc_len)) {
370 /* check if lengths match mod 64K */
371 if (((4 + rfclen) & 0xFFFF) == (clc_len & 0xFFFF))
372 return 0; /* bcc wrapped */
373 }
374 cifs_dbg(FYI, "Calculated size %u vs length %u mismatch for mid=%u\n",
375 clc_len, 4 + rfclen, mid);
376
377 if (4 + rfclen < clc_len) {
378 cifs_dbg(VFS, "RFC1001 size %u smaller than SMB for mid=%u\n",
379 rfclen, mid);
380 return -EIO;
381 } else if (rfclen > clc_len + 512) {
382 /*
383 * Some servers (Windows XP in particular) send more
384 * data than the lengths in the SMB packet would
385 * indicate on certain calls (byte range locks and
386 * trans2 find first calls in particular). While the
387 * client can handle such a frame by ignoring the
388 * trailing data, we choose limit the amount of extra
389 * data to 512 bytes.
390 */
391 cifs_dbg(VFS, "RFC1001 size %u more than 512 bytes larger than SMB for mid=%u\n",
392 rfclen, mid);
393 return -EIO;
394 }
395 }
396 return 0;
397 }
398
399 bool
400 is_valid_oplock_break(char *buffer, struct TCP_Server_Info *srv)
401 {
402 struct smb_hdr *buf = (struct smb_hdr *)buffer;
403 struct smb_com_lock_req *pSMB = (struct smb_com_lock_req *)buf;
404 struct list_head *tmp, *tmp1, *tmp2;
405 struct cifs_ses *ses;
406 struct cifs_tcon *tcon;
407 struct cifsInodeInfo *pCifsInode;
408 struct cifsFileInfo *netfile;
409
410 cifs_dbg(FYI, "Checking for oplock break or dnotify response\n");
411 if ((pSMB->hdr.Command == SMB_COM_NT_TRANSACT) &&
412 (pSMB->hdr.Flags & SMBFLG_RESPONSE)) {
413 struct smb_com_transaction_change_notify_rsp *pSMBr =
414 (struct smb_com_transaction_change_notify_rsp *)buf;
415 struct file_notify_information *pnotify;
416 __u32 data_offset = 0;
417 size_t len = srv->total_read - sizeof(pSMBr->hdr.smb_buf_length);
418
419 if (get_bcc(buf) > sizeof(struct file_notify_information)) {
420 data_offset = le32_to_cpu(pSMBr->DataOffset);
421
422 if (data_offset >
423 len - sizeof(struct file_notify_information)) {
424 cifs_dbg(FYI, "invalid data_offset %u\n",
425 data_offset);
426 return true;
427 }
428 pnotify = (struct file_notify_information *)
429 ((char *)&pSMBr->hdr.Protocol + data_offset);
430 cifs_dbg(FYI, "dnotify on %s Action: 0x%x\n",
431 pnotify->FileName, pnotify->Action);
432 /* cifs_dump_mem("Rcvd notify Data: ",buf,
433 sizeof(struct smb_hdr)+60); */
434 return true;
435 }
436 if (pSMBr->hdr.Status.CifsError) {
437 cifs_dbg(FYI, "notify err 0x%x\n",
438 pSMBr->hdr.Status.CifsError);
439 return true;
440 }
441 return false;
442 }
443 if (pSMB->hdr.Command != SMB_COM_LOCKING_ANDX)
444 return false;
445 if (pSMB->hdr.Flags & SMBFLG_RESPONSE) {
446 /* no sense logging error on invalid handle on oplock
447 break - harmless race between close request and oplock
448 break response is expected from time to time writing out
449 large dirty files cached on the client */
450 if ((NT_STATUS_INVALID_HANDLE) ==
451 le32_to_cpu(pSMB->hdr.Status.CifsError)) {
452 cifs_dbg(FYI, "invalid handle on oplock break\n");
453 return true;
454 } else if (ERRbadfid ==
455 le16_to_cpu(pSMB->hdr.Status.DosError.Error)) {
456 return true;
457 } else {
458 return false; /* on valid oplock brk we get "request" */
459 }
460 }
461 if (pSMB->hdr.WordCount != 8)
462 return false;
463
464 cifs_dbg(FYI, "oplock type 0x%x level 0x%x\n",
465 pSMB->LockType, pSMB->OplockLevel);
466 if (!(pSMB->LockType & LOCKING_ANDX_OPLOCK_RELEASE))
467 return false;
468
469 /* look up tcon based on tid & uid */
470 spin_lock(&cifs_tcp_ses_lock);
471 list_for_each(tmp, &srv->smb_ses_list) {
472 ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
473 list_for_each(tmp1, &ses->tcon_list) {
474 tcon = list_entry(tmp1, struct cifs_tcon, tcon_list);
475 if (tcon->tid != buf->Tid)
476 continue;
477
478 cifs_stats_inc(&tcon->stats.cifs_stats.num_oplock_brks);
479 spin_lock(&tcon->open_file_lock);
480 list_for_each(tmp2, &tcon->openFileList) {
481 netfile = list_entry(tmp2, struct cifsFileInfo,
482 tlist);
483 if (pSMB->Fid != netfile->fid.netfid)
484 continue;
485
486 cifs_dbg(FYI, "file id match, oplock break\n");
487 pCifsInode = CIFS_I(d_inode(netfile->dentry));
488
489 set_bit(CIFS_INODE_PENDING_OPLOCK_BREAK,
490 &pCifsInode->flags);
491
492 netfile->oplock_epoch = 0;
493 netfile->oplock_level = pSMB->OplockLevel;
494 netfile->oplock_break_cancelled = false;
495 cifs_queue_oplock_break(netfile);
496
497 spin_unlock(&tcon->open_file_lock);
498 spin_unlock(&cifs_tcp_ses_lock);
499 return true;
500 }
501 spin_unlock(&tcon->open_file_lock);
502 spin_unlock(&cifs_tcp_ses_lock);
503 cifs_dbg(FYI, "No matching file for oplock break\n");
504 return true;
505 }
506 }
507 spin_unlock(&cifs_tcp_ses_lock);
508 cifs_dbg(FYI, "Can not process oplock break for non-existent connection\n");
509 return true;
510 }
511
512 void
513 dump_smb(void *buf, int smb_buf_length)
514 {
515 if (traceSMB == 0)
516 return;
517
518 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_NONE, 8, 2, buf,
519 smb_buf_length, true);
520 }
521
522 void
523 cifs_autodisable_serverino(struct cifs_sb_info *cifs_sb)
524 {
525 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
526 struct cifs_tcon *tcon = NULL;
527
528 if (cifs_sb->master_tlink)
529 tcon = cifs_sb_master_tcon(cifs_sb);
530
531 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM;
532 cifs_sb->mnt_cifs_serverino_autodisabled = true;
533 cifs_dbg(VFS, "Autodisabling the use of server inode numbers on %s.\n",
534 tcon ? tcon->treeName : "new server");
535 cifs_dbg(VFS, "The server doesn't seem to support them properly or the files might be on different servers (DFS).\n");
536 cifs_dbg(VFS, "Hardlinks will not be recognized on this mount. Consider mounting with the \"noserverino\" option to silence this message.\n");
537
538 }
539 }
540
541 void cifs_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock)
542 {
543 oplock &= 0xF;
544
545 if (oplock == OPLOCK_EXCLUSIVE) {
546 cinode->oplock = CIFS_CACHE_WRITE_FLG | CIFS_CACHE_READ_FLG;
547 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
548 &cinode->vfs_inode);
549 } else if (oplock == OPLOCK_READ) {
550 cinode->oplock = CIFS_CACHE_READ_FLG;
551 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
552 &cinode->vfs_inode);
553 } else
554 cinode->oplock = 0;
555 }
556
557 /*
558 * We wait for oplock breaks to be processed before we attempt to perform
559 * writes.
560 */
561 int cifs_get_writer(struct cifsInodeInfo *cinode)
562 {
563 int rc;
564
565 start:
566 rc = wait_on_bit(&cinode->flags, CIFS_INODE_PENDING_OPLOCK_BREAK,
567 TASK_KILLABLE);
568 if (rc)
569 return rc;
570
571 spin_lock(&cinode->writers_lock);
572 if (!cinode->writers)
573 set_bit(CIFS_INODE_PENDING_WRITERS, &cinode->flags);
574 cinode->writers++;
575 /* Check to see if we have started servicing an oplock break */
576 if (test_bit(CIFS_INODE_PENDING_OPLOCK_BREAK, &cinode->flags)) {
577 cinode->writers--;
578 if (cinode->writers == 0) {
579 clear_bit(CIFS_INODE_PENDING_WRITERS, &cinode->flags);
580 wake_up_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS);
581 }
582 spin_unlock(&cinode->writers_lock);
583 goto start;
584 }
585 spin_unlock(&cinode->writers_lock);
586 return 0;
587 }
588
589 void cifs_put_writer(struct cifsInodeInfo *cinode)
590 {
591 spin_lock(&cinode->writers_lock);
592 cinode->writers--;
593 if (cinode->writers == 0) {
594 clear_bit(CIFS_INODE_PENDING_WRITERS, &cinode->flags);
595 wake_up_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS);
596 }
597 spin_unlock(&cinode->writers_lock);
598 }
599
600 /**
601 * cifs_queue_oplock_break - queue the oplock break handler for cfile
602 *
603 * This function is called from the demultiplex thread when it
604 * receives an oplock break for @cfile.
605 *
606 * Assumes the tcon->open_file_lock is held.
607 * Assumes cfile->file_info_lock is NOT held.
608 */
609 void cifs_queue_oplock_break(struct cifsFileInfo *cfile)
610 {
611 /*
612 * Bump the handle refcount now while we hold the
613 * open_file_lock to enforce the validity of it for the oplock
614 * break handler. The matching put is done at the end of the
615 * handler.
616 */
617 cifsFileInfo_get(cfile);
618
619 queue_work(cifsoplockd_wq, &cfile->oplock_break);
620 }
621
622 void cifs_done_oplock_break(struct cifsInodeInfo *cinode)
623 {
624 clear_bit(CIFS_INODE_PENDING_OPLOCK_BREAK, &cinode->flags);
625 wake_up_bit(&cinode->flags, CIFS_INODE_PENDING_OPLOCK_BREAK);
626 }
627
628 bool
629 backup_cred(struct cifs_sb_info *cifs_sb)
630 {
631 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_BACKUPUID) {
632 if (uid_eq(cifs_sb->mnt_backupuid, current_fsuid()))
633 return true;
634 }
635 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_BACKUPGID) {
636 if (in_group_p(cifs_sb->mnt_backupgid))
637 return true;
638 }
639
640 return false;
641 }
642
643 void
644 cifs_del_pending_open(struct cifs_pending_open *open)
645 {
646 spin_lock(&tlink_tcon(open->tlink)->open_file_lock);
647 list_del(&open->olist);
648 spin_unlock(&tlink_tcon(open->tlink)->open_file_lock);
649 }
650
651 void
652 cifs_add_pending_open_locked(struct cifs_fid *fid, struct tcon_link *tlink,
653 struct cifs_pending_open *open)
654 {
655 memcpy(open->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
656 open->oplock = CIFS_OPLOCK_NO_CHANGE;
657 open->tlink = tlink;
658 fid->pending_open = open;
659 list_add_tail(&open->olist, &tlink_tcon(tlink)->pending_opens);
660 }
661
662 void
663 cifs_add_pending_open(struct cifs_fid *fid, struct tcon_link *tlink,
664 struct cifs_pending_open *open)
665 {
666 spin_lock(&tlink_tcon(tlink)->open_file_lock);
667 cifs_add_pending_open_locked(fid, tlink, open);
668 spin_unlock(&tlink_tcon(open->tlink)->open_file_lock);
669 }
670
671 /* parses DFS refferal V3 structure
672 * caller is responsible for freeing target_nodes
673 * returns:
674 * - on success - 0
675 * - on failure - errno
676 */
677 int
678 parse_dfs_referrals(struct get_dfs_referral_rsp *rsp, u32 rsp_size,
679 unsigned int *num_of_nodes,
680 struct dfs_info3_param **target_nodes,
681 const struct nls_table *nls_codepage, int remap,
682 const char *searchName, bool is_unicode)
683 {
684 int i, rc = 0;
685 char *data_end;
686 struct dfs_referral_level_3 *ref;
687
688 *num_of_nodes = le16_to_cpu(rsp->NumberOfReferrals);
689
690 if (*num_of_nodes < 1) {
691 cifs_dbg(VFS, "num_referrals: must be at least > 0, but we get num_referrals = %d\n",
692 *num_of_nodes);
693 rc = -EINVAL;
694 goto parse_DFS_referrals_exit;
695 }
696
697 ref = (struct dfs_referral_level_3 *) &(rsp->referrals);
698 if (ref->VersionNumber != cpu_to_le16(3)) {
699 cifs_dbg(VFS, "Referrals of V%d version are not supported, should be V3\n",
700 le16_to_cpu(ref->VersionNumber));
701 rc = -EINVAL;
702 goto parse_DFS_referrals_exit;
703 }
704
705 /* get the upper boundary of the resp buffer */
706 data_end = (char *)rsp + rsp_size;
707
708 cifs_dbg(FYI, "num_referrals: %d dfs flags: 0x%x ...\n",
709 *num_of_nodes, le32_to_cpu(rsp->DFSFlags));
710
711 *target_nodes = kcalloc(*num_of_nodes, sizeof(struct dfs_info3_param),
712 GFP_KERNEL);
713 if (*target_nodes == NULL) {
714 rc = -ENOMEM;
715 goto parse_DFS_referrals_exit;
716 }
717
718 /* collect necessary data from referrals */
719 for (i = 0; i < *num_of_nodes; i++) {
720 char *temp;
721 int max_len;
722 struct dfs_info3_param *node = (*target_nodes)+i;
723
724 node->flags = le32_to_cpu(rsp->DFSFlags);
725 if (is_unicode) {
726 __le16 *tmp = kmalloc(strlen(searchName)*2 + 2,
727 GFP_KERNEL);
728 if (tmp == NULL) {
729 rc = -ENOMEM;
730 goto parse_DFS_referrals_exit;
731 }
732 cifsConvertToUTF16((__le16 *) tmp, searchName,
733 PATH_MAX, nls_codepage, remap);
734 node->path_consumed = cifs_utf16_bytes(tmp,
735 le16_to_cpu(rsp->PathConsumed),
736 nls_codepage);
737 kfree(tmp);
738 } else
739 node->path_consumed = le16_to_cpu(rsp->PathConsumed);
740
741 node->server_type = le16_to_cpu(ref->ServerType);
742 node->ref_flag = le16_to_cpu(ref->ReferralEntryFlags);
743
744 /* copy DfsPath */
745 temp = (char *)ref + le16_to_cpu(ref->DfsPathOffset);
746 max_len = data_end - temp;
747 node->path_name = cifs_strndup_from_utf16(temp, max_len,
748 is_unicode, nls_codepage);
749 if (!node->path_name) {
750 rc = -ENOMEM;
751 goto parse_DFS_referrals_exit;
752 }
753
754 /* copy link target UNC */
755 temp = (char *)ref + le16_to_cpu(ref->NetworkAddressOffset);
756 max_len = data_end - temp;
757 node->node_name = cifs_strndup_from_utf16(temp, max_len,
758 is_unicode, nls_codepage);
759 if (!node->node_name) {
760 rc = -ENOMEM;
761 goto parse_DFS_referrals_exit;
762 }
763
764 node->ttl = le32_to_cpu(ref->TimeToLive);
765
766 ref++;
767 }
768
769 parse_DFS_referrals_exit:
770 if (rc) {
771 free_dfs_info_array(*target_nodes, *num_of_nodes);
772 *target_nodes = NULL;
773 *num_of_nodes = 0;
774 }
775 return rc;
776 }
777
778 struct cifs_aio_ctx *
779 cifs_aio_ctx_alloc(void)
780 {
781 struct cifs_aio_ctx *ctx;
782
783 /*
784 * Must use kzalloc to initialize ctx->bv to NULL and ctx->direct_io
785 * to false so that we know when we have to unreference pages within
786 * cifs_aio_ctx_release()
787 */
788 ctx = kzalloc(sizeof(struct cifs_aio_ctx), GFP_KERNEL);
789 if (!ctx)
790 return NULL;
791
792 INIT_LIST_HEAD(&ctx->list);
793 mutex_init(&ctx->aio_mutex);
794 init_completion(&ctx->done);
795 kref_init(&ctx->refcount);
796 return ctx;
797 }
798
799 void
800 cifs_aio_ctx_release(struct kref *refcount)
801 {
802 struct cifs_aio_ctx *ctx = container_of(refcount,
803 struct cifs_aio_ctx, refcount);
804
805 cifsFileInfo_put(ctx->cfile);
806
807 /*
808 * ctx->bv is only set if setup_aio_ctx_iter() was call successfuly
809 * which means that iov_iter_get_pages() was a success and thus that
810 * we have taken reference on pages.
811 */
812 if (ctx->bv) {
813 unsigned i;
814
815 for (i = 0; i < ctx->npages; i++) {
816 if (ctx->should_dirty)
817 set_page_dirty(ctx->bv[i].bv_page);
818 put_page(ctx->bv[i].bv_page);
819 }
820 kvfree(ctx->bv);
821 }
822
823 kfree(ctx);
824 }
825
826 #define CIFS_AIO_KMALLOC_LIMIT (1024 * 1024)
827
828 int
829 setup_aio_ctx_iter(struct cifs_aio_ctx *ctx, struct iov_iter *iter, int rw)
830 {
831 ssize_t rc;
832 unsigned int cur_npages;
833 unsigned int npages = 0;
834 unsigned int i;
835 size_t len;
836 size_t count = iov_iter_count(iter);
837 unsigned int saved_len;
838 size_t start;
839 unsigned int max_pages = iov_iter_npages(iter, INT_MAX);
840 struct page **pages = NULL;
841 struct bio_vec *bv = NULL;
842
843 if (iov_iter_is_kvec(iter)) {
844 memcpy(&ctx->iter, iter, sizeof(struct iov_iter));
845 ctx->len = count;
846 iov_iter_advance(iter, count);
847 return 0;
848 }
849
850 if (max_pages * sizeof(struct bio_vec) <= CIFS_AIO_KMALLOC_LIMIT)
851 bv = kmalloc_array(max_pages, sizeof(struct bio_vec),
852 GFP_KERNEL);
853
854 if (!bv) {
855 bv = vmalloc(array_size(max_pages, sizeof(struct bio_vec)));
856 if (!bv)
857 return -ENOMEM;
858 }
859
860 if (max_pages * sizeof(struct page *) <= CIFS_AIO_KMALLOC_LIMIT)
861 pages = kmalloc_array(max_pages, sizeof(struct page *),
862 GFP_KERNEL);
863
864 if (!pages) {
865 pages = vmalloc(array_size(max_pages, sizeof(struct page *)));
866 if (!pages) {
867 kvfree(bv);
868 return -ENOMEM;
869 }
870 }
871
872 saved_len = count;
873
874 while (count && npages < max_pages) {
875 rc = iov_iter_get_pages(iter, pages, count, max_pages, &start);
876 if (rc < 0) {
877 cifs_dbg(VFS, "couldn't get user pages (rc=%zd)\n", rc);
878 break;
879 }
880
881 if (rc > count) {
882 cifs_dbg(VFS, "get pages rc=%zd more than %zu\n", rc,
883 count);
884 break;
885 }
886
887 iov_iter_advance(iter, rc);
888 count -= rc;
889 rc += start;
890 cur_npages = DIV_ROUND_UP(rc, PAGE_SIZE);
891
892 if (npages + cur_npages > max_pages) {
893 cifs_dbg(VFS, "out of vec array capacity (%u vs %u)\n",
894 npages + cur_npages, max_pages);
895 break;
896 }
897
898 for (i = 0; i < cur_npages; i++) {
899 len = rc > PAGE_SIZE ? PAGE_SIZE : rc;
900 bv[npages + i].bv_page = pages[i];
901 bv[npages + i].bv_offset = start;
902 bv[npages + i].bv_len = len - start;
903 rc -= len;
904 start = 0;
905 }
906
907 npages += cur_npages;
908 }
909
910 kvfree(pages);
911 ctx->bv = bv;
912 ctx->len = saved_len - count;
913 ctx->npages = npages;
914 iov_iter_bvec(&ctx->iter, rw, ctx->bv, npages, ctx->len);
915 return 0;
916 }
917
918 /**
919 * cifs_alloc_hash - allocate hash and hash context together
920 *
921 * The caller has to make sure @sdesc is initialized to either NULL or
922 * a valid context. Both can be freed via cifs_free_hash().
923 */
924 int
925 cifs_alloc_hash(const char *name,
926 struct crypto_shash **shash, struct sdesc **sdesc)
927 {
928 int rc = 0;
929 size_t size;
930
931 if (*sdesc != NULL)
932 return 0;
933
934 *shash = crypto_alloc_shash(name, 0, 0);
935 if (IS_ERR(*shash)) {
936 cifs_dbg(VFS, "could not allocate crypto %s\n", name);
937 rc = PTR_ERR(*shash);
938 *shash = NULL;
939 *sdesc = NULL;
940 return rc;
941 }
942
943 size = sizeof(struct shash_desc) + crypto_shash_descsize(*shash);
944 *sdesc = kmalloc(size, GFP_KERNEL);
945 if (*sdesc == NULL) {
946 cifs_dbg(VFS, "no memory left to allocate crypto %s\n", name);
947 crypto_free_shash(*shash);
948 *shash = NULL;
949 return -ENOMEM;
950 }
951
952 (*sdesc)->shash.tfm = *shash;
953 return 0;
954 }
955
956 /**
957 * cifs_free_hash - free hash and hash context together
958 *
959 * Freeing a NULL hash or context is safe.
960 */
961 void
962 cifs_free_hash(struct crypto_shash **shash, struct sdesc **sdesc)
963 {
964 kfree(*sdesc);
965 *sdesc = NULL;
966 if (*shash)
967 crypto_free_shash(*shash);
968 *shash = NULL;
969 }
970
971 /**
972 * rqst_page_get_length - obtain the length and offset for a page in smb_rqst
973 * Input: rqst - a smb_rqst, page - a page index for rqst
974 * Output: *len - the length for this page, *offset - the offset for this page
975 */
976 void rqst_page_get_length(struct smb_rqst *rqst, unsigned int page,
977 unsigned int *len, unsigned int *offset)
978 {
979 *len = rqst->rq_pagesz;
980 *offset = (page == 0) ? rqst->rq_offset : 0;
981
982 if (rqst->rq_npages == 1 || page == rqst->rq_npages-1)
983 *len = rqst->rq_tailsz;
984 else if (page == 0)
985 *len = rqst->rq_pagesz - rqst->rq_offset;
986 }
987
988 void extract_unc_hostname(const char *unc, const char **h, size_t *len)
989 {
990 const char *end;
991
992 /* skip initial slashes */
993 while (*unc && (*unc == '\\' || *unc == '/'))
994 unc++;
995
996 end = unc;
997
998 while (*end && !(*end == '\\' || *end == '/'))
999 end++;
1000
1001 *h = unc;
1002 *len = end - unc;
1003 }
1004
1005 /**
1006 * copy_path_name - copy src path to dst, possibly truncating
1007 *
1008 * returns number of bytes written (including trailing nul)
1009 */
1010 int copy_path_name(char *dst, const char *src)
1011 {
1012 int name_len;
1013
1014 /*
1015 * PATH_MAX includes nul, so if strlen(src) >= PATH_MAX it
1016 * will truncate and strlen(dst) will be PATH_MAX-1
1017 */
1018 name_len = strscpy(dst, src, PATH_MAX);
1019 if (WARN_ON_ONCE(name_len < 0))
1020 name_len = PATH_MAX-1;
1021
1022 /* we count the trailing nul */
1023 name_len++;
1024 return name_len;
1025 }
1026
1027 struct super_cb_data {
1028 struct TCP_Server_Info *server;
1029 struct super_block *sb;
1030 };
1031
1032 static void super_cb(struct super_block *sb, void *arg)
1033 {
1034 struct super_cb_data *d = arg;
1035 struct cifs_sb_info *cifs_sb;
1036 struct cifs_tcon *tcon;
1037
1038 if (d->sb)
1039 return;
1040
1041 cifs_sb = CIFS_SB(sb);
1042 tcon = cifs_sb_master_tcon(cifs_sb);
1043 if (tcon->ses->server == d->server)
1044 d->sb = sb;
1045 }
1046
1047 struct super_block *cifs_get_tcp_super(struct TCP_Server_Info *server)
1048 {
1049 struct super_cb_data d = {
1050 .server = server,
1051 .sb = NULL,
1052 };
1053
1054 iterate_supers_type(&cifs_fs_type, super_cb, &d);
1055
1056 if (unlikely(!d.sb))
1057 return ERR_PTR(-ENOENT);
1058 /*
1059 * Grab an active reference in order to prevent automounts (DFS links)
1060 * of expiring and then freeing up our cifs superblock pointer while
1061 * we're doing failover.
1062 */
1063 cifs_sb_active(d.sb);
1064 return d.sb;
1065 }
1066
1067 void cifs_put_tcp_super(struct super_block *sb)
1068 {
1069 if (!IS_ERR_OR_NULL(sb))
1070 cifs_sb_deactive(sb);
1071 }
1072
1073 int update_super_prepath(struct cifs_tcon *tcon, const char *prefix,
1074 size_t prefix_len)
1075 {
1076 struct super_block *sb;
1077 struct cifs_sb_info *cifs_sb;
1078 int rc = 0;
1079
1080 sb = cifs_get_tcp_super(tcon->ses->server);
1081 if (IS_ERR(sb))
1082 return PTR_ERR(sb);
1083
1084 cifs_sb = CIFS_SB(sb);
1085
1086 kfree(cifs_sb->prepath);
1087
1088 if (*prefix && prefix_len) {
1089 cifs_sb->prepath = kstrndup(prefix, prefix_len, GFP_ATOMIC);
1090 if (!cifs_sb->prepath) {
1091 rc = -ENOMEM;
1092 goto out;
1093 }
1094
1095 convert_delimiter(cifs_sb->prepath, CIFS_DIR_SEP(cifs_sb));
1096 } else
1097 cifs_sb->prepath = NULL;
1098
1099 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
1100
1101 out:
1102 cifs_put_tcp_super(sb);
1103 return rc;
1104 }