Whamcloud - gitweb
LU-3557 mdt: fix open existing file with O_CREAT for ro mount
[fs/lustre-release.git] / lustre / mdt / mdt_open.c
index 2e90841..686a76f 100644 (file)
@@ -655,7 +655,7 @@ void mdt_mfd_set_mode(struct mdt_file_data *mfd, __u64 mode)
 {
        LASSERT(mfd != NULL);
 
-       CDEBUG(D_HA, DFID "Change mfd mode 0x%Lx->0x%Lx\n",
+       CDEBUG(D_HA, DFID " Change mfd mode "LPO64" -> "LPO64".\n",
               PFID(mdt_object_fid(mfd->mfd_object)), mfd->mfd_mode, mode);
 
        mfd->mfd_mode = mode;
@@ -677,7 +677,7 @@ static int mdt_mfd_open(struct mdt_thread_info *info, struct mdt_object *p,
 
         isreg = S_ISREG(la->la_mode);
         isdir = S_ISDIR(la->la_mode);
-        if (isreg && !(ma->ma_valid & MA_LOV)) {
+       if (isreg && !(ma->ma_valid & MA_LOV) && !(flags & MDS_OPEN_RELEASE)) {
                 /*
                  * No EA, check whether it is will set regEA and dirEA since in
                  * above attr get, these size might be zero, so reset it, to
@@ -1375,6 +1375,23 @@ static void mdt_object_open_unlock(struct mdt_thread_info *info,
        RETURN_EXIT;
 }
 
+/**
+ * Check release is permitted for the current HSM flags.
+ */
+static bool mdt_hsm_release_allow(struct md_attr *ma)
+{
+       if (!(ma->ma_valid & MA_HSM))
+               return false;
+
+       if (ma->ma_hsm.mh_flags & (HS_DIRTY|HS_NORELEASE|HS_LOST))
+               return false;
+
+       if (!(ma->ma_hsm.mh_flags & HS_ARCHIVED))
+               return false;
+
+       return true;
+}
+
 int mdt_open_by_fid_lock(struct mdt_thread_info *info, struct ldlm_reply *rep,
                         struct mdt_lock_handle *lhc)
 {
@@ -1423,9 +1440,16 @@ int mdt_open_by_fid_lock(struct mdt_thread_info *info, struct ldlm_reply *rep,
 
        mdt_set_disposition(info, rep, (DISP_IT_EXECD | DISP_LOOKUP_EXECD));
 
+       if (flags & MDS_OPEN_RELEASE)
+               ma->ma_need |= MA_HSM;
        rc = mdt_attr_get_complex(info, o, ma);
-        if (rc)
-                GOTO(out, rc);
+       if (rc)
+               GOTO(out, rc);
+
+       /* If a release request, check file flags are fine and ask for an
+        * exclusive open access. */
+       if (flags & MDS_OPEN_RELEASE && !mdt_hsm_release_allow(ma))
+               GOTO(out, rc = -EPERM);
 
        rc = mdt_object_open_lock(info, o, lhc, &ibits);
         if (rc)
@@ -1672,6 +1696,8 @@ int mdt_reint_open(struct mdt_thread_info *info, struct mdt_lock_handle *lhc)
                 }
                 if (!(create_flags & MDS_OPEN_CREAT))
                         GOTO(out_parent, result);
+               if (exp_connect_flags(req->rq_export) & OBD_CONNECT_RDONLY)
+                       GOTO(out_parent, result = -EROFS);
                 *child_fid = *info->mti_rr.rr_fid2;
                 LASSERTF(fid_is_sane(child_fid), "fid="DFID"\n",
                          PFID(child_fid));
@@ -1814,6 +1840,15 @@ int mdt_reint_open(struct mdt_thread_info *info, struct mdt_lock_handle *lhc)
                result = rc;
                /* openlock will be released if mdt_finish_open failed */
                mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_LOCK);
+
+               if (created && create_flags & MDS_OPEN_VOLATILE) {
+                       CERROR("%s: cannot open volatile file "DFID", orphan "
+                              "file will be left in PENDING directory until "
+                              "next reboot, rc = %d\n", mdt_obd_name(mdt),
+                              PFID(mdt_object_fid(child)), rc);
+                       GOTO(out_child_unlock, result);
+               }
+
                if (created) {
                        ma->ma_need = 0;
                        ma->ma_valid = 0;
@@ -1843,6 +1878,240 @@ out:
        return result;
 }
 
+/**
+ * Create an orphan object use local root.
+ */
+static struct mdt_object *mdt_orphan_open(struct mdt_thread_info *info,
+                                         struct mdt_device *mdt,
+                                         const struct lu_fid *fid,
+                                         struct md_attr *attr, fmode_t fmode)
+{
+       const struct lu_env *env = info->mti_env;
+       struct md_op_spec *spec = &info->mti_spec;
+       struct lu_fid *rootfid = &info->mti_tmp_fid1;
+       struct mdt_object *obj = NULL;
+       struct mdt_object *local_root;
+       static const char name[] = "i_am_nobody";
+       struct lu_name *lname;
+       int rc;
+       ENTRY;
+
+       rc = dt_root_get(env, mdt->mdt_bottom, rootfid);
+       if (rc != 0)
+               RETURN(ERR_PTR(rc));
+
+       local_root = mdt_object_find(env, mdt, rootfid);
+       if (IS_ERR(local_root))
+               RETURN(local_root);
+
+       obj = mdt_object_new(env, mdt, fid);
+       if (IS_ERR(obj))
+               GOTO(out, rc = PTR_ERR(obj));
+
+       spec->sp_cr_lookup = 0;
+       spec->sp_feat = &dt_directory_features;
+       spec->sp_cr_mode = MDL_MINMODE; /* no lock */
+       spec->sp_cr_flags = MDS_OPEN_VOLATILE | fmode;
+       if (attr->ma_valid & MA_LOV) {
+               spec->u.sp_ea.eadata = attr->ma_lmm;
+               spec->u.sp_ea.eadatalen = attr->ma_lmm_size;
+               spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
+       } else {
+               spec->sp_cr_flags |= MDS_OPEN_DELAY_CREATE;
+       }
+
+       lname = mdt_name(env, (char *)name, sizeof(name) - 1);
+       rc = mdo_create(env, mdt_object_child(local_root), lname,
+                       mdt_object_child(obj), spec, attr);
+       if (rc == 0) {
+               rc = mo_open(env, mdt_object_child(obj), MDS_OPEN_CREATED);
+               if (rc < 0)
+                       CERROR("%s: cannot open volatile file "DFID", orphan "
+                              "file will be left in PENDING directory until "
+                              "next reboot, rc = %d\n", mdt_obd_name(mdt),
+                              PFID(fid), rc);
+       }
+       EXIT;
+
+out:
+       if (rc < 0) {
+               if (!IS_ERR(obj))
+                       mdt_object_put(env, obj);
+               obj = ERR_PTR(rc);
+       }
+       mdt_object_put(env, local_root);
+       return obj;
+}
+
+static int mdt_hsm_release(struct mdt_thread_info *info, struct mdt_object *o,
+                          struct md_attr *ma)
+{
+       struct mdt_lock_handle *lh = &info->mti_lh[MDT_LH_LAYOUT];
+       struct close_data      *data;
+       struct ldlm_lock       *lease;
+       struct mdt_object      *orphan;
+       struct md_attr         *orp_ma;
+       struct lu_buf          *buf;
+       bool                    lease_broken;
+       int                     rc;
+       int                     rc2;
+       ENTRY;
+
+       data = req_capsule_client_get(info->mti_pill, &RMF_CLOSE_DATA);
+       if (data == NULL)
+               RETURN(-EPROTO);
+
+       lease = ldlm_handle2lock(&data->cd_handle);
+       if (lease == NULL)
+               RETURN(-ESTALE);
+
+       /* try to hold open_sem so that nobody else can open the file */
+       if (!down_write_trylock(&o->mot_open_sem)) {
+               ldlm_lock_cancel(lease);
+               LDLM_LOCK_PUT(lease);
+               RETURN(-EBUSY);
+       }
+
+       /* Check if the lease open lease has already canceled */
+       lock_res_and_lock(lease);
+       lease_broken = ldlm_is_cancel(lease);
+       unlock_res_and_lock(lease);
+
+       LDLM_DEBUG(lease, DFID " lease broken? %d\n",
+                  PFID(mdt_object_fid(o)), lease_broken);
+
+       /* Cancel server side lease. Client side counterpart should
+        * have been cancelled. It's okay to cancel it now as we've
+        * held mot_open_sem. */
+       ldlm_lock_cancel(lease);
+       LDLM_LOCK_PUT(lease);
+
+       if (lease_broken) /* don't perform release task */
+               GOTO(out_unlock, rc = -ESTALE);
+
+       if (fid_is_zero(&data->cd_fid) || !fid_is_sane(&data->cd_fid))
+               GOTO(out_unlock, rc = -EINVAL);
+
+       /* ma_need was set before but it seems fine to change it in order to
+        * avoid modifying the one from RPC */
+       ma->ma_need = MA_HSM | MA_LOV;
+       rc = mdt_attr_get_complex(info, o, ma);
+       if (rc != 0)
+               GOTO(out_unlock, rc);
+
+       if (!mdt_hsm_release_allow(ma))
+               GOTO(out_unlock, rc = -EPERM);
+
+       /* already released? */
+       if (ma->ma_hsm.mh_flags & HS_RELEASED)
+               GOTO(out_unlock, rc = 0);
+
+       /* Compare on-disk and packed data_version */
+       if (data->cd_data_version != ma->ma_hsm.mh_arch_ver) {
+               CDEBUG(D_HSM, DFID" data_version mismatches: packed="LPU64
+                      " and on-disk="LPU64"\n", PFID(mdt_object_fid(o)),
+                      data->cd_data_version, ma->ma_hsm.mh_arch_ver);
+               /* XXX: Enable this line when hsm_archive is operational!
+               GOTO(out_unlock, rc = -EPERM);
+               */
+       }
+
+       ma->ma_valid = MA_INODE;
+       ma->ma_attr.la_valid &= LA_SIZE | LA_MTIME | LA_ATIME;
+       rc = mo_attr_set(info->mti_env, mdt_object_child(o), ma);
+       if (rc < 0)
+               GOTO(out_unlock, rc);
+
+       if (!(ma->ma_valid & MA_LOV)) {
+               /* Even empty file are released */
+               memset(ma->ma_lmm, 0, sizeof(*ma->ma_lmm));
+               ma->ma_lmm->lmm_magic = cpu_to_le32(LOV_MAGIC_V1_DEF);
+               ma->ma_lmm->lmm_pattern = cpu_to_le32(LOV_PATTERN_RAID0);
+               ma->ma_lmm->lmm_stripe_size = cpu_to_le32(LOV_MIN_STRIPE_SIZE);
+               ma->ma_valid |= MA_LOV;
+       } else {
+               /* Magic must be LOV_MAGIC_Vx_DEF otherwise LOD will interpret
+                * ma_lmm as lov_user_md, then it will be confused by union of
+                * layout_gen and stripe_offset. */
+               if (le32_to_cpu(ma->ma_lmm->lmm_magic) == LOV_MAGIC_V1)
+                       ma->ma_lmm->lmm_magic = cpu_to_le32(LOV_MAGIC_V1_DEF);
+               else if (le32_to_cpu(ma->ma_lmm->lmm_magic) == LOV_MAGIC_V3)
+                       ma->ma_lmm->lmm_magic = cpu_to_le32(LOV_MAGIC_V3_DEF);
+               else
+                       GOTO(out_unlock, rc = -EINVAL);
+       }
+
+       /* Set file as released */
+       ma->ma_lmm->lmm_pattern |= cpu_to_le32(LOV_PATTERN_F_RELEASED);
+
+       /* Hopefully it's not used in this call path */
+       orp_ma = &info->mti_u.som.attr;
+       orp_ma->ma_valid = MA_INODE | MA_LOV;
+       orp_ma->ma_attr.la_mode = S_IFREG;
+       orp_ma->ma_attr.la_valid = LA_MODE;
+       orp_ma->ma_lmm = ma->ma_lmm;
+       orp_ma->ma_lmm_size = ma->ma_lmm_size;
+       orphan = mdt_orphan_open(info, info->mti_mdt, &data->cd_fid, orp_ma,
+                                FMODE_WRITE);
+       if (IS_ERR(orphan)) {
+               CERROR("%s: cannot open orphan file "DFID": rc = %ld\n",
+                      mdt_obd_name(info->mti_mdt), PFID(&data->cd_fid),
+                      PTR_ERR(orphan));
+               GOTO(out_unlock, rc = PTR_ERR(orphan));
+       }
+
+       /* Set up HSM attribute for orphan object */
+       CLASSERT(sizeof(struct hsm_attrs) <= sizeof(info->mti_xattr_buf));
+       buf = &info->mti_buf;
+       buf->lb_buf = info->mti_xattr_buf;
+       buf->lb_len = sizeof(struct hsm_attrs);
+       ma->ma_hsm.mh_flags |= HS_RELEASED;
+       lustre_hsm2buf(buf->lb_buf, &ma->ma_hsm);
+       ma->ma_hsm.mh_flags &= ~HS_RELEASED;
+       rc = mo_xattr_set(info->mti_env, mdt_object_child(orphan), buf,
+                         XATTR_NAME_HSM, 0);
+       if (rc < 0)
+               GOTO(out_close, rc);
+
+       mdt_lock_reg_init(lh, LCK_EX);
+       rc = mdt_object_lock(info, o, lh, MDS_INODELOCK_LAYOUT, MDT_LOCAL_LOCK);
+       if (rc == 0) {
+               /* Swap layout with orphan object */
+               rc = mo_swap_layouts(info->mti_env, mdt_object_child(o),
+                                    mdt_object_child(orphan),
+                                    SWAP_LAYOUTS_MDS_HSM);
+
+               /* Release exclusive LL */
+               mdt_object_unlock(info, o, lh, 1);
+       }
+       EXIT;
+
+out_close:
+       /* Close orphan object anyway */
+       rc2 = mo_close(info->mti_env, mdt_object_child(orphan), orp_ma,
+                      FMODE_WRITE);
+       if (rc2 < 0)
+               CERROR("%s: error closing volatile file "DFID": rc = %d\n",
+                      mdt_obd_name(info->mti_mdt), PFID(&data->cd_fid), rc2);
+       LU_OBJECT_DEBUG(D_HSM, info->mti_env, &orphan->mot_obj,
+                       "object closed\n");
+       mdt_object_put(info->mti_env, orphan);
+
+out_unlock:
+       up_write(&o->mot_open_sem);
+
+       if (rc == 0) { /* already released */
+               struct mdt_body *repbody;
+               repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
+               LASSERT(repbody != NULL);
+               repbody->valid |= OBD_MD_FLRELEASED;
+       }
+
+       ma->ma_valid = 0;
+       ma->ma_need = 0;
+       return rc;
+}
+
 #define MFD_CLOSED(mode) (((mode) & ~(MDS_FMODE_EPOCH | MDS_FMODE_SOM | \
                                       MDS_FMODE_TRUNC)) == MDS_FMODE_CLOSED)
 
@@ -1863,6 +2132,16 @@ int mdt_mfd_close(struct mdt_thread_info *info, struct mdt_file_data *mfd)
 
         mode = mfd->mfd_mode;
 
+       if (ma->ma_attr_flags & MDS_HSM_RELEASE) {
+               rc = mdt_hsm_release(info, o, ma);
+               if (rc < 0) {
+                       CDEBUG(D_HSM, "%s: File " DFID " release failed: %d\n",
+                               mdt_obd_name(info->mti_mdt),
+                               PFID(mdt_object_fid(o)), rc);
+                       /* continue to close even error occurred. */
+               }
+       }
+
         if ((mode & FMODE_WRITE) || (mode & MDS_FMODE_TRUNC)) {
                 mdt_write_put(o);
                 ret = mdt_ioepoch_close(info, o);
@@ -1883,22 +2162,8 @@ int mdt_mfd_close(struct mdt_thread_info *info, struct mdt_file_data *mfd)
                 rc = mo_attr_set(info->mti_env, next, ma);
         }
 
-       /* If file data is modified, add the dirty flag.
-        *
-        * If MDS_CLOSE_CLEANUP is set, this file is being closed due to an
-        * eviction, file could have been modified and now dirty
-        * regarding to HSM archive, check this!
-        * The logic here is to mark a file dirty if there's a chance it was
-        * dirtied before the client was evicted, so that we don't have to wait
-        * for a release attempt before finding out the file was actually dirty
-        * and fail the release. Aggressively marking it dirty here will cause
-        * the policy engine to attempt to re-archive it; when rearchiving, we
-        * can compare the current version to the LMA data_version and make the
-        * archive request into a noop if it's not actually dirty.
-        */
-       if ((ma->ma_attr_flags & MDS_DATA_MODIFIED) ||
-           ((ma->ma_attr_flags & MDS_CLOSE_CLEANUP) &&
-            (mode & (FMODE_WRITE|MDS_FMODE_TRUNC))))
+       /* If file data is modified, add the dirty flag. */
+       if (ma->ma_attr_flags & MDS_DATA_MODIFIED)
                rc = mdt_add_dirty_flag(info, o, ma);
 
         ma->ma_need |= MA_INODE;