Whamcloud - gitweb
LU-15850 llite: pass dmv inherit depth instead of dir depth
[fs/lustre-release.git] / lustre / llite / llite_lib.c
index 88d193d..94dc136 100644 (file)
@@ -183,6 +183,7 @@ static struct ll_sb_info *ll_init_sbi(void)
        set_bit(LL_SBI_TINY_WRITE, sbi->ll_flags);
        set_bit(LL_SBI_PARALLEL_DIO, sbi->ll_flags);
        ll_sbi_set_encrypt(sbi, true);
+       ll_sbi_set_name_encrypt(sbi, true);
 
        /* root squash */
        sbi->ll_squash.rsi_uid = 0;
@@ -373,8 +374,10 @@ static int client_common_fill_super(struct super_block *sb, char *md, char *dt)
                data->ocd_connect_flags &= ~OBD_CONNECT_PINGLESS;
 
        obd_connect_set_secctx(data);
-       if (ll_sbi_has_encrypt(sbi))
+       if (ll_sbi_has_encrypt(sbi)) {
+               obd_connect_set_name_enc(data);
                obd_connect_set_enc(data);
+       }
 
 #if defined(CONFIG_SECURITY)
        data->ocd_connect_flags2 |= OBD_CONNECT2_SELINUX_POLICY;
@@ -452,6 +455,7 @@ static int client_common_fill_super(struct super_block *sb, char *md, char *dt)
        sb->s_blocksize_bits = log2(osfs->os_bsize);
        sb->s_magic = LL_SUPER_MAGIC;
        sb->s_maxbytes = MAX_LFS_FILESIZE;
+       sbi->ll_inode_cache_enabled = 1;
        sbi->ll_namelen = osfs->os_namelen;
        sbi->ll_mnt.mnt = current->fs->root.mnt;
        sbi->ll_mnt_ns = current->nsproxy->mnt_ns;
@@ -486,13 +490,24 @@ static int client_common_fill_super(struct super_block *sb, char *md, char *dt)
                set_bit(LL_SBI_FILE_SECCTX, sbi->ll_flags);
 
        if (ll_sbi_has_encrypt(sbi) && !obd_connect_has_enc(data)) {
-               if (ll_sbi_has_test_dummy_encryption(sbi))
+               if (ll_sb_has_test_dummy_encryption(sb))
                        LCONSOLE_WARN("%s: server %s does not support encryption feature, encryption deactivated.\n",
                                      sbi->ll_fsname,
                                      sbi->ll_md_exp->exp_obd->obd_name);
                ll_sbi_set_encrypt(sbi, false);
        }
 
+       if (ll_sbi_has_name_encrypt(sbi) && !obd_connect_has_name_enc(data)) {
+               struct  lustre_sb_info *lsi = s2lsi(sb);
+
+               if (ll_sb_has_test_dummy_encryption(sb))
+                       LCONSOLE_WARN("%s: server %s does not support name encryption, not using it.\n",
+                                     sbi->ll_fsname,
+                                     sbi->ll_md_exp->exp_obd->obd_name);
+               lsi->lsi_flags &= ~LSI_FILENAME_ENC;
+               ll_sbi_set_name_encrypt(sbi, false);
+       }
+
        if (data->ocd_ibits_known & MDS_INODELOCK_XATTR) {
                if (!(data->ocd_connect_flags & OBD_CONNECT_MAX_EASIZE)) {
                        LCONSOLE_INFO("%s: disabling xattr cache due to "
@@ -583,11 +598,11 @@ static int client_common_fill_super(struct super_block *sb, char *md, char *dt)
 
        if (ll_sbi_has_encrypt(sbi) &&
            !obd_connect_has_enc(&sbi->ll_dt_obd->u.lov.lov_ocd)) {
-               if (ll_sbi_has_test_dummy_encryption(sbi))
+               if (ll_sb_has_test_dummy_encryption(sb))
                        LCONSOLE_WARN("%s: server %s does not support encryption feature, encryption deactivated.\n",
                                      sbi->ll_fsname, dt);
                ll_sbi_set_encrypt(sbi, false);
-       } else if (ll_sbi_has_test_dummy_encryption(sbi)) {
+       } else if (ll_sb_has_test_dummy_encryption(sb)) {
                LCONSOLE_WARN("Test dummy encryption mode enabled\n");
        }
 
@@ -936,6 +951,7 @@ static const match_table_t ll_sbi_flags_name = {
        {LL_SBI_VERBOSE,                "verbose"},
        {LL_SBI_VERBOSE,                "noverbose"},
        {LL_SBI_ALWAYS_PING,            "always_ping"},
+       {LL_SBI_TEST_DUMMY_ENCRYPTION,  "test_dummy_encryption=%s"},
        {LL_SBI_TEST_DUMMY_ENCRYPTION,  "test_dummy_encryption"},
        {LL_SBI_ENCRYPT,                "encrypt"},
        {LL_SBI_ENCRYPT,                "noencrypt"},
@@ -953,6 +969,7 @@ static const match_table_t ll_sbi_flags_name = {
        {LL_SBI_TINY_WRITE,             "tiny_write"},
        {LL_SBI_FILE_HEAT,              "file_heat"},
        {LL_SBI_PARALLEL_DIO,           "parallel_dio"},
+       {LL_SBI_ENCRYPT_NAME,           "name_encrypt"},
 };
 
 int ll_sbi_flags_seq_show(struct seq_file *m, void *v)
@@ -984,6 +1001,7 @@ static int ll_options(char *options, struct super_block *sb)
 {
        struct ll_sb_info *sbi = ll_s2sbi(sb);
        char *s2, *s1, *opts;
+       int err = 0;
 
        ENTRY;
        if (!options)
@@ -1067,8 +1085,30 @@ static int ll_options(char *options, struct super_block *sb)
                        break;
                case LL_SBI_TEST_DUMMY_ENCRYPTION: {
 #ifdef HAVE_LUSTRE_CRYPTO
+#ifdef HAVE_FSCRYPT_DUMMY_CONTEXT_ENABLED
                        set_bit(token, sbi->ll_flags);
 #else
+                       struct lustre_sb_info *lsi = s2lsi(sb);
+
+                       err = llcrypt_set_test_dummy_encryption(sb, &args[0],
+                                                               &lsi->lsi_dummy_enc_ctx);
+                       if (!err)
+                               break;
+
+                       if (err == -EEXIST)
+                               LCONSOLE_WARN(
+                                        "Can't change test_dummy_encryption");
+                       else if (err == -EINVAL)
+                               LCONSOLE_WARN(
+                                       "Value of option \"%s\" unrecognized",
+                                       options);
+                       else
+                               LCONSOLE_WARN(
+                                        "Error processing option \"%s\" [%d]",
+                                        options, err);
+                       err = -1;
+#endif
+#else
                        LCONSOLE_WARN("Test dummy encryption mount option ignored: encryption not supported\n");
 #endif
                        break;
@@ -1128,7 +1168,7 @@ static int ll_options(char *options, struct super_block *sb)
                }
         }
        kfree(opts);
-        RETURN(0);
+       RETURN(err);
 }
 
 void ll_lli_init(struct ll_inode_info *lli)
@@ -1194,9 +1234,6 @@ void ll_lli_init(struct ll_inode_info *lli)
 #define MAX_STRING_SIZE 128
 
 #ifndef HAVE_SUPER_SETUP_BDI_NAME
-
-#define LSI_BDI_INITIALIZED    0x00400000
-
 #ifndef HAVE_BDI_CAP_MAP_COPY
 # define BDI_CAP_MAP_COPY      0
 #endif
@@ -1266,6 +1303,13 @@ int ll_fill_super(struct super_block *sb)
        if (err)
                GOTO(out_free_cfg, err);
 
+       if (ll_sb_has_test_dummy_encryption(sb))
+               /* enable filename encryption by default for dummy enc mode */
+               lsi->lsi_flags |= LSI_FILENAME_ENC;
+       else
+               /* filename encryption is disabled by default */
+               lsi->lsi_flags &= ~LSI_FILENAME_ENC;
+
        /* kernel >= 2.6.38 store dentry operations in sb->s_d_op. */
        sb->s_d_op = &ll_d_ops;
 
@@ -1438,10 +1482,15 @@ void ll_put_super(struct super_block *sb)
                client_common_put_super(sb);
        }
 
+       /* imitate failed cleanup */
+       if (OBD_FAIL_CHECK(OBD_FAIL_OBD_CLEANUP))
+               goto skip_cleanup;
+
        next = 0;
        while ((obd = class_devices_in_group(&sbi->ll_sb_uuid, &next)))
                class_manual_cleanup(obd);
 
+skip_cleanup:
        if (test_bit(LL_SBI_VERBOSE, sbi->ll_flags))
                LCONSOLE_WARN("Unmounted %s\n", profilenm ? profilenm : "");
 
@@ -1455,6 +1504,7 @@ void ll_put_super(struct super_block *sb)
        }
 #endif
 
+       llcrypt_free_dummy_context(&lsi->lsi_dummy_enc_ctx);
        ll_free_sbi(sb);
        lsi->lsi_llsbi = NULL;
 out_no_sbi:
@@ -1629,6 +1679,7 @@ static void ll_update_default_lsm_md(struct inode *inode, struct lustre_md *md)
                                lmv_free_memmd(lli->lli_default_lsm_md);
                                lli->lli_default_lsm_md = NULL;
                        }
+                       lli->lli_inherit_depth = 0;
                        up_write(&lli->lli_lsm_sem);
                }
                RETURN_EXIT;
@@ -1720,21 +1771,21 @@ static int ll_update_lsm_md(struct inode *inode, struct lustre_md *md)
        }
 
        rc = ll_init_lsm_md(inode, md);
-       up_write(&lli->lli_lsm_sem);
-
-       if (rc)
+       if (rc) {
+               up_write(&lli->lli_lsm_sem);
                RETURN(rc);
+       }
+
+       /* md_merge_attr() may take long, since lsm is already set, switch to
+        * read lock.
+        */
+       downgrade_write(&lli->lli_lsm_sem);
 
        /* set md->lmv to NULL, so the following free lustre_md will not free
         * this lsm.
         */
        md->lmv = NULL;
 
-       /* md_merge_attr() may take long, since lsm is already set, switch to
-        * read lock.
-        */
-       down_read(&lli->lli_lsm_sem);
-
        if (!lmv_dir_striped(lli->lli_lsm_md))
                GOTO(unlock, rc = 0);
 
@@ -1932,7 +1983,6 @@ int ll_io_zero_page(struct inode *inode, pgoff_t index, pgoff_t offset,
        struct cl_2queue *queue = NULL;
        struct cl_sync_io *anchor = NULL;
        bool holdinglock = false;
-       bool lockedbymyself = true;
        int rc;
 
        ENTRY;
@@ -1987,27 +2037,31 @@ int ll_io_zero_page(struct inode *inode, pgoff_t index, pgoff_t offset,
        if (!PageUptodate(vmpage) && !PageDirty(vmpage) &&
            !PageWriteback(vmpage)) {
                /* read page */
-               /* set PagePrivate2 to detect special case of empty page
-                * in osc_brw_fini_request()
+               /* Set PagePrivate2 to detect special case of empty page
+                * in osc_brw_fini_request().
+                * It is also used to tell ll_io_read_page() that we do not
+                * want the vmpage to be unlocked.
                 */
                SetPagePrivate2(vmpage);
                rc = ll_io_read_page(env, io, clpage, NULL);
-               if (!PagePrivate2(vmpage))
+               if (!PagePrivate2(vmpage)) {
                        /* PagePrivate2 was cleared in osc_brw_fini_request()
                         * meaning we read an empty page. In this case, in order
                         * to avoid allocating unnecessary block in truncated
                         * file, we must not zero and write as below. Subsequent
                         * server-side truncate will handle things correctly.
                         */
+                       cl_page_unassume(env, io, clpage);
                        GOTO(clpfini, rc = 0);
+               }
                ClearPagePrivate2(vmpage);
                if (rc)
                        GOTO(clpfini, rc);
-               lockedbymyself = trylock_page(vmpage);
-               cl_page_assume(env, io, clpage);
        }
 
-       /* zero range in page */
+       /* Thanks to PagePrivate2 flag, ll_io_read_page() did not unlock
+        * the vmpage, so we are good to proceed and zero range in page.
+        */
        zero_user(vmpage, offset, len);
 
        if (holdinglock && clpage) {
@@ -2017,7 +2071,7 @@ int ll_io_zero_page(struct inode *inode, pgoff_t index, pgoff_t offset,
                anchor = &vvp_env_info(env)->vti_anchor;
                cl_sync_io_init(anchor, 1);
                clpage->cp_sync_io = anchor;
-               cl_2queue_add(queue, clpage, true);
+               cl_page_list_add(&queue->c2_qin, clpage, true);
                rc = cl_io_submit_rw(env, io, CRT_WRITE, queue);
                if (rc)
                        GOTO(queuefini1, rc);
@@ -2029,7 +2083,7 @@ int ll_io_zero_page(struct inode *inode, pgoff_t index, pgoff_t offset,
 queuefini2:
                cl_2queue_discard(env, io, queue);
 queuefini1:
-               cl_2queue_disown(env, io, queue);
+               cl_2queue_disown(env, queue);
                cl_2queue_fini(env, queue);
        }
 
@@ -2037,10 +2091,8 @@ clpfini:
        if (clpage)
                cl_page_put(env, clpage);
 pagefini:
-       if (lockedbymyself) {
-               unlock_page(vmpage);
-               put_page(vmpage);
-       }
+       unlock_page(vmpage);
+       put_page(vmpage);
 rellock:
        if (holdinglock)
                cl_lock_release(env, lock);
@@ -2707,9 +2759,34 @@ int ll_update_inode(struct inode *inode, struct lustre_md *md)
        return 0;
 }
 
+/* child default LMV is inherited from parent */
+static inline bool ll_default_lmv_inherited(struct lmv_stripe_md *pdmv,
+                                           struct lmv_stripe_md *cdmv)
+{
+       if (!pdmv || !cdmv)
+               return false;
+
+       if (pdmv->lsm_md_magic != cdmv->lsm_md_magic ||
+           pdmv->lsm_md_stripe_count != cdmv->lsm_md_stripe_count ||
+           pdmv->lsm_md_master_mdt_index != cdmv->lsm_md_master_mdt_index ||
+           pdmv->lsm_md_hash_type != cdmv->lsm_md_hash_type)
+               return false;
+
+       if (cdmv->lsm_md_max_inherit !=
+           lmv_inherit_next(pdmv->lsm_md_max_inherit))
+               return false;
+
+       if (cdmv->lsm_md_max_inherit_rr !=
+           lmv_inherit_rr_next(pdmv->lsm_md_max_inherit_rr))
+               return false;
+
+       return true;
+}
+
 /* update directory depth to ROOT, called after LOOKUP lock is fetched. */
 void ll_update_dir_depth(struct inode *dir, struct inode *inode)
 {
+       struct ll_inode_info *plli;
        struct ll_inode_info *lli;
 
        if (!S_ISDIR(inode->i_mode))
@@ -2718,10 +2795,26 @@ void ll_update_dir_depth(struct inode *dir, struct inode *inode)
        if (inode == dir)
                return;
 
+       plli = ll_i2info(dir);
        lli = ll_i2info(inode);
-       lli->lli_dir_depth = ll_i2info(dir)->lli_dir_depth + 1;
-       CDEBUG(D_INODE, DFID" depth %hu\n",
-              PFID(&lli->lli_fid), lli->lli_dir_depth);
+       lli->lli_dir_depth = plli->lli_dir_depth + 1;
+       if (plli->lli_default_lsm_md && lli->lli_default_lsm_md) {
+               down_read(&plli->lli_lsm_sem);
+               down_read(&lli->lli_lsm_sem);
+               if (ll_default_lmv_inherited(plli->lli_default_lsm_md,
+                                            lli->lli_default_lsm_md))
+                       lli->lli_inherit_depth =
+                               plli->lli_inherit_depth + 1;
+               else
+                       lli->lli_inherit_depth = 0;
+               up_read(&lli->lli_lsm_sem);
+               up_read(&plli->lli_lsm_sem);
+       } else {
+               lli->lli_inherit_depth = 0;
+       }
+
+       CDEBUG(D_INODE, DFID" depth %hu default LMV depth %hu\n",
+              PFID(&lli->lli_fid), lli->lli_dir_depth, lli->lli_inherit_depth);
 }
 
 void ll_truncate_inode_pages_final(struct inode *inode)
@@ -3065,6 +3158,41 @@ void ll_open_cleanup(struct super_block *sb, struct req_capsule *pill)
        EXIT;
 }
 
+/* set filesystem-wide default LMV for subdir mount if it's enabled on ROOT. */
+static int ll_fileset_default_lmv_fixup(struct inode *inode,
+                                       struct lustre_md *md)
+{
+       struct ll_sb_info *sbi = ll_i2sbi(inode);
+       struct ptlrpc_request *req = NULL;
+       union lmv_mds_md *lmm = NULL;
+       int size = 0;
+       int rc;
+
+       LASSERT(is_root_inode(inode));
+       LASSERT(!fid_is_root(&sbi->ll_root_fid));
+       LASSERT(!md->default_lmv);
+
+       rc = ll_dir_get_default_layout(inode, (void **)&lmm, &size, &req,
+                                      OBD_MD_DEFAULT_MEA,
+                                      GET_DEFAULT_LAYOUT_ROOT);
+       if (rc && rc != -ENODATA)
+               GOTO(out, rc);
+
+       rc = 0;
+       if (lmm && size) {
+               rc = md_unpackmd(sbi->ll_md_exp, &md->default_lmv, lmm, size);
+               if (rc < 0)
+                       GOTO(out, rc);
+
+               rc = 0;
+       }
+       EXIT;
+out:
+       if (req)
+               ptlrpc_req_finished(req);
+       return rc;
+}
+
 int ll_prep_inode(struct inode **inode, struct req_capsule *pill,
                  struct super_block *sb, struct lookup_intent *it)
 {
@@ -3088,8 +3216,17 @@ int ll_prep_inode(struct inode **inode, struct req_capsule *pill,
         * ll_update_lsm_md() may change md.
         */
        if (it && (it->it_op & (IT_LOOKUP | IT_GETATTR)) &&
-           S_ISDIR(md.body->mbo_mode) && !md.default_lmv)
-               default_lmv_deleted = true;
+           S_ISDIR(md.body->mbo_mode) && !md.default_lmv) {
+               if (unlikely(*inode && is_root_inode(*inode) &&
+                            !fid_is_root(&sbi->ll_root_fid))) {
+                       rc = ll_fileset_default_lmv_fixup(*inode, &md);
+                       if (rc)
+                               GOTO(out, rc);
+               }
+
+               if (!md.default_lmv)
+                       default_lmv_deleted = true;
+       }
 
        if (*inode) {
                rc = ll_update_inode(*inode, &md);
@@ -3268,7 +3405,16 @@ struct md_op_data *ll_prep_md_op_data(struct md_op_data *op_data,
                return ERR_PTR(-ENOMEM);
 
        ll_i2gids(op_data->op_suppgids, i1, i2);
-       op_data->op_fid1 = *ll_inode2fid(i1);
+       /* If the client is using a subdir mount and looks at what it sees as
+        * /.fscrypt, interpret it as the .fscrypt dir at the root of the fs.
+        */
+       if (unlikely(i1->i_sb && i1->i_sb->s_root && is_root_inode(i1) &&
+                    !fid_is_root(ll_inode2fid(i1)) &&
+                    name && namelen == strlen(dot_fscrypt_name) &&
+                    strncmp(name, dot_fscrypt_name, namelen) == 0))
+               lu_root_fid(&op_data->op_fid1);
+       else
+               op_data->op_fid1 = *ll_inode2fid(i1);
 
        if (S_ISDIR(i1->i_mode)) {
                down_read_non_owner(&ll_i2info(i1)->lli_lsm_sem);
@@ -3336,9 +3482,10 @@ struct md_op_data *ll_prep_md_op_data(struct md_op_data *op_data,
                        op_data->op_bias = MDS_FID_OP;
                }
                if (fname.disk_name.name &&
-                   fname.disk_name.name != (unsigned char *)name)
+                   fname.disk_name.name != (unsigned char *)name) {
                        /* op_data->op_name must be freed after use */
                        op_data->op_flags |= MF_OPNAME_KMALLOCED;
+               }
        }
 
        /* In fact LUSTRE_OPC_LOOKUP, LUSTRE_OPC_OPEN
@@ -3418,6 +3565,8 @@ int ll_show_options(struct seq_file *seq, struct dentry *dentry)
                }
        }
 
+       llcrypt_show_test_dummy_encryption(seq, ',', dentry->d_sb);
+
        RETURN(0);
 }