Whamcloud - gitweb
9df1bfa24581e514799a4d761098bdecae7edc23
[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  * Author: Andrew Perepechko <Andrew_Perepechko@xyratex.com>
28  *
29  */
30
31 #define DEBUG_SUBSYSTEM S_LLITE
32
33 #include <linux/fs.h>
34 #include <linux/sched.h>
35 #include <linux/mm.h>
36 #include <obd_support.h>
37 #include <lustre_lite.h>
38 #include <lustre_dlm.h>
39 #include <lustre_ver.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         CFS_INIT_LIST_HEAD(&lli->lli_xattrs);
88         lli->lli_flags |= 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 or updates an xattr.
125  *
126  * Add @xattr_name attr with @xattr_val value and @xattr_val_len length,
127  * if the attribute already exists, then update its value.
128  *
129  * \retval 0       success
130  * \retval -ENOMEM if no memory could be allocated for the cached attr
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                 /* Found a cached EA, update it */
143
144                 if (xattr_val_len != xattr->xe_vallen) {
145                         char *val;
146                         OBD_ALLOC(val, xattr_val_len);
147                         if (val == NULL) {
148                                 CDEBUG(D_CACHE, "failed to allocate %u bytes "
149                                                 "for xattr %s update\n",
150                                                 xattr_val_len,
151                                                 xattr_name);
152                                 RETURN(-ENOMEM);
153                         }
154                         OBD_FREE(xattr->xe_value, xattr->xe_vallen);
155                         xattr->xe_value = val;
156                         xattr->xe_vallen = xattr_val_len;
157                 }
158                 memcpy(xattr->xe_value, xattr_val, xattr_val_len);
159
160                 CDEBUG(D_CACHE, "update: [%s]=%.*s\n", xattr_name,
161                         xattr_val_len, xattr_val);
162
163                 RETURN(0);
164         }
165
166         OBD_SLAB_ALLOC_PTR_GFP(xattr, xattr_kmem, __GFP_IO);
167         if (xattr == NULL) {
168                 CDEBUG(D_CACHE, "failed to allocate xattr\n");
169                 RETURN(-ENOMEM);
170         }
171
172         xattr->xe_namelen = strlen(xattr_name) + 1;
173
174         OBD_ALLOC(xattr->xe_name, xattr->xe_namelen);
175         if (!xattr->xe_name) {
176                 CDEBUG(D_CACHE, "failed to alloc xattr name %u\n",
177                        xattr->xe_namelen);
178                 goto err_name;
179         }
180         OBD_ALLOC(xattr->xe_value, xattr_val_len);
181         if (!xattr->xe_value) {
182                 CDEBUG(D_CACHE, "failed to alloc xattr value %d\n",
183                        xattr_val_len);
184                 goto err_value;
185         }
186
187         memcpy(xattr->xe_name, xattr_name, xattr->xe_namelen);
188         memcpy(xattr->xe_value, xattr_val, xattr_val_len);
189         xattr->xe_vallen = xattr_val_len;
190         list_add(&xattr->xe_list, cache);
191
192         CDEBUG(D_CACHE, "set: [%s]=%.*s\n", xattr_name,
193                 xattr_val_len, xattr_val);
194
195         RETURN(0);
196 err_value:
197         OBD_FREE(xattr->xe_name, xattr->xe_namelen);
198 err_name:
199         OBD_SLAB_FREE_PTR(xattr, xattr_kmem);
200
201         RETURN(-ENOMEM);
202 }
203
204 /**
205  * This removes an extended attribute from cache.
206  *
207  * Remove @xattr_name attribute from @cache.
208  *
209  * \retval 0        success
210  * \retval -ENODATA if @xattr_name is not cached
211  */
212 static int ll_xattr_cache_del(struct list_head *cache,
213                               const char *xattr_name)
214 {
215         struct ll_xattr_entry *xattr;
216
217         ENTRY;
218
219         CDEBUG(D_CACHE, "del xattr: %s\n", xattr_name);
220
221         if (ll_xattr_cache_find(cache, xattr_name, &xattr) == 0) {
222                 list_del(&xattr->xe_list);
223                 OBD_FREE(xattr->xe_name, xattr->xe_namelen);
224                 OBD_FREE(xattr->xe_value, xattr->xe_vallen);
225                 OBD_SLAB_FREE_PTR(xattr, xattr_kmem);
226
227                 RETURN(0);
228         }
229
230         RETURN(-ENODATA);
231 }
232
233 /**
234  * This iterates cached extended attributes.
235  *
236  * Walk over cached attributes in @cache and
237  * fill in @xld_buffer or only calculate buffer
238  * size if @xld_buffer is NULL.
239  *
240  * \retval >= 0     buffer list size
241  * \retval -ENODATA if the list cannot fit @xld_size buffer
242  */
243 static int ll_xattr_cache_list(struct list_head *cache,
244                                char *xld_buffer,
245                                int xld_size)
246 {
247         struct ll_xattr_entry *xattr, *tmp;
248         int xld_tail = 0;
249
250         ENTRY;
251
252         list_for_each_entry_safe(xattr, tmp, cache, xe_list) {
253                 CDEBUG(D_CACHE, "list: buffer=%p[%d] name=%s\n",
254                         xld_buffer, xld_tail, xattr->xe_name);
255
256                 if (xld_buffer) {
257                         xld_size -= xattr->xe_namelen;
258                         if (xld_size < 0)
259                                 break;
260                         memcpy(&xld_buffer[xld_tail],
261                                xattr->xe_name, xattr->xe_namelen);
262                 }
263                 xld_tail += xattr->xe_namelen;
264         }
265
266         if (xld_size < 0)
267                 RETURN(-ERANGE);
268
269         RETURN(xld_tail);
270 }
271
272 /**
273  * Check if the xattr cache is initialized (filled).
274  *
275  * \retval 0 @cache is not initialized
276  * \retval 1 @cache is initialized
277  */
278 int ll_xattr_cache_valid(struct ll_inode_info *lli)
279 {
280         return !!(lli->lli_flags & LLIF_XATTR_CACHE);
281 }
282
283 /**
284  * This finalizes the xattr cache.
285  *
286  * Free all xattr memory. @lli is the inode info pointer.
287  *
288  * \retval 0 no error occured
289  */
290 static int ll_xattr_cache_destroy_locked(struct ll_inode_info *lli)
291 {
292         ENTRY;
293
294         if (!ll_xattr_cache_valid(lli))
295                 RETURN(0);
296
297         while (ll_xattr_cache_del(&lli->lli_xattrs, NULL) == 0)
298                 /* empty loop */ ;
299         lli->lli_flags &= ~LLIF_XATTR_CACHE;
300
301         RETURN(0);
302 }
303
304 int ll_xattr_cache_destroy(struct inode *inode)
305 {
306         struct ll_inode_info *lli = ll_i2info(inode);
307         int rc;
308
309         ENTRY;
310
311         down_write(&lli->lli_xattrs_list_rwsem);
312         rc = ll_xattr_cache_destroy_locked(lli);
313         up_write(&lli->lli_xattrs_list_rwsem);
314
315         RETURN(rc);
316 }
317
318 /**
319  * Match or enqueue a PR or PW LDLM lock.
320  *
321  * Find or request an LDLM lock with xattr data.
322  * Since LDLM does not provide API for atomic match_or_enqueue,
323  * the function handles it with a separate enq lock.
324  * If successful, the function exits with the list lock held.
325  *
326  * \retval 0       no error occured
327  * \retval -ENOMEM not enough memory
328  */
329 static int ll_xattr_find_get_lock(struct inode *inode,
330                                   struct lookup_intent *oit,
331                                   struct ptlrpc_request **req)
332 {
333         ldlm_mode_t mode;
334         struct lustre_handle lockh = { 0 };
335         struct md_op_data *op_data;
336         struct ll_inode_info *lli = ll_i2info(inode);
337         struct ldlm_enqueue_info einfo = { .ei_type = LDLM_IBITS,
338                                            .ei_mode = it_to_lock_mode(oit),
339                                            .ei_cb_bl = ll_md_blocking_ast,
340                                            .ei_cb_cp = ldlm_completion_ast };
341         struct ll_sb_info *sbi = ll_i2sbi(inode);
342         struct obd_export *exp = sbi->ll_md_exp;
343         int rc;
344
345         ENTRY;
346
347         mutex_lock(&lli->lli_xattrs_enq_lock);
348         /* Try matching first. */
349         mode = ll_take_md_lock(inode, MDS_INODELOCK_XATTR, &lockh, 0,
350                                oit->it_op == IT_SETXATTR ? LCK_PW :
351                                                            (LCK_PR | LCK_PW));
352         if (mode != 0) {
353                 /* fake oit in mdc_revalidate_lock() manner */
354                 oit->d.lustre.it_lock_handle = lockh.cookie;
355                 oit->d.lustre.it_lock_mode = mode;
356                 goto out;
357         }
358
359         /* Enqueue if the lock isn't cached locally. */
360         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
361                                      LUSTRE_OPC_ANY, NULL);
362         if (IS_ERR(op_data)) {
363                 mutex_unlock(&lli->lli_xattrs_enq_lock);
364                 RETURN(PTR_ERR(op_data));
365         }
366
367         op_data->op_valid = OBD_MD_FLXATTR | OBD_MD_FLXATTRLS |
368                             OBD_MD_FLXATTRLOCKED;
369 #ifdef CONFIG_FS_POSIX_ACL
370         /* If working with ACLs, we would like to cache local ACLs */
371         if (sbi->ll_flags & LL_SBI_RMT_CLIENT)
372                 op_data->op_valid |= OBD_MD_FLRMTLGETFACL;
373 #endif
374
375         rc = md_enqueue(exp, &einfo, oit, op_data, &lockh, NULL, 0, NULL, 0);
376         ll_finish_md_op_data(op_data);
377
378         if (rc < 0) {
379                 CDEBUG(D_CACHE, "md_intent_lock failed with %d for fid "DFID"\n",
380                        rc, PFID(ll_inode2fid(inode)));
381                 mutex_unlock(&lli->lli_xattrs_enq_lock);
382                 RETURN(rc);
383         }
384
385         *req = (struct ptlrpc_request *)oit->d.lustre.it_data;
386 out:
387         down_write(&lli->lli_xattrs_list_rwsem);
388         mutex_unlock(&lli->lli_xattrs_enq_lock);
389
390         RETURN(0);
391 }
392
393 /**
394  * Refill the xattr cache.
395  *
396  * Fetch and cache the whole of xattrs for @inode, acquiring
397  * a read or a write xattr lock depending on operation in @oit.
398  * Intent is dropped on exit unless the operation is setxattr.
399  *
400  * \retval 0       no error occured
401  * \retval -EPROTO network protocol error
402  * \retval -ENOMEM not enough memory for the cache
403  */
404 static int ll_xattr_cache_refill(struct inode *inode, struct lookup_intent *oit)
405 {
406         struct ll_sb_info *sbi = ll_i2sbi(inode);
407         struct ptlrpc_request *req = NULL;
408         const char *xdata, *xval, *xtail, *xvtail;
409         struct ll_inode_info *lli = ll_i2info(inode);
410         struct mdt_body *body;
411         __u32 *xsizes;
412         int rc = 0, i;
413
414         ENTRY;
415
416         rc = ll_xattr_find_get_lock(inode, oit, &req);
417         if (rc)
418                 GOTO(out_no_unlock, rc);
419
420         /* Do we have the data at this point? */
421         if (ll_xattr_cache_valid(lli)) {
422                 ll_stats_ops_tally(sbi, LPROC_LL_GETXATTR_HITS, 1);
423                 GOTO(out_maybe_drop, rc = 0);
424         }
425
426         /* Matched but no cache? Cancelled on error by a parallel refill. */
427         if (unlikely(req == NULL)) {
428                 CDEBUG(D_CACHE, "cancelled by a parallel getxattr\n");
429                 GOTO(out_maybe_drop, rc = -EIO);
430         }
431
432         if (oit->d.lustre.it_status < 0) {
433                 CDEBUG(D_CACHE, "getxattr intent returned %d for fid "DFID"\n",
434                        oit->d.lustre.it_status, PFID(ll_inode2fid(inode)));
435                 GOTO(out_destroy, rc = oit->d.lustre.it_status);
436         }
437
438         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
439         if (body == NULL) {
440                 CERROR("no MDT BODY in the refill xattr reply\n");
441                 GOTO(out_destroy, rc = -EPROTO);
442         }
443         /* do not need swab xattr data */
444         xdata = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA,
445                                                 body->eadatasize);
446         xval = req_capsule_server_sized_get(&req->rq_pill, &RMF_EAVALS,
447                                                 body->aclsize);
448         xsizes = req_capsule_server_sized_get(&req->rq_pill, &RMF_EAVALS_LENS,
449                                               body->max_mdsize * sizeof(__u32));
450         if (xdata == NULL || xval == NULL || xsizes == NULL) {
451                 CERROR("wrong setxattr reply\n");
452                 GOTO(out_destroy, rc = -EPROTO);
453         }
454
455         xtail = xdata + body->eadatasize;
456         xvtail = xval + body->aclsize;
457
458         CDEBUG(D_CACHE, "caching: xdata=%p xtail=%p\n", xdata, xtail);
459
460         ll_xattr_cache_init(lli);
461
462         for (i = 0; i < body->max_mdsize; i++) {
463                 CDEBUG(D_CACHE, "caching [%s]=%.*s\n", xdata, *xsizes, xval);
464                 /* Perform consistency checks: attr names and vals in pill */
465                 if (memchr(xdata, 0, xtail - xdata) == NULL) {
466                         CERROR("xattr protocol violation (names are broken)\n");
467                         rc = -EPROTO;
468                 } else if (xval + *xsizes > xvtail) {
469                         CERROR("xattr protocol violation (vals are broken)\n");
470                         rc = -EPROTO;
471                 } else if (OBD_FAIL_CHECK(OBD_FAIL_LLITE_XATTR_ENOMEM)) {
472                         rc = -ENOMEM;
473                 } else {
474                         rc = ll_xattr_cache_add(&lli->lli_xattrs, xdata, xval,
475                                                 *xsizes);
476                 }
477                 if (rc < 0) {
478                         ll_xattr_cache_destroy_locked(lli);
479                         GOTO(out_destroy, rc);
480                 }
481                 xdata += strlen(xdata) + 1;
482                 xval  += *xsizes;
483                 xsizes++;
484         }
485
486         if (xdata != xtail || xval != xvtail)
487                 CERROR("a hole in xattr data\n");
488
489         ll_set_lock_data(sbi->ll_md_exp, inode, oit, NULL);
490
491         GOTO(out_maybe_drop, rc);
492 out_maybe_drop:
493         /* drop lock on error or getxattr */
494         if (rc != 0 || oit->it_op != IT_SETXATTR)
495                 ll_intent_drop_lock(oit);
496
497         if (rc != 0)
498                 up_write(&lli->lli_xattrs_list_rwsem);
499 out_no_unlock:
500         ptlrpc_req_finished(req);
501
502         return rc;
503
504 out_destroy:
505         up_write(&lli->lli_xattrs_list_rwsem);
506
507         ldlm_lock_decref_and_cancel((struct lustre_handle *)
508                                         &oit->d.lustre.it_lock_handle,
509                                         oit->d.lustre.it_lock_mode);
510
511         goto out_no_unlock;
512 }
513
514 /**
515  * Get an xattr value or list xattrs using the write-through cache.
516  *
517  * Get the xattr value (@valid has OBD_MD_FLXATTR set) of @name or
518  * list xattr names (@valid has OBD_MD_FLXATTRLS set) for @inode.
519  * The resulting value/list is stored in @buffer if the former
520  * is not larger than @size.
521  *
522  * \retval 0        no error occured
523  * \retval -EPROTO  network protocol error
524  * \retval -ENOMEM  not enough memory for the cache
525  * \retval -ERANGE  the buffer is not large enough
526  * \retval -ENODATA no such attr or the list is empty
527  */
528 int ll_xattr_cache_get(struct inode *inode,
529                         const char *name,
530                         char *buffer,
531                         size_t size,
532                         __u64 valid)
533 {
534         struct lookup_intent oit = { .it_op = IT_GETXATTR };
535         struct ll_inode_info *lli = ll_i2info(inode);
536         int rc = 0;
537
538         ENTRY;
539
540         LASSERT(!!(valid & OBD_MD_FLXATTR) ^ !!(valid & OBD_MD_FLXATTRLS));
541
542         down_read(&lli->lli_xattrs_list_rwsem);
543         if (!ll_xattr_cache_valid(lli)) {
544                 up_read(&lli->lli_xattrs_list_rwsem);
545                 rc = ll_xattr_cache_refill(inode, &oit);
546                 if (rc)
547                         RETURN(rc);
548                 downgrade_write(&lli->lli_xattrs_list_rwsem);
549         } else {
550                 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR_HITS, 1);
551         }
552
553         if (valid & OBD_MD_FLXATTR) {
554                 struct ll_xattr_entry *xattr;
555
556                 rc = ll_xattr_cache_find(&lli->lli_xattrs, name, &xattr);
557                 if (rc == 0) {
558                         rc = xattr->xe_vallen;
559                         /* zero size means we are only requested size in rc */
560                         if (size != 0) {
561                                 if (size >= xattr->xe_vallen)
562                                         memcpy(buffer, xattr->xe_value,
563                                                 xattr->xe_vallen);
564                                 else
565                                         rc = -ERANGE;
566                         }
567                 }
568         } else if (valid & OBD_MD_FLXATTRLS) {
569                 rc = ll_xattr_cache_list(&lli->lli_xattrs,
570                                          size ? buffer : NULL, size);
571         }
572
573         GOTO(out, rc);
574 out:
575         up_read(&lli->lli_xattrs_list_rwsem);
576
577         return rc;
578 }
579
580
581 /**
582  * Set/update an xattr value or remove xattr using the write-through cache.
583  *
584  * Set/update the xattr value (if @valid has OBD_MD_FLXATTR) of @name to @newval
585  * or
586  * remove the xattr @name (@valid has OBD_MD_FLXATTRRM set) from @inode.
587  * @flags is either XATTR_CREATE or XATTR_REPLACE as defined by setxattr(2)
588  *
589  * \retval 0        no error occured
590  * \retval -EPROTO  network protocol error
591  * \retval -ENOMEM  not enough memory for the cache
592  * \retval -ERANGE  the buffer is not large enough
593  * \retval -ENODATA no such attr (in the removal case)
594  */
595 int ll_xattr_cache_update(struct inode *inode,
596                         const char *name,
597                         const char *newval,
598                         size_t size,
599                         __u64 valid,
600                         int flags)
601 {
602         struct lookup_intent oit = { .it_op = IT_SETXATTR };
603         struct ll_sb_info *sbi = ll_i2sbi(inode);
604         struct ptlrpc_request *req = NULL;
605         struct ll_inode_info *lli = ll_i2info(inode);
606         struct obd_capa *oc;
607         int rc;
608
609         ENTRY;
610
611         LASSERT(!!(valid & OBD_MD_FLXATTR) ^ !!(valid & OBD_MD_FLXATTRRM));
612
613         rc = ll_xattr_cache_refill(inode, &oit);
614         if (rc)
615                 RETURN(rc);
616
617         oc = ll_mdscapa_get(inode);
618         rc = md_setxattr(sbi->ll_md_exp, ll_inode2fid(inode), oc,
619                         valid | OBD_MD_FLXATTRLOCKED, name, newval,
620                         size, 0, flags, ll_i2suppgid(inode), &req);
621         capa_put(oc);
622
623         if (rc) {
624                 ll_intent_drop_lock(&oit);
625                 GOTO(out, rc);
626         }
627
628         if (valid & OBD_MD_FLXATTR)
629                 rc = ll_xattr_cache_add(&lli->lli_xattrs, name, newval, size);
630         else if (valid & OBD_MD_FLXATTRRM)
631                 rc = ll_xattr_cache_del(&lli->lli_xattrs, name);
632
633         ll_intent_drop_lock(&oit);
634         GOTO(out, rc);
635 out:
636         up_write(&lli->lli_xattrs_list_rwsem);
637         ptlrpc_req_finished(req);
638
639         return rc;
640 }