1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * The NFSD open file cache.
4 *
5 * (c) 2015 - Jeff Layton <jeff.layton@primarydata.com>
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.
30 */
31
32 #include <linux/hash.h>
33 #include <linux/slab.h>
34 #include <linux/file.h>
35 #include <linux/pagemap.h>
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>
41 #include <linux/rhashtable.h>
42
43 #include "vfs.h"
44 #include "nfsd.h"
45 #include "nfsfh.h"
46 #include "netns.h"
47 #include "filecache.h"
48 #include "trace.h"
49
50 #define NFSD_LAUNDRETTE_DELAY (2 * HZ)
51
52 #define NFSD_FILE_CACHE_UP (0)
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|NFSD_MAY_LOCALIO)
56
57 static DEFINE_PER_CPU(unsigned long, nfsd_file_cache_hits);
58 static DEFINE_PER_CPU(unsigned long, nfsd_file_acquisitions);
59 static DEFINE_PER_CPU(unsigned long, nfsd_file_allocations);
60 static DEFINE_PER_CPU(unsigned long, nfsd_file_releases);
61 static DEFINE_PER_CPU(unsigned long, nfsd_file_total_age);
62 static DEFINE_PER_CPU(unsigned long, nfsd_file_evictions);
63
64 struct nfsd_fcache_disposal {
65 spinlock_t lock;
66 struct list_head freeme;
67 };
68
69 static struct kmem_cache *nfsd_file_slab;
70 static struct kmem_cache *nfsd_file_mark_slab;
71 static struct list_lru nfsd_file_lru;
72 static unsigned long nfsd_file_flags;
73 static struct fsnotify_group *nfsd_file_fsnotify_group;
74 static struct delayed_work nfsd_filecache_laundrette;
75 static struct rhltable nfsd_file_rhltable
76 ____cacheline_aligned_in_smp;
77
78 static bool
nfsd_match_cred(const struct cred * c1,const struct cred * c2)79 nfsd_match_cred(const struct cred *c1, const struct cred *c2)
80 {
81 int i;
82
83 if (!uid_eq(c1->fsuid, c2->fsuid))
84 return false;
85 if (!gid_eq(c1->fsgid, c2->fsgid))
86 return false;
87 if (c1->group_info == NULL || c2->group_info == NULL)
88 return c1->group_info == c2->group_info;
89 if (c1->group_info->ngroups != c2->group_info->ngroups)
90 return false;
91 for (i = 0; i < c1->group_info->ngroups; i++) {
92 if (!gid_eq(c1->group_info->gid[i], c2->group_info->gid[i]))
93 return false;
94 }
95 return true;
96 }
97
98 static const struct rhashtable_params nfsd_file_rhash_params = {
99 .key_len = sizeof_field(struct nfsd_file, nf_inode),
100 .key_offset = offsetof(struct nfsd_file, nf_inode),
101 .head_offset = offsetof(struct nfsd_file, nf_rlist),
102
103 /*
104 * Start with a single page hash table to reduce resizing churn
105 * on light workloads.
106 */
107 .min_size = 256,
108 .automatic_shrinking = true,
109 };
110
111 static void
nfsd_file_schedule_laundrette(void)112 nfsd_file_schedule_laundrette(void)
113 {
114 if (test_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags))
115 queue_delayed_work(system_unbound_wq, &nfsd_filecache_laundrette,
116 NFSD_LAUNDRETTE_DELAY);
117 }
118
119 static void
nfsd_file_slab_free(struct rcu_head * rcu)120 nfsd_file_slab_free(struct rcu_head *rcu)
121 {
122 struct nfsd_file *nf = container_of(rcu, struct nfsd_file, nf_rcu);
123
124 put_cred(nf->nf_cred);
125 kmem_cache_free(nfsd_file_slab, nf);
126 }
127
128 static void
nfsd_file_mark_free(struct fsnotify_mark * mark)129 nfsd_file_mark_free(struct fsnotify_mark *mark)
130 {
131 struct nfsd_file_mark *nfm = container_of(mark, struct nfsd_file_mark,
132 nfm_mark);
133
134 kmem_cache_free(nfsd_file_mark_slab, nfm);
135 }
136
137 static struct nfsd_file_mark *
nfsd_file_mark_get(struct nfsd_file_mark * nfm)138 nfsd_file_mark_get(struct nfsd_file_mark *nfm)
139 {
140 if (!refcount_inc_not_zero(&nfm->nfm_ref))
141 return NULL;
142 return nfm;
143 }
144
145 static void
nfsd_file_mark_put(struct nfsd_file_mark * nfm)146 nfsd_file_mark_put(struct nfsd_file_mark *nfm)
147 {
148 if (refcount_dec_and_test(&nfm->nfm_ref)) {
149 fsnotify_destroy_mark(&nfm->nfm_mark, nfsd_file_fsnotify_group);
150 fsnotify_put_mark(&nfm->nfm_mark);
151 }
152 }
153
154 static struct nfsd_file_mark *
nfsd_file_mark_find_or_create(struct inode * inode)155 nfsd_file_mark_find_or_create(struct inode *inode)
156 {
157 int err;
158 struct fsnotify_mark *mark;
159 struct nfsd_file_mark *nfm = NULL, *new;
160
161 do {
162 fsnotify_group_lock(nfsd_file_fsnotify_group);
163 mark = fsnotify_find_inode_mark(inode,
164 nfsd_file_fsnotify_group);
165 if (mark) {
166 nfm = nfsd_file_mark_get(container_of(mark,
167 struct nfsd_file_mark,
168 nfm_mark));
169 fsnotify_group_unlock(nfsd_file_fsnotify_group);
170 if (nfm) {
171 fsnotify_put_mark(mark);
172 break;
173 }
174 /* Avoid soft lockup race with nfsd_file_mark_put() */
175 fsnotify_destroy_mark(mark, nfsd_file_fsnotify_group);
176 fsnotify_put_mark(mark);
177 } else {
178 fsnotify_group_unlock(nfsd_file_fsnotify_group);
179 }
180
181 /* allocate a new nfm */
182 new = kmem_cache_alloc(nfsd_file_mark_slab, GFP_KERNEL);
183 if (!new)
184 return NULL;
185 fsnotify_init_mark(&new->nfm_mark, nfsd_file_fsnotify_group);
186 new->nfm_mark.mask = FS_ATTRIB|FS_DELETE_SELF;
187 refcount_set(&new->nfm_ref, 1);
188
189 err = fsnotify_add_inode_mark(&new->nfm_mark, inode, 0);
190
191 /*
192 * If the add was successful, then return the object.
193 * Otherwise, we need to put the reference we hold on the
194 * nfm_mark. The fsnotify code will take a reference and put
195 * it on failure, so we can't just free it directly. It's also
196 * not safe to call fsnotify_destroy_mark on it as the
197 * mark->group will be NULL. Thus, we can't let the nfm_ref
198 * counter drive the destruction at this point.
199 */
200 if (likely(!err))
201 nfm = new;
202 else
203 fsnotify_put_mark(&new->nfm_mark);
204 } while (unlikely(err == -EEXIST));
205
206 return nfm;
207 }
208
209 static struct nfsd_file *
nfsd_file_alloc(struct net * net,struct inode * inode,unsigned char need,bool want_gc)210 nfsd_file_alloc(struct net *net, struct inode *inode, unsigned char need,
211 bool want_gc)
212 {
213 struct nfsd_file *nf;
214
215 nf = kmem_cache_alloc(nfsd_file_slab, GFP_KERNEL);
216 if (unlikely(!nf))
217 return NULL;
218
219 this_cpu_inc(nfsd_file_allocations);
220 INIT_LIST_HEAD(&nf->nf_lru);
221 INIT_LIST_HEAD(&nf->nf_gc);
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;
233 return nf;
234 }
235
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 */
243 static void
nfsd_file_check_write_error(struct nfsd_file * nf)244 nfsd_file_check_write_error(struct nfsd_file *nf)
245 {
246 struct file *file = nf->nf_file;
247
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));
251 }
252
253 static void
nfsd_file_hash_remove(struct nfsd_file * nf)254 nfsd_file_hash_remove(struct nfsd_file *nf)
255 {
256 trace_nfsd_file_unhash(nf);
257 rhltable_remove(&nfsd_file_rhltable, &nf->nf_rlist,
258 nfsd_file_rhash_params);
259 }
260
261 static bool
nfsd_file_unhash(struct nfsd_file * nf)262 nfsd_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
271 static void
nfsd_file_free(struct nfsd_file * nf)272 nfsd_file_free(struct nfsd_file *nf)
273 {
274 s64 age = ktime_to_ms(ktime_sub(ktime_get(), nf->nf_birthtime));
275
276 trace_nfsd_file_free(nf);
277
278 this_cpu_inc(nfsd_file_releases);
279 this_cpu_add(nfsd_file_total_age, age);
280
281 nfsd_file_unhash(nf);
282 if (nf->nf_mark)
283 nfsd_file_mark_put(nf->nf_mark);
284 if (nf->nf_file) {
285 nfsd_file_check_write_error(nf);
286 nfsd_filp_close(nf->nf_file);
287 }
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)))
294 return;
295
296 call_rcu(&nf->nf_rcu, nfsd_file_slab_free);
297 }
298
299 static bool
nfsd_file_check_writeback(struct nfsd_file * nf)300 nfsd_file_check_writeback(struct nfsd_file *nf)
301 {
302 struct file *file = nf->nf_file;
303 struct address_space *mapping;
304
305 /* File not open for write? */
306 if (!(file->f_mode & FMODE_WRITE))
307 return false;
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
316 mapping = file->f_mapping;
317 return mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) ||
318 mapping_tagged(mapping, PAGECACHE_TAG_WRITEBACK);
319 }
320
321
nfsd_file_lru_add(struct nfsd_file * nf)322 static bool nfsd_file_lru_add(struct nfsd_file *nf)
323 {
324 set_bit(NFSD_FILE_REFERENCED, &nf->nf_flags);
325 if (list_lru_add_obj(&nfsd_file_lru, &nf->nf_lru)) {
326 trace_nfsd_file_lru_add(nf);
327 return true;
328 }
329 return false;
330 }
331
nfsd_file_lru_remove(struct nfsd_file * nf)332 static bool nfsd_file_lru_remove(struct nfsd_file *nf)
333 {
334 if (list_lru_del_obj(&nfsd_file_lru, &nf->nf_lru)) {
335 trace_nfsd_file_lru_del(nf);
336 return true;
337 }
338 return false;
339 }
340
341 struct nfsd_file *
nfsd_file_get(struct nfsd_file * nf)342 nfsd_file_get(struct nfsd_file *nf)
343 {
344 if (nf && refcount_inc_not_zero(&nf->nf_ref))
345 return nf;
346 return NULL;
347 }
348
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 */
357 void
nfsd_file_put(struct nfsd_file * nf)358 nfsd_file_put(struct nfsd_file *nf)
359 {
360 might_sleep();
361 trace_nfsd_file_put(nf);
362
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 }
379
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 }
388 }
389 if (refcount_dec_and_test(&nf->nf_ref))
390 nfsd_file_free(nf);
391 }
392
393 /**
394 * nfsd_file_put_local - put nfsd_file reference and arm nfsd_serv_put in caller
395 * @nf: nfsd_file of which to put the reference
396 *
397 * First save the associated net to return to caller, then put
398 * the reference of the nfsd_file.
399 */
400 struct net *
nfsd_file_put_local(struct nfsd_file * nf)401 nfsd_file_put_local(struct nfsd_file *nf)
402 {
403 struct net *net = nf->nf_net;
404
405 nfsd_file_put(nf);
406 return net;
407 }
408
409 /**
410 * nfsd_file_file - get the backing file of an nfsd_file
411 * @nf: nfsd_file of which to access the backing file.
412 *
413 * Return backing file for @nf.
414 */
415 struct file *
nfsd_file_file(struct nfsd_file * nf)416 nfsd_file_file(struct nfsd_file *nf)
417 {
418 return nf->nf_file;
419 }
420
421 static void
nfsd_file_dispose_list(struct list_head * dispose)422 nfsd_file_dispose_list(struct list_head *dispose)
423 {
424 struct nfsd_file *nf;
425
426 while (!list_empty(dispose)) {
427 nf = list_first_entry(dispose, struct nfsd_file, nf_gc);
428 list_del_init(&nf->nf_gc);
429 nfsd_file_free(nf);
430 }
431 }
432
433 /**
434 * nfsd_file_dispose_list_delayed - move list of dead files to net's freeme list
435 * @dispose: list of nfsd_files to be disposed
436 *
437 * Transfers each file to the "freeme" list for its nfsd_net, to eventually
438 * be disposed of by the per-net garbage collector.
439 */
440 static void
nfsd_file_dispose_list_delayed(struct list_head * dispose)441 nfsd_file_dispose_list_delayed(struct list_head *dispose)
442 {
443 while(!list_empty(dispose)) {
444 struct nfsd_file *nf = list_first_entry(dispose,
445 struct nfsd_file, nf_gc);
446 struct nfsd_net *nn = net_generic(nf->nf_net, nfsd_net_id);
447 struct nfsd_fcache_disposal *l = nn->fcache_disposal;
448 struct svc_serv *serv;
449
450 spin_lock(&l->lock);
451 list_move_tail(&nf->nf_gc, &l->freeme);
452 spin_unlock(&l->lock);
453
454 /*
455 * The filecache laundrette is shut down after the
456 * nn->nfsd_serv pointer is cleared, but before the
457 * svc_serv is freed.
458 */
459 serv = nn->nfsd_serv;
460 if (serv)
461 svc_wake_up(serv);
462 }
463 }
464
465 /**
466 * nfsd_file_net_dispose - deal with nfsd_files waiting to be disposed.
467 * @nn: nfsd_net in which to find files to be disposed.
468 *
469 * When files held open for nfsv3 are removed from the filecache, whether
470 * due to memory pressure or garbage collection, they are queued to
471 * a per-net-ns queue. This function completes the disposal, either
472 * directly or by waking another nfsd thread to help with the work.
473 */
nfsd_file_net_dispose(struct nfsd_net * nn)474 void nfsd_file_net_dispose(struct nfsd_net *nn)
475 {
476 struct nfsd_fcache_disposal *l = nn->fcache_disposal;
477
478 if (!list_empty(&l->freeme)) {
479 LIST_HEAD(dispose);
480 int i;
481
482 spin_lock(&l->lock);
483 for (i = 0; i < 8 && !list_empty(&l->freeme); i++)
484 list_move(l->freeme.next, &dispose);
485 spin_unlock(&l->lock);
486 if (!list_empty(&l->freeme))
487 /* Wake up another thread to share the work
488 * *before* doing any actual disposing.
489 */
490 svc_wake_up(nn->nfsd_serv);
491 nfsd_file_dispose_list(&dispose);
492 }
493 }
494
495 /**
496 * nfsd_file_lru_cb - Examine an entry on the LRU list
497 * @item: LRU entry to examine
498 * @lru: controlling LRU
499 * @lock: LRU list lock (unused)
500 * @arg: dispose list
501 *
502 * Return values:
503 * %LRU_REMOVED: @item was removed from the LRU
504 * %LRU_ROTATE: @item is to be moved to the LRU tail
505 * %LRU_SKIP: @item cannot be evicted
506 */
507 static enum lru_status
nfsd_file_lru_cb(struct list_head * item,struct list_lru_one * lru,spinlock_t * lock,void * arg)508 nfsd_file_lru_cb(struct list_head *item, struct list_lru_one *lru,
509 spinlock_t *lock, void *arg)
510 __releases(lock)
511 __acquires(lock)
512 {
513 struct list_head *head = arg;
514 struct nfsd_file *nf = list_entry(item, struct nfsd_file, nf_lru);
515
516 /* We should only be dealing with GC entries here */
517 WARN_ON_ONCE(!test_bit(NFSD_FILE_GC, &nf->nf_flags));
518
519 /*
520 * Don't throw out files that are still undergoing I/O or
521 * that have uncleared errors pending.
522 */
523 if (nfsd_file_check_writeback(nf)) {
524 trace_nfsd_file_gc_writeback(nf);
525 return LRU_SKIP;
526 }
527
528 /* If it was recently added to the list, skip it */
529 if (test_and_clear_bit(NFSD_FILE_REFERENCED, &nf->nf_flags)) {
530 trace_nfsd_file_gc_referenced(nf);
531 return LRU_ROTATE;
532 }
533
534 /*
535 * Put the reference held on behalf of the LRU. If it wasn't the last
536 * one, then just remove it from the LRU and ignore it.
537 */
538 if (!refcount_dec_and_test(&nf->nf_ref)) {
539 trace_nfsd_file_gc_in_use(nf);
540 list_lru_isolate(lru, &nf->nf_lru);
541 return LRU_REMOVED;
542 }
543
544 /* Refcount went to zero. Unhash it and queue it to the dispose list */
545 nfsd_file_unhash(nf);
546 list_lru_isolate(lru, &nf->nf_lru);
547 list_add(&nf->nf_gc, head);
548 this_cpu_inc(nfsd_file_evictions);
549 trace_nfsd_file_gc_disposed(nf);
550 return LRU_REMOVED;
551 }
552
553 static void
nfsd_file_gc(void)554 nfsd_file_gc(void)
555 {
556 LIST_HEAD(dispose);
557 unsigned long ret;
558
559 ret = list_lru_walk(&nfsd_file_lru, nfsd_file_lru_cb,
560 &dispose, list_lru_count(&nfsd_file_lru));
561 trace_nfsd_file_gc_removed(ret, list_lru_count(&nfsd_file_lru));
562 nfsd_file_dispose_list_delayed(&dispose);
563 }
564
565 static void
nfsd_file_gc_worker(struct work_struct * work)566 nfsd_file_gc_worker(struct work_struct *work)
567 {
568 nfsd_file_gc();
569 if (list_lru_count(&nfsd_file_lru))
570 nfsd_file_schedule_laundrette();
571 }
572
573 static unsigned long
nfsd_file_lru_count(struct shrinker * s,struct shrink_control * sc)574 nfsd_file_lru_count(struct shrinker *s, struct shrink_control *sc)
575 {
576 return list_lru_count(&nfsd_file_lru);
577 }
578
579 static unsigned long
nfsd_file_lru_scan(struct shrinker * s,struct shrink_control * sc)580 nfsd_file_lru_scan(struct shrinker *s, struct shrink_control *sc)
581 {
582 LIST_HEAD(dispose);
583 unsigned long ret;
584
585 ret = list_lru_shrink_walk(&nfsd_file_lru, sc,
586 nfsd_file_lru_cb, &dispose);
587 trace_nfsd_file_shrinker_removed(ret, list_lru_count(&nfsd_file_lru));
588 nfsd_file_dispose_list_delayed(&dispose);
589 return ret;
590 }
591
592 static struct shrinker *nfsd_file_shrinker;
593
594 /**
595 * nfsd_file_cond_queue - conditionally unhash and queue a nfsd_file
596 * @nf: nfsd_file to attempt to queue
597 * @dispose: private list to queue successfully-put objects
598 *
599 * Unhash an nfsd_file, try to get a reference to it, and then put that
600 * reference. If it's the last reference, queue it to the dispose list.
601 */
602 static void
nfsd_file_cond_queue(struct nfsd_file * nf,struct list_head * dispose)603 nfsd_file_cond_queue(struct nfsd_file *nf, struct list_head *dispose)
604 __must_hold(RCU)
605 {
606 int decrement = 1;
607
608 /* If we raced with someone else unhashing, ignore it */
609 if (!nfsd_file_unhash(nf))
610 return;
611
612 /* If we can't get a reference, ignore it */
613 if (!nfsd_file_get(nf))
614 return;
615
616 /* Extra decrement if we remove from the LRU */
617 if (nfsd_file_lru_remove(nf))
618 ++decrement;
619
620 /* If refcount goes to 0, then put on the dispose list */
621 if (refcount_sub_and_test(decrement, &nf->nf_ref)) {
622 list_add(&nf->nf_gc, dispose);
623 trace_nfsd_file_closing(nf);
624 }
625 }
626
627 /**
628 * nfsd_file_queue_for_close: try to close out any open nfsd_files for an inode
629 * @inode: inode on which to close out nfsd_files
630 * @dispose: list on which to gather nfsd_files to close out
631 *
632 * An nfsd_file represents a struct file being held open on behalf of nfsd.
633 * An open file however can block other activity (such as leases), or cause
634 * undesirable behavior (e.g. spurious silly-renames when reexporting NFS).
635 *
636 * This function is intended to find open nfsd_files when this sort of
637 * conflicting access occurs and then attempt to close those files out.
638 *
639 * Populates the dispose list with entries that have already had their
640 * refcounts go to zero. The actual free of an nfsd_file can be expensive,
641 * so we leave it up to the caller whether it wants to wait or not.
642 */
643 static void
nfsd_file_queue_for_close(struct inode * inode,struct list_head * dispose)644 nfsd_file_queue_for_close(struct inode *inode, struct list_head *dispose)
645 {
646 struct rhlist_head *tmp, *list;
647 struct nfsd_file *nf;
648
649 rcu_read_lock();
650 list = rhltable_lookup(&nfsd_file_rhltable, &inode,
651 nfsd_file_rhash_params);
652 rhl_for_each_entry_rcu(nf, tmp, list, nf_rlist) {
653 if (!test_bit(NFSD_FILE_GC, &nf->nf_flags))
654 continue;
655 nfsd_file_cond_queue(nf, dispose);
656 }
657 rcu_read_unlock();
658 }
659
660 /**
661 * nfsd_file_close_inode - attempt a delayed close of a nfsd_file
662 * @inode: inode of the file to attempt to remove
663 *
664 * Close out any open nfsd_files that can be reaped for @inode. The
665 * actual freeing is deferred to the dispose_list_delayed infrastructure.
666 *
667 * This is used by the fsnotify callbacks and setlease notifier.
668 */
669 static void
nfsd_file_close_inode(struct inode * inode)670 nfsd_file_close_inode(struct inode *inode)
671 {
672 LIST_HEAD(dispose);
673
674 nfsd_file_queue_for_close(inode, &dispose);
675 nfsd_file_dispose_list_delayed(&dispose);
676 }
677
678 /**
679 * nfsd_file_close_inode_sync - attempt to forcibly close a nfsd_file
680 * @inode: inode of the file to attempt to remove
681 *
682 * Close out any open nfsd_files that can be reaped for @inode. The
683 * nfsd_files are closed out synchronously.
684 *
685 * This is called from nfsd_rename and nfsd_unlink to avoid silly-renames
686 * when reexporting NFS.
687 */
688 void
nfsd_file_close_inode_sync(struct inode * inode)689 nfsd_file_close_inode_sync(struct inode *inode)
690 {
691 struct nfsd_file *nf;
692 LIST_HEAD(dispose);
693
694 trace_nfsd_file_close(inode);
695
696 nfsd_file_queue_for_close(inode, &dispose);
697 while (!list_empty(&dispose)) {
698 nf = list_first_entry(&dispose, struct nfsd_file, nf_gc);
699 list_del_init(&nf->nf_gc);
700 nfsd_file_free(nf);
701 }
702 }
703
704 static int
nfsd_file_lease_notifier_call(struct notifier_block * nb,unsigned long arg,void * data)705 nfsd_file_lease_notifier_call(struct notifier_block *nb, unsigned long arg,
706 void *data)
707 {
708 struct file_lease *fl = data;
709
710 /* Only close files for F_SETLEASE leases */
711 if (fl->c.flc_flags & FL_LEASE)
712 nfsd_file_close_inode(file_inode(fl->c.flc_file));
713 return 0;
714 }
715
716 static struct notifier_block nfsd_file_lease_notifier = {
717 .notifier_call = nfsd_file_lease_notifier_call,
718 };
719
720 static int
nfsd_file_fsnotify_handle_event(struct fsnotify_mark * mark,u32 mask,struct inode * inode,struct inode * dir,const struct qstr * name,u32 cookie)721 nfsd_file_fsnotify_handle_event(struct fsnotify_mark *mark, u32 mask,
722 struct inode *inode, struct inode *dir,
723 const struct qstr *name, u32 cookie)
724 {
725 if (WARN_ON_ONCE(!inode))
726 return 0;
727
728 trace_nfsd_file_fsnotify_handle_event(inode, mask);
729
730 /* Should be no marks on non-regular files */
731 if (!S_ISREG(inode->i_mode)) {
732 WARN_ON_ONCE(1);
733 return 0;
734 }
735
736 /* don't close files if this was not the last link */
737 if (mask & FS_ATTRIB) {
738 if (inode->i_nlink)
739 return 0;
740 }
741
742 nfsd_file_close_inode(inode);
743 return 0;
744 }
745
746
747 static const struct fsnotify_ops nfsd_file_fsnotify_ops = {
748 .handle_inode_event = nfsd_file_fsnotify_handle_event,
749 .free_mark = nfsd_file_mark_free,
750 };
751
752 int
nfsd_file_cache_init(void)753 nfsd_file_cache_init(void)
754 {
755 int ret;
756
757 lockdep_assert_held(&nfsd_mutex);
758 if (test_and_set_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 1)
759 return 0;
760
761 ret = rhltable_init(&nfsd_file_rhltable, &nfsd_file_rhash_params);
762 if (ret)
763 goto out;
764
765 ret = -ENOMEM;
766 nfsd_file_slab = KMEM_CACHE(nfsd_file, 0);
767 if (!nfsd_file_slab) {
768 pr_err("nfsd: unable to create nfsd_file_slab\n");
769 goto out_err;
770 }
771
772 nfsd_file_mark_slab = KMEM_CACHE(nfsd_file_mark, 0);
773 if (!nfsd_file_mark_slab) {
774 pr_err("nfsd: unable to create nfsd_file_mark_slab\n");
775 goto out_err;
776 }
777
778 ret = list_lru_init(&nfsd_file_lru);
779 if (ret) {
780 pr_err("nfsd: failed to init nfsd_file_lru: %d\n", ret);
781 goto out_err;
782 }
783
784 nfsd_file_shrinker = shrinker_alloc(0, "nfsd-filecache");
785 if (!nfsd_file_shrinker) {
786 ret = -ENOMEM;
787 pr_err("nfsd: failed to allocate nfsd_file_shrinker\n");
788 goto out_lru;
789 }
790
791 nfsd_file_shrinker->count_objects = nfsd_file_lru_count;
792 nfsd_file_shrinker->scan_objects = nfsd_file_lru_scan;
793 nfsd_file_shrinker->seeks = 1;
794
795 shrinker_register(nfsd_file_shrinker);
796
797 ret = lease_register_notifier(&nfsd_file_lease_notifier);
798 if (ret) {
799 pr_err("nfsd: unable to register lease notifier: %d\n", ret);
800 goto out_shrinker;
801 }
802
803 nfsd_file_fsnotify_group = fsnotify_alloc_group(&nfsd_file_fsnotify_ops,
804 0);
805 if (IS_ERR(nfsd_file_fsnotify_group)) {
806 pr_err("nfsd: unable to create fsnotify group: %ld\n",
807 PTR_ERR(nfsd_file_fsnotify_group));
808 ret = PTR_ERR(nfsd_file_fsnotify_group);
809 nfsd_file_fsnotify_group = NULL;
810 goto out_notifier;
811 }
812
813 INIT_DELAYED_WORK(&nfsd_filecache_laundrette, nfsd_file_gc_worker);
814 out:
815 if (ret)
816 clear_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags);
817 return ret;
818 out_notifier:
819 lease_unregister_notifier(&nfsd_file_lease_notifier);
820 out_shrinker:
821 shrinker_free(nfsd_file_shrinker);
822 out_lru:
823 list_lru_destroy(&nfsd_file_lru);
824 out_err:
825 kmem_cache_destroy(nfsd_file_slab);
826 nfsd_file_slab = NULL;
827 kmem_cache_destroy(nfsd_file_mark_slab);
828 nfsd_file_mark_slab = NULL;
829 rhltable_destroy(&nfsd_file_rhltable);
830 goto out;
831 }
832
833 /**
834 * __nfsd_file_cache_purge: clean out the cache for shutdown
835 * @net: net-namespace to shut down the cache (may be NULL)
836 *
837 * Walk the nfsd_file cache and close out any that match @net. If @net is NULL,
838 * then close out everything. Called when an nfsd instance is being shut down,
839 * and when the exports table is flushed.
840 */
841 static void
__nfsd_file_cache_purge(struct net * net)842 __nfsd_file_cache_purge(struct net *net)
843 {
844 struct rhashtable_iter iter;
845 struct nfsd_file *nf;
846 LIST_HEAD(dispose);
847
848 rhltable_walk_enter(&nfsd_file_rhltable, &iter);
849 do {
850 rhashtable_walk_start(&iter);
851
852 nf = rhashtable_walk_next(&iter);
853 while (!IS_ERR_OR_NULL(nf)) {
854 if (!net || nf->nf_net == net)
855 nfsd_file_cond_queue(nf, &dispose);
856 nf = rhashtable_walk_next(&iter);
857 }
858
859 rhashtable_walk_stop(&iter);
860 } while (nf == ERR_PTR(-EAGAIN));
861 rhashtable_walk_exit(&iter);
862
863 nfsd_file_dispose_list(&dispose);
864 }
865
866 static struct nfsd_fcache_disposal *
nfsd_alloc_fcache_disposal(void)867 nfsd_alloc_fcache_disposal(void)
868 {
869 struct nfsd_fcache_disposal *l;
870
871 l = kmalloc(sizeof(*l), GFP_KERNEL);
872 if (!l)
873 return NULL;
874 spin_lock_init(&l->lock);
875 INIT_LIST_HEAD(&l->freeme);
876 return l;
877 }
878
879 static void
nfsd_free_fcache_disposal(struct nfsd_fcache_disposal * l)880 nfsd_free_fcache_disposal(struct nfsd_fcache_disposal *l)
881 {
882 nfsd_file_dispose_list(&l->freeme);
883 kfree(l);
884 }
885
886 static void
nfsd_free_fcache_disposal_net(struct net * net)887 nfsd_free_fcache_disposal_net(struct net *net)
888 {
889 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
890 struct nfsd_fcache_disposal *l = nn->fcache_disposal;
891
892 nfsd_free_fcache_disposal(l);
893 }
894
895 int
nfsd_file_cache_start_net(struct net * net)896 nfsd_file_cache_start_net(struct net *net)
897 {
898 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
899
900 nn->fcache_disposal = nfsd_alloc_fcache_disposal();
901 return nn->fcache_disposal ? 0 : -ENOMEM;
902 }
903
904 /**
905 * nfsd_file_cache_purge - Remove all cache items associated with @net
906 * @net: target net namespace
907 *
908 */
909 void
nfsd_file_cache_purge(struct net * net)910 nfsd_file_cache_purge(struct net *net)
911 {
912 lockdep_assert_held(&nfsd_mutex);
913 if (test_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 1)
914 __nfsd_file_cache_purge(net);
915 }
916
917 void
nfsd_file_cache_shutdown_net(struct net * net)918 nfsd_file_cache_shutdown_net(struct net *net)
919 {
920 nfsd_file_cache_purge(net);
921 nfsd_free_fcache_disposal_net(net);
922 }
923
924 void
nfsd_file_cache_shutdown(void)925 nfsd_file_cache_shutdown(void)
926 {
927 int i;
928
929 lockdep_assert_held(&nfsd_mutex);
930 if (test_and_clear_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 0)
931 return;
932
933 lease_unregister_notifier(&nfsd_file_lease_notifier);
934 shrinker_free(nfsd_file_shrinker);
935 /*
936 * make sure all callers of nfsd_file_lru_cb are done before
937 * calling nfsd_file_cache_purge
938 */
939 cancel_delayed_work_sync(&nfsd_filecache_laundrette);
940 __nfsd_file_cache_purge(NULL);
941 list_lru_destroy(&nfsd_file_lru);
942 rcu_barrier();
943 fsnotify_put_group(nfsd_file_fsnotify_group);
944 nfsd_file_fsnotify_group = NULL;
945 kmem_cache_destroy(nfsd_file_slab);
946 nfsd_file_slab = NULL;
947 fsnotify_wait_marks_destroyed();
948 kmem_cache_destroy(nfsd_file_mark_slab);
949 nfsd_file_mark_slab = NULL;
950 rhltable_destroy(&nfsd_file_rhltable);
951
952 for_each_possible_cpu(i) {
953 per_cpu(nfsd_file_cache_hits, i) = 0;
954 per_cpu(nfsd_file_acquisitions, i) = 0;
955 per_cpu(nfsd_file_allocations, i) = 0;
956 per_cpu(nfsd_file_releases, i) = 0;
957 per_cpu(nfsd_file_total_age, i) = 0;
958 per_cpu(nfsd_file_evictions, i) = 0;
959 }
960 }
961
962 static struct nfsd_file *
nfsd_file_lookup_locked(const struct net * net,const struct cred * cred,struct inode * inode,unsigned char need,bool want_gc)963 nfsd_file_lookup_locked(const struct net *net, const struct cred *cred,
964 struct inode *inode, unsigned char need,
965 bool want_gc)
966 {
967 struct rhlist_head *tmp, *list;
968 struct nfsd_file *nf;
969
970 list = rhltable_lookup(&nfsd_file_rhltable, &inode,
971 nfsd_file_rhash_params);
972 rhl_for_each_entry_rcu(nf, tmp, list, nf_rlist) {
973 if (nf->nf_may != need)
974 continue;
975 if (nf->nf_net != net)
976 continue;
977 if (!nfsd_match_cred(nf->nf_cred, cred))
978 continue;
979 if (test_bit(NFSD_FILE_GC, &nf->nf_flags) != want_gc)
980 continue;
981 if (test_bit(NFSD_FILE_HASHED, &nf->nf_flags) == 0)
982 continue;
983
984 if (!nfsd_file_get(nf))
985 continue;
986 return nf;
987 }
988 return NULL;
989 }
990
991 /**
992 * nfsd_file_is_cached - are there any cached open files for this inode?
993 * @inode: inode to check
994 *
995 * The lookup matches inodes in all net namespaces and is atomic wrt
996 * nfsd_file_acquire().
997 *
998 * Return values:
999 * %true: filecache contains at least one file matching this inode
1000 * %false: filecache contains no files matching this inode
1001 */
1002 bool
nfsd_file_is_cached(struct inode * inode)1003 nfsd_file_is_cached(struct inode *inode)
1004 {
1005 struct rhlist_head *tmp, *list;
1006 struct nfsd_file *nf;
1007 bool ret = false;
1008
1009 rcu_read_lock();
1010 list = rhltable_lookup(&nfsd_file_rhltable, &inode,
1011 nfsd_file_rhash_params);
1012 rhl_for_each_entry_rcu(nf, tmp, list, nf_rlist)
1013 if (test_bit(NFSD_FILE_GC, &nf->nf_flags)) {
1014 ret = true;
1015 break;
1016 }
1017 rcu_read_unlock();
1018
1019 trace_nfsd_file_is_cached(inode, (int)ret);
1020 return ret;
1021 }
1022
1023 static __be32
nfsd_file_do_acquire(struct svc_rqst * rqstp,struct net * net,struct svc_cred * cred,struct auth_domain * client,struct svc_fh * fhp,unsigned int may_flags,struct file * file,struct nfsd_file ** pnf,bool want_gc)1024 nfsd_file_do_acquire(struct svc_rqst *rqstp, struct net *net,
1025 struct svc_cred *cred,
1026 struct auth_domain *client,
1027 struct svc_fh *fhp,
1028 unsigned int may_flags, struct file *file,
1029 struct nfsd_file **pnf, bool want_gc)
1030 {
1031 unsigned char need = may_flags & NFSD_FILE_MAY_MASK;
1032 struct nfsd_file *new, *nf;
1033 bool stale_retry = true;
1034 bool open_retry = true;
1035 struct inode *inode;
1036 __be32 status;
1037 int ret;
1038
1039 retry:
1040 if (rqstp) {
1041 status = fh_verify(rqstp, fhp, S_IFREG,
1042 may_flags|NFSD_MAY_OWNER_OVERRIDE);
1043 } else {
1044 status = fh_verify_local(net, cred, client, fhp, S_IFREG,
1045 may_flags|NFSD_MAY_OWNER_OVERRIDE);
1046 }
1047 if (status != nfs_ok)
1048 return status;
1049 inode = d_inode(fhp->fh_dentry);
1050
1051 rcu_read_lock();
1052 nf = nfsd_file_lookup_locked(net, current_cred(), inode, need, want_gc);
1053 rcu_read_unlock();
1054
1055 if (nf) {
1056 /*
1057 * If the nf is on the LRU then it holds an extra reference
1058 * that must be put if it's removed. It had better not be
1059 * the last one however, since we should hold another.
1060 */
1061 if (nfsd_file_lru_remove(nf))
1062 WARN_ON_ONCE(refcount_dec_and_test(&nf->nf_ref));
1063 goto wait_for_construction;
1064 }
1065
1066 new = nfsd_file_alloc(net, inode, need, want_gc);
1067 if (!new) {
1068 status = nfserr_jukebox;
1069 goto out;
1070 }
1071
1072 rcu_read_lock();
1073 spin_lock(&inode->i_lock);
1074 nf = nfsd_file_lookup_locked(net, current_cred(), inode, need, want_gc);
1075 if (unlikely(nf)) {
1076 spin_unlock(&inode->i_lock);
1077 rcu_read_unlock();
1078 nfsd_file_free(new);
1079 goto wait_for_construction;
1080 }
1081 nf = new;
1082 ret = rhltable_insert(&nfsd_file_rhltable, &nf->nf_rlist,
1083 nfsd_file_rhash_params);
1084 spin_unlock(&inode->i_lock);
1085 rcu_read_unlock();
1086 if (likely(ret == 0))
1087 goto open_file;
1088
1089 trace_nfsd_file_insert_err(rqstp, inode, may_flags, ret);
1090 status = nfserr_jukebox;
1091 goto construction_err;
1092
1093 wait_for_construction:
1094 wait_on_bit(&nf->nf_flags, NFSD_FILE_PENDING, TASK_UNINTERRUPTIBLE);
1095
1096 /* Did construction of this file fail? */
1097 if (!test_bit(NFSD_FILE_HASHED, &nf->nf_flags)) {
1098 trace_nfsd_file_cons_err(rqstp, inode, may_flags, nf);
1099 if (!open_retry) {
1100 status = nfserr_jukebox;
1101 goto construction_err;
1102 }
1103 nfsd_file_put(nf);
1104 open_retry = false;
1105 fh_put(fhp);
1106 goto retry;
1107 }
1108 this_cpu_inc(nfsd_file_cache_hits);
1109
1110 status = nfserrno(nfsd_open_break_lease(file_inode(nf->nf_file), may_flags));
1111 if (status != nfs_ok) {
1112 nfsd_file_put(nf);
1113 nf = NULL;
1114 }
1115
1116 out:
1117 if (status == nfs_ok) {
1118 this_cpu_inc(nfsd_file_acquisitions);
1119 nfsd_file_check_write_error(nf);
1120 *pnf = nf;
1121 }
1122 trace_nfsd_file_acquire(rqstp, inode, may_flags, nf, status);
1123 return status;
1124
1125 open_file:
1126 trace_nfsd_file_alloc(nf);
1127 nf->nf_mark = nfsd_file_mark_find_or_create(inode);
1128 if (nf->nf_mark) {
1129 if (file) {
1130 get_file(file);
1131 nf->nf_file = file;
1132 status = nfs_ok;
1133 trace_nfsd_file_opened(nf, status);
1134 } else {
1135 ret = nfsd_open_verified(rqstp, fhp, may_flags,
1136 &nf->nf_file);
1137 if (ret == -EOPENSTALE && stale_retry) {
1138 stale_retry = false;
1139 nfsd_file_unhash(nf);
1140 clear_and_wake_up_bit(NFSD_FILE_PENDING,
1141 &nf->nf_flags);
1142 if (refcount_dec_and_test(&nf->nf_ref))
1143 nfsd_file_free(nf);
1144 nf = NULL;
1145 fh_put(fhp);
1146 goto retry;
1147 }
1148 status = nfserrno(ret);
1149 trace_nfsd_file_open(nf, status);
1150 }
1151 } else
1152 status = nfserr_jukebox;
1153 /*
1154 * If construction failed, or we raced with a call to unlink()
1155 * then unhash.
1156 */
1157 if (status != nfs_ok || inode->i_nlink == 0)
1158 nfsd_file_unhash(nf);
1159 clear_and_wake_up_bit(NFSD_FILE_PENDING, &nf->nf_flags);
1160 if (status == nfs_ok)
1161 goto out;
1162
1163 construction_err:
1164 if (refcount_dec_and_test(&nf->nf_ref))
1165 nfsd_file_free(nf);
1166 nf = NULL;
1167 goto out;
1168 }
1169
1170 /**
1171 * nfsd_file_acquire_gc - Get a struct nfsd_file with an open file
1172 * @rqstp: the RPC transaction being executed
1173 * @fhp: the NFS filehandle of the file to be opened
1174 * @may_flags: NFSD_MAY_ settings for the file
1175 * @pnf: OUT: new or found "struct nfsd_file" object
1176 *
1177 * The nfsd_file object returned by this API is reference-counted
1178 * and garbage-collected. The object is retained for a few
1179 * seconds after the final nfsd_file_put() in case the caller
1180 * wants to re-use it.
1181 *
1182 * Return values:
1183 * %nfs_ok - @pnf points to an nfsd_file with its reference
1184 * count boosted.
1185 *
1186 * On error, an nfsstat value in network byte order is returned.
1187 */
1188 __be32
nfsd_file_acquire_gc(struct svc_rqst * rqstp,struct svc_fh * fhp,unsigned int may_flags,struct nfsd_file ** pnf)1189 nfsd_file_acquire_gc(struct svc_rqst *rqstp, struct svc_fh *fhp,
1190 unsigned int may_flags, struct nfsd_file **pnf)
1191 {
1192 return nfsd_file_do_acquire(rqstp, SVC_NET(rqstp), NULL, NULL,
1193 fhp, may_flags, NULL, pnf, true);
1194 }
1195
1196 /**
1197 * nfsd_file_acquire - Get a struct nfsd_file with an open file
1198 * @rqstp: the RPC transaction being executed
1199 * @fhp: the NFS filehandle of the file to be opened
1200 * @may_flags: NFSD_MAY_ settings for the file
1201 * @pnf: OUT: new or found "struct nfsd_file" object
1202 *
1203 * The nfsd_file_object returned by this API is reference-counted
1204 * but not garbage-collected. The object is unhashed after the
1205 * final nfsd_file_put().
1206 *
1207 * Return values:
1208 * %nfs_ok - @pnf points to an nfsd_file with its reference
1209 * count boosted.
1210 *
1211 * On error, an nfsstat value in network byte order is returned.
1212 */
1213 __be32
nfsd_file_acquire(struct svc_rqst * rqstp,struct svc_fh * fhp,unsigned int may_flags,struct nfsd_file ** pnf)1214 nfsd_file_acquire(struct svc_rqst *rqstp, struct svc_fh *fhp,
1215 unsigned int may_flags, struct nfsd_file **pnf)
1216 {
1217 return nfsd_file_do_acquire(rqstp, SVC_NET(rqstp), NULL, NULL,
1218 fhp, may_flags, NULL, pnf, false);
1219 }
1220
1221 /**
1222 * nfsd_file_acquire_local - Get a struct nfsd_file with an open file for localio
1223 * @net: The network namespace in which to perform a lookup
1224 * @cred: the user credential with which to validate access
1225 * @client: the auth_domain for LOCALIO lookup
1226 * @fhp: the NFS filehandle of the file to be opened
1227 * @may_flags: NFSD_MAY_ settings for the file
1228 * @pnf: OUT: new or found "struct nfsd_file" object
1229 *
1230 * This file lookup interface provide access to a file given the
1231 * filehandle and credential. No connection-based authorisation
1232 * is performed and in that way it is quite different to other
1233 * file access mediated by nfsd. It allows a kernel module such as the NFS
1234 * client to reach across network and filesystem namespaces to access
1235 * a file. The security implications of this should be carefully
1236 * considered before use.
1237 *
1238 * The nfsd_file object returned by this API is reference-counted
1239 * and garbage-collected. The object is retained for a few
1240 * seconds after the final nfsd_file_put() in case the caller
1241 * wants to re-use it.
1242 *
1243 * Return values:
1244 * %nfs_ok - @pnf points to an nfsd_file with its reference
1245 * count boosted.
1246 *
1247 * On error, an nfsstat value in network byte order is returned.
1248 */
1249 __be32
nfsd_file_acquire_local(struct net * net,struct svc_cred * cred,struct auth_domain * client,struct svc_fh * fhp,unsigned int may_flags,struct nfsd_file ** pnf)1250 nfsd_file_acquire_local(struct net *net, struct svc_cred *cred,
1251 struct auth_domain *client, struct svc_fh *fhp,
1252 unsigned int may_flags, struct nfsd_file **pnf)
1253 {
1254 /*
1255 * Save creds before calling nfsd_file_do_acquire() (which calls
1256 * nfsd_setuser). Important because caller (LOCALIO) is from
1257 * client context.
1258 */
1259 const struct cred *save_cred = get_current_cred();
1260 __be32 beres;
1261
1262 beres = nfsd_file_do_acquire(NULL, net, cred, client,
1263 fhp, may_flags, NULL, pnf, true);
1264 revert_creds(save_cred);
1265 return beres;
1266 }
1267
1268 /**
1269 * nfsd_file_acquire_opened - Get a struct nfsd_file using existing open file
1270 * @rqstp: the RPC transaction being executed
1271 * @fhp: the NFS filehandle of the file just created
1272 * @may_flags: NFSD_MAY_ settings for the file
1273 * @file: cached, already-open file (may be NULL)
1274 * @pnf: OUT: new or found "struct nfsd_file" object
1275 *
1276 * Acquire a nfsd_file object that is not GC'ed. If one doesn't already exist,
1277 * and @file is non-NULL, use it to instantiate a new nfsd_file instead of
1278 * opening a new one.
1279 *
1280 * Return values:
1281 * %nfs_ok - @pnf points to an nfsd_file with its reference
1282 * count boosted.
1283 *
1284 * On error, an nfsstat value in network byte order is returned.
1285 */
1286 __be32
nfsd_file_acquire_opened(struct svc_rqst * rqstp,struct svc_fh * fhp,unsigned int may_flags,struct file * file,struct nfsd_file ** pnf)1287 nfsd_file_acquire_opened(struct svc_rqst *rqstp, struct svc_fh *fhp,
1288 unsigned int may_flags, struct file *file,
1289 struct nfsd_file **pnf)
1290 {
1291 return nfsd_file_do_acquire(rqstp, SVC_NET(rqstp), NULL, NULL,
1292 fhp, may_flags, file, pnf, false);
1293 }
1294
1295 /*
1296 * Note that fields may be added, removed or reordered in the future. Programs
1297 * scraping this file for info should test the labels to ensure they're
1298 * getting the correct field.
1299 */
nfsd_file_cache_stats_show(struct seq_file * m,void * v)1300 int nfsd_file_cache_stats_show(struct seq_file *m, void *v)
1301 {
1302 unsigned long allocations = 0, releases = 0, evictions = 0;
1303 unsigned long hits = 0, acquisitions = 0;
1304 unsigned int i, count = 0, buckets = 0;
1305 unsigned long lru = 0, total_age = 0;
1306
1307 /* Serialize with server shutdown */
1308 mutex_lock(&nfsd_mutex);
1309 if (test_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 1) {
1310 struct bucket_table *tbl;
1311 struct rhashtable *ht;
1312
1313 lru = list_lru_count(&nfsd_file_lru);
1314
1315 rcu_read_lock();
1316 ht = &nfsd_file_rhltable.ht;
1317 count = atomic_read(&ht->nelems);
1318 tbl = rht_dereference_rcu(ht->tbl, ht);
1319 buckets = tbl->size;
1320 rcu_read_unlock();
1321 }
1322 mutex_unlock(&nfsd_mutex);
1323
1324 for_each_possible_cpu(i) {
1325 hits += per_cpu(nfsd_file_cache_hits, i);
1326 acquisitions += per_cpu(nfsd_file_acquisitions, i);
1327 allocations += per_cpu(nfsd_file_allocations, i);
1328 releases += per_cpu(nfsd_file_releases, i);
1329 total_age += per_cpu(nfsd_file_total_age, i);
1330 evictions += per_cpu(nfsd_file_evictions, i);
1331 }
1332
1333 seq_printf(m, "total inodes: %u\n", count);
1334 seq_printf(m, "hash buckets: %u\n", buckets);
1335 seq_printf(m, "lru entries: %lu\n", lru);
1336 seq_printf(m, "cache hits: %lu\n", hits);
1337 seq_printf(m, "acquisitions: %lu\n", acquisitions);
1338 seq_printf(m, "allocations: %lu\n", allocations);
1339 seq_printf(m, "releases: %lu\n", releases);
1340 seq_printf(m, "evictions: %lu\n", evictions);
1341 if (releases)
1342 seq_printf(m, "mean age (ms): %ld\n", total_age / releases);
1343 else
1344 seq_printf(m, "mean age (ms): -\n");
1345 return 0;
1346 }
1347