Lines Matching full:mark
7 * fsnotify inode mark locking/lifetime/and refcnting
10 * The group->recnt and mark->refcnt tell how many "things" in the kernel
13 * the reference a group and a mark hold to each other.
22 * mark->lock
23 * mark->connector->lock
26 * each mark is hooked via the g_list. It also protects the groups private
29 * mark->lock protects the marks attributes like its masks and flags.
30 * Furthermore it protects the access to a reference of the group that the mark
32 * that is being watched by the mark.
34 * mark->connector->lock protects the list of marks anchored inside an
35 * inode / vfsmount and each mark is hooked via the i_list.
39 * marks in the list and is also protected by fsnotify_mark_srcu. A mark gets
40 * detached from fsnotify_mark_connector when last reference to the mark is
41 * dropped. Thus having mark reference is enough to protect mark->connector
43 * because we remove mark from g_list before dropping mark reference associated
44 * with that, any mark found through g_list is guaranteed to have
45 * mark->connector set until we drop group->mark_mutex.
51 * The inode mark can be cleared for a number of different reasons including:
56 * - The fsnotify_group associated with the mark is going away and all such marks
94 void fsnotify_get_mark(struct fsnotify_mark *mark) in fsnotify_get_mark() argument
96 WARN_ON_ONCE(!refcount_read(&mark->refcnt)); in fsnotify_get_mark()
97 refcount_inc(&mark->refcnt); in fsnotify_get_mark()
214 * iput() outside of spinlocks. This happens when last mark that wanted iref is
228 /* Pin inode if any mark wants inode refcount held */ in fsnotify_update_iref()
232 /* Unpin inode after detach of last mark that wanted iref */ in fsnotify_update_iref()
244 struct fsnotify_mark *mark; in __fsnotify_recalc_mask() local
250 hlist_for_each_entry(mark, &conn->list, obj_list) { in __fsnotify_recalc_mask()
251 if (!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) in __fsnotify_recalc_mask()
253 new_mask |= fsnotify_calc_mask(mark); in __fsnotify_recalc_mask()
255 !(mark->flags & FSNOTIFY_MARK_FLAG_NO_IREF)) in __fsnotify_recalc_mask()
288 * this by holding a mark->lock or mark->group->mark_mutex for a mark on this
366 static void fsnotify_final_mark_destroy(struct fsnotify_mark *mark) in fsnotify_final_mark_destroy() argument
368 struct fsnotify_group *group = mark->group; in fsnotify_final_mark_destroy()
372 group->ops->free_mark(mark); in fsnotify_final_mark_destroy()
387 void fsnotify_put_mark(struct fsnotify_mark *mark) in fsnotify_put_mark() argument
389 struct fsnotify_mark_connector *conn = READ_ONCE(mark->connector); in fsnotify_put_mark()
396 if (refcount_dec_and_test(&mark->refcnt)) in fsnotify_put_mark()
397 fsnotify_final_mark_destroy(mark); in fsnotify_put_mark()
403 * safely grab mark reference. in fsnotify_put_mark()
405 if (!refcount_dec_and_lock(&mark->refcnt, &conn->lock)) in fsnotify_put_mark()
408 hlist_del_init_rcu(&mark->obj_list); in fsnotify_put_mark()
415 /* Update watched objects after detaching mark */ in fsnotify_put_mark()
421 WRITE_ONCE(mark->connector, NULL); in fsnotify_put_mark()
440 list_add(&mark->g_list, &destroy_list); in fsnotify_put_mark()
448 * Get mark reference when we found the mark via lockless traversal of object
449 * list. Mark can be already removed from the list by now and on its way to be
454 static bool fsnotify_get_mark_safe(struct fsnotify_mark *mark) in fsnotify_get_mark_safe() argument
456 if (!mark) in fsnotify_get_mark_safe()
459 if (refcount_inc_not_zero(&mark->refcnt)) { in fsnotify_get_mark_safe()
460 spin_lock(&mark->lock); in fsnotify_get_mark_safe()
461 if (mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED) { in fsnotify_get_mark_safe()
462 /* mark is attached, group is still alive then */ in fsnotify_get_mark_safe()
463 atomic_inc(&mark->group->user_waits); in fsnotify_get_mark_safe()
464 spin_unlock(&mark->lock); in fsnotify_get_mark_safe()
467 spin_unlock(&mark->lock); in fsnotify_get_mark_safe()
468 fsnotify_put_mark(mark); in fsnotify_get_mark_safe()
478 static void fsnotify_put_mark_wake(struct fsnotify_mark *mark) in fsnotify_put_mark_wake() argument
480 if (mark) { in fsnotify_put_mark_wake()
481 struct fsnotify_group *group = mark->group; in fsnotify_put_mark_wake()
483 fsnotify_put_mark(mark); in fsnotify_put_mark_wake()
499 /* This can fail if mark is being removed */ in fsnotify_prepare_user_wait()
532 * Mark mark as detached, remove it from group list. Mark still stays in object
533 * list until its last reference is dropped. Note that we rely on mark being
535 * particular we rely on mark->connector being valid while we hold
536 * group->mark_mutex if we found the mark through g_list.
539 * reference to the mark or be protected by fsnotify_mark_srcu.
541 void fsnotify_detach_mark(struct fsnotify_mark *mark) in fsnotify_detach_mark() argument
543 fsnotify_group_assert_locked(mark->group); in fsnotify_detach_mark()
545 refcount_read(&mark->refcnt) < 1 + in fsnotify_detach_mark()
546 !!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)); in fsnotify_detach_mark()
548 spin_lock(&mark->lock); in fsnotify_detach_mark()
549 /* something else already called this function on this mark */ in fsnotify_detach_mark()
550 if (!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) { in fsnotify_detach_mark()
551 spin_unlock(&mark->lock); in fsnotify_detach_mark()
554 mark->flags &= ~FSNOTIFY_MARK_FLAG_ATTACHED; in fsnotify_detach_mark()
555 list_del_init(&mark->g_list); in fsnotify_detach_mark()
556 spin_unlock(&mark->lock); in fsnotify_detach_mark()
558 /* Drop mark reference acquired in fsnotify_add_mark_locked() */ in fsnotify_detach_mark()
559 fsnotify_put_mark(mark); in fsnotify_detach_mark()
563 * Free fsnotify mark. The mark is actually only marked as being freed. The
564 * freeing is actually happening only once last reference to the mark is
567 * Caller must have a reference to the mark or be protected by
570 void fsnotify_free_mark(struct fsnotify_mark *mark) in fsnotify_free_mark() argument
572 struct fsnotify_group *group = mark->group; in fsnotify_free_mark()
574 spin_lock(&mark->lock); in fsnotify_free_mark()
575 /* something else already called this function on this mark */ in fsnotify_free_mark()
576 if (!(mark->flags & FSNOTIFY_MARK_FLAG_ALIVE)) { in fsnotify_free_mark()
577 spin_unlock(&mark->lock); in fsnotify_free_mark()
580 mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE; in fsnotify_free_mark()
581 spin_unlock(&mark->lock); in fsnotify_free_mark()
585 * callback to the group function to let it know that this mark in fsnotify_free_mark()
589 group->ops->freeing_mark(mark, group); in fsnotify_free_mark()
592 void fsnotify_destroy_mark(struct fsnotify_mark *mark, in fsnotify_destroy_mark() argument
596 fsnotify_detach_mark(mark); in fsnotify_destroy_mark()
598 fsnotify_free_mark(mark); in fsnotify_destroy_mark()
685 * Get mark connector, make sure it is alive and return with its lock held.
687 * hold reference to a mark on the list may directly lock connector->lock as
712 * Add mark into proper place in given list of marks. These marks may be used
717 static int fsnotify_add_mark_list(struct fsnotify_mark *mark, void *obj, in fsnotify_add_mark_list() argument
742 spin_lock(&mark->lock); in fsnotify_add_mark_list()
745 spin_unlock(&mark->lock); in fsnotify_add_mark_list()
752 /* is mark the first mark? */ in fsnotify_add_mark_list()
754 hlist_add_head_rcu(&mark->obj_list, &conn->list); in fsnotify_add_mark_list()
758 /* should mark be in the middle of the current list? */ in fsnotify_add_mark_list()
762 if ((lmark->group == mark->group) && in fsnotify_add_mark_list()
764 !(mark->group->flags & FSNOTIFY_GROUP_DUPS)) { in fsnotify_add_mark_list()
769 cmp = fsnotify_compare_groups(lmark->group, mark->group); in fsnotify_add_mark_list()
771 hlist_add_before_rcu(&mark->obj_list, &lmark->obj_list); in fsnotify_add_mark_list()
777 /* mark should be the last entry. last is the current last entry */ in fsnotify_add_mark_list()
778 hlist_add_behind_rcu(&mark->obj_list, &last->obj_list); in fsnotify_add_mark_list()
785 * seeing mark->connector set. in fsnotify_add_mark_list()
787 WRITE_ONCE(mark->connector, conn); in fsnotify_add_mark_list()
790 spin_unlock(&mark->lock); in fsnotify_add_mark_list()
795 * Attach an initialized mark to a given group and fs object.
799 int fsnotify_add_mark_locked(struct fsnotify_mark *mark, in fsnotify_add_mark_locked() argument
803 struct fsnotify_group *group = mark->group; in fsnotify_add_mark_locked()
811 * mark->lock in fsnotify_add_mark_locked()
812 * mark->connector->lock in fsnotify_add_mark_locked()
814 spin_lock(&mark->lock); in fsnotify_add_mark_locked()
815 mark->flags |= FSNOTIFY_MARK_FLAG_ALIVE | FSNOTIFY_MARK_FLAG_ATTACHED; in fsnotify_add_mark_locked()
817 list_add(&mark->g_list, &group->marks_list); in fsnotify_add_mark_locked()
818 fsnotify_get_mark(mark); /* for g_list */ in fsnotify_add_mark_locked()
819 spin_unlock(&mark->lock); in fsnotify_add_mark_locked()
821 ret = fsnotify_add_mark_list(mark, obj, obj_type, add_flags); in fsnotify_add_mark_locked()
825 fsnotify_recalc_mask(mark->connector); in fsnotify_add_mark_locked()
829 spin_lock(&mark->lock); in fsnotify_add_mark_locked()
830 mark->flags &= ~(FSNOTIFY_MARK_FLAG_ALIVE | in fsnotify_add_mark_locked()
832 list_del_init(&mark->g_list); in fsnotify_add_mark_locked()
833 spin_unlock(&mark->lock); in fsnotify_add_mark_locked()
835 fsnotify_put_mark(mark); in fsnotify_add_mark_locked()
839 int fsnotify_add_mark(struct fsnotify_mark *mark, void *obj, in fsnotify_add_mark() argument
843 struct fsnotify_group *group = mark->group; in fsnotify_add_mark()
846 ret = fsnotify_add_mark_locked(mark, obj, obj_type, add_flags); in fsnotify_add_mark()
853 * Given a list of marks, find the mark associated with given group. If found
854 * take a reference to that mark and return it, else return NULL.
861 struct fsnotify_mark *mark; in fsnotify_find_mark() local
870 hlist_for_each_entry(mark, &conn->list, obj_list) { in fsnotify_find_mark()
871 if (mark->group == group && in fsnotify_find_mark()
872 (mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) { in fsnotify_find_mark()
873 fsnotify_get_mark(mark); in fsnotify_find_mark()
875 return mark; in fsnotify_find_mark()
887 struct fsnotify_mark *lmark, *mark; in fsnotify_clear_marks_by_group() local
900 * list. And freeing mark requires us to drop mark_mutex. So we can in fsnotify_clear_marks_by_group()
901 * reliably free only the first mark in the list. That's why we first in fsnotify_clear_marks_by_group()
906 list_for_each_entry_safe(mark, lmark, &group->marks_list, g_list) { in fsnotify_clear_marks_by_group()
907 if (mark->connector->type == obj_type) in fsnotify_clear_marks_by_group()
908 list_move(&mark->g_list, &to_free); in fsnotify_clear_marks_by_group()
919 mark = list_first_entry(head, struct fsnotify_mark, g_list); in fsnotify_clear_marks_by_group()
920 fsnotify_get_mark(mark); in fsnotify_clear_marks_by_group()
921 fsnotify_detach_mark(mark); in fsnotify_clear_marks_by_group()
923 fsnotify_free_mark(mark); in fsnotify_clear_marks_by_group()
924 fsnotify_put_mark(mark); in fsnotify_clear_marks_by_group()
932 struct fsnotify_mark *mark, *old_mark = NULL; in fsnotify_destroy_marks() local
942 * list can get modified. However we are holding mark reference and in fsnotify_destroy_marks()
943 * thus our mark cannot be removed from obj_list so we can continue in fsnotify_destroy_marks()
946 hlist_for_each_entry(mark, &conn->list, obj_list) { in fsnotify_destroy_marks()
947 fsnotify_get_mark(mark); in fsnotify_destroy_marks()
951 old_mark = mark; in fsnotify_destroy_marks()
952 fsnotify_destroy_mark(mark, mark->group); in fsnotify_destroy_marks()
957 * mark references get dropped. It would lead to strange results such in fsnotify_destroy_marks()
970 void fsnotify_init_mark(struct fsnotify_mark *mark, in fsnotify_init_mark() argument
973 memset(mark, 0, sizeof(*mark)); in fsnotify_init_mark()
974 spin_lock_init(&mark->lock); in fsnotify_init_mark()
975 refcount_set(&mark->refcnt, 1); in fsnotify_init_mark()
977 mark->group = group; in fsnotify_init_mark()
978 WRITE_ONCE(mark->connector, NULL); in fsnotify_init_mark()
988 struct fsnotify_mark *mark, *next; in fsnotify_mark_destroy_workfn() local
998 list_for_each_entry_safe(mark, next, &private_destroy_list, g_list) { in fsnotify_mark_destroy_workfn()
999 list_del_init(&mark->g_list); in fsnotify_mark_destroy_workfn()
1000 fsnotify_final_mark_destroy(mark); in fsnotify_mark_destroy_workfn()