Whamcloud - gitweb
LU-9859 libcfs: delete libcfs/linux/libcfs.h
[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 void cfs_cap_raise(cfs_cap_t cap)
63 {
64         struct cred *cred;
65         if ((cred = prepare_creds())) {
66                 cap_raise(cred->cap_effective, cfs_cap_unpack(cap));
67                 commit_creds(cred);
68         }
69 }
70
71 void cfs_cap_lower(cfs_cap_t cap)
72 {
73         struct cred *cred;
74         if ((cred = prepare_creds())) {
75                 cap_lower(cred->cap_effective, cfs_cap_unpack(cap));
76                 commit_creds(cred);
77         }
78 }
79
80 int cfs_cap_raised(cfs_cap_t cap)
81 {
82         return cap_raised(current_cap(), cfs_cap_unpack(cap));
83 }
84
85 static void cfs_kernel_cap_pack(kernel_cap_t kcap, cfs_cap_t *cap)
86 {
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]);
94 #else
95         #error "need correct _KERNEL_CAPABILITY_VERSION "
96 #endif
97 }
98
99 static void cfs_kernel_cap_unpack(kernel_cap_t *kcap, cfs_cap_t cap)
100 {
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);
107 #else
108         #error "need correct _KERNEL_CAPABILITY_VERSION "
109 #endif
110 }
111
112 cfs_cap_t cfs_curproc_cap_pack(void)
113 {
114         cfs_cap_t cap;
115         cfs_kernel_cap_pack(current_cap(), &cap);
116         return cap;
117 }
118
119 void cfs_curproc_cap_unpack(cfs_cap_t cap)
120 {
121         struct cred *cred;
122         if ((cred = prepare_creds())) {
123                 cfs_kernel_cap_unpack(&cred->cap_effective, cap);
124                 commit_creds(cred);
125         }
126 }
127
128 int cfs_capable(cfs_cap_t cap)
129 {
130         return capable(cfs_cap_unpack(cap));
131 }
132
133 static int cfs_access_process_vm(struct task_struct *tsk,
134                                  struct mm_struct *mm,
135                                  unsigned long addr,
136                                  void *buf, int len, int write)
137 {
138         /* Just copied from kernel for the kernels which doesn't
139          * have access_process_vm() exported */
140         struct vm_area_struct *vma;
141         struct page *page;
142         void *old_buf = buf;
143
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)
149                 return -EDEADLK;
150
151         /* ignore errors, just check how much was successfully transferred */
152         while (len) {
153                 int bytes, rc, offset;
154                 void *maddr;
155
156 #if defined(HAVE_GET_USER_PAGES_GUP_FLAGS)
157                 rc = get_user_pages(addr, 1, write ? FOLL_WRITE : 0, &page, &vma);
158 #elif defined(HAVE_GET_USER_PAGES_6ARG)
159                 rc = get_user_pages(addr, 1, write, 1, &page, &vma);
160 #else
161                 rc = get_user_pages(tsk, mm, addr, 1, write, 1, &page, &vma);
162 #endif
163                 if (rc <= 0)
164                         break;
165
166                 bytes = len;
167                 offset = addr & (PAGE_SIZE-1);
168                 if (bytes > PAGE_SIZE-offset)
169                         bytes = PAGE_SIZE-offset;
170
171                 maddr = kmap(page);
172                 if (write) {
173                         copy_to_user_page(vma, page, addr,
174                                           maddr + offset, buf, bytes);
175                         set_page_dirty_lock(page);
176                 } else {
177                         copy_from_user_page(vma, page, addr,
178                                             buf, maddr + offset, bytes);
179                 }
180                 kunmap(page);
181                 put_page(page);
182                 len -= bytes;
183                 buf += bytes;
184                 addr += bytes;
185         }
186         up_read(&mm->mmap_sem);
187
188         return buf - old_buf;
189 }
190
191 /* Read the environment variable of current process specified by @key. */
192 int cfs_get_environ(const char *key, char *value, int *val_len)
193 {
194         struct mm_struct *mm;
195         char *buffer;
196         int buf_len = PAGE_SIZE;
197         int key_len = strlen(key);
198         unsigned long addr;
199         int rc;
200         bool skip = false;
201         ENTRY;
202
203         buffer = kmalloc(buf_len, GFP_USER);
204         if (!buffer)
205                 RETURN(-ENOMEM);
206
207         mm = get_task_mm(current);
208         if (!mm) {
209                 kfree(buffer);
210                 RETURN(-EINVAL);
211         }
212
213         addr = mm->env_start;
214         while (addr < mm->env_end) {
215                 int this_len, retval, scan_len;
216                 char *env_start, *env_end;
217
218                 memset(buffer, 0, buf_len);
219
220                 this_len = min_t(int, mm->env_end - addr, buf_len);
221                 retval = cfs_access_process_vm(current, mm, addr, buffer,
222                                                this_len, 0);
223                 if (retval < 0)
224                         GOTO(out, rc = retval);
225                 else if (retval != this_len)
226                         break;
227
228                 addr += retval;
229
230                 /* Parse the buffer to find out the specified key/value pair.
231                  * The "key=value" entries are separated by '\0'. */
232                 env_start = buffer;
233                 scan_len = this_len;
234                 while (scan_len) {
235                         char *entry;
236                         int entry_len;
237
238                         env_end = memscan(env_start, '\0', scan_len);
239                         LASSERT(env_end >= env_start &&
240                                 env_end <= env_start + scan_len);
241
242                         /* The last entry of this buffer cross the buffer
243                          * boundary, reread it in next cycle. */
244                         if (unlikely(env_end - env_start == scan_len)) {
245                                 /* Just skip the entry larger than page size,
246                                  * it can't be jobID env variable. */
247                                 if (unlikely(scan_len == this_len))
248                                         skip = true;
249                                 else
250                                         addr -= scan_len;
251                                 break;
252                         } else if (unlikely(skip)) {
253                                 skip = false;
254                                 goto skip;
255                         }
256
257                         entry = env_start;
258                         entry_len = env_end - env_start;
259
260                         /* Key length + length of '=' */
261                         if (entry_len > key_len + 1 &&
262                             !memcmp(entry, key, key_len)) {
263                                 entry += key_len + 1;
264                                 entry_len -= key_len + 1;
265                                 /* The 'value' buffer passed in is too small.*/
266                                 if (entry_len >= *val_len)
267                                         GOTO(out, rc = -EOVERFLOW);
268
269                                 memcpy(value, entry, entry_len);
270                                 *val_len = entry_len;
271                                 GOTO(out, rc = 0);
272                         }
273 skip:
274                         scan_len -= (env_end - env_start + 1);
275                         env_start = env_end + 1;
276                 }
277         }
278         GOTO(out, rc = -ENOENT);
279
280 out:
281         mmput(mm);
282         kfree((void *)buffer);
283         return rc;
284 }
285 EXPORT_SYMBOL(cfs_get_environ);
286
287 EXPORT_SYMBOL(cfs_cap_raise);
288 EXPORT_SYMBOL(cfs_cap_lower);
289 EXPORT_SYMBOL(cfs_cap_raised);
290 EXPORT_SYMBOL(cfs_curproc_cap_pack);
291 EXPORT_SYMBOL(cfs_capable);
292
293 /*
294  * Local variables:
295  * c-indentation-style: "K&R"
296  * c-basic-offset: 8
297  * tab-width: 8
298  * fill-column: 80
299  * scroll-step: 1
300  * End:
301  */