1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Functions to handle the cached directory entries
4 *
5 * Copyright (c) 2022, Ronnie Sahlberg <lsahlber@redhat.com>
6 */
7
8 #include <linux/namei.h>
9 #include "cifsglob.h"
10 #include "cifsproto.h"
11 #include "cifs_debug.h"
12 #include "smb2proto.h"
13 #include "cached_dir.h"
14
15 static struct cached_fid *init_cached_dir(const char *path);
16 static void free_cached_dir(struct cached_fid *cfid);
17 static void smb2_close_cached_fid(struct kref *ref);
18 static void cfids_laundromat_worker(struct work_struct *work);
19 static void close_cached_dir_locked(struct cached_fid *cfid);
20
21 struct cached_dir_dentry {
22 struct list_head entry;
23 struct dentry *dentry;
24 };
25
find_or_create_cached_dir(struct cached_fids * cfids,const char * path,bool lookup_only,__u32 max_cached_dirs)26 static struct cached_fid *find_or_create_cached_dir(struct cached_fids *cfids,
27 const char *path,
28 bool lookup_only,
29 __u32 max_cached_dirs)
30 {
31 struct cached_fid *cfid;
32
33 list_for_each_entry(cfid, &cfids->entries, entry) {
34 if (!strcmp(cfid->path, path)) {
35 /*
36 * If it doesn't have a lease it is either not yet
37 * fully cached or it may be in the process of
38 * being deleted due to a lease break.
39 */
40 if (!is_valid_cached_dir(cfid))
41 return NULL;
42 kref_get(&cfid->refcount);
43 return cfid;
44 }
45 }
46 if (lookup_only) {
47 return NULL;
48 }
49 if (cfids->num_entries >= max_cached_dirs) {
50 return NULL;
51 }
52 cfid = init_cached_dir(path);
53 if (cfid == NULL) {
54 return NULL;
55 }
56 cfid->cfids = cfids;
57 cfids->num_entries++;
58 list_add(&cfid->entry, &cfids->entries);
59 cfid->on_list = true;
60 kref_get(&cfid->refcount);
61 /*
62 * Set @cfid->has_lease to true during construction so that the lease
63 * reference can be put in cached_dir_lease_break() due to a potential
64 * lease break right after the request is sent or while @cfid is still
65 * being cached, or if a reconnection is triggered during construction.
66 * Concurrent processes won't be to use it yet due to @cfid->time being
67 * zero.
68 */
69 cfid->has_lease = true;
70
71 return cfid;
72 }
73
74 static struct dentry *
path_to_dentry(struct cifs_sb_info * cifs_sb,const char * path)75 path_to_dentry(struct cifs_sb_info *cifs_sb, const char *path)
76 {
77 struct dentry *dentry;
78 const char *s, *p;
79 char sep;
80
81 sep = CIFS_DIR_SEP(cifs_sb);
82 dentry = dget(cifs_sb->root);
83 s = path;
84
85 do {
86 struct inode *dir = d_inode(dentry);
87 struct dentry *child;
88
89 if (!S_ISDIR(dir->i_mode)) {
90 dput(dentry);
91 dentry = ERR_PTR(-ENOTDIR);
92 break;
93 }
94
95 /* skip separators */
96 while (*s == sep)
97 s++;
98 if (!*s)
99 break;
100 p = s++;
101 /* next separator */
102 while (*s && *s != sep)
103 s++;
104
105 child = lookup_noperm_positive_unlocked(&QSTR_LEN(p, s - p),
106 dentry);
107 dput(dentry);
108 dentry = child;
109 } while (!IS_ERR(dentry));
110 return dentry;
111 }
112
path_no_prefix(struct cifs_sb_info * cifs_sb,const char * path)113 static const char *path_no_prefix(struct cifs_sb_info *cifs_sb,
114 const char *path)
115 {
116 size_t len = 0;
117
118 if (!*path)
119 return path;
120
121 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) &&
122 cifs_sb->prepath) {
123 len = strlen(cifs_sb->prepath) + 1;
124 if (unlikely(len > strlen(path)))
125 return ERR_PTR(-EINVAL);
126 }
127 return path + len;
128 }
129
130 /*
131 * Open the and cache a directory handle.
132 * If error then *cfid is not initialized.
133 */
open_cached_dir(unsigned int xid,struct cifs_tcon * tcon,const char * path,struct cifs_sb_info * cifs_sb,bool lookup_only,struct cached_fid ** ret_cfid)134 int open_cached_dir(unsigned int xid, struct cifs_tcon *tcon,
135 const char *path,
136 struct cifs_sb_info *cifs_sb,
137 bool lookup_only, struct cached_fid **ret_cfid)
138 {
139 struct cifs_ses *ses;
140 struct TCP_Server_Info *server;
141 struct cifs_open_parms oparms;
142 struct smb2_create_rsp *o_rsp = NULL;
143 struct smb2_query_info_rsp *qi_rsp = NULL;
144 int resp_buftype[2];
145 struct smb_rqst rqst[2];
146 struct kvec rsp_iov[2];
147 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
148 struct kvec qi_iov[1];
149 int rc, flags = 0;
150 __le16 *utf16_path = NULL;
151 u8 oplock = SMB2_OPLOCK_LEVEL_II;
152 struct cifs_fid *pfid;
153 struct dentry *dentry = NULL;
154 struct cached_fid *cfid;
155 struct cached_fids *cfids;
156 const char *npath;
157 int retries = 0, cur_sleep = 0;
158 __le32 lease_flags = 0;
159
160 if (cifs_sb->root == NULL)
161 return -ENOENT;
162
163 if (tcon == NULL)
164 return -EOPNOTSUPP;
165
166 ses = tcon->ses;
167 cfids = tcon->cfids;
168
169 if (cfids == NULL)
170 return -EOPNOTSUPP;
171
172 replay_again:
173 /* reinitialize for possible replay */
174 flags = 0;
175 oplock = SMB2_OPLOCK_LEVEL_II;
176 server = cifs_pick_channel(ses);
177
178 if (!server->ops->new_lease_key)
179 return smb_EIO(smb_eio_trace_no_lease_key);
180
181 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
182 if (!utf16_path)
183 return -ENOMEM;
184
185 spin_lock(&cfids->cfid_list_lock);
186 cfid = find_or_create_cached_dir(cfids, path, lookup_only, tcon->max_cached_dirs);
187 if (cfid == NULL) {
188 spin_unlock(&cfids->cfid_list_lock);
189 kfree(utf16_path);
190 return -ENOENT;
191 }
192 /*
193 * Return cached fid if it is valid (has a lease and has a time).
194 * Otherwise, it is either a new entry or laundromat worker removed it
195 * from @cfids->entries. Caller will put last reference if the latter.
196 */
197 if (is_valid_cached_dir(cfid)) {
198 cfid->last_access_time = jiffies;
199 spin_unlock(&cfids->cfid_list_lock);
200 *ret_cfid = cfid;
201 kfree(utf16_path);
202 return 0;
203 }
204 spin_unlock(&cfids->cfid_list_lock);
205
206 pfid = &cfid->fid;
207
208 /*
209 * Skip any prefix paths in @path as lookup_noperm_positive_unlocked() ends up
210 * calling ->lookup() which already adds those through
211 * build_path_from_dentry(). Also, do it earlier as we might reconnect
212 * below when trying to send compounded request and then potentially
213 * having a different prefix path (e.g. after DFS failover).
214 */
215 npath = path_no_prefix(cifs_sb, path);
216 if (IS_ERR(npath)) {
217 rc = PTR_ERR(npath);
218 goto out;
219 }
220
221 if (!npath[0]) {
222 dentry = dget(cifs_sb->root);
223 } else {
224 dentry = path_to_dentry(cifs_sb, npath);
225 if (IS_ERR(dentry)) {
226 rc = -ENOENT;
227 goto out;
228 }
229 if (dentry->d_parent && server->dialect >= SMB30_PROT_ID) {
230 struct cached_fid *parent_cfid;
231
232 spin_lock(&cfids->cfid_list_lock);
233 list_for_each_entry(parent_cfid, &cfids->entries, entry) {
234 if (parent_cfid->dentry == dentry->d_parent) {
235 cifs_dbg(FYI, "found a parent cached file handle\n");
236 if (is_valid_cached_dir(parent_cfid)) {
237 lease_flags
238 |= SMB2_LEASE_FLAG_PARENT_LEASE_KEY_SET_LE;
239 memcpy(pfid->parent_lease_key,
240 parent_cfid->fid.lease_key,
241 SMB2_LEASE_KEY_SIZE);
242 }
243 break;
244 }
245 }
246 spin_unlock(&cfids->cfid_list_lock);
247 }
248 }
249 cfid->dentry = dentry;
250 cfid->tcon = tcon;
251
252 /*
253 * We do not hold the lock for the open because in case
254 * SMB2_open needs to reconnect.
255 * This is safe because no other thread will be able to get a ref
256 * to the cfid until we have finished opening the file and (possibly)
257 * acquired a lease.
258 */
259 if (smb3_encryption_required(tcon))
260 flags |= CIFS_TRANSFORM_REQ;
261
262 server->ops->new_lease_key(pfid);
263
264 memset(rqst, 0, sizeof(rqst));
265 resp_buftype[0] = resp_buftype[1] = CIFS_NO_BUFFER;
266 memset(rsp_iov, 0, sizeof(rsp_iov));
267
268 /* Open */
269 memset(&open_iov, 0, sizeof(open_iov));
270 rqst[0].rq_iov = open_iov;
271 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
272
273 oparms = (struct cifs_open_parms) {
274 .tcon = tcon,
275 .path = path,
276 .create_options = cifs_create_options(cifs_sb, CREATE_NOT_FILE),
277 .desired_access = FILE_READ_DATA | FILE_READ_ATTRIBUTES |
278 FILE_READ_EA,
279 .disposition = FILE_OPEN,
280 .fid = pfid,
281 .lease_flags = lease_flags,
282 .replay = !!(retries),
283 };
284
285 rc = SMB2_open_init(tcon, server,
286 &rqst[0], &oplock, &oparms, utf16_path);
287 if (rc)
288 goto oshr_free;
289 smb2_set_next_command(tcon, &rqst[0]);
290
291 memset(&qi_iov, 0, sizeof(qi_iov));
292 rqst[1].rq_iov = qi_iov;
293 rqst[1].rq_nvec = 1;
294
295 rc = SMB2_query_info_init(tcon, server,
296 &rqst[1], COMPOUND_FID,
297 COMPOUND_FID, FILE_ALL_INFORMATION,
298 SMB2_O_INFO_FILE, 0,
299 sizeof(struct smb2_file_all_info) +
300 PATH_MAX * 2, 0, NULL);
301 if (rc)
302 goto oshr_free;
303
304 smb2_set_related(&rqst[1]);
305
306 if (retries) {
307 /* Back-off before retry */
308 if (cur_sleep)
309 msleep(cur_sleep);
310
311 smb2_set_replay(server, &rqst[0]);
312 smb2_set_replay(server, &rqst[1]);
313 }
314
315 rc = compound_send_recv(xid, ses, server,
316 flags, 2, rqst,
317 resp_buftype, rsp_iov);
318 if (rc) {
319 if (rc == -EREMCHG) {
320 tcon->need_reconnect = true;
321 pr_warn_once("server share %s deleted\n",
322 tcon->tree_name);
323 }
324 goto oshr_free;
325 }
326 cfid->is_open = true;
327
328 spin_lock(&cfids->cfid_list_lock);
329
330 o_rsp = (struct smb2_create_rsp *)rsp_iov[0].iov_base;
331 oparms.fid->persistent_fid = o_rsp->PersistentFileId;
332 oparms.fid->volatile_fid = o_rsp->VolatileFileId;
333 #ifdef CONFIG_CIFS_DEBUG2
334 oparms.fid->mid = le64_to_cpu(o_rsp->hdr.MessageId);
335 #endif /* CIFS_DEBUG2 */
336
337
338 if (o_rsp->OplockLevel != SMB2_OPLOCK_LEVEL_LEASE) {
339 spin_unlock(&cfids->cfid_list_lock);
340 rc = -EINVAL;
341 goto oshr_free;
342 }
343
344 rc = smb2_parse_contexts(server, rsp_iov,
345 &oparms.fid->epoch,
346 oparms.fid->lease_key,
347 &oplock, NULL, NULL);
348 if (rc) {
349 spin_unlock(&cfids->cfid_list_lock);
350 goto oshr_free;
351 }
352
353 rc = -EINVAL;
354 if (!(oplock & SMB2_LEASE_READ_CACHING_HE)) {
355 spin_unlock(&cfids->cfid_list_lock);
356 goto oshr_free;
357 }
358 qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
359 if (le32_to_cpu(qi_rsp->OutputBufferLength) < sizeof(struct smb2_file_all_info)) {
360 spin_unlock(&cfids->cfid_list_lock);
361 goto oshr_free;
362 }
363 if (!smb2_validate_and_copy_iov(
364 le16_to_cpu(qi_rsp->OutputBufferOffset),
365 sizeof(struct smb2_file_all_info),
366 &rsp_iov[1], sizeof(struct smb2_file_all_info),
367 (char *)&cfid->file_all_info))
368 cfid->file_all_info_is_valid = true;
369
370 cfid->time = jiffies;
371 cfid->last_access_time = jiffies;
372 spin_unlock(&cfids->cfid_list_lock);
373 /* At this point the directory handle is fully cached */
374 rc = 0;
375
376 oshr_free:
377 SMB2_open_free(&rqst[0]);
378 SMB2_query_info_free(&rqst[1]);
379 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
380 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
381 out:
382 if (rc) {
383 spin_lock(&cfids->cfid_list_lock);
384 if (cfid->on_list) {
385 list_del(&cfid->entry);
386 cfid->on_list = false;
387 cfids->num_entries--;
388 }
389 if (cfid->has_lease) {
390 /*
391 * We are guaranteed to have two references at this
392 * point. One for the caller and one for a potential
393 * lease. Release one here, and the second below.
394 */
395 cfid->has_lease = false;
396 close_cached_dir_locked(cfid);
397 }
398 spin_unlock(&cfids->cfid_list_lock);
399
400 close_cached_dir(cfid);
401 } else {
402 *ret_cfid = cfid;
403 atomic_inc(&tcon->num_remote_opens);
404 }
405 kfree(utf16_path);
406
407 if (is_replayable_error(rc) &&
408 smb2_should_replay(tcon, &retries, &cur_sleep))
409 goto replay_again;
410
411 return rc;
412 }
413
open_cached_dir_by_dentry(struct cifs_tcon * tcon,struct dentry * dentry,struct cached_fid ** ret_cfid)414 int open_cached_dir_by_dentry(struct cifs_tcon *tcon,
415 struct dentry *dentry,
416 struct cached_fid **ret_cfid)
417 {
418 struct cached_fid *cfid;
419 struct cached_fids *cfids = tcon->cfids;
420
421 if (cfids == NULL)
422 return -EOPNOTSUPP;
423
424 if (!dentry)
425 return -ENOENT;
426
427 spin_lock(&cfids->cfid_list_lock);
428 list_for_each_entry(cfid, &cfids->entries, entry) {
429 if (cfid->dentry == dentry) {
430 if (!is_valid_cached_dir(cfid))
431 break;
432 cifs_dbg(FYI, "found a cached file handle by dentry\n");
433 kref_get(&cfid->refcount);
434 *ret_cfid = cfid;
435 cfid->last_access_time = jiffies;
436 spin_unlock(&cfids->cfid_list_lock);
437 return 0;
438 }
439 }
440 spin_unlock(&cfids->cfid_list_lock);
441 return -ENOENT;
442 }
443
444 static void
smb2_close_cached_fid(struct kref * ref)445 smb2_close_cached_fid(struct kref *ref)
446 __releases(&cfid->cfids->cfid_list_lock)
447 {
448 struct cached_fid *cfid = container_of(ref, struct cached_fid,
449 refcount);
450 int rc;
451
452 lockdep_assert_held(&cfid->cfids->cfid_list_lock);
453
454 if (cfid->on_list) {
455 list_del(&cfid->entry);
456 cfid->on_list = false;
457 cfid->cfids->num_entries--;
458 }
459 spin_unlock(&cfid->cfids->cfid_list_lock);
460
461 dput(cfid->dentry);
462 cfid->dentry = NULL;
463
464 if (cfid->is_open) {
465 rc = SMB2_close(0, cfid->tcon, cfid->fid.persistent_fid,
466 cfid->fid.volatile_fid);
467 if (rc) /* should we retry on -EBUSY or -EAGAIN? */
468 cifs_dbg(VFS, "close cached dir rc %d\n", rc);
469 }
470
471 free_cached_dir(cfid);
472 }
473
drop_cached_dir_by_name(const unsigned int xid,struct cifs_tcon * tcon,const char * name,struct cifs_sb_info * cifs_sb)474 void drop_cached_dir_by_name(const unsigned int xid, struct cifs_tcon *tcon,
475 const char *name, struct cifs_sb_info *cifs_sb)
476 {
477 struct cached_fid *cfid = NULL;
478 int rc;
479
480 rc = open_cached_dir(xid, tcon, name, cifs_sb, true, &cfid);
481 if (rc) {
482 cifs_dbg(FYI, "no cached dir found for rmdir(%s)\n", name);
483 return;
484 }
485 spin_lock(&cfid->cfids->cfid_list_lock);
486 if (cfid->has_lease) {
487 cfid->has_lease = false;
488 close_cached_dir_locked(cfid);
489 }
490 spin_unlock(&cfid->cfids->cfid_list_lock);
491 close_cached_dir(cfid);
492 }
493
494 /**
495 * close_cached_dir - drop a reference of a cached dir
496 *
497 * The release function will be called with cfid_list_lock held to remove the
498 * cached dirs from the list before any other thread can take another @cfid
499 * ref. Must not be called with cfid_list_lock held; use
500 * close_cached_dir_locked() called instead.
501 *
502 * @cfid: cached dir
503 */
close_cached_dir(struct cached_fid * cfid)504 void close_cached_dir(struct cached_fid *cfid)
505 {
506 lockdep_assert_not_held(&cfid->cfids->cfid_list_lock);
507 kref_put_lock(&cfid->refcount, smb2_close_cached_fid, &cfid->cfids->cfid_list_lock);
508 }
509
510 /**
511 * close_cached_dir_locked - put a reference of a cached dir with
512 * cfid_list_lock held
513 *
514 * Calling close_cached_dir() with cfid_list_lock held has the potential effect
515 * of causing a deadlock if the invariant of refcount >= 2 is false.
516 *
517 * This function is used in paths that hold cfid_list_lock and expect at least
518 * two references. If that invariant is violated, WARNs and returns without
519 * dropping a reference; the final put must still go through
520 * close_cached_dir().
521 *
522 * @cfid: cached dir
523 */
close_cached_dir_locked(struct cached_fid * cfid)524 static void close_cached_dir_locked(struct cached_fid *cfid)
525 {
526 lockdep_assert_held(&cfid->cfids->cfid_list_lock);
527
528 if (WARN_ON(kref_read(&cfid->refcount) < 2))
529 return;
530
531 kref_put(&cfid->refcount, smb2_close_cached_fid);
532 }
533
534 /*
535 * Called from cifs_kill_sb when we unmount a share
536 */
close_all_cached_dirs(struct cifs_sb_info * cifs_sb)537 void close_all_cached_dirs(struct cifs_sb_info *cifs_sb)
538 {
539 struct rb_root *root = &cifs_sb->tlink_tree;
540 struct rb_node *node;
541 struct cached_fid *cfid;
542 struct cifs_tcon *tcon;
543 struct tcon_link *tlink;
544 struct cached_fids *cfids;
545 struct cached_dir_dentry *tmp_list, *q;
546 LIST_HEAD(entry);
547
548 spin_lock(&cifs_sb->tlink_tree_lock);
549 for (node = rb_first(root); node; node = rb_next(node)) {
550 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
551 tcon = tlink_tcon(tlink);
552 if (IS_ERR(tcon))
553 continue;
554 cfids = tcon->cfids;
555 if (cfids == NULL)
556 continue;
557 spin_lock(&cfids->cfid_list_lock);
558 list_for_each_entry(cfid, &cfids->entries, entry) {
559 tmp_list = kmalloc_obj(*tmp_list, GFP_ATOMIC);
560 if (tmp_list == NULL) {
561 /*
562 * If the malloc() fails, we won't drop all
563 * dentries, and unmounting is likely to trigger
564 * a 'Dentry still in use' error.
565 */
566 cifs_tcon_dbg(VFS, "Out of memory while dropping dentries\n");
567 spin_unlock(&cfids->cfid_list_lock);
568 spin_unlock(&cifs_sb->tlink_tree_lock);
569 goto done;
570 }
571
572 tmp_list->dentry = cfid->dentry;
573 cfid->dentry = NULL;
574
575 list_add_tail(&tmp_list->entry, &entry);
576 }
577 spin_unlock(&cfids->cfid_list_lock);
578 }
579 spin_unlock(&cifs_sb->tlink_tree_lock);
580
581 done:
582 list_for_each_entry_safe(tmp_list, q, &entry, entry) {
583 list_del(&tmp_list->entry);
584 dput(tmp_list->dentry);
585 kfree(tmp_list);
586 }
587
588 /* Flush any pending work that will drop dentries */
589 flush_workqueue(cfid_put_wq);
590 }
591
592 /*
593 * Invalidate all cached dirs when a TCON has been reset
594 * due to a session loss.
595 */
invalidate_all_cached_dirs(struct cifs_tcon * tcon)596 void invalidate_all_cached_dirs(struct cifs_tcon *tcon)
597 {
598 struct cached_fids *cfids = tcon->cfids;
599 struct cached_fid *cfid, *q;
600
601 if (cfids == NULL)
602 return;
603
604 /*
605 * Mark all the cfids as closed, and move them to the cfids->dying list.
606 * They'll be cleaned up by laundromat. Take a reference to each cfid
607 * during this process.
608 */
609 spin_lock(&cfids->cfid_list_lock);
610 list_for_each_entry_safe(cfid, q, &cfids->entries, entry) {
611 list_move(&cfid->entry, &cfids->dying);
612 cfids->num_entries--;
613 cfid->is_open = false;
614 cfid->on_list = false;
615 if (cfid->has_lease) {
616 /*
617 * The lease was never cancelled from the server,
618 * so steal that reference.
619 */
620 cfid->has_lease = false;
621 } else
622 kref_get(&cfid->refcount);
623 }
624 spin_unlock(&cfids->cfid_list_lock);
625
626 /* run laundromat unconditionally now as there might have been previously queued work */
627 mod_delayed_work(cfid_put_wq, &cfids->laundromat_work, 0);
628 flush_delayed_work(&cfids->laundromat_work);
629 }
630
631 static void
cached_dir_offload_close(struct work_struct * work)632 cached_dir_offload_close(struct work_struct *work)
633 {
634 struct cached_fid *cfid = container_of(work,
635 struct cached_fid, close_work);
636 struct cifs_tcon *tcon = cfid->tcon;
637
638 WARN_ON(cfid->on_list);
639
640 close_cached_dir(cfid);
641 cifs_put_tcon(tcon, netfs_trace_tcon_ref_put_cached_close);
642 }
643
644 /*
645 * Release the cached directory's dentry, and then queue work to drop cached
646 * directory itself (closing on server if needed).
647 *
648 * Must be called with a reference to the cached_fid and a reference to the
649 * tcon.
650 */
cached_dir_put_work(struct work_struct * work)651 static void cached_dir_put_work(struct work_struct *work)
652 {
653 struct cached_fid *cfid = container_of(work, struct cached_fid,
654 put_work);
655 dput(cfid->dentry);
656 cfid->dentry = NULL;
657
658 queue_work(serverclose_wq, &cfid->close_work);
659 }
660
cached_dir_lease_break(struct cifs_tcon * tcon,__u8 lease_key[16])661 bool cached_dir_lease_break(struct cifs_tcon *tcon, __u8 lease_key[16])
662 {
663 struct cached_fids *cfids = tcon->cfids;
664 struct cached_fid *cfid;
665
666 if (cfids == NULL)
667 return false;
668
669 spin_lock(&cfids->cfid_list_lock);
670 list_for_each_entry(cfid, &cfids->entries, entry) {
671 if (cfid->has_lease &&
672 !memcmp(lease_key,
673 cfid->fid.lease_key,
674 SMB2_LEASE_KEY_SIZE)) {
675 cfid->has_lease = false;
676 cfid->time = 0;
677 /*
678 * We found a lease remove it from the list
679 * so no threads can access it.
680 */
681 list_del(&cfid->entry);
682 cfid->on_list = false;
683 cfids->num_entries--;
684
685 ++tcon->tc_count;
686 trace_smb3_tcon_ref(tcon->debug_id, tcon->tc_count,
687 netfs_trace_tcon_ref_get_cached_lease_break);
688 queue_work(cfid_put_wq, &cfid->put_work);
689 spin_unlock(&cfids->cfid_list_lock);
690 return true;
691 }
692 }
693 spin_unlock(&cfids->cfid_list_lock);
694 return false;
695 }
696
init_cached_dir(const char * path)697 static struct cached_fid *init_cached_dir(const char *path)
698 {
699 struct cached_fid *cfid;
700
701 cfid = kzalloc_obj(*cfid, GFP_ATOMIC);
702 if (!cfid)
703 return NULL;
704 cfid->path = kstrdup(path, GFP_ATOMIC);
705 if (!cfid->path) {
706 kfree(cfid);
707 return NULL;
708 }
709
710 INIT_WORK(&cfid->close_work, cached_dir_offload_close);
711 INIT_WORK(&cfid->put_work, cached_dir_put_work);
712 INIT_LIST_HEAD(&cfid->entry);
713 INIT_LIST_HEAD(&cfid->dirents.entries);
714 mutex_init(&cfid->dirents.de_mutex);
715 kref_init(&cfid->refcount);
716 return cfid;
717 }
718
free_cached_dir(struct cached_fid * cfid)719 static void free_cached_dir(struct cached_fid *cfid)
720 {
721 struct cached_dirent *dirent, *q;
722
723 WARN_ON(work_pending(&cfid->close_work));
724 WARN_ON(work_pending(&cfid->put_work));
725
726 dput(cfid->dentry);
727 cfid->dentry = NULL;
728
729 /*
730 * Delete all cached dirent names
731 */
732 list_for_each_entry_safe(dirent, q, &cfid->dirents.entries, entry) {
733 list_del(&dirent->entry);
734 kfree(dirent->name);
735 kfree(dirent);
736 }
737
738 /* adjust tcon-level counters and reset per-dir accounting */
739 if (cfid->cfids) {
740 if (cfid->dirents.entries_count)
741 atomic_long_sub((long)cfid->dirents.entries_count,
742 &cfid->cfids->total_dirents_entries);
743 if (cfid->dirents.bytes_used) {
744 atomic64_sub((long long)cfid->dirents.bytes_used,
745 &cfid->cfids->total_dirents_bytes);
746 atomic64_sub((long long)cfid->dirents.bytes_used,
747 &cifs_dircache_bytes_used);
748 }
749 }
750 cfid->dirents.entries_count = 0;
751 cfid->dirents.bytes_used = 0;
752
753 kfree(cfid->path);
754 cfid->path = NULL;
755 kfree(cfid);
756 }
757
cfids_laundromat_worker(struct work_struct * work)758 static void cfids_laundromat_worker(struct work_struct *work)
759 {
760 struct cached_fids *cfids;
761 struct cached_fid *cfid, *q;
762 LIST_HEAD(entry);
763
764 cfids = container_of(work, struct cached_fids, laundromat_work.work);
765
766 spin_lock(&cfids->cfid_list_lock);
767 /* move cfids->dying to the local list */
768 list_cut_before(&entry, &cfids->dying, &cfids->dying);
769
770 list_for_each_entry_safe(cfid, q, &cfids->entries, entry) {
771 if (cfid->last_access_time &&
772 time_after(jiffies, cfid->last_access_time + HZ * dir_cache_timeout)) {
773 cfid->on_list = false;
774 list_move(&cfid->entry, &entry);
775 cfids->num_entries--;
776 if (cfid->has_lease) {
777 /*
778 * Our lease has not yet been cancelled from the
779 * server. Steal that reference.
780 */
781 cfid->has_lease = false;
782 } else
783 kref_get(&cfid->refcount);
784 }
785 }
786 spin_unlock(&cfids->cfid_list_lock);
787
788 list_for_each_entry_safe(cfid, q, &entry, entry) {
789 list_del(&cfid->entry);
790
791 dput(cfid->dentry);
792 cfid->dentry = NULL;
793
794 if (cfid->is_open) {
795 spin_lock(&cfid->tcon->tc_lock);
796 ++cfid->tcon->tc_count;
797 trace_smb3_tcon_ref(cfid->tcon->debug_id, cfid->tcon->tc_count,
798 netfs_trace_tcon_ref_get_cached_laundromat);
799 spin_unlock(&cfid->tcon->tc_lock);
800 queue_work(serverclose_wq, &cfid->close_work);
801 } else
802 /*
803 * Drop the ref-count from above, either the lease-ref (if there
804 * was one) or the extra one acquired.
805 */
806 close_cached_dir(cfid);
807 }
808 queue_delayed_work(cfid_put_wq, &cfids->laundromat_work,
809 dir_cache_timeout * HZ);
810 }
811
init_cached_dirs(void)812 struct cached_fids *init_cached_dirs(void)
813 {
814 struct cached_fids *cfids;
815
816 cfids = kzalloc_obj(*cfids);
817 if (!cfids)
818 return NULL;
819 spin_lock_init(&cfids->cfid_list_lock);
820 INIT_LIST_HEAD(&cfids->entries);
821 INIT_LIST_HEAD(&cfids->dying);
822
823 INIT_DELAYED_WORK(&cfids->laundromat_work, cfids_laundromat_worker);
824 queue_delayed_work(cfid_put_wq, &cfids->laundromat_work,
825 dir_cache_timeout * HZ);
826
827 atomic_long_set(&cfids->total_dirents_entries, 0);
828 atomic64_set(&cfids->total_dirents_bytes, 0);
829
830 return cfids;
831 }
832
833 /*
834 * Called from tconInfoFree when we are tearing down the tcon.
835 * There are no active users or open files/directories at this point.
836 */
free_cached_dirs(struct cached_fids * cfids)837 void free_cached_dirs(struct cached_fids *cfids)
838 {
839 struct cached_fid *cfid, *q;
840 LIST_HEAD(entry);
841
842 if (cfids == NULL)
843 return;
844
845 cancel_delayed_work_sync(&cfids->laundromat_work);
846
847 spin_lock(&cfids->cfid_list_lock);
848 list_for_each_entry_safe(cfid, q, &cfids->entries, entry) {
849 cfid->on_list = false;
850 cfid->is_open = false;
851 list_move(&cfid->entry, &entry);
852 }
853 list_for_each_entry_safe(cfid, q, &cfids->dying, entry) {
854 cfid->on_list = false;
855 cfid->is_open = false;
856 list_move(&cfid->entry, &entry);
857 }
858 spin_unlock(&cfids->cfid_list_lock);
859
860 list_for_each_entry_safe(cfid, q, &entry, entry) {
861 list_del(&cfid->entry);
862 free_cached_dir(cfid);
863 }
864
865 kfree(cfids);
866 }
867