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