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