Merge tag 'powerpc-6.6-6' of git://git.kernel.org/pub/scm/linux/kernel/git/powerpc...
[platform/kernel/linux-starfive.git] / fs / smb / client / cached_dir.c
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
20 static struct cached_fid *find_or_create_cached_dir(struct cached_fids *cfids,
21                                                     const char *path,
22                                                     bool lookup_only,
23                                                     __u32 max_cached_dirs)
24 {
25         struct cached_fid *cfid;
26
27         spin_lock(&cfids->cfid_list_lock);
28         list_for_each_entry(cfid, &cfids->entries, entry) {
29                 if (!strcmp(cfid->path, path)) {
30                         /*
31                          * If it doesn't have a lease it is either not yet
32                          * fully cached or it may be in the process of
33                          * being deleted due to a lease break.
34                          */
35                         if (!cfid->has_lease) {
36                                 spin_unlock(&cfids->cfid_list_lock);
37                                 return NULL;
38                         }
39                         kref_get(&cfid->refcount);
40                         spin_unlock(&cfids->cfid_list_lock);
41                         return cfid;
42                 }
43         }
44         if (lookup_only) {
45                 spin_unlock(&cfids->cfid_list_lock);
46                 return NULL;
47         }
48         if (cfids->num_entries >= max_cached_dirs) {
49                 spin_unlock(&cfids->cfid_list_lock);
50                 return NULL;
51         }
52         cfid = init_cached_dir(path);
53         if (cfid == NULL) {
54                 spin_unlock(&cfids->cfid_list_lock);
55                 return NULL;
56         }
57         cfid->cfids = cfids;
58         cfids->num_entries++;
59         list_add(&cfid->entry, &cfids->entries);
60         cfid->on_list = true;
61         kref_get(&cfid->refcount);
62         spin_unlock(&cfids->cfid_list_lock);
63         return cfid;
64 }
65
66 static struct dentry *
67 path_to_dentry(struct cifs_sb_info *cifs_sb, const char *path)
68 {
69         struct dentry *dentry;
70         const char *s, *p;
71         char sep;
72
73         sep = CIFS_DIR_SEP(cifs_sb);
74         dentry = dget(cifs_sb->root);
75         s = path;
76
77         do {
78                 struct inode *dir = d_inode(dentry);
79                 struct dentry *child;
80
81                 if (!S_ISDIR(dir->i_mode)) {
82                         dput(dentry);
83                         dentry = ERR_PTR(-ENOTDIR);
84                         break;
85                 }
86
87                 /* skip separators */
88                 while (*s == sep)
89                         s++;
90                 if (!*s)
91                         break;
92                 p = s++;
93                 /* next separator */
94                 while (*s && *s != sep)
95                         s++;
96
97                 child = lookup_positive_unlocked(p, dentry, s - p);
98                 dput(dentry);
99                 dentry = child;
100         } while (!IS_ERR(dentry));
101         return dentry;
102 }
103
104 static const char *path_no_prefix(struct cifs_sb_info *cifs_sb,
105                                   const char *path)
106 {
107         size_t len = 0;
108
109         if (!*path)
110                 return path;
111
112         if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) &&
113             cifs_sb->prepath) {
114                 len = strlen(cifs_sb->prepath) + 1;
115                 if (unlikely(len > strlen(path)))
116                         return ERR_PTR(-EINVAL);
117         }
118         return path + len;
119 }
120
121 /*
122  * Open the and cache a directory handle.
123  * If error then *cfid is not initialized.
124  */
125 int open_cached_dir(unsigned int xid, struct cifs_tcon *tcon,
126                     const char *path,
127                     struct cifs_sb_info *cifs_sb,
128                     bool lookup_only, struct cached_fid **ret_cfid)
129 {
130         struct cifs_ses *ses;
131         struct TCP_Server_Info *server;
132         struct cifs_open_parms oparms;
133         struct smb2_create_rsp *o_rsp = NULL;
134         struct smb2_query_info_rsp *qi_rsp = NULL;
135         int resp_buftype[2];
136         struct smb_rqst rqst[2];
137         struct kvec rsp_iov[2];
138         struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
139         struct kvec qi_iov[1];
140         int rc, flags = 0;
141         __le16 *utf16_path = NULL;
142         u8 oplock = SMB2_OPLOCK_LEVEL_II;
143         struct cifs_fid *pfid;
144         struct dentry *dentry = NULL;
145         struct cached_fid *cfid;
146         struct cached_fids *cfids;
147         const char *npath;
148
149         if (tcon == NULL || tcon->cfids == NULL || tcon->nohandlecache ||
150             is_smb1_server(tcon->ses->server) || (dir_cache_timeout == 0))
151                 return -EOPNOTSUPP;
152
153         ses = tcon->ses;
154         server = ses->server;
155         cfids = tcon->cfids;
156
157         if (!server->ops->new_lease_key)
158                 return -EIO;
159
160         if (cifs_sb->root == NULL)
161                 return -ENOENT;
162
163         utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
164         if (!utf16_path)
165                 return -ENOMEM;
166
167         cfid = find_or_create_cached_dir(cfids, path, lookup_only, tcon->max_cached_dirs);
168         if (cfid == NULL) {
169                 kfree(utf16_path);
170                 return -ENOENT;
171         }
172         /*
173          * Return cached fid if it has a lease.  Otherwise, it is either a new
174          * entry or laundromat worker removed it from @cfids->entries.  Caller
175          * will put last reference if the latter.
176          */
177         spin_lock(&cfids->cfid_list_lock);
178         if (cfid->has_lease) {
179                 spin_unlock(&cfids->cfid_list_lock);
180                 *ret_cfid = cfid;
181                 kfree(utf16_path);
182                 return 0;
183         }
184         spin_unlock(&cfids->cfid_list_lock);
185
186         /*
187          * Skip any prefix paths in @path as lookup_positive_unlocked() ends up
188          * calling ->lookup() which already adds those through
189          * build_path_from_dentry().  Also, do it earlier as we might reconnect
190          * below when trying to send compounded request and then potentially
191          * having a different prefix path (e.g. after DFS failover).
192          */
193         npath = path_no_prefix(cifs_sb, path);
194         if (IS_ERR(npath)) {
195                 rc = PTR_ERR(npath);
196                 kfree(utf16_path);
197                 return rc;
198         }
199
200         /*
201          * We do not hold the lock for the open because in case
202          * SMB2_open needs to reconnect.
203          * This is safe because no other thread will be able to get a ref
204          * to the cfid until we have finished opening the file and (possibly)
205          * acquired a lease.
206          */
207         if (smb3_encryption_required(tcon))
208                 flags |= CIFS_TRANSFORM_REQ;
209
210         pfid = &cfid->fid;
211         server->ops->new_lease_key(pfid);
212
213         memset(rqst, 0, sizeof(rqst));
214         resp_buftype[0] = resp_buftype[1] = CIFS_NO_BUFFER;
215         memset(rsp_iov, 0, sizeof(rsp_iov));
216
217         /* Open */
218         memset(&open_iov, 0, sizeof(open_iov));
219         rqst[0].rq_iov = open_iov;
220         rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
221
222         oparms = (struct cifs_open_parms) {
223                 .tcon = tcon,
224                 .path = path,
225                 .create_options = cifs_create_options(cifs_sb, CREATE_NOT_FILE),
226                 .desired_access =  FILE_READ_DATA | FILE_READ_ATTRIBUTES,
227                 .disposition = FILE_OPEN,
228                 .fid = pfid,
229         };
230
231         rc = SMB2_open_init(tcon, server,
232                             &rqst[0], &oplock, &oparms, utf16_path);
233         if (rc)
234                 goto oshr_free;
235         smb2_set_next_command(tcon, &rqst[0]);
236
237         memset(&qi_iov, 0, sizeof(qi_iov));
238         rqst[1].rq_iov = qi_iov;
239         rqst[1].rq_nvec = 1;
240
241         rc = SMB2_query_info_init(tcon, server,
242                                   &rqst[1], COMPOUND_FID,
243                                   COMPOUND_FID, FILE_ALL_INFORMATION,
244                                   SMB2_O_INFO_FILE, 0,
245                                   sizeof(struct smb2_file_all_info) +
246                                   PATH_MAX * 2, 0, NULL);
247         if (rc)
248                 goto oshr_free;
249
250         smb2_set_related(&rqst[1]);
251
252         rc = compound_send_recv(xid, ses, server,
253                                 flags, 2, rqst,
254                                 resp_buftype, rsp_iov);
255         if (rc) {
256                 if (rc == -EREMCHG) {
257                         tcon->need_reconnect = true;
258                         pr_warn_once("server share %s deleted\n",
259                                      tcon->tree_name);
260                 }
261                 goto oshr_free;
262         }
263         cfid->tcon = tcon;
264         cfid->is_open = true;
265
266         o_rsp = (struct smb2_create_rsp *)rsp_iov[0].iov_base;
267         oparms.fid->persistent_fid = o_rsp->PersistentFileId;
268         oparms.fid->volatile_fid = o_rsp->VolatileFileId;
269 #ifdef CONFIG_CIFS_DEBUG2
270         oparms.fid->mid = le64_to_cpu(o_rsp->hdr.MessageId);
271 #endif /* CIFS_DEBUG2 */
272
273         if (o_rsp->OplockLevel != SMB2_OPLOCK_LEVEL_LEASE)
274                 goto oshr_free;
275
276         smb2_parse_contexts(server, o_rsp,
277                             &oparms.fid->epoch,
278                             oparms.fid->lease_key, &oplock,
279                             NULL, NULL);
280         if (!(oplock & SMB2_LEASE_READ_CACHING_HE))
281                 goto oshr_free;
282         qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
283         if (le32_to_cpu(qi_rsp->OutputBufferLength) < sizeof(struct smb2_file_all_info))
284                 goto oshr_free;
285         if (!smb2_validate_and_copy_iov(
286                                 le16_to_cpu(qi_rsp->OutputBufferOffset),
287                                 sizeof(struct smb2_file_all_info),
288                                 &rsp_iov[1], sizeof(struct smb2_file_all_info),
289                                 (char *)&cfid->file_all_info))
290                 cfid->file_all_info_is_valid = true;
291
292         if (!npath[0])
293                 dentry = dget(cifs_sb->root);
294         else {
295                 dentry = path_to_dentry(cifs_sb, npath);
296                 if (IS_ERR(dentry)) {
297                         rc = -ENOENT;
298                         goto oshr_free;
299                 }
300         }
301         spin_lock(&cfids->cfid_list_lock);
302         cfid->dentry = dentry;
303         cfid->time = jiffies;
304         cfid->has_lease = true;
305         spin_unlock(&cfids->cfid_list_lock);
306
307 oshr_free:
308         kfree(utf16_path);
309         SMB2_open_free(&rqst[0]);
310         SMB2_query_info_free(&rqst[1]);
311         free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
312         free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
313         spin_lock(&cfids->cfid_list_lock);
314         if (!cfid->has_lease) {
315                 if (rc) {
316                         if (cfid->on_list) {
317                                 list_del(&cfid->entry);
318                                 cfid->on_list = false;
319                                 cfids->num_entries--;
320                         }
321                         rc = -ENOENT;
322                 } else {
323                         /*
324                          * We are guaranteed to have two references at this
325                          * point. One for the caller and one for a potential
326                          * lease. Release the Lease-ref so that the directory
327                          * will be closed when the caller closes the cached
328                          * handle.
329                          */
330                         spin_unlock(&cfids->cfid_list_lock);
331                         kref_put(&cfid->refcount, smb2_close_cached_fid);
332                         goto out;
333                 }
334         }
335         spin_unlock(&cfids->cfid_list_lock);
336         if (rc) {
337                 if (cfid->is_open)
338                         SMB2_close(0, cfid->tcon, cfid->fid.persistent_fid,
339                                    cfid->fid.volatile_fid);
340                 free_cached_dir(cfid);
341                 cfid = NULL;
342         }
343 out:
344         if (rc == 0) {
345                 *ret_cfid = cfid;
346                 atomic_inc(&tcon->num_remote_opens);
347         }
348
349         return rc;
350 }
351
352 int open_cached_dir_by_dentry(struct cifs_tcon *tcon,
353                               struct dentry *dentry,
354                               struct cached_fid **ret_cfid)
355 {
356         struct cached_fid *cfid;
357         struct cached_fids *cfids = tcon->cfids;
358
359         if (cfids == NULL)
360                 return -ENOENT;
361
362         spin_lock(&cfids->cfid_list_lock);
363         list_for_each_entry(cfid, &cfids->entries, entry) {
364                 if (dentry && cfid->dentry == dentry) {
365                         cifs_dbg(FYI, "found a cached root file handle by dentry\n");
366                         kref_get(&cfid->refcount);
367                         *ret_cfid = cfid;
368                         spin_unlock(&cfids->cfid_list_lock);
369                         return 0;
370                 }
371         }
372         spin_unlock(&cfids->cfid_list_lock);
373         return -ENOENT;
374 }
375
376 static void
377 smb2_close_cached_fid(struct kref *ref)
378 {
379         struct cached_fid *cfid = container_of(ref, struct cached_fid,
380                                                refcount);
381
382         spin_lock(&cfid->cfids->cfid_list_lock);
383         if (cfid->on_list) {
384                 list_del(&cfid->entry);
385                 cfid->on_list = false;
386                 cfid->cfids->num_entries--;
387         }
388         spin_unlock(&cfid->cfids->cfid_list_lock);
389
390         dput(cfid->dentry);
391         cfid->dentry = NULL;
392
393         if (cfid->is_open) {
394                 SMB2_close(0, cfid->tcon, cfid->fid.persistent_fid,
395                            cfid->fid.volatile_fid);
396                 atomic_dec(&cfid->tcon->num_remote_opens);
397         }
398
399         free_cached_dir(cfid);
400 }
401
402 void drop_cached_dir_by_name(const unsigned int xid, struct cifs_tcon *tcon,
403                              const char *name, struct cifs_sb_info *cifs_sb)
404 {
405         struct cached_fid *cfid = NULL;
406         int rc;
407
408         rc = open_cached_dir(xid, tcon, name, cifs_sb, true, &cfid);
409         if (rc) {
410                 cifs_dbg(FYI, "no cached dir found for rmdir(%s)\n", name);
411                 return;
412         }
413         spin_lock(&cfid->cfids->cfid_list_lock);
414         if (cfid->has_lease) {
415                 cfid->has_lease = false;
416                 kref_put(&cfid->refcount, smb2_close_cached_fid);
417         }
418         spin_unlock(&cfid->cfids->cfid_list_lock);
419         close_cached_dir(cfid);
420 }
421
422
423 void close_cached_dir(struct cached_fid *cfid)
424 {
425         kref_put(&cfid->refcount, smb2_close_cached_fid);
426 }
427
428 /*
429  * Called from cifs_kill_sb when we unmount a share
430  */
431 void close_all_cached_dirs(struct cifs_sb_info *cifs_sb)
432 {
433         struct rb_root *root = &cifs_sb->tlink_tree;
434         struct rb_node *node;
435         struct cached_fid *cfid;
436         struct cifs_tcon *tcon;
437         struct tcon_link *tlink;
438         struct cached_fids *cfids;
439
440         for (node = rb_first(root); node; node = rb_next(node)) {
441                 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
442                 tcon = tlink_tcon(tlink);
443                 if (IS_ERR(tcon))
444                         continue;
445                 cfids = tcon->cfids;
446                 if (cfids == NULL)
447                         continue;
448                 list_for_each_entry(cfid, &cfids->entries, entry) {
449                         dput(cfid->dentry);
450                         cfid->dentry = NULL;
451                 }
452         }
453 }
454
455 /*
456  * Invalidate all cached dirs when a TCON has been reset
457  * due to a session loss.
458  */
459 void invalidate_all_cached_dirs(struct cifs_tcon *tcon)
460 {
461         struct cached_fids *cfids = tcon->cfids;
462         struct cached_fid *cfid, *q;
463         LIST_HEAD(entry);
464
465         if (cfids == NULL)
466                 return;
467
468         spin_lock(&cfids->cfid_list_lock);
469         list_for_each_entry_safe(cfid, q, &cfids->entries, entry) {
470                 list_move(&cfid->entry, &entry);
471                 cfids->num_entries--;
472                 cfid->is_open = false;
473                 cfid->on_list = false;
474                 /* To prevent race with smb2_cached_lease_break() */
475                 kref_get(&cfid->refcount);
476         }
477         spin_unlock(&cfids->cfid_list_lock);
478
479         list_for_each_entry_safe(cfid, q, &entry, entry) {
480                 list_del(&cfid->entry);
481                 cancel_work_sync(&cfid->lease_break);
482                 if (cfid->has_lease) {
483                         /*
484                          * We lease was never cancelled from the server so we
485                          * need to drop the reference.
486                          */
487                         spin_lock(&cfids->cfid_list_lock);
488                         cfid->has_lease = false;
489                         spin_unlock(&cfids->cfid_list_lock);
490                         kref_put(&cfid->refcount, smb2_close_cached_fid);
491                 }
492                 /* Drop the extra reference opened above*/
493                 kref_put(&cfid->refcount, smb2_close_cached_fid);
494         }
495 }
496
497 static void
498 smb2_cached_lease_break(struct work_struct *work)
499 {
500         struct cached_fid *cfid = container_of(work,
501                                 struct cached_fid, lease_break);
502
503         spin_lock(&cfid->cfids->cfid_list_lock);
504         cfid->has_lease = false;
505         spin_unlock(&cfid->cfids->cfid_list_lock);
506         kref_put(&cfid->refcount, smb2_close_cached_fid);
507 }
508
509 int cached_dir_lease_break(struct cifs_tcon *tcon, __u8 lease_key[16])
510 {
511         struct cached_fids *cfids = tcon->cfids;
512         struct cached_fid *cfid;
513
514         if (cfids == NULL)
515                 return false;
516
517         spin_lock(&cfids->cfid_list_lock);
518         list_for_each_entry(cfid, &cfids->entries, entry) {
519                 if (cfid->has_lease &&
520                     !memcmp(lease_key,
521                             cfid->fid.lease_key,
522                             SMB2_LEASE_KEY_SIZE)) {
523                         cfid->time = 0;
524                         /*
525                          * We found a lease remove it from the list
526                          * so no threads can access it.
527                          */
528                         list_del(&cfid->entry);
529                         cfid->on_list = false;
530                         cfids->num_entries--;
531
532                         queue_work(cifsiod_wq,
533                                    &cfid->lease_break);
534                         spin_unlock(&cfids->cfid_list_lock);
535                         return true;
536                 }
537         }
538         spin_unlock(&cfids->cfid_list_lock);
539         return false;
540 }
541
542 static struct cached_fid *init_cached_dir(const char *path)
543 {
544         struct cached_fid *cfid;
545
546         cfid = kzalloc(sizeof(*cfid), GFP_ATOMIC);
547         if (!cfid)
548                 return NULL;
549         cfid->path = kstrdup(path, GFP_ATOMIC);
550         if (!cfid->path) {
551                 kfree(cfid);
552                 return NULL;
553         }
554
555         INIT_WORK(&cfid->lease_break, smb2_cached_lease_break);
556         INIT_LIST_HEAD(&cfid->entry);
557         INIT_LIST_HEAD(&cfid->dirents.entries);
558         mutex_init(&cfid->dirents.de_mutex);
559         spin_lock_init(&cfid->fid_lock);
560         kref_init(&cfid->refcount);
561         return cfid;
562 }
563
564 static void free_cached_dir(struct cached_fid *cfid)
565 {
566         struct cached_dirent *dirent, *q;
567
568         dput(cfid->dentry);
569         cfid->dentry = NULL;
570
571         /*
572          * Delete all cached dirent names
573          */
574         list_for_each_entry_safe(dirent, q, &cfid->dirents.entries, entry) {
575                 list_del(&dirent->entry);
576                 kfree(dirent->name);
577                 kfree(dirent);
578         }
579
580         kfree(cfid->path);
581         cfid->path = NULL;
582         kfree(cfid);
583 }
584
585 static void cfids_laundromat_worker(struct work_struct *work)
586 {
587         struct cached_fids *cfids;
588         struct cached_fid *cfid, *q;
589         LIST_HEAD(entry);
590
591         cfids = container_of(work, struct cached_fids, laundromat_work.work);
592
593         spin_lock(&cfids->cfid_list_lock);
594         list_for_each_entry_safe(cfid, q, &cfids->entries, entry) {
595                 if (cfid->time &&
596                     time_after(jiffies, cfid->time + HZ * dir_cache_timeout)) {
597                         cfid->on_list = false;
598                         list_move(&cfid->entry, &entry);
599                         cfids->num_entries--;
600                         /* To prevent race with smb2_cached_lease_break() */
601                         kref_get(&cfid->refcount);
602                 }
603         }
604         spin_unlock(&cfids->cfid_list_lock);
605
606         list_for_each_entry_safe(cfid, q, &entry, entry) {
607                 list_del(&cfid->entry);
608                 /*
609                  * Cancel and wait for the work to finish in case we are racing
610                  * with it.
611                  */
612                 cancel_work_sync(&cfid->lease_break);
613                 if (cfid->has_lease) {
614                         /*
615                          * Our lease has not yet been cancelled from the server
616                          * so we need to drop the reference.
617                          */
618                         spin_lock(&cfids->cfid_list_lock);
619                         cfid->has_lease = false;
620                         spin_unlock(&cfids->cfid_list_lock);
621                         kref_put(&cfid->refcount, smb2_close_cached_fid);
622                 }
623                 /* Drop the extra reference opened above */
624                 kref_put(&cfid->refcount, smb2_close_cached_fid);
625         }
626         queue_delayed_work(cifsiod_wq, &cfids->laundromat_work,
627                            dir_cache_timeout * HZ);
628 }
629
630 struct cached_fids *init_cached_dirs(void)
631 {
632         struct cached_fids *cfids;
633
634         cfids = kzalloc(sizeof(*cfids), GFP_KERNEL);
635         if (!cfids)
636                 return NULL;
637         spin_lock_init(&cfids->cfid_list_lock);
638         INIT_LIST_HEAD(&cfids->entries);
639
640         INIT_DELAYED_WORK(&cfids->laundromat_work, cfids_laundromat_worker);
641         queue_delayed_work(cifsiod_wq, &cfids->laundromat_work,
642                            dir_cache_timeout * HZ);
643
644         return cfids;
645 }
646
647 /*
648  * Called from tconInfoFree when we are tearing down the tcon.
649  * There are no active users or open files/directories at this point.
650  */
651 void free_cached_dirs(struct cached_fids *cfids)
652 {
653         struct cached_fid *cfid, *q;
654         LIST_HEAD(entry);
655
656         if (cfids == NULL)
657                 return;
658
659         cancel_delayed_work_sync(&cfids->laundromat_work);
660
661         spin_lock(&cfids->cfid_list_lock);
662         list_for_each_entry_safe(cfid, q, &cfids->entries, entry) {
663                 cfid->on_list = false;
664                 cfid->is_open = false;
665                 list_move(&cfid->entry, &entry);
666         }
667         spin_unlock(&cfids->cfid_list_lock);
668
669         list_for_each_entry_safe(cfid, q, &entry, entry) {
670                 list_del(&cfid->entry);
671                 free_cached_dir(cfid);
672         }
673
674         kfree(cfids);
675 }