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