4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * libcfs/libcfs/linux/linux-curproc.c
38 * Lustre curproc API implementation for Linux kernel
40 * Author: Nikita Danilov <nikita@clusterfs.com>
43 #include <linux/sched.h>
44 #include <linux/fs_struct.h>
46 #include <linux/compat.h>
47 #include <linux/thread_info.h>
49 #define DEBUG_SUBSYSTEM S_LNET
51 #include <linux/libcfs/libcfs.h>
54 * Implementation of cfs_curproc API (see portals/include/libcfs/curproc.h)
58 int cfs_curproc_groups_nr(void)
63 nr = current_cred()->group_info->ngroups;
68 /* Currently all the CFS_CAP_* defines match CAP_* ones. */
69 #define cfs_cap_pack(cap) (cap)
70 #define cfs_cap_unpack(cap) (cap)
72 void cfs_cap_raise(cfs_cap_t cap)
75 if ((cred = prepare_creds())) {
76 cap_raise(cred->cap_effective, cfs_cap_unpack(cap));
81 void cfs_cap_lower(cfs_cap_t cap)
84 if ((cred = prepare_creds())) {
85 cap_lower(cred->cap_effective, cfs_cap_unpack(cap));
90 int cfs_cap_raised(cfs_cap_t cap)
92 return cap_raised(current_cap(), cfs_cap_unpack(cap));
95 void cfs_kernel_cap_pack(kernel_cap_t kcap, cfs_cap_t *cap)
97 #if defined (_LINUX_CAPABILITY_VERSION) && _LINUX_CAPABILITY_VERSION == 0x19980330
98 *cap = cfs_cap_pack(kcap);
99 #elif defined (_LINUX_CAPABILITY_VERSION) && _LINUX_CAPABILITY_VERSION == 0x20071026
100 *cap = cfs_cap_pack(kcap[0]);
101 #elif defined(_KERNEL_CAPABILITY_VERSION) && _KERNEL_CAPABILITY_VERSION == 0x20080522
102 /* XXX lost high byte */
103 *cap = cfs_cap_pack(kcap.cap[0]);
105 #error "need correct _KERNEL_CAPABILITY_VERSION "
109 void cfs_kernel_cap_unpack(kernel_cap_t *kcap, cfs_cap_t cap)
111 #if defined (_LINUX_CAPABILITY_VERSION) && _LINUX_CAPABILITY_VERSION == 0x19980330
112 *kcap = cfs_cap_unpack(cap);
113 #elif defined (_LINUX_CAPABILITY_VERSION) && _LINUX_CAPABILITY_VERSION == 0x20071026
114 (*kcap)[0] = cfs_cap_unpack(cap);
115 #elif defined(_KERNEL_CAPABILITY_VERSION) && _KERNEL_CAPABILITY_VERSION == 0x20080522
116 kcap->cap[0] = cfs_cap_unpack(cap);
118 #error "need correct _KERNEL_CAPABILITY_VERSION "
122 cfs_cap_t cfs_curproc_cap_pack(void)
125 cfs_kernel_cap_pack(current_cap(), &cap);
129 void cfs_curproc_cap_unpack(cfs_cap_t cap)
132 if ((cred = prepare_creds())) {
133 cfs_kernel_cap_unpack(&cred->cap_effective, cap);
138 int cfs_capable(cfs_cap_t cap)
140 return capable(cfs_cap_unpack(cap));
143 static int cfs_access_process_vm(struct task_struct *tsk, unsigned long addr,
144 void *buf, int len, int write)
146 /* Just copied from kernel for the kernels which doesn't
147 * have access_process_vm() exported */
148 struct mm_struct *mm;
149 struct vm_area_struct *vma;
153 mm = get_task_mm(tsk);
157 down_read(&mm->mmap_sem);
158 /* ignore errors, just check how much was successfully transferred */
160 int bytes, rc, offset;
163 rc = get_user_pages(tsk, mm, addr, 1,
164 write, 1, &page, &vma);
169 offset = addr & (PAGE_SIZE-1);
170 if (bytes > PAGE_SIZE-offset)
171 bytes = PAGE_SIZE-offset;
175 copy_to_user_page(vma, page, addr,
176 maddr + offset, buf, bytes);
177 set_page_dirty_lock(page);
179 copy_from_user_page(vma, page, addr,
180 buf, maddr + offset, bytes);
183 page_cache_release(page);
188 up_read(&mm->mmap_sem);
191 return buf - old_buf;
194 /* Read the environment variable of current process specified by @key. */
195 int cfs_get_environ(const char *key, char *value, int *val_len)
197 struct mm_struct *mm;
198 char *buffer, *tmp_buf = NULL;
199 int buf_len = PAGE_CACHE_SIZE;
200 int key_len = strlen(key);
204 buffer = kmalloc(buf_len, GFP_USER);
208 mm = get_task_mm(current);
214 /* Avoid deadlocks on mmap_sem if called from sys_mmap_pgoff(),
215 * which is already holding mmap_sem for writes. If some other
216 * thread gets the write lock in the meantime, this thread will
217 * block, but at least it won't deadlock on itself. LU-1735 */
218 if (down_read_trylock(&mm->mmap_sem) == 0) {
222 up_read(&mm->mmap_sem);
224 addr = mm->env_start;
225 while (addr < mm->env_end) {
226 int this_len, retval, scan_len;
227 char *env_start, *env_end;
229 memset(buffer, 0, buf_len);
231 this_len = min_t(int, mm->env_end - addr, buf_len);
232 retval = cfs_access_process_vm(current, addr, buffer,
234 if (retval != this_len)
239 /* Parse the buffer to find out the specified key/value pair.
240 * The "key=value" entries are separated by '\0'. */
247 env_end = memscan(env_start, '\0', scan_len);
248 LASSERT(env_end >= env_start &&
249 env_end <= env_start + scan_len);
251 /* The last entry of this buffer cross the buffer
252 * boundary, reread it in next cycle. */
253 if (unlikely(env_end - env_start == scan_len)) {
254 /* This entry is too large to fit in buffer */
255 if (unlikely(scan_len == this_len)) {
256 CERROR("Too long env variable.\n");
257 GOTO(out, rc = -EINVAL);
264 entry_len = env_end - env_start;
266 /* Key length + length of '=' */
267 if (entry_len > key_len + 1 &&
268 !memcmp(entry, key, key_len)) {
269 entry += key_len + 1;
270 entry_len -= key_len + 1;
271 /* The 'value' buffer passed in is too small.*/
272 if (entry_len >= *val_len)
273 GOTO(out, rc = -EOVERFLOW);
275 memcpy(value, entry, entry_len);
276 *val_len = entry_len;
280 scan_len -= (env_end - env_start + 1);
281 env_start = env_end + 1;
284 GOTO(out, rc = -ENOENT);
288 kfree((void *)buffer);
290 kfree((void *)tmp_buf);
293 EXPORT_SYMBOL(cfs_get_environ);
295 EXPORT_SYMBOL(cfs_curproc_groups_nr);
296 EXPORT_SYMBOL(cfs_cap_raise);
297 EXPORT_SYMBOL(cfs_cap_lower);
298 EXPORT_SYMBOL(cfs_cap_raised);
299 EXPORT_SYMBOL(cfs_curproc_cap_pack);
300 EXPORT_SYMBOL(cfs_curproc_cap_unpack);
301 EXPORT_SYMBOL(cfs_capable);
305 * c-indentation-style: "K&R"