Efficient xattr handling for VxFS Signed-off-by: Abhidnya Joshi <Abhidnya.Joshi@verit...
[samba.git] / source3 / modules / vfs_dirsort.c
1 /*
2  * VFS module to provide a sorted directory list.
3  *
4  * Copyright (C) Andy Kelk (andy@mopoke.co.uk), 2009
5  *
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include "includes.h"
22 #include "smbd/smbd.h"
23 #include "system/filesys.h"
24
25 static int compare_dirent (const struct dirent *da, const struct dirent *db)
26 {
27         return strcasecmp_m(da->d_name, db->d_name);
28 }
29
30 struct dirsort_privates {
31         struct dirsort_privates *prev, *next;
32         long pos;
33         struct dirent *directory_list;
34         unsigned int number_of_entries;
35         struct timespec mtime;
36         DIR *source_directory;
37         files_struct *fsp; /* If open via FDOPENDIR. */
38         struct smb_filename *smb_fname; /* If open via OPENDIR */
39 };
40
41 static bool get_sorted_dir_mtime(vfs_handle_struct *handle,
42                                 struct dirsort_privates *data,
43                                 struct timespec *ret_mtime)
44 {
45         int ret;
46         struct timespec mtime;
47
48         if (data->fsp) {
49                 ret = fsp_stat(data->fsp);
50                 mtime = data->fsp->fsp_name->st.st_ex_mtime;
51         } else {
52                 ret = SMB_VFS_STAT(handle->conn, data->smb_fname);
53                 mtime = data->smb_fname->st.st_ex_mtime;
54         }
55
56         if (ret == -1) {
57                 return false;
58         }
59
60         *ret_mtime = mtime;
61
62         return true;
63 }
64
65 static bool open_and_sort_dir(vfs_handle_struct *handle,
66                                 struct dirsort_privates *data)
67 {
68         unsigned int i = 0;
69         unsigned int total_count = 0;
70
71         data->number_of_entries = 0;
72
73         if (get_sorted_dir_mtime(handle, data, &data->mtime) == false) {
74                 return false;
75         }
76
77         while (SMB_VFS_NEXT_READDIR(handle, data->source_directory, NULL)
78                != NULL) {
79                 total_count++;
80         }
81
82         if (total_count == 0) {
83                 return false;
84         }
85
86         /* Open the underlying directory and count the number of entries
87            Skip back to the beginning as we'll read it again */
88         SMB_VFS_NEXT_REWINDDIR(handle, data->source_directory);
89
90         /* Set up an array and read the directory entries into it */
91         TALLOC_FREE(data->directory_list); /* destroy previous cache if needed */
92         data->directory_list = talloc_zero_array(data,
93                                         struct dirent,
94                                         total_count);
95         if (!data->directory_list) {
96                 return false;
97         }
98         for (i = 0; i < total_count; i++) {
99                 struct dirent *dp = SMB_VFS_NEXT_READDIR(handle,
100                                                 data->source_directory,
101                                                 NULL);
102                 if (dp == NULL) {
103                         break;
104                 }
105                 data->directory_list[i] = *dp;
106         }
107
108         data->number_of_entries = i;
109
110         /* Sort the directory entries by name */
111         TYPESAFE_QSORT(data->directory_list, data->number_of_entries, compare_dirent);
112         return true;
113 }
114
115 static DIR *dirsort_opendir(vfs_handle_struct *handle,
116                                 const struct smb_filename *smb_fname,
117                                 const char *mask,
118                                 uint32_t attr)
119 {
120         struct dirsort_privates *list_head = NULL;
121         struct dirsort_privates *data = NULL;
122
123         if (SMB_VFS_HANDLE_TEST_DATA(handle)) {
124                 /* Find the list head of all open directories. */
125                 SMB_VFS_HANDLE_GET_DATA(handle, list_head, struct dirsort_privates,
126                                 return NULL);
127         }
128
129         /* set up our private data about this directory */
130         data = talloc_zero(handle->conn, struct dirsort_privates);
131         if (!data) {
132                 return NULL;
133         }
134
135         data->smb_fname = cp_smb_filename(data, smb_fname);
136         if (data->smb_fname == NULL) {
137                 TALLOC_FREE(data);
138                 return NULL;
139         }
140
141         /* Open the underlying directory and count the number of entries */
142         data->source_directory = SMB_VFS_NEXT_OPENDIR(handle, smb_fname, mask,
143                                                       attr);
144
145         if (data->source_directory == NULL) {
146                 TALLOC_FREE(data);
147                 return NULL;
148         }
149
150         if (!open_and_sort_dir(handle, data)) {
151                 SMB_VFS_NEXT_CLOSEDIR(handle,data->source_directory);
152                 TALLOC_FREE(data);
153                 return NULL;
154         }
155
156         /* Add to the private list of all open directories. */
157         DLIST_ADD(list_head, data);
158         SMB_VFS_HANDLE_SET_DATA(handle, list_head, NULL,
159                                 struct dirsort_privates, return NULL);
160
161         return data->source_directory;
162 }
163
164 static DIR *dirsort_fdopendir(vfs_handle_struct *handle,
165                                         files_struct *fsp,
166                                         const char *mask,
167                                         uint32_t attr)
168 {
169         struct dirsort_privates *list_head = NULL;
170         struct dirsort_privates *data = NULL;
171
172         if (SMB_VFS_HANDLE_TEST_DATA(handle)) {
173                 /* Find the list head of all open directories. */
174                 SMB_VFS_HANDLE_GET_DATA(handle, list_head, struct dirsort_privates,
175                                 return NULL);
176         }
177
178         /* set up our private data about this directory */
179         data = talloc_zero(handle->conn, struct dirsort_privates);
180         if (!data) {
181                 return NULL;
182         }
183
184         data->fsp = fsp;
185
186         /* Open the underlying directory and count the number of entries */
187         data->source_directory = SMB_VFS_NEXT_FDOPENDIR(handle, fsp, mask,
188                                                       attr);
189
190         if (data->source_directory == NULL) {
191                 TALLOC_FREE(data);
192                 return NULL;
193         }
194
195         if (!open_and_sort_dir(handle, data)) {
196                 SMB_VFS_NEXT_CLOSEDIR(handle,data->source_directory);
197                 TALLOC_FREE(data);
198                 /* fd is now closed. */
199                 fsp->fh->fd = -1;
200                 return NULL;
201         }
202
203         /* Add to the private list of all open directories. */
204         DLIST_ADD(list_head, data);
205         SMB_VFS_HANDLE_SET_DATA(handle, list_head, NULL,
206                                 struct dirsort_privates, return NULL);
207
208         return data->source_directory;
209 }
210
211 static struct dirent *dirsort_readdir(vfs_handle_struct *handle,
212                                           DIR *dirp,
213                                           SMB_STRUCT_STAT *sbuf)
214 {
215         struct dirsort_privates *data = NULL;
216         struct timespec current_mtime;
217
218         SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates,
219                                 return NULL);
220
221         while(data && (data->source_directory != dirp)) {
222                 data = data->next;
223         }
224         if (data == NULL) {
225                 return NULL;
226         }
227
228         if (get_sorted_dir_mtime(handle, data, &current_mtime) == false) {
229                 return NULL;
230         }
231
232         /* throw away cache and re-read the directory if we've changed */
233         if (timespec_compare(&current_mtime, &data->mtime)) {
234                 SMB_VFS_NEXT_REWINDDIR(handle, data->source_directory);
235                 open_and_sort_dir(handle, data);
236         }
237
238         if (data->pos >= data->number_of_entries) {
239                 return NULL;
240         }
241
242         return &data->directory_list[data->pos++];
243 }
244
245 static void dirsort_seekdir(vfs_handle_struct *handle, DIR *dirp,
246                             long offset)
247 {
248         struct timespec current_mtime;
249         struct dirsort_privates *data = NULL;
250
251         SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates, return);
252
253         /* Find the entry holding dirp. */
254         while(data && (data->source_directory != dirp)) {
255                 data = data->next;
256         }
257         if (data == NULL) {
258                 return;
259         }
260         if (offset >= data->number_of_entries) {
261                 return;
262         }
263         data->pos = offset;
264
265         if (get_sorted_dir_mtime(handle, data, &current_mtime) == false) {
266                 return;
267         }
268
269         if (timespec_compare(&current_mtime, &data->mtime)) {
270                 /* Directory changed. We must re-read the
271                    cache and search for the name that was
272                    previously stored at the offset being
273                    requested, otherwise after the re-sort
274                    we will point to the wrong entry. The
275                    OS/2 incremental delete code relies on
276                    this. */
277                 unsigned int i;
278                 char *wanted_name = talloc_strdup(handle->conn,
279                                         data->directory_list[offset].d_name);
280                 if (wanted_name == NULL) {
281                         return;
282                 }
283                 SMB_VFS_NEXT_REWINDDIR(handle, data->source_directory);
284                 open_and_sort_dir(handle, data);
285                 /* Now search for where we were. */
286                 data->pos = 0;
287                 for (i = 0; i < data->number_of_entries; i++) {
288                         if(strcmp(wanted_name, data->directory_list[i].d_name) == 0) {
289                                 data->pos = i;
290                                 break;
291                         }
292                 }
293                 TALLOC_FREE(wanted_name);
294         }
295 }
296
297 static long dirsort_telldir(vfs_handle_struct *handle, DIR *dirp)
298 {
299         struct dirsort_privates *data = NULL;
300         SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates,
301                                 return -1);
302
303         /* Find the entry holding dirp. */
304         while(data && (data->source_directory != dirp)) {
305                 data = data->next;
306         }
307         if (data == NULL) {
308                 return -1;
309         }
310         return data->pos;
311 }
312
313 static void dirsort_rewinddir(vfs_handle_struct *handle, DIR *dirp)
314 {
315         struct dirsort_privates *data = NULL;
316         SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates, return);
317
318         /* Find the entry holding dirp. */
319         while(data && (data->source_directory != dirp)) {
320                 data = data->next;
321         }
322         if (data == NULL) {
323                 return;
324         }
325         data->pos = 0;
326 }
327
328 static int dirsort_closedir(vfs_handle_struct *handle, DIR *dirp)
329 {
330         struct dirsort_privates *list_head = NULL;
331         struct dirsort_privates *data = NULL;
332         int ret;
333
334         SMB_VFS_HANDLE_GET_DATA(handle, list_head, struct dirsort_privates, return -1);
335         /* Find the entry holding dirp. */
336         for(data = list_head; data && (data->source_directory != dirp); data = data->next) {
337                 ;
338         }
339         if (data == NULL) {
340                 return -1;
341         }
342         /* Remove from the list and re-store the list head. */
343         DLIST_REMOVE(list_head, data);
344         SMB_VFS_HANDLE_SET_DATA(handle, list_head, NULL,
345                                 struct dirsort_privates, return -1);
346
347         ret = SMB_VFS_NEXT_CLOSEDIR(handle, dirp);
348         TALLOC_FREE(data);
349         return ret;
350 }
351
352 static struct vfs_fn_pointers vfs_dirsort_fns = {
353         .opendir_fn = dirsort_opendir,
354         .fdopendir_fn = dirsort_fdopendir,
355         .readdir_fn = dirsort_readdir,
356         .seekdir_fn = dirsort_seekdir,
357         .telldir_fn = dirsort_telldir,
358         .rewind_dir_fn = dirsort_rewinddir,
359         .closedir_fn = dirsort_closedir,
360 };
361
362 static_decl_vfs;
363 NTSTATUS vfs_dirsort_init(void)
364 {
365         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "dirsort",
366                                 &vfs_dirsort_fns);
367 }