^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * (C) 2001 Clemson University and The University of Chicago
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * See COPYING in top-level directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include "protocol.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include "orangefs-kernel.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) /* tags assigned to kernel upcall operations */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) static __u64 next_tag_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) static DEFINE_SPINLOCK(next_tag_value_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) /* the orangefs memory caches */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) /* a cache for orangefs upcall/downcall operations */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) static struct kmem_cache *op_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) int op_cache_initialize(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) op_cache = kmem_cache_create("orangefs_op_cache",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) sizeof(struct orangefs_kernel_op_s),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) ORANGEFS_CACHE_CREATE_FLAGS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) if (!op_cache) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) gossip_err("Cannot create orangefs_op_cache\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) /* initialize our atomic tag counter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) spin_lock(&next_tag_value_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) next_tag_value = 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) spin_unlock(&next_tag_value_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) int op_cache_finalize(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) kmem_cache_destroy(op_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) char *get_opname_string(struct orangefs_kernel_op_s *new_op)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) if (new_op) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) __s32 type = new_op->upcall.type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) if (type == ORANGEFS_VFS_OP_FILE_IO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) return "OP_FILE_IO";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) else if (type == ORANGEFS_VFS_OP_LOOKUP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) return "OP_LOOKUP";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) else if (type == ORANGEFS_VFS_OP_CREATE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) return "OP_CREATE";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) else if (type == ORANGEFS_VFS_OP_GETATTR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) return "OP_GETATTR";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) else if (type == ORANGEFS_VFS_OP_REMOVE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) return "OP_REMOVE";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) else if (type == ORANGEFS_VFS_OP_MKDIR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) return "OP_MKDIR";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) else if (type == ORANGEFS_VFS_OP_READDIR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) return "OP_READDIR";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) else if (type == ORANGEFS_VFS_OP_READDIRPLUS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) return "OP_READDIRPLUS";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) else if (type == ORANGEFS_VFS_OP_SETATTR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) return "OP_SETATTR";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) else if (type == ORANGEFS_VFS_OP_SYMLINK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) return "OP_SYMLINK";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) else if (type == ORANGEFS_VFS_OP_RENAME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) return "OP_RENAME";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) else if (type == ORANGEFS_VFS_OP_STATFS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) return "OP_STATFS";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) else if (type == ORANGEFS_VFS_OP_TRUNCATE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) return "OP_TRUNCATE";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) else if (type == ORANGEFS_VFS_OP_RA_FLUSH)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) return "OP_RA_FLUSH";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) else if (type == ORANGEFS_VFS_OP_FS_MOUNT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) return "OP_FS_MOUNT";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) else if (type == ORANGEFS_VFS_OP_FS_UMOUNT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) return "OP_FS_UMOUNT";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) else if (type == ORANGEFS_VFS_OP_GETXATTR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) return "OP_GETXATTR";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) else if (type == ORANGEFS_VFS_OP_SETXATTR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) return "OP_SETXATTR";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) else if (type == ORANGEFS_VFS_OP_LISTXATTR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) return "OP_LISTXATTR";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) else if (type == ORANGEFS_VFS_OP_REMOVEXATTR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) return "OP_REMOVEXATTR";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) else if (type == ORANGEFS_VFS_OP_PARAM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) return "OP_PARAM";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) else if (type == ORANGEFS_VFS_OP_PERF_COUNT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) return "OP_PERF_COUNT";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) else if (type == ORANGEFS_VFS_OP_CANCEL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) return "OP_CANCEL";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) else if (type == ORANGEFS_VFS_OP_FSYNC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) return "OP_FSYNC";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) else if (type == ORANGEFS_VFS_OP_FSKEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) return "OP_FSKEY";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) else if (type == ORANGEFS_VFS_OP_FEATURES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) return "OP_FEATURES";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) return "OP_UNKNOWN?";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) void orangefs_new_tag(struct orangefs_kernel_op_s *op)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) spin_lock(&next_tag_value_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) op->tag = next_tag_value++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (next_tag_value == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) next_tag_value = 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) spin_unlock(&next_tag_value_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) struct orangefs_kernel_op_s *op_alloc(__s32 type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) struct orangefs_kernel_op_s *new_op = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) new_op = kmem_cache_zalloc(op_cache, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) if (new_op) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) INIT_LIST_HEAD(&new_op->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) spin_lock_init(&new_op->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) init_completion(&new_op->waitq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) new_op->upcall.type = ORANGEFS_VFS_OP_INVALID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) new_op->downcall.type = ORANGEFS_VFS_OP_INVALID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) new_op->downcall.status = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) new_op->op_state = OP_VFS_STATE_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) /* initialize the op specific tag and upcall credentials */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) orangefs_new_tag(new_op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) new_op->upcall.type = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) new_op->attempts = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) gossip_debug(GOSSIP_CACHE_DEBUG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) "Alloced OP (%p: %llu %s)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) new_op,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) llu(new_op->tag),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) get_opname_string(new_op));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) new_op->upcall.uid = from_kuid(&init_user_ns,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) current_fsuid());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) new_op->upcall.gid = from_kgid(&init_user_ns,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) current_fsgid());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) gossip_err("op_alloc: kmem_cache_zalloc failed!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) return new_op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) void op_release(struct orangefs_kernel_op_s *orangefs_op)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) if (orangefs_op) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) gossip_debug(GOSSIP_CACHE_DEBUG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) "Releasing OP (%p: %llu)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) orangefs_op,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) llu(orangefs_op->tag));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) kmem_cache_free(op_cache, orangefs_op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) gossip_err("NULL pointer in op_release\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }