Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Directory notifications for Linux. |
| 3 | * |
| 4 | * Copyright (C) 2000,2001,2002 Stephen Rothwell |
| 5 | * |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 6 | * Copyright (C) 2009 Eric Paris <Red Hat Inc> |
| 7 | * dnotify was largly rewritten to use the new fsnotify infrastructure |
| 8 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * This program is free software; you can redistribute it and/or modify it |
| 10 | * under the terms of the GNU General Public License as published by the |
| 11 | * Free Software Foundation; either version 2, or (at your option) any |
| 12 | * later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, but |
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | * General Public License for more details. |
| 18 | */ |
| 19 | #include <linux/fs.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/sched.h> |
| 22 | #include <linux/dnotify.h> |
| 23 | #include <linux/init.h> |
| 24 | #include <linux/spinlock.h> |
| 25 | #include <linux/slab.h> |
Al Viro | 9f3acc3 | 2008-04-24 07:44:08 -0400 | [diff] [blame] | 26 | #include <linux/fdtable.h> |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 27 | #include <linux/fsnotify_backend.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
Eric Dumazet | fa3536c | 2006-03-26 01:37:24 -0800 | [diff] [blame] | 29 | int dir_notify_enable __read_mostly = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 31 | static struct kmem_cache *dnotify_struct_cache __read_mostly; |
Eric Paris | ef5e2b7 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 32 | static struct kmem_cache *dnotify_mark_cache __read_mostly; |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 33 | static struct fsnotify_group *dnotify_group __read_mostly; |
| 34 | static DEFINE_MUTEX(dnotify_mark_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 36 | /* |
Eric Paris | e61ce86 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 37 | * dnotify will attach one of these to each inode (i_fsnotify_marks) which |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 38 | * is being watched by dnotify. If multiple userspace applications are watching |
| 39 | * the same directory with dnotify their information is chained in dn |
| 40 | */ |
Eric Paris | ef5e2b7 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 41 | struct dnotify_mark { |
| 42 | struct fsnotify_mark fsn_mark; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | struct dnotify_struct *dn; |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 44 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 46 | /* |
| 47 | * When a process starts or stops watching an inode the set of events which |
| 48 | * dnotify cares about for that inode may change. This function runs the |
| 49 | * list of everything receiving dnotify events about this directory and calculates |
| 50 | * the set of all those events. After it updates what dnotify is interested in |
| 51 | * it calls the fsnotify function so it can update the set of all events relevant |
| 52 | * to this inode. |
| 53 | */ |
Eric Paris | ef5e2b7 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 54 | static void dnotify_recalc_inode_mask(struct fsnotify_mark *fsn_mark) |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 55 | { |
| 56 | __u32 new_mask, old_mask; |
| 57 | struct dnotify_struct *dn; |
Eric Paris | ef5e2b7 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 58 | struct dnotify_mark *dn_mark = container_of(fsn_mark, |
| 59 | struct dnotify_mark, |
| 60 | fsn_mark); |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 61 | |
Eric Paris | ef5e2b7 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 62 | assert_spin_locked(&fsn_mark->lock); |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 63 | |
Eric Paris | ef5e2b7 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 64 | old_mask = fsn_mark->mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | new_mask = 0; |
Eric Paris | ef5e2b7 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 66 | for (dn = dn_mark->dn; dn != NULL; dn = dn->dn_next) |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 67 | new_mask |= (dn->dn_mask & ~FS_DN_MULTISHOT); |
Eric Paris | 90b1e7a | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 68 | fsnotify_set_mark_mask_locked(fsn_mark, new_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 70 | if (old_mask == new_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | return; |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 72 | |
Eric Paris | ef5e2b7 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 73 | if (fsn_mark->i.inode) |
| 74 | fsnotify_recalc_inode_mask(fsn_mark->i.inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 77 | /* |
| 78 | * Mains fsnotify call where events are delivered to dnotify. |
| 79 | * Find the dnotify mark on the relevant inode, run the list of dnotify structs |
| 80 | * on that mark and determine which of them has expressed interest in receiving |
| 81 | * events of this type. When found send the correct process and signal and |
| 82 | * destroy the dnotify struct if it was not registered to receive multiple |
| 83 | * events. |
| 84 | */ |
| 85 | static int dnotify_handle_event(struct fsnotify_group *group, |
Eric Paris | 3a9b16b | 2010-07-28 10:18:38 -0400 | [diff] [blame] | 86 | struct fsnotify_mark *mark, |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 87 | struct fsnotify_event *event) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | { |
Eric Paris | ef5e2b7 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 89 | struct dnotify_mark *dn_mark; |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 90 | struct inode *to_tell; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | struct dnotify_struct *dn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | struct dnotify_struct **prev; |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 93 | struct fown_struct *fown; |
Andreas Gruenbacher | 9455268 | 2009-10-15 00:13:23 +0200 | [diff] [blame] | 94 | __u32 test_mask = event->mask & ~FS_EVENT_ON_CHILD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 96 | to_tell = event->to_tell; |
| 97 | |
Eric Paris | c496313 | 2010-07-28 10:18:38 -0400 | [diff] [blame] | 98 | dn_mark = container_of(mark, struct dnotify_mark, fsn_mark); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | |
Eric Paris | c496313 | 2010-07-28 10:18:38 -0400 | [diff] [blame] | 100 | spin_lock(&mark->lock); |
Eric Paris | ef5e2b7 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 101 | prev = &dn_mark->dn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | while ((dn = *prev) != NULL) { |
Andreas Gruenbacher | 9455268 | 2009-10-15 00:13:23 +0200 | [diff] [blame] | 103 | if ((dn->dn_mask & test_mask) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | prev = &dn->dn_next; |
| 105 | continue; |
| 106 | } |
| 107 | fown = &dn->dn_filp->f_owner; |
| 108 | send_sigio(fown, dn->dn_fd, POLL_MSG); |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 109 | if (dn->dn_mask & FS_DN_MULTISHOT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | prev = &dn->dn_next; |
| 111 | else { |
| 112 | *prev = dn->dn_next; |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 113 | kmem_cache_free(dnotify_struct_cache, dn); |
Eric Paris | c496313 | 2010-07-28 10:18:38 -0400 | [diff] [blame] | 114 | dnotify_recalc_inode_mask(mark); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | } |
| 116 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
Eric Paris | c496313 | 2010-07-28 10:18:38 -0400 | [diff] [blame] | 118 | spin_unlock(&mark->lock); |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 119 | |
| 120 | return 0; |
| 121 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | |
| 123 | /* |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 124 | * Given an inode and mask determine if dnotify would be interested in sending |
| 125 | * userspace notification for that pair. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | */ |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 127 | static bool dnotify_should_send_event(struct fsnotify_group *group, |
Eric Paris | 3a9fb89 | 2009-12-17 21:24:23 -0500 | [diff] [blame] | 128 | struct inode *inode, struct vfsmount *mnt, |
Eric Paris | 3a9b16b | 2010-07-28 10:18:38 -0400 | [diff] [blame] | 129 | struct fsnotify_mark *mark, __u32 mask, |
| 130 | void *data, int data_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | { |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 132 | /* not a dir, dnotify doesn't care */ |
| 133 | if (!S_ISDIR(inode->i_mode)) |
| 134 | return false; |
| 135 | |
Eric Paris | 2612abb | 2010-07-28 10:18:39 -0400 | [diff] [blame^] | 136 | return true; |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 137 | } |
| 138 | |
Eric Paris | ef5e2b7 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 139 | static void dnotify_free_mark(struct fsnotify_mark *fsn_mark) |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 140 | { |
Eric Paris | ef5e2b7 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 141 | struct dnotify_mark *dn_mark = container_of(fsn_mark, |
| 142 | struct dnotify_mark, |
| 143 | fsn_mark); |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 144 | |
Eric Paris | ef5e2b7 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 145 | BUG_ON(dn_mark->dn); |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 146 | |
Eric Paris | ef5e2b7 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 147 | kmem_cache_free(dnotify_mark_cache, dn_mark); |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | static struct fsnotify_ops dnotify_fsnotify_ops = { |
| 151 | .handle_event = dnotify_handle_event, |
| 152 | .should_send_event = dnotify_should_send_event, |
| 153 | .free_group_priv = NULL, |
Eric Paris | a092ee2 | 2009-06-11 11:09:48 -0400 | [diff] [blame] | 154 | .freeing_mark = NULL, |
Eric Paris | e4aff11 | 2009-05-21 17:01:50 -0400 | [diff] [blame] | 155 | .free_event_priv = NULL, |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 156 | }; |
| 157 | |
| 158 | /* |
| 159 | * Called every time a file is closed. Looks first for a dnotify mark on the |
Eric Paris | e61ce86 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 160 | * inode. If one is found run all of the ->dn structures attached to that |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 161 | * mark for one relevant to this process closing the file and remove that |
| 162 | * dnotify_struct. If that was the last dnotify_struct also remove the |
Eric Paris | e61ce86 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 163 | * fsnotify_mark. |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 164 | */ |
| 165 | void dnotify_flush(struct file *filp, fl_owner_t id) |
| 166 | { |
Eric Paris | ef5e2b7 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 167 | struct fsnotify_mark *fsn_mark; |
| 168 | struct dnotify_mark *dn_mark; |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 169 | struct dnotify_struct *dn; |
| 170 | struct dnotify_struct **prev; |
| 171 | struct inode *inode; |
| 172 | |
| 173 | inode = filp->f_path.dentry->d_inode; |
| 174 | if (!S_ISDIR(inode->i_mode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | return; |
| 176 | |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 177 | fsn_mark = fsnotify_find_inode_mark(dnotify_group, inode); |
Eric Paris | ef5e2b7 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 178 | if (!fsn_mark) |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 179 | return; |
Eric Paris | ef5e2b7 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 180 | dn_mark = container_of(fsn_mark, struct dnotify_mark, fsn_mark); |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 181 | |
| 182 | mutex_lock(&dnotify_mark_mutex); |
| 183 | |
Eric Paris | ef5e2b7 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 184 | spin_lock(&fsn_mark->lock); |
| 185 | prev = &dn_mark->dn; |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 186 | while ((dn = *prev) != NULL) { |
| 187 | if ((dn->dn_owner == id) && (dn->dn_filp == filp)) { |
| 188 | *prev = dn->dn_next; |
| 189 | kmem_cache_free(dnotify_struct_cache, dn); |
Eric Paris | ef5e2b7 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 190 | dnotify_recalc_inode_mask(fsn_mark); |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 191 | break; |
| 192 | } |
| 193 | prev = &dn->dn_next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | } |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 195 | |
Eric Paris | ef5e2b7 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 196 | spin_unlock(&fsn_mark->lock); |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 197 | |
| 198 | /* nothing else could have found us thanks to the dnotify_mark_mutex */ |
Eric Paris | ef5e2b7 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 199 | if (dn_mark->dn == NULL) |
| 200 | fsnotify_destroy_mark(fsn_mark); |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 201 | |
| 202 | fsnotify_recalc_group_mask(dnotify_group); |
| 203 | |
| 204 | mutex_unlock(&dnotify_mark_mutex); |
| 205 | |
Eric Paris | ef5e2b7 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 206 | fsnotify_put_mark(fsn_mark); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | } |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 208 | |
| 209 | /* this conversion is done only at watch creation */ |
| 210 | static __u32 convert_arg(unsigned long arg) |
| 211 | { |
| 212 | __u32 new_mask = FS_EVENT_ON_CHILD; |
| 213 | |
| 214 | if (arg & DN_MULTISHOT) |
| 215 | new_mask |= FS_DN_MULTISHOT; |
| 216 | if (arg & DN_DELETE) |
| 217 | new_mask |= (FS_DELETE | FS_MOVED_FROM); |
| 218 | if (arg & DN_MODIFY) |
| 219 | new_mask |= FS_MODIFY; |
| 220 | if (arg & DN_ACCESS) |
| 221 | new_mask |= FS_ACCESS; |
| 222 | if (arg & DN_ATTRIB) |
| 223 | new_mask |= FS_ATTRIB; |
| 224 | if (arg & DN_RENAME) |
| 225 | new_mask |= FS_DN_RENAME; |
| 226 | if (arg & DN_CREATE) |
| 227 | new_mask |= (FS_CREATE | FS_MOVED_TO); |
| 228 | |
| 229 | return new_mask; |
| 230 | } |
| 231 | |
| 232 | /* |
| 233 | * If multiple processes watch the same inode with dnotify there is only one |
Eric Paris | e61ce86 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 234 | * dnotify mark in inode->i_fsnotify_marks but we chain a dnotify_struct |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 235 | * onto that mark. This function either attaches the new dnotify_struct onto |
| 236 | * that list, or it |= the mask onto an existing dnofiy_struct. |
| 237 | */ |
Eric Paris | ef5e2b7 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 238 | static int attach_dn(struct dnotify_struct *dn, struct dnotify_mark *dn_mark, |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 239 | fl_owner_t id, int fd, struct file *filp, __u32 mask) |
| 240 | { |
| 241 | struct dnotify_struct *odn; |
| 242 | |
Eric Paris | ef5e2b7 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 243 | odn = dn_mark->dn; |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 244 | while (odn != NULL) { |
| 245 | /* adding more events to existing dnofiy_struct? */ |
| 246 | if ((odn->dn_owner == id) && (odn->dn_filp == filp)) { |
| 247 | odn->dn_fd = fd; |
| 248 | odn->dn_mask |= mask; |
| 249 | return -EEXIST; |
| 250 | } |
| 251 | odn = odn->dn_next; |
| 252 | } |
| 253 | |
| 254 | dn->dn_mask = mask; |
| 255 | dn->dn_fd = fd; |
| 256 | dn->dn_filp = filp; |
| 257 | dn->dn_owner = id; |
Eric Paris | ef5e2b7 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 258 | dn->dn_next = dn_mark->dn; |
| 259 | dn_mark->dn = dn; |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 260 | |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | /* |
| 265 | * When a process calls fcntl to attach a dnotify watch to a directory it ends |
| 266 | * up here. Allocate both a mark for fsnotify to add and a dnotify_struct to be |
| 267 | * attached to the fsnotify_mark. |
| 268 | */ |
| 269 | int fcntl_dirnotify(int fd, struct file *filp, unsigned long arg) |
| 270 | { |
Eric Paris | ef5e2b7 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 271 | struct dnotify_mark *new_dn_mark, *dn_mark; |
| 272 | struct fsnotify_mark *new_fsn_mark, *fsn_mark; |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 273 | struct dnotify_struct *dn; |
| 274 | struct inode *inode; |
| 275 | fl_owner_t id = current->files; |
| 276 | struct file *f; |
| 277 | int destroy = 0, error = 0; |
| 278 | __u32 mask; |
| 279 | |
| 280 | /* we use these to tell if we need to kfree */ |
Eric Paris | ef5e2b7 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 281 | new_fsn_mark = NULL; |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 282 | dn = NULL; |
| 283 | |
| 284 | if (!dir_notify_enable) { |
| 285 | error = -EINVAL; |
| 286 | goto out_err; |
| 287 | } |
| 288 | |
| 289 | /* a 0 mask means we are explicitly removing the watch */ |
| 290 | if ((arg & ~DN_MULTISHOT) == 0) { |
| 291 | dnotify_flush(filp, id); |
| 292 | error = 0; |
| 293 | goto out_err; |
| 294 | } |
| 295 | |
| 296 | /* dnotify only works on directories */ |
| 297 | inode = filp->f_path.dentry->d_inode; |
| 298 | if (!S_ISDIR(inode->i_mode)) { |
| 299 | error = -ENOTDIR; |
| 300 | goto out_err; |
| 301 | } |
| 302 | |
| 303 | /* expect most fcntl to add new rather than augment old */ |
| 304 | dn = kmem_cache_alloc(dnotify_struct_cache, GFP_KERNEL); |
| 305 | if (!dn) { |
| 306 | error = -ENOMEM; |
| 307 | goto out_err; |
| 308 | } |
| 309 | |
| 310 | /* new fsnotify mark, we expect most fcntl calls to add a new mark */ |
Eric Paris | ef5e2b7 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 311 | new_dn_mark = kmem_cache_alloc(dnotify_mark_cache, GFP_KERNEL); |
| 312 | if (!new_dn_mark) { |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 313 | error = -ENOMEM; |
| 314 | goto out_err; |
| 315 | } |
| 316 | |
| 317 | /* convert the userspace DN_* "arg" to the internal FS_* defines in fsnotify */ |
| 318 | mask = convert_arg(arg); |
| 319 | |
Eric Paris | ef5e2b7 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 320 | /* set up the new_fsn_mark and new_dn_mark */ |
| 321 | new_fsn_mark = &new_dn_mark->fsn_mark; |
| 322 | fsnotify_init_mark(new_fsn_mark, dnotify_free_mark); |
| 323 | new_fsn_mark->mask = mask; |
| 324 | new_dn_mark->dn = NULL; |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 325 | |
| 326 | /* this is needed to prevent the fcntl/close race described below */ |
| 327 | mutex_lock(&dnotify_mark_mutex); |
| 328 | |
Eric Paris | ef5e2b7 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 329 | /* add the new_fsn_mark or find an old one. */ |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 330 | fsn_mark = fsnotify_find_inode_mark(dnotify_group, inode); |
Eric Paris | ef5e2b7 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 331 | if (fsn_mark) { |
| 332 | dn_mark = container_of(fsn_mark, struct dnotify_mark, fsn_mark); |
| 333 | spin_lock(&fsn_mark->lock); |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 334 | } else { |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 335 | fsnotify_add_mark(new_fsn_mark, dnotify_group, inode, NULL, 0); |
Eric Paris | ef5e2b7 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 336 | spin_lock(&new_fsn_mark->lock); |
| 337 | fsn_mark = new_fsn_mark; |
| 338 | dn_mark = new_dn_mark; |
| 339 | /* we used new_fsn_mark, so don't free it */ |
| 340 | new_fsn_mark = NULL; |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | rcu_read_lock(); |
| 344 | f = fcheck(fd); |
| 345 | rcu_read_unlock(); |
| 346 | |
| 347 | /* if (f != filp) means that we lost a race and another task/thread |
| 348 | * actually closed the fd we are still playing with before we grabbed |
Eric Paris | ef5e2b7 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 349 | * the dnotify_mark_mutex and fsn_mark->lock. Since closing the fd is the |
Eric Paris | e61ce86 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 350 | * only time we clean up the marks we need to get our mark off |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 351 | * the list. */ |
| 352 | if (f != filp) { |
| 353 | /* if we added ourselves, shoot ourselves, it's possible that |
Eric Paris | ef5e2b7 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 354 | * the flush actually did shoot this fsn_mark. That's fine too |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 355 | * since multiple calls to destroy_mark is perfectly safe, if |
Eric Paris | ef5e2b7 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 356 | * we found a dn_mark already attached to the inode, just sod |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 357 | * off silently as the flush at close time dealt with it. |
| 358 | */ |
Eric Paris | ef5e2b7 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 359 | if (dn_mark == new_dn_mark) |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 360 | destroy = 1; |
| 361 | goto out; |
| 362 | } |
| 363 | |
| 364 | error = __f_setown(filp, task_pid(current), PIDTYPE_PID, 0); |
| 365 | if (error) { |
| 366 | /* if we added, we must shoot */ |
Eric Paris | ef5e2b7 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 367 | if (dn_mark == new_dn_mark) |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 368 | destroy = 1; |
| 369 | goto out; |
| 370 | } |
| 371 | |
Eric Paris | ef5e2b7 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 372 | error = attach_dn(dn, dn_mark, id, fd, filp, mask); |
| 373 | /* !error means that we attached the dn to the dn_mark, so don't free it */ |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 374 | if (!error) |
| 375 | dn = NULL; |
| 376 | /* -EEXIST means that we didn't add this new dn and used an old one. |
| 377 | * that isn't an error (and the unused dn should be freed) */ |
| 378 | else if (error == -EEXIST) |
| 379 | error = 0; |
| 380 | |
Eric Paris | ef5e2b7 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 381 | dnotify_recalc_inode_mask(fsn_mark); |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 382 | out: |
Eric Paris | ef5e2b7 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 383 | spin_unlock(&fsn_mark->lock); |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 384 | |
| 385 | if (destroy) |
Eric Paris | ef5e2b7 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 386 | fsnotify_destroy_mark(fsn_mark); |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 387 | |
| 388 | fsnotify_recalc_group_mask(dnotify_group); |
| 389 | |
| 390 | mutex_unlock(&dnotify_mark_mutex); |
Eric Paris | ef5e2b7 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 391 | fsnotify_put_mark(fsn_mark); |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 392 | out_err: |
Eric Paris | ef5e2b7 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 393 | if (new_fsn_mark) |
| 394 | fsnotify_put_mark(new_fsn_mark); |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 395 | if (dn) |
| 396 | kmem_cache_free(dnotify_struct_cache, dn); |
| 397 | return error; |
| 398 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | |
| 400 | static int __init dnotify_init(void) |
| 401 | { |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 402 | dnotify_struct_cache = KMEM_CACHE(dnotify_struct, SLAB_PANIC); |
Eric Paris | ef5e2b7 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 403 | dnotify_mark_cache = KMEM_CACHE(dnotify_mark, SLAB_PANIC); |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 404 | |
Eric Paris | 0d2e2a1 | 2009-12-17 21:24:22 -0500 | [diff] [blame] | 405 | dnotify_group = fsnotify_alloc_group(&dnotify_fsnotify_ops); |
Eric Paris | 3c5119c | 2009-05-21 17:01:33 -0400 | [diff] [blame] | 406 | if (IS_ERR(dnotify_group)) |
| 407 | panic("unable to allocate fsnotify group for dnotify\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | return 0; |
| 409 | } |
| 410 | |
| 411 | module_init(dnotify_init) |