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