Whamcloud - gitweb
LU-14687 llite: Return errors for aio
[fs/lustre-release.git] / lustre / mdd / mdd_trans.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * lustre/mdd/mdd_trans.c
32  *
33  * Lustre Metadata Server (mdd) routines
34  *
35  * Author: Wang Di <wangdi@clusterfs.com>
36  */
37
38 #define DEBUG_SUBSYSTEM S_MDS
39
40 #include <linux/kthread.h>
41
42 #include <obd_class.h>
43 #include <lprocfs_status.h>
44 #include <lustre_mds.h>
45 #include <lustre_barrier.h>
46
47 #include "mdd_internal.h"
48
49 struct thandle *mdd_trans_create(const struct lu_env *env,
50                                  struct mdd_device *mdd)
51 {
52         struct thandle *th;
53         struct lu_ucred *uc = lu_ucred_check(env);
54
55         /* If blocked by the write barrier, then return "-EINPROGRESS"
56          * to the caller. Usually, such error will be forwarded to the
57          * client, and the expected behaviour is to re-try such modify
58          * RPC some time later until the barrier is thawed or expired. */
59         if (unlikely(!barrier_entry(mdd->mdd_bottom)))
60                 return ERR_PTR(-EINPROGRESS);
61
62         th = mdd_child_ops(mdd)->dt_trans_create(env, mdd->mdd_child);
63         if (!IS_ERR(th) && uc)
64                 th->th_ignore_quota = !!md_capable(uc, CAP_SYS_RESOURCE);
65
66         return th;
67 }
68
69 int mdd_trans_start(const struct lu_env *env, struct mdd_device *mdd,
70                     struct thandle *th)
71 {
72         return mdd_child_ops(mdd)->dt_trans_start(env, mdd->mdd_child, th);
73 }
74
75 struct mdd_changelog_gc {
76         struct mdd_device *mcgc_mdd;
77         __u32 mcgc_id;
78         __u32 mcgc_maxtime;
79         __u64 mcgc_maxindexes;
80         bool mcgc_found;
81 };
82
83 /* return first registered ChangeLog user idle since too long
84  * use ChangeLog's user plain LLOG mtime for this */
85 static int mdd_changelog_gc_cb(const struct lu_env *env,
86                                struct llog_handle *llh,
87                                struct llog_rec_hdr *hdr, void *data)
88 {
89         struct llog_changelog_user_rec *rec;
90         struct mdd_changelog_gc *mcgc = (struct mdd_changelog_gc *)data;
91         struct mdd_device *mdd = mcgc->mcgc_mdd;
92         ENTRY;
93
94         if ((llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN) == 0)
95                 RETURN(-ENXIO);
96
97         rec = container_of(hdr, struct llog_changelog_user_rec,
98                            cur_hdr);
99
100         /* find oldest idle user, based on last record update/cancel time (new
101          * behavior), or for old user records, last record index vs current
102          * ChangeLog index. Late users with old record format will be treated
103          * first as we assume they could be idle since longer
104          */
105         if (rec->cur_time != 0) {
106                 /* FIXME !!!! cur_time is a timestamp but only 32 bit in
107                  * in size. This is not 2038 safe !!!!
108                  */
109                 u32 time_now = (u32)ktime_get_real_seconds();
110                 timeout_t time_out = rec->cur_time +
111                                      mdd->mdd_changelog_max_idle_time;
112                 timeout_t idle_time = time_now - rec->cur_time;
113
114                 /* treat oldest idle user first, and if no old format user
115                  * has been already selected
116                  */
117                 if (time_after32(time_now, time_out) &&
118                     idle_time > mcgc->mcgc_maxtime &&
119                     mcgc->mcgc_maxindexes == 0) {
120                         mcgc->mcgc_maxtime = idle_time;
121                         mcgc->mcgc_id = rec->cur_id;
122                         mcgc->mcgc_found = true;
123                 }
124         } else {
125                 /* old user record with no idle time stamp, so use empirical
126                  * method based on its current index/position
127                  */
128                 __u64 idle_indexes;
129
130                 idle_indexes = mdd->mdd_cl.mc_index - rec->cur_endrec;
131
132                 /* treat user with the oldest/smallest current index first */
133                 if (idle_indexes >= mdd->mdd_changelog_max_idle_indexes &&
134                     idle_indexes > mcgc->mcgc_maxindexes) {
135                         mcgc->mcgc_maxindexes = idle_indexes;
136                         mcgc->mcgc_id = rec->cur_id;
137                         mcgc->mcgc_found = true;
138                 }
139
140         }
141         RETURN(0);
142 }
143
144 /* recover space from long-term inactive ChangeLog users */
145 static int mdd_chlg_garbage_collect(void *data)
146 {
147         struct mdd_device *mdd = (struct mdd_device *)data;
148         struct lu_env             *env = NULL;
149         int                        rc;
150         struct llog_ctxt *ctxt;
151         struct mdd_changelog_gc mcgc = {
152                 .mcgc_mdd = mdd,
153                 .mcgc_found = false,
154                 .mcgc_maxtime = 0,
155                 .mcgc_maxindexes = 0,
156         };
157         ENTRY;
158
159         mdd->mdd_cl.mc_gc_task = current;
160
161         CDEBUG(D_HA, "%s: ChangeLog garbage collect thread start with PID %d\n",
162                mdd2obd_dev(mdd)->obd_name, current->pid);
163
164         OBD_ALLOC_PTR(env);
165         if (env == NULL)
166                 GOTO(out, rc = -ENOMEM);
167
168         rc = lu_env_init(env, LCT_MD_THREAD);
169         if (rc)
170                 GOTO(out, rc);
171
172         for (;;) {
173                 ctxt = llog_get_context(mdd2obd_dev(mdd),
174                                         LLOG_CHANGELOG_USER_ORIG_CTXT);
175                 if (ctxt == NULL ||
176                     (ctxt->loc_handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) == 0)
177                         GOTO(out_ctxt, rc = -ENXIO);
178
179                 rc = llog_cat_process(env, ctxt->loc_handle,
180                                       mdd_changelog_gc_cb, &mcgc, 0, 0);
181                 if (rc != 0 || mcgc.mcgc_found == false)
182                         break;
183                 llog_ctxt_put(ctxt);
184
185                 if (mcgc.mcgc_maxindexes != 0)
186                         CWARN("%s: Force deregister of ChangeLog user cl%d "
187                               "idle with more than %llu unprocessed records\n",
188                               mdd2obd_dev(mdd)->obd_name, mcgc.mcgc_id,
189                               mcgc.mcgc_maxindexes);
190                 else
191                         CWARN("%s: Force deregister of ChangeLog user cl%d "
192                               "idle since more than %us\n",
193                               mdd2obd_dev(mdd)->obd_name, mcgc.mcgc_id,
194                               mcgc.mcgc_maxtime);
195
196                 mdd_changelog_user_purge(env, mdd, mcgc.mcgc_id);
197
198                 if (kthread_should_stop())
199                         GOTO(out_env, rc = 0);
200
201                 /* try again to search for another candidate */
202                 mcgc.mcgc_found = false;
203                 mcgc.mcgc_maxtime = 0;
204                 mcgc.mcgc_maxindexes = 0;
205         }
206
207 out_ctxt:
208         if (ctxt != NULL)
209                 llog_ctxt_put(ctxt);
210
211 out_env:
212         lu_env_fini(env);
213         GOTO(out, rc);
214 out:
215         if (env)
216                 OBD_FREE_PTR(env);
217
218         spin_lock(&mdd->mdd_cl.mc_lock);
219         mdd->mdd_cl.mc_gc_task = MDD_CHLG_GC_NONE;
220         spin_unlock(&mdd->mdd_cl.mc_lock);
221
222         return rc;
223 }
224
225 int mdd_trans_stop(const struct lu_env *env, struct mdd_device *mdd,
226                    int result, struct thandle *handle)
227 {
228         int rc;
229
230         handle->th_result = result;
231         rc = mdd_child_ops(mdd)->dt_trans_stop(env, mdd->mdd_child, handle);
232         barrier_exit(mdd->mdd_bottom);
233
234         /* bottom half of changelog garbage-collection mechanism, started
235          * from mdd_changelog_store(). This is required, as running a
236          * kthead can't occur during a journal transaction is being filled
237          * because otherwise a deadlock can happen if memory reclaim is
238          * triggered by kthreadd when forking the new thread, and thus
239          * I/Os could be attempted to the same device from shrinkers
240          * requiring a new journal transaction to be started when current
241          * could never complete (LU-10680).
242          */
243         if (unlikely(mdd->mdd_cl.mc_flags & CLM_ON &&
244                      cmpxchg(&mdd->mdd_cl.mc_gc_task, MDD_CHLG_GC_NEED,
245                              MDD_CHLG_GC_START) == MDD_CHLG_GC_NEED)) {
246                 /* XXX we may want to cmpxchg() only if MDD_CHLG_GC_NEED
247                  * to save its cost in the frequent case and have an extra
248                  * if/test cost in the rare case where we need to spawn?
249                  */
250                 struct task_struct *gc_task;
251                 struct obd_device *obd = mdd2obd_dev(mdd);
252
253                 gc_task = kthread_run(mdd_chlg_garbage_collect, mdd,
254                                       "chlg_gc_thread");
255                 if (IS_ERR(gc_task)) {
256                         CERROR("%s: cannot start ChangeLog garbage collection "
257                                "thread: rc = %ld\n", obd->obd_name,
258                                PTR_ERR(gc_task));
259                         mdd->mdd_cl.mc_gc_task = MDD_CHLG_GC_NONE;
260                 } else {
261                         CDEBUG(D_HA, "%s: a ChangeLog garbage collection "
262                                "thread has been started\n", obd->obd_name);
263                 }
264         }
265
266         /* if operation failed, return \a result, otherwise return status of
267          * dt_trans_stop */
268         return result ?: rc;
269 }