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