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