Merge remote-tracking branch 'regulator/topic/tps65910' into regulator-next
[deliverable/linux.git] / drivers / staging / lustre / lustre / libcfs / linux / linux-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) 2011, 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/linux/linux-curproc.c
37 *
38 * Lustre curproc API implementation for Linux kernel
39 *
40 * Author: Nikita Danilov <nikita@clusterfs.com>
41 */
42
43 #include <linux/sched.h>
44 #include <linux/fs_struct.h>
45
46 #include <linux/compat.h>
47 #include <linux/thread_info.h>
48
49 #define DEBUG_SUBSYSTEM S_LNET
50
51 #include <linux/libcfs/libcfs.h>
52
53 /*
54 * Implementation of cfs_curproc API (see portals/include/libcfs/curproc.h)
55 * for Linux kernel.
56 */
57
58 int cfs_curproc_groups_nr(void)
59 {
60 int nr;
61
62 task_lock(current);
63 nr = current_cred()->group_info->ngroups;
64 task_unlock(current);
65 return nr;
66 }
67
68 /* Currently all the CFS_CAP_* defines match CAP_* ones. */
69 #define cfs_cap_pack(cap) (cap)
70 #define cfs_cap_unpack(cap) (cap)
71
72 void cfs_cap_raise(cfs_cap_t cap)
73 {
74 struct cred *cred;
75 if ((cred = prepare_creds())) {
76 cap_raise(cred->cap_effective, cfs_cap_unpack(cap));
77 commit_creds(cred);
78 }
79 }
80
81 void cfs_cap_lower(cfs_cap_t cap)
82 {
83 struct cred *cred;
84 if ((cred = prepare_creds())) {
85 cap_lower(cred->cap_effective, cfs_cap_unpack(cap));
86 commit_creds(cred);
87 }
88 }
89
90 int cfs_cap_raised(cfs_cap_t cap)
91 {
92 return cap_raised(current_cap(), cfs_cap_unpack(cap));
93 }
94
95 void cfs_kernel_cap_pack(kernel_cap_t kcap, cfs_cap_t *cap)
96 {
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]);
104 #else
105 #error "need correct _KERNEL_CAPABILITY_VERSION "
106 #endif
107 }
108
109 void cfs_kernel_cap_unpack(kernel_cap_t *kcap, cfs_cap_t cap)
110 {
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);
117 #else
118 #error "need correct _KERNEL_CAPABILITY_VERSION "
119 #endif
120 }
121
122 cfs_cap_t cfs_curproc_cap_pack(void)
123 {
124 cfs_cap_t cap;
125 cfs_kernel_cap_pack(current_cap(), &cap);
126 return cap;
127 }
128
129 void cfs_curproc_cap_unpack(cfs_cap_t cap)
130 {
131 struct cred *cred;
132 if ((cred = prepare_creds())) {
133 cfs_kernel_cap_unpack(&cred->cap_effective, cap);
134 commit_creds(cred);
135 }
136 }
137
138 int cfs_capable(cfs_cap_t cap)
139 {
140 return capable(cfs_cap_unpack(cap));
141 }
142
143 /* Check if task is running in 32-bit API mode, for the purpose of
144 * userspace binary interfaces. On 32-bit Linux this is (unfortunately)
145 * always true, even if the application is using LARGEFILE64 and 64-bit
146 * APIs, because Linux provides no way for the filesystem to know if it
147 * is called via 32-bit or 64-bit APIs. Other clients may vary. On
148 * 64-bit systems, this will only be true if the binary is calling a
149 * 32-bit system call. */
150 int current_is_32bit(void)
151 {
152 return is_compat_task();
153 }
154
155 static int cfs_access_process_vm(struct task_struct *tsk, unsigned long addr,
156 void *buf, int len, int write)
157 {
158 /* Just copied from kernel for the kernels which doesn't
159 * have access_process_vm() exported */
160 struct mm_struct *mm;
161 struct vm_area_struct *vma;
162 struct page *page;
163 void *old_buf = buf;
164
165 mm = get_task_mm(tsk);
166 if (!mm)
167 return 0;
168
169 down_read(&mm->mmap_sem);
170 /* ignore errors, just check how much was sucessfully transfered */
171 while (len) {
172 int bytes, rc, offset;
173 void *maddr;
174
175 rc = get_user_pages(tsk, mm, addr, 1,
176 write, 1, &page, &vma);
177 if (rc <= 0)
178 break;
179
180 bytes = len;
181 offset = addr & (PAGE_SIZE-1);
182 if (bytes > PAGE_SIZE-offset)
183 bytes = PAGE_SIZE-offset;
184
185 maddr = kmap(page);
186 if (write) {
187 copy_to_user_page(vma, page, addr,
188 maddr + offset, buf, bytes);
189 set_page_dirty_lock(page);
190 } else {
191 copy_from_user_page(vma, page, addr,
192 buf, maddr + offset, bytes);
193 }
194 kunmap(page);
195 page_cache_release(page);
196 len -= bytes;
197 buf += bytes;
198 addr += bytes;
199 }
200 up_read(&mm->mmap_sem);
201 mmput(mm);
202
203 return buf - old_buf;
204 }
205
206 /* Read the environment variable of current process specified by @key. */
207 int cfs_get_environ(const char *key, char *value, int *val_len)
208 {
209 struct mm_struct *mm;
210 char *buffer, *tmp_buf = NULL;
211 int buf_len = PAGE_CACHE_SIZE;
212 int key_len = strlen(key);
213 unsigned long addr;
214 int rc;
215
216 buffer = kmalloc(buf_len, GFP_USER);
217 if (!buffer)
218 return -ENOMEM;
219
220 mm = get_task_mm(current);
221 if (!mm) {
222 kfree(buffer);
223 return -EINVAL;
224 }
225
226 /* Avoid deadlocks on mmap_sem if called from sys_mmap_pgoff(),
227 * which is already holding mmap_sem for writes. If some other
228 * thread gets the write lock in the meantime, this thread will
229 * block, but at least it won't deadlock on itself. LU-1735 */
230 if (down_read_trylock(&mm->mmap_sem) == 0)
231 return -EDEADLK;
232 up_read(&mm->mmap_sem);
233
234 addr = mm->env_start;
235 while (addr < mm->env_end) {
236 int this_len, retval, scan_len;
237 char *env_start, *env_end;
238
239 memset(buffer, 0, buf_len);
240
241 this_len = min_t(int, mm->env_end - addr, buf_len);
242 retval = cfs_access_process_vm(current, addr, buffer,
243 this_len, 0);
244 if (retval != this_len)
245 break;
246
247 addr += retval;
248
249 /* Parse the buffer to find out the specified key/value pair.
250 * The "key=value" entries are separated by '\0'. */
251 env_start = buffer;
252 scan_len = this_len;
253 while (scan_len) {
254 char *entry;
255 int entry_len;
256
257 env_end = memscan(env_start, '\0', scan_len);
258 LASSERT(env_end >= env_start &&
259 env_end <= env_start + scan_len);
260
261 /* The last entry of this buffer cross the buffer
262 * boundary, reread it in next cycle. */
263 if (unlikely(env_end - env_start == scan_len)) {
264 /* This entry is too large to fit in buffer */
265 if (unlikely(scan_len == this_len)) {
266 CERROR("Too long env variable.\n");
267 GOTO(out, rc = -EINVAL);
268 }
269 addr -= scan_len;
270 break;
271 }
272
273 entry = env_start;
274 entry_len = env_end - env_start;
275
276 /* Key length + length of '=' */
277 if (entry_len > key_len + 1 &&
278 !memcmp(entry, key, key_len)) {
279 entry += key_len + 1;
280 entry_len -= key_len + 1;
281 /* The 'value' buffer passed in is too small.*/
282 if (entry_len >= *val_len)
283 GOTO(out, rc = -EOVERFLOW);
284
285 memcpy(value, entry, entry_len);
286 *val_len = entry_len;
287 GOTO(out, rc = 0);
288 }
289
290 scan_len -= (env_end - env_start + 1);
291 env_start = env_end + 1;
292 }
293 }
294 GOTO(out, rc = -ENOENT);
295
296 out:
297 mmput(mm);
298 kfree((void *)buffer);
299 if (tmp_buf)
300 kfree((void *)tmp_buf);
301 return rc;
302 }
303 EXPORT_SYMBOL(cfs_get_environ);
304
305 EXPORT_SYMBOL(cfs_curproc_groups_nr);
306 EXPORT_SYMBOL(cfs_cap_raise);
307 EXPORT_SYMBOL(cfs_cap_lower);
308 EXPORT_SYMBOL(cfs_cap_raised);
309 EXPORT_SYMBOL(cfs_curproc_cap_pack);
310 EXPORT_SYMBOL(cfs_curproc_cap_unpack);
311 EXPORT_SYMBOL(cfs_capable);
312 EXPORT_SYMBOL(current_is_32bit);
313
314 /*
315 * Local variables:
316 * c-indentation-style: "K&R"
317 * c-basic-offset: 8
318 * tab-width: 8
319 * fill-column: 80
320 * scroll-step: 1
321 * End:
322 */
This page took 0.037302 seconds and 5 git commands to generate.