Whamcloud - gitweb
LU-8569 linkea: linkEA size limitation
[fs/lustre-release.git] / lustre / mdt / mdt_recovery.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, 2014, 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/mdt/mdt_recovery.c
33  *
34  * Lustre Metadata Target (mdt) recovery-related methods
35  *
36  * Author: Huang Hua <huanghua@clusterfs.com>
37  * Author: Pershin Mike <tappro@clusterfs.com>
38  */
39
40 #define DEBUG_SUBSYSTEM S_MDS
41
42 #include "mdt_internal.h"
43
44 struct lu_buf *mdt_buf(const struct lu_env *env, void *area, ssize_t len)
45 {
46         struct lu_buf *buf;
47         struct mdt_thread_info *mti;
48
49         mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
50         buf = &mti->mti_buf;
51         buf->lb_buf = area;
52         buf->lb_len = len;
53         return buf;
54 }
55
56 const struct lu_buf *mdt_buf_const(const struct lu_env *env,
57                                    const void *area, ssize_t len)
58 {
59         struct lu_buf *buf;
60         struct mdt_thread_info *mti;
61
62         mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
63         buf = &mti->mti_buf;
64
65         buf->lb_buf = (void *)area;
66         buf->lb_len = len;
67         return buf;
68 }
69
70 /*
71  * last_rcvd & last_committed update callbacks
72  */
73 extern struct lu_context_key mdt_thread_key;
74
75 /* This callback notifies MDT that transaction was done. This is needed by
76  * mdt_save_lock() only. It is similar to new target code and will be removed
77  * as mdt_save_lock() will be converted to use target structures */
78 static int mdt_txn_stop_cb(const struct lu_env *env,
79                            struct thandle *txn, void *cookie)
80 {
81         struct mdt_thread_info  *mti;
82
83         mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
84         LASSERT(mti);
85
86         if (mti->mti_has_trans)
87                 CDEBUG(D_INFO, "More than one transaction\n");
88         else
89                 mti->mti_has_trans = 1;
90         return 0;
91 }
92
93 int mdt_fs_setup(const struct lu_env *env, struct mdt_device *mdt,
94                  struct obd_device *obd, struct lustre_sb_info *lsi)
95 {
96         int rc = 0;
97
98         ENTRY;
99
100         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_FS_SETUP))
101                 RETURN(-ENOENT);
102
103         /* prepare transactions callbacks */
104         mdt->mdt_txn_cb.dtc_txn_start = NULL;
105         mdt->mdt_txn_cb.dtc_txn_stop = mdt_txn_stop_cb;
106         mdt->mdt_txn_cb.dtc_txn_commit = NULL;
107         mdt->mdt_txn_cb.dtc_cookie = NULL;
108         mdt->mdt_txn_cb.dtc_tag = LCT_MD_THREAD;
109         INIT_LIST_HEAD(&mdt->mdt_txn_cb.dtc_linkage);
110
111         dt_txn_callback_add(mdt->mdt_bottom, &mdt->mdt_txn_cb);
112
113         RETURN(rc);
114 }
115
116 void mdt_fs_cleanup(const struct lu_env *env, struct mdt_device *mdt)
117 {
118         ENTRY;
119
120         /* Remove transaction callback */
121         dt_txn_callback_del(mdt->mdt_bottom, &mdt->mdt_txn_cb);
122         EXIT;
123 }
124
125 /* reconstruction code */
126 static void mdt_steal_ack_locks(struct ptlrpc_request *req)
127 {
128         struct ptlrpc_service_part *svcpt;
129         struct obd_export          *exp = req->rq_export;
130         struct list_head           *tmp;
131         struct ptlrpc_reply_state  *oldrep;
132         int                         i;
133
134         /* CAVEAT EMPTOR: spinlock order */
135         spin_lock(&exp->exp_lock);
136         list_for_each(tmp, &exp->exp_outstanding_replies) {
137                 oldrep = list_entry(tmp, struct ptlrpc_reply_state,
138                                     rs_exp_list);
139
140                 if (oldrep->rs_xid != req->rq_xid)
141                         continue;
142
143                 if (oldrep->rs_opc != lustre_msg_get_opc(req->rq_reqmsg))
144                         CERROR("%s: Resent req xid %llu has mismatched opc: "
145                                "new %d old %d\n", exp->exp_obd->obd_name,
146                                req->rq_xid, lustre_msg_get_opc(req->rq_reqmsg),
147                                oldrep->rs_opc);
148
149                 svcpt = oldrep->rs_svcpt;
150                 spin_lock(&svcpt->scp_rep_lock);
151
152                 list_del_init(&oldrep->rs_exp_list);
153
154                 CDEBUG(D_HA, "Stealing %d locks from rs %p x%lld.t%lld"
155                        " o%d NID %s\n",
156                        oldrep->rs_nlocks, oldrep,
157                        oldrep->rs_xid, oldrep->rs_transno, oldrep->rs_opc,
158                        libcfs_nid2str(exp->exp_connection->c_peer.nid));
159
160                 for (i = 0; i < oldrep->rs_nlocks; i++)
161                         ptlrpc_save_lock(req, &oldrep->rs_locks[i],
162                                          oldrep->rs_modes[i], 0);
163                 oldrep->rs_nlocks = 0;
164
165                 DEBUG_REQ(D_HA, req, "stole locks for");
166                 spin_lock(&oldrep->rs_lock);
167                 ptlrpc_schedule_difficult_reply(oldrep);
168                 spin_unlock(&oldrep->rs_lock);
169
170                 spin_unlock(&svcpt->scp_rep_lock);
171                 break;
172         }
173         spin_unlock(&exp->exp_lock);
174 }
175
176 __u64 mdt_req_from_lrd(struct ptlrpc_request *req,
177                        struct tg_reply_data *trd)
178 {
179         struct lsd_reply_data *lrd;
180
181         LASSERT(trd != NULL);
182         lrd = &trd->trd_reply;
183
184         DEBUG_REQ(D_HA, req, "restoring transno %lld/status %d",
185                   lrd->lrd_transno, lrd->lrd_result);
186
187         req->rq_transno = lrd->lrd_transno;
188         req->rq_status = lrd->lrd_result;
189
190         lustre_msg_set_versions(req->rq_repmsg, trd->trd_pre_versions);
191
192         if (req->rq_status != 0)
193                 req->rq_transno = 0;
194         lustre_msg_set_transno(req->rq_repmsg, req->rq_transno);
195         lustre_msg_set_status(req->rq_repmsg, req->rq_status);
196
197         DEBUG_REQ(D_RPCTRACE, req, "restoring transno %lld/status %d",
198                   req->rq_transno, req->rq_status);
199
200         mdt_steal_ack_locks(req);
201
202         return lrd->lrd_data;
203 }
204
205
206 void mdt_reconstruct_generic(struct mdt_thread_info *mti,
207                              struct mdt_lock_handle *lhc)
208 {
209         struct ptlrpc_request *req = mdt_info_req(mti);
210
211         mdt_req_from_lrd(req, mti->mti_reply_data);
212         return;
213 }
214
215 /**
216  * Generate fake attributes for a non-existing object
217  *
218  * While the client was waiting for the reply, the original transaction
219  * got committed and corresponding rep-ack lock got released, then another
220  * client was able to destroy the object. But we still need to send some
221  * attributes back. So we fake them and set nlink=0, so the client will
222  * be able to detect a non-existing object and drop it from the cache
223  * immediately.
224  *
225  * \param[out] ma       attributes to fill
226  */
227 static void mdt_fake_ma(struct md_attr *ma)
228 {
229         ma->ma_valid = MA_INODE;
230         memset(&ma->ma_attr, 0, sizeof(ma->ma_attr));
231         ma->ma_attr.la_valid = LA_NLINK;
232         ma->ma_attr.la_mode = S_IFREG;
233 }
234
235 static void mdt_reconstruct_create(struct mdt_thread_info *mti,
236                                    struct mdt_lock_handle *lhc)
237 {
238         struct ptlrpc_request  *req = mdt_info_req(mti);
239         struct obd_export *exp = req->rq_export;
240         struct mdt_device *mdt = mti->mti_mdt;
241         struct mdt_object *child;
242         struct mdt_body *body;
243         int rc;
244
245         mdt_req_from_lrd(req, mti->mti_reply_data);
246         if (req->rq_status)
247                 return;
248
249         /* if no error, so child was created with requested fid */
250         child = mdt_object_find(mti->mti_env, mdt, mti->mti_rr.rr_fid2);
251         if (IS_ERR(child)) {
252                 rc = PTR_ERR(child);
253                 LCONSOLE_WARN("cannot lookup child "DFID": rc = %d; "
254                               "evicting client %s with export %s\n",
255                               PFID(mti->mti_rr.rr_fid2), rc,
256                               obd_uuid2str(&exp->exp_client_uuid),
257                               obd_export_nid2str(exp));
258                 mdt_export_evict(exp);
259                 RETURN_EXIT;
260         }
261
262         body = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
263         mti->mti_attr.ma_need = MA_INODE;
264         mti->mti_attr.ma_valid = 0;
265         rc = mdt_attr_get_complex(mti, child, &mti->mti_attr);
266         if (rc == -ENOENT) {
267                 mdt_fake_ma(&mti->mti_attr);
268         } else if (rc == -EREMOTE) {
269                 /* object was created on remote server */
270                 if (!mdt_is_dne_client(exp))
271                         /* Return -EIO for old client */
272                         rc = -EIO;
273
274                 req->rq_status = rc;
275                 body->mbo_valid |= OBD_MD_MDS;
276         }
277         mdt_pack_attr2body(mti, body, &mti->mti_attr.ma_attr,
278                            mdt_object_fid(child));
279         mdt_object_put(mti->mti_env, child);
280 }
281
282 static void mdt_reconstruct_setattr(struct mdt_thread_info *mti,
283                                     struct mdt_lock_handle *lhc)
284 {
285         struct ptlrpc_request  *req = mdt_info_req(mti);
286         struct obd_export *exp = req->rq_export;
287         struct mdt_device *mdt = mti->mti_mdt;
288         struct mdt_object *obj;
289         struct mdt_body *body;
290         int rc;
291
292         mdt_req_from_lrd(req, mti->mti_reply_data);
293         if (req->rq_status)
294                 return;
295
296         body = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
297         obj = mdt_object_find(mti->mti_env, mdt, mti->mti_rr.rr_fid1);
298         if (IS_ERR(obj)) {
299                 rc = PTR_ERR(obj);
300                 LCONSOLE_WARN("cannot lookup "DFID": rc = %d; "
301                               "evicting client %s with export %s\n",
302                               PFID(mti->mti_rr.rr_fid1), rc,
303                               obd_uuid2str(&exp->exp_client_uuid),
304                               obd_export_nid2str(exp));
305                 mdt_export_evict(exp);
306                 RETURN_EXIT;
307         }
308
309         mti->mti_attr.ma_need = MA_INODE;
310         mti->mti_attr.ma_valid = 0;
311
312         rc = mdt_attr_get_complex(mti, obj, &mti->mti_attr);
313         if (rc == -ENOENT)
314                 mdt_fake_ma(&mti->mti_attr);
315         mdt_pack_attr2body(mti, body, &mti->mti_attr.ma_attr,
316                            mdt_object_fid(obj));
317
318         mdt_object_put(mti->mti_env, obj);
319 }
320
321 typedef void (*mdt_reconstructor)(struct mdt_thread_info *mti,
322                                   struct mdt_lock_handle *lhc);
323
324 static mdt_reconstructor reconstructors[REINT_MAX] = {
325         [REINT_SETATTR]  = mdt_reconstruct_setattr,
326         [REINT_CREATE]   = mdt_reconstruct_create,
327         [REINT_LINK]     = mdt_reconstruct_generic,
328         [REINT_UNLINK]   = mdt_reconstruct_generic,
329         [REINT_RENAME]   = mdt_reconstruct_generic,
330         [REINT_OPEN]     = mdt_reconstruct_open,
331         [REINT_SETXATTR] = mdt_reconstruct_generic,
332         [REINT_RMENTRY]  = mdt_reconstruct_generic,
333         [REINT_MIGRATE] = mdt_reconstruct_generic
334 };
335
336 void mdt_reconstruct(struct mdt_thread_info *mti,
337                      struct mdt_lock_handle *lhc)
338 {
339         mdt_reconstructor reconst;
340         ENTRY;
341         LASSERT(mti->mti_rr.rr_opcode < REINT_MAX &&
342                 (reconst = reconstructors[mti->mti_rr.rr_opcode]) != NULL);
343         reconst(mti, lhc);
344         EXIT;
345 }