nfsd: Schedule the laundrette regularly irrespective of file errors
[linux-2.6-microblaze.git] / fs / nfsd / filecache.c
1 /*
2  * Open file cache.
3  *
4  * (c) 2015 - Jeff Layton <jeff.layton@primarydata.com>
5  */
6
7 #include <linux/hash.h>
8 #include <linux/slab.h>
9 #include <linux/file.h>
10 #include <linux/sched.h>
11 #include <linux/list_lru.h>
12 #include <linux/fsnotify_backend.h>
13 #include <linux/fsnotify.h>
14 #include <linux/seq_file.h>
15
16 #include "vfs.h"
17 #include "nfsd.h"
18 #include "nfsfh.h"
19 #include "netns.h"
20 #include "filecache.h"
21 #include "trace.h"
22
23 #define NFSDDBG_FACILITY        NFSDDBG_FH
24
25 /* FIXME: dynamically size this for the machine somehow? */
26 #define NFSD_FILE_HASH_BITS                   12
27 #define NFSD_FILE_HASH_SIZE                  (1 << NFSD_FILE_HASH_BITS)
28 #define NFSD_LAUNDRETTE_DELAY                (2 * HZ)
29
30 #define NFSD_FILE_SHUTDOWN                   (1)
31 #define NFSD_FILE_LRU_THRESHOLD              (4096UL)
32 #define NFSD_FILE_LRU_LIMIT                  (NFSD_FILE_LRU_THRESHOLD << 2)
33
34 /* We only care about NFSD_MAY_READ/WRITE for this cache */
35 #define NFSD_FILE_MAY_MASK      (NFSD_MAY_READ|NFSD_MAY_WRITE)
36
37 struct nfsd_fcache_bucket {
38         struct hlist_head       nfb_head;
39         spinlock_t              nfb_lock;
40         unsigned int            nfb_count;
41         unsigned int            nfb_maxcount;
42 };
43
44 static DEFINE_PER_CPU(unsigned long, nfsd_file_cache_hits);
45
46 struct nfsd_fcache_disposal {
47         struct list_head list;
48         struct work_struct work;
49         struct net *net;
50         spinlock_t lock;
51         struct list_head freeme;
52         struct rcu_head rcu;
53 };
54
55 struct workqueue_struct *nfsd_filecache_wq __read_mostly;
56
57 static struct kmem_cache                *nfsd_file_slab;
58 static struct kmem_cache                *nfsd_file_mark_slab;
59 static struct nfsd_fcache_bucket        *nfsd_file_hashtbl;
60 static struct list_lru                  nfsd_file_lru;
61 static long                             nfsd_file_lru_flags;
62 static struct fsnotify_group            *nfsd_file_fsnotify_group;
63 static atomic_long_t                    nfsd_filecache_count;
64 static struct delayed_work              nfsd_filecache_laundrette;
65 static DEFINE_SPINLOCK(laundrette_lock);
66 static LIST_HEAD(laundrettes);
67
68 static void nfsd_file_gc(void);
69
70 static void
71 nfsd_file_schedule_laundrette(void)
72 {
73         long count = atomic_long_read(&nfsd_filecache_count);
74
75         if (count == 0 || test_bit(NFSD_FILE_SHUTDOWN, &nfsd_file_lru_flags))
76                 return;
77
78         queue_delayed_work(system_wq, &nfsd_filecache_laundrette,
79                         NFSD_LAUNDRETTE_DELAY);
80 }
81
82 static void
83 nfsd_file_slab_free(struct rcu_head *rcu)
84 {
85         struct nfsd_file *nf = container_of(rcu, struct nfsd_file, nf_rcu);
86
87         put_cred(nf->nf_cred);
88         kmem_cache_free(nfsd_file_slab, nf);
89 }
90
91 static void
92 nfsd_file_mark_free(struct fsnotify_mark *mark)
93 {
94         struct nfsd_file_mark *nfm = container_of(mark, struct nfsd_file_mark,
95                                                   nfm_mark);
96
97         kmem_cache_free(nfsd_file_mark_slab, nfm);
98 }
99
100 static struct nfsd_file_mark *
101 nfsd_file_mark_get(struct nfsd_file_mark *nfm)
102 {
103         if (!atomic_inc_not_zero(&nfm->nfm_ref))
104                 return NULL;
105         return nfm;
106 }
107
108 static void
109 nfsd_file_mark_put(struct nfsd_file_mark *nfm)
110 {
111         if (atomic_dec_and_test(&nfm->nfm_ref)) {
112
113                 fsnotify_destroy_mark(&nfm->nfm_mark, nfsd_file_fsnotify_group);
114                 fsnotify_put_mark(&nfm->nfm_mark);
115         }
116 }
117
118 static struct nfsd_file_mark *
119 nfsd_file_mark_find_or_create(struct nfsd_file *nf)
120 {
121         int                     err;
122         struct fsnotify_mark    *mark;
123         struct nfsd_file_mark   *nfm = NULL, *new;
124         struct inode *inode = nf->nf_inode;
125
126         do {
127                 mutex_lock(&nfsd_file_fsnotify_group->mark_mutex);
128                 mark = fsnotify_find_mark(&inode->i_fsnotify_marks,
129                                 nfsd_file_fsnotify_group);
130                 if (mark) {
131                         nfm = nfsd_file_mark_get(container_of(mark,
132                                                  struct nfsd_file_mark,
133                                                  nfm_mark));
134                         mutex_unlock(&nfsd_file_fsnotify_group->mark_mutex);
135                         fsnotify_put_mark(mark);
136                         if (likely(nfm))
137                                 break;
138                 } else
139                         mutex_unlock(&nfsd_file_fsnotify_group->mark_mutex);
140
141                 /* allocate a new nfm */
142                 new = kmem_cache_alloc(nfsd_file_mark_slab, GFP_KERNEL);
143                 if (!new)
144                         return NULL;
145                 fsnotify_init_mark(&new->nfm_mark, nfsd_file_fsnotify_group);
146                 new->nfm_mark.mask = FS_ATTRIB|FS_DELETE_SELF;
147                 atomic_set(&new->nfm_ref, 1);
148
149                 err = fsnotify_add_inode_mark(&new->nfm_mark, inode, 0);
150
151                 /*
152                  * If the add was successful, then return the object.
153                  * Otherwise, we need to put the reference we hold on the
154                  * nfm_mark. The fsnotify code will take a reference and put
155                  * it on failure, so we can't just free it directly. It's also
156                  * not safe to call fsnotify_destroy_mark on it as the
157                  * mark->group will be NULL. Thus, we can't let the nfm_ref
158                  * counter drive the destruction at this point.
159                  */
160                 if (likely(!err))
161                         nfm = new;
162                 else
163                         fsnotify_put_mark(&new->nfm_mark);
164         } while (unlikely(err == -EEXIST));
165
166         return nfm;
167 }
168
169 static struct nfsd_file *
170 nfsd_file_alloc(struct inode *inode, unsigned int may, unsigned int hashval,
171                 struct net *net)
172 {
173         struct nfsd_file *nf;
174
175         nf = kmem_cache_alloc(nfsd_file_slab, GFP_KERNEL);
176         if (nf) {
177                 INIT_HLIST_NODE(&nf->nf_node);
178                 INIT_LIST_HEAD(&nf->nf_lru);
179                 nf->nf_file = NULL;
180                 nf->nf_cred = get_current_cred();
181                 nf->nf_net = net;
182                 nf->nf_flags = 0;
183                 nf->nf_inode = inode;
184                 nf->nf_hashval = hashval;
185                 atomic_set(&nf->nf_ref, 1);
186                 nf->nf_may = may & NFSD_FILE_MAY_MASK;
187                 if (may & NFSD_MAY_NOT_BREAK_LEASE) {
188                         if (may & NFSD_MAY_WRITE)
189                                 __set_bit(NFSD_FILE_BREAK_WRITE, &nf->nf_flags);
190                         if (may & NFSD_MAY_READ)
191                                 __set_bit(NFSD_FILE_BREAK_READ, &nf->nf_flags);
192                 }
193                 nf->nf_mark = NULL;
194                 trace_nfsd_file_alloc(nf);
195         }
196         return nf;
197 }
198
199 static bool
200 nfsd_file_free(struct nfsd_file *nf)
201 {
202         bool flush = false;
203
204         trace_nfsd_file_put_final(nf);
205         if (nf->nf_mark)
206                 nfsd_file_mark_put(nf->nf_mark);
207         if (nf->nf_file) {
208                 get_file(nf->nf_file);
209                 filp_close(nf->nf_file, NULL);
210                 fput(nf->nf_file);
211                 flush = true;
212         }
213         call_rcu(&nf->nf_rcu, nfsd_file_slab_free);
214         return flush;
215 }
216
217 static bool
218 nfsd_file_check_writeback(struct nfsd_file *nf)
219 {
220         struct file *file = nf->nf_file;
221         struct address_space *mapping;
222
223         if (!file || !(file->f_mode & FMODE_WRITE))
224                 return false;
225         mapping = file->f_mapping;
226         return mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) ||
227                 mapping_tagged(mapping, PAGECACHE_TAG_WRITEBACK);
228 }
229
230 static int
231 nfsd_file_check_write_error(struct nfsd_file *nf)
232 {
233         struct file *file = nf->nf_file;
234
235         if (!file || !(file->f_mode & FMODE_WRITE))
236                 return 0;
237         return filemap_check_wb_err(file->f_mapping, READ_ONCE(file->f_wb_err));
238 }
239
240 static void
241 nfsd_file_do_unhash(struct nfsd_file *nf)
242 {
243         lockdep_assert_held(&nfsd_file_hashtbl[nf->nf_hashval].nfb_lock);
244
245         trace_nfsd_file_unhash(nf);
246
247         if (nfsd_file_check_write_error(nf))
248                 nfsd_reset_boot_verifier(net_generic(nf->nf_net, nfsd_net_id));
249         --nfsd_file_hashtbl[nf->nf_hashval].nfb_count;
250         hlist_del_rcu(&nf->nf_node);
251         atomic_long_dec(&nfsd_filecache_count);
252 }
253
254 static bool
255 nfsd_file_unhash(struct nfsd_file *nf)
256 {
257         if (test_and_clear_bit(NFSD_FILE_HASHED, &nf->nf_flags)) {
258                 nfsd_file_do_unhash(nf);
259                 if (!list_empty(&nf->nf_lru))
260                         list_lru_del(&nfsd_file_lru, &nf->nf_lru);
261                 return true;
262         }
263         return false;
264 }
265
266 /*
267  * Return true if the file was unhashed.
268  */
269 static bool
270 nfsd_file_unhash_and_release_locked(struct nfsd_file *nf, struct list_head *dispose)
271 {
272         lockdep_assert_held(&nfsd_file_hashtbl[nf->nf_hashval].nfb_lock);
273
274         trace_nfsd_file_unhash_and_release_locked(nf);
275         if (!nfsd_file_unhash(nf))
276                 return false;
277         /* keep final reference for nfsd_file_lru_dispose */
278         if (atomic_add_unless(&nf->nf_ref, -1, 1))
279                 return true;
280
281         list_add(&nf->nf_lru, dispose);
282         return true;
283 }
284
285 static int
286 nfsd_file_put_noref(struct nfsd_file *nf)
287 {
288         int count;
289         trace_nfsd_file_put(nf);
290
291         count = atomic_dec_return(&nf->nf_ref);
292         if (!count) {
293                 WARN_ON(test_bit(NFSD_FILE_HASHED, &nf->nf_flags));
294                 nfsd_file_free(nf);
295         }
296         return count;
297 }
298
299 void
300 nfsd_file_put(struct nfsd_file *nf)
301 {
302         bool is_hashed = test_bit(NFSD_FILE_HASHED, &nf->nf_flags) != 0;
303
304         set_bit(NFSD_FILE_REFERENCED, &nf->nf_flags);
305         if (nfsd_file_put_noref(nf) == 1 && is_hashed)
306                 nfsd_file_schedule_laundrette();
307         if (atomic_long_read(&nfsd_filecache_count) >= NFSD_FILE_LRU_LIMIT)
308                 nfsd_file_gc();
309 }
310
311 struct nfsd_file *
312 nfsd_file_get(struct nfsd_file *nf)
313 {
314         if (likely(atomic_inc_not_zero(&nf->nf_ref)))
315                 return nf;
316         return NULL;
317 }
318
319 static void
320 nfsd_file_dispose_list(struct list_head *dispose)
321 {
322         struct nfsd_file *nf;
323
324         while(!list_empty(dispose)) {
325                 nf = list_first_entry(dispose, struct nfsd_file, nf_lru);
326                 list_del(&nf->nf_lru);
327                 nfsd_file_put_noref(nf);
328         }
329 }
330
331 static void
332 nfsd_file_dispose_list_sync(struct list_head *dispose)
333 {
334         bool flush = false;
335         struct nfsd_file *nf;
336
337         while(!list_empty(dispose)) {
338                 nf = list_first_entry(dispose, struct nfsd_file, nf_lru);
339                 list_del(&nf->nf_lru);
340                 if (!atomic_dec_and_test(&nf->nf_ref))
341                         continue;
342                 if (nfsd_file_free(nf))
343                         flush = true;
344         }
345         if (flush)
346                 flush_delayed_fput();
347 }
348
349 static void
350 nfsd_file_list_remove_disposal(struct list_head *dst,
351                 struct nfsd_fcache_disposal *l)
352 {
353         spin_lock(&l->lock);
354         list_splice_init(&l->freeme, dst);
355         spin_unlock(&l->lock);
356 }
357
358 static void
359 nfsd_file_list_add_disposal(struct list_head *files, struct net *net)
360 {
361         struct nfsd_fcache_disposal *l;
362
363         rcu_read_lock();
364         list_for_each_entry_rcu(l, &laundrettes, list) {
365                 if (l->net == net) {
366                         spin_lock(&l->lock);
367                         list_splice_tail_init(files, &l->freeme);
368                         spin_unlock(&l->lock);
369                         queue_work(nfsd_filecache_wq, &l->work);
370                         break;
371                 }
372         }
373         rcu_read_unlock();
374 }
375
376 static void
377 nfsd_file_list_add_pernet(struct list_head *dst, struct list_head *src,
378                 struct net *net)
379 {
380         struct nfsd_file *nf, *tmp;
381
382         list_for_each_entry_safe(nf, tmp, src, nf_lru) {
383                 if (nf->nf_net == net)
384                         list_move_tail(&nf->nf_lru, dst);
385         }
386 }
387
388 static void
389 nfsd_file_dispose_list_delayed(struct list_head *dispose)
390 {
391         LIST_HEAD(list);
392         struct nfsd_file *nf;
393
394         while(!list_empty(dispose)) {
395                 nf = list_first_entry(dispose, struct nfsd_file, nf_lru);
396                 nfsd_file_list_add_pernet(&list, dispose, nf->nf_net);
397                 nfsd_file_list_add_disposal(&list, nf->nf_net);
398         }
399 }
400
401 /*
402  * Note this can deadlock with nfsd_file_cache_purge.
403  */
404 static enum lru_status
405 nfsd_file_lru_cb(struct list_head *item, struct list_lru_one *lru,
406                  spinlock_t *lock, void *arg)
407         __releases(lock)
408         __acquires(lock)
409 {
410         struct list_head *head = arg;
411         struct nfsd_file *nf = list_entry(item, struct nfsd_file, nf_lru);
412
413         /*
414          * Do a lockless refcount check. The hashtable holds one reference, so
415          * we look to see if anything else has a reference, or if any have
416          * been put since the shrinker last ran. Those don't get unhashed and
417          * released.
418          *
419          * Note that in the put path, we set the flag and then decrement the
420          * counter. Here we check the counter and then test and clear the flag.
421          * That order is deliberate to ensure that we can do this locklessly.
422          */
423         if (atomic_read(&nf->nf_ref) > 1)
424                 goto out_skip;
425
426         /*
427          * Don't throw out files that are still undergoing I/O or
428          * that have uncleared errors pending.
429          */
430         if (nfsd_file_check_writeback(nf))
431                 goto out_skip;
432
433         if (test_and_clear_bit(NFSD_FILE_REFERENCED, &nf->nf_flags))
434                 goto out_skip;
435
436         if (!test_and_clear_bit(NFSD_FILE_HASHED, &nf->nf_flags))
437                 goto out_skip;
438
439         list_lru_isolate_move(lru, &nf->nf_lru, head);
440         return LRU_REMOVED;
441 out_skip:
442         return LRU_SKIP;
443 }
444
445 static unsigned long
446 nfsd_file_lru_walk_list(struct shrink_control *sc)
447 {
448         LIST_HEAD(head);
449         struct nfsd_file *nf;
450         unsigned long ret;
451
452         if (sc)
453                 ret = list_lru_shrink_walk(&nfsd_file_lru, sc,
454                                 nfsd_file_lru_cb, &head);
455         else
456                 ret = list_lru_walk(&nfsd_file_lru,
457                                 nfsd_file_lru_cb,
458                                 &head, LONG_MAX);
459         list_for_each_entry(nf, &head, nf_lru) {
460                 spin_lock(&nfsd_file_hashtbl[nf->nf_hashval].nfb_lock);
461                 nfsd_file_do_unhash(nf);
462                 spin_unlock(&nfsd_file_hashtbl[nf->nf_hashval].nfb_lock);
463         }
464         nfsd_file_dispose_list_delayed(&head);
465         return ret;
466 }
467
468 static void
469 nfsd_file_gc(void)
470 {
471         nfsd_file_lru_walk_list(NULL);
472 }
473
474 static void
475 nfsd_file_gc_worker(struct work_struct *work)
476 {
477         nfsd_file_gc();
478         nfsd_file_schedule_laundrette();
479 }
480
481 static unsigned long
482 nfsd_file_lru_count(struct shrinker *s, struct shrink_control *sc)
483 {
484         return list_lru_count(&nfsd_file_lru);
485 }
486
487 static unsigned long
488 nfsd_file_lru_scan(struct shrinker *s, struct shrink_control *sc)
489 {
490         return nfsd_file_lru_walk_list(sc);
491 }
492
493 static struct shrinker  nfsd_file_shrinker = {
494         .scan_objects = nfsd_file_lru_scan,
495         .count_objects = nfsd_file_lru_count,
496         .seeks = 1,
497 };
498
499 static void
500 __nfsd_file_close_inode(struct inode *inode, unsigned int hashval,
501                         struct list_head *dispose)
502 {
503         struct nfsd_file        *nf;
504         struct hlist_node       *tmp;
505
506         spin_lock(&nfsd_file_hashtbl[hashval].nfb_lock);
507         hlist_for_each_entry_safe(nf, tmp, &nfsd_file_hashtbl[hashval].nfb_head, nf_node) {
508                 if (inode == nf->nf_inode)
509                         nfsd_file_unhash_and_release_locked(nf, dispose);
510         }
511         spin_unlock(&nfsd_file_hashtbl[hashval].nfb_lock);
512 }
513
514 /**
515  * nfsd_file_close_inode_sync - attempt to forcibly close a nfsd_file
516  * @inode: inode of the file to attempt to remove
517  *
518  * Walk the whole hash bucket, looking for any files that correspond to "inode".
519  * If any do, then unhash them and put the hashtable reference to them and
520  * destroy any that had their last reference put. Also ensure that any of the
521  * fputs also have their final __fput done as well.
522  */
523 void
524 nfsd_file_close_inode_sync(struct inode *inode)
525 {
526         unsigned int            hashval = (unsigned int)hash_long(inode->i_ino,
527                                                 NFSD_FILE_HASH_BITS);
528         LIST_HEAD(dispose);
529
530         __nfsd_file_close_inode(inode, hashval, &dispose);
531         trace_nfsd_file_close_inode_sync(inode, hashval, !list_empty(&dispose));
532         nfsd_file_dispose_list_sync(&dispose);
533 }
534
535 /**
536  * nfsd_file_close_inode_sync - attempt to forcibly close a nfsd_file
537  * @inode: inode of the file to attempt to remove
538  *
539  * Walk the whole hash bucket, looking for any files that correspond to "inode".
540  * If any do, then unhash them and put the hashtable reference to them and
541  * destroy any that had their last reference put.
542  */
543 static void
544 nfsd_file_close_inode(struct inode *inode)
545 {
546         unsigned int            hashval = (unsigned int)hash_long(inode->i_ino,
547                                                 NFSD_FILE_HASH_BITS);
548         LIST_HEAD(dispose);
549
550         __nfsd_file_close_inode(inode, hashval, &dispose);
551         trace_nfsd_file_close_inode(inode, hashval, !list_empty(&dispose));
552         nfsd_file_dispose_list_delayed(&dispose);
553 }
554
555 /**
556  * nfsd_file_delayed_close - close unused nfsd_files
557  * @work: dummy
558  *
559  * Walk the LRU list and close any entries that have not been used since
560  * the last scan.
561  *
562  * Note this can deadlock with nfsd_file_cache_purge.
563  */
564 static void
565 nfsd_file_delayed_close(struct work_struct *work)
566 {
567         LIST_HEAD(head);
568         struct nfsd_fcache_disposal *l = container_of(work,
569                         struct nfsd_fcache_disposal, work);
570
571         nfsd_file_list_remove_disposal(&head, l);
572         nfsd_file_dispose_list(&head);
573 }
574
575 static int
576 nfsd_file_lease_notifier_call(struct notifier_block *nb, unsigned long arg,
577                             void *data)
578 {
579         struct file_lock *fl = data;
580
581         /* Only close files for F_SETLEASE leases */
582         if (fl->fl_flags & FL_LEASE)
583                 nfsd_file_close_inode_sync(file_inode(fl->fl_file));
584         return 0;
585 }
586
587 static struct notifier_block nfsd_file_lease_notifier = {
588         .notifier_call = nfsd_file_lease_notifier_call,
589 };
590
591 static int
592 nfsd_file_fsnotify_handle_event(struct fsnotify_group *group,
593                                 struct inode *inode,
594                                 u32 mask, const void *data, int data_type,
595                                 const struct qstr *file_name, u32 cookie,
596                                 struct fsnotify_iter_info *iter_info)
597 {
598         trace_nfsd_file_fsnotify_handle_event(inode, mask);
599
600         /* Should be no marks on non-regular files */
601         if (!S_ISREG(inode->i_mode)) {
602                 WARN_ON_ONCE(1);
603                 return 0;
604         }
605
606         /* don't close files if this was not the last link */
607         if (mask & FS_ATTRIB) {
608                 if (inode->i_nlink)
609                         return 0;
610         }
611
612         nfsd_file_close_inode(inode);
613         return 0;
614 }
615
616
617 static const struct fsnotify_ops nfsd_file_fsnotify_ops = {
618         .handle_event = nfsd_file_fsnotify_handle_event,
619         .free_mark = nfsd_file_mark_free,
620 };
621
622 int
623 nfsd_file_cache_init(void)
624 {
625         int             ret = -ENOMEM;
626         unsigned int    i;
627
628         clear_bit(NFSD_FILE_SHUTDOWN, &nfsd_file_lru_flags);
629
630         if (nfsd_file_hashtbl)
631                 return 0;
632
633         nfsd_filecache_wq = alloc_workqueue("nfsd_filecache", 0, 0);
634         if (!nfsd_filecache_wq)
635                 goto out;
636
637         nfsd_file_hashtbl = kcalloc(NFSD_FILE_HASH_SIZE,
638                                 sizeof(*nfsd_file_hashtbl), GFP_KERNEL);
639         if (!nfsd_file_hashtbl) {
640                 pr_err("nfsd: unable to allocate nfsd_file_hashtbl\n");
641                 goto out_err;
642         }
643
644         nfsd_file_slab = kmem_cache_create("nfsd_file",
645                                 sizeof(struct nfsd_file), 0, 0, NULL);
646         if (!nfsd_file_slab) {
647                 pr_err("nfsd: unable to create nfsd_file_slab\n");
648                 goto out_err;
649         }
650
651         nfsd_file_mark_slab = kmem_cache_create("nfsd_file_mark",
652                                         sizeof(struct nfsd_file_mark), 0, 0, NULL);
653         if (!nfsd_file_mark_slab) {
654                 pr_err("nfsd: unable to create nfsd_file_mark_slab\n");
655                 goto out_err;
656         }
657
658
659         ret = list_lru_init(&nfsd_file_lru);
660         if (ret) {
661                 pr_err("nfsd: failed to init nfsd_file_lru: %d\n", ret);
662                 goto out_err;
663         }
664
665         ret = register_shrinker(&nfsd_file_shrinker);
666         if (ret) {
667                 pr_err("nfsd: failed to register nfsd_file_shrinker: %d\n", ret);
668                 goto out_lru;
669         }
670
671         ret = lease_register_notifier(&nfsd_file_lease_notifier);
672         if (ret) {
673                 pr_err("nfsd: unable to register lease notifier: %d\n", ret);
674                 goto out_shrinker;
675         }
676
677         nfsd_file_fsnotify_group = fsnotify_alloc_group(&nfsd_file_fsnotify_ops);
678         if (IS_ERR(nfsd_file_fsnotify_group)) {
679                 pr_err("nfsd: unable to create fsnotify group: %ld\n",
680                         PTR_ERR(nfsd_file_fsnotify_group));
681                 nfsd_file_fsnotify_group = NULL;
682                 goto out_notifier;
683         }
684
685         for (i = 0; i < NFSD_FILE_HASH_SIZE; i++) {
686                 INIT_HLIST_HEAD(&nfsd_file_hashtbl[i].nfb_head);
687                 spin_lock_init(&nfsd_file_hashtbl[i].nfb_lock);
688         }
689
690         INIT_DELAYED_WORK(&nfsd_filecache_laundrette, nfsd_file_gc_worker);
691 out:
692         return ret;
693 out_notifier:
694         lease_unregister_notifier(&nfsd_file_lease_notifier);
695 out_shrinker:
696         unregister_shrinker(&nfsd_file_shrinker);
697 out_lru:
698         list_lru_destroy(&nfsd_file_lru);
699 out_err:
700         kmem_cache_destroy(nfsd_file_slab);
701         nfsd_file_slab = NULL;
702         kmem_cache_destroy(nfsd_file_mark_slab);
703         nfsd_file_mark_slab = NULL;
704         kfree(nfsd_file_hashtbl);
705         nfsd_file_hashtbl = NULL;
706         destroy_workqueue(nfsd_filecache_wq);
707         nfsd_filecache_wq = NULL;
708         goto out;
709 }
710
711 /*
712  * Note this can deadlock with nfsd_file_lru_cb.
713  */
714 void
715 nfsd_file_cache_purge(struct net *net)
716 {
717         unsigned int            i;
718         struct nfsd_file        *nf;
719         struct hlist_node       *next;
720         LIST_HEAD(dispose);
721         bool del;
722
723         if (!nfsd_file_hashtbl)
724                 return;
725
726         for (i = 0; i < NFSD_FILE_HASH_SIZE; i++) {
727                 struct nfsd_fcache_bucket *nfb = &nfsd_file_hashtbl[i];
728
729                 spin_lock(&nfb->nfb_lock);
730                 hlist_for_each_entry_safe(nf, next, &nfb->nfb_head, nf_node) {
731                         if (net && nf->nf_net != net)
732                                 continue;
733                         del = nfsd_file_unhash_and_release_locked(nf, &dispose);
734
735                         /*
736                          * Deadlock detected! Something marked this entry as
737                          * unhased, but hasn't removed it from the hash list.
738                          */
739                         WARN_ON_ONCE(!del);
740                 }
741                 spin_unlock(&nfb->nfb_lock);
742                 nfsd_file_dispose_list(&dispose);
743         }
744 }
745
746 static struct nfsd_fcache_disposal *
747 nfsd_alloc_fcache_disposal(struct net *net)
748 {
749         struct nfsd_fcache_disposal *l;
750
751         l = kmalloc(sizeof(*l), GFP_KERNEL);
752         if (!l)
753                 return NULL;
754         INIT_WORK(&l->work, nfsd_file_delayed_close);
755         l->net = net;
756         spin_lock_init(&l->lock);
757         INIT_LIST_HEAD(&l->freeme);
758         return l;
759 }
760
761 static void
762 nfsd_free_fcache_disposal(struct nfsd_fcache_disposal *l)
763 {
764         rcu_assign_pointer(l->net, NULL);
765         cancel_work_sync(&l->work);
766         nfsd_file_dispose_list(&l->freeme);
767         kfree_rcu(l, rcu);
768 }
769
770 static void
771 nfsd_add_fcache_disposal(struct nfsd_fcache_disposal *l)
772 {
773         spin_lock(&laundrette_lock);
774         list_add_tail_rcu(&l->list, &laundrettes);
775         spin_unlock(&laundrette_lock);
776 }
777
778 static void
779 nfsd_del_fcache_disposal(struct nfsd_fcache_disposal *l)
780 {
781         spin_lock(&laundrette_lock);
782         list_del_rcu(&l->list);
783         spin_unlock(&laundrette_lock);
784 }
785
786 static int
787 nfsd_alloc_fcache_disposal_net(struct net *net)
788 {
789         struct nfsd_fcache_disposal *l;
790
791         l = nfsd_alloc_fcache_disposal(net);
792         if (!l)
793                 return -ENOMEM;
794         nfsd_add_fcache_disposal(l);
795         return 0;
796 }
797
798 static void
799 nfsd_free_fcache_disposal_net(struct net *net)
800 {
801         struct nfsd_fcache_disposal *l;
802
803         rcu_read_lock();
804         list_for_each_entry_rcu(l, &laundrettes, list) {
805                 if (l->net != net)
806                         continue;
807                 nfsd_del_fcache_disposal(l);
808                 rcu_read_unlock();
809                 nfsd_free_fcache_disposal(l);
810                 return;
811         }
812         rcu_read_unlock();
813 }
814
815 int
816 nfsd_file_cache_start_net(struct net *net)
817 {
818         return nfsd_alloc_fcache_disposal_net(net);
819 }
820
821 void
822 nfsd_file_cache_shutdown_net(struct net *net)
823 {
824         nfsd_file_cache_purge(net);
825         nfsd_free_fcache_disposal_net(net);
826 }
827
828 void
829 nfsd_file_cache_shutdown(void)
830 {
831         set_bit(NFSD_FILE_SHUTDOWN, &nfsd_file_lru_flags);
832
833         lease_unregister_notifier(&nfsd_file_lease_notifier);
834         unregister_shrinker(&nfsd_file_shrinker);
835         /*
836          * make sure all callers of nfsd_file_lru_cb are done before
837          * calling nfsd_file_cache_purge
838          */
839         cancel_delayed_work_sync(&nfsd_filecache_laundrette);
840         nfsd_file_cache_purge(NULL);
841         list_lru_destroy(&nfsd_file_lru);
842         rcu_barrier();
843         fsnotify_put_group(nfsd_file_fsnotify_group);
844         nfsd_file_fsnotify_group = NULL;
845         kmem_cache_destroy(nfsd_file_slab);
846         nfsd_file_slab = NULL;
847         fsnotify_wait_marks_destroyed();
848         kmem_cache_destroy(nfsd_file_mark_slab);
849         nfsd_file_mark_slab = NULL;
850         kfree(nfsd_file_hashtbl);
851         nfsd_file_hashtbl = NULL;
852         destroy_workqueue(nfsd_filecache_wq);
853         nfsd_filecache_wq = NULL;
854 }
855
856 static bool
857 nfsd_match_cred(const struct cred *c1, const struct cred *c2)
858 {
859         int i;
860
861         if (!uid_eq(c1->fsuid, c2->fsuid))
862                 return false;
863         if (!gid_eq(c1->fsgid, c2->fsgid))
864                 return false;
865         if (c1->group_info == NULL || c2->group_info == NULL)
866                 return c1->group_info == c2->group_info;
867         if (c1->group_info->ngroups != c2->group_info->ngroups)
868                 return false;
869         for (i = 0; i < c1->group_info->ngroups; i++) {
870                 if (!gid_eq(c1->group_info->gid[i], c2->group_info->gid[i]))
871                         return false;
872         }
873         return true;
874 }
875
876 static struct nfsd_file *
877 nfsd_file_find_locked(struct inode *inode, unsigned int may_flags,
878                         unsigned int hashval, struct net *net)
879 {
880         struct nfsd_file *nf;
881         unsigned char need = may_flags & NFSD_FILE_MAY_MASK;
882
883         hlist_for_each_entry_rcu(nf, &nfsd_file_hashtbl[hashval].nfb_head,
884                                  nf_node) {
885                 if ((need & nf->nf_may) != need)
886                         continue;
887                 if (nf->nf_inode != inode)
888                         continue;
889                 if (nf->nf_net != net)
890                         continue;
891                 if (!nfsd_match_cred(nf->nf_cred, current_cred()))
892                         continue;
893                 if (nfsd_file_get(nf) != NULL)
894                         return nf;
895         }
896         return NULL;
897 }
898
899 /**
900  * nfsd_file_is_cached - are there any cached open files for this fh?
901  * @inode: inode of the file to check
902  *
903  * Scan the hashtable for open files that match this fh. Returns true if there
904  * are any, and false if not.
905  */
906 bool
907 nfsd_file_is_cached(struct inode *inode)
908 {
909         bool                    ret = false;
910         struct nfsd_file        *nf;
911         unsigned int            hashval;
912
913         hashval = (unsigned int)hash_long(inode->i_ino, NFSD_FILE_HASH_BITS);
914
915         rcu_read_lock();
916         hlist_for_each_entry_rcu(nf, &nfsd_file_hashtbl[hashval].nfb_head,
917                                  nf_node) {
918                 if (inode == nf->nf_inode) {
919                         ret = true;
920                         break;
921                 }
922         }
923         rcu_read_unlock();
924         trace_nfsd_file_is_cached(inode, hashval, (int)ret);
925         return ret;
926 }
927
928 __be32
929 nfsd_file_acquire(struct svc_rqst *rqstp, struct svc_fh *fhp,
930                   unsigned int may_flags, struct nfsd_file **pnf)
931 {
932         __be32  status;
933         struct net *net = SVC_NET(rqstp);
934         struct nfsd_file *nf, *new;
935         struct inode *inode;
936         unsigned int hashval;
937         bool retry = true;
938
939         /* FIXME: skip this if fh_dentry is already set? */
940         status = fh_verify(rqstp, fhp, S_IFREG,
941                                 may_flags|NFSD_MAY_OWNER_OVERRIDE);
942         if (status != nfs_ok)
943                 return status;
944
945         inode = d_inode(fhp->fh_dentry);
946         hashval = (unsigned int)hash_long(inode->i_ino, NFSD_FILE_HASH_BITS);
947 retry:
948         rcu_read_lock();
949         nf = nfsd_file_find_locked(inode, may_flags, hashval, net);
950         rcu_read_unlock();
951         if (nf)
952                 goto wait_for_construction;
953
954         new = nfsd_file_alloc(inode, may_flags, hashval, net);
955         if (!new) {
956                 trace_nfsd_file_acquire(rqstp, hashval, inode, may_flags,
957                                         NULL, nfserr_jukebox);
958                 return nfserr_jukebox;
959         }
960
961         spin_lock(&nfsd_file_hashtbl[hashval].nfb_lock);
962         nf = nfsd_file_find_locked(inode, may_flags, hashval, net);
963         if (nf == NULL)
964                 goto open_file;
965         spin_unlock(&nfsd_file_hashtbl[hashval].nfb_lock);
966         nfsd_file_slab_free(&new->nf_rcu);
967
968 wait_for_construction:
969         wait_on_bit(&nf->nf_flags, NFSD_FILE_PENDING, TASK_UNINTERRUPTIBLE);
970
971         /* Did construction of this file fail? */
972         if (!test_bit(NFSD_FILE_HASHED, &nf->nf_flags)) {
973                 if (!retry) {
974                         status = nfserr_jukebox;
975                         goto out;
976                 }
977                 retry = false;
978                 nfsd_file_put_noref(nf);
979                 goto retry;
980         }
981
982         this_cpu_inc(nfsd_file_cache_hits);
983
984         if (!(may_flags & NFSD_MAY_NOT_BREAK_LEASE)) {
985                 bool write = (may_flags & NFSD_MAY_WRITE);
986
987                 if (test_bit(NFSD_FILE_BREAK_READ, &nf->nf_flags) ||
988                     (test_bit(NFSD_FILE_BREAK_WRITE, &nf->nf_flags) && write)) {
989                         status = nfserrno(nfsd_open_break_lease(
990                                         file_inode(nf->nf_file), may_flags));
991                         if (status == nfs_ok) {
992                                 clear_bit(NFSD_FILE_BREAK_READ, &nf->nf_flags);
993                                 if (write)
994                                         clear_bit(NFSD_FILE_BREAK_WRITE,
995                                                   &nf->nf_flags);
996                         }
997                 }
998         }
999 out:
1000         if (status == nfs_ok) {
1001                 *pnf = nf;
1002         } else {
1003                 nfsd_file_put(nf);
1004                 nf = NULL;
1005         }
1006
1007         trace_nfsd_file_acquire(rqstp, hashval, inode, may_flags, nf, status);
1008         return status;
1009 open_file:
1010         nf = new;
1011         /* Take reference for the hashtable */
1012         atomic_inc(&nf->nf_ref);
1013         __set_bit(NFSD_FILE_HASHED, &nf->nf_flags);
1014         __set_bit(NFSD_FILE_PENDING, &nf->nf_flags);
1015         list_lru_add(&nfsd_file_lru, &nf->nf_lru);
1016         hlist_add_head_rcu(&nf->nf_node, &nfsd_file_hashtbl[hashval].nfb_head);
1017         ++nfsd_file_hashtbl[hashval].nfb_count;
1018         nfsd_file_hashtbl[hashval].nfb_maxcount = max(nfsd_file_hashtbl[hashval].nfb_maxcount,
1019                         nfsd_file_hashtbl[hashval].nfb_count);
1020         spin_unlock(&nfsd_file_hashtbl[hashval].nfb_lock);
1021         if (atomic_long_inc_return(&nfsd_filecache_count) >= NFSD_FILE_LRU_THRESHOLD)
1022                 nfsd_file_gc();
1023
1024         nf->nf_mark = nfsd_file_mark_find_or_create(nf);
1025         if (nf->nf_mark)
1026                 status = nfsd_open_verified(rqstp, fhp, S_IFREG,
1027                                 may_flags, &nf->nf_file);
1028         else
1029                 status = nfserr_jukebox;
1030         /*
1031          * If construction failed, or we raced with a call to unlink()
1032          * then unhash.
1033          */
1034         if (status != nfs_ok || inode->i_nlink == 0) {
1035                 bool do_free;
1036                 spin_lock(&nfsd_file_hashtbl[hashval].nfb_lock);
1037                 do_free = nfsd_file_unhash(nf);
1038                 spin_unlock(&nfsd_file_hashtbl[hashval].nfb_lock);
1039                 if (do_free)
1040                         nfsd_file_put_noref(nf);
1041         }
1042         clear_bit_unlock(NFSD_FILE_PENDING, &nf->nf_flags);
1043         smp_mb__after_atomic();
1044         wake_up_bit(&nf->nf_flags, NFSD_FILE_PENDING);
1045         goto out;
1046 }
1047
1048 /*
1049  * Note that fields may be added, removed or reordered in the future. Programs
1050  * scraping this file for info should test the labels to ensure they're
1051  * getting the correct field.
1052  */
1053 static int nfsd_file_cache_stats_show(struct seq_file *m, void *v)
1054 {
1055         unsigned int i, count = 0, longest = 0;
1056         unsigned long hits = 0;
1057
1058         /*
1059          * No need for spinlocks here since we're not terribly interested in
1060          * accuracy. We do take the nfsd_mutex simply to ensure that we
1061          * don't end up racing with server shutdown
1062          */
1063         mutex_lock(&nfsd_mutex);
1064         if (nfsd_file_hashtbl) {
1065                 for (i = 0; i < NFSD_FILE_HASH_SIZE; i++) {
1066                         count += nfsd_file_hashtbl[i].nfb_count;
1067                         longest = max(longest, nfsd_file_hashtbl[i].nfb_count);
1068                 }
1069         }
1070         mutex_unlock(&nfsd_mutex);
1071
1072         for_each_possible_cpu(i)
1073                 hits += per_cpu(nfsd_file_cache_hits, i);
1074
1075         seq_printf(m, "total entries: %u\n", count);
1076         seq_printf(m, "longest chain: %u\n", longest);
1077         seq_printf(m, "cache hits:    %lu\n", hits);
1078         return 0;
1079 }
1080
1081 int nfsd_file_cache_stats_open(struct inode *inode, struct file *file)
1082 {
1083         return single_open(file, nfsd_file_cache_stats_show, NULL);
1084 }