2 Unix SMB/CIFS implementation.
4 POSIX NTVFS backend - open and close
6 Copyright (C) Andrew Tridgell 2004
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 #include "include/includes.h"
24 #include "vfs_posix.h"
28 find open file handle given fnum
30 struct pvfs_file *pvfs_find_fd(struct pvfs_state *pvfs,
31 struct smbsrv_request *req, uint16_t fnum)
34 for (f=pvfs->open_files;f;f=f->next) {
35 if (f->fnum == fnum) {
36 if (req->session != f->session) {
37 DEBUG(2,("pvfs_find_fd: attempt to use wrong session for fnum %d\n", fnum));
47 by using a destructor we make sure that abnormal cleanup will not
48 leak file descriptors (assuming at least the top level pointer is freed, which
49 will cascade down to here)
51 static int pvfs_fd_destructor(void *p)
53 struct pvfs_file *f = p;
63 TODO: this is a temporary implementation derived from the simple backend
64 its purpose is to allow other tests to run
66 NTSTATUS pvfs_open(struct ntvfs_module_context *ntvfs,
67 struct smbsrv_request *req, union smb_open *io)
69 struct pvfs_state *pvfs = ntvfs->private_data;
71 struct pvfs_filename *name;
75 if (io->generic.level != RAW_OPEN_GENERIC) {
76 return ntvfs_map_open(req, io, ntvfs);
79 /* resolve the cifs name to a posix name */
80 status = pvfs_resolve_name(pvfs, req, io->ntcreatex.in.fname,
81 PVFS_RESOLVE_NO_WILDCARD, &name);
82 if (!NT_STATUS_IS_OK(status)) {
86 switch (io->generic.in.open_disposition) {
87 case NTCREATEX_DISP_SUPERSEDE:
88 case NTCREATEX_DISP_OVERWRITE_IF:
89 flags = O_CREAT | O_TRUNC;
91 case NTCREATEX_DISP_OPEN:
92 case NTCREATEX_DISP_OVERWRITE:
95 case NTCREATEX_DISP_CREATE:
96 flags = O_CREAT | O_EXCL;
98 case NTCREATEX_DISP_OPEN_IF:
108 /* we need to do this differently to support systems without O_DIRECTORY */
110 #define O_DIRECTORY 0
113 if ((io->generic.in.create_options & NTCREATEX_OPTIONS_DIRECTORY) &&
114 !(name->dos.attrib & FILE_ATTRIBUTE_DIRECTORY)) {
115 return NT_STATUS_NOT_A_DIRECTORY;
118 if (name->dos.attrib & FILE_ATTRIBUTE_DIRECTORY) {
119 flags = O_RDONLY | O_DIRECTORY;
120 if (pvfs->flags & PVFS_FLAG_READONLY) {
123 switch (io->generic.in.open_disposition) {
124 case NTCREATEX_DISP_CREATE:
125 if (mkdir(name->full_name, 0755) == -1) {
126 return pvfs_map_errno(pvfs,errno);
129 case NTCREATEX_DISP_OPEN_IF:
130 if (mkdir(name->full_name, 0755) == -1 && errno != EEXIST) {
131 return pvfs_map_errno(pvfs,errno);
138 fd = open(name->full_name, flags, 0644);
142 return pvfs_map_errno(pvfs,errno);
145 f = talloc_p(pvfs, struct pvfs_file);
148 return NT_STATUS_NO_MEMORY;
151 /* re-resolve the open fd */
152 status = pvfs_resolve_name_fd(pvfs, fd, name);
153 if (!NT_STATUS_IS_OK(status)) {
159 f->name = talloc_steal(f, name);
160 f->session = req->session;
161 f->smbpid = req->smbpid;
163 /* setup a destructor to avoid file descriptor leaks on
164 abnormal termination */
165 talloc_set_destructor(f, pvfs_fd_destructor);
167 DLIST_ADD(pvfs->open_files, f);
169 ZERO_STRUCT(io->generic.out);
171 io->generic.out.create_time = name->dos.create_time;
172 io->generic.out.access_time = name->dos.access_time;
173 io->generic.out.write_time = name->dos.write_time;
174 io->generic.out.change_time = name->dos.change_time;
175 io->generic.out.fnum = f->fnum;
176 io->generic.out.alloc_size = name->dos.alloc_size;
177 io->generic.out.size = name->st.st_size;
178 io->generic.out.attrib = name->dos.attrib;
179 io->generic.out.is_directory = (name->dos.attrib & FILE_ATTRIBUTE_DIRECTORY)?1:0;
188 NTSTATUS pvfs_close(struct ntvfs_module_context *ntvfs,
189 struct smbsrv_request *req, union smb_close *io)
191 struct pvfs_state *pvfs = ntvfs->private_data;
195 if (io->generic.level != RAW_CLOSE_CLOSE) {
196 /* we need a mapping function */
197 return NT_STATUS_INVALID_LEVEL;
200 f = pvfs_find_fd(pvfs, req, io->close.in.fnum);
202 return NT_STATUS_INVALID_HANDLE;
205 if (close(f->fd) != 0) {
206 status = pvfs_map_errno(pvfs, errno);
208 status = NT_STATUS_OK;
211 talloc_set_destructor(f, NULL);
213 DLIST_REMOVE(pvfs->open_files, f);
221 logoff - close all file descriptors open by a vuid
223 NTSTATUS pvfs_logoff(struct ntvfs_module_context *ntvfs,
224 struct smbsrv_request *req)
226 struct pvfs_state *pvfs = ntvfs->private_data;
227 struct pvfs_file *f, *next;
229 for (f=pvfs->open_files;f;f=next) {
231 if (f->session == req->session) {
232 talloc_set_destructor(f, NULL);
233 DLIST_REMOVE(pvfs->open_files, f);
243 exit - close files for the current pid
245 NTSTATUS pvfs_exit(struct ntvfs_module_context *ntvfs,
246 struct smbsrv_request *req)
248 struct pvfs_state *pvfs = ntvfs->private_data;
249 struct pvfs_file *f, *next;
251 for (f=pvfs->open_files;f;f=next) {
253 if (f->smbpid == req->smbpid) {
254 talloc_set_destructor(f, NULL);
255 DLIST_REMOVE(pvfs->open_files, f);