Fix headers, ldb_includes.h is a private header,
[Samba/bjacke.git] / source3 / modules / onefs_notify.c
blob40f690876d5fd2cfeab64436ff937b85c483baf6
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 "onefs.h"
38 #include <ifs/ifs_types.h>
39 #include <ifs/ifs_syscalls.h>
40 #include <isi_util/syscalls.h>
42 #include <sys/event.h>
44 #define ONEFS_IFS_EVENT_MAX_NUM 8
45 #define ONEFS_IFS_EVENT_MAX_BYTES (ONEFS_IFS_EVENT_MAX_NUM * \
46 sizeof(struct ifs_event))
48 struct onefs_notify_watch_context {
49 struct sys_notify_context *ctx;
50 int watch_fd;
51 ino_t watch_lin;
52 const char *path;
53 int ifs_event_fd;
54 uint32_t ifs_filter; /* the ifs event mask */
55 uint32_t smb_filter; /* the windows completion filter */
56 void (*callback)(struct sys_notify_context *ctx,
57 void *private_data,
58 struct notify_event *ev);
59 void *private_data;
62 /**
63 * Conversion map from a SMB completion filter to an IFS event mask.
65 static const struct {
66 uint32_t smb_filter;
67 uint32_t ifs_filter;
68 } onefs_notify_conv[] = {
69 {FILE_NOTIFY_CHANGE_FILE_NAME,
70 NOTE_CREATE | NOTE_DELETE | NOTE_RENAME_FROM | NOTE_RENAME_TO},
71 {FILE_NOTIFY_CHANGE_DIR_NAME,
72 NOTE_CREATE | NOTE_DELETE | NOTE_RENAME_FROM | NOTE_RENAME_TO},
73 {FILE_NOTIFY_CHANGE_ATTRIBUTES,
74 NOTE_CREATE | NOTE_DELETE | NOTE_RENAME_FROM | NOTE_RENAME_TO |
75 NOTE_ATTRIB},
76 {FILE_NOTIFY_CHANGE_SIZE,
77 NOTE_SIZE | NOTE_EXTEND},
78 {FILE_NOTIFY_CHANGE_LAST_WRITE,
79 NOTE_WRITE | NOTE_ATTRIB},
80 /* OneFS doesn't set atime by default, but we can somewhat fake it by
81 * notifying for other events that imply ACCESS */
82 {FILE_NOTIFY_CHANGE_LAST_ACCESS,
83 NOTE_WRITE | NOTE_ATTRIB},
84 /* We don't have an ifs_event for the setting of create time, but we
85 * can fake by notifying when a "new" file is created via rename */
86 {FILE_NOTIFY_CHANGE_CREATION,
87 NOTE_RENAME_TO},
88 {FILE_NOTIFY_CHANGE_SECURITY,
89 NOTE_SECURITY},
90 /* Unsupported bits
91 FILE_NOTIFY_CHANGE_EA (no EAs in OneFS)
92 FILE_NOTIFY_CHANGE_STREAM_NAME (no ifs_event equivalent)
93 FILE_NOTIFY_CHANGE_STREAM_SIZE (no ifs_event equivalent)
94 FILE_NOTIFY_CHANGE_STREAM_WRITE (no ifs_event equivalent) */
97 #define ONEFS_NOTIFY_UNSUPPORTED (FILE_NOTIFY_CHANGE_LAST_ACCESS | \
98 FILE_NOTIFY_CHANGE_CREATION | \
99 FILE_NOTIFY_CHANGE_EA | \
100 FILE_NOTIFY_CHANGE_STREAM_NAME | \
101 FILE_NOTIFY_CHANGE_STREAM_SIZE | \
102 FILE_NOTIFY_CHANGE_STREAM_WRITE)
105 * Convert Windows/SMB filter/flags to IFS event filter.
107 * @param[in] smb_filter Windows Completion Filter sent in the SMB
109 * @return ifs event filter mask
111 static uint32_t
112 onefs_notify_smb_filter_to_ifs_filter(uint32_t smb_filter)
114 int i;
115 uint32_t ifs_filter = 0x0;
117 for (i=0;i<ARRAY_SIZE(onefs_notify_conv);i++) {
118 if (onefs_notify_conv[i].smb_filter & smb_filter) {
119 ifs_filter |= onefs_notify_conv[i].ifs_filter;
123 return ifs_filter;
127 * Convert IFS filter/flags to a Windows notify action.
129 * Returns Win notification actions, types (1-5).
131 * @param[in] smb_filter Windows Completion Filter sent in the SMB
132 * @param[in] ifs_filter Returned from the kernel in the ifs_event
134 * @return 0 if there are no more relevant flags.
136 static int
137 onefs_notify_ifs_filter_to_smb_action(uint32_t smb_filter, uint32_t ifs_filter)
139 /* Handle Windows special cases, before modifying events bitmask */
141 /* Special case 1: win32api->MoveFile needs to send a modified
142 * notification on the new file, if smb_filter == ATTRIBUTES.
143 * Here we handle the case where two separate ATTR & NAME notifications
144 * have been registered. We handle the case where both bits are set in
145 * the same registration in onefs_notify_dispatch() */
146 if ((smb_filter & FILE_NOTIFY_CHANGE_ATTRIBUTES) &&
147 !(smb_filter & FILE_NOTIFY_CHANGE_FILE_NAME) &&
148 (ifs_filter & NOTE_FILE) && (ifs_filter & NOTE_RENAME_TO))
150 return NOTIFY_ACTION_MODIFIED;
153 /* Special case 2: Writes need to send a modified
154 * notification on the file, if smb_filter = ATTRIBUTES. */
155 if ((smb_filter & FILE_NOTIFY_CHANGE_ATTRIBUTES) &&
156 (ifs_filter & NOTE_FILE) && (ifs_filter & NOTE_WRITE))
158 return NOTIFY_ACTION_MODIFIED;
161 /* Loop because some events may be filtered out. Eventually all
162 * relevant events will be taken care of and returned. */
163 while (1) {
164 if (ifs_filter & NOTE_CREATE) {
165 ifs_filter &= ~NOTE_CREATE;
166 if ((smb_filter & FILE_NOTIFY_CHANGE_FILE_NAME) &&
167 (ifs_filter & NOTE_FILE))
168 return NOTIFY_ACTION_ADDED;
169 if ((smb_filter & FILE_NOTIFY_CHANGE_DIR_NAME) &&
170 (ifs_filter & NOTE_DIRECTORY))
171 return NOTIFY_ACTION_ADDED;
173 else if (ifs_filter & NOTE_DELETE) {
174 ifs_filter &= ~NOTE_DELETE;
175 if ((smb_filter & FILE_NOTIFY_CHANGE_FILE_NAME) &&
176 (ifs_filter & NOTE_FILE))
177 return NOTIFY_ACTION_REMOVED;
178 if ((smb_filter & FILE_NOTIFY_CHANGE_DIR_NAME) &&
179 (ifs_filter & NOTE_DIRECTORY))
180 return NOTIFY_ACTION_REMOVED;
182 else if (ifs_filter & NOTE_WRITE) {
183 ifs_filter &= ~NOTE_WRITE;
184 if ((smb_filter & FILE_NOTIFY_CHANGE_LAST_WRITE) ||
185 (smb_filter & FILE_NOTIFY_CHANGE_LAST_ACCESS))
186 return NOTIFY_ACTION_MODIFIED;
188 else if ((ifs_filter & NOTE_SIZE) || (ifs_filter & NOTE_EXTEND)) {
189 ifs_filter &= ~NOTE_SIZE;
190 ifs_filter &= ~NOTE_EXTEND;
192 /* TODO: Do something on NOTE_DIR? */
193 if ((smb_filter & FILE_NOTIFY_CHANGE_SIZE) &&
194 (ifs_filter & NOTE_FILE))
195 return NOTIFY_ACTION_MODIFIED;
197 else if (ifs_filter & NOTE_ATTRIB) {
198 ifs_filter &= ~NOTE_ATTRIB;
199 /* NOTE_ATTRIB needs to be converted to a
200 * LAST_WRITE as well, because we need to send
201 * LAST_WRITE when the mtime changes. Looking into
202 * better alternatives as this causes extra LAST_WRITE
203 * notifications. We also return LAST_ACCESS as a
204 * modification to attribs implies this. */
205 if ((smb_filter & FILE_NOTIFY_CHANGE_ATTRIBUTES) ||
206 (smb_filter & FILE_NOTIFY_CHANGE_LAST_WRITE) ||
207 (smb_filter & FILE_NOTIFY_CHANGE_LAST_ACCESS))
208 return NOTIFY_ACTION_MODIFIED;
210 else if (ifs_filter & NOTE_LINK) {
211 ifs_filter &= ~NOTE_LINK;
212 /* NOTE_LINK will send out NO notifications */
214 else if (ifs_filter & NOTE_REVOKE) {
215 ifs_filter &= ~NOTE_REVOKE;
216 /* NOTE_REVOKE will send out NO notifications */
218 else if (ifs_filter & NOTE_RENAME_FROM) {
219 ifs_filter &= ~NOTE_RENAME_FROM;
221 if (((smb_filter & FILE_NOTIFY_CHANGE_FILE_NAME) &&
222 (ifs_filter & NOTE_FILE)) ||
223 ((smb_filter & FILE_NOTIFY_CHANGE_DIR_NAME) &&
224 (ifs_filter & NOTE_DIRECTORY))) {
225 /* Check if this is a RENAME, not a MOVE */
226 if (ifs_filter & NOTE_RENAME_SAMEDIR) {
227 /* Remove the NOTE_RENAME_SAMEDIR, IFF
228 * RENAME_TO is not in this event */
229 if (!(ifs_filter & NOTE_RENAME_TO))
230 ifs_filter &=
231 ~NOTE_RENAME_SAMEDIR;
232 return NOTIFY_ACTION_OLD_NAME;
234 return NOTIFY_ACTION_REMOVED;
237 else if (ifs_filter & NOTE_RENAME_TO) {
238 ifs_filter &= ~NOTE_RENAME_TO;
240 if (((smb_filter & FILE_NOTIFY_CHANGE_FILE_NAME) &&
241 (ifs_filter & NOTE_FILE)) ||
242 ((smb_filter & FILE_NOTIFY_CHANGE_DIR_NAME) &&
243 (ifs_filter & NOTE_DIRECTORY))) {
244 /* Check if this is a RENAME, not a MOVE */
245 if (ifs_filter & NOTE_RENAME_SAMEDIR) {
246 /* Remove the NOTE_RENAME_SAMEDIR, IFF
247 * RENAME_FROM is not in this event */
248 if (!(ifs_filter & NOTE_RENAME_FROM))
249 ifs_filter &=
250 ~NOTE_RENAME_SAMEDIR;
251 return NOTIFY_ACTION_NEW_NAME;
253 return NOTIFY_ACTION_ADDED;
255 /* RAW-NOTIFY shows us that a rename triggers a
256 * creation time change */
257 if ((smb_filter & FILE_NOTIFY_CHANGE_CREATION) &&
258 (ifs_filter & NOTE_FILE))
259 return NOTIFY_ACTION_MODIFIED;
261 else if (ifs_filter & NOTE_SECURITY) {
262 ifs_filter &= ~NOTE_SECURITY;
264 if (smb_filter & FILE_NOTIFY_CHANGE_SECURITY)
265 return NOTIFY_ACTION_MODIFIED;
266 } else {
267 /* No relevant flags found */
268 return 0;
274 * Retrieve a directory path of a changed file, relative to the watched dir
276 * @param[in] wc watch context for the returned event
277 * @param[in] e ifs_event notification returned from the kernel
278 * @param[out] path name relative to the watched dir. This is talloced
279 * off of wc and needs to be freed by the caller.
281 * @return true on success
283 * TODO: This function currently doesn't handle path names with multiple
284 * encodings. enc_get_lin_path() should be used in the future to convert
285 * each path segment's encoding to UTF-8
287 static bool
288 get_ifs_event_path(struct onefs_notify_watch_context *wc, struct ifs_event *e,
289 char **path)
291 char *path_buf = NULL;
292 size_t pathlen = 0;
293 int error = 0;
295 SMB_ASSERT(path);
297 /* Lookup the path from watch_dir through our parent dir */
298 if (e->namelen > 0) {
299 error = lin_get_path(wc->watch_lin,
300 e->parent_lin,
301 HEAD_SNAPID,
302 e->parent_parent_lin,
303 e->parent_name_hash,
304 &pathlen, &path_buf);
305 if (!error) {
306 /* Only add slash if a path exists in path_buf from
307 * lin_get_path call. Windows does not expect a
308 * leading '/' */
309 if (pathlen > 0)
310 *path = talloc_asprintf(wc, "%s/%s",
311 path_buf, e->name);
312 else
313 *path = talloc_asprintf(wc, "%s", e->name);
314 SAFE_FREE(path_buf);
318 /* If ifs_event didn't return a name, or we errored out of our intial
319 * path lookup, try again using the lin of the changed file */
320 if (!(*path)) {
321 error = lin_get_path(wc->watch_lin,
322 e->lin,
323 HEAD_SNAPID,
324 e->parent_lin,
325 e->name_hash,
326 &pathlen, &path_buf);
327 if (error) {
328 /* It's possible that both the lin and the parent lin
329 * are invalid (or not given) -- we will skip these
330 * events. */
331 DEBUG(3,("Path lookup failed. LINS are invalid: "
332 "e->lin: 0x%llu, e->parent_lin: 0x%llu, "
333 "e->parent_parent_lin: 0x%llu\n",
334 e->lin, e->parent_lin, e->parent_parent_lin));
335 SAFE_FREE(path_buf);
336 return false;
337 } else {
338 *path = talloc_asprintf(wc, "%s", path_buf);
339 DEBUG(5, ("Using path from event LIN = %s\n",
340 path_buf));
341 SAFE_FREE(path_buf);
345 /* Replacement of UNIX slashes with WIN slashes is handled at a
346 * higher layer. */
348 return true;
352 * Dispatch one OneFS notify event to the general Samba code
354 * @param[in] wc watch context for the returned event
355 * @param[in] e event returned from the kernel
357 * @return nothing
359 static void
360 onefs_notify_dispatch(struct onefs_notify_watch_context *wc,
361 struct ifs_event *e)
363 char *path = NULL;
364 struct notify_event ne;
366 DEBUG(5, ("Retrieved ifs event from kernel: lin=%#llx, ifs_events=%#x, "
367 "parent_lin=%#llx, namelen=%d, name=\"%s\"\n",
368 e->lin, e->events, e->parent_lin, e->namelen, e->name));
370 /* Check validity of event returned from kernel */
371 if (e->lin == 0) {
372 /* The lin == 0 specifies 1 of 2 cases:
373 * 1) We are out of events. The kernel has a limited
374 * amount (somewhere near 90000)
375 * 2) Split nodes have merged back and had data written
376 * to them -- thus we've missed some of those events. */
377 DEBUG(3, ("We've missed some kernel ifs events!\n"));
379 /* Alert higher level to the problem so it returns catch-all
380 * response to the client */
381 ne.path = NULL;
382 ne.action = 0;
383 wc->callback(wc->ctx, wc->private_data, &ne);
386 if (e->lin == wc->watch_lin) {
387 /* Windows doesn't report notifications on root
388 * watched directory */
389 /* TODO: This should be abstracted out to the general layer
390 * instead of being handled in every notify provider */
391 DEBUG(5, ("Skipping notification on root of the watched "
392 "path.\n"));
393 return;
396 /* Retrieve the full path for the ifs event name */
397 if(!get_ifs_event_path(wc, e, &path)) {
398 DEBUG(3, ("Failed to convert the ifs_event lins to a path. "
399 "Skipping this event\n"));
400 return;
403 if (!strncmp(path, ".ifsvar", 7)) {
404 /* Skip notifications on file if its in ifs configuration
405 * directory */
406 goto clean;
409 ne.path = path;
411 /* Convert ifs event mask to an smb action mask */
412 ne.action = onefs_notify_ifs_filter_to_smb_action(wc->smb_filter,
413 e->events);
415 DEBUG(5, ("Converted smb_filter=%#x, ifs_events=%#x, to "
416 "ne.action = %d, for ne.path = %s\n",
417 wc->smb_filter, e->events, ne.action, ne.path));
419 if (!ne.action)
420 goto clean;
422 /* Return notify_event to higher level */
423 wc->callback(wc->ctx, wc->private_data, &ne);
425 /* SMB expects a file rename/move to generate three actions, a
426 * rename_from/delete on the from file, a rename_to/create on the to
427 * file, and a modify for the rename_to file. If we have two separate
428 * notifications registered for ATTRIBUTES and FILENAME, this case will
429 * be handled by separate ifs_events in
430 * onefs_notify_ifs_filter_to_smb_action(). If both bits are registered
431 * in the same notification, we must send an extra MODIFIED action
432 * here. */
433 if ((wc->smb_filter & FILE_NOTIFY_CHANGE_ATTRIBUTES) &&
434 (wc->smb_filter & FILE_NOTIFY_CHANGE_FILE_NAME) &&
435 (e->events & NOTE_FILE) && (e->events & NOTE_RENAME_TO))
437 ne.action = NOTIFY_ACTION_MODIFIED;
438 wc->callback(wc->ctx, wc->private_data, &ne);
441 /* FALLTHROUGH */
442 clean:
443 talloc_free(path);
444 return;
448 * Callback when the kernel has some events for us
450 * Read events off ifs event fd and pass them to our dispatch function
452 * @param ev context of all tevents registered in the smbd
453 * @param fde tevent struct specific to one ifs event channel
454 * @param flags tevent flags passed when we added our ifs event channel fd to
455 * the main loop
456 * @param private_data onefs_notify_watch_context specific to this ifs event
457 * channel
459 * @return nothing
461 static void
462 onefs_notify_handler(struct event_context *ev,
463 struct fd_event *fde,
464 uint16_t flags,
465 void *private_data)
467 struct onefs_notify_watch_context *wc = NULL;
468 struct ifs_event ifs_events[ONEFS_IFS_EVENT_MAX_NUM];
469 ssize_t nread = 0;
470 int count = 0;
471 int i = 0;
473 wc = talloc_get_type(private_data, struct onefs_notify_watch_context);
475 /* Read as many ifs events from the notify channel as we can */
476 nread = sys_read(wc->ifs_event_fd, &ifs_events,
477 ONEFS_IFS_EVENT_MAX_BYTES);
478 if (nread == 0) {
479 DEBUG(0,("No data found while reading ifs event fd?!\n"));
480 return;
482 if (nread < 0) {
483 DEBUG(0,("Failed to read ifs event data: %s\n",
484 strerror(errno)));
485 return;
488 count = nread / sizeof(struct ifs_event);
490 DEBUG(5, ("Got %d notification events in %d bytes.\n", count, nread));
492 /* Dispatch ifs_events one-at-a-time to higher level */
493 for (i=0; i < count; i++) {
494 onefs_notify_dispatch(wc, &ifs_events[i]);
499 * Destroy the ifs event channel
501 * This is called from talloc_free() when the generic Samba notify layer frees
502 * the onefs_notify_watch_context.
504 * @param[in] wc pointer to watch context which is being destroyed
506 * return 0 on success
508 static int
509 onefs_watch_destructor(struct onefs_notify_watch_context *wc)
511 /* The ifs_event_fd will re de-registered from the event loop by
512 * another destructor triggered from the freeing of this wc */
513 close(wc->ifs_event_fd);
514 return 0;
518 * Register a single change notify watch request.
520 * Open an event listener on a directory to watch for modifications. This
521 * channel is closed by a destructor when the caller calls talloc_free()
522 * on *handle.
524 * @param[in] vfs_handle handle given to most VFS operations
525 * @param[in] ctx context (conn and tevent) for this connection
526 * @param[in] e filter and path of client's notify request
527 * @param[in] callback function to call when file notification event is received
528 * from the kernel, passing that event up to Samba's
529 * generalized change notify layer
530 * @param[in] private_data opaque data given to us by the general change notify
531 * layer which must be returned in the callback function
532 * @param[out] handle_p handle returned to generalized change notify layer used
533 * to close the event channel when notification is
534 * cancelled
536 * @return NT_STATUS_OK unless error
538 NTSTATUS
539 onefs_notify_watch(vfs_handle_struct *vfs_handle,
540 struct sys_notify_context *ctx,
541 struct notify_entry *e,
542 void (*callback)(struct sys_notify_context *ctx,
543 void *private_data,
544 struct notify_event *ev),
545 void *private_data,
546 void *handle_p)
548 int ifs_event_fd = -1;
549 uint32_t ifs_filter = 0;
550 uint32_t smb_filter = e->filter;
551 bool watch_tree = !!e->subdir_filter;
552 struct onefs_notify_watch_context *wc = NULL;
553 void **handle = (void **)handle_p;
554 SMB_STRUCT_STAT sbuf;
555 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
557 /* Fallback to default Samba implementation if kernel CN is disabled */
558 if (!lp_kernel_change_notify(vfs_handle->conn->params)) {
559 (*handle) = NULL;
560 return NT_STATUS_OK;
563 /* The OneFS open path should always give us a valid fd on a directory*/
564 SMB_ASSERT(e->dir_fd >= 0);
566 /* Always set e->filter to 0 so we don't fallback on the default change
567 * notify backend. It's not cluster coherent or cross-protocol so we
568 * can't guarantee correctness using it. */
569 e->filter = 0;
570 e->subdir_filter = 0;
572 /* Snapshots do not currently allow event listeners. See Isilon
573 * bug 33476 for an example of .snapshot debug spew that can occur. */
574 if (e->dir_id.extid != HEAD_SNAPID)
575 return NT_STATUS_INVALID_PARAMETER;
577 /* Convert Completion Filter mask to IFS Event mask */
578 ifs_filter = onefs_notify_smb_filter_to_ifs_filter(smb_filter);
580 if (smb_filter & ONEFS_NOTIFY_UNSUPPORTED) {
581 /* One or more of the filter bits could not be fully handled by
582 * the ifs_event system. To be correct, if we cannot service a
583 * bit in the completion filter we should return
584 * NT_STATUS_NOT_IMPLEMENTED to let the client know that they
585 * won't be receiving all the notify events that they asked for.
586 * Unfortunately, WinXP clients cache this error message, stop
587 * trying to send any notify requests at all, and instead return
588 * NOT_IMPLEMENTED to all requesting apps without ever sending a
589 * message to us. Thus we lie, and say we can service all bits,
590 * but simply don't return actions for the filter bits we can't
591 * detect or fully implement. */
592 DEBUG(3,("One or more of the Windows completion filter bits "
593 "for \"%s\" could not be fully handled by the "
594 "ifs_event system. The failed bits are "
595 "smb_filter=%#x\n",
596 e->path, smb_filter & ONEFS_NOTIFY_UNSUPPORTED));
599 if (ifs_filter == 0) {
600 /* None of the filter bits given are supported by the ifs_event
601 * system. Don't create a kernel notify channel, but mock
602 * up a fake handle for the caller. */
603 DEBUG(3,("No bits in the Windows completion filter could be "
604 "translated to ifs_event mask for \"%s\", "
605 "smb_filter=%#x\n", e->path, smb_filter));
606 (*handle) = NULL;
607 return NT_STATUS_OK;
610 /* Register an ifs event channel for this watch request */
611 ifs_event_fd = ifs_create_listener(watch_tree ?
612 EVENT_RECURSIVE :
613 EVENT_CHILDREN,
614 ifs_filter,
615 e->dir_fd);
616 if (ifs_event_fd < 0) {
617 DEBUG(0,("Failed to create listener for %s with \"%s\". "
618 "smb_filter=0x%x, ifs_filter=0x%x, watch_tree=%u\n",
619 strerror(errno), e->path, smb_filter, ifs_filter,
620 watch_tree));
621 return map_nt_error_from_unix(errno);
624 /* Create a watch context to track this change notify request */
625 wc = talloc(ctx, struct onefs_notify_watch_context);
626 if (wc == NULL) {
627 status = NT_STATUS_NO_MEMORY;
628 goto err;
631 /* Get LIN for directory */
632 if (sys_fstat(e->dir_fd, &sbuf)) {
633 DEBUG(0, ("stat on directory fd failed: %s\n",
634 strerror(errno)));
635 status = map_nt_error_from_unix(errno);
636 goto err;
639 if (sbuf.st_ino == 0) {
640 DEBUG(0, ("0 LIN found!\n"));
641 goto err;
644 wc->ctx = ctx;
645 wc->watch_fd = e->dir_fd;
646 wc->watch_lin = sbuf.st_ino;
647 wc->ifs_event_fd = ifs_event_fd;
648 wc->ifs_filter = ifs_filter;
649 wc->smb_filter = smb_filter;
650 wc->callback = callback;
651 wc->private_data = private_data;
652 wc->path = talloc_strdup(wc, e->path);
653 if (wc->path == NULL) {
654 status = NT_STATUS_NO_MEMORY;
655 goto err;
658 (*handle) = wc;
660 /* The caller frees the handle to stop watching */
661 talloc_set_destructor(wc, onefs_watch_destructor);
663 /* Add a tevent waiting for the ifs event fd to be readable */
664 event_add_fd(ctx->ev, wc, wc->ifs_event_fd, EVENT_FD_READ,
665 onefs_notify_handler, wc);
667 DEBUG(10, ("Watching for changes on \"%s\" smb_filter=0x%x, "
668 "ifs_filter=0x%x, watch_tree=%d, ifs_event_fd=%d, "
669 "dir_fd=%d, dir_lin=0x%llx\n",
670 e->path, smb_filter, ifs_filter, watch_tree,
671 ifs_event_fd, e->dir_fd, sbuf.st_ino));
673 return NT_STATUS_OK;
675 err:
676 talloc_free(wc);
677 SMB_ASSERT(ifs_event_fd >= 0);
678 close(ifs_event_fd);
679 return status;