s3-torture: introduce test_cli_read()
[Samba/gebeck_regimport.git] / source3 / modules / onefs_notify.c
blobbbdade6015978c4a215a1e80999fc01a55ab3bab
1 /*
2 * Unix SMB/CIFS implementation.
4 * Support for change notify using OneFS's file event notification system
6 * Copyright (C) Andrew Tridgell, 2006
7 * Copyright (C) Steven Danneman, 2008
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, see <http://www.gnu.org/licenses/>.
23 /* Implement handling of change notify requests on files and directories using
24 * Isilon OneFS's "ifs event" file notification system.
26 * The structure of this file is based off the implementation of change notify
27 * using the inotify system calls in smbd/notify_inotify.c */
29 /* TODO: We could reduce the number of file descriptors used by merging
30 * multiple watch requests on the same directory into the same
31 * onefs_notify_watch_context. To do this we'd need mux/demux routines that
32 * when receiving an event on that watch context would check it against the
33 * CompletionFilter and WatchTree of open SMB requests, and return notify
34 * events back to the proper SMB requests */
36 #include "includes.h"
37 #include "smbd/smbd.h"
38 #include "onefs.h"
40 #include <ifs/ifs_types.h>
41 #include <ifs/ifs_syscalls.h>
42 #include <isi_util/syscalls.h>
44 #include <sys/event.h>
46 #define ONEFS_IFS_EVENT_MAX_NUM 8
47 #define ONEFS_IFS_EVENT_MAX_BYTES (ONEFS_IFS_EVENT_MAX_NUM * \
48 sizeof(struct ifs_event))
50 struct onefs_notify_watch_context {
51 struct sys_notify_context *ctx;
52 int watch_fd;
53 ino_t watch_lin;
54 const char *path;
55 int ifs_event_fd;
56 uint32_t ifs_filter; /* the ifs event mask */
57 uint32_t smb_filter; /* the windows completion filter */
58 void (*callback)(struct sys_notify_context *ctx,
59 void *private_data,
60 struct notify_event *ev);
61 void *private_data;
64 /**
65 * Conversion map from a SMB completion filter to an IFS event mask.
67 static const struct {
68 uint32_t smb_filter;
69 uint32_t ifs_filter;
70 } onefs_notify_conv[] = {
71 {FILE_NOTIFY_CHANGE_FILE_NAME,
72 NOTE_CREATE | NOTE_DELETE | NOTE_RENAME_FROM | NOTE_RENAME_TO},
73 {FILE_NOTIFY_CHANGE_DIR_NAME,
74 NOTE_CREATE | NOTE_DELETE | NOTE_RENAME_FROM | NOTE_RENAME_TO},
75 {FILE_NOTIFY_CHANGE_ATTRIBUTES,
76 NOTE_CREATE | NOTE_DELETE | NOTE_RENAME_FROM | NOTE_RENAME_TO |
77 NOTE_ATTRIB},
78 {FILE_NOTIFY_CHANGE_SIZE,
79 NOTE_SIZE | NOTE_EXTEND},
80 {FILE_NOTIFY_CHANGE_LAST_WRITE,
81 NOTE_WRITE | NOTE_ATTRIB},
82 /* OneFS doesn't set atime by default, but we can somewhat fake it by
83 * notifying for other events that imply ACCESS */
84 {FILE_NOTIFY_CHANGE_LAST_ACCESS,
85 NOTE_WRITE | NOTE_ATTRIB},
86 /* We don't have an ifs_event for the setting of create time, but we
87 * can fake by notifying when a "new" file is created via rename */
88 {FILE_NOTIFY_CHANGE_CREATION,
89 NOTE_RENAME_TO},
90 {FILE_NOTIFY_CHANGE_SECURITY,
91 NOTE_SECURITY},
92 /* Unsupported bits
93 FILE_NOTIFY_CHANGE_EA (no EAs in OneFS)
94 FILE_NOTIFY_CHANGE_STREAM_NAME (no ifs_event equivalent)
95 FILE_NOTIFY_CHANGE_STREAM_SIZE (no ifs_event equivalent)
96 FILE_NOTIFY_CHANGE_STREAM_WRITE (no ifs_event equivalent) */
99 #define ONEFS_NOTIFY_UNSUPPORTED (FILE_NOTIFY_CHANGE_LAST_ACCESS | \
100 FILE_NOTIFY_CHANGE_CREATION | \
101 FILE_NOTIFY_CHANGE_EA | \
102 FILE_NOTIFY_CHANGE_STREAM_NAME | \
103 FILE_NOTIFY_CHANGE_STREAM_SIZE | \
104 FILE_NOTIFY_CHANGE_STREAM_WRITE)
107 * Convert Windows/SMB filter/flags to IFS event filter.
109 * @param[in] smb_filter Windows Completion Filter sent in the SMB
111 * @return ifs event filter mask
113 static uint32_t
114 onefs_notify_smb_filter_to_ifs_filter(uint32_t smb_filter)
116 int i;
117 uint32_t ifs_filter = 0x0;
119 for (i=0;i<ARRAY_SIZE(onefs_notify_conv);i++) {
120 if (onefs_notify_conv[i].smb_filter & smb_filter) {
121 ifs_filter |= onefs_notify_conv[i].ifs_filter;
125 return ifs_filter;
129 * Convert IFS filter/flags to a Windows notify action.
131 * Returns Win notification actions, types (1-5).
133 * @param[in] smb_filter Windows Completion Filter sent in the SMB
134 * @param[in] ifs_filter Returned from the kernel in the ifs_event
136 * @return 0 if there are no more relevant flags.
138 static int
139 onefs_notify_ifs_filter_to_smb_action(uint32_t smb_filter, uint32_t ifs_filter)
141 /* Handle Windows special cases, before modifying events bitmask */
143 /* Special case 1: win32api->MoveFile needs to send a modified
144 * notification on the new file, if smb_filter == ATTRIBUTES.
145 * Here we handle the case where two separate ATTR & NAME notifications
146 * have been registered. We handle the case where both bits are set in
147 * the same registration in onefs_notify_dispatch() */
148 if ((smb_filter & FILE_NOTIFY_CHANGE_ATTRIBUTES) &&
149 !(smb_filter & FILE_NOTIFY_CHANGE_FILE_NAME) &&
150 (ifs_filter & NOTE_FILE) && (ifs_filter & NOTE_RENAME_TO))
152 return NOTIFY_ACTION_MODIFIED;
155 /* Special case 2: Writes need to send a modified
156 * notification on the file, if smb_filter = ATTRIBUTES. */
157 if ((smb_filter & FILE_NOTIFY_CHANGE_ATTRIBUTES) &&
158 (ifs_filter & NOTE_FILE) && (ifs_filter & NOTE_WRITE))
160 return NOTIFY_ACTION_MODIFIED;
163 /* Loop because some events may be filtered out. Eventually all
164 * relevant events will be taken care of and returned. */
165 while (1) {
166 if (ifs_filter & NOTE_CREATE) {
167 ifs_filter &= ~NOTE_CREATE;
168 if ((smb_filter & FILE_NOTIFY_CHANGE_FILE_NAME) &&
169 (ifs_filter & NOTE_FILE))
170 return NOTIFY_ACTION_ADDED;
171 if ((smb_filter & FILE_NOTIFY_CHANGE_DIR_NAME) &&
172 (ifs_filter & NOTE_DIRECTORY))
173 return NOTIFY_ACTION_ADDED;
175 else if (ifs_filter & NOTE_DELETE) {
176 ifs_filter &= ~NOTE_DELETE;
177 if ((smb_filter & FILE_NOTIFY_CHANGE_FILE_NAME) &&
178 (ifs_filter & NOTE_FILE))
179 return NOTIFY_ACTION_REMOVED;
180 if ((smb_filter & FILE_NOTIFY_CHANGE_DIR_NAME) &&
181 (ifs_filter & NOTE_DIRECTORY))
182 return NOTIFY_ACTION_REMOVED;
184 else if (ifs_filter & NOTE_WRITE) {
185 ifs_filter &= ~NOTE_WRITE;
186 if ((smb_filter & FILE_NOTIFY_CHANGE_LAST_WRITE) ||
187 (smb_filter & FILE_NOTIFY_CHANGE_LAST_ACCESS))
188 return NOTIFY_ACTION_MODIFIED;
190 else if ((ifs_filter & NOTE_SIZE) || (ifs_filter & NOTE_EXTEND)) {
191 ifs_filter &= ~NOTE_SIZE;
192 ifs_filter &= ~NOTE_EXTEND;
194 /* TODO: Do something on NOTE_DIR? */
195 if ((smb_filter & FILE_NOTIFY_CHANGE_SIZE) &&
196 (ifs_filter & NOTE_FILE))
197 return NOTIFY_ACTION_MODIFIED;
199 else if (ifs_filter & NOTE_ATTRIB) {
200 ifs_filter &= ~NOTE_ATTRIB;
201 /* NOTE_ATTRIB needs to be converted to a
202 * LAST_WRITE as well, because we need to send
203 * LAST_WRITE when the mtime changes. Looking into
204 * better alternatives as this causes extra LAST_WRITE
205 * notifications. We also return LAST_ACCESS as a
206 * modification to attribs implies this. */
207 if ((smb_filter & FILE_NOTIFY_CHANGE_ATTRIBUTES) ||
208 (smb_filter & FILE_NOTIFY_CHANGE_LAST_WRITE) ||
209 (smb_filter & FILE_NOTIFY_CHANGE_LAST_ACCESS))
210 return NOTIFY_ACTION_MODIFIED;
212 else if (ifs_filter & NOTE_LINK) {
213 ifs_filter &= ~NOTE_LINK;
214 /* NOTE_LINK will send out NO notifications */
216 else if (ifs_filter & NOTE_REVOKE) {
217 ifs_filter &= ~NOTE_REVOKE;
218 /* NOTE_REVOKE will send out NO notifications */
220 else if (ifs_filter & NOTE_RENAME_FROM) {
221 ifs_filter &= ~NOTE_RENAME_FROM;
223 if (((smb_filter & FILE_NOTIFY_CHANGE_FILE_NAME) &&
224 (ifs_filter & NOTE_FILE)) ||
225 ((smb_filter & FILE_NOTIFY_CHANGE_DIR_NAME) &&
226 (ifs_filter & NOTE_DIRECTORY))) {
227 /* Check if this is a RENAME, not a MOVE */
228 if (ifs_filter & NOTE_RENAME_SAMEDIR) {
229 /* Remove the NOTE_RENAME_SAMEDIR, IFF
230 * RENAME_TO is not in this event */
231 if (!(ifs_filter & NOTE_RENAME_TO))
232 ifs_filter &=
233 ~NOTE_RENAME_SAMEDIR;
234 return NOTIFY_ACTION_OLD_NAME;
236 return NOTIFY_ACTION_REMOVED;
239 else if (ifs_filter & NOTE_RENAME_TO) {
240 ifs_filter &= ~NOTE_RENAME_TO;
242 if (((smb_filter & FILE_NOTIFY_CHANGE_FILE_NAME) &&
243 (ifs_filter & NOTE_FILE)) ||
244 ((smb_filter & FILE_NOTIFY_CHANGE_DIR_NAME) &&
245 (ifs_filter & NOTE_DIRECTORY))) {
246 /* Check if this is a RENAME, not a MOVE */
247 if (ifs_filter & NOTE_RENAME_SAMEDIR) {
248 /* Remove the NOTE_RENAME_SAMEDIR, IFF
249 * RENAME_FROM is not in this event */
250 if (!(ifs_filter & NOTE_RENAME_FROM))
251 ifs_filter &=
252 ~NOTE_RENAME_SAMEDIR;
253 return NOTIFY_ACTION_NEW_NAME;
255 return NOTIFY_ACTION_ADDED;
257 /* RAW-NOTIFY shows us that a rename triggers a
258 * creation time change */
259 if ((smb_filter & FILE_NOTIFY_CHANGE_CREATION) &&
260 (ifs_filter & NOTE_FILE))
261 return NOTIFY_ACTION_MODIFIED;
263 else if (ifs_filter & NOTE_SECURITY) {
264 ifs_filter &= ~NOTE_SECURITY;
266 if (smb_filter & FILE_NOTIFY_CHANGE_SECURITY)
267 return NOTIFY_ACTION_MODIFIED;
268 } else {
269 /* No relevant flags found */
270 return 0;
276 * Retrieve a directory path of a changed file, relative to the watched dir
278 * @param[in] wc watch context for the returned event
279 * @param[in] e ifs_event notification returned from the kernel
280 * @param[out] path name relative to the watched dir. This is talloced
281 * off of wc and needs to be freed by the caller.
283 * @return true on success
285 * TODO: This function currently doesn't handle path names with multiple
286 * encodings. enc_get_lin_path() should be used in the future to convert
287 * each path segment's encoding to UTF-8
289 static bool
290 get_ifs_event_path(struct onefs_notify_watch_context *wc, struct ifs_event *e,
291 char **path)
293 char *path_buf = NULL;
294 size_t pathlen = 0;
295 int error = 0;
297 SMB_ASSERT(path);
299 /* Lookup the path from watch_dir through our parent dir */
300 if (e->namelen > 0) {
301 error = lin_get_path(wc->watch_lin,
302 e->parent_lin,
303 HEAD_SNAPID,
304 e->parent_parent_lin,
305 e->parent_name_hash,
306 &pathlen, &path_buf);
307 if (!error) {
308 /* Only add slash if a path exists in path_buf from
309 * lin_get_path call. Windows does not expect a
310 * leading '/' */
311 if (pathlen > 0)
312 *path = talloc_asprintf(wc, "%s/%s",
313 path_buf, e->name);
314 else
315 *path = talloc_asprintf(wc, "%s", e->name);
316 SAFE_FREE(path_buf);
320 /* If ifs_event didn't return a name, or we errored out of our intial
321 * path lookup, try again using the lin of the changed file */
322 if (!(*path)) {
323 error = lin_get_path(wc->watch_lin,
324 e->lin,
325 HEAD_SNAPID,
326 e->parent_lin,
327 e->name_hash,
328 &pathlen, &path_buf);
329 if (error) {
330 /* It's possible that both the lin and the parent lin
331 * are invalid (or not given) -- we will skip these
332 * events. */
333 DEBUG(3,("Path lookup failed. LINS are invalid: "
334 "e->lin: 0x%llu, e->parent_lin: 0x%llu, "
335 "e->parent_parent_lin: 0x%llu\n",
336 e->lin, e->parent_lin, e->parent_parent_lin));
337 SAFE_FREE(path_buf);
338 return false;
339 } else {
340 *path = talloc_asprintf(wc, "%s", path_buf);
341 DEBUG(5, ("Using path from event LIN = %s\n",
342 path_buf));
343 SAFE_FREE(path_buf);
347 /* Replacement of UNIX slashes with WIN slashes is handled at a
348 * higher layer. */
350 return true;
354 * Dispatch one OneFS notify event to the general Samba code
356 * @param[in] wc watch context for the returned event
357 * @param[in] e event returned from the kernel
359 * @return nothing
361 static void
362 onefs_notify_dispatch(struct onefs_notify_watch_context *wc,
363 struct ifs_event *e)
365 char *path = NULL;
366 struct notify_event ne;
368 DEBUG(5, ("Retrieved ifs event from kernel: lin=%#llx, ifs_events=%#x, "
369 "parent_lin=%#llx, namelen=%d, name=\"%s\"\n",
370 e->lin, e->events, e->parent_lin, e->namelen, e->name));
372 /* Check validity of event returned from kernel */
373 if (e->lin == 0) {
374 /* The lin == 0 specifies 1 of 2 cases:
375 * 1) We are out of events. The kernel has a limited
376 * amount (somewhere near 90000)
377 * 2) Split nodes have merged back and had data written
378 * to them -- thus we've missed some of those events. */
379 DEBUG(3, ("We've missed some kernel ifs events!\n"));
381 /* Alert higher level to the problem so it returns catch-all
382 * response to the client */
383 ne.path = NULL;
384 ne.action = 0;
385 wc->callback(wc->ctx, wc->private_data, &ne);
388 if (e->lin == wc->watch_lin) {
389 /* Windows doesn't report notifications on root
390 * watched directory */
391 /* TODO: This should be abstracted out to the general layer
392 * instead of being handled in every notify provider */
393 DEBUG(5, ("Skipping notification on root of the watched "
394 "path.\n"));
395 return;
398 /* Retrieve the full path for the ifs event name */
399 if(!get_ifs_event_path(wc, e, &path)) {
400 DEBUG(3, ("Failed to convert the ifs_event lins to a path. "
401 "Skipping this event\n"));
402 return;
405 if (!strncmp(path, ".ifsvar", 7)) {
406 /* Skip notifications on file if its in ifs configuration
407 * directory */
408 goto clean;
411 ne.path = path;
413 /* Convert ifs event mask to an smb action mask */
414 ne.action = onefs_notify_ifs_filter_to_smb_action(wc->smb_filter,
415 e->events);
417 DEBUG(5, ("Converted smb_filter=%#x, ifs_events=%#x, to "
418 "ne.action = %d, for ne.path = %s\n",
419 wc->smb_filter, e->events, ne.action, ne.path));
421 if (!ne.action)
422 goto clean;
424 /* Return notify_event to higher level */
425 wc->callback(wc->ctx, wc->private_data, &ne);
427 /* SMB expects a file rename/move to generate three actions, a
428 * rename_from/delete on the from file, a rename_to/create on the to
429 * file, and a modify for the rename_to file. If we have two separate
430 * notifications registered for ATTRIBUTES and FILENAME, this case will
431 * be handled by separate ifs_events in
432 * onefs_notify_ifs_filter_to_smb_action(). If both bits are registered
433 * in the same notification, we must send an extra MODIFIED action
434 * here. */
435 if ((wc->smb_filter & FILE_NOTIFY_CHANGE_ATTRIBUTES) &&
436 (wc->smb_filter & FILE_NOTIFY_CHANGE_FILE_NAME) &&
437 (e->events & NOTE_FILE) && (e->events & NOTE_RENAME_TO))
439 ne.action = NOTIFY_ACTION_MODIFIED;
440 wc->callback(wc->ctx, wc->private_data, &ne);
443 /* FALLTHROUGH */
444 clean:
445 talloc_free(path);
446 return;
450 * Callback when the kernel has some events for us
452 * Read events off ifs event fd and pass them to our dispatch function
454 * @param ev context of all tevents registered in the smbd
455 * @param fde tevent struct specific to one ifs event channel
456 * @param flags tevent flags passed when we added our ifs event channel fd to
457 * the main loop
458 * @param private_data onefs_notify_watch_context specific to this ifs event
459 * channel
461 * @return nothing
463 static void
464 onefs_notify_handler(struct event_context *ev,
465 struct fd_event *fde,
466 uint16_t flags,
467 void *private_data)
469 struct onefs_notify_watch_context *wc = NULL;
470 struct ifs_event ifs_events[ONEFS_IFS_EVENT_MAX_NUM];
471 ssize_t nread = 0;
472 int count = 0;
473 int i = 0;
475 wc = talloc_get_type(private_data, struct onefs_notify_watch_context);
477 /* Read as many ifs events from the notify channel as we can */
478 nread = sys_read(wc->ifs_event_fd, &ifs_events,
479 ONEFS_IFS_EVENT_MAX_BYTES);
480 if (nread == 0) {
481 DEBUG(0,("No data found while reading ifs event fd?!\n"));
482 return;
484 if (nread < 0) {
485 DEBUG(0,("Failed to read ifs event data: %s\n",
486 strerror(errno)));
487 return;
490 count = nread / sizeof(struct ifs_event);
492 DEBUG(5, ("Got %d notification events in %d bytes.\n", count, nread));
494 /* Dispatch ifs_events one-at-a-time to higher level */
495 for (i=0; i < count; i++) {
496 onefs_notify_dispatch(wc, &ifs_events[i]);
501 * Destroy the ifs event channel
503 * This is called from talloc_free() when the generic Samba notify layer frees
504 * the onefs_notify_watch_context.
506 * @param[in] wc pointer to watch context which is being destroyed
508 * return 0 on success
510 static int
511 onefs_watch_destructor(struct onefs_notify_watch_context *wc)
513 /* The ifs_event_fd will re de-registered from the event loop by
514 * another destructor triggered from the freeing of this wc */
515 close(wc->ifs_event_fd);
516 return 0;
520 * Register a single change notify watch request.
522 * Open an event listener on a directory to watch for modifications. This
523 * channel is closed by a destructor when the caller calls talloc_free()
524 * on *handle.
526 * @param[in] vfs_handle handle given to most VFS operations
527 * @param[in] ctx context (conn and tevent) for this connection
528 * @param[in] e filter and path of client's notify request
529 * @param[in] callback function to call when file notification event is received
530 * from the kernel, passing that event up to Samba's
531 * generalized change notify layer
532 * @param[in] private_data opaque data given to us by the general change notify
533 * layer which must be returned in the callback function
534 * @param[out] handle_p handle returned to generalized change notify layer used
535 * to close the event channel when notification is
536 * cancelled
538 * @return NT_STATUS_OK unless error
540 NTSTATUS
541 onefs_notify_watch(vfs_handle_struct *vfs_handle,
542 struct sys_notify_context *ctx,
543 struct notify_entry *e,
544 void (*callback)(struct sys_notify_context *ctx,
545 void *private_data,
546 struct notify_event *ev),
547 void *private_data,
548 void *handle_p)
550 int ifs_event_fd = -1;
551 uint32_t ifs_filter = 0;
552 uint32_t smb_filter = e->filter;
553 bool watch_tree = !!e->subdir_filter;
554 struct onefs_notify_watch_context *wc = NULL;
555 void **handle = (void **)handle_p;
556 SMB_STRUCT_STAT sbuf;
557 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
559 /* Fallback to default Samba implementation if kernel CN is disabled */
560 if (!lp_kernel_change_notify(vfs_handle->conn->params)) {
561 (*handle) = NULL;
562 return NT_STATUS_OK;
565 /* The OneFS open path should always give us a valid fd on a directory*/
566 SMB_ASSERT(e->dir_fd >= 0);
568 /* Always set e->filter to 0 so we don't fallback on the default change
569 * notify backend. It's not cluster coherent or cross-protocol so we
570 * can't guarantee correctness using it. */
571 e->filter = 0;
572 e->subdir_filter = 0;
574 /* Snapshots do not currently allow event listeners. See Isilon
575 * bug 33476 for an example of .snapshot debug spew that can occur. */
576 if (e->dir_id.extid != HEAD_SNAPID)
577 return NT_STATUS_INVALID_PARAMETER;
579 /* Convert Completion Filter mask to IFS Event mask */
580 ifs_filter = onefs_notify_smb_filter_to_ifs_filter(smb_filter);
582 if (smb_filter & ONEFS_NOTIFY_UNSUPPORTED) {
583 /* One or more of the filter bits could not be fully handled by
584 * the ifs_event system. To be correct, if we cannot service a
585 * bit in the completion filter we should return
586 * NT_STATUS_NOT_IMPLEMENTED to let the client know that they
587 * won't be receiving all the notify events that they asked for.
588 * Unfortunately, WinXP clients cache this error message, stop
589 * trying to send any notify requests at all, and instead return
590 * NOT_IMPLEMENTED to all requesting apps without ever sending a
591 * message to us. Thus we lie, and say we can service all bits,
592 * but simply don't return actions for the filter bits we can't
593 * detect or fully implement. */
594 DEBUG(3,("One or more of the Windows completion filter bits "
595 "for \"%s\" could not be fully handled by the "
596 "ifs_event system. The failed bits are "
597 "smb_filter=%#x\n",
598 e->path, smb_filter & ONEFS_NOTIFY_UNSUPPORTED));
601 if (ifs_filter == 0) {
602 /* None of the filter bits given are supported by the ifs_event
603 * system. Don't create a kernel notify channel, but mock
604 * up a fake handle for the caller. */
605 DEBUG(3,("No bits in the Windows completion filter could be "
606 "translated to ifs_event mask for \"%s\", "
607 "smb_filter=%#x\n", e->path, smb_filter));
608 (*handle) = NULL;
609 return NT_STATUS_OK;
612 /* Register an ifs event channel for this watch request */
613 ifs_event_fd = ifs_create_listener(watch_tree ?
614 EVENT_RECURSIVE :
615 EVENT_CHILDREN,
616 ifs_filter,
617 e->dir_fd);
618 if (ifs_event_fd < 0) {
619 DEBUG(0,("Failed to create listener for %s with \"%s\". "
620 "smb_filter=0x%x, ifs_filter=0x%x, watch_tree=%u\n",
621 strerror(errno), e->path, smb_filter, ifs_filter,
622 watch_tree));
623 return map_nt_error_from_unix(errno);
626 /* Create a watch context to track this change notify request */
627 wc = talloc(ctx, struct onefs_notify_watch_context);
628 if (wc == NULL) {
629 status = NT_STATUS_NO_MEMORY;
630 goto err;
633 /* Get LIN for directory */
634 if (onefs_sys_fstat(e->dir_fd, &sbuf)) {
635 DEBUG(0, ("stat on directory fd failed: %s\n",
636 strerror(errno)));
637 status = map_nt_error_from_unix(errno);
638 goto err;
641 if (sbuf.st_ex_ino == 0) {
642 DEBUG(0, ("0 LIN found!\n"));
643 goto err;
646 wc->ctx = ctx;
647 wc->watch_fd = e->dir_fd;
648 wc->watch_lin = sbuf.st_ex_ino;
649 wc->ifs_event_fd = ifs_event_fd;
650 wc->ifs_filter = ifs_filter;
651 wc->smb_filter = smb_filter;
652 wc->callback = callback;
653 wc->private_data = private_data;
654 wc->path = talloc_strdup(wc, e->path);
655 if (wc->path == NULL) {
656 status = NT_STATUS_NO_MEMORY;
657 goto err;
660 (*handle) = wc;
662 /* The caller frees the handle to stop watching */
663 talloc_set_destructor(wc, onefs_watch_destructor);
665 /* Add a tevent waiting for the ifs event fd to be readable */
666 event_add_fd(ctx->ev, wc, wc->ifs_event_fd, EVENT_FD_READ,
667 onefs_notify_handler, wc);
669 DEBUG(10, ("Watching for changes on \"%s\" smb_filter=0x%x, "
670 "ifs_filter=0x%x, watch_tree=%d, ifs_event_fd=%d, "
671 "dir_fd=%d, dir_lin=0x%llx\n",
672 e->path, smb_filter, ifs_filter, watch_tree,
673 ifs_event_fd, e->dir_fd, sbuf.st_ex_ino));
675 return NT_STATUS_OK;
677 err:
678 talloc_free(wc);
679 SMB_ASSERT(ifs_event_fd >= 0);
680 close(ifs_event_fd);
681 return status;