blob: aa3830b741c78d9e14459895413600233e5796f4 [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
Mike Marshall274dcf52015-07-17 10:38:13 -040019int op_cache_initialize(void)
20{
Yi Liu8bb8aef2015-11-24 15:12:14 -050021 op_cache = kmem_cache_create("orangefs_op_cache",
22 sizeof(struct orangefs_kernel_op_s),
Mike Marshall274dcf52015-07-17 10:38:13 -040023 0,
Yi Liu8bb8aef2015-11-24 15:12:14 -050024 ORANGEFS_CACHE_CREATE_FLAGS,
Mike Marshall274dcf52015-07-17 10:38:13 -040025 NULL);
26
27 if (!op_cache) {
Yi Liu8bb8aef2015-11-24 15:12:14 -050028 gossip_err("Cannot create orangefs_op_cache\n");
Mike Marshall274dcf52015-07-17 10:38:13 -040029 return -ENOMEM;
30 }
31
32 /* initialize our atomic tag counter */
33 spin_lock(&next_tag_value_lock);
34 next_tag_value = 100;
35 spin_unlock(&next_tag_value_lock);
36 return 0;
37}
38
39int op_cache_finalize(void)
40{
41 kmem_cache_destroy(op_cache);
42 return 0;
43}
44
Yi Liu8bb8aef2015-11-24 15:12:14 -050045char *get_opname_string(struct orangefs_kernel_op_s *new_op)
Mike Marshall274dcf52015-07-17 10:38:13 -040046{
47 if (new_op) {
48 __s32 type = new_op->upcall.type;
49
Yi Liu8bb8aef2015-11-24 15:12:14 -050050 if (type == ORANGEFS_VFS_OP_FILE_IO)
Mike Marshall274dcf52015-07-17 10:38:13 -040051 return "OP_FILE_IO";
Yi Liu8bb8aef2015-11-24 15:12:14 -050052 else if (type == ORANGEFS_VFS_OP_LOOKUP)
Mike Marshall274dcf52015-07-17 10:38:13 -040053 return "OP_LOOKUP";
Yi Liu8bb8aef2015-11-24 15:12:14 -050054 else if (type == ORANGEFS_VFS_OP_CREATE)
Mike Marshall274dcf52015-07-17 10:38:13 -040055 return "OP_CREATE";
Yi Liu8bb8aef2015-11-24 15:12:14 -050056 else if (type == ORANGEFS_VFS_OP_GETATTR)
Mike Marshall274dcf52015-07-17 10:38:13 -040057 return "OP_GETATTR";
Yi Liu8bb8aef2015-11-24 15:12:14 -050058 else if (type == ORANGEFS_VFS_OP_REMOVE)
Mike Marshall274dcf52015-07-17 10:38:13 -040059 return "OP_REMOVE";
Yi Liu8bb8aef2015-11-24 15:12:14 -050060 else if (type == ORANGEFS_VFS_OP_MKDIR)
Mike Marshall274dcf52015-07-17 10:38:13 -040061 return "OP_MKDIR";
Yi Liu8bb8aef2015-11-24 15:12:14 -050062 else if (type == ORANGEFS_VFS_OP_READDIR)
Mike Marshall274dcf52015-07-17 10:38:13 -040063 return "OP_READDIR";
Yi Liu8bb8aef2015-11-24 15:12:14 -050064 else if (type == ORANGEFS_VFS_OP_READDIRPLUS)
Mike Marshall274dcf52015-07-17 10:38:13 -040065 return "OP_READDIRPLUS";
Yi Liu8bb8aef2015-11-24 15:12:14 -050066 else if (type == ORANGEFS_VFS_OP_SETATTR)
Mike Marshall274dcf52015-07-17 10:38:13 -040067 return "OP_SETATTR";
Yi Liu8bb8aef2015-11-24 15:12:14 -050068 else if (type == ORANGEFS_VFS_OP_SYMLINK)
Mike Marshall274dcf52015-07-17 10:38:13 -040069 return "OP_SYMLINK";
Yi Liu8bb8aef2015-11-24 15:12:14 -050070 else if (type == ORANGEFS_VFS_OP_RENAME)
Mike Marshall274dcf52015-07-17 10:38:13 -040071 return "OP_RENAME";
Yi Liu8bb8aef2015-11-24 15:12:14 -050072 else if (type == ORANGEFS_VFS_OP_STATFS)
Mike Marshall274dcf52015-07-17 10:38:13 -040073 return "OP_STATFS";
Yi Liu8bb8aef2015-11-24 15:12:14 -050074 else if (type == ORANGEFS_VFS_OP_TRUNCATE)
Mike Marshall274dcf52015-07-17 10:38:13 -040075 return "OP_TRUNCATE";
Martin Brandenburg6eaff8c2016-08-02 14:31:05 -040076 else if (type == ORANGEFS_VFS_OP_RA_FLUSH)
77 return "OP_RA_FLUSH";
Yi Liu8bb8aef2015-11-24 15:12:14 -050078 else if (type == ORANGEFS_VFS_OP_FS_MOUNT)
Mike Marshall274dcf52015-07-17 10:38:13 -040079 return "OP_FS_MOUNT";
Yi Liu8bb8aef2015-11-24 15:12:14 -050080 else if (type == ORANGEFS_VFS_OP_FS_UMOUNT)
Mike Marshall274dcf52015-07-17 10:38:13 -040081 return "OP_FS_UMOUNT";
Yi Liu8bb8aef2015-11-24 15:12:14 -050082 else if (type == ORANGEFS_VFS_OP_GETXATTR)
Mike Marshall274dcf52015-07-17 10:38:13 -040083 return "OP_GETXATTR";
Yi Liu8bb8aef2015-11-24 15:12:14 -050084 else if (type == ORANGEFS_VFS_OP_SETXATTR)
Mike Marshall274dcf52015-07-17 10:38:13 -040085 return "OP_SETXATTR";
Yi Liu8bb8aef2015-11-24 15:12:14 -050086 else if (type == ORANGEFS_VFS_OP_LISTXATTR)
Mike Marshall274dcf52015-07-17 10:38:13 -040087 return "OP_LISTXATTR";
Yi Liu8bb8aef2015-11-24 15:12:14 -050088 else if (type == ORANGEFS_VFS_OP_REMOVEXATTR)
Mike Marshall274dcf52015-07-17 10:38:13 -040089 return "OP_REMOVEXATTR";
Yi Liu8bb8aef2015-11-24 15:12:14 -050090 else if (type == ORANGEFS_VFS_OP_PARAM)
Mike Marshall274dcf52015-07-17 10:38:13 -040091 return "OP_PARAM";
Yi Liu8bb8aef2015-11-24 15:12:14 -050092 else if (type == ORANGEFS_VFS_OP_PERF_COUNT)
Mike Marshall274dcf52015-07-17 10:38:13 -040093 return "OP_PERF_COUNT";
Yi Liu8bb8aef2015-11-24 15:12:14 -050094 else if (type == ORANGEFS_VFS_OP_CANCEL)
Mike Marshall274dcf52015-07-17 10:38:13 -040095 return "OP_CANCEL";
Yi Liu8bb8aef2015-11-24 15:12:14 -050096 else if (type == ORANGEFS_VFS_OP_FSYNC)
Mike Marshall274dcf52015-07-17 10:38:13 -040097 return "OP_FSYNC";
Yi Liu8bb8aef2015-11-24 15:12:14 -050098 else if (type == ORANGEFS_VFS_OP_FSKEY)
Mike Marshall274dcf52015-07-17 10:38:13 -040099 return "OP_FSKEY";
Martin Brandenburg482664d2016-08-12 12:02:31 -0400100 else if (type == ORANGEFS_VFS_OP_FEATURES)
101 return "OP_FEATURES";
Mike Marshall274dcf52015-07-17 10:38:13 -0400102 }
103 return "OP_UNKNOWN?";
104}
105
Al Viro78699e22016-02-11 23:07:19 -0500106void orangefs_new_tag(struct orangefs_kernel_op_s *op)
107{
108 spin_lock(&next_tag_value_lock);
109 op->tag = next_tag_value++;
110 if (next_tag_value == 0)
111 next_tag_value = 100;
112 spin_unlock(&next_tag_value_lock);
113}
114
Yi Liu8bb8aef2015-11-24 15:12:14 -0500115struct orangefs_kernel_op_s *op_alloc(__s32 type)
Mike Marshall274dcf52015-07-17 10:38:13 -0400116{
Yi Liu8bb8aef2015-11-24 15:12:14 -0500117 struct orangefs_kernel_op_s *new_op = NULL;
Mike Marshall274dcf52015-07-17 10:38:13 -0400118
Mike Marshall2d4cae02016-02-04 13:48:16 -0500119 new_op = kmem_cache_zalloc(op_cache, GFP_KERNEL);
Mike Marshall274dcf52015-07-17 10:38:13 -0400120 if (new_op) {
Mike Marshall274dcf52015-07-17 10:38:13 -0400121 INIT_LIST_HEAD(&new_op->list);
122 spin_lock_init(&new_op->lock);
Al Virod2d87a32016-02-13 10:15:22 -0500123 init_completion(&new_op->waitq);
Mike Marshall274dcf52015-07-17 10:38:13 -0400124
Al Viro115b93a2016-01-23 14:04:31 -0500125 new_op->upcall.type = ORANGEFS_VFS_OP_INVALID;
126 new_op->downcall.type = ORANGEFS_VFS_OP_INVALID;
127 new_op->downcall.status = -1;
128
129 new_op->op_state = OP_VFS_STATE_UNKNOWN;
Mike Marshall274dcf52015-07-17 10:38:13 -0400130
131 /* initialize the op specific tag and upcall credentials */
Al Viro78699e22016-02-11 23:07:19 -0500132 orangefs_new_tag(new_op);
Mike Marshall274dcf52015-07-17 10:38:13 -0400133 new_op->upcall.type = type;
134 new_op->attempts = 0;
135 gossip_debug(GOSSIP_CACHE_DEBUG,
136 "Alloced OP (%p: %llu %s)\n",
137 new_op,
138 llu(new_op->tag),
139 get_opname_string(new_op));
140
Jann Horn78fee0b2016-06-25 01:51:52 +0200141 new_op->upcall.uid = from_kuid(&init_user_ns,
Mike Marshall274dcf52015-07-17 10:38:13 -0400142 current_fsuid());
143
Jann Horn78fee0b2016-06-25 01:51:52 +0200144 new_op->upcall.gid = from_kgid(&init_user_ns,
Mike Marshall274dcf52015-07-17 10:38:13 -0400145 current_fsgid());
Mike Marshall274dcf52015-07-17 10:38:13 -0400146 } else {
Mike Marshall2d4cae02016-02-04 13:48:16 -0500147 gossip_err("op_alloc: kmem_cache_zalloc failed!\n");
Mike Marshall274dcf52015-07-17 10:38:13 -0400148 }
149 return new_op;
150}
151
Al Viroc1223ca2016-02-18 19:17:51 -0500152void op_release(struct orangefs_kernel_op_s *orangefs_op)
Mike Marshall274dcf52015-07-17 10:38:13 -0400153{
Yi Liu8bb8aef2015-11-24 15:12:14 -0500154 if (orangefs_op) {
Mike Marshall274dcf52015-07-17 10:38:13 -0400155 gossip_debug(GOSSIP_CACHE_DEBUG,
156 "Releasing OP (%p: %llu)\n",
Yi Liu8bb8aef2015-11-24 15:12:14 -0500157 orangefs_op,
158 llu(orangefs_op->tag));
Yi Liu8bb8aef2015-11-24 15:12:14 -0500159 kmem_cache_free(op_cache, orangefs_op);
Mike Marshall274dcf52015-07-17 10:38:13 -0400160 } else {
161 gossip_err("NULL pointer in op_release\n");
162 }
163}