Namjae Jeon | f441584 | 2021-03-16 10:50:04 +0900 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * Copyright (C) 2016 Namjae Jeon <linkinjeon@kernel.org> |
| 4 | * Copyright (C) 2019 Samsung Electronics Co., Ltd. |
| 5 | */ |
| 6 | |
| 7 | #include <linux/fs.h> |
| 8 | #include <linux/slab.h> |
| 9 | #include <linux/vmalloc.h> |
| 10 | |
Namjae Jeon | f441584 | 2021-03-16 10:50:04 +0900 | [diff] [blame] | 11 | #include "glob.h" |
| 12 | #include "vfs_cache.h" |
| 13 | #include "buffer_pool.h" |
Namjae Jeon | f441584 | 2021-03-16 10:50:04 +0900 | [diff] [blame] | 14 | #include "oplock.h" |
| 15 | #include "vfs.h" |
| 16 | #include "connection.h" |
| 17 | #include "mgmt/tree_connect.h" |
| 18 | #include "mgmt/user_session.h" |
Namjae Jeon | f441584 | 2021-03-16 10:50:04 +0900 | [diff] [blame] | 19 | #include "smb_common.h" |
| 20 | |
| 21 | #define S_DEL_PENDING 1 |
| 22 | #define S_DEL_ON_CLS 2 |
| 23 | #define S_DEL_ON_CLS_STREAM 8 |
| 24 | |
| 25 | static unsigned int inode_hash_mask __read_mostly; |
| 26 | static unsigned int inode_hash_shift __read_mostly; |
| 27 | static struct hlist_head *inode_hashtable __read_mostly; |
| 28 | static DEFINE_RWLOCK(inode_hash_lock); |
| 29 | |
| 30 | static struct ksmbd_file_table global_ft; |
| 31 | static atomic_long_t fd_limit; |
| 32 | |
| 33 | void ksmbd_set_fd_limit(unsigned long limit) |
| 34 | { |
| 35 | limit = min(limit, get_max_files()); |
| 36 | atomic_long_set(&fd_limit, limit); |
| 37 | } |
| 38 | |
| 39 | static bool fd_limit_depleted(void) |
| 40 | { |
| 41 | long v = atomic_long_dec_return(&fd_limit); |
| 42 | |
| 43 | if (v >= 0) |
| 44 | return false; |
| 45 | atomic_long_inc(&fd_limit); |
| 46 | return true; |
| 47 | } |
| 48 | |
| 49 | static void fd_limit_close(void) |
| 50 | { |
| 51 | atomic_long_inc(&fd_limit); |
| 52 | } |
| 53 | |
| 54 | /* |
| 55 | * INODE hash |
| 56 | */ |
| 57 | |
| 58 | static unsigned long inode_hash(struct super_block *sb, unsigned long hashval) |
| 59 | { |
| 60 | unsigned long tmp; |
| 61 | |
| 62 | tmp = (hashval * (unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) / |
| 63 | L1_CACHE_BYTES; |
| 64 | tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> inode_hash_shift); |
| 65 | return tmp & inode_hash_mask; |
| 66 | } |
| 67 | |
| 68 | static struct ksmbd_inode *__ksmbd_inode_lookup(struct inode *inode) |
| 69 | { |
| 70 | struct hlist_head *head = inode_hashtable + |
| 71 | inode_hash(inode->i_sb, inode->i_ino); |
| 72 | struct ksmbd_inode *ci = NULL, *ret_ci = NULL; |
| 73 | |
| 74 | hlist_for_each_entry(ci, head, m_hash) { |
| 75 | if (ci->m_inode == inode) { |
| 76 | if (atomic_inc_not_zero(&ci->m_count)) |
| 77 | ret_ci = ci; |
| 78 | break; |
| 79 | } |
| 80 | } |
| 81 | return ret_ci; |
| 82 | } |
| 83 | |
| 84 | static struct ksmbd_inode *ksmbd_inode_lookup(struct ksmbd_file *fp) |
| 85 | { |
| 86 | return __ksmbd_inode_lookup(FP_INODE(fp)); |
| 87 | } |
| 88 | |
| 89 | static struct ksmbd_inode *ksmbd_inode_lookup_by_vfsinode(struct inode *inode) |
| 90 | { |
| 91 | struct ksmbd_inode *ci; |
| 92 | |
| 93 | read_lock(&inode_hash_lock); |
| 94 | ci = __ksmbd_inode_lookup(inode); |
| 95 | read_unlock(&inode_hash_lock); |
| 96 | return ci; |
| 97 | } |
| 98 | |
| 99 | int ksmbd_query_inode_status(struct inode *inode) |
| 100 | { |
| 101 | struct ksmbd_inode *ci; |
| 102 | int ret = KSMBD_INODE_STATUS_UNKNOWN; |
| 103 | |
| 104 | read_lock(&inode_hash_lock); |
| 105 | ci = __ksmbd_inode_lookup(inode); |
| 106 | if (ci) { |
| 107 | ret = KSMBD_INODE_STATUS_OK; |
| 108 | if (ci->m_flags & S_DEL_PENDING) |
| 109 | ret = KSMBD_INODE_STATUS_PENDING_DELETE; |
| 110 | atomic_dec(&ci->m_count); |
| 111 | } |
| 112 | read_unlock(&inode_hash_lock); |
| 113 | return ret; |
| 114 | } |
| 115 | |
| 116 | bool ksmbd_inode_pending_delete(struct ksmbd_file *fp) |
| 117 | { |
| 118 | return (fp->f_ci->m_flags & S_DEL_PENDING); |
| 119 | } |
| 120 | |
| 121 | void ksmbd_set_inode_pending_delete(struct ksmbd_file *fp) |
| 122 | { |
| 123 | fp->f_ci->m_flags |= S_DEL_PENDING; |
| 124 | } |
| 125 | |
| 126 | void ksmbd_clear_inode_pending_delete(struct ksmbd_file *fp) |
| 127 | { |
| 128 | fp->f_ci->m_flags &= ~S_DEL_PENDING; |
| 129 | } |
| 130 | |
| 131 | void ksmbd_fd_set_delete_on_close(struct ksmbd_file *fp, |
| 132 | int file_info) |
| 133 | { |
| 134 | if (ksmbd_stream_fd(fp)) { |
| 135 | fp->f_ci->m_flags |= S_DEL_ON_CLS_STREAM; |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | fp->f_ci->m_flags |= S_DEL_ON_CLS; |
| 140 | } |
| 141 | |
| 142 | static void ksmbd_inode_hash(struct ksmbd_inode *ci) |
| 143 | { |
| 144 | struct hlist_head *b = inode_hashtable + |
| 145 | inode_hash(ci->m_inode->i_sb, ci->m_inode->i_ino); |
| 146 | |
| 147 | hlist_add_head(&ci->m_hash, b); |
| 148 | } |
| 149 | |
| 150 | static void ksmbd_inode_unhash(struct ksmbd_inode *ci) |
| 151 | { |
| 152 | write_lock(&inode_hash_lock); |
| 153 | hlist_del_init(&ci->m_hash); |
| 154 | write_unlock(&inode_hash_lock); |
| 155 | } |
| 156 | |
| 157 | static int ksmbd_inode_init(struct ksmbd_inode *ci, struct ksmbd_file *fp) |
| 158 | { |
| 159 | ci->m_inode = FP_INODE(fp); |
| 160 | atomic_set(&ci->m_count, 1); |
| 161 | atomic_set(&ci->op_count, 0); |
| 162 | atomic_set(&ci->sop_count, 0); |
| 163 | ci->m_flags = 0; |
| 164 | ci->m_fattr = 0; |
| 165 | INIT_LIST_HEAD(&ci->m_fp_list); |
| 166 | INIT_LIST_HEAD(&ci->m_op_list); |
| 167 | rwlock_init(&ci->m_lock); |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | static struct ksmbd_inode *ksmbd_inode_get(struct ksmbd_file *fp) |
| 172 | { |
| 173 | struct ksmbd_inode *ci, *tmpci; |
| 174 | int rc; |
| 175 | |
| 176 | read_lock(&inode_hash_lock); |
| 177 | ci = ksmbd_inode_lookup(fp); |
| 178 | read_unlock(&inode_hash_lock); |
| 179 | if (ci) |
| 180 | return ci; |
| 181 | |
| 182 | ci = kmalloc(sizeof(struct ksmbd_inode), GFP_KERNEL); |
| 183 | if (!ci) |
| 184 | return NULL; |
| 185 | |
| 186 | rc = ksmbd_inode_init(ci, fp); |
| 187 | if (rc) { |
| 188 | ksmbd_err("inode initialized failed\n"); |
| 189 | kfree(ci); |
| 190 | return NULL; |
| 191 | } |
| 192 | |
| 193 | write_lock(&inode_hash_lock); |
| 194 | tmpci = ksmbd_inode_lookup(fp); |
| 195 | if (!tmpci) { |
| 196 | ksmbd_inode_hash(ci); |
| 197 | } else { |
| 198 | kfree(ci); |
| 199 | ci = tmpci; |
| 200 | } |
| 201 | write_unlock(&inode_hash_lock); |
| 202 | return ci; |
| 203 | } |
| 204 | |
| 205 | static void ksmbd_inode_free(struct ksmbd_inode *ci) |
| 206 | { |
| 207 | ksmbd_inode_unhash(ci); |
| 208 | kfree(ci); |
| 209 | } |
| 210 | |
| 211 | static void ksmbd_inode_put(struct ksmbd_inode *ci) |
| 212 | { |
| 213 | if (atomic_dec_and_test(&ci->m_count)) |
| 214 | ksmbd_inode_free(ci); |
| 215 | } |
| 216 | |
| 217 | int __init ksmbd_inode_hash_init(void) |
| 218 | { |
| 219 | unsigned int loop; |
| 220 | unsigned long numentries = 16384; |
| 221 | unsigned long bucketsize = sizeof(struct hlist_head); |
| 222 | unsigned long size; |
| 223 | |
| 224 | inode_hash_shift = ilog2(numentries); |
| 225 | inode_hash_mask = (1 << inode_hash_shift) - 1; |
| 226 | |
| 227 | size = bucketsize << inode_hash_shift; |
| 228 | |
| 229 | /* init master fp hash table */ |
| 230 | inode_hashtable = vmalloc(size); |
| 231 | if (!inode_hashtable) |
| 232 | return -ENOMEM; |
| 233 | |
| 234 | for (loop = 0; loop < (1U << inode_hash_shift); loop++) |
| 235 | INIT_HLIST_HEAD(&inode_hashtable[loop]); |
| 236 | return 0; |
| 237 | } |
| 238 | |
Dan Carpenter | 849f59e | 2021-03-23 16:27:04 +0300 | [diff] [blame] | 239 | void ksmbd_release_inode_hash(void) |
Namjae Jeon | f441584 | 2021-03-16 10:50:04 +0900 | [diff] [blame] | 240 | { |
| 241 | vfree(inode_hashtable); |
| 242 | } |
| 243 | |
| 244 | static void __ksmbd_inode_close(struct ksmbd_file *fp) |
| 245 | { |
| 246 | struct dentry *dir, *dentry; |
| 247 | struct ksmbd_inode *ci = fp->f_ci; |
| 248 | int err; |
| 249 | struct file *filp; |
| 250 | |
| 251 | filp = fp->filp; |
| 252 | if (ksmbd_stream_fd(fp) && (ci->m_flags & S_DEL_ON_CLS_STREAM)) { |
| 253 | ci->m_flags &= ~S_DEL_ON_CLS_STREAM; |
| 254 | err = ksmbd_vfs_remove_xattr(filp->f_path.dentry, |
| 255 | fp->stream.name); |
| 256 | if (err) |
| 257 | ksmbd_err("remove xattr failed : %s\n", |
| 258 | fp->stream.name); |
| 259 | } |
| 260 | |
| 261 | if (atomic_dec_and_test(&ci->m_count)) { |
| 262 | write_lock(&ci->m_lock); |
| 263 | if (ci->m_flags & (S_DEL_ON_CLS | S_DEL_PENDING)) { |
| 264 | dentry = filp->f_path.dentry; |
| 265 | dir = dentry->d_parent; |
| 266 | ci->m_flags &= ~(S_DEL_ON_CLS | S_DEL_PENDING); |
| 267 | write_unlock(&ci->m_lock); |
| 268 | ksmbd_vfs_unlink(dir, dentry); |
| 269 | write_lock(&ci->m_lock); |
| 270 | } |
| 271 | write_unlock(&ci->m_lock); |
| 272 | |
| 273 | ksmbd_inode_free(ci); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | static void __ksmbd_remove_durable_fd(struct ksmbd_file *fp) |
| 278 | { |
| 279 | if (!HAS_FILE_ID(fp->persistent_id)) |
| 280 | return; |
| 281 | |
| 282 | write_lock(&global_ft.lock); |
| 283 | idr_remove(global_ft.idr, fp->persistent_id); |
| 284 | write_unlock(&global_ft.lock); |
| 285 | } |
| 286 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 287 | static void __ksmbd_remove_fd(struct ksmbd_file_table *ft, struct ksmbd_file *fp) |
Namjae Jeon | f441584 | 2021-03-16 10:50:04 +0900 | [diff] [blame] | 288 | { |
| 289 | if (!HAS_FILE_ID(fp->volatile_id)) |
| 290 | return; |
| 291 | |
| 292 | write_lock(&fp->f_ci->m_lock); |
| 293 | list_del_init(&fp->node); |
| 294 | write_unlock(&fp->f_ci->m_lock); |
| 295 | |
| 296 | write_lock(&ft->lock); |
| 297 | idr_remove(ft->idr, fp->volatile_id); |
| 298 | write_unlock(&ft->lock); |
| 299 | } |
| 300 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 301 | static void __ksmbd_close_fd(struct ksmbd_file_table *ft, struct ksmbd_file *fp) |
Namjae Jeon | f441584 | 2021-03-16 10:50:04 +0900 | [diff] [blame] | 302 | { |
| 303 | struct file *filp; |
| 304 | |
| 305 | fd_limit_close(); |
| 306 | __ksmbd_remove_durable_fd(fp); |
| 307 | __ksmbd_remove_fd(ft, fp); |
| 308 | |
| 309 | close_id_del_oplock(fp); |
| 310 | filp = fp->filp; |
| 311 | |
| 312 | __ksmbd_inode_close(fp); |
| 313 | if (!IS_ERR_OR_NULL(filp)) |
| 314 | fput(filp); |
| 315 | kfree(fp->filename); |
| 316 | if (ksmbd_stream_fd(fp)) |
| 317 | kfree(fp->stream.name); |
| 318 | ksmbd_free_file_struct(fp); |
| 319 | } |
| 320 | |
| 321 | static struct ksmbd_file *ksmbd_fp_get(struct ksmbd_file *fp) |
| 322 | { |
| 323 | if (!atomic_inc_not_zero(&fp->refcount)) |
| 324 | return NULL; |
| 325 | return fp; |
| 326 | } |
| 327 | |
| 328 | static struct ksmbd_file *__ksmbd_lookup_fd(struct ksmbd_file_table *ft, |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 329 | unsigned int id) |
Namjae Jeon | f441584 | 2021-03-16 10:50:04 +0900 | [diff] [blame] | 330 | { |
| 331 | bool unclaimed = true; |
| 332 | struct ksmbd_file *fp; |
| 333 | |
| 334 | read_lock(&ft->lock); |
| 335 | fp = idr_find(ft->idr, id); |
| 336 | if (fp) |
| 337 | fp = ksmbd_fp_get(fp); |
| 338 | |
| 339 | if (fp && fp->f_ci) { |
| 340 | read_lock(&fp->f_ci->m_lock); |
| 341 | unclaimed = list_empty(&fp->node); |
| 342 | read_unlock(&fp->f_ci->m_lock); |
| 343 | } |
| 344 | read_unlock(&ft->lock); |
| 345 | |
| 346 | if (fp && unclaimed) { |
| 347 | atomic_dec(&fp->refcount); |
| 348 | return NULL; |
| 349 | } |
| 350 | return fp; |
| 351 | } |
| 352 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 353 | static void __put_fd_final(struct ksmbd_work *work, struct ksmbd_file *fp) |
Namjae Jeon | f441584 | 2021-03-16 10:50:04 +0900 | [diff] [blame] | 354 | { |
| 355 | __ksmbd_close_fd(&work->sess->file_table, fp); |
| 356 | atomic_dec(&work->conn->stats.open_files_count); |
| 357 | } |
| 358 | |
| 359 | static void set_close_state_blocked_works(struct ksmbd_file *fp) |
| 360 | { |
| 361 | struct ksmbd_work *cancel_work, *ctmp; |
| 362 | |
| 363 | spin_lock(&fp->f_lock); |
| 364 | list_for_each_entry_safe(cancel_work, ctmp, &fp->blocked_works, |
| 365 | fp_entry) { |
| 366 | list_del(&cancel_work->fp_entry); |
| 367 | cancel_work->state = KSMBD_WORK_CLOSED; |
| 368 | cancel_work->cancel_fn(cancel_work->cancel_argv); |
| 369 | } |
| 370 | spin_unlock(&fp->f_lock); |
| 371 | } |
| 372 | |
| 373 | int ksmbd_close_fd(struct ksmbd_work *work, unsigned int id) |
| 374 | { |
| 375 | struct ksmbd_file *fp; |
| 376 | struct ksmbd_file_table *ft; |
| 377 | |
| 378 | if (!HAS_FILE_ID(id)) |
| 379 | return 0; |
| 380 | |
| 381 | ft = &work->sess->file_table; |
| 382 | read_lock(&ft->lock); |
| 383 | fp = idr_find(ft->idr, id); |
| 384 | if (fp) { |
| 385 | set_close_state_blocked_works(fp); |
| 386 | |
| 387 | if (!atomic_dec_and_test(&fp->refcount)) |
| 388 | fp = NULL; |
| 389 | } |
| 390 | read_unlock(&ft->lock); |
| 391 | |
| 392 | if (!fp) |
| 393 | return -EINVAL; |
| 394 | |
| 395 | __put_fd_final(work, fp); |
| 396 | return 0; |
| 397 | } |
| 398 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 399 | void ksmbd_fd_put(struct ksmbd_work *work, struct ksmbd_file *fp) |
Namjae Jeon | f441584 | 2021-03-16 10:50:04 +0900 | [diff] [blame] | 400 | { |
| 401 | if (!fp) |
| 402 | return; |
| 403 | |
| 404 | if (!atomic_dec_and_test(&fp->refcount)) |
| 405 | return; |
| 406 | __put_fd_final(work, fp); |
| 407 | } |
| 408 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 409 | static bool __sanity_check(struct ksmbd_tree_connect *tcon, struct ksmbd_file *fp) |
Namjae Jeon | f441584 | 2021-03-16 10:50:04 +0900 | [diff] [blame] | 410 | { |
| 411 | if (!fp) |
| 412 | return false; |
| 413 | if (fp->tcon != tcon) |
| 414 | return false; |
| 415 | return true; |
| 416 | } |
| 417 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 418 | struct ksmbd_file *ksmbd_lookup_foreign_fd(struct ksmbd_work *work, unsigned int id) |
Namjae Jeon | f441584 | 2021-03-16 10:50:04 +0900 | [diff] [blame] | 419 | { |
| 420 | return __ksmbd_lookup_fd(&work->sess->file_table, id); |
| 421 | } |
| 422 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 423 | struct ksmbd_file *ksmbd_lookup_fd_fast(struct ksmbd_work *work, unsigned int id) |
Namjae Jeon | f441584 | 2021-03-16 10:50:04 +0900 | [diff] [blame] | 424 | { |
| 425 | struct ksmbd_file *fp = __ksmbd_lookup_fd(&work->sess->file_table, id); |
| 426 | |
| 427 | if (__sanity_check(work->tcon, fp)) |
| 428 | return fp; |
| 429 | |
| 430 | ksmbd_fd_put(work, fp); |
| 431 | return NULL; |
| 432 | } |
| 433 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 434 | struct ksmbd_file *ksmbd_lookup_fd_slow(struct ksmbd_work *work, unsigned int id, |
| 435 | unsigned int pid) |
Namjae Jeon | f441584 | 2021-03-16 10:50:04 +0900 | [diff] [blame] | 436 | { |
| 437 | struct ksmbd_file *fp; |
| 438 | |
| 439 | if (!HAS_FILE_ID(id)) { |
| 440 | id = work->compound_fid; |
| 441 | pid = work->compound_pfid; |
| 442 | } |
| 443 | |
| 444 | if (!HAS_FILE_ID(id)) |
| 445 | return NULL; |
| 446 | |
| 447 | fp = __ksmbd_lookup_fd(&work->sess->file_table, id); |
| 448 | if (!__sanity_check(work->tcon, fp)) { |
| 449 | ksmbd_fd_put(work, fp); |
| 450 | return NULL; |
| 451 | } |
| 452 | if (fp->persistent_id != pid) { |
| 453 | ksmbd_fd_put(work, fp); |
| 454 | return NULL; |
| 455 | } |
| 456 | return fp; |
| 457 | } |
| 458 | |
| 459 | struct ksmbd_file *ksmbd_lookup_durable_fd(unsigned long long id) |
| 460 | { |
| 461 | return __ksmbd_lookup_fd(&global_ft, id); |
| 462 | } |
| 463 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 464 | int ksmbd_close_fd_app_id(struct ksmbd_work *work, char *app_id) |
Namjae Jeon | f441584 | 2021-03-16 10:50:04 +0900 | [diff] [blame] | 465 | { |
| 466 | struct ksmbd_file *fp = NULL; |
| 467 | unsigned int id; |
| 468 | |
| 469 | read_lock(&global_ft.lock); |
| 470 | idr_for_each_entry(global_ft.idr, fp, id) { |
| 471 | if (!memcmp(fp->app_instance_id, |
| 472 | app_id, |
| 473 | SMB2_CREATE_GUID_SIZE)) { |
| 474 | if (!atomic_dec_and_test(&fp->refcount)) |
| 475 | fp = NULL; |
| 476 | break; |
| 477 | } |
| 478 | } |
| 479 | read_unlock(&global_ft.lock); |
| 480 | |
| 481 | if (!fp) |
| 482 | return -EINVAL; |
| 483 | |
| 484 | __put_fd_final(work, fp); |
| 485 | return 0; |
| 486 | } |
| 487 | |
| 488 | struct ksmbd_file *ksmbd_lookup_fd_cguid(char *cguid) |
| 489 | { |
| 490 | struct ksmbd_file *fp = NULL; |
| 491 | unsigned int id; |
| 492 | |
| 493 | read_lock(&global_ft.lock); |
| 494 | idr_for_each_entry(global_ft.idr, fp, id) { |
| 495 | if (!memcmp(fp->create_guid, |
| 496 | cguid, |
| 497 | SMB2_CREATE_GUID_SIZE)) { |
| 498 | fp = ksmbd_fp_get(fp); |
| 499 | break; |
| 500 | } |
| 501 | } |
| 502 | read_unlock(&global_ft.lock); |
| 503 | |
| 504 | return fp; |
| 505 | } |
| 506 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 507 | struct ksmbd_file *ksmbd_lookup_fd_filename(struct ksmbd_work *work, char *filename) |
Namjae Jeon | f441584 | 2021-03-16 10:50:04 +0900 | [diff] [blame] | 508 | { |
| 509 | struct ksmbd_file *fp = NULL; |
| 510 | unsigned int id; |
| 511 | |
| 512 | read_lock(&work->sess->file_table.lock); |
| 513 | idr_for_each_entry(work->sess->file_table.idr, fp, id) { |
| 514 | if (!strcmp(fp->filename, filename)) { |
| 515 | fp = ksmbd_fp_get(fp); |
| 516 | break; |
| 517 | } |
| 518 | } |
| 519 | read_unlock(&work->sess->file_table.lock); |
| 520 | |
| 521 | return fp; |
| 522 | } |
| 523 | |
| 524 | struct ksmbd_file *ksmbd_lookup_fd_inode(struct inode *inode) |
| 525 | { |
| 526 | struct ksmbd_file *lfp; |
| 527 | struct ksmbd_inode *ci; |
| 528 | struct list_head *cur; |
| 529 | |
| 530 | ci = ksmbd_inode_lookup_by_vfsinode(inode); |
| 531 | if (!ci) |
| 532 | return NULL; |
| 533 | |
| 534 | read_lock(&ci->m_lock); |
| 535 | list_for_each(cur, &ci->m_fp_list) { |
| 536 | lfp = list_entry(cur, struct ksmbd_file, node); |
| 537 | if (inode == FP_INODE(lfp)) { |
| 538 | atomic_dec(&ci->m_count); |
| 539 | read_unlock(&ci->m_lock); |
| 540 | return lfp; |
| 541 | } |
| 542 | } |
| 543 | atomic_dec(&ci->m_count); |
| 544 | read_unlock(&ci->m_lock); |
| 545 | return NULL; |
| 546 | } |
| 547 | |
| 548 | #define OPEN_ID_TYPE_VOLATILE_ID (0) |
| 549 | #define OPEN_ID_TYPE_PERSISTENT_ID (1) |
| 550 | |
| 551 | static void __open_id_set(struct ksmbd_file *fp, unsigned int id, int type) |
| 552 | { |
| 553 | if (type == OPEN_ID_TYPE_VOLATILE_ID) |
| 554 | fp->volatile_id = id; |
| 555 | if (type == OPEN_ID_TYPE_PERSISTENT_ID) |
| 556 | fp->persistent_id = id; |
| 557 | } |
| 558 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 559 | static int __open_id(struct ksmbd_file_table *ft, struct ksmbd_file *fp, |
Namjae Jeon | f441584 | 2021-03-16 10:50:04 +0900 | [diff] [blame] | 560 | int type) |
| 561 | { |
| 562 | unsigned int id = 0; |
| 563 | int ret; |
| 564 | |
| 565 | if (type == OPEN_ID_TYPE_VOLATILE_ID && fd_limit_depleted()) { |
| 566 | __open_id_set(fp, KSMBD_NO_FID, type); |
| 567 | return -EMFILE; |
| 568 | } |
| 569 | |
| 570 | idr_preload(GFP_KERNEL); |
| 571 | write_lock(&ft->lock); |
| 572 | ret = idr_alloc_cyclic(ft->idr, fp, 0, INT_MAX, GFP_NOWAIT); |
| 573 | if (ret >= 0) { |
| 574 | id = ret; |
| 575 | ret = 0; |
| 576 | } else { |
| 577 | id = KSMBD_NO_FID; |
| 578 | fd_limit_close(); |
| 579 | } |
| 580 | |
| 581 | __open_id_set(fp, id, type); |
| 582 | write_unlock(&ft->lock); |
| 583 | idr_preload_end(); |
| 584 | return ret; |
| 585 | } |
| 586 | |
| 587 | unsigned int ksmbd_open_durable_fd(struct ksmbd_file *fp) |
| 588 | { |
| 589 | __open_id(&global_ft, fp, OPEN_ID_TYPE_PERSISTENT_ID); |
| 590 | return fp->persistent_id; |
| 591 | } |
| 592 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 593 | struct ksmbd_file *ksmbd_open_fd(struct ksmbd_work *work, struct file *filp) |
Namjae Jeon | f441584 | 2021-03-16 10:50:04 +0900 | [diff] [blame] | 594 | { |
| 595 | struct ksmbd_file *fp; |
| 596 | int ret; |
| 597 | |
| 598 | fp = ksmbd_alloc_file_struct(); |
| 599 | if (!fp) { |
| 600 | ksmbd_err("Failed to allocate memory\n"); |
| 601 | return ERR_PTR(-ENOMEM); |
| 602 | } |
| 603 | |
| 604 | INIT_LIST_HEAD(&fp->blocked_works); |
| 605 | INIT_LIST_HEAD(&fp->node); |
| 606 | spin_lock_init(&fp->f_lock); |
| 607 | atomic_set(&fp->refcount, 1); |
| 608 | |
| 609 | fp->filp = filp; |
| 610 | fp->conn = work->sess->conn; |
| 611 | fp->tcon = work->tcon; |
| 612 | fp->volatile_id = KSMBD_NO_FID; |
| 613 | fp->persistent_id = KSMBD_NO_FID; |
| 614 | fp->f_ci = ksmbd_inode_get(fp); |
| 615 | |
| 616 | if (!fp->f_ci) { |
| 617 | ksmbd_free_file_struct(fp); |
| 618 | return ERR_PTR(-ENOMEM); |
| 619 | } |
| 620 | |
| 621 | ret = __open_id(&work->sess->file_table, fp, OPEN_ID_TYPE_VOLATILE_ID); |
| 622 | if (ret) { |
| 623 | ksmbd_inode_put(fp->f_ci); |
| 624 | ksmbd_free_file_struct(fp); |
| 625 | return ERR_PTR(ret); |
| 626 | } |
| 627 | |
| 628 | atomic_inc(&work->conn->stats.open_files_count); |
| 629 | return fp; |
| 630 | } |
| 631 | |
| 632 | static inline bool is_reconnectable(struct ksmbd_file *fp) |
| 633 | { |
| 634 | struct oplock_info *opinfo = opinfo_get(fp); |
| 635 | bool reconn = false; |
| 636 | |
| 637 | if (!opinfo) |
| 638 | return false; |
| 639 | |
| 640 | if (opinfo->op_state != OPLOCK_STATE_NONE) { |
| 641 | opinfo_put(opinfo); |
| 642 | return false; |
| 643 | } |
| 644 | |
| 645 | if (fp->is_resilient || fp->is_persistent) |
| 646 | reconn = true; |
| 647 | else if (fp->is_durable && opinfo->is_lease && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 648 | opinfo->o_lease->state & SMB2_LEASE_HANDLE_CACHING_LE) |
Namjae Jeon | f441584 | 2021-03-16 10:50:04 +0900 | [diff] [blame] | 649 | reconn = true; |
| 650 | |
| 651 | else if (fp->is_durable && opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) |
| 652 | reconn = true; |
| 653 | |
| 654 | opinfo_put(opinfo); |
| 655 | return reconn; |
| 656 | } |
| 657 | |
| 658 | static int |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 659 | __close_file_table_ids(struct ksmbd_file_table *ft, struct ksmbd_tree_connect *tcon, |
| 660 | bool (*skip)(struct ksmbd_tree_connect *tcon, struct ksmbd_file *fp)) |
Namjae Jeon | f441584 | 2021-03-16 10:50:04 +0900 | [diff] [blame] | 661 | { |
| 662 | unsigned int id; |
| 663 | struct ksmbd_file *fp; |
| 664 | int num = 0; |
| 665 | |
| 666 | idr_for_each_entry(ft->idr, fp, id) { |
| 667 | if (skip(tcon, fp)) |
| 668 | continue; |
| 669 | |
| 670 | set_close_state_blocked_works(fp); |
| 671 | |
| 672 | if (!atomic_dec_and_test(&fp->refcount)) |
| 673 | continue; |
| 674 | __ksmbd_close_fd(ft, fp); |
| 675 | num++; |
| 676 | } |
| 677 | return num; |
| 678 | } |
| 679 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 680 | static bool tree_conn_fd_check(struct ksmbd_tree_connect *tcon, struct ksmbd_file *fp) |
Namjae Jeon | f441584 | 2021-03-16 10:50:04 +0900 | [diff] [blame] | 681 | { |
| 682 | return fp->tcon != tcon; |
| 683 | } |
| 684 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 685 | static bool session_fd_check(struct ksmbd_tree_connect *tcon, struct ksmbd_file *fp) |
Namjae Jeon | f441584 | 2021-03-16 10:50:04 +0900 | [diff] [blame] | 686 | { |
| 687 | if (!is_reconnectable(fp)) |
| 688 | return false; |
| 689 | |
| 690 | fp->conn = NULL; |
| 691 | fp->tcon = NULL; |
| 692 | fp->volatile_id = KSMBD_NO_FID; |
| 693 | return true; |
| 694 | } |
| 695 | |
| 696 | void ksmbd_close_tree_conn_fds(struct ksmbd_work *work) |
| 697 | { |
| 698 | int num = __close_file_table_ids(&work->sess->file_table, |
| 699 | work->tcon, |
| 700 | tree_conn_fd_check); |
| 701 | |
| 702 | atomic_sub(num, &work->conn->stats.open_files_count); |
| 703 | } |
| 704 | |
| 705 | void ksmbd_close_session_fds(struct ksmbd_work *work) |
| 706 | { |
| 707 | int num = __close_file_table_ids(&work->sess->file_table, |
| 708 | work->tcon, |
| 709 | session_fd_check); |
| 710 | |
| 711 | atomic_sub(num, &work->conn->stats.open_files_count); |
| 712 | } |
| 713 | |
| 714 | int ksmbd_init_global_file_table(void) |
| 715 | { |
| 716 | return ksmbd_init_file_table(&global_ft); |
| 717 | } |
| 718 | |
| 719 | void ksmbd_free_global_file_table(void) |
| 720 | { |
| 721 | struct ksmbd_file *fp = NULL; |
| 722 | unsigned int id; |
| 723 | |
| 724 | idr_for_each_entry(global_ft.idr, fp, id) { |
| 725 | __ksmbd_remove_durable_fd(fp); |
| 726 | ksmbd_free_file_struct(fp); |
| 727 | } |
| 728 | |
| 729 | ksmbd_destroy_file_table(&global_ft); |
| 730 | } |
| 731 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 732 | int ksmbd_reopen_durable_fd(struct ksmbd_work *work, struct ksmbd_file *fp) |
Namjae Jeon | f441584 | 2021-03-16 10:50:04 +0900 | [diff] [blame] | 733 | { |
| 734 | if (!fp->is_durable || fp->conn || fp->tcon) { |
| 735 | ksmbd_err("Invalid durable fd [%p:%p]\n", |
| 736 | fp->conn, fp->tcon); |
| 737 | return -EBADF; |
| 738 | } |
| 739 | |
| 740 | if (HAS_FILE_ID(fp->volatile_id)) { |
| 741 | ksmbd_err("Still in use durable fd: %u\n", fp->volatile_id); |
| 742 | return -EBADF; |
| 743 | } |
| 744 | |
| 745 | fp->conn = work->sess->conn; |
| 746 | fp->tcon = work->tcon; |
| 747 | |
| 748 | __open_id(&work->sess->file_table, fp, OPEN_ID_TYPE_VOLATILE_ID); |
| 749 | if (!HAS_FILE_ID(fp->volatile_id)) { |
| 750 | fp->conn = NULL; |
| 751 | fp->tcon = NULL; |
| 752 | return -EBADF; |
| 753 | } |
| 754 | return 0; |
| 755 | } |
| 756 | |
| 757 | static void close_fd_list(struct ksmbd_work *work, struct list_head *head) |
| 758 | { |
| 759 | while (!list_empty(head)) { |
| 760 | struct ksmbd_file *fp; |
| 761 | |
| 762 | fp = list_first_entry(head, struct ksmbd_file, node); |
| 763 | list_del_init(&fp->node); |
| 764 | |
| 765 | __ksmbd_close_fd(&work->sess->file_table, fp); |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | int ksmbd_close_inode_fds(struct ksmbd_work *work, struct inode *inode) |
| 770 | { |
| 771 | struct ksmbd_inode *ci; |
| 772 | bool unlinked = true; |
| 773 | struct ksmbd_file *fp, *fptmp; |
| 774 | LIST_HEAD(dispose); |
| 775 | |
| 776 | ci = ksmbd_inode_lookup_by_vfsinode(inode); |
| 777 | if (!ci) |
| 778 | return true; |
| 779 | |
| 780 | if (ci->m_flags & (S_DEL_ON_CLS | S_DEL_PENDING)) |
| 781 | unlinked = false; |
| 782 | |
| 783 | write_lock(&ci->m_lock); |
| 784 | list_for_each_entry_safe(fp, fptmp, &ci->m_fp_list, node) { |
| 785 | if (fp->conn) |
| 786 | continue; |
| 787 | |
| 788 | list_del(&fp->node); |
| 789 | list_add(&fp->node, &dispose); |
| 790 | } |
| 791 | atomic_dec(&ci->m_count); |
| 792 | write_unlock(&ci->m_lock); |
| 793 | |
| 794 | close_fd_list(work, &dispose); |
| 795 | return unlinked; |
| 796 | } |
| 797 | |
| 798 | int ksmbd_file_table_flush(struct ksmbd_work *work) |
| 799 | { |
| 800 | struct ksmbd_file *fp = NULL; |
| 801 | unsigned int id; |
| 802 | int ret; |
| 803 | |
| 804 | read_lock(&work->sess->file_table.lock); |
| 805 | idr_for_each_entry(work->sess->file_table.idr, fp, id) { |
| 806 | ret = ksmbd_vfs_fsync(work, fp->volatile_id, KSMBD_NO_FID); |
| 807 | if (ret) |
| 808 | break; |
| 809 | } |
| 810 | read_unlock(&work->sess->file_table.lock); |
| 811 | return ret; |
| 812 | } |
| 813 | |
| 814 | int ksmbd_init_file_table(struct ksmbd_file_table *ft) |
| 815 | { |
Namjae Jeon | 20ea7fd | 2021-03-30 12:40:47 +0900 | [diff] [blame] | 816 | ft->idr = kzalloc(sizeof(struct idr), GFP_KERNEL); |
Namjae Jeon | f441584 | 2021-03-16 10:50:04 +0900 | [diff] [blame] | 817 | if (!ft->idr) |
| 818 | return -ENOMEM; |
| 819 | |
| 820 | idr_init(ft->idr); |
| 821 | rwlock_init(&ft->lock); |
| 822 | return 0; |
| 823 | } |
| 824 | |
| 825 | void ksmbd_destroy_file_table(struct ksmbd_file_table *ft) |
| 826 | { |
| 827 | if (!ft->idr) |
| 828 | return; |
| 829 | |
| 830 | __close_file_table_ids(ft, NULL, session_fd_check); |
| 831 | idr_destroy(ft->idr); |
| 832 | ksmbd_free(ft->idr); |
| 833 | ft->idr = NULL; |
| 834 | } |