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 * @nf: 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 * nf)381 nfsd_file_put_local(struct nfsd_file *nf)
382 {
383 struct net *net = nf->nf_net;
384
385 nfsd_file_put(nf);
386 return net;
387 }
388
389 /**
390 * nfsd_file_file - get the backing file of an nfsd_file
391 * @nf: nfsd_file of which to access the backing file.
392 *
393 * Return backing file for @nf.
394 */
395 struct file *
nfsd_file_file(struct nfsd_file * nf)396 nfsd_file_file(struct nfsd_file *nf)
397 {
398 return nf->nf_file;
399 }
400
401 static void
nfsd_file_dispose_list(struct list_head * dispose)402 nfsd_file_dispose_list(struct list_head *dispose)
403 {
404 struct nfsd_file *nf;
405
406 while (!list_empty(dispose)) {
407 nf = list_first_entry(dispose, struct nfsd_file, nf_gc);
408 list_del_init(&nf->nf_gc);
409 nfsd_file_free(nf);
410 }
411 }
412
413 /**
414 * nfsd_file_dispose_list_delayed - move list of dead files to net's freeme list
415 * @dispose: list of nfsd_files to be disposed
416 *
417 * Transfers each file to the "freeme" list for its nfsd_net, to eventually
418 * be disposed of by the per-net garbage collector.
419 */
420 static void
nfsd_file_dispose_list_delayed(struct list_head * dispose)421 nfsd_file_dispose_list_delayed(struct list_head *dispose)
422 {
423 while(!list_empty(dispose)) {
424 struct nfsd_file *nf = list_first_entry(dispose,
425 struct nfsd_file, nf_gc);
426 struct nfsd_net *nn = net_generic(nf->nf_net, nfsd_net_id);
427 struct nfsd_fcache_disposal *l = nn->fcache_disposal;
428 struct svc_serv *serv;
429
430 spin_lock(&l->lock);
431 list_move_tail(&nf->nf_gc, &l->freeme);
432 spin_unlock(&l->lock);
433
434 /*
435 * The filecache laundrette is shut down after the
436 * nn->nfsd_serv pointer is cleared, but before the
437 * svc_serv is freed.
438 */
439 serv = nn->nfsd_serv;
440 if (serv)
441 svc_wake_up(serv);
442 }
443 }
444
445 /**
446 * nfsd_file_net_dispose - deal with nfsd_files waiting to be disposed.
447 * @nn: nfsd_net in which to find files to be disposed.
448 *
449 * When files held open for nfsv3 are removed from the filecache, whether
450 * due to memory pressure or garbage collection, they are queued to
451 * a per-net-ns queue. This function completes the disposal, either
452 * directly or by waking another nfsd thread to help with the work.
453 */
nfsd_file_net_dispose(struct nfsd_net * nn)454 void nfsd_file_net_dispose(struct nfsd_net *nn)
455 {
456 struct nfsd_fcache_disposal *l = nn->fcache_disposal;
457
458 if (!list_empty(&l->freeme)) {
459 LIST_HEAD(dispose);
460 int i;
461
462 spin_lock(&l->lock);
463 for (i = 0; i < 8 && !list_empty(&l->freeme); i++)
464 list_move(l->freeme.next, &dispose);
465 spin_unlock(&l->lock);
466 if (!list_empty(&l->freeme))
467 /* Wake up another thread to share the work
468 * *before* doing any actual disposing.
469 */
470 svc_wake_up(nn->nfsd_serv);
471 nfsd_file_dispose_list(&dispose);
472 }
473 }
474
475 /**
476 * nfsd_file_lru_cb - Examine an entry on the LRU list
477 * @item: LRU entry to examine
478 * @lru: controlling LRU
479 * @arg: dispose list
480 *
481 * Return values:
482 * %LRU_REMOVED: @item was removed from the LRU
483 * %LRU_ROTATE: @item is to be moved to the LRU tail
484 * %LRU_SKIP: @item cannot be evicted
485 */
486 static enum lru_status
nfsd_file_lru_cb(struct list_head * item,struct list_lru_one * lru,void * arg)487 nfsd_file_lru_cb(struct list_head *item, struct list_lru_one *lru,
488 void *arg)
489 {
490 struct list_head *head = arg;
491 struct nfsd_file *nf = list_entry(item, struct nfsd_file, nf_lru);
492
493 /* We should only be dealing with GC entries here */
494 WARN_ON_ONCE(!test_bit(NFSD_FILE_GC, &nf->nf_flags));
495
496 /*
497 * Don't throw out files that are still undergoing I/O or
498 * that have uncleared errors pending.
499 */
500 if (nfsd_file_check_writeback(nf)) {
501 trace_nfsd_file_gc_writeback(nf);
502 return LRU_SKIP;
503 }
504
505 /* If it was recently added to the list, skip it */
506 if (test_and_clear_bit(NFSD_FILE_REFERENCED, &nf->nf_flags)) {
507 trace_nfsd_file_gc_referenced(nf);
508 return LRU_ROTATE;
509 }
510
511 /*
512 * Put the reference held on behalf of the LRU if it is the last
513 * reference, else rotate.
514 */
515 if (!refcount_dec_if_one(&nf->nf_ref)) {
516 trace_nfsd_file_gc_in_use(nf);
517 return LRU_ROTATE;
518 }
519
520 /* Refcount went to zero. Unhash it and queue it to the dispose list */
521 nfsd_file_unhash(nf);
522 list_lru_isolate(lru, &nf->nf_lru);
523 list_add(&nf->nf_gc, head);
524 this_cpu_inc(nfsd_file_evictions);
525 trace_nfsd_file_gc_disposed(nf);
526 return LRU_REMOVED;
527 }
528
529 static enum lru_status
nfsd_file_gc_cb(struct list_head * item,struct list_lru_one * lru,void * arg)530 nfsd_file_gc_cb(struct list_head *item, struct list_lru_one *lru,
531 void *arg)
532 {
533 struct nfsd_file *nf = list_entry(item, struct nfsd_file, nf_lru);
534
535 if (test_and_clear_bit(NFSD_FILE_RECENT, &nf->nf_flags)) {
536 /*
537 * "REFERENCED" really means "should be at the end of the
538 * LRU. As we are putting it there we can clear the flag.
539 */
540 clear_bit(NFSD_FILE_REFERENCED, &nf->nf_flags);
541 trace_nfsd_file_gc_aged(nf);
542 return LRU_ROTATE;
543 }
544 return nfsd_file_lru_cb(item, lru, arg);
545 }
546
547 /* If the shrinker runs between calls to list_lru_walk_node() in
548 * nfsd_file_gc(), the "remaining" count will be wrong. This could
549 * result in premature freeing of some files. This may not matter much
550 * but is easy to fix with this spinlock which temporarily disables
551 * the shrinker.
552 */
553 static DEFINE_SPINLOCK(nfsd_gc_lock);
554 static void
nfsd_file_gc(void)555 nfsd_file_gc(void)
556 {
557 unsigned long ret = 0;
558 LIST_HEAD(dispose);
559 int nid;
560
561 spin_lock(&nfsd_gc_lock);
562 for_each_node_state(nid, N_NORMAL_MEMORY) {
563 unsigned long remaining = list_lru_count_node(&nfsd_file_lru, nid);
564
565 while (remaining > 0) {
566 unsigned long nr = min(remaining, NFSD_FILE_GC_BATCH);
567
568 remaining -= nr;
569 ret += list_lru_walk_node(&nfsd_file_lru, nid, nfsd_file_gc_cb,
570 &dispose, &nr);
571 if (nr)
572 /* walk aborted early */
573 remaining = 0;
574 }
575 }
576 spin_unlock(&nfsd_gc_lock);
577 trace_nfsd_file_gc_removed(ret, list_lru_count(&nfsd_file_lru));
578 nfsd_file_dispose_list_delayed(&dispose);
579 }
580
581 static void
nfsd_file_gc_worker(struct work_struct * work)582 nfsd_file_gc_worker(struct work_struct *work)
583 {
584 if (list_lru_count(&nfsd_file_lru))
585 nfsd_file_gc();
586 nfsd_file_schedule_laundrette();
587 }
588
589 static unsigned long
nfsd_file_lru_count(struct shrinker * s,struct shrink_control * sc)590 nfsd_file_lru_count(struct shrinker *s, struct shrink_control *sc)
591 {
592 return list_lru_count(&nfsd_file_lru);
593 }
594
595 static unsigned long
nfsd_file_lru_scan(struct shrinker * s,struct shrink_control * sc)596 nfsd_file_lru_scan(struct shrinker *s, struct shrink_control *sc)
597 {
598 LIST_HEAD(dispose);
599 unsigned long ret;
600
601 if (!spin_trylock(&nfsd_gc_lock))
602 return SHRINK_STOP;
603
604 ret = list_lru_shrink_walk(&nfsd_file_lru, sc,
605 nfsd_file_lru_cb, &dispose);
606 spin_unlock(&nfsd_gc_lock);
607 trace_nfsd_file_shrinker_removed(ret, list_lru_count(&nfsd_file_lru));
608 nfsd_file_dispose_list_delayed(&dispose);
609 return ret;
610 }
611
612 static struct shrinker *nfsd_file_shrinker;
613
614 /**
615 * nfsd_file_cond_queue - conditionally unhash and queue a nfsd_file
616 * @nf: nfsd_file to attempt to queue
617 * @dispose: private list to queue successfully-put objects
618 *
619 * Unhash an nfsd_file, try to get a reference to it, and then put that
620 * reference. If it's the last reference, queue it to the dispose list.
621 */
622 static void
nfsd_file_cond_queue(struct nfsd_file * nf,struct list_head * dispose)623 nfsd_file_cond_queue(struct nfsd_file *nf, struct list_head *dispose)
624 __must_hold(RCU)
625 {
626 int decrement = 1;
627
628 /* If we raced with someone else unhashing, ignore it */
629 if (!nfsd_file_unhash(nf))
630 return;
631
632 /* If we can't get a reference, ignore it */
633 if (!nfsd_file_get(nf))
634 return;
635
636 /* Extra decrement if we remove from the LRU */
637 if (nfsd_file_lru_remove(nf))
638 ++decrement;
639
640 /* If refcount goes to 0, then put on the dispose list */
641 if (refcount_sub_and_test(decrement, &nf->nf_ref)) {
642 list_add(&nf->nf_gc, dispose);
643 trace_nfsd_file_closing(nf);
644 }
645 }
646
647 /**
648 * nfsd_file_queue_for_close: try to close out any open nfsd_files for an inode
649 * @inode: inode on which to close out nfsd_files
650 * @dispose: list on which to gather nfsd_files to close out
651 *
652 * An nfsd_file represents a struct file being held open on behalf of nfsd.
653 * An open file however can block other activity (such as leases), or cause
654 * undesirable behavior (e.g. spurious silly-renames when reexporting NFS).
655 *
656 * This function is intended to find open nfsd_files when this sort of
657 * conflicting access occurs and then attempt to close those files out.
658 *
659 * Populates the dispose list with entries that have already had their
660 * refcounts go to zero. The actual free of an nfsd_file can be expensive,
661 * so we leave it up to the caller whether it wants to wait or not.
662 */
663 static void
nfsd_file_queue_for_close(struct inode * inode,struct list_head * dispose)664 nfsd_file_queue_for_close(struct inode *inode, struct list_head *dispose)
665 {
666 struct rhlist_head *tmp, *list;
667 struct nfsd_file *nf;
668
669 rcu_read_lock();
670 list = rhltable_lookup(&nfsd_file_rhltable, &inode,
671 nfsd_file_rhash_params);
672 rhl_for_each_entry_rcu(nf, tmp, list, nf_rlist) {
673 if (!test_bit(NFSD_FILE_GC, &nf->nf_flags))
674 continue;
675 nfsd_file_cond_queue(nf, dispose);
676 }
677 rcu_read_unlock();
678 }
679
680 /**
681 * nfsd_file_close_inode - attempt a delayed close of a nfsd_file
682 * @inode: inode of the file to attempt to remove
683 *
684 * Close out any open nfsd_files that can be reaped for @inode. The
685 * actual freeing is deferred to the dispose_list_delayed infrastructure.
686 *
687 * This is used by the fsnotify callbacks and setlease notifier.
688 */
689 static void
nfsd_file_close_inode(struct inode * inode)690 nfsd_file_close_inode(struct inode *inode)
691 {
692 LIST_HEAD(dispose);
693
694 nfsd_file_queue_for_close(inode, &dispose);
695 nfsd_file_dispose_list_delayed(&dispose);
696 }
697
698 /**
699 * nfsd_file_close_inode_sync - attempt to forcibly close a nfsd_file
700 * @inode: inode of the file to attempt to remove
701 *
702 * Close out any open nfsd_files that can be reaped for @inode. The
703 * nfsd_files are closed out synchronously.
704 *
705 * This is called from nfsd_rename and nfsd_unlink to avoid silly-renames
706 * when reexporting NFS.
707 */
708 void
nfsd_file_close_inode_sync(struct inode * inode)709 nfsd_file_close_inode_sync(struct inode *inode)
710 {
711 LIST_HEAD(dispose);
712
713 trace_nfsd_file_close(inode);
714
715 nfsd_file_queue_for_close(inode, &dispose);
716 nfsd_file_dispose_list(&dispose);
717 }
718
719 static int
nfsd_file_lease_notifier_call(struct notifier_block * nb,unsigned long arg,void * data)720 nfsd_file_lease_notifier_call(struct notifier_block *nb, unsigned long arg,
721 void *data)
722 {
723 struct file_lease *fl = data;
724
725 /* Only close files for F_SETLEASE leases */
726 if (fl->c.flc_flags & FL_LEASE)
727 nfsd_file_close_inode(file_inode(fl->c.flc_file));
728 return 0;
729 }
730
731 static struct notifier_block nfsd_file_lease_notifier = {
732 .notifier_call = nfsd_file_lease_notifier_call,
733 };
734
735 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)736 nfsd_file_fsnotify_handle_event(struct fsnotify_mark *mark, u32 mask,
737 struct inode *inode, struct inode *dir,
738 const struct qstr *name, u32 cookie)
739 {
740 if (WARN_ON_ONCE(!inode))
741 return 0;
742
743 trace_nfsd_file_fsnotify_handle_event(inode, mask);
744
745 /* Should be no marks on non-regular files */
746 if (!S_ISREG(inode->i_mode)) {
747 WARN_ON_ONCE(1);
748 return 0;
749 }
750
751 /* don't close files if this was not the last link */
752 if (mask & FS_ATTRIB) {
753 if (inode->i_nlink)
754 return 0;
755 }
756
757 nfsd_file_close_inode(inode);
758 return 0;
759 }
760
761
762 static const struct fsnotify_ops nfsd_file_fsnotify_ops = {
763 .handle_inode_event = nfsd_file_fsnotify_handle_event,
764 .free_mark = nfsd_file_mark_free,
765 };
766
767 int
nfsd_file_cache_init(void)768 nfsd_file_cache_init(void)
769 {
770 int ret;
771
772 lockdep_assert_held(&nfsd_mutex);
773 if (test_and_set_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 1)
774 return 0;
775
776 ret = rhltable_init(&nfsd_file_rhltable, &nfsd_file_rhash_params);
777 if (ret)
778 goto out;
779
780 ret = -ENOMEM;
781 nfsd_file_slab = KMEM_CACHE(nfsd_file, 0);
782 if (!nfsd_file_slab) {
783 pr_err("nfsd: unable to create nfsd_file_slab\n");
784 goto out_err;
785 }
786
787 nfsd_file_mark_slab = KMEM_CACHE(nfsd_file_mark, 0);
788 if (!nfsd_file_mark_slab) {
789 pr_err("nfsd: unable to create nfsd_file_mark_slab\n");
790 goto out_err;
791 }
792
793 ret = list_lru_init(&nfsd_file_lru);
794 if (ret) {
795 pr_err("nfsd: failed to init nfsd_file_lru: %d\n", ret);
796 goto out_err;
797 }
798
799 nfsd_file_shrinker = shrinker_alloc(0, "nfsd-filecache");
800 if (!nfsd_file_shrinker) {
801 ret = -ENOMEM;
802 pr_err("nfsd: failed to allocate nfsd_file_shrinker\n");
803 goto out_lru;
804 }
805
806 nfsd_file_shrinker->count_objects = nfsd_file_lru_count;
807 nfsd_file_shrinker->scan_objects = nfsd_file_lru_scan;
808 nfsd_file_shrinker->seeks = 1;
809
810 shrinker_register(nfsd_file_shrinker);
811
812 ret = lease_register_notifier(&nfsd_file_lease_notifier);
813 if (ret) {
814 pr_err("nfsd: unable to register lease notifier: %d\n", ret);
815 goto out_shrinker;
816 }
817
818 nfsd_file_fsnotify_group = fsnotify_alloc_group(&nfsd_file_fsnotify_ops,
819 0);
820 if (IS_ERR(nfsd_file_fsnotify_group)) {
821 pr_err("nfsd: unable to create fsnotify group: %ld\n",
822 PTR_ERR(nfsd_file_fsnotify_group));
823 ret = PTR_ERR(nfsd_file_fsnotify_group);
824 nfsd_file_fsnotify_group = NULL;
825 goto out_notifier;
826 }
827
828 INIT_DELAYED_WORK(&nfsd_filecache_laundrette, nfsd_file_gc_worker);
829 out:
830 if (ret)
831 clear_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags);
832 return ret;
833 out_notifier:
834 lease_unregister_notifier(&nfsd_file_lease_notifier);
835 out_shrinker:
836 shrinker_free(nfsd_file_shrinker);
837 out_lru:
838 list_lru_destroy(&nfsd_file_lru);
839 out_err:
840 kmem_cache_destroy(nfsd_file_slab);
841 nfsd_file_slab = NULL;
842 kmem_cache_destroy(nfsd_file_mark_slab);
843 nfsd_file_mark_slab = NULL;
844 rhltable_destroy(&nfsd_file_rhltable);
845 goto out;
846 }
847
848 /**
849 * __nfsd_file_cache_purge: clean out the cache for shutdown
850 * @net: net-namespace to shut down the cache (may be NULL)
851 *
852 * Walk the nfsd_file cache and close out any that match @net. If @net is NULL,
853 * then close out everything. Called when an nfsd instance is being shut down,
854 * and when the exports table is flushed.
855 */
856 static void
__nfsd_file_cache_purge(struct net * net)857 __nfsd_file_cache_purge(struct net *net)
858 {
859 struct rhashtable_iter iter;
860 struct nfsd_file *nf;
861 LIST_HEAD(dispose);
862
863 #if IS_ENABLED(CONFIG_NFS_LOCALIO)
864 if (net) {
865 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
866 nfs_localio_invalidate_clients(&nn->local_clients,
867 &nn->local_clients_lock);
868 }
869 #endif
870
871 rhltable_walk_enter(&nfsd_file_rhltable, &iter);
872 do {
873 rhashtable_walk_start(&iter);
874
875 nf = rhashtable_walk_next(&iter);
876 while (!IS_ERR_OR_NULL(nf)) {
877 if (!net || nf->nf_net == net)
878 nfsd_file_cond_queue(nf, &dispose);
879 nf = rhashtable_walk_next(&iter);
880 }
881
882 rhashtable_walk_stop(&iter);
883 } while (nf == ERR_PTR(-EAGAIN));
884 rhashtable_walk_exit(&iter);
885
886 nfsd_file_dispose_list(&dispose);
887 }
888
889 static struct nfsd_fcache_disposal *
nfsd_alloc_fcache_disposal(void)890 nfsd_alloc_fcache_disposal(void)
891 {
892 struct nfsd_fcache_disposal *l;
893
894 l = kmalloc(sizeof(*l), GFP_KERNEL);
895 if (!l)
896 return NULL;
897 spin_lock_init(&l->lock);
898 INIT_LIST_HEAD(&l->freeme);
899 return l;
900 }
901
902 static void
nfsd_free_fcache_disposal(struct nfsd_fcache_disposal * l)903 nfsd_free_fcache_disposal(struct nfsd_fcache_disposal *l)
904 {
905 nfsd_file_dispose_list(&l->freeme);
906 kfree(l);
907 }
908
909 static void
nfsd_free_fcache_disposal_net(struct net * net)910 nfsd_free_fcache_disposal_net(struct net *net)
911 {
912 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
913 struct nfsd_fcache_disposal *l = nn->fcache_disposal;
914
915 nfsd_free_fcache_disposal(l);
916 }
917
918 int
nfsd_file_cache_start_net(struct net * net)919 nfsd_file_cache_start_net(struct net *net)
920 {
921 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
922
923 nn->fcache_disposal = nfsd_alloc_fcache_disposal();
924 return nn->fcache_disposal ? 0 : -ENOMEM;
925 }
926
927 /**
928 * nfsd_file_cache_purge - Remove all cache items associated with @net
929 * @net: target net namespace
930 *
931 */
932 void
nfsd_file_cache_purge(struct net * net)933 nfsd_file_cache_purge(struct net *net)
934 {
935 lockdep_assert_held(&nfsd_mutex);
936 if (test_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 1)
937 __nfsd_file_cache_purge(net);
938 }
939
940 void
nfsd_file_cache_shutdown_net(struct net * net)941 nfsd_file_cache_shutdown_net(struct net *net)
942 {
943 nfsd_file_cache_purge(net);
944 nfsd_free_fcache_disposal_net(net);
945 }
946
947 void
nfsd_file_cache_shutdown(void)948 nfsd_file_cache_shutdown(void)
949 {
950 int i;
951
952 lockdep_assert_held(&nfsd_mutex);
953 if (test_and_clear_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 0)
954 return;
955
956 lease_unregister_notifier(&nfsd_file_lease_notifier);
957 shrinker_free(nfsd_file_shrinker);
958 /*
959 * make sure all callers of nfsd_file_lru_cb are done before
960 * calling nfsd_file_cache_purge
961 */
962 cancel_delayed_work_sync(&nfsd_filecache_laundrette);
963 __nfsd_file_cache_purge(NULL);
964 list_lru_destroy(&nfsd_file_lru);
965 rcu_barrier();
966 fsnotify_put_group(nfsd_file_fsnotify_group);
967 nfsd_file_fsnotify_group = NULL;
968 kmem_cache_destroy(nfsd_file_slab);
969 nfsd_file_slab = NULL;
970 fsnotify_wait_marks_destroyed();
971 kmem_cache_destroy(nfsd_file_mark_slab);
972 nfsd_file_mark_slab = NULL;
973 rhltable_destroy(&nfsd_file_rhltable);
974
975 for_each_possible_cpu(i) {
976 per_cpu(nfsd_file_cache_hits, i) = 0;
977 per_cpu(nfsd_file_acquisitions, i) = 0;
978 per_cpu(nfsd_file_allocations, i) = 0;
979 per_cpu(nfsd_file_releases, i) = 0;
980 per_cpu(nfsd_file_total_age, i) = 0;
981 per_cpu(nfsd_file_evictions, i) = 0;
982 }
983 }
984
985 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)986 nfsd_file_lookup_locked(const struct net *net, const struct cred *cred,
987 struct inode *inode, unsigned char need,
988 bool want_gc)
989 {
990 struct rhlist_head *tmp, *list;
991 struct nfsd_file *nf;
992
993 list = rhltable_lookup(&nfsd_file_rhltable, &inode,
994 nfsd_file_rhash_params);
995 rhl_for_each_entry_rcu(nf, tmp, list, nf_rlist) {
996 if (nf->nf_may != need)
997 continue;
998 if (nf->nf_net != net)
999 continue;
1000 if (!nfsd_match_cred(nf->nf_cred, cred))
1001 continue;
1002 if (test_bit(NFSD_FILE_GC, &nf->nf_flags) != want_gc)
1003 continue;
1004 if (test_bit(NFSD_FILE_HASHED, &nf->nf_flags) == 0)
1005 continue;
1006
1007 if (!nfsd_file_get(nf))
1008 continue;
1009 return nf;
1010 }
1011 return NULL;
1012 }
1013
1014 /**
1015 * nfsd_file_is_cached - are there any cached open files for this inode?
1016 * @inode: inode to check
1017 *
1018 * The lookup matches inodes in all net namespaces and is atomic wrt
1019 * nfsd_file_acquire().
1020 *
1021 * Return values:
1022 * %true: filecache contains at least one file matching this inode
1023 * %false: filecache contains no files matching this inode
1024 */
1025 bool
nfsd_file_is_cached(struct inode * inode)1026 nfsd_file_is_cached(struct inode *inode)
1027 {
1028 struct rhlist_head *tmp, *list;
1029 struct nfsd_file *nf;
1030 bool ret = false;
1031
1032 rcu_read_lock();
1033 list = rhltable_lookup(&nfsd_file_rhltable, &inode,
1034 nfsd_file_rhash_params);
1035 rhl_for_each_entry_rcu(nf, tmp, list, nf_rlist)
1036 if (test_bit(NFSD_FILE_GC, &nf->nf_flags)) {
1037 ret = true;
1038 break;
1039 }
1040 rcu_read_unlock();
1041
1042 trace_nfsd_file_is_cached(inode, (int)ret);
1043 return ret;
1044 }
1045
1046 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)1047 nfsd_file_do_acquire(struct svc_rqst *rqstp, struct net *net,
1048 struct svc_cred *cred,
1049 struct auth_domain *client,
1050 struct svc_fh *fhp,
1051 unsigned int may_flags, struct file *file,
1052 struct nfsd_file **pnf, bool want_gc)
1053 {
1054 unsigned char need = may_flags & NFSD_FILE_MAY_MASK;
1055 struct nfsd_file *new, *nf;
1056 bool stale_retry = true;
1057 bool open_retry = true;
1058 struct inode *inode;
1059 __be32 status;
1060 int ret;
1061
1062 retry:
1063 if (rqstp) {
1064 status = fh_verify(rqstp, fhp, S_IFREG,
1065 may_flags|NFSD_MAY_OWNER_OVERRIDE);
1066 } else {
1067 status = fh_verify_local(net, cred, client, fhp, S_IFREG,
1068 may_flags|NFSD_MAY_OWNER_OVERRIDE);
1069 }
1070 if (status != nfs_ok)
1071 return status;
1072 inode = d_inode(fhp->fh_dentry);
1073
1074 rcu_read_lock();
1075 nf = nfsd_file_lookup_locked(net, current_cred(), inode, need, want_gc);
1076 rcu_read_unlock();
1077
1078 if (nf)
1079 goto wait_for_construction;
1080
1081 new = nfsd_file_alloc(net, inode, need, want_gc);
1082 if (!new) {
1083 status = nfserr_jukebox;
1084 goto out;
1085 }
1086
1087 rcu_read_lock();
1088 spin_lock(&inode->i_lock);
1089 nf = nfsd_file_lookup_locked(net, current_cred(), inode, need, want_gc);
1090 if (unlikely(nf)) {
1091 spin_unlock(&inode->i_lock);
1092 rcu_read_unlock();
1093 nfsd_file_free(new);
1094 goto wait_for_construction;
1095 }
1096 nf = new;
1097 ret = rhltable_insert(&nfsd_file_rhltable, &nf->nf_rlist,
1098 nfsd_file_rhash_params);
1099 spin_unlock(&inode->i_lock);
1100 rcu_read_unlock();
1101 if (likely(ret == 0))
1102 goto open_file;
1103
1104 trace_nfsd_file_insert_err(rqstp, inode, may_flags, ret);
1105 status = nfserr_jukebox;
1106 goto construction_err;
1107
1108 wait_for_construction:
1109 wait_on_bit(&nf->nf_flags, NFSD_FILE_PENDING, TASK_UNINTERRUPTIBLE);
1110
1111 /* Did construction of this file fail? */
1112 if (!test_bit(NFSD_FILE_HASHED, &nf->nf_flags)) {
1113 trace_nfsd_file_cons_err(rqstp, inode, may_flags, nf);
1114 if (!open_retry) {
1115 status = nfserr_jukebox;
1116 goto construction_err;
1117 }
1118 nfsd_file_put(nf);
1119 open_retry = false;
1120 fh_put(fhp);
1121 goto retry;
1122 }
1123 this_cpu_inc(nfsd_file_cache_hits);
1124
1125 status = nfserrno(nfsd_open_break_lease(file_inode(nf->nf_file), may_flags));
1126 if (status != nfs_ok) {
1127 nfsd_file_put(nf);
1128 nf = NULL;
1129 }
1130
1131 out:
1132 if (status == nfs_ok) {
1133 this_cpu_inc(nfsd_file_acquisitions);
1134 nfsd_file_check_write_error(nf);
1135 *pnf = nf;
1136 }
1137 trace_nfsd_file_acquire(rqstp, inode, may_flags, nf, status);
1138 return status;
1139
1140 open_file:
1141 trace_nfsd_file_alloc(nf);
1142 nf->nf_mark = nfsd_file_mark_find_or_create(inode);
1143 if (nf->nf_mark) {
1144 if (file) {
1145 get_file(file);
1146 nf->nf_file = file;
1147 status = nfs_ok;
1148 trace_nfsd_file_opened(nf, status);
1149 } else {
1150 ret = nfsd_open_verified(fhp, may_flags, &nf->nf_file);
1151 if (ret == -EOPENSTALE && stale_retry) {
1152 stale_retry = false;
1153 nfsd_file_unhash(nf);
1154 clear_and_wake_up_bit(NFSD_FILE_PENDING,
1155 &nf->nf_flags);
1156 if (refcount_dec_and_test(&nf->nf_ref))
1157 nfsd_file_free(nf);
1158 nf = NULL;
1159 fh_put(fhp);
1160 goto retry;
1161 }
1162 status = nfserrno(ret);
1163 trace_nfsd_file_open(nf, status);
1164 }
1165 } else
1166 status = nfserr_jukebox;
1167 /*
1168 * If construction failed, or we raced with a call to unlink()
1169 * then unhash.
1170 */
1171 if (status != nfs_ok || inode->i_nlink == 0)
1172 nfsd_file_unhash(nf);
1173 else if (want_gc)
1174 nfsd_file_lru_add(nf);
1175
1176 clear_and_wake_up_bit(NFSD_FILE_PENDING, &nf->nf_flags);
1177 if (status == nfs_ok)
1178 goto out;
1179
1180 construction_err:
1181 if (refcount_dec_and_test(&nf->nf_ref))
1182 nfsd_file_free(nf);
1183 nf = NULL;
1184 goto out;
1185 }
1186
1187 /**
1188 * nfsd_file_acquire_gc - Get a struct nfsd_file with an open file
1189 * @rqstp: the RPC transaction being executed
1190 * @fhp: the NFS filehandle of the file to be opened
1191 * @may_flags: NFSD_MAY_ settings for the file
1192 * @pnf: OUT: new or found "struct nfsd_file" object
1193 *
1194 * The nfsd_file object returned by this API is reference-counted
1195 * and garbage-collected. The object is retained for a few
1196 * seconds after the final nfsd_file_put() in case the caller
1197 * wants to re-use it.
1198 *
1199 * Return values:
1200 * %nfs_ok - @pnf points to an nfsd_file with its reference
1201 * count boosted.
1202 *
1203 * On error, an nfsstat value in network byte order is returned.
1204 */
1205 __be32
nfsd_file_acquire_gc(struct svc_rqst * rqstp,struct svc_fh * fhp,unsigned int may_flags,struct nfsd_file ** pnf)1206 nfsd_file_acquire_gc(struct svc_rqst *rqstp, struct svc_fh *fhp,
1207 unsigned int may_flags, struct nfsd_file **pnf)
1208 {
1209 return nfsd_file_do_acquire(rqstp, SVC_NET(rqstp), NULL, NULL,
1210 fhp, may_flags, NULL, pnf, true);
1211 }
1212
1213 /**
1214 * nfsd_file_acquire - 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 * but not garbage-collected. The object is unhashed after the
1222 * final nfsd_file_put().
1223 *
1224 * Return values:
1225 * %nfs_ok - @pnf points to an nfsd_file with its reference
1226 * count boosted.
1227 *
1228 * On error, an nfsstat value in network byte order is returned.
1229 */
1230 __be32
nfsd_file_acquire(struct svc_rqst * rqstp,struct svc_fh * fhp,unsigned int may_flags,struct nfsd_file ** pnf)1231 nfsd_file_acquire(struct svc_rqst *rqstp, struct svc_fh *fhp,
1232 unsigned int may_flags, struct nfsd_file **pnf)
1233 {
1234 return nfsd_file_do_acquire(rqstp, SVC_NET(rqstp), NULL, NULL,
1235 fhp, may_flags, NULL, pnf, false);
1236 }
1237
1238 /**
1239 * nfsd_file_acquire_local - Get a struct nfsd_file with an open file for localio
1240 * @net: The network namespace in which to perform a lookup
1241 * @cred: the user credential with which to validate access
1242 * @client: the auth_domain for LOCALIO lookup
1243 * @fhp: the NFS filehandle of the file to be opened
1244 * @may_flags: NFSD_MAY_ settings for the file
1245 * @pnf: OUT: new or found "struct nfsd_file" object
1246 *
1247 * This file lookup interface provide access to a file given the
1248 * filehandle and credential. No connection-based authorisation
1249 * is performed and in that way it is quite different to other
1250 * file access mediated by nfsd. It allows a kernel module such as the NFS
1251 * client to reach across network and filesystem namespaces to access
1252 * a file. The security implications of this should be carefully
1253 * considered before use.
1254 *
1255 * The nfsd_file_object returned by this API is reference-counted
1256 * but not garbage-collected. The object is unhashed after the
1257 * final nfsd_file_put().
1258 *
1259 * Return values:
1260 * %nfs_ok - @pnf points to an nfsd_file with its reference
1261 * count boosted.
1262 *
1263 * On error, an nfsstat value in network byte order is returned.
1264 */
1265 __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)1266 nfsd_file_acquire_local(struct net *net, struct svc_cred *cred,
1267 struct auth_domain *client, struct svc_fh *fhp,
1268 unsigned int may_flags, struct nfsd_file **pnf)
1269 {
1270 /*
1271 * Save creds before calling nfsd_file_do_acquire() (which calls
1272 * nfsd_setuser). Important because caller (LOCALIO) is from
1273 * client context.
1274 */
1275 const struct cred *save_cred = get_current_cred();
1276 __be32 beres;
1277
1278 beres = nfsd_file_do_acquire(NULL, net, cred, client,
1279 fhp, may_flags, NULL, pnf, false);
1280 put_cred(revert_creds(save_cred));
1281 return beres;
1282 }
1283
1284 /**
1285 * nfsd_file_acquire_opened - Get a struct nfsd_file using existing open file
1286 * @rqstp: the RPC transaction being executed
1287 * @fhp: the NFS filehandle of the file just created
1288 * @may_flags: NFSD_MAY_ settings for the file
1289 * @file: cached, already-open file (may be NULL)
1290 * @pnf: OUT: new or found "struct nfsd_file" object
1291 *
1292 * Acquire a nfsd_file object that is not GC'ed. If one doesn't already exist,
1293 * and @file is non-NULL, use it to instantiate a new nfsd_file instead of
1294 * opening a new one.
1295 *
1296 * Return values:
1297 * %nfs_ok - @pnf points to an nfsd_file with its reference
1298 * count boosted.
1299 *
1300 * On error, an nfsstat value in network byte order is returned.
1301 */
1302 __be32
nfsd_file_acquire_opened(struct svc_rqst * rqstp,struct svc_fh * fhp,unsigned int may_flags,struct file * file,struct nfsd_file ** pnf)1303 nfsd_file_acquire_opened(struct svc_rqst *rqstp, struct svc_fh *fhp,
1304 unsigned int may_flags, struct file *file,
1305 struct nfsd_file **pnf)
1306 {
1307 return nfsd_file_do_acquire(rqstp, SVC_NET(rqstp), NULL, NULL,
1308 fhp, may_flags, file, pnf, false);
1309 }
1310
1311 /*
1312 * Note that fields may be added, removed or reordered in the future. Programs
1313 * scraping this file for info should test the labels to ensure they're
1314 * getting the correct field.
1315 */
nfsd_file_cache_stats_show(struct seq_file * m,void * v)1316 int nfsd_file_cache_stats_show(struct seq_file *m, void *v)
1317 {
1318 unsigned long allocations = 0, releases = 0, evictions = 0;
1319 unsigned long hits = 0, acquisitions = 0;
1320 unsigned int i, count = 0, buckets = 0;
1321 unsigned long lru = 0, total_age = 0;
1322
1323 /* Serialize with server shutdown */
1324 mutex_lock(&nfsd_mutex);
1325 if (test_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 1) {
1326 struct bucket_table *tbl;
1327 struct rhashtable *ht;
1328
1329 lru = list_lru_count(&nfsd_file_lru);
1330
1331 rcu_read_lock();
1332 ht = &nfsd_file_rhltable.ht;
1333 count = atomic_read(&ht->nelems);
1334 tbl = rht_dereference_rcu(ht->tbl, ht);
1335 buckets = tbl->size;
1336 rcu_read_unlock();
1337 }
1338 mutex_unlock(&nfsd_mutex);
1339
1340 for_each_possible_cpu(i) {
1341 hits += per_cpu(nfsd_file_cache_hits, i);
1342 acquisitions += per_cpu(nfsd_file_acquisitions, i);
1343 allocations += per_cpu(nfsd_file_allocations, i);
1344 releases += per_cpu(nfsd_file_releases, i);
1345 total_age += per_cpu(nfsd_file_total_age, i);
1346 evictions += per_cpu(nfsd_file_evictions, i);
1347 }
1348
1349 seq_printf(m, "total inodes: %u\n", count);
1350 seq_printf(m, "hash buckets: %u\n", buckets);
1351 seq_printf(m, "lru entries: %lu\n", lru);
1352 seq_printf(m, "cache hits: %lu\n", hits);
1353 seq_printf(m, "acquisitions: %lu\n", acquisitions);
1354 seq_printf(m, "allocations: %lu\n", allocations);
1355 seq_printf(m, "releases: %lu\n", releases);
1356 seq_printf(m, "evictions: %lu\n", evictions);
1357 if (releases)
1358 seq_printf(m, "mean age (ms): %ld\n", total_age / releases);
1359 else
1360 seq_printf(m, "mean age (ms): -\n");
1361 return 0;
1362 }
1363