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