Lines Matching full:mark
20 * fsnotify inode mark locking/lifetime/and refcnting
23 * The group->recnt and mark->refcnt tell how many "things" in the kernel
26 * the reference a group and a mark hold to each other.
35 * mark->lock
36 * mark->connector->lock
39 * each mark is hooked via the g_list. It also protects the groups private
42 * mark->lock protects the marks attributes like its masks and flags.
43 * Furthermore it protects the access to a reference of the group that the mark
45 * that is being watched by the mark.
47 * mark->connector->lock protects the list of marks anchored inside an
48 * inode / vfsmount and each mark is hooked via the i_list.
52 * marks in the list and is also protected by fsnotify_mark_srcu. A mark gets
53 * detached from fsnotify_mark_connector when last reference to the mark is
54 * dropped. Thus having mark reference is enough to protect mark->connector
56 * because we remove mark from g_list before dropping mark reference associated
57 * with that, any mark found through g_list is guaranteed to have
58 * mark->connector set until we drop group->mark_mutex.
64 * The inode mark can be cleared for a number of different reasons including:
69 * - The fsnotify_group associated with the mark is going away and all such marks
106 void fsnotify_get_mark(struct fsnotify_mark *mark) in fsnotify_get_mark() argument
108 WARN_ON_ONCE(!refcount_read(&mark->refcnt)); in fsnotify_get_mark()
109 refcount_inc(&mark->refcnt); in fsnotify_get_mark()
132 struct fsnotify_mark *mark; in __fsnotify_recalc_mask() local
138 hlist_for_each_entry(mark, &conn->list, obj_list) { in __fsnotify_recalc_mask()
139 if (mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED) in __fsnotify_recalc_mask()
140 new_mask |= mark->mask; in __fsnotify_recalc_mask()
148 * this by holding a mark->lock or mark->group->mark_mutex for a mark on this
207 static void fsnotify_final_mark_destroy(struct fsnotify_mark *mark) in fsnotify_final_mark_destroy() argument
209 struct fsnotify_group *group = mark->group; in fsnotify_final_mark_destroy()
213 group->ops->free_mark(mark); in fsnotify_final_mark_destroy()
235 void fsnotify_put_mark(struct fsnotify_mark *mark) in fsnotify_put_mark() argument
243 if (!mark->connector) { in fsnotify_put_mark()
244 if (refcount_dec_and_test(&mark->refcnt)) in fsnotify_put_mark()
245 fsnotify_final_mark_destroy(mark); in fsnotify_put_mark()
251 * safely grab mark reference. in fsnotify_put_mark()
253 if (!refcount_dec_and_lock(&mark->refcnt, &mark->connector->lock)) in fsnotify_put_mark()
256 conn = mark->connector; in fsnotify_put_mark()
257 hlist_del_init_rcu(&mark->obj_list); in fsnotify_put_mark()
264 mark->connector = NULL; in fsnotify_put_mark()
283 list_add(&mark->g_list, &destroy_list); in fsnotify_put_mark()
290 * Get mark reference when we found the mark via lockless traversal of object
291 * list. Mark can be already removed from the list by now and on its way to be
296 static bool fsnotify_get_mark_safe(struct fsnotify_mark *mark) in fsnotify_get_mark_safe() argument
298 if (!mark) in fsnotify_get_mark_safe()
301 if (refcount_inc_not_zero(&mark->refcnt)) { in fsnotify_get_mark_safe()
302 spin_lock(&mark->lock); in fsnotify_get_mark_safe()
303 if (mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED) { in fsnotify_get_mark_safe()
304 /* mark is attached, group is still alive then */ in fsnotify_get_mark_safe()
305 atomic_inc(&mark->group->user_waits); in fsnotify_get_mark_safe()
306 spin_unlock(&mark->lock); in fsnotify_get_mark_safe()
309 spin_unlock(&mark->lock); in fsnotify_get_mark_safe()
310 fsnotify_put_mark(mark); in fsnotify_get_mark_safe()
320 static void fsnotify_put_mark_wake(struct fsnotify_mark *mark) in fsnotify_put_mark_wake() argument
322 if (mark) { in fsnotify_put_mark_wake()
323 struct fsnotify_group *group = mark->group; in fsnotify_put_mark_wake()
325 fsnotify_put_mark(mark); in fsnotify_put_mark_wake()
340 /* This can fail if mark is being removed */ in fsnotify_prepare_user_wait()
370 * Mark mark as detached, remove it from group list. Mark still stays in object
371 * list until its last reference is dropped. Note that we rely on mark being
373 * particular we rely on mark->connector being valid while we hold
374 * group->mark_mutex if we found the mark through g_list.
377 * reference to the mark or be protected by fsnotify_mark_srcu.
379 void fsnotify_detach_mark(struct fsnotify_mark *mark) in fsnotify_detach_mark() argument
381 struct fsnotify_group *group = mark->group; in fsnotify_detach_mark()
385 refcount_read(&mark->refcnt) < 1 + in fsnotify_detach_mark()
386 !!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)); in fsnotify_detach_mark()
388 spin_lock(&mark->lock); in fsnotify_detach_mark()
389 /* something else already called this function on this mark */ in fsnotify_detach_mark()
390 if (!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) { in fsnotify_detach_mark()
391 spin_unlock(&mark->lock); in fsnotify_detach_mark()
394 mark->flags &= ~FSNOTIFY_MARK_FLAG_ATTACHED; in fsnotify_detach_mark()
395 list_del_init(&mark->g_list); in fsnotify_detach_mark()
396 spin_unlock(&mark->lock); in fsnotify_detach_mark()
400 /* Drop mark reference acquired in fsnotify_add_mark_locked() */ in fsnotify_detach_mark()
401 fsnotify_put_mark(mark); in fsnotify_detach_mark()
405 * Free fsnotify mark. The mark is actually only marked as being freed. The
406 * freeing is actually happening only once last reference to the mark is
409 * Caller must have a reference to the mark or be protected by
412 void fsnotify_free_mark(struct fsnotify_mark *mark) in fsnotify_free_mark() argument
414 struct fsnotify_group *group = mark->group; in fsnotify_free_mark()
416 spin_lock(&mark->lock); in fsnotify_free_mark()
417 /* something else already called this function on this mark */ in fsnotify_free_mark()
418 if (!(mark->flags & FSNOTIFY_MARK_FLAG_ALIVE)) { in fsnotify_free_mark()
419 spin_unlock(&mark->lock); in fsnotify_free_mark()
422 mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE; in fsnotify_free_mark()
423 spin_unlock(&mark->lock); in fsnotify_free_mark()
427 * callback to the group function to let it know that this mark in fsnotify_free_mark()
431 group->ops->freeing_mark(mark, group); in fsnotify_free_mark()
434 void fsnotify_destroy_mark(struct fsnotify_mark *mark, in fsnotify_destroy_mark() argument
438 fsnotify_detach_mark(mark); in fsnotify_destroy_mark()
440 fsnotify_free_mark(mark); in fsnotify_destroy_mark()
509 * Get mark connector, make sure it is alive and return with its lock held.
511 * hold reference to a mark on the list may directly lock connector->lock as
536 * Add mark into proper place in given list of marks. These marks may be used
541 static int fsnotify_add_mark_list(struct fsnotify_mark *mark, in fsnotify_add_mark_list() argument
553 spin_lock(&mark->lock); in fsnotify_add_mark_list()
556 spin_unlock(&mark->lock); in fsnotify_add_mark_list()
563 /* is mark the first mark? */ in fsnotify_add_mark_list()
565 hlist_add_head_rcu(&mark->obj_list, &conn->list); in fsnotify_add_mark_list()
569 /* should mark be in the middle of the current list? */ in fsnotify_add_mark_list()
573 if ((lmark->group == mark->group) && in fsnotify_add_mark_list()
580 cmp = fsnotify_compare_groups(lmark->group, mark->group); in fsnotify_add_mark_list()
582 hlist_add_before_rcu(&mark->obj_list, &lmark->obj_list); in fsnotify_add_mark_list()
588 /* mark should be the last entry. last is the current last entry */ in fsnotify_add_mark_list()
589 hlist_add_behind_rcu(&mark->obj_list, &last->obj_list); in fsnotify_add_mark_list()
591 mark->connector = conn; in fsnotify_add_mark_list()
594 spin_unlock(&mark->lock); in fsnotify_add_mark_list()
599 * Attach an initialized mark to a given group and fs object.
603 int fsnotify_add_mark_locked(struct fsnotify_mark *mark, in fsnotify_add_mark_locked() argument
607 struct fsnotify_group *group = mark->group; in fsnotify_add_mark_locked()
615 * mark->lock in fsnotify_add_mark_locked()
616 * mark->connector->lock in fsnotify_add_mark_locked()
618 spin_lock(&mark->lock); in fsnotify_add_mark_locked()
619 mark->flags |= FSNOTIFY_MARK_FLAG_ALIVE | FSNOTIFY_MARK_FLAG_ATTACHED; in fsnotify_add_mark_locked()
621 list_add(&mark->g_list, &group->marks_list); in fsnotify_add_mark_locked()
623 fsnotify_get_mark(mark); /* for g_list */ in fsnotify_add_mark_locked()
624 spin_unlock(&mark->lock); in fsnotify_add_mark_locked()
626 ret = fsnotify_add_mark_list(mark, connp, type, allow_dups); in fsnotify_add_mark_locked()
630 if (mark->mask) in fsnotify_add_mark_locked()
631 fsnotify_recalc_mask(mark->connector); in fsnotify_add_mark_locked()
635 spin_lock(&mark->lock); in fsnotify_add_mark_locked()
636 mark->flags &= ~(FSNOTIFY_MARK_FLAG_ALIVE | in fsnotify_add_mark_locked()
638 list_del_init(&mark->g_list); in fsnotify_add_mark_locked()
639 spin_unlock(&mark->lock); in fsnotify_add_mark_locked()
642 fsnotify_put_mark(mark); in fsnotify_add_mark_locked()
646 int fsnotify_add_mark(struct fsnotify_mark *mark, fsnotify_connp_t *connp, in fsnotify_add_mark() argument
650 struct fsnotify_group *group = mark->group; in fsnotify_add_mark()
653 ret = fsnotify_add_mark_locked(mark, connp, type, allow_dups); in fsnotify_add_mark()
659 * Given a list of marks, find the mark associated with given group. If found
660 * take a reference to that mark and return it, else return NULL.
666 struct fsnotify_mark *mark; in fsnotify_find_mark() local
672 hlist_for_each_entry(mark, &conn->list, obj_list) { in fsnotify_find_mark()
673 if (mark->group == group && in fsnotify_find_mark()
674 (mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) { in fsnotify_find_mark()
675 fsnotify_get_mark(mark); in fsnotify_find_mark()
677 return mark; in fsnotify_find_mark()
688 struct fsnotify_mark *lmark, *mark; in fsnotify_clear_marks_by_group() local
701 * list. And freeing mark requires us to drop mark_mutex. So we can in fsnotify_clear_marks_by_group()
702 * reliably free only the first mark in the list. That's why we first in fsnotify_clear_marks_by_group()
707 list_for_each_entry_safe(mark, lmark, &group->marks_list, g_list) { in fsnotify_clear_marks_by_group()
708 if ((1U << mark->connector->type) & type_mask) in fsnotify_clear_marks_by_group()
709 list_move(&mark->g_list, &to_free); in fsnotify_clear_marks_by_group()
720 mark = list_first_entry(head, struct fsnotify_mark, g_list); in fsnotify_clear_marks_by_group()
721 fsnotify_get_mark(mark); in fsnotify_clear_marks_by_group()
722 fsnotify_detach_mark(mark); in fsnotify_clear_marks_by_group()
724 fsnotify_free_mark(mark); in fsnotify_clear_marks_by_group()
725 fsnotify_put_mark(mark); in fsnotify_clear_marks_by_group()
733 struct fsnotify_mark *mark, *old_mark = NULL; in fsnotify_destroy_marks() local
743 * list can get modified. However we are holding mark reference and in fsnotify_destroy_marks()
744 * thus our mark cannot be removed from obj_list so we can continue in fsnotify_destroy_marks()
747 hlist_for_each_entry(mark, &conn->list, obj_list) { in fsnotify_destroy_marks()
748 fsnotify_get_mark(mark); in fsnotify_destroy_marks()
752 old_mark = mark; in fsnotify_destroy_marks()
753 fsnotify_destroy_mark(mark, mark->group); in fsnotify_destroy_marks()
758 * mark references get dropped. It would lead to strange results such in fsnotify_destroy_marks()
771 void fsnotify_init_mark(struct fsnotify_mark *mark, in fsnotify_init_mark() argument
774 memset(mark, 0, sizeof(*mark)); in fsnotify_init_mark()
775 spin_lock_init(&mark->lock); in fsnotify_init_mark()
776 refcount_set(&mark->refcnt, 1); in fsnotify_init_mark()
778 mark->group = group; in fsnotify_init_mark()
787 struct fsnotify_mark *mark, *next; in fsnotify_mark_destroy_workfn() local
797 list_for_each_entry_safe(mark, next, &private_destroy_list, g_list) { in fsnotify_mark_destroy_workfn()
798 list_del_init(&mark->g_list); in fsnotify_mark_destroy_workfn()
799 fsnotify_final_mark_destroy(mark); in fsnotify_mark_destroy_workfn()