blob: e72ac2083ac00fb8a22df1ed352aac8d9fe46113 [file] [log] [blame]
Mike Marshall274dcf52015-07-17 10:38:13 -04001/*
2 * (C) 2001 Clemson University and The University of Chicago
3 *
4 * See COPYING in top-level directory.
5 */
6
7#include "protocol.h"
Mike Marshall575e9462015-12-04 12:56:14 -05008#include "orangefs-kernel.h"
Mike Marshall274dcf52015-07-17 10:38:13 -04009
10/* tags assigned to kernel upcall operations */
11static __u64 next_tag_value;
12static DEFINE_SPINLOCK(next_tag_value_lock);
13
Yi Liu8bb8aef2015-11-24 15:12:14 -050014/* the orangefs memory caches */
Mike Marshall274dcf52015-07-17 10:38:13 -040015
Yi Liu8bb8aef2015-11-24 15:12:14 -050016/* a cache for orangefs upcall/downcall operations */
Mike Marshall274dcf52015-07-17 10:38:13 -040017static struct kmem_cache *op_cache;
18
19/* a cache for device (/dev/pvfs2-req) communication */
20static struct kmem_cache *dev_req_cache;
21
Yi Liu8bb8aef2015-11-24 15:12:14 -050022/* a cache for orangefs_kiocb objects (i.e orangefs iocb structures ) */
23static struct kmem_cache *orangefs_kiocb_cache;
Mike Marshall274dcf52015-07-17 10:38:13 -040024
25int op_cache_initialize(void)
26{
Yi Liu8bb8aef2015-11-24 15:12:14 -050027 op_cache = kmem_cache_create("orangefs_op_cache",
28 sizeof(struct orangefs_kernel_op_s),
Mike Marshall274dcf52015-07-17 10:38:13 -040029 0,
Yi Liu8bb8aef2015-11-24 15:12:14 -050030 ORANGEFS_CACHE_CREATE_FLAGS,
Mike Marshall274dcf52015-07-17 10:38:13 -040031 NULL);
32
33 if (!op_cache) {
Yi Liu8bb8aef2015-11-24 15:12:14 -050034 gossip_err("Cannot create orangefs_op_cache\n");
Mike Marshall274dcf52015-07-17 10:38:13 -040035 return -ENOMEM;
36 }
37
38 /* initialize our atomic tag counter */
39 spin_lock(&next_tag_value_lock);
40 next_tag_value = 100;
41 spin_unlock(&next_tag_value_lock);
42 return 0;
43}
44
45int op_cache_finalize(void)
46{
47 kmem_cache_destroy(op_cache);
48 return 0;
49}
50
Yi Liu8bb8aef2015-11-24 15:12:14 -050051char *get_opname_string(struct orangefs_kernel_op_s *new_op)
Mike Marshall274dcf52015-07-17 10:38:13 -040052{
53 if (new_op) {
54 __s32 type = new_op->upcall.type;
55
Yi Liu8bb8aef2015-11-24 15:12:14 -050056 if (type == ORANGEFS_VFS_OP_FILE_IO)
Mike Marshall274dcf52015-07-17 10:38:13 -040057 return "OP_FILE_IO";
Yi Liu8bb8aef2015-11-24 15:12:14 -050058 else if (type == ORANGEFS_VFS_OP_LOOKUP)
Mike Marshall274dcf52015-07-17 10:38:13 -040059 return "OP_LOOKUP";
Yi Liu8bb8aef2015-11-24 15:12:14 -050060 else if (type == ORANGEFS_VFS_OP_CREATE)
Mike Marshall274dcf52015-07-17 10:38:13 -040061 return "OP_CREATE";
Yi Liu8bb8aef2015-11-24 15:12:14 -050062 else if (type == ORANGEFS_VFS_OP_GETATTR)
Mike Marshall274dcf52015-07-17 10:38:13 -040063 return "OP_GETATTR";
Yi Liu8bb8aef2015-11-24 15:12:14 -050064 else if (type == ORANGEFS_VFS_OP_REMOVE)
Mike Marshall274dcf52015-07-17 10:38:13 -040065 return "OP_REMOVE";
Yi Liu8bb8aef2015-11-24 15:12:14 -050066 else if (type == ORANGEFS_VFS_OP_MKDIR)
Mike Marshall274dcf52015-07-17 10:38:13 -040067 return "OP_MKDIR";
Yi Liu8bb8aef2015-11-24 15:12:14 -050068 else if (type == ORANGEFS_VFS_OP_READDIR)
Mike Marshall274dcf52015-07-17 10:38:13 -040069 return "OP_READDIR";
Yi Liu8bb8aef2015-11-24 15:12:14 -050070 else if (type == ORANGEFS_VFS_OP_READDIRPLUS)
Mike Marshall274dcf52015-07-17 10:38:13 -040071 return "OP_READDIRPLUS";
Yi Liu8bb8aef2015-11-24 15:12:14 -050072 else if (type == ORANGEFS_VFS_OP_SETATTR)
Mike Marshall274dcf52015-07-17 10:38:13 -040073 return "OP_SETATTR";
Yi Liu8bb8aef2015-11-24 15:12:14 -050074 else if (type == ORANGEFS_VFS_OP_SYMLINK)
Mike Marshall274dcf52015-07-17 10:38:13 -040075 return "OP_SYMLINK";
Yi Liu8bb8aef2015-11-24 15:12:14 -050076 else if (type == ORANGEFS_VFS_OP_RENAME)
Mike Marshall274dcf52015-07-17 10:38:13 -040077 return "OP_RENAME";
Yi Liu8bb8aef2015-11-24 15:12:14 -050078 else if (type == ORANGEFS_VFS_OP_STATFS)
Mike Marshall274dcf52015-07-17 10:38:13 -040079 return "OP_STATFS";
Yi Liu8bb8aef2015-11-24 15:12:14 -050080 else if (type == ORANGEFS_VFS_OP_TRUNCATE)
Mike Marshall274dcf52015-07-17 10:38:13 -040081 return "OP_TRUNCATE";
Yi Liu8bb8aef2015-11-24 15:12:14 -050082 else if (type == ORANGEFS_VFS_OP_MMAP_RA_FLUSH)
Mike Marshall274dcf52015-07-17 10:38:13 -040083 return "OP_MMAP_RA_FLUSH";
Yi Liu8bb8aef2015-11-24 15:12:14 -050084 else if (type == ORANGEFS_VFS_OP_FS_MOUNT)
Mike Marshall274dcf52015-07-17 10:38:13 -040085 return "OP_FS_MOUNT";
Yi Liu8bb8aef2015-11-24 15:12:14 -050086 else if (type == ORANGEFS_VFS_OP_FS_UMOUNT)
Mike Marshall274dcf52015-07-17 10:38:13 -040087 return "OP_FS_UMOUNT";
Yi Liu8bb8aef2015-11-24 15:12:14 -050088 else if (type == ORANGEFS_VFS_OP_GETXATTR)
Mike Marshall274dcf52015-07-17 10:38:13 -040089 return "OP_GETXATTR";
Yi Liu8bb8aef2015-11-24 15:12:14 -050090 else if (type == ORANGEFS_VFS_OP_SETXATTR)
Mike Marshall274dcf52015-07-17 10:38:13 -040091 return "OP_SETXATTR";
Yi Liu8bb8aef2015-11-24 15:12:14 -050092 else if (type == ORANGEFS_VFS_OP_LISTXATTR)
Mike Marshall274dcf52015-07-17 10:38:13 -040093 return "OP_LISTXATTR";
Yi Liu8bb8aef2015-11-24 15:12:14 -050094 else if (type == ORANGEFS_VFS_OP_REMOVEXATTR)
Mike Marshall274dcf52015-07-17 10:38:13 -040095 return "OP_REMOVEXATTR";
Yi Liu8bb8aef2015-11-24 15:12:14 -050096 else if (type == ORANGEFS_VFS_OP_PARAM)
Mike Marshall274dcf52015-07-17 10:38:13 -040097 return "OP_PARAM";
Yi Liu8bb8aef2015-11-24 15:12:14 -050098 else if (type == ORANGEFS_VFS_OP_PERF_COUNT)
Mike Marshall274dcf52015-07-17 10:38:13 -040099 return "OP_PERF_COUNT";
Yi Liu8bb8aef2015-11-24 15:12:14 -0500100 else if (type == ORANGEFS_VFS_OP_CANCEL)
Mike Marshall274dcf52015-07-17 10:38:13 -0400101 return "OP_CANCEL";
Yi Liu8bb8aef2015-11-24 15:12:14 -0500102 else if (type == ORANGEFS_VFS_OP_FSYNC)
Mike Marshall274dcf52015-07-17 10:38:13 -0400103 return "OP_FSYNC";
Yi Liu8bb8aef2015-11-24 15:12:14 -0500104 else if (type == ORANGEFS_VFS_OP_FSKEY)
Mike Marshall274dcf52015-07-17 10:38:13 -0400105 return "OP_FSKEY";
Mike Marshall274dcf52015-07-17 10:38:13 -0400106 }
107 return "OP_UNKNOWN?";
108}
109
Yi Liu8bb8aef2015-11-24 15:12:14 -0500110struct orangefs_kernel_op_s *op_alloc(__s32 type)
Mike Marshall274dcf52015-07-17 10:38:13 -0400111{
Yi Liu8bb8aef2015-11-24 15:12:14 -0500112 struct orangefs_kernel_op_s *new_op = NULL;
Mike Marshall274dcf52015-07-17 10:38:13 -0400113
Al Viro115b93a2016-01-23 14:04:31 -0500114 new_op = kmem_cache_zalloc(op_cache, ORANGEFS_CACHE_ALLOC_FLAGS);
Mike Marshall274dcf52015-07-17 10:38:13 -0400115 if (new_op) {
Mike Marshall274dcf52015-07-17 10:38:13 -0400116 INIT_LIST_HEAD(&new_op->list);
117 spin_lock_init(&new_op->lock);
118 init_waitqueue_head(&new_op->waitq);
119
Al Viroed42fe02016-01-22 19:47:47 -0500120 atomic_set(&new_op->ref_count, 1);
Mike Marshall274dcf52015-07-17 10:38:13 -0400121
Al Viro115b93a2016-01-23 14:04:31 -0500122 init_completion(&new_op->done);
123
124 new_op->upcall.type = ORANGEFS_VFS_OP_INVALID;
125 new_op->downcall.type = ORANGEFS_VFS_OP_INVALID;
126 new_op->downcall.status = -1;
127
128 new_op->op_state = OP_VFS_STATE_UNKNOWN;
129 new_op->tag = 0;
Mike Marshall274dcf52015-07-17 10:38:13 -0400130
131 /* initialize the op specific tag and upcall credentials */
132 spin_lock(&next_tag_value_lock);
133 new_op->tag = next_tag_value++;
134 if (next_tag_value == 0)
135 next_tag_value = 100;
136 spin_unlock(&next_tag_value_lock);
137 new_op->upcall.type = type;
138 new_op->attempts = 0;
139 gossip_debug(GOSSIP_CACHE_DEBUG,
140 "Alloced OP (%p: %llu %s)\n",
141 new_op,
142 llu(new_op->tag),
143 get_opname_string(new_op));
144
145 new_op->upcall.uid = from_kuid(current_user_ns(),
146 current_fsuid());
147
148 new_op->upcall.gid = from_kgid(current_user_ns(),
149 current_fsgid());
Mike Marshall274dcf52015-07-17 10:38:13 -0400150 } else {
151 gossip_err("op_alloc: kmem_cache_alloc failed!\n");
152 }
153 return new_op;
154}
155
Al Viroed42fe02016-01-22 19:47:47 -0500156void __op_release(struct orangefs_kernel_op_s *orangefs_op)
Mike Marshall274dcf52015-07-17 10:38:13 -0400157{
Yi Liu8bb8aef2015-11-24 15:12:14 -0500158 if (orangefs_op) {
Mike Marshall274dcf52015-07-17 10:38:13 -0400159 gossip_debug(GOSSIP_CACHE_DEBUG,
160 "Releasing OP (%p: %llu)\n",
Yi Liu8bb8aef2015-11-24 15:12:14 -0500161 orangefs_op,
162 llu(orangefs_op->tag));
Yi Liu8bb8aef2015-11-24 15:12:14 -0500163 kmem_cache_free(op_cache, orangefs_op);
Mike Marshall274dcf52015-07-17 10:38:13 -0400164 } else {
165 gossip_err("NULL pointer in op_release\n");
166 }
167}
168
169int dev_req_cache_initialize(void)
170{
Yi Liu8bb8aef2015-11-24 15:12:14 -0500171 dev_req_cache = kmem_cache_create("orangefs_devreqcache",
Martin Brandenburga762ae62015-12-15 14:22:06 -0500172 MAX_DEV_REQ_DOWNSIZE,
Mike Marshall274dcf52015-07-17 10:38:13 -0400173 0,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500174 ORANGEFS_CACHE_CREATE_FLAGS,
Mike Marshall274dcf52015-07-17 10:38:13 -0400175 NULL);
176
177 if (!dev_req_cache) {
Yi Liu8bb8aef2015-11-24 15:12:14 -0500178 gossip_err("Cannot create orangefs_dev_req_cache\n");
Mike Marshall274dcf52015-07-17 10:38:13 -0400179 return -ENOMEM;
180 }
181 return 0;
182}
183
184int dev_req_cache_finalize(void)
185{
186 kmem_cache_destroy(dev_req_cache);
187 return 0;
188}
189
190void *dev_req_alloc(void)
191{
192 void *buffer;
193
Yi Liu8bb8aef2015-11-24 15:12:14 -0500194 buffer = kmem_cache_alloc(dev_req_cache, ORANGEFS_CACHE_ALLOC_FLAGS);
Mike Marshall274dcf52015-07-17 10:38:13 -0400195 if (buffer == NULL)
196 gossip_err("Failed to allocate from dev_req_cache\n");
197 else
Martin Brandenburga762ae62015-12-15 14:22:06 -0500198 memset(buffer, 0, sizeof(MAX_DEV_REQ_DOWNSIZE));
Mike Marshall274dcf52015-07-17 10:38:13 -0400199 return buffer;
200}
201
202void dev_req_release(void *buffer)
203{
204 if (buffer)
205 kmem_cache_free(dev_req_cache, buffer);
206 else
207 gossip_err("NULL pointer passed to dev_req_release\n");
208}
209
210int kiocb_cache_initialize(void)
211{
Yi Liu8bb8aef2015-11-24 15:12:14 -0500212 orangefs_kiocb_cache = kmem_cache_create("orangefs_kiocbcache",
213 sizeof(struct orangefs_kiocb_s),
Mike Marshall274dcf52015-07-17 10:38:13 -0400214 0,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500215 ORANGEFS_CACHE_CREATE_FLAGS,
Mike Marshall274dcf52015-07-17 10:38:13 -0400216 NULL);
217
Yi Liu8bb8aef2015-11-24 15:12:14 -0500218 if (!orangefs_kiocb_cache) {
219 gossip_err("Cannot create orangefs_kiocb_cache!\n");
Mike Marshall274dcf52015-07-17 10:38:13 -0400220 return -ENOMEM;
221 }
222 return 0;
223}
224
225int kiocb_cache_finalize(void)
226{
Yi Liu8bb8aef2015-11-24 15:12:14 -0500227 kmem_cache_destroy(orangefs_kiocb_cache);
Mike Marshall274dcf52015-07-17 10:38:13 -0400228 return 0;
229}
230
Yi Liu8bb8aef2015-11-24 15:12:14 -0500231struct orangefs_kiocb_s *kiocb_alloc(void)
Mike Marshall274dcf52015-07-17 10:38:13 -0400232{
Yi Liu8bb8aef2015-11-24 15:12:14 -0500233 struct orangefs_kiocb_s *x = NULL;
Mike Marshall274dcf52015-07-17 10:38:13 -0400234
Yi Liu8bb8aef2015-11-24 15:12:14 -0500235 x = kmem_cache_alloc(orangefs_kiocb_cache, ORANGEFS_CACHE_ALLOC_FLAGS);
Mike Marshall274dcf52015-07-17 10:38:13 -0400236 if (x == NULL)
237 gossip_err("kiocb_alloc: kmem_cache_alloc failed!\n");
238 else
Yi Liu8bb8aef2015-11-24 15:12:14 -0500239 memset(x, 0, sizeof(struct orangefs_kiocb_s));
Mike Marshall274dcf52015-07-17 10:38:13 -0400240 return x;
241}
242
Yi Liu8bb8aef2015-11-24 15:12:14 -0500243void kiocb_release(struct orangefs_kiocb_s *x)
Mike Marshall274dcf52015-07-17 10:38:13 -0400244{
245 if (x)
Yi Liu8bb8aef2015-11-24 15:12:14 -0500246 kmem_cache_free(orangefs_kiocb_cache, x);
Mike Marshall274dcf52015-07-17 10:38:13 -0400247 else
248 gossip_err("kiocb_release: kmem_cache_free NULL pointer!\n");
249}