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