Whamcloud - gitweb
e59719e87e093be07f6ef6b48f6c1d7bb733648c
[fs/lustre-release.git] / libcfs / libcfs / winnt / winnt-curproc.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * libcfs/libcfs/winnt/winnt-curproc.c
37  *
38  * Impletion of winnt curproc routines.
39  */
40
41 #define DEBUG_SUBSYSTEM S_LNET
42
43 #include <libcfs/libcfs.h>
44
45 /*
46  * Implementation of cfs_curproc API (see portals/include/libcfs/curproc.h)
47  * for Linux kernel.
48  */
49
50 cfs_task_t this_task =
51     { /* umask */ 0,/* blocked*/0, /* pid */ 0, /* pgrp */ 0,
52       /* uid,euid,suid,fsuid */  0, 0, 0, 0, 
53       /* gid_t gid,egid,sgid,fsgid */ 0, 0, 0, 0,
54       /* ngroups*/ 1, /*cgroups*/ 0, /*groups*/ 0,
55       /* group_info */ NULL,
56       /* cap_effective, cap_inheritable, cap_permitted */  0, 0, 0,
57       /* comm */"sysetm\0",
58       /* journal_info */ NULL
59     };
60
61
62 uid_t  cfs_curproc_uid(void)
63 {
64     return this_task.uid;
65 }
66
67 gid_t  cfs_curproc_gid(void)
68 {
69     return this_task.gid;
70 }
71
72 uid_t  cfs_curproc_fsuid(void)
73 {
74     return this_task.fsuid;
75 }
76
77 gid_t cfs_curproc_fsgid(void)
78 {
79     return this_task.fsgid;
80 }
81
82 pid_t cfs_curproc_pid(void)
83 {
84     return cfs_current()->pid;
85 }
86
87 int cfs_curproc_groups_nr(void)
88 {
89     return this_task.ngroups;
90 }
91
92 void cfs_curproc_groups_dump(gid_t *array, int size)
93 {
94     LASSERT(size <= NGROUPS);
95     size = min_t(int, size, this_task.ngroups);
96     memcpy(array, this_task.groups, size * sizeof(__u32));
97 }
98
99 int cfs_curproc_is_in_groups(gid_t gid)
100 {
101     return in_group_p(gid);
102 }
103
104 mode_t cfs_curproc_umask(void)
105 {
106     return this_task.umask;
107 }
108
109 char  *cfs_curproc_comm(void)
110 {
111     return this_task.comm;
112 }
113
114 void cfs_cap_raise(cfs_cap_t cap)
115 {
116         this_task.cap_effective |= (1 << cap);
117 }
118
119 void cfs_cap_lower(cfs_cap_t cap)
120 {
121         this_task.cap_effective &= ~(1 << cap);
122 }
123
124 int cfs_cap_raised(cfs_cap_t cap)
125 {
126         return this_task.cap_effective & (1 << cap);
127 }
128
129 cfs_cap_t cfs_curproc_cap_pack(void) {
130         return this_task.cap_effective;
131 }
132
133 void cfs_curproc_cap_unpack(cfs_cap_t cap) {
134         this_task.cap_effective = cap;
135 }
136
137 int cfs_capable(cfs_cap_t cap)
138 {
139         return TRUE;
140 }
141
142 /*
143  * Implementation of linux task management routines
144  */
145
146
147 /* global of the task manager structure */
148
149 TASK_MAN cfs_win_task_manger;
150
151 /* global idr context */
152 struct idr_context * cfs_win_task_slot_idp = NULL;
153
154 /*
155  *  task slot routiens
156  */
157
158 PTASK_SLOT alloc_task_slot()
159 {
160         if (cfs_win_task_manger.slab)
161                 return kmem_cache_alloc(cfs_win_task_manger.slab, 0);
162         else
163                 return kmalloc(sizeof(TASK_SLOT), 0);
164 }
165
166 void
167 init_task_slot(PTASK_SLOT task)
168 {
169     memset(task, 0, sizeof(TASK_SLOT));
170     task->Magic = TASKSLT_MAGIC;
171     task->task  = this_task;
172     cfs_init_event(&task->Event, TRUE, FALSE);
173 }
174
175 void cleanup_task_slot(PTASK_SLOT task)
176 {
177         if (task->task.pid)
178                 cfs_idr_remove(cfs_win_task_slot_idp, task->task.pid);
179
180         if (cfs_win_task_manger.slab)
181                 kmem_cache_free(cfs_win_task_manger.slab, task);
182         else
183                 kfree(task);
184 }
185
186 /*
187  *  task manager related routines
188  */
189
190 VOID
191 task_manager_notify(
192     IN HANDLE   ProcessId,
193     IN HANDLE   ThreadId,
194     IN BOOLEAN  Create
195     )
196 {
197     PLIST_ENTRY ListEntry = NULL; 
198     PTASK_SLOT  TaskSlot  = NULL;
199
200         spin_lock(&(cfs_win_task_manger.Lock));
201
202     ListEntry = cfs_win_task_manger.TaskList.Flink;
203     while (ListEntry != (&(cfs_win_task_manger.TaskList))) {
204
205         TaskSlot = CONTAINING_RECORD(ListEntry, TASK_SLOT, Link);
206
207         if (TaskSlot->Pid == ProcessId && TaskSlot->Tid == ThreadId) {
208
209             if (!Create) {
210                 /* remove the taskslot */
211                 RemoveEntryList(&(TaskSlot->Link));
212                 cfs_win_task_manger.NumOfTasks--;
213
214                 /* now free the task slot */
215                 cleanup_task_slot(TaskSlot);
216             }
217         }
218
219         ListEntry = ListEntry->Flink;
220     }
221
222         spin_unlock(&(cfs_win_task_manger.Lock));
223 }
224
225 int
226 init_task_manager()
227 {
228     NTSTATUS    status;
229
230     /* initialize the content and magic */
231     memset(&cfs_win_task_manger, 0, sizeof(TASK_MAN));
232     cfs_win_task_manger.Magic = TASKMAN_MAGIC;
233
234     /* initialize the spinlock protection */
235         spin_lock_init(&cfs_win_task_manger.Lock);
236
237         /* create slab memory cache */
238         cfs_win_task_manger.slab = kmem_cache_create("TSLT", sizeof(TASK_SLOT),
239                                                      0, 0, NULL);
240
241     /* intialize the list header */
242     InitializeListHead(&(cfs_win_task_manger.TaskList));
243
244     cfs_win_task_slot_idp = cfs_idr_init();
245     if (!cfs_win_task_slot_idp) {
246         return -ENOMEM;
247     }
248
249     /* set the thread creation/destruction notify routine */
250     status = PsSetCreateThreadNotifyRoutine(task_manager_notify);
251
252     if (!NT_SUCCESS(status)) {
253         cfs_enter_debugger();
254         /* remove idr context */
255         if (cfs_win_task_slot_idp) {
256             cfs_idr_exit(cfs_win_task_slot_idp);
257             cfs_win_task_slot_idp = NULL;
258         }
259         return cfs_error_code(status);
260     }
261
262     return 0;
263 }
264
265 void
266 cleanup_task_manager()
267 {
268     PLIST_ENTRY ListEntry = NULL; 
269     PTASK_SLOT  TaskSlot  = NULL;
270
271     /* remove ThreadNotifyRoutine: task_manager_notify */
272     PsRemoveCreateThreadNotifyRoutine(task_manager_notify);
273
274     /* remove idr context */
275     if (cfs_win_task_slot_idp) {
276         cfs_idr_exit(cfs_win_task_slot_idp);
277         cfs_win_task_slot_idp = NULL;
278     }
279
280     /* cleanup all the taskslots attached to the list */
281         spin_lock(&(cfs_win_task_manger.Lock));
282
283     while (!IsListEmpty(&(cfs_win_task_manger.TaskList))) {
284
285         ListEntry = cfs_win_task_manger.TaskList.Flink;
286         TaskSlot = CONTAINING_RECORD(ListEntry, TASK_SLOT, Link);
287
288         RemoveEntryList(ListEntry);
289         cleanup_task_slot(TaskSlot);
290     }
291
292         spin_unlock(&cfs_win_task_manger.Lock);
293
294         /* destroy the taskslot cache slab */
295         kmem_cache_destroy(cfs_win_task_manger.slab);
296         memset(&cfs_win_task_manger, 0, sizeof(TASK_MAN));
297 }
298
299
300 /*
301  * schedule routines (task slot list)
302  */
303
304
305 cfs_task_t *
306 cfs_current()
307 {
308     HANDLE      Pid = PsGetCurrentProcessId();
309     HANDLE      Tid = PsGetCurrentThreadId();
310     PETHREAD    Tet = PsGetCurrentThread();
311
312     PLIST_ENTRY ListEntry = NULL; 
313     PTASK_SLOT  TaskSlot  = NULL;
314
315         spin_lock(&(cfs_win_task_manger.Lock));
316
317     ListEntry = cfs_win_task_manger.TaskList.Flink;
318     while (ListEntry != (&(cfs_win_task_manger.TaskList))) {
319
320         TaskSlot = CONTAINING_RECORD(ListEntry, TASK_SLOT, Link);
321         if (TaskSlot->Pid == Pid && TaskSlot->Tid == Tid) {
322             if (TaskSlot->Tet != Tet) {
323
324                 //
325                 // The old thread was already exit. This must be a
326                 // new thread which get the same Tid to the previous.
327                 //
328
329                 TaskSlot->Tet = Tet;
330             }
331             break;
332
333         } else {
334
335             if (TaskSlot->Pid > Pid) {
336                 TaskSlot = NULL;
337                 break;
338             } else if (TaskSlot->Pid == Pid) {
339                 if (TaskSlot->Tid > Tid) {
340                     TaskSlot = NULL;
341                     break;
342                 }
343             }
344             TaskSlot =  NULL;
345         }
346
347         ListEntry = ListEntry->Flink;
348     }
349
350     if (!TaskSlot) {
351
352         /* allocate new task slot */
353         TaskSlot = alloc_task_slot();
354         if (!TaskSlot) {
355             cfs_enter_debugger();
356             goto errorout;
357         }
358
359         /* set task slot IDs */
360         init_task_slot(TaskSlot);
361         TaskSlot->Pid = Pid;
362         TaskSlot->Tid = Tid;
363         TaskSlot->Tet = Tet;
364         TaskSlot->task.pid = (pid_t)cfs_idr_get_new(cfs_win_task_slot_idp, Tet);
365
366         if (ListEntry == (&(cfs_win_task_manger.TaskList))) {
367             //
368             // Empty case or the biggest case, put it to the tail.
369             //
370             InsertTailList(&(cfs_win_task_manger.TaskList), &(TaskSlot->Link));
371         } else {
372             //
373             // Get a slot and smaller than it's tid, put it just before.
374             //
375             InsertHeadList(ListEntry->Blink, &(TaskSlot->Link));
376         }
377
378         cfs_win_task_manger.NumOfTasks++;
379     }
380
381     //
382     // To Check whether he task structures are arranged in the expected order ?
383     //
384
385     {
386         PTASK_SLOT  Prev = NULL, Curr = NULL;
387         
388         ListEntry = cfs_win_task_manger.TaskList.Flink;
389
390         while (ListEntry != (&(cfs_win_task_manger.TaskList))) {
391
392             Curr = CONTAINING_RECORD(ListEntry, TASK_SLOT, Link);
393             ListEntry = ListEntry->Flink;
394
395             if (Prev) {
396                 if (Prev->Pid > Curr->Pid) {
397                     cfs_enter_debugger();
398                 } else if (Prev->Pid == Curr->Pid) {
399                     if (Prev->Tid > Curr->Tid) {
400                         cfs_enter_debugger();
401                     }
402                 }
403             }
404
405             Prev = Curr;
406         }
407     }
408
409 errorout:
410
411         spin_unlock(&(cfs_win_task_manger.Lock));
412
413     if (!TaskSlot) {
414         cfs_enter_debugger();
415         return NULL;
416     }
417
418     return (&(TaskSlot->task));
419 }
420
421 /* deschedule for a bit... */
422 void
423 cfs_pause(cfs_duration_t ticks)
424 {
425     cfs_schedule_timeout_and_set_state(CFS_TASK_UNINTERRUPTIBLE, ticks);
426 }
427
428 void
429 cfs_schedule_timeout_and_set_state(cfs_task_state_t state, int64_t time)
430 {
431     cfs_task_t * task = cfs_current();
432     PTASK_SLOT   slot = NULL;
433
434     if (!task) {
435         cfs_enter_debugger();
436         return;
437     }
438
439     slot = CONTAINING_RECORD(task, TASK_SLOT, task);
440     cfs_assert(slot->Magic == TASKSLT_MAGIC);
441
442     if (time == CFS_MAX_SCHEDULE_TIMEOUT) {
443         time = 0;
444     }
445
446     cfs_wait_event_internal(&(slot->Event), time);
447 }
448
449 void
450 cfs_schedule()
451 {
452     cfs_schedule_timeout_and_set_state(CFS_TASK_UNINTERRUPTIBLE, 0);
453 }
454
455 int
456 wake_up_process(
457     cfs_task_t * task
458     )
459 {
460     PTASK_SLOT   slot = NULL;
461
462     if (!task) {
463         cfs_enter_debugger();
464         return 0;
465     }
466
467     slot = CONTAINING_RECORD(task, TASK_SLOT, task);
468     cfs_assert(slot->Magic == TASKSLT_MAGIC);
469
470     cfs_wake_event(&(slot->Event));
471
472     return TRUE;
473 }
474
475 void
476 sleep_on(cfs_waitq_t *waitq)
477 {
478         cfs_waitlink_t link;
479         
480         cfs_waitlink_init(&link);
481         cfs_waitq_add(waitq, &link);
482         cfs_waitq_wait(&link, CFS_TASK_INTERRUPTIBLE);
483         cfs_waitq_del(waitq, &link);
484 }
485
486 EXPORT_SYMBOL(cfs_curproc_uid);
487 EXPORT_SYMBOL(cfs_curproc_pid);
488 EXPORT_SYMBOL(cfs_curproc_gid);
489 EXPORT_SYMBOL(cfs_curproc_fsuid);
490 EXPORT_SYMBOL(cfs_curproc_fsgid);
491 EXPORT_SYMBOL(cfs_curproc_umask);
492 EXPORT_SYMBOL(cfs_curproc_comm);
493 EXPORT_SYMBOL(cfs_curproc_groups_nr);
494 EXPORT_SYMBOL(cfs_curproc_groups_dump);
495 EXPORT_SYMBOL(cfs_curproc_is_in_groups);
496 EXPORT_SYMBOL(cfs_cap_raise);
497 EXPORT_SYMBOL(cfs_cap_lower);
498 EXPORT_SYMBOL(cfs_cap_raised);
499 EXPORT_SYMBOL(cfs_curproc_cap_pack);
500 EXPORT_SYMBOL(cfs_curproc_cap_unpack);
501 EXPORT_SYMBOL(cfs_capable);