2 Unix SMB/CIFS implementation.
4 simple NTVFS filesystem backend
6 Copyright (C) Andrew Tridgell 2003
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.
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.
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.
23 this implements a very simple NTVFS filesystem backend.
25 this backend largely ignores the POSIX -> CIFS mappings, just doing absolutely
26 minimal work to give a working backend.
33 connect to a share - used when a tree_connect operation comes
34 in. For a disk based backend we needs to ensure that the base
35 directory exists (tho it doesn't need to be accessible by the user,
38 static NTSTATUS svfs_connect(struct request_context *req, const char *sharename)
41 struct tcon_context *conn = req->conn;
42 struct svfs_private *private;
44 conn->ntvfs_private = talloc(conn->mem_ctx, sizeof(struct svfs_private));
46 private = conn->ntvfs_private;
48 private->connectpath = talloc_strdup(conn->mem_ctx, lp_pathname(conn->service));
50 /* the directory must exist */
51 if (stat(private->connectpath, &st) != 0 || !S_ISDIR(st.st_mode)) {
52 DEBUG(0,("'%s' is not a directory, when connecting to [%s]\n",
53 private->connectpath, sharename));
54 return NT_STATUS_BAD_NETWORK_NAME;
57 conn->fs_type = talloc_strdup(conn->mem_ctx, "NTFS");
58 conn->dev_type = talloc_strdup(conn->mem_ctx, "A:");
64 disconnect from a share
66 static NTSTATUS svfs_disconnect(struct request_context *req)
72 delete a file - the dirtype specifies the file types to include in the search.
73 The name can contain CIFS wildcards, but rarely does (except with OS/2 clients)
75 static NTSTATUS svfs_unlink(struct request_context *req, struct smb_unlink *unl)
79 unix_path = svfs_unix_path(req, unl->in.pattern);
81 /* ignoring wildcards ... */
82 if (unlink(unix_path) == -1) {
83 return map_nt_error_from_unix(errno);
91 ioctl interface - we don't do any
93 static NTSTATUS svfs_ioctl(struct request_context *req, struct smb_ioctl *io)
95 return NT_STATUS_INVALID_PARAMETER;
99 check if a directory exists
101 static NTSTATUS svfs_chkpath(struct request_context *req, struct smb_chkpath *cp)
106 unix_path = svfs_unix_path(req, cp->in.path);
108 if (stat(unix_path, &st) == -1) {
109 return map_nt_error_from_unix(errno);
112 if (!S_ISDIR(st.st_mode)) {
113 return NT_STATUS_NOT_A_DIRECTORY;
120 approximately map a struct stat to a fileinfo struct
122 static NTSTATUS map_fileinfo(struct request_context *req, union smb_fileinfo *info, struct stat *st)
124 switch (info->generic.level) {
125 case SMB_FILEINFO_NETWORK_OPEN_INFORMATION:
126 unix_to_nt_time(&info->netopen.out.create_time, st->st_ctime);
127 unix_to_nt_time(&info->netopen.out.access_time, st->st_atime);
128 unix_to_nt_time(&info->netopen.out.write_time, st->st_mtime);
129 unix_to_nt_time(&info->netopen.out.change_time, st->st_mtime);
130 info->netopen.out.alloc_size = st->st_size;
131 info->netopen.out.size = st->st_size;
132 info->netopen.out.attrib = svfs_file_attrib(st);
133 info->netopen.out.unknown = 0;
136 case SMB_FILEINFO_ALL_INFO:
137 unix_to_nt_time(&info->all_info.out.create_time, st->st_ctime);
138 unix_to_nt_time(&info->all_info.out.access_time, st->st_atime);
139 unix_to_nt_time(&info->all_info.out.write_time, st->st_mtime);
140 unix_to_nt_time(&info->all_info.out.change_time, st->st_mtime);
141 info->all_info.out.attrib = svfs_file_attrib(st);
142 info->all_info.out.alloc_size = st->st_size;
143 info->all_info.out.size = st->st_size;
144 info->all_info.out.nlink = st->st_nlink;
145 info->all_info.out.delete_pending = 0;
146 info->all_info.out.directory = S_ISDIR(st->st_mode) ? 1 : 0;
147 info->all_info.out.index_number = st->st_ino;
148 info->all_info.out.ea_size = 0;
149 info->all_info.out.access_flags = 0xd01BF; /* what is this!? */
150 info->all_info.out.current_offset = 0;
151 info->all_info.out.open_mode = 0; /* what do do put here!? */
152 info->all_info.out.alignment_requirement = 0; /* what do do put here!? */
153 info->all_info.out.fname = talloc_strdup(req->mem_ctx, "TODO - STORE FILENAME");
156 case SMB_FILEINFO_BASIC:
157 unix_to_nt_time(&info->basic.out.create_time, st->st_ctime);
158 unix_to_nt_time(&info->basic.out.access_time, st->st_atime);
159 unix_to_nt_time(&info->basic.out.write_time, st->st_mtime);
160 unix_to_nt_time(&info->basic.out.change_time, st->st_mtime);
161 info->basic.out.attrib = svfs_file_attrib(st);
164 case SMB_FILEINFO_INFO_STANDARD:
165 info->info_standard.out.create_time = st->st_ctime;
166 info->info_standard.out.access_time = st->st_atime;
167 info->info_standard.out.write_time = st->st_mtime;
168 info->info_standard.out.size = st->st_size;
169 info->info_standard.out.alloc_size = st->st_size;
170 info->info_standard.out.attrib = svfs_file_attrib(st);
173 case SMB_FILEINFO_INFO_STANDARD_EA:
174 info->info_standard_ea.out.create_time = st->st_ctime;
175 info->info_standard_ea.out.access_time = st->st_atime;
176 info->info_standard_ea.out.write_time = st->st_mtime;
177 info->info_standard_ea.out.size = st->st_size;
178 info->info_standard_ea.out.alloc_size = st->st_size;
179 info->info_standard_ea.out.attrib = svfs_file_attrib(st);
180 info->info_standard_ea.out.ea_size = 0;
183 case SMB_FILEINFO_STANDARD_INFO:
184 info->standard_info.out.alloc_size = st->st_size;
185 info->standard_info.out.size = st->st_size;
186 info->standard_info.out.nlink = st->st_nlink;
187 info->standard_info.out.delete_pending = 0;
188 info->standard_info.out.directory = S_ISDIR(st->st_mode) ? 1 : 0;
189 info->standard_info.out.unknown = 0;
192 case SMB_FILEINFO_INTERNAL:
193 info->internal.out.device = st->st_dev;
194 info->internal.out.device = st->st_ino;
197 case SMB_FILEINFO_EA:
198 info->ea.out.unknown = 0;
201 case SMB_FILEINFO_ATTRIB_TAGINFO:
202 info->tag.out.attrib = svfs_file_attrib(st);
203 info->tag.out.reparse_tag = 0;
206 case SMB_FILEINFO_STREAM:
207 /* setup a single data stream */
208 info->stream.out.num_streams = 1;
209 info->stream.out.streams = talloc(req->mem_ctx, sizeof(info->stream.out.streams[0]));
210 if (!info->stream.out.streams) {
211 return NT_STATUS_NO_MEMORY;
213 info->stream.out.streams[0].size = st->st_size;
214 info->stream.out.streams[0].alloc_size = st->st_size;
215 info->stream.out.streams[0].stream_name = talloc_strdup(req->mem_ctx,"::$DATA");
219 return NT_STATUS_INVALID_LEVEL;
223 return info on a pathname
225 static NTSTATUS svfs_qpathinfo(struct request_context *req, union smb_fileinfo *info)
230 unix_path = svfs_unix_path(req, info->basic.in.fname);
232 if (stat(unix_path, &st) == -1) {
233 return map_nt_error_from_unix(errno);
236 return map_fileinfo(req, info, &st);
240 query info on a open file
242 static NTSTATUS svfs_qfileinfo(struct request_context *req, union smb_fileinfo *info)
246 if (fstat(info->generic.in.fnum, &st) == -1) {
247 return map_nt_error_from_unix(errno);
250 return map_fileinfo(req, info, &st);
255 set info on a pathname
257 static NTSTATUS svfs_setpathinfo(struct request_context *req, union smb_setfileinfo *st)
259 return NT_STATUS_NOT_SUPPORTED;
265 static NTSTATUS svfs_open(struct request_context *req, union smb_open *io)
271 if (io->generic.level != SMB_OPEN_GENERIC) {
272 return ntvfs_map_open(req, io);
275 unix_path = svfs_unix_path(req, io->ntcreatex.in.fname);
277 switch (io->generic.in.open_disposition) {
279 flags = O_RDWR | O_CREAT | O_TRUNC;
285 flags = O_RDWR | O_CREAT | O_EXCL;
288 flags = O_RDWR | O_CREAT;
293 case FILE_OVERWRITE_IF:
294 flags = O_RDWR | O_CREAT | O_TRUNC;
301 if (io->generic.in.create_options & FILE_DIRECTORY_FILE) {
302 flags = O_RDONLY | O_DIRECTORY;
303 switch (io->generic.in.open_disposition) {
305 if (mkdir(unix_path, 0755) == -1) {
306 return map_nt_error_from_unix(errno);
310 if (mkdir(unix_path, 0755) == -1 && errno != EEXIST) {
311 return map_nt_error_from_unix(errno);
317 fd = open(unix_path, flags, 0644);
319 return map_nt_error_from_unix(errno);
322 if (fstat(fd, &st) == -1) {
324 return map_nt_error_from_unix(errno);
327 ZERO_STRUCT(io->generic.out);
329 unix_to_nt_time(&io->generic.out.create_time, st.st_ctime);
330 unix_to_nt_time(&io->generic.out.access_time, st.st_atime);
331 unix_to_nt_time(&io->generic.out.write_time, st.st_mtime);
332 unix_to_nt_time(&io->generic.out.change_time, st.st_mtime);
333 io->generic.out.fnum = fd;
334 io->generic.out.alloc_size = st.st_size;
335 io->generic.out.size = st.st_size;
336 io->generic.out.file_attr = svfs_file_attrib(&st);
337 io->generic.out.is_directory = S_ISDIR(st.st_mode) ? 1 : 0;
345 static NTSTATUS svfs_mkdir(struct request_context *req, union smb_mkdir *md)
349 if (md->generic.level != RAW_MKDIR_MKDIR) {
350 return NT_STATUS_INVALID_LEVEL;
353 unix_path = svfs_unix_path(req, md->mkdir.in.path);
355 if (mkdir(unix_path, 0777) == -1) {
356 return map_nt_error_from_unix(errno);
365 static NTSTATUS svfs_rmdir(struct request_context *req, struct smb_rmdir *rd)
369 unix_path = svfs_unix_path(req, rd->in.path);
371 if (rmdir(unix_path) == -1) {
372 return map_nt_error_from_unix(errno);
379 rename a set of files
381 static NTSTATUS svfs_rename(struct request_context *req, union smb_rename *ren)
383 char *unix_path1, *unix_path2;
385 unix_path1 = svfs_unix_path(req, ren->in.pattern1);
386 unix_path2 = svfs_unix_path(req, ren->in.pattern2);
388 if (rename(unix_path1, unix_path2) != 0) {
389 return map_nt_error_from_unix(errno);
398 static NTSTATUS svfs_copy(struct request_context *req, struct smb_copy *cp)
400 return NT_STATUS_NOT_SUPPORTED;
406 static NTSTATUS svfs_read(struct request_context *req, union smb_read *rd)
410 if (rd->generic.level != SMB_READ_READX) {
411 return NT_STATUS_NOT_SUPPORTED;
414 ret = pread(rd->readx.in.fnum,
417 rd->readx.in.offset);
419 return map_nt_error_from_unix(errno);
422 rd->readx.out.nread = ret;
423 rd->readx.out.remaining = 0; /* should fill this in? */
431 static NTSTATUS svfs_write(struct request_context *req, union smb_write *wr)
435 switch (wr->generic.level) {
436 case SMB_WRITE_WRITEX:
437 ret = pwrite(wr->writex.in.fnum,
440 wr->writex.in.offset);
442 return map_nt_error_from_unix(errno);
445 wr->writex.out.nwritten = ret;
446 wr->writex.out.remaining = 0; /* should fill this in? */
450 case SMB_WRITE_WRITE:
451 if (wr->write.in.count == 0) {
453 ret = ftruncate(wr->write.in.fnum, wr->write.in.offset);
455 ret = pwrite(wr->write.in.fnum,
458 wr->write.in.offset);
461 return map_nt_error_from_unix(errno);
464 wr->write.out.nwritten = ret;
469 return NT_STATUS_NOT_SUPPORTED;
475 static NTSTATUS svfs_seek(struct request_context *req, struct smb_seek *io)
477 return NT_STATUS_NOT_SUPPORTED;
483 static NTSTATUS svfs_flush(struct request_context *req, struct smb_flush *io)
492 static NTSTATUS svfs_close(struct request_context *req, union smb_close *io)
494 if (io->generic.level != SMB_CLOSE_CLOSE) {
495 /* we need a mapping function */
496 return NT_STATUS_INVALID_LEVEL;
499 if (close(io->close.in.fnum) != 0) {
500 return map_nt_error_from_unix(errno);
507 exit - closing files?
509 static NTSTATUS svfs_exit(struct request_context *req)
511 return NT_STATUS_NOT_SUPPORTED;
517 static NTSTATUS svfs_lock(struct request_context *req, union smb_lock *lck)
519 DEBUG(0,("REWRITE: not doing byte range locking!\n"));
524 set info on a open file
526 static NTSTATUS svfs_setfileinfo(struct request_context *req,
527 union smb_setfileinfo *info)
529 DEBUG(0,("REWRITE: svfs_setfileinfo: not doing setfileinfo level %d\n",
530 info->generic.level));
531 switch (info->generic.level) {
532 case SMB_SETFILEINFO_BASIC:
533 info->basic.out.ea_error_offset = 0;
535 case SMB_SETFILEINFO_END_OF_FILE:
536 if (ftruncate(info->eof.in.fnum, info->eof.in.size) != 0) {
537 return map_nt_error_from_unix(errno);
539 info->eof.out.ea_error_offset = 0;
541 case SMB_SETFILEINFO_ALLOCATION:
542 info->eof.out.ea_error_offset = 0;
550 return filesystem space info
552 static NTSTATUS svfs_fsinfo(struct request_context *req, union smb_fsinfo *fs)
554 struct svfs_private *private = req->conn->ntvfs_private;
556 if (fs->generic.level != SMB_FSINFO_GENERIC) {
557 return ntvfs_map_fsinfo(req, fs);
560 if (sys_fsusage(private->connectpath,
561 &fs->generic.out.blocks_free,
562 &fs->generic.out.blocks_total) == -1) {
563 return map_nt_error_from_unix(errno);
566 fs->generic.out.block_size = 512;
572 return filesystem attribute info
574 static NTSTATUS svfs_fsattr(struct request_context *req, union smb_fsattr *fs)
577 struct svfs_private *private = req->conn->ntvfs_private;
579 if (fs->generic.level != SMB_FSATTR_GENERIC) {
580 return ntvfs_map_fsattr(req, fs);
583 if (stat(private->connectpath, &st) != 0) {
584 return map_nt_error_from_unix(errno);
587 unix_to_nt_time(&fs->generic.out.create_time, st.st_ctime);
588 fs->generic.out.fs_attr =
589 FILE_CASE_PRESERVED_NAMES |
590 FILE_CASE_SENSITIVE_SEARCH |
591 FILE_PERSISTENT_ACLS;
592 fs->generic.out.max_file_component_length = 255;
593 fs->generic.out.serial_number = 1;
594 fs->generic.out.fs_type = talloc_strdup(req->mem_ctx, "NTFS");
595 fs->generic.out.volume_name = talloc_strdup(req->mem_ctx,
596 lp_servicename(req->conn->service));
602 return print queue info
604 static NTSTATUS svfs_lpq(struct request_context *req, union smb_lpq *lpq)
606 return NT_STATUS_NOT_SUPPORTED;
610 list files in a directory matching a wildcard pattern
612 NTSTATUS svfs_search_first(struct request_context *req, union smb_search_first *io,
613 void *search_private,
614 BOOL (*callback)(void *, union smb_search_data *))
616 struct svfs_dir *dir;
618 struct svfs_private *private = req->conn->ntvfs_private;
619 struct search_state *search;
620 union smb_search_data file;
624 if (io->generic.level != SMB_SEARCH_T2FFIRST_BOTH) {
625 return NT_STATUS_NOT_SUPPORTED;
628 mem_ctx = talloc_init("svfs_search");
630 search = talloc_zero(mem_ctx, sizeof(struct search_state));
632 return NT_STATUS_NO_MEMORY;
635 max_count = io->t2ffirst.in.max_count;
637 dir = svfs_list(mem_ctx, req, io->t2ffirst.in.pattern);
639 talloc_destroy_pool(mem_ctx);
640 return NT_STATUS_FOOBAR;
643 search->mem_ctx = mem_ctx;
644 search->handle = private->next_search_handle;
647 if (dir->count < max_count) {
648 max_count = dir->count;
651 for (i=0; i < max_count;i++) {
653 unix_to_nt_time(&file.both.create_time, dir->files[i].st.st_ctime);
654 unix_to_nt_time(&file.both.access_time, dir->files[i].st.st_atime);
655 unix_to_nt_time(&file.both.write_time, dir->files[i].st.st_mtime);
656 unix_to_nt_time(&file.both.change_time, dir->files[i].st.st_mtime);
657 file.both.name = dir->files[i].name;
658 file.both.short_name = dir->files[i].name;
659 file.both.size = dir->files[i].st.st_size;
660 file.both.ex_attrib = svfs_file_attrib(&dir->files[i].st);
662 if (!callback(search_private, &file)) {
667 search->current_index = i;
669 io->t2ffirst.out.count = i;
670 io->t2ffirst.out.handle = search->handle;
671 io->t2ffirst.out.end_of_search = (i == dir->count) ? 1 : 0;
673 /* work out if we are going to keep the search state */
674 if ((io->t2ffirst.in.flags & FLAG_TRANS2_FIND_CLOSE) ||
675 ((io->t2ffirst.in.flags & FLAG_TRANS2_FIND_CLOSE_IF_END) && (i == dir->count))) {
676 talloc_destroy(search->mem_ctx);
678 private->next_search_handle++;
679 DLIST_ADD(private->search, search);
685 /* continue a search */
686 NTSTATUS svfs_search_next(struct request_context *req, union smb_search_next *io,
687 void *search_private,
688 BOOL (*callback)(void *, union smb_search_data *))
690 struct svfs_dir *dir;
692 struct svfs_private *private = req->conn->ntvfs_private;
693 struct search_state *search;
694 union smb_search_data file;
697 if (io->generic.level != SMB_SEARCH_T2FFIRST_BOTH) {
698 return NT_STATUS_NOT_SUPPORTED;
701 for (search=private->search; search; search = search->next) {
702 if (search->handle == io->t2fnext.in.handle) break;
706 /* we didn't find the search handle */
707 return NT_STATUS_FOOBAR;
712 /* the client might be asking for something other than just continuing
714 if (!(io->t2fnext.in.flags & FLAG_TRANS2_FIND_CONTINUE) &&
715 (io->t2fnext.in.flags & FLAG_TRANS2_FIND_REQUIRE_RESUME) &&
716 io->t2fnext.in.last_name && *io->t2fnext.in.last_name) {
717 /* look backwards first */
718 for (i=search->current_index; i > 0; i--) {
719 if (strcmp(io->t2fnext.in.last_name, dir->files[i-1].name) == 0) {
720 search->current_index = i;
725 /* then look forwards */
726 for (i=search->current_index+1; i <= dir->count; i++) {
727 if (strcmp(io->t2fnext.in.last_name, dir->files[i-1].name) == 0) {
728 search->current_index = i;
735 max_count = search->current_index + io->t2fnext.in.max_count;
737 if (max_count > dir->count) {
738 max_count = dir->count;
741 for (i = search->current_index; i < max_count;i++) {
743 unix_to_nt_time(&file.both.create_time, dir->files[i].st.st_ctime);
744 unix_to_nt_time(&file.both.access_time, dir->files[i].st.st_atime);
745 unix_to_nt_time(&file.both.write_time, dir->files[i].st.st_mtime);
746 unix_to_nt_time(&file.both.change_time, dir->files[i].st.st_mtime);
747 file.both.name = dir->files[i].name;
748 file.both.short_name = dir->files[i].name;
749 file.both.size = dir->files[i].st.st_size;
750 file.both.ex_attrib = svfs_file_attrib(&dir->files[i].st);
752 if (!callback(search_private, &file)) {
757 io->t2fnext.out.count = i - search->current_index;
758 io->t2fnext.out.end_of_search = (i == dir->count) ? 1 : 0;
760 search->current_index = i;
762 /* work out if we are going to keep the search state */
763 if ((io->t2fnext.in.flags & FLAG_TRANS2_FIND_CLOSE) ||
764 ((io->t2fnext.in.flags & FLAG_TRANS2_FIND_CLOSE_IF_END) && (i == dir->count))) {
765 DLIST_REMOVE(private->search, search);
766 talloc_destroy(search->mem_ctx);
773 NTSTATUS svfs_search_close(struct request_context *req, union smb_search_close *io)
775 struct svfs_private *private = req->conn->ntvfs_private;
776 struct search_state *search;
778 for (search=private->search; search; search = search->next) {
779 if (search->handle == io->findclose.in.handle) break;
783 /* we didn't find the search handle */
784 return NT_STATUS_FOOBAR;
787 DLIST_REMOVE(private->search, search);
788 talloc_destroy(search->mem_ctx);
795 initialialise the POSIX disk backend, registering ourselves with the ntvfs subsystem
797 BOOL posix_vfs_init(void)
800 struct ntvfs_ops ops;
804 /* fill in all the operations */
805 ops.connect = svfs_connect;
806 ops.disconnect = svfs_disconnect;
807 ops.unlink = svfs_unlink;
808 ops.chkpath = svfs_chkpath;
809 ops.qpathinfo = svfs_qpathinfo;
810 ops.setpathinfo = svfs_setpathinfo;
811 ops.open = svfs_open;
812 ops.mkdir = svfs_mkdir;
813 ops.rmdir = svfs_rmdir;
814 ops.rename = svfs_rename;
815 ops.copy = svfs_copy;
816 ops.ioctl = svfs_ioctl;
817 ops.read = svfs_read;
818 ops.write = svfs_write;
819 ops.seek = svfs_seek;
820 ops.flush = svfs_flush;
821 ops.close = svfs_close;
822 ops.exit = svfs_exit;
823 ops.lock = svfs_lock;
824 ops.setfileinfo = svfs_setfileinfo;
825 ops.qfileinfo = svfs_qfileinfo;
826 ops.fsinfo = svfs_fsinfo;
827 ops.fsattr = svfs_fsattr;
829 ops.search_first = svfs_search_first;
830 ops.search_next = svfs_search_next;
831 ops.search_close = svfs_search_close;
833 /* register ourselves with the NTVFS subsystem. We register under the name 'default'
834 as we wish to be the default backend */
835 ret = ntvfs_register("simple", NTVFS_DISK, &ops);
838 DEBUG(0,("Failed to register POSIX backend!\n"));