Whamcloud - gitweb
LU-15910 llite: use max default EA size to get default LMV
[fs/lustre-release.git] / lustre / llite / dir.c
index 79b329b..1892c65 100644 (file)
@@ -27,7 +27,6 @@
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
- * Lustre is a trademark of Sun Microsystems, Inc.
  *
  * lustre/llite/dir.c
  *
@@ -55,6 +54,7 @@
 #include <lustre_fid.h>
 #include <lustre_kernelcomm.h>
 #include <lustre_swab.h>
+#include <libcfs/libcfs_crypto.h>
 
 #include "llite_internal.h"
 
  * returned page, page hash collision has to be handled. Pages in the
  * hash chain, except first one, are termed "overflow pages".
  *
- * Solution to index uniqueness problem is to not cache overflow
- * pages. Instead, when page hash collision is detected, all overflow pages
- * from emerging chain are immediately requested from the server and placed in
- * a special data structure (struct ll_dir_chain). This data structure is used
- * by ll_readdir() to process entries from overflow pages. When readdir
- * invocation finishes, overflow pages are discarded. If page hash collision
- * chain weren't completely processed, next call to readdir will again detect
- * page hash collision, again read overflow pages in, process next portion of
- * entries and again discard the pages. This is not as wasteful as it looks,
- * because, given reasonable hash, page hash collisions are extremely rare.
+ * Proposed (unimplimented) solution to index uniqueness problem is to
+ * not cache overflow pages.  Instead, when page hash collision is
+ * detected, all overflow pages from emerging chain should be
+ * immediately requested from the server and placed in a special data
+ * structure.  This data structure can be used by ll_readdir() to
+ * process entries from overflow pages.  When readdir invocation
+ * finishes, overflow pages are discarded.  If page hash collision chain
+ * weren't completely processed, next call to readdir will again detect
+ * page hash collision, again read overflow pages in, process next
+ * portion of entries and again discard the pages.  This is not as
+ * wasteful as it looks, because, given reasonable hash, page hash
+ * collisions are extremely rare.
  *
  * 1. directory positioning
  *
  *
  */
 struct page *ll_get_dir_page(struct inode *dir, struct md_op_data *op_data,
-                            __u64 offset, struct ll_dir_chain *chain)
+                            __u64 offset, int *partial_readdir_rc)
 {
-       struct md_callback      cb_op;
-       struct page             *page;
-       int                     rc;
+       struct md_readdir_info mrinfo = {
+                                       .mr_blocking_ast = ll_md_blocking_ast };
+       struct page *page;
+       int rc;
 
-       cb_op.md_blocking_ast = ll_md_blocking_ast;
-       rc = md_read_page(ll_i2mdexp(dir), op_data, &cb_op, offset, &page);
+       rc = md_read_page(ll_i2mdexp(dir), op_data, &mrinfo, offset, &page);
        if (rc != 0)
                return ERR_PTR(rc);
 
+       if (partial_readdir_rc && mrinfo.mr_partial_readdir_rc)
+               *partial_readdir_rc = mrinfo.mr_partial_readdir_rc;
+
        return page;
 }
 
@@ -161,7 +166,8 @@ void ll_release_page(struct inode *inode, struct page *page,
 
        /* Always remove the page for striped dir, because the page is
         * built from temporarily in LMV layer */
-       if (inode && ll_dir_striped(inode)) {
+       if (inode && S_ISDIR(inode->i_mode) &&
+           lmv_dir_striped(ll_i2info(inode)->lli_lsm_md)) {
                __free_page(page);
                return;
        }
@@ -175,51 +181,32 @@ void ll_release_page(struct inode *inode, struct page *page,
        put_page(page);
 }
 
-/**
- * return IF_* type for given lu_dirent entry.
- * IF_* flag shld be converted to particular OS file type in
- * platform llite module.
- */
-static u16 ll_dirent_type_get(struct lu_dirent *ent)
-{
-       u16 type = 0;
-       struct luda_type *lt;
-       int len = 0;
-
-       if (le32_to_cpu(ent->lde_attrs) & LUDA_TYPE) {
-               const unsigned align = sizeof(struct luda_type) - 1;
-
-               len = le16_to_cpu(ent->lde_namelen);
-               len = (len + align) & ~align;
-               lt = (void *)ent->lde_name + len;
-               type = IFTODT(le16_to_cpu(lt->lt_type));
-       }
-
-       return type;
-}
-
 #ifdef HAVE_DIR_CONTEXT
 int ll_dir_read(struct inode *inode, __u64 *ppos, struct md_op_data *op_data,
-               struct dir_context *ctx)
+               struct dir_context *ctx, int *partial_readdir_rc)
 {
 #else
 int ll_dir_read(struct inode *inode, __u64 *ppos, struct md_op_data *op_data,
-               void *cookie, filldir_t filldir)
+               void *cookie, filldir_t filldir, int *partial_readdir_rc)
 {
 #endif
-       struct ll_sb_info    *sbi        = ll_i2sbi(inode);
-       __u64                 pos        = *ppos;
-       bool                  is_api32 = ll_need_32bit_api(sbi);
-       bool                  is_hash64 = sbi->ll_flags & LL_SBI_64BIT_HASH;
-       struct page          *page;
-       struct ll_dir_chain   chain;
-       bool                  done = false;
-       int                   rc = 0;
+       struct ll_sb_info *sbi = ll_i2sbi(inode);
+       __u64 pos = *ppos;
+       bool is_api32 = ll_need_32bit_api(sbi);
+       bool is_hash64 = test_bit(LL_SBI_64BIT_HASH, sbi->ll_flags);
+       struct page *page;
+       bool done = false;
+       struct llcrypt_str lltr = LLTR_INIT(NULL, 0);
+       int rc = 0;
        ENTRY;
 
-       ll_dir_chain_init(&chain);
+       if (IS_ENCRYPTED(inode)) {
+               rc = llcrypt_fname_alloc_buffer(inode, NAME_MAX, &lltr);
+               if (rc < 0)
+                       RETURN(rc);
+       }
 
-       page = ll_get_dir_page(inode, op_data, pos, &chain);
+       page = ll_get_dir_page(inode, op_data, pos, partial_readdir_rc);
 
        while (rc == 0 && !done) {
                struct lu_dirpage *dp;
@@ -256,15 +243,37 @@ int ll_dir_read(struct inode *inode, __u64 *ppos, struct md_op_data *op_data,
                                lhash = hash;
                        fid_le_to_cpu(&fid, &ent->lde_fid);
                        ino = cl_fid_build_ino(&fid, is_api32);
-                       type = ll_dirent_type_get(ent);
+                       type = S_DT(lu_dirent_type_get(ent));
                        /* For ll_nfs_get_name_filldir(), it will try to access
                         * 'ent' through 'lde_name', so the parameter 'name'
                         * for 'filldir()' must be part of the 'ent'. */
 #ifdef HAVE_DIR_CONTEXT
                        ctx->pos = lhash;
-                       done = !dir_emit(ctx, ent->lde_name, namelen, ino,
-                                        type);
+                       if (!IS_ENCRYPTED(inode)) {
+                               done = !dir_emit(ctx, ent->lde_name, namelen,
+                                                ino, type);
+                       } else {
+                               /* Directory is encrypted */
+                               int save_len = lltr.len;
+                               struct llcrypt_str de_name =
+                                       LLTR_INIT(ent->lde_name, namelen);
+
+                               rc = ll_fname_disk_to_usr(inode, 0, 0, &de_name,
+                                                         &lltr, &fid);
+                               de_name = lltr;
+                               lltr.len = save_len;
+                               if (rc) {
+                                       done = 1;
+                                       break;
+                               }
+                               done = !dir_emit(ctx, de_name.name, de_name.len,
+                                                ino, type);
+                       }
 #else
+                       /* HAVE_DIR_CONTEXT is defined from kernel 3.11, whereas
+                        * IS_ENCRYPTED is brought by kernel 4.14.
+                        * So there is no need to handle encryption case here.
+                        */
                        done = filldir(cookie, ent->lde_name, namelen, lhash,
                                       ino, type);
 #endif
@@ -294,7 +303,7 @@ int ll_dir_read(struct inode *inode, __u64 *ppos, struct md_op_data *op_data,
                                        LDF_COLLIDE);
                        next = pos;
                        page = ll_get_dir_page(inode, op_data, pos,
-                                              &chain);
+                                              partial_readdir_rc);
                }
        }
 #ifdef HAVE_DIR_CONTEXT
@@ -302,7 +311,7 @@ int ll_dir_read(struct inode *inode, __u64 *ppos, struct md_op_data *op_data,
 #else
        *ppos = pos;
 #endif
-       ll_dir_chain_fini(&chain);
+       llcrypt_fname_free_buffer(&lltr);
        RETURN(rc);
 }
 
@@ -313,26 +322,34 @@ static int ll_readdir(struct file *filp, void *cookie, filldir_t filldir)
 #endif
 {
        struct inode *inode = file_inode(filp);
-       struct ll_file_data *lfd = LUSTRE_FPRIVATE(filp);
+       struct ll_file_data *lfd = filp->private_data;
        struct ll_sb_info *sbi = ll_i2sbi(inode);
-       int hash64 = sbi->ll_flags & LL_SBI_64BIT_HASH;
+       bool hash64 = test_bit(LL_SBI_64BIT_HASH, sbi->ll_flags);
        int api32 = ll_need_32bit_api(sbi);
        struct md_op_data *op_data;
        struct lu_fid pfid = { 0 };
        ktime_t kstart = ktime_get();
+       /* result of possible partial readdir */
+       int partial_readdir_rc = 0;
        __u64 pos;
        int rc;
+
        ENTRY;
 
-       if (lfd != NULL)
-               pos = lfd->lfd_pos;
-       else
-               pos = 0;
+       LASSERT(lfd != NULL);
+       pos = lfd->lfd_pos;
 
-       CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p) pos/size"
-              "%lu/%llu 32bit_api %d\n", PFID(ll_inode2fid(inode)),
+       CDEBUG(D_VFSTRACE,
+              "VFS Op:inode="DFID"(%p) pos/size%lu/%llu 32bit_api %d\n",
+              PFID(ll_inode2fid(inode)),
               inode, (unsigned long)pos, i_size_read(inode), api32);
 
+       if (IS_ENCRYPTED(inode)) {
+               rc = llcrypt_get_encryption_info(inode);
+               if (rc && rc != -ENOKEY)
+                       GOTO(out, rc);
+       }
+
        if (pos == MDS_DIR_END_OFF)
                /*
                 * end-of-file.
@@ -379,13 +396,15 @@ static int ll_readdir(struct file *filp, void *cookie, filldir_t filldir)
 
 #ifdef HAVE_DIR_CONTEXT
        ctx->pos = pos;
-       rc = ll_dir_read(inode, &pos, op_data, ctx);
+       rc = ll_dir_read(inode, &pos, op_data, ctx, &partial_readdir_rc);
        pos = ctx->pos;
 #else
-       rc = ll_dir_read(inode, &pos, op_data, cookie, filldir);
+       rc = ll_dir_read(inode, &pos, op_data, cookie, filldir,
+                        &partial_readdir_rc);
 #endif
-       if (lfd != NULL)
-               lfd->lfd_pos = pos;
+       lfd->lfd_pos = pos;
+       if (!lfd->fd_partial_readdir_rc)
+               lfd->fd_partial_readdir_rc = partial_readdir_rc;
 
        if (pos == MDS_DIR_END_OFF) {
                if (api32)
@@ -411,27 +430,6 @@ out:
        RETURN(rc);
 }
 
-#if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 13, 53, 0)
-static int ll_send_mgc_param(struct obd_export *mgc, char *string)
-{
-        struct mgs_send_param *msp;
-        int rc = 0;
-
-        OBD_ALLOC_PTR(msp);
-        if (!msp)
-                return -ENOMEM;
-
-       strlcpy(msp->mgs_param, string, sizeof(msp->mgs_param));
-        rc = obd_set_info_async(NULL, mgc, sizeof(KEY_SET_INFO), KEY_SET_INFO,
-                                sizeof(struct mgs_send_param), msp, NULL);
-        if (rc)
-                CERROR("Failed to set parameter: %d\n", rc);
-        OBD_FREE_PTR(msp);
-
-        return rc;
-}
-#endif
-
 /**
  * Create striped directory with specified stripe(@lump)
  *
@@ -444,7 +442,8 @@ static int ll_send_mgc_param(struct obd_export *mgc, char *string)
  *                      <0 if the creation is failed.
  */
 static int ll_dir_setdirstripe(struct dentry *dparent, struct lmv_user_md *lump,
-                              size_t len, const char *dirname, umode_t mode)
+                              size_t len, const char *dirname, umode_t mode,
+                              bool createonly)
 {
        struct inode *parent = dparent->d_inode;
        struct ptlrpc_request *request = NULL;
@@ -460,17 +459,20 @@ static int ll_dir_setdirstripe(struct dentry *dparent, struct lmv_user_md *lump,
                                                  strlen(dirname)),
                },
        };
+       bool encrypt = false;
+       int hash_flags;
        int err;
-       ENTRY;
 
-       if (unlikely(!lmv_magic_supported(lump->lum_magic)))
+       ENTRY;
+       if (unlikely(!lmv_user_magic_supported(lump->lum_magic)))
                RETURN(-EINVAL);
 
        if (lump->lum_magic != LMV_MAGIC_FOREIGN) {
                CDEBUG(D_VFSTRACE,
-                      "VFS Op:inode="DFID"(%p) name %s stripe_offset %d, stripe_count: %u\n",
+                      "VFS Op:inode="DFID"(%p) name=%s stripe_offset=%d stripe_count=%u, hash_type=%x\n",
                       PFID(ll_inode2fid(parent)), parent, dirname,
-                      (int)lump->lum_stripe_offset, lump->lum_stripe_count);
+                      (int)lump->lum_stripe_offset, lump->lum_stripe_count,
+                      lump->lum_hash_type);
        } else {
                struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)lump;
 
@@ -488,7 +490,26 @@ static int ll_dir_setdirstripe(struct dentry *dparent, struct lmv_user_md *lump,
            !OBD_FAIL_CHECK(OBD_FAIL_LLITE_NO_CHECK_DEAD))
                RETURN(-ENOENT);
 
-       if (unlikely(!lmv_magic_supported(cpu_to_le32(lump->lum_magic))))
+       /* MDS < 2.14 doesn't support 'crush' hash type, and cannot handle
+        * unknown hash if client doesn't set a valid one. switch to fnv_1a_64.
+        */
+       if (CFS_FAIL_CHECK(OBD_FAIL_LMV_UNKNOWN_STRIPE)) {
+               lump->lum_hash_type = cfs_fail_val;
+       } else if (!(exp_connect_flags2(sbi->ll_md_exp) & OBD_CONNECT2_CRUSH)) {
+               enum lmv_hash_type type = lump->lum_hash_type &
+                                         LMV_HASH_TYPE_MASK;
+
+               if (type >= LMV_HASH_TYPE_CRUSH ||
+                   type == LMV_HASH_TYPE_UNKNOWN)
+                       lump->lum_hash_type = (lump->lum_hash_type ^ type) |
+                                             LMV_HASH_TYPE_FNV_1A_64;
+       }
+
+       hash_flags = lump->lum_hash_type & ~LMV_HASH_TYPE_MASK;
+       if (hash_flags & ~LMV_HASH_FLAG_KNOWN)
+               RETURN(-EINVAL);
+
+       if (unlikely(!lmv_user_magic_supported(cpu_to_le32(lump->lum_magic))))
                lustre_swab_lmv_user_md(lump);
 
        if (!IS_POSIXACL(parent) || !exp_connect_umask(ll_i2mdexp(parent)))
@@ -500,7 +521,20 @@ static int ll_dir_setdirstripe(struct dentry *dparent, struct lmv_user_md *lump,
        if (IS_ERR(op_data))
                RETURN(PTR_ERR(op_data));
 
-       if (sbi->ll_flags & LL_SBI_FILE_SECCTX) {
+       op_data->op_dir_depth = ll_i2info(parent)->lli_dir_depth;
+
+       if (ll_sbi_has_encrypt(sbi) &&
+           (IS_ENCRYPTED(parent) ||
+            unlikely(ll_sb_has_test_dummy_encryption(parent->i_sb)))) {
+               err = llcrypt_get_encryption_info(parent);
+               if (err)
+                       GOTO(out_op_data, err);
+               if (!llcrypt_has_encryption_key(parent))
+                       GOTO(out_op_data, err = -ENOKEY);
+               encrypt = true;
+       }
+
+       if (test_bit(LL_SBI_FILE_SECCTX, sbi->ll_flags)) {
                /* selinux_dentry_init_security() uses dentry->d_parent and name
                 * to determine the security context for the file. So our fake
                 * dentry should be real enough for this purpose. */
@@ -512,37 +546,54 @@ static int ll_dir_setdirstripe(struct dentry *dparent, struct lmv_user_md *lump,
                        GOTO(out_op_data, err);
        }
 
+       if (encrypt) {
+               err = llcrypt_inherit_context(parent, NULL, op_data, false);
+               if (err)
+                       GOTO(out_op_data, err);
+       }
+
        op_data->op_cli_flags |= CLI_SET_MEA;
+       if (createonly)
+               op_data->op_bias |= MDS_SETSTRIPE_CREATE;
+
        err = md_create(sbi->ll_md_exp, op_data, lump, len, mode,
                        from_kuid(&init_user_ns, current_fsuid()),
                        from_kgid(&init_user_ns, current_fsgid()),
-                       cfs_curproc_cap_pack(), 0, &request);
+                       current_cap(), 0, &request);
        if (err)
                GOTO(out_request, err);
 
        CFS_FAIL_TIMEOUT(OBD_FAIL_LLITE_SETDIRSTRIPE_PAUSE, cfs_fail_val);
 
-       err = ll_prep_inode(&inode, request, parent->i_sb, NULL);
+       err = ll_prep_inode(&inode, &request->rq_pill, parent->i_sb, NULL);
        if (err)
                GOTO(out_inode, err);
 
        dentry.d_inode = inode;
 
-       if (sbi->ll_flags & LL_SBI_FILE_SECCTX) {
-               inode_lock(inode);
+       if (test_bit(LL_SBI_FILE_SECCTX, sbi->ll_flags)) {
+               /* no need to protect selinux_inode_setsecurity() by
+                * inode_lock. Taking it would lead to a client deadlock
+                * LU-13617
+                */
                err = security_inode_notifysecctx(inode,
                                                  op_data->op_file_secctx,
                                                  op_data->op_file_secctx_size);
-               inode_unlock(inode);
        } else {
                err = ll_inode_init_security(&dentry, inode, parent);
        }
        if (err)
                GOTO(out_inode, err);
 
+       if (encrypt) {
+               err = ll_set_encflags(inode, op_data->op_file_encctx,
+                                     op_data->op_file_encctx_size, false);
+               if (err)
+                       GOTO(out_inode, err);
+       }
+
 out_inode:
-       if (inode != NULL)
-               iput(inode);
+       iput(inode);
 out_request:
        ptlrpc_req_finished(request);
 out_op_data:
@@ -558,10 +609,6 @@ int ll_dir_setstripe(struct inode *inode, struct lov_user_md *lump,
        struct md_op_data *op_data;
        struct ptlrpc_request *req = NULL;
        int rc = 0;
-#if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 13, 53, 0)
-       struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
-       struct obd_device *mgc = lsi->lsi_mgc;
-#endif
        int lum_size;
        ENTRY;
 
@@ -576,12 +623,29 @@ int ll_dir_setstripe(struct inode *inode, struct lov_user_md *lump,
                case LOV_USER_MAGIC_COMP_V1:
                        lum_size = ((struct lov_comp_md_v1 *)lump)->lcm_size;
                        break;
-               case LMV_USER_MAGIC:
-                       if (lump->lmm_magic != cpu_to_le32(LMV_USER_MAGIC))
-                               lustre_swab_lmv_user_md(
-                                       (struct lmv_user_md *)lump);
-                       lum_size = sizeof(struct lmv_user_md);
+               case LMV_USER_MAGIC: {
+                       struct lmv_user_md *lmv = (struct lmv_user_md *)lump;
+
+                       /* MDS < 2.14 doesn't support 'crush' hash type, and
+                        * cannot handle unknown hash if client doesn't set a
+                        * valid one. switch to fnv_1a_64.
+                        */
+                       if (!(exp_connect_flags2(sbi->ll_md_exp) &
+                             OBD_CONNECT2_CRUSH)) {
+                               enum lmv_hash_type type = lmv->lum_hash_type &
+                                                         LMV_HASH_TYPE_MASK;
+
+                               if (type >= LMV_HASH_TYPE_CRUSH ||
+                                   type == LMV_HASH_TYPE_UNKNOWN)
+                                       lmv->lum_hash_type =
+                                               (lmv->lum_hash_type ^ type) |
+                                               LMV_HASH_TYPE_FNV_1A_64;
+                       }
+                       if (lmv->lum_magic != cpu_to_le32(LMV_USER_MAGIC))
+                               lustre_swab_lmv_user_md(lmv);
+                       lum_size = sizeof(*lmv);
                        break;
+               }
                case LOV_USER_MAGIC_SPECIFIC: {
                        struct lov_user_md_v3 *v3 =
                                (struct lov_user_md_v3 *)lump;
@@ -623,81 +687,26 @@ int ll_dir_setstripe(struct inode *inode, struct lov_user_md *lump,
        if (rc)
                RETURN(rc);
 
-#if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 13, 53, 0)
-       /*
-        * 2.9 server has stored filesystem default stripe in ROOT xattr,
-        * and it's stored into system config for backward compatibility.
-        *
-        * In the following we use the fact that LOV_USER_MAGIC_V1 and
-        * LOV_USER_MAGIC_V3 have the same initial fields so we do not
-        * need the make the distiction between the 2 versions
-        */
-       if (set_default && mgc->u.cli.cl_mgc_mgsexp &&
-           (lump == NULL ||
-            le32_to_cpu(lump->lmm_magic) == LOV_USER_MAGIC_V1 ||
-            le32_to_cpu(lump->lmm_magic) == LOV_USER_MAGIC_V3)) {
-               char *param = NULL;
-               char *buf;
-
-               OBD_ALLOC(param, MGS_PARAM_MAXLEN);
-               if (param == NULL)
-                       GOTO(end, rc = -ENOMEM);
-
-               buf = param;
-               /* Get fsname and assume devname to be -MDT0000. */
-               snprintf(buf, MGS_PARAM_MAXLEN, "%s-MDT0000.lov",
-                        sbi->ll_fsname);
-               buf += strlen(buf);
-
-               /* Set root stripesize */
-               snprintf(buf, MGS_PARAM_MAXLEN, ".stripesize=%u",
-                        lump ? le32_to_cpu(lump->lmm_stripe_size) : 0);
-               rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
-               if (rc)
-                       GOTO(end, rc);
-
-               /* Set root stripecount */
-               snprintf(buf, MGS_PARAM_MAXLEN, ".stripecount=%hd",
-                        lump ? le16_to_cpu(lump->lmm_stripe_count) : 0);
-               rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
-               if (rc)
-                       GOTO(end, rc);
-
-               /* Set root stripeoffset */
-               snprintf(buf, MGS_PARAM_MAXLEN, ".stripeoffset=%hd",
-                        lump ? le16_to_cpu(lump->lmm_stripe_offset) :
-                               (typeof(lump->lmm_stripe_offset))(-1));
-               rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
-
-end:
-               if (param != NULL)
-                       OBD_FREE(param, MGS_PARAM_MAXLEN);
-       }
-#endif
        RETURN(rc);
 }
 
-static int ll_dir_get_default_layout(struct inode *inode, void **plmm,
-                                    int *plmm_size,
-                                    struct ptlrpc_request **request, u64 valid,
-                                    enum get_default_layout_type type)
+int ll_dir_get_default_layout(struct inode *inode, void **plmm, int *plmm_size,
+                             struct ptlrpc_request **request, u64 valid,
+                             enum get_default_layout_type type)
 {
        struct ll_sb_info *sbi = ll_i2sbi(inode);
        struct mdt_body   *body;
        struct lov_mds_md *lmm = NULL;
        struct ptlrpc_request *req = NULL;
-       int rc, lmm_size;
+       int lmm_size = OBD_MAX_DEFAULT_EA_SIZE;
        struct md_op_data *op_data;
        struct lu_fid fid;
-       ENTRY;
+       int rc;
 
-       rc = ll_get_default_mdsize(sbi, &lmm_size);
-       if (rc)
-               RETURN(rc);
+       ENTRY;
 
-       op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL,
-                                    0, lmm_size, LUSTRE_OPC_ANY,
-                                    NULL);
+       op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, lmm_size,
+                                    LUSTRE_OPC_ANY, NULL);
        if (IS_ERR(op_data))
                RETURN(PTR_ERR(op_data));
 
@@ -801,10 +810,13 @@ int ll_dir_getstripe_default(struct inode *inode, void **plmm, int *plmm_size,
        rc = ll_dir_get_default_layout(inode, (void **)&lmm, &lmm_size,
                                       &req, valid, 0);
        if (rc == -ENODATA && !fid_is_root(ll_inode2fid(inode)) &&
-           !(valid & (OBD_MD_MEA|OBD_MD_DEFAULT_MEA)) && root_request != NULL)
-               rc = ll_dir_get_default_layout(inode, (void **)&lmm, &lmm_size,
-                                              &root_req, valid,
-                                              GET_DEFAULT_LAYOUT_ROOT);
+           !(valid & OBD_MD_MEA) && root_request != NULL) {
+               int rc2 = ll_dir_get_default_layout(inode, (void **)&lmm,
+                                                   &lmm_size, &root_req, valid,
+                                                   GET_DEFAULT_LAYOUT_ROOT);
+               if (rc2 == 0)
+                       rc = 0;
+       }
 
        *plmm = lmm;
        *plmm_size = lmm_size;
@@ -1099,7 +1111,7 @@ static int copy_and_ct_start(int cmd, struct obd_export *exp,
 
                count = 0;
                for (i = 0; i < sizeof(archive_mask) * 8; i++) {
-                       if ((1 << i) & archive_mask) {
+                       if (BIT(i) & archive_mask) {
                                lk->lk_data[count] = i + 1;
                                count++;
                        }
@@ -1174,94 +1186,118 @@ static int check_owner(int type, int id)
        return 0;
 }
 
-static int quotactl_ioctl(struct ll_sb_info *sbi, struct if_quotactl *qctl)
+int quotactl_ioctl(struct super_block *sb, struct if_quotactl *qctl)
 {
-        int cmd = qctl->qc_cmd;
-        int type = qctl->qc_type;
-        int id = qctl->qc_id;
-        int valid = qctl->qc_valid;
-        int rc = 0;
-        ENTRY;
+       struct ll_sb_info *sbi = ll_s2sbi(sb);
+       int cmd = qctl->qc_cmd;
+       int type = qctl->qc_type;
+       int id = qctl->qc_id;
+       int valid = qctl->qc_valid;
+       int rc = 0;
+
+       ENTRY;
 
        switch (cmd) {
        case Q_SETQUOTA:
        case Q_SETINFO:
        case LUSTRE_Q_SETDEFAULT:
-               if (!cfs_capable(CFS_CAP_SYS_ADMIN))
+       case LUSTRE_Q_SETQUOTAPOOL:
+       case LUSTRE_Q_SETINFOPOOL:
+       case LUSTRE_Q_SETDEFAULT_POOL:
+       case LUSTRE_Q_DELETEQID:
+               if (!capable(CAP_SYS_ADMIN))
                        RETURN(-EPERM);
+
+               if (sb->s_flags & SB_RDONLY)
+                       RETURN(-EROFS);
                break;
        case Q_GETQUOTA:
        case LUSTRE_Q_GETDEFAULT:
+       case LUSTRE_Q_GETQUOTAPOOL:
+       case LUSTRE_Q_GETDEFAULT_POOL:
                if (check_owner(type, id) &&
-                   (!cfs_capable(CFS_CAP_SYS_ADMIN)))
+                   (!capable(CAP_SYS_ADMIN)))
                        RETURN(-EPERM);
                break;
        case Q_GETINFO:
+       case LUSTRE_Q_GETINFOPOOL:
                break;
        default:
                CERROR("unsupported quotactl op: %#x\n", cmd);
                RETURN(-ENOTSUPP);
        }
 
-        if (valid != QC_GENERAL) {
-                if (cmd == Q_GETINFO)
-                        qctl->qc_cmd = Q_GETOINFO;
-                else if (cmd == Q_GETQUOTA)
-                        qctl->qc_cmd = Q_GETOQUOTA;
-                else
-                        RETURN(-EINVAL);
-
-                switch (valid) {
-                case QC_MDTIDX:
-                        rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
-                                           sizeof(*qctl), qctl, NULL);
-                        break;
-                case QC_OSTIDX:
-                        rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_dt_exp,
-                                           sizeof(*qctl), qctl, NULL);
-                        break;
-                case QC_UUID:
-                        rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
-                                           sizeof(*qctl), qctl, NULL);
-                        if (rc == -EAGAIN)
-                                rc = obd_iocontrol(OBD_IOC_QUOTACTL,
-                                                   sbi->ll_dt_exp,
-                                                   sizeof(*qctl), qctl, NULL);
-                        break;
-                default:
-                        rc = -EINVAL;
-                        break;
-                }
+       if (valid != QC_GENERAL) {
+               if (cmd == Q_GETINFO)
+                       qctl->qc_cmd = Q_GETOINFO;
+               else if (cmd == Q_GETQUOTA ||
+                        cmd == LUSTRE_Q_GETQUOTAPOOL)
+                       qctl->qc_cmd = Q_GETOQUOTA;
+               else
+                       RETURN(-EINVAL);
 
-                if (rc)
-                        RETURN(rc);
+               switch (valid) {
+               case QC_MDTIDX:
+                       rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
+                                          sizeof(*qctl), qctl, NULL);
+                       break;
+               case QC_OSTIDX:
+                       rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_dt_exp,
+                                          sizeof(*qctl), qctl, NULL);
+                       break;
+               case QC_UUID:
+                       rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
+                                          sizeof(*qctl), qctl, NULL);
+                       if (rc == -EAGAIN)
+                               rc = obd_iocontrol(OBD_IOC_QUOTACTL,
+                                                  sbi->ll_dt_exp,
+                                                  sizeof(*qctl), qctl, NULL);
+                       break;
+               default:
+                       rc = -EINVAL;
+                       break;
+               }
 
-                qctl->qc_cmd = cmd;
-        } else {
-                struct obd_quotactl *oqctl;
+               qctl->qc_cmd = cmd;
+               if (rc)
+                       RETURN(rc);
+       } else {
+               struct obd_quotactl *oqctl;
+               int oqctl_len = sizeof(*oqctl);
 
-                OBD_ALLOC_PTR(oqctl);
-                if (oqctl == NULL)
-                        RETURN(-ENOMEM);
+               if (LUSTRE_Q_CMD_IS_POOL(cmd))
+                       oqctl_len += LOV_MAXPOOLNAME + 1;
 
-                QCTL_COPY(oqctl, qctl);
-                rc = obd_quotactl(sbi->ll_md_exp, oqctl);
-                if (rc) {
-                        OBD_FREE_PTR(oqctl);
-                        RETURN(rc);
-                }
+               OBD_ALLOC(oqctl, oqctl_len);
+               if (oqctl == NULL)
+                       RETURN(-ENOMEM);
+
+               QCTL_COPY(oqctl, qctl);
+               rc = obd_quotactl(sbi->ll_md_exp, oqctl);
+               if (rc) {
+                       OBD_FREE(oqctl, oqctl_len);
+                       RETURN(rc);
+               }
                 /* If QIF_SPACE is not set, client should collect the
                  * space usage from OSSs by itself */
-                if (cmd == Q_GETQUOTA &&
-                    !(oqctl->qc_dqblk.dqb_valid & QIF_SPACE) &&
-                    !oqctl->qc_dqblk.dqb_curspace) {
-                        struct obd_quotactl *oqctl_tmp;
-
-                        OBD_ALLOC_PTR(oqctl_tmp);
-                        if (oqctl_tmp == NULL)
-                                GOTO(out, rc = -ENOMEM);
-
-                        oqctl_tmp->qc_cmd = Q_GETOQUOTA;
+               if ((cmd == Q_GETQUOTA || cmd == LUSTRE_Q_GETQUOTAPOOL) &&
+                   !(oqctl->qc_dqblk.dqb_valid & QIF_SPACE) &&
+                   !oqctl->qc_dqblk.dqb_curspace) {
+                       struct obd_quotactl *oqctl_tmp;
+                       int qctl_len = sizeof(*oqctl_tmp) + LOV_MAXPOOLNAME + 1;
+
+                       OBD_ALLOC(oqctl_tmp, qctl_len);
+                       if (oqctl_tmp == NULL)
+                               GOTO(out, rc = -ENOMEM);
+
+                       if (cmd == LUSTRE_Q_GETQUOTAPOOL) {
+                               oqctl_tmp->qc_cmd = LUSTRE_Q_GETQUOTAPOOL;
+                               memcpy(oqctl_tmp->qc_poolname,
+                                      qctl->qc_poolname,
+                                      LOV_MAXPOOLNAME + 1);
+                       } else {
+                               oqctl_tmp->qc_cmd = Q_GETOQUOTA;
+                       }
                         oqctl_tmp->qc_id = oqctl->qc_id;
                         oqctl_tmp->qc_type = oqctl->qc_type;
 
@@ -1274,41 +1310,43 @@ static int quotactl_ioctl(struct ll_sb_info *sbi, struct if_quotactl *qctl)
                                 oqctl->qc_dqblk.dqb_valid |= QIF_SPACE;
                         }
 
-                        /* collect space & inode usage from MDTs */
-                        oqctl_tmp->qc_dqblk.dqb_curspace = 0;
-                        oqctl_tmp->qc_dqblk.dqb_curinodes = 0;
-                        rc = obd_quotactl(sbi->ll_md_exp, oqctl_tmp);
-                        if (!rc || rc == -EREMOTEIO) {
-                                oqctl->qc_dqblk.dqb_curspace +=
-                                        oqctl_tmp->qc_dqblk.dqb_curspace;
-                                oqctl->qc_dqblk.dqb_curinodes =
-                                        oqctl_tmp->qc_dqblk.dqb_curinodes;
-                                oqctl->qc_dqblk.dqb_valid |= QIF_INODES;
-                        } else {
-                                oqctl->qc_dqblk.dqb_valid &= ~QIF_SPACE;
-                        }
+                       /* collect space & inode usage from MDTs */
+                       oqctl_tmp->qc_cmd = Q_GETOQUOTA;
+                       oqctl_tmp->qc_dqblk.dqb_curspace = 0;
+                       oqctl_tmp->qc_dqblk.dqb_curinodes = 0;
+                       rc = obd_quotactl(sbi->ll_md_exp, oqctl_tmp);
+                       if (!rc || rc == -EREMOTEIO) {
+                               oqctl->qc_dqblk.dqb_curspace +=
+                                       oqctl_tmp->qc_dqblk.dqb_curspace;
+                               oqctl->qc_dqblk.dqb_curinodes =
+                                       oqctl_tmp->qc_dqblk.dqb_curinodes;
+                               oqctl->qc_dqblk.dqb_valid |= QIF_INODES;
+                       } else {
+                               oqctl->qc_dqblk.dqb_valid &= ~QIF_SPACE;
+                       }
 
-                        OBD_FREE_PTR(oqctl_tmp);
+                       OBD_FREE(oqctl_tmp, qctl_len);
                 }
 out:
-                QCTL_COPY(qctl, oqctl);
-                OBD_FREE_PTR(oqctl);
-        }
+               QCTL_COPY(qctl, oqctl);
+               OBD_FREE(oqctl, oqctl_len);
+       }
 
-        RETURN(rc);
+       RETURN(rc);
 }
 
 int ll_rmfid(struct file *file, void __user *arg)
 {
        const struct fid_array __user *ufa = arg;
+       struct inode *inode = file_inode(file);
        struct fid_array *lfa = NULL;
        size_t size;
        unsigned nr;
        int i, rc, *rcs = NULL;
        ENTRY;
 
-       if (!cfs_capable(CFS_CAP_DAC_READ_SEARCH) &&
-           !(ll_i2sbi(file_inode(file))->ll_flags & LL_SBI_USER_FID2PATH))
+       if (!capable(CAP_DAC_READ_SEARCH) &&
+           !test_bit(LL_SBI_USER_FID2PATH, ll_i2sbi(inode)->ll_flags))
                RETURN(-EPERM);
        /* Only need to get the buflen */
        if (get_user(nr, &ufa->fa_nr))
@@ -1321,7 +1359,7 @@ int ll_rmfid(struct file *file, void __user *arg)
        OBD_ALLOC(lfa, size);
        if (!lfa)
                RETURN(-ENOMEM);
-       OBD_ALLOC(rcs, sizeof(int) * nr);
+       OBD_ALLOC_PTR_ARRAY(rcs, nr);
        if (!rcs)
                GOTO(free_lfa, rc = -ENOMEM);
 
@@ -1339,7 +1377,7 @@ int ll_rmfid(struct file *file, void __user *arg)
        }
 
 free_rcs:
-       OBD_FREE(rcs, sizeof(int) * nr);
+       OBD_FREE_PTR_ARRAY(rcs, nr);
 free_lfa:
        OBD_FREE(lfa, size);
 
@@ -1379,7 +1417,7 @@ static long ll_dir_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
        struct dentry *dentry = file_dentry(file);
        struct inode *inode = file_inode(file);
        struct ll_sb_info *sbi = ll_i2sbi(inode);
-       struct obd_ioctl_data *data;
+       struct obd_ioctl_data *data = NULL;
        int rc = 0;
        ENTRY;
 
@@ -1417,14 +1455,12 @@ static long ll_dir_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
                return 0;
        }
        case IOC_MDC_LOOKUP: {
-                                    int namelen, len = 0;
-               char *buf = NULL;
+               int namelen, len = 0;
                char *filename;
 
-               rc = obd_ioctl_getdata(&buf, &len, (void __user *)arg);
+               rc = obd_ioctl_getdata(&data, &len, (void __user *)arg);
                if (rc != 0)
                        RETURN(rc);
-               data = (void *)buf;
 
                filename = data->ioc_inlbuf1;
                namelen = strlen(filename);
@@ -1440,24 +1476,23 @@ static long ll_dir_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
                        GOTO(out_free, rc);
                }
 out_free:
-               OBD_FREE_LARGE(buf, len);
-                return rc;
-        }
+               OBD_FREE_LARGE(data, len);
+               return rc;
+       }
        case LL_IOC_LMV_SETSTRIPE: {
                struct lmv_user_md  *lum;
-               char            *buf = NULL;
-               char            *filename;
-               int              namelen = 0;
-               int              lumlen = 0;
-               umode_t          mode;
-               int              len;
-               int              rc;
+               char *filename;
+               int namelen = 0;
+               int lumlen = 0;
+               umode_t mode;
+               bool createonly = false;
+               int len;
+               int rc;
 
-               rc = obd_ioctl_getdata(&buf, &len, (void __user *)arg);
+               rc = obd_ioctl_getdata(&data, &len, (void __user *)arg);
                if (rc)
                        RETURN(rc);
 
-               data = (void *)buf;
                if (data->ioc_inlbuf1 == NULL || data->ioc_inlbuf2 == NULL ||
                    data->ioc_inllen1 == 0 || data->ioc_inllen2 == 0)
                        GOTO(lmv_out_free, rc = -EINVAL);
@@ -1472,7 +1507,7 @@ out_free:
                lum = (struct lmv_user_md *)data->ioc_inlbuf2;
                lumlen = data->ioc_inllen2;
 
-               if (!lmv_magic_supported(lum->lum_magic)) {
+               if (!lmv_user_magic_supported(lum->lum_magic)) {
                        CERROR("%s: wrong lum magic %x : rc = %d\n", filename,
                               lum->lum_magic, -EINVAL);
                        GOTO(lmv_out_free, rc = -EINVAL);
@@ -1493,14 +1528,12 @@ out_free:
                        GOTO(lmv_out_free, rc = -EINVAL);
                }
 
-#if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 50, 0)
-               mode = data->ioc_type != 0 ? data->ioc_type : S_IRWXUGO;
-#else
                mode = data->ioc_type;
-#endif
-               rc = ll_dir_setdirstripe(dentry, lum, lumlen, filename, mode);
+               createonly = data->ioc_obdo1.o_flags & OBD_FL_OBDMDEXISTS;
+               rc = ll_dir_setdirstripe(dentry, lum, lumlen, filename, mode,
+                                        createonly);
 lmv_out_free:
-               OBD_FREE_LARGE(buf, len);
+               OBD_FREE_LARGE(data, len);
                RETURN(rc);
 
        }
@@ -1540,7 +1573,7 @@ lmv_out_free:
                if (copy_from_user(&lumv1, lumv1p, sizeof(lumv1)))
                        RETURN(-EFAULT);
 
-               if (inode->i_sb->s_root == file_dentry(file))
+               if (is_root_inode(inode))
                        set_default = 1;
 
                switch (lumv1.lmm_magic) {
@@ -1610,6 +1643,59 @@ out:
                        if (lmmsize > sizeof(*ulmv))
                                GOTO(finish_req, rc = -EINVAL);
 
+                       if (root_request != NULL) {
+                               struct lmv_user_md *lum;
+                               struct ll_inode_info *lli;
+
+                               lum = (struct lmv_user_md *)lmm;
+                               lli = ll_i2info(inode);
+                               if (lum->lum_max_inherit !=
+                                   LMV_INHERIT_UNLIMITED) {
+                                       if (lum->lum_max_inherit ==
+                                               LMV_INHERIT_NONE ||
+                                           lum->lum_max_inherit <
+                                               LMV_INHERIT_END ||
+                                           lum->lum_max_inherit >
+                                               LMV_INHERIT_MAX ||
+                                           lum->lum_max_inherit <
+                                               lli->lli_dir_depth)
+                                               GOTO(finish_req, rc = -ENODATA);
+
+                                       if (lum->lum_max_inherit ==
+                                           lli->lli_dir_depth) {
+                                               lum->lum_max_inherit =
+                                                       LMV_INHERIT_NONE;
+                                               lum->lum_max_inherit_rr =
+                                                       LMV_INHERIT_RR_NONE;
+                                               goto out_copy;
+                                       }
+
+                                       lum->lum_max_inherit -=
+                                               lli->lli_dir_depth;
+                               }
+
+                               if (lum->lum_max_inherit_rr !=
+                                       LMV_INHERIT_RR_UNLIMITED) {
+                                       if (lum->lum_max_inherit_rr ==
+                                               LMV_INHERIT_NONE ||
+                                           lum->lum_max_inherit_rr <
+                                               LMV_INHERIT_RR_END ||
+                                           lum->lum_max_inherit_rr >
+                                               LMV_INHERIT_RR_MAX ||
+                                           lum->lum_max_inherit_rr <=
+                                               lli->lli_dir_depth) {
+                                               lum->lum_max_inherit_rr =
+                                                       LMV_INHERIT_RR_NONE;
+                                               goto out_copy;
+                                       }
+
+                                       if (lum->lum_max_inherit_rr >
+                                               lli->lli_dir_depth)
+                                               lum->lum_max_inherit_rr -=
+                                                       lli->lli_dir_depth;
+                               }
+                       }
+out_copy:
                        if (copy_to_user(ulmv, lmm, lmmsize))
                                GOTO(finish_req, rc = -EFAULT);
 
@@ -1695,6 +1781,16 @@ finish_req:
                return rc;
        }
 
+       case LL_IOC_UNLOCK_FOREIGN:
+               /* if not a foreign symlink do nothing */
+               if (ll_foreign_is_removable(dentry, true)) {
+                       CDEBUG(D_INFO,
+                              "prevent rmdir of non-foreign dir ("DFID")\n",
+                              PFID(ll_inode2fid(inode)));
+                       RETURN(-EOPNOTSUPP);
+               }
+               RETURN(0);
+
        case LL_IOC_REMOVE_ENTRY: {
                char            *filename = NULL;
                int              namelen = 0;
@@ -1730,10 +1826,10 @@ out_rmdir:
                RETURN(ll_obd_statfs(inode, (void __user *)arg));
        case LL_IOC_LOV_GETSTRIPE:
        case LL_IOC_LOV_GETSTRIPE_NEW:
-       case LL_IOC_MDC_GETINFO:
-       case LL_IOC_MDC_GETINFO_OLD:
-       case IOC_MDC_GETFILEINFO:
-       case IOC_MDC_GETFILEINFO_OLD:
+       case LL_IOC_MDC_GETINFO_V1:
+       case LL_IOC_MDC_GETINFO_V2:
+       case IOC_MDC_GETFILEINFO_V1:
+       case IOC_MDC_GETFILEINFO_V2:
        case IOC_MDC_GETFILESTRIPE: {
                struct ptlrpc_request *request = NULL;
                struct ptlrpc_request *root_request = NULL;
@@ -1747,9 +1843,10 @@ out_rmdir:
                __u32 __user *lmmsizep = NULL;
                struct lu_fid __user *fidp = NULL;
                int lmmsize;
+               bool api32;
 
-               if (cmd == IOC_MDC_GETFILEINFO_OLD ||
-                   cmd == IOC_MDC_GETFILEINFO ||
+               if (cmd == IOC_MDC_GETFILEINFO_V1 ||
+                   cmd == IOC_MDC_GETFILEINFO_V2 ||
                    cmd == IOC_MDC_GETFILESTRIPE) {
                        filename = ll_getname((const char __user *)arg);
                        if (IS_ERR(filename))
@@ -1771,10 +1868,10 @@ out_rmdir:
                        GOTO(out_req, rc);
                }
 
-               if (rc == -ENODATA && (cmd == IOC_MDC_GETFILEINFO ||
-                                      cmd == LL_IOC_MDC_GETINFO ||
-                                      cmd == IOC_MDC_GETFILEINFO_OLD ||
-                                      cmd == LL_IOC_MDC_GETINFO_OLD)) {
+               if (rc == -ENODATA && (cmd == IOC_MDC_GETFILEINFO_V1 ||
+                                      cmd == LL_IOC_MDC_GETINFO_V1 ||
+                                      cmd == IOC_MDC_GETFILEINFO_V2 ||
+                                      cmd == LL_IOC_MDC_GETINFO_V2)) {
                        lmmsize = 0;
                        rc = 0;
                }
@@ -1786,8 +1883,8 @@ out_rmdir:
                    cmd == LL_IOC_LOV_GETSTRIPE ||
                    cmd == LL_IOC_LOV_GETSTRIPE_NEW) {
                        lump = (struct lov_user_md __user *)arg;
-               } else if (cmd == IOC_MDC_GETFILEINFO_OLD ||
-                          cmd == LL_IOC_MDC_GETINFO_OLD){
+               } else if (cmd == IOC_MDC_GETFILEINFO_V1 ||
+                          cmd == LL_IOC_MDC_GETINFO_V1){
                        struct lov_user_mds_data_v1 __user *lmdp;
 
                        lmdp = (struct lov_user_mds_data_v1 __user *)arg;
@@ -1814,9 +1911,10 @@ out_rmdir:
                                GOTO(out_req, rc = -EFAULT);
                        rc = -EOVERFLOW;
                }
+               api32 = test_bit(LL_SBI_32BIT_API, sbi->ll_flags);
 
-               if (cmd == IOC_MDC_GETFILEINFO_OLD ||
-                   cmd == LL_IOC_MDC_GETINFO_OLD) {
+               if (cmd == IOC_MDC_GETFILEINFO_V1 ||
+                   cmd == LL_IOC_MDC_GETINFO_V1) {
                        lstat_t st = { 0 };
 
                        st.st_dev       = inode->i_sb->s_dev;
@@ -1825,20 +1923,23 @@ out_rmdir:
                        st.st_uid       = body->mbo_uid;
                        st.st_gid       = body->mbo_gid;
                        st.st_rdev      = body->mbo_rdev;
-                       st.st_size      = body->mbo_size;
+                       if (llcrypt_require_key(inode) == -ENOKEY)
+                               st.st_size = round_up(st.st_size,
+                                                  LUSTRE_ENCRYPTION_UNIT_SIZE);
+                       else
+                               st.st_size = body->mbo_size;
                        st.st_blksize   = PAGE_SIZE;
                        st.st_blocks    = body->mbo_blocks;
                        st.st_atime     = body->mbo_atime;
                        st.st_mtime     = body->mbo_mtime;
                        st.st_ctime     = body->mbo_ctime;
                        st.st_ino       = cl_fid_build_ino(&body->mbo_fid1,
-                                               sbi->ll_flags &
-                                               LL_SBI_32BIT_API);
+                                                          api32);
 
                        if (copy_to_user(statp, &st, sizeof(st)))
                                GOTO(out_req, rc = -EFAULT);
-               } else if (cmd == IOC_MDC_GETFILEINFO ||
-                          cmd == LL_IOC_MDC_GETINFO) {
+               } else if (cmd == IOC_MDC_GETFILEINFO_V2 ||
+                          cmd == LL_IOC_MDC_GETINFO_V2) {
                        lstatx_t stx = { 0 };
                        __u64 valid = body->mbo_valid;
 
@@ -1848,18 +1949,22 @@ out_rmdir:
                        stx.stx_gid = body->mbo_gid;
                        stx.stx_mode = body->mbo_mode;
                        stx.stx_ino = cl_fid_build_ino(&body->mbo_fid1,
-                                                      sbi->ll_flags &
-                                                      LL_SBI_32BIT_API);
-                       stx.stx_size = body->mbo_size;
+                                                      api32);
+                       if (llcrypt_require_key(inode) == -ENOKEY)
+                               stx.stx_size = round_up(stx.stx_size,
+                                                  LUSTRE_ENCRYPTION_UNIT_SIZE);
+                       else
+                               stx.stx_size = body->mbo_size;
                        stx.stx_blocks = body->mbo_blocks;
                        stx.stx_atime.tv_sec = body->mbo_atime;
                        stx.stx_ctime.tv_sec = body->mbo_ctime;
                        stx.stx_mtime.tv_sec = body->mbo_mtime;
+                       stx.stx_btime.tv_sec = body->mbo_btime;
                        stx.stx_rdev_major = MAJOR(body->mbo_rdev);
                        stx.stx_rdev_minor = MINOR(body->mbo_rdev);
                        stx.stx_dev_major = MAJOR(inode->i_sb->s_dev);
                        stx.stx_dev_minor = MINOR(inode->i_sb->s_dev);
-                       stx.stx_mask |= STATX_BASIC_STATS;
+                       stx.stx_mask |= STATX_BASIC_STATS | STATX_BTIME;
 
                        /*
                         * For a striped directory, the size and blocks returned
@@ -1871,7 +1976,7 @@ out_rmdir:
                         * However, this whould be better decided by the MDS
                         * instead of the client.
                         */
-                       if (cmd == LL_IOC_MDC_GETINFO &&
+                       if (cmd == LL_IOC_MDC_GETINFO_V2 &&
                            ll_i2info(inode)->lli_lsm_md != NULL)
                                valid &= ~(OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
 
@@ -1905,27 +2010,36 @@ out_req:
                return rc;
        }
        case OBD_IOC_QUOTACTL: {
-                struct if_quotactl *qctl;
+               struct if_quotactl *qctl;
+               int qctl_len = sizeof(*qctl) + LOV_MAXPOOLNAME + 1;
 
-                OBD_ALLOC_PTR(qctl);
-                if (!qctl)
-                        RETURN(-ENOMEM);
+               OBD_ALLOC(qctl, qctl_len);
+               if (!qctl)
+                       RETURN(-ENOMEM);
 
                if (copy_from_user(qctl, (void __user *)arg, sizeof(*qctl)))
-                        GOTO(out_quotactl, rc = -EFAULT);
-
-                rc = quotactl_ioctl(sbi, qctl);
+                       GOTO(out_quotactl, rc = -EFAULT);
+
+               if (LUSTRE_Q_CMD_IS_POOL(qctl->qc_cmd)) {
+                       char __user *from = (char __user *)arg +
+                                       offsetof(typeof(*qctl), qc_poolname);
+                       if (copy_from_user(qctl->qc_poolname, from,
+                                          LOV_MAXPOOLNAME + 1))
+                               GOTO(out_quotactl, rc = -EFAULT);
+               }
 
-               if (rc == 0 &&
+               rc = quotactl_ioctl(inode->i_sb, qctl);
+               if ((rc == 0 || rc == -ENODATA) &&
                    copy_to_user((void __user *)arg, qctl, sizeof(*qctl)))
                         rc = -EFAULT;
 
-        out_quotactl:
-                OBD_FREE_PTR(qctl);
-                RETURN(rc);
+out_quotactl:
+               OBD_FREE(qctl, qctl_len);
+               RETURN(rc);
         }
-        case OBD_IOC_GETDTNAME:
-        case OBD_IOC_GETMDNAME:
+       case OBD_IOC_GETNAME_OLD:
+       case OBD_IOC_GETDTNAME:
+       case OBD_IOC_GETMDNAME:
                 RETURN(ll_get_obd_name(inode, cmd, arg));
         case LL_IOC_FLUSHCTX:
                 RETURN(ll_flush_ctx(inode));
@@ -2062,7 +2176,7 @@ out_hur:
                RETURN(rc);
        }
        case LL_IOC_HSM_CT_START:
-               if (!cfs_capable(CFS_CAP_SYS_ADMIN))
+               if (!capable(CAP_SYS_ADMIN))
                        RETURN(-EPERM);
 
                rc = copy_and_ct_start(cmd, sbi->ll_md_exp,
@@ -2109,23 +2223,23 @@ out_hur:
        }
        case LL_IOC_MIGRATE: {
                struct lmv_user_md *lum;
-               char *buf = NULL;
                int len;
                char *filename;
                int namelen = 0;
+               __u32 flags;
                int rc;
 
-               rc = obd_ioctl_getdata(&buf, &len, (void __user *)arg);
+               rc = obd_ioctl_getdata(&data, &len, (void __user *)arg);
                if (rc)
                        RETURN(rc);
 
-               data = (struct obd_ioctl_data *)buf;
                if (data->ioc_inlbuf1 == NULL || data->ioc_inlbuf2 == NULL ||
                    data->ioc_inllen1 == 0 || data->ioc_inllen2 == 0)
                        GOTO(migrate_free, rc = -EINVAL);
 
                filename = data->ioc_inlbuf1;
                namelen = data->ioc_inllen1;
+               flags = data->ioc_type;
 
                if (namelen < 1 || namelen != strlen(filename) + 1) {
                        CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
@@ -2141,16 +2255,18 @@ out_hur:
                        GOTO(migrate_free, rc);
                }
 
-               rc = ll_migrate(inode, file, lum, filename);
+               rc = ll_migrate(inode, file, lum, filename, flags);
 migrate_free:
-               OBD_FREE_LARGE(buf, len);
+               OBD_FREE_LARGE(data, len);
 
                RETURN(rc);
        }
-       case LL_IOC_FSGETXATTR:
+       case FS_IOC_FSGETXATTR:
                RETURN(ll_ioctl_fsgetxattr(inode, cmd, arg));
-       case LL_IOC_FSSETXATTR:
+       case FS_IOC_FSSETXATTR:
                RETURN(ll_ioctl_fssetxattr(inode, cmd, arg));
+       case LL_IOC_PROJECT:
+               RETURN(ll_ioctl_project(file, cmd, arg));
        case LL_IOC_PCC_DETACH_BY_FID: {
                struct lu_pcc_detach_fid *detach;
                struct lu_fid *fid;
@@ -2178,7 +2294,7 @@ migrate_free:
                if (!S_ISREG(inode2->i_mode))
                        GOTO(out_iput, rc = -EINVAL);
 
-               if (!inode_owner_or_capable(inode2))
+               if (!inode_owner_or_capable(&init_user_ns, inode2))
                        GOTO(out_iput, rc = -EPERM);
 
                rc = pcc_ioctl_detach(inode2, detach->pccd_opt);
@@ -2188,6 +2304,33 @@ out_detach:
                OBD_FREE_PTR(detach);
                RETURN(rc);
        }
+#ifdef HAVE_LUSTRE_CRYPTO
+       case LL_IOC_SET_ENCRYPTION_POLICY:
+               if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
+                       return -EOPNOTSUPP;
+               return llcrypt_ioctl_set_policy(file, (const void __user *)arg);
+       case LL_IOC_GET_ENCRYPTION_POLICY_EX:
+               if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
+                       return -EOPNOTSUPP;
+               return llcrypt_ioctl_get_policy_ex(file, (void __user *)arg);
+       case LL_IOC_ADD_ENCRYPTION_KEY:
+               if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
+                       return -EOPNOTSUPP;
+               return llcrypt_ioctl_add_key(file, (void __user *)arg);
+       case LL_IOC_REMOVE_ENCRYPTION_KEY:
+               if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
+                       return -EOPNOTSUPP;
+               return llcrypt_ioctl_remove_key(file, (void __user *)arg);
+       case LL_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
+               if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
+                       return -EOPNOTSUPP;
+               return llcrypt_ioctl_remove_key_all_users(file,
+                                                         (void __user *)arg);
+       case LL_IOC_GET_ENCRYPTION_KEY_STATUS:
+               if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
+                       return -EOPNOTSUPP;
+               return llcrypt_ioctl_get_key_status(file, (void __user *)arg);
+#endif
        default:
                RETURN(obd_iocontrol(cmd, sbi->ll_dt_exp, 0, NULL,
                                     (void __user *)arg));
@@ -2196,53 +2339,56 @@ out_detach:
 
 static loff_t ll_dir_seek(struct file *file, loff_t offset, int origin)
 {
-        struct inode *inode = file->f_mapping->host;
-        struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
-        struct ll_sb_info *sbi = ll_i2sbi(inode);
-        int api32 = ll_need_32bit_api(sbi);
-        loff_t ret = -EINVAL;
-        ENTRY;
+       struct inode *inode = file->f_mapping->host;
+       struct ll_file_data *fd = file->private_data;
+       struct ll_sb_info *sbi = ll_i2sbi(inode);
+       int api32 = ll_need_32bit_api(sbi);
+       loff_t ret = -EINVAL;
+       ENTRY;
 
        inode_lock(inode);
-        switch (origin) {
-                case SEEK_SET:
-                        break;
-                case SEEK_CUR:
-                        offset += file->f_pos;
-                        break;
-                case SEEK_END:
-                        if (offset > 0)
-                                GOTO(out, ret);
-                        if (api32)
-                                offset += LL_DIR_END_OFF_32BIT;
-                        else
-                                offset += LL_DIR_END_OFF;
-                        break;
-                default:
-                        GOTO(out, ret);
-        }
+       switch (origin) {
+       case SEEK_SET:
+               break;
+       case SEEK_CUR:
+               offset += file->f_pos;
+               break;
+       case SEEK_END:
+               if (offset > 0)
+                       GOTO(out, ret);
+               if (api32)
+                       offset += LL_DIR_END_OFF_32BIT;
+               else
+                       offset += LL_DIR_END_OFF;
+               break;
+       default:
+               GOTO(out, ret);
+       }
 
-        if (offset >= 0 &&
-            ((api32 && offset <= LL_DIR_END_OFF_32BIT) ||
-             (!api32 && offset <= LL_DIR_END_OFF))) {
-                if (offset != file->f_pos) {
-                        if ((api32 && offset == LL_DIR_END_OFF_32BIT) ||
-                            (!api32 && offset == LL_DIR_END_OFF))
+       if (offset >= 0 &&
+           ((api32 && offset <= LL_DIR_END_OFF_32BIT) ||
+            (!api32 && offset <= LL_DIR_END_OFF))) {
+               if (offset != file->f_pos) {
+                       bool hash64;
+
+                       hash64 = test_bit(LL_SBI_64BIT_HASH, sbi->ll_flags);
+                       if ((api32 && offset == LL_DIR_END_OFF_32BIT) ||
+                           (!api32 && offset == LL_DIR_END_OFF))
                                fd->lfd_pos = MDS_DIR_END_OFF;
-                        else if (api32 && sbi->ll_flags & LL_SBI_64BIT_HASH)
+                       else if (api32 && hash64)
                                fd->lfd_pos = offset << 32;
-                        else
+                       else
                                fd->lfd_pos = offset;
-                        file->f_pos = offset;
-                        file->f_version = 0;
-                }
-                ret = offset;
-        }
-        GOTO(out, ret);
+                       file->f_pos = offset;
+                       file->f_version = 0;
+               }
+               ret = offset;
+       }
+       GOTO(out, ret);
 
 out:
        inode_unlock(inode);
-        return ret;
+       return ret;
 }
 
 static int ll_dir_open(struct inode *inode, struct file *file)
@@ -2257,6 +2403,17 @@ static int ll_dir_release(struct inode *inode, struct file *file)
         RETURN(ll_file_release(inode, file));
 }
 
+/* notify error if partially read striped directory */
+static int ll_dir_flush(struct file *file, fl_owner_t id)
+{
+       struct ll_file_data *lfd = file->private_data;
+       int rc = lfd->fd_partial_readdir_rc;
+
+       lfd->fd_partial_readdir_rc = 0;
+
+       return rc;
+}
+
 const struct file_operations ll_dir_operations = {
        .llseek         = ll_dir_seek,
        .open           = ll_dir_open,
@@ -2269,4 +2426,5 @@ const struct file_operations ll_dir_operations = {
 #endif
        .unlocked_ioctl = ll_dir_ioctl,
        .fsync          = ll_fsync,
+       .flush          = ll_dir_flush,
 };