r3323: more warning reductions
[bbaumbach/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                 return map_nt_error_from_unix(errno);
266         }
267         DEBUG(19,("svfs_qpathinfo: file %s, stat done\n", unix_path));
268         return svfs_map_fileinfo(ntvfs, req, info, &st, unix_path);
269 }
270
271 /*
272   query info on a open file
273 */
274 static NTSTATUS svfs_qfileinfo(struct ntvfs_module_context *ntvfs,
275                                struct smbsrv_request *req, union smb_fileinfo *info)
276 {
277         struct svfs_private *private = ntvfs->private_data;
278         struct svfs_file *f;
279         struct stat st;
280
281         if (info->generic.level != RAW_FILEINFO_GENERIC) {
282                 return ntvfs_map_qfileinfo(req, info, ntvfs);
283         }
284
285         f = find_fd(private, info->generic.in.fnum);
286         if (!f) {
287                 return NT_STATUS_INVALID_HANDLE;
288         }
289         
290         if (fstat(info->generic.in.fnum, &st) == -1) {
291                 return map_nt_error_from_unix(errno);
292         }
293
294         return svfs_map_fileinfo(ntvfs, req,info, &st, f->name);
295 }
296
297
298 /*
299   open a file
300 */
301 static NTSTATUS svfs_open(struct ntvfs_module_context *ntvfs,
302                           struct smbsrv_request *req, union smb_open *io)
303 {
304         struct svfs_private *private = ntvfs->private_data;
305         char *unix_path;
306         struct stat st;
307         int fd, flags;
308         struct svfs_file *f;
309         int create_flags, rdwr_flags;
310         
311         if (io->generic.level != RAW_OPEN_GENERIC) {
312                 return ntvfs_map_open(req, io, ntvfs);
313         }
314
315         if (lp_readonly(req->tcon->service)) {
316                 create_flags = 0;
317                 rdwr_flags = O_RDONLY;
318         } else {
319                 create_flags = O_CREAT;
320                 rdwr_flags = O_RDWR;
321         }
322
323         unix_path = svfs_unix_path(ntvfs, req, io->ntcreatex.in.fname);
324
325         switch (io->generic.in.open_disposition) {
326         case NTCREATEX_DISP_SUPERSEDE:
327         case NTCREATEX_DISP_OVERWRITE_IF:
328                 flags = create_flags | O_TRUNC;
329                 break;
330         case NTCREATEX_DISP_OPEN:
331         case NTCREATEX_DISP_OVERWRITE:
332                 flags = 0;
333                 break;
334         case NTCREATEX_DISP_CREATE:
335                 flags = create_flags | O_EXCL;
336                 break;
337         case NTCREATEX_DISP_OPEN_IF:
338                 flags = create_flags;
339                 break;
340         default:
341                 flags = 0;
342                 break;
343         }
344         
345         flags |= rdwr_flags;
346
347         if (io->generic.in.create_options & NTCREATEX_OPTIONS_DIRECTORY) {
348                 flags = O_RDONLY | O_DIRECTORY;
349                 if (lp_readonly(req->tcon->service)) {
350                         goto do_open;
351                 }
352                 switch (io->generic.in.open_disposition) {
353                 case NTCREATEX_DISP_CREATE:
354                         if (mkdir(unix_path, 0755) == -1) {
355                                 DEBUG(9,("svfs_open: mkdir %s errno=%d\n", unix_path, errno));
356                                 return map_nt_error_from_unix(errno);
357                         }
358                         break;
359                 case NTCREATEX_DISP_OPEN_IF:
360                         if (mkdir(unix_path, 0755) == -1 && errno != EEXIST) {
361                                 DEBUG(9,("svfs_open: mkdir %s errno=%d\n", unix_path, errno));
362                                 return map_nt_error_from_unix(errno);
363                         }
364                         break;
365                 }
366         }
367
368 do_open:
369         fd = open(unix_path, flags, 0644);
370         if (fd == -1) {
371                 return map_nt_error_from_unix(errno);
372         }
373
374         if (fstat(fd, &st) == -1) {
375                 DEBUG(9,("svfs_open: fstat errno=%d\n", errno));
376                 close(fd);
377                 return map_nt_error_from_unix(errno);
378         }
379
380         f = talloc_p(req->tcon, struct svfs_file);
381         f->fd = fd;
382         f->name = talloc_strdup(req->tcon, unix_path);
383
384         DLIST_ADD(private->open_files, f);
385
386         ZERO_STRUCT(io->generic.out);
387         
388         unix_to_nt_time(&io->generic.out.create_time, st.st_ctime);
389         unix_to_nt_time(&io->generic.out.access_time, st.st_atime);
390         unix_to_nt_time(&io->generic.out.write_time,  st.st_mtime);
391         unix_to_nt_time(&io->generic.out.change_time, st.st_mtime);
392         io->generic.out.fnum = fd;
393         io->generic.out.alloc_size = st.st_size;
394         io->generic.out.size = st.st_size;
395         io->generic.out.attrib = svfs_unix_to_dos_attrib(st.st_mode);
396         io->generic.out.is_directory = S_ISDIR(st.st_mode) ? 1 : 0;
397
398         return NT_STATUS_OK;
399 }
400
401 /*
402   create a directory
403 */
404 static NTSTATUS svfs_mkdir(struct ntvfs_module_context *ntvfs,
405                            struct smbsrv_request *req, union smb_mkdir *md)
406 {
407         char *unix_path;
408
409         CHECK_READ_ONLY(req);
410
411         if (md->generic.level != RAW_MKDIR_MKDIR) {
412                 return NT_STATUS_INVALID_LEVEL;
413         }
414
415         unix_path = svfs_unix_path(ntvfs, req, md->mkdir.in.path);
416
417         if (mkdir(unix_path, 0777) == -1) {
418                 return map_nt_error_from_unix(errno);
419         }
420
421         return NT_STATUS_OK;
422 }
423
424 /*
425   remove a directory
426 */
427 static NTSTATUS svfs_rmdir(struct ntvfs_module_context *ntvfs,
428                            struct smbsrv_request *req, struct smb_rmdir *rd)
429 {
430         char *unix_path;
431
432         CHECK_READ_ONLY(req);
433
434         unix_path = svfs_unix_path(ntvfs, req, rd->in.path);
435
436         if (rmdir(unix_path) == -1) {
437                 return map_nt_error_from_unix(errno);
438         }
439
440         return NT_STATUS_OK;
441 }
442
443 /*
444   rename a set of files
445 */
446 static NTSTATUS svfs_rename(struct ntvfs_module_context *ntvfs,
447                             struct smbsrv_request *req, union smb_rename *ren)
448 {
449         char *unix_path1, *unix_path2;
450
451         CHECK_READ_ONLY(req);
452
453         if (ren->generic.level != RAW_RENAME_RENAME) {
454                 return NT_STATUS_INVALID_LEVEL;
455         }
456
457         unix_path1 = svfs_unix_path(ntvfs, req, ren->rename.in.pattern1);
458         unix_path2 = svfs_unix_path(ntvfs, req, ren->rename.in.pattern2);
459
460         if (rename(unix_path1, unix_path2) == -1) {
461                 return map_nt_error_from_unix(errno);
462         }
463         
464         return NT_STATUS_OK;
465 }
466
467 /*
468   copy a set of files
469 */
470 static NTSTATUS svfs_copy(struct ntvfs_module_context *ntvfs,
471                           struct smbsrv_request *req, struct smb_copy *cp)
472 {
473         return NT_STATUS_NOT_SUPPORTED;
474 }
475
476 /*
477   read from a file
478 */
479 static NTSTATUS svfs_read(struct ntvfs_module_context *ntvfs,
480                           struct smbsrv_request *req, union smb_read *rd)
481 {
482         ssize_t ret;
483
484         if (rd->generic.level != RAW_READ_READX) {
485                 return NT_STATUS_NOT_SUPPORTED;
486         }
487
488         ret = pread(rd->readx.in.fnum, 
489                     rd->readx.out.data, 
490                     rd->readx.in.maxcnt,
491                     rd->readx.in.offset);
492         if (ret == -1) {
493                 return map_nt_error_from_unix(errno);
494         }
495
496         rd->readx.out.nread = ret;
497         rd->readx.out.remaining = 0; /* should fill this in? */
498         rd->readx.out.compaction_mode = 0; 
499
500         return NT_STATUS_OK;
501 }
502
503 /*
504   write to a file
505 */
506 static NTSTATUS svfs_write(struct ntvfs_module_context *ntvfs,
507                            struct smbsrv_request *req, union smb_write *wr)
508 {
509         ssize_t ret;
510
511         if (wr->generic.level != RAW_WRITE_WRITEX) {
512                 return ntvfs_map_write(req, wr, ntvfs);
513         }
514
515         CHECK_READ_ONLY(req);
516
517         ret = pwrite(wr->writex.in.fnum, 
518                      wr->writex.in.data, 
519                      wr->writex.in.count,
520                      wr->writex.in.offset);
521         if (ret == -1) {
522                 return map_nt_error_from_unix(errno);
523         }
524                 
525         wr->writex.out.nwritten = ret;
526         wr->writex.out.remaining = 0; /* should fill this in? */
527         
528         return NT_STATUS_OK;
529 }
530
531 /*
532   seek in a file
533 */
534 static NTSTATUS svfs_seek(struct ntvfs_module_context *ntvfs,
535                           struct smbsrv_request *req, struct smb_seek *io)
536 {
537         return NT_STATUS_NOT_SUPPORTED;
538 }
539
540 /*
541   flush a file
542 */
543 static NTSTATUS svfs_flush(struct ntvfs_module_context *ntvfs,
544                            struct smbsrv_request *req, struct smb_flush *io)
545 {
546         fsync(io->in.fnum);
547         return NT_STATUS_OK;
548 }
549
550 /*
551   close a file
552 */
553 static NTSTATUS svfs_close(struct ntvfs_module_context *ntvfs,
554                            struct smbsrv_request *req, union smb_close *io)
555 {
556         struct svfs_private *private = ntvfs->private_data;
557         struct svfs_file *f;
558
559         if (io->generic.level != RAW_CLOSE_CLOSE) {
560                 /* we need a mapping function */
561                 return NT_STATUS_INVALID_LEVEL;
562         }
563
564         f = find_fd(private, io->close.in.fnum);
565         if (!f) {
566                 return NT_STATUS_INVALID_HANDLE;
567         }
568
569         if (close(io->close.in.fnum) == -1) {
570                 return map_nt_error_from_unix(errno);
571         }
572
573         DLIST_REMOVE(private->open_files, f);
574         talloc_free(f->name);
575         talloc_free(f);
576
577         return NT_STATUS_OK;
578 }
579
580 /*
581   exit - closing files
582 */
583 static NTSTATUS svfs_exit(struct ntvfs_module_context *ntvfs,
584                           struct smbsrv_request *req)
585 {
586         return NT_STATUS_NOT_SUPPORTED;
587 }
588
589 /*
590   logoff - closing files
591 */
592 static NTSTATUS svfs_logoff(struct ntvfs_module_context *ntvfs,
593                             struct smbsrv_request *req)
594 {
595         return NT_STATUS_NOT_SUPPORTED;
596 }
597
598 /*
599   setup for an async call
600 */
601 static NTSTATUS svfs_async_setup(struct ntvfs_module_context *ntvfs,
602                                  struct smbsrv_request *req, 
603                                  void *private)
604 {
605         return NT_STATUS_OK;
606 }
607
608 /*
609   lock a byte range
610 */
611 static NTSTATUS svfs_lock(struct ntvfs_module_context *ntvfs,
612                           struct smbsrv_request *req, union smb_lock *lck)
613 {
614         DEBUG(0,("REWRITE: not doing byte range locking!\n"));
615         return NT_STATUS_OK;
616 }
617
618 /*
619   set info on a pathname
620 */
621 static NTSTATUS svfs_setpathinfo(struct ntvfs_module_context *ntvfs,
622                                  struct smbsrv_request *req, union smb_setfileinfo *st)
623 {
624         CHECK_READ_ONLY(req);
625
626         return NT_STATUS_NOT_SUPPORTED;
627 }
628
629 /*
630   set info on a open file
631 */
632 static NTSTATUS svfs_setfileinfo(struct ntvfs_module_context *ntvfs,
633                                  struct smbsrv_request *req, 
634                                  union smb_setfileinfo *info)
635 {
636         struct utimbuf unix_times;
637         int fd;
638
639         CHECK_READ_ONLY(req);
640                 
641         switch (info->generic.level) {
642         case RAW_SFILEINFO_END_OF_FILE_INFO:
643         case RAW_SFILEINFO_END_OF_FILE_INFORMATION:
644                 if (ftruncate(info->end_of_file_info.file.fnum, 
645                               info->end_of_file_info.in.size) == -1) {
646                         return map_nt_error_from_unix(errno);
647                 }
648                 break;
649         case RAW_SFILEINFO_SETATTRE:
650                 unix_times.actime = info->setattre.in.access_time;
651                 unix_times.modtime = info->setattre.in.write_time;
652                 fd = info->setattre.file.fnum;
653         
654                 if (unix_times.actime == 0 && unix_times.modtime == 0) {
655                         break;
656                 } 
657
658                 /* set modify time = to access time if modify time was 0 */
659                 if (unix_times.actime != 0 && unix_times.modtime == 0) {
660                         unix_times.modtime = unix_times.actime;
661                 }
662
663                 /* Set the date on this file */
664                 if (svfs_file_utime(fd, &unix_times) != 0) {
665                         return NT_STATUS_ACCESS_DENIED;
666                 }
667                 break;
668         }
669         return NT_STATUS_OK;
670 }
671
672
673 /*
674   return filesystem space info
675 */
676 static NTSTATUS svfs_fsinfo(struct ntvfs_module_context *ntvfs,
677                             struct smbsrv_request *req, union smb_fsinfo *fs)
678 {
679         struct svfs_private *private = ntvfs->private_data;
680         struct stat st;
681
682         if (fs->generic.level != RAW_QFS_GENERIC) {
683                 return ntvfs_map_fsinfo(req, fs, ntvfs);
684         }
685
686         if (sys_fsusage(private->connectpath, 
687                         &fs->generic.out.blocks_free, 
688                         &fs->generic.out.blocks_total) == -1) {
689                 return map_nt_error_from_unix(errno);
690         }
691
692         fs->generic.out.block_size = 512;
693
694         if (stat(private->connectpath, &st) != 0) {
695                 return NT_STATUS_DISK_CORRUPT_ERROR;
696         }
697         
698         fs->generic.out.fs_id = st.st_ino;
699         unix_to_nt_time(&fs->generic.out.create_time, st.st_ctime);
700         fs->generic.out.serial_number = st.st_ino;
701         fs->generic.out.fs_attr = 0;
702         fs->generic.out.max_file_component_length = 255;
703         fs->generic.out.device_type = 0;
704         fs->generic.out.device_characteristics = 0;
705         fs->generic.out.quota_soft = 0;
706         fs->generic.out.quota_hard = 0;
707         fs->generic.out.quota_flags = 0;
708         fs->generic.out.volume_name = talloc_strdup(req, lp_servicename(req->tcon->service));
709         fs->generic.out.fs_type = req->tcon->fs_type;
710
711         return NT_STATUS_OK;
712 }
713
714 #if 0
715 /*
716   return filesystem attribute info
717 */
718 static NTSTATUS svfs_fsattr(struct ntvfs_module_context *ntvfs,
719                             struct smbsrv_request *req, union smb_fsattr *fs)
720 {
721         struct stat st;
722         struct svfs_private *private = ntvfs->private_data;
723
724         if (fs->generic.level != RAW_FSATTR_GENERIC) {
725                 return ntvfs_map_fsattr(req, fs, ntvfs);
726         }
727
728         if (stat(private->connectpath, &st) == -1) {
729                 return map_nt_error_from_unix(errno);
730         }
731
732         unix_to_nt_time(&fs->generic.out.create_time, st.st_ctime);
733         fs->generic.out.fs_attr = 
734                 FILE_CASE_PRESERVED_NAMES | 
735                 FILE_CASE_SENSITIVE_SEARCH | 
736                 FILE_PERSISTENT_ACLS;
737         fs->generic.out.max_file_component_length = 255;
738         fs->generic.out.serial_number = 1;
739         fs->generic.out.fs_type = talloc_strdup(req, "NTFS");
740         fs->generic.out.volume_name = talloc_strdup(req, 
741                                                     lp_servicename(req->tcon->service));
742
743         return NT_STATUS_OK;
744 }
745 #endif
746
747 /*
748   return print queue info
749 */
750 static NTSTATUS svfs_lpq(struct ntvfs_module_context *ntvfs,
751                          struct smbsrv_request *req, union smb_lpq *lpq)
752 {
753         return NT_STATUS_NOT_SUPPORTED;
754 }
755
756 /* 
757    list files in a directory matching a wildcard pattern
758 */
759 static NTSTATUS svfs_search_first(struct ntvfs_module_context *ntvfs,
760                                   struct smbsrv_request *req, union smb_search_first *io, 
761                                   void *search_private, 
762                                   BOOL (*callback)(void *, union smb_search_data *))
763 {
764         struct svfs_dir *dir;
765         int i;
766         struct svfs_private *private = ntvfs->private_data;
767         struct search_state *search;
768         union smb_search_data file;
769         uint_t max_count;
770
771         if (io->generic.level != RAW_SEARCH_BOTH_DIRECTORY_INFO) {
772                 return NT_STATUS_NOT_SUPPORTED;
773         }
774
775         search = talloc_zero_p(private, struct search_state);
776         if (!search) {
777                 return NT_STATUS_NO_MEMORY;
778         }
779
780         max_count = io->t2ffirst.in.max_count;
781
782         dir = svfs_list(ntvfs, req, io->t2ffirst.in.pattern);
783         if (!dir) {
784                 return NT_STATUS_FOOBAR;
785         }
786
787         search->handle = private->next_search_handle;
788         search->dir = dir;
789
790         if (dir->count < max_count) {
791                 max_count = dir->count;
792         }
793
794         for (i=0; i < max_count;i++) {
795                 ZERO_STRUCT(file);
796                 unix_to_nt_time(&file.both_directory_info.create_time, dir->files[i].st.st_ctime);
797                 unix_to_nt_time(&file.both_directory_info.access_time, dir->files[i].st.st_atime);
798                 unix_to_nt_time(&file.both_directory_info.write_time,  dir->files[i].st.st_mtime);
799                 unix_to_nt_time(&file.both_directory_info.change_time, dir->files[i].st.st_mtime);
800                 file.both_directory_info.name.s = dir->files[i].name;
801                 file.both_directory_info.short_name.s = dir->files[i].name;
802                 file.both_directory_info.size = dir->files[i].st.st_size;
803                 file.both_directory_info.attrib = svfs_unix_to_dos_attrib(dir->files[i].st.st_mode);
804
805                 if (!callback(search_private, &file)) {
806                         break;
807                 }
808         }
809
810         search->current_index = i;
811
812         io->t2ffirst.out.count = i;
813         io->t2ffirst.out.handle = search->handle;
814         io->t2ffirst.out.end_of_search = (i == dir->count) ? 1 : 0;
815
816         /* work out if we are going to keep the search state */
817         if ((io->t2ffirst.in.flags & FLAG_TRANS2_FIND_CLOSE) ||
818             ((io->t2ffirst.in.flags & FLAG_TRANS2_FIND_CLOSE_IF_END) && (i == dir->count))) {
819                 talloc_free(search);
820         } else {
821                 private->next_search_handle++;
822                 DLIST_ADD(private->search, search);
823         }
824
825         return NT_STATUS_OK;
826 }
827
828 /* continue a search */
829 static NTSTATUS svfs_search_next(struct ntvfs_module_context *ntvfs,
830                                  struct smbsrv_request *req, union smb_search_next *io, 
831                                  void *search_private, 
832                                  BOOL (*callback)(void *, union smb_search_data *))
833 {
834         struct svfs_dir *dir;
835         int i;
836         struct svfs_private *private = ntvfs->private_data;
837         struct search_state *search;
838         union smb_search_data file;
839         uint_t max_count;
840
841         if (io->generic.level != RAW_SEARCH_BOTH_DIRECTORY_INFO) {
842                 return NT_STATUS_NOT_SUPPORTED;
843         }
844
845         for (search=private->search; search; search = search->next) {
846                 if (search->handle == io->t2fnext.in.handle) break;
847         }
848         
849         if (!search) {
850                 /* we didn't find the search handle */
851                 return NT_STATUS_FOOBAR;
852         }
853
854         dir = search->dir;
855
856         /* the client might be asking for something other than just continuing
857            with the search */
858         if (!(io->t2fnext.in.flags & FLAG_TRANS2_FIND_CONTINUE) &&
859             (io->t2fnext.in.flags & FLAG_TRANS2_FIND_REQUIRE_RESUME) &&
860             io->t2fnext.in.last_name && *io->t2fnext.in.last_name) {
861                 /* look backwards first */
862                 for (i=search->current_index; i > 0; i--) {
863                         if (strcmp(io->t2fnext.in.last_name, dir->files[i-1].name) == 0) {
864                                 search->current_index = i;
865                                 goto found;
866                         }
867                 }
868
869                 /* then look forwards */
870                 for (i=search->current_index+1; i <= dir->count; i++) {
871                         if (strcmp(io->t2fnext.in.last_name, dir->files[i-1].name) == 0) {
872                                 search->current_index = i;
873                                 goto found;
874                         }
875                 }
876         }
877
878 found:  
879         max_count = search->current_index + io->t2fnext.in.max_count;
880
881         if (max_count > dir->count) {
882                 max_count = dir->count;
883         }
884
885         for (i = search->current_index; i < max_count;i++) {
886                 ZERO_STRUCT(file);
887                 unix_to_nt_time(&file.both_directory_info.create_time, dir->files[i].st.st_ctime);
888                 unix_to_nt_time(&file.both_directory_info.access_time, dir->files[i].st.st_atime);
889                 unix_to_nt_time(&file.both_directory_info.write_time,  dir->files[i].st.st_mtime);
890                 unix_to_nt_time(&file.both_directory_info.change_time, dir->files[i].st.st_mtime);
891                 file.both_directory_info.name.s = dir->files[i].name;
892                 file.both_directory_info.short_name.s = dir->files[i].name;
893                 file.both_directory_info.size = dir->files[i].st.st_size;
894                 file.both_directory_info.attrib = svfs_unix_to_dos_attrib(dir->files[i].st.st_mode);
895
896                 if (!callback(search_private, &file)) {
897                         break;
898                 }
899         }
900
901         io->t2fnext.out.count = i - search->current_index;
902         io->t2fnext.out.end_of_search = (i == dir->count) ? 1 : 0;
903
904         search->current_index = i;
905
906         /* work out if we are going to keep the search state */
907         if ((io->t2fnext.in.flags & FLAG_TRANS2_FIND_CLOSE) ||
908             ((io->t2fnext.in.flags & FLAG_TRANS2_FIND_CLOSE_IF_END) && (i == dir->count))) {
909                 DLIST_REMOVE(private->search, search);
910                 talloc_free(search);
911         }
912
913         return NT_STATUS_OK;
914 }
915
916 /* close a search */
917 static NTSTATUS svfs_search_close(struct ntvfs_module_context *ntvfs,
918                                   struct smbsrv_request *req, union smb_search_close *io)
919 {
920         struct svfs_private *private = ntvfs->private_data;
921         struct search_state *search;
922
923         for (search=private->search; search; search = search->next) {
924                 if (search->handle == io->findclose.in.handle) break;
925         }
926         
927         if (!search) {
928                 /* we didn't find the search handle */
929                 return NT_STATUS_FOOBAR;
930         }
931
932         DLIST_REMOVE(private->search, search);
933         talloc_free(search);
934
935         return NT_STATUS_OK;
936 }
937
938 /* SMBtrans - not used on file shares */
939 static NTSTATUS svfs_trans(struct ntvfs_module_context *ntvfs,
940                            struct smbsrv_request *req, struct smb_trans2 *trans2)
941 {
942         return NT_STATUS_ACCESS_DENIED;
943 }
944
945
946 /*
947   initialialise the POSIX disk backend, registering ourselves with the ntvfs subsystem
948  */
949 NTSTATUS ntvfs_simple_init(void)
950 {
951         NTSTATUS ret;
952         struct ntvfs_ops ops;
953
954         ZERO_STRUCT(ops);
955
956         /* fill in all the operations */
957         ops.connect = svfs_connect;
958         ops.disconnect = svfs_disconnect;
959         ops.unlink = svfs_unlink;
960         ops.chkpath = svfs_chkpath;
961         ops.qpathinfo = svfs_qpathinfo;
962         ops.setpathinfo = svfs_setpathinfo;
963         ops.open = svfs_open;
964         ops.mkdir = svfs_mkdir;
965         ops.rmdir = svfs_rmdir;
966         ops.rename = svfs_rename;
967         ops.copy = svfs_copy;
968         ops.ioctl = svfs_ioctl;
969         ops.read = svfs_read;
970         ops.write = svfs_write;
971         ops.seek = svfs_seek;
972         ops.flush = svfs_flush; 
973         ops.close = svfs_close;
974         ops.exit = svfs_exit;
975         ops.lock = svfs_lock;
976         ops.setfileinfo = svfs_setfileinfo;
977         ops.qfileinfo = svfs_qfileinfo;
978         ops.fsinfo = svfs_fsinfo;
979         ops.lpq = svfs_lpq;
980         ops.search_first = svfs_search_first;
981         ops.search_next = svfs_search_next;
982         ops.search_close = svfs_search_close;
983         ops.trans = svfs_trans;
984         ops.logoff = svfs_logoff;
985         ops.async_setup = svfs_async_setup;
986
987         /* register ourselves with the NTVFS subsystem. We register
988            under names 'simple'
989         */
990
991         ops.type = NTVFS_DISK;
992         ops.name = "simple";
993         ret = register_backend("ntvfs", &ops);
994
995         if (!NT_STATUS_IS_OK(ret)) {
996                 DEBUG(0,("Failed to register simple backend with name: %s!\n",
997                          ops.name));
998         }
999
1000         return ret;
1001 }