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