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