1 |
/* |
2 |
* fs/realpath.c |
3 |
* |
4 |
* Get the canonicalized absolute pathnames. The basis for SAKURA and TOMOYO. |
5 |
* |
6 |
* Copyright (C) 2005-2008 NTT DATA CORPORATION |
7 |
* |
8 |
* Version: 1.6.3+ 2008/08/22 |
9 |
* |
10 |
* This file is applicable to both 2.4.30 and 2.6.11 and later. |
11 |
* See README.ccs for ChangeLog. |
12 |
* |
13 |
*/ |
14 |
#include <linux/string.h> |
15 |
#include <linux/mm.h> |
16 |
#include <linux/utime.h> |
17 |
#include <linux/file.h> |
18 |
#include <linux/smp_lock.h> |
19 |
#include <linux/module.h> |
20 |
#include <linux/slab.h> |
21 |
#include <asm/uaccess.h> |
22 |
#include <asm/atomic.h> |
23 |
#include <linux/version.h> |
24 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0) |
25 |
#include <linux/namei.h> |
26 |
#include <linux/mount.h> |
27 |
static const int lookup_flags = LOOKUP_FOLLOW; |
28 |
#else |
29 |
static const int lookup_flags = LOOKUP_FOLLOW | LOOKUP_POSITIVE; |
30 |
#endif |
31 |
#include <linux/realpath.h> |
32 |
#include <linux/proc_fs.h> |
33 |
#include <linux/ccs_common.h> |
34 |
|
35 |
/** |
36 |
* get_absolute_path - Get the path of a dentry but ignores chroot'ed root. |
37 |
* |
38 |
* @dentry: Pointer to "struct dentry". |
39 |
* @vfsmnt: Pointer to "struct vfsmount". |
40 |
* @buffer: Pointer to buffer to return value in. |
41 |
* @buflen: Sizeof @buffer. |
42 |
* |
43 |
* Returns 0 on success, -ENOMEM otherwise. |
44 |
* |
45 |
* Caller holds the dcache_lock and vfsmount_lock. |
46 |
* Based on __d_path() in fs/dcache.c |
47 |
* |
48 |
* If dentry is a directory, trailing '/' is appended. |
49 |
* Characters out of 0x20 < c < 0x7F range are converted to |
50 |
* \ooo style octal string. |
51 |
* Character \ is converted to \\ string. |
52 |
*/ |
53 |
static int get_absolute_path(struct dentry *dentry, struct vfsmount *vfsmnt, |
54 |
char *buffer, int buflen) |
55 |
{ |
56 |
/***** CRITICAL SECTION START *****/ |
57 |
char *start = buffer; |
58 |
char *end = buffer + buflen; |
59 |
bool is_dir = (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)); |
60 |
|
61 |
if (buflen < 256) |
62 |
goto out; |
63 |
|
64 |
*--end = '\0'; |
65 |
buflen--; |
66 |
|
67 |
for (;;) { |
68 |
struct dentry *parent; |
69 |
|
70 |
if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) { |
71 |
/* Global root? */ |
72 |
if (vfsmnt->mnt_parent == vfsmnt) |
73 |
break; |
74 |
dentry = vfsmnt->mnt_mountpoint; |
75 |
vfsmnt = vfsmnt->mnt_parent; |
76 |
continue; |
77 |
} |
78 |
if (is_dir) { |
79 |
is_dir = false; |
80 |
*--end = '/'; |
81 |
buflen--; |
82 |
} |
83 |
parent = dentry->d_parent; |
84 |
{ |
85 |
const char *sp = dentry->d_name.name; |
86 |
const char *cp = sp + dentry->d_name.len - 1; |
87 |
unsigned char c; |
88 |
|
89 |
/* |
90 |
* Exception: Use /proc/self/ rather than |
91 |
* /proc/\$/ for current process. |
92 |
*/ |
93 |
if (IS_ROOT(parent) && *sp > '0' && *sp <= '9' && |
94 |
parent->d_sb && |
95 |
parent->d_sb->s_magic == PROC_SUPER_MAGIC) { |
96 |
char *ep; |
97 |
const pid_t pid |
98 |
= (pid_t) simple_strtoul(sp, &ep, 10); |
99 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0) |
100 |
if (!*ep && pid == current->tgid) { |
101 |
sp = "self"; |
102 |
cp = sp + 3; |
103 |
} |
104 |
#else |
105 |
if (!*ep && pid == current->pid) { |
106 |
sp = "self"; |
107 |
cp = sp + 3; |
108 |
} |
109 |
#endif |
110 |
} |
111 |
|
112 |
while (sp <= cp) { |
113 |
c = *(unsigned char *) cp; |
114 |
if (c == '\\') { |
115 |
buflen -= 2; |
116 |
if (buflen < 0) |
117 |
goto out; |
118 |
*--end = '\\'; |
119 |
*--end = '\\'; |
120 |
} else if (c > ' ' && c < 127) { |
121 |
if (--buflen < 0) |
122 |
goto out; |
123 |
*--end = (char) c; |
124 |
} else { |
125 |
buflen -= 4; |
126 |
if (buflen < 0) |
127 |
goto out; |
128 |
*--end = (c & 7) + '0'; |
129 |
*--end = ((c >> 3) & 7) + '0'; |
130 |
*--end = (c >> 6) + '0'; |
131 |
*--end = '\\'; |
132 |
} |
133 |
cp--; |
134 |
} |
135 |
if (--buflen < 0) |
136 |
goto out; |
137 |
*--end = '/'; |
138 |
} |
139 |
dentry = parent; |
140 |
} |
141 |
if (*end == '/') { |
142 |
buflen++; |
143 |
end++; |
144 |
} |
145 |
{ |
146 |
const char *sp = dentry->d_name.name; |
147 |
const char *cp = sp + dentry->d_name.len - 1; |
148 |
unsigned char c; |
149 |
while (sp <= cp) { |
150 |
c = *(unsigned char *) cp; |
151 |
if (c == '\\') { |
152 |
buflen -= 2; |
153 |
if (buflen < 0) |
154 |
goto out; |
155 |
*--end = '\\'; |
156 |
*--end = '\\'; |
157 |
} else if (c > ' ' && c < 127) { |
158 |
if (--buflen < 0) |
159 |
goto out; |
160 |
*--end = (char) c; |
161 |
} else { |
162 |
buflen -= 4; |
163 |
if (buflen < 0) |
164 |
goto out; |
165 |
*--end = (c & 7) + '0'; |
166 |
*--end = ((c >> 3) & 7) + '0'; |
167 |
*--end = (c >> 6) + '0'; |
168 |
*--end = '\\'; |
169 |
} |
170 |
cp--; |
171 |
} |
172 |
} |
173 |
/* Move the pathname to the top of the buffer. */ |
174 |
memmove(start, end, strlen(end) + 1); |
175 |
return 0; |
176 |
out: |
177 |
return -ENOMEM; |
178 |
/***** CRITICAL SECTION END *****/ |
179 |
} |
180 |
|
181 |
/** |
182 |
* ccs_realpath_from_dentry2 - Returns realpath(3) of the given dentry but ignores chroot'ed root. |
183 |
* |
184 |
* @dentry: Pointer to "struct dentry". |
185 |
* @mnt: Pointer to "struct vfsmount". |
186 |
* @newname: Pointer to buffer to return value in. |
187 |
* @newname_len: Size of @newname. |
188 |
* |
189 |
* Returns 0 on success, negative value otherwise. |
190 |
*/ |
191 |
int ccs_realpath_from_dentry2(struct dentry *dentry, struct vfsmount *mnt, |
192 |
char *newname, int newname_len) |
193 |
{ |
194 |
int error = -EINVAL; |
195 |
struct dentry *d_dentry; |
196 |
struct vfsmount *d_mnt; |
197 |
if (!dentry || !newname || newname_len <= 2048) |
198 |
goto out; |
199 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 22) |
200 |
if (dentry->d_op && dentry->d_op->d_dname) { |
201 |
/* For "socket:[\$]" and "pipe:[\$]". */ |
202 |
static const int offset = 1536; |
203 |
char *dp = newname; |
204 |
char *sp = dentry->d_op->d_dname(dentry, newname + offset, |
205 |
newname_len - offset); |
206 |
if (IS_ERR(sp)) { |
207 |
error = PTR_ERR(sp); |
208 |
goto out; |
209 |
} |
210 |
error = -ENOMEM; |
211 |
newname += offset; |
212 |
while (1) { |
213 |
const unsigned char c = *(unsigned char *) sp++; |
214 |
if (c == '\\') { |
215 |
if (dp + 2 >= newname) |
216 |
break; |
217 |
*dp++ = '\\'; |
218 |
*dp++ = '\\'; |
219 |
} else if (c > ' ' && c < 127) { |
220 |
if (dp + 1 >= newname) |
221 |
break; |
222 |
*dp++ = (char) c; |
223 |
} else if (c) { |
224 |
if (dp + 4 >= newname) |
225 |
break; |
226 |
*dp++ = '\\'; |
227 |
*dp++ = (c >> 6) + '0'; |
228 |
*dp++ = ((c >> 3) & 7) + '0'; |
229 |
*dp++ = (c & 7) + '0'; |
230 |
} else { |
231 |
*dp = '\0'; |
232 |
return 0; |
233 |
} |
234 |
} |
235 |
goto out; |
236 |
} |
237 |
#endif |
238 |
if (!mnt) |
239 |
goto out; |
240 |
d_dentry = dget(dentry); |
241 |
d_mnt = mntget(mnt); |
242 |
/***** CRITICAL SECTION START *****/ |
243 |
ccs_realpath_lock(); |
244 |
error = get_absolute_path(d_dentry, d_mnt, newname, newname_len); |
245 |
ccs_realpath_unlock(); |
246 |
/***** CRITICAL SECTION END *****/ |
247 |
dput(d_dentry); |
248 |
mntput(d_mnt); |
249 |
out: |
250 |
if (error) |
251 |
printk(KERN_WARNING "ccs_realpath: Pathname too long. (%d)\n", |
252 |
error); |
253 |
return error; |
254 |
} |
255 |
|
256 |
/** |
257 |
* ccs_realpath_from_dentry - Returns realpath(3) of the given pathname but ignores chroot'ed root. |
258 |
* |
259 |
* @dentry: Pointer to "struct dentry". |
260 |
* @mnt: Pointer to "struct vfsmount". |
261 |
* |
262 |
* Returns the realpath of the given @dentry and @mnt on success, |
263 |
* NULL otherwise. |
264 |
* |
265 |
* These functions use ccs_alloc(), so caller must ccs_free() |
266 |
* if these functions didn't return NULL. |
267 |
*/ |
268 |
char *ccs_realpath_from_dentry(struct dentry *dentry, struct vfsmount *mnt) |
269 |
{ |
270 |
char *buf = ccs_alloc(sizeof(struct ccs_page_buffer)); |
271 |
if (buf && ccs_realpath_from_dentry2(dentry, mnt, buf, |
272 |
CCS_MAX_PATHNAME_LEN - 1) == 0) |
273 |
return buf; |
274 |
ccs_free(buf); |
275 |
return NULL; |
276 |
} |
277 |
|
278 |
/** |
279 |
* ccs_realpath - Get realpath of a pathname. |
280 |
* |
281 |
* @pathname: The pathname to solve. |
282 |
* |
283 |
* Returns the realpath of @pathname on success, NULL otherwise. |
284 |
*/ |
285 |
char *ccs_realpath(const char *pathname) |
286 |
{ |
287 |
struct nameidata nd; |
288 |
if (pathname && path_lookup(pathname, lookup_flags, &nd) == 0) { |
289 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25) |
290 |
char *buf = ccs_realpath_from_dentry(nd.path.dentry, |
291 |
nd.path.mnt); |
292 |
path_put(&nd.path); |
293 |
#else |
294 |
char *buf = ccs_realpath_from_dentry(nd.dentry, nd.mnt); |
295 |
path_release(&nd); |
296 |
#endif |
297 |
return buf; |
298 |
} |
299 |
return NULL; |
300 |
} |
301 |
|
302 |
/** |
303 |
* ccs_realpath_nofollow - Get realpath of a pathname. |
304 |
* |
305 |
* @pathname: The pathname to solve. |
306 |
* |
307 |
* Returns the realpath of @pathname on success, NULL otherwise. |
308 |
*/ |
309 |
char *ccs_realpath_nofollow(const char *pathname) |
310 |
{ |
311 |
struct nameidata nd; |
312 |
if (pathname && path_lookup(pathname, lookup_flags ^ LOOKUP_FOLLOW, |
313 |
&nd) == 0) { |
314 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25) |
315 |
char *buf = ccs_realpath_from_dentry(nd.path.dentry, |
316 |
nd.path.mnt); |
317 |
path_put(&nd.path); |
318 |
#else |
319 |
char *buf = ccs_realpath_from_dentry(nd.dentry, nd.mnt); |
320 |
path_release(&nd); |
321 |
#endif |
322 |
return buf; |
323 |
} |
324 |
return NULL; |
325 |
} |
326 |
|
327 |
/** |
328 |
* round_up - Round up an integer so that the returned pointers are appropriately aligned. |
329 |
* |
330 |
* @size: Size in bytes. |
331 |
* |
332 |
* Returns rounded value of @size. |
333 |
* |
334 |
* FIXME: Are there more requirements that is needed for assigning value |
335 |
* atomically? |
336 |
*/ |
337 |
static inline unsigned int round_up(const unsigned int size) |
338 |
{ |
339 |
if (sizeof(void *) >= sizeof(long)) |
340 |
return ((size + sizeof(void *) - 1) |
341 |
/ sizeof(void *)) * sizeof(void *); |
342 |
else |
343 |
return ((size + sizeof(long) - 1) |
344 |
/ sizeof(long)) * sizeof(long); |
345 |
} |
346 |
|
347 |
static unsigned int allocated_memory_for_elements; |
348 |
static unsigned int quota_for_elements; |
349 |
|
350 |
/** |
351 |
* ccs_alloc_element - Allocate permanent memory for structures. |
352 |
* |
353 |
* @size: Size in bytes. |
354 |
* |
355 |
* Returns pointer to allocated memory on success, NULL otherwise. |
356 |
* |
357 |
* The RAM is chunked, so NEVER try to kfree() the returned pointer. |
358 |
*/ |
359 |
void *ccs_alloc_element(const unsigned int size) |
360 |
{ |
361 |
static DEFINE_MUTEX(lock); |
362 |
static char *buf; |
363 |
static unsigned int buf_used_len = PAGE_SIZE; |
364 |
char *ptr = NULL; |
365 |
const unsigned int word_aligned_size = round_up(size); |
366 |
if (word_aligned_size > PAGE_SIZE) |
367 |
return NULL; |
368 |
mutex_lock(&lock); |
369 |
if (buf_used_len + word_aligned_size > PAGE_SIZE) { |
370 |
if (!quota_for_elements || allocated_memory_for_elements |
371 |
+ PAGE_SIZE <= quota_for_elements) |
372 |
ptr = kzalloc(PAGE_SIZE, GFP_KERNEL); |
373 |
if (!ptr) { |
374 |
printk(KERN_WARNING "ERROR: Out of memory " |
375 |
"for ccs_alloc_element().\n"); |
376 |
if (!sbin_init_started) |
377 |
panic("MAC Initialization failed.\n"); |
378 |
} else { |
379 |
buf = ptr; |
380 |
allocated_memory_for_elements += PAGE_SIZE; |
381 |
buf_used_len = word_aligned_size; |
382 |
ptr = buf; |
383 |
} |
384 |
} else if (word_aligned_size) { |
385 |
int i; |
386 |
ptr = buf + buf_used_len; |
387 |
buf_used_len += word_aligned_size; |
388 |
for (i = 0; i < word_aligned_size; i++) { |
389 |
if (!ptr[i]) |
390 |
continue; |
391 |
printk(KERN_ERR "WARNING: Reserved memory was tainted! " |
392 |
"The system might go wrong.\n"); |
393 |
ptr[i] = '\0'; |
394 |
} |
395 |
} |
396 |
mutex_unlock(&lock); |
397 |
return ptr; |
398 |
} |
399 |
|
400 |
static unsigned int allocated_memory_for_savename; |
401 |
static unsigned int quota_for_savename; |
402 |
|
403 |
#define MAX_HASH 256 |
404 |
|
405 |
/* Structure for string data. */ |
406 |
struct name_entry { |
407 |
struct list1_head list; |
408 |
struct path_info entry; |
409 |
}; |
410 |
|
411 |
/* Structure for available memory region. */ |
412 |
struct free_memory_block_list { |
413 |
struct list_head list; |
414 |
char *ptr; /* Pointer to a free area. */ |
415 |
int len; /* Length of the area. */ |
416 |
}; |
417 |
|
418 |
/* The list for "struct name_entry". */ |
419 |
static struct list1_head name_list[MAX_HASH]; |
420 |
|
421 |
/** |
422 |
* ccs_save_name - Allocate permanent memory for string data. |
423 |
* |
424 |
* @name: The string to store into the permernent memory. |
425 |
* |
426 |
* Returns pointer to "struct path_info" on success, NULL otherwise. |
427 |
* |
428 |
* The RAM is shared, so NEVER try to modify or kfree() the returned name. |
429 |
*/ |
430 |
const struct path_info *ccs_save_name(const char *name) |
431 |
{ |
432 |
static LIST_HEAD(fmb_list); |
433 |
static DEFINE_MUTEX(lock); |
434 |
struct name_entry *ptr; |
435 |
unsigned int hash; |
436 |
struct free_memory_block_list *fmb; |
437 |
int len; |
438 |
char *cp; |
439 |
if (!name) |
440 |
return NULL; |
441 |
len = strlen(name) + 1; |
442 |
if (len > CCS_MAX_PATHNAME_LEN) { |
443 |
printk(KERN_WARNING "ERROR: Name too long " |
444 |
"for ccs_save_name().\n"); |
445 |
return NULL; |
446 |
} |
447 |
hash = full_name_hash((const unsigned char *) name, len - 1); |
448 |
mutex_lock(&lock); |
449 |
list1_for_each_entry(ptr, &name_list[hash % MAX_HASH], list) { |
450 |
if (hash == ptr->entry.hash && !strcmp(name, ptr->entry.name)) |
451 |
goto out; |
452 |
} |
453 |
list_for_each_entry(fmb, &fmb_list, list) { |
454 |
if (len <= fmb->len) |
455 |
goto ready; |
456 |
} |
457 |
if (!quota_for_savename || allocated_memory_for_savename + PAGE_SIZE |
458 |
<= quota_for_savename) |
459 |
cp = kzalloc(PAGE_SIZE, GFP_KERNEL); |
460 |
else |
461 |
cp = NULL; |
462 |
fmb = kzalloc(sizeof(*fmb), GFP_KERNEL); |
463 |
if (!cp || !fmb) { |
464 |
kfree(cp); |
465 |
kfree(fmb); |
466 |
printk(KERN_WARNING "ERROR: Out of memory " |
467 |
"for ccs_save_name().\n"); |
468 |
if (!sbin_init_started) |
469 |
panic("MAC Initialization failed.\n"); |
470 |
ptr = NULL; |
471 |
goto out; |
472 |
} |
473 |
allocated_memory_for_savename += PAGE_SIZE; |
474 |
list_add(&fmb->list, &fmb_list); |
475 |
fmb->ptr = cp; |
476 |
fmb->len = PAGE_SIZE; |
477 |
ready: |
478 |
ptr = ccs_alloc_element(sizeof(*ptr)); |
479 |
if (!ptr) |
480 |
goto out; |
481 |
ptr->entry.name = fmb->ptr; |
482 |
memmove(fmb->ptr, name, len); |
483 |
ccs_fill_path_info(&ptr->entry); |
484 |
fmb->ptr += len; |
485 |
fmb->len -= len; |
486 |
list1_add_tail_mb(&ptr->list, &name_list[hash % MAX_HASH]); |
487 |
if (fmb->len == 0) { |
488 |
list_del(&fmb->list); |
489 |
kfree(fmb); |
490 |
} |
491 |
out: |
492 |
mutex_unlock(&lock); |
493 |
return ptr ? &ptr->entry : NULL; |
494 |
} |
495 |
|
496 |
/* Structure for temporarily allocated memory. */ |
497 |
struct cache_entry { |
498 |
struct list_head list; |
499 |
void *ptr; |
500 |
int size; |
501 |
}; |
502 |
|
503 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20) |
504 |
static struct kmem_cache *ccs_cachep; |
505 |
#else |
506 |
static kmem_cache_t *ccs_cachep; |
507 |
#endif |
508 |
|
509 |
/** |
510 |
* ccs_realpath_init - Initialize realpath related code. |
511 |
* |
512 |
* Returns 0. |
513 |
*/ |
514 |
static int __init ccs_realpath_init(void) |
515 |
{ |
516 |
int i; |
517 |
if (CCS_MAX_PATHNAME_LEN > PAGE_SIZE) |
518 |
panic("Bad size."); |
519 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23) |
520 |
ccs_cachep = kmem_cache_create("ccs_cache", sizeof(struct cache_entry), |
521 |
0, 0, NULL); |
522 |
#else |
523 |
ccs_cachep = kmem_cache_create("ccs_cache", sizeof(struct cache_entry), |
524 |
0, 0, NULL, NULL); |
525 |
#endif |
526 |
if (!ccs_cachep) |
527 |
panic("Can't create cache.\n"); |
528 |
for (i = 0; i < MAX_HASH; i++) |
529 |
INIT_LIST1_HEAD(&name_list[i]); |
530 |
INIT_LIST1_HEAD(&KERNEL_DOMAIN.acl_info_list); |
531 |
KERNEL_DOMAIN.domainname = ccs_save_name(ROOT_NAME); |
532 |
list1_add_tail_mb(&KERNEL_DOMAIN.list, &domain_list); |
533 |
if (ccs_find_domain(ROOT_NAME) != &KERNEL_DOMAIN) |
534 |
panic("Can't register KERNEL_DOMAIN"); |
535 |
return 0; |
536 |
} |
537 |
|
538 |
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0) |
539 |
__initcall(ccs_realpath_init); |
540 |
#else |
541 |
core_initcall(ccs_realpath_init); |
542 |
#endif |
543 |
|
544 |
/* The list for "struct cache_entry". */ |
545 |
static LIST_HEAD(cache_list); |
546 |
|
547 |
static DEFINE_SPINLOCK(cache_list_lock); |
548 |
|
549 |
static unsigned int dynamic_memory_size; |
550 |
|
551 |
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0) |
552 |
/** |
553 |
* round2 - Rounded up to power-of-two value. |
554 |
* |
555 |
* @size: Size in bytes. |
556 |
* |
557 |
* Returns power-of-two of @size. |
558 |
*/ |
559 |
static int round2(size_t size) |
560 |
{ |
561 |
#if PAGE_SIZE == 4096 |
562 |
size_t bsize = 32; |
563 |
#else |
564 |
size_t bsize = 64; |
565 |
#endif |
566 |
while (size > bsize) |
567 |
bsize <<= 1; |
568 |
return bsize; |
569 |
} |
570 |
#endif |
571 |
|
572 |
/** |
573 |
* ccs_alloc - Allocate memory for temporal purpose. |
574 |
* |
575 |
* @size: Size in bytes. |
576 |
* |
577 |
* Returns pointer to allocated memory on success, NULL otherwise. |
578 |
*/ |
579 |
void *ccs_alloc(const size_t size) |
580 |
{ |
581 |
struct cache_entry *new_entry; |
582 |
void *ret = kzalloc(size, GFP_KERNEL); |
583 |
if (!ret) |
584 |
goto out; |
585 |
new_entry = kmem_cache_alloc(ccs_cachep, GFP_KERNEL); |
586 |
if (!new_entry) { |
587 |
kfree(ret); |
588 |
ret = NULL; |
589 |
goto out; |
590 |
} |
591 |
INIT_LIST_HEAD(&new_entry->list); |
592 |
new_entry->ptr = ret; |
593 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0) |
594 |
new_entry->size = ksize(ret); |
595 |
#else |
596 |
new_entry->size = round2(size); |
597 |
#endif |
598 |
/***** CRITICAL SECTION START *****/ |
599 |
spin_lock(&cache_list_lock); |
600 |
list_add_tail(&new_entry->list, &cache_list); |
601 |
dynamic_memory_size += new_entry->size; |
602 |
spin_unlock(&cache_list_lock); |
603 |
/***** CRITICAL SECTION END *****/ |
604 |
out: |
605 |
return ret; |
606 |
} |
607 |
|
608 |
/** |
609 |
* ccs_free - Release memory allocated by ccs_alloc(). |
610 |
* |
611 |
* @p: Pointer returned by ccs_alloc(). May be NULL. |
612 |
* |
613 |
* Returns nothing. |
614 |
*/ |
615 |
void ccs_free(const void *p) |
616 |
{ |
617 |
struct list_head *v; |
618 |
struct cache_entry *entry = NULL; |
619 |
if (!p) |
620 |
return; |
621 |
/***** CRITICAL SECTION START *****/ |
622 |
spin_lock(&cache_list_lock); |
623 |
list_for_each(v, &cache_list) { |
624 |
entry = list_entry(v, struct cache_entry, list); |
625 |
if (entry->ptr != p) { |
626 |
entry = NULL; |
627 |
continue; |
628 |
} |
629 |
list_del(&entry->list); |
630 |
dynamic_memory_size -= entry->size; |
631 |
break; |
632 |
} |
633 |
spin_unlock(&cache_list_lock); |
634 |
/***** CRITICAL SECTION END *****/ |
635 |
if (entry) { |
636 |
kfree(p); |
637 |
kmem_cache_free(ccs_cachep, entry); |
638 |
} else { |
639 |
printk(KERN_WARNING "BUG: ccs_free() with invalid pointer.\n"); |
640 |
} |
641 |
} |
642 |
|
643 |
/** |
644 |
* ccs_read_memory_counter - Check for memory usage. |
645 |
* |
646 |
* @head: Pointer to "struct ccs_io_buffer". |
647 |
* |
648 |
* Returns memory usage. |
649 |
*/ |
650 |
int ccs_read_memory_counter(struct ccs_io_buffer *head) |
651 |
{ |
652 |
if (!head->read_eof) { |
653 |
const unsigned int shared = allocated_memory_for_savename; |
654 |
const unsigned int private = allocated_memory_for_elements; |
655 |
const unsigned int dynamic = dynamic_memory_size; |
656 |
char buffer[64]; |
657 |
memset(buffer, 0, sizeof(buffer)); |
658 |
if (quota_for_savename) |
659 |
snprintf(buffer, sizeof(buffer) - 1, |
660 |
" (Quota: %10u)", quota_for_savename); |
661 |
else |
662 |
buffer[0] = '\0'; |
663 |
ccs_io_printf(head, "Shared: %10u%s\n", shared, buffer); |
664 |
if (quota_for_elements) |
665 |
snprintf(buffer, sizeof(buffer) - 1, |
666 |
" (Quota: %10u)", quota_for_elements); |
667 |
else |
668 |
buffer[0] = '\0'; |
669 |
ccs_io_printf(head, "Private: %10u%s\n", private, buffer); |
670 |
ccs_io_printf(head, "Dynamic: %10u\n", dynamic); |
671 |
ccs_io_printf(head, "Total: %10u\n", |
672 |
shared + private + dynamic); |
673 |
head->read_eof = true; |
674 |
} |
675 |
return 0; |
676 |
} |
677 |
|
678 |
/** |
679 |
* ccs_write_memory_quota - Set memory quota. |
680 |
* |
681 |
* @head: Pointer to "struct ccs_io_buffer". |
682 |
* |
683 |
* Returns 0. |
684 |
*/ |
685 |
int ccs_write_memory_quota(struct ccs_io_buffer *head) |
686 |
{ |
687 |
char *data = head->write_buf; |
688 |
unsigned int size; |
689 |
if (sscanf(data, "Shared: %u", &size) == 1) |
690 |
quota_for_savename = size; |
691 |
else if (sscanf(data, "Private: %u", &size) == 1) |
692 |
quota_for_elements = size; |
693 |
return 0; |
694 |
} |