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