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