Whamcloud - gitweb
LU-9859 libcfs: move remaining code from linux-module.c to module.c
[fs/lustre-release.git] / libcfs / libcfs / linux / linux-curproc.c
index f0e36a1..70510c9 100644 (file)
  *
  * You should have received a copy of the GNU General Public License
  * version 2 along with this program; If not, see
- * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
- *
- * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
- * CA 95054 USA or visit www.sun.com if you need additional information or
- * have any questions.
+ * http://www.gnu.org/licenses/gpl-2.0.html
  *
  * GPL HEADER END
  */
@@ -27,7 +23,7 @@
  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
  *
- * Copyright (c) 2012, 2014, Intel Corporation.
+ * Copyright (c) 2012, 2017, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
  */
 
 #include <linux/sched.h>
+#ifdef HAVE_SCHED_HEADERS
+#include <linux/sched/signal.h>
+#include <linux/sched/mm.h>
+#endif
 #include <linux/fs_struct.h>
-
+#include <linux/pagemap.h>
 #include <linux/compat.h>
 #include <linux/thread_info.h>
 
 #define cfs_cap_pack(cap) (cap)
 #define cfs_cap_unpack(cap) (cap)
 
-void cfs_cap_raise(cfs_cap_t cap)
-{
-        struct cred *cred;
-        if ((cred = prepare_creds())) {
-                cap_raise(cred->cap_effective, cfs_cap_unpack(cap));
-                commit_creds(cred);
-        }
-}
-
-void cfs_cap_lower(cfs_cap_t cap)
-{
-        struct cred *cred;
-        if ((cred = prepare_creds())) {
-                cap_lower(cred->cap_effective, cfs_cap_unpack(cap));
-                commit_creds(cred);
-        }
-}
-
-int cfs_cap_raised(cfs_cap_t cap)
-{
-        return cap_raised(current_cap(), cfs_cap_unpack(cap));
-}
-
 static void cfs_kernel_cap_pack(kernel_cap_t kcap, cfs_cap_t *cap)
 {
 #if defined (_LINUX_CAPABILITY_VERSION) && _LINUX_CAPABILITY_VERSION == 0x19980330
@@ -153,8 +130,13 @@ static int cfs_access_process_vm(struct task_struct *tsk,
                int bytes, rc, offset;
                void *maddr;
 
-               rc = get_user_pages(tsk, mm, addr, 1,
-                                    write, 1, &page, &vma);
+#if defined(HAVE_GET_USER_PAGES_GUP_FLAGS)
+               rc = get_user_pages(addr, 1, write ? FOLL_WRITE : 0, &page, &vma);
+#elif defined(HAVE_GET_USER_PAGES_6ARG)
+               rc = get_user_pages(addr, 1, write, 1, &page, &vma);
+#else
+               rc = get_user_pages(tsk, mm, addr, 1, write, 1, &page, &vma);
+#endif
                if (rc <= 0)
                        break;
 
@@ -173,7 +155,7 @@ static int cfs_access_process_vm(struct task_struct *tsk,
                                            buf, maddr + offset, bytes);
                }
                kunmap(page);
-               page_cache_release(page);
+               put_page(page);
                len -= bytes;
                buf += bytes;
                addr += bytes;
@@ -188,10 +170,11 @@ int cfs_get_environ(const char *key, char *value, int *val_len)
 {
        struct mm_struct *mm;
        char *buffer;
-       int buf_len = PAGE_CACHE_SIZE;
+       int buf_len = PAGE_SIZE;
        int key_len = strlen(key);
        unsigned long addr;
        int rc;
+       bool skip = false;
        ENTRY;
 
        buffer = kmalloc(buf_len, GFP_USER);
@@ -236,13 +219,16 @@ int cfs_get_environ(const char *key, char *value, int *val_len)
                        /* The last entry of this buffer cross the buffer
                         * boundary, reread it in next cycle. */
                        if (unlikely(env_end - env_start == scan_len)) {
-                               /* This entry is too large to fit in buffer */
-                               if (unlikely(scan_len == this_len)) {
-                                       CERROR("Too long env variable.\n");
-                                       GOTO(out, rc = -EINVAL);
-                               }
-                               addr -= scan_len;
+                               /* Just skip the entry larger than page size,
+                                * it can't be jobID env variable. */
+                               if (unlikely(scan_len == this_len))
+                                       skip = true;
+                               else
+                                       addr -= scan_len;
                                break;
+                       } else if (unlikely(skip)) {
+                               skip = false;
+                               goto skip;
                        }
 
                        entry = env_start;
@@ -253,15 +239,20 @@ int cfs_get_environ(const char *key, char *value, int *val_len)
                            !memcmp(entry, key, key_len)) {
                                entry += key_len + 1;
                                entry_len -= key_len + 1;
-                               /* The 'value' buffer passed in is too small.*/
-                               if (entry_len >= *val_len)
+
+                               /* The 'value' buffer passed in is too small.
+                                * Copy what fits, but return -EOVERFLOW. */
+                               if (entry_len >= *val_len) {
+                                       memcpy(value, entry, *val_len);
+                                       value[*val_len - 1] = 0;
                                        GOTO(out, rc = -EOVERFLOW);
+                               }
 
                                memcpy(value, entry, entry_len);
                                *val_len = entry_len;
                                GOTO(out, rc = 0);
                        }
-
+skip:
                        scan_len -= (env_end - env_start + 1);
                        env_start = env_end + 1;
                }
@@ -275,11 +266,7 @@ out:
 }
 EXPORT_SYMBOL(cfs_get_environ);
 
-EXPORT_SYMBOL(cfs_cap_raise);
-EXPORT_SYMBOL(cfs_cap_lower);
-EXPORT_SYMBOL(cfs_cap_raised);
 EXPORT_SYMBOL(cfs_curproc_cap_pack);
-EXPORT_SYMBOL(cfs_curproc_cap_unpack);
 EXPORT_SYMBOL(cfs_capable);
 
 /*