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