Lines Matching refs: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)
96 WARN_ON_ONCE(!refcount_read(&mark->refcnt));
97 refcount_inc(&mark->refcnt);
122 struct fsnotify_mark *mark;
128 hlist_for_each_entry(mark, &conn->list, obj_list) {
129 if (mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)
130 new_mask |= mark->mask;
138 * this by holding a mark->lock or mark->group->mark_mutex for a mark on this
199 static void fsnotify_final_mark_destroy(struct fsnotify_mark *mark)
201 struct fsnotify_group *group = mark->group;
205 group->ops->free_mark(mark);
227 void fsnotify_put_mark(struct fsnotify_mark *mark)
229 struct fsnotify_mark_connector *conn = READ_ONCE(mark->connector);
236 if (refcount_dec_and_test(&mark->refcnt))
237 fsnotify_final_mark_destroy(mark);
243 * safely grab mark reference.
245 if (!refcount_dec_and_lock(&mark->refcnt, &conn->lock))
248 hlist_del_init_rcu(&mark->obj_list);
255 WRITE_ONCE(mark->connector, NULL);
274 list_add(&mark->g_list, &destroy_list);
282 * Get mark reference when we found the mark via lockless traversal of object
288 static bool fsnotify_get_mark_safe(struct fsnotify_mark *mark)
290 if (!mark)
293 if (refcount_inc_not_zero(&mark->refcnt)) {
294 spin_lock(&mark->lock);
295 if (mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED) {
296 /* mark is attached, group is still alive then */
297 atomic_inc(&mark->group->user_waits);
298 spin_unlock(&mark->lock);
301 spin_unlock(&mark->lock);
302 fsnotify_put_mark(mark);
312 static void fsnotify_put_mark_wake(struct fsnotify_mark *mark)
314 if (mark) {
315 struct fsnotify_group *group = mark->group;
317 fsnotify_put_mark(mark);
333 /* This can fail if mark is being removed */
366 * Mark mark as detached, remove it from group list. Mark still stays in object
367 * list until its last reference is dropped. Note that we rely on mark being
369 * particular we rely on mark->connector being valid while we hold
370 * group->mark_mutex if we found the mark through g_list.
373 * reference to the mark or be protected by fsnotify_mark_srcu.
375 void fsnotify_detach_mark(struct fsnotify_mark *mark)
377 struct fsnotify_group *group = mark->group;
381 refcount_read(&mark->refcnt) < 1 +
382 !!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED));
384 spin_lock(&mark->lock);
385 /* something else already called this function on this mark */
386 if (!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) {
387 spin_unlock(&mark->lock);
390 mark->flags &= ~FSNOTIFY_MARK_FLAG_ATTACHED;
391 list_del_init(&mark->g_list);
392 spin_unlock(&mark->lock);
396 /* Drop mark reference acquired in fsnotify_add_mark_locked() */
397 fsnotify_put_mark(mark);
401 * Free fsnotify mark. The mark is actually only marked as being freed. The
402 * freeing is actually happening only once last reference to the mark is
405 * Caller must have a reference to the mark or be protected by
408 void fsnotify_free_mark(struct fsnotify_mark *mark)
410 struct fsnotify_group *group = mark->group;
412 spin_lock(&mark->lock);
413 /* something else already called this function on this mark */
414 if (!(mark->flags & FSNOTIFY_MARK_FLAG_ALIVE)) {
415 spin_unlock(&mark->lock);
418 mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE;
419 spin_unlock(&mark->lock);
423 * callback to the group function to let it know that this mark
427 group->ops->freeing_mark(mark, group);
430 void fsnotify_destroy_mark(struct fsnotify_mark *mark,
434 fsnotify_detach_mark(mark);
436 fsnotify_free_mark(mark);
515 * Get mark connector, make sure it is alive and return with its lock held.
517 * hold reference to a mark on the list may directly lock connector->lock as
542 * Add mark into proper place in given list of marks. These marks may be used
547 static int fsnotify_add_mark_list(struct fsnotify_mark *mark,
564 spin_lock(&mark->lock);
567 spin_unlock(&mark->lock);
595 /* is mark the first mark? */
597 hlist_add_head_rcu(&mark->obj_list, &conn->list);
601 /* should mark be in the middle of the current list? */
605 if ((lmark->group == mark->group) &&
612 cmp = fsnotify_compare_groups(lmark->group, mark->group);
614 hlist_add_before_rcu(&mark->obj_list, &lmark->obj_list);
620 /* mark should be the last entry. last is the current last entry */
621 hlist_add_behind_rcu(&mark->obj_list, &last->obj_list);
626 * seeing mark->connector set.
628 WRITE_ONCE(mark->connector, conn);
631 spin_unlock(&mark->lock);
636 * Attach an initialized mark to a given group and fs object.
640 int fsnotify_add_mark_locked(struct fsnotify_mark *mark,
644 struct fsnotify_group *group = mark->group;
652 * mark->lock
653 * mark->connector->lock
655 spin_lock(&mark->lock);
656 mark->flags |= FSNOTIFY_MARK_FLAG_ALIVE | FSNOTIFY_MARK_FLAG_ATTACHED;
658 list_add(&mark->g_list, &group->marks_list);
660 fsnotify_get_mark(mark); /* for g_list */
661 spin_unlock(&mark->lock);
663 ret = fsnotify_add_mark_list(mark, connp, type, allow_dups, fsid);
667 if (mark->mask)
668 fsnotify_recalc_mask(mark->connector);
672 spin_lock(&mark->lock);
673 mark->flags &= ~(FSNOTIFY_MARK_FLAG_ALIVE |
675 list_del_init(&mark->g_list);
676 spin_unlock(&mark->lock);
679 fsnotify_put_mark(mark);
683 int fsnotify_add_mark(struct fsnotify_mark *mark, fsnotify_connp_t *connp,
687 struct fsnotify_group *group = mark->group;
690 ret = fsnotify_add_mark_locked(mark, connp, type, allow_dups, fsid);
697 * Given a list of marks, find the mark associated with given group. If found
698 * take a reference to that mark and return it, else return NULL.
704 struct fsnotify_mark *mark;
710 hlist_for_each_entry(mark, &conn->list, obj_list) {
711 if (mark->group == group &&
712 (mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) {
713 fsnotify_get_mark(mark);
715 return mark;
727 struct fsnotify_mark *lmark, *mark;
740 * list. And freeing mark requires us to drop mark_mutex. So we can
741 * reliably free only the first mark in the list. That's why we first
746 list_for_each_entry_safe(mark, lmark, &group->marks_list, g_list) {
747 if ((1U << mark->connector->type) & type_mask)
748 list_move(&mark->g_list, &to_free);
759 mark = list_first_entry(head, struct fsnotify_mark, g_list);
760 fsnotify_get_mark(mark);
761 fsnotify_detach_mark(mark);
763 fsnotify_free_mark(mark);
764 fsnotify_put_mark(mark);
772 struct fsnotify_mark *mark, *old_mark = NULL;
782 * list can get modified. However we are holding mark reference and
783 * thus our mark cannot be removed from obj_list so we can continue
786 hlist_for_each_entry(mark, &conn->list, obj_list) {
787 fsnotify_get_mark(mark);
791 old_mark = mark;
792 fsnotify_destroy_mark(mark, mark->group);
797 * mark references get dropped. It would lead to strange results such
810 void fsnotify_init_mark(struct fsnotify_mark *mark,
813 memset(mark, 0, sizeof(*mark));
814 spin_lock_init(&mark->lock);
815 refcount_set(&mark->refcnt, 1);
817 mark->group = group;
818 WRITE_ONCE(mark->connector, NULL);
828 struct fsnotify_mark *mark, *next;
838 list_for_each_entry_safe(mark, next, &private_destroy_list, g_list) {
839 list_del_init(&mark->g_list);
840 fsnotify_final_mark_destroy(mark);