xref: /openbmc/linux/fs/orangefs/orangefs-cache.c (revision b2441318)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2575e9461SMike Marshall /*
3575e9461SMike Marshall  * (C) 2001 Clemson University and The University of Chicago
4575e9461SMike Marshall  *
5575e9461SMike Marshall  * See COPYING in top-level directory.
6575e9461SMike Marshall  */
7575e9461SMike Marshall 
8575e9461SMike Marshall #include "protocol.h"
9575e9461SMike Marshall #include "orangefs-kernel.h"
10575e9461SMike Marshall 
11575e9461SMike Marshall /* tags assigned to kernel upcall operations */
12575e9461SMike Marshall static __u64 next_tag_value;
13575e9461SMike Marshall static DEFINE_SPINLOCK(next_tag_value_lock);
14575e9461SMike Marshall 
15575e9461SMike Marshall /* the orangefs memory caches */
16575e9461SMike Marshall 
17575e9461SMike Marshall /* a cache for orangefs upcall/downcall operations */
18575e9461SMike Marshall static struct kmem_cache *op_cache;
19575e9461SMike Marshall 
op_cache_initialize(void)20575e9461SMike Marshall int op_cache_initialize(void)
21575e9461SMike Marshall {
22575e9461SMike Marshall 	op_cache = kmem_cache_create("orangefs_op_cache",
23575e9461SMike Marshall 				     sizeof(struct orangefs_kernel_op_s),
24575e9461SMike Marshall 				     0,
25575e9461SMike Marshall 				     ORANGEFS_CACHE_CREATE_FLAGS,
26575e9461SMike Marshall 				     NULL);
27575e9461SMike Marshall 
28575e9461SMike Marshall 	if (!op_cache) {
29575e9461SMike Marshall 		gossip_err("Cannot create orangefs_op_cache\n");
30575e9461SMike Marshall 		return -ENOMEM;
31575e9461SMike Marshall 	}
32575e9461SMike Marshall 
33575e9461SMike Marshall 	/* initialize our atomic tag counter */
34575e9461SMike Marshall 	spin_lock(&next_tag_value_lock);
35575e9461SMike Marshall 	next_tag_value = 100;
36575e9461SMike Marshall 	spin_unlock(&next_tag_value_lock);
37575e9461SMike Marshall 	return 0;
38575e9461SMike Marshall }
39575e9461SMike Marshall 
op_cache_finalize(void)40575e9461SMike Marshall int op_cache_finalize(void)
41575e9461SMike Marshall {
42575e9461SMike Marshall 	kmem_cache_destroy(op_cache);
43575e9461SMike Marshall 	return 0;
44575e9461SMike Marshall }
45575e9461SMike Marshall 
get_opname_string(struct orangefs_kernel_op_s * new_op)46575e9461SMike Marshall char *get_opname_string(struct orangefs_kernel_op_s *new_op)
47575e9461SMike Marshall {
48575e9461SMike Marshall 	if (new_op) {
49575e9461SMike Marshall 		__s32 type = new_op->upcall.type;
50575e9461SMike Marshall 
51575e9461SMike Marshall 		if (type == ORANGEFS_VFS_OP_FILE_IO)
52575e9461SMike Marshall 			return "OP_FILE_IO";
53575e9461SMike Marshall 		else if (type == ORANGEFS_VFS_OP_LOOKUP)
54575e9461SMike Marshall 			return "OP_LOOKUP";
55575e9461SMike Marshall 		else if (type == ORANGEFS_VFS_OP_CREATE)
56575e9461SMike Marshall 			return "OP_CREATE";
57575e9461SMike Marshall 		else if (type == ORANGEFS_VFS_OP_GETATTR)
58575e9461SMike Marshall 			return "OP_GETATTR";
59575e9461SMike Marshall 		else if (type == ORANGEFS_VFS_OP_REMOVE)
60575e9461SMike Marshall 			return "OP_REMOVE";
61575e9461SMike Marshall 		else if (type == ORANGEFS_VFS_OP_MKDIR)
62575e9461SMike Marshall 			return "OP_MKDIR";
63575e9461SMike Marshall 		else if (type == ORANGEFS_VFS_OP_READDIR)
64575e9461SMike Marshall 			return "OP_READDIR";
65575e9461SMike Marshall 		else if (type == ORANGEFS_VFS_OP_READDIRPLUS)
66575e9461SMike Marshall 			return "OP_READDIRPLUS";
67575e9461SMike Marshall 		else if (type == ORANGEFS_VFS_OP_SETATTR)
68575e9461SMike Marshall 			return "OP_SETATTR";
69575e9461SMike Marshall 		else if (type == ORANGEFS_VFS_OP_SYMLINK)
70575e9461SMike Marshall 			return "OP_SYMLINK";
71575e9461SMike Marshall 		else if (type == ORANGEFS_VFS_OP_RENAME)
72575e9461SMike Marshall 			return "OP_RENAME";
73575e9461SMike Marshall 		else if (type == ORANGEFS_VFS_OP_STATFS)
74575e9461SMike Marshall 			return "OP_STATFS";
75575e9461SMike Marshall 		else if (type == ORANGEFS_VFS_OP_TRUNCATE)
76575e9461SMike Marshall 			return "OP_TRUNCATE";
776eaff8c7SMartin Brandenburg 		else if (type == ORANGEFS_VFS_OP_RA_FLUSH)
786eaff8c7SMartin Brandenburg 			return "OP_RA_FLUSH";
79575e9461SMike Marshall 		else if (type == ORANGEFS_VFS_OP_FS_MOUNT)
80575e9461SMike Marshall 			return "OP_FS_MOUNT";
81575e9461SMike Marshall 		else if (type == ORANGEFS_VFS_OP_FS_UMOUNT)
82575e9461SMike Marshall 			return "OP_FS_UMOUNT";
83575e9461SMike Marshall 		else if (type == ORANGEFS_VFS_OP_GETXATTR)
84575e9461SMike Marshall 			return "OP_GETXATTR";
85575e9461SMike Marshall 		else if (type == ORANGEFS_VFS_OP_SETXATTR)
86575e9461SMike Marshall 			return "OP_SETXATTR";
87575e9461SMike Marshall 		else if (type == ORANGEFS_VFS_OP_LISTXATTR)
88575e9461SMike Marshall 			return "OP_LISTXATTR";
89575e9461SMike Marshall 		else if (type == ORANGEFS_VFS_OP_REMOVEXATTR)
90575e9461SMike Marshall 			return "OP_REMOVEXATTR";
91575e9461SMike Marshall 		else if (type == ORANGEFS_VFS_OP_PARAM)
92575e9461SMike Marshall 			return "OP_PARAM";
93575e9461SMike Marshall 		else if (type == ORANGEFS_VFS_OP_PERF_COUNT)
94575e9461SMike Marshall 			return "OP_PERF_COUNT";
95575e9461SMike Marshall 		else if (type == ORANGEFS_VFS_OP_CANCEL)
96575e9461SMike Marshall 			return "OP_CANCEL";
97575e9461SMike Marshall 		else if (type == ORANGEFS_VFS_OP_FSYNC)
98575e9461SMike Marshall 			return "OP_FSYNC";
99575e9461SMike Marshall 		else if (type == ORANGEFS_VFS_OP_FSKEY)
100575e9461SMike Marshall 			return "OP_FSKEY";
101482664ddSMartin Brandenburg 		else if (type == ORANGEFS_VFS_OP_FEATURES)
102482664ddSMartin Brandenburg 			return "OP_FEATURES";
103575e9461SMike Marshall 	}
104575e9461SMike Marshall 	return "OP_UNKNOWN?";
105575e9461SMike Marshall }
106575e9461SMike Marshall 
orangefs_new_tag(struct orangefs_kernel_op_s * op)10778699e29SAl Viro void orangefs_new_tag(struct orangefs_kernel_op_s *op)
10878699e29SAl Viro {
10978699e29SAl Viro 	spin_lock(&next_tag_value_lock);
11078699e29SAl Viro 	op->tag = next_tag_value++;
11178699e29SAl Viro 	if (next_tag_value == 0)
11278699e29SAl Viro 		next_tag_value = 100;
11378699e29SAl Viro 	spin_unlock(&next_tag_value_lock);
11478699e29SAl Viro }
11578699e29SAl Viro 
op_alloc(__s32 type)116575e9461SMike Marshall struct orangefs_kernel_op_s *op_alloc(__s32 type)
117575e9461SMike Marshall {
118575e9461SMike Marshall 	struct orangefs_kernel_op_s *new_op = NULL;
119575e9461SMike Marshall 
1202d4cae0dSMike Marshall 	new_op = kmem_cache_zalloc(op_cache, GFP_KERNEL);
121575e9461SMike Marshall 	if (new_op) {
122575e9461SMike Marshall 		INIT_LIST_HEAD(&new_op->list);
123575e9461SMike Marshall 		spin_lock_init(&new_op->lock);
124d2d87a3bSAl Viro 		init_completion(&new_op->waitq);
125575e9461SMike Marshall 
126115b93a8SAl Viro 		new_op->upcall.type = ORANGEFS_VFS_OP_INVALID;
127115b93a8SAl Viro 		new_op->downcall.type = ORANGEFS_VFS_OP_INVALID;
128115b93a8SAl Viro 		new_op->downcall.status = -1;
129115b93a8SAl Viro 
130115b93a8SAl Viro 		new_op->op_state = OP_VFS_STATE_UNKNOWN;
131575e9461SMike Marshall 
132575e9461SMike Marshall 		/* initialize the op specific tag and upcall credentials */
13378699e29SAl Viro 		orangefs_new_tag(new_op);
134575e9461SMike Marshall 		new_op->upcall.type = type;
135575e9461SMike Marshall 		new_op->attempts = 0;
136575e9461SMike Marshall 		gossip_debug(GOSSIP_CACHE_DEBUG,
137575e9461SMike Marshall 			     "Alloced OP (%p: %llu %s)\n",
138575e9461SMike Marshall 			     new_op,
139575e9461SMike Marshall 			     llu(new_op->tag),
140575e9461SMike Marshall 			     get_opname_string(new_op));
141575e9461SMike Marshall 
14278fee0b6SJann Horn 		new_op->upcall.uid = from_kuid(&init_user_ns,
143575e9461SMike Marshall 					       current_fsuid());
144575e9461SMike Marshall 
14578fee0b6SJann Horn 		new_op->upcall.gid = from_kgid(&init_user_ns,
146575e9461SMike Marshall 					       current_fsgid());
147575e9461SMike Marshall 	} else {
1482d4cae0dSMike Marshall 		gossip_err("op_alloc: kmem_cache_zalloc failed!\n");
149575e9461SMike Marshall 	}
150575e9461SMike Marshall 	return new_op;
151575e9461SMike Marshall }
152575e9461SMike Marshall 
op_release(struct orangefs_kernel_op_s * orangefs_op)153c1223ca4SAl Viro void op_release(struct orangefs_kernel_op_s *orangefs_op)
154575e9461SMike Marshall {
155575e9461SMike Marshall 	if (orangefs_op) {
156575e9461SMike Marshall 		gossip_debug(GOSSIP_CACHE_DEBUG,
157575e9461SMike Marshall 			     "Releasing OP (%p: %llu)\n",
158575e9461SMike Marshall 			     orangefs_op,
159575e9461SMike Marshall 			     llu(orangefs_op->tag));
160575e9461SMike Marshall 		kmem_cache_free(op_cache, orangefs_op);
161575e9461SMike Marshall 	} else {
162575e9461SMike Marshall 		gossip_err("NULL pointer in op_release\n");
163575e9461SMike Marshall 	}
164575e9461SMike Marshall }
165