cpufreq/powernow-k8: workqueue user shouldn't migrate the kworker to another CPU
[firefly-linux-kernel-4.4.55.git] / kernel / audit_tree.c
1 #include "audit.h"
2 #include <linux/fsnotify_backend.h>
3 #include <linux/namei.h>
4 #include <linux/mount.h>
5 #include <linux/kthread.h>
6 #include <linux/slab.h>
7
8 struct audit_tree;
9 struct audit_chunk;
10
11 struct audit_tree {
12         atomic_t count;
13         int goner;
14         struct audit_chunk *root;
15         struct list_head chunks;
16         struct list_head rules;
17         struct list_head list;
18         struct list_head same_root;
19         struct rcu_head head;
20         char pathname[];
21 };
22
23 struct audit_chunk {
24         struct list_head hash;
25         struct fsnotify_mark mark;
26         struct list_head trees;         /* with root here */
27         int dead;
28         int count;
29         atomic_long_t refs;
30         struct rcu_head head;
31         struct node {
32                 struct list_head list;
33                 struct audit_tree *owner;
34                 unsigned index;         /* index; upper bit indicates 'will prune' */
35         } owners[];
36 };
37
38 static LIST_HEAD(tree_list);
39 static LIST_HEAD(prune_list);
40
41 /*
42  * One struct chunk is attached to each inode of interest.
43  * We replace struct chunk on tagging/untagging.
44  * Rules have pointer to struct audit_tree.
45  * Rules have struct list_head rlist forming a list of rules over
46  * the same tree.
47  * References to struct chunk are collected at audit_inode{,_child}()
48  * time and used in AUDIT_TREE rule matching.
49  * These references are dropped at the same time we are calling
50  * audit_free_names(), etc.
51  *
52  * Cyclic lists galore:
53  * tree.chunks anchors chunk.owners[].list                      hash_lock
54  * tree.rules anchors rule.rlist                                audit_filter_mutex
55  * chunk.trees anchors tree.same_root                           hash_lock
56  * chunk.hash is a hash with middle bits of watch.inode as
57  * a hash function.                                             RCU, hash_lock
58  *
59  * tree is refcounted; one reference for "some rules on rules_list refer to
60  * it", one for each chunk with pointer to it.
61  *
62  * chunk is refcounted by embedded fsnotify_mark + .refs (non-zero refcount
63  * of watch contributes 1 to .refs).
64  *
65  * node.index allows to get from node.list to containing chunk.
66  * MSB of that sucker is stolen to mark taggings that we might have to
67  * revert - several operations have very unpleasant cleanup logics and
68  * that makes a difference.  Some.
69  */
70
71 static struct fsnotify_group *audit_tree_group;
72
73 static struct audit_tree *alloc_tree(const char *s)
74 {
75         struct audit_tree *tree;
76
77         tree = kmalloc(sizeof(struct audit_tree) + strlen(s) + 1, GFP_KERNEL);
78         if (tree) {
79                 atomic_set(&tree->count, 1);
80                 tree->goner = 0;
81                 INIT_LIST_HEAD(&tree->chunks);
82                 INIT_LIST_HEAD(&tree->rules);
83                 INIT_LIST_HEAD(&tree->list);
84                 INIT_LIST_HEAD(&tree->same_root);
85                 tree->root = NULL;
86                 strcpy(tree->pathname, s);
87         }
88         return tree;
89 }
90
91 static inline void get_tree(struct audit_tree *tree)
92 {
93         atomic_inc(&tree->count);
94 }
95
96 static void __put_tree(struct rcu_head *rcu)
97 {
98         struct audit_tree *tree = container_of(rcu, struct audit_tree, head);
99         kfree(tree);
100 }
101
102 static inline void put_tree(struct audit_tree *tree)
103 {
104         if (atomic_dec_and_test(&tree->count))
105                 call_rcu(&tree->head, __put_tree);
106 }
107
108 /* to avoid bringing the entire thing in audit.h */
109 const char *audit_tree_path(struct audit_tree *tree)
110 {
111         return tree->pathname;
112 }
113
114 static void free_chunk(struct audit_chunk *chunk)
115 {
116         int i;
117
118         for (i = 0; i < chunk->count; i++) {
119                 if (chunk->owners[i].owner)
120                         put_tree(chunk->owners[i].owner);
121         }
122         kfree(chunk);
123 }
124
125 void audit_put_chunk(struct audit_chunk *chunk)
126 {
127         if (atomic_long_dec_and_test(&chunk->refs))
128                 free_chunk(chunk);
129 }
130
131 static void __put_chunk(struct rcu_head *rcu)
132 {
133         struct audit_chunk *chunk = container_of(rcu, struct audit_chunk, head);
134         audit_put_chunk(chunk);
135 }
136
137 static void audit_tree_destroy_watch(struct fsnotify_mark *entry)
138 {
139         struct audit_chunk *chunk = container_of(entry, struct audit_chunk, mark);
140         call_rcu(&chunk->head, __put_chunk);
141 }
142
143 static struct audit_chunk *alloc_chunk(int count)
144 {
145         struct audit_chunk *chunk;
146         size_t size;
147         int i;
148
149         size = offsetof(struct audit_chunk, owners) + count * sizeof(struct node);
150         chunk = kzalloc(size, GFP_KERNEL);
151         if (!chunk)
152                 return NULL;
153
154         INIT_LIST_HEAD(&chunk->hash);
155         INIT_LIST_HEAD(&chunk->trees);
156         chunk->count = count;
157         atomic_long_set(&chunk->refs, 1);
158         for (i = 0; i < count; i++) {
159                 INIT_LIST_HEAD(&chunk->owners[i].list);
160                 chunk->owners[i].index = i;
161         }
162         fsnotify_init_mark(&chunk->mark, audit_tree_destroy_watch);
163         return chunk;
164 }
165
166 enum {HASH_SIZE = 128};
167 static struct list_head chunk_hash_heads[HASH_SIZE];
168 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(hash_lock);
169
170 static inline struct list_head *chunk_hash(const struct inode *inode)
171 {
172         unsigned long n = (unsigned long)inode / L1_CACHE_BYTES;
173         return chunk_hash_heads + n % HASH_SIZE;
174 }
175
176 /* hash_lock & entry->lock is held by caller */
177 static void insert_hash(struct audit_chunk *chunk)
178 {
179         struct fsnotify_mark *entry = &chunk->mark;
180         struct list_head *list;
181
182         if (!entry->i.inode)
183                 return;
184         list = chunk_hash(entry->i.inode);
185         list_add_rcu(&chunk->hash, list);
186 }
187
188 /* called under rcu_read_lock */
189 struct audit_chunk *audit_tree_lookup(const struct inode *inode)
190 {
191         struct list_head *list = chunk_hash(inode);
192         struct audit_chunk *p;
193
194         list_for_each_entry_rcu(p, list, hash) {
195                 /* mark.inode may have gone NULL, but who cares? */
196                 if (p->mark.i.inode == inode) {
197                         atomic_long_inc(&p->refs);
198                         return p;
199                 }
200         }
201         return NULL;
202 }
203
204 int audit_tree_match(struct audit_chunk *chunk, struct audit_tree *tree)
205 {
206         int n;
207         for (n = 0; n < chunk->count; n++)
208                 if (chunk->owners[n].owner == tree)
209                         return 1;
210         return 0;
211 }
212
213 /* tagging and untagging inodes with trees */
214
215 static struct audit_chunk *find_chunk(struct node *p)
216 {
217         int index = p->index & ~(1U<<31);
218         p -= index;
219         return container_of(p, struct audit_chunk, owners[0]);
220 }
221
222 static void untag_chunk(struct node *p)
223 {
224         struct audit_chunk *chunk = find_chunk(p);
225         struct fsnotify_mark *entry = &chunk->mark;
226         struct audit_chunk *new = NULL;
227         struct audit_tree *owner;
228         int size = chunk->count - 1;
229         int i, j;
230
231         fsnotify_get_mark(entry);
232
233         spin_unlock(&hash_lock);
234
235         if (size)
236                 new = alloc_chunk(size);
237
238         spin_lock(&entry->lock);
239         if (chunk->dead || !entry->i.inode) {
240                 spin_unlock(&entry->lock);
241                 if (new)
242                         free_chunk(new);
243                 goto out;
244         }
245
246         owner = p->owner;
247
248         if (!size) {
249                 chunk->dead = 1;
250                 spin_lock(&hash_lock);
251                 list_del_init(&chunk->trees);
252                 if (owner->root == chunk)
253                         owner->root = NULL;
254                 list_del_init(&p->list);
255                 list_del_rcu(&chunk->hash);
256                 spin_unlock(&hash_lock);
257                 spin_unlock(&entry->lock);
258                 fsnotify_destroy_mark(entry);
259                 goto out;
260         }
261
262         if (!new)
263                 goto Fallback;
264
265         fsnotify_duplicate_mark(&new->mark, entry);
266         if (fsnotify_add_mark(&new->mark, new->mark.group, new->mark.i.inode, NULL, 1)) {
267                 fsnotify_put_mark(&new->mark);
268                 goto Fallback;
269         }
270
271         chunk->dead = 1;
272         spin_lock(&hash_lock);
273         list_replace_init(&chunk->trees, &new->trees);
274         if (owner->root == chunk) {
275                 list_del_init(&owner->same_root);
276                 owner->root = NULL;
277         }
278
279         for (i = j = 0; j <= size; i++, j++) {
280                 struct audit_tree *s;
281                 if (&chunk->owners[j] == p) {
282                         list_del_init(&p->list);
283                         i--;
284                         continue;
285                 }
286                 s = chunk->owners[j].owner;
287                 new->owners[i].owner = s;
288                 new->owners[i].index = chunk->owners[j].index - j + i;
289                 if (!s) /* result of earlier fallback */
290                         continue;
291                 get_tree(s);
292                 list_replace_init(&chunk->owners[j].list, &new->owners[i].list);
293         }
294
295         list_replace_rcu(&chunk->hash, &new->hash);
296         list_for_each_entry(owner, &new->trees, same_root)
297                 owner->root = new;
298         spin_unlock(&hash_lock);
299         spin_unlock(&entry->lock);
300         fsnotify_destroy_mark(entry);
301         goto out;
302
303 Fallback:
304         // do the best we can
305         spin_lock(&hash_lock);
306         if (owner->root == chunk) {
307                 list_del_init(&owner->same_root);
308                 owner->root = NULL;
309         }
310         list_del_init(&p->list);
311         p->owner = NULL;
312         put_tree(owner);
313         spin_unlock(&hash_lock);
314         spin_unlock(&entry->lock);
315 out:
316         fsnotify_put_mark(entry);
317         spin_lock(&hash_lock);
318 }
319
320 static int create_chunk(struct inode *inode, struct audit_tree *tree)
321 {
322         struct fsnotify_mark *entry;
323         struct audit_chunk *chunk = alloc_chunk(1);
324         if (!chunk)
325                 return -ENOMEM;
326
327         entry = &chunk->mark;
328         if (fsnotify_add_mark(entry, audit_tree_group, inode, NULL, 0)) {
329                 fsnotify_put_mark(entry);
330                 return -ENOSPC;
331         }
332
333         spin_lock(&entry->lock);
334         spin_lock(&hash_lock);
335         if (tree->goner) {
336                 spin_unlock(&hash_lock);
337                 chunk->dead = 1;
338                 spin_unlock(&entry->lock);
339                 fsnotify_get_mark(entry);
340                 fsnotify_destroy_mark(entry);
341                 fsnotify_put_mark(entry);
342                 return 0;
343         }
344         chunk->owners[0].index = (1U << 31);
345         chunk->owners[0].owner = tree;
346         get_tree(tree);
347         list_add(&chunk->owners[0].list, &tree->chunks);
348         if (!tree->root) {
349                 tree->root = chunk;
350                 list_add(&tree->same_root, &chunk->trees);
351         }
352         insert_hash(chunk);
353         spin_unlock(&hash_lock);
354         spin_unlock(&entry->lock);
355         return 0;
356 }
357
358 /* the first tagged inode becomes root of tree */
359 static int tag_chunk(struct inode *inode, struct audit_tree *tree)
360 {
361         struct fsnotify_mark *old_entry, *chunk_entry;
362         struct audit_tree *owner;
363         struct audit_chunk *chunk, *old;
364         struct node *p;
365         int n;
366
367         old_entry = fsnotify_find_inode_mark(audit_tree_group, inode);
368         if (!old_entry)
369                 return create_chunk(inode, tree);
370
371         old = container_of(old_entry, struct audit_chunk, mark);
372
373         /* are we already there? */
374         spin_lock(&hash_lock);
375         for (n = 0; n < old->count; n++) {
376                 if (old->owners[n].owner == tree) {
377                         spin_unlock(&hash_lock);
378                         fsnotify_put_mark(old_entry);
379                         return 0;
380                 }
381         }
382         spin_unlock(&hash_lock);
383
384         chunk = alloc_chunk(old->count + 1);
385         if (!chunk) {
386                 fsnotify_put_mark(old_entry);
387                 return -ENOMEM;
388         }
389
390         chunk_entry = &chunk->mark;
391
392         spin_lock(&old_entry->lock);
393         if (!old_entry->i.inode) {
394                 /* old_entry is being shot, lets just lie */
395                 spin_unlock(&old_entry->lock);
396                 fsnotify_put_mark(old_entry);
397                 free_chunk(chunk);
398                 return -ENOENT;
399         }
400
401         fsnotify_duplicate_mark(chunk_entry, old_entry);
402         if (fsnotify_add_mark(chunk_entry, chunk_entry->group, chunk_entry->i.inode, NULL, 1)) {
403                 spin_unlock(&old_entry->lock);
404                 fsnotify_put_mark(chunk_entry);
405                 fsnotify_put_mark(old_entry);
406                 return -ENOSPC;
407         }
408
409         /* even though we hold old_entry->lock, this is safe since chunk_entry->lock could NEVER have been grabbed before */
410         spin_lock(&chunk_entry->lock);
411         spin_lock(&hash_lock);
412
413         /* we now hold old_entry->lock, chunk_entry->lock, and hash_lock */
414         if (tree->goner) {
415                 spin_unlock(&hash_lock);
416                 chunk->dead = 1;
417                 spin_unlock(&chunk_entry->lock);
418                 spin_unlock(&old_entry->lock);
419
420                 fsnotify_get_mark(chunk_entry);
421                 fsnotify_destroy_mark(chunk_entry);
422
423                 fsnotify_put_mark(chunk_entry);
424                 fsnotify_put_mark(old_entry);
425                 return 0;
426         }
427         list_replace_init(&old->trees, &chunk->trees);
428         for (n = 0, p = chunk->owners; n < old->count; n++, p++) {
429                 struct audit_tree *s = old->owners[n].owner;
430                 p->owner = s;
431                 p->index = old->owners[n].index;
432                 if (!s) /* result of fallback in untag */
433                         continue;
434                 get_tree(s);
435                 list_replace_init(&old->owners[n].list, &p->list);
436         }
437         p->index = (chunk->count - 1) | (1U<<31);
438         p->owner = tree;
439         get_tree(tree);
440         list_add(&p->list, &tree->chunks);
441         list_replace_rcu(&old->hash, &chunk->hash);
442         list_for_each_entry(owner, &chunk->trees, same_root)
443                 owner->root = chunk;
444         old->dead = 1;
445         if (!tree->root) {
446                 tree->root = chunk;
447                 list_add(&tree->same_root, &chunk->trees);
448         }
449         spin_unlock(&hash_lock);
450         spin_unlock(&chunk_entry->lock);
451         spin_unlock(&old_entry->lock);
452         fsnotify_destroy_mark(old_entry);
453         fsnotify_put_mark(old_entry); /* pair to fsnotify_find mark_entry */
454         return 0;
455 }
456
457 static void kill_rules(struct audit_tree *tree)
458 {
459         struct audit_krule *rule, *next;
460         struct audit_entry *entry;
461         struct audit_buffer *ab;
462
463         list_for_each_entry_safe(rule, next, &tree->rules, rlist) {
464                 entry = container_of(rule, struct audit_entry, rule);
465
466                 list_del_init(&rule->rlist);
467                 if (rule->tree) {
468                         /* not a half-baked one */
469                         ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE);
470                         audit_log_format(ab, "op=");
471                         audit_log_string(ab, "remove rule");
472                         audit_log_format(ab, " dir=");
473                         audit_log_untrustedstring(ab, rule->tree->pathname);
474                         audit_log_key(ab, rule->filterkey);
475                         audit_log_format(ab, " list=%d res=1", rule->listnr);
476                         audit_log_end(ab);
477                         rule->tree = NULL;
478                         list_del_rcu(&entry->list);
479                         list_del(&entry->rule.list);
480                         call_rcu(&entry->rcu, audit_free_rule_rcu);
481                 }
482         }
483 }
484
485 /*
486  * finish killing struct audit_tree
487  */
488 static void prune_one(struct audit_tree *victim)
489 {
490         spin_lock(&hash_lock);
491         while (!list_empty(&victim->chunks)) {
492                 struct node *p;
493
494                 p = list_entry(victim->chunks.next, struct node, list);
495
496                 untag_chunk(p);
497         }
498         spin_unlock(&hash_lock);
499         put_tree(victim);
500 }
501
502 /* trim the uncommitted chunks from tree */
503
504 static void trim_marked(struct audit_tree *tree)
505 {
506         struct list_head *p, *q;
507         spin_lock(&hash_lock);
508         if (tree->goner) {
509                 spin_unlock(&hash_lock);
510                 return;
511         }
512         /* reorder */
513         for (p = tree->chunks.next; p != &tree->chunks; p = q) {
514                 struct node *node = list_entry(p, struct node, list);
515                 q = p->next;
516                 if (node->index & (1U<<31)) {
517                         list_del_init(p);
518                         list_add(p, &tree->chunks);
519                 }
520         }
521
522         while (!list_empty(&tree->chunks)) {
523                 struct node *node;
524
525                 node = list_entry(tree->chunks.next, struct node, list);
526
527                 /* have we run out of marked? */
528                 if (!(node->index & (1U<<31)))
529                         break;
530
531                 untag_chunk(node);
532         }
533         if (!tree->root && !tree->goner) {
534                 tree->goner = 1;
535                 spin_unlock(&hash_lock);
536                 mutex_lock(&audit_filter_mutex);
537                 kill_rules(tree);
538                 list_del_init(&tree->list);
539                 mutex_unlock(&audit_filter_mutex);
540                 prune_one(tree);
541         } else {
542                 spin_unlock(&hash_lock);
543         }
544 }
545
546 static void audit_schedule_prune(void);
547
548 /* called with audit_filter_mutex */
549 int audit_remove_tree_rule(struct audit_krule *rule)
550 {
551         struct audit_tree *tree;
552         tree = rule->tree;
553         if (tree) {
554                 spin_lock(&hash_lock);
555                 list_del_init(&rule->rlist);
556                 if (list_empty(&tree->rules) && !tree->goner) {
557                         tree->root = NULL;
558                         list_del_init(&tree->same_root);
559                         tree->goner = 1;
560                         list_move(&tree->list, &prune_list);
561                         rule->tree = NULL;
562                         spin_unlock(&hash_lock);
563                         audit_schedule_prune();
564                         return 1;
565                 }
566                 rule->tree = NULL;
567                 spin_unlock(&hash_lock);
568                 return 1;
569         }
570         return 0;
571 }
572
573 static int compare_root(struct vfsmount *mnt, void *arg)
574 {
575         return mnt->mnt_root->d_inode == arg;
576 }
577
578 void audit_trim_trees(void)
579 {
580         struct list_head cursor;
581
582         mutex_lock(&audit_filter_mutex);
583         list_add(&cursor, &tree_list);
584         while (cursor.next != &tree_list) {
585                 struct audit_tree *tree;
586                 struct path path;
587                 struct vfsmount *root_mnt;
588                 struct node *node;
589                 int err;
590
591                 tree = container_of(cursor.next, struct audit_tree, list);
592                 get_tree(tree);
593                 list_del(&cursor);
594                 list_add(&cursor, &tree->list);
595                 mutex_unlock(&audit_filter_mutex);
596
597                 err = kern_path(tree->pathname, 0, &path);
598                 if (err)
599                         goto skip_it;
600
601                 root_mnt = collect_mounts(&path);
602                 path_put(&path);
603                 if (!root_mnt)
604                         goto skip_it;
605
606                 spin_lock(&hash_lock);
607                 list_for_each_entry(node, &tree->chunks, list) {
608                         struct audit_chunk *chunk = find_chunk(node);
609                         /* this could be NULL if the watch is dying else where... */
610                         struct inode *inode = chunk->mark.i.inode;
611                         node->index |= 1U<<31;
612                         if (iterate_mounts(compare_root, inode, root_mnt))
613                                 node->index &= ~(1U<<31);
614                 }
615                 spin_unlock(&hash_lock);
616                 trim_marked(tree);
617                 put_tree(tree);
618                 drop_collected_mounts(root_mnt);
619 skip_it:
620                 mutex_lock(&audit_filter_mutex);
621         }
622         list_del(&cursor);
623         mutex_unlock(&audit_filter_mutex);
624 }
625
626 int audit_make_tree(struct audit_krule *rule, char *pathname, u32 op)
627 {
628
629         if (pathname[0] != '/' ||
630             rule->listnr != AUDIT_FILTER_EXIT ||
631             op != Audit_equal ||
632             rule->inode_f || rule->watch || rule->tree)
633                 return -EINVAL;
634         rule->tree = alloc_tree(pathname);
635         if (!rule->tree)
636                 return -ENOMEM;
637         return 0;
638 }
639
640 void audit_put_tree(struct audit_tree *tree)
641 {
642         put_tree(tree);
643 }
644
645 static int tag_mount(struct vfsmount *mnt, void *arg)
646 {
647         return tag_chunk(mnt->mnt_root->d_inode, arg);
648 }
649
650 /* called with audit_filter_mutex */
651 int audit_add_tree_rule(struct audit_krule *rule)
652 {
653         struct audit_tree *seed = rule->tree, *tree;
654         struct path path;
655         struct vfsmount *mnt;
656         int err;
657
658         list_for_each_entry(tree, &tree_list, list) {
659                 if (!strcmp(seed->pathname, tree->pathname)) {
660                         put_tree(seed);
661                         rule->tree = tree;
662                         list_add(&rule->rlist, &tree->rules);
663                         return 0;
664                 }
665         }
666         tree = seed;
667         list_add(&tree->list, &tree_list);
668         list_add(&rule->rlist, &tree->rules);
669         /* do not set rule->tree yet */
670         mutex_unlock(&audit_filter_mutex);
671
672         err = kern_path(tree->pathname, 0, &path);
673         if (err)
674                 goto Err;
675         mnt = collect_mounts(&path);
676         path_put(&path);
677         if (!mnt) {
678                 err = -ENOMEM;
679                 goto Err;
680         }
681
682         get_tree(tree);
683         err = iterate_mounts(tag_mount, tree, mnt);
684         drop_collected_mounts(mnt);
685
686         if (!err) {
687                 struct node *node;
688                 spin_lock(&hash_lock);
689                 list_for_each_entry(node, &tree->chunks, list)
690                         node->index &= ~(1U<<31);
691                 spin_unlock(&hash_lock);
692         } else {
693                 trim_marked(tree);
694                 goto Err;
695         }
696
697         mutex_lock(&audit_filter_mutex);
698         if (list_empty(&rule->rlist)) {
699                 put_tree(tree);
700                 return -ENOENT;
701         }
702         rule->tree = tree;
703         put_tree(tree);
704
705         return 0;
706 Err:
707         mutex_lock(&audit_filter_mutex);
708         list_del_init(&tree->list);
709         list_del_init(&tree->rules);
710         put_tree(tree);
711         return err;
712 }
713
714 int audit_tag_tree(char *old, char *new)
715 {
716         struct list_head cursor, barrier;
717         int failed = 0;
718         struct path path1, path2;
719         struct vfsmount *tagged;
720         int err;
721
722         err = kern_path(new, 0, &path2);
723         if (err)
724                 return err;
725         tagged = collect_mounts(&path2);
726         path_put(&path2);
727         if (!tagged)
728                 return -ENOMEM;
729
730         err = kern_path(old, 0, &path1);
731         if (err) {
732                 drop_collected_mounts(tagged);
733                 return err;
734         }
735
736         mutex_lock(&audit_filter_mutex);
737         list_add(&barrier, &tree_list);
738         list_add(&cursor, &barrier);
739
740         while (cursor.next != &tree_list) {
741                 struct audit_tree *tree;
742                 int good_one = 0;
743
744                 tree = container_of(cursor.next, struct audit_tree, list);
745                 get_tree(tree);
746                 list_del(&cursor);
747                 list_add(&cursor, &tree->list);
748                 mutex_unlock(&audit_filter_mutex);
749
750                 err = kern_path(tree->pathname, 0, &path2);
751                 if (!err) {
752                         good_one = path_is_under(&path1, &path2);
753                         path_put(&path2);
754                 }
755
756                 if (!good_one) {
757                         put_tree(tree);
758                         mutex_lock(&audit_filter_mutex);
759                         continue;
760                 }
761
762                 failed = iterate_mounts(tag_mount, tree, tagged);
763                 if (failed) {
764                         put_tree(tree);
765                         mutex_lock(&audit_filter_mutex);
766                         break;
767                 }
768
769                 mutex_lock(&audit_filter_mutex);
770                 spin_lock(&hash_lock);
771                 if (!tree->goner) {
772                         list_del(&tree->list);
773                         list_add(&tree->list, &tree_list);
774                 }
775                 spin_unlock(&hash_lock);
776                 put_tree(tree);
777         }
778
779         while (barrier.prev != &tree_list) {
780                 struct audit_tree *tree;
781
782                 tree = container_of(barrier.prev, struct audit_tree, list);
783                 get_tree(tree);
784                 list_del(&tree->list);
785                 list_add(&tree->list, &barrier);
786                 mutex_unlock(&audit_filter_mutex);
787
788                 if (!failed) {
789                         struct node *node;
790                         spin_lock(&hash_lock);
791                         list_for_each_entry(node, &tree->chunks, list)
792                                 node->index &= ~(1U<<31);
793                         spin_unlock(&hash_lock);
794                 } else {
795                         trim_marked(tree);
796                 }
797
798                 put_tree(tree);
799                 mutex_lock(&audit_filter_mutex);
800         }
801         list_del(&barrier);
802         list_del(&cursor);
803         mutex_unlock(&audit_filter_mutex);
804         path_put(&path1);
805         drop_collected_mounts(tagged);
806         return failed;
807 }
808
809 /*
810  * That gets run when evict_chunk() ends up needing to kill audit_tree.
811  * Runs from a separate thread.
812  */
813 static int prune_tree_thread(void *unused)
814 {
815         mutex_lock(&audit_cmd_mutex);
816         mutex_lock(&audit_filter_mutex);
817
818         while (!list_empty(&prune_list)) {
819                 struct audit_tree *victim;
820
821                 victim = list_entry(prune_list.next, struct audit_tree, list);
822                 list_del_init(&victim->list);
823
824                 mutex_unlock(&audit_filter_mutex);
825
826                 prune_one(victim);
827
828                 mutex_lock(&audit_filter_mutex);
829         }
830
831         mutex_unlock(&audit_filter_mutex);
832         mutex_unlock(&audit_cmd_mutex);
833         return 0;
834 }
835
836 static void audit_schedule_prune(void)
837 {
838         kthread_run(prune_tree_thread, NULL, "audit_prune_tree");
839 }
840
841 /*
842  * ... and that one is done if evict_chunk() decides to delay until the end
843  * of syscall.  Runs synchronously.
844  */
845 void audit_kill_trees(struct list_head *list)
846 {
847         mutex_lock(&audit_cmd_mutex);
848         mutex_lock(&audit_filter_mutex);
849
850         while (!list_empty(list)) {
851                 struct audit_tree *victim;
852
853                 victim = list_entry(list->next, struct audit_tree, list);
854                 kill_rules(victim);
855                 list_del_init(&victim->list);
856
857                 mutex_unlock(&audit_filter_mutex);
858
859                 prune_one(victim);
860
861                 mutex_lock(&audit_filter_mutex);
862         }
863
864         mutex_unlock(&audit_filter_mutex);
865         mutex_unlock(&audit_cmd_mutex);
866 }
867
868 /*
869  *  Here comes the stuff asynchronous to auditctl operations
870  */
871
872 static void evict_chunk(struct audit_chunk *chunk)
873 {
874         struct audit_tree *owner;
875         struct list_head *postponed = audit_killed_trees();
876         int need_prune = 0;
877         int n;
878
879         if (chunk->dead)
880                 return;
881
882         chunk->dead = 1;
883         mutex_lock(&audit_filter_mutex);
884         spin_lock(&hash_lock);
885         while (!list_empty(&chunk->trees)) {
886                 owner = list_entry(chunk->trees.next,
887                                    struct audit_tree, same_root);
888                 owner->goner = 1;
889                 owner->root = NULL;
890                 list_del_init(&owner->same_root);
891                 spin_unlock(&hash_lock);
892                 if (!postponed) {
893                         kill_rules(owner);
894                         list_move(&owner->list, &prune_list);
895                         need_prune = 1;
896                 } else {
897                         list_move(&owner->list, postponed);
898                 }
899                 spin_lock(&hash_lock);
900         }
901         list_del_rcu(&chunk->hash);
902         for (n = 0; n < chunk->count; n++)
903                 list_del_init(&chunk->owners[n].list);
904         spin_unlock(&hash_lock);
905         if (need_prune)
906                 audit_schedule_prune();
907         mutex_unlock(&audit_filter_mutex);
908 }
909
910 static int audit_tree_handle_event(struct fsnotify_group *group,
911                                    struct fsnotify_mark *inode_mark,
912                                    struct fsnotify_mark *vfsmonut_mark,
913                                    struct fsnotify_event *event)
914 {
915         BUG();
916         return -EOPNOTSUPP;
917 }
918
919 static void audit_tree_freeing_mark(struct fsnotify_mark *entry, struct fsnotify_group *group)
920 {
921         struct audit_chunk *chunk = container_of(entry, struct audit_chunk, mark);
922
923         evict_chunk(chunk);
924         fsnotify_put_mark(entry);
925 }
926
927 static bool audit_tree_send_event(struct fsnotify_group *group, struct inode *inode,
928                                   struct fsnotify_mark *inode_mark,
929                                   struct fsnotify_mark *vfsmount_mark,
930                                   __u32 mask, void *data, int data_type)
931 {
932         return false;
933 }
934
935 static const struct fsnotify_ops audit_tree_ops = {
936         .handle_event = audit_tree_handle_event,
937         .should_send_event = audit_tree_send_event,
938         .free_group_priv = NULL,
939         .free_event_priv = NULL,
940         .freeing_mark = audit_tree_freeing_mark,
941 };
942
943 static int __init audit_tree_init(void)
944 {
945         int i;
946
947         audit_tree_group = fsnotify_alloc_group(&audit_tree_ops);
948         if (IS_ERR(audit_tree_group))
949                 audit_panic("cannot initialize fsnotify group for rectree watches");
950
951         for (i = 0; i < HASH_SIZE; i++)
952                 INIT_LIST_HEAD(&chunk_hash_heads[i]);
953
954         return 0;
955 }
956 __initcall(audit_tree_init);