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