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