1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright 2019, 2020 Amazon.com, Inc. or its affiliates. All rights reserved.
6 * User extended attribute client side cache functions.
8 * Author: Frank van der Linden <fllinden@amazon.com>
10 #include <linux/errno.h>
11 #include <linux/nfs_fs.h>
12 #include <linux/hashtable.h>
13 #include <linux/refcount.h>
14 #include <uapi/linux/xattr.h>
20 * User extended attributes client side caching is implemented by having
21 * a cache structure attached to NFS inodes. This structure is allocated
22 * when needed, and freed when the cache is zapped.
24 * The cache structure contains as hash table of entries, and a pointer
25 * to a special-cased entry for the listxattr cache.
27 * Accessing and allocating / freeing the caches is done via reference
28 * counting. The cache entries use a similar refcounting scheme.
30 * This makes freeing a cache, both from the shrinker and from the
31 * zap cache path, easy. It also means that, in current use cases,
32 * the large majority of inodes will not waste any memory, as they
33 * will never have any user extended attributes assigned to them.
35 * Attribute entries are hashed in to a simple hash table. They are
36 * also part of an LRU.
38 * There are three shrinkers.
40 * Two shrinkers deal with the cache entries themselves: one for
41 * large entries (> PAGE_SIZE), and one for smaller entries. The
42 * shrinker for the larger entries works more aggressively than
43 * those for the smaller entries.
45 * The other shrinker frees the cache structures themselves.
49 * 64 buckets is a good default. There is likely no reasonable
50 * workload that uses more than even 64 user extended attributes.
51 * You can certainly add a lot more - but you get what you ask for
52 * in those circumstances.
54 #define NFS4_XATTR_HASH_SIZE 64
56 #define NFSDBG_FACILITY NFSDBG_XATTRCACHE
58 struct nfs4_xattr_cache;
59 struct nfs4_xattr_entry;
61 struct nfs4_xattr_bucket {
63 struct hlist_head hlist;
64 struct nfs4_xattr_cache *cache;
68 struct nfs4_xattr_cache {
70 struct nfs4_xattr_bucket buckets[NFS4_XATTR_HASH_SIZE];
72 struct list_head dispose;
74 spinlock_t listxattr_lock;
76 struct nfs4_xattr_entry *listxattr;
79 struct nfs4_xattr_entry {
81 struct hlist_node hnode;
83 struct list_head dispose;
87 struct nfs4_xattr_bucket *bucket;
91 #define NFS4_XATTR_ENTRY_EXTVAL 0x0001
94 * LRU list of NFS inodes that have xattr caches.
96 static struct list_lru nfs4_xattr_cache_lru;
97 static struct list_lru nfs4_xattr_entry_lru;
98 static struct list_lru nfs4_xattr_large_entry_lru;
100 static struct kmem_cache *nfs4_xattr_cache_cachep;
103 * Hashing helper functions.
106 nfs4_xattr_hash_init(struct nfs4_xattr_cache *cache)
110 for (i = 0; i < NFS4_XATTR_HASH_SIZE; i++) {
111 INIT_HLIST_HEAD(&cache->buckets[i].hlist);
112 spin_lock_init(&cache->buckets[i].lock);
113 cache->buckets[i].cache = cache;
114 cache->buckets[i].draining = false;
120 * 1. inode i_lock or bucket lock
121 * 2. list_lru lock (taken by list_lru_* functions)
125 * Wrapper functions to add a cache entry to the right LRU.
128 nfs4_xattr_entry_lru_add(struct nfs4_xattr_entry *entry)
130 struct list_lru *lru;
132 lru = (entry->flags & NFS4_XATTR_ENTRY_EXTVAL) ?
133 &nfs4_xattr_large_entry_lru : &nfs4_xattr_entry_lru;
135 return list_lru_add(lru, &entry->lru);
139 nfs4_xattr_entry_lru_del(struct nfs4_xattr_entry *entry)
141 struct list_lru *lru;
143 lru = (entry->flags & NFS4_XATTR_ENTRY_EXTVAL) ?
144 &nfs4_xattr_large_entry_lru : &nfs4_xattr_entry_lru;
146 return list_lru_del(lru, &entry->lru);
150 * This function allocates cache entries. They are the normal
151 * extended attribute name/value pairs, but may also be a listxattr
152 * cache. Those allocations use the same entry so that they can be
153 * treated as one by the memory shrinker.
155 * xattr cache entries are allocated together with names. If the
156 * value fits in to one page with the entry structure and the name,
157 * it will also be part of the same allocation (kmalloc). This is
158 * expected to be the vast majority of cases. Larger allocations
159 * have a value pointer that is allocated separately by kvmalloc.
163 * @name: Name of the extended attribute. NULL for listxattr cache
165 * @value: Value of attribute, or listxattr cache. NULL if the
166 * value is to be copied from pages instead.
167 * @pages: Pages to copy the value from, if not NULL. Passed in to
168 * make it easier to copy the value after an RPC, even if
169 * the value will not be passed up to application (e.g.
170 * for a 'query' getxattr with NULL buffer).
171 * @len: Length of the value. Can be 0 for zero-length attributes.
172 * @value and @pages will be NULL if @len is 0.
174 static struct nfs4_xattr_entry *
175 nfs4_xattr_alloc_entry(const char *name, const void *value,
176 struct page **pages, size_t len)
178 struct nfs4_xattr_entry *entry;
181 size_t alloclen, slen;
185 BUILD_BUG_ON(sizeof(struct nfs4_xattr_entry) +
186 XATTR_NAME_MAX + 1 > PAGE_SIZE);
188 alloclen = sizeof(struct nfs4_xattr_entry);
190 slen = strlen(name) + 1;
195 if (alloclen + len <= PAGE_SIZE) {
199 flags = NFS4_XATTR_ENTRY_EXTVAL;
202 buf = kmalloc(alloclen, GFP_KERNEL);
205 entry = (struct nfs4_xattr_entry *)buf;
208 namep = buf + sizeof(struct nfs4_xattr_entry);
209 memcpy(namep, name, slen);
215 if (flags & NFS4_XATTR_ENTRY_EXTVAL) {
216 valp = kvmalloc(len, GFP_KERNEL);
221 } else if (len != 0) {
222 valp = buf + sizeof(struct nfs4_xattr_entry) + slen;
228 memcpy(valp, value, len);
230 _copy_from_pages(valp, pages, 0, len);
233 entry->flags = flags;
234 entry->xattr_value = valp;
235 kref_init(&entry->ref);
236 entry->xattr_name = namep;
237 entry->xattr_size = len;
238 entry->bucket = NULL;
239 INIT_LIST_HEAD(&entry->lru);
240 INIT_LIST_HEAD(&entry->dispose);
241 INIT_HLIST_NODE(&entry->hnode);
247 nfs4_xattr_free_entry(struct nfs4_xattr_entry *entry)
249 if (entry->flags & NFS4_XATTR_ENTRY_EXTVAL)
250 kvfree(entry->xattr_value);
255 nfs4_xattr_free_entry_cb(struct kref *kref)
257 struct nfs4_xattr_entry *entry;
259 entry = container_of(kref, struct nfs4_xattr_entry, ref);
261 if (WARN_ON(!list_empty(&entry->lru)))
264 nfs4_xattr_free_entry(entry);
268 nfs4_xattr_free_cache_cb(struct kref *kref)
270 struct nfs4_xattr_cache *cache;
273 cache = container_of(kref, struct nfs4_xattr_cache, ref);
275 for (i = 0; i < NFS4_XATTR_HASH_SIZE; i++) {
276 if (WARN_ON(!hlist_empty(&cache->buckets[i].hlist)))
278 cache->buckets[i].draining = false;
281 cache->listxattr = NULL;
283 kmem_cache_free(nfs4_xattr_cache_cachep, cache);
287 static struct nfs4_xattr_cache *
288 nfs4_xattr_alloc_cache(void)
290 struct nfs4_xattr_cache *cache;
292 cache = kmem_cache_alloc(nfs4_xattr_cache_cachep, GFP_KERNEL);
296 kref_init(&cache->ref);
297 atomic_long_set(&cache->nent, 0);
303 * Set the listxattr cache, which is a special-cased cache entry.
304 * The special value ERR_PTR(-ESTALE) is used to indicate that
305 * the cache is being drained - this prevents a new listxattr
306 * cache from being added to what is now a stale cache.
309 nfs4_xattr_set_listcache(struct nfs4_xattr_cache *cache,
310 struct nfs4_xattr_entry *new)
312 struct nfs4_xattr_entry *old;
315 spin_lock(&cache->listxattr_lock);
317 old = cache->listxattr;
319 if (old == ERR_PTR(-ESTALE)) {
324 cache->listxattr = new;
325 if (new != NULL && new != ERR_PTR(-ESTALE))
326 nfs4_xattr_entry_lru_add(new);
329 nfs4_xattr_entry_lru_del(old);
330 kref_put(&old->ref, nfs4_xattr_free_entry_cb);
333 spin_unlock(&cache->listxattr_lock);
339 * Unlink a cache from its parent inode, clearing out an invalid
340 * cache. Must be called with i_lock held.
342 static struct nfs4_xattr_cache *
343 nfs4_xattr_cache_unlink(struct inode *inode)
345 struct nfs_inode *nfsi;
346 struct nfs4_xattr_cache *oldcache;
350 oldcache = nfsi->xattr_cache;
351 if (oldcache != NULL) {
352 list_lru_del(&nfs4_xattr_cache_lru, &oldcache->lru);
353 oldcache->inode = NULL;
355 nfsi->xattr_cache = NULL;
356 nfsi->cache_validity &= ~NFS_INO_INVALID_XATTR;
363 * Discard a cache. Called by get_cache() if there was an old,
364 * invalid cache. Can also be called from a shrinker callback.
366 * The cache is dead, it has already been unlinked from its inode,
367 * and no longer appears on the cache LRU list.
369 * Mark all buckets as draining, so that no new entries are added. This
370 * could still happen in the unlikely, but possible case that another
371 * thread had grabbed a reference before it was unlinked from the inode,
372 * and is still holding it for an add operation.
374 * Remove all entries from the LRU lists, so that there is no longer
375 * any way to 'find' this cache. Then, remove the entries from the hash
378 * At that point, the cache will remain empty and can be freed when the final
379 * reference drops, which is very likely the kref_put at the end of
380 * this function, or the one called immediately afterwards in the
384 nfs4_xattr_discard_cache(struct nfs4_xattr_cache *cache)
387 struct nfs4_xattr_entry *entry;
388 struct nfs4_xattr_bucket *bucket;
389 struct hlist_node *n;
391 nfs4_xattr_set_listcache(cache, ERR_PTR(-ESTALE));
393 for (i = 0; i < NFS4_XATTR_HASH_SIZE; i++) {
394 bucket = &cache->buckets[i];
396 spin_lock(&bucket->lock);
397 bucket->draining = true;
398 hlist_for_each_entry_safe(entry, n, &bucket->hlist, hnode) {
399 nfs4_xattr_entry_lru_del(entry);
400 hlist_del_init(&entry->hnode);
401 kref_put(&entry->ref, nfs4_xattr_free_entry_cb);
403 spin_unlock(&bucket->lock);
406 atomic_long_set(&cache->nent, 0);
408 kref_put(&cache->ref, nfs4_xattr_free_cache_cb);
412 * Get a referenced copy of the cache structure. Avoid doing allocs
413 * while holding i_lock. Which means that we do some optimistic allocation,
414 * and might have to free the result in rare cases.
416 * This function only checks the NFS_INO_INVALID_XATTR cache validity bit
417 * and acts accordingly, replacing the cache when needed. For the read case
418 * (!add), this means that the caller must make sure that the cache
419 * is valid before caling this function. getxattr and listxattr call
420 * revalidate_inode to do this. The attribute cache timeout (for the
421 * non-delegated case) is expected to be dealt with in the revalidate
425 static struct nfs4_xattr_cache *
426 nfs4_xattr_get_cache(struct inode *inode, int add)
428 struct nfs_inode *nfsi;
429 struct nfs4_xattr_cache *cache, *oldcache, *newcache;
433 cache = oldcache = NULL;
435 spin_lock(&inode->i_lock);
437 if (nfsi->cache_validity & NFS_INO_INVALID_XATTR)
438 oldcache = nfs4_xattr_cache_unlink(inode);
440 cache = nfsi->xattr_cache;
443 kref_get(&cache->ref);
445 spin_unlock(&inode->i_lock);
447 if (add && cache == NULL) {
450 cache = nfs4_xattr_alloc_cache();
454 spin_lock(&inode->i_lock);
455 if (nfsi->cache_validity & NFS_INO_INVALID_XATTR) {
457 * The cache was invalidated again. Give up,
458 * since what we want to enter is now likely
461 spin_unlock(&inode->i_lock);
462 kref_put(&cache->ref, nfs4_xattr_free_cache_cb);
468 * Check if someone beat us to it.
470 if (nfsi->xattr_cache != NULL) {
471 newcache = nfsi->xattr_cache;
472 kref_get(&newcache->ref);
474 kref_get(&cache->ref);
475 nfsi->xattr_cache = cache;
476 cache->inode = inode;
477 list_lru_add(&nfs4_xattr_cache_lru, &cache->lru);
480 spin_unlock(&inode->i_lock);
483 * If there was a race, throw away the cache we just
484 * allocated, and use the new one allocated by someone
487 if (newcache != NULL) {
488 kref_put(&cache->ref, nfs4_xattr_free_cache_cb);
495 * Discard the now orphaned old cache.
497 if (oldcache != NULL)
498 nfs4_xattr_discard_cache(oldcache);
503 static inline struct nfs4_xattr_bucket *
504 nfs4_xattr_hash_bucket(struct nfs4_xattr_cache *cache, const char *name)
506 return &cache->buckets[jhash(name, strlen(name), 0) &
507 (ARRAY_SIZE(cache->buckets) - 1)];
510 static struct nfs4_xattr_entry *
511 nfs4_xattr_get_entry(struct nfs4_xattr_bucket *bucket, const char *name)
513 struct nfs4_xattr_entry *entry;
517 hlist_for_each_entry(entry, &bucket->hlist, hnode) {
518 if (!strcmp(entry->xattr_name, name))
526 nfs4_xattr_hash_add(struct nfs4_xattr_cache *cache,
527 struct nfs4_xattr_entry *entry)
529 struct nfs4_xattr_bucket *bucket;
530 struct nfs4_xattr_entry *oldentry = NULL;
533 bucket = nfs4_xattr_hash_bucket(cache, entry->xattr_name);
534 entry->bucket = bucket;
536 spin_lock(&bucket->lock);
538 if (bucket->draining) {
543 oldentry = nfs4_xattr_get_entry(bucket, entry->xattr_name);
544 if (oldentry != NULL) {
545 hlist_del_init(&oldentry->hnode);
546 nfs4_xattr_entry_lru_del(oldentry);
548 atomic_long_inc(&cache->nent);
551 hlist_add_head(&entry->hnode, &bucket->hlist);
552 nfs4_xattr_entry_lru_add(entry);
555 spin_unlock(&bucket->lock);
557 if (oldentry != NULL)
558 kref_put(&oldentry->ref, nfs4_xattr_free_entry_cb);
564 nfs4_xattr_hash_remove(struct nfs4_xattr_cache *cache, const char *name)
566 struct nfs4_xattr_bucket *bucket;
567 struct nfs4_xattr_entry *entry;
569 bucket = nfs4_xattr_hash_bucket(cache, name);
571 spin_lock(&bucket->lock);
573 entry = nfs4_xattr_get_entry(bucket, name);
575 hlist_del_init(&entry->hnode);
576 nfs4_xattr_entry_lru_del(entry);
577 atomic_long_dec(&cache->nent);
580 spin_unlock(&bucket->lock);
583 kref_put(&entry->ref, nfs4_xattr_free_entry_cb);
586 static struct nfs4_xattr_entry *
587 nfs4_xattr_hash_find(struct nfs4_xattr_cache *cache, const char *name)
589 struct nfs4_xattr_bucket *bucket;
590 struct nfs4_xattr_entry *entry;
592 bucket = nfs4_xattr_hash_bucket(cache, name);
594 spin_lock(&bucket->lock);
596 entry = nfs4_xattr_get_entry(bucket, name);
598 kref_get(&entry->ref);
600 spin_unlock(&bucket->lock);
606 * Entry point to retrieve an entry from the cache.
608 ssize_t nfs4_xattr_cache_get(struct inode *inode, const char *name, char *buf,
611 struct nfs4_xattr_cache *cache;
612 struct nfs4_xattr_entry *entry;
615 cache = nfs4_xattr_get_cache(inode, 0);
620 entry = nfs4_xattr_hash_find(cache, name);
623 dprintk("%s: cache hit '%s', len %lu\n", __func__,
624 entry->xattr_name, (unsigned long)entry->xattr_size);
626 /* Length probe only */
627 ret = entry->xattr_size;
628 } else if (buflen < entry->xattr_size)
631 memcpy(buf, entry->xattr_value, entry->xattr_size);
632 ret = entry->xattr_size;
634 kref_put(&entry->ref, nfs4_xattr_free_entry_cb);
636 dprintk("%s: cache miss '%s'\n", __func__, name);
640 kref_put(&cache->ref, nfs4_xattr_free_cache_cb);
646 * Retrieve a cached list of xattrs from the cache.
648 ssize_t nfs4_xattr_cache_list(struct inode *inode, char *buf, ssize_t buflen)
650 struct nfs4_xattr_cache *cache;
651 struct nfs4_xattr_entry *entry;
654 cache = nfs4_xattr_get_cache(inode, 0);
658 spin_lock(&cache->listxattr_lock);
660 entry = cache->listxattr;
662 if (entry != NULL && entry != ERR_PTR(-ESTALE)) {
664 /* Length probe only */
665 ret = entry->xattr_size;
666 } else if (entry->xattr_size > buflen)
669 memcpy(buf, entry->xattr_value, entry->xattr_size);
670 ret = entry->xattr_size;
676 spin_unlock(&cache->listxattr_lock);
678 kref_put(&cache->ref, nfs4_xattr_free_cache_cb);
684 * Add an xattr to the cache.
686 * This also invalidates the xattr list cache.
688 void nfs4_xattr_cache_add(struct inode *inode, const char *name,
689 const char *buf, struct page **pages, ssize_t buflen)
691 struct nfs4_xattr_cache *cache;
692 struct nfs4_xattr_entry *entry;
694 dprintk("%s: add '%s' len %lu\n", __func__,
695 name, (unsigned long)buflen);
697 cache = nfs4_xattr_get_cache(inode, 1);
701 entry = nfs4_xattr_alloc_entry(name, buf, pages, buflen);
705 (void)nfs4_xattr_set_listcache(cache, NULL);
707 if (!nfs4_xattr_hash_add(cache, entry))
708 kref_put(&entry->ref, nfs4_xattr_free_entry_cb);
711 kref_put(&cache->ref, nfs4_xattr_free_cache_cb);
716 * Remove an xattr from the cache.
718 * This also invalidates the xattr list cache.
720 void nfs4_xattr_cache_remove(struct inode *inode, const char *name)
722 struct nfs4_xattr_cache *cache;
724 dprintk("%s: remove '%s'\n", __func__, name);
726 cache = nfs4_xattr_get_cache(inode, 0);
730 (void)nfs4_xattr_set_listcache(cache, NULL);
731 nfs4_xattr_hash_remove(cache, name);
733 kref_put(&cache->ref, nfs4_xattr_free_cache_cb);
737 * Cache listxattr output, replacing any possible old one.
739 void nfs4_xattr_cache_set_list(struct inode *inode, const char *buf,
742 struct nfs4_xattr_cache *cache;
743 struct nfs4_xattr_entry *entry;
745 cache = nfs4_xattr_get_cache(inode, 1);
749 entry = nfs4_xattr_alloc_entry(NULL, buf, NULL, buflen);
754 * This is just there to be able to get to bucket->cache,
755 * which is obviously the same for all buckets, so just
758 entry->bucket = &cache->buckets[0];
760 if (!nfs4_xattr_set_listcache(cache, entry))
761 kref_put(&entry->ref, nfs4_xattr_free_entry_cb);
764 kref_put(&cache->ref, nfs4_xattr_free_cache_cb);
768 * Zap the entire cache. Called when an inode is evicted.
770 void nfs4_xattr_cache_zap(struct inode *inode)
772 struct nfs4_xattr_cache *oldcache;
774 spin_lock(&inode->i_lock);
775 oldcache = nfs4_xattr_cache_unlink(inode);
776 spin_unlock(&inode->i_lock);
779 nfs4_xattr_discard_cache(oldcache);
783 * The entry LRU is shrunk more aggressively than the cache LRU,
784 * by settings @seeks to 1.
786 * Cache structures are freed only when they've become empty, after
787 * pruning all but one entry.
790 static unsigned long nfs4_xattr_cache_count(struct shrinker *shrink,
791 struct shrink_control *sc);
792 static unsigned long nfs4_xattr_entry_count(struct shrinker *shrink,
793 struct shrink_control *sc);
794 static unsigned long nfs4_xattr_cache_scan(struct shrinker *shrink,
795 struct shrink_control *sc);
796 static unsigned long nfs4_xattr_entry_scan(struct shrinker *shrink,
797 struct shrink_control *sc);
799 static struct shrinker nfs4_xattr_cache_shrinker = {
800 .count_objects = nfs4_xattr_cache_count,
801 .scan_objects = nfs4_xattr_cache_scan,
802 .seeks = DEFAULT_SEEKS,
803 .flags = SHRINKER_MEMCG_AWARE,
806 static struct shrinker nfs4_xattr_entry_shrinker = {
807 .count_objects = nfs4_xattr_entry_count,
808 .scan_objects = nfs4_xattr_entry_scan,
809 .seeks = DEFAULT_SEEKS,
811 .flags = SHRINKER_MEMCG_AWARE,
814 static struct shrinker nfs4_xattr_large_entry_shrinker = {
815 .count_objects = nfs4_xattr_entry_count,
816 .scan_objects = nfs4_xattr_entry_scan,
819 .flags = SHRINKER_MEMCG_AWARE,
822 static enum lru_status
823 cache_lru_isolate(struct list_head *item,
824 struct list_lru_one *lru, spinlock_t *lru_lock, void *arg)
826 struct list_head *dispose = arg;
828 struct nfs4_xattr_cache *cache = container_of(item,
829 struct nfs4_xattr_cache, lru);
831 if (atomic_long_read(&cache->nent) > 1)
835 * If a cache structure is on the LRU list, we know that
836 * its inode is valid. Try to lock it to break the link.
837 * Since we're inverting the lock order here, only try.
839 inode = cache->inode;
841 if (!spin_trylock(&inode->i_lock))
844 kref_get(&cache->ref);
847 NFS_I(inode)->xattr_cache = NULL;
848 NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_XATTR;
849 list_lru_isolate(lru, &cache->lru);
851 spin_unlock(&inode->i_lock);
853 list_add_tail(&cache->dispose, dispose);
858 nfs4_xattr_cache_scan(struct shrinker *shrink, struct shrink_control *sc)
862 struct nfs4_xattr_cache *cache;
864 freed = list_lru_shrink_walk(&nfs4_xattr_cache_lru, sc,
865 cache_lru_isolate, &dispose);
866 while (!list_empty(&dispose)) {
867 cache = list_first_entry(&dispose, struct nfs4_xattr_cache,
869 list_del_init(&cache->dispose);
870 nfs4_xattr_discard_cache(cache);
871 kref_put(&cache->ref, nfs4_xattr_free_cache_cb);
879 nfs4_xattr_cache_count(struct shrinker *shrink, struct shrink_control *sc)
883 count = list_lru_shrink_count(&nfs4_xattr_cache_lru, sc);
884 return vfs_pressure_ratio(count);
887 static enum lru_status
888 entry_lru_isolate(struct list_head *item,
889 struct list_lru_one *lru, spinlock_t *lru_lock, void *arg)
891 struct list_head *dispose = arg;
892 struct nfs4_xattr_bucket *bucket;
893 struct nfs4_xattr_cache *cache;
894 struct nfs4_xattr_entry *entry = container_of(item,
895 struct nfs4_xattr_entry, lru);
897 bucket = entry->bucket;
898 cache = bucket->cache;
901 * Unhook the entry from its parent (either a cache bucket
902 * or a cache structure if it's a listxattr buf), so that
903 * it's no longer found. Then add it to the isolate list,
906 * In both cases, we're reverting lock order, so use
907 * trylock and skip the entry if we can't get the lock.
909 if (entry->xattr_name != NULL) {
910 /* Regular cache entry */
911 if (!spin_trylock(&bucket->lock))
914 kref_get(&entry->ref);
916 hlist_del_init(&entry->hnode);
917 atomic_long_dec(&cache->nent);
918 list_lru_isolate(lru, &entry->lru);
920 spin_unlock(&bucket->lock);
922 /* Listxattr cache entry */
923 if (!spin_trylock(&cache->listxattr_lock))
926 kref_get(&entry->ref);
928 cache->listxattr = NULL;
929 list_lru_isolate(lru, &entry->lru);
931 spin_unlock(&cache->listxattr_lock);
934 list_add_tail(&entry->dispose, dispose);
939 nfs4_xattr_entry_scan(struct shrinker *shrink, struct shrink_control *sc)
943 struct nfs4_xattr_entry *entry;
944 struct list_lru *lru;
946 lru = (shrink == &nfs4_xattr_large_entry_shrinker) ?
947 &nfs4_xattr_large_entry_lru : &nfs4_xattr_entry_lru;
949 freed = list_lru_shrink_walk(lru, sc, entry_lru_isolate, &dispose);
951 while (!list_empty(&dispose)) {
952 entry = list_first_entry(&dispose, struct nfs4_xattr_entry,
954 list_del_init(&entry->dispose);
957 * Drop two references: the one that we just grabbed
958 * in entry_lru_isolate, and the one that was set
959 * when the entry was first allocated.
961 kref_put(&entry->ref, nfs4_xattr_free_entry_cb);
962 kref_put(&entry->ref, nfs4_xattr_free_entry_cb);
969 nfs4_xattr_entry_count(struct shrinker *shrink, struct shrink_control *sc)
972 struct list_lru *lru;
974 lru = (shrink == &nfs4_xattr_large_entry_shrinker) ?
975 &nfs4_xattr_large_entry_lru : &nfs4_xattr_entry_lru;
977 count = list_lru_shrink_count(lru, sc);
978 return vfs_pressure_ratio(count);
982 static void nfs4_xattr_cache_init_once(void *p)
984 struct nfs4_xattr_cache *cache = p;
986 spin_lock_init(&cache->listxattr_lock);
987 atomic_long_set(&cache->nent, 0);
988 nfs4_xattr_hash_init(cache);
989 cache->listxattr = NULL;
990 INIT_LIST_HEAD(&cache->lru);
991 INIT_LIST_HEAD(&cache->dispose);
994 static int nfs4_xattr_shrinker_init(struct shrinker *shrinker,
995 struct list_lru *lru, const char *name)
999 ret = register_shrinker(shrinker, name);
1003 ret = list_lru_init_memcg(lru, shrinker);
1005 unregister_shrinker(shrinker);
1010 static void nfs4_xattr_shrinker_destroy(struct shrinker *shrinker,
1011 struct list_lru *lru)
1013 unregister_shrinker(shrinker);
1014 list_lru_destroy(lru);
1017 int __init nfs4_xattr_cache_init(void)
1021 nfs4_xattr_cache_cachep = kmem_cache_create("nfs4_xattr_cache_cache",
1022 sizeof(struct nfs4_xattr_cache), 0,
1023 (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD),
1024 nfs4_xattr_cache_init_once);
1025 if (nfs4_xattr_cache_cachep == NULL)
1028 ret = nfs4_xattr_shrinker_init(&nfs4_xattr_cache_shrinker,
1029 &nfs4_xattr_cache_lru,
1034 ret = nfs4_xattr_shrinker_init(&nfs4_xattr_entry_shrinker,
1035 &nfs4_xattr_entry_lru,
1040 ret = nfs4_xattr_shrinker_init(&nfs4_xattr_large_entry_shrinker,
1041 &nfs4_xattr_large_entry_lru,
1042 "nfs-xattr_large_entry");
1046 nfs4_xattr_shrinker_destroy(&nfs4_xattr_entry_shrinker,
1047 &nfs4_xattr_entry_lru);
1049 nfs4_xattr_shrinker_destroy(&nfs4_xattr_cache_shrinker,
1050 &nfs4_xattr_cache_lru);
1052 kmem_cache_destroy(nfs4_xattr_cache_cachep);
1057 void nfs4_xattr_cache_exit(void)
1059 nfs4_xattr_shrinker_destroy(&nfs4_xattr_large_entry_shrinker,
1060 &nfs4_xattr_large_entry_lru);
1061 nfs4_xattr_shrinker_destroy(&nfs4_xattr_entry_shrinker,
1062 &nfs4_xattr_entry_lru);
1063 nfs4_xattr_shrinker_destroy(&nfs4_xattr_cache_shrinker,
1064 &nfs4_xattr_cache_lru);
1065 kmem_cache_destroy(nfs4_xattr_cache_cachep);