blob: 3b6982bf6bcf3ecd64f36fa118d4ec551f080acf [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Mike Marshall274dcf52015-07-17 10:38:13 -04002/*
3 * (C) 2001 Clemson University and The University of Chicago
4 *
5 * See COPYING in top-level directory.
6 */
7
8#include "protocol.h"
Mike Marshall575e9462015-12-04 12:56:14 -05009#include "orangefs-kernel.h"
Mike Marshall274dcf52015-07-17 10:38:13 -040010
11/* tags assigned to kernel upcall operations */
12static __u64 next_tag_value;
13static DEFINE_SPINLOCK(next_tag_value_lock);
14
Yi Liu8bb8aef2015-11-24 15:12:14 -050015/* the orangefs memory caches */
Mike Marshall274dcf52015-07-17 10:38:13 -040016
Yi Liu8bb8aef2015-11-24 15:12:14 -050017/* a cache for orangefs upcall/downcall operations */
Mike Marshall274dcf52015-07-17 10:38:13 -040018static struct kmem_cache *op_cache;
19
Mike Marshall274dcf52015-07-17 10:38:13 -040020int op_cache_initialize(void)
21{
Yi Liu8bb8aef2015-11-24 15:12:14 -050022 op_cache = kmem_cache_create("orangefs_op_cache",
23 sizeof(struct orangefs_kernel_op_s),
Mike Marshall274dcf52015-07-17 10:38:13 -040024 0,
Yi Liu8bb8aef2015-11-24 15:12:14 -050025 ORANGEFS_CACHE_CREATE_FLAGS,
Mike Marshall274dcf52015-07-17 10:38:13 -040026 NULL);
27
28 if (!op_cache) {
Yi Liu8bb8aef2015-11-24 15:12:14 -050029 gossip_err("Cannot create orangefs_op_cache\n");
Mike Marshall274dcf52015-07-17 10:38:13 -040030 return -ENOMEM;
31 }
32
33 /* initialize our atomic tag counter */
34 spin_lock(&next_tag_value_lock);
35 next_tag_value = 100;
36 spin_unlock(&next_tag_value_lock);
37 return 0;
38}
39
40int op_cache_finalize(void)
41{
42 kmem_cache_destroy(op_cache);
43 return 0;
44}
45
Yi Liu8bb8aef2015-11-24 15:12:14 -050046char *get_opname_string(struct orangefs_kernel_op_s *new_op)
Mike Marshall274dcf52015-07-17 10:38:13 -040047{
48 if (new_op) {
49 __s32 type = new_op->upcall.type;
50
Yi Liu8bb8aef2015-11-24 15:12:14 -050051 if (type == ORANGEFS_VFS_OP_FILE_IO)
Mike Marshall274dcf52015-07-17 10:38:13 -040052 return "OP_FILE_IO";
Yi Liu8bb8aef2015-11-24 15:12:14 -050053 else if (type == ORANGEFS_VFS_OP_LOOKUP)
Mike Marshall274dcf52015-07-17 10:38:13 -040054 return "OP_LOOKUP";
Yi Liu8bb8aef2015-11-24 15:12:14 -050055 else if (type == ORANGEFS_VFS_OP_CREATE)
Mike Marshall274dcf52015-07-17 10:38:13 -040056 return "OP_CREATE";
Yi Liu8bb8aef2015-11-24 15:12:14 -050057 else if (type == ORANGEFS_VFS_OP_GETATTR)
Mike Marshall274dcf52015-07-17 10:38:13 -040058 return "OP_GETATTR";
Yi Liu8bb8aef2015-11-24 15:12:14 -050059 else if (type == ORANGEFS_VFS_OP_REMOVE)
Mike Marshall274dcf52015-07-17 10:38:13 -040060 return "OP_REMOVE";
Yi Liu8bb8aef2015-11-24 15:12:14 -050061 else if (type == ORANGEFS_VFS_OP_MKDIR)
Mike Marshall274dcf52015-07-17 10:38:13 -040062 return "OP_MKDIR";
Yi Liu8bb8aef2015-11-24 15:12:14 -050063 else if (type == ORANGEFS_VFS_OP_READDIR)
Mike Marshall274dcf52015-07-17 10:38:13 -040064 return "OP_READDIR";
Yi Liu8bb8aef2015-11-24 15:12:14 -050065 else if (type == ORANGEFS_VFS_OP_READDIRPLUS)
Mike Marshall274dcf52015-07-17 10:38:13 -040066 return "OP_READDIRPLUS";
Yi Liu8bb8aef2015-11-24 15:12:14 -050067 else if (type == ORANGEFS_VFS_OP_SETATTR)
Mike Marshall274dcf52015-07-17 10:38:13 -040068 return "OP_SETATTR";
Yi Liu8bb8aef2015-11-24 15:12:14 -050069 else if (type == ORANGEFS_VFS_OP_SYMLINK)
Mike Marshall274dcf52015-07-17 10:38:13 -040070 return "OP_SYMLINK";
Yi Liu8bb8aef2015-11-24 15:12:14 -050071 else if (type == ORANGEFS_VFS_OP_RENAME)
Mike Marshall274dcf52015-07-17 10:38:13 -040072 return "OP_RENAME";
Yi Liu8bb8aef2015-11-24 15:12:14 -050073 else if (type == ORANGEFS_VFS_OP_STATFS)
Mike Marshall274dcf52015-07-17 10:38:13 -040074 return "OP_STATFS";
Yi Liu8bb8aef2015-11-24 15:12:14 -050075 else if (type == ORANGEFS_VFS_OP_TRUNCATE)
Mike Marshall274dcf52015-07-17 10:38:13 -040076 return "OP_TRUNCATE";
Martin Brandenburg6eaff8c2016-08-02 14:31:05 -040077 else if (type == ORANGEFS_VFS_OP_RA_FLUSH)
78 return "OP_RA_FLUSH";
Yi Liu8bb8aef2015-11-24 15:12:14 -050079 else if (type == ORANGEFS_VFS_OP_FS_MOUNT)
Mike Marshall274dcf52015-07-17 10:38:13 -040080 return "OP_FS_MOUNT";
Yi Liu8bb8aef2015-11-24 15:12:14 -050081 else if (type == ORANGEFS_VFS_OP_FS_UMOUNT)
Mike Marshall274dcf52015-07-17 10:38:13 -040082 return "OP_FS_UMOUNT";
Yi Liu8bb8aef2015-11-24 15:12:14 -050083 else if (type == ORANGEFS_VFS_OP_GETXATTR)
Mike Marshall274dcf52015-07-17 10:38:13 -040084 return "OP_GETXATTR";
Yi Liu8bb8aef2015-11-24 15:12:14 -050085 else if (type == ORANGEFS_VFS_OP_SETXATTR)
Mike Marshall274dcf52015-07-17 10:38:13 -040086 return "OP_SETXATTR";
Yi Liu8bb8aef2015-11-24 15:12:14 -050087 else if (type == ORANGEFS_VFS_OP_LISTXATTR)
Mike Marshall274dcf52015-07-17 10:38:13 -040088 return "OP_LISTXATTR";
Yi Liu8bb8aef2015-11-24 15:12:14 -050089 else if (type == ORANGEFS_VFS_OP_REMOVEXATTR)
Mike Marshall274dcf52015-07-17 10:38:13 -040090 return "OP_REMOVEXATTR";
Yi Liu8bb8aef2015-11-24 15:12:14 -050091 else if (type == ORANGEFS_VFS_OP_PARAM)
Mike Marshall274dcf52015-07-17 10:38:13 -040092 return "OP_PARAM";
Yi Liu8bb8aef2015-11-24 15:12:14 -050093 else if (type == ORANGEFS_VFS_OP_PERF_COUNT)
Mike Marshall274dcf52015-07-17 10:38:13 -040094 return "OP_PERF_COUNT";
Yi Liu8bb8aef2015-11-24 15:12:14 -050095 else if (type == ORANGEFS_VFS_OP_CANCEL)
Mike Marshall274dcf52015-07-17 10:38:13 -040096 return "OP_CANCEL";
Yi Liu8bb8aef2015-11-24 15:12:14 -050097 else if (type == ORANGEFS_VFS_OP_FSYNC)
Mike Marshall274dcf52015-07-17 10:38:13 -040098 return "OP_FSYNC";
Yi Liu8bb8aef2015-11-24 15:12:14 -050099 else if (type == ORANGEFS_VFS_OP_FSKEY)
Mike Marshall274dcf52015-07-17 10:38:13 -0400100 return "OP_FSKEY";
Martin Brandenburg482664d2016-08-12 12:02:31 -0400101 else if (type == ORANGEFS_VFS_OP_FEATURES)
102 return "OP_FEATURES";
Mike Marshall274dcf52015-07-17 10:38:13 -0400103 }
104 return "OP_UNKNOWN?";
105}
106
Al Viro78699e22016-02-11 23:07:19 -0500107void orangefs_new_tag(struct orangefs_kernel_op_s *op)
108{
109 spin_lock(&next_tag_value_lock);
110 op->tag = next_tag_value++;
111 if (next_tag_value == 0)
112 next_tag_value = 100;
113 spin_unlock(&next_tag_value_lock);
114}
115
Yi Liu8bb8aef2015-11-24 15:12:14 -0500116struct orangefs_kernel_op_s *op_alloc(__s32 type)
Mike Marshall274dcf52015-07-17 10:38:13 -0400117{
Yi Liu8bb8aef2015-11-24 15:12:14 -0500118 struct orangefs_kernel_op_s *new_op = NULL;
Mike Marshall274dcf52015-07-17 10:38:13 -0400119
Mike Marshall2d4cae02016-02-04 13:48:16 -0500120 new_op = kmem_cache_zalloc(op_cache, GFP_KERNEL);
Mike Marshall274dcf52015-07-17 10:38:13 -0400121 if (new_op) {
Mike Marshall274dcf52015-07-17 10:38:13 -0400122 INIT_LIST_HEAD(&new_op->list);
123 spin_lock_init(&new_op->lock);
Al Virod2d87a32016-02-13 10:15:22 -0500124 init_completion(&new_op->waitq);
Mike Marshall274dcf52015-07-17 10:38:13 -0400125
Al Viro115b93a2016-01-23 14:04:31 -0500126 new_op->upcall.type = ORANGEFS_VFS_OP_INVALID;
127 new_op->downcall.type = ORANGEFS_VFS_OP_INVALID;
128 new_op->downcall.status = -1;
129
130 new_op->op_state = OP_VFS_STATE_UNKNOWN;
Mike Marshall274dcf52015-07-17 10:38:13 -0400131
132 /* initialize the op specific tag and upcall credentials */
Al Viro78699e22016-02-11 23:07:19 -0500133 orangefs_new_tag(new_op);
Mike Marshall274dcf52015-07-17 10:38:13 -0400134 new_op->upcall.type = type;
135 new_op->attempts = 0;
136 gossip_debug(GOSSIP_CACHE_DEBUG,
137 "Alloced OP (%p: %llu %s)\n",
138 new_op,
139 llu(new_op->tag),
140 get_opname_string(new_op));
141
Jann Horn78fee0b2016-06-25 01:51:52 +0200142 new_op->upcall.uid = from_kuid(&init_user_ns,
Mike Marshall274dcf52015-07-17 10:38:13 -0400143 current_fsuid());
144
Jann Horn78fee0b2016-06-25 01:51:52 +0200145 new_op->upcall.gid = from_kgid(&init_user_ns,
Mike Marshall274dcf52015-07-17 10:38:13 -0400146 current_fsgid());
Mike Marshall274dcf52015-07-17 10:38:13 -0400147 } else {
Mike Marshall2d4cae02016-02-04 13:48:16 -0500148 gossip_err("op_alloc: kmem_cache_zalloc failed!\n");
Mike Marshall274dcf52015-07-17 10:38:13 -0400149 }
150 return new_op;
151}
152
Al Viroc1223ca2016-02-18 19:17:51 -0500153void op_release(struct orangefs_kernel_op_s *orangefs_op)
Mike Marshall274dcf52015-07-17 10:38:13 -0400154{
Yi Liu8bb8aef2015-11-24 15:12:14 -0500155 if (orangefs_op) {
Mike Marshall274dcf52015-07-17 10:38:13 -0400156 gossip_debug(GOSSIP_CACHE_DEBUG,
157 "Releasing OP (%p: %llu)\n",
Yi Liu8bb8aef2015-11-24 15:12:14 -0500158 orangefs_op,
159 llu(orangefs_op->tag));
Yi Liu8bb8aef2015-11-24 15:12:14 -0500160 kmem_cache_free(op_cache, orangefs_op);
Mike Marshall274dcf52015-07-17 10:38:13 -0400161 } else {
162 gossip_err("NULL pointer in op_release\n");
163 }
164}