Whamcloud - gitweb
LU-3817 llite: Truncate to restore file
[fs/lustre-release.git] / lustre / mdt / mdt_idmap.c
index 162a02f..bc3e85f 100644 (file)
@@ -1,6 +1,4 @@
-/* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
- * vim:expandtab:shiftwidth=8:tabstop=8:
- *
+/*
  * GPL HEADER START
  *
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  * GPL HEADER END
  */
 /*
- * Copyright  2008 Sun Microsystems, Inc. All rights reserved
+ * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
+ *
+ * Copyright (c) 2012, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
  * Author: Fan Yong <fanyong@clusterfs.com>
  */
 
-#ifndef EXPORT_SYMTAB
-#define EXPORT_SYMTAB
-#endif
 #define DEBUG_SUBSYSTEM S_MDS
 
-#ifndef AUTOCONF_INCLUDED
-#include <linux/config.h>
-#endif
 #include <linux/module.h>
 #include <linux/kernel.h>
 #include <linux/mm.h>
@@ -56,7 +50,6 @@
 #include <linux/errno.h>
 #include <linux/version.h>
 #include <linux/unistd.h>
-#include <asm/system.h>
 #include <asm/uaccess.h>
 #include <linux/fs.h>
 #include <linux/stat.h>
@@ -64,6 +57,7 @@
 #include <linux/slab.h>
 
 #include <libcfs/libcfs.h>
+#include <libcfs/lucache.h>
 #include <obd.h>
 #include <obd_class.h>
 #include <obd_support.h>
@@ -72,7 +66,6 @@
 #include <lustre_dlm.h>
 #include <lustre_sec.h>
 #include <lustre_lib.h>
-#include <lustre_ucache.h>
 
 #include "mdt_internal.h"
 
@@ -82,101 +75,105 @@ do {                                                                    \
                                       OBD_CONNECT_RMT_CLIENT_FORCE |    \
                                       OBD_CONNECT_MDS_CAPA |            \
                                       OBD_CONNECT_OSS_CAPA);            \
-        spin_lock(&exp->exp_lock);                                      \
-        exp->exp_connect_flags = reply->ocd_connect_flags;              \
-        spin_unlock(&exp->exp_lock);                                    \
 } while (0)
 
 int mdt_init_sec_level(struct mdt_thread_info *info)
 {
-        struct mdt_device *mdt = info->mti_mdt;
-        struct ptlrpc_request *req = mdt_info_req(info);
-        char *client = libcfs_nid2str(req->rq_peer.nid);
-        struct obd_export *exp = req->rq_export;
-        struct obd_device *obd = exp->exp_obd;
-        struct obd_connect_data *data, *reply;
-        int rc = 0, remote;
-        ENTRY;
-
-        data = req_capsule_client_get(info->mti_pill, &RMF_CONNECT_DATA);
-        reply = req_capsule_server_get(info->mti_pill, &RMF_CONNECT_DATA);
-        if (data == NULL || reply == NULL)
-                RETURN(-EFAULT);
-
-        /* connection from MDT is always trusted */
-        if (req->rq_auth_usr_mdt) {
-                mdt_init_sec_none(reply, exp);
-                RETURN(0);
-        }
-
-        /* no GSS support case */
-        if (!req->rq_auth_gss) {
-                if (mdt->mdt_sec_level > LUSTRE_SEC_NONE) {
-                        CWARN("client %s -> target %s does not user GSS, "
-                              "can not run under security level %d.\n",
-                              client, obd->obd_name, mdt->mdt_sec_level);
-                        RETURN(-EACCES);
-                } else {
-                        mdt_init_sec_none(reply, exp);
-                        RETURN(0);
-                }
-        }
-
-        /* old version case */
-        if (unlikely(!(data->ocd_connect_flags & OBD_CONNECT_RMT_CLIENT) ||
-                     !(data->ocd_connect_flags & OBD_CONNECT_MDS_CAPA) ||
-                     !(data->ocd_connect_flags & OBD_CONNECT_OSS_CAPA))) {
-                if (mdt->mdt_sec_level > LUSTRE_SEC_NONE) {
-                        CWARN("client %s -> target %s uses old version, "
-                              "can not run under security level %d.\n",
-                              client, obd->obd_name, mdt->mdt_sec_level);
-                        RETURN(-EACCES);
-                } else {
-                        CWARN("client %s -> target %s uses old version, "
-                              "run under security level %d.\n",
-                              client, obd->obd_name, mdt->mdt_sec_level);
-                        mdt_init_sec_none(reply, exp);
-                        RETURN(0);
-                }
-        }
-
-        remote = data->ocd_connect_flags & OBD_CONNECT_RMT_CLIENT_FORCE;
-        if (remote) {
-                if (!req->rq_auth_remote)
-                        CDEBUG(D_SEC, "client (local realm) %s -> target %s "
-                               "asked to be remote.\n", client, obd->obd_name);
-        } else if (req->rq_auth_remote) {
-                remote = 1;
-                CDEBUG(D_SEC, "client (remote realm) %s -> target %s is set "
-                       "as remote by default.\n", client, obd->obd_name);
-        }
-
-        if (remote) {
-                if (!mdt->mdt_opts.mo_oss_capa) {
-                        CDEBUG(D_SEC, "client %s -> target %s is set as remote,"
-                               " but OSS capabilities are not enabled: %d.\n",
-                               client, obd->obd_name, mdt->mdt_opts.mo_oss_capa);
-                        RETURN(-EACCES);
-                }
-        } else {
-                if (req->rq_auth_uid == INVALID_UID) {
-                        CDEBUG(D_SEC, "client %s -> target %s: user is not "
-                               "authenticated!\n", client, obd->obd_name);
-                        RETURN(-EACCES);
-                }
-        }
-
-        switch (mdt->mdt_sec_level) {
-        case LUSTRE_SEC_NONE:
-                if (!remote) {
-                        mdt_init_sec_none(reply, exp);
-                        break;
-                } else {
-                        CDEBUG(D_SEC, "client %s -> target %s is set as remote, "
-                               "can not run under security level %d.\n",
-                               client, obd->obd_name, mdt->mdt_sec_level);
-                        RETURN(-EACCES);
-                }
+       struct mdt_device *mdt = info->mti_mdt;
+       struct ptlrpc_request *req = mdt_info_req(info);
+       char *client = libcfs_nid2str(req->rq_peer.nid);
+       struct obd_connect_data *data, *reply;
+       int rc = 0, remote;
+       ENTRY;
+
+       data = req_capsule_client_get(info->mti_pill, &RMF_CONNECT_DATA);
+       reply = req_capsule_server_get(info->mti_pill, &RMF_CONNECT_DATA);
+       if (data == NULL || reply == NULL)
+               RETURN(-EFAULT);
+
+       /* connection from MDT is always trusted */
+       if (req->rq_auth_usr_mdt) {
+               mdt_init_sec_none(reply, exp);
+               RETURN(0);
+       }
+
+       /* no GSS support case */
+       if (!req->rq_auth_gss) {
+               if (mdt->mdt_sec_level > LUSTRE_SEC_NONE) {
+                       CWARN("%s: client %s -> target %s does not user GSS, "
+                             "can not run under security level %d.\n",
+                             mdt_obd_name(mdt), client, mdt_obd_name(mdt),
+                             mdt->mdt_sec_level);
+                       RETURN(-EACCES);
+               } else {
+                       mdt_init_sec_none(reply, exp);
+                       RETURN(0);
+               }
+       }
+
+       /* old version case */
+       if (unlikely(!(data->ocd_connect_flags & OBD_CONNECT_RMT_CLIENT) ||
+                    !(data->ocd_connect_flags & OBD_CONNECT_MDS_CAPA) ||
+                    !(data->ocd_connect_flags & OBD_CONNECT_OSS_CAPA))) {
+               if (mdt->mdt_sec_level > LUSTRE_SEC_NONE) {
+                       CWARN("%s: client %s -> target %s uses old version, "
+                             "can not run under security level %d.\n",
+                             mdt_obd_name(mdt), client, mdt_obd_name(mdt),
+                             mdt->mdt_sec_level);
+                       RETURN(-EACCES);
+               } else {
+                       CWARN("%s: client %s -> target %s uses old version, "
+                             "run under security level %d.\n",
+                             mdt_obd_name(mdt), client, mdt_obd_name(mdt),
+                             mdt->mdt_sec_level);
+                       mdt_init_sec_none(reply, exp);
+                       RETURN(0);
+               }
+       }
+
+       remote = data->ocd_connect_flags & OBD_CONNECT_RMT_CLIENT_FORCE;
+       if (remote) {
+               if (!req->rq_auth_remote)
+                       CDEBUG(D_SEC, "%s: client (local realm) %s -> "
+                              "target %s asked to be remote.\n",
+                              mdt_obd_name(mdt), client, mdt_obd_name(mdt));
+       } else if (req->rq_auth_remote) {
+               remote = 1;
+               CDEBUG(D_SEC, "%s: client (remote realm) %s -> "
+                      "target %s is set as remote by default.\n",
+                      mdt_obd_name(mdt), client, mdt_obd_name(mdt));
+       }
+
+       if (remote) {
+               if (!mdt->mdt_opts.mo_oss_capa) {
+                       CDEBUG(D_SEC, "%s: client %s -> target %s is set as "
+                              "remote,but OSS capabilities are not enabled: "
+                              "%d.\n",
+                              mdt_obd_name(mdt), client, mdt_obd_name(mdt),
+                              mdt->mdt_opts.mo_oss_capa);
+                       RETURN(-EACCES);
+               }
+       } else {
+               if (req->rq_auth_uid == INVALID_UID) {
+                       CDEBUG(D_SEC, "%s: client %s -> target %s: user is not "
+                              "authenticated!\n",
+                              mdt_obd_name(mdt), client, mdt_obd_name(mdt));
+                       RETURN(-EACCES);
+               }
+       }
+
+       switch (mdt->mdt_sec_level) {
+       case LUSTRE_SEC_NONE:
+               if (!remote) {
+                       mdt_init_sec_none(reply, exp);
+                       break;
+               } else {
+                       CDEBUG(D_SEC, "%s: client %s -> target %s is set as "
+                              "remote, can not run under security level %d.\n",
+                              mdt_obd_name(mdt), client, mdt_obd_name(mdt),
+                              mdt->mdt_sec_level);
+                       RETURN(-EACCES);
+               }
         case LUSTRE_SEC_REMOTE:
                 if (!remote)
                         mdt_init_sec_none(reply, exp);
@@ -189,10 +186,6 @@ int mdt_init_sec_level(struct mdt_thread_info *info)
                                 reply->ocd_connect_flags &= ~OBD_CONNECT_MDS_CAPA;
                         if (!mdt->mdt_opts.mo_oss_capa)
                                 reply->ocd_connect_flags &= ~OBD_CONNECT_OSS_CAPA;
-
-                        spin_lock(&exp->exp_lock);
-                        exp->exp_connect_flags = reply->ocd_connect_flags;
-                        spin_unlock(&exp->exp_lock);
                 }
                 break;
         default:
@@ -204,51 +197,53 @@ int mdt_init_sec_level(struct mdt_thread_info *info)
 
 int mdt_init_idmap(struct mdt_thread_info *info)
 {
-        struct ptlrpc_request *req = mdt_info_req(info);
-        struct mdt_export_data *med = mdt_req2med(req);
-        struct obd_export *exp = req->rq_export;
-        char *client = libcfs_nid2str(req->rq_peer.nid);
-        struct obd_device *obd = exp->exp_obd;
-        int rc = 0;
-        ENTRY;
-
-        if (exp_connect_rmtclient(exp)) {
-                down(&med->med_idmap_sem);
-                if (!med->med_idmap)
-                        med->med_idmap = lustre_idmap_init();
-                up(&med->med_idmap_sem);
-
-                if (IS_ERR(med->med_idmap)) {
-                        long err = PTR_ERR(med->med_idmap);
-
-                        med->med_idmap = NULL;
-                        CERROR("client %s -> target %s "
-                               "failed to init idmap [%ld]!\n",
-                               client, obd->obd_name, err);
-                        RETURN(err);
-                } else if (!med->med_idmap) {
-                        CERROR("client %s -> target %s "
-                               "failed to init(2) idmap!\n",
-                               client, obd->obd_name);
-                        RETURN(-ENOMEM);
-                }
-
-                CDEBUG(D_SEC, "client %s -> target %s is remote.\n",
-                       client, obd->obd_name);
-                /* NB, MDS_CONNECT establish root idmap too! */
-                rc = mdt_handle_idmap(info);
-        }
-        RETURN(rc);
+       struct ptlrpc_request *req = mdt_info_req(info);
+       struct mdt_export_data *med = mdt_req2med(req);
+       struct obd_export *exp = req->rq_export;
+       char *client = libcfs_nid2str(req->rq_peer.nid);
+       int rc = 0;
+       ENTRY;
+
+       if (exp_connect_rmtclient(exp)) {
+               mutex_lock(&med->med_idmap_mutex);
+               if (!med->med_idmap)
+                       med->med_idmap = lustre_idmap_init();
+               mutex_unlock(&med->med_idmap_mutex);
+
+               if (IS_ERR(med->med_idmap)) {
+                       long err = PTR_ERR(med->med_idmap);
+
+                       med->med_idmap = NULL;
+                       CERROR("%s: client %s -> target %s "
+                              "failed to init idmap [%ld]!\n",
+                              mdt_obd_name(info->mti_mdt), client,
+                              mdt_obd_name(info->mti_mdt), err);
+                       RETURN(err);
+               } else if (!med->med_idmap) {
+                       CERROR("%s: client %s -> target %s "
+                              "failed to init(2) idmap!\n",
+                              mdt_obd_name(info->mti_mdt), client,
+                              mdt_obd_name(info->mti_mdt));
+                       RETURN(-ENOMEM);
+               }
+
+               CDEBUG(D_SEC, "%s: client %s -> target %s is remote.\n",
+                       mdt_obd_name(info->mti_mdt), client,
+                       mdt_obd_name(info->mti_mdt));
+               /* NB, MDS_CONNECT establish root idmap too! */
+               rc = mdt_handle_idmap(info);
+       }
+       RETURN(rc);
 }
 
 void mdt_cleanup_idmap(struct mdt_export_data *med)
 {
-        down(&med->med_idmap_sem);
+       mutex_lock(&med->med_idmap_mutex);
         if (med->med_idmap != NULL) {
                 lustre_idmap_fini(med->med_idmap);
                 med->med_idmap = NULL;
         }
-        up(&med->med_idmap_sem);
+       mutex_unlock(&med->med_idmap_mutex);
 }
 
 static inline void mdt_revoke_export_locks(struct obd_export *exp)
@@ -403,7 +398,7 @@ int ptlrpc_user_desc_do_idmap(struct ptlrpc_request *req,
 void mdt_body_reverse_idmap(struct mdt_thread_info *info, struct mdt_body *body)
 {
         struct ptlrpc_request     *req = mdt_info_req(info);
-        struct md_ucred           *uc = mdt_ucred(info);
+       struct lu_ucred           *uc = mdt_ucred(info);
         struct mdt_export_data    *med = mdt_req2med(req);
         struct lustre_idmap_table *idmap = med->med_idmap;
 
@@ -441,21 +436,21 @@ void mdt_body_reverse_idmap(struct mdt_thread_info *info, struct mdt_body *body)
 int mdt_fix_attr_ucred(struct mdt_thread_info *info, __u32 op)
 {
         struct ptlrpc_request     *req = mdt_info_req(info);
-        struct md_ucred           *uc = mdt_ucred(info);
+       struct lu_ucred           *uc = mdt_ucred_check(info);
         struct lu_attr            *attr = &info->mti_attr.ma_attr;
         struct mdt_export_data    *med = mdt_req2med(req);
         struct lustre_idmap_table *idmap = med->med_idmap;
 
-        if ((uc->mu_valid != UCRED_OLD) && (uc->mu_valid != UCRED_NEW))
-                return -EINVAL;
+       if (uc == NULL)
+               return -EINVAL;
 
         if (op != REINT_SETATTR) {
-                if ((attr->la_valid & LA_UID) && (attr->la_uid != -1))
-                        attr->la_uid = uc->mu_fsuid;
-                /* for S_ISGID, inherit gid from his parent, such work will be
-                 * done in cmm/mdd layer, here set all cases as uc->mu_fsgid. */
-                if ((attr->la_valid & LA_GID) && (attr->la_gid != -1))
-                        attr->la_gid = uc->mu_fsgid;
+               if ((attr->la_valid & LA_UID) && (attr->la_uid != -1))
+                       attr->la_uid = uc->uc_fsuid;
+               /* for S_ISGID, inherit gid from his parent, such work will be
+                * done in cmm/mdd layer, here set all cases as uc->uc_fsgid. */
+               if ((attr->la_valid & LA_GID) && (attr->la_gid != -1))
+                       attr->la_gid = uc->uc_fsgid;
         } else if (exp_connect_rmtclient(info->mti_exp)) {
                 /* NB: -1 case will be handled by mdt_fix_attr() later. */
                 if ((attr->la_valid & LA_UID) && (attr->la_uid != -1)) {