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