86ffab66baba3ee7bc19f4455529028600ccc3f4
[jelmer/samba4-debian.git] / source / ntvfs / simple / vfs_simple.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    simple NTVFS filesystem backend
5
6    Copyright (C) Andrew Tridgell 2003
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22 /*
23   this implements a very simple NTVFS filesystem backend. 
24   
25   this backend largely ignores the POSIX -> CIFS mappings, just doing absolutely
26   minimal work to give a working backend.
27 */
28
29 #include "includes.h"
30 #include "system/dir.h"
31 #include "system/filesys.h"
32 #include "svfs.h"
33 #include "system/time.h"
34 #include "dlinklist.h"
35 #include "smb_server/smb_server.h"
36
37 #ifndef O_DIRECTORY
38 #define O_DIRECTORY 0
39 #endif
40
41 #define CHECK_READ_ONLY(req) do { if (lp_readonly(req->tcon->service)) return NT_STATUS_ACCESS_DENIED; } while (0)
42
43 #ifndef HAVE_PREAD
44 static ssize_t pread(int __fd, void *__buf, size_t __nbytes, off_t __offset)
45 {
46         if (lseek(__fd, __offset, SEEK_SET) != __offset) {
47                 return -1;
48         }
49         return read(__fd, __buf, __nbytes);
50 }
51 #endif
52
53 #ifndef HAVE_PWRITE
54 static ssize_t pwrite(int __fd, const void *__buf, size_t __nbytes, off_t __offset)
55 {
56         if (lseek(__fd, __offset, SEEK_SET) != __offset) {
57                 return -1;
58         }
59         return write(__fd, __buf, __nbytes);
60 }
61 #endif
62
63 /*
64   connect to a share - used when a tree_connect operation comes
65   in. For a disk based backend we needs to ensure that the base
66   directory exists (tho it doesn't need to be accessible by the user,
67   that comes later)
68 */
69 static NTSTATUS svfs_connect(struct ntvfs_module_context *ntvfs,
70                              struct smbsrv_request *req, const char *sharename)
71 {
72         struct stat st;
73         struct smbsrv_tcon *tcon = req->tcon;
74         struct svfs_private *private;
75
76         private = talloc(tcon, struct svfs_private);
77
78         private->next_search_handle = 0;
79         private->connectpath = talloc_strdup(tcon, lp_pathname(tcon->service));
80         private->open_files = NULL;
81         private->search = NULL;
82
83         /* the directory must exist */
84         if (stat(private->connectpath, &st) != 0 || !S_ISDIR(st.st_mode)) {
85                 DEBUG(0,("'%s' is not a directory, when connecting to [%s]\n", 
86                          private->connectpath, sharename));
87                 return NT_STATUS_BAD_NETWORK_NAME;
88         }
89
90         tcon->fs_type = talloc_strdup(tcon, "NTFS");
91         tcon->dev_type = talloc_strdup(tcon, "A:");
92
93         ntvfs->private_data = private;
94
95         DEBUG(0,("WARNING: ntvfs simple: connect to share [%s] with ROOT privileges!!!\n",sharename));
96
97         return NT_STATUS_OK;
98 }
99
100 /*
101   disconnect from a share
102 */
103 static NTSTATUS svfs_disconnect(struct ntvfs_module_context *ntvfs,
104                                 struct smbsrv_tcon *tcon)
105 {
106         return NT_STATUS_OK;
107 }
108
109 /*
110   find open file handle given fd
111 */
112 static struct svfs_file *find_fd(struct svfs_private *private, int fd)
113 {
114         struct svfs_file *f;
115         for (f=private->open_files;f;f=f->next) {
116                 if (f->fd == fd) {
117                         return f;
118                 }
119         }
120         return NULL;
121 }
122
123 /*
124   delete a file - the dirtype specifies the file types to include in the search. 
125   The name can contain CIFS wildcards, but rarely does (except with OS/2 clients)
126 */
127 static NTSTATUS svfs_unlink(struct ntvfs_module_context *ntvfs,
128                             struct smbsrv_request *req, struct smb_unlink *unl)
129 {
130         char *unix_path;
131
132         CHECK_READ_ONLY(req);
133
134         unix_path = svfs_unix_path(ntvfs, req, unl->in.pattern);
135
136         /* ignoring wildcards ... */
137         if (unlink(unix_path) == -1) {
138                 return map_nt_error_from_unix(errno);
139         }
140
141         return NT_STATUS_OK;
142 }
143
144
145 /*
146   ioctl interface - we don't do any
147 */
148 static NTSTATUS svfs_ioctl(struct ntvfs_module_context *ntvfs,
149                            struct smbsrv_request *req, union smb_ioctl *io)
150 {
151         return NT_STATUS_INVALID_PARAMETER;
152 }
153
154 /*
155   check if a directory exists
156 */
157 static NTSTATUS svfs_chkpath(struct ntvfs_module_context *ntvfs,
158                              struct smbsrv_request *req, struct smb_chkpath *cp)
159 {
160         char *unix_path;
161         struct stat st;
162
163         unix_path = svfs_unix_path(ntvfs, req, cp->in.path);
164
165         if (stat(unix_path, &st) == -1) {
166                 return map_nt_error_from_unix(errno);
167         }
168
169         if (!S_ISDIR(st.st_mode)) {
170                 return NT_STATUS_NOT_A_DIRECTORY;
171         }
172
173         return NT_STATUS_OK;
174 }
175
176 /*
177   build a file_id from a stat struct
178 */
179 static uint64_t svfs_file_id(struct stat *st)
180 {
181         uint64_t ret = st->st_ino;
182         ret <<= 32;
183         ret |= st->st_dev;
184         return ret;
185 }
186
187 /*
188   approximately map a struct stat to a generic fileinfo struct
189 */
190 static NTSTATUS svfs_map_fileinfo(struct ntvfs_module_context *ntvfs,
191                                   struct smbsrv_request *req, union smb_fileinfo *info, 
192                                   struct stat *st, const char *unix_path)
193 {
194         struct svfs_dir *dir = NULL;
195         char *pattern = NULL;
196         int i;
197         const char *s, *short_name;
198
199         s = strrchr(unix_path, '/');
200         if (s) {
201                 short_name = s+1;
202         } else {
203                 short_name = "";
204         }
205
206         asprintf(&pattern, "%s:*", unix_path);
207         
208         if (pattern) {
209                 dir = svfs_list_unix(req, req, pattern);
210         }
211
212         unix_to_nt_time(&info->generic.out.create_time, st->st_ctime);
213         unix_to_nt_time(&info->generic.out.access_time, st->st_atime);
214         unix_to_nt_time(&info->generic.out.write_time,  st->st_mtime);
215         unix_to_nt_time(&info->generic.out.change_time, st->st_mtime);
216         info->generic.out.alloc_size = st->st_size;
217         info->generic.out.size = st->st_size;
218         info->generic.out.attrib = svfs_unix_to_dos_attrib(st->st_mode);
219         info->generic.out.alloc_size = st->st_blksize * st->st_blocks;
220         info->generic.out.nlink = st->st_nlink;
221         info->generic.out.directory = S_ISDIR(st->st_mode) ? 1 : 0;
222         info->generic.out.file_id = svfs_file_id(st);
223         /* REWRITE: TODO stuff in here */
224         info->generic.out.delete_pending = 0;
225         info->generic.out.ea_size = 0;
226         info->generic.out.num_eas = 0;
227         info->generic.out.fname.s = talloc_strdup(req, short_name);
228         info->generic.out.alt_fname.s = talloc_strdup(req, short_name);
229         info->generic.out.compressed_size = 0;
230         info->generic.out.format = 0;
231         info->generic.out.unit_shift = 0;
232         info->generic.out.chunk_shift = 0;
233         info->generic.out.cluster_shift = 0;
234         
235         info->generic.out.access_flags = 0;
236         info->generic.out.position = 0;
237         info->generic.out.mode = 0;
238         info->generic.out.alignment_requirement = 0;
239         info->generic.out.reparse_tag = 0;
240         info->generic.out.num_streams = 0;
241         /* setup a single data stream */
242         info->generic.out.num_streams = 1 + (dir?dir->count:0);
243         info->generic.out.streams = talloc_array(req, 
244                                                    struct stream_struct,
245                                                    info->generic.out.num_streams);
246         if (!info->generic.out.streams) {
247                 return NT_STATUS_NO_MEMORY;
248         }
249         info->generic.out.streams[0].size = st->st_size;
250         info->generic.out.streams[0].alloc_size = st->st_size;
251         info->generic.out.streams[0].stream_name.s = talloc_strdup(req,"::$DATA");
252
253         for (i=0;dir && i<dir->count;i++) {
254                 s = strchr(dir->files[i].name, ':');
255                 info->generic.out.streams[1+i].size = dir->files[i].st.st_size;
256                 info->generic.out.streams[1+i].alloc_size = dir->files[i].st.st_size;
257                 info->generic.out.streams[1+i].stream_name.s = s?s:dir->files[i].name;
258         }
259
260         return NT_STATUS_OK;
261 }
262
263 /*
264   return info on a pathname
265 */
266 static NTSTATUS svfs_qpathinfo(struct ntvfs_module_context *ntvfs,
267                                struct smbsrv_request *req, union smb_fileinfo *info)
268 {
269         char *unix_path;
270         struct stat st;
271
272         DEBUG(19,("svfs_qpathinfo: file %s level 0x%x\n", info->generic.in.fname, info->generic.level));
273         if (info->generic.level != RAW_FILEINFO_GENERIC) {
274                 return ntvfs_map_qpathinfo(req, info, ntvfs);
275         }
276         
277         unix_path = svfs_unix_path(ntvfs, req, info->generic.in.fname);
278         DEBUG(19,("svfs_qpathinfo: file %s\n", unix_path));
279         if (stat(unix_path, &st) == -1) {
280                 DEBUG(19,("svfs_qpathinfo: file %s errno=%d\n", unix_path, errno));
281                 return map_nt_error_from_unix(errno);
282         }
283         DEBUG(19,("svfs_qpathinfo: file %s, stat done\n", unix_path));
284         return svfs_map_fileinfo(ntvfs, req, info, &st, unix_path);
285 }
286
287 /*
288   query info on a open file
289 */
290 static NTSTATUS svfs_qfileinfo(struct ntvfs_module_context *ntvfs,
291                                struct smbsrv_request *req, union smb_fileinfo *info)
292 {
293         struct svfs_private *private = ntvfs->private_data;
294         struct svfs_file *f;
295         struct stat st;
296
297         if (info->generic.level != RAW_FILEINFO_GENERIC) {
298                 return ntvfs_map_qfileinfo(req, info, ntvfs);
299         }
300
301         f = find_fd(private, info->generic.in.fnum);
302         if (!f) {
303                 return NT_STATUS_INVALID_HANDLE;
304         }
305         
306         if (fstat(info->generic.in.fnum, &st) == -1) {
307                 return map_nt_error_from_unix(errno);
308         }
309
310         return svfs_map_fileinfo(ntvfs, req,info, &st, f->name);
311 }
312
313
314 /*
315   open a file
316 */
317 static NTSTATUS svfs_open(struct ntvfs_module_context *ntvfs,
318                           struct smbsrv_request *req, union smb_open *io)
319 {
320         struct svfs_private *private = ntvfs->private_data;
321         char *unix_path;
322         struct stat st;
323         int fd, flags;
324         struct svfs_file *f;
325         int create_flags, rdwr_flags;
326         
327         if (io->generic.level != RAW_OPEN_GENERIC) {
328                 return ntvfs_map_open(req, io, ntvfs);
329         }
330
331         if (lp_readonly(req->tcon->service)) {
332                 create_flags = 0;
333                 rdwr_flags = O_RDONLY;
334         } else {
335                 create_flags = O_CREAT;
336                 rdwr_flags = O_RDWR;
337         }
338
339         unix_path = svfs_unix_path(ntvfs, req, io->ntcreatex.in.fname);
340
341         switch (io->generic.in.open_disposition) {
342         case NTCREATEX_DISP_SUPERSEDE:
343         case NTCREATEX_DISP_OVERWRITE_IF:
344                 flags = create_flags | O_TRUNC;
345                 break;
346         case NTCREATEX_DISP_OPEN:
347         case NTCREATEX_DISP_OVERWRITE:
348                 flags = 0;
349                 break;
350         case NTCREATEX_DISP_CREATE:
351                 flags = create_flags | O_EXCL;
352                 break;
353         case NTCREATEX_DISP_OPEN_IF:
354                 flags = create_flags;
355                 break;
356         default:
357                 flags = 0;
358                 break;
359         }
360         
361         flags |= rdwr_flags;
362
363         if (io->generic.in.create_options & NTCREATEX_OPTIONS_DIRECTORY) {
364                 flags = O_RDONLY | O_DIRECTORY;
365                 if (lp_readonly(req->tcon->service)) {
366                         goto do_open;
367                 }
368                 switch (io->generic.in.open_disposition) {
369                 case NTCREATEX_DISP_CREATE:
370                         if (mkdir(unix_path, 0755) == -1) {
371                                 DEBUG(9,("svfs_open: mkdir %s errno=%d\n", unix_path, errno));
372                                 return map_nt_error_from_unix(errno);
373                         }
374                         break;
375                 case NTCREATEX_DISP_OPEN_IF:
376                         if (mkdir(unix_path, 0755) == -1 && errno != EEXIST) {
377                                 DEBUG(9,("svfs_open: mkdir %s errno=%d\n", unix_path, errno));
378                                 return map_nt_error_from_unix(errno);
379                         }
380                         break;
381                 }
382         }
383
384 do_open:
385         fd = open(unix_path, flags, 0644);
386         if (fd == -1) {
387                 return map_nt_error_from_unix(errno);
388         }
389
390         if (fstat(fd, &st) == -1) {
391                 DEBUG(9,("svfs_open: fstat errno=%d\n", errno));
392                 close(fd);
393                 return map_nt_error_from_unix(errno);
394         }
395
396         f = talloc(req->tcon, struct svfs_file);
397         f->fd = fd;
398         f->name = talloc_strdup(req->tcon, unix_path);
399
400         DLIST_ADD(private->open_files, f);
401
402         ZERO_STRUCT(io->generic.out);
403         
404         unix_to_nt_time(&io->generic.out.create_time, st.st_ctime);
405         unix_to_nt_time(&io->generic.out.access_time, st.st_atime);
406         unix_to_nt_time(&io->generic.out.write_time,  st.st_mtime);
407         unix_to_nt_time(&io->generic.out.change_time, st.st_mtime);
408         io->generic.out.fnum = fd;
409         io->generic.out.alloc_size = st.st_size;
410         io->generic.out.size = st.st_size;
411         io->generic.out.attrib = svfs_unix_to_dos_attrib(st.st_mode);
412         io->generic.out.is_directory = S_ISDIR(st.st_mode) ? 1 : 0;
413
414         return NT_STATUS_OK;
415 }
416
417 /*
418   create a directory
419 */
420 static NTSTATUS svfs_mkdir(struct ntvfs_module_context *ntvfs,
421                            struct smbsrv_request *req, union smb_mkdir *md)
422 {
423         char *unix_path;
424
425         CHECK_READ_ONLY(req);
426
427         if (md->generic.level != RAW_MKDIR_MKDIR) {
428                 return NT_STATUS_INVALID_LEVEL;
429         }
430
431         unix_path = svfs_unix_path(ntvfs, req, md->mkdir.in.path);
432
433         if (mkdir(unix_path, 0777) == -1) {
434                 return map_nt_error_from_unix(errno);
435         }
436
437         return NT_STATUS_OK;
438 }
439
440 /*
441   remove a directory
442 */
443 static NTSTATUS svfs_rmdir(struct ntvfs_module_context *ntvfs,
444                            struct smbsrv_request *req, struct smb_rmdir *rd)
445 {
446         char *unix_path;
447
448         CHECK_READ_ONLY(req);
449
450         unix_path = svfs_unix_path(ntvfs, req, rd->in.path);
451
452         if (rmdir(unix_path) == -1) {
453                 return map_nt_error_from_unix(errno);
454         }
455
456         return NT_STATUS_OK;
457 }
458
459 /*
460   rename a set of files
461 */
462 static NTSTATUS svfs_rename(struct ntvfs_module_context *ntvfs,
463                             struct smbsrv_request *req, union smb_rename *ren)
464 {
465         char *unix_path1, *unix_path2;
466
467         CHECK_READ_ONLY(req);
468
469         if (ren->generic.level != RAW_RENAME_RENAME) {
470                 return NT_STATUS_INVALID_LEVEL;
471         }
472
473         unix_path1 = svfs_unix_path(ntvfs, req, ren->rename.in.pattern1);
474         unix_path2 = svfs_unix_path(ntvfs, req, ren->rename.in.pattern2);
475
476         if (rename(unix_path1, unix_path2) == -1) {
477                 return map_nt_error_from_unix(errno);
478         }
479         
480         return NT_STATUS_OK;
481 }
482
483 /*
484   copy a set of files
485 */
486 static NTSTATUS svfs_copy(struct ntvfs_module_context *ntvfs,
487                           struct smbsrv_request *req, struct smb_copy *cp)
488 {
489         return NT_STATUS_NOT_SUPPORTED;
490 }
491
492 /*
493   read from a file
494 */
495 static NTSTATUS svfs_read(struct ntvfs_module_context *ntvfs,
496                           struct smbsrv_request *req, union smb_read *rd)
497 {
498         ssize_t ret;
499
500         if (rd->generic.level != RAW_READ_READX) {
501                 return NT_STATUS_NOT_SUPPORTED;
502         }
503
504         ret = pread(rd->readx.in.fnum, 
505                     rd->readx.out.data, 
506                     rd->readx.in.maxcnt,
507                     rd->readx.in.offset);
508         if (ret == -1) {
509                 return map_nt_error_from_unix(errno);
510         }
511
512         rd->readx.out.nread = ret;
513         rd->readx.out.remaining = 0; /* should fill this in? */
514         rd->readx.out.compaction_mode = 0; 
515
516         return NT_STATUS_OK;
517 }
518
519 /*
520   write to a file
521 */
522 static NTSTATUS svfs_write(struct ntvfs_module_context *ntvfs,
523                            struct smbsrv_request *req, union smb_write *wr)
524 {
525         ssize_t ret;
526
527         if (wr->generic.level != RAW_WRITE_WRITEX) {
528                 return ntvfs_map_write(req, wr, ntvfs);
529         }
530
531         CHECK_READ_ONLY(req);
532
533         ret = pwrite(wr->writex.in.fnum, 
534                      wr->writex.in.data, 
535                      wr->writex.in.count,
536                      wr->writex.in.offset);
537         if (ret == -1) {
538                 return map_nt_error_from_unix(errno);
539         }
540                 
541         wr->writex.out.nwritten = ret;
542         wr->writex.out.remaining = 0; /* should fill this in? */
543         
544         return NT_STATUS_OK;
545 }
546
547 /*
548   seek in a file
549 */
550 static NTSTATUS svfs_seek(struct ntvfs_module_context *ntvfs,
551                           struct smbsrv_request *req, struct smb_seek *io)
552 {
553         return NT_STATUS_NOT_SUPPORTED;
554 }
555
556 /*
557   flush a file
558 */
559 static NTSTATUS svfs_flush(struct ntvfs_module_context *ntvfs,
560                            struct smbsrv_request *req, struct smb_flush *io)
561 {
562         fsync(io->in.fnum);
563         return NT_STATUS_OK;
564 }
565
566 /*
567   close a file
568 */
569 static NTSTATUS svfs_close(struct ntvfs_module_context *ntvfs,
570                            struct smbsrv_request *req, union smb_close *io)
571 {
572         struct svfs_private *private = ntvfs->private_data;
573         struct svfs_file *f;
574
575         if (io->generic.level != RAW_CLOSE_CLOSE) {
576                 /* we need a mapping function */
577                 return NT_STATUS_INVALID_LEVEL;
578         }
579
580         f = find_fd(private, io->close.in.fnum);
581         if (!f) {
582                 return NT_STATUS_INVALID_HANDLE;
583         }
584
585         if (close(io->close.in.fnum) == -1) {
586                 return map_nt_error_from_unix(errno);
587         }
588
589         DLIST_REMOVE(private->open_files, f);
590         talloc_free(f->name);
591         talloc_free(f);
592
593         return NT_STATUS_OK;
594 }
595
596 /*
597   exit - closing files
598 */
599 static NTSTATUS svfs_exit(struct ntvfs_module_context *ntvfs,
600                           struct smbsrv_request *req)
601 {
602         return NT_STATUS_NOT_SUPPORTED;
603 }
604
605 /*
606   logoff - closing files
607 */
608 static NTSTATUS svfs_logoff(struct ntvfs_module_context *ntvfs,
609                             struct smbsrv_request *req)
610 {
611         return NT_STATUS_NOT_SUPPORTED;
612 }
613
614 /*
615   setup for an async call
616 */
617 static NTSTATUS svfs_async_setup(struct ntvfs_module_context *ntvfs,
618                                  struct smbsrv_request *req, 
619                                  void *private)
620 {
621         return NT_STATUS_OK;
622 }
623
624 /*
625   cancel an async call
626 */
627 static NTSTATUS svfs_cancel(struct ntvfs_module_context *ntvfs, struct smbsrv_request *req)
628 {
629         return NT_STATUS_UNSUCCESSFUL;
630 }
631
632 /*
633   lock a byte range
634 */
635 static NTSTATUS svfs_lock(struct ntvfs_module_context *ntvfs,
636                           struct smbsrv_request *req, union smb_lock *lck)
637 {
638         DEBUG(0,("REWRITE: not doing byte range locking!\n"));
639         return NT_STATUS_OK;
640 }
641
642 /*
643   set info on a pathname
644 */
645 static NTSTATUS svfs_setpathinfo(struct ntvfs_module_context *ntvfs,
646                                  struct smbsrv_request *req, union smb_setfileinfo *st)
647 {
648         CHECK_READ_ONLY(req);
649
650         return NT_STATUS_NOT_SUPPORTED;
651 }
652
653 /*
654   set info on a open file
655 */
656 static NTSTATUS svfs_setfileinfo(struct ntvfs_module_context *ntvfs,
657                                  struct smbsrv_request *req, 
658                                  union smb_setfileinfo *info)
659 {
660         struct utimbuf unix_times;
661         int fd;
662
663         CHECK_READ_ONLY(req);
664                 
665         switch (info->generic.level) {
666         case RAW_SFILEINFO_END_OF_FILE_INFO:
667         case RAW_SFILEINFO_END_OF_FILE_INFORMATION:
668                 if (ftruncate(info->end_of_file_info.file.fnum, 
669                               info->end_of_file_info.in.size) == -1) {
670                         return map_nt_error_from_unix(errno);
671                 }
672                 break;
673         case RAW_SFILEINFO_SETATTRE:
674                 unix_times.actime = info->setattre.in.access_time;
675                 unix_times.modtime = info->setattre.in.write_time;
676                 fd = info->setattre.file.fnum;
677         
678                 if (unix_times.actime == 0 && unix_times.modtime == 0) {
679                         break;
680                 } 
681
682                 /* set modify time = to access time if modify time was 0 */
683                 if (unix_times.actime != 0 && unix_times.modtime == 0) {
684                         unix_times.modtime = unix_times.actime;
685                 }
686
687                 /* Set the date on this file */
688                 if (svfs_file_utime(fd, &unix_times) != 0) {
689                         return NT_STATUS_ACCESS_DENIED;
690                 }
691                 break;
692         default:
693                 DEBUG(2,("svfs_setfileinfo: level %d not implemented\n", 
694                          info->generic.level));
695                 return NT_STATUS_NOT_IMPLEMENTED;
696         }
697         return NT_STATUS_OK;
698 }
699
700
701 /*
702   return filesystem space info
703 */
704 static NTSTATUS svfs_fsinfo(struct ntvfs_module_context *ntvfs,
705                             struct smbsrv_request *req, union smb_fsinfo *fs)
706 {
707         struct svfs_private *private = ntvfs->private_data;
708         struct stat st;
709
710         if (fs->generic.level != RAW_QFS_GENERIC) {
711                 return ntvfs_map_fsinfo(req, fs, ntvfs);
712         }
713
714         if (sys_fsusage(private->connectpath, 
715                         &fs->generic.out.blocks_free, 
716                         &fs->generic.out.blocks_total) == -1) {
717                 return map_nt_error_from_unix(errno);
718         }
719
720         fs->generic.out.block_size = 512;
721
722         if (stat(private->connectpath, &st) != 0) {
723                 return NT_STATUS_DISK_CORRUPT_ERROR;
724         }
725         
726         fs->generic.out.fs_id = st.st_ino;
727         unix_to_nt_time(&fs->generic.out.create_time, st.st_ctime);
728         fs->generic.out.serial_number = st.st_ino;
729         fs->generic.out.fs_attr = 0;
730         fs->generic.out.max_file_component_length = 255;
731         fs->generic.out.device_type = 0;
732         fs->generic.out.device_characteristics = 0;
733         fs->generic.out.quota_soft = 0;
734         fs->generic.out.quota_hard = 0;
735         fs->generic.out.quota_flags = 0;
736         fs->generic.out.volume_name = talloc_strdup(req, lp_servicename(req->tcon->service));
737         fs->generic.out.fs_type = req->tcon->fs_type;
738
739         return NT_STATUS_OK;
740 }
741
742 #if 0
743 /*
744   return filesystem attribute info
745 */
746 static NTSTATUS svfs_fsattr(struct ntvfs_module_context *ntvfs,
747                             struct smbsrv_request *req, union smb_fsattr *fs)
748 {
749         struct stat st;
750         struct svfs_private *private = ntvfs->private_data;
751
752         if (fs->generic.level != RAW_FSATTR_GENERIC) {
753                 return ntvfs_map_fsattr(req, fs, ntvfs);
754         }
755
756         if (stat(private->connectpath, &st) == -1) {
757                 return map_nt_error_from_unix(errno);
758         }
759
760         unix_to_nt_time(&fs->generic.out.create_time, st.st_ctime);
761         fs->generic.out.fs_attr = 
762                 FILE_CASE_PRESERVED_NAMES | 
763                 FILE_CASE_SENSITIVE_SEARCH | 
764                 FILE_PERSISTENT_ACLS;
765         fs->generic.out.max_file_component_length = 255;
766         fs->generic.out.serial_number = 1;
767         fs->generic.out.fs_type = talloc_strdup(req, "NTFS");
768         fs->generic.out.volume_name = talloc_strdup(req, 
769                                                     lp_servicename(req->tcon->service));
770
771         return NT_STATUS_OK;
772 }
773 #endif
774
775 /*
776   return print queue info
777 */
778 static NTSTATUS svfs_lpq(struct ntvfs_module_context *ntvfs,
779                          struct smbsrv_request *req, union smb_lpq *lpq)
780 {
781         return NT_STATUS_NOT_SUPPORTED;
782 }
783
784 /* 
785    list files in a directory matching a wildcard pattern
786 */
787 static NTSTATUS svfs_search_first(struct ntvfs_module_context *ntvfs,
788                                   struct smbsrv_request *req, union smb_search_first *io, 
789                                   void *search_private, 
790                                   BOOL (*callback)(void *, union smb_search_data *))
791 {
792         struct svfs_dir *dir;
793         int i;
794         struct svfs_private *private = ntvfs->private_data;
795         struct search_state *search;
796         union smb_search_data file;
797         uint_t max_count;
798
799         if (io->generic.level != RAW_SEARCH_BOTH_DIRECTORY_INFO) {
800                 return NT_STATUS_NOT_SUPPORTED;
801         }
802
803         search = talloc_zero(private, struct search_state);
804         if (!search) {
805                 return NT_STATUS_NO_MEMORY;
806         }
807
808         max_count = io->t2ffirst.in.max_count;
809
810         dir = svfs_list(ntvfs, req, io->t2ffirst.in.pattern);
811         if (!dir) {
812                 return NT_STATUS_FOOBAR;
813         }
814
815         search->handle = private->next_search_handle;
816         search->dir = dir;
817
818         if (dir->count < max_count) {
819                 max_count = dir->count;
820         }
821
822         for (i=0; i < max_count;i++) {
823                 ZERO_STRUCT(file);
824                 unix_to_nt_time(&file.both_directory_info.create_time, dir->files[i].st.st_ctime);
825                 unix_to_nt_time(&file.both_directory_info.access_time, dir->files[i].st.st_atime);
826                 unix_to_nt_time(&file.both_directory_info.write_time,  dir->files[i].st.st_mtime);
827                 unix_to_nt_time(&file.both_directory_info.change_time, dir->files[i].st.st_mtime);
828                 file.both_directory_info.name.s = dir->files[i].name;
829                 file.both_directory_info.short_name.s = dir->files[i].name;
830                 file.both_directory_info.size = dir->files[i].st.st_size;
831                 file.both_directory_info.attrib = svfs_unix_to_dos_attrib(dir->files[i].st.st_mode);
832
833                 if (!callback(search_private, &file)) {
834                         break;
835                 }
836         }
837
838         search->current_index = i;
839
840         io->t2ffirst.out.count = i;
841         io->t2ffirst.out.handle = search->handle;
842         io->t2ffirst.out.end_of_search = (i == dir->count) ? 1 : 0;
843
844         /* work out if we are going to keep the search state */
845         if ((io->t2ffirst.in.flags & FLAG_TRANS2_FIND_CLOSE) ||
846             ((io->t2ffirst.in.flags & FLAG_TRANS2_FIND_CLOSE_IF_END) && (i == dir->count))) {
847                 talloc_free(search);
848         } else {
849                 private->next_search_handle++;
850                 DLIST_ADD(private->search, search);
851         }
852
853         return NT_STATUS_OK;
854 }
855
856 /* continue a search */
857 static NTSTATUS svfs_search_next(struct ntvfs_module_context *ntvfs,
858                                  struct smbsrv_request *req, union smb_search_next *io, 
859                                  void *search_private, 
860                                  BOOL (*callback)(void *, union smb_search_data *))
861 {
862         struct svfs_dir *dir;
863         int i;
864         struct svfs_private *private = ntvfs->private_data;
865         struct search_state *search;
866         union smb_search_data file;
867         uint_t max_count;
868
869         if (io->generic.level != RAW_SEARCH_BOTH_DIRECTORY_INFO) {
870                 return NT_STATUS_NOT_SUPPORTED;
871         }
872
873         for (search=private->search; search; search = search->next) {
874                 if (search->handle == io->t2fnext.in.handle) break;
875         }
876         
877         if (!search) {
878                 /* we didn't find the search handle */
879                 return NT_STATUS_FOOBAR;
880         }
881
882         dir = search->dir;
883
884         /* the client might be asking for something other than just continuing
885            with the search */
886         if (!(io->t2fnext.in.flags & FLAG_TRANS2_FIND_CONTINUE) &&
887             (io->t2fnext.in.flags & FLAG_TRANS2_FIND_REQUIRE_RESUME) &&
888             io->t2fnext.in.last_name && *io->t2fnext.in.last_name) {
889                 /* look backwards first */
890                 for (i=search->current_index; i > 0; i--) {
891                         if (strcmp(io->t2fnext.in.last_name, dir->files[i-1].name) == 0) {
892                                 search->current_index = i;
893                                 goto found;
894                         }
895                 }
896
897                 /* then look forwards */
898                 for (i=search->current_index+1; i <= dir->count; i++) {
899                         if (strcmp(io->t2fnext.in.last_name, dir->files[i-1].name) == 0) {
900                                 search->current_index = i;
901                                 goto found;
902                         }
903                 }
904         }
905
906 found:  
907         max_count = search->current_index + io->t2fnext.in.max_count;
908
909         if (max_count > dir->count) {
910                 max_count = dir->count;
911         }
912
913         for (i = search->current_index; i < max_count;i++) {
914                 ZERO_STRUCT(file);
915                 unix_to_nt_time(&file.both_directory_info.create_time, dir->files[i].st.st_ctime);
916                 unix_to_nt_time(&file.both_directory_info.access_time, dir->files[i].st.st_atime);
917                 unix_to_nt_time(&file.both_directory_info.write_time,  dir->files[i].st.st_mtime);
918                 unix_to_nt_time(&file.both_directory_info.change_time, dir->files[i].st.st_mtime);
919                 file.both_directory_info.name.s = dir->files[i].name;
920                 file.both_directory_info.short_name.s = dir->files[i].name;
921                 file.both_directory_info.size = dir->files[i].st.st_size;
922                 file.both_directory_info.attrib = svfs_unix_to_dos_attrib(dir->files[i].st.st_mode);
923
924                 if (!callback(search_private, &file)) {
925                         break;
926                 }
927         }
928
929         io->t2fnext.out.count = i - search->current_index;
930         io->t2fnext.out.end_of_search = (i == dir->count) ? 1 : 0;
931
932         search->current_index = i;
933
934         /* work out if we are going to keep the search state */
935         if ((io->t2fnext.in.flags & FLAG_TRANS2_FIND_CLOSE) ||
936             ((io->t2fnext.in.flags & FLAG_TRANS2_FIND_CLOSE_IF_END) && (i == dir->count))) {
937                 DLIST_REMOVE(private->search, search);
938                 talloc_free(search);
939         }
940
941         return NT_STATUS_OK;
942 }
943
944 /* close a search */
945 static NTSTATUS svfs_search_close(struct ntvfs_module_context *ntvfs,
946                                   struct smbsrv_request *req, union smb_search_close *io)
947 {
948         struct svfs_private *private = ntvfs->private_data;
949         struct search_state *search;
950
951         for (search=private->search; search; search = search->next) {
952                 if (search->handle == io->findclose.in.handle) break;
953         }
954         
955         if (!search) {
956                 /* we didn't find the search handle */
957                 return NT_STATUS_FOOBAR;
958         }
959
960         DLIST_REMOVE(private->search, search);
961         talloc_free(search);
962
963         return NT_STATUS_OK;
964 }
965
966 /* SMBtrans - not used on file shares */
967 static NTSTATUS svfs_trans(struct ntvfs_module_context *ntvfs,
968                            struct smbsrv_request *req, struct smb_trans2 *trans2)
969 {
970         return NT_STATUS_ACCESS_DENIED;
971 }
972
973
974 /*
975   initialialise the POSIX disk backend, registering ourselves with the ntvfs subsystem
976  */
977 NTSTATUS ntvfs_simple_init(void)
978 {
979         NTSTATUS ret;
980         struct ntvfs_ops ops;
981
982         ZERO_STRUCT(ops);
983
984         /* fill in all the operations */
985         ops.connect = svfs_connect;
986         ops.disconnect = svfs_disconnect;
987         ops.unlink = svfs_unlink;
988         ops.chkpath = svfs_chkpath;
989         ops.qpathinfo = svfs_qpathinfo;
990         ops.setpathinfo = svfs_setpathinfo;
991         ops.openfile = svfs_open;
992         ops.mkdir = svfs_mkdir;
993         ops.rmdir = svfs_rmdir;
994         ops.rename = svfs_rename;
995         ops.copy = svfs_copy;
996         ops.ioctl = svfs_ioctl;
997         ops.read = svfs_read;
998         ops.write = svfs_write;
999         ops.seek = svfs_seek;
1000         ops.flush = svfs_flush; 
1001         ops.close = svfs_close;
1002         ops.exit = svfs_exit;
1003         ops.lock = svfs_lock;
1004         ops.setfileinfo = svfs_setfileinfo;
1005         ops.qfileinfo = svfs_qfileinfo;
1006         ops.fsinfo = svfs_fsinfo;
1007         ops.lpq = svfs_lpq;
1008         ops.search_first = svfs_search_first;
1009         ops.search_next = svfs_search_next;
1010         ops.search_close = svfs_search_close;
1011         ops.trans = svfs_trans;
1012         ops.logoff = svfs_logoff;
1013         ops.async_setup = svfs_async_setup;
1014         ops.cancel = svfs_cancel;
1015
1016         /* register ourselves with the NTVFS subsystem. We register
1017            under names 'simple'
1018         */
1019
1020         ops.type = NTVFS_DISK;
1021         ops.name = "simple";
1022         ret = ntvfs_register(&ops);
1023
1024         if (!NT_STATUS_IS_OK(ret)) {
1025                 DEBUG(0,("Failed to register simple backend with name: %s!\n",
1026                          ops.name));
1027         }
1028
1029         return ret;
1030 }