d54e7b56308dec9fd823f7ad5077d6b27b245339
[linux-2.6.git] / kernel / ckrm / ckrmutils.c
1 /* ckrmutils.c - Utility functions for CKRM
2  *
3  * Copyright (C) Chandra Seetharaman,  IBM Corp. 2003
4  *           (C) Hubertus Franke    ,  IBM Corp. 2004
5  * 
6  * Provides simple utility functions for the core module, CE and resource
7  * controllers.
8  *
9  * Latest version, more details at http://ckrm.sf.net
10  * 
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  */
17
18 /* Changes
19  * 
20  * 13 Nov 2003
21  *        Created
22  */
23
24 #include <linux/mm.h>
25 #include <linux/err.h>
26 #include <linux/mount.h>
27 #include <linux/module.h>
28 #include <linux/ckrm_rc.h>
29
30 int get_exe_path_name(struct task_struct *tsk, char *buf, int buflen)
31 {
32         struct vm_area_struct *vma;
33         struct vfsmount *mnt;
34         struct mm_struct *mm = get_task_mm(tsk);
35         struct dentry *dentry;
36         char *lname;
37         int rc = 0;
38
39         *buf = '\0';
40         if (!mm) {
41                 return -EINVAL;
42         }
43
44         down_read(&mm->mmap_sem);
45         vma = mm->mmap;
46         while (vma) {
47                 if ((vma->vm_flags & VM_EXECUTABLE) && vma->vm_file) {
48                         dentry = dget(vma->vm_file->f_dentry);
49                         mnt = mntget(vma->vm_file->f_vfsmnt);
50                         lname = d_path(dentry, mnt, buf, buflen);
51                         if (!IS_ERR(lname)) {
52                                 strncpy(buf, lname, strlen(lname) + 1);
53                         } else {
54                                 rc = (int)PTR_ERR(lname);
55                         }
56                         mntput(mnt);
57                         dput(dentry);
58                         break;
59                 }
60                 vma = vma->vm_next;
61         }
62         up_read(&mm->mmap_sem);
63         mmput(mm);
64         return rc;
65 }
66
67 /*
68  * must be called with cnt_lock of parres held
69  * Caller is responsible for making sure that the new guarantee doesn't
70  * overflow parent's total guarantee.
71  */
72 void child_guarantee_changed(struct ckrm_shares *parent, int cur, int new)
73 {
74         if (new == cur || !parent) {
75                 return;
76         }
77         if (new != CKRM_SHARE_DONTCARE) {
78                 parent->unused_guarantee -= new;
79         }
80         if (cur != CKRM_SHARE_DONTCARE) {
81                 parent->unused_guarantee += cur;
82         }
83         return;
84 }
85
86 /*
87  * must be called with cnt_lock of parres held
88  * Caller is responsible for making sure that the new limit is not more 
89  * than parent's max_limit
90  */
91 void child_maxlimit_changed(struct ckrm_shares *parent, int new_limit)
92 {
93         if (parent && parent->cur_max_limit < new_limit) {
94                 parent->cur_max_limit = new_limit;
95         }
96         return;
97 }
98
99 /*
100  * Caller is responsible for holding any lock to protect the data
101  * structures passed to this function
102  */
103 int
104 set_shares(struct ckrm_shares *new, struct ckrm_shares *cur,
105            struct ckrm_shares *par)
106 {
107         int rc = -EINVAL;
108         int cur_usage_guar = cur->total_guarantee - cur->unused_guarantee;
109         int increase_by = new->my_guarantee - cur->my_guarantee;
110
111         // Check total_guarantee for correctness
112         if (new->total_guarantee <= CKRM_SHARE_DONTCARE) {
113                 goto set_share_err;
114         } else if (new->total_guarantee == CKRM_SHARE_UNCHANGED) {
115                 ;               // do nothing
116         } else if (cur_usage_guar > new->total_guarantee) {
117                 goto set_share_err;
118         }
119         // Check max_limit for correctness
120         if (new->max_limit <= CKRM_SHARE_DONTCARE) {
121                 goto set_share_err;
122         } else if (new->max_limit == CKRM_SHARE_UNCHANGED) {
123                 ;               // do nothing
124         } else if (cur->cur_max_limit > new->max_limit) {
125                 goto set_share_err;
126         }
127         // Check my_guarantee for correctness
128         if (new->my_guarantee == CKRM_SHARE_UNCHANGED) {
129                 ;               // do nothing
130         } else if (new->my_guarantee == CKRM_SHARE_DONTCARE) {
131                 ;               // do nothing
132         } else if (par && increase_by > par->unused_guarantee) {
133                 goto set_share_err;
134         }
135         // Check my_limit for correctness
136         if (new->my_limit == CKRM_SHARE_UNCHANGED) {
137                 ;               // do nothing
138         } else if (new->my_limit == CKRM_SHARE_DONTCARE) {
139                 ;               // do nothing
140         } else if (par && new->my_limit > par->max_limit) {
141                 // I can't get more limit than my parent's limit
142                 goto set_share_err;
143
144         }
145         // make sure guarantee is lesser than limit
146         if (new->my_limit == CKRM_SHARE_DONTCARE) {
147                 ;               // do nothing
148         } else if (new->my_limit == CKRM_SHARE_UNCHANGED) {
149                 if (new->my_guarantee == CKRM_SHARE_DONTCARE) {
150                         ;       // do nothing
151                 } else if (new->my_guarantee == CKRM_SHARE_UNCHANGED) {
152                         ;       // do nothing earlier setting would've 
153                                 // taken care of it
154                 } else if (new->my_guarantee > cur->my_limit) {
155                         goto set_share_err;
156                 }
157         } else {                // new->my_limit has a valid value
158                 if (new->my_guarantee == CKRM_SHARE_DONTCARE) {
159                         ;       // do nothing
160                 } else if (new->my_guarantee == CKRM_SHARE_UNCHANGED) {
161                         if (cur->my_guarantee > new->my_limit) {
162                                 goto set_share_err;
163                         }
164                 } else if (new->my_guarantee > new->my_limit) {
165                         goto set_share_err;
166                 }
167         }
168
169         if (new->my_guarantee != CKRM_SHARE_UNCHANGED) {
170                 child_guarantee_changed(par, cur->my_guarantee,
171                                         new->my_guarantee);
172                 cur->my_guarantee = new->my_guarantee;
173         }
174
175         if (new->my_limit != CKRM_SHARE_UNCHANGED) {
176                 child_maxlimit_changed(par, new->my_limit);
177                 cur->my_limit = new->my_limit;
178         }
179
180         if (new->total_guarantee != CKRM_SHARE_UNCHANGED) {
181                 cur->unused_guarantee = new->total_guarantee - cur_usage_guar;
182                 cur->total_guarantee = new->total_guarantee;
183         }
184
185         if (new->max_limit != CKRM_SHARE_UNCHANGED) {
186                 cur->max_limit = new->max_limit;
187         }
188
189         rc = 0;
190       set_share_err:
191         return rc;
192 }
193
194 EXPORT_SYMBOL(get_exe_path_name);
195 EXPORT_SYMBOL(child_guarantee_changed);
196 EXPORT_SYMBOL(child_maxlimit_changed);
197 EXPORT_SYMBOL(set_shares);