1 // SPDX-License-Identifier: LGPL-2.1
4 * Copyright (C) International Business Machines Corp., 2002,2008
5 * Author(s): Steve French (sfrench@us.ibm.com)
9 #include <linux/slab.h>
10 #include <linux/ctype.h>
11 #include <linux/mempool.h>
12 #include <linux/vmalloc.h>
15 #include "cifsproto.h"
16 #include "cifs_debug.h"
19 #include "cifs_unicode.h"
22 #ifdef CONFIG_CIFS_DFS_UPCALL
23 #include "dns_resolve.h"
25 #include "fs_context.h"
27 extern mempool_t *cifs_sm_req_poolp;
28 extern mempool_t *cifs_req_poolp;
30 /* The xid serves as a useful identifier for each incoming vfs request,
31 in a similar way to the mid which is useful to track each sent smb,
32 and CurrentXid can also provide a running counter (although it
33 will eventually wrap past zero) of the total vfs operations handled
34 since the cifs fs was mounted */
41 spin_lock(&GlobalMid_Lock);
42 GlobalTotalActiveXid++;
44 /* keep high water mark for number of simultaneous ops in filesystem */
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);
55 _free_xid(unsigned int xid)
57 spin_lock(&GlobalMid_Lock);
58 /* if (GlobalTotalActiveXid == 0)
60 GlobalTotalActiveXid--;
61 spin_unlock(&GlobalMid_Lock);
67 struct cifs_ses *ret_buf;
69 ret_buf = kzalloc(sizeof(struct cifs_ses), GFP_KERNEL);
71 atomic_inc(&sesInfoAllocCount);
72 ret_buf->status = CifsNew;
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 spin_lock_init(&ret_buf->chan_lock);
84 sesInfoFree(struct cifs_ses *buf_to_free)
86 if (buf_to_free == NULL) {
87 cifs_dbg(FYI, "Null buffer passed to sesInfoFree\n");
91 atomic_dec(&sesInfoAllocCount);
92 kfree(buf_to_free->serverOS);
93 kfree(buf_to_free->serverDomain);
94 kfree(buf_to_free->serverNOS);
95 kfree_sensitive(buf_to_free->password);
96 kfree(buf_to_free->user_name);
97 kfree(buf_to_free->domainName);
98 kfree_sensitive(buf_to_free->auth_key.response);
99 kfree(buf_to_free->iface_list);
100 kfree_sensitive(buf_to_free);
106 struct cifs_tcon *ret_buf;
108 ret_buf = kzalloc(sizeof(*ret_buf), GFP_KERNEL);
111 ret_buf->crfid.fid = kzalloc(sizeof(*ret_buf->crfid.fid), GFP_KERNEL);
112 if (!ret_buf->crfid.fid) {
117 atomic_inc(&tconInfoAllocCount);
118 ret_buf->tidStatus = CifsNew;
120 INIT_LIST_HEAD(&ret_buf->openFileList);
121 INIT_LIST_HEAD(&ret_buf->tcon_list);
122 spin_lock_init(&ret_buf->open_file_lock);
123 mutex_init(&ret_buf->crfid.fid_mutex);
124 spin_lock_init(&ret_buf->stat_lock);
125 atomic_set(&ret_buf->num_local_opens, 0);
126 atomic_set(&ret_buf->num_remote_opens, 0);
132 tconInfoFree(struct cifs_tcon *buf_to_free)
134 if (buf_to_free == NULL) {
135 cifs_dbg(FYI, "Null buffer passed to tconInfoFree\n");
138 atomic_dec(&tconInfoAllocCount);
139 kfree(buf_to_free->nativeFileSystem);
140 kfree_sensitive(buf_to_free->password);
141 kfree(buf_to_free->crfid.fid);
148 struct smb_hdr *ret_buf = NULL;
150 * SMB2 header is bigger than CIFS one - no problems to clean some
151 * more bytes for CIFS.
153 size_t buf_size = sizeof(struct smb2_sync_hdr);
156 * We could use negotiated size instead of max_msgsize -
157 * but it may be more efficient to always alloc same size
158 * albeit slightly larger than necessary and maxbuffersize
159 * defaults to this and can not be bigger.
161 ret_buf = mempool_alloc(cifs_req_poolp, GFP_NOFS);
163 /* clear the first few header bytes */
164 /* for most paths, more is cleared in header_assemble */
165 memset(ret_buf, 0, buf_size + 3);
166 atomic_inc(&bufAllocCount);
167 #ifdef CONFIG_CIFS_STATS2
168 atomic_inc(&totBufAllocCount);
169 #endif /* CONFIG_CIFS_STATS2 */
175 cifs_buf_release(void *buf_to_free)
177 if (buf_to_free == NULL) {
178 /* cifs_dbg(FYI, "Null buffer passed to cifs_buf_release\n");*/
181 mempool_free(buf_to_free, cifs_req_poolp);
183 atomic_dec(&bufAllocCount);
188 cifs_small_buf_get(void)
190 struct smb_hdr *ret_buf = NULL;
192 /* We could use negotiated size instead of max_msgsize -
193 but it may be more efficient to always alloc same size
194 albeit slightly larger than necessary and maxbuffersize
195 defaults to this and can not be bigger */
196 ret_buf = mempool_alloc(cifs_sm_req_poolp, GFP_NOFS);
197 /* No need to clear memory here, cleared in header assemble */
198 /* memset(ret_buf, 0, sizeof(struct smb_hdr) + 27);*/
199 atomic_inc(&smBufAllocCount);
200 #ifdef CONFIG_CIFS_STATS2
201 atomic_inc(&totSmBufAllocCount);
202 #endif /* CONFIG_CIFS_STATS2 */
208 cifs_small_buf_release(void *buf_to_free)
211 if (buf_to_free == NULL) {
212 cifs_dbg(FYI, "Null buffer passed to cifs_small_buf_release\n");
215 mempool_free(buf_to_free, cifs_sm_req_poolp);
217 atomic_dec(&smBufAllocCount);
222 free_rsp_buf(int resp_buftype, void *rsp)
224 if (resp_buftype == CIFS_SMALL_BUFFER)
225 cifs_small_buf_release(rsp);
226 else if (resp_buftype == CIFS_LARGE_BUFFER)
227 cifs_buf_release(rsp);
230 /* NB: MID can not be set if treeCon not passed in, in that
231 case it is responsbility of caller to set the mid */
233 header_assemble(struct smb_hdr *buffer, char smb_command /* command */ ,
234 const struct cifs_tcon *treeCon, int word_count
235 /* length of fixed section (word count) in two byte units */)
237 char *temp = (char *) buffer;
239 memset(temp, 0, 256); /* bigger than MAX_CIFS_HDR_SIZE */
241 buffer->smb_buf_length = cpu_to_be32(
242 (2 * word_count) + sizeof(struct smb_hdr) -
243 4 /* RFC 1001 length field does not count */ +
244 2 /* for bcc field itself */) ;
246 buffer->Protocol[0] = 0xFF;
247 buffer->Protocol[1] = 'S';
248 buffer->Protocol[2] = 'M';
249 buffer->Protocol[3] = 'B';
250 buffer->Command = smb_command;
251 buffer->Flags = 0x00; /* case sensitive */
252 buffer->Flags2 = SMBFLG2_KNOWS_LONG_NAMES;
253 buffer->Pid = cpu_to_le16((__u16)current->tgid);
254 buffer->PidHigh = cpu_to_le16((__u16)(current->tgid >> 16));
256 buffer->Tid = treeCon->tid;
258 if (treeCon->ses->capabilities & CAP_UNICODE)
259 buffer->Flags2 |= SMBFLG2_UNICODE;
260 if (treeCon->ses->capabilities & CAP_STATUS32)
261 buffer->Flags2 |= SMBFLG2_ERR_STATUS;
263 /* Uid is not converted */
264 buffer->Uid = treeCon->ses->Suid;
265 if (treeCon->ses->server)
266 buffer->Mid = get_next_mid(treeCon->ses->server);
268 if (treeCon->Flags & SMB_SHARE_IS_IN_DFS)
269 buffer->Flags2 |= SMBFLG2_DFS;
271 buffer->Flags |= SMBFLG_CASELESS;
272 if ((treeCon->ses) && (treeCon->ses->server))
273 if (treeCon->ses->server->sign)
274 buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
277 /* endian conversion of flags is now done just before sending */
278 buffer->WordCount = (char) word_count;
283 check_smb_hdr(struct smb_hdr *smb)
285 /* does it have the right SMB "signature" ? */
286 if (*(__le32 *) smb->Protocol != cpu_to_le32(0x424d53ff)) {
287 cifs_dbg(VFS, "Bad protocol string signature header 0x%x\n",
288 *(unsigned int *)smb->Protocol);
292 /* if it's a response then accept */
293 if (smb->Flags & SMBFLG_RESPONSE)
296 /* only one valid case where server sends us request */
297 if (smb->Command == SMB_COM_LOCKING_ANDX)
300 cifs_dbg(VFS, "Server sent request, not response. mid=%u\n",
306 checkSMB(char *buf, unsigned int total_read, struct TCP_Server_Info *server)
308 struct smb_hdr *smb = (struct smb_hdr *)buf;
309 __u32 rfclen = be32_to_cpu(smb->smb_buf_length);
310 __u32 clc_len; /* calculated length */
311 cifs_dbg(FYI, "checkSMB Length: 0x%x, smb_buf_length: 0x%x\n",
314 /* is this frame too small to even get to a BCC? */
315 if (total_read < 2 + sizeof(struct smb_hdr)) {
316 if ((total_read >= sizeof(struct smb_hdr) - 1)
317 && (smb->Status.CifsError != 0)) {
318 /* it's an error return */
320 /* some error cases do not return wct and bcc */
322 } else if ((total_read == sizeof(struct smb_hdr) + 1) &&
323 (smb->WordCount == 0)) {
324 char *tmp = (char *)smb;
325 /* Need to work around a bug in two servers here */
326 /* First, check if the part of bcc they sent was zero */
327 if (tmp[sizeof(struct smb_hdr)] == 0) {
328 /* some servers return only half of bcc
329 * on simple responses (wct, bcc both zero)
330 * in particular have seen this on
331 * ulogoffX and FindClose. This leaves
332 * one byte of bcc potentially unitialized
334 /* zero rest of bcc */
335 tmp[sizeof(struct smb_hdr)+1] = 0;
338 cifs_dbg(VFS, "rcvd invalid byte count (bcc)\n");
340 cifs_dbg(VFS, "Length less than smb header size\n");
345 /* otherwise, there is enough to get to the BCC */
346 if (check_smb_hdr(smb))
348 clc_len = smbCalcSize(smb, server);
350 if (4 + rfclen != total_read) {
351 cifs_dbg(VFS, "Length read does not match RFC1001 length %d\n",
356 if (4 + rfclen != clc_len) {
357 __u16 mid = get_mid(smb);
358 /* check if bcc wrapped around for large read responses */
359 if ((rfclen > 64 * 1024) && (rfclen > clc_len)) {
360 /* check if lengths match mod 64K */
361 if (((4 + rfclen) & 0xFFFF) == (clc_len & 0xFFFF))
362 return 0; /* bcc wrapped */
364 cifs_dbg(FYI, "Calculated size %u vs length %u mismatch for mid=%u\n",
365 clc_len, 4 + rfclen, mid);
367 if (4 + rfclen < clc_len) {
368 cifs_dbg(VFS, "RFC1001 size %u smaller than SMB for mid=%u\n",
371 } else if (rfclen > clc_len + 512) {
373 * Some servers (Windows XP in particular) send more
374 * data than the lengths in the SMB packet would
375 * indicate on certain calls (byte range locks and
376 * trans2 find first calls in particular). While the
377 * client can handle such a frame by ignoring the
378 * trailing data, we choose limit the amount of extra
381 cifs_dbg(VFS, "RFC1001 size %u more than 512 bytes larger than SMB for mid=%u\n",
390 is_valid_oplock_break(char *buffer, struct TCP_Server_Info *srv)
392 struct smb_hdr *buf = (struct smb_hdr *)buffer;
393 struct smb_com_lock_req *pSMB = (struct smb_com_lock_req *)buf;
394 struct list_head *tmp, *tmp1, *tmp2;
395 struct cifs_ses *ses;
396 struct cifs_tcon *tcon;
397 struct cifsInodeInfo *pCifsInode;
398 struct cifsFileInfo *netfile;
400 cifs_dbg(FYI, "Checking for oplock break or dnotify response\n");
401 if ((pSMB->hdr.Command == SMB_COM_NT_TRANSACT) &&
402 (pSMB->hdr.Flags & SMBFLG_RESPONSE)) {
403 struct smb_com_transaction_change_notify_rsp *pSMBr =
404 (struct smb_com_transaction_change_notify_rsp *)buf;
405 struct file_notify_information *pnotify;
406 __u32 data_offset = 0;
407 size_t len = srv->total_read - sizeof(pSMBr->hdr.smb_buf_length);
409 if (get_bcc(buf) > sizeof(struct file_notify_information)) {
410 data_offset = le32_to_cpu(pSMBr->DataOffset);
413 len - sizeof(struct file_notify_information)) {
414 cifs_dbg(FYI, "Invalid data_offset %u\n",
418 pnotify = (struct file_notify_information *)
419 ((char *)&pSMBr->hdr.Protocol + data_offset);
420 cifs_dbg(FYI, "dnotify on %s Action: 0x%x\n",
421 pnotify->FileName, pnotify->Action);
422 /* cifs_dump_mem("Rcvd notify Data: ",buf,
423 sizeof(struct smb_hdr)+60); */
426 if (pSMBr->hdr.Status.CifsError) {
427 cifs_dbg(FYI, "notify err 0x%x\n",
428 pSMBr->hdr.Status.CifsError);
433 if (pSMB->hdr.Command != SMB_COM_LOCKING_ANDX)
435 if (pSMB->hdr.Flags & SMBFLG_RESPONSE) {
436 /* no sense logging error on invalid handle on oplock
437 break - harmless race between close request and oplock
438 break response is expected from time to time writing out
439 large dirty files cached on the client */
440 if ((NT_STATUS_INVALID_HANDLE) ==
441 le32_to_cpu(pSMB->hdr.Status.CifsError)) {
442 cifs_dbg(FYI, "Invalid handle on oplock break\n");
444 } else if (ERRbadfid ==
445 le16_to_cpu(pSMB->hdr.Status.DosError.Error)) {
448 return false; /* on valid oplock brk we get "request" */
451 if (pSMB->hdr.WordCount != 8)
454 cifs_dbg(FYI, "oplock type 0x%x level 0x%x\n",
455 pSMB->LockType, pSMB->OplockLevel);
456 if (!(pSMB->LockType & LOCKING_ANDX_OPLOCK_RELEASE))
459 /* look up tcon based on tid & uid */
460 spin_lock(&cifs_tcp_ses_lock);
461 list_for_each(tmp, &srv->smb_ses_list) {
462 ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
463 list_for_each(tmp1, &ses->tcon_list) {
464 tcon = list_entry(tmp1, struct cifs_tcon, tcon_list);
465 if (tcon->tid != buf->Tid)
468 cifs_stats_inc(&tcon->stats.cifs_stats.num_oplock_brks);
469 spin_lock(&tcon->open_file_lock);
470 list_for_each(tmp2, &tcon->openFileList) {
471 netfile = list_entry(tmp2, struct cifsFileInfo,
473 if (pSMB->Fid != netfile->fid.netfid)
476 cifs_dbg(FYI, "file id match, oplock break\n");
477 pCifsInode = CIFS_I(d_inode(netfile->dentry));
479 set_bit(CIFS_INODE_PENDING_OPLOCK_BREAK,
482 netfile->oplock_epoch = 0;
483 netfile->oplock_level = pSMB->OplockLevel;
484 netfile->oplock_break_cancelled = false;
485 cifs_queue_oplock_break(netfile);
487 spin_unlock(&tcon->open_file_lock);
488 spin_unlock(&cifs_tcp_ses_lock);
491 spin_unlock(&tcon->open_file_lock);
492 spin_unlock(&cifs_tcp_ses_lock);
493 cifs_dbg(FYI, "No matching file for oplock break\n");
497 spin_unlock(&cifs_tcp_ses_lock);
498 cifs_dbg(FYI, "Can not process oplock break for non-existent connection\n");
503 dump_smb(void *buf, int smb_buf_length)
508 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_NONE, 8, 2, buf,
509 smb_buf_length, true);
513 cifs_autodisable_serverino(struct cifs_sb_info *cifs_sb)
515 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
516 struct cifs_tcon *tcon = NULL;
518 if (cifs_sb->master_tlink)
519 tcon = cifs_sb_master_tcon(cifs_sb);
521 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM;
522 cifs_sb->mnt_cifs_serverino_autodisabled = true;
523 cifs_dbg(VFS, "Autodisabling the use of server inode numbers on %s\n",
524 tcon ? tcon->treeName : "new server");
525 cifs_dbg(VFS, "The server doesn't seem to support them properly or the files might be on different servers (DFS)\n");
526 cifs_dbg(VFS, "Hardlinks will not be recognized on this mount. Consider mounting with the \"noserverino\" option to silence this message.\n");
531 void cifs_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock)
535 if (oplock == OPLOCK_EXCLUSIVE) {
536 cinode->oplock = CIFS_CACHE_WRITE_FLG | CIFS_CACHE_READ_FLG;
537 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
539 } else if (oplock == OPLOCK_READ) {
540 cinode->oplock = CIFS_CACHE_READ_FLG;
541 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
548 * We wait for oplock breaks to be processed before we attempt to perform
551 int cifs_get_writer(struct cifsInodeInfo *cinode)
556 rc = wait_on_bit(&cinode->flags, CIFS_INODE_PENDING_OPLOCK_BREAK,
561 spin_lock(&cinode->writers_lock);
562 if (!cinode->writers)
563 set_bit(CIFS_INODE_PENDING_WRITERS, &cinode->flags);
565 /* Check to see if we have started servicing an oplock break */
566 if (test_bit(CIFS_INODE_PENDING_OPLOCK_BREAK, &cinode->flags)) {
568 if (cinode->writers == 0) {
569 clear_bit(CIFS_INODE_PENDING_WRITERS, &cinode->flags);
570 wake_up_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS);
572 spin_unlock(&cinode->writers_lock);
575 spin_unlock(&cinode->writers_lock);
579 void cifs_put_writer(struct cifsInodeInfo *cinode)
581 spin_lock(&cinode->writers_lock);
583 if (cinode->writers == 0) {
584 clear_bit(CIFS_INODE_PENDING_WRITERS, &cinode->flags);
585 wake_up_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS);
587 spin_unlock(&cinode->writers_lock);
591 * cifs_queue_oplock_break - queue the oplock break handler for cfile
592 * @cfile: The file to break the oplock on
594 * This function is called from the demultiplex thread when it
595 * receives an oplock break for @cfile.
597 * Assumes the tcon->open_file_lock is held.
598 * Assumes cfile->file_info_lock is NOT held.
600 void cifs_queue_oplock_break(struct cifsFileInfo *cfile)
603 * Bump the handle refcount now while we hold the
604 * open_file_lock to enforce the validity of it for the oplock
605 * break handler. The matching put is done at the end of the
608 cifsFileInfo_get(cfile);
610 queue_work(cifsoplockd_wq, &cfile->oplock_break);
613 void cifs_done_oplock_break(struct cifsInodeInfo *cinode)
615 clear_bit(CIFS_INODE_PENDING_OPLOCK_BREAK, &cinode->flags);
616 wake_up_bit(&cinode->flags, CIFS_INODE_PENDING_OPLOCK_BREAK);
620 backup_cred(struct cifs_sb_info *cifs_sb)
622 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_BACKUPUID) {
623 if (uid_eq(cifs_sb->ctx->backupuid, current_fsuid()))
626 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_BACKUPGID) {
627 if (in_group_p(cifs_sb->ctx->backupgid))
635 cifs_del_pending_open(struct cifs_pending_open *open)
637 spin_lock(&tlink_tcon(open->tlink)->open_file_lock);
638 list_del(&open->olist);
639 spin_unlock(&tlink_tcon(open->tlink)->open_file_lock);
643 cifs_add_pending_open_locked(struct cifs_fid *fid, struct tcon_link *tlink,
644 struct cifs_pending_open *open)
646 memcpy(open->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
647 open->oplock = CIFS_OPLOCK_NO_CHANGE;
649 fid->pending_open = open;
650 list_add_tail(&open->olist, &tlink_tcon(tlink)->pending_opens);
654 cifs_add_pending_open(struct cifs_fid *fid, struct tcon_link *tlink,
655 struct cifs_pending_open *open)
657 spin_lock(&tlink_tcon(tlink)->open_file_lock);
658 cifs_add_pending_open_locked(fid, tlink, open);
659 spin_unlock(&tlink_tcon(open->tlink)->open_file_lock);
663 * Critical section which runs after acquiring deferred_lock.
664 * As there is no reference count on cifs_deferred_close, pdclose
665 * should not be used outside deferred_lock.
668 cifs_is_deferred_close(struct cifsFileInfo *cfile, struct cifs_deferred_close **pdclose)
670 struct cifs_deferred_close *dclose;
672 list_for_each_entry(dclose, &CIFS_I(d_inode(cfile->dentry))->deferred_closes, dlist) {
673 if ((dclose->netfid == cfile->fid.netfid) &&
674 (dclose->persistent_fid == cfile->fid.persistent_fid) &&
675 (dclose->volatile_fid == cfile->fid.volatile_fid)) {
684 * Critical section which runs after acquiring deferred_lock.
687 cifs_add_deferred_close(struct cifsFileInfo *cfile, struct cifs_deferred_close *dclose)
689 bool is_deferred = false;
690 struct cifs_deferred_close *pdclose;
692 is_deferred = cifs_is_deferred_close(cfile, &pdclose);
698 dclose->tlink = cfile->tlink;
699 dclose->netfid = cfile->fid.netfid;
700 dclose->persistent_fid = cfile->fid.persistent_fid;
701 dclose->volatile_fid = cfile->fid.volatile_fid;
702 list_add_tail(&dclose->dlist, &CIFS_I(d_inode(cfile->dentry))->deferred_closes);
706 * Critical section which runs after acquiring deferred_lock.
709 cifs_del_deferred_close(struct cifsFileInfo *cfile)
711 bool is_deferred = false;
712 struct cifs_deferred_close *dclose;
714 is_deferred = cifs_is_deferred_close(cfile, &dclose);
717 list_del(&dclose->dlist);
722 cifs_close_deferred_file(struct cifsInodeInfo *cifs_inode)
724 struct cifsFileInfo *cfile = NULL;
725 struct file_list *tmp_list, *tmp_next_list;
726 struct list_head file_head;
728 if (cifs_inode == NULL)
731 INIT_LIST_HEAD(&file_head);
732 spin_lock(&cifs_inode->open_file_lock);
733 list_for_each_entry(cfile, &cifs_inode->openFileList, flist) {
734 if (delayed_work_pending(&cfile->deferred)) {
735 if (cancel_delayed_work(&cfile->deferred)) {
736 spin_lock(&cifs_inode->deferred_lock);
737 cifs_del_deferred_close(cfile);
738 spin_unlock(&cifs_inode->deferred_lock);
740 tmp_list = kmalloc(sizeof(struct file_list), GFP_ATOMIC);
741 if (tmp_list == NULL)
743 tmp_list->cfile = cfile;
744 list_add_tail(&tmp_list->list, &file_head);
748 spin_unlock(&cifs_inode->open_file_lock);
750 list_for_each_entry_safe(tmp_list, tmp_next_list, &file_head, list) {
751 _cifsFileInfo_put(tmp_list->cfile, false, false);
752 list_del(&tmp_list->list);
758 cifs_close_all_deferred_files(struct cifs_tcon *tcon)
760 struct cifsFileInfo *cfile;
761 struct list_head *tmp;
762 struct file_list *tmp_list, *tmp_next_list;
763 struct list_head file_head;
765 INIT_LIST_HEAD(&file_head);
766 spin_lock(&tcon->open_file_lock);
767 list_for_each(tmp, &tcon->openFileList) {
768 cfile = list_entry(tmp, struct cifsFileInfo, tlist);
769 if (delayed_work_pending(&cfile->deferred)) {
770 if (cancel_delayed_work(&cfile->deferred)) {
771 spin_lock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock);
772 cifs_del_deferred_close(cfile);
773 spin_unlock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock);
775 tmp_list = kmalloc(sizeof(struct file_list), GFP_ATOMIC);
776 if (tmp_list == NULL)
778 tmp_list->cfile = cfile;
779 list_add_tail(&tmp_list->list, &file_head);
783 spin_unlock(&tcon->open_file_lock);
785 list_for_each_entry_safe(tmp_list, tmp_next_list, &file_head, list) {
786 _cifsFileInfo_put(tmp_list->cfile, true, false);
787 list_del(&tmp_list->list);
792 cifs_close_deferred_file_under_dentry(struct cifs_tcon *tcon, const char *path)
794 struct cifsFileInfo *cfile;
795 struct list_head *tmp;
796 struct file_list *tmp_list, *tmp_next_list;
797 struct list_head file_head;
799 const char *full_path;
801 INIT_LIST_HEAD(&file_head);
802 page = alloc_dentry_path();
803 spin_lock(&tcon->open_file_lock);
804 list_for_each(tmp, &tcon->openFileList) {
805 cfile = list_entry(tmp, struct cifsFileInfo, tlist);
806 full_path = build_path_from_dentry(cfile->dentry, page);
807 if (strstr(full_path, path)) {
808 if (delayed_work_pending(&cfile->deferred)) {
809 if (cancel_delayed_work(&cfile->deferred)) {
810 spin_lock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock);
811 cifs_del_deferred_close(cfile);
812 spin_unlock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock);
814 tmp_list = kmalloc(sizeof(struct file_list), GFP_ATOMIC);
815 if (tmp_list == NULL)
817 tmp_list->cfile = cfile;
818 list_add_tail(&tmp_list->list, &file_head);
823 spin_unlock(&tcon->open_file_lock);
825 list_for_each_entry_safe(tmp_list, tmp_next_list, &file_head, list) {
826 _cifsFileInfo_put(tmp_list->cfile, true, false);
827 list_del(&tmp_list->list);
830 free_dentry_path(page);
833 /* parses DFS refferal V3 structure
834 * caller is responsible for freeing target_nodes
837 * - on failure - errno
840 parse_dfs_referrals(struct get_dfs_referral_rsp *rsp, u32 rsp_size,
841 unsigned int *num_of_nodes,
842 struct dfs_info3_param **target_nodes,
843 const struct nls_table *nls_codepage, int remap,
844 const char *searchName, bool is_unicode)
848 struct dfs_referral_level_3 *ref;
850 *num_of_nodes = le16_to_cpu(rsp->NumberOfReferrals);
852 if (*num_of_nodes < 1) {
853 cifs_dbg(VFS, "num_referrals: must be at least > 0, but we get num_referrals = %d\n",
856 goto parse_DFS_referrals_exit;
859 ref = (struct dfs_referral_level_3 *) &(rsp->referrals);
860 if (ref->VersionNumber != cpu_to_le16(3)) {
861 cifs_dbg(VFS, "Referrals of V%d version are not supported, should be V3\n",
862 le16_to_cpu(ref->VersionNumber));
864 goto parse_DFS_referrals_exit;
867 /* get the upper boundary of the resp buffer */
868 data_end = (char *)rsp + rsp_size;
870 cifs_dbg(FYI, "num_referrals: %d dfs flags: 0x%x ...\n",
871 *num_of_nodes, le32_to_cpu(rsp->DFSFlags));
873 *target_nodes = kcalloc(*num_of_nodes, sizeof(struct dfs_info3_param),
875 if (*target_nodes == NULL) {
877 goto parse_DFS_referrals_exit;
880 /* collect necessary data from referrals */
881 for (i = 0; i < *num_of_nodes; i++) {
884 struct dfs_info3_param *node = (*target_nodes)+i;
886 node->flags = le32_to_cpu(rsp->DFSFlags);
888 __le16 *tmp = kmalloc(strlen(searchName)*2 + 2,
892 goto parse_DFS_referrals_exit;
894 cifsConvertToUTF16((__le16 *) tmp, searchName,
895 PATH_MAX, nls_codepage, remap);
896 node->path_consumed = cifs_utf16_bytes(tmp,
897 le16_to_cpu(rsp->PathConsumed),
901 node->path_consumed = le16_to_cpu(rsp->PathConsumed);
903 node->server_type = le16_to_cpu(ref->ServerType);
904 node->ref_flag = le16_to_cpu(ref->ReferralEntryFlags);
907 temp = (char *)ref + le16_to_cpu(ref->DfsPathOffset);
908 max_len = data_end - temp;
909 node->path_name = cifs_strndup_from_utf16(temp, max_len,
910 is_unicode, nls_codepage);
911 if (!node->path_name) {
913 goto parse_DFS_referrals_exit;
916 /* copy link target UNC */
917 temp = (char *)ref + le16_to_cpu(ref->NetworkAddressOffset);
918 max_len = data_end - temp;
919 node->node_name = cifs_strndup_from_utf16(temp, max_len,
920 is_unicode, nls_codepage);
921 if (!node->node_name) {
923 goto parse_DFS_referrals_exit;
926 node->ttl = le32_to_cpu(ref->TimeToLive);
931 parse_DFS_referrals_exit:
933 free_dfs_info_array(*target_nodes, *num_of_nodes);
934 *target_nodes = NULL;
940 struct cifs_aio_ctx *
941 cifs_aio_ctx_alloc(void)
943 struct cifs_aio_ctx *ctx;
946 * Must use kzalloc to initialize ctx->bv to NULL and ctx->direct_io
947 * to false so that we know when we have to unreference pages within
948 * cifs_aio_ctx_release()
950 ctx = kzalloc(sizeof(struct cifs_aio_ctx), GFP_KERNEL);
954 INIT_LIST_HEAD(&ctx->list);
955 mutex_init(&ctx->aio_mutex);
956 init_completion(&ctx->done);
957 kref_init(&ctx->refcount);
962 cifs_aio_ctx_release(struct kref *refcount)
964 struct cifs_aio_ctx *ctx = container_of(refcount,
965 struct cifs_aio_ctx, refcount);
967 cifsFileInfo_put(ctx->cfile);
970 * ctx->bv is only set if setup_aio_ctx_iter() was call successfuly
971 * which means that iov_iter_get_pages() was a success and thus that
972 * we have taken reference on pages.
977 for (i = 0; i < ctx->npages; i++) {
978 if (ctx->should_dirty)
979 set_page_dirty(ctx->bv[i].bv_page);
980 put_page(ctx->bv[i].bv_page);
988 #define CIFS_AIO_KMALLOC_LIMIT (1024 * 1024)
991 setup_aio_ctx_iter(struct cifs_aio_ctx *ctx, struct iov_iter *iter, int rw)
994 unsigned int cur_npages;
995 unsigned int npages = 0;
998 size_t count = iov_iter_count(iter);
999 unsigned int saved_len;
1001 unsigned int max_pages = iov_iter_npages(iter, INT_MAX);
1002 struct page **pages = NULL;
1003 struct bio_vec *bv = NULL;
1005 if (iov_iter_is_kvec(iter)) {
1006 memcpy(&ctx->iter, iter, sizeof(*iter));
1008 iov_iter_advance(iter, count);
1012 if (array_size(max_pages, sizeof(*bv)) <= CIFS_AIO_KMALLOC_LIMIT)
1013 bv = kmalloc_array(max_pages, sizeof(*bv), GFP_KERNEL);
1016 bv = vmalloc(array_size(max_pages, sizeof(*bv)));
1021 if (array_size(max_pages, sizeof(*pages)) <= CIFS_AIO_KMALLOC_LIMIT)
1022 pages = kmalloc_array(max_pages, sizeof(*pages), GFP_KERNEL);
1025 pages = vmalloc(array_size(max_pages, sizeof(*pages)));
1034 while (count && npages < max_pages) {
1035 rc = iov_iter_get_pages(iter, pages, count, max_pages, &start);
1037 cifs_dbg(VFS, "Couldn't get user pages (rc=%zd)\n", rc);
1042 cifs_dbg(VFS, "get pages rc=%zd more than %zu\n", rc,
1047 iov_iter_advance(iter, rc);
1050 cur_npages = DIV_ROUND_UP(rc, PAGE_SIZE);
1052 if (npages + cur_npages > max_pages) {
1053 cifs_dbg(VFS, "out of vec array capacity (%u vs %u)\n",
1054 npages + cur_npages, max_pages);
1058 for (i = 0; i < cur_npages; i++) {
1059 len = rc > PAGE_SIZE ? PAGE_SIZE : rc;
1060 bv[npages + i].bv_page = pages[i];
1061 bv[npages + i].bv_offset = start;
1062 bv[npages + i].bv_len = len - start;
1067 npages += cur_npages;
1072 ctx->len = saved_len - count;
1073 ctx->npages = npages;
1074 iov_iter_bvec(&ctx->iter, rw, ctx->bv, npages, ctx->len);
1079 * cifs_alloc_hash - allocate hash and hash context together
1080 * @name: The name of the crypto hash algo
1081 * @shash: Where to put the pointer to the hash algo
1082 * @sdesc: Where to put the pointer to the hash descriptor
1084 * The caller has to make sure @sdesc is initialized to either NULL or
1085 * a valid context. Both can be freed via cifs_free_hash().
1088 cifs_alloc_hash(const char *name,
1089 struct crypto_shash **shash, struct sdesc **sdesc)
1097 *shash = crypto_alloc_shash(name, 0, 0);
1098 if (IS_ERR(*shash)) {
1099 cifs_dbg(VFS, "Could not allocate crypto %s\n", name);
1100 rc = PTR_ERR(*shash);
1106 size = sizeof(struct shash_desc) + crypto_shash_descsize(*shash);
1107 *sdesc = kmalloc(size, GFP_KERNEL);
1108 if (*sdesc == NULL) {
1109 cifs_dbg(VFS, "no memory left to allocate crypto %s\n", name);
1110 crypto_free_shash(*shash);
1115 (*sdesc)->shash.tfm = *shash;
1120 * cifs_free_hash - free hash and hash context together
1121 * @shash: Where to find the pointer to the hash algo
1122 * @sdesc: Where to find the pointer to the hash descriptor
1124 * Freeing a NULL hash or context is safe.
1127 cifs_free_hash(struct crypto_shash **shash, struct sdesc **sdesc)
1132 crypto_free_shash(*shash);
1137 * rqst_page_get_length - obtain the length and offset for a page in smb_rqst
1138 * @rqst: The request descriptor
1139 * @page: The index of the page to query
1140 * @len: Where to store the length for this page:
1141 * @offset: Where to store the offset for this page
1143 void rqst_page_get_length(const struct smb_rqst *rqst, unsigned int page,
1144 unsigned int *len, unsigned int *offset)
1146 *len = rqst->rq_pagesz;
1147 *offset = (page == 0) ? rqst->rq_offset : 0;
1149 if (rqst->rq_npages == 1 || page == rqst->rq_npages-1)
1150 *len = rqst->rq_tailsz;
1152 *len = rqst->rq_pagesz - rqst->rq_offset;
1155 void extract_unc_hostname(const char *unc, const char **h, size_t *len)
1159 /* skip initial slashes */
1160 while (*unc && (*unc == '\\' || *unc == '/'))
1165 while (*end && !(*end == '\\' || *end == '/'))
1173 * copy_path_name - copy src path to dst, possibly truncating
1174 * @dst: The destination buffer
1175 * @src: The source name
1177 * returns number of bytes written (including trailing nul)
1179 int copy_path_name(char *dst, const char *src)
1184 * PATH_MAX includes nul, so if strlen(src) >= PATH_MAX it
1185 * will truncate and strlen(dst) will be PATH_MAX-1
1187 name_len = strscpy(dst, src, PATH_MAX);
1188 if (WARN_ON_ONCE(name_len < 0))
1189 name_len = PATH_MAX-1;
1191 /* we count the trailing nul */
1196 struct super_cb_data {
1198 struct super_block *sb;
1201 static void tcp_super_cb(struct super_block *sb, void *arg)
1203 struct super_cb_data *sd = arg;
1204 struct TCP_Server_Info *server = sd->data;
1205 struct cifs_sb_info *cifs_sb;
1206 struct cifs_tcon *tcon;
1211 cifs_sb = CIFS_SB(sb);
1212 tcon = cifs_sb_master_tcon(cifs_sb);
1213 if (tcon->ses->server == server)
1217 static struct super_block *__cifs_get_super(void (*f)(struct super_block *, void *),
1220 struct super_cb_data sd = {
1224 struct file_system_type **fs_type = (struct file_system_type *[]) {
1225 &cifs_fs_type, &smb3_fs_type, NULL,
1228 for (; *fs_type; fs_type++) {
1229 iterate_supers_type(*fs_type, f, &sd);
1232 * Grab an active reference in order to prevent automounts (DFS links)
1233 * of expiring and then freeing up our cifs superblock pointer while
1234 * we're doing failover.
1236 cifs_sb_active(sd.sb);
1240 return ERR_PTR(-EINVAL);
1243 static void __cifs_put_super(struct super_block *sb)
1245 if (!IS_ERR_OR_NULL(sb))
1246 cifs_sb_deactive(sb);
1249 struct super_block *cifs_get_tcp_super(struct TCP_Server_Info *server)
1251 return __cifs_get_super(tcp_super_cb, server);
1254 void cifs_put_tcp_super(struct super_block *sb)
1256 __cifs_put_super(sb);
1259 #ifdef CONFIG_CIFS_DFS_UPCALL
1260 int match_target_ip(struct TCP_Server_Info *server,
1261 const char *share, size_t share_len,
1265 char *target, *tip = NULL;
1266 struct sockaddr tipaddr;
1270 target = kzalloc(share_len + 3, GFP_KERNEL);
1276 scnprintf(target, share_len + 3, "\\\\%.*s", (int)share_len, share);
1278 cifs_dbg(FYI, "%s: target name: %s\n", __func__, target + 2);
1280 rc = dns_resolve_server_name_to_ip(target, &tip, NULL);
1284 cifs_dbg(FYI, "%s: target ip: %s\n", __func__, tip);
1286 if (!cifs_convert_address(&tipaddr, tip, strlen(tip))) {
1287 cifs_dbg(VFS, "%s: failed to convert target ip address\n",
1293 *result = cifs_match_ipaddr((struct sockaddr *)&server->dstaddr,
1295 cifs_dbg(FYI, "%s: ip addresses match: %u\n", __func__, *result);
1305 int cifs_update_super_prepath(struct cifs_sb_info *cifs_sb, char *prefix)
1307 kfree(cifs_sb->prepath);
1309 if (prefix && *prefix) {
1310 cifs_sb->prepath = cifs_sanitize_prepath(prefix, GFP_ATOMIC);
1311 if (!cifs_sb->prepath)
1314 convert_delimiter(cifs_sb->prepath, CIFS_DIR_SEP(cifs_sb));
1316 cifs_sb->prepath = NULL;
1318 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;