nfsd: update comment over __nfsd_file_cache_purge
[linux-2.6-block.git] / fs / nfsd / filecache.c
CommitLineData
3f054211 1// SPDX-License-Identifier: GPL-2.0
65294c1f 2/*
3f054211 3 * The NFSD open file cache.
65294c1f
JL
4 *
5 * (c) 2015 - Jeff Layton <jeff.layton@primarydata.com>
b3276c1f
CL
6 *
7 * An nfsd_file object is a per-file collection of open state that binds
8 * together:
9 * - a struct file *
10 * - a user credential
11 * - a network namespace
12 * - a read-ahead context
13 * - monitoring for writeback errors
14 *
15 * nfsd_file objects are reference-counted. Consumers acquire a new
16 * object via the nfsd_file_acquire API. They manage their interest in
17 * the acquired object, and hence the object's reference count, via
18 * nfsd_file_get and nfsd_file_put. There are two varieties of nfsd_file
19 * object:
20 *
21 * * non-garbage-collected: When a consumer wants to precisely control
22 * the lifetime of a file's open state, it acquires a non-garbage-
23 * collected nfsd_file. The final nfsd_file_put releases the open
24 * state immediately.
25 *
26 * * garbage-collected: When a consumer does not control the lifetime
27 * of open state, it acquires a garbage-collected nfsd_file. The
28 * final nfsd_file_put allows the open state to linger for a period
29 * during which it may be re-used.
65294c1f
JL
30 */
31
32#include <linux/hash.h>
33#include <linux/slab.h>
65294c1f 34#include <linux/file.h>
cbcc268b 35#include <linux/pagemap.h>
65294c1f
JL
36#include <linux/sched.h>
37#include <linux/list_lru.h>
38#include <linux/fsnotify_backend.h>
39#include <linux/fsnotify.h>
40#include <linux/seq_file.h>
fc22945e 41#include <linux/rhashtable.h>
65294c1f
JL
42
43#include "vfs.h"
44#include "nfsd.h"
45#include "nfsfh.h"
5e113224 46#include "netns.h"
65294c1f
JL
47#include "filecache.h"
48#include "trace.h"
49
65294c1f
JL
50#define NFSD_LAUNDRETTE_DELAY (2 * HZ)
51
c7b824c3 52#define NFSD_FILE_CACHE_UP (0)
65294c1f
JL
53
54/* We only care about NFSD_MAY_READ/WRITE for this cache */
55#define NFSD_FILE_MAY_MASK (NFSD_MAY_READ|NFSD_MAY_WRITE)
56
65294c1f 57static DEFINE_PER_CPU(unsigned long, nfsd_file_cache_hits);
29d4bdbb 58static DEFINE_PER_CPU(unsigned long, nfsd_file_acquisitions);
d6329327 59static DEFINE_PER_CPU(unsigned long, nfsd_file_releases);
904940e9 60static DEFINE_PER_CPU(unsigned long, nfsd_file_total_age);
94660cc1 61static DEFINE_PER_CPU(unsigned long, nfsd_file_evictions);
65294c1f 62
9542e6a6 63struct nfsd_fcache_disposal {
9542e6a6 64 struct work_struct work;
9542e6a6
TM
65 spinlock_t lock;
66 struct list_head freeme;
9542e6a6
TM
67};
68
50d0def9 69static struct workqueue_struct *nfsd_filecache_wq __read_mostly;
9542e6a6 70
65294c1f
JL
71static struct kmem_cache *nfsd_file_slab;
72static struct kmem_cache *nfsd_file_mark_slab;
65294c1f 73static struct list_lru nfsd_file_lru;
c7b824c3 74static unsigned long nfsd_file_flags;
65294c1f 75static struct fsnotify_group *nfsd_file_fsnotify_group;
65294c1f 76static struct delayed_work nfsd_filecache_laundrette;
fc22945e
CL
77static struct rhashtable nfsd_file_rhash_tbl
78 ____cacheline_aligned_in_smp;
79
80enum nfsd_file_lookup_type {
81 NFSD_FILE_KEY_INODE,
82 NFSD_FILE_KEY_FULL,
83};
84
85struct nfsd_file_lookup_key {
86 struct inode *inode;
87 struct net *net;
88 const struct cred *cred;
89 unsigned char need;
4d1ea845 90 bool gc;
fc22945e
CL
91 enum nfsd_file_lookup_type type;
92};
93
94/*
95 * The returned hash value is based solely on the address of an in-code
96 * inode, a pointer to a slab-allocated object. The entropy in such a
97 * pointer is concentrated in its middle bits.
98 */
99static u32 nfsd_file_inode_hash(const struct inode *inode, u32 seed)
100{
101 unsigned long ptr = (unsigned long)inode;
102 u32 k;
103
104 k = ptr >> L1_CACHE_SHIFT;
105 k &= 0x00ffffff;
106 return jhash2(&k, 1, seed);
107}
108
109/**
110 * nfsd_file_key_hashfn - Compute the hash value of a lookup key
111 * @data: key on which to compute the hash value
112 * @len: rhash table's key_len parameter (unused)
113 * @seed: rhash table's random seed of the day
114 *
115 * Return value:
116 * Computed 32-bit hash value
117 */
118static u32 nfsd_file_key_hashfn(const void *data, u32 len, u32 seed)
119{
120 const struct nfsd_file_lookup_key *key = data;
121
122 return nfsd_file_inode_hash(key->inode, seed);
123}
124
125/**
126 * nfsd_file_obj_hashfn - Compute the hash value of an nfsd_file
127 * @data: object on which to compute the hash value
128 * @len: rhash table's key_len parameter (unused)
129 * @seed: rhash table's random seed of the day
130 *
131 * Return value:
132 * Computed 32-bit hash value
133 */
134static u32 nfsd_file_obj_hashfn(const void *data, u32 len, u32 seed)
135{
136 const struct nfsd_file *nf = data;
137
138 return nfsd_file_inode_hash(nf->nf_inode, seed);
139}
65294c1f 140
fc22945e
CL
141static bool
142nfsd_match_cred(const struct cred *c1, const struct cred *c2)
143{
144 int i;
145
146 if (!uid_eq(c1->fsuid, c2->fsuid))
147 return false;
148 if (!gid_eq(c1->fsgid, c2->fsgid))
149 return false;
150 if (c1->group_info == NULL || c2->group_info == NULL)
151 return c1->group_info == c2->group_info;
152 if (c1->group_info->ngroups != c2->group_info->ngroups)
153 return false;
154 for (i = 0; i < c1->group_info->ngroups; i++) {
155 if (!gid_eq(c1->group_info->gid[i], c2->group_info->gid[i]))
156 return false;
157 }
158 return true;
159}
160
161/**
162 * nfsd_file_obj_cmpfn - Match a cache item against search criteria
163 * @arg: search criteria
164 * @ptr: cache item to check
165 *
166 * Return values:
167 * %0 - Item matches search criteria
168 * %1 - Item does not match search criteria
169 */
170static int nfsd_file_obj_cmpfn(struct rhashtable_compare_arg *arg,
171 const void *ptr)
172{
173 const struct nfsd_file_lookup_key *key = arg->key;
174 const struct nfsd_file *nf = ptr;
175
176 switch (key->type) {
177 case NFSD_FILE_KEY_INODE:
6c31e4c9
JL
178 if (test_bit(NFSD_FILE_GC, &nf->nf_flags) != key->gc)
179 return 1;
fc22945e
CL
180 if (nf->nf_inode != key->inode)
181 return 1;
182 break;
183 case NFSD_FILE_KEY_FULL:
184 if (nf->nf_inode != key->inode)
185 return 1;
186 if (nf->nf_may != key->need)
187 return 1;
188 if (nf->nf_net != key->net)
189 return 1;
190 if (!nfsd_match_cred(nf->nf_cred, key->cred))
191 return 1;
d69b8dbf 192 if (test_bit(NFSD_FILE_GC, &nf->nf_flags) != key->gc)
4d1ea845 193 return 1;
fc22945e
CL
194 if (test_bit(NFSD_FILE_HASHED, &nf->nf_flags) == 0)
195 return 1;
196 break;
197 }
198 return 0;
199}
200
201static const struct rhashtable_params nfsd_file_rhash_params = {
202 .key_len = sizeof_field(struct nfsd_file, nf_inode),
203 .key_offset = offsetof(struct nfsd_file, nf_inode),
204 .head_offset = offsetof(struct nfsd_file, nf_rhash),
205 .hashfn = nfsd_file_key_hashfn,
206 .obj_hashfn = nfsd_file_obj_hashfn,
207 .obj_cmpfn = nfsd_file_obj_cmpfn,
208 /* Reduce resizing churn on light workloads */
209 .min_size = 512, /* buckets */
210 .automatic_shrinking = true,
211};
65294c1f
JL
212
213static void
9542e6a6 214nfsd_file_schedule_laundrette(void)
65294c1f 215{
22ae4c11
JL
216 if (test_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags))
217 queue_delayed_work(system_wq, &nfsd_filecache_laundrette,
218 NFSD_LAUNDRETTE_DELAY);
65294c1f
JL
219}
220
221static void
222nfsd_file_slab_free(struct rcu_head *rcu)
223{
224 struct nfsd_file *nf = container_of(rcu, struct nfsd_file, nf_rcu);
225
226 put_cred(nf->nf_cred);
227 kmem_cache_free(nfsd_file_slab, nf);
228}
229
230static void
231nfsd_file_mark_free(struct fsnotify_mark *mark)
232{
233 struct nfsd_file_mark *nfm = container_of(mark, struct nfsd_file_mark,
234 nfm_mark);
235
236 kmem_cache_free(nfsd_file_mark_slab, nfm);
237}
238
239static struct nfsd_file_mark *
240nfsd_file_mark_get(struct nfsd_file_mark *nfm)
241{
689827cd 242 if (!refcount_inc_not_zero(&nfm->nfm_ref))
65294c1f
JL
243 return NULL;
244 return nfm;
245}
246
247static void
248nfsd_file_mark_put(struct nfsd_file_mark *nfm)
249{
689827cd 250 if (refcount_dec_and_test(&nfm->nfm_ref)) {
65294c1f
JL
251 fsnotify_destroy_mark(&nfm->nfm_mark, nfsd_file_fsnotify_group);
252 fsnotify_put_mark(&nfm->nfm_mark);
253 }
254}
255
256static struct nfsd_file_mark *
427f5f83 257nfsd_file_mark_find_or_create(struct nfsd_file *nf, struct inode *inode)
65294c1f
JL
258{
259 int err;
260 struct fsnotify_mark *mark;
261 struct nfsd_file_mark *nfm = NULL, *new;
65294c1f
JL
262
263 do {
b8962a9d 264 fsnotify_group_lock(nfsd_file_fsnotify_group);
65294c1f 265 mark = fsnotify_find_mark(&inode->i_fsnotify_marks,
b8962a9d 266 nfsd_file_fsnotify_group);
65294c1f
JL
267 if (mark) {
268 nfm = nfsd_file_mark_get(container_of(mark,
269 struct nfsd_file_mark,
270 nfm_mark));
b8962a9d 271 fsnotify_group_unlock(nfsd_file_fsnotify_group);
90d2f1da
TM
272 if (nfm) {
273 fsnotify_put_mark(mark);
65294c1f 274 break;
90d2f1da
TM
275 }
276 /* Avoid soft lockup race with nfsd_file_mark_put() */
277 fsnotify_destroy_mark(mark, nfsd_file_fsnotify_group);
278 fsnotify_put_mark(mark);
b8962a9d
AG
279 } else {
280 fsnotify_group_unlock(nfsd_file_fsnotify_group);
281 }
65294c1f
JL
282
283 /* allocate a new nfm */
284 new = kmem_cache_alloc(nfsd_file_mark_slab, GFP_KERNEL);
285 if (!new)
286 return NULL;
287 fsnotify_init_mark(&new->nfm_mark, nfsd_file_fsnotify_group);
288 new->nfm_mark.mask = FS_ATTRIB|FS_DELETE_SELF;
689827cd 289 refcount_set(&new->nfm_ref, 1);
65294c1f
JL
290
291 err = fsnotify_add_inode_mark(&new->nfm_mark, inode, 0);
292
293 /*
294 * If the add was successful, then return the object.
295 * Otherwise, we need to put the reference we hold on the
296 * nfm_mark. The fsnotify code will take a reference and put
297 * it on failure, so we can't just free it directly. It's also
298 * not safe to call fsnotify_destroy_mark on it as the
299 * mark->group will be NULL. Thus, we can't let the nfm_ref
300 * counter drive the destruction at this point.
301 */
302 if (likely(!err))
303 nfm = new;
304 else
305 fsnotify_put_mark(&new->nfm_mark);
306 } while (unlikely(err == -EEXIST));
307
308 return nfm;
309}
310
311static struct nfsd_file *
ce502f81 312nfsd_file_alloc(struct nfsd_file_lookup_key *key, unsigned int may)
65294c1f
JL
313{
314 struct nfsd_file *nf;
315
316 nf = kmem_cache_alloc(nfsd_file_slab, GFP_KERNEL);
317 if (nf) {
65294c1f 318 INIT_LIST_HEAD(&nf->nf_lru);
904940e9 319 nf->nf_birthtime = ktime_get();
65294c1f
JL
320 nf->nf_file = NULL;
321 nf->nf_cred = get_current_cred();
ce502f81 322 nf->nf_net = key->net;
65294c1f 323 nf->nf_flags = 0;
ce502f81
CL
324 __set_bit(NFSD_FILE_HASHED, &nf->nf_flags);
325 __set_bit(NFSD_FILE_PENDING, &nf->nf_flags);
4d1ea845
CL
326 if (key->gc)
327 __set_bit(NFSD_FILE_GC, &nf->nf_flags);
ce502f81 328 nf->nf_inode = key->inode;
ac3a2585 329 refcount_set(&nf->nf_ref, 1);
ce502f81 330 nf->nf_may = key->need;
65294c1f 331 nf->nf_mark = NULL;
65294c1f
JL
332 }
333 return nf;
334}
335
4c475eee
JL
336/**
337 * nfsd_file_check_write_error - check for writeback errors on a file
338 * @nf: nfsd_file to check for writeback errors
339 *
340 * Check whether a nfsd_file has an unseen error. Reset the write
341 * verifier if so.
342 */
82141185 343static void
82141185
JL
344nfsd_file_check_write_error(struct nfsd_file *nf)
345{
346 struct file *file = nf->nf_file;
347
4c475eee
JL
348 if ((file->f_mode & FMODE_WRITE) &&
349 filemap_check_wb_err(file->f_mapping, READ_ONCE(file->f_wb_err)))
350 nfsd_reset_write_verifier(net_generic(nf->nf_net, nfsd_net_id));
82141185
JL
351}
352
353static void
354nfsd_file_hash_remove(struct nfsd_file *nf)
355{
356 trace_nfsd_file_unhash(nf);
82141185
JL
357 rhashtable_remove_fast(&nfsd_file_rhash_tbl, &nf->nf_rhash,
358 nfsd_file_rhash_params);
359}
360
361static bool
362nfsd_file_unhash(struct nfsd_file *nf)
363{
364 if (test_and_clear_bit(NFSD_FILE_HASHED, &nf->nf_flags)) {
365 nfsd_file_hash_remove(nf);
366 return true;
367 }
368 return false;
369}
370
ac3a2585 371static void
65294c1f
JL
372nfsd_file_free(struct nfsd_file *nf)
373{
904940e9 374 s64 age = ktime_to_ms(ktime_sub(ktime_get(), nf->nf_birthtime));
65294c1f 375
82141185
JL
376 trace_nfsd_file_free(nf);
377
d6329327 378 this_cpu_inc(nfsd_file_releases);
904940e9 379 this_cpu_add(nfsd_file_total_age, age);
d6329327 380
ac3a2585 381 nfsd_file_unhash(nf);
65294c1f
JL
382 if (nf->nf_mark)
383 nfsd_file_mark_put(nf->nf_mark);
384 if (nf->nf_file) {
4c475eee 385 nfsd_file_check_write_error(nf);
b2ff1bd7 386 filp_close(nf->nf_file, NULL);
65294c1f 387 }
668ed92e
CL
388
389 /*
390 * If this item is still linked via nf_lru, that's a bug.
391 * WARN and leak it to preserve system stability.
392 */
393 if (WARN_ON_ONCE(!list_empty(&nf->nf_lru)))
ac3a2585 394 return;
668ed92e 395
65294c1f 396 call_rcu(&nf->nf_rcu, nfsd_file_slab_free);
65294c1f
JL
397}
398
055b24a8
TM
399static bool
400nfsd_file_check_writeback(struct nfsd_file *nf)
401{
402 struct file *file = nf->nf_file;
403 struct address_space *mapping;
404
405 if (!file || !(file->f_mode & FMODE_WRITE))
406 return false;
407 mapping = file->f_mapping;
408 return mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) ||
409 mapping_tagged(mapping, PAGECACHE_TAG_WRITEBACK);
410}
411
ac3a2585 412static bool nfsd_file_lru_add(struct nfsd_file *nf)
65294c1f 413{
4a0e73e6 414 set_bit(NFSD_FILE_REFERENCED, &nf->nf_flags);
ac3a2585 415 if (list_lru_add(&nfsd_file_lru, &nf->nf_lru)) {
c46203ac 416 trace_nfsd_file_lru_add(nf);
ac3a2585
JL
417 return true;
418 }
419 return false;
c46203ac 420}
65294c1f 421
ac3a2585 422static bool nfsd_file_lru_remove(struct nfsd_file *nf)
c46203ac 423{
ac3a2585 424 if (list_lru_del(&nfsd_file_lru, &nf->nf_lru)) {
c46203ac 425 trace_nfsd_file_lru_del(nf);
ac3a2585
JL
426 return true;
427 }
428 return false;
c46203ac
CL
429}
430
82141185
JL
431struct nfsd_file *
432nfsd_file_get(struct nfsd_file *nf)
65294c1f 433{
70f62231 434 if (nf && refcount_inc_not_zero(&nf->nf_ref))
82141185
JL
435 return nf;
436 return NULL;
65294c1f
JL
437}
438
ac3a2585
JL
439/**
440 * nfsd_file_put - put the reference to a nfsd_file
441 * @nf: nfsd_file of which to put the reference
442 *
443 * Put a reference to a nfsd_file. In the non-GC case, we just put the
444 * reference immediately. In the GC case, if the reference would be
445 * the last one, the put it on the LRU instead to be cleaned up later.
446 */
65294c1f
JL
447void
448nfsd_file_put(struct nfsd_file *nf)
449{
08af54b3 450 might_sleep();
ac3a2585 451 trace_nfsd_file_put(nf);
08af54b3 452
ac3a2585
JL
453 if (test_bit(NFSD_FILE_GC, &nf->nf_flags) &&
454 test_bit(NFSD_FILE_HASHED, &nf->nf_flags)) {
455 /*
456 * If this is the last reference (nf_ref == 1), then try to
457 * transfer it to the LRU.
458 */
459 if (refcount_dec_not_one(&nf->nf_ref))
460 return;
461
462 /* Try to add it to the LRU. If that fails, decrement. */
463 if (nfsd_file_lru_add(nf)) {
464 /* If it's still hashed, we're done */
465 if (test_bit(NFSD_FILE_HASHED, &nf->nf_flags)) {
466 nfsd_file_schedule_laundrette();
467 return;
468 }
65294c1f 469
ac3a2585
JL
470 /*
471 * We're racing with unhashing, so try to remove it from
472 * the LRU. If removal fails, then someone else already
473 * has our reference.
474 */
475 if (!nfsd_file_lru_remove(nf))
476 return;
477 }
65294c1f 478 }
ac3a2585
JL
479 if (refcount_dec_and_test(&nf->nf_ref))
480 nfsd_file_free(nf);
65294c1f
JL
481}
482
483static void
ac3a2585 484nfsd_file_dispose_list(struct list_head *dispose)
65294c1f 485{
65294c1f
JL
486 struct nfsd_file *nf;
487
ac3a2585 488 while (!list_empty(dispose)) {
65294c1f 489 nf = list_first_entry(dispose, struct nfsd_file, nf_lru);
668ed92e 490 list_del_init(&nf->nf_lru);
ac3a2585 491 nfsd_file_free(nf);
65294c1f 492 }
65294c1f
JL
493}
494
9542e6a6
TM
495static void
496nfsd_file_list_remove_disposal(struct list_head *dst,
497 struct nfsd_fcache_disposal *l)
498{
499 spin_lock(&l->lock);
500 list_splice_init(&l->freeme, dst);
501 spin_unlock(&l->lock);
502}
503
504static void
505nfsd_file_list_add_disposal(struct list_head *files, struct net *net)
506{
1463b38e
N
507 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
508 struct nfsd_fcache_disposal *l = nn->fcache_disposal;
9542e6a6 509
1463b38e
N
510 spin_lock(&l->lock);
511 list_splice_tail_init(files, &l->freeme);
512 spin_unlock(&l->lock);
513 queue_work(nfsd_filecache_wq, &l->work);
9542e6a6
TM
514}
515
516static void
517nfsd_file_list_add_pernet(struct list_head *dst, struct list_head *src,
518 struct net *net)
519{
520 struct nfsd_file *nf, *tmp;
521
522 list_for_each_entry_safe(nf, tmp, src, nf_lru) {
523 if (nf->nf_net == net)
524 list_move_tail(&nf->nf_lru, dst);
525 }
526}
527
528static void
529nfsd_file_dispose_list_delayed(struct list_head *dispose)
530{
531 LIST_HEAD(list);
532 struct nfsd_file *nf;
533
534 while(!list_empty(dispose)) {
535 nf = list_first_entry(dispose, struct nfsd_file, nf_lru);
536 nfsd_file_list_add_pernet(&list, dispose, nf->nf_net);
537 nfsd_file_list_add_disposal(&list, nf->nf_net);
538 }
539}
540
4a0e73e6
CL
541/**
542 * nfsd_file_lru_cb - Examine an entry on the LRU list
543 * @item: LRU entry to examine
544 * @lru: controlling LRU
545 * @lock: LRU list lock (unused)
546 * @arg: dispose list
547 *
4a0e73e6
CL
548 * Return values:
549 * %LRU_REMOVED: @item was removed from the LRU
edead3a5 550 * %LRU_ROTATE: @item is to be moved to the LRU tail
4a0e73e6 551 * %LRU_SKIP: @item cannot be evicted
65294c1f
JL
552 */
553static enum lru_status
554nfsd_file_lru_cb(struct list_head *item, struct list_lru_one *lru,
555 spinlock_t *lock, void *arg)
556 __releases(lock)
557 __acquires(lock)
558{
559 struct list_head *head = arg;
560 struct nfsd_file *nf = list_entry(item, struct nfsd_file, nf_lru);
561
ac3a2585
JL
562 /* We should only be dealing with GC entries here */
563 WARN_ON_ONCE(!test_bit(NFSD_FILE_GC, &nf->nf_flags));
055b24a8
TM
564
565 /*
566 * Don't throw out files that are still undergoing I/O or
567 * that have uncleared errors pending.
568 */
c46203ac
CL
569 if (nfsd_file_check_writeback(nf)) {
570 trace_nfsd_file_gc_writeback(nf);
571 return LRU_SKIP;
572 }
055b24a8 573
ac3a2585 574 /* If it was recently added to the list, skip it */
c46203ac
CL
575 if (test_and_clear_bit(NFSD_FILE_REFERENCED, &nf->nf_flags)) {
576 trace_nfsd_file_gc_referenced(nf);
edead3a5 577 return LRU_ROTATE;
c46203ac 578 }
65294c1f 579
ac3a2585
JL
580 /*
581 * Put the reference held on behalf of the LRU. If it wasn't the last
582 * one, then just remove it from the LRU and ignore it.
583 */
584 if (!refcount_dec_and_test(&nf->nf_ref)) {
585 trace_nfsd_file_gc_in_use(nf);
586 list_lru_isolate(lru, &nf->nf_lru);
587 return LRU_REMOVED;
c46203ac 588 }
65294c1f 589
ac3a2585
JL
590 /* Refcount went to zero. Unhash it and queue it to the dispose list */
591 nfsd_file_unhash(nf);
65294c1f 592 list_lru_isolate_move(lru, &nf->nf_lru, head);
94660cc1 593 this_cpu_inc(nfsd_file_evictions);
c46203ac 594 trace_nfsd_file_gc_disposed(nf);
65294c1f 595 return LRU_REMOVED;
65294c1f
JL
596}
597
9542e6a6
TM
598static void
599nfsd_file_gc(void)
600{
3bc6d347 601 LIST_HEAD(dispose);
94660cc1 602 unsigned long ret;
3bc6d347 603
94660cc1 604 ret = list_lru_walk(&nfsd_file_lru, nfsd_file_lru_cb,
edead3a5 605 &dispose, list_lru_count(&nfsd_file_lru));
94660cc1 606 trace_nfsd_file_gc_removed(ret, list_lru_count(&nfsd_file_lru));
ac3a2585 607 nfsd_file_dispose_list_delayed(&dispose);
9542e6a6
TM
608}
609
610static void
611nfsd_file_gc_worker(struct work_struct *work)
612{
613 nfsd_file_gc();
22ae4c11
JL
614 if (list_lru_count(&nfsd_file_lru))
615 nfsd_file_schedule_laundrette();
65294c1f
JL
616}
617
618static unsigned long
619nfsd_file_lru_count(struct shrinker *s, struct shrink_control *sc)
620{
621 return list_lru_count(&nfsd_file_lru);
622}
623
624static unsigned long
625nfsd_file_lru_scan(struct shrinker *s, struct shrink_control *sc)
626{
39f1d1ff
CL
627 LIST_HEAD(dispose);
628 unsigned long ret;
629
630 ret = list_lru_shrink_walk(&nfsd_file_lru, sc,
631 nfsd_file_lru_cb, &dispose);
94660cc1 632 trace_nfsd_file_shrinker_removed(ret, list_lru_count(&nfsd_file_lru));
ac3a2585 633 nfsd_file_dispose_list_delayed(&dispose);
39f1d1ff 634 return ret;
65294c1f
JL
635}
636
637static struct shrinker nfsd_file_shrinker = {
638 .scan_objects = nfsd_file_lru_scan,
639 .count_objects = nfsd_file_lru_count,
640 .seeks = 1,
641};
642
4bdbba54
JL
643/**
644 * nfsd_file_cond_queue - conditionally unhash and queue a nfsd_file
645 * @nf: nfsd_file to attempt to queue
646 * @dispose: private list to queue successfully-put objects
647 *
648 * Unhash an nfsd_file, try to get a reference to it, and then put that
649 * reference. If it's the last reference, queue it to the dispose list.
650 */
651static void
652nfsd_file_cond_queue(struct nfsd_file *nf, struct list_head *dispose)
653 __must_hold(RCU)
654{
655 int decrement = 1;
656
657 /* If we raced with someone else unhashing, ignore it */
658 if (!nfsd_file_unhash(nf))
659 return;
660
661 /* If we can't get a reference, ignore it */
662 if (!nfsd_file_get(nf))
663 return;
664
665 /* Extra decrement if we remove from the LRU */
666 if (nfsd_file_lru_remove(nf))
667 ++decrement;
668
669 /* If refcount goes to 0, then put on the dispose list */
670 if (refcount_sub_and_test(decrement, &nf->nf_ref)) {
671 list_add(&nf->nf_lru, dispose);
672 trace_nfsd_file_closing(nf);
673 }
674}
675
ac3a2585
JL
676/**
677 * nfsd_file_queue_for_close: try to close out any open nfsd_files for an inode
678 * @inode: inode on which to close out nfsd_files
679 * @dispose: list on which to gather nfsd_files to close out
680 *
681 * An nfsd_file represents a struct file being held open on behalf of nfsd. An
682 * open file however can block other activity (such as leases), or cause
683 * undesirable behavior (e.g. spurious silly-renames when reexporting NFS).
684 *
685 * This function is intended to find open nfsd_files when this sort of
686 * conflicting access occurs and then attempt to close those files out.
687 *
688 * Populates the dispose list with entries that have already had their
689 * refcounts go to zero. The actual free of an nfsd_file can be expensive,
690 * so we leave it up to the caller whether it wants to wait or not.
a8455110 691 */
ac3a2585
JL
692static void
693nfsd_file_queue_for_close(struct inode *inode, struct list_head *dispose)
65294c1f 694{
ce502f81
CL
695 struct nfsd_file_lookup_key key = {
696 .type = NFSD_FILE_KEY_INODE,
697 .inode = inode,
6c31e4c9 698 .gc = true,
ce502f81 699 };
ce502f81 700 struct nfsd_file *nf;
65294c1f 701
ce502f81
CL
702 rcu_read_lock();
703 do {
704 nf = rhashtable_lookup(&nfsd_file_rhash_tbl, &key,
705 nfsd_file_rhash_params);
706 if (!nf)
707 break;
4bdbba54 708 nfsd_file_cond_queue(nf, dispose);
ce502f81
CL
709 } while (1);
710 rcu_read_unlock();
65294c1f
JL
711}
712
713/**
ac3a2585 714 * nfsd_file_close_inode - attempt a delayed close of a nfsd_file
65294c1f
JL
715 * @inode: inode of the file to attempt to remove
716 *
ac3a2585
JL
717 * Close out any open nfsd_files that can be reaped for @inode. The
718 * actual freeing is deferred to the dispose_list_delayed infrastructure.
719 *
720 * This is used by the fsnotify callbacks and setlease notifier.
65294c1f 721 */
ac3a2585
JL
722static void
723nfsd_file_close_inode(struct inode *inode)
65294c1f 724{
65294c1f
JL
725 LIST_HEAD(dispose);
726
ac3a2585
JL
727 nfsd_file_queue_for_close(inode, &dispose);
728 nfsd_file_dispose_list_delayed(&dispose);
65294c1f
JL
729}
730
731/**
ac3a2585 732 * nfsd_file_close_inode_sync - attempt to forcibly close a nfsd_file
65294c1f
JL
733 * @inode: inode of the file to attempt to remove
734 *
ac3a2585
JL
735 * Close out any open nfsd_files that can be reaped for @inode. The
736 * nfsd_files are closed out synchronously.
737 *
738 * This is called from nfsd_rename and nfsd_unlink to avoid silly-renames
739 * when reexporting NFS.
65294c1f 740 */
ac3a2585
JL
741void
742nfsd_file_close_inode_sync(struct inode *inode)
65294c1f 743{
ac3a2585 744 struct nfsd_file *nf;
65294c1f
JL
745 LIST_HEAD(dispose);
746
ac3a2585
JL
747 trace_nfsd_file_close(inode);
748
749 nfsd_file_queue_for_close(inode, &dispose);
750 while (!list_empty(&dispose)) {
751 nf = list_first_entry(&dispose, struct nfsd_file, nf_lru);
752 list_del_init(&nf->nf_lru);
753 nfsd_file_free(nf);
754 }
755 flush_delayed_fput();
65294c1f
JL
756}
757
758/**
759 * nfsd_file_delayed_close - close unused nfsd_files
760 * @work: dummy
761 *
ac3a2585 762 * Walk the LRU list and destroy any entries that have not been used since
65294c1f 763 * the last scan.
65294c1f
JL
764 */
765static void
766nfsd_file_delayed_close(struct work_struct *work)
767{
768 LIST_HEAD(head);
9542e6a6
TM
769 struct nfsd_fcache_disposal *l = container_of(work,
770 struct nfsd_fcache_disposal, work);
65294c1f 771
9542e6a6
TM
772 nfsd_file_list_remove_disposal(&head, l);
773 nfsd_file_dispose_list(&head);
65294c1f
JL
774}
775
776static int
777nfsd_file_lease_notifier_call(struct notifier_block *nb, unsigned long arg,
778 void *data)
779{
780 struct file_lock *fl = data;
781
782 /* Only close files for F_SETLEASE leases */
783 if (fl->fl_flags & FL_LEASE)
ac3a2585 784 nfsd_file_close_inode(file_inode(fl->fl_file));
65294c1f
JL
785 return 0;
786}
787
788static struct notifier_block nfsd_file_lease_notifier = {
789 .notifier_call = nfsd_file_lease_notifier_call,
790};
791
792static int
b9a1b977
AG
793nfsd_file_fsnotify_handle_event(struct fsnotify_mark *mark, u32 mask,
794 struct inode *inode, struct inode *dir,
950cc0d2 795 const struct qstr *name, u32 cookie)
65294c1f 796{
24dca905
GKB
797 if (WARN_ON_ONCE(!inode))
798 return 0;
799
65294c1f
JL
800 trace_nfsd_file_fsnotify_handle_event(inode, mask);
801
802 /* Should be no marks on non-regular files */
803 if (!S_ISREG(inode->i_mode)) {
804 WARN_ON_ONCE(1);
805 return 0;
806 }
807
808 /* don't close files if this was not the last link */
809 if (mask & FS_ATTRIB) {
810 if (inode->i_nlink)
811 return 0;
812 }
813
814 nfsd_file_close_inode(inode);
815 return 0;
816}
817
818
819static const struct fsnotify_ops nfsd_file_fsnotify_ops = {
b9a1b977 820 .handle_inode_event = nfsd_file_fsnotify_handle_event,
65294c1f
JL
821 .free_mark = nfsd_file_mark_free,
822};
823
824int
825nfsd_file_cache_init(void)
826{
0ec8e9d1 827 int ret;
65294c1f 828
c7b824c3
CL
829 lockdep_assert_held(&nfsd_mutex);
830 if (test_and_set_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 1)
65294c1f
JL
831 return 0;
832
fc22945e
CL
833 ret = rhashtable_init(&nfsd_file_rhash_tbl, &nfsd_file_rhash_params);
834 if (ret)
835 return ret;
836
837 ret = -ENOMEM;
9542e6a6
TM
838 nfsd_filecache_wq = alloc_workqueue("nfsd_filecache", 0, 0);
839 if (!nfsd_filecache_wq)
840 goto out;
841
65294c1f
JL
842 nfsd_file_slab = kmem_cache_create("nfsd_file",
843 sizeof(struct nfsd_file), 0, 0, NULL);
844 if (!nfsd_file_slab) {
845 pr_err("nfsd: unable to create nfsd_file_slab\n");
846 goto out_err;
847 }
848
849 nfsd_file_mark_slab = kmem_cache_create("nfsd_file_mark",
850 sizeof(struct nfsd_file_mark), 0, 0, NULL);
851 if (!nfsd_file_mark_slab) {
852 pr_err("nfsd: unable to create nfsd_file_mark_slab\n");
853 goto out_err;
854 }
855
856
857 ret = list_lru_init(&nfsd_file_lru);
858 if (ret) {
859 pr_err("nfsd: failed to init nfsd_file_lru: %d\n", ret);
860 goto out_err;
861 }
862
e33c267a 863 ret = register_shrinker(&nfsd_file_shrinker, "nfsd-filecache");
65294c1f
JL
864 if (ret) {
865 pr_err("nfsd: failed to register nfsd_file_shrinker: %d\n", ret);
866 goto out_lru;
867 }
868
869 ret = lease_register_notifier(&nfsd_file_lease_notifier);
870 if (ret) {
871 pr_err("nfsd: unable to register lease notifier: %d\n", ret);
872 goto out_shrinker;
873 }
874
867a448d 875 nfsd_file_fsnotify_group = fsnotify_alloc_group(&nfsd_file_fsnotify_ops,
b8962a9d 876 FSNOTIFY_GROUP_NOFS);
65294c1f
JL
877 if (IS_ERR(nfsd_file_fsnotify_group)) {
878 pr_err("nfsd: unable to create fsnotify group: %ld\n",
879 PTR_ERR(nfsd_file_fsnotify_group));
231307df 880 ret = PTR_ERR(nfsd_file_fsnotify_group);
65294c1f
JL
881 nfsd_file_fsnotify_group = NULL;
882 goto out_notifier;
883 }
884
9542e6a6 885 INIT_DELAYED_WORK(&nfsd_filecache_laundrette, nfsd_file_gc_worker);
65294c1f
JL
886out:
887 return ret;
888out_notifier:
889 lease_unregister_notifier(&nfsd_file_lease_notifier);
890out_shrinker:
891 unregister_shrinker(&nfsd_file_shrinker);
892out_lru:
893 list_lru_destroy(&nfsd_file_lru);
894out_err:
895 kmem_cache_destroy(nfsd_file_slab);
896 nfsd_file_slab = NULL;
897 kmem_cache_destroy(nfsd_file_mark_slab);
898 nfsd_file_mark_slab = NULL;
9542e6a6
TM
899 destroy_workqueue(nfsd_filecache_wq);
900 nfsd_filecache_wq = NULL;
fc22945e 901 rhashtable_destroy(&nfsd_file_rhash_tbl);
65294c1f
JL
902 goto out;
903}
904
ac3a2585
JL
905/**
906 * __nfsd_file_cache_purge: clean out the cache for shutdown
907 * @net: net-namespace to shut down the cache (may be NULL)
908 *
909 * Walk the nfsd_file cache and close out any that match @net. If @net is NULL,
972cc0e0
JL
910 * then close out everything. Called when an nfsd instance is being shut down,
911 * and when the exports table is flushed.
ac3a2585 912 */
c7b824c3
CL
913static void
914__nfsd_file_cache_purge(struct net *net)
65294c1f 915{
ce502f81
CL
916 struct rhashtable_iter iter;
917 struct nfsd_file *nf;
65294c1f 918 LIST_HEAD(dispose);
65294c1f 919
ce502f81
CL
920 rhashtable_walk_enter(&nfsd_file_rhash_tbl, &iter);
921 do {
922 rhashtable_walk_start(&iter);
5e113224 923
ce502f81
CL
924 nf = rhashtable_walk_next(&iter);
925 while (!IS_ERR_OR_NULL(nf)) {
4bdbba54
JL
926 if (!net || nf->nf_net == net)
927 nfsd_file_cond_queue(nf, &dispose);
ce502f81 928 nf = rhashtable_walk_next(&iter);
65294c1f 929 }
ce502f81
CL
930
931 rhashtable_walk_stop(&iter);
932 } while (nf == ERR_PTR(-EAGAIN));
933 rhashtable_walk_exit(&iter);
934
935 nfsd_file_dispose_list(&dispose);
65294c1f
JL
936}
937
9542e6a6 938static struct nfsd_fcache_disposal *
1463b38e 939nfsd_alloc_fcache_disposal(void)
9542e6a6
TM
940{
941 struct nfsd_fcache_disposal *l;
942
943 l = kmalloc(sizeof(*l), GFP_KERNEL);
944 if (!l)
945 return NULL;
946 INIT_WORK(&l->work, nfsd_file_delayed_close);
9542e6a6
TM
947 spin_lock_init(&l->lock);
948 INIT_LIST_HEAD(&l->freeme);
949 return l;
950}
951
952static void
953nfsd_free_fcache_disposal(struct nfsd_fcache_disposal *l)
954{
9542e6a6
TM
955 cancel_work_sync(&l->work);
956 nfsd_file_dispose_list(&l->freeme);
1463b38e 957 kfree(l);
9542e6a6
TM
958}
959
960static void
961nfsd_free_fcache_disposal_net(struct net *net)
962{
1463b38e
N
963 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
964 struct nfsd_fcache_disposal *l = nn->fcache_disposal;
9542e6a6 965
1463b38e 966 nfsd_free_fcache_disposal(l);
9542e6a6
TM
967}
968
969int
970nfsd_file_cache_start_net(struct net *net)
971{
1463b38e
N
972 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
973
974 nn->fcache_disposal = nfsd_alloc_fcache_disposal();
975 return nn->fcache_disposal ? 0 : -ENOMEM;
9542e6a6
TM
976}
977
c7b824c3
CL
978/**
979 * nfsd_file_cache_purge - Remove all cache items associated with @net
980 * @net: target net namespace
981 *
982 */
983void
984nfsd_file_cache_purge(struct net *net)
985{
986 lockdep_assert_held(&nfsd_mutex);
987 if (test_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 1)
988 __nfsd_file_cache_purge(net);
989}
990
9542e6a6
TM
991void
992nfsd_file_cache_shutdown_net(struct net *net)
993{
994 nfsd_file_cache_purge(net);
995 nfsd_free_fcache_disposal_net(net);
996}
997
65294c1f
JL
998void
999nfsd_file_cache_shutdown(void)
1000{
8b330f78
CL
1001 int i;
1002
c7b824c3
CL
1003 lockdep_assert_held(&nfsd_mutex);
1004 if (test_and_clear_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 0)
1005 return;
65294c1f
JL
1006
1007 lease_unregister_notifier(&nfsd_file_lease_notifier);
1008 unregister_shrinker(&nfsd_file_shrinker);
1009 /*
1010 * make sure all callers of nfsd_file_lru_cb are done before
1011 * calling nfsd_file_cache_purge
1012 */
1013 cancel_delayed_work_sync(&nfsd_filecache_laundrette);
c7b824c3 1014 __nfsd_file_cache_purge(NULL);
65294c1f
JL
1015 list_lru_destroy(&nfsd_file_lru);
1016 rcu_barrier();
1017 fsnotify_put_group(nfsd_file_fsnotify_group);
1018 nfsd_file_fsnotify_group = NULL;
1019 kmem_cache_destroy(nfsd_file_slab);
1020 nfsd_file_slab = NULL;
1021 fsnotify_wait_marks_destroyed();
1022 kmem_cache_destroy(nfsd_file_mark_slab);
1023 nfsd_file_mark_slab = NULL;
9542e6a6
TM
1024 destroy_workqueue(nfsd_filecache_wq);
1025 nfsd_filecache_wq = NULL;
fc22945e 1026 rhashtable_destroy(&nfsd_file_rhash_tbl);
8b330f78
CL
1027
1028 for_each_possible_cpu(i) {
1029 per_cpu(nfsd_file_cache_hits, i) = 0;
1030 per_cpu(nfsd_file_acquisitions, i) = 0;
1031 per_cpu(nfsd_file_releases, i) = 0;
1032 per_cpu(nfsd_file_total_age, i) = 0;
8b330f78 1033 per_cpu(nfsd_file_evictions, i) = 0;
65294c1f 1034 }
65294c1f
JL
1035}
1036
1037/**
ce502f81
CL
1038 * nfsd_file_is_cached - are there any cached open files for this inode?
1039 * @inode: inode to check
1040 *
1041 * The lookup matches inodes in all net namespaces and is atomic wrt
1042 * nfsd_file_acquire().
65294c1f 1043 *
ce502f81
CL
1044 * Return values:
1045 * %true: filecache contains at least one file matching this inode
1046 * %false: filecache contains no files matching this inode
65294c1f
JL
1047 */
1048bool
1049nfsd_file_is_cached(struct inode *inode)
1050{
ce502f81
CL
1051 struct nfsd_file_lookup_key key = {
1052 .type = NFSD_FILE_KEY_INODE,
1053 .inode = inode,
6c31e4c9 1054 .gc = true,
ce502f81
CL
1055 };
1056 bool ret = false;
1057
1058 if (rhashtable_lookup_fast(&nfsd_file_rhash_tbl, &key,
1059 nfsd_file_rhash_params) != NULL)
1060 ret = true;
54f7df70 1061 trace_nfsd_file_is_cached(inode, (int)ret);
65294c1f
JL
1062 return ret;
1063}
1064
fb70bf12 1065static __be32
be023006 1066nfsd_file_do_acquire(struct svc_rqst *rqstp, struct svc_fh *fhp,
0b3a551f
JL
1067 unsigned int may_flags, struct file *file,
1068 struct nfsd_file **pnf, bool want_gc)
65294c1f 1069{
ce502f81
CL
1070 struct nfsd_file_lookup_key key = {
1071 .type = NFSD_FILE_KEY_FULL,
1072 .need = may_flags & NFSD_FILE_MAY_MASK,
1073 .net = SVC_NET(rqstp),
4d1ea845 1074 .gc = want_gc,
ce502f81 1075 };
243a5263
JL
1076 bool open_retry = true;
1077 struct nfsd_file *nf;
ce502f81 1078 __be32 status;
243a5263 1079 int ret;
65294c1f 1080
65294c1f
JL
1081 status = fh_verify(rqstp, fhp, S_IFREG,
1082 may_flags|NFSD_MAY_OWNER_OVERRIDE);
1083 if (status != nfs_ok)
1084 return status;
ce502f81
CL
1085 key.inode = d_inode(fhp->fh_dentry);
1086 key.cred = get_current_cred();
65294c1f 1087
65294c1f 1088retry:
243a5263
JL
1089 rcu_read_lock();
1090 nf = rhashtable_lookup(&nfsd_file_rhash_tbl, &key,
1091 nfsd_file_rhash_params);
70f62231 1092 nf = nfsd_file_get(nf);
243a5263 1093 rcu_read_unlock();
ac3a2585
JL
1094
1095 if (nf) {
b680cb9b
JL
1096 /*
1097 * If the nf is on the LRU then it holds an extra reference
1098 * that must be put if it's removed. It had better not be
1099 * the last one however, since we should hold another.
1100 */
ac3a2585
JL
1101 if (nfsd_file_lru_remove(nf))
1102 WARN_ON_ONCE(refcount_dec_and_test(&nf->nf_ref));
65294c1f 1103 goto wait_for_construction;
ac3a2585 1104 }
65294c1f 1105
243a5263
JL
1106 nf = nfsd_file_alloc(&key, may_flags);
1107 if (!nf) {
54f7df70 1108 status = nfserr_jukebox;
c6593366 1109 goto out;
65294c1f
JL
1110 }
1111
243a5263
JL
1112 ret = rhashtable_lookup_insert_key(&nfsd_file_rhash_tbl,
1113 &key, &nf->nf_rhash,
1114 nfsd_file_rhash_params);
1115 if (likely(ret == 0))
65294c1f 1116 goto open_file;
243a5263 1117
243a5263
JL
1118 if (ret == -EEXIST)
1119 goto retry;
1120 trace_nfsd_file_insert_err(rqstp, key.inode, may_flags, ret);
1121 status = nfserr_jukebox;
c6593366 1122 goto construction_err;
65294c1f
JL
1123
1124wait_for_construction:
1125 wait_on_bit(&nf->nf_flags, NFSD_FILE_PENDING, TASK_UNINTERRUPTIBLE);
1126
1127 /* Did construction of this file fail? */
1128 if (!test_bit(NFSD_FILE_HASHED, &nf->nf_flags)) {
ce502f81 1129 trace_nfsd_file_cons_err(rqstp, key.inode, may_flags, nf);
243a5263 1130 if (!open_retry) {
28c7d86b 1131 status = nfserr_jukebox;
c6593366 1132 goto construction_err;
28c7d86b 1133 }
243a5263 1134 open_retry = false;
65294c1f
JL
1135 goto retry;
1136 }
65294c1f
JL
1137 this_cpu_inc(nfsd_file_cache_hits);
1138
23ba98de 1139 status = nfserrno(nfsd_open_break_lease(file_inode(nf->nf_file), may_flags));
c6593366
JL
1140 if (status != nfs_ok) {
1141 nfsd_file_put(nf);
1142 nf = NULL;
1143 }
1144
65294c1f
JL
1145out:
1146 if (status == nfs_ok) {
0b3a551f 1147 this_cpu_inc(nfsd_file_acquisitions);
4c475eee 1148 nfsd_file_check_write_error(nf);
65294c1f 1149 *pnf = nf;
65294c1f 1150 }
ce502f81 1151 put_cred(key.cred);
0b3a551f 1152 trace_nfsd_file_acquire(rqstp, key.inode, may_flags, nf, status);
65294c1f 1153 return status;
54f7df70 1154
65294c1f 1155open_file:
b40a2839 1156 trace_nfsd_file_alloc(nf);
427f5f83 1157 nf->nf_mark = nfsd_file_mark_find_or_create(nf, key.inode);
fb70bf12 1158 if (nf->nf_mark) {
0b3a551f
JL
1159 if (file) {
1160 get_file(file);
1161 nf->nf_file = file;
1162 status = nfs_ok;
1163 trace_nfsd_file_opened(nf, status);
1164 } else {
fb70bf12
CL
1165 status = nfsd_open_verified(rqstp, fhp, may_flags,
1166 &nf->nf_file);
0122e882 1167 trace_nfsd_file_open(nf, status);
0b3a551f 1168 }
fb70bf12 1169 } else
65294c1f
JL
1170 status = nfserr_jukebox;
1171 /*
1172 * If construction failed, or we raced with a call to unlink()
1173 * then unhash.
1174 */
ac3a2585
JL
1175 if (status == nfs_ok && key.inode->i_nlink == 0)
1176 status = nfserr_jukebox;
1177 if (status != nfs_ok)
1178 nfsd_file_unhash(nf);
b8bea9f6 1179 clear_and_wake_up_bit(NFSD_FILE_PENDING, &nf->nf_flags);
c6593366
JL
1180 if (status == nfs_ok)
1181 goto out;
1182
1183construction_err:
1184 if (refcount_dec_and_test(&nf->nf_ref))
1185 nfsd_file_free(nf);
1186 nf = NULL;
65294c1f
JL
1187 goto out;
1188}
1189
4d1ea845
CL
1190/**
1191 * nfsd_file_acquire_gc - Get a struct nfsd_file with an open file
1192 * @rqstp: the RPC transaction being executed
1193 * @fhp: the NFS filehandle of the file to be opened
1194 * @may_flags: NFSD_MAY_ settings for the file
1195 * @pnf: OUT: new or found "struct nfsd_file" object
1196 *
1197 * The nfsd_file object returned by this API is reference-counted
1198 * and garbage-collected. The object is retained for a few
1199 * seconds after the final nfsd_file_put() in case the caller
1200 * wants to re-use it.
1201 *
1202 * Returns nfs_ok and sets @pnf on success; otherwise an nfsstat in
1203 * network byte order is returned.
1204 */
1205__be32
1206nfsd_file_acquire_gc(struct svc_rqst *rqstp, struct svc_fh *fhp,
1207 unsigned int may_flags, struct nfsd_file **pnf)
1208{
0b3a551f 1209 return nfsd_file_do_acquire(rqstp, fhp, may_flags, NULL, pnf, true);
4d1ea845
CL
1210}
1211
fb70bf12
CL
1212/**
1213 * nfsd_file_acquire - Get a struct nfsd_file with an open file
1214 * @rqstp: the RPC transaction being executed
1215 * @fhp: the NFS filehandle of the file to be opened
1216 * @may_flags: NFSD_MAY_ settings for the file
1217 * @pnf: OUT: new or found "struct nfsd_file" object
1218 *
4d1ea845
CL
1219 * The nfsd_file_object returned by this API is reference-counted
1220 * but not garbage-collected. The object is unhashed after the
1221 * final nfsd_file_put().
1222 *
fb70bf12
CL
1223 * Returns nfs_ok and sets @pnf on success; otherwise an nfsstat in
1224 * network byte order is returned.
1225 */
1226__be32
1227nfsd_file_acquire(struct svc_rqst *rqstp, struct svc_fh *fhp,
1228 unsigned int may_flags, struct nfsd_file **pnf)
1229{
0b3a551f 1230 return nfsd_file_do_acquire(rqstp, fhp, may_flags, NULL, pnf, false);
fb70bf12
CL
1231}
1232
1233/**
0b3a551f 1234 * nfsd_file_acquire_opened - Get a struct nfsd_file using existing open file
fb70bf12
CL
1235 * @rqstp: the RPC transaction being executed
1236 * @fhp: the NFS filehandle of the file just created
1237 * @may_flags: NFSD_MAY_ settings for the file
0b3a551f 1238 * @file: cached, already-open file (may be NULL)
fb70bf12
CL
1239 * @pnf: OUT: new or found "struct nfsd_file" object
1240 *
0b3a551f
JL
1241 * Acquire a nfsd_file object that is not GC'ed. If one doesn't already exist,
1242 * and @file is non-NULL, use it to instantiate a new nfsd_file instead of
1243 * opening a new one.
4d1ea845 1244 *
fb70bf12
CL
1245 * Returns nfs_ok and sets @pnf on success; otherwise an nfsstat in
1246 * network byte order is returned.
1247 */
1248__be32
0b3a551f
JL
1249nfsd_file_acquire_opened(struct svc_rqst *rqstp, struct svc_fh *fhp,
1250 unsigned int may_flags, struct file *file,
1251 struct nfsd_file **pnf)
fb70bf12 1252{
0b3a551f 1253 return nfsd_file_do_acquire(rqstp, fhp, may_flags, file, pnf, false);
fb70bf12
CL
1254}
1255
65294c1f
JL
1256/*
1257 * Note that fields may be added, removed or reordered in the future. Programs
1258 * scraping this file for info should test the labels to ensure they're
1259 * getting the correct field.
1260 */
1342f9dd 1261int nfsd_file_cache_stats_show(struct seq_file *m, void *v)
65294c1f 1262{
1f696e23 1263 unsigned long releases = 0, evictions = 0;
df2aff52 1264 unsigned long hits = 0, acquisitions = 0;
ce502f81 1265 unsigned int i, count = 0, buckets = 0;
904940e9 1266 unsigned long lru = 0, total_age = 0;
65294c1f 1267
ce502f81 1268 /* Serialize with server shutdown */
65294c1f 1269 mutex_lock(&nfsd_mutex);
c7b824c3 1270 if (test_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 1) {
ce502f81
CL
1271 struct bucket_table *tbl;
1272 struct rhashtable *ht;
1273
0fd244c1 1274 lru = list_lru_count(&nfsd_file_lru);
ce502f81
CL
1275
1276 rcu_read_lock();
1277 ht = &nfsd_file_rhash_tbl;
1278 count = atomic_read(&ht->nelems);
1279 tbl = rht_dereference_rcu(ht->tbl, ht);
1280 buckets = tbl->size;
1281 rcu_read_unlock();
65294c1f
JL
1282 }
1283 mutex_unlock(&nfsd_mutex);
1284
29d4bdbb 1285 for_each_possible_cpu(i) {
65294c1f 1286 hits += per_cpu(nfsd_file_cache_hits, i);
29d4bdbb 1287 acquisitions += per_cpu(nfsd_file_acquisitions, i);
d6329327 1288 releases += per_cpu(nfsd_file_releases, i);
904940e9 1289 total_age += per_cpu(nfsd_file_total_age, i);
94660cc1 1290 evictions += per_cpu(nfsd_file_evictions, i);
29d4bdbb 1291 }
65294c1f
JL
1292
1293 seq_printf(m, "total entries: %u\n", count);
ce502f81 1294 seq_printf(m, "hash buckets: %u\n", buckets);
0fd244c1 1295 seq_printf(m, "lru entries: %lu\n", lru);
65294c1f 1296 seq_printf(m, "cache hits: %lu\n", hits);
29d4bdbb 1297 seq_printf(m, "acquisitions: %lu\n", acquisitions);
d6329327 1298 seq_printf(m, "releases: %lu\n", releases);
94660cc1 1299 seq_printf(m, "evictions: %lu\n", evictions);
904940e9
CL
1300 if (releases)
1301 seq_printf(m, "mean age (ms): %ld\n", total_age / releases);
1302 else
1303 seq_printf(m, "mean age (ms): -\n");
65294c1f
JL
1304 return 0;
1305}