Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 1 | /* |
| 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 Marshall | 575e946 | 2015-12-04 12:56:14 -0500 | [diff] [blame^] | 8 | #include "orangefs-kernel.h" |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 9 | |
| 10 | /* tags assigned to kernel upcall operations */ |
| 11 | static __u64 next_tag_value; |
| 12 | static DEFINE_SPINLOCK(next_tag_value_lock); |
| 13 | |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 14 | /* the orangefs memory caches */ |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 15 | |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 16 | /* a cache for orangefs upcall/downcall operations */ |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 17 | static struct kmem_cache *op_cache; |
| 18 | |
| 19 | /* a cache for device (/dev/pvfs2-req) communication */ |
| 20 | static struct kmem_cache *dev_req_cache; |
| 21 | |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 22 | /* a cache for orangefs_kiocb objects (i.e orangefs iocb structures ) */ |
| 23 | static struct kmem_cache *orangefs_kiocb_cache; |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 24 | |
| 25 | int op_cache_initialize(void) |
| 26 | { |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 27 | op_cache = kmem_cache_create("orangefs_op_cache", |
| 28 | sizeof(struct orangefs_kernel_op_s), |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 29 | 0, |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 30 | ORANGEFS_CACHE_CREATE_FLAGS, |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 31 | NULL); |
| 32 | |
| 33 | if (!op_cache) { |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 34 | gossip_err("Cannot create orangefs_op_cache\n"); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 35 | 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 | |
| 45 | int op_cache_finalize(void) |
| 46 | { |
| 47 | kmem_cache_destroy(op_cache); |
| 48 | return 0; |
| 49 | } |
| 50 | |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 51 | char *get_opname_string(struct orangefs_kernel_op_s *new_op) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 52 | { |
| 53 | if (new_op) { |
| 54 | __s32 type = new_op->upcall.type; |
| 55 | |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 56 | if (type == ORANGEFS_VFS_OP_FILE_IO) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 57 | return "OP_FILE_IO"; |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 58 | else if (type == ORANGEFS_VFS_OP_LOOKUP) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 59 | return "OP_LOOKUP"; |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 60 | else if (type == ORANGEFS_VFS_OP_CREATE) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 61 | return "OP_CREATE"; |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 62 | else if (type == ORANGEFS_VFS_OP_GETATTR) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 63 | return "OP_GETATTR"; |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 64 | else if (type == ORANGEFS_VFS_OP_REMOVE) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 65 | return "OP_REMOVE"; |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 66 | else if (type == ORANGEFS_VFS_OP_MKDIR) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 67 | return "OP_MKDIR"; |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 68 | else if (type == ORANGEFS_VFS_OP_READDIR) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 69 | return "OP_READDIR"; |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 70 | else if (type == ORANGEFS_VFS_OP_READDIRPLUS) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 71 | return "OP_READDIRPLUS"; |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 72 | else if (type == ORANGEFS_VFS_OP_SETATTR) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 73 | return "OP_SETATTR"; |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 74 | else if (type == ORANGEFS_VFS_OP_SYMLINK) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 75 | return "OP_SYMLINK"; |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 76 | else if (type == ORANGEFS_VFS_OP_RENAME) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 77 | return "OP_RENAME"; |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 78 | else if (type == ORANGEFS_VFS_OP_STATFS) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 79 | return "OP_STATFS"; |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 80 | else if (type == ORANGEFS_VFS_OP_TRUNCATE) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 81 | return "OP_TRUNCATE"; |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 82 | else if (type == ORANGEFS_VFS_OP_MMAP_RA_FLUSH) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 83 | return "OP_MMAP_RA_FLUSH"; |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 84 | else if (type == ORANGEFS_VFS_OP_FS_MOUNT) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 85 | return "OP_FS_MOUNT"; |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 86 | else if (type == ORANGEFS_VFS_OP_FS_UMOUNT) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 87 | return "OP_FS_UMOUNT"; |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 88 | else if (type == ORANGEFS_VFS_OP_GETXATTR) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 89 | return "OP_GETXATTR"; |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 90 | else if (type == ORANGEFS_VFS_OP_SETXATTR) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 91 | return "OP_SETXATTR"; |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 92 | else if (type == ORANGEFS_VFS_OP_LISTXATTR) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 93 | return "OP_LISTXATTR"; |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 94 | else if (type == ORANGEFS_VFS_OP_REMOVEXATTR) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 95 | return "OP_REMOVEXATTR"; |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 96 | else if (type == ORANGEFS_VFS_OP_PARAM) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 97 | return "OP_PARAM"; |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 98 | else if (type == ORANGEFS_VFS_OP_PERF_COUNT) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 99 | return "OP_PERF_COUNT"; |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 100 | else if (type == ORANGEFS_VFS_OP_CANCEL) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 101 | return "OP_CANCEL"; |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 102 | else if (type == ORANGEFS_VFS_OP_FSYNC) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 103 | return "OP_FSYNC"; |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 104 | else if (type == ORANGEFS_VFS_OP_FSKEY) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 105 | return "OP_FSKEY"; |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 106 | } |
| 107 | return "OP_UNKNOWN?"; |
| 108 | } |
| 109 | |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 110 | struct orangefs_kernel_op_s *op_alloc(__s32 type) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 111 | { |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 112 | struct orangefs_kernel_op_s *new_op = NULL; |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 113 | |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 114 | new_op = kmem_cache_alloc(op_cache, ORANGEFS_CACHE_ALLOC_FLAGS); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 115 | if (new_op) { |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 116 | memset(new_op, 0, sizeof(struct orangefs_kernel_op_s)); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 117 | |
| 118 | INIT_LIST_HEAD(&new_op->list); |
| 119 | spin_lock_init(&new_op->lock); |
| 120 | init_waitqueue_head(&new_op->waitq); |
| 121 | |
| 122 | init_waitqueue_head(&new_op->io_completion_waitq); |
| 123 | atomic_set(&new_op->aio_ref_count, 0); |
| 124 | |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 125 | orangefs_op_initialize(new_op); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 126 | |
| 127 | /* initialize the op specific tag and upcall credentials */ |
| 128 | spin_lock(&next_tag_value_lock); |
| 129 | new_op->tag = next_tag_value++; |
| 130 | if (next_tag_value == 0) |
| 131 | next_tag_value = 100; |
| 132 | spin_unlock(&next_tag_value_lock); |
| 133 | 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 | |
| 141 | new_op->upcall.uid = from_kuid(current_user_ns(), |
| 142 | current_fsuid()); |
| 143 | |
| 144 | new_op->upcall.gid = from_kgid(current_user_ns(), |
| 145 | current_fsgid()); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 146 | } else { |
| 147 | gossip_err("op_alloc: kmem_cache_alloc failed!\n"); |
| 148 | } |
| 149 | return new_op; |
| 150 | } |
| 151 | |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 152 | void op_release(struct orangefs_kernel_op_s *orangefs_op) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 153 | { |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 154 | if (orangefs_op) { |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 155 | gossip_debug(GOSSIP_CACHE_DEBUG, |
| 156 | "Releasing OP (%p: %llu)\n", |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 157 | orangefs_op, |
| 158 | llu(orangefs_op->tag)); |
| 159 | orangefs_op_initialize(orangefs_op); |
| 160 | kmem_cache_free(op_cache, orangefs_op); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 161 | } else { |
| 162 | gossip_err("NULL pointer in op_release\n"); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | int dev_req_cache_initialize(void) |
| 167 | { |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 168 | dev_req_cache = kmem_cache_create("orangefs_devreqcache", |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 169 | MAX_ALIGNED_DEV_REQ_DOWNSIZE, |
| 170 | 0, |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 171 | ORANGEFS_CACHE_CREATE_FLAGS, |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 172 | NULL); |
| 173 | |
| 174 | if (!dev_req_cache) { |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 175 | gossip_err("Cannot create orangefs_dev_req_cache\n"); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 176 | return -ENOMEM; |
| 177 | } |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | int dev_req_cache_finalize(void) |
| 182 | { |
| 183 | kmem_cache_destroy(dev_req_cache); |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | void *dev_req_alloc(void) |
| 188 | { |
| 189 | void *buffer; |
| 190 | |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 191 | buffer = kmem_cache_alloc(dev_req_cache, ORANGEFS_CACHE_ALLOC_FLAGS); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 192 | if (buffer == NULL) |
| 193 | gossip_err("Failed to allocate from dev_req_cache\n"); |
| 194 | else |
| 195 | memset(buffer, 0, sizeof(MAX_ALIGNED_DEV_REQ_DOWNSIZE)); |
| 196 | return buffer; |
| 197 | } |
| 198 | |
| 199 | void dev_req_release(void *buffer) |
| 200 | { |
| 201 | if (buffer) |
| 202 | kmem_cache_free(dev_req_cache, buffer); |
| 203 | else |
| 204 | gossip_err("NULL pointer passed to dev_req_release\n"); |
| 205 | } |
| 206 | |
| 207 | int kiocb_cache_initialize(void) |
| 208 | { |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 209 | orangefs_kiocb_cache = kmem_cache_create("orangefs_kiocbcache", |
| 210 | sizeof(struct orangefs_kiocb_s), |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 211 | 0, |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 212 | ORANGEFS_CACHE_CREATE_FLAGS, |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 213 | NULL); |
| 214 | |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 215 | if (!orangefs_kiocb_cache) { |
| 216 | gossip_err("Cannot create orangefs_kiocb_cache!\n"); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 217 | return -ENOMEM; |
| 218 | } |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | int kiocb_cache_finalize(void) |
| 223 | { |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 224 | kmem_cache_destroy(orangefs_kiocb_cache); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 225 | return 0; |
| 226 | } |
| 227 | |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 228 | struct orangefs_kiocb_s *kiocb_alloc(void) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 229 | { |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 230 | struct orangefs_kiocb_s *x = NULL; |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 231 | |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 232 | x = kmem_cache_alloc(orangefs_kiocb_cache, ORANGEFS_CACHE_ALLOC_FLAGS); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 233 | if (x == NULL) |
| 234 | gossip_err("kiocb_alloc: kmem_cache_alloc failed!\n"); |
| 235 | else |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 236 | memset(x, 0, sizeof(struct orangefs_kiocb_s)); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 237 | return x; |
| 238 | } |
| 239 | |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 240 | void kiocb_release(struct orangefs_kiocb_s *x) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 241 | { |
| 242 | if (x) |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 243 | kmem_cache_free(orangefs_kiocb_cache, x); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 244 | else |
| 245 | gossip_err("kiocb_release: kmem_cache_free NULL pointer!\n"); |
| 246 | } |