1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  *   Copyright (C) 2019 Samsung Electronics Co., Ltd.
4  */
5 
6 #ifndef __VFS_CACHE_H__
7 #define __VFS_CACHE_H__
8 
9 #include <linux/file.h>
10 #include <linux/fs.h>
11 #include <linux/rwsem.h>
12 #include <linux/spinlock.h>
13 #include <linux/idr.h>
14 #include <linux/workqueue.h>
15 
16 #include "vfs.h"
17 #include "mgmt/share_config.h"
18 
19 /* Windows style file permissions for extended response */
20 #define	FILE_GENERIC_ALL	0x1F01FF
21 #define	FILE_GENERIC_READ	0x120089
22 #define	FILE_GENERIC_WRITE	0x120116
23 #define	FILE_GENERIC_EXECUTE	0X1200a0
24 
25 #define KSMBD_START_FID		0
26 #define KSMBD_NO_FID		(INT_MAX)
27 #define SMB2_NO_FID		(0xFFFFFFFFFFFFFFFFULL)
28 
29 struct ksmbd_conn;
30 struct ksmbd_session;
31 
32 struct ksmbd_lock {
33 	struct file_lock *fl;
34 	struct list_head clist;
35 	struct list_head flist;
36 	struct list_head llist;
37 	unsigned int flags;
38 	int cmd;
39 	int zero_len;
40 	unsigned long long start;
41 	unsigned long long end;
42 };
43 
44 struct stream {
45 	char *name;
46 	ssize_t size;
47 };
48 
49 struct ksmbd_inode {
50 	struct rw_semaphore		m_lock;
51 	atomic_t			m_count;
52 	atomic_t			op_count;
53 	/* opinfo count for streams */
54 	atomic_t			sop_count;
55 	struct dentry			*m_de;
56 	unsigned int			m_flags;
57 	struct hlist_node		m_hash;
58 	struct list_head		m_fp_list;
59 	struct list_head		m_op_list;
60 	struct oplock_info		*m_opinfo;
61 	__le32				m_fattr;
62 };
63 
64 enum {
65 	FP_NEW = 0,
66 	FP_INITED,
67 	FP_CLOSED
68 };
69 
70 struct ksmbd_file {
71 	struct file			*filp;
72 	u64				persistent_id;
73 	u64				volatile_id;
74 
75 	spinlock_t			f_lock;
76 
77 	struct ksmbd_inode		*f_ci;
78 	struct ksmbd_inode		*f_parent_ci;
79 	struct oplock_info __rcu	*f_opinfo;
80 	struct ksmbd_conn		*conn;
81 	struct ksmbd_tree_connect	*tcon;
82 
83 	atomic_t			refcount;
84 	__le32				daccess;
85 	__le32				saccess;
86 	__le32				coption;
87 	__le32				cdoption;
88 	__u64				create_time;
89 	__u64				itime;
90 
91 	bool				is_nt_open;
92 	bool				attrib_only;
93 
94 	char				client_guid[16];
95 	char				create_guid[16];
96 	char				app_instance_id[16];
97 
98 	struct stream			stream;
99 	struct list_head		node;
100 	struct list_head		blocked_works;
101 	struct list_head		lock_list;
102 
103 	unsigned int			durable_timeout;
104 	unsigned int			durable_scavenger_timeout;
105 
106 	/* if ls is happening on directory, below is valid*/
107 	struct ksmbd_readdir_data	readdir_data;
108 	int				dot_dotdot[2];
109 	unsigned int			f_state;
110 	bool				reserve_lease_break;
111 	bool				is_durable;
112 	bool				is_persistent;
113 	bool				is_resilient;
114 };
115 
set_ctx_actor(struct dir_context * ctx,filldir_t actor)116 static inline void set_ctx_actor(struct dir_context *ctx,
117 				 filldir_t actor)
118 {
119 	ctx->actor = actor;
120 }
121 
122 #define KSMBD_NR_OPEN_DEFAULT BITS_PER_LONG
123 
124 struct ksmbd_file_table {
125 	rwlock_t		lock;
126 	struct idr		*idr;
127 };
128 
has_file_id(u64 id)129 static inline bool has_file_id(u64 id)
130 {
131 	return id < KSMBD_NO_FID;
132 }
133 
ksmbd_stream_fd(struct ksmbd_file * fp)134 static inline bool ksmbd_stream_fd(struct ksmbd_file *fp)
135 {
136 	return fp->stream.name != NULL;
137 }
138 
139 int ksmbd_init_file_table(struct ksmbd_file_table *ft);
140 void ksmbd_destroy_file_table(struct ksmbd_file_table *ft);
141 int ksmbd_close_fd(struct ksmbd_work *work, u64 id);
142 struct ksmbd_file *ksmbd_lookup_fd_fast(struct ksmbd_work *work, u64 id);
143 struct ksmbd_file *ksmbd_lookup_foreign_fd(struct ksmbd_work *work, u64 id);
144 struct ksmbd_file *ksmbd_lookup_fd_slow(struct ksmbd_work *work, u64 id,
145 					u64 pid);
146 void ksmbd_fd_put(struct ksmbd_work *work, struct ksmbd_file *fp);
147 struct ksmbd_inode *ksmbd_inode_lookup_lock(struct dentry *d);
148 void ksmbd_inode_put(struct ksmbd_inode *ci);
149 struct ksmbd_file *ksmbd_lookup_global_fd(unsigned long long id);
150 struct ksmbd_file *ksmbd_lookup_durable_fd(unsigned long long id);
151 void ksmbd_put_durable_fd(struct ksmbd_file *fp);
152 struct ksmbd_file *ksmbd_lookup_fd_cguid(char *cguid);
153 struct ksmbd_file *ksmbd_lookup_fd_inode(struct dentry *dentry);
154 unsigned int ksmbd_open_durable_fd(struct ksmbd_file *fp);
155 struct ksmbd_file *ksmbd_open_fd(struct ksmbd_work *work, struct file *filp);
156 void ksmbd_launch_ksmbd_durable_scavenger(void);
157 void ksmbd_stop_durable_scavenger(void);
158 void ksmbd_close_tree_conn_fds(struct ksmbd_work *work);
159 void ksmbd_close_session_fds(struct ksmbd_work *work);
160 int ksmbd_close_inode_fds(struct ksmbd_work *work, struct inode *inode);
161 int ksmbd_init_global_file_table(void);
162 void ksmbd_free_global_file_table(void);
163 void ksmbd_set_fd_limit(unsigned long limit);
164 void ksmbd_update_fstate(struct ksmbd_file_table *ft, struct ksmbd_file *fp,
165 			 unsigned int state);
166 
167 /*
168  * INODE hash
169  */
170 int __init ksmbd_inode_hash_init(void);
171 void ksmbd_release_inode_hash(void);
172 
173 enum KSMBD_INODE_STATUS {
174 	KSMBD_INODE_STATUS_OK,
175 	KSMBD_INODE_STATUS_UNKNOWN,
176 	KSMBD_INODE_STATUS_PENDING_DELETE,
177 };
178 
179 int ksmbd_query_inode_status(struct dentry *dentry);
180 bool ksmbd_inode_pending_delete(struct ksmbd_file *fp);
181 void ksmbd_set_inode_pending_delete(struct ksmbd_file *fp);
182 void ksmbd_clear_inode_pending_delete(struct ksmbd_file *fp);
183 void ksmbd_fd_set_delete_on_close(struct ksmbd_file *fp,
184 				  int file_info);
185 int ksmbd_reopen_durable_fd(struct ksmbd_work *work, struct ksmbd_file *fp);
186 int ksmbd_validate_name_reconnect(struct ksmbd_share_config *share,
187 				  struct ksmbd_file *fp, char *name);
188 int ksmbd_init_file_cache(void);
189 void ksmbd_exit_file_cache(void);
190 #endif /* __VFS_CACHE_H__ */
191