smbd: simplify error codepath in openat_pathref_fsp()
[gd/samba-autobuild/.git] / source3 / smbd / files.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Files[] structure handling
4    Copyright (C) Andrew Tridgell 1998
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "smbd/smbd.h"
22 #include "smbd/globals.h"
23 #include "libcli/security/security.h"
24 #include "util_tdb.h"
25 #include "lib/util/bitmap.h"
26
27 #define FILE_HANDLE_OFFSET 0x1000
28
29 static NTSTATUS fsp_attach_smb_fname(struct files_struct *fsp,
30                                      struct smb_filename **_smb_fname);
31
32 /**
33  * create new fsp to be used for file_new or a durable handle reconnect
34  */
35 NTSTATUS fsp_new(struct connection_struct *conn, TALLOC_CTX *mem_ctx,
36                  files_struct **result)
37 {
38         NTSTATUS status = NT_STATUS_NO_MEMORY;
39         files_struct *fsp = NULL;
40         struct smbd_server_connection *sconn = conn->sconn;
41
42         fsp = talloc_zero(mem_ctx, struct files_struct);
43         if (fsp == NULL) {
44                 goto fail;
45         }
46
47         /*
48          * This can't be a child of fsp because the file_handle can be ref'd
49          * when doing a dos/fcb open, which will then share the file_handle
50          * across multiple fsps.
51          */
52         fsp->fh = fd_handle_create(mem_ctx);
53         if (fsp->fh == NULL) {
54                 goto fail;
55         }
56
57         fsp->fsp_flags.use_ofd_locks = !lp_smbd_force_process_locks(SNUM(conn));
58 #ifndef HAVE_OFD_LOCKS
59         fsp->fsp_flags.use_ofd_locks = false;
60 #endif
61
62         fh_set_refcount(fsp->fh, 1);
63         fsp_set_fd(fsp, -1);
64
65         fsp->fnum = FNUM_FIELD_INVALID;
66         fsp->conn = conn;
67         fsp->close_write_time = make_omit_timespec();
68
69         DLIST_ADD(sconn->files, fsp);
70         sconn->num_files += 1;
71
72         conn->num_files_open++;
73
74         DBG_INFO("allocated files structure (%u used)\n",
75                 (unsigned int)sconn->num_files);
76
77         *result = fsp;
78         return NT_STATUS_OK;
79
80 fail:
81         if (fsp != NULL) {
82                 TALLOC_FREE(fsp->fh);
83         }
84         TALLOC_FREE(fsp);
85
86         return status;
87 }
88
89 void fsp_set_gen_id(files_struct *fsp)
90 {
91         static uint64_t gen_id = 1;
92
93         /*
94          * A billion of 64-bit increments per second gives us
95          * more than 500 years of runtime without wrap.
96          */
97         gen_id++;
98         fh_set_gen_id(fsp->fh, gen_id);
99 }
100
101 /****************************************************************************
102  Find first available file slot.
103 ****************************************************************************/
104
105 NTSTATUS fsp_bind_smb(struct files_struct *fsp, struct smb_request *req)
106 {
107         struct smbXsrv_open *op = NULL;
108         NTTIME now;
109         NTSTATUS status;
110
111         if (req == NULL) {
112                 DBG_DEBUG("INTERNAL_OPEN_ONLY, skipping smbXsrv_open\n");
113                 return NT_STATUS_OK;
114         }
115
116         now = timeval_to_nttime(&fsp->open_time);
117
118         status = smbXsrv_open_create(req->xconn,
119                                      fsp->conn->session_info,
120                                      now,
121                                      &op);
122         if (!NT_STATUS_IS_OK(status)) {
123                 return status;
124         }
125         fsp->op = op;
126         op->compat = fsp;
127         fsp->fnum = op->local_id;
128
129         fsp->mid = req->mid;
130         req->chain_fsp = fsp;
131
132         DBG_DEBUG("fsp [%s] mid [%" PRIu64"]\n",
133                 fsp_str_dbg(fsp), fsp->mid);
134
135         return NT_STATUS_OK;
136 }
137
138 NTSTATUS file_new(struct smb_request *req, connection_struct *conn,
139                   files_struct **result)
140 {
141         struct smbd_server_connection *sconn = conn->sconn;
142         files_struct *fsp;
143         NTSTATUS status;
144
145         status = fsp_new(conn, conn, &fsp);
146         if (!NT_STATUS_IS_OK(status)) {
147                 return status;
148         }
149
150         GetTimeOfDay(&fsp->open_time);
151
152         status = fsp_bind_smb(fsp, req);
153         if (!NT_STATUS_IS_OK(status)) {
154                 file_free(NULL, fsp);
155                 return status;
156         }
157
158         fsp_set_gen_id(fsp);
159
160         /*
161          * Create an smb_filename with "" for the base_name.  There are very
162          * few NULL checks, so make sure it's initialized with something. to
163          * be safe until an audit can be done.
164          */
165         fsp->fsp_name = synthetic_smb_fname(fsp,
166                                             "",
167                                             NULL,
168                                             NULL,
169                                             0,
170                                             0);
171         if (fsp->fsp_name == NULL) {
172                 file_free(NULL, fsp);
173                 return NT_STATUS_NO_MEMORY;
174         }
175
176         DBG_INFO("new file %s\n", fsp_fnum_dbg(fsp));
177
178         /* A new fsp invalidates the positive and
179           negative fsp_fi_cache as the new fsp is pushed
180           at the start of the list and we search from
181           a cache hit to the *end* of the list. */
182
183         ZERO_STRUCT(sconn->fsp_fi_cache);
184
185         *result = fsp;
186         return NT_STATUS_OK;
187 }
188
189 NTSTATUS create_internal_fsp(connection_struct *conn,
190                              const struct smb_filename *smb_fname,
191                              struct files_struct **_fsp)
192 {
193         struct files_struct *fsp = NULL;
194         NTSTATUS status;
195
196         status = file_new(NULL, conn, &fsp);
197         if (!NT_STATUS_IS_OK(status)) {
198                 return status;
199         }
200
201         status = fsp_set_smb_fname(fsp, smb_fname);
202         if (!NT_STATUS_IS_OK(status)) {
203                 file_free(NULL, fsp);
204                 return status;
205         }
206
207         *_fsp = fsp;
208         return NT_STATUS_OK;
209 }
210
211 /*
212  * Create an internal fsp for an *existing* directory.
213  *
214  * This should only be used by callers in the VFS that need to control the
215  * opening of the directory. Otherwise use open_internal_dirfsp_at().
216  */
217 NTSTATUS create_internal_dirfsp(connection_struct *conn,
218                                 const struct smb_filename *smb_dname,
219                                 struct files_struct **_fsp)
220 {
221         struct files_struct *fsp = NULL;
222         NTSTATUS status;
223
224         status = create_internal_fsp(conn, smb_dname, &fsp);
225         if (!NT_STATUS_IS_OK(status)) {
226                 return status;
227         }
228
229         fsp->access_mask = FILE_LIST_DIRECTORY;
230         fsp->fsp_flags.is_directory = true;
231         fsp->fsp_flags.is_dirfsp = true;
232
233         *_fsp = fsp;
234         return NT_STATUS_OK;
235 }
236
237 /*
238  * Open an internal fsp for an *existing* directory.
239  */
240 NTSTATUS open_internal_dirfsp(connection_struct *conn,
241                               const struct smb_filename *smb_dname,
242                               int open_flags,
243                               struct files_struct **_fsp)
244 {
245         struct files_struct *fsp = NULL;
246         NTSTATUS status;
247         int ret;
248
249         status = create_internal_dirfsp(conn, smb_dname, &fsp);
250         if (!NT_STATUS_IS_OK(status)) {
251                 return status;
252         }
253
254 #ifdef O_DIRECTORY
255         open_flags |= O_DIRECTORY;
256 #endif
257         status = fd_openat(conn->cwd_fsp, fsp->fsp_name, fsp, open_flags, 0);
258         if (!NT_STATUS_IS_OK(status)) {
259                 DBG_INFO("Could not open fd for %s (%s)\n",
260                          smb_fname_str_dbg(smb_dname),
261                          nt_errstr(status));
262                 file_free(NULL, fsp);
263                 return status;
264         }
265
266         ret = SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st);
267         if (ret != 0) {
268                 return map_nt_error_from_unix(errno);
269         }
270
271         if (!S_ISDIR(fsp->fsp_name->st.st_ex_mode)) {
272                 DBG_ERR("%s is not a directory!\n",
273                         smb_fname_str_dbg(smb_dname));
274                 file_free(NULL, fsp);
275                 return NT_STATUS_NOT_A_DIRECTORY;
276         }
277
278         fsp->file_id = vfs_file_id_from_sbuf(conn, &fsp->fsp_name->st);
279
280         *_fsp = fsp;
281         return NT_STATUS_OK;
282 }
283
284 /*
285  * The "link" in the name doesn't imply link in the filesystem
286  * sense. It's a object that "links" together an fsp and an smb_fname
287  * and the link allocated as talloc child of an fsp.
288  *
289  * The link is created for fsps that open_smbfname_fsp() returns in
290  * smb_fname->fsp. When this fsp is freed by fsp_free() by some caller
291  * somewhere, the destructor fsp_smb_fname_link_destructor() on the link object
292  * will use the link to reset the reference in smb_fname->fsp that is about to
293  * go away.
294  *
295  * This prevents smb_fname_internal_fsp_destructor() from seeing dangling fsp
296  * pointers.
297  */
298
299 struct fsp_smb_fname_link {
300         struct fsp_smb_fname_link **smb_fname_link;
301         struct files_struct **smb_fname_fsp;
302 };
303
304 static int fsp_smb_fname_link_destructor(struct fsp_smb_fname_link *link)
305 {
306         if (link->smb_fname_link == NULL) {
307                 return 0;
308         }
309
310         *link->smb_fname_link = NULL;
311         *link->smb_fname_fsp = NULL;
312         return 0;
313 }
314
315 static NTSTATUS fsp_smb_fname_link(struct files_struct *fsp,
316                                    struct fsp_smb_fname_link **smb_fname_link,
317                                    struct files_struct **smb_fname_fsp)
318 {
319         struct fsp_smb_fname_link *link = NULL;
320
321         SMB_ASSERT(*smb_fname_link == NULL);
322         SMB_ASSERT(*smb_fname_fsp == NULL);
323
324         link = talloc_zero(fsp, struct fsp_smb_fname_link);
325         if (link == NULL) {
326                 return NT_STATUS_NO_MEMORY;
327         }
328
329         link->smb_fname_link = smb_fname_link;
330         link->smb_fname_fsp = smb_fname_fsp;
331         *smb_fname_link = link;
332         *smb_fname_fsp = fsp;
333
334         talloc_set_destructor(link, fsp_smb_fname_link_destructor);
335         return NT_STATUS_OK;
336 }
337
338 /*
339  * Free a link, carefully avoiding to trigger the link destructor
340  */
341 static void destroy_fsp_smb_fname_link(struct fsp_smb_fname_link **_link)
342 {
343         struct fsp_smb_fname_link *link = *_link;
344
345         if (link == NULL) {
346                 return;
347         }
348         talloc_set_destructor(link, NULL);
349         TALLOC_FREE(link);
350         *_link = NULL;
351 }
352
353 /*
354  * Talloc destructor set on an smb_fname set by openat_pathref_fsp() used to
355  * close the embedded smb_fname->fsp.
356  */
357 static int smb_fname_fsp_destructor(struct smb_filename *smb_fname)
358 {
359         struct files_struct *fsp = smb_fname->fsp;
360         NTSTATUS status;
361
362         destroy_fsp_smb_fname_link(&smb_fname->fsp_link);
363
364         if (fsp == NULL) {
365                 return 0;
366         }
367
368         if (fsp->base_fsp != NULL) {
369                 struct files_struct *tmp_base_fsp = fsp->base_fsp;
370
371                 fsp_set_base_fsp(fsp, NULL);
372
373                 status = fd_close(tmp_base_fsp);
374                 SMB_ASSERT(NT_STATUS_IS_OK(status));
375                 file_free(NULL, tmp_base_fsp);
376         }
377
378         status = fd_close(fsp);
379         SMB_ASSERT(NT_STATUS_IS_OK(status));
380         file_free(NULL, fsp);
381         smb_fname->fsp = NULL;
382
383         return 0;
384 }
385
386 /*
387  * For proper streams support, we have to open the base_fsp for pathref
388  * fsp's as well.
389  */
390 static NTSTATUS open_pathref_base_fsp(const struct files_struct *dirfsp,
391                                       struct files_struct *fsp)
392 {
393         struct smb_filename *smb_fname_base = NULL;
394         NTSTATUS status;
395         int ret;
396
397         smb_fname_base = synthetic_smb_fname(talloc_tos(),
398                                              fsp->fsp_name->base_name,
399                                              NULL,
400                                              NULL,
401                                              fsp->fsp_name->twrp,
402                                              fsp->fsp_name->flags);
403         if (smb_fname_base == NULL) {
404                 return NT_STATUS_NO_MEMORY;
405         }
406
407         ret = vfs_stat(fsp->conn, smb_fname_base);
408         if (ret != 0) {
409                 return map_nt_error_from_unix(errno);
410         }
411
412         status = openat_pathref_fsp(dirfsp, smb_fname_base);
413         if (!NT_STATUS_IS_OK(status)) {
414                 TALLOC_FREE(smb_fname_base);
415                 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
416                         DBG_DEBUG("Opening base file failed: %s\n",
417                                   nt_errstr(status));
418                         return status;
419                 }
420                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
421         }
422
423         fsp_set_base_fsp(fsp, smb_fname_base->fsp);
424         smb_fname_fsp_unlink(smb_fname_base);
425         TALLOC_FREE(smb_fname_base);
426
427         return NT_STATUS_OK;
428 }
429
430 /*
431  * Open an internal O_PATH based fsp for smb_fname. If O_PATH is not
432  * available, open O_RDONLY as root. Both is done in fd_open() ->
433  * non_widelink_open(), triggered by setting fsp->fsp_flags.is_pathref to
434  * true.
435  */
436 NTSTATUS openat_pathref_fsp(const struct files_struct *dirfsp,
437                             struct smb_filename *smb_fname)
438 {
439         connection_struct *conn = dirfsp->conn;
440         struct smb_filename *full_fname = NULL;
441         struct files_struct *fsp = NULL;
442         int open_flags = O_RDONLY;
443         NTSTATUS status;
444
445         DBG_DEBUG("smb_fname [%s]\n", smb_fname_str_dbg(smb_fname));
446
447         if (smb_fname->fsp != NULL) {
448                 /* We already have one for this name. */
449                 DBG_DEBUG("smb_fname [%s] already has a pathref fsp.\n",
450                         smb_fname_str_dbg(smb_fname));
451                 return NT_STATUS_OK;
452         }
453
454         if (!VALID_STAT(smb_fname->st)) {
455                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
456         }
457
458         if (S_ISLNK(smb_fname->st.st_ex_mode)) {
459                 return NT_STATUS_STOPPED_ON_SYMLINK;
460         }
461
462         status = fsp_new(conn, conn, &fsp);
463         if (!NT_STATUS_IS_OK(status)) {
464                 return status;
465         }
466
467         GetTimeOfDay(&fsp->open_time);
468         fsp_set_gen_id(fsp);
469         ZERO_STRUCT(conn->sconn->fsp_fi_cache);
470
471         fsp->fsp_flags.is_pathref = true;
472         if (S_ISDIR(smb_fname->st.st_ex_mode)) {
473                 fsp->fsp_flags.is_directory = true;
474                 open_flags |= O_DIRECTORY;
475         }
476
477         full_fname = full_path_from_dirfsp_atname(fsp,
478                                                   dirfsp,
479                                                   smb_fname);
480         if (full_fname == NULL) {
481                 status = NT_STATUS_NO_MEMORY;
482                 goto fail;
483         }
484
485         if (is_ntfs_default_stream_smb_fname(full_fname)) {
486                 full_fname->stream_name = NULL;
487         }
488
489         status = fsp_attach_smb_fname(fsp, &full_fname);
490         if (!NT_STATUS_IS_OK(status)) {
491                 goto fail;
492         }
493
494         if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
495             && is_ntfs_stream_smb_fname(fsp->fsp_name))
496         {
497                 status = open_pathref_base_fsp(dirfsp, fsp);
498                 if (!NT_STATUS_IS_OK(status)) {
499                         goto fail;
500                 }
501         }
502
503         status = fd_openat(dirfsp, smb_fname, fsp, open_flags, 0);
504         if (!NT_STATUS_IS_OK(status)) {
505                 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND) ||
506                     NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_PATH_NOT_FOUND))
507                 {
508                         /*
509                          * streams_xattr return NT_STATUS_NOT_FOUND for
510                          * opens of not yet exisiting streams.
511                          *
512                          * ELOOP maps to NT_STATUS_OBJECT_PATH_NOT_FOUND
513                          * and this will result from a open request from
514                          * a POSIX client on a symlink.
515                          *
516                          * NT_STATUS_OBJECT_NAME_NOT_FOUND is the simple
517                          * ENOENT case.
518                          */
519                         status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
520                 }
521                 goto fail;
522         }
523
524         if (!check_same_dev_ino(&smb_fname->st, &fsp->fsp_name->st)) {
525                 DBG_DEBUG("file [%s] - dev/ino mismatch. "
526                           "Old (dev=%ju, ino=%ju). "
527                           "New (dev=%ju, ino=%ju).\n",
528                           smb_fname_str_dbg(smb_fname),
529                           (uintmax_t)smb_fname->st.st_ex_dev,
530                           (uintmax_t)smb_fname->st.st_ex_ino,
531                           (uintmax_t)fsp->fsp_name->st.st_ex_dev,
532                           (uintmax_t)fsp->fsp_name->st.st_ex_ino);
533                 status = NT_STATUS_ACCESS_DENIED;
534                 goto fail;
535         }
536
537         fsp->file_id = vfs_file_id_from_sbuf(conn, &fsp->fsp_name->st);
538
539         status = fsp_smb_fname_link(fsp,
540                                     &smb_fname->fsp_link,
541                                     &smb_fname->fsp);
542         if (!NT_STATUS_IS_OK(status)) {
543                 goto fail;
544         }
545
546         DBG_DEBUG("fsp [%s]: OK\n", fsp_str_dbg(fsp));
547
548         talloc_set_destructor(smb_fname, smb_fname_fsp_destructor);
549         return NT_STATUS_OK;
550
551 fail:
552         DBG_DEBUG("Opening pathref for [%s] failed: %s\n",
553                   smb_fname_str_dbg(smb_fname),
554                   nt_errstr(status));
555
556         if (fsp == NULL) {
557                 return status;
558         }
559         if (fsp->base_fsp != NULL) {
560                 struct files_struct *tmp_base_fsp = fsp->base_fsp;
561
562                 fsp_set_base_fsp(fsp, NULL);
563
564                 fd_close(tmp_base_fsp);
565                 file_free(NULL, tmp_base_fsp);
566         }
567         fd_close(fsp);
568         file_free(NULL, fsp);
569         return status;
570 }
571
572 void smb_fname_fsp_unlink(struct smb_filename *smb_fname)
573 {
574         talloc_set_destructor(smb_fname, NULL);
575         smb_fname->fsp = NULL;
576         destroy_fsp_smb_fname_link(&smb_fname->fsp_link);
577 }
578
579 /*
580  * Move any existing embedded fsp refs from the src name to the
581  * destination. It's safe to call this on src smb_fname's that have no embedded
582  * pathref fsp.
583  */
584 NTSTATUS move_smb_fname_fsp_link(struct smb_filename *smb_fname_dst,
585                                  struct smb_filename *smb_fname_src)
586 {
587         NTSTATUS status;
588
589         /*
590          * The target should always not be linked yet!
591          */
592         SMB_ASSERT(smb_fname_dst->fsp == NULL);
593         SMB_ASSERT(smb_fname_dst->fsp_link == NULL);
594
595         if (smb_fname_src->fsp == NULL) {
596                 return NT_STATUS_OK;
597         }
598
599         status = fsp_smb_fname_link(smb_fname_src->fsp,
600                                     &smb_fname_dst->fsp_link,
601                                     &smb_fname_dst->fsp);
602         if (!NT_STATUS_IS_OK(status)) {
603                 return status;
604         }
605
606         talloc_set_destructor(smb_fname_dst, smb_fname_fsp_destructor);
607
608         smb_fname_fsp_unlink(smb_fname_src);
609
610         return NT_STATUS_OK;
611 }
612
613 /**
614  * Create an smb_fname and open smb_fname->fsp pathref
615  **/
616 NTSTATUS synthetic_pathref(TALLOC_CTX *mem_ctx,
617                            struct files_struct *dirfsp,
618                            const char *base_name,
619                            const char *stream_name,
620                            const SMB_STRUCT_STAT *psbuf,
621                            NTTIME twrp,
622                            uint32_t flags,
623                            struct smb_filename **_smb_fname)
624 {
625         struct smb_filename *smb_fname = NULL;
626         NTSTATUS status;
627         int ret;
628
629         smb_fname = synthetic_smb_fname(mem_ctx,
630                                         base_name,
631                                         stream_name,
632                                         psbuf,
633                                         twrp,
634                                         flags);
635         if (smb_fname == NULL) {
636                 return NT_STATUS_NO_MEMORY;
637         }
638
639         if (!VALID_STAT(smb_fname->st)) {
640                 ret = vfs_stat(dirfsp->conn, smb_fname);
641                 if (ret != 0) {
642                         DBG_ERR("stat [%s] failed: %s",
643                                 smb_fname_str_dbg(smb_fname),
644                                 strerror(errno));
645                         TALLOC_FREE(smb_fname);
646                         return map_nt_error_from_unix(errno);
647                 }
648         }
649
650         status = openat_pathref_fsp(dirfsp, smb_fname);
651         if (NT_STATUS_EQUAL(status, NT_STATUS_STOPPED_ON_SYMLINK)) {
652                 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
653         }
654         if (!NT_STATUS_IS_OK(status)) {
655                 DBG_ERR("opening [%s] failed\n",
656                         smb_fname_str_dbg(smb_fname));
657                 TALLOC_FREE(smb_fname);
658                 return status;
659         }
660
661         *_smb_fname = smb_fname;
662         return NT_STATUS_OK;
663 }
664
665 static int atname_destructor(struct smb_filename *smb_fname)
666 {
667         destroy_fsp_smb_fname_link(&smb_fname->fsp_link);
668         return 0;
669 }
670
671 /**
672  * Turn a path into a parent pathref and atname
673  *
674  * This returns the parent pathref in _parent and the name relative to it. If
675  * smb_fname was a pathref (ie smb_fname->fsp != NULL), then _atname will be a
676  * pathref as well, ie _atname->fsp will point at the same fsp as
677  * smb_fname->fsp.
678  **/
679 NTSTATUS parent_pathref(TALLOC_CTX *mem_ctx,
680                         struct files_struct *dirfsp,
681                         const struct smb_filename *smb_fname,
682                         struct smb_filename **_parent,
683                         struct smb_filename **_atname)
684 {
685         struct smb_filename *parent = NULL;
686         struct smb_filename *atname = NULL;
687         NTSTATUS status;
688         int ret;
689         bool ok;
690
691         ok = parent_smb_fname(mem_ctx,
692                               smb_fname,
693                               &parent,
694                               &atname);
695         if (!ok) {
696                 return NT_STATUS_NO_MEMORY;
697         }
698
699         ret = vfs_stat(dirfsp->conn, parent);
700         if (ret != 0) {
701                 TALLOC_FREE(parent);
702                 return map_nt_error_from_unix(errno);
703         }
704
705         status = openat_pathref_fsp(dirfsp, parent);
706         if (NT_STATUS_EQUAL(status, NT_STATUS_STOPPED_ON_SYMLINK)) {
707                 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
708         }
709         if (!NT_STATUS_IS_OK(status)) {
710                 TALLOC_FREE(parent);
711                 return status;
712         }
713
714         if (smb_fname->fsp != NULL) {
715                 status = fsp_smb_fname_link(smb_fname->fsp,
716                                             &atname->fsp_link,
717                                             &atname->fsp);
718                 if (!NT_STATUS_IS_OK(status)) {
719                         TALLOC_FREE(parent);
720                         return status;
721                 }
722                 talloc_set_destructor(atname, atname_destructor);
723         }
724         *_parent = parent;
725         *_atname = atname;
726         return NT_STATUS_OK;
727 }
728
729 /****************************************************************************
730  Close all open files for a connection.
731 ****************************************************************************/
732
733 void file_close_conn(connection_struct *conn)
734 {
735         files_struct *fsp, *next;
736
737         for (fsp=conn->sconn->files; fsp; fsp=next) {
738                 next = fsp->next;
739                 if (fsp->conn != conn) {
740                         continue;
741                 }
742                 if (fsp->op != NULL && fsp->op->global->durable) {
743                         /*
744                          * A tree disconnect closes a durable handle
745                          */
746                         fsp->op->global->durable = false;
747                 }
748                 close_file(NULL, fsp, SHUTDOWN_CLOSE);
749         }
750 }
751
752 /****************************************************************************
753  Initialise file structures.
754 ****************************************************************************/
755
756 static int files_max_open_fds;
757
758 bool file_init_global(void)
759 {
760         int request_max = lp_max_open_files();
761         int real_lim;
762         int real_max;
763
764         if (files_max_open_fds != 0) {
765                 return true;
766         }
767
768         /*
769          * Set the max_open files to be the requested
770          * max plus a fudgefactor to allow for the extra
771          * fd's we need such as log files etc...
772          */
773         real_lim = set_maxfiles(request_max + MAX_OPEN_FUDGEFACTOR);
774
775         real_max = real_lim - MAX_OPEN_FUDGEFACTOR;
776
777         if (real_max + FILE_HANDLE_OFFSET + MAX_OPEN_PIPES > 65536) {
778                 real_max = 65536 - FILE_HANDLE_OFFSET - MAX_OPEN_PIPES;
779         }
780
781         if (real_max != request_max) {
782                 DEBUG(1, ("file_init_global: Information only: requested %d "
783                           "open files, %d are available.\n",
784                           request_max, real_max));
785         }
786
787         SMB_ASSERT(real_max > 100);
788
789         files_max_open_fds = real_max;
790         return true;
791 }
792
793 bool file_init(struct smbd_server_connection *sconn)
794 {
795         bool ok;
796
797         ok = file_init_global();
798         if (!ok) {
799                 return false;
800         }
801
802         sconn->real_max_open_files = files_max_open_fds;
803
804         return true;
805 }
806
807 /****************************************************************************
808  Close files open by a specified vuid.
809 ****************************************************************************/
810
811 void file_close_user(struct smbd_server_connection *sconn, uint64_t vuid)
812 {
813         files_struct *fsp, *next;
814
815         for (fsp=sconn->files; fsp; fsp=next) {
816                 next=fsp->next;
817                 if (fsp->vuid == vuid) {
818                         close_file(NULL, fsp, SHUTDOWN_CLOSE);
819                 }
820         }
821 }
822
823 /*
824  * Walk the files table until "fn" returns non-NULL
825  */
826
827 struct files_struct *files_forall(
828         struct smbd_server_connection *sconn,
829         struct files_struct *(*fn)(struct files_struct *fsp,
830                                    void *private_data),
831         void *private_data)
832 {
833         struct files_struct *fsp, *next;
834
835         for (fsp = sconn->files; fsp; fsp = next) {
836                 struct files_struct *ret;
837                 next = fsp->next;
838                 ret = fn(fsp, private_data);
839                 if (ret != NULL) {
840                         return ret;
841                 }
842         }
843         return NULL;
844 }
845
846 /****************************************************************************
847  Find a fsp given a file descriptor.
848 ****************************************************************************/
849
850 files_struct *file_find_fd(struct smbd_server_connection *sconn, int fd)
851 {
852         int count=0;
853         files_struct *fsp;
854
855         for (fsp=sconn->files; fsp; fsp=fsp->next,count++) {
856                 if (fsp_get_pathref_fd(fsp) == fd) {
857                         if (count > 10) {
858                                 DLIST_PROMOTE(sconn->files, fsp);
859                         }
860                         return fsp;
861                 }
862         }
863
864         return NULL;
865 }
866
867 /****************************************************************************
868  Find a fsp given a device, inode and file_id.
869 ****************************************************************************/
870
871 files_struct *file_find_dif(struct smbd_server_connection *sconn,
872                             struct file_id id, unsigned long gen_id)
873 {
874         int count=0;
875         files_struct *fsp;
876
877         if (gen_id == 0) {
878                 return NULL;
879         }
880
881         for (fsp = sconn->files; fsp; fsp = fsp->next,count++) {
882                 /*
883                  * We can have a fsp->fh->fd == -1 here as it could be a stat
884                  * open.
885                  */
886                 if (!file_id_equal(&fsp->file_id, &id)) {
887                         continue;
888                 }
889                 if (!fsp->fsp_flags.is_fsa) {
890                         continue;
891                 }
892                 if (fh_get_gen_id(fsp->fh) != gen_id) {
893                         continue;
894                 }
895                 if (count > 10) {
896                         DLIST_PROMOTE(sconn->files, fsp);
897                 }
898                 /* Paranoia check. */
899                 if ((fsp_get_pathref_fd(fsp) == -1) &&
900                     (fsp->oplock_type != NO_OPLOCK &&
901                      fsp->oplock_type != LEASE_OPLOCK))
902                 {
903                         struct file_id_buf idbuf;
904
905                         DBG_ERR("file %s file_id = "
906                                 "%s, gen = %u oplock_type = %u is a "
907                                 "stat open with oplock type !\n",
908                                 fsp_str_dbg(fsp),
909                                 file_id_str_buf(fsp->file_id, &idbuf),
910                                 (unsigned int)fh_get_gen_id(fsp->fh),
911                                 (unsigned int)fsp->oplock_type);
912                         smb_panic("file_find_dif");
913                 }
914                 return fsp;
915         }
916
917         return NULL;
918 }
919
920 /****************************************************************************
921  Find the first fsp given a device and inode.
922  We use a singleton cache here to speed up searching from getfilepathinfo
923  calls.
924 ****************************************************************************/
925
926 files_struct *file_find_di_first(struct smbd_server_connection *sconn,
927                                  struct file_id id,
928                                  bool need_fsa)
929 {
930         files_struct *fsp;
931
932         if (file_id_equal(&sconn->fsp_fi_cache.id, &id)) {
933                 /* Positive or negative cache hit. */
934                 return sconn->fsp_fi_cache.fsp;
935         }
936
937         sconn->fsp_fi_cache.id = id;
938
939         for (fsp=sconn->files;fsp;fsp=fsp->next) {
940                 if (need_fsa && !fsp->fsp_flags.is_fsa) {
941                         continue;
942                 }
943                 if (file_id_equal(&fsp->file_id, &id)) {
944                         /* Setup positive cache. */
945                         sconn->fsp_fi_cache.fsp = fsp;
946                         return fsp;
947                 }
948         }
949
950         /* Setup negative cache. */
951         sconn->fsp_fi_cache.fsp = NULL;
952         return NULL;
953 }
954
955 /****************************************************************************
956  Find the next fsp having the same device and inode.
957 ****************************************************************************/
958
959 files_struct *file_find_di_next(files_struct *start_fsp,
960                                 bool need_fsa)
961 {
962         files_struct *fsp;
963
964         for (fsp = start_fsp->next;fsp;fsp=fsp->next) {
965                 if (need_fsa && !fsp->fsp_flags.is_fsa) {
966                         continue;
967                 }
968                 if (file_id_equal(&fsp->file_id, &start_fsp->file_id)) {
969                         return fsp;
970                 }
971         }
972
973         return NULL;
974 }
975
976 struct files_struct *file_find_one_fsp_from_lease_key(
977         struct smbd_server_connection *sconn,
978         const struct smb2_lease_key *lease_key)
979 {
980         struct files_struct *fsp;
981
982         for (fsp = sconn->files; fsp; fsp=fsp->next) {
983                 if ((fsp->lease != NULL) &&
984                     (fsp->lease->lease.lease_key.data[0] ==
985                      lease_key->data[0]) &&
986                     (fsp->lease->lease.lease_key.data[1] ==
987                      lease_key->data[1])) {
988                         return fsp;
989                 }
990         }
991         return NULL;
992 }
993
994 /****************************************************************************
995  Find any fsp open with a pathname below that of an already open path.
996 ****************************************************************************/
997
998 bool file_find_subpath(files_struct *dir_fsp)
999 {
1000         files_struct *fsp;
1001         size_t dlen;
1002         char *d_fullname = NULL;
1003
1004         d_fullname = talloc_asprintf(talloc_tos(), "%s/%s",
1005                                      dir_fsp->conn->connectpath,
1006                                      dir_fsp->fsp_name->base_name);
1007
1008         if (!d_fullname) {
1009                 return false;
1010         }
1011
1012         dlen = strlen(d_fullname);
1013
1014         for (fsp=dir_fsp->conn->sconn->files; fsp; fsp=fsp->next) {
1015                 char *d1_fullname;
1016
1017                 if (fsp == dir_fsp) {
1018                         continue;
1019                 }
1020
1021                 d1_fullname = talloc_asprintf(talloc_tos(),
1022                                         "%s/%s",
1023                                         fsp->conn->connectpath,
1024                                         fsp->fsp_name->base_name);
1025
1026                 /*
1027                  * If the open file has a path that is a longer
1028                  * component, then it's a subpath.
1029                  */
1030                 if (strnequal(d_fullname, d1_fullname, dlen) &&
1031                                 (d1_fullname[dlen] == '/')) {
1032                         TALLOC_FREE(d1_fullname);
1033                         TALLOC_FREE(d_fullname);
1034                         return true;
1035                 }
1036                 TALLOC_FREE(d1_fullname);
1037         }
1038
1039         TALLOC_FREE(d_fullname);
1040         return false;
1041 }
1042
1043 /****************************************************************************
1044  Free up a fsp.
1045 ****************************************************************************/
1046
1047 static void fsp_free(files_struct *fsp)
1048 {
1049         struct smbd_server_connection *sconn = fsp->conn->sconn;
1050
1051         if (fsp == sconn->fsp_fi_cache.fsp) {
1052                 ZERO_STRUCT(sconn->fsp_fi_cache);
1053         }
1054
1055         DLIST_REMOVE(sconn->files, fsp);
1056         SMB_ASSERT(sconn->num_files > 0);
1057         sconn->num_files--;
1058
1059         TALLOC_FREE(fsp->fake_file_handle);
1060
1061         if (fh_get_refcount(fsp->fh) == 1) {
1062                 TALLOC_FREE(fsp->fh);
1063         } else {
1064                 size_t new_refcount = fh_get_refcount(fsp->fh) - 1;
1065                 fh_set_refcount(fsp->fh, new_refcount);
1066         }
1067
1068         if (fsp->lease != NULL) {
1069                 if (fsp->lease->ref_count == 1) {
1070                         TALLOC_FREE(fsp->lease);
1071                 } else {
1072                         fsp->lease->ref_count--;
1073                 }
1074         }
1075
1076         fsp->conn->num_files_open--;
1077
1078         if (fsp->fsp_name != NULL &&
1079             fsp->fsp_name->fsp_link != NULL)
1080         {
1081                 /*
1082                  * Free fsp_link of fsp->fsp_name. To do this in the correct
1083                  * talloc destructor order we have to do it here. The
1084                  * talloc_free() of the link should set the fsp pointer to NULL.
1085                  */
1086                 TALLOC_FREE(fsp->fsp_name->fsp_link);
1087                 SMB_ASSERT(fsp->fsp_name->fsp == NULL);
1088         }
1089
1090         /* this is paranoia, just in case someone tries to reuse the
1091            information */
1092         ZERO_STRUCTP(fsp);
1093
1094         /* fsp->fsp_name is a talloc child and is free'd automatically. */
1095         TALLOC_FREE(fsp);
1096 }
1097
1098 void file_free(struct smb_request *req, files_struct *fsp)
1099 {
1100         struct smbd_server_connection *sconn = fsp->conn->sconn;
1101         uint64_t fnum = fsp->fnum;
1102
1103         if (fsp == fsp->conn->cwd_fsp) {
1104                 return;
1105         }
1106
1107         if (fsp->notify) {
1108                 size_t len = fsp_fullbasepath(fsp, NULL, 0);
1109                 char fullpath[len+1];
1110
1111                 fsp_fullbasepath(fsp, fullpath, sizeof(fullpath));
1112
1113                 /*
1114                  * Avoid /. at the end of the path name. notify can't
1115                  * deal with it.
1116                  */
1117                 if (len > 1 && fullpath[len-1] == '.' &&
1118                     fullpath[len-2] == '/') {
1119                         fullpath[len-2] = '\0';
1120                 }
1121
1122                 notify_remove(fsp->conn->sconn->notify_ctx, fsp, fullpath);
1123                 TALLOC_FREE(fsp->notify);
1124         }
1125
1126         /* Ensure this event will never fire. */
1127         TALLOC_FREE(fsp->update_write_time_event);
1128
1129         if (fsp->op != NULL) {
1130                 fsp->op->compat = NULL;
1131         }
1132         TALLOC_FREE(fsp->op);
1133
1134         if ((req != NULL) && (fsp == req->chain_fsp)) {
1135                 req->chain_fsp = NULL;
1136         }
1137
1138         /*
1139          * Clear all possible chained fsp
1140          * pointers in the SMB2 request queue.
1141          */
1142         remove_smb2_chained_fsp(fsp);
1143
1144         /* Drop all remaining extensions. */
1145         vfs_remove_all_fsp_extensions(fsp);
1146
1147         fsp_free(fsp);
1148
1149         DEBUG(5,("freed files structure %llu (%u used)\n",
1150                  (unsigned long long)fnum, (unsigned int)sconn->num_files));
1151 }
1152
1153 /****************************************************************************
1154  Get an fsp from a packet given a 16 bit fnum.
1155 ****************************************************************************/
1156
1157 files_struct *file_fsp(struct smb_request *req, uint16_t fid)
1158 {
1159         struct smbXsrv_open *op;
1160         NTSTATUS status;
1161         NTTIME now = 0;
1162         files_struct *fsp;
1163
1164         if (req == NULL) {
1165                 /*
1166                  * We should never get here. req==NULL could in theory
1167                  * only happen from internal opens with a non-zero
1168                  * root_dir_fid. Internal opens just don't do that, at
1169                  * least they are not supposed to do so. And if they
1170                  * start to do so, they better fake up a smb_request
1171                  * from which we get the right smbd_server_conn. While
1172                  * this should never happen, let's return NULL here.
1173                  */
1174                 return NULL;
1175         }
1176
1177         if (req->chain_fsp != NULL) {
1178                 if (req->chain_fsp->fsp_flags.closing) {
1179                         return NULL;
1180                 }
1181                 return req->chain_fsp;
1182         }
1183
1184         if (req->xconn == NULL) {
1185                 return NULL;
1186         }
1187
1188         now = timeval_to_nttime(&req->request_time);
1189
1190         status = smb1srv_open_lookup(req->xconn,
1191                                      fid, now, &op);
1192         if (!NT_STATUS_IS_OK(status)) {
1193                 return NULL;
1194         }
1195
1196         fsp = op->compat;
1197         if (fsp == NULL) {
1198                 return NULL;
1199         }
1200
1201         if (fsp->fsp_flags.closing) {
1202                 return NULL;
1203         }
1204
1205         req->chain_fsp = fsp;
1206         return fsp;
1207 }
1208
1209 struct files_struct *file_fsp_get(struct smbd_smb2_request *smb2req,
1210                                   uint64_t persistent_id,
1211                                   uint64_t volatile_id)
1212 {
1213         struct smbXsrv_open *op;
1214         NTSTATUS status;
1215         NTTIME now = 0;
1216         struct files_struct *fsp;
1217
1218         now = timeval_to_nttime(&smb2req->request_time);
1219
1220         status = smb2srv_open_lookup(smb2req->xconn,
1221                                      persistent_id, volatile_id,
1222                                      now, &op);
1223         if (!NT_STATUS_IS_OK(status)) {
1224                 return NULL;
1225         }
1226
1227         fsp = op->compat;
1228         if (fsp == NULL) {
1229                 return NULL;
1230         }
1231
1232         if (smb2req->tcon == NULL) {
1233                 return NULL;
1234         }
1235
1236         if (smb2req->tcon->compat != fsp->conn) {
1237                 return NULL;
1238         }
1239
1240         if (smb2req->session == NULL) {
1241                 return NULL;
1242         }
1243
1244         if (smb2req->session->global->session_wire_id != fsp->vuid) {
1245                 return NULL;
1246         }
1247
1248         if (fsp->fsp_flags.closing) {
1249                 return NULL;
1250         }
1251
1252         return fsp;
1253 }
1254
1255 struct files_struct *file_fsp_smb2(struct smbd_smb2_request *smb2req,
1256                                    uint64_t persistent_id,
1257                                    uint64_t volatile_id)
1258 {
1259         struct files_struct *fsp;
1260
1261         if (smb2req->compat_chain_fsp != NULL) {
1262                 if (smb2req->compat_chain_fsp->fsp_flags.closing) {
1263                         return NULL;
1264                 }
1265                 return smb2req->compat_chain_fsp;
1266         }
1267
1268         fsp = file_fsp_get(smb2req, persistent_id, volatile_id);
1269         if (fsp == NULL) {
1270                 return NULL;
1271         }
1272
1273         smb2req->compat_chain_fsp = fsp;
1274         return fsp;
1275 }
1276
1277 /****************************************************************************
1278  Duplicate the file handle part for a DOS or FCB open.
1279 ****************************************************************************/
1280
1281 NTSTATUS dup_file_fsp(
1282         struct smb_request *req,
1283         files_struct *from,
1284         uint32_t access_mask,
1285         uint32_t create_options,
1286         files_struct *to)
1287 {
1288         size_t new_refcount;
1289
1290         /* this can never happen for print files */
1291         SMB_ASSERT(from->print_file == NULL);
1292
1293         TALLOC_FREE(to->fh);
1294
1295         to->fh = from->fh;
1296         new_refcount = fh_get_refcount(to->fh) + 1;
1297         fh_set_refcount(to->fh, new_refcount);
1298
1299         to->file_id = from->file_id;
1300         to->initial_allocation_size = from->initial_allocation_size;
1301         to->file_pid = from->file_pid;
1302         to->vuid = from->vuid;
1303         to->open_time = from->open_time;
1304         to->access_mask = access_mask;
1305         to->oplock_type = from->oplock_type;
1306         to->fsp_flags.can_lock = from->fsp_flags.can_lock;
1307         to->fsp_flags.can_read = ((access_mask & FILE_READ_DATA) != 0);
1308         to->fsp_flags.can_write =
1309                 CAN_WRITE(from->conn) &&
1310                 ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) != 0);
1311         to->fsp_flags.modified = from->fsp_flags.modified;
1312         to->fsp_flags.is_directory = from->fsp_flags.is_directory;
1313         to->fsp_flags.aio_write_behind = from->fsp_flags.aio_write_behind;
1314         to->fsp_flags.is_fsa = from->fsp_flags.is_fsa;
1315         to->fsp_flags.is_pathref = from->fsp_flags.is_pathref;
1316         to->fsp_flags.have_proc_fds = from->fsp_flags.have_proc_fds;
1317         to->fsp_flags.is_dirfsp = from->fsp_flags.is_dirfsp;
1318
1319         return fsp_set_smb_fname(to, from->fsp_name);
1320 }
1321
1322 /**
1323  * Return a jenkins hash of a pathname on a connection.
1324  */
1325
1326 NTSTATUS file_name_hash(connection_struct *conn,
1327                         const char *name, uint32_t *p_name_hash)
1328 {
1329         char tmpbuf[PATH_MAX];
1330         char *fullpath, *to_free;
1331         ssize_t len;
1332         TDB_DATA key;
1333
1334         /* Set the hash of the full pathname. */
1335
1336         len = full_path_tos(conn->connectpath, name, tmpbuf, sizeof(tmpbuf),
1337                             &fullpath, &to_free);
1338         if (len == -1) {
1339                 return NT_STATUS_NO_MEMORY;
1340         }
1341         key = (TDB_DATA) { .dptr = (uint8_t *)fullpath, .dsize = len+1 };
1342         *p_name_hash = tdb_jenkins_hash(&key);
1343
1344         DEBUG(10,("file_name_hash: %s hash 0x%x\n",
1345                   fullpath,
1346                 (unsigned int)*p_name_hash ));
1347
1348         TALLOC_FREE(to_free);
1349         return NT_STATUS_OK;
1350 }
1351
1352 static NTSTATUS fsp_attach_smb_fname(struct files_struct *fsp,
1353                                      struct smb_filename **_smb_fname)
1354 {
1355         struct smb_filename *smb_fname_new = *_smb_fname;
1356         const char *name_str = NULL;
1357         uint32_t name_hash = 0;
1358         NTSTATUS status;
1359
1360         name_str = smb_fname_str_dbg(smb_fname_new);
1361         if (name_str == NULL) {
1362                 return NT_STATUS_NO_MEMORY;
1363         }
1364
1365         status = file_name_hash(fsp->conn,
1366                                 name_str,
1367                                 &name_hash);
1368         if (!NT_STATUS_IS_OK(status)) {
1369                 return status;
1370         }
1371
1372         status = fsp_smb_fname_link(fsp,
1373                                     &smb_fname_new->fsp_link,
1374                                     &smb_fname_new->fsp);
1375         if (!NT_STATUS_IS_OK(status)) {
1376                 return status;
1377         }
1378
1379         fsp->name_hash = name_hash;
1380         fsp->fsp_name = smb_fname_new;
1381         *_smb_fname = NULL;
1382         return NT_STATUS_OK;
1383 }
1384
1385 /**
1386  * The only way that the fsp->fsp_name field should ever be set.
1387  */
1388 NTSTATUS fsp_set_smb_fname(struct files_struct *fsp,
1389                            const struct smb_filename *smb_fname_in)
1390 {
1391         struct smb_filename *smb_fname_old = fsp->fsp_name;
1392         struct smb_filename *smb_fname_new = NULL;
1393         NTSTATUS status;
1394
1395         smb_fname_new = cp_smb_filename(fsp, smb_fname_in);
1396         if (smb_fname_new == NULL) {
1397                 return NT_STATUS_NO_MEMORY;
1398         }
1399
1400         status = fsp_attach_smb_fname(fsp, &smb_fname_new);
1401         if (!NT_STATUS_IS_OK(status)) {
1402                 TALLOC_FREE(smb_fname_new);
1403                 return status;
1404         }
1405
1406         if (smb_fname_old != NULL) {
1407                 smb_fname_fsp_unlink(smb_fname_old);
1408                 TALLOC_FREE(smb_fname_old);
1409         }
1410
1411         return NT_STATUS_OK;
1412 }
1413
1414 size_t fsp_fullbasepath(struct files_struct *fsp, char *buf, size_t buflen)
1415 {
1416         int len = 0;
1417         char tmp_buf[1] = {'\0'};
1418
1419         /*
1420          * Don't pass NULL buffer to snprintf (to satisfy static checker)
1421          * Some callers will call this function with NULL for buf and
1422          * 0 for buflen in order to get length of fullbasepath (without
1423          * needing to allocate or write to buf)
1424          */
1425         if (buf == NULL) {
1426                 buf = tmp_buf;
1427         }
1428
1429         len = snprintf(buf, buflen, "%s/%s", fsp->conn->connectpath,
1430                        fsp->fsp_name->base_name);
1431         SMB_ASSERT(len>0);
1432
1433         return len;
1434 }
1435
1436 void fsp_set_base_fsp(struct files_struct *fsp, struct files_struct *base_fsp)
1437 {
1438         SMB_ASSERT(fsp->stream_fsp == NULL);
1439         if (base_fsp != NULL) {
1440                 SMB_ASSERT(base_fsp->base_fsp == NULL);
1441                 SMB_ASSERT(base_fsp->stream_fsp == NULL);
1442         }
1443
1444         if (fsp->base_fsp != NULL) {
1445                 SMB_ASSERT(fsp->base_fsp->stream_fsp == fsp);
1446                 fsp->base_fsp->stream_fsp = NULL;
1447         }
1448
1449         fsp->base_fsp = base_fsp;
1450         if (fsp->base_fsp != NULL) {
1451                 fsp->base_fsp->stream_fsp = fsp;
1452         }
1453 }