smbd: remove redundant conn arg from fd_open()
[samba.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 /**
30  * create new fsp to be used for file_new or a durable handle reconnect
31  */
32 NTSTATUS fsp_new(struct connection_struct *conn, TALLOC_CTX *mem_ctx,
33                  files_struct **result)
34 {
35         NTSTATUS status = NT_STATUS_NO_MEMORY;
36         files_struct *fsp = NULL;
37         struct smbd_server_connection *sconn = conn->sconn;
38
39         fsp = talloc_zero(mem_ctx, struct files_struct);
40         if (fsp == NULL) {
41                 goto fail;
42         }
43
44         /*
45          * This can't be a child of fsp because the file_handle can be ref'd
46          * when doing a dos/fcb open, which will then share the file_handle
47          * across multiple fsps.
48          */
49         fsp->fh = talloc_zero(mem_ctx, struct fd_handle);
50         if (fsp->fh == NULL) {
51                 goto fail;
52         }
53
54 #if defined(HAVE_OFD_LOCKS)
55         fsp->fsp_flags.use_ofd_locks = true;
56         if (lp_parm_bool(SNUM(conn),
57                          "smbd",
58                          "force process locks",
59                          false)) {
60                 fsp->fsp_flags.use_ofd_locks = false;
61         }
62 #endif
63         fsp->fh->ref_count = 1;
64         fsp->fh->fd = -1;
65
66         fsp->fnum = FNUM_FIELD_INVALID;
67         fsp->conn = conn;
68         fsp->close_write_time = make_omit_timespec();
69
70         DLIST_ADD(sconn->files, fsp);
71         sconn->num_files += 1;
72
73         conn->num_files_open++;
74
75         *result = fsp;
76         return NT_STATUS_OK;
77
78 fail:
79         if (fsp != NULL) {
80                 TALLOC_FREE(fsp->fh);
81         }
82         TALLOC_FREE(fsp);
83
84         return status;
85 }
86
87 void fsp_set_gen_id(files_struct *fsp)
88 {
89         static uint64_t gen_id = 1;
90
91         /*
92          * A billion of 64-bit increments per second gives us
93          * more than 500 years of runtime without wrap.
94          */
95         fsp->fh->gen_id = gen_id++;
96 }
97
98 /****************************************************************************
99  Find first available file slot.
100 ****************************************************************************/
101
102 NTSTATUS file_new(struct smb_request *req, connection_struct *conn,
103                   files_struct **result)
104 {
105         struct smbd_server_connection *sconn = conn->sconn;
106         files_struct *fsp;
107         NTSTATUS status;
108
109         status = fsp_new(conn, conn, &fsp);
110         if (!NT_STATUS_IS_OK(status)) {
111                 return status;
112         }
113
114         GetTimeOfDay(&fsp->open_time);
115
116         if (req) {
117                 struct smbXsrv_connection *xconn = req->xconn;
118                 struct smbXsrv_open *op = NULL;
119                 NTTIME now = timeval_to_nttime(&fsp->open_time);
120
121                 status = smbXsrv_open_create(xconn,
122                                              conn->session_info,
123                                              now, &op);
124                 if (!NT_STATUS_IS_OK(status)) {
125                         file_free(NULL, fsp);
126                         return status;
127                 }
128                 fsp->op = op;
129                 op->compat = fsp;
130                 fsp->fnum = op->local_id;
131         } else {
132                 DEBUG(10, ("%s: req==NULL, INTERNAL_OPEN_ONLY, smbXsrv_open "
133                            "allocated\n", __func__));
134         }
135
136         fsp_set_gen_id(fsp);
137
138         /*
139          * Create an smb_filename with "" for the base_name.  There are very
140          * few NULL checks, so make sure it's initialized with something. to
141          * be safe until an audit can be done.
142          */
143         fsp->fsp_name = synthetic_smb_fname(fsp,
144                                             "",
145                                             NULL,
146                                             NULL,
147                                             0,
148                                             0);
149         if (fsp->fsp_name == NULL) {
150                 file_free(NULL, fsp);
151                 return NT_STATUS_NO_MEMORY;
152         }
153
154         DEBUG(5,("allocated file structure %s (%u used)\n",
155                  fsp_fnum_dbg(fsp), (unsigned int)sconn->num_files));
156
157         if (req != NULL) {
158                 fsp->mid = req->mid;
159                 req->chain_fsp = fsp;
160         }
161
162         /* A new fsp invalidates the positive and
163           negative fsp_fi_cache as the new fsp is pushed
164           at the start of the list and we search from
165           a cache hit to the *end* of the list. */
166
167         ZERO_STRUCT(sconn->fsp_fi_cache);
168
169         *result = fsp;
170         return NT_STATUS_OK;
171 }
172
173 /*
174  * Create an internal fsp for an *existing* directory.
175  *
176  * This should only be used by callers in the VFS that need to control the
177  * opening of the directory. Otherwise use open_internal_dirfsp_at().
178  */
179 NTSTATUS create_internal_dirfsp(connection_struct *conn,
180                                 const struct smb_filename *smb_dname,
181                                 struct files_struct **_fsp)
182 {
183         struct files_struct *fsp = NULL;
184         NTSTATUS status;
185
186         status = file_new(NULL, conn, &fsp);
187         if (!NT_STATUS_IS_OK(status)) {
188                 return status;
189         }
190
191         status = fsp_set_smb_fname(fsp, smb_dname);
192         if (!NT_STATUS_IS_OK(status)) {
193                 file_free(NULL, fsp);
194                 return status;
195         }
196
197         fsp->access_mask = FILE_LIST_DIRECTORY;
198         fsp->fsp_flags.is_directory = true;
199         fsp->fsp_flags.is_dirfsp = true;
200
201         *_fsp = fsp;
202         return NT_STATUS_OK;
203 }
204
205 /*
206  * Open an internal fsp for an *existing* directory.
207  */
208 NTSTATUS open_internal_dirfsp(connection_struct *conn,
209                               const struct smb_filename *smb_dname,
210                               int open_flags,
211                               struct files_struct **_fsp)
212 {
213         struct files_struct *fsp = NULL;
214         NTSTATUS status;
215         int ret;
216
217         status = create_internal_dirfsp(conn, smb_dname, &fsp);
218         if (!NT_STATUS_IS_OK(status)) {
219                 return status;
220         }
221
222 #ifdef O_DIRECTORY
223         open_flags |= O_DIRECTORY;
224 #endif
225         status = fd_open(fsp, open_flags, 0);
226         if (!NT_STATUS_IS_OK(status)) {
227                 DBG_INFO("Could not open fd for %s (%s)\n",
228                          smb_fname_str_dbg(smb_dname),
229                          nt_errstr(status));
230                 file_free(NULL, fsp);
231                 return status;
232         }
233
234         ret = SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st);
235         if (ret != 0) {
236                 return map_nt_error_from_unix(errno);
237         }
238
239         if (!S_ISDIR(fsp->fsp_name->st.st_ex_mode)) {
240                 DBG_ERR("%s is not a directory!\n",
241                         smb_fname_str_dbg(smb_dname));
242                 file_free(NULL, fsp);
243                 return NT_STATUS_NOT_A_DIRECTORY;
244         }
245
246         fsp->file_id = vfs_file_id_from_sbuf(conn, &fsp->fsp_name->st);
247
248         *_fsp = fsp;
249         return NT_STATUS_OK;
250 }
251
252 /****************************************************************************
253  Close all open files for a connection.
254 ****************************************************************************/
255
256 void file_close_conn(connection_struct *conn)
257 {
258         files_struct *fsp, *next;
259
260         for (fsp=conn->sconn->files; fsp; fsp=next) {
261                 next = fsp->next;
262                 if (fsp->conn != conn) {
263                         continue;
264                 }
265                 if (fsp->op != NULL && fsp->op->global->durable) {
266                         /*
267                          * A tree disconnect closes a durable handle
268                          */
269                         fsp->op->global->durable = false;
270                 }
271                 close_file(NULL, fsp, SHUTDOWN_CLOSE);
272         }
273 }
274
275 /****************************************************************************
276  Initialise file structures.
277 ****************************************************************************/
278
279 static int files_max_open_fds;
280
281 bool file_init_global(void)
282 {
283         int request_max = lp_max_open_files();
284         int real_lim;
285         int real_max;
286
287         if (files_max_open_fds != 0) {
288                 return true;
289         }
290
291         /*
292          * Set the max_open files to be the requested
293          * max plus a fudgefactor to allow for the extra
294          * fd's we need such as log files etc...
295          */
296         real_lim = set_maxfiles(request_max + MAX_OPEN_FUDGEFACTOR);
297
298         real_max = real_lim - MAX_OPEN_FUDGEFACTOR;
299
300         if (real_max + FILE_HANDLE_OFFSET + MAX_OPEN_PIPES > 65536) {
301                 real_max = 65536 - FILE_HANDLE_OFFSET - MAX_OPEN_PIPES;
302         }
303
304         if (real_max != request_max) {
305                 DEBUG(1, ("file_init_global: Information only: requested %d "
306                           "open files, %d are available.\n",
307                           request_max, real_max));
308         }
309
310         SMB_ASSERT(real_max > 100);
311
312         files_max_open_fds = real_max;
313         return true;
314 }
315
316 bool file_init(struct smbd_server_connection *sconn)
317 {
318         bool ok;
319
320         ok = file_init_global();
321         if (!ok) {
322                 return false;
323         }
324
325         sconn->real_max_open_files = files_max_open_fds;
326
327         return true;
328 }
329
330 /****************************************************************************
331  Close files open by a specified vuid.
332 ****************************************************************************/
333
334 void file_close_user(struct smbd_server_connection *sconn, uint64_t vuid)
335 {
336         files_struct *fsp, *next;
337
338         for (fsp=sconn->files; fsp; fsp=next) {
339                 next=fsp->next;
340                 if (fsp->vuid == vuid) {
341                         close_file(NULL, fsp, SHUTDOWN_CLOSE);
342                 }
343         }
344 }
345
346 /*
347  * Walk the files table until "fn" returns non-NULL
348  */
349
350 struct files_struct *files_forall(
351         struct smbd_server_connection *sconn,
352         struct files_struct *(*fn)(struct files_struct *fsp,
353                                    void *private_data),
354         void *private_data)
355 {
356         struct files_struct *fsp, *next;
357
358         for (fsp = sconn->files; fsp; fsp = next) {
359                 struct files_struct *ret;
360                 next = fsp->next;
361                 ret = fn(fsp, private_data);
362                 if (ret != NULL) {
363                         return ret;
364                 }
365         }
366         return NULL;
367 }
368
369 /****************************************************************************
370  Find a fsp given a file descriptor.
371 ****************************************************************************/
372
373 files_struct *file_find_fd(struct smbd_server_connection *sconn, int fd)
374 {
375         int count=0;
376         files_struct *fsp;
377
378         for (fsp=sconn->files; fsp; fsp=fsp->next,count++) {
379                 if (fsp->fh->fd == fd) {
380                         if (count > 10) {
381                                 DLIST_PROMOTE(sconn->files, fsp);
382                         }
383                         return fsp;
384                 }
385         }
386
387         return NULL;
388 }
389
390 /****************************************************************************
391  Find a fsp given a device, inode and file_id.
392 ****************************************************************************/
393
394 files_struct *file_find_dif(struct smbd_server_connection *sconn,
395                             struct file_id id, unsigned long gen_id)
396 {
397         int count=0;
398         files_struct *fsp;
399
400         if (gen_id == 0) {
401                 return NULL;
402         }
403
404         for (fsp=sconn->files; fsp; fsp=fsp->next,count++) {
405                 /* We can have a fsp->fh->fd == -1 here as it could be a stat open. */
406                 if (file_id_equal(&fsp->file_id, &id) &&
407                     fsp->fh->gen_id == gen_id ) {
408                         if (count > 10) {
409                                 DLIST_PROMOTE(sconn->files, fsp);
410                         }
411                         /* Paranoia check. */
412                         if ((fsp->fh->fd == -1) &&
413                             (fsp->oplock_type != NO_OPLOCK &&
414                              fsp->oplock_type != LEASE_OPLOCK)) {
415                                 struct file_id_buf idbuf;
416                                 DEBUG(0,("file_find_dif: file %s file_id = "
417                                          "%s, gen = %u oplock_type = %u is a "
418                                          "stat open with oplock type !\n",
419                                          fsp_str_dbg(fsp),
420                                          file_id_str_buf(fsp->file_id, &idbuf),
421                                          (unsigned int)fsp->fh->gen_id,
422                                          (unsigned int)fsp->oplock_type ));
423                                 smb_panic("file_find_dif");
424                         }
425                         return fsp;
426                 }
427         }
428
429         return NULL;
430 }
431
432 /****************************************************************************
433  Find the first fsp given a device and inode.
434  We use a singleton cache here to speed up searching from getfilepathinfo
435  calls.
436 ****************************************************************************/
437
438 files_struct *file_find_di_first(struct smbd_server_connection *sconn,
439                                  struct file_id id)
440 {
441         files_struct *fsp;
442
443         if (file_id_equal(&sconn->fsp_fi_cache.id, &id)) {
444                 /* Positive or negative cache hit. */
445                 return sconn->fsp_fi_cache.fsp;
446         }
447
448         sconn->fsp_fi_cache.id = id;
449
450         for (fsp=sconn->files;fsp;fsp=fsp->next) {
451                 if (file_id_equal(&fsp->file_id, &id)) {
452                         /* Setup positive cache. */
453                         sconn->fsp_fi_cache.fsp = fsp;
454                         return fsp;
455                 }
456         }
457
458         /* Setup negative cache. */
459         sconn->fsp_fi_cache.fsp = NULL;
460         return NULL;
461 }
462
463 /****************************************************************************
464  Find the next fsp having the same device and inode.
465 ****************************************************************************/
466
467 files_struct *file_find_di_next(files_struct *start_fsp)
468 {
469         files_struct *fsp;
470
471         for (fsp = start_fsp->next;fsp;fsp=fsp->next) {
472                 if (file_id_equal(&fsp->file_id, &start_fsp->file_id)) {
473                         return fsp;
474                 }
475         }
476
477         return NULL;
478 }
479
480 struct files_struct *file_find_one_fsp_from_lease_key(
481         struct smbd_server_connection *sconn,
482         const struct smb2_lease_key *lease_key)
483 {
484         struct files_struct *fsp;
485
486         for (fsp = sconn->files; fsp; fsp=fsp->next) {
487                 if ((fsp->lease != NULL) &&
488                     (fsp->lease->lease.lease_key.data[0] ==
489                      lease_key->data[0]) &&
490                     (fsp->lease->lease.lease_key.data[1] ==
491                      lease_key->data[1])) {
492                         return fsp;
493                 }
494         }
495         return NULL;
496 }
497
498 /****************************************************************************
499  Find any fsp open with a pathname below that of an already open path.
500 ****************************************************************************/
501
502 bool file_find_subpath(files_struct *dir_fsp)
503 {
504         files_struct *fsp;
505         size_t dlen;
506         char *d_fullname = NULL;
507
508         d_fullname = talloc_asprintf(talloc_tos(), "%s/%s",
509                                      dir_fsp->conn->connectpath,
510                                      dir_fsp->fsp_name->base_name);
511
512         if (!d_fullname) {
513                 return false;
514         }
515
516         dlen = strlen(d_fullname);
517
518         for (fsp=dir_fsp->conn->sconn->files; fsp; fsp=fsp->next) {
519                 char *d1_fullname;
520
521                 if (fsp == dir_fsp) {
522                         continue;
523                 }
524
525                 d1_fullname = talloc_asprintf(talloc_tos(),
526                                         "%s/%s",
527                                         fsp->conn->connectpath,
528                                         fsp->fsp_name->base_name);
529
530                 /*
531                  * If the open file has a path that is a longer
532                  * component, then it's a subpath.
533                  */
534                 if (strnequal(d_fullname, d1_fullname, dlen) &&
535                                 (d1_fullname[dlen] == '/')) {
536                         TALLOC_FREE(d1_fullname);
537                         TALLOC_FREE(d_fullname);
538                         return true;
539                 }
540                 TALLOC_FREE(d1_fullname);
541         }
542
543         TALLOC_FREE(d_fullname);
544         return false;
545 }
546
547 /****************************************************************************
548  Free up a fsp.
549 ****************************************************************************/
550
551 void fsp_free(files_struct *fsp)
552 {
553         struct smbd_server_connection *sconn = fsp->conn->sconn;
554
555         if (fsp == sconn->fsp_fi_cache.fsp) {
556                 ZERO_STRUCT(sconn->fsp_fi_cache);
557         }
558
559         DLIST_REMOVE(sconn->files, fsp);
560         SMB_ASSERT(sconn->num_files > 0);
561         sconn->num_files--;
562
563         TALLOC_FREE(fsp->fake_file_handle);
564
565         if (fsp->fh->ref_count == 1) {
566                 TALLOC_FREE(fsp->fh);
567         } else {
568                 fsp->fh->ref_count--;
569         }
570
571         if (fsp->lease != NULL) {
572                 if (fsp->lease->ref_count == 1) {
573                         TALLOC_FREE(fsp->lease);
574                 } else {
575                         fsp->lease->ref_count--;
576                 }
577         }
578
579         fsp->conn->num_files_open--;
580
581         /* this is paranoia, just in case someone tries to reuse the
582            information */
583         ZERO_STRUCTP(fsp);
584
585         /* fsp->fsp_name is a talloc child and is free'd automatically. */
586         TALLOC_FREE(fsp);
587 }
588
589 void file_free(struct smb_request *req, files_struct *fsp)
590 {
591         struct smbd_server_connection *sconn = fsp->conn->sconn;
592         uint64_t fnum = fsp->fnum;
593
594         if (fsp->notify) {
595                 size_t len = fsp_fullbasepath(fsp, NULL, 0);
596                 char fullpath[len+1];
597
598                 fsp_fullbasepath(fsp, fullpath, sizeof(fullpath));
599
600                 /*
601                  * Avoid /. at the end of the path name. notify can't
602                  * deal with it.
603                  */
604                 if (len > 1 && fullpath[len-1] == '.' &&
605                     fullpath[len-2] == '/') {
606                         fullpath[len-2] = '\0';
607                 }
608
609                 notify_remove(fsp->conn->sconn->notify_ctx, fsp, fullpath);
610                 TALLOC_FREE(fsp->notify);
611         }
612
613         /* Ensure this event will never fire. */
614         TALLOC_FREE(fsp->update_write_time_event);
615
616         if (fsp->op != NULL) {
617                 fsp->op->compat = NULL;
618         }
619         TALLOC_FREE(fsp->op);
620
621         if ((req != NULL) && (fsp == req->chain_fsp)) {
622                 req->chain_fsp = NULL;
623         }
624
625         /*
626          * Clear all possible chained fsp
627          * pointers in the SMB2 request queue.
628          */
629         remove_smb2_chained_fsp(fsp);
630
631         /* Drop all remaining extensions. */
632         vfs_remove_all_fsp_extensions(fsp);
633
634         fsp_free(fsp);
635
636         DEBUG(5,("freed files structure %llu (%u used)\n",
637                  (unsigned long long)fnum, (unsigned int)sconn->num_files));
638 }
639
640 /****************************************************************************
641  Get an fsp from a packet given a 16 bit fnum.
642 ****************************************************************************/
643
644 files_struct *file_fsp(struct smb_request *req, uint16_t fid)
645 {
646         struct smbXsrv_open *op;
647         NTSTATUS status;
648         NTTIME now = 0;
649         files_struct *fsp;
650
651         if (req == NULL) {
652                 /*
653                  * We should never get here. req==NULL could in theory
654                  * only happen from internal opens with a non-zero
655                  * root_dir_fid. Internal opens just don't do that, at
656                  * least they are not supposed to do so. And if they
657                  * start to do so, they better fake up a smb_request
658                  * from which we get the right smbd_server_conn. While
659                  * this should never happen, let's return NULL here.
660                  */
661                 return NULL;
662         }
663
664         if (req->chain_fsp != NULL) {
665                 if (req->chain_fsp->fsp_flags.closing) {
666                         return NULL;
667                 }
668                 return req->chain_fsp;
669         }
670
671         if (req->xconn == NULL) {
672                 return NULL;
673         }
674
675         now = timeval_to_nttime(&req->request_time);
676
677         status = smb1srv_open_lookup(req->xconn,
678                                      fid, now, &op);
679         if (!NT_STATUS_IS_OK(status)) {
680                 return NULL;
681         }
682
683         fsp = op->compat;
684         if (fsp == NULL) {
685                 return NULL;
686         }
687
688         if (fsp->fsp_flags.closing) {
689                 return NULL;
690         }
691
692         req->chain_fsp = fsp;
693         return fsp;
694 }
695
696 struct files_struct *file_fsp_get(struct smbd_smb2_request *smb2req,
697                                   uint64_t persistent_id,
698                                   uint64_t volatile_id)
699 {
700         struct smbXsrv_open *op;
701         NTSTATUS status;
702         NTTIME now = 0;
703         struct files_struct *fsp;
704
705         now = timeval_to_nttime(&smb2req->request_time);
706
707         status = smb2srv_open_lookup(smb2req->xconn,
708                                      persistent_id, volatile_id,
709                                      now, &op);
710         if (!NT_STATUS_IS_OK(status)) {
711                 return NULL;
712         }
713
714         fsp = op->compat;
715         if (fsp == NULL) {
716                 return NULL;
717         }
718
719         if (smb2req->tcon == NULL) {
720                 return NULL;
721         }
722
723         if (smb2req->tcon->compat != fsp->conn) {
724                 return NULL;
725         }
726
727         if (smb2req->session == NULL) {
728                 return NULL;
729         }
730
731         if (smb2req->session->global->session_wire_id != fsp->vuid) {
732                 return NULL;
733         }
734
735         if (fsp->fsp_flags.closing) {
736                 return NULL;
737         }
738
739         return fsp;
740 }
741
742 struct files_struct *file_fsp_smb2(struct smbd_smb2_request *smb2req,
743                                    uint64_t persistent_id,
744                                    uint64_t volatile_id)
745 {
746         struct files_struct *fsp;
747
748         if (smb2req->compat_chain_fsp != NULL) {
749                 if (smb2req->compat_chain_fsp->fsp_flags.closing) {
750                         return NULL;
751                 }
752                 return smb2req->compat_chain_fsp;
753         }
754
755         fsp = file_fsp_get(smb2req, persistent_id, volatile_id);
756         if (fsp == NULL) {
757                 return NULL;
758         }
759
760         smb2req->compat_chain_fsp = fsp;
761         return fsp;
762 }
763
764 /****************************************************************************
765  Duplicate the file handle part for a DOS or FCB open.
766 ****************************************************************************/
767
768 NTSTATUS dup_file_fsp(
769         struct smb_request *req,
770         files_struct *from,
771         uint32_t access_mask,
772         uint32_t create_options,
773         files_struct *to)
774 {
775         /* this can never happen for print files */
776         SMB_ASSERT(from->print_file == NULL);
777
778         TALLOC_FREE(to->fh);
779
780         to->fh = from->fh;
781         to->fh->ref_count++;
782
783         to->file_id = from->file_id;
784         to->initial_allocation_size = from->initial_allocation_size;
785         to->file_pid = from->file_pid;
786         to->vuid = from->vuid;
787         to->open_time = from->open_time;
788         to->access_mask = access_mask;
789         to->oplock_type = from->oplock_type;
790         to->fsp_flags.can_lock = from->fsp_flags.can_lock;
791         to->fsp_flags.can_read = ((access_mask & FILE_READ_DATA) != 0);
792         to->fsp_flags.can_write =
793                 CAN_WRITE(from->conn) &&
794                 ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) != 0);
795         to->fsp_flags.modified = from->fsp_flags.modified;
796         to->fsp_flags.is_directory = from->fsp_flags.is_directory;
797         to->fsp_flags.aio_write_behind = from->fsp_flags.aio_write_behind;
798
799         return fsp_set_smb_fname(to, from->fsp_name);
800 }
801
802 /**
803  * Return a jenkins hash of a pathname on a connection.
804  */
805
806 NTSTATUS file_name_hash(connection_struct *conn,
807                         const char *name, uint32_t *p_name_hash)
808 {
809         char tmpbuf[PATH_MAX];
810         char *fullpath, *to_free;
811         ssize_t len;
812         TDB_DATA key;
813
814         /* Set the hash of the full pathname. */
815
816         len = full_path_tos(conn->connectpath, name, tmpbuf, sizeof(tmpbuf),
817                             &fullpath, &to_free);
818         if (len == -1) {
819                 return NT_STATUS_NO_MEMORY;
820         }
821         key = (TDB_DATA) { .dptr = (uint8_t *)fullpath, .dsize = len+1 };
822         *p_name_hash = tdb_jenkins_hash(&key);
823
824         DEBUG(10,("file_name_hash: %s hash 0x%x\n",
825                   fullpath,
826                 (unsigned int)*p_name_hash ));
827
828         TALLOC_FREE(to_free);
829         return NT_STATUS_OK;
830 }
831
832 /**
833  * The only way that the fsp->fsp_name field should ever be set.
834  */
835 NTSTATUS fsp_set_smb_fname(struct files_struct *fsp,
836                            const struct smb_filename *smb_fname_in)
837 {
838         struct smb_filename *smb_fname_new;
839
840         smb_fname_new = cp_smb_filename(fsp, smb_fname_in);
841         if (smb_fname_new == NULL) {
842                 return NT_STATUS_NO_MEMORY;
843         }
844
845         TALLOC_FREE(fsp->fsp_name);
846         fsp->fsp_name = smb_fname_new;
847
848         return file_name_hash(fsp->conn,
849                         smb_fname_str_dbg(fsp->fsp_name),
850                         &fsp->name_hash);
851 }
852
853 size_t fsp_fullbasepath(struct files_struct *fsp, char *buf, size_t buflen)
854 {
855         int len = 0;
856         char tmp_buf[1] = {'\0'};
857
858         /*
859          * Don't pass NULL buffer to snprintf (to satisfy static checker)
860          * Some callers will call this function with NULL for buf and
861          * 0 for buflen in order to get length of fullbasepatch (without
862          * needing to allocate or write to buf)
863          */
864         if (buf == NULL) {
865                 buf = tmp_buf;
866         }
867
868         len = snprintf(buf, buflen, "%s/%s", fsp->conn->connectpath,
869                        fsp->fsp_name->base_name);
870         SMB_ASSERT(len>0);
871
872         return len;
873 }