Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Xyratex Technology Limited |
| 3 | * |
Andreas Dilger | 1dc563a | 2015-11-08 18:09:37 -0500 | [diff] [blame] | 4 | * Copyright (c) 2013, 2015, Intel Corporation. |
| 5 | * |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 6 | * Author: Andrew Perepechko <Andrew_Perepechko@xyratex.com> |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | #define DEBUG_SUBSYSTEM S_LLITE |
| 11 | |
| 12 | #include <linux/fs.h> |
| 13 | #include <linux/sched.h> |
| 14 | #include <linux/mm.h> |
Greg Kroah-Hartman | 67a235f | 2014-07-11 21:51:41 -0700 | [diff] [blame] | 15 | #include "../include/obd_support.h" |
| 16 | #include "../include/lustre_lite.h" |
| 17 | #include "../include/lustre_dlm.h" |
| 18 | #include "../include/lustre_ver.h" |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 19 | #include "llite_internal.h" |
| 20 | |
| 21 | /* If we ever have hundreds of extended attributes, we might want to consider |
| 22 | * using a hash or a tree structure instead of list for faster lookups. |
| 23 | */ |
| 24 | struct ll_xattr_entry { |
| 25 | struct list_head xe_list; /* protected with |
Oleg Drokin | c0894c6 | 2016-02-24 22:00:30 -0500 | [diff] [blame] | 26 | * lli_xattrs_list_rwsem |
| 27 | */ |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 28 | char *xe_name; /* xattr name, \0-terminated */ |
| 29 | char *xe_value; /* xattr value */ |
| 30 | unsigned xe_namelen; /* strlen(xe_name) + 1 */ |
| 31 | unsigned xe_vallen; /* xattr value length */ |
| 32 | }; |
| 33 | |
| 34 | static struct kmem_cache *xattr_kmem; |
| 35 | static struct lu_kmem_descr xattr_caches[] = { |
| 36 | { |
| 37 | .ckd_cache = &xattr_kmem, |
| 38 | .ckd_name = "xattr_kmem", |
| 39 | .ckd_size = sizeof(struct ll_xattr_entry) |
| 40 | }, |
| 41 | { |
| 42 | .ckd_cache = NULL |
| 43 | } |
| 44 | }; |
| 45 | |
| 46 | int ll_xattr_init(void) |
| 47 | { |
| 48 | return lu_kmem_init(xattr_caches); |
| 49 | } |
| 50 | |
| 51 | void ll_xattr_fini(void) |
| 52 | { |
| 53 | lu_kmem_fini(xattr_caches); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Initializes xattr cache for an inode. |
| 58 | * |
| 59 | * This initializes the xattr list and marks cache presence. |
| 60 | */ |
| 61 | static void ll_xattr_cache_init(struct ll_inode_info *lli) |
| 62 | { |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 63 | INIT_LIST_HEAD(&lli->lli_xattrs); |
| 64 | lli->lli_flags |= LLIF_XATTR_CACHE; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * This looks for a specific extended attribute. |
| 69 | * |
| 70 | * Find in @cache and return @xattr_name attribute in @xattr, |
| 71 | * for the NULL @xattr_name return the first cached @xattr. |
| 72 | * |
| 73 | * \retval 0 success |
| 74 | * \retval -ENODATA if not found |
| 75 | */ |
| 76 | static int ll_xattr_cache_find(struct list_head *cache, |
| 77 | const char *xattr_name, |
| 78 | struct ll_xattr_entry **xattr) |
| 79 | { |
| 80 | struct ll_xattr_entry *entry; |
| 81 | |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 82 | list_for_each_entry(entry, cache, xe_list) { |
| 83 | /* xattr_name == NULL means look for any entry */ |
Oleg Drokin | 6e16818 | 2016-02-16 00:46:46 -0500 | [diff] [blame] | 84 | if (!xattr_name || strcmp(xattr_name, entry->xe_name) == 0) { |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 85 | *xattr = entry; |
| 86 | CDEBUG(D_CACHE, "find: [%s]=%.*s\n", |
| 87 | entry->xe_name, entry->xe_vallen, |
| 88 | entry->xe_value); |
| 89 | return 0; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | return -ENODATA; |
| 94 | } |
| 95 | |
| 96 | /** |
Andrew Perepechko | e93a308 | 2014-02-09 02:51:48 -0500 | [diff] [blame] | 97 | * This adds an xattr. |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 98 | * |
| 99 | * Add @xattr_name attr with @xattr_val value and @xattr_val_len length, |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 100 | * |
| 101 | * \retval 0 success |
| 102 | * \retval -ENOMEM if no memory could be allocated for the cached attr |
Andrew Perepechko | e93a308 | 2014-02-09 02:51:48 -0500 | [diff] [blame] | 103 | * \retval -EPROTO if duplicate xattr is being added |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 104 | */ |
| 105 | static int ll_xattr_cache_add(struct list_head *cache, |
| 106 | const char *xattr_name, |
| 107 | const char *xattr_val, |
| 108 | unsigned xattr_val_len) |
| 109 | { |
| 110 | struct ll_xattr_entry *xattr; |
| 111 | |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 112 | if (ll_xattr_cache_find(cache, xattr_name, &xattr) == 0) { |
Andrew Perepechko | e93a308 | 2014-02-09 02:51:48 -0500 | [diff] [blame] | 113 | CDEBUG(D_CACHE, "duplicate xattr: [%s]\n", xattr_name); |
| 114 | return -EPROTO; |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 115 | } |
| 116 | |
Amitoj Kaur Chawla | 21068c4 | 2016-02-26 14:25:09 +0530 | [diff] [blame] | 117 | xattr = kmem_cache_zalloc(xattr_kmem, GFP_NOFS); |
Oleg Drokin | 6e16818 | 2016-02-16 00:46:46 -0500 | [diff] [blame] | 118 | if (!xattr) { |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 119 | CDEBUG(D_CACHE, "failed to allocate xattr\n"); |
| 120 | return -ENOMEM; |
| 121 | } |
| 122 | |
Tapasweni Pathak | b3dd895 | 2014-10-24 21:46:00 +0530 | [diff] [blame] | 123 | xattr->xe_name = kstrdup(xattr_name, GFP_NOFS); |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 124 | if (!xattr->xe_name) { |
| 125 | CDEBUG(D_CACHE, "failed to alloc xattr name %u\n", |
| 126 | xattr->xe_namelen); |
| 127 | goto err_name; |
| 128 | } |
Ravindran, Madhusudhanan (M.) | 9cda685 | 2015-03-12 17:35:52 +0000 | [diff] [blame] | 129 | xattr->xe_value = kmemdup(xattr_val, xattr_val_len, GFP_NOFS); |
Quentin Lambert | 695a066 | 2015-02-12 15:56:07 +0100 | [diff] [blame] | 130 | if (!xattr->xe_value) |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 131 | goto err_value; |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 132 | |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 133 | xattr->xe_vallen = xattr_val_len; |
| 134 | list_add(&xattr->xe_list, cache); |
| 135 | |
Oleg Drokin | e15ba45 | 2016-02-26 01:49:49 -0500 | [diff] [blame] | 136 | CDEBUG(D_CACHE, "set: [%s]=%.*s\n", xattr_name, xattr_val_len, |
| 137 | xattr_val); |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 138 | |
| 139 | return 0; |
| 140 | err_value: |
Julia Lawall | 97903a2 | 2015-04-12 22:55:02 +0200 | [diff] [blame] | 141 | kfree(xattr->xe_name); |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 142 | err_name: |
Mike Rapoport | 50d3036 | 2015-10-20 12:39:51 +0300 | [diff] [blame] | 143 | kmem_cache_free(xattr_kmem, xattr); |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 144 | |
| 145 | return -ENOMEM; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * This removes an extended attribute from cache. |
| 150 | * |
| 151 | * Remove @xattr_name attribute from @cache. |
| 152 | * |
| 153 | * \retval 0 success |
| 154 | * \retval -ENODATA if @xattr_name is not cached |
| 155 | */ |
| 156 | static int ll_xattr_cache_del(struct list_head *cache, |
| 157 | const char *xattr_name) |
| 158 | { |
| 159 | struct ll_xattr_entry *xattr; |
| 160 | |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 161 | CDEBUG(D_CACHE, "del xattr: %s\n", xattr_name); |
| 162 | |
| 163 | if (ll_xattr_cache_find(cache, xattr_name, &xattr) == 0) { |
| 164 | list_del(&xattr->xe_list); |
Julia Lawall | 97903a2 | 2015-04-12 22:55:02 +0200 | [diff] [blame] | 165 | kfree(xattr->xe_name); |
| 166 | kfree(xattr->xe_value); |
Mike Rapoport | 50d3036 | 2015-10-20 12:39:51 +0300 | [diff] [blame] | 167 | kmem_cache_free(xattr_kmem, xattr); |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | return -ENODATA; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * This iterates cached extended attributes. |
| 177 | * |
| 178 | * Walk over cached attributes in @cache and |
| 179 | * fill in @xld_buffer or only calculate buffer |
| 180 | * size if @xld_buffer is NULL. |
| 181 | * |
| 182 | * \retval >= 0 buffer list size |
| 183 | * \retval -ENODATA if the list cannot fit @xld_size buffer |
| 184 | */ |
| 185 | static int ll_xattr_cache_list(struct list_head *cache, |
| 186 | char *xld_buffer, |
| 187 | int xld_size) |
| 188 | { |
| 189 | struct ll_xattr_entry *xattr, *tmp; |
| 190 | int xld_tail = 0; |
| 191 | |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 192 | list_for_each_entry_safe(xattr, tmp, cache, xe_list) { |
| 193 | CDEBUG(D_CACHE, "list: buffer=%p[%d] name=%s\n", |
Oleg Drokin | e15ba45 | 2016-02-26 01:49:49 -0500 | [diff] [blame] | 194 | xld_buffer, xld_tail, xattr->xe_name); |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 195 | |
| 196 | if (xld_buffer) { |
| 197 | xld_size -= xattr->xe_namelen; |
| 198 | if (xld_size < 0) |
| 199 | break; |
| 200 | memcpy(&xld_buffer[xld_tail], |
| 201 | xattr->xe_name, xattr->xe_namelen); |
| 202 | } |
| 203 | xld_tail += xattr->xe_namelen; |
| 204 | } |
| 205 | |
| 206 | if (xld_size < 0) |
| 207 | return -ERANGE; |
| 208 | |
| 209 | return xld_tail; |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Check if the xattr cache is initialized (filled). |
| 214 | * |
| 215 | * \retval 0 @cache is not initialized |
| 216 | * \retval 1 @cache is initialized |
| 217 | */ |
John L. Hammond | 2d95f10 | 2014-04-27 13:07:05 -0400 | [diff] [blame] | 218 | static int ll_xattr_cache_valid(struct ll_inode_info *lli) |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 219 | { |
| 220 | return !!(lli->lli_flags & LLIF_XATTR_CACHE); |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * This finalizes the xattr cache. |
| 225 | * |
| 226 | * Free all xattr memory. @lli is the inode info pointer. |
| 227 | * |
Masanari Iida | d0a0acc | 2014-03-08 22:58:32 +0900 | [diff] [blame] | 228 | * \retval 0 no error occurred |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 229 | */ |
| 230 | static int ll_xattr_cache_destroy_locked(struct ll_inode_info *lli) |
| 231 | { |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 232 | if (!ll_xattr_cache_valid(lli)) |
| 233 | return 0; |
| 234 | |
| 235 | while (ll_xattr_cache_del(&lli->lli_xattrs, NULL) == 0) |
| 236 | ; /* empty loop */ |
| 237 | lli->lli_flags &= ~LLIF_XATTR_CACHE; |
| 238 | |
| 239 | return 0; |
| 240 | } |
| 241 | |
| 242 | int ll_xattr_cache_destroy(struct inode *inode) |
| 243 | { |
| 244 | struct ll_inode_info *lli = ll_i2info(inode); |
| 245 | int rc; |
| 246 | |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 247 | down_write(&lli->lli_xattrs_list_rwsem); |
| 248 | rc = ll_xattr_cache_destroy_locked(lli); |
| 249 | up_write(&lli->lli_xattrs_list_rwsem); |
| 250 | |
| 251 | return rc; |
| 252 | } |
| 253 | |
| 254 | /** |
Andrew Perepechko | e93a308 | 2014-02-09 02:51:48 -0500 | [diff] [blame] | 255 | * Match or enqueue a PR lock. |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 256 | * |
| 257 | * Find or request an LDLM lock with xattr data. |
| 258 | * Since LDLM does not provide API for atomic match_or_enqueue, |
| 259 | * the function handles it with a separate enq lock. |
| 260 | * If successful, the function exits with the list lock held. |
| 261 | * |
Masanari Iida | d0a0acc | 2014-03-08 22:58:32 +0900 | [diff] [blame] | 262 | * \retval 0 no error occurred |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 263 | * \retval -ENOMEM not enough memory |
| 264 | */ |
| 265 | static int ll_xattr_find_get_lock(struct inode *inode, |
| 266 | struct lookup_intent *oit, |
| 267 | struct ptlrpc_request **req) |
| 268 | { |
Oleg Drokin | 52ee0d2 | 2016-02-24 21:59:54 -0500 | [diff] [blame] | 269 | enum ldlm_mode mode; |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 270 | struct lustre_handle lockh = { 0 }; |
| 271 | struct md_op_data *op_data; |
| 272 | struct ll_inode_info *lli = ll_i2info(inode); |
| 273 | struct ldlm_enqueue_info einfo = { .ei_type = LDLM_IBITS, |
| 274 | .ei_mode = it_to_lock_mode(oit), |
| 275 | .ei_cb_bl = ll_md_blocking_ast, |
| 276 | .ei_cb_cp = ldlm_completion_ast }; |
| 277 | struct ll_sb_info *sbi = ll_i2sbi(inode); |
| 278 | struct obd_export *exp = sbi->ll_md_exp; |
| 279 | int rc; |
| 280 | |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 281 | mutex_lock(&lli->lli_xattrs_enq_lock); |
Lai Siyao | d6abc59 | 2015-03-25 21:53:26 -0400 | [diff] [blame] | 282 | /* inode may have been shrunk and recreated, so data is gone, match lock |
Oleg Drokin | c0894c6 | 2016-02-24 22:00:30 -0500 | [diff] [blame] | 283 | * only when data exists. |
| 284 | */ |
Lai Siyao | d6abc59 | 2015-03-25 21:53:26 -0400 | [diff] [blame] | 285 | if (ll_xattr_cache_valid(lli)) { |
| 286 | /* Try matching first. */ |
| 287 | mode = ll_take_md_lock(inode, MDS_INODELOCK_XATTR, &lockh, 0, |
| 288 | LCK_PR); |
| 289 | if (mode != 0) { |
| 290 | /* fake oit in mdc_revalidate_lock() manner */ |
| 291 | oit->d.lustre.it_lock_handle = lockh.cookie; |
| 292 | oit->d.lustre.it_lock_mode = mode; |
| 293 | goto out; |
| 294 | } |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | /* Enqueue if the lock isn't cached locally. */ |
| 298 | op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0, |
| 299 | LUSTRE_OPC_ANY, NULL); |
| 300 | if (IS_ERR(op_data)) { |
| 301 | mutex_unlock(&lli->lli_xattrs_enq_lock); |
| 302 | return PTR_ERR(op_data); |
| 303 | } |
| 304 | |
Andrew Perepechko | e93a308 | 2014-02-09 02:51:48 -0500 | [diff] [blame] | 305 | op_data->op_valid = OBD_MD_FLXATTR | OBD_MD_FLXATTRLS; |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 306 | |
| 307 | rc = md_enqueue(exp, &einfo, oit, op_data, &lockh, NULL, 0, NULL, 0); |
| 308 | ll_finish_md_op_data(op_data); |
| 309 | |
| 310 | if (rc < 0) { |
| 311 | CDEBUG(D_CACHE, |
| 312 | "md_intent_lock failed with %d for fid "DFID"\n", |
| 313 | rc, PFID(ll_inode2fid(inode))); |
| 314 | mutex_unlock(&lli->lli_xattrs_enq_lock); |
| 315 | return rc; |
| 316 | } |
| 317 | |
| 318 | *req = (struct ptlrpc_request *)oit->d.lustre.it_data; |
| 319 | out: |
| 320 | down_write(&lli->lli_xattrs_list_rwsem); |
| 321 | mutex_unlock(&lli->lli_xattrs_enq_lock); |
| 322 | |
| 323 | return 0; |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * Refill the xattr cache. |
| 328 | * |
| 329 | * Fetch and cache the whole of xattrs for @inode, acquiring |
| 330 | * a read or a write xattr lock depending on operation in @oit. |
| 331 | * Intent is dropped on exit unless the operation is setxattr. |
| 332 | * |
Masanari Iida | d0a0acc | 2014-03-08 22:58:32 +0900 | [diff] [blame] | 333 | * \retval 0 no error occurred |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 334 | * \retval -EPROTO network protocol error |
| 335 | * \retval -ENOMEM not enough memory for the cache |
| 336 | */ |
| 337 | static int ll_xattr_cache_refill(struct inode *inode, struct lookup_intent *oit) |
| 338 | { |
| 339 | struct ll_sb_info *sbi = ll_i2sbi(inode); |
| 340 | struct ptlrpc_request *req = NULL; |
| 341 | const char *xdata, *xval, *xtail, *xvtail; |
| 342 | struct ll_inode_info *lli = ll_i2info(inode); |
| 343 | struct mdt_body *body; |
| 344 | __u32 *xsizes; |
Julia Lawall | f82ced5 | 2015-06-20 21:07:50 +0200 | [diff] [blame] | 345 | int rc, i; |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 346 | |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 347 | rc = ll_xattr_find_get_lock(inode, oit, &req); |
| 348 | if (rc) |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 349 | goto out_no_unlock; |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 350 | |
| 351 | /* Do we have the data at this point? */ |
| 352 | if (ll_xattr_cache_valid(lli)) { |
| 353 | ll_stats_ops_tally(sbi, LPROC_LL_GETXATTR_HITS, 1); |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 354 | rc = 0; |
| 355 | goto out_maybe_drop; |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | /* Matched but no cache? Cancelled on error by a parallel refill. */ |
Oleg Drokin | 6e16818 | 2016-02-16 00:46:46 -0500 | [diff] [blame] | 359 | if (unlikely(!req)) { |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 360 | CDEBUG(D_CACHE, "cancelled by a parallel getxattr\n"); |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 361 | rc = -EIO; |
| 362 | goto out_maybe_drop; |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | if (oit->d.lustre.it_status < 0) { |
| 366 | CDEBUG(D_CACHE, "getxattr intent returned %d for fid "DFID"\n", |
| 367 | oit->d.lustre.it_status, PFID(ll_inode2fid(inode))); |
Andrew Perepechko | e93a308 | 2014-02-09 02:51:48 -0500 | [diff] [blame] | 368 | rc = oit->d.lustre.it_status; |
| 369 | /* xattr data is so large that we don't want to cache it */ |
| 370 | if (rc == -ERANGE) |
| 371 | rc = -EAGAIN; |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 372 | goto out_destroy; |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY); |
Oleg Drokin | 6e16818 | 2016-02-16 00:46:46 -0500 | [diff] [blame] | 376 | if (!body) { |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 377 | CERROR("no MDT BODY in the refill xattr reply\n"); |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 378 | rc = -EPROTO; |
| 379 | goto out_destroy; |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 380 | } |
| 381 | /* do not need swab xattr data */ |
| 382 | xdata = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA, |
Oleg Drokin | e15ba45 | 2016-02-26 01:49:49 -0500 | [diff] [blame] | 383 | body->eadatasize); |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 384 | xval = req_capsule_server_sized_get(&req->rq_pill, &RMF_EAVALS, |
Oleg Drokin | e15ba45 | 2016-02-26 01:49:49 -0500 | [diff] [blame] | 385 | body->aclsize); |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 386 | xsizes = req_capsule_server_sized_get(&req->rq_pill, &RMF_EAVALS_LENS, |
| 387 | body->max_mdsize * sizeof(__u32)); |
Oleg Drokin | 6e16818 | 2016-02-16 00:46:46 -0500 | [diff] [blame] | 388 | if (!xdata || !xval || !xsizes) { |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 389 | CERROR("wrong setxattr reply\n"); |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 390 | rc = -EPROTO; |
| 391 | goto out_destroy; |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | xtail = xdata + body->eadatasize; |
| 395 | xvtail = xval + body->aclsize; |
| 396 | |
| 397 | CDEBUG(D_CACHE, "caching: xdata=%p xtail=%p\n", xdata, xtail); |
| 398 | |
| 399 | ll_xattr_cache_init(lli); |
| 400 | |
| 401 | for (i = 0; i < body->max_mdsize; i++) { |
| 402 | CDEBUG(D_CACHE, "caching [%s]=%.*s\n", xdata, *xsizes, xval); |
| 403 | /* Perform consistency checks: attr names and vals in pill */ |
Oleg Drokin | 6e16818 | 2016-02-16 00:46:46 -0500 | [diff] [blame] | 404 | if (!memchr(xdata, 0, xtail - xdata)) { |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 405 | CERROR("xattr protocol violation (names are broken)\n"); |
| 406 | rc = -EPROTO; |
| 407 | } else if (xval + *xsizes > xvtail) { |
| 408 | CERROR("xattr protocol violation (vals are broken)\n"); |
| 409 | rc = -EPROTO; |
| 410 | } else if (OBD_FAIL_CHECK(OBD_FAIL_LLITE_XATTR_ENOMEM)) { |
| 411 | rc = -ENOMEM; |
Andrew Perepechko | e93a308 | 2014-02-09 02:51:48 -0500 | [diff] [blame] | 412 | } else if (!strcmp(xdata, XATTR_NAME_ACL_ACCESS)) { |
| 413 | /* Filter out ACL ACCESS since it's cached separately */ |
| 414 | CDEBUG(D_CACHE, "not caching %s\n", |
| 415 | XATTR_NAME_ACL_ACCESS); |
| 416 | rc = 0; |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 417 | } else { |
| 418 | rc = ll_xattr_cache_add(&lli->lli_xattrs, xdata, xval, |
| 419 | *xsizes); |
| 420 | } |
| 421 | if (rc < 0) { |
| 422 | ll_xattr_cache_destroy_locked(lli); |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 423 | goto out_destroy; |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 424 | } |
| 425 | xdata += strlen(xdata) + 1; |
| 426 | xval += *xsizes; |
| 427 | xsizes++; |
| 428 | } |
| 429 | |
| 430 | if (xdata != xtail || xval != xvtail) |
| 431 | CERROR("a hole in xattr data\n"); |
| 432 | |
| 433 | ll_set_lock_data(sbi->ll_md_exp, inode, oit, NULL); |
| 434 | |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 435 | goto out_maybe_drop; |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 436 | out_maybe_drop: |
Andrew Perepechko | e93a308 | 2014-02-09 02:51:48 -0500 | [diff] [blame] | 437 | |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 438 | ll_intent_drop_lock(oit); |
| 439 | |
| 440 | if (rc != 0) |
| 441 | up_write(&lli->lli_xattrs_list_rwsem); |
| 442 | out_no_unlock: |
| 443 | ptlrpc_req_finished(req); |
| 444 | |
| 445 | return rc; |
| 446 | |
| 447 | out_destroy: |
| 448 | up_write(&lli->lli_xattrs_list_rwsem); |
| 449 | |
| 450 | ldlm_lock_decref_and_cancel((struct lustre_handle *) |
| 451 | &oit->d.lustre.it_lock_handle, |
| 452 | oit->d.lustre.it_lock_mode); |
| 453 | |
| 454 | goto out_no_unlock; |
| 455 | } |
| 456 | |
| 457 | /** |
| 458 | * Get an xattr value or list xattrs using the write-through cache. |
| 459 | * |
| 460 | * Get the xattr value (@valid has OBD_MD_FLXATTR set) of @name or |
| 461 | * list xattr names (@valid has OBD_MD_FLXATTRLS set) for @inode. |
| 462 | * The resulting value/list is stored in @buffer if the former |
| 463 | * is not larger than @size. |
| 464 | * |
Masanari Iida | d0a0acc | 2014-03-08 22:58:32 +0900 | [diff] [blame] | 465 | * \retval 0 no error occurred |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 466 | * \retval -EPROTO network protocol error |
| 467 | * \retval -ENOMEM not enough memory for the cache |
| 468 | * \retval -ERANGE the buffer is not large enough |
| 469 | * \retval -ENODATA no such attr or the list is empty |
| 470 | */ |
Oleg Drokin | e15ba45 | 2016-02-26 01:49:49 -0500 | [diff] [blame] | 471 | int ll_xattr_cache_get(struct inode *inode, const char *name, char *buffer, |
| 472 | size_t size, __u64 valid) |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 473 | { |
| 474 | struct lookup_intent oit = { .it_op = IT_GETXATTR }; |
| 475 | struct ll_inode_info *lli = ll_i2info(inode); |
| 476 | int rc = 0; |
| 477 | |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 478 | LASSERT(!!(valid & OBD_MD_FLXATTR) ^ !!(valid & OBD_MD_FLXATTRLS)); |
| 479 | |
| 480 | down_read(&lli->lli_xattrs_list_rwsem); |
| 481 | if (!ll_xattr_cache_valid(lli)) { |
| 482 | up_read(&lli->lli_xattrs_list_rwsem); |
| 483 | rc = ll_xattr_cache_refill(inode, &oit); |
| 484 | if (rc) |
| 485 | return rc; |
| 486 | downgrade_write(&lli->lli_xattrs_list_rwsem); |
| 487 | } else { |
| 488 | ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR_HITS, 1); |
| 489 | } |
| 490 | |
| 491 | if (valid & OBD_MD_FLXATTR) { |
| 492 | struct ll_xattr_entry *xattr; |
| 493 | |
| 494 | rc = ll_xattr_cache_find(&lli->lli_xattrs, name, &xattr); |
| 495 | if (rc == 0) { |
| 496 | rc = xattr->xe_vallen; |
| 497 | /* zero size means we are only requested size in rc */ |
| 498 | if (size != 0) { |
| 499 | if (size >= xattr->xe_vallen) |
| 500 | memcpy(buffer, xattr->xe_value, |
Oleg Drokin | e15ba45 | 2016-02-26 01:49:49 -0500 | [diff] [blame] | 501 | xattr->xe_vallen); |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 502 | else |
| 503 | rc = -ERANGE; |
| 504 | } |
| 505 | } |
| 506 | } else if (valid & OBD_MD_FLXATTRLS) { |
| 507 | rc = ll_xattr_cache_list(&lli->lli_xattrs, |
| 508 | size ? buffer : NULL, size); |
| 509 | } |
| 510 | |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 511 | goto out; |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 512 | out: |
| 513 | up_read(&lli->lli_xattrs_list_rwsem); |
| 514 | |
| 515 | return rc; |
| 516 | } |