+struct osd_check_lmv_buf {
+#ifdef HAVE_DIR_CONTEXT
+ /* please keep it as first member */
+ struct dir_context ctx;
+#endif
+ struct osd_thread_info *oclb_info;
+ struct osd_device *oclb_dev;
+ struct osd_idmap_cache *oclb_oic;
+};
+
+/**
+ * It is called internally by ->readdir() to filter out the
+ * local slave object's FID of the striped directory.
+ *
+ * \retval 1 found the local slave's FID
+ * \retval 0 continue to check next item
+ * \retval -ve for failure
+ */
+#ifdef HAVE_FILLDIR_USE_CTX
+static int osd_stripe_dir_filldir(struct dir_context *buf,
+#else
+static int osd_stripe_dir_filldir(void *buf,
+#endif
+ const char *name, int namelen,
+ loff_t offset, __u64 ino, unsigned d_type)
+{
+ struct osd_check_lmv_buf *oclb = (struct osd_check_lmv_buf *)buf;
+ struct osd_thread_info *oti = oclb->oclb_info;
+ struct lu_fid *fid = &oti->oti_fid3;
+ struct osd_inode_id *id = &oti->oti_id3;
+ struct osd_device *dev = oclb->oclb_dev;
+ struct osd_idmap_cache *oic = oclb->oclb_oic;
+ struct inode *inode;
+ int rc;
+
+ if (name[0] == '.')
+ return 0;
+
+ fid_zero(fid);
+ sscanf(name + 1, SFID, RFID(fid));
+ if (!fid_is_sane(fid))
+ return 0;
+
+ if (osd_remote_fid(oti->oti_env, dev, fid))
+ return 0;
+
+ osd_id_gen(id, ino, OSD_OII_NOGEN);
+ inode = osd_iget(oti, dev, id);
+ if (IS_ERR(inode))
+ return PTR_ERR(inode);
+
+ iput(inode);
+ osd_add_oi_cache(oti, dev, id, fid);
+ oic->oic_fid = *fid;
+ oic->oic_lid = *id;
+ oic->oic_dev = dev;
+ rc = osd_oii_insert(dev, oic, true);
+
+ return rc == 0 ? 1 : rc;
+}
+
+/* When lookup item under striped directory, we need to locate the master
+ * MDT-object of the striped directory firstly, then the client will send
+ * lookup (getattr_by_name) RPC to the MDT with some slave MDT-object's FID
+ * and the item's name. If the system is restored from MDT file level backup,
+ * then before the OI scrub completely built the OI files, the OI mappings of
+ * the master MDT-object and slave MDT-object may be invalid. Usually, it is
+ * not a problem for the master MDT-object. Because when locate the master
+ * MDT-object, we will do name based lookup (for the striped directory itself)
+ * firstly, during such process we can setup the correct OI mapping for the
+ * master MDT-object. But it will be trouble for the slave MDT-object. Because
+ * the client will not trigger name based lookup on the MDT to locate the slave
+ * MDT-object before locating item under the striped directory, then when
+ * osd_fid_lookup(), it will find that the OI mapping for the slave MDT-object
+ * is invalid and does not know what the right OI mapping is, then the MDT has
+ * to return -EINPROGRESS to the client to notify that the OI scrub is rebuiding
+ * the OI file, related OI mapping is unknown yet, please try again later. And
+ * then client will re-try the RPC again and again until related OI mapping has
+ * been updated. That is quite inefficient.
+ *
+ * To resolve above trouble, we will handle it as the following two cases:
+ *
+ * 1) The slave MDT-object and the master MDT-object are on different MDTs.
+ * It is relative easy. Be as one of remote MDT-objects, the slave MDT-object
+ * is linked under /REMOTE_PARENT_DIR with the name of its FID string.
+ * We can locate the slave MDT-object via lookup the /REMOTE_PARENT_DIR
+ * directly. Please check osd_fid_lookup().
+ *
+ * 2) The slave MDT-object and the master MDT-object reside on the same MDT.
+ * Under such case, during lookup the master MDT-object, we will lookup the
+ * slave MDT-object via readdir against the master MDT-object, because the
+ * slave MDT-objects information are stored as sub-directories with the name
+ * "${FID}:${index}". Then when find the local slave MDT-object, its OI
+ * mapping will be recorded. Then subsequent osd_fid_lookup() will know
+ * the correct OI mapping for the slave MDT-object. */
+static int osd_check_lmv(struct osd_thread_info *oti, struct osd_device *dev,
+ struct inode *inode, struct osd_idmap_cache *oic)
+{
+ struct lu_buf *buf = &oti->oti_big_buf;
+ struct dentry *dentry = &oti->oti_obj_dentry;
+ struct file *filp = &oti->oti_file;
+ const struct file_operations *fops;
+ struct lmv_mds_md_v1 *lmv1;
+ struct osd_check_lmv_buf oclb = {
+#ifdef HAVE_DIR_CONTEXT
+ .ctx.actor = osd_stripe_dir_filldir,
+#endif
+ .oclb_info = oti,
+ .oclb_dev = dev,
+ .oclb_oic = oic
+ };
+ int rc = 0;
+ ENTRY;
+
+again:
+ rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMV, buf->lb_buf,
+ buf->lb_len);
+ if (rc == -ERANGE) {
+ rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMV, NULL, 0);
+ if (rc > 0) {
+ lu_buf_realloc(buf, rc);
+ if (buf->lb_buf == NULL)
+ GOTO(out, rc = -ENOMEM);
+
+ goto again;
+ }
+ }
+
+ if (unlikely(rc == 0 || rc == -ENODATA))
+ GOTO(out, rc = 0);
+
+ if (rc < 0)
+ GOTO(out, rc);
+
+ if (unlikely(buf->lb_buf == NULL)) {
+ lu_buf_realloc(buf, rc);
+ if (buf->lb_buf == NULL)
+ GOTO(out, rc = -ENOMEM);
+
+ goto again;
+ }
+
+ lmv1 = buf->lb_buf;
+ if (le32_to_cpu(lmv1->lmv_magic) != LMV_MAGIC_V1)
+ GOTO(out, rc = 0);
+
+ fops = inode->i_fop;
+ dentry->d_inode = inode;
+ dentry->d_sb = inode->i_sb;
+ filp->f_pos = 0;
+ filp->f_path.dentry = dentry;
+ filp->f_mode = FMODE_64BITHASH;
+ filp->f_mapping = inode->i_mapping;
+ filp->f_op = fops;
+ filp->private_data = NULL;
+ set_file_inode(filp, inode);
+
+#ifdef HAVE_DIR_CONTEXT
+ oclb.ctx.pos = filp->f_pos;
+ rc = fops->iterate(filp, &oclb.ctx);
+ filp->f_pos = oclb.ctx.pos;
+#else
+ rc = fops->readdir(filp, &oclb, osd_stripe_dir_filldir);
+#endif
+ fops->release(inode, filp);
+
+out:
+ if (rc < 0)
+ CDEBUG(D_LFSCK, "%.16s: fail to check LMV EA, inode = %lu/%u,"
+ DFID": rc = %d\n",
+ LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
+ inode->i_ino, inode->i_generation,
+ PFID(&oic->oic_fid), rc);
+ else
+ rc = 0;
+
+ RETURN(rc);
+}
+