1
2
3
4
5
6
7
8
9#include <linux/slab.h>
10#include <linux/ctype.h>
11#include <linux/mempool.h>
12#include <linux/vmalloc.h>
13#include "cifspdu.h"
14#include "cifsglob.h"
15#include "cifsproto.h"
16#include "cifs_debug.h"
17#include "smberr.h"
18#include "nterr.h"
19#include "cifs_unicode.h"
20#include "smb2pdu.h"
21#include "cifsfs.h"
22#ifdef CONFIG_CIFS_DFS_UPCALL
23#include "dns_resolve.h"
24#endif
25#include "fs_context.h"
26
27extern mempool_t *cifs_sm_req_poolp;
28extern mempool_t *cifs_req_poolp;
29
30
31
32
33
34
35
36unsigned int
37_get_xid(void)
38{
39 unsigned int xid;
40
41 spin_lock(&GlobalMid_Lock);
42 GlobalTotalActiveXid++;
43
44
45 if (GlobalTotalActiveXid > GlobalMaxActiveXid)
46 GlobalMaxActiveXid = GlobalTotalActiveXid;
47 if (GlobalTotalActiveXid > 65000)
48 cifs_dbg(FYI, "warning: more than 65000 requests active\n");
49 xid = GlobalCurrentXid++;
50 spin_unlock(&GlobalMid_Lock);
51 return xid;
52}
53
54void
55_free_xid(unsigned int xid)
56{
57 spin_lock(&GlobalMid_Lock);
58
59
60 GlobalTotalActiveXid--;
61 spin_unlock(&GlobalMid_Lock);
62}
63
64struct cifs_ses *
65sesInfoAlloc(void)
66{
67 struct cifs_ses *ret_buf;
68
69 ret_buf = kzalloc(sizeof(struct cifs_ses), GFP_KERNEL);
70 if (ret_buf) {
71 atomic_inc(&sesInfoAllocCount);
72 ret_buf->status = CifsNew;
73 ++ret_buf->ses_count;
74 INIT_LIST_HEAD(&ret_buf->smb_ses_list);
75 INIT_LIST_HEAD(&ret_buf->tcon_list);
76 mutex_init(&ret_buf->session_mutex);
77 spin_lock_init(&ret_buf->iface_lock);
78 }
79 return ret_buf;
80}
81
82void
83sesInfoFree(struct cifs_ses *buf_to_free)
84{
85 if (buf_to_free == NULL) {
86 cifs_dbg(FYI, "Null buffer passed to sesInfoFree\n");
87 return;
88 }
89
90 atomic_dec(&sesInfoAllocCount);
91 kfree(buf_to_free->serverOS);
92 kfree(buf_to_free->serverDomain);
93 kfree(buf_to_free->serverNOS);
94 kfree_sensitive(buf_to_free->password);
95 kfree(buf_to_free->user_name);
96 kfree(buf_to_free->domainName);
97 kfree_sensitive(buf_to_free->auth_key.response);
98 kfree(buf_to_free->iface_list);
99 kfree_sensitive(buf_to_free);
100}
101
102struct cifs_tcon *
103tconInfoAlloc(void)
104{
105 struct cifs_tcon *ret_buf;
106
107 ret_buf = kzalloc(sizeof(*ret_buf), GFP_KERNEL);
108 if (!ret_buf)
109 return NULL;
110 ret_buf->crfid.fid = kzalloc(sizeof(*ret_buf->crfid.fid), GFP_KERNEL);
111 if (!ret_buf->crfid.fid) {
112 kfree(ret_buf);
113 return NULL;
114 }
115
116 atomic_inc(&tconInfoAllocCount);
117 ret_buf->tidStatus = CifsNew;
118 ++ret_buf->tc_count;
119 INIT_LIST_HEAD(&ret_buf->openFileList);
120 INIT_LIST_HEAD(&ret_buf->tcon_list);
121 spin_lock_init(&ret_buf->open_file_lock);
122 mutex_init(&ret_buf->crfid.fid_mutex);
123 spin_lock_init(&ret_buf->stat_lock);
124 atomic_set(&ret_buf->num_local_opens, 0);
125 atomic_set(&ret_buf->num_remote_opens, 0);
126
127 return ret_buf;
128}
129
130void
131tconInfoFree(struct cifs_tcon *buf_to_free)
132{
133 if (buf_to_free == NULL) {
134 cifs_dbg(FYI, "Null buffer passed to tconInfoFree\n");
135 return;
136 }
137 atomic_dec(&tconInfoAllocCount);
138 kfree(buf_to_free->nativeFileSystem);
139 kfree_sensitive(buf_to_free->password);
140 kfree(buf_to_free->crfid.fid);
141#ifdef CONFIG_CIFS_DFS_UPCALL
142 kfree(buf_to_free->dfs_path);
143#endif
144 kfree(buf_to_free);
145}
146
147struct smb_hdr *
148cifs_buf_get(void)
149{
150 struct smb_hdr *ret_buf = NULL;
151
152
153
154
155 size_t buf_size = sizeof(struct smb2_sync_hdr);
156
157
158
159
160
161
162
163 ret_buf = mempool_alloc(cifs_req_poolp, GFP_NOFS);
164
165
166
167 memset(ret_buf, 0, buf_size + 3);
168 atomic_inc(&bufAllocCount);
169#ifdef CONFIG_CIFS_STATS2
170 atomic_inc(&totBufAllocCount);
171#endif
172
173 return ret_buf;
174}
175
176void
177cifs_buf_release(void *buf_to_free)
178{
179 if (buf_to_free == NULL) {
180
181 return;
182 }
183 mempool_free(buf_to_free, cifs_req_poolp);
184
185 atomic_dec(&bufAllocCount);
186 return;
187}
188
189struct smb_hdr *
190cifs_small_buf_get(void)
191{
192 struct smb_hdr *ret_buf = NULL;
193
194
195
196
197
198 ret_buf = mempool_alloc(cifs_sm_req_poolp, GFP_NOFS);
199
200
201 atomic_inc(&smBufAllocCount);
202#ifdef CONFIG_CIFS_STATS2
203 atomic_inc(&totSmBufAllocCount);
204#endif
205
206 return ret_buf;
207}
208
209void
210cifs_small_buf_release(void *buf_to_free)
211{
212
213 if (buf_to_free == NULL) {
214 cifs_dbg(FYI, "Null buffer passed to cifs_small_buf_release\n");
215 return;
216 }
217 mempool_free(buf_to_free, cifs_sm_req_poolp);
218
219 atomic_dec(&smBufAllocCount);
220 return;
221}
222
223void
224free_rsp_buf(int resp_buftype, void *rsp)
225{
226 if (resp_buftype == CIFS_SMALL_BUFFER)
227 cifs_small_buf_release(rsp);
228 else if (resp_buftype == CIFS_LARGE_BUFFER)
229 cifs_buf_release(rsp);
230}
231
232
233
234void
235header_assemble(struct smb_hdr *buffer, char smb_command ,
236 const struct cifs_tcon *treeCon, int word_count
237 )
238{
239 char *temp = (char *) buffer;
240
241 memset(temp, 0, 256);
242
243 buffer->smb_buf_length = cpu_to_be32(
244 (2 * word_count) + sizeof(struct smb_hdr) -
245 4 +
246 2 ) ;
247
248 buffer->Protocol[0] = 0xFF;
249 buffer->Protocol[1] = 'S';
250 buffer->Protocol[2] = 'M';
251 buffer->Protocol[3] = 'B';
252 buffer->Command = smb_command;
253 buffer->Flags = 0x00;
254 buffer->Flags2 = SMBFLG2_KNOWS_LONG_NAMES;
255 buffer->Pid = cpu_to_le16((__u16)current->tgid);
256 buffer->PidHigh = cpu_to_le16((__u16)(current->tgid >> 16));
257 if (treeCon) {
258 buffer->Tid = treeCon->tid;
259 if (treeCon->ses) {
260 if (treeCon->ses->capabilities & CAP_UNICODE)
261 buffer->Flags2 |= SMBFLG2_UNICODE;
262 if (treeCon->ses->capabilities & CAP_STATUS32)
263 buffer->Flags2 |= SMBFLG2_ERR_STATUS;
264
265
266 buffer->Uid = treeCon->ses->Suid;
267 if (treeCon->ses->server)
268 buffer->Mid = get_next_mid(treeCon->ses->server);
269 }
270 if (treeCon->Flags & SMB_SHARE_IS_IN_DFS)
271 buffer->Flags2 |= SMBFLG2_DFS;
272 if (treeCon->nocase)
273 buffer->Flags |= SMBFLG_CASELESS;
274 if ((treeCon->ses) && (treeCon->ses->server))
275 if (treeCon->ses->server->sign)
276 buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
277 }
278
279
280 buffer->WordCount = (char) word_count;
281 return;
282}
283
284static int
285check_smb_hdr(struct smb_hdr *smb)
286{
287
288 if (*(__le32 *) smb->Protocol != cpu_to_le32(0x424d53ff)) {
289 cifs_dbg(VFS, "Bad protocol string signature header 0x%x\n",
290 *(unsigned int *)smb->Protocol);
291 return 1;
292 }
293
294
295 if (smb->Flags & SMBFLG_RESPONSE)
296 return 0;
297
298
299 if (smb->Command == SMB_COM_LOCKING_ANDX)
300 return 0;
301
302 cifs_dbg(VFS, "Server sent request, not response. mid=%u\n",
303 get_mid(smb));
304 return 1;
305}
306
307int
308checkSMB(char *buf, unsigned int total_read, struct TCP_Server_Info *server)
309{
310 struct smb_hdr *smb = (struct smb_hdr *)buf;
311 __u32 rfclen = be32_to_cpu(smb->smb_buf_length);
312 __u32 clc_len;
313 cifs_dbg(FYI, "checkSMB Length: 0x%x, smb_buf_length: 0x%x\n",
314 total_read, rfclen);
315
316
317 if (total_read < 2 + sizeof(struct smb_hdr)) {
318 if ((total_read >= sizeof(struct smb_hdr) - 1)
319 && (smb->Status.CifsError != 0)) {
320
321 smb->WordCount = 0;
322
323 return 0;
324 } else if ((total_read == sizeof(struct smb_hdr) + 1) &&
325 (smb->WordCount == 0)) {
326 char *tmp = (char *)smb;
327
328
329 if (tmp[sizeof(struct smb_hdr)] == 0) {
330
331
332
333
334
335
336
337 tmp[sizeof(struct smb_hdr)+1] = 0;
338 return 0;
339 }
340 cifs_dbg(VFS, "rcvd invalid byte count (bcc)\n");
341 } else {
342 cifs_dbg(VFS, "Length less than smb header size\n");
343 }
344 return -EIO;
345 }
346
347
348 if (check_smb_hdr(smb))
349 return -EIO;
350 clc_len = smbCalcSize(smb, server);
351
352 if (4 + rfclen != total_read) {
353 cifs_dbg(VFS, "Length read does not match RFC1001 length %d\n",
354 rfclen);
355 return -EIO;
356 }
357
358 if (4 + rfclen != clc_len) {
359 __u16 mid = get_mid(smb);
360
361 if ((rfclen > 64 * 1024) && (rfclen > clc_len)) {
362
363 if (((4 + rfclen) & 0xFFFF) == (clc_len & 0xFFFF))
364 return 0;
365 }
366 cifs_dbg(FYI, "Calculated size %u vs length %u mismatch for mid=%u\n",
367 clc_len, 4 + rfclen, mid);
368
369 if (4 + rfclen < clc_len) {
370 cifs_dbg(VFS, "RFC1001 size %u smaller than SMB for mid=%u\n",
371 rfclen, mid);
372 return -EIO;
373 } else if (rfclen > clc_len + 512) {
374
375
376
377
378
379
380
381
382
383 cifs_dbg(VFS, "RFC1001 size %u more than 512 bytes larger than SMB for mid=%u\n",
384 rfclen, mid);
385 return -EIO;
386 }
387 }
388 return 0;
389}
390
391bool
392is_valid_oplock_break(char *buffer, struct TCP_Server_Info *srv)
393{
394 struct smb_hdr *buf = (struct smb_hdr *)buffer;
395 struct smb_com_lock_req *pSMB = (struct smb_com_lock_req *)buf;
396 struct list_head *tmp, *tmp1, *tmp2;
397 struct cifs_ses *ses;
398 struct cifs_tcon *tcon;
399 struct cifsInodeInfo *pCifsInode;
400 struct cifsFileInfo *netfile;
401
402 cifs_dbg(FYI, "Checking for oplock break or dnotify response\n");
403 if ((pSMB->hdr.Command == SMB_COM_NT_TRANSACT) &&
404 (pSMB->hdr.Flags & SMBFLG_RESPONSE)) {
405 struct smb_com_transaction_change_notify_rsp *pSMBr =
406 (struct smb_com_transaction_change_notify_rsp *)buf;
407 struct file_notify_information *pnotify;
408 __u32 data_offset = 0;
409 size_t len = srv->total_read - sizeof(pSMBr->hdr.smb_buf_length);
410
411 if (get_bcc(buf) > sizeof(struct file_notify_information)) {
412 data_offset = le32_to_cpu(pSMBr->DataOffset);
413
414 if (data_offset >
415 len - sizeof(struct file_notify_information)) {
416 cifs_dbg(FYI, "Invalid data_offset %u\n",
417 data_offset);
418 return true;
419 }
420 pnotify = (struct file_notify_information *)
421 ((char *)&pSMBr->hdr.Protocol + data_offset);
422 cifs_dbg(FYI, "dnotify on %s Action: 0x%x\n",
423 pnotify->FileName, pnotify->Action);
424
425
426 return true;
427 }
428 if (pSMBr->hdr.Status.CifsError) {
429 cifs_dbg(FYI, "notify err 0x%x\n",
430 pSMBr->hdr.Status.CifsError);
431 return true;
432 }
433 return false;
434 }
435 if (pSMB->hdr.Command != SMB_COM_LOCKING_ANDX)
436 return false;
437 if (pSMB->hdr.Flags & SMBFLG_RESPONSE) {
438
439
440
441
442 if ((NT_STATUS_INVALID_HANDLE) ==
443 le32_to_cpu(pSMB->hdr.Status.CifsError)) {
444 cifs_dbg(FYI, "Invalid handle on oplock break\n");
445 return true;
446 } else if (ERRbadfid ==
447 le16_to_cpu(pSMB->hdr.Status.DosError.Error)) {
448 return true;
449 } else {
450 return false;
451 }
452 }
453 if (pSMB->hdr.WordCount != 8)
454 return false;
455
456 cifs_dbg(FYI, "oplock type 0x%x level 0x%x\n",
457 pSMB->LockType, pSMB->OplockLevel);
458 if (!(pSMB->LockType & LOCKING_ANDX_OPLOCK_RELEASE))
459 return false;
460
461
462 spin_lock(&cifs_tcp_ses_lock);
463 list_for_each(tmp, &srv->smb_ses_list) {
464 ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
465 list_for_each(tmp1, &ses->tcon_list) {
466 tcon = list_entry(tmp1, struct cifs_tcon, tcon_list);
467 if (tcon->tid != buf->Tid)
468 continue;
469
470 cifs_stats_inc(&tcon->stats.cifs_stats.num_oplock_brks);
471 spin_lock(&tcon->open_file_lock);
472 list_for_each(tmp2, &tcon->openFileList) {
473 netfile = list_entry(tmp2, struct cifsFileInfo,
474 tlist);
475 if (pSMB->Fid != netfile->fid.netfid)
476 continue;
477
478 cifs_dbg(FYI, "file id match, oplock break\n");
479 pCifsInode = CIFS_I(d_inode(netfile->dentry));
480
481 set_bit(CIFS_INODE_PENDING_OPLOCK_BREAK,
482 &pCifsInode->flags);
483
484 netfile->oplock_epoch = 0;
485 netfile->oplock_level = pSMB->OplockLevel;
486 netfile->oplock_break_cancelled = false;
487 cifs_queue_oplock_break(netfile);
488
489 spin_unlock(&tcon->open_file_lock);
490 spin_unlock(&cifs_tcp_ses_lock);
491 return true;
492 }
493 spin_unlock(&tcon->open_file_lock);
494 spin_unlock(&cifs_tcp_ses_lock);
495 cifs_dbg(FYI, "No matching file for oplock break\n");
496 return true;
497 }
498 }
499 spin_unlock(&cifs_tcp_ses_lock);
500 cifs_dbg(FYI, "Can not process oplock break for non-existent connection\n");
501 return true;
502}
503
504void
505dump_smb(void *buf, int smb_buf_length)
506{
507 if (traceSMB == 0)
508 return;
509
510 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_NONE, 8, 2, buf,
511 smb_buf_length, true);
512}
513
514void
515cifs_autodisable_serverino(struct cifs_sb_info *cifs_sb)
516{
517 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
518 struct cifs_tcon *tcon = NULL;
519
520 if (cifs_sb->master_tlink)
521 tcon = cifs_sb_master_tcon(cifs_sb);
522
523 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM;
524 cifs_sb->mnt_cifs_serverino_autodisabled = true;
525 cifs_dbg(VFS, "Autodisabling the use of server inode numbers on %s\n",
526 tcon ? tcon->treeName : "new server");
527 cifs_dbg(VFS, "The server doesn't seem to support them properly or the files might be on different servers (DFS)\n");
528 cifs_dbg(VFS, "Hardlinks will not be recognized on this mount. Consider mounting with the \"noserverino\" option to silence this message.\n");
529
530 }
531}
532
533void cifs_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock)
534{
535 oplock &= 0xF;
536
537 if (oplock == OPLOCK_EXCLUSIVE) {
538 cinode->oplock = CIFS_CACHE_WRITE_FLG | CIFS_CACHE_READ_FLG;
539 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
540 &cinode->vfs_inode);
541 } else if (oplock == OPLOCK_READ) {
542 cinode->oplock = CIFS_CACHE_READ_FLG;
543 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
544 &cinode->vfs_inode);
545 } else
546 cinode->oplock = 0;
547}
548
549
550
551
552
553int cifs_get_writer(struct cifsInodeInfo *cinode)
554{
555 int rc;
556
557start:
558 rc = wait_on_bit(&cinode->flags, CIFS_INODE_PENDING_OPLOCK_BREAK,
559 TASK_KILLABLE);
560 if (rc)
561 return rc;
562
563 spin_lock(&cinode->writers_lock);
564 if (!cinode->writers)
565 set_bit(CIFS_INODE_PENDING_WRITERS, &cinode->flags);
566 cinode->writers++;
567
568 if (test_bit(CIFS_INODE_PENDING_OPLOCK_BREAK, &cinode->flags)) {
569 cinode->writers--;
570 if (cinode->writers == 0) {
571 clear_bit(CIFS_INODE_PENDING_WRITERS, &cinode->flags);
572 wake_up_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS);
573 }
574 spin_unlock(&cinode->writers_lock);
575 goto start;
576 }
577 spin_unlock(&cinode->writers_lock);
578 return 0;
579}
580
581void cifs_put_writer(struct cifsInodeInfo *cinode)
582{
583 spin_lock(&cinode->writers_lock);
584 cinode->writers--;
585 if (cinode->writers == 0) {
586 clear_bit(CIFS_INODE_PENDING_WRITERS, &cinode->flags);
587 wake_up_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS);
588 }
589 spin_unlock(&cinode->writers_lock);
590}
591
592
593
594
595
596
597
598
599
600
601
602void cifs_queue_oplock_break(struct cifsFileInfo *cfile)
603{
604
605
606
607
608
609
610 cifsFileInfo_get(cfile);
611
612 queue_work(cifsoplockd_wq, &cfile->oplock_break);
613}
614
615void cifs_done_oplock_break(struct cifsInodeInfo *cinode)
616{
617 clear_bit(CIFS_INODE_PENDING_OPLOCK_BREAK, &cinode->flags);
618 wake_up_bit(&cinode->flags, CIFS_INODE_PENDING_OPLOCK_BREAK);
619}
620
621bool
622backup_cred(struct cifs_sb_info *cifs_sb)
623{
624 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_BACKUPUID) {
625 if (uid_eq(cifs_sb->ctx->backupuid, current_fsuid()))
626 return true;
627 }
628 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_BACKUPGID) {
629 if (in_group_p(cifs_sb->ctx->backupgid))
630 return true;
631 }
632
633 return false;
634}
635
636void
637cifs_del_pending_open(struct cifs_pending_open *open)
638{
639 spin_lock(&tlink_tcon(open->tlink)->open_file_lock);
640 list_del(&open->olist);
641 spin_unlock(&tlink_tcon(open->tlink)->open_file_lock);
642}
643
644void
645cifs_add_pending_open_locked(struct cifs_fid *fid, struct tcon_link *tlink,
646 struct cifs_pending_open *open)
647{
648 memcpy(open->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
649 open->oplock = CIFS_OPLOCK_NO_CHANGE;
650 open->tlink = tlink;
651 fid->pending_open = open;
652 list_add_tail(&open->olist, &tlink_tcon(tlink)->pending_opens);
653}
654
655void
656cifs_add_pending_open(struct cifs_fid *fid, struct tcon_link *tlink,
657 struct cifs_pending_open *open)
658{
659 spin_lock(&tlink_tcon(tlink)->open_file_lock);
660 cifs_add_pending_open_locked(fid, tlink, open);
661 spin_unlock(&tlink_tcon(open->tlink)->open_file_lock);
662}
663
664
665
666
667
668
669bool
670cifs_is_deferred_close(struct cifsFileInfo *cfile, struct cifs_deferred_close **pdclose)
671{
672 struct cifs_deferred_close *dclose;
673
674 list_for_each_entry(dclose, &CIFS_I(d_inode(cfile->dentry))->deferred_closes, dlist) {
675 if ((dclose->netfid == cfile->fid.netfid) &&
676 (dclose->persistent_fid == cfile->fid.persistent_fid) &&
677 (dclose->volatile_fid == cfile->fid.volatile_fid)) {
678 *pdclose = dclose;
679 return true;
680 }
681 }
682 return false;
683}
684
685
686
687
688void
689cifs_add_deferred_close(struct cifsFileInfo *cfile, struct cifs_deferred_close *dclose)
690{
691 bool is_deferred = false;
692 struct cifs_deferred_close *pdclose;
693
694 is_deferred = cifs_is_deferred_close(cfile, &pdclose);
695 if (is_deferred) {
696 kfree(dclose);
697 return;
698 }
699
700 dclose->tlink = cfile->tlink;
701 dclose->netfid = cfile->fid.netfid;
702 dclose->persistent_fid = cfile->fid.persistent_fid;
703 dclose->volatile_fid = cfile->fid.volatile_fid;
704 list_add_tail(&dclose->dlist, &CIFS_I(d_inode(cfile->dentry))->deferred_closes);
705}
706
707
708
709
710void
711cifs_del_deferred_close(struct cifsFileInfo *cfile)
712{
713 bool is_deferred = false;
714 struct cifs_deferred_close *dclose;
715
716 is_deferred = cifs_is_deferred_close(cfile, &dclose);
717 if (!is_deferred)
718 return;
719 list_del(&dclose->dlist);
720 kfree(dclose);
721}
722
723void
724cifs_close_deferred_file(struct cifsInodeInfo *cifs_inode)
725{
726 struct cifsFileInfo *cfile = NULL;
727 struct file_list *tmp_list, *tmp_next_list;
728 struct list_head file_head;
729
730 if (cifs_inode == NULL)
731 return;
732
733 INIT_LIST_HEAD(&file_head);
734 spin_lock(&cifs_inode->open_file_lock);
735 list_for_each_entry(cfile, &cifs_inode->openFileList, flist) {
736 if (delayed_work_pending(&cfile->deferred)) {
737 if (cancel_delayed_work(&cfile->deferred)) {
738 tmp_list = kmalloc(sizeof(struct file_list), GFP_ATOMIC);
739 if (tmp_list == NULL)
740 break;
741 tmp_list->cfile = cfile;
742 list_add_tail(&tmp_list->list, &file_head);
743 }
744 }
745 }
746 spin_unlock(&cifs_inode->open_file_lock);
747
748 list_for_each_entry_safe(tmp_list, tmp_next_list, &file_head, list) {
749 _cifsFileInfo_put(tmp_list->cfile, true, false);
750 list_del(&tmp_list->list);
751 kfree(tmp_list);
752 }
753}
754
755void
756cifs_close_all_deferred_files(struct cifs_tcon *tcon)
757{
758 struct cifsFileInfo *cfile;
759 struct list_head *tmp;
760 struct file_list *tmp_list, *tmp_next_list;
761 struct list_head file_head;
762
763 INIT_LIST_HEAD(&file_head);
764 spin_lock(&tcon->open_file_lock);
765 list_for_each(tmp, &tcon->openFileList) {
766 cfile = list_entry(tmp, struct cifsFileInfo, tlist);
767 if (delayed_work_pending(&cfile->deferred)) {
768 if (cancel_delayed_work(&cfile->deferred)) {
769 tmp_list = kmalloc(sizeof(struct file_list), GFP_ATOMIC);
770 if (tmp_list == NULL)
771 break;
772 tmp_list->cfile = cfile;
773 list_add_tail(&tmp_list->list, &file_head);
774 }
775 }
776 }
777 spin_unlock(&tcon->open_file_lock);
778
779 list_for_each_entry_safe(tmp_list, tmp_next_list, &file_head, list) {
780 _cifsFileInfo_put(tmp_list->cfile, true, false);
781 list_del(&tmp_list->list);
782 kfree(tmp_list);
783 }
784}
785void
786cifs_close_deferred_file_under_dentry(struct cifs_tcon *tcon, const char *path)
787{
788 struct cifsFileInfo *cfile;
789 struct list_head *tmp;
790 struct file_list *tmp_list, *tmp_next_list;
791 struct list_head file_head;
792 void *page;
793 const char *full_path;
794
795 INIT_LIST_HEAD(&file_head);
796 page = alloc_dentry_path();
797 spin_lock(&tcon->open_file_lock);
798 list_for_each(tmp, &tcon->openFileList) {
799 cfile = list_entry(tmp, struct cifsFileInfo, tlist);
800 full_path = build_path_from_dentry(cfile->dentry, page);
801 if (strstr(full_path, path)) {
802 if (delayed_work_pending(&cfile->deferred)) {
803 if (cancel_delayed_work(&cfile->deferred)) {
804 tmp_list = kmalloc(sizeof(struct file_list), GFP_ATOMIC);
805 if (tmp_list == NULL)
806 break;
807 tmp_list->cfile = cfile;
808 list_add_tail(&tmp_list->list, &file_head);
809 }
810 }
811 }
812 }
813 spin_unlock(&tcon->open_file_lock);
814
815 list_for_each_entry_safe(tmp_list, tmp_next_list, &file_head, list) {
816 _cifsFileInfo_put(tmp_list->cfile, true, false);
817 list_del(&tmp_list->list);
818 kfree(tmp_list);
819 }
820 free_dentry_path(page);
821}
822
823
824
825
826
827
828
829int
830parse_dfs_referrals(struct get_dfs_referral_rsp *rsp, u32 rsp_size,
831 unsigned int *num_of_nodes,
832 struct dfs_info3_param **target_nodes,
833 const struct nls_table *nls_codepage, int remap,
834 const char *searchName, bool is_unicode)
835{
836 int i, rc = 0;
837 char *data_end;
838 struct dfs_referral_level_3 *ref;
839
840 *num_of_nodes = le16_to_cpu(rsp->NumberOfReferrals);
841
842 if (*num_of_nodes < 1) {
843 cifs_dbg(VFS, "num_referrals: must be at least > 0, but we get num_referrals = %d\n",
844 *num_of_nodes);
845 rc = -EINVAL;
846 goto parse_DFS_referrals_exit;
847 }
848
849 ref = (struct dfs_referral_level_3 *) &(rsp->referrals);
850 if (ref->VersionNumber != cpu_to_le16(3)) {
851 cifs_dbg(VFS, "Referrals of V%d version are not supported, should be V3\n",
852 le16_to_cpu(ref->VersionNumber));
853 rc = -EINVAL;
854 goto parse_DFS_referrals_exit;
855 }
856
857
858 data_end = (char *)rsp + rsp_size;
859
860 cifs_dbg(FYI, "num_referrals: %d dfs flags: 0x%x ...\n",
861 *num_of_nodes, le32_to_cpu(rsp->DFSFlags));
862
863 *target_nodes = kcalloc(*num_of_nodes, sizeof(struct dfs_info3_param),
864 GFP_KERNEL);
865 if (*target_nodes == NULL) {
866 rc = -ENOMEM;
867 goto parse_DFS_referrals_exit;
868 }
869
870
871 for (i = 0; i < *num_of_nodes; i++) {
872 char *temp;
873 int max_len;
874 struct dfs_info3_param *node = (*target_nodes)+i;
875
876 node->flags = le32_to_cpu(rsp->DFSFlags);
877 if (is_unicode) {
878 __le16 *tmp = kmalloc(strlen(searchName)*2 + 2,
879 GFP_KERNEL);
880 if (tmp == NULL) {
881 rc = -ENOMEM;
882 goto parse_DFS_referrals_exit;
883 }
884 cifsConvertToUTF16((__le16 *) tmp, searchName,
885 PATH_MAX, nls_codepage, remap);
886 node->path_consumed = cifs_utf16_bytes(tmp,
887 le16_to_cpu(rsp->PathConsumed),
888 nls_codepage);
889 kfree(tmp);
890 } else
891 node->path_consumed = le16_to_cpu(rsp->PathConsumed);
892
893 node->server_type = le16_to_cpu(ref->ServerType);
894 node->ref_flag = le16_to_cpu(ref->ReferralEntryFlags);
895
896
897 temp = (char *)ref + le16_to_cpu(ref->DfsPathOffset);
898 max_len = data_end - temp;
899 node->path_name = cifs_strndup_from_utf16(temp, max_len,
900 is_unicode, nls_codepage);
901 if (!node->path_name) {
902 rc = -ENOMEM;
903 goto parse_DFS_referrals_exit;
904 }
905
906
907 temp = (char *)ref + le16_to_cpu(ref->NetworkAddressOffset);
908 max_len = data_end - temp;
909 node->node_name = cifs_strndup_from_utf16(temp, max_len,
910 is_unicode, nls_codepage);
911 if (!node->node_name) {
912 rc = -ENOMEM;
913 goto parse_DFS_referrals_exit;
914 }
915
916 node->ttl = le32_to_cpu(ref->TimeToLive);
917
918 ref++;
919 }
920
921parse_DFS_referrals_exit:
922 if (rc) {
923 free_dfs_info_array(*target_nodes, *num_of_nodes);
924 *target_nodes = NULL;
925 *num_of_nodes = 0;
926 }
927 return rc;
928}
929
930struct cifs_aio_ctx *
931cifs_aio_ctx_alloc(void)
932{
933 struct cifs_aio_ctx *ctx;
934
935
936
937
938
939
940 ctx = kzalloc(sizeof(struct cifs_aio_ctx), GFP_KERNEL);
941 if (!ctx)
942 return NULL;
943
944 INIT_LIST_HEAD(&ctx->list);
945 mutex_init(&ctx->aio_mutex);
946 init_completion(&ctx->done);
947 kref_init(&ctx->refcount);
948 return ctx;
949}
950
951void
952cifs_aio_ctx_release(struct kref *refcount)
953{
954 struct cifs_aio_ctx *ctx = container_of(refcount,
955 struct cifs_aio_ctx, refcount);
956
957 cifsFileInfo_put(ctx->cfile);
958
959
960
961
962
963
964 if (ctx->bv) {
965 unsigned i;
966
967 for (i = 0; i < ctx->npages; i++) {
968 if (ctx->should_dirty)
969 set_page_dirty(ctx->bv[i].bv_page);
970 put_page(ctx->bv[i].bv_page);
971 }
972 kvfree(ctx->bv);
973 }
974
975 kfree(ctx);
976}
977
978#define CIFS_AIO_KMALLOC_LIMIT (1024 * 1024)
979
980int
981setup_aio_ctx_iter(struct cifs_aio_ctx *ctx, struct iov_iter *iter, int rw)
982{
983 ssize_t rc;
984 unsigned int cur_npages;
985 unsigned int npages = 0;
986 unsigned int i;
987 size_t len;
988 size_t count = iov_iter_count(iter);
989 unsigned int saved_len;
990 size_t start;
991 unsigned int max_pages = iov_iter_npages(iter, INT_MAX);
992 struct page **pages = NULL;
993 struct bio_vec *bv = NULL;
994
995 if (iov_iter_is_kvec(iter)) {
996 memcpy(&ctx->iter, iter, sizeof(*iter));
997 ctx->len = count;
998 iov_iter_advance(iter, count);
999 return 0;
1000 }
1001
1002 if (array_size(max_pages, sizeof(*bv)) <= CIFS_AIO_KMALLOC_LIMIT)
1003 bv = kmalloc_array(max_pages, sizeof(*bv), GFP_KERNEL);
1004
1005 if (!bv) {
1006 bv = vmalloc(array_size(max_pages, sizeof(*bv)));
1007 if (!bv)
1008 return -ENOMEM;
1009 }
1010
1011 if (array_size(max_pages, sizeof(*pages)) <= CIFS_AIO_KMALLOC_LIMIT)
1012 pages = kmalloc_array(max_pages, sizeof(*pages), GFP_KERNEL);
1013
1014 if (!pages) {
1015 pages = vmalloc(array_size(max_pages, sizeof(*pages)));
1016 if (!pages) {
1017 kvfree(bv);
1018 return -ENOMEM;
1019 }
1020 }
1021
1022 saved_len = count;
1023
1024 while (count && npages < max_pages) {
1025 rc = iov_iter_get_pages(iter, pages, count, max_pages, &start);
1026 if (rc < 0) {
1027 cifs_dbg(VFS, "Couldn't get user pages (rc=%zd)\n", rc);
1028 break;
1029 }
1030
1031 if (rc > count) {
1032 cifs_dbg(VFS, "get pages rc=%zd more than %zu\n", rc,
1033 count);
1034 break;
1035 }
1036
1037 iov_iter_advance(iter, rc);
1038 count -= rc;
1039 rc += start;
1040 cur_npages = DIV_ROUND_UP(rc, PAGE_SIZE);
1041
1042 if (npages + cur_npages > max_pages) {
1043 cifs_dbg(VFS, "out of vec array capacity (%u vs %u)\n",
1044 npages + cur_npages, max_pages);
1045 break;
1046 }
1047
1048 for (i = 0; i < cur_npages; i++) {
1049 len = rc > PAGE_SIZE ? PAGE_SIZE : rc;
1050 bv[npages + i].bv_page = pages[i];
1051 bv[npages + i].bv_offset = start;
1052 bv[npages + i].bv_len = len - start;
1053 rc -= len;
1054 start = 0;
1055 }
1056
1057 npages += cur_npages;
1058 }
1059
1060 kvfree(pages);
1061 ctx->bv = bv;
1062 ctx->len = saved_len - count;
1063 ctx->npages = npages;
1064 iov_iter_bvec(&ctx->iter, rw, ctx->bv, npages, ctx->len);
1065 return 0;
1066}
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077int
1078cifs_alloc_hash(const char *name,
1079 struct crypto_shash **shash, struct sdesc **sdesc)
1080{
1081 int rc = 0;
1082 size_t size;
1083
1084 if (*sdesc != NULL)
1085 return 0;
1086
1087 *shash = crypto_alloc_shash(name, 0, 0);
1088 if (IS_ERR(*shash)) {
1089 cifs_dbg(VFS, "Could not allocate crypto %s\n", name);
1090 rc = PTR_ERR(*shash);
1091 *shash = NULL;
1092 *sdesc = NULL;
1093 return rc;
1094 }
1095
1096 size = sizeof(struct shash_desc) + crypto_shash_descsize(*shash);
1097 *sdesc = kmalloc(size, GFP_KERNEL);
1098 if (*sdesc == NULL) {
1099 cifs_dbg(VFS, "no memory left to allocate crypto %s\n", name);
1100 crypto_free_shash(*shash);
1101 *shash = NULL;
1102 return -ENOMEM;
1103 }
1104
1105 (*sdesc)->shash.tfm = *shash;
1106 return 0;
1107}
1108
1109
1110
1111
1112
1113
1114
1115
1116void
1117cifs_free_hash(struct crypto_shash **shash, struct sdesc **sdesc)
1118{
1119 kfree(*sdesc);
1120 *sdesc = NULL;
1121 if (*shash)
1122 crypto_free_shash(*shash);
1123 *shash = NULL;
1124}
1125
1126
1127
1128
1129
1130
1131
1132
1133void rqst_page_get_length(struct smb_rqst *rqst, unsigned int page,
1134 unsigned int *len, unsigned int *offset)
1135{
1136 *len = rqst->rq_pagesz;
1137 *offset = (page == 0) ? rqst->rq_offset : 0;
1138
1139 if (rqst->rq_npages == 1 || page == rqst->rq_npages-1)
1140 *len = rqst->rq_tailsz;
1141 else if (page == 0)
1142 *len = rqst->rq_pagesz - rqst->rq_offset;
1143}
1144
1145void extract_unc_hostname(const char *unc, const char **h, size_t *len)
1146{
1147 const char *end;
1148
1149
1150 while (*unc && (*unc == '\\' || *unc == '/'))
1151 unc++;
1152
1153 end = unc;
1154
1155 while (*end && !(*end == '\\' || *end == '/'))
1156 end++;
1157
1158 *h = unc;
1159 *len = end - unc;
1160}
1161
1162
1163
1164
1165
1166
1167
1168
1169int copy_path_name(char *dst, const char *src)
1170{
1171 int name_len;
1172
1173
1174
1175
1176
1177 name_len = strscpy(dst, src, PATH_MAX);
1178 if (WARN_ON_ONCE(name_len < 0))
1179 name_len = PATH_MAX-1;
1180
1181
1182 name_len++;
1183 return name_len;
1184}
1185
1186struct super_cb_data {
1187 void *data;
1188 struct super_block *sb;
1189};
1190
1191static void tcp_super_cb(struct super_block *sb, void *arg)
1192{
1193 struct super_cb_data *sd = arg;
1194 struct TCP_Server_Info *server = sd->data;
1195 struct cifs_sb_info *cifs_sb;
1196 struct cifs_tcon *tcon;
1197
1198 if (sd->sb)
1199 return;
1200
1201 cifs_sb = CIFS_SB(sb);
1202 tcon = cifs_sb_master_tcon(cifs_sb);
1203 if (tcon->ses->server == server)
1204 sd->sb = sb;
1205}
1206
1207static struct super_block *__cifs_get_super(void (*f)(struct super_block *, void *),
1208 void *data)
1209{
1210 struct super_cb_data sd = {
1211 .data = data,
1212 .sb = NULL,
1213 };
1214
1215 iterate_supers_type(&cifs_fs_type, f, &sd);
1216
1217 if (!sd.sb)
1218 return ERR_PTR(-EINVAL);
1219
1220
1221
1222
1223
1224 cifs_sb_active(sd.sb);
1225 return sd.sb;
1226}
1227
1228static void __cifs_put_super(struct super_block *sb)
1229{
1230 if (!IS_ERR_OR_NULL(sb))
1231 cifs_sb_deactive(sb);
1232}
1233
1234struct super_block *cifs_get_tcp_super(struct TCP_Server_Info *server)
1235{
1236 return __cifs_get_super(tcp_super_cb, server);
1237}
1238
1239void cifs_put_tcp_super(struct super_block *sb)
1240{
1241 __cifs_put_super(sb);
1242}
1243
1244#ifdef CONFIG_CIFS_DFS_UPCALL
1245int match_target_ip(struct TCP_Server_Info *server,
1246 const char *share, size_t share_len,
1247 bool *result)
1248{
1249 int rc;
1250 char *target, *tip = NULL;
1251 struct sockaddr tipaddr;
1252
1253 *result = false;
1254
1255 target = kzalloc(share_len + 3, GFP_KERNEL);
1256 if (!target) {
1257 rc = -ENOMEM;
1258 goto out;
1259 }
1260
1261 scnprintf(target, share_len + 3, "\\\\%.*s", (int)share_len, share);
1262
1263 cifs_dbg(FYI, "%s: target name: %s\n", __func__, target + 2);
1264
1265 rc = dns_resolve_server_name_to_ip(target, &tip, NULL);
1266 if (rc < 0)
1267 goto out;
1268
1269 cifs_dbg(FYI, "%s: target ip: %s\n", __func__, tip);
1270
1271 if (!cifs_convert_address(&tipaddr, tip, strlen(tip))) {
1272 cifs_dbg(VFS, "%s: failed to convert target ip address\n",
1273 __func__);
1274 rc = -EINVAL;
1275 goto out;
1276 }
1277
1278 *result = cifs_match_ipaddr((struct sockaddr *)&server->dstaddr,
1279 &tipaddr);
1280 cifs_dbg(FYI, "%s: ip addresses match: %u\n", __func__, *result);
1281 rc = 0;
1282
1283out:
1284 kfree(target);
1285 kfree(tip);
1286
1287 return rc;
1288}
1289
1290static void tcon_super_cb(struct super_block *sb, void *arg)
1291{
1292 struct super_cb_data *sd = arg;
1293 struct cifs_tcon *tcon = sd->data;
1294 struct cifs_sb_info *cifs_sb;
1295
1296 if (sd->sb)
1297 return;
1298
1299 cifs_sb = CIFS_SB(sb);
1300 if (tcon->dfs_path && cifs_sb->origin_fullpath &&
1301 !strcasecmp(tcon->dfs_path, cifs_sb->origin_fullpath))
1302 sd->sb = sb;
1303}
1304
1305static inline struct super_block *cifs_get_tcon_super(struct cifs_tcon *tcon)
1306{
1307 return __cifs_get_super(tcon_super_cb, tcon);
1308}
1309
1310static inline void cifs_put_tcon_super(struct super_block *sb)
1311{
1312 __cifs_put_super(sb);
1313}
1314#else
1315static inline struct super_block *cifs_get_tcon_super(struct cifs_tcon *tcon)
1316{
1317 return ERR_PTR(-EOPNOTSUPP);
1318}
1319
1320static inline void cifs_put_tcon_super(struct super_block *sb)
1321{
1322}
1323#endif
1324
1325int update_super_prepath(struct cifs_tcon *tcon, char *prefix)
1326{
1327 struct super_block *sb;
1328 struct cifs_sb_info *cifs_sb;
1329 int rc = 0;
1330
1331 sb = cifs_get_tcon_super(tcon);
1332 if (IS_ERR(sb))
1333 return PTR_ERR(sb);
1334
1335 cifs_sb = CIFS_SB(sb);
1336
1337 kfree(cifs_sb->prepath);
1338
1339 if (prefix && *prefix) {
1340 cifs_sb->prepath = kstrdup(prefix, GFP_ATOMIC);
1341 if (!cifs_sb->prepath) {
1342 rc = -ENOMEM;
1343 goto out;
1344 }
1345
1346 convert_delimiter(cifs_sb->prepath, CIFS_DIR_SEP(cifs_sb));
1347 } else
1348 cifs_sb->prepath = NULL;
1349
1350 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
1351
1352out:
1353 cifs_put_tcon_super(sb);
1354 return rc;
1355}
1356