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 or updates an xattr.
126 * Add @xattr_name attr with @xattr_val value and @xattr_val_len length,
127 * if the attribute already exists, then update its value.
130 * \retval -ENOMEM if no memory could be allocated for the cached attr
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 /* Found a cached EA, update it */
144 if (xattr_val_len != xattr->xe_vallen) {
146 OBD_ALLOC(val, xattr_val_len);
148 CDEBUG(D_CACHE, "failed to allocate %u bytes "
149 "for xattr %s update\n",
154 OBD_FREE(xattr->xe_value, xattr->xe_vallen);
155 xattr->xe_value = val;
156 xattr->xe_vallen = xattr_val_len;
158 memcpy(xattr->xe_value, xattr_val, xattr_val_len);
160 CDEBUG(D_CACHE, "update: [%s]=%.*s\n", xattr_name,
161 xattr_val_len, xattr_val);
166 OBD_SLAB_ALLOC_PTR_GFP(xattr, xattr_kmem, __GFP_IO);
168 CDEBUG(D_CACHE, "failed to allocate xattr\n");
172 xattr->xe_namelen = strlen(xattr_name) + 1;
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",
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",
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);
192 CDEBUG(D_CACHE, "set: [%s]=%.*s\n", xattr_name,
193 xattr_val_len, xattr_val);
197 OBD_FREE(xattr->xe_name, xattr->xe_namelen);
199 OBD_SLAB_FREE_PTR(xattr, xattr_kmem);
205 * This removes an extended attribute from cache.
207 * Remove @xattr_name attribute from @cache.
210 * \retval -ENODATA if @xattr_name is not cached
212 static int ll_xattr_cache_del(struct list_head *cache,
213 const char *xattr_name)
215 struct ll_xattr_entry *xattr;
219 CDEBUG(D_CACHE, "del xattr: %s\n", xattr_name);
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);
234 * This iterates cached extended attributes.
236 * Walk over cached attributes in @cache and
237 * fill in @xld_buffer or only calculate buffer
238 * size if @xld_buffer is NULL.
240 * \retval >= 0 buffer list size
241 * \retval -ENODATA if the list cannot fit @xld_size buffer
243 static int ll_xattr_cache_list(struct list_head *cache,
247 struct ll_xattr_entry *xattr, *tmp;
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);
257 xld_size -= xattr->xe_namelen;
260 memcpy(&xld_buffer[xld_tail],
261 xattr->xe_name, xattr->xe_namelen);
263 xld_tail += xattr->xe_namelen;
273 * Check if the xattr cache is initialized (filled).
275 * \retval 0 @cache is not initialized
276 * \retval 1 @cache is initialized
278 int ll_xattr_cache_valid(struct ll_inode_info *lli)
280 return !!(lli->lli_flags & LLIF_XATTR_CACHE);
284 * This finalizes the xattr cache.
286 * Free all xattr memory. @lli is the inode info pointer.
288 * \retval 0 no error occured
290 static int ll_xattr_cache_destroy_locked(struct ll_inode_info *lli)
294 if (!ll_xattr_cache_valid(lli))
297 while (ll_xattr_cache_del(&lli->lli_xattrs, NULL) == 0)
299 lli->lli_flags &= ~LLIF_XATTR_CACHE;
304 int ll_xattr_cache_destroy(struct inode *inode)
306 struct ll_inode_info *lli = ll_i2info(inode);
311 down_write(&lli->lli_xattrs_list_rwsem);
312 rc = ll_xattr_cache_destroy_locked(lli);
313 up_write(&lli->lli_xattrs_list_rwsem);
319 * Match or enqueue a PR or PW LDLM lock.
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.
326 * \retval 0 no error occured
327 * \retval -ENOMEM not enough memory
329 static int ll_xattr_find_get_lock(struct inode *inode,
330 struct lookup_intent *oit,
331 struct ptlrpc_request **req)
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;
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 :
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;
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));
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;
375 rc = md_enqueue(exp, &einfo, oit, op_data, &lockh, NULL, 0, NULL, 0);
376 ll_finish_md_op_data(op_data);
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);
385 *req = (struct ptlrpc_request *)oit->d.lustre.it_data;
387 down_write(&lli->lli_xattrs_list_rwsem);
388 mutex_unlock(&lli->lli_xattrs_enq_lock);
394 * Refill the xattr cache.
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.
400 * \retval 0 no error occured
401 * \retval -EPROTO network protocol error
402 * \retval -ENOMEM not enough memory for the cache
404 static int ll_xattr_cache_refill(struct inode *inode, struct lookup_intent *oit)
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;
416 rc = ll_xattr_find_get_lock(inode, oit, &req);
418 GOTO(out_no_unlock, rc);
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);
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);
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);
438 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
440 CERROR("no MDT BODY in the refill xattr reply\n");
441 GOTO(out_destroy, rc = -EPROTO);
443 /* do not need swab xattr data */
444 xdata = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA,
446 xval = req_capsule_server_sized_get(&req->rq_pill, &RMF_EAVALS,
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);
455 xtail = xdata + body->eadatasize;
456 xvtail = xval + body->aclsize;
458 CDEBUG(D_CACHE, "caching: xdata=%p xtail=%p\n", xdata, xtail);
460 ll_xattr_cache_init(lli);
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");
468 } else if (xval + *xsizes > xvtail) {
469 CERROR("xattr protocol violation (vals are broken)\n");
471 } else if (OBD_FAIL_CHECK(OBD_FAIL_LLITE_XATTR_ENOMEM)) {
474 rc = ll_xattr_cache_add(&lli->lli_xattrs, xdata, xval,
478 ll_xattr_cache_destroy_locked(lli);
479 GOTO(out_destroy, rc);
481 xdata += strlen(xdata) + 1;
486 if (xdata != xtail || xval != xvtail)
487 CERROR("a hole in xattr data\n");
489 ll_set_lock_data(sbi->ll_md_exp, inode, oit, NULL);
491 GOTO(out_maybe_drop, rc);
493 /* drop lock on error or getxattr */
494 if (rc != 0 || oit->it_op != IT_SETXATTR)
495 ll_intent_drop_lock(oit);
498 up_write(&lli->lli_xattrs_list_rwsem);
500 ptlrpc_req_finished(req);
505 up_write(&lli->lli_xattrs_list_rwsem);
507 ldlm_lock_decref_and_cancel((struct lustre_handle *)
508 &oit->d.lustre.it_lock_handle,
509 oit->d.lustre.it_lock_mode);
515 * Get an xattr value or list xattrs using the write-through cache.
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.
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
528 int ll_xattr_cache_get(struct inode *inode,
534 struct lookup_intent oit = { .it_op = IT_GETXATTR };
535 struct ll_inode_info *lli = ll_i2info(inode);
540 LASSERT(!!(valid & OBD_MD_FLXATTR) ^ !!(valid & OBD_MD_FLXATTRLS));
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);
548 downgrade_write(&lli->lli_xattrs_list_rwsem);
550 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR_HITS, 1);
553 if (valid & OBD_MD_FLXATTR) {
554 struct ll_xattr_entry *xattr;
556 rc = ll_xattr_cache_find(&lli->lli_xattrs, name, &xattr);
558 rc = xattr->xe_vallen;
559 /* zero size means we are only requested size in rc */
561 if (size >= xattr->xe_vallen)
562 memcpy(buffer, xattr->xe_value,
568 } else if (valid & OBD_MD_FLXATTRLS) {
569 rc = ll_xattr_cache_list(&lli->lli_xattrs,
570 size ? buffer : NULL, size);
575 up_read(&lli->lli_xattrs_list_rwsem);
582 * Set/update an xattr value or remove xattr using the write-through cache.
584 * Set/update the xattr value (if @valid has OBD_MD_FLXATTR) of @name to @newval
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)
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)
595 int ll_xattr_cache_update(struct inode *inode,
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);
611 LASSERT(!!(valid & OBD_MD_FLXATTR) ^ !!(valid & OBD_MD_FLXATTRRM));
613 rc = ll_xattr_cache_refill(inode, &oit);
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);
624 ll_intent_drop_lock(&oit);
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);
633 ll_intent_drop_lock(&oit);
636 up_write(&lli->lli_xattrs_list_rwsem);
637 ptlrpc_req_finished(req);