1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=4:tabstop=4:
4 * Copyright (c) 2004 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.lustre.org.
8 * Lustre is free software; you can redistribute it and/or modify it under
9 * the terms of version 2 of the GNU General Public License as published by
10 * the Free Software Foundation. Lustre is distributed in the hope that it
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details. You should have received a
14 * copy of the GNU General Public License along with Lustre; if not, write
15 * to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
18 * Impletion of winnt curproc routines.
21 #define DEBUG_SUBSYSTEM S_LNET
23 #include <libcfs/libcfs.h>
27 * Implementation of cfs_curproc API (see portals/include/libcfs/curproc.h)
31 cfs_task_t this_task =
32 { 0, 0, 0, 0, 0, 0, 0,
33 0, 0, 0, 0, 1, 0, 0, 0, 0,
37 uid_t cfs_curproc_uid(void)
42 gid_t cfs_curproc_gid(void)
47 uid_t cfs_curproc_fsuid(void)
49 return this_task.fsuid;
52 gid_t cfs_curproc_fsgid(void)
54 return this_task.fsgid;
57 pid_t cfs_curproc_pid(void)
59 return cfs_current()->pid;
62 int cfs_curproc_groups_nr(void)
64 return this_task.ngroups;
67 void cfs_curproc_groups_dump(gid_t *array, int size)
69 LASSERT(size <= NGROUPS);
70 size = min_t(int, size, this_task.ngroups);
71 memcpy(array, this_task.groups, size * sizeof(__u32));
74 int cfs_curproc_is_in_groups(gid_t gid)
76 return in_group_p(gid);
79 mode_t cfs_curproc_umask(void)
81 return this_task.umask;
84 char *cfs_curproc_comm(void)
86 return this_task.comm;
89 cfs_kernel_cap_t cfs_curproc_cap_get(void)
91 return this_task.cap_effective;
94 void cfs_curproc_cap_set(cfs_kernel_cap_t cap)
96 this_task.cap_effective = cap;
101 * Implementation of linux task management routines
105 /* global of the task manager structure */
117 PTASK_SLOT task = NULL;
120 task = cfs_mem_cache_alloc(TaskMan.slab, 0);
122 task = cfs_alloc(sizeof(TASK_SLOT), 0);
129 init_task_slot(PTASK_SLOT task)
131 memset(task, 0, sizeof(TASK_SLOT));
132 task->Magic = TASKSLT_MAGIC;
133 task->task = this_task;
134 task->task.pid = (pid_t)PsGetCurrentThreadId();
135 cfs_init_event(&task->Event, TRUE, FALSE);
140 cleanup_task_slot(PTASK_SLOT task)
143 cfs_mem_cache_free(TaskMan.slab, task);
150 * task manager related routines
160 PLIST_ENTRY ListEntry = NULL;
161 PTASK_SLOT TaskSlot = NULL;
163 spin_lock(&(TaskMan.Lock));
165 ListEntry = TaskMan.TaskList.Flink;
167 while (ListEntry != (&(TaskMan.TaskList))) {
169 TaskSlot = CONTAINING_RECORD(ListEntry, TASK_SLOT, Link);
171 if (TaskSlot->Pid == ProcessId && TaskSlot->Tid == ThreadId) {
175 DbgPrint("task_manager_notify: Pid=%xh Tid %xh resued (TaskSlot->Tet = %xh)...\n",
176 ProcessId, ThreadId, TaskSlot->Tet);
179 /* remove the taskslot */
180 RemoveEntryList(&(TaskSlot->Link));
181 TaskMan.NumOfTasks--;
183 /* now free the task slot */
184 cleanup_task_slot(TaskSlot);
188 ListEntry = ListEntry->Flink;
191 spin_unlock(&(TaskMan.Lock));
199 /* initialize the content and magic */
200 memset(&TaskMan, 0, sizeof(TASK_MAN));
201 TaskMan.Magic = TASKMAN_MAGIC;
203 /* initialize the spinlock protection */
204 spin_lock_init(&TaskMan.Lock);
206 /* create slab memory cache */
207 TaskMan.slab = cfs_mem_cache_create(
208 "TSLT", sizeof(TASK_SLOT), 0, 0);
210 /* intialize the list header */
211 InitializeListHead(&(TaskMan.TaskList));
213 /* set the thread creation/destruction notify routine */
214 status = PsSetCreateThreadNotifyRoutine(task_manager_notify);
216 if (!NT_SUCCESS(status)) {
217 cfs_enter_debugger();
224 cleanup_task_manager()
226 PLIST_ENTRY ListEntry = NULL;
227 PTASK_SLOT TaskSlot = NULL;
229 /* we must stay in system since we succeed to register the
230 CreateThreadNotifyRoutine: task_manager_notify */
231 cfs_enter_debugger();
234 /* cleanup all the taskslots attached to the list */
235 spin_lock(&(TaskMan.Lock));
237 while (!IsListEmpty(&(TaskMan.TaskList))) {
239 ListEntry = TaskMan.TaskList.Flink;
240 TaskSlot = CONTAINING_RECORD(ListEntry, TASK_SLOT, Link);
242 RemoveEntryList(ListEntry);
243 cleanup_task_slot(TaskSlot);
246 spin_unlock(&TaskMan.Lock);
248 /* destroy the taskslot cache slab */
249 cfs_mem_cache_destroy(TaskMan.slab);
250 memset(&TaskMan, 0, sizeof(TASK_MAN));
255 * schedule routines (task slot list)
262 HANDLE Pid = PsGetCurrentProcessId();
263 HANDLE Tid = PsGetCurrentThreadId();
264 PETHREAD Tet = PsGetCurrentThread();
266 PLIST_ENTRY ListEntry = NULL;
267 PTASK_SLOT TaskSlot = NULL;
269 spin_lock(&(TaskMan.Lock));
271 ListEntry = TaskMan.TaskList.Flink;
273 while (ListEntry != (&(TaskMan.TaskList))) {
275 TaskSlot = CONTAINING_RECORD(ListEntry, TASK_SLOT, Link);
277 if (TaskSlot->Pid == Pid && TaskSlot->Tid == Tid) {
278 if (TaskSlot->Tet != Tet) {
281 DbgPrint("cfs_current: Pid=%xh Tid %xh Tet = %xh resued (TaskSlot->Tet = %xh)...\n",
282 Pid, Tid, Tet, TaskSlot->Tet);
285 // The old thread was already exit. This must be a
286 // new thread which get the same Tid to the previous.
295 if ((ULONG)TaskSlot->Pid > (ULONG)Pid) {
298 } else if ((ULONG)TaskSlot->Pid == (ULONG)Pid) {
299 if ((ULONG)TaskSlot->Tid > (ULONG)Tid) {
308 ListEntry = ListEntry->Flink;
313 TaskSlot = alloc_task_slot();
316 cfs_enter_debugger();
320 init_task_slot(TaskSlot);
326 if (ListEntry == (&(TaskMan.TaskList))) {
328 // Empty case or the biggest case, put it to the tail.
330 InsertTailList(&(TaskMan.TaskList), &(TaskSlot->Link));
333 // Get a slot and smaller than it's tid, put it just before.
335 InsertHeadList(ListEntry->Blink, &(TaskSlot->Link));
338 TaskMan.NumOfTasks++;
342 // To Check whether he task structures are arranged in the expected order ?
346 PTASK_SLOT Prev = NULL, Curr = NULL;
348 ListEntry = TaskMan.TaskList.Flink;
350 while (ListEntry != (&(TaskMan.TaskList))) {
352 Curr = CONTAINING_RECORD(ListEntry, TASK_SLOT, Link);
353 ListEntry = ListEntry->Flink;
356 if ((ULONG)Prev->Pid > (ULONG)Curr->Pid) {
357 cfs_enter_debugger();
358 } else if ((ULONG)Prev->Pid == (ULONG)Curr->Pid) {
359 if ((ULONG)Prev->Tid > (ULONG)Curr->Tid) {
360 cfs_enter_debugger();
371 spin_unlock(&(TaskMan.Lock));
374 cfs_enter_debugger();
378 return (&(TaskSlot->task));
382 schedule_timeout(int64_t time)
384 cfs_task_t * task = cfs_current();
385 PTASK_SLOT slot = NULL;
388 cfs_enter_debugger();
392 slot = CONTAINING_RECORD(task, TASK_SLOT, task);
393 cfs_assert(slot->Magic == TASKSLT_MAGIC);
395 if (time == MAX_SCHEDULE_TIMEOUT) {
399 return (cfs_wait_event(&(slot->Event), time) != 0);
405 return schedule_timeout(0);
413 PTASK_SLOT slot = NULL;
416 cfs_enter_debugger();
420 slot = CONTAINING_RECORD(task, TASK_SLOT, task);
421 cfs_assert(slot->Magic == TASKSLT_MAGIC);
423 cfs_wake_event(&(slot->Event));
435 cfs_waitlink_init(&link);
436 cfs_waitq_add(waitq, &link);
437 cfs_waitq_wait(&link, CFS_TASK_INTERRUPTIBLE);
438 cfs_waitq_del(waitq, &link);
441 EXPORT_SYMBOL(cfs_curproc_uid);
442 EXPORT_SYMBOL(cfs_curproc_pid);
443 EXPORT_SYMBOL(cfs_curproc_gid);
444 EXPORT_SYMBOL(cfs_curproc_fsuid);
445 EXPORT_SYMBOL(cfs_curproc_fsgid);
446 EXPORT_SYMBOL(cfs_curproc_umask);
447 EXPORT_SYMBOL(cfs_curproc_comm);
448 EXPORT_SYMBOL(cfs_curproc_groups_nr);
449 EXPORT_SYMBOL(cfs_curproc_groups_dump);
450 EXPORT_SYMBOL(cfs_curproc_is_in_groups);
451 EXPORT_SYMBOL(cfs_curproc_cap_get);
452 EXPORT_SYMBOL(cfs_curproc_cap_set);