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