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