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