xref: /linux/fs/smb/client/cifsproto.h (revision f778a530f28546956e5f58db486fb74376f5d869)
1 /* SPDX-License-Identifier: LGPL-2.1 */
2 /*
3  *
4  *   Copyright (c) International Business Machines  Corp., 2002,2008
5  *   Author(s): Steve French (sfrench@us.ibm.com)
6  *
7  */
8 #ifndef _CIFSPROTO_H
9 #define _CIFSPROTO_H
10 #include <linux/nls.h>
11 #include <linux/ctype.h>
12 #include "cifsglob.h"
13 #include "trace.h"
14 #ifdef CONFIG_CIFS_DFS_UPCALL
15 #include "dfs_cache.h"
16 #endif
17 #include "smb1proto.h"
18 
19 struct statfs;
20 struct smb_rqst;
21 struct smb3_fs_context;
22 struct fs_context;
23 
24 /*
25  *****************************************************************
26  * All Prototypes
27  *****************************************************************
28  */
29 
30 void *cifs_buf_get(void);
31 void cifs_buf_release(void *buf_to_free);
32 void *cifs_small_buf_get(void);
33 void cifs_small_buf_release(void *buf_to_free);
34 void free_rsp_buf(int resp_buftype, void *rsp);
35 int smb_send_kvec(struct TCP_Server_Info *server, struct msghdr *smb_msg,
36 		  size_t *sent);
37 unsigned int _get_xid(void);
38 void _free_xid(unsigned int xid);
39 #define get_xid()							\
40 ({									\
41 	unsigned int __xid = _get_xid();				\
42 	cifs_dbg(FYI, "VFS: in %s as Xid: %u with uid: %d\n",		\
43 		 __func__, __xid,					\
44 		 from_kuid(&init_user_ns, current_fsuid()));		\
45 	trace_smb3_enter(__xid, __func__);				\
46 	__xid;								\
47 })
48 
49 #define free_xid(curr_xid)						\
50 do {									\
51 	_free_xid(curr_xid);						\
52 	cifs_dbg(FYI, "VFS: leaving %s (xid = %u) rc = %d\n",		\
53 		 __func__, curr_xid, (int)rc);				\
54 	if (rc)								\
55 		trace_smb3_exit_err(curr_xid, __func__, (int)rc);	\
56 	else								\
57 		trace_smb3_exit_done(curr_xid, __func__);		\
58 } while (0)
59 int init_cifs_idmap(void);
60 void exit_cifs_idmap(void);
61 int init_cifs_spnego(void);
62 void exit_cifs_spnego(void);
63 const char *build_path_from_dentry(struct dentry *direntry, void *page);
64 char *__build_path_from_dentry_optional_prefix(struct dentry *direntry,
65 					       void *page, const char *tree,
66 					       int tree_len, bool prefix);
67 char *build_path_from_dentry_optional_prefix(struct dentry *direntry,
68 					     void *page, bool prefix);
69 static inline void *alloc_dentry_path(void)
70 {
71 	return __getname();
72 }
73 
74 static inline void free_dentry_path(void *page)
75 {
76 	if (page)
77 		__putname(page);
78 }
79 
80 char *cifs_build_path_to_root(struct smb3_fs_context *ctx,
81 			      struct cifs_sb_info *cifs_sb,
82 			      struct cifs_tcon *tcon, int add_treename);
83 char *cifs_build_devname(char *nodename, const char *prepath);
84 void delete_mid(struct TCP_Server_Info *server, struct mid_q_entry *mid);
85 void __release_mid(struct TCP_Server_Info *server,
86 		   struct mid_q_entry *midEntry);
87 void cifs_wake_up_task(struct TCP_Server_Info *server,
88 		       struct mid_q_entry *mid);
89 int cifs_handle_standard(struct TCP_Server_Info *server,
90 			 struct mid_q_entry *mid);
91 char *smb3_fs_context_fullpath(const struct smb3_fs_context *ctx, char dirsep);
92 int smb3_parse_devname(const char *devname, struct smb3_fs_context *ctx);
93 int cifs_ipaddr_cmp(struct sockaddr *srcaddr, struct sockaddr *rhs);
94 bool cifs_match_ipaddr(struct sockaddr *srcaddr, struct sockaddr *rhs);
95 int cifs_discard_remaining_data(struct TCP_Server_Info *server);
96 int cifs_call_async(struct TCP_Server_Info *server, struct smb_rqst *rqst,
97 		    mid_receive_t receive, mid_callback_t callback,
98 		    mid_handle_t handle, void *cbdata, const int flags,
99 		    const struct cifs_credits *exist_credits);
100 struct TCP_Server_Info *cifs_pick_channel(struct cifs_ses *ses);
101 int cifs_send_recv(const unsigned int xid, struct cifs_ses *ses,
102 		   struct TCP_Server_Info *server, struct smb_rqst *rqst,
103 		   int *resp_buf_type, const int flags, struct kvec *resp_iov);
104 int compound_send_recv(const unsigned int xid, struct cifs_ses *ses,
105 		       struct TCP_Server_Info *server, const int flags,
106 		       const int num_rqst, struct smb_rqst *rqst,
107 		       int *resp_buf_type, struct kvec *resp_iov);
108 int cifs_sync_mid_result(struct mid_q_entry *mid,
109 			 struct TCP_Server_Info *server);
110 int __smb_send_rqst(struct TCP_Server_Info *server, int num_rqst,
111 		    struct smb_rqst *rqst);
112 int wait_for_free_request(struct TCP_Server_Info *server, const int flags,
113 			  unsigned int *instance);
114 int cifs_wait_mtu_credits(struct TCP_Server_Info *server, size_t size,
115 			  size_t *num, struct cifs_credits *credits,
116 			  bool nowait);
117 
118 static inline int
119 send_cancel(struct cifs_ses *ses, struct TCP_Server_Info *server,
120 	    struct smb_rqst *rqst, struct mid_q_entry *mid,
121 	    unsigned int xid)
122 {
123 	return server->ops->send_cancel ?
124 		server->ops->send_cancel(ses, server, rqst, mid, xid) : 0;
125 }
126 
127 int wait_for_response(struct TCP_Server_Info *server, struct mid_q_entry *mid);
128 
129 void smb2_query_server_interfaces(struct work_struct *work);
130 void cifs_signal_cifsd_for_reconnect(struct TCP_Server_Info *server,
131 				     bool all_channels);
132 void cifs_mark_tcp_ses_conns_for_reconnect(struct TCP_Server_Info *server,
133 					   bool mark_smb_session);
134 int cifs_reconnect(struct TCP_Server_Info *server, bool mark_smb_session);
135 void smb2_add_credits_from_hdr(char *buffer, struct TCP_Server_Info *server);
136 bool backup_cred(struct cifs_sb_info *cifs_sb);
137 bool is_size_safe_to_change(struct cifsInodeInfo *cifsInode, __u64 end_of_file,
138 			    bool from_readdir);
139 void cifs_write_subrequest_terminated(struct cifs_io_subrequest *wdata,
140 				      ssize_t result);
141 bool cifs_io_stopped(struct cifs_io_subrequest *subreq);
142 bool cifs_io_should_replay(struct cifs_io_subrequest *subreq);
143 struct cifsFileInfo *find_writable_file(struct cifsInodeInfo *cifs_inode,
144 					int flags);
145 int __cifs_get_writable_file(struct cifsInodeInfo *cifs_inode,
146 			     unsigned int find_flags, unsigned int open_flags,
147 			     struct cifsFileInfo **ret_file);
148 int cifs_get_writable_path(struct cifs_tcon *tcon, const char *name,
149 			   struct inode *inode, int flags,
150 			   struct cifsFileInfo **ret_file);
151 struct cifsFileInfo *__find_readable_file(struct cifsInodeInfo *cifs_inode,
152 					  unsigned int find_flags,
153 					  unsigned int open_flags);
154 int cifs_get_readable_path(struct cifs_tcon *tcon, const char *name,
155 			   struct cifsFileInfo **ret_file);
156 int cifs_get_hardlink_path(struct cifs_tcon *tcon, struct inode *inode,
157 			   struct file *file);
158 int decode_negTokenInit(unsigned char *security_blob, int length,
159 			struct TCP_Server_Info *server);
160 int cifs_convert_address(struct sockaddr *dst, const char *src, int len);
161 void cifs_set_port(struct sockaddr *addr, const unsigned short int port);
162 struct timespec64 cifs_NTtimeToUnix(__le64 ntutc);
163 u64 cifs_UnixTimeToNT(struct timespec64 t);
164 struct timespec64 cnvrtDosUnixTm(__le16 le_date, __le16 le_time, int offset);
165 void cifs_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock);
166 int cifs_get_writer(struct cifsInodeInfo *cinode);
167 void cifs_put_writer(struct cifsInodeInfo *cinode);
168 void cifs_done_oplock_break(struct cifsInodeInfo *cinode);
169 int cifs_unlock_range(struct cifsFileInfo *cfile, struct file_lock *flock,
170 		      unsigned int xid);
171 int cifs_push_mandatory_locks(struct cifsFileInfo *cfile);
172 
173 void cifs_down_write(struct rw_semaphore *sem);
174 struct cifsFileInfo *cifs_new_fileinfo(struct cifs_fid *fid, struct file *file,
175 				       struct tcon_link *tlink, __u32 oplock,
176 				       const char *symlink_target);
177 int cifs_posix_open(const char *full_path, struct inode **pinode,
178 		    struct super_block *sb, int mode, unsigned int f_flags,
179 		    __u32 *poplock, __u16 *pnetfid, unsigned int xid);
180 void cifs_fill_uniqueid(struct super_block *sb, struct cifs_fattr *fattr);
181 void cifs_unix_basic_to_fattr(struct cifs_fattr *fattr,
182 			      FILE_UNIX_BASIC_INFO *info,
183 			      struct cifs_sb_info *cifs_sb);
184 void cifs_dir_info_to_fattr(struct cifs_fattr *fattr,
185 			    FILE_DIRECTORY_INFO *info,
186 			    struct cifs_sb_info *cifs_sb);
187 int cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr,
188 			bool from_readdir);
189 struct inode *cifs_iget(struct super_block *sb, struct cifs_fattr *fattr);
190 
191 int cifs_get_inode_info(struct inode **inode, const char *full_path,
192 			struct cifs_open_info_data *data,
193 			struct super_block *sb, int xid,
194 			const struct cifs_fid *fid);
195 int smb311_posix_get_inode_info(struct inode **inode, const char *full_path,
196 				struct cifs_open_info_data *data,
197 				struct super_block *sb,
198 				const unsigned int xid);
199 int cifs_get_inode_info_unix(struct inode **pinode,
200 			     const unsigned char *full_path,
201 			     struct super_block *sb, unsigned int xid);
202 int cifs_set_file_info(struct inode *inode, struct iattr *attrs,
203 		       unsigned int xid, const char *full_path, __u32 dosattr);
204 int cifs_rename_pending_delete(const char *full_path, struct dentry *dentry,
205 			       const unsigned int xid);
206 int sid_to_id(struct cifs_sb_info *cifs_sb, struct smb_sid *psid,
207 	      struct cifs_fattr *fattr, uint sidtype);
208 int cifs_acl_to_fattr(struct cifs_sb_info *cifs_sb, struct cifs_fattr *fattr,
209 		      struct inode *inode, bool mode_from_special_sid,
210 		      const char *path, const struct cifs_fid *pfid);
211 int id_mode_to_cifs_acl(struct inode *inode, const char *path, __u64 *pnmode,
212 			kuid_t uid, kgid_t gid);
213 struct smb_ntsd *get_cifs_acl(struct cifs_sb_info *cifs_sb,
214 			      struct inode *inode, const char *path,
215 			      u32 *pacllen, u32 info);
216 struct smb_ntsd *get_cifs_acl_by_fid(struct cifs_sb_info *cifs_sb,
217 				     const struct cifs_fid *cifsfid,
218 				     u32 *pacllen, u32 info);
219 struct posix_acl *cifs_get_acl(const struct mnt_idmap *idmap, struct dentry *dentry,
220 			       int type);
221 int cifs_set_acl(const struct mnt_idmap *idmap, struct dentry *dentry,
222 		 struct posix_acl *acl, int type);
223 int set_cifs_acl(struct smb_ntsd *pnntsd, __u32 acllen, struct inode *inode,
224 		 const char *path, int aclflag);
225 unsigned int setup_authusers_ACE(struct smb_ace *pntace);
226 unsigned int setup_special_mode_ACE(struct smb_ace *pntace, bool posix,
227 				    __u64 nmode);
228 unsigned int setup_special_user_owner_ACE(struct smb_ace *pntace);
229 
230 void dequeue_mid(struct TCP_Server_Info *server, struct mid_q_entry *mid,
231 		 bool malformed);
232 int cifs_read_from_socket(struct TCP_Server_Info *server, char *buf,
233 			  unsigned int to_read);
234 ssize_t cifs_discard_from_socket(struct TCP_Server_Info *server,
235 				 size_t to_read);
236 int cifs_read_iter_from_socket(struct TCP_Server_Info *server,
237 			       struct iov_iter *iter, unsigned int to_read);
238 int cifs_setup_cifs_sb(struct cifs_sb_info *cifs_sb);
239 void cifs_mount_put_conns(struct cifs_mount_ctx *mnt_ctx);
240 int cifs_mount_get_session(struct cifs_mount_ctx *mnt_ctx);
241 int cifs_is_path_remote(struct cifs_mount_ctx *mnt_ctx);
242 int cifs_mount_get_tcon(struct cifs_mount_ctx *mnt_ctx);
243 int cifs_match_super(struct super_block *sb, struct fs_context *fc);
244 int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx);
245 void cifs_umount(struct cifs_sb_info *cifs_sb);
246 void cifs_mark_open_files_invalid(struct cifs_tcon *tcon);
247 bool cifs_reopen_persistent_handles(struct cifs_tcon *tcon);
248 
249 bool cifs_find_lock_conflict(struct cifsFileInfo *cfile, __u64 offset,
250 			     __u64 length, __u8 type, __u16 flags,
251 			     struct cifsLockInfo **conf_lock, int rw_check);
252 void cifs_add_pending_open(struct cifs_fid *fid, struct tcon_link *tlink,
253 			   struct cifs_pending_open *open);
254 void cifs_add_pending_open_locked(struct cifs_fid *fid,
255 				  struct tcon_link *tlink,
256 				  struct cifs_pending_open *open);
257 void cifs_del_pending_open(struct cifs_pending_open *open);
258 
259 bool cifs_is_deferred_close(struct cifsFileInfo *cfile,
260 			    struct cifs_deferred_close **pdclose);
261 
262 void cifs_add_deferred_close(struct cifsFileInfo *cfile,
263 			     struct cifs_deferred_close *dclose);
264 
265 void cifs_del_deferred_close(struct cifsFileInfo *cfile);
266 
267 void cifs_close_deferred_file(struct cifsInodeInfo *cifs_inode);
268 
269 void cifs_close_all_deferred_files(struct cifs_tcon *tcon);
270 
271 void cifs_close_all_deferred_files_sb(struct cifs_sb_info *cifs_sb);
272 void cifs_close_deferred_file_under_dentry(struct cifs_tcon *tcon,
273 					   struct dentry *dentry);
274 
275 void cifs_mark_open_handles_for_deleted_file(struct inode *inode,
276 					     const char *path);
277 
278 struct TCP_Server_Info *cifs_get_tcp_session(struct smb3_fs_context *ctx,
279 					     struct TCP_Server_Info *primary_server);
280 void cifs_put_tcp_session(struct TCP_Server_Info *server, int from_reconnect);
281 void cifs_put_tcon(struct cifs_tcon *tcon, enum smb3_tcon_ref_trace trace);
282 
283 void cifs_release_automount_timer(void);
284 
285 void cifs_proc_init(void);
286 void cifs_proc_clean(void);
287 
288 void cifs_move_llist(struct list_head *source, struct list_head *dest);
289 void cifs_free_llist(struct list_head *llist);
290 void cifs_del_lock_waiters(struct cifsLockInfo *lock);
291 
292 int cifs_tree_connect(const unsigned int xid, struct cifs_tcon *tcon);
293 
294 int cifs_negotiate_protocol(const unsigned int xid, struct cifs_ses *ses,
295 			    struct TCP_Server_Info *server);
296 int cifs_setup_session(const unsigned int xid, struct cifs_ses *ses,
297 		       struct TCP_Server_Info *server);
298 int cifs_enable_signing(struct TCP_Server_Info *server,
299 			bool mnt_sign_required);
300 
301 int parse_dfs_referrals(struct get_dfs_referral_rsp *rsp, u32 rsp_size,
302 			unsigned int *num_of_nodes,
303 			struct dfs_info3_param **target_nodes,
304 			const struct nls_table *nls_codepage, int remap,
305 			const char *searchName, bool is_unicode);
306 
307 struct cifs_ses *sesInfoAlloc(void);
308 void sesInfoFree(struct cifs_ses *buf_to_free);
309 struct cifs_tcon *tcon_info_alloc(bool dir_leases_enabled,
310 				  enum smb3_tcon_ref_trace trace);
311 void tconInfoFree(struct cifs_tcon *tcon, enum smb3_tcon_ref_trace trace);
312 
313 int setup_ntlmv2_rsp(struct cifs_ses *ses);
314 void cifs_crypto_secmech_release(struct TCP_Server_Info *server);
315 int calc_seckey(struct cifs_ses *ses);
316 int generate_smb30signingkey(struct cifs_ses *ses,
317 			     struct TCP_Server_Info *server);
318 int generate_smb311signingkey(struct cifs_ses *ses,
319 			      struct TCP_Server_Info *server);
320 
321 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
322 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
323 void cifs_autodisable_serverino(struct cifs_sb_info *cifs_sb, const char *reason, int rc);
324 bool couldbe_mf_symlink(const struct cifs_fattr *fattr);
325 int cifs_check_mf_symlink(unsigned int xid, struct cifs_tcon *tcon,
326 			  struct cifs_sb_info *cifs_sb,
327 			  const char *full_path, struct inode *inode,
328 			  struct cifs_fattr *fattr);
329 int E_md4hash(const unsigned char *passwd, unsigned char *p16,
330 	      const struct nls_table *codepage);
331 
332 struct TCP_Server_Info *cifs_find_tcp_session(struct smb3_fs_context *ctx);
333 
334 struct cifs_tcon *cifs_setup_ipc(struct cifs_ses *ses, bool seal);
335 
336 void __cifs_put_smb_ses(struct cifs_ses *ses);
337 
338 struct cifs_ses *cifs_get_smb_ses(struct TCP_Server_Info *server,
339 				  struct smb3_fs_context *ctx);
340 
341 int cifs_readv_receive(struct TCP_Server_Info *server,
342 		       struct mid_q_entry *mid);
343 
344 int cifs_query_mf_symlink(const unsigned int xid,
345 			  struct cifs_tcon *tcon,
346 			  struct cifs_sb_info *cifs_sb,
347 			  const char *full_path,
348 			  struct inode *inode, char *pbuf,
349 			  unsigned int *pbytes_read);
350 int cifs_create_mf_symlink(unsigned int xid, struct cifs_tcon *tcon,
351 			   struct cifs_sb_info *cifs_sb,
352 			   const unsigned char *path, char *pbuf,
353 			   unsigned int *pbytes_written);
354 int __cifs_calc_signature(struct smb_rqst *rqst,
355 			  struct TCP_Server_Info *server, char *signature,
356 			  struct cifs_calc_sig_ctx *ctx);
357 enum securityEnum cifs_select_sectype(struct TCP_Server_Info *server,
358 				      enum securityEnum requested);
359 
360 int cifs_try_adding_channels(struct cifs_ses *ses);
361 int smb3_update_ses_channels(struct cifs_ses *ses,
362 			     struct TCP_Server_Info *server,
363 			     bool from_reconnect, bool disable_mchan);
364 bool is_ses_using_iface(struct cifs_ses *ses, struct cifs_server_iface *iface);
365 
366 int cifs_ses_get_chan_index(struct cifs_ses *ses,
367 			    struct TCP_Server_Info *server);
368 void cifs_chan_set_in_reconnect(struct cifs_ses *ses,
369 				struct TCP_Server_Info *server);
370 void cifs_chan_clear_in_reconnect(struct cifs_ses *ses,
371 				  struct TCP_Server_Info *server);
372 void cifs_chan_set_need_reconnect(struct cifs_ses *ses,
373 				  struct TCP_Server_Info *server);
374 void cifs_chan_clear_need_reconnect(struct cifs_ses *ses,
375 				    struct TCP_Server_Info *server);
376 bool cifs_chan_needs_reconnect(struct cifs_ses *ses,
377 			       struct TCP_Server_Info *server);
378 bool cifs_chan_is_iface_active(struct cifs_ses *ses,
379 			       struct TCP_Server_Info *server);
380 void cifs_decrease_secondary_channels(struct cifs_ses *ses,
381 				      bool disable_mchan);
382 void cifs_chan_update_iface(struct cifs_ses *ses,
383 			    struct TCP_Server_Info *server);
384 int SMB3_request_interfaces(const unsigned int xid, struct cifs_tcon *tcon,
385 			    bool in_mount);
386 
387 void extract_unc_hostname(const char *unc, const char **h, size_t *len);
388 int copy_path_name(char *dst, const char *src);
389 int smb2_parse_query_directory(struct cifs_tcon *tcon, struct kvec *rsp_iov,
390 			       int resp_buftype,
391 			       struct cifs_search_info *srch_inf);
392 
393 struct super_block *cifs_get_dfs_tcon_super(struct cifs_tcon *tcon);
394 void cifs_put_tcp_super(struct super_block *sb);
395 int cifs_update_super_prepath(struct cifs_sb_info *cifs_sb, char *prefix);
396 char *extract_hostname(const char *unc);
397 char *extract_sharename(const char *unc);
398 int parse_reparse_point(struct reparse_data_buffer *buf, u32 plen,
399 			struct cifs_sb_info *cifs_sb, const char *full_path,
400 			struct cifs_open_info_data *data);
401 int __cifs_sfu_make_node(unsigned int xid, struct inode *inode,
402 			 struct dentry *dentry, struct cifs_tcon *tcon,
403 			 const char *full_path, umode_t mode, dev_t dev,
404 			 const char *symname);
405 int cifs_sfu_make_node(unsigned int xid, struct inode *inode,
406 		       struct dentry *dentry, struct cifs_tcon *tcon,
407 		       const char *full_path, umode_t mode, dev_t dev);
408 umode_t wire_mode_to_posix(u32 wire, bool is_dir);
409 
410 #ifdef CONFIG_CIFS_DFS_UPCALL
411 static inline int get_dfs_path(const unsigned int xid, struct cifs_ses *ses,
412 			       const char *old_path,
413 			       const struct nls_table *nls_codepage,
414 			       struct dfs_info3_param *referral, int remap)
415 {
416 	return dfs_cache_find(xid, ses, nls_codepage, remap, old_path,
417 			      referral, NULL);
418 }
419 
420 int match_target_ip(struct TCP_Server_Info *server, const char *host,
421 		    size_t hostlen, bool *result);
422 int cifs_inval_name_dfs_link_error(const unsigned int xid,
423 				   struct cifs_tcon *tcon,
424 				   struct cifs_sb_info *cifs_sb,
425 				   const char *full_path, bool *islink);
426 #else
427 static inline int cifs_inval_name_dfs_link_error(const unsigned int xid,
428 				   struct cifs_tcon *tcon,
429 				   struct cifs_sb_info *cifs_sb,
430 				   const char *full_path,
431 				   bool *islink)
432 {
433 	*islink = false;
434 	return 0;
435 }
436 #endif
437 
438 static inline int cifs_create_options(struct cifs_sb_info *cifs_sb, int options)
439 {
440 	if (cifs_sb && (backup_cred(cifs_sb)))
441 		return options | CREATE_OPEN_BACKUP_INTENT;
442 	else
443 		return options;
444 }
445 
446 void cifs_wake_ses_waiters(struct cifs_ses *ses);
447 int cifs_wait_for_server_reconnect(struct TCP_Server_Info *server,
448 				   struct cifs_tcon *tcon, int aborts,
449 				   bool nowait);
450 
451 static inline void cifs_put_smb_ses(struct cifs_ses *ses)
452 {
453 	__cifs_put_smb_ses(ses);
454 }
455 
456 /* Get an active reference of @ses and its children.
457  *
458  * NOTE: make sure to call this function when incrementing reference count of
459  * @ses to ensure that any DFS root session attached to it (@ses->dfs_root_ses)
460  * will also get its reference count incremented.
461  *
462  * cifs_put_smb_ses() will put all references, so call it when you're done.
463  */
464 static inline void cifs_smb_ses_inc_refcount(struct cifs_ses *ses)
465 {
466 	lockdep_assert_held(&cifs_tcp_ses_lock);
467 	ses->ses_count++;
468 }
469 
470 static inline bool dfs_src_pathname_equal(const char *s1, const char *s2)
471 {
472 	if (strlen(s1) != strlen(s2))
473 		return false;
474 	for (; *s1; s1++, s2++) {
475 		if (*s1 == '/' || *s1 == '\\') {
476 			if (*s2 != '/' && *s2 != '\\')
477 				return false;
478 		} else if (tolower(*s1) != tolower(*s2))
479 			return false;
480 	}
481 	return true;
482 }
483 
484 static inline void smb_get_mid(struct mid_q_entry *mid)
485 {
486 	refcount_inc(&mid->refcount);
487 }
488 
489 static inline void release_mid(struct TCP_Server_Info *server, struct mid_q_entry *mid)
490 {
491 	if (refcount_dec_and_test(&mid->refcount))
492 		__release_mid(server, mid);
493 }
494 
495 static inline void cifs_free_open_info(struct cifs_open_info_data *data)
496 {
497 	kfree(data->symlink_target);
498 	free_rsp_buf(data->reparse.io.buftype, data->reparse.io.iov.iov_base);
499 	memset(data, 0, sizeof(*data));
500 }
501 
502 static inline int smb_EIO(enum smb_eio_trace trace)
503 {
504 	trace_smb3_eio(trace, 0, 0);
505 	return -EIO;
506 }
507 
508 static inline int smb_EIO1(enum smb_eio_trace trace, unsigned long info)
509 {
510 	trace_smb3_eio(trace, info, 0);
511 	return -EIO;
512 }
513 
514 static inline int smb_EIO2(enum smb_eio_trace trace, unsigned long info, unsigned long info2)
515 {
516 	trace_smb3_eio(trace, info, info2);
517 	return -EIO;
518 }
519 
520 static inline int cifs_get_num_sgs(const struct smb_rqst *rqst,
521 				   int num_rqst,
522 				   const u8 *sig)
523 {
524 	unsigned int len, skip;
525 	unsigned int nents = 0;
526 	unsigned long addr;
527 	size_t data_size;
528 	int i, j;
529 
530 	/*
531 	 * The first rqst has a transform header where the first 20 bytes are
532 	 * not part of the encrypted blob.
533 	 */
534 	skip = 20;
535 
536 	/* Assumes the first rqst has a transform header as the first iov.
537 	 * I.e.
538 	 * rqst[0].rq_iov[0]  is transform header
539 	 * rqst[0].rq_iov[1+] data to be encrypted/decrypted
540 	 * rqst[1+].rq_iov[0+] data to be encrypted/decrypted
541 	 */
542 	for (i = 0; i < num_rqst; i++) {
543 		data_size = iov_iter_count(&rqst[i].rq_iter);
544 
545 		/* We really don't want a mixture of pinned and unpinned pages
546 		 * in the sglist.  It's hard to keep track of which is what.
547 		 * Instead, we convert to a BVEC-type iterator higher up.
548 		 */
549 		if (data_size &&
550 		    WARN_ON_ONCE(user_backed_iter(&rqst[i].rq_iter)))
551 			return smb_EIO(smb_eio_trace_user_iter);
552 
553 		/* We also don't want to have any extra refs or pins to clean
554 		 * up in the sglist.
555 		 */
556 		if (data_size &&
557 		    WARN_ON_ONCE(iov_iter_extract_will_pin(&rqst[i].rq_iter)))
558 			return smb_EIO(smb_eio_trace_extract_will_pin);
559 
560 		for (j = 0; j < rqst[i].rq_nvec; j++) {
561 			struct kvec *iov = &rqst[i].rq_iov[j];
562 
563 			addr = (unsigned long)iov->iov_base + skip;
564 			if (is_vmalloc_or_module_addr((void *)addr)) {
565 				len = iov->iov_len - skip;
566 				nents += DIV_ROUND_UP(offset_in_page(addr) + len,
567 						      PAGE_SIZE);
568 			} else {
569 				nents++;
570 			}
571 			skip = 0;
572 		}
573 		if (data_size)
574 			nents += iov_iter_npages(&rqst[i].rq_iter, INT_MAX);
575 	}
576 	nents += DIV_ROUND_UP(offset_in_page(sig) + SMB2_SIGNATURE_SIZE, PAGE_SIZE);
577 	return nents;
578 }
579 
580 /* We can not use the normal sg_set_buf() as we will sometimes pass a
581  * stack object as buf.
582  */
583 static inline void cifs_sg_set_buf(struct sg_table *sgtable,
584 				   const void *buf,
585 				   unsigned int buflen)
586 {
587 	unsigned long addr = (unsigned long)buf;
588 	unsigned int off = offset_in_page(addr);
589 
590 	addr &= PAGE_MASK;
591 	if (is_vmalloc_or_module_addr((void *)addr)) {
592 		do {
593 			unsigned int len = min_t(unsigned int, buflen, PAGE_SIZE - off);
594 
595 			sg_set_page(&sgtable->sgl[sgtable->nents++],
596 				    vmalloc_to_page((void *)addr), len, off);
597 
598 			off = 0;
599 			addr += PAGE_SIZE;
600 			buflen -= len;
601 		} while (buflen);
602 	} else {
603 		sg_set_page(&sgtable->sgl[sgtable->nents++],
604 			    virt_to_page((void *)addr), buflen, off);
605 	}
606 }
607 
608 static inline int cifs_get_writable_file(struct cifsInodeInfo *cifs_inode,
609 					 unsigned int find_flags,
610 					 struct cifsFileInfo **ret_file)
611 {
612 	find_flags &= ~FIND_OPEN_FLAGS;
613 	return __cifs_get_writable_file(cifs_inode, find_flags, 0, ret_file);
614 }
615 
616 static inline struct cifsFileInfo *
617 find_readable_file(struct cifsInodeInfo *cinode, unsigned int find_flags)
618 {
619 	find_flags &= ~FIND_OPEN_FLAGS;
620 	find_flags |= FIND_NO_PENDING_DELETE;
621 	return __find_readable_file(cinode, find_flags, 0);
622 }
623 
624 #endif			/* _CIFSPROTO_H */
625