Eric Paris | 3be25f4 | 2009-05-21 17:01:26 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 Red Hat, Inc., Eric Paris <eparis@redhat.com> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2, or (at your option) |
| 7 | * any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; see the file COPYING. If not, write to |
| 16 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
| 17 | */ |
| 18 | |
Eric Paris | 3be25f4 | 2009-05-21 17:01:26 -0400 | [diff] [blame] | 19 | #include <linux/fs.h> |
| 20 | #include <linux/init.h> |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/mutex.h> |
Eric Paris | 3be25f4 | 2009-05-21 17:01:26 -0400 | [diff] [blame] | 24 | #include <linux/spinlock.h> |
| 25 | |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 26 | #include <linux/atomic.h> |
Eric Paris | 3be25f4 | 2009-05-21 17:01:26 -0400 | [diff] [blame] | 27 | |
| 28 | #include <linux/fsnotify_backend.h> |
| 29 | #include "fsnotify.h" |
| 30 | |
Dave Chinner | 55fa609 | 2011-03-22 22:23:40 +1100 | [diff] [blame] | 31 | #include "../internal.h" |
| 32 | |
Eric Paris | 3be25f4 | 2009-05-21 17:01:26 -0400 | [diff] [blame] | 33 | /* |
Eric Paris | 3be25f4 | 2009-05-21 17:01:26 -0400 | [diff] [blame] | 34 | * Recalculate the inode->i_fsnotify_mask, or the mask of all FS_* event types |
| 35 | * any notifier is interested in hearing for this inode. |
| 36 | */ |
| 37 | void fsnotify_recalc_inode_mask(struct inode *inode) |
| 38 | { |
| 39 | spin_lock(&inode->i_lock); |
Jan Kara | 0809ab6 | 2014-12-12 16:58:36 -0800 | [diff] [blame] | 40 | inode->i_fsnotify_mask = fsnotify_recalc_mask(&inode->i_fsnotify_marks); |
Eric Paris | 3be25f4 | 2009-05-21 17:01:26 -0400 | [diff] [blame] | 41 | spin_unlock(&inode->i_lock); |
Eric Paris | c28f7e5 | 2009-05-21 17:01:29 -0400 | [diff] [blame] | 42 | |
| 43 | __fsnotify_update_child_dentry_flags(inode); |
Eric Paris | 3be25f4 | 2009-05-21 17:01:26 -0400 | [diff] [blame] | 44 | } |
| 45 | |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 46 | void fsnotify_destroy_inode_mark(struct fsnotify_mark *mark) |
Eric Paris | 3be25f4 | 2009-05-21 17:01:26 -0400 | [diff] [blame] | 47 | { |
Jan Kara | 0809ab6 | 2014-12-12 16:58:36 -0800 | [diff] [blame] | 48 | struct inode *inode = mark->inode; |
Eric Paris | 3be25f4 | 2009-05-21 17:01:26 -0400 | [diff] [blame] | 49 | |
Lino Sanfilippo | 986ab09 | 2011-06-14 17:29:50 +0200 | [diff] [blame] | 50 | BUG_ON(!mutex_is_locked(&mark->group->mark_mutex)); |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 51 | assert_spin_locked(&mark->lock); |
Eric Paris | 3be25f4 | 2009-05-21 17:01:26 -0400 | [diff] [blame] | 52 | |
Eric Paris | 3be25f4 | 2009-05-21 17:01:26 -0400 | [diff] [blame] | 53 | spin_lock(&inode->i_lock); |
| 54 | |
Jan Kara | 0809ab6 | 2014-12-12 16:58:36 -0800 | [diff] [blame] | 55 | hlist_del_init_rcu(&mark->obj_list); |
| 56 | mark->inode = NULL; |
Eric Paris | 3be25f4 | 2009-05-21 17:01:26 -0400 | [diff] [blame] | 57 | |
| 58 | /* |
Eric Paris | e61ce86 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 59 | * this mark is now off the inode->i_fsnotify_marks list and we |
Eric Paris | 3be25f4 | 2009-05-21 17:01:26 -0400 | [diff] [blame] | 60 | * hold the inode->i_lock, so this is the perfect time to update the |
| 61 | * inode->i_fsnotify_mask |
| 62 | */ |
Jan Kara | 0809ab6 | 2014-12-12 16:58:36 -0800 | [diff] [blame] | 63 | inode->i_fsnotify_mask = fsnotify_recalc_mask(&inode->i_fsnotify_marks); |
Eric Paris | 3be25f4 | 2009-05-21 17:01:26 -0400 | [diff] [blame] | 64 | spin_unlock(&inode->i_lock); |
Eric Paris | 3be25f4 | 2009-05-21 17:01:26 -0400 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | /* |
| 68 | * Given an inode, destroy all of the marks associated with that inode. |
| 69 | */ |
| 70 | void fsnotify_clear_marks_by_inode(struct inode *inode) |
| 71 | { |
Jan Kara | 0809ab6 | 2014-12-12 16:58:36 -0800 | [diff] [blame] | 72 | struct fsnotify_mark *mark; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 73 | struct hlist_node *n; |
Eric Paris | 3be25f4 | 2009-05-21 17:01:26 -0400 | [diff] [blame] | 74 | LIST_HEAD(free_list); |
| 75 | |
| 76 | spin_lock(&inode->i_lock); |
Jan Kara | 0809ab6 | 2014-12-12 16:58:36 -0800 | [diff] [blame] | 77 | hlist_for_each_entry_safe(mark, n, &inode->i_fsnotify_marks, obj_list) { |
| 78 | list_add(&mark->free_list, &free_list); |
| 79 | hlist_del_init_rcu(&mark->obj_list); |
Eric Paris | 841bdc1 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 80 | fsnotify_get_mark(mark); |
Eric Paris | 3be25f4 | 2009-05-21 17:01:26 -0400 | [diff] [blame] | 81 | } |
| 82 | spin_unlock(&inode->i_lock); |
| 83 | |
Jan Kara | 0809ab6 | 2014-12-12 16:58:36 -0800 | [diff] [blame] | 84 | fsnotify_destroy_marks(&free_list); |
Eric Paris | 3be25f4 | 2009-05-21 17:01:26 -0400 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /* |
Eric Paris | 4d92604 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 88 | * Given a group clear all of the inode marks associated with that group. |
| 89 | */ |
| 90 | void fsnotify_clear_inode_marks_by_group(struct fsnotify_group *group) |
| 91 | { |
| 92 | fsnotify_clear_marks_by_group_flags(group, FSNOTIFY_MARK_FLAG_INODE); |
| 93 | } |
| 94 | |
| 95 | /* |
Eric Paris | 3be25f4 | 2009-05-21 17:01:26 -0400 | [diff] [blame] | 96 | * given a group and inode, find the mark associated with that combination. |
| 97 | * if found take a reference to that mark and return it, else return NULL |
| 98 | */ |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 99 | struct fsnotify_mark *fsnotify_find_inode_mark(struct fsnotify_group *group, |
| 100 | struct inode *inode) |
Eric Paris | 3be25f4 | 2009-05-21 17:01:26 -0400 | [diff] [blame] | 101 | { |
Andreas Gruenbacher | 3556608 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 102 | struct fsnotify_mark *mark; |
| 103 | |
| 104 | spin_lock(&inode->i_lock); |
Jan Kara | 0809ab6 | 2014-12-12 16:58:36 -0800 | [diff] [blame] | 105 | mark = fsnotify_find_mark(&inode->i_fsnotify_marks, group); |
Andreas Gruenbacher | 3556608 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 106 | spin_unlock(&inode->i_lock); |
| 107 | |
| 108 | return mark; |
Eric Paris | 3be25f4 | 2009-05-21 17:01:26 -0400 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /* |
Eric Paris | 90b1e7a | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 112 | * If we are setting a mark mask on an inode mark we should pin the inode |
| 113 | * in memory. |
Eric Paris | 3be25f4 | 2009-05-21 17:01:26 -0400 | [diff] [blame] | 114 | */ |
Eric Paris | 90b1e7a | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 115 | void fsnotify_set_inode_mark_mask_locked(struct fsnotify_mark *mark, |
| 116 | __u32 mask) |
Eric Paris | 3be25f4 | 2009-05-21 17:01:26 -0400 | [diff] [blame] | 117 | { |
Eric Paris | 90b1e7a | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 118 | struct inode *inode; |
| 119 | |
| 120 | assert_spin_locked(&mark->lock); |
| 121 | |
| 122 | if (mask && |
Jan Kara | 0809ab6 | 2014-12-12 16:58:36 -0800 | [diff] [blame] | 123 | mark->inode && |
Eric Paris | 90b1e7a | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 124 | !(mark->flags & FSNOTIFY_MARK_FLAG_OBJECT_PINNED)) { |
| 125 | mark->flags |= FSNOTIFY_MARK_FLAG_OBJECT_PINNED; |
Jan Kara | 0809ab6 | 2014-12-12 16:58:36 -0800 | [diff] [blame] | 126 | inode = igrab(mark->inode); |
Eric Paris | 90b1e7a | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 127 | /* |
| 128 | * we shouldn't be able to get here if the inode wasn't |
| 129 | * already safely held in memory. But bug in case it |
| 130 | * ever is wrong. |
| 131 | */ |
| 132 | BUG_ON(!inode); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /* |
Eric Paris | 0c6532e | 2010-07-28 10:18:38 -0400 | [diff] [blame] | 137 | * Attach an initialized mark to a given inode. |
Eric Paris | 3be25f4 | 2009-05-21 17:01:26 -0400 | [diff] [blame] | 138 | * These marks may be used for the fsnotify backend to determine which |
Eric Paris | 0c6532e | 2010-07-28 10:18:38 -0400 | [diff] [blame] | 139 | * event types should be delivered to which group and for which inodes. These |
Eric Paris | 6ad2d4e | 2010-10-28 17:21:56 -0400 | [diff] [blame] | 140 | * marks are ordered according to priority, highest number first, and then by |
| 141 | * the group's location in memory. |
Eric Paris | 3be25f4 | 2009-05-21 17:01:26 -0400 | [diff] [blame] | 142 | */ |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 143 | int fsnotify_add_inode_mark(struct fsnotify_mark *mark, |
| 144 | struct fsnotify_group *group, struct inode *inode, |
| 145 | int allow_dups) |
Eric Paris | 3be25f4 | 2009-05-21 17:01:26 -0400 | [diff] [blame] | 146 | { |
Jan Kara | 0809ab6 | 2014-12-12 16:58:36 -0800 | [diff] [blame] | 147 | int ret; |
Eric Paris | 3be25f4 | 2009-05-21 17:01:26 -0400 | [diff] [blame] | 148 | |
Eric Paris | 700307a | 2010-07-28 10:18:38 -0400 | [diff] [blame] | 149 | mark->flags |= FSNOTIFY_MARK_FLAG_INODE; |
Eric Paris | 1ef5f13 | 2009-05-21 17:01:54 -0400 | [diff] [blame] | 150 | |
Lino Sanfilippo | 986ab09 | 2011-06-14 17:29:50 +0200 | [diff] [blame] | 151 | BUG_ON(!mutex_is_locked(&group->mark_mutex)); |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 152 | assert_spin_locked(&mark->lock); |
Eric Paris | 7131485 | 2009-12-17 21:24:23 -0500 | [diff] [blame] | 153 | |
Eric Paris | 3be25f4 | 2009-05-21 17:01:26 -0400 | [diff] [blame] | 154 | spin_lock(&inode->i_lock); |
Jan Kara | 0809ab6 | 2014-12-12 16:58:36 -0800 | [diff] [blame] | 155 | mark->inode = inode; |
| 156 | ret = fsnotify_add_mark_list(&inode->i_fsnotify_marks, mark, |
| 157 | allow_dups); |
| 158 | inode->i_fsnotify_mask = fsnotify_recalc_mask(&inode->i_fsnotify_marks); |
Eric Paris | 3be25f4 | 2009-05-21 17:01:26 -0400 | [diff] [blame] | 159 | spin_unlock(&inode->i_lock); |
Eric Paris | 3be25f4 | 2009-05-21 17:01:26 -0400 | [diff] [blame] | 160 | |
| 161 | return ret; |
| 162 | } |
Eric Paris | 164bc61 | 2009-05-21 17:01:58 -0400 | [diff] [blame] | 163 | |
| 164 | /** |
| 165 | * fsnotify_unmount_inodes - an sb is unmounting. handle any watched inodes. |
| 166 | * @list: list of inodes being unmounted (sb->s_inodes) |
| 167 | * |
Dave Chinner | 55fa609 | 2011-03-22 22:23:40 +1100 | [diff] [blame] | 168 | * Called during unmount with no locks held, so needs to be safe against |
| 169 | * concurrent modifiers. We temporarily drop inode_sb_list_lock and CAN block. |
Eric Paris | 164bc61 | 2009-05-21 17:01:58 -0400 | [diff] [blame] | 170 | */ |
| 171 | void fsnotify_unmount_inodes(struct list_head *list) |
| 172 | { |
| 173 | struct inode *inode, *next_i, *need_iput = NULL; |
| 174 | |
Dave Chinner | 55fa609 | 2011-03-22 22:23:40 +1100 | [diff] [blame] | 175 | spin_lock(&inode_sb_list_lock); |
Eric Paris | 164bc61 | 2009-05-21 17:01:58 -0400 | [diff] [blame] | 176 | list_for_each_entry_safe(inode, next_i, list, i_sb_list) { |
| 177 | struct inode *need_iput_tmp; |
| 178 | |
| 179 | /* |
Al Viro | a4ffdde | 2010-06-02 17:38:30 -0400 | [diff] [blame] | 180 | * We cannot __iget() an inode in state I_FREEING, |
Eric Paris | 164bc61 | 2009-05-21 17:01:58 -0400 | [diff] [blame] | 181 | * I_WILL_FREE, or I_NEW which is fine because by that point |
| 182 | * the inode cannot have any associated watches. |
| 183 | */ |
Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 184 | spin_lock(&inode->i_lock); |
| 185 | if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) { |
| 186 | spin_unlock(&inode->i_lock); |
Eric Paris | 164bc61 | 2009-05-21 17:01:58 -0400 | [diff] [blame] | 187 | continue; |
Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 188 | } |
Eric Paris | 164bc61 | 2009-05-21 17:01:58 -0400 | [diff] [blame] | 189 | |
| 190 | /* |
| 191 | * If i_count is zero, the inode cannot have any watches and |
| 192 | * doing an __iget/iput with MS_ACTIVE clear would actually |
| 193 | * evict all inodes with zero i_count from icache which is |
| 194 | * unnecessarily violent and may in fact be illegal to do. |
| 195 | */ |
Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 196 | if (!atomic_read(&inode->i_count)) { |
| 197 | spin_unlock(&inode->i_lock); |
Eric Paris | 164bc61 | 2009-05-21 17:01:58 -0400 | [diff] [blame] | 198 | continue; |
Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 199 | } |
Eric Paris | 164bc61 | 2009-05-21 17:01:58 -0400 | [diff] [blame] | 200 | |
| 201 | need_iput_tmp = need_iput; |
| 202 | need_iput = NULL; |
| 203 | |
| 204 | /* In case fsnotify_inode_delete() drops a reference. */ |
| 205 | if (inode != need_iput_tmp) |
| 206 | __iget(inode); |
| 207 | else |
| 208 | need_iput_tmp = NULL; |
Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 209 | spin_unlock(&inode->i_lock); |
Eric Paris | 164bc61 | 2009-05-21 17:01:58 -0400 | [diff] [blame] | 210 | |
| 211 | /* In case the dropping of a reference would nuke next_i. */ |
Jerry Hoemann | 6424bab | 2014-10-29 14:50:22 -0700 | [diff] [blame] | 212 | while (&next_i->i_sb_list != list) { |
Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 213 | spin_lock(&next_i->i_lock); |
Jerry Hoemann | 6424bab | 2014-10-29 14:50:22 -0700 | [diff] [blame] | 214 | if (!(next_i->i_state & (I_FREEING | I_WILL_FREE)) && |
| 215 | atomic_read(&next_i->i_count)) { |
Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 216 | __iget(next_i); |
| 217 | need_iput = next_i; |
Jerry Hoemann | 6424bab | 2014-10-29 14:50:22 -0700 | [diff] [blame] | 218 | spin_unlock(&next_i->i_lock); |
| 219 | break; |
Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 220 | } |
| 221 | spin_unlock(&next_i->i_lock); |
Jerry Hoemann | 6424bab | 2014-10-29 14:50:22 -0700 | [diff] [blame] | 222 | next_i = list_entry(next_i->i_sb_list.next, |
| 223 | struct inode, i_sb_list); |
Eric Paris | 164bc61 | 2009-05-21 17:01:58 -0400 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | /* |
Jerry Hoemann | 6424bab | 2014-10-29 14:50:22 -0700 | [diff] [blame] | 227 | * We can safely drop inode_sb_list_lock here because either |
| 228 | * we actually hold references on both inode and next_i or |
| 229 | * end of list. Also no new inodes will be added since the |
| 230 | * umount has begun. |
Eric Paris | 164bc61 | 2009-05-21 17:01:58 -0400 | [diff] [blame] | 231 | */ |
Dave Chinner | 55fa609 | 2011-03-22 22:23:40 +1100 | [diff] [blame] | 232 | spin_unlock(&inode_sb_list_lock); |
Eric Paris | 164bc61 | 2009-05-21 17:01:58 -0400 | [diff] [blame] | 233 | |
| 234 | if (need_iput_tmp) |
| 235 | iput(need_iput_tmp); |
| 236 | |
| 237 | /* for each watch, send FS_UNMOUNT and then remove it */ |
| 238 | fsnotify(inode, FS_UNMOUNT, inode, FSNOTIFY_EVENT_INODE, NULL, 0); |
| 239 | |
| 240 | fsnotify_inode_delete(inode); |
| 241 | |
| 242 | iput(inode); |
| 243 | |
Dave Chinner | 55fa609 | 2011-03-22 22:23:40 +1100 | [diff] [blame] | 244 | spin_lock(&inode_sb_list_lock); |
Eric Paris | 164bc61 | 2009-05-21 17:01:58 -0400 | [diff] [blame] | 245 | } |
Dave Chinner | 55fa609 | 2011-03-22 22:23:40 +1100 | [diff] [blame] | 246 | spin_unlock(&inode_sb_list_lock); |
Eric Paris | 164bc61 | 2009-05-21 17:01:58 -0400 | [diff] [blame] | 247 | } |