This repository has been archived by the owner on Jun 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathevent.c
1177 lines (1030 loc) · 29.7 KB
/
event.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* dazukofs: access control stackable filesystem
Copyright (C) 2008-2010 John Ogness
Author: John Ogness <[email protected]>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <linux/list.h>
#include <linux/sched.h>
#include <linux/file.h>
#include <linux/fs.h>
#include <linux/path.h>
#include <linux/mount.h>
#include <linux/freezer.h>
#include <linux/cred.h>
#include <linux/pid.h>
#include <linux/slab.h>
#include "dev.h"
#include "dazukofs_fs.h"
#include "event.h"
struct dazukofs_proc {
struct list_head list;
struct pid *proc_id;
int within_list;
};
struct dazukofs_event {
unsigned long event_id;
struct dentry *dentry;
struct vfsmount *mnt;
struct pid *proc_id;
wait_queue_head_t queue;
/* protects: deny, deprecated, assigned */
struct mutex assigned_mutex;
int deny;
int deprecated;
int assigned;
};
struct dazukofs_event_container {
struct list_head list;
struct dazukofs_event *event;
struct file *file;
int fd;
};
struct dazukofs_group {
struct list_head list;
char *name;
size_t name_length;
unsigned long group_id;
struct dazukofs_event_container todo_list;
wait_queue_head_t queue;
wait_queue_head_t poll_queue;
struct dazukofs_event_container working_list;
atomic_t use_count;
int tracking;
int track_count;
int deprecated;
};
static struct dazukofs_group group_list;
static int group_count;
/* protects: group_list, group_count */
static struct rw_semaphore group_count_sem;
/* protects: group_list, grp->members, last_event_id,
* todo_list, working_list */
static struct mutex work_mutex;
static struct mutex proc_mutex;
static struct dazukofs_proc proc_list;
static struct kmem_cache *dazukofs_group_cachep;
static struct kmem_cache *dazukofs_event_container_cachep;
static struct kmem_cache *dazukofs_event_cachep;
static int last_event_id;
/**
* dazukofs_init_events - initialize event handling infrastructure
*
* Description: This is called once to initialize all the structures
* needed to manage event handling.
*
* Returns 0 on success.
*/
int dazukofs_init_events(void)
{
mutex_init(&proc_mutex);
mutex_init(&work_mutex);
init_rwsem(&group_count_sem);
INIT_LIST_HEAD(&proc_list.list);
INIT_LIST_HEAD(&group_list.list);
dazukofs_group_cachep =
kmem_cache_create("dazukofs_group_cache",
sizeof(struct dazukofs_group), 0,
SLAB_HWCACHE_ALIGN, NULL);
if (!dazukofs_group_cachep)
goto error_out;
dazukofs_event_container_cachep =
kmem_cache_create("dazukofs_event_container_cache",
sizeof(struct dazukofs_event_container), 0,
SLAB_HWCACHE_ALIGN, NULL);
if (!dazukofs_event_container_cachep)
goto error_out;
dazukofs_event_cachep =
kmem_cache_create("dazukofs_event_cache",
sizeof(struct dazukofs_event), 0,
SLAB_HWCACHE_ALIGN, NULL);
if (!dazukofs_event_cachep)
goto error_out;
return 0;
error_out:
if (dazukofs_group_cachep)
kmem_cache_destroy(dazukofs_group_cachep);
if (dazukofs_event_container_cachep)
kmem_cache_destroy(dazukofs_event_container_cachep);
if (dazukofs_event_cachep)
kmem_cache_destroy(dazukofs_event_cachep);
return -ENOMEM;
}
/**
* release_event - release (and possible free) an event
* @evt: the event to release
* @decrement_assigned: flag to signal if the assigned count should be
* decremented (only for registered processes)
* @deny: flag if file access event should be denied
*
* Description: This function will decrement the assigned count for the
* event. The "decrement_assigned" flag is used to distinguish between
* the anonymous process accessing a file and the registered process. The
* assigned count is only incremented for registered process (although the
* anonymous process will also have a handle to the event).
*
* For the anonymous process (decrement_assigned = false):
* If the assigned count is not zero, there are registered processes that
* have a handle to this event. The event is marked deprecated. Otherwise
* we free the event.
*
* For a registered process (decrement_assigned = true):
* The assigned count is decremented. If it is now zero and the event is
* not deprecated, then the anonymous process still has a handle. In this
* case we wake the anonymous process. Otherwise we free the event.
*
* Aside from releasing the event, the deny status of the event is also
* updated. The "normal" release process involves the registered processes
* first releasing (and providing their deny values) and finally the
* anonymous process will release (and free) the event after reading the
* deny value.
*/
static void release_event(struct dazukofs_event *evt, int decrement_assigned,
int deny)
{
int free_event = 0;
mutex_lock(&evt->assigned_mutex);
if (deny)
evt->deny |= 1;
if (decrement_assigned) {
evt->assigned--;
if (evt->assigned == 0) {
if (!evt->deprecated)
wake_up(&evt->queue);
else
free_event = 1;
}
} else {
if (evt->assigned == 0)
free_event = 1;
else
evt->deprecated = 1;
}
mutex_unlock(&evt->assigned_mutex);
if (free_event) {
dput(evt->dentry);
mntput(evt->mnt);
put_pid(evt->proc_id);
kmem_cache_free(dazukofs_event_cachep, evt);
}
}
/**
* __clear_group_event_list - cleanup/release event list
* @event_list - the list to clear
*
* Description: All events (and their containers) will be released/freed
* for the given event list. The event list will be an empty (yet still
* valid) list after this function is finished.
*
* IMPORTANT: This function requires work_mutex to be held!
*/
static void __clear_group_event_list(struct list_head *event_list)
{
struct dazukofs_event_container *ec;
struct list_head *pos;
struct list_head *q;
list_for_each_safe(pos, q, event_list) {
ec = list_entry(pos, struct dazukofs_event_container, list);
list_del(pos);
release_event(ec->event, 1, 0);
kmem_cache_free(dazukofs_event_container_cachep, ec);
}
}
/**
* __remove_group - clear all activity associated with the group
* @grp: the group to clear
*
* Description: All pending and in-progress events are released/freed.
* Any processes waiting on the queue are woken.
*
* The actual group structure is not deleted, but rather marked as
* deprecated. Deprecated group structures are deleted as new
* groups are added.
*
* IMPORTANT: This function requires work_mutex to be held!
*/
static void __remove_group(struct dazukofs_group *grp)
{
grp->deprecated = 1;
group_count--;
__clear_group_event_list(&grp->working_list.list);
__clear_group_event_list(&grp->todo_list.list);
/* notify all registered process waiting for an event */
wake_up_all(&grp->queue);
wake_up_all(&grp->poll_queue);
}
/**
* dazukofs_destroy_events - cleanup/shutdown event handling infrastructure
*
* Description: Release all pending events, free all allocated structures.
*/
void dazukofs_destroy_events(void)
{
struct dazukofs_group *grp;
struct list_head *pos;
struct list_head *q;
/*
* We are not using any locks here because we assume
* everything else has been already cleaned up by
* the device layer.
*/
/* free the groups */
list_for_each_safe(pos, q, &group_list.list) {
grp = list_entry(pos, struct dazukofs_group, list);
list_del(pos);
__remove_group(grp);
/* free group name */
kfree(grp->name);
/* free group */
kmem_cache_free(dazukofs_group_cachep, grp);
}
/* free everything else */
kmem_cache_destroy(dazukofs_group_cachep);
kmem_cache_destroy(dazukofs_event_container_cachep);
kmem_cache_destroy(dazukofs_event_cachep);
}
/**
* __check_for_group - check if a group exists and set tracking
* @name: a group name to check for
* @id: a group id to check for
* @track: flag set if tracking is to be used
* @already_exists: will be set if the group already exists
*
* Description: This function checks names and id's to see if a group may
* be created. If the id already exists, but with a different group name,
* the group cannot be created. If the group name exists, but with a
* different id, the group cannot be created.
*
* If the group name exists and the id is already that which is requested,
* the function returns success, but sets the already_exists flag.
*
* NOTE: Although the function name may imply read-only, this function
* _will_ set a group to track if the group is found to exist and
* tracking should be set. We do this because it is convenient
* since the work_mutex is already locked.
*
* IMPORTANT: This function requires work_mutex to be held!
*
* Returns 0 if the group exists or may be created.
*/
static int __check_for_group(const char *name, int id, int track,
int *already_exists)
{
struct dazukofs_group *grp;
struct list_head *pos;
struct list_head *q;
int id_available = 1;
*already_exists = 0;
list_for_each_safe(pos, q, &group_list.list) {
grp = list_entry(pos, struct dazukofs_group, list);
if (grp->deprecated) {
/* cleanup deprecated groups */
if (atomic_read(&grp->use_count) == 0) {
list_del(pos);
kfree(grp->name);
kmem_cache_free(dazukofs_group_cachep, grp);
}
} else {
if (strcmp(name, grp->name) == 0) {
*already_exists = 1;
if (track)
grp->tracking = 1;
break;
} else if (grp->group_id == id) {
id_available = 0;
break;
}
}
}
if (*already_exists)
return 0;
if (id_available) {
/* we have found a free id */
return 0;
}
return -1;
}
/**
* __create_group - allocate and initialize a group structure
* @name: the name of the new group
* @id: the id of the new group
* @track: flag set if tracking is to be used
*
* Description: This function allocates and initializes a group
* structure. The group_count should be locked to ensure that
* the group id remains available until the group can be
* added to the group list.
*
* Returns the newly created and initialized group structure.
*/
static struct dazukofs_group *__create_group(const char *name, int id,
int track)
{
struct dazukofs_group *grp;
grp = kmem_cache_zalloc(dazukofs_group_cachep, GFP_KERNEL);
if (!grp)
return NULL;
atomic_set(&grp->use_count, 0);
grp->group_id = id;
grp->name = kstrdup(name, GFP_KERNEL);
if (!grp->name) {
kmem_cache_free(dazukofs_group_cachep, grp);
return NULL;
}
grp->name_length = strlen(name);
init_waitqueue_head(&grp->queue);
init_waitqueue_head(&grp->poll_queue);
INIT_LIST_HEAD(&grp->todo_list.list);
INIT_LIST_HEAD(&grp->working_list.list);
if (track)
grp->tracking = 1;
return grp;
}
/**
* dazukofs_add_group - add a new group
* @name: the name of the group to add
* @track: flag set if tracking is to be used
*
* Description: This function is called by the device layer to add a new
* group. It returns success if the group has been successfully created
* or if the group already exists.
*
* If the group already exists and is not tracking, but "track" is set,
* the group will be changed to start tracking (actually done in the
* function __check_for_group()).
*
* Returns 0 on success.
*/
int dazukofs_add_group(const char *name, int track)
{
int ret = 0;
int already_exists;
int available_id = 0;
struct dazukofs_group *grp;
down_write(&group_count_sem);
mutex_lock(&work_mutex);
while (__check_for_group(name, available_id, track,
&already_exists) != 0) {
/* try again with the next id */
available_id++;
}
mutex_unlock(&work_mutex);
if (already_exists)
goto out;
/* if we are here, the group doesn't already exist */
/* do we have room for a new group? */
if (group_count == GROUP_COUNT) {
ret = -EPERM;
goto out;
}
grp = __create_group(name, available_id, track);
if (!grp) {
ret = -ENOMEM;
goto out;
}
mutex_lock(&work_mutex);
list_add_tail(&grp->list, &group_list.list);
mutex_unlock(&work_mutex);
group_count++;
out:
up_write(&group_count_sem);
return ret;
}
/**
* dazukofs_remove_group - remove a group
* @name: the name of the group to remove
* @unsued: argument not used
*
* Description: This function is called by the device layer to remove a
* group. It returns success if the group has been deleted or the group
* does not exist.
*
* The unused argument exists for convenience to the device layer.
*
* Returns 0 on success.
*/
int dazukofs_remove_group(const char *name, int unused)
{
int ret = 0;
struct dazukofs_group *grp;
struct list_head *pos;
down_write(&group_count_sem);
if (group_count == 0)
goto out;
mutex_lock(&work_mutex);
/* set group deprecated */
list_for_each(pos, &group_list.list) {
grp = list_entry(pos, struct dazukofs_group, list);
if (!grp->deprecated && strcmp(name, grp->name) == 0) {
__remove_group(grp);
break;
}
}
mutex_unlock(&work_mutex);
out:
up_write(&group_count_sem);
return ret;
}
/**
* dazukofs_get_groups - get the names and id's of active groups as strings
* @buf: to be assigned the list of groups as a single printable string
*
* Description: This function will allocate a string that includes all the
* active (not deprecated) groups and their id's. This function is called
* by the device layer for presenting userspace with the list of groups.
*
* This function will allocate memory that must be freed by the caller.
*
* Returns 0 on success.
*/
int dazukofs_get_groups(char **buf)
{
struct dazukofs_group *grp;
char *tmp;
struct list_head *pos;
size_t buflen;
size_t allocsize = 256;
tryagain:
*buf = kzalloc(allocsize, GFP_KERNEL);
if (!*buf)
return -ENOMEM;
tmp = *buf;
buflen = 1;
mutex_lock(&work_mutex);
list_for_each(pos, &group_list.list) {
grp = list_entry(pos, struct dazukofs_group, list);
if (!grp->deprecated)
buflen += grp->name_length + 3;
}
if (buflen < allocsize) {
list_for_each(pos, &group_list.list) {
grp = list_entry(pos, struct dazukofs_group, list);
if (!grp->deprecated) {
snprintf(tmp, (allocsize - 1) - (tmp - *buf),
"%lu:%s\n", grp->group_id,
grp->name);
tmp += grp->name_length + 3;
}
}
mutex_unlock(&work_mutex);
} else {
mutex_unlock(&work_mutex);
allocsize *= 2;
kfree(*buf);
goto tryagain;
}
return 0;
}
/**
* check_recursion - check if current process is recursing
*
* Description: A list of anonymous processes is managed to prevent
* access event recursion. This function checks if the current process is
* a part of that list.
*
* If the current process is found in the process list, it is removed.
*
* NOTE: The proc structure is not freed. It is only removed from the
* list. Since it is a recursive call, the caller can free the
* structure after the call chain is finished.
*
* Returns 0 if this is a recursive process call.
*/
static int check_recursion(void)
{
struct dazukofs_proc *proc;
struct list_head *pos;
int found = 0;
struct pid *cur_proc_id = get_pid(task_pid(current));
mutex_lock(&proc_mutex);
list_for_each(pos, &proc_list.list) {
proc = list_entry(pos, struct dazukofs_proc, list);
if (proc->proc_id == cur_proc_id) {
found = 1;
list_del(pos);
proc->within_list = 0;
put_pid(proc->proc_id);
break;
}
}
mutex_unlock(&proc_mutex);
put_pid(cur_proc_id);
/* process event if not found */
return !found;
}
/**
* event_assigned - check if event is (still) assigned
* @event: event to check
*
* Description: This function checks if an event is still assigned. An
* assigned event means that it is sitting on the todo or working list
* of a group.
*
* Returns the number assigned count.
*/
static int event_assigned(struct dazukofs_event *event)
{
int val;
mutex_lock(&event->assigned_mutex);
val = event->assigned;
mutex_unlock(&event->assigned_mutex);
return val;
}
/**
* check_access_precheck - check if an access event should be generated
* @grp_count: the current number of groups
*
* Description: Check if the current process should cause an access event
* to be generated.
*
* Returns 0 if an access event should be generated.
*/
static int check_access_precheck(int grp_count)
{
/* do we have any groups? */
if (grp_count == 0)
return -1;
/* am I a recursion process? */
if (!check_recursion())
return -1;
/* am I an ignored process? */
if (!dazukofs_check_ignore_process())
return -1;
return 0;
}
/**
* assign_event_to_groups - post an event to be processed
* @evt: the event to be posted
* @ec_array: the containers for the event
*
* Description: This function will assign a unique id to the event.
* The event will be associated with each container and the container is
* placed on each group's todo list. Each group will also be woken to
* handle the new event.
*/
static void
assign_event_to_groups(struct dazukofs_event *evt,
struct dazukofs_event_container *ec_array[])
{
struct dazukofs_group *grp;
struct list_head *pos;
int i;
mutex_lock(&work_mutex);
mutex_lock(&evt->assigned_mutex);
/* assign the event a "unique" id */
last_event_id++;
evt->event_id = last_event_id;
/* assign the event to each group */
i = 0;
list_for_each(pos, &group_list.list) {
grp = list_entry(pos, struct dazukofs_group, list);
if (!grp->deprecated) {
ec_array[i]->event = evt;
evt->assigned++;
list_add_tail(&ec_array[i]->list,
&grp->todo_list.list);
/* notify someone to handle the event */
wake_up(&grp->queue);
wake_up(&grp->poll_queue);
i++;
}
}
mutex_unlock(&evt->assigned_mutex);
mutex_unlock(&work_mutex);
}
/**
* allocate_event_and_containers - allocate an event and event containers
* @evt: event pointer to be assigned a new event
* @ec: event container array to be filled with new array of containers
* @grp_count: the number of groups (size of the array)
*
* Description: New event and event container structures are allocated
* and initialized.
*
* Returns 0 on success.
*/
static int
allocate_event_and_containers(struct dazukofs_event **evt,
struct dazukofs_event_container *ec_array[],
int grp_count)
{
int i;
*evt = kmem_cache_zalloc(dazukofs_event_cachep, GFP_KERNEL);
if (!*evt)
return -1;
init_waitqueue_head(&(*evt)->queue);
mutex_init(&(*evt)->assigned_mutex);
/* allocate containers now while we don't have a lock */
for (i = 0; i < grp_count; i++) {
ec_array[i] = kmem_cache_zalloc(
dazukofs_event_container_cachep, GFP_KERNEL);
if (!ec_array[i])
goto error_out;
}
return 0;
error_out:
for (i--; i >= 0; i--) {
kmem_cache_free(dazukofs_event_container_cachep, ec_array[i]);
ec_array[i] = NULL;
}
kmem_cache_free(dazukofs_event_cachep, *evt);
*evt = NULL;
return -1;
}
/**
* dazukofs_check_access - check for allowed file access
* @dentry: the dentry associated with the file access
* @mnt: the vfsmount associated with the file access
*
* Description: This is the only function used by the stackable filesystem
* layer to check if a file may be accessed.
*
* Returns 0 if the file access is allowed.
*/
int dazukofs_check_access(struct dentry *dentry, struct vfsmount *mnt)
{
struct dazukofs_event_container *ec_array[GROUP_COUNT];
struct dazukofs_event *evt;
int err = 0;
down_read(&group_count_sem);
if (check_access_precheck(group_count)) {
up_read(&group_count_sem);
return 0;
}
/* at this point, the access should be handled */
if (allocate_event_and_containers(&evt, ec_array, group_count)) {
up_read(&group_count_sem);
err = -ENOMEM;
goto out;
}
evt->dentry = dget(dentry);
evt->mnt = mntget(mnt);
evt->proc_id = get_pid(task_pid(current));
assign_event_to_groups(evt, ec_array);
up_read(&group_count_sem);
/* wait (uninterruptible) until event completely processed */
wait_event(evt->queue, event_assigned(evt) == 0);
if (evt->deny)
err = -EPERM;
release_event(evt, 0, 0);
out:
return err;
}
/**
* dazukofs_group_open_tracking - begin tracking this process
* @group_id: id of the group we belong to
*
* Description: This function is called by the device layer to begin
* tracking the current process (if tracking for that group is enabled).
*
* Tracking simply means to keep track if there are any processes still
* registered with the group, so we use a simple counter for that.
* dazukofs_group_release_tracking() must be called when this process
* unregisters.
*
* Returns 0 if tracking is _not_ enabled.
*/
int dazukofs_group_open_tracking(unsigned long group_id)
{
struct dazukofs_group *grp;
struct list_head *pos;
int tracking = 0;
mutex_lock(&work_mutex);
list_for_each(pos, &group_list.list) {
grp = list_entry(pos, struct dazukofs_group, list);
if (!grp->deprecated && grp->group_id == group_id) {
if (grp->tracking) {
atomic_inc(&grp->use_count);
grp->track_count++;
tracking = 1;
}
break;
}
}
mutex_unlock(&work_mutex);
return tracking;
}
/**
* dazukofs_group_release_tracking - stop tracking this process
* @group_id: id of the group we belong to
*
* Description: This function is called by the device layer when a process
* is no longer registered and thus tracking for this process should end
* (if tracking for the group is enabled).
*/
void dazukofs_group_release_tracking(unsigned long group_id)
{
struct dazukofs_group *grp;
struct list_head *pos;
mutex_lock(&work_mutex);
list_for_each(pos, &group_list.list) {
grp = list_entry(pos, struct dazukofs_group, list);
if (!grp->deprecated && grp->group_id == group_id) {
if (grp->tracking) {
atomic_dec(&grp->use_count);
grp->track_count--;
if (grp->track_count == 0)
__remove_group(grp);
}
break;
}
}
mutex_unlock(&work_mutex);
}
/**
* unclaim_event - return an event to the todo list
* @grp: group to which the event is assigned
* @ec: event container of the event to be returned
*
* Description: This function removes the given event container from its
* current list, puts it on the todo list, and wakes the group.
*/
static void unclaim_event(struct dazukofs_group *grp,
struct dazukofs_event_container *ec)
{
/* put the event on the todo list */
mutex_lock(&work_mutex);
list_del(&ec->list);
list_add(&ec->list, &grp->todo_list.list);
mutex_unlock(&work_mutex);
/* wake up someone else to handle the event */
wake_up(&grp->queue);
wake_up(&grp->poll_queue);
}
/**
* dazukofs_return_event - return checked file access results
* @group_id: id of the group the event came from
* @event_id: the id of the event
* @deny: a flag indicating if file access should be denied
*
* Description: This function is called by the device layer when returning
* results from a checked file access event. If the event_id was valid, the
* event container will be freed and the event released.
*
* Returns 0 on success.
*/
int dazukofs_return_event(unsigned long group_id, unsigned long event_id,
dazukofs_response_t response)
{
struct dazukofs_group *grp;
struct dazukofs_event_container *ec = NULL;
struct dazukofs_event *evt = NULL;
struct list_head *pos;
int found = 0;
int ret = 0;
mutex_lock(&work_mutex);
list_for_each(pos, &group_list.list) {
grp = list_entry(pos, struct dazukofs_group, list);
if (!grp->deprecated && grp->group_id == group_id) {
atomic_inc(&grp->use_count);
found = 1;
break;
}
}
if (!found) {
mutex_unlock(&work_mutex);
return -EINVAL;
}
found = 0;
list_for_each(pos, &grp->working_list.list) {
ec = list_entry(pos, struct dazukofs_event_container, list);
evt = ec->event;
if (evt->event_id == event_id) {
found = 1;
if (response != REPOST) {
list_del(pos);
kmem_cache_free(
dazukofs_event_container_cachep, ec);
}
break;
}
}
mutex_unlock(&work_mutex);
if (found) {
if (response == REPOST)
unclaim_event(grp, ec);
else
release_event(evt, 1, response == DENY);
} else {
ret = -EINVAL;
}
atomic_dec(&grp->use_count);
return ret;
}
/**
* claim_event - grab an event from the todo list
* @grp: the group
*
* Description: Take the first event from the todo list and move it to the
* working list. The event is then returned to its called for processing.
*
* Returns the claimed event.
*/
static struct dazukofs_event_container *claim_event(struct dazukofs_group *grp)
{
struct dazukofs_event_container *ec = NULL;
/* move first todo-item to working list */
mutex_lock(&work_mutex);
if (!list_empty(&grp->todo_list.list)) {
ec = list_first_entry(&grp->todo_list.list,
struct dazukofs_event_container, list);
list_del(&ec->list);
list_add(&ec->list, &grp->working_list.list);
}
mutex_unlock(&work_mutex);
return ec;
}
/**
* mask_proc - mask the current process
* @proc: process structure to use for the list
*
* Description: Assign the current process to the provided proc structure
* and add the structure to the list. The list is used to prevent
* generating recursive file access events. The process is removed from
* the list with the check_recursion() function.
*/
static void mask_proc(struct dazukofs_proc *proc)
{
proc->proc_id = get_pid(task_pid(current));
mutex_lock(&proc_mutex);
list_add(&proc->list, &proc_list.list);
proc->within_list = 1;
mutex_unlock(&proc_mutex);
}
/**
* open_file - open a file for the current process (avoiding recursion)
* @ec: event container to store opened file descriptor
*
* Description: This function will open a file using the information within
* the provided event container. The calling process will be temporarily
* masked so that the file open does not generate a file access event.
*
* Returns 0 on success.
*/
static int open_file(struct dazukofs_event_container *ec)
{
struct dazukofs_event *evt = ec->event;
struct dazukofs_proc proc;
int ret;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)
struct path path;
#endif