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 * Copyright (c) 2013, 2015, Intel Corporation.
29 * Author: Andrew Perepechko <Andrew_Perepechko@xyratex.com>
33 #define DEBUG_SUBSYSTEM S_LLITE
36 #include <linux/sched.h>
38 #include <obd_support.h>
39 #include <lustre_dlm.h>
40 #include <lustre_ver.h>
41 #include "llite_internal.h"
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.
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 */
55 static struct kmem_cache *xattr_kmem;
56 static struct lu_kmem_descr xattr_caches[] = {
58 .ckd_cache = &xattr_kmem,
59 .ckd_name = "xattr_kmem",
60 .ckd_size = sizeof(struct ll_xattr_entry)
67 int ll_xattr_init(void)
69 return lu_kmem_init(xattr_caches);
72 void ll_xattr_fini(void)
74 lu_kmem_fini(xattr_caches);
78 * Initializes xattr cache for an inode.
80 * This initializes the xattr list and marks cache presence.
82 static void ll_xattr_cache_init(struct ll_inode_info *lli)
88 INIT_LIST_HEAD(&lli->lli_xattrs);
89 ll_file_set_flag(lli, LLIF_XATTR_CACHE);
93 * This looks for a specific extended attribute.
95 * Find in @cache and return @xattr_name attribute in @xattr,
96 * for the NULL @xattr_name return the first cached @xattr.
99 * \retval -ENODATA if not found
101 static int ll_xattr_cache_find(struct list_head *cache,
102 const char *xattr_name,
103 struct ll_xattr_entry **xattr)
105 struct ll_xattr_entry *entry;
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) {
114 CDEBUG(D_CACHE, "find: [%s]=%.*s\n",
115 entry->xe_name, entry->xe_vallen,
125 * This adds an xattr.
127 * Add @xattr_name attr with @xattr_val value and @xattr_val_len length,
130 * \retval -ENOMEM if no memory could be allocated for the cached attr
131 * \retval -EPROTO if duplicate xattr is being added
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)
138 struct ll_xattr_entry *xattr;
142 if (ll_xattr_cache_find(cache, xattr_name, &xattr) == 0) {
143 CDEBUG(D_CACHE, "duplicate xattr: [%s]\n", xattr_name);
147 OBD_SLAB_ALLOC_PTR_GFP(xattr, xattr_kmem, GFP_NOFS);
149 CDEBUG(D_CACHE, "failed to allocate xattr\n");
153 xattr->xe_namelen = strlen(xattr_name) + 1;
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",
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",
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);
173 CDEBUG(D_CACHE, "set: [%s]=%.*s\n", xattr_name,
174 xattr_val_len, xattr_val);
178 OBD_FREE(xattr->xe_name, xattr->xe_namelen);
180 OBD_SLAB_FREE_PTR(xattr, xattr_kmem);
186 * This removes an extended attribute from cache.
188 * Remove @xattr_name attribute from @cache.
191 * \retval -ENODATA if @xattr_name is not cached
193 static int ll_xattr_cache_del(struct list_head *cache,
194 const char *xattr_name)
196 struct ll_xattr_entry *xattr;
200 CDEBUG(D_CACHE, "del xattr: %s\n", xattr_name);
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);
215 * This iterates cached extended attributes.
217 * Walk over cached attributes in @cache and
218 * fill in @xld_buffer or only calculate buffer
219 * size if @xld_buffer is NULL.
221 * \retval >= 0 buffer list size
222 * \retval -ENODATA if the list cannot fit @xld_size buffer
224 static int ll_xattr_cache_list(struct list_head *cache,
228 struct ll_xattr_entry *xattr, *tmp;
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);
238 xld_size -= xattr->xe_namelen;
241 memcpy(&xld_buffer[xld_tail],
242 xattr->xe_name, xattr->xe_namelen);
244 xld_tail += xattr->xe_namelen;
254 * Check if the xattr cache is initialized (filled).
256 * \retval 0 @cache is not initialized
257 * \retval 1 @cache is initialized
259 static int ll_xattr_cache_valid(struct ll_inode_info *lli)
261 return ll_file_test_flag(lli, LLIF_XATTR_CACHE);
265 * This finalizes the xattr cache.
267 * Free all xattr memory. @lli is the inode info pointer.
269 * \retval 0 no error occured
271 static int ll_xattr_cache_destroy_locked(struct ll_inode_info *lli)
275 if (!ll_xattr_cache_valid(lli))
278 while (ll_xattr_cache_del(&lli->lli_xattrs, NULL) == 0)
281 ll_file_clear_flag(lli, LLIF_XATTR_CACHE);
286 int ll_xattr_cache_destroy(struct inode *inode)
288 struct ll_inode_info *lli = ll_i2info(inode);
293 down_write(&lli->lli_xattrs_list_rwsem);
294 rc = ll_xattr_cache_destroy_locked(lli);
295 up_write(&lli->lli_xattrs_list_rwsem);
301 * Match or enqueue a PR lock.
303 * Find or request an LDLM lock with xattr data.
304 * Since LDLM does not provide API for atomic match_or_enqueue,
305 * the function handles it with a separate enq lock.
306 * If successful, the function exits with the list lock held.
308 * \retval 0 no error occured
309 * \retval -ENOMEM not enough memory
311 static int ll_xattr_find_get_lock(struct inode *inode,
312 struct lookup_intent *oit,
313 struct ptlrpc_request **req)
316 struct lustre_handle lockh = { 0 };
317 struct md_op_data *op_data;
318 struct ll_inode_info *lli = ll_i2info(inode);
319 struct ldlm_enqueue_info einfo = {
320 .ei_type = LDLM_IBITS,
321 .ei_mode = it_to_lock_mode(oit),
322 .ei_cb_bl = &ll_md_blocking_ast,
323 .ei_cb_cp = &ldlm_completion_ast,
325 struct ll_sb_info *sbi = ll_i2sbi(inode);
326 struct obd_export *exp = sbi->ll_md_exp;
331 mutex_lock(&lli->lli_xattrs_enq_lock);
332 /* inode may have been shrunk and recreated, so data is gone, match lock
333 * only when data exists. */
334 if (ll_xattr_cache_valid(lli)) {
335 /* Try matching first. */
336 mode = ll_take_md_lock(inode, MDS_INODELOCK_XATTR, &lockh, 0,
339 /* fake oit in mdc_revalidate_lock() manner */
340 oit->it_lock_handle = lockh.cookie;
341 oit->it_lock_mode = mode;
346 /* Enqueue if the lock isn't cached locally. */
347 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
348 LUSTRE_OPC_ANY, NULL);
349 if (IS_ERR(op_data)) {
350 mutex_unlock(&lli->lli_xattrs_enq_lock);
351 RETURN(PTR_ERR(op_data));
354 op_data->op_valid = OBD_MD_FLXATTR | OBD_MD_FLXATTRLS;
356 rc = md_enqueue(exp, &einfo, NULL, oit, op_data, &lockh, 0);
357 ll_finish_md_op_data(op_data);
360 CDEBUG(D_CACHE, "md_intent_lock failed with %d for fid "DFID"\n",
361 rc, PFID(ll_inode2fid(inode)));
362 mutex_unlock(&lli->lli_xattrs_enq_lock);
366 *req = oit->it_request;
368 down_write(&lli->lli_xattrs_list_rwsem);
369 mutex_unlock(&lli->lli_xattrs_enq_lock);
375 * Refill the xattr cache.
377 * Fetch and cache the whole of xattrs for @inode, acquiring
378 * a read or a write xattr lock depending on operation in @oit.
379 * Intent is dropped on exit unless the operation is setxattr.
381 * \retval 0 no error occured
382 * \retval -EPROTO network protocol error
383 * \retval -ENOMEM not enough memory for the cache
385 static int ll_xattr_cache_refill(struct inode *inode, struct lookup_intent *oit)
387 struct ll_sb_info *sbi = ll_i2sbi(inode);
388 struct ptlrpc_request *req = NULL;
389 const char *xdata, *xval, *xtail, *xvtail;
390 struct ll_inode_info *lli = ll_i2info(inode);
391 struct mdt_body *body;
397 rc = ll_xattr_find_get_lock(inode, oit, &req);
399 GOTO(out_no_unlock, rc);
401 /* Do we have the data at this point? */
402 if (ll_xattr_cache_valid(lli)) {
403 ll_stats_ops_tally(sbi, LPROC_LL_GETXATTR_HITS, 1);
404 GOTO(out_maybe_drop, rc = 0);
407 /* Matched but no cache? Cancelled on error by a parallel refill. */
408 if (unlikely(req == NULL)) {
409 CDEBUG(D_CACHE, "cancelled by a parallel getxattr\n");
410 GOTO(out_maybe_drop, rc = -EIO);
413 if (oit->it_status < 0) {
414 CDEBUG(D_CACHE, "getxattr intent returned %d for fid "DFID"\n",
415 oit->it_status, PFID(ll_inode2fid(inode)));
417 /* xattr data is so large that we don't want to cache it */
420 GOTO(out_destroy, rc);
423 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
425 CERROR("no MDT BODY in the refill xattr reply\n");
426 GOTO(out_destroy, rc = -EPROTO);
428 /* do not need swab xattr data */
429 xdata = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA,
430 body->mbo_eadatasize);
431 xval = req_capsule_server_sized_get(&req->rq_pill, &RMF_EAVALS,
433 xsizes = req_capsule_server_sized_get(&req->rq_pill, &RMF_EAVALS_LENS,
434 body->mbo_max_mdsize *
436 if (xdata == NULL || xval == NULL || xsizes == NULL) {
437 CERROR("wrong setxattr reply\n");
438 GOTO(out_destroy, rc = -EPROTO);
441 xtail = xdata + body->mbo_eadatasize;
442 xvtail = xval + body->mbo_aclsize;
444 CDEBUG(D_CACHE, "caching: xdata=%p xtail=%p\n", xdata, xtail);
446 ll_xattr_cache_init(lli);
448 for (i = 0; i < body->mbo_max_mdsize; i++) {
449 CDEBUG(D_CACHE, "caching [%s]=%.*s\n", xdata, *xsizes, xval);
450 /* Perform consistency checks: attr names and vals in pill */
451 if (memchr(xdata, 0, xtail - xdata) == NULL) {
452 CERROR("xattr protocol violation (names are broken)\n");
454 } else if (xval + *xsizes > xvtail) {
455 CERROR("xattr protocol violation (vals are broken)\n");
457 } else if (OBD_FAIL_CHECK(OBD_FAIL_LLITE_XATTR_ENOMEM)) {
459 } else if (!strcmp(xdata, XATTR_NAME_ACL_ACCESS)) {
460 /* Filter out ACL ACCESS since it's cached separately */
461 CDEBUG(D_CACHE, "not caching %s\n",
462 XATTR_NAME_ACL_ACCESS);
464 } else if (!strcmp(xdata, "security.selinux")) {
465 /* Filter out security.selinux, it is cached in slab */
466 CDEBUG(D_CACHE, "not caching security.selinux\n");
469 rc = ll_xattr_cache_add(&lli->lli_xattrs, xdata, xval,
473 ll_xattr_cache_destroy_locked(lli);
474 GOTO(out_destroy, rc);
476 xdata += strlen(xdata) + 1;
481 if (xdata != xtail || xval != xvtail)
482 CERROR("a hole in xattr data\n");
484 ll_set_lock_data(sbi->ll_md_exp, inode, oit, NULL);
486 GOTO(out_maybe_drop, rc);
489 ll_intent_drop_lock(oit);
492 up_write(&lli->lli_xattrs_list_rwsem);
494 ptlrpc_req_finished(req);
499 up_write(&lli->lli_xattrs_list_rwsem);
501 ldlm_lock_decref_and_cancel((struct lustre_handle *)
502 &oit->it_lock_handle,
509 * Get an xattr value or list xattrs using the write-through cache.
511 * Get the xattr value (@valid has OBD_MD_FLXATTR set) of @name or
512 * list xattr names (@valid has OBD_MD_FLXATTRLS set) for @inode.
513 * The resulting value/list is stored in @buffer if the former
514 * is not larger than @size.
516 * \retval 0 no error occured
517 * \retval -EPROTO network protocol error
518 * \retval -ENOMEM not enough memory for the cache
519 * \retval -ERANGE the buffer is not large enough
520 * \retval -ENODATA no such attr or the list is empty
522 int ll_xattr_cache_get(struct inode *inode,
528 struct lookup_intent oit = { .it_op = IT_GETXATTR };
529 struct ll_inode_info *lli = ll_i2info(inode);
534 LASSERT(!!(valid & OBD_MD_FLXATTR) ^ !!(valid & OBD_MD_FLXATTRLS));
536 down_read(&lli->lli_xattrs_list_rwsem);
537 if (!ll_xattr_cache_valid(lli)) {
538 up_read(&lli->lli_xattrs_list_rwsem);
539 rc = ll_xattr_cache_refill(inode, &oit);
542 downgrade_write(&lli->lli_xattrs_list_rwsem);
544 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR_HITS, 1);
547 if (valid & OBD_MD_FLXATTR) {
548 struct ll_xattr_entry *xattr;
550 rc = ll_xattr_cache_find(&lli->lli_xattrs, name, &xattr);
552 rc = xattr->xe_vallen;
553 /* zero size means we are only requested size in rc */
555 if (size >= xattr->xe_vallen)
556 memcpy(buffer, xattr->xe_value,
562 } else if (valid & OBD_MD_FLXATTRLS) {
563 rc = ll_xattr_cache_list(&lli->lli_xattrs,
564 size ? buffer : NULL, size);
569 up_read(&lli->lli_xattrs_list_rwsem);