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