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_dlm.h>
38 #include <lustre_ver.h>
39 #include "llite_internal.h"
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.
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 */
53 static struct kmem_cache *xattr_kmem;
54 static struct lu_kmem_descr xattr_caches[] = {
56 .ckd_cache = &xattr_kmem,
57 .ckd_name = "xattr_kmem",
58 .ckd_size = sizeof(struct ll_xattr_entry)
65 int ll_xattr_init(void)
67 return lu_kmem_init(xattr_caches);
70 void ll_xattr_fini(void)
72 lu_kmem_fini(xattr_caches);
76 * Initializes xattr cache for an inode.
78 * This initializes the xattr list and marks cache presence.
80 static void ll_xattr_cache_init(struct ll_inode_info *lli)
86 INIT_LIST_HEAD(&lli->lli_xattrs);
87 lli->lli_flags |= LLIF_XATTR_CACHE;
91 * This looks for a specific extended attribute.
93 * Find in @cache and return @xattr_name attribute in @xattr,
94 * for the NULL @xattr_name return the first cached @xattr.
97 * \retval -ENODATA if not found
99 static int ll_xattr_cache_find(struct list_head *cache,
100 const char *xattr_name,
101 struct ll_xattr_entry **xattr)
103 struct ll_xattr_entry *entry;
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) {
112 CDEBUG(D_CACHE, "find: [%s]=%.*s\n",
113 entry->xe_name, entry->xe_vallen,
123 * This adds an xattr.
125 * Add @xattr_name attr with @xattr_val value and @xattr_val_len length,
128 * \retval -ENOMEM if no memory could be allocated for the cached attr
129 * \retval -EPROTO if duplicate xattr is being added
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)
136 struct ll_xattr_entry *xattr;
140 if (ll_xattr_cache_find(cache, xattr_name, &xattr) == 0) {
141 CDEBUG(D_CACHE, "duplicate xattr: [%s]\n", xattr_name);
145 OBD_SLAB_ALLOC_PTR_GFP(xattr, xattr_kmem, GFP_NOFS);
147 CDEBUG(D_CACHE, "failed to allocate xattr\n");
151 xattr->xe_namelen = strlen(xattr_name) + 1;
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",
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",
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);
171 CDEBUG(D_CACHE, "set: [%s]=%.*s\n", xattr_name,
172 xattr_val_len, xattr_val);
176 OBD_FREE(xattr->xe_name, xattr->xe_namelen);
178 OBD_SLAB_FREE_PTR(xattr, xattr_kmem);
184 * This removes an extended attribute from cache.
186 * Remove @xattr_name attribute from @cache.
189 * \retval -ENODATA if @xattr_name is not cached
191 static int ll_xattr_cache_del(struct list_head *cache,
192 const char *xattr_name)
194 struct ll_xattr_entry *xattr;
198 CDEBUG(D_CACHE, "del xattr: %s\n", xattr_name);
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);
213 * This iterates cached extended attributes.
215 * Walk over cached attributes in @cache and
216 * fill in @xld_buffer or only calculate buffer
217 * size if @xld_buffer is NULL.
219 * \retval >= 0 buffer list size
220 * \retval -ENODATA if the list cannot fit @xld_size buffer
222 static int ll_xattr_cache_list(struct list_head *cache,
226 struct ll_xattr_entry *xattr, *tmp;
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);
236 xld_size -= xattr->xe_namelen;
239 memcpy(&xld_buffer[xld_tail],
240 xattr->xe_name, xattr->xe_namelen);
242 xld_tail += xattr->xe_namelen;
252 * Check if the xattr cache is initialized (filled).
254 * \retval 0 @cache is not initialized
255 * \retval 1 @cache is initialized
257 static int ll_xattr_cache_valid(struct ll_inode_info *lli)
259 return !!(lli->lli_flags & LLIF_XATTR_CACHE);
263 * This finalizes the xattr cache.
265 * Free all xattr memory. @lli is the inode info pointer.
267 * \retval 0 no error occured
269 static int ll_xattr_cache_destroy_locked(struct ll_inode_info *lli)
273 if (!ll_xattr_cache_valid(lli))
276 while (ll_xattr_cache_del(&lli->lli_xattrs, NULL) == 0)
278 lli->lli_flags &= ~LLIF_XATTR_CACHE;
283 int ll_xattr_cache_destroy(struct inode *inode)
285 struct ll_inode_info *lli = ll_i2info(inode);
290 down_write(&lli->lli_xattrs_list_rwsem);
291 rc = ll_xattr_cache_destroy_locked(lli);
292 up_write(&lli->lli_xattrs_list_rwsem);
298 * Match or enqueue a PR lock.
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.
305 * \retval 0 no error occured
306 * \retval -ENOMEM not enough memory
308 static int ll_xattr_find_get_lock(struct inode *inode,
309 struct lookup_intent *oit,
310 struct ptlrpc_request **req)
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,
322 struct ll_sb_info *sbi = ll_i2sbi(inode);
323 struct obd_export *exp = sbi->ll_md_exp;
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,
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;
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));
351 op_data->op_valid = OBD_MD_FLXATTR | OBD_MD_FLXATTRLS;
353 rc = md_enqueue(exp, &einfo, NULL, oit, op_data, &lockh, 0);
354 ll_finish_md_op_data(op_data);
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);
363 *req = (struct ptlrpc_request *)oit->d.lustre.it_data;
365 down_write(&lli->lli_xattrs_list_rwsem);
366 mutex_unlock(&lli->lli_xattrs_enq_lock);
372 * Refill the xattr cache.
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.
378 * \retval 0 no error occured
379 * \retval -EPROTO network protocol error
380 * \retval -ENOMEM not enough memory for the cache
382 static int ll_xattr_cache_refill(struct inode *inode, struct lookup_intent *oit)
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;
394 rc = ll_xattr_find_get_lock(inode, oit, &req);
396 GOTO(out_no_unlock, rc);
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);
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);
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 */
417 GOTO(out_destroy, rc);
420 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
422 CERROR("no MDT BODY in the refill xattr reply\n");
423 GOTO(out_destroy, rc = -EPROTO);
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,
430 xsizes = req_capsule_server_sized_get(&req->rq_pill, &RMF_EAVALS_LENS,
431 body->mbo_max_mdsize *
433 if (xdata == NULL || xval == NULL || xsizes == NULL) {
434 CERROR("wrong setxattr reply\n");
435 GOTO(out_destroy, rc = -EPROTO);
438 xtail = xdata + body->mbo_eadatasize;
439 xvtail = xval + body->mbo_aclsize;
441 CDEBUG(D_CACHE, "caching: xdata=%p xtail=%p\n", xdata, xtail);
443 ll_xattr_cache_init(lli);
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");
451 } else if (xval + *xsizes > xvtail) {
452 CERROR("xattr protocol violation (vals are broken)\n");
454 } else if (OBD_FAIL_CHECK(OBD_FAIL_LLITE_XATTR_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);
462 rc = ll_xattr_cache_add(&lli->lli_xattrs, xdata, xval,
466 ll_xattr_cache_destroy_locked(lli);
467 GOTO(out_destroy, rc);
469 xdata += strlen(xdata) + 1;
474 if (xdata != xtail || xval != xvtail)
475 CERROR("a hole in xattr data\n");
477 ll_set_lock_data(sbi->ll_md_exp, inode, oit, NULL);
479 GOTO(out_maybe_drop, rc);
482 ll_intent_drop_lock(oit);
485 up_write(&lli->lli_xattrs_list_rwsem);
487 ptlrpc_req_finished(req);
492 up_write(&lli->lli_xattrs_list_rwsem);
494 ldlm_lock_decref_and_cancel((struct lustre_handle *)
495 &oit->d.lustre.it_lock_handle,
496 oit->d.lustre.it_lock_mode);
502 * Get an xattr value or list xattrs using the write-through cache.
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.
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
515 int ll_xattr_cache_get(struct inode *inode,
521 struct lookup_intent oit = { .it_op = IT_GETXATTR };
522 struct ll_inode_info *lli = ll_i2info(inode);
527 LASSERT(!!(valid & OBD_MD_FLXATTR) ^ !!(valid & OBD_MD_FLXATTRLS));
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);
535 downgrade_write(&lli->lli_xattrs_list_rwsem);
537 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR_HITS, 1);
540 if (valid & OBD_MD_FLXATTR) {
541 struct ll_xattr_entry *xattr;
543 rc = ll_xattr_cache_find(&lli->lli_xattrs, name, &xattr);
545 rc = xattr->xe_vallen;
546 /* zero size means we are only requested size in rc */
548 if (size >= xattr->xe_vallen)
549 memcpy(buffer, xattr->xe_value,
555 } else if (valid & OBD_MD_FLXATTRLS) {
556 rc = ll_xattr_cache_list(&lli->lli_xattrs,
557 size ? buffer : NULL, size);
562 up_read(&lli->lli_xattrs_list_rwsem);