3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
18 * Please visit http://www.xyratex.com/contact if you need additional
19 * information or have any questions.
25 * Copyright 2012 Xyratex Technology Limited
27 * Author: Andrew Perepechko <Andrew_Perepechko@xyratex.com>
31 #define DEBUG_SUBSYSTEM S_LLITE
34 #include <linux/sched.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"
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.
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 */
54 static struct kmem_cache *xattr_kmem;
55 static struct lu_kmem_descr xattr_caches[] = {
57 .ckd_cache = &xattr_kmem,
58 .ckd_name = "xattr_kmem",
59 .ckd_size = sizeof(struct ll_xattr_entry)
66 int ll_xattr_init(void)
68 return lu_kmem_init(xattr_caches);
71 void ll_xattr_fini(void)
73 lu_kmem_fini(xattr_caches);
77 * Initializes xattr cache for an inode.
79 * This initializes the xattr list and marks cache presence.
81 static void ll_xattr_cache_init(struct ll_inode_info *lli)
87 CFS_INIT_LIST_HEAD(&lli->lli_xattrs);
88 lli->lli_flags |= LLIF_XATTR_CACHE;
92 * This looks for a specific extended attribute.
94 * Find in @cache and return @xattr_name attribute in @xattr,
95 * for the NULL @xattr_name return the first cached @xattr.
98 * \retval -ENODATA if not found
100 static int ll_xattr_cache_find(struct list_head *cache,
101 const char *xattr_name,
102 struct ll_xattr_entry **xattr)
104 struct ll_xattr_entry *entry;
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) {
113 CDEBUG(D_CACHE, "find: [%s]=%.*s\n",
114 entry->xe_name, entry->xe_vallen,
124 * This adds an xattr.
126 * Add @xattr_name attr with @xattr_val value and @xattr_val_len length,
129 * \retval -ENOMEM if no memory could be allocated for the cached attr
130 * \retval -EPROTO if duplicate xattr is being added
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)
137 struct ll_xattr_entry *xattr;
141 if (ll_xattr_cache_find(cache, xattr_name, &xattr) == 0) {
142 CDEBUG(D_CACHE, "duplicate xattr: [%s]\n", xattr_name);
146 OBD_SLAB_ALLOC_PTR_GFP(xattr, xattr_kmem, __GFP_IO);
148 CDEBUG(D_CACHE, "failed to allocate xattr\n");
152 xattr->xe_namelen = strlen(xattr_name) + 1;
154 OBD_ALLOC(xattr->xe_name, xattr->xe_namelen);
155 if (!xattr->xe_name) {
156 CDEBUG(D_CACHE, "failed to alloc xattr name %u\n",
160 OBD_ALLOC(xattr->xe_value, xattr_val_len);
161 if (!xattr->xe_value) {
162 CDEBUG(D_CACHE, "failed to alloc xattr value %d\n",
167 memcpy(xattr->xe_name, xattr_name, xattr->xe_namelen);
168 memcpy(xattr->xe_value, xattr_val, xattr_val_len);
169 xattr->xe_vallen = xattr_val_len;
170 list_add(&xattr->xe_list, cache);
172 CDEBUG(D_CACHE, "set: [%s]=%.*s\n", xattr_name,
173 xattr_val_len, xattr_val);
177 OBD_FREE(xattr->xe_name, xattr->xe_namelen);
179 OBD_SLAB_FREE_PTR(xattr, xattr_kmem);
185 * This removes an extended attribute from cache.
187 * Remove @xattr_name attribute from @cache.
190 * \retval -ENODATA if @xattr_name is not cached
192 static int ll_xattr_cache_del(struct list_head *cache,
193 const char *xattr_name)
195 struct ll_xattr_entry *xattr;
199 CDEBUG(D_CACHE, "del xattr: %s\n", xattr_name);
201 if (ll_xattr_cache_find(cache, xattr_name, &xattr) == 0) {
202 list_del(&xattr->xe_list);
203 OBD_FREE(xattr->xe_name, xattr->xe_namelen);
204 OBD_FREE(xattr->xe_value, xattr->xe_vallen);
205 OBD_SLAB_FREE_PTR(xattr, xattr_kmem);
214 * This iterates cached extended attributes.
216 * Walk over cached attributes in @cache and
217 * fill in @xld_buffer or only calculate buffer
218 * size if @xld_buffer is NULL.
220 * \retval >= 0 buffer list size
221 * \retval -ENODATA if the list cannot fit @xld_size buffer
223 static int ll_xattr_cache_list(struct list_head *cache,
227 struct ll_xattr_entry *xattr, *tmp;
232 list_for_each_entry_safe(xattr, tmp, cache, xe_list) {
233 CDEBUG(D_CACHE, "list: buffer=%p[%d] name=%s\n",
234 xld_buffer, xld_tail, xattr->xe_name);
237 xld_size -= xattr->xe_namelen;
240 memcpy(&xld_buffer[xld_tail],
241 xattr->xe_name, xattr->xe_namelen);
243 xld_tail += xattr->xe_namelen;
253 * Check if the xattr cache is initialized (filled).
255 * \retval 0 @cache is not initialized
256 * \retval 1 @cache is initialized
258 int ll_xattr_cache_valid(struct ll_inode_info *lli)
260 return !!(lli->lli_flags & LLIF_XATTR_CACHE);
264 * This finalizes the xattr cache.
266 * Free all xattr memory. @lli is the inode info pointer.
268 * \retval 0 no error occured
270 static int ll_xattr_cache_destroy_locked(struct ll_inode_info *lli)
274 if (!ll_xattr_cache_valid(lli))
277 while (ll_xattr_cache_del(&lli->lli_xattrs, NULL) == 0)
279 lli->lli_flags &= ~LLIF_XATTR_CACHE;
284 int ll_xattr_cache_destroy(struct inode *inode)
286 struct ll_inode_info *lli = ll_i2info(inode);
291 down_write(&lli->lli_xattrs_list_rwsem);
292 rc = ll_xattr_cache_destroy_locked(lli);
293 up_write(&lli->lli_xattrs_list_rwsem);
299 * Match or enqueue a PR lock.
301 * Find or request an LDLM lock with xattr data.
302 * Since LDLM does not provide API for atomic match_or_enqueue,
303 * the function handles it with a separate enq lock.
304 * If successful, the function exits with the list lock held.
306 * \retval 0 no error occured
307 * \retval -ENOMEM not enough memory
309 static int ll_xattr_find_get_lock(struct inode *inode,
310 struct lookup_intent *oit,
311 struct ptlrpc_request **req)
314 struct lustre_handle lockh = { 0 };
315 struct md_op_data *op_data;
316 struct ll_inode_info *lli = ll_i2info(inode);
317 struct ldlm_enqueue_info einfo = { .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 struct ll_sb_info *sbi = ll_i2sbi(inode);
322 struct obd_export *exp = sbi->ll_md_exp;
327 mutex_lock(&lli->lli_xattrs_enq_lock);
328 /* Try matching first. */
329 mode = ll_take_md_lock(inode, MDS_INODELOCK_XATTR, &lockh, 0, LCK_PR);
331 /* fake oit in mdc_revalidate_lock() manner */
332 oit->d.lustre.it_lock_handle = lockh.cookie;
333 oit->d.lustre.it_lock_mode = mode;
337 /* Enqueue if the lock isn't cached locally. */
338 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
339 LUSTRE_OPC_ANY, NULL);
340 if (IS_ERR(op_data)) {
341 mutex_unlock(&lli->lli_xattrs_enq_lock);
342 RETURN(PTR_ERR(op_data));
345 op_data->op_valid = OBD_MD_FLXATTR | OBD_MD_FLXATTRLS;
347 rc = md_enqueue(exp, &einfo, oit, op_data, &lockh, NULL, 0, NULL, 0);
348 ll_finish_md_op_data(op_data);
351 CDEBUG(D_CACHE, "md_intent_lock failed with %d for fid "DFID"\n",
352 rc, PFID(ll_inode2fid(inode)));
353 mutex_unlock(&lli->lli_xattrs_enq_lock);
357 *req = (struct ptlrpc_request *)oit->d.lustre.it_data;
359 down_write(&lli->lli_xattrs_list_rwsem);
360 mutex_unlock(&lli->lli_xattrs_enq_lock);
366 * Refill the xattr cache.
368 * Fetch and cache the whole of xattrs for @inode, acquiring
369 * a read or a write xattr lock depending on operation in @oit.
370 * Intent is dropped on exit unless the operation is setxattr.
372 * \retval 0 no error occured
373 * \retval -EPROTO network protocol error
374 * \retval -ENOMEM not enough memory for the cache
376 static int ll_xattr_cache_refill(struct inode *inode, struct lookup_intent *oit)
378 struct ll_sb_info *sbi = ll_i2sbi(inode);
379 struct ptlrpc_request *req = NULL;
380 const char *xdata, *xval, *xtail, *xvtail;
381 struct ll_inode_info *lli = ll_i2info(inode);
382 struct mdt_body *body;
388 rc = ll_xattr_find_get_lock(inode, oit, &req);
390 GOTO(out_no_unlock, rc);
392 /* Do we have the data at this point? */
393 if (ll_xattr_cache_valid(lli)) {
394 ll_stats_ops_tally(sbi, LPROC_LL_GETXATTR_HITS, 1);
395 GOTO(out_maybe_drop, rc = 0);
398 /* Matched but no cache? Cancelled on error by a parallel refill. */
399 if (unlikely(req == NULL)) {
400 CDEBUG(D_CACHE, "cancelled by a parallel getxattr\n");
401 GOTO(out_maybe_drop, rc = -EIO);
404 if (oit->d.lustre.it_status < 0) {
405 CDEBUG(D_CACHE, "getxattr intent returned %d for fid "DFID"\n",
406 oit->d.lustre.it_status, PFID(ll_inode2fid(inode)));
407 rc = oit->d.lustre.it_status;
408 /* xattr data is so large that we don't want to cache it */
411 GOTO(out_destroy, rc);
414 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
416 CERROR("no MDT BODY in the refill xattr reply\n");
417 GOTO(out_destroy, rc = -EPROTO);
419 /* do not need swab xattr data */
420 xdata = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA,
422 xval = req_capsule_server_sized_get(&req->rq_pill, &RMF_EAVALS,
424 xsizes = req_capsule_server_sized_get(&req->rq_pill, &RMF_EAVALS_LENS,
425 body->max_mdsize * sizeof(__u32));
426 if (xdata == NULL || xval == NULL || xsizes == NULL) {
427 CERROR("wrong setxattr reply\n");
428 GOTO(out_destroy, rc = -EPROTO);
431 xtail = xdata + body->eadatasize;
432 xvtail = xval + body->aclsize;
434 CDEBUG(D_CACHE, "caching: xdata=%p xtail=%p\n", xdata, xtail);
436 ll_xattr_cache_init(lli);
438 for (i = 0; i < body->max_mdsize; i++) {
439 CDEBUG(D_CACHE, "caching [%s]=%.*s\n", xdata, *xsizes, xval);
440 /* Perform consistency checks: attr names and vals in pill */
441 if (memchr(xdata, 0, xtail - xdata) == NULL) {
442 CERROR("xattr protocol violation (names are broken)\n");
444 } else if (xval + *xsizes > xvtail) {
445 CERROR("xattr protocol violation (vals are broken)\n");
447 } else if (OBD_FAIL_CHECK(OBD_FAIL_LLITE_XATTR_ENOMEM)) {
449 } else if (!strcmp(xdata, XATTR_NAME_ACL_ACCESS)) {
450 /* Filter out ACL ACCESS since it's cached separately */
451 CDEBUG(D_CACHE, "not caching %s\n",
452 XATTR_NAME_ACL_ACCESS);
455 rc = ll_xattr_cache_add(&lli->lli_xattrs, xdata, xval,
459 ll_xattr_cache_destroy_locked(lli);
460 GOTO(out_destroy, rc);
462 xdata += strlen(xdata) + 1;
467 if (xdata != xtail || xval != xvtail)
468 CERROR("a hole in xattr data\n");
470 ll_set_lock_data(sbi->ll_md_exp, inode, oit, NULL);
472 GOTO(out_maybe_drop, rc);
475 ll_intent_drop_lock(oit);
478 up_write(&lli->lli_xattrs_list_rwsem);
480 ptlrpc_req_finished(req);
485 up_write(&lli->lli_xattrs_list_rwsem);
487 ldlm_lock_decref_and_cancel((struct lustre_handle *)
488 &oit->d.lustre.it_lock_handle,
489 oit->d.lustre.it_lock_mode);
495 * Get an xattr value or list xattrs using the write-through cache.
497 * Get the xattr value (@valid has OBD_MD_FLXATTR set) of @name or
498 * list xattr names (@valid has OBD_MD_FLXATTRLS set) for @inode.
499 * The resulting value/list is stored in @buffer if the former
500 * is not larger than @size.
502 * \retval 0 no error occured
503 * \retval -EPROTO network protocol error
504 * \retval -ENOMEM not enough memory for the cache
505 * \retval -ERANGE the buffer is not large enough
506 * \retval -ENODATA no such attr or the list is empty
508 int ll_xattr_cache_get(struct inode *inode,
514 struct lookup_intent oit = { .it_op = IT_GETXATTR };
515 struct ll_inode_info *lli = ll_i2info(inode);
520 LASSERT(!!(valid & OBD_MD_FLXATTR) ^ !!(valid & OBD_MD_FLXATTRLS));
522 down_read(&lli->lli_xattrs_list_rwsem);
523 if (!ll_xattr_cache_valid(lli)) {
524 up_read(&lli->lli_xattrs_list_rwsem);
525 rc = ll_xattr_cache_refill(inode, &oit);
528 downgrade_write(&lli->lli_xattrs_list_rwsem);
530 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR_HITS, 1);
533 if (valid & OBD_MD_FLXATTR) {
534 struct ll_xattr_entry *xattr;
536 rc = ll_xattr_cache_find(&lli->lli_xattrs, name, &xattr);
538 rc = xattr->xe_vallen;
539 /* zero size means we are only requested size in rc */
541 if (size >= xattr->xe_vallen)
542 memcpy(buffer, xattr->xe_value,
548 } else if (valid & OBD_MD_FLXATTRLS) {
549 rc = ll_xattr_cache_list(&lli->lli_xattrs,
550 size ? buffer : NULL, size);
555 up_read(&lli->lli_xattrs_list_rwsem);