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) 2012, 2014, 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 <libcfs/libcfs.h>
54 * Implementation of cfs_curproc API (see portals/include/libcfs/curproc.h)
58 /* Currently all the CFS_CAP_* defines match CAP_* ones. */
59 #define cfs_cap_pack(cap) (cap)
60 #define cfs_cap_unpack(cap) (cap)
62 void cfs_cap_raise(cfs_cap_t cap)
65 if ((cred = prepare_creds())) {
66 cap_raise(cred->cap_effective, cfs_cap_unpack(cap));
71 void cfs_cap_lower(cfs_cap_t cap)
74 if ((cred = prepare_creds())) {
75 cap_lower(cred->cap_effective, cfs_cap_unpack(cap));
80 int cfs_cap_raised(cfs_cap_t cap)
82 return cap_raised(current_cap(), cfs_cap_unpack(cap));
85 static void cfs_kernel_cap_pack(kernel_cap_t kcap, cfs_cap_t *cap)
87 #if defined (_LINUX_CAPABILITY_VERSION) && _LINUX_CAPABILITY_VERSION == 0x19980330
88 *cap = cfs_cap_pack(kcap);
89 #elif defined (_LINUX_CAPABILITY_VERSION) && _LINUX_CAPABILITY_VERSION == 0x20071026
90 *cap = cfs_cap_pack(kcap[0]);
91 #elif defined(_KERNEL_CAPABILITY_VERSION) && _KERNEL_CAPABILITY_VERSION == 0x20080522
92 /* XXX lost high byte */
93 *cap = cfs_cap_pack(kcap.cap[0]);
95 #error "need correct _KERNEL_CAPABILITY_VERSION "
99 static void cfs_kernel_cap_unpack(kernel_cap_t *kcap, cfs_cap_t cap)
101 #if defined (_LINUX_CAPABILITY_VERSION) && _LINUX_CAPABILITY_VERSION == 0x19980330
102 *kcap = cfs_cap_unpack(cap);
103 #elif defined (_LINUX_CAPABILITY_VERSION) && _LINUX_CAPABILITY_VERSION == 0x20071026
104 (*kcap)[0] = cfs_cap_unpack(cap);
105 #elif defined(_KERNEL_CAPABILITY_VERSION) && _KERNEL_CAPABILITY_VERSION == 0x20080522
106 kcap->cap[0] = cfs_cap_unpack(cap);
108 #error "need correct _KERNEL_CAPABILITY_VERSION "
112 cfs_cap_t cfs_curproc_cap_pack(void)
115 cfs_kernel_cap_pack(current_cap(), &cap);
119 void cfs_curproc_cap_unpack(cfs_cap_t cap)
122 if ((cred = prepare_creds())) {
123 cfs_kernel_cap_unpack(&cred->cap_effective, cap);
128 int cfs_capable(cfs_cap_t cap)
130 return capable(cfs_cap_unpack(cap));
133 static int cfs_access_process_vm(struct task_struct *tsk,
134 struct mm_struct *mm,
136 void *buf, int len, int write)
138 /* Just copied from kernel for the kernels which doesn't
139 * have access_process_vm() exported */
140 struct vm_area_struct *vma;
144 /* Avoid deadlocks on mmap_sem if called from sys_mmap_pgoff(),
145 * which is already holding mmap_sem for writes. If some other
146 * thread gets the write lock in the meantime, this thread will
147 * block, but at least it won't deadlock on itself. LU-1735 */
148 if (down_read_trylock(&mm->mmap_sem) == 0)
151 /* ignore errors, just check how much was successfully transferred */
153 int bytes, rc, offset;
156 rc = get_user_pages(tsk, mm, addr, 1,
157 write, 1, &page, &vma);
162 offset = addr & (PAGE_SIZE-1);
163 if (bytes > PAGE_SIZE-offset)
164 bytes = PAGE_SIZE-offset;
168 copy_to_user_page(vma, page, addr,
169 maddr + offset, buf, bytes);
170 set_page_dirty_lock(page);
172 copy_from_user_page(vma, page, addr,
173 buf, maddr + offset, bytes);
176 page_cache_release(page);
181 up_read(&mm->mmap_sem);
183 return buf - old_buf;
186 /* Read the environment variable of current process specified by @key. */
187 int cfs_get_environ(const char *key, char *value, int *val_len)
189 struct mm_struct *mm;
191 int buf_len = PAGE_CACHE_SIZE;
192 int key_len = strlen(key);
198 buffer = kmalloc(buf_len, GFP_USER);
202 mm = get_task_mm(current);
208 addr = mm->env_start;
209 while (addr < mm->env_end) {
210 int this_len, retval, scan_len;
211 char *env_start, *env_end;
213 memset(buffer, 0, buf_len);
215 this_len = min_t(int, mm->env_end - addr, buf_len);
216 retval = cfs_access_process_vm(current, mm, addr, buffer,
219 GOTO(out, rc = retval);
220 else if (retval != this_len)
225 /* Parse the buffer to find out the specified key/value pair.
226 * The "key=value" entries are separated by '\0'. */
233 env_end = memscan(env_start, '\0', scan_len);
234 LASSERT(env_end >= env_start &&
235 env_end <= env_start + scan_len);
237 /* The last entry of this buffer cross the buffer
238 * boundary, reread it in next cycle. */
239 if (unlikely(env_end - env_start == scan_len)) {
240 /* Just skip the entry larger than page size,
241 * it can't be jobID env variable. */
242 if (unlikely(scan_len == this_len))
247 } else if (unlikely(skip)) {
253 entry_len = env_end - env_start;
255 /* Key length + length of '=' */
256 if (entry_len > key_len + 1 &&
257 !memcmp(entry, key, key_len)) {
258 entry += key_len + 1;
259 entry_len -= key_len + 1;
260 /* The 'value' buffer passed in is too small.*/
261 if (entry_len >= *val_len)
262 GOTO(out, rc = -EOVERFLOW);
264 memcpy(value, entry, entry_len);
265 *val_len = entry_len;
269 scan_len -= (env_end - env_start + 1);
270 env_start = env_end + 1;
273 GOTO(out, rc = -ENOENT);
277 kfree((void *)buffer);
280 EXPORT_SYMBOL(cfs_get_environ);
282 EXPORT_SYMBOL(cfs_cap_raise);
283 EXPORT_SYMBOL(cfs_cap_lower);
284 EXPORT_SYMBOL(cfs_cap_raised);
285 EXPORT_SYMBOL(cfs_curproc_cap_pack);
286 EXPORT_SYMBOL(cfs_capable);
290 * c-indentation-style: "K&R"