Whamcloud - gitweb
LU-12275 sec: atomicity of encryption context getting/setting
[fs/lustre-release.git] / lustre / llite / xattr_cache.c
1 /* GPL HEADER START
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 only,
7  * as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License version 2 for more details (a copy is included
13  * in the LICENSE file that accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License
16  * version 2 along with this program; If not, see http://www.gnu.org/licenses
17  *
18  * Please  visit http://www.xyratex.com/contact if you need additional
19  * information or have any questions.
20  *
21  * GPL HEADER END
22  */
23
24 /*
25  * Copyright 2012 Xyratex Technology Limited
26  *
27  * Copyright (c) 2013, 2017, Intel Corporation.
28  *
29  * Author: Andrew Perepechko <Andrew_Perepechko@xyratex.com>
30  *
31  */
32
33 #define DEBUG_SUBSYSTEM S_LLITE
34
35 #include <linux/fs.h>
36 #include <linux/sched.h>
37 #include <linux/mm.h>
38 #include <obd_support.h>
39 #include <lustre_dlm.h>
40 #include "llite_internal.h"
41
42 /* If we ever have hundreds of extended attributes, we might want to consider
43  * using a hash or a tree structure instead of list for faster lookups.
44  */
45 struct ll_xattr_entry {
46         struct list_head        xe_list;    /* protected with
47                                              * lli_xattrs_list_rwsem */
48         char                    *xe_name;   /* xattr name, \0-terminated */
49         char                    *xe_value;  /* xattr value */
50         unsigned                xe_namelen; /* strlen(xe_name) + 1 */
51         unsigned                xe_vallen;  /* xattr value length */
52 };
53
54 static struct kmem_cache *xattr_kmem;
55 static struct lu_kmem_descr xattr_caches[] = {
56         {
57                 .ckd_cache = &xattr_kmem,
58                 .ckd_name  = "xattr_kmem",
59                 .ckd_size  = sizeof(struct ll_xattr_entry)
60         },
61         {
62                 .ckd_cache = NULL
63         }
64 };
65
66 int ll_xattr_init(void)
67 {
68         return lu_kmem_init(xattr_caches);
69 }
70
71 void ll_xattr_fini(void)
72 {
73         lu_kmem_fini(xattr_caches);
74 }
75
76 /**
77  * Initializes xattr cache for an inode.
78  *
79  * This initializes the xattr list and marks cache presence.
80  */
81 static void ll_xattr_cache_init(struct ll_inode_info *lli)
82 {
83         ENTRY;
84
85         LASSERT(lli != NULL);
86
87         INIT_LIST_HEAD(&lli->lli_xattrs);
88         ll_file_set_flag(lli, LLIF_XATTR_CACHE);
89 }
90
91 /**
92  *  This looks for a specific extended attribute.
93  *
94  *  Find in @cache and return @xattr_name attribute in @xattr,
95  *  for the NULL @xattr_name return the first cached @xattr.
96  *
97  *  \retval 0        success
98  *  \retval -ENODATA if not found
99  */
100 static int ll_xattr_cache_find(struct list_head *cache,
101                                const char *xattr_name,
102                                struct ll_xattr_entry **xattr)
103 {
104         struct ll_xattr_entry *entry;
105
106         ENTRY;
107
108         list_for_each_entry(entry, cache, xe_list) {
109                 /* xattr_name == NULL means look for any entry */
110                 if (xattr_name == NULL ||
111                     strcmp(xattr_name, entry->xe_name) == 0) {
112                         *xattr = entry;
113                         CDEBUG(D_CACHE, "find: [%s]=%.*s\n",
114                                entry->xe_name, entry->xe_vallen,
115                                entry->xe_value);
116                         RETURN(0);
117                 }
118         }
119
120         RETURN(-ENODATA);
121 }
122
123 /**
124  * This adds an xattr.
125  *
126  * Add @xattr_name attr with @xattr_val value and @xattr_val_len length,
127  *
128  * \retval 0       success
129  * \retval -ENOMEM if no memory could be allocated for the cached attr
130  * \retval -EPROTO if duplicate xattr is being added
131  */
132 static int ll_xattr_cache_add(struct list_head *cache,
133                               const char *xattr_name,
134                               const char *xattr_val,
135                               unsigned xattr_val_len)
136 {
137         struct ll_xattr_entry *xattr;
138
139         ENTRY;
140
141         if (ll_xattr_cache_find(cache, xattr_name, &xattr) == 0) {
142                 CDEBUG(D_CACHE, "duplicate xattr: [%s]\n", xattr_name);
143                 RETURN(-EPROTO);
144         }
145
146         OBD_SLAB_ALLOC_PTR_GFP(xattr, xattr_kmem, GFP_NOFS);
147         if (xattr == NULL) {
148                 CDEBUG(D_CACHE, "failed to allocate xattr\n");
149                 RETURN(-ENOMEM);
150         }
151
152         xattr->xe_namelen = strlen(xattr_name) + 1;
153
154         OBD_ALLOC(xattr->xe_name, xattr->xe_namelen);
155         if (!xattr->xe_name) {
156                 CDEBUG(D_CACHE, "failed to alloc xattr name %u\n",
157                        xattr->xe_namelen);
158                 goto err_name;
159         }
160         OBD_ALLOC(xattr->xe_value, xattr_val_len);
161         if (!xattr->xe_value) {
162                 CDEBUG(D_CACHE, "failed to alloc xattr value %d\n",
163                        xattr_val_len);
164                 goto err_value;
165         }
166
167         memcpy(xattr->xe_name, xattr_name, xattr->xe_namelen);
168         memcpy(xattr->xe_value, xattr_val, xattr_val_len);
169         xattr->xe_vallen = xattr_val_len;
170         list_add(&xattr->xe_list, cache);
171
172         CDEBUG(D_CACHE, "set: [%s]=%.*s\n", xattr_name,
173                 xattr_val_len, xattr_val);
174
175         RETURN(0);
176 err_value:
177         OBD_FREE(xattr->xe_name, xattr->xe_namelen);
178 err_name:
179         OBD_SLAB_FREE_PTR(xattr, xattr_kmem);
180
181         RETURN(-ENOMEM);
182 }
183
184 /**
185  * This removes an extended attribute from cache.
186  *
187  * Remove @xattr_name attribute from @cache.
188  *
189  * \retval 0        success
190  * \retval -ENODATA if @xattr_name is not cached
191  */
192 static int ll_xattr_cache_del(struct list_head *cache,
193                               const char *xattr_name)
194 {
195         struct ll_xattr_entry *xattr;
196
197         ENTRY;
198
199         CDEBUG(D_CACHE, "del xattr: %s\n", xattr_name);
200
201         if (ll_xattr_cache_find(cache, xattr_name, &xattr) == 0) {
202                 list_del(&xattr->xe_list);
203                 OBD_FREE(xattr->xe_name, xattr->xe_namelen);
204                 OBD_FREE(xattr->xe_value, xattr->xe_vallen);
205                 OBD_SLAB_FREE_PTR(xattr, xattr_kmem);
206
207                 RETURN(0);
208         }
209
210         RETURN(-ENODATA);
211 }
212
213 /**
214  * This iterates cached extended attributes.
215  *
216  * Walk over cached attributes in @cache and
217  * fill in @xld_buffer or only calculate buffer
218  * size if @xld_buffer is NULL.
219  *
220  * \retval >= 0     buffer list size
221  * \retval -ENODATA if the list cannot fit @xld_size buffer
222  */
223 static int ll_xattr_cache_list(struct list_head *cache,
224                                char *xld_buffer,
225                                int xld_size)
226 {
227         struct ll_xattr_entry *xattr, *tmp;
228         int xld_tail = 0;
229
230         ENTRY;
231
232         list_for_each_entry_safe(xattr, tmp, cache, xe_list) {
233                 CDEBUG(D_CACHE, "list: buffer=%p[%d] name=%s\n",
234                         xld_buffer, xld_tail, xattr->xe_name);
235
236                 if (xld_buffer) {
237                         xld_size -= xattr->xe_namelen;
238                         if (xld_size < 0)
239                                 break;
240                         memcpy(&xld_buffer[xld_tail],
241                                xattr->xe_name, xattr->xe_namelen);
242                 }
243                 xld_tail += xattr->xe_namelen;
244         }
245
246         if (xld_size < 0)
247                 RETURN(-ERANGE);
248
249         RETURN(xld_tail);
250 }
251
252 /**
253  * Check if the xattr cache is initialized (filled).
254  *
255  * \retval 0 @cache is not initialized
256  * \retval 1 @cache is initialized
257  */
258 static int ll_xattr_cache_valid(struct ll_inode_info *lli)
259 {
260         return ll_file_test_flag(lli, LLIF_XATTR_CACHE);
261 }
262
263 /**
264  * This finalizes the xattr cache.
265  *
266  * Free all xattr memory. @lli is the inode info pointer.
267  *
268  * \retval 0 no error occured
269  */
270 static int ll_xattr_cache_destroy_locked(struct ll_inode_info *lli)
271 {
272         ENTRY;
273
274         if (!ll_xattr_cache_valid(lli))
275                 RETURN(0);
276
277         while (ll_xattr_cache_del(&lli->lli_xattrs, NULL) == 0)
278                 /* empty loop */ ;
279
280         ll_file_clear_flag(lli, LLIF_XATTR_CACHE);
281
282         RETURN(0);
283 }
284
285 int ll_xattr_cache_destroy(struct inode *inode)
286 {
287         struct ll_inode_info *lli = ll_i2info(inode);
288         int rc;
289
290         ENTRY;
291
292         down_write(&lli->lli_xattrs_list_rwsem);
293         rc = ll_xattr_cache_destroy_locked(lli);
294         up_write(&lli->lli_xattrs_list_rwsem);
295
296         RETURN(rc);
297 }
298
299 /**
300  * Match or enqueue a PR lock.
301  *
302  * Find or request an LDLM lock with xattr data.
303  * Since LDLM does not provide API for atomic match_or_enqueue,
304  * the function handles it with a separate enq lock.
305  * If successful, the function exits with the list lock held.
306  *
307  * \retval 0       no error occured
308  * \retval -ENOMEM not enough memory
309  */
310 static int ll_xattr_find_get_lock(struct inode *inode,
311                                   struct lookup_intent *oit,
312                                   struct ptlrpc_request **req)
313 {
314         enum ldlm_mode mode;
315         struct lustre_handle lockh = { 0 };
316         struct md_op_data *op_data;
317         struct ll_inode_info *lli = ll_i2info(inode);
318         struct ll_sb_info *sbi = ll_i2sbi(inode);
319         struct obd_export *exp = sbi->ll_md_exp;
320         int rc;
321
322         ENTRY;
323
324         mutex_lock(&lli->lli_xattrs_enq_lock);
325         /* inode may have been shrunk and recreated, so data is gone, match lock
326          * only when data exists. */
327         if (ll_xattr_cache_valid(lli)) {
328                 /* Try matching first. */
329                 mode = ll_take_md_lock(inode, MDS_INODELOCK_XATTR, &lockh, 0,
330                                         LCK_PR);
331                 if (mode != 0) {
332                         /* fake oit in mdc_revalidate_lock() manner */
333                         oit->it_lock_handle = lockh.cookie;
334                         oit->it_lock_mode = mode;
335                         goto out;
336                 }
337         }
338
339         /* Enqueue if the lock isn't cached locally. */
340         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
341                                      LUSTRE_OPC_ANY, NULL);
342         if (IS_ERR(op_data)) {
343                 mutex_unlock(&lli->lli_xattrs_enq_lock);
344                 RETURN(PTR_ERR(op_data));
345         }
346
347         op_data->op_valid = OBD_MD_FLXATTR | OBD_MD_FLXATTRLS;
348
349         rc = md_intent_lock(exp, op_data, oit, req, &ll_md_blocking_ast, 0);
350         ll_finish_md_op_data(op_data);
351         *req = oit->it_request;
352
353         if (rc < 0) {
354                 CDEBUG(D_CACHE, "md_intent_lock failed with %d for fid "DFID"\n",
355                        rc, PFID(ll_inode2fid(inode)));
356                 mutex_unlock(&lli->lli_xattrs_enq_lock);
357                 RETURN(rc);
358         }
359
360 out:
361         down_write(&lli->lli_xattrs_list_rwsem);
362         mutex_unlock(&lli->lli_xattrs_enq_lock);
363
364         RETURN(0);
365 }
366
367 /**
368  * Refill the xattr cache.
369  *
370  * Fetch and cache the whole of xattrs for @inode, acquiring a read lock.
371  *
372  * \retval 0       no error occured
373  * \retval -EPROTO network protocol error
374  * \retval -ENOMEM not enough memory for the cache
375  */
376 static int ll_xattr_cache_refill(struct inode *inode)
377 {
378         struct lookup_intent oit = { .it_op = IT_GETXATTR };
379         struct ll_sb_info *sbi = ll_i2sbi(inode);
380         struct ptlrpc_request *req = NULL;
381         const char *xdata, *xval, *xtail, *xvtail;
382         struct ll_inode_info *lli = ll_i2info(inode);
383         struct mdt_body *body;
384         __u32 *xsizes;
385         int rc = 0, i;
386
387         ENTRY;
388
389         rc = ll_xattr_find_get_lock(inode, &oit, &req);
390         if (rc)
391                 GOTO(err_req, rc);
392
393         /* Do we have the data at this point? */
394         if (ll_xattr_cache_valid(lli)) {
395                 ll_stats_ops_tally(sbi, LPROC_LL_GETXATTR_HITS, 1);
396                 ll_intent_drop_lock(&oit);
397                 GOTO(err_req, rc = 0);
398         }
399
400         /* Matched but no cache? Cancelled on error by a parallel refill. */
401         if (unlikely(req == NULL)) {
402                 CDEBUG(D_CACHE, "cancelled by a parallel getxattr\n");
403                 ll_intent_drop_lock(&oit);
404                 GOTO(err_unlock, rc = -EAGAIN);
405         }
406
407         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
408         if (body == NULL) {
409                 CERROR("no MDT BODY in the refill xattr reply\n");
410                 GOTO(err_cancel, rc = -EPROTO);
411         }
412         /* do not need swab xattr data */
413         xdata = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA,
414                                                 body->mbo_eadatasize);
415         xval = req_capsule_server_sized_get(&req->rq_pill, &RMF_EAVALS,
416                                                 body->mbo_aclsize);
417         xsizes = req_capsule_server_sized_get(&req->rq_pill, &RMF_EAVALS_LENS,
418                                               body->mbo_max_mdsize *
419                                               sizeof(__u32));
420         if (xdata == NULL || xval == NULL || xsizes == NULL) {
421                 CERROR("wrong setxattr reply\n");
422                 GOTO(err_cancel, rc = -EPROTO);
423         }
424
425         xtail = xdata + body->mbo_eadatasize;
426         xvtail = xval + body->mbo_aclsize;
427
428         CDEBUG(D_CACHE, "caching: xdata=%p xtail=%p\n", xdata, xtail);
429
430         ll_xattr_cache_init(lli);
431
432         for (i = 0; i < body->mbo_max_mdsize; i++) {
433                 CDEBUG(D_CACHE, "caching [%s]=%.*s\n", xdata, *xsizes, xval);
434                 /* Perform consistency checks: attr names and vals in pill */
435                 if (memchr(xdata, 0, xtail - xdata) == NULL) {
436                         CERROR("xattr protocol violation (names are broken)\n");
437                         rc = -EPROTO;
438                 } else if (xval + *xsizes > xvtail) {
439                         CERROR("xattr protocol violation (vals are broken)\n");
440                         rc = -EPROTO;
441                 } else if (OBD_FAIL_CHECK(OBD_FAIL_LLITE_XATTR_ENOMEM)) {
442                         rc = -ENOMEM;
443                 } else if (!strcmp(xdata, XATTR_NAME_ACL_ACCESS)) {
444                         /* Filter out ACL ACCESS since it's cached separately */
445                         CDEBUG(D_CACHE, "not caching %s\n",
446                                XATTR_NAME_ACL_ACCESS);
447                         rc = 0;
448                 } else if (!strcmp(xdata, "security.selinux")) {
449                         /* Filter out security.selinux, it is cached in slab */
450                         CDEBUG(D_CACHE, "not caching security.selinux\n");
451                         rc = 0;
452                 } else {
453                         rc = ll_xattr_cache_add(&lli->lli_xattrs, xdata, xval,
454                                                 *xsizes);
455                 }
456                 if (rc < 0) {
457                         ll_xattr_cache_destroy_locked(lli);
458                         GOTO(err_cancel, rc);
459                 }
460                 xdata += strlen(xdata) + 1;
461                 xval  += *xsizes;
462                 xsizes++;
463         }
464
465         if (xdata != xtail || xval != xvtail)
466                 CERROR("a hole in xattr data\n");
467
468         ll_set_lock_data(sbi->ll_md_exp, inode, &oit, NULL);
469         ll_intent_drop_lock(&oit);
470
471         ptlrpc_req_finished(req);
472         RETURN(0);
473
474 err_cancel:
475         ldlm_lock_decref_and_cancel((struct lustre_handle *)
476                                     &oit.it_lock_handle,
477                                     oit.it_lock_mode);
478 err_unlock:
479         up_write(&lli->lli_xattrs_list_rwsem);
480 err_req:
481         if (rc == -ERANGE)
482                 rc = -EAGAIN;
483
484         ptlrpc_req_finished(req);
485         RETURN(rc);
486 }
487
488 /**
489  * Get an xattr value or list xattrs using the write-through cache.
490  *
491  * Get the xattr value (@valid has OBD_MD_FLXATTR set) of @name or
492  * list xattr names (@valid has OBD_MD_FLXATTRLS set) for @inode.
493  * The resulting value/list is stored in @buffer if the former
494  * is not larger than @size.
495  *
496  * \retval 0        no error occured
497  * \retval -EPROTO  network protocol error
498  * \retval -ENOMEM  not enough memory for the cache
499  * \retval -ERANGE  the buffer is not large enough
500  * \retval -ENODATA no such attr or the list is empty
501  */
502 int ll_xattr_cache_get(struct inode *inode,
503                         const char *name,
504                         char *buffer,
505                         size_t size,
506                         __u64 valid)
507 {
508         struct ll_inode_info *lli = ll_i2info(inode);
509         int rc = 0;
510
511         ENTRY;
512
513         LASSERT(!!(valid & OBD_MD_FLXATTR) ^ !!(valid & OBD_MD_FLXATTRLS));
514
515         down_read(&lli->lli_xattrs_list_rwsem);
516         if (!ll_xattr_cache_valid(lli)) {
517                 up_read(&lli->lli_xattrs_list_rwsem);
518                 rc = ll_xattr_cache_refill(inode);
519                 if (rc)
520                         RETURN(rc);
521                 downgrade_write(&lli->lli_xattrs_list_rwsem);
522         } else {
523                 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR_HITS, 1);
524         }
525
526         if (valid & OBD_MD_FLXATTR) {
527                 struct ll_xattr_entry *xattr;
528
529                 rc = ll_xattr_cache_find(&lli->lli_xattrs, name, &xattr);
530                 if (rc == 0) {
531                         rc = xattr->xe_vallen;
532                         /* zero size means we are only requested size in rc */
533                         if (size != 0) {
534                                 if (size >= xattr->xe_vallen)
535                                         memcpy(buffer, xattr->xe_value,
536                                                 xattr->xe_vallen);
537                                 else
538                                         rc = -ERANGE;
539                         }
540                 }
541         } else if (valid & OBD_MD_FLXATTRLS) {
542                 rc = ll_xattr_cache_list(&lli->lli_xattrs,
543                                          size ? buffer : NULL, size);
544         }
545
546         GOTO(out, rc);
547 out:
548         up_read(&lli->lli_xattrs_list_rwsem);
549
550         RETURN(rc);
551 }
552
553 /**
554  * Insert an xattr value into the cache.
555  *
556  * Add @name xattr with @buffer value and @size length for @inode.
557  * Init cache for @inode if necessary.
558  *
559  * \retval 0       success
560  * \retval < 0     from ll_xattr_cache_add(), except -EPROTO is ignored for
561  *                 LL_XATTR_NAME_ENCRYPTION_CONTEXT xattr
562  */
563 int ll_xattr_cache_insert(struct inode *inode,
564                           const char *name,
565                           char *buffer,
566                           size_t size)
567 {
568         struct ll_inode_info *lli = ll_i2info(inode);
569         int rc;
570
571         ENTRY;
572
573         down_read(&lli->lli_xattrs_list_rwsem);
574         if (!ll_xattr_cache_valid(lli))
575                 ll_xattr_cache_init(lli);
576         rc = ll_xattr_cache_add(&lli->lli_xattrs, name, buffer,
577                                 size);
578         up_read(&lli->lli_xattrs_list_rwsem);
579
580         if (rc == -EPROTO &&
581             strcmp(name, LL_XATTR_NAME_ENCRYPTION_CONTEXT) == 0)
582                 /* it means enc ctx was already in cache,
583                  * ignore error as it cannot be modified
584                  */
585                 rc = 0;
586         RETURN(rc);
587 }