Whamcloud - gitweb
LU-12169 llite: fill copied dentry name's ending char properly
[fs/lustre-release.git] / lustre / llite / file.c
index 6af68fa..4d39246 100644 (file)
@@ -139,8 +139,7 @@ static int ll_close_inode_openhandle(struct inode *inode,
 
        if (class_exp2obd(md_exp) == NULL) {
                CERROR("%s: invalid MDC connection handle closing "DFID"\n",
-                      ll_get_fsname(inode->i_sb, NULL, 0),
-                      PFID(&lli->lli_fid));
+                      ll_i2sbi(inode)->ll_fsname, PFID(&lli->lli_fid));
                GOTO(out, rc = 0);
        }
 
@@ -456,7 +455,7 @@ void ll_dom_finish_open(struct inode *inode, struct ptlrpc_request *req,
         */
        if (rnb->rnb_offset + rnb->rnb_len < i_size_read(inode)) {
                CERROR("%s: server returns off/len %llu/%u < i_size %llu\n",
-                      ll_get_fsname(inode->i_sb, NULL, 0), rnb->rnb_offset,
+                      ll_i2sbi(inode)->ll_fsname, rnb->rnb_offset,
                       rnb->rnb_len, i_size_read(inode));
                RETURN_EXIT;
        }
@@ -482,8 +481,8 @@ void ll_dom_finish_open(struct inode *inode, struct ptlrpc_request *req,
                if (IS_ERR(vmpage)) {
                        CWARN("%s: cannot fill page %lu for "DFID
                              " with data: rc = %li\n",
-                             ll_get_fsname(inode->i_sb, NULL, 0),
-                             index + start, PFID(lu_object_fid(&obj->co_lu)),
+                             ll_i2sbi(inode)->ll_fsname, index + start,
+                             PFID(lu_object_fid(&obj->co_lu)),
                              PTR_ERR(vmpage));
                        break;
                }
@@ -498,7 +497,7 @@ static int ll_intent_file_open(struct dentry *de, void *lmm, int lmmsize,
 {
        struct ll_sb_info *sbi = ll_i2sbi(de->d_inode);
        struct dentry *parent = de->d_parent;
-       const char *name = NULL;
+       char *name = NULL;
        int len = 0;
        struct md_op_data *op_data;
        struct ptlrpc_request *req = NULL;
@@ -510,21 +509,43 @@ static int ll_intent_file_open(struct dentry *de, void *lmm, int lmmsize,
 
        /* if server supports open-by-fid, or file name is invalid, don't pack
         * name in open request */
-       if (!(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_OPEN_BY_FID) &&
-           lu_name_is_valid_2(de->d_name.name, de->d_name.len)) {
-               name = de->d_name.name;
+       if (OBD_FAIL_CHECK(OBD_FAIL_LLITE_OPEN_BY_NAME) ||
+           !(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_OPEN_BY_FID)) {
+retry:
                len = de->d_name.len;
+               name = kmalloc(len + 1, GFP_NOFS);
+               if (!name)
+                       RETURN(-ENOMEM);
+
+               /* race here */
+               spin_lock(&de->d_lock);
+               if (len != de->d_name.len) {
+                       spin_unlock(&de->d_lock);
+                       kfree(name);
+                       goto retry;
+               }
+               memcpy(name, de->d_name.name, len);
+               name[len] = '\0';
+               spin_unlock(&de->d_lock);
+
+               if (!lu_name_is_valid_2(name, len)) {
+                       kfree(name);
+                       RETURN(-ESTALE);
+               }
        }
 
        op_data = ll_prep_md_op_data(NULL, parent->d_inode, de->d_inode,
                                     name, len, 0, LUSTRE_OPC_ANY, NULL);
-       if (IS_ERR(op_data))
+       if (IS_ERR(op_data)) {
+               kfree(name);
                RETURN(PTR_ERR(op_data));
+       }
        op_data->op_data = lmm;
        op_data->op_data_size = lmmsize;
 
        rc = md_intent_lock(sbi->ll_md_exp, op_data, itp, &req,
                            &ll_md_blocking_ast, 0);
+       kfree(name);
        ll_finish_md_op_data(op_data);
        if (rc == -ESTALE) {
                /* reason for keep own exit path - don`t flood log
@@ -1032,8 +1053,7 @@ out_close:
        rc2 = ll_close_inode_openhandle(inode, och, 0, NULL);
        if (rc2 < 0)
                CERROR("%s: error closing file "DFID": %d\n",
-                      ll_get_fsname(inode->i_sb, NULL, 0),
-                      PFID(&ll_i2info(inode)->lli_fid), rc2);
+                      sbi->ll_fsname, PFID(&ll_i2info(inode)->lli_fid), rc2);
        och = NULL; /* och has been freed in ll_close_inode_openhandle() */
 out_release_it:
        ll_intent_release(&it);
@@ -1077,7 +1097,7 @@ static int ll_swap_layouts_close(struct obd_client_handle *och,
        ENTRY;
 
        CDEBUG(D_INODE, "%s: biased close of file "DFID"\n",
-              ll_get_fsname(inode->i_sb, NULL, 0), PFID(fid1));
+              ll_i2sbi(inode)->ll_fsname, PFID(fid1));
 
        rc = ll_check_swap_layouts_validity(inode, inode2);
        if (rc < 0)
@@ -1349,6 +1369,37 @@ static void ll_io_init(struct cl_io *io, struct file *file, enum cl_io_type iot)
        ll_io_set_mirror(io, file);
 }
 
+static void ll_heat_add(struct inode *inode, enum cl_io_type iot,
+                       __u64 count)
+{
+       struct ll_inode_info *lli = ll_i2info(inode);
+       struct ll_sb_info *sbi = ll_i2sbi(inode);
+       enum obd_heat_type sample_type;
+       enum obd_heat_type iobyte_type;
+       __u64 now = ktime_get_real_seconds();
+
+       if (!ll_sbi_has_file_heat(sbi) ||
+           lli->lli_heat_flags & LU_HEAT_FLAG_OFF)
+               return;
+
+       if (iot == CIT_READ) {
+               sample_type = OBD_HEAT_READSAMPLE;
+               iobyte_type = OBD_HEAT_READBYTE;
+       } else if (iot == CIT_WRITE) {
+               sample_type = OBD_HEAT_WRITESAMPLE;
+               iobyte_type = OBD_HEAT_WRITEBYTE;
+       } else {
+               return;
+       }
+
+       spin_lock(&lli->lli_heat_lock);
+       obd_heat_add(&lli->lli_heat_instances[sample_type], now, 1,
+                    sbi->ll_heat_decay_weight, sbi->ll_heat_period_second);
+       obd_heat_add(&lli->lli_heat_instances[iobyte_type], now, count,
+                    sbi->ll_heat_decay_weight, sbi->ll_heat_period_second);
+       spin_unlock(&lli->lli_heat_lock);
+}
+
 static ssize_t
 ll_file_io_generic(const struct lu_env *env, struct vvp_io_args *args,
                   struct file *file, enum cl_io_type iot,
@@ -1479,6 +1530,8 @@ out:
        }
 
        CDEBUG(D_VFSTRACE, "iot: %d, result: %zd\n", iot, result);
+       if (result > 0)
+               ll_heat_add(inode, iot, result);
 
        RETURN(result > 0 ? result : rc);
 }
@@ -1539,9 +1592,11 @@ ll_do_fast_read(struct kiocb *iocb, struct iov_iter *iter)
        if (result == -ENODATA)
                result = 0;
 
-       if (result > 0)
+       if (result > 0) {
+               ll_heat_add(file_inode(iocb->ki_filp), CIT_READ, result);
                ll_stats_ops_tally(ll_i2sbi(file_inode(iocb->ki_filp)),
                                LPROC_LL_READ_BYTES, result);
+       }
 
        return result;
 }
@@ -1601,8 +1656,9 @@ out:
 static ssize_t ll_do_tiny_write(struct kiocb *iocb, struct iov_iter *iter)
 {
        ssize_t count = iov_iter_count(iter);
-       struct file *file = iocb->ki_filp;
-       struct inode *inode = file_inode(file);
+       struct  file *file = iocb->ki_filp;
+       struct  inode *inode = file_inode(file);
+       bool    lock_inode = !IS_NOSEC(inode);
        ssize_t result = 0;
 
        ENTRY;
@@ -1614,8 +1670,13 @@ static ssize_t ll_do_tiny_write(struct kiocb *iocb, struct iov_iter *iter)
            (iocb->ki_pos & (PAGE_SIZE-1)) + count > PAGE_SIZE)
                RETURN(0);
 
+       if (unlikely(lock_inode))
+               inode_lock(inode);
        result = __generic_file_write_iter(iocb, iter);
 
+       if (unlikely(lock_inode))
+               inode_unlock(inode);
+
        /* If the page is not already dirty, ll_tiny_write_begin returns
         * -ENODATA.  We continue on to normal write.
         */
@@ -1623,6 +1684,7 @@ static ssize_t ll_do_tiny_write(struct kiocb *iocb, struct iov_iter *iter)
                result = 0;
 
        if (result > 0) {
+               ll_heat_add(inode, CIT_WRITE, result);
                ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_WRITE_BYTES,
                                   result);
                ll_file_set_flag(ll_i2info(inode), LLIF_DATA_MODIFIED);
@@ -2383,7 +2445,7 @@ int ll_hsm_release(struct inode *inode)
        ENTRY;
 
        CDEBUG(D_INODE, "%s: Releasing file "DFID".\n",
-              ll_get_fsname(inode->i_sb, NULL, 0),
+              ll_i2sbi(inode)->ll_fsname,
               PFID(&ll_i2info(inode)->lli_fid));
 
        och = ll_lease_open(inode, NULL, FMODE_WRITE, MDS_OPEN_RELEASE);
@@ -2791,6 +2853,7 @@ static const char *const ladvise_names[] = LU_LADVISE_NAMES;
 static int ll_ladvise_sanity(struct inode *inode,
                             struct llapi_lu_ladvise *ladvise)
 {
+       struct ll_sb_info *sbi = ll_i2sbi(inode);
        enum lu_ladvise_type advice = ladvise->lla_advice;
        /* Note the peradvice flags is a 32 bit field, so per advice flags must
         * be in the first 32 bits of enum ladvise_flags */
@@ -2802,7 +2865,7 @@ static int ll_ladvise_sanity(struct inode *inode,
                rc = -EINVAL;
                CDEBUG(D_VFSTRACE, "%s: advice with value '%d' not recognized,"
                       "last supported advice is %s (value '%d'): rc = %d\n",
-                      ll_get_fsname(inode->i_sb, NULL, 0), advice,
+                      sbi->ll_fsname, advice,
                       ladvise_names[LU_LADVISE_MAX-1], LU_LADVISE_MAX-1, rc);
                GOTO(out, rc);
        }
@@ -2813,8 +2876,7 @@ static int ll_ladvise_sanity(struct inode *inode,
                if (flags & ~LF_LOCKNOEXPAND_MASK) {
                        rc = -EINVAL;
                        CDEBUG(D_VFSTRACE, "%s: Invalid flags (%x) for %s: "
-                              "rc = %d\n",
-                              ll_get_fsname(inode->i_sb, NULL, 0), flags,
+                              "rc = %d\n", sbi->ll_fsname, flags,
                               ladvise_names[advice], rc);
                        GOTO(out, rc);
                }
@@ -2825,8 +2887,7 @@ static int ll_ladvise_sanity(struct inode *inode,
                    ladvise->lla_lockahead_mode == 0) {
                        rc = -EINVAL;
                        CDEBUG(D_VFSTRACE, "%s: Invalid mode (%d) for %s: "
-                              "rc = %d\n",
-                              ll_get_fsname(inode->i_sb, NULL, 0),
+                              "rc = %d\n", sbi->ll_fsname,
                               ladvise->lla_lockahead_mode,
                               ladvise_names[advice], rc);
                        GOTO(out, rc);
@@ -2839,16 +2900,14 @@ static int ll_ladvise_sanity(struct inode *inode,
                if (flags & ~LF_DEFAULT_MASK) {
                        rc = -EINVAL;
                        CDEBUG(D_VFSTRACE, "%s: Invalid flags (%x) for %s: "
-                              "rc = %d\n",
-                              ll_get_fsname(inode->i_sb, NULL, 0), flags,
+                              "rc = %d\n", sbi->ll_fsname, flags,
                               ladvise_names[advice], rc);
                        GOTO(out, rc);
                }
                if (ladvise->lla_start >= ladvise->lla_end) {
                        rc = -EINVAL;
                        CDEBUG(D_VFSTRACE, "%s: Invalid range (%llu to %llu) "
-                              "for %s: rc = %d\n",
-                              ll_get_fsname(inode->i_sb, NULL, 0),
+                              "for %s: rc = %d\n", sbi->ll_fsname,
                               ladvise->lla_start, ladvise->lla_end,
                               ladvise_names[advice], rc);
                        GOTO(out, rc);
@@ -3212,6 +3271,41 @@ static long ll_file_set_lease(struct file *file, struct ll_ioc_lease *ioc,
        RETURN(rc);
 }
 
+static void ll_heat_get(struct inode *inode, struct lu_heat *heat)
+{
+       struct ll_inode_info *lli = ll_i2info(inode);
+       struct ll_sb_info *sbi = ll_i2sbi(inode);
+       __u64 now = ktime_get_real_seconds();
+       int i;
+
+       spin_lock(&lli->lli_heat_lock);
+       heat->lh_flags = lli->lli_heat_flags;
+       for (i = 0; i < heat->lh_count; i++)
+               heat->lh_heat[i] = obd_heat_get(&lli->lli_heat_instances[i],
+                                               now, sbi->ll_heat_decay_weight,
+                                               sbi->ll_heat_period_second);
+       spin_unlock(&lli->lli_heat_lock);
+}
+
+static int ll_heat_set(struct inode *inode, __u64 flags)
+{
+       struct ll_inode_info *lli = ll_i2info(inode);
+       int rc = 0;
+
+       spin_lock(&lli->lli_heat_lock);
+       if (flags & LU_HEAT_FLAG_CLEAR)
+               obd_heat_clear(lli->lli_heat_instances, OBD_HEAT_COUNT);
+
+       if (flags & LU_HEAT_FLAG_OFF)
+               lli->lli_heat_flags |= LU_HEAT_FLAG_OFF;
+       else
+               lli->lli_heat_flags &= ~LU_HEAT_FLAG_OFF;
+
+       spin_unlock(&lli->lli_heat_lock);
+
+       RETURN(rc);
+}
+
 static long
 ll_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 {
@@ -3595,6 +3689,37 @@ out_ladvise:
                RETURN(ll_ioctl_fssetxattr(inode, cmd, arg));
        case BLKSSZGET:
                RETURN(put_user(PAGE_SIZE, (int __user *)arg));
+       case LL_IOC_HEAT_GET: {
+               struct lu_heat uheat;
+               struct lu_heat *heat;
+               int size;
+
+               if (copy_from_user(&uheat, (void __user *)arg, sizeof(uheat)))
+                       RETURN(-EFAULT);
+
+               if (uheat.lh_count > OBD_HEAT_COUNT)
+                       uheat.lh_count = OBD_HEAT_COUNT;
+
+               size = offsetof(typeof(uheat), lh_heat[uheat.lh_count]);
+               OBD_ALLOC(heat, size);
+               if (heat == NULL)
+                       RETURN(-ENOMEM);
+
+               heat->lh_count = uheat.lh_count;
+               ll_heat_get(inode, heat);
+               rc = copy_to_user((char __user *)arg, heat, size);
+               OBD_FREE(heat, size);
+               RETURN(rc ? -EFAULT : 0);
+       }
+       case LL_IOC_HEAT_SET: {
+               __u64 flags;
+
+               if (copy_from_user(&flags, (void __user *)arg, sizeof(flags)))
+                       RETURN(-EFAULT);
+
+               rc = ll_heat_set(inode, flags);
+               RETURN(rc);
+       }
        default:
                RETURN(obd_iocontrol(cmd, ll_i2dtexp(inode), 0, NULL,
                                     (void __user *)arg));
@@ -4073,8 +4198,7 @@ int ll_migrate(struct inode *parent, struct file *file, struct lmv_user_md *lum,
                if (le32_to_cpu(lum->lum_stripe_count) > 1 ||
                    ll_i2info(child_inode)->lli_lsm_md) {
                        CERROR("%s: MDT doesn't support stripe directory "
-                              "migration!\n",
-                              ll_get_fsname(parent->i_sb, NULL, 0));
+                              "migration!\n", ll_i2sbi(parent)->ll_fsname);
                        GOTO(out_iput, rc = -EOPNOTSUPP);
                }
        }
@@ -4096,7 +4220,7 @@ int ll_migrate(struct inode *parent, struct file *file, struct lmv_user_md *lum,
        op_data->op_fid3 = *ll_inode2fid(child_inode);
        if (!fid_is_sane(&op_data->op_fid3)) {
                CERROR("%s: migrate %s, but FID "DFID" is insane\n",
-                      ll_get_fsname(parent->i_sb, NULL, 0), name,
+                      ll_i2sbi(parent)->ll_fsname, name,
                       PFID(&op_data->op_fid3));
                GOTO(out_unlock, rc = -EINVAL);
        }
@@ -4134,7 +4258,9 @@ again:
        if (rc == 0) {
                LASSERT(request != NULL);
                ll_update_times(request, parent);
+       }
 
+       if (rc == 0 || rc == -EAGAIN) {
                body = req_capsule_server_get(&request->rq_pill, &RMF_MDT_BODY);
                LASSERT(body != NULL);
 
@@ -4155,7 +4281,7 @@ again:
                request = NULL;
        }
 
-       /* Try again if the file layout has changed. */
+       /* Try again if the lease has cancelled. */
        if (rc == -EAGAIN && S_ISREG(child_inode->i_mode))
                goto again;
 
@@ -4269,7 +4395,7 @@ static int ll_inode_revalidate_fini(struct inode *inode, int rc)
        } else if (rc != 0) {
                CDEBUG_LIMIT((rc == -EACCES || rc == -EIDRM) ? D_INFO : D_ERROR,
                             "%s: revalidate FID "DFID" error: rc = %d\n",
-                            ll_get_fsname(inode->i_sb, NULL, 0),
+                            ll_i2sbi(inode)->ll_fsname,
                             PFID(ll_inode2fid(inode)), rc);
        }
 
@@ -4944,8 +5070,7 @@ out:
        /* wait for IO to complete if it's still being used. */
        if (wait_layout) {
                CDEBUG(D_INODE, "%s: "DFID"(%p) wait for layout reconf\n",
-                      ll_get_fsname(inode->i_sb, NULL, 0),
-                      PFID(&lli->lli_fid), inode);
+                      sbi->ll_fsname, PFID(&lli->lli_fid), inode);
 
                memset(&conf, 0, sizeof conf);
                conf.coc_opc = OBJECT_CONF_WAIT;
@@ -4955,8 +5080,7 @@ out:
                        rc = -EAGAIN;
 
                CDEBUG(D_INODE, "%s file="DFID" waiting layout return: %d\n",
-                      ll_get_fsname(inode->i_sb, NULL, 0),
-                      PFID(&lli->lli_fid), rc);
+                      sbi->ll_fsname, PFID(&lli->lli_fid), rc);
        }
        RETURN(rc);
 }
@@ -4994,8 +5118,7 @@ static int ll_layout_intent(struct inode *inode, struct layout_intent *intent)
                it.it_flags = FMODE_WRITE;
 
        LDLM_DEBUG_NOLOCK("%s: requeue layout lock for file "DFID"(%p)",
-                         ll_get_fsname(inode->i_sb, NULL, 0),
-                         PFID(&lli->lli_fid), inode);
+                         sbi->ll_fsname, PFID(&lli->lli_fid), inode);
 
        rc = md_intent_lock(sbi->ll_md_exp, op_data, &it, &req,
                            &ll_md_blocking_ast, 0);