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 loff_t pos; 48 }; 49 50 struct ksmbd_inode { 51 struct rw_semaphore m_lock; 52 atomic_t m_count; 53 atomic_t op_count; 54 /* opinfo count for streams */ 55 atomic_t sop_count; 56 struct dentry *m_de; 57 unsigned int m_flags; 58 struct hlist_node m_hash; 59 struct list_head m_fp_list; 60 struct list_head m_op_list; 61 struct oplock_info *m_opinfo; 62 __le32 m_fattr; 63 }; 64 65 enum { 66 FP_NEW = 0, 67 FP_INITED, 68 FP_CLOSED 69 }; 70 71 /* Owner information for durable handle reconnect */ 72 struct durable_owner { 73 unsigned int uid; 74 unsigned int gid; 75 char *name; 76 }; 77 78 struct ksmbd_file { 79 struct file *filp; 80 u64 persistent_id; 81 u64 volatile_id; 82 83 spinlock_t f_lock; 84 85 struct ksmbd_inode *f_ci; 86 struct ksmbd_inode *f_parent_ci; 87 struct oplock_info __rcu *f_opinfo; 88 struct ksmbd_conn *conn; 89 struct ksmbd_tree_connect *tcon; 90 91 atomic_t refcount; 92 __le32 daccess; 93 __le32 saccess; 94 __le32 coption; 95 __le32 cdoption; 96 __u64 create_time; 97 __u64 itime; 98 99 bool is_nt_open; 100 bool attrib_only; 101 102 char client_guid[16]; 103 char create_guid[16]; 104 char app_instance_id[16]; 105 106 struct stream stream; 107 struct list_head node; 108 struct list_head blocked_works; 109 struct list_head lock_list; 110 111 unsigned int durable_timeout; 112 unsigned int durable_scavenger_timeout; 113 114 /* if ls is happening on directory, below is valid*/ 115 struct ksmbd_readdir_data readdir_data; 116 int dot_dotdot[2]; 117 unsigned int f_state; 118 bool reserve_lease_break; 119 bool is_durable; 120 bool is_persistent; 121 bool is_resilient; 122 123 bool is_posix_ctxt; 124 struct durable_owner owner; 125 }; 126 127 static inline void set_ctx_actor(struct dir_context *ctx, 128 filldir_t actor) 129 { 130 ctx->actor = actor; 131 } 132 133 #define KSMBD_NR_OPEN_DEFAULT BITS_PER_LONG 134 135 struct ksmbd_file_table { 136 rwlock_t lock; 137 struct idr *idr; 138 }; 139 140 static inline bool has_file_id(u64 id) 141 { 142 return id < KSMBD_NO_FID; 143 } 144 145 static inline bool ksmbd_stream_fd(struct ksmbd_file *fp) 146 { 147 return fp->stream.name != NULL; 148 } 149 150 int ksmbd_init_file_table(struct ksmbd_file_table *ft); 151 void ksmbd_destroy_file_table(struct ksmbd_session *sess); 152 int ksmbd_close_fd(struct ksmbd_work *work, u64 id); 153 struct ksmbd_file *ksmbd_lookup_fd_fast(struct ksmbd_work *work, u64 id); 154 struct ksmbd_file *ksmbd_lookup_foreign_fd(struct ksmbd_work *work, u64 id); 155 struct ksmbd_file *ksmbd_lookup_fd_slow(struct ksmbd_work *work, u64 id, 156 u64 pid); 157 void ksmbd_fd_put(struct ksmbd_work *work, struct ksmbd_file *fp); 158 struct ksmbd_inode *ksmbd_inode_lookup_lock(struct dentry *d); 159 void ksmbd_inode_put(struct ksmbd_inode *ci); 160 struct ksmbd_file *ksmbd_lookup_global_fd(unsigned long long id); 161 struct ksmbd_file *ksmbd_lookup_durable_fd(unsigned long long id); 162 void ksmbd_put_durable_fd(struct ksmbd_file *fp); 163 struct ksmbd_file *ksmbd_lookup_fd_cguid(char *cguid); 164 struct ksmbd_file *ksmbd_lookup_fd_inode(struct dentry *dentry); 165 unsigned int ksmbd_open_durable_fd(struct ksmbd_file *fp); 166 struct ksmbd_file *ksmbd_open_fd(struct ksmbd_work *work, struct file *filp); 167 void ksmbd_launch_ksmbd_durable_scavenger(void); 168 void ksmbd_stop_durable_scavenger(void); 169 void ksmbd_close_tree_conn_fds(struct ksmbd_work *work); 170 void ksmbd_close_session_fds(struct ksmbd_work *work); 171 int ksmbd_close_inode_fds(struct ksmbd_work *work, struct inode *inode); 172 int ksmbd_init_global_file_table(void); 173 void ksmbd_free_global_file_table(void); 174 void ksmbd_set_fd_limit(unsigned long limit); 175 void ksmbd_update_fstate(struct ksmbd_file_table *ft, struct ksmbd_file *fp, 176 unsigned int state); 177 bool ksmbd_vfs_compare_durable_owner(struct ksmbd_file *fp, 178 struct ksmbd_user *user); 179 180 /* 181 * INODE hash 182 */ 183 int __init ksmbd_inode_hash_init(void); 184 void ksmbd_release_inode_hash(void); 185 186 enum KSMBD_INODE_STATUS { 187 KSMBD_INODE_STATUS_OK, 188 KSMBD_INODE_STATUS_UNKNOWN, 189 KSMBD_INODE_STATUS_PENDING_DELETE, 190 }; 191 192 int ksmbd_query_inode_status(struct dentry *dentry); 193 bool ksmbd_inode_pending_delete(struct ksmbd_file *fp); 194 void ksmbd_set_inode_pending_delete(struct ksmbd_file *fp); 195 void ksmbd_clear_inode_pending_delete(struct ksmbd_file *fp); 196 void ksmbd_fd_set_delete_on_close(struct ksmbd_file *fp, 197 int file_info); 198 int ksmbd_reopen_durable_fd(struct ksmbd_work *work, struct ksmbd_file *fp); 199 int ksmbd_validate_name_reconnect(struct ksmbd_share_config *share, 200 struct ksmbd_file *fp, char *name); 201 int ksmbd_init_file_cache(void); 202 void ksmbd_exit_file_cache(void); 203 #endif /* __VFS_CACHE_H__ */ 204