Whamcloud - gitweb
70510c990c9679c5625a7fea0878a457ef5aac12
[fs/lustre-release.git] / libcfs / 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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * libcfs/libcfs/linux/linux-curproc.c
33  *
34  * Lustre curproc API implementation for Linux kernel
35  *
36  * Author: Nikita Danilov <nikita@clusterfs.com>
37  */
38
39 #include <linux/sched.h>
40 #ifdef HAVE_SCHED_HEADERS
41 #include <linux/sched/signal.h>
42 #include <linux/sched/mm.h>
43 #endif
44 #include <linux/fs_struct.h>
45 #include <linux/pagemap.h>
46 #include <linux/compat.h>
47 #include <linux/thread_info.h>
48
49 #define DEBUG_SUBSYSTEM S_LNET
50
51 #include <libcfs/libcfs.h>
52
53 /*
54  * Implementation of cfs_curproc API (see portals/include/libcfs/curproc.h)
55  * for Linux kernel.
56  */
57
58 /* Currently all the CFS_CAP_* defines match CAP_* ones. */
59 #define cfs_cap_pack(cap) (cap)
60 #define cfs_cap_unpack(cap) (cap)
61
62 static void cfs_kernel_cap_pack(kernel_cap_t kcap, cfs_cap_t *cap)
63 {
64 #if defined (_LINUX_CAPABILITY_VERSION) && _LINUX_CAPABILITY_VERSION == 0x19980330
65         *cap = cfs_cap_pack(kcap);
66 #elif defined (_LINUX_CAPABILITY_VERSION) && _LINUX_CAPABILITY_VERSION == 0x20071026
67         *cap = cfs_cap_pack(kcap[0]);
68 #elif defined(_KERNEL_CAPABILITY_VERSION) && _KERNEL_CAPABILITY_VERSION == 0x20080522
69         /* XXX lost high byte */
70         *cap = cfs_cap_pack(kcap.cap[0]);
71 #else
72         #error "need correct _KERNEL_CAPABILITY_VERSION "
73 #endif
74 }
75
76 static void cfs_kernel_cap_unpack(kernel_cap_t *kcap, cfs_cap_t cap)
77 {
78 #if defined (_LINUX_CAPABILITY_VERSION) && _LINUX_CAPABILITY_VERSION == 0x19980330
79         *kcap = cfs_cap_unpack(cap);
80 #elif defined (_LINUX_CAPABILITY_VERSION) && _LINUX_CAPABILITY_VERSION == 0x20071026
81         (*kcap)[0] = cfs_cap_unpack(cap);
82 #elif defined(_KERNEL_CAPABILITY_VERSION) && _KERNEL_CAPABILITY_VERSION == 0x20080522
83         kcap->cap[0] = cfs_cap_unpack(cap);
84 #else
85         #error "need correct _KERNEL_CAPABILITY_VERSION "
86 #endif
87 }
88
89 cfs_cap_t cfs_curproc_cap_pack(void)
90 {
91         cfs_cap_t cap;
92         cfs_kernel_cap_pack(current_cap(), &cap);
93         return cap;
94 }
95
96 void cfs_curproc_cap_unpack(cfs_cap_t cap)
97 {
98         struct cred *cred;
99         if ((cred = prepare_creds())) {
100                 cfs_kernel_cap_unpack(&cred->cap_effective, cap);
101                 commit_creds(cred);
102         }
103 }
104
105 int cfs_capable(cfs_cap_t cap)
106 {
107         return capable(cfs_cap_unpack(cap));
108 }
109
110 static int cfs_access_process_vm(struct task_struct *tsk,
111                                  struct mm_struct *mm,
112                                  unsigned long addr,
113                                  void *buf, int len, int write)
114 {
115         /* Just copied from kernel for the kernels which doesn't
116          * have access_process_vm() exported */
117         struct vm_area_struct *vma;
118         struct page *page;
119         void *old_buf = buf;
120
121         /* Avoid deadlocks on mmap_sem if called from sys_mmap_pgoff(),
122          * which is already holding mmap_sem for writes.  If some other
123          * thread gets the write lock in the meantime, this thread will
124          * block, but at least it won't deadlock on itself.  LU-1735 */
125         if (down_read_trylock(&mm->mmap_sem) == 0)
126                 return -EDEADLK;
127
128         /* ignore errors, just check how much was successfully transferred */
129         while (len) {
130                 int bytes, rc, offset;
131                 void *maddr;
132
133 #if defined(HAVE_GET_USER_PAGES_GUP_FLAGS)
134                 rc = get_user_pages(addr, 1, write ? FOLL_WRITE : 0, &page, &vma);
135 #elif defined(HAVE_GET_USER_PAGES_6ARG)
136                 rc = get_user_pages(addr, 1, write, 1, &page, &vma);
137 #else
138                 rc = get_user_pages(tsk, mm, addr, 1, write, 1, &page, &vma);
139 #endif
140                 if (rc <= 0)
141                         break;
142
143                 bytes = len;
144                 offset = addr & (PAGE_SIZE-1);
145                 if (bytes > PAGE_SIZE-offset)
146                         bytes = PAGE_SIZE-offset;
147
148                 maddr = kmap(page);
149                 if (write) {
150                         copy_to_user_page(vma, page, addr,
151                                           maddr + offset, buf, bytes);
152                         set_page_dirty_lock(page);
153                 } else {
154                         copy_from_user_page(vma, page, addr,
155                                             buf, maddr + offset, bytes);
156                 }
157                 kunmap(page);
158                 put_page(page);
159                 len -= bytes;
160                 buf += bytes;
161                 addr += bytes;
162         }
163         up_read(&mm->mmap_sem);
164
165         return buf - old_buf;
166 }
167
168 /* Read the environment variable of current process specified by @key. */
169 int cfs_get_environ(const char *key, char *value, int *val_len)
170 {
171         struct mm_struct *mm;
172         char *buffer;
173         int buf_len = PAGE_SIZE;
174         int key_len = strlen(key);
175         unsigned long addr;
176         int rc;
177         bool skip = false;
178         ENTRY;
179
180         buffer = kmalloc(buf_len, GFP_USER);
181         if (!buffer)
182                 RETURN(-ENOMEM);
183
184         mm = get_task_mm(current);
185         if (!mm) {
186                 kfree(buffer);
187                 RETURN(-EINVAL);
188         }
189
190         addr = mm->env_start;
191         while (addr < mm->env_end) {
192                 int this_len, retval, scan_len;
193                 char *env_start, *env_end;
194
195                 memset(buffer, 0, buf_len);
196
197                 this_len = min_t(int, mm->env_end - addr, buf_len);
198                 retval = cfs_access_process_vm(current, mm, addr, buffer,
199                                                this_len, 0);
200                 if (retval < 0)
201                         GOTO(out, rc = retval);
202                 else if (retval != this_len)
203                         break;
204
205                 addr += retval;
206
207                 /* Parse the buffer to find out the specified key/value pair.
208                  * The "key=value" entries are separated by '\0'. */
209                 env_start = buffer;
210                 scan_len = this_len;
211                 while (scan_len) {
212                         char *entry;
213                         int entry_len;
214
215                         env_end = memscan(env_start, '\0', scan_len);
216                         LASSERT(env_end >= env_start &&
217                                 env_end <= env_start + scan_len);
218
219                         /* The last entry of this buffer cross the buffer
220                          * boundary, reread it in next cycle. */
221                         if (unlikely(env_end - env_start == scan_len)) {
222                                 /* Just skip the entry larger than page size,
223                                  * it can't be jobID env variable. */
224                                 if (unlikely(scan_len == this_len))
225                                         skip = true;
226                                 else
227                                         addr -= scan_len;
228                                 break;
229                         } else if (unlikely(skip)) {
230                                 skip = false;
231                                 goto skip;
232                         }
233
234                         entry = env_start;
235                         entry_len = env_end - env_start;
236
237                         /* Key length + length of '=' */
238                         if (entry_len > key_len + 1 &&
239                             !memcmp(entry, key, key_len)) {
240                                 entry += key_len + 1;
241                                 entry_len -= key_len + 1;
242
243                                 /* The 'value' buffer passed in is too small.
244                                  * Copy what fits, but return -EOVERFLOW. */
245                                 if (entry_len >= *val_len) {
246                                         memcpy(value, entry, *val_len);
247                                         value[*val_len - 1] = 0;
248                                         GOTO(out, rc = -EOVERFLOW);
249                                 }
250
251                                 memcpy(value, entry, entry_len);
252                                 *val_len = entry_len;
253                                 GOTO(out, rc = 0);
254                         }
255 skip:
256                         scan_len -= (env_end - env_start + 1);
257                         env_start = env_end + 1;
258                 }
259         }
260         GOTO(out, rc = -ENOENT);
261
262 out:
263         mmput(mm);
264         kfree((void *)buffer);
265         return rc;
266 }
267 EXPORT_SYMBOL(cfs_get_environ);
268
269 EXPORT_SYMBOL(cfs_curproc_cap_pack);
270 EXPORT_SYMBOL(cfs_capable);
271
272 /*
273  * Local variables:
274  * c-indentation-style: "K&R"
275  * c-basic-offset: 8
276  * tab-width: 8
277  * fill-column: 80
278  * scroll-step: 1
279  * End:
280  */