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