Blame view

fs/nfsd/filecache.c 26.8 KB
65294c1f2   Jeff Layton   nfsd: add a new s...
1
2
3
4
5
6
7
8
  /*
   * Open file cache.
   *
   * (c) 2015 - Jeff Layton <jeff.layton@primarydata.com>
   */
  
  #include <linux/hash.h>
  #include <linux/slab.h>
65294c1f2   Jeff Layton   nfsd: add a new s...
9
10
11
12
13
14
15
16
17
18
  #include <linux/file.h>
  #include <linux/sched.h>
  #include <linux/list_lru.h>
  #include <linux/fsnotify_backend.h>
  #include <linux/fsnotify.h>
  #include <linux/seq_file.h>
  
  #include "vfs.h"
  #include "nfsd.h"
  #include "nfsfh.h"
5e113224c   Trond Myklebust   nfsd: nfsd_file c...
19
  #include "netns.h"
65294c1f2   Jeff Layton   nfsd: add a new s...
20
21
22
23
24
25
26
27
28
  #include "filecache.h"
  #include "trace.h"
  
  #define NFSDDBG_FACILITY	NFSDDBG_FH
  
  /* FIXME: dynamically size this for the machine somehow? */
  #define NFSD_FILE_HASH_BITS                   12
  #define NFSD_FILE_HASH_SIZE                  (1 << NFSD_FILE_HASH_BITS)
  #define NFSD_LAUNDRETTE_DELAY		     (2 * HZ)
65294c1f2   Jeff Layton   nfsd: add a new s...
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  #define NFSD_FILE_SHUTDOWN		     (1)
  #define NFSD_FILE_LRU_THRESHOLD		     (4096UL)
  #define NFSD_FILE_LRU_LIMIT		     (NFSD_FILE_LRU_THRESHOLD << 2)
  
  /* We only care about NFSD_MAY_READ/WRITE for this cache */
  #define NFSD_FILE_MAY_MASK	(NFSD_MAY_READ|NFSD_MAY_WRITE)
  
  struct nfsd_fcache_bucket {
  	struct hlist_head	nfb_head;
  	spinlock_t		nfb_lock;
  	unsigned int		nfb_count;
  	unsigned int		nfb_maxcount;
  };
  
  static DEFINE_PER_CPU(unsigned long, nfsd_file_cache_hits);
9542e6a64   Trond Myklebust   nfsd: Containeris...
44
45
46
47
48
49
50
51
  struct nfsd_fcache_disposal {
  	struct list_head list;
  	struct work_struct work;
  	struct net *net;
  	spinlock_t lock;
  	struct list_head freeme;
  	struct rcu_head rcu;
  };
50d0def96   Chen Zhou   nfsd: make nfsd_f...
52
  static struct workqueue_struct *nfsd_filecache_wq __read_mostly;
9542e6a64   Trond Myklebust   nfsd: Containeris...
53

65294c1f2   Jeff Layton   nfsd: add a new s...
54
55
56
57
58
59
60
61
  static struct kmem_cache		*nfsd_file_slab;
  static struct kmem_cache		*nfsd_file_mark_slab;
  static struct nfsd_fcache_bucket	*nfsd_file_hashtbl;
  static struct list_lru			nfsd_file_lru;
  static long				nfsd_file_lru_flags;
  static struct fsnotify_group		*nfsd_file_fsnotify_group;
  static atomic_long_t			nfsd_filecache_count;
  static struct delayed_work		nfsd_filecache_laundrette;
9542e6a64   Trond Myklebust   nfsd: Containeris...
62
63
  static DEFINE_SPINLOCK(laundrette_lock);
  static LIST_HEAD(laundrettes);
65294c1f2   Jeff Layton   nfsd: add a new s...
64

9542e6a64   Trond Myklebust   nfsd: Containeris...
65
  static void nfsd_file_gc(void);
65294c1f2   Jeff Layton   nfsd: add a new s...
66
67
  
  static void
9542e6a64   Trond Myklebust   nfsd: Containeris...
68
  nfsd_file_schedule_laundrette(void)
65294c1f2   Jeff Layton   nfsd: add a new s...
69
70
71
72
73
  {
  	long count = atomic_long_read(&nfsd_filecache_count);
  
  	if (count == 0 || test_bit(NFSD_FILE_SHUTDOWN, &nfsd_file_lru_flags))
  		return;
9542e6a64   Trond Myklebust   nfsd: Containeris...
74
75
  	queue_delayed_work(system_wq, &nfsd_filecache_laundrette,
  			NFSD_LAUNDRETTE_DELAY);
65294c1f2   Jeff Layton   nfsd: add a new s...
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
  }
  
  static void
  nfsd_file_slab_free(struct rcu_head *rcu)
  {
  	struct nfsd_file *nf = container_of(rcu, struct nfsd_file, nf_rcu);
  
  	put_cred(nf->nf_cred);
  	kmem_cache_free(nfsd_file_slab, nf);
  }
  
  static void
  nfsd_file_mark_free(struct fsnotify_mark *mark)
  {
  	struct nfsd_file_mark *nfm = container_of(mark, struct nfsd_file_mark,
  						  nfm_mark);
  
  	kmem_cache_free(nfsd_file_mark_slab, nfm);
  }
  
  static struct nfsd_file_mark *
  nfsd_file_mark_get(struct nfsd_file_mark *nfm)
  {
689827cd5   Trond Myklebust   nfsd: convert fil...
99
  	if (!refcount_inc_not_zero(&nfm->nfm_ref))
65294c1f2   Jeff Layton   nfsd: add a new s...
100
101
102
103
104
105
106
  		return NULL;
  	return nfm;
  }
  
  static void
  nfsd_file_mark_put(struct nfsd_file_mark *nfm)
  {
689827cd5   Trond Myklebust   nfsd: convert fil...
107
  	if (refcount_dec_and_test(&nfm->nfm_ref)) {
65294c1f2   Jeff Layton   nfsd: add a new s...
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
  		fsnotify_destroy_mark(&nfm->nfm_mark, nfsd_file_fsnotify_group);
  		fsnotify_put_mark(&nfm->nfm_mark);
  	}
  }
  
  static struct nfsd_file_mark *
  nfsd_file_mark_find_or_create(struct nfsd_file *nf)
  {
  	int			err;
  	struct fsnotify_mark	*mark;
  	struct nfsd_file_mark	*nfm = NULL, *new;
  	struct inode *inode = nf->nf_inode;
  
  	do {
  		mutex_lock(&nfsd_file_fsnotify_group->mark_mutex);
  		mark = fsnotify_find_mark(&inode->i_fsnotify_marks,
  				nfsd_file_fsnotify_group);
  		if (mark) {
  			nfm = nfsd_file_mark_get(container_of(mark,
  						 struct nfsd_file_mark,
  						 nfm_mark));
  			mutex_unlock(&nfsd_file_fsnotify_group->mark_mutex);
90d2f1da8   Trond Myklebust   nfsd: Fix a soft ...
130
131
  			if (nfm) {
  				fsnotify_put_mark(mark);
65294c1f2   Jeff Layton   nfsd: add a new s...
132
  				break;
90d2f1da8   Trond Myklebust   nfsd: Fix a soft ...
133
134
135
136
  			}
  			/* Avoid soft lockup race with nfsd_file_mark_put() */
  			fsnotify_destroy_mark(mark, nfsd_file_fsnotify_group);
  			fsnotify_put_mark(mark);
65294c1f2   Jeff Layton   nfsd: add a new s...
137
138
139
140
141
142
143
144
145
  		} else
  			mutex_unlock(&nfsd_file_fsnotify_group->mark_mutex);
  
  		/* allocate a new nfm */
  		new = kmem_cache_alloc(nfsd_file_mark_slab, GFP_KERNEL);
  		if (!new)
  			return NULL;
  		fsnotify_init_mark(&new->nfm_mark, nfsd_file_fsnotify_group);
  		new->nfm_mark.mask = FS_ATTRIB|FS_DELETE_SELF;
689827cd5   Trond Myklebust   nfsd: convert fil...
146
  		refcount_set(&new->nfm_ref, 1);
65294c1f2   Jeff Layton   nfsd: add a new s...
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
  
  		err = fsnotify_add_inode_mark(&new->nfm_mark, inode, 0);
  
  		/*
  		 * If the add was successful, then return the object.
  		 * Otherwise, we need to put the reference we hold on the
  		 * nfm_mark. The fsnotify code will take a reference and put
  		 * it on failure, so we can't just free it directly. It's also
  		 * not safe to call fsnotify_destroy_mark on it as the
  		 * mark->group will be NULL. Thus, we can't let the nfm_ref
  		 * counter drive the destruction at this point.
  		 */
  		if (likely(!err))
  			nfm = new;
  		else
  			fsnotify_put_mark(&new->nfm_mark);
  	} while (unlikely(err == -EEXIST));
  
  	return nfm;
  }
  
  static struct nfsd_file *
5e113224c   Trond Myklebust   nfsd: nfsd_file c...
169
170
  nfsd_file_alloc(struct inode *inode, unsigned int may, unsigned int hashval,
  		struct net *net)
65294c1f2   Jeff Layton   nfsd: add a new s...
171
172
173
174
175
176
177
178
179
  {
  	struct nfsd_file *nf;
  
  	nf = kmem_cache_alloc(nfsd_file_slab, GFP_KERNEL);
  	if (nf) {
  		INIT_HLIST_NODE(&nf->nf_node);
  		INIT_LIST_HEAD(&nf->nf_lru);
  		nf->nf_file = NULL;
  		nf->nf_cred = get_current_cred();
5e113224c   Trond Myklebust   nfsd: nfsd_file c...
180
  		nf->nf_net = net;
65294c1f2   Jeff Layton   nfsd: add a new s...
181
182
183
  		nf->nf_flags = 0;
  		nf->nf_inode = inode;
  		nf->nf_hashval = hashval;
689827cd5   Trond Myklebust   nfsd: convert fil...
184
  		refcount_set(&nf->nf_ref, 1);
65294c1f2   Jeff Layton   nfsd: add a new s...
185
186
187
188
189
190
191
192
  		nf->nf_may = may & NFSD_FILE_MAY_MASK;
  		if (may & NFSD_MAY_NOT_BREAK_LEASE) {
  			if (may & NFSD_MAY_WRITE)
  				__set_bit(NFSD_FILE_BREAK_WRITE, &nf->nf_flags);
  			if (may & NFSD_MAY_READ)
  				__set_bit(NFSD_FILE_BREAK_READ, &nf->nf_flags);
  		}
  		nf->nf_mark = NULL;
5011af4c6   Trond Myklebust   nfsd: Fix stable ...
193
  		init_rwsem(&nf->nf_rwsem);
65294c1f2   Jeff Layton   nfsd: add a new s...
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
  		trace_nfsd_file_alloc(nf);
  	}
  	return nf;
  }
  
  static bool
  nfsd_file_free(struct nfsd_file *nf)
  {
  	bool flush = false;
  
  	trace_nfsd_file_put_final(nf);
  	if (nf->nf_mark)
  		nfsd_file_mark_put(nf->nf_mark);
  	if (nf->nf_file) {
  		get_file(nf->nf_file);
  		filp_close(nf->nf_file, NULL);
  		fput(nf->nf_file);
  		flush = true;
  	}
  	call_rcu(&nf->nf_rcu, nfsd_file_slab_free);
  	return flush;
  }
055b24a8f   Trond Myklebust   nfsd: Don't garba...
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
  static bool
  nfsd_file_check_writeback(struct nfsd_file *nf)
  {
  	struct file *file = nf->nf_file;
  	struct address_space *mapping;
  
  	if (!file || !(file->f_mode & FMODE_WRITE))
  		return false;
  	mapping = file->f_mapping;
  	return mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) ||
  		mapping_tagged(mapping, PAGECACHE_TAG_WRITEBACK);
  }
  
  static int
  nfsd_file_check_write_error(struct nfsd_file *nf)
  {
  	struct file *file = nf->nf_file;
  
  	if (!file || !(file->f_mode & FMODE_WRITE))
  		return 0;
  	return filemap_check_wb_err(file->f_mapping, READ_ONCE(file->f_wb_err));
  }
65294c1f2   Jeff Layton   nfsd: add a new s...
238
239
240
241
242
243
  static void
  nfsd_file_do_unhash(struct nfsd_file *nf)
  {
  	lockdep_assert_held(&nfsd_file_hashtbl[nf->nf_hashval].nfb_lock);
  
  	trace_nfsd_file_unhash(nf);
055b24a8f   Trond Myklebust   nfsd: Don't garba...
244
245
  	if (nfsd_file_check_write_error(nf))
  		nfsd_reset_boot_verifier(net_generic(nf->nf_net, nfsd_net_id));
65294c1f2   Jeff Layton   nfsd: add a new s...
246
247
  	--nfsd_file_hashtbl[nf->nf_hashval].nfb_count;
  	hlist_del_rcu(&nf->nf_node);
65294c1f2   Jeff Layton   nfsd: add a new s...
248
249
250
251
252
253
254
255
  	atomic_long_dec(&nfsd_filecache_count);
  }
  
  static bool
  nfsd_file_unhash(struct nfsd_file *nf)
  {
  	if (test_and_clear_bit(NFSD_FILE_HASHED, &nf->nf_flags)) {
  		nfsd_file_do_unhash(nf);
36ebbdb96   Trond Myklebust   nfsd: cleanup nfs...
256
257
  		if (!list_empty(&nf->nf_lru))
  			list_lru_del(&nfsd_file_lru, &nf->nf_lru);
65294c1f2   Jeff Layton   nfsd: add a new s...
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
  		return true;
  	}
  	return false;
  }
  
  /*
   * Return true if the file was unhashed.
   */
  static bool
  nfsd_file_unhash_and_release_locked(struct nfsd_file *nf, struct list_head *dispose)
  {
  	lockdep_assert_held(&nfsd_file_hashtbl[nf->nf_hashval].nfb_lock);
  
  	trace_nfsd_file_unhash_and_release_locked(nf);
  	if (!nfsd_file_unhash(nf))
  		return false;
  	/* keep final reference for nfsd_file_lru_dispose */
689827cd5   Trond Myklebust   nfsd: convert fil...
275
  	if (refcount_dec_not_one(&nf->nf_ref))
65294c1f2   Jeff Layton   nfsd: add a new s...
276
277
278
279
280
  		return true;
  
  	list_add(&nf->nf_lru, dispose);
  	return true;
  }
b6669305d   Trond Myklebust   nfsd: Reduce the ...
281
  static void
65294c1f2   Jeff Layton   nfsd: add a new s...
282
283
  nfsd_file_put_noref(struct nfsd_file *nf)
  {
65294c1f2   Jeff Layton   nfsd: add a new s...
284
  	trace_nfsd_file_put(nf);
689827cd5   Trond Myklebust   nfsd: convert fil...
285
  	if (refcount_dec_and_test(&nf->nf_ref)) {
65294c1f2   Jeff Layton   nfsd: add a new s...
286
287
288
  		WARN_ON(test_bit(NFSD_FILE_HASHED, &nf->nf_flags));
  		nfsd_file_free(nf);
  	}
65294c1f2   Jeff Layton   nfsd: add a new s...
289
290
291
292
293
  }
  
  void
  nfsd_file_put(struct nfsd_file *nf)
  {
b6669305d   Trond Myklebust   nfsd: Reduce the ...
294
  	bool is_hashed;
65294c1f2   Jeff Layton   nfsd: add a new s...
295
296
  
  	set_bit(NFSD_FILE_REFERENCED, &nf->nf_flags);
689827cd5   Trond Myklebust   nfsd: convert fil...
297
  	if (refcount_read(&nf->nf_ref) > 2 || !nf->nf_file) {
b6669305d   Trond Myklebust   nfsd: Reduce the ...
298
299
300
301
302
303
304
305
  		nfsd_file_put_noref(nf);
  		return;
  	}
  
  	filemap_flush(nf->nf_file->f_mapping);
  	is_hashed = test_bit(NFSD_FILE_HASHED, &nf->nf_flags) != 0;
  	nfsd_file_put_noref(nf);
  	if (is_hashed)
9542e6a64   Trond Myklebust   nfsd: Containeris...
306
307
308
  		nfsd_file_schedule_laundrette();
  	if (atomic_long_read(&nfsd_filecache_count) >= NFSD_FILE_LRU_LIMIT)
  		nfsd_file_gc();
65294c1f2   Jeff Layton   nfsd: add a new s...
309
310
311
312
313
  }
  
  struct nfsd_file *
  nfsd_file_get(struct nfsd_file *nf)
  {
689827cd5   Trond Myklebust   nfsd: convert fil...
314
  	if (likely(refcount_inc_not_zero(&nf->nf_ref)))
65294c1f2   Jeff Layton   nfsd: add a new s...
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
  		return nf;
  	return NULL;
  }
  
  static void
  nfsd_file_dispose_list(struct list_head *dispose)
  {
  	struct nfsd_file *nf;
  
  	while(!list_empty(dispose)) {
  		nf = list_first_entry(dispose, struct nfsd_file, nf_lru);
  		list_del(&nf->nf_lru);
  		nfsd_file_put_noref(nf);
  	}
  }
  
  static void
  nfsd_file_dispose_list_sync(struct list_head *dispose)
  {
  	bool flush = false;
  	struct nfsd_file *nf;
  
  	while(!list_empty(dispose)) {
  		nf = list_first_entry(dispose, struct nfsd_file, nf_lru);
  		list_del(&nf->nf_lru);
689827cd5   Trond Myklebust   nfsd: convert fil...
340
  		if (!refcount_dec_and_test(&nf->nf_ref))
65294c1f2   Jeff Layton   nfsd: add a new s...
341
342
343
344
345
346
347
  			continue;
  		if (nfsd_file_free(nf))
  			flush = true;
  	}
  	if (flush)
  		flush_delayed_fput();
  }
9542e6a64   Trond Myklebust   nfsd: Containeris...
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
  static void
  nfsd_file_list_remove_disposal(struct list_head *dst,
  		struct nfsd_fcache_disposal *l)
  {
  	spin_lock(&l->lock);
  	list_splice_init(&l->freeme, dst);
  	spin_unlock(&l->lock);
  }
  
  static void
  nfsd_file_list_add_disposal(struct list_head *files, struct net *net)
  {
  	struct nfsd_fcache_disposal *l;
  
  	rcu_read_lock();
  	list_for_each_entry_rcu(l, &laundrettes, list) {
  		if (l->net == net) {
  			spin_lock(&l->lock);
  			list_splice_tail_init(files, &l->freeme);
  			spin_unlock(&l->lock);
  			queue_work(nfsd_filecache_wq, &l->work);
  			break;
  		}
  	}
  	rcu_read_unlock();
  }
  
  static void
  nfsd_file_list_add_pernet(struct list_head *dst, struct list_head *src,
  		struct net *net)
  {
  	struct nfsd_file *nf, *tmp;
  
  	list_for_each_entry_safe(nf, tmp, src, nf_lru) {
  		if (nf->nf_net == net)
  			list_move_tail(&nf->nf_lru, dst);
  	}
  }
  
  static void
  nfsd_file_dispose_list_delayed(struct list_head *dispose)
  {
  	LIST_HEAD(list);
  	struct nfsd_file *nf;
  
  	while(!list_empty(dispose)) {
  		nf = list_first_entry(dispose, struct nfsd_file, nf_lru);
  		nfsd_file_list_add_pernet(&list, dispose, nf->nf_net);
  		nfsd_file_list_add_disposal(&list, nf->nf_net);
  	}
  }
65294c1f2   Jeff Layton   nfsd: add a new s...
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
  /*
   * Note this can deadlock with nfsd_file_cache_purge.
   */
  static enum lru_status
  nfsd_file_lru_cb(struct list_head *item, struct list_lru_one *lru,
  		 spinlock_t *lock, void *arg)
  	__releases(lock)
  	__acquires(lock)
  {
  	struct list_head *head = arg;
  	struct nfsd_file *nf = list_entry(item, struct nfsd_file, nf_lru);
  
  	/*
  	 * Do a lockless refcount check. The hashtable holds one reference, so
  	 * we look to see if anything else has a reference, or if any have
  	 * been put since the shrinker last ran. Those don't get unhashed and
  	 * released.
  	 *
  	 * Note that in the put path, we set the flag and then decrement the
  	 * counter. Here we check the counter and then test and clear the flag.
  	 * That order is deliberate to ensure that we can do this locklessly.
  	 */
689827cd5   Trond Myklebust   nfsd: convert fil...
421
  	if (refcount_read(&nf->nf_ref) > 1)
65294c1f2   Jeff Layton   nfsd: add a new s...
422
  		goto out_skip;
055b24a8f   Trond Myklebust   nfsd: Don't garba...
423
424
425
426
427
428
429
  
  	/*
  	 * Don't throw out files that are still undergoing I/O or
  	 * that have uncleared errors pending.
  	 */
  	if (nfsd_file_check_writeback(nf))
  		goto out_skip;
65294c1f2   Jeff Layton   nfsd: add a new s...
430
  	if (test_and_clear_bit(NFSD_FILE_REFERENCED, &nf->nf_flags))
bd6e1cece   Trond Myklebust   nfsd: Remove unus...
431
  		goto out_skip;
65294c1f2   Jeff Layton   nfsd: add a new s...
432
433
434
435
436
437
  
  	if (!test_and_clear_bit(NFSD_FILE_HASHED, &nf->nf_flags))
  		goto out_skip;
  
  	list_lru_isolate_move(lru, &nf->nf_lru, head);
  	return LRU_REMOVED;
65294c1f2   Jeff Layton   nfsd: add a new s...
438
439
440
  out_skip:
  	return LRU_SKIP;
  }
9542e6a64   Trond Myklebust   nfsd: Containeris...
441
442
  static unsigned long
  nfsd_file_lru_walk_list(struct shrink_control *sc)
65294c1f2   Jeff Layton   nfsd: add a new s...
443
  {
9542e6a64   Trond Myklebust   nfsd: Containeris...
444
  	LIST_HEAD(head);
36ebbdb96   Trond Myklebust   nfsd: cleanup nfs...
445
  	struct nfsd_file *nf;
9542e6a64   Trond Myklebust   nfsd: Containeris...
446
  	unsigned long ret;
36ebbdb96   Trond Myklebust   nfsd: cleanup nfs...
447

9542e6a64   Trond Myklebust   nfsd: Containeris...
448
449
450
451
452
453
454
455
  	if (sc)
  		ret = list_lru_shrink_walk(&nfsd_file_lru, sc,
  				nfsd_file_lru_cb, &head);
  	else
  		ret = list_lru_walk(&nfsd_file_lru,
  				nfsd_file_lru_cb,
  				&head, LONG_MAX);
  	list_for_each_entry(nf, &head, nf_lru) {
65294c1f2   Jeff Layton   nfsd: add a new s...
456
457
458
  		spin_lock(&nfsd_file_hashtbl[nf->nf_hashval].nfb_lock);
  		nfsd_file_do_unhash(nf);
  		spin_unlock(&nfsd_file_hashtbl[nf->nf_hashval].nfb_lock);
65294c1f2   Jeff Layton   nfsd: add a new s...
459
  	}
9542e6a64   Trond Myklebust   nfsd: Containeris...
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
  	nfsd_file_dispose_list_delayed(&head);
  	return ret;
  }
  
  static void
  nfsd_file_gc(void)
  {
  	nfsd_file_lru_walk_list(NULL);
  }
  
  static void
  nfsd_file_gc_worker(struct work_struct *work)
  {
  	nfsd_file_gc();
  	nfsd_file_schedule_laundrette();
65294c1f2   Jeff Layton   nfsd: add a new s...
475
476
477
478
479
480
481
482
483
484
485
  }
  
  static unsigned long
  nfsd_file_lru_count(struct shrinker *s, struct shrink_control *sc)
  {
  	return list_lru_count(&nfsd_file_lru);
  }
  
  static unsigned long
  nfsd_file_lru_scan(struct shrinker *s, struct shrink_control *sc)
  {
9542e6a64   Trond Myklebust   nfsd: Containeris...
486
  	return nfsd_file_lru_walk_list(sc);
65294c1f2   Jeff Layton   nfsd: add a new s...
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
  }
  
  static struct shrinker	nfsd_file_shrinker = {
  	.scan_objects = nfsd_file_lru_scan,
  	.count_objects = nfsd_file_lru_count,
  	.seeks = 1,
  };
  
  static void
  __nfsd_file_close_inode(struct inode *inode, unsigned int hashval,
  			struct list_head *dispose)
  {
  	struct nfsd_file	*nf;
  	struct hlist_node	*tmp;
  
  	spin_lock(&nfsd_file_hashtbl[hashval].nfb_lock);
  	hlist_for_each_entry_safe(nf, tmp, &nfsd_file_hashtbl[hashval].nfb_head, nf_node) {
  		if (inode == nf->nf_inode)
  			nfsd_file_unhash_and_release_locked(nf, dispose);
  	}
  	spin_unlock(&nfsd_file_hashtbl[hashval].nfb_lock);
  }
  
  /**
   * nfsd_file_close_inode_sync - attempt to forcibly close a nfsd_file
   * @inode: inode of the file to attempt to remove
   *
   * Walk the whole hash bucket, looking for any files that correspond to "inode".
   * If any do, then unhash them and put the hashtable reference to them and
   * destroy any that had their last reference put. Also ensure that any of the
   * fputs also have their final __fput done as well.
   */
  void
  nfsd_file_close_inode_sync(struct inode *inode)
  {
  	unsigned int		hashval = (unsigned int)hash_long(inode->i_ino,
  						NFSD_FILE_HASH_BITS);
  	LIST_HEAD(dispose);
  
  	__nfsd_file_close_inode(inode, hashval, &dispose);
  	trace_nfsd_file_close_inode_sync(inode, hashval, !list_empty(&dispose));
  	nfsd_file_dispose_list_sync(&dispose);
  }
  
  /**
   * nfsd_file_close_inode_sync - attempt to forcibly close a nfsd_file
   * @inode: inode of the file to attempt to remove
   *
   * Walk the whole hash bucket, looking for any files that correspond to "inode".
   * If any do, then unhash them and put the hashtable reference to them and
   * destroy any that had their last reference put.
   */
  static void
  nfsd_file_close_inode(struct inode *inode)
  {
  	unsigned int		hashval = (unsigned int)hash_long(inode->i_ino,
  						NFSD_FILE_HASH_BITS);
  	LIST_HEAD(dispose);
  
  	__nfsd_file_close_inode(inode, hashval, &dispose);
  	trace_nfsd_file_close_inode(inode, hashval, !list_empty(&dispose));
9542e6a64   Trond Myklebust   nfsd: Containeris...
548
  	nfsd_file_dispose_list_delayed(&dispose);
65294c1f2   Jeff Layton   nfsd: add a new s...
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
  }
  
  /**
   * nfsd_file_delayed_close - close unused nfsd_files
   * @work: dummy
   *
   * Walk the LRU list and close any entries that have not been used since
   * the last scan.
   *
   * Note this can deadlock with nfsd_file_cache_purge.
   */
  static void
  nfsd_file_delayed_close(struct work_struct *work)
  {
  	LIST_HEAD(head);
9542e6a64   Trond Myklebust   nfsd: Containeris...
564
565
  	struct nfsd_fcache_disposal *l = container_of(work,
  			struct nfsd_fcache_disposal, work);
65294c1f2   Jeff Layton   nfsd: add a new s...
566

9542e6a64   Trond Myklebust   nfsd: Containeris...
567
568
  	nfsd_file_list_remove_disposal(&head, l);
  	nfsd_file_dispose_list(&head);
65294c1f2   Jeff Layton   nfsd: add a new s...
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
  }
  
  static int
  nfsd_file_lease_notifier_call(struct notifier_block *nb, unsigned long arg,
  			    void *data)
  {
  	struct file_lock *fl = data;
  
  	/* Only close files for F_SETLEASE leases */
  	if (fl->fl_flags & FL_LEASE)
  		nfsd_file_close_inode_sync(file_inode(fl->fl_file));
  	return 0;
  }
  
  static struct notifier_block nfsd_file_lease_notifier = {
  	.notifier_call = nfsd_file_lease_notifier_call,
  };
  
  static int
b9a1b9772   Amir Goldstein   fsnotify: create ...
588
589
  nfsd_file_fsnotify_handle_event(struct fsnotify_mark *mark, u32 mask,
  				struct inode *inode, struct inode *dir,
c9be99c86   Amir Goldstein   fsnotify: general...
590
  				const struct qstr *name, u32 cookie)
65294c1f2   Jeff Layton   nfsd: add a new s...
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
  {
  	trace_nfsd_file_fsnotify_handle_event(inode, mask);
  
  	/* Should be no marks on non-regular files */
  	if (!S_ISREG(inode->i_mode)) {
  		WARN_ON_ONCE(1);
  		return 0;
  	}
  
  	/* don't close files if this was not the last link */
  	if (mask & FS_ATTRIB) {
  		if (inode->i_nlink)
  			return 0;
  	}
  
  	nfsd_file_close_inode(inode);
  	return 0;
  }
  
  
  static const struct fsnotify_ops nfsd_file_fsnotify_ops = {
b9a1b9772   Amir Goldstein   fsnotify: create ...
612
  	.handle_inode_event = nfsd_file_fsnotify_handle_event,
65294c1f2   Jeff Layton   nfsd: add a new s...
613
614
615
616
617
618
619
620
621
622
623
624
625
  	.free_mark = nfsd_file_mark_free,
  };
  
  int
  nfsd_file_cache_init(void)
  {
  	int		ret = -ENOMEM;
  	unsigned int	i;
  
  	clear_bit(NFSD_FILE_SHUTDOWN, &nfsd_file_lru_flags);
  
  	if (nfsd_file_hashtbl)
  		return 0;
9542e6a64   Trond Myklebust   nfsd: Containeris...
626
627
628
  	nfsd_filecache_wq = alloc_workqueue("nfsd_filecache", 0, 0);
  	if (!nfsd_filecache_wq)
  		goto out;
65294c1f2   Jeff Layton   nfsd: add a new s...
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
  	nfsd_file_hashtbl = kcalloc(NFSD_FILE_HASH_SIZE,
  				sizeof(*nfsd_file_hashtbl), GFP_KERNEL);
  	if (!nfsd_file_hashtbl) {
  		pr_err("nfsd: unable to allocate nfsd_file_hashtbl
  ");
  		goto out_err;
  	}
  
  	nfsd_file_slab = kmem_cache_create("nfsd_file",
  				sizeof(struct nfsd_file), 0, 0, NULL);
  	if (!nfsd_file_slab) {
  		pr_err("nfsd: unable to create nfsd_file_slab
  ");
  		goto out_err;
  	}
  
  	nfsd_file_mark_slab = kmem_cache_create("nfsd_file_mark",
  					sizeof(struct nfsd_file_mark), 0, 0, NULL);
  	if (!nfsd_file_mark_slab) {
  		pr_err("nfsd: unable to create nfsd_file_mark_slab
  ");
  		goto out_err;
  	}
  
  
  	ret = list_lru_init(&nfsd_file_lru);
  	if (ret) {
  		pr_err("nfsd: failed to init nfsd_file_lru: %d
  ", ret);
  		goto out_err;
  	}
  
  	ret = register_shrinker(&nfsd_file_shrinker);
  	if (ret) {
  		pr_err("nfsd: failed to register nfsd_file_shrinker: %d
  ", ret);
  		goto out_lru;
  	}
  
  	ret = lease_register_notifier(&nfsd_file_lease_notifier);
  	if (ret) {
  		pr_err("nfsd: unable to register lease notifier: %d
  ", ret);
  		goto out_shrinker;
  	}
  
  	nfsd_file_fsnotify_group = fsnotify_alloc_group(&nfsd_file_fsnotify_ops);
  	if (IS_ERR(nfsd_file_fsnotify_group)) {
  		pr_err("nfsd: unable to create fsnotify group: %ld
  ",
  			PTR_ERR(nfsd_file_fsnotify_group));
  		nfsd_file_fsnotify_group = NULL;
  		goto out_notifier;
  	}
  
  	for (i = 0; i < NFSD_FILE_HASH_SIZE; i++) {
  		INIT_HLIST_HEAD(&nfsd_file_hashtbl[i].nfb_head);
  		spin_lock_init(&nfsd_file_hashtbl[i].nfb_lock);
  	}
9542e6a64   Trond Myklebust   nfsd: Containeris...
688
  	INIT_DELAYED_WORK(&nfsd_filecache_laundrette, nfsd_file_gc_worker);
65294c1f2   Jeff Layton   nfsd: add a new s...
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
  out:
  	return ret;
  out_notifier:
  	lease_unregister_notifier(&nfsd_file_lease_notifier);
  out_shrinker:
  	unregister_shrinker(&nfsd_file_shrinker);
  out_lru:
  	list_lru_destroy(&nfsd_file_lru);
  out_err:
  	kmem_cache_destroy(nfsd_file_slab);
  	nfsd_file_slab = NULL;
  	kmem_cache_destroy(nfsd_file_mark_slab);
  	nfsd_file_mark_slab = NULL;
  	kfree(nfsd_file_hashtbl);
  	nfsd_file_hashtbl = NULL;
9542e6a64   Trond Myklebust   nfsd: Containeris...
704
705
  	destroy_workqueue(nfsd_filecache_wq);
  	nfsd_filecache_wq = NULL;
65294c1f2   Jeff Layton   nfsd: add a new s...
706
707
708
709
710
711
712
  	goto out;
  }
  
  /*
   * Note this can deadlock with nfsd_file_lru_cb.
   */
  void
5e113224c   Trond Myklebust   nfsd: nfsd_file c...
713
  nfsd_file_cache_purge(struct net *net)
65294c1f2   Jeff Layton   nfsd: add a new s...
714
715
716
  {
  	unsigned int		i;
  	struct nfsd_file	*nf;
5e113224c   Trond Myklebust   nfsd: nfsd_file c...
717
  	struct hlist_node	*next;
65294c1f2   Jeff Layton   nfsd: add a new s...
718
719
720
721
722
723
724
  	LIST_HEAD(dispose);
  	bool del;
  
  	if (!nfsd_file_hashtbl)
  		return;
  
  	for (i = 0; i < NFSD_FILE_HASH_SIZE; i++) {
5e113224c   Trond Myklebust   nfsd: nfsd_file c...
725
726
727
728
729
730
  		struct nfsd_fcache_bucket *nfb = &nfsd_file_hashtbl[i];
  
  		spin_lock(&nfb->nfb_lock);
  		hlist_for_each_entry_safe(nf, next, &nfb->nfb_head, nf_node) {
  			if (net && nf->nf_net != net)
  				continue;
65294c1f2   Jeff Layton   nfsd: add a new s...
731
732
733
734
735
736
737
738
  			del = nfsd_file_unhash_and_release_locked(nf, &dispose);
  
  			/*
  			 * Deadlock detected! Something marked this entry as
  			 * unhased, but hasn't removed it from the hash list.
  			 */
  			WARN_ON_ONCE(!del);
  		}
5e113224c   Trond Myklebust   nfsd: nfsd_file c...
739
  		spin_unlock(&nfb->nfb_lock);
65294c1f2   Jeff Layton   nfsd: add a new s...
740
741
742
  		nfsd_file_dispose_list(&dispose);
  	}
  }
9542e6a64   Trond Myklebust   nfsd: Containeris...
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
  static struct nfsd_fcache_disposal *
  nfsd_alloc_fcache_disposal(struct net *net)
  {
  	struct nfsd_fcache_disposal *l;
  
  	l = kmalloc(sizeof(*l), GFP_KERNEL);
  	if (!l)
  		return NULL;
  	INIT_WORK(&l->work, nfsd_file_delayed_close);
  	l->net = net;
  	spin_lock_init(&l->lock);
  	INIT_LIST_HEAD(&l->freeme);
  	return l;
  }
  
  static void
  nfsd_free_fcache_disposal(struct nfsd_fcache_disposal *l)
  {
  	rcu_assign_pointer(l->net, NULL);
  	cancel_work_sync(&l->work);
  	nfsd_file_dispose_list(&l->freeme);
  	kfree_rcu(l, rcu);
  }
  
  static void
  nfsd_add_fcache_disposal(struct nfsd_fcache_disposal *l)
  {
  	spin_lock(&laundrette_lock);
  	list_add_tail_rcu(&l->list, &laundrettes);
  	spin_unlock(&laundrette_lock);
  }
  
  static void
  nfsd_del_fcache_disposal(struct nfsd_fcache_disposal *l)
  {
  	spin_lock(&laundrette_lock);
  	list_del_rcu(&l->list);
  	spin_unlock(&laundrette_lock);
  }
  
  static int
  nfsd_alloc_fcache_disposal_net(struct net *net)
  {
  	struct nfsd_fcache_disposal *l;
  
  	l = nfsd_alloc_fcache_disposal(net);
  	if (!l)
  		return -ENOMEM;
  	nfsd_add_fcache_disposal(l);
  	return 0;
  }
  
  static void
  nfsd_free_fcache_disposal_net(struct net *net)
  {
  	struct nfsd_fcache_disposal *l;
  
  	rcu_read_lock();
  	list_for_each_entry_rcu(l, &laundrettes, list) {
  		if (l->net != net)
  			continue;
  		nfsd_del_fcache_disposal(l);
  		rcu_read_unlock();
  		nfsd_free_fcache_disposal(l);
  		return;
  	}
  	rcu_read_unlock();
  }
  
  int
  nfsd_file_cache_start_net(struct net *net)
  {
  	return nfsd_alloc_fcache_disposal_net(net);
  }
  
  void
  nfsd_file_cache_shutdown_net(struct net *net)
  {
  	nfsd_file_cache_purge(net);
  	nfsd_free_fcache_disposal_net(net);
  }
65294c1f2   Jeff Layton   nfsd: add a new s...
824
825
826
  void
  nfsd_file_cache_shutdown(void)
  {
65294c1f2   Jeff Layton   nfsd: add a new s...
827
828
829
830
831
832
833
834
835
  	set_bit(NFSD_FILE_SHUTDOWN, &nfsd_file_lru_flags);
  
  	lease_unregister_notifier(&nfsd_file_lease_notifier);
  	unregister_shrinker(&nfsd_file_shrinker);
  	/*
  	 * make sure all callers of nfsd_file_lru_cb are done before
  	 * calling nfsd_file_cache_purge
  	 */
  	cancel_delayed_work_sync(&nfsd_filecache_laundrette);
5e113224c   Trond Myklebust   nfsd: nfsd_file c...
836
  	nfsd_file_cache_purge(NULL);
65294c1f2   Jeff Layton   nfsd: add a new s...
837
838
839
840
841
842
843
844
845
846
847
  	list_lru_destroy(&nfsd_file_lru);
  	rcu_barrier();
  	fsnotify_put_group(nfsd_file_fsnotify_group);
  	nfsd_file_fsnotify_group = NULL;
  	kmem_cache_destroy(nfsd_file_slab);
  	nfsd_file_slab = NULL;
  	fsnotify_wait_marks_destroyed();
  	kmem_cache_destroy(nfsd_file_mark_slab);
  	nfsd_file_mark_slab = NULL;
  	kfree(nfsd_file_hashtbl);
  	nfsd_file_hashtbl = NULL;
9542e6a64   Trond Myklebust   nfsd: Containeris...
848
849
  	destroy_workqueue(nfsd_filecache_wq);
  	nfsd_filecache_wq = NULL;
65294c1f2   Jeff Layton   nfsd: add a new s...
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
  }
  
  static bool
  nfsd_match_cred(const struct cred *c1, const struct cred *c2)
  {
  	int i;
  
  	if (!uid_eq(c1->fsuid, c2->fsuid))
  		return false;
  	if (!gid_eq(c1->fsgid, c2->fsgid))
  		return false;
  	if (c1->group_info == NULL || c2->group_info == NULL)
  		return c1->group_info == c2->group_info;
  	if (c1->group_info->ngroups != c2->group_info->ngroups)
  		return false;
  	for (i = 0; i < c1->group_info->ngroups; i++) {
  		if (!gid_eq(c1->group_info->gid[i], c2->group_info->gid[i]))
  			return false;
  	}
  	return true;
  }
  
  static struct nfsd_file *
  nfsd_file_find_locked(struct inode *inode, unsigned int may_flags,
5e113224c   Trond Myklebust   nfsd: nfsd_file c...
874
  			unsigned int hashval, struct net *net)
65294c1f2   Jeff Layton   nfsd: add a new s...
875
876
877
878
879
  {
  	struct nfsd_file *nf;
  	unsigned char need = may_flags & NFSD_FILE_MAY_MASK;
  
  	hlist_for_each_entry_rcu(nf, &nfsd_file_hashtbl[hashval].nfb_head,
057a22743   Madhuparna Bhowmik   fs: nfsd: fileach...
880
  				 nf_node, lockdep_is_held(&nfsd_file_hashtbl[hashval].nfb_lock)) {
ae3c57b5c   J. Bruce Fields   nfsd: Cache R, RW...
881
  		if (nf->nf_may != need)
65294c1f2   Jeff Layton   nfsd: add a new s...
882
883
884
  			continue;
  		if (nf->nf_inode != inode)
  			continue;
5e113224c   Trond Myklebust   nfsd: nfsd_file c...
885
886
  		if (nf->nf_net != net)
  			continue;
65294c1f2   Jeff Layton   nfsd: add a new s...
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
  		if (!nfsd_match_cred(nf->nf_cred, current_cred()))
  			continue;
  		if (nfsd_file_get(nf) != NULL)
  			return nf;
  	}
  	return NULL;
  }
  
  /**
   * nfsd_file_is_cached - are there any cached open files for this fh?
   * @inode: inode of the file to check
   *
   * Scan the hashtable for open files that match this fh. Returns true if there
   * are any, and false if not.
   */
  bool
  nfsd_file_is_cached(struct inode *inode)
  {
  	bool			ret = false;
  	struct nfsd_file	*nf;
  	unsigned int		hashval;
  
          hashval = (unsigned int)hash_long(inode->i_ino, NFSD_FILE_HASH_BITS);
  
  	rcu_read_lock();
  	hlist_for_each_entry_rcu(nf, &nfsd_file_hashtbl[hashval].nfb_head,
  				 nf_node) {
  		if (inode == nf->nf_inode) {
  			ret = true;
  			break;
  		}
  	}
  	rcu_read_unlock();
  	trace_nfsd_file_is_cached(inode, hashval, (int)ret);
  	return ret;
  }
  
  __be32
  nfsd_file_acquire(struct svc_rqst *rqstp, struct svc_fh *fhp,
  		  unsigned int may_flags, struct nfsd_file **pnf)
  {
  	__be32	status;
5e113224c   Trond Myklebust   nfsd: nfsd_file c...
929
  	struct net *net = SVC_NET(rqstp);
65294c1f2   Jeff Layton   nfsd: add a new s...
930
931
932
  	struct nfsd_file *nf, *new;
  	struct inode *inode;
  	unsigned int hashval;
28c7d86bb   Trond Myklebust   nfsd: fix filecac...
933
  	bool retry = true;
65294c1f2   Jeff Layton   nfsd: add a new s...
934
935
936
937
938
939
940
941
942
943
944
  
  	/* FIXME: skip this if fh_dentry is already set? */
  	status = fh_verify(rqstp, fhp, S_IFREG,
  				may_flags|NFSD_MAY_OWNER_OVERRIDE);
  	if (status != nfs_ok)
  		return status;
  
  	inode = d_inode(fhp->fh_dentry);
  	hashval = (unsigned int)hash_long(inode->i_ino, NFSD_FILE_HASH_BITS);
  retry:
  	rcu_read_lock();
5e113224c   Trond Myklebust   nfsd: nfsd_file c...
945
  	nf = nfsd_file_find_locked(inode, may_flags, hashval, net);
65294c1f2   Jeff Layton   nfsd: add a new s...
946
947
948
  	rcu_read_unlock();
  	if (nf)
  		goto wait_for_construction;
5e113224c   Trond Myklebust   nfsd: nfsd_file c...
949
  	new = nfsd_file_alloc(inode, may_flags, hashval, net);
65294c1f2   Jeff Layton   nfsd: add a new s...
950
951
952
953
954
955
956
  	if (!new) {
  		trace_nfsd_file_acquire(rqstp, hashval, inode, may_flags,
  					NULL, nfserr_jukebox);
  		return nfserr_jukebox;
  	}
  
  	spin_lock(&nfsd_file_hashtbl[hashval].nfb_lock);
5e113224c   Trond Myklebust   nfsd: nfsd_file c...
957
  	nf = nfsd_file_find_locked(inode, may_flags, hashval, net);
65294c1f2   Jeff Layton   nfsd: add a new s...
958
959
960
961
962
963
964
965
966
967
  	if (nf == NULL)
  		goto open_file;
  	spin_unlock(&nfsd_file_hashtbl[hashval].nfb_lock);
  	nfsd_file_slab_free(&new->nf_rcu);
  
  wait_for_construction:
  	wait_on_bit(&nf->nf_flags, NFSD_FILE_PENDING, TASK_UNINTERRUPTIBLE);
  
  	/* Did construction of this file fail? */
  	if (!test_bit(NFSD_FILE_HASHED, &nf->nf_flags)) {
28c7d86bb   Trond Myklebust   nfsd: fix filecac...
968
969
970
971
972
  		if (!retry) {
  			status = nfserr_jukebox;
  			goto out;
  		}
  		retry = false;
65294c1f2   Jeff Layton   nfsd: add a new s...
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
  		nfsd_file_put_noref(nf);
  		goto retry;
  	}
  
  	this_cpu_inc(nfsd_file_cache_hits);
  
  	if (!(may_flags & NFSD_MAY_NOT_BREAK_LEASE)) {
  		bool write = (may_flags & NFSD_MAY_WRITE);
  
  		if (test_bit(NFSD_FILE_BREAK_READ, &nf->nf_flags) ||
  		    (test_bit(NFSD_FILE_BREAK_WRITE, &nf->nf_flags) && write)) {
  			status = nfserrno(nfsd_open_break_lease(
  					file_inode(nf->nf_file), may_flags));
  			if (status == nfs_ok) {
  				clear_bit(NFSD_FILE_BREAK_READ, &nf->nf_flags);
  				if (write)
  					clear_bit(NFSD_FILE_BREAK_WRITE,
  						  &nf->nf_flags);
  			}
  		}
  	}
  out:
  	if (status == nfs_ok) {
  		*pnf = nf;
  	} else {
  		nfsd_file_put(nf);
  		nf = NULL;
  	}
  
  	trace_nfsd_file_acquire(rqstp, hashval, inode, may_flags, nf, status);
  	return status;
  open_file:
  	nf = new;
  	/* Take reference for the hashtable */
689827cd5   Trond Myklebust   nfsd: convert fil...
1007
  	refcount_inc(&nf->nf_ref);
65294c1f2   Jeff Layton   nfsd: add a new s...
1008
1009
1010
1011
1012
1013
1014
1015
  	__set_bit(NFSD_FILE_HASHED, &nf->nf_flags);
  	__set_bit(NFSD_FILE_PENDING, &nf->nf_flags);
  	list_lru_add(&nfsd_file_lru, &nf->nf_lru);
  	hlist_add_head_rcu(&nf->nf_node, &nfsd_file_hashtbl[hashval].nfb_head);
  	++nfsd_file_hashtbl[hashval].nfb_count;
  	nfsd_file_hashtbl[hashval].nfb_maxcount = max(nfsd_file_hashtbl[hashval].nfb_maxcount,
  			nfsd_file_hashtbl[hashval].nfb_count);
  	spin_unlock(&nfsd_file_hashtbl[hashval].nfb_lock);
9542e6a64   Trond Myklebust   nfsd: Containeris...
1016
1017
  	if (atomic_long_inc_return(&nfsd_filecache_count) >= NFSD_FILE_LRU_THRESHOLD)
  		nfsd_file_gc();
65294c1f2   Jeff Layton   nfsd: add a new s...
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
  
  	nf->nf_mark = nfsd_file_mark_find_or_create(nf);
  	if (nf->nf_mark)
  		status = nfsd_open_verified(rqstp, fhp, S_IFREG,
  				may_flags, &nf->nf_file);
  	else
  		status = nfserr_jukebox;
  	/*
  	 * If construction failed, or we raced with a call to unlink()
  	 * then unhash.
  	 */
  	if (status != nfs_ok || inode->i_nlink == 0) {
  		bool do_free;
  		spin_lock(&nfsd_file_hashtbl[hashval].nfb_lock);
  		do_free = nfsd_file_unhash(nf);
  		spin_unlock(&nfsd_file_hashtbl[hashval].nfb_lock);
  		if (do_free)
  			nfsd_file_put_noref(nf);
  	}
  	clear_bit_unlock(NFSD_FILE_PENDING, &nf->nf_flags);
  	smp_mb__after_atomic();
  	wake_up_bit(&nf->nf_flags, NFSD_FILE_PENDING);
  	goto out;
  }
  
  /*
   * Note that fields may be added, removed or reordered in the future. Programs
   * scraping this file for info should test the labels to ensure they're
   * getting the correct field.
   */
  static int nfsd_file_cache_stats_show(struct seq_file *m, void *v)
  {
  	unsigned int i, count = 0, longest = 0;
  	unsigned long hits = 0;
  
  	/*
  	 * No need for spinlocks here since we're not terribly interested in
  	 * accuracy. We do take the nfsd_mutex simply to ensure that we
  	 * don't end up racing with server shutdown
  	 */
  	mutex_lock(&nfsd_mutex);
  	if (nfsd_file_hashtbl) {
  		for (i = 0; i < NFSD_FILE_HASH_SIZE; i++) {
  			count += nfsd_file_hashtbl[i].nfb_count;
  			longest = max(longest, nfsd_file_hashtbl[i].nfb_count);
  		}
  	}
  	mutex_unlock(&nfsd_mutex);
  
  	for_each_possible_cpu(i)
  		hits += per_cpu(nfsd_file_cache_hits, i);
  
  	seq_printf(m, "total entries: %u
  ", count);
  	seq_printf(m, "longest chain: %u
  ", longest);
  	seq_printf(m, "cache hits:    %lu
  ", hits);
  	return 0;
  }
  
  int nfsd_file_cache_stats_open(struct inode *inode, struct file *file)
  {
  	return single_open(file, nfsd_file_cache_stats_show, NULL);
  }