Whamcloud - gitweb
LU-7403 llite: flatten struct lookup_intent
[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, 2015, 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 ldlm_enqueue_info einfo = {
320                 .ei_type = LDLM_IBITS,
321                 .ei_mode = it_to_lock_mode(oit),
322                 .ei_cb_bl = &ll_md_blocking_ast,
323                 .ei_cb_cp = &ldlm_completion_ast,
324         };
325         struct ll_sb_info *sbi = ll_i2sbi(inode);
326         struct obd_export *exp = sbi->ll_md_exp;
327         int rc;
328
329         ENTRY;
330
331         mutex_lock(&lli->lli_xattrs_enq_lock);
332         /* inode may have been shrunk and recreated, so data is gone, match lock
333          * only when data exists. */
334         if (ll_xattr_cache_valid(lli)) {
335                 /* Try matching first. */
336                 mode = ll_take_md_lock(inode, MDS_INODELOCK_XATTR, &lockh, 0,
337                                         LCK_PR);
338                 if (mode != 0) {
339                         /* fake oit in mdc_revalidate_lock() manner */
340                         oit->it_lock_handle = lockh.cookie;
341                         oit->it_lock_mode = mode;
342                         goto out;
343                 }
344         }
345
346         /* Enqueue if the lock isn't cached locally. */
347         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
348                                      LUSTRE_OPC_ANY, NULL);
349         if (IS_ERR(op_data)) {
350                 mutex_unlock(&lli->lli_xattrs_enq_lock);
351                 RETURN(PTR_ERR(op_data));
352         }
353
354         op_data->op_valid = OBD_MD_FLXATTR | OBD_MD_FLXATTRLS;
355
356         rc = md_enqueue(exp, &einfo, NULL, oit, op_data, &lockh, 0);
357         ll_finish_md_op_data(op_data);
358
359         if (rc < 0) {
360                 CDEBUG(D_CACHE, "md_intent_lock failed with %d for fid "DFID"\n",
361                        rc, PFID(ll_inode2fid(inode)));
362                 mutex_unlock(&lli->lli_xattrs_enq_lock);
363                 RETURN(rc);
364         }
365
366         *req = (struct ptlrpc_request *)oit->it_data;
367 out:
368         down_write(&lli->lli_xattrs_list_rwsem);
369         mutex_unlock(&lli->lli_xattrs_enq_lock);
370
371         RETURN(0);
372 }
373
374 /**
375  * Refill the xattr cache.
376  *
377  * Fetch and cache the whole of xattrs for @inode, acquiring
378  * a read or a write xattr lock depending on operation in @oit.
379  * Intent is dropped on exit unless the operation is setxattr.
380  *
381  * \retval 0       no error occured
382  * \retval -EPROTO network protocol error
383  * \retval -ENOMEM not enough memory for the cache
384  */
385 static int ll_xattr_cache_refill(struct inode *inode, struct lookup_intent *oit)
386 {
387         struct ll_sb_info *sbi = ll_i2sbi(inode);
388         struct ptlrpc_request *req = NULL;
389         const char *xdata, *xval, *xtail, *xvtail;
390         struct ll_inode_info *lli = ll_i2info(inode);
391         struct mdt_body *body;
392         __u32 *xsizes;
393         int rc = 0, i;
394
395         ENTRY;
396
397         rc = ll_xattr_find_get_lock(inode, oit, &req);
398         if (rc)
399                 GOTO(out_no_unlock, rc);
400
401         /* Do we have the data at this point? */
402         if (ll_xattr_cache_valid(lli)) {
403                 ll_stats_ops_tally(sbi, LPROC_LL_GETXATTR_HITS, 1);
404                 GOTO(out_maybe_drop, rc = 0);
405         }
406
407         /* Matched but no cache? Cancelled on error by a parallel refill. */
408         if (unlikely(req == NULL)) {
409                 CDEBUG(D_CACHE, "cancelled by a parallel getxattr\n");
410                 GOTO(out_maybe_drop, rc = -EIO);
411         }
412
413         if (oit->it_status < 0) {
414                 CDEBUG(D_CACHE, "getxattr intent returned %d for fid "DFID"\n",
415                        oit->it_status, PFID(ll_inode2fid(inode)));
416                 rc = oit->it_status;
417                 /* xattr data is so large that we don't want to cache it */
418                 if (rc == -ERANGE)
419                         rc = -EAGAIN;
420                 GOTO(out_destroy, rc);
421         }
422
423         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
424         if (body == NULL) {
425                 CERROR("no MDT BODY in the refill xattr reply\n");
426                 GOTO(out_destroy, rc = -EPROTO);
427         }
428         /* do not need swab xattr data */
429         xdata = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA,
430                                                 body->mbo_eadatasize);
431         xval = req_capsule_server_sized_get(&req->rq_pill, &RMF_EAVALS,
432                                                 body->mbo_aclsize);
433         xsizes = req_capsule_server_sized_get(&req->rq_pill, &RMF_EAVALS_LENS,
434                                               body->mbo_max_mdsize *
435                                               sizeof(__u32));
436         if (xdata == NULL || xval == NULL || xsizes == NULL) {
437                 CERROR("wrong setxattr reply\n");
438                 GOTO(out_destroy, rc = -EPROTO);
439         }
440
441         xtail = xdata + body->mbo_eadatasize;
442         xvtail = xval + body->mbo_aclsize;
443
444         CDEBUG(D_CACHE, "caching: xdata=%p xtail=%p\n", xdata, xtail);
445
446         ll_xattr_cache_init(lli);
447
448         for (i = 0; i < body->mbo_max_mdsize; i++) {
449                 CDEBUG(D_CACHE, "caching [%s]=%.*s\n", xdata, *xsizes, xval);
450                 /* Perform consistency checks: attr names and vals in pill */
451                 if (memchr(xdata, 0, xtail - xdata) == NULL) {
452                         CERROR("xattr protocol violation (names are broken)\n");
453                         rc = -EPROTO;
454                 } else if (xval + *xsizes > xvtail) {
455                         CERROR("xattr protocol violation (vals are broken)\n");
456                         rc = -EPROTO;
457                 } else if (OBD_FAIL_CHECK(OBD_FAIL_LLITE_XATTR_ENOMEM)) {
458                         rc = -ENOMEM;
459                 } else if (!strcmp(xdata, XATTR_NAME_ACL_ACCESS)) {
460                         /* Filter out ACL ACCESS since it's cached separately */
461                         CDEBUG(D_CACHE, "not caching %s\n",
462                                XATTR_NAME_ACL_ACCESS);
463                         rc = 0;
464                 } else if (!strcmp(xdata, "security.selinux")) {
465                         /* Filter out security.selinux, it is cached in slab */
466                         CDEBUG(D_CACHE, "not caching security.selinux\n");
467                         rc = 0;
468                 } else {
469                         rc = ll_xattr_cache_add(&lli->lli_xattrs, xdata, xval,
470                                                 *xsizes);
471                 }
472                 if (rc < 0) {
473                         ll_xattr_cache_destroy_locked(lli);
474                         GOTO(out_destroy, rc);
475                 }
476                 xdata += strlen(xdata) + 1;
477                 xval  += *xsizes;
478                 xsizes++;
479         }
480
481         if (xdata != xtail || xval != xvtail)
482                 CERROR("a hole in xattr data\n");
483
484         ll_set_lock_data(sbi->ll_md_exp, inode, oit, NULL);
485
486         GOTO(out_maybe_drop, rc);
487 out_maybe_drop:
488
489         ll_intent_drop_lock(oit);
490
491         if (rc != 0)
492                 up_write(&lli->lli_xattrs_list_rwsem);
493 out_no_unlock:
494         ptlrpc_req_finished(req);
495
496         return rc;
497
498 out_destroy:
499         up_write(&lli->lli_xattrs_list_rwsem);
500
501         ldlm_lock_decref_and_cancel((struct lustre_handle *)
502                                         &oit->it_lock_handle,
503                                         oit->it_lock_mode);
504
505         goto out_no_unlock;
506 }
507
508 /**
509  * Get an xattr value or list xattrs using the write-through cache.
510  *
511  * Get the xattr value (@valid has OBD_MD_FLXATTR set) of @name or
512  * list xattr names (@valid has OBD_MD_FLXATTRLS set) for @inode.
513  * The resulting value/list is stored in @buffer if the former
514  * is not larger than @size.
515  *
516  * \retval 0        no error occured
517  * \retval -EPROTO  network protocol error
518  * \retval -ENOMEM  not enough memory for the cache
519  * \retval -ERANGE  the buffer is not large enough
520  * \retval -ENODATA no such attr or the list is empty
521  */
522 int ll_xattr_cache_get(struct inode *inode,
523                         const char *name,
524                         char *buffer,
525                         size_t size,
526                         __u64 valid)
527 {
528         struct lookup_intent oit = { .it_op = IT_GETXATTR };
529         struct ll_inode_info *lli = ll_i2info(inode);
530         int rc = 0;
531
532         ENTRY;
533
534         LASSERT(!!(valid & OBD_MD_FLXATTR) ^ !!(valid & OBD_MD_FLXATTRLS));
535
536         down_read(&lli->lli_xattrs_list_rwsem);
537         if (!ll_xattr_cache_valid(lli)) {
538                 up_read(&lli->lli_xattrs_list_rwsem);
539                 rc = ll_xattr_cache_refill(inode, &oit);
540                 if (rc)
541                         RETURN(rc);
542                 downgrade_write(&lli->lli_xattrs_list_rwsem);
543         } else {
544                 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR_HITS, 1);
545         }
546
547         if (valid & OBD_MD_FLXATTR) {
548                 struct ll_xattr_entry *xattr;
549
550                 rc = ll_xattr_cache_find(&lli->lli_xattrs, name, &xattr);
551                 if (rc == 0) {
552                         rc = xattr->xe_vallen;
553                         /* zero size means we are only requested size in rc */
554                         if (size != 0) {
555                                 if (size >= xattr->xe_vallen)
556                                         memcpy(buffer, xattr->xe_value,
557                                                 xattr->xe_vallen);
558                                 else
559                                         rc = -ERANGE;
560                         }
561                 }
562         } else if (valid & OBD_MD_FLXATTRLS) {
563                 rc = ll_xattr_cache_list(&lli->lli_xattrs,
564                                          size ? buffer : NULL, size);
565         }
566
567         GOTO(out, rc);
568 out:
569         up_read(&lli->lli_xattrs_list_rwsem);
570
571         return rc;
572 }
573