Whamcloud - gitweb
LU-11656 llite: fetch default layout for a directory
[fs/lustre-release.git] / lustre / llite / dir.c
index f3c86bc..e013f1b 100644 (file)
@@ -314,15 +314,16 @@ static int ll_iterate(struct file *filp, struct dir_context *ctx)
 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_sb_info       *sbi    = ll_i2sbi(inode);
-       int                     hash64  = sbi->ll_flags & LL_SBI_64BIT_HASH;
-       int                     api32   = ll_need_32bit_api(sbi);
-       struct md_op_data       *op_data;
-       struct lu_fid           pfid = { 0 };
-       __u64                   pos;
-       int                     rc;
+       struct inode *inode = file_inode(filp);
+       struct ll_file_data *lfd = LUSTRE_FPRIVATE(filp);
+       struct ll_sb_info *sbi = ll_i2sbi(inode);
+       int hash64 = sbi->ll_flags & LL_SBI_64BIT_HASH;
+       int api32 = ll_need_32bit_api(sbi);
+       struct md_op_data *op_data;
+       struct lu_fid pfid = { 0 };
+       ktime_t kstart = ktime_get();
+       __u64 pos;
+       int rc;
        ENTRY;
 
        if (lfd != NULL)
@@ -406,7 +407,8 @@ static int ll_readdir(struct file *filp, void *cookie, filldir_t filldir)
 
 out:
        if (!rc)
-               ll_stats_ops_tally(sbi, LPROC_LL_READDIR, 1);
+               ll_stats_ops_tally(sbi, LPROC_LL_READDIR,
+                                  ktime_us_delta(ktime_get(), kstart));
 
        RETURN(rc);
 }
@@ -677,16 +679,10 @@ end:
        RETURN(rc);
 }
 
-/**
- * This function will be used to get default LOV/LMV/Default LMV
- * @valid will be used to indicate which stripe it will retrieve
- *     OBD_MD_MEA              LMV stripe EA
- *     OBD_MD_DEFAULT_MEA      Default LMV stripe EA
- *     otherwise               Default LOV EA.
- * Each time, it can only retrieve 1 stripe EA
- **/
-int ll_dir_getstripe(struct inode *inode, void **plmm, int *plmm_size,
-                    struct ptlrpc_request **request, u64 valid)
+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)
 {
        struct ll_sb_info *sbi = ll_i2sbi(inode);
        struct mdt_body   *body;
@@ -694,6 +690,7 @@ int ll_dir_getstripe(struct inode *inode, void **plmm, int *plmm_size,
        struct ptlrpc_request *req = NULL;
        int rc, lmm_size;
        struct md_op_data *op_data;
+       struct lu_fid fid;
        ENTRY;
 
        rc = ll_get_default_mdsize(sbi, &lmm_size);
@@ -707,11 +704,19 @@ int ll_dir_getstripe(struct inode *inode, void **plmm, int *plmm_size,
                RETURN(PTR_ERR(op_data));
 
        op_data->op_valid = valid | OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
+
+       if (type == GET_DEFAULT_LAYOUT_ROOT) {
+               lu_root_fid(&op_data->op_fid1);
+               fid = op_data->op_fid1;
+       } else {
+               fid = *ll_inode2fid(inode);
+       }
+
        rc = md_getattr(sbi->ll_md_exp, op_data, &req);
        ll_finish_md_op_data(op_data);
        if (rc < 0) {
-               CDEBUG(D_INFO, "md_getattr failed on inode "
-                      DFID": rc %d\n", PFID(ll_inode2fid(inode)), rc);
+               CDEBUG(D_INFO, "md_getattr failed on inode "DFID": rc %d\n",
+                      PFID(&fid), rc);
                GOTO(out, rc);
        }
 
@@ -773,6 +778,72 @@ out:
        return rc;
 }
 
+/**
+ * This function will be used to get default LOV/LMV/Default LMV
+ * @valid will be used to indicate which stripe it will retrieve.
+ * If the directory does not have its own default layout, then the
+ * function will request the default layout from root FID.
+ *     OBD_MD_MEA              LMV stripe EA
+ *     OBD_MD_DEFAULT_MEA      Default LMV stripe EA
+ *     otherwise               Default LOV EA.
+ * Each time, it can only retrieve 1 stripe EA
+ **/
+int ll_dir_getstripe_default(struct inode *inode, void **plmm, int *plmm_size,
+                            struct ptlrpc_request **request,
+                            struct ptlrpc_request **root_request,
+                            u64 valid)
+{
+       struct ptlrpc_request *req = NULL;
+       struct ptlrpc_request *root_req = NULL;
+       struct lov_mds_md *lmm = NULL;
+       int lmm_size = 0;
+       int rc = 0;
+       ENTRY;
+
+       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);
+
+       *plmm = lmm;
+       *plmm_size = lmm_size;
+       *request = req;
+       if (root_request != NULL)
+               *root_request = root_req;
+
+       RETURN(rc);
+}
+
+/**
+ * This function will be used to get default LOV/LMV/Default LMV
+ * @valid will be used to indicate which stripe it will retrieve
+ *     OBD_MD_MEA              LMV stripe EA
+ *     OBD_MD_DEFAULT_MEA      Default LMV stripe EA
+ *     otherwise               Default LOV EA.
+ * Each time, it can only retrieve 1 stripe EA
+ **/
+int ll_dir_getstripe(struct inode *inode, void **plmm, int *plmm_size,
+                    struct ptlrpc_request **request, u64 valid)
+{
+       struct ptlrpc_request *req = NULL;
+       struct lov_mds_md *lmm = NULL;
+       int lmm_size = 0;
+       int rc = 0;
+       ENTRY;
+
+       rc = ll_dir_get_default_layout(inode, (void **)&lmm, &lmm_size,
+                                      &req, valid, 0);
+
+       *plmm = lmm;
+       *plmm_size = lmm_size;
+       *request = req;
+
+       RETURN(rc);
+}
+
 int ll_get_mdt_idx_by_fid(struct ll_sb_info *sbi, const struct lu_fid *fid)
 {
        struct md_op_data       *op_data;
@@ -1464,9 +1535,9 @@ lmv_out_free:
 
                int set_default = 0;
 
-               CLASSERT(sizeof(struct lov_user_md_v3) >
-                        sizeof(struct lov_comp_md_v1));
-               CLASSERT(sizeof(*lumv3) == sizeof(*lumv3p));
+               BUILD_BUG_ON(sizeof(struct lov_user_md_v3) <=
+                            sizeof(struct lov_comp_md_v1));
+               BUILD_BUG_ON(sizeof(*lumv3) != sizeof(*lumv3p));
                /* first try with v1 which is smaller than v3 */
                if (copy_from_user(&lumv1, lumv1p, sizeof(lumv1)))
                        RETURN(-EFAULT);
@@ -1505,6 +1576,7 @@ out:
                                        (struct lmv_user_md __user *)arg;
                struct lmv_user_md      lum;
                struct ptlrpc_request   *request = NULL;
+               struct ptlrpc_request   *root_request = NULL;
                union lmv_mds_md        *lmm = NULL;
                int                     lmmsize;
                u64                     valid = 0;
@@ -1530,8 +1602,8 @@ out:
                else
                        RETURN(-EINVAL);
 
-               rc = ll_dir_getstripe(inode, (void **)&lmm, &lmmsize, &request,
-                                     valid);
+               rc = ll_dir_getstripe_default(inode, (void **)&lmm, &lmmsize,
+                                             &request, &root_request, valid);
                if (rc != 0)
                        GOTO(finish_req, rc);
 
@@ -1621,6 +1693,7 @@ out_tmp:
                OBD_FREE(tmp, lum_size);
 finish_req:
                ptlrpc_req_finished(request);
+               ptlrpc_req_finished(root_request);
                return rc;
        }
 
@@ -1660,38 +1733,50 @@ out_rmdir:
        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 IOC_MDC_GETFILESTRIPE: {
                struct ptlrpc_request *request = NULL;
+               struct ptlrpc_request *root_request = NULL;
                struct lov_user_md __user *lump;
-                struct lov_mds_md *lmm = NULL;
-                struct mdt_body *body;
-                char *filename = NULL;
-                int lmmsize;
-
-                if (cmd == IOC_MDC_GETFILEINFO ||
-                    cmd == IOC_MDC_GETFILESTRIPE) {
+               struct lov_mds_md *lmm = NULL;
+               struct mdt_body *body;
+               char *filename = NULL;
+               lstat_t __user *statp = NULL;
+               lstatx_t __user *stxp = NULL;
+               __u64 __user *flagsp = NULL;
+               __u32 __user *lmmsizep = NULL;
+               struct lu_fid __user *fidp = NULL;
+               int lmmsize;
+
+               if (cmd == IOC_MDC_GETFILEINFO_OLD ||
+                   cmd == IOC_MDC_GETFILEINFO ||
+                   cmd == IOC_MDC_GETFILESTRIPE) {
                        filename = ll_getname((const char __user *)arg);
-                        if (IS_ERR(filename))
-                                RETURN(PTR_ERR(filename));
+                       if (IS_ERR(filename))
+                               RETURN(PTR_ERR(filename));
 
-                        rc = ll_lov_getstripe_ea_info(inode, filename, &lmm,
-                                                      &lmmsize, &request);
+                       rc = ll_lov_getstripe_ea_info(inode, filename, &lmm,
+                                                     &lmmsize, &request);
                } else {
-                       rc = ll_dir_getstripe(inode, (void **)&lmm, &lmmsize,
-                                             &request, 0);
+                       rc = ll_dir_getstripe_default(inode, (void **)&lmm,
+                                                     &lmmsize, &request,
+                                                     &root_request, 0);
                }
 
-                if (request) {
-                        body = req_capsule_server_get(&request->rq_pill,
-                                                      &RMF_MDT_BODY);
-                        LASSERT(body != NULL);
-                } else {
-                        GOTO(out_req, rc);
-                }
+               if (request) {
+                       body = req_capsule_server_get(&request->rq_pill,
+                                                     &RMF_MDT_BODY);
+                       LASSERT(body != NULL);
+               } else {
+                       GOTO(out_req, rc);
+               }
 
                if (rc == -ENODATA && (cmd == IOC_MDC_GETFILEINFO ||
-                                      cmd == LL_IOC_MDC_GETINFO)) {
+                                      cmd == LL_IOC_MDC_GETINFO ||
+                                      cmd == IOC_MDC_GETFILEINFO_OLD ||
+                                      cmd == LL_IOC_MDC_GETINFO_OLD)) {
                        lmmsize = 0;
                        rc = 0;
                }
@@ -1703,11 +1788,23 @@ out_rmdir:
                    cmd == LL_IOC_LOV_GETSTRIPE ||
                    cmd == LL_IOC_LOV_GETSTRIPE_NEW) {
                        lump = (struct lov_user_md __user *)arg;
-                } else {
+               } else if (cmd == IOC_MDC_GETFILEINFO_OLD ||
+                          cmd == LL_IOC_MDC_GETINFO_OLD){
+                       struct lov_user_mds_data_v1 __user *lmdp;
+
+                       lmdp = (struct lov_user_mds_data_v1 __user *)arg;
+                       statp = &lmdp->lmd_st;
+                       lump = &lmdp->lmd_lmm;
+               } else {
                        struct lov_user_mds_data __user *lmdp;
+
                        lmdp = (struct lov_user_mds_data __user *)arg;
-                        lump = &lmdp->lmd_lmm;
-                }
+                       fidp = &lmdp->lmd_fid;
+                       stxp = &lmdp->lmd_stx;
+                       flagsp = &lmdp->lmd_flags;
+                       lmmsizep = &lmdp->lmd_lmmsize;
+                       lump = &lmdp->lmd_lmm;
+               }
 
                if (lmmsize == 0) {
                        /* If the file has no striping then zero out *lump so
@@ -1720,9 +1817,9 @@ out_rmdir:
                        rc = -EOVERFLOW;
                }
 
-                if (cmd == IOC_MDC_GETFILEINFO || cmd == LL_IOC_MDC_GETINFO) {
-                       struct lov_user_mds_data __user *lmdp;
-                        lstat_t st = { 0 };
+               if (cmd == IOC_MDC_GETFILEINFO_OLD ||
+                   cmd == LL_IOC_MDC_GETINFO_OLD) {
+                       lstat_t st = { 0 };
 
                        st.st_dev       = inode->i_sb->s_dev;
                        st.st_mode      = body->mbo_mode;
@@ -1740,18 +1837,75 @@ out_rmdir:
                                                sbi->ll_flags &
                                                LL_SBI_32BIT_API);
 
-                       lmdp = (struct lov_user_mds_data __user *)arg;
-                       if (copy_to_user(&lmdp->lmd_st, &st, sizeof(st)))
-                                GOTO(out_req, rc = -EFAULT);
-                }
+                       if (copy_to_user(statp, &st, sizeof(st)))
+                               GOTO(out_req, rc = -EFAULT);
+               } else if (cmd == IOC_MDC_GETFILEINFO ||
+                          cmd == LL_IOC_MDC_GETINFO) {
+                       lstatx_t stx = { 0 };
+                       __u64 valid = body->mbo_valid;
+
+                       stx.stx_blksize = PAGE_SIZE;
+                       stx.stx_nlink = body->mbo_nlink;
+                       stx.stx_uid = body->mbo_uid;
+                       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;
+                       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_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;
 
-                EXIT;
-        out_req:
-                ptlrpc_req_finished(request);
-                if (filename)
-                        ll_putname(filename);
-                return rc;
-        }
+                       /*
+                        * For a striped directory, the size and blocks returned
+                        * from MDT is not correct.
+                        * The size and blocks are aggregated by client across
+                        * all stripes.
+                        * Thus for a striped directory, do not return the valid
+                        * FLSIZE and FLBLOCKS flags to the caller.
+                        * However, this whould be better decided by the MDS
+                        * instead of the client.
+                        */
+                       if (cmd == LL_IOC_MDC_GETINFO &&
+                           ll_i2info(inode)->lli_lsm_md != NULL)
+                               valid &= ~(OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
+
+                       if (flagsp && copy_to_user(flagsp, &valid,
+                                                  sizeof(*flagsp)))
+                               GOTO(out_req, rc = -EFAULT);
+
+                       if (fidp && copy_to_user(fidp, &body->mbo_fid1,
+                                                sizeof(*fidp)))
+                               GOTO(out_req, rc = -EFAULT);
+
+                       if (!(valid & OBD_MD_FLSIZE))
+                               stx.stx_mask &= ~STATX_SIZE;
+                       if (!(valid & OBD_MD_FLBLOCKS))
+                               stx.stx_mask &= ~STATX_BLOCKS;
+
+                       if (stxp && copy_to_user(stxp, &stx, sizeof(stx)))
+                               GOTO(out_req, rc = -EFAULT);
+
+                       if (lmmsizep && copy_to_user(lmmsizep, &lmmsize,
+                                                    sizeof(*lmmsizep)))
+                               GOTO(out_req, rc = -EFAULT);
+               }
+
+               EXIT;
+out_req:
+               ptlrpc_req_finished(request);
+               ptlrpc_req_finished(root_request);
+               if (filename)
+                       ll_putname(filename);
+               return rc;
+       }
        case OBD_IOC_QUOTACTL: {
                 struct if_quotactl *qctl;
 
@@ -2111,7 +2265,7 @@ const struct file_operations ll_dir_operations = {
        .release        = ll_dir_release,
        .read           = generic_read_dir,
 #ifdef HAVE_DIR_CONTEXT
-       .iterate        = ll_iterate,
+       .iterate_shared = ll_iterate,
 #else
        .readdir        = ll_readdir,
 #endif